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 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 "filter_rescale.h"
23 #include <framework/mlt_frame.h>
30 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 );
32 static void scale_alpha( mlt_frame this, int iwidth, int iheight, int owidth, int oheight );
34 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 )
37 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
39 // Get the rescaling interpolsation
40 char *interps = mlt_properties_get( properties, "rescale.interp" );
42 // Carry out the rescaling
43 if ( iformat == mlt_image_yuv422 && oformat == mlt_image_yuv422 )
46 mlt_frame_rescale_yuv422( this, owidth, oheight );
49 *image = mlt_properties_get_data( properties, "image", NULL );
51 // Scale the alpha channel only if exists and not correct size
53 mlt_properties_get_data( properties, "alpha", &alpha_size );
54 if ( alpha_size > 0 && alpha_size != ( owidth * oheight ) )
55 scale_alpha( this, iwidth, iheight, owidth, oheight );
57 else if ( iformat == mlt_image_rgb24 || iformat == mlt_image_rgb24a )
59 int bpp = (iformat == mlt_image_rgb24a ? 4 : 3 );
61 // Create the yuv image
62 uint8_t *output = mlt_pool_alloc( iwidth * ( iheight + 1 ) * 2 );
64 if ( strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
66 // Extract YUV422 and alpha
69 // Allocate the alpha mask
70 uint8_t *alpha = mlt_pool_alloc( iwidth * ( iheight + 1 ) );
72 // Convert the image and extract alpha
73 mlt_convert_rgb24a_to_yuv422( *image, iwidth, iheight, iwidth * 4, output, alpha );
75 mlt_properties_set_data( properties, "alpha", alpha, iwidth * ( iheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
77 scale_alpha( this, iwidth, iheight, owidth, oheight );
81 // No alpha to extract
82 mlt_convert_rgb24_to_yuv422( *image, iwidth, iheight, iwidth * 3, output );
85 mlt_properties_set_data( properties, "image", output, iwidth * ( iheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
88 output = mlt_frame_rescale_yuv422( this, owidth, oheight );
93 // Extract YUV422 and alpha
96 // Allocate the alpha mask
97 uint8_t *alpha = mlt_pool_alloc( owidth * ( oheight + 1 ) );
99 // Convert the image and extract alpha
100 mlt_convert_rgb24a_to_yuv422( *image, owidth, oheight, owidth * 4, output, alpha );
102 mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
104 scale_alpha( this, iwidth, iheight, owidth, oheight );
108 // No alpha to extract
109 mlt_convert_rgb24_to_yuv422( *image, owidth, oheight, owidth * 3, output );
113 // Now update the frame
114 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
115 mlt_properties_set_int( properties, "width", owidth );
116 mlt_properties_set_int( properties, "height", oheight );
124 static void scale_alpha( mlt_frame this, int iwidth, int iheight, int owidth, int oheight )
127 uint8_t *output = NULL;
128 uint8_t *input = mlt_frame_get_alpha_mask( this );
134 int ox = ( iwidth << 10 ) / owidth;
135 int oy = ( iheight << 10 ) / oheight;
137 output = mlt_pool_alloc( owidth * oheight );
140 // Loop for the entirety of our output height.
141 for ( y = 0; y < oheight; y ++ )
142 for ( x = 0; x < owidth; x ++ )
143 *out_line ++ = *( input + ( ( 512 + ( y * oy * iwidth ) + x * ox ) >> 10 ) );
145 // Set it back on the frame
146 mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "alpha", output, owidth * oheight, mlt_pool_release, NULL );
153 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
155 // Get the frame properties
156 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
158 // Get the filter from the stack
159 mlt_filter filter = mlt_frame_pop_service( this );
161 // Get the filter properties
162 mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
164 // Get the image scaler method
165 image_scaler scaler_method = mlt_properties_get_data( filter_properties, "method", NULL );
167 // Correct Width/height if necessary
168 if ( *width == 0 || *height == 0 )
170 *width = mlt_properties_get_int( properties, "normalised_width" );
171 *height = mlt_properties_get_int( properties, "normalised_height" );
174 // There can be problems with small images - avoid them (by hacking - gah)
175 if ( *width >= 6 && *height >= 6 )
178 int iheight = *height;
180 int oheight = *height;
181 char *interps = mlt_properties_get( properties, "rescale.interp" );
182 int wanted_format = *format;
184 // Default from the scaler if not specifed on the frame
185 if ( interps == NULL )
187 interps = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "interpolation" );
188 mlt_properties_set( properties, "rescale.interp", interps );
191 // If real_width/height exist, we want that as minimum information
192 if ( mlt_properties_get_int( properties, "real_width" ) )
194 iwidth = mlt_properties_get_int( properties, "real_width" );
195 iheight = mlt_properties_get_int( properties, "real_height" );
198 // Let the producer know what we are actually requested to obtain
199 if ( *format == mlt_image_yuv422 && strcmp( interps, "none" ) )
201 mlt_properties_set_int( properties, "rescale_width", *width );
202 mlt_properties_set_int( properties, "rescale_height", *height );
206 // When no scaling is requested, revert the requested dimensions if possible
207 mlt_properties_set_int( properties, "rescale_width", iwidth );
208 mlt_properties_set_int( properties, "rescale_height", iheight );
211 // Get the image as requested
212 mlt_frame_get_image( this, image, format, &iwidth, &iheight, writable );
214 // Get rescale interpretation again, in case the producer wishes to override scaling
215 interps = mlt_properties_get( properties, "rescale.interp" );
217 if ( *image != NULL && ( *format != mlt_image_yuv422 || ( iwidth != owidth || iheight != oheight ) ) )
219 // If the colour space is correct and scaling is off, do nothing
220 if ( *format == mlt_image_yuv422 && !strcmp( interps, "none" ) )
225 else if ( *format == mlt_image_yuv422 )
227 // Call the local scaler
228 scaler_method( this, image, *format, mlt_image_yuv422, iwidth, iheight, owidth, oheight );
232 else if ( *format == mlt_image_rgb24 && wanted_format == mlt_image_rgb24 )
234 // Call the local scaler
235 scaler_method( this, image, *format, mlt_image_rgb24, iwidth, iheight, owidth, oheight );
241 else if ( *format == mlt_image_rgb24 || *format == mlt_image_rgb24a )
243 // Call the local scaler
244 scaler_method( this, image, *format, mlt_image_yuv422, iwidth, iheight, owidth, oheight );
247 *format = mlt_image_yuv422;
265 // Store the requested width/height
267 int iheight = *height;
269 // Get the image as requested
270 mlt_frame_get_image( this, image, format, &iwidth, &iheight, writable );
272 // Too small - for now just assign as though we got what we wanted
281 /** Filter processing.
284 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
287 mlt_frame_push_service( frame, this );
289 // Push the get image method
290 mlt_frame_push_service( frame, filter_get_image );
295 /** Constructor for the filter.
298 mlt_filter filter_rescale_init( char *arg )
300 // Create a new scaler
301 mlt_filter this = mlt_filter_new( );
303 // If successful, then initialise it
306 // Get the properties
307 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
309 // Set the process method
310 this->process = filter_process;
312 // Set the inerpolation
313 mlt_properties_set( properties, "interpolation", arg == NULL ? "bilinear" : arg );
316 mlt_properties_set_data( properties, "method", filter_scale, 0, NULL, NULL );