]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vf_w3fdif.c
avfilter/vf_w3fdif: add x86 SIMD
[ffmpeg] / libavfilter / vf_w3fdif.c
index bbd8db00d31ee6f0c26b26026f5ae25abdf609b7..a07efe4c391f7e33650ce822286f510fcb04bc7f 100644 (file)
@@ -29,6 +29,7 @@
 #include "formats.h"
 #include "internal.h"
 #include "video.h"
+#include "w3fdif.h"
 
 typedef struct W3FDIFContext {
     const AVClass *class;
@@ -42,6 +43,8 @@ typedef struct W3FDIFContext {
     AVFrame *prev, *cur, *next;  ///< previous, current, next frames
     int32_t **work_line;  ///< lines we are calculating
     int nb_threads;
+
+    W3FDIFDSPContext dsp;
 } W3FDIFContext;
 
 #define OFFSET(x) offsetof(W3FDIFContext, x)
@@ -81,6 +84,78 @@ static int query_formats(AVFilterContext *ctx)
     return ff_set_common_formats(ctx, fmts_list);
 }
 
+static void filter_simple_low(int32_t *work_line,
+                              uint8_t *in_lines_cur[2],
+                              const int16_t *coef, int linesize)
+{
+    int i;
+
+    for (i = 0; i < linesize; i++) {
+        *work_line    = *in_lines_cur[0]++ * coef[0];
+        *work_line++ += *in_lines_cur[1]++ * coef[1];
+    }
+}
+
+static void filter_complex_low(int32_t *work_line,
+                               uint8_t *in_lines_cur[4],
+                               const int16_t *coef, int linesize)
+{
+    int i;
+
+    for (i = 0; i < linesize; i++) {
+        *work_line    = *in_lines_cur[0]++ * coef[0];
+        *work_line   += *in_lines_cur[1]++ * coef[1];
+        *work_line   += *in_lines_cur[2]++ * coef[2];
+        *work_line++ += *in_lines_cur[3]++ * coef[3];
+    }
+}
+
+static void filter_simple_high(int32_t *work_line,
+                               uint8_t *in_lines_cur[3],
+                               uint8_t *in_lines_adj[3],
+                               const int16_t *coef, int linesize)
+{
+    int i;
+
+    for (i = 0; i < linesize; i++) {
+        *work_line   += *in_lines_cur[0]++ * coef[0];
+        *work_line   += *in_lines_adj[0]++ * coef[0];
+        *work_line   += *in_lines_cur[1]++ * coef[1];
+        *work_line   += *in_lines_adj[1]++ * coef[1];
+        *work_line   += *in_lines_cur[2]++ * coef[2];
+        *work_line++ += *in_lines_adj[2]++ * coef[2];
+    }
+}
+
+static void filter_complex_high(int32_t *work_line,
+                                uint8_t *in_lines_cur[5],
+                                uint8_t *in_lines_adj[5],
+                                const int16_t *coef, int linesize)
+{
+    int i;
+
+    for (i = 0; i < linesize; i++) {
+        *work_line   += *in_lines_cur[0]++ * coef[0];
+        *work_line   += *in_lines_adj[0]++ * coef[0];
+        *work_line   += *in_lines_cur[1]++ * coef[1];
+        *work_line   += *in_lines_adj[1]++ * coef[1];
+        *work_line   += *in_lines_cur[2]++ * coef[2];
+        *work_line   += *in_lines_adj[2]++ * coef[2];
+        *work_line   += *in_lines_cur[3]++ * coef[3];
+        *work_line   += *in_lines_adj[3]++ * coef[3];
+        *work_line   += *in_lines_cur[4]++ * coef[4];
+        *work_line++ += *in_lines_adj[4]++ * coef[4];
+    }
+}
+
+static void filter_scale(uint8_t *out_pixel, const int32_t *work_pixel, int linesize)
+{
+    int j;
+
+    for (j = 0; j < linesize; j++, out_pixel++, work_pixel++)
+        *out_pixel = av_clip(*work_pixel, 0, 255 * 256 * 128) >> 15;
+}
+
 static int config_input(AVFilterLink *inlink)
 {
     AVFilterContext *ctx = inlink->dst;
@@ -101,11 +176,20 @@ static int config_input(AVFilterLink *inlink)
         return AVERROR(ENOMEM);
 
     for (i = 0; i < s->nb_threads; i++) {
-        s->work_line[i] = av_calloc(s->linesize[0], sizeof(*s->work_line[0]));
+        s->work_line[i] = av_calloc(FFALIGN(s->linesize[0], 32), sizeof(*s->work_line[0]));
         if (!s->work_line[i])
             return AVERROR(ENOMEM);
     }
 
+    s->dsp.filter_simple_low   = filter_simple_low;
+    s->dsp.filter_complex_low  = filter_complex_low;
+    s->dsp.filter_simple_high  = filter_simple_high;
+    s->dsp.filter_complex_high = filter_complex_high;
+    s->dsp.filter_scale        = filter_scale;
+
+    if (ARCH_X86)
+        ff_w3fdif_init_x86(&s->dsp);
+
     return 0;
 }
 
