]> git.sesse.net Git - mlt/blob - src/modules/gtk2/producer_pixbuf.c
Refactor to use mlt_frame_set_image/_alpha.
[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 this, mlt_properties producer_properties );
65 static void refresh_image( producer_pixbuf this, 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 this = calloc( sizeof( struct producer_pixbuf_s ), 1 );
72         if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
73         {
74                 mlt_producer producer = &this->parent;
75
76                 // Get the properties interface
77                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->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( this, properties );
92                 if ( this->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( &this->mutex, NULL );
99                                 mlt_properties_set_data( frame_properties, "producer_pixbuf", this, 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( this, frame, 0, 0 );
103                                 mlt_frame_close( frame );
104                         }
105                 }
106                 if ( this->width == 0 )
107                 {
108                         producer_close( producer );
109                         producer = NULL;
110                 }
111                 return producer;
112         }
113         free( this );
114         return NULL;
115 }
116
117 static void load_filenames( producer_pixbuf this, mlt_properties producer_properties )
118 {
119         char *filename = mlt_properties_get( producer_properties, "resource" );
120         this->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( this->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( this->filenames, key, full );
168                                 gap = 0;
169                         }
170                         else
171                         {
172                                 gap ++;
173                         }
174                 }
175                 if ( mlt_properties_count( this->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( this->filenames, dir_name, wildcard, 1 );
188
189                 free( dir_name );
190         }
191         else
192         {
193                 mlt_properties_set( this->filenames, "0", filename );
194         }
195
196         this->count = mlt_properties_count( this->filenames );
197 }
198
199 static void refresh_image( producer_pixbuf this, 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 = &this->parent;
206
207         // Obtain properties of producer
208         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
209
210         // Obtain the cache flag and structure
211         int use_cache = mlt_properties_get_int( producer_props, "cache" );
212         mlt_properties cache = mlt_properties_get_data( producer_props, "_cache", NULL );
213         int update_cache = 0;
214
215         // restore GdkPixbuf
216         pthread_mutex_lock( &this->mutex );
217         mlt_cache_item pixbuf_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf" );
218         GdkPixbuf *pixbuf = mlt_cache_item_data( pixbuf_cache, NULL );
219         GError *error = NULL;
220
221         // restore scaled image
222         this->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image" );
223         this->image = mlt_cache_item_data( this->image_cache, NULL );
224
225         // Check if user wants us to reload the image
226         if ( mlt_properties_get_int( producer_props, "force_reload" ) )
227         {
228                 pixbuf = NULL;
229                 this->image = NULL;
230                 mlt_properties_set_int( producer_props, "force_reload", 0 );
231         }
232
233         // Get the time to live for each frame
234         double ttl = mlt_properties_get_int( producer_props, "ttl" );
235
236         // Get the original position of this frame
237         mlt_position position = mlt_properties_get_position( properties, "pixbuf_position" );
238         position += mlt_producer_get_in( producer );
239
240         // Image index
241         int image_idx = ( int )floor( ( double )position / ttl ) % this->count;
242
243         // Key for the cache
244         char image_key[ 10 ];
245         sprintf( image_key, "%d", image_idx );
246
247         pthread_mutex_lock( &g_mutex );
248
249         // Check if the frame is already loaded
250         if ( use_cache )
251         {
252                 if ( cache == NULL )
253                 {
254                         cache = mlt_properties_new( );
255                         mlt_properties_set_data( producer_props, "_cache", cache, 0, ( mlt_destructor )mlt_properties_close, NULL );
256                 }
257
258                 mlt_frame cached = mlt_properties_get_data( cache, image_key, NULL );
259
260                 if ( cached )
261                 {
262                         this->image_idx = image_idx;
263                         mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
264                         this->width = mlt_properties_get_int( cached_props, "width" );
265                         this->height = mlt_properties_get_int( cached_props, "height" );
266                         mlt_properties_set_int( producer_props, "_real_width", mlt_properties_get_int( cached_props, "real_width" ) );
267                         mlt_properties_set_int( producer_props, "_real_height", mlt_properties_get_int( cached_props, "real_height" ) );
268                         this->image = mlt_properties_get_data( cached_props, "image", NULL );
269                         this->alpha = mlt_properties_get_int( cached_props, "alpha" );
270
271                         if ( width != 0 && ( width != this->width || height != this->height ) )
272                                 this->image = NULL;
273                 }
274         }
275         int disable_exif = mlt_properties_get_int( producer_props, "disable_exif" );
276         
277     // optimization for subsequent iterations on single picture
278         if ( width != 0 && ( image_idx != this->image_idx || width != this->width || height != this->height ) )
279                 this->image = NULL;
280         if ( image_idx != this->pixbuf_idx )
281                 pixbuf = NULL;
282         mlt_log_debug( MLT_PRODUCER_SERVICE( producer ), "image %p pixbuf %p idx %d image_idx %d pixbuf_idx %d width %d\n",
283                 this->image, pixbuf, image_idx, this->image_idx, this->pixbuf_idx, width );
284         if ( !pixbuf || mlt_properties_get_int( producer_props, "_disable_exif" ) != disable_exif )
285         {
286                 this->image = NULL;
287                 pixbuf = gdk_pixbuf_new_from_file( mlt_properties_get_value( this->filenames, image_idx ), &error );
288
289                 if ( pixbuf )
290                 {
291 #ifdef USE_EXIF
292                         // Read the exif value for this file
293                         if ( disable_exif == 0) {
294                                 ExifData *d = exif_data_new_from_file( mlt_properties_get_value( this->filenames, image_idx ) );
295                                 ExifEntry *entry;
296                                 int exif_orientation = 0;
297
298                                 /* get orientation and rotate image accordingly if necessary */
299                                 if (d) {
300                                         if ( ( entry = exif_content_get_entry ( d->ifd[EXIF_IFD_0], EXIF_TAG_ORIENTATION ) ) )
301                                                 exif_orientation = exif_get_short (entry->data, exif_data_get_byte_order (d));
302
303                                         /* Free the EXIF data */
304                                         exif_data_unref(d);
305                                 }
306                                 
307                                 // Remember EXIF value, might be useful for someone
308                                 mlt_properties_set_int( producer_props, "_exif_orientation" , exif_orientation );
309
310                                 if ( exif_orientation > 1 )
311                                 {
312                                         GdkPixbuf *processed = NULL;
313                                         GdkPixbufRotation matrix = GDK_PIXBUF_ROTATE_NONE;
314
315                                         // Rotate image according to exif data
316                                         switch ( exif_orientation ) {
317                                           case 2:
318                                               processed = gdk_pixbuf_flip ( pixbuf, TRUE );
319                                               break;
320                                           case 3:
321                                               matrix = GDK_PIXBUF_ROTATE_UPSIDEDOWN;
322                                               processed = pixbuf;
323                                               break;
324                                           case 4:
325                                               processed = gdk_pixbuf_flip ( pixbuf, FALSE );
326                                               break;
327                                           case 5:
328                                               matrix = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
329                                               processed = gdk_pixbuf_flip ( pixbuf, TRUE );
330                                               break;
331                                           case 6:
332                                               matrix = GDK_PIXBUF_ROTATE_CLOCKWISE;
333                                               processed = pixbuf;
334                                               break;
335                                           case 7:
336                                               matrix = GDK_PIXBUF_ROTATE_CLOCKWISE;
337                                               processed = gdk_pixbuf_flip ( pixbuf, TRUE );
338                                               break;
339                                           case 8:
340                                               matrix = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
341                                               processed = pixbuf;
342                                               break;
343                                         }
344                                         if ( processed )
345                                                 pixbuf = gdk_pixbuf_rotate_simple( processed, matrix );
346                                 }
347                         }
348 #endif
349
350                         // Register this pixbuf for destruction and reuse
351                         mlt_cache_item_close( pixbuf_cache );
352                         mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref );
353                         pixbuf_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.pixbuf" );
354                         this->pixbuf_idx = image_idx;
355
356                         mlt_events_block( producer_props, NULL );
357                         mlt_properties_set_int( producer_props, "_real_width", gdk_pixbuf_get_width( pixbuf ) );
358                         mlt_properties_set_int( producer_props, "_real_height", gdk_pixbuf_get_height( pixbuf ) );
359                         mlt_properties_set_int( producer_props, "_disable_exif", disable_exif );
360                         mlt_events_unblock( producer_props, NULL );
361
362                         // Store the width/height of the pixbuf temporarily
363                         this->width = gdk_pixbuf_get_width( pixbuf );
364                         this->height = gdk_pixbuf_get_height( pixbuf );
365                 }
366         }
367
368         // If we have a pixbuf and we need an image
369         if ( pixbuf && width > 0 && !this->image )
370         {
371                 char *interps = mlt_properties_get( properties, "rescale.interp" );
372                 int interp = GDK_INTERP_BILINEAR;
373
374                 if ( strcmp( interps, "nearest" ) == 0 )
375                         interp = GDK_INTERP_NEAREST;
376                 else if ( strcmp( interps, "tiles" ) == 0 )
377                         interp = GDK_INTERP_TILES;
378                 else if ( strcmp( interps, "hyper" ) == 0 || strcmp( interps, "bicubic" ) == 0 )
379                         interp = GDK_INTERP_HYPER;
380
381                 // Note - the original pixbuf is already safe and ready for destruction
382                 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
383
384                 // Store width and height
385                 this->width = width;
386                 this->height = height;
387                 
388                 // Allocate/define image
389                 this->alpha = gdk_pixbuf_get_has_alpha( pixbuf );
390                 int src_stride = gdk_pixbuf_get_rowstride( pixbuf );
391                 int dst_stride = this->width * ( this->alpha ? 4 : 3 );
392                 int image_size = dst_stride * ( height + 1 );
393                 this->image = mlt_pool_alloc( image_size );
394
395                 if ( src_stride != dst_stride )
396                 {
397                         int y = this->height;
398                         uint8_t *src = gdk_pixbuf_get_pixels( pixbuf );
399                         uint8_t *dst = this->image;
400                         while ( y-- )
401                         {
402                                 memcpy( dst, src, dst_stride );
403                                 dst += dst_stride;
404                                 src += src_stride;
405                         }
406                 }
407                 else
408                 {
409                         memcpy( this->image, gdk_pixbuf_get_pixels( pixbuf ), src_stride * height );
410                 }
411                 if ( !use_cache )
412                         mlt_cache_item_close( this->image_cache );
413                 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image", this->image, image_size, mlt_pool_release );
414                 this->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "pixbuf.image" );
415                 this->image_idx = image_idx;
416
417                 // Finished with pixbuf now
418                 g_object_unref( pixbuf );
419
420                 // Ensure we update the cache when we need to
421                 update_cache = use_cache;
422         }
423
424         // release references no longer needed
425         mlt_cache_item_close( pixbuf_cache );
426         if ( width == 0 )
427         {
428                 pthread_mutex_unlock( &this->mutex );
429                 mlt_cache_item_close( this->image_cache );
430         }
431
432         // Set width/height of frame
433         mlt_properties_set_int( properties, "width", this->width );
434         mlt_properties_set_int( properties, "height", this->height );
435         mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
436         mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
437
438         if ( update_cache )
439         {
440                 mlt_frame cached = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
441                 mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
442                 mlt_properties_set_int( cached_props, "width", this->width );
443                 mlt_properties_set_int( cached_props, "height", this->height );
444                 mlt_properties_set_int( cached_props, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
445                 mlt_properties_set_int( cached_props, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
446                 mlt_frame_set_image( cached, this->image, this->width * ( this->alpha ? 4 : 3 ) * this->height, mlt_pool_release );
447                 mlt_properties_set_int( cached_props, "alpha", this->alpha );
448                 mlt_properties_set_data( cache, image_key, cached, 0, ( mlt_destructor )mlt_frame_close, NULL );
449         }
450
451         pthread_mutex_unlock( &g_mutex );
452 }
453
454 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
455 {
456         int error = 0;
457         
458         // Obtain properties of frame
459         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
460
461         // Obtain the producer for this frame
462         producer_pixbuf this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
463
464         *width = mlt_properties_get_int( properties, "rescale_width" );
465         *height = mlt_properties_get_int( properties, "rescale_height" );
466
467         mlt_service_lock( MLT_PRODUCER_SERVICE( &this->parent ) );
468
469         // Refresh the image
470         refresh_image( this, frame, *width, *height );
471
472         // Get width and height (may have changed during the refresh)
473         *width = this->width;
474         *height = this->height;
475
476         // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
477         // The fault is not in the design of mlt, but in the implementation of the pixbuf producer...
478         if ( this->image )
479         {
480                 // Clone the image
481                 int image_size = this->width * this->height * ( this->alpha ? 4 :3 );
482                 uint8_t *image_copy = mlt_pool_alloc( image_size );
483                 memcpy( image_copy, this->image, image_size );
484                 // Now update properties so we free the copy after
485                 mlt_frame_set_image( frame, image_copy, image_size, mlt_pool_release );
486                 // We're going to pass the copy on
487                 *buffer = image_copy;
488                 *format = this->alpha ? mlt_image_rgb24a : mlt_image_rgb24;
489                 mlt_log_debug( MLT_PRODUCER_SERVICE( &this->parent ), "%dx%d (%s)\n",
490                         this->width, this->height, mlt_image_format_name( *format ) );
491         }
492         else
493         {
494                 error = 1;
495         }
496
497         // Release references and locks
498         pthread_mutex_unlock( &this->mutex );
499         mlt_cache_item_close( this->image_cache );
500         mlt_service_unlock( MLT_PRODUCER_SERVICE( &this->parent ) );
501
502         return error;
503 }
504
505 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
506 {
507         // Get the real structure for this producer
508         producer_pixbuf this = producer->child;
509
510         // Fetch the producers properties
511         mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
512
513         if ( this->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
514                 load_filenames( this, producer_properties );
515
516         // Generate a frame
517         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
518
519         if ( *frame != NULL && this->count > 0 )
520         {
521                 // Obtain properties of frame and producer
522                 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
523
524                 // Set the producer on the frame properties
525                 mlt_properties_set_data( properties, "producer_pixbuf", this, 0, NULL, NULL );
526
527                 // Update timecode on the frame we're creating
528                 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
529
530                 // Ensure that we have a way to obtain the position in the get_image
531                 mlt_properties_set_position( properties, "pixbuf_position", mlt_producer_position( producer ) );
532
533                 // Refresh the image
534                 refresh_image( this, *frame, 0, 0 );
535
536                 // Set producer-specific frame properties
537                 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( producer_properties, "progressive" ) );
538                 
539                 double force_ratio = mlt_properties_get_double( producer_properties, "force_aspect_ratio" );
540                 if ( force_ratio > 0.0 )
541                         mlt_properties_set_double( properties, "aspect_ratio", force_ratio );
542                 else
543                         mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
544
545                 // Push the get_image method
546                 mlt_frame_push_get_image( *frame, producer_get_image );
547         }
548
549         // Calculate the next timecode
550         mlt_producer_prepare_next( producer );
551
552         return 0;
553 }
554
555 static void producer_close( mlt_producer parent )
556 {
557         producer_pixbuf this = parent->child;
558         pthread_mutex_destroy( &this->mutex );
559         parent->close = NULL;
560         mlt_service_cache_purge( MLT_PRODUCER_SERVICE(parent) );
561         mlt_producer_close( parent );
562         mlt_properties_close( this->filenames );
563         free( this );
564 }