Coverage Report

Created: 2026-10-28 08:13

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