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