Prevent potential divide-by-zero errors in sdl_still consumer.
[melted] / src / modules / sdl / producer_sdl_image.c
index d1c13f9..19f4f4b 100644 (file)
@@ -3,22 +3,22 @@
  * Copyright (C) 2005 Visual Media FX
  * Author: Charles Yates <charles.yates@gmail.com>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This program is distributed in the hope that it will be useful,
+ * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * 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>
 
@@ -37,6 +37,7 @@ 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;
@@ -45,6 +46,20 @@ static int producer_get_image( mlt_frame frame, uint8_t **image, mlt_image_forma
        *image = mlt_pool_alloc( *width * *height * 2 );
        alpha = mlt_pool_alloc( *width * *height );
 
+       if ( surface->format->BitsPerPixel != 32 && surface->format->BitsPerPixel != 24 )
+       {
+               SDL_PixelFormat fmt;
+               fmt.BitsPerPixel = 24;
+               fmt.BytesPerPixel = 3;
+               fmt.Rshift = 16;
+               fmt.Gshift = 8;
+               fmt.Bshift = 0;
+               fmt.Rmask = 0xff << 16;
+               fmt.Gmask = 0xff << 8;
+               fmt.Bmask = 0xff;
+               converted = SDL_ConvertSurface( surface, &fmt, 0 );
+       }
+
        switch( surface->format->BitsPerPixel )
        {
                case 32:
@@ -55,9 +70,14 @@ static int producer_get_image( mlt_frame frame, uint8_t **image, mlt_image_forma
                        memset( alpha, 255, *width * *height );
                        break;
                default:
+                       mlt_convert_rgb24_to_yuv422( converted->pixels, *width, *height, converted->pitch, *image );
+                       memset( alpha, 255, *width * *height );
                        break;
        }
-       
+
+       if ( converted )
+               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 );
@@ -130,23 +150,28 @@ static SDL_Surface *load_image( mlt_producer producer )
                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, "_real_width", surface->w );
+                               mlt_properties_set_int( properties, "_real_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;
 }
@@ -154,7 +179,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 )
        {
@@ -197,7 +222,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 )
@@ -216,6 +241,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 );