]> git.sesse.net Git - mlt/blobdiff - src/modules/core/transition_composite.c
Feed rework and fixes to westley and composite
[mlt] / src / modules / core / transition_composite.c
index 3c4557ed356e486e9f002b159e3a7d74f36b18fa..fafe387f568380f43344bce9205f4101d32519d6 100644 (file)
@@ -40,6 +40,7 @@ typedef void ( *composite_line_fn )( uint8_t *dest, uint8_t *src, int width_src,
 
 struct geometry_s
 {
+       int frame;
        float position;
        float mix;
        int nw; // normalised width
@@ -154,23 +155,31 @@ static void geometry_calculate( struct geometry_s *output, struct geometry_s *in
        position = ( position - in->position ) / ( out->position - in->position );
 
        // Calculate this frames geometry
-       output->nw = in->nw;
-       output->nh = in->nh;
-       output->x = in->x + ( out->x - in->x ) * position;
-       output->y = in->y + ( out->y - in->y ) * position;
-       output->w = in->w + ( out->w - in->w ) * position;
-       output->h = in->h + ( out->h - in->h ) * position;
-       output->mix = in->mix + ( out->mix - in->mix ) * position;
-       output->distort = in->distort;
-
-       // DRD> These break on negative values. I do not think they are needed
-       // since yuv_composite takes care of YUYV group alignment
-       //output->x = ( int )floor( output->x ) & 0xfffffffe;
-       //output->w = ( int )floor( output->w ) & 0xfffffffe;
-       //output->sw &= 0xfffffffe;
+       if ( in->frame != out->frame - 1 )
+       {
+               output->nw = in->nw;
+               output->nh = in->nh;
+               output->x = rint( in->x + ( out->x - in->x ) * position + 0.5 );
+               output->y = rint( in->y + ( out->y - in->y ) * position + 0.5 );
+               output->w = rint( in->w + ( out->w - in->w ) * position + 0.5 );
+               output->h = rint( in->h + ( out->h - in->h ) * position + 0.5 );
+               output->mix = in->mix + ( out->mix - in->mix ) * position;
+               output->distort = in->distort;
+       }
+       else
+       {
+               output->nw = out->nw;
+               output->nh = out->nh;
+               output->x = out->x;
+               output->y = out->y;
+               output->w = out->w;
+               output->h = out->h;
+               output->mix = out->mix;
+               output->distort = out->distort;
+       }
 }
 
-void transition_destroy_keys( void *arg )
+static void transition_destroy_keys( void *arg )
 {
        struct geometry_s *ptr = arg;
        struct geometry_s *next = NULL;
@@ -189,7 +198,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 );
@@ -239,7 +248,8 @@ static struct geometry_s *transition_parse_keys( mlt_transition this,  int norma
                                // Parse and add to the list
                                geometry_parse( temp, ptr, value, normalised_width, normalised_height );
 
-                               // Assign the position
+                               // Assign the position and frame
+                               temp->frame = frame;
                                temp->position = position;
 
                                // Allow the next to be appended after this one
@@ -259,9 +269,6 @@ static struct geometry_s *transition_parse_keys( mlt_transition this,  int norma
        else
                end->position = 1;
 
-       // Assign to properties to ensure we get destroyed
-       mlt_properties_set_data( properties, "geometries", start, 0, transition_destroy_keys, NULL );
-
        return start;
 }
 
@@ -315,8 +322,8 @@ static inline float delta_calculate( mlt_transition this, mlt_frame frame )
        mlt_position out = mlt_transition_get_out( this );
 
        // Get the position of the frame
-       char *name = mlt_properties_get( mlt_transition_properties( this ), "_unique_id" );
-       mlt_position position = mlt_properties_get_position( mlt_frame_properties( frame ), name );
+       char *name = mlt_properties_get( MLT_TRANSITION_PROPERTIES( this ), "_unique_id" );
+       mlt_position position = mlt_properties_get_position( MLT_FRAME_PROPERTIES( frame ), name );
 
        // Now do the calcs
        float x = ( float )( position - in ) / ( float )( out - in + 1 );
@@ -531,10 +538,7 @@ static int composite_yuv( uint8_t *p_dest, int width_dest, int height_dest, uint
        // Adjust to consumer scale
        int x = geometry.x * width_dest / geometry.nw;
        int y = geometry.y * height_dest / geometry.nh;
-
-       // Align x to a full YUYV group
-       x &= 0xfffffffe;
-       width_src &= 0xfffffffe;
+       int uneven = ( x & 1 );
 
        // optimization points - no work to do
        if ( width_src <= 0 || height_src <= 0 )
@@ -552,7 +556,7 @@ static int composite_yuv( uint8_t *p_dest, int width_dest, int height_dest, uint
        }
        
        // crop overlay beyond right edge of frame
-       else if ( x + width_src > width_dest )
+       if ( x + width_src > width_dest )
                width_src = width_dest - x;
 
        // crop overlay off the top edge of the frame
@@ -560,9 +564,11 @@ static int composite_yuv( uint8_t *p_dest, int width_dest, int height_dest, uint
        {
                y_src = -y;
                height_src -= y_src;
+               y = 0;
        }
+       
        // crop overlay below bottom edge of frame
-       else if ( y + height_src > height_dest )
+       if ( y + height_src > height_dest )
                height_src = height_dest - y;
 
        // offset pointer into overlay buffer based on cropping
@@ -605,20 +611,40 @@ static int composite_yuv( uint8_t *p_dest, int width_dest, int height_dest, uint
        stride_dest *= step;
        int alpha_stride = stride_src / bpp;
 
-       if ( line_fn == NULL )
-               line_fn = composite_line_yuv;
+       if ( uneven )
+       {
+               p_dest += 2;
+               width_src --;
+       }
 
        // now do the compositing only to cropped extents
-       for ( i = 0; i < height_src; i += step )
+       if ( line_fn != NULL )
        {
-               line_fn( p_dest, p_src, width_src, p_alpha, weight, p_luma, softness );
-
-               p_src += stride_src;
-               p_dest += stride_dest;
-               if ( p_alpha )
-                       p_alpha += alpha_stride;
-               if ( p_luma )
-                       p_luma += alpha_stride;
+               for ( i = 0; i < height_src; i += step )
+               {
+                       line_fn( p_dest, p_src, width_src, p_alpha, weight, p_luma, softness );
+       
+                       p_src += stride_src;
+                       p_dest += stride_dest;
+                       if ( p_alpha )
+                               p_alpha += alpha_stride;
+                       if ( p_luma )
+                               p_luma += alpha_stride;
+               }
+       }
+       else
+       {
+               for ( i = 0; i < height_src; i += step )
+               {
+                       composite_line_yuv( p_dest, p_src, width_src, p_alpha, weight, p_luma, softness );
+       
+                       p_src += stride_src;
+                       p_dest += stride_dest;
+                       if ( p_alpha )
+                               p_alpha += alpha_stride;
+                       if ( p_luma )
+                               p_luma += alpha_stride;
+               }
        }
 
        return ret;
@@ -700,7 +726,7 @@ static uint16_t* get_luma( mlt_properties properties, int width, int height )
                                if ( producer != NULL )
                                {
                                        // Get the producer properties
-                                       mlt_properties producer_properties = mlt_producer_properties( producer );
+                                       mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
        
                                        // Ensure that we loop
                                        mlt_properties_set( producer_properties, "eof", "loop" );
@@ -712,13 +738,13 @@ static uint16_t* get_luma( mlt_properties properties, int width, int height )
                                        mlt_frame luma_frame = NULL;
        
                                        // Get the luma frame
-                                       if ( mlt_service_get_frame( mlt_producer_service( producer ), &luma_frame, 0 ) == 0 )
+                                       if ( mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &luma_frame, 0 ) == 0 )
                                        {
                                                uint8_t *luma_image;
                                                mlt_image_format luma_format = mlt_image_yuv422;
        
                                                // Get image from the luma producer
-                                               mlt_properties_set( mlt_frame_properties( luma_frame ), "rescale.interp", "none" );
+                                               mlt_properties_set( MLT_FRAME_PROPERTIES( luma_frame ), "rescale.interp", "none" );
                                                mlt_frame_get_image( luma_frame, &luma_image, &luma_format, &luma_width, &luma_height, 0 );
        
                                                // Generate the luma map
@@ -760,10 +786,10 @@ static int get_b_frame_image( mlt_transition this, mlt_frame b_frame, uint8_t **
        mlt_image_format format = mlt_image_yuv422;
 
        // Get the properties objects
-       mlt_properties b_props = mlt_frame_properties( b_frame );
-       mlt_properties properties = mlt_transition_properties( this );
+       mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
+       mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
 
-       if ( mlt_properties_get( properties, "distort" ) == NULL && geometry->distort == 0 )
+       if ( mlt_properties_get( properties, "distort" ) == NULL && mlt_properties_get( b_props, "distort" ) == NULL && geometry->distort == 0 )
        {
                // Adjust b_frame pixel aspect
                int normalised_width = geometry->w;
@@ -809,7 +835,8 @@ static int get_b_frame_image( mlt_transition this, mlt_frame b_frame, uint8_t **
        mlt_properties_set( b_props, "distort", "true" );
 
        // Take into consideration alignment for optimisation
-       alignment_calculate( geometry );
+       if ( !mlt_properties_get_int( properties, "titles" ) )
+               alignment_calculate( geometry );
 
        // Adjust to consumer scale
        int x = geometry->x * *width / geometry->nw;
@@ -817,7 +844,7 @@ static int get_b_frame_image( mlt_transition this, mlt_frame b_frame, uint8_t **
        *width = geometry->sw * *width / geometry->nw;
        *height = geometry->sh * *height / geometry->nh;
 
-       x &= 0xfffffffe;
+       //x = ( x | 1 ) ^ 1;
 
        // optimization points - no work to do
        if ( *width < 1 || *height < 1 )
@@ -832,26 +859,30 @@ static int get_b_frame_image( mlt_transition this, mlt_frame b_frame, uint8_t **
 }
 
 
-struct geometry_s *composite_calculate( struct geometry_s *result, mlt_transition this, mlt_frame a_frame, float position )
+static 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 );
 
+       // Obtain the normalised width and height from the a_frame
+       int normalised_width = mlt_properties_get_int( a_props, "normalised_width" );
+       int normalised_height = mlt_properties_get_int( a_props, "normalised_height" );
+
        // Now parse the geometries
-       if ( start == NULL )
+       if ( start == NULL || mlt_properties_get_int( properties, "refresh" ) || start->nw != normalised_width || start->nh != normalised_height )
        {
-               // Obtain the normalised width and height from the a_frame
-               int normalised_width = mlt_properties_get_int( a_props, "normalised_width" );
-               int normalised_height = mlt_properties_get_int( a_props, "normalised_height" );
-
                // Parse the transitions properties
                start = transition_parse_keys( this, normalised_width, normalised_height );
+
+               // Assign to properties to ensure we get destroyed
+               mlt_properties_set_data( properties, "geometries", start, 0, transition_destroy_keys, NULL );
+               mlt_properties_set_int( properties, "refresh", 0 );
        }
 
        // Do the calculation
@@ -864,16 +895,26 @@ struct geometry_s *composite_calculate( struct geometry_s *result, mlt_transitio
        return start;
 }
 
+static inline void inline_memcpy( uint8_t *dest, uint8_t *src, int length )
+{
+       uint8_t *end = src + length;
+       while ( src < end )
+       {
+               *dest ++ = *src ++;
+               *dest ++ = *src ++;
+       }
+}
+
 mlt_frame composite_copy_region( mlt_transition this, mlt_frame a_frame, mlt_position frame_position )
 {
        // Create a frame to return
        mlt_frame b_frame = mlt_frame_init( );
 
        // 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 );
 
        // Get the position
        float position = position_calculate( this, frame_position );
@@ -909,8 +950,17 @@ mlt_frame composite_copy_region( mlt_transition this, mlt_frame a_frame, mlt_pos
        w = result.w * width / result.nw;
        h = result.h * height / result.nh;
 
-       x &= 0xfffffffe;
-       //w &= 0xfffffffe;
+       if ( y < 0 )
+       {
+               h = h + y;
+               y = 0;
+       }
+
+       if ( y + h > height )
+               h = height - y;
+
+       x = ( x | 1 ) ^ 1;
+       w = ( w | 1 ) ^ 1;
 
        // Now we need to create a new destination image
        dest = mlt_pool_alloc( w * h * 2 );
@@ -922,7 +972,7 @@ mlt_frame composite_copy_region( mlt_transition this, mlt_frame a_frame, mlt_pos
 
        while ( q < r )
        {
-               memcpy( q, p, w * 2 );
+               inline_memcpy( q, p, w * 2 );
                q += w * 2;
                p += width * 2;
        }
@@ -934,6 +984,7 @@ mlt_frame composite_copy_region( mlt_transition this, mlt_frame a_frame, mlt_pos
 
        // Assign this position to the b frame
        mlt_frame_set_position( b_frame, frame_position );
+       mlt_properties_set( b_props, "distort", "true" );
 
        // Return the frame
        return b_frame;
@@ -956,16 +1007,16 @@ static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_f
        // Get the image from the a frame
        mlt_frame_get_image( a_frame, image, format, width, height, 1 );
 
+       // Get the properties from the transition
+       mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
+
        if ( b_frame != NULL )
        {
                // 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 );
-
-               // Get the properties from the transition
-               mlt_properties properties = mlt_transition_properties( this );
+               mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
 
                // Structures for geometry
                struct geometry_s result;
@@ -976,33 +1027,61 @@ static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_f
 
                // Do the calculation
                struct geometry_s *start = composite_calculate( &result, this, a_frame, position );
-               
+
+               // Get the image from the b frame
+               uint8_t *image_b = NULL;
+               int width_b = *width;
+               int height_b = *height;
+       
                // Optimisation - no compositing required
                if ( result.mix == 0 || ( result.w == 0 && result.h == 0 ) )
                        return 0;
 
+               // Need to keep the width/height of the a_frame on the b_frame for titling
+               if ( mlt_properties_get( a_props, "dest_width" ) == NULL )
+               {
+                       mlt_properties_set_int( a_props, "dest_width", *width );
+                       mlt_properties_set_int( a_props, "dest_height", *height );
+                       mlt_properties_set_int( b_props, "dest_width", *width );
+                       mlt_properties_set_int( b_props, "dest_height", *height );
+               }
+               else
+               {
+                       mlt_properties_set_int( b_props, "dest_width", mlt_properties_get_int( a_props, "dest_width" ) );
+                       mlt_properties_set_int( b_props, "dest_height", mlt_properties_get_int( a_props, "dest_height" ) );
+               }
+
                // Since we are the consumer of the b_frame, we must pass along these
                // consumer properties from the a_frame
+               mlt_properties_set_double( b_props, "consumer_deinterlace", mlt_properties_get_double( a_props, "consumer_deinterlace" ) );
                mlt_properties_set_double( b_props, "consumer_aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
+               mlt_properties_set_int( b_props, "normalised_width", mlt_properties_get_double( a_props, "normalised_width" ) );
+               mlt_properties_set_int( b_props, "normalised_height", mlt_properties_get_double( a_props, "normalised_height" ) );
+
+               // Special case for titling...
+               if ( mlt_properties_get_int( properties, "titles" ) )
+               {
+                       if ( mlt_properties_get( b_props, "rescale.interp" ) == NULL )
+                               mlt_properties_set( b_props, "rescale.interp", "hyper" );
+                       mlt_properties_set( properties, "fill", NULL );
+                       width_b = mlt_properties_get_int( a_props, "dest_width" );
+                       height_b = mlt_properties_get_int( a_props, "dest_height" );
+               }
 
-               // Get the image from the b frame
-               uint8_t *image_b = NULL;
-               int width_b = *width;
-               int height_b = *height;
-               
                if ( get_b_frame_image( this, b_frame, &image_b, &width_b, &height_b, &result ) == 0 )
                {
                        uint8_t *dest = *image;
                        uint8_t *src = image_b;
                        uint8_t *alpha = mlt_frame_get_alpha_mask( b_frame );
-                       int progressive = mlt_properties_get_int( a_props, "progressive" ) ||
-                                       mlt_properties_get_int( a_props, "consumer_progressive" ) ||
+                       int progressive = 
+                                       mlt_properties_get_int( a_props, "consumer_deinterlace" ) ||
                                        mlt_properties_get_int( properties, "progressive" );
                        int field;
                        
                        int32_t luma_softness = mlt_properties_get_double( properties, "softness" ) * ( 1 << 16 );
                        uint16_t *luma_bitmap = get_luma( properties, width_b, height_b );
-                       composite_line_fn line_fn = mlt_properties_get_int( properties, "_MMX" ) ? composite_line_yuv_mmx : composite_line_yuv;
+                       //composite_line_fn line_fn = mlt_properties_get_int( properties, "_MMX" ) ? composite_line_yuv_mmx : NULL;
+                       composite_line_fn line_fn = NULL;
 
                        for ( field = 0; field < ( progressive ? 1 : 2 ); field++ )
                        {
@@ -1012,6 +1091,14 @@ static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_f
                                // Do the calculation if we need to
                                geometry_calculate( &result, start, field_position );
 
+                               if ( mlt_properties_get_int( properties, "titles" ) )
+                               {
+                                       result.nw = result.w = *width;
+                                       result.nh = result.h = *height;
+                                       result.sw = width_b;
+                                       result.sh = height_b;
+                               }
+
                                // Align
                                alignment_calculate( &result );
 
@@ -1030,13 +1117,13 @@ static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_f
 static mlt_frame composite_process( mlt_transition this, 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( this ), "_unique_id" );
+       char *name = mlt_properties_get( MLT_TRANSITION_PROPERTIES( this ), "_unique_id" );
 
        // Assign the current position to the name
-       mlt_properties_set_position( mlt_frame_properties( a_frame ), name, mlt_frame_get_position( a_frame ) );
+       mlt_properties_set_position( MLT_FRAME_PROPERTIES( a_frame ), name, mlt_frame_get_position( a_frame ) );
 
        // Propogate the transition properties to the b frame
-       mlt_properties_set_double( mlt_frame_properties( b_frame ), "relative_position", position_calculate( this, mlt_frame_get_position( a_frame ) ) );
+       mlt_properties_set_double( MLT_FRAME_PROPERTIES( b_frame ), "relative_position", position_calculate( this, mlt_frame_get_position( a_frame ) ) );
        
        mlt_frame_push_service( a_frame, this );
        mlt_frame_push_frame( a_frame, b_frame );
@@ -1052,7 +1139,7 @@ mlt_transition transition_composite_init( char *arg )
        mlt_transition this = calloc( sizeof( struct mlt_transition_s ), 1 );
        if ( this != NULL && mlt_transition_init( this, NULL ) == 0 )
        {
-               mlt_properties properties = mlt_transition_properties( this );
+               mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
                
                this->process = composite_process;