avfilter/vf_atadenoise: rewrite using macro
authorPaul B Mahol <onemda@gmail.com>
Mon, 14 Oct 2019 14:39:56 +0000 (16:39 +0200)
committerPaul B Mahol <onemda@gmail.com>
Mon, 14 Oct 2019 14:39:56 +0000 (16:39 +0200)
libavfilter/vf_atadenoise.c

index b7d958b..be7c4e2 100644 (file)
@@ -57,6 +57,10 @@ typedef struct ATADenoiseContext {
     int available;
 
     int (*filter_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
+    void (*filter_row)(const uint8_t *src, uint8_t *dst,
+                       const uint8_t *srcf[SIZE],
+                       int w, int mid, int size,
+                       int thra, int thrb);
 } ATADenoiseContext;
 
 #define OFFSET(x) offsetof(ATADenoiseContext, x)
@@ -125,7 +129,54 @@ typedef struct ThreadData {
     AVFrame *in, *out;
 } ThreadData;
 
-static int filter_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+#define FILTER_ROW(type, name)                                              \
+static void filter_row##name(const uint8_t *ssrc, uint8_t *ddst,            \
+                             const uint8_t *ssrcf[SIZE],                    \
+                             int w, int mid, int size,                      \
+                             int thra, int thrb)                            \
+{                                                                           \
+    const type *src = (const type *)ssrc;                                   \
+    const type **srcf = (const type **)ssrcf;                               \
+    type *dst = (type *)ddst;                                               \
+                                                                            \
+    for (int x = 0; x < w; x++) {                                           \
+       const int srcx = src[x];                                             \
+       unsigned lsumdiff = 0, rsumdiff = 0;                                 \
+       unsigned ldiff, rdiff;                                               \
+       unsigned sum = srcx;                                                 \
+       int l = 0, r = 0;                                                    \
+       int srcjx, srcix;                                                    \
+                                                                            \
+       for (int j = mid - 1, i = mid + 1; j >= 0 && i < size; j--, i++) {   \
+           srcjx = srcf[j][x];                                              \
+                                                                            \
+           ldiff = FFABS(srcx - srcjx);                                     \
+           lsumdiff += ldiff;                                               \
+           if (ldiff > thra ||                                              \
+               lsumdiff > thrb)                                             \
+               break;                                                       \
+           l++;                                                             \
+           sum += srcjx;                                                    \
+                                                                            \
+           srcix = srcf[i][x];                                              \
+                                                                            \
+           rdiff = FFABS(srcx - srcix);                                     \
+           rsumdiff += rdiff;                                               \
+           if (rdiff > thra ||                                              \
+               rsumdiff > thrb)                                             \
+               break;                                                       \
+           r++;                                                             \
+           sum += srcix;                                                    \
+       }                                                                    \
+                                                                            \
+       dst[x] = sum / (r + l + 1);                                          \
+   }                                                                        \
+}
+
+FILTER_ROW(uint8_t, 8)
+FILTER_ROW(uint16_t, 16)
+
+static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
     ATADenoiseContext *s = ctx->priv;
     ThreadData *td = arg;
@@ -133,7 +184,7 @@ static int filter_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs
     AVFrame *out = td->out;
     const int size = s->size;
     const int mid = s->mid;
