avfilter/vf_stack: add slice threading
authorPaul B Mahol <onemda@gmail.com>
Wed, 25 Sep 2019 17:02:57 +0000 (19:02 +0200)
committerPaul B Mahol <onemda@gmail.com>
Wed, 25 Sep 2019 17:02:57 +0000 (19:02 +0200)
libavfilter/vf_stack.c

index 3b05807..b9e87a4 100644 (file)
@@ -116,6 +116,29 @@ static av_cold int init(AVFilterContext *ctx)
     return 0;
 }
 
+static int process_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
+{
+    StackContext *s = ctx->priv;
+    AVFrame *out = arg;
+    AVFrame **in = s->frames;
+    const int start = (s->nb_inputs *  job   ) / nb_jobs;
+    const int end   = (s->nb_inputs * (job+1)) / nb_jobs;
+
+    for (int i = start; i < end; i++) {
+        StackItem *item = &s->items[i];
+
+        for (int p = 0; p < s->nb_planes; p++) {
+            av_image_copy_plane(out->data[p] + out->linesize[p] * item->y[p] + item->x[p],
+                                out->linesize[p],
+                                in[i]->data[p],
+                                in[i]->linesize[p],
+                                item->linesize[p], item->height[p]);
+        }
+    }
+
+    return 0;
+}
+
 static int process_frame(FFFrameSync *fs)
 {
     AVFilterContext *ctx = fs->parent;
@@ -123,7 +146,7 @@ static int process_frame(FFFrameSync *fs)
     StackContext *s = fs->opaque;
     AVFrame **in = s->frames;
     AVFrame *out;
-    int i, p, ret;
+    int i, ret;
 
     for (i = 0; i < s->nb_inputs; i++) {
         if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
@@ -136,17 +159,7 @@ static int process_frame(FFFrameSync *fs)
     out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
     out->sample_aspect_ratio = outlink->sample_aspect_ratio;
 
-    for (i = 0; i < s->nb_inputs; i++) {
-        StackItem *item = &s->items[i];
-
-        for (p = 0; p < s->nb_planes; p++) {
-            av_image_copy_plane(out->data[p] + out->linesize[p] * item->y[p] + item->x[p],
-                                out->linesize[p],
-                                in[i]->data[p],
-                                in[i]->linesize[p],
-                                item->linesize[p], item->height[p]);
-        }
-    }
+    ctx->internal->execute(ctx, process_slice, out, NULL, FFMIN(s->nb_inputs, ff_filter_get_nb_threads(ctx)));
 
     return ff_filter_frame(outlink, out);
 }
@@ -370,7 +383,7 @@ AVFilter ff_vf_hstack = {
     .init          = init,
     .uninit        = uninit,
     .activate      = activate,
-    .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS,
+    .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS,
 };
 
 #endif /* CONFIG_HSTACK_FILTER */
@@ -390,7 +403,7 @@ AVFilter ff_vf_vstack = {
     .init          = init,
     .uninit        = uninit,
     .activate      = activate,
-    .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS,
+    .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS,
 };
 
 #endif /* CONFIG_VSTACK_FILTER */
@@ -416,7 +429,7 @@ AVFilter ff_vf_xstack = {
     .init          = init,
     .uninit        = uninit,
     .activate      = activate,
-    .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS,
+    .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS,
 };
 
 #endif /* CONFIG_XSTACK_FILTER */