]> git.sesse.net Git - mlt/blobdiff - src/modules/sdl/producer_sdl_image.c
Fix reading binary files on Windows.
[mlt] / src / modules / sdl / producer_sdl_image.c
index ea913ba328cc2942cb53ce3fb00a086bebf06353..b405ecdcaf41f897aa5d91d81c7176d47546bfd8 100644 (file)
@@ -18,7 +18,7 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-#include "producer_sdl_image.h"
+#include <framework/mlt_producer.h>
 #include <framework/mlt_frame.h>
 #include <framework/mlt_pool.h>
 
@@ -38,13 +38,10 @@ static int producer_get_image( mlt_frame frame, uint8_t **image, mlt_image_forma
        mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
        SDL_Surface *surface = mlt_properties_get_data( properties, "surface", NULL );
        SDL_Surface *converted = NULL;
-       uint8_t *alpha;
 
        *width = surface->w;
        *height = surface->h;
-       *format = mlt_image_yuv422;
-       *image = mlt_pool_alloc( *width * *height * 2 );
-       alpha = mlt_pool_alloc( *width * *height );
+       int image_size = *width * *height * 3;
 
        if ( surface->format->BitsPerPixel != 32 && surface->format->BitsPerPixel != 24 )
        {
@@ -63,15 +60,15 @@ static int producer_get_image( mlt_frame frame, uint8_t **image, mlt_image_forma
        switch( surface->format->BitsPerPixel )
        {
                case 32:
-                       mlt_convert_rgb24a_to_yuv422( surface->pixels, *width, *height, surface->pitch, *image, alpha );
-                       break;
-               case 24:
-                       mlt_convert_rgb24_to_yuv422( surface->pixels, *width, *height, surface->pitch, *image );
-                       memset( alpha, 255, *width * *height );
+                       *format = mlt_image_rgb24a;
+                       image_size = *width * *height * 4;
+                       *image = mlt_pool_alloc( image_size );
+                       memcpy( *image, surface->pixels, image_size );
                        break;
                default:
-                       mlt_convert_rgb24_to_yuv422( converted->pixels, *width, *height, converted->pitch, *image );
-                       memset( alpha, 255, *width * *height );
+                       *format = mlt_image_rgb24;
+                       *image = mlt_pool_alloc( image_size );
+                       memcpy( *image, surface->pixels, image_size );
                        break;
        }
 
@@ -79,15 +76,12 @@ static int producer_get_image( mlt_frame frame, uint8_t **image, mlt_image_forma
                SDL_FreeSurface( converted );
 
        // Update the frame
-       mlt_properties_set_data( properties, "image", *image, *width * *height * 2, mlt_pool_release, NULL );
-       mlt_properties_set_data( properties, "alpha", alpha, *width * *height, mlt_pool_release, NULL );
-       mlt_properties_set_int( properties, "width", *width );
-       mlt_properties_set_int( properties, "height", *height );
+       mlt_frame_set_image( frame, *image, image_size, mlt_pool_release );
 
        return 0;
 }
 
-static int filter_files( struct dirent *de )
+static int filter_files( const struct dirent *de )
 {
        return de->d_name[ 0 ] != '.';
 }
@@ -147,26 +141,32 @@ static SDL_Surface *load_image( mlt_producer producer )
        if ( filenames == NULL )
        {
                filenames = parse_file_names( resource );
+               mlt_properties_set_data( properties, "_filenames", filenames, 0, ( mlt_destructor )mlt_properties_close, 0 );
                mlt_properties_set_data( properties, "_surface", surface, 0, ( mlt_destructor )SDL_FreeSurface, 0 );
        }
 
-       image_idx = ( int )floor( ( double )position / ttl ) % mlt_properties_count( filenames );
-       this_resource = mlt_properties_get_value( filenames, image_idx );
-
-       if ( last_resource == NULL || strcmp( last_resource, this_resource ) )
+       if ( mlt_properties_count( filenames ) ) 
        {
-               surface = IMG_Load( this_resource );
-               if ( surface != NULL )
+               image_idx = ( int )floor( ( double )position / ttl ) % mlt_properties_count( filenames );
+               this_resource = mlt_properties_get_value( filenames, image_idx );
+
+               if ( surface == NULL || last_resource == NULL || strcmp( last_resource, this_resource ) )
+               {
+                       surface = IMG_Load( this_resource );
+                       if ( surface != NULL )
+                       {
+                               surface->refcount ++;
+                               mlt_properties_set_data( properties, "_surface", surface, 0, ( mlt_destructor )SDL_FreeSurface, 0 );
+                               mlt_properties_set( properties, "_last_resource", this_resource );
+                               mlt_properties_set_int( properties, "meta.media.width", surface->w );
+                               mlt_properties_set_int( properties, "meta.media.height", surface->h );
+                       }
+               }
+               else if ( surface != NULL )
                {
                        surface->refcount ++;
-                       mlt_properties_set_data( properties, "_surface", surface, 0, ( mlt_destructor )SDL_FreeSurface, 0 );
-                       mlt_properties_set( properties, "_last_resource", this_resource );
                }
        }
-       else if ( surface != NULL )
-       {
-               surface->refcount ++;
-       }
 
        return surface;
 }
@@ -174,7 +174,7 @@ static SDL_Surface *load_image( mlt_producer producer )
 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
 {
        // Generate a frame
-       *frame = mlt_frame_init( );
+       *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
 
        if ( *frame != NULL )
        {
@@ -196,8 +196,6 @@ static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int i
                        mlt_properties_set_int( properties, "progressive", 1 );
                        mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_props, "aspect_ratio" ) );
                        mlt_properties_set_data( properties, "surface", surface, 0, ( mlt_destructor )SDL_FreeSurface, NULL );
-                       mlt_properties_set_int( properties, "real_width", surface->w );
-                       mlt_properties_set_int( properties, "real_height", surface->h );
 
                        // Push the get_image method
                        mlt_frame_push_get_image( *frame, producer_get_image );
@@ -217,7 +215,7 @@ static void producer_close( mlt_producer producer )
        free( producer );
 }
 
-mlt_producer producer_sdl_image_init( char *file )
+mlt_producer producer_sdl_image_init( mlt_profile profile, mlt_service_type type, const char *id, char *file )
 {
        mlt_producer producer = calloc( 1, sizeof( struct mlt_producer_s ) );
        if ( producer != NULL && mlt_producer_init( producer, NULL ) == 0 )
@@ -236,6 +234,18 @@ mlt_producer producer_sdl_image_init( char *file )
                mlt_properties_set_int( properties, "ttl", 25 );
                mlt_properties_set_int( properties, "progressive", 1 );
                
+               // Validate the resource
+               SDL_Surface *surface = NULL;
+               if ( file && ( surface = load_image( producer ) ) )
+               {
+                       SDL_FreeSurface( surface );
+                       mlt_properties_set_data( properties, "_surface", NULL, 0, NULL, NULL );
+               }
+               else
+               {
+                       producer_close( producer );
+                       producer = NULL;
+               }
                return producer;
        }
        free( producer );