]> git.sesse.net Git - mlt/blob - src/modules/qimage/qimage_wrapper.cpp
af6838eb2e1d71ed065fee5192b9f1a1660cfe12
[mlt] / src / modules / qimage / qimage_wrapper.cpp
1 /*
2  * qimage_wrapper.cpp -- 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 "qimage_wrapper.h"
25 #include <qimage.h>
26
27
28 #include "config.h"
29
30 #ifdef USE_KDE
31 #include <kinstance.h>
32 #include <kimageio.h>
33 #endif
34
35 #include <cmath>
36
37 extern "C" {
38
39 #include <framework/mlt_pool.h>
40
41 #ifdef USE_KDE
42 static KInstance *instance = 0L;
43 #endif
44
45 static void qimage_delete( void *data )
46 {
47         QImage *image = ( QImage * )data;
48         delete image;
49 #ifdef USE_KDE
50         if (instance) delete instance;
51         instance = 0L;
52 #endif
53 }
54
55 static void clear_buffered_image( mlt_properties producer_props, uint8_t **current_image, uint8_t **current_alpha )
56 {
57         mlt_events_block( producer_props, NULL );
58         mlt_properties_set_data( producer_props, "_qimage_image", NULL, 0, NULL, NULL );
59         mlt_properties_set_data( producer_props, "_qimage_alpha", NULL, 0, NULL, NULL );
60         *current_image = NULL;
61         *current_alpha = NULL;
62         mlt_events_unblock( producer_props, NULL );
63 }
64
65 static void assign_buffered_image( mlt_properties producer_props, uint8_t *current_image, uint8_t *current_alpha, int width, int height )
66 {
67         int use_cache = mlt_properties_get_int( producer_props, "cache" );
68         mlt_destructor destructor = use_cache ? NULL : mlt_pool_release;
69         mlt_events_block( producer_props, NULL );
70         mlt_properties_set_data( producer_props, "_qimage_image", current_image, 0, destructor, NULL );
71         mlt_properties_set_data( producer_props, "_qimage_alpha", current_alpha, 0, destructor, NULL );
72         mlt_properties_set_int( producer_props, "_qimage_width", width );
73         mlt_properties_set_int( producer_props, "_qimage_height", height );
74         mlt_events_unblock( producer_props, NULL );
75 }
76
77 void init_qimage()
78 {
79 #ifdef USE_KDE
80         if (!instance) {
81             instance = new KInstance("qimage_prod");
82             KImageIO::registerFormats();
83         }
84 #endif
85 }
86
87 void refresh_qimage( mlt_frame frame, int width, int height )
88 {
89         // Obtain a previous assigned qimage (if it exists) 
90         QImage *qimage = ( QImage * )mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "qimage", NULL );
91
92         // Obtain properties of frame
93         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
94
95         // Obtain the producer for this frame
96         producer_qimage self = ( producer_qimage )mlt_properties_get_data( properties, "producer_qimage", NULL );
97
98         // Obtain the producer 
99         mlt_producer producer = &self->parent;
100
101         // Obtain properties of producer
102         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
103
104         // Obtain the cache flag and structure
105         int use_cache = mlt_properties_get_int( producer_props, "cache" );
106         mlt_properties cache = ( mlt_properties )mlt_properties_get_data( producer_props, "_cache", NULL );
107         int update_cache = 0;
108
109         // Retrieve current info if available
110         uint8_t *current_image = ( uint8_t * )mlt_properties_get_data( producer_props, "_qimage_image", NULL );
111         uint8_t *current_alpha = ( uint8_t * )mlt_properties_get_data( producer_props, "_qimage_alpha", NULL );
112         int current_width = mlt_properties_get_int( producer_props, "_qimage_width" );
113         int current_height = mlt_properties_get_int( producer_props, "_qimage_height" );
114
115         // Get the time to live for each frame
116         double ttl = mlt_properties_get_int( producer_props, "ttl" );
117
118         // Get the original position of this frame
119         mlt_position position = mlt_properties_get_position( properties, "qimage_position" );
120
121         // Image index
122         int image_idx = ( int )floor( ( double )position / ttl ) % self->count;
123
124         // Key for the cache
125         char image_key[ 10 ];
126         sprintf( image_key, "%d", image_idx );
127
128         // Check if the frame is already loaded
129         if ( use_cache )
130         {
131                 if ( cache == NULL )
132                 {
133                         cache = mlt_properties_new( );
134                         mlt_properties_set_data( producer_props, "_cache", cache, 0, ( mlt_destructor )mlt_properties_close, NULL );
135                 }
136
137                 mlt_frame cached = ( mlt_frame )mlt_properties_get_data( cache, image_key, NULL );
138
139                 if ( cached )
140                 {
141                         self->image_idx = image_idx;
142                         mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
143                         current_width = mlt_properties_get_int( cached_props, "width" );
144                         current_height = mlt_properties_get_int( cached_props, "height" );
145                         mlt_properties_set_int( producer_props, "_real_width", mlt_properties_get_int( cached_props, "real_width" ) );
146                         mlt_properties_set_int( producer_props, "_real_height", mlt_properties_get_int( cached_props, "real_height" ) );
147                         current_image = ( uint8_t * )mlt_properties_get_data( cached_props, "image", NULL );
148                         current_alpha = ( uint8_t * )mlt_properties_get_data( cached_props, "alpha", NULL );
149
150                         if ( width != 0 && ( width != current_width || height != current_height ) )
151                                 current_image = NULL;
152
153                         assign_buffered_image( producer_props, current_image, current_alpha, current_width, current_height );
154                 }
155         }
156
157     // optimization for subsequent iterations on single picture
158         if ( width != 0 && current_image != NULL && image_idx == self->image_idx )
159         {
160                 if ( width != current_width || height != current_height )
161                 {
162                         qimage = ( QImage * )mlt_properties_get_data( producer_props, "_qimage", NULL );
163                         clear_buffered_image( producer_props, &current_image, &current_alpha );
164                 }
165         }
166         else if ( qimage == NULL && ( current_image == NULL || image_idx != self->image_idx ) )
167         {
168                 clear_buffered_image( producer_props, &current_image, &current_alpha );
169
170                 self->image_idx = image_idx;
171                 qimage = new QImage( mlt_properties_get_value( self->filenames, image_idx ) );
172
173                 if ( !qimage->isNull( ) )
174                 {
175                         QImage *frame_copy = new QImage( *qimage );
176
177                         // Store the width/height of the pixbuf 
178                         current_width = qimage->width( );
179                         current_height = qimage->height( );
180
181                         // Register qimage for destruction and reuse
182                         mlt_events_block( producer_props, NULL );
183                         mlt_properties_set_data( producer_props, "_qimage", qimage, 0, ( mlt_destructor )qimage_delete, NULL );
184                         mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), "qimage", frame_copy, 0, ( mlt_destructor )qimage_delete, NULL );
185                         mlt_properties_set_int( producer_props, "_real_width", current_width );
186                         mlt_properties_set_int( producer_props, "_real_height", current_height );
187                         mlt_events_unblock( producer_props, NULL );
188                 }
189                 else
190                 {
191                         delete qimage;
192                         qimage = NULL;
193                 }
194         }
195
196         // If we have a pixbuf and this request specifies a valid dimension and we haven't already got a cached version...
197         if ( qimage && width > 0 && current_image == NULL )
198         {
199                 char *interps = mlt_properties_get( properties, "rescale.interp" );
200                 int interp = 0;
201
202                 // QImage has two scaling modes - we'll toggle between them here
203                 if ( strcmp( interps, "tiles" ) == 0 )
204                         interp = 1;
205                 else if ( strcmp( interps, "hyper" ) == 0 )
206                         interp = 1;
207
208                 // Note - the original qimage is already safe and ready for destruction
209                 QImage scaled = interp == 0 ? qimage->scale( width, height, QImage::ScaleFree ) : qimage->smoothScale( width, height, QImage::ScaleFree );
210                 QImage temp = scaled.convertDepth( 32 );
211
212                 // Store width and height
213                 current_width = width;
214                 current_height = height;
215                 
216                 // Allocate/define image
217                 current_image = ( uint8_t * )mlt_pool_alloc( width * ( height + 1 ) * 2 );
218
219                 // Allocate the alpha mask
220                 current_alpha = ( uint8_t * )mlt_pool_alloc( current_width * current_height );
221
222                 // Convert the image
223                 if ( QImage::systemByteOrder( ) == QImage::BigEndian )
224                         mlt_convert_argb_to_yuv422( temp.bits( ), current_width, current_height, temp.bytesPerLine( ), current_image, current_alpha );
225                 else
226                         mlt_convert_bgr24a_to_yuv422( temp.bits( ), current_width, current_height, temp.bytesPerLine( ), current_image, current_alpha );
227
228                 assign_buffered_image( producer_props, current_image, current_alpha, current_width, current_height );
229
230                 // Ensure we update the cache when we need to
231                 update_cache = use_cache;
232         }
233
234         // Set width/height of frame
235         mlt_properties_set_int( properties, "width", current_width );
236         mlt_properties_set_int( properties, "height", current_height );
237         mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
238         mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
239
240         // pass the image data without destructor
241         mlt_properties_set_data( properties, "image", current_image, current_width * ( current_height + 1 ) * 2, NULL, NULL );
242         mlt_properties_set_data( properties, "alpha", current_alpha, current_width * current_height, NULL, NULL );
243
244         if ( update_cache )
245         {
246                 mlt_frame cached = mlt_frame_init( );
247                 mlt_properties cached_props = MLT_FRAME_PROPERTIES( cached );
248                 mlt_properties_set_int( cached_props, "width", current_width );
249                 mlt_properties_set_int( cached_props, "height", current_height );
250                 mlt_properties_set_int( cached_props, "real_width", mlt_properties_get_int( producer_props, "_real_width" ) );
251                 mlt_properties_set_int( cached_props, "real_height", mlt_properties_get_int( producer_props, "_real_height" ) );
252                 mlt_properties_set_data( cached_props, "image", current_image, current_width * ( current_height + 1 ) * 2, mlt_pool_release, NULL );
253                 mlt_properties_set_data( cached_props, "alpha", current_alpha, current_width * current_height, mlt_pool_release, NULL );
254                 mlt_properties_set_data( cache, image_key, cached, 0, ( mlt_destructor )mlt_frame_close, NULL );
255         }
256 }
257
258 }
259