qimage module: add mutex, fix caching and use alpha only if necessary (mostly borrowe...
[melted] / src / modules / qimage / producer_qimage.c
1 /*
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>
5 *
6 * NB: This module is designed to be functionally equivalent to the
7 * gtk2 image loading module so it can be used as replacement.
8 *
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.
13 *
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.
18 *
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.
22 */
23
24 #include <framework/mlt_producer.h>
25 #include "qimage_wrapper.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <math.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34
35 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
36 static void producer_close( mlt_producer parent );
37
38 mlt_producer producer_qimage_init( mlt_profile profile, mlt_service_type type, const char *id, char *filename )
39 {
40 producer_qimage this = calloc( sizeof( struct producer_qimage_s ), 1 );
41 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
42 {
43 mlt_producer producer = &this->parent;
44
45 // Get the properties interface
46 mlt_properties properties = MLT_PRODUCER_PROPERTIES( &this->parent );
47
48 // Callback registration
49 #ifdef USE_KDE
50 init_qimage();
51 #endif
52 producer->get_frame = producer_get_frame;
53 producer->close = ( mlt_destructor )producer_close;
54
55 // Set the default properties
56 mlt_properties_set( properties, "resource", filename );
57 mlt_properties_set_int( properties, "ttl", 25 );
58 mlt_properties_set_int( properties, "aspect_ratio", 1 );
59 mlt_properties_set_int( properties, "progressive", 1 );
60
61 return producer;
62 }
63 free( this );
64 return NULL;
65 }
66
67 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
68 {
69 // Obtain properties of frame
70 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
71
72 // We need to know the size of the image to clone it
73 int image_size = 0;
74 int alpha_size = 0;
75
76 // Alpha channel
77 uint8_t *alpha = NULL;
78
79 *width = mlt_properties_get_int( properties, "rescale_width" );
80 *height = mlt_properties_get_int( properties, "rescale_height" );
81
82 // Refresh the image
83 refresh_qimage( frame, *width, *height );
84
85 // Get the image
86 *buffer = mlt_properties_get_data( properties, "image", &image_size );
87 alpha = mlt_properties_get_data( properties, "alpha", &alpha_size );
88
89 // Get width and height (may have changed during the refresh)
90 *width = mlt_properties_get_int( properties, "width" );
91 *height = mlt_properties_get_int( properties, "height" );
92
93 // NB: Cloning is necessary with this producer (due to processing of images ahead of use)
94 // The fault is not in the design of mlt, but in the implementation of the qimage producer...
95 if ( *buffer != NULL )
96 {
97 if ( *format == mlt_image_yuv422 || *format == mlt_image_yuv420p )
98 {
99 // Clone the image and the alpha
100 uint8_t *image_copy = mlt_pool_alloc( image_size );
101 uint8_t *alpha_copy = mlt_pool_alloc( alpha_size );
102
103 memcpy( image_copy, *buffer, image_size );
104
105 // Copy or default the alpha
106 if ( alpha != NULL )
107 memcpy( alpha_copy, alpha, alpha_size );
108 else
109 memset( alpha_copy, 255, alpha_size );
110
111 // Now update properties so we free the copy after
112 mlt_properties_set_data( properties, "image", image_copy, image_size, mlt_pool_release, NULL );
113 mlt_properties_set_data( properties, "alpha", alpha_copy, alpha_size, mlt_pool_release, NULL );
114
115 // We're going to pass the copy on
116 *buffer = image_copy;
117 }
118 else if ( *format == mlt_image_rgb24a )
119 {
120 // Clone the image and the alpha
121 image_size = *width * ( *height + 1 ) * 4;
122 alpha_size = *width * ( *height + 1 );
123 uint8_t *image_copy = mlt_pool_alloc( image_size );
124 uint8_t *alpha_copy = mlt_pool_alloc( alpha_size );
125
126 mlt_convert_yuv422_to_rgb24a(*buffer, image_copy, (*width)*(*height));
127
128 // Now update properties so we free the copy after
129 mlt_properties_set_data( properties, "image", image_copy, image_size, mlt_pool_release, NULL );
130 mlt_properties_set_data( properties, "alpha", alpha_copy, alpha_size, mlt_pool_release, NULL );
131
132 // We're going to pass the copy on
133 *buffer = image_copy;
134 }
135 }
136 else
137 {
138 // TODO: Review all cases of invalid images
139 *buffer = mlt_pool_alloc( 50 * 50 * 2 );
140 mlt_properties_set_data( properties, "image", *buffer, image_size, mlt_pool_release, NULL );
141 *width = 50;
142 *height = 50;
143 }
144
145 return 0;
146 }
147
148 static uint8_t *producer_get_alpha_mask( mlt_frame this )
149 {
150 // Obtain properties of frame
151 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
152
153 // Return the alpha mask
154 return mlt_properties_get_data( properties, "alpha", NULL );
155 }
156
157 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
158 {
159 // Get the real structure for this producer
160 producer_qimage this = producer->child;
161
162 // Fetch the producers properties
163 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
164
165 if ( this->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
166 {
167 char *filename = mlt_properties_get( producer_properties, "resource" );
168 this->filenames = mlt_properties_new( );
169
170 // Read xml string
171 if ( strstr( filename, "<svg" ) )
172 {
173 // Generate a temporary file for the svg
174 char fullname[ 1024 ] = "/tmp/mlt.XXXXXX";
175 int fd = mkstemp( fullname );
176
177 if ( fd > -1 )
178 {
179 // Write the svg into the temp file
180 ssize_t remaining_bytes;
181 char *xml = filename;
182
183 // Strip leading crap
184 while ( xml[0] != '<' )
185 xml++;
186
187 remaining_bytes = strlen( xml );
188 while ( remaining_bytes > 0 )
189 remaining_bytes -= write( fd, xml + strlen( xml ) - remaining_bytes, remaining_bytes );
190 close( fd );
191
192 mlt_properties_set( this->filenames, "0", fullname );
193
194 // Teehe - when the producer closes, delete the temp file and the space allo
195 mlt_properties_set_data( producer_properties, "__temporary_file__", fullname, 0, ( mlt_destructor )unlink, NULL );
196 }
197 }
198 // Obtain filenames
199 else if ( strchr( filename, '%' ) != NULL )
200 {
201 // handle picture sequences
202 int i = mlt_properties_get_int( producer_properties, "begin" );
203 int gap = 0;
204 char full[1024];
205 int keyvalue = 0;
206 char key[ 50 ];
207
208 while ( gap < 100 )
209 {
210 struct stat buf;
211 snprintf( full, 1023, filename, i ++ );
212 if ( stat( full, &buf ) == 0 )
213 {
214 sprintf( key, "%d", keyvalue ++ );
215 mlt_properties_set( this->filenames, key, full );
216 gap = 0;
217 }
218 else
219 {
220 gap ++;
221 }
222 }
223 }
224 else if ( strstr( filename, "/.all." ) != NULL )
225 {
226 char wildcard[ 1024 ];
227 char *dir_name = strdup( filename );
228 char *extension = strrchr( dir_name, '.' );
229
230 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
231 sprintf( wildcard, "*%s", extension );
232
233 mlt_properties_dir_list( this->filenames, dir_name, wildcard, 1 );
234
235 free( dir_name );
236 }
237 else
238 {
239 mlt_properties_set( this->filenames, "0", filename );
240 }
241
242 this->count = mlt_properties_count( this->filenames );
243 }
244
245 // Generate a frame
246 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
247
248 if ( *frame != NULL && this->count > 0 )
249 {
250 // Obtain properties of frame and producer
251 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
252
253 // Set the producer on the frame properties
254 mlt_properties_set_data( properties, "producer_qimage", this, 0, NULL, NULL );
255
256 // Update timecode on the frame we're creating
257 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
258
259 // Ensure that we have a way to obtain the position in the get_image
260 mlt_properties_set_position( properties, "qimage_position", mlt_producer_position( producer ) );
261
262 // Refresh the image
263 refresh_qimage( *frame, 0, 0 );
264
265 // Set producer-specific frame properties
266 mlt_properties_set_int( properties, "progressive", mlt_properties_get_int( producer_properties, "progressive" ) );
267 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
268
269 // Set alpha call back
270 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
271
272 // Push the get_image method
273 mlt_frame_push_get_image( *frame, producer_get_image );
274 }
275
276 // Calculate the next timecode
277 mlt_producer_prepare_next( producer );
278
279 return 0;
280 }
281
282 static void producer_close( mlt_producer parent )
283 {
284 producer_qimage this = parent->child;
285 parent->close = NULL;
286 mlt_pool_release( this->current_image );
287 mlt_pool_release( this->current_alpha );
288 mlt_producer_close( parent );
289 mlt_properties_close( this->filenames );
290 free( this );
291 }