FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vvc/ps.c
Date: 2024-07-16 12:46:59
Exec Total Coverage
Lines: 720 818 88.0%
Functions: 70 73 95.9%
Branches: 351 464 75.6%

Line Branch Exec Source
1 /*
2 * VVC parameter set parser
3 *
4 * Copyright (C) 2023 Nuo Mi
5 * Copyright (C) 2022 Xu Mu
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include "libavcodec/cbs_h266.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavcodec/refstruct.h"
28 #include "data.h"
29 #include "ps.h"
30 #include "dec.h"
31
32 56 static int sps_map_pixel_format(VVCSPS *sps, void *log_ctx)
33 {
34 56 const H266RawSPS *r = sps->r;
35 const AVPixFmtDescriptor *desc;
36
37
2/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
56 switch (sps->bit_depth) {
38 2 case 8:
39
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (r->sps_chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY8;
40
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (r->sps_chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P;
41
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (r->sps_chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P;
42
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (r->sps_chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P;
43 2 break;
44 54 case 10:
45
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 52 times.
54 if (r->sps_chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY10;
46
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 4 times.
54 if (r->sps_chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P10;
47
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (r->sps_chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P10;
48
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 52 times.
54 if (r->sps_chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P10;
49 54 break;
50 case 12:
51 if (r->sps_chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY12;
52 if (r->sps_chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P12;
53 if (r->sps_chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P12;
54 if (r->sps_chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P12;
55 break;
56 default:
57 av_log(log_ctx, AV_LOG_ERROR,
58 "The following bit-depths are currently specified: 8, 10, 12 bits, "
59 "chroma_format_idc is %d, depth is %d\n",
60 r->sps_chroma_format_idc, sps->bit_depth);
61 return AVERROR_INVALIDDATA;
62 }
63
64 56 desc = av_pix_fmt_desc_get(sps->pix_fmt);
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (!desc)
66 return AVERROR(EINVAL);
67
68 56 sps->hshift[0] = sps->vshift[0] = 0;
69 56 sps->hshift[2] = sps->hshift[1] = desc->log2_chroma_w;
70 56 sps->vshift[2] = sps->vshift[1] = desc->log2_chroma_h;
71
72 56 sps->pixel_shift = sps->bit_depth > 8;
73
74 56 return 0;
75 }
76
77 56 static int sps_bit_depth(VVCSPS *sps, void *log_ctx)
78 {
79 56 const H266RawSPS *r = sps->r;
80
81 56 sps->bit_depth = r->sps_bitdepth_minus8 + 8;
82 56 sps->qp_bd_offset = 6 * (sps->bit_depth - 8);
83 56 sps->log2_transform_range =
84
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
56 r->sps_extended_precision_flag ? FFMAX(15, FFMIN(20, sps->bit_depth + 6)) : 15;
85 56 return sps_map_pixel_format(sps, log_ctx);
86 }
87
88 54 static int sps_chroma_qp_table(VVCSPS *sps)
89 {
90 54 const H266RawSPS *r = sps->r;
91 108 const int num_qp_tables = r->sps_same_qp_table_for_chroma_flag ?
92
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
54 1 : (r->sps_joint_cbcr_enabled_flag ? 3 : 2);
93
94
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 54 times.
112 for (int i = 0; i < num_qp_tables; i++) {
95 int num_points_in_qp_table;
96 int8_t qp_in[VVC_MAX_POINTS_IN_QP_TABLE], qp_out[VVC_MAX_POINTS_IN_QP_TABLE];
97 unsigned int delta_qp_in[VVC_MAX_POINTS_IN_QP_TABLE];
98 58 int off = sps->qp_bd_offset;
99
100 58 num_points_in_qp_table = r->sps_num_points_in_qp_table_minus1[i] + 1;
101
102 58 qp_out[0] = qp_in[0] = r->sps_qp_table_start_minus26[i] + 26;
103
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 58 times.
236 for (int j = 0; j < num_points_in_qp_table; j++ ) {
104 178 const uint8_t delta_qp_out = (r->sps_delta_qp_in_val_minus1[i][j] ^ r->sps_delta_qp_diff_val[i][j]);
105 178 delta_qp_in[j] = r->sps_delta_qp_in_val_minus1[i][j] + 1;
106 // Note: we cannot check qp_{in,out}[j+1] here as qp_*[j] + delta_qp_*
107 // may not fit in an 8-bit signed integer.
108
2/4
✓ Branch 0 taken 178 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 178 times.
178 if (qp_in[j] + delta_qp_in[j] > 63 || qp_out[j] + delta_qp_out > 63)
109 return AVERROR(EINVAL);
110 178 qp_in[j+1] = qp_in[j] + delta_qp_in[j];
111 178 qp_out[j+1] = qp_out[j] + delta_qp_out;
112 }
113 58 sps->chroma_qp_table[i][qp_in[0] + off] = qp_out[0];
114
2/2
✓ Branch 0 taken 1618 times.
✓ Branch 1 taken 58 times.
1676 for (int k = qp_in[0] - 1 + off; k >= 0; k--)
115 1618 sps->chroma_qp_table[i][k] = av_clip(sps->chroma_qp_table[i][k+1]-1, -off, 63);
116
117
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 58 times.
236 for (int j = 0; j < num_points_in_qp_table; j++) {
118 178 int sh = delta_qp_in[j] >> 1;
119
2/2
✓ Branch 0 taken 1534 times.
✓ Branch 1 taken 178 times.
1712 for (int k = qp_in[j] + 1 + off, m = 1; k <= qp_in[j+1] + off; k++, m++) {
120 1534 sps->chroma_qp_table[i][k] = sps->chroma_qp_table[i][qp_in[j] + off] +
121 1534 ((qp_out[j+1] - qp_out[j]) * m + sh) / delta_qp_in[j];
122 }
123 }
124
2/2
✓ Branch 0 taken 1174 times.
✓ Branch 1 taken 58 times.
1232 for (int k = qp_in[num_points_in_qp_table] + 1 + off; k <= 63 + off; k++)
125 1174 sps->chroma_qp_table[i][k] = av_clip(sps->chroma_qp_table[i][k-1] + 1, -sps->qp_bd_offset, 63);
126 }
127
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 2 times.
54 if (r->sps_same_qp_table_for_chroma_flag) {
128 52 memcpy(&sps->chroma_qp_table[1], &sps->chroma_qp_table[0], sizeof(sps->chroma_qp_table[0]));
129 52 memcpy(&sps->chroma_qp_table[2], &sps->chroma_qp_table[0], sizeof(sps->chroma_qp_table[0]));
130 }
131
132 54 return 0;
133 }
134
135 56 static void sps_poc(VVCSPS *sps)
136 {
137 56 sps->max_pic_order_cnt_lsb = 1 << (sps->r->sps_log2_max_pic_order_cnt_lsb_minus4 + 4);
138 56 }
139
140 56 static void sps_inter(VVCSPS *sps)
141 {
142 56 const H266RawSPS *r = sps->r;
143
144 56 sps->max_num_merge_cand = 6 - r->sps_six_minus_max_num_merge_cand;
145 56 sps->max_num_ibc_merge_cand = 6 - r->sps_six_minus_max_num_ibc_merge_cand;
146
147
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 6 times.
56 if (sps->r->sps_gpm_enabled_flag) {
148 50 sps->max_num_gpm_merge_cand = 2;
149
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 if (sps->max_num_merge_cand >= 3)
150 50 sps->max_num_gpm_merge_cand = sps->max_num_merge_cand - r->sps_max_num_merge_cand_minus_max_num_gpm_cand;
151 }
152
153 56 sps->log2_parallel_merge_level = r->sps_log2_parallel_merge_level_minus2 + 2;
154 56 }
155
156 56 static void sps_partition_constraints(VVCSPS* sps)
157 {
158 56 const H266RawSPS *r = sps->r;
159
160 56 sps->ctb_log2_size_y = r->sps_log2_ctu_size_minus5 + 5;
161 56 sps->ctb_size_y = 1 << sps->ctb_log2_size_y;
162 56 sps->min_cb_log2_size_y = r->sps_log2_min_luma_coding_block_size_minus2 + 2;
163 56 sps->min_cb_size_y = 1 << sps->min_cb_log2_size_y;
164
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 2 times.
56 sps->max_tb_size_y = 1 << (r->sps_max_luma_transform_size_64_flag ? 6 : 5);
165 56 sps->max_ts_size = 1 << (r->sps_log2_transform_skip_max_size_minus2 + 2);
166 56 }
167
168 56 static void sps_ladf(VVCSPS* sps)
169 {
170 56 const H266RawSPS *r = sps->r;
171
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (r->sps_ladf_enabled_flag) {
173 sps->num_ladf_intervals = r->sps_num_ladf_intervals_minus2 + 2;
174 sps->ladf_interval_lower_bound[0] = 0;
175 for (int i = 0; i < sps->num_ladf_intervals - 1; i++) {
176 sps->ladf_interval_lower_bound[i + 1] =
177 sps->ladf_interval_lower_bound[i] + r->sps_ladf_delta_threshold_minus1[i] + 1;
178 }
179 }
180 56 }
181
182 56 static int sps_derive(VVCSPS *sps, void *log_ctx)
183 {
184 int ret;
185 56 const H266RawSPS *r = sps->r;
186
187 56 ret = sps_bit_depth(sps, log_ctx);
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (ret < 0)
189 return ret;
190 56 sps_poc(sps);
191 56 sps_inter(sps);
192 56 sps_partition_constraints(sps);
193 56 sps_ladf(sps);
194
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 2 times.
56 if (r->sps_chroma_format_idc != 0) {
195 54 ret = sps_chroma_qp_table(sps);
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (ret < 0)
197 return ret;
198 }
199
200 56 return 0;
201 }
202
203 56 static void sps_free(FFRefStructOpaque opaque, void *obj)
204 {
205 56 VVCSPS *sps = obj;
206 56 ff_refstruct_unref(&sps->r);
207 56 }
208
209 56 static const VVCSPS *sps_alloc(const H266RawSPS *rsps, void *log_ctx)
210 {
211 int ret;
212 56 VVCSPS *sps = ff_refstruct_alloc_ext(sizeof(*sps), 0, NULL, sps_free);
213
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (!sps)
215 return NULL;
216
217 56 ff_refstruct_replace(&sps->r, rsps);
218
219 56 ret = sps_derive(sps, log_ctx);
220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (ret < 0)
221 goto fail;
222
223 56 return sps;
224
225 fail:
226 ff_refstruct_unref(&sps);
227 return NULL;
228 }
229
230 877 static int decode_sps(VVCParamSets *ps, const H266RawSPS *rsps, void *log_ctx, int is_clvss)
231 {
232 877 const int sps_id = rsps->sps_seq_parameter_set_id;
233 877 const VVCSPS *old_sps = ps->sps_list[sps_id];
234 const VVCSPS *sps;
235
236
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 812 times.
877 if (is_clvss) {
237 65 ps->sps_id_used = 0;
238 }
239
240
2/2
✓ Branch 0 taken 824 times.
✓ Branch 1 taken 53 times.
877 if (old_sps) {
241
4/4
✓ Branch 0 taken 284 times.
✓ Branch 1 taken 540 times.
✓ Branch 2 taken 281 times.
✓ Branch 3 taken 3 times.
824 if (old_sps->r == rsps || !memcmp(old_sps->r, rsps, sizeof(*old_sps->r)))
242 821 return 0;
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 else if (ps->sps_id_used & (1 << sps_id))
244 return AVERROR_INVALIDDATA;
245 }
246
247 56 sps = sps_alloc(rsps, log_ctx);
248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (!sps)
249 return AVERROR(ENOMEM);
250
251 56 ff_refstruct_unref(&ps->sps_list[sps_id]);
252 56 ps->sps_list[sps_id] = sps;
253 56 ps->sps_id_used |= (1 << sps_id);
254
255 56 return 0;
256 }
257
258 135 static void pps_chroma_qp_offset(VVCPPS *pps)
259 {
260 135 pps->chroma_qp_offset[CB - 1] = pps->r->pps_cb_qp_offset;
261 135 pps->chroma_qp_offset[CR - 1] = pps->r->pps_cr_qp_offset;
262 135 pps->chroma_qp_offset[JCBCR - 1]= pps->r->pps_joint_cbcr_qp_offset_value;
263
2/2
✓ Branch 0 taken 810 times.
✓ Branch 1 taken 135 times.
945 for (int i = 0; i < 6; i++) {
264 810 pps->chroma_qp_offset_list[i][CB - 1] = pps->r->pps_cb_qp_offset_list[i];
265 810 pps->chroma_qp_offset_list[i][CR - 1] = pps->r->pps_cr_qp_offset_list[i];
266 810 pps->chroma_qp_offset_list[i][JCBCR - 1]= pps->r->pps_joint_cbcr_qp_offset_list[i];
267 }
268 135 }
269
270 135 static void pps_width_height(VVCPPS *pps, const VVCSPS *sps)
271 {
272 135 const H266RawPPS *r = pps->r;
273
274 135 pps->width = r->pps_pic_width_in_luma_samples;
275 135 pps->height = r->pps_pic_height_in_luma_samples;
276
277 135 pps->ctb_width = AV_CEIL_RSHIFT(pps->width, sps->ctb_log2_size_y);
278 135 pps->ctb_height = AV_CEIL_RSHIFT(pps->height, sps->ctb_log2_size_y);
279 135 pps->ctb_count = pps->ctb_width * pps->ctb_height;
280
281 135 pps->min_cb_width = pps->width >> sps->min_cb_log2_size_y;
282 135 pps->min_cb_height = pps->height >> sps->min_cb_log2_size_y;
283
284 135 pps->min_pu_width = pps->width >> MIN_PU_LOG2;
285 135 pps->min_pu_height = pps->height >> MIN_PU_LOG2;
286 135 pps->min_tu_width = pps->width >> MIN_TU_LOG2;
287 135 pps->min_tu_height = pps->height >> MIN_TU_LOG2;
288
289 135 pps->width32 = AV_CEIL_RSHIFT(pps->width, 5);
290 135 pps->height32 = AV_CEIL_RSHIFT(pps->height, 5);
291 135 pps->width64 = AV_CEIL_RSHIFT(pps->width, 6);
292 135 pps->height64 = AV_CEIL_RSHIFT(pps->height, 6);
293 135 }
294
295 135 static int pps_bd(VVCPPS *pps)
296 {
297 135 const H266RawPPS *r = pps->r;
298
299 135 pps->col_bd = av_calloc(r->num_tile_columns + 1, sizeof(*pps->col_bd));
300 135 pps->row_bd = av_calloc(r->num_tile_rows + 1, sizeof(*pps->row_bd));
301 135 pps->ctb_to_col_bd = av_calloc(pps->ctb_width + 1, sizeof(*pps->ctb_to_col_bd));
302 135 pps->ctb_to_row_bd = av_calloc(pps->ctb_height + 1, sizeof(*pps->ctb_to_col_bd));
303
4/8
✓ Branch 0 taken 135 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 135 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 135 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 135 times.
135 if (!pps->col_bd || !pps->row_bd || !pps->ctb_to_col_bd || !pps->ctb_to_row_bd)
304 return AVERROR(ENOMEM);
305
306
2/2
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 135 times.
311 for (int i = 0, j = 0; i < r->num_tile_columns; i++) {
307 176 pps->col_bd[i] = j;
308 176 j += r->col_width_val[i];
309
2/2
✓ Branch 0 taken 888 times.
✓ Branch 1 taken 176 times.
1064 for (int k = pps->col_bd[i]; k < j; k++)
310 888 pps->ctb_to_col_bd[k] = pps->col_bd[i];
311 }
312 135 pps->col_bd[r->num_tile_columns] = pps->ctb_to_col_bd[pps->ctb_width] = pps->ctb_width;
313
314
2/2
✓ Branch 0 taken 167 times.
✓ Branch 1 taken 135 times.
302 for (int i = 0, j = 0; i < r->num_tile_rows; i++) {
315 167 pps->row_bd[i] = j;
316 167 j += r->row_height_val[i];
317
2/2
✓ Branch 0 taken 491 times.
✓ Branch 1 taken 167 times.
658 for (int k = pps->row_bd[i]; k < j; k++)
318 491 pps->ctb_to_row_bd[k] = pps->row_bd[i];
319 }
320 135 pps->row_bd[r->num_tile_rows] = pps->ctb_to_row_bd[pps->ctb_height] = pps->ctb_height;
321
322 135 return 0;
323 }
324
325
326 78 static int next_tile_idx(int tile_idx, const int i, const H266RawPPS *r)
327 {
328
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 23 times.
78 if (r->pps_tile_idx_delta_present_flag) {
329 55 tile_idx += r->pps_tile_idx_delta_val[i];
330 } else {
331 23 tile_idx += r->pps_slice_width_in_tiles_minus1[i] + 1;
332
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 16 times.
23 if (tile_idx % r->num_tile_columns == 0)
333 7 tile_idx += (r->pps_slice_height_in_tiles_minus1[i]) * r->num_tile_columns;
334 }
335 78 return tile_idx;
336 }
337
338 78 static void tile_xy(int *tile_x, int *tile_y, const int tile_idx, const VVCPPS *pps)
339 {
340 78 *tile_x = tile_idx % pps->r->num_tile_columns;
341 78 *tile_y = tile_idx / pps->r->num_tile_columns;
342 78 }
343
344 308 static void ctu_xy(int *rx, int *ry, const int tile_x, const int tile_y, const VVCPPS *pps)
345 {
346 308 *rx = pps->col_bd[tile_x];
347 308 *ry = pps->row_bd[tile_y];
348 308 }
349
350 4849 static int ctu_rs(const int rx, const int ry, const VVCPPS *pps)
351 {
352 4849 return pps->ctb_width * ry + rx;
353 }
354
355 354 static int pps_add_ctus(VVCPPS *pps, int *off, const int rx, const int ry,
356 const int w, const int h)
357 {
358 354 int start = *off;
359
2/2
✓ Branch 0 taken 818 times.
✓ Branch 1 taken 354 times.
1172 for (int y = 0; y < h; y++) {
360
2/2
✓ Branch 0 taken 4849 times.
✓ Branch 1 taken 818 times.
5667 for (int x = 0; x < w; x++) {
361 4849 pps->ctb_addr_in_slice[*off] = ctu_rs(rx + x, ry + y, pps);
362 4849 (*off)++;
363 }
364 }
365 354 return *off - start;
366 }
367
368 static void pps_single_slice_picture(VVCPPS *pps, int *off)
369 {
370 for (int j = 0; j < pps->r->num_tile_rows; j++) {
371 for (int i = 0; i < pps->r->num_tile_columns; i++) {
372 pps->num_ctus_in_slice[0] = pps_add_ctus(pps, off,
373 pps->col_bd[i], pps->row_bd[j],
374 pps->r->col_width_val[i], pps->r->row_height_val[j]);
375 }
376 }
377 }
378
379 16 static void subpic_tiles(int *tile_x, int *tile_y, int *tile_x_end, int *tile_y_end,
380 const VVCSPS *sps, const VVCPPS *pps, const int i)
381 {
382 16 const int rx = sps->r->sps_subpic_ctu_top_left_x[i];
383 16 const int ry = sps->r->sps_subpic_ctu_top_left_y[i];
384
385 16 *tile_x = *tile_y = 0;
386
387
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 16 times.
40 while (pps->col_bd[*tile_x] != rx)
388 24 (*tile_x)++;
389
390
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 16 times.
24 while (pps->row_bd[*tile_y] != ry)
391 8 (*tile_y)++;
392
393 16 *tile_x_end = (*tile_x);
394 16 *tile_y_end = (*tile_y);
395
396
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
32 while (pps->col_bd[*tile_x_end] < rx + sps->r->sps_subpic_width_minus1[i] + 1)
397 16 (*tile_x_end)++;
398
399
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
32 while (pps->row_bd[*tile_y_end] < ry + sps->r->sps_subpic_height_minus1[i] + 1)
400 16 (*tile_y_end)++;
401 16 }
402
403 static void pps_subpic_less_than_one_tile_slice(VVCPPS *pps, const VVCSPS *sps, const int i, const int tx, const int ty, int *off)
404 {
405 pps->num_ctus_in_slice[i] = pps_add_ctus(pps, off,
406 pps->col_bd[tx], pps->row_bd[ty],
407 pps->r->col_width_val[tx], sps->r->sps_subpic_height_minus1[i] + 1);
408 }
409
410 16 static void pps_subpic_one_or_more_tiles_slice(VVCPPS *pps, const int tile_x, const int tile_y, const int x_end, const int y_end, const int i, int *off)
411 {
412
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
32 for (int ty = tile_y; ty < y_end; ty++) {
413
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
32 for (int tx = tile_x; tx < x_end; tx++) {
414 16 pps->num_ctus_in_slice[i] += pps_add_ctus(pps, off,
415 16 pps->col_bd[tx], pps->row_bd[ty],
416 16 pps->r->col_width_val[tx], pps->r->row_height_val[ty]);
417 }
418 }
419 16 }
420
421 16 static void pps_subpic_slice(VVCPPS *pps, const VVCSPS *sps, const int i, int *off)
422 {
423 int tx, ty, x_end, y_end;
424
425 16 pps->slice_start_offset[i] = *off;
426 16 pps->num_ctus_in_slice[i] = 0;
427
428 16 subpic_tiles(&tx, &ty, &x_end, &y_end, sps, pps, i);
429
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
16 if (ty + 1 == y_end && sps->r->sps_subpic_height_minus1[i] + 1 < pps->r->row_height_val[ty])
430 pps_subpic_less_than_one_tile_slice(pps, sps, i, tx, ty, off);
431 else
432 16 pps_subpic_one_or_more_tiles_slice(pps, tx, ty, x_end, y_end, i, off);
433 16 }
434
435 2 static void pps_single_slice_per_subpic(VVCPPS *pps, const VVCSPS *sps, int *off)
436 {
437
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!sps->r->sps_subpic_info_present_flag) {
438 pps_single_slice_picture(pps, off);
439 } else {
440
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
18 for (int i = 0; i < pps->r->pps_num_slices_in_pic_minus1 + 1; i++)
441 16 pps_subpic_slice(pps, sps, i, off);
442 }
443 2 }
444
445 61 static int pps_one_tile_slices(VVCPPS *pps, const int tile_idx, int i, int *off)
446 {
447 61 const H266RawPPS *r = pps->r;
448 int rx, ry, ctu_y_end, tile_x, tile_y;
449
450 61 tile_xy(&tile_x, &tile_y, tile_idx, pps);
451 61 ctu_xy(&rx, &ry, tile_x, tile_y, pps);
452 61 ctu_y_end = ry + r->row_height_val[tile_y];
453
2/2
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 61 times.
152 while (ry < ctu_y_end) {
454 91 pps->slice_start_offset[i] = *off;
455 182 pps->num_ctus_in_slice[i] = pps_add_ctus(pps, off, rx, ry,
456 91 r->col_width_val[tile_x], r->slice_height_in_ctus[i]);
457 91 ry += r->slice_height_in_ctus[i++];
458 }
459 61 i--;
460 61 return i;
461 }
462
463 17 static void pps_multi_tiles_slice(VVCPPS *pps, const int tile_idx, const int i, int *off)
464 {
465 17 const H266RawPPS *r = pps->r;
466 int rx, ry, tile_x, tile_y;
467
468 17 tile_xy(&tile_x, &tile_y, tile_idx, pps);
469 17 pps->slice_start_offset[i] = *off;
470 17 pps->num_ctus_in_slice[i] = 0;
471
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 17 times.
48 for (int ty = tile_y; ty <= tile_y + r->pps_slice_height_in_tiles_minus1[i]; ty++) {
472
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 31 times.
106 for (int tx = tile_x; tx <= tile_x + r->pps_slice_width_in_tiles_minus1[i]; tx++) {
473 75 ctu_xy(&rx, &ry, tx, ty, pps);
474 75 pps->num_ctus_in_slice[i] += pps_add_ctus(pps, off, rx, ry,
475 75 r->col_width_val[tx], r->row_height_val[ty]);
476 }
477 }
478 17 }
479
480 11 static void pps_rect_slice(VVCPPS *pps, const VVCSPS *sps)
481 {
482 11 const H266RawPPS *r = pps->r;
483 11 int tile_idx = 0, off = 0;
484
485
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 9 times.
11 if (r->pps_single_slice_per_subpic_flag) {
486 2 pps_single_slice_per_subpic(pps, sps, &off);
487 2 return;
488 }
489
490
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 9 times.
87 for (int i = 0; i < r->pps_num_slices_in_pic_minus1 + 1; i++) {
491
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 12 times.
78 if (!r->pps_slice_width_in_tiles_minus1[i] &&
492
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 5 times.
66 !r->pps_slice_height_in_tiles_minus1[i]) {
493 61 i = pps_one_tile_slices(pps, tile_idx, i, &off);
494 } else {
495 17 pps_multi_tiles_slice(pps, tile_idx, i, &off);
496 }
497 78 tile_idx = next_tile_idx(tile_idx, i, r);
498 }
499 }
500
501 124 static void pps_no_rect_slice(VVCPPS* pps)
502 {
503 124 const H266RawPPS* r = pps->r;
504 124 int rx, ry, off = 0;
505
506
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 124 times.
256 for (int tile_y = 0; tile_y < r->num_tile_rows; tile_y++) {
507
2/2
✓ Branch 0 taken 172 times.
✓ Branch 1 taken 132 times.
304 for (int tile_x = 0; tile_x < r->num_tile_columns; tile_x++) {
508 172 ctu_xy(&rx, &ry, tile_x, tile_y, pps);
509 172 pps_add_ctus(pps, &off, rx, ry, r->col_width_val[tile_x], r->row_height_val[tile_y]);
510 }
511 }
512 124 }
513
514 135 static int pps_slice_map(VVCPPS *pps, const VVCSPS *sps)
515 {
516 135 pps->ctb_addr_in_slice = av_calloc(pps->ctb_count, sizeof(*pps->ctb_addr_in_slice));
517
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 135 times.
135 if (!pps->ctb_addr_in_slice)
518 return AVERROR(ENOMEM);
519
520
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 124 times.
135 if (pps->r->pps_rect_slice_flag)
521 11 pps_rect_slice(pps, sps);
522 else
523 124 pps_no_rect_slice(pps);
524
525 135 return 0;
526 }
527
528 135 static void pps_ref_wraparound_offset(VVCPPS *pps, const VVCSPS *sps)
529 {
530 135 const H266RawPPS *r = pps->r;
531
532
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 133 times.
135 if (r->pps_ref_wraparound_enabled_flag)
533 2 pps->ref_wraparound_offset = (pps->width / sps->min_cb_size_y) - r->pps_pic_width_minus_wraparound_offset;
534 135 }
535
536 135 static void pps_subpic(VVCPPS *pps, const VVCSPS *sps)
537 {
538 135 const H266RawSPS *rsps = sps->r;
539
2/2
✓ Branch 0 taken 169 times.
✓ Branch 1 taken 135 times.
304 for (int i = 0; i < rsps->sps_num_subpics_minus1 + 1; i++) {
540
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 128 times.
169 if (rsps->sps_subpic_treated_as_pic_flag[i]) {
541 41 pps->subpic_x[i] = rsps->sps_subpic_ctu_top_left_x[i] << sps->ctb_log2_size_y;
542 41 pps->subpic_y[i] = rsps->sps_subpic_ctu_top_left_y[i] << sps->ctb_log2_size_y;
543 41 pps->subpic_width[i] = FFMIN(pps->width - pps->subpic_x[i], (rsps->sps_subpic_width_minus1[i] + 1) << sps->ctb_log2_size_y);
544 41 pps->subpic_height[i] = FFMIN(pps->height - pps->subpic_y[i], (rsps->sps_subpic_height_minus1[i] + 1) << sps->ctb_log2_size_y);
545 } else {
546 128 pps->subpic_x[i] = 0;
547 128 pps->subpic_y[i] = 0;
548 128 pps->subpic_width[i] = pps->width;
549 128 pps->subpic_height[i] = pps->height;
550 }
551 }
552 135 }
553
554 135 static int pps_derive(VVCPPS *pps, const VVCSPS *sps)
555 {
556 int ret;
557
558 135 pps_chroma_qp_offset(pps);
559 135 pps_width_height(pps, sps);
560
561 135 ret = pps_bd(pps);
562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 135 times.
135 if (ret < 0)
563 return ret;
564
565 135 ret = pps_slice_map(pps, sps);
566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 135 times.
135 if (ret < 0)
567 return ret;
568
569 135 pps_ref_wraparound_offset(pps, sps);
570 135 pps_subpic(pps, sps);
571
572 135 return 0;
573 }
574
575 135 static void pps_free(FFRefStructOpaque opaque, void *obj)
576 {
577 135 VVCPPS *pps = obj;
578
579 135 ff_refstruct_unref(&pps->r);
580
581 135 av_freep(&pps->col_bd);
582 135 av_freep(&pps->row_bd);
583 135 av_freep(&pps->ctb_to_col_bd);
584 135 av_freep(&pps->ctb_to_row_bd);
585 135 av_freep(&pps->ctb_addr_in_slice);
586 135 }
587
588 135 static const VVCPPS *pps_alloc(const H266RawPPS *rpps, const VVCSPS *sps)
589 {
590 int ret;
591 135 VVCPPS *pps = ff_refstruct_alloc_ext(sizeof(*pps), 0, NULL, pps_free);
592
593
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 135 times.
135 if (!pps)
594 return NULL;
595
596 135 ff_refstruct_replace(&pps->r, rpps);
597
598 135 ret = pps_derive(pps, sps);
599
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 135 times.
135 if (ret < 0)
600 goto fail;
601
602 135 return pps;
603
604 fail:
605 ff_refstruct_unref(&pps);
606 return NULL;
607 }
608
609 877 static int decode_pps(VVCParamSets *ps, const H266RawPPS *rpps)
610 {
611 877 int ret = 0;
612 877 const int pps_id = rpps->pps_pic_parameter_set_id;
613 877 const int sps_id = rpps->pps_seq_parameter_set_id;
614 877 const VVCPPS *old_pps = ps->pps_list[pps_id];
615 const VVCPPS *pps;
616
617
4/4
✓ Branch 0 taken 819 times.
✓ Branch 1 taken 58 times.
✓ Branch 2 taken 742 times.
✓ Branch 3 taken 77 times.
877 if (old_pps && old_pps->r == rpps)
618 742 return 0;
619
620 135 pps = pps_alloc(rpps, ps->sps_list[sps_id]);
621
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 135 times.
135 if (!pps)
622 return AVERROR(ENOMEM);
623
624 135 ff_refstruct_unref(&ps->pps_list[pps_id]);
625 135 ps->pps_list[pps_id] = pps;
626
627 135 return ret;
628 }
629
630 877 static int decode_ps(VVCParamSets *ps, const CodedBitstreamH266Context *h266, void *log_ctx, int is_clvss)
631 {
632 877 const H266RawPictureHeader *ph = h266->ph;
633 const H266RawPPS *rpps;
634 const H266RawSPS *rsps;
635 int ret;
636
637
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 877 times.
877 if (!ph)
638 return AVERROR_INVALIDDATA;
639
640 877 rpps = h266->pps[ph->ph_pic_parameter_set_id];
641
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 877 times.
877 if (!rpps)
642 return AVERROR_INVALIDDATA;
643
644 877 rsps = h266->sps[rpps->pps_seq_parameter_set_id];
645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 877 times.
877 if (!rsps)
646 return AVERROR_INVALIDDATA;
647
648 877 ret = decode_sps(ps, rsps, log_ctx, is_clvss);
649
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 877 times.
877 if (ret < 0)
650 return ret;
651
652 877 ret = decode_pps(ps, rpps);
653
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 877 times.
877 if (ret < 0)
654 return ret;
655
656 877 return 0;
657 }
658
659 #define WEIGHT_TABLE(x) \
660 w->nb_weights[L##x] = r->num_weights_l##x; \
661 for (int i = 0; i < w->nb_weights[L##x]; i++) { \
662 w->weight_flag[L##x][LUMA][i] = r->luma_weight_l##x##_flag[i]; \
663 w->weight_flag[L##x][CHROMA][i] = r->chroma_weight_l##x##_flag[i]; \
664 w->weight[L##x][LUMA][i] = denom[LUMA] + r->delta_luma_weight_l##x[i]; \
665 w->offset[L##x][LUMA][i] = r->luma_offset_l##x[i]; \
666 for (int j = CB; j <= CR; j++) { \
667 w->weight[L##x][j][i] = denom[CHROMA] + r->delta_chroma_weight_l##x[i][j - 1]; \
668 w->offset[L##x][j][i] = 128 + r->delta_chroma_offset_l##x[i][j - 1]; \
669 w->offset[L##x][j][i] -= (128 * w->weight[L##x][j][i]) >> w->log2_denom[CHROMA]; \
670 w->offset[L##x][j][i] = av_clip_intp2(w->offset[L##x][j][i], 7); \
671 } \
672 } \
673
674 16 static void pred_weight_table(PredWeightTable *w, const H266RawPredWeightTable *r)
675 {
676 int denom[2];
677
678 16 w->log2_denom[LUMA] = r->luma_log2_weight_denom;
679 16 w->log2_denom[CHROMA] = w->log2_denom[LUMA] + r->delta_chroma_log2_weight_denom;
680 16 denom[LUMA] = 1 << w->log2_denom[LUMA];
681 16 denom[CHROMA] = 1 << w->log2_denom[CHROMA];
682
4/4
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 16 times.
109 WEIGHT_TABLE(0)
683
4/4
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 16 times.
109 WEIGHT_TABLE(1)
684 16 }
685
686 // 8.3.1 Decoding process for picture order count
687 877 static int ph_compute_poc(const H266RawPictureHeader *ph, const H266RawSPS *sps, const int poc_tid0, const int is_clvss)
688 {
689 877 const int max_poc_lsb = 1 << (sps->sps_log2_max_pic_order_cnt_lsb_minus4 + 4);
690 877 const int prev_poc_lsb = poc_tid0 % max_poc_lsb;
691 877 const int prev_poc_msb = poc_tid0 - prev_poc_lsb;
692 877 const int poc_lsb = ph->ph_pic_order_cnt_lsb;
693 int poc_msb;
694
695
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 877 times.
877 if (ph->ph_poc_msb_cycle_present_flag) {
696 poc_msb = ph->ph_poc_msb_cycle_val * max_poc_lsb;
697
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 812 times.
877 } else if (is_clvss) {
698 65 poc_msb = 0;
699 } else {
700
3/4
✓ Branch 0 taken 511 times.
✓ Branch 1 taken 301 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 511 times.
812 if (poc_lsb < prev_poc_lsb && prev_poc_lsb - poc_lsb >= max_poc_lsb / 2)
701 poc_msb = prev_poc_msb + max_poc_lsb;
702
3/4
✓ Branch 0 taken 301 times.
✓ Branch 1 taken 511 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 301 times.
812 else if (poc_lsb > prev_poc_lsb && poc_lsb - prev_poc_lsb > max_poc_lsb / 2)
703 poc_msb = prev_poc_msb - max_poc_lsb;
704 else
705 812 poc_msb = prev_poc_msb;
706 }
707
708 877 return poc_msb + poc_lsb;
709 }
710
711 684032 static av_always_inline uint16_t lmcs_derive_lut_sample(uint16_t sample,
712 uint16_t *pivot1, uint16_t *pivot2, uint16_t *scale_coeff, const int idx, const int max)
713 {
714 684032 const int lut_sample =
715 684032 pivot1[idx] + ((scale_coeff[idx] * (sample - pivot2[idx]) + (1<< 10)) >> 11);
716 684032 return av_clip(lut_sample, 0, max - 1);
717 }
718
719 //8.8.2.2 Inverse mapping process for a luma sample
720 334 static int lmcs_derive_lut(VVCLMCS *lmcs, const H266RawAPS *rlmcs, const H266RawSPS *sps)
721 {
722 334 const int bit_depth = (sps->sps_bitdepth_minus8 + 8);
723 334 const int max = (1 << bit_depth);
724 334 const int org_cw = max / LMCS_MAX_BIN_SIZE;
725 334 const int shift = av_log2(org_cw);
726 334 const int off = 1 << (shift - 1);
727 int cw[LMCS_MAX_BIN_SIZE];
728 uint16_t input_pivot[LMCS_MAX_BIN_SIZE];
729 uint16_t scale_coeff[LMCS_MAX_BIN_SIZE];
730 uint16_t inv_scale_coeff[LMCS_MAX_BIN_SIZE];
731 int i, delta_crs;
732
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 334 times.
334 if (bit_depth > LMCS_MAX_BIT_DEPTH)
733 return AVERROR_PATCHWELCOME;
734
735
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 334 times.
334 if (!rlmcs)
736 return AVERROR_INVALIDDATA;
737
738 334 lmcs->min_bin_idx = rlmcs->lmcs_min_bin_idx;
739 334 lmcs->max_bin_idx = LMCS_MAX_BIN_SIZE - 1 - rlmcs->lmcs_min_bin_idx;
740
741 334 memset(cw, 0, sizeof(cw));
742
2/2
✓ Branch 0 taken 4610 times.
✓ Branch 1 taken 334 times.
4944 for (int i = lmcs->min_bin_idx; i <= lmcs->max_bin_idx; i++)
743 4610 cw[i] = org_cw + (1 - 2 * rlmcs->lmcs_delta_sign_cw_flag[i]) * rlmcs->lmcs_delta_abs_cw[i];
744
745 334 delta_crs = (1 - 2 * rlmcs->lmcs_delta_sign_crs_flag) * rlmcs->lmcs_delta_abs_crs;
746
747 334 lmcs->pivot[0] = 0;
748
2/2
✓ Branch 0 taken 5344 times.
✓ Branch 1 taken 334 times.
5678 for (i = 0; i < LMCS_MAX_BIN_SIZE; i++) {
749 5344 input_pivot[i] = i * org_cw;
750 5344 lmcs->pivot[i + 1] = lmcs->pivot[i] + cw[i];
751 5344 scale_coeff[i] = (cw[i] * (1 << 11) + off) >> shift;
752
2/2
✓ Branch 0 taken 734 times.
✓ Branch 1 taken 4610 times.
5344 if (cw[i] == 0) {
753 734 inv_scale_coeff[i] = 0;
754 734 lmcs->chroma_scale_coeff[i] = (1 << 11);
755 } else {
756 4610 inv_scale_coeff[i] = org_cw * (1 << 11) / cw[i];
757 4610 lmcs->chroma_scale_coeff[i] = org_cw * (1 << 11) / (cw[i] + delta_crs);
758 }
759 }
760
761 //derive lmcs_fwd_lut
762
2/2
✓ Branch 0 taken 342016 times.
✓ Branch 1 taken 334 times.
342350 for (uint16_t sample = 0; sample < max; sample++) {
763 342016 const int idx_y = sample / org_cw;
764 342016 const uint16_t fwd_sample = lmcs_derive_lut_sample(sample, lmcs->pivot,
765 input_pivot, scale_coeff, idx_y, max);
766
1/2
✓ Branch 0 taken 342016 times.
✗ Branch 1 not taken.
342016 if (bit_depth > 8)
767 342016 lmcs->fwd_lut.u16[sample] = fwd_sample;
768 else
769 lmcs->fwd_lut.u8 [sample] = fwd_sample;
770
771 }
772
773 //derive lmcs_inv_lut
774 334 i = lmcs->min_bin_idx;
775
2/2
✓ Branch 0 taken 342016 times.
✓ Branch 1 taken 334 times.
342350 for (uint16_t sample = 0; sample < max; sample++) {
776 uint16_t inv_sample;
777
4/4
✓ Branch 0 taken 333457 times.
✓ Branch 1 taken 13169 times.
✓ Branch 2 taken 4610 times.
✓ Branch 3 taken 328847 times.
346626 while (i <= lmcs->max_bin_idx && sample >= lmcs->pivot[i + 1])
778 4610 i++;
779
780 342016 inv_sample = lmcs_derive_lut_sample(sample, input_pivot, lmcs->pivot,
781 inv_scale_coeff, i, max);
782
783
1/2
✓ Branch 0 taken 342016 times.
✗ Branch 1 not taken.
342016 if (bit_depth > 8)
784 342016 lmcs->inv_lut.u16[sample] = inv_sample;
785 else
786 lmcs->inv_lut.u8 [sample] = inv_sample;
787 }
788
789 334 return 0;
790 }
791
792 877 static int ph_max_num_subblock_merge_cand(const H266RawSPS *sps, const H266RawPictureHeader *ph)
793 {
794
2/2
✓ Branch 0 taken 874 times.
✓ Branch 1 taken 3 times.
877 if (sps->sps_affine_enabled_flag)
795 874 return 5 - sps->sps_five_minus_max_num_subblock_merge_cand;
796
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 return sps->sps_sbtmvp_enabled_flag && ph->ph_temporal_mvp_enabled_flag;
797 }
798
799 static int ph_vb_pos(uint16_t *vbs, uint8_t *num_vbs, const uint16_t *pos_minus_1, const uint8_t num_pos, uint16_t max, const int ctb_size_y)
800 {
801 max = FF_CEIL_RSHIFT(max, 3) - 2;
802 for (int i = 0; i < num_pos; i++) {
803 if (pos_minus_1[i] > max)
804 return AVERROR_INVALIDDATA;
805
806 vbs[i] = (pos_minus_1[i] + 1) << 3;
807
808 // The distance between any two vertical virtual boundaries shall be greater than or equal to CtbSizeY luma samples
809 if (i && vbs[i] < vbs[i - 1] + ctb_size_y)
810 return AVERROR_INVALIDDATA;
811 }
812 *num_vbs = num_pos;
813
814 return 0;
815 }
816
817 #define VBF(f) (sps->sps_virtual_boundaries_present_flag ? sps->sps_##f : ph->r->ph_##f)
818 #define VBFS(c, d) VBF(virtual_boundary_pos_##c##_minus1), VBF(num_##d##_virtual_boundaries)
819
820 877 static int ph_vb(VVCPH *ph, const H266RawSPS *sps, const H266RawPPS *pps)
821 {
822 877 const int ctb_size_y = 1 << (sps->sps_log2_ctu_size_minus5 + 5);
823 int ret;
824
825
1/2
✓ Branch 0 taken 877 times.
✗ Branch 1 not taken.
877 if (!sps->sps_virtual_boundaries_enabled_flag)
826 877 return 0;
827
828 ret = ph_vb_pos(ph->vb_pos_x, &ph->num_ver_vbs, VBFS(x, ver), pps->pps_pic_width_in_luma_samples, ctb_size_y);
829 if (ret < 0)
830 return ret;
831
832 ret = ph_vb_pos(ph->vb_pos_y, &ph->num_hor_vbs, VBFS(y, hor), pps->pps_pic_height_in_luma_samples, ctb_size_y);
833 if (ret < 0)
834 return ret;
835
836 return 0;
837 }
838
839 877 static int ph_derive(VVCPH *ph, const H266RawSPS *sps, const H266RawPPS *pps, const int poc_tid0, const int is_clvss)
840 {
841 int ret;
842 877 ph->max_num_subblock_merge_cand = ph_max_num_subblock_merge_cand(sps, ph->r);
843
844 877 ph->poc = ph_compute_poc(ph->r, sps, poc_tid0, is_clvss);
845
846
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 877 times.
877 if (pps->pps_wp_info_in_ph_flag)
847 pred_weight_table(&ph->pwt, &ph->r->ph_pred_weight_table);
848
849 877 ret = ph_vb(ph, sps, pps);
850
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 877 times.
877 if (ret < 0)
851 return ret;
852
853 877 return 0;
854 }
855
856 877 static int decode_ph(VVCFrameParamSets *fps, const H266RawPictureHeader *rph, void *rph_ref,
857 const int poc_tid0, const int is_clvss)
858 {
859 int ret;
860 877 VVCPH *ph = &fps->ph;
861 877 const H266RawSPS *sps = fps->sps->r;
862 877 const H266RawPPS *pps = fps->pps->r;
863
864 877 ph->r = rph;
865 877 ff_refstruct_replace(&ph->rref, rph_ref);
866 877 ret = ph_derive(ph, sps, pps, poc_tid0, is_clvss);
867
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 877 times.
877 if (ret < 0)
868 return ret;
869
870 877 return 0;
871 }
872
873 877 static int decode_frame_ps(VVCFrameParamSets *fps, const VVCParamSets *ps,
874 const CodedBitstreamH266Context *h266, const int poc_tid0, const int is_clvss)
875 {
876 877 const H266RawPictureHeader *ph = h266->ph;
877 const H266RawPPS *rpps;
878 int ret;
879
880
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 877 times.
877 if (!ph)
881 return AVERROR_INVALIDDATA;
882
883 877 rpps = h266->pps[ph->ph_pic_parameter_set_id];
884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 877 times.
877 if (!rpps)
885 return AVERROR_INVALIDDATA;
886
887 877 ff_refstruct_replace(&fps->sps, ps->sps_list[rpps->pps_seq_parameter_set_id]);
888 877 ff_refstruct_replace(&fps->pps, ps->pps_list[rpps->pps_pic_parameter_set_id]);
889
890 877 ret = decode_ph(fps, ph, h266->ph_ref, poc_tid0, is_clvss);
891
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 877 times.
877 if (ret < 0)
892 return ret;
893
894
2/2
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 763 times.
877 if (ph->ph_explicit_scaling_list_enabled_flag)
895 114 ff_refstruct_replace(&fps->sl, ps->scaling_list[ph->ph_scaling_list_aps_id]);
896
897
2/2
✓ Branch 0 taken 334 times.
✓ Branch 1 taken 543 times.
877 if (ph->ph_lmcs_enabled_flag) {
898 334 ret = lmcs_derive_lut(&fps->lmcs, ps->lmcs_list[ph->ph_lmcs_aps_id], fps->sps->r);
899
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 334 times.
334 if (ret < 0)
900 return ret;
901 }
902
903
2/2
✓ Branch 0 taken 7016 times.
✓ Branch 1 taken 877 times.
7893 for (int i = 0; i < FF_ARRAY_ELEMS(fps->alf_list); i++)
904 7016 ff_refstruct_replace(&fps->alf_list[i], ps->alf_list[i]);
905
906 877 return 0;
907 }
908
909 877 static void decode_recovery_flag(VVCContext *s)
910 {
911
4/4
✓ Branch 0 taken 874 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 58 times.
✓ Branch 3 taken 816 times.
877 if (IS_IDR(s))
912 61 s->no_output_before_recovery_flag = 1;
913
4/4
✓ Branch 0 taken 806 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 804 times.
816 else if (IS_CRA(s) || IS_GDR(s))
914 12 s->no_output_before_recovery_flag = s->last_eos;
915 877 }
916
917 877 static void decode_recovery_poc(VVCContext *s, const VVCPH *ph)
918 {
919
2/2
✓ Branch 0 taken 692 times.
✓ Branch 1 taken 185 times.
877 if (s->no_output_before_recovery_flag) {
920
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 690 times.
692 if (IS_GDR(s))
921 2 s->gdr_recovery_point_poc = ph->poc + ph->r->ph_recovery_poc_cnt;
922
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 690 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
692 if (!GDR_IS_RECOVERED(s) && s->gdr_recovery_point_poc <= ph->poc)
923 2 GDR_SET_RECOVERED(s);
924 }
925 877 }
926
927 877 int ff_vvc_decode_frame_ps(VVCFrameParamSets *fps, struct VVCContext *s)
928 {
929 877 int ret = 0;
930 877 VVCParamSets *ps = &s->ps;
931 877 const CodedBitstreamH266Context *h266 = s->cbc->priv_data;
932 int is_clvss;
933
934 877 decode_recovery_flag(s);
935
10/10
✓ Branch 0 taken 874 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 816 times.
✓ Branch 3 taken 58 times.
✓ Branch 4 taken 806 times.
✓ Branch 5 taken 10 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 804 times.
✓ Branch 8 taken 65 times.
✓ Branch 9 taken 8 times.
877 is_clvss = IS_CLVSS(s);
936
937 877 ret = decode_ps(ps, h266, s->avctx, is_clvss);
938
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 877 times.
877 if (ret < 0)
939 return ret;
940
941 877 ret = decode_frame_ps(fps, ps, h266, s->poc_tid0, is_clvss);
942 877 decode_recovery_poc(s, &fps->ph);
943 877 return ret;
944 }
945
946 416 void ff_vvc_frame_ps_free(VVCFrameParamSets *fps)
947 {
948 416 ff_refstruct_unref(&fps->sps);
949 416 ff_refstruct_unref(&fps->pps);
950 416 ff_refstruct_unref(&fps->ph.rref);
951 416 ff_refstruct_unref(&fps->sl);
952
2/2
✓ Branch 0 taken 3328 times.
✓ Branch 1 taken 416 times.
3744 for (int i = 0; i < FF_ARRAY_ELEMS(fps->alf_list); i++)
953 3328 ff_refstruct_unref(&fps->alf_list[i]);
954 416 }
955
956 52 void ff_vvc_ps_uninit(VVCParamSets *ps)
957 {
958
2/2
✓ Branch 0 taken 416 times.
✓ Branch 1 taken 52 times.
468 for (int i = 0; i < FF_ARRAY_ELEMS(ps->scaling_list); i++)
959 416 ff_refstruct_unref(&ps->scaling_list[i]);
960
2/2
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 52 times.
260 for (int i = 0; i < FF_ARRAY_ELEMS(ps->lmcs_list); i++)
961 208 ff_refstruct_unref(&ps->lmcs_list[i]);
962
2/2
✓ Branch 0 taken 416 times.
✓ Branch 1 taken 52 times.
468 for (int i = 0; i < FF_ARRAY_ELEMS(ps->alf_list); i++)
963 416 ff_refstruct_unref(&ps->alf_list[i]);
964
2/2
✓ Branch 0 taken 832 times.
✓ Branch 1 taken 52 times.
884 for (int i = 0; i < FF_ARRAY_ELEMS(ps->sps_list); i++)
965 832 ff_refstruct_unref(&ps->sps_list[i]);
966
2/2
✓ Branch 0 taken 3328 times.
✓ Branch 1 taken 52 times.
3380 for (int i = 0; i < FF_ARRAY_ELEMS(ps->pps_list); i++)
967 3328 ff_refstruct_unref(&ps->pps_list[i]);
968 52 }
969
970 4315 static void alf_coeff(int16_t *coeff,
971 const uint8_t *abs, const uint8_t *sign, const int size)
972 {
973
2/2
✓ Branch 0 taken 49890 times.
✓ Branch 1 taken 4315 times.
54205 for (int i = 0; i < size; i++)
974 49890 coeff[i] = (1 - 2 * sign[i]) * abs[i];
975 4315 }
976
977 380 static void alf_coeff_cc(int16_t *coeff,
978 const uint8_t *mapped_abs, const uint8_t *sign)
979 {
980
2/2
✓ Branch 0 taken 2660 times.
✓ Branch 1 taken 380 times.
3040 for (int i = 0; i < ALF_NUM_COEFF_CC; i++) {
981 2660 int c = mapped_abs[i];
982
2/2
✓ Branch 0 taken 2257 times.
✓ Branch 1 taken 403 times.
2660 if (c)
983 2257 c = (1 - 2 * sign[i]) * (1 << (c - 1));
984 2660 coeff[i] = c;
985 }
986 380 }
987
988 206 static void alf_luma(VVCALF *alf, const H266RawAPS *aps)
989 {
990
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 160 times.
206 if (!aps->alf_luma_filter_signal_flag)
991 46 return;
992
993
2/2
✓ Branch 0 taken 4000 times.
✓ Branch 1 taken 160 times.
4160 for (int i = 0; i < ALF_NUM_FILTERS_LUMA; i++) {
994 4000 const int ref = aps->alf_luma_coeff_delta_idx[i];
995 4000 const uint8_t *abs = aps->alf_luma_coeff_abs[ref];
996 4000 const uint8_t *sign = aps->alf_luma_coeff_sign[ref];
997
998 4000 alf_coeff(alf->luma_coeff[i], abs, sign, ALF_NUM_COEFF_LUMA);
999 4000 memcpy(alf->luma_clip_idx[i], aps->alf_luma_clip_idx[ref],
1000 sizeof(alf->luma_clip_idx[i]));
1001 }
1002 }
1003
1004 206 static void alf_chroma(VVCALF *alf, const H266RawAPS *aps)
1005 {
1006
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 123 times.
206 if (!aps->alf_chroma_filter_signal_flag)
1007 83 return;
1008
1009 123 alf->num_chroma_filters = aps->alf_chroma_num_alt_filters_minus1 + 1;
1010
2/2
✓ Branch 0 taken 315 times.
✓ Branch 1 taken 123 times.
438 for (int i = 0; i < alf->num_chroma_filters; i++) {
1011 315 const uint8_t *abs = aps->alf_chroma_coeff_abs[i];
1012 315 const uint8_t *sign = aps->alf_chroma_coeff_sign[i];
1013
1014 315 alf_coeff(alf->chroma_coeff[i], abs, sign, ALF_NUM_COEFF_CHROMA);
1015 315 memcpy(alf->chroma_clip_idx[i], aps->alf_chroma_clip_idx[i],
1016 sizeof(alf->chroma_clip_idx[i]));
1017 }
1018 }
1019
1020 206 static void alf_cc(VVCALF *alf, const H266RawAPS *aps)
1021 {
1022 206 const uint8_t (*abs[])[ALF_NUM_COEFF_CC] =
1023 206 { aps->alf_cc_cb_mapped_coeff_abs, aps->alf_cc_cr_mapped_coeff_abs };
1024 206 const uint8_t (*sign[])[ALF_NUM_COEFF_CC] =
1025 206 {aps->alf_cc_cb_coeff_sign, aps->alf_cc_cr_coeff_sign };
1026 206 const int signaled[] = { aps->alf_cc_cb_filter_signal_flag, aps->alf_cc_cr_filter_signal_flag};
1027
1028 206 alf->num_cc_filters[0] = aps->alf_cc_cb_filters_signalled_minus1 + 1;
1029 206 alf->num_cc_filters[1] = aps->alf_cc_cr_filters_signalled_minus1 + 1;
1030
1031
2/2
✓ Branch 0 taken 412 times.
✓ Branch 1 taken 206 times.
618 for (int idx = 0; idx < 2; idx++) {
1032
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 307 times.
412 if (signaled[idx]) {
1033
2/2
✓ Branch 0 taken 380 times.
✓ Branch 1 taken 105 times.
485 for (int i = 0; i < alf->num_cc_filters[idx]; i++)
1034 380 alf_coeff_cc(alf->cc_coeff[idx][i], abs[idx][i], sign[idx][i]);
1035 }
1036 }
1037 206 }
1038
1039 206 static void alf_derive(VVCALF *alf, const H266RawAPS *aps)
1040 {
1041 206 alf_luma(alf, aps);
1042 206 alf_chroma(alf, aps);
1043 206 alf_cc(alf, aps);
1044 206 }
1045
1046 206 static int aps_decode_alf(const VVCALF **alf, const H266RawAPS *aps)
1047 {
1048 206 VVCALF *a = ff_refstruct_allocz(sizeof(*a));
1049
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 206 times.
206 if (!a)
1050 return AVERROR(ENOMEM);
1051
1052 206 alf_derive(a, aps);
1053 206 ff_refstruct_replace(alf, a);
1054 206 ff_refstruct_unref(&a);
1055
1056 206 return 0;
1057 }
1058
1059 252 static int is_luma_list(const int id)
1060 {
1061
4/4
✓ Branch 0 taken 171 times.
✓ Branch 1 taken 81 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 162 times.
252 return id % VVC_MAX_SAMPLE_ARRAYS == SL_START_4x4 || id == SL_START_64x64 + 1;
1062 }
1063
1064 336 static int derive_matrix_size(const int id)
1065 {
1066
4/4
✓ Branch 0 taken 312 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 72 times.
✓ Branch 3 taken 240 times.
336 return id < SL_START_4x4 ? 2 : (id < SL_START_8x8 ? 4 : 8);
1067 }
1068
1069 // 7.4.3.20 Scaling list data semantics
1070 12 static void scaling_derive(VVCScalingList *sl, const H266RawAPS *aps)
1071 {
1072
2/2
✓ Branch 0 taken 336 times.
✓ Branch 1 taken 12 times.
348 for (int id = 0; id < SL_MAX_ID; id++) {
1073 336 const int matrix_size = derive_matrix_size(id);
1074 336 const int log2_size = av_log2(matrix_size);
1075 336 const int list_size = matrix_size * matrix_size;
1076 int coeff[SL_MAX_MATRIX_SIZE * SL_MAX_MATRIX_SIZE];
1077 const uint8_t *pred;
1078 const int *scaling_list;
1079 336 int dc = 0;
1080
1081
4/4
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 84 times.
✓ Branch 3 taken 90 times.
✓ Branch 4 taken 162 times.
336 if (aps->aps_chroma_present_flag || is_luma_list(id)) {
1082
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 117 times.
174 if (!aps->scaling_list_copy_mode_flag[id]) {
1083 57 int next_coef = 0;
1084
1085
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 49 times.
57 if (id >= SL_START_16x16)
1086 8 dc = next_coef = aps->scaling_list_dc_coef[id - SL_START_16x16];
1087
1088
2/2
✓ Branch 0 taken 2412 times.
✓ Branch 1 taken 57 times.
2469 for (int i = 0; i < list_size; i++) {
1089 2412 const int x = ff_vvc_diag_scan_x[3][3][i];
1090 2412 const int y = ff_vvc_diag_scan_y[3][3][i];
1091
1092
6/6
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 2284 times.
✓ Branch 2 taken 64 times.
✓ Branch 3 taken 64 times.
✓ Branch 4 taken 32 times.
✓ Branch 5 taken 32 times.
2412 if (!(id >= SL_START_64x64 && x >= 4 && y >= 4))
1093 2380 next_coef += aps->scaling_list_delta_coef[id][i];
1094 2412 coeff[i] = next_coef;
1095 }
1096 }
1097 }
1098
1099 //dc
1100
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 168 times.
336 if (id >= SL_START_16x16) {
1101
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 160 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
168 if (!aps->scaling_list_copy_mode_flag[id] && !aps->scaling_list_pred_mode_flag[id]) {
1102 sl->scaling_matrix_dc_rec[id - SL_START_16x16] = 8;
1103
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 96 times.
168 } else if (!aps->scaling_list_pred_id_delta[id]) {
1104 72 sl->scaling_matrix_dc_rec[id - SL_START_16x16] = 16;
1105 } else {
1106 96 const int ref_id = id - aps->scaling_list_pred_id_delta[id];
1107
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 26 times.
96 if (ref_id >= SL_START_16x16)
1108 70 dc += sl->scaling_matrix_dc_rec[ref_id - SL_START_16x16];
1109 else
1110 26 dc += sl->scaling_matrix_rec[ref_id][0];
1111 96 sl->scaling_matrix_dc_rec[id - SL_START_16x16] = dc & 255;
1112 }
1113 }
1114
1115 //ac
1116
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 279 times.
336 scaling_list = aps->scaling_list_copy_mode_flag[id] ? ff_vvc_scaling_list0 : coeff;
1117
4/4
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 279 times.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 30 times.
336 if (!aps->scaling_list_copy_mode_flag[id] && !aps->scaling_list_pred_mode_flag[id])
1118 27 pred = ff_vvc_scaling_pred_8;
1119
2/2
✓ Branch 0 taken 170 times.
✓ Branch 1 taken 139 times.
309 else if (!aps->scaling_list_pred_id_delta[id])
1120 170 pred = ff_vvc_scaling_pred_16;
1121 else
1122 139 pred = sl->scaling_matrix_rec[id - aps->scaling_list_pred_id_delta[id]];
1123
2/2
✓ Branch 0 taken 16608 times.
✓ Branch 1 taken 336 times.
16944 for (int i = 0; i < list_size; i++) {
1124 16608 const int x = ff_vvc_diag_scan_x[log2_size][log2_size][i];
1125 16608 const int y = ff_vvc_diag_scan_y[log2_size][log2_size][i];
1126 16608 const int off = y * matrix_size + x;
1127 16608 sl->scaling_matrix_rec[id][off] = (pred[off] + scaling_list[i]) & 255;
1128 }
1129 }
1130 12 }
1131
1132 12 static int aps_decode_scaling(const VVCScalingList **scaling, const H266RawAPS *aps)
1133 {
1134 12 VVCScalingList *sl = ff_refstruct_allocz(sizeof(*sl));
1135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (!sl)
1136 return AVERROR(ENOMEM);
1137
1138 12 scaling_derive(sl, aps);
1139 12 ff_refstruct_replace(scaling, sl);
1140 12 ff_refstruct_unref(&sl);
1141
1142 12 return 0;
1143 }
1144
1145 273 int ff_vvc_decode_aps(VVCParamSets *ps, const CodedBitstreamUnit *unit)
1146 {
1147 273 const H266RawAPS *aps = unit->content_ref;
1148 273 int ret = 0;
1149
1150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 273 times.
273 if (!aps)
1151 return AVERROR_INVALIDDATA;
1152
1153
3/4
✓ Branch 0 taken 206 times.
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
273 switch (aps->aps_params_type) {
1154 206 case VVC_ASP_TYPE_ALF:
1155 206 ret = aps_decode_alf(&ps->alf_list[aps->aps_adaptation_parameter_set_id], aps);
1156 206 break;
1157 55 case VVC_ASP_TYPE_LMCS:
1158 55 ff_refstruct_replace(&ps->lmcs_list[aps->aps_adaptation_parameter_set_id], aps);
1159 55 break;
1160 12 case VVC_ASP_TYPE_SCALING:
1161 12 ret = aps_decode_scaling(&ps->scaling_list[aps->aps_adaptation_parameter_set_id], aps);
1162 12 break;
1163 }
1164
1165 273 return ret;
1166 }
1167
1168 1583 static int sh_alf_aps(const VVCSH *sh, const VVCFrameParamSets *fps)
1169 {
1170
2/2
✓ Branch 0 taken 321 times.
✓ Branch 1 taken 1262 times.
1583 if (!sh->r->sh_alf_enabled_flag)
1171 321 return 0;
1172
1173
2/2
✓ Branch 0 taken 1415 times.
✓ Branch 1 taken 1262 times.
2677 for (int i = 0; i < sh->r->sh_num_alf_aps_ids_luma; i++) {
1174 1415 const VVCALF *alf_aps_luma = fps->alf_list[sh->r->sh_alf_aps_id_luma[i]];
1175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1415 times.
1415 if (!alf_aps_luma)
1176 return AVERROR_INVALIDDATA;
1177 }
1178
1179
4/4
✓ Branch 0 taken 711 times.
✓ Branch 1 taken 551 times.
✓ Branch 2 taken 149 times.
✓ Branch 3 taken 562 times.
1262 if (sh->r->sh_alf_cb_enabled_flag || sh->r->sh_alf_cr_enabled_flag) {
1180 700 const VVCALF *alf_aps_chroma = fps->alf_list[sh->r->sh_alf_aps_id_chroma];
1181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 700 times.
700 if (!alf_aps_chroma)
1182 return AVERROR_INVALIDDATA;
1183 }
1184
1185
2/2
✓ Branch 0 taken 1204 times.
✓ Branch 1 taken 58 times.
1262 if (fps->sps->r->sps_ccalf_enabled_flag) {
1186
2/2
✓ Branch 0 taken 155 times.
✓ Branch 1 taken 1049 times.
1204 if (sh->r->sh_alf_cc_cb_enabled_flag) {
1187 155 const VVCALF *alf_aps_cc_cr = fps->alf_list[sh->r->sh_alf_cc_cb_aps_id];
1188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 155 times.
155 if (!alf_aps_cc_cr)
1189 return AVERROR_INVALIDDATA;
1190 }
1191
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 1073 times.
1204 if (sh->r->sh_alf_cc_cr_enabled_flag) {
1192 131 const VVCALF *alf_aps_cc_cr = fps->alf_list[sh->r->sh_alf_cc_cr_aps_id];
1193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 131 times.
131 if (!alf_aps_cc_cr)
1194 return AVERROR_INVALIDDATA;
1195 }
1196 }
1197
1198 1262 return 0;
1199 }
1200
1201 1583 static void sh_slice_address(VVCSH *sh, const H266RawSPS *sps, const VVCPPS *pps)
1202 {
1203 1583 const int slice_address = sh->r->sh_slice_address;
1204
1205
2/2
✓ Branch 0 taken 600 times.
✓ Branch 1 taken 983 times.
1583 if (pps->r->pps_rect_slice_flag) {
1206 600 int pic_level_slice_idx = slice_address;
1207
2/2
✓ Branch 0 taken 974 times.
✓ Branch 1 taken 600 times.
1574 for (int j = 0; j < sh->r->curr_subpic_idx; j++)
1208 974 pic_level_slice_idx += pps->r->num_slices_in_subpic[j];
1209 600 sh->ctb_addr_in_curr_slice = pps->ctb_addr_in_slice + pps->slice_start_offset[pic_level_slice_idx];
1210 600 sh->num_ctus_in_curr_slice = pps->num_ctus_in_slice[pic_level_slice_idx];
1211 } else {
1212 983 int tile_x = slice_address % pps->r->num_tile_columns;
1213 983 int tile_y = slice_address / pps->r->num_tile_columns;
1214 983 const int slice_start_ctb = pps->row_bd[tile_y] * pps->ctb_width + pps->col_bd[tile_x] * pps->r->row_height_val[tile_y];
1215
1216 983 sh->ctb_addr_in_curr_slice = pps->ctb_addr_in_slice + slice_start_ctb;
1217
1218 983 sh->num_ctus_in_curr_slice = 0;
1219
2/2
✓ Branch 0 taken 1063 times.
✓ Branch 1 taken 983 times.
2046 for (int tile_idx = slice_address; tile_idx <= slice_address + sh->r->sh_num_tiles_in_slice_minus1; tile_idx++) {
1220 1063 tile_x = tile_idx % pps->r->num_tile_columns;
1221 1063 tile_y = tile_idx / pps->r->num_tile_columns;
1222 1063 sh->num_ctus_in_curr_slice += pps->r->row_height_val[tile_y] * pps->r->col_width_val[tile_x];
1223 }
1224 }
1225 1583 }
1226
1227 1583 static void sh_qp_y(VVCSH *sh, const H266RawPPS *pps, const H266RawPictureHeader *ph)
1228 {
1229 1583 const int init_qp = pps->pps_init_qp_minus26 + 26;
1230
1231
2/2
✓ Branch 0 taken 1319 times.
✓ Branch 1 taken 264 times.
1583 if (!pps->pps_qp_delta_info_in_ph_flag)
1232 1319 sh->slice_qp_y = init_qp + sh->r->sh_qp_delta;
1233 else
1234 264 sh->slice_qp_y = init_qp + ph->ph_qp_delta;
1235 1583 }
1236
1237 1583 static void sh_inter(VVCSH *sh, const H266RawSPS *sps, const H266RawPPS *pps)
1238 {
1239 1583 const H266RawSliceHeader *rsh = sh->r;
1240
1241
1/2
✓ Branch 0 taken 1583 times.
✗ Branch 1 not taken.
1583 if (!pps->pps_wp_info_in_ph_flag &&
1242
3/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1565 times.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
1583 ((pps->pps_weighted_pred_flag && IS_P(rsh)) ||
1243
4/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1565 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 2 times.
1583 (pps->pps_weighted_bipred_flag && IS_B(rsh))))
1244 16 pred_weight_table(&sh->pwt, &rsh->sh_pred_weight_table);
1245 1583 }
1246
1247 1583 static void sh_deblock_offsets(VVCSH *sh)
1248 {
1249 1583 const H266RawSliceHeader *r = sh->r;
1250
1251
1/2
✓ Branch 0 taken 1583 times.
✗ Branch 1 not taken.
1583 if (!r->sh_deblocking_filter_disabled_flag) {
1252 1583 sh->deblock.beta_offset[LUMA] = r->sh_luma_beta_offset_div2 * 2;
1253 1583 sh->deblock.tc_offset[LUMA] = r->sh_luma_tc_offset_div2 * 2;
1254 1583 sh->deblock.beta_offset[CB] = r->sh_cb_beta_offset_div2 * 2;
1255 1583 sh->deblock.tc_offset[CB] = r->sh_cb_tc_offset_div2 * 2;
1256 1583 sh->deblock.beta_offset[CR] = r->sh_cr_beta_offset_div2 * 2;
1257 1583 sh->deblock.tc_offset[CR] = r->sh_cr_tc_offset_div2 * 2;
1258 }
1259 1583 }
1260
1261 1583 static void sh_partition_constraints(VVCSH *sh, const H266RawSPS *sps, const H266RawPictureHeader *ph)
1262 {
1263 1583 const int min_cb_log2_size_y = sps->sps_log2_min_luma_coding_block_size_minus2 + 2;
1264 int min_qt_log2_size_y[2];
1265
1266
2/2
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 1365 times.
1583 if (IS_I(sh->r)) {
1267 218 min_qt_log2_size_y[LUMA] = (min_cb_log2_size_y + ph->ph_log2_diff_min_qt_min_cb_intra_slice_luma);
1268 218 min_qt_log2_size_y[CHROMA] = (min_cb_log2_size_y + ph->ph_log2_diff_min_qt_min_cb_intra_slice_chroma);
1269
1270 218 sh->max_bt_size[LUMA] = 1 << (min_qt_log2_size_y[LUMA] + ph->ph_log2_diff_max_bt_min_qt_intra_slice_luma);
1271 218 sh->max_bt_size[CHROMA] = 1 << (min_qt_log2_size_y[CHROMA]+ ph->ph_log2_diff_max_bt_min_qt_intra_slice_chroma);
1272
1273 218 sh->max_tt_size[LUMA] = 1 << (min_qt_log2_size_y[LUMA] + ph->ph_log2_diff_max_tt_min_qt_intra_slice_luma);
1274 218 sh->max_tt_size[CHROMA] = 1 << (min_qt_log2_size_y[CHROMA]+ ph->ph_log2_diff_max_tt_min_qt_intra_slice_chroma);
1275
1276 218 sh->max_mtt_depth[LUMA] = ph->ph_max_mtt_hierarchy_depth_intra_slice_luma;
1277 218 sh->max_mtt_depth[CHROMA] = ph->ph_max_mtt_hierarchy_depth_intra_slice_chroma;
1278
1279 218 sh->cu_qp_delta_subdiv = ph->ph_cu_qp_delta_subdiv_intra_slice;
1280 218 sh->cu_chroma_qp_offset_subdiv = ph->ph_cu_chroma_qp_offset_subdiv_intra_slice;
1281 } else {
1282
2/2
✓ Branch 0 taken 2730 times.
✓ Branch 1 taken 1365 times.
4095 for (int i = LUMA; i <= CHROMA; i++) {
1283 2730 min_qt_log2_size_y[i] = (min_cb_log2_size_y + ph->ph_log2_diff_min_qt_min_cb_inter_slice);
1284 2730 sh->max_bt_size[i] = 1 << (min_qt_log2_size_y[i] + ph->ph_log2_diff_max_bt_min_qt_inter_slice);
1285 2730 sh->max_tt_size[i] = 1 << (min_qt_log2_size_y[i] + ph->ph_log2_diff_max_tt_min_qt_inter_slice);
1286 2730 sh->max_mtt_depth[i] = ph->ph_max_mtt_hierarchy_depth_inter_slice;
1287 }
1288
1289 1365 sh->cu_qp_delta_subdiv = ph->ph_cu_qp_delta_subdiv_inter_slice;
1290 1365 sh->cu_chroma_qp_offset_subdiv = ph->ph_cu_chroma_qp_offset_subdiv_inter_slice;
1291 }
1292
1293 1583 sh->min_qt_size[LUMA] = 1 << min_qt_log2_size_y[LUMA];
1294 1583 sh->min_qt_size[CHROMA] = 1 << min_qt_log2_size_y[CHROMA];
1295 1583 }
1296
1297 1583 static void sh_entry_points(VVCSH *sh, const H266RawSPS *sps, const VVCPPS *pps)
1298 {
1299
1/2
✓ Branch 0 taken 1583 times.
✗ Branch 1 not taken.
1583 if (sps->sps_entry_point_offsets_present_flag) {
1300
2/2
✓ Branch 0 taken 41600 times.
✓ Branch 1 taken 1583 times.
43183 for (int i = 1, j = 0; i < sh->num_ctus_in_curr_slice; i++) {
1301 41600 const int pre_ctb_addr_x = sh->ctb_addr_in_curr_slice[i - 1] % pps->ctb_width;
1302 41600 const int pre_ctb_addr_y = sh->ctb_addr_in_curr_slice[i - 1] / pps->ctb_width;
1303 41600 const int ctb_addr_x = sh->ctb_addr_in_curr_slice[i] % pps->ctb_width;
1304 41600 const int ctb_addr_y = sh->ctb_addr_in_curr_slice[i] / pps->ctb_width;
1305
2/2
✓ Branch 0 taken 41563 times.
✓ Branch 1 taken 37 times.
41600 if (pps->ctb_to_row_bd[ctb_addr_y] != pps->ctb_to_row_bd[pre_ctb_addr_y] ||
1306
4/4
✓ Branch 0 taken 41406 times.
✓ Branch 1 taken 157 times.
✓ Branch 2 taken 3149 times.
✓ Branch 3 taken 38257 times.
41563 pps->ctb_to_col_bd[ctb_addr_x] != pps->ctb_to_col_bd[pre_ctb_addr_x] ||
1307
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 2999 times.
3149 (ctb_addr_y != pre_ctb_addr_y && sps->sps_entropy_coding_sync_enabled_flag)) {
1308 344 sh->entry_point_start_ctu[j++] = i;
1309 }
1310 }
1311 }
1312 1583 }
1313
1314 1583 static int sh_derive(VVCSH *sh, const VVCFrameParamSets *fps)
1315 {
1316 1583 const H266RawSPS *sps = fps->sps->r;
1317 1583 const H266RawPPS *pps = fps->pps->r;
1318 1583 const H266RawPictureHeader *ph = fps->ph.r;
1319 int ret;
1320
1321 1583 sh_slice_address(sh, sps, fps->pps);
1322 1583 ret = sh_alf_aps(sh, fps);
1323
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1583 times.
1583 if (ret < 0)
1324 return ret;
1325 1583 sh_inter(sh, sps, pps);
1326 1583 sh_qp_y(sh, pps, ph);
1327 1583 sh_deblock_offsets(sh);
1328 1583 sh_partition_constraints(sh, sps, ph);
1329 1583 sh_entry_points(sh, sps, fps->pps);
1330
1331 1583 return 0;
1332 }
1333
1334 1583 int ff_vvc_decode_sh(VVCSH *sh, const VVCFrameParamSets *fps, const CodedBitstreamUnit *unit)
1335 {
1336 int ret;
1337
1338
2/4
✓ Branch 0 taken 1583 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1583 times.
1583 if (!fps->sps || !fps->pps)
1339 return AVERROR_INVALIDDATA;
1340
1341 1583 ff_refstruct_replace(&sh->r, unit->content_ref);
1342
1343 1583 ret = sh_derive(sh, fps);
1344
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1583 times.
1583 if (ret < 0)
1345 return ret;
1346
1347 1583 return 0;
1348 }
1349