FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc_ps.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 896 1273 70.4%
Functions: 26 27 96.3%
Branches: 534 867 61.6%

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 "golomb.h"
28 #include "h2645_vui.h"
29 #include "hevc_data.h"
30 #include "hevc_ps.h"
31 #include "refstruct.h"
32
33 static const uint8_t default_scaling_list_intra[] = {
34 16, 16, 16, 16, 17, 18, 21, 24,
35 16, 16, 16, 16, 17, 19, 22, 25,
36 16, 16, 17, 18, 20, 22, 25, 29,
37 16, 16, 18, 21, 24, 27, 31, 36,
38 17, 17, 20, 24, 30, 35, 41, 47,
39 18, 19, 22, 27, 35, 44, 54, 65,
40 21, 22, 25, 31, 41, 54, 70, 88,
41 24, 25, 29, 36, 47, 65, 88, 115
42 };
43
44 static const uint8_t default_scaling_list_inter[] = {
45 16, 16, 16, 16, 17, 18, 20, 24,
46 16, 16, 16, 17, 18, 20, 24, 25,
47 16, 16, 17, 18, 20, 24, 25, 28,
48 16, 17, 18, 20, 24, 25, 28, 33,
49 17, 18, 20, 24, 25, 28, 33, 41,
50 18, 20, 24, 25, 28, 33, 41, 54,
51 20, 24, 25, 28, 33, 41, 54, 71,
52 24, 25, 28, 33, 41, 54, 71, 91
53 };
54
55 static const uint8_t hevc_sub_width_c[] = {
56 1, 2, 2, 1
57 };
58
59 static const uint8_t hevc_sub_height_c[] = {
60 1, 2, 1, 1
61 };
62
63 3278 static void remove_pps(HEVCParamSets *s, int id)
64 {
65
2/2
✓ Branch 0 taken 2614 times.
✓ Branch 1 taken 664 times.
3278 if (s->pps == s->pps_list[id])
66 2614 s->pps = NULL;
67 3278 ff_refstruct_unref(&s->pps_list[id]);
68 3278 }
69
70 621 static void remove_sps(HEVCParamSets *s, int id)
71 {
72 int i;
73
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 610 times.
621 if (s->sps_list[id]) {
74
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (s->sps == s->sps_list[id])
75 11 s->sps = NULL;
76
77 /* drop all PPS that depend on this SPS */
78
2/2
✓ Branch 0 taken 704 times.
✓ Branch 1 taken 11 times.
715 for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++)
79
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 693 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
704 if (s->pps_list[i] && s->pps_list[i]->sps_id == id)
80 11 remove_pps(s, i);
81
82
2/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
11 av_assert0(!(s->sps_list[id] && s->sps == s->sps_list[id]));
83 11 ff_refstruct_unref(&s->sps_list[id]);
84 }
85 621 }
86
87 608 static void remove_vps(HEVCParamSets *s, int id)
88 {
89 int i;
90
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 605 times.
608 if (s->vps_list[id]) {
91
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (s->vps == s->vps_list[id])
92 3 s->vps = NULL;
93
94
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 3 times.
51 for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++)
95
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
48 if (s->sps_list[i] && s->sps_list[i]->vps_id == id)
96 3 remove_sps(s, i);
97 3 ff_refstruct_unref(&s->vps_list[id]);
98 }
99 608 }
100
101 12487 int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx,
102 ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header)
103 {
104 int delta_poc;
105 12487 int k0 = 0;
106 12487 int k = 0;
107 int i;
108
109 12487 rps->rps_predict = 0;
110
111
4/4
✓ Branch 0 taken 11578 times.
✓ Branch 1 taken 909 times.
✓ Branch 2 taken 11294 times.
✓ Branch 3 taken 284 times.
12487 if (rps != sps->st_rps && sps->nb_st_rps)
112 11294 rps->rps_predict = get_bits1(gb);
113
114
2/2
✓ Branch 0 taken 7522 times.
✓ Branch 1 taken 4965 times.
12487 if (rps->rps_predict) {
115 const ShortTermRPS *rps_ridx;
116 int delta_rps;
117
118
2/2
✓ Branch 0 taken 619 times.
✓ Branch 1 taken 6903 times.
7522 if (is_slice_header) {
119 619 rps->delta_idx = get_ue_golomb_long(gb) + 1;
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 619 times.
619 if (rps->delta_idx > sps->nb_st_rps) {
121 av_log(avctx, AV_LOG_ERROR,
122 "Invalid value of delta_idx in slice header RPS: %d > %d.\n",
123 rps->delta_idx, sps->nb_st_rps);
124 return AVERROR_INVALIDDATA;
125 }
126 619 rps_ridx = &sps->st_rps[sps->nb_st_rps - rps->delta_idx];
127 619 rps->rps_idx_num_delta_pocs = rps_ridx->num_delta_pocs;
128 } else
129 6903 rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
130
131 7522 rps->delta_rps_sign = get_bits1(gb);
132 7522 rps->abs_delta_rps = get_ue_golomb_long(gb) + 1;
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7522 times.
7522 if (rps->abs_delta_rps > 32768) {
134 av_log(avctx, AV_LOG_ERROR,
135 "Invalid value of abs_delta_rps: %d\n",
136 rps->abs_delta_rps);
137 return AVERROR_INVALIDDATA;
138 }
139 7522 delta_rps = (1 - (rps->delta_rps_sign << 1)) * rps->abs_delta_rps;
140
2/2
✓ Branch 0 taken 32614 times.
✓ Branch 1 taken 7522 times.
40136 for (i = 0; i <= rps_ridx->num_delta_pocs; i++) {
141 32614 int used = rps->used[k] = get_bits1(gb);
142
143 32614 rps->use_delta_flag = 0;
144
2/2
✓ Branch 0 taken 6465 times.
✓ Branch 1 taken 26149 times.
32614 if (!used)
145 6465 rps->use_delta_flag = get_bits1(gb);
146
147
4/4
✓ Branch 0 taken 6465 times.
✓ Branch 1 taken 26149 times.
✓ Branch 2 taken 50 times.
✓ Branch 3 taken 6415 times.
32614 if (used || rps->use_delta_flag) {
148
2/2
✓ Branch 0 taken 20649 times.
✓ Branch 1 taken 5550 times.
26199 if (i < rps_ridx->num_delta_pocs)
149 20649 delta_poc = delta_rps + rps_ridx->delta_poc[i];
150 else
151 5550 delta_poc = delta_rps;
152 26199 rps->delta_poc[k] = delta_poc;
153
2/2
✓ Branch 0 taken 18140 times.
✓ Branch 1 taken 8059 times.
26199 if (delta_poc < 0)
154 18140 k0++;
155 26199 k++;
156 }
157 }
158
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7522 times.
7522 if (k >= FF_ARRAY_ELEMS(rps->used)) {
160 av_log(avctx, AV_LOG_ERROR,
161 "Invalid num_delta_pocs: %d\n", k);
162 return AVERROR_INVALIDDATA;
163 }
164
165 7522 rps->num_delta_pocs = k;
166 7522 rps->num_negative_pics = k0;
167 // sort in increasing order (smallest first)
168
1/2
✓ Branch 0 taken 7522 times.
✗ Branch 1 not taken.
7522 if (rps->num_delta_pocs != 0) {
169 int used, tmp;
170
2/2
✓ Branch 0 taken 18677 times.
✓ Branch 1 taken 7522 times.
26199 for (i = 1; i < rps->num_delta_pocs; i++) {
171 18677 delta_poc = rps->delta_poc[i];
172 18677 used = rps->used[i];
173
2/2
✓ Branch 0 taken 34794 times.
✓ Branch 1 taken 18677 times.
53471 for (k = i - 1; k >= 0; k--) {
174 34794 tmp = rps->delta_poc[k];
175
2/2
✓ Branch 0 taken 11103 times.
✓ Branch 1 taken 23691 times.
34794 if (delta_poc < tmp) {
176 11103 rps->delta_poc[k + 1] = tmp;
177 11103 rps->used[k + 1] = rps->used[k];
178 11103 rps->delta_poc[k] = delta_poc;
179 11103 rps->used[k] = used;
180 }
181 }
182 }
183 }
184
2/2
✓ Branch 0 taken 5707 times.
✓ Branch 1 taken 1815 times.
7522 if ((rps->num_negative_pics >> 1) != 0) {
185 int used;
186 5707 k = rps->num_negative_pics - 1;
187 // flip the negative values to largest first
188
2/2
✓ Branch 0 taken 7480 times.
✓ Branch 1 taken 5707 times.
13187 for (i = 0; i < rps->num_negative_pics >> 1; i++) {
189 7480 delta_poc = rps->delta_poc[i];
190 7480 used = rps->used[i];
191 7480 rps->delta_poc[i] = rps->delta_poc[k];
192 7480 rps->used[i] = rps->used[k];
193 7480 rps->delta_poc[k] = delta_poc;
194 7480 rps->used[k] = used;
195 7480 k--;
196 }
197 }
198 } else {
199 unsigned int prev, nb_positive_pics;
200 4965 rps->num_negative_pics = get_ue_golomb_long(gb);
201 4965 nb_positive_pics = get_ue_golomb_long(gb);
202
203
2/4
✓ Branch 0 taken 4965 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4965 times.
4965 if (rps->num_negative_pics >= HEVC_MAX_REFS ||
204 nb_positive_pics >= HEVC_MAX_REFS) {
205 av_log(avctx, AV_LOG_ERROR, "Too many refs in a short term RPS.\n");
206 return AVERROR_INVALIDDATA;
207 }
208
209 4965 rps->num_delta_pocs = rps->num_negative_pics + nb_positive_pics;
210
2/2
✓ Branch 0 taken 4638 times.
✓ Branch 1 taken 327 times.
4965 if (rps->num_delta_pocs) {
211 4638 prev = 0;
212
2/2
✓ Branch 0 taken 13937 times.
✓ Branch 1 taken 4638 times.
18575 for (i = 0; i < rps->num_negative_pics; i++) {
213 13937 delta_poc = rps->delta_poc_s0[i] = get_ue_golomb_long(gb) + 1;
214
2/4
✓ Branch 0 taken 13937 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13937 times.
13937 if (delta_poc < 1 || delta_poc > 32768) {
215 av_log(avctx, AV_LOG_ERROR,
216 "Invalid value of delta_poc: %d\n",
217 delta_poc);
218 return AVERROR_INVALIDDATA;
219 }
220 13937 prev -= delta_poc;
221 13937 rps->delta_poc[i] = prev;
222 13937 rps->used[i] = get_bits1(gb);
223 }
224 4638 prev = 0;
225
2/2
✓ Branch 0 taken 1197 times.
✓ Branch 1 taken 4638 times.
5835 for (i = 0; i < nb_positive_pics; i++) {
226 1197 delta_poc = rps->delta_poc_s1[i] = get_ue_golomb_long(gb) + 1;
227
2/4
✓ Branch 0 taken 1197 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1197 times.
1197 if (delta_poc < 1 || delta_poc > 32768) {
228 av_log(avctx, AV_LOG_ERROR,
229 "Invalid value of delta_poc: %d\n",
230 delta_poc);
231 return AVERROR_INVALIDDATA;
232 }
233 1197 prev += delta_poc;
234 1197 rps->delta_poc[rps->num_negative_pics + i] = prev;
235 1197 rps->used[rps->num_negative_pics + i] = get_bits1(gb);
236 }
237 }
238 }
239 12487 return 0;
240 }
241
242
243 2153 static int decode_profile_tier_level(GetBitContext *gb, AVCodecContext *avctx,
244 PTLCommon *ptl)
245 {
246 int i;
247
248
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2153 times.
2153 if (get_bits_left(gb) < 2+1+5 + 32 + 4 + 43 + 1)
249 return -1;
250
251 2153 ptl->profile_space = get_bits(gb, 2);
252 2153 ptl->tier_flag = get_bits1(gb);
253 2153 ptl->profile_idc = get_bits(gb, 5);
254
2/2
✓ Branch 0 taken 1771 times.
✓ Branch 1 taken 382 times.
2153 if (ptl->profile_idc == AV_PROFILE_HEVC_MAIN)
255 1771 av_log(avctx, AV_LOG_DEBUG, "Main profile bitstream\n");
256
2/2
✓ Branch 0 taken 194 times.
✓ Branch 1 taken 188 times.
382 else if (ptl->profile_idc == AV_PROFILE_HEVC_MAIN_10)
257 194 av_log(avctx, AV_LOG_DEBUG, "Main 10 profile bitstream\n");
258
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 180 times.
188 else if (ptl->profile_idc == AV_PROFILE_HEVC_MAIN_STILL_PICTURE)
259 8 av_log(avctx, AV_LOG_DEBUG, "Main Still Picture profile bitstream\n");
260
2/2
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 50 times.
180 else if (ptl->profile_idc == AV_PROFILE_HEVC_REXT)
261 130 av_log(avctx, AV_LOG_DEBUG, "Range Extension profile bitstream\n");
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 else if (ptl->profile_idc == AV_PROFILE_HEVC_SCC)
263 av_log(avctx, AV_LOG_DEBUG, "Screen Content Coding Extension profile bitstream\n");
264 else
265 50 av_log(avctx, AV_LOG_WARNING, "Unknown HEVC profile: %d\n", ptl->profile_idc);
266
267
2/2
✓ Branch 0 taken 68896 times.
✓ Branch 1 taken 2153 times.
71049 for (i = 0; i < 32; i++) {
268 68896 ptl->profile_compatibility_flag[i] = get_bits1(gb);
269
270
6/6
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 68776 times.
✓ Branch 2 taken 80 times.
✓ Branch 3 taken 40 times.
✓ Branch 4 taken 40 times.
✓ Branch 5 taken 40 times.
68896 if (ptl->profile_idc == 0 && i > 0 && ptl->profile_compatibility_flag[i])
271 40 ptl->profile_idc = i;
272 }
273 2153 ptl->progressive_source_flag = get_bits1(gb);
274 2153 ptl->interlaced_source_flag = get_bits1(gb);
275 2153 ptl->non_packed_constraint_flag = get_bits1(gb);
276 2153 ptl->frame_only_constraint_flag = get_bits1(gb);
277
278 #define check_profile_idc(idc) \
279 ptl->profile_idc == idc || ptl->profile_compatibility_flag[idc]
280
281
8/12
✓ Branch 0 taken 2023 times.
✓ Branch 1 taken 130 times.
✓ Branch 2 taken 2023 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2013 times.
✓ Branch 5 taken 10 times.
✓ Branch 6 taken 2013 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2013 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2013 times.
✗ Branch 11 not taken.
2153 if (check_profile_idc(4) || check_profile_idc(5) || check_profile_idc(6) ||
282
6/12
✓ Branch 0 taken 2013 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2013 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2013 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2013 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2013 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2013 times.
✗ Branch 11 not taken.
2013 check_profile_idc(7) || check_profile_idc(8) || check_profile_idc(9) ||
283
2/4
✓ Branch 0 taken 2013 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2013 times.
2013 check_profile_idc(10)) {
284
285 140 ptl->max_12bit_constraint_flag = get_bits1(gb);
286 140 ptl->max_10bit_constraint_flag = get_bits1(gb);
287 140 ptl->max_8bit_constraint_flag = get_bits1(gb);
288 140 ptl->max_422chroma_constraint_flag = get_bits1(gb);
289 140 ptl->max_420chroma_constraint_flag = get_bits1(gb);
290 140 ptl->max_monochrome_constraint_flag = get_bits1(gb);
291 140 ptl->intra_constraint_flag = get_bits1(gb);
292 140 ptl->one_picture_only_constraint_flag = get_bits1(gb);
293 140 ptl->lower_bit_rate_constraint_flag = get_bits1(gb);
294
295
7/12
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 130 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 130 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 130 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 130 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 130 times.
140 if (check_profile_idc(5) || check_profile_idc(9) || check_profile_idc(10)) {
296 10 ptl->max_14bit_constraint_flag = get_bits1(gb);
297 10 skip_bits_long(gb, 33); // XXX_reserved_zero_33bits[0..32]
298 } else {
299 130 skip_bits_long(gb, 34); // XXX_reserved_zero_34bits[0..33]
300 }
301
4/4
✓ Branch 0 taken 1779 times.
✓ Branch 1 taken 234 times.
✓ Branch 2 taken 1467 times.
✓ Branch 3 taken 312 times.
2013 } else if (check_profile_idc(2)) {
302 1701 skip_bits(gb, 7);
303 1701 ptl->one_picture_only_constraint_flag = get_bits1(gb);
304 1701 skip_bits_long(gb, 35); // XXX_reserved_zero_35bits[0..34]
305 } else {
306 312 skip_bits_long(gb, 43); // XXX_reserved_zero_43bits[0..42]
307 }
308
309
9/12
✓ Branch 0 taken 382 times.
✓ Branch 1 taken 1771 times.
✓ Branch 2 taken 374 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 140 times.
✓ Branch 5 taken 234 times.
✓ Branch 6 taken 140 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 140 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 140 times.
✗ Branch 11 not taken.
2153 if (check_profile_idc(1) || check_profile_idc(2) || check_profile_idc(3) ||
310
4/12
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 130 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 10 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
140 check_profile_idc(4) || check_profile_idc(5) || check_profile_idc(9))
311 2153 ptl->inbld_flag = get_bits1(gb);
312 else
313 skip_bits1(gb);
314 #undef check_profile_idc
315
316 2153 return 0;
317 }
318
319 2101 static int parse_ptl(GetBitContext *gb, AVCodecContext *avctx,
320 PTL *ptl, int max_num_sub_layers)
321 {
322 int i;
323
2/4
✓ Branch 1 taken 2101 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2101 times.
4202 if (decode_profile_tier_level(gb, avctx, &ptl->general_ptl) < 0 ||
324
2/2
✓ Branch 1 taken 236 times.
✓ Branch 2 taken 1865 times.
2101 get_bits_left(gb) < 8 + (8*2 * (max_num_sub_layers - 1 > 0))) {
325 av_log(avctx, AV_LOG_ERROR, "PTL information too short\n");
326 return -1;
327 }
328
329 2101 ptl->general_ptl.level_idc = get_bits(gb, 8);
330
331
2/2
✓ Branch 0 taken 468 times.
✓ Branch 1 taken 2101 times.
2569 for (i = 0; i < max_num_sub_layers - 1; i++) {
332 468 ptl->sub_layer_profile_present_flag[i] = get_bits1(gb);
333 468 ptl->sub_layer_level_present_flag[i] = get_bits1(gb);
334 }
335
336
2/2
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 1865 times.
2101 if (max_num_sub_layers - 1> 0)
337
2/2
✓ Branch 0 taken 1420 times.
✓ Branch 1 taken 236 times.
1656 for (i = max_num_sub_layers - 1; i < 8; i++)
338 1420 skip_bits(gb, 2); // reserved_zero_2bits[i]
339
2/2
✓ Branch 0 taken 468 times.
✓ Branch 1 taken 2101 times.
2569 for (i = 0; i < max_num_sub_layers - 1; i++) {
340
3/4
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 416 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 52 times.
520 if (ptl->sub_layer_profile_present_flag[i] &&
341 52 decode_profile_tier_level(gb, avctx, &ptl->sub_layer_ptl[i]) < 0) {
342 av_log(avctx, AV_LOG_ERROR,
343 "PTL information for sublayer %i too short\n", i);
344 return -1;
345 }
346
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 416 times.
468 if (ptl->sub_layer_level_present_flag[i]) {
347
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 52 times.
52 if (get_bits_left(gb) < 8) {
348 av_log(avctx, AV_LOG_ERROR,
349 "Not enough data for sublayer %i level_idc\n", i);
350 return -1;
351 } else
352 52 ptl->sub_layer_ptl[i].level_idc = get_bits(gb, 8);
353 }
354 }
355
356 2101 return 0;
357 }
358
359 97 static void decode_sublayer_hrd(GetBitContext *gb, unsigned int nb_cpb,
360 HEVCSublayerHdrParams *par, int subpic_params_present)
361 {
362 int i;
363
364
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 97 times.
194 for (i = 0; i < nb_cpb; i++) {
365 97 par->bit_rate_value_minus1[i] = get_ue_golomb_long(gb);
366 97 par->cpb_size_value_minus1[i] = get_ue_golomb_long(gb);
367
368
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 71 times.
97 if (subpic_params_present) {
369 26 par->cpb_size_du_value_minus1[i] = get_ue_golomb_long(gb);
370 26 par->bit_rate_du_value_minus1[i] = get_ue_golomb_long(gb);
371 }
372
373 97 par->cbr_flag = get_bits1(gb);
374 }
375 97 }
376
377 51 static int decode_hrd(GetBitContext *gb, int common_inf_present,
378 HEVCHdrParams *hdr, int max_sublayers)
379 {
380
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
51 if (common_inf_present) {
381 51 hdr->flags.nal_hrd_parameters_present_flag = get_bits1(gb);
382 51 hdr->flags.vcl_hrd_parameters_present_flag = get_bits1(gb);
383
384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (hdr->flags.nal_hrd_parameters_present_flag ||
385 hdr->flags.vcl_hrd_parameters_present_flag) {
386 51 hdr->flags.sub_pic_hrd_params_present_flag = get_bits1(gb);
387
388
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 38 times.
51 if (hdr->flags.sub_pic_hrd_params_present_flag) {
389 13 hdr->tick_divisor_minus2 = get_bits(gb, 8);
390 13 hdr->du_cpb_removal_delay_increment_length_minus1 = get_bits(gb, 5);
391 13 hdr->flags.sub_pic_cpb_params_in_pic_timing_sei_flag = get_bits1(gb);
392 13 hdr->dpb_output_delay_du_length_minus1 = get_bits(gb, 5);
393 }
394
395 51 hdr->bit_rate_scale = get_bits(gb, 4);
396 51 hdr->cpb_size_scale = get_bits(gb, 4);
397
398
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 38 times.
51 if (hdr->flags.sub_pic_hrd_params_present_flag)
399 13 hdr->cpb_size_du_scale = get_bits(gb, 4);
400
401 51 hdr->initial_cpb_removal_delay_length_minus1 = get_bits(gb, 5);
402 51 hdr->au_cpb_removal_delay_length_minus1 = get_bits(gb, 5);
403 51 hdr->dpb_output_delay_length_minus1 = get_bits(gb, 5);
404 }
405 }
406
407
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 51 times.
102 for (int i = 0; i < max_sublayers; i++) {
408 51 hdr->flags.fixed_pic_rate_general_flag = get_bits1(gb);
409
410
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 28 times.
51 if (!hdr->flags.fixed_pic_rate_general_flag)
411 23 hdr->flags.fixed_pic_rate_within_cvs_flag = get_bits1(gb);
412
413
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
51 if (hdr->flags.fixed_pic_rate_within_cvs_flag ||
414
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 23 times.
51 hdr->flags.fixed_pic_rate_general_flag)
415 28 hdr->elemental_duration_in_tc_minus1[i] = get_ue_golomb_long(gb);
416 else
417 23 hdr->flags.low_delay_hrd_flag = get_bits1(gb);
418
419
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
51 if (!hdr->flags.low_delay_hrd_flag) {
420 51 unsigned cpb_cnt_minus1 = get_ue_golomb_long(gb);
421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (cpb_cnt_minus1 > 31) {
422 av_log(NULL, AV_LOG_ERROR, "nb_cpb %d invalid\n",
423 cpb_cnt_minus1);
424 return AVERROR_INVALIDDATA;
425 }
426 51 hdr->cpb_cnt_minus1[i] = cpb_cnt_minus1;
427 }
428
429
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
51 if (hdr->flags.nal_hrd_parameters_present_flag)
430 51 decode_sublayer_hrd(gb, hdr->cpb_cnt_minus1[i]+1, &hdr->nal_params[i],
431 51 hdr->flags.sub_pic_hrd_params_present_flag);
432
433
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 5 times.
51 if (hdr->flags.vcl_hrd_parameters_present_flag)
434 46 decode_sublayer_hrd(gb, hdr->cpb_cnt_minus1[i]+1, &hdr->vcl_params[i],
435 46 hdr->flags.sub_pic_hrd_params_present_flag);
436 }
437
438 51 return 0;
439 }
440
441 1054 int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx,
442 HEVCParamSets *ps)
443 {
444 int i,j;
445 1054 int vps_id = 0;
446 ptrdiff_t nal_size;
447 1054 HEVCVPS *vps = ff_refstruct_allocz(sizeof(*vps));
448
449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1054 times.
1054 if (!vps)
450 return AVERROR(ENOMEM);
451
452 1054 av_log(avctx, AV_LOG_DEBUG, "Decoding VPS\n");
453
454 1054 nal_size = gb->buffer_end - gb->buffer;
455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1054 times.
1054 if (nal_size > sizeof(vps->data)) {
456 av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized VPS "
457 "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
458 nal_size, sizeof(vps->data));
459 vps->data_size = sizeof(vps->data);
460 } else {
461 1054 vps->data_size = nal_size;
462 }
463 1054 memcpy(vps->data, gb->buffer, vps->data_size);
464
465 1054 vps_id = vps->vps_id = get_bits(gb, 4);
466
467
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1054 times.
1054 if (get_bits(gb, 2) != 3) { // vps_reserved_three_2bits
468 av_log(avctx, AV_LOG_ERROR, "vps_reserved_three_2bits is not three\n");
469 goto err;
470 }
471
472 1054 vps->vps_max_layers = get_bits(gb, 6) + 1;
473 1054 vps->vps_max_sub_layers = get_bits(gb, 3) + 1;
474 1054 vps->vps_temporal_id_nesting_flag = get_bits1(gb);
475
476
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1054 times.
1054 if (get_bits(gb, 16) != 0xffff) { // vps_reserved_ffff_16bits
477 av_log(avctx, AV_LOG_ERROR, "vps_reserved_ffff_16bits is not 0xffff\n");
478 goto err;
479 }
480
481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1054 times.
1054 if (vps->vps_max_sub_layers > HEVC_MAX_SUB_LAYERS) {
482 av_log(avctx, AV_LOG_ERROR, "vps_max_sub_layers out of range: %d\n",
483 vps->vps_max_sub_layers);
484 goto err;
485 }
486
487
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1054 times.
1054 if (parse_ptl(gb, avctx, &vps->ptl, vps->vps_max_sub_layers) < 0)
488 goto err;
489
490 1054 vps->vps_sub_layer_ordering_info_present_flag = get_bits1(gb);
491
492
2/2
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 894 times.
1054 i = vps->vps_sub_layer_ordering_info_present_flag ? 0 : vps->vps_max_sub_layers - 1;
493
2/2
✓ Branch 0 taken 1201 times.
✓ Branch 1 taken 1054 times.
2255 for (; i < vps->vps_max_sub_layers; i++) {
494 1201 vps->vps_max_dec_pic_buffering[i] = get_ue_golomb_long(gb) + 1;
495 1201 vps->vps_num_reorder_pics[i] = get_ue_golomb_long(gb);
496 1201 vps->vps_max_latency_increase[i] = get_ue_golomb_long(gb) - 1;
497
498
2/4
✓ Branch 0 taken 1201 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1201 times.
1201 if (vps->vps_max_dec_pic_buffering[i] > HEVC_MAX_DPB_SIZE || !vps->vps_max_dec_pic_buffering[i]) {
499 av_log(avctx, AV_LOG_ERROR, "vps_max_dec_pic_buffering_minus1 out of range: %d\n",
500 vps->vps_max_dec_pic_buffering[i] - 1);
501 goto err;
502 }
503
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1201 times.
1201 if (vps->vps_num_reorder_pics[i] > vps->vps_max_dec_pic_buffering[i] - 1) {
504 av_log(avctx, AV_LOG_WARNING, "vps_max_num_reorder_pics out of range: %d\n",
505 vps->vps_num_reorder_pics[i]);
506 if (avctx->err_recognition & AV_EF_EXPLODE)
507 goto err;
508 }
509 }
510
511 1054 vps->vps_max_layer_id = get_bits(gb, 6);
512 1054 vps->vps_num_layer_sets = get_ue_golomb_long(gb) + 1;
513
2/4
✓ Branch 0 taken 1054 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1054 times.
✗ Branch 3 not taken.
1054 if (vps->vps_num_layer_sets < 1 || vps->vps_num_layer_sets > 1024 ||
514
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1054 times.
1054 (vps->vps_num_layer_sets - 1LL) * (vps->vps_max_layer_id + 1LL) > get_bits_left(gb)) {
515 av_log(avctx, AV_LOG_ERROR, "too many layer_id_included_flags\n");
516 goto err;
517 }
518
519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1054 times.
1054 for (i = 1; i < vps->vps_num_layer_sets; i++)
520 for (j = 0; j <= vps->vps_max_layer_id; j++)
521 skip_bits(gb, 1); // layer_id_included_flag[i][j]
522
523 1054 vps->vps_timing_info_present_flag = get_bits1(gb);
524
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 1016 times.
1054 if (vps->vps_timing_info_present_flag) {
525 38 vps->vps_num_units_in_tick = get_bits_long(gb, 32);
526 38 vps->vps_time_scale = get_bits_long(gb, 32);
527 38 vps->vps_poc_proportional_to_timing_flag = get_bits1(gb);
528
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 if (vps->vps_poc_proportional_to_timing_flag)
529 38 vps->vps_num_ticks_poc_diff_one = get_ue_golomb_long(gb) + 1;
530 38 vps->vps_num_hrd_parameters = get_ue_golomb_long(gb);
531
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (vps->vps_num_hrd_parameters > (unsigned)vps->vps_num_layer_sets) {
532 av_log(avctx, AV_LOG_ERROR,
533 "vps_num_hrd_parameters %d is invalid\n", vps->vps_num_hrd_parameters);
534 goto err;
535 }
536
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 38 times.
43 for (i = 0; i < vps->vps_num_hrd_parameters; i++) {
537 5 int common_inf_present = 1;
538
539 5 get_ue_golomb_long(gb); // hrd_layer_set_idx
540
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (i)
541 common_inf_present = get_bits1(gb);
542 5 decode_hrd(gb, common_inf_present, &vps->hdr[i],
543 5 vps->vps_max_sub_layers);
544 }
545 }
546 1054 get_bits1(gb); /* vps_extension_flag */
547
548
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1054 times.
1054 if (get_bits_left(gb) < 0) {
549 av_log(avctx, AV_LOG_ERROR,
550 "Overread VPS by %d bits\n", -get_bits_left(gb));
551 if (ps->vps_list[vps_id])
552 goto err;
553 }
554
555
2/2
✓ Branch 0 taken 449 times.
✓ Branch 1 taken 605 times.
1054 if (ps->vps_list[vps_id] &&
556
2/2
✓ Branch 0 taken 446 times.
✓ Branch 1 taken 3 times.
449 !memcmp(ps->vps_list[vps_id], vps, sizeof(*vps))) {
557 446 ff_refstruct_unref(&vps);
558 } else {
559 608 remove_vps(ps, vps_id);
560 608 ps->vps_list[vps_id] = vps;
561 }
562
563 1054 return 0;
564
565 err:
566 ff_refstruct_unref(&vps);
567 return AVERROR_INVALIDDATA;
568 }
569
570 269 static void decode_vui(GetBitContext *gb, AVCodecContext *avctx,
571 int apply_defdispwin, HEVCSPS *sps)
572 {
573 269 VUI backup_vui, *vui = &sps->vui;
574 GetBitContext backup;
575 269 int alt = 0;
576
577 269 ff_h2645_decode_common_vui_params(gb, &sps->vui.common, avctx);
578
579
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 215 times.
269 if (vui->common.video_signal_type_present_flag) {
580
4/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 5 times.
54 if (vui->common.video_full_range_flag && sps->pix_fmt == AV_PIX_FMT_YUV420P)
581 5 sps->pix_fmt = AV_PIX_FMT_YUVJ420P;
582
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 5 times.
54 if (vui->common.colour_description_present_flag) {
583
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (vui->common.matrix_coeffs == AVCOL_SPC_RGB) {
584 switch (sps->pix_fmt) {
585 case AV_PIX_FMT_YUV444P:
586 sps->pix_fmt = AV_PIX_FMT_GBRP;
587 break;
588 case AV_PIX_FMT_YUV444P10:
589 sps->pix_fmt = AV_PIX_FMT_GBRP10;
590 break;
591 case AV_PIX_FMT_YUV444P12:
592 sps->pix_fmt = AV_PIX_FMT_GBRP12;
593 break;
594 }
595 }
596 }
597 }
598
599 269 vui->neutra_chroma_indication_flag = get_bits1(gb);
600 269 vui->field_seq_flag = get_bits1(gb);
601 269 vui->frame_field_info_present_flag = get_bits1(gb);
602
603 // Backup context in case an alternate header is detected
604 269 memcpy(&backup, gb, sizeof(backup));
605 269 memcpy(&backup_vui, vui, sizeof(backup_vui));
606
3/4
✓ Branch 1 taken 94 times.
✓ Branch 2 taken 175 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 94 times.
269 if (get_bits_left(gb) >= 68 && show_bits(gb, 21) == 0x100000) {
607 vui->default_display_window_flag = 0;
608 av_log(avctx, AV_LOG_WARNING, "Invalid default display window\n");
609 } else
610 269 vui->default_display_window_flag = get_bits1(gb);
611
612
2/2
✓ Branch 0 taken 159 times.
✓ Branch 1 taken 110 times.
269 if (vui->default_display_window_flag) {
613 110 int vert_mult = hevc_sub_height_c[sps->chroma_format_idc];
614 110 int horiz_mult = hevc_sub_width_c[sps->chroma_format_idc];
615 110 vui->def_disp_win.left_offset = get_ue_golomb_long(gb) * horiz_mult;
616 110 vui->def_disp_win.right_offset = get_ue_golomb_long(gb) * horiz_mult;
617 110 vui->def_disp_win.top_offset = get_ue_golomb_long(gb) * vert_mult;
618 110 vui->def_disp_win.bottom_offset = get_ue_golomb_long(gb) * vert_mult;
619
620
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 52 times.
110 if (apply_defdispwin &&
621
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {
622 av_log(avctx, AV_LOG_DEBUG,
623 "discarding vui default display window, "
624 "original values are l:%u r:%u t:%u b:%u\n",
625 vui->def_disp_win.left_offset,
626 vui->def_disp_win.right_offset,
627 vui->def_disp_win.top_offset,
628 vui->def_disp_win.bottom_offset);
629
630 vui->def_disp_win.left_offset =
631 vui->def_disp_win.right_offset =
632 vui->def_disp_win.top_offset =
633 vui->def_disp_win.bottom_offset = 0;
634 }
635 }
636
637 269 timing_info:
638 269 vui->vui_timing_info_present_flag = get_bits1(gb);
639
640
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 175 times.
269 if (vui->vui_timing_info_present_flag) {
641
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 94 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
94 if( get_bits_left(gb) < 66 && !alt) {
642 // The alternate syntax seem to have timing info located
643 // at where def_disp_win is normally located
644 av_log(avctx, AV_LOG_WARNING,
645 "Strange VUI timing information, retrying...\n");
646 memcpy(vui, &backup_vui, sizeof(backup_vui));
647 memcpy(gb, &backup, sizeof(backup));
648 alt = 1;
649 goto timing_info;
650 }
651 94 vui->vui_num_units_in_tick = get_bits_long(gb, 32);
652 94 vui->vui_time_scale = get_bits_long(gb, 32);
653
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 94 times.
94 if (alt) {
654 av_log(avctx, AV_LOG_INFO, "Retry got %"PRIu32"/%"PRIu32" fps\n",
655 vui->vui_time_scale, vui->vui_num_units_in_tick);
656 }
657 94 vui->vui_poc_proportional_to_timing_flag = get_bits1(gb);
658
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 56 times.
94 if (vui->vui_poc_proportional_to_timing_flag)
659 38 vui->vui_num_ticks_poc_diff_one_minus1 = get_ue_golomb_long(gb);
660 94 vui->vui_hrd_parameters_present_flag = get_bits1(gb);
661
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 48 times.
94 if (vui->vui_hrd_parameters_present_flag)
662 46 decode_hrd(gb, 1, &sps->hdr, sps->max_sub_layers);
663 }
664
665 269 vui->bitstream_restriction_flag = get_bits1(gb);
666
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 157 times.
269 if (vui->bitstream_restriction_flag) {
667
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
112 if (get_bits_left(gb) < 8 && !alt) {
668 av_log(avctx, AV_LOG_WARNING,
669 "Strange VUI bitstream restriction information, retrying"
670 " from timing information...\n");
671 memcpy(vui, &backup_vui, sizeof(backup_vui));
672 memcpy(gb, &backup, sizeof(backup));
673 alt = 1;
674 goto timing_info;
675 }
676 112 vui->tiles_fixed_structure_flag = get_bits1(gb);
677 112 vui->motion_vectors_over_pic_boundaries_flag = get_bits1(gb);
678 112 vui->restricted_ref_pic_lists_flag = get_bits1(gb);
679 112 vui->min_spatial_segmentation_idc = get_ue_golomb_long(gb);
680 112 vui->max_bytes_per_pic_denom = get_ue_golomb_long(gb);
681 112 vui->max_bits_per_min_cu_denom = get_ue_golomb_long(gb);
682 112 vui->log2_max_mv_length_horizontal = get_ue_golomb_long(gb);
683 112 vui->log2_max_mv_length_vertical = get_ue_golomb_long(gb);
684 }
685
686
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 269 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
269 if (get_bits_left(gb) < 1 && !alt) {
687 // XXX: Alternate syntax when sps_range_extension_flag != 0?
688 av_log(avctx, AV_LOG_WARNING,
689 "Overread in VUI, retrying from timing information...\n");
690 memcpy(vui, &backup_vui, sizeof(backup_vui));
691 memcpy(gb, &backup, sizeof(backup));
692 alt = 1;
693 goto timing_info;
694 }
695 269 }
696
697 517 static void set_default_scaling_list_data(ScalingList *sl)
698 {
699 int matrixId;
700
701
2/2
✓ Branch 0 taken 3102 times.
✓ Branch 1 taken 517 times.
3619 for (matrixId = 0; matrixId < 6; matrixId++) {
702 // 4x4 default is 16
703 3102 memset(sl->sl[0][matrixId], 16, 16);
704 3102 sl->sl_dc[0][matrixId] = 16; // default for 16x16
705 3102 sl->sl_dc[1][matrixId] = 16; // default for 32x32
706 }
707 517 memcpy(sl->sl[1][0], default_scaling_list_intra, 64);
708 517 memcpy(sl->sl[1][1], default_scaling_list_intra, 64);
709 517 memcpy(sl->sl[1][2], default_scaling_list_intra, 64);
710 517 memcpy(sl->sl[1][3], default_scaling_list_inter, 64);
711 517 memcpy(sl->sl[1][4], default_scaling_list_inter, 64);
712 517 memcpy(sl->sl[1][5], default_scaling_list_inter, 64);
713 517 memcpy(sl->sl[2][0], default_scaling_list_intra, 64);
714 517 memcpy(sl->sl[2][1], default_scaling_list_intra, 64);
715 517 memcpy(sl->sl[2][2], default_scaling_list_intra, 64);
716 517 memcpy(sl->sl[2][3], default_scaling_list_inter, 64);
717 517 memcpy(sl->sl[2][4], default_scaling_list_inter, 64);
718 517 memcpy(sl->sl[2][5], default_scaling_list_inter, 64);
719 517 memcpy(sl->sl[3][0], default_scaling_list_intra, 64);
720 517 memcpy(sl->sl[3][1], default_scaling_list_intra, 64);
721 517 memcpy(sl->sl[3][2], default_scaling_list_intra, 64);
722 517 memcpy(sl->sl[3][3], default_scaling_list_inter, 64);
723 517 memcpy(sl->sl[3][4], default_scaling_list_inter, 64);
724 517 memcpy(sl->sl[3][5], default_scaling_list_inter, 64);
725 517 }
726
727 498 static int scaling_list_data(GetBitContext *gb, AVCodecContext *avctx,
728 ScalingList *sl, const HEVCSPS *sps)
729 {
730 uint8_t scaling_list_pred_mode_flag;
731 uint8_t scaling_list_dc_coef[2][6];
732 int size_id, matrix_id, pos;
733 int i;
734
735
2/2
✓ Branch 0 taken 1992 times.
✓ Branch 1 taken 498 times.
2490 for (size_id = 0; size_id < 4; size_id++)
736
4/4
✓ Branch 0 taken 996 times.
✓ Branch 1 taken 8964 times.
✓ Branch 2 taken 9960 times.
✓ Branch 3 taken 1992 times.
11952 for (matrix_id = 0; matrix_id < 6; matrix_id += ((size_id == 3) ? 3 : 1)) {
737 9960 scaling_list_pred_mode_flag = get_bits1(gb);
738
2/2
✓ Branch 0 taken 3978 times.
✓ Branch 1 taken 5982 times.
9960 if (!scaling_list_pred_mode_flag) {
739 3978 unsigned int delta = get_ue_golomb_long(gb);
740 /* Only need to handle non-zero delta. Zero means default,
741 * which should already be in the arrays. */
742
2/2
✓ Branch 0 taken 3852 times.
✓ Branch 1 taken 126 times.
3978 if (delta) {
743 // Copy from previous array.
744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3852 times.
3852 delta *= (size_id == 3) ? 3 : 1;
745
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3852 times.
3852 if (matrix_id < delta) {
746 av_log(avctx, AV_LOG_ERROR,
747 "Invalid delta in scaling list data: %d.\n", delta);
748 return AVERROR_INVALIDDATA;
749 }
750
751
2/2
✓ Branch 0 taken 2231 times.
✓ Branch 1 taken 1621 times.
3852 memcpy(sl->sl[size_id][matrix_id],
752 3852 sl->sl[size_id][matrix_id - delta],
753 size_id > 0 ? 64 : 16);
754
2/2
✓ Branch 0 taken 530 times.
✓ Branch 1 taken 3322 times.
3852 if (size_id > 1)
755 530 sl->sl_dc[size_id - 2][matrix_id] = sl->sl_dc[size_id - 2][matrix_id - delta];
756 }
757 } else {
758 int next_coef, coef_num;
759 int32_t scaling_list_delta_coef;
760
761 5982 next_coef = 8;
762 5982 coef_num = FFMIN(64, 1 << (4 + (size_id << 1)));
763
2/2
✓ Branch 0 taken 3412 times.
✓ Branch 1 taken 2570 times.
5982 if (size_id > 1) {
764 3412 int scaling_list_coeff_minus8 = get_se_golomb(gb);
765
2/4
✓ Branch 0 taken 3412 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3412 times.
3412 if (scaling_list_coeff_minus8 < -7 ||
766 scaling_list_coeff_minus8 > 247)
767 return AVERROR_INVALIDDATA;
768 3412 scaling_list_dc_coef[size_id - 2][matrix_id] = scaling_list_coeff_minus8 + 8;
769 3412 next_coef = scaling_list_dc_coef[size_id - 2][matrix_id];
770 3412 sl->sl_dc[size_id - 2][matrix_id] = next_coef;
771 }
772
2/2
✓ Branch 0 taken 318240 times.
✓ Branch 1 taken 5982 times.
324222 for (i = 0; i < coef_num; i++) {
773
2/2
✓ Branch 0 taken 21536 times.
✓ Branch 1 taken 296704 times.
318240 if (size_id == 0)
774 21536 pos = 4 * ff_hevc_diag_scan4x4_y[i] +
775 21536 ff_hevc_diag_scan4x4_x[i];
776 else
777 296704 pos = 8 * ff_hevc_diag_scan8x8_y[i] +
778 296704 ff_hevc_diag_scan8x8_x[i];
779
780 318240 scaling_list_delta_coef = get_se_golomb(gb);
781 318240 next_coef = (next_coef + 256U + scaling_list_delta_coef) % 256;
782 318240 sl->sl[size_id][matrix_id][pos] = next_coef;
783 }
784 }
785 }
786
787
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 455 times.
498 if (sps->chroma_format_idc == 3) {
788
2/2
✓ Branch 0 taken 2752 times.
✓ Branch 1 taken 43 times.
2795 for (i = 0; i < 64; i++) {
789 2752 sl->sl[3][1][i] = sl->sl[2][1][i];
790 2752 sl->sl[3][2][i] = sl->sl[2][2][i];
791 2752 sl->sl[3][4][i] = sl->sl[2][4][i];
792 2752 sl->sl[3][5][i] = sl->sl[2][5][i];
793 }
794 43 sl->sl_dc[1][1] = sl->sl_dc[0][1];
795 43 sl->sl_dc[1][2] = sl->sl_dc[0][2];
796 43 sl->sl_dc[1][4] = sl->sl_dc[0][4];
797 43 sl->sl_dc[1][5] = sl->sl_dc[0][5];
798 }
799
800
801 498 return 0;
802 }
803
804 1047 static int map_pixel_format(AVCodecContext *avctx, HEVCSPS *sps)
805 {
806 const AVPixFmtDescriptor *desc;
807
3/5
✓ Branch 0 taken 898 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 126 times.
✓ Branch 3 taken 23 times.
✗ Branch 4 not taken.
1047 switch (sps->bit_depth) {
808 898 case 8:
809
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 894 times.
898 if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY8;
810
2/2
✓ Branch 0 taken 885 times.
✓ Branch 1 taken 13 times.
898 if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P;
811
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 898 times.
898 if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P;
812
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 889 times.
898 if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P;
813 898 break;
814 case 9:
815 if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY9;
816 if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P9;
817 if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P9;
818 if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P9;
819 break;
820 126 case 10:
821
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
126 if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY10;
822
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 29 times.
126 if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P10;
823
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 97 times.
126 if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P10;
824
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
126 if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P10;
825 126 break;
826 23 case 12:
827
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;
828
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;
829
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;
830
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;
831 23 break;
832 default:
833 av_log(avctx, AV_LOG_ERROR,
834 "The following bit-depths are currently specified: 8, 9, 10 and 12 bits, "
835 "chroma_format_idc is %d, depth is %d\n",
836 sps->chroma_format_idc, sps->bit_depth);
837 return AVERROR_INVALIDDATA;
838 }
839
840 1047 desc = av_pix_fmt_desc_get(sps->pix_fmt);
841
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (!desc)
842 return AVERROR(EINVAL);
843
844 1047 sps->hshift[0] = sps->vshift[0] = 0;
845 1047 sps->hshift[2] = sps->hshift[1] = desc->log2_chroma_w;
846 1047 sps->vshift[2] = sps->vshift[1] = desc->log2_chroma_h;
847
848 1047 sps->pixel_shift = sps->bit_depth > 8;
849
850 1047 return 0;
851 }
852
853 1047 int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id,
854 int apply_defdispwin, const HEVCVPS * const *vps_list,
855 AVCodecContext *avctx)
856 {
857 HEVCWindow *ow;
858 1047 int ret = 0;
859 int bit_depth_chroma, start, num_comps;
860 int i;
861
862 // Coded parameters
863
864 1047 sps->vps_id = get_bits(gb, 4);
865
866
2/4
✓ Branch 0 taken 1047 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1047 times.
1047 if (vps_list && !vps_list[sps->vps_id]) {
867 av_log(avctx, AV_LOG_ERROR, "VPS %d does not exist\n",
868 sps->vps_id);
869 return AVERROR_INVALIDDATA;
870 }
871
872 1047 sps->max_sub_layers = get_bits(gb, 3) + 1;
873
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (sps->max_sub_layers > HEVC_MAX_SUB_LAYERS) {
874 av_log(avctx, AV_LOG_ERROR, "sps_max_sub_layers out of range: %d\n",
875 sps->max_sub_layers);
876 return AVERROR_INVALIDDATA;
877 }
878
879 1047 sps->temporal_id_nesting_flag = get_bits(gb, 1);
880
881
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1047 times.
1047 if ((ret = parse_ptl(gb, avctx, &sps->ptl, sps->max_sub_layers)) < 0)
882 return ret;
883
884 1047 *sps_id = get_ue_golomb_long(gb);
885
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (*sps_id >= HEVC_MAX_SPS_COUNT) {
886 av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", *sps_id);
887 return AVERROR_INVALIDDATA;
888 }
889
890 1047 sps->chroma_format_idc = get_ue_golomb_long(gb);
891
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (sps->chroma_format_idc > 3U) {
892 av_log(avctx, AV_LOG_ERROR, "chroma_format_idc %d is invalid\n", sps->chroma_format_idc);
893 return AVERROR_INVALIDDATA;
894 }
895
896
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1015 times.
1047 if (sps->chroma_format_idc == 3)
897 32 sps->separate_colour_plane_flag = get_bits1(gb);
898
899
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (sps->separate_colour_plane_flag)
900 sps->chroma_format_idc = 0;
901
902 1047 sps->width = get_ue_golomb_long(gb);
903 1047 sps->height = get_ue_golomb_long(gb);
904
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if ((ret = av_image_check_size(sps->width,
905 1047 sps->height, 0, avctx)) < 0)
906 return ret;
907
908 1047 sps->conformance_window_flag = get_bits1(gb);
909
2/2
✓ Branch 0 taken 863 times.
✓ Branch 1 taken 184 times.
1047 if (sps->conformance_window_flag) {
910 863 int vert_mult = hevc_sub_height_c[sps->chroma_format_idc];
911 863 int horiz_mult = hevc_sub_width_c[sps->chroma_format_idc];
912 863 sps->pic_conf_win.left_offset = get_ue_golomb_long(gb) * horiz_mult;
913 863 sps->pic_conf_win.right_offset = get_ue_golomb_long(gb) * horiz_mult;
914 863 sps->pic_conf_win.top_offset = get_ue_golomb_long(gb) * vert_mult;
915 863 sps->pic_conf_win.bottom_offset = get_ue_golomb_long(gb) * vert_mult;
916
917
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 863 times.
863 if (avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {
918 av_log(avctx, AV_LOG_DEBUG,
919 "discarding sps conformance window, "
920 "original values are l:%u r:%u t:%u b:%u\n",
921 sps->pic_conf_win.left_offset,
922 sps->pic_conf_win.right_offset,
923 sps->pic_conf_win.top_offset,
924 sps->pic_conf_win.bottom_offset);
925
926 sps->pic_conf_win.left_offset =
927 sps->pic_conf_win.right_offset =
928 sps->pic_conf_win.top_offset =
929 sps->pic_conf_win.bottom_offset = 0;
930 }
931 863 sps->output_window = sps->pic_conf_win;
932 }
933
934 1047 sps->bit_depth = get_ue_golomb_31(gb) + 8;
935
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (sps->bit_depth > 16) {
936 av_log(avctx, AV_LOG_ERROR, "Luma bit depth (%d) is out of range\n",
937 sps->bit_depth);
938 return AVERROR_INVALIDDATA;
939 }
940 1047 bit_depth_chroma = get_ue_golomb_31(gb) + 8;
941
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (bit_depth_chroma > 16) {
942 av_log(avctx, AV_LOG_ERROR, "Chroma bit depth (%d) is out of range\n",
943 bit_depth_chroma);
944 return AVERROR_INVALIDDATA;
945 }
946
3/4
✓ Branch 0 taken 1043 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1043 times.
1047 if (sps->chroma_format_idc && bit_depth_chroma != sps->bit_depth) {
947 av_log(avctx, AV_LOG_ERROR,
948 "Luma bit depth (%d) is different from chroma bit depth (%d), "
949 "this is unsupported.\n",
950 sps->bit_depth, bit_depth_chroma);
951 return AVERROR_INVALIDDATA;
952 }
953 1047 sps->bit_depth_chroma = bit_depth_chroma;
954
955 1047 ret = map_pixel_format(avctx, sps);
956
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (ret < 0)
957 return ret;
958
959 1047 sps->log2_max_poc_lsb = get_ue_golomb_long(gb) + 4;
960
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (sps->log2_max_poc_lsb > 16) {
961 av_log(avctx, AV_LOG_ERROR, "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n",
962 sps->log2_max_poc_lsb - 4);
963 return AVERROR_INVALIDDATA;
964 }
965
966 1047 sps->sublayer_ordering_info_flag = get_bits1(gb);
967
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 897 times.
1047 start = sps->sublayer_ordering_info_flag ? 0 : sps->max_sub_layers - 1;
968
2/2
✓ Branch 0 taken 1194 times.
✓ Branch 1 taken 1047 times.
2241 for (i = start; i < sps->max_sub_layers; i++) {
969 1194 sps->temporal_layer[i].max_dec_pic_buffering = get_ue_golomb_long(gb) + 1;
970 1194 sps->temporal_layer[i].num_reorder_pics = get_ue_golomb_long(gb);
971 1194 sps->temporal_layer[i].max_latency_increase = get_ue_golomb_long(gb) - 1;
972
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1194 times.
1194 if (sps->temporal_layer[i].max_dec_pic_buffering > (unsigned)HEVC_MAX_DPB_SIZE) {
973 av_log(avctx, AV_LOG_ERROR, "sps_max_dec_pic_buffering_minus1 out of range: %d\n",
974 sps->temporal_layer[i].max_dec_pic_buffering - 1U);
975 return AVERROR_INVALIDDATA;
976 }
977
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1194 times.
1194 if (sps->temporal_layer[i].num_reorder_pics > sps->temporal_layer[i].max_dec_pic_buffering - 1) {
978 av_log(avctx, AV_LOG_WARNING, "sps_max_num_reorder_pics out of range: %d\n",
979 sps->temporal_layer[i].num_reorder_pics);
980 if (avctx->err_recognition & AV_EF_EXPLODE ||
981 sps->temporal_layer[i].num_reorder_pics > HEVC_MAX_DPB_SIZE - 1) {
982 return AVERROR_INVALIDDATA;
983 }
984 sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[i].num_reorder_pics + 1;
985 }
986 }
987
988
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 897 times.
1047 if (!sps->sublayer_ordering_info_flag) {
989
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 150 times.
232 for (i = 0; i < start; i++) {
990 82 sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[start].max_dec_pic_buffering;
991 82 sps->temporal_layer[i].num_reorder_pics = sps->temporal_layer[start].num_reorder_pics;
992 82 sps->temporal_layer[i].max_latency_increase = sps->temporal_layer[start].max_latency_increase;
993 }
994 }
995
996 1047 sps->log2_min_cb_size = get_ue_golomb_long(gb) + 3;
997 1047 sps->log2_diff_max_min_coding_block_size = get_ue_golomb_long(gb);
998 1047 sps->log2_min_tb_size = get_ue_golomb_long(gb) + 2;
999 1047 sps->log2_diff_max_min_transform_block_size = get_ue_golomb_long(gb);
1000 1047 sps->log2_max_trafo_size = sps->log2_diff_max_min_transform_block_size +
1001 1047 sps->log2_min_tb_size;
1002
1003
2/4
✓ Branch 0 taken 1047 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1047 times.
1047 if (sps->log2_min_cb_size < 3 || sps->log2_min_cb_size > 30) {
1004 av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_min_cb_size", sps->log2_min_cb_size);
1005 return AVERROR_INVALIDDATA;
1006 }
1007
1008
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (sps->log2_diff_max_min_coding_block_size > 30) {
1009 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);
1010 return AVERROR_INVALIDDATA;
1011 }
1012
1013
2/4
✓ Branch 0 taken 1047 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1047 times.
1047 if (sps->log2_min_tb_size >= sps->log2_min_cb_size || sps->log2_min_tb_size < 2) {
1014 av_log(avctx, AV_LOG_ERROR, "Invalid value for log2_min_tb_size");
1015 return AVERROR_INVALIDDATA;
1016 }
1017
1018
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (sps->log2_diff_max_min_transform_block_size > 30) {
1019 av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_diff_max_min_transform_block_size",
1020 sps->log2_diff_max_min_transform_block_size);
1021 return AVERROR_INVALIDDATA;
1022 }
1023
1024 1047 sps->max_transform_hierarchy_depth_inter = get_ue_golomb_long(gb);
1025 1047 sps->max_transform_hierarchy_depth_intra = get_ue_golomb_long(gb);
1026
1027 1047 sps->scaling_list_enable_flag = get_bits1(gb);
1028
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 1002 times.
1047 if (sps->scaling_list_enable_flag) {
1029 45 set_default_scaling_list_data(&sps->scaling_list);
1030
1031
2/2
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 19 times.
45 if (get_bits1(gb)) {
1032 26 ret = scaling_list_data(gb, avctx, &sps->scaling_list, sps);
1033
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (ret < 0)
1034 return ret;
1035 }
1036 }
1037
1038 1047 sps->amp_enabled_flag = get_bits1(gb);
1039 1047 sps->sao_enabled = get_bits1(gb);
1040
1041 1047 sps->pcm_enabled_flag = get_bits1(gb);
1042
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 972 times.
1047 if (sps->pcm_enabled_flag) {
1043 75 sps->pcm.bit_depth = get_bits(gb, 4) + 1;
1044 75 sps->pcm.bit_depth_chroma = get_bits(gb, 4) + 1;
1045 75 sps->pcm.log2_min_pcm_cb_size = get_ue_golomb_long(gb) + 3;
1046 150 sps->pcm.log2_max_pcm_cb_size = sps->pcm.log2_min_pcm_cb_size +
1047 75 get_ue_golomb_long(gb);
1048
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) {
1049 av_log(avctx, AV_LOG_ERROR,
1050 "PCM bit depth (%d, %d) is greater than normal bit depth (%d)\n",
1051 sps->pcm.bit_depth, sps->pcm.bit_depth_chroma, sps->bit_depth);
1052 return AVERROR_INVALIDDATA;
1053 }
1054
1055 75 sps->pcm.loop_filter_disable_flag = get_bits1(gb);
1056 }
1057
1058 1047 sps->nb_st_rps = get_ue_golomb_long(gb);
1059
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (sps->nb_st_rps > HEVC_MAX_SHORT_TERM_REF_PIC_SETS) {
1060 av_log(avctx, AV_LOG_ERROR, "Too many short term RPS: %d.\n",
1061 sps->nb_st_rps);
1062 return AVERROR_INVALIDDATA;
1063 }
1064
2/2
✓ Branch 0 taken 9251 times.
✓ Branch 1 taken 1047 times.
10298 for (i = 0; i < sps->nb_st_rps; i++) {
1065
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9251 times.
9251 if ((ret = ff_hevc_decode_short_term_rps(gb, avctx, &sps->st_rps[i],
1066 sps, 0)) < 0)
1067 return ret;
1068 }
1069
1070 1047 sps->long_term_ref_pics_present_flag = get_bits1(gb);
1071
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 946 times.
1047 if (sps->long_term_ref_pics_present_flag) {
1072 101 sps->num_long_term_ref_pics_sps = get_ue_golomb_long(gb);
1073
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (sps->num_long_term_ref_pics_sps > HEVC_MAX_LONG_TERM_REF_PICS) {
1074 av_log(avctx, AV_LOG_ERROR, "Too many long term ref pics: %d.\n",
1075 sps->num_long_term_ref_pics_sps);
1076 return AVERROR_INVALIDDATA;
1077 }
1078
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 101 times.
165 for (i = 0; i < sps->num_long_term_ref_pics_sps; i++) {
1079 64 sps->lt_ref_pic_poc_lsb_sps[i] = get_bits(gb, sps->log2_max_poc_lsb);
1080 64 sps->used_by_curr_pic_lt_sps_flag[i] = get_bits1(gb);
1081 }
1082 }
1083
1084 1047 sps->sps_temporal_mvp_enabled_flag = get_bits1(gb);
1085 1047 sps->sps_strong_intra_smoothing_enable_flag = get_bits1(gb);
1086 1047 sps->vui.common.sar = (AVRational){0, 1};
1087 1047 sps->vui_present = get_bits1(gb);
1088
2/2
✓ Branch 0 taken 269 times.
✓ Branch 1 taken 778 times.
1047 if (sps->vui_present)
1089 269 decode_vui(gb, avctx, apply_defdispwin, sps);
1090
1091 1047 sps->sps_extension_present_flag = get_bits1(gb);
1092
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 971 times.
1047 if (sps->sps_extension_present_flag) {
1093 76 sps->sps_range_extension_flag = get_bits1(gb);
1094 76 sps->sps_multilayer_extension_flag = get_bits1(gb);
1095 76 sps->sps_3d_extension_flag = get_bits1(gb);
1096 76 sps->sps_scc_extension_flag = get_bits1(gb);
1097 76 skip_bits(gb, 4); // sps_extension_4bits
1098
1099
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 20 times.
76 if (sps->sps_range_extension_flag) {
1100 56 sps->transform_skip_rotation_enabled_flag = get_bits1(gb);
1101 56 sps->transform_skip_context_enabled_flag = get_bits1(gb);
1102 56 sps->implicit_rdpcm_enabled_flag = get_bits1(gb);
1103
1104 56 sps->explicit_rdpcm_enabled_flag = get_bits1(gb);
1105
1106 56 sps->extended_precision_processing_flag = get_bits1(gb);
1107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (sps->extended_precision_processing_flag)
1108 av_log(avctx, AV_LOG_WARNING,
1109 "extended_precision_processing_flag not yet implemented\n");
1110
1111 56 sps->intra_smoothing_disabled_flag = get_bits1(gb);
1112 56 sps->high_precision_offsets_enabled_flag = get_bits1(gb);
1113
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 18 times.
56 if (sps->high_precision_offsets_enabled_flag)
1114 38 av_log(avctx, AV_LOG_WARNING,
1115 "high_precision_offsets_enabled_flag not yet implemented\n");
1116
1117 56 sps->persistent_rice_adaptation_enabled_flag = get_bits1(gb);
1118
1119 56 sps->cabac_bypass_alignment_enabled_flag = get_bits1(gb);
1120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (sps->cabac_bypass_alignment_enabled_flag)
1121 av_log(avctx, AV_LOG_WARNING,
1122 "cabac_bypass_alignment_enabled_flag not yet implemented\n");
1123 }
1124
1125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (sps->sps_multilayer_extension_flag) {
1126 skip_bits1(gb); // inter_view_mv_vert_constraint_flag
1127 av_log(avctx, AV_LOG_WARNING,
1128 "sps_multilayer_extension_flag not yet implemented\n");
1129 }
1130
1131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (sps->sps_3d_extension_flag) {
1132 for (i = 0; i <= 1; i++) {
1133 skip_bits1(gb); // iv_di_mc_enabled_flag
1134 skip_bits1(gb); // iv_mv_scal_enabled_flag
1135 if (i == 0) {
1136 get_ue_golomb_long(gb); // log2_ivmc_sub_pb_size_minus3
1137 skip_bits1(gb); // iv_res_pred_enabled_flag
1138 skip_bits1(gb); // depth_ref_enabled_flag
1139 skip_bits1(gb); // vsp_mc_enabled_flag
1140 skip_bits1(gb); // dbbp_enabled_flag
1141 } else {
1142 skip_bits1(gb); // tex_mc_enabled_flag
1143 get_ue_golomb_long(gb); // log2_ivmc_sub_pb_size_minus3
1144 skip_bits1(gb); // intra_contour_enabled_flag
1145 skip_bits1(gb); // intra_dc_only_wedge_enabled_flag
1146 skip_bits1(gb); // cqt_cu_part_pred_enabled_flag
1147 skip_bits1(gb); // inter_dc_only_enabled_flag
1148 skip_bits1(gb); // skip_intra_enabled_flag
1149 }
1150 }
1151 av_log(avctx, AV_LOG_WARNING,
1152 "sps_3d_extension_flag not yet implemented\n");
1153 }
1154
1155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (sps->sps_scc_extension_flag) {
1156 sps->sps_curr_pic_ref_enabled_flag = get_bits1(gb);
1157 sps->palette_mode_enabled_flag = get_bits1(gb);
1158 if (sps->palette_mode_enabled_flag) {
1159 sps->palette_max_size = get_ue_golomb(gb);
1160 sps->delta_palette_max_predictor_size = get_ue_golomb(gb);
1161 sps->sps_palette_predictor_initializers_present_flag = get_bits1(gb);
1162
1163 if (sps->sps_palette_predictor_initializers_present_flag) {
1164 sps->sps_num_palette_predictor_initializers = get_ue_golomb(gb) + 1;
1165 if (sps->sps_num_palette_predictor_initializers > HEVC_MAX_PALETTE_PREDICTOR_SIZE) {
1166 av_log(avctx, AV_LOG_ERROR,
1167 "sps_num_palette_predictor_initializers out of range: %u\n",
1168 sps->sps_num_palette_predictor_initializers);
1169 return AVERROR_INVALIDDATA;
1170 }
1171 num_comps = !sps->chroma_format_idc ? 1 : 3;
1172 for (int comp = 0; comp < num_comps; comp++) {
1173 int bit_depth = !comp ? sps->bit_depth : sps->bit_depth_chroma;
1174 for (i = 0; i < sps->sps_num_palette_predictor_initializers; i++)
1175 sps->sps_palette_predictor_initializer[comp][i] = get_bits(gb, bit_depth);
1176 }
1177 }
1178 }
1179 sps->motion_vector_resolution_control_idc = get_bits(gb, 2);
1180 sps->intra_boundary_filtering_disabled_flag = get_bits1(gb);
1181 }
1182 }
1183
2/2
✓ Branch 0 taken 430 times.
✓ Branch 1 taken 617 times.
1047 if (apply_defdispwin) {
1184 430 sps->output_window.left_offset += sps->vui.def_disp_win.left_offset;
1185 430 sps->output_window.right_offset += sps->vui.def_disp_win.right_offset;
1186 430 sps->output_window.top_offset += sps->vui.def_disp_win.top_offset;
1187 430 sps->output_window.bottom_offset += sps->vui.def_disp_win.bottom_offset;
1188 }
1189
1190 1047 ow = &sps->output_window;
1191
1/2
✓ Branch 0 taken 1047 times.
✗ Branch 1 not taken.
1047 if (ow->left_offset >= INT_MAX - ow->right_offset ||
1192
1/2
✓ Branch 0 taken 1047 times.
✗ Branch 1 not taken.
1047 ow->top_offset >= INT_MAX - ow->bottom_offset ||
1193
1/2
✓ Branch 0 taken 1047 times.
✗ Branch 1 not taken.
1047 ow->left_offset + ow->right_offset >= sps->width ||
1194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 ow->top_offset + ow->bottom_offset >= sps->height) {
1195 av_log(avctx, AV_LOG_WARNING, "Invalid cropping offsets: %u/%u/%u/%u\n",
1196 ow->left_offset, ow->right_offset, ow->top_offset, ow->bottom_offset);
1197 if (avctx->err_recognition & AV_EF_EXPLODE) {
1198 return AVERROR_INVALIDDATA;
1199 }
1200 av_log(avctx, AV_LOG_WARNING,
1201 "Displaying the whole video surface.\n");
1202 memset(ow, 0, sizeof(*ow));
1203 memset(&sps->pic_conf_win, 0, sizeof(sps->pic_conf_win));
1204 }
1205
1206 // Inferred parameters
1207 1047 sps->log2_ctb_size = sps->log2_min_cb_size +
1208 1047 sps->log2_diff_max_min_coding_block_size;
1209 1047 sps->log2_min_pu_size = sps->log2_min_cb_size - 1;
1210
1211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (sps->log2_ctb_size > HEVC_MAX_LOG2_CTB_SIZE) {
1212 av_log(avctx, AV_LOG_ERROR, "CTB size out of range: 2^%d\n", sps->log2_ctb_size);
1213 return AVERROR_INVALIDDATA;
1214 }
1215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (sps->log2_ctb_size < 4) {
1216 av_log(avctx,
1217 AV_LOG_ERROR,
1218 "log2_ctb_size %d differs from the bounds of any known profile\n",
1219 sps->log2_ctb_size);
1220 avpriv_request_sample(avctx, "log2_ctb_size %d", sps->log2_ctb_size);
1221 return AVERROR_INVALIDDATA;
1222 }
1223
1224 1047 sps->ctb_width = (sps->width + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
1225 1047 sps->ctb_height = (sps->height + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
1226 1047 sps->ctb_size = sps->ctb_width * sps->ctb_height;
1227
1228 1047 sps->min_cb_width = sps->width >> sps->log2_min_cb_size;
1229 1047 sps->min_cb_height = sps->height >> sps->log2_min_cb_size;
1230 1047 sps->min_tb_width = sps->width >> sps->log2_min_tb_size;
1231 1047 sps->min_tb_height = sps->height >> sps->log2_min_tb_size;
1232 1047 sps->min_pu_width = sps->width >> sps->log2_min_pu_size;
1233 1047 sps->min_pu_height = sps->height >> sps->log2_min_pu_size;
1234 1047 sps->tb_mask = (1 << (sps->log2_ctb_size - sps->log2_min_tb_size)) - 1;
1235
1236 1047 sps->qp_bd_offset = 6 * (sps->bit_depth - 8);
1237
1238
1/2
✓ Branch 0 taken 1047 times.
✗ Branch 1 not taken.
1047 if (av_mod_uintp2(sps->width, sps->log2_min_cb_size) ||
1239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 av_mod_uintp2(sps->height, sps->log2_min_cb_size)) {
1240 av_log(avctx, AV_LOG_ERROR, "Invalid coded frame dimensions.\n");
1241 return AVERROR_INVALIDDATA;
1242 }
1243
1244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (sps->max_transform_hierarchy_depth_inter > sps->log2_ctb_size - sps->log2_min_tb_size) {
1245 av_log(avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_inter out of range: %d\n",
1246 sps->max_transform_hierarchy_depth_inter);
1247 return AVERROR_INVALIDDATA;
1248 }
1249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (sps->max_transform_hierarchy_depth_intra > sps->log2_ctb_size - sps->log2_min_tb_size) {
1250 av_log(avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_intra out of range: %d\n",
1251 sps->max_transform_hierarchy_depth_intra);
1252 return AVERROR_INVALIDDATA;
1253 }
1254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (sps->log2_max_trafo_size > FFMIN(sps->log2_ctb_size, 5)) {
1255 av_log(avctx, AV_LOG_ERROR,
1256 "max transform block size out of range: %d\n",
1257 sps->log2_max_trafo_size);
1258 return AVERROR_INVALIDDATA;
1259 }
1260
1261
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1047 times.
1047 if (get_bits_left(gb) < 0) {
1262 av_log(avctx, AV_LOG_ERROR,
1263 "Overread SPS by %d bits\n", -get_bits_left(gb));
1264 return AVERROR_INVALIDDATA;
1265 }
1266
1267 1047 return 0;
1268 }
1269
1270 1047 int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx,
1271 HEVCParamSets *ps, int apply_defdispwin)
1272 {
1273 1047 HEVCSPS *sps = ff_refstruct_allocz(sizeof(*sps));
1274 unsigned int sps_id;
1275 int ret;
1276 ptrdiff_t nal_size;
1277
1278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (!sps)
1279 return AVERROR(ENOMEM);
1280
1281 1047 av_log(avctx, AV_LOG_DEBUG, "Decoding SPS\n");
1282
1283 1047 nal_size = gb->buffer_end - gb->buffer;
1284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (nal_size > sizeof(sps->data)) {
1285 av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized SPS "
1286 "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
1287 nal_size, sizeof(sps->data));
1288 sps->data_size = sizeof(sps->data);
1289 } else {
1290 1047 sps->data_size = nal_size;
1291 }
1292 1047 memcpy(sps->data, gb->buffer, sps->data_size);
1293
1294 1047 ret = ff_hevc_parse_sps(sps, gb, &sps_id,
1295 apply_defdispwin,
1296 1047 ps->vps_list, avctx);
1297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (ret < 0) {
1298 ff_refstruct_unref(&sps);
1299 return ret;
1300 }
1301
1302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1047 times.
1047 if (avctx->debug & FF_DEBUG_BITSTREAM) {
1303 av_log(avctx, AV_LOG_DEBUG,
1304 "Parsed SPS: id %d; coded wxh: %dx%d; "
1305 "cropped wxh: %dx%d; pix_fmt: %s.\n",
1306 sps_id, sps->width, sps->height,
1307 sps->width - (sps->output_window.left_offset + sps->output_window.right_offset),
1308 sps->height - (sps->output_window.top_offset + sps->output_window.bottom_offset),
1309 av_get_pix_fmt_name(sps->pix_fmt));
1310 }
1311
1312 /* check if this is a repeat of an already parsed SPS, then keep the
1313 * original one.
1314 * otherwise drop all PPSes that depend on it */
1315
2/2
✓ Branch 0 taken 437 times.
✓ Branch 1 taken 610 times.
1047 if (ps->sps_list[sps_id] &&
1316
2/2
✓ Branch 0 taken 429 times.
✓ Branch 1 taken 8 times.
437 !memcmp(ps->sps_list[sps_id], sps, sizeof(*sps))) {
1317 429 ff_refstruct_unref(&sps);
1318 } else {
1319 618 remove_sps(ps, sps_id);
1320 618 ps->sps_list[sps_id] = sps;
1321 }
1322
1323 1047 return 0;
1324 }
1325
1326 3271 static void hevc_pps_free(FFRefStructOpaque unused, void *obj)
1327 {
1328 3271 HEVCPPS *pps = obj;
1329
1330 3271 av_freep(&pps->column_width);
1331 3271 av_freep(&pps->row_height);
1332 3271 av_freep(&pps->col_bd);
1333 3271 av_freep(&pps->row_bd);
1334 3271 av_freep(&pps->col_idxX);
1335 3271 av_freep(&pps->ctb_addr_rs_to_ts);
1336 3271 av_freep(&pps->ctb_addr_ts_to_rs);
1337 3271 av_freep(&pps->tile_pos_rs);
1338 3271 av_freep(&pps->tile_id);
1339 3271 av_freep(&pps->min_tb_addr_zs_tab);
1340 3271 }
1341
1342 5 static void colour_mapping_octants(GetBitContext *gb, HEVCPPS *pps, int inp_depth,
1343 int idx_y, int idx_cb, int idx_cr, int inp_length)
1344 {
1345 unsigned int split_octant_flag, part_num_y, coded_res_flag, res_coeff_q, res_coeff_r;
1346 int cm_res_bits;
1347
1348 5 part_num_y = 1 << pps->cm_y_part_num_log2;
1349
1350
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 split_octant_flag = inp_depth < pps->cm_octant_depth ? get_bits1(gb) : 0;
1351
1352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (split_octant_flag)
1353 for (int k = 0; k < 2; k++)
1354 for (int m = 0; m < 2; m++)
1355 for (int n = 0; n < 2; n++)
1356 colour_mapping_octants(gb, pps, inp_depth + 1,
1357 idx_y + part_num_y * k * inp_length / 2,
1358 idx_cb + m * inp_length / 2,
1359 idx_cr + n * inp_length / 2,
1360 inp_length / 2);
1361 else
1362
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 5 times.
45 for (int i = 0; i < part_num_y; i++) {
1363
2/2
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 40 times.
200 for (int j = 0; j < 4; j++) {
1364 160 coded_res_flag = get_bits1(gb);
1365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 160 times.
160 if (coded_res_flag)
1366 for (int c = 0; c < 3; c++) {
1367 res_coeff_q = get_ue_golomb_long(gb);
1368 cm_res_bits = FFMAX(0, 10 + pps->luma_bit_depth_cm_input -
1369 pps->luma_bit_depth_cm_output -
1370 pps->cm_res_quant_bits - pps->cm_delta_flc_bits);
1371 res_coeff_r = cm_res_bits ? get_bits(gb, cm_res_bits) : 0;
1372 if (res_coeff_q || res_coeff_r)
1373 skip_bits1(gb);
1374 }
1375 }
1376 }
1377 5 }
1378
1379 5 static int colour_mapping_table(GetBitContext *gb, AVCodecContext *avctx, HEVCPPS *pps)
1380 {
1381 5 pps->num_cm_ref_layers = get_ue_golomb(gb) + 1;
1382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (pps->num_cm_ref_layers > 62) {
1383 av_log(avctx, AV_LOG_ERROR,
1384 "num_cm_ref_layers_minus1 shall be in the range [0, 61].\n");
1385 return AVERROR_INVALIDDATA;
1386 }
1387
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5 times.
20 for (int i = 0; i < pps->num_cm_ref_layers; i++)
1388 15 pps->cm_ref_layer_id[i] = get_bits(gb, 6);
1389
1390 5 pps->cm_octant_depth = get_bits(gb, 2);
1391 5 pps->cm_y_part_num_log2 = get_bits(gb, 2);
1392
1393 5 pps->luma_bit_depth_cm_input = get_ue_golomb(gb) + 8;
1394 5 pps->chroma_bit_depth_cm_input = get_ue_golomb(gb) + 8;
1395 5 pps->luma_bit_depth_cm_output = get_ue_golomb(gb) + 8;
1396 5 pps->chroma_bit_depth_cm_output = get_ue_golomb(gb) + 8;
1397
1398 5 pps->cm_res_quant_bits = get_bits(gb, 2);
1399 5 pps->cm_delta_flc_bits = get_bits(gb, 2) + 1;
1400
1401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (pps->cm_octant_depth == 1) {
1402 pps->cm_adapt_threshold_u_delta = get_se_golomb_long(gb);
1403 pps->cm_adapt_threshold_v_delta = get_se_golomb_long(gb);
1404 }
1405
1406 5 colour_mapping_octants(gb, pps, 0, 0, 0, 0, 1 << pps->cm_octant_depth);
1407
1408 5 return 0;
1409 }
1410
1411 5 static int pps_multilayer_extension(GetBitContext *gb, AVCodecContext *avctx,
1412 HEVCPPS *pps, const HEVCSPS *sps, const HEVCVPS *vps)
1413 {
1414 5 pps->poc_reset_info_present_flag = get_bits1(gb);
1415 5 pps->pps_infer_scaling_list_flag = get_bits1(gb);
1416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (pps->pps_infer_scaling_list_flag)
1417 pps->pps_scaling_list_ref_layer_id = get_bits(gb, 6);
1418
1419 5 pps->num_ref_loc_offsets = get_ue_golomb(gb);
1420
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (pps->num_ref_loc_offsets > vps->vps_max_layers - 1)
1421 return AVERROR_INVALIDDATA;
1422
1423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 for (int i = 0; i < pps->num_ref_loc_offsets; i++) {
1424 pps->ref_loc_offset_layer_id[i] = get_bits(gb, 6);
1425 pps->scaled_ref_layer_offset_present_flag[i] = get_bits1(gb);
1426 if (pps->scaled_ref_layer_offset_present_flag[i]) {
1427 pps->scaled_ref_layer_left_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1428 pps->scaled_ref_layer_top_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1429 pps->scaled_ref_layer_right_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1430 pps->scaled_ref_layer_bottom_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1431 }
1432
1433 pps->ref_region_offset_present_flag[i] = get_bits1(gb);
1434 if (pps->ref_region_offset_present_flag[i]) {
1435 pps->ref_region_left_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1436 pps->ref_region_top_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1437 pps->ref_region_right_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1438 pps->ref_region_bottom_offset[pps->ref_loc_offset_layer_id[i]] = get_se_golomb_long(gb);
1439 }
1440
1441 pps->resample_phase_set_present_flag[i] = get_bits1(gb);
1442 if (pps->resample_phase_set_present_flag[i]) {
1443 pps->phase_hor_luma[pps->ref_loc_offset_layer_id[i]] = get_ue_golomb_31(gb);
1444 pps->phase_ver_luma[pps->ref_loc_offset_layer_id[i]] = get_ue_golomb_31(gb);
1445 pps->phase_hor_chroma[pps->ref_loc_offset_layer_id[i]] = get_ue_golomb(gb) - 8;
1446 pps->phase_ver_chroma[pps->ref_loc_offset_layer_id[i]] = get_ue_golomb(gb) - 8;
1447 }
1448 }
1449
1450 5 pps->colour_mapping_enabled_flag = get_bits1(gb);
1451
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (pps->colour_mapping_enabled_flag) {
1452 5 int ret = colour_mapping_table(gb, avctx, pps);
1453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (ret < 0)
1454 return ret;
1455 }
1456
1457 5 return 0;
1458 }
1459
1460 static void delta_dlt(GetBitContext *gb, HEVCPPS *pps)
1461 {
1462 unsigned int num_val_delta_dlt, max_diff = 0;
1463 int min_diff_minus1 = -1;
1464 unsigned int len;
1465
1466 num_val_delta_dlt = get_bits(gb, pps->pps_bit_depth_for_depth_layers_minus8 + 8);
1467 if (num_val_delta_dlt) {
1468 if (num_val_delta_dlt > 1)
1469 max_diff = get_bits(gb, pps->pps_bit_depth_for_depth_layers_minus8 + 8);
1470 if (num_val_delta_dlt > 2 && max_diff) {
1471 len = av_log2(max_diff) + 1;
1472 min_diff_minus1 = get_bits(gb, len);
1473 }
1474 if (max_diff > (min_diff_minus1 + 1))
1475 for (int k = 1; k < num_val_delta_dlt; k++) {
1476 len = av_log2(max_diff - (min_diff_minus1 + 1)) + 1;
1477 skip_bits(gb, len); // delta_val_diff_minus_min
1478 }
1479 }
1480 }
1481
1482 5 static int pps_3d_extension(GetBitContext *gb, AVCodecContext *avctx,
1483 HEVCPPS *pps, const HEVCSPS *sps)
1484 {
1485 unsigned int pps_depth_layers_minus1;
1486
1487
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 if (get_bits1(gb)) { // dlts_present_flag
1488 pps_depth_layers_minus1 = get_bits(gb, 6);
1489 pps->pps_bit_depth_for_depth_layers_minus8 = get_bits(gb, 4);
1490 for (int i = 0; i <= pps_depth_layers_minus1; i++) {
1491 if (get_bits1(gb)) { // dlt_flag[i]
1492 if (!get_bits1(gb)) { // dlt_pred_flag[i]
1493 if (get_bits1(gb)) { // dlt_val_flags_present_flag[i]
1494 for (int j = 0; j <= ((1 << (pps->pps_bit_depth_for_depth_layers_minus8 + 8)) - 1); j++)
1495 skip_bits1(gb); // dlt_value_flag[i][j]
1496 } else
1497 delta_dlt(gb, pps);
1498 }
1499 }
1500 }
1501 }
1502
1503 5 return 0;
1504 }
1505
1506 76 static int pps_range_extensions(GetBitContext *gb, AVCodecContext *avctx,
1507 HEVCPPS *pps, const HEVCSPS *sps)
1508 {
1509
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 if (pps->transform_skip_enabled_flag) {
1510 76 pps->log2_max_transform_skip_block_size = get_ue_golomb_31(gb) + 2;
1511 }
1512 76 pps->cross_component_prediction_enabled_flag = get_bits1(gb);
1513 76 pps->chroma_qp_offset_list_enabled_flag = get_bits1(gb);
1514
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 66 times.
76 if (pps->chroma_qp_offset_list_enabled_flag) {
1515 10 pps->diff_cu_chroma_qp_offset_depth = get_ue_golomb_31(gb);
1516 10 pps->chroma_qp_offset_list_len_minus1 = get_ue_golomb_31(gb);
1517
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (pps->chroma_qp_offset_list_len_minus1 > 5) {
1518 av_log(avctx, AV_LOG_ERROR,
1519 "chroma_qp_offset_list_len_minus1 shall be in the range [0, 5].\n");
1520 return AVERROR_INVALIDDATA;
1521 }
1522
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 for (int i = 0; i <= pps->chroma_qp_offset_list_len_minus1; i++) {
1523 10 pps->cb_qp_offset_list[i] = get_se_golomb(gb);
1524
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (pps->cb_qp_offset_list[i]) {
1525 10 av_log(avctx, AV_LOG_WARNING,
1526 "cb_qp_offset_list not tested yet.\n");
1527 }
1528 10 pps->cr_qp_offset_list[i] = get_se_golomb(gb);
1529
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (pps->cr_qp_offset_list[i]) {
1530 10 av_log(avctx, AV_LOG_WARNING,
1531 "cb_qp_offset_list not tested yet.\n");
1532 }
1533 }
1534 }
1535 76 pps->log2_sao_offset_scale_luma = get_ue_golomb_31(gb);
1536 76 pps->log2_sao_offset_scale_chroma = get_ue_golomb_31(gb);
1537
1538
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 if ( pps->log2_sao_offset_scale_luma > FFMAX(sps->bit_depth - 10, 0)
1539
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 || pps->log2_sao_offset_scale_chroma > FFMAX(sps->bit_depth_chroma - 10, 0)
1540 )
1541 return AVERROR_INVALIDDATA;
1542
1543 76 return(0);
1544 }
1545
1546 5 static int pps_scc_extension(GetBitContext *gb, AVCodecContext *avctx,
1547 HEVCPPS *pps, const HEVCSPS *sps)
1548 {
1549 int num_comps, ret;
1550
1551 5 pps->pps_curr_pic_ref_enabled_flag = get_bits1(gb);
1552
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 if (pps->residual_adaptive_colour_transform_enabled_flag = get_bits1(gb)) {
1553 pps->pps_slice_act_qp_offsets_present_flag = get_bits1(gb);
1554 pps->pps_act_y_qp_offset = get_se_golomb(gb) - 5;
1555 pps->pps_act_cb_qp_offset = get_se_golomb(gb) - 5;
1556 pps->pps_act_cr_qp_offset = get_se_golomb(gb) - 3;
1557
1558 #define CHECK_QP_OFFSET(name) (pps->pps_act_ ## name ## _qp_offset <= -12 || \
1559 pps->pps_act_ ## name ## _qp_offset >= 12)
1560 ret = CHECK_QP_OFFSET(y) || CHECK_QP_OFFSET(cb) || CHECK_QP_OFFSET(cr);
1561 #undef CHECK_QP_OFFSET
1562 if (ret) {
1563 av_log(avctx, AV_LOG_ERROR,
1564 "PpsActQpOffsetY/Cb/Cr shall be in the range of [-12, 12].\n");
1565 return AVERROR_INVALIDDATA;
1566 }
1567 }
1568
1569
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 if (pps->pps_palette_predictor_initializers_present_flag = get_bits1(gb)) {
1570 pps->pps_num_palette_predictor_initializers = get_ue_golomb(gb);
1571 if (pps->pps_num_palette_predictor_initializers > 0) {
1572 if (pps->pps_num_palette_predictor_initializers > HEVC_MAX_PALETTE_PREDICTOR_SIZE) {
1573 av_log(avctx, AV_LOG_ERROR,
1574 "pps_num_palette_predictor_initializers out of range: %u\n",
1575 pps->pps_num_palette_predictor_initializers);
1576 return AVERROR_INVALIDDATA;
1577 }
1578 pps->monochrome_palette_flag = get_bits1(gb);
1579 pps->luma_bit_depth_entry = get_ue_golomb_31(gb) + 8;
1580 if (pps->luma_bit_depth_entry != sps->bit_depth)
1581 return AVERROR_INVALIDDATA;
1582 if (!pps->monochrome_palette_flag) {
1583 pps->chroma_bit_depth_entry = get_ue_golomb_31(gb) + 8;
1584 if (pps->chroma_bit_depth_entry != sps->bit_depth_chroma)
1585 return AVERROR_INVALIDDATA;
1586 }
1587
1588 num_comps = pps->monochrome_palette_flag ? 1 : 3;
1589 for (int comp = 0; comp < num_comps; comp++) {
1590 int bit_depth = !comp ? pps->luma_bit_depth_entry : pps->chroma_bit_depth_entry;
1591 for (int i = 0; i < pps->pps_num_palette_predictor_initializers; i++)
1592 pps->pps_palette_predictor_initializer[comp][i] = get_bits(gb, bit_depth);
1593 }
1594 }
1595 }
1596
1597 5 return 0;
1598 }
1599
1600 3267 static inline int setup_pps(AVCodecContext *avctx, GetBitContext *gb,
1601 HEVCPPS *pps, const HEVCSPS *sps)
1602 {
1603 int log2_diff;
1604 int pic_area_in_ctbs;
1605 int i, j, x, y, ctb_addr_rs, tile_id;
1606
1607 // Inferred parameters
1608 3267 pps->col_bd = av_malloc_array(pps->num_tile_columns + 1, sizeof(*pps->col_bd));
1609 3267 pps->row_bd = av_malloc_array(pps->num_tile_rows + 1, sizeof(*pps->row_bd));
1610 3267 pps->col_idxX = av_malloc_array(sps->ctb_width, sizeof(*pps->col_idxX));
1611
3/6
✓ Branch 0 taken 3267 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3267 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3267 times.
3267 if (!pps->col_bd || !pps->row_bd || !pps->col_idxX)
1612 return AVERROR(ENOMEM);
1613
1614
2/2
✓ Branch 0 taken 2735 times.
✓ Branch 1 taken 532 times.
3267 if (pps->uniform_spacing_flag) {
1615
2/2
✓ Branch 0 taken 2359 times.
✓ Branch 1 taken 376 times.
2735 if (!pps->column_width) {
1616 2359 pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1617 2359 pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1618 }
1619
2/4
✓ Branch 0 taken 2735 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2735 times.
2735 if (!pps->column_width || !pps->row_height)
1620 return AVERROR(ENOMEM);
1621
1622
2/2
✓ Branch 0 taken 3002 times.
✓ Branch 1 taken 2735 times.
5737 for (i = 0; i < pps->num_tile_columns; i++) {
1623 3002 pps->column_width[i] = ((i + 1) * sps->ctb_width) / pps->num_tile_columns -
1624 3002 (i * sps->ctb_width) / pps->num_tile_columns;
1625 }
1626
1627
2/2
✓ Branch 0 taken 3033 times.
✓ Branch 1 taken 2735 times.
5768 for (i = 0; i < pps->num_tile_rows; i++) {
1628 3033 pps->row_height[i] = ((i + 1) * sps->ctb_height) / pps->num_tile_rows -
1629 3033 (i * sps->ctb_height) / pps->num_tile_rows;
1630 }
1631 }
1632
1633 3267 pps->col_bd[0] = 0;
1634
2/2
✓ Branch 0 taken 5610 times.
✓ Branch 1 taken 3267 times.
8877 for (i = 0; i < pps->num_tile_columns; i++)
1635 5610 pps->col_bd[i + 1] = pps->col_bd[i] + pps->column_width[i];
1636
1637 3267 pps->row_bd[0] = 0;
1638
2/2
✓ Branch 0 taken 5627 times.
✓ Branch 1 taken 3267 times.
8894 for (i = 0; i < pps->num_tile_rows; i++)
1639 5627 pps->row_bd[i + 1] = pps->row_bd[i] + pps->row_height[i];
1640
1641
2/2
✓ Branch 0 taken 57429 times.
✓ Branch 1 taken 3267 times.
60696 for (i = 0, j = 0; i < sps->ctb_width; i++) {
1642
2/2
✓ Branch 0 taken 5596 times.
✓ Branch 1 taken 51833 times.
57429 if (i > pps->col_bd[j])
1643 5596 j++;
1644 57429 pps->col_idxX[i] = j;
1645 }
1646
1647 /**
1648 * 6.5
1649 */
1650 3267 pic_area_in_ctbs = sps->ctb_width * sps->ctb_height;
1651
1652 3267 pps->ctb_addr_rs_to_ts = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_rs_to_ts));
1653 3267 pps->ctb_addr_ts_to_rs = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_ts_to_rs));
1654 3267 pps->tile_id = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->tile_id));
1655 3267 pps->min_tb_addr_zs_tab = av_malloc_array((sps->tb_mask+2) * (sps->tb_mask+2), sizeof(*pps->min_tb_addr_zs_tab));
1656
2/4
✓ Branch 0 taken 3267 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3267 times.
✗ Branch 3 not taken.
3267 if (!pps->ctb_addr_rs_to_ts || !pps->ctb_addr_ts_to_rs ||
1657
2/4
✓ Branch 0 taken 3267 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3267 times.
3267 !pps->tile_id || !pps->min_tb_addr_zs_tab) {
1658 return AVERROR(ENOMEM);
1659 }
1660
1661
2/2
✓ Branch 0 taken 840067 times.
✓ Branch 1 taken 3267 times.
843334 for (ctb_addr_rs = 0; ctb_addr_rs < pic_area_in_ctbs; ctb_addr_rs++) {
1662 840067 int tb_x = ctb_addr_rs % sps->ctb_width;
1663 840067 int tb_y = ctb_addr_rs / sps->ctb_width;
1664 840067 int tile_x = 0;
1665 840067 int tile_y = 0;
1666 840067 int val = 0;
1667
1668
1/2
✓ Branch 0 taken 1410801 times.
✗ Branch 1 not taken.
1410801 for (i = 0; i < pps->num_tile_columns; i++) {
1669
2/2
✓ Branch 0 taken 840067 times.
✓ Branch 1 taken 570734 times.
1410801 if (tb_x < pps->col_bd[i + 1]) {
1670 840067 tile_x = i;
1671 840067 break;
1672 }
1673 }
1674
1675
1/2
✓ Branch 0 taken 1259533 times.
✗ Branch 1 not taken.
1259533 for (i = 0; i < pps->num_tile_rows; i++) {
1676
2/2
✓ Branch 0 taken 840067 times.
✓ Branch 1 taken 419466 times.
1259533 if (tb_y < pps->row_bd[i + 1]) {
1677 840067 tile_y = i;
1678 840067 break;
1679 }
1680 }
1681
1682
2/2
✓ Branch 0 taken 570734 times.
✓ Branch 1 taken 840067 times.
1410801 for (i = 0; i < tile_x; i++)
1683 570734 val += pps->row_height[tile_y] * pps->column_width[i];
1684
2/2
✓ Branch 0 taken 419466 times.
✓ Branch 1 taken 840067 times.
1259533 for (i = 0; i < tile_y; i++)
1685 419466 val += sps->ctb_width * pps->row_height[i];
1686
1687 840067 val += (tb_y - pps->row_bd[tile_y]) * pps->column_width[tile_x] +
1688 840067 tb_x - pps->col_bd[tile_x];
1689
1690 840067 pps->ctb_addr_rs_to_ts[ctb_addr_rs] = val;
1691 840067 pps->ctb_addr_ts_to_rs[val] = ctb_addr_rs;
1692 }
1693
1694
2/2
✓ Branch 0 taken 5627 times.
✓ Branch 1 taken 3267 times.
8894 for (j = 0, tile_id = 0; j < pps->num_tile_rows; j++)
1695
2/2
✓ Branch 0 taken 16271 times.
✓ Branch 1 taken 5627 times.
21898 for (i = 0; i < pps->num_tile_columns; i++, tile_id++)
1696
2/2
✓ Branch 0 taken 78985 times.
✓ Branch 1 taken 16271 times.
95256 for (y = pps->row_bd[j]; y < pps->row_bd[j + 1]; y++)
1697
2/2
✓ Branch 0 taken 840067 times.
✓ Branch 1 taken 78985 times.
919052 for (x = pps->col_bd[i]; x < pps->col_bd[i + 1]; x++)
1698 840067 pps->tile_id[pps->ctb_addr_rs_to_ts[y * sps->ctb_width + x]] = tile_id;
1699
1700 3267 pps->tile_pos_rs = av_malloc_array(tile_id, sizeof(*pps->tile_pos_rs));
1701
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3267 times.
3267 if (!pps->tile_pos_rs)
1702 return AVERROR(ENOMEM);
1703
1704
2/2
✓ Branch 0 taken 5627 times.
✓ Branch 1 taken 3267 times.
8894 for (j = 0; j < pps->num_tile_rows; j++)
1705
2/2
✓ Branch 0 taken 16271 times.
✓ Branch 1 taken 5627 times.
21898 for (i = 0; i < pps->num_tile_columns; i++)
1706 16271 pps->tile_pos_rs[j * pps->num_tile_columns + i] =
1707 16271 pps->row_bd[j] * sps->ctb_width + pps->col_bd[i];
1708
1709 3267 log2_diff = sps->log2_ctb_size - sps->log2_min_tb_size;
1710 3267 pps->min_tb_addr_zs = &pps->min_tb_addr_zs_tab[1*(sps->tb_mask+2)+1];
1711
2/2
✓ Branch 0 taken 54179 times.
✓ Branch 1 taken 3267 times.
57446 for (y = 0; y < sps->tb_mask+2; y++) {
1712 54179 pps->min_tb_addr_zs_tab[y*(sps->tb_mask+2)] = -1;
1713 54179 pps->min_tb_addr_zs_tab[y] = -1;
1714 }
1715
2/2
✓ Branch 0 taken 50912 times.
✓ Branch 1 taken 3267 times.
54179 for (y = 0; y < sps->tb_mask+1; y++) {
1716
2/2
✓ Branch 0 taken 806016 times.
✓ Branch 1 taken 50912 times.
856928 for (x = 0; x < sps->tb_mask+1; x++) {
1717 806016 int tb_x = x >> log2_diff;
1718 806016 int tb_y = y >> log2_diff;
1719 806016 int rs = sps->ctb_width * tb_y + tb_x;
1720 806016 int val = pps->ctb_addr_rs_to_ts[rs] << (log2_diff * 2);
1721
2/2
✓ Branch 0 taken 3216256 times.
✓ Branch 1 taken 806016 times.
4022272 for (i = 0; i < log2_diff; i++) {
1722 3216256 int m = 1 << i;
1723
4/4
✓ Branch 0 taken 1608128 times.
✓ Branch 1 taken 1608128 times.
✓ Branch 2 taken 1608128 times.
✓ Branch 3 taken 1608128 times.
3216256 val += (m & x ? m * m : 0) + (m & y ? 2 * m * m : 0);
1724 }
1725 806016 pps->min_tb_addr_zs[y * (sps->tb_mask+2) + x] = val;
1726 }
1727 }
1728
1729 3267 return 0;
1730 }
1731
1732 3271 int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx,
1733 HEVCParamSets *ps)
1734 {
1735 3271 const HEVCSPS *sps = NULL;
1736 3271 const HEVCVPS *vps = NULL;
1737 3271 int i, ret = 0;
1738 3271 unsigned int pps_id = 0;
1739 ptrdiff_t nal_size;
1740 unsigned log2_parallel_merge_level_minus2;
1741
1742 3271 HEVCPPS *pps = ff_refstruct_alloc_ext(sizeof(*pps), 0, NULL, hevc_pps_free);
1743
1744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3271 times.
3271 if (!pps)
1745 return AVERROR(ENOMEM);
1746
1747 3271 av_log(avctx, AV_LOG_DEBUG, "Decoding PPS\n");
1748
1749 3271 nal_size = gb->buffer_end - gb->buffer;
1750
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3271 times.
3271 if (nal_size > sizeof(pps->data)) {
1751 av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized PPS "
1752 "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
1753 nal_size, sizeof(pps->data));
1754 pps->data_size = sizeof(pps->data);
1755 } else {
1756 3271 pps->data_size = nal_size;
1757 }
1758 3271 memcpy(pps->data, gb->buffer, pps->data_size);
1759
1760 // Default values
1761 3271 pps->loop_filter_across_tiles_enabled_flag = 1;
1762 3271 pps->num_tile_columns = 1;
1763 3271 pps->num_tile_rows = 1;
1764 3271 pps->uniform_spacing_flag = 1;
1765 3271 pps->disable_dbf = 0;
1766 3271 pps->beta_offset = 0;
1767 3271 pps->tc_offset = 0;
1768 3271 pps->log2_max_transform_skip_block_size = 2;
1769
1770 // Coded parameters
1771 3271 pps_id = pps->pps_id = get_ue_golomb_long(gb);
1772
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3271 times.
3271 if (pps_id >= HEVC_MAX_PPS_COUNT) {
1773 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
1774 ret = AVERROR_INVALIDDATA;
1775 goto err;
1776 }
1777 3271 pps->sps_id = get_ue_golomb_long(gb);
1778
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3271 times.
3271 if (pps->sps_id >= HEVC_MAX_SPS_COUNT) {
1779 av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", pps->sps_id);
1780 ret = AVERROR_INVALIDDATA;
1781 goto err;
1782 }
1783
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3267 times.
3271 if (!ps->sps_list[pps->sps_id]) {
1784 4 av_log(avctx, AV_LOG_ERROR, "SPS %u does not exist.\n", pps->sps_id);
1785 4 ret = AVERROR_INVALIDDATA;
1786 4 goto err;
1787 }
1788 3267 sps = ps->sps_list[pps->sps_id];
1789 3267 vps = ps->vps_list[sps->vps_id];
1790
1791 3267 pps->dependent_slice_segments_enabled_flag = get_bits1(gb);
1792 3267 pps->output_flag_present_flag = get_bits1(gb);
1793 3267 pps->num_extra_slice_header_bits = get_bits(gb, 3);
1794
1795 3267 pps->sign_data_hiding_flag = get_bits1(gb);
1796
1797 3267 pps->cabac_init_present_flag = get_bits1(gb);
1798
1799 3267 pps->num_ref_idx_l0_default_active = get_ue_golomb_31(gb) + 1;
1800 3267 pps->num_ref_idx_l1_default_active = get_ue_golomb_31(gb) + 1;
1801
1/2
✓ Branch 0 taken 3267 times.
✗ Branch 1 not taken.
3267 if (pps->num_ref_idx_l0_default_active >= HEVC_MAX_REFS ||
1802
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3267 times.
3267 pps->num_ref_idx_l1_default_active >= HEVC_MAX_REFS) {
1803 av_log(avctx, AV_LOG_ERROR, "Too many default refs in PPS: %d/%d.\n",
1804 pps->num_ref_idx_l0_default_active, pps->num_ref_idx_l1_default_active);
1805 goto err;
1806 }
1807
1808 3267 pps->pic_init_qp_minus26 = get_se_golomb(gb);
1809
1810 3267 pps->constrained_intra_pred_flag = get_bits1(gb);
1811 3267 pps->transform_skip_enabled_flag = get_bits1(gb);
1812
1813 3267 pps->cu_qp_delta_enabled_flag = get_bits1(gb);
1814 3267 pps->diff_cu_qp_delta_depth = 0;
1815
2/2
✓ Branch 0 taken 546 times.
✓ Branch 1 taken 2721 times.
3267 if (pps->cu_qp_delta_enabled_flag)
1816 546 pps->diff_cu_qp_delta_depth = get_ue_golomb_long(gb);
1817
1818
1/2
✓ Branch 0 taken 3267 times.
✗ Branch 1 not taken.
3267 if (pps->diff_cu_qp_delta_depth < 0 ||
1819
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3267 times.
3267 pps->diff_cu_qp_delta_depth > sps->log2_diff_max_min_coding_block_size) {
1820 av_log(avctx, AV_LOG_ERROR, "diff_cu_qp_delta_depth %d is invalid\n",
1821 pps->diff_cu_qp_delta_depth);
1822 ret = AVERROR_INVALIDDATA;
1823 goto err;
1824 }
1825
1826 3267 pps->cb_qp_offset = get_se_golomb(gb);
1827
2/4
✓ Branch 0 taken 3267 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3267 times.
3267 if (pps->cb_qp_offset < -12 || pps->cb_qp_offset > 12) {
1828 av_log(avctx, AV_LOG_ERROR, "pps_cb_qp_offset out of range: %d\n",
1829 pps->cb_qp_offset);
1830 ret = AVERROR_INVALIDDATA;
1831 goto err;
1832 }
1833 3267 pps->cr_qp_offset = get_se_golomb(gb);
1834
2/4
✓ Branch 0 taken 3267 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3267 times.
3267 if (pps->cr_qp_offset < -12 || pps->cr_qp_offset > 12) {
1835 av_log(avctx, AV_LOG_ERROR, "pps_cr_qp_offset out of range: %d\n",
1836 pps->cr_qp_offset);
1837 ret = AVERROR_INVALIDDATA;
1838 goto err;
1839 }
1840 3267 pps->pic_slice_level_chroma_qp_offsets_present_flag = get_bits1(gb);
1841
1842 3267 pps->weighted_pred_flag = get_bits1(gb);
1843 3267 pps->weighted_bipred_flag = get_bits1(gb);
1844
1845 3267 pps->transquant_bypass_enable_flag = get_bits1(gb);
1846 3267 pps->tiles_enabled_flag = get_bits1(gb);
1847 3267 pps->entropy_coding_sync_enabled_flag = get_bits1(gb);
1848
1849
2/2
✓ Branch 0 taken 908 times.
✓ Branch 1 taken 2359 times.
3267 if (pps->tiles_enabled_flag) {
1850 908 int num_tile_columns_minus1 = get_ue_golomb(gb);
1851 908 int num_tile_rows_minus1 = get_ue_golomb(gb);
1852
1853
1/2
✓ Branch 0 taken 908 times.
✗ Branch 1 not taken.
908 if (num_tile_columns_minus1 < 0 ||
1854
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 908 times.
908 num_tile_columns_minus1 >= sps->ctb_width) {
1855 av_log(avctx, AV_LOG_ERROR, "num_tile_columns_minus1 out of range: %d\n",
1856 num_tile_columns_minus1);
1857 ret = num_tile_columns_minus1 < 0 ? num_tile_columns_minus1 : AVERROR_INVALIDDATA;
1858 goto err;
1859 }
1860
1/2
✓ Branch 0 taken 908 times.
✗ Branch 1 not taken.
908 if (num_tile_rows_minus1 < 0 ||
1861
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 908 times.
908 num_tile_rows_minus1 >= sps->ctb_height) {
1862 av_log(avctx, AV_LOG_ERROR, "num_tile_rows_minus1 out of range: %d\n",
1863 num_tile_rows_minus1);
1864 ret = num_tile_rows_minus1 < 0 ? num_tile_rows_minus1 : AVERROR_INVALIDDATA;
1865 goto err;
1866 }
1867 908 pps->num_tile_columns = num_tile_columns_minus1 + 1;
1868 908 pps->num_tile_rows = num_tile_rows_minus1 + 1;
1869
1870 908 pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1871 908 pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1872
2/4
✓ Branch 0 taken 908 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 908 times.
908 if (!pps->column_width || !pps->row_height) {
1873 ret = AVERROR(ENOMEM);
1874 goto err;
1875 }
1876
1877 908 pps->uniform_spacing_flag = get_bits1(gb);
1878
2/2
✓ Branch 0 taken 532 times.
✓ Branch 1 taken 376 times.
908 if (!pps->uniform_spacing_flag) {
1879 532 uint64_t sum = 0;
1880
2/2
✓ Branch 0 taken 2076 times.
✓ Branch 1 taken 532 times.
2608 for (i = 0; i < pps->num_tile_columns - 1; i++) {
1881 2076 pps->column_width[i] = get_ue_golomb_long(gb) + 1;
1882 2076 sum += pps->column_width[i];
1883 }
1884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 532 times.
532 if (sum >= sps->ctb_width) {
1885 av_log(avctx, AV_LOG_ERROR, "Invalid tile widths.\n");
1886 ret = AVERROR_INVALIDDATA;
1887 goto err;
1888 }
1889 532 pps->column_width[pps->num_tile_columns - 1] = sps->ctb_width - sum;
1890
1891 532 sum = 0;
1892
2/2
✓ Branch 0 taken 2062 times.
✓ Branch 1 taken 532 times.
2594 for (i = 0; i < pps->num_tile_rows - 1; i++) {
1893 2062 pps->row_height[i] = get_ue_golomb_long(gb) + 1;
1894 2062 sum += pps->row_height[i];
1895 }
1896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 532 times.
532 if (sum >= sps->ctb_height) {
1897 av_log(avctx, AV_LOG_ERROR, "Invalid tile heights.\n");
1898 ret = AVERROR_INVALIDDATA;
1899 goto err;
1900 }
1901 532 pps->row_height[pps->num_tile_rows - 1] = sps->ctb_height - sum;
1902 }
1903 908 pps->loop_filter_across_tiles_enabled_flag = get_bits1(gb);
1904 }
1905
1906 3267 pps->seq_loop_filter_across_slices_enabled_flag = get_bits1(gb);
1907
1908 3267 pps->deblocking_filter_control_present_flag = get_bits1(gb);
1909
2/2
✓ Branch 0 taken 1035 times.
✓ Branch 1 taken 2232 times.
3267 if (pps->deblocking_filter_control_present_flag) {
1910 1035 pps->deblocking_filter_override_enabled_flag = get_bits1(gb);
1911 1035 pps->disable_dbf = get_bits1(gb);
1912
2/2
✓ Branch 0 taken 893 times.
✓ Branch 1 taken 142 times.
1035 if (!pps->disable_dbf) {
1913 893 int beta_offset_div2 = get_se_golomb(gb);
1914 893 int tc_offset_div2 = get_se_golomb(gb) ;
1915
2/4
✓ Branch 0 taken 893 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 893 times.
893 if (beta_offset_div2 < -6 || beta_offset_div2 > 6) {
1916 av_log(avctx, AV_LOG_ERROR, "pps_beta_offset_div2 out of range: %d\n",
1917 beta_offset_div2);
1918 ret = AVERROR_INVALIDDATA;
1919 goto err;
1920 }
1921
2/4
✓ Branch 0 taken 893 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 893 times.
893 if (tc_offset_div2 < -6 || tc_offset_div2 > 6) {
1922 av_log(avctx, AV_LOG_ERROR, "pps_tc_offset_div2 out of range: %d\n",
1923 tc_offset_div2);
1924 ret = AVERROR_INVALIDDATA;
1925 goto err;
1926 }
1927 893 pps->beta_offset = 2 * beta_offset_div2;
1928 893 pps->tc_offset = 2 * tc_offset_div2;
1929