avfilter/trim: support all channel numbers in atrim filter
[ffmpeg.git] / libpostproc / postprocess_altivec_template.c
index c7ef78e..fa6ebe2 100644 (file)
@@ -20,7 +20,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "avutil.h"
+#include "libavutil/avutil.h"
 
 #define ALTIVEC_TRANSPOSE_8x8_SHORT(src_a,src_b,src_c,src_d,src_e,src_f,src_g,src_h) \
     do {                                                          \
@@ -62,10 +62,11 @@ static inline int vertClassify_altivec(uint8_t src[], int stride, PPContext *c)
     vector by assuming (stride % 16) == 0, unfortunately
     this is not always true.
     */
-    DECLARE_ALIGNED(16, short, data[8]) =
+    short data_0 = ((c->nonBQP*c->ppMode.baseDcDiff)>>8) + 1;
+    DECLARE_ALIGNED(16, short, data)[8] =
                     {
-                        ((c->nonBQP*c->ppMode.baseDcDiff)>>8) + 1,
-                        data[0] * 2 + 1,
+                        data_0,
+                        data_0 * 2 + 1,
                         c->QP * 2,
                         c->QP * 4
                     };
@@ -84,6 +85,17 @@ static inline int vertClassify_altivec(uint8_t src[], int stride, PPContext *c)
     vector signed short v_data = vec_ld(0, data);
     vector signed short v_srcAss0, v_srcAss1, v_srcAss2, v_srcAss3,
                         v_srcAss4, v_srcAss5, v_srcAss6, v_srcAss7;
+//FIXME avoid this mess if possible
+    register int j0 = 0,
+                 j1 = stride,
+                 j2 = 2 * stride,
+                 j3 = 3 * stride,
+                 j4 = 4 * stride,
+                 j5 = 5 * stride,
+                 j6 = 6 * stride,
+                 j7 = 7 * stride;
+    vector unsigned char v_srcA0, v_srcA1, v_srcA2, v_srcA3,
+                         v_srcA4, v_srcA5, v_srcA6, v_srcA7;
 
     v_dcOffset = vec_splat(v_data, 0);
     v_dcThreshold = (vector unsigned short)vec_splat(v_data, 1);
@@ -92,23 +104,21 @@ static inline int vertClassify_altivec(uint8_t src[], int stride, PPContext *c)
 
     src2 += stride * 4;
 
-
 #define LOAD_LINE(i)                                                    \
-    register int j##i = i * stride;                                     \
+    {                                                                   \
     vector unsigned char perm##i = vec_lvsl(j##i, src2);                \
-    const vector unsigned char v_srcA1##i = vec_ld(j##i, src2);         \
     vector unsigned char v_srcA2##i;                                    \
+    vector unsigned char v_srcA1##i = vec_ld(j##i, src2);               \
     if (two_vectors)                                                    \
         v_srcA2##i = vec_ld(j##i + 16, src2);                           \
-    const vector unsigned char v_srcA##i =                              \
+    v_srcA##i =                                                         \
         vec_perm(v_srcA1##i, v_srcA2##i, perm##i);                      \
     v_srcAss##i =                                                       \
         (vector signed short)vec_mergeh((vector signed char)zero,       \
-                                        (vector signed char)v_srcA##i)
+                                        (vector signed char)v_srcA##i); }
 
 #define LOAD_LINE_ALIGNED(i)                                            \
-    register int j##i = i * stride;                                     \
-    const vector unsigned char v_srcA##i = vec_ld(j##i, src2);          \
+    v_srcA##i = vec_ld(j##i, src2);                                     \
     v_srcAss##i =                                                       \
         (vector signed short)vec_mergeh((vector signed char)zero,       \
                                         (vector signed char)v_srcA##i)
@@ -146,16 +156,26 @@ static inline int vertClassify_altivec(uint8_t src[], int stride, PPContext *c)
     const vector signed short v_comp##i =                               \
         (vector signed short)vec_cmplt((vector unsigned short)v_sum##i, \
                                        v_dcThreshold);                  \
-    const vector signed short v_part##i = vec_and(mask, v_comp##i);     \
-    v_numEq = vec_sum4s(v_part##i, v_numEq);
-
-    ITER(0, 1);
-    ITER(1, 2);
-    ITER(2, 3);
-    ITER(3, 4);
-    ITER(4, 5);
-    ITER(5, 6);
-    ITER(6, 7);
+    const vector signed short v_part##i = vec_and(mask, v_comp##i);
+
+    {
+        ITER(0, 1)
+        ITER(1, 2)
+        ITER(2, 3)
+        ITER(3, 4)
+        ITER(4, 5)
+        ITER(5, 6)
+        ITER(6, 7)
+
+        v_numEq = vec_sum4s(v_part0, v_numEq);
+        v_numEq = vec_sum4s(v_part1, v_numEq);
+        v_numEq = vec_sum4s(v_part2, v_numEq);
+        v_numEq = vec_sum4s(v_part3, v_numEq);
+        v_numEq = vec_sum4s(v_part4, v_numEq);
+        v_numEq = vec_sum4s(v_part5, v_numEq);
+        v_numEq = vec_sum4s(v_part6, v_numEq);
+    }
+
 #undef ITER
 
     v_numEq = vec_sums(v_numEq, zero);
@@ -165,11 +185,11 @@ static inline int vertClassify_altivec(uint8_t src[], int stride, PPContext *c)
 
     if (numEq > c->ppMode.flatnessThreshold){
         const vector unsigned char mmoP1 = (const vector unsigned char)
-            AVV(0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
-                0x00, 0x01, 0x12, 0x13, 0x08, 0x09, 0x1A, 0x1B);
+            {0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
+             0x00, 0x01, 0x12, 0x13, 0x08, 0x09, 0x1A, 0x1B};
         const vector unsigned char mmoP2 = (const vector unsigned char)
-            AVV(0x04, 0x05, 0x16, 0x17, 0x0C, 0x0D, 0x1E, 0x1F,
-                0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f);
+            {0x04, 0x05, 0x16, 0x17, 0x0C, 0x0D, 0x1E, 0x1F,
+             0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f};
         const vector unsigned char mmoP = (const vector unsigned char)
             vec_lvsl(8, (unsigned char*)0);
 
@@ -203,21 +223,31 @@ static inline void doVertLowPass_altivec(uint8_t *src, int stride, PPContext *c)
     const vector signed int zero = vec_splat_s32(0);
     const int properStride = (stride % 16);
     const int srcAlign = ((unsigned long)src2 % 16);
-    DECLARE_ALIGNED(16, short, qp[8]);
-    qp[0] = c->QP;
+    DECLARE_ALIGNED(16, short, qp)[8] = {c->QP};
     vector signed short vqp = vec_ld(0, qp);
+    vector signed short vb0, vb1, vb2, vb3, vb4, vb5, vb6, vb7, vb8, vb9;
+    vector unsigned char vbA0, av_uninit(vbA1), av_uninit(vbA2), av_uninit(vbA3), av_uninit(vbA4), av_uninit(vbA5), av_uninit(vbA6), av_uninit(vbA7), av_uninit(vbA8), vbA9;
+    vector unsigned char vbB0, av_uninit(vbB1), av_uninit(vbB2), av_uninit(vbB3), av_uninit(vbB4), av_uninit(vbB5), av_uninit(vbB6), av_uninit(vbB7), av_uninit(vbB8), vbB9;
+    vector unsigned char vbT0, vbT1, vbT2, vbT3, vbT4, vbT5, vbT6, vbT7, vbT8, vbT9;
+    vector unsigned char perml0, perml1, perml2, perml3, perml4,
+                         perml5, perml6, perml7, perml8, perml9;
+    register int j0 = 0,
+                 j1 = stride,
+                 j2 = 2 * stride,
+                 j3 = 3 * stride,
+                 j4 = 4 * stride,
+                 j5 = 5 * stride,
+                 j6 = 6 * stride,
+                 j7 = 7 * stride,
+                 j8 = 8 * stride,
+                 j9 = 9 * stride;
+
     vqp = vec_splat(vqp, 0);
 
     src2 += stride*3;
 
-    vector signed short vb0, vb1, vb2, vb3, vb4, vb5, vb6, vb7, vb8, vb9;
-    vector unsigned char vbA0, vbA1, vbA2, vbA3, vbA4, vbA5, vbA6, vbA7, vbA8, vbA9;
-    vector unsigned char vbB0, vbB1, vbB2, vbB3, vbB4, vbB5, vbB6, vbB7, vbB8, vbB9;
-    vector unsigned char vbT0, vbT1, vbT2, vbT3, vbT4, vbT5, vbT6, vbT7, vbT8, vbT9;
-
 #define LOAD_LINE(i)                                                    \
-    const vector unsigned char perml##i =                               \
-        vec_lvsl(i * stride, src2);                                     \
+    perml##i = vec_lvsl(i * stride, src2);                              \
     vbA##i = vec_ld(i * stride, src2);                                  \
     vbB##i = vec_ld(i * stride + 16, src2);                             \
     vbT##i = vec_perm(vbA##i, vbB##i, perml##i);                        \
@@ -226,7 +256,6 @@ static inline void doVertLowPass_altivec(uint8_t *src, int stride, PPContext *c)
                                         (vector unsigned char)vbT##i)
 
 #define LOAD_LINE_ALIGNED(i)                                            \
-    register int j##i = i * stride;                                     \
     vbT##i = vec_ld(j##i, src2);                                        \
     vb##i =                                                             \
         (vector signed short)vec_mergeh((vector signed char)zero,       \
@@ -235,7 +264,7 @@ static inline void doVertLowPass_altivec(uint8_t *src, int stride, PPContext *c)
       /* Special-casing the aligned case is worthwhile, as all calls from
        * the (transposed) horizontable deblocks will be aligned, in addition
        * to the naturally aligned vertical deblocks. */
-      if (properStride && srcAlign) {
+    if (properStride && srcAlign) {
           LOAD_LINE_ALIGNED(0);
           LOAD_LINE_ALIGNED(1);
           LOAD_LINE_ALIGNED(2);
@@ -246,7 +275,7 @@ static inline void doVertLowPass_altivec(uint8_t *src, int stride, PPContext *c)
           LOAD_LINE_ALIGNED(7);
           LOAD_LINE_ALIGNED(8);
           LOAD_LINE_ALIGNED(9);
-      } else {
+    } else {
           LOAD_LINE(0);
           LOAD_LINE(1);
           LOAD_LINE(2);
@@ -257,76 +286,76 @@ static inline void doVertLowPass_altivec(uint8_t *src, int stride, PPContext *c)
           LOAD_LINE(7);
           LOAD_LINE(8);
           LOAD_LINE(9);
-      }
+    }
 #undef LOAD_LINE
 #undef LOAD_LINE_ALIGNED
-
-    const vector unsigned short v_2 = vec_splat_u16(2);
-    const vector unsigned short v_4 = vec_splat_u16(4);
-
-    const vector signed short v_diff01 = vec_sub(vb0, vb1);
-    const vector unsigned short v_cmp01 =
-        (const vector unsigned short) vec_cmplt(vec_abs(v_diff01), vqp);
-    const vector signed short v_first = vec_sel(vb1, vb0, v_cmp01);
-    const vector signed short v_diff89 = vec_sub(vb8, vb9);
-    const vector unsigned short v_cmp89 =
-        (const vector unsigned short) vec_cmplt(vec_abs(v_diff89), vqp);
-    const vector signed short v_last = vec_sel(vb8, vb9, v_cmp89);
-
-    const vector signed short temp01 = vec_mladd(v_first, (vector signed short)v_4, vb1);
-    const vector signed short temp02 = vec_add(vb2, vb3);
-    const vector signed short temp03 = vec_add(temp01, (vector signed short)v_4);
-    const vector signed short v_sumsB0 = vec_add(temp02, temp03);
-
-    const vector signed short temp11 = vec_sub(v_sumsB0, v_first);
-    const vector signed short v_sumsB1 = vec_add(temp11, vb4);
-
-    const vector signed short temp21 = vec_sub(v_sumsB1, v_first);
-    const vector signed short v_sumsB2 = vec_add(temp21, vb5);
-
-    const vector signed short temp31 = vec_sub(v_sumsB2, v_first);
-    const vector signed short v_sumsB3 = vec_add(temp31, vb6);
-
-    const vector signed short temp41 = vec_sub(v_sumsB3, v_first);
-    const vector signed short v_sumsB4 = vec_add(temp41, vb7);
-
-    const vector signed short temp51 = vec_sub(v_sumsB4, vb1);
-    const vector signed short v_sumsB5 = vec_add(temp51, vb8);
-
-    const vector signed short temp61 = vec_sub(v_sumsB5, vb2);
-    const vector signed short v_sumsB6 = vec_add(temp61, v_last);
-
-    const vector signed short temp71 = vec_sub(v_sumsB6, vb3);
-    const vector signed short v_sumsB7 = vec_add(temp71, v_last);
-
-    const vector signed short temp81 = vec_sub(v_sumsB7, vb4);
-    const vector signed short v_sumsB8 = vec_add(temp81, v_last);
-
-    const vector signed short temp91 = vec_sub(v_sumsB8, vb5);
-    const vector signed short v_sumsB9 = vec_add(temp91, v_last);
-
-#define COMPUTE_VR(i, j, k)                                             \
-    const vector signed short temps1##i =                               \
-        vec_add(v_sumsB##i, v_sumsB##k);                                \
-    const vector signed short temps2##i =                               \
-        vec_mladd(vb##j, (vector signed short)v_2, temps1##i);          \
-    const vector signed short  vr##j = vec_sra(temps2##i, v_4)
-
-    COMPUTE_VR(0, 1, 2);
-    COMPUTE_VR(1, 2, 3);
-    COMPUTE_VR(2, 3, 4);
-    COMPUTE_VR(3, 4, 5);
-    COMPUTE_VR(4, 5, 6);
-    COMPUTE_VR(5, 6, 7);
-    COMPUTE_VR(6, 7, 8);
-    COMPUTE_VR(7, 8, 9);
-
-    const vector signed char neg1 = vec_splat_s8(-1);
-    const vector unsigned char permHH = (const vector unsigned char)AVV(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
-                                                                        0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F);
+    {
+        const vector unsigned short v_2 = vec_splat_u16(2);
+        const vector unsigned short v_4 = vec_splat_u16(4);
+
+        const vector signed short v_diff01 = vec_sub(vb0, vb1);
+        const vector unsigned short v_cmp01 =
+            (const vector unsigned short) vec_cmplt(vec_abs(v_diff01), vqp);
+        const vector signed short v_first = vec_sel(vb1, vb0, v_cmp01);
+        const vector signed short v_diff89 = vec_sub(vb8, vb9);
+        const vector unsigned short v_cmp89 =
+            (const vector unsigned short) vec_cmplt(vec_abs(v_diff89), vqp);
+        const vector signed short v_last = vec_sel(vb8, vb9, v_cmp89);
+
+        const vector signed short temp01 = vec_mladd(v_first, (vector signed short)v_4, vb1);
+        const vector signed short temp02 = vec_add(vb2, vb3);
+        const vector signed short temp03 = vec_add(temp01, (vector signed short)v_4);
+        const vector signed short v_sumsB0 = vec_add(temp02, temp03);
+
+        const vector signed short temp11 = vec_sub(v_sumsB0, v_first);
+        const vector signed short v_sumsB1 = vec_add(temp11, vb4);
+
+        const vector signed short temp21 = vec_sub(v_sumsB1, v_first);
+        const vector signed short v_sumsB2 = vec_add(temp21, vb5);
+
+        const vector signed short temp31 = vec_sub(v_sumsB2, v_first);
+        const vector signed short v_sumsB3 = vec_add(temp31, vb6);
+
+        const vector signed short temp41 = vec_sub(v_sumsB3, v_first);
+        const vector signed short v_sumsB4 = vec_add(temp41, vb7);
+
+        const vector signed short temp51 = vec_sub(v_sumsB4, vb1);
+        const vector signed short v_sumsB5 = vec_add(temp51, vb8);
+
+        const vector signed short temp61 = vec_sub(v_sumsB5, vb2);
+        const vector signed short v_sumsB6 = vec_add(temp61, v_last);
+
+        const vector signed short temp71 = vec_sub(v_sumsB6, vb3);
+        const vector signed short v_sumsB7 = vec_add(temp71, v_last);
+
+        const vector signed short temp81 = vec_sub(v_sumsB7, vb4);
+        const vector signed short v_sumsB8 = vec_add(temp81, v_last);
+
+        const vector signed short temp91 = vec_sub(v_sumsB8, vb5);
+        const vector signed short v_sumsB9 = vec_add(temp91, v_last);
+
+    #define COMPUTE_VR(i, j, k)                                             \
+        const vector signed short temps1##i =                               \
+            vec_add(v_sumsB##i, v_sumsB##k);                                \
+        const vector signed short temps2##i =                               \
+            vec_mladd(vb##j, (vector signed short)v_2, temps1##i);          \
+        const vector signed short  vr##j = vec_sra(temps2##i, v_4)
+
+        COMPUTE_VR(0, 1, 2);
+        COMPUTE_VR(1, 2, 3);
+        COMPUTE_VR(2, 3, 4);
+        COMPUTE_VR(3, 4, 5);
+        COMPUTE_VR(4, 5, 6);
+        COMPUTE_VR(5, 6, 7);
+        COMPUTE_VR(6, 7, 8);
+        COMPUTE_VR(7, 8, 9);
+
+        const vector signed char neg1 = vec_splat_s8(-1);
+        const vector unsigned char permHH = (const vector unsigned char){0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+                                                                         0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
 
 #define PACK_AND_STORE(i)                                       \
-    const vector unsigned char perms##i =                       \
+{   const vector unsigned char perms##i =                       \
         vec_lvsr(i * stride, src2);                             \
     const vector unsigned char vf##i =                          \
         vec_packsu(vr##i, (vector signed short)zero);           \
@@ -341,39 +370,40 @@ static inline void doVertLowPass_altivec(uint8_t *src, int stride, PPContext *c)
     const vector unsigned char svB##i =                         \
         vec_sel(vg2##i, vbB##i, mask##i);                       \
     vec_st(svA##i, i * stride, src2);                           \
-    vec_st(svB##i, i * stride + 16, src2)
+    vec_st(svB##i, i * stride + 16, src2);}
 
 #define PACK_AND_STORE_ALIGNED(i)                               \
-    const vector unsigned char vf##i =                          \
+{   const vector unsigned char vf##i =                          \
         vec_packsu(vr##i, (vector signed short)zero);           \
     const vector unsigned char vg##i =                          \
         vec_perm(vf##i, vbT##i, permHH);                        \
-    vec_st(vg##i, i * stride, src2)
-
-    /* Special-casing the aligned case is worthwhile, as all calls from
-     * the (transposed) horizontable deblocks will be aligned, in addition
-     * to the naturally aligned vertical deblocks. */
-    if (properStride && srcAlign) {
-        PACK_AND_STORE_ALIGNED(1);
-        PACK_AND_STORE_ALIGNED(2);
-        PACK_AND_STORE_ALIGNED(3);
-        PACK_AND_STORE_ALIGNED(4);
-        PACK_AND_STORE_ALIGNED(5);
-        PACK_AND_STORE_ALIGNED(6);
-        PACK_AND_STORE_ALIGNED(7);
-        PACK_AND_STORE_ALIGNED(8);
-    } else {
-        PACK_AND_STORE(1);
-        PACK_AND_STORE(2);
-        PACK_AND_STORE(3);
-        PACK_AND_STORE(4);
-        PACK_AND_STORE(5);
-        PACK_AND_STORE(6);
-        PACK_AND_STORE(7);
-        PACK_AND_STORE(8);
+    vec_st(vg##i, i * stride, src2);}
+
+        /* Special-casing the aligned case is worthwhile, as all calls from
+         * the (transposed) horizontable deblocks will be aligned, in addition
+         * to the naturally aligned vertical deblocks. */
+        if (properStride && srcAlign) {
+            PACK_AND_STORE_ALIGNED(1)
+            PACK_AND_STORE_ALIGNED(2)
+            PACK_AND_STORE_ALIGNED(3)
+            PACK_AND_STORE_ALIGNED(4)
+            PACK_AND_STORE_ALIGNED(5)
+            PACK_AND_STORE_ALIGNED(6)
+            PACK_AND_STORE_ALIGNED(7)
+            PACK_AND_STORE_ALIGNED(8)
+        } else {
+            PACK_AND_STORE(1)
+            PACK_AND_STORE(2)
+            PACK_AND_STORE(3)
+            PACK_AND_STORE(4)
+            PACK_AND_STORE(5)
+            PACK_AND_STORE(6)
+            PACK_AND_STORE(7)
+            PACK_AND_STORE(8)
+        }
+    #undef PACK_AND_STORE
+    #undef PACK_AND_STORE_ALIGNED
     }
-#undef PACK_AND_STORE
-#undef PACK_AND_STORE_ALIGNED
 }
 
 
@@ -387,12 +417,11 @@ static inline void doVertDefFilter_altivec(uint8_t src[], int stride, PPContext
     can be removed by assuming proper alignment of
     src & stride :-(
     */
-    uint8_t *src2 = src;
+    uint8_t *src2 = src + stride*3;
     const vector signed int zero = vec_splat_s32(0);
-    DECLARE_ALIGNED(16, short, qp[8]);
-    qp[0] = 8*c->QP;
-    vector signed short vqp = vec_ld(0, qp);
-    vqp = vec_splat(vqp, 0);
+    DECLARE_ALIGNED(16, short, qp)[8] = {8*c->QP};
+    vector signed short vqp = vec_splat(
+                                (vector signed short)vec_ld(0, qp), 0);
 
 #define LOAD_LINE(i)                                                    \
     const vector unsigned char perm##i =                                \
@@ -407,8 +436,6 @@ static inline void doVertDefFilter_altivec(uint8_t src[], int stride, PPContext
         (vector signed short)vec_mergeh((vector unsigned char)zero,     \
                                         (vector unsigned char)vbT##i)
 
-    src2 += stride*3;
-
      LOAD_LINE(1);
      LOAD_LINE(2);
      LOAD_LINE(3);
@@ -479,11 +506,11 @@ static inline void doVertDefFilter_altivec(uint8_t src[], int stride, PPContext
      const vector unsigned char st5 = vec_packsu(vb5plusd,  (vector signed short)zero);
 
      const vector signed char neg1 = vec_splat_s8(-1);
-     const vector unsigned char permHH = (const vector unsigned char)AVV(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
-                                                                         0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F);
+     const vector unsigned char permHH = (const vector unsigned char){0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+                                                                      0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
 
 #define STORE(i)                                                \
-     const vector unsigned char perms##i =                      \
+{    const vector unsigned char perms##i =                      \
          vec_lvsr(i * stride, src2);                            \
      const vector unsigned char vg##i =                         \
          vec_perm(st##i, vbT##i, permHH);                       \
@@ -496,13 +523,46 @@ static inline void doVertDefFilter_altivec(uint8_t src[], int stride, PPContext
      const vector unsigned char svB##i =                        \
          vec_sel(vg2##i, vbB##i, mask##i);                      \
      vec_st(svA##i, i * stride, src2);                          \
-     vec_st(svB##i, i * stride + 16, src2)
+     vec_st(svB##i, i * stride + 16, src2);}
 
-     STORE(4);
-     STORE(5);
+     STORE(4)
+     STORE(5)
 }
 
 static inline void dering_altivec(uint8_t src[], int stride, PPContext *c) {
+    const vector signed int vsint32_8 = vec_splat_s32(8);
+    const vector unsigned int vuint32_4 = vec_splat_u32(4);
+    const vector signed char neg1 = vec_splat_s8(-1);
+
+    const vector unsigned char permA1 = (vector unsigned char)
+        {0x00, 0x01, 0x02, 0x10, 0x11, 0x12, 0x1F, 0x1F,
+         0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F};
+    const vector unsigned char permA2 = (vector unsigned char)
+        {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x10, 0x11,
+         0x12, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F};
+    const vector unsigned char permA1inc = (vector unsigned char)
+        {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,
+         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+    const vector unsigned char permA2inc = (vector unsigned char)
+        {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
+         0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+    const vector unsigned char magic = (vector unsigned char)
+        {0x01, 0x02, 0x01, 0x02, 0x04, 0x02, 0x01, 0x02,
+         0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+    const vector unsigned char extractPerm = (vector unsigned char)
+        {0x10, 0x10, 0x10, 0x01, 0x10, 0x10, 0x10, 0x01,
+         0x10, 0x10, 0x10, 0x01, 0x10, 0x10, 0x10, 0x01};
+    const vector unsigned char extractPermInc = (vector unsigned char)
+        {0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
+         0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01};
+    const vector unsigned char identity = vec_lvsl(0,(unsigned char *)0);
+    const vector unsigned char tenRight = (vector unsigned char)
+        {0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+    const vector unsigned char eightLeft = (vector unsigned char)
+        {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08};
+
     /*
     this code makes no assumption on src or stride.
     One could remove the recomputation of the perm
@@ -512,17 +572,15 @@ static inline void dering_altivec(uint8_t src[], int stride, PPContext *c) {
     src & stride :-(
     */
     uint8_t *srcCopy = src;
-    DECLARE_ALIGNED(16, uint8_t, dt[16]);
+    DECLARE_ALIGNED(16, uint8_t, dt)[16] = { deringThreshold };
     const vector signed int zero = vec_splat_s32(0);
-    vector unsigned char v_dt;
-    dt[0] = deringThreshold;
-    v_dt = vec_splat(vec_ld(0, dt), 0);
+    vector unsigned char v_dt = vec_splat(vec_ld(0, dt), 0);
 
-#define LOAD_LINE(i)                                                    \
-    const vector unsigned char perm##i =                               \
-        vec_lvsl(i * stride, srcCopy);                                 \
-    vector unsigned char sA##i = vec_ld(i * stride, srcCopy);          \
-    vector unsigned char sB##i = vec_ld(i * stride + 16, srcCopy);     \
+#define LOAD_LINE(i)                                                  \
+    const vector unsigned char perm##i =                              \
+        vec_lvsl(i * stride, srcCopy);                                \
+    vector unsigned char sA##i = vec_ld(i * stride, srcCopy);         \
+    vector unsigned char sB##i = vec_ld(i * stride + 16, srcCopy);    \
     vector unsigned char src##i = vec_perm(sA##i, sB##i, perm##i)
 
     LOAD_LINE(0);
@@ -538,31 +596,37 @@ static inline void dering_altivec(uint8_t src[], int stride, PPContext *c) {
 #undef LOAD_LINE
 
     vector unsigned char v_avg;
+    DECLARE_ALIGNED(16, signed int, S)[8];
+    DECLARE_ALIGNED(16, int, tQP2)[4] = { c->QP/2 + 1 };
+    vector signed int vQP2 = vec_ld(0, tQP2);
+    vQP2 = vec_splat(vQP2, 0);
+
     {
     const vector unsigned char trunc_perm = (vector unsigned char)
-        AVV(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
-            0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18);
+        {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
+         0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18};
     const vector unsigned char trunc_src12 = vec_perm(src1, src2, trunc_perm);
     const vector unsigned char trunc_src34 = vec_perm(src3, src4, trunc_perm);
     const vector unsigned char trunc_src56 = vec_perm(src5, src6, trunc_perm);
     const vector unsigned char trunc_src78 = vec_perm(src7, src8, trunc_perm);
 
 #define EXTRACT(op) do {                                                \
-    const vector unsigned char s##op##_1   = vec_##op(trunc_src12, trunc_src34); \
-    const vector unsigned char s##op##_2   = vec_##op(trunc_src56, trunc_src78); \
-    const vector unsigned char s##op##_6   = vec_##op(s##op##_1, s##op##_2);     \
-    const vector unsigned char s##op##_8h  = vec_mergeh(s##op##_6, s##op##_6);   \
-    const vector unsigned char s##op##_8l  = vec_mergel(s##op##_6, s##op##_6);   \
-    const vector unsigned char s##op##_9   = vec_##op(s##op##_8h, s##op##_8l);   \
-    const vector unsigned char s##op##_9h  = vec_mergeh(s##op##_9, s##op##_9);   \
-    const vector unsigned char s##op##_9l  = vec_mergel(s##op##_9, s##op##_9);   \
-    const vector unsigned char s##op##_10  = vec_##op(s##op##_9h, s##op##_9l);   \
-    const vector unsigned char s##op##_10h = vec_mergeh(s##op##_10, s##op##_10); \
-    const vector unsigned char s##op##_10l = vec_mergel(s##op##_10, s##op##_10); \
-    const vector unsigned char s##op##_11  = vec_##op(s##op##_10h, s##op##_10l); \
-    const vector unsigned char s##op##_11h = vec_mergeh(s##op##_11, s##op##_11); \
-    const vector unsigned char s##op##_11l = vec_mergel(s##op##_11, s##op##_11); \
-    v_##op = vec_##op(s##op##_11h, s##op##_11l); } while (0)
+    const vector unsigned char s_1   = vec_##op(trunc_src12, trunc_src34); \
+    const vector unsigned char s_2   = vec_##op(trunc_src56, trunc_src78); \
+    const vector unsigned char s_6   = vec_##op(s_1, s_2);     \
+    const vector unsigned char s_8h  = vec_mergeh(s_6, s_6);   \
+    const vector unsigned char s_8l  = vec_mergel(s_6, s_6);   \
+    const vector unsigned char s_9   = vec_##op(s_8h, s_8l);   \
+    const vector unsigned char s_9h  = vec_mergeh(s_9, s_9);   \
+    const vector unsigned char s_9l  = vec_mergel(s_9, s_9);   \
+    const vector unsigned char s_10  = vec_##op(s_9h, s_9l);   \
+    const vector unsigned char s_10h = vec_mergeh(s_10, s_10); \
+    const vector unsigned char s_10l = vec_mergel(s_10, s_10); \
+    const vector unsigned char s_11  = vec_##op(s_10h, s_10l); \
+    const vector unsigned char s_11h = vec_mergeh(s_11, s_11); \
+    const vector unsigned char s_11l = vec_mergel(s_11, s_11); \
+    v_##op = vec_##op(s_11h, s_11l);                           \
+} while (0)
 
     vector unsigned char v_min;
     vector unsigned char v_max;
@@ -576,34 +640,38 @@ static inline void dering_altivec(uint8_t src[], int stride, PPContext *c) {
     v_avg = vec_avg(v_min, v_max);
     }
 
-    DECLARE_ALIGNED(16, signed int, S[8]);
     {
     const vector unsigned short mask1 = (vector unsigned short)
-        AVV(0x0001, 0x0002, 0x0004, 0x0008,
-            0x0010, 0x0020, 0x0040, 0x0080);
+                                        {0x0001, 0x0002, 0x0004, 0x0008,
+                                         0x0010, 0x0020, 0x0040, 0x0080};
     const vector unsigned short mask2 = (vector unsigned short)
-        AVV(0x0100, 0x0200, 0x0000, 0x0000,
-            0x0000, 0x0000, 0x0000, 0x0000);
+                                        {0x0100, 0x0200, 0x0000, 0x0000,
+                                         0x0000, 0x0000, 0x0000, 0x0000};
 
     const vector unsigned int vuint32_16 = vec_sl(vec_splat_u32(1), vec_splat_u32(4));
     const vector unsigned int vuint32_1 = vec_splat_u32(1);
 
+    vector signed int sumA2;
+    vector signed int sumB2;
+    vector signed int sum0, sum1, sum2, sum3, sum4;
+    vector signed int sum5, sum6, sum7, sum8, sum9;
+
 #define COMPARE(i)                                                      \
-    vector signed int sum##i;                                           \
     do {                                                                \
-        const vector unsigned char cmp##i =                             \
+        const vector unsigned char cmp =                                \
             (vector unsigned char)vec_cmpgt(src##i, v_avg);             \
-        const vector unsigned short cmpHi##i =                          \
-            (vector unsigned short)vec_mergeh(cmp##i, cmp##i);          \
-        const vector unsigned short cmpLi##i =                          \
-            (vector unsigned short)vec_mergel(cmp##i, cmp##i);          \
-        const vector signed short cmpHf##i =                            \
-            (vector signed short)vec_and(cmpHi##i, mask1);              \
-        const vector signed short cmpLf##i =                            \
-            (vector signed short)vec_and(cmpLi##i, mask2);              \
-        const vector signed int sump##i = vec_sum4s(cmpHf##i, zero);    \
-        const vector signed int sumq##i = vec_sum4s(cmpLf##i, sump##i); \
-        sum##i  = vec_sums(sumq##i, zero); } while (0)
+        const vector unsigned short cmpHi =                             \
+            (vector unsigned short)vec_mergeh(cmp, cmp);                \
+        const vector unsigned short cmpLi =                             \
+            (vector unsigned short)vec_mergel(cmp, cmp);                \
+        const vector signed short cmpHf =                               \
+            (vector signed short)vec_and(cmpHi, mask1);                 \
+        const vector signed short cmpLf =                               \
+            (vector signed short)vec_and(cmpLi, mask2);                 \
+        const vector signed int sump = vec_sum4s(cmpHf, zero);          \
+        const vector signed int sumq = vec_sum4s(cmpLf, sump);          \
+        sum##i  = vec_sums(sumq, zero);                                 \
+    } while (0)
 
     COMPARE(0);
     COMPARE(1);
@@ -617,8 +685,6 @@ static inline void dering_altivec(uint8_t src[], int stride, PPContext *c) {
     COMPARE(9);
 #undef COMPARE
 
-    vector signed int sumA2;
-    vector signed int sumB2;
     {
     const vector signed int sump02 = vec_mergel(sum0, sum2);
     const vector signed int sump13 = vec_mergel(sum1, sum3);
@@ -672,86 +738,43 @@ static inline void dering_altivec(uint8_t src[], int stride, PPContext *c) {
     /* I'm not sure the following is actually faster
        than straight, unvectorized C code :-( */
 
-    DECLARE_ALIGNED(16, int, tQP2[4]);
-    tQP2[0]= c->QP/2 + 1;
-    vector signed int vQP2 = vec_ld(0, tQP2);
-    vQP2 = vec_splat(vQP2, 0);
-    const vector signed int vsint32_8 = vec_splat_s32(8);
-    const vector unsigned int vuint32_4 = vec_splat_u32(4);
-
-    const vector unsigned char permA1 = (vector unsigned char)
-        AVV(0x00, 0x01, 0x02, 0x10, 0x11, 0x12, 0x1F, 0x1F,
-            0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F);
-    const vector unsigned char permA2 = (vector unsigned char)
-        AVV(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x10, 0x11,
-            0x12, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F);
-    const vector unsigned char permA1inc = (vector unsigned char)
-        AVV(0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
-    const vector unsigned char permA2inc = (vector unsigned char)
-        AVV(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
-            0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
-    const vector unsigned char magic = (vector unsigned char)
-        AVV(0x01, 0x02, 0x01, 0x02, 0x04, 0x02, 0x01, 0x02,
-            0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
-    const vector unsigned char extractPerm = (vector unsigned char)
-        AVV(0x10, 0x10, 0x10, 0x01, 0x10, 0x10, 0x10, 0x01,
-            0x10, 0x10, 0x10, 0x01, 0x10, 0x10, 0x10, 0x01);
-    const vector unsigned char extractPermInc = (vector unsigned char)
-        AVV(0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
-            0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01);
-    const vector unsigned char identity = vec_lvsl(0,(unsigned char *)0);
-    const vector unsigned char tenRight = (vector unsigned char)
-        AVV(0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
-    const vector unsigned char eightLeft = (vector unsigned char)
-        AVV(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08);
-
-
-#define F_INIT(i)                                       \
-    vector unsigned char tenRightM##i = tenRight;       \
-    vector unsigned char permA1M##i = permA1;           \
-    vector unsigned char permA2M##i = permA2;           \
-    vector unsigned char extractPermM##i = extractPerm
+#define F_INIT()                                       \
+    vector unsigned char tenRightM = tenRight;         \
+    vector unsigned char permA1M = permA1;             \
+    vector unsigned char permA2M = permA2;             \
+    vector unsigned char extractPermM = extractPerm
 
 #define F2(i, j, k, l)                                                  \
     if (S[i] & (1 << (l+1))) {                                          \
-        const vector unsigned char a_##j##_A##l =                       \
-            vec_perm(src##i, src##j, permA1M##i);                       \
-        const vector unsigned char a_##j##_B##l =                       \
-            vec_perm(a_##j##_A##l, src##k, permA2M##i);                 \
-        const vector signed int a_##j##_sump##l =                       \
-            (vector signed int)vec_msum(a_##j##_B##l, magic,            \
-                                        (vector unsigned int)zero);     \
-        vector signed int F_##j##_##l =                                 \
-            vec_sr(vec_sums(a_##j##_sump##l, vsint32_8), vuint32_4);    \
-        F_##j##_##l = vec_splat(F_##j##_##l, 3);                        \
-        const vector signed int p_##j##_##l =                           \
-            (vector signed int)vec_perm(src##j,                         \
-                                        (vector unsigned char)zero,     \
-                                        extractPermM##i);               \
-        const vector signed int sum_##j##_##l  = vec_add( p_##j##_##l, vQP2);\
-        const vector signed int diff_##j##_##l = vec_sub( p_##j##_##l, vQP2);\
-        vector signed int newpm_##j##_##l;                              \
-        if (vec_all_lt(sum_##j##_##l, F_##j##_##l))                     \
-            newpm_##j##_##l = sum_##j##_##l;                            \
-        else if (vec_all_gt(diff_##j##_##l, F_##j##_##l))               \
-            newpm_##j##_##l = diff_##j##_##l;                           \
-        else newpm_##j##_##l = F_##j##_##l;                             \
-        const vector unsigned char newpm2_##j##_##l =                   \
-            vec_splat((vector unsigned char)newpm_##j##_##l, 15);       \
-        const vector unsigned char mask##j##l = vec_add(identity,       \
-                                                        tenRightM##i);  \
-        src##j = vec_perm(src##j, newpm2_##j##_##l, mask##j##l);        \
+        const vector unsigned char a_A = vec_perm(src##i, src##j, permA1M); \
+        const vector unsigned char a_B = vec_perm(a_A, src##k, permA2M); \
+        const vector signed int a_sump =                                \
+            (vector signed int)vec_msum(a_B, magic, (vector unsigned int)zero);\
+        vector signed int F = vec_sr(vec_sums(a_sump, vsint32_8), vuint32_4); \
+        const vector signed int p =                                     \
+            (vector signed int)vec_perm(src##j, (vector unsigned char)zero, \
+                                        extractPermM);                  \
+        const vector signed int sum  = vec_add(p, vQP2);                \
+        const vector signed int diff = vec_sub(p, vQP2);                \
+        vector signed int newpm;                                        \
+        vector unsigned char newpm2, mask;                              \
+        F = vec_splat(F, 3);                                            \
+        if (vec_all_lt(sum, F))                                         \
+            newpm = sum;                                                \
+        else if (vec_all_gt(diff, F))                                   \
+            newpm = diff;                                               \
+        else newpm = F;                                                 \
+        newpm2 = vec_splat((vector unsigned char)newpm, 15);            \
+        mask = vec_add(identity, tenRightM);                            \
+        src##j = vec_perm(src##j, newpm2, mask);                        \
     }                                                                   \
-    permA1M##i = vec_add(permA1M##i, permA1inc);                        \
-    permA2M##i = vec_add(permA2M##i, permA2inc);                        \
-    tenRightM##i = vec_sro(tenRightM##i, eightLeft);                    \
-    extractPermM##i = vec_add(extractPermM##i, extractPermInc)
+    permA1M = vec_add(permA1M, permA1inc);                              \
+    permA2M = vec_add(permA2M, permA2inc);                              \
+    tenRightM = vec_sro(tenRightM, eightLeft);                          \
+    extractPermM = vec_add(extractPermM, extractPermInc)
 
-#define ITER(i, j, k)                           \
-    F_INIT(i);                                  \
+#define ITER(i, j, k) do {                      \
+    F_INIT();                                   \
     F2(i, j, k, 0);                             \
     F2(i, j, k, 1);                             \
     F2(i, j, k, 2);                             \
@@ -759,7 +782,8 @@ static inline void dering_altivec(uint8_t src[], int stride, PPContext *c) {
     F2(i, j, k, 4);                             \
     F2(i, j, k, 5);                             \
     F2(i, j, k, 6);                             \
-    F2(i, j, k, 7)
+    F2(i, j, k, 7);                             \
+} while (0)
 
     ITER(0, 1, 2);
     ITER(1, 2, 3);
@@ -770,19 +794,18 @@ static inline void dering_altivec(uint8_t src[], int stride, PPContext *c) {
     ITER(6, 7, 8);
     ITER(7, 8, 9);
 
-    const vector signed char neg1 = vec_splat_s8(-1);
-
-#define STORE_LINE(i)                                   \
-    const vector unsigned char permST##i =              \
+#define STORE_LINE(i) do {                              \
+    const vector unsigned char permST =                 \
         vec_lvsr(i * stride, srcCopy);                  \
-    const vector unsigned char maskST##i =              \
+    const vector unsigned char maskST =                 \
         vec_perm((vector unsigned char)zero,            \
-                 (vector unsigned char)neg1, permST##i);\
-    src##i = vec_perm(src##i ,src##i, permST##i);       \
-    sA##i= vec_sel(sA##i, src##i, maskST##i);           \
-    sB##i= vec_sel(src##i, sB##i, maskST##i);           \
+                 (vector unsigned char)neg1, permST);   \
+    src##i = vec_perm(src##i ,src##i, permST);          \
+    sA##i= vec_sel(sA##i, src##i, maskST);              \
+    sB##i= vec_sel(src##i, sB##i, maskST);              \
     vec_st(sA##i, i * stride, srcCopy);                 \
-    vec_st(sB##i, i * stride + 16, srcCopy)
+    vec_st(sB##i, i * stride + 16, srcCopy);            \
+} while (0)
 
     STORE_LINE(1);
     STORE_LINE(2);
@@ -802,19 +825,19 @@ static inline void dering_altivec(uint8_t src[], int stride, PPContext *c) {
 #define doHorizDefFilter_altivec(a...) doHorizDefFilter_C(a)
 #define do_a_deblock_altivec(a...) do_a_deblock_C(a)
 
-static inline void RENAME(tempNoiseReducer)(uint8_t *src, int stride,
+static inline void tempNoiseReducer_altivec(uint8_t *src, int stride,
                                             uint8_t *tempBlurred, uint32_t *tempBlurredPast, int *maxNoise)
 {
+    const vector signed char neg1 = vec_splat_s8(-1);
+    const vector unsigned char permHH = (const vector unsigned char){0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+                                                                     0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
+
     const vector signed int zero = vec_splat_s32(0);
     const vector signed short vsint16_1 = vec_splat_s16(1);
     vector signed int v_dp = zero;
     vector signed int v_sysdp = zero;
     int d, sysd, i;
 
-    tempBlurredPast[127]= maxNoise[0];
-    tempBlurredPast[128]= maxNoise[1];
-    tempBlurredPast[129]= maxNoise[2];
-
 #define LOAD_LINE(src, i)                                               \
     register int j##src##i = i * stride;                                \
     vector unsigned char perm##src##i = vec_lvsl(j##src##i, src);       \
@@ -845,11 +868,12 @@ static inline void RENAME(tempNoiseReducer)(uint8_t *src, int stride,
     LOAD_LINE(tempBlurred, 7);
 #undef LOAD_LINE
 
-#define ACCUMULATE_DIFFS(i)                                     \
-    vector signed short v_d##i = vec_sub(v_tempBlurredAss##i,    \
-                                         v_srcAss##i);          \
-    v_dp = vec_msums(v_d##i, v_d##i, v_dp);                     \
-    v_sysdp = vec_msums(v_d##i, vsint16_1, v_sysdp)
+#define ACCUMULATE_DIFFS(i) do {                                \
+        vector signed short v_d = vec_sub(v_tempBlurredAss##i,  \
+                                          v_srcAss##i);         \
+        v_dp = vec_msums(v_d, v_d, v_dp);                       \
+        v_sysdp = vec_msums(v_d, vsint16_1, v_sysdp);           \
+    } while (0)
 
     ACCUMULATE_DIFFS(0);
     ACCUMULATE_DIFFS(1);
@@ -861,6 +885,10 @@ static inline void RENAME(tempNoiseReducer)(uint8_t *src, int stride,
     ACCUMULATE_DIFFS(7);
 #undef ACCUMULATE_DIFFS
 
+    tempBlurredPast[127]= maxNoise[0];
+    tempBlurredPast[128]= maxNoise[1];
+    tempBlurredPast[129]= maxNoise[2];
+
     v_dp = vec_sums(v_dp, zero);
     v_sysdp = vec_sums(v_sysdp, zero);
 
@@ -911,13 +939,12 @@ static inline void RENAME(tempNoiseReducer)(uint8_t *src, int stride,
             const vector signed short vsint16_4 = vec_splat_s16(4);
             const vector unsigned short vuint16_3 = vec_splat_u16(3);
 
-#define OP(i)                                                   \
-            const vector signed short v_temp##i =               \
-                vec_mladd(v_tempBlurredAss##i,                   \
-                          vsint16_7, v_srcAss##i);              \
-            const vector signed short v_temp2##i =              \
-                vec_add(v_temp##i, vsint16_4);                  \
-            v_tempBlurredAss##i = vec_sr(v_temp2##i, vuint16_3)
+#define OP(i) do {                                                      \
+            const vector signed short v_temp =                          \
+                vec_mladd(v_tempBlurredAss##i, vsint16_7, v_srcAss##i); \
+            const vector signed short v_temp2 = vec_add(v_temp, vsint16_4); \
+            v_tempBlurredAss##i = vec_sr(v_temp2, vuint16_3);           \
+        } while (0)
 
             OP(0);
             OP(1);
@@ -932,13 +959,13 @@ static inline void RENAME(tempNoiseReducer)(uint8_t *src, int stride,
             const vector signed short vsint16_3 = vec_splat_s16(3);
             const vector signed short vsint16_2 = vec_splat_s16(2);
 
-#define OP(i)                                                   \
-            const vector signed short v_temp##i =               \
-                vec_mladd(v_tempBlurredAss##i,                   \
-                          vsint16_3, v_srcAss##i);              \
-            const vector signed short v_temp2##i =              \
-                vec_add(v_temp##i, vsint16_2);                  \
-            v_tempBlurredAss##i = vec_sr(v_temp2##i, (vector unsigned short)vsint16_2)
+#define OP(i) do {                                              \
+            const vector signed short v_temp =                  \
+                vec_mladd(v_tempBlurredAss##i, vsint16_3, v_srcAss##i); \
+            const vector signed short v_temp2 = vec_add(v_temp, vsint16_2); \
+            v_tempBlurredAss##i =                                       \
+                vec_sr(v_temp2, (vector unsigned short)vsint16_2);      \
+        } while (0)
 
             OP(0);
             OP(1);
@@ -952,27 +979,19 @@ static inline void RENAME(tempNoiseReducer)(uint8_t *src, int stride,
         }
     }
 
-    const vector signed char neg1 = vec_splat_s8(-1);
-    const vector unsigned char permHH = (const vector unsigned char)AVV(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
-                                                                        0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F);
-
-#define PACK_AND_STORE(src, i)                                  \
-    const vector unsigned char perms##src##i =                  \
-        vec_lvsr(i * stride, src);                              \
-    const vector unsigned char vf##src##i =                     \
-        vec_packsu(v_tempBlurredAss##i, (vector signed short)zero); \
-    const vector unsigned char vg##src##i =                     \
-        vec_perm(vf##src##i, v_##src##A##i, permHH);            \
-    const vector unsigned char mask##src##i =                   \
-        vec_perm((vector unsigned char)zero, (vector unsigned char)neg1, perms##src##i); \
-    const vector unsigned char vg2##src##i =                    \
-        vec_perm(vg##src##i, vg##src##i, perms##src##i);        \
-    const vector unsigned char svA##src##i =                    \
-        vec_sel(v_##src##A1##i, vg2##src##i, mask##src##i);     \
-    const vector unsigned char svB##src##i =                    \
-        vec_sel(vg2##src##i, v_##src##A2##i, mask##src##i);     \
-    vec_st(svA##src##i, i * stride, src);                       \
-    vec_st(svB##src##i, i * stride + 16, src)
+#define PACK_AND_STORE(src, i) do {                                      \
+    const vector unsigned char perms = vec_lvsr(i * stride, src);        \
+    const vector unsigned char vf =                                      \
+        vec_packsu(v_tempBlurredAss##1, (vector signed short)zero);     \
+    const vector unsigned char vg = vec_perm(vf, v_##src##A##i, permHH); \
+    const vector unsigned char mask =                                    \
+        vec_perm((vector unsigned char)zero, (vector unsigned char)neg1, perms); \
+    const vector unsigned char vg2 = vec_perm(vg, vg, perms);            \
+    const vector unsigned char svA = vec_sel(v_##src##A1##i, vg2, mask); \
+    const vector unsigned char svB = vec_sel(vg2, v_##src##A2##i, mask); \
+    vec_st(svA, i * stride, src);                                        \
+    vec_st(svB, i * stride + 16, src);                                   \
+} while (0)
 
     PACK_AND_STORE(src, 0);
     PACK_AND_STORE(src, 1);
@@ -1029,16 +1048,16 @@ static inline void transpose_16x8_char_toPackedAlign_altivec(unsigned char* dst,
     vector unsigned char tempO = vec_mergeh(src7, zero);
     vector unsigned char tempP = vec_mergel(src7, zero);
 
-    vector unsigned char temp0 = vec_mergeh(tempA, tempI);
-    vector unsigned char temp1 = vec_mergel(tempA, tempI);
-    vector unsigned char temp2 = vec_mergeh(tempB, tempJ);
-    vector unsigned char temp3 = vec_mergel(tempB, tempJ);
-    vector unsigned char temp4 = vec_mergeh(tempC, tempK);
-    vector unsigned char temp5 = vec_mergel(tempC, tempK);
-    vector unsigned char temp6 = vec_mergeh(tempD, tempL);
-    vector unsigned char temp7 = vec_mergel(tempD, tempL);
-    vector unsigned char temp8 = vec_mergeh(tempE, tempM);
-    vector unsigned char temp9 = vec_mergel(tempE, tempM);
+    vector unsigned char temp0  = vec_mergeh(tempA, tempI);
+    vector unsigned char temp1  = vec_mergel(tempA, tempI);
+    vector unsigned char temp2  = vec_mergeh(tempB, tempJ);
+    vector unsigned char temp3  = vec_mergel(tempB, tempJ);
+    vector unsigned char temp4  = vec_mergeh(tempC, tempK);
+    vector unsigned char temp5  = vec_mergel(tempC, tempK);
+    vector unsigned char temp6  = vec_mergeh(tempD, tempL);
+    vector unsigned char temp7  = vec_mergel(tempD, tempL);
+    vector unsigned char temp8  = vec_mergeh(tempE, tempM);
+    vector unsigned char temp9  = vec_mergel(tempE, tempM);
     vector unsigned char temp10 = vec_mergeh(tempF, tempN);
     vector unsigned char temp11 = vec_mergel(tempF, tempN);
     vector unsigned char temp12 = vec_mergeh(tempG, tempO);
@@ -1063,16 +1082,16 @@ static inline void transpose_16x8_char_toPackedAlign_altivec(unsigned char* dst,
     tempO = vec_mergeh(temp7, temp15);
     tempP = vec_mergel(temp7, temp15);
 
-    temp0 = vec_mergeh(tempA, tempI);
-    temp1 = vec_mergel(tempA, tempI);
-    temp2 = vec_mergeh(tempB, tempJ);
-    temp3 = vec_mergel(tempB, tempJ);
-    temp4 = vec_mergeh(tempC, tempK);
-    temp5 = vec_mergel(tempC, tempK);
-    temp6 = vec_mergeh(tempD, tempL);
-    temp7 = vec_mergel(tempD, tempL);
-    temp8 = vec_mergeh(tempE, tempM);
-    temp9 = vec_mergel(tempE, tempM);
+    temp0  = vec_mergeh(tempA, tempI);
+    temp1  = vec_mergel(tempA, tempI);
+    temp2  = vec_mergeh(tempB, tempJ);
+    temp3  = vec_mergel(tempB, tempJ);
+    temp4  = vec_mergeh(tempC, tempK);
+    temp5  = vec_mergel(tempC, tempK);
+    temp6  = vec_mergeh(tempD, tempL);
+    temp7  = vec_mergel(tempD, tempL);
+    temp8  = vec_mergeh(tempE, tempM);
+    temp9  = vec_mergel(tempE, tempM);
     temp10 = vec_mergeh(tempF, tempN);
     temp11 = vec_mergel(tempF, tempN);
     temp12 = vec_mergeh(tempG, tempO);
@@ -1080,16 +1099,16 @@ static inline void transpose_16x8_char_toPackedAlign_altivec(unsigned char* dst,
     temp14 = vec_mergeh(tempH, tempP);
     temp15 = vec_mergel(tempH, tempP);
 
-    vec_st(temp0, 0, dst);
-    vec_st(temp1, 16, dst);
-    vec_st(temp2, 32, dst);
-    vec_st(temp3, 48, dst);
-    vec_st(temp4, 64, dst);
-    vec_st(temp5, 80, dst);
-    vec_st(temp6, 96, dst);
-    vec_st(temp7, 112, dst);
-    vec_st(temp8, 128, dst);
-    vec_st(temp9, 144, dst);
+    vec_st(temp0,    0, dst);
+    vec_st(temp1,   16, dst);
+    vec_st(temp2,   32, dst);
+    vec_st(temp3,   48, dst);
+    vec_st(temp4,   64, dst);
+    vec_st(temp5,   80, dst);
+    vec_st(temp6,   96, dst);
+    vec_st(temp7,  112, dst);
+    vec_st(temp8,  128, dst);
+    vec_st(temp9,  144, dst);
     vec_st(temp10, 160, dst);
     vec_st(temp11, 176, dst);
     vec_st(temp12, 192, dst);
@@ -1100,6 +1119,7 @@ static inline void transpose_16x8_char_toPackedAlign_altivec(unsigned char* dst,
 
 static inline void transpose_8x16_char_fromPackedAlign_altivec(unsigned char* dst, unsigned char* src, int stride) {
     const vector unsigned char zero = vec_splat_u8(0);
+    const vector signed   char neg1 = vec_splat_s8(-1);
 
 #define LOAD_DOUBLE_LINE(i, j)                                  \
     vector unsigned char src##i = vec_ld(i * 16, src);            \
@@ -1160,26 +1180,28 @@ static inline void transpose_8x16_char_fromPackedAlign_altivec(unsigned char* ds
     temp7 = vec_mergel(tempD, tempL);
 
 
-    const vector signed char neg1 = vec_splat_s8(-1);
-#define STORE_DOUBLE_LINE(i, j)                                         \
-    vector unsigned char dstA##i = vec_ld(i * stride, dst);             \
-    vector unsigned char dstB##i = vec_ld(i * stride + 16, dst);        \
-    vector unsigned char dstA##j = vec_ld(j * stride, dst);             \
-    vector unsigned char dstB##j = vec_ld(j * stride+ 16, dst);         \
-    vector unsigned char align##i = vec_lvsr(i * stride, dst);          \
-    vector unsigned char align##j = vec_lvsr(j * stride, dst);          \
-    vector unsigned char mask##i = vec_perm(zero, (vector unsigned char)neg1, align##i); \
-    vector unsigned char mask##j = vec_perm(zero, (vector unsigned char)neg1, align##j); \
-    vector unsigned char dstR##i = vec_perm(temp##i, temp##i, align##i);\
-    vector unsigned char dstR##j = vec_perm(temp##j, temp##j, align##j);\
-    vector unsigned char dstAF##i = vec_sel(dstA##i, dstR##i, mask##i); \
-    vector unsigned char dstBF##i = vec_sel(dstR##i, dstB##i, mask##i); \
-    vector unsigned char dstAF##j = vec_sel(dstA##j, dstR##j, mask##j); \
-    vector unsigned char dstBF##j = vec_sel(dstR##j, dstB##j, mask##j); \
-    vec_st(dstAF##i, i * stride, dst);                                  \
-    vec_st(dstBF##i, i * stride + 16, dst);                             \
-    vec_st(dstAF##j, j * stride, dst);                                  \
-    vec_st(dstBF##j, j * stride + 16, dst)
+#define STORE_DOUBLE_LINE(i, j) do {                                    \
+    vector unsigned char dstAi = vec_ld(i * stride, dst);               \
+    vector unsigned char dstBi = vec_ld(i * stride + 16, dst);          \
+    vector unsigned char dstAj = vec_ld(j * stride, dst);               \
+    vector unsigned char dstBj = vec_ld(j * stride+ 16, dst);           \
+    vector unsigned char aligni = vec_lvsr(i * stride, dst);            \
+    vector unsigned char alignj = vec_lvsr(j * stride, dst);            \
+    vector unsigned char maski =                                        \
+        vec_perm(zero, (vector unsigned char)neg1, aligni);             \
+    vector unsigned char maskj =                                        \
+        vec_perm(zero, (vector unsigned char)neg1, alignj);             \
+    vector unsigned char dstRi = vec_perm(temp##i, temp##i, aligni);    \
+    vector unsigned char dstRj = vec_perm(temp##j, temp##j, alignj);    \
+    vector unsigned char dstAFi = vec_sel(dstAi, dstRi, maski);         \
+    vector unsigned char dstBFi = vec_sel(dstRi, dstBi, maski);         \
+    vector unsigned char dstAFj = vec_sel(dstAj, dstRj, maskj);         \
+    vector unsigned char dstBFj = vec_sel(dstRj, dstBj, maskj);         \
+    vec_st(dstAFi, i * stride, dst);                                    \
+    vec_st(dstBFi, i * stride + 16, dst);                               \
+    vec_st(dstAFj, j * stride, dst);                                    \
+    vec_st(dstBFj, j * stride + 16, dst);                               \
+} while (0)
 
     STORE_DOUBLE_LINE(0,1);
     STORE_DOUBLE_LINE(2,3);