FFmpeg
Loading...
Searching...
No Matches
dec.c
Go to the documentation of this file.
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
26#include "libavcodec/decode.h"
28#include "libavcodec/hwconfig.h"
29#include "libavcodec/profiles.h"
30#include "libavutil/refstruct.h"
32#include "libavcodec/thread.h"
33#include "libavutil/cpu.h"
34#include "libavutil/mem.h"
35#include "libavutil/thread.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
47typedef struct Tab {
48 void **tab;
49 size_t size;
50} Tab;
51
52typedef struct TabList {
55
56 int zero;
58} TabList;
59
60#define TL_ADD(t, s) do { \
61 av_assert0(l->nb_tabs < TAB_MAX); \
62 l->tabs[l->nb_tabs].tab = (void**)&fc->tab.t; \
63 l->tabs[l->nb_tabs].size = sizeof(*fc->tab.t) * (s); \
64 l->nb_tabs++; \
65} while (0)
66
67static void tl_init(TabList *l, const int zero, const int realloc)
68{
69 l->nb_tabs = 0;
70 l->zero = zero;
71 l->realloc = realloc;
72}
73
74static int tl_free(TabList *l)
75{
76 for (int i = 0; i < l->nb_tabs; i++)
77 av_freep(l->tabs[i].tab);
78
79 return 0;
80}
81
82static int tl_create(TabList *l)
83{
84 if (l->realloc) {
85 tl_free(l);
86
87 for (int i = 0; i < l->nb_tabs; i++) {
88 Tab *t = l->tabs + i;
89 if (!t->size)
90 continue;
91 *t->tab = l->zero ? av_mallocz(t->size) : av_malloc(t->size);
92 if (!*t->tab)
93 return AVERROR(ENOMEM);
94 }
95 }
96 return 0;
97}
98
99static int tl_zero(TabList *l)
100{
101 if (l->zero) {
102 for (int i = 0; i < l->nb_tabs; i++) {
103 Tab *t = l->tabs + i;
104 memset(*t->tab, 0, t->size);
105 }
106 }
107 return 0;
108}
109
111{
112 const VVCSPS *sps = fc->ps.sps;
113 const VVCPPS *pps = fc->ps.pps;
114 const int ctu_size = sps ? (1 << sps->ctb_log2_size_y << sps->ctb_log2_size_y) : 0;
115 const int ctu_count = pps ? pps->ctb_count : 0;
116 const int changed = fc->tab.sz.ctu_count != ctu_count || fc->tab.sz.ctu_size != ctu_size;
117
118 tl_init(l, 0, changed);
119
120 TL_ADD(cus, ctu_count);
121 TL_ADD(ctus, ctu_count);
122 TL_ADD(deblock, ctu_count);
123 TL_ADD(sao, ctu_count);
124 TL_ADD(alf, ctu_count);
125 TL_ADD(slice_idx, ctu_count);
126 TL_ADD(coeffs, ctu_count * ctu_size * VVC_MAX_SAMPLE_ARRAYS);
127}
128
130{
131 const VVCPPS *pps = fc->ps.pps;
132 const int pic_size_in_min_cb = pps ? pps->min_cb_width * pps->min_cb_height : 0;
133 const int changed = fc->tab.sz.pic_size_in_min_cb != pic_size_in_min_cb;
134
135 tl_init(l, 1, changed);
136
137 TL_ADD(imf, pic_size_in_min_cb);
138
139 for (int i = LUMA; i <= CHROMA; i++)
140 TL_ADD(cb_width[i], pic_size_in_min_cb); //is_a0_available requires this
141}
142
144{
145 const VVCPPS *pps = fc->ps.pps;
146 const int pic_size_in_min_cb = pps ? pps->min_cb_width * pps->min_cb_height : 0;
147 const int changed = fc->tab.sz.pic_size_in_min_cb != pic_size_in_min_cb;
148
149 tl_init(l, 0, changed);
150
151 TL_ADD(skip, pic_size_in_min_cb);
152 TL_ADD(ipm, pic_size_in_min_cb);
153
154 for (int i = LUMA; i <= CHROMA; i++) {
155 TL_ADD(cqt_depth[i], pic_size_in_min_cb);
156 TL_ADD(cb_pos_x[i], pic_size_in_min_cb);
157 TL_ADD(cb_pos_y[i], pic_size_in_min_cb);
158 TL_ADD(cb_height[i], pic_size_in_min_cb);
159 TL_ADD(cp_mv[i], pic_size_in_min_cb * MAX_CONTROL_POINTS);
160 TL_ADD(cpm[i], pic_size_in_min_cb);
161 TL_ADD(pcmf[i], pic_size_in_min_cb);
162 }
163 // For luma, qp can only change at the CU level, so the qp tab size is related to the CU.
164 TL_ADD(qp[LUMA], pic_size_in_min_cb);
165}
166
168{
169 const VVCPPS *pps = fc->ps.pps;
170 const int pic_size_in_min_pu = pps ? pps->min_pu_width * pps->min_pu_height : 0;
171 const int changed = fc->tab.sz.pic_size_in_min_pu != pic_size_in_min_pu;
172
173 tl_init(l, 1, changed);
174
175 TL_ADD(iaf, pic_size_in_min_pu);
176}
177
179{
180 const VVCPPS *pps = fc->ps.pps;
181 const int pic_size_in_min_pu = pps ? pps->min_pu_width * pps->min_pu_height : 0;
182 const int changed = fc->tab.sz.pic_size_in_min_pu != pic_size_in_min_pu;
183
184 tl_init(l, 0, changed);
185
186 TL_ADD(msf, pic_size_in_min_pu);
187 TL_ADD(mmi, pic_size_in_min_pu);
188 TL_ADD(mvf, pic_size_in_min_pu);
189}
190
192{
193 const VVCPPS *pps = fc->ps.pps;
194 const int pic_size_in_min_tu = pps ? pps->min_tu_width * pps->min_tu_height : 0;
195 const int changed = fc->tab.sz.pic_size_in_min_tu != pic_size_in_min_tu;
196
197 tl_init(l, 1, changed);
198
199 TL_ADD(tu_joint_cbcr_residual_flag, pic_size_in_min_tu);
200
201 for (int i = 0; i < VVC_MAX_SAMPLE_ARRAYS; i++) {
202 TL_ADD(tu_coded_flag[i], pic_size_in_min_tu);
203
204 for (int vertical = 0; vertical < 2; vertical++)
205 TL_ADD(bs[vertical][i], pic_size_in_min_tu);
206 }
207}
208
210{
211 const VVCPPS *pps = fc->ps.pps;
212 const int pic_size_in_min_tu = pps ? pps->min_tu_width * pps->min_tu_height : 0;
213 const int changed = fc->tab.sz.pic_size_in_min_tu != pic_size_in_min_tu;
214
215 tl_init(l, 0, changed);
216
217 for (int i = LUMA; i <= CHROMA; i++) {
218 TL_ADD(tb_width[i], pic_size_in_min_tu);
219 TL_ADD(tb_height[i], pic_size_in_min_tu);
220 }
221
222 for (int vertical = 0; vertical < 2; vertical++) {
223 TL_ADD(max_len_p[vertical], pic_size_in_min_tu);
224 TL_ADD(max_len_q[vertical], pic_size_in_min_tu);
225 }
226
227 // For chroma, considering the joint CbCr, the QP tab size is related to the TU.
228 for (int i = CB; i < VVC_MAX_SAMPLE_ARRAYS; i++)
229 TL_ADD(qp[i], pic_size_in_min_tu);
230}
231
233{
234 const VVCSPS *sps = fc->ps.sps;
235 const VVCPPS *pps = fc->ps.pps;
236 const int width = pps ? pps->width : 0;
237 const int height = pps ? pps->height : 0;
238 const int ctu_width = pps ? pps->ctb_width : 0;
239 const int ctu_height = pps ? pps->ctb_height : 0;
240 const int chroma_idc = sps ? sps->r->sps_chroma_format_idc : 0;
241 const int ps = sps ? sps->pixel_shift : 0;
242 const int c_end = chroma_idc ? VVC_MAX_SAMPLE_ARRAYS : 1;
243 const int changed = fc->tab.sz.chroma_format_idc != chroma_idc ||
244 fc->tab.sz.width != width || fc->tab.sz.height != height ||
245 fc->tab.sz.ctu_width != ctu_width || fc->tab.sz.ctu_height != ctu_height ||
246 fc->tab.sz.pixel_shift != ps;
247
248 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 for (int c_idx = 0; c_idx < VVC_MAX_SAMPLE_ARRAYS; c_idx++) {
253 const int active = c_idx < c_end;
254 const int w = active ? width >> (sps ? sps->hshift[c_idx] : 0) : 0;
255 const int h = active ? height >> (sps ? sps->vshift[c_idx] : 0) : 0;
256 const int border_pixels = c_idx ? ALF_BORDER_CHROMA : ALF_BORDER_LUMA;
257 TL_ADD(sao_pixel_buffer_h[c_idx], (w * 2 * ctu_height) << ps);
258 TL_ADD(sao_pixel_buffer_v[c_idx], (h * 2 * ctu_width) << ps);
259 for (int i = 0; i < 2; i++) {
260 TL_ADD(alf_pixel_buffer_h[c_idx][i], (w * border_pixels * ctu_height) << ps);
261 TL_ADD(alf_pixel_buffer_v[c_idx][i], h * ALF_PADDING_SIZE * ctu_width);
262 }
263 }
264}
265
267{
268 const VVCPPS *pps = fc->ps.pps;
269 const int w32 = pps ? AV_CEIL_RSHIFT(pps->width, 5) : 0;
270 const int h32 = pps ? AV_CEIL_RSHIFT(pps->height, 5) : 0;
271 const int changed = AV_CEIL_RSHIFT(fc->tab.sz.width, 5) != w32 ||
272 AV_CEIL_RSHIFT(fc->tab.sz.height, 5) != h32;
273
274 tl_init(l, 1, changed);
275
276 for (int i = LUMA; i <= CHROMA; i++)
277 TL_ADD(msm[i], w32 * h32);
278}
279
281{
282 const VVCPPS *pps = fc->ps.pps;
283 const int w64 = pps ? AV_CEIL_RSHIFT(pps->width, 6) : 0;
284 const int h64 = pps ? AV_CEIL_RSHIFT(pps->height, 6) : 0;
285 const int changed = AV_CEIL_RSHIFT(fc->tab.sz.width, 6) != w64 ||
286 AV_CEIL_RSHIFT(fc->tab.sz.height, 6) != h64;
287
288 tl_init(l, 1, changed);
289
290 TL_ADD(ispmf, w64 * h64);
291}
292
294{
295 const VVCSPS *sps = fc->ps.sps;
296 const VVCPPS *pps = fc->ps.pps;
297 const int ctu_height = pps ? pps->ctb_height : 0;
298 const int ctu_size = sps ? sps->ctb_size_y : 0;
299 const int ps = sps ? sps->pixel_shift : 0;
300 const int chroma_idc = sps ? sps->r->sps_chroma_format_idc : 0;
301 const int has_ibc = sps ? sps->r->sps_ibc_enabled_flag : 0;
302 const int changed = fc->tab.sz.chroma_format_idc != chroma_idc ||
303 fc->tab.sz.ctu_height != ctu_height ||
304 fc->tab.sz.ctu_size != ctu_size * ctu_size ||
305 fc->tab.sz.pixel_shift != ps;
306
307 fc->tab.sz.ibc_buffer_width = ctu_size ? 2 * MAX_CTU_SIZE * MAX_CTU_SIZE / ctu_size : 0;
308
309 tl_init(l, has_ibc, changed);
310
311 for (int i = LUMA; i < VVC_MAX_SAMPLE_ARRAYS; i++) {
312 const int hs = sps ? sps->hshift[i] : 0;
313 const int vs = sps ? sps->vshift[i] : 0;
314 TL_ADD(ibc_vir_buf[i], fc->tab.sz.ibc_buffer_width * ctu_size * ctu_height << ps >> hs >> vs);
315 }
316}
317
318typedef void (*tl_init_fn)(TabList *l, VVCFrameContext *fc);
319
320static int frame_context_for_each_tl(VVCFrameContext *fc, int (*unary_fn)(TabList *l))
321{
322 const tl_init_fn init[] = {
334 };
335
336 for (int i = 0; i < FF_ARRAY_ELEMS(init); i++) {
337 TabList l;
338 int ret;
339
340 init[i](&l, fc);
341 ret = unary_fn(&l);
342 if (ret < 0)
343 return ret;
344 }
345 return 0;
346}
347
349{
350 if (fc->tab.cus) {
351 for (int i = 0; i < fc->tab.sz.ctu_count; i++)
352 ff_vvc_ctu_free_cus(fc->tab.cus + i);
353 }
354}
355
357{
358 free_cus(fc);
360 av_refstruct_pool_uninit(&fc->rpl_tab_pool);
361 av_refstruct_pool_uninit(&fc->tab_dmvr_mvf_pool);
362
363 memset(&fc->tab.sz, 0, sizeof(fc->tab.sz));
364}
365
367{
368 const VVCSPS *sps = fc->ps.sps;
369 const VVCPPS *pps = fc->ps.pps;
370 const int ctu_count = pps->ctb_count;
371 const int pic_size_in_min_pu = pps->min_pu_width * pps->min_pu_height;
372
373 free_cus(fc);
374
376 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 memset(fc->tab.cus, 0, sizeof(*fc->tab.cus) * ctu_count);
380
381 memset(fc->tab.slice_idx, -1, sizeof(*fc->tab.slice_idx) * ctu_count);
382
383 if (fc->tab.sz.ctu_count != ctu_count) {
384 av_refstruct_pool_uninit(&fc->rpl_tab_pool);
385 fc->rpl_tab_pool = av_refstruct_pool_alloc(ctu_count * sizeof(RefPicListTab), 0);
386 if (!fc->rpl_tab_pool)
387 goto fail;
388 }
389
390 if (fc->tab.sz.pic_size_in_min_pu != pic_size_in_min_pu) {
391 av_refstruct_pool_uninit(&fc->tab_dmvr_mvf_pool);
392 fc->tab_dmvr_mvf_pool = av_refstruct_pool_alloc(
393 pic_size_in_min_pu * sizeof(MvField), AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME);
394 if (!fc->tab_dmvr_mvf_pool)
395 goto fail;
396 }
397
398 fc->tab.sz.ctu_count = pps->ctb_count;
399 fc->tab.sz.ctu_size = 1 << sps->ctb_log2_size_y << sps->ctb_log2_size_y;
400 fc->tab.sz.pic_size_in_min_cb = pps->min_cb_width * pps->min_cb_height;
401 fc->tab.sz.pic_size_in_min_pu = pic_size_in_min_pu;
402 fc->tab.sz.pic_size_in_min_tu = pps->min_tu_width * pps->min_tu_height;
403 fc->tab.sz.width = pps->width;
404 fc->tab.sz.height = pps->height;
405 fc->tab.sz.ctu_width = pps->ctb_width;
406 fc->tab.sz.ctu_height = pps->ctb_height;
407 fc->tab.sz.chroma_format_idc = sps->r->sps_chroma_format_idc;
408 fc->tab.sz.pixel_shift = sps->pixel_shift;
409
410 return 0;
411
412fail:
413 fc->tab.sz.ctu_count = 0;
415 return AVERROR(ENOMEM);
416}
417
422
423static int min_positive(const int idx, const int diff, const int min_diff)
424{
425 return diff > 0 && (idx < 0 || diff < min_diff);
426}
427
428static int max_negtive(const int idx, const int diff, const int max_diff)
429{
430 return diff < 0 && (idx < 0 || diff > max_diff);
431}
432
433typedef int (*smvd_find_fxn)(const int idx, const int diff, const int old_diff);
434
435static int8_t smvd_find(const VVCFrameContext *fc, const SliceContext *sc, int lx, smvd_find_fxn find)
436{
437 const H266RawSliceHeader *rsh = sc->sh.r;
438 const RefPicList *rpl = sc->rpl + lx;
439 const int poc = fc->ref->poc;
440 int8_t idx = -1;
441 int old_diff = -1;
442 for (int i = 0; i < rsh->num_ref_idx_active[lx]; i++) {
443 if (!rpl->refs[i].is_lt) {
444 int diff = poc - rpl->refs[i].poc;
445 if (find(idx, diff, old_diff)) {
446 idx = i;
447 old_diff = diff;
448 }
449 }
450 }
451 return idx;
452}
453
455{
456 VVCSH *sh = &sc->sh;
457 if (IS_B(sh->r)) {
458 sh->ref_idx_sym[0] = smvd_find(fc, sc, 0, min_positive);
459 sh->ref_idx_sym[1] = smvd_find(fc, sc, 1, max_negtive);
460 if (sh->ref_idx_sym[0] == -1 || sh->ref_idx_sym[1] == -1) {
461 sh->ref_idx_sym[0] = smvd_find(fc, sc, 0, max_negtive);
462 sh->ref_idx_sym[1] = smvd_find(fc, sc, 1, min_positive);
463 }
464 }
465}
466
467static void eps_free(SliceContext *slice)
468{
469 av_freep(&slice->eps);
470 slice->nb_eps = 0;
471}
472
474{
475 if (fc->slices) {
476 for (int i = 0; i < fc->nb_slices_allocated; i++) {
477 SliceContext *slice = fc->slices[i];
478 if (slice) {
479 av_refstruct_unref(&slice->ref);
480 av_refstruct_unref(&slice->sh.r);
481 eps_free(slice);
482 av_free(slice);
483 }
484 }
485 av_freep(&fc->slices);
486 }
487 fc->nb_slices_allocated = 0;
488 fc->nb_slices = 0;
489}
490
492{
493 void *p;
494 const int size = (fc->nb_slices_allocated + 1) * 3 / 2;
495
496 if (fc->nb_slices < fc->nb_slices_allocated)
497 return 0;
498
499 p = av_realloc_array(fc->slices, size, sizeof(*fc->slices));
500 if (!p)
501 return AVERROR(ENOMEM);
502
503 fc->slices = p;
504 for (int i = fc->nb_slices_allocated; i < size; i++) {
505 fc->slices[i] = av_mallocz(sizeof(*fc->slices[0]));
506 if (!fc->slices[i]) {
507 fc->nb_slices_allocated = i;
508 return AVERROR(ENOMEM);
509 }
510 fc->slices[i]->slice_idx = i;
511 }
512 fc->nb_slices_allocated = size;
513
514 return 0;
515}
516
517static int get_ep_size(const H266RawSliceHeader *rsh, const GetByteContext *gb,
518 const H2645NAL *nal, const int header_size, const int ep_index)
519{
520 int size;
521
522 if (ep_index < rsh->num_entry_points) {
523 int skipped = 0;
524 int64_t start = bytestream2_tell(gb);
525 int64_t end = start + rsh->sh_entry_point_offset_minus1[ep_index] + 1;
526 while (skipped < nal->skipped_bytes && nal->skipped_bytes_pos[skipped] <= start + header_size) {
527 skipped++;
528 }
529 while (skipped < nal->skipped_bytes && nal->skipped_bytes_pos[skipped] <= end + header_size) {
530 end--;
531 skipped++;
532 }
533 size = end - start;
535 } else {
537 }
538 return size;
539}
540
542{
543 int ret;
544
546 ret = ff_init_cabac_decoder(&ep->cc, gb->buffer, size);
547 if (ret < 0)
548 return ret;
550 return 0;
551}
552
553static int ep_init(EntryPoint *ep, const int ctu_addr, const int ctu_end,
554 GetByteContext *gb, const int size)
555{
556 const int ret = ep_init_cabac_decoder(ep, gb, size);
557
558 if (ret < 0)
559 return ret;
560
561 ep->ctu_start = ctu_addr;
562 ep->ctu_end = ctu_end;
563
564 for (int c_idx = LUMA; c_idx <= CR; c_idx++)
565 ep->pp[c_idx].size = 0;
566
567 return 0;
568}
569
571 VVCFrameContext *fc, const H2645NAL *nal, const CodedBitstreamUnit *unit)
572{
573 const VVCSH *sh = &sc->sh;
574 const H266RawSlice *slice = unit->content_ref;
575 int nb_eps = sh->r->num_entry_points + 1;
576 int ctu_addr = 0;
578 int ret;
579
580 if (sc->nb_eps != nb_eps) {
581 eps_free(sc);
582 sc->eps = av_calloc(nb_eps, sizeof(*sc->eps));
583 if (!sc->eps)
584 return AVERROR(ENOMEM);
585 sc->nb_eps = nb_eps;
586 }
587
588 bytestream2_init(&gb, slice->data, slice->data_size);
589
590 for (int i = 0; i < sc->nb_eps; i++)
591 {
592 const int size = get_ep_size(sc->sh.r, &gb, nal, slice->header_size, i);
593 const int ctu_end = (i + 1 == sc->nb_eps ? sh->num_ctus_in_curr_slice : sh->entry_point_start_ctu[i]);
594 EntryPoint *ep = sc->eps + i;
595
596 ret = ep_init(ep, ctu_addr, ctu_end, &gb, size);
597 if (ret < 0)
598 return ret;
599
600 for (int j = ep->ctu_start; j < ep->ctu_end; j++) {
601 const int rs = sc->sh.ctb_addr_in_curr_slice[j];
602 fc->tab.slice_idx[rs] = sc->slice_idx;
603 }
604
605 if (i + 1 < sc->nb_eps)
606 ctu_addr = sh->entry_point_start_ctu[i];
607 }
608
609 return 0;
610}
611
613{
614 const int size = s->nb_fcs;
615 const int idx = (fc - s->fcs + delta + size) % size;
616 return s->fcs + idx;
617}
618
619static int ref_frame(VVCFrame *dst, const VVCFrame *src)
620{
621 int ret;
622
623 ret = av_frame_ref(dst->frame, src->frame);
624 if (ret < 0)
625 return ret;
626
627 av_refstruct_replace(&dst->sps, src->sps);
628 av_refstruct_replace(&dst->pps, src->pps);
629
630 if (src->needs_fg) {
631 ret = av_frame_ref(dst->frame_grain, src->frame_grain);
632 if (ret < 0)
633 return ret;
634
635 dst->needs_fg = src->needs_fg;
636 }
637
638 av_refstruct_replace(&dst->progress, src->progress);
639
640 av_refstruct_replace(&dst->tab_dmvr_mvf, src->tab_dmvr_mvf);
641
642 av_refstruct_replace(&dst->rpl_tab, src->rpl_tab);
643 av_refstruct_replace(&dst->rpl, src->rpl);
644 av_refstruct_replace(&dst->hwaccel_picture_private,
645 src->hwaccel_picture_private);
646 dst->nb_rpl_elems = src->nb_rpl_elems;
647
648 dst->poc = src->poc;
649 dst->ctb_count = src->ctb_count;
650
651 dst->scaling_win = src->scaling_win;
652 dst->ref_width = src->ref_width;
653 dst->ref_height = src->ref_height;
654
655 dst->flags = src->flags;
656 dst->sequence = src->sequence;
657
658 return 0;
659}
660
662{
664
665 av_refstruct_pool_uninit(&fc->tu_pool);
666 av_refstruct_pool_uninit(&fc->cu_pool);
667
668 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
669 ff_vvc_unref_frame(fc, &fc->DPB[i], ~0);
670 av_frame_free(&fc->DPB[i].frame);
671 av_frame_free(&fc->DPB[i].frame_grain);
672 }
673
676 av_frame_free(&fc->output_frame);
678 ff_vvc_sei_reset(&fc->sei);
679}
680
682{
683
684 fc->log_ctx = avctx;
685
686 fc->output_frame = av_frame_alloc();
687 if (!fc->output_frame)
688 return AVERROR(ENOMEM);
689
690 for (int j = 0; j < FF_ARRAY_ELEMS(fc->DPB); j++) {
691 fc->DPB[j].frame = av_frame_alloc();
692 if (!fc->DPB[j].frame)
693 return AVERROR(ENOMEM);
694
695 fc->DPB[j].frame_grain = av_frame_alloc();
696 if (!fc->DPB[j].frame_grain)
697 return AVERROR(ENOMEM);
698 }
699 fc->cu_pool = av_refstruct_pool_alloc(sizeof(CodingUnit), 0);
700 if (!fc->cu_pool)
701 return AVERROR(ENOMEM);
702
703 fc->tu_pool = av_refstruct_pool_alloc(sizeof(TransformUnit), 0);
704 if (!fc->tu_pool)
705 return AVERROR(ENOMEM);
706
707 return 0;
708}
709
711{
712 int ret;
713
714 // copy refs from the last frame
715 if (s->nb_frames && s->nb_fcs > 1) {
717 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
718 ff_vvc_unref_frame(fc, &fc->DPB[i], ~0);
719 if (prev->DPB[i].frame->buf[0]) {
720 ret = ref_frame(&fc->DPB[i], &prev->DPB[i]);
721 if (ret < 0)
722 return ret;
723 }
724 }
725
726 ret = ff_vvc_sei_replace(&fc->sei, &prev->sei);
727 if (ret < 0)
728 return ret;
729 }
730
731 if (IS_IDR(s)) {
733 }
734
736 if (ret < 0)
737 return ret;
738 ff_vvc_dsp_init(&fc->vvcdsp, fc->ps.sps->bit_depth);
739 ff_videodsp_init(&fc->vdsp, fc->ps.sps->bit_depth);
740 return 0;
741}
742
743/* SEI does not affect decoding, so we ignore the return value */
745{
746 CodedBitstreamFragment *frame = &s->current_frame;
747
748 for (int i = 0; i < frame->nb_units; i++) {
749 const CodedBitstreamUnit *unit = frame->units + i;
750
751 if (unit->type == VVC_PREFIX_SEI_NUT) {
752 int ret = ff_vvc_sei_decode(&fc->sei, unit->content_ref, fc);
753 if (ret < 0)
754 return;
755 }
756 }
757}
758
760{
761 AVFrame *out = fc->ref->frame;
762
763 return ff_h2645_sei_to_frame(out, &fc->sei.common, AV_CODEC_ID_VVC, s->avctx,
764 NULL, fc->ps.sps->bit_depth, fc->ps.sps->bit_depth, fc->ref->poc);
765}
766
768{
769 int ret;
770
771 fc->ref->needs_fg = (fc->sei.common.film_grain_characteristics &&
772 fc->sei.common.film_grain_characteristics->present ||
773 fc->sei.common.itut_t35.aom_film_grain.enable) &&
774 !(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) &&
775 !s->avctx->hwaccel;
776
777 if (fc->ref->needs_fg &&
778 (fc->sei.common.film_grain_characteristics &&
779 fc->sei.common.film_grain_characteristics->present &&
780 !ff_h274_film_grain_params_supported(fc->sei.common.film_grain_characteristics->model_id,
781 fc->ref->frame->format) ||
782 !av_film_grain_params_select(fc->ref->frame))) {
783 av_log_once(s->avctx, AV_LOG_WARNING, AV_LOG_DEBUG, &s->film_grain_warning_shown,
784 "Unsupported film grain parameters. Ignoring film grain.\n");
785 fc->ref->needs_fg = 0;
786 }
787
788 if (fc->ref->needs_fg) {
789 fc->ref->frame_grain->format = fc->ref->frame->format;
790 fc->ref->frame_grain->width = fc->ref->frame->width;
791 fc->ref->frame_grain->height = fc->ref->frame->height;
792
793 ret = ff_thread_get_buffer(s->avctx, fc->ref->frame_grain, 0);
794 if (ret < 0)
795 return ret;
796
797 return av_frame_copy_props(fc->ref->frame_grain, fc->ref->frame);
798 }
799
800 return 0;
801}
802
804{
805 const VVCPH *ph = &fc->ps.ph;
806 const H266RawSliceHeader *rsh = sc->sh.r;
807 int ret;
808
809 // 8.3.1 Decoding process for picture order count
810 if (!s->temporal_id && !ph->r->ph_non_ref_pic_flag && !(IS_RASL(s) || IS_RADL(s)))
811 s->poc_tid0 = ph->poc;
812
814
815 if ((ret = ff_vvc_set_new_ref(s, fc, &fc->frame)) < 0)
816 goto fail;
817
818 ret = set_side_data(s, fc);
819 if (ret < 0)
820 goto fail;
821
823 if (ret < 0)
824 goto fail;
825
826 if (!IS_IDR(s))
828
829 av_frame_unref(fc->output_frame);
830
831 /* Do not abort the current picture when outputting an earlier one
832 * fails: decode and submit it, then report the error afterwards. */
833 fc->output_error = ff_vvc_output_frame(s, fc, fc->output_frame,
835 if (fc->output_error < 0)
836 av_frame_unref(fc->output_frame);
837
838 if ((ret = ff_vvc_frame_rpl(s, fc, sc)) < 0)
839 goto fail;
840
841 if ((ret = ff_vvc_frame_thread_init(fc)) < 0)
842 goto fail;
843 return 0;
844fail:
845 if (fc->ref)
846 ff_vvc_unref_frame(fc, fc->ref, ~0);
847 fc->ref = NULL;
848 return ret;
849}
850
852 const CodedBitstreamUnit *unit, const int is_first_slice)
853{
854 VVCSH *sh = &sc->sh;
855 int ret;
856
857 ret = ff_vvc_decode_sh(sh, &fc->ps, unit);
858 if (ret < 0)
859 return ret;
860
861 if (is_first_slice) {
862 ret = frame_start(s, fc, sc);
863 if (ret < 0)
864 return ret;
865 } else if (fc->ref) {
866 if (!IS_I(sh->r)) {
867 ret = ff_vvc_slice_rpl(s, fc, sc);
868 if (ret < 0) {
869 av_log(fc->log_ctx, AV_LOG_WARNING,
870 "Error constructing the reference lists for the current slice.\n");
871 return ret;
872 }
873 }
874 } else {
875 av_log(fc->log_ctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
876 return ret;
877 }
878
879 if (!IS_I(sh->r))
880 smvd_ref_idx(fc, sc);
881
882 return 0;
883}
884
886{
887#define HWACCEL_MAX CONFIG_VVC_VAAPI_HWACCEL
888
889 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
890
891 switch (sps->pix_fmt) {
893#if CONFIG_VVC_VAAPI_HWACCEL
894 *fmt++ = AV_PIX_FMT_VAAPI;
895#endif
896 break;
898#if CONFIG_VVC_VAAPI_HWACCEL
899 *fmt++ = AV_PIX_FMT_VAAPI;
900#endif
901 break;
902 }
903
904 *fmt++ = sps->pix_fmt;
905 *fmt = AV_PIX_FMT_NONE;
906
907 return ff_get_format(avctx, pix_fmts);
908}
909
911{
912 AVCodecContext *c = s->avctx;
913 const VVCSPS *sps = fc->ps.sps;
914 const VVCPPS *pps = fc->ps.pps;
915
916 // Reset the format if pix_fmt/w/h change.
917 if (c->sw_pix_fmt != sps->pix_fmt || c->coded_width != pps->width || c->coded_height != pps->height) {
918 c->coded_width = pps->width;
919 c->coded_height = pps->height;
920 c->sw_pix_fmt = sps->pix_fmt;
921 c->pix_fmt = get_format(c, sps);
922 if (c->pix_fmt < 0)
923 return AVERROR_INVALIDDATA;
924 }
925
926 c->width = pps->width - ((pps->r->pps_conf_win_left_offset + pps->r->pps_conf_win_right_offset) << sps->hshift[CHROMA]);
927 c->height = pps->height - ((pps->r->pps_conf_win_top_offset + pps->r->pps_conf_win_bottom_offset) << sps->vshift[CHROMA]);
928
929 return 0;
930}
931
933{
935 if (ret < 0)
936 return ret;
937
939 if (ret < 0)
940 return ret;
941
943 if (ret < 0)
944 return ret;
945
946 return 0;
947}
948
950 const H2645NAL *nal, const CodedBitstreamUnit *unit)
951{
952 int ret;
953 SliceContext *sc;
954 const int is_first_slice = !fc->nb_slices;
955
957 if (ret < 0)
958 return ret;
959
960 sc = fc->slices[fc->nb_slices];
962
963 s->vcl_unit_type = nal->type;
964 if (is_first_slice) {
965 ret = frame_setup(fc, s);
966 if (ret < 0)
967 return ret;
968 } else {
969 const H266RawSlice *first = fc->slices[0]->ref;
970 const H266RawSlice *slice = sc->ref;
971
972 if (slice->ph != first->ph || slice->pps != first->pps) {
973 av_log(fc->log_ctx, AV_LOG_ERROR,
974 "Slice does not use the PH and PPS of the first slice.\n");
975 return AVERROR_INVALIDDATA;
976 }
977 }
978
979 ret = slice_start(sc, s, fc, unit, is_first_slice);
980 if (ret < 0)
981 return ret;
982
983 ret = slice_init_entry_points(sc, fc, nal, unit);
984 if (ret < 0)
985 return ret;
986
987 if (s->avctx->hwaccel) {
988 if (is_first_slice) {
989 ret = FF_HW_CALL(s->avctx, start_frame, buf_ref, NULL, 0);
990 if (ret < 0)
991 return ret;
992 }
993
994 ret = FF_HW_CALL(s->avctx, decode_slice,
995 nal->raw_data, nal->raw_size);
996 if (ret < 0)
997 return ret;
998 }
999
1000 fc->nb_slices++;
1001
1002 return 0;
1003}
1004
1006 const H2645NAL *nal, const CodedBitstreamUnit *unit)
1007{
1008 int ret;
1009
1010 s->temporal_id = nal->temporal_id;
1011
1012 if (nal->nuh_layer_id > 0) {
1014 "Decoding of multilayer bitstreams");
1015 return AVERROR_PATCHWELCOME;
1016 }
1017
1018 switch (unit->type) {
1019 case VVC_VPS_NUT:
1020 case VVC_SPS_NUT:
1021 case VVC_PPS_NUT:
1022 /* vps, sps, sps cached by s->cbc */
1023 break;
1024 case VVC_TRAIL_NUT:
1025 case VVC_STSA_NUT:
1026 case VVC_RADL_NUT:
1027 case VVC_RASL_NUT:
1028 case VVC_IDR_W_RADL:
1029 case VVC_IDR_N_LP:
1030 case VVC_CRA_NUT:
1031 case VVC_GDR_NUT:
1032 ret = decode_slice(s, fc, buf_ref, nal, unit);
1033 if (ret < 0)
1034 return ret;
1035 break;
1036 case VVC_PREFIX_APS_NUT:
1037 case VVC_SUFFIX_APS_NUT:
1038 ret = ff_vvc_decode_aps(&s->ps, unit);
1039 if (ret < 0)
1040 return ret;
1041 break;
1042 case VVC_PREFIX_SEI_NUT:
1043 /* handle by decode_prefix_sei() */
1044 break;
1045
1046 case VVC_SUFFIX_SEI_NUT:
1047 /* SEI does not affect decoding, so we ignore the return value*/
1048 if (fc)
1049 ff_vvc_sei_decode(&fc->sei, unit->content_ref, fc);
1050 break;
1051 }
1052
1053 return 0;
1054}
1055
1057{
1058 const CodedBitstreamH266Context *h266 = s->cbc->priv_data;
1059 CodedBitstreamFragment *frame = &s->current_frame;
1060 int ret = 0;
1061 s->last_eos = s->eos;
1062 s->eos = 0;
1063 fc->ref = NULL;
1064
1065 ff_cbs_fragment_reset(frame);
1066 ret = ff_cbs_read_packet(s->cbc, frame, avpkt);
1067 if (ret < 0) {
1068 av_log(s->avctx, AV_LOG_ERROR, "Failed to read packet.\n");
1069 return ret;
1070 }
1071 /* decode the NAL units */
1072 for (int i = 0; i < frame->nb_units; i++) {
1073 const H2645NAL *nal = h266->common.read_packet.nals + i;
1074 const CodedBitstreamUnit *unit = frame->units + i;
1075
1076 if (unit->type == VVC_EOB_NUT || unit->type == VVC_EOS_NUT) {
1077 s->last_eos = 1;
1078 } else {
1079 ret = decode_nal_unit(s, fc, avpkt->buf, nal, unit);
1080 if (ret < 0) {
1081 av_log(s->avctx, AV_LOG_WARNING,
1082 "Error parsing NAL unit #%d.\n", i);
1083 goto fail;
1084 }
1085 }
1086 }
1087 return 0;
1088
1089fail:
1090 if (fc->ref)
1092 return ret;
1093}
1094
1096{
1097 const AVFilmGrainParams *fgp;
1098 int ret;
1099
1100 if (fc->ref->needs_fg) {
1101 av_assert0(fc->ref->frame_grain->buf[0]);
1102 fgp = av_film_grain_params_select(fc->ref->frame);
1103 switch (fgp->type) {
1105 av_assert0(0);
1106 return AVERROR_BUG;
1108 ret = ff_h274_apply_film_grain(fc->ref->frame_grain, fc->ref->frame, fgp);
1109 if (ret < 0)
1110 return ret;
1111 break;
1113 ret = ff_aom_apply_film_grain(fc->ref->frame_grain, fc->ref->frame, fgp);
1114 if (ret < 0)
1115 return ret;
1116 break;
1117 }
1118 }
1119
1120 if (!s->avctx->hwaccel && s->avctx->err_recognition & AV_EF_CRCCHECK) {
1121 VVCSEI *sei = &fc->sei;
1122 if (sei->picture_hash.present) {
1123 ret = ff_h274_hash_init(&s->hash_ctx, sei->picture_hash.hash_type);
1124 if (ret < 0)
1125 return ret;
1126
1127 ret = ff_h274_hash_verify(s->hash_ctx, &sei->picture_hash, fc->ref->frame, fc->ps.pps->width, fc->ps.pps->height);
1128 av_log(s->avctx, ret < 0 ? AV_LOG_ERROR : AV_LOG_DEBUG,
1129 "Verifying checksum for frame with decode_order %d: %s\n",
1130 (int)fc->decode_order, ret < 0 ? "incorrect": "correct");
1131 if (ret < 0 && (s->avctx->err_recognition & AV_EF_EXPLODE))
1132 return ret;
1133 }
1134 }
1135
1136 return 0;
1137}
1138
1139static int wait_delayed_frame(VVCContext *s, AVFrame *output, int *got_output)
1140{
1141 VVCFrameContext *delayed = get_frame_context(s, s->fcs, s->nb_frames - s->nb_delayed);
1142 int ret = ff_vvc_frame_wait(s, delayed);
1143
1144 if (!ret) {
1145 ret = frame_end(s, delayed);
1146 if (ret >= 0 && delayed->output_error < 0)
1147 ret = delayed->output_error;
1148 if (ret >= 0 && delayed->output_frame->buf[0] && output) {
1149 av_frame_move_ref(output, delayed->output_frame);
1150 *got_output = 1;
1151 }
1152 }
1153 s->nb_delayed--;
1154
1155 return ret;
1156}
1157
1158static int submit_frame(VVCContext *s, VVCFrameContext *fc, AVFrame *output, int *got_output)
1159{
1160 int ret;
1161
1162 if (s->avctx->hwaccel) {
1163 if ((ret = FF_HW_SIMPLE_CALL(s->avctx, end_frame)) < 0) {
1164 av_log(s->avctx, AV_LOG_ERROR,
1165 "Hardware accelerator failed to decode picture\n");
1166 ff_vvc_unref_frame(fc, fc->ref, ~0);
1167 return ret;
1168 }
1169 } else {
1170 if ((ret = ff_vvc_frame_submit(s, fc)) < 0) {
1172 return ret;
1173 }
1174 }
1175
1176 s->nb_frames++;
1177 s->nb_delayed++;
1178
1179 if (s->nb_delayed >= s->nb_fcs || s->avctx->hwaccel) {
1180 if ((ret = wait_delayed_frame(s, output, got_output)) < 0)
1181 return ret;
1182 }
1183 return 0;
1184}
1185
1186static int get_decoded_frame(VVCContext *s, AVFrame *output, int *got_output)
1187{
1188 int ret;
1189 while (s->nb_delayed) {
1190 if ((ret = wait_delayed_frame(s, output, got_output)) < 0)
1191 return ret;
1192 if (*got_output)
1193 return 0;
1194 }
1195 if (s->nb_frames) {
1196 //we still have frames cached in dpb.
1197 VVCFrameContext *last = get_frame_context(s, s->fcs, s->nb_frames - 1);
1198
1199 ret = ff_vvc_output_frame(s, last, output, 0, 1);
1200 if (ret < 0)
1201 return ret;
1202 *got_output = ret;
1203 }
1204 return 0;
1205}
1206
1207static int vvc_decode_frame(AVCodecContext *avctx, AVFrame *output,
1208 int *got_output, AVPacket *avpkt)
1209{
1210 VVCContext *s = avctx->priv_data;
1212 int ret;
1213
1214 if (!avpkt->size)
1215 return get_decoded_frame(s, output, got_output);
1216
1217 fc = get_frame_context(s, s->fcs, s->nb_frames);
1218
1219 fc->nb_slices = 0;
1220 fc->decode_order = s->nb_frames;
1221
1222 ret = decode_nal_units(s, fc, avpkt);
1223 if (ret < 0)
1224 return ret;
1225
1226 if (!fc->ft || !fc->ref)
1227 return avpkt->size;
1228
1229 ret = submit_frame(s, fc, output, got_output);
1230 if (ret < 0)
1231 return ret;
1232
1233 return avpkt->size;
1234}
1235
1237{
1238 VVCContext *s = avctx->priv_data;
1239 int got_output = 0;
1240
1241 while (s->nb_delayed)
1242 wait_delayed_frame(s, NULL, &got_output);
1243
1244 if (s->fcs) {
1245 VVCFrameContext *last = get_frame_context(s, s->fcs, s->nb_frames - 1);
1246 ff_vvc_sei_reset(&last->sei);
1247 ff_vvc_flush_dpb(last);
1248 }
1249
1250 s->ps.sps_id_used = 0;
1251
1252 s->eos = 1;
1253}
1254
1256{
1257 VVCContext *s = avctx->priv_data;
1258
1259 ff_cbs_fragment_free(&s->current_frame);
1260 vvc_decode_flush(avctx);
1261 ff_vvc_executor_free(&s->executor);
1262 if (s->fcs) {
1263 for (int i = 0; i < s->nb_fcs; i++)
1264 frame_context_free(s->fcs + i);
1265 av_free(s->fcs);
1266 }
1267 ff_h274_hash_freep(&s->hash_ctx);
1268 ff_vvc_ps_uninit(&s->ps);
1269 ff_cbs_close(&s->cbc);
1270
1271 return 0;
1272}
1273
1275{
1276 memset(&ff_vvc_default_scale_m, 16, sizeof(ff_vvc_default_scale_m));
1277}
1278
1279#define VVC_MAX_DELAYED_FRAMES 16
1281{
1282 VVCContext *s = avctx->priv_data;
1283 static AVOnce init_static_once = AV_ONCE_INIT;
1284 const int cpu_count = av_cpu_count();
1285 const int delayed = FFMIN(cpu_count, VVC_MAX_DELAYED_FRAMES);
1286 int thread_count = avctx->thread_count ? avctx->thread_count : delayed;
1287 int ret;
1288
1289 s->avctx = avctx;
1290
1291 ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_VVC, avctx);
1292 if (ret)
1293 return ret;
1294
1295 if (avctx->extradata_size > 0 && avctx->extradata) {
1296 ret = ff_cbs_read_extradata_from_codec(s->cbc, &s->current_frame, avctx);
1297 if (ret < 0)
1298 return ret;
1299 }
1300
1301 s->nb_fcs = (avctx->flags & AV_CODEC_FLAG_LOW_DELAY) ? 1 : delayed;
1302 s->fcs = av_calloc(s->nb_fcs, sizeof(*s->fcs));
1303 if (!s->fcs)
1304 return AVERROR(ENOMEM);
1305
1306 for (int i = 0; i < s->nb_fcs; i++) {
1307 VVCFrameContext *fc = s->fcs + i;
1308 ret = frame_context_init(fc, avctx);
1309 if (ret < 0)
1310 return ret;
1311 }
1312
1313 if (thread_count == 1)
1314 thread_count = 0;
1315 s->executor = ff_vvc_executor_alloc(s, thread_count);
1316 if (!s->executor)
1317 return AVERROR(ENOMEM);
1318
1319 s->eos = 1;
1321 ff_thread_once(&init_static_once, init_default_scale_m);
1322
1323 return 0;
1324}
1325
1327 .p.name = "vvc",
1328 .p.long_name = NULL_IF_CONFIG_SMALL("VVC (Versatile Video Coding)"),
1329 .p.type = AVMEDIA_TYPE_VIDEO,
1330 .p.id = AV_CODEC_ID_VVC,
1331 .priv_data_size = sizeof(VVCContext),
1335 .flush = vvc_decode_flush,
1340 .hw_configs = (const AVCodecHWConfigInternal *const []) {
1341#if CONFIG_VVC_VAAPI_HWACCEL
1342 HWACCEL_VAAPI(vvc),
1343#endif
1344 NULL
1345 },
1346};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
const FFCodec ff_vvc_decoder
Definition dec.c:1326
int ff_aom_apply_film_grain(AVFrame *out, const AVFrame *in, const AVFilmGrainParams *params)
AOM film grain synthesis.
static FILE * out
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition bytestream.h:174
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition bytestream.h:192
int ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size)
Definition cabac.c:162
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static int FUNC sei(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEI *current)
static int FUNC sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
static int FUNC ph(CodedBitstreamContext *ctx, RWContext *rw, H266RawPH *current)
static int FUNC nal(CodedBitstreamContext *ctx, RWContext *rw, LCEVCRawNAL *current, int nal_unit_type)
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_CAP_EXPORTS_CROPPING
The decoder sets the cropping fields in the output frames manually.
#define FF_CODEC_DECODE_CB(func)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define FF_CODEC_CAP_AUTO_THREADS
Codec handles avctx->thread_count == 0 (auto) internally.
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define av_clip
Definition common.h:100
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
void ff_vvc_ctu_free_cus(CodingUnit **cus)
Definition ctu.c:2893
#define ALF_BORDER_CHROMA
Definition ctu.h:80
#define ALF_BORDER_LUMA
Definition ctu.h:79
#define MAX_CONTROL_POINTS
Definition ctu.h:67
#define ALF_PADDING_SIZE
Definition ctu.h:76
#define MAX_CTU_SIZE
Definition ctu.h:33
static const uint16_t fc[]
Definition dcaenc.h:43
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition decode.c:1248
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data,...
Definition defs.h:48
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition defs.h:51
static AVFrame * frame
uint64_t pps
Definition dovi_rpuenc.c:36
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
int ret
Definition ffplay.c:391
static int decode_slice(AVCodecContext *c, void *arg)
Definition ffv1dec.c:451
const AVFilmGrainParams * av_film_grain_params_select(const AVFrame *frame)
Select the most appropriate film grain parameters set for the frame, taking into account the frame's ...
@ AV_FILM_GRAIN_PARAMS_H274
The union is valid when interpreted as AVFilmGrainH274Params (codec.h274)
@ AV_FILM_GRAIN_PARAMS_AV1
The union is valid when interpreted as AVFilmGrainAOMParams (codec.aom)
@ AV_FILM_GRAIN_PARAMS_NONE
#define fail
Definition test.h:479
#define AV_CODEC_CAP_OTHER_THREADS
Codec supports multithreading through a method other than slice- or frame-level multithreading.
Definition codec.h:112
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition codec.h:79
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_EXPORT_DATA_FILM_GRAIN
Decoding only.
Definition avcodec.h:404
#define AV_CODEC_FLAG_LOW_DELAY
Force low delay.
Definition avcodec.h:314
@ AV_CODEC_ID_VVC
Definition codec_id.h:247
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:499
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition frame.c:526
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition frame.c:281
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:602
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:53
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition mem.c:318
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int ff_h2645_sei_to_frame(AVFrame *frame, H2645SEI *sei, enum AVCodecID codec_id, AVCodecContext *avctx, const H2645VUI *vui, unsigned bit_depth_luma, unsigned bit_depth_chroma, int seed)
Definition h2645_sei.c:519
#define CHROMA(h)
Definition h264dec.h:88
int ff_h274_hash_init(H274HashContext **ctx, const int type)
Definition h274.c:948
void ff_h274_hash_freep(H274HashContext **ctx)
Definition h274.c:935
int ff_h274_hash_verify(H274HashContext *c, const H274SEIPictureHash *hash, const AVFrame *frame, const int coded_width, const int coded_height)
Definition h274.c:983
int ff_h274_apply_film_grain(AVFrame *out_frame, const AVFrame *in_frame, const AVFilmGrainParams *params)
Definition h274.c:260
static int ff_h274_film_grain_params_supported(int model_id, enum AVPixelFormat pix_fmt)
Check whether ff_h274_apply_film_grain() supports the given parameter combination.
Definition h274.h:39
#define LUMA
Definition filter.c:31
#define CR
Definition filter.c:33
#define CB
Definition filter.c:32
#define IS_IDR(s)
Definition hevcdec.h:75
#define FF_HW_SIMPLE_CALL(avctx, function)
#define FF_HW_CALL(avctx, function,...)
#define HWACCEL_VAAPI(codec)
Definition hwconfig.h:72
static int zero(InterplayACMContext *s, unsigned ind, unsigned col)
#define HWACCEL_MAX
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition cbs.c:118
Multithreading API for decoders.
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition videodsp.c:39
@ VVC_MAX_SAMPLE_ARRAYS
Definition vvc.h:77
@ VVC_RASL_NUT
Definition vvc.h:32
@ VVC_SPS_NUT
Definition vvc.h:44
@ VVC_PREFIX_SEI_NUT
Definition vvc.h:52
@ VVC_EOS_NUT
Definition vvc.h:50
@ VVC_CRA_NUT
Definition vvc.h:38
@ VVC_IDR_N_LP
Definition vvc.h:37
@ VVC_TRAIL_NUT
Definition vvc.h:29
@ VVC_EOB_NUT
Definition vvc.h:51
@ VVC_SUFFIX_SEI_NUT
Definition vvc.h:53
@ VVC_RADL_NUT
Definition vvc.h:31
@ VVC_VPS_NUT
Definition vvc.h:43
@ VVC_IDR_W_RADL
Definition vvc.h:36
@ VVC_STSA_NUT
Definition vvc.h:30
@ VVC_GDR_NUT
Definition vvc.h:39
@ VVC_PREFIX_APS_NUT
Definition vvc.h:46
@ VVC_PPS_NUT
Definition vvc.h:45
@ VVC_SUFFIX_APS_NUT
Definition vvc.h:47
#define av_cold
Definition attributes.h:117
static atomic_int cpu_count
Definition cpu.c:57
int av_cpu_count(void)
Definition cpu.c:229
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define AVOnce
Definition thread.h:202
static int ff_thread_once(char *control, void(*routine)(void))
Definition thread.h:205
#define AV_ONCE_INIT
Definition thread.h:203
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
uint8_t w
Definition llvidencdsp.c:39
void av_log_once(void *avcl, int initial_level, int subsequent_level, int *state, const char *fmt,...)
Definition log.c:456
#define FFMIN(a, b)
Definition macros.h:49
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:548
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_VAAPI
Hardware acceleration through VA-API, data[3] contains a VASurfaceID.
Definition pixfmt.h:126
const AVProfile ff_vvc_profiles[]
Definition profiles.c:105
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
AVRefStructPool * av_refstruct_pool_alloc(size_t size, unsigned flags)
Equivalent to av_refstruct_pool_alloc(size, flags, NULL, NULL, NULL, NULL, NULL)
Definition refstruct.c:353
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition refstruct.c:121
void av_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
Definition refstruct.c:161
#define AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME
If this flag is set, the entries will be zeroed before being returned to the user (after the init or ...
Definition refstruct.h:221
static void av_refstruct_pool_uninit(AVRefStructPool **poolp)
Mark the pool as being available for freeing.
Definition refstruct.h:292
static void deblock(const RV60Context *s, AVFrame *frame, int xpos, int ypos, int size, int dpos)
Definition rv60dec.c:2167
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
Definition buffer.h:82
main external API structure.
Definition avcodec.h:443
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition avcodec.h:1589
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
void * priv_data
Definition avcodec.h:470
This structure describes how to handle film grain synthesis in video for specific codecs.
enum AVFilmGrainParamsType type
Specifies the codec for which this structure is valid.
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition frame.h:656
This structure stores compressed data.
Definition packet.h:587
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition packet.h:593
int size
Definition packet.h:611
Coded bitstream fragment structure, combining one or more units.
Definition cbs.h:129
CodedBitstreamH2645Context common
Definition cbs_h266.h:866
Coded bitstream unit structure.
Definition cbs.h:77
void * content_ref
If content is reference counted, a RefStruct reference backing content.
Definition cbs.h:119
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition cbs.h:81
CABACContext cc
Definition ctu.h:375
int ctu_end
Definition ctu.h:371
int ctu_start
Definition ctu.h:370
Palette pp[VVC_MAX_SAMPLE_ARRAYS]
Definition ctu.h:378
const uint8_t * buffer
Definition bytestream.h:34
H2645NAL * nals
Definition h2645_parse.h:83
uint32_t sh_entry_point_offset_minus1[VVC_MAX_ENTRY_POINTS]
Definition cbs_h266.h:835
uint8_t sh_no_output_of_prior_pics_flag
Definition cbs_h266.h:782
uint32_t num_entry_points
NumEntryPoints.
Definition cbs_h266.h:839
uint8_t num_ref_idx_active[2]
NumRefIdxActive[].
Definition cbs_h266.h:840
uint8_t * data
Definition cbs_h266.h:847
H266RawPictureHeader * ph
Definition cbs_h266.h:855
H266RawPPS * pps
RefStruct reference to referred-to PPS.
Definition cbs_h266.h:854
size_t header_size
Definition cbs_h266.h:849
size_t data_size
Definition cbs_h266.h:850
uint8_t size
Definition ctu.h:285
VVCRefPic refs[VVC_MAX_REF_ENTRIES]
Definition dec.h:58
VVCSH sh
Definition dec.h:115
int slice_idx
Definition dec.h:114
int nb_eps
Definition dec.h:117
void * ref
RefStruct reference, backing slice data.
Definition dec.h:119
struct EntryPoint * eps
Definition dec.h:116
RefPicList * rpl
Definition dec.h:118
Definition dec.c:52
Tab tabs[TAB_MAX]
Definition dec.c:53
int realloc
Definition dec.c:57
int nb_tabs
Definition dec.c:54
int zero
Definition dec.c:56
Definition dec.c:47
size_t size
Definition dec.c:49
void ** tab
Definition dec.c:48
VVCFrame DPB[VVC_MAX_DPB_SIZE+1]
Definition dec.h:126
int output_error
Definition dec.h:130
struct AVFrame * output_frame
Definition dec.h:129
VVCSEI sei
Definition dec.h:133
Definition dec.h:73
struct AVFrame * frame
Definition dec.h:74
Definition ps.h:147
Definition ps.h:92
int is_lt
Definition dec.h:50
int poc
Definition dec.h:49
Definition sei.h:36
Definition ps.h:238
const H266RawSliceHeader * r
RefStruct reference.
Definition ps.h:239
uint32_t entry_point_start_ctu[VVC_MAX_ENTRY_POINTS]
entry point start in ctu_addr
Definition ps.h:265
const uint32_t * ctb_addr_in_curr_slice
CtbAddrInCurrSlice.
Definition ps.h:244
uint32_t num_ctus_in_curr_slice
NumCtusInCurrSlice.
Definition ps.h:243
int8_t ref_idx_sym[2]
RefIdxSymL0, RefIdxSymL1.
Definition ps.h:248
Definition ps.h:58
#define av_free(p)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
#define height
Definition dsp.h:99
#define width
Definition dsp.h:99
void ff_vvc_frame_thread_free(VVCFrameContext *fc)
Definition thread.c:699
int ff_vvc_frame_thread_init(VVCFrameContext *fc)
Definition thread.c:739
av_cold FFExecutor * ff_vvc_executor_alloc(VVCContext *s, const int thread_count)
Definition thread.c:683
int ff_vvc_frame_wait(VVCContext *s, VVCFrameContext *fc)
Definition thread.c:833
int ff_vvc_frame_submit(VVCContext *s, VVCFrameContext *fc)
Definition thread.c:804
av_cold void ff_vvc_executor_free(FFExecutor **e)
Definition thread.c:694
int size
#define imf
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
float delta
static double c[64]
uint8_t ff_vvc_default_scale_m[64 *64]
Definition data.c:1641
static int slice_init_entry_points(SliceContext *sc, VVCFrameContext *fc, const H2645NAL *nal, const CodedBitstreamUnit *unit)
Definition dec.c:570
static void ibc_tl_init(TabList *l, VVCFrameContext *fc)
Definition dec.c:293
static av_cold int vvc_decode_init(AVCodecContext *avctx)
Definition dec.c:1280
static int check_film_grain(VVCContext *s, VVCFrameContext *fc)
Definition dec.c:767
static int8_t smvd_find(const VVCFrameContext *fc, const SliceContext *sc, int lx, smvd_find_fxn find)
Definition dec.c:435
static int export_frame_params(VVCContext *s, const VVCFrameContext *fc)
Definition dec.c:910
static void tl_init(TabList *l, const int zero, const int realloc)
Definition dec.c:67
static void min_cb_nz_tl_init(TabList *l, VVCFrameContext *fc)
Definition dec.c:143
static void min_tu_nz_tl_init(TabList *l, VVCFrameContext *fc)
Definition dec.c:209
static void pic_arrays_free(VVCFrameContext *fc)
Definition dec.c:356
static int slices_realloc(VVCFrameContext *fc)
Definition dec.c:491
static void min_cb_tl_init(TabList *l, VVCFrameContext *fc)
Definition dec.c:129
static VVCFrameContext * get_frame_context(const VVCContext *s, const VVCFrameContext *fc, const int delta)
Definition dec.c:612
static int ref_frame(VVCFrame *dst, const VVCFrame *src)
Definition dec.c:619
static int tl_zero(TabList *l)
Definition dec.c:99
static av_cold int vvc_decode_free(AVCodecContext *avctx)
Definition dec.c:1255
void(* tl_init_fn)(TabList *l, VVCFrameContext *fc)
Definition dec.c:318
static void msm_tl_init(TabList *l, VVCFrameContext *fc)
Definition dec.c:266
static enum AVPixelFormat get_format(AVCodecContext *avctx, const VVCSPS *sps)
Definition dec.c:885
#define TAB_MAX
Definition dec.c:45
static int pic_arrays_init(VVCContext *s, VVCFrameContext *fc)
Definition dec.c:366
static int min_positive(const int idx, const int diff, const int min_diff)
Definition dec.c:423
static void smvd_ref_idx(const VVCFrameContext *fc, SliceContext *sc)
Definition dec.c:454
static void ispmf_tl_init(TabList *l, VVCFrameContext *fc)
Definition dec.c:280
static int ep_init(EntryPoint *ep, const int ctu_addr, const int ctu_end, GetByteContext *gb, const int size)
Definition dec.c:553
static int tl_free(TabList *l)
Definition dec.c:74
static int submit_frame(VVCContext *s, VVCFrameContext *fc, AVFrame *output, int *got_output)
Definition dec.c:1158
static av_cold void vvc_decode_flush(AVCodecContext *avctx)
Definition dec.c:1236
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition dec.c:851
static void ctu_nz_tl_init(TabList *l, VVCFrameContext *fc)
Definition dec.c:110
static int wait_delayed_frame(VVCContext *s, AVFrame *output, int *got_output)
Definition dec.c:1139
static int decode_nal_unit(VVCContext *s, VVCFrameContext *fc, AVBufferRef *buf_ref, const H2645NAL *nal, const CodedBitstreamUnit *unit)
Definition dec.c:1005
static void eps_free(SliceContext *slice)
Definition dec.c:467
static void free_cus(VVCFrameContext *fc)
Definition dec.c:348
static int ep_init_cabac_decoder(EntryPoint *ep, GetByteContext *gb, const int size)
Definition dec.c:541
static av_cold void frame_context_free(VVCFrameContext *fc)
Definition dec.c:661
static int vvc_decode_frame(AVCodecContext *avctx, AVFrame *output, int *got_output, AVPacket *avpkt)
Definition dec.c:1207
static int frame_context_setup(VVCFrameContext *fc, VVCContext *s)
Definition dec.c:710
static int get_ep_size(const H266RawSliceHeader *rsh, const GetByteContext *gb, const H2645NAL *nal, const int header_size, const int ep_index)
Definition dec.c:517
static void min_tu_tl_init(TabList *l, VVCFrameContext *fc)
Definition dec.c:191
static void min_pu_nz_tl_init(TabList *l, VVCFrameContext *fc)
Definition dec.c:178
static int max_negtive(const int idx, const int diff, const int max_diff)
Definition dec.c:428
static int frame_setup(VVCFrameContext *fc, VVCContext *s)
Definition dec.c:932
static void decode_prefix_sei(VVCFrameContext *fc, VVCContext *s)
Definition dec.c:744
#define TL_ADD(t, s)
Definition dec.c:60
static av_cold int frame_context_init(VVCFrameContext *fc, AVCodecContext *avctx)
Definition dec.c:681
int(* smvd_find_fxn)(const int idx, const int diff, const int old_diff)
Definition dec.c:433
static int frame_start(VVCContext *s, VVCFrameContext *fc, SliceContext *sc)
Definition dec.c:803
int ff_vvc_per_frame_init(VVCFrameContext *fc)
Definition dec.c:418
static void min_pu_tl_init(TabList *l, VVCFrameContext *fc)
Definition dec.c:167
static int decode_nal_units(VVCContext *s, VVCFrameContext *fc, AVPacket *avpkt)
Definition dec.c:1056
static int tl_create(TabList *l)
Definition dec.c:82
static av_cold void init_default_scale_m(void)
Definition dec.c:1274
static void pixel_buffer_nz_tl_init(TabList *l, VVCFrameContext *fc)
Definition dec.c:232
static int frame_end(VVCContext *s, VVCFrameContext *fc)
Definition dec.c:1095
static int set_side_data(VVCContext *s, VVCFrameContext *fc)
Definition dec.c:759
static int decode_slice(VVCContext *s, VVCFrameContext *fc, AVBufferRef *buf_ref, const H2645NAL *nal, const CodedBitstreamUnit *unit)
Definition dec.c:949
static int frame_context_for_each_tl(VVCFrameContext *fc, int(*unary_fn)(TabList *l))
Definition dec.c:320
#define VVC_MAX_DELAYED_FRAMES
Definition dec.c:1279
static int get_decoded_frame(VVCContext *s, AVFrame *output, int *got_output)
Definition dec.c:1186
static void slices_free(VVCFrameContext *fc)
Definition dec.c:473
void ff_vvc_dsp_init(VVCDSPContext *vvcdsp, int bit_depth)
Definition dsp.c:86
int ff_vvc_decode_aps(VVCParamSets *ps, const CodedBitstreamUnit *unit)
Definition ps.c:1312
int ff_vvc_decode_frame_ps(struct VVCFrameContext *fc, struct VVCContext *s)
Definition ps.c:1083
void ff_vvc_ps_uninit(VVCParamSets *ps)
Definition ps.c:1115
void ff_vvc_frame_ps_free(VVCFrameParamSets *fps)
Definition ps.c:1105
int ff_vvc_decode_sh(VVCSH *sh, const VVCFrameParamSets *fps, const CodedBitstreamUnit *unit)
Definition ps.c:1519
#define IS_RASL(s)
Definition ps.h:35
#define GDR_SET_RECOVERED(s)
Definition ps.h:44
#define IS_I(rsh)
Definition ps.h:38
#define IS_RADL(s)
Definition ps.h:36
#define IS_B(rsh)
Definition ps.h:40
void ff_vvc_clear_refs(VVCFrameContext *fc)
Definition refs.c:86
int ff_vvc_frame_rpl(VVCContext *s, VVCFrameContext *fc, SliceContext *sc)
Definition refs.c:614
void ff_vvc_unref_frame(VVCFrameContext *fc, VVCFrame *frame, int flags)
Definition refs.c:44
void ff_vvc_bump_frame(VVCContext *s, VVCFrameContext *fc)
Definition refs.c:339
void ff_vvc_report_frame_finished(VVCFrame *frame)
Definition refs.c:638
int ff_vvc_output_frame(VVCContext *s, VVCFrameContext *fc, AVFrame *out, const int no_output_of_prior_pics_flag, int flush)
Definition refs.c:272
int ff_vvc_slice_rpl(VVCContext *s, VVCFrameContext *fc, SliceContext *sc)
Definition refs.c:553
int ff_vvc_set_new_ref(VVCContext *s, VVCFrameContext *fc, AVFrame **frame)
Definition refs.c:229
void ff_vvc_flush_dpb(VVCFrameContext *fc)
Definition refs.c:93
void ff_vvc_sei_reset(VVCSEI *s)
Definition sei.c:279
int ff_vvc_sei_decode(VVCSEI *s, const H266RawSEI *sei, const struct VVCFrameContext *fc)
Definition sei.c:201
int ff_vvc_sei_replace(VVCSEI *dst, const VVCSEI *src)
Definition sei.c:272