Coverage Report

Created: 2026-05-16 07:49

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