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 <gdk-pixbuf/gdk-pixbuf.h>
31 #include <sys/types.h>
36 static pthread_mutex_t g_mutex
= 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
;
53 mlt_cache_item image_cache
;
54 mlt_cache_item alpha_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
);
170 else if ( strstr( filename
, "/.all." ) != NULL
)
172 char wildcard
[ 1024 ];
173 char *dir_name
= strdup( filename
);
174 char *extension
= strrchr( dir_name
, '.' );
176 *( strstr( dir_name
, "/.all." ) + 1 ) = '\0';
177 sprintf( wildcard
, "*%s", extension
);
179 mlt_properties_dir_list( this->filenames
, dir_name
, wildcard
, 1 );
185 mlt_properties_set( this->filenames
, "0", filename
);
188 this->count
= mlt_properties_count( this->filenames
);
191 static void refresh_image( producer_pixbuf
this, mlt_frame frame
, int width
, int height
)
193 // Obtain properties of frame
194 mlt_properties properties
= MLT_FRAME_PROPERTIES( frame
);
196 // Obtain the producer
197 mlt_producer producer
= &this->parent
;
199 // Obtain properties of producer
200 mlt_properties producer_props
= MLT_PRODUCER_PROPERTIES( producer
);
202 // Obtain the cache flag and structure
203 int use_cache
= mlt_properties_get_int( producer_props
, "cache" );
204 mlt_properties cache
= mlt_properties_get_data( producer_props
, "_cache", NULL
);
205 int update_cache
= 0;
208 pthread_mutex_lock( &this->mutex
);
209 mlt_cache_item pixbuf_cache
= mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer
), "pixbuf.pixbuf" );
210 GdkPixbuf
*pixbuf
= mlt_cache_item_data( pixbuf_cache
, NULL
);
211 GError
*error
= NULL
;
213 // restore scaled image
214 this->image_cache
= mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer
), "pixbuf.image" );
215 this->image
= mlt_cache_item_data( this->image_cache
, NULL
);
217 // restore alpha channel
218 this->alpha_cache
= mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer
), "pixbuf.alpha" );
219 this->alpha
= mlt_cache_item_data( this->alpha_cache
, NULL
);
221 // Check if user wants us to reload the image
222 if ( mlt_properties_get_int( producer_props
, "force_reload" ) )
226 mlt_properties_set_int( producer_props
, "force_reload", 0 );
229 // Get the time to live for each frame
230 double ttl
= mlt_properties_get_int( producer_props
, "ttl" );
232 // Get the original position of this frame
233 mlt_position position
= mlt_properties_get_position( properties
, "pixbuf_position" );
234 position
+= mlt_producer_get_in( producer
);
237 int image_idx
= ( int )floor( ( double )position
/ ttl
) % this->count
;
240 char image_key
[ 10 ];
241 sprintf( image_key
, "%d", image_idx
);
243 pthread_mutex_lock( &g_mutex
);
245 // Check if the frame is already loaded
250 cache
= mlt_properties_new( );
251 mlt_properties_set_data( producer_props
, "_cache", cache
, 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
254 mlt_frame cached
= mlt_properties_get_data( cache
, image_key
, NULL
);
258 this->image_idx
= image_idx
;
259 mlt_properties cached_props
= MLT_FRAME_PROPERTIES( cached
);
260 this->width
= mlt_properties_get_int( cached_props
, "width" );
261 this->height
= mlt_properties_get_int( cached_props
, "height" );
262 mlt_properties_set_int( producer_props
, "_real_width", mlt_properties_get_int( cached_props
, "real_width" ) );
263 mlt_properties_set_int( producer_props
, "_real_height", mlt_properties_get_int( cached_props
, "real_height" ) );
264 this->image
= mlt_properties_get_data( cached_props
, "image", NULL
);
265 this->alpha
= mlt_properties_get_data( cached_props
, "alpha", NULL
);
267 if ( width
!= 0 && ( width
!= this->width
|| height
!= this->height
) )
272 // optimization for subsequent iterations on single picture
273 if ( width
!= 0 && ( image_idx
!= this->image_idx
|| width
!= this->width
|| height
!= this->height
) )
275 if ( image_idx
!= this->image_idx
)
277 if ( pixbuf
== NULL
&& ( width
== 0 || this->image
== NULL
) )
280 this->image_idx
= image_idx
;
281 pixbuf
= gdk_pixbuf_new_from_file( mlt_properties_get_value( this->filenames
, image_idx
), &error
);
285 // Register this pixbuf for destruction and reuse
286 mlt_cache_item_close( pixbuf_cache
);
287 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer
), "pixbuf.pixbuf", pixbuf
, 0, ( mlt_destructor
)g_object_unref
);
288 pixbuf_cache
= mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer
), "pixbuf.pixbuf" );
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
== NULL
)
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->image
= mlt_pool_alloc( width
* ( height
+ 1 ) * 2 );
324 mlt_cache_item_close( this->image_cache
);
325 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer
), "pixbuf.image", this->image
, width
* ( height
+ 1 ) * 2, mlt_pool_release
);
326 this->image_cache
= mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer
), "pixbuf.image" );
328 // Extract YUV422 and alpha
329 if ( gdk_pixbuf_get_has_alpha( pixbuf
) )
331 // Allocate the alpha mask
332 this->alpha
= mlt_pool_alloc( this->width
* this->height
);
334 mlt_cache_item_close( this->alpha_cache
);
335 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer
), "pixbuf.alpha", this->alpha
, width
* height
, mlt_pool_release
);
336 this->alpha_cache
= mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer
), "pixbuf.alpha" );
339 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf
),
340 this->width
, this->height
,
341 gdk_pixbuf_get_rowstride( pixbuf
),
342 this->image
, this->alpha
);
346 // No alpha to extract
347 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf
),
348 this->width
, this->height
,
349 gdk_pixbuf_get_rowstride( pixbuf
),
353 // Finished with pixbuf now
354 g_object_unref( pixbuf
);
356 // Ensure we update the cache when we need to
357 update_cache
= use_cache
;
360 // release references no longer needed
361 mlt_cache_item_close( pixbuf_cache
);
364 pthread_mutex_unlock( &this->mutex
);
365 mlt_cache_item_close( this->image_cache
);
366 mlt_cache_item_close( this->alpha_cache
);
369 // Set width/height of frame
370 mlt_properties_set_int( properties
, "width", this->width
);
371 mlt_properties_set_int( properties
, "height", this->height
);
372 mlt_properties_set_int( properties
, "real_width", mlt_properties_get_int( producer_props
, "_real_width" ) );
373 mlt_properties_set_int( properties
, "real_height", mlt_properties_get_int( producer_props
, "_real_height" ) );
377 mlt_frame cached
= mlt_frame_init( MLT_PRODUCER_SERVICE( producer
) );
378 mlt_properties cached_props
= MLT_FRAME_PROPERTIES( cached
);
379 mlt_properties_set_int( cached_props
, "width", this->width
);
380 mlt_properties_set_int( cached_props
, "height", this->height
);
381 mlt_properties_set_int( cached_props
, "real_width", mlt_properties_get_int( producer_props
, "_real_width" ) );
382 mlt_properties_set_int( cached_props
, "real_height", mlt_properties_get_int( producer_props
, "_real_height" ) );
383 mlt_properties_set_data( cached_props
, "image", this->image
, this->width
* ( this->height
+ 1 ) * 2, mlt_pool_release
, NULL
);
384 mlt_properties_set_data( cached_props
, "alpha", this->alpha
, this->width
* this->height
, mlt_pool_release
, NULL
);
385 mlt_properties_set_data( cache
, image_key
, cached
, 0, ( mlt_destructor
)mlt_frame_close
, NULL
);
388 pthread_mutex_unlock( &g_mutex
);
391 static int producer_get_image( mlt_frame frame
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
393 // Obtain properties of frame
394 mlt_properties properties
= MLT_FRAME_PROPERTIES( frame
);
396 // Obtain the producer for this frame
397 producer_pixbuf
this = mlt_properties_get_data( properties
, "producer_pixbuf", NULL
);
399 *width
= mlt_properties_get_int( properties
, "rescale_width" );
400 *height
= mlt_properties_get_int( properties
, "rescale_height" );
403 refresh_image( this, frame
, *width
, *height
);
405 // Get the image size
406 int image_size
= this->width
* ( this->height
+ 1 ) * 2;
407 int alpha_size
= this->width
* this->height
;
409 // Get width and height (may have changed during the refresh)
410 *width
= mlt_properties_get_int( properties
, "width" );
411 *height
= mlt_properties_get_int( properties
, "height" );
413 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
414 // The fault is not in the design of mlt, but in the implementation of the pixbuf producer...
417 if ( *format
== mlt_image_yuv422
|| *format
== mlt_image_yuv420p
)
419 // Clone the image and the alpha
420 uint8_t *image_copy
= mlt_pool_alloc( image_size
);
421 uint8_t *alpha_copy
= mlt_pool_alloc( alpha_size
);
423 memcpy( image_copy
, this->image
, image_size
);
425 // Copy or default the alpha
426 if ( this->alpha
!= NULL
)
427 memcpy( alpha_copy
, this->alpha
, alpha_size
);
429 memset( alpha_copy
, 255, alpha_size
);
431 // Now update properties so we free the copy after
432 mlt_properties_set_data( properties
, "image", image_copy
, image_size
, mlt_pool_release
, NULL
);
433 mlt_properties_set_data( properties
, "alpha", alpha_copy
, alpha_size
, mlt_pool_release
, NULL
);
435 // We're going to pass the copy on
436 *buffer
= image_copy
;
438 else if ( *format
== mlt_image_rgb24a
)
440 // Clone the image and the alpha
441 image_size
= *width
* ( *height
+ 1 ) * 4;
442 alpha_size
= *width
* ( *height
+ 1 );
443 uint8_t *image_copy
= mlt_pool_alloc( image_size
);
444 uint8_t *alpha_copy
= mlt_pool_alloc( alpha_size
);
446 mlt_convert_yuv422_to_rgb24a( this->image
, image_copy
, (*width
)*(*height
) );
448 // Now update properties so we free the copy after
449 mlt_properties_set_data( properties
, "image", image_copy
, image_size
, mlt_pool_release
, NULL
);
450 mlt_properties_set_data( properties
, "alpha", alpha_copy
, alpha_size
, mlt_pool_release
, NULL
);
452 // We're going to pass the copy on
453 *buffer
= image_copy
;
459 // TODO: Review all cases of invalid images
460 *buffer
= mlt_pool_alloc( 50 * 50 * 2 );
461 mlt_properties_set_data( properties
, "image", *buffer
, image_size
, mlt_pool_release
, NULL
);
466 // Release references and locks
467 pthread_mutex_unlock( &this->mutex
);
468 mlt_cache_item_close( this->image_cache
);
469 mlt_cache_item_close( this->alpha_cache
);
474 static uint8_t *producer_get_alpha_mask( mlt_frame
this )
476 // Obtain properties of frame
477 mlt_properties properties
= MLT_FRAME_PROPERTIES( this );
479 // Return the alpha mask
480 return mlt_properties_get_data( properties
, "alpha", NULL
);
483 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
485 // Get the real structure for this producer
486 producer_pixbuf
this = producer
->child
;
488 // Fetch the producers properties
489 mlt_properties producer_properties
= MLT_PRODUCER_PROPERTIES( producer
);
491 if ( this->filenames
== NULL
&& mlt_properties_get( producer_properties
, "resource" ) != NULL
)
492 load_filenames( this, producer_properties
);
495 *frame
= mlt_frame_init( MLT_PRODUCER_SERVICE( producer
) );
497 if ( *frame
!= NULL
&& this->count
> 0 )
499 // Obtain properties of frame and producer
500 mlt_properties properties
= MLT_FRAME_PROPERTIES( *frame
);
502 // Set the producer on the frame properties
503 mlt_properties_set_data( properties
, "producer_pixbuf", this, 0, NULL
, NULL
);
505 // Update timecode on the frame we're creating
506 mlt_frame_set_position( *frame
, mlt_producer_position( producer
) );
508 // Ensure that we have a way to obtain the position in the get_image
509 mlt_properties_set_position( properties
, "pixbuf_position", mlt_producer_position( producer
) );
512 refresh_image( this, *frame
, 0, 0 );
514 // Set producer-specific frame properties
515 mlt_properties_set_int( properties
, "progressive", mlt_properties_get_int( producer_properties
, "progressive" ) );
516 mlt_properties_set_double( properties
, "aspect_ratio", mlt_properties_get_double( producer_properties
, "aspect_ratio" ) );
518 // Set alpha call back
519 ( *frame
)->get_alpha_mask
= producer_get_alpha_mask
;
521 // Push the get_image method
522 mlt_frame_push_get_image( *frame
, producer_get_image
);
525 // Calculate the next timecode
526 mlt_producer_prepare_next( producer
);
531 static void producer_close( mlt_producer parent
)
533 producer_pixbuf
this = parent
->child
;
534 pthread_mutex_destroy( &this->mutex
);
535 parent
->close
= NULL
;
536 mlt_producer_close( parent
);
537 mlt_properties_close( this->filenames
);