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