]> git.sesse.net Git - mlt/blob - src/modules/gtk2/producer_pixbuf.c
Add service locks for parallelism.
[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 <libexif/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 = g_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 || 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 = NULL;
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                                         if ( processed )
342                                                 pixbuf = gdk_pixbuf_rotate_simple( processed, matrix );
343                                 }
344                         }
345 #endif
346
347                         // Register this pixbuf for destruction and reuse
348                         mlt_cache_item_close( pixbuf_cache );
349                         mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref );
350                         pixbuf_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf" );
351                         this->pixbuf_idx = image_idx;
352
353                         mlt_events_block( producer_props, NULL );
354                         mlt_properties_set_int( producer_props, "_real_width", gdk_pixbuf_get_width( pixbuf ) );
355                         mlt_properties_set_int( producer_props, "_real_height", gdk_pixbuf_get_height( pixbuf ) );
356                         mlt_properties_set_int( producer_props, "_disable_exif", disable_exif );
357                         mlt_events_unblock( producer_props, NULL );
358
359                         // Store the width/height of the pixbuf temporarily
360                         this->width = gdk_pixbuf_get_width( pixbuf );
361                         this->height = gdk_pixbuf_get_height( pixbuf );
362                 }
363         }
364
365         // If we have a pixbuf and we need an image
366         if ( pixbuf && width > 0 && !this->image )
367         {
368                 char *interps = mlt_properties_get( properties, "rescale.interp" );
369                 int interp = GDK_INTERP_BILINEAR;
370
371                 if ( strcmp( interps, "nearest" ) == 0 )
372                         interp = GDK_INTERP_NEAREST;
373                 else if ( strcmp( interps, "tiles" ) == 0 )
374                         interp = GDK_INTERP_TILES;
375                 else if ( strcmp( interps, "hyper" ) == 0 )
376                         interp = GDK_INTERP_HYPER;
377
378                 // Note - the original pixbuf is already safe and ready for destruction
379                 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
380
381                 // Store width and height
382                 this->width = width;
383                 this->height = height;
384                 
385                 // Allocate/define image
386                 this->alpha = gdk_pixbuf_get_has_alpha( pixbuf );
387                 int src_stride = gdk_pixbuf_get_rowstride( pixbuf );
388                 int dst_stride = this->width * ( this->alpha ? 4 : 3 );
389                 int image_size = dst_stride * ( height + 1 );
390                 this->image = mlt_pool_alloc( image_size );
391
392                 if ( src_stride != dst_stride )
393                 {
394                         int y = this->height;
395                         uint8_t *src = gdk_pixbuf_get_pixels( pixbuf );
396                         uint8_t *dst = this->image;
397                         while ( y-- )
398                         {
399                                 memcpy( dst, src, dst_stride );
400                                 dst += dst_stride;
401                                 src += src_stride;
402                         }
403                 }
404                 else
405                 {
406                         memcpy( this->image, gdk_pixbuf_get_pixels( pixbuf ), src_stride * height );
407                 }
408                 if ( !use_cache )
409                         mlt_cache_item_close( this->image_cache );
410                 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image", this->image, image_size, mlt_pool_release );
411                 this->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image" );
412                 this->image_idx = image_idx;
413
414                 // Finished with pixbuf now
415                 g_object_unref( pixbuf );
416
417                 // Ensure we update the cache when we need to
418                 update_cache = use_cache;
419         }
420
421         // release references no longer needed
422         mlt_cache_item_close( pixbuf_cache );
423         if ( width == 0 )
424         {
425                 pthread_mutex_unlock( &this->mutex );
426                 mlt_cache_item_close( this->image_cache );
427         }
428
429         // Set width/height of frame
430         mlt_properties_set_int( properties, "width", this->width );
431         mlt_properties_set_int( properties, "height", this->height );
432         mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
433         mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
434
435         if ( update_cache )
436         {
437                 mlt_frame cached = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
438                 mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
439                 mlt_properties_set_int( cached_props, "width", this->width );
440                 mlt_properties_set_int( cached_props, "height", this->height );
441                 mlt_properties_set_int( cached_props, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
442                 mlt_properties_set_int( cached_props, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
443                 mlt_properties_set_data( cached_props, "image", this->image, this->width * ( this->alpha ? 4 : 3 ) * this->height, mlt_pool_release, NULL );
444                 mlt_properties_set_int( cached_props, "alpha", this->alpha );
445                 mlt_properties_set_data( cache, image_key, cached, 0, ( mlt_destructor )mlt_frame_close, NULL );
446         }
447
448         pthread_mutex_unlock( &g_mutex );
449 }
450
451 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
452 {
453         int error = 0;
454         
455         // Obtain properties of frame
456         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
457
458         // Obtain the producer for this frame
459         producer_pixbuf this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
460
461         *width = mlt_properties_get_int( properties, "rescale_width" );
462         *height = mlt_properties_get_int( properties, "rescale_height" );
463
464         mlt_service_lock( MLT_PRODUCER_SERVICE( &this->parent ) );
465
466         // Refresh the image
467         refresh_image( this, frame, *width, *height );
468
469         // Get width and height (may have changed during the refresh)
470         *width = this->width;
471         *height = this->height;
472
473         // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
474         // The fault is not in the design of mlt, but in the implementation of the pixbuf producer...
475         if ( this->image )
476         {
477                 // Clone the image
478                 int image_size = this->width * this->height * ( this->alpha ? 4 :3 );
479                 uint8_t *image_copy = mlt_pool_alloc( image_size );
480                 memcpy( image_copy, this->image, image_size );
481                 // Now update properties so we free the copy after
482                 mlt_properties_set_data( properties, "image", image_copy, image_size, mlt_pool_release, NULL );
483                 // We're going to pass the copy on
484                 *buffer = image_copy;
485                 *format = this->alpha ? mlt_image_rgb24a : mlt_image_rgb24;
486                 mlt_log_debug( MLT_PRODUCER_SERVICE( &this->parent ), "%dx%d (%s)\n",
487                         this->width, this->height, mlt_image_format_name( *format ) );
488         }
489         else
490         {
491                 error = 1;
492         }
493
494         // Release references and locks
495         pthread_mutex_unlock( &this->mutex );
496         mlt_cache_item_close( this->image_cache );
497         mlt_service_unlock( MLT_PRODUCER_SERVICE( &this->parent ) );
498
499         return error;
500 }
501
502 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
503 {
504         // Get the real structure for this producer
505         producer_pixbuf this = producer->child;
506
507         // Fetch the producers properties
508         mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
509
510         if ( this->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
511                 load_filenames( this, producer_properties );
512
513         // Generate a frame
514         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
515
516         if ( *frame != NULL && this->count > 0 )
517         {
518                 // Obtain properties of frame and producer
519                 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
520
521                 // Set the producer on the frame properties
522                 mlt_properties_set_data( properties, "producer_pixbuf", this, 0, NULL, NULL );
523
524                 // Update timecode on the frame we're creating
525                 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
526
527                 // Ensure that we have a way to obtain the position in the get_image
528                 mlt_properties_set_position( properties, "pixbuf_position", mlt_producer_position( producer ) );
529
530                 // Refresh the image
531                 refresh_image( this, *frame, 0, 0 );
532
533                 // Set producer-specific frame properties
534                 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( producer_properties, "progressive" ) );
535                 
536                 double force_ratio = mlt_properties_get_double( producer_properties, "force_aspect_ratio" );
537                 if ( force_ratio > 0.0 )
538                         mlt_properties_set_double( properties, "aspect_ratio", force_ratio );
539                 else
540                         mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
541
542                 // Push the get_image method
543                 mlt_frame_push_get_image( *frame, producer_get_image );
544         }
545
546         // Calculate the next timecode
547         mlt_producer_prepare_next( producer );
548
549         return 0;
550 }
551
552 static void producer_close( mlt_producer parent )
553 {
554         producer_pixbuf this = parent->child;
555         pthread_mutex_destroy( &this->mutex );
556         parent->close = NULL;
557         mlt_service_cache_purge( MLT_PRODUCER_SERVICE(parent) );
558         mlt_producer_close( parent );
559         mlt_properties_close( this->filenames );
560         free( this );
561 }