Imported Debian version 2.5.0~trusty1.1
[deb_ffmpeg.git] / ffmpeg / libavfilter / vf_noise.c
index 1028a3c41f2ca8027303a774be7a3d7b2b962f8c..4acad8a98b4fafeeda5baf4a1e02f21073bc2517 100644 (file)
 #include "libavutil/lfg.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/pixdesc.h"
-#include "libavutil/x86/asm.h"
 #include "avfilter.h"
 #include "formats.h"
 #include "internal.h"
+#include "vf_noise.h"
 #include "video.h"
 
-#define MAX_NOISE 5120
-#define MAX_SHIFT 1024
-#define MAX_RES (MAX_NOISE-MAX_SHIFT)
-
-#define NOISE_UNIFORM  1
-#define NOISE_TEMPORAL 2
-#define NOISE_AVERAGED 8
-#define NOISE_PATTERN  16
-
-typedef struct {
-    int strength;
-    unsigned flags;
-    AVLFG lfg;
-    int seed;
-    int8_t *noise;
-    int8_t *prev_shift[MAX_RES][3];
-} FilterParams;
-
-typedef struct {
-    const AVClass *class;
-    int nb_planes;
-    int bytewidth[4];
-    int height[4];
-    FilterParams all;
-    FilterParams param[4];
-    int rand_shift[MAX_RES];
-    int rand_shift_init;
-    void (*line_noise)(uint8_t *dst, const uint8_t *src, const int8_t *noise, int len, int shift);
-    void (*line_noise_avg)(uint8_t *dst, const uint8_t *src, int len, const int8_t * const *shift);
-} NoiseContext;
-
 typedef struct ThreadData {
     AVFrame *in, *out;
 } ThreadData;
@@ -110,7 +79,7 @@ static av_cold int init_noise(NoiseContext *n, int comp)
     if (!noise)
         return AVERROR(ENOMEM);
 
-    av_lfg_init(&fp->lfg, fp->seed);
+    av_lfg_init(&fp->lfg, fp->seed + comp*31415U);
 
     for (i = 0, j = 0; i < MAX_NOISE; i++, j++) {
         if (flags & NOISE_UNIFORM) {
@@ -157,12 +126,6 @@ static av_cold int init_noise(NoiseContext *n, int comp)
         for (j = 0; j < 3; j++)
             fp->prev_shift[i][j] = noise + (av_lfg_get(lfg) & (MAX_SHIFT - 1));
 
-    if (!n->rand_shift_init) {
-        for (i = 0; i < MAX_RES; i++)
-            n->rand_shift[i] = av_lfg_get(lfg) & (MAX_SHIFT - 1);
-        n->rand_shift_init = 1;
-    }
-
     fp->noise = noise;
     return 0;
 }
@@ -199,8 +162,8 @@ static int config_input(AVFilterLink *inlink)
     return 0;
 }
 
-static inline void line_noise_c(uint8_t *dst, const uint8_t *src, const int8_t *noise,
-                       int len, int shift)
+void ff_line_noise_c(uint8_t *dst, const uint8_t *src, const int8_t *noise,
+                     int len, int shift)
 {
     int i;
 
@@ -212,70 +175,8 @@ static inline void line_noise_c(uint8_t *dst, const uint8_t *src, const int8_t *
     }
 }
 
