2 * Copyright (c) 2011 Derek Buitenhuis
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation;
9 * version 2 of the License.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * 'ULY0' (YCbCr 4:2:0), 'ULY2' (YCbCr 4:2:2), 'ULRG' (RGB), 'ULRA' (RGBA)
31 #include "libutvideo.h"
34 static av_cold int utvideo_decode_init(AVCodecContext *avctx)
36 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
41 if (avctx->extradata_size != 4*4) {
42 av_log(avctx, AV_LOG_ERROR, "Extradata size mismatch.\n");
47 info.version = AV_RL32(avctx->extradata);
48 info.original_format = AV_RL32(avctx->extradata + 4);
49 info.frameinfo_size = AV_RL32(avctx->extradata + 8);
50 info.flags = AV_RL32(avctx->extradata + 12);
52 /* Pick format based on FOURCC */
53 switch (avctx->codec_tag) {
54 case MKTAG('U', 'L', 'Y', '0'):
55 avctx->pix_fmt = PIX_FMT_YUV420P;
58 case MKTAG('U', 'L', 'Y', '2'):
59 avctx->pix_fmt = PIX_FMT_YUYV422;
62 case MKTAG('U', 'L', 'R', 'G'):
63 avctx->pix_fmt = PIX_FMT_BGR24;
64 format = UTVF_RGB24_WIN;
66 case MKTAG('U', 'L', 'R', 'A'):
67 avctx->pix_fmt = PIX_FMT_RGB32;
68 format = UTVF_RGB32_WIN;
71 av_log(avctx, AV_LOG_ERROR,
72 "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
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));
80 if (utv->buffer == NULL) {
81 av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
85 /* Allocate the output frame */
86 avctx->coded_frame = avcodec_alloc_frame();
88 /* Ut Video only supports 8-bit */
89 avctx->bits_per_raw_sample = 8;
91 /* Is it interlaced? */
92 avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
94 /* Apparently Ut Video doesn't store this info... */
95 avctx->coded_frame->top_field_first = 1;
98 * Create a Ut Video instance. Since the function wants
99 * an "interface name" string, pass it the name of the lib.
101 utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
103 /* Initialize Decoding */
104 begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
105 CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
107 /* Check to see if the decoder initlized properly */
108 if (begin_ret != 0) {
109 av_log(avctx, AV_LOG_ERROR,
110 "Could not initialize decoder: %d\n", begin_ret);
117 static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
118 int *data_size, AVPacket *avpkt)
120 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
121 AVFrame *pic = avctx->coded_frame;
122 int w = avctx->width, h = avctx->height;
126 pic->pict_type = AV_PICTURE_TYPE_I;
129 /* Decode the frame */
130 utv->codec->DecodeFrame(utv->buffer, avpkt->data, true);
132 /* Set the output data depending on the colorspace */
133 switch (avctx->pix_fmt) {
134 case PIX_FMT_YUV420P:
135 pic->linesize[0] = w;
136 pic->linesize[1] = pic->linesize[2] = w / 2;
137 pic->data[0] = utv->buffer;
138 pic->data[2] = utv->buffer + (w * h);
139 pic->data[1] = pic->data[2] + (w * h / 4);
141 case PIX_FMT_YUYV422:
142 pic->linesize[0] = w * 2;
143 pic->data[0] = utv->buffer;
147 /* Make the linesize negative, since Ut Video uses bottom-up BGR */
148 pic->linesize[0] = -1 * w * (avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4);
149 pic->data[0] = utv->buffer + utv->buf_size + pic->linesize[0];
153 *data_size = sizeof(AVFrame);
154 *(AVFrame *)data = *pic;
159 static av_cold int utvideo_decode_close(AVCodecContext *avctx)
161 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
164 av_freep(&avctx->coded_frame);
165 av_freep(&utv->buffer);
167 /* Finish decoding and clean up the instance */
168 utv->codec->DecodeEnd();
169 CCodec::DeleteInstance(utv->codec);
174 AVCodec ff_libutvideo_decoder = {
176 NULL_IF_CONFIG_SMALL("Ut Video"),
180 NULL, //supported_framerates
182 NULL, //supported_samplerates
184 NULL, //channel_layouts
188 sizeof(UtVideoContext),
190 NULL, //init_thread_copy
191 NULL, //update_thread_context
193 NULL, //init_static_data
197 utvideo_decode_frame,
198 utvideo_decode_close,