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>
30 #include <sys/types.h>
35 pthread_mutex_t fastmutex
= PTHREAD_MUTEX_INITIALIZER
;
37 typedef struct producer_pixbuf_s
*producer_pixbuf
;
39 struct producer_pixbuf_s
41 struct mlt_producer_s parent
;
54 static int producer_get_frame( mlt_producer parent
, mlt_frame_ptr frame
, int index
);
55 static void producer_close( mlt_producer parent
);
57 static int filter_files( const struct dirent
*de
)
59 if ( de
->d_name
[ 0 ] != '.' )
65 mlt_producer
producer_pixbuf_init( char *filename
)
67 producer_pixbuf
this = calloc( sizeof( struct producer_pixbuf_s
), 1 );
68 if ( this != NULL
&& mlt_producer_init( &this->parent
, this ) == 0 )
70 mlt_producer producer
= &this->parent
;
72 // Get the properties interface
73 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( &this->parent
);
75 // Callback registration
76 producer
->get_frame
= producer_get_frame
;
77 producer
->close
= ( mlt_destructor
)producer_close
;
79 // Set the default properties
80 mlt_properties_set( properties
, "resource", filename
);
81 mlt_properties_set_int( properties
, "ttl", 25 );
82 mlt_properties_set_int( properties
, "aspect_ratio", 1 );
83 mlt_properties_set_int( properties
, "progressive", 1 );
91 static void refresh_image( mlt_frame frame
, int width
, int height
)
94 GdkPixbuf
*pixbuf
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "pixbuf", NULL
);
97 // Obtain properties of frame
98 mlt_properties properties
= MLT_FRAME_PROPERTIES( frame
);
100 // Obtain the producer for this frame
101 producer_pixbuf
this = mlt_properties_get_data( properties
, "producer_pixbuf", NULL
);
103 // Obtain the producer
104 mlt_producer producer
= &this->parent
;
106 // Obtain properties of producer
107 mlt_properties producer_props
= MLT_PRODUCER_PROPERTIES( producer
);
109 // Get the time to live for each frame
110 double ttl
= mlt_properties_get_int( producer_props
, "ttl" );
112 // Get the original position of this frame
113 mlt_position position
= mlt_properties_get_position( properties
, "pixbuf_position" );
116 int image_idx
= ( int )floor( ( double )position
/ ttl
) % this->count
;
118 pthread_mutex_lock( &fastmutex
);
120 // optimization for subsequent iterations on single picture
121 if ( width
!= 0 && this->image
!= NULL
&& image_idx
== this->image_idx
)
123 if ( width
!= this->width
|| height
!= this->height
)
125 pixbuf
= mlt_properties_get_data( producer_props
, "pixbuf", NULL
);
126 mlt_pool_release( this->image
);
127 mlt_pool_release( this->alpha
);
132 else if ( pixbuf
== NULL
&& ( this->image
== NULL
|| image_idx
!= this->image_idx
) )
134 mlt_pool_release( this->image
);
135 mlt_pool_release( this->alpha
);
139 this->image_idx
= image_idx
;
140 pixbuf
= gdk_pixbuf_new_from_file( this->filenames
[ image_idx
], &error
);
142 if ( pixbuf
!= NULL
)
144 // Register this pixbuf for destruction and reuse
145 mlt_properties_set_data( producer_props
, "pixbuf", pixbuf
, 0, ( mlt_destructor
)g_object_unref
, NULL
);
146 g_object_ref( pixbuf
);
147 mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame
), "pixbuf", pixbuf
, 0, ( mlt_destructor
)g_object_unref
, NULL
);
149 mlt_properties_set_int( producer_props
, "real_width", gdk_pixbuf_get_width( pixbuf
) );
150 mlt_properties_set_int( producer_props
, "real_height", gdk_pixbuf_get_height( pixbuf
) );
152 // Store the width/height of the pixbuf temporarily
153 this->width
= gdk_pixbuf_get_width( pixbuf
);
154 this->height
= gdk_pixbuf_get_height( pixbuf
);
158 // If we have a pixbuf
159 if ( pixbuf
&& width
> 0 )
161 char *interps
= mlt_properties_get( properties
, "rescale.interp" );
162 int interp
= GDK_INTERP_BILINEAR
;
164 if ( strcmp( interps
, "nearest" ) == 0 )
165 interp
= GDK_INTERP_NEAREST
;
166 else if ( strcmp( interps
, "tiles" ) == 0 )
167 interp
= GDK_INTERP_TILES
;
168 else if ( strcmp( interps
, "hyper" ) == 0 )
169 interp
= GDK_INTERP_HYPER
;
171 // Note - the original pixbuf is already safe and ready for destruction
172 pixbuf
= gdk_pixbuf_scale_simple( pixbuf
, width
, height
, interp
);
174 // Store width and height
176 this->height
= height
;
178 // Allocate/define image
179 this->image
= mlt_pool_alloc( width
* ( height
+ 1 ) * 2 );
181 // Extract YUV422 and alpha
182 if ( gdk_pixbuf_get_has_alpha( pixbuf
) )
184 // Allocate the alpha mask
185 this->alpha
= mlt_pool_alloc( this->width
* this->height
);
188 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf
),
189 this->width
, this->height
,
190 gdk_pixbuf_get_rowstride( pixbuf
),
191 this->image
, this->alpha
);
195 // No alpha to extract
196 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf
),
197 this->width
, this->height
,
198 gdk_pixbuf_get_rowstride( pixbuf
),
202 // Finished with pixbuf now
203 g_object_unref( pixbuf
);
206 // Set width/height of frame
207 mlt_properties_set_int( properties
, "width", this->width
);
208 mlt_properties_set_int( properties
, "height", this->height
);
209 mlt_properties_set_int( properties
, "real_width", mlt_properties_get_int( producer_props
, "real_width" ) );
210 mlt_properties_set_int( properties
, "real_height", mlt_properties_get_int( producer_props
, "real_height" ) );
212 // pass the image data without destructor
213 mlt_properties_set_data( properties
, "image", this->image
, this->width
* ( this->height
+ 1 ) * 2, NULL
, NULL
);
214 mlt_properties_set_data( properties
, "alpha", this->alpha
, this->width
* this->height
, NULL
, NULL
);
216 pthread_mutex_unlock( &fastmutex
);
219 static int producer_get_image( mlt_frame frame
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
221 // Obtain properties of frame
222 mlt_properties properties
= MLT_FRAME_PROPERTIES( frame
);
224 // We need to know the size of the image to clone it
229 uint8_t *alpha
= NULL
;
231 *width
= mlt_properties_get_int( properties
, "rescale_width" );
232 *height
= mlt_properties_get_int( properties
, "rescale_height" );
235 refresh_image( frame
, *width
, *height
);
238 *buffer
= mlt_properties_get_data( properties
, "image", &image_size
);
239 alpha
= mlt_properties_get_data( properties
, "alpha", &alpha_size
);
241 // Get width and height (may have changed during the refresh)
242 *width
= mlt_properties_get_int( properties
, "width" );
243 *height
= mlt_properties_get_int( properties
, "height" );
245 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
246 // The fault is not in the design of mlt, but in the implementation of the pixbuf producer...
247 if ( *buffer
!= NULL
)
249 // Clone the image and the alpha
250 uint8_t *image_copy
= mlt_pool_alloc( image_size
);
251 uint8_t *alpha_copy
= mlt_pool_alloc( alpha_size
);
253 memcpy( image_copy
, *buffer
, image_size
);
255 // Copy or default the alpha
257 memcpy( alpha_copy
, alpha
, alpha_size
);
259 memset( alpha_copy
, 255, alpha_size
);
261 // Now update properties so we free the copy after
262 mlt_properties_set_data( properties
, "image", image_copy
, image_size
, mlt_pool_release
, NULL
);
263 mlt_properties_set_data( properties
, "alpha", alpha_copy
, alpha_size
, mlt_pool_release
, NULL
);
265 // We're going to pass the copy on
266 *buffer
= image_copy
;
270 // TODO: Review all cases of invalid images
271 *buffer
= mlt_pool_alloc( 50 * 50 * 2 );
272 mlt_properties_set_data( properties
, "image", *buffer
, image_size
, mlt_pool_release
, NULL
);
280 static uint8_t *producer_get_alpha_mask( mlt_frame
this )
282 // Obtain properties of frame
283 mlt_properties properties
= MLT_FRAME_PROPERTIES( this );
285 // Return the alpha mask
286 return mlt_properties_get_data( properties
, "alpha", NULL
);
289 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
291 // Get the real structure for this producer
292 producer_pixbuf
this = producer
->child
;
294 // Fetch the producers properties
295 mlt_properties producer_properties
= MLT_PRODUCER_PROPERTIES( producer
);
297 if ( this->count
== 0 && mlt_properties_get( producer_properties
, "resource" ) != NULL
)
299 char *filename
= mlt_properties_get( producer_properties
, "resource" );
302 if ( strstr( filename
, "<svg" ) )
304 // Generate a temporary file for the svg
305 char fullname
[ 1024 ] = "/tmp/mlt.XXXXXX";
306 int fd
= mkstemp( fullname
);
310 // Write the svg into the temp file
311 ssize_t remaining_bytes
;
312 char *xml
= filename
;
314 // Strip leading crap
315 while ( xml
[0] != '<' )
318 remaining_bytes
= strlen( xml
);
319 while ( remaining_bytes
> 0 )
320 remaining_bytes
-= write( fd
, xml
+ strlen( xml
) - remaining_bytes
, remaining_bytes
);
323 this->filenames
= realloc( this->filenames
, sizeof( char * ) * ( this->count
+ 1 ) );
324 this->filenames
[ this->count
++ ] = strdup( fullname
);
326 // Teehe - when the producer closes, delete the temp file and the space allo
327 mlt_properties_set_data( producer_properties
, "__temporary_file__", this->filenames
[ this->count
- 1 ], 0, ( mlt_destructor
)unlink
, NULL
);
331 else if ( strchr( filename
, '%' ) != NULL
)
333 // handle picture sequences
334 int i
= mlt_properties_get_int( producer_properties
, "begin" );
341 snprintf( full
, 1023, filename
, i
++ );
342 if ( stat( full
, &buf
) == 0 )
344 this->filenames
= realloc( this->filenames
, sizeof( char * ) * ( this->count
+ 1 ) );
345 this->filenames
[ this->count
++ ] = strdup( full
);
354 else if ( strstr( filename
, "/.all." ) != NULL
)
356 char *dir_name
= strdup( filename
);
357 char *extension
= strrchr( filename
, '.' );
358 *( strstr( dir_name
, "/.all." ) + 1 ) = '\0';
359 char fullname
[ 1024 ];
360 strcpy( fullname
, dir_name
);
361 struct dirent
**de
= NULL
;
362 int n
= scandir( fullname
, &de
, filter_files
, alphasort
);
366 for (i
= 0; i
< n
; i
++ )
368 snprintf( fullname
, 1023, "%s%s", dir_name
, de
[i
]->d_name
);
370 if ( strstr( fullname
, extension
) && lstat( fullname
, &info
) == 0 &&
371 ( S_ISREG( info
.st_mode
) || info
.st_mode
| S_IXUSR
) )
373 this->filenames
= realloc( this->filenames
, sizeof( char * ) * ( this->count
+ 1 ) );
374 this->filenames
[ this->count
++ ] = strdup( fullname
);
384 this->filenames
= realloc( this->filenames
, sizeof( char * ) * ( this->count
+ 1 ) );
385 this->filenames
[ this->count
++ ] = strdup( filename
);
390 *frame
= mlt_frame_init( );
392 if ( *frame
!= NULL
&& this->count
> 0 )
394 // Obtain properties of frame and producer
395 mlt_properties properties
= MLT_FRAME_PROPERTIES( *frame
);
397 // Set the producer on the frame properties
398 mlt_properties_set_data( properties
, "producer_pixbuf", this, 0, NULL
, NULL
);
400 // Update timecode on the frame we're creating
401 mlt_frame_set_position( *frame
, mlt_producer_position( producer
) );
403 // Ensure that we have a way to obtain the position in the get_image
404 mlt_properties_set_position( properties
, "pixbuf_position", mlt_producer_position( producer
) );
407 refresh_image( *frame
, 0, 0 );
409 // Set producer-specific frame properties
410 mlt_properties_set_int( properties
, "progressive", mlt_properties_get_int( producer_properties
, "progressive" ) );
411 mlt_properties_set_double( properties
, "aspect_ratio", mlt_properties_get_double( producer_properties
, "aspect_ratio" ) );
413 // Set alpha call back
414 ( *frame
)->get_alpha_mask
= producer_get_alpha_mask
;
416 // Push the get_image method
417 mlt_frame_push_get_image( *frame
, producer_get_image
);
420 // Calculate the next timecode
421 mlt_producer_prepare_next( producer
);
426 static void producer_close( mlt_producer parent
)
428 producer_pixbuf
this = parent
->child
;
429 mlt_pool_release( this->image
);
430 parent
->close
= NULL
;
431 mlt_producer_close( parent
);
432 while ( this->count
-- )
433 free( this->filenames
[ this->count
] );
434 free( this->filenames
);