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
);
43 static int filter_files( const struct dirent
*de
)
45 if ( de
->d_name
[ 0 ] != '.' )
52 mlt_producer
producer_pixbuf_init( const char *filename
)
54 producer_pixbuf
this = calloc( sizeof( struct producer_pixbuf_s
), 1 );
55 if ( this != NULL
&& mlt_producer_init( &this->parent
, this ) == 0 )
57 mlt_producer producer
= &this->parent
;
59 producer
->get_frame
= producer_get_frame
;
60 producer
->close
= producer_close
;
62 // Get the properties interface
63 mlt_properties properties
= mlt_producer_properties( &this->parent
);
65 // Set the default properties
66 mlt_properties_set_int( properties
, "video_standard", mlt_video_standard_pal
);
67 mlt_properties_set_double( properties
, "ttl", 5 );
70 if ( strchr( filename
, '%' ) != NULL
)
72 // handle picture sequences
80 snprintf( full
, 1023, filename
, i
++ );
81 if ( stat( full
, &buf
) == 0 )
83 this->filenames
= realloc( this->filenames
, sizeof( char * ) * ( this->count
+ 1 ) );
84 this->filenames
[ this->count
++ ] = strdup( full
);
93 else if ( strstr( filename
, "/.all." ) != NULL
)
95 char *dir_name
= strdup( filename
);
96 char *extension
= strrchr( filename
, '.' );
97 *( strstr( dir_name
, "/.all." ) + 1 ) = '\0';
98 char fullname
[ 1024 ];
99 strcpy( fullname
, dir_name
);
100 struct dirent
**de
= NULL
;
101 int n
= scandir( fullname
, &de
, filter_files
, alphasort
);
105 for (i
= 0; i
< n
; i
++ )
107 snprintf( fullname
, 1023, "%s%s", dir_name
, de
[i
]->d_name
);
109 if ( lstat( fullname
, &info
) == 0 &&
110 ( S_ISREG( info
.st_mode
) || ( strstr( fullname
, extension
) && info
.st_mode
| S_IXUSR
) ) )
112 this->filenames
= realloc( this->filenames
, sizeof( char * ) * ( this->count
+ 1 ) );
113 this->filenames
[ this->count
++ ] = strdup( fullname
);
123 this->filenames
= realloc( this->filenames
, sizeof( char * ) * ( this->count
+ 1 ) );
124 this->filenames
[ this->count
++ ] = strdup( filename
);
127 // Initialise gobject types
136 static int producer_get_image( mlt_frame
this, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
138 // Obtain properties of frame
139 mlt_properties properties
= mlt_frame_properties( this );
141 // May need to know the size of the image to clone it
145 uint8_t *image
= mlt_properties_get_data( properties
, "image", &size
);
147 // Get width and height
148 *width
= mlt_properties_get_int( properties
, "width" );
149 *height
= mlt_properties_get_int( properties
, "height" );
151 // Clone if necessary
152 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
153 // The fault is not in the design of mlt, but in the implementation of pixbuf...
156 size
= *width
* *height
* 2;
159 uint8_t *copy
= malloc( size
);
160 memcpy( copy
, image
, size
);
162 // We're going to pass the copy on
165 // Now update properties so we free the copy after
166 mlt_properties_set_data( properties
, "image", copy
, size
, free
, NULL
);
175 static uint8_t *producer_get_alpha_mask( mlt_frame
this )
177 // Obtain properties of frame
178 mlt_properties properties
= mlt_frame_properties( this );
180 // Return the alpha mask
181 return mlt_properties_get_data( properties
, "alpha", NULL
);
184 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
186 producer_pixbuf
this = producer
->child
;
187 GdkPixbuf
*pixbuf
= NULL
;
188 GError
*error
= NULL
;
191 *frame
= mlt_frame_init( );
193 // Obtain properties of frame
194 mlt_properties properties
= mlt_frame_properties( *frame
);
196 // Get the time to live for each frame
197 double ttl
= mlt_properties_get_double( mlt_producer_properties( producer
), "ttl" );
200 int image_idx
= ( int )floor( mlt_producer_position( producer
) / ttl
) % this->count
;
202 // Update timecode on the frame we're creating
203 mlt_frame_set_timecode( *frame
, mlt_producer_position( producer
) );
205 // optimization for subsequent iterations on single picture
206 if ( this->image
!= NULL
&& image_idx
== this->image_idx
)
209 mlt_properties_set_int( properties
, "width", this->width
);
210 mlt_properties_set_int( properties
, "height", this->height
);
212 // if picture sequence pass the image and alpha data without destructor
213 mlt_properties_set_data( properties
, "image", this->image
, 0, NULL
, NULL
);
214 mlt_properties_set_data( properties
, "alpha", this->alpha
, 0, NULL
, NULL
);
216 // Set alpha mask call back
217 ( *frame
)->get_alpha_mask
= producer_get_alpha_mask
;
219 // Stack the get image callback
220 mlt_frame_push_get_image( *frame
, producer_get_image
);
226 this->image_idx
= image_idx
;
227 pixbuf
= gdk_pixbuf_new_from_file( this->filenames
[ image_idx
], &error
);
230 // If we have a pixbuf
233 // Scale to adjust for sample aspect ratio
234 if ( mlt_properties_get_int( properties
, "video_standard" ) == mlt_video_standard_pal
)
236 GdkPixbuf
*temp
= pixbuf
;
237 GdkPixbuf
*scaled
= gdk_pixbuf_scale_simple( pixbuf
,
238 (gint
) ( (float) gdk_pixbuf_get_width( pixbuf
) * 54.0/59.0),
239 gdk_pixbuf_get_height( pixbuf
), GDK_INTERP_HYPER
);
241 g_object_unref( temp
);
245 GdkPixbuf
*temp
= pixbuf
;
246 GdkPixbuf
*scaled
= gdk_pixbuf_scale_simple( pixbuf
,
247 (gint
) ( (float) gdk_pixbuf_get_width( pixbuf
) * 11.0/10.0 ),
248 gdk_pixbuf_get_height( pixbuf
), GDK_INTERP_HYPER
);
250 g_object_unref( temp
);
253 // Store width and height
254 this->width
= gdk_pixbuf_get_width( pixbuf
);
255 this->height
= gdk_pixbuf_get_height( pixbuf
);
256 this->width
-= this->width
% 4;
257 this->height
-= this->height
% 2;
259 // Allocate/define image and alpha
260 uint8_t *image
= malloc( this->width
* this->height
* 2 );
261 uint8_t *alpha
= NULL
;
263 // Extract YUV422 and alpha
264 if ( gdk_pixbuf_get_has_alpha( pixbuf
) )
266 // Allocate the alpha mask
267 alpha
= malloc( this->width
* this->height
);
270 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf
),
271 this->width
, this->height
,
272 gdk_pixbuf_get_rowstride( pixbuf
),
277 // No alpha to extract
278 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf
),
279 this->width
, this->height
,
280 gdk_pixbuf_get_rowstride( pixbuf
),
284 // Finished with pixbuf now
285 g_object_unref( pixbuf
);
287 // Set width/height of frame
288 mlt_properties_set_int( properties
, "width", this->width
);
289 mlt_properties_set_int( properties
, "height", this->height
);
291 // Pass alpha and image on properties with or without destructor
295 // pass the image and alpha data without destructor
296 mlt_properties_set_data( properties
, "image", image
, 0, NULL
, NULL
);
297 mlt_properties_set_data( properties
, "alpha", alpha
, 0, NULL
, NULL
);
299 // Set alpha call back
300 ( *frame
)->get_alpha_mask
= producer_get_alpha_mask
;
302 // Push the get_image method
303 mlt_frame_push_get_image( *frame
, producer_get_image
);
306 // Calculate the next timecode
307 mlt_producer_prepare_next( producer
);
312 static void producer_close( mlt_producer parent
)
314 producer_pixbuf
this = parent
->child
;
319 parent
->close
= NULL
;
320 mlt_producer_close( parent
);