]> git.sesse.net Git - mlt/blobdiff - src/modules/kdenlive/producer_framebuffer.c
make mlt_position type double
[mlt] / src / modules / kdenlive / producer_framebuffer.c
index a96ca3a791679fa08a94695f8cfd8c83c61edc8d..e4780721ec99688275c9d3e972e2a0bb01d039a3 100644 (file)
 #include <assert.h>
 
 // Forward references.
-static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index );
+static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
 
 /** Image stack(able) method
 */
 
-static int framebuffer_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
+static int framebuffer_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
 {
 
        // Get the filter object and properties
-       mlt_producer producer = mlt_frame_pop_service( this );
-       int index = ( int )mlt_frame_pop_service( this );
+       mlt_producer producer = mlt_frame_pop_service( frame );
+       int index = ( int )mlt_frame_pop_service( frame );
        mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
 
+       mlt_service_lock( MLT_PRODUCER_SERVICE( producer ) );
+
        // Frame properties objects
-       mlt_properties frame_properties = MLT_FRAME_PROPERTIES( this );
+       mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
        mlt_frame first_frame = mlt_properties_get_data( properties, "first_frame", NULL );
 
        // Get producer parameters
@@ -50,6 +52,7 @@ static int framebuffer_get_image( mlt_frame this, uint8_t **image, mlt_image_for
        int freeze = mlt_properties_get_int( properties, "freeze" );
        int freeze_after = mlt_properties_get_int( properties, "freeze_after" );
        int freeze_before = mlt_properties_get_int( properties, "freeze_before" );
+       int in = mlt_properties_get_position( properties, "in" );
 
        // Determine the position
        mlt_position first_position = (first_frame != NULL) ? mlt_frame_get_position( first_frame ) : -1;
@@ -58,7 +61,7 @@ static int framebuffer_get_image( mlt_frame this, uint8_t **image, mlt_image_for
        if ( !freeze || freeze_after || freeze_before )
        {
                double prod_speed = mlt_properties_get_double( properties, "_speed" );
-               double actual_position = prod_speed * (double) mlt_producer_position( producer );
+               double actual_position = in + prod_speed * (double) mlt_producer_position( producer );
 
                if ( mlt_properties_get_int( properties, "reverse" ) )
                        actual_position = mlt_producer_get_playtime( producer ) - actual_position;
@@ -71,7 +74,7 @@ static int framebuffer_get_image( mlt_frame this, uint8_t **image, mlt_image_for
                {
                        // Strobe effect wanted, calculate frame position
                        need_first = floor( actual_position );
-                       need_first -= need_first % strobe;
+                       need_first -= MLT_POSITION_MOD(need_first, strobe);
                }
                if ( freeze )
                {
@@ -79,12 +82,51 @@ static int framebuffer_get_image( mlt_frame this, uint8_t **image, mlt_image_for
                        else if ( freeze_before && need_first < freeze ) need_first = freeze;
                }
        }
+       
+       // Determine output buffer size
+       *width = mlt_properties_get_int( frame_properties, "width" );
+       *height = mlt_properties_get_int( frame_properties, "height" );
+       int size = mlt_image_format_size( *format, *width, *height, NULL );
+
+       // Get output buffer
+       int buffersize = 0;
+        int alphasize = *width * *height;
+       uint8_t *output = mlt_properties_get_data( properties, "output_buffer", &buffersize );
+        uint8_t *output_alpha = mlt_properties_get_data( properties, "output_alpha", NULL );
+       if( buffersize == 0 || buffersize != size )
+       {
+               // invalidate cached frame
+               first_position = -1;
+       }
 
        if ( need_first != first_position )
        {
+               // invalidate cached frame
+               first_position = -1;
+               
                // Bust the cached frame
-               first_frame = NULL;
                mlt_properties_set_data( properties, "first_frame", NULL, 0, NULL, NULL );
+               first_frame = NULL;
+       }
+
+       if ( output && first_position != -1 ) {
+               // Using the cached frame
+               uint8_t *image_copy = mlt_pool_alloc( size );
+               memcpy( image_copy, output, size );
+                uint8_t *alpha_copy = mlt_pool_alloc( alphasize );
+                memcpy( alpha_copy, output_alpha, alphasize );
+
+               // Set the output image
+               *image = image_copy;
+               mlt_frame_set_image( frame, image_copy, size, mlt_pool_release );
+                mlt_frame_set_alpha( frame, alpha_copy, alphasize, mlt_pool_release );
+
+               *width = mlt_properties_get_int( properties, "_output_width" );
+               *height = mlt_properties_get_int( properties, "_output_height" );
+               *format = mlt_properties_get_int( properties, "_output_format" );
+
+               mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) );
+               return 0;
        }
 
        // Get the cached frame
@@ -104,78 +146,102 @@ static int framebuffer_get_image( mlt_frame this, uint8_t **image, mlt_image_for
        }
        mlt_properties first_frame_properties = MLT_FRAME_PROPERTIES( first_frame );
 
-       // Determine output buffer size
-       *width = mlt_properties_get_int( frame_properties, "width" );
-       *height = mlt_properties_get_int( frame_properties, "height" );
-       int size;
-       switch ( *format )
-       {
-               case mlt_image_yuv420p:
-                       size = *width * 3 * ( *height + 1 ) / 2;
-                       break;
-               case mlt_image_rgb24:
-                       size = *width * ( *height + 1 ) * 3;
-                       break;
-               default:
-                       *format = mlt_image_yuv422;
-                       size = *width * ( *height + 1 ) * 2;
-                       break;
-       }
-
-       // Get output buffer
-       uint8_t *output = mlt_properties_get_data( properties, "output_buffer", NULL );
-       if( output == NULL )
-       {
-               output = mlt_pool_alloc( size );
-
-               // Let someone else clean up
-               mlt_properties_set_data( properties, "output_buffer", output, size, mlt_pool_release, NULL ); 
-       }
 
        // Which frames are buffered?
        uint8_t *first_image = mlt_properties_get_data( first_frame_properties, "image", NULL );
-       if( first_image == NULL )
+        uint8_t *first_alpha = mlt_properties_get_data( first_frame_properties, "alpha", NULL );
+       if ( !first_image )
        {
-               mlt_properties props = MLT_FRAME_PROPERTIES( this );
-               mlt_properties test_properties = MLT_FRAME_PROPERTIES( first_frame );
-               mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( props, "consumer_aspect_ratio" ) );
-               mlt_properties_set( test_properties, "rescale.interp", mlt_properties_get( props, "rescale.interp" ) );
+               mlt_properties_set( first_frame_properties, "rescale.interp", mlt_properties_get( frame_properties, "rescale.interp" ) );
 
                int error = mlt_frame_get_image( first_frame, &first_image, format, width, height, writable );
 
                if ( error != 0 ) {
-                       fprintf(stderr, "first_image == NULL get image died\n");
+                       mlt_log_warning( MLT_PRODUCER_SERVICE( producer ), "first_image == NULL get image died\n" );
+                       mlt_properties_set_data( properties, "first_frame", NULL, 0, NULL, NULL );
+                       mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) );
                        return error;
                }
+               output = mlt_pool_alloc( size );
+               memcpy( output, first_image, size );
+               // Let someone else clean up
+               mlt_properties_set_data( properties, "output_buffer", output, size, mlt_pool_release, NULL ); 
+               mlt_properties_set_int( properties, "_output_width", *width );
+               mlt_properties_set_int( properties, "_output_height", *height );
+               mlt_properties_set_int( properties, "_output_format", *format );
+       
        }
 
