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>
32 #include <sys/types.h>
37 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
39 typedef struct producer_pixbuf_s *producer_pixbuf;
41 struct producer_pixbuf_s
43 struct mlt_producer_s parent;
46 mlt_properties filenames;
54 mlt_cache_item image_cache;
55 pthread_mutex_t mutex;
58 static void load_filenames( producer_pixbuf this, mlt_properties producer_properties );
59 static void refresh_image( producer_pixbuf this, mlt_frame frame, int width, int height );
60 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
61 static void producer_close( mlt_producer parent );
63 mlt_producer producer_pixbuf_init( char *filename )
65 producer_pixbuf this = calloc( sizeof( struct producer_pixbuf_s ), 1 );
66 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
68 mlt_producer producer = &this->parent;
70 // Get the properties interface
71 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
73 // Callback registration
74 producer->get_frame = producer_get_frame;
75 producer->close = ( mlt_destructor )producer_close;
77 // Set the default properties
78 mlt_properties_set( properties, "resource", filename );
79 mlt_properties_set_int( properties, "ttl", 25 );
80 mlt_properties_set_int( properties, "aspect_ratio", 1 );
81 mlt_properties_set_int( properties, "progressive", 1 );
83 // Validate the resource
85 load_filenames( this, properties );
88 mlt_frame frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
91 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
92 pthread_mutex_init( &this->mutex, NULL );
93 mlt_properties_set_data( frame_properties, "producer_pixbuf", this, 0, NULL, NULL );
94 mlt_frame_set_position( frame, mlt_producer_position( producer ) );
95 mlt_properties_set_position( frame_properties, "pixbuf_position", mlt_producer_position( producer ) );
96 refresh_image( this, frame, 0, 0 );
97 mlt_frame_close( frame );
100 if ( this->width == 0 )
102 producer_close( producer );
111 static void load_filenames( producer_pixbuf this, mlt_properties producer_properties )
113 char *filename = mlt_properties_get( producer_properties, "resource" );
114 this->filenames = mlt_properties_new( );
117 if ( strstr( filename, "<svg" ) )
119 // Generate a temporary file for the svg
120 char fullname[ 1024 ] = "/tmp/mlt.XXXXXX";
121 int fd = mkstemp( fullname );
125 // Write the svg into the temp file
126 ssize_t remaining_bytes;
127 char *xml = filename;
129 // Strip leading crap
130 while ( xml[0] != '<' )
133 remaining_bytes = strlen( xml );
134 while ( remaining_bytes > 0 )
135 remaining_bytes -= write( fd, xml + strlen( xml ) - remaining_bytes, remaining_bytes );
138 mlt_properties_set( this->filenames, "0", fullname );
140 // Teehe - when the producer closes, delete the temp file and the space allo
141 mlt_properties_set_data( producer_properties, "__temporary_file__", fullname, 0, ( mlt_destructor )unlink, NULL );
145 else if ( strchr( filename, '%' ) != NULL )
147 // handle picture sequences
148 int i = mlt_properties_get_int( producer_properties, "begin" );
157 snprintf( full, 1023, filename, i ++ );
158 if ( stat( full, &buf ) == 0 )
160 sprintf( key, "%d", keyvalue ++ );
161 mlt_properties_set( this->filenames, key, full );
169 if ( mlt_properties_count( this->filenames ) > 0 )
170 mlt_properties_set_int( producer_properties, "ttl", 1 );
172 else if ( strstr( filename, "/.all." ) != NULL )
174 char wildcard[ 1024 ];
175 char *dir_name = strdup( filename );
176 char *extension = strrchr( dir_name, '.' );
178 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
179 sprintf( wildcard, "*%s", extension );
181 mlt_properties_dir_list( this->filenames, dir_name, wildcard, 1 );
187 mlt_properties_set( this->filenames, "0", filename );
190 this->count = mlt_properties_count( this->filenames );
193 static void refresh_image( producer_pixbuf this, mlt_frame frame, int width, int height )
195 // Obtain properties of frame
196 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
198 // Obtain the producer
199 mlt_producer producer = &this->parent;
201 // Obtain properties of producer
202 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
204 // Obtain the cache flag and structure
205 int use_cache = mlt_properties_get_int( producer_props, "cache" );
206 mlt_properties cache = mlt_properties_get_data( producer_props, "_cache", NULL );
207 int update_cache = 0;
210 pthread_mutex_lock( &this->mutex );
211 mlt_cache_item pixbuf_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf" );
212 GdkPixbuf *pixbuf = mlt_cache_item_data( pixbuf_cache, NULL );
213 GError *error = NULL;
215 // restore scaled image
216 this->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image" );
217 this->image = mlt_cache_item_data( this->image_cache, NULL );
219 // Check if user wants us to reload the image
220 if ( mlt_properties_get_int( producer_props, "force_reload" ) )
224 mlt_properties_set_int( producer_props, "force_reload", 0 );
227 // Get the time to live for each frame
228 double ttl = mlt_properties_get_int( producer_props, "ttl" );
230 // Get the original position of this frame
231 mlt_position position = mlt_properties_get_position( properties, "pixbuf_position" );
232 position += mlt_producer_get_in( producer );
235 int image_idx = ( int )floor( ( double )position / ttl ) % this->count;
238 char image_key[ 10 ];
239 sprintf( image_key, "%d", image_idx );
241 pthread_mutex_lock( &g_mutex );
243 // Check if the frame is already loaded
248 cache = mlt_properties_new( );
249 mlt_properties_set_data( producer_props, "_cache", cache, 0, ( mlt_destructor )mlt_properties_close, NULL );
252 mlt_frame cached = mlt_properties_get_data( cache, image_key, NULL );
256 this->image_idx = image_idx;
257 mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
258 this->width = mlt_properties_get_int( cached_props, "width" );
259 this->height = mlt_properties_get_int( cached_props, "height" );
260 mlt_properties_set_int( producer_props, "_real_width", mlt_properties_get_int( cached_props, "real_width" ) );
261 mlt_properties_set_int( producer_props, "_real_height", mlt_properties_get_int( cached_props, "real_height" ) );
262 this->image = mlt_properties_get_data( cached_props, "image", NULL );
263 this->alpha = mlt_properties_get_int( cached_props, "alpha" );
265 if ( width != 0 && ( width != this->width || height != this->height ) )
270 // optimization for subsequent iterations on single picture
271 if ( width != 0 && ( image_idx != this->image_idx || width != this->width || height != this->height ) )
273 if ( image_idx != this->pixbuf_idx )
275 mlt_log_debug( MLT_PRODUCER_SERVICE( producer ), "image %p pixbuf %p idx %d image_idx %d pixbuf_idx %d width %d\n",
276 this->image, pixbuf, image_idx, this->image_idx, this->pixbuf_idx, width );
277 if ( !pixbuf && !this->image )
280 pixbuf = gdk_pixbuf_new_from_file( mlt_properties_get_value( this->filenames, image_idx ), &error );
284 // Register this pixbuf for destruction and reuse
285 mlt_cache_item_close( pixbuf_cache );
286 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref );
287 pixbuf_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf" );
288 this->pixbuf_idx = image_idx;
290 mlt_events_block( producer_props, NULL );
291 mlt_properties_set_int( producer_props, "_real_width", gdk_pixbuf_get_width( pixbuf ) );
292 mlt_properties_set_int( producer_props, "_real_height", gdk_pixbuf_get_height( pixbuf ) );
293 mlt_events_unblock( producer_props, NULL );
295 // Store the width/height of the pixbuf temporarily
296 this->width = gdk_pixbuf_get_width( pixbuf );
297 this->height = gdk_pixbuf_get_height( pixbuf );
301 // If we have a pixbuf and we need an image
302 if ( pixbuf && width > 0 && !this->image )
304 char *interps = mlt_properties_get( properties, "rescale.interp" );
305 int interp = GDK_INTERP_BILINEAR;
307 if ( strcmp( interps, "nearest" ) == 0 )
308 interp = GDK_INTERP_NEAREST;
309 else if ( strcmp( interps, "tiles" ) == 0 )
310 interp = GDK_INTERP_TILES;
311 else if ( strcmp( interps, "hyper" ) == 0 )
312 interp = GDK_INTERP_HYPER;
314 // Note - the original pixbuf is already safe and ready for destruction
315 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
317 // Store width and height
319 this->height = height;
321 // Allocate/define image
322 this->alpha = gdk_pixbuf_get_has_alpha( pixbuf );
323 int src_stride = gdk_pixbuf_get_rowstride( pixbuf );
324 int dst_stride = this->width * ( this->alpha ? 4 : 3 );
325 int image_size = dst_stride * ( height + 1 );
326 this->image = mlt_pool_alloc( image_size );
328 if ( src_stride != dst_stride )
330 int y = this->height;
331 uint8_t *src = gdk_pixbuf_get_pixels( pixbuf );
332 uint8_t *dst = this->image;
335 memcpy( dst, src, dst_stride );
342 memcpy( this->image, gdk_pixbuf_get_pixels( pixbuf ), src_stride * height );
345 mlt_cache_item_close( this->image_cache );
346 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image", this->image, image_size, mlt_pool_release );
347 this->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image" );
348 this->image_idx = image_idx;
350 // Finished with pixbuf now
351 g_object_unref( pixbuf );
353 // Ensure we update the cache when we need to
354 update_cache = use_cache;
357 // release references no longer needed
358 mlt_cache_item_close( pixbuf_cache );
361 pthread_mutex_unlock( &this->mutex );
362 mlt_cache_item_close( this->image_cache );
365 // Set width/height of frame
366 mlt_properties_set_int( properties, "width", this->width );
367 mlt_properties_set_int( properties, "height", this->height );
368 mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
369 mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
373 mlt_frame cached = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
374 mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
375 mlt_properties_set_int( cached_props, "width", this->width );
376 mlt_properties_set_int( cached_props, "height", this->height );
377 mlt_properties_set_int( cached_props, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
378 mlt_properties_set_int( cached_props, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
379 mlt_properties_set_data( cached_props, "image", this->image, this->width * ( this->alpha ? 4 : 3 ) * this->height, mlt_pool_release, NULL );
380 mlt_properties_set_int( cached_props, "alpha", this->alpha );
381 mlt_properties_set_data( cache, image_key, cached, 0, ( mlt_destructor )mlt_frame_close, NULL );
384 pthread_mutex_unlock( &g_mutex );
387 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
389 // Obtain properties of frame
390 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
392 // Obtain the producer for this frame
393 producer_pixbuf this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
395 *width = mlt_properties_get_int( properties, "rescale_width" );
396 *height = mlt_properties_get_int( properties, "rescale_height" );
399 refresh_image( this, frame, *width, *height );
401 // Get width and height (may have changed during the refresh)
402 *width = this->width;
403 *height = this->height;
405 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
406 // The fault is not in the design of mlt, but in the implementation of the pixbuf producer...
410 int image_size = this->width * this->height * ( this->alpha ? 4 :3 );
411 uint8_t *image_copy = mlt_pool_alloc( image_size );
412 memcpy( image_copy, this->image, image_size );
413 // Now update properties so we free the copy after
414 mlt_properties_set_data( properties, "image", image_copy, image_size, mlt_pool_release, NULL );
415 // We're going to pass the copy on
416 *buffer = image_copy;
417 *format = this->alpha ? mlt_image_rgb24a : mlt_image_rgb24;
418 mlt_log_debug( MLT_PRODUCER_SERVICE( &this->parent ), "%dx%d (%s)\n",
419 this->width, this->height, mlt_image_format_name( *format ) );
423 // TODO: Review all cases of invalid images
424 *buffer = mlt_pool_alloc( 50 * 50 * 2 );
425 mlt_properties_set_data( properties, "image", *buffer, 50 * 50 * 2, mlt_pool_release, NULL );
428 *format = mlt_image_yuv422;
431 // Release references and locks
432 pthread_mutex_unlock( &this->mutex );
433 mlt_cache_item_close( this->image_cache );
438 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
440 // Get the real structure for this producer
441 producer_pixbuf this = producer->child;
443 // Fetch the producers properties
444 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
446 if ( this->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
447 load_filenames( this, producer_properties );
450 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
452 if ( *frame != NULL && this->count > 0 )
454 // Obtain properties of frame and producer
455 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
457 // Set the producer on the frame properties
458 mlt_properties_set_data( properties, "producer_pixbuf", this, 0, NULL, NULL );
460 // Update timecode on the frame we're creating
461 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
463 // Ensure that we have a way to obtain the position in the get_image
464 mlt_properties_set_position( properties, "pixbuf_position", mlt_producer_position( producer ) );
467 refresh_image( this, *frame, 0, 0 );
469 // Set producer-specific frame properties
470 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( producer_properties, "progressive" ) );
471 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
473 // Push the get_image method
474 mlt_frame_push_get_image( *frame, producer_get_image );
477 // Calculate the next timecode
478 mlt_producer_prepare_next( producer );
483 static void producer_close( mlt_producer parent )
485 producer_pixbuf this = parent->child;
486 pthread_mutex_destroy( &this->mutex );
487 parent->close = NULL;
488 mlt_producer_close( parent );
489 mlt_properties_close( this->filenames );