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