[FFmpeg-devel] [PATCH v2 2/2] libavfilter/graphparser: Add scale_sws_opts parse support in avfilter_graph_parse2

Linjie Fu fulinjie at zju.edu.cn
Mon Aug 9 17:50:14 EEST 2021


From: Linjie Fu <linjie.justin.fu at gmail.com>

To pass the swscale options for the inserted scalers.

ffmpeg -i input.mp4 -filter_complex \
    "scale_sws_opts=alphablend=checkerboard;format=nv12" \
    -t 0.5 output.mp4

Update docs.

Signed-off-by: Linjie Fu <linjie.justin.fu at gmail.com>
---
 doc/filters.texi          |  7 ++++---
 libavfilter/graphparser.c | 22 ++++++++++++++++++++++
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index bdeb3fedfd..67108f91ff 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -204,9 +204,9 @@ pads must be connected. A filtergraph is considered valid if all the
 filter input and output pads of all the filterchains are connected.
 
 Libavfilter will automatically insert @ref{scale} filters where format
-conversion is required. It is possible to specify swscale flags
-for those automatically inserted scalers by prepending
- at code{sws_flags=@var{flags};}
+conversion is required. It is possible to specify swscale flags or
+scale_sws_opts for those automatically inserted scalers by prepending
+ at code{sws_flags=@var{flags};} or @code{scale_sws_opts=@var{scale_sws_opts};}
 to the filtergraph description.
 
 Here is a BNF description of the filtergraph syntax:
@@ -219,6 +219,7 @@ Here is a BNF description of the filtergraph syntax:
 @var{FILTER}           ::= [@var{LINKLABELS}] @var{FILTER_NAME} ["=" @var{FILTER_ARGUMENTS}] [@var{LINKLABELS}]
 @var{FILTERCHAIN}      ::= @var{FILTER} [, at var{FILTERCHAIN}]
 @var{FILTERGRAPH}      ::= [sws_flags=@var{flags};] @var{FILTERCHAIN} [;@var{FILTERGRAPH}]
+ at var{FILTERGRAPH}      ::= [scale_sws_opts=@var{opts};] @var{FILTERCHAIN} [;@var{FILTERGRAPH}]
 @end example
 
 @anchor{filtergraph escaping}
diff --git a/libavfilter/graphparser.c b/libavfilter/graphparser.c
index 1385c3ae71..96ae8940af 100644
--- a/libavfilter/graphparser.c
+++ b/libavfilter/graphparser.c
@@ -415,6 +415,25 @@ static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
     return 0;
 }
 
+static int parse_scale_sws_opts(const char **buf, AVFilterGraph *graph)
+{
+    char *p = strchr(*buf, ';');
+
+    if (!av_strstart(*buf, "scale_sws_opts=", buf))
+        return 0;
+
+    if (!p) {
+        av_log(graph, AV_LOG_ERROR, "scale_sws_opts not terminated with ';'.\n");
+        return AVERROR(EINVAL);
+    }
+
+    av_freep(&graph->scale_sws_opts);
+    graph->scale_sws_opts = av_strndup(*buf, p - *buf);
+
+    *buf = p + 1;
+    return 0;
+}
+
 int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
                           AVFilterInOut **inputs,
                           AVFilterInOut **outputs)
@@ -429,6 +448,9 @@ int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
     if ((ret = parse_sws_flags(&filters, graph)) < 0)
         goto fail;
 
+    if ((ret = parse_scale_sws_opts(&filters, graph)) < 0)
+        goto fail;
+
     do {
         AVFilterContext *filter;
         filters += strspn(filters, WHITESPACES);
-- 
2.31.1



More information about the ffmpeg-devel mailing list