diff mbox series

[FFmpeg-devel,3/3] fftools/ffmpeg: auto insert enhancement filters when available

Message ID 20240920154434.2541-1-jamrial@gmail.com
State New
Headers show
Series [FFmpeg-devel,1/2] configure: add missing filter dependencies to ffmpeg | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 fail Make failed
andriy/make_x86 fail Make failed

Commit Message

James Almer Sept. 20, 2024, 3:44 p.m. UTC
This is an alternative to https://ffmpeg.org//pipermail/ffmpeg-devel/2024-September/333396.html
where things are left to the caller and we don't rely on lavc applying the LCEVC metadata at
the end of the decoding process.

Signed-off-by: James Almer <jamrial@gmail.com>
---
 configure               |  2 +-
 fftools/ffmpeg.h        |  2 ++
 fftools/ffmpeg_demux.c  |  5 +++++
 fftools/ffmpeg_filter.c | 22 +++++++++++++++++++++-
 fftools/ffmpeg_opt.c    |  3 +++
 5 files changed, 32 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/configure b/configure
index f07f6c7c46..fb009d0c3f 100755
--- a/configure
+++ b/configure
@@ -4058,7 +4058,7 @@  ffmpeg_deps="avcodec avfilter avformat threads"
 ffmpeg_select="aformat_filter anull_filter atrim_filter crop_filter
                format_filter hflip_filter null_filter rotate_filter
                transpose_filter trim_filter vflip_filter"
