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
30
35 #define UNCHECKED_BITSTREAM_READER 1
41
50 /** -1:not set yet 0:off=RGB 1:on=YUV 2:variable */
53
65 int data_offset;
// End of mb_scan_index, where macroblocks start
71 int bit_depth;
// 8, 10, 12 or 0 if not initialized at all.
80
81 #define DNXHD_VLC_BITS 9
82 #define DNXHD_DC_VLC_BITS 7
83
94
96 {
98
103 }
104
107
111
112 return 0;
113 }
114
116 {
120
124 }
130 }
133
137
139 ctx->cid_table->ac_bits, 1, 1,
140 ctx->cid_table->ac_codes, 2, 2, 0)) < 0)
143 ctx->cid_table->dc_bits, 1, 1,
144 ctx->cid_table->dc_codes, 1, 1, 0)) < 0)
147 ctx->cid_table->run_bits, 1, 1,
148 ctx->cid_table->run_codes, 2, 2,
149 ctx->cid_table->run, 1, 1, 0)) < 0)
151
153 }
159 }
160
162 {
164 case 1270:
166 case 1271:
168 case 1272:
170 case 1273:
172 case 1274:
174 }
176 }
177
181 {
184 uint64_t header_prefix;
187 "buffer too small (%d < 640).\n",
buf_size);
189 }
190
192 if (header_prefix == 0) {
194 "unknown header 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n",
197 }
198 if (
buf[5] & 2) {
/* interlaced */
204 "interlaced %d, cur field %d\n",
buf[5] & 3,
ctx->cur_field);
205 } else {
207 }
208 ctx->mbaff = (
buf[0x6] >> 5) & 1;
209 ctx->alpha =
buf[0x7] & 1;
210 ctx->lla = (
buf[0x7] >> 1) & 1;
213
216
217 switch(
buf[0x21] >> 5) {
221 default:
223 "Unknown bitdepth indicator (%d)\n",
buf[0x21] >> 5);
225 }
226
228
230
233 if (
ctx->mbaff &&
ctx->cid_table->cid != 1260)
235 "Adaptive MB interlace flag in an unsupported profile.\n");
236
237 switch ((
buf[0x2C] >> 1) & 3) {
242 }
243
245 if (
ctx->act &&
ctx->cid_table->cid != 1256 &&
ctx->cid_table->cid != 1270)
247 "Adaptive color transform in an unsupported profile.\n");
248
249 ctx->is_444 = (
buf[0x2C] >> 6) & 1;
258 } else {
262 }
269 else
272 } else {
275 }
276
278 if (
ctx->bit_depth != old_bit_depth) {
282 ctx->idsp.idct_permutation);
283 }
284
285 // make sure profile size constraints are respected
286 // DNx100 allows 1920->1440 and 1280->960 subsampling
287 if (
ctx->width !=
ctx->cid_table->width &&
290 &
ctx->avctx->sample_aspect_ratio.den,
291 ctx->width,
ctx->cid_table->width, 255);
292 ctx->width =
ctx->cid_table->width;
293 }
294
299 }
300
301 ctx->mb_width = (
ctx->width + 15)>> 4;
303
306
308 ctx->width,
ctx->height,
ctx->is_444 ?
"4:4" :
"2:2",
310
311 // Newer format supports variable mb_scan_index sizes
313 ctx->data_offset = 0x170 + (
ctx->mb_height << 2);
314 } else {
315 if (
ctx->mb_height > 68) {
317 "mb height too big: %d\n",
ctx->mb_height);
319 }
320 ctx->data_offset = 0x280;
321 }
324 "mb height too big: %d\n",
ctx->mb_height);
326 }
327
330 "buffer too small (%d < %d).\n",
buf_size,
ctx->data_offset);
332 }
333
338 }
339
340 for (
i = 0;
i <
ctx->mb_height;
i++) {
342 ff_dlog(
ctx->avctx,
"mb scan index %d, pos %d: %"PRIu32
"\n",
343 i, 0x170 + (
i << 2),
ctx->mb_scan_index[
i]);
346 "invalid mb scan index (%"PRIu32" vs %u).\n",
349 }
350 }
351
352 return 0;
353 }
354
357 int n,
358 int index_bits,
359 int level_bias,
360 int level_shift,
361 int dc_shift)
362 {
364 int level, component, sign;
366 const uint8_t *weight_matrix;
367 const uint8_t *ac_info =
ctx->cid_table->ac_info;
369 const int eob_index =
ctx->cid_table->eob_index;
372
374
376 if (n & 2) {
377 component = 1 + (n & 1);
379 weight_matrix =
ctx->cid_table->chroma_weight;
380 } else {
381 component = 0;
383 weight_matrix =
ctx->cid_table->luma_weight;
384 }
385 } else {
386 component = (n >> 1) % 3;
387 if (component) {
389 weight_matrix =
ctx->cid_table->chroma_weight;
390 } else {
392 weight_matrix =
ctx->cid_table->luma_weight;
393 }
394 }
395
401 }
408 }
410
412
416
417 while (index1 != eob_index) {
418 level = ac_info[2*index1+0];
419 flags = ac_info[2*index1+1];
420
423
427 }
428
435 }
436
440 break;
441 }
442
443 j =
ctx->permutated_scantable[
i];
446 if (level_bias < 32 || weight_matrix[
i] != level_bias)
447 level += level_bias;
// 1<<(level_shift-1)
448 level >>= level_shift;
449
451
455 }
459 }
460
463 {
465 }
466
469 {
471 }
472
475 {
477 }
478
481 {
483 }
484
487 {
489 }
490
493 {
495 int dct_linesize_luma =
frame->linesize[0];
496 int dct_linesize_chroma =
frame->linesize[1];
497 uint8_t *dest_y, *dest_u, *dest_v;
498 int dct_y_offset, dct_x_offset;
500 int interlaced_mb = 0;
501
505 } else {
507 }
511 static int act_warned;
512 if (!act_warned) {
513 act_warned = 1;
515 "ACT flag set, in violation of frame header.\n");
516 }
517 }
else if (row->
format == -1) {
520 row->
format = 2;
// Variable
521 }
522 }
523
525 for (
i = 0;
i < 64;
i++) {
528 }
530 }
531
532 for (
i = 0;
i < 8 + 4 *
ctx->is_444;
i++) {
533 if (
ctx->decode_dct_block(
ctx, row,
i) < 0)
535 }
536
538 dct_linesize_luma <<= 1;
539 dct_linesize_chroma <<= 1;
540 }
541
542 dest_y =
frame->data[0] + ((y * dct_linesize_luma) << 4) + (x << (4 +
shift1));
543 dest_u =
frame->data[1] + ((y * dct_linesize_chroma) << 4) + (x << (3 +
shift1 +
ctx->is_444));
544 dest_v =
frame->data[2] + ((y * dct_linesize_chroma) << 4) + (x << (3 +
shift1 +
ctx->is_444));
545
547 dest_y +=
frame->linesize[0];
548 dest_u +=
frame->linesize[1];
549 dest_v +=
frame->linesize[2];
550 }
551 if (interlaced_mb) {
552 dct_linesize_luma <<= 1;
553 dct_linesize_chroma <<= 1;
554 }
555
556 dct_y_offset = interlaced_mb ?
frame->linesize[0] : (dct_linesize_luma << 3);
557 dct_x_offset = 8 <<
shift1;
559 ctx->idsp.idct_put(dest_y, dct_linesize_luma, row->
blocks[0]);
560 ctx->idsp.idct_put(dest_y + dct_x_offset, dct_linesize_luma, row->
blocks[1]);
561 ctx->idsp.idct_put(dest_y + dct_y_offset, dct_linesize_luma, row->
blocks[4]);
562 ctx->idsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, row->
blocks[5]);
563
565 dct_y_offset = interlaced_mb ?
frame->linesize[1] : (dct_linesize_chroma << 3);
566 ctx->idsp.idct_put(dest_u, dct_linesize_chroma, row->
blocks[2]);
567 ctx->idsp.idct_put(dest_v, dct_linesize_chroma, row->
blocks[3]);
568 ctx->idsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, row->
blocks[6]);
569 ctx->idsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, row->
blocks[7]);
570 }
571 } else {
572 ctx->idsp.idct_put(dest_y, dct_linesize_luma, row->
blocks[0]);
573 ctx->idsp.idct_put(dest_y + dct_x_offset, dct_linesize_luma, row->
blocks[1]);
574 ctx->idsp.idct_put(dest_y + dct_y_offset, dct_linesize_luma, row->
blocks[6]);
575 ctx->idsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, row->
blocks[7]);
576
578 dct_y_offset = interlaced_mb ?
frame->linesize[1] : (dct_linesize_chroma << 3);
579 ctx->idsp.idct_put(dest_u, dct_linesize_chroma, row->
blocks[2]);
580 ctx->idsp.idct_put(dest_u + dct_x_offset, dct_linesize_chroma, row->
blocks[3]);
581 ctx->idsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, row->
blocks[8]);
582 ctx->idsp.idct_put(dest_u + dct_y_offset + dct_x_offset, dct_linesize_chroma, row->
blocks[9]);
583 ctx->idsp.idct_put(dest_v, dct_linesize_chroma, row->
blocks[4]);
584 ctx->idsp.idct_put(dest_v + dct_x_offset, dct_linesize_chroma, row->
blocks[5]);
585 ctx->idsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, row->
blocks[10]);
586 ctx->idsp.idct_put(dest_v + dct_y_offset + dct_x_offset, dct_linesize_chroma, row->
blocks[11]);
587 }
588 }
589
590 return 0;
591 }
592
594 int rownb, int threadnb)
595 {
597 uint32_t
offset =
ctx->mb_scan_index[rownb];
600
603 row->
last_dc[2] = 1 << (
ctx->bit_depth + 2);
// for levels +2^(bitdepth-1)
608 }
609 for (x = 0; x <
ctx->mb_width; x++) {
614 }
615 }
616
617 return 0;
618 }
619
622 {
623 const uint8_t *
buf = avpkt->
data;
628
630
632 ctx->rows[
i].format = -1;
633
634 decode_coding_unit:
637
643 }
648 }
649
654
658 }
659
663
665 buf +=
ctx->cid_table->coding_unit_size;
668 goto decode_coding_unit;
669 }
670
674 ctx->rows[
i].errors = 0;
675 }
676
678 static int act_warned;
682 ctx->rows[
i].format != -1
/* not run */) {
684 break;
685 }
686 }
688 case -1:
689 case 2:
690 if (!act_warned) {
691 act_warned = 1;
693 "Unsupported: variable ACT flag.\n");
694 }
695 break;
696 case 0:
697 ctx->pix_fmt =
ctx->bit_depth==10
699 break;
700 case 1:
701 ctx->pix_fmt =
ctx->bit_depth==10
703 break;
704 }
705 }
710 }
711
712 *got_frame = 1;
714 }
715
717 {
719
723
725
726 return 0;
727 }
728
741 };