FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/ps.c
Date: 2026-04-22 08:36:36
Exec Total Coverage
Lines: 1080 1532 70.5%
Functions: 29 30 96.7%
Branches: 662 1049 63.1%

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 av_log(avctx, AV_LOG_WARNING,
615 "Unsupported num_output_layer_sets: %d\n", vps->num_output_layer_sets);
616 return AVERROR_PATCHWELCOME;
617 }
618
619 27 sub_layers_max_present = get_bits1(gb); // vps_sub_layers_max_minus1_present_flag
620
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (sub_layers_max_present) {
621 for (int i = 0; i < vps->vps_max_layers; i++)
622 max_sub_layers[i] = sub_layers_max_present ? get_bits(gb, 3) + 1 :
623 vps->vps_max_sub_layers;
624 }
625
626
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
27 if (get_bits1(gb) /* max_tid_ref_present_flag */)
627 skip_bits(gb, 3); // max_tid_il_ref_pics_plus1
628
629 27 vps->default_ref_layers_active = get_bits1(gb);
630
631 27 nb_ptl = get_ue_golomb(gb) + 1;
632 /* idx [0] is signalled in base VPS, idx [1] is signalled at the
633 * start of VPS extension, indices 2+ are signalled here;
634 * we ignore all but the first one anyway */
635
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 27 times.
52 for (int i = 2; i < nb_ptl; i++) {
636 25 int profile_present = get_bits1(gb);
637
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)
638 return AVERROR_INVALIDDATA;
639 }
640
641 27 num_add_olss = get_ue_golomb(gb);
642
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (num_add_olss != 0) {
643 /* Since we don't implement support for independent output layer sets
644 * and auxiliary layers, this should never nonzero */
645 av_log(avctx, AV_LOG_ERROR, "Unexpected num_add_olss: %d\n", num_add_olss);
646 return AVERROR_PATCHWELCOME;
647 }
648
649 27 default_output_layer_idc = get_bits(gb, 2);
650
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (default_output_layer_idc != 0) {
651 av_log(avctx, AV_LOG_WARNING, "Unsupported default_output_layer_idc: %d\n",
652 default_output_layer_idc);
653 return AVERROR_PATCHWELCOME;
654 }
655
656 /* Consequence of established layer dependencies */
657
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2 times.
27 if (layer1_id_included &&
658 25 layer1_id_included != ((1ULL << vps->layer_id_in_nuh[0]) |
659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 (1ULL << vps->layer_id_in_nuh[1]))) {
660 av_log(avctx, AV_LOG_ERROR,
661 "Dependent layer not included in layer ID?\n");
662 return AVERROR_PATCHWELCOME;
663 }
664
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 25 times.
27 if (!layer1_id_included)
665 2 vps->ols[1] = 2;
666 else
667 25 vps->ols[1] = 3;
668
669
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)
670 2 skip_bits1(gb);
671
672
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (nb_ptl > 1)
673
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 27 times.
79 for (int j = 0; j < av_popcount64(vps->ols[1]); j++) {
674 52 int ptl_idx = get_bits(gb, av_ceil_log2(nb_ptl));
675
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (ptl_idx >= nb_ptl) {
676 av_log(avctx, AV_LOG_ERROR, "Invalid PTL index: %d\n", ptl_idx);
677 return AVERROR_INVALIDDATA;
678 }
679 }
680
681
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
27 if (get_ue_golomb_31(gb) != 0 /* vps_num_rep_formats_minus1 */) {
682 av_log(avctx, AV_LOG_ERROR, "Unexpected extra rep formats\n");
683 return AVERROR_PATCHWELCOME;
684 }
685
686 27 vps->rep_format.pic_width_in_luma_samples = get_bits(gb, 16);
687 27 vps->rep_format.pic_height_in_luma_samples = get_bits(gb, 16);
688
689
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
27 if (!get_bits1(gb) /* chroma_and_bit_depth_vps_present_flag */) {
690 av_log(avctx, AV_LOG_ERROR,
691 "chroma_and_bit_depth_vps_present_flag=0 in first rep_format\n");
692 return AVERROR_INVALIDDATA;
693 }
694 27 vps->rep_format.chroma_format_idc = get_bits(gb, 2);
695
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (vps->rep_format.chroma_format_idc == 3)
696 vps->rep_format.separate_colour_plane_flag = get_bits1(gb);
697 27 vps->rep_format.bit_depth_luma = get_bits(gb, 4) + 8;
698 27 vps->rep_format.bit_depth_chroma = get_bits(gb, 4) + 8;
699
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (vps->rep_format.bit_depth_luma > 16 ||
700
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 vps->rep_format.bit_depth_chroma > 16 ||
701
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 vps->rep_format.bit_depth_luma != vps->rep_format.bit_depth_chroma) {
702 av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %"PRIu8" %"PRIu8"\n",
703 vps->rep_format.bit_depth_luma, vps->rep_format.bit_depth_chroma);
704 return AVERROR_PATCHWELCOME;
705 }
706
707
2/2
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 2 times.
27 if (get_bits1(gb) /* conformance_window_vps_flag */) {
708 25 int vert_mult = hevc_sub_height_c[vps->rep_format.chroma_format_idc];
709 25 int horiz_mult = hevc_sub_width_c[vps->rep_format.chroma_format_idc];
710 25 vps->rep_format.conf_win_left_offset = get_ue_golomb(gb) * horiz_mult;
711 25 vps->rep_format.conf_win_right_offset = get_ue_golomb(gb) * horiz_mult;
712 25 vps->rep_format.conf_win_top_offset = get_ue_golomb(gb) * vert_mult;
713 25 vps->rep_format.conf_win_bottom_offset = get_ue_golomb(gb) * vert_mult;
714 }
715
716 27 vps->max_one_active_ref_layer = get_bits1(gb);
717 27 vps->poc_lsb_aligned = get_bits1(gb);
718
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 25 times.
27 if (!vps->num_direct_ref_layers[1])
719 2 vps->poc_lsb_not_present = get_bits1(gb) << 1;
720
721 27 sub_layer_flag_info_present_flag = get_bits1(gb);
722
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++) {
723 27 int sub_layer_dpb_info_present_flag = 1;
724
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)
725 sub_layer_dpb_info_present_flag = get_bits1(gb);
726
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (sub_layer_dpb_info_present_flag) {
727
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 27 times.
79 for (int k = 0; k < av_popcount64(vps->ols[1]); k++)
728 52 vps->dpb_size.max_dec_pic_buffering = get_ue_golomb_long(gb) + 1;
729 27 vps->dpb_size.max_num_reorder_pics = get_ue_golomb_long(gb);
730 27 vps->dpb_size.max_latency_increase = get_ue_golomb_long(gb) - 1;
731 }
732 }
733
734 27 direct_dep_type_len = get_ue_golomb_31(gb) + 2;
735
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (direct_dep_type_len > 32) {
736 av_log(avctx, AV_LOG_ERROR, "Invalid direct_dep_type_len: %d\n",
737 direct_dep_type_len);
738 return AVERROR_INVALIDDATA;
739 }
740
741 /* direct_depenency_all_layers_flag */
742
2/2
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 2 times.
27 if (get_bits1(gb)) {
743 25 direct_dep_type = get_bits_long(gb, direct_dep_type_len);
744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (direct_dep_type > HEVC_DEP_TYPE_BOTH) {
745 av_log(avctx, AV_LOG_WARNING, "Unsupported direct_dep_type: %d\n",
746 direct_dep_type);
747 return AVERROR_PATCHWELCOME;
748 }
749 }
750
751 27 non_vui_extension_length = get_ue_golomb(gb);
752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (non_vui_extension_length > 4096) {
753 av_log(avctx, AV_LOG_ERROR, "vps_non_vui_extension_length too large: %u\n",
754 non_vui_extension_length);
755 return AVERROR_INVALIDDATA;
756 }
757 27 skip_bits_long(gb, non_vui_extension_length * 8);
758
759
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
27 if (get_bits1(gb)) // vps_vui_present_flag
760 av_log(avctx, AV_LOG_WARNING, "VPS VUI not supported\n");
761
762 27 return 0;
763 }
764
765 1226 int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx,
766 HEVCParamSets *ps)
767 {
768 int i;
769 1226 int vps_id = get_bits(gb, 4);
770 1226 ptrdiff_t nal_size = get_bits_bytesize(gb, 1);
771 1226 int ret = AVERROR_INVALIDDATA;
772 1226 uint64_t layer1_id_included = 0;
773 unsigned vps_base_layer_internal_flag, vps_base_layer_available_flag;
774 HEVCVPS *vps;
775
776
2/2
✓ Branch 0 taken 483 times.
✓ Branch 1 taken 743 times.
1226 if (ps->vps_list[vps_id]) {
777 483 const HEVCVPS *vps1 = ps->vps_list[vps_id];
778
2/2
✓ Branch 0 taken 477 times.
✓ Branch 1 taken 6 times.
483 if (vps1->data_size == nal_size &&
779
2/2
✓ Branch 0 taken 474 times.
✓ Branch 1 taken 3 times.
477 !memcmp(vps1->data, gb->buffer, vps1->data_size))
780 474 return 0;
781 }
782
783 752 vps = av_refstruct_alloc_ext(sizeof(*vps), 0, NULL, hevc_vps_free);
784
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 752 times.
752 if (!vps)
785 return AVERROR(ENOMEM);
786
787 752 av_log(avctx, AV_LOG_DEBUG, "Decoding VPS\n");
788
789 752 vps->data_size = nal_size;
790 752 vps->data = av_memdup(gb->buffer, nal_size);
791
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 752 times.
752 if (!vps->data) {
792 ret = AVERROR(ENOMEM);
793 goto err;
794 }
795 752 vps->vps_id = vps_id;
796
797 752 vps_base_layer_internal_flag = get_bits1(gb);
798 752 vps_base_layer_available_flag = get_bits1(gb);
799
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) {
800 av_log(avctx, AV_LOG_ERROR,
801 "vps_base_layer_internal_flag or vps_base_layer_available_flag not set\n");
802 ret = AVERROR_PATCHWELCOME;
803 goto err;
804 }
805
806 752 vps->vps_max_layers = get_bits(gb, 6) + 1;
807 752 vps->vps_max_sub_layers = get_bits(gb, 3) + 1;
808 752 vps->vps_temporal_id_nesting_flag = get_bits1(gb);
809
810
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 752 times.
752 if (get_bits(gb, 16) != 0xffff) { // vps_reserved_ffff_16bits
811 av_log(avctx, AV_LOG_ERROR, "vps_reserved_ffff_16bits is not 0xffff\n");
812 goto err;
813 }
814
815
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 752 times.
752 if (vps->vps_max_sub_layers > HEVC_MAX_SUB_LAYERS) {
816 av_log(avctx, AV_LOG_ERROR, "vps_max_sub_layers out of range: %d\n",
817 vps->vps_max_sub_layers);
818 goto err;
819 }
820
821
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)
822 goto err;
823
824 752 vps->vps_sub_layer_ordering_info_present_flag = get_bits1(gb);
825
826
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;
827
2/2
✓ Branch 0 taken 810 times.
✓ Branch 1 taken 752 times.
1562 for (; i < vps->vps_max_sub_layers; i++) {
828 810 vps->vps_max_dec_pic_buffering[i] = get_ue_golomb_long(gb) + 1;
829 810 vps->vps_num_reorder_pics[i] = get_ue_golomb_long(gb);
830 810 vps->vps_max_latency_increase[i] = get_ue_golomb_long(gb) - 1;
831
832
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]) {
833 av_log(avctx, AV_LOG_ERROR, "vps_max_dec_pic_buffering_minus1 out of range: %d\n",
834 vps->vps_max_dec_pic_buffering[i] - 1);
835 goto err;
836 }
837
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) {
838 av_log(avctx, AV_LOG_WARNING, "vps_max_num_reorder_pics out of range: %d\n",
839 vps->vps_num_reorder_pics[i]);
840 if (avctx->err_recognition & AV_EF_EXPLODE)
841 goto err;
842 }
843 }
844
845 752 vps->vps_max_layer_id = get_bits(gb, 6);
846 752 vps->vps_num_layer_sets = get_ue_golomb_long(gb) + 1;
847
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 ||
848
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)) {
849 av_log(avctx, AV_LOG_ERROR, "too many layer_id_included_flags\n");
850 goto err;
851 }
852
853 752 vps->num_output_layer_sets = 1;
854 752 vps->ols[0] = 1;
855
856 // we support at most 2 layers, so ignore the others
857
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 727 times.
752 if (vps->vps_num_layer_sets > 1)
858 25 layer1_id_included = get_bits64(gb, vps->vps_max_layer_id + 1); // layer_id_included_flag
859
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 752 times.
752 if (vps->vps_num_layer_sets > 2)
860 skip_bits_long(gb, (vps->vps_num_layer_sets - 2) * (vps->vps_max_layer_id + 1));
861
862 752 vps->vps_timing_info_present_flag = get_bits1(gb);
863
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 730 times.
752 if (vps->vps_timing_info_present_flag) {
864 22 vps->vps_num_units_in_tick = get_bits_long(gb, 32);
865 22 vps->vps_time_scale = get_bits_long(gb, 32);
866 22 vps->vps_poc_proportional_to_timing_flag = get_bits1(gb);
867
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (vps->vps_poc_proportional_to_timing_flag)
868 22 vps->vps_num_ticks_poc_diff_one = get_ue_golomb_long(gb) + 1;
869 22 vps->vps_num_hrd_parameters = get_ue_golomb_long(gb);
870
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (vps->vps_num_hrd_parameters > (unsigned)vps->vps_num_layer_sets) {
871 av_log(avctx, AV_LOG_ERROR,
872 "vps_num_hrd_parameters %d is invalid\n", vps->vps_num_hrd_parameters);
873 goto err;
874 }
875
876
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 19 times.
22 if (vps->vps_num_hrd_parameters) {
877 3 vps->hdr = av_calloc(vps->vps_num_hrd_parameters, sizeof(*vps->hdr));
878
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!vps->hdr)
879 goto err;
880 }
881
882
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 22 times.
25 for (i = 0; i < vps->vps_num_hrd_parameters; i++) {
883 3 int common_inf_present = 1;
884
885 3 get_ue_golomb_long(gb); // hrd_layer_set_idx
886
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (i)
887 common_inf_present = get_bits1(gb);
888 3 decode_hrd(gb, common_inf_present, &vps->hdr[i],
889 3 vps->vps_max_sub_layers);
890 }
891 }
892
893 752 vps->nb_layers = 1;
894 752 vps->layer_idx[0] = 0;
895
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++)
896 46624 vps->layer_idx[i] = -1;
897
898
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 */
899 27 int ret = decode_vps_ext(gb, avctx, vps, layer1_id_included);
900
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret == AVERROR_PATCHWELCOME) {
901 /* If alpha layer info was already parsed, preserve it for alpha decoding */
902 if (!(avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT)) &&
903 vps->nb_layers == 2 &&
904 vps->layer_id_in_nuh[1] &&
905 (vps->scalability_mask_flag & HEVC_SCALABILITY_AUXILIARY)) {
906 av_log(avctx, AV_LOG_WARNING,
907 "Broken VPS extension, treating as alpha video\n");
908 /* If alpha layer has no direct dependency on base layer,
909 * assume poc_lsb_not_present for the alpha layer, so that
910 * IDR slices on that layer won't read pic_order_cnt_lsb.
911 * This matches the behavior of Apple VideoToolbox encoders. */
912 if (!vps->num_direct_ref_layers[1])
913 vps->poc_lsb_not_present |= 1 << 1;
914 } else {
915 vps->nb_layers = 1;
916 av_log(avctx, AV_LOG_WARNING, "Ignoring unsupported VPS extension\n");
917 }
918 ret = 0;
919
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 } else if (ret < 0)
920 goto err;
921 }
922
923
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 752 times.
752 if (get_bits_left(gb) < 0) {
924 av_log(avctx, AV_LOG_ERROR,
925 "Overread VPS by %d bits\n", -get_bits_left(gb));
926 if (ps->vps_list[vps_id])
927 goto err;
928 }
929
930 752 remove_vps(ps, vps_id);
931 752 ps->vps_list[vps_id] = vps;
932
933 752 return 0;
934
935 err:
936 av_refstruct_unref(&vps);
937 return ret;
938 }
939
940 332 static void decode_vui(GetBitContext *gb, AVCodecContext *avctx,
941 int apply_defdispwin, HEVCSPS *sps)
942 {
943 332 VUI backup_vui, *vui = &sps->vui;
944 GetBitContext backup;
945 332 int alt = 0;
946
947 332 ff_h2645_decode_common_vui_params(gb, &sps->vui.common, avctx);
948
949
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 230 times.
332 if (vui->common.video_signal_type_present_flag) {
950
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)
951 7 sps->pix_fmt = AV_PIX_FMT_YUVJ420P;
952
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 12 times.
102 if (vui->common.colour_description_present_flag) {
953
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
90 if (vui->common.matrix_coeffs == AVCOL_SPC_RGB) {
954 switch (sps->pix_fmt) {
955 case AV_PIX_FMT_YUV444P:
956 sps->pix_fmt = AV_PIX_FMT_GBRP;
957 break;
958 case AV_PIX_FMT_YUV444P10:
959 sps->pix_fmt = AV_PIX_FMT_GBRP10;
960 break;
961 case AV_PIX_FMT_YUV444P12:
962 sps->pix_fmt = AV_PIX_FMT_GBRP12;
963 break;
964 }
965 }
966 }
967 }
968
969 332 vui->neutra_chroma_indication_flag = get_bits1(gb);
970 332 vui->field_seq_flag = get_bits1(gb);
971 332 vui->frame_field_info_present_flag = get_bits1(gb);
972
973 // Backup context in case an alternate header is detected
974 332 memcpy(&backup, gb, sizeof(backup));
975 332 memcpy(&backup_vui, vui, sizeof(backup_vui));
976
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) {
977 vui->default_display_window_flag = 0;
978 av_log(avctx, AV_LOG_WARNING, "Invalid default display window\n");
979 } else
980 332 vui->default_display_window_flag = get_bits1(gb);
981
982
2/2
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 114 times.
332 if (vui->default_display_window_flag) {
983 114 int vert_mult = hevc_sub_height_c[sps->chroma_format_idc];
984 114 int horiz_mult = hevc_sub_width_c[sps->chroma_format_idc];
985 114 vui->def_disp_win.left_offset = get_ue_golomb_long(gb) * horiz_mult;
986 114 vui->def_disp_win.right_offset = get_ue_golomb_long(gb) * horiz_mult;
987 114 vui->def_disp_win.top_offset = get_ue_golomb_long(gb) * vert_mult;
988 114 vui->def_disp_win.bottom_offset = get_ue_golomb_long(gb) * vert_mult;
989
990
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 54 times.
114 if (apply_defdispwin &&
991
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
54 avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {
992 av_log(avctx, AV_LOG_DEBUG,
993 "discarding vui default display window, "
994 "original values are l:%u r:%u t:%u b:%u\n",
995 vui->def_disp_win.left_offset,
996 vui->def_disp_win.right_offset,
997 vui->def_disp_win.top_offset,
998 vui->def_disp_win.bottom_offset);
999
1000 vui->def_disp_win.left_offset =
1001 vui->def_disp_win.right_offset =
1002 vui->def_disp_win.top_offset =
1003 vui->def_disp_win.bottom_offset = 0;
1004 }
1005 }
1006
1007 332 timing_info:
1008 332 vui->vui_timing_info_present_flag = get_bits1(gb);
1009
1010
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 207 times.
332 if (vui->vui_timing_info_present_flag) {
1011
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) {
1012 // The alternate syntax seem to have timing info located
1013 // at where def_disp_win is normally located
1014 av_log(avctx, AV_LOG_WARNING,
1015 "Strange VUI timing information, retrying...\n");
1016 memcpy(vui, &backup_vui, sizeof(backup_vui));
1017 memcpy(gb, &backup, sizeof(backup));
1018 alt = 1;
1019 goto timing_info;
1020 }
1021 125 vui->vui_num_units_in_tick = get_bits_long(gb, 32);
1022 125 vui->vui_time_scale = get_bits_long(gb, 32);
1023
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if (alt) {
1024 av_log(avctx, AV_LOG_INFO, "Retry got %"PRIu32"/%"PRIu32" fps\n",
1025 vui->vui_time_scale, vui->vui_num_units_in_tick);
1026 }
1027 125 vui->vui_poc_proportional_to_timing_flag = get_bits1(gb);
1028
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 87 times.
125 if (vui->vui_poc_proportional_to_timing_flag)
1029 38 vui->vui_num_ticks_poc_diff_one_minus1 = get_ue_golomb_long(gb);
1030 125 vui->vui_hrd_parameters_present_flag = get_bits1(gb);
1031
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 67 times.
125 if (vui->vui_hrd_parameters_present_flag)
1032 58 decode_hrd(gb, 1, &sps->hdr, sps->max_sub_layers);
1033 }
1034
1035 332 vui->bitstream_restriction_flag = get_bits1(gb);
1036
2/2
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 202 times.
332 if (vui->bitstream_restriction_flag) {
1037
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) {
1038 av_log(avctx, AV_LOG_WARNING,
1039 "Strange VUI bitstream restriction information, retrying"
1040 " from timing information...\n");
1041 memcpy(vui, &backup_vui, sizeof(backup_vui));
1042 memcpy(gb, &backup, sizeof(backup));
1043 alt = 1;
1044 goto timing_info;
1045 }
1046 130 vui->tiles_fixed_structure_flag = get_bits1(gb);
1047 130 vui->motion_vectors_over_pic_boundaries_flag = get_bits1(gb);
1048 130 vui->restricted_ref_pic_lists_flag = get_bits1(gb);
1049 130 vui->min_spatial_segmentation_idc = get_ue_golomb_long(gb);
1050 130 vui->max_bytes_per_pic_denom = get_ue_golomb_long(gb);
1051 130 vui->max_bits_per_min_cu_denom = get_ue_golomb_long(gb);
1052 130 vui->log2_max_mv_length_horizontal = get_ue_golomb_long(gb);
1053 130 vui->log2_max_mv_length_vertical = get_ue_golomb_long(gb);
1054 }
1055
1056
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) {
1057 // XXX: Alternate syntax when sps_range_extension_flag != 0?
1058 av_log(avctx, AV_LOG_WARNING,
1059 "Overread in VUI, retrying from timing information...\n");
1060 memcpy(vui, &backup_vui, sizeof(backup_vui));
1061 memcpy(gb, &backup, sizeof(backup));
1062 alt = 1;
1063 goto timing_info;
1064 }
1065 332 }
1066
1067 204 static void set_default_scaling_list_data(ScalingList *sl)
1068 {
1069 int matrixId;
1070
1071
2/2
✓ Branch 0 taken 1224 times.
✓ Branch 1 taken 204 times.
1428 for (matrixId = 0; matrixId < 6; matrixId++) {
1072 // 4x4 default is 16
1073 1224 memset(sl->sl[0][matrixId], 16, 16);
1074 1224 sl->sl_dc[0][matrixId] = 16; // default for 16x16
1075 1224 sl->sl_dc[1][matrixId] = 16; // default for 32x32
1076 }
1077 204 memcpy(sl->sl[1][0], default_scaling_list_intra, 64);
1078 204 memcpy(sl->sl[1][1], default_scaling_list_intra, 64);
1079 204 memcpy(sl->sl[1][2], default_scaling_list_intra, 64);
1080 204 memcpy(sl->sl[1][3], default_scaling_list_inter, 64);
1081 204 memcpy(sl->sl[1][4], default_scaling_list_inter, 64);
1082 204 memcpy(sl->sl[1][5], default_scaling_list_inter, 64);
1083 204 memcpy(sl->sl[2][0], default_scaling_list_intra, 64);
1084 204 memcpy(sl->sl[2][1], default_scaling_list_intra, 64);
1085 204 memcpy(sl->sl[2][2], default_scaling_list_intra, 64);
1086 204 memcpy(sl->sl[2][3], default_scaling_list_inter, 64);
1087 204 memcpy(sl->sl[2][4], default_scaling_list_inter, 64);
1088 204 memcpy(sl->sl[2][5], default_scaling_list_inter, 64);
1089 204 memcpy(sl->sl[3][0], default_scaling_list_intra, 64);
1090 204 memcpy(sl->sl[3][1], default_scaling_list_intra, 64);
1091 204 memcpy(sl->sl[3][2], default_scaling_list_intra, 64);
1092 204 memcpy(sl->sl[3][3], default_scaling_list_inter, 64);
1093 204 memcpy(sl->sl[3][4], default_scaling_list_inter, 64);
1094 204 memcpy(sl->sl[3][5], default_scaling_list_inter, 64);
1095 204 }
1096
1097 165 static int scaling_list_data(GetBitContext *gb, AVCodecContext *avctx,
1098 ScalingList *sl, const HEVCSPS *sps)
1099 {
1100 uint8_t scaling_list_pred_mode_flag;
1101 uint8_t scaling_list_dc_coef[2][6];
1102 int size_id, matrix_id, pos;
1103 int i;
1104
1105
2/2
✓ Branch 0 taken 660 times.
✓ Branch 1 taken 165 times.
825 for (size_id = 0; size_id < 4; size_id++)
1106
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)) {
1107 3300 scaling_list_pred_mode_flag = get_bits1(gb);
1108
2/2
✓ Branch 0 taken 979 times.
✓ Branch 1 taken 2321 times.
3300 if (!scaling_list_pred_mode_flag) {
1109 979 unsigned int delta = get_ue_golomb_long(gb);
1110 /* Only need to handle non-zero delta. Zero means default,
1111 * which should already be in the arrays. */
1112
2/2
✓ Branch 0 taken 766 times.
✓ Branch 1 taken 213 times.
979 if (delta) {
1113 // Copy from previous array.
1114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 766 times.
766 delta *= (size_id == 3) ? 3 : 1;
1115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 766 times.
766 if (matrix_id < delta) {
1116 av_log(avctx, AV_LOG_ERROR,
1117 "Invalid delta in scaling list data: %d.\n", delta);
1118 return AVERROR_INVALIDDATA;
1119 }
1120
1121
2/2
✓ Branch 0 taken 482 times.
✓ Branch 1 taken 284 times.
766 memcpy(sl->sl[size_id][matrix_id],
1122 766 sl->sl[size_id][matrix_id - delta],
1123 size_id > 0 ? 64 : 16);
1124
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 616 times.
766 if (size_id > 1)
1125 150 sl->sl_dc[size_id - 2][matrix_id] = sl->sl_dc[size_id - 2][matrix_id - delta];
1126 }
1127 } else {
1128 int next_coef, coef_num;
1129 int32_t scaling_list_delta_coef;
1130
1131 2321 next_coef = 8;
1132 2321 coef_num = FFMIN(64, 1 << (4 + (size_id << 1)));
1133
2/2
✓ Branch 0 taken 1091 times.
✓ Branch 1 taken 1230 times.
2321 if (size_id > 1) {
1134 1091 int scaling_list_coeff_minus8 = get_se_golomb(gb);
1135
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 ||
1136 scaling_list_coeff_minus8 > 247)
1137 return AVERROR_INVALIDDATA;
1138 1091 scaling_list_dc_coef[size_id - 2][matrix_id] = scaling_list_coeff_minus8 + 8;
1139 1091 next_coef = scaling_list_dc_coef[size_id - 2][matrix_id];
1140 1091 sl->sl_dc[size_id - 2][matrix_id] = next_coef;
1141 }
1142
2/2
✓ Branch 0 taken 116480 times.
✓ Branch 1 taken 2321 times.
118801 for (i = 0; i < coef_num; i++) {
1143
2/2
✓ Branch 0 taken 10688 times.
✓ Branch 1 taken 105792 times.
116480 if (size_id == 0)
1144 10688 pos = 4 * ff_hevc_diag_scan4x4_y[i] +
1145 10688 ff_hevc_diag_scan4x4_x[i];
1146 else
1147 105792 pos = 8 * ff_hevc_diag_scan8x8_y[i] +
1148 105792 ff_hevc_diag_scan8x8_x[i];
1149
1150 116480 scaling_list_delta_coef = get_se_golomb(gb);
1151 116480 next_coef = (next_coef + 256U + scaling_list_delta_coef) % 256;
1152 116480 sl->sl[size_id][matrix_id][pos] = next_coef;
1153 }
1154 }
1155 }
1156
1157
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 122 times.
165 if (sps->chroma_format_idc == 3) {
1158
2/2
✓ Branch 0 taken 2752 times.
✓ Branch 1 taken 43 times.
2795 for (i = 0; i < 64; i++) {
1159 2752 sl->sl[3][1][i] = sl->sl[2][1][i];
1160 2752 sl->sl[3][2][i] = sl->sl[2][2][i];
1161 2752 sl->sl[3][4][i] = sl->sl[2][4][i];
1162 2752 sl->sl[3][5][i] = sl->sl[2][5][i];
1163 }
1164 43 sl->sl_dc[1][1] = sl->sl_dc[0][1];
1165 43 sl->sl_dc[1][2] = sl->sl_dc[0][2];
1166 43 sl->sl_dc[1][4] = sl->sl_dc[0][4];
1167 43 sl->sl_dc[1][5] = sl->sl_dc[0][5];
1168 }
1169
1170
1171 165 return 0;
1172 }
1173
1174 1253 static int map_pixel_format(AVCodecContext *avctx, HEVCSPS *sps)
1175 {
1176 const AVPixFmtDescriptor *desc;
1177
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) {
1178 1080 case 8:
1179
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;
1180
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;
1181
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;
1182
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;
1183 1080 break;
1184 case 9:
1185 if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY9;
1186 if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P9;
1187 if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P9;
1188 if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P9;
1189 break;
1190 150 case 10:
1191
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;
1192
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;
1193
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;
1194
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;
1195 150 break;
1196 23 case 12:
1197
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;
1198
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;
1199
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;
1200
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;
1201 23 break;
1202 default:
1203 av_log(avctx, AV_LOG_ERROR,
1204 "The following bit-depths are currently specified: 8, 9, 10 and 12 bits, "
1205 "chroma_format_idc is %d, depth is %d\n",
1206 sps->chroma_format_idc, sps->bit_depth);
1207 return AVERROR_INVALIDDATA;
1208 }
1209
1210 1253 desc = av_pix_fmt_desc_get(sps->pix_fmt);
1211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (!desc)
1212 return AVERROR(EINVAL);
1213
1214 1253 sps->hshift[0] = sps->vshift[0] = 0;
1215 1253 sps->hshift[2] = sps->hshift[1] = desc->log2_chroma_w;
1216 1253 sps->vshift[2] = sps->vshift[1] = desc->log2_chroma_h;
1217
1218 1253 sps->pixel_shift = sps->bit_depth > 8;
1219
1220 1253 return 0;
1221 }
1222
1223 1253 int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id,
1224 unsigned nuh_layer_id, int apply_defdispwin,
1225 const HEVCVPS * const *vps_list, AVCodecContext *avctx)
1226 {
1227 HEVCWindow *ow;
1228 1253 int ret = 0;
1229 int bit_depth_chroma, num_comps, multi_layer_ext;
1230 int vps_max_sub_layers;
1231 int i;
1232
1233 // Coded parameters
1234
1235 1253 sps->vps_id = get_bits(gb, 4);
1236
1237
1/2
✓ Branch 0 taken 1253 times.
✗ Branch 1 not taken.
1253 if (vps_list) {
1238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (!vps_list[sps->vps_id]) {
1239 av_log(avctx, AV_LOG_ERROR, "VPS %d does not exist\n",
1240 sps->vps_id);
1241 return AVERROR_INVALIDDATA;
1242 }
1243 1253 sps->vps = av_refstruct_ref_c(vps_list[sps->vps_id]);
1244 }
1245
1246 1253 sps->max_sub_layers = get_bits(gb, 3) + 1;
1247
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1223 times.
1283 multi_layer_ext = nuh_layer_id > 0 &&
1248
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 2 times.
30 sps->max_sub_layers == HEVC_MAX_SUB_LAYERS + 1;
1249
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1225 times.
1253 if (multi_layer_ext) {
1250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (!sps->vps)
1251 return AVERROR(EINVAL);
1252
1253 28 sps->max_sub_layers = sps->vps->vps_max_sub_layers;
1254 }
1255 3759 vps_max_sub_layers = sps->vps ? sps->vps->vps_max_sub_layers
1256
1/2
✓ Branch 0 taken 1253 times.
✗ Branch 1 not taken.
1253 : FFMIN(sps->max_sub_layers, HEVC_MAX_SUB_LAYERS);
1257
1258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (sps->max_sub_layers > vps_max_sub_layers) {
1259 av_log(avctx, AV_LOG_ERROR, "sps_max_sub_layers out of range: %d\n",
1260 sps->max_sub_layers);
1261 return AVERROR_INVALIDDATA;
1262 }
1263
1264
2/2
✓ Branch 0 taken 1225 times.
✓ Branch 1 taken 28 times.
1253 if (!multi_layer_ext) {
1265 1225 sps->temporal_id_nesting = get_bits(gb, 1);
1266
1267
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)
1268 return ret;
1269 } else {
1270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 sps->temporal_id_nesting = sps->max_sub_layers > 1 ?
1271 sps->vps->vps_max_sub_layers : 1;
1272 }
1273
1274 1253 *sps_id = get_ue_golomb_long(gb);
1275
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (*sps_id >= HEVC_MAX_SPS_COUNT) {
1276 av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", *sps_id);
1277 return AVERROR_INVALIDDATA;
1278 }
1279
1280
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1225 times.
1253 if (multi_layer_ext) {
1281 28 const RepFormat *rf = &sps->vps->rep_format;
1282
1283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (sps->vps->nb_layers == 1) {
1284 av_log(avctx, AV_LOG_WARNING, "SPS %d references an unsupported VPS extension. Ignoring\n",
1285 *sps_id);
1286 return AVERROR(ENOSYS);
1287 }
1288
1289
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
1290 get_bits(gb, 8)) { // sps_rep_format_idx
1291 av_log(avctx, AV_LOG_ERROR, "sps_rep_format_idx!=0\n");
1292 return AVERROR_PATCHWELCOME;
1293 }
1294
1295 28 sps->separate_colour_plane = rf->separate_colour_plane_flag;
1296
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 sps->chroma_format_idc = sps->separate_colour_plane ? 0 :
1297 28 rf->chroma_format_idc;
1298 28 sps->bit_depth = rf->bit_depth_luma;
1299 28 sps->width = rf->pic_width_in_luma_samples;
1300 28 sps->height = rf->pic_height_in_luma_samples;
1301
1302 28 sps->pic_conf_win.left_offset = rf->conf_win_left_offset;
1303 28 sps->pic_conf_win.right_offset = rf->conf_win_right_offset;
1304 28 sps->pic_conf_win.top_offset = rf->conf_win_top_offset;
1305 28 sps->pic_conf_win.bottom_offset = rf->conf_win_bottom_offset;
1306
1307 } else {
1308 1225 sps->chroma_format_idc = get_ue_golomb_long(gb);
1309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1225 times.
1225 if (sps->chroma_format_idc > 3U) {
1310 av_log(avctx, AV_LOG_ERROR, "chroma_format_idc %d is invalid\n", sps->chroma_format_idc);
1311 return AVERROR_INVALIDDATA;
1312 }
1313
1314
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1193 times.
1225 if (sps->chroma_format_idc == 3)
1315 32 sps->separate_colour_plane = get_bits1(gb);
1316
1317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1225 times.
1225 if (sps->separate_colour_plane)
1318 sps->chroma_format_idc = 0;
1319
1320 1225 sps->width = get_ue_golomb_long(gb);
1321 1225 sps->height = get_ue_golomb_long(gb);
1322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1225 times.
1225 if ((ret = av_image_check_size(sps->width,
1323 1225 sps->height, 0, avctx)) < 0)
1324 return ret;
1325
1326 1225 sps->conformance_window = get_bits1(gb);
1327
2/2
✓ Branch 0 taken 1012 times.
✓ Branch 1 taken 213 times.
1225 if (sps->conformance_window) {
1328 1012 int vert_mult = hevc_sub_height_c[sps->chroma_format_idc];
1329 1012 int horiz_mult = hevc_sub_width_c[sps->chroma_format_idc];
1330 1012 sps->pic_conf_win.left_offset = get_ue_golomb_long(gb) * horiz_mult;
1331 1012 sps->pic_conf_win.right_offset = get_ue_golomb_long(gb) * horiz_mult;
1332 1012 sps->pic_conf_win.top_offset = get_ue_golomb_long(gb) * vert_mult;
1333 1012 sps->pic_conf_win.bottom_offset = get_ue_golomb_long(gb) * vert_mult;
1334
1335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1012 times.
1012 if (avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {
1336 av_log(avctx, AV_LOG_DEBUG,
1337 "discarding sps conformance window, "
1338 "original values are l:%u r:%u t:%u b:%u\n",
1339 sps->pic_conf_win.left_offset,
1340 sps->pic_conf_win.right_offset,
1341 sps->pic_conf_win.top_offset,
1342 sps->pic_conf_win.bottom_offset);
1343
1344 sps->pic_conf_win.left_offset =
1345 sps->pic_conf_win.right_offset =
1346 sps->pic_conf_win.top_offset =
1347 sps->pic_conf_win.bottom_offset = 0;
1348 }
1349 }
1350
1351 1225 sps->bit_depth = get_ue_golomb_31(gb) + 8;
1352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1225 times.
1225 if (sps->bit_depth > 16) {
1353 av_log(avctx, AV_LOG_ERROR, "Luma bit depth (%d) is out of range\n",
1354 sps->bit_depth);
1355 return AVERROR_INVALIDDATA;
1356 }
1357 1225 bit_depth_chroma = get_ue_golomb_31(gb) + 8;
1358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1225 times.
1225 if (bit_depth_chroma > 16) {
1359 av_log(avctx, AV_LOG_ERROR, "Chroma bit depth (%d) is out of range\n",
1360 bit_depth_chroma);
1361 return AVERROR_INVALIDDATA;
1362 }
1363
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) {
1364 av_log(avctx, AV_LOG_ERROR,
1365 "Luma bit depth (%d) is different from chroma bit depth (%d), "
1366 "this is unsupported.\n",
1367 sps->bit_depth, bit_depth_chroma);
1368 return AVERROR_INVALIDDATA;
1369 }
1370 1225 sps->bit_depth_chroma = bit_depth_chroma;
1371 }
1372
1373 1253 sps->output_window = sps->pic_conf_win;
1374
1375 1253 ret = map_pixel_format(avctx, sps);
1376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (ret < 0)
1377 return ret;
1378
1379 1253 sps->log2_max_poc_lsb = get_ue_golomb_long(gb) + 4;
1380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (sps->log2_max_poc_lsb > 16) {
1381 av_log(avctx, AV_LOG_ERROR, "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n",
1382 sps->log2_max_poc_lsb - 4);
1383 return AVERROR_INVALIDDATA;
1384 }
1385
1386
2/2
✓ Branch 0 taken 1225 times.
✓ Branch 1 taken 28 times.
1253 if (!multi_layer_ext) {
1387 int start;
1388
1389 1225 sps->sublayer_ordering_info = get_bits1(gb);
1390
2/2
✓ Branch 0 taken 161 times.
✓ Branch 1 taken 1064 times.
1225 start = sps->sublayer_ordering_info ? 0 : sps->max_sub_layers - 1;
1391
2/2
✓ Branch 0 taken 1372 times.
✓ Branch 1 taken 1225 times.
2597 for (i = start; i < sps->max_sub_layers; i++) {
1392 1372 sps->temporal_layer[i].max_dec_pic_buffering = get_ue_golomb_long(gb) + 1;
1393 1372 sps->temporal_layer[i].num_reorder_pics = get_ue_golomb_long(gb);
1394 1372 sps->temporal_layer[i].max_latency_increase = get_ue_golomb_long(gb) - 1;
1395
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) {
1396 av_log(avctx, AV_LOG_ERROR, "sps_max_dec_pic_buffering_minus1 out of range: %d\n",
1397 sps->temporal_layer[i].max_dec_pic_buffering - 1U);
1398 return AVERROR_INVALIDDATA;
1399 }
1400
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) {
1401 av_log(avctx, AV_LOG_WARNING, "sps_max_num_reorder_pics out of range: %d\n",
1402 sps->temporal_layer[i].num_reorder_pics);
1403 if (avctx->err_recognition & AV_EF_EXPLODE ||
1404 sps->temporal_layer[i].num_reorder_pics > HEVC_MAX_DPB_SIZE - 1) {
1405 return AVERROR_INVALIDDATA;
1406 }
1407 sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[i].num_reorder_pics + 1;
1408 }
1409 }
1410
1411
2/2
✓ Branch 0 taken 161 times.
✓ Branch 1 taken 1064 times.
1225 if (!sps->sublayer_ordering_info) {
1412
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 161 times.
243 for (i = 0; i < start; i++) {
1413 82 sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[start].max_dec_pic_buffering;
1414 82 sps->temporal_layer[i].num_reorder_pics = sps->temporal_layer[start].num_reorder_pics;
1415 82 sps->temporal_layer[i].max_latency_increase = sps->temporal_layer[start].max_latency_increase;
1416 }
1417 }
1418 } else {
1419
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
56 for (int i = 0; i < sps->max_sub_layers; i++) {
1420 28 sps->temporal_layer[i].max_dec_pic_buffering = sps->vps->dpb_size.max_dec_pic_buffering;
1421 28 sps->temporal_layer[i].num_reorder_pics = sps->vps->dpb_size.max_num_reorder_pics;
1422 28 sps->temporal_layer[i].max_latency_increase = sps->vps->dpb_size.max_latency_increase;
1423 }
1424 }
1425
1426 1253 sps->log2_min_cb_size = get_ue_golomb_long(gb) + 3;
1427 1253 sps->log2_diff_max_min_coding_block_size = get_ue_golomb_long(gb);
1428 1253 sps->log2_min_tb_size = get_ue_golomb_long(gb) + 2;
1429 1253 sps->log2_diff_max_min_transform_block_size = get_ue_golomb_long(gb);
1430 1253 sps->log2_max_trafo_size = sps->log2_diff_max_min_transform_block_size +
1431 1253 sps->log2_min_tb_size;
1432
1433
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) {
1434 av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_min_cb_size", sps->log2_min_cb_size);
1435 return AVERROR_INVALIDDATA;
1436 }
1437
1438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (sps->log2_diff_max_min_coding_block_size > 30) {
1439 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);
1440 return AVERROR_INVALIDDATA;
1441 }
1442
1443
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) {
1444 av_log(avctx, AV_LOG_ERROR, "Invalid value for log2_min_tb_size");
1445 return AVERROR_INVALIDDATA;
1446 }
1447
1448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (sps->log2_diff_max_min_transform_block_size > 30) {
1449 av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_diff_max_min_transform_block_size",
1450 sps->log2_diff_max_min_transform_block_size);
1451 return AVERROR_INVALIDDATA;
1452 }
1453
1454 1253 sps->max_transform_hierarchy_depth_inter = get_ue_golomb_long(gb);
1455 1253 sps->max_transform_hierarchy_depth_intra = get_ue_golomb_long(gb);
1456
1457 1253 sps->scaling_list_enabled = get_bits1(gb);
1458
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 1180 times.
1253 if (sps->scaling_list_enabled) {
1459 73 set_default_scaling_list_data(&sps->scaling_list);
1460
1461
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
1462 av_log(avctx, AV_LOG_ERROR, "sps_infer_scaling_list_flag=1 not supported\n");
1463 return AVERROR_PATCHWELCOME;
1464 }
1465
1466
2/2
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 39 times.
73 if (get_bits1(gb)) {
1467 34 ret = scaling_list_data(gb, avctx, &sps->scaling_list, sps);
1468
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (ret < 0)
1469 return ret;
1470 }
1471 }
1472
1473 1253 sps->amp_enabled = get_bits1(gb);
1474 1253 sps->sao_enabled = get_bits1(gb);
1475
1476 1253 sps->pcm_enabled = get_bits1(gb);
1477
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 1178 times.
1253 if (sps->pcm_enabled) {
1478 75 sps->pcm.bit_depth = get_bits(gb, 4) + 1;
1479 75 sps->pcm.bit_depth_chroma = get_bits(gb, 4) + 1;
1480 75 sps->pcm.log2_min_pcm_cb_size = get_ue_golomb_long(gb) + 3;
1481 150 sps->pcm.log2_max_pcm_cb_size = sps->pcm.log2_min_pcm_cb_size +
1482 75 get_ue_golomb_long(gb);
1483
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) {
1484 av_log(avctx, AV_LOG_ERROR,
1485 "PCM bit depth (%d, %d) is greater than normal bit depth (%d)\n",
1486 sps->pcm.bit_depth, sps->pcm.bit_depth_chroma, sps->bit_depth);
1487 return AVERROR_INVALIDDATA;
1488 }
1489
1490 75 sps->pcm_loop_filter_disabled = get_bits1(gb);
1491 }
1492
1493 1253 sps->nb_st_rps = get_ue_golomb_long(gb);
1494
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (sps->nb_st_rps > HEVC_MAX_SHORT_TERM_REF_PIC_SETS) {
1495 av_log(avctx, AV_LOG_ERROR, "Too many short term RPS: %d.\n",
1496 sps->nb_st_rps);
1497 return AVERROR_INVALIDDATA;
1498 }
1499
2/2
✓ Branch 0 taken 10352 times.
✓ Branch 1 taken 1253 times.
11605 for (i = 0; i < sps->nb_st_rps; i++) {
1500
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],
1501 sps, 0)) < 0)
1502 return ret;
1503 }
1504
1505 1253 sps->long_term_ref_pics_present = get_bits1(gb);
1506
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 1148 times.
1253 if (sps->long_term_ref_pics_present) {
1507 105 sps->num_long_term_ref_pics_sps = get_ue_golomb_long(gb);
1508
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) {
1509 av_log(avctx, AV_LOG_ERROR, "Too many long term ref pics: %d.\n",
1510 sps->num_long_term_ref_pics_sps);
1511 return AVERROR_INVALIDDATA;
1512 }
1513
1514 105 sps->used_by_curr_pic_lt = 0;
1515
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++) {
1516 64 sps->lt_ref_pic_poc_lsb_sps[i] = get_bits(gb, sps->log2_max_poc_lsb);
1517 64 sps->used_by_curr_pic_lt |= get_bits1(gb) << i;
1518 }
1519 }
1520
1521 1253 sps->temporal_mvp_enabled = get_bits1(gb);
1522 1253 sps->strong_intra_smoothing_enabled = get_bits1(gb);
1523 1253 sps->vui.common.sar = (AVRational){0, 1};
1524 1253 sps->vui_present = get_bits1(gb);
1525
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 921 times.
1253 if (sps->vui_present)
1526 332 decode_vui(gb, avctx, apply_defdispwin, sps);
1527
1528 1253 sps->extension_present = get_bits1(gb);
1529
2/2
✓ Branch 0 taken 135 times.
✓ Branch 1 taken 1118 times.
1253 if (sps->extension_present) {
1530 135 sps->range_extension = get_bits1(gb);
1531 135 sps->multilayer_extension = get_bits1(gb);
1532 135 sps->sps_3d_extension = get_bits1(gb);
1533 135 sps->scc_extension = get_bits1(gb);
1534 135 skip_bits(gb, 4); // sps_extension_4bits
1535
1536
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 79 times.
135 if (sps->range_extension) {
1537 56 sps->transform_skip_rotation_enabled = get_bits1(gb);
1538 56 sps->transform_skip_context_enabled = get_bits1(gb);
1539 56 sps->implicit_rdpcm_enabled = get_bits1(gb);
1540 56 sps->explicit_rdpcm_enabled = get_bits1(gb);
1541
1542 56 sps->extended_precision_processing = get_bits1(gb);
1543
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (sps->extended_precision_processing)
1544 av_log(avctx, AV_LOG_WARNING,
1545 "extended_precision_processing_flag not yet implemented\n");
1546
1547 56 sps->intra_smoothing_disabled = get_bits1(gb);
1548 56 sps->high_precision_offsets_enabled = get_bits1(gb);
1549
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 18 times.
56 if (sps->high_precision_offsets_enabled)
1550 38 av_log(avctx, AV_LOG_WARNING,
1551 "high_precision_offsets_enabled_flag not yet implemented\n");
1552
1553 56 sps->persistent_rice_adaptation_enabled = get_bits1(gb);
1554
1555 56 sps->cabac_bypass_alignment_enabled = get_bits1(gb);
1556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (sps->cabac_bypass_alignment_enabled)
1557 av_log(avctx, AV_LOG_WARNING,
1558 "cabac_bypass_alignment_enabled_flag not yet implemented\n");
1559 }
1560
1561
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 76 times.
135 if (sps->multilayer_extension) {
1562 59 skip_bits1(gb); // inter_view_mv_vert_constraint_flag
1563 }
1564
1565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 135 times.
135 if (sps->sps_3d_extension) {
1566 for (i = 0; i <= 1; i++) {
1567 skip_bits1(gb); // iv_di_mc_enabled_flag
1568 skip_bits1(gb); // iv_mv_scal_enabled_flag
1569 if (i == 0) {
1570 get_ue_golomb_long(gb); // log2_ivmc_sub_pb_size_minus3
1571 skip_bits1(gb); // iv_res_pred_enabled_flag
1572 skip_bits1(gb); // depth_ref_enabled_flag
1573 skip_bits1(gb); // vsp_mc_enabled_flag
1574 skip_bits1(gb); // dbbp_enabled_flag
1575 } else {
1576 skip_bits1(gb); // tex_mc_enabled_flag
1577 get_ue_golomb_long(gb); // log2_ivmc_sub_pb_size_minus3
1578 skip_bits1(gb); // intra_contour_enabled_flag
1579 skip_bits1(gb); // intra_dc_only_wedge_enabled_flag
1580 skip_bits1(gb); // cqt_cu_part_pred_enabled_flag
1581 skip_bits1(gb); // inter_dc_only_enabled_flag
1582 skip_bits1(gb); // skip_intra_enabled_flag
1583 }
1584 }
1585 av_log(avctx, AV_LOG_WARNING,
1586 "sps_3d_extension_flag not yet implemented\n");
1587 }
1588
1589
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 135 times.
135 if (sps->scc_extension) {
1590 sps->curr_pic_ref_enabled = get_bits1(gb);
1591 sps->palette_mode_enabled = get_bits1(gb);
1592 if (sps->palette_mode_enabled) {
1593 sps->palette_max_size = get_ue_golomb(gb);
1594 sps->delta_palette_max_predictor_size = get_ue_golomb(gb);
1595 sps->palette_predictor_initializers_present = get_bits1(gb);
1596
1597 if (sps->palette_predictor_initializers_present) {
1598 sps->sps_num_palette_predictor_initializers = get_ue_golomb(gb) + 1;
1599 if (sps->sps_num_palette_predictor_initializers > HEVC_MAX_PALETTE_PREDICTOR_SIZE) {
1600 av_log(avctx, AV_LOG_ERROR,
1601 "sps_num_palette_predictor_initializers out of range: %u\n",
1602 sps->sps_num_palette_predictor_initializers);
1603 return AVERROR_INVALIDDATA;
1604 }
1605 num_comps = !sps->chroma_format_idc ? 1 : 3;
1606 for (int comp = 0; comp < num_comps; comp++) {
1607 int bit_depth = !comp ? sps->bit_depth : sps->bit_depth_chroma;
1608 for (i = 0; i < sps->sps_num_palette_predictor_initializers; i++)
1609 sps->sps_palette_predictor_initializer[comp][i] = get_bits(gb, bit_depth);
1610 }
1611 }
1612 }
1613 sps->motion_vector_resolution_control_idc = get_bits(gb, 2);
1614 sps->intra_boundary_filtering_disabled = get_bits1(gb);
1615 }
1616 }
1617
2/2
✓ Branch 0 taken 480 times.
✓ Branch 1 taken 773 times.
1253 if (apply_defdispwin) {
1618 480 sps->output_window.left_offset += sps->vui.def_disp_win.left_offset;
1619 480 sps->output_window.right_offset += sps->vui.def_disp_win.right_offset;
1620 480 sps->output_window.top_offset += sps->vui.def_disp_win.top_offset;
1621 480 sps->output_window.bottom_offset += sps->vui.def_disp_win.bottom_offset;
1622 }
1623
1624 1253 ow = &sps->output_window;
1625
1/2
✓ Branch 0 taken 1253 times.
✗ Branch 1 not taken.
1253 if (ow->left_offset >= INT_MAX - ow->right_offset ||
1626
1/2
✓ Branch 0 taken 1253 times.
✗ Branch 1 not taken.
1253 ow->top_offset >= INT_MAX - ow->bottom_offset ||
1627
1/2
✓ Branch 0 taken 1253 times.
✗ Branch 1 not taken.
1253 ow->left_offset + ow->right_offset >= sps->width ||
1628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 ow->top_offset + ow->bottom_offset >= sps->height) {
1629 av_log(avctx, AV_LOG_WARNING, "Invalid cropping offsets: %u/%u/%u/%u\n",
1630 ow->left_offset, ow->right_offset, ow->top_offset, ow->bottom_offset);
1631 if (avctx->err_recognition & AV_EF_EXPLODE) {
1632 return AVERROR_INVALIDDATA;
1633 }
1634 av_log(avctx, AV_LOG_WARNING,
1635 "Displaying the whole video surface.\n");
1636 memset(ow, 0, sizeof(*ow));
1637 memset(&sps->pic_conf_win, 0, sizeof(sps->pic_conf_win));
1638 }
1639
1640 // Inferred parameters
1641 1253 sps->log2_ctb_size = sps->log2_min_cb_size +
1642 1253 sps->log2_diff_max_min_coding_block_size;
1643 1253 sps->log2_min_pu_size = sps->log2_min_cb_size - 1;
1644
1645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (sps->log2_ctb_size > HEVC_MAX_LOG2_CTB_SIZE) {
1646 av_log(avctx, AV_LOG_ERROR, "CTB size out of range: 2^%d\n", sps->log2_ctb_size);
1647 return AVERROR_INVALIDDATA;
1648 }
1649
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (sps->log2_ctb_size < 4) {
1650 av_log(avctx,
1651 AV_LOG_ERROR,
1652 "log2_ctb_size %d differs from the bounds of any known profile\n",
1653 sps->log2_ctb_size);
1654 avpriv_request_sample(avctx, "log2_ctb_size %d", sps->log2_ctb_size);
1655 return AVERROR_INVALIDDATA;
1656 }
1657
1658 1253 sps->ctb_width = (sps->width + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
1659 1253 sps->ctb_height = (sps->height + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
1660 1253 sps->ctb_size = sps->ctb_width * sps->ctb_height;
1661
1662 1253 sps->min_cb_width = sps->width >> sps->log2_min_cb_size;
1663 1253 sps->min_cb_height = sps->height >> sps->log2_min_cb_size;
1664 1253 sps->min_tb_width = sps->width >> sps->log2_min_tb_size;
1665 1253 sps->min_tb_height = sps->height >> sps->log2_min_tb_size;
1666 1253 sps->min_pu_width = sps->width >> sps->log2_min_pu_size;
1667 1253 sps->min_pu_height = sps->height >> sps->log2_min_pu_size;
1668 1253 sps->tb_mask = (1 << (sps->log2_ctb_size - sps->log2_min_tb_size)) - 1;
1669
1670 1253 sps->qp_bd_offset = 6 * (sps->bit_depth - 8);
1671
1672
1/2
✓ Branch 0 taken 1253 times.
✗ Branch 1 not taken.
1253 if (av_zero_extend(sps->width, sps->log2_min_cb_size) ||
1673
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 av_zero_extend(sps->height, sps->log2_min_cb_size)) {
1674 av_log(avctx, AV_LOG_ERROR, "Invalid coded frame dimensions.\n");
1675 return AVERROR_INVALIDDATA;
1676 }
1677
1678
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) {
1679 av_log(avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_inter out of range: %d\n",
1680 sps->max_transform_hierarchy_depth_inter);
1681 return AVERROR_INVALIDDATA;
1682 }
1683
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) {
1684 av_log(avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_intra out of range: %d\n",
1685 sps->max_transform_hierarchy_depth_intra);
1686 return AVERROR_INVALIDDATA;
1687 }
1688
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (sps->log2_max_trafo_size > FFMIN(sps->log2_ctb_size, 5)) {
1689 av_log(avctx, AV_LOG_ERROR,
1690 "max transform block size out of range: %d\n",
1691 sps->log2_max_trafo_size);
1692 return AVERROR_INVALIDDATA;
1693 }
1694
1695
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1253 times.
1253 if (get_bits_left(gb) < 0) {
1696 av_log(avctx, AV_LOG_ERROR,
1697 "Overread SPS by %d bits\n", -get_bits_left(gb));
1698 return AVERROR_INVALIDDATA;
1699 }
1700
1701 1253 return 0;
1702 }
1703
1704 1253 static void hevc_sps_free(AVRefStructOpaque opaque, void *obj)
1705 {
1706 1253 HEVCSPS *sps = obj;
1707
1708 1253 av_refstruct_unref(&sps->vps);
1709
1710 1253 av_freep(&sps->data);
1711 1253 }
1712
1713 471 static int compare_sps(const HEVCSPS *sps1, const HEVCSPS *sps2)
1714 {
1715
2/2
✓ Branch 0 taken 467 times.
✓ Branch 1 taken 4 times.
938 return sps1->data_size == sps2->data_size &&
1716
2/2
✓ Branch 0 taken 463 times.
✓ Branch 1 taken 4 times.
467 !memcmp(sps1->data, sps2->data, sps1->data_size);
1717 }
1718
1719 1253 int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx,
1720 HEVCParamSets *ps, unsigned nuh_layer_id,
1721 int apply_defdispwin)
1722 {
1723 1253 HEVCSPS *sps = av_refstruct_alloc_ext(sizeof(*sps), 0, NULL, hevc_sps_free);
1724 unsigned int sps_id;
1725 int ret;
1726
1727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (!sps)
1728 return AVERROR(ENOMEM);
1729
1730 1253 av_log(avctx, AV_LOG_DEBUG, "Decoding SPS\n");
1731
1732 1253 sps->data_size = get_bits_bytesize(gb, 1);
1733 1253 sps->data = av_memdup(gb->buffer, sps->data_size);
1734
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (!sps->data) {
1735 ret = AVERROR(ENOMEM);
1736 goto err;
1737 }
1738
1739 1253 ret = ff_hevc_parse_sps(sps, gb, &sps_id,
1740 nuh_layer_id, apply_defdispwin,
1741 1253 ps->vps_list, avctx);
1742
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (ret < 0)
1743 goto err;
1744
1745
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (avctx->debug & FF_DEBUG_BITSTREAM) {
1746 av_log(avctx, AV_LOG_DEBUG,
1747 "Parsed SPS: id %d; coded wxh: %dx%d; "
1748 "cropped wxh: %dx%d; pix_fmt: %s.\n",
1749 sps_id, sps->width, sps->height,
1750 sps->width - (sps->output_window.left_offset + sps->output_window.right_offset),
1751 sps->height - (sps->output_window.top_offset + sps->output_window.bottom_offset),
1752 av_get_pix_fmt_name(sps->pix_fmt));
1753 }
1754
1755 /* check if this is a repeat of an already parsed SPS, then keep the
1756 * original one.
1757 * otherwise drop all PPSes that depend on it */
1758
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] &&
1759 471 compare_sps(ps->sps_list[sps_id], sps)) {
1760 463 av_refstruct_unref(&sps);
1761 } else {
1762 790 remove_sps(ps, sps_id);
1763 790 ps->sps_list[sps_id] = sps;
1764 }
1765
1766 1253 return 0;
1767 err:
1768 av_refstruct_unref(&sps);
1769 return ret;
1770 }
1771
1772 2266 static void hevc_pps_free(AVRefStructOpaque unused, void *obj)
1773 {
1774 2266 HEVCPPS *pps = obj;
1775
1776 2266 av_refstruct_unref(&pps->sps);
1777
1778 2266 av_freep(&pps->column_width);
1779 2266 av_freep(&pps->row_height);
1780 2266 av_freep(&pps->col_bd);
1781 2266 av_freep(&pps->row_bd);
1782 2266 av_freep(&pps->col_idxX);
1783 2266 av_freep(&pps->ctb_addr_rs_to_ts);
1784 2266 av_freep(&pps->ctb_addr_ts_to_rs);
1785 2266 av_freep(&pps->tile_pos_rs);
1786 2266 av_freep(&pps->tile_id);
1787 2266 av_freep(&pps->min_tb_addr_zs_tab);
1788 2266 av_freep(&pps->data);
1789 2266 }
1790
1791 3 static void colour_mapping_octants(GetBitContext *gb, HEVCPPS *pps, int inp_depth,
1792 int idx_y, int idx_cb, int idx_cr, int inp_length)
1793 {
1794 unsigned int split_octant_flag, part_num_y, coded_res_flag, res_coeff_q, res_coeff_r;
1795 int cm_res_bits;
1796
1797 3 part_num_y = 1 << pps->cm_y_part_num_log2;
1798
1799
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;
1800
1801
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (split_octant_flag)
1802 for (int k = 0; k < 2; k++)
1803 for (int m = 0; m < 2; m++)
1804 for (int n = 0; n < 2; n++)
1805 colour_mapping_octants(gb, pps, inp_depth + 1,
1806 idx_y + part_num_y * k * inp_length / 2,
1807 idx_cb + m * inp_length / 2,
1808 idx_cr + n * inp_length / 2,
1809 inp_length / 2);
1810 else
1811
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 3 times.
27 for (int i = 0; i < part_num_y; i++) {
1812
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 24 times.
120 for (int j = 0; j < 4; j++) {
1813 96 coded_res_flag = get_bits1(gb);
1814
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 if (coded_res_flag)
1815 for (int c = 0; c < 3; c++) {
1816 res_coeff_q = get_ue_golomb_long(gb);
1817 cm_res_bits = FFMAX(0, 10 + pps->luma_bit_depth_cm_input -
1818 pps->luma_bit_depth_cm_output -
1819 pps->cm_res_quant_bits - pps->cm_delta_flc_bits);
1820 res_coeff_r = cm_res_bits ? get_bits(gb, cm_res_bits) : 0;
1821 if (res_coeff_q || res_coeff_r)
1822 skip_bits1(gb);
1823 }
1824 }
1825 }
1826 3 }
1827
1828 3 static int colour_mapping_table(GetBitContext *gb, AVCodecContext *avctx, HEVCPPS *pps)
1829 {
1830 3 pps->num_cm_ref_layers = get_ue_golomb(gb) + 1;
1831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (pps->num_cm_ref_layers > 62) {
1832 av_log(avctx, AV_LOG_ERROR,
1833 "num_cm_ref_layers_minus1 shall be in the range [0, 61].\n");
1834 return AVERROR_INVALIDDATA;
1835 }
1836
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
12 for (int i = 0; i < pps->num_cm_ref_layers; i++)
1837 9 pps->cm_ref_layer_id[i] = get_bits(gb, 6);
1838
1839 3 pps->cm_octant_depth = get_bits(gb, 2);
1840 3 pps->cm_y_part_num_log2 = get_bits(gb, 2);
1841
1842 3 pps->luma_bit_depth_cm_input = get_ue_golomb(gb) + 8;
1843 3 pps->chroma_bit_depth_cm_input = get_ue_golomb(gb) + 8;
1844 3 pps->luma_bit_depth_cm_output = get_ue_golomb(gb) + 8;
1845 3 pps->chroma_bit_depth_cm_output = get_ue_golomb(gb) + 8;
1846
1847
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if ( pps-> luma_bit_depth_cm_output < pps-> luma_bit_depth_cm_input
1848
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 || pps->chroma_bit_depth_cm_output < pps->chroma_bit_depth_cm_input)
1849 return AVERROR_INVALIDDATA;
1850
1851 3 pps->cm_res_quant_bits = get_bits(gb, 2);
1852 3 pps->cm_delta_flc_bits = get_bits(gb, 2) + 1;
1853
1854
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (pps->cm_octant_depth == 1) {
1855 pps->cm_adapt_threshold_u_delta = get_se_golomb_long(gb);
1856 pps->cm_adapt_threshold_v_delta = get_se_golomb_long(gb);
1857 }
1858
1859 3 colour_mapping_octants(gb, pps, 0, 0, 0, 0, 1 << pps->cm_octant_depth);
1860
1861 3 return 0;
1862 }
1863
1864 47 static int pps_multilayer_extension(GetBitContext *gb, AVCodecContext *avctx,
1865 HEVCPPS *pps, const HEVCSPS *sps, const HEVCVPS *vps)
1866 {
1867 47 pps->poc_reset_info_present_flag = get_bits1(gb);
1868 47 pps->pps_infer_scaling_list_flag = get_bits1(gb);
1869
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (pps->pps_infer_scaling_list_flag)
1870 pps->pps_scaling_list_ref_layer_id = get_bits(gb, 6);
1871
1872 47 pps->num_ref_loc_offsets = get_ue_golomb(gb);
1873
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (pps->num_ref_loc_offsets > vps->vps_max_layers - 1)
1874 return AVERROR_INVALIDDATA;
1875
1876
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 for (int i = 0; i < pps->num_ref_loc_offsets; i++) {
1877 pps->ref_loc_offset_layer_id[i] = get_bits(gb, 6);
1878 pps->scaled_ref_layer_offset_present_flag[i] = get_bits1(gb);
1879 if (pps->scaled_ref_layer_offset_present_flag[i]) {
1880 pps->scaled_ref_layer_left_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1881 pps->scaled_ref_layer_top_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1882 pps->scaled_ref_layer_right_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1883 pps->scaled_ref_layer_bottom_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1884 }
1885
1886 pps->ref_region_offset_present_flag[i] = get_bits1(gb);
1887 if (pps->ref_region_offset_present_flag[i]) {
1888 pps->ref_region_left_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1889 pps->ref_region_top_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1890 pps->ref_region_right_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1891 pps->ref_region_bottom_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1892 }
1893
1894 pps->resample_phase_set_present_flag[i] = get_bits1(gb);
1895 if (pps->resample_phase_set_present_flag[i]) {
1896 pps->phase_hor_luma[pps->ref_loc_offset_layer_id[i]] = get_ue_golomb_31(gb);
1897 pps->phase_ver_luma[pps->ref_loc_offset_layer_id[i]] = get_ue_golomb_31(gb);
1898 pps->phase_hor_chroma[pps->ref_loc_offset_layer_id[i]] = get_ue_golomb(gb) - 8;
1899 pps->phase_ver_chroma[pps->ref_loc_offset_layer_id[i]] = get_ue_golomb(gb) - 8;
1900 }
1901 }
1902
1903 47 pps->colour_mapping_enabled_flag = get_bits1(gb);
1904
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 44 times.
47 if (pps->colour_mapping_enabled_flag) {
1905 3 int ret = colour_mapping_table(gb, avctx, pps);
1906
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
1907 return ret;
1908 }
1909
1910 47 return 0;
1911 }
1912
1913 static void delta_dlt(GetBitContext *gb, HEVCPPS *pps)
1914 {
1915 unsigned int num_val_delta_dlt, max_diff = 0;
1916 int min_diff_minus1 = -1;
1917 unsigned int len;
1918
1919 num_val_delta_dlt = get_bits(gb, pps->pps_bit_depth_for_depth_layers_minus8 + 8);
1920 if (num_val_delta_dlt) {
1921 if (num_val_delta_dlt > 1)
1922 max_diff = get_bits(gb, pps->pps_bit_depth_for_depth_layers_minus8 + 8);
1923 if (num_val_delta_dlt > 2 && max_diff) {
1924 len = av_log2(max_diff) + 1;
1925 min_diff_minus1 = get_bits(gb, len);
1926 }
1927 if (max_diff > (min_diff_minus1 + 1))
1928 for (int k = 1; k < num_val_delta_dlt; k++) {
1929 len = av_log2(max_diff - (min_diff_minus1 + 1)) + 1;
1930 skip_bits(gb, len); // delta_val_diff_minus_min
1931 }
1932 }
1933 }
1934
1935 3 static int pps_3d_extension(GetBitContext *gb, AVCodecContext *avctx,
1936 HEVCPPS *pps, const HEVCSPS *sps)
1937 {
1938 unsigned int pps_depth_layers_minus1;
1939
1940
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (get_bits1(gb)) { // dlts_present_flag
1941 pps_depth_layers_minus1 = get_bits(gb, 6);
1942 pps->pps_bit_depth_for_depth_layers_minus8 = get_bits(gb, 4);
1943 for (int i = 0; i <= pps_depth_layers_minus1; i++) {
1944 if (get_bits1(gb)) { // dlt_flag[i]
1945 if (!get_bits1(gb)) { // dlt_pred_flag[i]
1946 if (get_bits1(gb)) { // dlt_val_flags_present_flag[i]
1947 for (int j = 0; j <= ((1 << (pps->pps_bit_depth_for_depth_layers_minus8 + 8)) - 1); j++)
1948 skip_bits1(gb); // dlt_value_flag[i][j]
1949 } else
1950 delta_dlt(gb, pps);
1951 }
1952 }
1953 }
1954 }
1955
1956 3 return 0;
1957 }
1958
1959 63 static int pps_range_extensions(GetBitContext *gb, AVCodecContext *avctx,
1960 HEVCPPS *pps, const HEVCSPS *sps)
1961 {
1962
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 if (pps->transform_skip_enabled_flag) {
1963 63 pps->log2_max_transform_skip_block_size = get_ue_golomb_31(gb) + 2;
1964 }
1965 63 pps->cross_component_prediction_enabled_flag = get_bits1(gb);
1966 63 pps->chroma_qp_offset_list_enabled_flag = get_bits1(gb);
1967
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 57 times.
63 if (pps->chroma_qp_offset_list_enabled_flag) {
1968 6 pps->diff_cu_chroma_qp_offset_depth = get_ue_golomb_31(gb);
1969 6 pps->chroma_qp_offset_list_len_minus1 = get_ue_golomb_31(gb);
1970
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (pps->chroma_qp_offset_list_len_minus1 > 5) {
1971 av_log(avctx, AV_LOG_ERROR,
1972 "chroma_qp_offset_list_len_minus1 shall be in the range [0, 5].\n");
1973 return AVERROR_INVALIDDATA;
1974 }
1975
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++) {
1976 6 pps->cb_qp_offset_list[i] = get_se_golomb(gb);
1977
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (pps->cb_qp_offset_list[i]) {
1978 6 av_log(avctx, AV_LOG_WARNING,
1979 "cb_qp_offset_list not tested yet.\n");
1980 }
1981 6 pps->cr_qp_offset_list[i] = get_se_golomb(gb);
1982
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (pps->cr_qp_offset_list[i]) {
1983 6 av_log(avctx, AV_LOG_WARNING,
1984 "cb_qp_offset_list not tested yet.\n");
1985 }
1986 }
1987 }
1988 63 pps->log2_sao_offset_scale_luma = get_ue_golomb_31(gb);
1989 63 pps->log2_sao_offset_scale_chroma = get_ue_golomb_31(gb);
1990
1991
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)
1992
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)
1993 )
1994 return AVERROR_INVALIDDATA;
1995
1996 63 return(0);
1997 }
1998
1999 3 static int pps_scc_extension(GetBitContext *gb, AVCodecContext *avctx,
2000 HEVCPPS *pps, const HEVCSPS *sps)
2001 {
2002 int num_comps, ret;
2003
2004 3 pps->pps_curr_pic_ref_enabled_flag = get_bits1(gb);
2005
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (pps->residual_adaptive_colour_transform_enabled_flag = get_bits1(gb)) {
2006 pps->pps_slice_act_qp_offsets_present_flag = get_bits1(gb);
2007 pps->pps_act_y_qp_offset = get_se_golomb(gb) - 5;
2008 pps->pps_act_cb_qp_offset = get_se_golomb(gb) - 5;
2009 pps->pps_act_cr_qp_offset = get_se_golomb(gb) - 3;
2010
2011 #define CHECK_QP_OFFSET(name) (pps->pps_act_ ## name ## _qp_offset <= -12 || \
2012 pps->pps_act_ ## name ## _qp_offset >= 12)
2013 ret = CHECK_QP_OFFSET(y) || CHECK_QP_OFFSET(cb) || CHECK_QP_OFFSET(cr);
2014 #undef CHECK_QP_OFFSET
2015 if (ret) {
2016 av_log(avctx, AV_LOG_ERROR,
2017 "PpsActQpOffsetY/Cb/Cr shall be in the range of [-12, 12].\n");
2018 return AVERROR_INVALIDDATA;
2019 }
2020 }
2021
2022
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (pps->pps_palette_predictor_initializers_present_flag = get_bits1(gb)) {
2023 pps->pps_num_palette_predictor_initializers = get_ue_golomb(gb);
2024 if (pps->pps_num_palette_predictor_initializers > 0) {
2025 if (pps->pps_num_palette_predictor_initializers > HEVC_MAX_PALETTE_PREDICTOR_SIZE) {
2026 av_log(avctx, AV_LOG_ERROR,
2027 "pps_num_palette_predictor_initializers out of range: %u\n",
2028 pps->pps_num_palette_predictor_initializers);
2029 return AVERROR_INVALIDDATA;
2030 }
2031 pps->monochrome_palette_flag = get_bits1(gb);
2032 pps->luma_bit_depth_entry = get_ue_golomb_31(gb) + 8;
2033 if (pps->luma_bit_depth_entry != sps->bit_depth)
2034 return AVERROR_INVALIDDATA;
2035 if (!pps->monochrome_palette_flag) {
2036 pps->chroma_bit_depth_entry = get_ue_golomb_31(gb) + 8;
2037 if (pps->chroma_bit_depth_entry != sps->bit_depth_chroma)
2038 return AVERROR_INVALIDDATA;
2039 }
2040
2041 num_comps = pps->monochrome_palette_flag ? 1 : 3;
2042 for (int comp = 0; comp < num_comps; comp++) {
2043 int bit_depth = !comp ? pps->luma_bit_depth_entry : pps->chroma_bit_depth_entry;
2044 for (int i = 0; i < pps->pps_num_palette_predictor_initializers; i++)
2045 pps->pps_palette_predictor_initializer[comp][i] = get_bits(gb, bit_depth);
2046 }
2047 }
2048 }
2049
2050 3 return 0;
2051 }
2052
2053 2262 static inline int setup_pps(AVCodecContext *avctx, GetBitContext *gb,
2054 HEVCPPS *pps, const HEVCSPS *sps)
2055 {
2056 int log2_diff;
2057 int pic_area_in_ctbs;
2058 int i, j, x, y, ctb_addr_rs, tile_id;
2059
2060 // Inferred parameters
2061 2262 pps->col_bd = av_malloc_array(pps->num_tile_columns + 1, sizeof(*pps->col_bd));
2062 2262 pps->row_bd = av_malloc_array(pps->num_tile_rows + 1, sizeof(*pps->row_bd));
2063 2262 pps->col_idxX = av_malloc_array(sps->ctb_width, sizeof(*pps->col_idxX));
2064
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)
2065 return AVERROR(ENOMEM);
2066
2067
2/2
✓ Branch 0 taken 1730 times.
✓ Branch 1 taken 532 times.
2262 if (pps->uniform_spacing_flag) {
2068
2/2
✓ Branch 0 taken 1504 times.
✓ Branch 1 taken 226 times.
1730 if (!pps->column_width) {
2069 1504 pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
2070 1504 pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
2071 }
2072
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)
2073 return AVERROR(ENOMEM);
2074
2075
2/2
✓ Branch 0 taken 1891 times.
✓ Branch 1 taken 1730 times.
3621 for (i = 0; i < pps->num_tile_columns; i++) {
2076 1891 pps->column_width[i] = ((i + 1) * sps->ctb_width) / pps->num_tile_columns -
2077 1891 (i * sps->ctb_width) / pps->num_tile_columns;
2078 }
2079
2080
2/2
✓ Branch 0 taken 1909 times.
✓ Branch 1 taken 1730 times.
3639 for (i = 0; i < pps->num_tile_rows; i++) {
2081 1909 pps->row_height[i] = ((i + 1) * sps->ctb_height) / pps->num_tile_rows -
2082 1909 (i * sps->ctb_height) / pps->num_tile_rows;
2083 }
2084 }
2085
2086 2262 pps->col_bd[0] = 0;
2087
2/2
✓ Branch 0 taken 4480 times.
✓ Branch 1 taken 2262 times.
6742 for (i = 0; i < pps->num_tile_columns; i++)
2088 4480 pps->col_bd[i + 1] = pps->col_bd[i] + pps->column_width[i];
2089
2090 2262 pps->row_bd[0] = 0;
2091
2/2
✓ Branch 0 taken 4471 times.
✓ Branch 1 taken 2262 times.
6733 for (i = 0; i < pps->num_tile_rows; i++)
2092 4471 pps->row_bd[i + 1] = pps->row_bd[i] + pps->row_height[i];
2093
2094
2/2
✓ Branch 0 taken 45277 times.
✓ Branch 1 taken 2262 times.
47539 for (i = 0, j = 0; i < sps->ctb_width; i++) {
2095
2/2
✓ Branch 0 taken 4471 times.
✓ Branch 1 taken 40806 times.
45277 if (i > pps->col_bd[j])
2096 4471 j++;
2097 45277 pps->col_idxX[i] = j;
2098 }
2099
2100 /**
2101 * 6.5
2102 */
2103 2262 pic_area_in_ctbs = sps->ctb_width * sps->ctb_height;
2104
2105 2262 pps->ctb_addr_rs_to_ts = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_rs_to_ts));
2106 2262 pps->ctb_addr_ts_to_rs = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_ts_to_rs));
2107 2262 pps->tile_id = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->tile_id));
2108 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));
2109
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 ||
2110
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) {
2111 return AVERROR(ENOMEM);
2112 }
2113
2114
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++) {
2115 754483 int tb_x = ctb_addr_rs % sps->ctb_width;
2116 754483 int tb_y = ctb_addr_rs / sps->ctb_width;
2117 754483 int tile_x = 0;
2118 754483 int tile_y = 0;
2119 754483 int val = 0;
2120
2121
1/2
✓ Branch 0 taken 1278363 times.
✗ Branch 1 not taken.
1278363 for (i = 0; i < pps->num_tile_columns; i++) {
2122
2/2
✓ Branch 0 taken 754483 times.
✓ Branch 1 taken 523880 times.
1278363 if (tb_x < pps->col_bd[i + 1]) {
2123 754483 tile_x = i;
2124 754483 break;
2125 }
2126 }
2127
2128
1/2
✓ Branch 0 taken 1132881 times.
✗ Branch 1 not taken.
1132881 for (i = 0; i < pps->num_tile_rows; i++) {
2129
2/2
✓ Branch 0 taken 754483 times.
✓ Branch 1 taken 378398 times.
1132881 if (tb_y < pps->row_bd[i + 1]) {
2130 754483 tile_y = i;
2131 754483 break;
2132 }
2133 }
2134
2135
2/2
✓ Branch 0 taken 523880 times.
✓ Branch 1 taken 754483 times.
1278363 for (i = 0; i < tile_x; i++)
2136 523880 val += pps->row_height[tile_y] * pps->column_width[i];
2137
2/2
✓ Branch 0 taken 378398 times.
✓ Branch 1 taken 754483 times.
1132881 for (i = 0; i < tile_y; i++)
2138 378398 val += sps->ctb_width * pps->row_height[i];
2139
2140 754483 val += (tb_y - pps->row_bd[tile_y]) * pps->column_width[tile_x] +
2141 754483 tb_x - pps->col_bd[tile_x];
2142
2143 754483 pps->ctb_addr_rs_to_ts[ctb_addr_rs] = val;
2144 754483 pps->ctb_addr_ts_to_rs[val] = ctb_addr_rs;
2145 }
2146
2147
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++)
2148
2/2
✓ Branch 0 taken 14841 times.
✓ Branch 1 taken 4471 times.
19312 for (i = 0; i < pps->num_tile_columns; i++, tile_id++)
2149
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++)
2150
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++)
2151 754483 pps->tile_id[pps->ctb_addr_rs_to_ts[y * sps->ctb_width + x]] = tile_id;
2152
2153 2262 pps->tile_pos_rs = av_malloc_array(tile_id, sizeof(*pps->tile_pos_rs));
2154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2262 times.
2262 if (!pps->tile_pos_rs)
2155 return AVERROR(ENOMEM);
2156
2157
2/2
✓ Branch 0 taken 4471 times.
✓ Branch 1 taken 2262 times.
6733 for (j = 0; j < pps->num_tile_rows; j++)
2158
2/2
✓ Branch 0 taken 14841 times.
✓ Branch 1 taken 4471 times.
19312 for (i = 0; i < pps->num_tile_columns; i++)
2159 14841 pps->tile_pos_rs[j * pps->num_tile_columns + i] =
2160 14841 pps->row_bd[j] * sps->ctb_width + pps->col_bd[i];
2161
2162 2262 log2_diff = sps->log2_ctb_size - sps->log2_min_tb_size;
2163 2262 pps->min_tb_addr_zs = &pps->min_tb_addr_zs_tab[1*(sps->tb_mask+2)+1];
2164
2/2
✓ Branch 0 taken 37278 times.
✓ Branch 1 taken 2262 times.
39540 for (y = 0; y < sps->tb_mask+2; y++) {
2165 37278 pps->min_tb_addr_zs_tab[y*(sps->tb_mask+2)] = -1;
2166 37278 pps->min_tb_addr_zs_tab[y] = -1;
2167 }
2168
2/2
✓ Branch 0 taken 35016 times.
✓ Branch 1 taken 2262 times.
37278 for (y = 0; y < sps->tb_mask+1; y++) {
2169
2/2
✓ Branch 0 taken 552384 times.
✓ Branch 1 taken 35016 times.
587400 for (x = 0; x < sps->tb_mask+1; x++) {
2170 552384 int tb_x = x >> log2_diff;
2171 552384 int tb_y = y >> log2_diff;
2172 552384 int rs = sps->ctb_width * tb_y + tb_x;
2173 552384 int val = pps->ctb_addr_rs_to_ts[rs] << (log2_diff * 2);
2174
2/2
✓ Branch 0 taken 2202176 times.
✓ Branch 1 taken 552384 times.
2754560 for (i = 0; i < log2_diff; i++) {
2175 2202176 int m = 1 << i;
2176
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);
2177 }
2178 552384 pps->min_tb_addr_zs[y * (sps->tb_mask+2) + x] = val;
2179 }
2180 }
2181
2182 2262 return 0;
2183 }
2184
2185 3601 int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx,
2186 HEVCParamSets *ps)
2187 {
2188 3601 const HEVCSPS *sps = NULL;
2189 3601 const HEVCVPS *vps = NULL;
2190 3601 int i, ret = 0;
2191 3601 ptrdiff_t nal_size = get_bits_bytesize(gb, 1);
2192 3601 unsigned int pps_id = get_ue_golomb_long(gb);
2193 unsigned log2_parallel_merge_level_minus2;
2194 HEVCPPS *pps;
2195
2196 3601 av_log(avctx, AV_LOG_DEBUG, "Decoding PPS\n");
2197
2198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3601 times.
3601 if (pps_id >= HEVC_MAX_PPS_COUNT) {
2199 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
2200 return AVERROR_INVALIDDATA;
2201 }
2202
2203
2/2
✓ Branch 0 taken 2556 times.
✓ Branch 1 taken 1045 times.
3601 if (ps->pps_list[pps_id]) {
2204 2556 const HEVCPPS *pps1 = ps->pps_list[pps_id];
2205
2/2
✓ Branch 0 taken 2160 times.
✓ Branch 1 taken 396 times.
2556 if (pps1->data_size == nal_size &&
2206
2/2
✓ Branch 0 taken 1335 times.
✓ Branch 1 taken 825 times.
2160 !memcmp(pps1->data, gb->buffer, pps1->data_size))
2207 1335 return 0;
2208 }
2209
2210 2266 pps = av_refstruct_alloc_ext(sizeof(*pps), 0, NULL, hevc_pps_free);
2211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2266 times.
2266 if (!pps)
2212 return AVERROR(ENOMEM);
2213
2214 2266 pps->data_size = nal_size;
2215 2266 pps->data = av_memdup(gb->buffer, nal_size);
2216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2266 times.
2266 if (!pps->data) {
2217 ret = AVERROR_INVALIDDATA;
2218 goto err;
2219 }
2220
2221 // Default values
2222 2266 pps->loop_filter_across_tiles_enabled_flag = 1;
2223 2266 pps->num_tile_columns = 1;
2224 2266 pps->num_tile_rows = 1;
2225 2266 pps->uniform_spacing_flag = 1;
2226 2266 pps->disable_dbf = 0;
2227 2266 pps->beta_offset = 0;
2228 2266 pps->tc_offset = 0;
2229 2266 pps->log2_max_transform_skip_block_size = 2;
2230
2231 // Coded parameters
2232 2266 pps->pps_id = pps_id;
2233 2266 pps->sps_id = get_ue_golomb_long(gb);
2234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2266 times.
2266 if (pps->sps_id >= HEVC_MAX_SPS_COUNT) {
2235 av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", pps->sps_id);
2236 ret = AVERROR_INVALIDDATA;
2237 goto err;
2238 }
2239
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2262 times.
2266 if (!ps->sps_list[pps->sps_id]) {
2240 4 av_log(avctx, AV_LOG_ERROR, "SPS %u does not exist.\n", pps->sps_id);
2241 4 ret = AVERROR_INVALIDDATA;
2242 4 goto err;
2243 }
2244 2262 sps = ps->sps_list[pps->sps_id];
2245 2262 vps = ps->vps_list[sps->vps_id];
2246
2247 2262 pps->sps = av_refstruct_ref_c(sps);
2248
2249 2262 pps->dependent_slice_segments_enabled_flag = get_bits1(gb);
2250 2262 pps->output_flag_present_flag = get_bits1(gb);
2251 2262 pps->num_extra_slice_header_bits = get_bits(gb, 3);
2252
2253 2262 pps->sign_data_hiding_flag = get_bits1(gb);
2254
2255 2262 pps->cabac_init_present_flag = get_bits1(gb);
2256
2257 2262 pps->num_ref_idx_l0_default_active = get_ue_golomb_31(gb) + 1;
2258 2262 pps->num_ref_idx_l1_default_active = get_ue_golomb_31(gb) + 1;
2259
1/2
✓ Branch 0 taken 2262 times.
✗ Branch 1 not taken.
2262 if (pps->num_ref_idx_l0_default_active >= HEVC_MAX_REFS ||
2260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2262 times.
2262 pps->num_ref_idx_l1_default_active >= HEVC_MAX_REFS) {
2261 av_log(avctx, AV_LOG_ERROR, "Too many default refs in PPS: %d/%d.\n",
2262 pps->num_ref_idx_l0_default_active, pps->num_ref_idx_l1_default_active);
2263 goto err;
2264 }
2265
2266 2262 pps->pic_init_qp_minus26 = get_se_golomb(gb);
2267
2268 2262 pps->constrained_intra_pred_flag = get_bits1(gb);
2269 2262 pps->transform_skip_enabled_flag = get_bits1(gb);
2270
2271 2262 pps->cu_qp_delta_enabled_flag = get_bits1(gb);
2272 2262 pps->diff_cu_qp_delta_depth = 0;
2273
2/2
✓ Branch 0 taken 508 times.
✓ Branch 1 taken 1754 times.
2262 if (pps->cu_qp_delta_enabled_flag)
2274 508 pps->diff_cu_qp_delta_depth = get_ue_golomb_long(gb);
2275
2276
1/2
✓ Branch 0 taken 2262 times.
✗ Branch 1 not taken.
2262 if (pps->diff_cu_qp_delta_depth < 0 ||
2277
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) {
2278 av_log(avctx, AV_LOG_ERROR, "diff_cu_qp_delta_depth %d is invalid\n",
2279 pps->diff_cu_qp_delta_depth);
2280 ret = AVERROR_INVALIDDATA;
2281 goto err;
2282 }
2283
2284 2262 pps->cb_qp_offset = get_se_golomb(gb);
2285
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) {
2286 av_log(avctx, AV_LOG_ERROR, "pps_cb_qp_offset out of range: %d\n",
2287 pps->cb_qp_offset);
2288 ret = AVERROR_INVALIDDATA;
2289 goto err;
2290 }
2291 2262 pps->cr_qp_offset = get_se_golomb(gb);
2292
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) {
2293 av_log(avctx, AV_LOG_ERROR, "pps_cr_qp_offset out of range: %d\n",
2294 pps->cr_qp_offset);
2295 ret = AVERROR_INVALIDDATA;
2296 goto err;
2297 }
2298 2262 pps->pic_slice_level_chroma_qp_offsets_present_flag = get_bits1(gb);
2299
2300 2262 pps->weighted_pred_flag = get_bits1(gb);
2301 2262 pps->weighted_bipred_flag = get_bits1(gb);
2302
2303 2262 pps->transquant_bypass_enable_flag = get_bits1(gb);
2304 2262 pps->tiles_enabled_flag = get_bits1(gb);
2305 2262 pps->entropy_coding_sync_enabled_flag = get_bits1(gb);
2306
2307
2/2
✓ Branch 0 taken 758 times.
✓ Branch 1 taken 1504 times.
2262 if (pps->tiles_enabled_flag) {
2308 758 int num_tile_columns_minus1 = get_ue_golomb(gb);
2309 758 int num_tile_rows_minus1 = get_ue_golomb(gb);
2310
2311
1/2
✓ Branch 0 taken 758 times.
✗ Branch 1 not taken.
758 if (num_tile_columns_minus1 < 0 ||
2312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 758 times.
758 num_tile_columns_minus1 >= sps->ctb_width) {
2313 av_log(avctx, AV_LOG_ERROR, "num_tile_columns_minus1 out of range: %d\n",
2314 num_tile_columns_minus1);
2315 ret = num_tile_columns_minus1 < 0 ? num_tile_columns_minus1 : AVERROR_INVALIDDATA;
2316 goto err;
2317 }
2318
1/2
✓ Branch 0 taken 758 times.
✗ Branch 1 not taken.
758 if (num_tile_rows_minus1 < 0 ||
2319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 758 times.
758 num_tile_rows_minus1 >= sps->ctb_height) {
2320 av_log(avctx, AV_LOG_ERROR, "num_tile_rows_minus1 out of range: %d\n",
2321 num_tile_rows_minus1);
2322 ret = num_tile_rows_minus1 < 0 ? num_tile_rows_minus1 : AVERROR_INVALIDDATA;
2323 goto err;
2324 }
2325 758 pps->num_tile_columns = num_tile_columns_minus1 + 1;
2326 758 pps->num_tile_rows = num_tile_rows_minus1 + 1;
2327
2328 758 pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
2329 758 pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
2330
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) {
2331 ret = AVERROR(ENOMEM);
2332 goto err;
2333 }
2334
2335 758 pps->uniform_spacing_flag = get_bits1(gb);
2336
2/2
✓ Branch 0 taken 532 times.
✓ Branch 1 taken 226 times.
758 if (!pps->uniform_spacing_flag) {
2337 532 uint64_t sum = 0;
2338
2/2
✓ Branch 0 taken 2057 times.
✓ Branch 1 taken 532 times.
2589 for (i = 0; i < pps->num_tile_columns - 1; i++) {
2339 2057 pps->column_width[i] = get_ue_golomb_long(gb) + 1;
2340 2057 sum += pps->column_width[i];
2341 }
2342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 532 times.
532 if (sum >= sps->ctb_width) {
2343 av_log(avctx, AV_LOG_ERROR, "Invalid tile widths.\n");
2344 ret = AVERROR_INVALIDDATA;
2345 goto err;
2346 }
2347 532 pps->column_width[pps->num_tile_columns - 1] = sps->ctb_width - sum;
2348
2349 532 sum = 0;
2350
2/2
✓ Branch 0 taken 2030 times.
✓ Branch 1 taken 532 times.
2562 for (i = 0; i < pps->num_tile_rows - 1; i++) {
2351 2030 pps->row_height[i] = get_ue_golomb_long(gb) + 1;
2352 2030 sum += pps->row_height[i];
2353 }
2354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 532 times.
532 if (sum >= sps->ctb_height) {
2355 av_log(avctx, AV_LOG_ERROR, "Invalid tile heights.\n");
2356 ret = AVERROR_INVALIDDATA;
2357 goto err;
2358 }
2359 532 pps->row_height[pps->num_tile_rows - 1] = sps->ctb_height - sum;
2360 }
2361 758 pps->loop_filter_across_tiles_enabled_flag = get_bits1(gb);
2362 }
2363
2364 2262 pps->seq_loop_filter_across_slices_enabled_flag = get_bits1(gb);
2365
2366 2262 pps->deblocking_filter_control_present_flag = get_bits1(gb);
2367
2/2
✓ Branch 0 taken 897 times.
✓ Branch 1 taken 1365 times.
2262 if (pps->deblocking_filter_control_present_flag) {
2368 897 pps->deblocking_filter_override_enabled_flag = get_bits1(gb);
2369 897 pps->disable_dbf = get_bits1(gb);
2370
2/2
✓ Branch 0 taken 771 times.
✓ Branch 1 taken 126 times.
897 if (!pps->disable_dbf) {
2371 771 int beta_offset_div2 = get_se_golomb(gb);
2372 771 int tc_offset_div2 = get_se_golomb(gb) ;
2373
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) {
2374 av_log(avctx, AV_LOG_ERROR, "pps_beta_offset_div2 out of range: %d\n",
2375 beta_offset_div2);
2376 ret = AVERROR_INVALIDDATA;
2377 goto err;
2378 }
2379
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) {
2380 av_log(avctx, AV_LOG_ERROR, "pps_tc_offset_div2 out of range: %d\n",
2381 tc_offset_div2);
2382 ret = AVERROR_INVALIDDATA;
2383 goto err;
2384 }
2385 771 pps->beta_offset = 2 * beta_offset_div2;
2386 771 pps->tc_offset = 2 * tc_offset_div2;
2387 }
2388 }
2389
2390 2262 pps->scaling_list_data_present_flag = get_bits1(gb);
2391
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 2131 times.
2262 if (pps->scaling_list_data_present_flag) {
2392 131 set_default_scaling_list_data(&pps->scaling_list);
2393 131 ret = scaling_list_data(gb, avctx, &pps->scaling_list, sps);
2394
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 131 times.
131 if (ret < 0)
2395 goto err;
2396 }
2397 2262 pps->lists_modification_present_flag = get_bits1(gb);
2398 2262 log2_parallel_merge_level_minus2 = get_ue_golomb_long(gb);
2399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2262 times.
2262 if (log2_parallel_merge_level_minus2 > sps->log2_ctb_size) {
2400 av_log(avctx, AV_LOG_ERROR, "log2_parallel_merge_level_minus2 out of range: %d\n",
2401 log2_parallel_merge_level_minus2);
2402 ret = AVERROR_INVALIDDATA;
2403 goto err;
2404 }
2405 2262 pps->log2_parallel_merge_level = log2_parallel_merge_level_minus2 + 2;
2406
2407 2262 pps->slice_header_extension_present_flag = get_bits1(gb);
2408
2409 2262 pps->pps_extension_present_flag = get_bits1(gb);
2410
2/2
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 2149 times.
2262 if (pps->pps_extension_present_flag) {
2411 113 pps->pps_range_extensions_flag = get_bits1(gb);
2412 113 pps->pps_multilayer_extension_flag = get_bits1(gb);
2413 113 pps->pps_3d_extension_flag = get_bits1(gb);
2414 113 pps->pps_scc_extension_flag = get_bits1(gb);
2415 113 skip_bits(gb, 4); // pps_extension_4bits
2416
2417
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) {
2418
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
63 if ((ret = pps_range_extensions(gb, avctx, pps, sps)) < 0)
2419 goto err;
2420 }
2421
2422
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 66 times.
113 if (pps->pps_multilayer_extension_flag) {
2423
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
47 if ((ret = pps_multilayer_extension(gb, avctx, pps, sps, vps)) < 0)
2424 goto err;
2425 }
2426
2427
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 110 times.
113 if (pps->pps_3d_extension_flag) {
2428
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = pps_3d_extension(gb, avctx, pps, sps)) < 0)
2429 goto err;
2430 }
2431
2432
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 110 times.
113 if (pps->pps_scc_extension_flag) {
2433
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = pps_scc_extension(gb, avctx, pps, sps)) < 0)
2434 goto err;
2435 }
2436 }
2437
2438 2262 ret = setup_pps(avctx, gb, pps, sps);
2439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2262 times.
2262 if (ret < 0)
2440 goto err;
2441
2442
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2259 times.
2262 if (get_bits_left(gb) < 0) {
2443 3 av_log(avctx, AV_LOG_WARNING,
2444 3 "Overread PPS by %d bits\n", -get_bits_left(gb));
2445 }
2446
2447 2262 av_refstruct_unref(&ps->pps_list[pps_id]);
2448 2262 ps->pps_list[pps_id] = pps;
2449
2450 2262 return 0;
2451
2452 4 err:
2453 4 av_refstruct_unref(&pps);
2454 4 return ret;
2455 }
2456
2457 808 void ff_hevc_ps_uninit(HEVCParamSets *ps)
2458 {
2459 int i;
2460
2461
2/2
✓ Branch 0 taken 12928 times.
✓ Branch 1 taken 808 times.
13736 for (i = 0; i < FF_ARRAY_ELEMS(ps->vps_list); i++)
2462 12928 av_refstruct_unref(&ps->vps_list[i]);
2463
2/2
✓ Branch 0 taken 12928 times.
✓ Branch 1 taken 808 times.
13736 for (i = 0; i < FF_ARRAY_ELEMS(ps->sps_list); i++)
2464 12928 av_refstruct_unref(&ps->sps_list[i]);
2465
2/2
✓ Branch 0 taken 51712 times.
✓ Branch 1 taken 808 times.
52520 for (i = 0; i < FF_ARRAY_ELEMS(ps->pps_list); i++)
2466 51712 av_refstruct_unref(&ps->pps_list[i]);
2467 808 }
2468
2469 32839 int ff_hevc_compute_poc2(unsigned log2_max_poc_lsb, int pocTid0, int poc_lsb, int nal_unit_type)
2470 {
2471 32839 int max_poc_lsb = 1 << log2_max_poc_lsb;
2472 32839 int prev_poc_lsb = pocTid0 % max_poc_lsb;
2473 32839 int prev_poc_msb = pocTid0 - prev_poc_lsb;
2474 int poc_msb;
2475
2476
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)
2477 40 poc_msb = prev_poc_msb + max_poc_lsb;
2478
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)
2479 344 poc_msb = prev_poc_msb - max_poc_lsb;
2480 else
2481 32455 poc_msb = prev_poc_msb;
2482
2483 // For BLA picture types, POCmsb is set to 0.
2484
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 ||
2485
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 32815 times.
32818 nal_unit_type == HEVC_NAL_BLA_W_RADL ||
2486 nal_unit_type == HEVC_NAL_BLA_N_LP)
2487 24 poc_msb = 0;
2488
2489 32839 return poc_msb + poc_lsb;
2490 }
2491