d77d73103b18b27ceb8214858c9ca8773c132920
[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 typedef struct producer_pixbuf_s *producer_pixbuf;
35
36 struct producer_pixbuf_s
37 {
38 struct mlt_producer_s parent;
39
40 // File name list
41 char **filenames;
42 int count;
43 int image_idx;
44
45 int width;
46 int height;
47 void *image_release;
48 uint8_t *image;
49 void *alpha_release;
50 uint8_t *alpha;
51 };
52
53 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
54 static void producer_close( mlt_producer parent );
55
56 static int filter_files( const struct dirent *de )
57 {
58 if ( de->d_name[ 0 ] != '.' )
59 return 1;
60 else
61 return 0;
62 }
63
64 mlt_producer producer_pixbuf_init( char *filename )
65 {
66 producer_pixbuf this = calloc( sizeof( struct producer_pixbuf_s ), 1 );
67 if ( filename != NULL && this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
68 {
69 mlt_producer producer = &this->parent;
70
71 // Get the properties interface
72 mlt_properties properties = mlt_producer_properties( &this->parent );
73
74 // Callback registration
75 producer->get_frame = producer_get_frame;
76 producer->close = producer_close;
77
78 // Set the default properties
79 mlt_properties_set( properties, "resource", filename );
80 mlt_properties_set_int( properties, "ttl", 25 );
81
82 // Obtain filenames
83 if ( strchr( filename, '%' ) != NULL )
84 {
85 // handle picture sequences
86 int i = 0;
87 int gap = 0;
88 char full[1024];
89
90 while ( gap < 100 )
91 {
92 struct stat buf;
93 snprintf( full, 1023, filename, i ++ );
94 if ( stat( full, &buf ) == 0 )
95 {
96 this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
97 this->filenames[ this->count ++ ] = strdup( full );
98 gap = 0;
99 }
100 else
101 {
102 gap ++;
103 }
104 }
105 mlt_properties_set_position( properties, "out", this->count * 250 );
106 }
107 else if ( strstr( filename, "/.all." ) != NULL )
108 {
109 char *dir_name = strdup( filename );
110 char *extension = strrchr( filename, '.' );
111 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
112 char fullname[ 1024 ];
113 strcpy( fullname, dir_name );
114 struct dirent **de = NULL;
115 int n = scandir( fullname, &de, filter_files, alphasort );
116 int i;
117 struct stat info;
118
119 for (i = 0; i < n; i++ )
120 {
121 snprintf( fullname, 1023, "%s%s", dir_name, de[i]->d_name );
122
123 if ( lstat( fullname, &info ) == 0 &&
124 ( S_ISREG( info.st_mode ) || ( strstr( fullname, extension ) && info.st_mode | S_IXUSR ) ) )
125 {
126 this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
127 this->filenames[ this->count ++ ] = strdup( fullname );
128 }
129 free( de[ i ] );
130 }
131
132 free( de );
133 free( dir_name );
134 }
135 else
136 {
137 this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
138 this->filenames[ this->count ++ ] = strdup( filename );
139 mlt_properties_set_position( properties, "out", 250 );
140 }
141
142 // Initialise gobject types
143 g_type_init();
144
145 return producer;
146 }
147 free( this );
148 return NULL;
149 }
150
151 static void refresh_image( mlt_frame frame, int width, int height )
152 {
153 // Pixbuf
154 GdkPixbuf *pixbuf = NULL;
155 GError *error = NULL;
156
157 // Obtain properties of frame
158 mlt_properties properties = mlt_frame_properties( frame );
159
160 // Obtain the producer for this frame
161 producer_pixbuf this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
162
163 // Obtain the producer
164 mlt_producer producer = &this->parent;
165
166 // Obtain properties of producer
167 mlt_properties producer_props = mlt_producer_properties( producer );
168
169 // Get the time to live for each frame
170 double ttl = mlt_properties_get_int( producer_props, "ttl" );
171
172 // Image index
173 int image_idx = ( int )floor( mlt_producer_position( producer ) / ttl ) % this->count;
174
175 // Update timecode on the frame we're creating
176 mlt_frame_set_position( frame, mlt_producer_position( producer ) );
177
178 // optimization for subsequent iterations on single picture
179 if ( width != 0 && this->image != NULL && image_idx == this->image_idx )
180 {
181 if ( width != this->width || height != this->height )
182 {
183 pixbuf = mlt_properties_get_data( producer_props, "pixbuf", NULL );
184 mlt_pool_release( this->image_release );
185 mlt_pool_release( this->alpha_release );
186 this->image_release = NULL;
187 this->alpha_release = NULL;
188 this->image = NULL;
189 this->alpha = NULL;
190 }
191 }
192 else if ( this->image == NULL || image_idx != this->image_idx )
193 {
194 mlt_pool_release( this->image_release );
195 mlt_pool_release( this->alpha_release );
196 this->image_release = NULL;
197 this->alpha_release = NULL;
198 this->image = NULL;
199 this->alpha = NULL;
200
201 this->image_idx = image_idx;
202 pixbuf = gdk_pixbuf_new_from_file( this->filenames[ image_idx ], &error );
203
204 if ( pixbuf != NULL )
205 {
206 // Register this pixbuf for destruction and reuse
207 mlt_properties_set_data( producer_props, "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
208
209 mlt_properties_set_int( producer_props, "real_width", gdk_pixbuf_get_width( pixbuf ) );
210 mlt_properties_set_int( producer_props, "real_height", gdk_pixbuf_get_height( pixbuf ) );
211
212 // Store the width/height of the pixbuf temporarily
213 this->width = gdk_pixbuf_get_width( pixbuf );
214 this->height = gdk_pixbuf_get_height( pixbuf );
215 }
216 }
217
218 // If we have a pixbuf
219 if ( pixbuf && width > 0 )
220 {
221 char *interps = mlt_properties_get( properties, "rescale.interp" );
222 int interp = GDK_INTERP_BILINEAR;
223
224 if ( strcmp( interps, "nearest" ) == 0 )
225 interp = GDK_INTERP_NEAREST;
226 else if ( strcmp( interps, "tiles" ) == 0 )
227 interp = GDK_INTERP_TILES;
228 else if ( strcmp( interps, "hyper" ) == 0 )
229 interp = GDK_INTERP_HYPER;
230
231 // Note - the original pixbuf is already safe and ready for destruction
232 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
233
234 // Store width and height
235 this->width = width;
236 this->height = height;
237
238 // Allocate/define image
239 this->image = mlt_pool_allocate( width * ( height + 1 ) * 2, &this->image_release );
240
241 // Extract YUV422 and alpha
242 if ( gdk_pixbuf_get_has_alpha( pixbuf ) )
243 {
244 // Allocate the alpha mask
245 this->alpha = mlt_pool_allocate( this->width * this->height, &this->alpha_release );
246
247 // Convert the image
248 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
249 this->width, this->height,
250 gdk_pixbuf_get_rowstride( pixbuf ),
251 this->image, this->alpha );
252 }
253 else
254 {
255 // No alpha to extract
256 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
257 this->width, this->height,
258 gdk_pixbuf_get_rowstride( pixbuf ),
259 this->image );
260 }
261
262 // Finished with pixbuf now
263 g_object_unref( pixbuf );
264 }
265
266 // Set width/height of frame
267 mlt_properties_set_int( properties, "width", this->width );
268 mlt_properties_set_int( properties, "height", this->height );
269 mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "real_width" ) );
270 mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "real_height" ) );
271
272 // pass the image data without destructor
273 mlt_properties_set_data( properties, "image", this->image, this->width * ( this->height + 1 ) * 2, NULL, NULL );
274 mlt_properties_set_data( properties, "alpha", this->alpha, this->width * this->height, NULL, NULL );
275 }
276
277 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
278 {
279 // Obtain properties of frame
280 mlt_properties properties = mlt_frame_properties( frame );
281
282 *width = mlt_properties_get_int( properties, "rescale_width" );
283 *height = mlt_properties_get_int( properties, "rescale_height" );
284
285 // Refresh the image
286 refresh_image( frame, *width, *height );
287
288 // Determine format
289 //mlt_producer this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
290 //*format = ( mlt_properties_get_int( mlt_producer_properties( this ), "bpp" ) == 4 ) ? mlt_image_rgb24a : mlt_image_rgb24;
291
292 // May need to know the size of the image to clone it
293 int size = 0;
294
295 // Get the image
296 uint8_t *image = mlt_properties_get_data( properties, "image", &size );
297
298 // Get width and height
299 *width = mlt_properties_get_int( properties, "width" );
300 *height = mlt_properties_get_int( properties, "height" );
301
302 // Clone if necessary
303 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
304 // The fault is not in the design of mlt, but in the implementation of pixbuf...
305 //if ( writable )
306 {
307 // Clone our image
308 void *release = NULL;
309 uint8_t *copy = mlt_pool_allocate( size, &release );
310 memcpy( copy, image, size );
311
312 // We're going to pass the copy on
313 image = copy;
314
315 // Now update properties so we free the copy after
316 mlt_properties_set_data( properties, "image_release", release, 0, mlt_pool_release, NULL );
317 mlt_properties_set_data( properties, "image", copy, size, NULL, NULL );
318 }
319
320 // Pass on the image
321 *buffer = image;
322
323 return 0;
324 }
325
326 static uint8_t *producer_get_alpha_mask( mlt_frame this )
327 {
328 // Obtain properties of frame
329 mlt_properties properties = mlt_frame_properties( this );
330
331 // Return the alpha mask
332 return mlt_properties_get_data( properties, "alpha", NULL );
333 }
334
335 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
336 {
337 // Get the real structure for this producer
338 producer_pixbuf this = producer->child;
339
340 // Generate a frame
341 *frame = mlt_frame_init( );
342
343 // Obtain properties of frame and producer
344 mlt_properties properties = mlt_frame_properties( *frame );
345
346 // Set the producer on the frame properties
347 mlt_properties_set_data( properties, "producer_pixbuf", this, 0, NULL, NULL );
348
349 // Refresh the image
350 refresh_image( *frame, 0, 0 );
351
352 // Set producer-specific frame properties
353 mlt_properties_set_int( properties, "progressive", 1 );
354 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( properties, "real_width" ) / mlt_properties_get_double( properties, "real_height" ) );
355
356 // Set alpha call back
357 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
358
359 // Push the get_image method
360 mlt_frame_push_get_image( *frame, producer_get_image );
361
362 // Calculate the next timecode
363 mlt_producer_prepare_next( producer );
364
365 return 0;
366 }
367
368 static void producer_close( mlt_producer parent )
369 {
370 producer_pixbuf this = parent->child;
371 free( this->image );
372 parent->close = NULL;
373 mlt_producer_close( parent );
374 free( this );
375 }
376