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>
32 static int filter_get_image( mlt_frame
this, uint8_t **image
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
36 // Get the properties from the frame
37 mlt_properties properties
= MLT_FRAME_PROPERTIES( this );
39 // Pop the top of stack now
40 mlt_filter filter
= mlt_frame_pop_service( this );
42 // Assign requested width/height from our subordinate
44 int oheight
= *height
;
46 // Check for the special case - no aspect ratio means no problem :-)
47 if ( mlt_frame_get_aspect_ratio( this ) == 0 )
48 mlt_properties_set_double( properties
, "aspect_ratio", mlt_properties_get_double( properties
, "consumer_aspect_ratio" ) );
51 char *rescale
= mlt_properties_get( properties
, "rescale.interp" );
52 if ( rescale
!= NULL
&& !strcmp( rescale
, "none" ) )
53 return mlt_frame_get_image( this, image
, format
, width
, height
, writable
);
55 if ( mlt_properties_get_int( properties
, "distort" ) == 0 )
57 // Normalise the input and out display aspect
58 int normalised_width
= mlt_properties_get_int( properties
, "normalised_width" );
59 int normalised_height
= mlt_properties_get_int( properties
, "normalised_height" );
60 int real_width
= mlt_properties_get_int( properties
, "real_width" );
61 int real_height
= mlt_properties_get_int( properties
, "real_height" );
62 if ( real_width
== 0 )
63 real_width
= mlt_properties_get_int( properties
, "width" );
64 if ( real_height
== 0 )
65 real_height
= mlt_properties_get_int( properties
, "height" );
66 double input_ar
= mlt_frame_get_aspect_ratio( this ) * real_width
/ real_height
;
67 double output_ar
= mlt_properties_get_double( properties
, "consumer_aspect_ratio" ) * owidth
/ oheight
;
70 //fprintf( stderr, "normalised %dx%d output %dx%d %f %f\n", normalised_width, normalised_height, owidth, oheight, ( float )output_ar, ( float )mlt_properties_get_double( properties, "consumer_aspect_ratio" ) * owidth / oheight );
72 // Optimised for the input_ar > output_ar case (e.g. widescreen on standard)
73 int scaled_width
= input_ar
/ output_ar
* normalised_width
+ 0.5;
74 int scaled_height
= normalised_height
;
76 // Now ensure that our images fit in the output frame
77 if ( scaled_width
> normalised_width
)
79 scaled_width
= normalised_width
;
80 scaled_height
= output_ar
/ input_ar
* normalised_height
+ 0.5;
83 // Now calculate the actual image size that we want
84 owidth
= scaled_width
* owidth
/ normalised_width
;
85 oheight
= scaled_height
* oheight
/ normalised_height
;
87 // Tell frame we have conformed the aspect to the consumer
88 mlt_frame_set_aspect_ratio( this, mlt_properties_get_double( properties
, "consumer_aspect_ratio" ) );
91 // Now pass on the calculations down the line
92 mlt_properties_set_int( properties
, "resize_width", *width
);
93 mlt_properties_set_int( properties
, "resize_height", *height
);
96 error
= mlt_frame_get_image( this, image
, format
, &owidth
, &oheight
, writable
);
98 // We only know how to process yuv422 at the moment
99 if ( error
== 0 && *format
== mlt_image_yuv422
&& *image
!= NULL
)
101 // Get the requested scale operation
102 char *op
= mlt_properties_get( MLT_FILTER_PROPERTIES( filter
), "scale" );
104 // Correct field order if needed
105 if ( mlt_properties_get_int( properties
, "top_field_first" ) == 1 )
107 // Get the input image, width and height
109 uint8_t *image
= mlt_properties_get_data( properties
, "image", &size
);
111 // Keep the original image around to be destroyed on frame close
112 mlt_properties_rename( properties
, "image", "original_image" );
114 // Duplicate the last line in the field to avoid artifact
115 memcpy( image
+ oheight
* owidth
* 2, image
+ oheight
* owidth
* 2 - owidth
* 4, owidth
* 2 );
117 // Offset the image pointer by one line
121 // Set the new image pointer with no destructor
122 mlt_properties_set_data( properties
, "image", image
, size
, NULL
, NULL
);
124 // Set the normalised field order
125 mlt_properties_set_int( properties
, "top_field_first", 0 );
128 if ( !strcmp( op
, "affine" ) )
130 *image
= mlt_frame_rescale_yuv422( this, *width
, *height
);
132 else if ( strcmp( op
, "none" ) != 0 )
134 *image
= mlt_frame_resize_yuv422( this, *width
, *height
);
146 /** Filter processing.
149 static mlt_frame
filter_process( mlt_filter
this, mlt_frame frame
)
151 // Push this on to the service stack
152 mlt_frame_push_service( frame
, this );
154 // Push the get_image method on to the stack
155 mlt_frame_push_get_image( frame
, filter_get_image
);
160 /** Constructor for the filter.
163 mlt_filter
filter_resize_init( char *arg
)
165 mlt_filter
this = calloc( sizeof( struct mlt_filter_s
), 1 );
166 if ( mlt_filter_init( this, this ) == 0 )
168 this->process
= filter_process
;
169 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "scale", arg
== NULL ?
"off" : arg
);