@@ -117,13 +201,12 @@ static int config_output(AVFilterLink *outlink)
     outlink->time_base.den = inlink->time_base.den * 2;
     outlink->frame_rate.num = inlink->frame_rate.num * 2;
     outlink->frame_rate.den = inlink->frame_rate.den;
-    outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
 
     return 0;
 }
 
 /*
- * Filter coefficients from PH-2071, scaled by 256 * 256.
+ * Filter coefficients from PH-2071, scaled by 256 * 128.
  * Each set of coefficients has a set for low-frequencies and high-frequencies.
  * n_coef_lf[] and n_coef_hf[] are the number of coefs for simple and more-complex.
  * It is important for later that n_coef_lf[] is even and n_coef_hf[] is odd.
@@ -131,11 +214,11 @@ static int config_output(AVFilterLink *outlink)
  * and high-frequencies for simple and more-complex mode.
  */
 static const int8_t   n_coef_lf[2] = { 2, 4 };
-static const int32_t coef_lf[2][4] = {{ 32768, 32768,     0,     0},
-                                      { -1704, 34472, 34472, -1704}};
+static const int16_t coef_lf[2][4] = {{ 16384, 16384,     0,    0},
+                                      {  -852, 17236, 17236, -852}};
 static const int8_t   n_coef_hf[2] = { 3, 5 };
