3cdd5ed6edbe6ad6086048a85ee6debc04b5d7d7
[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 mlt_properties_set_int( producer_props, "bpp", gdk_pixbuf_get_has_alpha( pixbuf ) ? 4 : 3 );
166 free( this->image );
167 free( this->alpha );
168 this->image = NULL;
169 this->alpha = NULL;
170 }
171 }
172 else if ( this->image == NULL || image_idx != this->image_idx )
173 {
174 free( this->image );
175 free( this->alpha );
176 this->image = NULL;
177 this->alpha = NULL;
178
179 this->image_idx = image_idx;
180 pixbuf = gdk_pixbuf_new_from_file( this->filenames[ image_idx ], &error );
181
182 if ( pixbuf != NULL )
183 {
184 // Register this pixbuf for destruction and reuse
185 mlt_properties_set_data( producer_props, "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
186
187 mlt_properties_set_int( producer_props, "real_width", gdk_pixbuf_get_width( pixbuf ) );
188 mlt_properties_set_int( producer_props, "real_height", gdk_pixbuf_get_height( pixbuf ) );
189
190 // Store the width/height of the pixbuf temporarily
191 this->width = gdk_pixbuf_get_width( pixbuf );
192 this->height = gdk_pixbuf_get_height( pixbuf );
193
194 mlt_properties_set_int( producer_props, "bpp", gdk_pixbuf_get_has_alpha( pixbuf ) ? 4 : 3 );
195 }
196 }
197
198 int bpp = mlt_properties_get_int( producer_props, "bpp" );
199
200 // If we have a pixbuf
201 if ( pixbuf && width > 0 )
202 {
203
204 // Note - the original pixbuf is already safe and ready for destruction
205 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, GDK_INTERP_NEAREST );
206
207 // Store width and height
208 this->width = width;
209 this->height = height;
210
211 //fprintf( stderr, "SCALING PIXBUF from %dx%d to %dx%d %dx%d\n", gdk_pixbuf_get_width( pixbuf ), gdk_pixbuf_get_height( pixbuf ), width, height, this->width, this->height );
212 // Allocate/define image
213 // IRRIGATE ME
214 uint8_t *image = malloc( width * ( height + 1 ) * bpp );
215 uint8_t *alpha = NULL;
216
217 // Extract YUV422 and alpha
218 if ( gdk_pixbuf_get_has_alpha( pixbuf ) )
219 {
220 // Allocate the alpha mask
221 alpha = malloc( this->width * this->height );
222
223 // Convert the image
224 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
225 this->width, this->height,
226 gdk_pixbuf_get_rowstride( pixbuf ),
227 image, alpha );
228 }
229 else
230 {
231 // No alpha to extract
232 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
233 this->width, this->height,
234 gdk_pixbuf_get_rowstride( pixbuf ),
235 image );
236 }
237
238 // Finished with pixbuf now
239 g_object_unref( pixbuf );
240
241 // Assign images to producer
242 this->image = image;
243 this->alpha = alpha;
244 }
245
246 // Set width/height of frame
247 mlt_properties_set_int( properties, "width", this->width );
248 mlt_properties_set_int( properties, "height", this->height );
249 mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "real_width" ) );
250 mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "real_height" ) );
251
252 // pass the image data without destructor
253 mlt_properties_set_data( properties, "image", this->image, this->width * ( this->height + 1 ) * 2, NULL, NULL );
254 mlt_properties_set_data( properties, "alpha", this->alpha, this->width * this->height, NULL, NULL );
255 }
256
257 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
258 {
259 // Obtain properties of frame
260 mlt_properties properties = mlt_frame_properties( frame );
261
262 *width = mlt_properties_get_int( properties, "rescale_width" );
263 *height = mlt_properties_get_int( properties, "rescale_height" );
264
265 // Refresh the image
266 refresh_image( frame, *width, *height );
267
268 // Determine format
269 //mlt_producer this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
270 //*format = ( mlt_properties_get_int( mlt_producer_properties( this ), "bpp" ) == 4 ) ? mlt_image_rgb24a : mlt_image_rgb24;
271
272 // May need to know the size of the image to clone it
273 int size = 0;
274
275 // Get the image
276 uint8_t *image = mlt_properties_get_data( properties, "image", &size );
277
278 // Get width and height
279 *width = mlt_properties_get_int( properties, "width" );
280 *height = mlt_properties_get_int( properties, "height" );
281
282 // Clone if necessary
283 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
284 // The fault is not in the design of mlt, but in the implementation of pixbuf...
285 //if ( writable )
286 {
287 // Clone our image
288 // IRRIGATE ME
289 uint8_t *copy = malloc( size );
290 memcpy( copy, image, size );
291
292 // We're going to pass the copy on
293 image = copy;
294
295 // Now update properties so we free the copy after
296 mlt_properties_set_data( properties, "image", copy, size, free, NULL );
297 }
298
299 // Pass on the image
300 *buffer = image;
301
302 return 0;
303 }
304
305 static uint8_t *producer_get_alpha_mask( mlt_frame this )
306 {
307 // Obtain properties of frame
308 mlt_properties properties = mlt_frame_properties( this );
309
310 // Return the alpha mask
311 return mlt_properties_get_data( properties, "alpha", NULL );
312 }
313
314 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
315 {
316 // Get the real structure for this producer
317 producer_pixbuf this = producer->child;
318
319 // Generate a frame
320 *frame = mlt_frame_init( );
321
322 // Obtain properties of frame and producer
323 mlt_properties properties = mlt_frame_properties( *frame );
324
325 // Set the producer on the frame properties
326 mlt_properties_set_data( properties, "producer_pixbuf", this, 0, NULL, NULL );
327
328 // Refresh the image
329 refresh_image( *frame, 0, 0 );
330
331 // Set producer-specific frame properties
332 mlt_properties_set_int( properties, "progressive", 1 );
333 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( properties, "real_width" ) / mlt_properties_get_double( properties, "real_height" ) );
334
335 // Set alpha call back
336 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
337
338 // Push the get_image method
339 mlt_frame_push_get_image( *frame, producer_get_image );
340
341 // Calculate the next timecode
342 mlt_producer_prepare_next( producer );
343
344 return 0;
345 }
346
347 static void producer_close( mlt_producer parent )
348 {
349 producer_pixbuf this = parent->child;
350 free( this->image );
351 parent->close = NULL;
352 mlt_producer_close( parent );
353 free( this );
354 }
355