FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vvc/ps.c
Date: 2026-04-20 16:43:18
Exec Total Coverage
Lines: 815 918 88.8%
Functions: 75 76 98.7%
Branches: 411 532 77.3%

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