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