[FFmpeg-devel] [PATCH] lavfi/noise: support slice threading

Paul B Mahol onemda at gmail.com
Sun May 26 19:43:07 CEST 2013


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---

Alternative version.

---
 libavfilter/vf_noise.c | 85 +++++++++++++++++++++++++++++---------------------
 1 file changed, 49 insertions(+), 36 deletions(-)

diff --git a/libavfilter/vf_noise.c b/libavfilter/vf_noise.c
index 12aca24..7ae6c53 100644
--- a/libavfilter/vf_noise.c
+++ b/libavfilter/vf_noise.c
@@ -47,7 +47,6 @@
 typedef struct {
     int strength;
     unsigned flags;
-    int shiftptr;
     AVLFG lfg;
     int seed;
     int8_t *noise;
@@ -67,6 +66,10 @@ typedef struct {
     void (*line_noise_avg)(uint8_t *dst, const uint8_t *src, int len, int8_t **shift);
 } NoiseContext;
 
+typedef struct ThreadData {
+    AVFrame *in, *out;
+} ThreadData;
+
 #define OFFSET(x) offsetof(NoiseContext, x)
 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
@@ -161,7 +164,6 @@ static int init_noise(NoiseContext *n, int comp)
     }
 
     fp->noise = noise;
-    fp->shiftptr = 0;
     return 0;
 }
 
@@ -328,48 +330,60 @@ static inline void line_noise_avg_mmx(uint8_t *dst, const uint8_t *src,
 #endif
 }
 
-static void noise(uint8_t *dst, const uint8_t *src,
-                  int dst_linesize, int src_linesize,
-                  int width, int height, NoiseContext *n, int comp)
+static int noise(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
-    int8_t *noise = n->param[comp].noise;
-    int flags = n->param[comp].flags;
-    AVLFG *lfg = &n->param[comp].lfg;
-    int shift, y;
+    NoiseContext *n = ctx->priv;
+    ThreadData *td = arg;
+    int i;
 
-    if (!noise) {
-        if (dst != src)
-            av_image_copy_plane(dst, dst_linesize, src, src_linesize, width, height);
-        return;
-    }
+    for (i = 0; i < n->nb_planes; i++) {
+        FilterParams *p  = &n->param[i];
+        AVLFG *lfg       = &p->lfg;
+        int8_t *noise    = p->noise;
+        const int height = n->height[i];
+        const int width  = n->linesize[i];
+        const int flags  = p->flags;
+        const int start  = (height *  jobnr   ) / nb_jobs;
+        const int end    = (height * (jobnr+1)) / nb_jobs;
+        uint8_t *dst = td->out->data[i] + start * td->out->linesize[i];
+        const uint8_t *src = td->in->data[i] + start * td->in->linesize[i];
+        int shift, y;
+
+        if (!noise) {
+            if (dst != src)
+                av_image_copy_plane(dst, td->out->linesize[i],
+                                    src, td->in->linesize[i],
+                                    width, end - start);
+            continue;
+        }
 
-    for (y = 0; y < height; y++) {
-        if (flags & NOISE_TEMPORAL)
-            shift = av_lfg_get(lfg) & (MAX_SHIFT - 1);
-        else
-            shift = n->rand_shift[y];
+        for (y = start; y < end; y++) {
+            if (flags & NOISE_TEMPORAL)
+                shift = av_lfg_get(lfg) & (MAX_SHIFT - 1);
+            else
+                shift = n->rand_shift[y];
 
-        if (flags & NOISE_AVERAGED) {
-            n->line_noise_avg(dst, src, width, n->param[comp].prev_shift[y]);
-            n->param[comp].prev_shift[y][n->param[comp].shiftptr] = noise + shift;
-        } else {
-            n->line_noise(dst, src, noise, width, shift);
+            if (flags & NOISE_AVERAGED) {
+                n->line_noise_avg(dst, src, width, p->prev_shift[y]);
+                p->prev_shift[y][shift & 3] = noise + shift;
+            } else {
+                n->line_noise(dst, src, noise, width, shift);
+            }
+            dst += td->out->linesize[i];
+            src += td->in->linesize[i];
         }
-        dst += dst_linesize;
-        src += src_linesize;
     }
 
-    n->param[comp].shiftptr++;
-    if (n->param[comp].shiftptr == 3)
-        n->param[comp].shiftptr = 0;
+    return 0;
 }
 
 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
 {
-    NoiseContext *n = inlink->dst->priv;
-    AVFilterLink *outlink = inlink->dst->outputs[0];
+    AVFilterContext *ctx = inlink->dst;
+    AVFilterLink *outlink = ctx->outputs[0];
+    NoiseContext *n = ctx->priv;
+    ThreadData td;
     AVFrame *out;
-    int i;
 
     if (av_frame_is_writable(inpicref)) {
         out = inpicref;
@@ -382,9 +396,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
         av_frame_copy_props(out, inpicref);
     }
 
-    for (i = 0; i < n->nb_planes; i++)
-        noise(out->data[i], inpicref->data[i], out->linesize[i],
-              inpicref->linesize[i], n->linesize[i], n->height[i], n, i);
+    td.in = inpicref; td.out = out;
+    ctx->internal->execute(ctx, noise, &td, NULL, FFMIN(n->height[0], ctx->graph->nb_threads));
     emms_c();
 
     if (inpicref != out)
@@ -463,5 +476,5 @@ AVFilter avfilter_vf_noise = {
     .inputs        = noise_inputs,
     .outputs       = noise_outputs,
     .priv_class    = &noise_class,
-    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
 };
-- 
1.7.11.2



More information about the ffmpeg-devel mailing list