[FFmpeg-devel] [PATCH] move/factorize mirror() to libavutil: av_mirror_int()

Michael Niedermayer michaelni at gmx.at
Thu Jun 21 00:12:32 CEST 2012


Signed-off-by: Michael Niedermayer <michaelni at gmx.at>
---
 libavcodec/dwt.c        |   90 +++++++++++++++++++++--------------------------
 libavfilter/transform.c |   14 ++------
 libavutil/common.h      |   10 ++++++
 3 files changed, 52 insertions(+), 62 deletions(-)

diff --git a/libavcodec/dwt.c b/libavcodec/dwt.c
index 3980f06..179c5c0 100644
--- a/libavcodec/dwt.c
+++ b/libavcodec/dwt.c
@@ -106,16 +106,6 @@ void ff_slice_buffer_destroy(slice_buffer *buf)
     av_freep(&buf->line);
 }
 
-static inline int mirror(int v, int m)
-{
-    while ((unsigned)v > (unsigned)m) {
-        v = -v;
-        if (v < 0)
-            v += 2 * m;
-    }
-    return v;
-}
-
 static av_always_inline void lift(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
                                   int dst_step, int src_step, int ref_step,
                                   int width, int mul, int add, int shift,
@@ -316,12 +306,12 @@ static void spatial_decompose53i(DWTELEM *buffer, DWTELEM *temp,
                                  int width, int height, int stride)
 {
     int y;
-    DWTELEM *b0 = buffer + mirror(-2 - 1, height - 1) * stride;
-    DWTELEM *b1 = buffer + mirror(-2,     height - 1) * stride;
+    DWTELEM *b0 = buffer + av_mirror_int(-2 - 1, height - 1) * stride;
+    DWTELEM *b1 = buffer + av_mirror_int(-2,     height - 1) * stride;
 
     for (y = -2; y < height; y += 2) {
-        DWTELEM *b2 = buffer + mirror(y + 1, height - 1) * stride;
-        DWTELEM *b3 = buffer + mirror(y + 2, height - 1) * stride;
+        DWTELEM *b2 = buffer + av_mirror_int(y + 1, height - 1) * stride;
+        DWTELEM *b3 = buffer + av_mirror_int(y + 2, height - 1) * stride;
 
         if (y + 1 < (unsigned)height)
             horizontal_decompose53i(b2, temp, width);
@@ -393,14 +383,14 @@ static void spatial_decompose97i(DWTELEM *buffer, DWTELEM *temp,
                                  int width, int height, int stride)
 {
     int y;
-    DWTELEM *b0 = buffer + mirror(-4 - 1, height - 1) * stride;
-    DWTELEM *b1 = buffer + mirror(-4,     height - 1) * stride;
-    DWTELEM *b2 = buffer + mirror(-4 + 1, height - 1) * stride;
-    DWTELEM *b3 = buffer + mirror(-4 + 2, height - 1) * stride;
+    DWTELEM *b0 = buffer + av_mirror_int(-4 - 1, height - 1) * stride;
+    DWTELEM *b1 = buffer + av_mirror_int(-4,     height - 1) * stride;
+    DWTELEM *b2 = buffer + av_mirror_int(-4 + 1, height - 1) * stride;
+    DWTELEM *b3 = buffer + av_mirror_int(-4 + 2, height - 1) * stride;
 
     for (y = -4; y < height; y += 2) {
-        DWTELEM *b4 = buffer + mirror(y + 3, height - 1) * stride;
-        DWTELEM *b5 = buffer + mirror(y + 4, height - 1) * stride;
+        DWTELEM *b4 = buffer + av_mirror_int(y + 3, height - 1) * stride;
+        DWTELEM *b5 = buffer + av_mirror_int(y + 4, height - 1) * stride;
 
         if (y + 3 < (unsigned)height)
             horizontal_decompose97i(b4, temp, width);
@@ -491,16 +481,16 @@ static void spatial_compose53i_buffered_init(DWTCompose *cs, slice_buffer *sb,
                                              int height, int stride_line)
 {
     cs->b0 = slice_buffer_get_line(sb,
-                                   mirror(-1 - 1, height - 1) * stride_line);
-    cs->b1 = slice_buffer_get_line(sb, mirror(-1, height - 1) * stride_line);
+                                   av_mirror_int(-1 - 1, height - 1) * stride_line);
+    cs->b1 = slice_buffer_get_line(sb, av_mirror_int(-1, height - 1) * stride_line);
     cs->y  = -1;
 }
 
 static void spatial_compose53i_init(DWTCompose *cs, IDWTELEM *buffer,
                                     int height, int stride)
 {
-    cs->b0 = buffer + mirror(-1 - 1, height - 1) * stride;
-    cs->b1 = buffer + mirror(-1,     height - 1) * stride;
+    cs->b0 = buffer + av_mirror_int(-1 - 1, height - 1) * stride;
+    cs->b1 = buffer + av_mirror_int(-1,     height - 1) * stride;
     cs->y  = -1;
 }
 
@@ -514,10 +504,10 @@ static void spatial_compose53i_dy_buffered(DWTCompose *cs, slice_buffer *sb,
     IDWTELEM *b0 = cs->b0;
     IDWTELEM *b1 = cs->b1;
     IDWTELEM *b2 = slice_buffer_get_line(sb,
-                                         mirror(y + 1, height - 1) *
+                                         av_mirror_int(y + 1, height - 1) *
                                          stride_line);
     IDWTELEM *b3 = slice_buffer_get_line(sb,
-                                         mirror(y + 2, height - 1) *
+                                         av_mirror_int(y + 2, height - 1) *
                                          stride_line);
 
     if (y + 1 < (unsigned)height && y < (unsigned)height) {
@@ -551,8 +541,8 @@ static void spatial_compose53i_dy(DWTCompose *cs, IDWTELEM *buffer,
     int y        = cs->y;
     IDWTELEM *b0 = cs->b0;
     IDWTELEM *b1 = cs->b1;
-    IDWTELEM *b2 = buffer + mirror(y + 1, height - 1) * stride;
-    IDWTELEM *b3 = buffer + mirror(y + 2, height - 1) * stride;
+    IDWTELEM *b2 = buffer + av_mirror_int(y + 1, height - 1) * stride;
+    IDWTELEM *b3 = buffer + av_mirror_int(y + 2, height - 1) * stride;
 
     if (y + 1 < (unsigned)height)
         vertical_compose53iL0(b1, b2, b3, width);
@@ -675,20 +665,20 @@ void ff_snow_vertical_compose97i(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2,
 static void spatial_compose97i_buffered_init(DWTCompose *cs, slice_buffer *sb,
                                              int height, int stride_line)
 {
-    cs->b0 = slice_buffer_get_line(sb, mirror(-3 - 1, height - 1) * stride_line);
-    cs->b1 = slice_buffer_get_line(sb, mirror(-3,     height - 1) * stride_line);
-    cs->b2 = slice_buffer_get_line(sb, mirror(-3 + 1, height - 1) * stride_line);
-    cs->b3 = slice_buffer_get_line(sb, mirror(-3 + 2, height - 1) * stride_line);
+    cs->b0 = slice_buffer_get_line(sb, av_mirror_int(-3 - 1, height - 1) * stride_line);
+    cs->b1 = slice_buffer_get_line(sb, av_mirror_int(-3,     height - 1) * stride_line);
+    cs->b2 = slice_buffer_get_line(sb, av_mirror_int(-3 + 1, height - 1) * stride_line);
+    cs->b3 = slice_buffer_get_line(sb, av_mirror_int(-3 + 2, height - 1) * stride_line);
     cs->y  = -3;
 }
 
 static void spatial_compose97i_init(DWTCompose *cs, IDWTELEM *buffer, int height,
                                     int stride)
 {
-    cs->b0 = buffer + mirror(-3 - 1, height - 1) * stride;
-    cs->b1 = buffer + mirror(-3,     height - 1) * stride;
-    cs->b2 = buffer + mirror(-3 + 1, height - 1) * stride;
-    cs->b3 = buffer + mirror(-3 + 2, height - 1) * stride;
+    cs->b0 = buffer + av_mirror_int(-3 - 1, height - 1) * stride;
+    cs->b1 = buffer + av_mirror_int(-3,     height - 1) * stride;
+    cs->b2 = buffer + av_mirror_int(-3 + 1, height - 1) * stride;
+    cs->b3 = buffer + av_mirror_int(-3 + 2, height - 1) * stride;
     cs->y  = -3;
 }
 
@@ -704,10 +694,10 @@ static void spatial_compose97i_dy_buffered(DWTContext *dsp, DWTCompose *cs,
     IDWTELEM *b2 = cs->b2;
     IDWTELEM *b3 = cs->b3;
     IDWTELEM *b4 = slice_buffer_get_line(sb,
-                                         mirror(y + 3, height - 1) *
+                                         av_mirror_int(y + 3, height - 1) *
                                          stride_line);
     IDWTELEM *b5 = slice_buffer_get_line(sb,
-                                         mirror(y + 4, height - 1) *
+                                         av_mirror_int(y + 4, height - 1) *
                                          stride_line);
 
     if (y > 0 && y + 4 < height) {
@@ -744,8 +734,8 @@ static void spatial_compose97i_dy(DWTCompose *cs, IDWTELEM *buffer,
     IDWTELEM *b1 = cs->b1;
     IDWTELEM *b2 = cs->b2;
     IDWTELEM *b3 = cs->b3;
-    IDWTELEM *b4 = buffer + mirror(y + 3, height - 1) * stride;
-    IDWTELEM *b5 = buffer + mirror(y + 4, height - 1) * stride;
+    IDWTELEM *b4 = buffer + av_mirror_int(y + 3, height - 1) * stride;
+    IDWTELEM *b5 = buffer + av_mirror_int(y + 4, height - 1) * stride;
 
     if (y + 3 < (unsigned)height)
         vertical_compose97iL1(b3, b4, b5, width);
@@ -1260,8 +1250,8 @@ static void spatial_compose_dirac53i_dy(DWTContext *d, int level, int width, int
 
     int y= cs->y;
     IDWTELEM *b[4] = { cs->b[0], cs->b[1] };
-    b[2] = d->buffer + mirror(y+1, height-1)*stride;
-    b[3] = d->buffer + mirror(y+2, height-1)*stride;
+    b[2] = d->buffer + av_mirror_int(y+1, height-1)*stride;
+    b[3] = d->buffer + av_mirror_int(y+2, height-1)*stride;
 
         if(y+1<(unsigned)height) vertical_compose_l0(b[1], b[2], b[3], width);
         if(y+0<(unsigned)height) vertical_compose_h0(b[0], b[1], b[2], width);
@@ -1353,8 +1343,8 @@ static void spatial_compose_daub97i_dy(DWTContext *d, int level, int width, int
     IDWTELEM *b[6];
     for (i = 0; i < 4; i++)
         b[i] = cs->b[i];
-    b[4] = d->buffer + mirror(y+3, height-1)*stride;
-    b[5] = d->buffer + mirror(y+4, height-1)*stride;
+    b[4] = d->buffer + av_mirror_int(y+3, height-1)*stride;
+    b[5] = d->buffer + av_mirror_int(y+4, height-1)*stride;
 
         if(y+3<(unsigned)height) vertical_compose_l1(b[3], b[4], b[5], width);
         if(y+2<(unsigned)height) vertical_compose_h1(b[2], b[3], b[4], width);
@@ -1372,17 +1362,17 @@ static void spatial_compose_daub97i_dy(DWTContext *d, int level, int width, int
 
 static void spatial_compose97i_init2(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
 {
-    cs->b[0] = buffer + mirror(-3-1, height-1)*stride;
-    cs->b[1] = buffer + mirror(-3  , height-1)*stride;
-    cs->b[2] = buffer + mirror(-3+1, height-1)*stride;
-    cs->b[3] = buffer + mirror(-3+2, height-1)*stride;
+    cs->b[0] = buffer + av_mirror_int(-3-1, height-1)*stride;
+    cs->b[1] = buffer + av_mirror_int(-3  , height-1)*stride;
+    cs->b[2] = buffer + av_mirror_int(-3+1, height-1)*stride;
+    cs->b[3] = buffer + av_mirror_int(-3+2, height-1)*stride;
     cs->y = -3;
 }
 
 static void spatial_compose53i_init2(DWTCompose *cs, IDWTELEM *buffer, int height, int stride)
 {
-    cs->b[0] = buffer + mirror(-1-1, height-1)*stride;
-    cs->b[1] = buffer + mirror(-1  , height-1)*stride;
+    cs->b[0] = buffer + av_mirror_int(-1-1, height-1)*stride;
+    cs->b[1] = buffer + av_mirror_int(-1  , height-1)*stride;
     cs->y = -1;
 }
 
diff --git a/libavfilter/transform.c b/libavfilter/transform.c
index b3f85dd..19ce2b1 100644
--- a/libavfilter/transform.c
+++ b/libavfilter/transform.c
@@ -136,16 +136,6 @@ void avfilter_mul_matrix(const float *m1, float scalar, float *result)
         result[i] = m1[i] * scalar;
 }
 
-static inline int mirror(int v, int m)
-{
-    while ((unsigned)v > (unsigned)m) {
-        v = -v;
-        if (v < 0)
-            v += 2 * m;
-    }
-    return v;
-}
-
 void avfilter_transform(const uint8_t *src, uint8_t *dst,
                         int src_stride, int dst_stride,
                         int width, int height, const float *matrix,
@@ -184,8 +174,8 @@ void avfilter_transform(const uint8_t *src, uint8_t *dst,
                     def = src[(int)y_s * src_stride + (int)x_s];
                     break;
                 case FILL_MIRROR:
-                    x_s = mirror(x_s,  width-1);
-                    y_s = mirror(y_s, height-1);
+                    x_s = av_mirror_int(x_s,  width-1);
+                    y_s = av_mirror_int(y_s, height-1);
 
                     av_assert2(x_s >= 0 && y_s >= 0);
                     av_assert2(x_s < width && y_s < height);
diff --git a/libavutil/common.h b/libavutil/common.h
index a11a325..5f7260c 100644
--- a/libavutil/common.h
+++ b/libavutil/common.h
@@ -97,6 +97,16 @@ static av_always_inline av_const int av_log2_16bit_c(unsigned int v)
     return n;
 }
 
+static inline int av_mirror_int(int v, int m)
+{
+    while ((unsigned)v > (unsigned)m) {
+        v = -v;
+        if (v < 0)
+            v += 2 * m;
+    }
+    return v;
+}
+
 #ifdef HAVE_AV_CONFIG_H
 #   include "config.h"
 #   include "intmath.h"
-- 
1.7.9.5



More information about the ffmpeg-devel mailing list