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