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