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