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 <framework/mlt_filter.h>
22 #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 void scale_alpha( mlt_frame this, int iwidth, int iheight, int owidth, int oheight );
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 )
36 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
38 // Get the rescaling interpolsation
39 char *interps = mlt_properties_get( properties, "rescale.interp" );
41 // Carry out the rescaling
42 if ( iformat == mlt_image_yuv422 && oformat == mlt_image_yuv422 )
45 mlt_frame_rescale_yuv422( this, owidth, oheight );
48 *image = mlt_properties_get_data( properties, "image", NULL );
50 else if ( iformat == mlt_image_rgb24 || iformat == mlt_image_rgb24a )
52 int bpp = (iformat == mlt_image_rgb24a ? 4 : 3 );
54 // Create the yuv image
55 uint8_t *output = mlt_pool_alloc( iwidth * ( iheight + 1 ) * 2 );
57 if ( strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
59 // Extract YUV422 and alpha
62 // Allocate the alpha mask
63 uint8_t *alpha = mlt_pool_alloc( iwidth * ( iheight + 1 ) );
65 // Convert the image and extract alpha
66 mlt_convert_rgb24a_to_yuv422( *image, iwidth, iheight, iwidth * 4, output, alpha );
68 mlt_properties_set_data( properties, "alpha", alpha, iwidth * ( iheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
70 scale_alpha( this, iwidth, iheight, owidth, oheight );
74 // No alpha to extract
75 mlt_convert_rgb24_to_yuv422( *image, iwidth, iheight, iwidth * 3, output );
78 mlt_properties_set_data( properties, "image", output, iwidth * ( iheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
81 output = mlt_frame_rescale_yuv422( this, owidth, oheight );
86 // Extract YUV422 and alpha
89 // Allocate the alpha mask
90 uint8_t *alpha = mlt_pool_alloc( owidth * ( oheight + 1 ) );
92 // Convert the image and extract alpha
93 mlt_convert_rgb24a_to_yuv422( *image, owidth, oheight, owidth * 4, output, alpha );
95 mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
97 scale_alpha( this, iwidth, iheight, owidth, oheight );
101 // No alpha to extract
102 mlt_convert_rgb24_to_yuv422( *image, owidth, oheight, owidth * 3, output );
106 // Now update the frame
107 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
108 mlt_properties_set_int( properties, "width", owidth );
109 mlt_properties_set_int( properties, "height", oheight );
117 static void scale_alpha( mlt_frame this, int iwidth, int iheight, int owidth, int oheight )
120 uint8_t *output = NULL;
121 uint8_t *input = mlt_frame_get_alpha_mask( this );
127 int ox = ( iwidth << 10 ) / owidth;
128 int oy = ( iheight << 10 ) / oheight;
130 output = mlt_pool_alloc( owidth * oheight );
133 // Loop for the entirety of our output height.
134 for ( y = 0; y < oheight; y ++ )
135 for ( x = 0; x < owidth; x ++ )
136 *out_line ++ = *( input + ( ( 512 + ( y * oy * iwidth ) + x * ox ) >> 10 ) );
138 // Set it back on the frame
139 mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "alpha", output, owidth * oheight, mlt_pool_release, NULL );
146 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
148 // Get the frame properties
149 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
151 // Get the filter from the stack
152 mlt_filter filter = mlt_frame_pop_service( this );
154 // Get the filter properties
155 mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
157 // Get the image scaler method
158 image_scaler scaler_method = mlt_properties_get_data( filter_properties, "method", NULL );
160 // Correct Width/height if necessary
161 if ( *width == 0 || *height == 0 )
163 *width = mlt_properties_get_int( properties, "normalised_width" );
164 *height = mlt_properties_get_int( properties, "normalised_height" );
167 // There can be problems with small images - avoid them (by hacking - gah)
168 if ( *width >= 6 && *height >= 6 )
171 int iheight = *height;
173 int oheight = *height;
174 char *interps = mlt_properties_get( properties, "rescale.interp" );
175 int wanted_format = *format;
177 // Default from the scaler if not specifed on the frame
178 if ( interps == NULL )
180 interps = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "interpolation" );
181 mlt_properties_set( properties, "rescale.interp", interps );
184 // If real_width/height exist, we want that as minimum information
185 if ( mlt_properties_get_int( properties, "real_width" ) )
187 iwidth = mlt_properties_get_int( properties, "real_width" );
188 iheight = mlt_properties_get_int( properties, "real_height" );
191 // Let the producer know what we are actually requested to obtain
192 if ( *format == mlt_image_yuv422 && strcmp( interps, "none" ) )
194 mlt_properties_set_int( properties, "rescale_width", *width );
195 mlt_properties_set_int( properties, "rescale_height", *height );
199 // When no scaling is requested, revert the requested dimensions if possible
200 mlt_properties_set_int( properties, "rescale_width", iwidth );
201 mlt_properties_set_int( properties, "rescale_height", iheight );
204 // Deinterlace if height is changing to prevent fields mixing on interpolation
205 // One exception: non-interpolated, integral scaling
206 if ( iheight != oheight && ( strcmp( interps, "nearest" ) || ( iheight % oheight != 0 ) ) )
207 mlt_properties_set_int( properties, "consumer_deinterlace", 1 );
209 // Get the image as requested
210 mlt_frame_get_image( this, image, format, &iwidth, &iheight, writable );
212 // Get rescale interpretation again, in case the producer wishes to override scaling
213 interps = mlt_properties_get( properties, "rescale.interp" );
215 if ( *image != NULL && ( *format != mlt_image_yuv422 || ( iwidth != owidth || iheight != oheight ) ) )
217 // If the colour space is correct and scaling is off, do nothing
218 if ( *format == mlt_image_yuv422 && !strcmp( interps, "none" ) )
223 else if ( *format == mlt_image_yuv422 )
225 // Call the local scaler
226 scaler_method( this, image, *format, mlt_image_yuv422, iwidth, iheight, owidth, oheight );
230 else if ( *format == mlt_image_rgb24 && wanted_format == mlt_image_rgb24 )
232 // Call the local scaler
233 scaler_method( this, image, *format, mlt_image_rgb24, iwidth, iheight, owidth, oheight );
239 else if ( *format == mlt_image_rgb24 || *format == mlt_image_rgb24a )
241 // Call the local scaler
242 scaler_method( this, image, *format, mlt_image_yuv422, iwidth, iheight, owidth, oheight );
245 *format = mlt_image_yuv422;
254 if ( *width == owidth )
256 // Scale the alpha channel only if exists and not correct size
258 mlt_properties_get_data( properties, "alpha", &alpha_size );
259 if ( alpha_size > 0 && alpha_size != ( owidth * oheight ) && alpha_size != ( owidth * ( oheight + 1 ) ) )
260 scale_alpha( this, iwidth, iheight, owidth, oheight );
271 // Store the requested width/height
273 int iheight = *height;
275 // Get the image as requested
276 mlt_frame_get_image( this, image, format, &iwidth, &iheight, writable );
278 // Too small - for now just assign as though we got what we wanted
287 /** Filter processing.
290 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
293 mlt_frame_push_service( frame, this );
295 // Push the get image method
296 mlt_frame_push_service( frame, filter_get_image );
301 /** Constructor for the filter.
304 mlt_filter filter_rescale_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
306 // Create a new scaler
307 mlt_filter this = mlt_filter_new( );
309 // If successful, then initialise it
312 // Get the properties
313 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
315 // Set the process method
316 this->process = filter_process;
318 // Set the inerpolation
319 mlt_properties_set( properties, "interpolation", arg == NULL ? "bilinear" : arg );
322 mlt_properties_set_data( properties, "method", filter_scale, 0, NULL, NULL );