84ed6e64b09b0a2f0f5c05f98c89aa1f8af98408
[melted] / mlt / src / modules / gtk2 / producer_pixbuf.c
1 /*
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>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 #include "producer_pixbuf.h"
22 #include <framework/mlt_frame.h>
23 #include <gdk-pixbuf/gdk-pixbuf.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <math.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include <dirent.h>
33
34 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
35 static void producer_close( mlt_producer parent );
36
37 typedef enum
38 {
39 SIGNAL_FORMAT_PAL,
40 SIGNAL_FORMAT_NTSC
41 } mlt_signal_format;
42
43 static int filter_files( const struct dirent *de )
44 {
45 if ( de->d_name[ 0 ] != '.' )
46 return 1;
47 else
48 return 0;
49 }
50
51
52 mlt_producer producer_pixbuf_init( char *filename )
53 {
54 producer_pixbuf this = calloc( sizeof( struct producer_pixbuf_s ), 1 );
55 if ( filename != NULL && this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
56 {
57 mlt_producer producer = &this->parent;
58
59 producer->get_frame = producer_get_frame;
60 producer->close = producer_close;
61
62 // Get the properties interface
63 mlt_properties properties = mlt_producer_properties( &this->parent );
64
65 // Set the default properties
66 mlt_properties_set( properties, "resource", filename );
67 mlt_properties_set_int( properties, "video_standard", mlt_video_standard_pal );
68 mlt_properties_set_double( properties, "ttl", 5 );
69
70 // Obtain filenames
71 if ( strchr( filename, '%' ) != NULL )
72 {
73 // handle picture sequences
74 int i = 0;
75 int gap = 0;
76 char full[1024];
77
78 while ( gap < 100 )
79 {
80 struct stat buf;
81 snprintf( full, 1023, filename, i ++ );
82 if ( stat( full, &buf ) == 0 )
83 {
84 this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
85 this->filenames[ this->count ++ ] = strdup( full );
86 gap = 0;
87 }
88 else
89 {
90 gap ++;
91 }
92 }
93 mlt_properties_set_timecode( properties, "out", this->count );
94 }
95 else if ( strstr( filename, "/.all." ) != NULL )
96 {
97 char *dir_name = strdup( filename );
98 char *extension = strrchr( filename, '.' );
99 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
100 char fullname[ 1024 ];
101 strcpy( fullname, dir_name );
102 struct dirent **de = NULL;
103 int n = scandir( fullname, &de, filter_files, alphasort );
104 int i;
105 struct stat info;
106
107 for (i = 0; i < n; i++ )
108 {
109 snprintf( fullname, 1023, "%s%s", dir_name, de[i]->d_name );
110
111 if ( lstat( fullname, &info ) == 0 &&
112 ( S_ISREG( info.st_mode ) || ( strstr( fullname, extension ) && info.st_mode | S_IXUSR ) ) )
113 {
114 this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
115 this->filenames[ this->count ++ ] = strdup( fullname );
116 }
117 free( de[ i ] );
118 }
119
120 mlt_properties_set_timecode( properties, "out", this->count );
121 free( de );
122 free( dir_name );
123 }
124 else
125 {
126 this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
127 this->filenames[ this->count ++ ] = strdup( filename );
128 mlt_properties_set_timecode( properties, "out", 1 );
129 }
130
131 // Initialise gobject types
132 g_type_init();
133
134 return producer;
135 }
136 free( this );
137 return NULL;
138 }
139
140 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
141 {
142 // Obtain properties of frame
143 mlt_properties properties = mlt_frame_properties( this );
144
145 // May need to know the size of the image to clone it
146 int size = 0;
147
148 // Get the image
149 uint8_t *image = mlt_properties_get_data( properties, "image", &size );
150
151 // Get width and height
152 *width = mlt_properties_get_int( properties, "width" );
153 *height = mlt_properties_get_int( properties, "height" );
154
155 // Clone if necessary
156 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
157 // The fault is not in the design of mlt, but in the implementation of pixbuf...
158 //if ( writable )
159 {
160 size = *width * *height * 2;
161
162 // Clone our image
163 uint8_t *copy = malloc( size );
164 memcpy( copy, image, size );
165
166 // We're going to pass the copy on
167 image = copy;
168
169 // Now update properties so we free the copy after
170 mlt_properties_set_data( properties, "image", copy, size, free, NULL );
171 }
172
173 // Pass on the image
174 *buffer = image;
175
176 return 0;
177 }
178
179 static uint8_t *producer_get_alpha_mask( mlt_frame this )
180 {
181 // Obtain properties of frame
182 mlt_properties properties = mlt_frame_properties( this );
183
184 // Return the alpha mask
185 return mlt_properties_get_data( properties, "alpha", NULL );
186 }
187
188 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
189 {
190 producer_pixbuf this = producer->child;
191 GdkPixbuf *pixbuf = NULL;
192 GError *error = NULL;
193
194 // Generate a frame
195 *frame = mlt_frame_init( );
196
197 // Obtain properties of frame
198 mlt_properties properties = mlt_frame_properties( *frame );
199
200 // Obtain properties of producer
201 mlt_properties producer_props = mlt_producer_properties( producer );
202
203 // Get the time to live for each frame
204 double ttl = mlt_properties_get_double( producer_props, "ttl" );
205
206 // Image index
207 int image_idx = ( int )floor( mlt_producer_position( producer ) / ttl ) % this->count;
208
209 // Update timecode on the frame we're creating
210 mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
211
212 // optimization for subsequent iterations on single picture
213 if ( this->image != NULL && image_idx == this->image_idx )
214 {
215 // Set width/height
216 mlt_properties_set_int( properties, "width", this->width );
217 mlt_properties_set_int( properties, "height", this->height );
218
219 // Set the compositing properties
220 if ( mlt_properties_get( producer_props, "x" ) != NULL )
221 mlt_properties_set_int( properties, "x", mlt_properties_get_int( producer_props, "x" ) );
222 if ( mlt_properties_get( producer_props, "y" ) != NULL )
223 mlt_properties_set_int( properties, "y", mlt_properties_get_int( producer_props, "y" ) );
224 if ( mlt_properties_get( producer_props, "mix" ) != NULL )
225 mlt_properties_set_double( properties, "mix", mlt_properties_get_double( producer_props, "mix" ) );
226
227 // if picture sequence pass the image and alpha data without destructor
228 mlt_properties_set_data( properties, "image", this->image, 0, NULL, NULL );
229 mlt_properties_set_data( properties, "alpha", this->alpha, 0, NULL, NULL );
230
231 // Set alpha mask call back
232 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
233
234 // Stack the get image callback
235 mlt_frame_push_get_image( *frame, producer_get_image );
236 }
237 else
238 {
239 free( this->image );
240 free( this->alpha );
241 this->image_idx = image_idx;
242 pixbuf = gdk_pixbuf_new_from_file( this->filenames[ image_idx ], &error );
243 }
244
245 // If we have a pixbuf
246 if ( pixbuf )
247 {
248 // Scale to adjust for sample aspect ratio
249 if ( mlt_properties_get_int( properties, "video_standard" ) == mlt_video_standard_pal )
250 {
251 GdkPixbuf *temp = pixbuf;
252 GdkPixbuf *scaled = gdk_pixbuf_scale_simple( pixbuf,
253 (gint) ( (float) gdk_pixbuf_get_width( pixbuf ) * 54.0/59.0),
254 gdk_pixbuf_get_height( pixbuf ), GDK_INTERP_HYPER );
255 pixbuf = scaled;
256 g_object_unref( temp );
257 }
258 else
259 {
260 GdkPixbuf *temp = pixbuf;
261 GdkPixbuf *scaled = gdk_pixbuf_scale_simple( pixbuf,
262 (gint) ( (float) gdk_pixbuf_get_width( pixbuf ) * 11.0/10.0 ),
263 gdk_pixbuf_get_height( pixbuf ), GDK_INTERP_HYPER );
264 pixbuf = scaled;
265 g_object_unref( temp );
266 }
267
268 // Store width and height
269 this->width = gdk_pixbuf_get_width( pixbuf );
270 this->height = gdk_pixbuf_get_height( pixbuf );
271 this->width -= this->width % 4;
272 this->height -= this->height % 2;
273
274 // Allocate/define image and alpha
275 uint8_t *image = malloc( this->width * this->height * 2 );
276 uint8_t *alpha = NULL;
277
278 // Extract YUV422 and alpha
279 if ( gdk_pixbuf_get_has_alpha( pixbuf ) )
280 {
281 // Allocate the alpha mask
282 alpha = malloc( this->width * this->height );
283
284 // Convert the image
285 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
286 this->width, this->height,
287 gdk_pixbuf_get_rowstride( pixbuf ),
288 image, alpha );
289 }
290 else
291 {
292 // No alpha to extract
293 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
294 this->width, this->height,
295 gdk_pixbuf_get_rowstride( pixbuf ),
296 image );
297 }
298
299 // Finished with pixbuf now
300 g_object_unref( pixbuf );
301
302 // Set width/height of frame
303 mlt_properties_set_int( properties, "width", this->width );
304 mlt_properties_set_int( properties, "height", this->height );
305
306 // Set the compositing properties
307 if ( mlt_properties_get( producer_props, "x" ) != NULL )
308 mlt_properties_set_int( properties, "x", mlt_properties_get_int( producer_props, "x" ) );
309 if ( mlt_properties_get( producer_props, "y" ) != NULL )
310 mlt_properties_set_int( properties, "y", mlt_properties_get_int( producer_props, "y" ) );
311 if ( mlt_properties_get( producer_props, "mix" ) != NULL )
312 mlt_properties_set_double( properties, "mix", mlt_properties_get_double( producer_props, "mix" ) );
313
314 // Pass alpha and image on properties with or without destructor
315 this->image = image;
316 this->alpha = alpha;
317
318 // pass the image and alpha data without destructor
319 mlt_properties_set_data( properties, "image", image, this->width * this->height * 2, NULL, NULL );
320 mlt_properties_set_data( properties, "alpha", alpha, this->width * this->height, NULL, NULL );
321
322 // Set alpha call back
323 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
324
325 // Push the get_image method
326 mlt_frame_push_get_image( *frame, producer_get_image );
327 }
328
329 // Calculate the next timecode
330 mlt_producer_prepare_next( producer );
331
332 return 0;
333 }
334
335 static void producer_close( mlt_producer parent )
336 {
337 producer_pixbuf this = parent->child;
338 if ( this->image )
339 free( this->image );
340 if ( this->alpha )
341 free( this->alpha );
342 parent->close = NULL;
343 mlt_producer_close( parent );
344 free( this );
345 }
346