Merge ../mlt
[melted] / src / modules / gtk2 / pixops.c
index 9fe02b0..05b8292 100644 (file)
@@ -1,9 +1,12 @@
 /* GdkPixbuf library - Scaling and compositing functions
  *
- * Copyright (C) 1999 The Free Software Foundation
- *
+ * Original:
+ * Copyright (C) 2000 Red Hat, Inc
  * Author: Owen Taylor <otaylor@redhat.com>
- * Modified for YUV422 by Dan Dennedy <dan@dennedy.org>
+ *
+ * Modification for MLT:
+ * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
+ * Author: Dan Dennedy <dan@dennedy.org>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -20,7 +23,7 @@
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
+
 #include <math.h>
 #include <glib.h>
 #include <stdio.h>
@@ -58,8 +61,8 @@ typedef void ( *PixopsPixelFunc ) ( guchar *dest, guint y1, guint cr, guint y2,
 
 
 /* mmx function declarations */
-#ifdef USE_MMX
-guchar *pixops_scale_line_22_33_mmx ( guint32 weights[ 16 ][ 8 ], guchar *p, guchar *q1, guchar *q2, int x_step, guchar *p_stop, int x_init );
+#if defined(USE_MMX) && !defined(ARCH_X86_64)
+guchar *pixops_scale_line_22_yuv_mmx ( guint32 weights[ 16 ][ 8 ], guchar *p, guchar *q1, guchar *q2, int x_step, guchar *p_stop, int x_init, int destx );
 int pixops_have_mmx ( void );
 #endif
 
