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 frame_properties
= MLT_FRAME_PROPERTIES( frame
);
88 mlt_properties_set_data( frame_properties
, "producer_pixbuf", this, 0, NULL
, NULL
);
89 mlt_frame_set_position( frame
, mlt_producer_position( producer
) );
90 mlt_properties_set_position( frame_properties
, "pixbuf_position", mlt_producer_position( producer
) );
91 refresh_image( frame
, 0, 0 );
92 mlt_frame_close( frame
);
93 mlt_properties_set_data( properties
, "_pixbuf", NULL
, 0, NULL
, NULL
);
96 if ( this->width
== 0 )
98 producer_close( producer
);
107 static void load_filenames( producer_pixbuf
this, mlt_properties producer_properties
)
109 char *filename
= mlt_properties_get( producer_properties
, "resource" );
110 this->filenames
= mlt_properties_new( );
113 if ( strstr( filename
, "<svg" ) )
115 // Generate a temporary file for the svg
116 char fullname
[ 1024 ] = "/tmp/mlt.XXXXXX";
117 int fd
= mkstemp( fullname
);
121 // Write the svg into the temp file
122 ssize_t remaining_bytes
;
123 char *xml
= filename
;
125 // Strip leading crap
126 while ( xml
[0] != '<' )
129 remaining_bytes
= strlen( xml
);
130 while ( remaining_bytes
> 0 )
131 remaining_bytes
-= write( fd
, xml
+ strlen( xml
) - remaining_bytes
, remaining_bytes
);
134 mlt_properties_set( this->filenames
, "0", fullname
);
136 // Teehe - when the producer closes, delete the temp file and the space allo
137 mlt_properties_set_data( producer_properties
, "__temporary_file__", fullname
, 0, ( mlt_destructor
)unlink
, NULL
);
141 else if ( strchr( filename
, '%' ) != NULL
)
143 // handle picture sequences
144 int i
= mlt_properties_get_int( producer_properties
, "begin" );
153 snprintf( full
, 1023, filename
, i
++ );
154 if ( stat( full
, &buf
) == 0 )
156 sprintf( key
, "%d", keyvalue
++ );
157 mlt_properties_set( this->filenames
, key
, full
);
166 else if ( strstr( filename
, "/.all." ) != NULL
)
168 char wildcard
[ 1024 ];
169 char *dir_name
= strdup( filename
);
170 char *extension
= strrchr( dir_name
, '.' );
172 *( strstr( dir_name
, "/.all." ) + 1 ) = '\0';
173 sprintf( wildcard
, "*%s", extension
);
175 mlt_properties_dir_list( this->filenames
, dir_name
, wildcard
, 1 );
181 mlt_properties_set( this->filenames
, "0", filename
);
184 this->count
= mlt_properties_count( this->filenames
);
187 static void refresh_image( mlt_frame frame
, int width
, int height
)
190 GdkPixbuf
*pixbuf
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "pixbuf", NULL
);
191 GError
*error
= NULL
;
193 // Obtain properties of frame
194 mlt_properties properties
= MLT_FRAME_PROPERTIES( frame
);
196 // Obtain the producer for this frame
197 producer_pixbuf
this = mlt_properties_get_data( properties
, "producer_pixbuf", NULL
);
199 // Obtain the producer
200 mlt_producer producer
= &this->parent
;
202 // Obtain properties of producer
203 mlt_properties producer_props
= MLT_PRODUCER_PROPERTIES( producer
);
205 // Obtain the cache flag and structure
206 int use_cache
= mlt_properties_get_int( producer_props
, "cache" );
207 mlt_properties cache
= mlt_properties_get_data( producer_props
, "_cache", NULL
);
208 int update_cache
= 0;
210 // Check if user wants us to reload the image
211 if ( mlt_properties_get_int( producer_props
, "force_reload" ) )
214 if ( !use_cache
&& this->image
)
215 mlt_pool_release( this->image
);
217 mlt_properties_set_int( producer_props
, "force_reload", 0 );
220 // Get the time to live for each frame
221 double ttl
= mlt_properties_get_int( producer_props
, "ttl" );
223 // Get the original position of this frame
224 mlt_position position
= mlt_properties_get_position( properties
, "pixbuf_position" );
225 position
+= mlt_producer_get_in( producer
);
228 int image_idx
= ( int )floor( ( double )position
/ ttl
) % this->count
;
231 char image_key
[ 10 ];
232 sprintf( image_key
, "%d", image_idx
);
234 pthread_mutex_lock( &fastmutex
);
236 // Check if the frame is already loaded
241 cache
= mlt_properties_new( );
242 mlt_properties_set_data( producer_props
, "_cache", cache
, 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
245 mlt_frame cached
= mlt_properties_get_data( cache
, image_key
, NULL
);
249 this->image_idx
= image_idx
;
250 mlt_properties cached_props
= MLT_FRAME_PROPERTIES( cached
);
251 this->width
= mlt_properties_get_int( cached_props
, "width" );
252 this->height
= mlt_properties_get_int( cached_props
, "height" );
253 mlt_properties_set_int( producer_props
, "_real_width", mlt_properties_get_int( cached_props
, "real_width" ) );
254 mlt_properties_set_int( producer_props
, "_real_height", mlt_properties_get_int( cached_props
, "real_height" ) );
255 this->image
= mlt_properties_get_data( cached_props
, "image", NULL
);
256 this->alpha
= mlt_properties_get_data( cached_props
, "alpha", NULL
);
258 if ( width
!= 0 && ( width
!= this->width
|| height
!= this->height
) )
263 // optimization for subsequent iterations on single picture
264 if ( width
!= 0 && this->image
!= NULL
&& image_idx
== this->image_idx
)
266 if ( width
!= this->width
|| height
!= this->height
)
268 pixbuf
= mlt_properties_get_data( producer_props
, "_pixbuf", NULL
);
271 else if ( pixbuf
== NULL
&& ( this->image
== NULL
|| image_idx
!= this->image_idx
) )
273 this->image_idx
= image_idx
;
274 pixbuf
= gdk_pixbuf_new_from_file( mlt_properties_get_value( this->filenames
, image_idx
), &error
);
276 if ( pixbuf
!= NULL
)
278 // Register this pixbuf for destruction and reuse
279 mlt_events_block( producer_props
, NULL
);
280 mlt_properties_set_data( producer_props
, "_pixbuf", pixbuf
, 0, ( mlt_destructor
)g_object_unref
, NULL
);
281 g_object_ref( pixbuf
);
282 mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame
), "pixbuf", pixbuf
, 0, ( mlt_destructor
)g_object_unref
, NULL
);
284 mlt_properties_set_int( producer_props
, "_real_width", gdk_pixbuf_get_width( pixbuf
) );
285 mlt_properties_set_int( producer_props
, "_real_height", gdk_pixbuf_get_height( pixbuf
) );
286 mlt_events_unblock( producer_props
, NULL
);
288 // Store the width/height of the pixbuf temporarily
289 this->width
= gdk_pixbuf_get_width( pixbuf
);
290 this->height
= gdk_pixbuf_get_height( pixbuf
);
294 // If we have a pixbuf
295 if ( pixbuf
&& width
> 0 )
297 char *interps
= mlt_properties_get( properties
, "rescale.interp" );
298 int interp
= GDK_INTERP_BILINEAR
;
300 if ( strcmp( interps
, "nearest" ) == 0 )
301 interp
= GDK_INTERP_NEAREST
;
302 else if ( strcmp( interps
, "tiles" ) == 0 )
303 interp
= GDK_INTERP_TILES
;
304 else if ( strcmp( interps
, "hyper" ) == 0 )
305 interp
= GDK_INTERP_HYPER
;
307 // Note - the original pixbuf is already safe and ready for destruction
308 pixbuf
= gdk_pixbuf_scale_simple( pixbuf
, width
, height
, interp
);
310 // Store width and height
312 this->height
= height
;
314 // Allocate/define image
315 if ( !use_cache
&& this->image
)
316 mlt_pool_release( this->image
);
317 this->image
= mlt_pool_alloc( width
* ( height
+ 1 ) * 2 );
319 // Extract YUV422 and alpha
320 if ( gdk_pixbuf_get_has_alpha( pixbuf
) )
322 // Allocate the alpha mask
323 if ( !use_cache
&& this->alpha
)
324 mlt_pool_release( this->alpha
);
325 this->alpha
= mlt_pool_alloc( this->width
* this->height
);
328 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf
),
329 this->width
, this->height
,
330 gdk_pixbuf_get_rowstride( pixbuf
),
331 this->image
, this->alpha
);
335 // No alpha to extract
336 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf
),
337 this->width
, this->height
,
338 gdk_pixbuf_get_rowstride( pixbuf
),
342 // Finished with pixbuf now
343 g_object_unref( pixbuf
);
345 // Ensure we update the cache when we need to
346 update_cache
= use_cache
;
349 // Set width/height of frame
350 mlt_properties_set_int( properties
, "width", this->width
);
351 mlt_properties_set_int( properties
, "height", this->height
);
352 mlt_properties_set_int( properties
, "real_width", mlt_properties_get_int( producer_props
, "_real_width" ) );
353 mlt_properties_set_int( properties
, "real_height", mlt_properties_get_int( producer_props
, "_real_height" ) );
355 // pass the image data without destructor
356 mlt_properties_set_data( properties
, "image", this->image
, this->width
* ( this->height
+ 1 ) * 2, NULL
, NULL
);
357 mlt_properties_set_data( properties
, "alpha", this->alpha
, this->width
* this->height
, NULL
, NULL
);
361 mlt_frame cached
= mlt_frame_init( MLT_PRODUCER_SERVICE( producer
) );
362 mlt_properties cached_props
= MLT_FRAME_PROPERTIES( cached
);
363 mlt_properties_set_int( cached_props
, "width", this->width
);
364 mlt_properties_set_int( cached_props
, "height", this->height
);
365 mlt_properties_set_int( cached_props
, "real_width", mlt_properties_get_int( producer_props
, "_real_width" ) );
366 mlt_properties_set_int( cached_props
, "real_height", mlt_properties_get_int( producer_props
, "_real_height" ) );
367 mlt_properties_set_data( cached_props
, "image", this->image
, this->width
* ( this->height
+ 1 ) * 2, mlt_pool_release
, NULL
);
368 mlt_properties_set_data( cached_props
, "alpha", this->alpha
, this->width
* this->height
, mlt_pool_release
, NULL
);
369 mlt_properties_set_data( cache
, image_key
, cached
, 0, ( mlt_destructor
)mlt_frame_close
, NULL
);
372 pthread_mutex_unlock( &fastmutex
);
375 static int producer_get_image( mlt_frame frame
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
377 // Obtain properties of frame
378 mlt_properties properties
= MLT_FRAME_PROPERTIES( frame
);
380 // We need to know the size of the image to clone it
385 uint8_t *alpha
= NULL
;
387 *width
= mlt_properties_get_int( properties
, "rescale_width" );
388 *height
= mlt_properties_get_int( properties
, "rescale_height" );
391 refresh_image( frame
, *width
, *height
);
394 *buffer
= mlt_properties_get_data( properties
, "image", &image_size
);
395 alpha
= mlt_properties_get_data( properties
, "alpha", &alpha_size
);
397 // Get width and height (may have changed during the refresh)
398 *width
= mlt_properties_get_int( properties
, "width" );
399 *height
= mlt_properties_get_int( properties
, "height" );
401 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
402 // The fault is not in the design of mlt, but in the implementation of the pixbuf producer...
403 if ( *buffer
!= NULL
)
405 if ( *format
== mlt_image_yuv422
|| *format
== mlt_image_yuv420p
)
407 // Clone the image and the alpha
408 uint8_t *image_copy
= mlt_pool_alloc( image_size
);
409 uint8_t *alpha_copy
= mlt_pool_alloc( alpha_size
);
411 memcpy( image_copy
, *buffer
, image_size
);
413 // Copy or default the alpha
415 memcpy( alpha_copy
, alpha
, alpha_size
);
417 memset( alpha_copy
, 255, alpha_size
);
419 // Now update properties so we free the copy after
420 mlt_properties_set_data( properties
, "image", image_copy
, image_size
, mlt_pool_release
, NULL
);
421 mlt_properties_set_data( properties
, "alpha", alpha_copy
, alpha_size
, mlt_pool_release
, NULL
);
423 // We're going to pass the copy on
424 *buffer
= image_copy
;
426 else if ( *format
== mlt_image_rgb24a
)
428 // Clone the image and the alpha
429 image_size
= *width
* ( *height
+ 1 ) * 4;
430 alpha_size
= *width
* ( *height
+ 1 );
431 uint8_t *image_copy
= mlt_pool_alloc( image_size
);
432 uint8_t *alpha_copy
= mlt_pool_alloc( alpha_size
);
434 mlt_convert_yuv422_to_rgb24a(*buffer
, image_copy
, (*width
)*(*height
));
436 // Now update properties so we free the copy after
437 mlt_properties_set_data( properties
, "image", image_copy
, image_size
, mlt_pool_release
, NULL
);
438 mlt_properties_set_data( properties
, "alpha", alpha_copy
, alpha_size
, mlt_pool_release
, NULL
);
440 // We're going to pass the copy on
441 *buffer
= image_copy
;
447 // TODO: Review all cases of invalid images
448 *buffer
= mlt_pool_alloc( 50 * 50 * 2 );
449 mlt_properties_set_data( properties
, "image", *buffer
, image_size
, mlt_pool_release
, NULL
);
457 static uint8_t *producer_get_alpha_mask( mlt_frame
this )
459 // Obtain properties of frame
460 mlt_properties properties
= MLT_FRAME_PROPERTIES( this );
462 // Return the alpha mask
463 return mlt_properties_get_data( properties
, "alpha", NULL
);
466 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
468 // Get the real structure for this producer
469 producer_pixbuf
this = producer
->child
;
471 // Fetch the producers properties
472 mlt_properties producer_properties
= MLT_PRODUCER_PROPERTIES( producer
);
474 if ( this->filenames
== NULL
&& mlt_properties_get( producer_properties
, "resource" ) != NULL
)
475 load_filenames( this, producer_properties
);
478 *frame
= mlt_frame_init( MLT_PRODUCER_SERVICE( producer
) );
480 if ( *frame
!= NULL
&& this->count
> 0 )
482 // Obtain properties of frame and producer
483 mlt_properties properties
= MLT_FRAME_PROPERTIES( *frame
);
485 // Set the producer on the frame properties
486 mlt_properties_set_data( properties
, "producer_pixbuf", this, 0, NULL
, NULL
);
488 // Update timecode on the frame we're creating
489 mlt_frame_set_position( *frame
, mlt_producer_position( producer
) );
491 // Ensure that we have a way to obtain the position in the get_image
492 mlt_properties_set_position( properties
, "pixbuf_position", mlt_producer_position( producer
) );
495 refresh_image( *frame
, 0, 0 );
497 // Set producer-specific frame properties
498 mlt_properties_set_int( properties
, "progressive", mlt_properties_get_int( producer_properties
, "progressive" ) );
499 mlt_properties_set_double( properties
, "aspect_ratio", mlt_properties_get_double( producer_properties
, "aspect_ratio" ) );
501 // Set alpha call back
502 ( *frame
)->get_alpha_mask
= producer_get_alpha_mask
;
504 // Push the get_image method
505 mlt_frame_push_get_image( *frame
, producer_get_image
);
508 // Calculate the next timecode
509 mlt_producer_prepare_next( producer
);
514 static void producer_close( mlt_producer parent
)
516 producer_pixbuf
this = parent
->child
;
517 if ( !mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( parent
), "cache" ) )
519 mlt_pool_release( this->image
);
520 mlt_pool_release( this->alpha
);
522 parent
->close
= NULL
;
523 mlt_producer_close( parent
);
524 mlt_properties_close( this->filenames
);