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