]> git.sesse.net Git - mlt/blob - src/modules/gtk2/producer_pixbuf.c
Fix crashing when using opengl services with wrapper producers.
[mlt] / src / modules / gtk2 / producer_pixbuf.c
1 /*
2  * producer_pixbuf.c -- raster image loader based upon gdk-pixbuf
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <framework/mlt_producer.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_cache.h>
24 #include <framework/mlt_log.h>
25 #include <framework/mlt_tokeniser.h>
26 #include <gdk-pixbuf/gdk-pixbuf.h>
27
28 #ifdef USE_EXIF
29 #include <libexif/exif-data.h>
30 #endif
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <pthread.h>
36 #include <math.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
40 #include <dirent.h>
41 #include <ctype.h>
42
43 // this protects concurrent access to gdk_pixbuf
44 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
45
46 typedef struct producer_pixbuf_s *producer_pixbuf;
47
48 struct producer_pixbuf_s
49 {
50         struct mlt_producer_s parent;
51
52         // File name list
53         mlt_properties filenames;
54         int count;
55         int image_idx;
56         int pixbuf_idx;
57         int width;
58         int height;
59         uint8_t *alpha;
60         uint8_t *image;
61         mlt_cache_item image_cache;
62         mlt_cache_item alpha_cache;
63         mlt_cache_item pixbuf_cache;
64         GdkPixbuf *pixbuf;
65         mlt_image_format format;
66 };
67
68 static void load_filenames( producer_pixbuf self, mlt_properties producer_properties );
69 static int refresh_pixbuf( producer_pixbuf self, mlt_frame frame );
70 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
71 static void producer_close( mlt_producer parent );
72
73 mlt_producer producer_pixbuf_init( char *filename )
74 {
75         producer_pixbuf self = calloc( 1, sizeof( struct producer_pixbuf_s ) );
76         if ( self != NULL && mlt_producer_init( &self->parent, self ) == 0 )
77         {
78                 mlt_producer producer = &self->parent;
79
80                 // Get the properties interface
81                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &self->parent );
82         
83                 // Callback registration
84                 producer->get_frame = producer_get_frame;
85                 producer->close = ( mlt_destructor )producer_close;
86
87                 // Set the default properties
88                 mlt_properties_set( properties, "resource", filename );
89                 mlt_properties_set_int( properties, "ttl", 25 );
90                 mlt_properties_set_int( properties, "aspect_ratio", 1 );
91                 mlt_properties_set_int( properties, "progressive", 1 );
92                 mlt_properties_set_int( properties, "seekable", 1 );
93                 mlt_properties_set_int( properties, "loop", 1 );
94
95                 // Validate the resource
96                 if ( filename )
97                         load_filenames( self, properties );
98                 if ( self->count )
99                 {
100                         mlt_frame frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
101                         if ( frame )
102                         {
103                                 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
104                                 mlt_properties_set_data( frame_properties, "producer_pixbuf", self, 0, NULL, NULL );
105                                 mlt_frame_set_position( frame, mlt_producer_position( producer ) );
106                                 refresh_pixbuf( self, frame );
107                                 mlt_cache_item_close( self->pixbuf_cache );
108                                 mlt_frame_close( frame );
109                         }
110                 }
111                 if ( self->width == 0 )
112                 {
113                         producer_close( producer );
114                         producer = NULL;
115                 }
116                 return producer;
117         }
118         free( self );
119         return NULL;
120 }
121
122 static int load_svg( producer_pixbuf self, mlt_properties properties, const char *filename )
123 {
124         int result = 0;
125
126         // Read xml string
127         if ( strstr( filename, "<svg" ) )
128         {
129                 // Generate a temporary file for the svg
130                 char fullname[ 1024 ] = "/tmp/mlt.XXXXXX";
131                 int fd = g_mkstemp( fullname );
132
133                 if ( fd > -1 )
134                 {
135                         // Write the svg into the temp file
136                         ssize_t remaining_bytes;
137                         const char *xml = filename;
138
139                         // Strip leading crap
140                         while ( xml[0] != '<' )
141                                 xml++;
142
143                         remaining_bytes = strlen( xml );
144                         while ( remaining_bytes > 0 )
145                                 remaining_bytes -= write( fd, xml + strlen( xml ) - remaining_bytes, remaining_bytes );
146                         close( fd );
147
148                         mlt_properties_set( self->filenames, "0", fullname );
149
150                         // Teehe - when the producer closes, delete the temp file and the space allo
151                         mlt_properties_set_data( properties, "__temporary_file__", fullname, 0, ( mlt_destructor )unlink, NULL );
152                         result = 1;
153                 }
154         }
155         return result;
156 }
157
158 static int load_sequence_sprintf( producer_pixbuf self, mlt_properties properties, const char *filename )
159 {
160         int result = 0;
161
162         // Obtain filenames with pattern
163         if ( strchr( filename, '%' ) != NULL )
164         {
165                 // handle picture sequences
166                 int i = mlt_properties_get_int( properties, "begin" );
167                 int gap = 0;
168                 char full[1024];
169                 int keyvalue = 0;
170                 char key[ 50 ];
171
172                 while ( gap < 100 )
173                 {
174                         struct stat buf;
175                         snprintf( full, 1023, filename, i ++ );
176                         if ( stat( full, &buf ) == 0 )
177                         {
178                                 sprintf( key, "%d", keyvalue ++ );
179                                 mlt_properties_set( self->filenames, key, full );
180                                 gap = 0;
181                         }
182                         else
183                         {
184                                 gap ++;
185                         }
186                 }
187                 if ( mlt_properties_count( self->filenames ) > 0 )
188                 {
189                         mlt_properties_set_int( properties, "ttl", 1 );
190                         result = 1;
191                 }
192         }
193         return result;
194 }
195
196 static int load_sequence_deprecated( producer_pixbuf self, mlt_properties properties, const char *filename )
197 {
198         int result = 0;
199         const char *start;
200
201         // This approach is deprecated in favor of the begin querystring parameter.
202         // Obtain filenames with pattern containing a begin value, e.g. foo%1234d.png
203         if ( ( start = strchr( filename, '%' ) ) )
204         {
205                 const char *end = ++start;
206                 while ( isdigit( *end ) ) end++;
207                 if ( end > start && ( end[0] == 'd' || end[0] == 'i' || end[0] == 'u' ) )
208                 {
209                         int n = end - start;
210                         char *s = calloc( 1, n + 1 );
211                         strncpy( s, start, n );
212                         mlt_properties_set( properties, "begin", s );
213                         free( s );
214                         s = calloc( 1, strlen( filename ) + 2 );
215                         strncpy( s, filename, start - filename );
216                         sprintf( s + ( start - filename ), ".%d%s", n, end );
217                         result = load_sequence_sprintf( self, properties, s );
218                         free( s );
219                 }
220         }
221         return result;
222 }
223
224 static int load_sequence_querystring( producer_pixbuf self, mlt_properties properties, const char *filename )
225 {
226         int result = 0;
227
228         // Obtain filenames with pattern and begin value in query string
229         if ( strchr( filename, '%' ) && strchr( filename, '?' ) )
230         {
231                 // Split filename into pattern and query string
232                 char *s = strdup( filename );
233                 char *querystring = strrchr( s, '?' );
234                 *querystring++ = '\0';
235                 if ( strstr( filename, "begin=" ) )
236                         mlt_properties_set( properties, "begin", strstr( querystring, "begin=" ) + 6 );
237                 else if ( strstr( filename, "begin:" ) )
238                         mlt_properties_set( properties, "begin", strstr( querystring, "begin:" ) + 6 );
239                 // Coerce to an int value so serialization does not have any extra query string cruft
240                 mlt_properties_set_int( properties, "begin", mlt_properties_get_int( properties, "begin" ) );
241                 result = load_sequence_sprintf( self, properties, s );
242                 free( s );
243         }
244         return result;
245 }
246
247 static int load_folder( producer_pixbuf self, mlt_properties properties, const char *filename )
248 {
249         int result = 0;
250
251         // Obtain filenames with folder
252         if ( strstr( filename, "/.all." ) != NULL )
253         {
254                 char wildcard[ 1024 ];
255                 char *dir_name = strdup( filename );
256                 char *extension = strrchr( dir_name, '.' );
257
258                 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
259                 sprintf( wildcard, "*%s", extension );
260
261                 mlt_properties_dir_list( self->filenames, dir_name, wildcard, 1 );
262
263                 free( dir_name );
264                 result = 1;
265         }
266         return result;
267 }
268
269 static void load_filenames( producer_pixbuf self, mlt_properties properties )
270 {
271         char *filename = mlt_properties_get( properties, "resource" );
272         self->filenames = mlt_properties_new( );
273
274         if (!load_svg( self, properties, filename ) &&
275                 !load_sequence_querystring( self, properties, filename ) &&
276                 !load_sequence_sprintf( self, properties, filename ) &&
277                 !load_sequence_deprecated( self, properties, filename ) &&
278                 !load_folder( self, properties, filename ) )
279         {
280                 mlt_properties_set( self->filenames, "0", filename );
281         }
282         self->count = mlt_properties_count( self->filenames );
283 }
284
285 static GdkPixbuf* reorient_with_exif( producer_pixbuf self, int image_idx, GdkPixbuf *pixbuf )
286 {
287 #ifdef USE_EXIF
288         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( &self->parent );
289         ExifData *d = exif_data_new_from_file( mlt_properties_get_value( self->filenames, image_idx ) );
290         ExifEntry *entry;
291         int exif_orientation = 0;
292
293         /* get orientation and rotate image accordingly if necessary */
294         if (d)
295         {
296                 if ( ( entry = exif_content_get_entry ( d->ifd[EXIF_IFD_0], EXIF_TAG_ORIENTATION ) ) )
297                         exif_orientation = exif_get_short (entry->data, exif_data_get_byte_order (d));
298
299                 /* Free the EXIF data */
300                 exif_data_unref(d);
301         }
302
303         // Remember EXIF value, might be useful for someone
304         mlt_properties_set_int( producer_props, "_exif_orientation" , exif_orientation );
305
306         if ( exif_orientation > 1 )
307         {
308                 GdkPixbuf *processed = NULL;
309                 GdkPixbufRotation matrix = GDK_PIXBUF_ROTATE_NONE;
310
311                 // Rotate image according to exif data
312                 switch ( exif_orientation ) {
313                   case 2:
314                           processed = gdk_pixbuf_flip ( pixbuf, TRUE );
315                           break;
316                   case 3:
317                           matrix = GDK_PIXBUF_ROTATE_UPSIDEDOWN;
318                           processed = pixbuf;
319                           break;
320                   case 4:
321                           processed = gdk_pixbuf_flip ( pixbuf, FALSE );
322                           break;
323                   case 5:
324                           matrix = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
325                           processed = gdk_pixbuf_flip ( pixbuf, TRUE );
326                           break;
327                   case 6:
328                           matrix = GDK_PIXBUF_ROTATE_CLOCKWISE;
329                           processed = pixbuf;
330                           break;
331                   case 7:
332                           matrix = GDK_PIXBUF_ROTATE_CLOCKWISE;
333                           processed = gdk_pixbuf_flip ( pixbuf, TRUE );
334                           break;
335                   case 8:
336                           matrix = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
337                           processed = pixbuf;
338                           break;
339                 }
340                 if ( processed )
341                 {
342                         pixbuf = gdk_pixbuf_rotate_simple( processed, matrix );
343                         g_object_unref( processed );
344                 }
345         }
346 #endif
347         return pixbuf;
348 }
349
350 static int refresh_pixbuf( producer_pixbuf self, mlt_frame frame )
351 {
352         // Obtain properties of frame and producer
353         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
354         mlt_producer producer = &self->parent;
355         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
356
357         // Check if user wants us to reload the image
358         if ( mlt_properties_get_int( producer_props, "force_reload" ) )
359         {
360                 self->pixbuf = NULL;
361                 self->image = NULL;
362                 mlt_properties_set_int( producer_props, "force_reload", 0 );
363         }
364
365         // Get the time to live for each frame
366         double ttl = mlt_properties_get_int( producer_props, "ttl" );
367
368         // Get the original position of this frame
369         mlt_position position = mlt_frame_original_position( frame );
370         position += mlt_producer_get_in( producer );
371
372         // Image index
373         int loop = mlt_properties_get_int( producer_props, "loop" );
374         int current_idx;
375         if (loop) {
376                 current_idx = ( int )floor( ( double )position / ttl ) % self->count;
377         } else {
378                 current_idx = MIN(( double )position / ttl, self->count - 1);
379         }
380
381         // Key for the cache
382         char image_key[ 10 ];
383         sprintf( image_key, "%d", current_idx );
384
385         int disable_exif = mlt_properties_get_int( producer_props, "disable_exif" );
386
387         if ( current_idx != self->pixbuf_idx )
388                 self->pixbuf = NULL;
389         if ( !self->pixbuf || mlt_properties_get_int( producer_props, "_disable_exif" ) != disable_exif )
390         {
391                 GError *error = NULL;
392
393                 self->image = NULL;
394                 pthread_mutex_lock( &g_mutex );
395                 self->pixbuf = gdk_pixbuf_new_from_file( mlt_properties_get_value( self->filenames, current_idx ), &error );
396                 if ( self->pixbuf )
397                 {
398                         // Read the exif value for this file
399                         if ( !disable_exif )
400                                 self->pixbuf = reorient_with_exif( self, current_idx, self->pixbuf );
401
402                         // Register this pixbuf for destruction and reuse
403                         mlt_cache_item_close( self->pixbuf_cache );
404                         mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf", self->pixbuf, 0, ( mlt_destructor )g_object_unref );
405                         self->pixbuf_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf" );
406                         self->pixbuf_idx = current_idx;
407
408                         // Store the width/height of the pixbuf temporarily
409                         self->width = gdk_pixbuf_get_width( self->pixbuf );
410                         self->height = gdk_pixbuf_get_height( self->pixbuf );
411
412                         mlt_events_block( producer_props, NULL );
413                         mlt_properties_set_int( producer_props, "meta.media.width", self->width );
414                         mlt_properties_set_int( producer_props, "meta.media.height", self->height );
415                         mlt_properties_set_int( producer_props, "_disable_exif", disable_exif );
416                         mlt_events_unblock( producer_props, NULL );
417
418                 }
419                 pthread_mutex_unlock( &g_mutex );
420         }
421
422         // Set width/height of frame
423         mlt_properties_set_int( properties, "width", self->width );
424         mlt_properties_set_int( properties, "height", self->height );
425
426         return current_idx;
427 }
428
429 static void refresh_image( producer_pixbuf self, mlt_frame frame, mlt_image_format format, int width, int height )
430 {
431         // Obtain properties of frame and producer
432         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
433         mlt_producer producer = &self->parent;
434
435         // Get index and pixbuf
436         int current_idx = refresh_pixbuf( self, frame );
437
438         // optimization for subsequent iterations on single picture
439         if ( current_idx != self->image_idx || width != self->width || height != self->height )
440                 self->image = NULL;
441         mlt_log_debug( MLT_PRODUCER_SERVICE( producer ), "image %p pixbuf %p idx %d current_idx %d pixbuf_idx %d width %d\n",
442                 self->image, self->pixbuf, current_idx, self->image_idx, self->pixbuf_idx, width );
443
444         // If we have a pixbuf and we need an image
445         if ( self->pixbuf && ( !self->image || ( format != mlt_image_none && format != mlt_image_glsl && format != self->format ) ) )
446         {
447                 char *interps = mlt_properties_get( properties, "rescale.interp" );
448                 if ( interps ) interps = strdup( interps );
449                 int interp = GDK_INTERP_BILINEAR;
450
451                 if ( !interps ) {
452                         // Keep bilinear by default
453                 }
454                 else if ( strcmp( interps, "nearest" ) == 0 )
455                         interp = GDK_INTERP_NEAREST;
456                 else if ( strcmp( interps, "tiles" ) == 0 )
457                         interp = GDK_INTERP_TILES;
458                 else if ( strcmp( interps, "hyper" ) == 0 || strcmp( interps, "bicubic" ) == 0 )
459                         interp = GDK_INTERP_HYPER;
460                 if ( interps ) free( interps );
461
462                 // Note - the original pixbuf is already safe and ready for destruction
463                 pthread_mutex_lock( &g_mutex );
464                 GdkPixbuf* pixbuf = gdk_pixbuf_scale_simple( self->pixbuf, width, height, interp );
465
466                 // Store width and height
467                 self->width = width;
468                 self->height = height;
469
470                 // Allocate/define image
471                 int has_alpha = gdk_pixbuf_get_has_alpha( pixbuf );
472                 int src_stride = gdk_pixbuf_get_rowstride( pixbuf );
473                 int dst_stride = self->width * ( has_alpha ? 4 : 3 );
474                 int image_size = dst_stride * ( height + 1 );
475                 self->image = mlt_pool_alloc( image_size );
476                 self->alpha = NULL;
477                 self->format = has_alpha ? mlt_image_rgb24a : mlt_image_rgb24;
478
479                 if ( src_stride != dst_stride )
480                 {
481                         int y = self->height;
482                         uint8_t *src = gdk_pixbuf_get_pixels( pixbuf );
483                         uint8_t *dst = self->image;
484                         while ( y-- )
485                         {
486                                 memcpy( dst, src, dst_stride );
487                                 dst += dst_stride;
488                                 src += src_stride;
489                         }
490                 }
491                 else
492                 {
493                         memcpy( self->image, gdk_pixbuf_get_pixels( pixbuf ), src_stride * height );
494                 }
495                 pthread_mutex_unlock( &g_mutex );
496
497                 // Convert image to requested format
498                 if ( format != mlt_image_none && format != mlt_image_glsl && format != self->format )
499                 {
500                         uint8_t *buffer = NULL;
501
502                         // First, set the image so it can be converted when we get it
503                         mlt_frame_replace_image( frame, self->image, self->format, width, height );
504                         mlt_frame_set_image( frame, self->image, image_size, mlt_pool_release );
505                         self->format = format;
506
507                         // get_image will do the format conversion
508                         mlt_frame_get_image( frame, &buffer, &format, &width, &height, 0 );
509
510                         // cache copies of the image and alpha buffers
511                         if ( buffer )
512                         {
513                                 image_size = mlt_image_format_size( format, width, height, NULL );
514                                 self->image = mlt_pool_alloc( image_size );
515                                 memcpy( self->image, buffer, image_size );
516                         }
517                         if ( ( buffer = mlt_frame_get_alpha_mask( frame ) ) )
518                         {
519                                 self->alpha = mlt_pool_alloc( width * height );
520                                 memcpy( self->alpha, buffer, width * height );
521                         }
522                 }
523
524                 // Update the cache
525                 mlt_cache_item_close( self->image_cache );
526                 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image", self->image, image_size, mlt_pool_release );
527                 self->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image" );
528                 self->image_idx = current_idx;
529                 mlt_cache_item_close( self->alpha_cache );
530                 self->alpha_cache = NULL;
531                 if ( self->alpha )
532                 {
533                         mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.alpha", self->alpha, width * height, mlt_pool_release );
534                         self->alpha_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.alpha" );
535                 }
536
537                 // Finished with pixbuf now
538                 g_object_unref( pixbuf );
539         }
540
541         // Set width/height of frame
542         mlt_properties_set_int( properties, "width", self->width );
543         mlt_properties_set_int( properties, "height", self->height );
544 }
545
546 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
547 {
548         int error = 0;
549         
550         // Obtain properties of frame and producer
551         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
552         producer_pixbuf self = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
553         mlt_producer producer = &self->parent;
554
555         // Use the width and height suggested by the rescale filter because we can do our own scaling.
556         if ( mlt_properties_get_int( properties, "rescale_width" ) > 0 )
557                 *width = mlt_properties_get_int( properties, "rescale_width" );
558         if ( mlt_properties_get_int( properties, "rescale_height" ) > 0 )
559                 *height = mlt_properties_get_int( properties, "rescale_height" );
560
561         // Restore pixbuf and image
562         mlt_service_lock( MLT_PRODUCER_SERVICE( producer ) );
563         self->pixbuf_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf" );
564         self->pixbuf = mlt_cache_item_data( self->pixbuf_cache, NULL );
565         self->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image" );
566         self->image = mlt_cache_item_data( self->image_cache, NULL );
567         self->alpha_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.alpha" );
568         self->alpha = mlt_cache_item_data( self->alpha_cache, NULL );
569
570         // Refresh the image
571         refresh_image( self, frame, *format, *width, *height );
572
573         // Get width and height (may have changed during the refresh)
574         *width = self->width;
575         *height = self->height;
576         *format = self->format;
577
578         // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
579         // The fault is not in the design of mlt, but in the implementation of the pixbuf producer...
580         if ( self->image )
581         {
582                 // Clone the image
583                 int image_size = mlt_image_format_size( self->format, self->width, self->height, NULL );
584                 uint8_t *image_copy = mlt_pool_alloc( image_size );
585                 memcpy( image_copy, self->image, image_size );
586                 // Now update properties so we free the copy after
587                 mlt_frame_set_image( frame, image_copy, image_size, mlt_pool_release );
588                 // We're going to pass the copy on
589                 *buffer = image_copy;
590                 mlt_log_debug( MLT_PRODUCER_SERVICE( &self->parent ), "%dx%d (%s)\n",
591                         self->width, self->height, mlt_image_format_name( *format ) );
592                 // Clone the alpha channel
593                 if ( self->alpha )
594                 {
595                         image_copy = mlt_pool_alloc( self->width * self->height );
596                         memcpy( image_copy, self->alpha, self->width * self->height );
597                         mlt_frame_set_alpha( frame, image_copy, self->width * self->height, mlt_pool_release );
598                 }
599         }
600         else
601         {
602                 error = 1;
603         }
604
605         // Release references and locks
606         mlt_cache_item_close( self->pixbuf_cache );
607         mlt_cache_item_close( self->image_cache );
608         mlt_cache_item_close( self->alpha_cache );
609         mlt_service_unlock( MLT_PRODUCER_SERVICE( &self->parent ) );
610
611         return error;
612 }
613
614 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
615 {
616         // Get the real structure for this producer
617         producer_pixbuf self = producer->child;
618
619         // Fetch the producers properties
620         mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
621
622         if ( self->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
623                 load_filenames( self, producer_properties );
624
625         // Generate a frame
626         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
627
628         if ( *frame != NULL && self->count > 0 )
629         {
630                 // Obtain properties of frame and producer
631                 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
632
633                 // Set the producer on the frame properties
634                 mlt_properties_set_data( properties, "producer_pixbuf", self, 0, NULL, NULL );
635
636                 // Update timecode on the frame we're creating
637                 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
638
639                 // Refresh the pixbuf
640                 self->pixbuf_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf" );
641                 self->pixbuf = mlt_cache_item_data( self->pixbuf_cache, NULL );
642                 refresh_pixbuf( self, *frame );
643                 mlt_cache_item_close( self->pixbuf_cache );
644
645                 // Set producer-specific frame properties
646                 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( producer_properties, "progressive" ) );
647                 
648                 double force_ratio = mlt_properties_get_double( producer_properties, "force_aspect_ratio" );
649                 if ( force_ratio > 0.0 )
650                         mlt_properties_set_double( properties, "aspect_ratio", force_ratio );
651                 else
652                         mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
653
654                 // Push the get_image method
655                 mlt_frame_push_get_image( *frame, producer_get_image );
656         }
657
658         // Calculate the next timecode
659         mlt_producer_prepare_next( producer );
660
661         return 0;
662 }
663
664 static void producer_close( mlt_producer parent )
665 {
666         producer_pixbuf self = parent->child;
667         parent->close = NULL;
668         mlt_service_cache_purge( MLT_PRODUCER_SERVICE(parent) );
669         mlt_producer_close( parent );
670         mlt_properties_close( self->filenames );
671         free( self );
672 }