diff mbox series

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

Message ID 20240319140636.4323-3-jamrial@gmail.com
State New
Headers show
Series [FFmpeg-devel,1/3] libavcodec/h2645_sei: export raw LCEVC metadata | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

James Almer March 19, 2024, 2:06 p.m. UTC
Signed-off-by: James Almer <jamrial@gmail.com>
---
 fftools/ffmpeg.h        |  3 +++
 fftools/ffmpeg_demux.c  |  4 ++++
 fftools/ffmpeg_filter.c | 28 +++++++++++++++++++++++++++-
 fftools/ffmpeg_opt.c    |  3 +++
 4 files changed, 37 insertions(+), 1 deletion(-)

Comments

Andreas Rheinhardt March 19, 2024, 3:01 p.m. UTC | #1
James Almer:
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>  fftools/ffmpeg.h        |  3 +++
>  fftools/ffmpeg_demux.c  |  4 ++++
>  fftools/ffmpeg_filter.c | 28 +++++++++++++++++++++++++++-
>  fftools/ffmpeg_opt.c    |  3 +++
>  4 files changed, 37 insertions(+), 1 deletion(-)
> 
> diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
> index 7454089c2d..8d54affc0a 100644
> --- a/fftools/ffmpeg.h
> +++ b/fftools/ffmpeg.h
> @@ -155,6 +155,7 @@ typedef struct OptionsContext {
>      SpecifierOptList hwaccel_devices;
>      SpecifierOptList hwaccel_output_formats;
>      SpecifierOptList autorotate;
> +    SpecifierOptList autoenhance;
>  
>      /* output options */
>      StreamMap *stream_maps;
> @@ -239,6 +240,7 @@ enum IFilterFlags {
>      IFILTER_FLAG_AUTOROTATE     = (1 << 0),
>      IFILTER_FLAG_REINIT         = (1 << 1),
>      IFILTER_FLAG_CFR            = (1 << 2),
> +    IFILTER_FLAG_AUTOENHANCE    = (1 << 3),
>  };
>  
>  typedef struct InputFilterOptions {
> @@ -369,6 +371,7 @@ typedef struct InputStream {
>  #endif
>  
>      int                   autorotate;
> +    int                   autoenhance;
>  
>      int                   fix_sub_duration;
>  
> diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
> index 47312c9fe1..0b4ab3d7ec 100644
> --- a/fftools/ffmpeg_demux.c
> +++ b/fftools/ffmpeg_demux.c
> @@ -1056,6 +1056,7 @@ int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple,
>          return AVERROR(ENOMEM);
>  
>      opts->flags |= IFILTER_FLAG_AUTOROTATE * !!(ist->autorotate) |
> +                   IFILTER_FLAG_AUTOENHANCE * !!(ist->autoenhance) |
>                     IFILTER_FLAG_REINIT     * !!(ds->reinit_filters);
>  
>      return ds->sch_idx_dec;
> @@ -1238,6 +1239,9 @@ static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st)
>      ist->autorotate = 1;
>      MATCH_PER_STREAM_OPT(autorotate, i, ist->autorotate, ic, st);
>  
> +    ist->autoenhance = 1;
> +    MATCH_PER_STREAM_OPT(autoenhance, i, ist->autoenhance, ic, st);
> +
>      MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, ic, st);
>      if (codec_tag) {
>          uint32_t tag = strtol(codec_tag, &next, 0);
> diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
> index 3d88482d07..c4d900d95b 100644
> --- a/fftools/ffmpeg_filter.c
> +++ b/fftools/ffmpeg_filter.c
> @@ -18,6 +18,8 @@
>   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
>   */
>  
> +#include "config_components.h"
> +
>  #include <stdint.h>
>  
>  #include "ffmpeg.h"
> @@ -145,6 +147,8 @@ typedef struct InputFilterPriv {
>      int                 displaymatrix_present;
>      int32_t             displaymatrix[9];
>  
> +    int                 enhancement_present;
> +
>      // fallback parameters to use when no input is ever sent
>      struct {
>          AVRational          time_base;
> @@ -1567,6 +1571,15 @@ static int configure_input_video_filter(FilterGraph *fg, AVFilterGraph *graph,
>      desc = av_pix_fmt_desc_get(ifp->format);
>      av_assert0(desc);
>  
> +#if CONFIG_LCEVC_FILTER
> +    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 < 0)
> +            return ret;
> +    }

If I see this correctly, this will add this filter automatically for all
videos when this filter is present, although it will be unneeded in the
vast majority of cases. I am against this.

> +#endif
> +
>      // TODO: insert hwaccel enabled filters like transpose_vaapi into the graph
>      if ((ifp->opts.flags & IFILTER_FLAG_AUTOROTATE) &&
>          !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
> @@ -1883,6 +1896,10 @@ static int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *fr
>          memcpy(ifp->displaymatrix, sd->data, sizeof(ifp->displaymatrix));
>      ifp->displaymatrix_present = !!sd;
>  
> +#if CONFIG_LCEVC_FILTER
> +    ifp->enhancement_present = !!(av_frame_get_side_data(frame, AV_FRAME_DATA_LCEVC));
> +#endif
> +
>      return 0;
>  }
>  
> @@ -2584,7 +2601,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)
> @@ -2625,6 +2643,12 @@ static int send_frame(FilterGraph *fg, FilterGraphThread *fgt,
>      } else if (ifp->displaymatrix_present)
>          need_reinit |= MATRIX_CHANGED;
>  
> +#if CONFIG_LCEVC_FILTER
> +    if (sd = av_frame_get_side_data(frame, AV_FRAME_DATA_LCEVC))
> +        if (!ifp->enhancement_present)
> +            need_reinit |= ENHANCEMENT_CHANGED;
> +#endif
> +
>      if (!(ifp->opts.flags & IFILTER_FLAG_REINIT) && fgt->graph)
>          need_reinit = 0;
>  
> @@ -2681,6 +2705,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 4b3f9789ba..b87782806e 100644
> --- a/fftools/ffmpeg_opt.c
> +++ b/fftools/ffmpeg_opt.c
> @@ -1738,6 +1738,9 @@ const OptionDef options[] = {
>      { "autorotate",                 OPT_TYPE_BOOL,   OPT_PERSTREAM | OPT_EXPERT | OPT_INPUT,
>          { .off = OFFSET(autorotate) },
>          "automatically insert correct rotate filters" },
> +    { "autoenhance",                OPT_TYPE_BOOL,   OPT_PERSTREAM | OPT_EXPERT | OPT_INPUT,
> +        { .off = OFFSET(autoenhance) },
> +        "automatically insert enhancement filters (LCEVC)" },
>      { "autoscale",                  OPT_TYPE_BOOL,   OPT_PERSTREAM | OPT_EXPERT | OPT_OUTPUT,
>          { .off = OFFSET(autoscale) },
>          "automatically insert a scale filter at the end of the filter graph" },
James Almer March 19, 2024, 3:08 p.m. UTC | #2
On 3/19/2024 12:01 PM, Andreas Rheinhardt wrote:
> James Almer:
>> Signed-off-by: James Almer <jamrial@gmail.com>
>> ---
>>   fftools/ffmpeg.h        |  3 +++
>>   fftools/ffmpeg_demux.c  |  4 ++++
>>   fftools/ffmpeg_filter.c | 28 +++++++++++++++++++++++++++-
>>   fftools/ffmpeg_opt.c    |  3 +++
>>   4 files changed, 37 insertions(+), 1 deletion(-)
>>
>> diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
>> index 7454089c2d..8d54affc0a 100644
>> --- a/fftools/ffmpeg.h
>> +++ b/fftools/ffmpeg.h
>> @@ -155,6 +155,7 @@ typedef struct OptionsContext {
>>       SpecifierOptList hwaccel_devices;
>>       SpecifierOptList hwaccel_output_formats;
>>       SpecifierOptList autorotate;
>> +    SpecifierOptList autoenhance;
>>   
>>       /* output options */
>>       StreamMap *stream_maps;
>> @@ -239,6 +240,7 @@ enum IFilterFlags {
>>       IFILTER_FLAG_AUTOROTATE     = (1 << 0),
>>       IFILTER_FLAG_REINIT         = (1 << 1),
>>       IFILTER_FLAG_CFR            = (1 << 2),
>> +    IFILTER_FLAG_AUTOENHANCE    = (1 << 3),
>>   };
>>   
>>   typedef struct InputFilterOptions {
>> @@ -369,6 +371,7 @@ typedef struct InputStream {
>>   #endif
>>   
>>       int                   autorotate;
>> +    int                   autoenhance;
>>   
>>       int                   fix_sub_duration;
>>   
>> diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
>> index 47312c9fe1..0b4ab3d7ec 100644
>> --- a/fftools/ffmpeg_demux.c
>> +++ b/fftools/ffmpeg_demux.c
>> @@ -1056,6 +1056,7 @@ int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple,
>>           return AVERROR(ENOMEM);
>>   
>>       opts->flags |= IFILTER_FLAG_AUTOROTATE * !!(ist->autorotate) |
>> +                   IFILTER_FLAG_AUTOENHANCE * !!(ist->autoenhance) |
>>                      IFILTER_FLAG_REINIT     * !!(ds->reinit_filters);
>>   
>>       return ds->sch_idx_dec;
>> @@ -1238,6 +1239,9 @@ static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st)
>>       ist->autorotate = 1;
>>       MATCH_PER_STREAM_OPT(autorotate, i, ist->autorotate, ic, st);
>>   
>> +    ist->autoenhance = 1;
>> +    MATCH_PER_STREAM_OPT(autoenhance, i, ist->autoenhance, ic, st);
>> +
>>       MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, ic, st);
>>       if (codec_tag) {
>>           uint32_t tag = strtol(codec_tag, &next, 0);
>> diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
>> index 3d88482d07..c4d900d95b 100644
>> --- a/fftools/ffmpeg_filter.c
>> +++ b/fftools/ffmpeg_filter.c
>> @@ -18,6 +18,8 @@
>>    * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
>>    */
>>   
>> +#include "config_components.h"
>> +
>>   #include <stdint.h>
>>   
>>   #include "ffmpeg.h"
>> @@ -145,6 +147,8 @@ typedef struct InputFilterPriv {
>>       int                 displaymatrix_present;
>>       int32_t             displaymatrix[9];
>>   
>> +    int                 enhancement_present;
>> +
>>       // fallback parameters to use when no input is ever sent
>>       struct {
>>           AVRational          time_base;
>> @@ -1567,6 +1571,15 @@ static int configure_input_video_filter(FilterGraph *fg, AVFilterGraph *graph,
>>       desc = av_pix_fmt_desc_get(ifp->format);
>>       av_assert0(desc);
>>   
>> +#if CONFIG_LCEVC_FILTER
>> +    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 < 0)
>> +            return ret;
>> +    }
> 
> If I see this correctly, this will add this filter automatically for all
> videos when this filter is present, although it will be unneeded in the
> vast majority of cases. I am against this.

Yeah, good catch. Missed a ifp->enhancement_present check here.
diff mbox series

Patch

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 7454089c2d..8d54affc0a 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -155,6 +155,7 @@  typedef struct OptionsContext {
     SpecifierOptList hwaccel_devices;
     SpecifierOptList hwaccel_output_formats;
     SpecifierOptList autorotate;
+    SpecifierOptList autoenhance;
 
     /* output options */
     StreamMap *stream_maps;
@@ -239,6 +240,7 @@  enum IFilterFlags {
     IFILTER_FLAG_AUTOROTATE     = (1 << 0),
     IFILTER_FLAG_REINIT         = (1 << 1),
     IFILTER_FLAG_CFR            = (1 << 2),
+    IFILTER_FLAG_AUTOENHANCE    = (1 << 3),
 };
 
 typedef struct InputFilterOptions {
@@ -369,6 +371,7 @@  typedef struct InputStream {
 #endif
 
     int                   autorotate;
+    int                   autoenhance;
 
     int                   fix_sub_duration;
 
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index 47312c9fe1..0b4ab3d7ec 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -1056,6 +1056,7 @@  int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple,
         return AVERROR(ENOMEM);
 
     opts->flags |= IFILTER_FLAG_AUTOROTATE * !!(ist->autorotate) |
+                   IFILTER_FLAG_AUTOENHANCE * !!(ist->autoenhance) |
                    IFILTER_FLAG_REINIT     * !!(ds->reinit_filters);
 
     return ds->sch_idx_dec;
@@ -1238,6 +1239,9 @@  static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st)
     ist->autorotate = 1;
     MATCH_PER_STREAM_OPT(autorotate, i, ist->autorotate, ic, st);
 
+    ist->autoenhance = 1;
+    MATCH_PER_STREAM_OPT(autoenhance, i, ist->autoenhance, ic, st);
+
     MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, ic, st);
     if (codec_tag) {
         uint32_t tag = strtol(codec_tag, &next, 0);
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 3d88482d07..c4d900d95b 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -18,6 +18,8 @@ 
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "config_components.h"
+
 #include <stdint.h>
 
 #include "ffmpeg.h"
@@ -145,6 +147,8 @@  typedef struct InputFilterPriv {
     int                 displaymatrix_present;
     int32_t             displaymatrix[9];
 
+    int                 enhancement_present;
+
     // fallback parameters to use when no input is ever sent
     struct {
         AVRational          time_base;
@@ -1567,6 +1571,15 @@  static int configure_input_video_filter(FilterGraph *fg, AVFilterGraph *graph,
     desc = av_pix_fmt_desc_get(ifp->format);
     av_assert0(desc);
 
+#if CONFIG_LCEVC_FILTER
+    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 < 0)
+            return ret;
+    }
+#endif
+
     // TODO: insert hwaccel enabled filters like transpose_vaapi into the graph
     if ((ifp->opts.flags & IFILTER_FLAG_AUTOROTATE) &&
         !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
@@ -1883,6 +1896,10 @@  static int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *fr
         memcpy(ifp->displaymatrix, sd->data, sizeof(ifp->displaymatrix));
     ifp->displaymatrix_present = !!sd;
 
+#if CONFIG_LCEVC_FILTER
+    ifp->enhancement_present = !!(av_frame_get_side_data(frame, AV_FRAME_DATA_LCEVC));
+#endif
+
     return 0;
 }
 
@@ -2584,7 +2601,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)
@@ -2625,6 +2643,12 @@  static int send_frame(FilterGraph *fg, FilterGraphThread *fgt,
     } else if (ifp->displaymatrix_present)
         need_reinit |= MATRIX_CHANGED;
 
+#if CONFIG_LCEVC_FILTER
+    if (sd = av_frame_get_side_data(frame, AV_FRAME_DATA_LCEVC))
+        if (!ifp->enhancement_present)
+            need_reinit |= ENHANCEMENT_CHANGED;
+#endif
+
     if (!(ifp->opts.flags & IFILTER_FLAG_REINIT) && fgt->graph)
         need_reinit = 0;
 
@@ -2681,6 +2705,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 4b3f9789ba..b87782806e 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1738,6 +1738,9 @@  const OptionDef options[] = {
     { "autorotate",                 OPT_TYPE_BOOL,   OPT_PERSTREAM | OPT_EXPERT | OPT_INPUT,
         { .off = OFFSET(autorotate) },
         "automatically insert correct rotate filters" },
+    { "autoenhance",                OPT_TYPE_BOOL,   OPT_PERSTREAM | OPT_EXPERT | OPT_INPUT,
+        { .off = OFFSET(autoenhance) },
+        "automatically insert enhancement filters (LCEVC)" },
     { "autoscale",                  OPT_TYPE_BOOL,   OPT_PERSTREAM | OPT_EXPERT | OPT_OUTPUT,
         { .off = OFFSET(autoscale) },
         "automatically insert a scale filter at the end of the filter graph" },