1 /*
2 * Copyright (c) 2010-2011 Maxim Poliakovski
3 * Copyright (c) 2010-2011 Elvis Presley
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Known FOURCCs: 'apch' (HQ), 'apcn' (SD), 'apcs' (LT), 'apco' (Proxy), 'ap4h' (4444), 'ap4x' (4444 XQ)
25 */
26
27 //#define DEBUG
28
29 #define LONG_BITSTREAM_READER
30
31 #include "config_components.h"
32
35
47
48 #define ALPHA_SHIFT_16_TO_10(alpha_val) (alpha_val >> 6)
49 #define ALPHA_SHIFT_8_TO_10(alpha_val) ((alpha_val << 2) | (alpha_val >> 6))
50 #define ALPHA_SHIFT_16_TO_12(alpha_val) (alpha_val >> 4)
51 #define ALPHA_SHIFT_8_TO_12(alpha_val) ((alpha_val << 4) | (alpha_val >> 4))
52
54 const int num_bits, const int decode_precision) {
55 const int mask = (1 << num_bits) - 1;
56 int i, idx,
val, alpha_val;
57
58 idx = 0;
60 do {
61 do {
64 } else {
65 int sign;
69 if (sign)
71 }
72 alpha_val = (alpha_val +
val) &
mask;
73 if (num_bits == 16) {
74 if (decode_precision == 10) {
76 } else { /* 12b */
78 }
79 } else {
80 if (decode_precision == 10) {
82 } else { /* 12b */
84 }
85 }
86 if (idx >= num_coeffs)
87 break;
92 if (idx +
val > num_coeffs)
93 val = num_coeffs - idx;
94 if (num_bits == 16) {
95 for (
i = 0;
i <
val;
i++) {
96 if (decode_precision == 10) {
98 } else { /* 12b */
100 }
101 }
102 } else {
103 for (
i = 0;
i <
val;
i++) {
104 if (decode_precision == 10) {
106 } else { /* 12b */
108 }
109 }
110 }
111 } while (idx < num_coeffs);
112 }
113
115 const int num_bits)
116 {
117 if (num_bits == 16) {
119 } else { /* 8 bits alpha */
121 }
122 }
123
125 const int num_bits)
126 {
127 if (num_bits == 16) {
129 } else { /* 8 bits alpha */
131 }
132 }
133
135 {
138 uint8_t idct_permutation[64];
139
141
143 case MKTAG(
'a',
'p',
'c',
'o'):
145 break;
146 case MKTAG(
'a',
'p',
'c',
's'):
148 break;
149 case MKTAG(
'a',
'p',
'c',
'n'):
151 break;
152 case MKTAG(
'a',
'p',
'c',
'h'):
154 break;
155 case MKTAG(
'a',
'p',
'4',
'h'):
158 break;
159 case MKTAG(
'a',
'p',
'4',
'x'):
162 break;
163 default:
166 }
167
169 av_log(avctx,
AV_LOG_DEBUG,
"Auto bitdepth precision. Use 10b decoding based on codec tag.\n");
170 } else { /* 12b */
171 av_log(avctx,
AV_LOG_DEBUG,
"Auto bitdepth precision. Use 12b decoding based on codec tag.\n");
172 }
173
179 }
180
182 ctx->prodsp.idct_permutation_type);
183
186
188
193 } else {
196 }
198 }
199
202 {
205 const uint8_t *ptr;
207
209 ff_dlog(avctx,
"header size %d\n", hdr_size);
210 if (hdr_size > data_size) {
213 }
214
220 }
221
224
227
232 }
233
234 ctx->frame_type = (buf[12] >> 2) & 3;
235 ctx->alpha_info = buf[17] & 0xf;
236
237 if (
ctx->alpha_info > 2) {
240 }
242
243 ff_dlog(avctx,
"frame type %d\n",
ctx->frame_type);
244
245 if (
ctx->frame_type == 0) {
246 ctx->scan =
ctx->progressive_scan;
// permuted
247 } else {
248 ctx->scan =
ctx->interlaced_scan;
// permuted
250 if (
ctx->frame_type == 1)
252 }
253
254 if (
ctx->alpha_info) {
257 } else { /* 12b */
259 }
260 } else {
263 } else { /* 12b */
265 }
266 }
267
269 #define HWACCEL_MAX (CONFIG_PRORES_VIDEOTOOLBOX_HWACCEL)
272
274
275 #if CONFIG_PRORES_VIDEOTOOLBOX_HWACCEL
277 #endif
278 *fmtp++ =
ctx->pix_fmt;
280
283
285 }
286
287 ctx->frame->color_primaries = buf[14];
288 ctx->frame->color_trc = buf[15];
289 ctx->frame->colorspace = buf[16];
291
292 ptr = buf + 20;
295
297 if(buf + data_size - ptr < 64) {
300 }
302 ptr += 64;
303 } else {
304 memset(
ctx->qmat_luma, 4, 64);
305 }
306
308 if(buf + data_size - ptr < 64) {
311 }
313 } else {
314 memcpy(
ctx->qmat_chroma,
ctx->qmat_luma, 64);
315 }
316
317 return hdr_size;
318 }
319
321 {
323 int i, hdr_size, slice_count;
324 unsigned pic_data_size;
325 int log2_slice_mb_width, log2_slice_mb_height;
326 int slice_mb_count, mb_x, mb_y;
327 const uint8_t *data_ptr, *index_ptr;
328
329 hdr_size = buf[0] >> 3;
330 if (hdr_size < 8 || hdr_size > buf_size) {
333 }
334
335 pic_data_size =
AV_RB32(buf + 1);
336 if (pic_data_size > buf_size) {
339 }
340
341 log2_slice_mb_width = buf[7] >> 4;
342 log2_slice_mb_height = buf[7] & 0xF;
343 if (log2_slice_mb_width > 3 || log2_slice_mb_height) {
345 1 << log2_slice_mb_width, 1 << log2_slice_mb_height);
347 }
348
349 ctx->mb_width = (avctx->
width + 15) >> 4;
351 ctx->mb_height = (avctx->
height + 31) >> 5;
352 else
353 ctx->mb_height = (avctx->
height + 15) >> 4;
354
355 // QT ignores the written value
356 // slice_count = AV_RB16(buf + 5);
357 slice_count =
ctx->mb_height * ((
ctx->mb_width >> log2_slice_mb_width) +
359
360 if (
ctx->slice_count != slice_count || !
ctx->slices) {
362 ctx->slice_count = 0;
366 ctx->slice_count = slice_count;
367 }
368
369 if (!slice_count)
371
372 if (hdr_size + slice_count*2 > buf_size) {
375 }
376
377 // parse slice information
378 index_ptr = buf + hdr_size;
379 data_ptr = index_ptr + slice_count*2;
380
381 slice_mb_count = 1 << log2_slice_mb_width;
382 mb_x = 0;
383 mb_y = 0;
384
385 for (
i = 0;
i < slice_count;
i++) {
387
388 slice->
data = data_ptr;
389 data_ptr +=
AV_RB16(index_ptr +
i*2);
390
391 while (
ctx->mb_width - mb_x < slice_mb_count)
392 slice_mb_count >>= 1;
393
398
402 }
403
404 mb_x += slice_mb_count;
405 if (mb_x ==
ctx->mb_width) {
406 slice_mb_count = 1 << log2_slice_mb_width;
407 mb_x = 0;
408 mb_y++;
409 }
410 if (data_ptr > buf + buf_size) {
413 }
414 }
415
416 if (mb_x || mb_y !=
ctx->mb_height) {
418 mb_y,
ctx->mb_height);
420 }
421
422 return pic_data_size;
423 }
424
425 #define DECODE_CODEWORD(val, codebook, SKIP) \
426 do { \
427 unsigned int rice_order, exp_order, switch_bits; \
428 unsigned int q, buf, bits; \
429 \
430 UPDATE_CACHE(re, gb); \
431 buf = GET_CACHE(re, gb); \
432 \
433 /* number of bits to switch between rice and exp golomb */ \
434 switch_bits = codebook & 3; \
435 rice_order = codebook >> 5; \
436 exp_order = (codebook >> 2) & 7; \
437 \
438 q = 31 - av_log2(buf); \
439 \
440 if (q > switch_bits) { /* exp golomb */ \
441 bits = exp_order - switch_bits + (q<<1); \
442 if (bits > FFMIN(MIN_CACHE_BITS, 31)) \
443 return AVERROR_INVALIDDATA; \
444 val = SHOW_UBITS(re, gb, bits) - (1 << exp_order) + \
445 ((switch_bits + 1) << rice_order); \
446 SKIP(re, gb, bits); \
447 } else if (rice_order) { \
448 SKIP_BITS(re, gb, q+1); \
449 val = (q << rice_order) + SHOW_UBITS(re, gb, rice_order); \
450 SKIP(re, gb, rice_order); \
451 } else { \
452 val = q; \
453 SKIP(re, gb, q+1); \
454 } \
455 } while (0)
456
457 #define TOSIGNED(x) (((x) >> 1) ^ (-((x) & 1)))
458
459 #define FIRST_DC_CB 0xB8
460
461 static const uint8_t
dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
462
464 int blocks_per_slice)
465 {
466 int16_t prev_dc;
468
470
474
475 out += 64;
// dc coeff for the next block
476
478 sign = 0;
479 for (
i = 1;
i < blocks_per_slice;
i++,
out += 64) {
482 else sign = 0;
483 prev_dc += (((
code + 1) >> 1) ^ sign) - sign;
485 }
487 return 0;
488 }
489
490 // adaptive codebook switching lut according to previous run/level values
491 static const uint8_t
run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29, 0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
492 static const uint8_t
lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28, 0x28, 0x28, 0x28, 0x4C };
493
495 int16_t *
out,
int blocks_per_slice)
496 {
498 int block_mask, sign;
501 int log2_block_count =
av_log2(blocks_per_slice);
502
505 run = 4;
507
508 max_coeffs = 64 << log2_block_count;
509 block_mask = blocks_per_slice - 1;
510
511 for (
pos = block_mask;;) {
514 break;
515
518 if (
pos >= max_coeffs) {
521 }
522
525
526 i =
pos >> log2_block_count;
527
530 out[((
pos & block_mask) << 6) +
ctx->scan[
i]] = ((
level ^ sign) - sign);
531 }
532
534 return 0;
535 }
536
538 uint16_t *dst, int dst_stride,
539 const uint8_t *buf, unsigned buf_size,
540 const int16_t *qmat)
541 {
546 int i, blocks_per_slice = slice->
mb_count<<2;
548
549 for (
i = 0;
i < blocks_per_slice;
i++)
550 ctx->bdsp.clear_block(blocks+(
i<<6));
551
553
558
561 ctx->prodsp.idct_put(dst, dst_stride,
block+(0<<6), qmat);
562 ctx->prodsp.idct_put(dst +8, dst_stride,
block+(1<<6), qmat);
563 ctx->prodsp.idct_put(dst+4*dst_stride , dst_stride,
block+(2<<6), qmat);
564 ctx->prodsp.idct_put(dst+4*dst_stride+8, dst_stride,
block+(3<<6), qmat);
566 dst += 16;
567 }
568 return 0;
569 }
570
572 uint16_t *dst, int dst_stride,
573 const uint8_t *buf, unsigned buf_size,
574 const int16_t *qmat, int log2_blocks_per_mb)
575 {
580 int i, j, blocks_per_slice = slice->
mb_count << log2_blocks_per_mb;
582
583 for (
i = 0;
i < blocks_per_slice;
i++)
584 ctx->bdsp.clear_block(blocks+(
i<<6));
585
587
592
595 for (j = 0; j < log2_blocks_per_mb; j++) {
596 ctx->prodsp.idct_put(dst, dst_stride,
block+(0<<6), qmat);
597 ctx->prodsp.idct_put(dst+4*dst_stride, dst_stride,
block+(1<<6), qmat);
599 dst += 8;
600 }
601 }
602 return 0;
603 }
604
605 /**
606 * Decode alpha slice plane.
607 */
609 uint16_t *dst, int dst_stride,
610 const uint8_t *buf, int buf_size,
611 int blocks_per_slice)
612 {
617
618 for (
i = 0;
i < blocks_per_slice<<2;
i++)
619 ctx->bdsp.clear_block(blocks+(
i<<6));
620
622
623 if (
ctx->alpha_info == 2) {
624 ctx->unpack_alpha(&gb, blocks, blocks_per_slice * 4 * 64, 16);
625 } else {
626 ctx->unpack_alpha(&gb, blocks, blocks_per_slice * 4 * 64, 8);
627 }
628
630
631 for (
i = 0;
i < 16;
i++) {
632 memcpy(dst,
block, 16 * blocks_per_slice *
sizeof(*dst));
633 dst += dst_stride >> 1;
634 block += 16 * blocks_per_slice;
635 }
636 }
637
639 {
642 const uint8_t *buf = slice->
data;
644 int i, hdr_size, qscale, log2_chroma_blocks_per_mb;
645 int luma_stride, chroma_stride;
646 int y_data_size, u_data_size, v_data_size, a_data_size,
offset;
647 uint8_t *dest_y, *dest_u, *dest_v;
650 int mb_x_shift;
652 uint16_t val_no_chroma;
653
655 //av_log(avctx, AV_LOG_INFO, "slice %d mb width %d mb x %d y %d\n",
656 // jobnr, slice->mb_count, slice->mb_x, slice->mb_y);
657
658 // slice header
659 hdr_size = buf[0] >> 3;
660 qscale =
av_clip(buf[1], 1, 224);
661 qscale = qscale > 128 ? qscale - 96 << 2: qscale;
662 y_data_size =
AV_RB16(buf + 2);
663 u_data_size =
AV_RB16(buf + 4);
664 v_data_size = slice->
data_size - y_data_size - u_data_size - hdr_size;
665 if (hdr_size > 7) v_data_size =
AV_RB16(buf + 6);
666 a_data_size = slice->
data_size - y_data_size - u_data_size -
667 v_data_size - hdr_size;
668
669 if (y_data_size < 0 || u_data_size < 0 || v_data_size < 0
670 || hdr_size+y_data_size+u_data_size+v_data_size > slice->
data_size){
673 }
674
675 buf += hdr_size;
676
677 for (
i = 0;
i < 64;
i++) {
678 qmat_luma_scaled [
i] =
ctx->qmat_luma [
i] * qscale;
679 qmat_chroma_scaled[
i] =
ctx->qmat_chroma[
i] * qscale;
680 }
681
682 if (
ctx->frame_type == 0) {
685 } else {
686 luma_stride = pic->
linesize[0] << 1;
687 chroma_stride = pic->
linesize[1] << 1;
688 }
689
692 mb_x_shift = 5;
693 log2_chroma_blocks_per_mb = 2;
694 } else {
695 mb_x_shift = 4;
696 log2_chroma_blocks_per_mb = 1;
697 }
698
701 dest_u = pic->
data[1] + (slice->
mb_y << 4) * chroma_stride + (slice->
mb_x << mb_x_shift);
702 dest_v = pic->
data[2] + (slice->
mb_y << 4) * chroma_stride + (slice->
mb_x << mb_x_shift);
703
709 }
710
712 buf, y_data_size, qmat_luma_scaled);
715
718 buf + y_data_size, u_data_size,
719 qmat_chroma_scaled, log2_chroma_blocks_per_mb);
722
724 buf + y_data_size + u_data_size, v_data_size,
725 qmat_chroma_scaled, log2_chroma_blocks_per_mb);
728 }
729 else {
730 size_t mb_max_x = slice->
mb_count << (mb_x_shift - 1);
733 val_no_chroma = 511;
734 } else { /* 12b */
735 val_no_chroma = 511 * 4;
736 }
737 for (
i = 0;
i < 16; ++
i)
738 for (j = 0; j < mb_max_x; ++j) {
739 *(uint16_t*)(dest_u + (
i * chroma_stride) + (j << 1)) = val_no_chroma;
740 *(uint16_t*)(dest_v + (
i * chroma_stride) + (j << 1)) = val_no_chroma;
741 }
742 }
743
744 /* decode alpha plane if available */
745 if (
ctx->alpha_info && pic->
data[3] && a_data_size) {
748 buf + y_data_size + u_data_size + v_data_size,
750 }
751
753 return 0;
754 }
755
757 {
761
763
764 for (
i = 0;
i <
ctx->slice_count;
i++)
766
769 if (error < ctx->slice_count)
770 return 0;
771
772 return ctx->slices[0].ret;
773 }
774
777 {
779 const uint8_t *buf = avpkt->
data;
780 int buf_size = avpkt->
size;
781 int frame_hdr_size, pic_size,
ret;
782
786 }
787
791 ctx->first_field = 1;
792
793 buf += 8;
794 buf_size -= 8;
795
797 if (frame_hdr_size < 0)
798 return frame_hdr_size;
799
800 buf += frame_hdr_size;
801 buf_size -= frame_hdr_size;
802
806
819 }
820
823 if (pic_size < 0) {
825 return pic_size;
826 }
827
831 }
832
833 buf += pic_size;
834 buf_size -= pic_size;
835
836 if (
ctx->frame_type && buf_size > 0 &&
ctx->first_field) {
837 ctx->first_field = 0;
839 }
840
842 *got_frame = 1;
843
845 }
846
848 {
850
852
853 return 0;
854 }
855
856 #if HAVE_THREADS
858 {
861
863
864 return 0;
865 }
866 #endif
867
881 #if CONFIG_PRORES_VIDEOTOOLBOX_HWACCEL
883 #endif
885 },
886 };