FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/ps.c
Date: 2026-07-22 00:55:30
Exec Total Coverage
Lines: 1084 1541 70.3%
Functions: 30 31 96.8%
Branches: 672 1067 63.0%

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