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>
24 #include <framework/mlt_profile.h>
31 /** virtual function declaration for an image scaler
33 * image scaler implementations are expected to support the following in and out formats:
41 typedef int ( *image_scaler )( mlt_frame frame, uint8_t **image, mlt_image_format *format, int iwidth, int iheight, int owidth, int oheight );
43 static int filter_scale( mlt_frame frame, uint8_t **image, mlt_image_format *format, int iwidth, int iheight, int owidth, int oheight )
45 // Create the output image
46 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
49 int istride = iwidth * 2;
50 int ostride = owidth * 2;
51 iwidth = iwidth - ( iwidth % 4 );
53 // Derived coordinates
57 int out_x_range = owidth / 2;
58 int out_y_range = oheight / 2;
59 int in_x_range = iwidth / 2;
60 int in_y_range = iheight / 2;
63 register uint8_t *out_line = output;
64 register uint8_t *out_ptr;
66 // Calculate a middle pointer
67 uint8_t *in_middle = *image + istride * in_y_range + in_x_range * 2;
70 // Generate the affine transform scaling values
71 register int scale_width = ( iwidth << 16 ) / owidth;
72 register int scale_height = ( iheight << 16 ) / oheight;
73 register int base = 0;
75 int outer = out_x_range * scale_width;
76 int bottom = out_y_range * scale_height;
78 // Loop for the entirety of our output height.
79 for ( dy = - bottom; dy < bottom; dy += scale_height )
81 // Start at the beginning of the line
84 // Pointer to the middle of the input line
85 in_line = in_middle + ( dy >> 16 ) * istride;
87 // Loop for the entirety of our output row.
88 for ( dx = - outer; dx < outer; dx += scale_width )
92 *out_ptr ++ = *( in_line + base );
94 *out_ptr ++ = *( in_line + base + 1 );
98 *out_ptr ++ = *( in_line + base );
100 *out_ptr ++ = *( in_line + base + 3 );
102 // Move to next output line
106 // Now update the frame
107 mlt_frame_set_image( frame, output, owidth * ( oheight + 1 ) * 2, mlt_pool_release );
113 static void scale_alpha( mlt_frame frame, int iwidth, int iheight, int owidth, int oheight )
116 uint8_t *output = NULL;
117 uint8_t *input = mlt_frame_get_alpha_mask( frame );
121 uint8_t *out_line, *in_line;
122 register int i, j, x, y;
123 register int ox = ( iwidth << 16 ) / owidth;
124 register int oy = ( iheight << 16 ) / oheight;
126 output = mlt_pool_alloc( owidth * oheight );
129 // Loop for the entirety of our output height.
130 for ( i = 0, y = (oy >> 1); i < oheight; i++, y += oy )
132 in_line = &input[ (y >> 16) * iwidth ];
133 for ( j = 0, x = (ox >> 1); j < owidth; j++, x += ox )
134 *out_line ++ = in_line[ x >> 16 ];
137 // Set it back on the frame
138 mlt_frame_set_alpha( frame, output, owidth * oheight, mlt_pool_release );
145 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
149 // Get the frame properties
150 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
152 // Get the filter from the stack
153 mlt_filter filter = mlt_frame_pop_service( frame );
155 // Get the filter properties
156 mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
158 // Get the image scaler method
159 image_scaler scaler_method = mlt_properties_get_data( filter_properties, "method", NULL );
161 // Correct Width/height if necessary
162 if ( *width == 0 || *height == 0 )
164 mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( filter ) );
165 *width = profile->width;
166 *height = profile->height;
169 // There can be problems with small images - avoid them (by hacking - gah)
170 if ( *width >= 6 && *height >= 6 )
173 int iheight = *height;
174 double factor = mlt_properties_get_double( filter_properties, "factor" );
175 factor = factor > 0 ? factor : 1.0;
176 int owidth = *width * factor;
177 int oheight = *height * factor;
178 char *interps = mlt_properties_get( properties, "rescale.interp" );
180 // Default from the scaler if not specifed on the frame
181 if ( interps == NULL )
183 interps = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "interpolation" );
184 mlt_properties_set( properties, "rescale.interp", interps );
187 // If meta.media.width/height exist, we want that as minimum information
188 if ( mlt_properties_get_int( properties, "meta.media.width" ) )
190 iwidth = mlt_properties_get_int( properties, "meta.media.width" );
191 iheight = mlt_properties_get_int( properties, "meta.media.height" );
194 // Let the producer know what we are actually requested to obtain
195 if ( strcmp( interps, "none" ) )
197 mlt_properties_set_int( properties, "rescale_width", *width );
198 mlt_properties_set_int( properties, "rescale_height", *height );
202 // When no scaling is requested, revert the requested dimensions if possible
203 mlt_properties_set_int( properties, "rescale_width", iwidth );
204 mlt_properties_set_int( properties, "rescale_height", iheight );
207 // Deinterlace if height is changing to prevent fields mixing on interpolation
208 // One exception: non-interpolated, integral scaling
209 if ( iheight != oheight && ( strcmp( interps, "nearest" ) || ( iheight % oheight != 0 ) ) )
210 mlt_properties_set_int( properties, "consumer_deinterlace", 1 );
212 // Convert the image to yuv422 when using the local scaler
213 if ( scaler_method == filter_scale )
214 *format = mlt_image_yuv422;
216 // Get the image as requested
217 mlt_frame_get_image( frame, image, format, &iwidth, &iheight, writable );
219 // Get rescale interpretation again, in case the producer wishes to override scaling
220 interps = mlt_properties_get( properties, "rescale.interp" );
222 if ( *image && strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
224 mlt_log_debug( MLT_FILTER_SERVICE( filter ), "%dx%d -> %dx%d (%s) %s\n",
225 iwidth, iheight, owidth, oheight, mlt_image_format_name( *format ), interps );
227 // If valid colorspace
228 if ( *format == mlt_image_yuv422 || *format == mlt_image_rgb24 ||
229 *format == mlt_image_rgb24a || *format == mlt_image_opengl )
231 // Call the virtual function
232 scaler_method( frame, image, format, iwidth, iheight, owidth, oheight );
236 // Scale the alpha channel only if exists and not correct size
238 mlt_properties_get_data( properties, "alpha", &alpha_size );
239 if ( alpha_size > 0 && alpha_size != ( owidth * oheight ) && alpha_size != ( owidth * ( oheight + 1 ) ) )
240 scale_alpha( frame, iwidth, iheight, owidth, oheight );
256 /** Filter processing.
259 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
262 mlt_frame_push_service( frame, filter );
264 // Push the get image method
265 mlt_frame_push_service( frame, filter_get_image );
270 /** Constructor for the filter.
273 mlt_filter filter_rescale_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
275 // Create a new scaler
276 mlt_filter filter = mlt_filter_new( );
278 // If successful, then initialise it
279 if ( filter != NULL )
281 // Get the properties
282 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
284 // Set the process method
285 filter->process = filter_process;
287 // Set the inerpolation
288 mlt_properties_set( properties, "interpolation", arg == NULL ? "bilinear" : arg );
291 mlt_properties_set_data( properties, "method", filter_scale, 0, NULL, NULL );