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>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "filter_rescale.h"
23 #include <framework/mlt_frame.h>
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 );
31 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 mlt_properties properties = mlt_frame_properties( this );
36 // Get the rescaling interpolsation
37 char *interps = mlt_properties_get( properties, "rescale.interp" );
39 // Carry out the rescaling
40 if ( iformat == mlt_image_yuv422 && oformat == mlt_image_yuv422 )
43 mlt_frame_rescale_yuv422( this, owidth, oheight );
46 *image = mlt_properties_get_data( properties, "image", NULL );
48 else if ( iformat == mlt_image_rgb24 || iformat == mlt_image_rgb24a )
50 int bpp = (iformat == mlt_image_rgb24a ? 4 : 3 );
52 // Create the yuv image
53 uint8_t *output = mlt_pool_alloc( iwidth * ( iheight + 1 ) * 2 );
55 if ( strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
57 // Extract YUV422 and alpha
60 // Allocate the alpha mask
61 uint8_t *alpha = mlt_pool_alloc( iwidth * ( iheight + 1 ) );
63 // Convert the image and extract alpha
64 mlt_convert_rgb24a_to_yuv422( *image, iwidth, iheight, iwidth * 2, output, alpha );
66 mlt_properties_set_data( properties, "alpha", alpha, iwidth * ( iheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
70 // No alpha to extract
71 mlt_convert_rgb24_to_yuv422( *image, iwidth, iheight, iwidth * 2, output );
75 mlt_frame_rescale_yuv422( this, owidth, oheight );
78 *image = mlt_properties_get_data( properties, "image", NULL );
82 // Extract YUV422 and alpha
85 // Allocate the alpha mask
86 uint8_t *alpha = mlt_pool_alloc( owidth * ( oheight + 1 ) );
88 // Convert the image and extract alpha
89 mlt_convert_rgb24a_to_yuv422( *image, owidth, oheight, owidth * 4, output, alpha );
91 mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
95 // No alpha to extract
96 mlt_convert_rgb24_to_yuv422( *image, owidth, oheight, owidth * 3, output );
100 // Now update the frame
101 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
102 mlt_properties_set_int( properties, "width", owidth );
103 mlt_properties_set_int( properties, "height", oheight );
111 static void scale_alpha( mlt_frame this, int iwidth, int iheight, int owidth, int oheight )
113 uint8_t *input = mlt_frame_get_alpha_mask( this );
117 uint8_t *output = mlt_pool_alloc( owidth * oheight );
119 // Derived coordinates
123 int out_x_range = owidth / 2;
124 int out_y_range = oheight / 2;
125 int in_x_range = iwidth / 2;
126 int in_y_range = iheight / 2;
129 register uint8_t *out_line = output;
130 register uint8_t *out_ptr;
132 // Calculate a middle pointer
133 uint8_t *in_middle = input + iwidth * in_y_range + in_x_range;
136 // Generate the affine transform scaling values
137 register int scale_width = ( iwidth << 16 ) / owidth;
138 register int scale_height = ( iheight << 16 ) / oheight;
139 register int base = 0;
141 int outer = out_x_range * scale_width;
142 int bottom = out_y_range * scale_height;
144 // Loop for the entirety of our output height.
145 for ( dy = - bottom; dy < bottom; dy += scale_height )
147 // Start at the beginning of the line
150 // Pointer to the middle of the input line
151 in_line = in_middle + ( dy >> 16 ) * iwidth;
153 // Loop for the entirety of our output row.
154 for ( dx = - outer; dx < outer; dx += scale_width )
157 *out_ptr ++ = *( in_line + base );
160 // Move to next output line
164 this->get_alpha_mask = NULL;
165 mlt_properties_set_data( mlt_frame_properties( this ), "alpha", output, 0, mlt_pool_release, NULL );
172 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
174 // Get the frame properties
175 mlt_properties properties = mlt_frame_properties( this );
177 // Get the filter from the stack
178 mlt_filter filter = mlt_frame_pop_service( this );
180 // Get the filter properties
181 mlt_properties filter_properties = mlt_filter_properties( filter );
183 // Get the image scaler method
184 image_scaler scaler_method = mlt_properties_get_data( filter_properties, "method", NULL );
186 // Correct Width/height if necessary
187 if ( *width == 0 || *height == 0 )
189 *width = mlt_properties_get_int( properties, "normalised_width" );
190 *height = mlt_properties_get_int( properties, "normalised_height" );
193 // There can be problems with small images - avoid them (by hacking - gah)
194 if ( *width >= 6 && *height >= 6 )
197 int iheight = *height;
199 int oheight = *height;
200 char *interps = mlt_properties_get( properties, "rescale.interp" );
201 int wanted_format = *format;
203 // Default from the scaler if not specifed on the frame
204 if ( interps == NULL )
206 interps = mlt_properties_get( mlt_filter_properties( filter ), "interpolation" );
207 mlt_properties_set( properties, "rescale.interp", interps );
210 // If real_width/height exist, we want that as minimum information
211 if ( mlt_properties_get_int( properties, "real_width" ) )
213 iwidth = mlt_properties_get_int( properties, "real_width" );
214 iheight = mlt_properties_get_int( properties, "real_height" );
217 // Let the producer know what we are actually requested to obtain
218 if ( *format == mlt_image_yuv422 && strcmp( interps, "none" ) )
220 mlt_properties_set_int( properties, "rescale_width", *width );
221 mlt_properties_set_int( properties, "rescale_height", *height );
225 // When no scaling is requested, revert the requested dimensions if possible
226 mlt_properties_set_int( properties, "rescale_width", ( iwidth / 2 ) * 2 );
227 mlt_properties_set_int( properties, "rescale_height", iheight );
230 // Get the image as requested
231 mlt_frame_get_image( this, image, format, &iwidth, &iheight, writable );
233 // Get rescale interpretation again, in case the producer wishes to override scaling
234 interps = mlt_properties_get( properties, "rescale.interp" );
236 if ( *image != NULL && ( *format != mlt_image_yuv422 || ( iwidth != owidth || iheight != oheight ) ) )
238 // If the colour space is correct and scaling is off, do nothing
239 if ( *format == mlt_image_yuv422 && !strcmp( interps, "none" ) )
244 else if ( *format == mlt_image_yuv422 )
246 // Call the local scaler
247 scaler_method( this, image, *format, mlt_image_yuv422, iwidth, iheight, owidth, oheight );
252 scale_alpha( this, iwidth, iheight, owidth, oheight );
254 else if ( *format == mlt_image_rgb24 && wanted_format == mlt_image_rgb24 )
256 // Call the local scaler
257 scaler_method( this, image, *format, mlt_image_rgb24, iwidth, iheight, owidth, oheight );
264 scale_alpha( this, iwidth, iheight, owidth, oheight );
266 else if ( *format == mlt_image_rgb24 || *format == mlt_image_rgb24a )
268 // Call the local scaler
269 scaler_method( this, image, *format, mlt_image_yuv422, iwidth, iheight, owidth, oheight );
272 *format = mlt_image_yuv422;
277 scale_alpha( this, iwidth, iheight, owidth, oheight );
293 // Store the requested width/height
295 int iheight = *height;
297 // Get the image as requested
298 mlt_frame_get_image( this, image, format, &iwidth, &iheight, writable );
300 // Too small - for now just assign as though we got what we wanted
309 /** Filter processing.
312 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
315 mlt_frame_push_service( frame, this );
317 // Push the get image method
318 mlt_frame_push_service( frame, filter_get_image );
323 /** Constructor for the filter.
326 mlt_filter filter_rescale_init( char *arg )
328 // Create a new scaler
329 mlt_filter this = mlt_filter_new( );
331 // If successful, then initialise it
334 // Get the properties
335 mlt_properties properties = mlt_filter_properties( this );
337 // Set the process method
338 this->process = filter_process;
340 // Set the inerpolation
341 mlt_properties_set( properties, "interpolation", arg == NULL ? "bilinear" : arg );
344 mlt_properties_set_data( properties, "method", filter_scale, 0, NULL, NULL );