699550d92d31b9286c0f2112466c36e7b60b1cf4
[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 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_int( properties, "ttl", 25 );
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_position( properties, "out", this->count * 25 );
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 free( de );
121 free( dir_name );
122 }
123 else
124 {
125 this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
126 this->filenames[ this->count ++ ] = strdup( filename );
127 mlt_properties_set_position( properties, "out", 25 );
128 }
129
130 // Initialise gobject types
131 g_type_init();
132
133 return producer;
134 }
135 free( this );
136 return NULL;
137 }
138
139 static void refresh_image( mlt_frame frame, int width, int height )
140 {
141 // Pixbuf
142 GdkPixbuf *pixbuf = NULL;
143 GError *error = NULL;
144
145 // Obtain properties of frame
146 mlt_properties properties = mlt_frame_properties( frame );
147
148 // Obtain the producer pango for this frame
149 producer_pixbuf this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
150
151 // Obtain the producer
152 mlt_producer producer = &this->parent;
153
154 // Obtain properties of producer
155 mlt_properties producer_props = mlt_producer_properties( producer );
156
157 // Get the time to live for each frame
158 double ttl = mlt_properties_get_int( producer_props, "ttl" );
159
160 // Image index
161 int image_idx = ( int )floor( mlt_producer_position( producer ) / ttl ) % this->count;
162
163 // Update timecode on the frame we're creating
164 mlt_frame_set_position( frame, mlt_producer_position( producer ) );
165
166 // optimization for subsequent iterations on single picture
167 if ( this->image != NULL && image_idx == this->image_idx )
168 {
169 if ( width != this->width || height != this->height )
170 {
171 pixbuf = mlt_properties_get_data( producer_props, "pixbuf", NULL );
172 }
173 }
174 else
175 {
176 free( this->image );
177 this->image = NULL;
178 free( this->alpha );
179 this->alpha = NULL;
180 this->image_idx = image_idx;
181 pixbuf = gdk_pixbuf_new_from_file( this->filenames[ image_idx ], &error );
182
183 if ( pixbuf != NULL )
184 {
185 // Register this pixbuf for destruction and reuse
186 mlt_properties_set_data( producer_props, "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
187
188 mlt_properties_set_int( producer_props, "real_width", gdk_pixbuf_get_width( pixbuf ) );
189 mlt_properties_set_int( producer_props, "real_height", gdk_pixbuf_get_height( pixbuf ) );
190
191 // Store the width/height of the pixbuf temporarily
192 this->width = gdk_pixbuf_get_width( pixbuf );
193 this->height = gdk_pixbuf_get_height( pixbuf );
194 }
195 }
196
197 // If we have a pixbuf
198 if ( pixbuf && width > 0 )
199 {
200 // Note - the original pixbuf is already safe and ready for destruction
201 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, GDK_INTERP_HYPER );
202
203 // Store width and height
204 this->width = gdk_pixbuf_get_width( pixbuf );
205 this->height = gdk_pixbuf_get_height( pixbuf );
206
207 // Allocate/define image and alpha
208 uint8_t *image = malloc( this->width * this->height * 2 );
209 uint8_t *alpha = NULL;
210
211 // Extract YUV422 and alpha
212 if ( gdk_pixbuf_get_has_alpha( pixbuf ) )
213 {
214 // Allocate the alpha mask
215 alpha = malloc( this->width * this->height );
216
217 // Convert the image
218 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
219 this->width, this->height,
220 gdk_pixbuf_get_rowstride( pixbuf ),
221 image, alpha );
222 }
223 else
224 {
225 // No alpha to extract
226 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
227 this->width, this->height,
228 gdk_pixbuf_get_rowstride( pixbuf ),
229 image );
230 }
231
232 // Finished with pixbuf now
233 g_object_unref( pixbuf );
234
235 // Pass alpha and image on properties with or without destructor
236 this->image = image;
237 this->alpha = alpha;
238 }
239
240 // Set width/height of frame
241 mlt_properties_set_int( properties, "width", this->width );
242 mlt_properties_set_int( properties, "height", this->height );
243 mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "real_width" ) );
244 mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "real_height" ) );
245
246 // pass the image and alpha data without destructor
247 mlt_properties_set_data( properties, "image", this->image, this->width * this->height * 2, NULL, NULL );
248 mlt_properties_set_data( properties, "alpha", this->alpha, this->width * this->height, NULL, NULL );
249 }
250
251 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
252 {
253 // Obtain properties of frame
254 mlt_properties properties = mlt_frame_properties( frame );
255
256 // Refresh the image
257 refresh_image( frame, *width, *height );
258
259 // May need to know the size of the image to clone it
260 int size = 0;
261
262 // Get the image
263 uint8_t *image = mlt_properties_get_data( properties, "image", &size );
264
265 // Get width and height
266 *width = mlt_properties_get_int( properties, "width" );
267 *height = mlt_properties_get_int( properties, "height" );
268
269 // Clone if necessary
270 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
271 // The fault is not in the design of mlt, but in the implementation of pixbuf...
272 //if ( writable )
273 {
274 size = *width * *height * 2;
275
276 // Clone our image
277 uint8_t *copy = malloc( size );
278 memcpy( copy, image, size );
279
280 // We're going to pass the copy on
281 image = copy;
282
283 // Now update properties so we free the copy after
284 mlt_properties_set_data( properties, "image", copy, size, free, NULL );
285 }
286
287 // Pass on the image
288 *buffer = image;
289
290 return 0;
291 }
292
293 static uint8_t *producer_get_alpha_mask( mlt_frame this )
294 {
295 // Obtain properties of frame
296 mlt_properties properties = mlt_frame_properties( this );
297
298 // Return the alpha mask
299 return mlt_properties_get_data( properties, "alpha", NULL );
300 }
301
302 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
303 {
304 // Get the real structure for this producer
305 producer_pixbuf this = producer->child;
306
307 // Generate a frame
308 *frame = mlt_frame_init( );
309
310 // Obtain properties of frame and producer
311 mlt_properties properties = mlt_frame_properties( *frame );
312
313 // Set the producer on the frame properties
314 mlt_properties_set_data( properties, "producer_pixbuf", this, 0, NULL, NULL );
315
316 // Refresh the pango image
317 refresh_image( *frame, 0, 0 );
318
319 // Set alpha call back
320 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
321
322 // Push the get_image method
323 mlt_frame_push_get_image( *frame, producer_get_image );
324
325 // Calculate the next timecode
326 mlt_producer_prepare_next( producer );
327
328 return 0;
329 }
330
331 static void producer_close( mlt_producer parent )
332 {
333 producer_pixbuf this = parent->child;
334 free( this->image );
335 free( this->alpha );
336 parent->close = NULL;
337 mlt_producer_close( parent );
338 free( this );
339 }
340