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