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>
36 static void load_filenames( producer_qimage this, mlt_properties producer_properties );
37 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
38 static void producer_close( mlt_producer parent );
40 mlt_producer producer_qimage_init( mlt_profile profile, mlt_service_type type, const char *id, char *filename )
42 producer_qimage this = calloc( sizeof( struct producer_qimage_s ), 1 );
43 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
45 mlt_producer producer = &this->parent;
47 // Get the properties interface
48 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
50 // Callback registration
54 producer->get_frame = producer_get_frame;
55 producer->close = ( mlt_destructor )producer_close;
57 // Set the default properties
58 mlt_properties_set( properties, "resource", filename );
59 mlt_properties_set_int( properties, "ttl", 25 );
60 mlt_properties_set_int( properties, "aspect_ratio", 1 );
61 mlt_properties_set_int( properties, "progressive", 1 );
63 // Validate the resource
65 load_filenames( this, properties );
68 mlt_frame frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
71 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
72 mlt_properties_set_data( frame_properties, "producer_qimage", this, 0, NULL, NULL );
73 mlt_frame_set_position( frame, mlt_producer_position( producer ) );
74 mlt_properties_set_position( frame_properties, "qimage_position", mlt_producer_position( producer ) );
75 refresh_qimage( this, frame, 0, 0 );
76 mlt_frame_close( frame );
79 if ( this->current_width == 0 )
81 producer_close( producer );
85 pthread_mutex_init( &this->mutex, NULL );
92 static void load_filenames( producer_qimage this, mlt_properties producer_properties )
94 char *filename = mlt_properties_get( producer_properties, "resource" );
95 this->filenames = mlt_properties_new( );
98 if ( strstr( filename, "<svg" ) )
100 make_tempfile( this, filename );
103 else if ( strchr( filename, '%' ) != NULL )
105 // handle picture sequences
106 int i = mlt_properties_get_int( producer_properties, "begin" );
115 snprintf( full, 1023, filename, i ++ );
116 if ( stat( full, &buf ) == 0 )
118 sprintf( key, "%d", keyvalue ++ );
119 mlt_properties_set( this->filenames, key, full );
127 if ( mlt_properties_count( this->filenames ) > 0 )
128 mlt_properties_set_int( producer_properties, "ttl", 1 );
130 else if ( strstr( filename, "/.all." ) != NULL )
132 char wildcard[ 1024 ];
133 char *dir_name = strdup( filename );
134 char *extension = strrchr( dir_name, '.' );
136 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
137 sprintf( wildcard, "*%s", extension );
139 mlt_properties_dir_list( this->filenames, dir_name, wildcard, 1 );
145 mlt_properties_set( this->filenames, "0", filename );
148 this->count = mlt_properties_count( this->filenames );
151 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
155 // Obtain properties of frame
156 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
158 // Obtain the producer for this frame
159 producer_qimage this = mlt_properties_get_data( properties, "producer_qimage", NULL );
161 *width = mlt_properties_get_int( properties, "rescale_width" );
162 *height = mlt_properties_get_int( properties, "rescale_height" );
164 mlt_service_lock( MLT_PRODUCER_SERVICE( &this->parent ) );
167 refresh_qimage( this, frame, *width, *height );
169 // Get width and height (may have changed during the refresh)
170 *width = mlt_properties_get_int( properties, "width" );
171 *height = mlt_properties_get_int( properties, "height" );
173 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
174 // The fault is not in the design of mlt, but in the implementation of the qimage producer...
175 if ( this->current_image )
177 // Clone the image and the alpha
178 int image_size = this->current_width * ( this->current_height + 1 ) * ( this->has_alpha ? 4 :3 );
179 uint8_t *image_copy = mlt_pool_alloc( image_size );
180 memcpy( image_copy, this->current_image, image_size );
181 // Now update properties so we free the copy after
182 mlt_frame_set_image( frame, image_copy, image_size, mlt_pool_release );
183 // We're going to pass the copy on
184 *buffer = image_copy;
185 *format = this->has_alpha ? mlt_image_rgb24a : mlt_image_rgb24;
186 mlt_log_debug( MLT_PRODUCER_SERVICE( &this->parent ), "%dx%d (%s)\n",
187 this->current_width, this->current_height, mlt_image_format_name( *format ) );
194 // Release references and locks
195 pthread_mutex_unlock( &this->mutex );
196 mlt_cache_item_close( this->image_cache );
197 mlt_service_unlock( MLT_PRODUCER_SERVICE( &this->parent ) );
202 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
204 // Get the real structure for this producer
205 producer_qimage this = producer->child;
207 // Fetch the producers properties
208 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
210 if ( this->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
211 load_filenames( this, producer_properties );
214 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
216 if ( *frame != NULL && this->count > 0 )
218 // Obtain properties of frame and producer
219 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
221 // Set the producer on the frame properties
222 mlt_properties_set_data( properties, "producer_qimage", this, 0, NULL, NULL );
224 // Update timecode on the frame we're creating
225 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
227 // Ensure that we have a way to obtain the position in the get_image
228 mlt_properties_set_position( properties, "qimage_position", mlt_producer_position( producer ) );
231 refresh_qimage( this, *frame, 0, 0 );
233 // Set producer-specific frame properties
234 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( producer_properties, "progressive" ) );
235 double force_ratio = mlt_properties_get_double( producer_properties, "force_aspect_ratio" );
236 if ( force_ratio > 0.0 )
237 mlt_properties_set_double( properties, "aspect_ratio", force_ratio );
239 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
241 // Push the get_image method
242 mlt_frame_push_get_image( *frame, producer_get_image );
245 // Calculate the next timecode
246 mlt_producer_prepare_next( producer );
251 static void producer_close( mlt_producer parent )
253 producer_qimage this = parent->child;
254 pthread_mutex_destroy( &this->mutex );
255 parent->close = NULL;
256 mlt_service_cache_purge( MLT_PRODUCER_SERVICE(parent) );
257 mlt_producer_close( parent );
258 mlt_properties_close( this->filenames );