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>
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.
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.
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.
21 #include "producer_pixbuf.h"
22 #include <framework/mlt_frame.h>
23 #include <gdk-pixbuf/gdk-pixbuf.h>
29 #include <sys/types.h>
34 typedef struct producer_pixbuf_s *producer_pixbuf;
36 struct producer_pixbuf_s
38 struct mlt_producer_s parent;
51 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
52 static void producer_close( mlt_producer parent );
54 static int filter_files( const struct dirent *de )
56 if ( de->d_name[ 0 ] != '.' )
62 mlt_producer producer_pixbuf_init( char *filename )
64 producer_pixbuf this = calloc( sizeof( struct producer_pixbuf_s ), 1 );
65 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
67 mlt_producer producer = &this->parent;
69 // Get the properties interface
70 mlt_properties properties = mlt_producer_properties( &this->parent );
72 // Callback registration
73 producer->get_frame = producer_get_frame;
74 producer->close = producer_close;
76 // Set the default properties
77 mlt_properties_set( properties, "resource", filename );
78 mlt_properties_set_int( properties, "ttl", 25 );
80 // Initialise gobject types
89 static void refresh_image( mlt_frame frame, int width, int height )
92 GdkPixbuf *pixbuf = NULL;
95 // Obtain properties of frame
96 mlt_properties properties = mlt_frame_properties( frame );
98 // Obtain the producer for this frame
99 producer_pixbuf this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
101 // Obtain the producer
102 mlt_producer producer = &this->parent;
104 // Obtain properties of producer
105 mlt_properties producer_props = mlt_producer_properties( producer );
107 // Get the time to live for each frame
108 double ttl = mlt_properties_get_int( producer_props, "ttl" );
110 // Get the original position of this frame
111 mlt_position position = mlt_properties_get_position( properties, "pixbuf_position" );
114 int image_idx = ( int )floor( ( double )position / ttl ) % this->count;
116 // optimization for subsequent iterations on single picture
117 if ( width != 0 && this->image != NULL && image_idx == this->image_idx )
119 if ( width != this->width || height != this->height )
121 pixbuf = mlt_properties_get_data( producer_props, "pixbuf", NULL );
122 mlt_pool_release( this->image );
123 mlt_pool_release( this->alpha );
128 else if ( this->image == NULL || image_idx != this->image_idx )
130 mlt_pool_release( this->image );
131 mlt_pool_release( this->alpha );
135 this->image_idx = image_idx;
136 pixbuf = gdk_pixbuf_new_from_file( this->filenames[ image_idx ], &error );
138 if ( pixbuf != NULL )
140 // Register this pixbuf for destruction and reuse
141 mlt_properties_set_data( producer_props, "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
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 ) );
146 // Store the width/height of the pixbuf temporarily
147 this->width = gdk_pixbuf_get_width( pixbuf );
148 this->height = gdk_pixbuf_get_height( pixbuf );
152 // If we have a pixbuf
153 if ( pixbuf && width > 0 )
155 char *interps = mlt_properties_get( properties, "rescale.interp" );
156 int interp = GDK_INTERP_BILINEAR;
158 if ( strcmp( interps, "nearest" ) == 0 )
159 interp = GDK_INTERP_NEAREST;
160 else if ( strcmp( interps, "tiles" ) == 0 )
161 interp = GDK_INTERP_TILES;
162 else if ( strcmp( interps, "hyper" ) == 0 )
163 interp = GDK_INTERP_HYPER;
165 // Note - the original pixbuf is already safe and ready for destruction
166 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
168 // Store width and height
170 this->height = height;
172 // Allocate/define image
173 this->image = mlt_pool_alloc( width * ( height + 1 ) * 2 );
175 // Extract YUV422 and alpha
176 if ( gdk_pixbuf_get_has_alpha( pixbuf ) )
178 // Allocate the alpha mask
179 this->alpha = mlt_pool_alloc( this->width * this->height );
182 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
183 this->width, this->height,
184 gdk_pixbuf_get_rowstride( pixbuf ),
185 this->image, this->alpha );
189 // No alpha to extract
190 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
191 this->width, this->height,
192 gdk_pixbuf_get_rowstride( pixbuf ),
196 // Finished with pixbuf now
197 g_object_unref( pixbuf );
200 // Set width/height of frame
201 mlt_properties_set_int( properties, "width", this->width );
202 mlt_properties_set_int( properties, "height", this->height );
203 mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "real_width" ) );
204 mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "real_height" ) );
206 // pass the image data without destructor
207 mlt_properties_set_data( properties, "image", this->image, this->width * ( this->height + 1 ) * 2, NULL, NULL );
208 mlt_properties_set_data( properties, "alpha", this->alpha, this->width * this->height, NULL, NULL );
211 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
213 // Obtain properties of frame
214 mlt_properties properties = mlt_frame_properties( frame );
216 *width = mlt_properties_get_int( properties, "rescale_width" );
217 *height = mlt_properties_get_int( properties, "rescale_height" );
220 refresh_image( frame, *width, *height );
222 // May need to know the size of the image to clone it
226 uint8_t *image = mlt_properties_get_data( properties, "image", &size );
228 // Get width and height
229 *width = mlt_properties_get_int( properties, "width" );
230 *height = mlt_properties_get_int( properties, "height" );
234 *width = mlt_properties_get_int( properties, "normalised_width" );
235 *height = mlt_properties_get_int( properties, "normalised_height" );
236 size = *width * ( *height + 1 );
239 // Clone if necessary
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 pixbuf...
245 uint8_t *copy = mlt_pool_alloc( size );
247 memcpy( copy, image, size );
249 // We're going to pass the copy on
252 // Now update properties so we free the copy after
253 mlt_properties_set_data( properties, "image", copy, size, mlt_pool_release, NULL );
262 static uint8_t *producer_get_alpha_mask( mlt_frame this )
264 // Obtain properties of frame
265 mlt_properties properties = mlt_frame_properties( this );
267 // Return the alpha mask
268 return mlt_properties_get_data( properties, "alpha", NULL );
271 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
273 // Get the real structure for this producer
274 producer_pixbuf this = producer->child;
276 if ( this->count == 0 && mlt_properties_get( mlt_producer_properties( producer ), "resource" ) != NULL )
278 mlt_properties properties = mlt_producer_properties( producer );
279 char *filename = mlt_properties_get( properties, "resource" );
282 if ( strstr( filename, "<svg" ) )
284 // Generate a temporary file for the svg
285 char fullname[ 1024 ] = "/tmp/mlt.XXXXXX";
286 int fd = mkstemp( fullname );
290 // Write the svg into the temp file
291 ssize_t remaining_bytes;
292 char *xml = filename;
294 // Strip leading crap
295 while ( xml[0] != '<' )
298 remaining_bytes = strlen( xml );
299 while ( remaining_bytes > 0 )
300 remaining_bytes -= write( fd, xml + strlen( xml ) - remaining_bytes, remaining_bytes );
303 this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
304 this->filenames[ this->count ++ ] = strdup( fullname );
306 mlt_properties_set_position( properties, "out", 250 );
308 // Teehe - when the producer closes, delete the temp file and the space allo
309 mlt_properties_set_data( properties, "__temporary_file__", this->filenames[ this->count - 1 ], 0, ( mlt_destructor )unlink, NULL );
313 else if ( strchr( filename, '%' ) != NULL )
315 // handle picture sequences
323 snprintf( full, 1023, filename, i ++ );
324 if ( stat( full, &buf ) == 0 )
326 this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
327 this->filenames[ this->count ++ ] = strdup( full );
335 mlt_properties_set_position( properties, "out", this->count * 250 );
337 else if ( strstr( filename, "/.all." ) != NULL )
339 char *dir_name = strdup( filename );
340 char *extension = strrchr( filename, '.' );
341 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
342 char fullname[ 1024 ];
343 strcpy( fullname, dir_name );
344 struct dirent **de = NULL;
345 int n = scandir( fullname, &de, filter_files, alphasort );
349 for (i = 0; i < n; i++ )
351 snprintf( fullname, 1023, "%s%s", dir_name, de[i]->d_name );
353 if ( strstr( fullname, extension ) && lstat( fullname, &info ) == 0 &&
354 ( S_ISREG( info.st_mode ) || info.st_mode | S_IXUSR ) )
356 this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
357 this->filenames[ this->count ++ ] = strdup( fullname );
367 this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
368 this->filenames[ this->count ++ ] = strdup( filename );
369 mlt_properties_set_position( properties, "out", 250 );
374 *frame = mlt_frame_init( );
376 if ( *frame != NULL && this->count > 0 )
378 // Obtain properties of frame and producer
379 mlt_properties properties = mlt_frame_properties( *frame );
381 // Set the producer on the frame properties
382 mlt_properties_set_data( properties, "producer_pixbuf", this, 0, NULL, NULL );
384 // Update timecode on the frame we're creating
385 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
387 // Ensure that we have a way to obtain the position in the get_image
388 mlt_properties_set_position( properties, "pixbuf_position", mlt_producer_position( producer ) );
391 refresh_image( *frame, 0, 0 );
393 // Set producer-specific frame properties
394 mlt_properties_set_int( properties, "progressive", 1 );
395 mlt_properties_set_double( properties, "aspect_ratio", 1 );
397 // Set alpha call back
398 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
400 // Push the get_image method
401 mlt_frame_push_get_image( *frame, producer_get_image );
404 // Calculate the next timecode
405 mlt_producer_prepare_next( producer );
410 static void producer_close( mlt_producer parent )
412 producer_pixbuf this = parent->child;
413 mlt_pool_release( this->image );
414 parent->close = NULL;
415 mlt_producer_close( parent );
416 while ( this->count -- )
417 free( this->filenames[ this->count ] );
418 free( this->filenames );