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