+ Usage of mlt_properties_dir_list
[melted] / src / modules / gtk2 / producer_pixbuf.c
1
2 /*
3 * producer_pixbuf.c -- raster image loader based upon gdk-pixbuf
4 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
5 * Author: Dan Dennedy <dan@dennedy.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22 #include "producer_pixbuf.h"
23 #include <framework/mlt_frame.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 fastmutex = 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 };
54
55 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
56 static void producer_close( mlt_producer parent );
57
58 mlt_producer producer_pixbuf_init( char *filename )
59 {
60 producer_pixbuf this = calloc( sizeof( struct producer_pixbuf_s ), 1 );
61 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
62 {
63 mlt_producer producer = &this->parent;
64
65 // Get the properties interface
66 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
67
68 // Callback registration
69 producer->get_frame = producer_get_frame;
70 producer->close = ( mlt_destructor )producer_close;
71
72 // Set the default properties
73 mlt_properties_set( properties, "resource", filename );
74 mlt_properties_set_int( properties, "ttl", 25 );
75 mlt_properties_set_int( properties, "aspect_ratio", 1 );
76 mlt_properties_set_int( properties, "progressive", 1 );
77
78 return producer;
79 }
80 free( this );
81 return NULL;
82 }
83
84 static void refresh_image( mlt_frame frame, int width, int height )
85 {
86 // Pixbuf
87 GdkPixbuf *pixbuf = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "pixbuf", NULL );
88 GError *error = NULL;
89
90 // Obtain properties of frame
91 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
92
93 // Obtain the producer for this frame
94 producer_pixbuf this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
95
96 // Obtain the producer
97 mlt_producer producer = &this->parent;
98
99 // Obtain properties of producer
100 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
101
102 // Get the time to live for each frame
103 double ttl = mlt_properties_get_int( producer_props, "ttl" );
104
105 // Get the original position of this frame
106 mlt_position position = mlt_properties_get_position( properties, "pixbuf_position" );
107
108 // Image index
109 int image_idx = ( int )floor( ( double )position / ttl ) % this->count;
110
111 pthread_mutex_lock( &fastmutex );
112
113 // optimization for subsequent iterations on single picture
114 if ( width != 0 && this->image != NULL && image_idx == this->image_idx )
115 {
116 if ( width != this->width || height != this->height )
117 {
118 pixbuf = mlt_properties_get_data( producer_props, "_pixbuf", NULL );
119 mlt_pool_release( this->image );
120 mlt_pool_release( this->alpha );
121 this->image = NULL;
122 this->alpha = NULL;
123 }
124 }
125 else if ( pixbuf == NULL && ( this->image == NULL || image_idx != this->image_idx ) )
126 {
127 mlt_pool_release( this->image );
128 mlt_pool_release( this->alpha );
129 this->image = NULL;
130 this->alpha = NULL;
131
132 this->image_idx = image_idx;
133 pixbuf = gdk_pixbuf_new_from_file( mlt_properties_get_value( this->filenames, image_idx ), &error );
134
135 if ( pixbuf != NULL )
136 {
137 // Register this pixbuf for destruction and reuse
138 mlt_events_block( producer_props, NULL );
139 mlt_properties_set_data( producer_props, "_pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
140 g_object_ref( pixbuf );
141 mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
142
143 mlt_properties_set_int( producer_props, "_real_width", gdk_pixbuf_get_width( pixbuf ) );
144 mlt_properties_set_int( producer_props, "_real_height", gdk_pixbuf_get_height( pixbuf ) );
145 mlt_events_unblock( producer_props, NULL );
146
147 // Store the width/height of the pixbuf temporarily
148 this->width = gdk_pixbuf_get_width( pixbuf );
149 this->height = gdk_pixbuf_get_height( pixbuf );
150 }
151 }
152
153 // If we have a pixbuf
154 if ( pixbuf && width > 0 )
155 {
156 char *interps = mlt_properties_get( properties, "rescale.interp" );
157 int interp = GDK_INTERP_BILINEAR;
158
159 if ( strcmp( interps, "nearest" ) == 0 )
160 interp = GDK_INTERP_NEAREST;
161 else if ( strcmp( interps, "tiles" ) == 0 )
162 interp = GDK_INTERP_TILES;
163 else if ( strcmp( interps, "hyper" ) == 0 )
164 interp = GDK_INTERP_HYPER;
165
166 // Note - the original pixbuf is already safe and ready for destruction
167 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
168
169 // Store width and height
170 this->width = width;
171 this->height = height;
172
173 // Allocate/define image
174 this->image = mlt_pool_alloc( width * ( height + 1 ) * 2 );
175
176 // Extract YUV422 and alpha
177 if ( gdk_pixbuf_get_has_alpha( pixbuf ) )
178 {
179 // Allocate the alpha mask
180 this->alpha = mlt_pool_alloc( this->width * this->height );
181
182 // Convert the image
183 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
184 this->width, this->height,
185 gdk_pixbuf_get_rowstride( pixbuf ),
186 this->image, this->alpha );
187 }
188 else
189 {
190 // No alpha to extract
191 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
192 this->width, this->height,
193 gdk_pixbuf_get_rowstride( pixbuf ),
194 this->image );
195 }
196
197 // Finished with pixbuf now
198 g_object_unref( pixbuf );
199 }
200
201 // Set width/height of frame
202 mlt_properties_set_int( properties, "width", this->width );
203 mlt_properties_set_int( properties, "height", this->height );
204 mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
205 mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
206
207 // pass the image data without destructor
208 mlt_properties_set_data( properties, "image", this->image, this->width * ( this->height + 1 ) * 2, NULL, NULL );
209 mlt_properties_set_data( properties, "alpha", this->alpha, this->width * this->height, NULL, NULL );
210
211 pthread_mutex_unlock( &fastmutex );
212 }
213
214 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
215 {
216 // Obtain properties of frame
217 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
218
219 // We need to know the size of the image to clone it
220 int image_size = 0;
221 int alpha_size = 0;
222
223 // Alpha channel
224 uint8_t *alpha = NULL;
225
226 *width = mlt_properties_get_int( properties, "rescale_width" );
227 *height = mlt_properties_get_int( properties, "rescale_height" );
228
229 // Refresh the image
230 refresh_image( frame, *width, *height );
231
232 // Get the image
233 *buffer = mlt_properties_get_data( properties, "image", &image_size );
234 alpha = mlt_properties_get_data( properties, "alpha", &alpha_size );
235
236 // Get width and height (may have changed during the refresh)
237 *width = mlt_properties_get_int( properties, "width" );
238 *height = mlt_properties_get_int( properties, "height" );
239
240 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
241 // The fault is not in the design of mlt, but in the implementation of the pixbuf producer...
242 if ( *buffer != NULL )
243 {
244 // Clone the image and the alpha
245 uint8_t *image_copy = mlt_pool_alloc( image_size );
246 uint8_t *alpha_copy = mlt_pool_alloc( alpha_size );
247
248 memcpy( image_copy, *buffer, image_size );
249
250 // Copy or default the alpha
251 if ( alpha != NULL )
252 memcpy( alpha_copy, alpha, alpha_size );
253 else
254 memset( alpha_copy, 255, alpha_size );
255
256 // Now update properties so we free the copy after
257 mlt_properties_set_data( properties, "image", image_copy, image_size, mlt_pool_release, NULL );
258 mlt_properties_set_data( properties, "alpha", alpha_copy, alpha_size, mlt_pool_release, NULL );
259
260 // We're going to pass the copy on
261 *buffer = image_copy;
262 }
263 else
264 {
265 // TODO: Review all cases of invalid images
266 *buffer = mlt_pool_alloc( 50 * 50 * 2 );
267 mlt_properties_set_data( properties, "image", *buffer, image_size, mlt_pool_release, NULL );
268 *width = 50;
269 *height = 50;
270 }
271
272 return 0;
273 }
274
275 static uint8_t *producer_get_alpha_mask( mlt_frame this )
276 {
277 // Obtain properties of frame
278 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
279
280 // Return the alpha mask
281 return mlt_properties_get_data( properties, "alpha", NULL );
282 }
283
284 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
285 {
286 // Get the real structure for this producer
287 producer_pixbuf this = producer->child;
288
289 // Fetch the producers properties
290 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
291
292 if ( this->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
293 {
294 char *filename = mlt_properties_get( producer_properties, "resource" );
295 this->filenames = mlt_properties_new( );
296
297 // Read xml string
298 if ( strstr( filename, "<svg" ) )
299 {
300 // Generate a temporary file for the svg
301 char fullname[ 1024 ] = "/tmp/mlt.XXXXXX";
302 int fd = mkstemp( fullname );
303
304 if ( fd > -1 )
305 {
306 // Write the svg into the temp file
307 ssize_t remaining_bytes;
308 char *xml = filename;
309
310 // Strip leading crap
311 while ( xml[0] != '<' )
312 xml++;
313
314 remaining_bytes = strlen( xml );
315 while ( remaining_bytes > 0 )
316 remaining_bytes -= write( fd, xml + strlen( xml ) - remaining_bytes, remaining_bytes );
317 close( fd );
318
319 mlt_properties_set( this->filenames, "0", fullname );
320
321 // Teehe - when the producer closes, delete the temp file and the space allo
322 mlt_properties_set_data( producer_properties, "__temporary_file__", fullname, 0, ( mlt_destructor )unlink, NULL );
323 }
324 }
325 // Obtain filenames
326 else if ( strchr( filename, '%' ) != NULL )
327 {
328 // handle picture sequences
329 int i = mlt_properties_get_int( producer_properties, "begin" );
330 int gap = 0;
331 char full[1024];
332 int keyvalue = 0;
333 char key[ 50 ];
334
335 while ( gap < 100 )
336 {
337 struct stat buf;
338 snprintf( full, 1023, filename, i ++ );
339 if ( stat( full, &buf ) == 0 )
340 {
341 sprintf( key, "%d", keyvalue ++ );
342 mlt_properties_set( this->filenames, "0", full );
343 gap = 0;
344 }
345 else
346 {
347 gap ++;
348 }
349 }
350 }
351 else if ( strstr( filename, "/.all." ) != NULL )
352 {
353 char wildcard[ 1024 ];
354 char *dir_name = strdup( filename );
355 char *extension = strrchr( dir_name, '.' );
356
357 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
358 sprintf( wildcard, "*%s", extension );
359
360 mlt_properties_dir_list( this->filenames, dir_name, wildcard, 1 );
361
362 free( dir_name );
363 }
364 else
365 {
366 mlt_properties_set( this->filenames, "0", filename );
367 }
368
369 this->count = mlt_properties_count( this->filenames );
370 }
371
372 // Generate a frame
373 *frame = mlt_frame_init( );
374
375 if ( *frame != NULL && this->count > 0 )
376 {
377 // Obtain properties of frame and producer
378 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
379
380 // Set the producer on the frame properties
381 mlt_properties_set_data( properties, "producer_pixbuf", this, 0, NULL, NULL );
382
383 // Update timecode on the frame we're creating
384 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
385
386 // Ensure that we have a way to obtain the position in the get_image
387 mlt_properties_set_position( properties, "pixbuf_position", mlt_producer_position( producer ) );
388
389 // Refresh the image
390 refresh_image( *frame, 0, 0 );
391
392 // Set producer-specific frame properties
393 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( producer_properties, "progressive" ) );
394 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
395
396 // Set alpha call back
397 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
398
399 // Push the get_image method
400 mlt_frame_push_get_image( *frame, producer_get_image );
401 }
402
403 // Calculate the next timecode
404 mlt_producer_prepare_next( producer );
405
406 return 0;
407 }
408
409 static void producer_close( mlt_producer parent )
410 {
411 producer_pixbuf this = parent->child;
412 mlt_pool_release( this->image );
413 parent->close = NULL;
414 mlt_producer_close( parent );
415 mlt_properties_close( this->filenames );
416 free( this );
417 }