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 <gdk-pixbuf/gdk-pixbuf.h>
30 #include <sys/types.h>
35 static pthread_mutex_t fastmutex
= PTHREAD_MUTEX_INITIALIZER
;
37 typedef struct producer_pixbuf_s
*producer_pixbuf
;
39 struct producer_pixbuf_s
41 struct mlt_producer_s parent
;
44 mlt_properties filenames
;
54 static void load_filenames( producer_pixbuf
this, mlt_properties producer_properties
);
55 static void refresh_image( mlt_frame frame
, int width
, int height
);
56 static int producer_get_frame( mlt_producer parent
, mlt_frame_ptr frame
, int index
);
57 static void producer_close( mlt_producer parent
);
59 mlt_producer
producer_pixbuf_init( char *filename
)
61 producer_pixbuf
this = calloc( sizeof( struct producer_pixbuf_s
), 1 );
62 if ( this != NULL
&& mlt_producer_init( &this->parent
, this ) == 0 )
64 mlt_producer producer
= &this->parent
;
66 // Get the properties interface
67 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( &this->parent
);
69 // Callback registration
70 producer
->get_frame
= producer_get_frame
;
71 producer
->close
= ( mlt_destructor
)producer_close
;
73 // Set the default properties
74 mlt_properties_set( properties
, "resource", filename
);
75 mlt_properties_set_int( properties
, "ttl", 25 );
76 mlt_properties_set_int( properties
, "aspect_ratio", 1 );
77 mlt_properties_set_int( properties
, "progressive", 1 );
79 // Validate the resource
81 load_filenames( this, properties
);
84 mlt_frame frame
= mlt_frame_init( MLT_PRODUCER_SERVICE( producer
) );
87 mlt_properties properties
= MLT_FRAME_PROPERTIES( frame
);
88 mlt_properties_set_data( properties
, "producer_pixbuf", this, 0, NULL
, NULL
);
89 mlt_frame_set_position( frame
, mlt_producer_position( producer
) );
90 mlt_properties_set_position( properties
, "pixbuf_position", mlt_producer_position( producer
) );
91 refresh_image( frame
, 0, 0 );
92 mlt_frame_close( frame
);
95 if ( this->width
== 0 )
97 producer_close( producer
);
106 static void load_filenames( producer_pixbuf
this, mlt_properties producer_properties
)
108 char *filename
= mlt_properties_get( producer_properties
, "resource" );
109 this->filenames
= mlt_properties_new( );
112 if ( strstr( filename
, "<svg" ) )
114 // Generate a temporary file for the svg
115 char fullname
[ 1024 ] = "/tmp/mlt.XXXXXX";
116 int fd
= mkstemp( fullname
);
120 // Write the svg into the temp file
121 ssize_t remaining_bytes
;
122 char *xml
= filename
;
124 // Strip leading crap
125 while ( xml
[0] != '<' )
128 remaining_bytes
= strlen( xml
);
129 while ( remaining_bytes
> 0 )
130 remaining_bytes
-= write( fd
, xml
+ strlen( xml
) - remaining_bytes
, remaining_bytes
);
133 mlt_properties_set( this->filenames
, "0", fullname
);
135 // Teehe - when the producer closes, delete the temp file and the space allo
136 mlt_properties_set_data( producer_properties
, "__temporary_file__", fullname
, 0, ( mlt_destructor
)unlink
, NULL
);
140 else if ( strchr( filename
, '%' ) != NULL
)
142 // handle picture sequences
143 int i
= mlt_properties_get_int( producer_properties
, "begin" );
152 snprintf( full
, 1023, filename
, i
++ );
153 if ( stat( full
, &buf
) == 0 )
155 sprintf( key
, "%d", keyvalue
++ );
156 mlt_properties_set( this->filenames
, key
, full
);
165 else if ( strstr( filename
, "/.all." ) != NULL
)
167 char wildcard
[ 1024 ];
168 char *dir_name
= strdup( filename
);
169 char *extension
= strrchr( dir_name
, '.' );
171 *( strstr( dir_name
, "/.all." ) + 1 ) = '\0';
172 sprintf( wildcard
, "*%s", extension
);
174 mlt_properties_dir_list( this->filenames
, dir_name
, wildcard
, 1 );
180 mlt_properties_set( this->filenames
, "0", filename
);
183 this->count
= mlt_properties_count( this->filenames
);
186 static void refresh_image( mlt_frame frame
, int width
, int height
)
189 GdkPixbuf
*pixbuf
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "pixbuf", NULL
);
190 GError
*error
= NULL
;
192 // Obtain properties of frame
193 mlt_properties properties
= MLT_FRAME_PROPERTIES( frame
);
195 // Obtain the producer for this frame
196 producer_pixbuf
this = mlt_properties_get_data( properties
, "producer_pixbuf", NULL
);
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 // Check if user wants us to reload the image
205 if ( mlt_properties_get_int( producer_props
, "force_reload" ) )
209 mlt_properties_set_int( producer_props
, "force_reload", 0 );
212 // Obtain the cache flag and structure
213 int use_cache
= mlt_properties_get_int( producer_props
, "cache" );
214 mlt_properties cache
= mlt_properties_get_data( producer_props
, "_cache", NULL
);
215 int update_cache
= 0;
217 // Get the time to live for each frame
218 double ttl
= mlt_properties_get_int( producer_props
, "ttl" );
220 // Get the original position of this frame
221 mlt_position position
= mlt_properties_get_position( properties
, "pixbuf_position" );
222 position
+= mlt_producer_get_in( producer
);
225 int image_idx
= ( int )floor( ( double )position
/ ttl
) % this->count
;
228 char image_key
[ 10 ];
229 sprintf( image_key
, "%d", image_idx
);
231 pthread_mutex_lock( &fastmutex
);
233 // Check if the frame is already loaded
238 cache
= mlt_properties_new( );
239 mlt_properties_set_data( producer_props
, "_cache", cache
, 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
242 mlt_frame cached
= mlt_properties_get_data( cache
, image_key
, NULL
);
246 this->image_idx
= image_idx
;
247 mlt_properties cached_props
= MLT_FRAME_PROPERTIES( cached
);
248 this->width
= mlt_properties_get_int( cached_props
, "width" );
249 this->height
= mlt_properties_get_int( cached_props
, "height" );
250 mlt_properties_set_int( producer_props
, "_real_width", mlt_properties_get_int( cached_props
, "real_width" ) );
251 mlt_properties_set_int( producer_props
, "_real_height", mlt_properties_get_int( cached_props
, "real_height" ) );
252 this->image
= mlt_properties_get_data( cached_props
, "image", NULL
);
253 this->alpha
= mlt_properties_get_data( cached_props
, "alpha", NULL
);
255 if ( width
!= 0 && ( width
!= this->width
|| height
!= this->height
) )
260 // optimization for subsequent iterations on single picture
261 if ( width
!= 0 && this->image
!= NULL
&& image_idx
== this->image_idx
)
263 if ( width
!= this->width
|| height
!= this->height
)
265 pixbuf
= mlt_properties_get_data( producer_props
, "_pixbuf", NULL
);
268 mlt_pool_release( this->image
);
269 mlt_pool_release( this->alpha
);
275 else if ( pixbuf
== NULL
&& ( this->image
== NULL
|| image_idx
!= this->image_idx
) )
279 mlt_pool_release( this->image
);
280 mlt_pool_release( this->alpha
);
285 this->image_idx
= image_idx
;
286 pixbuf
= gdk_pixbuf_new_from_file( mlt_properties_get_value( this->filenames
, image_idx
), &error
);
288 if ( pixbuf
!= NULL
)
290 // Register this pixbuf for destruction and reuse
291 mlt_events_block( producer_props
, NULL
);
292 mlt_properties_set_data( producer_props
, "_pixbuf", pixbuf
, 0, ( mlt_destructor
)g_object_unref
, NULL
);
293 g_object_ref( pixbuf
);
294 mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame
), "pixbuf", pixbuf
, 0, ( mlt_destructor
)g_object_unref
, NULL
);
296 mlt_properties_set_int( producer_props
, "_real_width", gdk_pixbuf_get_width( pixbuf
) );
297 mlt_properties_set_int( producer_props
, "_real_height", gdk_pixbuf_get_height( pixbuf
) );
298 mlt_events_unblock( producer_props
, NULL
);
300 // Store the width/height of the pixbuf temporarily
301 this->width
= gdk_pixbuf_get_width( pixbuf
);
302 this->height
= gdk_pixbuf_get_height( pixbuf
);
306 // If we have a pixbuf
307 if ( pixbuf
&& width
> 0 )
309 char *interps
= mlt_properties_get( properties
, "rescale.interp" );
310 int interp
= GDK_INTERP_BILINEAR
;
312 if ( strcmp( interps
, "nearest" ) == 0 )
313 interp
= GDK_INTERP_NEAREST
;
314 else if ( strcmp( interps
, "tiles" ) == 0 )
315 interp
= GDK_INTERP_TILES
;
316 else if ( strcmp( interps
, "hyper" ) == 0 )
317 interp
= GDK_INTERP_HYPER
;
319 // Note - the original pixbuf is already safe and ready for destruction
320 pixbuf
= gdk_pixbuf_scale_simple( pixbuf
, width
, height
, interp
);
322 // Store width and height
324 this->height
= height
;
326 // Allocate/define image
327 this->image
= mlt_pool_alloc( width
* ( height
+ 1 ) * 2 );
329 // Extract YUV422 and alpha
330 if ( gdk_pixbuf_get_has_alpha( pixbuf
) )
332 // Allocate the alpha mask
333 this->alpha
= mlt_pool_alloc( this->width
* this->height
);
336 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf
),
337 this->width
, this->height
,
338 gdk_pixbuf_get_rowstride( pixbuf
),
339 this->image
, this->alpha
);
343 // No alpha to extract
344 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf
),
345 this->width
, this->height
,
346 gdk_pixbuf_get_rowstride( pixbuf
),
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 // Set width/height of frame
358 mlt_properties_set_int( properties
, "width", this->width
);
359 mlt_properties_set_int( properties
, "height", this->height
);
360 mlt_properties_set_int( properties
, "real_width", mlt_properties_get_int( producer_props
, "_real_width" ) );
361 mlt_properties_set_int( properties
, "real_height", mlt_properties_get_int( producer_props
, "_real_height" ) );
363 // pass the image data without destructor
364 mlt_properties_set_data( properties
, "image", this->image
, this->width
* ( this->height
+ 1 ) * 2, NULL
, NULL
);
365 mlt_properties_set_data( properties
, "alpha", this->alpha
, this->width
* this->height
, NULL
, NULL
);
369 mlt_frame cached
= mlt_frame_init( MLT_PRODUCER_SERVICE( producer
) );
370 mlt_properties cached_props
= MLT_FRAME_PROPERTIES( cached
);
371 mlt_properties_set_int( cached_props
, "width", this->width
);
372 mlt_properties_set_int( cached_props
, "height", this->height
);
373 mlt_properties_set_int( cached_props
, "real_width", mlt_properties_get_int( producer_props
, "_real_width" ) );
374 mlt_properties_set_int( cached_props
, "real_height", mlt_properties_get_int( producer_props
, "_real_height" ) );
375 mlt_properties_set_data( cached_props
, "image", this->image
, this->width
* ( this->height
+ 1 ) * 2, mlt_pool_release
, NULL
);
376 mlt_properties_set_data( cached_props
, "alpha", this->alpha
, this->width
* this->height
, mlt_pool_release
, NULL
);
377 mlt_properties_set_data( cache
, image_key
, cached
, 0, ( mlt_destructor
)mlt_frame_close
, NULL
);
380 pthread_mutex_unlock( &fastmutex
);
383 static int producer_get_image( mlt_frame frame
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
385 // Obtain properties of frame
386 mlt_properties properties
= MLT_FRAME_PROPERTIES( frame
);
388 // We need to know the size of the image to clone it
393 uint8_t *alpha
= NULL
;
395 *width
= mlt_properties_get_int( properties
, "rescale_width" );
396 *height
= mlt_properties_get_int( properties
, "rescale_height" );
399 refresh_image( frame
, *width
, *height
);
402 *buffer
= mlt_properties_get_data( properties
, "image", &image_size
);
403 alpha
= mlt_properties_get_data( properties
, "alpha", &alpha_size
);
405 // Get width and height (may have changed during the refresh)
406 *width
= mlt_properties_get_int( properties
, "width" );
407 *height
= mlt_properties_get_int( properties
, "height" );
409 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
410 // The fault is not in the design of mlt, but in the implementation of the pixbuf producer...
411 if ( *buffer
!= NULL
)
413 if ( *format
== mlt_image_yuv422
|| *format
== mlt_image_yuv420p
)
415 // Clone the image and the alpha
416 uint8_t *image_copy
= mlt_pool_alloc( image_size
);
417 uint8_t *alpha_copy
= mlt_pool_alloc( alpha_size
);
419 memcpy( image_copy
, *buffer
, image_size
);
421 // Copy or default the alpha
423 memcpy( alpha_copy
, alpha
, alpha_size
);
425 memset( alpha_copy
, 255, alpha_size
);
427 // Now update properties so we free the copy after
428 mlt_properties_set_data( properties
, "image", image_copy
, image_size
, mlt_pool_release
, NULL
);
429 mlt_properties_set_data( properties
, "alpha", alpha_copy
, alpha_size
, mlt_pool_release
, NULL
);
431 // We're going to pass the copy on
432 *buffer
= image_copy
;
434 else if ( *format
== mlt_image_rgb24a
)
436 // Clone the image and the alpha
437 image_size
= *width
* ( *height
+ 1 ) * 4;
438 alpha_size
= *width
* ( *height
+ 1 );
439 uint8_t *image_copy
= mlt_pool_alloc( image_size
);
440 uint8_t *alpha_copy
= mlt_pool_alloc( alpha_size
);
442 mlt_convert_yuv422_to_rgb24a(*buffer
, image_copy
, (*width
)*(*height
));
444 // Now update properties so we free the copy after
445 mlt_properties_set_data( properties
, "image", image_copy
, image_size
, mlt_pool_release
, NULL
);
446 mlt_properties_set_data( properties
, "alpha", alpha_copy
, alpha_size
, mlt_pool_release
, NULL
);
448 // We're going to pass the copy on
449 *buffer
= image_copy
;
455 // TODO: Review all cases of invalid images
456 *buffer
= mlt_pool_alloc( 50 * 50 * 2 );
457 mlt_properties_set_data( properties
, "image", *buffer
, image_size
, mlt_pool_release
, NULL
);
465 static uint8_t *producer_get_alpha_mask( mlt_frame
this )
467 // Obtain properties of frame
468 mlt_properties properties
= MLT_FRAME_PROPERTIES( this );
470 // Return the alpha mask
471 return mlt_properties_get_data( properties
, "alpha", NULL
);
474 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
476 // Get the real structure for this producer
477 producer_pixbuf
this = producer
->child
;
479 // Fetch the producers properties
480 mlt_properties producer_properties
= MLT_PRODUCER_PROPERTIES( producer
);
482 if ( this->filenames
== NULL
&& mlt_properties_get( producer_properties
, "resource" ) != NULL
)
483 load_filenames( this, producer_properties
);
486 *frame
= mlt_frame_init( MLT_PRODUCER_SERVICE( producer
) );
488 if ( *frame
!= NULL
&& this->count
> 0 )
490 // Obtain properties of frame and producer
491 mlt_properties properties
= MLT_FRAME_PROPERTIES( *frame
);
493 // Set the producer on the frame properties
494 mlt_properties_set_data( properties
, "producer_pixbuf", this, 0, NULL
, NULL
);
496 // Update timecode on the frame we're creating
497 mlt_frame_set_position( *frame
, mlt_producer_position( producer
) );
499 // Ensure that we have a way to obtain the position in the get_image
500 mlt_properties_set_position( properties
, "pixbuf_position", mlt_producer_position( producer
) );
503 refresh_image( *frame
, 0, 0 );
505 // Set producer-specific frame properties
506 mlt_properties_set_int( properties
, "progressive", mlt_properties_get_int( producer_properties
, "progressive" ) );
507 mlt_properties_set_double( properties
, "aspect_ratio", mlt_properties_get_double( producer_properties
, "aspect_ratio" ) );
509 // Set alpha call back
510 ( *frame
)->get_alpha_mask
= producer_get_alpha_mask
;
512 // Push the get_image method
513 mlt_frame_push_get_image( *frame
, producer_get_image
);
516 // Calculate the next timecode
517 mlt_producer_prepare_next( producer
);
522 static void producer_close( mlt_producer parent
)
524 producer_pixbuf
this = parent
->child
;
525 if ( !mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( parent
), "cache" ) )
527 mlt_pool_release( this->image
);
528 mlt_pool_release( this->alpha
);
530 parent
->close
= NULL
;
531 mlt_producer_close( parent
);
532 mlt_properties_close( this->filenames
);