diff mbox

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

Message ID E1c0q7H-0000oM-6R@rubyweapon.execulink.net
State Accepted
Commit 6d50dff816ec30e589ac71e44824f7fb6c34fd2e
Headers show

Commit Message

DHE Oct. 30, 2016, 1:25 p.m. UTC
Enables specifying how many threads are available to each filtergraph.
---
v2->v3:
- Typos in docs fixed

v1->v2:
- Reworded documentation
- Removed hunk from ffmpeg_filter.c not directly applicable to patch

 doc/ffmpeg.texi | 10 ++++++++++
 ffmpeg.h        |  3 +++
 ffmpeg_filter.c |  7 +++++++
 ffmpeg_opt.c    |  4 ++++
 4 files changed, 24 insertions(+)

Comments

Paul B Mahol Oct. 30, 2016, 4:09 p.m. UTC | #1
On 10/30/16, DeHackEd <git@dehacked.net> wrote:
> Enables specifying how many threads are available to each filtergraph.
> ---
> v2->v3:
> - Typos in docs fixed
>
> v1->v2:
> - Reworded documentation
> - Removed hunk from ffmpeg_filter.c not directly applicable to patch
>
>  doc/ffmpeg.texi | 10 ++++++++++
>  ffmpeg.h        |  3 +++
>  ffmpeg_filter.c |  7 +++++++
>  ffmpeg_opt.c    |  4 ++++
>  4 files changed, 24 insertions(+)
>
> diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi
> index 47c8935..fd8a0c1 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_threads} (@emph{global})
> +Defines how many threads are used to process a filter pipeline. Each
> pipeline
> +will produce a thread pool with this many threads available for 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 filter_complex graph.
> +Similar to filter_threads but used for @code{-filter_complex} graphs only.
> +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..95b9c43 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[] =
> @@ -992,6 +995,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 +1027,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
>

How this plays out with number of threads one can set for each filter instance?
DHE Oct. 30, 2016, 6:39 p.m. UTC | #2
On 10/30/2016 12:09 PM, Paul B Mahol wrote:
> On 10/30/16, DeHackEd <git@dehacked.net> wrote:
>> Enables specifying how many threads are available to each filtergraph.
>> ---
>> v2->v3:
>> - Typos in docs fixed
>>
>> v1->v2:
>> - Reworded documentation
>> - Removed hunk from ffmpeg_filter.c not directly applicable to patch
>>
>>  doc/ffmpeg.texi | 10 ++++++++++
>>  ffmpeg.h        |  3 +++
>>  ffmpeg_filter.c |  7 +++++++
>>  ffmpeg_opt.c    |  4 ++++
>>  4 files changed, 24 insertions(+)
>>
...
>> 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
>>
> 
> How this plays out with number of threads one can set for each filter instance?

Each individual filter uses MIN(filter_specific_limit, filtergrpah_limit) threads, but regardless the filtergraph will
create filtergraph_limit threads each time. This patch adds user control to the filtergraph_limit.

My main motivation for this parameter is a system I have with 80 CPUs (including hyperthreads). The video filtering
workload does not benefit from so many threads being created or used, and I suffer from a significant ulimit hit and
greater difficulty debugging.

> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
Paul B Mahol Oct. 30, 2016, 9:13 p.m. UTC | #3
On 10/30/16, DeHackEd <git@dehacked.net> wrote:
> On 10/30/2016 12:09 PM, Paul B Mahol wrote:
>> On 10/30/16, DeHackEd <git@dehacked.net> wrote:
>>> Enables specifying how many threads are available to each filtergraph.
>>> ---
>>> v2->v3:
>>> - Typos in docs fixed
>>>
>>> v1->v2:
>>> - Reworded documentation
>>> - Removed hunk from ffmpeg_filter.c not directly applicable to patch
>>>
>>>  doc/ffmpeg.texi | 10 ++++++++++
>>>  ffmpeg.h        |  3 +++
>>>  ffmpeg_filter.c |  7 +++++++
>>>  ffmpeg_opt.c    |  4 ++++
>>>  4 files changed, 24 insertions(+)
>>>
> ...
>>> 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
>>>
>>
>> How this plays out with number of threads one can set for each filter
>> instance?
>
> Each individual filter uses MIN(filter_specific_limit, filtergrpah_limit)
> threads, but regardless the filtergraph will
> create filtergraph_limit threads each time. This patch adds user control to
> the filtergraph_limit.
>
> My main motivation for this parameter is a system I have with 80 CPUs
> (including hyperthreads). The video filtering
> workload does not benefit from so many threads being created or used, and I
> suffer from a significant ulimit hit and
> greater difficulty debugging.

