FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_ps.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 419 533 78.6%
Branches: 243 356 68.3%

Line Branch Exec Source
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
30 #include "libavutil/imgutils.h"
31 #include "mathops.h"
32 #include "avcodec.h"
33 #include "h264data.h"
34 #include "h264_ps.h"
35 #include "golomb.h"
36
37 #define MIN_LOG2_MAX_FRAME_NUM 4
38
39 #define EXTENDED_SAR 255
40
41 static const uint8_t default_scaling4[2][16] = {
42 { 6, 13, 20, 28, 13, 20, 28, 32,
43 20, 28, 32, 37, 28, 32, 37, 42 },
44 { 10, 14, 20, 24, 14, 20, 24, 27,
45 20, 24, 27, 30, 24, 27, 30, 34 }
46 };
47
48 static const uint8_t default_scaling8[2][64] = {
49 { 6, 10, 13, 16, 18, 23, 25, 27,
50 10, 11, 16, 18, 23, 25, 27, 29,
51 13, 16, 18, 23, 25, 27, 29, 31,
52 16, 18, 23, 25, 27, 29, 31, 33,
53 18, 23, 25, 27, 29, 31, 33, 36,
54 23, 25, 27, 29, 31, 33, 36, 38,
55 25, 27, 29, 31, 33, 36, 38, 40,
56 27, 29, 31, 33, 36, 38, 40, 42 },
57 { 9, 13, 15, 17, 19, 21, 22, 24,
58 13, 13, 17, 19, 21, 22, 24, 25,
59 15, 17, 19, 21, 22, 24, 25, 27,
60 17, 19, 21, 22, 24, 25, 27, 28,
61 19, 21, 22, 24, 25, 27, 28, 30,
62 21, 22, 24, 25, 27, 28, 30, 32,
63 22, 24, 25, 27, 28, 30, 32, 33,
64 24, 25, 27, 28, 30, 32, 33, 35 }
65 };
66
67 /* maximum number of MBs in the DPB for a given level */
68 static const int level_max_dpb_mbs[][2] = {
69 { 10, 396 },
70 { 11, 900 },
71 { 12, 2376 },
72 { 13, 2376 },
73 { 20, 2376 },
74 { 21, 4752 },
75 { 22, 8100 },
76 { 30, 8100 },
77 { 31, 18000 },
78 { 32, 20480 },
79 { 40, 32768 },
80 { 41, 32768 },
81 { 42, 34816 },
82 { 50, 110400 },
83 { 51, 184320 },
84 { 52, 184320 },
85 };
86
87 14997 static void remove_pps(H264ParamSets *s, int id)
88 {
89 14997 av_buffer_unref(&s->pps_list[id]);
90 14997 }
91
92 1052 static void remove_sps(H264ParamSets *s, int id)
93 {
94 #if 0
95 int i;
96 if (s->sps_list[id]) {
97 /* drop all PPS that depend on this SPS */
98 for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++)
99 if (s->pps_list[i] && ((PPS*)s->pps_list[i]->data)->sps_id == id)
100 remove_pps(s, i);
101 }
102 #endif
103 1052 av_buffer_unref(&s->sps_list[id]);
104 1052 }
105
106 138 static inline int decode_hrd_parameters(GetBitContext *gb, void *logctx,
107 SPS *sps)
108 {
109 int cpb_count, i;
110 138 cpb_count = get_ue_golomb_31(gb) + 1;
111
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 138 times.
138 if (cpb_count > 32U) {
113 av_log(logctx, AV_LOG_ERROR, "cpb_count %d invalid\n", cpb_count);
114 return AVERROR_INVALIDDATA;
115 }
116
117 138 get_bits(gb, 4); /* bit_rate_scale */
118 138 get_bits(gb, 4); /* cpb_size_scale */
119
2/2
✓ Branch 0 taken 138 times.
✓ Branch 1 taken 138 times.
276 for (i = 0; i < cpb_count; i++) {
120 138 get_ue_golomb_long(gb); /* bit_rate_value_minus1 */
121 138 get_ue_golomb_long(gb); /* cpb_size_value_minus1 */
122 138 get_bits1(gb); /* cbr_flag */
123 }
124 138 sps->initial_cpb_removal_delay_length = get_bits(gb, 5) + 1;
125 138 sps->cpb_removal_delay_length = get_bits(gb, 5) + 1;
126 138 sps->dpb_output_delay_length = get_bits(gb, 5) + 1;
127 138 sps->time_offset_length = get_bits(gb, 5);
128 138 sps->cpb_cnt = cpb_count;
129 138 return 0;
130 }
131
132 815 static inline int decode_vui_parameters(GetBitContext *gb, void *logctx,
133 SPS *sps)
134 {
135 int aspect_ratio_info_present_flag;
136 unsigned int aspect_ratio_idc;
137
138 815 aspect_ratio_info_present_flag = get_bits1(gb);
139
140
2/2
✓ Branch 0 taken 417 times.
✓ Branch 1 taken 398 times.
815 if (aspect_ratio_info_present_flag) {
141 417 aspect_ratio_idc = get_bits(gb, 8);
142
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 377 times.
417 if (aspect_ratio_idc == EXTENDED_SAR) {
143 40 sps->sar.num = get_bits(gb, 16);
144 40 sps->sar.den = get_bits(gb, 16);
145
1/2
✓ Branch 0 taken 377 times.
✗ Branch 1 not taken.
377 } else if (aspect_ratio_idc < FF_ARRAY_ELEMS(ff_h264_pixel_aspect)) {
146 377 sps->sar = ff_h264_pixel_aspect[aspect_ratio_idc];
147 } else {
148 av_log(logctx, AV_LOG_ERROR, "illegal aspect ratio\n");
149 return AVERROR_INVALIDDATA;
150 }
151 } else {
152 398 sps->sar.num =
153 398 sps->sar.den = 0;
154 }
155
156
2/2
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 737 times.
815 if (get_bits1(gb)) /* overscan_info_present_flag */
157 78 get_bits1(gb); /* overscan_appropriate_flag */
158
159 815 sps->video_signal_type_present_flag = get_bits1(gb);
160
2/2
✓ Branch 0 taken 161 times.
✓ Branch 1 taken 654 times.
815 if (sps->video_signal_type_present_flag) {
161 161 get_bits(gb, 3); /* video_format */
162 161 sps->full_range = get_bits1(gb); /* video_full_range_flag */
163
164 161 sps->colour_description_present_flag = get_bits1(gb);
165
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 21 times.
161 if (sps->colour_description_present_flag) {
166 140 sps->color_primaries = get_bits(gb, 8); /* colour_primaries */
167 140 sps->color_trc = get_bits(gb, 8); /* transfer_characteristics */
168 140 sps->colorspace = get_bits(gb, 8); /* matrix_coefficients */
169
170 // Set invalid values to "unspecified"
171
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 140 times.
140 if (!av_color_primaries_name(sps->color_primaries))
172 sps->color_primaries = AVCOL_PRI_UNSPECIFIED;
173
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 140 times.
140 if (!av_color_transfer_name(sps->color_trc))
174 sps->color_trc = AVCOL_TRC_UNSPECIFIED;
175
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 140 times.
140 if (!av_color_space_name(sps->colorspace))
176 sps->colorspace = AVCOL_SPC_UNSPECIFIED;
177 }
178 }
179
180 /* chroma_location_info_present_flag */
181
2/2
✓ Branch 1 taken 106 times.
✓ Branch 2 taken 709 times.
815 if (get_bits1(gb)) {
182 /* chroma_sample_location_type_top_field */
183 106 sps->chroma_location = get_ue_golomb_31(gb) + 1;
184 106 get_ue_golomb_31(gb); /* chroma_sample_location_type_bottom_field */
185 } else
186 709 sps->chroma_location = AVCHROMA_LOC_LEFT;
187
188
3/4
✓ Branch 1 taken 739 times.
✓ Branch 2 taken 76 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 739 times.
815 if (show_bits1(gb) && get_bits_left(gb) < 10) {
189 av_log(logctx, AV_LOG_WARNING, "Truncated VUI (%d)\n", get_bits_left(gb));
190 return 0;
191 }
192
193 815 sps->timing_info_present_flag = get_bits1(gb);
194
2/2
✓ Branch 0 taken 739 times.
✓ Branch 1 taken 76 times.
815 if (sps->timing_info_present_flag) {
195 739 unsigned num_units_in_tick = get_bits_long(gb, 32);
196 739 unsigned time_scale = get_bits_long(gb, 32);
197
2/4
✓ Branch 0 taken 739 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 739 times.
739 if (!num_units_in_tick || !time_scale) {
198 av_log(logctx, AV_LOG_ERROR,
199 "time_scale/num_units_in_tick invalid or unsupported (%u/%u)\n",
200 time_scale, num_units_in_tick);
201 sps->timing_info_present_flag = 0;
202 } else {
203 739 sps->num_units_in_tick = num_units_in_tick;
204 739 sps->time_scale = time_scale;
205 }
206 739 sps->fixed_frame_rate_flag = get_bits1(gb);
207 }
208
209 815 sps->nal_hrd_parameters_present_flag = get_bits1(gb);
210
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 713 times.
815 if (sps->nal_hrd_parameters_present_flag)
211
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 102 times.
102 if (decode_hrd_parameters(gb, logctx, sps) < 0)
212 return AVERROR_INVALIDDATA;
213 815 sps->vcl_hrd_parameters_present_flag = get_bits1(gb);
214
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 779 times.
815 if (sps->vcl_hrd_parameters_present_flag)
215
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
36 if (decode_hrd_parameters(gb, logctx, sps) < 0)
216 return AVERROR_INVALIDDATA;
217
2/2
✓ Branch 0 taken 713 times.
✓ Branch 1 taken 102 times.
815 if (sps->nal_hrd_parameters_present_flag ||
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 713 times.
713 sps->vcl_hrd_parameters_present_flag)
219 102 get_bits1(gb); /* low_delay_hrd_flag */
220 815 sps->pic_struct_present_flag = get_bits1(gb);
221
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 815 times.
815 if (!get_bits_left(gb))
222 return 0;
223 815 sps->bitstream_restriction_flag = get_bits1(gb);
224
2/2
✓ Branch 0 taken 662 times.
✓ Branch 1 taken 153 times.
815 if (sps->bitstream_restriction_flag) {
225 662 get_bits1(gb); /* motion_vectors_over_pic_boundaries_flag */
226 662 get_ue_golomb_31(gb); /* max_bytes_per_pic_denom */
227 662 get_ue_golomb_31(gb); /* max_bits_per_mb_denom */
228 662 get_ue_golomb_31(gb); /* log2_max_mv_length_horizontal */
229 662 get_ue_golomb_31(gb); /* log2_max_mv_length_vertical */
230 662 sps->num_reorder_frames = get_ue_golomb_31(gb);
231 662 get_ue_golomb_31(gb); /*max_dec_frame_buffering*/
232
233
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 660 times.
662 if (get_bits_left(gb) < 0) {
234 2 sps->num_reorder_frames = 0;
235 2 sps->bitstream_restriction_flag = 0;
236 }
237
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 662 times.
662 if (sps->num_reorder_frames > 16U
239 /* max_dec_frame_buffering || max_dec_frame_buffering > 16 */) {
240 av_log(logctx, AV_LOG_ERROR,
241 "Clipping illegal num_reorder_frames %d\n",
242 sps->num_reorder_frames);
243 sps->num_reorder_frames = 16;
244 return AVERROR_INVALIDDATA;
245 }
246 }
247
248 815 return 0;
249 }
250
251 2320 static int decode_scaling_list(GetBitContext *gb, uint8_t *factors, int size,
252 const uint8_t *jvt_list,
253 const uint8_t *fallback_list)
254 {
255 2320 int i, last = 8, next = 8;
256
2/2
✓ Branch 0 taken 1740 times.
✓ Branch 1 taken 580 times.
2320 const uint8_t *scan = size == 16 ? ff_zigzag_scan : ff_zigzag_direct;
257
2/2
✓ Branch 1 taken 1140 times.
✓ Branch 2 taken 1180 times.
2320 if (!get_bits1(gb)) /* matrix not written, we use the predicted one */
258 1140 memcpy(factors, fallback_list, size * sizeof(uint8_t));
259 else
260
2/2
✓ Branch 0 taken 30664 times.
✓ Branch 1 taken 1076 times.
31740 for (i = 0; i < size; i++) {
261
2/2
✓ Branch 0 taken 28656 times.
✓ Branch 1 taken 2008 times.
30664 if (next) {
262 28656 int v = get_se_golomb(gb);
263
2/4
✓ Branch 0 taken 28656 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 28656 times.
28656 if (v < -128 || v > 127) {
264 av_log(NULL, AV_LOG_ERROR, "delta scale %d is invalid\n", v);
265 return AVERROR_INVALIDDATA;
266 }
267 28656 next = (last + v) & 0xff;
268 }
269
4/4
✓ Branch 0 taken 1180 times.
✓ Branch 1 taken 29484 times.
✓ Branch 2 taken 104 times.
✓ Branch 3 taken 1076 times.
30664 if (!i && !next) { /* matrix not written, we use the preset one */
270 104 memcpy(factors, jvt_list, size * sizeof(uint8_t));
271 104 break;
272 }
273
2/2
✓ Branch 0 taken 28406 times.
✓ Branch 1 taken 2154 times.
30560 last = factors[scan[i]] = next ? next : last;
274 }
275 2320 return 0;
276 }
277
278 /* returns non zero if the provided SPS scaling matrix has been filled */
279 1562 static int decode_scaling_matrices(GetBitContext *gb, const SPS *sps,
280 const PPS *pps, int is_sps,
281 uint8_t(*scaling_matrix4)[16],
282 uint8_t(*scaling_matrix8)[64])
283 {
284
4/4
✓ Branch 0 taken 718 times.
✓ Branch 1 taken 844 times.
✓ Branch 2 taken 260 times.
✓ Branch 3 taken 458 times.
1562 int fallback_sps = !is_sps && sps->scaling_matrix_present;
285 6248 const uint8_t *fallback[4] = {
286
2/2
✓ Branch 0 taken 260 times.
✓ Branch 1 taken 1302 times.
1562 fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
287
2/2
✓ Branch 0 taken 260 times.
✓ Branch 1 taken 1302 times.
1562 fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
288
2/2
✓ Branch 0 taken 260 times.
✓ Branch 1 taken 1302 times.
1562 fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
289
2/2
✓ Branch 0 taken 260 times.
✓ Branch 1 taken 1302 times.
1562 fallback_sps ? sps->scaling_matrix8[3] : default_scaling8[1]
290 };
291 1562 int ret = 0;
292
2/2
✓ Branch 1 taken 290 times.
✓ Branch 2 taken 1272 times.
1562 if (get_bits1(gb)) {
293 290 ret |= decode_scaling_list(gb, scaling_matrix4[0], 16, default_scaling4[0], fallback[0]); // Intra, Y
294 290 ret |= decode_scaling_list(gb, scaling_matrix4[1], 16, default_scaling4[0], scaling_matrix4[0]); // Intra, Cr
295 290 ret |= decode_scaling_list(gb, scaling_matrix4[2], 16, default_scaling4[0], scaling_matrix4[1]); // Intra, Cb
296 290 ret |= decode_scaling_list(gb, scaling_matrix4[3], 16, default_scaling4[1], fallback[1]); // Inter, Y
297 290 ret |= decode_scaling_list(gb, scaling_matrix4[4], 16, default_scaling4[1], scaling_matrix4[3]); // Inter, Cr
298 290 ret |= decode_scaling_list(gb, scaling_matrix4[5], 16, default_scaling4[1], scaling_matrix4[4]); // Inter, Cb
299
3/4
✓ Branch 0 taken 145 times.
✓ Branch 1 taken 145 times.
✓ Branch 2 taken 145 times.
✗ Branch 3 not taken.
290 if (is_sps || pps->transform_8x8_mode) {
300 290 ret |= decode_scaling_list(gb, scaling_matrix8[0], 64, default_scaling8[0], fallback[2]); // Intra, Y
301 290 ret |= decode_scaling_list(gb, scaling_matrix8[3], 64, default_scaling8[1], fallback[3]); // Inter, Y
302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 290 times.
290 if (sps->chroma_format_idc == 3) {
303 ret |= decode_scaling_list(gb, scaling_matrix8[1], 64, default_scaling8[0], scaling_matrix8[0]); // Intra, Cr
304 ret |= decode_scaling_list(gb, scaling_matrix8[4], 64, default_scaling8[1], scaling_matrix8[3]); // Inter, Cr
305 ret |= decode_scaling_list(gb, scaling_matrix8[2], 64, default_scaling8[0], scaling_matrix8[1]); // Intra, Cb
306 ret |= decode_scaling_list(gb, scaling_matrix8[5], 64, default_scaling8[1], scaling_matrix8[4]); // Inter, Cb
307 }
308 }
309
1/2
✓ Branch 0 taken 290 times.
✗ Branch 1 not taken.
290 if (!ret)
310 290 ret = is_sps;
311 }
312
313 1562 return ret;
314 }
315
316 1002 void ff_h264_ps_uninit(H264ParamSets *ps)
317 {
318 int i;
319
320
2/2
✓ Branch 0 taken 32064 times.
✓ Branch 1 taken 1002 times.
33066 for (i = 0; i < MAX_SPS_COUNT; i++)
321 32064 av_buffer_unref(&ps->sps_list[i]);
322
323
2/2
✓ Branch 0 taken 256512 times.
✓ Branch 1 taken 1002 times.
257514 for (i = 0; i < MAX_PPS_COUNT; i++)
324 256512 av_buffer_unref(&ps->pps_list[i]);
325
326 1002 av_buffer_unref(&ps->pps_ref);
327
328 1002 ps->pps = NULL;
329 1002 ps->sps = NULL;
330 1002 }
331
332 2441 int ff_h264_decode_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx,
333 H264ParamSets *ps, int ignore_truncation)
334 {
335 AVBufferRef *sps_buf;
336 2441 int profile_idc, level_idc, constraint_set_flags = 0;
337 unsigned int sps_id;
338 int i, log2_max_frame_num_minus4;
339 SPS *sps;
340 int ret;
341
342 2441 sps_buf = av_buffer_allocz(sizeof(*sps));
343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2441 times.
2441 if (!sps_buf)
344 return AVERROR(ENOMEM);
345 2441 sps = (SPS*)sps_buf->data;
346
347 2441 sps->data_size = gb->buffer_end - gb->buffer;
348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2441 times.
2441 if (sps->data_size > sizeof(sps->data)) {
349 av_log(avctx, AV_LOG_DEBUG, "Truncating likely oversized SPS\n");
350 sps->data_size = sizeof(sps->data);
351 }
352 2441 memcpy(sps->data, gb->buffer, sps->data_size);
353
354 // Re-add the removed stop bit (may be used by hwaccels).
355
3/4
✓ Branch 0 taken 981 times.
✓ Branch 1 taken 1460 times.
✓ Branch 2 taken 981 times.
✗ Branch 3 not taken.
2441 if (!(gb->size_in_bits & 7) && sps->data_size < sizeof(sps->data))
356 981 sps->data[sps->data_size++] = 0x80;
357
358 2441 profile_idc = get_bits(gb, 8);
359 2441 constraint_set_flags |= get_bits1(gb) << 0; // constraint_set0_flag
360 2441 constraint_set_flags |= get_bits1(gb) << 1; // constraint_set1_flag
361 2441 constraint_set_flags |= get_bits1(gb) << 2; // constraint_set2_flag
362 2441 constraint_set_flags |= get_bits1(gb) << 3; // constraint_set3_flag
363 2441 constraint_set_flags |= get_bits1(gb) << 4; // constraint_set4_flag
364 2441 constraint_set_flags |= get_bits1(gb) << 5; // constraint_set5_flag
365 2441 skip_bits(gb, 2); // reserved_zero_2bits
366 2441 level_idc = get_bits(gb, 8);
367 2441 sps_id = get_ue_golomb_31(gb);
368
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2441 times.
2441 if (sps_id >= MAX_SPS_COUNT) {
370 av_log(avctx, AV_LOG_ERROR, "sps_id %u out of range\n", sps_id);
371 goto fail;
372 }
373
374 2441 sps->sps_id = sps_id;
375 2441 sps->time_offset_length = 24;
376 2441 sps->profile_idc = profile_idc;
377 2441 sps->constraint_set_flags = constraint_set_flags;
378 2441 sps->level_idc = level_idc;
379 2441 sps->full_range = -1;
380
381 2441 memset(sps->scaling_matrix4, 16, sizeof(sps->scaling_matrix4));
382 2441 memset(sps->scaling_matrix8, 16, sizeof(sps->scaling_matrix8));
383 2441 sps->scaling_matrix_present = 0;
384 2441 sps->colorspace = 2; //AVCOL_SPC_UNSPECIFIED
385
386
2/2
✓ Branch 0 taken 1833 times.
✓ Branch 1 taken 608 times.
2441 if (sps->profile_idc == 100 || // High profile
387
2/2
✓ Branch 0 taken 1785 times.
✓ Branch 1 taken 48 times.
1833 sps->profile_idc == 110 || // High10 profile
388
2/2
✓ Branch 0 taken 1727 times.
✓ Branch 1 taken 58 times.
1785 sps->profile_idc == 122 || // High422 profile
389
2/2
✓ Branch 0 taken 1597 times.
✓ Branch 1 taken 130 times.
1727 sps->profile_idc == 244 || // High444 Predictive profile
390
1/2
✓ Branch 0 taken 1597 times.
✗ Branch 1 not taken.
1597 sps->profile_idc == 44 || // Cavlc444 profile
391
1/2
✓ Branch 0 taken 1597 times.
✗ Branch 1 not taken.
1597 sps->profile_idc == 83 || // Scalable Constrained High profile (SVC)
392
1/2
✓ Branch 0 taken 1597 times.
✗ Branch 1 not taken.
1597 sps->profile_idc == 86 || // Scalable High Intra profile (SVC)
393
1/2
✓ Branch 0 taken 1597 times.
✗ Branch 1 not taken.
1597 sps->profile_idc == 118 || // Stereo High profile (MVC)
394
1/2
✓ Branch 0 taken 1597 times.
✗ Branch 1 not taken.
1597 sps->profile_idc == 128 || // Multiview High profile (MVC)
395
1/2
✓ Branch 0 taken 1597 times.
✗ Branch 1 not taken.
1597 sps->profile_idc == 138 || // Multiview Depth High profile (MVCD)
396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1597 times.
1597 sps->profile_idc == 144) { // old High444 profile
397 844 sps->chroma_format_idc = get_ue_golomb_31(gb);
398
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 844 times.
844 if (sps->chroma_format_idc > 3U) {
399 avpriv_request_sample(avctx, "chroma_format_idc %u",
400 sps->chroma_format_idc);
401 goto fail;
402
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 718 times.
844 } else if (sps->chroma_format_idc == 3) {
403 126 sps->residual_color_transform_flag = get_bits1(gb);
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
126 if (sps->residual_color_transform_flag) {
405 av_log(avctx, AV_LOG_ERROR, "separate color planes are not supported\n");
406 goto fail;
407 }
408 }
409 844 sps->bit_depth_luma = get_ue_golomb_31(gb) + 8;
410 844 sps->bit_depth_chroma = get_ue_golomb_31(gb) + 8;
411
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 844 times.
844 if (sps->bit_depth_chroma != sps->bit_depth_luma) {
412 avpriv_request_sample(avctx,
413 "Different chroma and luma bit depth");
414 goto fail;
415 }
416
2/4
✓ Branch 0 taken 844 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 844 times.
✗ Branch 3 not taken.
844 if (sps->bit_depth_luma < 8 || sps->bit_depth_luma > 14 ||
417
2/4
✓ Branch 0 taken 844 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 844 times.
844 sps->bit_depth_chroma < 8 || sps->bit_depth_chroma > 14) {
418 av_log(avctx, AV_LOG_ERROR, "illegal bit depth value (%d, %d)\n",
419 sps->bit_depth_luma, sps->bit_depth_chroma);
420 goto fail;
421 }
422 844 sps->transform_bypass = get_bits1(gb);
423 844 ret = decode_scaling_matrices(gb, sps, NULL, 1,
424 844 sps->scaling_matrix4, sps->scaling_matrix8);
425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 844 times.
844 if (ret < 0)
426 goto fail;
427 844 sps->scaling_matrix_present |= ret;
428 } else {
429 1597 sps->chroma_format_idc = 1;
430 1597 sps->bit_depth_luma = 8;
431 1597 sps->bit_depth_chroma = 8;
432 }
433
434 2441 log2_max_frame_num_minus4 = get_ue_golomb_31(gb);
435
2/4
✓ Branch 0 taken 2441 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2441 times.
2441 if (log2_max_frame_num_minus4 < MIN_LOG2_MAX_FRAME_NUM - 4 ||
436 log2_max_frame_num_minus4 > MAX_LOG2_MAX_FRAME_NUM - 4) {
437 av_log(avctx, AV_LOG_ERROR,
438 "log2_max_frame_num_minus4 out of range (0-12): %d\n",
439 log2_max_frame_num_minus4);
440 goto fail;
441 }
442 2441 sps->log2_max_frame_num = log2_max_frame_num_minus4 + 4;
443
444 2441 sps->poc_type = get_ue_golomb_31(gb);
445
446
2/2
✓ Branch 0 taken 1233 times.
✓ Branch 1 taken 1208 times.
2441 if (sps->poc_type == 0) { // FIXME #define
447 1233 unsigned t = get_ue_golomb_31(gb);
448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1233 times.
1233 if (t>12) {
449 av_log(avctx, AV_LOG_ERROR, "log2_max_poc_lsb (%d) is out of range\n", t);
450 goto fail;
451 }
452 1233 sps->log2_max_poc_lsb = t + 4;
453
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 1132 times.
1208 } else if (sps->poc_type == 1) { // FIXME #define
454 76 sps->delta_pic_order_always_zero_flag = get_bits1(gb);
455 76 sps->offset_for_non_ref_pic = get_se_golomb_long(gb);
456 76 sps->offset_for_top_to_bottom_field = get_se_golomb_long(gb);
457
458
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 if ( sps->offset_for_non_ref_pic == INT32_MIN
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 || sps->offset_for_top_to_bottom_field == INT32_MIN
460 ) {
461 av_log(avctx, AV_LOG_ERROR,
462 "offset_for_non_ref_pic or offset_for_top_to_bottom_field is out of range\n");
463 goto fail;
464 }
465
466 76 sps->poc_cycle_length = get_ue_golomb(gb);
467
468
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if ((unsigned)sps->poc_cycle_length >=
469 FF_ARRAY_ELEMS(sps->offset_for_ref_frame)) {
470 av_log(avctx, AV_LOG_ERROR,
471 "poc_cycle_length overflow %d\n", sps->poc_cycle_length);
472 goto fail;
473 }
474
475
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 76 times.
176 for (i = 0; i < sps->poc_cycle_length; i++) {
476 100 sps->offset_for_ref_frame[i] = get_se_golomb_long(gb);
477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 100 times.
100 if (sps->offset_for_ref_frame[i] == INT32_MIN) {
478 av_log(avctx, AV_LOG_ERROR,
479 "offset_for_ref_frame is out of range\n");
480 goto fail;
481 }
482 }
483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1132 times.
1132 } else if (sps->poc_type != 2) {
484 av_log(avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
485 goto fail;
486 }
487
488 2441 sps->ref_frame_count = get_ue_golomb_31(gb);
489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2441 times.
2441 if (avctx->codec_tag == MKTAG('S', 'M', 'V', '2'))
490 sps->ref_frame_count = FFMAX(2, sps->ref_frame_count);
491
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2441 times.
2441 if (sps->ref_frame_count > H264_MAX_DPB_FRAMES) {
492 av_log(avctx, AV_LOG_ERROR,
493 "too many reference frames %d\n", sps->ref_frame_count);
494 goto fail;
495 }
496 2441 sps->gaps_in_frame_num_allowed_flag = get_bits1(gb);
497 2441 sps->mb_width = get_ue_golomb(gb) + 1;
498 2441 sps->mb_height = get_ue_golomb(gb) + 1;
499
500 2441 sps->frame_mbs_only_flag = get_bits1(gb);
501
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2441 times.
2441 if (sps->mb_height >= INT_MAX / 2U) {
503 av_log(avctx, AV_LOG_ERROR, "height overflow\n");
504 goto fail;
505 }
506 2441 sps->mb_height *= 2 - sps->frame_mbs_only_flag;
507
508
2/2
✓ Branch 0 taken 521 times.
✓ Branch 1 taken 1920 times.
2441 if (!sps->frame_mbs_only_flag)
509 521 sps->mb_aff = get_bits1(gb);
510 else
511 1920 sps->mb_aff = 0;
512
513
1/2
✓ Branch 0 taken 2441 times.
✗ Branch 1 not taken.
2441 if ((unsigned)sps->mb_width >= INT_MAX / 16 ||
514
2/4
✓ Branch 0 taken 2441 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2441 times.
4882 (unsigned)sps->mb_height >= INT_MAX / 16 ||
515 2441 av_image_check_size(16 * sps->mb_width,
516 2441 16 * sps->mb_height, 0, avctx)) {
517 av_log(avctx, AV_LOG_ERROR, "mb_width/height overflow\n");
518 goto fail;
519 }
520
521 2441 sps->direct_8x8_inference_flag = get_bits1(gb);
522
523 2441 sps->crop = get_bits1(gb);
524
2/2
✓ Branch 0 taken 342 times.
✓ Branch 1 taken 2099 times.
2441 if (sps->crop) {
525 342 unsigned int crop_left = get_ue_golomb(gb);
526 342 unsigned int crop_right = get_ue_golomb(gb);
527 342 unsigned int crop_top = get_ue_golomb(gb);
528 342 unsigned int crop_bottom = get_ue_golomb(gb);
529 342 int width = 16 * sps->mb_width;
530 342 int height = 16 * sps->mb_height;
531
532
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 342 times.
342 if (avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {
533 av_log(avctx, AV_LOG_DEBUG, "discarding sps cropping, original "
534 "values are l:%d r:%d t:%d b:%d\n",
535 crop_left, crop_right, crop_top, crop_bottom);
536
537 sps->crop_left =
538 sps->crop_right =
539 sps->crop_top =
540 sps->crop_bottom = 0;
541 } else {
542 342 int vsub = (sps->chroma_format_idc == 1) ? 1 : 0;
543 684 int hsub = (sps->chroma_format_idc == 1 ||
544
4/4
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 276 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 36 times.
342 sps->chroma_format_idc == 2) ? 1 : 0;
545 342 int step_x = 1 << hsub;
546 342 int step_y = (2 - sps->frame_mbs_only_flag) << vsub;
547
548
1/2
✓ Branch 0 taken 342 times.
✗ Branch 1 not taken.
342 if (crop_left > (unsigned)INT_MAX / 4 / step_x ||
549
1/2
✓ Branch 0 taken 342 times.
✗ Branch 1 not taken.
342 crop_right > (unsigned)INT_MAX / 4 / step_x ||
550
1/2
✓ Branch 0 taken 342 times.
✗ Branch 1 not taken.
342 crop_top > (unsigned)INT_MAX / 4 / step_y ||
551
1/2
✓ Branch 0 taken 342 times.
✗ Branch 1 not taken.
342 crop_bottom> (unsigned)INT_MAX / 4 / step_y ||
552
1/2
✓ Branch 0 taken 342 times.
✗ Branch 1 not taken.
342 (crop_left + crop_right ) * step_x >= width ||
553
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 342 times.
342 (crop_top + crop_bottom) * step_y >= height
554 ) {
555 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);
556 goto fail;
557 }
558
559 342 sps->crop_left = crop_left * step_x;
560 342 sps->crop_right = crop_right * step_x;
561 342 sps->crop_top = crop_top * step_y;
562 342 sps->crop_bottom = crop_bottom * step_y;
563 }
564 } else {
565 2099 sps->crop_left =
566 2099 sps->crop_right =
567 2099 sps->crop_top =
568 2099 sps->crop_bottom =
569 2099 sps->crop = 0;
570 }
571
572 2441 sps->vui_parameters_present_flag = get_bits1(gb);
573
2/2
✓ Branch 0 taken 815 times.
✓ Branch 1 taken 1626 times.
2441 if (sps->vui_parameters_present_flag) {
574 815 int ret = decode_vui_parameters(gb, avctx, sps);
575
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 815 times.
815 if (ret < 0)
576 goto fail;
577 }
578
579
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 2436 times.
2441 if (get_bits_left(gb) < 0) {
580 10 av_log_once(avctx, ignore_truncation ? AV_LOG_WARNING : AV_LOG_ERROR, AV_LOG_DEBUG,
581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 &ps->overread_warning_printed[sps->vui_parameters_present_flag],
582
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 "Overread %s by %d bits\n", sps->vui_parameters_present_flag ? "VUI" : "SPS", -get_bits_left(gb));
583
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (!ignore_truncation)
584 5 goto fail;
585 }
586
587 /* if the maximum delay is not stored in the SPS, derive it based on the
588 * level */
589
2/2
✓ Branch 0 taken 1776 times.
✓ Branch 1 taken 660 times.
2436 if (!sps->bitstream_restriction_flag &&
590
3/4
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 1717 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 59 times.
1776 (sps->ref_frame_count || avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT)) {
591 1717 sps->num_reorder_frames = H264_MAX_DPB_FRAMES - 1;
592
1/2
✓ Branch 0 taken 9444 times.
✗ Branch 1 not taken.
9444 for (i = 0; i < FF_ARRAY_ELEMS(level_max_dpb_mbs); i++) {
593
2/2
✓ Branch 0 taken 1717 times.
✓ Branch 1 taken 7727 times.
9444 if (level_max_dpb_mbs[i][0] == sps->level_idc) {
594 1717 sps->num_reorder_frames = FFMIN(level_max_dpb_mbs[i][1] / (sps->mb_width * sps->mb_height),
595 sps->num_reorder_frames);
596 1717 break;
597 }
598 }
599 }
600
601
2/2
✓ Branch 0 taken 2021 times.
✓ Branch 1 taken 415 times.
2436 if (!sps->sar.den)
602 2021 sps->sar.den = 1;
603
604
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2436 times.
2436 if (avctx->debug & FF_DEBUG_PICT_INFO) {
605 static const char csp[4][5] = { "Gray", "420", "422", "444" };
606 av_log(avctx, AV_LOG_DEBUG,
607 "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",
608 sps_id, sps->profile_idc, sps->level_idc,
609 sps->poc_type,
610 sps->ref_frame_count,
611 sps->mb_width, sps->mb_height,
612 sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
613 sps->direct_8x8_inference_flag ? "8B8" : "",
614 sps->crop_left, sps->crop_right,
615 sps->crop_top, sps->crop_bottom,
616 sps->vui_parameters_present_flag ? "VUI" : "",
617 csp[sps->chroma_format_idc],
618 sps->timing_info_present_flag ? sps->num_units_in_tick : 0,
619 sps->timing_info_present_flag ? sps->time_scale : 0,
620 sps->bit_depth_luma,
621 sps->bitstream_restriction_flag ? sps->num_reorder_frames : -1
622 );
623 }
624
625 /* check if this is a repeat of an already parsed SPS, then keep the
626 * original one.
627 * otherwise drop all PPSes that depend on it */
628
2/2
✓ Branch 0 taken 1453 times.
✓ Branch 1 taken 983 times.
2436 if (ps->sps_list[sps_id] &&
629
2/2
✓ Branch 0 taken 1384 times.
✓ Branch 1 taken 69 times.
1453 !memcmp(ps->sps_list[sps_id]->data, sps_buf->data, sps_buf->size)) {
630 1384 av_buffer_unref(&sps_buf);
631 } else {
632 1052 remove_sps(ps, sps_id);
633 1052 ps->sps_list[sps_id] = sps_buf;
634 }
635
636 2436 return 0;
637
638 5 fail:
639 5 av_buffer_unref(&sps_buf);
640 5 return AVERROR_INVALIDDATA;
641 }
642
643 705 static void init_dequant8_coeff_table(PPS *pps, const SPS *sps)
644 {
645 int i, j, q, x;
646 705 const int max_qp = 51 + 6 * (sps->bit_depth_luma - 8);
647
648
2/2
✓ Branch 0 taken 4230 times.
✓ Branch 1 taken 705 times.
4935 for (i = 0; i < 6; i++) {
649 4230 pps->dequant8_coeff[i] = pps->dequant8_buffer[i];
650
2/2
✓ Branch 0 taken 4862 times.
✓ Branch 1 taken 1236 times.
6098 for (j = 0; j < i; j++)
651
2/2
✓ Branch 0 taken 2994 times.
✓ Branch 1 taken 1868 times.
4862 if (!memcmp(pps->scaling_matrix8[j], pps->scaling_matrix8[i],
652 64 * sizeof(uint8_t))) {
653 2994 pps->dequant8_coeff[i] = pps->dequant8_buffer[j];
654 2994 break;
655 }
656
2/2
✓ Branch 0 taken 2994 times.
✓ Branch 1 taken 1236 times.
4230 if (j < i)
657 2994 continue;
658
659
2/2
✓ Branch 0 taken 65598 times.
✓ Branch 1 taken 1236 times.
66834 for (q = 0; q < max_qp + 1; q++) {
660 65598 int shift = ff_h264_quant_div6[q];
661 65598 int idx = ff_h264_quant_rem6[q];
662
2/2
✓ Branch 0 taken 4198272 times.
✓ Branch 1 taken 65598 times.
4263870 for (x = 0; x < 64; x++)
663 4198272 pps->dequant8_coeff[i][q][(x >> 3) | ((x & 7) << 3)] =
664 4198272 ((uint32_t)ff_h264_dequant8_coeff_init[idx][ff_h264_dequant8_coeff_init_scan[((x >> 1) & 12) | (x & 3)]] *
665 4198272 pps->scaling_matrix8[i][x]) << shift;
666 }
667 }
668 705 }
669
670 14997 static void init_dequant4_coeff_table(PPS *pps, const SPS *sps)
671 {
672 int i, j, q, x;
673 14997 const int max_qp = 51 + 6 * (sps->bit_depth_luma - 8);
674
2/2
✓ Branch 0 taken 89982 times.
✓ Branch 1 taken 14997 times.
104979 for (i = 0; i < 6; i++) {
675 89982 pps->dequant4_coeff[i] = pps->dequant4_buffer[i];
676
2/2
✓ Branch 0 taken 78351 times.
✓ Branch 1 taken 15817 times.
94168 for (j = 0; j < i; j++)
677
2/2
✓ Branch 0 taken 74165 times.
✓ Branch 1 taken 4186 times.
78351 if (!memcmp(pps->scaling_matrix4[j], pps->scaling_matrix4[i],
678 16 * sizeof(uint8_t))) {
679 74165 pps->dequant4_coeff[i] = pps->dequant4_buffer[j];
680 74165 break;
681 }
682
2/2
✓ Branch 0 taken 74165 times.
✓ Branch 1 taken 15817 times.
89982 if (j < i)
683 74165 continue;
684
685
2/2
✓ Branch 0 taken 824176 times.
✓ Branch 1 taken 15817 times.
839993 for (q = 0; q < max_qp + 1; q++) {
686 824176 int shift = ff_h264_quant_div6[q] + 2;
687 824176 int idx = ff_h264_quant_rem6[q];
688
2/2
✓ Branch 0 taken 13186816 times.
✓ Branch 1 taken 824176 times.
14010992 for (x = 0; x < 16; x++)
689 13186816 pps->dequant4_coeff[i][q][(x >> 2) | ((x << 2) & 0xF)] =
690 13186816 ((uint32_t)ff_h264_dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] *
691 13186816 pps->scaling_matrix4[i][x]) << shift;
692 }
693 }
694 14997 }
695
696 14997 static void init_dequant_tables(PPS *pps, const SPS *sps)
697 {
698 int i, x;
699 14997 init_dequant4_coeff_table(pps, sps);
700 14997 memset(pps->dequant8_coeff, 0, sizeof(pps->dequant8_coeff));
701
702
2/2
✓ Branch 0 taken 705 times.
✓ Branch 1 taken 14292 times.
14997 if (pps->transform_8x8_mode)
703 705 init_dequant8_coeff_table(pps, sps);
704
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 14975 times.
14997 if (sps->transform_bypass) {
705
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 22 times.
154 for (i = 0; i < 6; i++)
706
2/2
✓ Branch 0 taken 2112 times.
✓ Branch 1 taken 132 times.
2244 for (x = 0; x < 16; x++)
707 2112 pps->dequant4_coeff[i][0][x] = 1 << 6;
708
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 1 times.
22 if (pps->transform_8x8_mode)
709
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 21 times.
147 for (i = 0; i < 6; i++)
710
2/2
✓ Branch 0 taken 8064 times.
✓ Branch 1 taken 126 times.
8190 for (x = 0; x < 64; x++)
711 8064 pps->dequant8_coeff[i][0][x] = 1 << 6;
712 }
713 14997 }
714
715 29994 static void build_qp_table(PPS *pps, int t, int index, const int depth)
716 {
717 int i;
718 29994 const int max_qp = 51 + 6 * (depth - 8);
719
2/2
✓ Branch 0 taken 1562664 times.
✓ Branch 1 taken 29994 times.
1592658 for (i = 0; i < max_qp + 1; i++)
720 1562664 pps->chroma_qp_table[t][i] =
721 1562664 ff_h264_chroma_qp[depth - 8][av_clip(i + index, 0, max_qp)];
722 29994 }
723
724 718 static int more_rbsp_data_in_pps(const SPS *sps, void *logctx)
725 {
726 718 int profile_idc = sps->profile_idc;
727
728
5/6
✓ Branch 0 taken 708 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 705 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 705 times.
718 if ((profile_idc == 66 || profile_idc == 77 ||
729
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 profile_idc == 88) && (sps->constraint_set_flags & 7)) {
730 av_log(logctx, AV_LOG_VERBOSE,
731 "Current profile doesn't provide more RBSP data in PPS, skipping\n");
732 return 0;
733 }
734
735 718 return 1;
736 }
737
738 15011 static void pps_free(void *opaque, uint8_t *data)
739 {
740 15011 PPS *pps = (PPS*)data;
741
742 15011 av_buffer_unref(&pps->sps_ref);
743
744 15011 av_freep(&data);
745 15011 }
746
747 15011 int ff_h264_decode_picture_parameter_set(GetBitContext *gb, AVCodecContext *avctx,
748 H264ParamSets *ps, int bit_length)
749 {
750 AVBufferRef *pps_buf;
751 const SPS *sps;
752 15011 unsigned int pps_id = get_ue_golomb(gb);
753 PPS *pps;
754 int qp_bd_offset;
755 int bits_left;
756 int ret;
757
758
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15011 times.
15011 if (pps_id >= MAX_PPS_COUNT) {
759 av_log(avctx, AV_LOG_ERROR, "pps_id %u out of range\n", pps_id);
760 return AVERROR_INVALIDDATA;
761 }
762
763 15011 pps = av_mallocz(sizeof(*pps));
764
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15011 times.
15011 if (!pps)
765 return AVERROR(ENOMEM);
766 15011 pps_buf = av_buffer_create((uint8_t*)pps, sizeof(*pps),
767 pps_free, NULL, 0);
768
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15011 times.
15011 if (!pps_buf) {
769 av_freep(&pps);
770 return AVERROR(ENOMEM);
771 }
772
773 15011 pps->data_size = gb->buffer_end - gb->buffer;
774
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15011 times.
15011 if (pps->data_size > sizeof(pps->data)) {
775 av_log(avctx, AV_LOG_DEBUG, "Truncating likely oversized PPS "
776 "(%"SIZE_SPECIFIER" > %"SIZE_SPECIFIER")\n",
777 pps->data_size, sizeof(pps->data));
778 pps->data_size = sizeof(pps->data);
779 }
780 15011 memcpy(pps->data, gb->buffer, pps->data_size);
781
782 // Re-add the removed stop bit (may be used by hwaccels).
783
3/4
✓ Branch 0 taken 10359 times.
✓ Branch 1 taken 4652 times.
✓ Branch 2 taken 10359 times.
✗ Branch 3 not taken.
15011 if (!(bit_length & 7) && pps->data_size < sizeof(pps->data))
784 10359 pps->data[pps->data_size++] = 0x80;
785
786 15011 pps->sps_id = get_ue_golomb_31(gb);
787
1/2
✓ Branch 0 taken 15011 times.
✗ Branch 1 not taken.
15011 if ((unsigned)pps->sps_id >= MAX_SPS_COUNT ||
788
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15011 times.
15011 !ps->sps_list[pps->sps_id]) {
789 av_log(avctx, AV_LOG_ERROR, "sps_id %u out of range\n", pps->sps_id);
790 ret = AVERROR_INVALIDDATA;
791 goto fail;
792 }
793 15011 pps->sps_ref = av_buffer_ref(ps->sps_list[pps->sps_id]);
794
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15011 times.
15011 if (!pps->sps_ref) {
795 ret = AVERROR(ENOMEM);
796 goto fail;
797 }
798 15011 pps->sps = (const SPS*)pps->sps_ref->data;
799 15011 sps = pps->sps;
800
801
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15011 times.
15011 if (sps->bit_depth_luma > 14) {
802 av_log(avctx, AV_LOG_ERROR,
803 "Invalid luma bit depth=%d\n",
804 sps->bit_depth_luma);
805 ret = AVERROR_INVALIDDATA;
806 goto fail;
807
2/4
✓ Branch 0 taken 15011 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15011 times.
15011 } else if (sps->bit_depth_luma == 11 || sps->bit_depth_luma == 13) {
808 avpriv_report_missing_feature(avctx,
809 "Unimplemented luma bit depth=%d",
810 sps->bit_depth_luma);
811 ret = AVERROR_PATCHWELCOME;
812 goto fail;
813 }
814
815 15011 pps->cabac = get_bits1(gb);
816 15011 pps->pic_order_present = get_bits1(gb);
817 15011 pps->slice_group_count = get_ue_golomb(gb) + 1;
818
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14997 times.
15011 if (pps->slice_group_count > 1) {
819 14 pps->mb_slice_group_map_type = get_ue_golomb(gb);
820 14 avpriv_report_missing_feature(avctx, "FMO");
821 14 ret = AVERROR_PATCHWELCOME;
822 14 goto fail;
823 }
824 14997 pps->ref_count[0] = get_ue_golomb(gb) + 1;
825 14997 pps->ref_count[1] = get_ue_golomb(gb) + 1;
826
2/4
✓ Branch 0 taken 14997 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14997 times.
14997 if (pps->ref_count[0] - 1 > 32 - 1 || pps->ref_count[1] - 1 > 32 - 1) {
827 av_log(avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
828 ret = AVERROR_INVALIDDATA;
829 goto fail;
830 }
831
832 14997 qp_bd_offset = 6 * (sps->bit_depth_luma - 8);
833
834 14997 pps->weighted_pred = get_bits1(gb);
835 14997 pps->weighted_bipred_idc = get_bits(gb, 2);
836 14997 pps->init_qp = get_se_golomb(gb) + 26U + qp_bd_offset;
837 14997 pps->init_qs = get_se_golomb(gb) + 26U + qp_bd_offset;
838 14997 pps->chroma_qp_index_offset[0] = get_se_golomb(gb);
839
2/4
✓ Branch 0 taken 14997 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14997 times.
14997 if (pps->chroma_qp_index_offset[0] < -12 || pps->chroma_qp_index_offset[0] > 12) {
840 ret = AVERROR_INVALIDDATA;
841 goto fail;
842 }
843
844 14997 pps->deblocking_filter_parameters_present = get_bits1(gb);
845 14997 pps->constrained_intra_pred = get_bits1(gb);
846 14997 pps->redundant_pic_cnt_present = get_bits1(gb);
847
848 14997 pps->transform_8x8_mode = 0;
849 14997 memcpy(pps->scaling_matrix4, sps->scaling_matrix4,
850 sizeof(pps->scaling_matrix4));
851 14997 memcpy(pps->scaling_matrix8, sps->scaling_matrix8,
852 sizeof(pps->scaling_matrix8));
853
854 14997 bits_left = bit_length - get_bits_count(gb);
855
3/4
✓ Branch 0 taken 718 times.
✓ Branch 1 taken 14279 times.
✓ Branch 3 taken 718 times.
✗ Branch 4 not taken.
14997 if (bits_left > 0 && more_rbsp_data_in_pps(sps, avctx)) {
856 718 pps->transform_8x8_mode = get_bits1(gb);
857 718 ret = decode_scaling_matrices(gb, sps, pps, 0,
858 718 pps->scaling_matrix4, pps->scaling_matrix8);
859
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 718 times.
718 if (ret < 0)
860 goto fail;
861 // second_chroma_qp_index_offset
862 718 pps->chroma_qp_index_offset[1] = get_se_golomb(gb);
863
2/4
✓ Branch 0 taken 718 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 718 times.
718 if (pps->chroma_qp_index_offset[1] < -12 || pps->chroma_qp_index_offset[1] > 12) {
864 ret = AVERROR_INVALIDDATA;
865 goto fail;
866 }
867 } else {
868 14279 pps->chroma_qp_index_offset[1] = pps->chroma_qp_index_offset[0];
869 }
870
871 14997 build_qp_table(pps, 0, pps->chroma_qp_index_offset[0],
872 14997 sps->bit_depth_luma);
873 14997 build_qp_table(pps, 1, pps->chroma_qp_index_offset[1],
874 14997 sps->bit_depth_luma);
875
876 14997 init_dequant_tables(pps, sps);
877
878
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 14921 times.
14997 if (pps->chroma_qp_index_offset[0] != pps->chroma_qp_index_offset[1])
879 76 pps->chroma_qp_diff = 1;
880
881
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14997 times.
14997 if (avctx->debug & FF_DEBUG_PICT_INFO) {
882 av_log(avctx, AV_LOG_DEBUG,
883 "pps:%u sps:%u %s slice_groups:%d ref:%u/%u %s qp:%d/%d/%d/%d %s %s %s %s\n",
884 pps_id, pps->sps_id,
885 pps->cabac ? "CABAC" : "CAVLC",
886 pps->slice_group_count,
887 pps->ref_count[0], pps->ref_count[1],
888 pps->weighted_pred ? "weighted" : "",
889 pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset[0], pps->chroma_qp_index_offset[1],
890 pps->deblocking_filter_parameters_present ? "LPAR" : "",
891 pps->constrained_intra_pred ? "CONSTR" : "",
892 pps->redundant_pic_cnt_present ? "REDU" : "",
893 pps->transform_8x8_mode ? "8x8DCT" : "");
894 }
895
896 14997 remove_pps(ps, pps_id);
897 14997 ps->pps_list[pps_id] = pps_buf;
898
899 14997 return 0;
900
901 14 fail:
902 14 av_buffer_unref(&pps_buf);
903 14 return ret;
904 }
905