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