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 // this protects concurrent access to gdk_pixbuf
44 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
46 typedef struct producer_pixbuf_s *producer_pixbuf;
48 struct producer_pixbuf_s
50 struct mlt_producer_s parent;
53 mlt_properties filenames;
63 mlt_cache_item image_cache;
64 mlt_cache_item alpha_cache;
65 mlt_cache_item pixbuf_cache;
67 mlt_image_format format;
70 static void load_filenames( producer_pixbuf self, mlt_properties producer_properties );
71 static int refresh_pixbuf( producer_pixbuf self, mlt_frame frame );
72 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
73 static void producer_close( mlt_producer parent );
75 mlt_producer producer_pixbuf_init( char *filename )
77 producer_pixbuf self = calloc( sizeof( struct producer_pixbuf_s ), 1 );
78 if ( self != NULL && mlt_producer_init( &self->parent, self ) == 0 )
80 mlt_producer producer = &self->parent;
82 // Get the properties interface
83 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &self->parent );
85 // Callback registration
86 producer->get_frame = producer_get_frame;
87 producer->close = ( mlt_destructor )producer_close;
89 // Set the default properties
90 mlt_properties_set( properties, "resource", filename );
91 mlt_properties_set_int( properties, "ttl", 25 );
92 mlt_properties_set_int( properties, "aspect_ratio", 1 );
93 mlt_properties_set_int( properties, "progressive", 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 mlt_properties_set_position( frame_properties, "pixbuf_position", mlt_producer_position( producer ) );
107 refresh_pixbuf( self, frame );
108 mlt_frame_close( frame );
111 if ( self->width == 0 )
113 producer_close( producer );
122 static void load_filenames( producer_pixbuf self, mlt_properties producer_properties )
124 char *filename = mlt_properties_get( producer_properties, "resource" );
125 self->filenames = mlt_properties_new( );
128 if ( strstr( filename, "<svg" ) )
130 // Generate a temporary file for the svg
131 char fullname[ 1024 ] = "/tmp/mlt.XXXXXX";
132 int fd = g_mkstemp( fullname );
136 // Write the svg into the temp file
137 ssize_t remaining_bytes;
138 char *xml = filename;
140 // Strip leading crap
141 while ( xml[0] != '<' )
144 remaining_bytes = strlen( xml );
145 while ( remaining_bytes > 0 )
146 remaining_bytes -= write( fd, xml + strlen( xml ) - remaining_bytes, remaining_bytes );
149 mlt_properties_set( self->filenames, "0", fullname );
151 // Teehe - when the producer closes, delete the temp file and the space allo
152 mlt_properties_set_data( producer_properties, "__temporary_file__", fullname, 0, ( mlt_destructor )unlink, NULL );
156 else if ( strchr( filename, '%' ) != NULL )
158 // handle picture sequences
159 int i = mlt_properties_get_int( producer_properties, "begin" );
168 snprintf( full, 1023, filename, i ++ );
169 if ( stat( full, &buf ) == 0 )
171 sprintf( key, "%d", keyvalue ++ );
172 mlt_properties_set( self->filenames, key, full );
180 if ( mlt_properties_count( self->filenames ) > 0 )
181 mlt_properties_set_int( producer_properties, "ttl", 1 );
183 else if ( strstr( filename, "/.all." ) != NULL )
185 char wildcard[ 1024 ];
186 char *dir_name = strdup( filename );
187 char *extension = strrchr( dir_name, '.' );
189 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
190 sprintf( wildcard, "*%s", extension );
192 mlt_properties_dir_list( self->filenames, dir_name, wildcard, 1 );
198 mlt_properties_set( self->filenames, "0", filename );
201 self->count = mlt_properties_count( self->filenames );
204 static GdkPixbuf* reorient_with_exif( producer_pixbuf self, int image_idx, GdkPixbuf *pixbuf )
207 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( &self->parent );
208 ExifData *d = exif_data_new_from_file( mlt_properties_get_value( self->filenames, image_idx ) );
210 int exif_orientation = 0;
212 /* get orientation and rotate image accordingly if necessary */
215 if ( ( entry = exif_content_get_entry ( d->ifd[EXIF_IFD_0], EXIF_TAG_ORIENTATION ) ) )
216 exif_orientation = exif_get_short (entry->data, exif_data_get_byte_order (d));
218 /* Free the EXIF data */
222 // Remember EXIF value, might be useful for someone
223 mlt_properties_set_int( producer_props, "_exif_orientation" , exif_orientation );
225 if ( exif_orientation > 1 )
227 GdkPixbuf *processed = NULL;
228 GdkPixbufRotation matrix = GDK_PIXBUF_ROTATE_NONE;
230 // Rotate image according to exif data
231 switch ( exif_orientation ) {
233 processed = gdk_pixbuf_flip ( pixbuf, TRUE );
236 matrix = GDK_PIXBUF_ROTATE_UPSIDEDOWN;
240 processed = gdk_pixbuf_flip ( pixbuf, FALSE );
243 matrix = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
244 processed = gdk_pixbuf_flip ( pixbuf, TRUE );
247 matrix = GDK_PIXBUF_ROTATE_CLOCKWISE;
251 matrix = GDK_PIXBUF_ROTATE_CLOCKWISE;
252 processed = gdk_pixbuf_flip ( pixbuf, TRUE );
255 matrix = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
261 pixbuf = gdk_pixbuf_rotate_simple( processed, matrix );
262 g_object_unref( processed );
269 static int refresh_pixbuf( producer_pixbuf self, mlt_frame frame )
271 // Obtain properties of frame and producer
272 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
273 mlt_producer producer = &self->parent;
274 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
276 // Check if user wants us to reload the image
277 if ( mlt_properties_get_int( producer_props, "force_reload" ) )
281 mlt_properties_set_int( producer_props, "force_reload", 0 );
284 // Get the time to live for each frame
285 double ttl = mlt_properties_get_int( producer_props, "ttl" );
287 // Get the original position of this frame
288 mlt_position position = mlt_properties_get_position( properties, "pixbuf_position" );
289 position += mlt_producer_get_in( producer );
292 int current_idx = ( int )floor( ( double )position / ttl ) % self->count;
295 char image_key[ 10 ];
296 sprintf( image_key, "%d", current_idx );
298 int disable_exif = mlt_properties_get_int( producer_props, "disable_exif" );
300 if ( current_idx != self->pixbuf_idx )
302 if ( !self->pixbuf || mlt_properties_get_int( producer_props, "_disable_exif" ) != disable_exif )
304 GError *error = NULL;
307 pthread_mutex_lock( &g_mutex );
308 self->pixbuf = gdk_pixbuf_new_from_file( mlt_properties_get_value( self->filenames, current_idx ), &error );
311 // Read the exif value for this file
313 self->pixbuf = reorient_with_exif( self, current_idx, self->pixbuf );
315 // Register this pixbuf for destruction and reuse
316 mlt_cache_item_close( self->pixbuf_cache );
317 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf", self->pixbuf, 0, ( mlt_destructor )g_object_unref );
318 self->pixbuf_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf" );
319 self->pixbuf_idx = current_idx;
321 mlt_events_block( producer_props, NULL );
322 self->pixbuf_width = gdk_pixbuf_get_width( self->pixbuf );
323 self->pixbuf_height = gdk_pixbuf_get_height( self->pixbuf );
324 mlt_properties_set_int( producer_props, "_disable_exif", disable_exif );
325 mlt_events_unblock( producer_props, NULL );
327 // Store the width/height of the pixbuf temporarily
328 self->width = gdk_pixbuf_get_width( self->pixbuf );
329 self->height = gdk_pixbuf_get_height( self->pixbuf );
331 pthread_mutex_unlock( &g_mutex );
334 // Set width/height of frame
335 mlt_properties_set_int( properties, "width", self->width );
336 mlt_properties_set_int( properties, "height", self->height );
337 mlt_properties_set_int( properties, "real_width", self->pixbuf_width );
338 mlt_properties_set_int( properties, "real_height", self->pixbuf_height );
343 static void refresh_image( producer_pixbuf self, mlt_frame frame, mlt_image_format format, int width, int height )
345 // Obtain properties of frame and producer
346 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
347 mlt_producer producer = &self->parent;
349 // Get index and pixbuf
350 int current_idx = refresh_pixbuf( self, frame );
352 // optimization for subsequent iterations on single picture
353 if ( current_idx != self->image_idx || width != self->width || height != self->height )
355 mlt_log_debug( MLT_PRODUCER_SERVICE( producer ), "image %p pixbuf %p idx %d current_idx %d pixbuf_idx %d width %d\n",
356 self->image, self->pixbuf, current_idx, self->image_idx, self->pixbuf_idx, width );
358 // If we have a pixbuf and we need an image
359 if ( self->pixbuf && ( !self->image || ( format != mlt_image_none && format != self->format ) ) )
361 char *interps = mlt_properties_get( properties, "rescale.interp" );
362 int interp = GDK_INTERP_BILINEAR;
364 if ( strcmp( interps, "nearest" ) == 0 )
365 interp = GDK_INTERP_NEAREST;
366 else if ( strcmp( interps, "tiles" ) == 0 )
367 interp = GDK_INTERP_TILES;
368 else if ( strcmp( interps, "hyper" ) == 0 || strcmp( interps, "bicubic" ) == 0 )
369 interp = GDK_INTERP_HYPER;
371 // Note - the original pixbuf is already safe and ready for destruction
372 pthread_mutex_lock( &g_mutex );
373 GdkPixbuf* pixbuf = gdk_pixbuf_scale_simple( self->pixbuf, width, height, interp );
375 // Store width and height
377 self->height = height;
379 // Allocate/define image
380 int has_alpha = gdk_pixbuf_get_has_alpha( pixbuf );
381 int src_stride = gdk_pixbuf_get_rowstride( pixbuf );
382 int dst_stride = self->width * ( has_alpha ? 4 : 3 );
383 int image_size = dst_stride * ( height + 1 );
384 self->image = mlt_pool_alloc( image_size );
385 self->format = has_alpha ? mlt_image_rgb24a : mlt_image_rgb24;
387 if ( src_stride != dst_stride )
389 int y = self->height;
390 uint8_t *src = gdk_pixbuf_get_pixels( pixbuf );
391 uint8_t *dst = self->image;
394 memcpy( dst, src, dst_stride );
401 memcpy( self->image, gdk_pixbuf_get_pixels( pixbuf ), src_stride * height );
403 pthread_mutex_unlock( &g_mutex );
405 // Convert image to requested format
406 if ( format != mlt_image_none && format != self->format )
408 uint8_t *buffer = NULL;
410 // First, set the image so it can be converted when we get it
411 mlt_frame_set_image( frame, self->image, image_size, mlt_pool_release );
412 mlt_properties_set_int( properties, "format", self->format );
413 mlt_properties_set_int( properties, "width", width );
414 mlt_properties_set_int( properties, "height", height );
415 self->format = format;
417 // get_image will do the format conversion
418 mlt_frame_get_image( frame, &buffer, &format, &width, &height, 0 );
420 // cache copies of the image and alpha buffers
423 image_size = mlt_image_format_size( format, width, height, NULL );
424 self->image = mlt_pool_alloc( image_size );
425 memcpy( self->image, buffer, image_size );
427 if ( ( buffer = mlt_frame_get_alpha_mask( frame ) ) )
429 self->alpha = mlt_pool_alloc( width * height );
430 memcpy( self->alpha, buffer, width * height );
434 mlt_cache_item_close( self->image_cache );
435 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image", self->image, image_size, mlt_pool_release );
436 self->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image" );
437 self->image_idx = current_idx;
438 mlt_cache_item_close( self->alpha_cache );
439 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.alpha", self->alpha, width * height, mlt_pool_release );
440 self->alpha_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.alpha" );
442 // Finished with pixbuf now
443 g_object_unref( pixbuf );
446 // Set width/height of frame
447 mlt_properties_set_int( properties, "width", self->width );
448 mlt_properties_set_int( properties, "height", self->height );
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 and producer
456 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
457 producer_pixbuf self = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
458 mlt_producer producer = &self->parent;
460 // Use the width and height suggested by the rescale filter because we can do our own scaling.
461 *width = mlt_properties_get_int( properties, "rescale_width" );
462 *height = mlt_properties_get_int( properties, "rescale_height" );
464 // Restore pixbuf and image
465 mlt_service_lock( MLT_PRODUCER_SERVICE( producer ) );
466 self->pixbuf_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf" );
467 self->pixbuf = mlt_cache_item_data( self->pixbuf_cache, NULL );
468 self->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image" );
469 self->image = mlt_cache_item_data( self->image_cache, NULL );
470 self->alpha_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.alpha" );
471 self->alpha = mlt_cache_item_data( self->alpha_cache, NULL );
474 refresh_image( self, frame, *format, *width, *height );
476 // Get width and height (may have changed during the refresh)
477 *width = self->width;
478 *height = self->height;
479 *format = self->format;
481 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
482 // The fault is not in the design of mlt, but in the implementation of the pixbuf producer...
486 int image_size = mlt_image_format_size( self->format, self->width, self->height, NULL );
487 uint8_t *image_copy = mlt_pool_alloc( image_size );
488 memcpy( image_copy, self->image, image_size );
489 // Now update properties so we free the copy after
490 mlt_frame_set_image( frame, image_copy, image_size, mlt_pool_release );
491 // We're going to pass the copy on
492 *buffer = image_copy;
493 mlt_log_debug( MLT_PRODUCER_SERVICE( &self->parent ), "%dx%d (%s)\n",
494 self->width, self->height, mlt_image_format_name( *format ) );
495 // Clone the alpha channel
498 image_copy = mlt_pool_alloc( self->width * self->height );
499 memcpy( image_copy, self->alpha, self->width * self->height );
500 mlt_frame_set_alpha( frame, image_copy, self->width * self->height, mlt_pool_release );
508 // Release references and locks
509 mlt_cache_item_close( self->pixbuf_cache );
510 mlt_cache_item_close( self->image_cache );
511 mlt_cache_item_close( self->alpha_cache );
512 mlt_service_unlock( MLT_PRODUCER_SERVICE( &self->parent ) );
517 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
519 // Get the real structure for this producer
520 producer_pixbuf self = producer->child;
522 // Fetch the producers properties
523 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
525 if ( self->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
526 load_filenames( self, producer_properties );
529 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
531 if ( *frame != NULL && self->count > 0 )
533 // Obtain properties of frame and producer
534 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
536 // Set the producer on the frame properties
537 mlt_properties_set_data( properties, "producer_pixbuf", self, 0, NULL, NULL );
539 // Update timecode on the frame we're creating
540 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
542 // Ensure that we have a way to obtain the position in the get_image
543 mlt_properties_set_position( properties, "pixbuf_position", mlt_producer_position( producer ) );
545 // Refresh the pixbuf
546 self->pixbuf_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf" );
547 self->pixbuf = mlt_cache_item_data( self->pixbuf_cache, NULL );
548 refresh_pixbuf( self, *frame );
549 mlt_cache_item_close( self->pixbuf_cache );
551 // Set producer-specific frame properties
552 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( producer_properties, "progressive" ) );
554 double force_ratio = mlt_properties_get_double( producer_properties, "force_aspect_ratio" );
555 if ( force_ratio > 0.0 )
556 mlt_properties_set_double( properties, "aspect_ratio", force_ratio );
558 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
560 // Push the get_image method
561 mlt_frame_push_get_image( *frame, producer_get_image );
564 // Calculate the next timecode
565 mlt_producer_prepare_next( producer );
570 static void producer_close( mlt_producer parent )
572 producer_pixbuf self = parent->child;
573 parent->close = NULL;
574 mlt_service_cache_purge( MLT_PRODUCER_SERVICE(parent) );
575 mlt_producer_close( parent );
576 mlt_properties_close( self->filenames );