2 * filter_resize.c -- resizing filter
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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.
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.
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
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
32 static inline void swap_bytes( uint8_t *upper, uint8_t *lower )
39 static uint8_t *resize_alpha( uint8_t *input, int owidth, int oheight, int iwidth, int iheight, uint8_t alpha_value )
41 uint8_t *output = NULL;
43 if ( input != NULL && ( iwidth != owidth || iheight != oheight ) && ( owidth > 6 && oheight > 6 ) )
46 int offset_x = ( owidth - iwidth ) / 2;
47 int offset_y = ( oheight - iheight ) / 2;
50 output = mlt_pool_alloc( owidth * oheight );
51 memset( output, alpha_value, owidth * oheight );
53 offset_x -= offset_x % 2;
55 out_line = output + offset_y * owidth;
58 // Loop for the entirety of our output height.
61 // We're in the input range for this row.
62 memcpy( out_line, input, iused );
64 // Move to next input line
67 // Move to next output line
75 static void resize_image( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight, int bpp )
78 int istride = iwidth * bpp;
79 int ostride = owidth * bpp;
80 int offset_x = ( owidth - iwidth ) / 2 * bpp;
81 int offset_y = ( oheight - iheight ) / 2;
82 uint8_t *in_line = input;
84 int size = owidth * oheight;
88 if ( output == NULL || input == NULL || ( owidth <= 6 || oheight <= 6 || iwidth <= 6 || oheight <= 6 ) )
92 else if ( iwidth == owidth && iheight == oheight )
94 memcpy( output, input, iheight * istride );
105 offset_x -= offset_x % 4;
114 out_line = output + offset_y * ostride;
115 out_line += offset_x;
117 // Loop for the entirety of our output height.
120 // We're in the input range for this row.
121 memcpy( out_line, in_line, istride );
123 // Move to next input line
126 // Move to next output line
131 /** A padding function for frames - this does not rescale, but simply
135 static uint8_t *frame_resize_image( mlt_frame this, int owidth, int oheight, int bpp )
138 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
140 // Get the input image, width and height
141 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
142 uint8_t *alpha = mlt_frame_get_alpha_mask( this );
144 mlt_properties_get_data( properties, "alpha", &alpha_size );
146 int iwidth = mlt_properties_get_int( properties, "width" );
147 int iheight = mlt_properties_get_int( properties, "height" );
149 // If width and height are correct, don't do anything
150 if ( iwidth < owidth || iheight < oheight )
152 uint8_t alpha_value = mlt_properties_get_int( properties, "resize_alpha" );
154 // Create the output image
155 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * bpp );
157 // Call the generic resize
158 resize_image( output, owidth, oheight, input, iwidth, iheight, bpp );
160 // Now update the frame
161 mlt_frame_set_image( this, output, owidth * ( oheight + 1 ) * bpp, mlt_pool_release );
163 // We should resize the alpha too
164 if ( alpha && alpha_size >= iwidth * iheight )
166 alpha = resize_alpha( alpha, owidth, oheight, iwidth, iheight, alpha_value );
169 mlt_frame_set_alpha( this, alpha, owidth * oheight, mlt_pool_release );
170 this->get_alpha_mask = NULL;
177 // No change, return input
184 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
188 // Get the properties from the frame
189 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
191 // Pop the top of stack now
192 mlt_filter filter = mlt_frame_pop_service( this );
194 // Retrieve the aspect ratio
195 double aspect_ratio = mlt_deque_pop_back_double( MLT_FRAME_IMAGE_STACK( this ) );
197 // Correct Width/height if necessary
198 if ( *width == 0 || *height == 0 )
200 *width = mlt_properties_get_int( properties, "normalised_width" );
201 *height = mlt_properties_get_int( properties, "normalised_height" );
204 // Assign requested width/height from our subordinate
206 int oheight = *height;
208 // Check for the special case - no aspect ratio means no problem :-)
209 if ( aspect_ratio == 0.0 )
210 aspect_ratio = mlt_properties_get_double( properties, "consumer_aspect_ratio" );
212 // Reset the aspect ratio
213 mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
216 char *rescale = mlt_properties_get( properties, "rescale.interp" );
217 if ( rescale != NULL && !strcmp( rescale, "none" ) )
218 return mlt_frame_get_image( this, image, format, width, height, writable );
220 if ( mlt_properties_get_int( properties, "distort" ) == 0 )
222 // Normalise the input and out display aspect
223 int normalised_width = mlt_properties_get_int( properties, "normalised_width" );
224 int normalised_height = mlt_properties_get_int( properties, "normalised_height" );
225 int real_width = mlt_properties_get_int( properties, "real_width" );
226 int real_height = mlt_properties_get_int( properties, "real_height" );
227 if ( real_width == 0 )
228 real_width = mlt_properties_get_int( properties, "width" );
229 if ( real_height == 0 )
230 real_height = mlt_properties_get_int( properties, "height" );
231 double input_ar = aspect_ratio * real_width / real_height;
232 double output_ar = mlt_properties_get_double( properties, "consumer_aspect_ratio" ) * owidth / oheight;
234 // fprintf( stderr, "real %dx%d normalised %dx%d output %dx%d sar %f in-dar %f out-dar %f\n",
235 // real_width, real_height, normalised_width, normalised_height, owidth, oheight, aspect_ratio, input_ar, output_ar);
237 // Optimised for the input_ar > output_ar case (e.g. widescreen on standard)
238 int scaled_width = rint( ( input_ar * normalised_width ) / output_ar );
239 int scaled_height = normalised_height;
241 // Now ensure that our images fit in the output frame
242 if ( scaled_width > normalised_width )
244 scaled_width = normalised_width;
245 scaled_height = rint( ( output_ar * normalised_height ) / input_ar );
248 // Now calculate the actual image size that we want
249 owidth = rint( scaled_width * owidth / normalised_width );
250 oheight = rint( scaled_height * oheight / normalised_height );
252 // Tell frame we have conformed the aspect to the consumer
253 mlt_frame_set_aspect_ratio( this, mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
256 mlt_properties_set_int( properties, "distort", 0 );
258 // Now pass on the calculations down the line
259 mlt_properties_set_int( properties, "resize_width", *width );
260 mlt_properties_set_int( properties, "resize_height", *height );
263 if ( *format == mlt_image_yuv422 )
264 owidth -= owidth % 2;
265 error = mlt_frame_get_image( this, image, format, &owidth, &oheight, writable );
267 if ( error == 0 && *image )
269 // Get the requested scale operation
270 char *op = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "scale" );
275 case mlt_image_yuv422:
278 case mlt_image_rgb24:
281 case mlt_image_rgb24a:
282 case mlt_image_opengl:
286 // XXX: we only know how to resize packed formats
290 // Provides a manual override for misreported field order
291 if ( mlt_properties_get( properties, "meta.top_field_first" ) )
292 mlt_properties_set_int( properties, "top_field_first", mlt_properties_get_int( properties, "meta.top_field_first" ) );
294 // Correct field order if needed
295 if ( mlt_properties_get_int( properties, "top_field_first" ) == 1 &&
296 mlt_properties_get( properties, "progressive" ) &&
297 mlt_properties_get_int( properties, "progressive" ) == 0 )
299 // Get the input image, width and height
300 int size = owidth * oheight * bpp;
301 uint8_t *new_image = mlt_pool_alloc( size );
302 mlt_frame_set_image( this, new_image, size, mlt_pool_release );
303 uint8_t *ptr = new_image + owidth * bpp;
304 memcpy( new_image, *image, owidth * bpp );
305 memcpy( ptr, *image, size - owidth * bpp );
308 // Set the normalised field order
309 mlt_properties_set_int( properties, "top_field_first", 0 );
310 mlt_properties_set_int( properties, "meta.top_field_first", 0 );
313 if ( !strcmp( op, "affine" ) )
315 // TODO: Determine where this is needed and find a different way
316 // *image = mlt_frame_rescale_image( this, *width, *height, bpp );
318 else if ( strcmp( op, "none" ) != 0 )
320 *image = frame_resize_image( this, *width, *height, bpp );
332 /** Filter processing.
335 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
337 // Store the aspect ratio reported by the source
338 mlt_deque_push_back_double( MLT_FRAME_IMAGE_STACK( frame ), mlt_frame_get_aspect_ratio( frame ) );
340 // Push this on to the service stack
341 mlt_frame_push_service( frame, this );
343 // Push the get_image method on to the stack
344 mlt_frame_push_get_image( frame, filter_get_image );
349 /** Constructor for the filter.
352 mlt_filter filter_resize_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
354 mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
355 if ( mlt_filter_init( this, this ) == 0 )
357 this->process = filter_process;
358 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "scale", arg == NULL ? "off" : arg );