-static const int32_t coef_hf[2][5] = {{ -4096,  8192, -4096,     0,     0},
-                                      {  2032, -7602, 11140, -7602,  2032}};
+static const int16_t coef_hf[2][5] = {{ -2048,  4096, -2048,     0,    0},
+                                      {  1016, -3801,  5570, -3801, 1016}};
 
 typedef struct ThreadData {
     AVFrame *out, *cur, *adj;
@@ -164,7 +247,7 @@ static int deinterlace_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_
     const int dst_line_stride = out->linesize[plane];
     const int start = (height * jobnr) / nb_jobs;
     const int end = (height * (jobnr+1)) / nb_jobs;
-    int i, j, y_in, y_out;
+    int j, y_in, y_out;
 
     /* copy unchanged the lines of the field */
     y_out = start + (s->field == cur->top_field_first) - (start & 1);
@@ -185,9 +268,6 @@ static int deinterlace_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_
     out_line = dst_data + (y_out * dst_line_stride);
 
     while (y_out < end) {
-        /* clear workspace */
-        memset(s->work_line[jobnr], 0, sizeof(*s->work_line[jobnr]) * linesize);
-
         /* get low vertical frequencies from current field */
         for (j = 0; j < n_coef_lf[filter]; j++) {
             y_in = (y_out + 1) + (j * 2) - n_coef_lf[filter];
@@ -203,18 +283,12 @@ static int deinterlace_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_
         work_line = s->work_line[jobnr];
         switch (n_coef_lf[filter]) {
         case 2:
-            for (i = 0; i < linesize; i++) {
-                *work_line   += *in_lines_cur[0]++ * coef_lf[filter][0];
-                *work_line++ += *in_lines_cur[1]++ * coef_lf[filter][1];
-            }
+            s->dsp.filter_simple_low(work_line, in_lines_cur,
+                                     coef_lf[filter], linesize);
             break;
         case 4:
-            for (i = 0; i < linesize; i++) {
-                *work_line   += *in_lines_cur[0]++ * coef_lf[filter][0];
-                *work_line   += *in_lines_cur[1]++ * coef_lf[filter][1];
-                *work_line   += *in_lines_cur[2]++ * coef_lf[filter][2];
-                *work_line++ += *in_lines_cur[3]++ * coef_lf[filter][3];
-            }
+            s->dsp.filter_complex_low(work_line, in_lines_cur,
+                                      coef_lf[filter], linesize);
         }
 
         /* get high vertical frequencies from adjacent fields */
@@ -233,36 +307,19 @@ static int deinterlace_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_
         work_line = s->work_line[jobnr];
         switch (n_coef_hf[filter]) {
         case 3:
-            for (i = 0; i < linesize; i++) {
-                *work_line   += *in_lines_cur[0]++ * coef_hf[filter][0];
-                *work_line   += *in_lines_adj[0]++ * coef_hf[filter][0];
-                *work_line   += *in_lines_cur[1]++ * coef_hf[filter][1];
-                *work_line   += *in_lines_adj[1]++ * coef_hf[filter][1];
-                *work_line   += *in_lines_cur[2]++ * coef_hf[filter][2];
-                *work_line++ += *in_lines_adj[2]++ * coef_hf[filter][2];
-            }
+            s->dsp.filter_simple_high(work_line, in_lines_cur, in_lines_adj,
+                                      coef_hf[filter], linesize);
             break;
         case 5:
-            for (i = 0; i < linesize; i++) {
-                *work_line   += *in_lines_cur[0]++ * coef_hf[filter][0];
-                *work_line   += *in_lines_adj[0]++ * coef_hf[filter][0];
-                *work_line   += *in_lines_cur[1]++ * coef_hf[filter][1];
-                *work_line   += *in_lines_adj[1]++ * coef_hf[filter][1];
-                *work_line   += *in_lines_cur[2]++ * coef_hf[filter][2];
-                *work_line   += *in_lines_adj[2]++ * coef_hf[filter][2];
-                *work_line   += *in_lines_cur[3]++ * coef_hf[filter][3];
-                *work_line   += *in_lines_adj[3]++ * coef_hf[filter][3];
-                *work_line   += *in_lines_cur[4]++ * coef_hf[filter][4];
-                *work_line++ += *in_lines_adj[4]++ * coef_hf[filter][4];
-            }
+            s->dsp.filter_complex_high(work_line, in_lines_cur, in_lines_adj,
+                                       coef_hf[filter], linesize);
         }
 
-        /* save scaled result to the output frame, scaling down by 256 * 256 */
+        /* save scaled result to the output frame, scaling down by 256 * 128 */
         work_pixel = s->work_line[jobnr];
         out_pixel = out_line;
 
-        for (j = 0; j < linesize; j++, out_pixel++, work_pixel++)
-             *out_pixel = av_clip(*work_pixel, 0, 255 * 256 * 256) >> 16;
+        s->dsp.filter_scale(out_pixel, work_pixel, linesize);
 
         /* move on to next line */
         y_out += 2;
@@ -354,26 +411,23 @@ static int request_frame(AVFilterLink *outlink)
 {
     AVFilterContext *ctx = outlink->src;
     W3FDIFContext *s = ctx->priv;
+    int ret;
 
-    do {
-        int ret;
-
-        if (s->eof)
-            return AVERROR_EOF;
+    if (s->eof)
+        return AVERROR_EOF;
 
-        ret = ff_request_frame(ctx->inputs[0]);
+    ret = ff_request_frame(ctx->inputs[0]);
 
-        if (ret == AVERROR_EOF && s->cur) {
-            AVFrame *next = av_frame_clone(s->next);
-            if (!next)
-                return AVERROR(ENOMEM);
-            next->pts = s->next->pts * 2 - s->cur->pts;
-            filter_frame(ctx->inputs[0], next);
-            s->eof = 1;
-        } else if (ret < 0) {
-            return ret;
-        }
-    } while (!s->cur);
+    if (ret == AVERROR_EOF && s->cur) {
+        AVFrame *next = av_frame_clone(s->next);
+        if (!next)
+            return AVERROR(ENOMEM);
+        next->pts = s->next->pts * 2 - s->cur->pts;
+        filter_frame(ctx->inputs[0], next);
+        s->eof = 1;
+    } else if (ret < 0) {
+        return ret;
+    }
 
     return 0;
 }