-#define ASMALIGN(ZEROBITS) ".p2align " #ZEROBITS "\n\t"
-
-static void line_noise_mmx(uint8_t *dst, const uint8_t *src,
-                           const int8_t *noise, int len, int shift)
-{
-#if HAVE_MMX_INLINE
-    x86_reg mmx_len= len&(~7);
-    noise+=shift;
-
-    __asm__ volatile(
-            "mov %3, %%"REG_a"               \n\t"
-            "pcmpeqb %%mm7, %%mm7            \n\t"
-            "psllw $15, %%mm7                \n\t"
-            "packsswb %%mm7, %%mm7           \n\t"
-            ASMALIGN(4)
-            "1:                              \n\t"
-            "movq (%0, %%"REG_a"), %%mm0     \n\t"
-            "movq (%1, %%"REG_a"), %%mm1     \n\t"
-            "pxor %%mm7, %%mm0               \n\t"
-            "paddsb %%mm1, %%mm0             \n\t"
-            "pxor %%mm7, %%mm0               \n\t"
-            "movq %%mm0, (%2, %%"REG_a")     \n\t"
-            "add $8, %%"REG_a"               \n\t"
-            " js 1b                          \n\t"
-            :: "r" (src+mmx_len), "r" (noise+mmx_len), "r" (dst+mmx_len), "g" (-mmx_len)
-            : "%"REG_a
-    );
-    if (mmx_len!=len)
-        line_noise_c(dst+mmx_len, src+mmx_len, noise+mmx_len, len-mmx_len, 0);
-#endif
-}
-
-static void line_noise_mmxext(uint8_t *dst, const uint8_t *src,
-                              const int8_t *noise, int len, int shift)
-{
-#if HAVE_MMXEXT_INLINE
-    x86_reg mmx_len= len&(~7);
-    noise+=shift;
-
-    __asm__ volatile(
-            "mov %3, %%"REG_a"                \n\t"
-            "pcmpeqb %%mm7, %%mm7             \n\t"
-            "psllw $15, %%mm7                 \n\t"
-            "packsswb %%mm7, %%mm7            \n\t"
-            ASMALIGN(4)
-            "1:                               \n\t"
-            "movq (%0, %%"REG_a"), %%mm0      \n\t"
-            "movq (%1, %%"REG_a"), %%mm1      \n\t"
-            "pxor %%mm7, %%mm0                \n\t"
-            "paddsb %%mm1, %%mm0              \n\t"
-            "pxor %%mm7, %%mm0                \n\t"
-            "movntq %%mm0, (%2, %%"REG_a")    \n\t"
-            "add $8, %%"REG_a"                \n\t"
-            " js 1b                           \n\t"
-            :: "r" (src+mmx_len), "r" (noise+mmx_len), "r" (dst+mmx_len), "g" (-mmx_len)
-            : "%"REG_a
-            );
-    if (mmx_len != len)
-        line_noise_c(dst+mmx_len, src+mmx_len, noise+mmx_len, len-mmx_len, 0);
-#endif
-}
-
-static inline void line_noise_avg_c(uint8_t *dst, const uint8_t *src,
-                           int len, const int8_t * const *shift)
+void ff_line_noise_avg_c(uint8_t *dst, const uint8_t *src,
+                         int len, const int8_t * const *shift)
 {
     int i;
     const int8_t *src2 = (const int8_t*)src;
@@ -286,50 +187,6 @@ static inline void line_noise_avg_c(uint8_t *dst, const uint8_t *src,
     }
 }
 
-static inline void line_noise_avg_mmx(uint8_t *dst, const uint8_t *src,
-                                      int len, const int8_t * const *shift)
-{
-#if HAVE_MMX_INLINE && HAVE_6REGS
-    x86_reg mmx_len= len&(~7);
-
-    __asm__ volatile(
-            "mov %5, %%"REG_a"              \n\t"
-            ASMALIGN(4)
-            "1:                             \n\t"
-            "movq (%1, %%"REG_a"), %%mm1    \n\t"
-            "movq (%0, %%"REG_a"), %%mm0    \n\t"
-            "paddb (%2, %%"REG_a"), %%mm1   \n\t"
-            "paddb (%3, %%"REG_a"), %%mm1   \n\t"
-            "movq %%mm0, %%mm2              \n\t"
-            "movq %%mm1, %%mm3              \n\t"
-            "punpcklbw %%mm0, %%mm0         \n\t"
-            "punpckhbw %%mm2, %%mm2         \n\t"
-            "punpcklbw %%mm1, %%mm1         \n\t"
-            "punpckhbw %%mm3, %%mm3         \n\t"
-            "pmulhw %%mm0, %%mm1            \n\t"
-            "pmulhw %%mm2, %%mm3            \n\t"
-            "paddw %%mm1, %%mm1             \n\t"
-            "paddw %%mm3, %%mm3             \n\t"
-            "paddw %%mm0, %%mm1             \n\t"
-            "paddw %%mm2, %%mm3             \n\t"
-            "psrlw $8, %%mm1                \n\t"
-            "psrlw $8, %%mm3                \n\t"
-            "packuswb %%mm3, %%mm1          \n\t"
-            "movq %%mm1, (%4, %%"REG_a")    \n\t"
-            "add $8, %%"REG_a"              \n\t"
-            " js 1b                         \n\t"
-            :: "r" (src+mmx_len), "r" (shift[0]+mmx_len), "r" (shift[1]+mmx_len), "r" (shift[2]+mmx_len),
-               "r" (dst+mmx_len), "g" (-mmx_len)
-            : "%"REG_a
-        );
-
-    if (mmx_len != len){
-        const int8_t *shift2[3]={shift[0]+mmx_len, shift[1]+mmx_len, shift[2]+mmx_len};
-        line_noise_avg_c(dst+mmx_len, src+mmx_len, len-mmx_len, shift2);
-    }
-#endif
-}
-
 static void noise(uint8_t *dst, const uint8_t *src,
                   int dst_linesize, int src_linesize,
                   int width, int start, int end, NoiseContext *n, int comp)
