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 static int producer_get_frame( mlt_producer parent
, mlt_frame_ptr frame
, int index
);
35 static void producer_close( mlt_producer parent
);
37 static int filter_files( const struct dirent
*de
)
39 if ( de
->d_name
[ 0 ] != '.' )
45 mlt_producer
producer_pixbuf_init( char *filename
)
47 producer_pixbuf
this = calloc( sizeof( struct producer_pixbuf_s
), 1 );
48 if ( filename
!= NULL
&& this != NULL
&& mlt_producer_init( &this->parent
, this ) == 0 )
50 mlt_producer producer
= &this->parent
;
52 // Get the properties interface
53 mlt_properties properties
= mlt_producer_properties( &this->parent
);
55 // Callback registration
56 producer
->get_frame
= producer_get_frame
;
57 producer
->close
= producer_close
;
59 // Set the default properties
60 mlt_properties_set( properties
, "resource", filename
);
61 mlt_properties_set_int( properties
, "ttl", 25 );
64 if ( strchr( filename
, '%' ) != NULL
)
66 // handle picture sequences
74 snprintf( full
, 1023, filename
, i
++ );
75 if ( stat( full
, &buf
) == 0 )
77 this->filenames
= realloc( this->filenames
, sizeof( char * ) * ( this->count
+ 1 ) );
78 this->filenames
[ this->count
++ ] = strdup( full
);
86 mlt_properties_set_position( properties
, "out", this->count
* 250 );
88 else if ( strstr( filename
, "/.all." ) != NULL
)
90 char *dir_name
= strdup( filename
);
91 char *extension
= strrchr( filename
, '.' );
92 *( strstr( dir_name
, "/.all." ) + 1 ) = '\0';
93 char fullname
[ 1024 ];
94 strcpy( fullname
, dir_name
);
95 struct dirent
**de
= NULL
;
96 int n
= scandir( fullname
, &de
, filter_files
, alphasort
);
100 for (i
= 0; i
< n
; i
++ )
102 snprintf( fullname
, 1023, "%s%s", dir_name
, de
[i
]->d_name
);
104 if ( lstat( fullname
, &info
) == 0 &&
105 ( S_ISREG( info
.st_mode
) || ( strstr( fullname
, extension
) && info
.st_mode
| S_IXUSR
) ) )
107 this->filenames
= realloc( this->filenames
, sizeof( char * ) * ( this->count
+ 1 ) );
108 this->filenames
[ this->count
++ ] = strdup( fullname
);
118 this->filenames
= realloc( this->filenames
, sizeof( char * ) * ( this->count
+ 1 ) );
119 this->filenames
[ this->count
++ ] = strdup( filename
);
120 mlt_properties_set_position( properties
, "out", 250 );
123 // Initialise gobject types
132 static void refresh_image( mlt_frame frame
, int width
, int height
)
135 GdkPixbuf
*pixbuf
= NULL
;
136 GError
*error
= NULL
;
138 // Obtain properties of frame
139 mlt_properties properties
= mlt_frame_properties( frame
);
141 // Obtain the producer for this frame
142 producer_pixbuf
this = mlt_properties_get_data( properties
, "producer_pixbuf", NULL
);
144 // Obtain the producer
145 mlt_producer producer
= &this->parent
;
147 // Obtain properties of producer
148 mlt_properties producer_props
= mlt_producer_properties( producer
);
150 // Get the time to live for each frame
151 double ttl
= mlt_properties_get_int( producer_props
, "ttl" );
154 int image_idx
= ( int )floor( mlt_producer_position( producer
) / ttl
) % this->count
;
156 // Update timecode on the frame we're creating
157 mlt_frame_set_position( frame
, mlt_producer_position( producer
) );
159 // optimization for subsequent iterations on single picture
160 if ( width
!= 0 && this->image
!= NULL
&& image_idx
== this->image_idx
)
162 if ( width
!= this->width
|| height
!= this->height
)
164 pixbuf
= mlt_properties_get_data( producer_props
, "pixbuf", NULL
);
171 else if ( this->image
== NULL
|| image_idx
!= this->image_idx
)
178 this->image_idx
= image_idx
;
179 pixbuf
= gdk_pixbuf_new_from_file( this->filenames
[ image_idx
], &error
);
181 if ( pixbuf
!= NULL
)
183 // Register this pixbuf for destruction and reuse
184 mlt_properties_set_data( producer_props
, "pixbuf", pixbuf
, 0, ( mlt_destructor
)g_object_unref
, NULL
);
186 mlt_properties_set_int( producer_props
, "real_width", gdk_pixbuf_get_width( pixbuf
) );
187 mlt_properties_set_int( producer_props
, "real_height", gdk_pixbuf_get_height( pixbuf
) );
189 // Store the width/height of the pixbuf temporarily
190 this->width
= gdk_pixbuf_get_width( pixbuf
);
191 this->height
= gdk_pixbuf_get_height( pixbuf
);
195 // If we have a pixbuf
196 if ( pixbuf
&& width
> 0 )
198 char *interps
= mlt_properties_get( properties
, "rescale.interp" );
199 int interp
= GDK_INTERP_BILINEAR
;
201 if ( strcmp( interps
, "nearest" ) == 0 )
202 interp
= GDK_INTERP_NEAREST
;
203 else if ( strcmp( interps
, "tiles" ) == 0 )
204 interp
= GDK_INTERP_TILES
;
205 else if ( strcmp( interps
, "hyper" ) == 0 )
206 interp
= GDK_INTERP_HYPER
;
208 // fprintf( stderr, "SCALING PIXBUF from %dx%d to %dx%d was %dx%d\n", gdk_pixbuf_get_width( pixbuf ), gdk_pixbuf_get_height( pixbuf ), width, height, this->width, this->height );
210 // Note - the original pixbuf is already safe and ready for destruction
211 pixbuf
= gdk_pixbuf_scale_simple( pixbuf
, width
, height
, interp
);
213 // Store width and height
215 this->height
= height
;
217 // Allocate/define image
219 uint8_t *image
= malloc( width
* ( height
+ 1 ) * 2 );
220 uint8_t *alpha
= NULL
;
222 // Extract YUV422 and alpha
223 if ( gdk_pixbuf_get_has_alpha( pixbuf
) )
225 // Allocate the alpha mask
226 alpha
= malloc( this->width
* this->height
);
229 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf
),
230 this->width
, this->height
,
231 gdk_pixbuf_get_rowstride( pixbuf
),
236 // No alpha to extract
237 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf
),
238 this->width
, this->height
,
239 gdk_pixbuf_get_rowstride( pixbuf
),
243 // Finished with pixbuf now
244 g_object_unref( pixbuf
);
246 // Assign images to producer
251 // Set width/height of frame
252 mlt_properties_set_int( properties
, "width", this->width
);
253 mlt_properties_set_int( properties
, "height", this->height
);
254 mlt_properties_set_int( properties
, "real_width", mlt_properties_get_int( producer_props
, "real_width" ) );
255 mlt_properties_set_int( properties
, "real_height", mlt_properties_get_int( producer_props
, "real_height" ) );
257 // pass the image data without destructor
258 mlt_properties_set_data( properties
, "image", this->image
, this->width
* ( this->height
+ 1 ) * 2, NULL
, NULL
);
259 mlt_properties_set_data( properties
, "alpha", this->alpha
, this->width
* this->height
, NULL
, NULL
);
262 static int producer_get_image( mlt_frame frame
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
264 // Obtain properties of frame
265 mlt_properties properties
= mlt_frame_properties( frame
);
267 *width
= mlt_properties_get_int( properties
, "rescale_width" );
268 *height
= mlt_properties_get_int( properties
, "rescale_height" );
271 refresh_image( frame
, *width
, *height
);
274 //mlt_producer this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
275 //*format = ( mlt_properties_get_int( mlt_producer_properties( this ), "bpp" ) == 4 ) ? mlt_image_rgb24a : mlt_image_rgb24;
277 // May need to know the size of the image to clone it
281 uint8_t *image
= mlt_properties_get_data( properties
, "image", &size
);
283 // Get width and height
284 *width
= mlt_properties_get_int( properties
, "width" );
285 *height
= mlt_properties_get_int( properties
, "height" );
287 // Clone if necessary
288 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
289 // The fault is not in the design of mlt, but in the implementation of pixbuf...
294 uint8_t *copy
= malloc( size
);
295 memcpy( copy
, image
, size
);
297 // We're going to pass the copy on
300 // Now update properties so we free the copy after
301 mlt_properties_set_data( properties
, "image", copy
, size
, free
, NULL
);
310 static uint8_t *producer_get_alpha_mask( mlt_frame
this )
312 // Obtain properties of frame
313 mlt_properties properties
= mlt_frame_properties( this );
315 // Return the alpha mask
316 return mlt_properties_get_data( properties
, "alpha", NULL
);
319 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
321 // Get the real structure for this producer
322 producer_pixbuf
this = producer
->child
;
325 *frame
= mlt_frame_init( );
327 // Obtain properties of frame and producer
328 mlt_properties properties
= mlt_frame_properties( *frame
);
330 // Set the producer on the frame properties
331 mlt_properties_set_data( properties
, "producer_pixbuf", this, 0, NULL
, NULL
);
334 refresh_image( *frame
, 0, 0 );
336 // Set producer-specific frame properties
337 mlt_properties_set_int( properties
, "progressive", 1 );
338 mlt_properties_set_double( properties
, "aspect_ratio", mlt_properties_get_double( properties
, "real_width" ) / mlt_properties_get_double( properties
, "real_height" ) );
340 // Set alpha call back
341 ( *frame
)->get_alpha_mask
= producer_get_alpha_mask
;
343 // Push the get_image method
344 mlt_frame_push_get_image( *frame
, producer_get_image
);
346 // Calculate the next timecode
347 mlt_producer_prepare_next( producer
);
352 static void producer_close( mlt_producer parent
)
354 producer_pixbuf
this = parent
->child
;
356 parent
->close
= NULL
;
357 mlt_producer_close( parent
);