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>
23 #include <framework/mlt_log.h>
30 /** virtual function declaration for an image scaler
32 * image scaler implementations are expected to support the following in and out formats:
40 typedef int ( *image_scaler )( mlt_frame this, uint8_t **image, mlt_image_format *format, int iwidth, int iheight, int owidth, int oheight );
42 static int filter_scale( mlt_frame this, uint8_t **image, mlt_image_format *format, int iwidth, int iheight, int owidth, int oheight )
44 // Create the output image
45 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
48 int istride = iwidth * 2;
49 int ostride = owidth * 2;
50 iwidth = iwidth - ( iwidth % 4 );
52 // Derived coordinates
56 int out_x_range = owidth / 2;
57 int out_y_range = oheight / 2;
58 int in_x_range = iwidth / 2;
59 int in_y_range = iheight / 2;
62 register uint8_t *out_line = output;
63 register uint8_t *out_ptr;
65 // Calculate a middle pointer
66 uint8_t *in_middle = *image + istride * in_y_range + in_x_range * 2;
69 // Generate the affine transform scaling values
70 register int scale_width = ( iwidth << 16 ) / owidth;
71 register int scale_height = ( iheight << 16 ) / oheight;
72 register int base = 0;
74 int outer = out_x_range * scale_width;
75 int bottom = out_y_range * scale_height;
77 // Loop for the entirety of our output height.
78 for ( dy = - bottom; dy < bottom; dy += scale_height )
80 // Start at the beginning of the line
83 // Pointer to the middle of the input line
84 in_line = in_middle + ( dy >> 16 ) * istride;
86 // Loop for the entirety of our output row.
87 for ( dx = - outer; dx < outer; dx += scale_width )
91 *out_ptr ++ = *( in_line + base );
93 *out_ptr ++ = *( in_line + base + 1 );
97 *out_ptr ++ = *( in_line + base );
99 *out_ptr ++ = *( in_line + base + 3 );
101 // Move to next output line
105 // Now update the frame
106 mlt_frame_set_image( this, output, owidth * ( oheight + 1 ) * 2, mlt_pool_release );
112 static void scale_alpha( mlt_frame this, int iwidth, int iheight, int owidth, int oheight )
115 uint8_t *output = NULL;
116 uint8_t *input = mlt_frame_get_alpha_mask( this );
120 uint8_t *out_line, *in_line;
121 register int i, j, x, y;
122 register int ox = ( iwidth << 16 ) / owidth;
123 register int oy = ( iheight << 16 ) / oheight;
125 output = mlt_pool_alloc( owidth * oheight );
128 // Loop for the entirety of our output height.
129 for ( i = 0, y = (oy >> 1); i < oheight; i++, y += oy )
131 in_line = &input[ (y >> 16) * iwidth ];
132 for ( j = 0, x = (ox >> 1); j < owidth; j++, x += ox )
133 *out_line ++ = in_line[ x >> 16 ];
136 // Set it back on the frame
137 mlt_frame_set_alpha( this, output, owidth * oheight, mlt_pool_release );
144 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;
172 double factor = mlt_properties_get_double( filter_properties, "factor" );
173 factor = factor > 0 ? factor : 1.0;
174 int owidth = *width * factor;
175 int oheight = *height * factor;
176 char *interps = mlt_properties_get( properties, "rescale.interp" );
178 // Default from the scaler if not specifed on the frame
179 if ( interps == NULL )
181 interps = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "interpolation" );
182 mlt_properties_set( properties, "rescale.interp", interps );
185 // If real_width/height exist, we want that as minimum information
186 if ( mlt_properties_get_int( properties, "real_width" ) )
188 iwidth = mlt_properties_get_int( properties, "real_width" );
189 iheight = mlt_properties_get_int( properties, "real_height" );
192 // Let the producer know what we are actually requested to obtain
193 if ( strcmp( interps, "none" ) )
195 mlt_properties_set_int( properties, "rescale_width", *width );
196 mlt_properties_set_int( properties, "rescale_height", *height );
200 // When no scaling is requested, revert the requested dimensions if possible
201 mlt_properties_set_int( properties, "rescale_width", iwidth );
202 mlt_properties_set_int( properties, "rescale_height", iheight );
205 // Deinterlace if height is changing to prevent fields mixing on interpolation
206 // One exception: non-interpolated, integral scaling
207 if ( iheight != oheight && ( strcmp( interps, "nearest" ) || ( iheight % oheight != 0 ) ) )
208 mlt_properties_set_int( properties, "consumer_deinterlace", 1 );
210 // Convert the image to yuv422 when using the local scaler
211 if ( scaler_method == filter_scale )
212 *format = mlt_image_yuv422;
214 // Get the image as requested
215 mlt_frame_get_image( this, image, format, &iwidth, &iheight, writable );
217 // Get rescale interpretation again, in case the producer wishes to override scaling
218 interps = mlt_properties_get( properties, "rescale.interp" );
220 if ( *image && strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
222 mlt_log_debug( MLT_FILTER_SERVICE( filter ), "%dx%d -> %dx%d (%s) %s\n",
223 iwidth, iheight, owidth, oheight, mlt_image_format_name( *format ), interps );
225 // If valid colorspace
226 if ( *format == mlt_image_yuv422 || *format == mlt_image_rgb24 ||
227 *format == mlt_image_rgb24a || *format == mlt_image_opengl )
229 // Call the virtual function
230 scaler_method( this, image, format, iwidth, iheight, owidth, oheight );
234 // Scale the alpha channel only if exists and not correct size
236 mlt_properties_get_data( properties, "alpha", &alpha_size );
237 if ( alpha_size > 0 && alpha_size != ( owidth * oheight ) && alpha_size != ( owidth * ( oheight + 1 ) ) )
238 scale_alpha( this, iwidth, iheight, owidth, oheight );
254 /** Filter processing.
257 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
260 mlt_frame_push_service( frame, this );
262 // Push the get image method
263 mlt_frame_push_service( frame, filter_get_image );
268 /** Constructor for the filter.
271 mlt_filter filter_rescale_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
273 // Create a new scaler
274 mlt_filter this = mlt_filter_new( );
276 // If successful, then initialise it
279 // Get the properties
280 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
282 // Set the process method
283 this->process = filter_process;
285 // Set the inerpolation
286 mlt_properties_set( properties, "interpolation", arg == NULL ? "bilinear" : arg );
289 mlt_properties_set_data( properties, "method", filter_scale, 0, NULL, NULL );