FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vvc/dec.c
Date: 2026-05-02 03:33:10
Exec Total Coverage
Lines: 634 766 82.8%
Functions: 56 56 100.0%
Branches: 379 565 67.1%

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