]> git.sesse.net Git - mlt/blob - src/modules/gtk2/producer_pixbuf.c
Cleanup & fix memleak
[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 <gdk-pixbuf/gdk-pixbuf.h>
26
27 #include "config.h"
28
29 #ifdef USE_EXIF
30 #include <exif-data.h>
31 #endif
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <pthread.h>
37 #include <math.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <unistd.h>
41 #include <dirent.h>
42
43 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
44
45 typedef struct producer_pixbuf_s *producer_pixbuf;
46
47 struct producer_pixbuf_s
48 {
49         struct mlt_producer_s parent;
50
51         // File name list
52         mlt_properties filenames;
53         int count;
54         int image_idx;
55         int pixbuf_idx;
56         int width;
57         int height;
58         int alpha;
59         uint8_t *image;
60         mlt_cache_item image_cache;
61         pthread_mutex_t mutex;
62 };
63
64 static void load_filenames( producer_pixbuf this, mlt_properties producer_properties );
65 static void refresh_image( producer_pixbuf this, mlt_frame frame, int width, int height );
66 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
67 static void producer_close( mlt_producer parent );
68
69 mlt_producer producer_pixbuf_init( char *filename )
70 {
71         producer_pixbuf this = calloc( sizeof( struct producer_pixbuf_s ), 1 );
72         if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
73         {
74                 mlt_producer producer = &this->parent;
75
76                 // Get the properties interface
77                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
78         
79                 // Callback registration
80                 producer->get_frame = producer_get_frame;
81                 producer->close = ( mlt_destructor )producer_close;
82
83                 // Set the default properties
84                 mlt_properties_set( properties, "resource", filename );
85                 mlt_properties_set_int( properties, "ttl", 25 );
86                 mlt_properties_set_int( properties, "aspect_ratio", 1 );
87                 mlt_properties_set_int( properties, "progressive", 1 );
88
89                 // Validate the resource
90                 if ( filename )
91                         load_filenames( this, properties );
92                 if ( this->count )
93                 {
94                         mlt_frame frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
95                         if ( frame )
96                         {
97                                 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
98                                 pthread_mutex_init( &this->mutex, NULL );
99                                 mlt_properties_set_data( frame_properties, "producer_pixbuf", this, 0, NULL, NULL );
100                                 mlt_frame_set_position( frame, mlt_producer_position( producer ) );
101                                 mlt_properties_set_position( frame_properties, "pixbuf_position", mlt_producer_position( producer ) );
102                                 refresh_image( this, frame, 0, 0 );
103                                 mlt_frame_close( frame );
104                         }
105                 }
106                 if ( this->width == 0 )
107                 {
108                         producer_close( producer );
109                         producer = NULL;
110                 }
111                 return producer;
112         }
113         free( this );
114         return NULL;
115 }
116
117 static void load_filenames( producer_pixbuf this, mlt_properties producer_properties )
118 {
119         char *filename = mlt_properties_get( producer_properties, "resource" );
120         this->filenames = mlt_properties_new( );
121
122         // Read xml string
123         if ( strstr( filename, "<svg" ) )
124         {
125                 // Generate a temporary file for the svg
126                 char fullname[ 1024 ] = "/tmp/mlt.XXXXXX";
127                 int fd = mkstemp( fullname );
128
129                 if ( fd > -1 )
130                 {
131                         // Write the svg into the temp file
132                         ssize_t remaining_bytes;
133                         char *xml = filename;
134                         
135                         // Strip leading crap
136                         while ( xml[0] != '<' )
137                                 xml++;
138                         
139                         remaining_bytes = strlen( xml );
140                         while ( remaining_bytes > 0 )
141                                 remaining_bytes -= write( fd, xml + strlen( xml ) - remaining_bytes, remaining_bytes );
142                         close( fd );
143
144                         mlt_properties_set( this->filenames, "0", fullname );
145
146                         // Teehe - when the producer closes, delete the temp file and the space allo
147                         mlt_properties_set_data( producer_properties, "__temporary_file__", fullname, 0, ( mlt_destructor )unlink, NULL );
148                 }
149         }
150         // Obtain filenames
151         else if ( strchr( filename, '%' ) != NULL )
152         {
153                 // handle picture sequences
154                 int i = mlt_properties_get_int( producer_properties, "begin" );
155                 int gap = 0;
156                 char full[1024];
157                 int keyvalue = 0;
158                 char key[ 50 ];
159
160                 while ( gap < 100 )
161                 {
162                         struct stat buf;
163                         snprintf( full, 1023, filename, i ++ );
164                         if ( stat( full, &buf ) == 0 )
165                         {
166                                 sprintf( key, "%d", keyvalue ++ );
167                                 mlt_properties_set( this->filenames, key, full );
168                                 gap = 0;
169                         }
170                         else
171                         {
172                                 gap ++;
173                         }
174                 }
175                 if ( mlt_properties_count( this->filenames ) > 0 )
176                         mlt_properties_set_int( producer_properties, "ttl", 1 );
177         }
178         else if ( strstr( filename, "/.all." ) != NULL )
179         {
180                 char wildcard[ 1024 ];
181                 char *dir_name = strdup( filename );
182                 char *extension = strrchr( dir_name, '.' );
183
184                 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
185                 sprintf( wildcard, "*%s", extension );
186
187                 mlt_properties_dir_list( this->filenames, dir_name, wildcard, 1 );
188
189                 free( dir_name );
190         }
191         else
192         {
193                 mlt_properties_set( this->filenames, "0", filename );
194         }
195
196         this->count = mlt_properties_count( this->filenames );
197 }
198
199 static void refresh_image( producer_pixbuf this, mlt_frame frame, int width, int height )
200 {
201         // Obtain properties of frame
202         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
203
204         // Obtain the producer
205         mlt_producer producer = &this->parent;
206
207         // Obtain properties of producer
208         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
209
210         // Obtain the cache flag and structure
211         int use_cache = mlt_properties_get_int( producer_props, "cache" );
212         mlt_properties cache = mlt_properties_get_data( producer_props, "_cache", NULL );
213         int update_cache = 0;
214
215         // restore GdkPixbuf
216         pthread_mutex_lock( &this->mutex );
217         mlt_cache_item pixbuf_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf" );
218         GdkPixbuf *pixbuf = mlt_cache_item_data( pixbuf_cache, NULL );
219         GError *error = NULL;
220
221         // restore scaled image
222         this->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image" );
223         this->image = mlt_cache_item_data( this->image_cache, NULL );
224
225         // Check if user wants us to reload the image
226         if ( mlt_properties_get_int( producer_props, "force_reload" ) )
227         {
228                 pixbuf = NULL;
229                 this->image = NULL;
230                 mlt_properties_set_int( producer_props, "force_reload", 0 );
231         }
232
233         // Get the time to live for each frame
234         double ttl = mlt_properties_get_int( producer_props, "ttl" );
235
236         // Get the original position of this frame
237         mlt_position position = mlt_properties_get_position( properties, "pixbuf_position" );
238         position += mlt_producer_get_in( producer );
239
240         // Image index
241         int image_idx = ( int )floor( ( double )position / ttl ) % this->count;
242
243         // Key for the cache
244         char image_key[ 10 ];
245         sprintf( image_key, "%d", image_idx );
246
247         pthread_mutex_lock( &g_mutex );
248
249         // Check if the frame is already loaded
250         if ( use_cache )
251         {
252                 if ( cache == NULL )
253                 {
254                         cache = mlt_properties_new( );
255                         mlt_properties_set_data( producer_props, "_cache", cache, 0, ( mlt_destructor )mlt_properties_close, NULL );
256                 }
257
258                 mlt_frame cached = mlt_properties_get_data( cache, image_key, NULL );
259
260                 if ( cached )
261                 {
262                         this->image_idx = image_idx;
263                         mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
264                         this->width = mlt_properties_get_int( cached_props, "width" );
265                         this->height = mlt_properties_get_int( cached_props, "height" );
266                         mlt_properties_set_int( producer_props, "_real_width", mlt_properties_get_int( cached_props, "real_width" ) );
267                         mlt_properties_set_int( producer_props, "_real_height", mlt_properties_get_int( cached_props, "real_height" ) );
268                         this->image = mlt_properties_get_data( cached_props, "image", NULL );
269                         this->alpha = mlt_properties_get_int( cached_props, "alpha" );
270
271                         if ( width != 0 && ( width != this->width || height != this->height ) )
272                                 this->image = NULL;
273                 }
274         }
275         int disable_exif = mlt_properties_get_int( producer_props, "disable_exif" );
276         
277     // optimization for subsequent iterations on single picture
278         if ( width != 0 && ( image_idx != this->image_idx || width != this->width || height != this->height ) )
279                 this->image = NULL;
280         if ( image_idx != this->pixbuf_idx )
281                 pixbuf = NULL;
282         mlt_log_debug( MLT_PRODUCER_SERVICE( producer ), "image %p pixbuf %p idx %d image_idx %d pixbuf_idx %d width %d\n",
283                 this->image, pixbuf, image_idx, this->image_idx, this->pixbuf_idx, width );
284         if ( ( !pixbuf && !this->image ) || mlt_properties_get_int( producer_props, "_disable_exif" ) != disable_exif )
285         {
286                 this->image = NULL;
287                 pixbuf = gdk_pixbuf_new_from_file( mlt_properties_get_value( this->filenames, image_idx ), &error );
288
289                 if ( pixbuf )
290                 {
291 #ifdef USE_EXIF
292                         // Read the exif value for this file
293                         if ( disable_exif == 0) {
294                                 ExifData *d = exif_data_new_from_file( mlt_properties_get_value( this->filenames, image_idx ) );
295                                 ExifEntry *entry;
296                                 int exif_orientation = 0;
297
298                                 /* get orientation and rotate image accordingly if necessary */
299                                 if (d) {
300                                         if ( ( entry = exif_content_get_entry ( d->ifd[EXIF_IFD_0], EXIF_TAG_ORIENTATION ) ) )
301                                                 exif_orientation = exif_get_short (entry->data, exif_data_get_byte_order (d));
302
303                                         /* Free the EXIF data */
304                                         exif_data_unref(d);
305                                 }
306
307                                 if ( exif_orientation > 1 )
308                                 {
309                                         GdkPixbuf *processed;
310                                         GdkPixbufRotation matrix = GDK_PIXBUF_ROTATE_NONE;
311
312                                         // Rotate image according to exif data
313                                         switch ( exif_orientation ) {
314                                           case 2:
315                                               processed = gdk_pixbuf_flip ( pixbuf, TRUE );
316                                               break;
317                                           case 3:
318                                               matrix = GDK_PIXBUF_ROTATE_UPSIDEDOWN;
319                                               processed = pixbuf;
320                                               break;
321                                           case 4:
322                                               processed = gdk_pixbuf_flip ( pixbuf, FALSE );
323                                               break;
324                                           case 5:
325                                               matrix = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
326                                               processed = gdk_pixbuf_flip ( pixbuf, TRUE );
327                                               break;
328                                           case 6:
329                                               matrix = GDK_PIXBUF_ROTATE_CLOCKWISE;
330                                               processed = pixbuf;
331                                               break;
332                                           case 7:
333                                               matrix = GDK_PIXBUF_ROTATE_CLOCKWISE;
334                                               processed = gdk_pixbuf_flip ( pixbuf, TRUE );
335                                               break;
336                                           case 8:
337                                               matrix = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
338                                               processed = pixbuf;
339                                               break;
340                                         }
341                                         pixbuf = gdk_pixbuf_rotate_simple( processed, matrix );
342                                 }
343                         }
344 #endif
345
346                         // Register this pixbuf for destruction and reuse
347                         mlt_cache_item_close( pixbuf_cache );
348                         mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref );
349                         pixbuf_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf" );
350                         this->pixbuf_idx = image_idx;
351
352                         mlt_events_block( producer_props, NULL );
353                         mlt_properties_set_int( producer_props, "_real_width", gdk_pixbuf_get_width( pixbuf ) );
354                         mlt_properties_set_int( producer_props, "_real_height", gdk_pixbuf_get_height( pixbuf ) );
355                         mlt_properties_set_int( producer_props, "_disable_exif", disable_exif );
356                         mlt_events_unblock( producer_props, NULL );
357
358                         // Store the width/height of the pixbuf temporarily
359                         this->width = gdk_pixbuf_get_width( pixbuf );
360                         this->height = gdk_pixbuf_get_height( pixbuf );
361                 }
362         }
363
364         // If we have a pixbuf and we need an image
365         if ( pixbuf && width > 0 && !this->image )
366         {
367                 char *interps = mlt_properties_get( properties, "rescale.interp" );
368                 int interp = GDK_INTERP_BILINEAR;
369
370                 if ( strcmp( interps, "nearest" ) == 0 )
371                         interp = GDK_INTERP_NEAREST;
372                 else if ( strcmp( interps, "tiles" ) == 0 )
373                         interp = GDK_INTERP_TILES;
374                 else if ( strcmp( interps, "hyper" ) == 0 )
375                         interp = GDK_INTERP_HYPER;
376
377                 // Note - the original pixbuf is already safe and ready for destruction
378                 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
379
380                 // Store width and height
381                 this->width = width;
382                 this->height = height;
383                 
384                 // Allocate/define image
385                 this->alpha = gdk_pixbuf_get_has_alpha( pixbuf );
386                 int src_stride = gdk_pixbuf_get_rowstride( pixbuf );
387                 int dst_stride = this->width * ( this->alpha ? 4 : 3 );
388                 int image_size = dst_stride * ( height + 1 );
389                 this->image = mlt_pool_alloc( image_size );
390
391                 if ( src_stride != dst_stride )
392                 {
393                         int y = this->height;
394                         uint8_t *src = gdk_pixbuf_get_pixels( pixbuf );
395                         uint8_t *dst = this->image;
396                         while ( y-- )
397                         {
398                                 memcpy( dst, src, dst_stride );
399                                 dst += dst_stride;
400                                 src += src_stride;
401                         }
402                 }
403                 else
404                 {
405                         memcpy( this->image, gdk_pixbuf_get_pixels( pixbuf ), src_stride * height );
406                 }
407                 if ( !use_cache )
408                         mlt_cache_item_close( this->image_cache );
409                 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image", this->image, image_size, mlt_pool_release );
410                 this->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image" );
411                 this->image_idx = image_idx;
412
413                 // Finished with pixbuf now
414                 g_object_unref( pixbuf );
415
416                 // Ensure we update the cache when we need to
417                 update_cache = use_cache;
418         }
419
420         // release references no longer needed
421         mlt_cache_item_close( pixbuf_cache );
422         if ( width == 0 )
423         {
424                 pthread_mutex_unlock( &this->mutex );
425                 mlt_cache_item_close( this->image_cache );
426         }
427
428         // Set width/height of frame
429         mlt_properties_set_int( properties, "width", this->width );
430         mlt_properties_set_int( properties, "height", this->height );
431         mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
432         mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
433
434         if ( update_cache )
435         {
436                 mlt_frame cached = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
437                 mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
438                 mlt_properties_set_int( cached_props, "width", this->width );
439                 mlt_properties_set_int( cached_props, "height", this->height );
440                 mlt_properties_set_int( cached_props, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
441                 mlt_properties_set_int( cached_props, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
442                 mlt_properties_set_data( cached_props, "image", this->image, this->width * ( this->alpha ? 4 : 3 ) * this->height, mlt_pool_release, NULL );
443                 mlt_properties_set_int( cached_props, "alpha", this->alpha );
444                 mlt_properties_set_data( cache, image_key, cached, 0, ( mlt_destructor )mlt_frame_close, NULL );
445         }
446
447         pthread_mutex_unlock( &g_mutex );
448 }
449
450 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
451 {
452         int error = 0;
453         
454         // Obtain properties of frame
455         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
456
457         // Obtain the producer for this frame
458         producer_pixbuf this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
459
460         *width = mlt_properties_get_int( properties, "rescale_width" );
461         *height = mlt_properties_get_int( properties, "rescale_height" );
462
463         // Refresh the image
464         refresh_image( this, frame, *width, *height );
465
466         // Get width and height (may have changed during the refresh)
467         *width = this->width;
468         *height = this->height;
469
470         // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
471         // The fault is not in the design of mlt, but in the implementation of the pixbuf producer...
472         if ( this->image )
473         {
474                 // Clone the image
475                 int image_size = this->width * this->height * ( this->alpha ? 4 :3 );
476                 uint8_t *image_copy = mlt_pool_alloc( image_size );
477                 memcpy( image_copy, this->image, image_size );
478                 // Now update properties so we free the copy after
479                 mlt_properties_set_data( properties, "image", image_copy, image_size, mlt_pool_release, NULL );
480                 // We're going to pass the copy on
481                 *buffer = image_copy;
482                 *format = this->alpha ? mlt_image_rgb24a : mlt_image_rgb24;
483                 mlt_log_debug( MLT_PRODUCER_SERVICE( &this->parent ), "%dx%d (%s)\n",
484                         this->width, this->height, mlt_image_format_name( *format ) );
485         }
486         else
487         {
488                 error = 1;
489         }
490
491         // Release references and locks
492         pthread_mutex_unlock( &this->mutex );
493         mlt_cache_item_close( this->image_cache );
494
495         return error;
496 }
497
498 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
499 {
500         // Get the real structure for this producer
501         producer_pixbuf this = producer->child;
502
503         // Fetch the producers properties
504         mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
505
506         if ( this->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
507                 load_filenames( this, producer_properties );
508
509         // Generate a frame
510         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
511
512         if ( *frame != NULL && this->count > 0 )
513         {
514                 // Obtain properties of frame and producer
515                 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
516
517                 // Set the producer on the frame properties
518                 mlt_properties_set_data( properties, "producer_pixbuf", this, 0, NULL, NULL );
519
520                 // Update timecode on the frame we're creating
521                 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
522
523                 // Ensure that we have a way to obtain the position in the get_image
524                 mlt_properties_set_position( properties, "pixbuf_position", mlt_producer_position( producer ) );
525
526                 // Refresh the image
527                 refresh_image( this, *frame, 0, 0 );
528
529                 // Set producer-specific frame properties
530                 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( producer_properties, "progressive" ) );
531                 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
532
533                 // Push the get_image method
534                 mlt_frame_push_get_image( *frame, producer_get_image );
535         }
536
537         // Calculate the next timecode
538         mlt_producer_prepare_next( producer );
539
540         return 0;
541 }
542
543 static void producer_close( mlt_producer parent )
544 {
545         producer_pixbuf this = parent->child;
546         pthread_mutex_destroy( &this->mutex );
547         parent->close = NULL;
548         mlt_service_cache_purge( MLT_PRODUCER_SERVICE(parent) );
549         mlt_producer_close( parent );
550         mlt_properties_close( this->filenames );
551         free( this );
552 }