Idea sounds fine, I will leave code review to someone else.

>
>> _______________________________________________
>> ffmpeg-devel mailing list
>> ffmpeg-devel@ffmpeg.org
>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>>
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
DHE Nov. 5, 2016, 4:54 p.m. UTC | #4
On 10/30/2016 09:25 AM, DeHackEd wrote:
> Enables specifying how many threads are available to each filtergraph.
> ---
> v2->v3:
> - Typos in docs fixed
> 
> v1->v2:
> - Reworded documentation
> - Removed hunk from ffmpeg_filter.c not directly applicable to patch
> 
>  doc/ffmpeg.texi | 10 ++++++++++
>  ffmpeg.h        |  3 +++
>  ffmpeg_filter.c |  7 +++++++
>  ffmpeg_opt.c    |  4 ++++
>  4 files changed, 24 insertions(+)
> 
> diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi
> index 47c8935..fd8a0c1 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_threads} (@emph{global})
> +Defines how many threads are used to process a filter pipeline. Each pipeline
> +will produce a thread pool with this many threads available for 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 filter_complex graph.
> +Similar to filter_threads but used for @code{-filter_complex} graphs only.
> +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..95b9c43 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[] =
> @@ -992,6 +995,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 +1027,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 },
> 

All outstanding issues were fixed in this "v3" patch. Bumping for visibility (last email was from the end of the month
in October)
Michael Niedermayer Nov. 5, 2016, 6:51 p.m. UTC | #5
On Sun, Oct 30, 2016 at 09:25:39AM -0400, DeHackEd wrote:
> Enables specifying how many threads are available to each filtergraph.
> ---
> v2->v3:
> - Typos in docs fixed
> 
> v1->v2:
> - Reworded documentation
> - Removed hunk from ffmpeg_filter.c not directly applicable to patch
> 
>  doc/ffmpeg.texi | 10 ++++++++++
>  ffmpeg.h        |  3 +++
>  ffmpeg_filter.c |  7 +++++++
>  ffmpeg_opt.c    |  4 ++++
>  4 files changed, 24 insertions(+)

Is "DeHackEd" the intended Author name for the commit ?

[...]
DHE Nov. 6, 2016, 1:17 a.m. UTC | #6
On 11/05/2016 02:51 PM, Michael Niedermayer wrote:
> On Sun, Oct 30, 2016 at 09:25:39AM -0400, DeHackEd wrote:
>> Enables specifying how many threads are available to each filtergraph.
>> ---
>> v2->v3:
>> - Typos in docs fixed
>>
>> v1->v2:
>> - Reworded documentation
>> - Removed hunk from ffmpeg_filter.c not directly applicable to patch
>>
>>  doc/ffmpeg.texi | 10 ++++++++++
>>  ffmpeg.h        |  3 +++
>>  ffmpeg_filter.c |  7 +++++++
>>  ffmpeg_opt.c    |  4 ++++
>>  4 files changed, 24 insertions(+)
> 
> Is "DeHackEd" the intended Author name for the commit ?
> 

"DeHackEd" is the name I usually go by, but some patches read "DHE". That's my own fault, but either is fine.
Michael Niedermayer Nov. 6, 2016, 11:44 a.m. UTC | #7
On Sun, Oct 30, 2016 at 09:25:39AM -0400, DeHackEd wrote:
[...]

>      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 },

why are there 2 options instead of 1 ?

