Merge ../mlt
[melted] / src / modules / core / filter_rescale.c
1 /*
2 * filter_rescale.c -- scale the producer video frame size to match the consumer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <math.h>
28
29 typedef int ( *image_scaler )( mlt_frame this, uint8_t **image, mlt_image_format iformat, mlt_image_format oformat, int iwidth, int iheight, int owidth, int oheight );
30
31 static void scale_alpha( mlt_frame this, int iwidth, int iheight, int owidth, int oheight );
32
33 static int filter_scale( mlt_frame this, uint8_t **image, mlt_image_format iformat, mlt_image_format oformat, int iwidth, int iheight, int owidth, int oheight )
34 {
35 // Get the properties
36 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
37
38 // Get the rescaling interpolsation
39 char *interps = mlt_properties_get( properties, "rescale.interp" );
40
41 // Carry out the rescaling
42 if ( iformat == mlt_image_yuv422 && oformat == mlt_image_yuv422 )
43 {
44 // Scale the frame
45 mlt_frame_rescale_yuv422( this, owidth, oheight );
46
47 // Return the output
48 *image = mlt_properties_get_data( properties, "image", NULL );
49
50 // Scale the alpha channel only if exists and not correct size
51 int alpha_size = 0;
52 mlt_properties_get_data( properties, "alpha", &alpha_size );
53 if ( alpha_size > 0 && alpha_size != ( owidth * oheight ) )
54 scale_alpha( this, iwidth, iheight, owidth, oheight );
55 }
56 else if ( iformat == mlt_image_rgb24 || iformat == mlt_image_rgb24a )
57 {
58 int bpp = (iformat == mlt_image_rgb24a ? 4 : 3 );
59
60 // Create the yuv image
61 uint8_t *output = mlt_pool_alloc( iwidth * ( iheight + 1 ) * 2 );
62
63 if ( strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
64 {
65 // Extract YUV422 and alpha
66 if ( bpp == 4 )
67 {
68 // Allocate the alpha mask
69 uint8_t *alpha = mlt_pool_alloc( iwidth * ( iheight + 1 ) );
70
71 // Convert the image and extract alpha
72 mlt_convert_rgb24a_to_yuv422( *image, iwidth, iheight, iwidth * 4, output, alpha );
73
74 mlt_properties_set_data( properties, "alpha", alpha, iwidth * ( iheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
75
76 scale_alpha( this, iwidth, iheight, owidth, oheight );
77 }
78 else
79 {
80 // No alpha to extract
81 mlt_convert_rgb24_to_yuv422( *image, iwidth, iheight, iwidth * 3, output );
82 }
83
84 mlt_properties_set_data( properties, "image", output, iwidth * ( iheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
85
86 // Scale the frame
87 output = mlt_frame_rescale_yuv422( this, owidth, oheight );
88
89 }
90 else
91 {
92 // Extract YUV422 and alpha
93 if ( bpp == 4 )
94 {
95 // Allocate the alpha mask
96 uint8_t *alpha = mlt_pool_alloc( owidth * ( oheight + 1 ) );
97
98 // Convert the image and extract alpha
99 mlt_convert_rgb24a_to_yuv422( *image, owidth, oheight, owidth * 4, output, alpha );
100
101 mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
102
103 scale_alpha( this, iwidth, iheight, owidth, oheight );
104 }
105 else
106 {
107 // No alpha to extract
108 mlt_convert_rgb24_to_yuv422( *image, owidth, oheight, owidth * 3, output );
109 }
110 }
111
112 // Now update the frame
113 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
114 mlt_properties_set_int( properties, "width", owidth );
115 mlt_properties_set_int( properties, "height", oheight );
116
117 *image = output;
118 }
119
120 return 0;
121 }
122
123 static void scale_alpha( mlt_frame this, int iwidth, int iheight, int owidth, int oheight )
124 {
125 // Scale the alpha
126 uint8_t *output = NULL;
127 uint8_t *input = mlt_frame_get_alpha_mask( this );
128
129 if ( input != NULL )
130 {
131 uint8_t *out_line;
132 int x, y;
133 int ox = ( iwidth << 10 ) / owidth;
134 int oy = ( iheight << 10 ) / oheight;
135
136 output = mlt_pool_alloc( owidth * oheight );
137 out_line = output;
138
139 // Loop for the entirety of our output height.
140 for ( y = 0; y < oheight; y ++ )
141 for ( x = 0; x < owidth; x ++ )
142 *out_line ++ = *( input + ( ( 512 + ( y * oy * iwidth ) + x * ox ) >> 10 ) );
143
144 // Set it back on the frame
145 mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "alpha", output, owidth * oheight, mlt_pool_release, NULL );
146 }
147 }
148
149 /** Do it :-).
150 */
151
152 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
153 {
154 // Get the frame properties
155 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
156
157 // Get the filter from the stack
158 mlt_filter filter = mlt_frame_pop_service( this );
159
160 // Get the filter properties
161 mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
162
163 // Get the image scaler method
164 image_scaler scaler_method = mlt_properties_get_data( filter_properties, "method", NULL );
165
166 // Correct Width/height if necessary
167 if ( *width == 0 || *height == 0 )
168 {
169 *width = mlt_properties_get_int( properties, "normalised_width" );
170 *height = mlt_properties_get_int( properties, "normalised_height" );
171 }
172
173 // There can be problems with small images - avoid them (by hacking - gah)
174 if ( *width >= 6 && *height >= 6 )
175 {
176 int iwidth = *width;
177 int iheight = *height;
178 int owidth = *width;
179 int oheight = *height;
180 char *interps = mlt_properties_get( properties, "rescale.interp" );
181 int wanted_format = *format;
182
183 // Default from the scaler if not specifed on the frame
184 if ( interps == NULL )
185 {
186 interps = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "interpolation" );
187 mlt_properties_set( properties, "rescale.interp", interps );
188 }
189
190 // If real_width/height exist, we want that as minimum information
191 if ( mlt_properties_get_int( properties, "real_width" ) )
192 {
193 iwidth = mlt_properties_get_int( properties, "real_width" );
194 iheight = mlt_properties_get_int( properties, "real_height" );
195 }
196
197 // Let the producer know what we are actually requested to obtain
198 if ( *format == mlt_image_yuv422 && strcmp( interps, "none" ) )
199 {
200 mlt_properties_set_int( properties, "rescale_width", *width );
201 mlt_properties_set_int( properties, "rescale_height", *height );
202 }
203 else
204 {
205 // When no scaling is requested, revert the requested dimensions if possible
206 mlt_properties_set_int( properties, "rescale_width", iwidth );
207 mlt_properties_set_int( properties, "rescale_height", iheight );
208 }
209
210 // Deinterlace if height is changing to prevent fields mixing on interpolation
211 // One exception: non-interpolated, integral scaling
212 if ( iheight != oheight && ( strcmp( interps, "nearest" ) || ( iheight % oheight != 0 ) ) )
213 mlt_properties_set_int( properties, "consumer_deinterlace", 1 );
214
215 // Get the image as requested
216 mlt_frame_get_image( this, image, format, &iwidth, &iheight, writable );
217
218 // Get rescale interpretation again, in case the producer wishes to override scaling
219 interps = mlt_properties_get( properties, "rescale.interp" );
220
221 if ( *image != NULL && ( *format != mlt_image_yuv422 || ( iwidth != owidth || iheight != oheight ) ) )
222 {
223 // If the colour space is correct and scaling is off, do nothing
224 if ( *format == mlt_image_yuv422 && !strcmp( interps, "none" ) )
225 {
226 *width = iwidth;
227 *height = iheight;
228 }
229 else if ( *format == mlt_image_yuv422 )
230 {
231 // Call the local scaler
232 scaler_method( this, image, *format, mlt_image_yuv422, iwidth, iheight, owidth, oheight );
233 *width = owidth;
234 *height = oheight;
235 }
236 else if ( *format == mlt_image_rgb24 && wanted_format == mlt_image_rgb24 )
237 {
238 // Call the local scaler
239 scaler_method( this, image, *format, mlt_image_rgb24, iwidth, iheight, owidth, oheight );
240
241 // Return the output
242 *width = owidth;
243 *height = oheight;
244 }
245 else if ( *format == mlt_image_rgb24 || *format == mlt_image_rgb24a )
246 {
247 // Call the local scaler
248 scaler_method( this, image, *format, mlt_image_yuv422, iwidth, iheight, owidth, oheight );
249
250 // Return the output
251 *format = mlt_image_yuv422;
252 *width = owidth;
253 *height = oheight;
254 }
255 else
256 {
257 *width = iwidth;
258 *height = iheight;
259 }
260 }
261 else
262 {
263 *width = iwidth;
264 *height = iheight;
265 }
266 }
267 else
268 {
269 // Store the requested width/height
270 int iwidth = *width;
271 int iheight = *height;
272
273 // Get the image as requested
274 mlt_frame_get_image( this, image, format, &iwidth, &iheight, writable );
275
276 // Too small - for now just assign as though we got what we wanted
277 *width = iwidth;
278 *height = iheight;
279 }
280
281
282 return 0;
283 }
284
285 /** Filter processing.
286 */
287
288 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
289 {
290 // Push the filter
291 mlt_frame_push_service( frame, this );
292
293 // Push the get image method
294 mlt_frame_push_service( frame, filter_get_image );
295
296 return frame;
297 }
298
299 /** Constructor for the filter.
300 */
301
302 mlt_filter filter_rescale_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
303 {
304 // Create a new scaler
305 mlt_filter this = mlt_filter_new( );
306
307 // If successful, then initialise it
308 if ( this != NULL )
309 {
310 // Get the properties
311 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
312
313 // Set the process method
314 this->process = filter_process;
315
316 // Set the inerpolation
317 mlt_properties_set( properties, "interpolation", arg == NULL ? "bilinear" : arg );
318
319 // Set the method
320 mlt_properties_set_data( properties, "method", filter_scale, 0, NULL, NULL );
321 }
322
323 return this;
324 }
325