FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vvc/ps.c
Date: 2025-06-23 20:06:14
Exec Total Coverage
Lines: 802 915 87.7%
Functions: 75 76 98.7%
Branches: 404 534 75.7%

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