History

avcodec/vvc/dec: support applying film grain by the decoder

Wu Jianhua committed 1 year ago

Commit a8d949b1 parent 1608d604 files · +104 −6Browse files

Showing the changes to libavcodec (4 of 4 files). Show all

Unified · Split

4 files
  1. libavcodec/Makefile +1 −1
  2. libavcodec/vvc/dec.c +86 −3
  3. libavcodec/vvc/dec.h +7 −1
  4. libavcodec/vvc/refs.c +10 −1
libavcodec/Makefile+1−1
@@ -811,7 +811,7 @@ OBJS-$(CONFIG_VP9_V4L2M2M_DECODER) += v4l2_m2m_dec.o · 810 unmodified lines
811811 OBJS-$(CONFIG_VQA_DECODER) += vqavideo.o
812812 OBJS-$(CONFIG_VQC_DECODER) += vqcdec.o
813813 OBJS-$(CONFIG_VVC_DECODER) += executor.o h2645data.o
814−OBJS-$(CONFIG_VVC_SEI) += h2645_sei.o aom_film_grain.o
814+OBJS-$(CONFIG_VVC_SEI) += h2645_sei.o aom_film_grain.o h274.o
815815 OBJS-$(CONFIG_WADY_DPCM_DECODER) += dpcm.o
816816 OBJS-$(CONFIG_WAVARC_DECODER) += wavarc.o
817817 OBJS-$(CONFIG_WAVPACK_DECODER) += wavpack.o wavpackdata.o dsd.o
libavcodec/vvc/dec.c+86−3
@@ -26,9 +26,12 @@ · 25 unmodified lines
2626 #include "libavcodec/hwconfig.h"
2727 #include "libavcodec/profiles.h"
2828 #include "libavutil/refstruct.h"
29+#include "libavcodec/aom_film_grain.h"
30+#include "libavcodec/thread.h"
2931 #include "libavutil/cpu.h"
3032 #include "libavutil/mem.h"
3133 #include "libavutil/thread.h"
34+#include "libavutil/film_grain_params.h"
3235
3336 #include "dec.h"
3437 #include "ctu.h"
@@ -618,6 +621,14 @@ static int ref_frame(VVCFrame *dst, const VVCFrame *src) · 583 unmodified lines
618621 av_refstruct_replace(&dst->sps, src->sps);
619622 av_refstruct_replace(&dst->pps, src->pps);
620623
624+ if (src->needs_fg) {
625+ ret = av_frame_ref(dst->frame_grain, src->frame_grain);
626+ if (ret < 0)
627+ return ret;
628+
629+ dst->needs_fg = src->needs_fg;
630+ }
631+
621632 av_refstruct_replace(&dst->progress, src->progress);
622633
623634 av_refstruct_replace(&dst->tab_dmvr_mvf, src->tab_dmvr_mvf);
@@ -651,6 +662,7 @@ static av_cold void frame_context_free(VVCFrameContext *fc) · 27 unmodified lines
651662 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
652663 ff_vvc_unref_frame(fc, &fc->DPB[i], ~0);
653664 av_frame_free(&fc->DPB[i].frame);
665+ av_frame_free(&fc->DPB[i].frame_grain);
654666 }
655667
656668 ff_vvc_frame_thread_free(fc);
@@ -673,6 +685,10 @@ static av_cold int frame_context_init(VVCFrameContext *fc, AVCodecContext *avctx · 16 unmodified lines
673685 fc->DPB[j].frame = av_frame_alloc();
674686 if (!fc->DPB[j].frame)
675687 return AVERROR(ENOMEM);
688+
689+ fc->DPB[j].frame_grain = av_frame_alloc();
690+ if (!fc->DPB[j].frame_grain)
691+ return AVERROR(ENOMEM);
676692 }
677693 fc->cu_pool = av_refstruct_pool_alloc(sizeof(CodingUnit), 0);
678694 if (!fc->cu_pool)
@@ -743,6 +759,41 @@ static int set_side_data(VVCContext *s, VVCFrameContext *fc) · 64 unmodified lines
743759 NULL, fc->ps.sps->bit_depth, fc->ps.sps->bit_depth, fc->ref->poc);
744760 }
745761
762+static int check_film_grain(VVCContext *s, VVCFrameContext *fc)
763+{
764+ int ret;
765+
766+ fc->ref->needs_fg = (fc->sei.common.film_grain_characteristics &&
767+ fc->sei.common.film_grain_characteristics->present ||
768+ fc->sei.common.aom_film_grain.enable) &&
769+ !(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) &&
770+ !s->avctx->hwaccel;
771+
772+ if (fc->ref->needs_fg &&
773+ (fc->sei.common.film_grain_characteristics->present &&
774+ !ff_h274_film_grain_params_supported(fc->sei.common.film_grain_characteristics->model_id,
775+ fc->ref->frame->format) ||
776+ !av_film_grain_params_select(fc->ref->frame))) {
777+ av_log_once(s->avctx, AV_LOG_WARNING, AV_LOG_DEBUG, &s->film_grain_warning_shown,
778+ "Unsupported film grain parameters. Ignoring film grain.\n");
779+ fc->ref->needs_fg = 0;
780+ }
781+
782+ if (fc->ref->needs_fg) {
783+ fc->ref->frame_grain->format = fc->ref->frame->format;
784+ fc->ref->frame_grain->width = fc->ref->frame->width;
785+ fc->ref->frame_grain->height = fc->ref->frame->height;
786+
787+ ret = ff_thread_get_buffer(s->avctx, fc->ref->frame_grain, 0);
788+ if (ret < 0)
789+ return ret;
790+
791+ return av_frame_copy_props(fc->ref->frame_grain, fc->ref->frame);
792+ }
793+
794+ return 0;
795+}
796+
746797 static int frame_start(VVCContext *s, VVCFrameContext *fc, SliceContext *sc)
747798 {
748799 const VVCPH *ph = &fc->ps.ph;
@@ -762,6 +813,10 @@ static int frame_start(VVCContext *s, VVCFrameContext *fc, SliceContext *sc) · 13 unmodified lines
762813 if (ret < 0)
763814 goto fail;
764815
816+ ret = check_film_grain(s, fc);
817+ if (ret < 0)
818+ goto fail;
819+
765820 if (!IS_IDR(s))
766821 ff_vvc_bump_frame(s, fc);
767822
@@ -1019,14 +1074,42 @@ fail: · 251 unmodified lines
10191074 return ret;
10201075 }
10211076
1077+static int frame_end(VVCContext *s, VVCFrameContext *fc)
1078+{
1079+ const AVFilmGrainParams *fgp;
1080+ int ret = 0;
1081+
1082+ if (fc->ref->needs_fg) {
1083+ av_assert0(fc->ref->frame_grain->buf[0]);
1084+ fgp = av_film_grain_params_select(fc->ref->frame);
1085+ switch (fgp->type) {
1086+ case AV_FILM_GRAIN_PARAMS_NONE:
1087+ av_assert0(0);
1088+ return AVERROR_BUG;
1089+ case AV_FILM_GRAIN_PARAMS_H274:
1090+ ret = ff_h274_apply_film_grain(fc->ref->frame_grain, fc->ref->frame,
1091+ &s->h274db, fgp);
1092+ break;
1093+ case AV_FILM_GRAIN_PARAMS_AV1:
1094+ ret = ff_aom_apply_film_grain(fc->ref->frame_grain, fc->ref->frame, fgp);
1095+ break;
1096+ }
1097+ }
1098+
1099+ return ret;
1100+}
1101+
10221102 static int wait_delayed_frame(VVCContext *s, AVFrame *output, int *got_output)
10231103 {
10241104 VVCFrameContext *delayed = get_frame_context(s, s->fcs, s->nb_frames - s->nb_delayed);
10251105 int ret = ff_vvc_frame_wait(s, delayed);
10261106
1027− if (!ret && delayed->output_frame->buf[0] && output) {
1028− av_frame_move_ref(output, delayed->output_frame);
1029− *got_output = 1;
1107+ if (!ret) {
1108+ ret = frame_end(s, delayed);
1109+ if (ret >= 0 && delayed->output_frame->buf[0] && output) {
1110+ av_frame_move_ref(output, delayed->output_frame);
1111+ *got_output = 1;
1112+ }
10301113 }
10311114 s->nb_delayed--;
10321115
libavcodec/vvc/dec.h+7−1
@@ -26,6 +26,7 @@ · 25 unmodified lines
2626
2727 #include "libavcodec/videodsp.h"
2828 #include "libavcodec/vvc.h"
29+#include "libavcodec/h274.h"
2930
3031 #include "ps.h"
3132 #include "dsp.h"
@@ -71,12 +72,15 @@ typedef struct VVCWindow { · 39 unmodified lines
7172
7273 typedef struct VVCFrame {
7374 struct AVFrame *frame;
74−
75+ struct AVFrame *frame_grain;
7576 const VVCSPS *sps; ///< RefStruct reference
7677 const VVCPPS *pps; ///< RefStruct reference
7778 struct MvField *tab_dmvr_mvf; ///< RefStruct reference
7879 RefPicListTab **rpl_tab; ///< RefStruct reference
7980 RefPicListTab *rpl; ///< RefStruct reference
81+
82+ int needs_fg; ///< 1 if grain needs to be applied by the decoder
83+
8084 int nb_rpl_elems;
8185
8286 int ctb_count;
@@ -218,6 +222,7 @@ typedef struct VVCContext { · 135 unmodified lines
218222 CodedBitstreamFragment current_frame;
219223
220224 VVCParamSets ps;
225+ H274FilmGrainDatabase h274db;
221226
222227 int temporal_id; ///< temporal_id_plus1 - 1
223228 int poc_tid0;
@@ -228,6 +233,7 @@ typedef struct VVCContext { · 4 unmodified lines
228233 enum VVCNALUnitType vcl_unit_type;
229234 int no_output_before_recovery_flag; ///< NoOutputBeforeRecoveryFlag
230235 int gdr_recovery_point_poc; ///< recoveryPointPocVal
236+ int film_grain_warning_shown;
231237
232238 /**
233239 * Sequence counters for decoded and output frames, so that old
libavcodec/vvc/refs.c+10−1
@@ -52,6 +52,12 @@ void ff_vvc_unref_frame(VVCFrameContext *fc, VVCFrame *frame, int flags) · 51 unmodified lines
5252 frame->flags = 0;
5353 if (!frame->flags) {
5454 av_frame_unref(frame->frame);
55+
56+ if (frame->needs_fg) {
57+ av_frame_unref(frame->frame_grain);
58+ frame->needs_fg = 0;
59+ }
60+
5561 av_refstruct_unref(&frame->sps);
5662 av_refstruct_unref(&frame->pps);
5763 av_refstruct_unref(&frame->progress);
@@ -285,7 +291,10 @@ int ff_vvc_output_frame(VVCContext *s, VVCFrameContext *fc, AVFrame *out, const · 227 unmodified lines
285291 if (frame->flags & VVC_FRAME_FLAG_CORRUPT)
286292 frame->frame->flags |= AV_FRAME_FLAG_CORRUPT;
287293
288− ret = av_frame_ref(out, frame->frame);
294+ ret = av_frame_ref(out, frame->needs_fg ? frame->frame_grain : frame->frame);
295+ if (ret < 0)
296+ return ret;
297+
289298 if (frame->flags & VVC_FRAME_FLAG_BUMPING)
290299 ff_vvc_unref_frame(fc, frame, VVC_FRAME_FLAG_OUTPUT | VVC_FRAME_FLAG_BUMPING);
291300 else

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t