]> git.sesse.net Git - mlt/commitdiff
add framerate adaption to consumer producer
authorDan Dennedy <dan@dennedy.org>
Wed, 16 Nov 2011 03:02:12 +0000 (19:02 -0800)
committerDan Dennedy <dan@dennedy.org>
Wed, 16 Nov 2011 03:02:12 +0000 (19:02 -0800)
src/modules/core/producer_consumer.c

index b2e7d69d32f6b81f5572756f53fef5c9f578aaee..99831ee952b342dc0399bada157a6a4ff674ed0a 100644 (file)
@@ -30,6 +30,8 @@ struct context_s {
        mlt_producer producer;
        mlt_consumer consumer;
        mlt_profile profile;
+       int64_t audio_counter;
+       mlt_position audio_position;
 };
 typedef struct context_s *context; 
 
@@ -45,8 +47,7 @@ static int get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format
        int result = mlt_frame_get_image( nested_frame, image, format, width, height, writable );
 
        // Allocate the image
-       int size = *width * *height * ( *format == mlt_image_yuv422 ? 2 : *format == mlt_image_rgb24 ? 3 :
-               ( *format == mlt_image_rgb24a || *format == mlt_image_opengl ) ? 4 : ( 3 / 2 ) );
+       int size = mlt_image_format_size( *format, *width, *height, NULL );
        uint8_t *new_image = mlt_pool_alloc( size );
 
        // Update the frame
@@ -70,14 +71,31 @@ static int get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format
 
 static int get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
 {
+       context cx = mlt_frame_pop_audio( frame );
        mlt_frame nested_frame = mlt_frame_pop_audio( frame );
-       int result = mlt_frame_get_audio( nested_frame, buffer, format, frequency, channels, samples );
-       int size = mlt_audio_format_size( *format, *samples, *channels );
-       int16_t *new_buffer = mlt_pool_alloc( size );
+       int result = 0;
 
-       mlt_frame_set_audio( frame, new_buffer, *format, size, mlt_pool_release );
-       memcpy( new_buffer, *buffer, size );
-       *buffer = new_buffer;
+       // if not repeating last frame
+       if ( mlt_frame_get_position( nested_frame ) != cx->audio_position )
+       {
+               double fps = mlt_profile_fps( cx->profile );
+               if ( mlt_producer_get_fps( cx->this ) < fps )
+                       fps = mlt_producer_get_fps( cx->this );
+               *samples = mlt_sample_calculator( fps, *frequency, cx->audio_counter++ );
+               result = mlt_frame_get_audio( nested_frame, buffer, format, frequency, channels, samples );
+               int size = mlt_audio_format_size( *format, *samples, *channels );
+               int16_t *new_buffer = mlt_pool_alloc( size );
+
+               mlt_frame_set_audio( frame, new_buffer, *format, size, mlt_pool_release );
+               memcpy( new_buffer, *buffer, size );
+               *buffer = new_buffer;
+       }
+       else
+       {
+               // otherwise return no samples
+               *samples = 0;
+       }
+       cx->audio_position = mlt_frame_get_position( nested_frame );
 
        return result;
 }
@@ -91,6 +109,7 @@ static int get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
        {
                // Allocate and initialize our context
                cx = mlt_pool_alloc( sizeof( struct context_s ) );
+               memset( cx, 0, sizeof( *cx ) );
                mlt_properties_set_data( properties, "context", cx, 0, mlt_pool_release, NULL );
                cx->this = this;
                char *profile_name = mlt_properties_get( properties, "profile" );
@@ -107,25 +126,16 @@ static int get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
                        cx->profile->is_explicit = 0;
                }
 
-               if ( ! mlt_properties_get_int( properties, "autoprofile" ) )
-               {
-                       // For now, we must conform the nested network's frame rate to the parent network's
-                       // framerate.
-                       cx->profile->frame_rate_num = profile->frame_rate_num;
-                       cx->profile->frame_rate_den = profile->frame_rate_den;
-               }
-
                // Encapsulate a real producer for the resource
                cx->producer = mlt_factory_producer( cx->profile, NULL,
                        mlt_properties_get( properties, "resource" ) );
-               if ( mlt_properties_get_int( properties, "autoprofile" ) )
+               if ( ( profile_name && !strcmp( profile_name, "auto" ) ) ||
+                       mlt_properties_get_int( properties, "autoprofile" ) )
                {
                        mlt_profile_from_producer( cx->profile, cx->producer );
                        mlt_producer_close( cx->producer );
                        cx->producer = mlt_factory_producer( cx->profile, NULL, mlt_properties_get( properties, "resource" ) );
                }
-               mlt_properties_pass_list( properties, MLT_PRODUCER_PROPERTIES( cx->producer ),
-                       "out, length" );
 
                // Since we control the seeking, prevent it from seeking on its own
                mlt_producer_set_speed( cx->producer, 0 );
@@ -148,17 +158,15 @@ static int get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
        *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
        if ( frame )
        {
-               // Our "in" needs to be the same, keep it so
-               mlt_properties_pass_list( MLT_PRODUCER_PROPERTIES( cx->producer ), properties, "in, out" );
-
                // Seek the producer to the correct place
                // Calculate our positions
-               double actual_position = (double)mlt_producer_position( this );
+               double actual_position = (double) mlt_producer_frame( this );
                if ( mlt_producer_get_speed( this ) != 0 )
                        actual_position *= mlt_producer_get_speed( this );
                mlt_position need_first = floor( actual_position );
-               mlt_producer_seek( cx->producer, need_first );
-               
+               mlt_producer_seek( cx->producer, need_first * mlt_profile_fps( cx->profile )
+                       / mlt_producer_get_fps( this ) );
+
                // Get the nested frame
                mlt_frame nested_frame = mlt_consumer_rt_frame( cx->consumer );
 
@@ -167,6 +175,7 @@ static int get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
                mlt_frame_push_service( *frame, cx );
                mlt_frame_push_get_image( *frame, get_image );
                mlt_frame_push_audio( *frame, nested_frame );
+               mlt_frame_push_audio( *frame, cx );
                mlt_frame_push_audio( *frame, get_audio );
                
                // Give the returned frame temporal identity
@@ -214,7 +223,7 @@ mlt_producer producer_consumer_init( mlt_profile profile, mlt_service_type type,
        mlt_producer this = mlt_producer_new( profile );
 
        // Encapsulate the real producer
-       mlt_profile temp_profile = mlt_profile_init( NULL );
+       mlt_profile temp_profile = mlt_profile_clone( profile );
        mlt_producer real_producer = mlt_factory_producer( temp_profile, NULL, arg );
 
        if ( this && real_producer )