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