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;
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;
65  break;
66  case MKTAG('U', 'L', 'R', 'A'):
67  avctx->pix_fmt = AV_PIX_FMT_RGB32;
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 };
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
AVFrame * coded_frame
the picture in the bitstream
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
output residual component w
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
initialize output if(nPeaks >3)%at least 3 peaks in spectrum for trying to find f0 nf0peaks
uint8_t
#define av_cold
Definition: attributes.h:78
uint8_t * buffer
Definition: libutvideo.h:58
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
uint8_t * data
bitstream reader API header.
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:270
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
external API header
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
MIPS optimizations info
Definition: mips.txt:2
int width
picture width / height.
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate.The lists are not just lists
Known FOURCCs: 'ULY0' (YCbCr 4:2:0), 'ULY2' (YCbCr 4:2:2), 'ULRG' (RGB), 'ULRA' (RGBA) ...
#define AV_RL32
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:71
#define UTVF_NFCC_BGR_BU
Definition: libutvideo.h:41
NULL
Definition: eval.c:55
main external API structure.
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:69
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
static av_cold int utvideo_decode_close(AVCodecContext *avctx)
unsigned int buf_size
Definition: libutvideo.h:57
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:68
CCodec * codec
Definition: libutvideo.h:56
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.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
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)
#define MKTAG(a, b, c, d)
Definition: common.h:282
This structure stores compressed data.
#define UTVF_NFCC_BGRA_BU
Definition: libutvideo.h:45