Skip to content

Commit

Permalink
avcodec/vvc/dec: support apply film grain by decoder
Browse files Browse the repository at this point in the history
Signed-off-by: Wu Jianhua <toqsxw@outlook.com>
  • Loading branch information
QSXW committed Aug 6, 2024
1 parent b9efa77 commit 9e189c8
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 7 deletions.
1 change: 1 addition & 0 deletions libavcodec/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
85 changes: 80 additions & 5 deletions libavcodec/vvc/dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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--;

Expand Down
8 changes: 7 additions & 1 deletion libavcodec/vvc/dec.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "libavcodec/videodsp.h"
#include "libavcodec/vvc.h"
#include "libavcodec/h274.h"

#include "ps.h"
#include "dsp.h"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -221,6 +225,7 @@ typedef struct VVCContext {
CodedBitstreamFragment current_frame;

VVCParamSets ps;
H274FilmGrainDatabase h274db;

int temporal_id; ///< temporal_id_plus1 - 1
int poc_tid0;
Expand All @@ -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
Expand Down
14 changes: 13 additions & 1 deletion libavcodec/vvc/refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9e189c8

Please sign in to comment.