]> git.sesse.net Git - mlt/blobdiff - src/modules/plus/transition_affine.c
Alphas and global feeds revisted
[mlt] / src / modules / plus / transition_affine.c
index bd399e733f8fd428e470bfe2e2db3a2340c6c597..4a8fe0916ab007141dc91606cd67bf106c479dab 100644 (file)
@@ -177,7 +177,7 @@ static struct geometry_s *transition_parse_keys( mlt_transition this,  int norma
        int i = 0;
 
        // Get the properties of the transition
-       mlt_properties properties = mlt_transition_properties( this );
+       mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
 
        // Get the in and out position
        mlt_position in = mlt_transition_get_in( this );
@@ -257,10 +257,10 @@ static struct geometry_s *transition_parse_keys( mlt_transition this,  int norma
 struct geometry_s *composite_calculate( struct geometry_s *result, mlt_transition this, mlt_frame a_frame, float position )
 {
        // Get the properties from the transition
-       mlt_properties properties = mlt_transition_properties( this );
+       mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
 
        // Get the properties from the frame
-       mlt_properties a_props = mlt_frame_properties( a_frame );
+       mlt_properties a_props = MLT_FRAME_PROPERTIES( a_frame );
        
        // Structures for geometry
        struct geometry_s *start = mlt_properties_get_data( properties, "geometries", NULL );
@@ -407,18 +407,18 @@ static void affine_offset( float this[3][3], int x, int y )
 }
 
 // Obtain the mapped x coordinate of the input
-static inline int MapX( float this[3][3], int x, int y )
+static inline double MapX( float this[3][3], int x, int y )
 {
        return this[0][0] * x + this[0][1] * y + this[0][2];
 }
 
 // Obtain the mapped y coordinate of the input
-static inline int MapY( float this[3][3], int x, int y )
+static inline double MapY( float this[3][3], int x, int y )
 {
        return this[1][0] * x + this[1][1] * y + this[1][2];
 }
 
-static inline float MapZ( float this[3][3], int x, int y )
+static inline double MapZ( float this[3][3], int x, int y )
 {
        return this[2][0] * x + this[2][1] * y + this[2][2];
 }
@@ -426,16 +426,16 @@ static inline float MapZ( float this[3][3], int x, int y )
 #define MAX( x, y ) x > y ? x : y
 #define MIN( x, y ) x < y ? x : y
 
-static void affine_max_output( float this[3][3], float *w, float *h )
+static void affine_max_output( float this[3][3], float *w, float *h, float dz )
 {
-       int tlx = MapX( this, -720, 576 );
-       int tly = MapY( this, -720, 576 );
-       int trx = MapX( this, 720, 576 );
-       int try = MapY( this, 720, 576 );
-       int blx = MapX( this, -720, -576 );
-       int bly = MapY( this, -720, -576 );
-       int brx = MapX( this, 720, -576 );
-       int bry = MapY( this, 720, -576 );
+       int tlx = MapX( this, -720, 576 ) / dz;
+       int tly = MapY( this, -720, 576 ) / dz;
+       int trx = MapX( this, 720, 576 ) / dz;
+       int try = MapY( this, 720, 576 ) / dz;
+       int blx = MapX( this, -720, -576 ) / dz;
+       int bly = MapY( this, -720, -576 ) / dz;
+       int brx = MapX( this, 720, -576 ) / dz;
+       int bry = MapY( this, 720, -576 ) / dz;
 
        int max_x;
        int max_y;
@@ -476,13 +476,13 @@ static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_f
        mlt_transition this = mlt_frame_pop_service( a_frame );
 
        // Get the properties of the transition
-       mlt_properties properties = mlt_transition_properties( this );
+       mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
 
        // Get the properties of the a frame
-       mlt_properties a_props = mlt_frame_properties( a_frame );
+       mlt_properties a_props = MLT_FRAME_PROPERTIES( a_frame );
 
        // Get the properties of the b frame
-       mlt_properties b_props = mlt_frame_properties( b_frame );
+       mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
 
        // Image, format, width, height and image for the b frame
        uint8_t *b_image = NULL;
@@ -497,11 +497,16 @@ static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_f
        mlt_position position =  mlt_properties_get_position( a_props, name );
        mlt_position in = mlt_properties_get_position( properties, "in" );
        mlt_position out = mlt_properties_get_position( properties, "out" );
+       int mirror = mlt_properties_get_position( properties, "mirror" );
+       int length = out - in + 1;
 
        // Structures for geometry
        struct geometry_s *start = mlt_properties_get_data( properties, "geometries", NULL );
        struct geometry_s result;
 
+       if ( mirror && position > length / 2 )
+               position = abs( position - length );
+
        // Now parse the geometries
        if ( start == NULL )
        {
@@ -513,11 +518,14 @@ static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_f
                start = transition_parse_keys( this, normalised_width, normalised_height );
        }
 
+       if ( *height & 1 )
+               *height ++;
+
        // Fetch the a frame image
        mlt_frame_get_image( a_frame, image, format, width, height, 1 );
 
        // Calculate the region now
-       composite_calculate( &result, this, a_frame, ( float )position / ( out - in + 1 ) );
+       composite_calculate( &result, this, a_frame, ( float )position / length );
 
        // Fetch the b frame image
        result.w = ( int )( result.w * *width / result.nw );
@@ -540,7 +548,7 @@ static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_f
                mlt_properties_set_double( b_props, "consumer_aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
        }
 
-       mlt_properties_set( b_props, "distort", mlt_properties_get( properties, "distort" ) );
+       mlt_properties_set_int( b_props, "distort", mlt_properties_get_int( properties, "distort" ) );
        mlt_frame_get_image( b_frame, &b_image, &b_format, &b_width, &b_height, 0 );
        result.w = b_width;
        result.h = b_height;
@@ -550,15 +558,21 @@ static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_f
        {
                register int x, y;
                register int dx, dy;
+               double dz;
                float sw, sh;
 
                // Get values from the transition
-               float rotate_x = mlt_properties_get_double( properties, "rotate" );
+               float fix_rotate_x = mlt_properties_get_double( properties, "fix_rotate_x" );
+               float fix_rotate_y = mlt_properties_get_double( properties, "fix_rotate_y" );
+               float fix_rotate_z = mlt_properties_get_double( properties, "fix_rotate_z" );
+               float rotate_x = mlt_properties_get_double( properties, "rotate_x" );
                float rotate_y = mlt_properties_get_double( properties, "rotate_y" );
                float rotate_z = mlt_properties_get_double( properties, "rotate_z" );
                float fix_shear_x = mlt_properties_get_double( properties, "fix_shear_x" );
                float fix_shear_y = mlt_properties_get_double( properties, "fix_shear_y" );
                float fix_shear_z = mlt_properties_get_double( properties, "fix_shear_z" );
+               float scale_x = mlt_properties_get_double( properties, "scale_x" );
+               float scale_y = mlt_properties_get_double( properties, "scale_y" );
                float shear_x = mlt_properties_get_double( properties, "shear_x" );
                float shear_y = mlt_properties_get_double( properties, "shear_y" );
                float shear_z = mlt_properties_get_double( properties, "shear_z" );
@@ -583,63 +597,90 @@ static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_f
                int y_offset = ( int )result.h >> 1;
 
                uint8_t *alpha = mlt_frame_get_alpha_mask( b_frame );
+               uint8_t *mask = mlt_frame_get_alpha_mask( a_frame );
+               uint8_t *pmask = mask;
                float mix;
 
                affine_t affine;
                affine_init( affine.matrix );
-               affine_rotate( affine.matrix, rotate_x * ( position - in ) );
-               affine_rotate_y( affine.matrix, rotate_y * ( position - in ) );
-               affine_rotate_z( affine.matrix, rotate_z * ( position - in ) );
+               affine_rotate( affine.matrix, fix_rotate_x + rotate_x * position );
+               affine_rotate_y( affine.matrix, fix_rotate_y + rotate_y * position );
+               affine_rotate_z( affine.matrix, fix_rotate_z + rotate_z * position );
                affine_shear( affine.matrix, 
-                                         fix_shear_x + shear_x * ( position - in )
-                                         fix_shear_y + shear_y * ( position - in ),
-                                         fix_shear_z + shear_z * ( position - in ) );
+                                         fix_shear_x + shear_x * position
+                                         fix_shear_y + shear_y * position,
+                                         fix_shear_z + shear_z * position );
                affine_offset( affine.matrix, ox, oy );
 
-               if ( scale )
-               {
-                       affine_max_output( affine.matrix, &sw, &sh );
-                       affine_scale( affine.matrix, sw, sh );
-               }
-       
                lower_x -= ( lower_x & 1 );
                upper_x -= ( upper_x & 1 );
 
                q = *image;
 
+               dz = MapZ( affine.matrix, 0, 0 );
+
+               if ( mask == NULL )
+               {
+                       mask = mlt_pool_alloc( *width * *height );
+                       pmask = mask;
+                       memset( mask, 255, *width * *height );
+               }
+
+               if ( ( int )abs( dz * 1000 ) < 25 )
+                       goto getout;
+
+               if ( scale )
+               {
+                       affine_max_output( affine.matrix, &sw, &sh, dz );
+                       affine_scale( affine.matrix, 1.0 / sw, 1.0 / sh );
+               }
+               else if ( scale_x != 0 && scale_y != 0 )
+               {
+                       affine_scale( affine.matrix, scale_x, scale_y );
+               }
+
                for ( y = lower_y; y < upper_y; y ++ )
                {
                        p = q;
 
                        for ( x = lower_x; x < upper_x; x ++ )
                        {
-                               dx = MapX( affine.matrix, x, y ) + x_offset;
-                               dy = MapY( affine.matrix, x, y ) + y_offset;
+                               dx = MapX( affine.matrix, x, y ) / dz + x_offset;
+                               dy = MapY( affine.matrix, x, y ) / dz + y_offset;
 
                                if ( dx >= 0 && dx < b_width && dy >=0 && dy < b_height )
                                {
                                        if ( alpha == NULL )
                                        {
+                                               *pmask ++;
                                                dx += dx & 1;
                                                *p ++ = *( b_image + dy * b_stride + ( dx << 1 ) );
                                                *p ++ = *( b_image + dy * b_stride + ( dx << 1 ) + ( ( x & 1 ) << 1 ) + 1 );
                                        }
                                        else
                                        {
+                                               *pmask ++ = *( alpha + dy * b_width + dx );
                                                mix = ( float )*( alpha + dy * b_width + dx ) / 255.0;
                                                dx += dx & 1;
-                                               *p ++ = *p * ( 1 - mix ) + mix * *( b_image + dy * b_stride + ( dx << 1 ) );
-                                               *p ++ = *p * ( 1 - mix ) + mix * *( b_image + dy * b_stride + ( dx << 1 ) + ( ( x & 1 ) << 1 ) + 1 );
+                                               *p = *p * ( 1 - mix ) + mix * *( b_image + dy * b_stride + ( dx << 1 ) );
+                                               p ++;
+                                               *p = *p * ( 1 - mix ) + mix * *( b_image + dy * b_stride + ( dx << 1 ) + ( ( x & 1 ) << 1 ) + 1 );
+                                               p ++;
                                        }
                                }
                                else
                                {
                                        p += 2;
+                                       pmask ++;
                                }
                        }
 
                        q += a_stride;
                }
+
+getout:
+               a_frame->get_alpha_mask = NULL;
+               mlt_properties_set_data( a_props, "alpha", mask, 0, mlt_pool_release, NULL );
        }
 
        return 0;
@@ -651,10 +692,10 @@ static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_f
 static mlt_frame transition_process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
 {
        // Get a unique name to store the frame position
-       char *name = mlt_properties_get( mlt_transition_properties( transition ), "_unique_id" );
+       char *name = mlt_properties_get( MLT_TRANSITION_PROPERTIES( transition ), "_unique_id" );
 
        // Assign the current position to the name
-       mlt_properties a_props = mlt_frame_properties( a_frame );
+       mlt_properties a_props = MLT_FRAME_PROPERTIES( a_frame );
        mlt_properties_set_position( a_props, name, mlt_frame_get_position( a_frame ) );
 
        // Push the transition on to the frame
@@ -666,7 +707,6 @@ static mlt_frame transition_process( mlt_transition transition, mlt_frame a_fram
        // Push the transition method
        mlt_frame_push_get_image( a_frame, transition_get_image );
 
-
        return a_frame;
 }
 
@@ -678,10 +718,12 @@ mlt_transition transition_affine_init( char *arg )
        mlt_transition transition = mlt_transition_new( );
        if ( transition != NULL )
        {
-               mlt_properties_set_int( mlt_transition_properties( transition ), "sx", 1 );
-               mlt_properties_set_int( mlt_transition_properties( transition ), "sy", 1 );
-               mlt_properties_set( mlt_transition_properties( transition ), "distort", NULL );
-               mlt_properties_set( mlt_transition_properties( transition ), "start", "0,0:100%x100%" );
+               mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( transition ), "sx", 1 );
+               mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( transition ), "sy", 1 );
+               mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( transition ), "distort", 0 );
+               mlt_properties_set( MLT_TRANSITION_PROPERTIES( transition ), "start", "0,0:100%x100%" );
+               // Inform apps and framework that this is a video only transition
+               mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( transition ), "_transition_type", 1 );
                transition->process = transition_process;
        }
        return transition;