Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: toqsxw@gmail.com
To: ffmpeg-devel@ffmpeg.org
Cc: Wu Jianhua <toqsxw@outlook.com>
Subject: [FFmpeg-devel] [PATCH v1 05/19] avcodec/vvc/dec: support applying film grain by the decoder
Date: Wed,  2 Apr 2025 01:16:02 +0800
Message-ID: <20250401171616.1378-5-toqsxw@outlook.com> (raw)
In-Reply-To: <20250401171616.1378-1-toqsxw@outlook.com>

From: Wu Jianhua <toqsxw@outlook.com>

Signed-off-by: Wu Jianhua <toqsxw@outlook.com>
---
 libavcodec/Makefile   |  2 +-
 libavcodec/vvc/dec.c  | 89 +++++++++++++++++++++++++++++++++++++++++--
 libavcodec/vvc/dec.h  |  8 +++-
 libavcodec/vvc/refs.c | 11 +++++-
 4 files changed, 104 insertions(+), 6 deletions(-)

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 7467c883d3..e2d324202d 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -809,7 +809,7 @@ 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)             += executor.o h2645data.o
-OBJS-$(CONFIG_VVC_SEI)                 += h2645_sei.o aom_film_grain.o
+OBJS-$(CONFIG_VVC_SEI)                 += h2645_sei.o 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 8b1c2c751b..4c0d08440a 100644
--- a/libavcodec/vvc/dec.c
+++ b/libavcodec/vvc/dec.c
@@ -26,9 +26,12 @@
 #include "libavcodec/hwconfig.h"
 #include "libavcodec/profiles.h"
 #include "libavutil/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"
@@ -601,6 +604,14 @@ static int ref_frame(VVCFrame *dst, const VVCFrame *src)
     av_refstruct_replace(&dst->sps, src->sps);
     av_refstruct_replace(&dst->pps, src->pps);
 
+    if (src->needs_fg) {
+        ret = av_frame_ref(dst->frame_grain, src->frame_grain);
+        if (ret < 0)
+            return ret;
+
+        dst->needs_fg = src->needs_fg;
+    }
+
     av_refstruct_replace(&dst->progress, src->progress);
 
     av_refstruct_replace(&dst->tab_dmvr_mvf, src->tab_dmvr_mvf);
@@ -634,6 +645,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);
@@ -656,6 +668,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 = av_refstruct_pool_alloc(sizeof(CodingUnit), 0);
     if (!fc->cu_pool)
@@ -726,6 +742,41 @@ 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)
+{
+    int ret;
+
+    fc->ref->needs_fg = (fc->sei.common.film_grain_characteristics &&
+        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;
+
+        ret = ff_thread_get_buffer(s->avctx, fc->ref->frame_grain, 0);
+        if (ret < 0)
+            return ret;
+
+        return av_frame_copy_props(fc->ref->frame_grain, fc->ref->frame);
+    }
+
+    return 0;
+}
+
 static int frame_start(VVCContext *s, VVCFrameContext *fc, SliceContext *sc)
 {
     const VVCPH *ph                 = &fc->ps.ph;
@@ -745,6 +796,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);
 
@@ -1002,14 +1057,42 @@ fail:
     return ret;
 }
 
+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);
-        *got_output = 1;
+    if (!ret) {
+        ret = frame_end(s, delayed);
+        if (ret >= 0 && delayed->output_frame->buf[0] && output) {
+            av_frame_move_ref(output, delayed->output_frame);
+            *got_output = 1;
+        }
     }
     s->nb_delayed--;
 
