FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
libutvideodec.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Derek Buitenhuis
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 /**
22  * @file
23  * Known FOURCCs:
24  * 'ULY0' (YCbCr 4:2:0), 'ULY2' (YCbCr 4:2:2), 'ULRG' (RGB), 'ULRA' (RGBA)
25  */
26 
27 extern "C" {
28 #include "avcodec.h"
29 }
30 
31 #include "libutvideo.h"
32 #include "get_bits.h"
33 
35 {
36  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
37  UtVideoExtra info;
38  int format;
39  int begin_ret;
40 
41  if (avctx->extradata_size != 4*4) {
42  av_log(avctx, AV_LOG_ERROR, "Extradata size mismatch.\n");
43  return -1;
44  }
45 
46  /* Read extradata */
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);
51 
52  /* Pick format based on FOURCC */
53  switch (avctx->codec_tag) {
54  case MKTAG('U', 'L', 'Y', '0'):
55  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
56  format = UTVF_YV12;
57  break;
58  case MKTAG('U', 'L', 'Y', '2'):
59  avctx->pix_fmt = AV_PIX_FMT_YUYV422;
60  format = UTVF_YUY2;
61  break;
62  case MKTAG('U', 'L', 'R', 'G'):
63  avctx->pix_fmt = AV_PIX_FMT_BGR24;
64  format = UTVF_RGB24_WIN;
65  break;
66  case MKTAG('U', 'L', 'R', 'A'):
67  avctx->pix_fmt = AV_PIX_FMT_RGB32;
68  format = UTVF_RGB32_WIN;
69  break;
70  default:
71  av_log(avctx, AV_LOG_ERROR,
72  "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
73  return -1;
74  }
75 
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));
79 
80  if (utv->buffer == NULL) {
81  av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
82  return -1;
83  }
84 
85  /* Allocate the output frame */
87 
88  /* Ut Video only supports 8-bit */
89  avctx->bits_per_raw_sample = 8;
90 
91  /* Is it interlaced? */
92  avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
93 
94  /* Apparently Ut Video doesn't store this info... */
95  avctx->coded_frame->top_field_first = 1;
96 
97  /*
98  * Create a Ut Video instance. Since the function wants
99  * an "interface name" string, pass it the name of the lib.
100  */
101  utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
102 
103  /* Initialize Decoding */
104  begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
105  CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
106 
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);
111  return -1;
112  }
113 
114  return 0;
115 }
116 
117 static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
118  int *got_frame, AVPacket *avpkt)
119 {
120  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
121  AVFrame *pic = avctx->coded_frame;
122  int w = avctx->width, h = avctx->height;
123 
124  /* Set flags */
125  pic->reference = 0;
126  pic->pict_type = AV_PICTURE_TYPE_I;
127  pic->key_frame = 1;
128 
129  /* Decode the frame */
130  utv->codec->DecodeFrame(utv->buffer, avpkt->data, true);
131 
132  /* Set the output data depending on the colorspace */
133  switch (avctx->pix_fmt) {
134  case AV_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);
140  break;
141  case AV_PIX_FMT_YUYV422:
142  pic->linesize[0] = w * 2;
143  pic->data[0] = utv->buffer;
144  break;
145  case AV_PIX_FMT_BGR24:
146  case AV_PIX_FMT_RGB32:
147  /* Make the linesize negative, since Ut Video uses bottom-up BGR */
148  pic->linesize[0] = -1 * w * (avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4);
149  pic->data[0] = utv->buffer + utv->buf_size + pic->linesize[0];
150  break;
151  }
152 
153  *got_frame = 1;
154  *(AVFrame *)data = *pic;
155 
156  return avpkt->size;
157 }
158 
160 {
161  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
162 
163  /* Free output */
164  av_freep(&avctx->coded_frame);
165  av_freep(&utv->buffer);
166 
167  /* Finish decoding and clean up the instance */
168  utv->codec->DecodeEnd();
169  CCodec::DeleteInstance(utv->codec);
170 
171  return 0;
172 }
173 
175  "libutvideo",
176  NULL_IF_CONFIG_SMALL("Ut Video"),
179  0, //capabilities
180  NULL, //supported_framerates
181  NULL, //pix_fmts
182  NULL, //supported_samplerates
183  NULL, //sample_fmts
184  NULL, //channel_layouts
185  0, //max_lowres
186  NULL, //priv_class
187  NULL, //profiles
188  sizeof(UtVideoContext),
189  NULL, //next
190  NULL, //init_thread_copy
191  NULL, //update_thread_context
192  NULL, //defaults
193  NULL, //init_static_data
195  NULL, //encode
196  NULL, //encode2
199 };