2 * filter_resize.c -- resizing filter
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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_resize.h"
23 #include <framework/mlt_frame.h>
29 static int get_value( mlt_properties properties, char *preferred, char *fallback )
31 int value = mlt_properties_get_int( properties, preferred );
33 value = mlt_properties_get_int( properties, fallback );
40 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
42 // Get the properties from the frame
43 mlt_properties properties = mlt_frame_properties( this );
45 // Assign requested width/height from our subordinate
47 int oheight = *height;
50 char *rescale = mlt_properties_get( properties, "rescale.interp" );
51 if ( rescale != NULL && !strcmp( rescale, "none" ) )
52 return mlt_frame_get_image( this, image, format, width, height, writable );
54 if ( mlt_properties_get( properties, "distort" ) == NULL )
56 // Normalise the input and out display aspect
57 int normalised_width = mlt_properties_get_int( properties, "normalised_width" );
58 int normalised_height = mlt_properties_get_int( properties, "normalised_height" );
59 double input_ar = mlt_frame_get_aspect_ratio( this );
60 double output_ar = mlt_properties_get_double( properties, "consumer_aspect_ratio" );
62 // Optimised for the input_ar > output_ar case (e.g. widescreen on standard)
63 int scaled_width = normalised_width;
64 int scaled_height = output_ar / input_ar * normalised_height;
66 // Now ensure that our images fit in the normalised frame
67 if ( scaled_height > normalised_height )
69 scaled_width = input_ar / output_ar * normalised_width;
70 scaled_height = normalised_height;
72 //fprintf( stderr, "resize: %dx%d from %dx%d input aspect %f output aspect %f\n",
73 // scaled_width, scaled_height, normalised_width, normalised_height, input_ar, output_ar );
76 int real_width = get_value( properties, "real_height", "height" );
77 int real_height = get_value( properties, "real_height", "height" );
79 if ( ( real_height * 2 ) == normalised_height )
81 scaled_width = normalised_width;
82 scaled_height = normalised_height;
86 // Now calculate the actual image size that we want
87 owidth = scaled_width * owidth / normalised_width;
88 oheight = scaled_height * oheight / normalised_height;
90 // Tell frame we have conformed the aspect to the consumer
91 mlt_frame_set_aspect_ratio( this, output_ar );
94 // Now pass on the calculations down the line
95 mlt_properties_set_int( properties, "resize_width", *width );
96 mlt_properties_set_int( properties, "resize_height", *height );
99 mlt_frame_get_image( this, image, format, &owidth, &oheight, writable );
101 // Correct field order if needed
102 if ( mlt_properties_get_int( properties, "top_field_first" ) == 1 )
104 // Get the input image, width and height
106 uint8_t *image = mlt_properties_get_data( properties, "image", &size );
108 // Keep the original image around to be destroyed on frame close
109 mlt_properties_rename( properties, "image", "original_image" );
111 // Offset the image pointer by one line
115 // Set the new image pointer with no destructor
116 mlt_properties_set_data( properties, "image", image, size, NULL, NULL );
118 // Set the normalised field order
119 mlt_properties_set_int( properties, "top_field_first", 0 );
122 if ( !strcmp( mlt_properties_get( properties, "resize.scale" ), "affine" ) )
123 *image = mlt_frame_rescale_yuv422( this, *width, *height );
124 else if ( strcmp( mlt_properties_get( properties, "resize.scale" ), "none" ) != 0 )
125 *image = mlt_frame_resize_yuv422( this, *width, *height );
134 /** Filter processing.
137 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
139 mlt_frame_push_get_image( frame, filter_get_image );
140 mlt_properties_set( mlt_frame_properties( frame ), "resize.scale", mlt_properties_get( mlt_filter_properties( this ), "scale" ) );
144 /** Constructor for the filter.
147 mlt_filter filter_resize_init( char *arg )
149 mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
150 if ( mlt_filter_init( this, this ) == 0 )
152 this->process = filter_process;
154 mlt_properties_set( mlt_filter_properties( this ), "scale", arg );
156 mlt_properties_set( mlt_filter_properties( this ), "scale", "off" );