-       // Start with a base image
-       memcpy( output, first_image, size );
+       if ( !first_alpha )
+        {
+                alphasize = *width * *height;
+                first_alpha = mlt_frame_get_alpha_mask( first_frame );
+                output_alpha = mlt_pool_alloc( alphasize );
+                memcpy( output_alpha, first_alpha, alphasize );
+                mlt_properties_set_data( properties, "output_alpha", output_alpha, alphasize, mlt_pool_release, NULL ); 
+        }
+
+       mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) );
+
+       // Create a copy
+       uint8_t *image_copy = mlt_pool_alloc( size );
+       memcpy( image_copy, first_image, size );
+        uint8_t *alpha_copy = mlt_pool_alloc( alphasize );
+        memcpy( alpha_copy, first_alpha, alphasize );
 
        // Set the output image
-       *image = output;
-       mlt_properties_set_data( frame_properties, "image", output, size, NULL, NULL );
+       *image = image_copy;
+       mlt_frame_set_image( frame, *image, size, mlt_pool_release );
 
-       // Make sure that no further scaling is done
-       mlt_properties_set( frame_properties, "rescale.interps", "none" );
-       mlt_properties_set( frame_properties, "scale", "off" );
+       mlt_frame_set_alpha( frame, alpha_copy, alphasize, mlt_pool_release );
 
        return 0;
 }
 