-ffmpeg_suggest="ole32 psapi shell32"
+ffmpeg_suggest="lcevc_filter ole32 psapi shell32"
 ffplay_deps="avcodec avformat avfilter swscale swresample sdl2"
 ffplay_select="crop_filter transpose_filter hflip_filter vflip_filter rotate_filter"
 ffplay_suggest="shell32 libplacebo vulkan"
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index f4a10b2a66..44c7ca08ed 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -154,6 +154,7 @@  typedef struct OptionsContext {
     SpecifierOptList hwaccels;
     SpecifierOptList hwaccel_devices;
     SpecifierOptList hwaccel_output_formats;
+    SpecifierOptList autoenhance;
     SpecifierOptList autorotate;
     SpecifierOptList apply_cropping;
 
@@ -241,6 +242,7 @@  enum IFilterFlags {
     IFILTER_FLAG_REINIT         = (1 << 1),
     IFILTER_FLAG_CFR            = (1 << 2),
     IFILTER_FLAG_CROP           = (1 << 3),
+    IFILTER_FLAG_AUTOENHANCE    = (1 << 4),
 };
 
 typedef struct InputFilterOptions {
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index 0b639f2b60..f0290fa286 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -66,6 +66,7 @@  typedef struct DemuxStream {
     int                      have_sub2video;
     int                      reinit_filters;
     int                      autorotate;
+    int                      autoenhance;
     int                      apply_cropping;
 
 
@@ -1072,6 +1073,7 @@  int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple,
         return AVERROR(ENOMEM);
 
     opts->flags |= IFILTER_FLAG_AUTOROTATE * !!(ds->autorotate) |
+                   IFILTER_FLAG_AUTOENHANCE * !!(ds->autoenhance) |
                    IFILTER_FLAG_REINIT     * !!(ds->reinit_filters);
 
     return ds->sch_idx_dec;
@@ -1253,6 +1255,9 @@  static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st, AVDictiona
     ds->ts_scale = 1.0;
     opt_match_per_stream_dbl(ist, &o->ts_scale, ic, st, &ds->ts_scale);
 
+    ds->autoenhance = 1;
+    opt_match_per_stream_int(ist, &o->autoenhance, ic, st, &ds->autoenhance);
+
     ds->autorotate = 1;
     opt_match_per_stream_int(ist, &o->autorotate, ic, st, &ds->autorotate);
 
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 529e631781..6a64b5a091 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -147,6 +147,8 @@  typedef struct InputFilterPriv {
     int                 displaymatrix_applied;
     int32_t             displaymatrix[9];
 
+    int                 enhancement_present;
+
     struct {
         AVFrame *frame;
 
@@ -1697,6 +1699,15 @@  static int configure_input_video_filter(FilterGraph *fg, AVFilterGraph *graph,
     desc = av_pix_fmt_desc_get(ifp->format);
     av_assert0(desc);
 
+    if ((ifp->opts.flags & IFILTER_FLAG_AUTOENHANCE) &&
+        !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
+        ret = insert_filter(&last_filter, &pad_idx, "lcevc", NULL);
+        if (ret == AVERROR_BUG) // this filter is optional
+            ret = 0;
+        if (ret < 0)
+            return ret;
+    }
+
     if ((ifp->opts.flags & IFILTER_FLAG_CROP)) {
         char crop_buf[64];
         snprintf(crop_buf, sizeof(crop_buf), "w=iw-%u-%u:h=ih-%u-%u:x=%u:y=%u",
@@ -2028,6 +2039,8 @@  static int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *fr
         memcpy(ifp->displaymatrix, sd->data, sizeof(ifp->displaymatrix));
     ifp->displaymatrix_present = !!sd;
 
+    ifp->enhancement_present = !!(av_frame_get_side_data(frame, AV_FRAME_DATA_LCEVC));
+
     return 0;
 }
 
@@ -2727,7 +2740,8 @@  enum ReinitReason {
     VIDEO_CHANGED   = (1 << 0),
     AUDIO_CHANGED   = (1 << 1),
     MATRIX_CHANGED  = (1 << 2),
-    HWACCEL_CHANGED = (1 << 3)
+    HWACCEL_CHANGED = (1 << 3),
+    ENHANCEMENT_CHANGED = (1 << 4),
 };
 
 static const char *unknown_if_null(const char *str)
@@ -2768,6 +2782,10 @@  static int send_frame(FilterGraph *fg, FilterGraphThread *fgt,
     } else if (ifp->displaymatrix_present)
         need_reinit |= MATRIX_CHANGED;
 
+    if (sd = av_frame_get_side_data(frame, AV_FRAME_DATA_LCEVC))
+        if (!ifp->enhancement_present)
+            need_reinit |= ENHANCEMENT_CHANGED;
+
     if (!(ifp->opts.flags & IFILTER_FLAG_REINIT) && fgt->graph)
         need_reinit = 0;
 
@@ -2824,6 +2842,8 @@  static int send_frame(FilterGraph *fg, FilterGraphThread *fgt,
                 av_bprintf(&reason, "display matrix changed, ");
             if (need_reinit & HWACCEL_CHANGED)
                 av_bprintf(&reason, "hwaccel changed, ");
+            if (need_reinit & ENHANCEMENT_CHANGED)
+                av_bprintf(&reason, "enhancement data found, ");
             if (reason.len > 1)
                 reason.str[reason.len - 2] = '\0'; // remove last comma
             av_log(fg, AV_LOG_INFO, "Reconfiguring filter graph%s%s\n", reason.len ? " because " : "", reason.str);
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 1aa187f706..8cb062cced 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1789,6 +1789,9 @@  const OptionDef options[] = {
     { "hwaccels",                   OPT_TYPE_FUNC,   OPT_EXIT | OPT_EXPERT,
         { .func_arg = show_hwaccels },
         "show available HW acceleration methods" },
+    { "autoenhance",                OPT_TYPE_BOOL,   OPT_PERSTREAM | OPT_EXPERT | OPT_INPUT,
+        { .off = OFFSET(autoenhance) },
+        "automatically insert enhancement filters" },
     { "autorotate",                 OPT_TYPE_BOOL,   OPT_VIDEO | OPT_PERSTREAM | OPT_EXPERT | OPT_INPUT,
         { .off = OFFSET(autorotate) },
         "automatically insert correct rotate filters" },