FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vvc/dec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 600 684 87.7%
Functions: 51 51 100.0%
Branches: 363 503 72.2%

Line Branch Exec Source
1 /*
2 * VVC video decoder
3 *
4 * Copyright (C) 2021 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 "libavcodec/codec_internal.h"
24 #include "libavcodec/decode.h"
25 #include "libavcodec/hwaccel_internal.h"
26 #include "libavcodec/hwconfig.h"
27 #include "libavcodec/profiles.h"
28 #include "libavcodec/refstruct.h"
29 #include "libavutil/cpu.h"
30 #include "libavutil/mem.h"
31 #include "libavutil/thread.h"
32
33 #include "dec.h"
34 #include "ctu.h"
35 #include "data.h"
36 #include "refs.h"
37 #include "thread.h"
38 #include "config_components.h"
39
40 #define TAB_MAX 32
41
42 typedef struct Tab {
43 void **tab;
44 size_t size;
45 } Tab;
46
47 typedef struct TabList {
48 Tab tabs[TAB_MAX];
49 int nb_tabs;
50
51 int zero;
52 int realloc;
53 } TabList;
54
55 #define TL_ADD(t, s) do { \
56 av_assert0(l->nb_tabs < TAB_MAX); \
57 l->tabs[l->nb_tabs].tab = (void**)&fc->tab.t; \
58 l->tabs[l->nb_tabs].size = sizeof(*fc->tab.t) * (s); \
59 l->nb_tabs++; \
60 } while (0)
61
62 27808 static void tl_init(TabList *l, const int zero, const int realloc)
63 {
64 27808 l->nb_tabs = 0;
65 27808 l->zero = zero;
66 27808 l->realloc = realloc;
67 27808 }
68
69 10612 static int tl_free(TabList *l)
70 {
71
2/2
✓ Branch 0 taken 66364 times.
✓ Branch 1 taken 10612 times.
76976 for (int i = 0; i < l->nb_tabs; i++)
72 66364 av_freep(l->tabs[i].tab);
73
74 10612 return 0;
75 }
76
77 10296 static int tl_create(TabList *l)
78 {
79
2/2
✓ Branch 0 taken 3396 times.
✓ Branch 1 taken 6900 times.
10296 if (l->realloc) {
80 3396 tl_free(l);
81
82
2/2
✓ Branch 0 taken 20892 times.
✓ Branch 1 taken 3396 times.
24288 for (int i = 0; i < l->nb_tabs; i++) {
83 20892 Tab *t = l->tabs + i;
84
2/2
✓ Branch 0 taken 4482 times.
✓ Branch 1 taken 16410 times.
20892 *t->tab = l->zero ? av_mallocz(t->size) : av_malloc(t->size);
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20892 times.
20892 if (!*t->tab)
86 return AVERROR(ENOMEM);
87 }
88 }
89 10296 return 0;
90 }
91
92 10296 static int tl_zero(TabList *l)
93 {
94
2/2
✓ Branch 0 taken 4698 times.
✓ Branch 1 taken 5598 times.
10296 if (l->zero) {
95
2/2
✓ Branch 0 taken 16902 times.
✓ Branch 1 taken 4698 times.
21600 for (int i = 0; i < l->nb_tabs; i++) {
96 16902 Tab *t = l->tabs + i;
97 16902 memset(*t->tab, 0, t->size);
98 }
99 }
100 10296 return 0;
101 }
102
103 2528 static void ctu_nz_tl_init(TabList *l, VVCFrameContext *fc)
104 {
105 2528 const VVCSPS *sps = fc->ps.sps;
106 2528 const VVCPPS *pps = fc->ps.pps;
107
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int ctu_size = sps ? (1 << sps->ctb_log2_size_y << sps->ctb_log2_size_y) : 0;
108
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int ctu_count = pps ? pps->ctb_count : 0;
109
3/4
✓ Branch 0 taken 2282 times.
✓ Branch 1 taken 246 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2282 times.
2528 const int changed = fc->tab.sz.ctu_count != ctu_count || fc->tab.sz.ctu_size != ctu_size;
110
111 2528 tl_init(l, 0, changed);
112
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2528 times.
2528 TL_ADD(cus, ctu_count);
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2528 times.
2528 TL_ADD(ctus, ctu_count);
115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2528 times.
2528 TL_ADD(deblock, ctu_count);
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2528 times.
2528 TL_ADD(sao, ctu_count);
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2528 times.
2528 TL_ADD(alf, ctu_count);
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2528 times.
2528 TL_ADD(slice_idx, ctu_count);
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2528 times.
2528 TL_ADD(coeffs, ctu_count * ctu_size * VVC_MAX_SAMPLE_ARRAYS);
120 2528 }
121
122 2528 static void min_cb_tl_init(TabList *l, VVCFrameContext *fc)
123 {
124 2528 const VVCPPS *pps = fc->ps.pps;
125
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int pic_size_in_min_cb = pps ? pps->min_cb_width * pps->min_cb_height : 0;
126 2528 const int changed = fc->tab.sz.pic_size_in_min_cb != pic_size_in_min_cb;
127
128 2528 tl_init(l, 1, changed);
129
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2528 times.
2528 TL_ADD(imf, pic_size_in_min_cb);
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2528 times.
2528 TL_ADD(imm, pic_size_in_min_cb);
132
133
2/2
✓ Branch 0 taken 5056 times.
✓ Branch 1 taken 2528 times.
7584 for (int i = LUMA; i <= CHROMA; i++)
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5056 times.
5056 TL_ADD(cb_width[i], pic_size_in_min_cb); //is_a0_available requires this
135 2528 }
136
137 2528 static void min_cb_nz_tl_init(TabList *l, VVCFrameContext *fc)
138 {
139 2528 const VVCPPS *pps = fc->ps.pps;
140
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int pic_size_in_min_cb = pps ? pps->min_cb_width * pps->min_cb_height : 0;
141 2528 const int changed = fc->tab.sz.pic_size_in_min_cb != pic_size_in_min_cb;
142
143 2528 tl_init(l, 0, changed);
144
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2528 times.
2528 TL_ADD(skip, pic_size_in_min_cb);
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2528 times.
2528 TL_ADD(imtf, pic_size_in_min_cb);
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2528 times.
2528 TL_ADD(ipm, pic_size_in_min_cb);
148
149
2/2
✓ Branch 0 taken 5056 times.
✓ Branch 1 taken 2528 times.
7584 for (int i = LUMA; i <= CHROMA; i++) {
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5056 times.
5056 TL_ADD(cqt_depth[i], pic_size_in_min_cb);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5056 times.
5056 TL_ADD(cb_pos_x[i], pic_size_in_min_cb);
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5056 times.
5056 TL_ADD(cb_pos_y[i], pic_size_in_min_cb);
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5056 times.
5056 TL_ADD(cb_height[i], pic_size_in_min_cb);
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5056 times.
5056 TL_ADD(cp_mv[i], pic_size_in_min_cb * MAX_CONTROL_POINTS);
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5056 times.
5056 TL_ADD(cpm[i], pic_size_in_min_cb);
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5056 times.
5056 TL_ADD(pcmf[i], pic_size_in_min_cb);
157 }
158 // For luma, qp can only change at the CU level, so the qp tab size is related to the CU.
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2528 times.
2528 TL_ADD(qp[LUMA], pic_size_in_min_cb);
160 2528 }
161
162 2528 static void min_pu_tl_init(TabList *l, VVCFrameContext *fc)
163 {
164 2528 const VVCPPS *pps = fc->ps.pps;
165
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int pic_size_in_min_pu = pps ? pps->min_pu_width * pps->min_pu_height : 0;
166 2528 const int changed = fc->tab.sz.pic_size_in_min_pu != pic_size_in_min_pu;
167
168 2528 tl_init(l, 1, changed);
169
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2528 times.
2528 TL_ADD(iaf, pic_size_in_min_pu);
171 2528 }
172
173 2528 static void min_pu_nz_tl_init(TabList *l, VVCFrameContext *fc)
174 {
175 2528 const VVCPPS *pps = fc->ps.pps;
176
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int pic_size_in_min_pu = pps ? pps->min_pu_width * pps->min_pu_height : 0;
177 2528 const int changed = fc->tab.sz.pic_size_in_min_pu != pic_size_in_min_pu;
178
179 2528 tl_init(l, 0, changed);
180
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2528 times.
2528 TL_ADD(msf, pic_size_in_min_pu);
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2528 times.
2528 TL_ADD(mmi, pic_size_in_min_pu);
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2528 times.
2528 TL_ADD(mvf, pic_size_in_min_pu);
184 2528 }
185
186 2528 static void min_tu_tl_init(TabList *l, VVCFrameContext *fc)
187 {
188 2528 const VVCPPS *pps = fc->ps.pps;
189
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int pic_size_in_min_tu = pps ? pps->min_tu_width * pps->min_tu_height : 0;
190 2528 const int changed = fc->tab.sz.pic_size_in_min_tu != pic_size_in_min_tu;
191
192 2528 tl_init(l, 1, changed);
193
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2528 times.
2528 TL_ADD(tu_joint_cbcr_residual_flag, pic_size_in_min_tu);
195
196
2/2
✓ Branch 0 taken 7584 times.
✓ Branch 1 taken 2528 times.
10112 for (int i = 0; i < VVC_MAX_SAMPLE_ARRAYS; i++) {
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7584 times.
7584 TL_ADD(tu_coded_flag[i], pic_size_in_min_tu);
198
199
2/2
✓ Branch 0 taken 15168 times.
✓ Branch 1 taken 7584 times.
22752 for (int vertical = 0; vertical < 2; vertical++)
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15168 times.
15168 TL_ADD(bs[vertical][i], pic_size_in_min_tu);
201 }
202 2528 }
203
204 2528 static void min_tu_nz_tl_init(TabList *l, VVCFrameContext *fc)
205 {
206 2528 const VVCPPS *pps = fc->ps.pps;
207
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int pic_size_in_min_tu = pps ? pps->min_tu_width * pps->min_tu_height : 0;
208 2528 const int changed = fc->tab.sz.pic_size_in_min_tu != pic_size_in_min_tu;
209
210 2528 tl_init(l, 0, changed);
211
212
2/2
✓ Branch 0 taken 5056 times.
✓ Branch 1 taken 2528 times.
7584 for (int i = LUMA; i <= CHROMA; i++) {
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5056 times.
5056 TL_ADD(tb_width[i], pic_size_in_min_tu);
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5056 times.
5056 TL_ADD(tb_height[i], pic_size_in_min_tu);
215 }
216
217
2/2
✓ Branch 0 taken 5056 times.
✓ Branch 1 taken 2528 times.
7584 for (int vertical = 0; vertical < 2; vertical++) {
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5056 times.
5056 TL_ADD(max_len_p[vertical], pic_size_in_min_tu);
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5056 times.
5056 TL_ADD(max_len_q[vertical], pic_size_in_min_tu);
220 }
221
222 // For chroma, considering the joint CbCr, the QP tab size is related to the TU.
223
2/2
✓ Branch 0 taken 5056 times.
✓ Branch 1 taken 2528 times.
7584 for (int i = CB; i < VVC_MAX_SAMPLE_ARRAYS; i++)
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5056 times.
5056 TL_ADD(qp[i], pic_size_in_min_tu);
225 2528 }
226
227 2528 static void pixel_buffer_nz_tl_init(TabList *l, VVCFrameContext *fc)
228 {
229 2528 const VVCSPS *sps = fc->ps.sps;
230 2528 const VVCPPS *pps = fc->ps.pps;
231
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int width = pps ? pps->width : 0;
232
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int height = pps ? pps->height : 0;
233
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int ctu_width = pps ? pps->ctb_width : 0;
234
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int ctu_height = pps ? pps->ctb_height : 0;
235
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int chroma_idc = sps ? sps->r->sps_chroma_format_idc : 0;
236
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int ps = sps ? sps->pixel_shift : 0;
237
2/2
✓ Branch 0 taken 1976 times.
✓ Branch 1 taken 552 times.
2528 const int c_end = chroma_idc ? VVC_MAX_SAMPLE_ARRAYS : 1;
238 7348 const int changed = fc->tab.sz.chroma_format_idc != chroma_idc ||
239
3/4
✓ Branch 0 taken 2282 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 2282 times.
✗ Branch 3 not taken.
2292 fc->tab.sz.width != width || fc->tab.sz.height != height ||
240
4/6
✓ Branch 0 taken 2292 times.
✓ Branch 1 taken 236 times.
✓ Branch 2 taken 2282 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2282 times.
✗ Branch 5 not taken.
7102 fc->tab.sz.ctu_width != ctu_width || fc->tab.sz.ctu_height != ctu_height ||
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2282 times.
2282 fc->tab.sz.pixel_shift != ps;
242
243 2528 tl_init(l, 0, changed);
244
245
2/2
✓ Branch 0 taken 6480 times.
✓ Branch 1 taken 2528 times.
9008 for (int c_idx = 0; c_idx < c_end; c_idx++) {
246
2/2
✓ Branch 0 taken 6070 times.
✓ Branch 1 taken 410 times.
6480 const int w = width >> (sps ? sps->hshift[c_idx] : 0);
247
2/2
✓ Branch 0 taken 6070 times.
✓ Branch 1 taken 410 times.
6480 const int h = height >> (sps ? sps->vshift[c_idx] : 0);
248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6480 times.
6480 TL_ADD(sao_pixel_buffer_h[c_idx], (w * 2 * ctu_height) << ps);
249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6480 times.
6480 TL_ADD(sao_pixel_buffer_v[c_idx], (h * 2 * ctu_width) << ps);
250 }
251
252
2/2
✓ Branch 0 taken 6480 times.
✓ Branch 1 taken 2528 times.
9008 for (int c_idx = 0; c_idx < c_end; c_idx++) {
253
2/2
✓ Branch 0 taken 6070 times.
✓ Branch 1 taken 410 times.
6480 const int w = width >> (sps ? sps->hshift[c_idx] : 0);
254
2/2
✓ Branch 0 taken 6070 times.
✓ Branch 1 taken 410 times.
6480 const int h = height >> (sps ? sps->vshift[c_idx] : 0);
255
2/2
✓ Branch 0 taken 3952 times.
✓ Branch 1 taken 2528 times.
6480 const int border_pixels = c_idx ? ALF_BORDER_CHROMA : ALF_BORDER_LUMA;
256
2/2
✓ Branch 0 taken 12960 times.
✓ Branch 1 taken 6480 times.
19440 for (int i = 0; i < 2; i++) {
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12960 times.
12960 TL_ADD(alf_pixel_buffer_h[c_idx][i], (w * border_pixels * ctu_height) << ps);
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12960 times.
12960 TL_ADD(alf_pixel_buffer_v[c_idx][i], h * ALF_PADDING_SIZE * ctu_width);
259 }
260 }
261 2528 }
262
263 2528 static void msm_tl_init(TabList *l, VVCFrameContext *fc)
264 {
265 2528 const VVCPPS *pps = fc->ps.pps;
266
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int w32 = pps ? AV_CEIL_RSHIFT(pps->width, 5) : 0;
267
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int h32 = pps ? AV_CEIL_RSHIFT(pps->height, 5) : 0;
268
2/2
✓ Branch 0 taken 2282 times.
✓ Branch 1 taken 246 times.
4810 const int changed = AV_CEIL_RSHIFT(fc->tab.sz.width, 5) != w32 ||
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2282 times.
2282 AV_CEIL_RSHIFT(fc->tab.sz.height, 5) != h32;
270
271 2528 tl_init(l, 1, changed);
272
273
2/2
✓ Branch 0 taken 5056 times.
✓ Branch 1 taken 2528 times.
7584 for (int i = LUMA; i <= CHROMA; i++)
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5056 times.
5056 TL_ADD(msm[i], w32 * h32);
275 2528 }
276
277 2528 static void ispmf_tl_init(TabList *l, VVCFrameContext *fc)
278 {
279 2528 const VVCPPS *pps = fc->ps.pps;
280
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int w64 = pps ? AV_CEIL_RSHIFT(pps->width, 6) : 0;
281
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int h64 = pps ? AV_CEIL_RSHIFT(pps->height, 6) : 0;
282
2/2
✓ Branch 0 taken 2282 times.
✓ Branch 1 taken 246 times.
4810 const int changed = AV_CEIL_RSHIFT(fc->tab.sz.width, 6) != w64 ||
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2282 times.
2282 AV_CEIL_RSHIFT(fc->tab.sz.height, 6) != h64;
284
285 2528 tl_init(l, 1, changed);
286
287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2528 times.
2528 TL_ADD(ispmf, w64 * h64);
288 2528 }
289
290 2528 static void ibc_tl_init(TabList *l, VVCFrameContext *fc)
291 {
292 2528 const VVCSPS *sps = fc->ps.sps;
293 2528 const VVCPPS *pps = fc->ps.pps;
294
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int ctu_height = pps ? pps->ctb_height : 0;
295
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int ctu_size = sps ? sps->ctb_size_y : 0;
296
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int ps = sps ? sps->pixel_shift : 0;
297
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int chroma_idc = sps ? sps->r->sps_chroma_format_idc : 0;
298
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 const int has_ibc = sps ? sps->r->sps_ibc_enabled_flag : 0;
299 7348 const int changed = fc->tab.sz.chroma_format_idc != chroma_idc ||
300
2/2
✓ Branch 0 taken 2282 times.
✓ Branch 1 taken 10 times.
2292 fc->tab.sz.ctu_height != ctu_height ||
301
4/4
✓ Branch 0 taken 2292 times.
✓ Branch 1 taken 236 times.
✓ Branch 2 taken 410 times.
✓ Branch 3 taken 1872 times.
5230 fc->tab.sz.ctu_size != ctu_size ||
302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 410 times.
410 fc->tab.sz.pixel_shift != ps;
303
304
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 410 times.
2528 fc->tab.sz.ibc_buffer_width = ctu_size ? 2 * MAX_CTU_SIZE * MAX_CTU_SIZE / ctu_size : 0;
305
306 2528 tl_init(l, has_ibc, changed);
307
308
2/2
✓ Branch 0 taken 7584 times.
✓ Branch 1 taken 2528 times.
10112 for (int i = LUMA; i < VVC_MAX_SAMPLE_ARRAYS; i++) {
309
2/2
✓ Branch 0 taken 6354 times.
✓ Branch 1 taken 1230 times.
7584 const int hs = sps ? sps->hshift[i] : 0;
310
2/2
✓ Branch 0 taken 6354 times.
✓ Branch 1 taken 1230 times.
7584 const int vs = sps ? sps->vshift[i] : 0;
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7584 times.
7584 TL_ADD(ibc_vir_buf[i], fc->tab.sz.ibc_buffer_width * ctu_size * ctu_height << ps >> hs >> vs);
312 }
313 2528 }
314
315 typedef void (*tl_init_fn)(TabList *l, VVCFrameContext *fc);
316
317 2528 static int frame_context_for_each_tl(VVCFrameContext *fc, int (*unary_fn)(TabList *l))
318 {
319 2528 const tl_init_fn init[] = {
320 ctu_nz_tl_init,
321 min_cb_tl_init,
322 min_cb_nz_tl_init,
323 min_pu_tl_init,
324 min_pu_nz_tl_init,
325 min_tu_tl_init,
326 min_tu_nz_tl_init,
327 pixel_buffer_nz_tl_init,
328 msm_tl_init,
329 ispmf_tl_init,
330 ibc_tl_init,
331 };
332
333
2/2
✓ Branch 0 taken 27808 times.
✓ Branch 1 taken 2528 times.
30336 for (int i = 0; i < FF_ARRAY_ELEMS(init); i++) {
334 TabList l;
335 int ret;
336
337 27808 init[i](&l, fc);
338 27808 ret = unary_fn(&l);
339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27808 times.
27808 if (ret < 0)
340 return ret;
341 }
342 2528 return 0;
343 }
344
345 1592 static void free_cus(VVCFrameContext *fc)
346 {
347
2/2
✓ Branch 0 taken 936 times.
✓ Branch 1 taken 656 times.
1592 if (fc->tab.cus) {
348
2/2
✓ Branch 0 taken 45792 times.
✓ Branch 1 taken 936 times.
46728 for (int i = 0; i < fc->tab.sz.ctu_count; i++)
349 45792 ff_vvc_ctu_free_cus(fc->tab.cus + i);
350 }
351 1592 }
352
353 656 static void pic_arrays_free(VVCFrameContext *fc)
354 {
355 656 free_cus(fc);
356 656 frame_context_for_each_tl(fc, tl_free);
357 656 ff_refstruct_pool_uninit(&fc->rpl_tab_pool);
358 656 ff_refstruct_pool_uninit(&fc->tab_dmvr_mvf_pool);
359
360 656 memset(&fc->tab.sz, 0, sizeof(fc->tab.sz));
361 656 }
362
363 936 static int pic_arrays_init(VVCContext *s, VVCFrameContext *fc)
364 {
365 936 const VVCSPS *sps = fc->ps.sps;
366 936 const VVCPPS *pps = fc->ps.pps;
367 936 const int ctu_count = pps->ctb_count;
368 936 const int pic_size_in_min_pu = pps->min_pu_width * pps->min_pu_height;
369 int ret;
370
371 936 free_cus(fc);
372
373 936 ret = frame_context_for_each_tl(fc, tl_create);
374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 936 times.
936 if (ret < 0)
375 return ret;
376
377 // for error handling case, we may call free_cus before VVC_TASK_STAGE_INIT, so we need to set cus to 0 here
378 936 memset(fc->tab.cus, 0, sizeof(*fc->tab.cus) * ctu_count);
379
380 936 memset(fc->tab.slice_idx, -1, sizeof(*fc->tab.slice_idx) * ctu_count);
381
382
2/2
✓ Branch 0 taken 246 times.
✓ Branch 1 taken 690 times.
936 if (fc->tab.sz.ctu_count != ctu_count) {
383 246 ff_refstruct_pool_uninit(&fc->rpl_tab_pool);
384 246 fc->rpl_tab_pool = ff_refstruct_pool_alloc(ctu_count * sizeof(RefPicListTab), 0);
385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 246 times.
246 if (!fc->rpl_tab_pool)
386 return AVERROR(ENOMEM);
387 }
388
389
2/2
✓ Branch 0 taken 246 times.
✓ Branch 1 taken 690 times.
936 if (fc->tab.sz.pic_size_in_min_pu != pic_size_in_min_pu) {
390 246 ff_refstruct_pool_uninit(&fc->tab_dmvr_mvf_pool);
391 246 fc->tab_dmvr_mvf_pool = ff_refstruct_pool_alloc(
392 pic_size_in_min_pu * sizeof(MvField), FF_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME);
393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 246 times.
246 if (!fc->tab_dmvr_mvf_pool)
394 return AVERROR(ENOMEM);
395 }
396
397 936 fc->tab.sz.ctu_count = pps->ctb_count;
398 936 fc->tab.sz.ctu_size = 1 << sps->ctb_log2_size_y << sps->ctb_log2_size_y;
399 936 fc->tab.sz.pic_size_in_min_cb = pps->min_cb_width * pps->min_cb_height;
400 936 fc->tab.sz.pic_size_in_min_pu = pic_size_in_min_pu;
401 936 fc->tab.sz.pic_size_in_min_tu = pps->min_tu_width * pps->min_tu_height;
402 936 fc->tab.sz.width = pps->width;
403 936 fc->tab.sz.height = pps->height;
404 936 fc->tab.sz.ctu_width = pps->ctb_width;
405 936 fc->tab.sz.ctu_height = pps->ctb_height;
406 936 fc->tab.sz.chroma_format_idc = sps->r->sps_chroma_format_idc;
407 936 fc->tab.sz.pixel_shift = sps->pixel_shift;
408
409 936 return 0;
410 }
411
412 936 int ff_vvc_per_frame_init(VVCFrameContext *fc)
413 {
414 936 return frame_context_for_each_tl(fc, tl_zero);
415 }
416
417 2985 static int min_positive(const int idx, const int diff, const int min_diff)
418 {
419
5/6
✓ Branch 0 taken 2595 times.
✓ Branch 1 taken 390 times.
✓ Branch 2 taken 996 times.
✓ Branch 3 taken 1599 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 996 times.
2985 return diff > 0 && (idx < 0 || diff < min_diff);
420 }
421
422 2985 static int max_negtive(const int idx, const int diff, const int max_diff)
423 {
424
5/6
✓ Branch 0 taken 1812 times.
✓ Branch 1 taken 1173 times.
✓ Branch 2 taken 671 times.
✓ Branch 3 taken 1141 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 671 times.
2985 return diff < 0 && (idx < 0 || diff > max_diff);
425 }
426
427 typedef int (*smvd_find_fxn)(const int idx, const int diff, const int old_diff);
428
429 3246 static int8_t smvd_find(const VVCFrameContext *fc, const SliceContext *sc, int lx, smvd_find_fxn find)
430 {
431 3246 const H266RawSliceHeader *rsh = sc->sh.r;
432 3246 const RefPicList *rpl = sc->rpl + lx;
433 3246 const int poc = fc->ref->poc;
434 3246 int8_t idx = -1;
435 3246 int old_diff = -1;
436
2/2
✓ Branch 0 taken 5970 times.
✓ Branch 1 taken 3246 times.
9216 for (int i = 0; i < rsh->num_ref_idx_active[lx]; i++) {
437
1/2
✓ Branch 0 taken 5970 times.
✗ Branch 1 not taken.
5970 if (!rpl->refs[i].is_lt) {
438 5970 int diff = poc - rpl->refs[i].poc;
439
2/2
✓ Branch 1 taken 2740 times.
✓ Branch 2 taken 3230 times.
5970 if (find(idx, diff, old_diff)) {
440 2740 idx = i;
441 2740 old_diff = diff;
442 }
443 }
444 }
445 3246 return idx;
446 }
447
448 1392 static void smvd_ref_idx(const VVCFrameContext *fc, SliceContext *sc)
449 {
450 1392 VVCSH *sh = &sc->sh;
451
2/2
✓ Branch 0 taken 1370 times.
✓ Branch 1 taken 22 times.
1392 if (IS_B(sh->r)) {
452 1370 sh->ref_idx_sym[0] = smvd_find(fc, sc, 0, min_positive);
453 1370 sh->ref_idx_sym[1] = smvd_find(fc, sc, 1, max_negtive);
454
4/4
✓ Branch 0 taken 1358 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 241 times.
✓ Branch 3 taken 1117 times.
1370 if (sh->ref_idx_sym[0] == -1 || sh->ref_idx_sym[1] == -1) {
455 253 sh->ref_idx_sym[0] = smvd_find(fc, sc, 0, max_negtive);
456 253 sh->ref_idx_sym[1] = smvd_find(fc, sc, 1, min_positive);
457 }
458 }
459 1392 }
460
461 1421 static void eps_free(SliceContext *slice)
462 {
463 1421 av_freep(&slice->eps);
464 1421 slice->nb_eps = 0;
465 1421 }
466
467 656 static void slices_free(VVCFrameContext *fc)
468 {
469
2/2
✓ Branch 0 taken 246 times.
✓ Branch 1 taken 410 times.
656 if (fc->slices) {
470
2/2
✓ Branch 0 taken 760 times.
✓ Branch 1 taken 246 times.
1006 for (int i = 0; i < fc->nb_slices_allocated; i++) {
471 760 SliceContext *slice = fc->slices[i];
472
1/2
✓ Branch 0 taken 760 times.
✗ Branch 1 not taken.
760 if (slice) {
473 760 ff_refstruct_unref(&slice->ref);
474 760 ff_refstruct_unref(&slice->sh.r);
475 760 eps_free(slice);
476 760 av_free(slice);
477 }
478 }
479 246 av_freep(&fc->slices);
480 }
481 656 fc->nb_slices_allocated = 0;
482 656 fc->nb_slices = 0;
483 656 }
484
485 1659 static int slices_realloc(VVCFrameContext *fc)
486 {
487 void *p;
488 1659 const int size = (fc->nb_slices_allocated + 1) * 3 / 2;
489
490
2/2
✓ Branch 0 taken 1312 times.
✓ Branch 1 taken 347 times.
1659 if (fc->nb_slices < fc->nb_slices_allocated)
491 1312 return 0;
492
493 347 p = av_realloc_array(fc->slices, size, sizeof(*fc->slices));
494
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 347 times.
347 if (!p)
495 return AVERROR(ENOMEM);
496
497 347 fc->slices = p;
498
2/2
✓ Branch 0 taken 760 times.
✓ Branch 1 taken 347 times.
1107 for (int i = fc->nb_slices_allocated; i < size; i++) {
499 760 fc->slices[i] = av_mallocz(sizeof(*fc->slices[0]));
500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 760 times.
760 if (!fc->slices[i]) {
501 fc->nb_slices_allocated = i;
502 return AVERROR(ENOMEM);
503 }
504 760 fc->slices[i]->slice_idx = i;
505 }
506 347 fc->nb_slices_allocated = size;
507
508 347 return 0;
509 }
510
511 2026 static int ep_init_cabac_decoder(SliceContext *sc, const int index,
512 const H2645NAL *nal, GetBitContext *gb, const CodedBitstreamUnit *unit)
513 {
514 2026 const H266RawSlice *slice = unit->content_ref;
515 2026 const H266RawSliceHeader *rsh = sc->sh.r;
516 2026 EntryPoint *ep = sc->eps + index;
517 int size;
518 int ret;
519
520
2/2
✓ Branch 0 taken 367 times.
✓ Branch 1 taken 1659 times.
2026 if (index < rsh->num_entry_points) {
521 367 int skipped = 0;
522 367 int64_t start = (gb->index >> 3);
523 367 int64_t end = start + rsh->sh_entry_point_offset_minus1[index] + 1;
524
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 361 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
367 while (skipped < nal->skipped_bytes && nal->skipped_bytes_pos[skipped] <= start + slice->header_size) {
525 skipped++;
526 }
527
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 361 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
367 while (skipped < nal->skipped_bytes && nal->skipped_bytes_pos[skipped] <= end + slice->header_size) {
528 end--;
529 skipped++;
530 }
531 367 size = end - start;
532 367 size = av_clip(size, 0, get_bits_left(gb) / 8);
533 } else {
534 1659 size = get_bits_left(gb) / 8;
535 }
536
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2026 times.
2026 av_assert0(gb->buffer + get_bits_count(gb) / 8 + size <= gb->buffer_end);
537 2026 ret = ff_init_cabac_decoder (&ep->cc, gb->buffer + get_bits_count(gb) / 8, size);
538
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2026 times.
2026 if (ret < 0)
539 return ret;
540 2026 skip_bits(gb, size * 8);
541 2026 return 0;
542 }
543
544 1659 static int slice_init_entry_points(SliceContext *sc,
545 VVCFrameContext *fc, const H2645NAL *nal, const CodedBitstreamUnit *unit)
546 {
547 1659 const VVCSH *sh = &sc->sh;
548 1659 const H266RawSlice *slice = unit->content_ref;
549 1659 int nb_eps = sh->r->num_entry_points + 1;
550 1659 int ctu_addr = 0;
551 GetBitContext gb;
552 int ret;
553
554
2/2
✓ Branch 0 taken 661 times.
✓ Branch 1 taken 998 times.
1659 if (sc->nb_eps != nb_eps) {
555 661 eps_free(sc);
556 661 sc->eps = av_calloc(nb_eps, sizeof(*sc->eps));
557
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 661 times.
661 if (!sc->eps)
558 return AVERROR(ENOMEM);
559 661 sc->nb_eps = nb_eps;
560 }
561
562 1659 ret = init_get_bits8(&gb, slice->data, slice->data_size);
563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1659 times.
1659 if (ret < 0)
564 return ret;
565
2/2
✓ Branch 0 taken 2026 times.
✓ Branch 1 taken 1659 times.
3685 for (int i = 0; i < sc->nb_eps; i++)
566 {
567 2026 EntryPoint *ep = sc->eps + i;
568
569 2026 ep->ctu_start = ctu_addr;
570
2/2
✓ Branch 0 taken 1659 times.
✓ Branch 1 taken 367 times.
2026 ep->ctu_end = (i + 1 == sc->nb_eps ? sh->num_ctus_in_curr_slice : sh->entry_point_start_ctu[i]);
571
572
2/2
✓ Branch 0 taken 45792 times.
✓ Branch 1 taken 2026 times.
47818 for (int j = ep->ctu_start; j < ep->ctu_end; j++) {
573 45792 const int rs = sc->sh.ctb_addr_in_curr_slice[j];
574 45792 fc->tab.slice_idx[rs] = sc->slice_idx;
575 }
576
577 2026 ret = ep_init_cabac_decoder(sc, i, nal, &gb, unit);
578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2026 times.
2026 if (ret < 0)
579 return ret;
580
581
2/2
✓ Branch 0 taken 367 times.
✓ Branch 1 taken 1659 times.
2026 if (i + 1 < sc->nb_eps)
582 367 ctu_addr = sh->entry_point_start_ctu[i];
583 }
584
585 1659 return 0;
586 }
587
588 2913 static VVCFrameContext* get_frame_context(const VVCContext *s, const VVCFrameContext *fc, const int delta)
589 {
590 2913 const int size = s->nb_fcs;
591 2913 const int idx = (fc - s->fcs + delta + size) % size;
592 2913 return s->fcs + idx;
593 }
594
595 4343 static int ref_frame(VVCFrame *dst, const VVCFrame *src)
596 {
597 int ret;
598
599 4343 ret = av_frame_ref(dst->frame, src->frame);
600
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4343 times.
4343 if (ret < 0)
601 return ret;
602
603 4343 ff_refstruct_replace(&dst->sps, src->sps);
604 4343 ff_refstruct_replace(&dst->pps, src->pps);
605
606 4343 ff_refstruct_replace(&dst->progress, src->progress);
607
608 4343 ff_refstruct_replace(&dst->tab_dmvr_mvf, src->tab_dmvr_mvf);
609
610 4343 ff_refstruct_replace(&dst->rpl_tab, src->rpl_tab);
611 4343 ff_refstruct_replace(&dst->rpl, src->rpl);
612 4343 ff_refstruct_replace(&dst->hwaccel_picture_private,
613 4343 src->hwaccel_picture_private);
614 4343 dst->nb_rpl_elems = src->nb_rpl_elems;
615
616 4343 dst->poc = src->poc;
617 4343 dst->ctb_count = src->ctb_count;
618
619 4343 dst->scaling_win = src->scaling_win;
620 4343 dst->ref_width = src->ref_width;
621 4343 dst->ref_height = src->ref_height;
622
623 4343 dst->flags = src->flags;
624 4343 dst->sequence = src->sequence;
625
626 4343 return 0;
627 }
628
629 656 static av_cold void frame_context_free(VVCFrameContext *fc)
630 {
631 656 slices_free(fc);
632
633 656 ff_refstruct_pool_uninit(&fc->tu_pool);
634 656 ff_refstruct_pool_uninit(&fc->cu_pool);
635
636
2/2
✓ Branch 0 taken 11152 times.
✓ Branch 1 taken 656 times.
11808 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
637 11152 ff_vvc_unref_frame(fc, &fc->DPB[i], ~0);
638 11152 av_frame_free(&fc->DPB[i].frame);
639 }
640
641 656 ff_vvc_frame_thread_free(fc);
642 656 pic_arrays_free(fc);
643 656 av_frame_free(&fc->output_frame);
644 656 ff_vvc_frame_ps_free(&fc->ps);
645 656 }
646
647 656 static av_cold int frame_context_init(VVCFrameContext *fc, AVCodecContext *avctx)
648 {
649
650 656 fc->log_ctx = avctx;
651
652 656 fc->output_frame = av_frame_alloc();
653
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 656 times.
656 if (!fc->output_frame)
654 return AVERROR(ENOMEM);
655
656
2/2
✓ Branch 0 taken 11152 times.
✓ Branch 1 taken 656 times.
11808 for (int j = 0; j < FF_ARRAY_ELEMS(fc->DPB); j++) {
657 11152 fc->DPB[j].frame = av_frame_alloc();
658
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11152 times.
11152 if (!fc->DPB[j].frame)
659 return AVERROR(ENOMEM);
660 }
661 656 fc->cu_pool = ff_refstruct_pool_alloc(sizeof(CodingUnit), 0);
662
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 656 times.
656 if (!fc->cu_pool)
663 return AVERROR(ENOMEM);
664
665 656 fc->tu_pool = ff_refstruct_pool_alloc(sizeof(TransformUnit), 0);
666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 656 times.
656 if (!fc->tu_pool)
667 return AVERROR(ENOMEM);
668
669 656 return 0;
670 }
671
672 936 static int frame_context_setup(VVCFrameContext *fc, VVCContext *s)
673 {
674 int ret;
675
676 936 fc->ref = NULL;
677
678 // copy refs from the last frame
679
3/4
✓ Branch 0 taken 854 times.
✓ Branch 1 taken 82 times.
✓ Branch 2 taken 854 times.
✗ Branch 3 not taken.
936 if (s->nb_frames && s->nb_fcs > 1) {
680 854 VVCFrameContext *prev = get_frame_context(s, fc, -1);
681
2/2
✓ Branch 0 taken 14518 times.
✓ Branch 1 taken 854 times.
15372 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
682 14518 ff_vvc_unref_frame(fc, &fc->DPB[i], ~0);
683
2/2
✓ Branch 0 taken 4343 times.
✓ Branch 1 taken 10175 times.
14518 if (prev->DPB[i].frame->buf[0]) {
684 4343 ret = ref_frame(&fc->DPB[i], &prev->DPB[i]);
685
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4343 times.
4343 if (ret < 0)
686 return ret;
687 }
688 }
689 }
690
691
4/4
✓ Branch 0 taken 929 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 845 times.
936 if (IS_IDR(s)) {
692 91 s->seq_decode = (s->seq_decode + 1) & 0xff;
693 91 ff_vvc_clear_refs(fc);
694 }
695
696 936 ret = pic_arrays_init(s, fc);
697
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 936 times.
936 if (ret < 0)
698 return ret;
699 936 ff_vvc_dsp_init(&fc->vvcdsp, fc->ps.sps->bit_depth);
700 936 ff_videodsp_init(&fc->vdsp, fc->ps.sps->bit_depth);
701 936 return 0;
702 }
703
704 936 static int frame_start(VVCContext *s, VVCFrameContext *fc, SliceContext *sc)
705 {
706 936 const VVCPH *ph = &fc->ps.ph;
707 936 const H266RawSliceHeader *rsh = sc->sh.r;
708 int ret;
709
710 // 8.3.1 Decoding process for picture order count
711
6/8
✓ Branch 0 taken 215 times.
✓ Branch 1 taken 721 times.
✓ Branch 2 taken 203 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 203 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 203 times.
✗ Branch 7 not taken.
936 if (!s->temporal_id && !ph->r->ph_non_ref_pic_flag && !(IS_RASL(s) || IS_RADL(s)))
712 203 s->poc_tid0 = ph->poc;
713
714
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 936 times.
936 if ((ret = ff_vvc_set_new_ref(s, fc, &fc->frame)) < 0)
715 goto fail;
716
717
4/4
✓ Branch 0 taken 929 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 845 times.
✓ Branch 3 taken 84 times.
936 if (!IS_IDR(s))
718 845 ff_vvc_bump_frame(s, fc);
719
720 936 av_frame_unref(fc->output_frame);
721
722
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 936 times.
936 if ((ret = ff_vvc_output_frame(s, fc, fc->output_frame,rsh->sh_no_output_of_prior_pics_flag, 0)) < 0)
723 goto fail;
724
725
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 936 times.
936 if ((ret = ff_vvc_frame_rpl(s, fc, sc)) < 0)
726 goto fail;
727
728
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 936 times.
936 if ((ret = ff_vvc_frame_thread_init(fc)) < 0)
729 goto fail;
730 936 return 0;
731 fail:
732 if (fc->ref)
733 ff_vvc_unref_frame(fc, fc->ref, ~0);
734 fc->ref = NULL;
735 return ret;
736 }
737
738 1659 static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc,
739 const CodedBitstreamUnit *unit, const int is_first_slice)
740 {
741 1659 VVCSH *sh = &sc->sh;
742 int ret;
743
744 1659 ret = ff_vvc_decode_sh(sh, &fc->ps, unit);
745
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1659 times.
1659 if (ret < 0)
746 return ret;
747
748 1659 ff_refstruct_replace(&sc->ref, unit->content_ref);
749
750
2/2
✓ Branch 0 taken 936 times.
✓ Branch 1 taken 723 times.
1659 if (is_first_slice) {
751 936 ret = frame_start(s, fc, sc);
752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 936 times.
936 if (ret < 0)
753 return ret;
754
1/2
✓ Branch 0 taken 723 times.
✗ Branch 1 not taken.
723 } else if (fc->ref) {
755
2/2
✓ Branch 0 taken 561 times.
✓ Branch 1 taken 162 times.
723 if (!IS_I(sh->r)) {
756 561 ret = ff_vvc_slice_rpl(s, fc, sc);
757
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 561 times.
561 if (ret < 0) {
758 av_log(fc->log_ctx, AV_LOG_WARNING,
759 "Error constructing the reference lists for the current slice.\n");
760 return ret;
761 }
762 }
763 } else {
764 av_log(fc->log_ctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
765 return ret;
766 }
767
768
2/2
✓ Branch 0 taken 1392 times.
✓ Branch 1 taken 267 times.
1659 if (!IS_I(sh->r))
769 1392 smvd_ref_idx(fc, sc);
770
771 1659 return 0;
772 }
773
774 83 static enum AVPixelFormat get_format(AVCodecContext *avctx, const VVCSPS *sps)
775 {
776 #define HWACCEL_MAX CONFIG_VVC_VAAPI_HWACCEL
777
778 83 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
779
780
3/3
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 6 times.
83 switch (sps->pix_fmt) {
781 5 case AV_PIX_FMT_YUV420P:
782 #if CONFIG_VVC_VAAPI_HWACCEL
783 5 *fmt++ = AV_PIX_FMT_VAAPI;
784 #endif
785 5 break;
786 72 case AV_PIX_FMT_YUV420P10:
787 #if CONFIG_VVC_VAAPI_HWACCEL
788 72 *fmt++ = AV_PIX_FMT_VAAPI;
789 #endif
790 72 break;
791 }
792
793 83 *fmt++ = sps->pix_fmt;
794 83 *fmt = AV_PIX_FMT_NONE;
795
796 83 return ff_get_format(avctx, pix_fmts);
797 }
798
799 936 static int export_frame_params(VVCContext *s, const VVCFrameContext *fc)
800 {
801 936 AVCodecContext *c = s->avctx;
802 936 const VVCSPS *sps = fc->ps.sps;
803 936 const VVCPPS *pps = fc->ps.pps;
804 int ret;
805
806 // Reset HW config if pix_fmt/w/h change.
807
5/6
✓ Branch 0 taken 854 times.
✓ Branch 1 taken 82 times.
✓ Branch 2 taken 853 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 853 times.
936 if (s->pix_fmt != sps->pix_fmt || c->coded_width != pps->width || c->coded_height != pps->height) {
808 83 c->coded_width = pps->width;
809 83 c->coded_height = pps->height;
810 83 ret = get_format(c, sps);
811
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83 times.
83 if (ret < 0)
812 return ret;
813
814 83 c->pix_fmt = ret;
815 83 s->pix_fmt = sps->pix_fmt;
816 }
817
818 936 c->width = pps->width - ((pps->r->pps_conf_win_left_offset + pps->r->pps_conf_win_right_offset) << sps->hshift[CHROMA]);
819 936 c->height = pps->height - ((pps->r->pps_conf_win_top_offset + pps->r->pps_conf_win_bottom_offset) << sps->vshift[CHROMA]);
820 936 c->has_b_frames = sps->r->sps_dpb_params.dpb_max_num_reorder_pics[sps->r->sps_max_sublayers_minus1];
821
822 936 return 0;
823 }
824
825 936 static int frame_setup(VVCFrameContext *fc, VVCContext *s)
826 {
827 936 int ret = ff_vvc_decode_frame_ps(&fc->ps, s);
828
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 936 times.
936 if (ret < 0)
829 return ret;
830
831 936 ret = frame_context_setup(fc, s);
832
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 936 times.
936 if (ret < 0)
833 return ret;
834
835 936 ret = export_frame_params(s, fc);
836
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 936 times.
936 if (ret < 0)
837 return ret;
838
839 936 return 0;
840 }
841
842 1659 static int decode_slice(VVCContext *s, VVCFrameContext *fc, const H2645NAL *nal, const CodedBitstreamUnit *unit)
843 {
844 int ret;
845 SliceContext *sc;
846 1659 const int is_first_slice = !fc->nb_slices;
847
848 1659 ret = slices_realloc(fc);
849
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1659 times.
1659 if (ret < 0)
850 return ret;
851
852 1659 sc = fc->slices[fc->nb_slices];
853
854 1659 s->vcl_unit_type = nal->type;
855
2/2
✓ Branch 0 taken 936 times.
✓ Branch 1 taken 723 times.
1659 if (is_first_slice) {
856 936 ret = frame_setup(fc, s);
857
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 936 times.
936 if (ret < 0)
858 return ret;
859 }
860
861 1659 ret = slice_start(sc, s, fc, unit, is_first_slice);
862
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1659 times.
1659 if (ret < 0)
863 return ret;
864
865 1659 ret = slice_init_entry_points(sc, fc, nal, unit);
866
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1659 times.
1659 if (ret < 0)
867 return ret;
868
869
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1659 times.
1659 if (s->avctx->hwaccel) {
870 if (is_first_slice) {
871 ret = FF_HW_CALL(s->avctx, start_frame, NULL, 0);
872 if (ret < 0)
873 return ret;
874 }
875
876 ret = FF_HW_CALL(s->avctx, decode_slice,
877 nal->raw_data, nal->raw_size);
878 if (ret < 0)
879 return ret;
880 }
881
882 1659 fc->nb_slices++;
883
884 1659 return 0;
885 }
886
887 3308 static int decode_nal_unit(VVCContext *s, VVCFrameContext *fc, const H2645NAL *nal, const CodedBitstreamUnit *unit)
888 {
889 int ret;
890
891 3308 s->temporal_id = nal->temporal_id;
892
893
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3308 times.
3308 if (nal->nuh_layer_id > 0) {
894 avpriv_report_missing_feature(fc->log_ctx,
895 "Decoding of multilayer bitstreams");
896 return AVERROR_PATCHWELCOME;
897 }
898
899
4/4
✓ Branch 0 taken 283 times.
✓ Branch 1 taken 1659 times.
✓ Branch 2 taken 315 times.
✓ Branch 3 taken 1051 times.
3308 switch (unit->type) {
900 283 case VVC_VPS_NUT:
901 case VVC_SPS_NUT:
902 case VVC_PPS_NUT:
903 /* vps, sps, sps cached by s->cbc */
904 283 break;
905 1659 case VVC_TRAIL_NUT:
906 case VVC_STSA_NUT:
907 case VVC_RADL_NUT:
908 case VVC_RASL_NUT:
909 case VVC_IDR_W_RADL:
910 case VVC_IDR_N_LP:
911 case VVC_CRA_NUT:
912 case VVC_GDR_NUT:
913 1659 ret = decode_slice(s, fc, nal, unit);
914
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1659 times.
1659 if (ret < 0)
915 return ret;
916 1659 break;
917 315 case VVC_PREFIX_APS_NUT:
918 case VVC_SUFFIX_APS_NUT:
919 315 ret = ff_vvc_decode_aps(&s->ps, unit);
920
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 315 times.
315 if (ret < 0)
921 return ret;
922 315 break;
923 }
924
925 3308 return 0;
926 }
927
928 936 static int decode_nal_units(VVCContext *s, VVCFrameContext *fc, AVPacket *avpkt)
929 {
930 936 const CodedBitstreamH266Context *h266 = s->cbc->priv_data;
931 936 CodedBitstreamFragment *frame = &s->current_frame;
932 936 int ret = 0;
933 936 s->last_eos = s->eos;
934 936 s->eos = 0;
935
936 936 ff_cbs_fragment_reset(frame);
937 936 ret = ff_cbs_read_packet(s->cbc, frame, avpkt);
938
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 936 times.
936 if (ret < 0) {
939 av_log(s->avctx, AV_LOG_ERROR, "Failed to read packet.\n");
940 return ret;
941 }
942 /* decode the NAL units */
943
2/2
✓ Branch 0 taken 3308 times.
✓ Branch 1 taken 936 times.
4244 for (int i = 0; i < frame->nb_units; i++) {
944 3308 const H2645NAL *nal = h266->common.read_packet.nals + i;
945 3308 const CodedBitstreamUnit *unit = frame->units + i;
946
947
2/4
✓ Branch 0 taken 3308 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3308 times.
3308 if (unit->type == VVC_EOB_NUT || unit->type == VVC_EOS_NUT) {
948 s->last_eos = 1;
949 } else {
950 3308 ret = decode_nal_unit(s, fc, nal, unit);
951
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3308 times.
3308 if (ret < 0) {
952 av_log(s->avctx, AV_LOG_WARNING,
953 "Error parsing NAL unit #%d.\n", i);
954 goto fail;
955 }
956 }
957 }
958 936 return 0;
959
960 fail:
961 if (fc->ref)
962 ff_vvc_report_frame_finished(fc->ref);
963 return ret;
964 }
965
966 866 static int set_output_format(const VVCContext *s, const AVFrame *output)
967 {
968 866 AVCodecContext *c = s->avctx;
969 int ret;
970
971
3/4
✓ Branch 0 taken 775 times.
✓ Branch 1 taken 91 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 775 times.
866 if (output->width != c->width || output->height != c->height) {
972
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 91 times.
91 if ((ret = ff_set_dimensions(c, output->width, output->height)) < 0)
973 return ret;
974 }
975 866 c->pix_fmt = output->format;
976 866 return 0;
977 }
978
979 936 static int wait_delayed_frame(VVCContext *s, AVFrame *output, int *got_output)
980 {
981 936 VVCFrameContext *delayed = get_frame_context(s, s->fcs, s->nb_frames - s->nb_delayed);
982 936 int ret = ff_vvc_frame_wait(s, delayed);
983
984
5/6
✓ Branch 0 taken 936 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 803 times.
✓ Branch 3 taken 133 times.
✓ Branch 4 taken 788 times.
✓ Branch 5 taken 15 times.
936 if (!ret && delayed->output_frame->buf[0] && output) {
985 788 av_frame_move_ref(output, delayed->output_frame);
986 788 ret = set_output_format(s, output);
987
1/2
✓ Branch 0 taken 788 times.
✗ Branch 1 not taken.
788 if (!ret)
988 788 *got_output = 1;
989 }
990 936 s->nb_delayed--;
991
992 936 return ret;
993 }
994
995 936 static int submit_frame(VVCContext *s, VVCFrameContext *fc, AVFrame *output, int *got_output)
996 {
997 int ret;
998
999
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 936 times.
936 if (s->avctx->hwaccel) {
1000 if (ret = FF_HW_SIMPLE_CALL(s->avctx, end_frame) < 0) {
1001 av_log(s->avctx, AV_LOG_ERROR,
1002 "Hardware accelerator failed to decode picture\n");
1003 ff_vvc_unref_frame(fc, fc->ref, ~0);
1004 return ret;
1005 }
1006 } else {
1007
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 936 times.
936 if (ret = ff_vvc_frame_submit(s, fc) < 0) {
1008 ff_vvc_report_frame_finished(fc->ref);
1009 return ret;
1010 }
1011 }
1012
1013 936 s->nb_frames++;
1014 936 s->nb_delayed++;
1015
1016
3/4
✓ Branch 0 taken 225 times.
✓ Branch 1 taken 711 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 225 times.
936 if (s->nb_delayed >= s->nb_fcs || s->avctx->hwaccel) {
1017
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 711 times.
711 if ((ret = wait_delayed_frame(s, output, got_output)) < 0)
1018 return ret;
1019 }
1020 936 return 0;
1021 }
1022
1023 257 static int get_decoded_frame(VVCContext *s, AVFrame *output, int *got_output)
1024 {
1025 int ret;
1026
2/2
✓ Branch 0 taken 170 times.
✓ Branch 1 taken 105 times.
275 while (s->nb_delayed) {
1027
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 170 times.
170 if ((ret = wait_delayed_frame(s, output, got_output)) < 0)
1028 return ret;
1029
2/2
✓ Branch 0 taken 152 times.
✓ Branch 1 taken 18 times.
170 if (*got_output)
1030 152 return 0;
1031 }
1032
1/2
✓ Branch 0 taken 105 times.
✗ Branch 1 not taken.
105 if (s->nb_frames) {
1033 //we still have frames cached in dpb.
1034 105 VVCFrameContext *last = get_frame_context(s, s->fcs, s->nb_frames - 1);
1035
1036 105 ret = ff_vvc_output_frame(s, last, output, 0, 1);
1037
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 105 times.
105 if (ret < 0)
1038 return ret;
1039
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 27 times.
105 if (ret) {
1040 78 *got_output = ret;
1041
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
78 if ((ret = set_output_format(s, output)) < 0)
1042 return ret;
1043 }
1044 }
1045 105 return 0;
1046 }
1047
1048 1193 static int vvc_decode_frame(AVCodecContext *avctx, AVFrame *output,
1049 int *got_output, AVPacket *avpkt)
1050 {
1051 1193 VVCContext *s = avctx->priv_data;
1052 VVCFrameContext *fc;
1053 int ret;
1054
1055
2/2
✓ Branch 0 taken 257 times.
✓ Branch 1 taken 936 times.
1193 if (!avpkt->size)
1056 257 return get_decoded_frame(s, output, got_output);
1057
1058 936 fc = get_frame_context(s, s->fcs, s->nb_frames);
1059
1060 936 fc->nb_slices = 0;
1061 936 fc->decode_order = s->nb_frames;
1062
1063 936 ret = decode_nal_units(s, fc, avpkt);
1064
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 936 times.
936 if (ret < 0)
1065 return ret;
1066
1067
2/4
✓ Branch 0 taken 936 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 936 times.
936 if (!fc->ft || !fc->ref)
1068 return avpkt->size;
1069
1070 936 ret = submit_frame(s, fc, output, got_output);
1071
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 936 times.
936 if (ret < 0)
1072 return ret;
1073
1074 936 return avpkt->size;
1075 }
1076
1077 82 static av_cold void vvc_decode_flush(AVCodecContext *avctx)
1078 {
1079 82 VVCContext *s = avctx->priv_data;
1080 82 int got_output = 0;
1081
1082
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 82 times.
137 while (s->nb_delayed)
1083 55 wait_delayed_frame(s, NULL, &got_output);
1084
1085
1/2
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
82 if (s->fcs) {
1086 82 VVCFrameContext *last = get_frame_context(s, s->fcs, s->nb_frames - 1);
1087 82 ff_vvc_flush_dpb(last);
1088 }
1089
1090 82 s->ps.sps_id_used = 0;
1091
1092 82 s->eos = 1;
1093 82 }
1094
1095 82 static av_cold int vvc_decode_free(AVCodecContext *avctx)
1096 {
1097 82 VVCContext *s = avctx->priv_data;
1098
1099 82 ff_cbs_fragment_free(&s->current_frame);
1100 82 vvc_decode_flush(avctx);
1101 82 ff_vvc_executor_free(&s->executor);
1102
1/2
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
82 if (s->fcs) {
1103
2/2
✓ Branch 0 taken 656 times.
✓ Branch 1 taken 82 times.
738 for (int i = 0; i < s->nb_fcs; i++)
1104 656 frame_context_free(s->fcs + i);
1105 82 av_free(s->fcs);
1106 }
1107 82 ff_vvc_ps_uninit(&s->ps);
1108 82 ff_cbs_close(&s->cbc);
1109
1110 82 return 0;
1111 }
1112
1113 55 static av_cold void init_default_scale_m(void)
1114 {
1115 55 memset(&ff_vvc_default_scale_m, 16, sizeof(ff_vvc_default_scale_m));
1116 55 }
1117
1118 #define VVC_MAX_DELAYED_FRAMES 16
1119 82 static av_cold int vvc_decode_init(AVCodecContext *avctx)
1120 {
1121 82 VVCContext *s = avctx->priv_data;
1122 static AVOnce init_static_once = AV_ONCE_INIT;
1123 82 const int cpu_count = av_cpu_count();
1124 82 const int delayed = FFMIN(cpu_count, VVC_MAX_DELAYED_FRAMES);
1125
1/2
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
82 int thread_count = avctx->thread_count ? avctx->thread_count : delayed;
1126 int ret;
1127
1128 82 s->avctx = avctx;
1129
1130 82 ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_VVC, avctx);
1131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 if (ret)
1132 return ret;
1133
1134
3/4
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 54 times.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
82 if (avctx->extradata_size > 0 && avctx->extradata) {
1135 28 ret = ff_cbs_read_extradata_from_codec(s->cbc, &s->current_frame, avctx);
1136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (ret < 0)
1137 return ret;
1138 }
1139
1140
1/2
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
82 s->nb_fcs = (avctx->flags & AV_CODEC_FLAG_LOW_DELAY) ? 1 : delayed;
1141 82 s->fcs = av_calloc(s->nb_fcs, sizeof(*s->fcs));
1142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 if (!s->fcs)
1143 return AVERROR(ENOMEM);
1144
1145
2/2
✓ Branch 0 taken 656 times.
✓ Branch 1 taken 82 times.
738 for (int i = 0; i < s->nb_fcs; i++) {
1146 656 VVCFrameContext *fc = s->fcs + i;
1147 656 ret = frame_context_init(fc, avctx);
1148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 656 times.
656 if (ret < 0)
1149 return ret;
1150 }
1151
1152
1/2
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
82 if (thread_count == 1)
1153 82 thread_count = 0;
1154 82 s->executor = ff_vvc_executor_alloc(s, thread_count);
1155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 if (!s->executor)
1156 return AVERROR(ENOMEM);
1157
1158 82 s->eos = 1;
1159 82 GDR_SET_RECOVERED(s);
1160 82 ff_thread_once(&init_static_once, init_default_scale_m);
1161
1162 82 s->pix_fmt = AV_PIX_FMT_NONE;
1163
1164 82 return 0;
1165 }
1166
1167 const FFCodec ff_vvc_decoder = {
1168 .p.name = "vvc",
1169 .p.long_name = NULL_IF_CONFIG_SMALL("VVC (Versatile Video Coding)"),
1170 .p.type = AVMEDIA_TYPE_VIDEO,
1171 .p.id = AV_CODEC_ID_VVC,
1172 .priv_data_size = sizeof(VVCContext),
1173 .init = vvc_decode_init,
1174 .close = vvc_decode_free,
1175 FF_CODEC_DECODE_CB(vvc_decode_frame),
1176 .flush = vvc_decode_flush,
1177 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS,
1178 .caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING | FF_CODEC_CAP_INIT_CLEANUP |
1179 FF_CODEC_CAP_AUTO_THREADS,
1180 .p.profiles = NULL_IF_CONFIG_SMALL(ff_vvc_profiles),
1181 .hw_configs = (const AVCodecHWConfigInternal *const []) {
1182 #if CONFIG_VVC_VAAPI_HWACCEL
1183 HWACCEL_VAAPI(vvc),
1184 #endif
1185 NULL
1186 },
1187 };
1188