FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc_ps.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 828 1066 77.7%
Functions: 21 21 100.0%
Branches: 502 723 69.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 "golomb.h"
28 #include "h2645_vui.h"
29 #include "hevc_data.h"
30 #include "hevc_ps.h"
31
32 static const uint8_t default_scaling_list_intra[] = {
33 16, 16, 16, 16, 17, 18, 21, 24,
34 16, 16, 16, 16, 17, 19, 22, 25,
35 16, 16, 17, 18, 20, 22, 25, 29,
36 16, 16, 18, 21, 24, 27, 31, 36,
37 17, 17, 20, 24, 30, 35, 41, 47,
38 18, 19, 22, 27, 35, 44, 54, 65,
39 21, 22, 25, 31, 41, 54, 70, 88,
40 24, 25, 29, 36, 47, 65, 88, 115
41 };
42
43 static const uint8_t default_scaling_list_inter[] = {
44 16, 16, 16, 16, 17, 18, 20, 24,
45 16, 16, 16, 17, 18, 20, 24, 25,
46 16, 16, 17, 18, 20, 24, 25, 28,
47 16, 17, 18, 20, 24, 25, 28, 33,
48 17, 18, 20, 24, 25, 28, 33, 41,
49 18, 20, 24, 25, 28, 33, 41, 54,
50 20, 24, 25, 28, 33, 41, 54, 71,
51 24, 25, 28, 33, 41, 54, 71, 91
52 };
53
54 static const uint8_t hevc_sub_width_c[] = {
55 1, 2, 2, 1
56 };
57
58 static const uint8_t hevc_sub_height_c[] = {
59 1, 2, 1, 1
60 };
61
62 3257 static void remove_pps(HEVCParamSets *s, int id)
63 {
64
4/4
✓ Branch 0 taken 2411 times.
✓ Branch 1 taken 846 times.
✓ Branch 2 taken 1789 times.
✓ Branch 3 taken 622 times.
3257 if (s->pps_list[id] && s->pps == (const HEVCPPS*)s->pps_list[id]->data)
65 1789 s->pps = NULL;
66 3257 av_buffer_unref(&s->pps_list[id]);
67 3257 }
68
69 607 static void remove_sps(HEVCParamSets *s, int id)
70 {
71 int i;
72
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 596 times.
607 if (s->sps_list[id]) {
73
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (s->sps == (const HEVCSPS*)s->sps_list[id]->data)
74 11 s->sps = NULL;
75
76 /* drop all PPS that depend on this SPS */
77
2/2
✓ Branch 0 taken 704 times.
✓ Branch 1 taken 11 times.
715 for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++)
78
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 693 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
704 if (s->pps_list[i] && ((HEVCPPS*)s->pps_list[i]->data)->sps_id == id)
79 11 remove_pps(s, i);
80
81
2/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
11 av_assert0(!(s->sps_list[id] && s->sps == (HEVCSPS*)s->sps_list[id]->data));
82 }
83 607 av_buffer_unref(&s->sps_list[id]);
84 607 }
85
86 594 static void remove_vps(HEVCParamSets *s, int id)
87 {
88 int i;
89
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 591 times.
594 if (s->vps_list[id]) {
90
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (s->vps == (const HEVCVPS*)s->vps_list[id]->data)
91 3 s->vps = NULL;
92
93
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 3 times.
51 for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++)
94
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
48 if (s->sps_list[i] && ((HEVCSPS*)s->sps_list[i]->data)->vps_id == id)
95 3 remove_sps(s, i);
96 }
97 594 av_buffer_unref(&s->vps_list[id]);
98 594 }
99
100 12350 int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx,
101 ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header)
102 {
103 12350 uint8_t rps_predict = 0;
104 int delta_poc;
105 12350 int k0 = 0;
106 12350 int k = 0;
107 int i;
108
109
4/4
✓ Branch 0 taken 11455 times.
✓ Branch 1 taken 895 times.
✓ Branch 2 taken 11172 times.
✓ Branch 3 taken 283 times.
12350 if (rps != sps->st_rps && sps->nb_st_rps)
110 11172 rps_predict = get_bits1(gb);
111
112
2/2
✓ Branch 0 taken 7414 times.
✓ Branch 1 taken 4936 times.
12350 if (rps_predict) {
113 const ShortTermRPS *rps_ridx;
114 int delta_rps;
115 unsigned abs_delta_rps;
116 7414 uint8_t use_delta_flag = 0;
117 uint8_t delta_rps_sign;
118
119
2/2
✓ Branch 0 taken 619 times.
✓ Branch 1 taken 6795 times.
7414 if (is_slice_header) {
120 619 unsigned int delta_idx = get_ue_golomb_long(gb) + 1;
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 619 times.
619 if (delta_idx > sps->nb_st_rps) {
122 av_log(avctx, AV_LOG_ERROR,
123 "Invalid value of delta_idx in slice header RPS: %d > %d.\n",
124 delta_idx, sps->nb_st_rps);
125 return AVERROR_INVALIDDATA;
126 }
127 619 rps_ridx = &sps->st_rps[sps->nb_st_rps - delta_idx];
128 619 rps->rps_idx_num_delta_pocs = rps_ridx->num_delta_pocs;
129 } else
130 6795 rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
131
132 7414 delta_rps_sign = get_bits1(gb);
133 7414 abs_delta_rps = get_ue_golomb_long(gb) + 1;
134
2/4
✓ Branch 0 taken 7414 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7414 times.
7414 if (abs_delta_rps < 1 || abs_delta_rps > 32768) {
135 av_log(avctx, AV_LOG_ERROR,
136 "Invalid value of abs_delta_rps: %d\n",
137 abs_delta_rps);
138 return AVERROR_INVALIDDATA;
139 }
140 7414 delta_rps = (1 - (delta_rps_sign << 1)) * abs_delta_rps;
141
2/2
✓ Branch 0 taken 32146 times.
✓ Branch 1 taken 7414 times.
39560 for (i = 0; i <= rps_ridx->num_delta_pocs; i++) {
142 32146 int used = rps->used[k] = get_bits1(gb);
143
144
2/2
✓ Branch 0 taken 6381 times.
✓ Branch 1 taken 25765 times.
32146 if (!used)
145 6381 use_delta_flag = get_bits1(gb);
146
147
4/4
✓ Branch 0 taken 6381 times.
✓ Branch 1 taken 25765 times.
✓ Branch 2 taken 50 times.
✓ Branch 3 taken 6331 times.
32146 if (used || use_delta_flag) {
148
2/2
✓ Branch 0 taken 20337 times.
✓ Branch 1 taken 5478 times.
25815 if (i < rps_ridx->num_delta_pocs)
149 20337 delta_poc = delta_rps + rps_ridx->delta_poc[i];
150 else
151 5478 delta_poc = delta_rps;
152 25815 rps->delta_poc[k] = delta_poc;
153
2/2
✓ Branch 0 taken 17936 times.
✓ Branch 1 taken 7879 times.
25815 if (delta_poc < 0)
154 17936 k0++;
155 25815 k++;
156 }
157 }
158
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7414 times.
7414 if (k >= FF_ARRAY_ELEMS(rps->used)) {
160 av_log(avctx, AV_LOG_ERROR,
161 "Invalid num_delta_pocs: %d\n", k);
162 return AVERROR_INVALIDDATA;
163 }
164
165 7414 rps->num_delta_pocs = k;
166 7414 rps->num_negative_pics = k0;
167 // sort in increasing order (smallest first)
168
1/2
✓ Branch 0 taken 7414 times.
✗ Branch 1 not taken.
7414 if (rps->num_delta_pocs != 0) {
169 int used, tmp;
170
2/2
✓ Branch 0 taken 18401 times.
✓ Branch 1 taken 7414 times.
25815 for (i = 1; i < rps->num_delta_pocs; i++) {
171 18401 delta_poc = rps->delta_poc[i];
172 18401 used = rps->used[i];
173
2/2
✓ Branch 0 taken 34278 times.
✓ Branch 1 taken 18401 times.
52679 for (k = i - 1; k >= 0; k--) {
174 34278 tmp = rps->delta_poc[k];
175
2/2
✓ Branch 0 taken 10983 times.
✓ Branch 1 taken 23295 times.
34278 if (delta_poc < tmp) {
176 10983 rps->delta_poc[k + 1] = tmp;
177 10983 rps->used[k + 1] = rps->used[k];
178 10983 rps->delta_poc[k] = delta_poc;
179 10983 rps->used[k] = used;
180 }
181 }
182 }
183 }
184
2/2
✓ Branch 0 taken 5635 times.
✓ Branch 1 taken 1779 times.
7414 if ((rps->num_negative_pics >> 1) != 0) {
185 int used;
186 5635 k = rps->num_negative_pics - 1;
187 // flip the negative values to largest first
188
2/2
✓ Branch 0 taken 7408 times.
✓ Branch 1 taken 5635 times.
13043 for (i = 0; i < rps->num_negative_pics >> 1; i++) {
189 7408 delta_poc = rps->delta_poc[i];
190 7408 used = rps->used[i];
191 7408 rps->delta_poc[i] = rps->delta_poc[k];
192 7408 rps->used[i] = rps->used[k];
193 7408 rps->delta_poc[k] = delta_poc;
194 7408 rps->used[k] = used;
195 7408 k--;
196 }
197 }
198 } else {
199 unsigned int prev, nb_positive_pics;
200 4936 rps->num_negative_pics = get_ue_golomb_long(gb);
201 4936 nb_positive_pics = get_ue_golomb_long(gb);
202
203
2/4
✓ Branch 0 taken 4936 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4936 times.
4936 if (rps->num_negative_pics >= HEVC_MAX_REFS ||
204 nb_positive_pics >= HEVC_MAX_REFS) {
205 av_log(avctx, AV_LOG_ERROR, "Too many refs in a short term RPS.\n");
206 return AVERROR_INVALIDDATA;
207 }
208
209 4936 rps->num_delta_pocs = rps->num_negative_pics + nb_positive_pics;
210
2/2
✓ Branch 0 taken 4614 times.
✓ Branch 1 taken 322 times.
4936 if (rps->num_delta_pocs) {
211 4614 prev = 0;
212
2/2
✓ Branch 0 taken 13877 times.
✓ Branch 1 taken 4614 times.
18491 for (i = 0; i < rps->num_negative_pics; i++) {
213 13877 delta_poc = get_ue_golomb_long(gb) + 1;
214
2/4
✓ Branch 0 taken 13877 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13877 times.
13877 if (delta_poc < 1 || delta_poc > 32768) {
215 av_log(avctx, AV_LOG_ERROR,
216 "Invalid value of delta_poc: %d\n",
217 delta_poc);
218 return AVERROR_INVALIDDATA;
219 }
220 13877 prev -= delta_poc;
221 13877 rps->delta_poc[i] = prev;
222 13877 rps->used[i] = get_bits1(gb);
223 }
224 4614 prev = 0;
225
2/2
✓ Branch 0 taken 1197 times.
✓ Branch 1 taken 4614 times.
5811 for (i = 0; i < nb_positive_pics; i++) {
226 1197 delta_poc = get_ue_golomb_long(gb) + 1;
227
2/4
✓ Branch 0 taken 1197 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1197 times.
1197 if (delta_poc < 1 || delta_poc > 32768) {
228 av_log(avctx, AV_LOG_ERROR,
229 "Invalid value of delta_poc: %d\n",
230 delta_poc);
231 return AVERROR_INVALIDDATA;
232 }
233 1197 prev += delta_poc;
234 1197 rps->delta_poc[rps->num_negative_pics + i] = prev;
235 1197 rps->used[rps->num_negative_pics + i] = get_bits1(gb);
236 }
237 }
238 }
239 12350 return 0;
240 }
241
242
243 2117 static int decode_profile_tier_level(GetBitContext *gb, AVCodecContext *avctx,
244 PTLCommon *ptl)
245 {
246 int i;
247
248
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2117 times.
2117 if (get_bits_left(gb) < 2+1+5 + 32 + 4 + 43 + 1)
249 return -1;
250
251 2117 ptl->profile_space = get_bits(gb, 2);
252 2117 ptl->tier_flag = get_bits1(gb);
253 2117 ptl->profile_idc = get_bits(gb, 5);
254
2/2
✓ Branch 0 taken 1765 times.
✓ Branch 1 taken 352 times.
2117 if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN)
255 1765 av_log(avctx, AV_LOG_DEBUG, "Main profile bitstream\n");
256
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 188 times.
352 else if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN_10)
257 164 av_log(avctx, AV_LOG_DEBUG, "Main 10 profile bitstream\n");
258
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 180 times.
188 else if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN_STILL_PICTURE)
259 8 av_log(avctx, AV_LOG_DEBUG, "Main Still Picture profile bitstream\n");
260
2/2
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 50 times.
180 else if (ptl->profile_idc == FF_PROFILE_HEVC_REXT)
261 130 av_log(avctx, AV_LOG_DEBUG, "Range Extension profile bitstream\n");
262 else
263 50 av_log(avctx, AV_LOG_WARNING, "Unknown HEVC profile: %d\n", ptl->profile_idc);
264
265
2/2
✓ Branch 0 taken 67744 times.
✓ Branch 1 taken 2117 times.
69861 for (i = 0; i < 32; i++) {
266 67744 ptl->profile_compatibility_flag[i] = get_bits1(gb);
267
268
6/6
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 67624 times.
✓ Branch 2 taken 80 times.
✓ Branch 3 taken 40 times.
✓ Branch 4 taken 40 times.
✓ Branch 5 taken 40 times.
67744 if (ptl->profile_idc == 0 && i > 0 && ptl->profile_compatibility_flag[i])
269 40 ptl->profile_idc = i;
270 }
271 2117 ptl->progressive_source_flag = get_bits1(gb);
272 2117 ptl->interlaced_source_flag = get_bits1(gb);
273 2117 ptl->non_packed_constraint_flag = get_bits1(gb);
274 2117 ptl->frame_only_constraint_flag = get_bits1(gb);
275
276 #define check_profile_idc(idc) \
277 ptl->profile_idc == idc || ptl->profile_compatibility_flag[idc]
278
279
8/12
✓ Branch 0 taken 1987 times.
✓ Branch 1 taken 130 times.
✓ Branch 2 taken 1987 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1977 times.
✓ Branch 5 taken 10 times.
✓ Branch 6 taken 1977 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1977 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1977 times.
✗ Branch 11 not taken.
2117 if (check_profile_idc(4) || check_profile_idc(5) || check_profile_idc(6) ||
280
6/12
✓ Branch 0 taken 1977 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1977 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1977 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1977 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1977 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1977 times.
✗ Branch 11 not taken.
1977 check_profile_idc(7) || check_profile_idc(8) || check_profile_idc(9) ||
281
2/4
✓ Branch 0 taken 1977 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1977 times.
1977 check_profile_idc(10)) {
282
283 140 ptl->max_12bit_constraint_flag = get_bits1(gb);
284 140 ptl->max_10bit_constraint_flag = get_bits1(gb);
285 140 ptl->max_8bit_constraint_flag = get_bits1(gb);
286 140 ptl->max_422chroma_constraint_flag = get_bits1(gb);
287 140 ptl->max_420chroma_constraint_flag = get_bits1(gb);
288 140 ptl->max_monochrome_constraint_flag = get_bits1(gb);
289 140 ptl->intra_constraint_flag = get_bits1(gb);
290 140 ptl->one_picture_only_constraint_flag = get_bits1(gb);
291 140 ptl->lower_bit_rate_constraint_flag = get_bits1(gb);
292
293
7/12
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 130 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 130 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 130 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 130 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 130 times.
140 if (check_profile_idc(5) || check_profile_idc(9) || check_profile_idc(10)) {
294 10 ptl->max_14bit_constraint_flag = get_bits1(gb);
295 10 skip_bits_long(gb, 33); // XXX_reserved_zero_33bits[0..32]
296 } else {
297 130 skip_bits_long(gb, 34); // XXX_reserved_zero_34bits[0..33]
298 }
299
4/4
✓ Branch 0 taken 1773 times.
✓ Branch 1 taken 204 times.
✓ Branch 2 taken 1461 times.
✓ Branch 3 taken 312 times.
1977 } else if (check_profile_idc(2)) {
300 1665 skip_bits(gb, 7);
301 1665 ptl->one_picture_only_constraint_flag = get_bits1(gb);
302 1665 skip_bits_long(gb, 35); // XXX_reserved_zero_35bits[0..34]
303 } else {
304 312 skip_bits_long(gb, 43); // XXX_reserved_zero_43bits[0..42]
305 }
306
307
9/12
✓ Branch 0 taken 352 times.
✓ Branch 1 taken 1765 times.
✓ Branch 2 taken 344 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 140 times.
✓ Branch 5 taken 204 times.
✓ Branch 6 taken 140 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 140 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 140 times.
✗ Branch 11 not taken.
2117 if (check_profile_idc(1) || check_profile_idc(2) || check_profile_idc(3) ||
308
4/12
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 130 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 10 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
140 check_profile_idc(4) || check_profile_idc(5) || check_profile_idc(9))
309 2117 ptl->inbld_flag = get_bits1(gb);
310 else
311 skip_bits1(gb);
312 #undef check_profile_idc
313
314 2117 return 0;
315 }
316
317 2065 static int parse_ptl(GetBitContext *gb, AVCodecContext *avctx,
318 PTL *ptl, int max_num_sub_layers)
319 {
320 int i;
321
2/4
✓ Branch 1 taken 2065 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2065 times.
4130 if (decode_profile_tier_level(gb, avctx, &ptl->general_ptl) < 0 ||
322
2/2
✓ Branch 1 taken 236 times.
✓ Branch 2 taken 1829 times.
2065 get_bits_left(gb) < 8 + (8*2 * (max_num_sub_layers - 1 > 0))) {
323 av_log(avctx, AV_LOG_ERROR, "PTL information too short\n");
324 return -1;
325 }
326
327 2065 ptl->general_ptl.level_idc = get_bits(gb, 8);
328
329
2/2
✓ Branch 0 taken 468 times.
✓ Branch 1 taken 2065 times.
2533 for (i = 0; i < max_num_sub_layers - 1; i++) {
330 468 ptl->sub_layer_profile_present_flag[i] = get_bits1(gb);
331 468 ptl->sub_layer_level_present_flag[i] = get_bits1(gb);
332 }
333
334
2/2
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 1829 times.
2065 if (max_num_sub_layers - 1> 0)
335
2/2
✓ Branch 0 taken 1420 times.
✓ Branch 1 taken 236 times.
1656 for (i = max_num_sub_layers - 1; i < 8; i++)
336 1420 skip_bits(gb, 2); // reserved_zero_2bits[i]
337
2/2
✓ Branch 0 taken 468 times.
✓ Branch 1 taken 2065 times.
2533 for (i = 0; i < max_num_sub_layers - 1; i++) {
338
3/4
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 416 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 52 times.
520 if (ptl->sub_layer_profile_present_flag[i] &&
339 52 decode_profile_tier_level(gb, avctx, &ptl->sub_layer_ptl[i]) < 0) {
340 av_log(avctx, AV_LOG_ERROR,
341 "PTL information for sublayer %i too short\n", i);
342 return -1;
343 }
344
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 416 times.
468 if (ptl->sub_layer_level_present_flag[i]) {
345
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 52 times.
52 if (get_bits_left(gb) < 8) {
346 av_log(avctx, AV_LOG_ERROR,
347 "Not enough data for sublayer %i level_idc\n", i);
348 return -1;
349 } else
350 52 ptl->sub_layer_ptl[i].level_idc = get_bits(gb, 8);
351 }
352 }
353
354 2065 return 0;
355 }
356
357 97 static void decode_sublayer_hrd(GetBitContext *gb, unsigned int nb_cpb,
358 int subpic_params_present)
359 {
360 int i;
361
362
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 97 times.
194 for (i = 0; i < nb_cpb; i++) {
363 97 get_ue_golomb_long(gb); // bit_rate_value_minus1
364 97 get_ue_golomb_long(gb); // cpb_size_value_minus1
365
366
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 71 times.
97 if (subpic_params_present) {
367 26 get_ue_golomb_long(gb); // cpb_size_du_value_minus1
368 26 get_ue_golomb_long(gb); // bit_rate_du_value_minus1
369 }
370 97 skip_bits1(gb); // cbr_flag
371 }
372 97 }
373
374 51 static int decode_hrd(GetBitContext *gb, int common_inf_present,
375 int max_sublayers)
376 {
377 51 int nal_params_present = 0, vcl_params_present = 0;
378 51 int subpic_params_present = 0;
379 int i;
380
381
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
51 if (common_inf_present) {
382 51 nal_params_present = get_bits1(gb);
383 51 vcl_params_present = get_bits1(gb);
384
385
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
51 if (nal_params_present || vcl_params_present) {
386 51 subpic_params_present = get_bits1(gb);
387
388
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 38 times.
51 if (subpic_params_present) {
389 13 skip_bits(gb, 8); // tick_divisor_minus2
390 13 skip_bits(gb, 5); // du_cpb_removal_delay_increment_length_minus1
391 13 skip_bits(gb, 1); // sub_pic_cpb_params_in_pic_timing_sei_flag
392 13 skip_bits(gb, 5); // dpb_output_delay_du_length_minus1
393 }
394
395 51 skip_bits(gb, 4); // bit_rate_scale
396 51 skip_bits(gb, 4); // cpb_size_scale
397
398
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 38 times.
51 if (subpic_params_present)
399 13 skip_bits(gb, 4); // cpb_size_du_scale
400
401 51 skip_bits(gb, 5); // initial_cpb_removal_delay_length_minus1
402 51 skip_bits(gb, 5); // au_cpb_removal_delay_length_minus1
403 51 skip_bits(gb, 5); // dpb_output_delay_length_minus1
404 }
405 }
406
407
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 51 times.
102 for (i = 0; i < max_sublayers; i++) {
408 51 int low_delay = 0;
409 51 unsigned int nb_cpb = 1;
410 51 int fixed_rate = get_bits1(gb);
411
412
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 28 times.
51 if (!fixed_rate)
413 23 fixed_rate = get_bits1(gb);
414
415
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 23 times.
51 if (fixed_rate)
416 28 get_ue_golomb_long(gb); // elemental_duration_in_tc_minus1
417 else
418 23 low_delay = get_bits1(gb);
419
420
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
51 if (!low_delay) {
421 51 nb_cpb = get_ue_golomb_long(gb) + 1;
422
2/4
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 51 times.
51 if (nb_cpb < 1 || nb_cpb > 32) {
423 av_log(NULL, AV_LOG_ERROR, "nb_cpb %d invalid\n", nb_cpb);
424 return AVERROR_INVALIDDATA;
425 }
426 }
427
428
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
51 if (nal_params_present)
429 51 decode_sublayer_hrd(gb, nb_cpb, subpic_params_present);
430
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 5 times.
51 if (vcl_params_present)
431 46 decode_sublayer_hrd(gb, nb_cpb, subpic_params_present);
432 }
433 51 return 0;
434 }
435
436 1036 int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx,
437 HEVCParamSets *ps)
438 {
439 int i,j;
440 1036 int vps_id = 0;
441 ptrdiff_t nal_size;
442 HEVCVPS *vps;
443 1036 AVBufferRef *vps_buf = av_buffer_allocz(sizeof(*vps));
444
445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1036 times.
1036 if (!vps_buf)
446 return AVERROR(ENOMEM);
447 1036 vps = (HEVCVPS*)vps_buf->data;
448
449 1036 av_log(avctx, AV_LOG_DEBUG, "Decoding VPS\n");
450
451 1036 nal_size = gb->buffer_end - gb->buffer;
452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1036 times.
1036 if (nal_size > sizeof(vps->data)) {
453 av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized VPS "
454 "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
455 nal_size, sizeof(vps->data));
456 vps->data_size = sizeof(vps->data);
457 } else {
458 1036 vps->data_size = nal_size;
459 }
460 1036 memcpy(vps->data, gb->buffer, vps->data_size);
461
462 1036 vps_id = get_bits(gb, 4);
463
464
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1036 times.
1036 if (get_bits(gb, 2) != 3) { // vps_reserved_three_2bits
465 av_log(avctx, AV_LOG_ERROR, "vps_reserved_three_2bits is not three\n");
466 goto err;
467 }
468
469 1036 vps->vps_max_layers = get_bits(gb, 6) + 1;
470 1036 vps->vps_max_sub_layers = get_bits(gb, 3) + 1;
471 1036 vps->vps_temporal_id_nesting_flag = get_bits1(gb);
472
473
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1036 times.
1036 if (get_bits(gb, 16) != 0xffff) { // vps_reserved_ffff_16bits
474 av_log(avctx, AV_LOG_ERROR, "vps_reserved_ffff_16bits is not 0xffff\n");
475 goto err;
476 }
477
478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1036 times.
1036 if (vps->vps_max_sub_layers > HEVC_MAX_SUB_LAYERS) {
479 av_log(avctx, AV_LOG_ERROR, "vps_max_sub_layers out of range: %d\n",
480 vps->vps_max_sub_layers);
481 goto err;
482 }
483
484
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1036 times.
1036 if (parse_ptl(gb, avctx, &vps->ptl, vps->vps_max_sub_layers) < 0)
485 goto err;
486
487 1036 vps->vps_sub_layer_ordering_info_present_flag = get_bits1(gb);
488
489
2/2
✓ Branch 0 taken 157 times.
✓ Branch 1 taken 879 times.
1036 i = vps->vps_sub_layer_ordering_info_present_flag ? 0 : vps->vps_max_sub_layers - 1;
490
2/2
✓ Branch 0 taken 1183 times.
✓ Branch 1 taken 1036 times.
2219 for (; i < vps->vps_max_sub_layers; i++) {
491 1183 vps->vps_max_dec_pic_buffering[i] = get_ue_golomb_long(gb) + 1;
492 1183 vps->vps_num_reorder_pics[i] = get_ue_golomb_long(gb);
493 1183 vps->vps_max_latency_increase[i] = get_ue_golomb_long(gb) - 1;
494
495
2/4
✓ Branch 0 taken 1183 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1183 times.
1183 if (vps->vps_max_dec_pic_buffering[i] > HEVC_MAX_DPB_SIZE || !vps->vps_max_dec_pic_buffering[i]) {
496 av_log(avctx, AV_LOG_ERROR, "vps_max_dec_pic_buffering_minus1 out of range: %d\n",
497 vps->vps_max_dec_pic_buffering[i] - 1);
498 goto err;
499 }
500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1183 times.
1183 if (vps->vps_num_reorder_pics[i] > vps->vps_max_dec_pic_buffering[i] - 1) {
501 av_log(avctx, AV_LOG_WARNING, "vps_max_num_reorder_pics out of range: %d\n",
502 vps->vps_num_reorder_pics[i]);
503 if (avctx->err_recognition & AV_EF_EXPLODE)
504 goto err;
505 }
506 }
507
508 1036 vps->vps_max_layer_id = get_bits(gb, 6);
509 1036 vps->vps_num_layer_sets = get_ue_golomb_long(gb) + 1;
510
2/4
✓ Branch 0 taken 1036 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1036 times.
✗ Branch 3 not taken.
1036 if (vps->vps_num_layer_sets < 1 || vps->vps_num_layer_sets > 1024 ||
511
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1036 times.
1036 (vps->vps_num_layer_sets - 1LL) * (vps->vps_max_layer_id + 1LL) > get_bits_left(gb)) {
512 av_log(avctx, AV_LOG_ERROR, "too many layer_id_included_flags\n");
513 goto err;
514 }
515
516
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1036 times.
1036 for (i = 1; i < vps->vps_num_layer_sets; i++)
517 for (j = 0; j <= vps->vps_max_layer_id; j++)
518 skip_bits(gb, 1); // layer_id_included_flag[i][j]
519
520 1036 vps->vps_timing_info_present_flag = get_bits1(gb);
521
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 998 times.
1036 if (vps->vps_timing_info_present_flag) {
522 38 vps->vps_num_units_in_tick = get_bits_long(gb, 32);
523 38 vps->vps_time_scale = get_bits_long(gb, 32);
524 38 vps->vps_poc_proportional_to_timing_flag = get_bits1(gb);
525
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 if (vps->vps_poc_proportional_to_timing_flag)
526 38 vps->vps_num_ticks_poc_diff_one = get_ue_golomb_long(gb) + 1;
527 38 vps->vps_num_hrd_parameters = get_ue_golomb_long(gb);
528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (vps->vps_num_hrd_parameters > (unsigned)vps->vps_num_layer_sets) {
529 av_log(avctx, AV_LOG_ERROR,
530 "vps_num_hrd_parameters %d is invalid\n", vps->vps_num_hrd_parameters);
531 goto err;
532 }
533
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 38 times.
43 for (i = 0; i < vps->vps_num_hrd_parameters; i++) {
534 5 int common_inf_present = 1;
535
536 5 get_ue_golomb_long(gb); // hrd_layer_set_idx
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (i)
538 common_inf_present = get_bits1(gb);
539 5 decode_hrd(gb, common_inf_present, vps->vps_max_sub_layers);
540 }
541 }
542 1036 get_bits1(gb); /* vps_extension_flag */
543
544
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1036 times.
1036 if (get_bits_left(gb) < 0) {
545 av_log(avctx, AV_LOG_ERROR,
546 "Overread VPS by %d bits\n", -get_bits_left(gb));
547 if (ps->vps_list[vps_id])
548 goto err;
549 }
550
551
2/2
✓ Branch 0 taken 445 times.
✓ Branch 1 taken 591 times.
1036 if (ps->vps_list[vps_id] &&
552
2/2
✓ Branch 0 taken 442 times.
✓ Branch 1 taken 3 times.
445 !memcmp(ps->vps_list[vps_id]->data, vps_buf->data, vps_buf->size)) {
553 442 av_buffer_unref(&vps_buf);
554 } else {
555 594 remove_vps(ps, vps_id);
556 594 ps->vps_list[vps_id] = vps_buf;
557 }
558
559 1036 return 0;
560
561 err:
562 av_buffer_unref(&vps_buf);
563 return AVERROR_INVALIDDATA;
564 }
565
566 253 static void decode_vui(GetBitContext *gb, AVCodecContext *avctx,
567 int apply_defdispwin, HEVCSPS *sps)
568 {
569 253 VUI backup_vui, *vui = &sps->vui;
570 GetBitContext backup;
571 253 int alt = 0;
572
573 253 ff_h2645_decode_common_vui_params(gb, &sps->vui.common, avctx);
574
575
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 202 times.
253 if (vui->common.video_signal_type_present_flag) {
576
4/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 41 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 5 times.
51 if (vui->common.video_full_range_flag && sps->pix_fmt == AV_PIX_FMT_YUV420P)
577 5 sps->pix_fmt = AV_PIX_FMT_YUVJ420P;
578
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 5 times.
51 if (vui->common.colour_description_present_flag) {
579
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (vui->common.matrix_coeffs == AVCOL_SPC_RGB) {
580 switch (sps->pix_fmt) {
581 case AV_PIX_FMT_YUV444P:
582 sps->pix_fmt = AV_PIX_FMT_GBRP;
583 break;
584 case AV_PIX_FMT_YUV444P10:
585 sps->pix_fmt = AV_PIX_FMT_GBRP10;
586 break;
587 case AV_PIX_FMT_YUV444P12:
588 sps->pix_fmt = AV_PIX_FMT_GBRP12;
589 break;
590 }
591 }
592 }
593 }
594
595 253 vui->neutra_chroma_indication_flag = get_bits1(gb);
596 253 vui->field_seq_flag = get_bits1(gb);
597 253 vui->frame_field_info_present_flag = get_bits1(gb);
598
599 // Backup context in case an alternate header is detected
600 253 memcpy(&backup, gb, sizeof(backup));
601 253 memcpy(&backup_vui, vui, sizeof(backup_vui));
602
3/4
✓ Branch 1 taken 93 times.
✓ Branch 2 taken 160 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 93 times.
253 if (get_bits_left(gb) >= 68 && show_bits(gb, 21) == 0x100000) {
603 vui->default_display_window_flag = 0;
604 av_log(avctx, AV_LOG_WARNING, "Invalid default display window\n");
605 } else
606 253 vui->default_display_window_flag = get_bits1(gb);
607
608
2/2
✓ Branch 0 taken 155 times.
✓ Branch 1 taken 98 times.
253 if (vui->default_display_window_flag) {
609 98 int vert_mult = hevc_sub_height_c[sps->chroma_format_idc];
610 98 int horiz_mult = hevc_sub_width_c[sps->chroma_format_idc];
611 98 vui->def_disp_win.left_offset = get_ue_golomb_long(gb) * horiz_mult;
612 98 vui->def_disp_win.right_offset = get_ue_golomb_long(gb) * horiz_mult;
613 98 vui->def_disp_win.top_offset = get_ue_golomb_long(gb) * vert_mult;
614 98 vui->def_disp_win.bottom_offset = get_ue_golomb_long(gb) * vert_mult;
615
616
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 44 times.
98 if (apply_defdispwin &&
617
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {
618 av_log(avctx, AV_LOG_DEBUG,
619 "discarding vui default display window, "
620 "original values are l:%u r:%u t:%u b:%u\n",
621 vui->def_disp_win.left_offset,
622 vui->def_disp_win.right_offset,
623 vui->def_disp_win.top_offset,
624 vui->def_disp_win.bottom_offset);
625
626 vui->def_disp_win.left_offset =
627 vui->def_disp_win.right_offset =
628 vui->def_disp_win.top_offset =
629 vui->def_disp_win.bottom_offset = 0;
630 }
631 }
632
633 253 timing_info:
634 253 vui->vui_timing_info_present_flag = get_bits1(gb);
635
636
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 160 times.
253 if (vui->vui_timing_info_present_flag) {
637
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 93 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
93 if( get_bits_left(gb) < 66 && !alt) {
638 // The alternate syntax seem to have timing info located
639 // at where def_disp_win is normally located
640 av_log(avctx, AV_LOG_WARNING,
641 "Strange VUI timing information, retrying...\n");
642 memcpy(vui, &backup_vui, sizeof(backup_vui));
643 memcpy(gb, &backup, sizeof(backup));
644 alt = 1;
645 goto timing_info;
646 }
647 93 vui->vui_num_units_in_tick = get_bits_long(gb, 32);
648 93 vui->vui_time_scale = get_bits_long(gb, 32);
649
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (alt) {
650 av_log(avctx, AV_LOG_INFO, "Retry got %"PRIu32"/%"PRIu32" fps\n",
651 vui->vui_time_scale, vui->vui_num_units_in_tick);
652 }
653 93 vui->vui_poc_proportional_to_timing_flag = get_bits1(gb);
654
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 55 times.
93 if (vui->vui_poc_proportional_to_timing_flag)
655 38 vui->vui_num_ticks_poc_diff_one_minus1 = get_ue_golomb_long(gb);
656 93 vui->vui_hrd_parameters_present_flag = get_bits1(gb);
657
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 47 times.
93 if (vui->vui_hrd_parameters_present_flag)
658 46 decode_hrd(gb, 1, sps->max_sub_layers);
659 }
660
661 253 vui->bitstream_restriction_flag = get_bits1(gb);
662
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 141 times.
253 if (vui->bitstream_restriction_flag) {
663
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
112 if (get_bits_left(gb) < 8 && !alt) {
664 av_log(avctx, AV_LOG_WARNING,
665 "Strange VUI bitstream restriction information, retrying"
666 " from timing information...\n");
667 memcpy(vui, &backup_vui, sizeof(backup_vui));
668 memcpy(gb, &backup, sizeof(backup));
669 alt = 1;
670 goto timing_info;
671 }
672 112 vui->tiles_fixed_structure_flag = get_bits1(gb);
673 112 vui->motion_vectors_over_pic_boundaries_flag = get_bits1(gb);
674 112 vui->restricted_ref_pic_lists_flag = get_bits1(gb);
675 112 vui->min_spatial_segmentation_idc = get_ue_golomb_long(gb);
676 112 vui->max_bytes_per_pic_denom = get_ue_golomb_long(gb);
677 112 vui->max_bits_per_min_cu_denom = get_ue_golomb_long(gb);
678 112 vui->log2_max_mv_length_horizontal = get_ue_golomb_long(gb);
679 112 vui->log2_max_mv_length_vertical = get_ue_golomb_long(gb);
680 }
681
682
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 253 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
253 if (get_bits_left(gb) < 1 && !alt) {
683 // XXX: Alternate syntax when sps_range_extension_flag != 0?
684 av_log(avctx, AV_LOG_WARNING,
685 "Overread in VUI, retrying from timing information...\n");
686 memcpy(vui, &backup_vui, sizeof(backup_vui));
687 memcpy(gb, &backup, sizeof(backup));
688 alt = 1;
689 goto timing_info;
690 }
691 253 }
692
693 511 static void set_default_scaling_list_data(ScalingList *sl)
694 {
695 int matrixId;
696
697
2/2
✓ Branch 0 taken 3066 times.
✓ Branch 1 taken 511 times.
3577 for (matrixId = 0; matrixId < 6; matrixId++) {
698 // 4x4 default is 16
699 3066 memset(sl->sl[0][matrixId], 16, 16);
700 3066 sl->sl_dc[0][matrixId] = 16; // default for 16x16
701 3066 sl->sl_dc[1][matrixId] = 16; // default for 32x32
702 }
703 511 memcpy(sl->sl[1][0], default_scaling_list_intra, 64);
704 511 memcpy(sl->sl[1][1], default_scaling_list_intra, 64);
705 511 memcpy(sl->sl[1][2], default_scaling_list_intra, 64);
706 511 memcpy(sl->sl[1][3], default_scaling_list_inter, 64);
707 511 memcpy(sl->sl[1][4], default_scaling_list_inter, 64);
708 511 memcpy(sl->sl[1][5], default_scaling_list_inter, 64);
709 511 memcpy(sl->sl[2][0], default_scaling_list_intra, 64);
710 511 memcpy(sl->sl[2][1], default_scaling_list_intra, 64);
711 511 memcpy(sl->sl[2][2], default_scaling_list_intra, 64);
712 511 memcpy(sl->sl[2][3], default_scaling_list_inter, 64);
713 511 memcpy(sl->sl[2][4], default_scaling_list_inter, 64);
714 511 memcpy(sl->sl[2][5], default_scaling_list_inter, 64);
715 511 memcpy(sl->sl[3][0], default_scaling_list_intra, 64);
716 511 memcpy(sl->sl[3][1], default_scaling_list_intra, 64);
717 511 memcpy(sl->sl[3][2], default_scaling_list_intra, 64);
718 511 memcpy(sl->sl[3][3], default_scaling_list_inter, 64);
719 511 memcpy(sl->sl[3][4], default_scaling_list_inter, 64);
720 511 memcpy(sl->sl[3][5], default_scaling_list_inter, 64);
721 511 }
722
723 495 static int scaling_list_data(GetBitContext *gb, AVCodecContext *avctx, ScalingList *sl, HEVCSPS *sps)
724 {
725 uint8_t scaling_list_pred_mode_flag;
726 uint8_t scaling_list_dc_coef[2][6];
727 int size_id, matrix_id, pos;
728 int i;
729
730
2/2
✓ Branch 0 taken 1980 times.
✓ Branch 1 taken 495 times.
2475 for (size_id = 0; size_id < 4; size_id++)
731
4/4
✓ Branch 0 taken 990 times.
✓ Branch 1 taken 8910 times.
✓ Branch 2 taken 9900 times.
✓ Branch 3 taken 1980 times.
11880 for (matrix_id = 0; matrix_id < 6; matrix_id += ((size_id == 3) ? 3 : 1)) {
732 9900 scaling_list_pred_mode_flag = get_bits1(gb);
733
2/2
✓ Branch 0 taken 3978 times.
✓ Branch 1 taken 5922 times.
9900 if (!scaling_list_pred_mode_flag) {
734 3978 unsigned int delta = get_ue_golomb_long(gb);
735 /* Only need to handle non-zero delta. Zero means default,
736 * which should already be in the arrays. */
737
2/2
✓ Branch 0 taken 3852 times.
✓ Branch 1 taken 126 times.
3978 if (delta) {
738 // Copy from previous array.
739
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3852 times.
3852 delta *= (size_id == 3) ? 3 : 1;
740
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3852 times.
3852 if (matrix_id < delta) {
741 av_log(avctx, AV_LOG_ERROR,
742 "Invalid delta in scaling list data: %d.\n", delta);
743 return AVERROR_INVALIDDATA;
744 }
745
746
2/2
✓ Branch 0 taken 2231 times.
✓ Branch 1 taken 1621 times.
3852 memcpy(sl->sl[size_id][matrix_id],
747 3852 sl->sl[size_id][matrix_id - delta],
748 size_id > 0 ? 64 : 16);
749
2/2
✓ Branch 0 taken 530 times.
✓ Branch 1 taken 3322 times.
3852 if (size_id > 1)
750 530 sl->sl_dc[size_id - 2][matrix_id] = sl->sl_dc[size_id - 2][matrix_id - delta];
751 }
752 } else {
753 int next_coef, coef_num;
754 int32_t scaling_list_delta_coef;
755
756 5922 next_coef = 8;
757 5922 coef_num = FFMIN(64, 1 << (4 + (size_id << 1)));
758
2/2
✓ Branch 0 taken 3388 times.
✓ Branch 1 taken 2534 times.
5922 if (size_id > 1) {
759 3388 int scaling_list_coeff_minus8 = get_se_golomb(gb);
760
2/4
✓ Branch 0 taken 3388 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3388 times.
3388 if (scaling_list_coeff_minus8 < -7 ||
761 scaling_list_coeff_minus8 > 247)
762 return AVERROR_INVALIDDATA;
763 3388 scaling_list_dc_coef[size_id - 2][matrix_id] = scaling_list_coeff_minus8 + 8;
764 3388 next_coef = scaling_list_dc_coef[size_id - 2][matrix_id];
765 3388 sl->sl_dc[size_id - 2][matrix_id] = next_coef;
766 }
767
2/2
✓ Branch 0 taken 315264 times.
✓ Branch 1 taken 5922 times.
321186 for (i = 0; i < coef_num; i++) {
768
2/2
✓ Branch 0 taken 21248 times.
✓ Branch 1 taken 294016 times.
315264 if (size_id == 0)
769 21248 pos = 4 * ff_hevc_diag_scan4x4_y[i] +
770 21248 ff_hevc_diag_scan4x4_x[i];
771 else
772 294016 pos = 8 * ff_hevc_diag_scan8x8_y[i] +
773 294016 ff_hevc_diag_scan8x8_x[i];
774
775 315264 scaling_list_delta_coef = get_se_golomb(gb);
776 315264 next_coef = (next_coef + 256U + scaling_list_delta_coef) % 256;
777 315264 sl->sl[size_id][matrix_id][pos] = next_coef;
778 }
779 }
780 }
781
782
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 452 times.
495 if (sps->chroma_format_idc == 3) {
783
2/2
✓ Branch 0 taken 2752 times.
✓ Branch 1 taken 43 times.
2795 for (i = 0; i < 64; i++) {
784 2752 sl->sl[3][1][i] = sl->sl[2][1][i];
785 2752 sl->sl[3][2][i] = sl->sl[2][2][i];
786 2752 sl->sl[3][4][i] = sl->sl[2][4][i];
787 2752 sl->sl[3][5][i] = sl->sl[2][5][i];
788 }
789 43 sl->sl_dc[1][1] = sl->sl_dc[0][1];
790 43 sl->sl_dc[1][2] = sl->sl_dc[0][2];
791 43 sl->sl_dc[1][4] = sl->sl_dc[0][4];
792 43 sl->sl_dc[1][5] = sl->sl_dc[0][5];
793 }
794
795
796 495 return 0;
797 }
798
799 1029 static int map_pixel_format(AVCodecContext *avctx, HEVCSPS *sps)
800 {
801 const AVPixFmtDescriptor *desc;
802
3/5
✓ Branch 0 taken 895 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 111 times.
✓ Branch 3 taken 23 times.
✗ Branch 4 not taken.
1029 switch (sps->bit_depth) {
803 895 case 8:
804
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 891 times.
895 if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY8;
805
2/2
✓ Branch 0 taken 882 times.
✓ Branch 1 taken 13 times.
895 if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P;
806
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 895 times.
895 if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P;
807
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 886 times.
895 if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P;
808 895 break;
809 case 9:
810 if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY9;
811 if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P9;
812 if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P9;
813 if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P9;
814 break;
815 111 case 10:
816
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 111 times.
111 if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY10;
817
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 29 times.
111 if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P10;
818
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 82 times.
111 if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P10;
819
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 111 times.
111 if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P10;
820 111 break;
821 23 case 12:
822
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;
823
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;
824
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;
825
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;
826 23 break;
827 default:
828 av_log(avctx, AV_LOG_ERROR,
829 "The following bit-depths are currently specified: 8, 9, 10 and 12 bits, "
830 "chroma_format_idc is %d, depth is %d\n",
831 sps->chroma_format_idc, sps->bit_depth);
832 return AVERROR_INVALIDDATA;
833 }
834
835 1029 desc = av_pix_fmt_desc_get(sps->pix_fmt);
836
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if (!desc)
837 return AVERROR(EINVAL);
838
839 1029 sps->hshift[0] = sps->vshift[0] = 0;
840 1029 sps->hshift[2] = sps->hshift[1] = desc->log2_chroma_w;
841 1029 sps->vshift[2] = sps->vshift[1] = desc->log2_chroma_h;
842
843 1029 sps->pixel_shift = sps->bit_depth > 8;
844
845 1029 return 0;
846 }
847
848 1029 int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id,
849 int apply_defdispwin, AVBufferRef **vps_list, AVCodecContext *avctx)
850 {
851 HEVCWindow *ow;
852 1029 int ret = 0;
853 int log2_diff_max_min_transform_block_size;
854 int bit_depth_chroma, start, vui_present, sublayer_ordering_info;
855 int i;
856
857 // Coded parameters
858
859 1029 sps->vps_id = get_bits(gb, 4);
860
861
2/4
✓ Branch 0 taken 1029 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1029 times.
1029 if (vps_list && !vps_list[sps->vps_id]) {
862 av_log(avctx, AV_LOG_ERROR, "VPS %d does not exist\n",
863 sps->vps_id);
864 return AVERROR_INVALIDDATA;
865 }
866
867 1029 sps->max_sub_layers = get_bits(gb, 3) + 1;
868
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if (sps->max_sub_layers > HEVC_MAX_SUB_LAYERS) {
869 av_log(avctx, AV_LOG_ERROR, "sps_max_sub_layers out of range: %d\n",
870 sps->max_sub_layers);
871 return AVERROR_INVALIDDATA;
872 }
873
874 1029 sps->temporal_id_nesting_flag = get_bits(gb, 1);
875
876
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1029 times.
1029 if ((ret = parse_ptl(gb, avctx, &sps->ptl, sps->max_sub_layers)) < 0)
877 return ret;
878
879 1029 *sps_id = get_ue_golomb_long(gb);
880
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if (*sps_id >= HEVC_MAX_SPS_COUNT) {
881 av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", *sps_id);
882 return AVERROR_INVALIDDATA;
883 }
884
885 1029 sps->chroma_format_idc = get_ue_golomb_long(gb);
886
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if (sps->chroma_format_idc > 3U) {
887 av_log(avctx, AV_LOG_ERROR, "chroma_format_idc %d is invalid\n", sps->chroma_format_idc);
888 return AVERROR_INVALIDDATA;
889 }
890
891
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 997 times.
1029 if (sps->chroma_format_idc == 3)
892 32 sps->separate_colour_plane_flag = get_bits1(gb);
893
894
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if (sps->separate_colour_plane_flag)
895 sps->chroma_format_idc = 0;
896
897 1029 sps->width = get_ue_golomb_long(gb);
898 1029 sps->height = get_ue_golomb_long(gb);
899
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if ((ret = av_image_check_size(sps->width,
900 1029 sps->height, 0, avctx)) < 0)
901 return ret;
902
903
2/2
✓ Branch 1 taken 846 times.
✓ Branch 2 taken 183 times.
1029 if (get_bits1(gb)) { // pic_conformance_flag
904 846 int vert_mult = hevc_sub_height_c[sps->chroma_format_idc];
905 846 int horiz_mult = hevc_sub_width_c[sps->chroma_format_idc];
906 846 sps->pic_conf_win.left_offset = get_ue_golomb_long(gb) * horiz_mult;
907 846 sps->pic_conf_win.right_offset = get_ue_golomb_long(gb) * horiz_mult;
908 846 sps->pic_conf_win.top_offset = get_ue_golomb_long(gb) * vert_mult;
909 846 sps->pic_conf_win.bottom_offset = get_ue_golomb_long(gb) * vert_mult;
910
911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 846 times.
846 if (avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {
912 av_log(avctx, AV_LOG_DEBUG,
913 "discarding sps conformance window, "
914 "original values are l:%u r:%u t:%u b:%u\n",
915 sps->pic_conf_win.left_offset,
916 sps->pic_conf_win.right_offset,
917 sps->pic_conf_win.top_offset,
918 sps->pic_conf_win.bottom_offset);
919
920 sps->pic_conf_win.left_offset =
921 sps->pic_conf_win.right_offset =
922 sps->pic_conf_win.top_offset =
923 sps->pic_conf_win.bottom_offset = 0;
924 }
925 846 sps->output_window = sps->pic_conf_win;
926 }
927
928 1029 sps->bit_depth = get_ue_golomb_long(gb) + 8;
929 1029 bit_depth_chroma = get_ue_golomb_long(gb) + 8;
930
3/4
✓ Branch 0 taken 1025 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1025 times.
1029 if (sps->chroma_format_idc && bit_depth_chroma != sps->bit_depth) {
931 av_log(avctx, AV_LOG_ERROR,
932 "Luma bit depth (%d) is different from chroma bit depth (%d), "
933 "this is unsupported.\n",
934 sps->bit_depth, bit_depth_chroma);
935 return AVERROR_INVALIDDATA;
936 }
937 1029 sps->bit_depth_chroma = bit_depth_chroma;
938
939 1029 ret = map_pixel_format(avctx, sps);
940
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if (ret < 0)
941 return ret;
942
943 1029 sps->log2_max_poc_lsb = get_ue_golomb_long(gb) + 4;
944
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if (sps->log2_max_poc_lsb > 16) {
945 av_log(avctx, AV_LOG_ERROR, "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n",
946 sps->log2_max_poc_lsb - 4);
947 return AVERROR_INVALIDDATA;
948 }
949
950 1029 sublayer_ordering_info = get_bits1(gb);
951
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 882 times.
1029 start = sublayer_ordering_info ? 0 : sps->max_sub_layers - 1;
952
2/2
✓ Branch 0 taken 1176 times.
✓ Branch 1 taken 1029 times.
2205 for (i = start; i < sps->max_sub_layers; i++) {
953 1176 sps->temporal_layer[i].max_dec_pic_buffering = get_ue_golomb_long(gb) + 1;
954 1176 sps->temporal_layer[i].num_reorder_pics = get_ue_golomb_long(gb);
955 1176 sps->temporal_layer[i].max_latency_increase = get_ue_golomb_long(gb) - 1;
956
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1176 times.
1176 if (sps->temporal_layer[i].max_dec_pic_buffering > (unsigned)HEVC_MAX_DPB_SIZE) {
957 av_log(avctx, AV_LOG_ERROR, "sps_max_dec_pic_buffering_minus1 out of range: %d\n",
958 sps->temporal_layer[i].max_dec_pic_buffering - 1U);
959 return AVERROR_INVALIDDATA;
960 }
961
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1176 times.
1176 if (sps->temporal_layer[i].num_reorder_pics > sps->temporal_layer[i].max_dec_pic_buffering - 1) {
962 av_log(avctx, AV_LOG_WARNING, "sps_max_num_reorder_pics out of range: %d\n",
963 sps->temporal_layer[i].num_reorder_pics);
964 if (avctx->err_recognition & AV_EF_EXPLODE ||
965 sps->temporal_layer[i].num_reorder_pics > HEVC_MAX_DPB_SIZE - 1) {
966 return AVERROR_INVALIDDATA;
967 }
968 sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[i].num_reorder_pics + 1;
969 }
970 }
971
972
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 882 times.
1029 if (!sublayer_ordering_info) {
973
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 147 times.
229 for (i = 0; i < start; i++) {
974 82 sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[start].max_dec_pic_buffering;
975 82 sps->temporal_layer[i].num_reorder_pics = sps->temporal_layer[start].num_reorder_pics;
976 82 sps->temporal_layer[i].max_latency_increase = sps->temporal_layer[start].max_latency_increase;
977 }
978 }
979
980 1029 sps->log2_min_cb_size = get_ue_golomb_long(gb) + 3;
981 1029 sps->log2_diff_max_min_coding_block_size = get_ue_golomb_long(gb);
982 1029 sps->log2_min_tb_size = get_ue_golomb_long(gb) + 2;
983 1029 log2_diff_max_min_transform_block_size = get_ue_golomb_long(gb);
984 1029 sps->log2_max_trafo_size = log2_diff_max_min_transform_block_size +
985 1029 sps->log2_min_tb_size;
986
987
2/4
✓ Branch 0 taken 1029 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1029 times.
1029 if (sps->log2_min_cb_size < 3 || sps->log2_min_cb_size > 30) {
988 av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_min_cb_size", sps->log2_min_cb_size);
989 return AVERROR_INVALIDDATA;
990 }
991
992
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if (sps->log2_diff_max_min_coding_block_size > 30) {
993 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);
994 return AVERROR_INVALIDDATA;
995 }
996
997
2/4
✓ Branch 0 taken 1029 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1029 times.
1029 if (sps->log2_min_tb_size >= sps->log2_min_cb_size || sps->log2_min_tb_size < 2) {
998 av_log(avctx, AV_LOG_ERROR, "Invalid value for log2_min_tb_size");
999 return AVERROR_INVALIDDATA;
1000 }
1001
1002
2/4
✓ Branch 0 taken 1029 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1029 times.
1029 if (log2_diff_max_min_transform_block_size < 0 || log2_diff_max_min_transform_block_size > 30) {
1003 av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_diff_max_min_transform_block_size", log2_diff_max_min_transform_block_size);
1004 return AVERROR_INVALIDDATA;
1005 }
1006
1007 1029 sps->max_transform_hierarchy_depth_inter = get_ue_golomb_long(gb);
1008 1029 sps->max_transform_hierarchy_depth_intra = get_ue_golomb_long(gb);
1009
1010 1029 sps->scaling_list_enable_flag = get_bits1(gb);
1011
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 987 times.
1029 if (sps->scaling_list_enable_flag) {
1012 42 set_default_scaling_list_data(&sps->scaling_list);
1013
1014
2/2
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 16 times.
42 if (get_bits1(gb)) {
1015 26 ret = scaling_list_data(gb, avctx, &sps->scaling_list, sps);
1016
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (ret < 0)
1017 return ret;
1018 }
1019 }
1020
1021 1029 sps->amp_enabled_flag = get_bits1(gb);
1022 1029 sps->sao_enabled = get_bits1(gb);
1023
1024 1029 sps->pcm_enabled_flag = get_bits1(gb);
1025
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 954 times.
1029 if (sps->pcm_enabled_flag) {
1026 75 sps->pcm.bit_depth = get_bits(gb, 4) + 1;
1027 75 sps->pcm.bit_depth_chroma = get_bits(gb, 4) + 1;
1028 75 sps->pcm.log2_min_pcm_cb_size = get_ue_golomb_long(gb) + 3;
1029 150 sps->pcm.log2_max_pcm_cb_size = sps->pcm.log2_min_pcm_cb_size +
1030 75 get_ue_golomb_long(gb);
1031
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) {
1032 av_log(avctx, AV_LOG_ERROR,
1033 "PCM bit depth (%d, %d) is greater than normal bit depth (%d)\n",
1034 sps->pcm.bit_depth, sps->pcm.bit_depth_chroma, sps->bit_depth);
1035 return AVERROR_INVALIDDATA;
1036 }
1037
1038 75 sps->pcm.loop_filter_disable_flag = get_bits1(gb);
1039 }
1040
1041 1029 sps->nb_st_rps = get_ue_golomb_long(gb);
1042
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if (sps->nb_st_rps > HEVC_MAX_SHORT_TERM_REF_PIC_SETS) {
1043 av_log(avctx, AV_LOG_ERROR, "Too many short term RPS: %d.\n",
1044 sps->nb_st_rps);
1045 return AVERROR_INVALIDDATA;
1046 }
1047
2/2
✓ Branch 0 taken 9115 times.
✓ Branch 1 taken 1029 times.
10144 for (i = 0; i < sps->nb_st_rps; i++) {
1048
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9115 times.
9115 if ((ret = ff_hevc_decode_short_term_rps(gb, avctx, &sps->st_rps[i],
1049 sps, 0)) < 0)
1050 return ret;
1051 }
1052
1053 1029 sps->long_term_ref_pics_present_flag = get_bits1(gb);
1054
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 928 times.
1029 if (sps->long_term_ref_pics_present_flag) {
1055 101 sps->num_long_term_ref_pics_sps = get_ue_golomb_long(gb);
1056
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) {
1057 av_log(avctx, AV_LOG_ERROR, "Too many long term ref pics: %d.\n",
1058 sps->num_long_term_ref_pics_sps);
1059 return AVERROR_INVALIDDATA;
1060 }
1061
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++) {
1062 64 sps->lt_ref_pic_poc_lsb_sps[i] = get_bits(gb, sps->log2_max_poc_lsb);
1063 64 sps->used_by_curr_pic_lt_sps_flag[i] = get_bits1(gb);
1064 }
1065 }
1066
1067 1029 sps->sps_temporal_mvp_enabled_flag = get_bits1(gb);
1068 1029 sps->sps_strong_intra_smoothing_enable_flag = get_bits1(gb);
1069 1029 sps->vui.common.sar = (AVRational){0, 1};
1070 1029 vui_present = get_bits1(gb);
1071
2/2
✓ Branch 0 taken 253 times.
✓ Branch 1 taken 776 times.
1029 if (vui_present)
1072 253 decode_vui(gb, avctx, apply_defdispwin, sps);
1073
1074
2/2
✓ Branch 1 taken 76 times.
✓ Branch 2 taken 953 times.
1029 if (get_bits1(gb)) { // sps_extension_flag
1075 76 sps->sps_range_extension_flag = get_bits1(gb);
1076 76 skip_bits(gb, 7); //sps_extension_7bits = get_bits(gb, 7);
1077
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 20 times.
76 if (sps->sps_range_extension_flag) {
1078 56 sps->transform_skip_rotation_enabled_flag = get_bits1(gb);
1079 56 sps->transform_skip_context_enabled_flag = get_bits1(gb);
1080 56 sps->implicit_rdpcm_enabled_flag = get_bits1(gb);
1081
1082 56 sps->explicit_rdpcm_enabled_flag = get_bits1(gb);
1083
1084 56 sps->extended_precision_processing_flag = get_bits1(gb);
1085
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (sps->extended_precision_processing_flag)
1086 av_log(avctx, AV_LOG_WARNING,
1087 "extended_precision_processing_flag not yet implemented\n");
1088
1089 56 sps->intra_smoothing_disabled_flag = get_bits1(gb);
1090 56 sps->high_precision_offsets_enabled_flag = get_bits1(gb);
1091
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 18 times.
56 if (sps->high_precision_offsets_enabled_flag)
1092 38 av_log(avctx, AV_LOG_WARNING,
1093 "high_precision_offsets_enabled_flag not yet implemented\n");
1094
1095 56 sps->persistent_rice_adaptation_enabled_flag = get_bits1(gb);
1096
1097 56 sps->cabac_bypass_alignment_enabled_flag = get_bits1(gb);
1098
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (sps->cabac_bypass_alignment_enabled_flag)
1099 av_log(avctx, AV_LOG_WARNING,
1100 "cabac_bypass_alignment_enabled_flag not yet implemented\n");
1101 }
1102 }
1103
2/2
✓ Branch 0 taken 421 times.
✓ Branch 1 taken 608 times.
1029 if (apply_defdispwin) {
1104 421 sps->output_window.left_offset += sps->vui.def_disp_win.left_offset;
1105 421 sps->output_window.right_offset += sps->vui.def_disp_win.right_offset;
1106 421 sps->output_window.top_offset += sps->vui.def_disp_win.top_offset;
1107 421 sps->output_window.bottom_offset += sps->vui.def_disp_win.bottom_offset;
1108 }
1109
1110 1029 ow = &sps->output_window;
1111
1/2
✓ Branch 0 taken 1029 times.
✗ Branch 1 not taken.
1029 if (ow->left_offset >= INT_MAX - ow->right_offset ||
1112
1/2
✓ Branch 0 taken 1029 times.
✗ Branch 1 not taken.
1029 ow->top_offset >= INT_MAX - ow->bottom_offset ||
1113
1/2
✓ Branch 0 taken 1029 times.
✗ Branch 1 not taken.
1029 ow->left_offset + ow->right_offset >= sps->width ||
1114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 ow->top_offset + ow->bottom_offset >= sps->height) {
1115 av_log(avctx, AV_LOG_WARNING, "Invalid cropping offsets: %u/%u/%u/%u\n",
1116 ow->left_offset, ow->right_offset, ow->top_offset, ow->bottom_offset);
1117 if (avctx->err_recognition & AV_EF_EXPLODE) {
1118 return AVERROR_INVALIDDATA;
1119 }
1120 av_log(avctx, AV_LOG_WARNING,
1121 "Displaying the whole video surface.\n");
1122 memset(ow, 0, sizeof(*ow));
1123 memset(&sps->pic_conf_win, 0, sizeof(sps->pic_conf_win));
1124 }
1125
1126 // Inferred parameters
1127 1029 sps->log2_ctb_size = sps->log2_min_cb_size +
1128 1029 sps->log2_diff_max_min_coding_block_size;
1129 1029 sps->log2_min_pu_size = sps->log2_min_cb_size - 1;
1130
1131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if (sps->log2_ctb_size > HEVC_MAX_LOG2_CTB_SIZE) {
1132 av_log(avctx, AV_LOG_ERROR, "CTB size out of range: 2^%d\n", sps->log2_ctb_size);
1133 return AVERROR_INVALIDDATA;
1134 }
1135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if (sps->log2_ctb_size < 4) {
1136 av_log(avctx,
1137 AV_LOG_ERROR,
1138 "log2_ctb_size %d differs from the bounds of any known profile\n",
1139 sps->log2_ctb_size);
1140 avpriv_request_sample(avctx, "log2_ctb_size %d", sps->log2_ctb_size);
1141 return AVERROR_INVALIDDATA;
1142 }
1143
1144 1029 sps->ctb_width = (sps->width + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
1145 1029 sps->ctb_height = (sps->height + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
1146 1029 sps->ctb_size = sps->ctb_width * sps->ctb_height;
1147
1148 1029 sps->min_cb_width = sps->width >> sps->log2_min_cb_size;
1149 1029 sps->min_cb_height = sps->height >> sps->log2_min_cb_size;
1150 1029 sps->min_tb_width = sps->width >> sps->log2_min_tb_size;
1151 1029 sps->min_tb_height = sps->height >> sps->log2_min_tb_size;
1152 1029 sps->min_pu_width = sps->width >> sps->log2_min_pu_size;
1153 1029 sps->min_pu_height = sps->height >> sps->log2_min_pu_size;
1154 1029 sps->tb_mask = (1 << (sps->log2_ctb_size - sps->log2_min_tb_size)) - 1;
1155
1156 1029 sps->qp_bd_offset = 6 * (sps->bit_depth - 8);
1157
1158
1/2
✓ Branch 0 taken 1029 times.
✗ Branch 1 not taken.
1029 if (av_mod_uintp2(sps->width, sps->log2_min_cb_size) ||
1159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 av_mod_uintp2(sps->height, sps->log2_min_cb_size)) {
1160 av_log(avctx, AV_LOG_ERROR, "Invalid coded frame dimensions.\n");
1161 return AVERROR_INVALIDDATA;
1162 }
1163
1164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if (sps->max_transform_hierarchy_depth_inter > sps->log2_ctb_size - sps->log2_min_tb_size) {
1165 av_log(avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_inter out of range: %d\n",
1166 sps->max_transform_hierarchy_depth_inter);
1167 return AVERROR_INVALIDDATA;
1168 }
1169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if (sps->max_transform_hierarchy_depth_intra > sps->log2_ctb_size - sps->log2_min_tb_size) {
1170 av_log(avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_intra out of range: %d\n",
1171 sps->max_transform_hierarchy_depth_intra);
1172 return AVERROR_INVALIDDATA;
1173 }
1174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if (sps->log2_max_trafo_size > FFMIN(sps->log2_ctb_size, 5)) {
1175 av_log(avctx, AV_LOG_ERROR,
1176 "max transform block size out of range: %d\n",
1177 sps->log2_max_trafo_size);
1178 return AVERROR_INVALIDDATA;
1179 }
1180
1181
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1029 times.
1029 if (get_bits_left(gb) < 0) {
1182 av_log(avctx, AV_LOG_ERROR,
1183 "Overread SPS by %d bits\n", -get_bits_left(gb));
1184 return AVERROR_INVALIDDATA;
1185 }
1186
1187 1029 return 0;
1188 }
1189
1190 1029 int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx,
1191 HEVCParamSets *ps, int apply_defdispwin)
1192 {
1193 HEVCSPS *sps;
1194 1029 AVBufferRef *sps_buf = av_buffer_allocz(sizeof(*sps));
1195 unsigned int sps_id;
1196 int ret;
1197 ptrdiff_t nal_size;
1198
1199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if (!sps_buf)
1200 return AVERROR(ENOMEM);
1201 1029 sps = (HEVCSPS*)sps_buf->data;
1202
1203 1029 av_log(avctx, AV_LOG_DEBUG, "Decoding SPS\n");
1204
1205 1029 nal_size = gb->buffer_end - gb->buffer;
1206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if (nal_size > sizeof(sps->data)) {
1207 av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized SPS "
1208 "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
1209 nal_size, sizeof(sps->data));
1210 sps->data_size = sizeof(sps->data);
1211 } else {
1212 1029 sps->data_size = nal_size;
1213 }
1214 1029 memcpy(sps->data, gb->buffer, sps->data_size);
1215
1216 1029 ret = ff_hevc_parse_sps(sps, gb, &sps_id,
1217 apply_defdispwin,
1218 1029 ps->vps_list, avctx);
1219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if (ret < 0) {
1220 av_buffer_unref(&sps_buf);
1221 return ret;
1222 }
1223
1224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1029 times.
1029 if (avctx->debug & FF_DEBUG_BITSTREAM) {
1225 av_log(avctx, AV_LOG_DEBUG,
1226 "Parsed SPS: id %d; coded wxh: %dx%d; "
1227 "cropped wxh: %dx%d; pix_fmt: %s.\n",
1228 sps_id, sps->width, sps->height,
1229 sps->width - (sps->output_window.left_offset + sps->output_window.right_offset),
1230 sps->height - (sps->output_window.top_offset + sps->output_window.bottom_offset),
1231 av_get_pix_fmt_name(sps->pix_fmt));
1232 }
1233
1234 /* check if this is a repeat of an already parsed SPS, then keep the
1235 * original one.
1236 * otherwise drop all PPSes that depend on it */
1237
2/2
✓ Branch 0 taken 433 times.
✓ Branch 1 taken 596 times.
1029 if (ps->sps_list[sps_id] &&
1238
2/2
✓ Branch 0 taken 425 times.
✓ Branch 1 taken 8 times.
433 !memcmp(ps->sps_list[sps_id]->data, sps_buf->data, sps_buf->size)) {
1239 425 av_buffer_unref(&sps_buf);
1240 } else {
1241 604 remove_sps(ps, sps_id);
1242 604 ps->sps_list[sps_id] = sps_buf;
1243 }
1244
1245 1029 return 0;
1246 }
1247
1248 3250 static void hevc_pps_free(void *opaque, uint8_t *data)
1249 {
1250 3250 HEVCPPS *pps = (HEVCPPS*)data;
1251
1252 3250 av_freep(&pps->column_width);
1253 3250 av_freep(&pps->row_height);
1254 3250 av_freep(&pps->col_bd);
1255 3250 av_freep(&pps->row_bd);
1256 3250 av_freep(&pps->col_idxX);
1257 3250 av_freep(&pps->ctb_addr_rs_to_ts);
1258 3250 av_freep(&pps->ctb_addr_ts_to_rs);
1259 3250 av_freep(&pps->tile_pos_rs);
1260 3250 av_freep(&pps->tile_id);
1261 3250 av_freep(&pps->min_tb_addr_zs_tab);
1262
1263 3250 av_freep(&pps);
1264 3250 }
1265
1266 76 static int pps_range_extensions(GetBitContext *gb, AVCodecContext *avctx,
1267 HEVCPPS *pps, HEVCSPS *sps) {
1268 int i;
1269
1270
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 if (pps->transform_skip_enabled_flag) {
1271 76 pps->log2_max_transform_skip_block_size = get_ue_golomb_long(gb) + 2;
1272 }
1273 76 pps->cross_component_prediction_enabled_flag = get_bits1(gb);
1274 76 pps->chroma_qp_offset_list_enabled_flag = get_bits1(gb);
1275
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 66 times.
76 if (pps->chroma_qp_offset_list_enabled_flag) {
1276 10 pps->diff_cu_chroma_qp_offset_depth = get_ue_golomb_long(gb);
1277 10 pps->chroma_qp_offset_list_len_minus1 = get_ue_golomb_long(gb);
1278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (pps->chroma_qp_offset_list_len_minus1 > 5) {
1279 av_log(avctx, AV_LOG_ERROR,
1280 "chroma_qp_offset_list_len_minus1 shall be in the range [0, 5].\n");
1281 return AVERROR_INVALIDDATA;
1282 }
1283
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 for (i = 0; i <= pps->chroma_qp_offset_list_len_minus1; i++) {
1284 10 pps->cb_qp_offset_list[i] = get_se_golomb_long(gb);
1285
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (pps->cb_qp_offset_list[i]) {
1286 10 av_log(avctx, AV_LOG_WARNING,
1287 "cb_qp_offset_list not tested yet.\n");
1288 }
1289 10 pps->cr_qp_offset_list[i] = get_se_golomb_long(gb);
1290
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (pps->cr_qp_offset_list[i]) {
1291 10 av_log(avctx, AV_LOG_WARNING,
1292 "cb_qp_offset_list not tested yet.\n");
1293 }
1294 }
1295 }
1296 76 pps->log2_sao_offset_scale_luma = get_ue_golomb_long(gb);
1297 76 pps->log2_sao_offset_scale_chroma = get_ue_golomb_long(gb);
1298
1299
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 if ( pps->log2_sao_offset_scale_luma > FFMAX(sps->bit_depth - 10, 0)
1300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 || pps->log2_sao_offset_scale_chroma > FFMAX(sps->bit_depth_chroma - 10, 0)
1301 )
1302 return AVERROR_INVALIDDATA;
1303
1304 76 return(0);
1305 }
1306
1307 3246 static inline int setup_pps(AVCodecContext *avctx, GetBitContext *gb,
1308 HEVCPPS *pps, HEVCSPS *sps)
1309 {
1310 int log2_diff;
1311 int pic_area_in_ctbs;
1312 int i, j, x, y, ctb_addr_rs, tile_id;
1313
1314 // Inferred parameters
1315 3246 pps->col_bd = av_malloc_array(pps->num_tile_columns + 1, sizeof(*pps->col_bd));
1316 3246 pps->row_bd = av_malloc_array(pps->num_tile_rows + 1, sizeof(*pps->row_bd));
1317 3246 pps->col_idxX = av_malloc_array(sps->ctb_width, sizeof(*pps->col_idxX));
1318
3/6
✓ Branch 0 taken 3246 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3246 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3246 times.
3246 if (!pps->col_bd || !pps->row_bd || !pps->col_idxX)
1319 return AVERROR(ENOMEM);
1320
1321
2/2
✓ Branch 0 taken 2714 times.
✓ Branch 1 taken 532 times.
3246 if (pps->uniform_spacing_flag) {
1322
2/2
✓ Branch 0 taken 2338 times.
✓ Branch 1 taken 376 times.
2714 if (!pps->column_width) {
1323 2338 pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1324 2338 pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1325 }
1326
2/4
✓ Branch 0 taken 2714 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2714 times.
2714 if (!pps->column_width || !pps->row_height)
1327 return AVERROR(ENOMEM);
1328
1329
2/2
✓ Branch 0 taken 2981 times.
✓ Branch 1 taken 2714 times.
5695 for (i = 0; i < pps->num_tile_columns; i++) {
1330 2981 pps->column_width[i] = ((i + 1) * sps->ctb_width) / pps->num_tile_columns -
1331 2981 (i * sps->ctb_width) / pps->num_tile_columns;
1332 }
1333
1334
2/2
✓ Branch 0 taken 3012 times.
✓ Branch 1 taken 2714 times.
5726 for (i = 0; i < pps->num_tile_rows; i++) {
1335 3012 pps->row_height[i] = ((i + 1) * sps->ctb_height) / pps->num_tile_rows -
1336 3012 (i * sps->ctb_height) / pps->num_tile_rows;
1337 }
1338 }
1339
1340 3246 pps->col_bd[0] = 0;
1341
2/2
✓ Branch 0 taken 5589 times.
✓ Branch 1 taken 3246 times.
8835 for (i = 0; i < pps->num_tile_columns; i++)
1342 5589 pps->col_bd[i + 1] = pps->col_bd[i] + pps->column_width[i];
1343
1344 3246 pps->row_bd[0] = 0;
1345
2/2
✓ Branch 0 taken 5606 times.
✓ Branch 1 taken 3246 times.
8852 for (i = 0; i < pps->num_tile_rows; i++)
1346 5606 pps->row_bd[i + 1] = pps->row_bd[i] + pps->row_height[i];
1347
1348
2/2
✓ Branch 0 taken 56943 times.
✓ Branch 1 taken 3246 times.
60189 for (i = 0, j = 0; i < sps->ctb_width; i++) {
1349
2/2
✓ Branch 0 taken 5575 times.
✓ Branch 1 taken 51368 times.
56943 if (i > pps->col_bd[j])
1350 5575 j++;
1351 56943 pps->col_idxX[i] = j;
1352 }
1353
1354 /**
1355 * 6.5
1356 */
1357 3246 pic_area_in_ctbs = sps->ctb_width * sps->ctb_height;
1358
1359 3246 pps->ctb_addr_rs_to_ts = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_rs_to_ts));
1360 3246 pps->ctb_addr_ts_to_rs = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->ctb_addr_ts_to_rs));
1361 3246 pps->tile_id = av_malloc_array(pic_area_in_ctbs, sizeof(*pps->tile_id));
1362 3246 pps->min_tb_addr_zs_tab = av_malloc_array((sps->tb_mask+2) * (sps->tb_mask+2), sizeof(*pps->min_tb_addr_zs_tab));
1363
2/4
✓ Branch 0 taken 3246 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3246 times.
✗ Branch 3 not taken.
3246 if (!pps->ctb_addr_rs_to_ts || !pps->ctb_addr_ts_to_rs ||
1364
2/4
✓ Branch 0 taken 3246 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3246 times.
3246 !pps->tile_id || !pps->min_tb_addr_zs_tab) {
1365 return AVERROR(ENOMEM);
1366 }
1367
1368
2/2
✓ Branch 0 taken 827007 times.
✓ Branch 1 taken 3246 times.
830253 for (ctb_addr_rs = 0; ctb_addr_rs < pic_area_in_ctbs; ctb_addr_rs++) {
1369 827007 int tb_x = ctb_addr_rs % sps->ctb_width;
1370 827007 int tb_y = ctb_addr_rs / sps->ctb_width;
1371 827007 int tile_x = 0;
1372 827007 int tile_y = 0;
1373 827007 int val = 0;
1374
1375
1/2
✓ Branch 0 taken 1397741 times.
✗ Branch 1 not taken.
1397741 for (i = 0; i < pps->num_tile_columns; i++) {
1376
2/2
✓ Branch 0 taken 827007 times.
✓ Branch 1 taken 570734 times.
1397741 if (tb_x < pps->col_bd[i + 1]) {
1377 827007 tile_x = i;
1378 827007 break;
1379 }
1380 }
1381
1382
1/2
✓ Branch 0 taken 1246473 times.
✗ Branch 1 not taken.
1246473 for (i = 0; i < pps->num_tile_rows; i++) {
1383
2/2
✓ Branch 0 taken 827007 times.
✓ Branch 1 taken 419466 times.
1246473 if (tb_y < pps->row_bd[i + 1]) {
1384 827007 tile_y = i;
1385 827007 break;
1386 }
1387 }
1388
1389
2/2
✓ Branch 0 taken 570734 times.
✓ Branch 1 taken 827007 times.
1397741 for (i = 0; i < tile_x; i++)
1390 570734 val += pps->row_height[tile_y] * pps->column_width[i];
1391
2/2
✓ Branch 0 taken 419466 times.
✓ Branch 1 taken 827007 times.
1246473 for (i = 0; i < tile_y; i++)
1392 419466 val += sps->ctb_width * pps->row_height[i];
1393
1394 827007 val += (tb_y - pps->row_bd[tile_y]) * pps->column_width[tile_x] +
1395 827007 tb_x - pps->col_bd[tile_x];
1396
1397 827007 pps->ctb_addr_rs_to_ts[ctb_addr_rs] = val;
1398 827007 pps->ctb_addr_ts_to_rs[val] = ctb_addr_rs;
1399 }
1400
1401
2/2
✓ Branch 0 taken 5606 times.
✓ Branch 1 taken 3246 times.
8852 for (j = 0, tile_id = 0; j < pps->num_tile_rows; j++)
1402
2/2
✓ Branch 0 taken 16250 times.
✓ Branch 1 taken 5606 times.
21856 for (i = 0; i < pps->num_tile_columns; i++, tile_id++)
1403
2/2
✓ Branch 0 taken 78707 times.
✓ Branch 1 taken 16250 times.
94957 for (y = pps->row_bd[j]; y < pps->row_bd[j + 1]; y++)
1404
2/2
✓ Branch 0 taken 827007 times.
✓ Branch 1 taken 78707 times.
905714 for (x = pps->col_bd[i]; x < pps->col_bd[i + 1]; x++)
1405 827007 pps->tile_id[pps->ctb_addr_rs_to_ts[y * sps->ctb_width + x]] = tile_id;
1406
1407 3246 pps->tile_pos_rs = av_malloc_array(tile_id, sizeof(*pps->tile_pos_rs));
1408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3246 times.
3246 if (!pps->tile_pos_rs)
1409 return AVERROR(ENOMEM);
1410
1411
2/2
✓ Branch 0 taken 5606 times.
✓ Branch 1 taken 3246 times.
8852 for (j = 0; j < pps->num_tile_rows; j++)
1412
2/2
✓ Branch 0 taken 16250 times.
✓ Branch 1 taken 5606 times.
21856 for (i = 0; i < pps->num_tile_columns; i++)
1413 16250 pps->tile_pos_rs[j * pps->num_tile_columns + i] =
1414 16250 pps->row_bd[j] * sps->ctb_width + pps->col_bd[i];
1415
1416 3246 log2_diff = sps->log2_ctb_size - sps->log2_min_tb_size;
1417 3246 pps->min_tb_addr_zs = &pps->min_tb_addr_zs_tab[1*(sps->tb_mask+2)+1];
1418
2/2
✓ Branch 0 taken 53870 times.
✓ Branch 1 taken 3246 times.
57116 for (y = 0; y < sps->tb_mask+2; y++) {
1419 53870 pps->min_tb_addr_zs_tab[y*(sps->tb_mask+2)] = -1;
1420 53870 pps->min_tb_addr_zs_tab[y] = -1;
1421 }
1422
2/2
✓ Branch 0 taken 50624 times.
✓ Branch 1 taken 3246 times.
53870 for (y = 0; y < sps->tb_mask+1; y++) {
1423
2/2
✓ Branch 0 taken 801792 times.
✓ Branch 1 taken 50624 times.
852416 for (x = 0; x < sps->tb_mask+1; x++) {
1424 801792 int tb_x = x >> log2_diff;
1425 801792 int tb_y = y >> log2_diff;
1426 801792 int rs = sps->ctb_width * tb_y + tb_x;
1427 801792 int val = pps->ctb_addr_rs_to_ts[rs] << (log2_diff * 2);
1428
2/2
✓ Branch 0 taken 3199744 times.
✓ Branch 1 taken 801792 times.
4001536 for (i = 0; i < log2_diff; i++) {
1429 3199744 int m = 1 << i;
1430
4/4
✓ Branch 0 taken 1599872 times.
✓ Branch 1 taken 1599872 times.
✓ Branch 2 taken 1599872 times.
✓ Branch 3 taken 1599872 times.
3199744 val += (m & x ? m * m : 0) + (m & y ? 2 * m * m : 0);
1431 }
1432 801792 pps->min_tb_addr_zs[y * (sps->tb_mask+2) + x] = val;
1433 }
1434 }
1435
1436 3246 return 0;
1437 }
1438
1439 3250 int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx,
1440 HEVCParamSets *ps)
1441 {
1442 3250 HEVCSPS *sps = NULL;
1443 3250 int i, ret = 0;
1444 3250 unsigned int pps_id = 0;
1445 ptrdiff_t nal_size;
1446 unsigned log2_parallel_merge_level_minus2;
1447
1448 AVBufferRef *pps_buf;
1449 3250 HEVCPPS *pps = av_mallocz(sizeof(*pps));
1450
1451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3250 times.
3250 if (!pps)
1452 return AVERROR(ENOMEM);
1453
1454 3250 pps_buf = av_buffer_create((uint8_t *)pps, sizeof(*pps),
1455 hevc_pps_free, NULL, 0);
1456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3250 times.
3250 if (!pps_buf) {
1457 av_freep(&pps);
1458 return AVERROR(ENOMEM);
1459 }
1460
1461 3250 av_log(avctx, AV_LOG_DEBUG, "Decoding PPS\n");
1462
1463 3250 nal_size = gb->buffer_end - gb->buffer;
1464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3250 times.
3250 if (nal_size > sizeof(pps->data)) {
1465 av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized PPS "
1466 "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
1467 nal_size, sizeof(pps->data));
1468 pps->data_size = sizeof(pps->data);
1469 } else {
1470 3250 pps->data_size = nal_size;
1471 }
1472 3250 memcpy(pps->data, gb->buffer, pps->data_size);
1473
1474 // Default values
1475 3250 pps->loop_filter_across_tiles_enabled_flag = 1;
1476 3250 pps->num_tile_columns = 1;
1477 3250 pps->num_tile_rows = 1;
1478 3250 pps->uniform_spacing_flag = 1;
1479 3250 pps->disable_dbf = 0;
1480 3250 pps->beta_offset = 0;
1481 3250 pps->tc_offset = 0;
1482 3250 pps->log2_max_transform_skip_block_size = 2;
1483
1484 // Coded parameters
1485 3250 pps_id = get_ue_golomb_long(gb);
1486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3250 times.
3250 if (pps_id >= HEVC_MAX_PPS_COUNT) {
1487 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
1488 ret = AVERROR_INVALIDDATA;
1489 goto err;
1490 }
1491 3250 pps->sps_id = get_ue_golomb_long(gb);
1492
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3250 times.
3250 if (pps->sps_id >= HEVC_MAX_SPS_COUNT) {
1493 av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", pps->sps_id);
1494 ret = AVERROR_INVALIDDATA;
1495 goto err;
1496 }
1497
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3246 times.
3250 if (!ps->sps_list[pps->sps_id]) {
1498 4 av_log(avctx, AV_LOG_ERROR, "SPS %u does not exist.\n", pps->sps_id);
1499 4 ret = AVERROR_INVALIDDATA;
1500 4 goto err;
1501 }
1502 3246 sps = (HEVCSPS *)ps->sps_list[pps->sps_id]->data;
1503
1504 3246 pps->dependent_slice_segments_enabled_flag = get_bits1(gb);
1505 3246 pps->output_flag_present_flag = get_bits1(gb);
1506 3246 pps->num_extra_slice_header_bits = get_bits(gb, 3);
1507
1508 3246 pps->sign_data_hiding_flag = get_bits1(gb);
1509
1510 3246 pps->cabac_init_present_flag = get_bits1(gb);
1511
1512 3246 pps->num_ref_idx_l0_default_active = get_ue_golomb_long(gb) + 1;
1513 3246 pps->num_ref_idx_l1_default_active = get_ue_golomb_long(gb) + 1;
1514
1515 3246 pps->pic_init_qp_minus26 = get_se_golomb(gb);
1516
1517 3246 pps->constrained_intra_pred_flag = get_bits1(gb);
1518 3246 pps->transform_skip_enabled_flag = get_bits1(gb);
1519
1520 3246 pps->cu_qp_delta_enabled_flag = get_bits1(gb);
1521 3246 pps->diff_cu_qp_delta_depth = 0;
1522
2/2
✓ Branch 0 taken 527 times.
✓ Branch 1 taken 2719 times.
3246 if (pps->cu_qp_delta_enabled_flag)
1523 527 pps->diff_cu_qp_delta_depth = get_ue_golomb_long(gb);
1524
1525
1/2
✓ Branch 0 taken 3246 times.
✗ Branch 1 not taken.
3246 if (pps->diff_cu_qp_delta_depth < 0 ||
1526
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3246 times.
3246 pps->diff_cu_qp_delta_depth > sps->log2_diff_max_min_coding_block_size) {
1527 av_log(avctx, AV_LOG_ERROR, "diff_cu_qp_delta_depth %d is invalid\n",
1528 pps->diff_cu_qp_delta_depth);
1529 ret = AVERROR_INVALIDDATA;
1530 goto err;
1531 }
1532
1533 3246 pps->cb_qp_offset = get_se_golomb(gb);
1534
2/4
✓ Branch 0 taken 3246 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3246 times.
3246 if (pps->cb_qp_offset < -12 || pps->cb_qp_offset > 12) {
1535 av_log(avctx, AV_LOG_ERROR, "pps_cb_qp_offset out of range: %d\n",
1536 pps->cb_qp_offset);
1537 ret = AVERROR_INVALIDDATA;
1538 goto err;
1539 }
1540 3246 pps->cr_qp_offset = get_se_golomb(gb);
1541
2/4
✓ Branch 0 taken 3246 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3246 times.
3246 if (pps->cr_qp_offset < -12 || pps->cr_qp_offset > 12) {
1542 av_log(avctx, AV_LOG_ERROR, "pps_cr_qp_offset out of range: %d\n",
1543 pps->cr_qp_offset);
1544 ret = AVERROR_INVALIDDATA;
1545 goto err;
1546 }
1547 3246 pps->pic_slice_level_chroma_qp_offsets_present_flag = get_bits1(gb);
1548
1549 3246 pps->weighted_pred_flag = get_bits1(gb);
1550 3246 pps->weighted_bipred_flag = get_bits1(gb);
1551
1552 3246 pps->transquant_bypass_enable_flag = get_bits1(gb);
1553 3246 pps->tiles_enabled_flag = get_bits1(gb);
1554 3246 pps->entropy_coding_sync_enabled_flag = get_bits1(gb);
1555
1556
2/2
✓ Branch 0 taken 908 times.
✓ Branch 1 taken 2338 times.
3246 if (pps->tiles_enabled_flag) {
1557 908 int num_tile_columns_minus1 = get_ue_golomb(gb);
1558 908 int num_tile_rows_minus1 = get_ue_golomb(gb);
1559
1560
1/2
✓ Branch 0 taken 908 times.
✗ Branch 1 not taken.
908 if (num_tile_columns_minus1 < 0 ||
1561
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 908 times.
908 num_tile_columns_minus1 >= sps->ctb_width) {
1562 av_log(avctx, AV_LOG_ERROR, "num_tile_columns_minus1 out of range: %d\n",
1563 num_tile_columns_minus1);
1564 ret = num_tile_columns_minus1 < 0 ? num_tile_columns_minus1 : AVERROR_INVALIDDATA;
1565 goto err;
1566 }
1567
1/2
✓ Branch 0 taken 908 times.
✗ Branch 1 not taken.
908 if (num_tile_rows_minus1 < 0 ||
1568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 908 times.
908 num_tile_rows_minus1 >= sps->ctb_height) {
1569 av_log(avctx, AV_LOG_ERROR, "num_tile_rows_minus1 out of range: %d\n",
1570 num_tile_rows_minus1);
1571 ret = num_tile_rows_minus1 < 0 ? num_tile_rows_minus1 : AVERROR_INVALIDDATA;
1572 goto err;
1573 }
1574 908 pps->num_tile_columns = num_tile_columns_minus1 + 1;
1575 908 pps->num_tile_rows = num_tile_rows_minus1 + 1;
1576
1577 908 pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
1578 908 pps->row_height = av_malloc_array(pps->num_tile_rows, sizeof(*pps->row_height));
1579
2/4
✓ Branch 0 taken 908 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 908 times.
908 if (!pps->column_width || !pps->row_height) {
1580 ret = AVERROR(ENOMEM);
1581 goto err;
1582 }
1583
1584 908 pps->uniform_spacing_flag = get_bits1(gb);
1585
2/2
✓ Branch 0 taken 532 times.
✓ Branch 1 taken 376 times.
908 if (!pps->uniform_spacing_flag) {
1586 532 uint64_t sum = 0;
1587
2/2
✓ Branch 0 taken 2076 times.
✓ Branch 1 taken 532 times.
2608 for (i = 0; i < pps->num_tile_columns - 1; i++) {
1588 2076 pps->column_width[i] = get_ue_golomb_long(gb) + 1;
1589 2076 sum += pps->column_width[i];
1590 }
1591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 532 times.
532 if (sum >= sps->ctb_width) {
1592 av_log(avctx, AV_LOG_ERROR, "Invalid tile widths.\n");
1593 ret = AVERROR_INVALIDDATA;
1594 goto err;
1595 }
1596 532 pps->column_width[pps->num_tile_columns - 1] = sps->ctb_width - sum;
1597
1598 532 sum = 0;
1599
2/2
✓ Branch 0 taken 2062 times.
✓ Branch 1 taken 532 times.
2594 for (i = 0; i < pps->num_tile_rows - 1; i++) {
1600 2062 pps->row_height[i] = get_ue_golomb_long(gb) + 1;
1601 2062 sum += pps->row_height[i];
1602 }
1603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 532 times.
532 if (sum >= sps->ctb_height) {
1604 av_log(avctx, AV_LOG_ERROR, "Invalid tile heights.\n");
1605 ret = AVERROR_INVALIDDATA;
1606 goto err;
1607 }
1608 532 pps->row_height[pps->num_tile_rows - 1] = sps->ctb_height - sum;
1609 }
1610 908 pps->loop_filter_across_tiles_enabled_flag = get_bits1(gb);
1611 }
1612
1613 3246 pps->seq_loop_filter_across_slices_enabled_flag = get_bits1(gb);
1614
1615 3246 pps->deblocking_filter_control_present_flag = get_bits1(gb);
1616
2/2
✓ Branch 0 taken 1029 times.
✓ Branch 1 taken 2217 times.
3246 if (pps->deblocking_filter_control_present_flag) {
1617 1029 pps->deblocking_filter_override_enabled_flag = get_bits1(gb);
1618 1029 pps->disable_dbf = get_bits1(gb);
1619
2/2
✓ Branch 0 taken 887 times.
✓ Branch 1 taken 142 times.
1029 if (!pps->disable_dbf) {
1620 887 int beta_offset_div2 = get_se_golomb(gb);
1621 887 int tc_offset_div2 = get_se_golomb(gb) ;
1622
2/4
✓ Branch 0 taken 887 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 887 times.
887 if (beta_offset_div2 < -6 || beta_offset_div2 > 6) {
1623 av_log(avctx, AV_LOG_ERROR, "pps_beta_offset_div2 out of range: %d\n",
1624 beta_offset_div2);
1625 ret = AVERROR_INVALIDDATA;
1626 goto err;
1627 }
1628
2/4
✓ Branch 0 taken 887 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 887 times.
887 if (tc_offset_div2 < -6 || tc_offset_div2 > 6) {
1629 av_log(avctx, AV_LOG_ERROR, "pps_tc_offset_div2 out of range: %d\n",
1630 tc_offset_div2);
1631 ret = AVERROR_INVALIDDATA;
1632 goto err;
1633 }
1634 887 pps->beta_offset = 2 * beta_offset_div2;
1635 887 pps->tc_offset = 2 * tc_offset_div2;
1636 }
1637 }
1638
1639 3246 pps->scaling_list_data_present_flag = get_bits1(gb);
1640
2/2
✓ Branch 0 taken 469 times.
✓ Branch 1 taken 2777 times.
3246 if (pps->scaling_list_data_present_flag) {
1641 469 set_default_scaling_list_data(&pps->scaling_list);
1642 469 ret = scaling_list_data(gb, avctx, &pps->scaling_list, sps);
1643
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 469 times.
469 if (ret < 0)
1644 goto err;
1645 }
1646 3246 pps->lists_modification_present_flag = get_bits1(gb);
1647 3246 log2_parallel_merge_level_minus2 = get_ue_golomb_long(gb);
1648
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3246 times.
3246 if (log2_parallel_merge_level_minus2 > sps->log2_ctb_size) {
1649 av_log(avctx, AV_LOG_ERROR, "log2_parallel_merge_level_minus2 out of range: %d\n",
1650 log2_parallel_merge_level_minus2);
1651 ret = AVERROR_INVALIDDATA;
1652 goto err;
1653 }
1654 3246 pps->log2_parallel_merge_level = log2_parallel_merge_level_minus2 + 2;
1655
1656 3246 pps->slice_header_extension_present_flag = get_bits1(gb);
1657
1658
2/2
✓ Branch 1 taken 91 times.
✓ Branch 2 taken 3155 times.
3246 if (get_bits1(gb)) { // pps_extension_present_flag
1659 91 pps->pps_range_extensions_flag = get_bits1(gb);
1660 91 skip_bits(gb, 7); // pps_extension_7bits
1661
3/4
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 76 times.
✗ Branch 3 not taken.
91 if (sps->ptl.general_ptl.profile_idc == FF_PROFILE_HEVC_REXT && pps->pps_range_extensions_flag) {
1662
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 76 times.
76 if ((ret = pps_range_extensions(gb, avctx, pps, sps)) < 0)
1663 goto err;
1664 }
1665 }
1666
1667 3246 ret = setup_pps(avctx, gb, pps, sps);
1668
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3246 times.
3246 if (ret < 0)
1669 goto err;
1670
1671
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3246 times.
3246 if (get_bits_left(gb) < 0) {
1672 av_log(avctx, AV_LOG_ERROR,
1673 "Overread PPS by %d bits\n", -get_bits_left(gb));
1674 goto err;
1675 }
1676
1677 3246 remove_pps(ps, pps_id);
1678 3246 ps->pps_list[pps_id] = pps_buf;
1679
1680 3246 return 0;
1681
1682 4 err:
1683 4 av_buffer_unref(&pps_buf);
1684 4 return ret;
1685 }
1686
1687 601 void ff_hevc_ps_uninit(HEVCParamSets *ps)
1688 {
1689 int i;
1690
1691
2/2
✓ Branch 0 taken 9616 times.
✓ Branch 1 taken 601 times.
10217 for (i = 0; i < FF_ARRAY_ELEMS(ps->vps_list); i++)
1692 9616 av_buffer_unref(&ps->vps_list[i]);
1693
2/2
✓ Branch 0 taken 9616 times.
✓ Branch 1 taken 601 times.
10217 for (i = 0; i < FF_ARRAY_ELEMS(ps->sps_list); i++)
1694 9616 av_buffer_unref(&ps->sps_list[i]);
1695
2/2
✓ Branch 0 taken 38464 times.
✓ Branch 1 taken 601 times.
39065 for (i = 0; i < FF_ARRAY_ELEMS(ps->pps_list); i++)
1696 38464 av_buffer_unref(&ps->pps_list[i]);
1697
1698 601 ps->sps = NULL;
1699 601 ps->pps = NULL;
1700 601 ps->vps = NULL;
1701 601 }
1702
1703 30326 int ff_hevc_compute_poc(const HEVCSPS *sps, int pocTid0, int poc_lsb, int nal_unit_type)
1704 {
1705 30326 int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
1706 30326 int prev_poc_lsb = pocTid0 % max_poc_lsb;
1707 30326 int prev_poc_msb = pocTid0 - prev_poc_lsb;
1708 int poc_msb;
1709
1710
4/4
✓ Branch 0 taken 10270 times.
✓ Branch 1 taken 20056 times.
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 10230 times.
30326 if (poc_lsb < prev_poc_lsb && prev_poc_lsb - poc_lsb >= max_poc_lsb / 2)
1711 40 poc_msb = prev_poc_msb + max_poc_lsb;
1712
4/4
✓ Branch 0 taken 14464 times.
✓ Branch 1 taken 15822 times.
✓ Branch 2 taken 302 times.
✓ Branch 3 taken 14162 times.
30286 else if (poc_lsb > prev_poc_lsb && poc_lsb - prev_poc_lsb > max_poc_lsb / 2)
1713 302 poc_msb = prev_poc_msb - max_poc_lsb;
1714 else
1715 29984 poc_msb = prev_poc_msb;
1716
1717 // For BLA picture types, POCmsb is set to 0.
1718
4/4
✓ Branch 0 taken 30311 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 30305 times.
✓ Branch 3 taken 6 times.
30326 if (nal_unit_type == HEVC_NAL_BLA_W_LP ||
1719
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 30302 times.
30305 nal_unit_type == HEVC_NAL_BLA_W_RADL ||
1720 nal_unit_type == HEVC_NAL_BLA_N_LP)
1721 24 poc_msb = 0;
1722
1723 30326 return poc_msb + poc_lsb;
1724 }
1725