]> git.sesse.net Git - mlt/blob - src/modules/gtk2/producer_pixbuf.c
Filter optimisations and cleanup part 1
[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 program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "producer_pixbuf.h"
22 #include <framework/mlt_frame.h>
23 #include <gdk-pixbuf/gdk-pixbuf.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <math.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include <dirent.h>
33
34 typedef struct producer_pixbuf_s *producer_pixbuf;
35
36 struct producer_pixbuf_s
37 {
38         struct mlt_producer_s parent;
39
40         // File name list
41         char **filenames;
42         int count;
43         int image_idx;
44
45         int width;
46         int height;
47         uint8_t *image;
48         uint8_t *alpha;
49 };
50
51 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
52 static void producer_close( mlt_producer parent );
53
54 static int filter_files( const struct dirent *de )
55 {
56         if ( de->d_name[ 0 ] != '.' )
57                 return 1;
58         else
59                 return 0;
60 }
61
62 mlt_producer producer_pixbuf_init( char *filename )
63 {
64         producer_pixbuf this = calloc( sizeof( struct producer_pixbuf_s ), 1 );
65         if ( filename != NULL && this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
66         {
67                 mlt_producer producer = &this->parent;
68
69                 // Get the properties interface
70                 mlt_properties properties = mlt_producer_properties( &this->parent );
71         
72                 // Callback registration
73                 producer->get_frame = producer_get_frame;
74                 producer->close = producer_close;
75
76                 // Set the default properties
77                 mlt_properties_set( properties, "resource", filename );
78                 mlt_properties_set_int( properties, "ttl", 25 );
79                 
80                 // Obtain filenames
81                 if ( strchr( filename, '%' ) != NULL )
82                 {
83                         // handle picture sequences
84                         int i = 0;
85                         int gap = 0;
86                         char full[1024];
87
88                         while ( gap < 100 )
89                         {
90                                 struct stat buf;
91                                 snprintf( full, 1023, filename, i ++ );
92                                 if ( stat( full, &buf ) == 0 )
93                                 {
94                                         this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
95                                         this->filenames[ this->count ++ ] = strdup( full );
96                                         gap = 0;
97                                 }
98                                 else
99                                 {
100                                         gap ++;
101                                 }
102                         } 
103                         mlt_properties_set_position( properties, "out", this->count * 250 );
104                 }
105                 else if ( strstr( filename, "/.all." ) != NULL )
106                 {
107                         char *dir_name = strdup( filename );
108                         char *extension = strrchr( filename, '.' );
109                         *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
110                         char fullname[ 1024 ];
111                         strcpy( fullname, dir_name );
112                         struct dirent **de = NULL;
113                         int n = scandir( fullname, &de, filter_files, alphasort );
114                         int i;
115                         struct stat info;
116
117                         for (i = 0; i < n; i++ )
118                         {
119                                 snprintf( fullname, 1023, "%s%s", dir_name, de[i]->d_name );
120
121                                 if ( lstat( fullname, &info ) == 0 && 
122                                         ( S_ISREG( info.st_mode ) || ( strstr( fullname, extension ) && info.st_mode | S_IXUSR ) ) )
123                                 {
124                                         this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
125                                         this->filenames[ this->count ++ ] = strdup( fullname );
126                                 }
127                                 free( de[ i ] );
128                         }
129
130                         free( de );
131                         free( dir_name );
132                 }
133                 else
134                 {
135                         this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
136                         this->filenames[ this->count ++ ] = strdup( filename );
137                         mlt_properties_set_position( properties, "out", 250 );
138                 }
139
140                 // Initialise gobject types
141                 g_type_init();
142
143                 return producer;
144         }
145         free( this );
146         return NULL;
147 }
148
149 static void refresh_image( mlt_frame frame, int width, int height )
150 {
151         // Pixbuf 
152         GdkPixbuf *pixbuf = NULL;
153         GError *error = NULL;
154
155         // Obtain properties of frame
156         mlt_properties properties = mlt_frame_properties( frame );
157
158         // Obtain the producer for this frame
159         producer_pixbuf this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
160
161         // Obtain the producer 
162         mlt_producer producer = &this->parent;
163
164         // Obtain properties of producer
165         mlt_properties producer_props = mlt_producer_properties( producer );
166
167         // Get the time to live for each frame
168         double ttl = mlt_properties_get_int( producer_props, "ttl" );
169
170         // Image index
171         int image_idx = ( int )floor( mlt_frame_get_position( frame ) / ttl ) % this->count;
172
173     // optimization for subsequent iterations on single picture
174         if ( width != 0 && this->image != NULL && image_idx == this->image_idx )
175         {
176                 if ( width != this->width || height != this->height )
177                 {
178                         pixbuf = mlt_properties_get_data( producer_props, "pixbuf", NULL );
179                         mlt_pool_release( this->image );
180                         mlt_pool_release( this->alpha );
181                         this->image = NULL;
182                         this->alpha = NULL;
183                 }
184         }
185         else if ( this->image == NULL || image_idx != this->image_idx )
186         {
187                 mlt_pool_release( this->image );
188                 mlt_pool_release( this->alpha );
189                 this->image = NULL;
190                 this->alpha = NULL;
191
192                 this->image_idx = image_idx;
193                 pixbuf = gdk_pixbuf_new_from_file( this->filenames[ image_idx ], &error );
194
195                 if ( pixbuf != NULL )
196                 {
197                         // Register this pixbuf for destruction and reuse
198                         mlt_properties_set_data( producer_props, "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
199
200                         mlt_properties_set_int( producer_props, "real_width", gdk_pixbuf_get_width( pixbuf ) );
201                         mlt_properties_set_int( producer_props, "real_height", gdk_pixbuf_get_height( pixbuf ) );
202
203                         // Store the width/height of the pixbuf temporarily
204                         this->width = gdk_pixbuf_get_width( pixbuf );
205                         this->height = gdk_pixbuf_get_height( pixbuf );
206                 }
207         }
208
209         // If we have a pixbuf
210         if ( pixbuf && width > 0 )
211         {
212                 char *interps = mlt_properties_get( properties, "rescale.interp" );
213                 int interp = GDK_INTERP_BILINEAR;
214
215                 if ( strcmp( interps, "nearest" ) == 0 )
216                         interp = GDK_INTERP_NEAREST;
217                 else if ( strcmp( interps, "tiles" ) == 0 )
218                         interp = GDK_INTERP_TILES;
219                 else if ( strcmp( interps, "hyper" ) == 0 )
220                         interp = GDK_INTERP_HYPER;
221
222                 // Note - the original pixbuf is already safe and ready for destruction
223                 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
224                 
225                 // Store width and height
226                 this->width = width;
227                 this->height = height;
228                 
229                 // Allocate/define image
230                 this->image = mlt_pool_alloc( width * ( height + 1 ) * 2 );
231
232                 // Extract YUV422 and alpha
233                 if ( gdk_pixbuf_get_has_alpha( pixbuf ) )
234                 {
235                         // Allocate the alpha mask
236                         this->alpha = mlt_pool_alloc( this->width * this->height );
237
238                         // Convert the image
239                         mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
240                                                                                   this->width, this->height,
241                                                                                   gdk_pixbuf_get_rowstride( pixbuf ),
242                                                                                   this->image, this->alpha );
243                 }
244                 else
245                 { 
246                         // No alpha to extract
247                         mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
248                                                                                  this->width, this->height,
249                                                                                  gdk_pixbuf_get_rowstride( pixbuf ),
250                                                                                  this->image );
251                 }
252
253                 // Finished with pixbuf now
254                 g_object_unref( pixbuf );
255         }
256
257         // Set width/height of frame
258         mlt_properties_set_int( properties, "width", this->width );
259         mlt_properties_set_int( properties, "height", this->height );
260         mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "real_width" ) );
261         mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "real_height" ) );
262
263         // pass the image data without destructor
264         mlt_properties_set_data( properties, "image", this->image, this->width * ( this->height + 1 ) * 2, NULL, NULL );
265         mlt_properties_set_data( properties, "alpha", this->alpha, this->width * this->height, NULL, NULL );
266 }
267
268 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
269 {
270         // Obtain properties of frame
271         mlt_properties properties = mlt_frame_properties( frame );
272
273         *width = mlt_properties_get_int( properties, "rescale_width" );
274         *height = mlt_properties_get_int( properties, "rescale_height" );
275
276         // Refresh the image
277         refresh_image( frame, *width, *height );
278
279         // Determine format
280         //mlt_producer this = mlt_properties_get_data( properties, "producer_pixbuf", NULL );
281         //*format = ( mlt_properties_get_int( mlt_producer_properties( this ), "bpp" ) == 4 ) ? mlt_image_rgb24a : mlt_image_rgb24;
282
283         // May need to know the size of the image to clone it
284         int size = 0;
285
286         // Get the image
287         uint8_t *image = mlt_properties_get_data( properties, "image", &size );
288
289         // Get width and height
290         *width = mlt_properties_get_int( properties, "width" );
291         *height = mlt_properties_get_int( properties, "height" );
292
293         // Clone if necessary
294         // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
295         // The fault is not in the design of mlt, but in the implementation of pixbuf...
296         //if ( writable )
297         {
298                 // Clone our image
299                 uint8_t *copy = mlt_pool_alloc( size );
300                 memcpy( copy, image, size );
301
302                 // We're going to pass the copy on
303                 image = copy;
304
305                 // Now update properties so we free the copy after
306                 mlt_properties_set_data( properties, "image", copy, size, mlt_pool_release, NULL );
307         }
308
309         // Pass on the image
310         *buffer = image;
311
312         return 0;
313 }
314
315 static uint8_t *producer_get_alpha_mask( mlt_frame this )
316 {
317         // Obtain properties of frame
318         mlt_properties properties = mlt_frame_properties( this );
319
320         // Return the alpha mask
321         return mlt_properties_get_data( properties, "alpha", NULL );
322 }
323
324 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
325 {
326         // Get the real structure for this producer
327         producer_pixbuf this = producer->child;
328
329         // Generate a frame
330         *frame = mlt_frame_init( );
331
332         // Obtain properties of frame and producer
333         mlt_properties properties = mlt_frame_properties( *frame );
334
335         // Set the producer on the frame properties
336         mlt_properties_set_data( properties, "producer_pixbuf", this, 0, NULL, NULL );
337
338         // Update timecode on the frame we're creating
339         mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
340
341         // Refresh the image
342         refresh_image( *frame, 0, 0 );
343
344         // Set producer-specific frame properties
345         mlt_properties_set_int( properties, "progressive", 1 );
346         mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( properties, "real_width" ) / mlt_properties_get_double( properties, "real_height" ) );
347
348         // Set alpha call back
349         ( *frame )->get_alpha_mask = producer_get_alpha_mask;
350
351         // Push the get_image method
352         mlt_frame_push_get_image( *frame, producer_get_image );
353
354         // Calculate the next timecode
355         mlt_producer_prepare_next( producer );
356
357         return 0;
358 }
359
360 static void producer_close( mlt_producer parent )
361 {
362         producer_pixbuf this = parent->child;
363         mlt_pool_release( this->image );
364         parent->close = NULL;
365         mlt_producer_close( parent );
366         free( this );
367 }
368