@@ -337,8 +194,7 @@ static void noise(uint8_t *dst, const uint8_t *src,
     FilterParams *p = &n->param[comp];
     int8_t *noise = p->noise;
     const int flags = p->flags;
-    AVLFG *lfg = &p->lfg;
-    int shift, y;
+    int y;
 
     if (!noise) {
         if (dst != src)
@@ -348,16 +204,17 @@ static void noise(uint8_t *dst, const uint8_t *src,
 
     for (y = start; y < end; y++) {
         const int ix = y & (MAX_RES - 1);
-        if (flags & NOISE_TEMPORAL)
-            shift = av_lfg_get(lfg) & (MAX_SHIFT - 1);
-        else
-            shift = n->rand_shift[ix];
+        int x;
+        for (x=0; x < width; x+= MAX_RES) {
+            int w = FFMIN(width - x, MAX_RES);
+            int shift = p->rand_shift[ix];
 
-        if (flags & NOISE_AVERAGED) {
-            n->line_noise_avg(dst, src, width, (const int8_t**)p->prev_shift[ix]);
-            p->prev_shift[ix][shift & 3] = noise + shift;
-        } else {
-            n->line_noise(dst, src, noise, width, shift);
+            if (flags & NOISE_AVERAGED) {
+                n->line_noise_avg(dst + x, src + x, w, (const int8_t**)p->prev_shift[ix]);
+                p->prev_shift[ix][shift & 3] = noise + shift;
+            } else {
+                n->line_noise(dst + x, src + x, noise, w, shift);
+            }
         }
         dst += dst_linesize;
         src += src_linesize;
@@ -389,6 +246,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
     NoiseContext *n = ctx->priv;
     ThreadData td;
     AVFrame *out;
+    int comp, i;
 
     if (av_frame_is_writable(inpicref)) {
         out = inpicref;
@@ -401,6 +259,18 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
         av_frame_copy_props(out, inpicref);
     }
 
+    for (comp = 0; comp < 4; comp++) {
+        FilterParams *fp = &n->param[comp];
+
+        if ((!fp->rand_shift_init || (fp->flags & NOISE_TEMPORAL)) && fp->strength) {
+
+            for (i = 0; i < MAX_RES; i++) {
+                fp->rand_shift[i] = av_lfg_get(&fp->lfg) & (MAX_SHIFT - 1);
+            }
+            fp->rand_shift_init = 1;
+        }
+    }
+
     td.in = inpicref; td.out = out;
     ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(n->height[0], ctx->graph->nb_threads));
     emms_c();
@@ -414,7 +284,6 @@ static av_cold int init(AVFilterContext *ctx)
 {
     NoiseContext *n = ctx->priv;
     int ret, i;
-    int cpu_flags = av_get_cpu_flags();
 
     for (i = 0; i < 4; i++) {
         if (n->all.seed >= 0)
@@ -432,19 +301,11 @@ static av_cold int init(AVFilterContext *ctx)
             return ret;
     }
 
-    n->line_noise     = line_noise_c;
-    n->line_noise_avg = line_noise_avg_c;
+    n->line_noise     = ff_line_noise_c;
+    n->line_noise_avg = ff_line_noise_avg_c;
 
-    if (HAVE_MMX_INLINE &&
-        cpu_flags & AV_CPU_FLAG_MMX) {
-        n->line_noise = line_noise_mmx;
-#if HAVE_6REGS
-        n->line_noise_avg = line_noise_avg_mmx;
-#endif
-    }
-    if (HAVE_MMXEXT_INLINE &&
-        cpu_flags & AV_CPU_FLAG_MMXEXT)
-        n->line_noise = line_noise_mmxext;
+    if (ARCH_X86)
+        ff_noise_init_x86(n);
 
     return 0;
 }