2 * producer_image.c -- a QT/QImage based producer for MLT
3 * Copyright (C) 2006 Visual Media
4 * Author: Charles Yates <charles.yates@gmail.com>
6 * NB: This module is designed to be functionally equivalent to the
7 * gtk2 image loading module so it can be used as replacement.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <framework/mlt_producer.h>
25 #include <framework/mlt_cache.h>
26 #include "qimage_wrapper.h"
32 #include <sys/types.h>
37 static void load_filenames( producer_qimage self, mlt_properties producer_properties );
38 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
39 static void producer_close( mlt_producer parent );
41 mlt_producer producer_qimage_init( mlt_profile profile, mlt_service_type type, const char *id, char *filename )
43 producer_qimage self = calloc( 1, sizeof( struct producer_qimage_s ) );
44 if ( self != NULL && mlt_producer_init( &self->parent, self ) == 0 )
46 mlt_producer producer = &self->parent;
48 // Get the properties interface
49 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &self->parent );
51 // Initialize KDE image plugins
54 // Callback registration
55 producer->get_frame = producer_get_frame;
56 producer->close = ( mlt_destructor )producer_close;
58 // Set the default properties
59 mlt_properties_set( properties, "resource", filename );
60 mlt_properties_set_int( properties, "ttl", 25 );
61 mlt_properties_set_int( properties, "aspect_ratio", 1 );
62 mlt_properties_set_int( properties, "progressive", 1 );
63 mlt_properties_set_int( properties, "seekable", 1 );
65 // Validate the resource
67 load_filenames( self, properties );
70 mlt_frame frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
73 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
74 mlt_properties_set_data( frame_properties, "producer_qimage", self, 0, NULL, NULL );
75 mlt_frame_set_position( frame, mlt_producer_position( producer ) );
76 refresh_qimage( self, frame );
77 mlt_cache_item_close( self->qimage_cache );
78 mlt_frame_close( frame );
81 if ( self->current_width == 0 )
83 producer_close( producer );
92 static int load_svg( producer_qimage self, mlt_properties properties, const char *filename )
97 if ( strstr( filename, "<svg" ) )
99 make_tempfile( self, filename );
105 static int load_sequence_sprintf( producer_qimage self, mlt_properties properties, const char *filename )
109 // Obtain filenames with pattern
110 if ( strchr( filename, '%' ) != NULL )
112 // handle picture sequences
113 int i = mlt_properties_get_int( properties, "begin" );
122 snprintf( full, 1023, filename, i ++ );
123 if ( stat( full, &buf ) == 0 )
125 sprintf( key, "%d", keyvalue ++ );
126 mlt_properties_set( self->filenames, key, full );
134 if ( mlt_properties_count( self->filenames ) > 0 )
136 mlt_properties_set_int( properties, "ttl", 1 );
143 static int load_sequence_deprecated( producer_qimage self, mlt_properties properties, const char *filename )
148 // Obtain filenames with pattern containing a begin value, e.g. foo%1234d.png
149 if ( ( start = strchr( filename, '%' ) ) )
151 const char *end = ++start;
152 while ( isdigit( *end ) ) end++;
153 if ( end > start && ( end[0] == 'd' || end[0] == 'i' || end[0] == 'u' ) )
156 char *s = calloc( 1, n + 1 );
157 strncpy( s, start, n );
158 mlt_properties_set( properties, "begin", s );
160 s = calloc( 1, strlen( filename ) + 2 );
161 strncpy( s, filename, start - filename );
162 sprintf( s + ( start - filename ), ".%d%s", n, end );
163 result = load_sequence_sprintf( self, properties, s );
170 static int load_sequence_querystring( producer_qimage self, mlt_properties properties, const char *filename )
174 // Obtain filenames with pattern and begin value in query string
175 if ( strchr( filename, '%' ) && strchr( filename, '?' ) )
177 // Split filename into pattern and query string
178 char *s = strdup( filename );
179 char *querystring = strrchr( s, '?' );
180 *querystring++ = '\0';
181 if ( strstr( filename, "begin=" ) )
182 mlt_properties_set( properties, "begin", strstr( querystring, "begin=" ) + 6 );
183 else if ( strstr( filename, "begin:" ) )
184 mlt_properties_set( properties, "begin", strstr( querystring, "begin:" ) + 6 );
185 // Coerce to an int value so serialization does not have any extra query string cruft
186 mlt_properties_set_int( properties, "begin", mlt_properties_get_int( properties, "begin" ) );
187 result = load_sequence_sprintf( self, properties, s );
193 static int load_folder( producer_qimage self, mlt_properties properties, const char *filename )
197 // Obtain filenames within folder
198 if ( strstr( filename, "/.all." ) != NULL )
200 char wildcard[ 1024 ];
201 char *dir_name = strdup( filename );
202 char *extension = strrchr( dir_name, '.' );
204 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
205 sprintf( wildcard, "*%s", extension );
207 mlt_properties_dir_list( self->filenames, dir_name, wildcard, 1 );
215 static void load_filenames( producer_qimage self, mlt_properties properties )
217 char *filename = mlt_properties_get( properties, "resource" );
218 self->filenames = mlt_properties_new( );
220 if (!load_svg( self, properties, filename ) &&
221 !load_sequence_querystring( self, properties, filename ) &&
222 !load_sequence_sprintf( self, properties, filename ) &&
223 !load_sequence_deprecated( self, properties, filename ) &&
224 !load_folder( self, properties, filename ) )
226 mlt_properties_set( self->filenames, "0", filename );
228 self->count = mlt_properties_count( self->filenames );
231 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
235 // Obtain properties of frame and producer
236 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
237 producer_qimage self = mlt_properties_get_data( properties, "producer_qimage", NULL );
238 mlt_producer producer = &self->parent;
240 *width = mlt_properties_get_int( properties, "rescale_width" );
241 *height = mlt_properties_get_int( properties, "rescale_height" );
243 mlt_service_lock( MLT_PRODUCER_SERVICE( &self->parent ) );
246 self->qimage_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qimage.qimage" );
247 self->qimage = mlt_cache_item_data( self->qimage_cache, NULL );
248 self->image_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qimage.image" );
249 self->current_image = mlt_cache_item_data( self->image_cache, NULL );
250 self->alpha_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qimage.alpha" );
251 self->current_alpha = mlt_cache_item_data( self->alpha_cache, NULL );
252 refresh_image( self, frame, *format, *width, *height );
254 // Get width and height (may have changed during the refresh)
255 *width = mlt_properties_get_int( properties, "width" );
256 *height = mlt_properties_get_int( properties, "height" );
257 *format = self->format;
259 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
260 // The fault is not in the design of mlt, but in the implementation of the qimage producer...
261 if ( self->current_image )
263 // Clone the image and the alpha
264 int image_size = mlt_image_format_size( self->format, self->current_width, self->current_height, NULL );
265 uint8_t *image_copy = mlt_pool_alloc( image_size );
266 memcpy( image_copy, self->current_image, image_size );
267 // Now update properties so we free the copy after
268 mlt_frame_set_image( frame, image_copy, image_size, mlt_pool_release );
269 // We're going to pass the copy on
270 *buffer = image_copy;
271 mlt_log_debug( MLT_PRODUCER_SERVICE( &self->parent ), "%dx%d (%s)\n",
272 self->current_width, self->current_height, mlt_image_format_name( *format ) );
273 // Clone the alpha channel
274 if ( self->current_alpha )
276 image_copy = mlt_pool_alloc( self->current_width * self->current_height );
277 memcpy( image_copy, self->current_alpha, self->current_width * self->current_height );
278 mlt_frame_set_alpha( frame, image_copy, self->current_width * self->current_height, mlt_pool_release );
286 // Release references and locks
287 mlt_cache_item_close( self->qimage_cache );
288 mlt_cache_item_close( self->image_cache );
289 mlt_cache_item_close( self->alpha_cache );
290 mlt_service_unlock( MLT_PRODUCER_SERVICE( &self->parent ) );
295 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
297 // Get the real structure for this producer
298 producer_qimage self = producer->child;
300 // Fetch the producers properties
301 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
303 if ( self->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
304 load_filenames( self, producer_properties );
307 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
309 if ( *frame != NULL && self->count > 0 )
311 // Obtain properties of frame and producer
312 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
314 // Set the producer on the frame properties
315 mlt_properties_set_data( properties, "producer_qimage", self, 0, NULL, NULL );
317 // Update timecode on the frame we're creating
318 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
321 self->qimage_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qimage.qimage" );
322 self->qimage = mlt_cache_item_data( self->qimage_cache, NULL );
323 refresh_qimage( self, *frame );
324 mlt_cache_item_close( self->qimage_cache );
326 // Set producer-specific frame properties
327 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( producer_properties, "progressive" ) );
328 double force_ratio = mlt_properties_get_double( producer_properties, "force_aspect_ratio" );
329 if ( force_ratio > 0.0 )
330 mlt_properties_set_double( properties, "aspect_ratio", force_ratio );
332 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
334 // Push the get_image method
335 mlt_frame_push_get_image( *frame, producer_get_image );
338 // Calculate the next timecode
339 mlt_producer_prepare_next( producer );
344 static void producer_close( mlt_producer parent )
346 producer_qimage self = parent->child;
347 parent->close = NULL;
348 mlt_service_cache_purge( MLT_PRODUCER_SERVICE(parent) );
349 mlt_producer_close( parent );
350 mlt_properties_close( self->filenames );