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;
54 unsigned int buf_size;
56 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)
71 case MKTAG('U', 'L', 'Y', '0'):
72 avctx->pix_fmt = PIX_FMT_YUV420P;
75 case MKTAG('U', 'L', 'Y', '2'):
76 avctx->pix_fmt = PIX_FMT_YUYV422;
79 case MKTAG('U', 'L', 'R', 'G'):
80 avctx->pix_fmt = PIX_FMT_BGR24;
81 format = UTVF_RGB24_WIN;
83 case MKTAG('U', 'L', 'R', 'A'):
84 avctx->pix_fmt = PIX_FMT_RGB32;
85 format = UTVF_RGB32_WIN;
88 av_log(avctx, AV_LOG_ERROR,
89 "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
93 /* Only allocate the buffer once */
94 buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
95 utv->output = (uint8_t *)av_malloc(buf_size * sizeof(uint8_t));
97 if(utv->output == NULL)
99 av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
103 /* Allocate the output frame */
104 avctx->coded_frame = avcodec_alloc_frame();
106 /* Ut Video only supports 8-bit */
107 avctx->bits_per_raw_sample = 8;
109 /* Is it interlaced? */
110 avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
112 /* Apparently Ut Video doesn't store this info... */
113 avctx->coded_frame->top_field_first = 1;
116 * Create a Ut Video instance. Since the function wants
117 * an "interface name" string, pass it the name of the lib.
119 utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
121 /* Initialize Decoding */
122 utv->codec->DecodeBegin(format, avctx->width, avctx->height,
123 CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
128 static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
129 int *data_size, AVPacket *avpkt)
131 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
132 AVFrame *pic = avctx->coded_frame;
133 int w = avctx->width, h = avctx->height;
137 pic->pict_type = AV_PICTURE_TYPE_I;
140 /* Decode the frame */
141 utv->codec->DecodeFrame(utv->output, avpkt->data, true);
143 /* Set the output data depending on the colorspace */
144 switch(avctx->pix_fmt)
146 case PIX_FMT_YUV420P:
147 pic->linesize[0] = w;
148 pic->linesize[1] = pic->linesize[2] = w / 2;
149 pic->data[0] = utv->output;
150 pic->data[2] = utv->output + (w * h);
151 pic->data[1] = pic->data[2] + (w * h / 4);
153 case PIX_FMT_YUYV422:
154 pic->linesize[0] = w * 2;
155 pic->data[0] = utv->output;
159 /* Make the linesize negative, since Ut Video uses bottom-up BGR */
160 pic->linesize[0] = -1 * w * (avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4);
161 pic->data[0] = utv->output + utv->buf_size + pic->linesize[0];
165 *data_size = sizeof(AVFrame);
166 *(AVFrame *)data = *pic;
171 static av_cold int utvideo_decode_close(AVCodecContext *avctx)
173 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
176 av_freep(&avctx->coded_frame);
177 av_freep(&utv->output);
179 /* Finish decoding and clean up the instance */
180 utv->codec->DecodeEnd();
181 CCodec::DeleteInstance(utv->codec);
186 AVCodec ff_libutvideo_decoder = {
190 sizeof(UtVideoContext),
193 utvideo_decode_close,
194 utvideo_decode_frame,
200 NULL_IF_CONFIG_SMALL("Ut Video"),