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
;
53 static int producer_get_frame( mlt_producer parent
, mlt_frame_ptr frame
, int index
);
54 static void producer_close( mlt_producer parent
);
56 static int filter_files( const struct dirent
*de
)
58 if ( de
->d_name
[ 0 ] != '.' )
64 mlt_producer
producer_pixbuf_init( char *filename
)
66 producer_pixbuf
this = calloc( sizeof( struct producer_pixbuf_s
), 1 );
67 if ( filename
!= NULL
&& this != NULL
&& mlt_producer_init( &this->parent
, this ) == 0 )
69 mlt_producer producer
= &this->parent
;
71 // Get the properties interface
72 mlt_properties properties
= mlt_producer_properties( &this->parent
);
74 // Callback registration
75 producer
->get_frame
= producer_get_frame
;
76 producer
->close
= producer_close
;
78 // Set the default properties
79 mlt_properties_set( properties
, "resource", filename
);
80 mlt_properties_set_int( properties
, "ttl", 25 );
83 if ( strchr( filename
, '%' ) != NULL
)
85 // handle picture sequences
93 snprintf( full
, 1023, filename
, i
++ );
94 if ( stat( full
, &buf
) == 0 )
96 this->filenames
= realloc( this->filenames
, sizeof( char * ) * ( this->count
+ 1 ) );
97 this->filenames
[ this->count
++ ] = strdup( full
);
105 mlt_properties_set_position( properties
, "out", this->count
* 250 );
107 else if ( strstr( filename
, "/.all." ) != NULL
)
109 char *dir_name
= strdup( filename
);
110 char *extension
= strrchr( filename
, '.' );
111 *( strstr( dir_name
, "/.all." ) + 1 ) = '\0';
112 char fullname
[ 1024 ];
113 strcpy( fullname
, dir_name
);
114 struct dirent
**de
= NULL
;
115 int n
= scandir( fullname
, &de
, filter_files
, alphasort
);
119 for (i
= 0; i
< n
; i
++ )
121 snprintf( fullname
, 1023, "%s%s", dir_name
, de
[i
]->d_name
);
123 if ( lstat( fullname
, &info
) == 0 &&
124 ( S_ISREG( info
.st_mode
) || ( strstr( fullname
, extension
) && info
.st_mode
| S_IXUSR
) ) )
126 this->filenames
= realloc( this->filenames
, sizeof( char * ) * ( this->count
+ 1 ) );
127 this->filenames
[ this->count
++ ] = strdup( fullname
);
137 this->filenames
= realloc( this->filenames
, sizeof( char * ) * ( this->count
+ 1 ) );
138 this->filenames
[ this->count
++ ] = strdup( filename
);
139 mlt_properties_set_position( properties
, "out", 250 );
142 // Initialise gobject types
151 static void refresh_image( mlt_frame frame
, int width
, int height
)
154 GdkPixbuf
*pixbuf
= NULL
;
155 GError
*error
= NULL
;
157 // Obtain properties of frame
158 mlt_properties properties
= mlt_frame_properties( frame
);
160 // Obtain the producer for this frame
161 producer_pixbuf
this = mlt_properties_get_data( properties
, "producer_pixbuf", NULL
);
163 // Obtain the producer
164 mlt_producer producer
= &this->parent
;
166 // Obtain properties of producer
167 mlt_properties producer_props
= mlt_producer_properties( producer
);
169 // Get the time to live for each frame
170 double ttl
= mlt_properties_get_int( producer_props
, "ttl" );
173 int image_idx
= ( int )floor( mlt_producer_position( producer
) / ttl
) % this->count
;
175 // Update timecode on the frame we're creating
176 mlt_frame_set_position( frame
, mlt_producer_position( producer
) );
178 // optimization for subsequent iterations on single picture
179 if ( width
!= 0 && this->image
!= NULL
&& image_idx
== this->image_idx
)
181 if ( width
!= this->width
|| height
!= this->height
)
183 pixbuf
= mlt_properties_get_data( producer_props
, "pixbuf", NULL
);
184 mlt_pool_release( this->image_release
);
185 mlt_pool_release( this->alpha_release
);
186 this->image_release
= NULL
;
187 this->alpha_release
= NULL
;
192 else if ( this->image
== NULL
|| image_idx
!= this->image_idx
)
194 mlt_pool_release( this->image_release
);
195 mlt_pool_release( this->alpha_release
);
196 this->image_release
= NULL
;
197 this->alpha_release
= NULL
;
201 this->image_idx
= image_idx
;
202 pixbuf
= gdk_pixbuf_new_from_file( this->filenames
[ image_idx
], &error
);
204 if ( pixbuf
!= NULL
)
206 // Register this pixbuf for destruction and reuse
207 mlt_properties_set_data( producer_props
, "pixbuf", pixbuf
, 0, ( mlt_destructor
)g_object_unref
, NULL
);
209 mlt_properties_set_int( producer_props
, "real_width", gdk_pixbuf_get_width( pixbuf
) );
210 mlt_properties_set_int( producer_props
, "real_height", gdk_pixbuf_get_height( pixbuf
) );
212 // Store the width/height of the pixbuf temporarily
213 this->width
= gdk_pixbuf_get_width( pixbuf
);
214 this->height
= gdk_pixbuf_get_height( pixbuf
);
218 // If we have a pixbuf
219 if ( pixbuf
&& width
> 0 )
221 char *interps
= mlt_properties_get( properties
, "rescale.interp" );
222 int interp
= GDK_INTERP_BILINEAR
;
224 if ( strcmp( interps
, "nearest" ) == 0 )
225 interp
= GDK_INTERP_NEAREST
;
226 else if ( strcmp( interps
, "tiles" ) == 0 )
227 interp
= GDK_INTERP_TILES
;
228 else if ( strcmp( interps
, "hyper" ) == 0 )
229 interp
= GDK_INTERP_HYPER
;
231 // Note - the original pixbuf is already safe and ready for destruction
232 pixbuf
= gdk_pixbuf_scale_simple( pixbuf
, width
, height
, interp
);
234 // Store width and height
236 this->height
= height
;
238 // Allocate/define image
239 this->image
= mlt_pool_allocate( width
* ( height
+ 1 ) * 2, &this->image_release
);
241 // Extract YUV422 and alpha
242 if ( gdk_pixbuf_get_has_alpha( pixbuf
) )
244 // Allocate the alpha mask
245 this->alpha
= mlt_pool_allocate( this->width
* this->height
, &this->alpha_release
);
248 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf
),
249 this->width
, this->height
,
250 gdk_pixbuf_get_rowstride( pixbuf
),
251 this->image
, this->alpha
);
255 // No alpha to extract
256 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf
),
257 this->width
, this->height
,
258 gdk_pixbuf_get_rowstride( pixbuf
),
262 // Finished with pixbuf now
263 g_object_unref( pixbuf
);
266 // Set width/height of frame
267 mlt_properties_set_int( properties
, "width", this->width
);
268 mlt_properties_set_int( properties
, "height", this->height
);
269 mlt_properties_set_int( properties
, "real_width", mlt_properties_get_int( producer_props
, "real_width" ) );
270 mlt_properties_set_int( properties
, "real_height", mlt_properties_get_int( producer_props
, "real_height" ) );
272 // pass the image data without destructor
273 mlt_properties_set_data( properties
, "image", this->image
, this->width
* ( this->height
+ 1 ) * 2, NULL
, NULL
);
274 mlt_properties_set_data( properties
, "alpha", this->alpha
, this->width
* this->height
, NULL
, NULL
);
277 static int producer_get_image( mlt_frame frame
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
279 // Obtain properties of frame
280 mlt_properties properties
= mlt_frame_properties( frame
);
282 *width
= mlt_properties_get_int( properties
, "rescale_width" );
283 *height
= mlt_properties_get_int( properties
, "rescale_height" );
286 refresh_image( frame
, *width
, *height
);
289 //mlt_producer this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
290 //*format = ( mlt_properties_get_int( mlt_producer_properties( this ), "bpp" ) == 4 ) ? mlt_image_rgb24a : mlt_image_rgb24;
292 // May need to know the size of the image to clone it
296 uint8_t *image
= mlt_properties_get_data( properties
, "image", &size
);
298 // Get width and height
299 *width
= mlt_properties_get_int( properties
, "width" );
300 *height
= mlt_properties_get_int( properties
, "height" );
302 // Clone if necessary
303 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
304 // The fault is not in the design of mlt, but in the implementation of pixbuf...
308 void *release
= NULL
;
309 uint8_t *copy
= mlt_pool_allocate( size
, &release
);
310 memcpy( copy
, image
, size
);
312 // We're going to pass the copy on
315 // Now update properties so we free the copy after
316 mlt_properties_set_data( properties
, "image_release", release
, 0, mlt_pool_release
, NULL
);
317 mlt_properties_set_data( properties
, "image", copy
, size
, NULL
, NULL
);
326 static uint8_t *producer_get_alpha_mask( mlt_frame
this )
328 // Obtain properties of frame
329 mlt_properties properties
= mlt_frame_properties( this );
331 // Return the alpha mask
332 return mlt_properties_get_data( properties
, "alpha", NULL
);
335 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
337 // Get the real structure for this producer
338 producer_pixbuf
this = producer
->child
;
341 *frame
= mlt_frame_init( );
343 // Obtain properties of frame and producer
344 mlt_properties properties
= mlt_frame_properties( *frame
);
346 // Set the producer on the frame properties
347 mlt_properties_set_data( properties
, "producer_pixbuf", this, 0, NULL
, NULL
);
350 refresh_image( *frame
, 0, 0 );
352 // Set producer-specific frame properties
353 mlt_properties_set_int( properties
, "progressive", 1 );
354 mlt_properties_set_double( properties
, "aspect_ratio", mlt_properties_get_double( properties
, "real_width" ) / mlt_properties_get_double( properties
, "real_height" ) );
356 // Set alpha call back
357 ( *frame
)->get_alpha_mask
= producer_get_alpha_mask
;
359 // Push the get_image method
360 mlt_frame_push_get_image( *frame
, producer_get_image
);
362 // Calculate the next timecode
363 mlt_producer_prepare_next( producer
);
368 static void producer_close( mlt_producer parent
)
370 producer_pixbuf
this = parent
->child
;
372 parent
->close
= NULL
;
373 mlt_producer_close( parent
);