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>
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 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 )
35 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
37 // Get the rescaling interpolsation
38 char *interps = mlt_properties_get( properties, "rescale.interp" );
40 // Carry out the rescaling
41 if ( iformat == mlt_image_yuv422 && oformat == mlt_image_yuv422 )
44 mlt_frame_rescale_yuv422( this, owidth, oheight );
47 *image = mlt_properties_get_data( properties, "image", NULL );
49 else if ( iformat == mlt_image_rgb24 || iformat == mlt_image_rgb24a )
51 int bpp = (iformat == mlt_image_rgb24a ? 4 : 3 );
53 // Create the yuv image
54 uint8_t *output = mlt_pool_alloc( iwidth * ( iheight + 1 ) * 2 );
56 if ( strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
58 // Extract YUV422 and alpha
61 // Allocate the alpha mask
62 uint8_t *alpha = mlt_pool_alloc( iwidth * ( iheight + 1 ) );
64 // Convert the image and extract alpha
65 mlt_convert_rgb24a_to_yuv422( *image, iwidth, iheight, iwidth * 4, output, alpha );
67 mlt_properties_set_data( properties, "alpha", alpha, iwidth * ( iheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
71 // No alpha to extract
72 mlt_convert_rgb24_to_yuv422( *image, iwidth, iheight, iwidth * 3, output );
75 mlt_properties_set_data( properties, "image", output, iwidth * ( iheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
78 output = mlt_frame_rescale_yuv422( this, owidth, oheight );
83 // Extract YUV422 and alpha
86 // Allocate the alpha mask
87 uint8_t *alpha = mlt_pool_alloc( owidth * ( oheight + 1 ) );
89 // Convert the image and extract alpha
90 mlt_convert_rgb24a_to_yuv422( *image, owidth, oheight, owidth * 4, output, alpha );
92 mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
96 // No alpha to extract
97 mlt_convert_rgb24_to_yuv422( *image, owidth, oheight, owidth * 3, output );
101 // Now update the frame
102 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
103 mlt_properties_set_int( properties, "width", owidth );
104 mlt_properties_set_int( properties, "height", oheight );
112 static void scale_alpha( mlt_frame this, int iwidth, int iheight, int owidth, int oheight )
114 uint8_t *output = NULL;
115 uint8_t *input = mlt_frame_get_alpha_mask( this );
121 int ox = ( iwidth << 10 ) / owidth;
122 int oy = ( iheight << 10 ) / oheight;
124 output = mlt_pool_alloc( owidth * oheight );
127 // Loop for the entirety of our output height.
128 for ( y = 0; y < oheight; y ++ )
129 for ( x = 0; x < owidth; x ++ )
130 *out_line ++ = *( input + ( ( 512 + ( y * oy * iwidth ) + x * ox ) >> 10 ) );
132 // Set it back on the frame
133 mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "alpha", output, owidth * oheight, mlt_pool_release, NULL );
140 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
142 // Get the frame properties
143 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
145 // Get the filter from the stack
146 mlt_filter filter = mlt_frame_pop_service( this );
148 // Get the filter properties
149 mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
151 // Get the image scaler method
152 image_scaler scaler_method = mlt_properties_get_data( filter_properties, "method", NULL );
154 // Correct Width/height if necessary
155 if ( *width == 0 || *height == 0 )
157 *width = mlt_properties_get_int( properties, "normalised_width" );
158 *height = mlt_properties_get_int( properties, "normalised_height" );
161 // There can be problems with small images - avoid them (by hacking - gah)
162 if ( *width >= 6 && *height >= 6 )
165 int iheight = *height;
167 int oheight = *height;
168 char *interps = mlt_properties_get( properties, "rescale.interp" );
169 int wanted_format = *format;
171 // Default from the scaler if not specifed on the frame
172 if ( interps == NULL )
174 interps = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "interpolation" );
175 mlt_properties_set( properties, "rescale.interp", interps );
178 // If real_width/height exist, we want that as minimum information
179 if ( mlt_properties_get_int( properties, "real_width" ) )
181 iwidth = mlt_properties_get_int( properties, "real_width" );
182 iheight = mlt_properties_get_int( properties, "real_height" );
185 // Let the producer know what we are actually requested to obtain
186 if ( *format == mlt_image_yuv422 && strcmp( interps, "none" ) )
188 mlt_properties_set_int( properties, "rescale_width", *width );
189 mlt_properties_set_int( properties, "rescale_height", *height );
193 // When no scaling is requested, revert the requested dimensions if possible
194 mlt_properties_set_int( properties, "rescale_width", iwidth );
195 mlt_properties_set_int( properties, "rescale_height", iheight );
198 // Get the image as requested
199 mlt_frame_get_image( this, image, format, &iwidth, &iheight, writable );
201 // Get rescale interpretation again, in case the producer wishes to override scaling
202 interps = mlt_properties_get( properties, "rescale.interp" );
204 if ( *image != NULL && ( *format != mlt_image_yuv422 || ( iwidth != owidth || iheight != oheight ) ) )
206 // If the colour space is correct and scaling is off, do nothing
207 if ( *format == mlt_image_yuv422 && !strcmp( interps, "none" ) )
212 else if ( *format == mlt_image_yuv422 )
214 // Call the local scaler
215 scaler_method( this, image, *format, mlt_image_yuv422, iwidth, iheight, owidth, oheight );
220 scale_alpha( this, iwidth, iheight, owidth, oheight );
222 else if ( *format == mlt_image_rgb24 && wanted_format == mlt_image_rgb24 )
224 // Call the local scaler
225 scaler_method( this, image, *format, mlt_image_rgb24, iwidth, iheight, owidth, oheight );
232 scale_alpha( this, iwidth, iheight, owidth, oheight );
234 else if ( *format == mlt_image_rgb24 || *format == mlt_image_rgb24a )
236 // Call the local scaler
237 scaler_method( this, image, *format, mlt_image_yuv422, iwidth, iheight, owidth, oheight );
240 *format = mlt_image_yuv422;
245 scale_alpha( this, iwidth, iheight, owidth, oheight );
256 scale_alpha( this, iwidth, iheight, owidth, oheight );
263 // Store the requested width/height
265 int iheight = *height;
267 // Get the image as requested
268 mlt_frame_get_image( this, image, format, &iwidth, &iheight, writable );
270 // Too small - for now just assign as though we got what we wanted
279 /** Filter processing.
282 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
285 mlt_frame_push_service( frame, this );
287 // Push the get image method
288 mlt_frame_push_service( frame, filter_get_image );
293 /** Constructor for the filter.
296 mlt_filter filter_rescale_init( char *arg )
298 // Create a new scaler
299 mlt_filter this = mlt_filter_new( );
301 // If successful, then initialise it
304 // Get the properties
305 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
307 // Set the process method
308 this->process = filter_process;
310 // Set the inerpolation
311 mlt_properties_set( properties, "interpolation", arg == NULL ? "bilinear" : arg );
314 mlt_properties_set_data( properties, "method", filter_scale, 0, NULL, NULL );