]> git.sesse.net Git - mlt/blobdiff - src/modules/gtk2/producer_pixbuf.c
Refactor to use mlt_frame_set_image/_alpha.
[mlt] / src / modules / gtk2 / producer_pixbuf.c
index b72e9c827ba4ffb6af6c24b12a929e15ac81ca87..b12d7811b7412a8f12af802924d86fd5a9d02604 100644 (file)
 #include <framework/mlt_log.h>
 #include <gdk-pixbuf/gdk-pixbuf.h>
 
+#include "config.h"
+
+#ifdef USE_EXIF
+#include <libexif/exif-data.h>
+#endif
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -49,10 +55,9 @@ struct producer_pixbuf_s
        int pixbuf_idx;
        int width;
        int height;
+       int alpha;
        uint8_t *image;
-       uint8_t *alpha;
        mlt_cache_item image_cache;
-       mlt_cache_item alpha_cache;
        pthread_mutex_t mutex;
 };
 
@@ -119,7 +124,7 @@ static void load_filenames( producer_pixbuf this, mlt_properties producer_proper
        {
                // Generate a temporary file for the svg
                char fullname[ 1024 ] = "/tmp/mlt.XXXXXX";
-               int fd = mkstemp( fullname );
+               int fd = g_mkstemp( fullname );
 
                if ( fd > -1 )
                {
@@ -217,12 +222,8 @@ static void refresh_image( producer_pixbuf this, mlt_frame frame, int width, int
        this->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image" );
        this->image = mlt_cache_item_data( this->image_cache, NULL );
 
-       // restore alpha channel
-       this->alpha_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.alpha" );
-       this->alpha = mlt_cache_item_data( this->alpha_cache, NULL );
-
        // Check if user wants us to reload the image
-       if ( mlt_properties_get_int( producer_props, "force_reload" ) ) 
+       if ( mlt_properties_get_int( producer_props, "force_reload" ) )
        {
                pixbuf = NULL;
                this->image = NULL;
@@ -265,13 +266,14 @@ static void refresh_image( producer_pixbuf this, mlt_frame frame, int width, int
                        mlt_properties_set_int( producer_props, "_real_width", mlt_properties_get_int( cached_props, "real_width" ) );
                        mlt_properties_set_int( producer_props, "_real_height", mlt_properties_get_int( cached_props, "real_height" ) );
                        this->image = mlt_properties_get_data( cached_props, "image", NULL );
-                       this->alpha = mlt_properties_get_data( cached_props, "alpha", NULL );
+                       this->alpha = mlt_properties_get_int( cached_props, "alpha" );
 
                        if ( width != 0 && ( width != this->width || height != this->height ) )
                                this->image = NULL;
                }
        }
-
+       int disable_exif = mlt_properties_get_int( producer_props, "disable_exif" );
+       
     // optimization for subsequent iterations on single picture
        if ( width != 0 && ( image_idx != this->image_idx || width != this->width || height != this->height ) )
                this->image = NULL;
@@ -279,13 +281,72 @@ static void refresh_image( producer_pixbuf this, mlt_frame frame, int width, int
                pixbuf = NULL;
        mlt_log_debug( MLT_PRODUCER_SERVICE( producer ), "image %p pixbuf %p idx %d image_idx %d pixbuf_idx %d width %d\n",
                this->image, pixbuf, image_idx, this->image_idx, this->pixbuf_idx, width );
-       if ( !pixbuf && !this->image )
+       if ( !pixbuf || mlt_properties_get_int( producer_props, "_disable_exif" ) != disable_exif )
        {
                this->image = NULL;
                pixbuf = gdk_pixbuf_new_from_file( mlt_properties_get_value( this->filenames, image_idx ), &error );
 
                if ( pixbuf )
                {
+#ifdef USE_EXIF
+                       // Read the exif value for this file
+                       if ( disable_exif == 0) {
+                               ExifData *d = exif_data_new_from_file( mlt_properties_get_value( this->filenames, image_idx ) );
+                               ExifEntry *entry;
+                               int exif_orientation = 0;
+
+                               /* get orientation and rotate image accordingly if necessary */
+                               if (d) {
+                                       if ( ( entry = exif_content_get_entry ( d->ifd[EXIF_IFD_0], EXIF_TAG_ORIENTATION ) ) )
+                                               exif_orientation = exif_get_short (entry->data, exif_data_get_byte_order (d));
+
+                                       /* Free the EXIF data */
+                                       exif_data_unref(d);
+                               }
+                               
+                               // Remember EXIF value, might be useful for someone
+                               mlt_properties_set_int( producer_props, "_exif_orientation" , exif_orientation );
+
+                               if ( exif_orientation > 1 )
+                               {
+                                       GdkPixbuf *processed = NULL;
+                                       GdkPixbufRotation matrix = GDK_PIXBUF_ROTATE_NONE;
+
+                                       // Rotate image according to exif data
+                                       switch ( exif_orientation ) {
+                                         case 2:
+                                             processed = gdk_pixbuf_flip ( pixbuf, TRUE );
+                                             break;
+                                         case 3:
+                                             matrix = GDK_PIXBUF_ROTATE_UPSIDEDOWN;
+                                             processed = pixbuf;
+                                             break;
+                                         case 4:
+                                             processed = gdk_pixbuf_flip ( pixbuf, FALSE );
+                                             break;
+                                         case 5:
+                                             matrix = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
+                                             processed = gdk_pixbuf_flip ( pixbuf, TRUE );
+                                             break;
+                                         case 6:
+                                             matrix = GDK_PIXBUF_ROTATE_CLOCKWISE;
+                                             processed = pixbuf;
+                                             break;
+                                         case 7:
+                                             matrix = GDK_PIXBUF_ROTATE_CLOCKWISE;
+                                             processed = gdk_pixbuf_flip ( pixbuf, TRUE );
+                                             break;
+                                         case 8:
+                                             matrix = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
+                                             processed = pixbuf;
+                                             break;
+                                       }
+                                       if ( processed )
+                                               pixbuf = gdk_pixbuf_rotate_simple( processed, matrix );
+                               }
+                       }
+#endif
+
                        // Register this pixbuf for destruction and reuse
                        mlt_cache_item_close( pixbuf_cache );
                        mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref );
@@ -295,6 +356,7 @@ static void refresh_image( producer_pixbuf this, mlt_frame frame, int width, int
                        mlt_events_block( producer_props, NULL );
                        mlt_properties_set_int( producer_props, "_real_width", gdk_pixbuf_get_width( pixbuf ) );
                        mlt_properties_set_int( producer_props, "_real_height", gdk_pixbuf_get_height( pixbuf ) );
+                       mlt_properties_set_int( producer_props, "_disable_exif", disable_exif );
                        mlt_events_unblock( producer_props, NULL );
 
                        // Store the width/height of the pixbuf temporarily
@@ -313,7 +375,7 @@ static void refresh_image( producer_pixbuf this, mlt_frame frame, int width, int
                        interp = GDK_INTERP_NEAREST;
                else if ( strcmp( interps, "tiles" ) == 0 )
                        interp = GDK_INTERP_TILES;
-               else if ( strcmp( interps, "hyper" ) == 0 )
+               else if ( strcmp( interps, "hyper" ) == 0 || strcmp( interps, "bicubic" ) == 0 )
                        interp = GDK_INTERP_HYPER;
 
                // Note - the original pixbuf is already safe and ready for destruction
@@ -324,37 +386,33 @@ static void refresh_image( producer_pixbuf this, mlt_frame frame, int width, int
                this->height = height;
                
                // Allocate/define image
-               this->image = mlt_pool_alloc( width * ( height + 1 ) * 2 );
-               if ( !use_cache )
-                       mlt_cache_item_close( this->image_cache );
-               mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image", this->image, width * ( height + 1 ) * 2, mlt_pool_release );
-               this->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image" );
-               this->image_idx = image_idx;
+               this->alpha = gdk_pixbuf_get_has_alpha( pixbuf );
+               int src_stride = gdk_pixbuf_get_rowstride( pixbuf );
+               int dst_stride = this->width * ( this->alpha ? 4 : 3 );
+               int image_size = dst_stride * ( height + 1 );
+               this->image = mlt_pool_alloc( image_size );
 
-               // Extract YUV422 and alpha
-               if ( gdk_pixbuf_get_has_alpha( pixbuf ) )
+               if ( src_stride != dst_stride )
                {
-                       // Allocate the alpha mask
-                       this->alpha = mlt_pool_alloc( this->width * this->height );
-                       if ( !use_cache )
-                               mlt_cache_item_close( this->alpha_cache );
-                       mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.alpha", this->alpha, width * height, mlt_pool_release );
-                       this->alpha_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.alpha" );
-
-                       // Convert the image
-                       mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
-                                                                                 this->width, this->height,
-                                                                                 gdk_pixbuf_get_rowstride( pixbuf ),
-                                                                                 this->image, this->alpha );
+                       int y = this->height;
+                       uint8_t *src = gdk_pixbuf_get_pixels( pixbuf );
+                       uint8_t *dst = this->image;
+                       while ( y-- )
+                       {
+                               memcpy( dst, src, dst_stride );
+                               dst += dst_stride;
+                               src += src_stride;
+                       }
                }
                else
-               { 
-                       // No alpha to extract
-                       mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
-                                                                                this->width, this->height,
-                                                                                gdk_pixbuf_get_rowstride( pixbuf ),
-                                                                                this->image );
+               {
+                       memcpy( this->image, gdk_pixbuf_get_pixels( pixbuf ), src_stride * height );
                }
+               if ( !use_cache )
+                       mlt_cache_item_close( this->image_cache );
+               mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image", this->image, image_size, mlt_pool_release );
+               this->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image" );
+               this->image_idx = image_idx;
 
                // Finished with pixbuf now
                g_object_unref( pixbuf );
@@ -369,7 +427,6 @@ static void refresh_image( producer_pixbuf this, mlt_frame frame, int width, int
        {
                pthread_mutex_unlock( &this->mutex );
                mlt_cache_item_close( this->image_cache );
-               mlt_cache_item_close( this->alpha_cache );
        }
 
        // Set width/height of frame
@@ -386,8 +443,8 @@ static void refresh_image( producer_pixbuf this, mlt_frame frame, int width, int
                mlt_properties_set_int( cached_props, "height", this->height );
                mlt_properties_set_int( cached_props, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
                mlt_properties_set_int( cached_props, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
-               mlt_properties_set_data( cached_props, "image", this->image, this->width * ( this->height + 1 ) * 2, mlt_pool_release, NULL );
-               mlt_properties_set_data( cached_props, "alpha", this->alpha, this->width * this->height, mlt_pool_release, NULL );
+               mlt_frame_set_image( cached, this->image, this->width * ( this->alpha ? 4 : 3 ) * this->height, mlt_pool_release );
+               mlt_properties_set_int( cached_props, "alpha", this->alpha );
                mlt_properties_set_data( cache, image_key, cached, 0, ( mlt_destructor )mlt_frame_close, NULL );
        }
 
@@ -396,6 +453,8 @@ static void refresh_image( producer_pixbuf this, mlt_frame frame, int width, int
 
 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
 {
+       int error = 0;
+       
        // Obtain properties of frame
        mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
 
@@ -405,85 +464,42 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
        *width = mlt_properties_get_int( properties, "rescale_width" );
        *height = mlt_properties_get_int( properties, "rescale_height" );
 
+       mlt_service_lock( MLT_PRODUCER_SERVICE( &this->parent ) );
+
        // Refresh the image
        refresh_image( this, frame, *width, *height );
 
-       // Get the image size
-       int image_size = this->width * ( this->height + 1 ) * 2;
-       int alpha_size = this->width * this->height;
-
        // Get width and height (may have changed during the refresh)
-       *width = mlt_properties_get_int( properties, "width" );
-       *height = mlt_properties_get_int( properties, "height" );
+       *width = this->width;
+       *height = this->height;
 
        // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
        // The fault is not in the design of mlt, but in the implementation of the pixbuf producer...
        if ( this->image )
        {
-               if ( *format == mlt_image_yuv422 || *format == mlt_image_yuv420p )
-               {
-                       // Clone the image and the alpha
-                       uint8_t *image_copy = mlt_pool_alloc( image_size );
-                       uint8_t *alpha_copy = mlt_pool_alloc( alpha_size );
-
-                       memcpy( image_copy, this->image, image_size );
-
-                       // Copy or default the alpha
-                       if ( this->alpha != NULL )
-                               memcpy( alpha_copy, this->alpha, alpha_size );
-                       else
-                               memset( alpha_copy, 255, alpha_size );
-
-                       // Now update properties so we free the copy after
-                       mlt_properties_set_data( properties, "image", image_copy, image_size, mlt_pool_release, NULL );
-                       mlt_properties_set_data( properties, "alpha", alpha_copy, alpha_size, mlt_pool_release, NULL );
-
-                       // We're going to pass the copy on
-                       *buffer = image_copy;
-               }
-               else if ( *format == mlt_image_rgb24a )
-               {
-                       // Clone the image and the alpha
-                       image_size = *width * ( *height + 1 ) * 4;
-                       alpha_size = *width * ( *height + 1 );
-                       uint8_t *image_copy = mlt_pool_alloc( image_size );
-                       uint8_t *alpha_copy = mlt_pool_alloc( alpha_size );
-
-                       mlt_convert_yuv422_to_rgb24a( this->image, image_copy, (*width)*(*height) );
-
-                       // Now update properties so we free the copy after
-                       mlt_properties_set_data( properties, "image", image_copy, image_size, mlt_pool_release, NULL );
-                       mlt_properties_set_data( properties, "alpha", alpha_copy, alpha_size, mlt_pool_release, NULL );
-
-                       // We're going to pass the copy on
-                       *buffer = image_copy;
-               }
-
+               // Clone the image
+               int image_size = this->width * this->height * ( this->alpha ? 4 :3 );
+               uint8_t *image_copy = mlt_pool_alloc( image_size );
+               memcpy( image_copy, this->image, image_size );
+               // Now update properties so we free the copy after
+               mlt_frame_set_image( frame, image_copy, image_size, mlt_pool_release );
+               // We're going to pass the copy on
+               *buffer = image_copy;
+               *format = this->alpha ? mlt_image_rgb24a : mlt_image_rgb24;
+               mlt_log_debug( MLT_PRODUCER_SERVICE( &this->parent ), "%dx%d (%s)\n",
+                       this->width, this->height, mlt_image_format_name( *format ) );
        }
        else
        {
-               // TODO: Review all cases of invalid images
-               *buffer = mlt_pool_alloc( 50 * 50 * 2 );
-               mlt_properties_set_data( properties, "image", *buffer, image_size, mlt_pool_release, NULL );
-               *width = 50;
-               *height = 50;
+               error = 1;
        }
 
        // Release references and locks
        pthread_mutex_unlock( &this->mutex );
        mlt_cache_item_close( this->image_cache );
-       mlt_cache_item_close( this->alpha_cache );
-
-       return 0;
-}
-
-static uint8_t *producer_get_alpha_mask( mlt_frame this )
-{
-       // Obtain properties of frame
-       mlt_properties properties = MLT_FRAME_PROPERTIES( this );
+       mlt_service_unlock( MLT_PRODUCER_SERVICE( &this->parent ) );
 
-       // Return the alpha mask
-       return mlt_properties_get_data( properties, "alpha", NULL );
+       return error;
 }
 
 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
@@ -519,10 +535,12 @@ static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int i
 
                // Set producer-specific frame properties
                mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( producer_properties, "progressive" ) );
-               mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
-
-               // Set alpha call back
-               ( *frame )->get_alpha_mask = producer_get_alpha_mask;
+               
+               double force_ratio = mlt_properties_get_double( producer_properties, "force_aspect_ratio" );
+               if ( force_ratio > 0.0 )
+                       mlt_properties_set_double( properties, "aspect_ratio", force_ratio );
+               else
+                       mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
 
                // Push the get_image method
                mlt_frame_push_get_image( *frame, producer_get_image );
@@ -539,6 +557,7 @@ static void producer_close( mlt_producer parent )
        producer_pixbuf this = parent->child;
        pthread_mutex_destroy( &this->mutex );
        parent->close = NULL;
+       mlt_service_cache_purge( MLT_PRODUCER_SERVICE(parent) );
        mlt_producer_close( parent );
        mlt_properties_close( this->filenames );
        free( this );