-static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
+static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
 {
-       // Construct a new frame
-       *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
-       if( frame != NULL )
+       if ( frame )
        {
+               // Construct a new frame
+               *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
+
                // Stack the producer and producer's get image
                mlt_frame_push_service( *frame, (void*) index );
-               mlt_frame_push_service( *frame, this );
+               mlt_frame_push_service( *frame, producer );
                mlt_frame_push_service( *frame, framebuffer_get_image );
 
+               mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
+               mlt_properties frame_properties = MLT_FRAME_PROPERTIES(*frame);
+
+               // Get frame from the real producer
+               mlt_frame first_frame = mlt_properties_get_data( properties, "first_frame", NULL );
+
+               if ( first_frame == NULL )
+               {
+                   // Get the frame to cache from the real producer
+                   mlt_producer real_producer = mlt_properties_get_data( properties, "producer", NULL );
+
+                   // Seek the producer to the correct place
+                   mlt_producer_seek( real_producer, mlt_producer_position( producer ) );
+
+                   // Get the frame
+                   mlt_service_get_frame( MLT_PRODUCER_SERVICE( real_producer ), &first_frame, index );
+                   // Cache the frame
+                   mlt_properties_set_data( properties, "first_frame", first_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
+               }
+
+               mlt_properties_inherit( frame_properties, MLT_FRAME_PROPERTIES(first_frame) );
+               
+               double force_aspect_ratio = mlt_properties_get_double( properties, "force_aspect_ratio" );
+               if ( force_aspect_ratio <= 0.0 ) force_aspect_ratio = mlt_properties_get_double( properties, "aspect_ratio" );
+               mlt_properties_set_double( frame_properties, "aspect_ratio", force_aspect_ratio );
+                
                // Give the returned frame temporal identity
-               mlt_frame_set_position( *frame, mlt_producer_position( this ) );
+               mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
+
+               mlt_properties_set_int( frame_properties, "meta.media.width", mlt_properties_get_int( properties, "width" ) );
+               mlt_properties_set_int( frame_properties, "meta.media.height", mlt_properties_get_int( properties, "height" ) );
+               mlt_properties_pass_list( frame_properties, properties, "width, height" );
        }
 
        return 0;
@@ -185,9 +251,16 @@ static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index
 mlt_producer producer_framebuffer_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
 {
        if ( !arg ) return NULL;
-       mlt_producer this = NULL;
-       this = calloc( 1, sizeof( struct mlt_producer_s ) );
-       mlt_producer_init( this, NULL );
+       mlt_producer producer = NULL;
+       producer = calloc( 1, sizeof( struct mlt_producer_s ) );
+       if ( !producer )
+               return NULL;
+
+       if ( mlt_producer_init( producer, NULL ) )
+       {
+               free( producer );
+               return NULL;
+       }
 
        // Wrap loader
        mlt_producer real_producer;
@@ -221,25 +294,23 @@ mlt_producer producer_framebuffer_init( mlt_profile profile, mlt_service_type ty
                        *ptr = '\0';
        }
                
-       real_producer = mlt_factory_producer( profile, NULL, props );
+       real_producer = mlt_factory_producer( profile, "abnormal", props );
        free( props );
 
        if (speed == 0.0) speed = 1.0;
 
-       if ( this != NULL && real_producer != NULL)
+       if ( producer != NULL && real_producer != NULL)
        {
                // Get the properties of this producer
-               mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
+               mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
 
-               // The loader normalised it for us already
-               mlt_properties_set_int( properties, "loader_normalised", 1);
                mlt_properties_set( properties, "resource", arg);
 
                // Store the producer and fitler
                mlt_properties_set_data( properties, "producer", real_producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
 
                // Grab some stuff from the real_producer
-               mlt_properties_pass_list( properties, MLT_PRODUCER_PROPERTIES( real_producer ), "length, width,height" );
+               mlt_properties_pass_list( properties, MLT_PRODUCER_PROPERTIES( real_producer ), "length, width, height, aspect_ratio" );
 
                if ( speed < 0 )
                {
@@ -251,24 +322,46 @@ mlt_producer producer_framebuffer_init( mlt_profile profile, mlt_service_type ty
                {
                        double real_length = ( (double)  mlt_producer_get_length( real_producer ) ) / speed;
                        mlt_properties_set_position( properties, "length", real_length );
+                       mlt_properties real_properties = MLT_PRODUCER_PROPERTIES( real_producer );
+                       const char* service = mlt_properties_get( real_properties, "mlt_service" );
+                       if ( service && !strcmp( service, "avformat" ) )
+                       {
+                               int n = mlt_properties_count( real_properties );
+                               int i;
+                               for ( i = 0; i < n; i++ )
+                               {
+                                       if ( strstr( mlt_properties_get_name( real_properties, i ), "stream.frame_rate" ) )
+                                       {
+                                               double source_fps = mlt_properties_get_double( real_properties, mlt_properties_get_name( real_properties, i ) );
+                                               if ( source_fps > mlt_profile_fps( profile ) )
+                                               {
+                                                       mlt_properties_set_double( real_properties, "force_fps", source_fps * speed );
+                                                       mlt_properties_set_position( real_properties, "length", real_length );
+                                                       mlt_properties_set_position( real_properties, "out", real_length - 1 );
+                                                       speed = 1.0;
+                                               }
+                                               break;
+                                       }
+                               }
+                       }
                }
-               mlt_properties_set_position( properties, "out", mlt_producer_get_length( this ) - 1 );
+               mlt_properties_set_position( properties, "out", mlt_producer_get_length( producer ) - 1 );
 
                // Since we control the seeking, prevent it from seeking on its own
                mlt_producer_set_speed( real_producer, 0 );
-               mlt_producer_set_speed( this, speed );
+               mlt_producer_set_speed( producer, speed );
 
                // Override the get_frame method
-               this->get_frame = producer_get_frame;
+               producer->get_frame = producer_get_frame;
        }
        else
        {
-               if ( this )
-                       mlt_producer_close( this );
+               if ( producer )
+                       mlt_producer_close( producer );
                if ( real_producer )
                        mlt_producer_close( real_producer );
 
-               this = NULL;
+               producer = NULL;
        }
-       return this;
+       return producer;
 }