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)
32 #include <utvideo/utvideo.h>
33 #include <utvideo/Codec.h>
39 uint32_t original_format;
40 uint32_t frameinfo_size;
49 static av_cold int utvideo_decode_init(AVCodecContext *avctx)
51 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
55 unsigned int buf_size;
57 if (avctx->extradata_size != 4*4) {
58 av_log(avctx, AV_LOG_ERROR, "Extradata size mismatch.\n");
63 info.version = AV_RL32(avctx->extradata);
64 info.original_format = AV_RL32(avctx->extradata + 4);
65 info.frameinfo_size = AV_RL32(avctx->extradata + 8);
66 info.flags = AV_RL32(avctx->extradata + 12);
68 /* Pick format based on FOURCC */
69 switch (avctx->codec_tag) {
70 case MKTAG('U', 'L', 'Y', '0'):
71 avctx->pix_fmt = PIX_FMT_YUV420P;
74 case MKTAG('U', 'L', 'Y', '2'):
75 avctx->pix_fmt = PIX_FMT_YUYV422;
78 case MKTAG('U', 'L', 'R', 'G'):
79 avctx->pix_fmt = PIX_FMT_BGR24;
80 format = UTVF_RGB24_WIN;
82 case MKTAG('U', 'L', 'R', 'A'):
83 avctx->pix_fmt = PIX_FMT_RGB32;
84 format = UTVF_RGB32_WIN;
87 av_log(avctx, AV_LOG_ERROR,
88 "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
92 /* Only allocate the buffer once */
93 buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
94 utv->output = (uint8_t *)av_malloc(buf_size * sizeof(uint8_t));
96 if (utv->output == NULL) {
97 av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
101 /* Allocate the output frame */
102 avctx->coded_frame = avcodec_alloc_frame();
104 /* Ut Video only supports 8-bit */
105 avctx->bits_per_raw_sample = 8;
107 /* Is it interlaced? */
108 avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
110 /* Apparently Ut Video doesn't store this info... */
111 avctx->coded_frame->top_field_first = 1;
114 * Create a Ut Video instance. Since the function wants
115 * an "interface name" string, pass it the name of the lib.
117 utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
119 /* Initialize Decoding */
120 begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
121 CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
123 /* Check to see if the decoder initlized properly */
124 if (begin_ret != 0) {
125 av_log(avctx, AV_LOG_ERROR,
126 "Could not initialize decoder: %d\n", begin_ret);
133 static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
134 int *data_size, AVPacket *avpkt)
136 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
137 AVFrame *pic = avctx->coded_frame;
138 int w = avctx->width, h = avctx->height;
142 pic->pict_type = AV_PICTURE_TYPE_I;
145 /* Decode the frame */
146 utv->codec->DecodeFrame(utv->output, avpkt->data, true);
148 /* Set the output data depending on the colorspace */
149 switch (avctx->pix_fmt) {
150 case PIX_FMT_YUV420P:
151 pic->linesize[0] = w;
152 pic->linesize[1] = pic->linesize[2] = w / 2;
153 pic->data[0] = utv->output;
154 pic->data[2] = utv->output + (w * h);
155 pic->data[1] = pic->data[2] + (w * h / 4);
157 case PIX_FMT_YUYV422:
158 pic->linesize[0] = w * 2;
159 pic->data[0] = utv->output;
163 /* Make the linesize negative, since Ut Video uses bottom-up BGR */
164 pic->linesize[0] = -1 * w * (avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4);
165 pic->data[0] = utv->output + utv->buf_size + pic->linesize[0];
169 *data_size = sizeof(AVFrame);
170 *(AVFrame *)data = *pic;
175 static av_cold int utvideo_decode_close(AVCodecContext *avctx)
177 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
180 av_freep(&avctx->coded_frame);
181 av_freep(&utv->output);
183 /* Finish decoding and clean up the instance */
184 utv->codec->DecodeEnd();
185 CCodec::DeleteInstance(utv->codec);
190 AVCodec ff_libutvideo_decoder = {
194 sizeof(UtVideoContext),
197 utvideo_decode_close,
198 utvideo_decode_frame,
204 NULL_IF_CONFIG_SMALL("Ut Video"),