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