-    int p, x, y, i, j;
+    int p, y, i;
 
     for (p = 0; p < s->nb_planes; p++) {
         const int h = s->planeheight[p];
@@ -158,38 +209,7 @@ static int filter_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs
             srcf[i] = data[i] + slice_start * linesize[i];
 
         for (y = slice_start; y < slice_end; y++) {
-            for (x = 0; x < w; x++) {
-                const int srcx = src[x];
-                unsigned lsumdiff = 0, rsumdiff = 0;
-                unsigned ldiff, rdiff;
-                unsigned sum = srcx;
-                int l = 0, r = 0;
-                int srcjx, srcix;
-
-                for (j = mid - 1, i = mid + 1; j >= 0 && i < size; j--, i++) {
-                    srcjx = srcf[j][x];
-
-                    ldiff = FFABS(srcx - srcjx);
-                    lsumdiff += ldiff;
-                    if (ldiff > thra ||
-                        lsumdiff > thrb)
-                        break;
-                    l++;
-                    sum += srcjx;
-
-                    srcix = srcf[i][x];
-
-                    rdiff = FFABS(srcx - srcix);
-                    rsumdiff += rdiff;
-                    if (rdiff > thra ||
-                        rsumdiff > thrb)
-                        break;
-                    r++;
-                    sum += srcix;
-                }
-
-                dst[x] = sum / (r + l + 1);
-            }
+            s->filter_row(src, dst, srcf, w, mid, size, thra, thrb);
 
             dst += out->linesize[p];
             src += in->linesize[p];
@@ -202,83 +222,6 @@ static int filter_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs
     return 0;
 }
 
-static int filter_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
-{
-    ATADenoiseContext *s = ctx->priv;
-    ThreadData *td = arg;
-    AVFrame *in = td->in;
-    AVFrame *out = td->out;
-    const int size = s->size;
-    const int mid = s->mid;
-    int p, x, y, i, j;
-
-    for (p = 0; p < s->nb_planes; p++) {
-        const int h = s->planeheight[p];
-        const int w = s->planewidth[p];
-        const int slice_start = (h * jobnr) / nb_jobs;
-        const int slice_end = (h * (jobnr+1)) / nb_jobs;
-        const uint16_t *src = (uint16_t *)(in->data[p] + slice_start * in->linesize[p]);
-        uint16_t *dst = (uint16_t *)(out->data[p] + slice_start * out->linesize[p]);
-        const int thra = s->thra[p];
-        const int thrb = s->thrb[p];
-        const uint8_t **data = (const uint8_t **)s->data[p];
-        const int *linesize = (const int *)s->linesize[p];
-        const uint16_t *srcf[SIZE];
-
-        if (!((1 << p) & s->planes)) {
-            av_image_copy_plane((uint8_t *)dst, out->linesize[p], (uint8_t *)src, in->linesize[p],
-                                w * 2, slice_end - slice_start);
-            continue;
-        }
-
-        for (i = 0; i < s->size; i++)
-            srcf[i] = (const uint16_t *)(data[i] + slice_start * linesize[i]);
-
-        for (y = slice_start; y < slice_end; y++) {
-            for (x = 0; x < w; x++) {
-                const int srcx = src[x];
-                unsigned lsumdiff = 0, rsumdiff = 0;
-                unsigned ldiff, rdiff;
-                unsigned sum = srcx;
-                int l = 0, r = 0;
-                int srcjx, srcix;
-
-                for (j = mid - 1, i = mid + 1; j >= 0 && i < size; j--, i++) {
-                    srcjx = srcf[j][x];
-
-                    ldiff = FFABS(srcx - srcjx);
-                    lsumdiff += ldiff;
-                    if (ldiff > thra ||
-                        lsumdiff > thrb)
-                        break;
-                    l++;
-                    sum += srcjx;
-
-                    srcix = srcf[i][x];
-
-                    rdiff = FFABS(srcx - srcix);
-                    rsumdiff += rdiff;
-                    if (rdiff > thra ||
-                        rsumdiff > thrb)
-                        break;
-                    r++;
-                    sum += srcix;
-                }
-
-                dst[x] = sum / (r + l + 1);
-            }
-
-            dst += out->linesize[p] / 2;
-            src += in->linesize[p] / 2;
-
-            for (i = 0; i < size; i++)
-                srcf[i] += linesize[i] / 2;
-        }
-    }
-
-    return 0;
-}
-
 static int config_input(AVFilterLink *inlink)
 {
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
@@ -294,10 +237,11 @@ static int config_input(AVFilterLink *inlink)
     s->planewidth[0]  = s->planewidth[3]  = inlink->w;
 
     depth = desc->comp[0].depth;
+    s->filter_slice = filter_slice;
     if (depth == 8)
-        s->filter_slice = filter_slice8;
+        s->filter_row = filter_row8;
     else
-        s->filter_slice = filter_slice16;
+        s->filter_row = filter_row16;
 
     s->thra[0] = s->fthra[0] * (1 << depth) - 1;
     s->thra[1] = s->fthra[1] * (1 << depth) - 1;