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 ( filename
!= NULL
&& 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 );
81 if ( strchr( filename
, '%' ) != NULL
)
83 // handle picture sequences
91 snprintf( full
, 1023, filename
, i
++ );
92 if ( stat( full
, &buf
) == 0 )
94 this->filenames
= realloc( this->filenames
, sizeof( char * ) * ( this->count
+ 1 ) );
95 this->filenames
[ this->count
++ ] = strdup( full
);
103 mlt_properties_set_position( properties
, "out", this->count
* 250 );
105 else if ( strstr( filename
, "/.all." ) != NULL
)
107 char *dir_name
= strdup( filename
);
108 char *extension
= strrchr( filename
, '.' );
109 *( strstr( dir_name
, "/.all." ) + 1 ) = '\0';
110 char fullname
[ 1024 ];
111 strcpy( fullname
, dir_name
);
112 struct dirent
**de
= NULL
;
113 int n
= scandir( fullname
, &de
, filter_files
, alphasort
);
117 for (i
= 0; i
< n
; i
++ )
119 snprintf( fullname
, 1023, "%s%s", dir_name
, de
[i
]->d_name
);
121 if ( lstat( fullname
, &info
) == 0 &&
122 ( S_ISREG( info
.st_mode
) || ( strstr( fullname
, extension
) && info
.st_mode
| S_IXUSR
) ) )
124 this->filenames
= realloc( this->filenames
, sizeof( char * ) * ( this->count
+ 1 ) );
125 this->filenames
[ this->count
++ ] = strdup( fullname
);
135 this->filenames
= realloc( this->filenames
, sizeof( char * ) * ( this->count
+ 1 ) );
136 this->filenames
[ this->count
++ ] = strdup( filename
);
137 mlt_properties_set_position( properties
, "out", 250 );
140 // Initialise gobject types
149 static void refresh_image( mlt_frame frame
, int width
, int height
)
152 GdkPixbuf
*pixbuf
= NULL
;
153 GError
*error
= NULL
;
155 // Obtain properties of frame
156 mlt_properties properties
= mlt_frame_properties( frame
);
158 // Obtain the producer for this frame
159 producer_pixbuf
this = mlt_properties_get_data( properties
, "producer_pixbuf", NULL
);
161 // Obtain the producer
162 mlt_producer producer
= &this->parent
;
164 // Obtain properties of producer
165 mlt_properties producer_props
= mlt_producer_properties( producer
);
167 // Get the time to live for each frame
168 double ttl
= mlt_properties_get_int( producer_props
, "ttl" );
171 int image_idx
= ( int )floor( mlt_producer_position( producer
) / ttl
) % this->count
;
173 // Update timecode on the frame we're creating
174 mlt_frame_set_position( frame
, mlt_producer_position( producer
) );
176 // optimization for subsequent iterations on single picture
177 if ( width
!= 0 && this->image
!= NULL
&& image_idx
== this->image_idx
)
179 if ( width
!= this->width
|| height
!= this->height
)
181 pixbuf
= mlt_properties_get_data( producer_props
, "pixbuf", NULL
);
182 mlt_pool_release( this->image
);
183 mlt_pool_release( this->alpha
);
188 else if ( this->image
== NULL
|| image_idx
!= this->image_idx
)
190 mlt_pool_release( this->image
);
191 mlt_pool_release( this->alpha
);
195 this->image_idx
= image_idx
;
196 pixbuf
= gdk_pixbuf_new_from_file( this->filenames
[ image_idx
], &error
);
198 if ( pixbuf
!= NULL
)
200 // Register this pixbuf for destruction and reuse
201 mlt_properties_set_data( producer_props
, "pixbuf", pixbuf
, 0, ( mlt_destructor
)g_object_unref
, NULL
);
203 mlt_properties_set_int( producer_props
, "real_width", gdk_pixbuf_get_width( pixbuf
) );
204 mlt_properties_set_int( producer_props
, "real_height", gdk_pixbuf_get_height( pixbuf
) );
206 // Store the width/height of the pixbuf temporarily
207 this->width
= gdk_pixbuf_get_width( pixbuf
);
208 this->height
= gdk_pixbuf_get_height( pixbuf
);
212 // If we have a pixbuf
213 if ( pixbuf
&& width
> 0 )
215 char *interps
= mlt_properties_get( properties
, "rescale.interp" );
216 int interp
= GDK_INTERP_BILINEAR
;
218 if ( strcmp( interps
, "nearest" ) == 0 )
219 interp
= GDK_INTERP_NEAREST
;
220 else if ( strcmp( interps
, "tiles" ) == 0 )
221 interp
= GDK_INTERP_TILES
;
222 else if ( strcmp( interps
, "hyper" ) == 0 )
223 interp
= GDK_INTERP_HYPER
;
225 // Note - the original pixbuf is already safe and ready for destruction
226 pixbuf
= gdk_pixbuf_scale_simple( pixbuf
, width
, height
, interp
);
228 // Store width and height
230 this->height
= height
;
232 // Allocate/define image
233 this->image
= mlt_pool_alloc( width
* ( height
+ 1 ) * 2 );
235 // Extract YUV422 and alpha
236 if ( gdk_pixbuf_get_has_alpha( pixbuf
) )
238 // Allocate the alpha mask
239 this->alpha
= mlt_pool_alloc( this->width
* this->height
);
242 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf
),
243 this->width
, this->height
,
244 gdk_pixbuf_get_rowstride( pixbuf
),
245 this->image
, this->alpha
);
249 // No alpha to extract
250 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf
),
251 this->width
, this->height
,
252 gdk_pixbuf_get_rowstride( pixbuf
),
256 // Finished with pixbuf now
257 g_object_unref( pixbuf
);
260 // Set width/height of frame
261 mlt_properties_set_int( properties
, "width", this->width
);
262 mlt_properties_set_int( properties
, "height", this->height
);
263 mlt_properties_set_int( properties
, "real_width", mlt_properties_get_int( producer_props
, "real_width" ) );
264 mlt_properties_set_int( properties
, "real_height", mlt_properties_get_int( producer_props
, "real_height" ) );
266 // pass the image data without destructor
267 mlt_properties_set_data( properties
, "image", this->image
, this->width
* ( this->height
+ 1 ) * 2, NULL
, NULL
);
268 mlt_properties_set_data( properties
, "alpha", this->alpha
, this->width
* this->height
, NULL
, NULL
);
271 static int producer_get_image( mlt_frame frame
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
273 // Obtain properties of frame
274 mlt_properties properties
= mlt_frame_properties( frame
);
276 *width
= mlt_properties_get_int( properties
, "rescale_width" );
277 *height
= mlt_properties_get_int( properties
, "rescale_height" );
280 refresh_image( frame
, *width
, *height
);
283 //mlt_producer this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
284 //*format = ( mlt_properties_get_int( mlt_producer_properties( this ), "bpp" ) == 4 ) ? mlt_image_rgb24a : mlt_image_rgb24;
286 // May need to know the size of the image to clone it
290 uint8_t *image
= mlt_properties_get_data( properties
, "image", &size
);
292 // Get width and height
293 *width
= mlt_properties_get_int( properties
, "width" );
294 *height
= mlt_properties_get_int( properties
, "height" );
296 // Clone if necessary
297 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
298 // The fault is not in the design of mlt, but in the implementation of pixbuf...
302 uint8_t *copy
= mlt_pool_alloc( size
);
303 memcpy( copy
, image
, size
);
305 // We're going to pass the copy on
308 // Now update properties so we free the copy after
309 mlt_properties_set_data( properties
, "image", copy
, size
, mlt_pool_release
, NULL
);
318 static uint8_t *producer_get_alpha_mask( mlt_frame
this )
320 // Obtain properties of frame
321 mlt_properties properties
= mlt_frame_properties( this );
323 // Return the alpha mask
324 return mlt_properties_get_data( properties
, "alpha", NULL
);
327 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
329 // Get the real structure for this producer
330 producer_pixbuf
this = producer
->child
;
333 *frame
= mlt_frame_init( );
335 // Obtain properties of frame and producer
336 mlt_properties properties
= mlt_frame_properties( *frame
);
338 // Set the producer on the frame properties
339 mlt_properties_set_data( properties
, "producer_pixbuf", this, 0, NULL
, NULL
);
342 refresh_image( *frame
, 0, 0 );
344 // Set producer-specific frame properties
345 mlt_properties_set_int( properties
, "progressive", 1 );
346 mlt_properties_set_double( properties
, "aspect_ratio", mlt_properties_get_double( properties
, "real_width" ) / mlt_properties_get_double( properties
, "real_height" ) );
348 // Set alpha call back
349 ( *frame
)->get_alpha_mask
= producer_get_alpha_mask
;
351 // Push the get_image method
352 mlt_frame_push_get_image( *frame
, producer_get_image
);
354 // Calculate the next timecode
355 mlt_producer_prepare_next( producer
);
360 static void producer_close( mlt_producer parent
)
362 producer_pixbuf
this = parent
->child
;
363 mlt_pool_release( this->image
);
364 parent
->close
= NULL
;
365 mlt_producer_close( parent
);