FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/ps.c
Date: 2026-02-12 10:37:11
Exec Total Coverage
Lines: 1078 1520 70.9%
Functions: 29 30 96.7%
Branches: 660 1035 63.8%

Line Branch Exec Source
1 /*
2 * HEVC Parameter Set decoding
3 *
4 * Copyright (C) 2012 - 2013 Guillaume Martres
5 * Copyright (C) 2012 - 2013 Mickael Raulet
6 * Copyright (C) 2012 - 2013 Gildas Cocherel
7 * Copyright (C) 2013 Vittorio Giovara
8 *
9 * This file is part of FFmpeg.
10 *
11 * FFmpeg is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * FFmpeg is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include "libavutil/imgutils.h"
27 #include "libavutil/mem.h"
28 #include "golomb.h"
29 #include "h2645_vui.h"
30 #include "data.h"
31 #include "ps.h"
32 #include "profiles.h"
33 #include "libavutil/refstruct.h"
34
35 static const uint8_t default_scaling_list_intra[] = {
36 16, 16, 16, 16, 17, 18, 21, 24,
37 16, 16, 16, 16, 17, 19, 22, 25,
38 16, 16, 17, 18, 20, 22, 25, 29,
39 16, 16, 18, 21, 24, 27, 31, 36,
40 17, 17, 20, 24, 30, 35, 41, 47,
41 18, 19, 22, 27, 35, 44, 54, 65,
42 21, 22, 25, 31, 41, 54, 70, 88,
43 24, 25, 29, 36, 47, 65, 88, 115
44 };
45
46 static const uint8_t default_scaling_list_inter[] = {
47 16, 16, 16, 16, 17, 18, 20, 24,
48 16, 16, 16, 17, 18, 20, 24, 25,
49 16, 16, 17, 18, 20, 24, 25, 28,
50 16, 17, 18, 20, 24, 25, 28, 33,
51 17, 18, 20, 24, 25, 28, 33, 41,
52 18, 20, 24, 25, 28, 33, 41, 54,
53 20, 24, 25, 28, 33, 41, 54, 71,
54 24, 25, 28, 33, 41, 54, 71, 91
55 };
56
57 static const uint8_t hevc_sub_width_c[] = {
58 1, 2, 2, 1
59 };
60
61 static const uint8_t hevc_sub_height_c[] = {
62 1, 2, 1, 1
63 };
64
65 800 static void remove_sps(HEVCParamSets *s, int id)
66 {
67 int i;
68
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 782 times.
800 if (s->sps_list[id]) {
69 /* drop all PPS that depend on this SPS */
70
2/2
✓ Branch 0 taken 1152 times.
✓ Branch 1 taken 18 times.
1170 for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++)
71
4/4
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1133 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 1 times.
1152 if (s->pps_list[i] && s->pps_list[i]->sps_id == id)
72 18 av_refstruct_unref(&s->pps_list[i]);
73
74 18 av_refstruct_unref(&s->sps_list[id]);
75 }
76 800 }
77
78 752 static void remove_vps(HEVCParamSets *s, int id)
79 {
80 int i;
81
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 743 times.
752 if (s->vps_list[id]) {
82
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 9 times.
153 for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++)
83
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 134 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
144 if (s->sps_list[i] && s->sps_list[i]->vps_id == id)
84 10 remove_sps(s, i);
85 9 av_refstruct_unref(&s->vps_list[id]);
86 }
87 752 }
88
89 14218 int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx,
90 ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header)
91 {
92 int delta_poc;
93 14218 int k0 = 0;
94 14218 int k = 0;
95 int i;
96
97 14218 rps->used = 0;
98 14218 rps->rps_predict = 0;
99
100
4/4
✓ Branch 0 taken 13133 times.
✓ Branch 1 taken 1085 times.
✓ Branch 2 taken 12809 times.
✓ Branch 3 taken 324 times.
14218 if (rps != sps->st_rps && sps->nb_st_rps)
101 12809 rps->rps_predict = get_bits1(gb);
102
103
2/2
✓ Branch 0 taken 8303 times.
✓ Branch 1 taken 5915 times.
14218 if (rps->rps_predict) {
104 const ShortTermRPS *rps_ridx;
105 8303 uint8_t used[32] = { 0 };
106 int delta_rps;
107
108
2/2
✓ Branch 0 taken 699 times.
✓ Branch 1 taken 7604 times.
8303 if (is_slice_header) {
109 699 rps->delta_idx = get_ue_golomb_long(gb) + 1;
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 699 times.
699 if (rps->delta_idx > sps->nb_st_rps) {
111 av_log(avctx, AV_LOG_ERROR,
112 "Invalid value of delta_idx in slice header RPS: %d > %d.\n",
113 rps->delta_idx, sps->nb_st_rps);
114 return AVERROR_INVALIDDATA;
115 }
116 699 rps_ridx = &sps->st_rps[sps->nb_st_rps - rps->delta_idx];
117 699 rps->rps_idx_num_delta_pocs = rps_ridx->num_delta_pocs;
118 } else
119 7604 rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
120
121 8303 rps->delta_rps_sign = get_bits1(gb);
122 8303 rps->abs_delta_rps = get_ue_golomb_long(gb) + 1;
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8303 times.
8303 if (rps->abs_delta_rps > 32768) {
124 av_log(avctx, AV_LOG_ERROR,
125 "Invalid value of abs_delta_rps: %d\n",
126 rps->abs_delta_rps);
127 return AVERROR_INVALIDDATA;
128 }
129 8303 delta_rps = (1 - (rps->delta_rps_sign << 1)) * rps->abs_delta_rps;
130
2/2
✓ Branch 0 taken 36028 times.
✓ Branch 1 taken 8303 times.
44331 for (i = 0; i <= rps_ridx->num_delta_pocs; i++) {
131 36028 used[k] = get_bits1(gb);
132
133 36028 rps->use_delta = 0;
134
2/2
✓ Branch 0 taken 7164 times.
✓ Branch 1 taken 28864 times.
36028 if (!used[k])
135 7164 rps->use_delta = get_bits1(gb);
136
137
4/4
✓ Branch 0 taken 7164 times.
✓ Branch 1 taken 28864 times.
✓ Branch 2 taken 50 times.
✓ Branch 3 taken 7114 times.
36028 if (used[k] || rps->use_delta) {
138
2/2
✓ Branch 0 taken 22846 times.
✓ Branch 1 taken 6068 times.
28914 if (i < rps_ridx->num_delta_pocs)
139 22846 delta_poc = delta_rps + rps_ridx->delta_poc[i];
140 else
141 6068 delta_poc = delta_rps;
142 28914 rps->delta_poc[k] = delta_poc;
143
2/2
✓ Branch 0 taken 19692 times.
✓ Branch 1 taken 9222 times.
28914 if (delta_poc < 0)
144 19692 k0++;
145 28914 k++;
146 }
147 }
148
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8303 times.
8303 if (k >= FF_ARRAY_ELEMS(used)) {
150 av_log(avctx, AV_LOG_ERROR,
151 "Invalid num_delta_pocs: %d\n", k);
152 return AVERROR_INVALIDDATA;
153 }
154
155 8303 rps->num_delta_pocs = k;
156 8303 rps->num_negative_pics = k0;
157 // sort in increasing order (smallest first)
158
1/2
✓ Branch 0 taken 8303 times.
✗ Branch 1 not taken.
8303 if (rps->num_delta_pocs != 0) {
159 int u, tmp;
160
2/2
✓ Branch 0 taken 20611 times.
✓ Branch 1 taken 8303 times.
28914 for (i = 1; i < rps->num_delta_pocs; i++) {
161 20611 delta_poc = rps->delta_poc[i];
162 20611 u = used[i];
163
2/2
✓ Branch 0 taken 38361 times.
✓ Branch 1 taken 20611 times.
58972 for (k = i - 1; k >= 0; k--) {
164 38361 tmp = rps->delta_poc[k];
165
2/2
✓ Branch 0 taken 12001 times.
✓ Branch 1 taken 26360 times.
38361 if (delta_poc < tmp) {
166 12001 rps->delta_poc[k + 1] = tmp;
167 12001 used[k + 1] = used[k];
168 12001 rps->delta_poc[k] = delta_poc;
169 12001 used[k] = u;
170 }
171 }
172 }
173 }
174
2/2
✓ Branch 0 taken 6243 times.
✓ Branch 1 taken 2060 times.
8303 if ((rps->num_negative_pics >> 1) != 0) {
175 int u;
176 6243 k = rps->num_negative_pics - 1;
177 // flip the negative values to largest first
178
2/2
✓ Branch 0 taken 8052 times.
✓ Branch 1 taken 6243 times.
14295 for (i = 0; i < rps->num_negative_pics >> 1; i++) {
179 8052 delta_poc = rps->delta_poc[i];
180 8052 u = used[i];
181 8052 rps->delta_poc[i] = rps->delta_poc[k];
182 8052 used[i] = used[k];
183 8052 rps->delta_poc[k] = delta_poc;
184 8052 used[k] = u;
185 8052 k--;
186 }
187 }
188
189
2/2
✓ Branch 0 taken 265696 times.
✓ Branch 1 taken 8303 times.
273999 for (unsigned i = 0; i < FF_ARRAY_ELEMS(used); i++)
190 265696 rps->used |= (uint32_t)used[i] << i;
191 } else {
192 unsigned int nb_positive_pics;
193
194 5915 rps->num_negative_pics = get_ue_golomb_long(gb);
195 5915 nb_positive_pics = get_ue_golomb_long(gb);
196
197
2/4
✓ Branch 0 taken 5915 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5915 times.
5915 if (rps->num_negative_pics >= HEVC_MAX_REFS ||
198 nb_positive_pics >= HEVC_MAX_REFS) {
199 av_log(avctx, AV_LOG_ERROR, "Too many refs in a short term RPS.\n");
200 return AVERROR_INVALIDDATA;
201 }
202
203 5915 rps->num_delta_pocs = rps->num_negative_pics + nb_positive_pics;
204
2/2
✓ Branch 0 taken 4902 times.
✓ Branch 1 taken 1013 times.
5915 if (rps->num_delta_pocs) {
205 4902 int prev = 0;
206
207
2/2
✓ Branch 0 taken 14503 times.
✓ Branch 1 taken 4902 times.
19405 for (i = 0; i < rps->num_negative_pics; i++) {
208 14503 delta_poc = get_ue_golomb_long(gb) + 1;
209
2/4
✓ Branch 0 taken 14503 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14503 times.
14503 if (delta_poc < 1 || delta_poc > 32768) {
210 av_log(avctx, AV_LOG_ERROR,
211 "Invalid value of delta_poc: %d\n",
212 delta_poc);
213 return AVERROR_INVALIDDATA;
214 }
215 14503 prev -= delta_poc;
216 14503 rps->delta_poc[i] = prev;
217 14503 rps->used |= get_bits1(gb) * (1 << i);
218 }
219 4902 prev = 0;
220
2/2
✓ Branch 0 taken 1235 times.
✓ Branch 1 taken 4902 times.
6137 for (i = 0; i < nb_positive_pics; i++) {
221 1235 delta_poc = get_ue_golomb_long(gb) + 1;
222
2/4
✓ Branch 0 taken 1235 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1235 times.
1235 if (delta_poc < 1 || delta_poc > 32768) {
223 av_log(avctx, AV_LOG_ERROR,
224 "Invalid value of delta_poc: %d\n",
225 delta_poc);
226 return AVERROR_INVALIDDATA;
227 }
228 1235 prev += delta_poc;
229 1235 rps->delta_poc[rps->num_negative_pics + i] = prev;
230 1235 rps->used |= get_bits1(gb) * (1 << (rps->num_negative_pics + i));
231 }
232 }
233 }
234 14218 return 0;
235 }
236
237
238 2044 static int decode_profile_tier_level(GetBitContext *gb, AVCodecContext *avctx,
239 PTLCommon *ptl)
240 {
241 int i;
242
243
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2044 times.
2044 if (get_bits_left(gb) < 2+1+5 + 32 + 4 + 43 + 1)
244 return -1;
245
246 2044 ptl->profile_space = get_bits(gb, 2);
247 2044 ptl->tier_flag = get_bits1(gb);
248 2044 ptl->profile_idc = get_bits(gb, 5);
249
250 #if !CONFIG_SMALL
251 2044 const char *profile_name = NULL;
252
2/2
✓ Branch 0 taken 2877 times.
✓ Branch 1 taken 40 times.
2917 for (int i = 0; ff_hevc_profiles[i].profile != AV_PROFILE_UNKNOWN; i++)
253
2/2
✓ Branch 0 taken 2004 times.
✓ Branch 1 taken 873 times.
2877 if (ff_hevc_profiles[i].profile == ptl->profile_idc) {
254 2004 profile_name = ff_hevc_profiles[i].name;
255 2004 break;
256 }
257
4/4
✓ Branch 0 taken 2004 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 2004 times.
✓ Branch 3 taken 40 times.
2044 av_log(avctx, profile_name ? AV_LOG_DEBUG : AV_LOG_WARNING,
258 "%s profile bitstream\n", profile_name ? profile_name : "Unknown");
259 #endif
260
261
2/2
✓ Branch 0 taken 65408 times.
✓ Branch 1 taken 2044 times.
67452 for (i = 0; i < 32; i++) {
262 65408 ptl->profile_compatibility_flag[i] = get_bits1(gb);
263
264
6/6
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 65312 times.
✓ Branch 2 taken 64 times.
✓ Branch 3 taken 32 times.
✓ Branch 4 taken 32 times.
✓ Branch 5 taken 32 times.
65408 if (ptl->profile_idc == 0 && i > 0 && ptl->profile_compatibility_flag[i])
265 32 ptl->profile_idc = i;
266 }
267 2044 ptl->progressive_source_flag = get_bits1(gb);
268 2044 ptl->interlaced_source_flag = get_bits1(gb);
269 2044 ptl->non_packed_constraint_flag = get_bits1(gb);
270 2044 ptl->frame_only_constraint_flag = get_bits1(gb);
271
272 #define check_profile_idc(idc) \
273 ptl->profile_idc == idc || ptl->profile_compatibility_flag[idc]
274
275
9/12
✓ Branch 0 taken 1940 times.
✓ Branch 1 taken 104 times.
✓ Branch 2 taken 1940 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1932 times.
✓ Branch 5 taken 8 times.
✓ Branch 6 taken 1932 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1907 times.
✓ Branch 9 taken 25 times.
✓ Branch 10 taken 1907 times.
✗ Branch 11 not taken.
2044 if (check_profile_idc(4) || check_profile_idc(5) || check_profile_idc(6) ||
276
6/12
✓ Branch 0 taken 1907 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1907 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1907 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1907 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1907 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1907 times.
✗ Branch 11 not taken.
1907 check_profile_idc(7) || check_profile_idc(8) || check_profile_idc(9) ||
277
2/4
✓ Branch 0 taken 1907 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1907 times.
1907 check_profile_idc(10)) {
278
279 137 ptl->max_12bit_constraint_flag = get_bits1(gb);
280 137 ptl->max_10bit_constraint_flag = get_bits1(gb);
281 137 ptl->max_8bit_constraint_flag = get_bits1(gb);
282 137 ptl->max_422chroma_constraint_flag = get_bits1(gb);
283 137 ptl->max_420chroma_constraint_flag = get_bits1(gb);
284 137 ptl->max_monochrome_constraint_flag = get_bits1(gb);
285 137 ptl->intra_constraint_flag = get_bits1(gb);
286 137 ptl->one_picture_only_constraint_flag = get_bits1(gb);
287 137 ptl->lower_bit_rate_constraint_flag = get_bits1(gb);
288
289
7/12
✓ Branch 0 taken 129 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 129 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 129 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 129 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 129 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 129 times.
137 if (check_profile_idc(5) || check_profile_idc(9) || check_profile_idc(10)) {
290 8 ptl->max_14bit_constraint_flag = get_bits1(gb);
291 8 skip_bits_long(gb, 33); // XXX_reserved_zero_33bits[0..32]
292 } else {
293 129 skip_bits_long(gb, 34); // XXX_reserved_zero_34bits[0..33]
294 }
295
4/4
✓ Branch 0 taken 1668 times.
✓ Branch 1 taken 239 times.
✓ Branch 2 taken 1397 times.
✓ Branch 3 taken 271 times.
1907 } else if (check_profile_idc(2)) {
296 1636 skip_bits(gb, 7);
297 1636 ptl->one_picture_only_constraint_flag = get_bits1(gb);
298 1636 skip_bits_long(gb, 35); // XXX_reserved_zero_35bits[0..34]
299 } else {
300 271 skip_bits_long(gb, 43); // XXX_reserved_zero_43bits[0..42]
301 }
302
303
9/12
✓ Branch 0 taken 383 times.
✓ Branch 1 taken 1661 times.
✓ Branch 2 taken 376 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 137 times.
✓ Branch 5 taken 239 times.
✓ Branch 6 taken 137 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 137 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 137 times.
✗ Branch 11 not taken.
2044 if (check_profile_idc(1) || check_profile_idc(2) || check_profile_idc(3) ||
304
8/12
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 104 times.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 25 times.
✓ Branch 5 taken 8 times.
✓ Branch 6 taken 25 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 25 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 25 times.
137 check_profile_idc(4) || check_profile_idc(5) || check_profile_idc(9))
305 2019 ptl->inbld_flag = get_bits1(gb);
306 else
307 25 skip_bits1(gb);
308 #undef check_profile_idc
309
310 2044 return 0;
311 }
312
313 2029 static int parse_ptl(GetBitContext *gb, AVCodecContext *avctx,
314 int profile_present, PTL *ptl, int max_num_sub_layers)
315 {
316 2029 int i, status = 0;
317
318
2/2
✓ Branch 0 taken 2002 times.
✓ Branch 1 taken 27 times.
2029 if (profile_present) {
319 2002 status = decode_profile_tier_level(gb, avctx, &ptl->general_ptl);
320 } else {
321 27 memset(&ptl->general_ptl, 0, sizeof(ptl->general_ptl));
322 }
323
324
4/6
✓ Branch 0 taken 2029 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 153 times.
✓ Branch 4 taken 1876 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2029 times.
2029 if (status < 0 || get_bits_left(gb) < 8 + (8*2 * (max_num_sub_layers - 1 > 0))) {
325 av_log(avctx, AV_LOG_ERROR, "PTL information too short\n");
326 return -1;
327 }
328
329 2029 ptl->general_ptl.level_idc = get_bits(gb, 8);
330
331
2/2
✓ Branch 0 taken 303 times.
✓ Branch 1 taken 2029 times.
2332 for (i = 0; i < max_num_sub_layers - 1; i++) {
332 303 ptl->sub_layer_profile_present_flag[i] = get_bits1(gb);
333 303 ptl->sub_layer_level_present_flag[i] = get_bits1(gb);
334 }
335
336
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 1876 times.
2029 if (max_num_sub_layers - 1> 0)
337
2/2
✓ Branch 0 taken 921 times.
✓ Branch 1 taken 153 times.
1074 for (i = max_num_sub_layers - 1; i < 8; i++)
338 921 skip_bits(gb, 2); // reserved_zero_2bits[i]
339
2/2
✓ Branch 0 taken 303 times.
✓ Branch 1 taken 2029 times.
2332 for (i = 0; i < max_num_sub_layers - 1; i++) {
340
3/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 261 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 42 times.
345 if (ptl->sub_layer_profile_present_flag[i] &&
341 42 decode_profile_tier_level(gb, avctx, &ptl->sub_layer_ptl[i]) < 0) {
342 av_log(avctx, AV_LOG_ERROR,
343 "PTL information for sublayer %i too short\n", i);
344 return -1;
345 }
346
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 261 times.
303 if (ptl->sub_layer_level_present_flag[i]) {
347
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
42 if (get_bits_left(gb) < 8) {
348 av_log(avctx, AV_LOG_ERROR,
349 "Not enough data for sublayer %i level_idc\n", i);
350 return -1;
351 } else
352 42 ptl->sub_layer_ptl[i].level_idc = get_bits(gb, 8);
353 }
354 }
355
356 2029 return 0;
357 }
358
359 112 static void decode_sublayer_hrd(GetBitContext *gb, unsigned int nb_cpb,
360 HEVCSublayerHdrParams *par, int subpic_params_present)
361 {
362 int i;
363
364
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 112 times.
224 for (i = 0; i < nb_cpb; i++) {
365 112 par->bit_rate_value_minus1[i] = get_ue_golomb_long(gb);
366 112 par->cpb_size_value_minus1[i] = get_ue_golomb_long(gb);
367
368
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 78 times.
112 if (subpic_params_present) {
369 34 par->cpb_size_du_value_minus1[i] = get_ue_golomb_long(gb);
370 34 par->bit_rate_du_value_minus1[i] = get_ue_golomb_long(gb);
371 }
372
373 112 par->cbr_flag |= get_bits1(gb) << i;
374 }
375 112 }
376
377 61 static int decode_hrd(GetBitContext *gb, int common_inf_present,
378 HEVCHdrParams *hdr, int max_sublayers)
379 {
380
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 if (common_inf_present) {
381 61 hdr->nal_hrd_parameters_present_flag = get_bits1(gb);
382 61 hdr->vcl_hrd_parameters_present_flag = get_bits1(gb);
383
384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (hdr->nal_hrd_parameters_present_flag ||
385 hdr->vcl_hrd_parameters_present_flag) {
386 61 hdr->sub_pic_hrd_params_present_flag = get_bits1(gb);
387
388
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 44 times.
61 if (hdr->sub_pic_hrd_params_present_flag) {
389 17 hdr->tick_divisor_minus2 = get_bits(gb, 8);
390 17 hdr->du_cpb_removal_delay_increment_length_minus1 = get_bits(gb, 5);
391 17 hdr->sub_pic_cpb_params_in_pic_timing_sei_flag = get_bits1(gb);
392 17 hdr->dpb_output_delay_du_length_minus1 = get_bits(gb, 5);
393 }
394
395 61 hdr->bit_rate_scale = get_bits(gb, 4);
396 61 hdr->cpb_size_scale = get_bits(gb, 4);
397
398
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 44 times.
61 if (hdr->sub_pic_hrd_params_present_flag)
399 17 hdr->cpb_size_du_scale = get_bits(gb, 4);
400
401 61 hdr->initial_cpb_removal_delay_length_minus1 = get_bits(gb, 5);
402 61 hdr->au_cpb_removal_delay_length_minus1 = get_bits(gb, 5);
403 61 hdr->dpb_output_delay_length_minus1 = get_bits(gb, 5);
404 }
405 }
406
407
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 61 times.
122 for (int i = 0; i < max_sublayers; i++) {
408 61 unsigned fixed_pic_rate_general_flag = get_bits1(gb);
409 61 unsigned fixed_pic_rate_within_cvs_flag = 0;
410 61 unsigned low_delay_hrd_flag = 0;
411 61 hdr->flags.fixed_pic_rate_general_flag |= fixed_pic_rate_general_flag << i;
412
413
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 38 times.
61 if (!fixed_pic_rate_general_flag)
414 23 fixed_pic_rate_within_cvs_flag = get_bits1(gb);
415 61 hdr->flags.fixed_pic_rate_within_cvs_flag |= fixed_pic_rate_within_cvs_flag << i;
416
417
3/4
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
✓ Branch 3 taken 23 times.
61 if (fixed_pic_rate_within_cvs_flag || fixed_pic_rate_general_flag)
418 38 hdr->elemental_duration_in_tc_minus1[i] = get_ue_golomb_long(gb);
419 else
420 23 low_delay_hrd_flag = get_bits1(gb);
421 61 hdr->flags.low_delay_hrd_flag |= low_delay_hrd_flag << i;
422
423
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 if (!low_delay_hrd_flag) {
424 61 unsigned cpb_cnt_minus1 = get_ue_golomb_long(gb);
425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (cpb_cnt_minus1 > 31) {
426 av_log(NULL, AV_LOG_ERROR, "nb_cpb %d invalid\n",
427 cpb_cnt_minus1);
428 return AVERROR_INVALIDDATA;
429 }
430 61 hdr->cpb_cnt_minus1[i] = cpb_cnt_minus1;
431 }
432
433
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 if (hdr->nal_hrd_parameters_present_flag)
434 61 decode_sublayer_hrd(gb, hdr->cpb_cnt_minus1[i]+1, &hdr->nal_params[i],
435 61 hdr->sub_pic_hrd_params_present_flag);
436
437
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 10 times.
61 if (hdr->vcl_hrd_parameters_present_flag)
438 51 decode_sublayer_hrd(gb, hdr->cpb_cnt_minus1[i]+1, &hdr->vcl_params[i],
439 51 hdr->sub_pic_hrd_params_present_flag);
440 }
441
442 61 return 0;
443 }
444
445 752 static void hevc_vps_free(AVRefStructOpaque opaque, void *obj)
446 {
447 752 HEVCVPS *vps = obj;
448
449 752 av_freep(&vps->hdr);
450 752 av_freep(&vps->data);
451 752 }
452
453 enum DependencyType {
454 HEVC_DEP_TYPE_SAMPLE = 0,
455 HEVC_DEP_TYPE_MV = 1,
456 HEVC_DEP_TYPE_BOTH = 2,
457 };
458
459 27 static int decode_vps_ext(GetBitContext *gb, AVCodecContext *avctx, HEVCVPS *vps,
460 uint64_t layer1_id_included)
461 {
462 PTL ptl_dummy;
463 27 uint8_t max_sub_layers[HEVC_MAX_LAYERS] = {1, 1};
464 27 uint8_t dimension_id_len[16] = {0};
465 27 uint8_t dimension_id[16] = {0};
466 unsigned n;
467
468 int splitting_flag, view_id_len, num_add_olss, num_scalability_types,
469 default_output_layer_idc, direct_dep_type_len, direct_dep_type,
470 sub_layers_max_present, sub_layer_flag_info_present_flag, nb_ptl;
471 unsigned non_vui_extension_length;
472
473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (vps->vps_max_layers == 1) {
474 av_log(avctx, AV_LOG_VERBOSE, "Ignoring VPS extensions with a single layer\n");
475 return 0;
476 }
477
478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (vps->vps_max_layers > 2) {
479 av_log(avctx, AV_LOG_ERROR,
480 "VPS has %d layers, only 2 layers are supported\n",
481 vps->vps_max_layers);
482 return AVERROR_PATCHWELCOME;
483 }
484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (vps->vps_num_layer_sets > 2) {
485 av_log(avctx, AV_LOG_ERROR,
486 "VPS has %d layer sets, only 2 layer sets are supported\n",
487 vps->vps_num_layer_sets);
488 return AVERROR_PATCHWELCOME;
489 }
490
491 27 align_get_bits(gb);
492
493 /**
494 * For stereoscopic MV-HEVC, the following simplifying assumptions are made:
495 *
496 * - vps_max_layers = 2 (one base layer, one multiview layer)
497 * - vps_num_layer_sets = 2 (one output layer set for each view)
498 * - NumScalabilityTypes = 1 (only HEVC_SCALABILITY_MULTIVIEW)
499 * - direct_dependency_flag[1][0] = 1 (second layer depends on first)
500 * - num_add_olss = 0 (no extra output layer sets)
501 * - default_output_layer_idc = 0 (1:1 mapping between OLSs and layers)
502 * - layer_id_included_flag[1] = {1, 1} (consequence of layer dependencies)
503 * - vps_num_rep_formats_minus1 = 0 (all layers have the same size)
504 *
505 * Which results in the following derived variables:
506 * - ViewOrderIdx = {0, 1}
507 * - NumViews = 2
508 * - DependencyFlag[1][0] = 1
509 * - NumDirectRefLayers = {0, 1}
510 * - NumRefLayers = {0, 1}
511 * - NumPredictedLayers = {1, 0}
512 * - NumIndependentLayers = 1
513 * - NumLayersInTreePartition = {2}
514 * - NumLayerSets = 2
515 * - NumOutputLayerSets = 2
516 * - OlsIdxToLsIdx = {0, 1}
517 * - LayerIdxInVps = {0, 1}
518 * - NumLayersInIdList = {1, 2}
519 * - NumNecessaryLayers = {1, 2}
520 * - NecessaryLayerFlag = {{1, 0}, {1, 1}}
521 * - NumOutputLayersInOutputLayerSet = {1, 2}
522 * - OutputLayerFlag = {{1, 0}, {1, 1}}
523 */
524 27 vps->nb_layers = 2;
525
526 /* vps_base_layer_internal_flag is true has been checked before */
527
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
27 if (parse_ptl(gb, avctx, 0, &ptl_dummy, vps->vps_max_sub_layers) < 0)
528 return AVERROR_INVALIDDATA;
529
530 27 splitting_flag = get_bits1(gb);
531 27 vps->scalability_mask_flag = get_bits(gb, 16);
532 27 num_scalability_types = av_popcount(vps->scalability_mask_flag);
533
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!num_scalability_types) {
534 av_log(avctx, AV_LOG_ERROR, "Missing scalability mask\n");
535 return AVERROR_INVALIDDATA;
536 }
537
538
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!(vps->scalability_mask_flag &
539 (HEVC_SCALABILITY_MULTIVIEW | HEVC_SCALABILITY_AUXILIARY))) {
540 av_log(avctx, AV_LOG_ERROR, "Scalability type %d not supported\n",
541 15 - ff_ctz(vps->scalability_mask_flag));
542 return AVERROR_PATCHWELCOME;
543 }
544 // x265 specify MULTIVIEW when the stream really is alpha video only.
545
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (num_scalability_types > 1)
546 av_log(avctx, AV_LOG_WARNING, "Multiple scalability types presented\n");
547
548 27 n = 0;
549
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 27 times.
54 for (int i = 0; i < num_scalability_types - splitting_flag; i++) {
550 27 dimension_id_len[i] = get_bits(gb, 3) + 1;
551 27 n += dimension_id_len[i];
552 }
553
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (splitting_flag)
554 dimension_id_len[num_scalability_types - 1] = 5 - n;
555
556
2/2
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 14 times.
27 if (get_bits1(gb)) { /* vps_nuh_layer_id_present_flag */
557 13 int layer_id_in_nuh = get_bits(gb, 6);
558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (layer_id_in_nuh >= FF_ARRAY_ELEMS(vps->layer_idx)) {
559 av_log(avctx, AV_LOG_ERROR, "Invalid layer_id_in_nuh[1]: %d\n",
560 layer_id_in_nuh);
561 return AVERROR_INVALIDDATA;
562 }
563 13 vps->layer_idx[layer_id_in_nuh] = 1;
564 13 vps->layer_id_in_nuh[1] = layer_id_in_nuh;
565 } else {
566 14 vps->layer_idx[1] = 1;
567 14 vps->layer_id_in_nuh[1] = 1;
568 }
569
570
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (!splitting_flag) {
571 27 int index = 0;
572
573
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 27 times.
54 for (int i = 0; i < num_scalability_types; i++)
574 27 dimension_id[i] = get_bits(gb, dimension_id_len[i]);
575
576
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2 times.
27 if (vps->scalability_mask_flag & HEVC_SCALABILITY_MULTIVIEW)
577 25 index++;
578
579 /* AuxId 1 is alpha, 2 is depth. Only support alpha */
580
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 25 times.
27 if (vps->scalability_mask_flag & HEVC_SCALABILITY_AUXILIARY &&
581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 dimension_id[index] != HEVC_AUX_ALPHA) {
582 av_log(avctx, AV_LOG_WARNING,
583 "Unsupported dimension_id %d for HEVC_SCALABILITY_AUXILIARY\n",
584 dimension_id[index]);
585 return AVERROR_PATCHWELCOME;
586 }
587 }
588
589 27 view_id_len = get_bits(gb, 4);
590
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2 times.
27 if (view_id_len) {
591
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 n = (vps->scalability_mask_flag & HEVC_SCALABILITY_MULTIVIEW) ? 2 : 1;
592
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 25 times.
75 for (int i = 0; i < n; i++)
593 50 vps->view_id[i] = get_bits(gb, view_id_len);
594 }
595
596 /* direct_dependency_flag */
597 27 vps->num_direct_ref_layers[1] = get_bits1(gb);
598
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 25 times.
27 if (!vps->num_direct_ref_layers[1]) {
599 2 vps->num_add_layer_sets = get_ue_golomb(gb);
600
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (vps->num_add_layer_sets > 1) {
601 av_log(avctx, AV_LOG_WARNING,
602 "Unsupported num_add_layer_sets: %d\n", vps->num_add_layer_sets);
603 return AVERROR_PATCHWELCOME;
604 }
605
606
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (vps->num_add_layer_sets) {
607 /* highest_layer_idx_plus1 */
608
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!get_bits1(gb))
609 return AVERROR_PATCHWELCOME;
610 }
611 }
612 27 vps->num_output_layer_sets = vps->vps_num_layer_sets + vps->num_add_layer_sets;
613
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (vps->num_output_layer_sets != 2)
614 return AVERROR_INVALIDDATA;
615
616 27 sub_layers_max_present = get_bits1(gb); // vps_sub_layers_max_minus1_present_flag
617
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (sub_layers_max_present) {
618 for (int i = 0; i < vps->vps_max_layers; i++)
619 max_sub_layers[i] = sub_layers_max_present ? get_bits(gb, 3) + 1 :
620 vps->vps_max_sub_layers;
621 }
622
623
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
27 if (get_bits1(gb) /* max_tid_ref_present_flag */)
624 skip_bits(gb, 3); // max_tid_il_ref_pics_plus1
625
626 27 vps->default_ref_layers_active = get_bits1(gb);
627
628 27 nb_ptl = get_ue_golomb(gb) + 1;
629 /* idx [0] is signalled in base VPS, idx [1] is signalled at the
630 * start of VPS extension, indices 2+ are signalled here;
631 * we ignore all but the first one anyway */
632
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 27 times.
52 for (int i = 2; i < nb_ptl; i++) {
633 25 int profile_present = get_bits1(gb);
634
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
25 if (parse_ptl(gb, avctx, profile_present, &ptl_dummy, vps->vps_max_sub_layers) < 0)
635 return AVERROR_INVALIDDATA;
636 }
637
638 27 num_add_olss = get_ue_golomb(gb);
639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (num_add_olss != 0) {
640 /* Since we don't implement support for independent output layer sets
641 * and auxiliary layers, this should never nonzero */
642 av_log(avctx, AV_LOG_ERROR, "Unexpected num_add_olss: %d\n", num_add_olss);
643 return AVERROR_PATCHWELCOME;
644 }
645
646 27 default_output_layer_idc = get_bits(gb, 2);
647
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (default_output_layer_idc != 0) {
648 av_log(avctx, AV_LOG_WARNING, "Unsupported default_output_layer_idc: %d\n",
649 default_output_layer_idc);
650 return AVERROR_PATCHWELCOME;
651 }
652
653 /* Consequence of established layer dependencies */
654
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2 times.
27 if (layer1_id_included &&
655 25 layer1_id_included != ((1ULL << vps->layer_id_in_nuh[0]) |
656
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 (1ULL << vps->layer_id_in_nuh[1]))) {
657 av_log(avctx, AV_LOG_ERROR,
658 "Dependent layer not included in layer ID?\n");
659 return AVERROR_PATCHWELCOME;
660 }
661
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 25 times.
27 if (!layer1_id_included)
662 2 vps->ols[1] = 2;
663 else
664 25 vps->ols[1] = 3;
665
666
3/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 25 times.
27 if (vps->vps_num_layer_sets == 1 || default_output_layer_idc == 2)
667 2 skip_bits1(gb);
668
669
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (nb_ptl > 1)
670
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 27 times.
79 for (int j = 0; j < av_popcount64(vps->ols[1]); j++) {
671 52 int ptl_idx = get_bits(gb, av_ceil_log2(nb_ptl));
672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (ptl_idx >= nb_ptl) {
673 av_log(avctx, AV_LOG_ERROR, "Invalid PTL index: %d\n", ptl_idx);
674 return AVERROR_INVALIDDATA;
675 }
676 }
677
678
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
27 if (get_ue_golomb_31(gb) != 0 /* vps_num_rep_formats_minus1 */) {
679 av_log(avctx, AV_LOG_ERROR, "Unexpected extra rep formats\n");
680 return AVERROR_INVALIDDATA;
681 }
682
683 27 vps->rep_format.pic_width_in_luma_samples = get_bits(gb, 16);
684 27 vps->rep_format.pic_height_in_luma_samples = get_bits(gb, 16);
685
686
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
27 if (!get_bits1(gb) /* chroma_and_bit_depth_vps_present_flag */) {
687 av_log(avctx, AV_LOG_ERROR,
688 "chroma_and_bit_depth_vps_present_flag=0 in first rep_format\n");
689 return AVERROR_INVALIDDATA;
690 }
691 27 vps->rep_format.chroma_format_idc = get_bits(gb, 2);
692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (vps->rep_format.chroma_format_idc == 3)
693 vps->rep_format.separate_colour_plane_flag = get_bits1(gb);
694 27 vps->rep_format.bit_depth_luma = get_bits(gb, 4) + 8;
695 27 vps->rep_format.bit_depth_chroma = get_bits(gb, 4) + 8;
696
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (vps->rep_format.bit_depth_luma > 16 ||
697
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 vps->rep_format.bit_depth_chroma > 16 ||
698
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 vps->rep_format.bit_depth_luma != vps->rep_format.bit_depth_chroma) {
699 av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %"PRIu8" %"PRIu8"\n",
700 vps->rep_format.bit_depth_luma, vps->rep_format.bit_depth_chroma);
701 return AVERROR_PATCHWELCOME;
702 }
703
704
2/2
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 2 times.
27 if (get_bits1(gb) /* conformance_window_vps_flag */) {
705 25 int vert_mult = hevc_sub_height_c[vps->rep_format.chroma_format_idc];
706 25 int horiz_mult = hevc_sub_width_c[vps->rep_format.chroma_format_idc];
707 25 vps->rep_format.conf_win_left_offset = get_ue_golomb(gb) * horiz_mult;
708 25 vps->rep_format.conf_win_right_offset = get_ue_golomb(gb) * horiz_mult;
709 25 vps->rep_format.conf_win_top_offset = get_ue_golomb(gb) * vert_mult;
710 25 vps->rep_format.conf_win_bottom_offset = get_ue_golomb(gb) * vert_mult;
711 }
712
713 27 vps->max_one_active_ref_layer = get_bits1(gb);
714 27 vps->poc_lsb_aligned = get_bits1(gb);
715
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 25 times.
27 if (!vps->num_direct_ref_layers[1])
716 2 vps->poc_lsb_not_present = get_bits1(gb) << 1;
717
718 27 sub_layer_flag_info_present_flag = get_bits1(gb);
719
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 27 times.
54 for (int j = 0; j < FFMAX(max_sub_layers[0], max_sub_layers[1]); j++) {
720 27 int sub_layer_dpb_info_present_flag = 1;
721
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
27 if (j > 0 && sub_layer_flag_info_present_flag)
722 sub_layer_dpb_info_present_flag = get_bits1(gb);
723
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (sub_layer_dpb_info_present_flag) {
724
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 27 times.
79 for (int k = 0; k < av_popcount64(vps->ols[1]); k++)
725 52 vps->dpb_size.max_dec_pic_buffering = get_ue_golomb_long(gb) + 1;
726 27 vps->dpb_size.max_num_reorder_pics = get_ue_golomb_long(gb);
727 27 vps->dpb_size.max_latency_increase = get_ue_golomb_long(gb) - 1;
728 }
729 }
730
731 27 direct_dep_type_len = get_ue_golomb_31(gb) + 2;
732
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (direct_dep_type_len > 32) {
733 av_log(avctx, AV_LOG_ERROR, "Invalid direct_dep_type_len: %d\n",
734 direct_dep_type_len);
735 return AVERROR_INVALIDDATA;
736 }
737
738 /* direct_depenency_all_layers_flag */
739
2/2
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 2 times.
27 if (get_bits1(gb)) {
740 25 direct_dep_type = get_bits_long(gb, direct_dep_type_len);
741
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (direct_dep_type > HEVC_DEP_TYPE_BOTH) {
742 av_log(avctx, AV_LOG_WARNING, "Unsupported direct_dep_type: %d\n",
743 direct_dep_type);
744 return AVERROR_PATCHWELCOME;
745 }
746 }
747
748 27 non_vui_extension_length = get_ue_golomb(gb);
749
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (non_vui_extension_length > 4096) {
750 av_log(avctx, AV_LOG_ERROR, "vps_non_vui_extension_length too large: %u\n",
751 non_vui_extension_length);
752 return AVERROR_INVALIDDATA;
753 }
754 27 skip_bits_long(gb, non_vui_extension_length * 8);
755
756
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
27 if (get_bits1(gb)) // vps_vui_present_flag
757 av_log(avctx, AV_LOG_WARNING, "VPS VUI not supported\n");
758
759 27 return 0;
760 }
761
762 1226 int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx,
763 HEVCParamSets *ps)
764 {
765 int i;
766 1226 int vps_id = get_bits(gb, 4);
767 1226 ptrdiff_t nal_size = get_bits_bytesize(gb, 1);
768 1226 int ret = AVERROR_INVALIDDATA;
769 1226 uint64_t layer1_id_included = 0;
770 unsigned vps_base_layer_internal_flag, vps_base_layer_available_flag;
771 HEVCVPS *vps;
772
773
2/2
✓ Branch 0 taken 483 times.
✓ Branch 1 taken 743 times.
1226 if (ps->vps_list[vps_id]) {
774 483 const HEVCVPS *vps1 = ps->vps_list[vps_id];
775
2/2
✓ Branch 0 taken 477 times.
✓ Branch 1 taken 6 times.
483 if (vps1->data_size == nal_size &&
776
2/2
✓ Branch 0 taken 474 times.
✓ Branch 1 taken 3 times.
477 !memcmp(vps1->data, gb->buffer, vps1->data_size))
777 474 return 0;
778 }
779
780 752 vps = av_refstruct_alloc_ext(sizeof(*vps), 0, NULL, hevc_vps_free);
781
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 752 times.
752 if (!vps)
782 return AVERROR(ENOMEM);
783
784 752 av_log(avctx, AV_LOG_DEBUG, "Decoding VPS\n");
785
786 752 vps->data_size = nal_size;
787 752 vps->data = av_memdup(gb->buffer, nal_size);
788
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 752 times.
752 if (!vps->data) {
789 ret = AVERROR(ENOMEM);
790 goto err;
791 }
792 752 vps->vps_id = vps_id;
793
794 752 vps_base_layer_internal_flag = get_bits1(gb);
795 752 vps_base_layer_available_flag = get_bits1(gb);
796
2/4
✓ Branch 0 taken 752 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 752 times.
752 if (!vps_base_layer_internal_flag || !vps_base_layer_available_flag) {
797 av_log(avctx, AV_LOG_ERROR,
798 "vps_base_layer_internal_flag or vps_base_layer_available_flag not set\n");
799 ret = AVERROR_PATCHWELCOME;
800 goto err;
801 }
802
803 752 vps->vps_max_layers = get_bits(gb, 6) + 1;
804 752 vps->vps_max_sub_layers = get_bits(gb, 3) + 1;
805 752 vps->vps_temporal_id_nesting_flag = get_bits1(gb);
806
807
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 752 times.
752 if (get_bits(gb, 16) != 0xffff) { // vps_reserved_ffff_16bits
808 av_log(avctx, AV_LOG_ERROR, "vps_reserved_ffff_16bits is not 0xffff\n");
809 goto err;
810 }
811
812
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 752 times.
752 if (vps->vps_max_sub_layers > HEVC_MAX_SUB_LAYERS) {
813 av_log(avctx, AV_LOG_ERROR, "vps_max_sub_layers out of range: %d\n",
814 vps->vps_max_sub_layers);
815 goto err;
816 }
817
818
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 752 times.
752 if (parse_ptl(gb, avctx, 1, &vps->ptl, vps->vps_max_sub_layers) < 0)
819 goto err;
820
821 752 vps->vps_sub_layer_ordering_info_present_flag = get_bits1(gb);
822
823
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 667 times.
752 i = vps->vps_sub_layer_ordering_info_present_flag ? 0 : vps->vps_max_sub_layers - 1;
824
2/2
✓ Branch 0 taken 810 times.
✓ Branch 1 taken 752 times.
1562 for (; i < vps->vps_max_sub_layers; i++) {
825 810 vps->vps_max_dec_pic_buffering[i] = get_ue_golomb_long(gb) + 1;
826 810 vps->vps_num_reorder_pics[i] = get_ue_golomb_long(gb);
827 810 vps->vps_max_latency_increase[i] = get_ue_golomb_long(gb) - 1;
828
829
2/4
✓ Branch 0 taken 810 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 810 times.
810 if (vps->vps_max_dec_pic_buffering[i] > HEVC_MAX_DPB_SIZE || !vps->vps_max_dec_pic_buffering[i]) {
830 av_log(avctx, AV_LOG_ERROR, "vps_max_dec_pic_buffering_minus1 out of range: %d\n",
831 vps->vps_max_dec_pic_buffering[i] - 1);
832 goto err;
833 }
834
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 810 times.
810 if (vps->vps_num_reorder_pics[i] > vps->vps_max_dec_pic_buffering[i] - 1) {
835 av_log(avctx, AV_LOG_WARNING, "vps_max_num_reorder_pics out of range: %d\n",
836 vps->vps_num_reorder_pics[i]);
837 if (avctx->err_recognition & AV_EF_EXPLODE)
838 goto err;
839 }
840 }
841
842 752 vps->vps_max_layer_id = get_bits(gb, 6);
843 752 vps->vps_num_layer_sets = get_ue_golomb_long(gb) + 1;
844
2/4
✓ Branch 0 taken 752 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 752 times.
✗ Branch 3 not taken.
752 if (vps->vps_num_layer_sets < 1 || vps->vps_num_layer_sets > 1024 ||
845
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 752 times.
752 (vps->vps_num_layer_sets - 1LL) * (vps->vps_max_layer_id + 1LL) > get_bits_left(gb)) {
846 av_log(avctx, AV_LOG_ERROR, "too many layer_id_included_flags\n");
847 goto err;
848 }
849
850 752 vps->num_output_layer_sets = 1;
851 752 vps->ols[0] = 1;
852
853 // we support at most 2 layers, so ignore the others
854
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 727 times.
752 if (vps->vps_num_layer_sets > 1)
855 25 layer1_id_included = get_bits64(gb, vps->vps_max_layer_id + 1); // layer_id_included_flag
856
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 752 times.
752 if (vps->vps_num_layer_sets > 2)
857 skip_bits_long(gb, (vps->vps_num_layer_sets - 2) * (vps->vps_max_layer_id + 1));
858
859 752 vps->vps_timing_info_present_flag = get_bits1(gb);
860
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 730 times.
752 if (vps->vps_timing_info_present_flag) {
861 22 vps->vps_num_units_in_tick = get_bits_long(gb, 32);
862 22 vps->vps_time_scale = get_bits_long(gb, 32);
863 22 vps->vps_poc_proportional_to_timing_flag = get_bits1(gb);
864
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (vps->vps_poc_proportional_to_timing_flag)
865 22 vps->vps_num_ticks_poc_diff_one = get_ue_golomb_long(gb) + 1;
866 22 vps->vps_num_hrd_parameters = get_ue_golomb_long(gb);
867
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (vps->vps_num_hrd_parameters > (unsigned)vps->vps_num_layer_sets) {
868 av_log(avctx, AV_LOG_ERROR,
869 "vps_num_hrd_parameters %d is invalid\n", vps->vps_num_hrd_parameters);
870 goto err;
871 }
872
873
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 19 times.
22 if (vps->vps_num_hrd_parameters) {
874 3 vps->hdr = av_calloc(vps->vps_num_hrd_parameters, sizeof(*vps->hdr));
875
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!vps->hdr)
876 goto err;
877 }
878
879
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 22 times.
25 for (i = 0; i < vps->vps_num_hrd_parameters; i++) {
880 3 int common_inf_present = 1;
881
882 3 get_ue_golomb_long(gb); // hrd_layer_set_idx
883
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (i)
884 common_inf_present = get_bits1(gb);
885 3 decode_hrd(gb, common_inf_present, &vps->hdr[i],
886 3 vps->vps_max_sub_layers);
887 }
888 }
889
890 752 vps->nb_layers = 1;
891 752 vps->layer_idx[0] = 0;
892
2/2
✓ Branch 0 taken 46624 times.
✓ Branch 1 taken 752 times.
47376 for (int i = 1; i < FF_ARRAY_ELEMS(vps->layer_idx); i++)
893 46624 vps->layer_idx[i] = -1;
894
895
3/4
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 725 times.
✓ Branch 3 taken 27 times.
✗ Branch 4 not taken.
752 if (vps->vps_max_layers > 1 && get_bits1(gb)) { /* vps_extension_flag */
896 27 int ret = decode_vps_ext(gb, avctx, vps, layer1_id_included);
897
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret == AVERROR_PATCHWELCOME) {
898 vps->nb_layers = 1;
899 av_log(avctx, AV_LOG_WARNING, "Ignoring unsupported VPS extension\n");
900 ret = 0;
901
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 } else if (ret < 0)
902 goto err;
903 }
904
905
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 752 times.
752 if (get_bits_left(gb) < 0) {
906 av_log(avctx, AV_LOG_ERROR,
907 "Overread VPS by %d bits\n", -get_bits_left(gb));
908 if (ps->vps_list[vps_id])
909 goto err;
910 }
911
912 752 remove_vps(ps, vps_id);
913 752 ps->vps_list[vps_id] = vps;
914
915 752 return 0;
916
917 err:
918 av_refstruct_unref(&vps);
919 return ret;
920 }
921
922 332 static void decode_vui(GetBitContext *gb, AVCodecContext *avctx,
923 int apply_defdispwin, HEVCSPS *sps)
924 {
925 332 VUI backup_vui, *vui = &sps->vui;
926 GetBitContext backup;
927 332 int alt = 0;
928
929 332 ff_h2645_decode_common_vui_params(gb, &sps->vui.common, avctx);
930
931
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 230 times.
332 if (vui->common.video_signal_type_present_flag) {
932
4/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 87 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 8 times.
102 if (vui->common.video_full_range_flag && sps->pix_fmt == AV_PIX_FMT_YUV420P)
933 7 sps->pix_fmt = AV_PIX_FMT_YUVJ420P;
934
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 12 times.
102 if (vui->common.colour_description_present_flag) {
935
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
90 if (vui->common.matrix_coeffs == AVCOL_SPC_RGB) {
936 switch (sps->pix_fmt) {
937 case AV_PIX_FMT_YUV444P:
938 sps->pix_fmt = AV_PIX_FMT_GBRP;
939 break;
940 case AV_PIX_FMT_YUV444P10:
941 sps->pix_fmt = AV_PIX_FMT_GBRP10;
942 break;
943 case AV_PIX_FMT_YUV444P12:
944 sps->pix_fmt = AV_PIX_FMT_GBRP12;
945 break;
946 }
947 }
948 }
949 }
950
951 332 vui->neutra_chroma_indication_flag = get_bits1(gb);
952 332 vui->field_seq_flag = get_bits1(gb);
953 332 vui->frame_field_info_present_flag = get_bits1(gb);
954
955 // Backup context in case an alternate header is detected
956 332 memcpy(&backup, gb, sizeof(backup));
957 332 memcpy(&backup_vui, vui, sizeof(backup_vui));
958
3/4
✓ Branch 1 taken 125 times.
✓ Branch 2 taken 207 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 125 times.
332 if (get_bits_left(gb) >= 68 && show_bits(gb, 21) == 0x100000) {
959 vui->default_display_window_flag = 0;
960 av_log(avctx, AV_LOG_WARNING, "Invalid default display window\n");
961 } else
962 332 vui->default_display_window_flag = get_bits1(gb);
963
964
2/2
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 114 times.
332 if (vui->default_display_window_flag) {
965 114 int vert_mult = hevc_sub_height_c[sps->chroma_format_idc];
966 114 int horiz_mult = hevc_sub_width_c[sps->chroma_format_idc];
967 114 vui->def_disp_win.left_offset = get_ue_golomb_long(gb) * horiz_mult;
968 114 vui->def_disp_win.right_offset = get_ue_golomb_long(gb) * horiz_mult;
969 114 vui->def_disp_win.top_offset = get_ue_golomb_long(gb) * vert_mult;
970 114 vui->def_disp_win.bottom_offset = get_ue_golomb_long(gb) * vert_mult;
971
972
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 54 times.
114 if (apply_defdispwin &&
973
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
54 avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {
974 av_log(avctx, AV_LOG_DEBUG,
975 "discarding vui default display window, "
976 "original values are l:%u r:%u t:%u b:%u\n",
977 vui->def_disp_win.left_offset,
978 vui->def_disp_win.right_offset,
979 vui->def_disp_win.top_offset,
980 vui->def_disp_win.bottom_offset);
981
982 vui->def_disp_win.left_offset =
983 vui->def_disp_win.right_offset =
984 vui->def_disp_win.top_offset =
985 vui->def_disp_win.bottom_offset = 0;
986 }
987 }
988
989 332 timing_info:
990 332 vui->vui_timing_info_present_flag = get_bits1(gb);
991
992
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 207 times.
332 if (vui->vui_timing_info_present_flag) {
993
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 125 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
125 if( get_bits_left(gb) < 66 && !alt) {
994 // The alternate syntax seem to have timing info located
995 // at where def_disp_win is normally located
996 av_log(avctx, AV_LOG_WARNING,
997 "Strange VUI timing information, retrying...\n");
998 memcpy(vui, &backup_vui, sizeof(backup_vui));
999 memcpy(gb, &backup, sizeof(backup));
1000 alt = 1;
1001 goto timing_info;
1002 }
1003 125 vui->vui_num_units_in_tick = get_bits_long(gb, 32);
1004 125 vui->vui_time_scale = get_bits_long(gb, 32);
1005
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if (alt) {
1006 av_log(avctx, AV_LOG_INFO, "Retry got %"PRIu32"/%"PRIu32" fps\n",
1007 vui->vui_time_scale, vui->vui_num_units_in_tick);
1008 }
1009 125 vui->vui_poc_proportional_to_timing_flag = get_bits1(gb);
1010
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 87 times.
125 if (vui->vui_poc_proportional_to_timing_flag)
1011 38 vui->vui_num_ticks_poc_diff_one_minus1 = get_ue_golomb_long(gb);
1012 125 vui->vui_hrd_parameters_present_flag = get_bits1(gb);
1013
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 67 times.
125 if (vui->vui_hrd_parameters_present_flag)
1014 58 decode_hrd(gb, 1, &sps->hdr, sps->max_sub_layers);
1015 }
1016
1017 332 vui->bitstream_restriction_flag = get_bits1(gb);
1018
2/2
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 202 times.
332 if (vui->bitstream_restriction_flag) {
1019
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 130 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
130 if (get_bits_left(gb) < 8 && !alt) {
1020 av_log(avctx, AV_LOG_WARNING,
1021 "Strange VUI bitstream restriction information, retrying"
1022 " from timing information...\n");
1023 memcpy(vui, &backup_vui, sizeof(backup_vui));
1024 memcpy(gb, &backup, sizeof(backup));
1025 alt = 1;
1026 goto timing_info;
1027 }
1028 130 vui->tiles_fixed_structure_flag = get_bits1(gb);
1029 130 vui->motion_vectors_over_pic_boundaries_flag = get_bits1(gb);
1030 130 vui->restricted_ref_pic_lists_flag = get_bits1(gb);
1031 130 vui->min_spatial_segmentation_idc = get_ue_golomb_long(gb);
1032 130 vui->max_bytes_per_pic_denom = get_ue_golomb_long(gb);
1033 130 vui->max_bits_per_min_cu_denom = get_ue_golomb_long(gb);
1034 130 vui->log2_max_mv_length_horizontal = get_ue_golomb_long(gb);
1035 130 vui->log2_max_mv_length_vertical = get_ue_golomb_long(gb);
1036 }
1037
1038
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 332 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
332 if (get_bits_left(gb) < 1 && !alt) {
1039 // XXX: Alternate syntax when sps_range_extension_flag != 0?
1040 av_log(avctx, AV_LOG_WARNING,
1041 "Overread in VUI, retrying from timing information...\n");
1042 memcpy(vui, &backup_vui, sizeof(backup_vui));
1043 memcpy(gb, &backup, sizeof(backup));
1044 alt = 1;
1045 goto timing_info;
1046 }
1047 332 }
1048
1049 204 static void set_default_scaling_list_data(ScalingList *sl)
1050 {
1051 int matrixId;
1052
1053
2/2
✓ Branch 0 taken 1224 times.
✓ Branch 1 taken 204 times.
1428 for (matrixId = 0; matrixId < 6; matrixId++) {
1054 // 4x4 default is 16
1055 1224 memset(sl->sl[0][matrixId], 16, 16);
1056 1224 sl->sl_dc[0][matrixId] = 16; // default for 16x16
1057 1224 sl->sl_dc[1][matrixId] = 16; // default for 32x32
1058 }
1059 204 memcpy(sl->sl[1][0], default_scaling_list_intra, 64);
1060 204 memcpy(sl->sl[1][1], default_scaling_list_intra, 64);
1061 204 memcpy(sl->sl[1][2], default_scaling_list_intra, 64);
1062 204 memcpy(sl->sl[1][3], default_scaling_list_inter, 64);
1063 204 memcpy(sl->sl[1][4], default_scaling_list_inter, 64);
1064 204 memcpy(sl->sl[1][5], default_scaling_list_inter, 64);
1065 204 memcpy(sl->sl[2][0], default_scaling_list_intra, 64);
1066 204 memcpy(sl->sl[2][1], default_scaling_list_intra, 64);
1067 204 memcpy(sl->sl[2][2], default_scaling_list_intra, 64);
1068 204 memcpy(sl->sl[2][3], default_scaling_list_inter, 64);
1069 204 memcpy(sl->sl[2][4], default_scaling_list_inter, 64);
1070 204 memcpy(sl->sl[2][5], default_scaling_list_inter, 64);
1071 204 memcpy(sl->sl[3][0], default_scaling_list_intra, 64);
1072 204 memcpy(sl->sl[3][1], default_scaling_list_intra, 64);
1073 204 memcpy(sl->sl[3][2], default_scaling_list_intra, 64);
1074 204 memcpy(sl->sl[3][3], default_scaling_list_inter, 64);
1075 204 memcpy(sl->sl[3][4], default_scaling_list_inter, 64);
1076 204 memcpy(sl->sl[3][5], default_scaling_list_inter, 64);
1077 204 }
1078
1079 165 static int scaling_list_data(GetBitContext *gb, AVCodecContext *avctx,
1080 ScalingList *sl, const HEVCSPS *sps)
1081 {
1082 uint8_t scaling_list_pred_mode_flag;
1083 uint8_t scaling_list_dc_coef[2][6];
1084 int size_id, matrix_id, pos;
1085 int i;
1086
1087
2/2
✓ Branch 0 taken 660 times.
✓ Branch 1 taken 165 times.
825 for (size_id = 0; size_id < 4; size_id++)
1088
4/4
✓ Branch 0 taken 330 times.
✓ Branch 1 taken 2970 times.
✓ Branch 2 taken 3300 times.
✓ Branch 3 taken 660 times.
3960 for (matrix_id = 0; matrix_id < 6; matrix_id += ((size_id == 3) ? 3 : 1)) {
1089 3300 scaling_list_pred_mode_flag = get_bits1(gb);
1090
2/2
✓ Branch 0 taken 979 times.
✓ Branch 1 taken 2321 times.
3300 if (!scaling_list_pred_mode_flag) {
1091 979 unsigned int delta = get_ue_golomb_long(gb);
1092 /* Only need to handle non-zero delta. Zero means default,
1093 * which should already be in the arrays. */
1094
2/2
✓ Branch 0 taken 766 times.
✓ Branch 1 taken 213 times.
979 if (delta) {
1095 // Copy from previous array.
1096
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 766 times.
766 delta *= (size_id == 3) ? 3 : 1;
1097
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 766 times.
766 if (matrix_id < delta) {
1098 av_log(avctx, AV_LOG_ERROR,
1099 "Invalid delta in scaling list data: %d.\n", delta);
1100 return AVERROR_INVALIDDATA;
1101 }
1102
1103
2/2
✓ Branch 0 taken 482 times.
✓ Branch 1 taken 284 times.
766 memcpy(sl->sl[size_id][matrix_id],
1104 766 sl->sl[size_id][matrix_id - delta],
1105 size_id > 0 ? 64 : 16);
1106
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 616 times.
766 if (size_id > 1)
1107 150 sl->sl_dc[size_id - 2][matrix_id] = sl->sl_dc[size_id - 2][matrix_id - delta];
1108 }
1109 } else {
1110 int next_coef, coef_num;
1111 int32_t scaling_list_delta_coef;
1112
1113 2321 next_coef = 8;
1114 2321 coef_num = FFMIN(64, 1 << (4 + (size_id << 1)));
1115
2/2
✓ Branch 0 taken 1091 times.
✓ Branch 1 taken 1230 times.
2321 if (size_id > 1) {
1116 1091 int scaling_list_coeff_minus8 = get_se_golomb(gb);
1117
2/4
✓ Branch 0 taken 1091 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1091 times.
1091 if (scaling_list_coeff_minus8 < -7 ||
1118 scaling_list_coeff_minus8 > 247)
1119 return AVERROR_INVALIDDATA;
1120 1091 scaling_list_dc_coef[size_id - 2][matrix_id] = scaling_list_coeff_minus8 + 8;
1121 1091 next_coef = scaling_list_dc_coef[size_id - 2][matrix_id];
1122 1091 sl->sl_dc[size_id - 2][matrix_id] = next_coef;
1123 }
1124
2/2
✓ Branch 0 taken 116480 times.
✓ Branch 1 taken 2321 times.
118801 for (i = 0; i < coef_num; i++) {
1125
2/2
✓ Branch 0 taken 10688 times.
✓ Branch 1 taken 105792 times.
116480 if (size_id == 0)
1126 10688 pos = 4 * ff_hevc_diag_scan4x4_y[i] +
1127 10688 ff_hevc_diag_scan4x4_x[i];
1128 else
1129 105792 pos = 8 * ff_hevc_diag_scan8x8_y[i] +
1130 105792 ff_hevc_diag_scan8x8_x[i];
1131
1132 116480 scaling_list_delta_coef = get_se_golomb(gb);
1133 116480 next_coef = (next_coef + 256U + scaling_list_delta_coef) % 256;
1134 116480 sl->sl[size_id][matrix_id][pos] = next_coef;
1135 }
1136 }
1137 }
1138
1139
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 122 times.
165 if (sps->chroma_format_idc == 3) {
1140
2/2
✓ Branch 0 taken 2752 times.
✓ Branch 1 taken 43 times.
2795 for (i = 0; i < 64; i++) {
1141 2752 sl->sl[3][1][i] = sl->sl[2][1][i];
1142 2752 sl->sl[3][2][i] = sl->sl[2][2][i];
1143 2752 sl->sl[3][4][i] = sl->sl[2][4][i];
1144 2752 sl->sl[3][5][i] = sl->sl[2][5][i];
1145 }
1146 43 sl->sl_dc[1][1] = sl->sl_dc[0][1];
1147 43 sl->sl_dc[1][2] = sl->sl_dc[0][2];
1148 43 sl->sl_dc[1][4] = sl->sl_dc[0][4];
1149 43 sl->sl_dc[1][5] = sl->sl_dc[0][5];
1150 }
1151
1152
1153 165 return 0;
1154 }
1155
1156 1253 static int map_pixel_format(AVCodecContext *avctx, HEVCSPS *sps)
1157 {
1158 const AVPixFmtDescriptor *desc;
1159
3/5
✓ Branch 0 taken 1080 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 150 times.
✓ Branch 3 taken 23 times.
✗ Branch 4 not taken.
1253 switch (sps->bit_depth) {
1160 1080 case 8:
1161
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1076 times.
1080 if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY8;
1162
2/2
✓ Branch 0 taken 1067 times.
✓ Branch 1 taken 13 times.
1080 if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P;
1163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1080 times.
1080 if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P;
1164
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1071 times.
1080 if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P;
1165 1080 break;
1166 case 9:
1167 if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY9;
1168 if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P9;
1169 if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P9;
1170 if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P9;
1171 break;
1172 150 case 10:
1173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY10;
1174
2/2
✓ Branch 0 taken 121 times.
✓ Branch 1 taken 29 times.
150 if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P10;
1175
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 121 times.
150 if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P10;
1176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P10;
1177 150 break;
1178 23 case 12:
1179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY12;
1180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P12;
1181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P12;
1182
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P12;
1183 23 break;
1184 default:
1185 av_log(avctx, AV_LOG_ERROR,
1186 "The following bit-depths are currently specified: 8, 9, 10 and 12 bits, "
1187 "chroma_format_idc is %d, depth is %d\n",
1188 sps->chroma_format_idc, sps->bit_depth);
1189 return AVERROR_INVALIDDATA;
1190 }
1191
1192 1253 desc = av_pix_fmt_desc_get(sps->pix_fmt);
1193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (!desc)
1194 return AVERROR(EINVAL);
1195
1196 1253 sps->hshift[0] = sps->vshift[0] = 0;
1197 1253 sps->hshift[2] = sps->hshift[1] = desc->log2_chroma_w;
1198 1253 sps->vshift[2] = sps->vshift[1] = desc->log2_chroma_h;
1199
1200 1253 sps->pixel_shift = sps->bit_depth > 8;
1201
1202 1253 return 0;
1203 }
1204
1205 1253 int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id,
1206 unsigned nuh_layer_id, int apply_defdispwin,
1207 const HEVCVPS * const *vps_list, AVCodecContext *avctx)
1208 {
1209 HEVCWindow *ow;
1210 1253 int ret = 0;
1211 int bit_depth_chroma, num_comps, multi_layer_ext;
1212 int vps_max_sub_layers;
1213 int i;
1214
1215 // Coded parameters
1216
1217 1253 sps->vps_id = get_bits(gb, 4);
1218
1219
1/2
✓ Branch 0 taken 1253 times.
✗ Branch 1 not taken.
1253 if (vps_list) {
1220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (!vps_list[sps->vps_id]) {
1221 av_log(avctx, AV_LOG_ERROR, "VPS %d does not exist\n",
1222 sps->vps_id);
1223 return AVERROR_INVALIDDATA;
1224 }
1225 1253 sps->vps = av_refstruct_ref_c(vps_list[sps->vps_id]);
1226 }
1227
1228 1253 sps->max_sub_layers = get_bits(gb, 3) + 1;
1229
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1223 times.
1283 multi_layer_ext = nuh_layer_id > 0 &&
1230
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 2 times.
30 sps->max_sub_layers == HEVC_MAX_SUB_LAYERS + 1;
1231
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1225 times.
1253 if (multi_layer_ext) {
1232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (!sps->vps)
1233 return AVERROR(EINVAL);
1234
1235 28 sps->max_sub_layers = sps->vps->vps_max_sub_layers;
1236 }
1237 3759 vps_max_sub_layers = sps->vps ? sps->vps->vps_max_sub_layers
1238
1/2
✓ Branch 0 taken 1253 times.
✗ Branch 1 not taken.
1253 : FFMIN(sps->max_sub_layers, HEVC_MAX_SUB_LAYERS);
1239
1240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (sps->max_sub_layers > vps_max_sub_layers) {
1241 av_log(avctx, AV_LOG_ERROR, "sps_max_sub_layers out of range: %d\n",
1242 sps->max_sub_layers);
1243 return AVERROR_INVALIDDATA;
1244 }
1245
1246
2/2
✓ Branch 0 taken 1225 times.
✓ Branch 1 taken 28 times.
1253 if (!multi_layer_ext) {
1247 1225 sps->temporal_id_nesting = get_bits(gb, 1);
1248
1249
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1225 times.
1225 if ((ret = parse_ptl(gb, avctx, 1, &sps->ptl, sps->max_sub_layers)) < 0)
1250 return ret;
1251 } else {
1252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 sps->temporal_id_nesting = sps->max_sub_layers > 1 ?
1253 sps->vps->vps_max_sub_layers : 1;
1254 }
1255
1256 1253 *sps_id = get_ue_golomb_long(gb);
1257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (*sps_id >= HEVC_MAX_SPS_COUNT) {
1258 av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", *sps_id);
1259 return AVERROR_INVALIDDATA;
1260 }
1261
1262
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1225 times.
1253 if (multi_layer_ext) {
1263 28 const RepFormat *rf = &sps->vps->rep_format;
1264
1265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (sps->vps->nb_layers == 1) {
1266 av_log(avctx, AV_LOG_WARNING, "SPS %d references an unsupported VPS extension. Ignoring\n",
1267 *sps_id);
1268 return AVERROR(ENOSYS);
1269 }
1270
1271
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
28 if (get_bits1(gb) && // update_rep_format_flag
1272 get_bits(gb, 8)) { // sps_rep_format_idx
1273 av_log(avctx, AV_LOG_ERROR, "sps_rep_format_idx!=0\n");
1274 return AVERROR_PATCHWELCOME;
1275 }
1276
1277 28 sps->separate_colour_plane = rf->separate_colour_plane_flag;
1278
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 sps->chroma_format_idc = sps->separate_colour_plane ? 0 :
1279 28 rf->chroma_format_idc;
1280 28 sps->bit_depth = rf->bit_depth_luma;
1281 28 sps->width = rf->pic_width_in_luma_samples;
1282 28 sps->height = rf->pic_height_in_luma_samples;
1283
1284 28 sps->pic_conf_win.left_offset = rf->conf_win_left_offset;
1285 28 sps->pic_conf_win.right_offset = rf->conf_win_right_offset;
1286 28 sps->pic_conf_win.top_offset = rf->conf_win_top_offset;
1287 28 sps->pic_conf_win.bottom_offset = rf->conf_win_bottom_offset;
1288
1289 } else {
1290 1225 sps->chroma_format_idc = get_ue_golomb_long(gb);
1291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1225 times.
1225 if (sps->chroma_format_idc > 3U) {
1292 av_log(avctx, AV_LOG_ERROR, "chroma_format_idc %d is invalid\n", sps->chroma_format_idc);
1293 return AVERROR_INVALIDDATA;
1294 }
1295
1296
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1193 times.
1225 if (sps->chroma_format_idc == 3)
1297 32 sps->separate_colour_plane = get_bits1(gb);
1298
1299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1225 times.
1225 if (sps->separate_colour_plane)
1300 sps->chroma_format_idc = 0;
1301
1302 1225 sps->width = get_ue_golomb_long(gb);
1303 1225 sps->height = get_ue_golomb_long(gb);
1304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1225 times.
1225 if ((ret = av_image_check_size(sps->width,
1305 1225 sps->height, 0, avctx)) < 0)
1306 return ret;
1307
1308 1225 sps->conformance_window = get_bits1(gb);
1309
2/2
✓ Branch 0 taken 1012 times.
✓ Branch 1 taken 213 times.
1225 if (sps->conformance_window) {
1310 1012 int vert_mult = hevc_sub_height_c[sps->chroma_format_idc];
1311 1012 int horiz_mult = hevc_sub_width_c[sps->chroma_format_idc];
1312 1012 sps->pic_conf_win.left_offset = get_ue_golomb_long(gb) * horiz_mult;
1313 1012 sps->pic_conf_win.right_offset = get_ue_golomb_long(gb) * horiz_mult;
1314 1012 sps->pic_conf_win.top_offset = get_ue_golomb_long(gb) * vert_mult;
1315 1012 sps->pic_conf_win.bottom_offset = get_ue_golomb_long(gb) * vert_mult;
1316
1317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1012 times.
1012 if (avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {
1318 av_log(avctx, AV_LOG_DEBUG,
1319 "discarding sps conformance window, "
1320 "original values are l:%u r:%u t:%u b:%u\n",
1321 sps->pic_conf_win.left_offset,
1322 sps->pic_conf_win.right_offset,
1323 sps->pic_conf_win.top_offset,
1324 sps->pic_conf_win.bottom_offset);
1325
1326 sps->pic_conf_win.left_offset =
1327 sps->pic_conf_win.right_offset =
1328 sps->pic_conf_win.top_offset =
1329 sps->pic_conf_win.bottom_offset = 0;
1330 }
1331 }
1332
1333 1225 sps->bit_depth = get_ue_golomb_31(gb) + 8;
1334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1225 times.
1225 if (sps->bit_depth > 16) {
1335 av_log(avctx, AV_LOG_ERROR, "Luma bit depth (%d) is out of range\n",
1336 sps->bit_depth);
1337 return AVERROR_INVALIDDATA;
1338 }
1339 1225 bit_depth_chroma = get_ue_golomb_31(gb) + 8;
1340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1225 times.
1225 if (bit_depth_chroma > 16) {
1341 av_log(avctx, AV_LOG_ERROR, "Chroma bit depth (%d) is out of range\n",
1342 bit_depth_chroma);
1343 return AVERROR_INVALIDDATA;
1344 }
1345
3/4
✓ Branch 0 taken 1221 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1221 times.
1225 if (sps->chroma_format_idc && bit_depth_chroma != sps->bit_depth) {
1346 av_log(avctx, AV_LOG_ERROR,
1347 "Luma bit depth (%d) is different from chroma bit depth (%d), "
1348 "this is unsupported.\n",
1349 sps->bit_depth, bit_depth_chroma);
1350 return AVERROR_INVALIDDATA;
1351 }
1352 1225 sps->bit_depth_chroma = bit_depth_chroma;
1353 }
1354
1355 1253 sps->output_window = sps->pic_conf_win;
1356
1357 1253 ret = map_pixel_format(avctx, sps);
1358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (ret < 0)
1359 return ret;
1360
1361 1253 sps->log2_max_poc_lsb = get_ue_golomb_long(gb) + 4;
1362
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (sps->log2_max_poc_lsb > 16) {
1363 av_log(avctx, AV_LOG_ERROR, "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n",
1364 sps->log2_max_poc_lsb - 4);
1365 return AVERROR_INVALIDDATA;
1366 }
1367
1368
2/2
✓ Branch 0 taken 1225 times.
✓ Branch 1 taken 28 times.
1253 if (!multi_layer_ext) {
1369 int start;
1370
1371 1225 sps->sublayer_ordering_info = get_bits1(gb);
1372
2/2
✓ Branch 0 taken 161 times.
✓ Branch 1 taken 1064 times.
1225 start = sps->sublayer_ordering_info ? 0 : sps->max_sub_layers - 1;
1373
2/2
✓ Branch 0 taken 1372 times.
✓ Branch 1 taken 1225 times.
2597 for (i = start; i < sps->max_sub_layers; i++) {
1374 1372 sps->temporal_layer[i].max_dec_pic_buffering = get_ue_golomb_long(gb) + 1;
1375 1372 sps->temporal_layer[i].num_reorder_pics = get_ue_golomb_long(gb);
1376 1372 sps->temporal_layer[i].max_latency_increase = get_ue_golomb_long(gb) - 1;
1377
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1372 times.
1372 if (sps->temporal_layer[i].max_dec_pic_buffering > (unsigned)HEVC_MAX_DPB_SIZE) {
1378 av_log(avctx, AV_LOG_ERROR, "sps_max_dec_pic_buffering_minus1 out of range: %d\n",
1379 sps->temporal_layer[i].max_dec_pic_buffering - 1U);
1380 return AVERROR_INVALIDDATA;
1381 }
1382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1372 times.
1372 if (sps->temporal_layer[i].num_reorder_pics > sps->temporal_layer[i].max_dec_pic_buffering - 1) {
1383 av_log(avctx, AV_LOG_WARNING, "sps_max_num_reorder_pics out of range: %d\n",
1384 sps->temporal_layer[i].num_reorder_pics);
1385 if (avctx->err_recognition & AV_EF_EXPLODE ||
1386 sps->temporal_layer[i].num_reorder_pics > HEVC_MAX_DPB_SIZE - 1) {
1387 return AVERROR_INVALIDDATA;
1388 }
1389 sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[i].num_reorder_pics + 1;
1390 }
1391 }
1392
1393
2/2
✓ Branch 0 taken 161 times.
✓ Branch 1 taken 1064 times.
1225 if (!sps->sublayer_ordering_info) {
1394
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 161 times.
243 for (i = 0; i < start; i++) {
1395 82 sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[start].max_dec_pic_buffering;
1396 82 sps->temporal_layer[i].num_reorder_pics = sps->temporal_layer[start].num_reorder_pics;
1397 82 sps->temporal_layer[i].max_latency_increase = sps->temporal_layer[start].max_latency_increase;
1398 }
1399 }
1400 } else {
1401
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
56 for (int i = 0; i < sps->max_sub_layers; i++) {
1402 28 sps->temporal_layer[i].max_dec_pic_buffering = sps->vps->dpb_size.max_dec_pic_buffering;
1403 28 sps->temporal_layer[i].num_reorder_pics = sps->vps->dpb_size.max_num_reorder_pics;
1404 28 sps->temporal_layer[i].max_latency_increase = sps->vps->dpb_size.max_latency_increase;
1405 }
1406 }
1407
1408 1253 sps->log2_min_cb_size = get_ue_golomb_long(gb) + 3;
1409 1253 sps->log2_diff_max_min_coding_block_size = get_ue_golomb_long(gb);
1410 1253 sps->log2_min_tb_size = get_ue_golomb_long(gb) + 2;
1411 1253 sps->log2_diff_max_min_transform_block_size = get_ue_golomb_long(gb);
1412 1253 sps->log2_max_trafo_size = sps->log2_diff_max_min_transform_block_size +
1413 1253 sps->log2_min_tb_size;
1414
1415
2/4
✓ Branch 0 taken 1253 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1253 times.
1253 if (sps->log2_min_cb_size < 3 || sps->log2_min_cb_size > 30) {
1416 av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_min_cb_size", sps->log2_min_cb_size);
1417 return AVERROR_INVALIDDATA;
1418 }
1419
1420
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (sps->log2_diff_max_min_coding_block_size > 30) {
1421 av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_diff_max_min_coding_block_size", sps->log2_diff_max_min_coding_block_size);
1422 return AVERROR_INVALIDDATA;
1423 }
1424
1425
2/4
✓ Branch 0 taken 1253 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1253 times.
1253 if (sps->log2_min_tb_size >= sps->log2_min_cb_size || sps->log2_min_tb_size < 2) {
1426 av_log(avctx, AV_LOG_ERROR, "Invalid value for log2_min_tb_size");
1427 return AVERROR_INVALIDDATA;
1428 }
1429
1430
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (sps->log2_diff_max_min_transform_block_size > 30) {
1431 av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_diff_max_min_transform_block_size",
1432 sps->log2_diff_max_min_transform_block_size);
1433 return AVERROR_INVALIDDATA;
1434 }
1435
1436 1253 sps->max_transform_hierarchy_depth_inter = get_ue_golomb_long(gb);
1437 1253 sps->max_transform_hierarchy_depth_intra = get_ue_golomb_long(gb);
1438
1439 1253 sps->scaling_list_enabled = get_bits1(gb);
1440
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 1180 times.
1253 if (sps->scaling_list_enabled) {
1441 73 set_default_scaling_list_data(&sps->scaling_list);
1442
1443
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 69 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
73 if (multi_layer_ext && get_bits1(gb)) { // sps_infer_scaling_list_flag
1444 av_log(avctx, AV_LOG_ERROR, "sps_infer_scaling_list_flag=1 not supported\n");
1445 return AVERROR_PATCHWELCOME;
1446 }
1447
1448
2/2
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 39 times.
73 if (get_bits1(gb)) {
1449 34 ret = scaling_list_data(gb, avctx, &sps->scaling_list, sps);
1450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (ret < 0)
1451 return ret;
1452 }
1453 }
1454
1455 1253 sps->amp_enabled = get_bits1(gb);
1456 1253 sps->sao_enabled = get_bits1(gb);
1457
1458 1253 sps->pcm_enabled = get_bits1(gb);
1459
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 1178 times.
1253 if (sps->pcm_enabled) {
1460 75 sps->pcm.bit_depth = get_bits(gb, 4) + 1;
1461 75 sps->pcm.bit_depth_chroma = get_bits(gb, 4) + 1;
1462 75 sps->pcm.log2_min_pcm_cb_size = get_ue_golomb_long(gb) + 3;
1463 150 sps->pcm.log2_max_pcm_cb_size = sps->pcm.log2_min_pcm_cb_size +
1464 75 get_ue_golomb_long(gb);
1465
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
75 if (FFMAX(sps->pcm.bit_depth, sps->pcm.bit_depth_chroma) > sps->bit_depth) {
1466 av_log(avctx, AV_LOG_ERROR,
1467 "PCM bit depth (%d, %d) is greater than normal bit depth (%d)\n",
1468 sps->pcm.bit_depth, sps->pcm.bit_depth_chroma, sps->bit_depth);
1469 return AVERROR_INVALIDDATA;
1470 }
1471
1472 75 sps->pcm_loop_filter_disabled = get_bits1(gb);
1473 }
1474
1475 1253 sps->nb_st_rps = get_ue_golomb_long(gb);
1476
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (sps->nb_st_rps > HEVC_MAX_SHORT_TERM_REF_PIC_SETS) {
1477 av_log(avctx, AV_LOG_ERROR, "Too many short term RPS: %d.\n",
1478 sps->nb_st_rps);
1479 return AVERROR_INVALIDDATA;
1480 }
1481
2/2
✓ Branch 0 taken 10352 times.
✓ Branch 1 taken 1253 times.
11605 for (i = 0; i < sps->nb_st_rps; i++) {
1482
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10352 times.
10352 if ((ret = ff_hevc_decode_short_term_rps(gb, avctx, &sps->st_rps[i],
1483 sps, 0)) < 0)
1484 return ret;
1485 }
1486
1487 1253 sps->long_term_ref_pics_present = get_bits1(gb);
1488
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 1148 times.
1253 if (sps->long_term_ref_pics_present) {
1489 105 sps->num_long_term_ref_pics_sps = get_ue_golomb_long(gb);
1490
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 105 times.
105 if (sps->num_long_term_ref_pics_sps > HEVC_MAX_LONG_TERM_REF_PICS) {
1491 av_log(avctx, AV_LOG_ERROR, "Too many long term ref pics: %d.\n",
1492 sps->num_long_term_ref_pics_sps);
1493 return AVERROR_INVALIDDATA;
1494 }
1495
1496 105 sps->used_by_curr_pic_lt = 0;
1497
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 105 times.
169 for (i = 0; i < sps->num_long_term_ref_pics_sps; i++) {
1498 64 sps->lt_ref_pic_poc_lsb_sps[i] = get_bits(gb, sps->log2_max_poc_lsb);
1499 64 sps->used_by_curr_pic_lt |= get_bits1(gb) << i;
1500 }
1501 }
1502
1503 1253 sps->temporal_mvp_enabled = get_bits1(gb);
1504 1253 sps->strong_intra_smoothing_enabled = get_bits1(gb);
1505 1253 sps->vui.common.sar = (AVRational){0, 1};
1506 1253 sps->vui_present = get_bits1(gb);
1507
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 921 times.
1253 if (sps->vui_present)
1508 332 decode_vui(gb, avctx, apply_defdispwin, sps);
1509
1510 1253 sps->extension_present = get_bits1(gb);
1511
2/2
✓ Branch 0 taken 135 times.
✓ Branch 1 taken 1118 times.
1253 if (sps->extension_present) {
1512 135 sps->range_extension = get_bits1(gb);
1513 135 sps->multilayer_extension = get_bits1(gb);
1514 135 sps->sps_3d_extension = get_bits1(gb);
1515 135 sps->scc_extension = get_bits1(gb);
1516 135 skip_bits(gb, 4); // sps_extension_4bits
1517
1518
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 79 times.
135 if (sps->range_extension) {
1519 56 sps->transform_skip_rotation_enabled = get_bits1(gb);
1520 56 sps->transform_skip_context_enabled = get_bits1(gb);
1521 56 sps->implicit_rdpcm_enabled = get_bits1(gb);
1522 56 sps->explicit_rdpcm_enabled = get_bits1(gb);
1523
1524 56 sps->extended_precision_processing = get_bits1(gb);
1525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (sps->extended_precision_processing)
1526 av_log(avctx, AV_LOG_WARNING,
1527 "extended_precision_processing_flag not yet implemented\n");
1528
1529 56 sps->intra_smoothing_disabled = get_bits1(gb);
1530 56 sps->high_precision_offsets_enabled = get_bits1(gb);
1531
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 18 times.
56 if (sps->high_precision_offsets_enabled)
1532 38 av_log(avctx, AV_LOG_WARNING,
1533 "high_precision_offsets_enabled_flag not yet implemented\n");
1534
1535 56 sps->persistent_rice_adaptation_enabled = get_bits1(gb);
1536
1537 56 sps->cabac_bypass_alignment_enabled = get_bits1(gb);
1538
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (sps->cabac_bypass_alignment_enabled)
1539 av_log(avctx, AV_LOG_WARNING,
1540 "cabac_bypass_alignment_enabled_flag not yet implemented\n");
1541 }
1542
1543
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 76 times.
135 if (sps->multilayer_extension) {
1544 59 skip_bits1(gb); // inter_view_mv_vert_constraint_flag
1545 }
1546
1547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 135 times.
135 if (sps->sps_3d_extension) {
1548 for (i = 0; i <= 1; i++) {
1549 skip_bits1(gb); // iv_di_mc_enabled_flag
1550 skip_bits1(gb); // iv_mv_scal_enabled_flag
1551 if (i == 0) {
1552 get_ue_golomb_long(gb); // log2_ivmc_sub_pb_size_minus3
1553 skip_bits1(gb); // iv_res_pred_enabled_flag
1554 skip_bits1(gb); // depth_ref_enabled_flag
1555 skip_bits1(gb); // vsp_mc_enabled_flag
1556 skip_bits1(gb); // dbbp_enabled_flag
1557 } else {
1558 skip_bits1(gb); // tex_mc_enabled_flag
1559 get_ue_golomb_long(gb); // log2_ivmc_sub_pb_size_minus3
1560 skip_bits1(gb); // intra_contour_enabled_flag
1561 skip_bits1(gb); // intra_dc_only_wedge_enabled_flag
1562 skip_bits1(gb); // cqt_cu_part_pred_enabled_flag
1563 skip_bits1(gb); // inter_dc_only_enabled_flag
1564 skip_bits1(gb); // skip_intra_enabled_flag
1565 }
1566 }
1567 av_log(avctx, AV_LOG_WARNING,
1568 "sps_3d_extension_flag not yet implemented\n");
1569 }
1570
1571
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 135 times.
135 if (sps->scc_extension) {
1572 sps->curr_pic_ref_enabled = get_bits1(gb);
1573 sps->palette_mode_enabled = get_bits1(gb);
1574 if (sps->palette_mode_enabled) {
1575 sps->palette_max_size = get_ue_golomb(gb);
1576 sps->delta_palette_max_predictor_size = get_ue_golomb(gb);
1577 sps->palette_predictor_initializers_present = get_bits1(gb);
1578
1579 if (sps->palette_predictor_initializers_present) {
1580 sps->sps_num_palette_predictor_initializers = get_ue_golomb(gb) + 1;
1581 if (sps->sps_num_palette_predictor_initializers > HEVC_MAX_PALETTE_PREDICTOR_SIZE) {
1582 av_log(avctx, AV_LOG_ERROR,
1583 "sps_num_palette_predictor_initializers out of range: %u\n",
1584 sps->sps_num_palette_predictor_initializers);
1585 return AVERROR_INVALIDDATA;
1586 }
1587 num_comps = !sps->chroma_format_idc ? 1 : 3;
1588 for (int comp = 0; comp < num_comps; comp++) {
1589 int bit_depth = !comp ? sps->bit_depth : sps->bit_depth_chroma;
1590 for (i = 0; i < sps->sps_num_palette_predictor_initializers; i++)
1591 sps->sps_palette_predictor_initializer[comp][i] = get_bits(gb, bit_depth);
1592 }
1593 }
1594 }
1595 sps->motion_vector_resolution_control_idc = get_bits(gb, 2);
1596 sps->intra_boundary_filtering_disabled = get_bits1(gb);
1597 }
1598 }
1599
2/2
✓ Branch 0 taken 480 times.
✓ Branch 1 taken 773 times.
1253 if (apply_defdispwin) {
1600 480 sps->output_window.left_offset += sps->vui.def_disp_win.left_offset;
1601 480 sps->output_window.right_offset += sps->vui.def_disp_win.right_offset;
1602 480 sps->output_window.top_offset += sps->vui.def_disp_win.top_offset;
1603 480 sps->output_window.bottom_offset += sps->vui.def_disp_win.bottom_offset;
1604 }
1605
1606 1253 ow = &sps->output_window;
1607
1/2
✓ Branch 0 taken 1253 times.
✗ Branch 1 not taken.
1253 if (ow->left_offset >= INT_MAX - ow->right_offset ||
1608
1/2
✓ Branch 0 taken 1253 times.
✗ Branch 1 not taken.
1253 ow->top_offset >= INT_MAX - ow->bottom_offset ||
1609
1/2
✓ Branch 0 taken 1253 times.
✗ Branch 1 not taken.
1253 ow->left_offset + ow->right_offset >= sps->width ||
1610
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 ow->top_offset + ow->bottom_offset >= sps->height) {
1611 av_log(avctx, AV_LOG_WARNING, "Invalid cropping offsets: %u/%u/%u/%u\n",
1612 ow->left_offset, ow->right_offset, ow->top_offset, ow->bottom_offset);
1613 if (avctx->err_recognition & AV_EF_EXPLODE) {
1614 return AVERROR_INVALIDDATA;
1615 }
1616 av_log(avctx, AV_LOG_WARNING,
1617 "Displaying the whole video surface.\n");
1618 memset(ow, 0, sizeof(*ow));
1619 memset(&sps->pic_conf_win, 0, sizeof(sps->pic_conf_win));
1620 }
1621
1622 // Inferred parameters
1623 1253 sps->log2_ctb_size = sps->log2_min_cb_size +
1624 1253 sps->log2_diff_max_min_coding_block_size;
1625 1253 sps->log2_min_pu_size = sps->log2_min_cb_size - 1;
1626
1627
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (sps->log2_ctb_size > HEVC_MAX_LOG2_CTB_SIZE) {
1628 av_log(avctx, AV_LOG_ERROR, "CTB size out of range: 2^%d\n", sps->log2_ctb_size);
1629 return AVERROR_INVALIDDATA;
1630 }
1631
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (sps->log2_ctb_size < 4) {
1632 av_log(avctx,
1633 AV_LOG_ERROR,
1634 "log2_ctb_size %d differs from the bounds of any known profile\n",
1635 sps->log2_ctb_size);
1636 avpriv_request_sample(avctx, "log2_ctb_size %d", sps->log2_ctb_size);
1637 return AVERROR_INVALIDDATA;
1638 }
1639
1640 1253 sps->ctb_width = (sps->width + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
1641 1253 sps->ctb_height = (sps->height + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
1642 1253 sps->ctb_size = sps->ctb_width * sps->ctb_height;
1643
1644 1253 sps->min_cb_width = sps->width >> sps->log2_min_cb_size;
1645 1253 sps->min_cb_height = sps->height >> sps->log2_min_cb_size;
1646 1253 sps->min_tb_width = sps->width >> sps->log2_min_tb_size;
1647 1253 sps->min_tb_height = sps->height >> sps->log2_min_tb_size;
1648 1253 sps->min_pu_width = sps->width >> sps->log2_min_pu_size;
1649 1253 sps->min_pu_height = sps->height >> sps->log2_min_pu_size;
1650 1253 sps->tb_mask = (1 << (sps->log2_ctb_size - sps->log2_min_tb_size)) - 1;
1651
1652 1253 sps->qp_bd_offset = 6 * (sps->bit_depth - 8);
1653
1654
1/2
✓ Branch 0 taken 1253 times.
✗ Branch 1 not taken.
1253 if (av_zero_extend(sps->width, sps->log2_min_cb_size) ||
1655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 av_zero_extend(sps->height, sps->log2_min_cb_size)) {
1656 av_log(avctx, AV_LOG_ERROR, "Invalid coded frame dimensions.\n");
1657 return AVERROR_INVALIDDATA;
1658 }
1659
1660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (sps->max_transform_hierarchy_depth_inter > sps->log2_ctb_size - sps->log2_min_tb_size) {
1661 av_log(avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_inter out of range: %d\n",
1662 sps->max_transform_hierarchy_depth_inter);
1663 return AVERROR_INVALIDDATA;
1664 }
1665
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (sps->max_transform_hierarchy_depth_intra > sps->log2_ctb_size - sps->log2_min_tb_size) {
1666 av_log(avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_intra out of range: %d\n",
1667 sps->max_transform_hierarchy_depth_intra);
1668 return AVERROR_INVALIDDATA;
1669 }
1670
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (sps->log2_max_trafo_size > FFMIN(sps->log2_ctb_size, 5)) {
1671 av_log(avctx, AV_LOG_ERROR,
1672 "max transform block size out of range: %d\n",
1673 sps->log2_max_trafo_size);
1674 return AVERROR_INVALIDDATA;
1675 }
1676
1677
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1253 times.
1253 if (get_bits_left(gb) < 0) {
1678 av_log(avctx, AV_LOG_ERROR,
1679 "Overread SPS by %d bits\n", -get_bits_left(gb));
1680 return AVERROR_INVALIDDATA;
1681 }
1682
1683 1253 return 0;
1684 }
1685
1686 1253 static void hevc_sps_free(AVRefStructOpaque opaque, void *obj)
1687 {
1688 1253 HEVCSPS *sps = obj;
1689
1690 1253 av_refstruct_unref(&sps->vps);
1691
1692 1253 av_freep(&sps->data);
1693 1253 }
1694
1695 471 static int compare_sps(const HEVCSPS *sps1, const HEVCSPS *sps2)
1696 {
1697
2/2
✓ Branch 0 taken 467 times.
✓ Branch 1 taken 4 times.
938 return sps1->data_size == sps2->data_size &&
1698
2/2
✓ Branch 0 taken 463 times.
✓ Branch 1 taken 4 times.
467 !memcmp(sps1->data, sps2->data, sps1->data_size);
1699 }
1700
1701 1253 int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx,
1702 HEVCParamSets *ps, unsigned nuh_layer_id,
1703 int apply_defdispwin)
1704 {
1705 1253 HEVCSPS *sps = av_refstruct_alloc_ext(sizeof(*sps), 0, NULL, hevc_sps_free);
1706 unsigned int sps_id;
1707 int ret;
1708
1709
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (!sps)
1710 return AVERROR(ENOMEM);
1711
1712 1253 av_log(avctx, AV_LOG_DEBUG, "Decoding SPS\n");
1713
1714 1253 sps->data_size = get_bits_bytesize(gb, 1);
1715 1253 sps->data = av_memdup(gb->buffer, sps->data_size);
1716
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (!sps->data) {
1717 ret = AVERROR(ENOMEM);
1718 goto err;
1719 }
1720
1721 1253 ret = ff_hevc_parse_sps(sps, gb, &sps_id,
1722 nuh_layer_id, apply_defdispwin,
1723 1253 ps->vps_list, avctx);
1724
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (ret < 0)
1725 goto err;
1726
1727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (avctx->debug & FF_DEBUG_BITSTREAM) {
1728 av_log(avctx, AV_LOG_DEBUG,
1729 "Parsed SPS: id %d; coded wxh: %dx%d; "
1730 "cropped wxh: %dx%d; pix_fmt: %s.\n",
1731 sps_id, sps->width, sps->height,
1732 sps->width - (sps->output_window.left_offset + sps->output_window.right_offset),
1733 sps->height - (sps->output_window.top_offset + sps->output_window.bottom_offset),
1734 av_get_pix_fmt_name(sps->pix_fmt));
1735 }
1736
1737 /* check if this is a repeat of an already parsed SPS, then keep the
1738 * original one.
1739 * otherwise drop all PPSes that depend on it */
1740
4/4
✓ Branch 0 taken 471 times.
✓ Branch 1 taken 782 times.
✓ Branch 2 taken 463 times.
✓ Branch 3 taken 8 times.
1724 if (ps->sps_list[sps_id] &&
1741 471 compare_sps(ps->sps_list[sps_id], sps)) {
1742 463 av_refstruct_unref(&sps);
1743 } else {
1744 790 remove_sps(ps, sps_id);
1745 790 ps->sps_list[sps_id] = sps;
1746 }
1747
1748 1253 return 0;
1749 err:
1750 av_refstruct_unref(&sps);
1751 return ret;
1752 }
1753
1754 2266 static void hevc_pps_free(AVRefStructOpaque unused, void *obj)
1755 {
1756 2266 HEVCPPS *pps = obj;
1757
1758 2266 av_refstruct_unref(&pps->sps);
1759
1760 2266 av_freep(&pps->column_width);
1761 2266 av_freep(&pps->row_height);
1762 2266 av_freep(&pps->col_bd);
1763 2266 av_freep(&pps->row_bd);
1764 2266 av_freep(&pps->col_idxX);
1765 2266 av_freep(&pps->ctb_addr_rs_to_ts);
1766 2266 av_freep(&pps->ctb_addr_ts_to_rs);
1767 2266 av_freep(&pps->tile_pos_rs);
1768 2266 av_freep(&pps->tile_id);
1769 2266 av_freep(&pps->min_tb_addr_zs_tab);
1770 2266 av_freep(&pps->data);
1771 2266 }
1772
1773 3 static void colour_mapping_octants(GetBitContext *gb, HEVCPPS *pps, int inp_depth,
1774 int idx_y, int idx_cb, int idx_cr, int inp_length)
1775 {
1776 unsigned int split_octant_flag, part_num_y, coded_res_flag, res_coeff_q, res_coeff_r;
1777 int cm_res_bits;
1778
1779 3 part_num_y = 1 << pps->cm_y_part_num_log2;
1780
1781
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 split_octant_flag = inp_depth < pps->cm_octant_depth ? get_bits1(gb) : 0;
1782
1783
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (split_octant_flag)
1784 for (int k = 0; k < 2; k++)
1785 for (int m = 0; m < 2; m++)
1786 for (int n = 0; n < 2; n++)
1787 colour_mapping_octants(gb, pps, inp_depth + 1,
1788 idx_y + part_num_y * k * inp_length / 2,
1789 idx_cb + m * inp_length / 2,
1790 idx_cr + n * inp_length / 2,
1791 inp_length / 2);
1792 else
1793
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 3 times.
27 for (int i = 0; i < part_num_y; i++) {
1794
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 24 times.
120 for (int j = 0; j < 4; j++) {
1795 96 coded_res_flag = get_bits1(gb);
1796
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 if (coded_res_flag)
1797 for (int c = 0; c < 3; c++) {
1798 res_coeff_q = get_ue_golomb_long(gb);
1799 cm_res_bits = FFMAX(0, 10 + pps->luma_bit_depth_cm_input -
1800 pps->luma_bit_depth_cm_output -
1801 pps->cm_res_quant_bits - pps->cm_delta_flc_bits);
1802 res_coeff_r = cm_res_bits ? get_bits(gb, cm_res_bits) : 0;
1803 if (res_coeff_q || res_coeff_r)
1804 skip_bits1(gb);
1805 }
1806 }
1807 }
1808 3 }
1809
1810 3 static int colour_mapping_table(GetBitContext *gb, AVCodecContext *avctx, HEVCPPS *pps)
1811 {
1812 3 pps->num_cm_ref_layers = get_ue_golomb(gb) + 1;
1813
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (pps->num_cm_ref_layers > 62) {
1814 av_log(avctx, AV_LOG_ERROR,
1815 "num_cm_ref_layers_minus1 shall be in the range [0, 61].\n");
1816 return AVERROR_INVALIDDATA;
1817 }
1818
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
12 for (int i = 0; i < pps->num_cm_ref_layers; i++)
1819 9 pps->cm_ref_layer_id[i] = get_bits(gb, 6);
1820
1821 3 pps->cm_octant_depth = get_bits(gb, 2);
1822 3 pps->cm_y_part_num_log2 = get_bits(gb, 2);
1823
1824 3 pps->luma_bit_depth_cm_input = get_ue_golomb(gb) + 8;
1825 3 pps->chroma_bit_depth_cm_input = get_ue_golomb(gb) + 8;
1826 3 pps->luma_bit_depth_cm_output = get_ue_golomb(gb) + 8;
1827 3 pps->chroma_bit_depth_cm_output = get_ue_golomb(gb) + 8;
1828
1829 3 pps->cm_res_quant_bits = get_bits(gb, 2);
1830 3 pps->cm_delta_flc_bits = get_bits(gb, 2) + 1;
1831
1832
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (pps->cm_octant_depth == 1) {
1833 pps->cm_adapt_threshold_u_delta = get_se_golomb_long(gb);
1834 pps->cm_adapt_threshold_v_delta = get_se_golomb_long(gb);
1835 }
1836
1837 3 colour_mapping_octants(gb, pps, 0, 0, 0, 0, 1 << pps->cm_octant_depth);
1838
1839 3 return 0;
1840 }
1841
1842 47 static int pps_multilayer_extension(GetBitContext *gb, AVCodecContext *avctx,
1843 HEVCPPS *pps, const HEVCSPS *sps, const HEVCVPS *vps)
1844 {
1845 47 pps->poc_reset_info_present_flag = get_bits1(gb);
1846 47 pps->pps_infer_scaling_list_flag = get_bits1(gb);
1847
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (pps->pps_infer_scaling_list_flag)
1848 pps->pps_scaling_list_ref_layer_id = get_bits(gb, 6);
1849
1850 47 pps->num_ref_loc_offsets = get_ue_golomb(gb);
1851
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (pps->num_ref_loc_offsets > vps->vps_max_layers - 1)
1852 return AVERROR_INVALIDDATA;
1853
1854
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 for (int i = 0; i < pps->num_ref_loc_offsets; i++) {
1855 pps->ref_loc_offset_layer_id[i] = get_bits(gb, 6);
1856 pps->scaled_ref_layer_offset_present_flag[i] = get_bits1(gb);
1857 if (pps->scaled_ref_layer_offset_present_flag[i]) {
1858 pps->scaled_ref_layer_left_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1859 pps->scaled_ref_layer_top_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1860 pps->scaled_ref_layer_right_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1861 pps->scaled_ref_layer_bottom_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1862 }
1863
1864 pps->ref_region_offset_present_flag[i] = get_bits1(gb);
1865 if (pps->ref_region_offset_present_flag[i]) {
1866 pps->ref_region_left_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1867 pps->ref_region_top_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1868 pps->ref_region_right_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1869 pps->ref_region_bottom_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1870 }
1871
1872 pps->resample_phase_set_present_flag[i] = get_bits1(gb);
1873 if (pps->resample_phase_set_present_flag[i]) {
1874 pps->phase_hor_luma[pps->ref_loc_offset_layer_id[i]] = get_ue_golomb_31(gb);
1875 pps->phase_ver_luma[pps->ref_loc_offset_layer_id[i]] = get_ue_golomb_31(gb);
1876 pps->phase_hor_chroma[pps->ref_loc_offset_layer_id[i]] = get_ue_golomb(gb) - 8;
1877 pps->phase_ver_chroma[pps->ref_loc_offset_layer_id[i]] = get_ue_golomb(gb) - 8;
1878 }
1879 }
1880
1881 47 pps->colour_mapping_enabled_flag = get_bits1(gb);
1882
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 44 times.
47 if (pps->colour_mapping_enabled_flag) {
1883 3 int ret = colour_mapping_table(gb, avctx, pps);
1884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
1885 return ret;
1886 }
1887
1888 47 return 0;
1889 }
1890
1891 static void delta_dlt(GetBitContext *gb, HEVCPPS *pps)
1892 {
1893 unsigned int num_val_delta_dlt, max_diff = 0;
1894 int min_diff_minus1 = -1;
1895 unsigned int len;
1896
1897 num_val_delta_dlt = get_bits(gb, pps->pps_bit_depth_for_depth_layers_minus8 + 8);
1898 if (num_val_delta_dlt) {
1899 if (num_val_delta_dlt > 1)
1900 max_diff = get_bits(gb, pps->pps_bit_depth_for_depth_layers_minus8 + 8);
1901 if (num_val_delta_dlt > 2 && max_diff) {
1902 len = av_log2(max_diff) + 1;
1903 min_diff_minus1 = get_bits(gb, len);
1904 }
1905 if (max_diff > (min_diff_minus1 + 1))
1906 for (int k = 1; k < num_val_delta_dlt; k++) {
1907 len = av_log2(max_diff - (min_diff_minus1 + 1)) + 1;
1908 skip_bits(gb, len); // delta_val_diff_minus_min
1909 }
1910 }
1911 }
1912
1913 3 static int pps_3d_extension(GetBitContext *gb, AVCodecContext *avctx,
1914 HEVCPPS *pps, const HEVCSPS *sps)
1915 {
1916 unsigned int pps_depth_layers_minus1;
1917
1918
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (get_bits1(gb)) { // dlts_present_flag
1919 pps_depth_layers_minus1 = get_bits(gb, 6);
1920 pps->pps_bit_depth_for_depth_layers_minus8 = get_bits(gb, 4);
1921 for (int i = 0; i <= pps_depth_layers_minus1; i++) {
1922 if (get_bits1(gb)) { // dlt_flag[i]
1923 if (!get_bits1(gb)) { // dlt_pred_flag[i]
1924 if (get_bits1(gb)) { // dlt_val_flags_present_flag[i]
1925 for (int j = 0; j <= ((1 << (pps->pps_bit_depth_for_depth_layers_minus8 + 8)) - 1); j++)
1926 skip_bits1(gb); // dlt_value_flag[i][j]
1927 } else
1928 delta_dlt(gb, pps);
1929 }
1930 }
1931 }
1932 }
1933
1934 3 return 0;
1935 }
1936
1937 63 static int pps_range_extensions(GetBitContext *gb, AVCodecContext *avctx,
1938 HEVCPPS *pps, const HEVCSPS *sps)
1939 {
1940
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 if (pps->transform_skip_enabled_flag) {
1941 63 pps->log2_max_transform_skip_block_size = get_ue_golomb_31(gb) + 2;
1942 }
1943 63 pps->cross_component_prediction_enabled_flag = get_bits1(gb);
1944 63 pps->chroma_qp_offset_list_enabled_flag = get_bits1(gb);
1945
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 57 times.
63 if (pps->chroma_qp_offset_list_enabled_flag) {
1946 6 pps->diff_cu_chroma_qp_offset_depth = get_ue_golomb_31(gb);
1947 6 pps->chroma_qp_offset_list_len_minus1 = get_ue_golomb_31(gb);
1948
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (pps->chroma_qp_offset_list_len_minus1 > 5) {
1949 av_log(avctx, AV_LOG_ERROR,
1950 "chroma_qp_offset_list_len_minus1 shall be in the range [0, 5].\n");
1951 return AVERROR_INVALIDDATA;
1952 }
1953
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 for (int i = 0; i <= pps->chroma_qp_offset_list_len_minus1; i++) {
1954 6 pps->cb_qp_offset_list[i] = get_se_golomb(gb);
1955
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (pps->cb_qp_offset_list[i]) {
1956 6 av_log(avctx, AV_LOG_WARNING,
1957 "cb_qp_offset_list not tested yet.\n");
1958 }
1959 6 pps->cr_qp_offset_list[i] = get_se_golomb(gb);
1960
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (pps->cr_qp_offset_list[i]) {
1961 6 av_log(avctx, AV_LOG_WARNING,
1962 "cb_qp_offset_list not tested yet.\n");
1963 }
1964 }
1965 }
1966 63 pps->log2_sao_offset_scale_luma = get_ue_golomb_31(gb);
1967 63 pps->log2_sao_offset_scale_chroma = get_ue_golomb_31(gb);
1968
1969
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 if ( pps->log2_sao_offset_scale_luma > FFMAX(sps->bit_depth - 10, 0)
1970
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
63 || pps->log2_sao_offset_scale_chroma > FFMAX(sps->bit_depth_chroma - 10, 0)
1971 )
1972 return AVERROR_INVALIDDATA;
1973
1974 63 return(0);
1975 }
1976
1977 3 static int pps_scc_extension(GetBitContext *gb, AVCodecContext *avctx,
1978 HEVCPPS *pps, const HEVCSPS *sps)
1979 {
1980 int num_comps, ret;
1981
1982 3 pps->pps_curr_pic_ref_enabled_flag = get_bits1(gb);
1983
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (pps->residual_adaptive_colour_transform_enabled_flag = get_bits1(gb)) {
1984 pps->pps_slice_act_qp_offsets_present_flag = get_bits1(gb);
1985 pps->pps_act_y_qp_offset = get_se_golomb(gb) - 5;
1986 pps->pps_act_cb_qp_offset = get_se_golomb(gb) - 5;
1987 pps->pps_act_cr_qp_offset = get_se_golomb(gb) - 3;
1988
1989 #define CHECK_QP_OFFSET(name) (pps->pps_act_ ## name ## _qp_offset <= -12 || \
1990 pps->pps_act_ ## name ## _qp_offset >= 12)
1991 ret = CHECK_QP_OFFSET(y) || CHECK_QP_OFFSET(cb) || CHECK_QP_OFFSET(cr);
1992 #undef CHECK_QP_OFFSET
1993 if (ret) {
1994 av_log(avctx, AV_LOG_ERROR,
1995 "PpsActQpOffsetY/Cb/Cr shall be in the range of [-12, 12].\n");
1996 return AVERROR_INVALIDDATA;
1997 }
1998 }
1999
2000
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (pps->pps_palette_predictor_initializers_present_flag = get_bits1(gb)) {
2001 pps->pps_num_palette_predictor_initializers = get_ue_golomb(gb);
2002 if (pps->pps_num_palette_predictor_initializers > 0) {
2003 if (pps->pps_num_palette_predictor_initializers > HEVC_MAX_PALETTE_PREDICTOR_SIZE) {
2004 av_log(avctx, AV_LOG_ERROR,
2005 "pps_num_palette_predictor_initializers out of range: %u\n",
2006 pps->pps_num_palette_predictor_initializers);
2007 return AVERROR_INVALIDDATA;
2008 }
2009 pps->monochrome_palette_flag = get_bits1(gb);
2010 pps->luma_bit_depth_entry = get_ue_golomb_31(gb) + 8;
2011 if (pps->luma_bit_depth_entry != sps->bit_depth)
2012 return AVERROR_INVALIDDATA;
2013 if (!pps->monochrome_palette_flag) {
2014 pps->chroma_bit_depth_entry = get_ue_golomb_31(gb) + 8;
2015 if (pps->chroma_bit_depth_entry != sps->bit_depth_chroma)
2016 return AVERROR_INVALIDDATA;
2017 }
2018
2019 num_comps = pps->monochrome_palette_flag ? 1 : 3;
2020 for (int comp = 0; comp < num_comps; comp++) {
2021 int bit_depth = !comp ? pps->luma_bit_depth_entry : pps->chroma_bit_depth_entry;
2022 for (int i = 0; i < pps->pps_num_palette_predictor_initializers; i++)
2023 pps->pps_palette_predictor_initializer[comp][i] = get_bits(gb, bit_depth);
2024 }
2025 }
2026 }
2027
2028 3 return 0;
2029 }
2030
2031 2262 static inline int setup_pps(AVCodecContext *avctx, GetBitContext *gb,
2032 HEVCPPS *pps, const HEVCSPS *sps)
2033 {
2034 int log2_diff;
2035 int pic_area_in_ctbs;
2036 int i, j, x, y, ctb_addr_rs, tile_id;
2037
2038 // Inferred parameters
2039 2262 pps->col_bd = av_malloc_array(pps->num_tile_columns + 1, sizeof(*pps->col_bd));
2040 2262 pps->row_bd = av_malloc_array(pps->num_tile_rows + 1, sizeof(*pps->row_bd));
2041 2262 pps->col_idxX = av_malloc_array(sps->ctb_width, sizeof(*pps->col_idxX));
2042
3/6
✓ Branch 0 taken 2262 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2262 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2262 times.
2262 if (!pps->col_bd || !pps->row_bd || !pps->col_idxX)
2043 return AVERROR(ENOMEM);
2044
2045
2/2
✓ Branch 0 taken 1730 times.
✓ Branch 1 taken 532 times.
2262 if (pps->uniform_spacing_flag) {
2046
2/2
✓ Branch 0 taken 1504 times.
✓ Branch 1 taken 226 times.
1730 if (!pps->column_width) {
2047 1504 pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
2048 1504 pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
2049 }
2050
2/4
✓ Branch 0 taken 1730 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1730 times.
1730 if (!pps->column_width || !pps->row_height)
2051 return AVERROR(ENOMEM);
2052
2053
2/2
✓ Branch 0 taken 1891 times.
✓ Branch 1 taken 1730 times.
3621 for (i = 0; i < pps->num_tile_columns; i++) {
2054 1891 pps->column_width[i] = ((i + 1) * sps->ctb_width) / pps->num_tile_columns -
2055 1891 (i * sps->ctb_width) / pps->num_tile_columns;
2056 }
2057
2058
2/2
✓ Branch 0 taken 1909 times.
✓ Branch 1 taken 1730 times.
3639 for (i = 0; i < pps->num_tile_rows; i++) {
2059 1909 pps->row_height[i] = ((i + 1) * sps->ctb_height) / pps->num_tile_rows -
2060 1909 (i * sps->ctb_height) / pps->num_tile_rows;
2061 }
2062 }
2063
2064 2262 pps->col_bd[0] = 0;
2065
2/2
✓ Branch 0 taken 4480 times.
✓ Branch 1 taken 2262 times.
6742 for (i = 0; i < pps->num_tile_columns; i++)
2066 4480 pps->col_bd[i + 1] = pps->col_bd[i] + pps->column_width[i];
2067
2068 2262 pps->row_bd[0] = 0;
2069
2/2
✓ Branch 0 taken 4471 times.
✓ Branch 1 taken 2262 times.
6733 for (i = 0; i < pps->num_tile_rows; i++)
2070 4471 pps->row_bd[i + 1] = pps->row_bd[i] + pps->row_height[i];
2071
2072
2/2
✓ Branch 0 taken 45277 times.
✓ Branch 1 taken 2262 times.
47539 for (i = 0, j = 0; i < sps->ctb_width; i++) {
2073
2/2
✓ Branch 0 taken 4471 times.
✓ Branch 1 taken 40806 times.
45277 if (i > pps->col_bd[j])
2074 4471 j++;
2075 45277 pps->col_idxX[i] = j;
2076 }
2077
2078 /**
2079 * 6.5
2080 */
2081 2262 pic_area_in_ctbs = sps->ctb_width * sps->ctb_height;
2082
2083 2262 pps->ctb_addr_rs_to_ts = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_rs_to_ts));
2084 2262 pps->ctb_addr_ts_to_rs = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_ts_to_rs));
2085 2262 pps->tile_id = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->tile_id));
2086 2262 pps->min_tb_addr_zs_tab = av_malloc_array((sps->tb_mask+2) * (sps->tb_mask+2), sizeof(*pps->min_tb_addr_zs_tab));
2087
2/4
✓ Branch 0 taken 2262 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2262 times.
✗ Branch 3 not taken.
2262 if (!pps->ctb_addr_rs_to_ts || !pps->ctb_addr_ts_to_rs ||
2088
2/4
✓ Branch 0 taken 2262 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2262 times.
2262 !pps->tile_id || !pps->min_tb_addr_zs_tab) {
2089 return AVERROR(ENOMEM);
2090 }
2091
2092
2/2
✓ Branch 0 taken 754483 times.
✓ Branch 1 taken 2262 times.
756745 for (ctb_addr_rs = 0; ctb_addr_rs < pic_area_in_ctbs; ctb_addr_rs++) {
2093 754483 int tb_x = ctb_addr_rs % sps->ctb_width;
2094 754483 int tb_y = ctb_addr_rs / sps->ctb_width;
2095 754483 int tile_x = 0;
2096 754483 int tile_y = 0;
2097 754483 int val = 0;
2098
2099
1/2
✓ Branch 0 taken 1278363 times.
✗ Branch 1 not taken.
1278363 for (i = 0; i < pps->num_tile_columns; i++) {
2100
2/2
✓ Branch 0 taken 754483 times.
✓ Branch 1 taken 523880 times.
1278363 if (tb_x < pps->col_bd[i + 1]) {
2101 754483 tile_x = i;
2102 754483 break;
2103 }
2104 }
2105
2106
1/2
✓ Branch 0 taken 1132881 times.
✗ Branch 1 not taken.
1132881 for (i = 0; i < pps->num_tile_rows; i++) {
2107
2/2
✓ Branch 0 taken 754483 times.
✓ Branch 1 taken 378398 times.
1132881 if (tb_y < pps->row_bd[i + 1]) {
2108 754483 tile_y = i;
2109 754483 break;
2110 }
2111 }
2112
2113
2/2
✓ Branch 0 taken 523880 times.
✓ Branch 1 taken 754483 times.
1278363 for (i = 0; i < tile_x; i++)
2114 523880 val += pps->row_height[tile_y] * pps->column_width[i];
2115
2/2
✓ Branch 0 taken 378398 times.
✓ Branch 1 taken 754483 times.
1132881 for (i = 0; i < tile_y; i++)
2116 378398 val += sps->ctb_width * pps->row_height[i];
2117
2118 754483 val += (tb_y - pps->row_bd[tile_y]) * pps->column_width[tile_x] +
2119 754483 tb_x - pps->col_bd[tile_x];
2120
2121 754483 pps->ctb_addr_rs_to_ts[ctb_addr_rs] = val;
2122 754483 pps->ctb_addr_ts_to_rs[val] = ctb_addr_rs;
2123 }
2124
2125
2/2
✓ Branch 0 taken 4471 times.
✓ Branch 1 taken 2262 times.
6733 for (j = 0, tile_id = 0; j < pps->num_tile_rows; j++)
2126
2/2
✓ Branch 0 taken 14841 times.
✓ Branch 1 taken 4471 times.
19312 for (i = 0; i < pps->num_tile_columns; i++, tile_id++)
2127
2/2
✓ Branch 0 taken 67879 times.
✓ Branch 1 taken 14841 times.
82720 for (y = pps->row_bd[j]; y < pps->row_bd[j + 1]; y++)
2128
2/2
✓ Branch 0 taken 754483 times.
✓ Branch 1 taken 67879 times.
822362 for (x = pps->col_bd[i]; x < pps->col_bd[i + 1]; x++)
2129 754483 pps->tile_id[pps->ctb_addr_rs_to_ts[y * sps->ctb_width + x]] = tile_id;
2130
2131 2262 pps->tile_pos_rs = av_malloc_array(tile_id, sizeof(*pps->tile_pos_rs));
2132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2262 times.
2262 if (!pps->tile_pos_rs)
2133 return AVERROR(ENOMEM);
2134
2135
2/2
✓ Branch 0 taken 4471 times.
✓ Branch 1 taken 2262 times.
6733 for (j = 0; j < pps->num_tile_rows; j++)
2136
2/2
✓ Branch 0 taken 14841 times.
✓ Branch 1 taken 4471 times.
19312 for (i = 0; i < pps->num_tile_columns; i++)
2137 14841 pps->tile_pos_rs[j * pps->num_tile_columns + i] =
2138 14841 pps->row_bd[j] * sps->ctb_width + pps->col_bd[i];
2139
2140 2262 log2_diff = sps->log2_ctb_size - sps->log2_min_tb_size;
2141 2262 pps->min_tb_addr_zs = &pps->min_tb_addr_zs_tab[1*(sps->tb_mask+2)+1];
2142
2/2
✓ Branch 0 taken 37278 times.
✓ Branch 1 taken 2262 times.
39540 for (y = 0; y < sps->tb_mask+2; y++) {
2143 37278 pps->min_tb_addr_zs_tab[y*(sps->tb_mask+2)] = -1;
2144 37278 pps->min_tb_addr_zs_tab[y] = -1;
2145 }
2146
2/2
✓ Branch 0 taken 35016 times.
✓ Branch 1 taken 2262 times.
37278 for (y = 0; y < sps->tb_mask+1; y++) {
2147
2/2
✓ Branch 0 taken 552384 times.
✓ Branch 1 taken 35016 times.
587400 for (x = 0; x < sps->tb_mask+1; x++) {
2148 552384 int tb_x = x >> log2_diff;
2149 552384 int tb_y = y >> log2_diff;
2150 552384 int rs = sps->ctb_width * tb_y + tb_x;
2151 552384 int val = pps->ctb_addr_rs_to_ts[rs] << (log2_diff * 2);
2152
2/2
✓ Branch 0 taken 2202176 times.
✓ Branch 1 taken 552384 times.
2754560 for (i = 0; i < log2_diff; i++) {
2153 2202176 int m = 1 << i;
2154
4/4
✓ Branch 0 taken 1101088 times.
✓ Branch 1 taken 1101088 times.
✓ Branch 2 taken 1101088 times.
✓ Branch 3 taken 1101088 times.
2202176 val += (m & x ? m * m : 0) + (m & y ? 2 * m * m : 0);
2155 }
2156 552384 pps->min_tb_addr_zs[y * (sps->tb_mask+2) + x] = val;
2157 }
2158 }
2159
2160 2262 return 0;
2161 }
2162
2163 3601 int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx,
2164 HEVCParamSets *ps)
2165 {
2166 3601 const HEVCSPS *sps = NULL;
2167 3601 const HEVCVPS *vps = NULL;
2168 3601 int i, ret = 0;
2169 3601 ptrdiff_t nal_size = get_bits_bytesize(gb, 1);
2170 3601 unsigned int pps_id = get_ue_golomb_long(gb);
2171 unsigned log2_parallel_merge_level_minus2;
2172 HEVCPPS *pps;
2173
2174 3601 av_log(avctx, AV_LOG_DEBUG, "Decoding PPS\n");
2175
2176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3601 times.
3601 if (pps_id >= HEVC_MAX_PPS_COUNT) {
2177 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
2178 return AVERROR_INVALIDDATA;
2179 }
2180
2181
2/2
✓ Branch 0 taken 2556 times.
✓ Branch 1 taken 1045 times.
3601 if (ps->pps_list[pps_id]) {
2182 2556 const HEVCPPS *pps1 = ps->pps_list[pps_id];
2183
2/2
✓ Branch 0 taken 2160 times.
✓ Branch 1 taken 396 times.
2556 if (pps1->data_size == nal_size &&
2184
2/2
✓ Branch 0 taken 1335 times.
✓ Branch 1 taken 825 times.
2160 !memcmp(pps1->data, gb->buffer, pps1->data_size))
2185 1335 return 0;
2186 }
2187
2188 2266 pps = av_refstruct_alloc_ext(sizeof(*pps), 0, NULL, hevc_pps_free);
2189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2266 times.
2266 if (!pps)
2190 return AVERROR(ENOMEM);
2191
2192 2266 pps->data_size = nal_size;
2193 2266 pps->data = av_memdup(gb->buffer, nal_size);
2194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2266 times.
2266 if (!pps->data) {
2195 ret = AVERROR_INVALIDDATA;
2196 goto err;
2197 }
2198
2199 // Default values
2200 2266 pps->loop_filter_across_tiles_enabled_flag = 1;
2201 2266 pps->num_tile_columns = 1;
2202 2266 pps->num_tile_rows = 1;
2203 2266 pps->uniform_spacing_flag = 1;
2204 2266 pps->disable_dbf = 0;
2205 2266 pps->beta_offset = 0;
2206 2266 pps->tc_offset = 0;
2207 2266 pps->log2_max_transform_skip_block_size = 2;
2208
2209 // Coded parameters
2210 2266 pps->pps_id = pps_id;
2211 2266 pps->sps_id = get_ue_golomb_long(gb);
2212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2266 times.
2266 if (pps->sps_id >= HEVC_MAX_SPS_COUNT) {
2213 av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", pps->sps_id);
2214 ret = AVERROR_INVALIDDATA;
2215 goto err;
2216 }
2217
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2262 times.
2266 if (!ps->sps_list[pps->sps_id]) {
2218 4 av_log(avctx, AV_LOG_ERROR, "SPS %u does not exist.\n", pps->sps_id);
2219 4 ret = AVERROR_INVALIDDATA;
2220 4 goto err;
2221 }
2222 2262 sps = ps->sps_list[pps->sps_id];
2223 2262 vps = ps->vps_list[sps->vps_id];
2224
2225 2262 pps->sps = av_refstruct_ref_c(sps);
2226
2227 2262 pps->dependent_slice_segments_enabled_flag = get_bits1(gb);
2228 2262 pps->output_flag_present_flag = get_bits1(gb);
2229 2262 pps->num_extra_slice_header_bits = get_bits(gb, 3);
2230
2231 2262 pps->sign_data_hiding_flag = get_bits1(gb);
2232
2233 2262 pps->cabac_init_present_flag = get_bits1(gb);
2234
2235 2262 pps->num_ref_idx_l0_default_active = get_ue_golomb_31(gb) + 1;
2236 2262 pps->num_ref_idx_l1_default_active = get_ue_golomb_31(gb) + 1;
2237
1/2
✓ Branch 0 taken 2262 times.
✗ Branch 1 not taken.
2262 if (pps->num_ref_idx_l0_default_active >= HEVC_MAX_REFS ||
2238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2262 times.
2262 pps->num_ref_idx_l1_default_active >= HEVC_MAX_REFS) {
2239 av_log(avctx, AV_LOG_ERROR, "Too many default refs in PPS: %d/%d.\n",
2240 pps->num_ref_idx_l0_default_active, pps->num_ref_idx_l1_default_active);
2241 goto err;
2242 }
2243
2244 2262 pps->pic_init_qp_minus26 = get_se_golomb(gb);
2245
2246 2262 pps->constrained_intra_pred_flag = get_bits1(gb);
2247 2262 pps->transform_skip_enabled_flag = get_bits1(gb);
2248
2249 2262 pps->cu_qp_delta_enabled_flag = get_bits1(gb);
2250 2262 pps->diff_cu_qp_delta_depth = 0;
2251
2/2
✓ Branch 0 taken 508 times.
✓ Branch 1 taken 1754 times.
2262 if (pps->cu_qp_delta_enabled_flag)
2252 508 pps->diff_cu_qp_delta_depth = get_ue_golomb_long(gb);
2253
2254
1/2
✓ Branch 0 taken 2262 times.
✗ Branch 1 not taken.
2262 if (pps->diff_cu_qp_delta_depth < 0 ||
2255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2262 times.
2262 pps->diff_cu_qp_delta_depth > sps->log2_diff_max_min_coding_block_size) {
2256 av_log(avctx, AV_LOG_ERROR, "diff_cu_qp_delta_depth %d is invalid\n",
2257 pps->diff_cu_qp_delta_depth);
2258 ret = AVERROR_INVALIDDATA;
2259 goto err;
2260 }
2261
2262 2262 pps->cb_qp_offset = get_se_golomb(gb);
2263
2/4
✓ Branch 0 taken 2262 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2262 times.
2262 if (pps->cb_qp_offset < -12 || pps->cb_qp_offset > 12) {
2264 av_log(avctx, AV_LOG_ERROR, "pps_cb_qp_offset out of range: %d\n",
2265 pps->cb_qp_offset);
2266 ret = AVERROR_INVALIDDATA;
2267 goto err;
2268 }
2269 2262 pps->cr_qp_offset = get_se_golomb(gb);
2270
2/4
✓ Branch 0 taken 2262 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2262 times.
2262 if (pps->cr_qp_offset < -12 || pps->cr_qp_offset > 12) {
2271 av_log(avctx, AV_LOG_ERROR, "pps_cr_qp_offset out of range: %d\n",
2272 pps->cr_qp_offset);
2273 ret = AVERROR_INVALIDDATA;
2274 goto err;
2275 }
2276 2262 pps->pic_slice_level_chroma_qp_offsets_present_flag = get_bits1(gb);
2277
2278 2262 pps->weighted_pred_flag = get_bits1(gb);
2279 2262 pps->weighted_bipred_flag = get_bits1(gb);
2280
2281 2262 pps->transquant_bypass_enable_flag = get_bits1(gb);
2282 2262 pps->tiles_enabled_flag = get_bits1(gb);
2283 2262 pps->entropy_coding_sync_enabled_flag = get_bits1(gb);
2284
2285
2/2
✓ Branch 0 taken 758 times.
✓ Branch 1 taken 1504 times.
2262 if (pps->tiles_enabled_flag) {
2286 758 int num_tile_columns_minus1 = get_ue_golomb(gb);
2287 758 int num_tile_rows_minus1 = get_ue_golomb(gb);
2288
2289
1/2
✓ Branch 0 taken 758 times.
✗ Branch 1 not taken.
758 if (num_tile_columns_minus1 < 0 ||
2290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 758 times.
758 num_tile_columns_minus1 >= sps->ctb_width) {
2291 av_log(avctx, AV_LOG_ERROR, "num_tile_columns_minus1 out of range: %d\n",
2292 num_tile_columns_minus1);
2293 ret = num_tile_columns_minus1 < 0 ? num_tile_columns_minus1 : AVERROR_INVALIDDATA;
2294 goto err;
2295 }
2296
1/2
✓ Branch 0 taken 758 times.
✗ Branch 1 not taken.
758 if (num_tile_rows_minus1 < 0 ||
2297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 758 times.
758 num_tile_rows_minus1 >= sps->ctb_height) {
2298 av_log(avctx, AV_LOG_ERROR, "num_tile_rows_minus1 out of range: %d\n",
2299 num_tile_rows_minus1);
2300 ret = num_tile_rows_minus1 < 0 ? num_tile_rows_minus1 : AVERROR_INVALIDDATA;
2301 goto err;
2302 }
2303 758 pps->num_tile_columns = num_tile_columns_minus1 + 1;
2304 758 pps->num_tile_rows = num_tile_rows_minus1 + 1;
2305
2306 758 pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
2307 758 pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
2308
2/4
✓ Branch 0 taken 758 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 758 times.
758 if (!pps->column_width || !pps->row_height) {
2309 ret = AVERROR(ENOMEM);
2310 goto err;
2311 }
2312
2313 758 pps->uniform_spacing_flag = get_bits1(gb);
2314
2/2
✓ Branch 0 taken 532 times.
✓ Branch 1 taken 226 times.
758 if (!pps->uniform_spacing_flag) {
2315 532 uint64_t sum = 0;
2316
2/2
✓ Branch 0 taken 2057 times.
✓ Branch 1 taken 532 times.
2589 for (i = 0; i < pps->num_tile_columns - 1; i++) {
2317 2057 pps->column_width[i] = get_ue_golomb_long(gb) + 1;
2318 2057 sum += pps->column_width[i];
2319 }
2320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 532 times.
532 if (sum >= sps->ctb_width) {
2321 av_log(avctx, AV_LOG_ERROR, "Invalid tile widths.\n");
2322 ret = AVERROR_INVALIDDATA;
2323 goto err;
2324 }
2325 532 pps->column_width[pps->num_tile_columns - 1] = sps->ctb_width - sum;
2326
2327 532 sum = 0;
2328
2/2
✓ Branch 0 taken 2030 times.
✓ Branch 1 taken 532 times.
2562 for (i = 0; i < pps->num_tile_rows - 1; i++) {
2329 2030 pps->row_height[i] = get_ue_golomb_long(gb) + 1;
2330 2030 sum += pps->row_height[i];
2331 }
2332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 532 times.
532 if (sum >= sps->ctb_height) {
2333 av_log(avctx, AV_LOG_ERROR, "Invalid tile heights.\n");
2334 ret = AVERROR_INVALIDDATA;
2335 goto err;
2336 }
2337 532 pps->row_height[pps->num_tile_rows - 1] = sps->ctb_height - sum;
2338 }
2339 758 pps->loop_filter_across_tiles_enabled_flag = get_bits1(gb);
2340 }
2341
2342 2262 pps->seq_loop_filter_across_slices_enabled_flag = get_bits1(gb);
2343
2344 2262 pps->deblocking_filter_control_present_flag = get_bits1(gb);
2345
2/2
✓ Branch 0 taken 897 times.
✓ Branch 1 taken 1365 times.
2262 if (pps->deblocking_filter_control_present_flag) {
2346 897 pps->deblocking_filter_override_enabled_flag = get_bits1(gb);
2347 897 pps->disable_dbf = get_bits1(gb);
2348
2/2
✓ Branch 0 taken 771 times.
✓ Branch 1 taken 126 times.
897 if (!pps->disable_dbf) {
2349 771 int beta_offset_div2 = get_se_golomb(gb);
2350 771 int tc_offset_div2 = get_se_golomb(gb) ;
2351
2/4
✓ Branch 0 taken 771 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 771 times.
771 if (beta_offset_div2 < -6 || beta_offset_div2 > 6) {
2352 av_log(avctx, AV_LOG_ERROR, "pps_beta_offset_div2 out of range: %d\n",
2353 beta_offset_div2);
2354 ret = AVERROR_INVALIDDATA;
2355 goto err;
2356 }
2357
2/4
✓ Branch 0 taken 771 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 771 times.
771 if (tc_offset_div2 < -6 || tc_offset_div2 > 6) {
2358 av_log(avctx, AV_LOG_ERROR, "pps_tc_offset_div2 out of range: %d\n",
2359 tc_offset_div2);
2360 ret = AVERROR_INVALIDDATA;
2361 goto err;
2362 }
2363 771 pps->beta_offset = 2 * beta_offset_div2;
2364 771 pps->tc_offset = 2 * tc_offset_div2;
2365 }
2366 }
2367
2368 2262 pps->scaling_list_data_present_flag = get_bits1(gb);
2369
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 2131 times.
2262 if (pps->scaling_list_data_present_flag) {
2370 131 set_default_scaling_list_data(&pps->scaling_list);
2371 131 ret = scaling_list_data(gb, avctx, &pps->scaling_list, sps);
2372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 131 times.
131 if (ret < 0)
2373 goto err;
2374 }
2375 2262 pps->lists_modification_present_flag = get_bits1(gb);
2376 2262 log2_parallel_merge_level_minus2 = get_ue_golomb_long(gb);
2377
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2262 times.
2262 if (log2_parallel_merge_level_minus2 > sps->log2_ctb_size) {
2378 av_log(avctx, AV_LOG_ERROR, "log2_parallel_merge_level_minus2 out of range: %d\n",
2379 log2_parallel_merge_level_minus2);
2380 ret = AVERROR_INVALIDDATA;
2381 goto err;
2382 }
2383 2262 pps->log2_parallel_merge_level = log2_parallel_merge_level_minus2 + 2;
2384
2385 2262 pps->slice_header_extension_present_flag = get_bits1(gb);
2386
2387 2262 pps->pps_extension_present_flag = get_bits1(gb);
2388
2/2
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 2149 times.
2262 if (pps->pps_extension_present_flag) {
2389 113 pps->pps_range_extensions_flag = get_bits1(gb);
2390 113 pps->pps_multilayer_extension_flag = get_bits1(gb);
2391 113 pps->pps_3d_extension_flag = get_bits1(gb);
2392 113 pps->pps_scc_extension_flag = get_bits1(gb);
2393 113 skip_bits(gb, 4); // pps_extension_4bits
2394
2395
3/4
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 50 times.
✓ Branch 2 taken 63 times.
✗ Branch 3 not taken.
113 if (sps->ptl.general_ptl.profile_idc >= AV_PROFILE_HEVC_REXT && pps->pps_range_extensions_flag) {
2396
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
63 if ((ret = pps_range_extensions(gb, avctx, pps, sps)) < 0)
2397 goto err;
2398 }
2399
2400
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 66 times.
113 if (pps->pps_multilayer_extension_flag) {
2401
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
47 if ((ret = pps_multilayer_extension(gb, avctx, pps, sps, vps)) < 0)
2402 goto err;
2403 }
2404
2405
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 110 times.
113 if (pps->pps_3d_extension_flag) {
2406
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = pps_3d_extension(gb, avctx, pps, sps)) < 0)
2407 goto err;
2408 }
2409
2410
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 110 times.
113 if (pps->pps_scc_extension_flag) {
2411
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = pps_scc_extension(gb, avctx, pps, sps)) < 0)
2412 goto err;
2413 }
2414 }
2415
2416 2262 ret = setup_pps(avctx, gb, pps, sps);
2417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2262 times.
2262 if (ret < 0)
2418 goto err;
2419
2420
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2259 times.
2262 if (get_bits_left(gb) < 0) {
2421 3 av_log(avctx, AV_LOG_WARNING,
2422 3 "Overread PPS by %d bits\n", -get_bits_left(gb));
2423 }
2424
2425 2262 av_refstruct_unref(&ps->pps_list[pps_id]);
2426 2262 ps->pps_list[pps_id] = pps;
2427
2428 2262 return 0;
2429
2430 4 err:
2431 4 av_refstruct_unref(&pps);
2432 4 return ret;
2433 }
2434
2435 808 void ff_hevc_ps_uninit(HEVCParamSets *ps)
2436 {
2437 int i;
2438
2439
2/2
✓ Branch 0 taken 12928 times.
✓ Branch 1 taken 808 times.
13736 for (i = 0; i < FF_ARRAY_ELEMS(ps->vps_list); i++)
2440 12928 av_refstruct_unref(&ps->vps_list[i]);
2441
2/2
✓ Branch 0 taken 12928 times.
✓ Branch 1 taken 808 times.
13736 for (i = 0; i < FF_ARRAY_ELEMS(ps->sps_list); i++)
2442 12928 av_refstruct_unref(&ps->sps_list[i]);
2443
2/2
✓ Branch 0 taken 51712 times.
✓ Branch 1 taken 808 times.
52520 for (i = 0; i < FF_ARRAY_ELEMS(ps->pps_list); i++)
2444 51712 av_refstruct_unref(&ps->pps_list[i]);
2445 808 }
2446
2447 32839 int ff_hevc_compute_poc2(unsigned log2_max_poc_lsb, int pocTid0, int poc_lsb, int nal_unit_type)
2448 {
2449 32839 int max_poc_lsb = 1 << log2_max_poc_lsb;
2450 32839 int prev_poc_lsb = pocTid0 % max_poc_lsb;
2451 32839 int prev_poc_msb = pocTid0 - prev_poc_lsb;
2452 int poc_msb;
2453
2454
4/4
✓ Branch 0 taken 11122 times.
✓ Branch 1 taken 21717 times.
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 11082 times.
32839 if (poc_lsb < prev_poc_lsb && prev_poc_lsb - poc_lsb >= max_poc_lsb / 2)
2455 40 poc_msb = prev_poc_msb + max_poc_lsb;
2456
4/4
✓ Branch 0 taken 15604 times.
✓ Branch 1 taken 17195 times.
✓ Branch 2 taken 344 times.
✓ Branch 3 taken 15260 times.
32799 else if (poc_lsb > prev_poc_lsb && poc_lsb - prev_poc_lsb > max_poc_lsb / 2)
2457 344 poc_msb = prev_poc_msb - max_poc_lsb;
2458 else
2459 32455 poc_msb = prev_poc_msb;
2460
2461 // For BLA picture types, POCmsb is set to 0.
2462
4/4
✓ Branch 0 taken 32824 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 32818 times.
✓ Branch 3 taken 6 times.
32839 if (nal_unit_type == HEVC_NAL_BLA_W_LP ||
2463
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 32815 times.
32818 nal_unit_type == HEVC_NAL_BLA_W_RADL ||
2464 nal_unit_type == HEVC_NAL_BLA_N_LP)
2465 24 poc_msb = 0;
2466
2467 32839 return poc_msb + poc_lsb;
2468 }
2469