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