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