@@ -92,27 +95,22 @@ pixops_scale_nearest ( guchar *dest_buf,
                        double scale_x,
                        double scale_y )
 {
-       int i, j;
-       int x;
-       int x_step = ( 1 << SCALE_SHIFT ) / scale_x;
-       int y_step = ( 1 << SCALE_SHIFT ) / scale_y;
+       register int i, j;
+       register int x_step = ( 1 << SCALE_SHIFT ) / scale_x;
+       register int y_step = ( 1 << SCALE_SHIFT ) / scale_y;
+       register int x, x_scaled;
 
        for ( i = 0; i < ( render_y1 - render_y0 ); i++ )
        {
-               const guchar *src = src_buf + ( ( ( i + render_y0 ) * y_step + y_step / 2 ) >> SCALE_SHIFT ) * src_rowstride;
+               const guchar *src = src_buf + ( ( ( i + render_y0 ) * y_step + ( y_step >> 1 ) ) >> SCALE_SHIFT ) * src_rowstride;
                guchar *dest = dest_buf + i * dest_rowstride;
-
-               x = render_x0 * x_step + x_step / 2;
+               x = render_x0 * x_step + ( x_step >> 1 );
 
                for ( j = 0; j < ( render_x1 - render_x0 ); j++ )
                {
-                       const guchar *p = src + ( x >> SCALE_SHIFT ) * 4;
-                       guint32 *p32;
-
-                       p32 = ( guint32 * ) dest;
-                       *p32 = *( ( guint32 * ) p );
-
-                       dest += 4;
+                       x_scaled = x >> SCALE_SHIFT;
+                       *dest++ = src[ x_scaled << 1 ];
+                       *dest++ = src[ ( ( x_scaled >> 1 ) << 2 ) + ( ( j & 1 ) << 1 ) + 1 ];
                        x += x_step;
                }
        }
@@ -125,51 +123,45 @@ scale_line ( int *weights, int n_x, int n_y,
              guchar **src,
              int x_init, int x_step, int src_width )
 {
-       int x = x_init;
-       int i, j;
+       register int x = x_init;
+       register int i, j, x_scaled, y_index, uv_index;
 
        while ( dest < dest_end )
        {
-               int x_scaled = x >> SCALE_SHIFT;
-               int *pixel_weights;
+               unsigned int y = 0, uv = 0;
+               int *pixel_weights = weights + ( ( x >> ( SCALE_SHIFT - SUBSAMPLE_BITS ) ) & SUBSAMPLE_MASK ) * n_x * n_y;
 
-               pixel_weights = weights + ( ( x >> ( SCALE_SHIFT - SUBSAMPLE_BITS ) ) & SUBSAMPLE_MASK ) * n_x * n_y;
+               x_scaled = x >> SCALE_SHIFT;
+               y_index = x_scaled << 1;
+               uv_index = ( ( x_scaled >> 1 ) << 2 ) + ( ( dest_x & 1 ) << 1 ) + 1;
 
-               unsigned int y1 = 0, cb = 0, y2 = 0, cr = 0;
                for ( i = 0; i < n_y; i++ )
                {
-                       guchar *q = src[ i ] + x_scaled * 4;
                        int *line_weights = pixel_weights + n_x * i;
+                       guchar *q = src[ i ];
 
-                       for ( j = 0; j < n_x; j++ )
+                       for ( j = 0; j < n_x; j ++ )
                        {
                                unsigned int ta = line_weights[ j ];
 
-                               y1 += ta * q[ 0 ];
-                               cb += ta * q[ 1 ];
-                               y2 += ta * q[ 2 ];
-                               cr += ta * q[ 3 ];
-
-                               q += 4;
+                               y  += ta * q[ y_index ];
+                               uv += ta * q[ uv_index ];
                        }
                }
 
-               dest[ 0 ] = ( y1 + 0xffff ) >> 16;
-               dest[ 1 ] = ( cb + 0xffff ) >> 16;
-               dest[ 2 ] = ( y2 + 0xffff ) >> 16;
-               dest[ 3 ] = ( cr + 0xffff ) >> 16;
-
-               dest += 4;
+               *dest++ = ( y  + 0xffff ) >> SCALE_SHIFT;
+               *dest++ = ( uv + 0xffff ) >> SCALE_SHIFT;
 
                x += x_step;
+               dest_x++;
        }
 
        return dest;
 }
 
-#ifdef USE_MMX
+#if defined(USE_MMX) && !defined(ARCH_X86_64)
 static inline guchar *
-scale_line_22_33_mmx_stub ( int *weights, int n_x, int n_y,
+scale_line_22_yuv_mmx_stub ( int *weights, int n_x, int n_y,
                             guchar *dest, int dest_x, guchar *dest_end,
                             guchar **src,
                             int x_init, int x_step, int src_width )
@@ -189,65 +181,59 @@ scale_line_22_33_mmx_stub ( int *weights, int n_x, int n_y,
                mmx_weights[ j ][ 7 ] = 0x00010001 * ( weights[ 4 * j + 3 ] >> 8 );
        }
 
-       return pixops_scale_line_22_33_mmx ( mmx_weights, dest, src[ 0 ], src[ 1 ], x_step, dest_end, x_init );
+       return pixops_scale_line_22_yuv_mmx ( mmx_weights, dest, src[ 0 ], src[ 1 ], x_step, dest_end, x_init, dest_x );
 }
 #endif /* USE_MMX */
 
 static inline guchar *
-scale_line_22_33 ( int *weights, int n_x, int n_y,
+scale_line_22_yuv ( int *weights, int n_x, int n_y,
                    guchar *dest, int dest_x, guchar *dest_end,
                    guchar **src,
                    int x_init, int x_step, int src_width )
 {
-       int x = x_init;
-       guchar *src0 = src[ 0 ];
-       guchar *src1 = src[ 1 ];
+       register int x = x_init;
+       register guchar *src0 = src[ 0 ];
+       register guchar *src1 = src[ 1 ];
+       register unsigned int p;
+       register guchar *q0, *q1;
+       register int w1, w2, w3, w4;
+       register int x_scaled, x_aligned, uv_index;
 
        while ( dest < dest_end )
        {
-               unsigned int y1, cb, y2, cr;
-               int x_scaled = x >> SCALE_SHIFT;
-               int *pixel_weights;
-               guchar *q0, *q1;
-               int w1, w2, w3, w4;
-
-               q0 = src0 + x_scaled * 4;
-               q1 = src1 + x_scaled * 4;
+               int *pixel_weights = weights + ( ( x >> ( SCALE_SHIFT - SUBSAMPLE_BITS ) ) & SUBSAMPLE_MASK ) * 4;
 
-               pixel_weights = weights + ( ( x >> ( SCALE_SHIFT - SUBSAMPLE_BITS ) ) & SUBSAMPLE_MASK ) * 4;
+               x_scaled = x >> SCALE_SHIFT;
 
                w1 = pixel_weights[ 0 ];
                w2 = pixel_weights[ 1 ];
                w3 = pixel_weights[ 2 ];
                w4 = pixel_weights[ 3 ];
 
-               y1 = w1 * q0[ 0 ];
-               cb = w1 * q0[ 1 ];
-               y2 = w1 * q0[ 2 ];
-               cr = w1 * q0[ 3 ];
-
-               y1 += w2 * q0[ 4 ];
-               cb += w2 * q0[ 5 ];
-               y2 += w2 * q0[ 6 ];
-               cr += w2 * q0[ 7 ];
-
-               y1 += w3 * q1[ 0 ];
-               cb += w3 * q1[ 1 ];
-               y2 += w3 * q1[ 2 ];
-               cr += w3 * q1[ 3 ];
-
-               y1 += w4 * q1[ 4 ];
-               cb += w4 * q1[ 5 ];
-               y2 += w4 * q1[ 6 ];
-               cr += w4 * q1[ 7 ];
+               /* process Y */
+               q0 = src0 + ( x_scaled << 1 );
+               q1 = src1 + ( x_scaled << 1 );
+               p  = w1 * q0[ 0 ];
+               p += w2 * q0[ 2 ];
+               p += w3 * q1[ 0 ];
+               p += w4 * q1[ 2 ];
+               *dest++ = ( p + 0x8000 ) >> SCALE_SHIFT;
+
+               /* process U/V */
+               x_aligned = ( ( x_scaled >> 1 ) << 2 );
+               uv_index = ( ( dest_x & 1 ) << 1 ) + 1;
+
+               q0 = src0 + x_aligned;
+               q1 = src1 + x_aligned;
+               p  = w1 * q0[ uv_index ];
+               p += w3 * q1[ uv_index ];
+               p += w2 * q0[ uv_index ];
+               p += w4 * q1[ uv_index ];
 
-               dest[ 0 ] = ( y1 + 0x8000 ) >> 16;
-               dest[ 1 ] = ( cb + 0x8000 ) >> 16;
-               dest[ 2 ] = ( y2 + 0x8000 ) >> 16;
-               dest[ 3 ] = ( cr + 0x8000 ) >> 16;
-
-               dest += 4;
                x += x_step;
+               dest_x ++;
+
+               *dest++ = ( p + 0x8000 ) >> SCALE_SHIFT;
        }
 
        return dest;
@@ -260,8 +246,9 @@ process_pixel ( int *weights, int n_x, int n_y,
                 guchar **src, int src_channels,
                 int x_start, int src_width )
 {
-       unsigned int y1 = 0, cb = 0, y2 = 0, cr = 0;
-       int i, j;
+       register unsigned int y = 0, uv = 0;
+       register int i, j;
+       int uv_index = ( ( dest_x & 1 ) << 1 ) + 1;
 
        for ( i = 0; i < n_y; i++ )
        {
@@ -269,29 +256,28 @@ process_pixel ( int *weights, int n_x, int n_y,
 
                for ( j = 0; j < n_x; j++ )
                {
-                       unsigned int ta;
-                       guchar *q;
+                       unsigned int ta = 0xff * line_weights[ j ];
 
                        if ( x_start + j < 0 )
-                               q = src[ i ];
+                       {
+                               y  += ta * src[ i ][ 0 ];
+                               uv += ta * src[ i ][ uv_index ];
+                       }
                        else if ( x_start + j < src_width )
-                               q = src[ i ] + ( x_start + j ) * src_channels;
+                       {
+                               y  += ta * src[ i ][ ( x_start + j ) << 1 ];
+                               uv += ta * src[ i ][ ( ( ( x_start + j ) >> 1 ) << 2) + uv_index ];
+                       }
                        else
-                               q = src[ i ] + ( src_width - 1 ) * src_channels;
-
-                       ta = 0xff * line_weights[ j ];
-
-                       y1 += ta * q[ 0 ];
-                       cb += ta * q[ 1 ];
-                       y2 += ta * q[ 2 ];
-                       cr += ta * q[ 3 ];
+                       {
+                               y  += ta * src[ i ][ ( src_width - 1 ) << 1 ];
+                               uv += ta * src[ i ][ ( ( ( src_width - 1 ) >> 1 ) << 2) + uv_index ];
+                       }
                }
        }
 
-       dest[ 0 ] = ( y1 + 0xffffff ) >> 24;
-       dest[ 1 ] = ( cb + 0xffffff ) >> 24;
-       dest[ 2 ] = ( y2 + 0xffffff ) >> 24;
-       dest[ 3 ] = ( cr + 0xffffff ) >> 24;
+       *dest++ = ( y  + 0xffffff ) >> 24;
+       *dest++ = ( uv + 0xffffff ) >> 24;
 }
 
 
@@ -729,10 +715,10 @@ yuv422_scale ( guchar *dest_buf,
                double scale_y,
                PixopsInterpType interp_type )
 {
-       PixopsFilter filter;
+       PixopsFilter filter = { { 0, 0, 0}, { 0, 0, 0 }, 0 };
        PixopsLineFunc line_func;
 
-#ifdef USE_MMX
+#if defined(USE_MMX) && !defined(ARCH_X86_64)
        gboolean found_mmx = pixops_have_mmx();
 #endif
 
@@ -749,22 +735,24 @@ yuv422_scale ( guchar *dest_buf,
                                       dest_rowstride,
                                       src_buf, src_width, src_height, src_rowstride,
                                       scale_x, scale_y );
-               return ;
+               return;
        }
 
        filter.overall_alpha = 1.0;
        make_weights ( &filter, interp_type, scale_x, scale_y );
 
-fprintf( stderr, "RESCALE: %d %d\n", filter.x.n, filter.y.n );
        if ( filter.x.n == 2 && filter.y.n == 2 )
        {
-#ifdef USE_MMX
-               if ( 0 && found_mmx )
-                       line_func = scale_line_22_33_mmx_stub;
+#if defined(USE_MMX) && !defined(ARCH_X86_64)
+               if ( found_mmx )
+               {
+                       //fprintf( stderr, "rescale: using mmx\n" );
+                       line_func = scale_line_22_yuv_mmx_stub;
+               }
                else
 #endif
 
-                       line_func = scale_line_22_33;
+                       line_func = scale_line_22_yuv;
        }
        else
                line_func = scale_line;