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