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