[FFmpeg-devel] [PATCH v1 1/4] avfilter/af_loudnorm: Add file option for the measured stats

lance.lmwang at gmail.com lance.lmwang at gmail.com
Thu Apr 9 14:07:17 EEST 2020


From: Limin Wang <lance.lmwang at gmail.com>

Signed-off-by: Limin Wang <lance.lmwang at gmail.com>
---
 doc/filters.texi          |  3 +++
 libavfilter/af_loudnorm.c | 54 +++++++++++++++++++++++++++++++++++++--
 2 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 3931d8d79e..738a40df4e 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -4285,6 +4285,9 @@ Options are true or false. Default is false.
 @item print_format
 Set print format for stats. Options are summary, json, or none.
 Default value is none.
+
+ at item file, f
+Set file path for the measured stats
 @end table
 
 @section lowpass
diff --git a/libavfilter/af_loudnorm.c b/libavfilter/af_loudnorm.c
index 314b25fa39..bf530195e4 100644
--- a/libavfilter/af_loudnorm.c
+++ b/libavfilter/af_loudnorm.c
@@ -20,6 +20,8 @@
 
 /* http://k.ylo.ph/2016/04/04/loudnorm.html */
 
+#include "libavutil/avstring.h"
+#include "libavformat/avio.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "internal.h"
@@ -62,6 +64,9 @@ typedef struct LoudNormContext {
     int linear;
     int dual_mono;
     enum PrintFormat print_format;
+    AVIOContext* pb;
+    char *filename;
+    void (*print)(AVFilterContext *ctx, const char *msg, ...) av_printf_format(2, 3);
 
     double *buf;
     int buf_size;
@@ -119,6 +124,8 @@ static const AVOption loudnorm_options[] = {
     {     "none",         0,                                   0,                        AV_OPT_TYPE_CONST,   {.i64 =  NONE},     0,         0,  FLAGS, "print_format" },
     {     "json",         0,                                   0,                        AV_OPT_TYPE_CONST,   {.i64 =  JSON},     0,         0,  FLAGS, "print_format" },
     {     "summary",      0,                                   0,                        AV_OPT_TYPE_CONST,   {.i64 =  SUMMARY},  0,         0,  FLAGS, "print_format" },
+    { "file", "set file path for the measured stats",          OFFSET(filename),         AV_OPT_TYPE_STRING,  {.str=NULL},                       FLAGS },
+    { "f",    "set file path for the measured stats",          OFFSET(filename),         AV_OPT_TYPE_STRING,  {.str=NULL},                       FLAGS },
     { NULL }
 };
 
@@ -781,6 +788,30 @@ static int config_input(AVFilterLink *inlink)
     return 0;
 }
 
+static void print_log(AVFilterContext *ctx, const char *msg, ...)
+{
+    va_list va;
+
+    va_start(va, msg);
+    if (msg)
+        av_vlog(ctx, AV_LOG_INFO, msg, va);
+    va_end(va);
+}
+
+static void print_file(AVFilterContext *ctx, const char *msg, ...)
+{
+    LoudNormContext *s = ctx->priv;
+    va_list va;
+
+    va_start(va, msg);
+    if (msg) {
+        char buf[1024];
+        vsnprintf(buf, sizeof(buf), msg, va);
+        avio_write(s->pb, buf, av_strnlen(buf, sizeof(buf)));
+    }
+    va_end(va);
+}
+
 static av_cold int init(AVFilterContext *ctx)
 {
     LoudNormContext *s = ctx->priv;
@@ -799,6 +830,22 @@ static av_cold int init(AVFilterContext *ctx)
         }
     }
 
+    if (s->print_format != NONE && s->filename) {
+        s->print = print_file;
+    } else {
+        s->print = print_log;
+    }
+
+    if (s->filename) {
+        int ret = avio_open(&s->pb, s->filename, AVIO_FLAG_WRITE);
+        if (ret < 0) {
+            char buf[128];
+            av_strerror(ret, buf, sizeof(buf));
+            av_log(ctx, AV_LOG_ERROR, "Could not open %s: %s\n", s->filename, buf);
+            return ret;
+        }
+    }
+
     return 0;
 }
 
@@ -836,7 +883,7 @@ static av_cold void uninit(AVFilterContext *ctx)
         break;
 
     case JSON:
-        av_log(ctx, AV_LOG_INFO,
+        s->print(ctx,
             "\n{\n"
             "\t\"input_i\" : \"%.2f\",\n"
             "\t\"input_tp\" : \"%.2f\",\n"
@@ -863,7 +910,7 @@ static av_cold void uninit(AVFilterContext *ctx)
         break;
 
     case SUMMARY:
-        av_log(ctx, AV_LOG_INFO,
+        s->print(ctx,
             "\n"
             "Input Integrated:   %+6.1f LUFS\n"
             "Input True Peak:    %+6.1f dBTP\n"
@@ -899,6 +946,9 @@ end:
     av_freep(&s->limiter_buf);
     av_freep(&s->prev_smp);
     av_freep(&s->buf);
+
+    if (s->pb)
+        avio_closep(&s->pb);
 }
 
 static const AVFilterPad avfilter_af_loudnorm_inputs[] = {
-- 
2.21.0



More information about the ffmpeg-devel mailing list