]> git.sesse.net Git - mlt/blob - src/modules/gtk2/producer_pixbuf.c
836dc87a42cac83cda8e9c84e359d125acba5276
[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 library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <framework/mlt_producer.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_cache.h>
24 #include <framework/mlt_log.h>
25 #include <gdk-pixbuf/gdk-pixbuf.h>
26
27 #include "config.h"
28
29 #ifdef USE_EXIF
30 #include <libexif/exif-data.h>
31 #endif
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <pthread.h>
37 #include <math.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <unistd.h>
41 #include <dirent.h>
42
43 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
44
45 typedef struct producer_pixbuf_s *producer_pixbuf;
46
47 struct producer_pixbuf_s
48 {
49         struct mlt_producer_s parent;
50
51         // File name list
52         mlt_properties filenames;
53         int count;
54         int image_idx;
55         int pixbuf_idx;
56         int width;
57         int height;
58         int alpha;
59         uint8_t *image;
60         mlt_cache_item image_cache;
61         pthread_mutex_t mutex;
62 };
63
64 static void load_filenames( producer_pixbuf self, mlt_properties producer_properties );
65 static void refresh_image( producer_pixbuf self, mlt_frame frame, int width, int height );
66 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
67 static void producer_close( mlt_producer parent );
68
69 mlt_producer producer_pixbuf_init( char *filename )
70 {
71         producer_pixbuf self = calloc( sizeof( struct producer_pixbuf_s ), 1 );
72         if ( self != NULL && mlt_producer_init( &self->parent, self ) == 0 )
73         {
74                 mlt_producer producer = &self->parent;
75
76                 // Get the properties interface
77                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &self->parent );
78         
79                 // Callback registration
80                 producer->get_frame = producer_get_frame;
81                 producer->close = ( mlt_destructor )producer_close;
82
83                 // Set the default properties
84                 mlt_properties_set( properties, "resource", filename );
85                 mlt_properties_set_int( properties, "ttl", 25 );
86                 mlt_properties_set_int( properties, "aspect_ratio", 1 );
87                 mlt_properties_set_int( properties, "progressive", 1 );
88
89                 // Validate the resource
90                 if ( filename )
91                         load_filenames( self, properties );
92                 if ( self->count )
93                 {
94                         mlt_frame frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
95                         if ( frame )
96                         {
97                                 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
98                                 pthread_mutex_init( &self->mutex, NULL );
99                                 mlt_properties_set_data( frame_properties, "producer_pixbuf", self, 0, NULL, NULL );
100                                 mlt_frame_set_position( frame, mlt_producer_position( producer ) );
101                                 mlt_properties_set_position( frame_properties, "pixbuf_position", mlt_producer_position( producer ) );
102                                 refresh_image( self, frame, 0, 0 );
103                                 mlt_frame_close( frame );
104                         }
105                 }
106                 if ( self->width == 0 )
107                 {
108                         producer_close( producer );
109                         producer = NULL;
110                 }
111                 return producer;
112         }
113         free( self );
114         return NULL;
115 }
116
117 static void load_filenames( producer_pixbuf self, mlt_properties producer_properties )
118 {
119         char *filename = mlt_properties_get( producer_properties, "resource" );
120         self->filenames = mlt_properties_new( );
121
122         // Read xml string
123         if ( strstr( filename, "<svg" ) )
124         {
125                 // Generate a temporary file for the svg
126                 char fullname[ 1024 ] = "/tmp/mlt.XXXXXX";
127                 int fd = g_mkstemp( fullname );
128
129                 if ( fd > -1 )
130                 {
131                         // Write the svg into the temp file
132                         ssize_t remaining_bytes;
133                         char *xml = filename;
134                         
135                         // Strip leading crap
136                         while ( xml[0] != '<' )
137                                 xml++;
138                         
139                         remaining_bytes = strlen( xml );
140                         while ( remaining_bytes > 0 )
141                                 remaining_bytes -= write( fd, xml + strlen( xml ) - remaining_bytes, remaining_bytes );
142                         close( fd );
143
144                         mlt_properties_set( self->filenames, "0", fullname );
145
146                         // Teehe - when the producer closes, delete the temp file and the space allo
147                         mlt_properties_set_data( producer_properties, "__temporary_file__", fullname, 0, ( mlt_destructor )unlink, NULL );
148                 }
149         }
150         // Obtain filenames
151         else if ( strchr( filename, '%' ) != NULL )
152         {
153                 // handle picture sequences
154                 int i = mlt_properties_get_int( producer_properties, "begin" );
155                 int gap = 0;
156                 char full[1024];
157                 int keyvalue = 0;
158                 char key[ 50 ];
159
160                 while ( gap < 100 )
161                 {
162                         struct stat buf;
163                         snprintf( full, 1023, filename, i ++ );
164                         if ( stat( full, &buf ) == 0 )
165                         {
166                                 sprintf( key, "%d", keyvalue ++ );
167                                 mlt_properties_set( self->filenames, key, full );
168                                 gap = 0;
169                         }
170                         else
171                         {
172                                 gap ++;
173                         }
174                 }
175                 if ( mlt_properties_count( self->filenames ) > 0 )
176                         mlt_properties_set_int( producer_properties, "ttl", 1 );
177         }
178         else if ( strstr( filename, "/.all." ) != NULL )
179         {
180                 char wildcard[ 1024 ];
181                 char *dir_name = strdup( filename );
182                 char *extension = strrchr( dir_name, '.' );
183
184                 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
185                 sprintf( wildcard, "*%s", extension );
186
187                 mlt_properties_dir_list( self->filenames, dir_name, wildcard, 1 );
188
189                 free( dir_name );
190         }
191         else
192         {
193                 mlt_properties_set( self->filenames, "0", filename );
194         }
195
196         self->count = mlt_properties_count( self->filenames );
197 }
198
199 static void refresh_image( producer_pixbuf self, mlt_frame frame, int width, int height )
200 {
201         // Obtain properties of frame
202         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
203
204         // Obtain the producer
205         mlt_producer producer = &self->parent;
206
207         // Obtain properties of producer
208         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
209
210         // restore GdkPixbuf
211         pthread_mutex_lock( &self->mutex );
212         mlt_cache_item pixbuf_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf" );
213         GdkPixbuf *pixbuf = mlt_cache_item_data( pixbuf_cache, NULL );
214         GError *error = NULL;
215
216         // restore scaled image
217         self->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image" );
218         self->image = mlt_cache_item_data( self->image_cache, NULL );
219
220         // Check if user wants us to reload the image
221         if ( mlt_properties_get_int( producer_props, "force_reload" ) )
222         {
223                 pixbuf = NULL;
224                 self->image = NULL;
225                 mlt_properties_set_int( producer_props, "force_reload", 0 );
226         }
227
228         // Get the time to live for each frame
229         double ttl = mlt_properties_get_int( producer_props, "ttl" );
230
231         // Get the original position of this frame
232         mlt_position position = mlt_properties_get_position( properties, "pixbuf_position" );
233         position += mlt_producer_get_in( producer );
234
235         // Image index
236         int image_idx = ( int )floor( ( double )position / ttl ) % self->count;
237
238         // Key for the cache
239         char image_key[ 10 ];
240         sprintf( image_key, "%d", image_idx );
241
242         pthread_mutex_lock( &g_mutex );
243
244         int disable_exif = mlt_properties_get_int( producer_props, "disable_exif" );
245         
246         // optimization for subsequent iterations on single picture
247         if ( width != 0 && ( image_idx != self->image_idx || width != self->width || height != self->height ) )
248                 self->image = NULL;
249         if ( image_idx != self->pixbuf_idx )
250                 pixbuf = NULL;
251         mlt_log_debug( MLT_PRODUCER_SERVICE( producer ), "image %p pixbuf %p idx %d image_idx %d pixbuf_idx %d width %d\n",
252                 self->image, pixbuf, image_idx, self->image_idx, self->pixbuf_idx, width );
253         if ( !pixbuf || mlt_properties_get_int( producer_props, "_disable_exif" ) != disable_exif )
254         {
255                 self->image = NULL;
256                 pixbuf = gdk_pixbuf_new_from_file( mlt_properties_get_value( self->filenames, image_idx ), &error );
257
258                 if ( pixbuf )
259                 {
260 #ifdef USE_EXIF
261                         // Read the exif value for this file
262                         if ( disable_exif == 0) {
263                                 ExifData *d = exif_data_new_from_file( mlt_properties_get_value( self->filenames, image_idx ) );
264                                 ExifEntry *entry;
265                                 int exif_orientation = 0;
266
267                                 /* get orientation and rotate image accordingly if necessary */
268                                 if (d) {
269                                         if ( ( entry = exif_content_get_entry ( d->ifd[EXIF_IFD_0], EXIF_TAG_ORIENTATION ) ) )
270                                                 exif_orientation = exif_get_short (entry->data, exif_data_get_byte_order (d));
271
272                                         /* Free the EXIF data */
273                                         exif_data_unref(d);
274                                 }
275                                 
276                                 // Remember EXIF value, might be useful for someone
277                                 mlt_properties_set_int( producer_props, "_exif_orientation" , exif_orientation );
278
279                                 if ( exif_orientation > 1 )
280                                 {
281                                         GdkPixbuf *processed = NULL;
282                                         GdkPixbufRotation matrix = GDK_PIXBUF_ROTATE_NONE;
283
284                                         // Rotate image according to exif data
285                                         switch ( exif_orientation ) {
286                                           case 2:
287                                               processed = gdk_pixbuf_flip ( pixbuf, TRUE );
288                                               break;
289                                           case 3:
290                                               matrix = GDK_PIXBUF_ROTATE_UPSIDEDOWN;
291                                               processed = pixbuf;
292                                               break;
293                                           case 4:
294                                               processed = gdk_pixbuf_flip ( pixbuf, FALSE );
295                                               break;
296                                           case 5:
297                                               matrix = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
298                                               processed = gdk_pixbuf_flip ( pixbuf, TRUE );
299                                               break;
300                                           case 6:
301                                               matrix = GDK_PIXBUF_ROTATE_CLOCKWISE;
302                                               processed = pixbuf;
303                                               break;
304                                           case 7:
305                                               matrix = GDK_PIXBUF_ROTATE_CLOCKWISE;
306                                               processed = gdk_pixbuf_flip ( pixbuf, TRUE );
307                                               break;
308                                           case 8:
309                                               matrix = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
310                                               processed = pixbuf;
311                                               break;
312                                         }
313                                         if ( processed )
314                                                 pixbuf = gdk_pixbuf_rotate_simple( processed, matrix );
315                                 }
316                         }
317 #endif
318
319                         // Register this pixbuf for destruction and reuse
320                         mlt_cache_item_close( pixbuf_cache );
321                         mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref );
322                         pixbuf_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf" );
323                         self->pixbuf_idx = image_idx;
324
325                         mlt_events_block( producer_props, NULL );
326                         mlt_properties_set_int( producer_props, "_real_width", gdk_pixbuf_get_width( pixbuf ) );
327                         mlt_properties_set_int( producer_props, "_real_height", gdk_pixbuf_get_height( pixbuf ) );
328                         mlt_properties_set_int( producer_props, "_disable_exif", disable_exif );
329                         mlt_events_unblock( producer_props, NULL );
330
331                         // Store the width/height of the pixbuf temporarily
332                         self->width = gdk_pixbuf_get_width( pixbuf );
333                         self->height = gdk_pixbuf_get_height( pixbuf );
334                 }
335         }
336
337         // If we have a pixbuf and we need an image
338         if ( pixbuf && width > 0 && !self->image )
339         {
340                 char *interps = mlt_properties_get( properties, "rescale.interp" );
341                 int interp = GDK_INTERP_BILINEAR;
342
343                 if ( strcmp( interps, "nearest" ) == 0 )
344                         interp = GDK_INTERP_NEAREST;
345                 else if ( strcmp( interps, "tiles" ) == 0 )
346                         interp = GDK_INTERP_TILES;
347                 else if ( strcmp( interps, "hyper" ) == 0 || strcmp( interps, "bicubic" ) == 0 )
348                         interp = GDK_INTERP_HYPER;
349
350                 // Note - the original pixbuf is already safe and ready for destruction
351                 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
352
353                 // Store width and height
354                 self->width = width;
355                 self->height = height;
356                 
357                 // Allocate/define image
358                 self->alpha = gdk_pixbuf_get_has_alpha( pixbuf );
359                 int src_stride = gdk_pixbuf_get_rowstride( pixbuf );
360                 int dst_stride = self->width * ( self->alpha ? 4 : 3 );
361                 int image_size = dst_stride * ( height + 1 );
362                 self->image = mlt_pool_alloc( image_size );
363
364                 if ( src_stride != dst_stride )
365                 {
366                         int y = self->height;
367                         uint8_t *src = gdk_pixbuf_get_pixels( pixbuf );
368                         uint8_t *dst = self->image;
369                         while ( y-- )
370                         {
371                                 memcpy( dst, src, dst_stride );
372                                 dst += dst_stride;
373                                 src += src_stride;
374                         }
375                 }
376                 else
377                 {
378                         memcpy( self->image, gdk_pixbuf_get_pixels( pixbuf ), src_stride * height );
379                 }
380                 mlt_cache_item_close( self->image_cache );
381                 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image", self->image, image_size, mlt_pool_release );
382                 self->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image" );
383                 self->image_idx = image_idx;
384
385                 // Finished with pixbuf now
386                 g_object_unref( pixbuf );
387         }
388
389         // release references no longer needed
390         mlt_cache_item_close( pixbuf_cache );
391         if ( width == 0 )
392         {
393                 pthread_mutex_unlock( &self->mutex );
394                 mlt_cache_item_close( self->image_cache );
395         }
396
397         // Set width/height of frame
398         mlt_properties_set_int( properties, "width", self->width );
399         mlt_properties_set_int( properties, "height", self->height );
400         mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
401         mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
402
403         pthread_mutex_unlock( &g_mutex );
404 }
405
406 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
407 {
408         int error = 0;
409         
410         // Obtain properties of frame
411         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
412
413         // Obtain the producer for this frame
414         producer_pixbuf self = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
415
416         *width = mlt_properties_get_int( properties, "rescale_width" );
417         *height = mlt_properties_get_int( properties, "rescale_height" );
418
419         mlt_service_lock( MLT_PRODUCER_SERVICE( &self->parent ) );
420
421         // Refresh the image
422         refresh_image( self, frame, *width, *height );
423
424         // Get width and height (may have changed during the refresh)
425         *width = self->width;
426         *height = self->height;
427         *format = self->alpha ? mlt_image_rgb24a : mlt_image_rgb24;
428
429         // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
430         // The fault is not in the design of mlt, but in the implementation of the pixbuf producer...
431         if ( self->image )
432         {
433                 // Clone the image
434                 int image_size = self->width * self->height * ( self->alpha ? 4 :3 );
435                 uint8_t *image_copy = mlt_pool_alloc( image_size );
436                 memcpy( image_copy, self->image, image_size );
437                 // Now update properties so we free the copy after
438                 mlt_frame_set_image( frame, image_copy, image_size, mlt_pool_release );
439                 // We're going to pass the copy on
440                 *buffer = image_copy;
441                 mlt_log_debug( MLT_PRODUCER_SERVICE( &self->parent ), "%dx%d (%s)\n",
442                         self->width, self->height, mlt_image_format_name( *format ) );
443         }
444         else
445         {
446                 error = 1;
447         }
448
449         // Release references and locks
450         pthread_mutex_unlock( &self->mutex );
451         mlt_cache_item_close( self->image_cache );
452         mlt_service_unlock( MLT_PRODUCER_SERVICE( &self->parent ) );
453
454         return error;
455 }
456
457 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
458 {
459         // Get the real structure for this producer
460         producer_pixbuf self = producer->child;
461
462         // Fetch the producers properties
463         mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
464
465         if ( self->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
466                 load_filenames( self, producer_properties );
467
468         // Generate a frame
469         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
470
471         if ( *frame != NULL && self->count > 0 )
472         {
473                 // Obtain properties of frame and producer
474                 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
475
476                 // Set the producer on the frame properties
477                 mlt_properties_set_data( properties, "producer_pixbuf", self, 0, NULL, NULL );
478
479                 // Update timecode on the frame we're creating
480                 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
481
482                 // Ensure that we have a way to obtain the position in the get_image
483                 mlt_properties_set_position( properties, "pixbuf_position", mlt_producer_position( producer ) );
484
485                 // Refresh the image
486                 refresh_image( self, *frame, 0, 0 );
487
488                 // Set producer-specific frame properties
489                 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( producer_properties, "progressive" ) );
490                 
491                 double force_ratio = mlt_properties_get_double( producer_properties, "force_aspect_ratio" );
492                 if ( force_ratio > 0.0 )
493                         mlt_properties_set_double( properties, "aspect_ratio", force_ratio );
494                 else
495                         mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
496
497                 // Push the get_image method
498                 mlt_frame_push_get_image( *frame, producer_get_image );
499         }
500
501         // Calculate the next timecode
502         mlt_producer_prepare_next( producer );
503
504         return 0;
505 }
506
507 static void producer_close( mlt_producer parent )
508 {
509         producer_pixbuf self = parent->child;
510         pthread_mutex_destroy( &self->mutex );
511         parent->close = NULL;
512         mlt_service_cache_purge( MLT_PRODUCER_SERVICE(parent) );
513         mlt_producer_close( parent );
514         mlt_properties_close( self->filenames );
515         free( self );
516 }