From 9e189c81e28bd335f9a627770c91f2ea51048d1c Mon Sep 17 00:00:00 2001 From: Wu Jianhua Date: Wed, 7 Aug 2024 02:05:25 +0800 Subject: [PATCH] avcodec/vvc/dec: support apply film grain by decoder Signed-off-by: Wu Jianhua --- libavcodec/Makefile | 1 + libavcodec/vvc/dec.c | 85 ++++++++++++++++++++++++++++++++++++++++--- libavcodec/vvc/dec.h | 8 +++- libavcodec/vvc/refs.c | 14 ++++++- 4 files changed, 101 insertions(+), 7 deletions(-) diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 262d0a3d3e..ab65aea252 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -795,6 +795,7 @@ OBJS-$(CONFIG_VPLAYER_DECODER) += textdec.o ass.o OBJS-$(CONFIG_VP9_V4L2M2M_DECODER) += v4l2_m2m_dec.o OBJS-$(CONFIG_VQA_DECODER) += vqavideo.o OBJS-$(CONFIG_VQC_DECODER) += vqcdec.o +OBJS-$(CONFIG_VVC_DECODER) += aom_film_grain.o h274.o OBJS-$(CONFIG_WADY_DPCM_DECODER) += dpcm.o OBJS-$(CONFIG_WAVARC_DECODER) += wavarc.o OBJS-$(CONFIG_WAVPACK_DECODER) += wavpack.o wavpackdata.o dsd.o diff --git a/libavcodec/vvc/dec.c b/libavcodec/vvc/dec.c index d710c8c4e4..8d36dc4fe4 100644 --- a/libavcodec/vvc/dec.c +++ b/libavcodec/vvc/dec.c @@ -24,9 +24,12 @@ #include "libavcodec/decode.h" #include "libavcodec/profiles.h" #include "libavcodec/refstruct.h" +#include "libavcodec/aom_film_grain.h" +#include "libavcodec/thread.h" #include "libavutil/cpu.h" #include "libavutil/mem.h" #include "libavutil/thread.h" +#include "libavutil/film_grain_params.h" #include "dec.h" #include "ctu.h" @@ -597,6 +600,11 @@ static int ref_frame(VVCFrame *dst, const VVCFrame *src) if (ret < 0) return ret; + if (src->needs_fg) { + ret = av_frame_ref(dst->frame_grain, src->frame_grain); + dst->needs_fg = src->needs_fg; + } + ff_refstruct_replace(&dst->sps, src->sps); ff_refstruct_replace(&dst->pps, src->pps); @@ -631,6 +639,7 @@ static av_cold void frame_context_free(VVCFrameContext *fc) for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) { ff_vvc_unref_frame(fc, &fc->DPB[i], ~0); av_frame_free(&fc->DPB[i].frame); + av_frame_free(&fc->DPB[i].frame_grain); } ff_vvc_frame_thread_free(fc); @@ -653,6 +662,10 @@ static av_cold int frame_context_init(VVCFrameContext *fc, AVCodecContext *avctx fc->DPB[j].frame = av_frame_alloc(); if (!fc->DPB[j].frame) return AVERROR(ENOMEM); + + fc->DPB[j].frame_grain = av_frame_alloc(); + if (!fc->DPB[j].frame_grain) + return AVERROR(ENOMEM); } fc->cu_pool = ff_refstruct_pool_alloc(sizeof(CodingUnit), 0); if (!fc->cu_pool) @@ -707,6 +720,33 @@ static int set_side_data(VVCContext *s, VVCFrameContext *fc) NULL, fc->ps.sps->bit_depth, fc->ps.sps->bit_depth, fc->ref->poc); } +static int check_film_grain(VVCContext *s, VVCFrameContext *fc) +{ + fc->ref->needs_fg = (fc->sei.common.film_grain_characteristics.present || + fc->sei.common.aom_film_grain.enable) && + !(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) && + !s->avctx->hwaccel; + + if (fc->ref->needs_fg && + (fc->sei.common.film_grain_characteristics.present && + !ff_h274_film_grain_params_supported(fc->sei.common.film_grain_characteristics.model_id, + fc->ref->frame->format) || + !av_film_grain_params_select(fc->ref->frame))) { + av_log_once(s->avctx, AV_LOG_WARNING, AV_LOG_DEBUG, &s->film_grain_warning_shown, + "Unsupported film grain parameters. Ignoring film grain.\n"); + fc->ref->needs_fg = 0; + } + + if (fc->ref->needs_fg) { + fc->ref->frame_grain->format = fc->ref->frame->format; + fc->ref->frame_grain->width = fc->ref->frame->width; + fc->ref->frame_grain->height = fc->ref->frame->height; + return ff_thread_get_buffer(s->avctx, fc->ref->frame_grain, 0); + } + + return 0; +} + static int frame_start(VVCContext *s, VVCFrameContext *fc, SliceContext *sc) { const VVCPH *ph = &fc->ps.ph; @@ -724,6 +764,10 @@ static int frame_start(VVCContext *s, VVCFrameContext *fc, SliceContext *sc) if (ret < 0) goto fail; + ret = check_film_grain(s, fc); + if (ret < 0) + goto fail; + if (!IS_IDR(s)) ff_vvc_bump_frame(s, fc); @@ -954,16 +998,47 @@ static int set_output_format(const VVCContext *s, const AVFrame *output) return 0; } +static int frame_end(VVCContext *s, VVCFrameContext *fc) +{ + const AVFilmGrainParams *fgp; + int ret = 0; + + if (fc->ref->needs_fg) { + av_assert0(fc->ref->frame_grain->buf[0]); + fgp = av_film_grain_params_select(fc->ref->frame); + switch (fgp->type) { + case AV_FILM_GRAIN_PARAMS_NONE: + av_assert0(0); + return AVERROR_BUG; + case AV_FILM_GRAIN_PARAMS_H274: + ret = ff_h274_apply_film_grain(fc->ref->frame_grain, fc->ref->frame, + &s->h274db, fgp); + break; + case AV_FILM_GRAIN_PARAMS_AV1: + ret = ff_aom_apply_film_grain(fc->ref->frame_grain, fc->ref->frame, fgp); + break; + } + } + + return ret; +} + static int wait_delayed_frame(VVCContext *s, AVFrame *output, int *got_output) { VVCFrameContext *delayed = get_frame_context(s, s->fcs, s->nb_frames - s->nb_delayed); int ret = ff_vvc_frame_wait(s, delayed); - if (!ret && delayed->output_frame->buf[0] && output) { - av_frame_move_ref(output, delayed->output_frame); - ret = set_output_format(s, output); - if (!ret) - *got_output = 1; + if (!ret) { + ret = frame_end(s, delayed); + if (ret < 0) + return ret; + + if (delayed->output_frame->buf[0] && output) { + av_frame_move_ref(output, delayed->output_frame); + ret = set_output_format(s, output); + if (!ret) + *got_output = 1; + } } s->nb_delayed--; diff --git a/libavcodec/vvc/dec.h b/libavcodec/vvc/dec.h index 8f8224926c..fd73a110bb 100644 --- a/libavcodec/vvc/dec.h +++ b/libavcodec/vvc/dec.h @@ -26,6 +26,7 @@ #include "libavcodec/videodsp.h" #include "libavcodec/vvc.h" +#include "libavcodec/h274.h" #include "ps.h" #include "dsp.h" @@ -71,12 +72,15 @@ typedef struct VVCWindow { typedef struct VVCFrame { struct AVFrame *frame; - + struct AVFrame *frame_grain; const VVCSPS *sps; ///< RefStruct reference const VVCPPS *pps; ///< RefStruct reference struct MvField *tab_dmvr_mvf; ///< RefStruct reference RefPicListTab **rpl_tab; ///< RefStruct reference RefPicListTab *rpl; ///< RefStruct reference + + int needs_fg; ///< 1 if grain needs to be applied by the decoder + int nb_rpl_elems; int ctb_count; @@ -221,6 +225,7 @@ typedef struct VVCContext { CodedBitstreamFragment current_frame; VVCParamSets ps; + H274FilmGrainDatabase h274db; int temporal_id; ///< temporal_id_plus1 - 1 int poc_tid0; @@ -231,6 +236,7 @@ typedef struct VVCContext { enum VVCNALUnitType vcl_unit_type; int no_output_before_recovery_flag; ///< NoOutputBeforeRecoveryFlag int gdr_recovery_point_poc; ///< recoveryPointPocVal + int film_grain_warning_shown; /** * Sequence counters for decoded and output frames, so that old diff --git a/libavcodec/vvc/refs.c b/libavcodec/vvc/refs.c index c1fc6132c2..32c1d2f2f8 100644 --- a/libavcodec/vvc/refs.c +++ b/libavcodec/vvc/refs.c @@ -52,6 +52,12 @@ void ff_vvc_unref_frame(VVCFrameContext *fc, VVCFrame *frame, int flags) frame->flags &= ~flags; if (!frame->flags) { av_frame_unref(frame->frame); + + if (frame->needs_fg) { + av_frame_unref(frame->frame_grain); + frame->needs_fg = 0; + } + ff_refstruct_unref(&frame->sps); ff_refstruct_unref(&frame->pps); ff_refstruct_unref(&frame->progress); @@ -246,7 +252,13 @@ int ff_vvc_output_frame(VVCContext *s, VVCFrameContext *fc, AVFrame *out, const if (nb_output) { VVCFrame *frame = &fc->DPB[min_idx]; - ret = av_frame_ref(out, frame->frame); + ret = av_frame_ref(out, frame->needs_fg ? frame->frame_grain : frame->frame); + if (ret < 0) + return ret; + + if (frame->needs_fg && (ret = av_frame_copy_props(frame->frame_grain, frame->frame)) < 0) + return ret; + if (frame->flags & VVC_FRAME_FLAG_BUMPING) ff_vvc_unref_frame(fc, frame, VVC_FRAME_FLAG_OUTPUT | VVC_FRAME_FLAG_BUMPING); else