]> git.sesse.net Git - mlt/blobdiff - src/modules/core/transition_composite.c
Mutable watermark producer and small optimisation
[mlt] / src / modules / core / transition_composite.c
index 1cfed9be2dd7ac5559ee4217caa971e1264d38f5..622ae8fe85546443763735e77f38a015be600b07 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,14 +155,28 @@ 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;
+       if ( in->frame != out->frame - 1 )
+       {
+               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;
+       }
+       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;
+       }
 
        // DRD> These break on negative values. I do not think they are needed
        // since yuv_composite takes care of YUYV group alignment
@@ -239,7 +254,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 +275,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;
 }
 
@@ -552,7 +565,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
@@ -561,8 +574,9 @@ static int composite_yuv( uint8_t *p_dest, int width_dest, int height_dest, uint
                y_src = -y;
                height_src -= y_src;
        }
+       
        // 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 +619,34 @@ 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;
-
        // 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;
@@ -817,7 +845,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 -= x % 2;
+       x &= 0xfffffffe;
 
        // optimization points - no work to do
        if ( *width < 1 || *height < 1 )
@@ -832,7 +860,7 @@ 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 );
@@ -844,7 +872,7 @@ struct geometry_s *composite_calculate( struct geometry_s *result, mlt_transitio
        struct geometry_s *start = mlt_properties_get_data( properties, "geometries", NULL );
 
        // Now parse the geometries
-       if ( start == NULL )
+       if ( start == NULL || mlt_properties_get_int( properties, "refresh" ) )
        {
                // Obtain the normalised width and height from the a_frame
                int normalised_width = mlt_properties_get_int( a_props, "normalised_width" );
@@ -852,6 +880,10 @@ struct geometry_s *composite_calculate( struct geometry_s *result, mlt_transitio
 
                // 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,6 +896,16 @@ 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
@@ -909,8 +951,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;
 
+       if ( y < 0 )
+       {
+               h = h + y;
+               y = 0;
+       }
+
+       if ( y + h > height )
+               h = height - y;
+
        x &= 0xfffffffe;
-       //w &= 0xfffffffe;
+       w &= 0xfffffffe;
 
        // Now we need to create a new destination image
        dest = mlt_pool_alloc( w * h * 2 );
@@ -922,7 +973,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;
        }
@@ -956,6 +1007,9 @@ 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
@@ -964,9 +1018,6 @@ static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_f
                // 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 );
-
                // Structures for geometry
                struct geometry_s result;
 
@@ -995,14 +1046,15 @@ static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_f
                        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" ) ||
+                       int progressive = 
                                        mlt_properties_get_int( a_props, "consumer_progressive" ) ||
                                        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++ )
                        {