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;
46 unsigned int buf_size;
50 static av_cold int utvideo_decode_init(AVCodecContext *avctx)
52 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
54 int defined_fourcc = 0;
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.stripes = AV_RL32(avctx->extradata + 8);
66 info.flags = AV_RL32(avctx->extradata + 12);
68 /* Try to output the original format */
69 switch(UNFCC(info.original_format))
72 avctx->pix_fmt = PIX_FMT_YUV420P;
77 avctx->pix_fmt = PIX_FMT_YUYV422;
81 avctx->pix_fmt = PIX_FMT_UYVY422;
84 avctx->pix_fmt = PIX_FMT_BGR24;
87 avctx->pix_fmt = PIX_FMT_RGB32;
90 avctx->pix_fmt = PIX_FMT_ARGB;
93 /* Fall back on FOURCC */
94 switch(UNFCC(avctx->codec_tag))
97 avctx->pix_fmt = PIX_FMT_YUV420P;
98 defined_fourcc = UTVF_YV12;
101 avctx->pix_fmt = PIX_FMT_YUYV422;
102 defined_fourcc = UTVF_YUY2;
105 avctx->pix_fmt = PIX_FMT_BGR24;
106 defined_fourcc = UTVF_RGB24_WIN;
109 avctx->pix_fmt = PIX_FMT_RGB32;
110 defined_fourcc = UTVF_RGB32_WIN;
115 av_log(avctx, AV_LOG_ERROR,
116 "Codec ExtraData is Corrupt or Invalid: %X\n", info.original_format);
120 /* Only allocate the buffer once */
121 utv->buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
122 utv->output = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
124 if(utv->output == NULL)
126 av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
130 /* Allocate the output frame */
131 avctx->coded_frame = avcodec_alloc_frame();
133 /* Ut Video only supports 8-bit */
134 avctx->bits_per_raw_sample = 8;
136 /* Is it interlaced? */
137 avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
139 /* Apparently Ut Video doesn't store this info... */
140 avctx->coded_frame->top_field_first = 1;
143 * Create a Ut Video instance. Since the function wants
144 * an "interface name" string, pass it the name of the lib.
146 utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
148 /* Initialize Decoding */
149 utv->codec->DecodeBegin(defined_fourcc ? defined_fourcc : UNFCC(info.original_format),
150 avctx->width, avctx->height, CBGROSSWIDTH_WINDOWS, &info,
151 sizeof(UtVideoExtra));
156 static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
157 int *data_size, AVPacket *avpkt)
159 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
160 AVFrame *pic = avctx->coded_frame;
161 unsigned int w = avctx->width, h = avctx->height;
165 pic->pict_type = AV_PICTURE_TYPE_I;
168 /* Decode the frame */
169 utv->codec->DecodeFrame(utv->output, avpkt->data, true);
171 /* Set the output data depending on the colorspace */
172 switch(avctx->pix_fmt)
174 case PIX_FMT_YUV420P:
175 pic->linesize[0] = w;
176 pic->linesize[1] = pic->linesize[2] = w / 2;
177 pic->data[0] = utv->output;
178 pic->data[2] = utv->output + (w * h);
179 pic->data[1] = pic->data[2] + (w * h / 4);
181 case PIX_FMT_YUYV422:
182 case PIX_FMT_UYVY422:
183 pic->linesize[0] = w * 2;
184 pic->data[0] = utv->output;
188 /* Make the linesize negative, since Ut Video uses bottom-up BGR */
189 pic->linesize[0] = -1 * w * (avctx->pix_fmt == PIX_FMT_BGR24 ? 3 : 4);
190 pic->data[0] = utv->output + utv->buf_size + pic->linesize[0];
194 *data_size = sizeof(AVFrame);
195 *(AVFrame *)data = *pic;
200 static av_cold int utvideo_decode_close(AVCodecContext *avctx)
202 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
205 av_freep(&avctx->coded_frame);
206 av_freep(&utv->output);
208 /* Finish decoding and clean up the instance */
209 utv->codec->DecodeEnd();
210 CCodec::DeleteInstance(utv->codec);
215 AVCodec ff_libutvideo_decoder = {
219 sizeof(UtVideoContext),
222 utvideo_decode_close,
223 utvideo_decode_frame,
229 NULL_IF_CONFIG_SMALL("Ut Video"),