FFmpeg
 All Data Structures Namespaces 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  * '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 != 16 && avctx->extradata_size != 8 ) {
43  av_log(avctx, AV_LOG_ERROR, "Extradata size (%d) mismatch.\n", avctx->extradata_size);
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;
78  break;
79  case MKTAG('U', 'L', 'R', 'A'):
80  avctx->pix_fmt = AV_PIX_FMT_RGB32;
82  break;
83 #ifdef UTVF_UQY2
84  case MKTAG('U', 'Q', 'Y', '2'):
86  format = UTVF_v210;
87  break;
88 #endif
89  default:
90  av_log(avctx, AV_LOG_ERROR,
91  "Not a Ut Video FOURCC: %X\n", avctx->codec_tag);
92  return -1;
93  }
94 
95  /* Only allocate the buffer once */
96  utv->buf_size = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
97 #ifdef UTVF_UQY2
98  if (format == UTVF_v210)
99  utv->buf_size += avctx->height * ((avctx->width + 47) / 48) * 128; // the linesize used by the decoder, this does not seem to be exported
100 #endif
101  utv->buffer = (uint8_t *)av_malloc(utv->buf_size * sizeof(uint8_t));
102 
103  if (utv->buffer == NULL) {
104  av_log(avctx, AV_LOG_ERROR, "Unable to allocate output buffer.\n");
105  return -1;
106  }
107 
108  /* Allocate the output frame */
109  avctx->coded_frame = av_frame_alloc();
110 
111  /* Ut Video only supports 8-bit */
112  avctx->bits_per_raw_sample = 8;
113 
114  /* Is it interlaced? */
115  avctx->coded_frame->interlaced_frame = info.flags & 0x800 ? 1 : 0;
116 
117  /* Apparently Ut Video doesn't store this info... */
118  avctx->coded_frame->top_field_first = 1;
119 
120  /*
121  * Create a Ut Video instance. Since the function wants
122  * an "interface name" string, pass it the name of the lib.
123  */
124  utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
125 
126  /* Initialize Decoding */
127  begin_ret = utv->codec->DecodeBegin(format, avctx->width, avctx->height,
128  CBGROSSWIDTH_WINDOWS, &info, sizeof(UtVideoExtra));
129 
130  /* Check to see if the decoder initlized properly */
131  if (begin_ret != 0) {
132  av_log(avctx, AV_LOG_ERROR,
133  "Could not initialize decoder: %d\n", begin_ret);
134  return -1;
135  }
136 
137  return 0;
138 }
139 
140 static int utvideo_decode_frame(AVCodecContext *avctx, void *data,
141  int *got_frame, AVPacket *avpkt)
142 {
143  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
144  AVFrame *pic = avctx->coded_frame;
145  int w = avctx->width, h = avctx->height;
146 
147  /* Set flags */
148  pic->pict_type = AV_PICTURE_TYPE_I;
149  pic->key_frame = 1;
150 
151  /* Decode the frame */
152  utv->codec->DecodeFrame(utv->buffer, avpkt->data, true);
153 
154  /* Set the output data depending on the colorspace */
155  switch (avctx->pix_fmt) {
156  case AV_PIX_FMT_YUV420P:
157  pic->linesize[0] = w;
158  pic->linesize[1] = pic->linesize[2] = w / 2;
159  pic->data[0] = utv->buffer;
160  pic->data[2] = utv->buffer + (w * h);
161  pic->data[1] = pic->data[2] + (w * h / 4);
162  break;
163  case AV_PIX_FMT_YUYV422:
164  pic->linesize[0] = w * 2;
165  pic->data[0] = utv->buffer;
166  break;
167  case AV_PIX_FMT_YUV422P10: {
168  uint16_t *y, *u, *v;
169  int i,j;
170  int linesize = ((w + 47) / 48) * 128;
171 
172  pic->linesize[0] = w * 2;
173  pic->linesize[1] =
174  pic->linesize[2] = w;
175  pic->data[0] = utv->buffer + linesize * h;
176  pic->data[1] = pic->data[0] + h*pic->linesize[0];
177  pic->data[2] = pic->data[1] + h*pic->linesize[1];
178  y = (uint16_t*)pic->data[0];
179  u = (uint16_t*)pic->data[1];
180  v = (uint16_t*)pic->data[2];
181  for (j = 0; j < h; j++) {
182  const uint8_t *in = utv->buffer + j * linesize;
183 
184  for (i = 0; i + 1 < w; i += 6, in += 4) {
185  unsigned a,b;
186  a = AV_RL32(in);
187  in += 4;
188  b = AV_RL32(in);
189  *u++ = (a ) & 0x3FF;
190  *y++ = (a>>10) & 0x3FF;
191  *v++ = (a>>20) & 0x3FF;
192  *y++ = (b ) & 0x3FF;
193 
194  if (i + 3 >= w)
195  break;
196 
197  in += 4;
198  a = AV_RL32(in);
199  *u++ = (b>>10) & 0x3FF;
200  *y++ = (b>>20) & 0x3FF;
201  *v++ = (a ) & 0x3FF;
202  *y++ = (a>>10) & 0x3FF;
203 
204  if (i + 5 >= w)
205  break;
206 
207  in += 4;
208  b = AV_RL32(in);
209  *u++ = (a>>20) & 0x3FF;
210  *y++ = (b ) & 0x3FF;
211  *v++ = (b>>10) & 0x3FF;
212  *y++ = (b>>20) & 0x3FF;
213  }
214  }
215  break;
216  }
217  case AV_PIX_FMT_BGR24:
218  case AV_PIX_FMT_RGB32:
219  /* Make the linesize negative, since Ut Video uses bottom-up BGR */
220  pic->linesize[0] = -1 * w * (avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4);
221  pic->data[0] = utv->buffer + utv->buf_size + pic->linesize[0];
222  break;
223  }
224 
225  *got_frame = 1;
226  av_frame_move_ref((AVFrame*)data, pic);
227 
228  return avpkt->size;
229 }
230 
232 {
233  UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
234 
235  /* Free output */
236  av_frame_free(&avctx->coded_frame);
237  av_freep(&utv->buffer);
238 
239  /* Finish decoding and clean up the instance */
240  utv->codec->DecodeEnd();
241  CCodec::DeleteInstance(utv->codec);
242 
243  return 0;
244 }
245 
247  "libutvideo",
248  NULL_IF_CONFIG_SMALL("Ut Video"),
251  0, //capabilities
252  NULL, //supported_framerates
253  NULL, //pix_fmts
254  NULL, //supported_samplerates
255  NULL, //sample_fmts
256  NULL, //channel_layouts
257  0, //max_lowres
258  NULL, //priv_class
259  NULL, //profiles
260  sizeof(UtVideoContext),
261  NULL, //next
262  NULL, //init_thread_copy
263  NULL, //update_thread_context
264  NULL, //defaults
265  NULL, //init_static_data
267  NULL, //encode
268  NULL, //encode2
271 };
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:438
#define NULL
Definition: coverity.c:32
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int size
Definition: avcodec.h:1589
const char * b
Definition: vf_curves.c:113
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1885
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:508
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:3057
AVCodec.
Definition: avcodec.h:3559
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:140
uint8_t * buffer
Definition: libutvideo.h:67
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1772
uint8_t * data
Definition: avcodec.h:1588
bitstream reader API header.
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:318
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:153
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
int width
picture width / height.
Definition: avcodec.h:1844
attribute_deprecated int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
Definition: avpicture.c:52
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)
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:65
#define UTVF_NFCC_BGR_BU
Definition: libutvideo.h:42
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1657
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:320
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1689
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:63
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
int extradata_size
Definition: avcodec.h:1773
static const char * format
Definition: movenc.c:47
static av_cold int utvideo_decode_close(AVCodecContext *avctx)
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2386
#define u(width,...)
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:344
unsigned int buf_size
Definition: libutvideo.h:66
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
if(ret< 0)
Definition: vf_mcdeint.c:282
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:3078
CCodec * codec
Definition: libutvideo.h:65
void * priv_data
Definition: avcodec.h:1699
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:323
static av_cold int utvideo_decode_init(AVCodecContext *avctx)
AVCodec ff_libutvideo_decoder
#define av_freep(p)
static int utvideo_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
#define MKTAG(a, b, c, d)
Definition: common.h:342
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1565
for(j=16;j >0;--j)
#define UTVF_NFCC_BGRA_BU
Definition: libutvideo.h:46