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),
25 * 'ULH0' (YCbCr 4:2:0 BT.709), 'ULH2' (YCbCr 4:2:2 BT.709)
30 #include "libavutil/imgutils.h"
33 #include "libutvideo.h"
36 static av_cold int utvideo_decode_init(AVCodecContext *avctx)
38 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
43 if (avctx->extradata_size != 16 && avctx->extradata_size != 8 ) {
44 av_log(avctx, AV_LOG_ERROR, "Extradata size (%d) mismatch.\n", avctx->extradata_size);
49 info.version = AV_RL32(avctx->extradata);
50 info.original_format = AV_RL32(avctx->extradata + 4);
51 info.frameinfo_size = AV_RL32(avctx->extradata + 8);
52 info.flags = AV_RL32(avctx->extradata + 12);
54 /* Pick format based on FOURCC */
55 switch (avctx->codec_tag) {
57 case MKTAG('U', 'L', 'H', '0'):
58 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
59 avctx->colorspace = AVCOL_SPC_BT709;
62 case MKTAG('U', 'L', 'H', '2'):
63 avctx->pix_fmt = AV_PIX_FMT_YUYV422;
64 avctx->colorspace = AVCOL_SPC_BT709;
68 case MKTAG('U', 'L', 'Y', '0'):
69 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
72 case MKTAG('U', 'L', 'Y', '2'):
73 avctx->pix_fmt = AV_PIX_FMT_YUYV422;
76 case MKTAG('U', 'L', 'R', 'G'):
77 avctx->pix_fmt = AV_PIX_FMT_BGR24;
78 format = UTVF_NFCC_BGR_BU;
80 case MKTAG('U', 'L', 'R', 'A'):
81 avctx->pix_fmt = AV_PIX_FMT_RGB32;
82 format = UTVF_NFCC_BGRA_BU;
85 case MKTAG('U', 'Q', 'Y', '2'):
86 avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
91 av_log(avctx, AV_LOG_ERROR,
92 "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
96 /* Only allocate the buffer once */
97 utv->buf_size = av_image_get_buffer_size(avctx->pix_fmt, avctx->width, avctx->height, 1);
99 if (format == UTVF_v210)
100 utv->buf_size += avctx->height * ((avctx->width + 47) / 48) * 128; // the linesize used by the decoder, this does not seem to be exported
102 utv->buffer = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
104 if (utv->buffer == NULL) {
105 av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
109 /* Allocate the output frame */
110 avctx->coded_frame = av_frame_alloc();
112 /* Is it interlaced? */
113 avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
115 /* Apparently Ut Video doesn't store this info... */
116 avctx->coded_frame->top_field_first = 1;
119 * Create a Ut Video instance. Since the function wants
120 * an "interface name" string, pass it the name of the lib.
122 utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
124 /* Initialize Decoding */
125 begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
126 CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
128 /* Check to see if the decoder initlized properly */
129 if (begin_ret != 0) {
130 av_log(avctx, AV_LOG_ERROR,
131 "Could not initialize decoder: %d\n", begin_ret);
138 static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
139 int *got_frame, AVPacket *avpkt)
141 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
142 AVFrame *pic = avctx->coded_frame;
143 int w = avctx->width, h = avctx->height;
146 pic->pict_type = AV_PICTURE_TYPE_I;
149 /* Decode the frame */
150 utv->codec->DecodeFrame(utv->buffer, avpkt->data, true);
152 /* Set the output data depending on the colorspace */
153 switch (avctx->pix_fmt) {
154 case AV_PIX_FMT_YUV420P:
155 pic->linesize[0] = w;
156 pic->linesize[1] = pic->linesize[2] = w / 2;
157 pic->data[0] = utv->buffer;
158 pic->data[2] = utv->buffer + (w * h);
159 pic->data[1] = pic->data[2] + (w * h / 4);
161 case AV_PIX_FMT_YUYV422:
162 pic->linesize[0] = w * 2;
163 pic->data[0] = utv->buffer;
165 case AV_PIX_FMT_YUV422P10: {
168 int linesize = ((w + 47) / 48) * 128;
170 pic->linesize[0] = w * 2;
172 pic->linesize[2] = w;
173 pic->data[0] = utv->buffer + linesize * h;
174 pic->data[1] = pic->data[0] + h*pic->linesize[0];
175 pic->data[2] = pic->data[1] + h*pic->linesize[1];
176 y = (uint16_t*)pic->data[0];
177 u = (uint16_t*)pic->data[1];
178 v = (uint16_t*)pic->data[2];
179 for (j = 0; j < h; j++) {
180 const uint8_t *in = utv->buffer + j * linesize;
182 for (i = 0; i + 1 < w; i += 6, in += 4) {
188 *y++ = (a>>10) & 0x3FF;
189 *v++ = (a>>20) & 0x3FF;
197 *u++ = (b>>10) & 0x3FF;
198 *y++ = (b>>20) & 0x3FF;
200 *y++ = (a>>10) & 0x3FF;
207 *u++ = (a>>20) & 0x3FF;
209 *v++ = (b>>10) & 0x3FF;
210 *y++ = (b>>20) & 0x3FF;
215 case AV_PIX_FMT_BGR24:
216 case AV_PIX_FMT_RGB32:
217 /* Make the linesize negative, since Ut Video uses bottom-up BGR */
218 pic->linesize[0] = -1 * w * (avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4);
219 pic->data[0] = utv->buffer + utv->buf_size + pic->linesize[0];
224 pic->format = avctx->pix_fmt;
226 if (avctx->refcounted_frames) {
227 int ret = av_frame_ref((AVFrame*)data, pic);
231 av_frame_move_ref((AVFrame*)data, pic);
239 static av_cold int utvideo_decode_close(AVCodecContext *avctx)
241 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
244 av_frame_free(&avctx->coded_frame);
245 av_freep(&utv->buffer);
247 /* Finish decoding and clean up the instance */
248 utv->codec->DecodeEnd();
249 CCodec::DeleteInstance(utv->codec);
254 AVCodec ff_libutvideo_decoder = {
256 NULL_IF_CONFIG_SMALL("Ut Video"),
260 NULL, //supported_framerates
262 NULL, //supported_samplerates
264 NULL, //channel_layouts
268 sizeof(UtVideoContext),
270 NULL, //init_thread_copy
271 NULL, //update_thread_context
273 NULL, //init_static_data
277 utvideo_decode_frame,
278 utvideo_decode_close,