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
46
47 static void permute(uint8_t *dst,
const uint8_t *
src,
const uint8_t permutation[64])
48 {
50 for (
i = 0;
i < 64;
i++)
51 dst[
i] = permutation[
src[
i]];
52 }
53
54 #define ALPHA_SHIFT_16_TO_10(alpha_val) (alpha_val >> 6)
55 #define ALPHA_SHIFT_8_TO_10(alpha_val) ((alpha_val << 2) | (alpha_val >> 6))
56 #define ALPHA_SHIFT_16_TO_12(alpha_val) (alpha_val >> 4)
57 #define ALPHA_SHIFT_8_TO_12(alpha_val) ((alpha_val << 4) | (alpha_val >> 4))
58
60 const int num_bits, const int decode_precision) {
61 const int mask = (1 << num_bits) - 1;
62 int i, idx,
val, alpha_val;
63
64 idx = 0;
66 do {
67 do {
70 } else {
71 int sign;
75 if (sign)
77 }
78 alpha_val = (alpha_val +
val) &
mask;
79 if (num_bits == 16) {
80 if (decode_precision == 10) {
82 } else { /* 12b */
84 }
85 } else {
86 if (decode_precision == 10) {
88 } else { /* 12b */
90 }
91 }
92 if (idx >= num_coeffs)
93 break;
98 if (idx +
val > num_coeffs)
99 val = num_coeffs - idx;
100 if (num_bits == 16) {
101 for (
i = 0;
i <
val;
i++) {
102 if (decode_precision == 10) {
104 } else { /* 12b */
106 }
107 }
108 } else {
109 for (
i = 0;
i <
val;
i++) {
110 if (decode_precision == 10) {
112 } else { /* 12b */
114 }
115 }
116 }
117 } while (idx < num_coeffs);
118 }
119
121 const int num_bits)
122 {
123 if (num_bits == 16) {
125 } else { /* 8 bits alpha */
127 }
128 }
129
131 const int num_bits)
132 {
133 if (num_bits == 16) {
135 } else { /* 8 bits alpha */
137 }
138 }
139
141 {
144 uint8_t idct_permutation[64];
145
147
149 case MKTAG(
'a',
'p',
'c',
'o'):
151 break;
152 case MKTAG(
'a',
'p',
'c',
's'):
154 break;
155 case MKTAG(
'a',
'p',
'c',
'n'):
157 break;
158 case MKTAG(
'a',
'p',
'c',
'h'):
160 break;
161 case MKTAG(
'a',
'p',
'4',
'h'):
164 break;
165 case MKTAG(
'a',
'p',
'4',
'x'):
168 break;
169 default:
172 }
173
175 av_log(avctx,
AV_LOG_DEBUG,
"Auto bitdepth precision. Use 10b decoding based on codec tag.\n");
176 } else { /* 12b */
177 av_log(avctx,
AV_LOG_DEBUG,
"Auto bitdepth precision. Use 12b decoding based on codec tag.\n");
178 }
179
185 }
186
188 ctx->prodsp.idct_permutation_type);
189
192
194
199 } else {
202 }
204 }
205
208 {
211 const uint8_t *ptr;
213
215 ff_dlog(avctx,
"header size %d\n", hdr_size);
216 if (hdr_size > data_size) {
219 }
220
226 }
227
230
233
238 }
239
240 ctx->frame_type = (buf[12] >> 2) & 3;
241 ctx->alpha_info = buf[17] & 0xf;
242
243 if (
ctx->alpha_info > 2) {
246 }
248
249 ff_dlog(avctx,
"frame type %d\n",
ctx->frame_type);
250
251 if (
ctx->frame_type == 0) {
252 ctx->scan =
ctx->progressive_scan;
// permuted
253 } else {
254 ctx->scan =
ctx->interlaced_scan;
// permuted
255 ctx->frame->interlaced_frame = 1;
256 ctx->frame->top_field_first =
ctx->frame_type == 1;
257 }
258
259 if (
ctx->alpha_info) {
262 } else { /* 12b */
264 }
265 } else {
268 } else { /* 12b */
270 }
271 }
272
274 #define HWACCEL_MAX (CONFIG_PRORES_VIDEOTOOLBOX_HWACCEL)
277
279
280 #if CONFIG_PRORES_VIDEOTOOLBOX_HWACCEL
282 #endif
283 *fmtp++ =
ctx->pix_fmt;
285
288
290 }
291
292 ctx->frame->color_primaries = buf[14];
293 ctx->frame->color_trc = buf[15];
294 ctx->frame->colorspace = buf[16];
296
297 ptr = buf + 20;
300
302 if(buf + data_size - ptr < 64) {
305 }
307 ptr += 64;
308 } else {
309 memset(
ctx->qmat_luma, 4, 64);
310 }
311
313 if(buf + data_size - ptr < 64) {
316 }
317 permute(
ctx->qmat_chroma,
ctx->prodsp.idct_permutation, ptr);
318 } else {
319 memcpy(
ctx->qmat_chroma,
ctx->qmat_luma, 64);
320 }
321
322 return hdr_size;
323 }
324
326 {
328 int i, hdr_size, slice_count;
329 unsigned pic_data_size;
330 int log2_slice_mb_width, log2_slice_mb_height;
331 int slice_mb_count, mb_x, mb_y;
332 const uint8_t *data_ptr, *index_ptr;
333
334 hdr_size = buf[0] >> 3;
335 if (hdr_size < 8 || hdr_size > buf_size) {
338 }
339
340 pic_data_size =
AV_RB32(buf + 1);
341 if (pic_data_size > buf_size) {
344 }
345
346 log2_slice_mb_width = buf[7] >> 4;
347 log2_slice_mb_height = buf[7] & 0xF;
348 if (log2_slice_mb_width > 3 || log2_slice_mb_height) {
350 1 << log2_slice_mb_width, 1 << log2_slice_mb_height);
352 }
353
354 ctx->mb_width = (avctx->
width + 15) >> 4;
356 ctx->mb_height = (avctx->
height + 31) >> 5;
357 else
358 ctx->mb_height = (avctx->
height + 15) >> 4;
359
360 // QT ignores the written value
361 // slice_count = AV_RB16(buf + 5);
362 slice_count =
ctx->mb_height * ((
ctx->mb_width >> log2_slice_mb_width) +
364
365 if (
ctx->slice_count != slice_count || !
ctx->slices) {
367 ctx->slice_count = 0;
371 ctx->slice_count = slice_count;
372 }
373
374 if (!slice_count)
376
377 if (hdr_size + slice_count*2 > buf_size) {
380 }
381
382 // parse slice information
383 index_ptr = buf + hdr_size;
384 data_ptr = index_ptr + slice_count*2;
385
386 slice_mb_count = 1 << log2_slice_mb_width;
387 mb_x = 0;
388 mb_y = 0;
389
390 for (
i = 0;
i < slice_count;
i++) {
392
393 slice->
data = data_ptr;
394 data_ptr +=
AV_RB16(index_ptr +
i*2);
395
396 while (
ctx->mb_width - mb_x < slice_mb_count)
397 slice_mb_count >>= 1;
398
403
407 }
408
409 mb_x += slice_mb_count;
410 if (mb_x ==
ctx->mb_width) {
411 slice_mb_count = 1 << log2_slice_mb_width;
412 mb_x = 0;
413 mb_y++;
414 }
415 if (data_ptr > buf + buf_size) {
418 }
419 }
420
421 if (mb_x || mb_y !=
ctx->mb_height) {
423 mb_y,
ctx->mb_height);
425 }
426
427 return pic_data_size;
428 }
429
430 #define DECODE_CODEWORD(val, codebook, SKIP) \
431 do { \
432 unsigned int rice_order, exp_order, switch_bits; \
433 unsigned int q, buf, bits; \
434 \
435 UPDATE_CACHE(re, gb); \
436 buf = GET_CACHE(re, gb); \
437 \
438 /* number of bits to switch between rice and exp golomb */ \
439 switch_bits = codebook & 3; \
440 rice_order = codebook >> 5; \
441 exp_order = (codebook >> 2) & 7; \
442 \
443 q = 31 - av_log2(buf); \
444 \
445 if (q > switch_bits) { /* exp golomb */ \
446 bits = exp_order - switch_bits + (q<<1); \
447 if (bits > FFMIN(MIN_CACHE_BITS, 31)) \
448 return AVERROR_INVALIDDATA; \
449 val = SHOW_UBITS(re, gb, bits) - (1 << exp_order) + \
450 ((switch_bits + 1) << rice_order); \
451 SKIP(re, gb, bits); \
452 } else if (rice_order) { \
453 SKIP_BITS(re, gb, q+1); \
454 val = (q << rice_order) + SHOW_UBITS(re, gb, rice_order); \
455 SKIP(re, gb, rice_order); \
456 } else { \
457 val = q; \
458 SKIP(re, gb, q+1); \
459 } \
460 } while (0)
461
462 #define TOSIGNED(x) (((x) >> 1) ^ (-((x) & 1)))
463
464 #define FIRST_DC_CB 0xB8
465
466 static const uint8_t
dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
467
469 int blocks_per_slice)
470 {
471 int16_t prev_dc;
473
475
479
480 out += 64;
// dc coeff for the next block
481
483 sign = 0;
484 for (
i = 1;
i < blocks_per_slice;
i++,
out += 64) {
487 else sign = 0;
488 prev_dc += (((
code + 1) >> 1) ^ sign) - sign;
490 }
492 return 0;
493 }
494
495 // adaptive codebook switching lut according to previous run/level values
496 static const uint8_t
run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29, 0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
497 static const uint8_t
lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28, 0x28, 0x28, 0x28, 0x4C };
498
500 int16_t *
out,
int blocks_per_slice)
501 {
503 int block_mask, sign;
506 int log2_block_count =
av_log2(blocks_per_slice);
507
510 run = 4;
512
513 max_coeffs = 64 << log2_block_count;
514 block_mask = blocks_per_slice - 1;
515
516 for (
pos = block_mask;;) {
519 break;
520
523 if (
pos >= max_coeffs) {
526 }
527
530
531 i =
pos >> log2_block_count;
532
535 out[((
pos & block_mask) << 6) +
ctx->scan[
i]] = ((
level ^ sign) - sign);
536 }
537
539 return 0;
540 }
541
543 uint16_t *dst, int dst_stride,
544 const uint8_t *buf, unsigned buf_size,
545 const int16_t *qmat)
546 {
551 int i, blocks_per_slice = slice->
mb_count<<2;
553
554 for (
i = 0;
i < blocks_per_slice;
i++)
555 ctx->bdsp.clear_block(blocks+(
i<<6));
556
558
563
566 ctx->prodsp.idct_put(dst, dst_stride,
block+(0<<6), qmat);
567 ctx->prodsp.idct_put(dst +8, dst_stride,
block+(1<<6), qmat);
568 ctx->prodsp.idct_put(dst+4*dst_stride , dst_stride,
block+(2<<6), qmat);
569 ctx->prodsp.idct_put(dst+4*dst_stride+8, dst_stride,
block+(3<<6), qmat);
571 dst += 16;
572 }
573 return 0;
574 }
575
577 uint16_t *dst, int dst_stride,
578 const uint8_t *buf, unsigned buf_size,
579 const int16_t *qmat, int log2_blocks_per_mb)
580 {
585 int i, j, blocks_per_slice = slice->
mb_count << log2_blocks_per_mb;
587
588 for (
i = 0;
i < blocks_per_slice;
i++)
589 ctx->bdsp.clear_block(blocks+(
i<<6));
590
592
597
600 for (j = 0; j < log2_blocks_per_mb; j++) {
601 ctx->prodsp.idct_put(dst, dst_stride,
block+(0<<6), qmat);
602 ctx->prodsp.idct_put(dst+4*dst_stride, dst_stride,
block+(1<<6), qmat);
604 dst += 8;
605 }
606 }
607 return 0;
608 }
609
610 /**
611 * Decode alpha slice plane.
612 */
614 uint16_t *dst, int dst_stride,
615 const uint8_t *buf, int buf_size,
616 int blocks_per_slice)
617 {
622
623 for (
i = 0;
i < blocks_per_slice<<2;
i++)
624 ctx->bdsp.clear_block(blocks+(
i<<6));
625
627
628 if (
ctx->alpha_info == 2) {
629 ctx->unpack_alpha(&gb, blocks, blocks_per_slice * 4 * 64, 16);
630 } else {
631 ctx->unpack_alpha(&gb, blocks, blocks_per_slice * 4 * 64, 8);
632 }
633
635
636 for (
i = 0;
i < 16;
i++) {
637 memcpy(dst,
block, 16 * blocks_per_slice *
sizeof(*dst));
638 dst += dst_stride >> 1;
639 block += 16 * blocks_per_slice;
640 }
641 }
642
644 {
647 const uint8_t *buf = slice->
data;
649 int i, hdr_size, qscale, log2_chroma_blocks_per_mb;
650 int luma_stride, chroma_stride;
651 int y_data_size, u_data_size, v_data_size, a_data_size,
offset;
652 uint8_t *dest_y, *dest_u, *dest_v;
655 int mb_x_shift;
657 uint16_t val_no_chroma;
658
660 //av_log(avctx, AV_LOG_INFO, "slice %d mb width %d mb x %d y %d\n",
661 // jobnr, slice->mb_count, slice->mb_x, slice->mb_y);
662
663 // slice header
664 hdr_size = buf[0] >> 3;
665 qscale =
av_clip(buf[1], 1, 224);
666 qscale = qscale > 128 ? qscale - 96 << 2: qscale;
667 y_data_size =
AV_RB16(buf + 2);
668 u_data_size =
AV_RB16(buf + 4);
669 v_data_size = slice->
data_size - y_data_size - u_data_size - hdr_size;
670 if (hdr_size > 7) v_data_size =
AV_RB16(buf + 6);
671 a_data_size = slice->
data_size - y_data_size - u_data_size -
672 v_data_size - hdr_size;
673
674 if (y_data_size < 0 || u_data_size < 0 || v_data_size < 0
675 || hdr_size+y_data_size+u_data_size+v_data_size > slice->
data_size){
678 }
679
680 buf += hdr_size;
681
682 for (
i = 0;
i < 64;
i++) {
683 qmat_luma_scaled [
i] =
ctx->qmat_luma [
i] * qscale;
684 qmat_chroma_scaled[
i] =
ctx->qmat_chroma[
i] * qscale;
685 }
686
687 if (
ctx->frame_type == 0) {
690 } else {
691 luma_stride = pic->
linesize[0] << 1;
692 chroma_stride = pic->
linesize[1] << 1;
693 }
694
697 mb_x_shift = 5;
698 log2_chroma_blocks_per_mb = 2;
699 } else {
700 mb_x_shift = 4;
701 log2_chroma_blocks_per_mb = 1;
702 }
703
706 dest_u = pic->
data[1] + (slice->
mb_y << 4) * chroma_stride + (slice->
mb_x << mb_x_shift);
707 dest_v = pic->
data[2] + (slice->
mb_y << 4) * chroma_stride + (slice->
mb_x << mb_x_shift);
708
709 if (
ctx->frame_type &&
ctx->first_field ^
ctx->frame->top_field_first) {
714 }
715
717 buf, y_data_size, qmat_luma_scaled);
720
723 buf + y_data_size, u_data_size,
724 qmat_chroma_scaled, log2_chroma_blocks_per_mb);
727
729 buf + y_data_size + u_data_size, v_data_size,
730 qmat_chroma_scaled, log2_chroma_blocks_per_mb);
733 }
734 else {
735 size_t mb_max_x = slice->
mb_count << (mb_x_shift - 1);
738 val_no_chroma = 511;
739 } else { /* 12b */
740 val_no_chroma = 511 * 4;
741 }
742 for (
i = 0;
i < 16; ++
i)
743 for (j = 0; j < mb_max_x; ++j) {
744 *(uint16_t*)(dest_u + (
i * chroma_stride) + (j << 1)) = val_no_chroma;
745 *(uint16_t*)(dest_v + (
i * chroma_stride) + (j << 1)) = val_no_chroma;
746 }
747 }
748
749 /* decode alpha plane if available */
750 if (
ctx->alpha_info && pic->
data[3] && a_data_size) {
753 buf + y_data_size + u_data_size + v_data_size,
755 }
756
758 return 0;
759 }
760
762 {
766
768
769 for (
i = 0;
i <
ctx->slice_count;
i++)
771
774 if (error < ctx->slice_count)
775 return 0;
776
777 return ctx->slices[0].ret;
778 }
779
782 {
784 const uint8_t *buf = avpkt->
data;
785 int buf_size = avpkt->
size;
786 int frame_hdr_size, pic_size,
ret;
787
791 }
792
795 ctx->frame->key_frame = 1;
796 ctx->first_field = 1;
797
798 buf += 8;
799 buf_size -= 8;
800
802 if (frame_hdr_size < 0)
803 return frame_hdr_size;
804
805 buf += frame_hdr_size;
806 buf_size -= frame_hdr_size;
807
811
823 }
824
827 if (pic_size < 0) {
829 return pic_size;
830 }
831
835 }
836
837 buf += pic_size;
838 buf_size -= pic_size;
839
840 if (
ctx->frame_type && buf_size > 0 &&
ctx->first_field) {
841 ctx->first_field = 0;
843 }
844
846 *got_frame = 1;
847
849 }
850
852 {
854
856
857 return 0;
858 }
859
860 #if HAVE_THREADS
862 {
865
867
868 return 0;
869 }
870 #endif
871
885 #if CONFIG_PRORES_VIDEOTOOLBOX_HWACCEL
887 #endif
889 },
890 };