westley serialises with entry in/out; full field, aspect, and colour space normalisat...
[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, "ttl", 25 );
68
69 // Obtain filenames
70 if ( strchr( filename, '%' ) != NULL )
71 {
72 // handle picture sequences
73 int i = 0;
74 int gap = 0;
75 char full[1024];
76
77 while ( gap < 100 )
78 {
79 struct stat buf;
80 snprintf( full, 1023, filename, i ++ );
81 if ( stat( full, &buf ) == 0 )
82 {
83 this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
84 this->filenames[ this->count ++ ] = strdup( full );
85 gap = 0;
86 }
87 else
88 {
89 gap ++;
90 }
91 }
92 mlt_properties_set_position( properties, "out", this->count * 25 );
93 }
94 else if ( strstr( filename, "/.all." ) != NULL )
95 {
96 char *dir_name = strdup( filename );
97 char *extension = strrchr( filename, '.' );
98 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
99 char fullname[ 1024 ];
100 strcpy( fullname, dir_name );
101 struct dirent **de = NULL;
102 int n = scandir( fullname, &de, filter_files, alphasort );
103 int i;
104 struct stat info;
105
106 for (i = 0; i < n; i++ )
107 {
108 snprintf( fullname, 1023, "%s%s", dir_name, de[i]->d_name );
109
110 if ( lstat( fullname, &info ) == 0 &&
111 ( S_ISREG( info.st_mode ) || ( strstr( fullname, extension ) && info.st_mode | S_IXUSR ) ) )
112 {
113 this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
114 this->filenames[ this->count ++ ] = strdup( fullname );
115 }
116 free( de[ i ] );
117 }
118
119 free( de );
120 free( dir_name );
121 }
122 else
123 {
124 this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
125 this->filenames[ this->count ++ ] = strdup( filename );
126 mlt_properties_set_position( properties, "out", 25 );
127 }
128
129 // Initialise gobject types
130 g_type_init();
131
132 return producer;
133 }
134 free( this );
135 return NULL;
136 }
137
138 static void refresh_image( mlt_frame frame, int width, int height )
139 {
140 // Pixbuf
141 GdkPixbuf *pixbuf = NULL;
142 GError *error = NULL;
143
144 // Obtain properties of frame
145 mlt_properties properties = mlt_frame_properties( frame );
146
147 // Obtain the producer for this frame
148 producer_pixbuf this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
149
150 // Obtain the producer
151 mlt_producer producer = &this->parent;
152
153 // Obtain properties of producer
154 mlt_properties producer_props = mlt_producer_properties( producer );
155
156 // Get the time to live for each frame
157 double ttl = mlt_properties_get_int( producer_props, "ttl" );
158
159 // Image index
160 int image_idx = ( int )floor( mlt_producer_position( producer ) / ttl ) % this->count;
161
162 // Update timecode on the frame we're creating
163 mlt_frame_set_position( frame, mlt_producer_position( producer ) );
164
165 // optimization for subsequent iterations on single picture
166 if ( this->image != NULL && image_idx == this->image_idx )
167 {
168 if ( width != this->width || height != this->height )
169 {
170 pixbuf = mlt_properties_get_data( producer_props, "pixbuf", NULL );
171 mlt_properties_set_int( producer_props, "bpp", gdk_pixbuf_get_has_alpha( pixbuf ) ? 4 : 3 );
172 }
173 }
174 else
175 {
176 this->image_idx = image_idx;
177 pixbuf = gdk_pixbuf_new_from_file( this->filenames[ image_idx ], &error );
178
179 if ( pixbuf != NULL )
180 {
181 // Register this pixbuf for destruction and reuse
182 mlt_properties_set_data( producer_props, "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
183
184 mlt_properties_set_int( producer_props, "real_width", gdk_pixbuf_get_width( pixbuf ) );
185 mlt_properties_set_int( producer_props, "real_height", gdk_pixbuf_get_height( pixbuf ) );
186
187 // Store the width/height of the pixbuf temporarily
188 this->width = gdk_pixbuf_get_width( pixbuf );
189 this->height = gdk_pixbuf_get_height( pixbuf );
190
191 mlt_properties_set_int( producer_props, "bpp", gdk_pixbuf_get_has_alpha( pixbuf ) ? 4 : 3 );
192 }
193 }
194
195 int bpp = mlt_properties_get_int( producer_props, "bpp" );
196
197 // If we have a pixbuf
198 if ( pixbuf && width > 0 )
199 {
200 int i;
201 //fprintf( stderr, "SCALING PIXBUF from %dx%d to %dx%d\n", gdk_pixbuf_get_width( pixbuf ), gdk_pixbuf_get_height( pixbuf ), width, height );
202
203 // Note - the original pixbuf is already safe and ready for destruction
204 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, GDK_INTERP_HYPER );
205
206 // Store width and height
207 this->width = gdk_pixbuf_get_width( pixbuf );
208 this->height = gdk_pixbuf_get_height( pixbuf );
209
210 // Allocate/define image
211 // IRRIGATE ME
212 uint8_t *image = malloc( width * ( height + 1 ) * bpp );
213
214 for ( i = 0; i < height; i++ )
215 memcpy( image + i * width * bpp,
216 gdk_pixbuf_get_pixels( pixbuf ) + i * gdk_pixbuf_get_rowstride( pixbuf ),
217 gdk_pixbuf_get_width( pixbuf ) * bpp );
218
219 // Finished with pixbuf now
220 g_object_unref( pixbuf );
221
222 // Pass image on properties with or without destructor
223 free( this->image );
224 this->image = image;
225 }
226
227 // Set width/height of frame
228 mlt_properties_set_int( properties, "width", this->width );
229 mlt_properties_set_int( properties, "height", this->height );
230 mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "real_width" ) );
231 mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "real_height" ) );
232
233 // pass the image data without destructor
234 mlt_properties_set_data( properties, "image", this->image, this->width * ( this->height + 1 ) * bpp, NULL, NULL );
235 }
236
237 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
238 {
239 // Obtain properties of frame
240 mlt_properties properties = mlt_frame_properties( frame );
241
242 // Refresh the image
243 refresh_image( frame, *width, *height );
244
245 // Determine format
246 mlt_producer this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
247 *format = ( mlt_properties_get_int( mlt_producer_properties( this ), "bpp" ) == 4 ) ? mlt_image_rgb24a : mlt_image_rgb24;
248
249 // May need to know the size of the image to clone it
250 int size = 0;
251
252 // Get the image
253 uint8_t *image = mlt_properties_get_data( properties, "image", &size );
254
255 // Get width and height
256 *width = mlt_properties_get_int( properties, "width" );
257 *height = mlt_properties_get_int( properties, "height" );
258
259 // Clone if necessary
260 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
261 // The fault is not in the design of mlt, but in the implementation of pixbuf...
262 //if ( writable )
263 {
264 // Clone our image
265 // IRRIGATE ME
266 uint8_t *copy = malloc( size );
267 memcpy( copy, image, size );
268
269 // We're going to pass the copy on
270 image = copy;
271
272 // Now update properties so we free the copy after
273 mlt_properties_set_data( properties, "image", copy, size, free, NULL );
274 }
275
276 // Pass on the image
277 *buffer = image;
278
279 return 0;
280 }
281
282 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
283 {
284 // Get the real structure for this producer
285 producer_pixbuf this = producer->child;
286
287 // Generate a frame
288 *frame = mlt_frame_init( );
289
290 // Obtain properties of frame and producer
291 mlt_properties properties = mlt_frame_properties( *frame );
292
293 // Set the producer on the frame properties
294 mlt_properties_set_data( properties, "producer_pixbuf", this, 0, NULL, NULL );
295
296 // Refresh the image
297 refresh_image( *frame, 0, 0 );
298
299 // Set producer-specific frame properties
300 mlt_properties_set_int( properties, "progressive", 1 );
301 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( properties, "real_width" ) / mlt_properties_get_double( properties, "real_height" ) );
302
303 // Push the get_image method
304 mlt_frame_push_get_image( *frame, producer_get_image );
305
306 // Calculate the next timecode
307 mlt_producer_prepare_next( producer );
308
309 return 0;
310 }
311
312 static void producer_close( mlt_producer parent )
313 {
314 producer_pixbuf this = parent->child;
315 free( this->image );
316 parent->close = NULL;
317 mlt_producer_close( parent );
318 free( this );
319 }
320