avfilter/vsrc_testsrc: fix a preprocessor check
[ffmpeg.git] / libavfilter / vf_dctdnoiz.c
index fe1c5d7..cdbe5f8 100644 (file)
 static const char *const var_names[] = { "c", NULL };
 enum { VAR_C, VAR_VARS_NB };
 
+#define MAX_THREADS 8
+
 typedef struct DCTdnoizContext {
     const AVClass *class;
 
     /* coefficient factor expression */
     char *expr_str;
-    AVExpr *expr;
-    double var_values[VAR_VARS_NB];
+    AVExpr *expr[MAX_THREADS];
+    double var_values[MAX_THREADS][VAR_VARS_NB];
 
+    int nb_threads;
     int pr_width, pr_height;    // width and height to process
     float sigma;                // used when no expression are st
     float th;                   // threshold (3*sigma)
     float *cbuf[2][3];          // two planar rgb color buffers
+    float *slices[MAX_THREADS]; // slices buffers (1 slice buffer per thread)
     float *weights;             // dct coeff are cumulated with overlapping; these values are used for averaging
     int p_linesize;             // line sizes for color and weights
     int overlap;                // number of block overlapping pixels
@@ -56,11 +60,12 @@ typedef struct DCTdnoizContext {
     int bsize;                  // block size, 1<<n
     void (*filter_freq_func)(struct DCTdnoizContext *s,
                              const float *src, int src_linesize,
-                             float *dst, int dst_linesize);
+                             float *dst, int dst_linesize,
+                             int thread_id);
     void (*color_decorrelation)(float **dst, int dst_linesize,
-                                const uint8_t *src, int src_linesize,
+                                const uint8_t **src, int src_linesize,
                                 int w, int h);
-    void (*color_correlation)(uint8_t *dst, int dst_linesize,
+    void (*color_correlation)(uint8_t **dst, int dst_linesize,
                               float **src, int src_linesize,
                               int w, int h);
 } DCTdnoizContext;
@@ -362,10 +367,10 @@ static av_always_inline void filter_freq_##bsize(const float *src, int src_lines
         float *b = &tmp_block2[i];                                                          \
         /* frequency filtering */                                                           \
         if (expr) {                                                                         \
-            var_values[VAR_C] = FFABS(*b);                                                  \
+            var_values[VAR_C] = fabsf(*b);                                                  \
             *b *= av_expr_eval(expr, var_values, NULL);                                     \
         } else {                                                                            \
-            if (FFABS(*b) < sigma_th)                                                       \
+            if (fabsf(*b) < sigma_th)                                                       \
                 *b = 0;                                                                     \
         }                                                                                   \
     }                                                                                       \
@@ -377,16 +382,17 @@ static av_always_inline void filter_freq_##bsize(const float *src, int src_lines
                                                                                             \
 static void filter_freq_sigma_##bsize(DCTdnoizContext *s,                                   \
                                       const float *src, int src_linesize,                   \
-                                      float *dst, int dst_linesize)                         \
+                                      float *dst, int dst_linesize, int thread_id)          \
 {                                                                                           \
     filter_freq_##bsize(src, src_linesize, dst, dst_linesize, NULL, NULL, s->th);           \
 }                                                                                           \
                                                                                             \
 static void filter_freq_expr_##bsize(DCTdnoizContext *s,                                    \
                                      const float *src, int src_linesize,                    \
-                                     float *dst, int dst_linesize)                          \
+                                     float *dst, int dst_linesize, int thread_id)           \
 {                                                                                           \
-    filter_freq_##bsize(src, src_linesize, dst, dst_linesize, s->expr, s->var_values, 0);   \
+    filter_freq_##bsize(src, src_linesize, dst, dst_linesize,                               \
+                        s->expr[thread_id], s->var_values[thread_id], 0);                   \
 }
 
 DEF_FILTER_FREQ_FUNCS(8)
