]> git.sesse.net Git - mlt/blob - src/modules/core/filter_rescale.c
0305db4174026f9d14811fccd1cd26dab1170b37
[mlt] / src / modules / core / filter_rescale.c
1 /*
2  * filter_rescale.c -- scale the producer video frame size to match the consumer
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "filter_rescale.h"
22
23 #include <framework/mlt_frame.h>
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28
29 typedef int ( *image_scaler )( mlt_frame this, uint8_t **image, mlt_image_format iformat, mlt_image_format oformat, int iwidth, int iheight, int owidth, int oheight );
30
31 static int filter_scale( mlt_frame this, uint8_t **image, mlt_image_format iformat, mlt_image_format oformat, int iwidth, int iheight, int owidth, int oheight )
32 {
33         // Get the properties
34         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
35
36         // Get the rescaling interpolsation
37         char *interps = mlt_properties_get( properties, "rescale.interp" );
38
39         // Carry out the rescaling
40         if ( iformat == mlt_image_yuv422 && oformat == mlt_image_yuv422 )
41         {
42                 // Scale the frame
43                 mlt_frame_rescale_yuv422( this, owidth, oheight );
44                 
45                 // Return the output
46                 *image = mlt_properties_get_data( properties, "image", NULL );
47         }
48         else if ( iformat == mlt_image_rgb24 || iformat == mlt_image_rgb24a )
49         {
50                 int bpp = (iformat == mlt_image_rgb24a ? 4 : 3 );
51                         
52                 // Create the yuv image
53                 uint8_t *output = mlt_pool_alloc( iwidth * ( iheight + 1 ) * 2 );
54
55                 if ( strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
56                 {
57                         // Extract YUV422 and alpha
58                         if ( bpp == 4 )
59                         {
60                                 // Allocate the alpha mask
61                                 uint8_t *alpha = mlt_pool_alloc( iwidth * ( iheight + 1 ) );
62
63                                 // Convert the image and extract alpha
64                                 mlt_convert_rgb24a_to_yuv422( *image, iwidth, iheight, iwidth * 2, output, alpha );
65
66                                 mlt_properties_set_data( properties, "alpha", alpha, iwidth * ( iheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
67                         }
68                         else
69                         {
70                                 // No alpha to extract
71                                 mlt_convert_rgb24_to_yuv422( *image, iwidth, iheight, iwidth * 2, output );
72                         }
73
74                         // Scale the frame
75                         mlt_frame_rescale_yuv422( this, owidth, oheight );
76                 
77                         // Return the output
78                         *image = mlt_properties_get_data( properties, "image", NULL );
79                 }
80                 else
81                 {
82                         // Extract YUV422 and alpha
83                         if ( bpp == 4 )
84                         {
85                                 // Allocate the alpha mask
86                                 uint8_t *alpha = mlt_pool_alloc( owidth * ( oheight + 1 ) );
87
88                                 // Convert the image and extract alpha
89                                 mlt_convert_rgb24a_to_yuv422( *image, owidth, oheight, owidth * 4, output, alpha );
90
91                                 mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
92                         }
93                         else
94                         {
95                                 // No alpha to extract
96                                 mlt_convert_rgb24_to_yuv422( *image, owidth, oheight, owidth * 3, output );
97                         }
98                 }
99
100                 // Now update the frame
101                 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
102                 mlt_properties_set_int( properties, "width", owidth );
103                 mlt_properties_set_int( properties, "height", oheight );
104
105                 *image = output;
106         }
107
108         return 0;
109 }
110
111 static void scale_alpha( mlt_frame this, int iwidth, int iheight, int owidth, int oheight )
112 {
113         int size = 0;
114         uint8_t *input = mlt_properties_get_data( MLT_FRAME_PROPERTIES( this ), "alpha", &size );
115         
116         if ( input != NULL && ( ( size == iwidth * iheight ) || size == ( iwidth * ( iheight + 1 ) ) ) )
117         {
118                 uint8_t *output = mlt_pool_alloc( owidth * oheight );
119
120                 // Derived coordinates
121                 int dy, dx;
122
123         // Calculate ranges
124         int out_x_range = owidth / 2;
125         int out_y_range = oheight / 2;
126         int in_x_range = iwidth / 2;
127         int in_y_range = iheight / 2;
128
129         // Output pointers
130         register uint8_t *out_line = output;
131         register uint8_t *out_ptr;
132
133         // Calculate a middle pointer
134         uint8_t *in_middle = input + iwidth * in_y_range + in_x_range;
135         uint8_t *in_line;
136
137                 // Generate the affine transform scaling values
138                 register int scale_width = ( iwidth << 16 ) / owidth;
139                 register int scale_height = ( iheight << 16 ) / oheight;
140                 register int base = 0;
141
142                 int outer = out_x_range * scale_width;
143                 int bottom = out_y_range * scale_height;
144
145         // Loop for the entirety of our output height.
146         for ( dy = - bottom; dy < bottom; dy += scale_height )
147         {
148                 // Start at the beginning of the line
149                 out_ptr = out_line;
150         
151                 // Pointer to the middle of the input line
152                 in_line = in_middle + ( dy >> 16 ) * iwidth;
153
154                 // Loop for the entirety of our output row.
155                 for ( dx = - outer; dx < outer; dx += scale_width )
156                 {
157                                 base = dx >> 15;
158                                 *out_ptr ++ = *( in_line + base );
159                 }
160
161                 // Move to next output line
162                 out_line += owidth;
163         }
164
165                 this->get_alpha_mask = NULL;
166                 mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "alpha", output, 0, mlt_pool_release, NULL );
167         }
168 }
169
170 /** Do it :-).
171 */
172
173 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
174 {
175         // Get the frame properties
176         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
177
178         // Get the filter from the stack
179         mlt_filter filter = mlt_frame_pop_service( this );
180
181         // Get the filter properties
182         mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
183
184         // Get the image scaler method
185         image_scaler scaler_method = mlt_properties_get_data( filter_properties, "method", NULL );
186
187         // Correct Width/height if necessary
188         if ( *width == 0 || *height == 0 )
189         {
190                 *width = mlt_properties_get_int( properties, "normalised_width" );
191                 *height = mlt_properties_get_int( properties, "normalised_height" );
192         }
193
194         // There can be problems with small images - avoid them (by hacking - gah)
195         if ( *width >= 6 && *height >= 6 )
196         {
197                 int iwidth = *width;
198                 int iheight = *height;
199                 int owidth = *width;
200                 int oheight = *height;
201                 char *interps = mlt_properties_get( properties, "rescale.interp" );
202                 int wanted_format = *format;
203
204                 // Default from the scaler if not specifed on the frame
205                 if ( interps == NULL )
206                 {
207                         interps = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "interpolation" );
208                         mlt_properties_set( properties, "rescale.interp", interps );
209                 }
210         
211                 // If real_width/height exist, we want that as minimum information
212                 if ( mlt_properties_get_int( properties, "real_width" ) )
213                 {
214                         iwidth = mlt_properties_get_int( properties, "real_width" );
215                         iheight = mlt_properties_get_int( properties, "real_height" );
216                 }
217         
218                 // Let the producer know what we are actually requested to obtain
219                 if ( *format == mlt_image_yuv422 && strcmp( interps, "none" ) )
220                 {
221                         mlt_properties_set_int( properties, "rescale_width", *width );
222                         mlt_properties_set_int( properties, "rescale_height", *height );
223                 }
224                 else
225                 {
226                         // When no scaling is requested, revert the requested dimensions if possible
227                         mlt_properties_set_int( properties, "rescale_width", ( iwidth / 2 ) * 2 );
228                         mlt_properties_set_int( properties, "rescale_height", iheight );
229                 }
230         
231                 // Get the image as requested
232                 mlt_frame_get_image( this, image, format, &iwidth, &iheight, writable );
233
234                 // Get rescale interpretation again, in case the producer wishes to override scaling
235                 interps = mlt_properties_get( properties, "rescale.interp" );
236         
237                 if ( *image != NULL && ( *format != mlt_image_yuv422 || ( iwidth != owidth || iheight != oheight ) ) )
238                 {
239                         // If the colour space is correct and scaling is off, do nothing
240                         if ( *format == mlt_image_yuv422 && !strcmp( interps, "none" ) )
241                         {
242                                 *width = iwidth;
243                                 *height = iheight;
244                         }
245                         else if ( *format == mlt_image_yuv422 )
246                         {
247                                 // Call the local scaler
248                                 scaler_method( this, image, *format, mlt_image_yuv422, iwidth, iheight, owidth, oheight );
249                                 *width = owidth;
250                                 *height = oheight;
251
252                                 // Scale the alpha
253                                 scale_alpha( this, iwidth, iheight, owidth, oheight );
254                         }
255                         else if ( *format == mlt_image_rgb24 && wanted_format == mlt_image_rgb24 )
256                         {
257                                 // Call the local scaler
258                                 scaler_method( this, image, *format, mlt_image_rgb24, iwidth, iheight, owidth, oheight );
259
260                                 // Return the output
261                                 *width = owidth;
262                                 *height = oheight;
263
264                                 // Scale the alpha
265                                 scale_alpha( this, iwidth, iheight, owidth, oheight );
266                         }
267                         else if ( *format == mlt_image_rgb24 || *format == mlt_image_rgb24a )
268                         {
269                                 // Call the local scaler
270                                 scaler_method( this, image, *format, mlt_image_yuv422, iwidth, iheight, owidth, oheight );
271
272                                 // Return the output
273                                 *format = mlt_image_yuv422;
274                                 *width = owidth;
275                                 *height = oheight;
276
277                                 // Scale the alpha
278                                 scale_alpha( this, iwidth, iheight, owidth, oheight );
279                         }
280                         else
281                         {
282                                 *width = iwidth;
283                                 *height = iheight;
284                         }
285                 }
286                 else
287                 {
288                         *width = iwidth;
289                         *height = iheight;
290                 }
291         }
292         else
293         {
294                 // Store the requested width/height
295                 int iwidth = *width;
296                 int iheight = *height;
297
298                 // Get the image as requested
299                 mlt_frame_get_image( this, image, format, &iwidth, &iheight, writable );
300
301                 // Too small - for now just assign as though we got what we wanted
302                 *width = iwidth;
303                 *height = iheight;
304         }
305
306
307         return 0;
308 }
309
310 /** Filter processing.
311 */
312
313 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
314 {
315         // Push the filter
316         mlt_frame_push_service( frame, this );
317
318         // Push the get image method
319         mlt_frame_push_service( frame, filter_get_image );
320
321         return frame;
322 }
323
324 /** Constructor for the filter.
325 */
326
327 mlt_filter filter_rescale_init( char *arg )
328 {
329         // Create a new scaler
330         mlt_filter this = mlt_filter_new( );
331
332         // If successful, then initialise it
333         if ( this != NULL )
334         {
335                 // Get the properties
336                 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
337
338                 // Set the process method
339                 this->process = filter_process;
340
341                 // Set the inerpolation
342                 mlt_properties_set( properties, "interpolation", arg == NULL ? "bilinear" : arg );
343
344                 // Set the method
345                 mlt_properties_set_data( properties, "method", filter_scale, 0, NULL, NULL );
346         }
347
348         return this;
349 }
350