]> git.sesse.net Git - mlt/blob - src/modules/core/filter_rescale.c
152c7624a75db16a480d9c8db2ae73b807ce8149
[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 #include <framework/mlt_log.h>
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <math.h>
29
30 /** virtual function declaration for an image scaler
31  *
32  * image scaler implementations are expected to support the following in and out formats:
33  * yuv422 -> yuv422
34  * rgb24 -> rgb24
35  * rgb24a -> rgb24a
36  * rgb24 -> yuv422
37  * rgb24a -> yuv422
38  */
39
40 typedef int ( *image_scaler )( mlt_frame this, uint8_t **image, mlt_image_format *format, int iwidth, int iheight, int owidth, int oheight );
41
42 static int filter_scale( mlt_frame this, uint8_t **image, mlt_image_format *format, int iwidth, int iheight, int owidth, int oheight )
43 {
44         // Get the properties
45         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
46
47         // Create the output image
48         uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
49
50         // Calculate strides
51         int istride = iwidth * 2;
52         int ostride = owidth * 2;
53         iwidth = iwidth - ( iwidth % 4 );
54
55         // Derived coordinates
56         int dy, dx;
57
58         // Calculate ranges
59         int out_x_range = owidth / 2;
60         int out_y_range = oheight / 2;
61         int in_x_range = iwidth / 2;
62         int in_y_range = iheight / 2;
63
64         // Output pointers
65         register uint8_t *out_line = output;
66         register uint8_t *out_ptr;
67
68         // Calculate a middle pointer
69         uint8_t *in_middle = *image + istride * in_y_range + in_x_range * 2;
70         uint8_t *in_line;
71
72         // Generate the affine transform scaling values
73         register int scale_width = ( iwidth << 16 ) / owidth;
74         register int scale_height = ( iheight << 16 ) / oheight;
75         register int base = 0;
76
77         int outer = out_x_range * scale_width;
78         int bottom = out_y_range * scale_height;
79
80         // Loop for the entirety of our output height.
81         for ( dy = - bottom; dy < bottom; dy += scale_height )
82         {
83                 // Start at the beginning of the line
84                 out_ptr = out_line;
85
86                 // Pointer to the middle of the input line
87                 in_line = in_middle + ( dy >> 16 ) * istride;
88
89                 // Loop for the entirety of our output row.
90                 for ( dx = - outer; dx < outer; dx += scale_width )
91                 {
92                         base = dx >> 15;
93                         base &= 0xfffffffe;
94                         *out_ptr ++ = *( in_line + base );
95                         base &= 0xfffffffc;
96                         *out_ptr ++ = *( in_line + base + 1 );
97                         dx += scale_width;
98                         base = dx >> 15;
99                         base &= 0xfffffffe;
100                         *out_ptr ++ = *( in_line + base );
101                         base &= 0xfffffffc;
102                         *out_ptr ++ = *( in_line + base + 3 );
103                 }
104                 // Move to next output line
105                 out_line += ostride;
106         }
107  
108         // Now update the frame
109         mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor ) mlt_pool_release, NULL );
110         mlt_properties_set_int( properties, "width", owidth );
111         mlt_properties_set_int( properties, "height", oheight );
112
113         *image = output;
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, *in_line;
126                 register int i, j, x, y;
127                 register int ox = ( iwidth << 16 ) / owidth;
128                 register int oy = ( iheight << 16 ) / 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 ( i = 0, y = (oy >> 1); i < oheight; i++, y += oy )
135                 {
136                         in_line = &input[ (y >> 16) * iwidth ];
137                         for ( j = 0, x = (ox >> 1); j < owidth; j++, x += ox )
138                                 *out_line ++ = in_line[ x >> 16 ];
139                 }
140
141                 // Set it back on the frame
142                 mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "alpha", output, owidth * oheight, mlt_pool_release, NULL );
143         }
144 }
145
146 /** Do it :-).
147 */
148
149 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
150 {
151         int error = 0;
152         
153         // Get the frame properties
154         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
155
156         // Get the filter from the stack
157         mlt_filter filter = mlt_frame_pop_service( this );
158
159         // Get the filter properties
160         mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
161
162         // Get the image scaler method
163         image_scaler scaler_method = mlt_properties_get_data( filter_properties, "method", NULL );
164
165         // Correct Width/height if necessary
166         if ( *width == 0 || *height == 0 )
167         {
168                 *width = mlt_properties_get_int( properties, "normalised_width" );
169                 *height = mlt_properties_get_int( properties, "normalised_height" );
170         }
171
172         // There can be problems with small images - avoid them (by hacking - gah)
173         if ( *width >= 6 && *height >= 6 )
174         {
175                 int iwidth = *width;
176                 int iheight = *height;
177                 double factor = mlt_properties_get_double( filter_properties, "factor" );
178                 factor = factor > 0 ? factor : 1.0;
179                 int owidth = *width * factor;
180                 int oheight = *height * factor;
181                 char *interps = mlt_properties_get( properties, "rescale.interp" );
182
183                 // Default from the scaler if not specifed on the frame
184                 if ( interps == NULL )
185                 {
186                         interps = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "interpolation" );
187                         mlt_properties_set( properties, "rescale.interp", interps );
188                 }
189         
190                 // If real_width/height exist, we want that as minimum information
191                 if ( mlt_properties_get_int( properties, "real_width" ) )
192                 {
193                         iwidth = mlt_properties_get_int( properties, "real_width" );
194                         iheight = mlt_properties_get_int( properties, "real_height" );
195                 }
196         
197                 // Let the producer know what we are actually requested to obtain
198                 if ( strcmp( interps, "none" ) )
199                 {
200                         mlt_properties_set_int( properties, "rescale_width", *width );
201                         mlt_properties_set_int( properties, "rescale_height", *height );
202                 }
203                 else
204                 {
205                         // When no scaling is requested, revert the requested dimensions if possible
206                         mlt_properties_set_int( properties, "rescale_width", iwidth );
207                         mlt_properties_set_int( properties, "rescale_height", iheight );
208                 }
209
210                 // Deinterlace if height is changing to prevent fields mixing on interpolation
211                 // One exception: non-interpolated, integral scaling
212                 if ( iheight != oheight && ( strcmp( interps, "nearest" ) || ( iheight % oheight != 0 ) ) )
213                         mlt_properties_set_int( properties, "consumer_deinterlace", 1 );
214
215                 // Convert the image to yuv422 when using the local scaler
216                 if ( scaler_method == filter_scale )
217                         *format = mlt_image_yuv422;
218
219                 // Get the image as requested
220                 mlt_frame_get_image( this, image, format, &iwidth, &iheight, writable );
221
222                 // Get rescale interpretation again, in case the producer wishes to override scaling
223                 interps = mlt_properties_get( properties, "rescale.interp" );
224         
225                 if ( *image && strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
226                 {
227                         mlt_log_debug( MLT_FILTER_SERVICE( filter ), "%dx%d -> %dx%d (%s) %s\n",
228                                 iwidth, iheight, owidth, oheight, mlt_image_format_name( *format ), interps );
229
230                         // If valid colorspace
231                         if ( *format == mlt_image_yuv422 || *format == mlt_image_rgb24 ||
232                              *format == mlt_image_rgb24a || *format == mlt_image_opengl )
233                         {
234                                 // Call the virtual function
235                                 scaler_method( this, image, format, iwidth, iheight, owidth, oheight );
236                                 *width = owidth;
237                                 *height = oheight;
238                         }
239                         // Scale the alpha channel only if exists and not correct size
240                         int alpha_size = 0;
241                         mlt_properties_get_data( properties, "alpha", &alpha_size );
242                         if ( alpha_size > 0 && alpha_size != ( owidth * oheight ) && alpha_size != ( owidth * ( oheight + 1 ) ) )
243                                 scale_alpha( this, iwidth, iheight, owidth, oheight );
244                 }
245                 else
246                 {
247                         *width = iwidth;
248                         *height = iheight;
249                 }
250         }
251         else
252         {
253                 error = 1;
254         }
255
256         return error;
257 }
258
259 /** Filter processing.
260 */
261
262 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
263 {
264         // Push the filter
265         mlt_frame_push_service( frame, this );
266
267         // Push the get image method
268         mlt_frame_push_service( frame, filter_get_image );
269
270         return frame;
271 }
272
273 /** Constructor for the filter.
274 */
275
276 mlt_filter filter_rescale_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
277 {
278         // Create a new scaler
279         mlt_filter this = mlt_filter_new( );
280
281         // If successful, then initialise it
282         if ( this != NULL )
283         {
284                 // Get the properties
285                 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
286
287                 // Set the process method
288                 this->process = filter_process;
289
290                 // Set the inerpolation
291                 mlt_properties_set( properties, "interpolation", arg == NULL ? "bilinear" : arg );
292
293                 // Set the method
294                 mlt_properties_set_data( properties, "method", filter_scale, 0, NULL, NULL );
295         }
296
297         return this;
298 }
299