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>
44 // this protects concurrent access to gdk_pixbuf
45 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
47 typedef struct producer_pixbuf_s *producer_pixbuf;
49 struct producer_pixbuf_s
51 struct mlt_producer_s parent;
54 mlt_properties filenames;
62 mlt_cache_item image_cache;
63 mlt_cache_item alpha_cache;
64 mlt_cache_item pixbuf_cache;
66 mlt_image_format format;
69 static void load_filenames( producer_pixbuf self, mlt_properties producer_properties );
70 static int refresh_pixbuf( producer_pixbuf self, mlt_frame frame );
71 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
72 static void producer_close( mlt_producer parent );
74 mlt_producer producer_pixbuf_init( char *filename )
76 producer_pixbuf self = calloc( 1, sizeof( struct producer_pixbuf_s ) );
77 if ( self != NULL && mlt_producer_init( &self->parent, self ) == 0 )
79 mlt_producer producer = &self->parent;
81 // Get the properties interface
82 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &self->parent );
84 // Callback registration
85 producer->get_frame = producer_get_frame;
86 producer->close = ( mlt_destructor )producer_close;
88 // Set the default properties
89 mlt_properties_set( properties, "resource", filename );
90 mlt_properties_set_int( properties, "ttl", 25 );
91 mlt_properties_set_int( properties, "aspect_ratio", 1 );
92 mlt_properties_set_int( properties, "progressive", 1 );
93 mlt_properties_set_int( properties, "seekable", 1 );
95 // Validate the resource
97 load_filenames( self, properties );
100 mlt_frame frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
103 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
104 mlt_properties_set_data( frame_properties, "producer_pixbuf", self, 0, NULL, NULL );
105 mlt_frame_set_position( frame, mlt_producer_position( producer ) );
106 refresh_pixbuf( self, frame );
107 mlt_cache_item_close( self->pixbuf_cache );
108 mlt_frame_close( frame );
111 if ( self->width == 0 )
113 producer_close( producer );
122 static int load_svg( producer_pixbuf self, mlt_properties properties, const char *filename )
127 if ( strstr( filename, "<svg" ) )
129 // Generate a temporary file for the svg
130 char fullname[ 1024 ] = "/tmp/mlt.XXXXXX";
131 int fd = g_mkstemp( fullname );
135 // Write the svg into the temp file
136 ssize_t remaining_bytes;
137 const char *xml = filename;
139 // Strip leading crap
140 while ( xml[0] != '<' )
143 remaining_bytes = strlen( xml );
144 while ( remaining_bytes > 0 )
145 remaining_bytes -= write( fd, xml + strlen( xml ) - remaining_bytes, remaining_bytes );
148 mlt_properties_set( self->filenames, "0", fullname );
150 // Teehe - when the producer closes, delete the temp file and the space allo
151 mlt_properties_set_data( properties, "__temporary_file__", fullname, 0, ( mlt_destructor )unlink, NULL );
158 static int load_sequence( producer_pixbuf self, mlt_properties properties, const char *filename )
162 // Obtain filenames with pattern
163 if ( strchr( filename, '%' ) != NULL )
165 // handle picture sequences
166 int i = mlt_properties_get_int( properties, "begin" );
175 snprintf( full, 1023, filename, i ++ );
176 if ( stat( full, &buf ) == 0 )
178 sprintf( key, "%d", keyvalue ++ );
179 mlt_properties_set( self->filenames, key, full );
187 if ( mlt_properties_count( self->filenames ) > 0 )
189 mlt_properties_set_int( properties, "ttl", 1 );
196 static int load_sequence2( producer_pixbuf self, mlt_properties properties, const char *filename )
201 // Obtain filenames with pattern containing a begin value, e.g. foo%1234d.png
202 if ( ( start = strchr( filename, '%' ) ) )
204 const char *end = ++start;
205 while ( isdigit( *end ) ) end++;
206 if ( end > start && ( end[0] == 'd' || end[0] == 'i' || end[0] == 'u' ) )
209 char *s = calloc( 1, n + 1 );
210 strncpy( s, start, n );
211 mlt_properties_set( properties, "begin", s );
213 s = calloc( 1, strlen( filename ) );
214 strncpy( s, filename, start - filename );
215 sprintf( s + ( start - filename ), ".%d%s", n, end );
216 result = load_sequence( self, properties, s );
223 static int load_folder( producer_pixbuf self, mlt_properties properties, const char *filename )
227 // Obtain filenames with folder
228 if ( strstr( filename, "/.all." ) != NULL )
230 char wildcard[ 1024 ];
231 char *dir_name = strdup( filename );
232 char *extension = strrchr( dir_name, '.' );
234 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
235 sprintf( wildcard, "*%s", extension );
237 mlt_properties_dir_list( self->filenames, dir_name, wildcard, 1 );
245 static void load_filenames( producer_pixbuf self, mlt_properties properties )
247 char *filename = mlt_properties_get( properties, "resource" );
248 self->filenames = mlt_properties_new( );
250 if (!load_svg( self, properties, filename ) &&
251 !load_sequence( self, properties, filename ) &&
252 !load_sequence2( self, properties, filename ) &&
253 !load_folder( self, properties, filename ) )
255 mlt_properties_set( self->filenames, "0", filename );
257 self->count = mlt_properties_count( self->filenames );
260 static GdkPixbuf* reorient_with_exif( producer_pixbuf self, int image_idx, GdkPixbuf *pixbuf )
263 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( &self->parent );
264 ExifData *d = exif_data_new_from_file( mlt_properties_get_value( self->filenames, image_idx ) );
266 int exif_orientation = 0;
268 /* get orientation and rotate image accordingly if necessary */
271 if ( ( entry = exif_content_get_entry ( d->ifd[EXIF_IFD_0], EXIF_TAG_ORIENTATION ) ) )
272 exif_orientation = exif_get_short (entry->data, exif_data_get_byte_order (d));
274 /* Free the EXIF data */
278 // Remember EXIF value, might be useful for someone
279 mlt_properties_set_int( producer_props, "_exif_orientation" , exif_orientation );
281 if ( exif_orientation > 1 )
283 GdkPixbuf *processed = NULL;
284 GdkPixbufRotation matrix = GDK_PIXBUF_ROTATE_NONE;
286 // Rotate image according to exif data
287 switch ( exif_orientation ) {
289 processed = gdk_pixbuf_flip ( pixbuf, TRUE );
292 matrix = GDK_PIXBUF_ROTATE_UPSIDEDOWN;
296 processed = gdk_pixbuf_flip ( pixbuf, FALSE );
299 matrix = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
300 processed = gdk_pixbuf_flip ( pixbuf, TRUE );
303 matrix = GDK_PIXBUF_ROTATE_CLOCKWISE;
307 matrix = GDK_PIXBUF_ROTATE_CLOCKWISE;
308 processed = gdk_pixbuf_flip ( pixbuf, TRUE );
311 matrix = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
317 pixbuf = gdk_pixbuf_rotate_simple( processed, matrix );
318 g_object_unref( processed );
325 static int refresh_pixbuf( producer_pixbuf self, mlt_frame frame )
327 // Obtain properties of frame and producer
328 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
329 mlt_producer producer = &self->parent;
330 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
332 // Check if user wants us to reload the image
333 if ( mlt_properties_get_int( producer_props, "force_reload" ) )
337 mlt_properties_set_int( producer_props, "force_reload", 0 );
340 // Get the time to live for each frame
341 double ttl = mlt_properties_get_int( producer_props, "ttl" );
343 // Get the original position of this frame
344 mlt_position position = mlt_frame_original_position( frame );
345 position += mlt_producer_get_in( producer );
348 int current_idx = ( int )floor( ( double )position / ttl ) % self->count;
351 char image_key[ 10 ];
352 sprintf( image_key, "%d", current_idx );
354 int disable_exif = mlt_properties_get_int( producer_props, "disable_exif" );
356 if ( current_idx != self->pixbuf_idx )
358 if ( !self->pixbuf || mlt_properties_get_int( producer_props, "_disable_exif" ) != disable_exif )
360 GError *error = NULL;
363 pthread_mutex_lock( &g_mutex );
364 self->pixbuf = gdk_pixbuf_new_from_file( mlt_properties_get_value( self->filenames, current_idx ), &error );
367 // Read the exif value for this file
369 self->pixbuf = reorient_with_exif( self, current_idx, self->pixbuf );
371 // Register this pixbuf for destruction and reuse
372 mlt_cache_item_close( self->pixbuf_cache );
373 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf", self->pixbuf, 0, ( mlt_destructor )g_object_unref );
374 self->pixbuf_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf" );
375 self->pixbuf_idx = current_idx;
377 // Store the width/height of the pixbuf temporarily
378 self->width = gdk_pixbuf_get_width( self->pixbuf );
379 self->height = gdk_pixbuf_get_height( self->pixbuf );
381 mlt_events_block( producer_props, NULL );
382 mlt_properties_set_int( producer_props, "meta.media.width", self->width );
383 mlt_properties_set_int( producer_props, "meta.media.height", self->height );
384 mlt_properties_set_int( producer_props, "_disable_exif", disable_exif );
385 mlt_events_unblock( producer_props, NULL );
388 pthread_mutex_unlock( &g_mutex );
391 // Set width/height of frame
392 mlt_properties_set_int( properties, "width", self->width );
393 mlt_properties_set_int( properties, "height", self->height );
398 static void refresh_image( producer_pixbuf self, mlt_frame frame, mlt_image_format format, int width, int height )
400 // Obtain properties of frame and producer
401 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
402 mlt_producer producer = &self->parent;
404 // Get index and pixbuf
405 int current_idx = refresh_pixbuf( self, frame );
407 // optimization for subsequent iterations on single picture
408 if ( current_idx != self->image_idx || width != self->width || height != self->height )
410 mlt_log_debug( MLT_PRODUCER_SERVICE( producer ), "image %p pixbuf %p idx %d current_idx %d pixbuf_idx %d width %d\n",
411 self->image, self->pixbuf, current_idx, self->image_idx, self->pixbuf_idx, width );
413 // If we have a pixbuf and we need an image
414 if ( self->pixbuf && ( !self->image || ( format != mlt_image_none && format != self->format ) ) )
416 char *interps = mlt_properties_get( properties, "rescale.interp" );
417 int interp = GDK_INTERP_BILINEAR;
419 if ( strcmp( interps, "nearest" ) == 0 )
420 interp = GDK_INTERP_NEAREST;
421 else if ( strcmp( interps, "tiles" ) == 0 )
422 interp = GDK_INTERP_TILES;
423 else if ( strcmp( interps, "hyper" ) == 0 || strcmp( interps, "bicubic" ) == 0 )
424 interp = GDK_INTERP_HYPER;
426 // Note - the original pixbuf is already safe and ready for destruction
427 pthread_mutex_lock( &g_mutex );
428 GdkPixbuf* pixbuf = gdk_pixbuf_scale_simple( self->pixbuf, width, height, interp );
430 // Store width and height
432 self->height = height;
434 // Allocate/define image
435 int has_alpha = gdk_pixbuf_get_has_alpha( pixbuf );
436 int src_stride = gdk_pixbuf_get_rowstride( pixbuf );
437 int dst_stride = self->width * ( has_alpha ? 4 : 3 );
438 int image_size = dst_stride * ( height + 1 );
439 self->image = mlt_pool_alloc( image_size );
441 self->format = has_alpha ? mlt_image_rgb24a : mlt_image_rgb24;
443 if ( src_stride != dst_stride )
445 int y = self->height;
446 uint8_t *src = gdk_pixbuf_get_pixels( pixbuf );
447 uint8_t *dst = self->image;
450 memcpy( dst, src, dst_stride );
457 memcpy( self->image, gdk_pixbuf_get_pixels( pixbuf ), src_stride * height );
459 pthread_mutex_unlock( &g_mutex );
461 // Convert image to requested format
462 if ( format != mlt_image_none && format != self->format )
464 uint8_t *buffer = NULL;
466 // First, set the image so it can be converted when we get it
467 mlt_frame_replace_image( frame, self->image, self->format, width, height );
468 mlt_frame_set_image( frame, self->image, image_size, mlt_pool_release );
469 self->format = format;
471 // get_image will do the format conversion
472 mlt_frame_get_image( frame, &buffer, &format, &width, &height, 0 );
474 // cache copies of the image and alpha buffers
477 image_size = mlt_image_format_size( format, width, height, NULL );
478 self->image = mlt_pool_alloc( image_size );
479 memcpy( self->image, buffer, image_size );
481 if ( ( buffer = mlt_frame_get_alpha_mask( frame ) ) )
483 self->alpha = mlt_pool_alloc( width * height );
484 memcpy( self->alpha, buffer, width * height );
489 mlt_cache_item_close( self->image_cache );
490 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image", self->image, image_size, mlt_pool_release );
491 self->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image" );
492 self->image_idx = current_idx;
493 mlt_cache_item_close( self->alpha_cache );
494 self->alpha_cache = NULL;
497 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.alpha", self->alpha, width * height, mlt_pool_release );
498 self->alpha_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.alpha" );
501 // Finished with pixbuf now
502 g_object_unref( pixbuf );
505 // Set width/height of frame
506 mlt_properties_set_int( properties, "width", self->width );
507 mlt_properties_set_int( properties, "height", self->height );
510 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
514 // Obtain properties of frame and producer
515 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
516 producer_pixbuf self = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
517 mlt_producer producer = &self->parent;
519 // Use the width and height suggested by the rescale filter because we can do our own scaling.
520 *width = mlt_properties_get_int( properties, "rescale_width" );
521 *height = mlt_properties_get_int( properties, "rescale_height" );
523 // Restore pixbuf and image
524 mlt_service_lock( MLT_PRODUCER_SERVICE( producer ) );
525 self->pixbuf_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf" );
526 self->pixbuf = mlt_cache_item_data( self->pixbuf_cache, NULL );
527 self->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image" );
528 self->image = mlt_cache_item_data( self->image_cache, NULL );
529 self->alpha_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.alpha" );
530 self->alpha = mlt_cache_item_data( self->alpha_cache, NULL );
533 refresh_image( self, frame, *format, *width, *height );
535 // Get width and height (may have changed during the refresh)
536 *width = self->width;
537 *height = self->height;
538 *format = self->format;
540 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
541 // The fault is not in the design of mlt, but in the implementation of the pixbuf producer...
545 int image_size = mlt_image_format_size( self->format, self->width, self->height, NULL );
546 uint8_t *image_copy = mlt_pool_alloc( image_size );
547 memcpy( image_copy, self->image, image_size );
548 // Now update properties so we free the copy after
549 mlt_frame_set_image( frame, image_copy, image_size, mlt_pool_release );
550 // We're going to pass the copy on
551 *buffer = image_copy;
552 mlt_log_debug( MLT_PRODUCER_SERVICE( &self->parent ), "%dx%d (%s)\n",
553 self->width, self->height, mlt_image_format_name( *format ) );
554 // Clone the alpha channel
557 image_copy = mlt_pool_alloc( self->width * self->height );
558 memcpy( image_copy, self->alpha, self->width * self->height );
559 mlt_frame_set_alpha( frame, image_copy, self->width * self->height, mlt_pool_release );
567 // Release references and locks
568 mlt_cache_item_close( self->pixbuf_cache );
569 mlt_cache_item_close( self->image_cache );
570 mlt_cache_item_close( self->alpha_cache );
571 mlt_service_unlock( MLT_PRODUCER_SERVICE( &self->parent ) );
576 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
578 // Get the real structure for this producer
579 producer_pixbuf self = producer->child;
581 // Fetch the producers properties
582 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
584 if ( self->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
585 load_filenames( self, producer_properties );
588 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
590 if ( *frame != NULL && self->count > 0 )
592 // Obtain properties of frame and producer
593 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
595 // Set the producer on the frame properties
596 mlt_properties_set_data( properties, "producer_pixbuf", self, 0, NULL, NULL );
598 // Update timecode on the frame we're creating
599 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
601 // Refresh the pixbuf
602 self->pixbuf_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf" );
603 self->pixbuf = mlt_cache_item_data( self->pixbuf_cache, NULL );
604 refresh_pixbuf( self, *frame );
605 mlt_cache_item_close( self->pixbuf_cache );
607 // Set producer-specific frame properties
608 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( producer_properties, "progressive" ) );
610 double force_ratio = mlt_properties_get_double( producer_properties, "force_aspect_ratio" );
611 if ( force_ratio > 0.0 )
612 mlt_properties_set_double( properties, "aspect_ratio", force_ratio );
614 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
616 // Push the get_image method
617 mlt_frame_push_get_image( *frame, producer_get_image );
620 // Calculate the next timecode
621 mlt_producer_prepare_next( producer );
626 static void producer_close( mlt_producer parent )
628 producer_pixbuf self = parent->child;
629 parent->close = NULL;
630 mlt_service_cache_purge( MLT_PRODUCER_SERVICE(parent) );
631 mlt_producer_close( parent );
632 mlt_properties_close( self->filenames );