]> git.sesse.net Git - mlt/blob - src/modules/gtk2/producer_pixbuf.c
incomplete next/prev clip behaviour
[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 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
35 static void producer_close( mlt_producer parent );
36
37 typedef enum
38 {
39         SIGNAL_FORMAT_PAL,
40         SIGNAL_FORMAT_NTSC
41 } mlt_signal_format;
42
43 static int filter_files( const struct dirent *de )
44 {
45         if ( de->d_name[ 0 ] != '.' )
46                 return 1;
47         else
48                 return 0;
49 }
50
51
52 mlt_producer producer_pixbuf_init( const char *filename )
53 {
54         producer_pixbuf this = calloc( sizeof( struct producer_pixbuf_s ), 1 );
55         if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
56         {
57                 mlt_producer producer = &this->parent;
58
59                 producer->get_frame = producer_get_frame;
60                 producer->close = producer_close;
61
62                 // Get the properties interface
63                 mlt_properties properties = mlt_producer_properties( &this->parent );
64         
65                 // Set the default properties
66                 mlt_properties_set_int( properties, "video_standard", mlt_video_standard_pal );
67                 mlt_properties_set_double( properties, "ttl", 5 );
68                 
69                 // Obtain filenames
70                 if ( strchr( filename, '%' ) != NULL )
71                 {
72                         // handle picture sequences
73                         int i = 0;
74                         int gap = 0;
75                         char full[1024];
76
77                         while ( gap < 100 )
78                         {
79                                 struct stat buf;
80                                 snprintf( full, 1023, filename, i ++ );
81                                 if ( stat( full, &buf ) == 0 )
82                                 {
83                                         this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
84                                         this->filenames[ this->count ++ ] = strdup( full );
85                                         gap = 0;
86                                 }
87                                 else
88                                 {
89                                         gap ++;
90                                 }
91                         } 
92                         mlt_properties_set_timecode( properties, "out", this->count );
93                 }
94                 else if ( strstr( filename, "/.all." ) != NULL )
95                 {
96                         char *dir_name = strdup( filename );
97                         char *extension = strrchr( filename, '.' );
98                         *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
99                         char fullname[ 1024 ];
100                         strcpy( fullname, dir_name );
101                         struct dirent **de = NULL;
102                         int n = scandir( fullname, &de, filter_files, alphasort );
103                         int i;
104                         struct stat info;
105
106                         for (i = 0; i < n; i++ )
107                         {
108                                 snprintf( fullname, 1023, "%s%s", dir_name, de[i]->d_name );
109
110                                 if ( lstat( fullname, &info ) == 0 && 
111                                         ( S_ISREG( info.st_mode ) || ( strstr( fullname, extension ) && info.st_mode | S_IXUSR ) ) )
112                                 {
113                                         this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
114                                         this->filenames[ this->count ++ ] = strdup( fullname );
115                                 }
116                                 free( de[ i ] );
117                         }
118
119                         mlt_properties_set_timecode( properties, "out", this->count );
120                         free( de );
121                         free( dir_name );
122                 }
123                 else
124                 {
125                         this->filenames = realloc( this->filenames, sizeof( char * ) * ( this->count + 1 ) );
126                         this->filenames[ this->count ++ ] = strdup( filename );
127                         mlt_properties_set_timecode( properties, "out", 1 );
128                 }
129
130                 // Initialise gobject types
131                 g_type_init();
132
133                 return producer;
134         }
135         free( this );
136         return NULL;
137 }
138
139 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
140 {
141         // Obtain properties of frame
142         mlt_properties properties = mlt_frame_properties( this );
143
144         // May need to know the size of the image to clone it
145         int size = 0;
146
147         // Get the image
148         uint8_t *image = mlt_properties_get_data( properties, "image", &size );
149
150         // Get width and height
151         *width = mlt_properties_get_int( properties, "width" );
152         *height = mlt_properties_get_int( properties, "height" );
153
154         // Clone if necessary
155         // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
156         // The fault is not in the design of mlt, but in the implementation of pixbuf...
157         //if ( writable )
158         {
159                 size = *width * *height * 2;
160
161                 // Clone our image
162                 uint8_t *copy = malloc( size );
163                 memcpy( copy, image, size );
164
165                 // We're going to pass the copy on
166                 image = copy;
167
168                 // Now update properties so we free the copy after
169                 mlt_properties_set_data( properties, "image", copy, size, free, NULL );
170         }
171
172         // Pass on the image
173         *buffer = image;
174
175         return 0;
176 }
177
178 static uint8_t *producer_get_alpha_mask( mlt_frame this )
179 {
180         // Obtain properties of frame
181         mlt_properties properties = mlt_frame_properties( this );
182
183         // Return the alpha mask
184         return mlt_properties_get_data( properties, "alpha", NULL );
185 }
186
187 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
188 {
189         producer_pixbuf this = producer->child;
190         GdkPixbuf *pixbuf = NULL;
191         GError *error = NULL;
192
193         // Generate a frame
194         *frame = mlt_frame_init( );
195
196         // Obtain properties of frame
197         mlt_properties properties = mlt_frame_properties( *frame );
198
199         // Get the time to live for each frame
200         double ttl = mlt_properties_get_double( mlt_producer_properties( producer ), "ttl" );
201
202         // Image index
203         int image_idx = ( int )floor( mlt_producer_position( producer ) / ttl ) % this->count;
204
205         // Update timecode on the frame we're creating
206         mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
207
208     // optimization for subsequent iterations on single picture
209         if ( this->image != NULL && image_idx == this->image_idx )
210         {
211                 // Set width/height
212                 mlt_properties_set_int( properties, "width", this->width );
213                 mlt_properties_set_int( properties, "height", this->height );
214
215                 // if picture sequence pass the image and alpha data without destructor
216                 mlt_properties_set_data( properties, "image", this->image, 0, NULL, NULL );
217                 mlt_properties_set_data( properties, "alpha", this->alpha, 0, NULL, NULL );
218
219                 // Set alpha mask call back
220         ( *frame )->get_alpha_mask = producer_get_alpha_mask;
221
222                 // Stack the get image callback
223                 mlt_frame_push_get_image( *frame, producer_get_image );
224         }
225         else 
226         {
227                 free( this->image );
228                 free( this->alpha );
229                 this->image_idx = image_idx;
230                 pixbuf = gdk_pixbuf_new_from_file( this->filenames[ image_idx ], &error );
231         }
232
233         // If we have a pixbuf
234         if ( pixbuf )
235         {
236                 // Scale to adjust for sample aspect ratio
237                 if ( mlt_properties_get_int( properties, "video_standard" ) == mlt_video_standard_pal )
238                 {
239                         GdkPixbuf *temp = pixbuf;
240                         GdkPixbuf *scaled = gdk_pixbuf_scale_simple( pixbuf,
241                                 (gint) ( (float) gdk_pixbuf_get_width( pixbuf ) * 54.0/59.0),
242                                 gdk_pixbuf_get_height( pixbuf ), GDK_INTERP_HYPER );
243                         pixbuf = scaled;
244                         g_object_unref( temp );
245                 }
246                 else
247                 {
248                         GdkPixbuf *temp = pixbuf;
249                         GdkPixbuf *scaled = gdk_pixbuf_scale_simple( pixbuf,
250                                 (gint) ( (float) gdk_pixbuf_get_width( pixbuf ) * 11.0/10.0 ),
251                                 gdk_pixbuf_get_height( pixbuf ), GDK_INTERP_HYPER );
252                         pixbuf = scaled;
253                         g_object_unref( temp );
254                 }
255
256                 // Store width and height
257                 this->width = gdk_pixbuf_get_width( pixbuf );
258                 this->height = gdk_pixbuf_get_height( pixbuf );
259                 this->width -= this->width % 4;
260                 this->height -= this->height % 2;
261
262                 // Allocate/define image and alpha
263                 uint8_t *image = malloc( this->width * this->height * 2 );
264                 uint8_t *alpha = NULL;
265
266                 // Extract YUV422 and alpha
267                 if ( gdk_pixbuf_get_has_alpha( pixbuf ) )
268                 {
269                         // Allocate the alpha mask
270                         alpha = malloc( this->width * this->height );
271
272                         // Convert the image
273                         mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
274                                                                                   this->width, this->height,
275                                                                                   gdk_pixbuf_get_rowstride( pixbuf ),
276                                                                                   image, alpha );
277                 }
278                 else
279                 { 
280                         // No alpha to extract
281                         mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
282                                                                                  this->width, this->height,
283                                                                                  gdk_pixbuf_get_rowstride( pixbuf ),
284                                                                                  image );
285                 }
286
287                 // Finished with pixbuf now
288                 g_object_unref( pixbuf );
289
290                 // Set width/height of frame
291                 mlt_properties_set_int( properties, "width", this->width );
292                 mlt_properties_set_int( properties, "height", this->height );
293
294                 // Pass alpha and image on properties with or without destructor
295                 this->image = image;
296                 this->alpha = alpha;
297
298                 // pass the image and alpha data without destructor
299                 mlt_properties_set_data( properties, "image", image, 0, NULL, NULL );
300                 mlt_properties_set_data( properties, "alpha", alpha, 0, NULL, NULL );
301
302                 // Set alpha call back
303                 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
304
305                 // Push the get_image method
306                 mlt_frame_push_get_image( *frame, producer_get_image );
307         }
308
309         // Calculate the next timecode
310         mlt_producer_prepare_next( producer );
311
312         return 0;
313 }
314
315 static void producer_close( mlt_producer parent )
316 {
317         producer_pixbuf this = parent->child;
318         if ( this->image )
319                 free( this->image );
320         if ( this->alpha )
321                 free( this->alpha );
322         parent->close = NULL;
323         mlt_producer_close( parent );
324         free( this );
325 }
326