3 * producer_pixbuf.c -- raster image loader based upon gdk-pixbuf
4 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
5 * Author: Dan Dennedy <dan@dennedy.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "producer_pixbuf.h"
23 #include <framework/mlt_frame.h>
24 #include <gdk-pixbuf/gdk-pixbuf.h>
31 #include <sys/types.h>
36 static pthread_mutex_t fastmutex = PTHREAD_MUTEX_INITIALIZER;
38 typedef struct producer_pixbuf_s *producer_pixbuf;
40 struct producer_pixbuf_s
42 struct mlt_producer_s parent;
45 mlt_properties filenames;
55 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
56 static void producer_close( mlt_producer parent );
58 mlt_producer producer_pixbuf_init( char *filename )
60 producer_pixbuf this = calloc( sizeof( struct producer_pixbuf_s ), 1 );
61 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
63 mlt_producer producer = &this->parent;
65 // Get the properties interface
66 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
68 // Callback registration
69 producer->get_frame = producer_get_frame;
70 producer->close = ( mlt_destructor )producer_close;
72 // Set the default properties
73 mlt_properties_set( properties, "resource", filename );
74 mlt_properties_set_int( properties, "ttl", 25 );
75 mlt_properties_set_int( properties, "aspect_ratio", 1 );
76 mlt_properties_set_int( properties, "progressive", 1 );
84 static void refresh_image( mlt_frame frame, int width, int height )
87 GdkPixbuf *pixbuf = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "pixbuf", NULL );
90 // Obtain properties of frame
91 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
93 // Obtain the producer for this frame
94 producer_pixbuf this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
96 // Obtain the producer
97 mlt_producer producer = &this->parent;
99 // Obtain properties of producer
100 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
102 // Obtain the cache flag and structure
103 int use_cache = mlt_properties_get_int( producer_props, "cache" );
104 mlt_properties cache = mlt_properties_get_data( producer_props, "_cache", NULL );
105 int update_cache = 0;
107 // Get the time to live for each frame
108 double ttl = mlt_properties_get_int( producer_props, "ttl" );
110 // Get the original position of this frame
111 mlt_position position = mlt_properties_get_position( properties, "pixbuf_position" );
114 int image_idx = ( int )floor( ( double )position / ttl ) % this->count;
117 char image_key[ 10 ];
118 sprintf( image_key, "%d", image_idx );
120 pthread_mutex_lock( &fastmutex );
122 // Check if the frame is already loaded
127 cache = mlt_properties_new( );
128 mlt_properties_set_data( producer_props, "_cache", cache, 0, ( mlt_destructor )mlt_properties_close, NULL );
131 mlt_frame cached = mlt_properties_get_data( cache, image_key, NULL );
135 this->image_idx = image_idx;
136 mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
137 this->width = mlt_properties_get_int( cached_props, "width" );
138 this->height = mlt_properties_get_int( cached_props, "height" );
139 mlt_properties_set_int( producer_props, "_real_width", mlt_properties_get_int( cached_props, "real_width" ) );
140 mlt_properties_set_int( producer_props, "_real_height", mlt_properties_get_int( cached_props, "real_height" ) );
141 this->image = mlt_properties_get_data( cached_props, "image", NULL );
142 this->alpha = mlt_properties_get_data( cached_props, "alpha", NULL );
144 if ( width != 0 && ( width != this->width || height != this->height ) )
149 // optimization for subsequent iterations on single picture
150 if ( width != 0 && this->image != NULL && image_idx == this->image_idx )
152 if ( width != this->width || height != this->height )
154 pixbuf = mlt_properties_get_data( producer_props, "_pixbuf", NULL );
157 mlt_pool_release( this->image );
158 mlt_pool_release( this->alpha );
164 else if ( pixbuf == NULL && ( this->image == NULL || image_idx != this->image_idx ) )
168 mlt_pool_release( this->image );
169 mlt_pool_release( this->alpha );
174 this->image_idx = image_idx;
175 pixbuf = gdk_pixbuf_new_from_file( mlt_properties_get_value( this->filenames, image_idx ), &error );
177 if ( pixbuf != NULL )
179 // Register this pixbuf for destruction and reuse
180 mlt_events_block( producer_props, NULL );
181 mlt_properties_set_data( producer_props, "_pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
182 g_object_ref( pixbuf );
183 mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
185 mlt_properties_set_int( producer_props, "_real_width", gdk_pixbuf_get_width( pixbuf ) );
186 mlt_properties_set_int( producer_props, "_real_height", gdk_pixbuf_get_height( pixbuf ) );
187 mlt_events_unblock( producer_props, NULL );
189 // Store the width/height of the pixbuf temporarily
190 this->width = gdk_pixbuf_get_width( pixbuf );
191 this->height = gdk_pixbuf_get_height( pixbuf );
195 // If we have a pixbuf
196 if ( pixbuf && width > 0 )
198 char *interps = mlt_properties_get( properties, "rescale.interp" );
199 int interp = GDK_INTERP_BILINEAR;
201 if ( strcmp( interps, "nearest" ) == 0 )
202 interp = GDK_INTERP_NEAREST;
203 else if ( strcmp( interps, "tiles" ) == 0 )
204 interp = GDK_INTERP_TILES;
205 else if ( strcmp( interps, "hyper" ) == 0 )
206 interp = GDK_INTERP_HYPER;
208 // Note - the original pixbuf is already safe and ready for destruction
209 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
211 // Store width and height
213 this->height = height;
215 // Allocate/define image
216 this->image = mlt_pool_alloc( width * ( height + 1 ) * 2 );
218 // Extract YUV422 and alpha
219 if ( gdk_pixbuf_get_has_alpha( pixbuf ) )
221 // Allocate the alpha mask
222 this->alpha = mlt_pool_alloc( this->width * this->height );
225 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
226 this->width, this->height,
227 gdk_pixbuf_get_rowstride( pixbuf ),
228 this->image, this->alpha );
232 // No alpha to extract
233 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
234 this->width, this->height,
235 gdk_pixbuf_get_rowstride( pixbuf ),
239 // Finished with pixbuf now
240 g_object_unref( pixbuf );
242 // Ensure we update the cache when we need to
243 update_cache = use_cache;
246 // Set width/height of frame
247 mlt_properties_set_int( properties, "width", this->width );
248 mlt_properties_set_int( properties, "height", this->height );
249 mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
250 mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
252 // pass the image data without destructor
253 mlt_properties_set_data( properties, "image", this->image, this->width * ( this->height + 1 ) * 2, NULL, NULL );
254 mlt_properties_set_data( properties, "alpha", this->alpha, this->width * this->height, NULL, NULL );
258 mlt_frame cached = mlt_frame_init( );
259 mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
260 mlt_properties_set_int( cached_props, "width", this->width );
261 mlt_properties_set_int( cached_props, "height", this->height );
262 mlt_properties_set_int( cached_props, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
263 mlt_properties_set_int( cached_props, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
264 mlt_properties_set_data( cached_props, "image", this->image, this->width * ( this->height + 1 ) * 2, mlt_pool_release, NULL );
265 mlt_properties_set_data( cached_props, "alpha", this->alpha, this->width * this->height, mlt_pool_release, NULL );
266 mlt_properties_set_data( cache, image_key, cached, 0, ( mlt_destructor )mlt_frame_close, NULL );
269 pthread_mutex_unlock( &fastmutex );
272 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
274 // Obtain properties of frame
275 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
277 // We need to know the size of the image to clone it
282 uint8_t *alpha = NULL;
284 *width = mlt_properties_get_int( properties, "rescale_width" );
285 *height = mlt_properties_get_int( properties, "rescale_height" );
288 refresh_image( frame, *width, *height );
291 *buffer = mlt_properties_get_data( properties, "image", &image_size );
292 alpha = mlt_properties_get_data( properties, "alpha", &alpha_size );
294 // Get width and height (may have changed during the refresh)
295 *width = mlt_properties_get_int( properties, "width" );
296 *height = mlt_properties_get_int( properties, "height" );
298 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
299 // The fault is not in the design of mlt, but in the implementation of the pixbuf producer...
300 if ( *buffer != NULL )
302 // Clone the image and the alpha
303 uint8_t *image_copy = mlt_pool_alloc( image_size );
304 uint8_t *alpha_copy = mlt_pool_alloc( alpha_size );
306 memcpy( image_copy, *buffer, image_size );
308 // Copy or default the alpha
310 memcpy( alpha_copy, alpha, alpha_size );
312 memset( alpha_copy, 255, alpha_size );
314 // Now update properties so we free the copy after
315 mlt_properties_set_data( properties, "image", image_copy, image_size, mlt_pool_release, NULL );
316 mlt_properties_set_data( properties, "alpha", alpha_copy, alpha_size, mlt_pool_release, NULL );
318 // We're going to pass the copy on
319 *buffer = image_copy;
323 // TODO: Review all cases of invalid images
324 *buffer = mlt_pool_alloc( 50 * 50 * 2 );
325 mlt_properties_set_data( properties, "image", *buffer, image_size, mlt_pool_release, NULL );
333 static uint8_t *producer_get_alpha_mask( mlt_frame this )
335 // Obtain properties of frame
336 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
338 // Return the alpha mask
339 return mlt_properties_get_data( properties, "alpha", NULL );
342 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
344 // Get the real structure for this producer
345 producer_pixbuf this = producer->child;
347 // Fetch the producers properties
348 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
350 if ( this->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
352 char *filename = mlt_properties_get( producer_properties, "resource" );
353 this->filenames = mlt_properties_new( );
356 if ( strstr( filename, "<svg" ) )
358 // Generate a temporary file for the svg
359 char fullname[ 1024 ] = "/tmp/mlt.XXXXXX";
360 int fd = mkstemp( fullname );
364 // Write the svg into the temp file
365 ssize_t remaining_bytes;
366 char *xml = filename;
368 // Strip leading crap
369 while ( xml[0] != '<' )
372 remaining_bytes = strlen( xml );
373 while ( remaining_bytes > 0 )
374 remaining_bytes -= write( fd, xml + strlen( xml ) - remaining_bytes, remaining_bytes );
377 mlt_properties_set( this->filenames, "0", fullname );
379 // Teehe - when the producer closes, delete the temp file and the space allo
380 mlt_properties_set_data( producer_properties, "__temporary_file__", fullname, 0, ( mlt_destructor )unlink, NULL );
384 else if ( strchr( filename, '%' ) != NULL )
386 // handle picture sequences
387 int i = mlt_properties_get_int( producer_properties, "begin" );
396 snprintf( full, 1023, filename, i ++ );
397 if ( stat( full, &buf ) == 0 )
399 sprintf( key, "%d", keyvalue ++ );
400 mlt_properties_set( this->filenames, "0", full );
409 else if ( strstr( filename, "/.all." ) != NULL )
411 char wildcard[ 1024 ];
412 char *dir_name = strdup( filename );
413 char *extension = strrchr( dir_name, '.' );
415 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
416 sprintf( wildcard, "*%s", extension );
418 mlt_properties_dir_list( this->filenames, dir_name, wildcard, 1 );
424 mlt_properties_set( this->filenames, "0", filename );
427 this->count = mlt_properties_count( this->filenames );
431 *frame = mlt_frame_init( );
433 if ( *frame != NULL && this->count > 0 )
435 // Obtain properties of frame and producer
436 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
438 // Set the producer on the frame properties
439 mlt_properties_set_data( properties, "producer_pixbuf", this, 0, NULL, NULL );
441 // Update timecode on the frame we're creating
442 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
444 // Ensure that we have a way to obtain the position in the get_image
445 mlt_properties_set_position( properties, "pixbuf_position", mlt_producer_position( producer ) );
448 refresh_image( *frame, 0, 0 );
450 // Set producer-specific frame properties
451 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( producer_properties, "progressive" ) );
452 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
454 // Set alpha call back
455 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
457 // Push the get_image method
458 mlt_frame_push_get_image( *frame, producer_get_image );
461 // Calculate the next timecode
462 mlt_producer_prepare_next( producer );
467 static void producer_close( mlt_producer parent )
469 producer_pixbuf this = parent->child;
470 if ( !mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( parent ), "cache" ) )
472 mlt_pool_release( this->image );
473 mlt_pool_release( this->alpha );
475 parent->close = NULL;
476 mlt_producer_close( parent );
477 mlt_properties_close( this->filenames );