diff mbox series

[FFmpeg-devel,3/7] lavfi: add jobs args

Message ID 20220920175021.60790-4-rcombs@rcombs.me
State New
Headers show
Series [FFmpeg-devel,1/7] lavu/cpu: add av_cpu_job_count() | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/configure_x86 warning Failed to run configure

Commit Message

rcombs Sept. 20, 2022, 5:50 p.m. UTC
This allows tuning the number of tasks that frames are sliced into
independently from the number of execution pool threads, which can improve
performance significantly on asymmetric-multiprocessing systems.
---
 doc/APIchanges              |  3 +++
 libavfilter/avfilter.c      |  9 +++++++++
 libavfilter/avfilter.h      | 14 ++++++++++++++
 libavfilter/avfiltergraph.c |  4 ++++
 libavfilter/internal.h      |  6 ++++++
 libavfilter/pthread.c       |  5 +++++
 libavfilter/version.h       |  2 +-
 7 files changed, 42 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/doc/APIchanges b/doc/APIchanges
index 6059b495dd..ae8b0bf0b4 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,9 @@  libavutil:     2021-04-27
 
 API changes, most recent first:
 
+2022-09-20 - xxxxxxxxxx - lavfi 8.50.100 - avfilter.h
+  Add AVFilterContext.nb_jobs and AVFilterGraph.nb_jobs.
+
 2022-09-20 - xxxxxxxxxx - lavu 57.37.100 - cpu.h
   Add av_cpu_job_count() and av_cpu_force_job_count().
 
diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c
index f34204e650..4e16e312c6 100644
--- a/libavfilter/avfilter.c
+++ b/libavfilter/avfilter.c
@@ -621,6 +621,8 @@  static const AVOption avfilter_options[] = {
     { "enable", "set enable expression", OFFSET(enable_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = TFLAGS },
     { "threads", "Allowed number of threads", OFFSET(nb_threads), AV_OPT_TYPE_INT,
         { .i64 = 0 }, 0, INT_MAX, FLAGS },
+    { "jobs", "Allowed number of jobs", OFFSET(nb_jobs), AV_OPT_TYPE_INT,
+        { .i64 = 0 }, 0, INT_MAX, FLAGS },
     { "extra_hw_frames", "Number of extra hardware frames to allocate for the user",
         OFFSET(extra_hw_frames), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
     { NULL },
@@ -797,6 +799,13 @@  int ff_filter_get_nb_threads(AVFilterContext *ctx)
     return ctx->graph->nb_threads;
 }
 
+int ff_filter_get_nb_jobs(AVFilterContext *ctx)
+{
+    if (ctx->nb_jobs > 0)
+        return FFMIN(ctx->nb_jobs, ctx->graph->nb_jobs);
+    return ctx->graph->nb_jobs;
+}
+
 static int process_options(AVFilterContext *ctx, AVDictionary **options,
                            const char *args)
 {
diff --git a/libavfilter/avfilter.h b/libavfilter/avfilter.h
index 2e8197c9a6..aadeadd41c 100644
--- a/libavfilter/avfilter.h
+++ b/libavfilter/avfilter.h
@@ -492,6 +492,13 @@  struct AVFilterContext {
      * configured.
      */
     int extra_hw_frames;
+
+    /**
+     * Max number of jobs allowed in this filter instance.
+     * If <= 0, its value is ignored.
+     * Overrides global number of jobs set per filter graph.
+     */
+    int nb_jobs;
 };
 
 /**
@@ -935,6 +942,13 @@  typedef struct AVFilterGraph {
     int sink_links_count;
 
     unsigned disable_auto_convert;
+
+    /**
+     * Maximum number of jobs used by filters in this graph. May be set by
+     * the caller before adding any filters to the filtergraph. Zero (the
+     * default) means that the number of jobs is determined automatically.
+     */
+    int nb_jobs;
 } AVFilterGraph;
 
 /**
diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c
index 53f468494d..4aac62c6c3 100644
--- a/libavfilter/avfiltergraph.c
+++ b/libavfilter/avfiltergraph.c
@@ -51,6 +51,9 @@  static const AVOption filtergraph_options[] = {
     { "threads",     "Maximum number of threads", OFFSET(nb_threads), AV_OPT_TYPE_INT,
         { .i64 = 0 }, 0, INT_MAX, F|V|A, "threads"},
         {"auto", "autodetect a suitable number of threads to use", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, .flags = F|V|A, .unit = "threads"},
+    { "jobs",        "Maximum number of jobs", OFFSET(nb_jobs), AV_OPT_TYPE_INT,
+        { .i64 = 0 }, 0, INT_MAX, F|V|A, "jobs"},
+        {"auto", "autodetect a suitable number of jobs to use", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, .flags = F|V|A, .unit = "jobs"},
     {"scale_sws_opts"       , "default scale filter options"        , OFFSET(scale_sws_opts)        ,
         AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, F|V },
     {"aresample_swr_opts"   , "default aresample filter options"    , OFFSET(aresample_swr_opts)    ,
@@ -75,6 +78,7 @@  int ff_graph_thread_init(AVFilterGraph *graph)
 {
     graph->thread_type = 0;
     graph->nb_threads  = 1;
+    graph->nb_jobs     = 1;
     return 0;
 }
 #endif
diff --git a/libavfilter/internal.h b/libavfilter/internal.h
index 0128820be0..be05e8550b 100644
--- a/libavfilter/internal.h
+++ b/libavfilter/internal.h
@@ -384,6 +384,12 @@  int ff_filter_graph_run_once(AVFilterGraph *graph);
  */
 int ff_filter_get_nb_threads(AVFilterContext *ctx) av_pure;
 
+/**
+ * Get number of jobs for current filter instance.
+ * This number is always same or less than graph->nb_jobs.
+ */
+int ff_filter_get_nb_jobs(AVFilterContext *ctx) av_pure;
+
 /**
  * Generic processing of user supplied commands that are set
  * in the same way as the filter options.
diff --git a/libavfilter/pthread.c b/libavfilter/pthread.c
index 1a063d3cc0..589e32e263 100644
--- a/libavfilter/pthread.c
+++ b/libavfilter/pthread.c
@@ -23,6 +23,7 @@ 
 
 #include <stddef.h>
 
+#include "libavutil/cpu.h"
 #include "libavutil/error.h"
 #include "libavutil/macros.h"
 #include "libavutil/mem.h"
@@ -98,10 +99,14 @@  int ff_graph_thread_init(AVFilterGraph *graph)
         av_freep(&graph->internal->thread);
         graph->thread_type = 0;
         graph->nb_threads  = 1;
+        graph->nb_jobs     = 1;
         return (ret < 0) ? ret : 0;
     }
     graph->nb_threads = ret;
 
+    if (graph->nb_jobs < 1)
+        graph->nb_jobs = av_cpu_job_count();
+
     graph->internal->thread_execute = thread_execute;
 
     return 0;
diff --git a/libavfilter/version.h b/libavfilter/version.h
index 5aac9c513a..436c2b8b17 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -31,7 +31,7 @@ 
 
 #include "version_major.h"
 
-#define LIBAVFILTER_VERSION_MINOR  49
+#define LIBAVFILTER_VERSION_MINOR  50
 #define LIBAVFILTER_VERSION_MICRO 100