dad221ce4f1a45f7e274b0d46e704bf7f625a863
[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
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 return producer;
80 }
81 free( this );
82 return NULL;
83 }
84
85 static void refresh_image( mlt_frame frame, int width, int height )
86 {
87 // Pixbuf
88 GdkPixbuf *pixbuf = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "pixbuf", NULL );
89 GError *error = NULL;
90
91 // Obtain properties of frame
92 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
93
94 // Obtain the producer for this frame
95 producer_pixbuf this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
96
97 // Obtain the producer
98 mlt_producer producer = &this->parent;
99
100 // Obtain properties of producer
101 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
102
103 // Get the time to live for each frame
104 double ttl = mlt_properties_get_int( producer_props, "ttl" );
105
106 // Get the original position of this frame
107 mlt_position position = mlt_properties_get_position( properties, "pixbuf_position" );
108
109 // Image index
110 int image_idx = ( int )floor( ( double )position / ttl ) % this->count;
111
112 pthread_mutex_lock( &fastmutex );
113
114 // optimization for subsequent iterations on single picture
115 if ( width != 0 && this->image != NULL && image_idx == this->image_idx )
116 {
117 if ( width != this->width || height != this->height )
118 {
119 pixbuf = mlt_properties_get_data( producer_props, "_pixbuf", NULL );
120 mlt_pool_release( this->image );
121 mlt_pool_release( this->alpha );
122 this->image = NULL;
123 this->alpha = NULL;
124 }
125 }
126 else if ( pixbuf == NULL && ( this->image == NULL || image_idx != this->image_idx ) )
127 {
128 mlt_pool_release( this->image );
129 mlt_pool_release( this->alpha );
130 this->image = NULL;
131 this->alpha = NULL;
132
133 this->image_idx = image_idx;
134 pixbuf = gdk_pixbuf_new_from_file( mlt_properties_get_value( this->filenames, image_idx ), &error );
135
136 if ( pixbuf != NULL )
137 {
138 // Register this pixbuf for destruction and reuse
139 mlt_events_block( producer_props, NULL );
140 mlt_properties_set_data( producer_props, "_pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
141 g_object_ref( pixbuf );
142 mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
143
144 mlt_properties_set_int( producer_props, "_real_width", gdk_pixbuf_get_width( pixbuf ) );
145 mlt_properties_set_int( producer_props, "_real_height", gdk_pixbuf_get_height( pixbuf ) );
146 mlt_events_unblock( producer_props, NULL );
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 // We need to know the size of the image to clone it
221 int image_size = 0;
222 int alpha_size = 0;
223
224 // Alpha channel
225 uint8_t *alpha = NULL;
226
227 *width = mlt_properties_get_int( properties, "rescale_width" );
228 *height = mlt_properties_get_int( properties, "rescale_height" );
229
230 // Refresh the image
231 refresh_image( frame, *width, *height );
232
233 // Get the image
234 *buffer = mlt_properties_get_data( properties, "image", &image_size );
235 alpha = mlt_properties_get_data( properties, "alpha", &alpha_size );
236
237 // Get width and height (may have changed during the refresh)
238 *width = mlt_properties_get_int( properties, "width" );
239 *height = mlt_properties_get_int( properties, "height" );
240
241 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
242 // The fault is not in the design of mlt, but in the implementation of the pixbuf producer...
243 if ( *buffer != NULL )
244 {
245
246 if ( *format == mlt_image_yuv422 || *format == mlt_image_yuv420p )
247 {
248 // Clone the image and the alpha
249 uint8_t *image_copy = mlt_pool_alloc( image_size );
250 uint8_t *alpha_copy = mlt_pool_alloc( alpha_size );
251
252 memcpy( image_copy, *buffer, image_size );
253
254 // Copy or default the alpha
255 if ( alpha != NULL )
256 memcpy( alpha_copy, alpha, alpha_size );
257 else
258 memset( alpha_copy, 255, alpha_size );
259
260 // Now update properties so we free the copy after
261 mlt_properties_set_data( properties, "image", image_copy, image_size, mlt_pool_release, NULL );
262 mlt_properties_set_data( properties, "alpha", alpha_copy, alpha_size, mlt_pool_release, NULL );
263
264 // We're going to pass the copy on
265 *buffer = image_copy;
266 }
267 else if ( *format == mlt_image_rgb24a )
268 {
269 // Clone the image and the alpha
270 image_size = *width * ( *height + 1 ) * 4;
271 alpha_size = *width * ( *height + 1 );
272 uint8_t *image_copy = mlt_pool_alloc( image_size );
273 uint8_t *alpha_copy = mlt_pool_alloc( alpha_size );
274
275 mlt_convert_yuv422_to_rgb24a(*buffer, image_copy, (*width)*(*height));
276
277 // Now update properties so we free the copy after
278 mlt_properties_set_data( properties, "image", image_copy, image_size, mlt_pool_release, NULL );
279 mlt_properties_set_data( properties, "alpha", alpha_copy, alpha_size, mlt_pool_release, NULL );
280
281 // We're going to pass the copy on
282 *buffer = image_copy;
283 }
284 }
285 else
286 {
287 // TODO: Review all cases of invalid images
288 *buffer = mlt_pool_alloc( 50 * 50 * 2 );
289 mlt_properties_set_data( properties, "image", *buffer, image_size, mlt_pool_release, NULL );
290 *width = 50;
291 *height = 50;
292 }
293
294 return 0;
295 }
296
297
298 static uint8_t *producer_get_alpha_mask( mlt_frame this )
299 {
300 // Obtain properties of frame
301 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
302
303 // Return the alpha mask
304 return mlt_properties_get_data( properties, "alpha", NULL );
305 }
306
307 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
308 {
309 // Get the real structure for this producer
310 producer_pixbuf this = producer->child;
311
312 // Fetch the producers properties
313 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
314
315 if ( this->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
316 {
317 char *filename = mlt_properties_get( producer_properties, "resource" );
318 this->filenames = mlt_properties_new( );
319
320 // Read xml string
321 if ( strstr( filename, "<svg" ) )
322 {
323 // Generate a temporary file for the svg
324 char fullname[ 1024 ] = "/tmp/mlt.XXXXXX";
325 int fd = mkstemp( fullname );
326
327 if ( fd > -1 )
328 {
329 // Write the svg into the temp file
330 ssize_t remaining_bytes;
331 char *xml = filename;
332
333 // Strip leading crap
334 while ( xml[0] != '<' )
335 xml++;
336
337 remaining_bytes = strlen( xml );
338 while ( remaining_bytes > 0 )
339 remaining_bytes -= write( fd, xml + strlen( xml ) - remaining_bytes, remaining_bytes );
340 close( fd );
341
342 mlt_properties_set( this->filenames, "0", fullname );
343
344 // Teehe - when the producer closes, delete the temp file and the space allo
345 mlt_properties_set_data( producer_properties, "__temporary_file__", fullname, 0, ( mlt_destructor )unlink, NULL );
346 }
347 }
348 // Obtain filenames
349 else if ( strchr( filename, '%' ) != NULL )
350 {
351 // handle picture sequences
352 int i = mlt_properties_get_int( producer_properties, "begin" );
353 int gap = 0;
354 char full[1024];
355 int keyvalue = 0;
356 char key[ 50 ];
357
358 while ( gap < 100 )
359 {
360 struct stat buf;
361 snprintf( full, 1023, filename, i ++ );
362 if ( stat( full, &buf ) == 0 )
363 {
364 sprintf( key, "%d", keyvalue ++ );
365 mlt_properties_set( this->filenames, "0", full );
366 gap = 0;
367 }
368 else
369 {
370 gap ++;
371 }
372 }
373 }
374 else if ( strstr( filename, "/.all." ) != NULL )
375 {
376 char wildcard[ 1024 ];
377 char *dir_name = strdup( filename );
378 char *extension = strrchr( dir_name, '.' );
379
380 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
381 sprintf( wildcard, "*%s", extension );
382
383 mlt_properties_dir_list( this->filenames, dir_name, wildcard, 1 );
384
385 free( dir_name );
386 }
387 else
388 {
389 mlt_properties_set( this->filenames, "0", filename );
390 }
391
392 this->count = mlt_properties_count( this->filenames );
393 }
394
395 // Generate a frame
396 *frame = mlt_frame_init( );
397
398 if ( *frame != NULL && this->count > 0 )
399 {
400 // Obtain properties of frame and producer
401 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
402
403 // Set the producer on the frame properties
404 mlt_properties_set_data( properties, "producer_pixbuf", this, 0, NULL, NULL );
405
406 // Update timecode on the frame we're creating
407 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
408
409 // Ensure that we have a way to obtain the position in the get_image
410 mlt_properties_set_position( properties, "pixbuf_position", mlt_producer_position( producer ) );
411
412 // Refresh the image
413 refresh_image( *frame, 0, 0 );
414
415 // Set producer-specific frame properties
416 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( producer_properties, "progressive" ) );
417 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
418
419 // Set alpha call back
420 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
421
422 // Push the get_image method
423 mlt_frame_push_get_image( *frame, producer_get_image );
424 }
425
426 // Calculate the next timecode
427 mlt_producer_prepare_next( producer );
428
429 return 0;
430 }
431
432 static void producer_close( mlt_producer parent )
433 {
434 producer_pixbuf this = parent->child;
435 mlt_pool_release( this->image );
436 mlt_pool_release( this->alpha );
437 parent->close = NULL;
438 mlt_producer_close( parent );
439 mlt_properties_close( this->filenames );
440 free( this );
441 }