]> git.sesse.net Git - mlt/blobdiff - src/framework/mlt_consumer.c
remove consumer_aspect_ratio property - use profile instead
[mlt] / src / framework / mlt_consumer.c
index fda0fee1e59504682ab6237df24cc7d45a8a327f..9de633231da5f92169d220c7498712985305164e 100644 (file)
@@ -86,6 +86,7 @@ int mlt_consumer_init( mlt_consumer self, void *child, mlt_profile profile )
 
                // Default read ahead buffer size
                mlt_properties_set_int( properties, "buffer", 25 );
+               mlt_properties_set_int( properties, "drop_max", 5 );
 
                // Default audio frequency and channels
                mlt_properties_set_int( properties, "frequency", 48000 );
@@ -99,6 +100,8 @@ int mlt_consumer_init( mlt_consumer self, void *child, mlt_profile profile )
 
                // Hmm - default all consumers to yuv422 :-/
                self->format = mlt_image_yuv422;
+               mlt_properties_set( properties, "mlt_image_format", mlt_image_format_name( self->format ) );
+               mlt_properties_set( properties, "mlt_audio_format", mlt_audio_format_name( mlt_audio_s16 ) );
 
                mlt_events_register( properties, "consumer-frame-show", ( mlt_transmitter )mlt_consumer_frame_show );
                mlt_events_register( properties, "consumer-frame-render", ( mlt_transmitter )mlt_consumer_frame_render );
@@ -450,7 +453,23 @@ int mlt_consumer_start( mlt_consumer self )
 
        // For worker threads implementation, buffer must be at least # threads
        if ( abs( self->real_time ) > 1 && mlt_properties_get_int( properties, "buffer" ) <= abs( self->real_time ) )
-               mlt_properties_set_int( properties, "buffer", abs( self->real_time ) + 1 );
+               mlt_properties_set_int( properties, "_buffer", abs( self->real_time ) + 1 );
+
+       // Get the image format to use for rendering threads
+       const char* format = mlt_properties_get( properties, "mlt_image_format" );
+       if ( format )
+       {
+               if ( !strcmp( format, "rgb24" ) )
+                       self->format = mlt_image_rgb24;
+               else if ( !strcmp( format, "rgb24a" ) )
+                       self->format = mlt_image_rgb24a;
+               else if ( !strcmp( format, "yuv420p" ) )
+                       self->format = mlt_image_yuv420p;
+               else if ( !strcmp( format, "none" ) )
+                       self->format = mlt_image_none;
+               else
+                       self->format = mlt_image_yuv422;
+       }
 
        // Start the service
        if ( self->start != NULL )
@@ -562,13 +581,12 @@ mlt_frame mlt_consumer_get_frame( mlt_consumer self )
                if ( test_card != NULL )
                        mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
 
-               // Attach the rescale property
+               // Pass along the interpolation and deinterlace options
+               // TODO: get rid of consumer_deinterlace and use profile.progressive
                mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
-
-               // Aspect ratio and other jiggery pokery
-               mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
                mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
                mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
+               mlt_properties_set_int( frame_properties, "consumer_tff", mlt_properties_get_int( properties, "top_field_first" ) );
        }
 
        // Return the frame
@@ -617,6 +635,20 @@ static void *consumer_read_ahead_thread( void *arg )
 
        // Get the audio settings
        mlt_audio_format afmt = mlt_audio_s16;
+       const char *format = mlt_properties_get( properties, "mlt_audio_format" );
+       if ( format )
+       {
+               if ( !strcmp( format, "none" ) )
+                       afmt = mlt_audio_none;
+               else if ( !strcmp( format, "s32" ) )
+                       afmt = mlt_audio_s32;
+               else if ( !strcmp( format, "s32le" ) )
+                       afmt = mlt_audio_s32le;
+               else if ( !strcmp( format, "float" ) )
+                       afmt = mlt_audio_float;
+               else if ( !strcmp( format, "f32le" ) )
+                       afmt = mlt_audio_f32le;
+       }
        int counter = 0;
        double fps = mlt_properties_get_double( properties, "fps" );
        int channels = mlt_properties_get_int( properties, "channels" );
@@ -646,6 +678,7 @@ static void *consumer_read_ahead_thread( void *arg )
        mlt_position start_pos = 0;
        mlt_position last_pos = 0;
        int frame_duration = mlt_properties_get_int( properties, "frame_duration" );
+       int drop_max = mlt_properties_get_int( properties, "drop_max" );
 
        if ( preview_off && preview_format != 0 )
                self->format = preview_format;
@@ -734,7 +767,7 @@ static void *consumer_read_ahead_thread( void *arg )
                        skipped++;
 
                        // If too many (1 sec) consecutively-skipped frames
