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