Fixes threaded pixbuf usage and removes flash when swicthing between sdl preview...
[melted] / src / modules / sdl / consumer_sdl.c
index 6e8e12d..b5c8fd1 100644 (file)
@@ -61,6 +61,7 @@ struct consumer_sdl_s
        SDL_Overlay *sdl_overlay;
        SDL_Rect rect;
        uint8_t *buffer;
+       int bpp;
 };
 
 /** Forward references to static functions.
@@ -118,7 +119,7 @@ mlt_consumer consumer_sdl_init( char *arg )
                mlt_properties_set_int( this->properties, "progressive", 0 );
 
                // Default audio buffer
-               mlt_properties_set_int( this->properties, "audio_buffer", 1024 );
+               mlt_properties_set_int( this->properties, "audio_buffer", 512 );
 
                // Get sample aspect ratio
                this->aspect_ratio = mlt_properties_get_double( this->properties, "aspect_ratio" );
@@ -386,7 +387,7 @@ static int consumer_play_video( consumer_sdl this, mlt_frame frame )
                        SDL_Event event;
        
                        changed = consumer_get_dimensions( &this->window_width, &this->window_height );
-       
+
                        while ( SDL_PollEvent( &event ) )
                        {
                                mlt_events_fire( this->properties, "consumer-sdl-event", &event, NULL );
@@ -417,25 +418,48 @@ static int consumer_play_video( consumer_sdl this, mlt_frame frame )
                        }
                }
        
-               if ( width != this->width || height != this->height || 
-                        ( ( int )( this->last_frame_aspect * 1000 ) != ( int )( mlt_frame_get_aspect_ratio( frame ) * 1000 ) &&
-                        ( mlt_frame_get_aspect_ratio( frame ) != 1.0 || this->last_frame_aspect == 0.0 ) ) )
+               if ( width != this->width || height != this->height )
+               {
+                       if ( this->sdl_overlay != NULL )
+                               SDL_FreeYUVOverlay( this->sdl_overlay );
+                       this->sdl_overlay = NULL;
+               }
 
+               if ( this->running && ( this->sdl_screen == NULL || changed ) )
                {
-                       this->width = width;
-                       this->height = height;
-                       this->last_frame_aspect = mlt_frame_get_aspect_ratio( frame );
-                       changed = 1;
+                       // Force an overlay recreation
+                       if ( this->sdl_overlay != NULL )
+                               SDL_FreeYUVOverlay( this->sdl_overlay );
+                       this->sdl_overlay = NULL;
+
+                       // open SDL window with video overlay, if possible
+                       sdl_lock_display();
+                       this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
+                       sdl_unlock_display();
+                       if ( consumer_get_dimensions( &this->window_width, &this->window_height ) )
+                               this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
+                       //SDL_Flip( this->sdl_screen );
+                       mlt_properties_set_int( properties, "changed", 0 );
+               }
+               else if ( mlt_properties_get_int( properties, "changed" ) )
+               {
+                       sdl_lock_display();
+                       this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
+                       SDL_SetClipRect( this->sdl_screen, &this->rect );
+                       sdl_unlock_display();
+                       mlt_properties_set_int( properties, "changed", 0 );
                }
 
-               if ( this->sdl_screen == NULL || changed )
+               if ( 1 )
                {
-                       // Determine frame's display aspect ratio
-                       float frame_aspect = mlt_frame_get_aspect_ratio( frame ) * this->width / this->height;
-                       
                        // Determine window's new display aspect ratio
                        float this_aspect = ( float )this->window_width / this->window_height;
 
+                       // Determine frame's display aspect ratio
+                       float frame_aspect = mlt_frame_get_aspect_ratio( frame ) * width / height;
+                       this->width = width;
+                       this->height = height;
+
                        // If using hardware scaler
                        if ( mlt_properties_get( properties, "rescale" ) != NULL &&
                                !strcmp( mlt_properties_get( properties, "rescale" ), "none" ) )
@@ -487,35 +511,16 @@ static int consumer_play_video( consumer_sdl this, mlt_frame frame )
                        mlt_properties_set_int( this->properties, "rect_w", this->rect.w );
                        mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
 
-                       // Force an overlay recreation
-                       if ( this->sdl_overlay != NULL )
-                               SDL_FreeYUVOverlay( this->sdl_overlay );
-
-                       // open SDL window with video overlay, if possible
-                       sdl_lock_display();
-                       this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
-                       sdl_unlock_display();
-                       if ( consumer_get_dimensions( &this->window_width, &this->window_height ) )
-                               this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
-                       SDL_Flip( this->sdl_screen );
-                       mlt_properties_set_int( properties, "changed", 0 );
-               
-                       if ( this->sdl_screen != NULL )
-                       {
-                               SDL_SetClipRect( this->sdl_screen, &this->rect );
-                               sdl_lock_display();
-                               this->sdl_overlay = SDL_CreateYUVOverlay( this->width, this->height, SDL_YUY2_OVERLAY, this->sdl_screen );
-                               sdl_unlock_display();
-                       }
+                       SDL_SetClipRect( this->sdl_screen, &this->rect );
                }
-                       
-               if ( mlt_properties_get_int( properties, "changed" ) )
+
+               if ( this->sdl_screen != NULL && this->sdl_overlay == NULL )
                {
-                       sdl_lock_display();
-                       this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
                        SDL_SetClipRect( this->sdl_screen, &this->rect );
+                       SDL_Flip( this->sdl_screen );
+                       sdl_lock_display();
+                       this->sdl_overlay = SDL_CreateYUVOverlay( this->width, this->height, SDL_YUY2_OVERLAY, this->sdl_screen );
                        sdl_unlock_display();
-                       mlt_properties_set_int( properties, "changed", 0 );
                }
 
                if ( this->sdl_screen != NULL && this->sdl_overlay != NULL )
@@ -640,6 +645,8 @@ static void *consumer_thread( void *arg )
        int64_t playtime = 0;
        struct timespec tm = { 0, 100000 };
 
+       this->bpp = mlt_properties_get_int( this->properties, "bpp" );
+
        if ( mlt_properties_get_int( mlt_consumer_properties( consumer ), "sdl_started" ) == 0 )
        {
                if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
@@ -654,11 +661,15 @@ static void *consumer_thread( void *arg )
        else
        {
                if ( SDL_GetVideoSurface( ) != NULL )
+               {
+                       this->sdl_screen = SDL_GetVideoSurface( );
                        consumer_get_dimensions( &this->window_width, &this->window_height );
-               mlt_properties_set_int( mlt_consumer_properties( consumer ), "changed", 1 );
+                       mlt_properties_set_int( this->properties, "changed", 0 );
+               }
        }
 
-       SDL_InitSubSystem( SDL_INIT_AUDIO );
+       if ( !mlt_properties_get_int( mlt_consumer_properties( consumer ), "audio_off" ) )
+               SDL_InitSubSystem( SDL_INIT_AUDIO );
 
        // Loop until told not to
        while( this->running )
@@ -715,7 +726,8 @@ static void *consumer_thread( void *arg )
        if ( this->sdl_overlay != NULL )
                SDL_FreeYUVOverlay( this->sdl_overlay );
 
-       SDL_QuitSubSystem( SDL_INIT_AUDIO );
+       if ( !mlt_properties_get_int( mlt_consumer_properties( consumer ), "audio_off" ) )
+               SDL_QuitSubSystem( SDL_INIT_AUDIO );
 
        if ( mlt_properties_get_int( mlt_consumer_properties( consumer ), "sdl_started" ) == 0 )
                SDL_Quit( );