svq3: drop the build dependency on the h264 decoder
[libav.git] / libavcodec / h264_ps.c
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 Libav.
6 *
7 * Libav 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 * Libav 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 Libav; 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 / MPEG4 part10 parameter set decoding.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28 #include <inttypes.h>
29
30 #include "libavutil/imgutils.h"
31 #include "internal.h"
32 #include "mathops.h"
33 #include "avcodec.h"
34 #include "h264.h"
35 #include "h264data.h"
36 #include "golomb.h"
37
38 #define MAX_LOG2_MAX_FRAME_NUM (12 + 4)
39 #define MIN_LOG2_MAX_FRAME_NUM 4
40
41 static 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 },
59 };
60
61 static 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
68 static 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
87 /* maximum number of MBs in the DPB for a given level */
88 static 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
107 static inline int decode_hrd_parameters(H264Context *h, SPS *sps)
108 {
109 int cpb_count, i;
110 cpb_count = get_ue_golomb_31(&h->gb) + 1;
111
112 if (cpb_count > 32U) {
113 av_log(h->avctx, AV_LOG_ERROR, "cpb_count %d invalid\n", cpb_count);
114 return AVERROR_INVALIDDATA;
115 }
116
117 get_bits(&h->gb, 4); /* bit_rate_scale */
118 get_bits(&h->gb, 4); /* cpb_size_scale */
119 for (i = 0; i < cpb_count; i++) {
120 get_ue_golomb_long(&h->gb); /* bit_rate_value_minus1 */
121 get_ue_golomb_long(&h->gb); /* cpb_size_value_minus1 */
122 get_bits1(&h->gb); /* cbr_flag */
123 }
124 sps->initial_cpb_removal_delay_length = get_bits(&h->gb, 5) + 1;
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;
129 return 0;
130 }
131
132 static inline int decode_vui_parameters(H264Context *h, SPS *sps)
133 {
134 int aspect_ratio_info_present_flag;
135 unsigned int aspect_ratio_idc;
136
137 aspect_ratio_info_present_flag = get_bits1(&h->gb);
138
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 {
147 av_log(h->avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
148 return AVERROR_INVALIDDATA;
149 }
150 } else {
151 sps->sar.num =
152 sps->sar.den = 0;
153 }
154
155 if (get_bits1(&h->gb)) /* overscan_info_present_flag */
156 get_bits1(&h->gb); /* overscan_appropriate_flag */
157
158 sps->video_signal_type_present_flag = get_bits1(&h->gb);
159 if (sps->video_signal_type_present_flag) {
160 get_bits(&h->gb, 3); /* video_format */
161 sps->full_range = get_bits1(&h->gb); /* video_full_range_flag */
162
163 sps->colour_description_present_flag = get_bits1(&h->gb);
164 if (sps->colour_description_present_flag) {
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 */
168 if (sps->color_primaries >= AVCOL_PRI_NB)
169 sps->color_primaries = AVCOL_PRI_UNSPECIFIED;
170 if (sps->color_trc >= AVCOL_TRC_NB)
171 sps->color_trc = AVCOL_TRC_UNSPECIFIED;
172 if (sps->colorspace >= AVCOL_SPC_NB)
173 sps->colorspace = AVCOL_SPC_UNSPECIFIED;
174 }
175 }
176
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;
181 get_ue_golomb(&h->gb); /* chroma_sample_location_type_bottom_field */
182 }
183
184 sps->timing_info_present_flag = get_bits1(&h->gb);
185 if (sps->timing_info_present_flag) {
186 sps->num_units_in_tick = get_bits_long(&h->gb, 32);
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,
190 "time_scale/num_units_in_tick invalid or unsupported (%"PRIu32"/%"PRIu32")\n",
191 sps->time_scale, sps->num_units_in_tick);
192 return AVERROR_INVALIDDATA;
193 }
194 sps->fixed_frame_rate_flag = get_bits1(&h->gb);
195 }
196
197 sps->nal_hrd_parameters_present_flag = get_bits1(&h->gb);
198 if (sps->nal_hrd_parameters_present_flag)
199 if (decode_hrd_parameters(h, sps) < 0)
200 return AVERROR_INVALIDDATA;
201 sps->vcl_hrd_parameters_present_flag = get_bits1(&h->gb);
202 if (sps->vcl_hrd_parameters_present_flag)
203 if (decode_hrd_parameters(h, sps) < 0)
204 return AVERROR_INVALIDDATA;
205 if (sps->nal_hrd_parameters_present_flag ||
206 sps->vcl_hrd_parameters_present_flag)
207 get_bits1(&h->gb); /* low_delay_hrd_flag */
208 sps->pic_struct_present_flag = get_bits1(&h->gb);
209
210 sps->bitstream_restriction_flag = get_bits1(&h->gb);
211 if (sps->bitstream_restriction_flag) {
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 */
217 sps->num_reorder_frames = get_ue_golomb(&h->gb);
218 get_ue_golomb(&h->gb); /*max_dec_frame_buffering*/
219
220 if (get_bits_left(&h->gb) < 0) {
221 sps->num_reorder_frames = 0;
222 sps->bitstream_restriction_flag = 0;
223 }
224
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,
228 "Clipping illegal num_reorder_frames %d\n",
229 sps->num_reorder_frames);
230 sps->num_reorder_frames = 16;
231 return AVERROR_INVALIDDATA;
232 }
233 }
234 if (get_bits_left(&h->gb) < 0) {
235 av_log(h->avctx, AV_LOG_ERROR,
236 "Overread VUI by %d bits\n", -get_bits_left(&h->gb));
237 return AVERROR_INVALIDDATA;
238 }
239
240 return 0;
241 }
242
243 static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
244 const uint8_t *jvt_list,
245 const uint8_t *fallback_list)
246 {
247 int i, last = 8, next = 8;
248 const uint8_t *scan = size == 16 ? ff_zigzag_scan : ff_zigzag_direct;
249 if (!get_bits1(&h->gb)) /* matrix not written, we use the predicted one */
250 memcpy(factors, fallback_list, size * sizeof(uint8_t));
251 else
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;
260 }
261 }
262
263 static 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 {
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],
273 fallback_sps ? sps->scaling_matrix8[3] : default_scaling8[1]
274 };
275 if (get_bits1(&h->gb)) {
276 sps->scaling_matrix_present |= is_sps;
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
288 }
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
293 }
294 }
295 }
296 }
297
298 int ff_h264_decode_seq_parameter_set(H264Context *h)
299 {
300 int profile_idc, level_idc, constraint_set_flags = 0;
301 unsigned int sps_id;
302 int i, log2_max_frame_num_minus4;
303 SPS *sps;
304
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
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
313 level_idc = get_bits(&h->gb, 8);
314 sps_id = get_ue_golomb_31(&h->gb);
315
316 if (sps_id >= MAX_SPS_COUNT) {
317 av_log(h->avctx, AV_LOG_ERROR, "sps_id %u out of range\n", sps_id);
318 return AVERROR_INVALIDDATA;
319 }
320 sps = av_mallocz(sizeof(SPS));
321 if (!sps)
322 return AVERROR(ENOMEM);
323
324 sps->sps_id = sps_id;
325 sps->time_offset_length = 24;
326 sps->profile_idc = profile_idc;
327 sps->constraint_set_flags = constraint_set_flags;
328 sps->level_idc = level_idc;
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
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
345 sps->chroma_format_idc = get_ue_golomb_31(&h->gb);
346 if (sps->chroma_format_idc > 3) {
347 avpriv_request_sample(h->avctx, "chroma_format_idc %u",
348 sps->chroma_format_idc);
349 goto fail;
350 } else if (sps->chroma_format_idc == 3) {
351 sps->residual_color_transform_flag = get_bits1(&h->gb);
352 }
353 sps->bit_depth_luma = get_ue_golomb(&h->gb) + 8;
354 sps->bit_depth_chroma = get_ue_golomb(&h->gb) + 8;
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 }
360 sps->transform_bypass = get_bits1(&h->gb);
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;
367 }
368
369 log2_max_frame_num_minus4 = get_ue_golomb(&h->gb);
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) {
372 av_log(h->avctx, AV_LOG_ERROR,
373 "log2_max_frame_num_minus4 out of range (0-12): %d\n",
374 log2_max_frame_num_minus4);
375 goto fail;
376 }
377 sps->log2_max_frame_num = log2_max_frame_num_minus4 + 4;
378
379 sps->poc_type = get_ue_golomb_31(&h->gb);
380
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);
388
389 if ((unsigned)sps->poc_cycle_length >=
390 FF_ARRAY_ELEMS(sps->offset_for_ref_frame)) {
391 av_log(h->avctx, AV_LOG_ERROR,
392 "poc_cycle_length overflow %d\n", sps->poc_cycle_length);
393 goto fail;
394 }
395
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) {
399 av_log(h->avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
400 goto fail;
401 }
402
403 sps->ref_frame_count = get_ue_golomb_31(&h->gb);
404 if (sps->ref_frame_count > H264_MAX_PICTURE_COUNT - 2 ||
405 sps->ref_frame_count >= 32U) {
406 av_log(h->avctx, AV_LOG_ERROR,
407 "too many reference frames %d\n", sps->ref_frame_count);
408 goto fail;
409 }
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)) {
417 av_log(h->avctx, AV_LOG_ERROR, "mb_width/height overflow\n");
418 goto fail;
419 }
420
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);
424 else
425 sps->mb_aff = 0;
426
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");
431 goto fail;
432 }
433
434 #ifndef ALLOW_INTERLACE
435 if (sps->mb_aff)
436 av_log(h->avctx, AV_LOG_ERROR,
437 "MBAFF support not included; enable it at compile-time.\n");
438 #endif
439 sps->crop = get_bits1(&h->gb);
440 if (sps->crop) {
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);
445
446 if (h->avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {
447 av_log(h->avctx, AV_LOG_DEBUG, "discarding sps cropping, original "
448 "values are l:%d r:%d t:%d b:%d\n",
449 crop_left, crop_right, crop_top, crop_bottom);
450
451 sps->crop_left =
452 sps->crop_right =
453 sps->crop_top =
454 sps->crop_bottom = 0;
455 } else {
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;
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)) &&
463 !(h->avctx->flags & AV_CODEC_FLAG_UNALIGNED)) {
464 crop_left &= ~(0x1F >> (sps->bit_depth_luma > 8));
465 av_log(h->avctx, AV_LOG_WARNING,
466 "Reducing left cropping to %d "
467 "chroma samples to preserve alignment.\n",
468 crop_left);
469 }
470
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
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;
487 }
488 } else {
489 sps->crop_left =
490 sps->crop_right =
491 sps->crop_top =
492 sps->crop_bottom =
493 sps->crop = 0;
494 }
495
496 sps->vui_parameters_present_flag = get_bits1(&h->gb);
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)
500 goto fail;
501 }
502
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
516 if (!sps->sar.den)
517 sps->sar.den = 1;
518
519 if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
520 static const char csp[4][5] = { "Gray", "420", "422", "444" };
521 av_log(h->avctx, AV_LOG_DEBUG,
522 "sps:%u profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%u/%u/%u/%u %s %s %"PRId32"/%"PRId32"\n",
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" : "",
532 csp[sps->chroma_format_idc],
533 sps->timing_info_present_flag ? sps->num_units_in_tick : 0,
534 sps->timing_info_present_flag ? sps->time_scale : 0);
535 }
536 sps->new = 1;
537
538 av_free(h->sps_buffers[sps_id]);
539 h->sps_buffers[sps_id] = sps;
540 h->sps = *sps;
541
542 return 0;
543
544 fail:
545 av_free(sps);
546 return AVERROR_INVALIDDATA;
547 }
548
549 static void build_qp_table(PPS *pps, int t, int index, const int depth)
550 {
551 int i;
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)];
556 }
557
558 int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length)
559 {
560 const SPS *sps;
561 unsigned int pps_id = get_ue_golomb(&h->gb);
562 PPS *pps;
563 int qp_bd_offset;
564 int bits_left;
565 int ret;
566
567 if (pps_id >= MAX_PPS_COUNT) {
568 av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", pps_id);
569 return AVERROR_INVALIDDATA;
570 }
571
572 pps = av_mallocz(sizeof(PPS));
573 if (!pps)
574 return AVERROR(ENOMEM);
575 pps->sps_id = get_ue_golomb_31(&h->gb);
576 if ((unsigned)pps->sps_id >= MAX_SPS_COUNT ||
577 !h->sps_buffers[pps->sps_id]) {
578 av_log(h->avctx, AV_LOG_ERROR, "sps_id %u out of range\n", pps->sps_id);
579 ret = AVERROR_INVALIDDATA;
580 goto fail;
581 }
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);
588 ret = AVERROR_PATCHWELCOME;
589 goto fail;
590 }
591
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);
597 av_log(h->avctx, AV_LOG_ERROR, "FMO not supported\n");
598 switch (pps->mb_slice_group_map_type) {
599 case 0:
600 #if 0
601 | for (i = 0; i <= num_slice_groups_minus1; i++) | | |
602 | run_length[i] |1 |ue(v) |
603 #endif
604 break;
605 case 2:
606 #if 0
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 | } | | |
611 #endif
612 break;
613 case 3:
614 case 4:
615 case 5:
616 #if 0
617 | slice_group_change_direction_flag |1 |u(1) |
618 | slice_group_change_rate_minus1 |1 |ue(v) |
619 #endif
620 break;
621 case 6:
622 #if 0
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) |
626 #endif
627 break;
628 }
629 }
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) {
633 av_log(h->avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
634 ret = AVERROR_INVALIDDATA;
635 goto fail;
636 }
637
638 qp_bd_offset = 6 * (sps->bit_depth_luma - 8);
639
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));
656
657 bits_left = bit_length - get_bits_count(&h->gb);
658 if (bits_left && (bits_left > 8 ||
659 show_bits(&h->gb, bits_left) != 1 << (bits_left - 1))) {
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);
665 } else {
666 pps->chroma_qp_index_offset[1] = pps->chroma_qp_index_offset[0];
667 }
668
669 build_qp_table(pps, 0, pps->chroma_qp_index_offset[0],
670 sps->bit_depth_luma);
671 build_qp_table(pps, 1, pps->chroma_qp_index_offset[1],
672 sps->bit_depth_luma);
673 if (pps->chroma_qp_index_offset[0] != pps->chroma_qp_index_offset[1])
674 pps->chroma_qp_diff = 1;
675
676 if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
677 av_log(h->avctx, AV_LOG_DEBUG,
678 "pps:%u sps:%u %s slice_groups:%d ref:%u/%u %s qp:%d/%d/%d/%d %s %s %s %s\n",
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" : "",
688 pps->transform_8x8_mode ? "8x8DCT" : "");
689 }
690
691 av_free(h->pps_buffers[pps_id]);
692 h->pps_buffers[pps_id] = pps;
693 return 0;
694
695 fail:
696 av_free(pps);
697 return ret;
698 }