]> git.sesse.net Git - mlt/blob - src/modules/qimage/producer_qimage.c
Refactor to use mlt_frame_set_image/_alpha.
[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                                 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 );
77                         }
78                 }
79                 if ( this->current_width == 0 )
80                 {
81                         producer_close( producer );
82                         producer = NULL;
83                 }
84                 if ( producer )
85                         pthread_mutex_init( &this->mutex, NULL );
86                 return producer;
87         }
88         free( this );
89         return NULL;
90 }
91
92 static void load_filenames( producer_qimage this, mlt_properties producer_properties )
93 {
94         char *filename = mlt_properties_get( producer_properties, "resource" );
95         this->filenames = mlt_properties_new( );
96
97         // Read xml string
98         if ( strstr( filename, "<svg" ) )
99         {
100                 make_tempfile( this, filename );
101         }
102         // Obtain filenames
103         else if ( strchr( filename, '%' ) != NULL )
104         {
105                 // handle picture sequences
106                 int i = mlt_properties_get_int( producer_properties, "begin" );
107                 int gap = 0;
108                 char full[1024];
109                 int keyvalue = 0;
110                 char key[ 50 ];
111
112                 while ( gap < 100 )
113                 {
114                         struct stat buf;
115                         snprintf( full, 1023, filename, i ++ );
116                         if ( stat( full, &buf ) == 0 )
117                         {
118                                 sprintf( key, "%d", keyvalue ++ );
119                                 mlt_properties_set( this->filenames, key, full );
120                                 gap = 0;
121                         }
122                         else
123                         {
124                                 gap ++;
125                         }
126                 }
127                 if ( mlt_properties_count( this->filenames ) > 0 )
128                         mlt_properties_set_int( producer_properties, "ttl", 1 );
129         }
130         else if ( strstr( filename, "/.all." ) != NULL )
131         {
132                 char wildcard[ 1024 ];
133                 char *dir_name = strdup( filename );
134                 char *extension = strrchr( dir_name, '.' );
135
136                 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
137                 sprintf( wildcard, "*%s", extension );
138
139                 mlt_properties_dir_list( this->filenames, dir_name, wildcard, 1 );
140
141                 free( dir_name );
142         }
143         else
144         {
145                 mlt_properties_set( this->filenames, "0", filename );
146         }
147
148         this->count = mlt_properties_count( this->filenames );
149 }
150
151 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
152 {
153         int error = 0;
154         
155         // Obtain properties of frame
156         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
157
158         // Obtain the producer for this frame
159         producer_qimage this = mlt_properties_get_data( properties, "producer_qimage", NULL );
160
161         *width = mlt_properties_get_int( properties, "rescale_width" );
162         *height = mlt_properties_get_int( properties, "rescale_height" );
163
164         mlt_service_lock( MLT_PRODUCER_SERVICE( &this->parent ) );
165
166         // Refresh the image
167         refresh_qimage( this, frame, *width, *height );
168
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" );
172
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 )
176         {
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 ) );
188         }
189         else
190         {
191                 error = 1;
192         }
193
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 ) );
198
199         return error;
200 }
201
202 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
203 {
204         // Get the real structure for this producer
205         producer_qimage this = producer->child;
206
207         // Fetch the producers properties
208         mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
209
210         if ( this->filenames == NULL && mlt_properties_get( producer_properties, "resource" ) != NULL )
211                 load_filenames( this, producer_properties );
212
213         // Generate a frame
214         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
215
216         if ( *frame != NULL && this->count > 0 )
217         {
218                 // Obtain properties of frame and producer
219                 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
220
221                 // Set the producer on the frame properties
222                 mlt_properties_set_data( properties, "producer_qimage", this, 0, NULL, NULL );
223
224                 // Update timecode on the frame we're creating
225                 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
226
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 ) );
229
230                 // Refresh the image
231                 refresh_qimage( this, *frame, 0, 0 );
232
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 );
238                 else
239                         mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_properties, "aspect_ratio" ) );
240
241                 // Push the get_image method
242                 mlt_frame_push_get_image( *frame, producer_get_image );
243         }
244
245         // Calculate the next timecode
246         mlt_producer_prepare_next( producer );
247
248         return 0;
249 }
250
251 static void producer_close( mlt_producer parent )
252 {
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 );
259         free( this );
260 }