field rendering and alignment for composite, bugfixes for luma, pixbuf and pango
[melted] / 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 static int filter_files( const struct dirent *de )
38 {
39 if ( de->d_name[ 0 ] != '.' )
40 return 1;
41 else
42 return 0;
43 }
44
45 mlt_producer producer_pixbuf_init( char *filename )
46 {
47 producer_pixbuf this = calloc( sizeof( struct producer_pixbuf_s ), 1 );
48 if ( filename != NULL && this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
49 {
50 mlt_producer producer = &this->parent;
51
52 // Get the properties interface
53 mlt_properties properties = mlt_producer_properties( &this->parent );
54
55 // Callback registration
56 producer->get_frame = producer_get_frame;
57 producer->close = producer_close;
58
59 // Set the default properties
60 mlt_properties_set( properties, "resource", filename );
61 mlt_properties_set_int( properties, "ttl", 25 );
62
63 // Obtain filenames
64 if ( strchr( filename, '%' ) != NULL )
65 {
66 // handle picture sequences
67 int i = 0;
68 int gap = 0;
69 char full[1024];
70
71 while ( gap < 100 )
72 {
73 struct stat buf;
74 snprintf( full, 1023, filename, i ++ );
75 if ( stat( full, &buf ) == 0 )
76 {
77 this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
78 this->filenames[ this->count ++ ] = strdup( full );
79 gap = 0;
80 }
81 else
82 {
83 gap ++;
84 }
85 }
86 mlt_properties_set_position( properties, "out", this->count * 250 );
87 }
88 else if ( strstr( filename, "/.all." ) != NULL )
89 {
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 );
97 int i;
98 struct stat info;
99
100 for (i = 0; i < n; i++ )
101 {
102 snprintf( fullname, 1023, "%s%s", dir_name, de[i]->d_name );
103
104 if ( lstat( fullname, &info ) == 0 &&
105 ( S_ISREG( info.st_mode ) || ( strstr( fullname, extension ) && info.st_mode | S_IXUSR ) ) )
106 {
107 this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
108 this->filenames[ this->count ++ ] = strdup( fullname );
109 }
110 free( de[ i ] );
111 }
112
113 free( de );
114 free( dir_name );
115 }
116 else
117 {
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 );
121 }
122
123 // Initialise gobject types
124 g_type_init();
125
126 return producer;
127 }
128 free( this );
129 return NULL;
130 }
131
132 static void refresh_image( mlt_frame frame, int width, int height )
133 {
134 // Pixbuf
135 GdkPixbuf *pixbuf = NULL;
136 GError *error = NULL;
137
138 // Obtain properties of frame
139 mlt_properties properties = mlt_frame_properties( frame );
140
141 // Obtain the producer for this frame
142 producer_pixbuf this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
143
144 // Obtain the producer
145 mlt_producer producer = &this->parent;
146
147 // Obtain properties of producer
148 mlt_properties producer_props = mlt_producer_properties( producer );
149
150 // Get the time to live for each frame
151 double ttl = mlt_properties_get_int( producer_props, "ttl" );
152
153 // Image index
154 int image_idx = ( int )floor( mlt_producer_position( producer ) / ttl ) % this->count;
155
156 // Update timecode on the frame we're creating
157 mlt_frame_set_position( frame, mlt_producer_position( producer ) );
158
159 // optimization for subsequent iterations on single picture
160 if ( width != 0 && this->image != NULL && image_idx == this->image_idx )
161 {
162 if ( width != this->width || height != this->height )
163 {
164 pixbuf = mlt_properties_get_data( producer_props, "pixbuf", NULL );
165 free( this->image );
166 free( this->alpha );
167 this->image = NULL;
168 this->alpha = NULL;
169 }
170 }
171 else if ( this->image == NULL || image_idx != this->image_idx )
172 {
173 free( this->image );
174 free( this->alpha );
175 this->image = NULL;
176 this->alpha = NULL;
177
178 this->image_idx = image_idx;
179 pixbuf = gdk_pixbuf_new_from_file( this->filenames[ image_idx ], &error );
180
181 if ( pixbuf != NULL )
182 {
183 // Register this pixbuf for destruction and reuse
184 mlt_properties_set_data( producer_props, "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
185
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 ) );
188
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 );
192 }
193 }
194
195 // If we have a pixbuf
196 if ( pixbuf && width > 0 )
197 {
198 char *interps = mlt_properties_get( properties, "rescale.interp" );
199 int interp = GDK_INTERP_BILINEAR;
200
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;
207
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 );
209
210 // Note - the original pixbuf is already safe and ready for destruction
211 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
212
213 // Store width and height
214 this->width = width;
215 this->height = height;
216
217 // Allocate/define image
218 // IRRIGATE ME
219 uint8_t *image = malloc( width * ( height + 1 ) * 2 );
220 uint8_t *alpha = NULL;
221
222 // Extract YUV422 and alpha
223 if ( gdk_pixbuf_get_has_alpha( pixbuf ) )
224 {
225 // Allocate the alpha mask
226 alpha = malloc( this->width * this->height );
227
228 // Convert the image
229 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
230 this->width, this->height,
231 gdk_pixbuf_get_rowstride( pixbuf ),
232 image, alpha );
233 }
234 else
235 {
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 ),
240 image );
241 }
242
243 // Finished with pixbuf now
244 g_object_unref( pixbuf );
245
246 // Assign images to producer
247 this->image = image;
248 this->alpha = alpha;
249 }
250
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" ) );
256
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 );
260 }
261
262 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
263 {
264 // Obtain properties of frame
265 mlt_properties properties = mlt_frame_properties( frame );
266
267 *width = mlt_properties_get_int( properties, "rescale_width" );
268 *height = mlt_properties_get_int( properties, "rescale_height" );
269
270 // Refresh the image
271 refresh_image( frame, *width, *height );
272
273 // Determine format
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;
276
277 // May need to know the size of the image to clone it
278 int size = 0;
279
280 // Get the image
281 uint8_t *image = mlt_properties_get_data( properties, "image", &size );
282
283 // Get width and height
284 *width = mlt_properties_get_int( properties, "width" );
285 *height = mlt_properties_get_int( properties, "height" );
286
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...
290 //if ( writable )
291 {
292 // Clone our image
293 // IRRIGATE ME
294 uint8_t *copy = malloc( size );
295 memcpy( copy, image, size );
296
297 // We're going to pass the copy on
298 image = copy;
299
300 // Now update properties so we free the copy after
301 mlt_properties_set_data( properties, "image", copy, size, free, NULL );
302 }
303
304 // Pass on the image
305 *buffer = image;
306
307 return 0;
308 }
309
310 static uint8_t *producer_get_alpha_mask( mlt_frame this )
311 {
312 // Obtain properties of frame
313 mlt_properties properties = mlt_frame_properties( this );
314
315 // Return the alpha mask
316 return mlt_properties_get_data( properties, "alpha", NULL );
317 }
318
319 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
320 {
321 // Get the real structure for this producer
322 producer_pixbuf this = producer->child;
323
324 // Generate a frame
325 *frame = mlt_frame_init( );
326
327 // Obtain properties of frame and producer
328 mlt_properties properties = mlt_frame_properties( *frame );
329
330 // Set the producer on the frame properties
331 mlt_properties_set_data( properties, "producer_pixbuf", this, 0, NULL, NULL );
332
333 // Refresh the image
334 refresh_image( *frame, 0, 0 );
335
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" ) );
339
340 // Set alpha call back
341 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
342
343 // Push the get_image method
344 mlt_frame_push_get_image( *frame, producer_get_image );
345
346 // Calculate the next timecode
347 mlt_producer_prepare_next( producer );
348
349 return 0;
350 }
351
352 static void producer_close( mlt_producer parent )
353 {
354 producer_pixbuf this = parent->child;
355 free( this->image );
356 parent->close = NULL;
357 mlt_producer_close( parent );
358 free( this );
359 }
360