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 self, mlt_properties producer_properties );
65 static void refresh_image( producer_pixbuf self, 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 self = calloc( sizeof( struct producer_pixbuf_s ), 1 );
72 if ( self != NULL && mlt_producer_init( &self->parent, self ) == 0 )
74 mlt_producer producer = &self->parent;
76 // Get the properties interface
77 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &self->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( self, 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( &self->mutex, NULL );
99 mlt_properties_set_data( frame_properties, "producer_pixbuf", self, 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( self, frame, 0, 0 );
103 mlt_frame_close( frame );
106 if ( self->width == 0 )
108 producer_close( producer );
117 static void load_filenames( producer_pixbuf self, mlt_properties producer_properties )
119 char *filename = mlt_properties_get( producer_properties, "resource" );
120 self->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 = g_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( self->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( self->filenames, key, full );
175 if ( mlt_properties_count( self->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( self->filenames, dir_name, wildcard, 1 );
193 mlt_properties_set( self->filenames, "0", filename );
196 self->count = mlt_properties_count( self->filenames );
199 static void refresh_image( producer_pixbuf self, 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 = &self->parent;
207 // Obtain properties of producer
208 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
211 pthread_mutex_lock( &self->mutex );
212 mlt_cache_item pixbuf_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf" );
213 GdkPixbuf *pixbuf = mlt_cache_item_data( pixbuf_cache, NULL );
214 GError *error = NULL;
216 // restore scaled image
217 self->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image" );
218 self->image = mlt_cache_item_data( self->image_cache, NULL );
220 // Check if user wants us to reload the image
221 if ( mlt_properties_get_int( producer_props, "force_reload" ) )
225 mlt_properties_set_int( producer_props, "force_reload", 0 );
228 // Get the time to live for each frame
229 double ttl = mlt_properties_get_int( producer_props, "ttl" );
231 // Get the original position of this frame
232 mlt_position position = mlt_properties_get_position( properties, "pixbuf_position" );
233 position += mlt_producer_get_in( producer );
236 int image_idx = ( int )floor( ( double )position / ttl ) % self->count;
239 char image_key[ 10 ];
240 sprintf( image_key, "%d", image_idx );
242 pthread_mutex_lock( &g_mutex );
244 int disable_exif = mlt_properties_get_int( producer_props, "disable_exif" );
246 // optimization for subsequent iterations on single picture
247 if ( width != 0 && ( image_idx != self->image_idx || width != self->width || height != self->height ) )
249 if ( image_idx != self->pixbuf_idx )
251 mlt_log_debug( MLT_PRODUCER_SERVICE( producer ), "image %p pixbuf %p idx %d image_idx %d pixbuf_idx %d width %d\n",
252 self->image, pixbuf, image_idx, self->image_idx, self->pixbuf_idx, width );
253 if ( !pixbuf || mlt_properties_get_int( producer_props, "_disable_exif" ) != disable_exif )
256 pixbuf = gdk_pixbuf_new_from_file( mlt_properties_get_value( self->filenames, image_idx ), &error );
261 // Read the exif value for this file
262 if ( disable_exif == 0) {
263 ExifData *d = exif_data_new_from_file( mlt_properties_get_value( self->filenames, image_idx ) );
265 int exif_orientation = 0;
267 /* get orientation and rotate image accordingly if necessary */
269 if ( ( entry = exif_content_get_entry ( d->ifd[EXIF_IFD_0], EXIF_TAG_ORIENTATION ) ) )
270 exif_orientation = exif_get_short (entry->data, exif_data_get_byte_order (d));
272 /* Free the EXIF data */
276 // Remember EXIF value, might be useful for someone
277 mlt_properties_set_int( producer_props, "_exif_orientation" , exif_orientation );
279 if ( exif_orientation > 1 )
281 GdkPixbuf *processed = NULL;
282 GdkPixbufRotation matrix = GDK_PIXBUF_ROTATE_NONE;
284 // Rotate image according to exif data
285 switch ( exif_orientation ) {
287 processed = gdk_pixbuf_flip ( pixbuf, TRUE );
290 matrix = GDK_PIXBUF_ROTATE_UPSIDEDOWN;
294 processed = gdk_pixbuf_flip ( pixbuf, FALSE );
297 matrix = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
298 processed = gdk_pixbuf_flip ( pixbuf, TRUE );
301 matrix = GDK_PIXBUF_ROTATE_CLOCKWISE;
305 matrix = GDK_PIXBUF_ROTATE_CLOCKWISE;
306 processed = gdk_pixbuf_flip ( pixbuf, TRUE );
309 matrix = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
314 pixbuf = gdk_pixbuf_rotate_simple( processed, matrix );
319 // Register this pixbuf for destruction and reuse
320 mlt_cache_item_close( pixbuf_cache );
321 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref );
322 pixbuf_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf" );
323 self->pixbuf_idx = image_idx;
325 mlt_events_block( producer_props, NULL );
326 mlt_properties_set_int( producer_props, "_real_width", gdk_pixbuf_get_width( pixbuf ) );
327 mlt_properties_set_int( producer_props, "_real_height", gdk_pixbuf_get_height( pixbuf ) );
328 mlt_properties_set_int( producer_props, "_disable_exif", disable_exif );
329 mlt_events_unblock( producer_props, NULL );
331 // Store the width/height of the pixbuf temporarily
332 self->width = gdk_pixbuf_get_width( pixbuf );
333 self->height = gdk_pixbuf_get_height( pixbuf );
337 // If we have a pixbuf and we need an image
338 if ( pixbuf && width > 0 && !self->image )
340 char *interps = mlt_properties_get( properties, "rescale.interp" );
341 int interp = GDK_INTERP_BILINEAR;
343 if ( strcmp( interps, "nearest" ) == 0 )
344 interp = GDK_INTERP_NEAREST;
345 else if ( strcmp( interps, "tiles" ) == 0 )
346 interp = GDK_INTERP_TILES;
347 else if ( strcmp( interps, "hyper" ) == 0 || strcmp( interps, "bicubic" ) == 0 )
348 interp = GDK_INTERP_HYPER;
350 // Note - the original pixbuf is already safe and ready for destruction
351 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
353 // Store width and height
355 self->height = height;
357 // Allocate/define image
358 self->alpha = gdk_pixbuf_get_has_alpha( pixbuf );
359 int src_stride = gdk_pixbuf_get_rowstride( pixbuf );
360 int dst_stride = self->width * ( self->alpha ? 4 : 3 );
361 int image_size = dst_stride * ( height + 1 );
362 self->image = mlt_pool_alloc( image_size );
364 if ( src_stride != dst_stride )
366 int y = self->height;
367 uint8_t *src = gdk_pixbuf_get_pixels( pixbuf );
368 uint8_t *dst = self->image;
371 memcpy( dst, src, dst_stride );
378 memcpy( self->image, gdk_pixbuf_get_pixels( pixbuf ), src_stride * height );
380 mlt_cache_item_close( self->image_cache );
381 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image", self->image, image_size, mlt_pool_release );
382 self->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image" );
383 self->image_idx = image_idx;
385 // Finished with pixbuf now
386 g_object_unref( pixbuf );
389 // release references no longer needed
390 mlt_cache_item_close( pixbuf_cache );
393 pthread_mutex_unlock( &self->mutex );
394 mlt_cache_item_close( self->image_cache );
397 // Set width/height of frame
398 mlt_properties_set_int( properties, "width", self->width );
399 mlt_properties_set_int( properties, "height", self->height );
400 mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
401 mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
403 pthread_mutex_unlock( &g_mutex );
406 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
410 // Obtain properties of frame
411 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
413 // Obtain the producer for this frame
414 producer_pixbuf self = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
416 *width = mlt_properties_get_int( properties, "rescale_width" );
417 *height = mlt_properties_get_int( properties, "rescale_height" );
419 mlt_service_lock( MLT_PRODUCER_SERVICE( &self->parent ) );
422 refresh_image( self, frame, *width, *height );
424 // Get width and height (may have changed during the refresh)
425 *width = self->width;
426 *height = self->height;
427 *format = self->alpha ? mlt_image_rgb24a : mlt_image_rgb24;
429 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
430 // The fault is not in the design of mlt, but in the implementation of the pixbuf producer...
434 int image_size = self->width * self->height * ( self->alpha ? 4 :3 );
435 uint8_t *image_copy = mlt_pool_alloc( image_size );
436 memcpy( image_copy, self->image, image_size );
437 // Now update properties so we free the copy after
438 mlt_frame_set_image( frame, image_copy, image_size, mlt_pool_release );
439 // We're going to pass the copy on
440 *buffer = image_copy;
441 mlt_log_debug( MLT_PRODUCER_SERVICE( &self->parent ), "%dx%d (%s)\n",
442 self->width, self->height, mlt_image_format_name( *format ) );
449 // Release references and locks
450 pthread_mutex_unlock( &self->mutex );
451 mlt_cache_item_close( self->image_cache );
452 mlt_service_unlock( MLT_PRODUCER_SERVICE( &self->parent ) );
457 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
459 // Get the real structure for this producer
460 producer_pixbuf self = producer->child;
462 // Fetch the producers properties
463 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
465 if ( self->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
466 load_filenames( self, producer_properties );
469 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
471 if ( *frame != NULL && self->count > 0 )
473 // Obtain properties of frame and producer
474 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
476 // Set the producer on the frame properties
477 mlt_properties_set_data( properties, "producer_pixbuf", self, 0, NULL, NULL );
479 // Update timecode on the frame we're creating
480 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
482 // Ensure that we have a way to obtain the position in the get_image
483 mlt_properties_set_position( properties, "pixbuf_position", mlt_producer_position( producer ) );
486 refresh_image( self, *frame, 0, 0 );
488 // Set producer-specific frame properties
489 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( producer_properties, "progressive" ) );
491 double force_ratio = mlt_properties_get_double( producer_properties, "force_aspect_ratio" );
492 if ( force_ratio > 0.0 )
493 mlt_properties_set_double( properties, "aspect_ratio", force_ratio );
495 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
497 // Push the get_image method
498 mlt_frame_push_get_image( *frame, producer_get_image );
501 // Calculate the next timecode
502 mlt_producer_prepare_next( producer );
507 static void producer_close( mlt_producer parent )
509 producer_pixbuf self = parent->child;
510 pthread_mutex_destroy( &self->mutex );
511 parent->close = NULL;
512 mlt_service_cache_purge( MLT_PRODUCER_SERVICE(parent) );
513 mlt_producer_close( parent );
514 mlt_properties_close( self->filenames );