FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc_ps.c
Date: 2024-04-18 10:05:09
Exec Total Coverage
Lines: 923 1302 70.9%
Functions: 29 30 96.7%
Branches: 548 883 62.1%

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