FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/ps.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 1054 1484 71.0%
Functions: 29 30 96.7%
Branches: 637 1005 63.4%

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