1 /*
2 * H.26L/H.264/AVC/JVT/14496-10/... parameter set decoding
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
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 * H.264 / AVC / MPEG-4 part10 parameter set decoding.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28 #include <inttypes.h>
29
37
38 #define MIN_LOG2_MAX_FRAME_NUM 4
39
40 #define EXTENDED_SAR 255
41
43 { 6, 13, 20, 28, 13, 20, 28, 32,
44 20, 28, 32, 37, 28, 32, 37, 42 },
45 { 10, 14, 20, 24, 14, 20, 24, 27,
46 20, 24, 27, 30, 24, 27, 30, 34 }
47 };
48
50 { 6, 10, 13, 16, 18, 23, 25, 27,
51 10, 11, 16, 18, 23, 25, 27, 29,
52 13, 16, 18, 23, 25, 27, 29, 31,
53 16, 18, 23, 25, 27, 29, 31, 33,
54 18, 23, 25, 27, 29, 31, 33, 36,
55 23, 25, 27, 29, 31, 33, 36, 38,
56 25, 27, 29, 31, 33, 36, 38, 40,
57 27, 29, 31, 33, 36, 38, 40, 42 },
58 { 9, 13, 15, 17, 19, 21, 22, 24,
59 13, 13, 17, 19, 21, 22, 24, 25,
60 15, 17, 19, 21, 22, 24, 25, 27,
61 17, 19, 21, 22, 24, 25, 27, 28,
62 19, 21, 22, 24, 25, 27, 28, 30,
63 21, 22, 24, 25, 27, 28, 30, 32,
64 22, 24, 25, 27, 28, 30, 32, 33,
65 24, 25, 27, 28, 30, 32, 33, 35 }
66 };
67
68 /* maximum number of MBs in the DPB for a given level */
70 { 10, 396 },
71 { 11, 900 },
72 { 12, 2376 },
73 { 13, 2376 },
74 { 20, 2376 },
75 { 21, 4752 },
76 { 22, 8100 },
77 { 30, 8100 },
78 { 31, 18000 },
79 { 32, 20480 },
80 { 40, 32768 },
81 { 41, 32768 },
82 { 42, 34816 },
83 { 50, 110400 },
84 { 51, 184320 },
85 { 52, 184320 },
86 };
87
89 {
91 }
92
94 {
95 #if 0
97 if (
s->sps_list[
id]) {
98 /* drop all PPS that depend on this SPS */
100 if (
s->pps_list[
i] && ((
PPS*)
s->pps_list[
i]->data)->sps_id ==
id)
102 }
103 #endif
105 }
106
109 {
112
113 if (cpb_count > 32
U) {
116 }
117
118 get_bits(gb, 4);
/* bit_rate_scale */
119 get_bits(gb, 4);
/* cpb_size_scale */
120 for (
i = 0;
i < cpb_count;
i++) {
124 }
125 sps->initial_cpb_removal_delay_length =
get_bits(gb, 5) + 1;
126 sps->cpb_removal_delay_length =
get_bits(gb, 5) + 1;
127 sps->dpb_output_delay_length =
get_bits(gb, 5) + 1;
129 sps->cpb_cnt = cpb_count;
130 return 0;
131 }
132
135 {
136 int aspect_ratio_info_present_flag;
137 unsigned int aspect_ratio_idc;
138
139 aspect_ratio_info_present_flag =
get_bits1(gb);
140
141 if (aspect_ratio_info_present_flag) {
148 } else {
151 }
152 } else {
155 }
156
157 if (
get_bits1(gb))
/* overscan_info_present_flag */
158 get_bits1(gb);
/* overscan_appropriate_flag */
159
161 if (
sps->video_signal_type_present_flag) {
163 sps->full_range =
get_bits1(gb);
/* video_full_range_flag */
164
166 if (
sps->colour_description_present_flag) {
167 sps->color_primaries =
get_bits(gb, 8);
/* colour_primaries */
168 sps->color_trc =
get_bits(gb, 8);
/* transfer_characteristics */
169 sps->colorspace =
get_bits(gb, 8);
/* matrix_coefficients */
170
171 // Set invalid values to "unspecified"
178 }
179 }
180
181 /* chroma_location_info_present_flag */
183 /* chroma_sample_location_type_top_field */
186 } else
188
191 return 0;
192 }
193
195 if (
sps->timing_info_present_flag) {
198 if (!num_units_in_tick || !time_scale) {
200 "time_scale/num_units_in_tick invalid or unsupported (%u/%u)\n",
201 time_scale, num_units_in_tick);
202 sps->timing_info_present_flag = 0;
203 } else {
204 sps->num_units_in_tick = num_units_in_tick;
205 sps->time_scale = time_scale;
206 }
208 }
209
211 if (
sps->nal_hrd_parameters_present_flag)
215 if (
sps->vcl_hrd_parameters_present_flag)
218 if (
sps->nal_hrd_parameters_present_flag ||
219 sps->vcl_hrd_parameters_present_flag)
223 return 0;
225 if (
sps->bitstream_restriction_flag) {
226 get_bits1(gb);
/* motion_vectors_over_pic_boundaries_flag */
233
235 sps->num_reorder_frames = 0;
236 sps->bitstream_restriction_flag = 0;
237 }
238
239 if (
sps->num_reorder_frames > 16
U
240 /* max_dec_frame_buffering || max_dec_frame_buffering > 16 */) {
242 "Clipping illegal num_reorder_frames %d\n",
243 sps->num_reorder_frames);
244 sps->num_reorder_frames = 16;
246 }
247 }
248
249 return 0;
250 }
251
253 const uint8_t *jvt_list,
254 const uint8_t *fallback_list)
255 {
256 int i, last = 8, next = 8;
258 if (!
get_bits1(gb))
/* matrix not written, we use the predicted one */
259 memcpy(factors, fallback_list,
size *
sizeof(uint8_t));
260 else
262 if (next) {
264 if (v < -128 || v > 127) {
267 }
268 next = (last + v) & 0xff;
269 }
270 if (!
i && !next) {
/* matrix not written, we use the preset one */
271 memcpy(factors, jvt_list,
size *
sizeof(uint8_t));
272 break;
273 }
274 last = factors[scan[
i]] = next ? next : last;
275 }
276 return 0;
277 }
278
279 /* returns non zero if the provided SPS scaling matrix has been filled */
281 const PPS *
pps,
int is_sps,
282 uint8_t(*scaling_matrix4)[16],
283 uint8_t(*scaling_matrix8)[64])
284 {
285 int fallback_sps = !is_sps &&
sps->scaling_matrix_present;
286 const uint8_t *fallback[4] = {
291 };
300 if (is_sps ||
pps->transform_8x8_mode) {
303 if (
sps->chroma_format_idc == 3) {
308 }
309 }
312 }
313
315 }
316
318 {
320
323
326
328
331 }
332
335 {
338 unsigned int sps_id;
339 int i, log2_max_frame_num_minus4;
342
344 if (!sps_buf)
347
349 if (
sps->data_size >
sizeof(
sps->data)) {
351 sps->data_size =
sizeof(
sps->data);
352 }
354
356 constraint_set_flags |=
get_bits1(gb) << 0;
// constraint_set0_flag
357 constraint_set_flags |=
get_bits1(gb) << 1;
// constraint_set1_flag
358 constraint_set_flags |=
get_bits1(gb) << 2;
// constraint_set2_flag
359 constraint_set_flags |=
get_bits1(gb) << 3;
// constraint_set3_flag
360 constraint_set_flags |=
get_bits1(gb) << 4;
// constraint_set4_flag
361 constraint_set_flags |=
get_bits1(gb) << 5;
// constraint_set5_flag
365
369 }
370
371 sps->sps_id = sps_id;
372 sps->time_offset_length = 24;
374 sps->constraint_set_flags = constraint_set_flags;
376 sps->full_range = -1;
377
378 memset(
sps->scaling_matrix4, 16,
sizeof(
sps->scaling_matrix4));
379 memset(
sps->scaling_matrix8, 16,
sizeof(
sps->scaling_matrix8));
380 sps->scaling_matrix_present = 0;
381 sps->colorspace = 2;
//AVCOL_SPC_UNSPECIFIED
382
383 if (
sps->profile_idc == 100 ||
// High profile
384 sps->profile_idc == 110 ||
// High10 profile
385 sps->profile_idc == 122 ||
// High422 profile
386 sps->profile_idc == 244 ||
// High444 Predictive profile
387 sps->profile_idc == 44 ||
// Cavlc444 profile
388 sps->profile_idc == 83 ||
// Scalable Constrained High profile (SVC)
389 sps->profile_idc == 86 ||
// Scalable High Intra profile (SVC)
390 sps->profile_idc == 118 ||
// Stereo High profile (MVC)
391 sps->profile_idc == 128 ||
// Multiview High profile (MVC)
392 sps->profile_idc == 138 ||
// Multiview Depth High profile (MVCD)
393 sps->profile_idc == 144) {
// old High444 profile
395 if (
sps->chroma_format_idc > 3
U) {
397 sps->chroma_format_idc);
399 }
else if (
sps->chroma_format_idc == 3) {
401 if (
sps->residual_color_transform_flag) {
404 }
405 }
408 if (
sps->bit_depth_chroma !=
sps->bit_depth_luma) {
410 "Different chroma and luma bit depth");
412 }
413 if (
sps->bit_depth_luma < 8 ||
sps->bit_depth_luma > 14 ||
414 sps->bit_depth_chroma < 8 ||
sps->bit_depth_chroma > 14) {
416 sps->bit_depth_luma,
sps->bit_depth_chroma);
418 }
421 sps->scaling_matrix4,
sps->scaling_matrix8);
424 sps->scaling_matrix_present |=
ret;
425 } else {
426 sps->chroma_format_idc = 1;
427 sps->bit_depth_luma = 8;
428 sps->bit_depth_chroma = 8;
429 }
430
435 "log2_max_frame_num_minus4 out of range (0-12): %d\n",
436 log2_max_frame_num_minus4);
438 }
439 sps->log2_max_frame_num = log2_max_frame_num_minus4 + 4;
440
442
443 if (
sps->poc_type == 0) {
// FIXME #define
445 if (t>12) {
448 }
449 sps->log2_max_poc_lsb = t + 4;
450 }
else if (
sps->poc_type == 1) {
// FIXME #define
454
455 if (
sps->offset_for_non_ref_pic == INT32_MIN
456 ||
sps->offset_for_top_to_bottom_field == INT32_MIN
457 ) {
459 "offset_for_non_ref_pic or offset_for_top_to_bottom_field is out of range\n");
461 }
462
464
465 if ((
unsigned)
sps->poc_cycle_length >=
468 "poc_cycle_length overflow %d\n",
sps->poc_cycle_length);
470 }
471
472 for (
i = 0;
i <
sps->poc_cycle_length;
i++) {
474 if (
sps->offset_for_ref_frame[
i] == INT32_MIN) {
476 "offset_for_ref_frame is out of range\n");
478 }
479 }
480 }
else if (
sps->poc_type != 2) {
483 }
484
487 sps->ref_frame_count =
FFMAX(2,
sps->ref_frame_count);
490 "too many reference frames %d\n",
sps->ref_frame_count);
492 }
496
498
499 if (
sps->mb_height >= INT_MAX / 2
U) {
502 }
503 sps->mb_height *= 2 -
sps->frame_mbs_only_flag;
504
505 if (!
sps->frame_mbs_only_flag)
507 else
509
510 if ((
unsigned)
sps->mb_width >= INT_MAX / 16 ||
511 (
unsigned)
sps->mb_height >= INT_MAX / 16 ||
513 16 *
sps->mb_height, 0, avctx)) {
516 }
517
519
520 #ifndef ALLOW_INTERLACE
523 "MBAFF support not included; enable it at compile-time.\n");
524 #endif
533
536 "values are l:%d r:%d t:%d b:%d\n",
537 crop_left, crop_right, crop_top, crop_bottom);
538
542 sps->crop_bottom = 0;
543 } else {
544 int vsub = (
sps->chroma_format_idc == 1) ? 1 : 0;
545 int hsub = (
sps->chroma_format_idc == 1 ||
546 sps->chroma_format_idc == 2) ? 1 : 0;
547 int step_x = 1 <<
hsub;
548 int step_y = (2 -
sps->frame_mbs_only_flag) << vsub;
549
550 if (crop_left > (unsigned)INT_MAX / 4 / step_x ||
551 crop_right > (unsigned)INT_MAX / 4 / step_x ||
552 crop_top > (unsigned)INT_MAX / 4 / step_y ||
553 crop_bottom> (unsigned)INT_MAX / 4 / step_y ||
554 (crop_left + crop_right ) * step_x >=
width ||
555 (crop_top + crop_bottom) * step_y >=
height
556 ) {
559 }
560
561 sps->crop_left = crop_left * step_x;
562 sps->crop_right = crop_right * step_x;
563 sps->crop_top = crop_top * step_y;
564 sps->crop_bottom = crop_bottom * step_y;
565 }
566 } else {
572 }
573
575 if (
sps->vui_parameters_present_flag) {
579 }
580
584 "Overread %s by %d bits\n",
sps->vui_parameters_present_flag ?
"VUI" :
"SPS", -
get_bits_left(gb));
585 if (!ignore_truncation)
587 }
588
589 /* if the maximum delay is not stored in the SPS, derive it based on the
590 * level */
591 if (!
sps->bitstream_restriction_flag &&
597 sps->num_reorder_frames);
598 break;
599 }
600 }
601 }
602
605
607 static const char csp[4][5] = { "Gray", "420", "422", "444" };
609 "sps:%u profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%u/%u/%u/%u %s %s %"PRId32"/%"PRId32" b%d reo:%d\n",
610 sps_id,
sps->profile_idc,
sps->level_idc,
612 sps->ref_frame_count,
613 sps->mb_width,
sps->mb_height,
614 sps->frame_mbs_only_flag ?
"FRM" : (
sps->mb_aff ?
"MB-AFF" :
"PIC-AFF"),
615 sps->direct_8x8_inference_flag ?
"8B8" :
"",
616 sps->crop_left,
sps->crop_right,
617 sps->crop_top,
sps->crop_bottom,
618 sps->vui_parameters_present_flag ?
"VUI" :
"",
619 csp[
sps->chroma_format_idc],
620 sps->timing_info_present_flag ?
sps->num_units_in_tick : 0,
621 sps->timing_info_present_flag ?
sps->time_scale : 0,
623 sps->bitstream_restriction_flag ?
sps->num_reorder_frames : -1
624 );
625 }
626
627 /* check if this is a repeat of an already parsed SPS, then keep the
628 * original one.
629 * otherwise drop all PPSes that depend on it */
633 } else {
636 }
637
638 return 0;
639
643 }
644
646 {
648 const int max_qp = 51 + 6 * (
sps->bit_depth_luma - 8);
649
650 for (
i = 0;
i < 6;
i++) {
651 pps->dequant8_coeff[
i] =
pps->dequant8_buffer[
i];
652 for (j = 0; j <
i; j++)
653 if (!memcmp(
pps->scaling_matrix8[j],
pps->scaling_matrix8[
i],
654 64 * sizeof(uint8_t))) {
655 pps->dequant8_coeff[
i] =
pps->dequant8_buffer[j];
656 break;
657 }
659 continue;
660
661 for (q = 0; q < max_qp + 1; q++) {
664 for (x = 0; x < 64; x++)
665 pps->dequant8_coeff[
i][q][(x >> 3) | ((x & 7) << 3)] =
668 }
669 }
670 }
671
673 {
675 const int max_qp = 51 + 6 * (
sps->bit_depth_luma - 8);
676 for (
i = 0;
i < 6;
i++) {
677 pps->dequant4_coeff[
i] =
pps->dequant4_buffer[
i];
678 for (j = 0; j <
i; j++)
679 if (!memcmp(
pps->scaling_matrix4[j],
pps->scaling_matrix4[
i],
680 16 * sizeof(uint8_t))) {
681 pps->dequant4_coeff[
i] =
pps->dequant4_buffer[j];
682 break;
683 }
685 continue;
686
687 for (q = 0; q < max_qp + 1; q++) {
690 for (x = 0; x < 16; x++)
691 pps->dequant4_coeff[
i][q][(x >> 2) | ((x << 2) & 0xF)] =
694 }
695 }
696 }
697
699 {
702 memset(
pps->dequant8_coeff, 0,
sizeof(
pps->dequant8_coeff));
703
704 if (
pps->transform_8x8_mode)
706 if (
sps->transform_bypass) {
707 for (
i = 0;
i < 6;
i++)
708 for (x = 0; x < 16; x++)
709 pps->dequant4_coeff[
i][0][x] = 1 << 6;
710 if (
pps->transform_8x8_mode)
711 for (
i = 0;
i < 6;
i++)
712 for (x = 0; x < 64; x++)
713 pps->dequant8_coeff[
i][0][x] = 1 << 6;
714 }
715 }
716
718 {
720 const int max_qp = 51 + 6 * (depth - 8);
721 for (
i = 0;
i < max_qp + 1;
i++)
722 pps->chroma_qp_table[t][
i] =
724 }
725
727 {
729
733 "Current profile doesn't provide more RBSP data in PPS, skipping\n");
734 return 0;
735 }
736
737 return 1;
738 }
739
741 {
743
745
747 }
748
751 {
756 int qp_bd_offset;
757 int bits_left;
759
763 }
764
770 if (!pps_buf) {
773 }
774
776 if (
pps->data_size >
sizeof(
pps->data)) {
779 pps->data_size,
sizeof(
pps->data));
780 pps->data_size =
sizeof(
pps->data);
781 }
783
790 }
795 }
796 pps->sps = (
const SPS*)
pps->sps_ref->data;
798
799 if (
sps->bit_depth_luma > 14) {
801 "Invalid luma bit depth=%d\n",
802 sps->bit_depth_luma);
805 }
else if (
sps->bit_depth_luma == 11 ||
sps->bit_depth_luma == 13) {
807 "Unimplemented luma bit depth=%d",
808 sps->bit_depth_luma);
811 }
812
816 if (
pps->slice_group_count > 1) {
821 }
824 if (
pps->ref_count[0] - 1 > 32 - 1 ||
pps->ref_count[1] - 1 > 32 - 1) {
828 }
829
830 qp_bd_offset = 6 * (
sps->bit_depth_luma - 8);
831
837 if (
pps->chroma_qp_index_offset[0] < -12 ||
pps->chroma_qp_index_offset[0] > 12) {
840 }
841
842 pps->deblocking_filter_parameters_present =
get_bits1(gb);
845
846 pps->transform_8x8_mode = 0;
847 memcpy(
pps->scaling_matrix4,
sps->scaling_matrix4,
848 sizeof(
pps->scaling_matrix4));
849 memcpy(
pps->scaling_matrix8,
sps->scaling_matrix8,
850 sizeof(
pps->scaling_matrix8));
851
856 pps->scaling_matrix4,
pps->scaling_matrix8);
859 // second_chroma_qp_index_offset
861 if (
pps->chroma_qp_index_offset[1] < -12 ||
pps->chroma_qp_index_offset[1] > 12) {
864 }
865 } else {
866 pps->chroma_qp_index_offset[1] =
pps->chroma_qp_index_offset[0];
867 }
868
870 sps->bit_depth_luma);
872 sps->bit_depth_luma);
873
875
876 if (
pps->chroma_qp_index_offset[0] !=
pps->chroma_qp_index_offset[1])
877 pps->chroma_qp_diff = 1;
878
881 "pps:%u sps:%u %s slice_groups:%d ref:%u/%u %s qp:%d/%d/%d/%d %s %s %s %s\n",
883 pps->cabac ?
"CABAC" :
"CAVLC",
884 pps->slice_group_count,
885 pps->ref_count[0],
pps->ref_count[1],
886 pps->weighted_pred ?
"weighted" :
"",
887 pps->init_qp,
pps->init_qs,
pps->chroma_qp_index_offset[0],
pps->chroma_qp_index_offset[1],
888 pps->deblocking_filter_parameters_present ?
"LPAR" :
"",
889 pps->constrained_intra_pred ?
"CONSTR" :
"",
890 pps->redundant_pic_cnt_present ?
"REDU" :
"",
891 pps->transform_8x8_mode ?
"8x8DCT" :
"");
892 }
893
896
897 return 0;
898
902 }