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