Memory pooling
[melted] / src / modules / gtk2 / producer_pixbuf.c
index 9150e36..d77d731 100644 (file)
 #include <unistd.h>
 #include <dirent.h>
 
+typedef struct producer_pixbuf_s *producer_pixbuf;
+
+struct producer_pixbuf_s
+{
+       struct mlt_producer_s parent;
+
+       // File name list
+       char **filenames;
+       int count;
+       int image_idx;
+
+       int width;
+       int height;
+       void *image_release;
+       uint8_t *image;
+       void *alpha_release;
+       uint8_t *alpha;
+};
+
 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
 static void producer_close( mlt_producer parent );
 
@@ -162,16 +181,20 @@ static void refresh_image( mlt_frame frame, int width, int height )
                if ( width != this->width || height != this->height )
                {
                        pixbuf = mlt_properties_get_data( producer_props, "pixbuf", NULL );
-                       free( this->image );
-                       free( this->alpha );
+                       mlt_pool_release( this->image_release );
+                       mlt_pool_release( this->alpha_release );
+                       this->image_release = NULL;
+                       this->alpha_release = NULL;
                        this->image = NULL;
                        this->alpha = NULL;
                }
        }
        else if ( this->image == NULL || image_idx != this->image_idx )
        {
-               free( this->image );
-               free( this->alpha );
+               mlt_pool_release( this->image_release );
+               mlt_pool_release( this->alpha_release );
+               this->image_release = NULL;
+               this->alpha_release = NULL;
                this->image = NULL;
                this->alpha = NULL;
 
@@ -205,8 +228,6 @@ static void refresh_image( mlt_frame frame, int width, int height )
                else if ( strcmp( interps, "hyper" ) == 0 )
                        interp = GDK_INTERP_HYPER;
 
-//             fprintf( stderr, "SCALING PIXBUF from %dx%d to %dx%d was %dx%d\n", gdk_pixbuf_get_width( pixbuf ), gdk_pixbuf_get_height( pixbuf ), width, height, this->width, this->height );
-               
                // Note - the original pixbuf is already safe and ready for destruction
                pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
                
@@ -215,21 +236,19 @@ static void refresh_image( mlt_frame frame, int width, int height )
                this->height = height;
                
                // Allocate/define image
-               // IRRIGATE ME
-               uint8_t *image = malloc( width * ( height + 1 ) * 2 );
-               uint8_t *alpha = NULL;
+               this->image = mlt_pool_allocate( width * ( height + 1 ) * 2, &this->image_release );
 
                // Extract YUV422 and alpha
                if ( gdk_pixbuf_get_has_alpha( pixbuf ) )
                {
                        // Allocate the alpha mask
-                       alpha = malloc( this->width * this->height );
+                       this->alpha = mlt_pool_allocate( this->width * this->height, &this->alpha_release );
 
                        // Convert the image
                        mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
                                                                                  this->width, this->height,
                                                                                  gdk_pixbuf_get_rowstride( pixbuf ),
-                                                                                 image, alpha );
+                                                                                 this->image, this->alpha );
                }
                else
                { 
@@ -237,15 +256,11 @@ static void refresh_image( mlt_frame frame, int width, int height )
                        mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
                                                                                 this->width, this->height,
                                                                                 gdk_pixbuf_get_rowstride( pixbuf ),
-                                                                                image );
+                                                                                this->image );
                }
 
                // Finished with pixbuf now
                g_object_unref( pixbuf );
-
-               // Assign images to producer
-               this->image = image;
-               this->alpha = alpha;
        }
 
        // Set width/height of frame
@@ -290,15 +305,16 @@ static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_form
        //if ( writable )
        {
                // Clone our image
-               // IRRIGATE ME
-               uint8_t *copy = malloc( size );
+               void *release = NULL;
+               uint8_t *copy = mlt_pool_allocate( size, &release );
                memcpy( copy, image, size );
 
                // We're going to pass the copy on
                image = copy;
 
                // Now update properties so we free the copy after
-               mlt_properties_set_data( properties, "image", copy, size, free, NULL );
+               mlt_properties_set_data( properties, "image_release", release, 0, mlt_pool_release, NULL );
+               mlt_properties_set_data( properties, "image", copy, size, NULL, NULL );
        }
 
        // Pass on the image