libutvideodec.cpp 5.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Copyright (c) 2011 Derek Buitenhuis
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation;
 * version 2 of the License.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * Known FOURCCs:
 *     'ULY0' (YCbCr 4:2:0), 'ULY2' (YCbCr 4:2:2), 'ULRG' (RGB), 'ULRA' (RGBA)
 */

extern "C" {
#include "avcodec.h"
}

31
#include "libutvideo.h"
32 33 34 35 36 37
#include "get_bits.h"

static av_cold int utvideo_decode_init(AVCodecContext *avctx)
{
    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
    UtVideoExtra info;
38
    int format;
39
    int begin_ret;
40

41
    if (avctx->extradata_size != 4*4) {
42 43 44 45 46 47 48
        av_log(avctx, AV_LOG_ERROR, "Extradata size mismatch.\n");
        return -1;
    }

    /* Read extradata */
    info.version = AV_RL32(avctx->extradata);
    info.original_format = AV_RL32(avctx->extradata + 4);
49
    info.frameinfo_size = AV_RL32(avctx->extradata + 8);
50 51
    info.flags = AV_RL32(avctx->extradata + 12);

52
    /* Pick format based on FOURCC */
53 54
    switch (avctx->codec_tag) {
    case MKTAG('U', 'L', 'Y', '0'):
55
        avctx->pix_fmt = AV_PIX_FMT_YUV420P;
56 57 58
        format = UTVF_YV12;
        break;
    case MKTAG('U', 'L', 'Y', '2'):
59
        avctx->pix_fmt = AV_PIX_FMT_YUYV422;
60 61 62
        format = UTVF_YUY2;
        break;
    case MKTAG('U', 'L', 'R', 'G'):
63
        avctx->pix_fmt = AV_PIX_FMT_BGR24;
64
        format = UTVF_NFCC_BGR_BU;
65 66
        break;
    case MKTAG('U', 'L', 'R', 'A'):
67
        avctx->pix_fmt = AV_PIX_FMT_RGB32;
68
        format = UTVF_NFCC_BGRA_BU;
69 70 71 72 73
        break;
    default:
        av_log(avctx, AV_LOG_ERROR,
              "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
        return -1;
74 75 76
    }

    /* Only allocate the buffer once */
77
    utv->buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
78
    utv->buffer = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
79

80
    if (utv->buffer == NULL) {
81 82 83 84
        av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
        return -1;
    }

85
    /* Allocate the output frame */
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    avctx->coded_frame = avcodec_alloc_frame();

    /* Ut Video only supports 8-bit */
    avctx->bits_per_raw_sample = 8;

    /* Is it interlaced? */
    avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;

    /* Apparently Ut Video doesn't store this info... */
    avctx->coded_frame->top_field_first = 1;

    /*
     * Create a Ut Video instance. Since the function wants
     * an "interface name" string, pass it the name of the lib.
     */
    utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");

    /* Initialize Decoding */
104
    begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
105
                            CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
106

107
    /* Check to see if the decoder initlized properly */
108
    if (begin_ret != 0) {
109 110 111 112 113
        av_log(avctx, AV_LOG_ERROR,
               "Could not initialize decoder: %d\n", begin_ret);
        return -1;
    }

114 115 116 117
    return 0;
}

static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
118
                                int *got_frame, AVPacket *avpkt)
119 120 121
{
    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
    AVFrame *pic = avctx->coded_frame;
122
    int w = avctx->width, h = avctx->height;
123 124 125 126 127 128 129

    /* Set flags */
    pic->reference = 0;
    pic->pict_type = AV_PICTURE_TYPE_I;
    pic->key_frame = 1;

    /* Decode the frame */
130
    utv->codec->DecodeFrame(utv->buffer, avpkt->data, true);
131 132

    /* Set the output data depending on the colorspace */
133
    switch (avctx->pix_fmt) {
134
    case AV_PIX_FMT_YUV420P:
135 136
        pic->linesize[0] = w;
        pic->linesize[1] = pic->linesize[2] = w / 2;
137 138
        pic->data[0] = utv->buffer;
        pic->data[2] = utv->buffer + (w * h);
139 140
        pic->data[1] = pic->data[2] + (w * h / 4);
        break;
141
    case AV_PIX_FMT_YUYV422:
142
        pic->linesize[0] = w * 2;
143
        pic->data[0] = utv->buffer;
144
        break;
145 146
    case AV_PIX_FMT_BGR24:
    case AV_PIX_FMT_RGB32:
147
        /* Make the linesize negative, since Ut Video uses bottom-up BGR */
148
        pic->linesize[0] = -1 * w * (avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4);
149
        pic->data[0] = utv->buffer + utv->buf_size + pic->linesize[0];
150 151
        break;
    }
152

153
    *got_frame = 1;
154 155 156 157 158 159 160 161 162 163 164
    *(AVFrame *)data = *pic;

    return avpkt->size;
}

static av_cold int utvideo_decode_close(AVCodecContext *avctx)
{
    UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;

    /* Free output */
    av_freep(&avctx->coded_frame);
165
    av_freep(&utv->buffer);
166 167 168 169 170 171 172 173 174

    /* Finish decoding and clean up the instance */
    utv->codec->DecodeEnd();
    CCodec::DeleteInstance(utv->codec);

    return 0;
}

AVCodec ff_libutvideo_decoder = {
175
    "libutvideo",
176
    NULL_IF_CONFIG_SMALL("Ut Video"),
177
    AVMEDIA_TYPE_VIDEO,
178
    AV_CODEC_ID_UTVIDEO,
179 180 181 182 183 184 185 186 187
    0,    //capabilities
    NULL, //supported_framerates
    NULL, //pix_fmts
    NULL, //supported_samplerates
    NULL, //sample_fmts
    NULL, //channel_layouts
    0,    //max_lowres
    NULL, //priv_class
    NULL, //profiles
188
    sizeof(UtVideoContext),
189 190 191 192 193
    NULL, //next
    NULL, //init_thread_copy
    NULL, //update_thread_context
    NULL, //defaults
    NULL, //init_static_data
194
    utvideo_decode_init,
195 196
    NULL, //encode
    NULL, //encode2
197
    utvideo_decode_frame,
198
    utvideo_decode_close,
199
};