svq3: drop the build dependency on the h264 decoder
[libav.git] / libavcodec / h264_ps.c
CommitLineData
889fce8e
MN
1/*
2 * H.26L/H.264/AVC/JVT/14496-10/... parameter set decoding
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 *
2912e87a 5 * This file is part of Libav.
889fce8e 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
889fce8e
MN
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
889fce8e
MN
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
889fce8e
MN
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
ba87f080 23 * @file
889fce8e
MN
24 * H.264 / AVC / MPEG4 part10 parameter set decoding.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
cc8163e1
DB
28#include <inttypes.h>
29
737eb597 30#include "libavutil/imgutils.h"
889fce8e 31#include "internal.h"
f4d581cd 32#include "mathops.h"
889fce8e
MN
33#include "avcodec.h"
34#include "h264.h"
add1467e 35#include "h264data.h"
889fce8e
MN
36#include "golomb.h"
37
d7d6efe4
JG
38#define MAX_LOG2_MAX_FRAME_NUM (12 + 4)
39#define MIN_LOG2_MAX_FRAME_NUM 4
40
330ad1f6
DB
41static const AVRational pixel_aspect[17] = {
42 { 0, 1 },
43 { 1, 1 },
44 { 12, 11 },
45 { 10, 11 },
46 { 16, 11 },
47 { 40, 33 },
48 { 24, 11 },
49 { 20, 11 },
50 { 32, 11 },
51 { 80, 33 },
52 { 18, 11 },
53 { 15, 11 },
54 { 64, 33 },
55 { 160, 99 },
56 { 4, 3 },
57 { 3, 2 },
58 { 2, 1 },
889fce8e
MN
59};
60
330ad1f6
DB
61static const uint8_t default_scaling4[2][16] = {
62 { 6, 13, 20, 28, 13, 20, 28, 32,
63 20, 28, 32, 37, 28, 32, 37, 42 },
64 { 10, 14, 20, 24, 14, 20, 24, 27,
65 20, 24, 27, 30, 24, 27, 30, 34 }
66};
67
68static const uint8_t default_scaling8[2][64] = {
69 { 6, 10, 13, 16, 18, 23, 25, 27,
70 10, 11, 16, 18, 23, 25, 27, 29,
71 13, 16, 18, 23, 25, 27, 29, 31,
72 16, 18, 23, 25, 27, 29, 31, 33,
73 18, 23, 25, 27, 29, 31, 33, 36,
74 23, 25, 27, 29, 31, 33, 36, 38,
75 25, 27, 29, 31, 33, 36, 38, 40,
76 27, 29, 31, 33, 36, 38, 40, 42 },
77 { 9, 13, 15, 17, 19, 21, 22, 24,
78 13, 13, 17, 19, 21, 22, 24, 25,
79 15, 17, 19, 21, 22, 24, 25, 27,
80 17, 19, 21, 22, 24, 25, 27, 28,
81 19, 21, 22, 24, 25, 27, 28, 30,
82 21, 22, 24, 25, 27, 28, 30, 32,
83 22, 24, 25, 27, 28, 30, 32, 33,
84 24, 25, 27, 28, 30, 32, 33, 35 }
85};
86
b09ad37c
AK
87/* maximum number of MBs in the DPB for a given level */
88static const int level_max_dpb_mbs[][2] = {
89 { 10, 396 },
90 { 11, 900 },
91 { 12, 2376 },
92 { 13, 2376 },
93 { 20, 2376 },
94 { 21, 4752 },
95 { 22, 8100 },
96 { 30, 8100 },
97 { 31, 18000 },
98 { 32, 20480 },
99 { 40, 32768 },
100 { 41, 32768 },
101 { 42, 34816 },
102 { 50, 110400 },
103 { 51, 184320 },
104 { 52, 184320 },
105};
106
330ad1f6
DB
107static inline int decode_hrd_parameters(H264Context *h, SPS *sps)
108{
889fce8e 109 int cpb_count, i;
2c541554 110 cpb_count = get_ue_golomb_31(&h->gb) + 1;
889fce8e 111
330ad1f6 112 if (cpb_count > 32U) {
2c541554 113 av_log(h->avctx, AV_LOG_ERROR, "cpb_count %d invalid\n", cpb_count);
c18838f5 114 return AVERROR_INVALIDDATA;
889fce8e
MN
115 }
116
2c541554
AK
117 get_bits(&h->gb, 4); /* bit_rate_scale */
118 get_bits(&h->gb, 4); /* cpb_size_scale */
330ad1f6 119 for (i = 0; i < cpb_count; i++) {
2c541554
AK
120 get_ue_golomb_long(&h->gb); /* bit_rate_value_minus1 */
121 get_ue_golomb_long(&h->gb); /* cpb_size_value_minus1 */
330ad1f6 122 get_bits1(&h->gb); /* cbr_flag */
889fce8e 123 }
2c541554 124 sps->initial_cpb_removal_delay_length = get_bits(&h->gb, 5) + 1;
330ad1f6
DB
125 sps->cpb_removal_delay_length = get_bits(&h->gb, 5) + 1;
126 sps->dpb_output_delay_length = get_bits(&h->gb, 5) + 1;
127 sps->time_offset_length = get_bits(&h->gb, 5);
128 sps->cpb_cnt = cpb_count;
889fce8e
MN
129 return 0;
130}
131
330ad1f6
DB
132static inline int decode_vui_parameters(H264Context *h, SPS *sps)
133{
889fce8e
MN
134 int aspect_ratio_info_present_flag;
135 unsigned int aspect_ratio_idc;
136
330ad1f6 137 aspect_ratio_info_present_flag = get_bits1(&h->gb);
889fce8e 138
330ad1f6
DB
139 if (aspect_ratio_info_present_flag) {
140 aspect_ratio_idc = get_bits(&h->gb, 8);
141 if (aspect_ratio_idc == EXTENDED_SAR) {
142 sps->sar.num = get_bits(&h->gb, 16);
143 sps->sar.den = get_bits(&h->gb, 16);
144 } else if (aspect_ratio_idc < FF_ARRAY_ELEMS(pixel_aspect)) {
145 sps->sar = pixel_aspect[aspect_ratio_idc];
146 } else {
2c541554 147 av_log(h->avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
c18838f5 148 return AVERROR_INVALIDDATA;
889fce8e 149 }
330ad1f6
DB
150 } else {
151 sps->sar.num =
152 sps->sar.den = 0;
889fce8e 153 }
889fce8e 154
330ad1f6 155 if (get_bits1(&h->gb)) /* overscan_info_present_flag */
2c541554 156 get_bits1(&h->gb); /* overscan_appropriate_flag */
889fce8e 157
2c541554 158 sps->video_signal_type_present_flag = get_bits1(&h->gb);
330ad1f6
DB
159 if (sps->video_signal_type_present_flag) {
160 get_bits(&h->gb, 3); /* video_format */
2c541554 161 sps->full_range = get_bits1(&h->gb); /* video_full_range_flag */
889fce8e 162
2c541554 163 sps->colour_description_present_flag = get_bits1(&h->gb);
330ad1f6 164 if (sps->colour_description_present_flag) {
2c541554
AK
165 sps->color_primaries = get_bits(&h->gb, 8); /* colour_primaries */
166 sps->color_trc = get_bits(&h->gb, 8); /* transfer_characteristics */
167 sps->colorspace = get_bits(&h->gb, 8); /* matrix_coefficients */
889fce8e 168 if (sps->color_primaries >= AVCOL_PRI_NB)
330ad1f6 169 sps->color_primaries = AVCOL_PRI_UNSPECIFIED;
889fce8e 170 if (sps->color_trc >= AVCOL_TRC_NB)
330ad1f6 171 sps->color_trc = AVCOL_TRC_UNSPECIFIED;
889fce8e 172 if (sps->colorspace >= AVCOL_SPC_NB)
330ad1f6 173 sps->colorspace = AVCOL_SPC_UNSPECIFIED;
889fce8e
MN
174 }
175 }
176
330ad1f6
DB
177 /* chroma_location_info_present_flag */
178 if (get_bits1(&h->gb)) {
179 /* chroma_sample_location_type_top_field */
180 h->avctx->chroma_sample_location = get_ue_golomb(&h->gb) + 1;
2c541554 181 get_ue_golomb(&h->gb); /* chroma_sample_location_type_bottom_field */
889fce8e
MN
182 }
183
2c541554 184 sps->timing_info_present_flag = get_bits1(&h->gb);
330ad1f6 185 if (sps->timing_info_present_flag) {
2c541554 186 sps->num_units_in_tick = get_bits_long(&h->gb, 32);
330ad1f6
DB
187 sps->time_scale = get_bits_long(&h->gb, 32);
188 if (!sps->num_units_in_tick || !sps->time_scale) {
189 av_log(h->avctx, AV_LOG_ERROR,
cc8163e1 190 "time_scale/num_units_in_tick invalid or unsupported (%"PRIu32"/%"PRIu32")\n",
330ad1f6 191 sps->time_scale, sps->num_units_in_tick);
c18838f5 192 return AVERROR_INVALIDDATA;
889fce8e 193 }
2c541554 194 sps->fixed_frame_rate_flag = get_bits1(&h->gb);
889fce8e
MN
195 }
196
2c541554 197 sps->nal_hrd_parameters_present_flag = get_bits1(&h->gb);
330ad1f6
DB
198 if (sps->nal_hrd_parameters_present_flag)
199 if (decode_hrd_parameters(h, sps) < 0)
c18838f5 200 return AVERROR_INVALIDDATA;
2c541554 201 sps->vcl_hrd_parameters_present_flag = get_bits1(&h->gb);
330ad1f6
DB
202 if (sps->vcl_hrd_parameters_present_flag)
203 if (decode_hrd_parameters(h, sps) < 0)
c18838f5 204 return AVERROR_INVALIDDATA;
330ad1f6
DB
205 if (sps->nal_hrd_parameters_present_flag ||
206 sps->vcl_hrd_parameters_present_flag)
2c541554
AK
207 get_bits1(&h->gb); /* low_delay_hrd_flag */
208 sps->pic_struct_present_flag = get_bits1(&h->gb);
889fce8e 209
2c541554 210 sps->bitstream_restriction_flag = get_bits1(&h->gb);
330ad1f6 211 if (sps->bitstream_restriction_flag) {
2c541554
AK
212 get_bits1(&h->gb); /* motion_vectors_over_pic_boundaries_flag */
213 get_ue_golomb(&h->gb); /* max_bytes_per_pic_denom */
214 get_ue_golomb(&h->gb); /* max_bits_per_mb_denom */
215 get_ue_golomb(&h->gb); /* log2_max_mv_length_horizontal */
216 get_ue_golomb(&h->gb); /* log2_max_mv_length_vertical */
330ad1f6 217 sps->num_reorder_frames = get_ue_golomb(&h->gb);
2c541554
AK
218 get_ue_golomb(&h->gb); /*max_dec_frame_buffering*/
219
220 if (get_bits_left(&h->gb) < 0) {
330ad1f6
DB
221 sps->num_reorder_frames = 0;
222 sps->bitstream_restriction_flag = 0;
f4348e6c
MN
223 }
224
330ad1f6
DB
225 if (sps->num_reorder_frames > 16U
226 /* max_dec_frame_buffering || max_dec_frame_buffering > 16 */) {
227 av_log(h->avctx, AV_LOG_ERROR,
9ecabd78
AK
228 "Clipping illegal num_reorder_frames %d\n",
229 sps->num_reorder_frames);
230 sps->num_reorder_frames = 16;
c18838f5 231 return AVERROR_INVALIDDATA;
889fce8e
MN
232 }
233 }
2c541554 234 if (get_bits_left(&h->gb) < 0) {
330ad1f6
DB
235 av_log(h->avctx, AV_LOG_ERROR,
236 "Overread VUI by %d bits\n", -get_bits_left(&h->gb));
3aa661ec
MN
237 return AVERROR_INVALIDDATA;
238 }
889fce8e
MN
239
240 return 0;
241}
242
243static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
330ad1f6
DB
244 const uint8_t *jvt_list,
245 const uint8_t *fallback_list)
246{
889fce8e 247 int i, last = 8, next = 8;
f4d581cd 248 const uint8_t *scan = size == 16 ? ff_zigzag_scan : ff_zigzag_direct;
330ad1f6
DB
249 if (!get_bits1(&h->gb)) /* matrix not written, we use the predicted one */
250 memcpy(factors, fallback_list, size * sizeof(uint8_t));
889fce8e 251 else
330ad1f6
DB
252 for (i = 0; i < size; i++) {
253 if (next)
254 next = (last + get_se_golomb(&h->gb)) & 0xff;
255 if (!i && !next) { /* matrix not written, we use the preset one */
256 memcpy(factors, jvt_list, size * sizeof(uint8_t));
257 break;
258 }
259 last = factors[scan[i]] = next ? next : last;
889fce8e 260 }
889fce8e
MN
261}
262
330ad1f6
DB
263static void decode_scaling_matrices(H264Context *h, SPS *sps,
264 PPS *pps, int is_sps,
265 uint8_t(*scaling_matrix4)[16],
266 uint8_t(*scaling_matrix8)[64])
267{
889fce8e
MN
268 int fallback_sps = !is_sps && sps->scaling_matrix_present;
269 const uint8_t *fallback[4] = {
270 fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
271 fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
272 fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
c90b9442 273 fallback_sps ? sps->scaling_matrix8[3] : default_scaling8[1]
889fce8e 274 };
330ad1f6 275 if (get_bits1(&h->gb)) {
889fce8e 276 sps->scaling_matrix_present |= is_sps;
330ad1f6
DB
277 decode_scaling_list(h, scaling_matrix4[0], 16, default_scaling4[0], fallback[0]); // Intra, Y
278 decode_scaling_list(h, scaling_matrix4[1], 16, default_scaling4[0], scaling_matrix4[0]); // Intra, Cr
279 decode_scaling_list(h, scaling_matrix4[2], 16, default_scaling4[0], scaling_matrix4[1]); // Intra, Cb
280 decode_scaling_list(h, scaling_matrix4[3], 16, default_scaling4[1], fallback[1]); // Inter, Y
281 decode_scaling_list(h, scaling_matrix4[4], 16, default_scaling4[1], scaling_matrix4[3]); // Inter, Cr
282 decode_scaling_list(h, scaling_matrix4[5], 16, default_scaling4[1], scaling_matrix4[4]); // Inter, Cb
283 if (is_sps || pps->transform_8x8_mode) {
284 decode_scaling_list(h, scaling_matrix8[0], 64, default_scaling8[0], fallback[2]); // Intra, Y
285 if (sps->chroma_format_idc == 3) {
286 decode_scaling_list(h, scaling_matrix8[1], 64, default_scaling8[0], scaling_matrix8[0]); // Intra, Cr
287 decode_scaling_list(h, scaling_matrix8[2], 64, default_scaling8[0], scaling_matrix8[1]); // Intra, Cb
c90b9442 288 }
330ad1f6
DB
289 decode_scaling_list(h, scaling_matrix8[3], 64, default_scaling8[1], fallback[3]); // Inter, Y
290 if (sps->chroma_format_idc == 3) {
291 decode_scaling_list(h, scaling_matrix8[4], 64, default_scaling8[1], scaling_matrix8[3]); // Inter, Cr
292 decode_scaling_list(h, scaling_matrix8[5], 64, default_scaling8[1], scaling_matrix8[4]); // Inter, Cb
c90b9442 293 }
889fce8e
MN
294 }
295 }
296}
297
330ad1f6
DB
298int ff_h264_decode_seq_parameter_set(H264Context *h)
299{
fe9a3fbe 300 int profile_idc, level_idc, constraint_set_flags = 0;
889fce8e 301 unsigned int sps_id;
d7d6efe4 302 int i, log2_max_frame_num_minus4;
889fce8e
MN
303 SPS *sps;
304
330ad1f6
DB
305 profile_idc = get_bits(&h->gb, 8);
306 constraint_set_flags |= get_bits1(&h->gb) << 0; // constraint_set0_flag
307 constraint_set_flags |= get_bits1(&h->gb) << 1; // constraint_set1_flag
308 constraint_set_flags |= get_bits1(&h->gb) << 2; // constraint_set2_flag
309 constraint_set_flags |= get_bits1(&h->gb) << 3; // constraint_set3_flag
ee6280ca
VG
310 constraint_set_flags |= get_bits1(&h->gb) << 4; // constraint_set4_flag
311 constraint_set_flags |= get_bits1(&h->gb) << 5; // constraint_set5_flag
312 skip_bits(&h->gb, 2); // reserved_zero_2bits
330ad1f6
DB
313 level_idc = get_bits(&h->gb, 8);
314 sps_id = get_ue_golomb_31(&h->gb);
889fce8e 315
330ad1f6 316 if (sps_id >= MAX_SPS_COUNT) {
73e8fab3 317 av_log(h->avctx, AV_LOG_ERROR, "sps_id %u out of range\n", sps_id);
c18838f5 318 return AVERROR_INVALIDDATA;
889fce8e 319 }
330ad1f6 320 sps = av_mallocz(sizeof(SPS));
c18838f5
DB
321 if (!sps)
322 return AVERROR(ENOMEM);
889fce8e 323
3a057670 324 sps->sps_id = sps_id;
330ad1f6
DB
325 sps->time_offset_length = 24;
326 sps->profile_idc = profile_idc;
fe9a3fbe 327 sps->constraint_set_flags = constraint_set_flags;
330ad1f6 328 sps->level_idc = level_idc;
889fce8e
MN
329
330 memset(sps->scaling_matrix4, 16, sizeof(sps->scaling_matrix4));
331 memset(sps->scaling_matrix8, 16, sizeof(sps->scaling_matrix8));
332 sps->scaling_matrix_present = 0;
333
e70ab7c1
VG
334 if (sps->profile_idc == 100 || // High profile
335 sps->profile_idc == 110 || // High10 profile
336 sps->profile_idc == 122 || // High422 profile
337 sps->profile_idc == 244 || // High444 Predictive profile
338 sps->profile_idc == 44 || // Cavlc444 profile
339 sps->profile_idc == 83 || // Scalable Constrained High profile (SVC)
340 sps->profile_idc == 86 || // Scalable High Intra profile (SVC)
341 sps->profile_idc == 118 || // Stereo High profile (MVC)
342 sps->profile_idc == 128 || // Multiview High profile (MVC)
343 sps->profile_idc == 138 || // Multiview Depth High profile (MVCD)
344 sps->profile_idc == 144) { // old High444 profile
330ad1f6
DB
345 sps->chroma_format_idc = get_ue_golomb_31(&h->gb);
346 if (sps->chroma_format_idc > 3) {
614b9e4d
VG
347 avpriv_request_sample(h->avctx, "chroma_format_idc %u",
348 sps->chroma_format_idc);
cb348677 349 goto fail;
330ad1f6 350 } else if (sps->chroma_format_idc == 3) {
2c541554 351 sps->residual_color_transform_flag = get_bits1(&h->gb);
6ef40639 352 }
2c541554
AK
353 sps->bit_depth_luma = get_ue_golomb(&h->gb) + 8;
354 sps->bit_depth_chroma = get_ue_golomb(&h->gb) + 8;
bfd26b7c
AK
355 if (sps->bit_depth_chroma != sps->bit_depth_luma) {
356 avpriv_request_sample(h->avctx,
357 "Different chroma and luma bit depth");
358 goto fail;
359 }
2c541554 360 sps->transform_bypass = get_bits1(&h->gb);
330ad1f6
DB
361 decode_scaling_matrices(h, sps, NULL, 1,
362 sps->scaling_matrix4, sps->scaling_matrix8);
363 } else {
364 sps->chroma_format_idc = 1;
365 sps->bit_depth_luma = 8;
366 sps->bit_depth_chroma = 8;
889fce8e
MN
367 }
368
2c541554 369 log2_max_frame_num_minus4 = get_ue_golomb(&h->gb);
d7d6efe4
JG
370 if (log2_max_frame_num_minus4 < MIN_LOG2_MAX_FRAME_NUM - 4 ||
371 log2_max_frame_num_minus4 > MAX_LOG2_MAX_FRAME_NUM - 4) {
2c541554 372 av_log(h->avctx, AV_LOG_ERROR,
d7d6efe4
JG
373 "log2_max_frame_num_minus4 out of range (0-12): %d\n",
374 log2_max_frame_num_minus4);
a421bbfe 375 goto fail;
d7d6efe4
JG
376 }
377 sps->log2_max_frame_num = log2_max_frame_num_minus4 + 4;
378
330ad1f6 379 sps->poc_type = get_ue_golomb_31(&h->gb);
889fce8e 380
330ad1f6
DB
381 if (sps->poc_type == 0) { // FIXME #define
382 sps->log2_max_poc_lsb = get_ue_golomb(&h->gb) + 4;
383 } else if (sps->poc_type == 1) { // FIXME #define
384 sps->delta_pic_order_always_zero_flag = get_bits1(&h->gb);
385 sps->offset_for_non_ref_pic = get_se_golomb(&h->gb);
386 sps->offset_for_top_to_bottom_field = get_se_golomb(&h->gb);
387 sps->poc_cycle_length = get_ue_golomb(&h->gb);
889fce8e 388
330ad1f6
DB
389 if ((unsigned)sps->poc_cycle_length >=
390 FF_ARRAY_ELEMS(sps->offset_for_ref_frame)) {
391 av_log(h->avctx, AV_LOG_ERROR,
cc8163e1 392 "poc_cycle_length overflow %d\n", sps->poc_cycle_length);
889fce8e
MN
393 goto fail;
394 }
395
330ad1f6
DB
396 for (i = 0; i < sps->poc_cycle_length; i++)
397 sps->offset_for_ref_frame[i] = get_se_golomb(&h->gb);
398 } else if (sps->poc_type != 2) {
2c541554 399 av_log(h->avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
889fce8e
MN
400 goto fail;
401 }
402
330ad1f6 403 sps->ref_frame_count = get_ue_golomb_31(&h->gb);
5d1c2e53 404 if (sps->ref_frame_count > H264_MAX_PICTURE_COUNT - 2 ||
330ad1f6 405 sps->ref_frame_count >= 32U) {
73e8fab3
VG
406 av_log(h->avctx, AV_LOG_ERROR,
407 "too many reference frames %d\n", sps->ref_frame_count);
889fce8e
MN
408 goto fail;
409 }
330ad1f6
DB
410 sps->gaps_in_frame_num_allowed_flag = get_bits1(&h->gb);
411 sps->mb_width = get_ue_golomb(&h->gb) + 1;
412 sps->mb_height = get_ue_golomb(&h->gb) + 1;
413 if ((unsigned)sps->mb_width >= INT_MAX / 16 ||
414 (unsigned)sps->mb_height >= INT_MAX / 16 ||
415 av_image_check_size(16 * sps->mb_width,
416 16 * sps->mb_height, 0, h->avctx)) {
2c541554 417 av_log(h->avctx, AV_LOG_ERROR, "mb_width/height overflow\n");
889fce8e
MN
418 goto fail;
419 }
420
330ad1f6
DB
421 sps->frame_mbs_only_flag = get_bits1(&h->gb);
422 if (!sps->frame_mbs_only_flag)
423 sps->mb_aff = get_bits1(&h->gb);
889fce8e 424 else
330ad1f6 425 sps->mb_aff = 0;
889fce8e 426
330ad1f6
DB
427 sps->direct_8x8_inference_flag = get_bits1(&h->gb);
428 if (!sps->frame_mbs_only_flag && !sps->direct_8x8_inference_flag) {
429 av_log(h->avctx, AV_LOG_ERROR,
430 "This stream was generated by a broken encoder, invalid 8x8 inference\n");
1c3ce2cd
MN
431 goto fail;
432 }
889fce8e
MN
433
434#ifndef ALLOW_INTERLACE
330ad1f6
DB
435 if (sps->mb_aff)
436 av_log(h->avctx, AV_LOG_ERROR,
437 "MBAFF support not included; enable it at compile-time.\n");
889fce8e 438#endif
330ad1f6
DB
439 sps->crop = get_bits1(&h->gb);
440 if (sps->crop) {
d8a45d2d
AK
441 unsigned int crop_left = get_ue_golomb(&h->gb);
442 unsigned int crop_right = get_ue_golomb(&h->gb);
443 unsigned int crop_top = get_ue_golomb(&h->gb);
444 unsigned int crop_bottom = get_ue_golomb(&h->gb);
5e83d9ac 445
7c6eb0a1 446 if (h->avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {
5e83d9ac 447 av_log(h->avctx, AV_LOG_DEBUG, "discarding sps cropping, original "
cc8163e1 448 "values are l:%d r:%d t:%d b:%d\n",
330ad1f6 449 crop_left, crop_right, crop_top, crop_bottom);
a84fb6e0
VG
450
451 sps->crop_left =
452 sps->crop_right =
453 sps->crop_top =
454 sps->crop_bottom = 0;
5e83d9ac 455 } else {
330ad1f6
DB
456 int vsub = (sps->chroma_format_idc == 1) ? 1 : 0;
457 int hsub = (sps->chroma_format_idc == 1 ||
458 sps->chroma_format_idc == 2) ? 1 : 0;
5e83d9ac
AK
459 int step_x = 1 << hsub;
460 int step_y = (2 - sps->frame_mbs_only_flag) << vsub;
461
462 if (crop_left & (0x1F >> (sps->bit_depth_luma > 8)) &&
7c6eb0a1 463 !(h->avctx->flags & AV_CODEC_FLAG_UNALIGNED)) {
5e83d9ac 464 crop_left &= ~(0x1F >> (sps->bit_depth_luma > 8));
330ad1f6
DB
465 av_log(h->avctx, AV_LOG_WARNING,
466 "Reducing left cropping to %d "
5e83d9ac
AK
467 "chroma samples to preserve alignment.\n",
468 crop_left);
469 }
470
d8a45d2d
AK
471 if (INT_MAX / step_x <= crop_left ||
472 INT_MAX / step_x - crop_left <= crop_right ||
473 16 * sps->mb_width <= step_x * (crop_left + crop_right) ||
474 INT_MAX / step_y <= crop_top ||
475 INT_MAX / step_y - crop_top <= crop_bottom ||
476 16 * sps->mb_height <= step_y * (crop_top + crop_bottom)) {
477 av_log(h->avctx, AV_LOG_WARNING, "Invalid crop parameters\n");
478 if (h->avctx->err_recognition & AV_EF_EXPLODE)
479 goto fail;
480 crop_left = crop_right = crop_top = crop_bottom = 0;
481 }
482
5e83d9ac
AK
483 sps->crop_left = crop_left * step_x;
484 sps->crop_right = crop_right * step_x;
485 sps->crop_top = crop_top * step_y;
486 sps->crop_bottom = crop_bottom * step_y;
889fce8e 487 }
330ad1f6
DB
488 } else {
489 sps->crop_left =
490 sps->crop_right =
491 sps->crop_top =
492 sps->crop_bottom =
493 sps->crop = 0;
889fce8e
MN
494 }
495
330ad1f6 496 sps->vui_parameters_present_flag = get_bits1(&h->gb);
94603feb
AK
497 if (sps->vui_parameters_present_flag) {
498 int ret = decode_vui_parameters(h, sps);
499 if (ret < 0 && h->avctx->err_recognition & AV_EF_EXPLODE)
889fce8e 500 goto fail;
94603feb 501 }
889fce8e 502
b09ad37c
AK
503 /* if the maximum delay is not stored in the SPS, derive it based on the
504 * level */
505 if (!sps->bitstream_restriction_flag) {
506 sps->num_reorder_frames = MAX_DELAYED_PIC_COUNT - 1;
507 for (i = 0; i < FF_ARRAY_ELEMS(level_max_dpb_mbs); i++) {
508 if (level_max_dpb_mbs[i][0] == sps->level_idc) {
509 sps->num_reorder_frames = FFMIN(level_max_dpb_mbs[i][1] / (sps->mb_width * sps->mb_height),
510 sps->num_reorder_frames);
511 break;
512 }
513 }
514 }
515
330ad1f6
DB
516 if (!sps->sar.den)
517 sps->sar.den = 1;
cfa5a81e 518
330ad1f6 519 if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
be391fb6 520 static const char csp[4][5] = { "Gray", "420", "422", "444" };
330ad1f6 521 av_log(h->avctx, AV_LOG_DEBUG,
cba4e606 522 "sps:%u profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%u/%u/%u/%u %s %s %"PRId32"/%"PRId32"\n",
889fce8e
MN
523 sps_id, sps->profile_idc, sps->level_idc,
524 sps->poc_type,
525 sps->ref_frame_count,
526 sps->mb_width, sps->mb_height,
527 sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
528 sps->direct_8x8_inference_flag ? "8B8" : "",
529 sps->crop_left, sps->crop_right,
530 sps->crop_top, sps->crop_bottom,
531 sps->vui_parameters_present_flag ? "VUI" : "",
be391fb6 532 csp[sps->chroma_format_idc],
889fce8e 533 sps->timing_info_present_flag ? sps->num_units_in_tick : 0,
330ad1f6 534 sps->timing_info_present_flag ? sps->time_scale : 0);
889fce8e 535 }
072be3e8 536 sps->new = 1;
889fce8e
MN
537
538 av_free(h->sps_buffers[sps_id]);
072be3e8
JG
539 h->sps_buffers[sps_id] = sps;
540 h->sps = *sps;
072be3e8 541
889fce8e 542 return 0;
330ad1f6 543
889fce8e
MN
544fail:
545 av_free(sps);
86e1a358 546 return AVERROR_INVALIDDATA;
889fce8e
MN
547}
548
330ad1f6 549static void build_qp_table(PPS *pps, int t, int index, const int depth)
889fce8e
MN
550{
551 int i;
330ad1f6
DB
552 const int max_qp = 51 + 6 * (depth - 8);
553 for (i = 0; i < max_qp + 1; i++)
554 pps->chroma_qp_table[t][i] =
555 ff_h264_chroma_qp[depth - 8][av_clip(i + index, 0, max_qp)];
889fce8e
MN
556}
557
330ad1f6
DB
558int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length)
559{
84f226a3 560 const SPS *sps;
330ad1f6 561 unsigned int pps_id = get_ue_golomb(&h->gb);
889fce8e 562 PPS *pps;
84f226a3 563 int qp_bd_offset;
be1242a3 564 int bits_left;
86e1a358 565 int ret;
889fce8e 566
330ad1f6 567 if (pps_id >= MAX_PPS_COUNT) {
73e8fab3 568 av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", pps_id);
c18838f5 569 return AVERROR_INVALIDDATA;
889fce8e
MN
570 }
571
330ad1f6 572 pps = av_mallocz(sizeof(PPS));
c18838f5
DB
573 if (!pps)
574 return AVERROR(ENOMEM);
330ad1f6
DB
575 pps->sps_id = get_ue_golomb_31(&h->gb);
576 if ((unsigned)pps->sps_id >= MAX_SPS_COUNT ||
f929ab05 577 !h->sps_buffers[pps->sps_id]) {
73e8fab3 578 av_log(h->avctx, AV_LOG_ERROR, "sps_id %u out of range\n", pps->sps_id);
86e1a358 579 ret = AVERROR_INVALIDDATA;
889fce8e
MN
580 goto fail;
581 }
84f226a3
AK
582 sps = h->sps_buffers[pps->sps_id];
583
584 if (sps->bit_depth_luma > 10) {
585 av_log(h->avctx, AV_LOG_ERROR,
586 "Unimplemented luma bit depth=%d (max=10)\n",
587 sps->bit_depth_luma);
86e1a358
VG
588 ret = AVERROR_PATCHWELCOME;
589 goto fail;
84f226a3 590 }
889fce8e 591
330ad1f6
DB
592 pps->cabac = get_bits1(&h->gb);
593 pps->pic_order_present = get_bits1(&h->gb);
594 pps->slice_group_count = get_ue_golomb(&h->gb) + 1;
595 if (pps->slice_group_count > 1) {
596 pps->mb_slice_group_map_type = get_ue_golomb(&h->gb);
2c541554 597 av_log(h->avctx, AV_LOG_ERROR, "FMO not supported\n");
330ad1f6 598 switch (pps->mb_slice_group_map_type) {
889fce8e
MN
599 case 0:
600#if 0
330ad1f6
DB
601 | for (i = 0; i <= num_slice_groups_minus1; i++) | | |
602 | run_length[i] |1 |ue(v) |
889fce8e
MN
603#endif
604 break;
605 case 2:
606#if 0
330ad1f6
DB
607 | for (i = 0; i < num_slice_groups_minus1; i++) { | | |
608 | top_left_mb[i] |1 |ue(v) |
609 | bottom_right_mb[i] |1 |ue(v) |
610 | } | | |
889fce8e
MN
611#endif
612 break;
613 case 3:
614 case 4:
615 case 5:
616#if 0
330ad1f6
DB
617 | slice_group_change_direction_flag |1 |u(1) |
618 | slice_group_change_rate_minus1 |1 |ue(v) |
889fce8e
MN
619#endif
620 break;
621 case 6:
622#if 0
330ad1f6
DB
623 | slice_group_id_cnt_minus1 |1 |ue(v) |
624 | for (i = 0; i <= slice_group_id_cnt_minus1; i++)| | |
625 | slice_group_id[i] |1 |u(v) |
889fce8e
MN
626#endif
627 break;
628 }
629 }
330ad1f6
DB
630 pps->ref_count[0] = get_ue_golomb(&h->gb) + 1;
631 pps->ref_count[1] = get_ue_golomb(&h->gb) + 1;
632 if (pps->ref_count[0] - 1 > 32 - 1 || pps->ref_count[1] - 1 > 32 - 1) {
2c541554 633 av_log(h->avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
86e1a358 634 ret = AVERROR_INVALIDDATA;
889fce8e
MN
635 goto fail;
636 }
637
84f226a3
AK
638 qp_bd_offset = 6 * (sps->bit_depth_luma - 8);
639
330ad1f6
DB
640 pps->weighted_pred = get_bits1(&h->gb);
641 pps->weighted_bipred_idc = get_bits(&h->gb, 2);
642 pps->init_qp = get_se_golomb(&h->gb) + 26 + qp_bd_offset;
643 pps->init_qs = get_se_golomb(&h->gb) + 26 + qp_bd_offset;
644 pps->chroma_qp_index_offset[0] = get_se_golomb(&h->gb);
645 pps->deblocking_filter_parameters_present = get_bits1(&h->gb);
646 pps->constrained_intra_pred = get_bits1(&h->gb);
647 pps->redundant_pic_cnt_present = get_bits1(&h->gb);
648
649 pps->transform_8x8_mode = 0;
650 // contents of sps/pps can change even if id doesn't, so reinit
651 h->dequant_coeff_pps = -1;
652 memcpy(pps->scaling_matrix4, h->sps_buffers[pps->sps_id]->scaling_matrix4,
653 sizeof(pps->scaling_matrix4));
654 memcpy(pps->scaling_matrix8, h->sps_buffers[pps->sps_id]->scaling_matrix8,
655 sizeof(pps->scaling_matrix8));
889fce8e 656
2c541554 657 bits_left = bit_length - get_bits_count(&h->gb);
be1242a3 658 if (bits_left && (bits_left > 8 ||
2c541554 659 show_bits(&h->gb, bits_left) != 1 << (bits_left - 1))) {
330ad1f6
DB
660 pps->transform_8x8_mode = get_bits1(&h->gb);
661 decode_scaling_matrices(h, h->sps_buffers[pps->sps_id], pps, 0,
662 pps->scaling_matrix4, pps->scaling_matrix8);
663 // second_chroma_qp_index_offset
664 pps->chroma_qp_index_offset[1] = get_se_golomb(&h->gb);
889fce8e 665 } else {
330ad1f6 666 pps->chroma_qp_index_offset[1] = pps->chroma_qp_index_offset[0];
889fce8e
MN
667 }
668
330ad1f6 669 build_qp_table(pps, 0, pps->chroma_qp_index_offset[0],
84f226a3 670 sps->bit_depth_luma);
330ad1f6 671 build_qp_table(pps, 1, pps->chroma_qp_index_offset[1],
84f226a3 672 sps->bit_depth_luma);
330ad1f6
DB
673 if (pps->chroma_qp_index_offset[0] != pps->chroma_qp_index_offset[1])
674 pps->chroma_qp_diff = 1;
889fce8e 675
330ad1f6
DB
676 if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
677 av_log(h->avctx, AV_LOG_DEBUG,
cc8163e1 678 "pps:%u sps:%u %s slice_groups:%d ref:%u/%u %s qp:%d/%d/%d/%d %s %s %s %s\n",
889fce8e
MN
679 pps_id, pps->sps_id,
680 pps->cabac ? "CABAC" : "CAVLC",
681 pps->slice_group_count,
682 pps->ref_count[0], pps->ref_count[1],
683 pps->weighted_pred ? "weighted" : "",
684 pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset[0], pps->chroma_qp_index_offset[1],
685 pps->deblocking_filter_parameters_present ? "LPAR" : "",
686 pps->constrained_intra_pred ? "CONSTR" : "",
687 pps->redundant_pic_cnt_present ? "REDU" : "",
330ad1f6 688 pps->transform_8x8_mode ? "8x8DCT" : "");
889fce8e
MN
689 }
690
691 av_free(h->pps_buffers[pps_id]);
330ad1f6 692 h->pps_buffers[pps_id] = pps;
889fce8e 693 return 0;
330ad1f6 694
889fce8e
MN
695fail:
696 av_free(pps);
86e1a358 697 return ret;
889fce8e 698}