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>
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.
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.
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
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>
30 #include <libexif/exif-data.h>
38 #include <sys/types.h>
43 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
45 typedef struct producer_pixbuf_s *producer_pixbuf;
47 struct producer_pixbuf_s
49 struct mlt_producer_s parent;
52 mlt_properties filenames;
60 mlt_cache_item image_cache;
61 pthread_mutex_t mutex;
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 );
69 mlt_producer producer_pixbuf_init( char *filename )
71 producer_pixbuf this = calloc( sizeof( struct producer_pixbuf_s ), 1 );
72 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
74 mlt_producer producer = &this->parent;
76 // Get the properties interface
77 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
79 // Callback registration
80 producer->get_frame = producer_get_frame;
81 producer->close = ( mlt_destructor )producer_close;
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 );
89 // Validate the resource
91 load_filenames( this, properties );
94 mlt_frame frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
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 );
106 if ( this->width == 0 )
108 producer_close( producer );
117 static void load_filenames( producer_pixbuf this, mlt_properties producer_properties )
119 char *filename = mlt_properties_get( producer_properties, "resource" );
120 this->filenames = mlt_properties_new( );
123 if ( strstr( filename, "<svg" ) )
125 // Generate a temporary file for the svg
126 char fullname[ 1024 ] = "/tmp/mlt.XXXXXX";
127 int fd = mkstemp( fullname );
131 // Write the svg into the temp file
132 ssize_t remaining_bytes;
133 char *xml = filename;
135 // Strip leading crap
136 while ( xml[0] != '<' )
139 remaining_bytes = strlen( xml );
140 while ( remaining_bytes > 0 )
141 remaining_bytes -= write( fd, xml + strlen( xml ) - remaining_bytes, remaining_bytes );
144 mlt_properties_set( this->filenames, "0", fullname );
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 );
151 else if ( strchr( filename, '%' ) != NULL )
153 // handle picture sequences
154 int i = mlt_properties_get_int( producer_properties, "begin" );
163 snprintf( full, 1023, filename, i ++ );
164 if ( stat( full, &buf ) == 0 )
166 sprintf( key, "%d", keyvalue ++ );
167 mlt_properties_set( this->filenames, key, full );
175 if ( mlt_properties_count( this->filenames ) > 0 )
176 mlt_properties_set_int( producer_properties, "ttl", 1 );
178 else if ( strstr( filename, "/.all." ) != NULL )
180 char wildcard[ 1024 ];
181 char *dir_name = strdup( filename );
182 char *extension = strrchr( dir_name, '.' );
184 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
185 sprintf( wildcard, "*%s", extension );
187 mlt_properties_dir_list( this->filenames, dir_name, wildcard, 1 );
193 mlt_properties_set( this->filenames, "0", filename );
196 this->count = mlt_properties_count( this->filenames );
199 static void refresh_image( producer_pixbuf this, mlt_frame frame, int width, int height )
201 // Obtain properties of frame
202 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
204 // Obtain the producer
205 mlt_producer producer = &this->parent;
207 // Obtain properties of producer
208 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
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;
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;
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 );
225 // Check if user wants us to reload the image
226 if ( mlt_properties_get_int( producer_props, "force_reload" ) )
230 mlt_properties_set_int( producer_props, "force_reload", 0 );
233 // Get the time to live for each frame
234 double ttl = mlt_properties_get_int( producer_props, "ttl" );
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 );
241 int image_idx = ( int )floor( ( double )position / ttl ) % this->count;
244 char image_key[ 10 ];
245 sprintf( image_key, "%d", image_idx );
247 pthread_mutex_lock( &g_mutex );
249 // Check if the frame is already loaded
254 cache = mlt_properties_new( );
255 mlt_properties_set_data( producer_props, "_cache", cache, 0, ( mlt_destructor )mlt_properties_close, NULL );
258 mlt_frame cached = mlt_properties_get_data( cache, image_key, NULL );
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" );
271 if ( width != 0 && ( width != this->width || height != this->height ) )
275 int disable_exif = mlt_properties_get_int( producer_props, "disable_exif" );
277 // optimization for subsequent iterations on single picture
278 if ( width != 0 && ( image_idx != this->image_idx || width != this->width || height != this->height ) )
280 if ( image_idx != this->pixbuf_idx )
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 )
287 pixbuf = gdk_pixbuf_new_from_file( mlt_properties_get_value( this->filenames, image_idx ), &error );
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 ) );
296 int exif_orientation = 0;
298 /* get orientation and rotate image accordingly if necessary */
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));
303 /* Free the EXIF data */
307 if ( exif_orientation > 1 )
309 GdkPixbuf *processed = NULL;
310 GdkPixbufRotation matrix = GDK_PIXBUF_ROTATE_NONE;
312 // Rotate image according to exif data
313 switch ( exif_orientation ) {
315 processed = gdk_pixbuf_flip ( pixbuf, TRUE );
318 matrix = GDK_PIXBUF_ROTATE_UPSIDEDOWN;
322 processed = gdk_pixbuf_flip ( pixbuf, FALSE );
325 matrix = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
326 processed = gdk_pixbuf_flip ( pixbuf, TRUE );
329 matrix = GDK_PIXBUF_ROTATE_CLOCKWISE;
333 matrix = GDK_PIXBUF_ROTATE_CLOCKWISE;
334 processed = gdk_pixbuf_flip ( pixbuf, TRUE );
337 matrix = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
342 pixbuf = gdk_pixbuf_rotate_simple( processed, matrix );
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;
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 );
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 );
365 // If we have a pixbuf and we need an image
366 if ( pixbuf && width > 0 && !this->image )
368 char *interps = mlt_properties_get( properties, "rescale.interp" );
369 int interp = GDK_INTERP_BILINEAR;
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;
378 // Note - the original pixbuf is already safe and ready for destruction
379 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
381 // Store width and height
383 this->height = height;
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 );
392 if ( src_stride != dst_stride )
394 int y = this->height;
395 uint8_t *src = gdk_pixbuf_get_pixels( pixbuf );
396 uint8_t *dst = this->image;
399 memcpy( dst, src, dst_stride );
406 memcpy( this->image, gdk_pixbuf_get_pixels( pixbuf ), src_stride * height );
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;
414 // Finished with pixbuf now
415 g_object_unref( pixbuf );
417 // Ensure we update the cache when we need to
418 update_cache = use_cache;
421 // release references no longer needed
422 mlt_cache_item_close( pixbuf_cache );
425 pthread_mutex_unlock( &this->mutex );
426 mlt_cache_item_close( this->image_cache );
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" ) );
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 );
448 pthread_mutex_unlock( &g_mutex );
451 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
455 // Obtain properties of frame
456 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
458 // Obtain the producer for this frame
459 producer_pixbuf this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
461 *width = mlt_properties_get_int( properties, "rescale_width" );
462 *height = mlt_properties_get_int( properties, "rescale_height" );
465 refresh_image( this, frame, *width, *height );
467 // Get width and height (may have changed during the refresh)
468 *width = this->width;
469 *height = this->height;
471 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
472 // The fault is not in the design of mlt, but in the implementation of the pixbuf producer...
476 int image_size = this->width * this->height * ( this->alpha ? 4 :3 );
477 uint8_t *image_copy = mlt_pool_alloc( image_size );
478 memcpy( image_copy, this->image, image_size );
479 // Now update properties so we free the copy after
480 mlt_properties_set_data( properties, "image", image_copy, image_size, mlt_pool_release, NULL );
481 // We're going to pass the copy on
482 *buffer = image_copy;
483 *format = this->alpha ? mlt_image_rgb24a : mlt_image_rgb24;
484 mlt_log_debug( MLT_PRODUCER_SERVICE( &this->parent ), "%dx%d (%s)\n",
485 this->width, this->height, mlt_image_format_name( *format ) );
492 // Release references and locks
493 pthread_mutex_unlock( &this->mutex );
494 mlt_cache_item_close( this->image_cache );
499 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
501 // Get the real structure for this producer
502 producer_pixbuf this = producer->child;
504 // Fetch the producers properties
505 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
507 if ( this->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
508 load_filenames( this, producer_properties );
511 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
513 if ( *frame != NULL && this->count > 0 )
515 // Obtain properties of frame and producer
516 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
518 // Set the producer on the frame properties
519 mlt_properties_set_data( properties, "producer_pixbuf", this, 0, NULL, NULL );
521 // Update timecode on the frame we're creating
522 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
524 // Ensure that we have a way to obtain the position in the get_image
525 mlt_properties_set_position( properties, "pixbuf_position", mlt_producer_position( producer ) );
528 refresh_image( this, *frame, 0, 0 );
530 // Set producer-specific frame properties
531 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( producer_properties, "progressive" ) );
532 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
534 // Push the get_image method
535 mlt_frame_push_get_image( *frame, producer_get_image );
538 // Calculate the next timecode
539 mlt_producer_prepare_next( producer );
544 static void producer_close( mlt_producer parent )
546 producer_pixbuf this = parent->child;
547 pthread_mutex_destroy( &this->mutex );
548 parent->close = NULL;
549 mlt_service_cache_purge( MLT_PRODUCER_SERVICE(parent) );
550 mlt_producer_close( parent );
551 mlt_properties_close( this->filenames );