Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
To: ffmpeg-devel@ffmpeg.org
Cc: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Subject: [FFmpeg-devel] [PATCH 1/2] avcodec/hevcdec: Check early whether film grain is supported, fix race
Date: Sun, 17 Sep 2023 02:10:46 +0200
Message-ID: <GV1P250MB0737EE6DEA209D1B92E33C3F8FF4A@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM> (raw)

Applying film grain happens after ff_thread_finish_setup(),
so the parameters synced in hevc_update_thread_context() must not
be modified. But this is exactly what happens in case applying
film grain fails. (The likely result is that in case of frame threading
an uninitialized frame is output.)

Given that it is actually very easy to know in advance whether
ff_h274_apply_film_grain() supports a given set of parameters,
one can check for this before ff_thread_finish_setup()
and avoid allocating an unused buffer lateron.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/h274.h    | 15 +++++++++++++++
 libavcodec/hevcdec.c | 19 ++++++++++++-------
 libavcodec/hevcdec.h |  2 ++
 3 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/libavcodec/h274.h b/libavcodec/h274.h
index 920f6991fb..27a07e4a91 100644
--- a/libavcodec/h274.h
+++ b/libavcodec/h274.h
@@ -40,11 +40,26 @@ typedef struct H274FilmGrainDatabase {
     int16_t slice_tmp[64][64];
 } H274FilmGrainDatabase;
 
+/**
+ * Check whether ff_h274_apply_film_grain() supports the given parameter combination.
+ *
+ * @param model_id model_id from AVFilmGrainParams to be supplied
+ * @param pix_fmt  pixel format of the frames to be supplied
+ */
+static inline int ff_h274_film_grain_params_supported(int model_id, enum AVPixelFormat pix_fmt)
+{
+    return model_id == 1 && pix_fmt == AV_PIX_FMT_YUV420P;
+}
+
 // Synthesizes film grain on top of `in` and stores the result to `out`. `out`
 // must already have been allocated and set to the same size and format as
 // `in`.
 //
 // Returns a negative error code on error, such as invalid params.
+// If ff_h274_film_grain_params_supported() indicated that the parameters
+// are supported, no error will be returned if the arguments given to
+// ff_h274_film_grain_params_supported() coincide with actual values
+// from the frames and params.
 int ff_h274_apply_film_grain(AVFrame *out, const AVFrame *in,
                              H274FilmGrainDatabase *db,
                              const AVFilmGrainParams *params);
diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
index 81b9c5e089..2be62ddfb2 100644
--- a/libavcodec/hevcdec.c
+++ b/libavcodec/hevcdec.c
@@ -2884,6 +2884,14 @@ static int hevc_frame_start(HEVCContext *s)
         !(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) &&
         !s->avctx->hwaccel;
 
+    if (s->ref->needs_fg &&
+        !ff_h274_film_grain_params_supported(s->sei.common.film_grain_characteristics.model_id,
+                                             s->ref->frame->format)) {
+        av_log_once(s->avctx, AV_LOG_WARNING, AV_LOG_DEBUG, &s->film_grain_warning_shown,
+                    "Unsupported film grain parameters. Ignoring film grain.\n");
+        s->ref->needs_fg = 0;
+    }
+
     if (s->ref->needs_fg) {
         s->ref->frame_grain->format = s->ref->frame->format;
         s->ref->frame_grain->width = s->ref->frame->width;
@@ -2922,19 +2930,14 @@ static int hevc_frame_end(HEVCContext *s)
 {
     HEVCFrame *out = s->ref;
     const AVFrameSideData *sd;
-    int ret;
+    av_unused int ret;
 
     if (out->needs_fg) {
         sd = av_frame_get_side_data(out->frame, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
         av_assert0(out->frame_grain->buf[0] && sd);
         ret = ff_h274_apply_film_grain(out->frame_grain, out->frame, &s->h274db,
                                        (AVFilmGrainParams *) sd->data);
-
-        if (ret < 0) {
-            av_log(s->avctx, AV_LOG_WARNING, "Failed synthesizing film "
-                   "grain, ignoring: %s\n", av_err2str(ret));
-            out->needs_fg = 0;
-        }
+        av_assert1(ret >= 0);
     }
 
     return 0;
@@ -3574,6 +3577,8 @@ static int hevc_update_thread_context(AVCodecContext *dst,
     s->threads_number      = s0->threads_number;
     s->threads_type        = s0->threads_type;
 
+    s->film_grain_warning_shown = s0->film_grain_warning_shown;
+
     if (s0->eos) {
         s->seq_decode = (s->seq_decode + 1) & HEVC_SEQUENCE_COUNTER_MASK;
         s->max_ra = INT_MAX;
diff --git a/libavcodec/hevcdec.h b/libavcodec/hevcdec.h
index 94609e4699..9b232df68c 100644
--- a/libavcodec/hevcdec.h
+++ b/libavcodec/hevcdec.h
@@ -596,6 +596,8 @@ typedef struct HEVCContext {
     int nal_length_size;    ///< Number of bytes used for nal length (1, 2 or 4)
     int nuh_layer_id;
 
+    int film_grain_warning_shown;
+
     AVBufferRef *rpu_buf;       ///< 0 or 1 Dolby Vision RPUs.
     DOVIContext dovi_ctx;       ///< Dolby Vision decoding context
 } HEVCContext;
-- 
2.34.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".

             reply	other threads:[~2023-09-17  0:09 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-17  0:10 Andreas Rheinhardt [this message]
2023-09-17  0:15 ` [FFmpeg-devel] [PATCH 2/2] avcodec/h264dec: " Andreas Rheinhardt
2023-09-19 15:31 ` [FFmpeg-devel] [PATCH 1/2] avcodec/hevcdec: " Andreas Rheinhardt
2023-09-26 10:53 ` Niklas Haas

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=GV1P250MB0737EE6DEA209D1B92E33C3F8FF4A@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM \
    --to=andreas.rheinhardt@outlook.com \
    --cc=ffmpeg-devel@ffmpeg.org \
    /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