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