FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vvc/ps.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 778 892 87.2%
Functions: 75 77 97.4%
Branches: 382 508 75.2%

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