-                       if ( skipped > fps )
+                       if ( skipped > drop_max )
                        {
                                // Reset cost tracker
                                time_process = 0;
@@ -1153,10 +1186,12 @@ static mlt_frame worker_get_frame( mlt_consumer self, mlt_properties properties
        // Frame to return
        mlt_frame frame = NULL;
 
-       int size = abs( self->real_time );
-       int buffer = mlt_properties_get_int( properties, "buffer" );
+       double fps = mlt_properties_get_double( properties, "fps" );
+       int threads = abs( self->real_time );
+       int buffer = mlt_properties_get_int( properties, "_buffer" );
+       buffer = buffer > 0 ? buffer : mlt_properties_get_int( properties, "buffer" );
        // This is a heuristic to determine a suitable minimum buffer size for the number of threads.
-       int headroom = 2 + size * size;
+       int headroom = 2 + threads * threads;
        buffer = buffer < headroom ? headroom : buffer;
 
        // Start worker threads if not already started.
@@ -1188,11 +1223,11 @@ static mlt_frame worker_get_frame( mlt_consumer self, mlt_properties properties
                        pthread_cond_wait( &self->done_cond, &self->done_mutex );
                        pthread_mutex_unlock( &self->done_mutex );
                }
-               self->process_head = size;
+               self->process_head = threads;
        }
 
 //     mlt_log_verbose( MLT_CONSUMER_SERVICE(self), "size %d done count %d work count %d process_head %d\n",
-//             size, first_unprocessed_frame( self ), mlt_deque_count( self->queue ), self->process_head );
+//             threads, first_unprocessed_frame( self ), mlt_deque_count( self->queue ), self->process_head );
 
        // Feed the work queue
        while ( self->ahead && mlt_deque_count( self->queue ) < buffer )
@@ -1207,8 +1242,9 @@ static mlt_frame worker_get_frame( mlt_consumer self, mlt_properties properties
        }
 
        // Wait if not realtime.
-       while( self->ahead && self->real_time < 0 &&
-              ! mlt_properties_get_int( MLT_FRAME_PROPERTIES( MLT_FRAME( mlt_deque_peek_front( self->queue ) ) ), "rendered" ) )
+       mlt_frame head_frame = MLT_FRAME( mlt_deque_peek_front( self->queue ) );
+       while ( self->ahead && self->real_time < 0 &&
+               !( head_frame && mlt_properties_get_int( MLT_FRAME_PROPERTIES( head_frame ), "rendered" ) ) )
        {
                pthread_mutex_lock( &self->done_mutex );
                pthread_cond_wait( &self->done_cond, &self->done_mutex );
@@ -1226,7 +1262,7 @@ static mlt_frame worker_get_frame( mlt_consumer self, mlt_properties properties
                if ( frame && mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "rendered" ) )
                {
                        self->consecutive_dropped = 0;
-                       if ( self->process_head > size && self->consecutive_rendered >= self->process_head )
+                       if ( self->process_head > threads && self->consecutive_rendered >= self->process_head )
                                self->process_head--;
                        else
                                self->consecutive_rendered++;
@@ -1234,13 +1270,37 @@ static mlt_frame worker_get_frame( mlt_consumer self, mlt_properties properties
                else
                {
                        self->consecutive_rendered = 0;
-                       if ( self->process_head < buffer - size && self->consecutive_dropped > size )
+                       if ( self->process_head < buffer - threads && self->consecutive_dropped > threads )
                                self->process_head++;
                        else
                                self->consecutive_dropped++;
                }
 //             mlt_log_verbose( MLT_CONSUMER_SERVICE(self), "dropped %d rendered %d process_head %d\n",
 //                     self->consecutive_dropped, self->consecutive_rendered, self->process_head );
+
+               // Check for too many consecutively dropped frames
+               if ( self->consecutive_dropped > mlt_properties_get_int( properties, "drop_max" ) )
+               {
+                       int orig_buffer = mlt_properties_get_int( properties, "buffer" );
+                       int prefill = mlt_properties_get_int( properties, "prefill" );
+                       mlt_log_verbose( self, "too many frames dropped - " );
+
+                       // If using a default low-latency buffer level (SDL) and below the limit
+                       if ( ( orig_buffer == 1 || prefill == 1 ) && buffer < (threads + 1) * 10 )
+                       {
+                               // Auto-scale the buffer to compensate
+                               mlt_log_verbose( self, "increasing buffer to %d\n", buffer + threads );
+                               mlt_properties_set_int( properties, "_buffer", buffer + threads );
+                               self->consecutive_dropped = fps / 2;
+                       }
+                       else
+                       {
+                               // Tell the consumer to render it
+                               mlt_log_verbose( self, "forcing next frame\n" );
+                               mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
+                               self->consecutive_dropped = 0;
+                       }
+               }
        }
        
        return frame;