FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_ps.c
Date: 2023-09-24 13:02:57
Exec Total Coverage
Lines: 400 509 78.6%
Functions: 15 15 100.0%
Branches: 227 336 67.6%

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