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>
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.
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.
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.
21 #include "producer_pixbuf.h"
22 #include <framework/mlt_frame.h>
23 #include <gdk-pixbuf/gdk-pixbuf.h>
29 #include <sys/types.h>
34 static int producer_get_frame( mlt_producer parent
, mlt_frame_ptr frame
, int index
);
35 static void producer_close( mlt_producer parent
);
43 static int filter_files( const struct dirent
*de
)
45 if ( de
->d_name
[ 0 ] != '.' )
52 mlt_producer
producer_pixbuf_init( char *filename
)
54 producer_pixbuf
this = calloc( sizeof( struct producer_pixbuf_s
), 1 );
55 if ( filename
!= NULL
&& this != NULL
&& mlt_producer_init( &this->parent
, this ) == 0 )
57 mlt_producer producer
= &this->parent
;
59 producer
->get_frame
= producer_get_frame
;
60 producer
->close
= producer_close
;
62 // Get the properties interface
63 mlt_properties properties
= mlt_producer_properties( &this->parent
);
65 // Set the default properties
66 mlt_properties_set( properties
, "resource", filename
);
67 mlt_properties_set_int( properties
, "ttl", 25 );
70 if ( strchr( filename
, '%' ) != NULL
)
72 // handle picture sequences
80 snprintf( full
, 1023, filename
, i
++ );
81 if ( stat( full
, &buf
) == 0 )
83 this->filenames
= realloc( this->filenames
, sizeof( char * ) * ( this->count
+ 1 ) );
84 this->filenames
[ this->count
++ ] = strdup( full
);
92 mlt_properties_set_position( properties
, "out", this->count
* 25 );
94 else if ( strstr( filename
, "/.all." ) != NULL
)
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
);
106 for (i
= 0; i
< n
; i
++ )
108 snprintf( fullname
, 1023, "%s%s", dir_name
, de
[i
]->d_name
);
110 if ( lstat( fullname
, &info
) == 0 &&
111 ( S_ISREG( info
.st_mode
) || ( strstr( fullname
, extension
) && info
.st_mode
| S_IXUSR
) ) )
113 this->filenames
= realloc( this->filenames
, sizeof( char * ) * ( this->count
+ 1 ) );
114 this->filenames
[ this->count
++ ] = strdup( fullname
);
124 this->filenames
= realloc( this->filenames
, sizeof( char * ) * ( this->count
+ 1 ) );
125 this->filenames
[ this->count
++ ] = strdup( filename
);
126 mlt_properties_set_position( properties
, "out", 25 );
129 // Initialise gobject types
138 static void refresh_image( mlt_frame frame
, int width
, int height
)
141 GdkPixbuf
*pixbuf
= NULL
;
142 GError
*error
= NULL
;
144 // Obtain properties of frame
145 mlt_properties properties
= mlt_frame_properties( frame
);
147 // Obtain the producer pango for this frame
148 producer_pixbuf
this = mlt_properties_get_data( properties
, "producer_pixbuf", NULL
);
150 // Obtain the producer
151 mlt_producer producer
= &this->parent
;
153 // Obtain properties of producer
154 mlt_properties producer_props
= mlt_producer_properties( producer
);
156 // Get the time to live for each frame
157 double ttl
= mlt_properties_get_int( producer_props
, "ttl" );
160 int image_idx
= ( int )floor( mlt_producer_position( producer
) / ttl
) % this->count
;
162 // Update timecode on the frame we're creating
163 mlt_frame_set_position( frame
, mlt_producer_position( producer
) );
165 // optimization for subsequent iterations on single picture
166 if ( this->image
!= NULL
&& image_idx
== this->image_idx
)
168 if ( width
!= this->width
|| height
!= this->height
)
170 pixbuf
= mlt_properties_get_data( producer_props
, "pixbuf", NULL
);
179 this->image_idx
= image_idx
;
180 pixbuf
= gdk_pixbuf_new_from_file( this->filenames
[ image_idx
], &error
);
182 if ( pixbuf
!= NULL
)
184 // Register this pixbuf for destruction and reuse
185 mlt_properties_set_data( producer_props
, "pixbuf", pixbuf
, 0, ( mlt_destructor
)g_object_unref
, NULL
);
187 mlt_properties_set_int( producer_props
, "real_width", gdk_pixbuf_get_width( pixbuf
) );
188 mlt_properties_set_int( producer_props
, "real_height", gdk_pixbuf_get_height( pixbuf
) );
190 // Store the width/height of the pixbuf temporarily
191 this->width
= gdk_pixbuf_get_width( pixbuf
);
192 this->height
= gdk_pixbuf_get_height( pixbuf
);
196 // If we have a pixbuf
197 if ( pixbuf
&& width
> 0 )
199 // Note - the original pixbuf is already safe and ready for destruction
200 pixbuf
= gdk_pixbuf_scale_simple( pixbuf
, width
, height
, GDK_INTERP_HYPER
);
202 // Store width and height
203 this->width
= gdk_pixbuf_get_width( pixbuf
);
204 this->height
= gdk_pixbuf_get_height( pixbuf
);
206 // Allocate/define image and alpha
207 uint8_t *image
= malloc( this->width
* this->height
* 2 );
208 uint8_t *alpha
= NULL
;
210 // Extract YUV422 and alpha
211 if ( gdk_pixbuf_get_has_alpha( pixbuf
) )
213 // Allocate the alpha mask
214 alpha
= malloc( this->width
* this->height
);
217 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf
),
218 this->width
, this->height
,
219 gdk_pixbuf_get_rowstride( pixbuf
),
224 // No alpha to extract
225 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf
),
226 this->width
, this->height
,
227 gdk_pixbuf_get_rowstride( pixbuf
),
231 // Finished with pixbuf now
232 g_object_unref( pixbuf
);
234 // Pass alpha and image on properties with or without destructor
239 // Set width/height of frame
240 mlt_properties_set_int( properties
, "width", this->width
);
241 mlt_properties_set_int( properties
, "height", this->height
);
242 mlt_properties_set_int( properties
, "real_width", mlt_properties_get_int( producer_props
, "real_width" ) );
243 mlt_properties_set_int( properties
, "real_height", mlt_properties_get_int( producer_props
, "real_height" ) );
245 // pass the image and alpha data without destructor
246 mlt_properties_set_data( properties
, "image", this->image
, this->width
* this->height
* 2, NULL
, NULL
);
247 mlt_properties_set_data( properties
, "alpha", this->alpha
, this->width
* this->height
, NULL
, NULL
);
250 static int producer_get_image( mlt_frame frame
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
252 // Obtain properties of frame
253 mlt_properties properties
= mlt_frame_properties( frame
);
256 refresh_image( frame
, *width
, *height
);
258 // May need to know the size of the image to clone it
262 uint8_t *image
= mlt_properties_get_data( properties
, "image", &size
);
264 // Get width and height
265 *width
= mlt_properties_get_int( properties
, "width" );
266 *height
= mlt_properties_get_int( properties
, "height" );
268 // Clone if necessary
269 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
270 // The fault is not in the design of mlt, but in the implementation of pixbuf...
273 size
= *width
* *height
* 2;
276 uint8_t *copy
= malloc( size
);
277 memcpy( copy
, image
, size
);
279 // We're going to pass the copy on
282 // Now update properties so we free the copy after
283 mlt_properties_set_data( properties
, "image", copy
, size
, free
, NULL
);
292 static uint8_t *producer_get_alpha_mask( mlt_frame
this )
294 // Obtain properties of frame
295 mlt_properties properties
= mlt_frame_properties( this );
297 // Return the alpha mask
298 return mlt_properties_get_data( properties
, "alpha", NULL
);
301 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
303 // Get the real structure for this producer
304 producer_pixbuf
this = producer
->child
;
307 *frame
= mlt_frame_init( );
309 // Obtain properties of frame and producer
310 mlt_properties properties
= mlt_frame_properties( *frame
);
312 // Set the producer on the frame properties
313 mlt_properties_set_data( properties
, "producer_pixbuf", this, 0, NULL
, NULL
);
315 // Refresh the pango image
316 refresh_image( *frame
, 0, 0 );
318 // Set alpha call back
319 ( *frame
)->get_alpha_mask
= producer_get_alpha_mask
;
321 // Push the get_image method
322 mlt_frame_push_get_image( *frame
, producer_get_image
);
324 // Calculate the next timecode
325 mlt_producer_prepare_next( producer
);
330 static void producer_close( mlt_producer parent
)
332 producer_pixbuf
this = parent
->child
;
335 parent
->close
= NULL
;
336 mlt_producer_close( parent
);