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