]> git.sesse.net Git - mlt/blob - src/modules/core/filter_resize.c
Merge ../mlt++
[mlt] / src / modules / core / filter_resize.c
1 /*
2  * filter_resize.c -- resizing filter
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <math.h>
28
29 /** Swapbytes inline.
30 */
31
32 static inline void swap_bytes( uint8_t *upper, uint8_t *lower )
33 {
34         uint8_t t = *lower;
35         *lower = *upper;
36         *upper = t;
37 }
38
39 /** Do it :-).
40 */
41
42 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
43 {
44         int error = 0;
45
46         // Get the properties from the frame
47         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
48
49         // Pop the top of stack now
50         mlt_filter filter = mlt_frame_pop_service( this );
51
52         // Retrieve the aspect ratio
53         double aspect_ratio = mlt_deque_pop_back_double( MLT_FRAME_IMAGE_STACK( this ) );
54
55         // Correct Width/height if necessary
56         if ( *width == 0 || *height == 0 )
57         {
58                 *width = mlt_properties_get_int( properties, "normalised_width" );
59                 *height = mlt_properties_get_int( properties, "normalised_height" );
60         }
61
62         // Assign requested width/height from our subordinate
63         int owidth = *width;
64         int oheight = *height;
65
66         // Check for the special case - no aspect ratio means no problem :-)
67         if ( aspect_ratio == 0.0 )
68                 aspect_ratio = mlt_properties_get_double( properties, "consumer_aspect_ratio" );
69
70         // Reset the aspect ratio
71         mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
72
73         // Hmmm...
74         char *rescale = mlt_properties_get( properties, "rescale.interp" );
75         if ( rescale != NULL && !strcmp( rescale, "none" ) )
76                 return mlt_frame_get_image( this, image, format, width, height, writable );
77
78         if ( mlt_properties_get_int( properties, "distort" ) == 0 )
79         {
80                 // Normalise the input and out display aspect
81                 int normalised_width = mlt_properties_get_int( properties, "normalised_width" );
82                 int normalised_height = mlt_properties_get_int( properties, "normalised_height" );
83                 int real_width = mlt_properties_get_int( properties, "real_width" );
84                 int real_height = mlt_properties_get_int( properties, "real_height" );
85                 if ( real_width == 0 )
86                         real_width = mlt_properties_get_int( properties, "width" );
87                 if ( real_height == 0 )
88                         real_height = mlt_properties_get_int( properties, "height" );
89                 double input_ar = aspect_ratio * real_width / real_height;
90                 double output_ar = mlt_properties_get_double( properties, "consumer_aspect_ratio" ) * owidth / oheight;
91                 
92 //              fprintf( stderr, "real %dx%d normalised %dx%d output %dx%d sar %f in-dar %f out-dar %f\n",
93 //              real_width, real_height, normalised_width, normalised_height, owidth, oheight, aspect_ratio, input_ar, output_ar);
94
95                 // Optimised for the input_ar > output_ar case (e.g. widescreen on standard)
96                 int scaled_width = rint( ( input_ar * normalised_width ) / output_ar );
97                 int scaled_height = normalised_height;
98
99                 // Now ensure that our images fit in the output frame
100                 if ( scaled_width > normalised_width )
101                 {
102                         scaled_width = normalised_width;
103                         scaled_height = rint( ( output_ar * normalised_height ) / input_ar );
104                 }
105
106                 // Now calculate the actual image size that we want
107                 owidth = rint( scaled_width * owidth / normalised_width );
108                 oheight = rint( scaled_height * oheight / normalised_height );
109
110                 // Tell frame we have conformed the aspect to the consumer
111                 mlt_frame_set_aspect_ratio( this, mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
112         }
113
114         mlt_properties_set_int( properties, "distort", 0 );
115
116         // Now pass on the calculations down the line
117         mlt_properties_set_int( properties, "resize_width", *width );
118         mlt_properties_set_int( properties, "resize_height", *height );
119
120         // Now get the image
121         error = mlt_frame_get_image( this, image, format, &owidth, &oheight, writable );
122
123         // We only know how to process yuv422 at the moment
124         if ( error == 0 && *format == mlt_image_yuv422 && *image != NULL )
125         {
126                 // Get the requested scale operation
127                 char *op = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "scale" );
128
129                 // Provides a manual override for misreported field order
130                 if ( mlt_properties_get( properties, "meta.top_field_first" ) )
131                         mlt_properties_set_int( properties, "top_field_first", mlt_properties_get_int( properties, "meta.top_field_first" ) );
132
133                 // Correct field order if needed
134                 if ( mlt_properties_get_int( properties, "top_field_first" ) == 1 )
135                 {
136                         // Get the input image, width and height
137                         int size;
138                         uint8_t *image = mlt_properties_get_data( properties, "image", &size );
139                         uint8_t *ptr = image + owidth * 2;
140                         memmove( ptr, image, size - owidth * 2 );
141                         
142                         // Set the normalised field order
143                         mlt_properties_set_int( properties, "top_field_first", 0 );
144                         mlt_properties_set_int( properties, "meta.top_field_first", 0 );
145                 }
146
147                 if ( !strcmp( op, "affine" ) )
148                 {
149                         *image = mlt_frame_rescale_yuv422( this, *width, *height );
150                 }
151                 else if ( strcmp( op, "none" ) != 0 )
152                 {
153                         *image = mlt_frame_resize_yuv422( this, *width, *height );
154                 }
155                 else
156                 {
157                         *width = owidth;
158                         *height = oheight;
159                 }
160         }
161
162         return error;
163 }
164
165 /** Filter processing.
166 */
167
168 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
169 {
170         // Store the aspect ratio reported by the source
171         mlt_deque_push_back_double( MLT_FRAME_IMAGE_STACK( frame ), mlt_frame_get_aspect_ratio( frame ) );
172
173         // Push this on to the service stack
174         mlt_frame_push_service( frame, this );
175
176         // Push the get_image method on to the stack
177         mlt_frame_push_get_image( frame, filter_get_image );
178
179         return frame;
180 }
181
182 /** Constructor for the filter.
183 */
184
185 mlt_filter filter_resize_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
186 {
187         mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
188         if ( mlt_filter_init( this, this ) == 0 )
189         {
190                 this->process = filter_process;
191                 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "scale", arg == NULL ? "off" : arg );
192         }
193         return this;
194 }