]> git.sesse.net Git - mlt/blob - src/modules/core/filter_resize.c
fbbac56dc00520fd765b926dfc5b19ff5d627a6e
[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 #include <framework/mlt_profile.h>
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <math.h>
29
30 static uint8_t *resize_alpha( uint8_t *input, int owidth, int oheight, int iwidth, int iheight, uint8_t alpha_value )
31 {
32         uint8_t *output = NULL;
33
34         if ( input != NULL && ( iwidth != owidth || iheight != oheight ) && ( owidth > 6 && oheight > 6 ) )
35         {
36                 uint8_t *out_line;
37                 int offset_x = ( owidth - iwidth ) / 2;
38                 int offset_y = ( oheight - iheight ) / 2;
39                 int iused = iwidth;
40
41                 output = mlt_pool_alloc( owidth * oheight );
42                 memset( output, alpha_value, owidth * oheight );
43
44                 offset_x -= offset_x % 2;
45
46                 out_line = output + offset_y * owidth;
47                 out_line += offset_x;
48
49                 // Loop for the entirety of our output height.
50                 while ( iheight -- )
51                 {
52                         // We're in the input range for this row.
53                         memcpy( out_line, input, iused );
54
55                         // Move to next input line
56                         input += iwidth;
57
58                         // Move to next output line
59                         out_line += owidth;
60                 }
61         }
62
63         return output;
64 }
65
66 static void resize_image( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight, int bpp )
67 {
68         // Calculate strides
69         int istride = iwidth * bpp;
70         int ostride = owidth * bpp;
71         int offset_x = ( owidth - iwidth ) / 2 * bpp;
72         int offset_y = ( oheight - iheight ) / 2;
73         uint8_t *in_line = input;
74         uint8_t *out_line;
75         int size = owidth * oheight;
76         uint8_t *p = output;
77
78         // Optimisation point
79         if ( output == NULL || input == NULL || ( owidth <= 6 || oheight <= 6 || iwidth <= 6 || oheight <= 6 ) )
80         {
81                 return;
82         }
83         else if ( iwidth == owidth && iheight == oheight )
84         {
85                 memcpy( output, input, iheight * istride );
86                 return;
87         }
88
89         if ( bpp == 2 )
90         {
91                 while( size -- )
92                 {
93                         *p ++ = 16;
94                         *p ++ = 128;
95                 }
96                 offset_x -= offset_x % 4;
97         }
98         else
99         {
100                 size *= bpp;
101                 while ( size-- )
102                         *p ++ = 0;
103         }
104
105         out_line = output + offset_y * ostride;
106         out_line += offset_x;
107
108         // Loop for the entirety of our output height.
109         while ( iheight -- )
110         {
111                 // We're in the input range for this row.
112                 memcpy( out_line, in_line, istride );
113
114                 // Move to next input line
115                 in_line += istride;
116
117                 // Move to next output line
118                 out_line += ostride;
119         }
120 }
121
122 /** A padding function for frames - this does not rescale, but simply
123         resizes.
124 */
125
126 static uint8_t *frame_resize_image( mlt_frame frame, int owidth, int oheight, int bpp )
127 {
128         // Get properties
129         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
130
131         // Get the input image, width and height
132         uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
133         uint8_t *alpha = mlt_frame_get_alpha_mask( frame );
134         int alpha_size = 0;
135         mlt_properties_get_data( properties, "alpha", &alpha_size );
136
137         int iwidth = mlt_properties_get_int( properties, "width" );
138         int iheight = mlt_properties_get_int( properties, "height" );
139
140         // If width and height are correct, don't do anything
141         if ( iwidth < owidth || iheight < oheight )
142         {
143                 uint8_t alpha_value = mlt_properties_get_int( properties, "resize_alpha" );
144
145                 // Create the output image
146                 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * bpp );
147
148                 // Call the generic resize
149                 resize_image( output, owidth, oheight, input, iwidth, iheight, bpp );
150
151                 // Now update the frame
152                 mlt_frame_set_image( frame, output, owidth * ( oheight + 1 ) * bpp, mlt_pool_release );
153
154                 // We should resize the alpha too
155                 if ( alpha && alpha_size >= iwidth * iheight )
156                 {
157                         alpha = resize_alpha( alpha, owidth, oheight, iwidth, iheight, alpha_value );
158                         if ( alpha )
159                                 mlt_frame_set_alpha( frame, alpha, owidth * oheight, mlt_pool_release );
160                 }
161
162                 // Return the output
163                 return output;
164         }
165         // No change, return input
166         return input;
167 }
168
169 /** Do it :-).
170 */
171
172 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
173 {
174         int error = 0;
175
176         // Get the properties from the frame
177         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
178
179         // Pop the top of stack now
180         mlt_filter filter = mlt_frame_pop_service( frame );
181
182         // Retrieve the aspect ratio
183         double aspect_ratio = mlt_deque_pop_back_double( MLT_FRAME_IMAGE_STACK( frame ) );
184         double consumer_aspect = mlt_profile_sar( mlt_service_profile( MLT_FILTER_SERVICE( filter ) ) );
185
186         // Correct Width/height if necessary
187         if ( *width == 0 || *height == 0 )
188         {
189                 *width = mlt_properties_get_int( properties, "normalised_width" );
190                 *height = mlt_properties_get_int( properties, "normalised_height" );
191         }
192
193         // Assign requested width/height from our subordinate
194         int owidth = *width;
195         int oheight = *height;
196
197         // Check for the special case - no aspect ratio means no problem :-)
198         if ( aspect_ratio == 0.0 )
199                 aspect_ratio = consumer_aspect;
200
201         // Reset the aspect ratio
202         mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
203
204         // Hmmm...
205         char *rescale = mlt_properties_get( properties, "rescale.interp" );
206         if ( rescale != NULL && !strcmp( rescale, "none" ) )
207                 return mlt_frame_get_image( frame, image, format, width, height, writable );
208
209         if ( mlt_properties_get_int( properties, "distort" ) == 0 )
210         {
211                 // Normalise the input and out display aspect
212                 int normalised_width = mlt_properties_get_int( properties, "normalised_width" );
213                 int normalised_height = mlt_properties_get_int( properties, "normalised_height" );
214                 int real_width = mlt_properties_get_int( properties, "real_width" );
215                 int real_height = mlt_properties_get_int( properties, "real_height" );
216                 if ( real_width == 0 )
217                         real_width = mlt_properties_get_int( properties, "width" );
218                 if ( real_height == 0 )
219                         real_height = mlt_properties_get_int( properties, "height" );
220                 double input_ar = aspect_ratio * real_width / real_height;
221                 double output_ar = consumer_aspect * owidth / oheight;
222                 
223 //              fprintf( stderr, "real %dx%d normalised %dx%d output %dx%d sar %f in-dar %f out-dar %f\n",
224 //              real_width, real_height, normalised_width, normalised_height, owidth, oheight, aspect_ratio, input_ar, output_ar);
225
226                 // Optimised for the input_ar > output_ar case (e.g. widescreen on standard)
227                 int scaled_width = rint( ( input_ar * normalised_width ) / output_ar );
228                 int scaled_height = normalised_height;
229
230                 // Now ensure that our images fit in the output frame
231                 if ( scaled_width > normalised_width )
232                 {
233                         scaled_width = normalised_width;
234                         scaled_height = rint( ( output_ar * normalised_height ) / input_ar );
235                 }
236
237                 // Now calculate the actual image size that we want
238                 owidth = rint( scaled_width * owidth / normalised_width );
239                 oheight = rint( scaled_height * oheight / normalised_height );
240
241                 // Tell frame we have conformed the aspect to the consumer
242                 mlt_frame_set_aspect_ratio( frame, consumer_aspect );
243         }
244
245         mlt_properties_set_int( properties, "distort", 0 );
246
247         // Now pass on the calculations down the line
248         mlt_properties_set_int( properties, "resize_width", *width );
249         mlt_properties_set_int( properties, "resize_height", *height );
250
251         // Now get the image
252         if ( *format == mlt_image_yuv422 )
253                 owidth -= owidth % 2;
254         error = mlt_frame_get_image( frame, image, format, &owidth, &oheight, writable );
255
256         if ( error == 0 && *image )
257         {
258                 int bpp;
259                 mlt_image_format_size( *format, owidth, oheight, &bpp );
260                 *image = frame_resize_image( frame, *width, *height, bpp );
261         }
262
263         return error;
264 }
265
266 /** Filter processing.
267 */
268
269 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
270 {
271         // Store the aspect ratio reported by the source
272         mlt_deque_push_back_double( MLT_FRAME_IMAGE_STACK( frame ), mlt_frame_get_aspect_ratio( frame ) );
273
274         // Push this on to the service stack
275         mlt_frame_push_service( frame, filter );
276
277         // Push the get_image method on to the stack
278         mlt_frame_push_get_image( frame, filter_get_image );
279
280         return frame;
281 }
282
283 /** Constructor for the filter.
284 */
285
286 mlt_filter filter_resize_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
287 {
288         mlt_filter filter = calloc( sizeof( struct mlt_filter_s ), 1 );
289         if ( mlt_filter_init( filter, filter ) == 0 )
290         {
291                 filter->process = filter_process;
292         }
293         return filter;
294 }