diff mbox

[FFmpeg-devel] ffmpeg: parameters for filter thread counts

Message ID E1c08At-00015d-2C@rubyweapon.execulink.net
State Superseded
Headers show

Commit Message

DHE Oct. 28, 2016, 2:30 p.m. UTC
Enables specifying how many threads are available to each filtergraph.
---
 doc/ffmpeg.texi | 10 ++++++++++
 ffmpeg.h        |  3 +++
 ffmpeg_filter.c |  9 +++++++++
 ffmpeg_opt.c    |  4 ++++
 4 files changed, 26 insertions(+)

Comments

Steven Liu Oct. 29, 2016, 1:30 a.m. UTC | #1
DHE <git@dehacked.net>于2016年10月28日 周五下午10:30写道:

> Enables specifying how many threads are available to each filtergraph.
> ---
>  doc/ffmpeg.texi | 10 ++++++++++
>  ffmpeg.h        |  3 +++
>  ffmpeg_filter.c |  9 +++++++++
>  ffmpeg_opt.c    |  4 ++++
>  4 files changed, 26 insertions(+)
>
> diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi
> index 47c8935..8be24b2 100644
> --- a/doc/ffmpeg.texi
> +++ b/doc/ffmpeg.texi
> @@ -415,6 +415,11 @@ This option is similar to @option{-filter}, the only
> difference is that its
>  argument is the name of the file from which a filtergraph description is
> to be
>  read.
>
> +@item -filter_threads @var{nb_thraeds} (@emph{global})
> +Defines how many threads are used to process a filter pipeline.
> +Some filters support parallel processing. The default is the number of
> available
> +CPUs.
> +
>  @item -pre[:@var{stream_specifier}] @var{preset_name}
> (@emph{output,per-stream})
>  Specify the preset for matching stream(s).
>
> @@ -1201,6 +1206,11 @@ To generate 5 seconds of pure red video using lavfi
> @code{color} source:
>  ffmpeg -filter_complex 'color=c=red' -t 5 out.mkv
>  @end example
>
> +@item -filter_complex_threads @var{nb_threads} (@emph{global})
> +Defines how many threads are used to process a @code{-filter_complex}
> pipeline.
> +Some filters support parallel processing. The default is the number of
> available
> +CPUs.
> +
>  @item -lavfi @var{filtergraph} (@emph{global})
>  Define a complex filtergraph, i.e. one with arbitrary number of inputs
> and/or
>  outputs. Equivalent to @option{-filter_complex}.
> diff --git a/ffmpeg.h b/ffmpeg.h
> index e1d4593..9a4389f 100644
> --- a/ffmpeg.h
> +++ b/ffmpeg.h
> @@ -569,6 +569,9 @@ extern AVIOContext *progress_avio;
>  extern float max_error_rate;
>  extern char *videotoolbox_pixfmt;
>
> +extern int filter_nbthreads;
> +extern int filter_complex_nbthreads;
> +
>  extern const AVIOInterruptCB int_cb;
>
>  extern const OptionDef options[];
> diff --git a/ffmpeg_filter.c b/ffmpeg_filter.c
> index 27aeca0..4554456 100644
> --- a/ffmpeg_filter.c
> +++ b/ffmpeg_filter.c
> @@ -39,6 +39,9 @@
>  #include "libavutil/imgutils.h"
>  #include "libavutil/samplefmt.h"
>
> +int filter_nbthreads = -1;
> +int filter_complex_nbthreads = -1;
> +
>  static const enum AVPixelFormat *get_compliance_unofficial_pix_fmts(enum
> AVCodecID codec_id, const enum AVPixelFormat default_formats[])
>  {
>      static const enum AVPixelFormat mjpeg_formats[] =
> @@ -309,6 +312,8 @@ int init_complex_filtergraph(FilterGraph *fg)
>      if (!graph)
>          return AVERROR(ENOMEM);
>
> +    // As a temporary graph, don't bother making threads
> +    graph->nb_threads = 1;
>      ret = avfilter_graph_parse2(graph, fg->graph_desc, &inputs, &outputs);
>      if (ret < 0)
>          goto fail;
> @@ -992,6 +997,8 @@ int configure_filtergraph(FilterGraph *fg)
>          char args[512];
>          AVDictionaryEntry *e = NULL;
>
> +        fg->graph->nb_threads = filter_complex_nbthreads;
> +
>          args[0] = 0;
>          while ((e = av_dict_get(ost->sws_dict, "", e,
>                                  AV_DICT_IGNORE_SUFFIX))) {
> @@ -1022,6 +1029,8 @@ int configure_filtergraph(FilterGraph *fg)
>          e = av_dict_get(ost->encoder_opts, "threads", NULL, 0);
>          if (e)
>              av_opt_set(fg->graph, "threads", e->value, 0);
> +    } else {
> +        fg->graph->nb_threads = filter_nbthreads;
>      }
>
>      if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs,
> &outputs)) < 0)
> diff --git a/ffmpeg_opt.c b/ffmpeg_opt.c
> index 4d25fff..dc94380 100644
> --- a/ffmpeg_opt.c
> +++ b/ffmpeg_opt.c
> @@ -3365,12 +3365,16 @@ const OptionDef options[] = {
>          "set profile", "profile" },
>      { "filter",         HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, {
> .off = OFFSET(filters) },
>          "set stream filtergraph", "filter_graph" },
> +    { "filter_threads",  HAS_ARG | OPT_INT,                          {
> &filter_nbthreads },
> +        "number of non-complex filter threads" },
>      { "filter_script",  HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, {
> .off = OFFSET(filter_scripts) },
>          "read stream filtergraph description from a file", "filename" },
>      { "reinit_filter",  HAS_ARG | OPT_INT | OPT_SPEC | OPT_INPUT,    {
> .off = OFFSET(reinit_filters) },
>          "reinit filtergraph on input parameter changes", "" },
>      { "filter_complex", HAS_ARG | OPT_EXPERT,                        {
> .func_arg = opt_filter_complex },
>          "create a complex filtergraph", "graph_description" },
> +    { "filter_complex_threads", HAS_ARG | OPT_INT,                   {
> &filter_complex_nbthreads },
> +        "number of threads for -filter_complex" },
>      { "lavfi",          HAS_ARG | OPT_EXPERT,                        {
> .func_arg = opt_filter_complex },
>          "create a complex filtergraph", "graph_description" },
>      { "filter_complex_script", HAS_ARG | OPT_EXPERT,                 {
> .func_arg = opt_filter_complex_script },
> --
> 1.8.4.1
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>

LGTM
DHE Oct. 29, 2016, 1:51 a.m. UTC | #2
On 10/28/2016 10:30 AM, DHE wrote:
> Enables specifying how many threads are available to each filtergraph.
> ---
>  doc/ffmpeg.texi | 10 ++++++++++
>  ffmpeg.h        |  3 +++
>  ffmpeg_filter.c |  9 +++++++++
>  ffmpeg_opt.c    |  4 ++++
>  4 files changed, 26 insertions(+)
> 
> diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi
> index 47c8935..8be24b2 100644
> --- a/doc/ffmpeg.texi
> +++ b/doc/ffmpeg.texi
> @@ -415,6 +415,11 @@ This option is similar to @option{-filter}, the only difference is that its
>  argument is the name of the file from which a filtergraph description is to be
>  read.
>  
> +@item -filter_threads @var{nb_thraeds} (@emph{global})
> +Defines how many threads are used to process a filter pipeline.
> +Some filters support parallel processing. The default is the number of available
> +CPUs.
> +
>  @item -pre[:@var{stream_specifier}] @var{preset_name} (@emph{output,per-stream})
>  Specify the preset for matching stream(s).
>  
> @@ -1201,6 +1206,11 @@ To generate 5 seconds of pure red video using lavfi @code{color} source:
>  ffmpeg -filter_complex 'color=c=red' -t 5 out.mkv
>  @end example
>  
> +@item -filter_complex_threads @var{nb_threads} (@emph{global})
> +Defines how many threads are used to process a @code{-filter_complex} pipeline.
> +Some filters support parallel processing. The default is the number of available
> +CPUs.
> +
>  @item -lavfi @var{filtergraph} (@emph{global})
>  Define a complex filtergraph, i.e. one with arbitrary number of inputs and/or
>  outputs. Equivalent to @option{-filter_complex}.
> diff --git a/ffmpeg.h b/ffmpeg.h
> index e1d4593..9a4389f 100644
> --- a/ffmpeg.h
> +++ b/ffmpeg.h
> @@ -569,6 +569,9 @@ extern AVIOContext *progress_avio;
>  extern float max_error_rate;
>  extern char *videotoolbox_pixfmt;
>  
> +extern int filter_nbthreads;
> +extern int filter_complex_nbthreads;
> +
>  extern const AVIOInterruptCB int_cb;
>  
>  extern const OptionDef options[];
> diff --git a/ffmpeg_filter.c b/ffmpeg_filter.c
> index 27aeca0..4554456 100644
> --- a/ffmpeg_filter.c
> +++ b/ffmpeg_filter.c
> @@ -39,6 +39,9 @@
>  #include "libavutil/imgutils.h"
>  #include "libavutil/samplefmt.h"
>  
> +int filter_nbthreads = -1;
> +int filter_complex_nbthreads = -1;
> +
>  static const enum AVPixelFormat *get_compliance_unofficial_pix_fmts(enum AVCodecID codec_id, const enum AVPixelFormat default_formats[])
>  {
>      static const enum AVPixelFormat mjpeg_formats[] =
> @@ -309,6 +312,8 @@ int init_complex_filtergraph(FilterGraph *fg)
>      if (!graph)
>          return AVERROR(ENOMEM);
>  
> +    // As a temporary graph, don't bother making threads
> +    graph->nb_threads = 1;

I just noticed, this hunk arguably doens't belong in this patch as it isn't directly related to the command-line
parameters I added. I meant to split this into a different patch depending on how well this one was received.

>      ret = avfilter_graph_parse2(graph, fg->graph_desc, &inputs, &outputs);
>      if (ret < 0)
>          goto fail;
> @@ -992,6 +997,8 @@ int configure_filtergraph(FilterGraph *fg)
>          char args[512];
>          AVDictionaryEntry *e = NULL;
>  
> +        fg->graph->nb_threads = filter_complex_nbthreads;
> +
>          args[0] = 0;
>          while ((e = av_dict_get(ost->sws_dict, "", e,
>                                  AV_DICT_IGNORE_SUFFIX))) {
> @@ -1022,6 +1029,8 @@ int configure_filtergraph(FilterGraph *fg)
>          e = av_dict_get(ost->encoder_opts, "threads", NULL, 0);
>          if (e)
>              av_opt_set(fg->graph, "threads", e->value, 0);
> +    } else {
> +        fg->graph->nb_threads = filter_nbthreads;
>      }
>  
>      if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
> diff --git a/ffmpeg_opt.c b/ffmpeg_opt.c
> index 4d25fff..dc94380 100644
> --- a/ffmpeg_opt.c
> +++ b/ffmpeg_opt.c
> @@ -3365,12 +3365,16 @@ const OptionDef options[] = {
>          "set profile", "profile" },
>      { "filter",         HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(filters) },
>          "set stream filtergraph", "filter_graph" },
> +    { "filter_threads",  HAS_ARG | OPT_INT,                          { &filter_nbthreads },
> +        "number of non-complex filter threads" },
>      { "filter_script",  HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(filter_scripts) },
>          "read stream filtergraph description from a file", "filename" },
>      { "reinit_filter",  HAS_ARG | OPT_INT | OPT_SPEC | OPT_INPUT,    { .off = OFFSET(reinit_filters) },
>          "reinit filtergraph on input parameter changes", "" },
>      { "filter_complex", HAS_ARG | OPT_EXPERT,                        { .func_arg = opt_filter_complex },
>          "create a complex filtergraph", "graph_description" },
> +    { "filter_complex_threads", HAS_ARG | OPT_INT,                   { &filter_complex_nbthreads },
> +        "number of threads for -filter_complex" },
>      { "lavfi",          HAS_ARG | OPT_EXPERT,                        { .func_arg = opt_filter_complex },
>          "create a complex filtergraph", "graph_description" },
>      { "filter_complex_script", HAS_ARG | OPT_EXPERT,                 { .func_arg = opt_filter_complex_script },
>
diff mbox

Patch

diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi
index 47c8935..8be24b2 100644
--- a/doc/ffmpeg.texi
+++ b/doc/ffmpeg.texi
@@ -415,6 +415,11 @@  This option is similar to @option{-filter}, the only difference is that its
 argument is the name of the file from which a filtergraph description is to be
 read.
 
+@item -filter_threads @var{nb_thraeds} (@emph{global})
+Defines how many threads are used to process a filter pipeline.
+Some filters support parallel processing. The default is the number of available
+CPUs.
+
 @item -pre[:@var{stream_specifier}] @var{preset_name} (@emph{output,per-stream})
 Specify the preset for matching stream(s).
 
@@ -1201,6 +1206,11 @@  To generate 5 seconds of pure red video using lavfi @code{color} source:
 ffmpeg -filter_complex 'color=c=red' -t 5 out.mkv
 @end example
 
+@item -filter_complex_threads @var{nb_threads} (@emph{global})
+Defines how many threads are used to process a @code{-filter_complex} pipeline.
+Some filters support parallel processing. The default is the number of available
+CPUs.
+
 @item -lavfi @var{filtergraph} (@emph{global})
 Define a complex filtergraph, i.e. one with arbitrary number of inputs and/or
 outputs. Equivalent to @option{-filter_complex}.
diff --git a/ffmpeg.h b/ffmpeg.h
index e1d4593..9a4389f 100644
--- a/ffmpeg.h
+++ b/ffmpeg.h
@@ -569,6 +569,9 @@  extern AVIOContext *progress_avio;
 extern float max_error_rate;
 extern char *videotoolbox_pixfmt;
 
+extern int filter_nbthreads;
+extern int filter_complex_nbthreads;
+
 extern const AVIOInterruptCB int_cb;
 
 extern const OptionDef options[];
diff --git a/ffmpeg_filter.c b/ffmpeg_filter.c
index 27aeca0..4554456 100644
--- a/ffmpeg_filter.c
+++ b/ffmpeg_filter.c
@@ -39,6 +39,9 @@ 
 #include "libavutil/imgutils.h"
 #include "libavutil/samplefmt.h"
 
+int filter_nbthreads = -1;
+int filter_complex_nbthreads = -1;
+
 static const enum AVPixelFormat *get_compliance_unofficial_pix_fmts(enum AVCodecID codec_id, const enum AVPixelFormat default_formats[])
 {
     static const enum AVPixelFormat mjpeg_formats[] =
@@ -309,6 +312,8 @@  int init_complex_filtergraph(FilterGraph *fg)
     if (!graph)
         return AVERROR(ENOMEM);
 
+    // As a temporary graph, don't bother making threads
+    graph->nb_threads = 1;
     ret = avfilter_graph_parse2(graph, fg->graph_desc, &inputs, &outputs);
     if (ret < 0)
         goto fail;
@@ -992,6 +997,8 @@  int configure_filtergraph(FilterGraph *fg)
         char args[512];
         AVDictionaryEntry *e = NULL;
 
+        fg->graph->nb_threads = filter_complex_nbthreads;
+
         args[0] = 0;
         while ((e = av_dict_get(ost->sws_dict, "", e,
                                 AV_DICT_IGNORE_SUFFIX))) {
@@ -1022,6 +1029,8 @@  int configure_filtergraph(FilterGraph *fg)
         e = av_dict_get(ost->encoder_opts, "threads", NULL, 0);
         if (e)
             av_opt_set(fg->graph, "threads", e->value, 0);
+    } else {
+        fg->graph->nb_threads = filter_nbthreads;
     }
 
     if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
diff --git a/ffmpeg_opt.c b/ffmpeg_opt.c
index 4d25fff..dc94380 100644
--- a/ffmpeg_opt.c
+++ b/ffmpeg_opt.c
@@ -3365,12 +3365,16 @@  const OptionDef options[] = {
         "set profile", "profile" },
     { "filter",         HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(filters) },
         "set stream filtergraph", "filter_graph" },
+    { "filter_threads",  HAS_ARG | OPT_INT,                          { &filter_nbthreads },
+        "number of non-complex filter threads" },
     { "filter_script",  HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(filter_scripts) },
         "read stream filtergraph description from a file", "filename" },
     { "reinit_filter",  HAS_ARG | OPT_INT | OPT_SPEC | OPT_INPUT,    { .off = OFFSET(reinit_filters) },
         "reinit filtergraph on input parameter changes", "" },
     { "filter_complex", HAS_ARG | OPT_EXPERT,                        { .func_arg = opt_filter_complex },
         "create a complex filtergraph", "graph_description" },
+    { "filter_complex_threads", HAS_ARG | OPT_INT,                   { &filter_complex_nbthreads },
+        "number of threads for -filter_complex" },
     { "lavfi",          HAS_ARG | OPT_EXPERT,                        { .func_arg = opt_filter_complex },
         "create a complex filtergraph", "graph_description" },
     { "filter_complex_script", HAS_ARG | OPT_EXPERT,                 { .func_arg = opt_filter_complex_script },