45e877046542cab85136ec4706fba39ae6ec6149
[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 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 );
58
59 mlt_producer producer_pixbuf_init( char *filename )
60 {
61 producer_pixbuf this = calloc( sizeof( struct producer_pixbuf_s ), 1 );
62 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
63 {
64 mlt_producer producer = &this->parent;
65
66 // Get the properties interface
67 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
68
69 // Callback registration
70 producer->get_frame = producer_get_frame;
71 producer->close = ( mlt_destructor )producer_close;
72
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 );
78
79 // Validate the resource
80 if ( filename )
81 load_filenames( this, properties );
82 if ( this->count )
83 {
84 mlt_frame frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
85 if ( frame )
86 {
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 );
94 }
95 }
96 if ( this->width == 0 )
97 {
98 producer_close( producer );
99 producer = NULL;
100 }
101 return producer;
102 }
103 free( this );
104 return NULL;
105 }
106
107 static void load_filenames( producer_pixbuf this, mlt_properties producer_properties )
108 {
109 char *filename = mlt_properties_get( producer_properties, "resource" );
110 this->filenames = mlt_properties_new( );
111
112 // Read xml string
113 if ( strstr( filename, "<svg" ) )
114 {
115 // Generate a temporary file for the svg
116 char fullname[ 1024 ] = "/tmp/mlt.XXXXXX";
117 int fd = mkstemp( fullname );
118
119 if ( fd > -1 )
120 {
121 // Write the svg into the temp file
122 ssize_t remaining_bytes;
123 char *xml = filename;
124
125 // Strip leading crap
126 while ( xml[0] != '<' )
127 xml++;
128
129 remaining_bytes = strlen( xml );
130 while ( remaining_bytes > 0 )
131 remaining_bytes -= write( fd, xml + strlen( xml ) - remaining_bytes, remaining_bytes );
132 close( fd );
133
134 mlt_properties_set( this->filenames, "0", fullname );
135
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 );
138 }
139 }
140 // Obtain filenames
141 else if ( strchr( filename, '%' ) != NULL )
142 {
143 // handle picture sequences
144 int i = mlt_properties_get_int( producer_properties, "begin" );
145 int gap = 0;
146 char full[1024];
147 int keyvalue = 0;
148 char key[ 50 ];
149
150 while ( gap < 100 )
151 {
152 struct stat buf;
153 snprintf( full, 1023, filename, i ++ );
154 if ( stat( full, &buf ) == 0 )
155 {
156 sprintf( key, "%d", keyvalue ++ );
157 mlt_properties_set( this->filenames, key, full );
158 gap = 0;
159 }
160 else
161 {
162 gap ++;
163 }
164 }
165 }
166 else if ( strstr( filename, "/.all." ) != NULL )
167 {
168 char wildcard[ 1024 ];
169 char *dir_name = strdup( filename );
170 char *extension = strrchr( dir_name, '.' );
171
172 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
173 sprintf( wildcard, "*%s", extension );
174
175 mlt_properties_dir_list( this->filenames, dir_name, wildcard, 1 );
176
177 free( dir_name );
178 }
179 else
180 {
181 mlt_properties_set( this->filenames, "0", filename );
182 }
183
184 this->count = mlt_properties_count( this->filenames );
185 }
186
187 static void refresh_image( mlt_frame frame, int width, int height )
188 {
189 // Pixbuf
190 GdkPixbuf *pixbuf = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "pixbuf", NULL );
191 GError *error = NULL;
192
193 // Obtain properties of frame
194 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
195
196 // Obtain the producer for this frame
197 producer_pixbuf this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
198
199 // Obtain the producer
200 mlt_producer producer = &this->parent;
201
202 // Obtain properties of producer
203 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
204
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;
209
210 // Check if user wants us to reload the image
211 if ( mlt_properties_get_int( producer_props, "force_reload" ) )
212 {
213 pixbuf = NULL;
214 if ( !use_cache && this->image )
215 mlt_pool_release( this->image );
216 this->image = NULL;
217 mlt_properties_set_int( producer_props, "force_reload", 0 );
218 }
219
220 // Get the time to live for each frame
221 double ttl = mlt_properties_get_int( producer_props, "ttl" );
222
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 );
226
227 // Image index
228 int image_idx = ( int )floor( ( double )position / ttl ) % this->count;
229
230 // Key for the cache
231 char image_key[ 10 ];
232 sprintf( image_key, "%d", image_idx );
233
234 pthread_mutex_lock( &fastmutex );
235
236 // Check if the frame is already loaded
237 if ( use_cache )
238 {
239 if ( cache == NULL )
240 {
241 cache = mlt_properties_new( );
242 mlt_properties_set_data( producer_props, "_cache", cache, 0, ( mlt_destructor )mlt_properties_close, NULL );
243 }
244
245 mlt_frame cached = mlt_properties_get_data( cache, image_key, NULL );
246
247 if ( cached )
248 {
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 );
257
258 if ( width != 0 && ( width != this->width || height != this->height ) )
259 this->image = NULL;
260 }
261 }
262
263 // optimization for subsequent iterations on single picture
264 if ( width != 0 && this->image != NULL && image_idx == this->image_idx )
265 {
266 if ( width != this->width || height != this->height )
267 {
268 pixbuf = mlt_properties_get_data( producer_props, "_pixbuf", NULL );
269 }
270 }
271 else if ( pixbuf == NULL && ( this->image == NULL || image_idx != this->image_idx ) )
272 {
273 this->image_idx = image_idx;
274 pixbuf = gdk_pixbuf_new_from_file( mlt_properties_get_value( this->filenames, image_idx ), &error );
275
276 if ( pixbuf != NULL )
277 {
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 );
283
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 );
287
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 );
291 }
292 }
293
294 // If we have a pixbuf
295 if ( pixbuf && width > 0 )
296 {
297 char *interps = mlt_properties_get( properties, "rescale.interp" );
298 int interp = GDK_INTERP_BILINEAR;
299
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;
306
307 // Note - the original pixbuf is already safe and ready for destruction
308 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
309
310 // Store width and height
311 this->width = width;
312 this->height = height;
313
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 );
318
319 // Extract YUV422 and alpha
320 if ( gdk_pixbuf_get_has_alpha( pixbuf ) )
321 {
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 );
326
327 // Convert the image
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 );
332 }
333 else
334 {
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 ),
339 this->image );
340 }
341
342 // Finished with pixbuf now
343 g_object_unref( pixbuf );
344
345 // Ensure we update the cache when we need to
346 update_cache = use_cache;
347 }
348
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" ) );
354
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 );
358
359 if ( update_cache )
360 {
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 );
370 }
371
372 pthread_mutex_unlock( &fastmutex );
373 }
374
375 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
376 {
377 // Obtain properties of frame
378 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
379
380 // We need to know the size of the image to clone it
381 int image_size = 0;
382 int alpha_size = 0;
383
384 // Alpha channel
385 uint8_t *alpha = NULL;
386
387 *width = mlt_properties_get_int( properties, "rescale_width" );
388 *height = mlt_properties_get_int( properties, "rescale_height" );
389
390 // Refresh the image
391 refresh_image( frame, *width, *height );
392
393 // Get the image
394 *buffer = mlt_properties_get_data( properties, "image", &image_size );
395 alpha = mlt_properties_get_data( properties, "alpha", &alpha_size );
396
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" );
400
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 )
404 {
405 if ( *format == mlt_image_yuv422 || *format == mlt_image_yuv420p )
406 {
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 );
410
411 memcpy( image_copy, *buffer, image_size );
412
413 // Copy or default the alpha
414 if ( alpha != NULL )
415 memcpy( alpha_copy, alpha, alpha_size );
416 else
417 memset( alpha_copy, 255, alpha_size );
418
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 );
422
423 // We're going to pass the copy on
424 *buffer = image_copy;
425 }
426 else if ( *format == mlt_image_rgb24a )
427 {
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 );
433
434 mlt_convert_yuv422_to_rgb24a(*buffer, image_copy, (*width)*(*height));
435
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 );
439
440 // We're going to pass the copy on
441 *buffer = image_copy;
442 }
443
444 }
445 else
446 {
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 );
450 *width = 50;
451 *height = 50;
452 }
453
454 return 0;
455 }
456
457 static uint8_t *producer_get_alpha_mask( mlt_frame this )
458 {
459 // Obtain properties of frame
460 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
461
462 // Return the alpha mask
463 return mlt_properties_get_data( properties, "alpha", NULL );
464 }
465
466 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
467 {
468 // Get the real structure for this producer
469 producer_pixbuf this = producer->child;
470
471 // Fetch the producers properties
472 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
473
474 if ( this->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
475 load_filenames( this, producer_properties );
476
477 // Generate a frame
478 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
479
480 if ( *frame != NULL && this->count > 0 )
481 {
482 // Obtain properties of frame and producer
483 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
484
485 // Set the producer on the frame properties
486 mlt_properties_set_data( properties, "producer_pixbuf", this, 0, NULL, NULL );
487
488 // Update timecode on the frame we're creating
489 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
490
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 ) );
493
494 // Refresh the image
495 refresh_image( *frame, 0, 0 );
496
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" ) );
500
501 // Set alpha call back
502 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
503
504 // Push the get_image method
505 mlt_frame_push_get_image( *frame, producer_get_image );
506 }
507
508 // Calculate the next timecode
509 mlt_producer_prepare_next( producer );
510
511 return 0;
512 }
513
514 static void producer_close( mlt_producer parent )
515 {
516 producer_pixbuf this = parent->child;
517 if ( !mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( parent ), "cache" ) )
518 {
519 mlt_pool_release( this->image );
520 mlt_pool_release( this->alpha );
521 }
522 parent->close = NULL;
523 mlt_producer_close( parent );
524 mlt_properties_close( this->filenames );
525 free( this );
526 }