d24c92081a76198c47a4af902e64c922f2a9669b
[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 <gdk-pixbuf/gdk-pixbuf.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <pthread.h>
29 #include <math.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <dirent.h>
34
35 static pthread_mutex_t fastmutex = PTHREAD_MUTEX_INITIALIZER;
36
37 typedef struct producer_pixbuf_s *producer_pixbuf;
38
39 struct producer_pixbuf_s
40 {
41 struct mlt_producer_s parent;
42
43 // File name list
44 mlt_properties filenames;
45 int count;
46 int image_idx;
47
48 int width;
49 int height;
50 uint8_t *image;
51 uint8_t *alpha;
52 };
53
54 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
55 static void producer_close( mlt_producer parent );
56
57 mlt_producer producer_pixbuf_init( char *filename )
58 {
59 producer_pixbuf this = calloc( sizeof( struct producer_pixbuf_s ), 1 );
60 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
61 {
62 mlt_producer producer = &this->parent;
63
64 // Get the properties interface
65 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
66
67 // Callback registration
68 producer->get_frame = producer_get_frame;
69 producer->close = ( mlt_destructor )producer_close;
70
71 // Set the default properties
72 mlt_properties_set( properties, "resource", filename );
73 mlt_properties_set_int( properties, "ttl", 25 );
74 mlt_properties_set_int( properties, "aspect_ratio", 1 );
75 mlt_properties_set_int( properties, "progressive", 1 );
76
77 return producer;
78 }
79 free( this );
80 return NULL;
81 }
82
83 static void refresh_image( mlt_frame frame, int width, int height )
84 {
85 // Pixbuf
86 GdkPixbuf *pixbuf = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "pixbuf", NULL );
87 GError *error = NULL;
88
89 // Obtain properties of frame
90 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
91
92 // Obtain the producer for this frame
93 producer_pixbuf this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
94
95 // Obtain the producer
96 mlt_producer producer = &this->parent;
97
98 // Obtain properties of producer
99 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
100
101 // Check if user wants us to reload the image
102 if ( mlt_properties_get_int( producer_props, "force_reload" ) )
103 {
104 pixbuf = NULL;
105 this->image = NULL;
106 mlt_properties_set_int( producer_props, "force_reload", 0 );
107 }
108
109 // Obtain the cache flag and structure
110 int use_cache = mlt_properties_get_int( producer_props, "cache" );
111 mlt_properties cache = mlt_properties_get_data( producer_props, "_cache", NULL );
112 int update_cache = 0;
113
114 // Get the time to live for each frame
115 double ttl = mlt_properties_get_int( producer_props, "ttl" );
116
117 // Get the original position of this frame
118 mlt_position position = mlt_properties_get_position( properties, "pixbuf_position" );
119 position += mlt_producer_get_in( producer );
120
121 // Image index
122 int image_idx = ( int )floor( ( double )position / ttl ) % this->count;
123
124 // Key for the cache
125 char image_key[ 10 ];
126 sprintf( image_key, "%d", image_idx );
127
128 pthread_mutex_lock( &fastmutex );
129
130 // Check if the frame is already loaded
131 if ( use_cache )
132 {
133 if ( cache == NULL )
134 {
135 cache = mlt_properties_new( );
136 mlt_properties_set_data( producer_props, "_cache", cache, 0, ( mlt_destructor )mlt_properties_close, NULL );
137 }
138
139 mlt_frame cached = mlt_properties_get_data( cache, image_key, NULL );
140
141 if ( cached )
142 {
143 this->image_idx = image_idx;
144 mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
145 this->width = mlt_properties_get_int( cached_props, "width" );
146 this->height = mlt_properties_get_int( cached_props, "height" );
147 mlt_properties_set_int( producer_props, "_real_width", mlt_properties_get_int( cached_props, "real_width" ) );
148 mlt_properties_set_int( producer_props, "_real_height", mlt_properties_get_int( cached_props, "real_height" ) );
149 this->image = mlt_properties_get_data( cached_props, "image", NULL );
150 this->alpha = mlt_properties_get_data( cached_props, "alpha", NULL );
151
152 if ( width != 0 && ( width != this->width || height != this->height ) )
153 this->image = NULL;
154 }
155 }
156
157 // optimization for subsequent iterations on single picture
158 if ( width != 0 && this->image != NULL && image_idx == this->image_idx )
159 {
160 if ( width != this->width || height != this->height )
161 {
162 pixbuf = mlt_properties_get_data( producer_props, "_pixbuf", NULL );
163 if ( !use_cache )
164 {
165 mlt_pool_release( this->image );
166 mlt_pool_release( this->alpha );
167 }
168 this->image = NULL;
169 this->alpha = NULL;
170 }
171 }
172 else if ( pixbuf == NULL && ( this->image == NULL || image_idx != this->image_idx ) )
173 {
174 if ( !use_cache )
175 {
176 mlt_pool_release( this->image );
177 mlt_pool_release( this->alpha );
178 }
179 this->image = NULL;
180 this->alpha = NULL;
181
182 this->image_idx = image_idx;
183 pixbuf = gdk_pixbuf_new_from_file( mlt_properties_get_value( this->filenames, image_idx ), &error );
184
185 if ( pixbuf != NULL )
186 {
187 // Register this pixbuf for destruction and reuse
188 mlt_events_block( producer_props, NULL );
189 mlt_properties_set_data( producer_props, "_pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
190 g_object_ref( pixbuf );
191 mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
192
193 mlt_properties_set_int( producer_props, "_real_width", gdk_pixbuf_get_width( pixbuf ) );
194 mlt_properties_set_int( producer_props, "_real_height", gdk_pixbuf_get_height( pixbuf ) );
195 mlt_events_unblock( producer_props, NULL );
196
197 // Store the width/height of the pixbuf temporarily
198 this->width = gdk_pixbuf_get_width( pixbuf );
199 this->height = gdk_pixbuf_get_height( pixbuf );
200 }
201 }
202
203 // If we have a pixbuf
204 if ( pixbuf && width > 0 )
205 {
206 char *interps = mlt_properties_get( properties, "rescale.interp" );
207 int interp = GDK_INTERP_BILINEAR;
208
209 if ( strcmp( interps, "nearest" ) == 0 )
210 interp = GDK_INTERP_NEAREST;
211 else if ( strcmp( interps, "tiles" ) == 0 )
212 interp = GDK_INTERP_TILES;
213 else if ( strcmp( interps, "hyper" ) == 0 )
214 interp = GDK_INTERP_HYPER;
215
216 // Note - the original pixbuf is already safe and ready for destruction
217 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
218
219 // Store width and height
220 this->width = width;
221 this->height = height;
222
223 // Allocate/define image
224 this->image = mlt_pool_alloc( width * ( height + 1 ) * 2 );
225
226 // Extract YUV422 and alpha
227 if ( gdk_pixbuf_get_has_alpha( pixbuf ) )
228 {
229 // Allocate the alpha mask
230 this->alpha = mlt_pool_alloc( this->width * this->height );
231
232 // Convert the image
233 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
234 this->width, this->height,
235 gdk_pixbuf_get_rowstride( pixbuf ),
236 this->image, this->alpha );
237 }
238 else
239 {
240 // No alpha to extract
241 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
242 this->width, this->height,
243 gdk_pixbuf_get_rowstride( pixbuf ),
244 this->image );
245 }
246
247 // Finished with pixbuf now
248 g_object_unref( pixbuf );
249
250 // Ensure we update the cache when we need to
251 update_cache = use_cache;
252 }
253
254 // Set width/height of frame
255 mlt_properties_set_int( properties, "width", this->width );
256 mlt_properties_set_int( properties, "height", this->height );
257 mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
258 mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
259
260 // pass the image data without destructor
261 mlt_properties_set_data( properties, "image", this->image, this->width * ( this->height + 1 ) * 2, NULL, NULL );
262 mlt_properties_set_data( properties, "alpha", this->alpha, this->width * this->height, NULL, NULL );
263
264 if ( update_cache )
265 {
266 mlt_frame cached = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
267 mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
268 mlt_properties_set_int( cached_props, "width", this->width );
269 mlt_properties_set_int( cached_props, "height", this->height );
270 mlt_properties_set_int( cached_props, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
271 mlt_properties_set_int( cached_props, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
272 mlt_properties_set_data( cached_props, "image", this->image, this->width * ( this->height + 1 ) * 2, mlt_pool_release, NULL );
273 mlt_properties_set_data( cached_props, "alpha", this->alpha, this->width * this->height, mlt_pool_release, NULL );
274 mlt_properties_set_data( cache, image_key, cached, 0, ( mlt_destructor )mlt_frame_close, NULL );
275 }
276
277 pthread_mutex_unlock( &fastmutex );
278 }
279
280 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
281 {
282 // Obtain properties of frame
283 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
284
285 // We need to know the size of the image to clone it
286 int image_size = 0;
287 int alpha_size = 0;
288
289 // Alpha channel
290 uint8_t *alpha = NULL;
291
292 *width = mlt_properties_get_int( properties, "rescale_width" );
293 *height = mlt_properties_get_int( properties, "rescale_height" );
294
295 // Refresh the image
296 refresh_image( frame, *width, *height );
297
298 // Get the image
299 *buffer = mlt_properties_get_data( properties, "image", &image_size );
300 alpha = mlt_properties_get_data( properties, "alpha", &alpha_size );
301
302 // Get width and height (may have changed during the refresh)
303 *width = mlt_properties_get_int( properties, "width" );
304 *height = mlt_properties_get_int( properties, "height" );
305
306 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
307 // The fault is not in the design of mlt, but in the implementation of the pixbuf producer...
308 if ( *buffer != NULL )
309 {
310 if ( *format == mlt_image_yuv422 || *format == mlt_image_yuv420p )
311 {
312 // Clone the image and the alpha
313 uint8_t *image_copy = mlt_pool_alloc( image_size );
314 uint8_t *alpha_copy = mlt_pool_alloc( alpha_size );
315
316 memcpy( image_copy, *buffer, image_size );
317
318 // Copy or default the alpha
319 if ( alpha != NULL )
320 memcpy( alpha_copy, alpha, alpha_size );
321 else
322 memset( alpha_copy, 255, alpha_size );
323
324 // Now update properties so we free the copy after
325 mlt_properties_set_data( properties, "image", image_copy, image_size, mlt_pool_release, NULL );
326 mlt_properties_set_data( properties, "alpha", alpha_copy, alpha_size, mlt_pool_release, NULL );
327
328 // We're going to pass the copy on
329 *buffer = image_copy;
330 }
331 else if ( *format == mlt_image_rgb24a )
332 {
333 // Clone the image and the alpha
334 image_size = *width * ( *height + 1 ) * 4;
335 alpha_size = *width * ( *height + 1 );
336 uint8_t *image_copy = mlt_pool_alloc( image_size );
337 uint8_t *alpha_copy = mlt_pool_alloc( alpha_size );
338
339 mlt_convert_yuv422_to_rgb24a(*buffer, image_copy, (*width)*(*height));
340
341 // Now update properties so we free the copy after
342 mlt_properties_set_data( properties, "image", image_copy, image_size, mlt_pool_release, NULL );
343 mlt_properties_set_data( properties, "alpha", alpha_copy, alpha_size, mlt_pool_release, NULL );
344
345 // We're going to pass the copy on
346 *buffer = image_copy;
347 }
348
349 }
350 else
351 {
352 // TODO: Review all cases of invalid images
353 *buffer = mlt_pool_alloc( 50 * 50 * 2 );
354 mlt_properties_set_data( properties, "image", *buffer, image_size, mlt_pool_release, NULL );
355 *width = 50;
356 *height = 50;
357 }
358
359 return 0;
360 }
361
362 static uint8_t *producer_get_alpha_mask( mlt_frame this )
363 {
364 // Obtain properties of frame
365 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
366
367 // Return the alpha mask
368 return mlt_properties_get_data( properties, "alpha", NULL );
369 }
370
371 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
372 {
373 // Get the real structure for this producer
374 producer_pixbuf this = producer->child;
375
376 // Fetch the producers properties
377 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
378
379 if ( this->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
380 {
381 char *filename = mlt_properties_get( producer_properties, "resource" );
382 this->filenames = mlt_properties_new( );
383
384 // Read xml string
385 if ( strstr( filename, "<svg" ) )
386 {
387 // Generate a temporary file for the svg
388 char fullname[ 1024 ] = "/tmp/mlt.XXXXXX";
389 int fd = mkstemp( fullname );
390
391 if ( fd > -1 )
392 {
393 // Write the svg into the temp file
394 ssize_t remaining_bytes;
395 char *xml = filename;
396
397 // Strip leading crap
398 while ( xml[0] != '<' )
399 xml++;
400
401 remaining_bytes = strlen( xml );
402 while ( remaining_bytes > 0 )
403 remaining_bytes -= write( fd, xml + strlen( xml ) - remaining_bytes, remaining_bytes );
404 close( fd );
405
406 mlt_properties_set( this->filenames, "0", fullname );
407
408 // Teehe - when the producer closes, delete the temp file and the space allo
409 mlt_properties_set_data( producer_properties, "__temporary_file__", fullname, 0, ( mlt_destructor )unlink, NULL );
410 }
411 }
412 // Obtain filenames
413 else if ( strchr( filename, '%' ) != NULL )
414 {
415 // handle picture sequences
416 int i = mlt_properties_get_int( producer_properties, "begin" );
417 int gap = 0;
418 char full[1024];
419 int keyvalue = 0;
420 char key[ 50 ];
421
422 while ( gap < 100 )
423 {
424 struct stat buf;
425 snprintf( full, 1023, filename, i ++ );
426 if ( stat( full, &buf ) == 0 )
427 {
428 sprintf( key, "%d", keyvalue ++ );
429 mlt_properties_set( this->filenames, key, full );
430 gap = 0;
431 }
432 else
433 {
434 gap ++;
435 }
436 }
437 }
438 else if ( strstr( filename, "/.all." ) != NULL )
439 {
440 char wildcard[ 1024 ];
441 char *dir_name = strdup( filename );
442 char *extension = strrchr( dir_name, '.' );
443
444 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
445 sprintf( wildcard, "*%s", extension );
446
447 mlt_properties_dir_list( this->filenames, dir_name, wildcard, 1 );
448
449 free( dir_name );
450 }
451 else
452 {
453 mlt_properties_set( this->filenames, "0", filename );
454 }
455
456 this->count = mlt_properties_count( this->filenames );
457 }
458
459 // Generate a frame
460 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
461
462 if ( *frame != NULL && this->count > 0 )
463 {
464 // Obtain properties of frame and producer
465 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
466
467 // Set the producer on the frame properties
468 mlt_properties_set_data( properties, "producer_pixbuf", this, 0, NULL, NULL );
469
470 // Update timecode on the frame we're creating
471 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
472
473 // Ensure that we have a way to obtain the position in the get_image
474 mlt_properties_set_position( properties, "pixbuf_position", mlt_producer_position( producer ) );
475
476 // Refresh the image
477 refresh_image( *frame, 0, 0 );
478
479 // Set producer-specific frame properties
480 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( producer_properties, "progressive" ) );
481 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
482
483 // Set alpha call back
484 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
485
486 // Push the get_image method
487 mlt_frame_push_get_image( *frame, producer_get_image );
488 }
489
490 // Calculate the next timecode
491 mlt_producer_prepare_next( producer );
492
493 return 0;
494 }
495
496 static void producer_close( mlt_producer parent )
497 {
498 producer_pixbuf this = parent->child;
499 if ( !mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( parent ), "cache" ) )
500 {
501 mlt_pool_release( this->image );
502 mlt_pool_release( this->alpha );
503 }
504 parent->close = NULL;
505 mlt_producer_close( parent );
506 mlt_properties_close( this->filenames );
507 free( this );
508 }