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 MAX_LOG2_MAX_FRAME_NUM (12 + 4)
39 #define MIN_LOG2_MAX_FRAME_NUM 4
40
41 #define EXTENDED_SAR 255
42
44 { 6, 13, 20, 28, 13, 20, 28, 32,
45 20, 28, 32, 37, 28, 32, 37, 42 },
46 { 10, 14, 20, 24, 14, 20, 24, 27,
47 20, 24, 27, 30, 24, 27, 30, 34 }
48 };
49
51 { 6, 10, 13, 16, 18, 23, 25, 27,
52 10, 11, 16, 18, 23, 25, 27, 29,
53 13, 16, 18, 23, 25, 27, 29, 31,
54 16, 18, 23, 25, 27, 29, 31, 33,
55 18, 23, 25, 27, 29, 31, 33, 36,
56 23, 25, 27, 29, 31, 33, 36, 38,
57 25, 27, 29, 31, 33, 36, 38, 40,
58 27, 29, 31, 33, 36, 38, 40, 42 },
59 { 9, 13, 15, 17, 19, 21, 22, 24,
60 13, 13, 17, 19, 21, 22, 24, 25,
61 15, 17, 19, 21, 22, 24, 25, 27,
62 17, 19, 21, 22, 24, 25, 27, 28,
63 19, 21, 22, 24, 25, 27, 28, 30,
64 21, 22, 24, 25, 27, 28, 30, 32,
65 22, 24, 25, 27, 28, 30, 32, 33,
66 24, 25, 27, 28, 30, 32, 33, 35 }
67 };
68
69 /* maximum number of MBs in the DPB for a given level */
71 { 10, 396 },
72 { 11, 900 },
73 { 12, 2376 },
74 { 13, 2376 },
75 { 20, 2376 },
76 { 21, 4752 },
77 { 22, 8100 },
78 { 30, 8100 },
79 { 31, 18000 },
80 { 32, 20480 },
81 { 40, 32768 },
82 { 41, 32768 },
83 { 42, 34816 },
84 { 50, 110400 },
85 { 51, 184320 },
86 { 52, 184320 },
87 };
88
90 {
92 }
93
95 {
96 #if 0
97 int i;
99 /* drop all PPS that depend on this SPS */
103 }
104 #endif
106 }
107
110 {
111 int cpb_count, i;
113
114 if (cpb_count > 32
U) {
117 }
118
119 get_bits(gb, 4);
/* bit_rate_scale */
120 get_bits(gb, 4);
/* cpb_size_scale */
121 for (i = 0; i < cpb_count; i++) {
125 }
131 return 0;
132 }
133
136 {
137 int aspect_ratio_info_present_flag;
138 unsigned int aspect_ratio_idc;
139
140 aspect_ratio_info_present_flag =
get_bits1(gb);
141
142 if (aspect_ratio_info_present_flag) {
149 } else {
152 }
153 } else {
156 }
157
158 if (
get_bits1(gb))
/* overscan_info_present_flag */
159 get_bits1(gb);
/* overscan_appropriate_flag */
160
165
171
172 // Set invalid values to "unspecified"
179 }
180 }
181
182 /* chroma_location_info_present_flag */
184 /* chroma_sample_location_type_top_field */
186 get_ue_golomb(gb);
/* chroma_sample_location_type_bottom_field */
187 }
188
191 return 0;
192 }
193
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);
203 } else {
206 }
208 }
209
223 return 0;
226 get_bits1(gb);
/* motion_vectors_over_pic_boundaries_flag */
233
237 }
238
240 /* max_dec_frame_buffering || max_dec_frame_buffering > 16 */) {
242 "Clipping illegal num_reorder_frames %d\n",
246 }
247 }
248
249 return 0;
250 }
251
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
261 for (i = 0; i <
size; i++) {
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,
284 {
291 };
292 int ret = 0;
308 }
309 }
310 if (!ret)
311 ret = is_sps;
312 }
313
314 return ret;
315 }
316
318 {
319 int i;
320
323
326
329
332 }
333
336 {
338 int profile_idc, level_idc, constraint_set_flags = 0;
339 unsigned int sps_id;
340 int i, log2_max_frame_num_minus4;
342 int ret;
343
345 if (!sps_buf)
348
353 }
355
357 constraint_set_flags |=
get_bits1(gb) << 0;
// constraint_set0_flag
358 constraint_set_flags |=
get_bits1(gb) << 1;
// constraint_set1_flag
359 constraint_set_flags |=
get_bits1(gb) << 2;
// constraint_set2_flag
360 constraint_set_flags |=
get_bits1(gb) << 3;
// constraint_set3_flag
361 constraint_set_flags |=
get_bits1(gb) << 4;
// constraint_set4_flag
362 constraint_set_flags |=
get_bits1(gb) << 5;
// constraint_set5_flag
366
370 }
371
378
383
387 sps->
profile_idc == 244 ||
// High444 Predictive profile
389 sps->
profile_idc == 83 ||
// Scalable Constrained High profile (SVC)
390 sps->
profile_idc == 86 ||
// Scalable High Intra profile (SVC)
391 sps->
profile_idc == 118 ||
// Stereo High profile (MVC)
392 sps->
profile_idc == 128 ||
// Multiview High profile (MVC)
393 sps->
profile_idc == 138 ||
// Multiview Depth High profile (MVCD)
405 }
406 }
411 "Different chroma and luma bit depth");
413 }
419 }
423 if (ret < 0)
426 } else {
430 }
431
436 "log2_max_frame_num_minus4 out of range (0-12): %d\n",
437 log2_max_frame_num_minus4);
439 }
441
443
444 if (sps->
poc_type == 0) {
// FIXME #define
446 if (t>12) {
449 }
451 }
else if (sps->
poc_type == 1) {
// FIXME #define
456
462 }
463
469 }
470
478 }
482
484
488 }
490
493 else
495
496 if ((
unsigned)sps->
mb_width >= INT_MAX / 16 ||
497 (
unsigned)sps->
mb_height >= INT_MAX / 16 ||
502 }
503
505
506 #ifndef ALLOW_INTERLACE
509 "MBAFF support not included; enable it at compile-time.\n");
510 #endif
519
522 "values are l:%d r:%d t:%d b:%d\n",
523 crop_left, crop_right, crop_top, crop_bottom);
524
529 } else {
533 int step_x = 1 << hsub;
535
536 if (crop_left > (unsigned)INT_MAX / 4 / step_x ||
537 crop_right > (unsigned)INT_MAX / 4 / step_x ||
538 crop_top > (unsigned)INT_MAX / 4 / step_y ||
539 crop_bottom> (unsigned)INT_MAX / 4 / step_y ||
540 (crop_left + crop_right ) * step_x >= width ||
541 (crop_top + crop_bottom) * step_y >= height
542 ) {
543 av_log(avctx,
AV_LOG_ERROR,
"crop values invalid %d %d %d %d / %d %d\n", crop_left, crop_right, crop_top, crop_bottom, width, height);
545 }
546
551 }
552 } else {
558 }
559
563 if (ret < 0)
565 }
566
570 if (!ignore_truncation)
572 }
573
574 /* if the maximum delay is not stored in the SPS, derive it based on the
575 * level */
583 break;
584 }
585 }
586 }
587
590
592 static const char csp[4][5] = { "Gray", "420", "422", "444" };
594 "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",
609 );
610 }
611
612 /* check if this is a repeat of an already parsed SPS, then keep the
613 * original one.
614 * otherwise drop all PPSes that depend on it */
618 } else {
621 }
622
623 return 0;
624
628 }
629
631 {
632 int i, j, q, x;
634
635 for (i = 0; i < 6; i++) {
637 for (j = 0; j < i; j++)
641 break;
642 }
643 if (j < i)
644 continue;
645
646 for (q = 0; q < max_qp + 1; q++) {
649 for (x = 0; x < 64; x++)
653 }
654 }
655 }
656
658 {
659 int i, j, q, x;
661 for (i = 0; i < 6; i++) {
663 for (j = 0; j < i; j++)
667 break;
668 }
669 if (j < i)
670 continue;
671
672 for (q = 0; q < max_qp + 1; q++) {
675 for (x = 0; x < 16; x++)
679 }
680 }
681 }
682
684 {
685 int i, x;
688
692 for (i = 0; i < 6; i++)
693 for (x = 0; x < 16; x++)
696 for (i = 0; i < 6; i++)
697 for (x = 0; x < 64; x++)
699 }
700 }
701
703 {
704 int i;
705 const int max_qp = 51 + 6 * (depth - 8);
706 for (i = 0; i < max_qp + 1; i++)
709 }
710
712 {
714
715 if ((profile_idc == 66 || profile_idc == 77 ||
718 "Current profile doesn't provide more RBSP data in PPS, skipping\n");
719 return 0;
720 }
721
722 return 1;
723 }
724
727 {
732 int qp_bd_offset;
733 int bits_left;
734 int ret;
735
739 }
740
742 if (!pps_buf)
745
752 }
754
761 }
765 "Invalid luma bit depth=%d\n",
771 "Unimplemented luma bit depth=%d",
775 }
776
783 }
790 }
791
793
802 }
803
807
813
819 if (ret < 0)
821 // second_chroma_qp_index_offset
826 }
827 } else {
829 }
830
835
837
840
843 "pps:%u sps:%u %s slice_groups:%d ref:%u/%u %s qp:%d/%d/%d/%d %s %s %s %s\n",
845 pps->
cabac ?
"CABAC" :
"CAVLC",
854 }
855
858
859 return 0;
860
863 return ret;
864 }
int ff_h264_decode_picture_parameter_set(GetBitContext *gb, AVCodecContext *avctx, H264ParamSets *ps, int bit_length)
Decode PPS.
static unsigned int show_bits1(GetBitContext *s)
#define MAX_LOG2_MAX_FRAME_NUM
int video_signal_type_present_flag
static void remove_pps(H264ParamSets *s, int id)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static int decode_hrd_parameters(GetBitContext *gb, AVCodecContext *avctx, SPS *sps)
static int shift(int a, int b)
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define AV_LOG_WARNING
Something somehow does not look correct.
int chroma_qp_index_offset[2]
AVBufferRef * sps_list[MAX_SPS_COUNT]
int scaling_matrix_present
uint8_t scaling_matrix4[6][16]
int bitstream_restriction_flag
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
const uint8_t ff_h264_quant_rem6[QP_MAX_NUM+1]
uint32_t num_units_in_tick
const char * av_color_space_name(enum AVColorSpace space)
unsigned int crop_top
frame_cropping_rect_top_offset
int fixed_frame_rate_flag
uint8_t scaling_matrix4[6][16]
int deblocking_filter_parameters_present
deblocking_filter_parameters_present_flag
static const AVRational ff_h264_pixel_aspect[17]
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
const uint8_t ff_h264_dequant4_coeff_init[6][3]
unsigned int crop_left
frame_cropping_rect_left_offset
int offset_for_non_ref_pic
int gaps_in_frame_num_allowed_flag
int bit_depth_chroma
bit_depth_chroma_minus8 + 8
#define FF_DEBUG_PICT_INFO
enum AVColorPrimaries color_primaries
int cabac
entropy_coding_mode_flag
unsigned int crop_right
frame_cropping_rect_right_offset
uint32_t(*[6] dequant4_coeff)[16]
int transform_bypass
qpprime_y_zero_transform_bypass_flag
uint32_t(*[6] dequant8_coeff)[64]
static int get_bits_count(const GetBitContext *s)
static const int level_max_dpb_mbs[][2]
int redundant_pic_cnt_present
redundant_pic_cnt_present_flag
#define AV_LOG_VERBOSE
Detailed information.
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
static int get_bits_left(GetBitContext *gb)
H.264 parameter set handling.
int mb_aff
mb_adaptive_frame_field_flag
enum AVColorTransferCharacteristic color_trc
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define AV_CODEC_FLAG2_IGNORE_CROP
Discard cropping information from SPS.
static int get_ue_golomb(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to 8190.
int poc_type
pic_order_cnt_type
int constrained_intra_pred
constrained_intra_pred_flag
const uint8_t ff_zigzag_scan[16+1]
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
int weighted_pred
weighted_pred_flag
static int decode_scaling_matrices(GetBitContext *gb, const SPS *sps, const PPS *pps, int is_sps, uint8_t(*scaling_matrix4)[16], uint8_t(*scaling_matrix8)[64])
#define MIN_LOG2_MAX_FRAME_NUM
int residual_color_transform_flag
residual_colour_transform_flag
int delta_pic_order_always_zero_flag
int offset_for_top_to_bottom_field
const char * av_color_primaries_name(enum AVColorPrimaries primaries)
static void remove_sps(H264ParamSets *s, int id)
static void init_dequant8_coeff_table(PPS *pps, const SPS *sps)
int crop
frame_cropping_flag
uint8_t scaling_matrix8[6][64]
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
int ref_frame_count
num_ref_frames
int initial_cpb_removal_delay_length
initial_cpb_removal_delay_length_minus1 + 1
int poc_cycle_length
num_ref_frames_in_pic_order_cnt_cycle
int colour_description_present_flag
uint8_t chroma_qp_table[2][QP_MAX_NUM+1]
pre-scaled (with chroma_qp_index_offset) version of qp_table
int init_qp
pic_init_qp_minus26 + 26
int direct_8x8_inference_flag
uint8_t scaling_matrix8[6][64]
static int decode_scaling_list(GetBitContext *gb, uint8_t *factors, int size, const uint8_t *jvt_list, const uint8_t *fallback_list)
#define FF_ARRAY_ELEMS(a)
static unsigned get_ue_golomb_long(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
int pic_order_present
pic_order_present_flag
short offset_for_ref_frame[256]
static int more_rbsp_data_in_pps(const SPS *sps, void *logctx)
static int decode_vui_parameters(GetBitContext *gb, AVCodecContext *avctx, SPS *sps)
int timing_info_present_flag
int vcl_hrd_parameters_present_flag
Libavcodec external API header.
#define MAX_DELAYED_PIC_COUNT
AVBufferRef * pps_list[MAX_PPS_COUNT]
static int get_ue_golomb_31(GetBitContext *gb)
read unsigned exp golomb code, constraint to a max of 31.
main external API structure.
const uint8_t ff_h264_chroma_qp[7][QP_MAX_NUM+1]
int dpb_output_delay_length
dpb_output_delay_length_minus1 + 1
uint8_t * data
The data buffer.
int vui_parameters_present_flag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
static void init_dequant4_coeff_table(PPS *pps, const SPS *sps)
AVBufferRef * av_buffer_allocz(int size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
int constraint_set_flags
constraint_set[0-3]_flag
static unsigned int get_bits1(GetBitContext *s)
const uint8_t ff_h264_quant_div6[QP_MAX_NUM+1]
static void skip_bits(GetBitContext *s, int n)
static int FUNC() pps(CodedBitstreamContext *ctx, RWContext *rw, H264RawPPS *current)
const uint8_t ff_zigzag_direct[64]
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
#define FF_COMPLIANCE_STRICT
Strictly conform to all the things in the spec no matter what consequences.
int log2_max_poc_lsb
log2_max_pic_order_cnt_lsb_minus4
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
int size
Size of data in bytes.
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
int transform_8x8_mode
transform_8x8_mode_flag
int pic_struct_present_flag
const uint8_t ff_h264_dequant8_coeff_init[6][6]
A reference to a data buffer.
int init_qs
pic_init_qs_minus26 + 26
common internal api header.
int nal_hrd_parameters_present_flag
int log2_max_frame_num
log2_max_frame_num_minus4 + 4
const char * av_color_transfer_name(enum AVColorTransferCharacteristic transfer)
const uint8_t * buffer_end
static const uint8_t default_scaling4[2][16]
void ff_h264_ps_uninit(H264ParamSets *ps)
Uninit H264 param sets structure.
int bit_depth_luma
bit_depth_luma_minus8 + 8
int ff_h264_decode_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx, H264ParamSets *ps, int ignore_truncation)
Decode SPS.
int mb_width
pic_width_in_mbs_minus1 + 1
int flags2
AV_CODEC_FLAG2_*.
int slice_group_count
num_slice_groups_minus1 + 1
int cpb_cnt
See H.264 E.1.2.
uint32_t dequant4_buffer[6][QP_MAX_NUM+1][16]
static const uint8_t default_scaling8[2][64]
uint32_t dequant8_buffer[6][QP_MAX_NUM+1][64]
int cpb_removal_delay_length
cpb_removal_delay_length_minus1 + 1
static void init_dequant_tables(PPS *pps, const SPS *sps)
#define MKTAG(a, b, c, d)
unsigned int crop_bottom
frame_cropping_rect_bottom_offset
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
const uint8_t ff_h264_dequant8_coeff_init_scan[16]
static void build_qp_table(PPS *pps, int t, int index, const int depth)
int mb_slice_group_map_type
enum AVColorSpace colorspace