1 /*
2 * VC3/DNxHD decoder.
3 * Copyright (c) 2007 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
4 * Copyright (c) 2011 MirriAd Ltd
5 * Copyright (c) 2015 Christophe Gisquet
6 *
7 * 10 bit support added by MirriAd Ltd, Joseph Artsimovich <joseph@mirriad.com>
8 * Slice multithreading and MB interlaced support added by Christophe Gisquet
9 *
10 * This file is part of FFmpeg.
11 *
12 * FFmpeg is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * FFmpeg is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with FFmpeg; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
31 #define UNCHECKED_BITSTREAM_READER 1
37
46 /** -1:not set yet 0:off=RGB 1:on=YUV 2:variable */
49
56 int64_t
cid;
///< compression id
61 int data_offset;
// End of mb_scan_index, where macroblocks start
67 int bit_depth;
// 8, 10, 12 or 0 if not initialized at all.
74
75 #define DNXHD_VLC_BITS 9
76 #define DNXHD_DC_VLC_BITS 7
77
88
90 {
92
96
99
103
104 return 0;
105 }
106
108 {
109 if (cid != ctx->
cid) {
111
115 }
120 }
121 if (bitdepth > 10) {
125 }
128
132
142
144 }
145 return 0;
146 }
147
149 {
151
152 // make sure VLC tables will be loaded when cid is parsed
154
158
159 return 0;
160 }
161
165 {
167 int old_bit_depth = ctx->
bit_depth, bitdepth;
168 uint64_t header_prefix;
169 if (buf_size < 0x280) {
171 "buffer too small (%d < 640).\n", buf_size);
173 }
174
176 if (header_prefix == 0) {
178 "unknown header 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n",
179 buf[0], buf[1], buf[2], buf[3], buf[4]);
181 }
182 if (buf[5] & 2) { /* interlaced */
187 "interlaced %d, cur field %d\n", buf[5] & 3, ctx->
cur_field);
188 } else {
190 }
191 ctx->
mbaff = (buf[0x6] >> 5) & 1;
192
195
196 switch(buf[0x21] >> 5) {
197 case 1: bitdepth = 8; break;
198 case 2: bitdepth = 10; break;
199 case 3: bitdepth = 12; break;
200 default:
202 "Unknown bitdepth indicator (%d)\n", buf[0x21] >> 5);
204 }
205
208 return ret;
211 "Adaptive MB interlace flag in an unsupported profile.\n");
212
213 ctx->
act = buf[0x2C] & 7;
216 "Adaptive color transform in an unsupported profile.\n");
217
218 ctx->
is_444 = (buf[0x2C] >> 6) & 1;
220 if (bitdepth == 8) {
223 } else if (bitdepth == 10) {
227 } else {
231 }
232 } else if (bitdepth == 12) {
235 } else if (bitdepth == 10) {
238 } else {
241 }
242
249 }
250
251 // make sure profile size constraints are respected
252 // DNx100 allows 1920->1440 and 1280->960 subsampling
259 }
260
265 }
266
269
272
276
277 // Newer format supports variable mb_scan_index sizes
280 } else {
284 "mb height too big: %d\n", ctx->
mb_height);
286 }
288 }
289
292 "buffer too small (%d < %d).\n", buf_size, ctx->
data_offset);
294 }
295
297
303 "invalid mb scan index (%u vs %u).\n",
306 }
307 }
308
309 return 0;
310 }
311
315 int index_bits,
316 int level_bias,
317 int level_shift,
318 int dc_shift)
319 {
320 int i, j, index1, index2,
len,
flags;
321 int level, component, sign;
322 const int *scale;
327 int ret = 0;
329
331
333 if (n & 2) {
334 component = 1 + (n & 1);
337 } else {
338 component = 0;
341 }
342 } else {
343 component = (n >> 1) % 3;
344 if (component) {
347 } else {
350 }
351 }
352
355 if (len) {
358 sign = ~level >> 31;
359 level = (
NEG_USR32(sign ^ level, len) ^ sign) - sign;
360 row->
last_dc[component] += level * (1 << dc_shift);
361 }
362 block[0] = row->
last_dc[component];
363
364 i = 0;
365
369
370 while (index1 != eob_index) {
371 level = ac_info[2*index1+0];
372 flags = ac_info[2*index1+1];
373
376
377 if (flags & 1) {
380 }
381
382 if (flags & 2) {
387 }
388
389 if (++i > 63) {
391 ret = -1;
392 break;
393 }
394
396 level *= scale[i];
397 level += scale[i] >> 1;
398 if (level_bias < 32 || weight_matrix[i] != level_bias)
399 level += level_bias; // 1<<(level_shift-1)
400 level >>= level_shift;
401
402 block[j] = (level ^ sign) - sign;
403
407 }
408
410 return ret;
411 }
412
415 {
417 }
418
421 {
423 }
424
427 {
429 }
430
433 {
435 }
436
439 {
441 }
442
445 {
447 int dct_linesize_luma = frame->
linesize[0];
448 int dct_linesize_chroma = frame->
linesize[1];
449 uint8_t *dest_y, *dest_u, *dest_v;
450 int dct_y_offset, dct_x_offset;
452 int interlaced_mb = 0;
453
457 } else {
459 }
461 if (act) {
463 static int act_warned;
464 if (!act_warned) {
465 act_warned = 1;
467 "ACT flag set, in violation of frame header.\n");
468 }
469 }
else if (row->
format == -1) {
471 }
else if (row->
format != act) {
472 row->
format = 2;
// Variable
473 }
474 }
475
477 for (i = 0; i < 64; i++) {
480 }
482 }
483
484 for (i = 0; i < 8 + 4 * ctx->
is_444; i++) {
487 }
488
490 dct_linesize_luma <<= 1;
491 dct_linesize_chroma <<= 1;
492 }
493
494 dest_y = frame->
data[0] + ((y * dct_linesize_luma) << 4) + (x << (4 +
shift1));
495 dest_u = frame->
data[1] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1 + ctx->
is_444));
496 dest_v = frame->
data[2] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1 + ctx->
is_444));
497
502 }
503 if (interlaced_mb) {
504 dct_linesize_luma <<= 1;
505 dct_linesize_chroma <<= 1;
506 }
507
508 dct_y_offset = interlaced_mb ? frame->
linesize[0] : (dct_linesize_luma << 3);
509 dct_x_offset = 8 <<
shift1;
514 ctx->
idsp.
idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, row->
blocks[5]);
515
517 dct_y_offset = interlaced_mb ? frame->
linesize[1] : (dct_linesize_chroma << 3);
522 }
523 } else {
527 ctx->
idsp.
idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, row->
blocks[7]);
528
530 dct_y_offset = interlaced_mb ? frame->
linesize[1] : (dct_linesize_chroma << 3);
534 ctx->
idsp.
idct_put(dest_u + dct_y_offset + dct_x_offset, dct_linesize_chroma, row->
blocks[9]);
538 ctx->
idsp.
idct_put(dest_v + dct_y_offset + dct_x_offset, dct_linesize_chroma, row->
blocks[11]);
539 }
540 }
541
542 return 0;
543 }
544
546 int rownb, int threadnb)
547 {
551 int x;
552
557 for (x = 0; x < ctx->
mb_width; x++) {
558 //START_TIMER;
560 if (ret < 0) {
562 return ret;
563 }
564 //STOP_TIMER("decode macroblock");
565 }
566
567 return 0;
568 }
569
572 {
579 int ret, i;
580
581 ff_dlog(avctx,
"frame size %d\n", buf_size);
582
585
586 decode_coding_unit:
588 return ret;
589
594 first_field = 1;
595 }
599 first_field = 1;
600 }
601
604 if (ret < 0)
605 return ret;
606
607 if (first_field) {
609 return ret;
612 }
613
617
621 first_field = 0;
622 goto decode_coding_unit;
623 }
624
625 ret = 0;
629 }
630
632 static int act_warned;
637 format = 2;
638 break;
639 }
640 }
641 switch (format) {
642 case -1:
643 case 2:
644 if (!act_warned) {
645 act_warned = 1;
647 "Unsupported: variable ACT flag.\n");
648 }
649 break;
650 case 0:
653 break;
654 case 1:
657 break;
658 }
659 }
661 if (ret) {
664 }
665
666 *got_frame = 1;
668 }
669
671 {
673
677
679
680 return 0;
681 }
682
695 };
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
static int dnxhd_decode_dct_block_8(const DNXHDContext *ctx, RowContext *row, int n)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
void(* clear_block)(int16_t *block)
int coded_width
Bitstream width / height, may be different from width/height e.g.
int64_t cid
compression id
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define AV_LOG_WARNING
Something somehow does not look correct.
const uint8_t * luma_weight
static int init_thread_copy(AVCodecContext *avctx)
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
static av_cold int init(AVCodecContext *avctx)
static av_cold int dnxhd_decode_init_thread_copy(AVCodecContext *avctx)
#define AV_PIX_FMT_GBRP10
const CIDEntry ff_dnxhd_cid_table[]
const uint16_t * run_codes
#define DECLARE_ALIGNED(n, t, v)
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
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.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
static av_cold int dnxhd_decode_close(AVCodecContext *avctx)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static int dnxhd_decode_dct_block_12(const DNXHDContext *ctx, RowContext *row, int n)
Multithreading support functions.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
static av_always_inline int dnxhd_decode_dct_block(const DNXHDContext *ctx, RowContext *row, int n, int index_bits, int level_bias, int level_shift, int dc_shift)
int format
-1:not set yet 0:off=RGB 1:on=YUV 2:variable
bitstream reader API header.
#define AV_LOG_VERBOSE
Detailed information.
int interlaced_frame
The content of the picture is interlaced.
unsigned int coding_unit_size
#define DNXHD_DC_VLC_BITS
#define AV_PIX_FMT_YUV422P12
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
high precision timer, useful to profile code
enum AVPixelFormat pix_fmt
static int first_field(const struct video_data *s)
const uint16_t * ac_codes
#define UPDATE_CACHE(name, gb)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int ff_dnxhd_get_cid_table(int cid)
static const int shift1[6]
static int dnxhd_decode_row(AVCodecContext *avctx, void *data, int rownb, int threadnb)
uint64_t avpriv_dnxhd_parse_header_prefix(const uint8_t *buf)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
int flags
AV_CODEC_FLAG_*.
const char * name
Name of the codec implementation.
#define AV_PIX_FMT_YUV444P10
static const uint8_t offset[127][2]
#define CLOSE_READER(name, gb)
static int dnxhd_decode_dct_block_10_444(const DNXHDContext *ctx, RowContext *row, int n)
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
uint32_t mb_scan_index[256]
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
const uint8_t * chroma_weight
#define SKIP_BITS(name, gb, num)
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
enum AVPictureType pict_type
Picture type of the frame.
int width
picture width / height.
#define LAST_SKIP_BITS(name, gb, num)
#define GET_VLC(code, name, gb, table, bits, max_depth)
If the vlc code is invalid and max_depth=1, then no bits will be removed.
uint8_t idct_permutation[64]
IDCT input permutation.
static int dnxhd_decode_macroblock(const DNXHDContext *ctx, RowContext *row, AVFrame *frame, int x, int y)
#define SHOW_UBITS(name, gb, num)
#define FF_ARRAY_ELEMS(a)
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
void(* idct_put)(uint8_t *dest, int line_size, int16_t *block)
block -> idct -> clip to unsigned 8 bit -> dest.
Libavcodec external API header.
int cur_field
current interlaced field
av_cold void ff_blockdsp_init(BlockDSPContext *c, AVCodecContext *avctx)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
static int dnxhd_decode_dct_block_10(const DNXHDContext *ctx, RowContext *row, int n)
main external API structure.
static const char * format
#define OPEN_READER(name, gb)
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
static unsigned int get_bits1(GetBitContext *s)
enum AVColorSpace colorspace
YUV colorspace type.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
#define GET_CACHE(name, gb)
const uint8_t ff_zigzag_direct[64]
#define AV_PIX_FMT_GBRP12
#define AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV444P12
static int dnxhd_init_vlc(DNXHDContext *ctx, uint32_t cid, int bitdepth)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
#define DNXHD_VARIABLE
Indicate that a CIDEntry value must be read in the bitstream.
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
#define SHOW_SBITS(name, gb, num)
common internal api header.
int(* decode_dct_block)(const struct DNXHDContext *ctx, RowContext *row, int n)
static unsigned bit_depth(uint64_t mask)
av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
int top_field_first
If the content is interlaced, is top field displayed first.
static int dnxhd_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
VLC_TYPE(* table)[2]
code, bits
int key_frame
1 -> keyframe, 0-> not
const CIDEntry * cid_table
static void * av_mallocz_array(size_t nmemb, size_t size)
static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame, const uint8_t *buf, int buf_size, int first_field)
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
AVPixelFormat
Pixel format.
This structure stores compressed data.
void ff_free_vlc(VLC *vlc)
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
static int dnxhd_decode_dct_block_12_444(const DNXHDContext *ctx, RowContext *row, int n)
static av_cold int dnxhd_decode_init(AVCodecContext *avctx)