FFmpeg  2.1.1
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  * 'ULH0' (YCbCr 4:2:0 BT.709), 'ULH2' (YCbCr 4:2:2 BT.709)
26  */
27 
28 extern "C" {
29 #include "avcodec.h"
30 }
31 
32 #include "libutvideo.h"
33 #include "get_bits.h"
34 
36 {
37  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
38  UtVideoExtra info;
39  int format;
40  int begin_ret;
41 
42  if (avctx->extradata_size != 4*4) {
43  av_log(avctx, AV_LOG_ERROR, "Extradata size mismatch.\n");
44  return -1;
45  }
46 
47  /* Read extradata */
48  info.version = AV_RL32(avctx->extradata);
49  info.original_format = AV_RL32(avctx->extradata + 4);
50  info.frameinfo_size = AV_RL32(avctx->extradata + 8);
51  info.flags = AV_RL32(avctx->extradata + 12);
52 
53  /* Pick format based on FOURCC */
54  switch (avctx->codec_tag) {
55 #ifdef UTV_BT709
56  case MKTAG('U', 'L', 'H', '0'):
57  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
58  avctx->colorspace = AVCOL_SPC_BT709;
59  format = UTVF_YV12;
60  break;
61  case MKTAG('U', 'L', 'H', '2'):
62  avctx->pix_fmt = AV_PIX_FMT_YUYV422;
63  avctx->colorspace = AVCOL_SPC_BT709;
64  format = UTVF_YUY2;
65  break;
66 #endif
67  case MKTAG('U', 'L', 'Y', '0'):
68  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
69  format = UTVF_YV12;
70  break;
71  case MKTAG('U', 'L', 'Y', '2'):
72  avctx->pix_fmt = AV_PIX_FMT_YUYV422;
73  format = UTVF_YUY2;
74  break;
75  case MKTAG('U', 'L', 'R', 'G'):
76  avctx->pix_fmt = AV_PIX_FMT_BGR24;
77  format = UTVF_NFCC_BGR_BU;
78  break;
79  case MKTAG('U', 'L', 'R', 'A'):
80  avctx->pix_fmt = AV_PIX_FMT_RGB32;
81  format = UTVF_NFCC_BGRA_BU;
82  break;
83  default:
84  av_log(avctx, AV_LOG_ERROR,
85  "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
86  return -1;
87  }
88 
89  /* Only allocate the buffer once */
90  utv->buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
91  utv->buffer = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
92 
93  if (utv->buffer == NULL) {
94  av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
95  return -1;
96  }
97 
98  /* Allocate the output frame */
100 
101  /* Ut Video only supports 8-bit */
102  avctx->bits_per_raw_sample = 8;
103 
104  /* Is it interlaced? */
105  avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
106 
107  /* Apparently Ut Video doesn't store this info... */
108  avctx->coded_frame->top_field_first = 1;
109 
110  /*
111  * Create a Ut Video instance. Since the function wants
112  * an "interface name" string, pass it the name of the lib.
113  */
114  utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
115 
116  /* Initialize Decoding */
117  begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
118  CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
119 
120  /* Check to see if the decoder initlized properly */
121  if (begin_ret != 0) {
122  av_log(avctx, AV_LOG_ERROR,
123  "Could not initialize decoder: %d\n", begin_ret);
124  return -1;
125  }
126 
127  return 0;
128 }
129 
130 static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
131  int *got_frame, AVPacket *avpkt)
132 {
133  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
134  AVFrame *pic = avctx->coded_frame;
135  int w = avctx->width, h = avctx->height;
136 
137  /* Set flags */
138  pic->reference = 0;
139  pic->pict_type = AV_PICTURE_TYPE_I;
140  pic->key_frame = 1;
141 
142  /* Decode the frame */
143  utv->codec->DecodeFrame(utv->buffer, avpkt->data, true);
144 
145  /* Set the output data depending on the colorspace */
146  switch (avctx->pix_fmt) {
147  case AV_PIX_FMT_YUV420P:
148  pic->linesize[0] = w;
149  pic->linesize[1] = pic->linesize[2] = w / 2;
150  pic->data[0] = utv->buffer;
151  pic->data[2] = utv->buffer + (w * h);
152  pic->data[1] = pic->data[2] + (w * h / 4);
153  break;
154  case AV_PIX_FMT_YUYV422:
155  pic->linesize[0] = w * 2;
156  pic->data[0] = utv->buffer;
157  break;
158  case AV_PIX_FMT_BGR24:
159  case AV_PIX_FMT_RGB32:
160  /* Make the linesize negative, since Ut Video uses bottom-up BGR */
161  pic->linesize[0] = -1 * w * (avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4);
162  pic->data[0] = utv->buffer + utv->buf_size + pic->linesize[0];
163  break;
164  }
165 
166  *got_frame = 1;
167  *(AVFrame *)data = *pic;
168 
169  return avpkt->size;
170 }
171 
173 {
174  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
175 
176  /* Free output */
177  av_freep(&avctx->coded_frame);
178  av_freep(&utv->buffer);
179 
180  /* Finish decoding and clean up the instance */
181  utv->codec->DecodeEnd();
182  CCodec::DeleteInstance(utv->codec);
183 
184  return 0;
185 }
186 
188  "libutvideo",
189  NULL_IF_CONFIG_SMALL("Ut Video"),
192  0, //capabilities
193  NULL, //supported_framerates
194  NULL, //pix_fmts
195  NULL, //supported_samplerates
196  NULL, //sample_fmts
197  NULL, //channel_layouts
198  0, //max_lowres
199  NULL, //priv_class
200  NULL, //profiles
201  sizeof(UtVideoContext),
202  NULL, //next
203  NULL, //init_thread_copy
204  NULL, //update_thread_context
205  NULL, //defaults
206  NULL, //init_static_data
208  NULL, //encode
209  NULL, //encode2
212 };
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
Definition: utils.c:1071
int size
Definition: avcodec.h:1064
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2570
AVCodec.
Definition: avcodec.h:2922
#define av_cold
Definition: avcodec.h:653
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1254
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
Definition: avfilter.c:965
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: avcodec.h:4537
uint8_t
uint8_t * buffer
Definition: libutvideo.h:67
const char data[16]
Definition: mxf.c:68
bitstream reader API header.
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:293
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
Libavcodec external API header.
int width
picture width / height.
Definition: avcodec.h:1314
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
#define AV_PIX_FMT_RGB32
Definition: avcodec.h:4928
Known FOURCCs: 'ULY0' (YCbCr 4:2:0), 'ULY2' (YCbCr 4:2:2), 'ULRG' (RGB), 'ULRA' (RGBA), 'ULH0' (YCbCr 4:2:0 BT.709), 'ULH2' (YCbCr 4:2:2 BT.709)
#define UTVF_NFCC_BGR_BU
Definition: libutvideo.h:42
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: avcodec.h:4534
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: avcodec.h:4535
main external API structure.
Definition: avcodec.h:1146
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1172
int extradata_size
Definition: avcodec.h:1255
static av_cold int utvideo_decode_close(AVCodecContext *avctx)
#define MKTAG(a, b, c, d)
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1841
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2588
uint8_t * data
Definition: avcodec.h:1063
void * priv_data
Definition: avcodec.h:1182
unsigned int buf_size
Definition: libutvideo.h:66
CCodec * codec
Definition: libutvideo.h:65
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:298
#define AV_RL32(x)
Definition: intreadwrite.h:275
int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
Calculate the size in bytes that a picture of the given width and height would occupy if stored in th...
Definition: avpicture.c:49
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: frame.h:35
static av_cold int utvideo_decode_init(AVCodecContext *avctx)
AVCodec ff_libutvideo_decoder
static int utvideo_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
This structure stores compressed data.
Definition: avcodec.h:1040
#define UTVF_NFCC_BGRA_BU
Definition: libutvideo.h:46