diff --git a/libavcodec/vvc/dec.h b/libavcodec/vvc/dec.h
index df81a83489..b159863b31 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;
@@ -218,6 +222,7 @@ typedef struct VVCContext {
     CodedBitstreamFragment current_frame;
 
     VVCParamSets ps;
+    H274FilmGrainDatabase h274db;
 
     int temporal_id;        ///< temporal_id_plus1 - 1
     int poc_tid0;
@@ -228,6 +233,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 1cfca48204..4cf44bcf46 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 = 0;
     if (!frame->flags) {
         av_frame_unref(frame->frame);
+
+        if (frame->needs_fg) {
+            av_frame_unref(frame->frame_grain);
+            frame->needs_fg = 0;
+        }
+
         av_refstruct_unref(&frame->sps);
         av_refstruct_unref(&frame->pps);
         av_refstruct_unref(&frame->progress);
@@ -285,7 +291,10 @@ int ff_vvc_output_frame(VVCContext *s, VVCFrameContext *fc, AVFrame *out, const
             if (frame->flags & VVC_FRAME_FLAG_CORRUPT)
                 frame->frame->flags |= AV_FRAME_FLAG_CORRUPT;
 
-            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->flags & VVC_FRAME_FLAG_BUMPING)
                 ff_vvc_unref_frame(fc, frame, VVC_FRAME_FLAG_OUTPUT | VVC_FRAME_FLAG_BUMPING);
             else
-- 
2.44.0.windows.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

  parent reply	other threads:[~2025-04-01 17:17 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-01 17:15 [FFmpeg-devel] [PATCH v1 01/19] avcodec/cbs_sei_syntax_template: add sei message film_grain_characteristics toqsxw
2025-04-01 17:15 ` [FFmpeg-devel] [PATCH v1 02/19] avcodec/vvc: support decoding prefix and suffix nal units toqsxw
2025-04-01 17:16 ` [FFmpeg-devel] [PATCH v1 03/19] avcodec/vvc/sei: add decode_film_grain_characteristics toqsxw
2025-04-01 17:16 ` [FFmpeg-devel] [PATCH v1 04/19] avcodec/vvc/dec: export sei to the frame when the frame starts toqsxw
2025-04-01 17:16 ` toqsxw [this message]
2025-04-01 17:16 ` [FFmpeg-devel] [PATCH v1 06/19] avcodec/vvc/dec: support removing film grain params from side data toqsxw
2025-04-01 17:16 ` [FFmpeg-devel] [PATCH v1 07/19] avcodec/h274: add H274SEIPictureHash struct toqsxw
2025-04-01 17:16 ` [FFmpeg-devel] [PATCH v1 08/19] avcodec/vvc/sei: add decode_decoded_picture_hash toqsxw
2025-04-01 17:16 ` [FFmpeg-devel] [PATCH v1 09/19] avcodec/h274: add ff_h274_hash functions toqsxw
2025-04-01 17:16 ` [FFmpeg-devel] [PATCH v1 10/19] avcodec/vvcdec: verify picture hash toqsxw
2025-04-01 17:16 ` [FFmpeg-devel] [PATCH v1 11/19] avcodec/cbs_sei_syntax_template: add sei message sei_display_orientation toqsxw
2025-04-01 17:16 ` [FFmpeg-devel] [PATCH v1 12/19] avcodec/vvc/sei: add decode_display_orientation toqsxw
2025-04-01 17:16 ` [FFmpeg-devel] [PATCH v1 13/19] avcodec/vvc/sei: add decode_content_light_level_info toqsxw
2025-04-01 17:16 ` [FFmpeg-devel] [PATCH v1 14/19] avcodec/cbs_sei_syntax_template: add sei message frame_field_information toqsxw
2025-04-01 17:16 ` [FFmpeg-devel] [PATCH v1 15/19] avcodec/h274: add H274SEIFrameFieldInfo struct toqsxw
2025-04-01 17:16 ` [FFmpeg-devel] [PATCH v1 16/19] avcodec/vvc/sei: add decode_frame_field_info toqsxw
2025-04-01 17:16 ` [FFmpeg-devel] [PATCH v1 17/19] avcodec/vvc: support fields toqsxw
2025-04-01 17:35   ` Andreas Rheinhardt
2025-04-01 18:11     ` [FFmpeg-devel] 回复: " Wu Jianhua
2025-04-01 17:16 ` [FFmpeg-devel] [PATCH v1 18/19] avcodec/vvc/sei: add decode_ambient_viewing_environment toqsxw
2025-04-01 17:16 ` [FFmpeg-devel] [PATCH v1 19/19] avcodec/vvc/sei: add decode_mastering_display_colour_volume toqsxw

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250401171616.1378-5-toqsxw@outlook.com \
    --to=toqsxw@gmail.com \
    --cc=ffmpeg-devel@ffmpeg.org \
    --cc=toqsxw@outlook.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git