[...]
DHE Nov. 6, 2016, 2:20 p.m. UTC | #8
On 11/06/2016 06:44 AM, Michael Niedermayer wrote:
> On Sun, Oct 30, 2016 at 09:25:39AM -0400, DeHackEd wrote:
> [...]
> 
>>      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 },
> 
> why are there 2 options instead of 1 ?
> 

One affects -filter_complex filtergraphs, one affects -af and -vf filtergraphs...

The main motivation was that you typically have one -filter_complex graph, but could have many -af and -vf filtergraphs,
so if you are trying to limit how many threads you use then you would have different needs depending on which type you
are using.
Michael Niedermayer Nov. 6, 2016, 2:53 p.m. UTC | #9
On Sun, Nov 06, 2016 at 09:20:33AM -0500, DeHackEd wrote:
> On 11/06/2016 06:44 AM, Michael Niedermayer wrote:
> > On Sun, Oct 30, 2016 at 09:25:39AM -0400, DeHackEd wrote:
> > [...]
> > 
> >>      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 },
> > 
> > why are there 2 options instead of 1 ?
> > 
> 
> One affects -filter_complex filtergraphs, one affects -af and -vf filtergraphs...
> 
> The main motivation was that you typically have one -filter_complex graph, but could have many -af and -vf filtergraphs,
> so if you are trying to limit how many threads you use then you would have different needs depending on which type you
> are using.

ok, makes sense

patch applied

thx

[...]
Marton Balint Nov. 24, 2016, 2:32 p.m. UTC | #10
On Sun, 6 Nov 2016, Michael Niedermayer wrote:

> On Sun, Nov 06, 2016 at 09:20:33AM -0500, DeHackEd wrote:
>> On 11/06/2016 06:44 AM, Michael Niedermayer wrote:
>>> On Sun, Oct 30, 2016 at 09:25:39AM -0400, DeHackEd wrote:
>>> [...]
>>>
>>>>      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 },
>>>
>>> why are there 2 options instead of 1 ?
>>>
>>
>> One affects -filter_complex filtergraphs, one affects -af and -vf filtergraphs...
>>
>> The main motivation was that you typically have one -filter_complex graph, but could have many -af and -vf filtergraphs,
>> so if you are trying to limit how many threads you use then you would have different needs depending on which type you
>> are using.
>
> ok, makes sense
>
> patch applied
>

It seems to me this patch disabled filter threading by default. Is this 
intentional?

Thanks,
Marton
Michael Niedermayer Nov. 24, 2016, 3:04 p.m. UTC | #11
On Thu, Nov 24, 2016 at 03:32:13PM +0100, Marton Balint wrote:
> 
> 
> On Sun, 6 Nov 2016, Michael Niedermayer wrote:
> 
> >On Sun, Nov 06, 2016 at 09:20:33AM -0500, DeHackEd wrote:
> >>On 11/06/2016 06:44 AM, Michael Niedermayer wrote:
> >>>On Sun, Oct 30, 2016 at 09:25:39AM -0400, DeHackEd wrote:
> >>>[...]
> >>>
> >>>>     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 },
> >>>
> >>>why are there 2 options instead of 1 ?
> >>>
> >>
> >>One affects -filter_complex filtergraphs, one affects -af and -vf filtergraphs...
> >>
> >>The main motivation was that you typically have one -filter_complex graph, but could have many -af and -vf filtergraphs,
> >>so if you are trying to limit how many threads you use then you would have different needs depending on which type you
> >>are using.
> >
> >ok, makes sense
> >
> >patch applied
> >
> 
> It seems to me this patch disabled filter threading by default. Is
> this intentional?

not from my side for sure

[...]
diff mbox

Patch

diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi
index 47c8935..fd8a0c1 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_threads} (@emph{global})
+Defines how many threads are used to process a filter pipeline. Each pipeline
+will produce a thread pool with this many threads available for 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 filter_complex graph.
+Similar to filter_threads but used for @code{-filter_complex} graphs only.
+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..95b9c43 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[] =
@@ -992,6 +995,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 +1027,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 },