Merge ../mlt
[melted] / src / modules / gtk2 / producer_pixbuf.c
1 /*
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>
5 *
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.
10 *
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.
15 *
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
19 */
20
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>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <pthread.h>
30 #include <math.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34 #include <dirent.h>
35
36 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
37
38 typedef struct producer_pixbuf_s *producer_pixbuf;
39
40 struct producer_pixbuf_s
41 {
42 struct mlt_producer_s parent;
43
44 // File name list
45 mlt_properties filenames;
46 int count;
47 int image_idx;
48
49 int width;
50 int height;
51 uint8_t *image;
52 uint8_t *alpha;
53 mlt_cache_item image_cache;
54 mlt_cache_item alpha_cache;
55 pthread_mutex_t mutex;
56 };
57
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 );
62
63 mlt_producer producer_pixbuf_init( char *filename )
64 {
65 producer_pixbuf this = calloc( sizeof( struct producer_pixbuf_s ), 1 );
66 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
67 {
68 mlt_producer producer = &this->parent;
69
70 // Get the properties interface
71 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
72
73 // Callback registration
74 producer->get_frame = producer_get_frame;
75 producer->close = ( mlt_destructor )producer_close;
76
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 );
82
83 // Validate the resource
84 if ( filename )
85 load_filenames( this, properties );
86 if ( this->count )
87 {
88 mlt_frame frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
89 if ( frame )
90 {
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 );
98 }
99 }
100 if ( this->width == 0 )
101 {
102 producer_close( producer );
103 producer = NULL;
104 }
105 return producer;
106 }
107 free( this );
108 return NULL;
109 }
110
111 static void load_filenames( producer_pixbuf this, mlt_properties producer_properties )
112 {
113 char *filename = mlt_properties_get( producer_properties, "resource" );
114 this->filenames = mlt_properties_new( );
115
116 // Read xml string
117 if ( strstr( filename, "<svg" ) )
118 {
119 // Generate a temporary file for the svg
120 char fullname[ 1024 ] = "/tmp/mlt.XXXXXX";
121 int fd = mkstemp( fullname );
122
123 if ( fd > -1 )
124 {
125 // Write the svg into the temp file
126 ssize_t remaining_bytes;
127 char *xml = filename;
128
129 // Strip leading crap
130 while ( xml[0] != '<' )
131 xml++;
132
133 remaining_bytes = strlen( xml );
134 while ( remaining_bytes > 0 )
135 remaining_bytes -= write( fd, xml + strlen( xml ) - remaining_bytes, remaining_bytes );
136 close( fd );
137
138 mlt_properties_set( this->filenames, "0", fullname );
139
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 );
142 }
143 }
144 // Obtain filenames
145 else if ( strchr( filename, '%' ) != NULL )
146 {
147 // handle picture sequences
148 int i = mlt_properties_get_int( producer_properties, "begin" );
149 int gap = 0;
150 char full[1024];
151 int keyvalue = 0;
152 char key[ 50 ];
153
154 while ( gap < 100 )
155 {
156 struct stat buf;
157 snprintf( full, 1023, filename, i ++ );
158 if ( stat( full, &buf ) == 0 )
159 {
160 sprintf( key, "%d", keyvalue ++ );
161 mlt_properties_set( this->filenames, key, full );
162 gap = 0;
163 }
164 else
165 {
166 gap ++;
167 }
168 }
169 }
170 else if ( strstr( filename, "/.all." ) != NULL )
171 {
172 char wildcard[ 1024 ];
173 char *dir_name = strdup( filename );
174 char *extension = strrchr( dir_name, '.' );
175
176 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
177 sprintf( wildcard, "*%s", extension );
178
179 mlt_properties_dir_list( this->filenames, dir_name, wildcard, 1 );
180
181 free( dir_name );
182 }
183 else
184 {
185 mlt_properties_set( this->filenames, "0", filename );
186 }
187
188 this->count = mlt_properties_count( this->filenames );
189 }
190
191 static void refresh_image( producer_pixbuf this, mlt_frame frame, int width, int height )
192 {
193 // Obtain properties of frame
194 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
195
196 // Obtain the producer
197 mlt_producer producer = &this->parent;
198
199 // Obtain properties of producer
200 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
201
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;
206
207 // restore GdkPixbuf
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;
212
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 );
216
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 );
220
221 // Check if user wants us to reload the image
222 if ( mlt_properties_get_int( producer_props, "force_reload" ) )
223 {
224 pixbuf = NULL;
225 this->image = NULL;
226 mlt_properties_set_int( producer_props, "force_reload", 0 );
227 }
228
229 // Get the time to live for each frame
230 double ttl = mlt_properties_get_int( producer_props, "ttl" );
231
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 );
235
236 // Image index
237 int image_idx = ( int )floor( ( double )position / ttl ) % this->count;
238
239 // Key for the cache
240 char image_key[ 10 ];
241 sprintf( image_key, "%d", image_idx );
242
243 pthread_mutex_lock( &g_mutex );
244
245 // Check if the frame is already loaded
246 if ( use_cache )
247 {
248 if ( cache == NULL )
249 {
250 cache = mlt_properties_new( );
251 mlt_properties_set_data( producer_props, "_cache", cache, 0, ( mlt_destructor )mlt_properties_close, NULL );
252 }
253
254 mlt_frame cached = mlt_properties_get_data( cache, image_key, NULL );
255
256 if ( cached )
257 {
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 );
266
267 if ( width != 0 && ( width != this->width || height != this->height ) )
268 this->image = NULL;
269 }
270 }
271
272 // optimization for subsequent iterations on single picture
273 if ( width != 0 && ( image_idx != this->image_idx || width != this->width || height != this->height ) )
274 this->image = NULL;
275 if ( image_idx != this->image_idx )
276 pixbuf = NULL;
277 if ( pixbuf == NULL && ( width == 0 || this->image == NULL ) )
278 {
279 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 );
282
283 if ( pixbuf )
284 {
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" );
289
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 );
294
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 );
298 }
299 }
300
301 // If we have a pixbuf and we need an image
302 if ( pixbuf && width > 0 && this->image == NULL )
303 {
304 char *interps = mlt_properties_get( properties, "rescale.interp" );
305 int interp = GDK_INTERP_BILINEAR;
306
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;
313
314 // Note - the original pixbuf is already safe and ready for destruction
315 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
316
317 // Store width and height
318 this->width = width;
319 this->height = height;
320
321 // Allocate/define image
322 this->image = mlt_pool_alloc( width * ( height + 1 ) * 2 );
323 if ( !use_cache )
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" );
327
328 // Extract YUV422 and alpha
329 if ( gdk_pixbuf_get_has_alpha( pixbuf ) )
330 {
331 // Allocate the alpha mask
332 this->alpha = mlt_pool_alloc( this->width * this->height );
333 if ( !use_cache )
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" );
337
338 // Convert the image
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 );
343 }
344 else
345 {
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 ),
350 this->image );
351 }
352
353 // Finished with pixbuf now
354 g_object_unref( pixbuf );
355
356 // Ensure we update the cache when we need to
357 update_cache = use_cache;
358 }
359
360 // release references no longer needed
361 mlt_cache_item_close( pixbuf_cache );
362 if ( width == 0 )
363 {
364 pthread_mutex_unlock( &this->mutex );
365 mlt_cache_item_close( this->image_cache );
366 mlt_cache_item_close( this->alpha_cache );
367 }
368
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" ) );
374
375 if ( update_cache )
376 {
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 );
386 }
387
388 pthread_mutex_unlock( &g_mutex );
389 }
390
391 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
392 {
393 // Obtain properties of frame
394 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
395
396 // Obtain the producer for this frame
397 producer_pixbuf this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
398
399 *width = mlt_properties_get_int( properties, "rescale_width" );
400 *height = mlt_properties_get_int( properties, "rescale_height" );
401
402 // Refresh the image
403 refresh_image( this, frame, *width, *height );
404
405 // Get the image size
406 int image_size = this->width * ( this->height + 1 ) * 2;
407 int alpha_size = this->width * this->height;
408
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" );
412
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...
415 if ( this->image )
416 {
417 if ( *format == mlt_image_yuv422 || *format == mlt_image_yuv420p )
418 {
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 );
422
423 memcpy( image_copy, this->image, image_size );
424
425 // Copy or default the alpha
426 if ( this->alpha != NULL )
427 memcpy( alpha_copy, this->alpha, alpha_size );
428 else
429 memset( alpha_copy, 255, alpha_size );
430
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 );
434
435 // We're going to pass the copy on
436 *buffer = image_copy;
437 }
438 else if ( *format == mlt_image_rgb24a )
439 {
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 );
445
446 mlt_convert_yuv422_to_rgb24a( this->image, image_copy, (*width)*(*height) );
447
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 );
451
452 // We're going to pass the copy on
453 *buffer = image_copy;
454 }
455
456 }
457 else
458 {
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 );
462 *width = 50;
463 *height = 50;
464 }
465
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 );
470
471 return 0;
472 }
473
474 static uint8_t *producer_get_alpha_mask( mlt_frame this )
475 {
476 // Obtain properties of frame
477 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
478
479 // Return the alpha mask
480 return mlt_properties_get_data( properties, "alpha", NULL );
481 }
482
483 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
484 {
485 // Get the real structure for this producer
486 producer_pixbuf this = producer->child;
487
488 // Fetch the producers properties
489 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
490
491 if ( this->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
492 load_filenames( this, producer_properties );
493
494 // Generate a frame
495 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
496
497 if ( *frame != NULL && this->count > 0 )
498 {
499 // Obtain properties of frame and producer
500 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
501
502 // Set the producer on the frame properties
503 mlt_properties_set_data( properties, "producer_pixbuf", this, 0, NULL, NULL );
504
505 // Update timecode on the frame we're creating
506 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
507
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 ) );
510
511 // Refresh the image
512 refresh_image( this, *frame, 0, 0 );
513
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" ) );
517
518 // Set alpha call back
519 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
520
521 // Push the get_image method
522 mlt_frame_push_get_image( *frame, producer_get_image );
523 }
524
525 // Calculate the next timecode
526 mlt_producer_prepare_next( producer );
527
528 return 0;
529 }
530
531 static void producer_close( mlt_producer parent )
532 {
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 );
538 free( this );
539 }