@@ -402,7 +408,7 @@ DEF_FILTER_FREQ_FUNCS(16)
 #define DCT3X3_2_2  0.4082482904638631f /*  1/sqrt(6) */
 
 static av_always_inline void color_decorrelation(float **dst, int dst_linesize,
-                                                 const uint8_t *src, int src_linesize,
+                                                 const uint8_t **src, int src_linesize,
                                                  int w, int h,
                                                  int r, int g, int b)
 {
@@ -410,24 +416,23 @@ static av_always_inline void color_decorrelation(float **dst, int dst_linesize,
     float *dstp_r = dst[0];
     float *dstp_g = dst[1];
     float *dstp_b = dst[2];
+    const uint8_t *srcp = src[0];
 
     for (y = 0; y < h; y++) {
-        const uint8_t *srcp = src;
-
         for (x = 0; x < w; x++) {
             dstp_r[x] = srcp[r] * DCT3X3_0_0 + srcp[g] * DCT3X3_0_1 + srcp[b] * DCT3X3_0_2;
             dstp_g[x] = srcp[r] * DCT3X3_1_0 +                        srcp[b] * DCT3X3_1_2;
             dstp_b[x] = srcp[r] * DCT3X3_2_0 + srcp[g] * DCT3X3_2_1 + srcp[b] * DCT3X3_2_2;
             srcp += 3;
         }
-        src += src_linesize;
+        srcp   += src_linesize - w * 3;
         dstp_r += dst_linesize;
         dstp_g += dst_linesize;
         dstp_b += dst_linesize;
     }
 }
 
-static av_always_inline void color_correlation(uint8_t *dst, int dst_linesize,
+static av_always_inline void color_correlation(uint8_t **dst, int dst_linesize,
                                                float **src, int src_linesize,
                                                int w, int h,
                                                int r, int g, int b)
@@ -436,17 +441,16 @@ static av_always_inline void color_correlation(uint8_t *dst, int dst_linesize,
     const float *src_r = src[0];
     const float *src_g = src[1];
     const float *src_b = src[2];
+    uint8_t *dstp = dst[0];
 
     for (y = 0; y < h; y++) {
-        uint8_t *dstp = dst;
-
         for (x = 0; x < w; x++) {
             dstp[r] = av_clip_uint8(src_r[x] * DCT3X3_0_0 + src_g[x] * DCT3X3_1_0 + src_b[x] * DCT3X3_2_0);
             dstp[g] = av_clip_uint8(src_r[x] * DCT3X3_0_1 +                         src_b[x] * DCT3X3_2_1);
             dstp[b] = av_clip_uint8(src_r[x] * DCT3X3_0_2 + src_g[x] * DCT3X3_1_2 + src_b[x] * DCT3X3_2_2);
             dstp += 3;
         }
-        dst += dst_linesize;
+        dstp  += dst_linesize - w * 3;
         src_r += src_linesize;
         src_g += src_linesize;
         src_b += src_linesize;
@@ -455,13 +459,13 @@ static av_always_inline void color_correlation(uint8_t *dst, int dst_linesize,
 
 #define DECLARE_COLOR_FUNCS(name, r, g, b)                                          \
 static void color_decorrelation_##name(float **dst, int dst_linesize,               \
-                                       const uint8_t *src, int src_linesize,        \
+                                       const uint8_t **src, int src_linesize,       \
                                        int w, int h)                                \
 {                                                                                   \
     color_decorrelation(dst, dst_linesize, src, src_linesize, w, h, r, g, b);       \
 }                                                                                   \
                                                                                     \
-static void color_correlation_##name(uint8_t *dst, int dst_linesize,                \
+static void color_correlation_##name(uint8_t **dst, int dst_linesize,               \
                                      float **src, int src_linesize,                 \
                                      int w, int h)                                  \
 {                                                                                   \
@@ -471,11 +475,65 @@ static void color_correlation_##name(uint8_t *dst, int dst_linesize,
 DECLARE_COLOR_FUNCS(rgb, 0, 1, 2)
 DECLARE_COLOR_FUNCS(bgr, 2, 1, 0)
 
+static av_always_inline void color_decorrelation_gbrp(float **dst, int dst_linesize,
+                                                      const uint8_t **src, int src_linesize,
+                                                      int w, int h)
+{
+    int x, y;
+    float *dstp_r = dst[0];
+    float *dstp_g = dst[1];
+    float *dstp_b = dst[2];
+    const uint8_t *srcp_r = src[2];
+    const uint8_t *srcp_g = src[0];
+    const uint8_t *srcp_b = src[1];
+
+    for (y = 0; y < h; y++) {
+        for (x = 0; x < w; x++) {
+            dstp_r[x] = srcp_r[x] * DCT3X3_0_0 + srcp_g[x] * DCT3X3_0_1 + srcp_b[x] * DCT3X3_0_2;
+            dstp_g[x] = srcp_r[x] * DCT3X3_1_0 +                          srcp_b[x] * DCT3X3_1_2;
+            dstp_b[x] = srcp_r[x] * DCT3X3_2_0 + srcp_g[x] * DCT3X3_2_1 + srcp_b[x] * DCT3X3_2_2;
+        }
+        srcp_r += src_linesize;
+        srcp_g += src_linesize;
+        srcp_b += src_linesize;
+        dstp_r += dst_linesize;
+        dstp_g += dst_linesize;
+        dstp_b += dst_linesize;
+    }
+}
+
+static av_always_inline void color_correlation_gbrp(uint8_t **dst, int dst_linesize,
+                                                    float **src, int src_linesize,
+                                                    int w, int h)
+{
+    int x, y;
+    const float *src_r = src[0];
+    const float *src_g = src[1];
+    const float *src_b = src[2];
+    uint8_t *dstp_r = dst[2];
+    uint8_t *dstp_g = dst[0];
+    uint8_t *dstp_b = dst[1];
+
+    for (y = 0; y < h; y++) {
+        for (x = 0; x < w; x++) {
+            dstp_r[x] = av_clip_uint8(src_r[x] * DCT3X3_0_0 + src_g[x] * DCT3X3_1_0 + src_b[x] * DCT3X3_2_0);
+            dstp_g[x] = av_clip_uint8(src_r[x] * DCT3X3_0_1 +                         src_b[x] * DCT3X3_2_1);
+            dstp_b[x] = av_clip_uint8(src_r[x] * DCT3X3_0_2 + src_g[x] * DCT3X3_1_2 + src_b[x] * DCT3X3_2_2);
+        }
+        dstp_r += dst_linesize;
+        dstp_g += dst_linesize;
+        dstp_b += dst_linesize;
+        src_r += src_linesize;
+        src_g += src_linesize;
+        src_b += src_linesize;
+    }
+}
+
 static int config_input(AVFilterLink *inlink)
 {
     AVFilterContext *ctx = inlink->dst;
     DCTdnoizContext *s = ctx->priv;
-    int i, x, y, bx, by, linesize, *iweights;
+    int i, x, y, bx, by, linesize, *iweights, max_slice_h, slice_h;
     const int bsize = 1 << s->n;
 
     switch (inlink->format) {
@@ -487,6 +545,10 @@ static int config_input(AVFilterLink *inlink)
         s->color_decorrelation = color_decorrelation_rgb;
         s->color_correlation   = color_correlation_rgb;
         break;
+    case AV_PIX_FMT_GBRP:
+        s->color_decorrelation = color_decorrelation_gbrp;
+        s->color_correlation   = color_correlation_gbrp;
+        break;
     default:
         av_assert0(0);
     }
@@ -500,15 +562,41 @@ static int config_input(AVFilterLink *inlink)
         av_log(ctx, AV_LOG_WARNING, "The last %d vertical pixels won't be denoised\n",
                inlink->h - s->pr_height);
 
+    max_slice_h = s->pr_height / ((s->bsize - 1) * 2);
+    s->nb_threads = FFMIN3(MAX_THREADS, ff_filter_get_nb_threads(ctx), max_slice_h);
+    av_log(ctx, AV_LOG_DEBUG, "threads: [max=%d hmax=%d user=%d] => %d\n",
+           MAX_THREADS, max_slice_h, ff_filter_get_nb_threads(ctx), s->nb_threads);
+
     s->p_linesize = linesize = FFALIGN(s->pr_width, 32);
     for (i = 0; i < 2; i++) {
-        s->cbuf[i][0] = av_malloc(linesize * s->pr_height * sizeof(*s->cbuf[i][0]));
-        s->cbuf[i][1] = av_malloc(linesize * s->pr_height * sizeof(*s->cbuf[i][1]));
-        s->cbuf[i][2] = av_malloc(linesize * s->pr_height * sizeof(*s->cbuf[i][2]));
+        s->cbuf[i][0] = av_malloc_array(linesize * s->pr_height, sizeof(*s->cbuf[i][0]));
+        s->cbuf[i][1] = av_malloc_array(linesize * s->pr_height, sizeof(*s->cbuf[i][1]));
+        s->cbuf[i][2] = av_malloc_array(linesize * s->pr_height, sizeof(*s->cbuf[i][2]));
         if (!s->cbuf[i][0] || !s->cbuf[i][1] || !s->cbuf[i][2])
             return AVERROR(ENOMEM);
     }
 
+    /* eval expressions are probably not thread safe when the eval internal
+     * state can be changed (typically through load & store operations) */
+    if (s->expr_str) {
+        for (i = 0; i < s->nb_threads; i++) {
+            int ret = av_expr_parse(&s->expr[i], s->expr_str, var_names,
+                                    NULL, NULL, NULL, NULL, 0, ctx);
+            if (ret < 0)
+                return ret;
+        }
+    }
+
+    /* each slice will need to (pre & re)process the top and bottom block of
+     * the previous one in in addition to its processing area. This is because
+     * each pixel is averaged by all the surrounding blocks */
+    slice_h = (int)ceilf(s->pr_height / (float)s->nb_threads) + (s->bsize - 1) * 2;
+    for (i = 0; i < s->nb_threads; i++) {
+        s->slices[i] = av_malloc_array(linesize, slice_h * sizeof(*s->slices[i]));
+        if (!s->slices[i])
+            return AVERROR(ENOMEM);
+    }
+
     s->weights = av_malloc(s->pr_height * linesize * sizeof(*s->weights));
     if (!s->weights)
         return AVERROR(ENOMEM);
@@ -544,10 +632,6 @@ static av_cold int init(AVFilterContext *ctx)
     }
 
     if (s->expr_str) {
-        int ret = av_expr_parse(&s->expr, s->expr_str, var_names,
-                                NULL, NULL, NULL, NULL, 0, ctx);
-        if (ret < 0)
-            return ret;
         switch (s->n) {
         case 3: s->filter_freq_func = filter_freq_expr_8;  break;
         case 4: s->filter_freq_func = filter_freq_expr_16; break;
@@ -570,42 +654,65 @@ static int query_formats(AVFilterContext *ctx)
 {
     static const enum AVPixelFormat pix_fmts[] = {
         AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
+        AV_PIX_FMT_GBRP,
         AV_PIX_FMT_NONE
     };
-    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
-    return 0;
+    AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
+    if (!fmts_list)
+        return AVERROR(ENOMEM);
+    return ff_set_common_formats(ctx, fmts_list);
 }
 
-static void filter_plane(AVFilterContext *ctx,
-                         float *dst, int dst_linesize,
-                         const float *src, int src_linesize,
-                         int w, int h)
+typedef struct ThreadData {
+    float *src, *dst;
+} ThreadData;
+
+static int filter_slice(AVFilterContext *ctx,
+                        void *arg, int jobnr, int nb_jobs)
 {
     int x, y;
     DCTdnoizContext *s = ctx->priv;
-    float *dst0 = dst;
-    const float *weights = s->weights;
+    const ThreadData *td = arg;
+    const int w = s->pr_width;
+    const int h = s->pr_height;
+    const int slice_start = (h *  jobnr   ) / nb_jobs;
+    const int slice_end   = (h * (jobnr+1)) / nb_jobs;
+    const int slice_start_ctx = FFMAX(slice_start - s->bsize + 1, 0);
+    const int slice_end_ctx   = FFMIN(slice_end, h - s->bsize + 1);
+    const int slice_h = slice_end_ctx - slice_start_ctx;
+    const int src_linesize   = s->p_linesize;
+    const int dst_linesize   = s->p_linesize;
+    const int slice_linesize = s->p_linesize;
+    float *dst;
+    const float *src = td->src + slice_start_ctx * src_linesize;
+    const float *weights = s->weights + slice_start * dst_linesize;
+    float *slice = s->slices[jobnr];
 
     // reset block sums
-    memset(dst, 0, h * dst_linesize * sizeof(*dst));
+    memset(slice, 0, (slice_h + s->bsize - 1) * dst_linesize * sizeof(*slice));
 
     // block dct sums
-    for (y = 0; y < h - s->bsize + 1; y += s->step) {
+    for (y = 0; y < slice_h; y += s->step) {
         for (x = 0; x < w - s->bsize + 1; x += s->step)
             s->filter_freq_func(s, src + x, src_linesize,
-                                   dst + x, dst_linesize);
+                                slice + x, slice_linesize,
+                                jobnr);
         src += s->step * src_linesize;
-        dst += s->step * dst_linesize;
+        slice += s->step * slice_linesize;
     }
 
     // average blocks
-    dst = dst0;
-    for (y = 0; y < h; y++) {
+    slice = s->slices[jobnr] + (slice_start - slice_start_ctx) * slice_linesize;
+    dst = td->dst + slice_start * dst_linesize;
+    for (y = slice_start; y < slice_end; y++) {
         for (x = 0; x < w; x++)
-            dst[x] *= weights[x];
+            dst[x] = slice[x] * weights[x];
+        slice += slice_linesize;
         dst += dst_linesize;
         weights += dst_linesize;
     }
+
+    return 0;
 }
 
 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
@@ -630,13 +737,16 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     }
 
     s->color_decorrelation(s->cbuf[0], s->p_linesize,
-                           in->data[0], in->linesize[0],
+                           (const uint8_t **)in->data, in->linesize[0],
                            s->pr_width, s->pr_height);
-    for (plane = 0; plane < 3; plane++)
-        filter_plane(ctx, s->cbuf[1][plane], s->p_linesize,
-                          s->cbuf[0][plane], s->p_linesize,
-                          s->pr_width, s->pr_height);
-    s->color_correlation(out->data[0], out->linesize[0],
+    for (plane = 0; plane < 3; plane++) {
+        ThreadData td = {
+            .src = s->cbuf[0][plane],
+            .dst = s->cbuf[1][plane],
+        };
+        ctx->internal->execute(ctx, filter_slice, &td, NULL, s->nb_threads);
+    }
+    s->color_correlation(out->data, out->linesize[0],
                          s->cbuf[1], s->p_linesize,
                          s->pr_width, s->pr_height);
 
@@ -681,13 +791,16 @@ static av_cold void uninit(AVFilterContext *ctx)
     int i;
     DCTdnoizContext *s = ctx->priv;
 
-    av_free(s->weights);
+    av_freep(&s->weights);
     for (i = 0; i < 2; i++) {
-        av_free(s->cbuf[i][0]);
-        av_free(s->cbuf[i][1]);
-        av_free(s->cbuf[i][2]);
+        av_freep(&s->cbuf[i][0]);
+        av_freep(&s->cbuf[i][1]);
+        av_freep(&s->cbuf[i][2]);
+    }
+    for (i = 0; i < s->nb_threads; i++) {
+        av_freep(&s->slices[i]);
+        av_expr_free(s->expr[i]);
     }
-    av_expr_free(s->expr);
 }
 
 static const AVFilterPad dctdnoiz_inputs[] = {
@@ -718,5 +831,5 @@ AVFilter ff_vf_dctdnoiz = {
     .inputs        = dctdnoiz_inputs,
     .outputs       = dctdnoiz_outputs,
     .priv_class    = &dctdnoiz_class,
-    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
 };