1 /*
2 * filter_rescale.c -- scale the producer video frame size to match the consumer
3 * Copyright (C) 2003-2014 Meltytech, LLC
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <framework/mlt_filter.h>
21 #include <framework/mlt_frame.h>
22 #include <framework/mlt_log.h>
23 #include <framework/mlt_profile.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 frame, uint8_t **image, mlt_image_format *format, int iwidth, int iheight, int owidth, int oheight );
41
filter_scale(mlt_frame frame,uint8_t ** image,mlt_image_format * format,int iwidth,int iheight,int owidth,int oheight)42 static int filter_scale( mlt_frame frame, uint8_t **image, mlt_image_format *format, int iwidth, int iheight, int owidth, int oheight )
43 {
44 // Create the output image
45 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
46
47 // Calculate strides
48 int istride = iwidth * 2;
49 int ostride = owidth * 2;
50 iwidth = iwidth - ( iwidth % 4 );
51
52 // Derived coordinates
53 int dy, dx;
54
55 // Calculate ranges
56 int out_x_range = owidth / 2;
57 int out_y_range = oheight / 2;
58 int in_x_range = iwidth / 2;
59 int in_y_range = iheight / 2;
60
61 // Output pointers
62 register uint8_t *out_line = output;
63 register uint8_t *out_ptr;
64
65 // Calculate a middle pointer
66 uint8_t *in_middle = *image + istride * in_y_range + in_x_range * 2;
67 uint8_t *in_line;
68
69 // Generate the affine transform scaling values
70 register int scale_width = ( iwidth << 16 ) / owidth;
71 register int scale_height = ( iheight << 16 ) / oheight;
72 register int base = 0;
73
74 int outer = out_x_range * scale_width;
75 int bottom = out_y_range * scale_height;
76
77 // Loop for the entirety of our output height.
78 for ( dy = - bottom; dy < bottom; dy += scale_height )
79 {
80 // Start at the beginning of the line
81 out_ptr = out_line;
82
83 // Pointer to the middle of the input line
84 in_line = in_middle + ( dy >> 16 ) * istride;
85
86 // Loop for the entirety of our output row.
87 for ( dx = - outer; dx < outer; dx += scale_width )
88 {
89 base = dx >> 15;
90 base &= 0xfffffffe;
91 *out_ptr ++ = *( in_line + base );
92 base &= 0xfffffffc;
93 *out_ptr ++ = *( in_line + base + 1 );
94 dx += scale_width;
95 base = dx >> 15;
96 base &= 0xfffffffe;
97 *out_ptr ++ = *( in_line + base );
98 base &= 0xfffffffc;
99 *out_ptr ++ = *( in_line + base + 3 );
100 }
101 // Move to next output line
102 out_line += ostride;
103 }
104
105 // Now update the frame
106 mlt_frame_set_image( frame, output, owidth * ( oheight + 1 ) * 2, mlt_pool_release );
107 *image = output;
108
109 return 0;
110 }
111
scale_alpha(mlt_frame frame,int iwidth,int iheight,int owidth,int oheight)112 static void scale_alpha( mlt_frame frame, int iwidth, int iheight, int owidth, int oheight )
113 {
114 // Scale the alpha
115 uint8_t *output = NULL;
116 uint8_t *input = mlt_frame_get_alpha( frame );
117
118 if ( input != NULL )
119 {
120 uint8_t *out_line, *in_line;
121 register int i, j, x, y;
122 register int ox = ( iwidth << 16 ) / owidth;
123 register int oy = ( iheight << 16 ) / oheight;
124
125 output = mlt_pool_alloc( owidth * oheight );
126 out_line = output;
127
128 // Loop for the entirety of our output height.
129 for ( i = 0, y = (oy >> 1); i < oheight; i++, y += oy )
130 {
131 in_line = &input[ (y >> 16) * iwidth ];
132 for ( j = 0, x = (ox >> 1); j < owidth; j++, x += ox )
133 *out_line ++ = in_line[ x >> 16 ];
134 }
135
136 // Set it back on the frame
137 mlt_frame_set_alpha( frame, output, owidth * oheight, mlt_pool_release );
138 }
139 }
140
141 /** Do it :-).
142 */
143
filter_get_image(mlt_frame frame,uint8_t ** image,mlt_image_format * format,int * width,int * height,int writable)144 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
145 {
146 int error = 0;
147
148 // Get the frame properties
149 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
150
151 // Get the filter from the stack
152 mlt_filter filter = mlt_frame_pop_service( frame );
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 mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( filter ) );
164 *width = profile->width;
165 *height = profile->height;
166 }
167
168 // There can be problems with small images - avoid them (by hacking - gah)
169 if ( *width >= 6 && *height >= 6 )
170 {
171 int iwidth = *width;
172 int iheight = *height;
173 int owidth = *width;
174 int oheight = *height;
175 char *interps = mlt_properties_get( properties, "rescale.interp" );
176
177 if ( mlt_properties_get( filter_properties, "factor" ) )
178 {
179 double factor = mlt_properties_get_double( filter_properties, "factor" );
180 owidth *= factor;
181 oheight *= factor;
182 }
183
184 // Default from the scaler if not specified 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 meta.media.width/height exist, we want that as minimum information
192 if ( mlt_properties_get_int( properties, "meta.media.width" ) )
193 {
194 iwidth = mlt_properties_get_int( properties, "meta.media.width" );
195 iheight = mlt_properties_get_int( properties, "meta.media.height" );
196 }
197
198 // Let the producer know what we are actually requested to obtain
199 if ( 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 // Deinterlace if height is changing to prevent fields mixing on interpolation
212 // One exception: non-interpolated, integral scaling
213 if ( iheight != oheight && ( strcmp( interps, "nearest" ) || ( iheight % oheight != 0 ) ) )
214 mlt_properties_set_int( properties, "consumer_deinterlace", 1 );
215
216 // Convert the image to yuv422 when using the local scaler
217 if ( scaler_method == filter_scale )
218 *format = mlt_image_yuv422;
219
220 // Get the image as requested
221 mlt_frame_get_image( frame, image, format, &iwidth, &iheight, writable );
222
223 // Get rescale interpretation again, in case the producer wishes to override scaling
224 interps = mlt_properties_get( properties, "rescale.interp" );
225
226 if ( *image && strcmp( interps, "none" ) && ( iwidth != owidth || iheight != oheight ) )
227 {
228 mlt_log_debug( MLT_FILTER_SERVICE( filter ), "%dx%d -> %dx%d (%s) %s\n",
229 iwidth, iheight, owidth, oheight, mlt_image_format_name( *format ), interps );
230
231 // If valid colorspace
232 if ( *format == mlt_image_yuv422 || *format == mlt_image_rgb24 ||
233 *format == mlt_image_rgb24a || *format == mlt_image_opengl )
234 {
235 // Call the virtual function
236 scaler_method( frame, image, format, iwidth, iheight, owidth, oheight );
237 *width = owidth;
238 *height = oheight;
239 }
240 // Scale the alpha channel only if exists and not correct size
241 int alpha_size = 0;
242 mlt_properties_get_data( properties, "alpha", &alpha_size );
243 if ( alpha_size > 0 && alpha_size != ( owidth * oheight ) && alpha_size != ( owidth * ( oheight + 1 ) ) )
244 scale_alpha( frame, iwidth, iheight, owidth, oheight );
245 }
246 else
247 {
248 *width = iwidth;
249 *height = iheight;
250 }
251 }
252 else
253 {
254 error = 1;
255 }
256
257 return error;
258 }
259
260 /** Filter processing.
261 */
262
filter_process(mlt_filter filter,mlt_frame frame)263 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
264 {
265 // Push the filter
266 mlt_frame_push_service( frame, filter );
267
268 // Push the get image method
269 mlt_frame_push_service( frame, filter_get_image );
270
271 return frame;
272 }
273
274 /** Constructor for the filter.
275 */
276
filter_rescale_init(mlt_profile profile,mlt_service_type type,const char * id,char * arg)277 mlt_filter filter_rescale_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
278 {
279 // Create a new scaler
280 mlt_filter filter = mlt_filter_new( );
281
282 // If successful, then initialise it
283 if ( filter != NULL )
284 {
285 // Get the properties
286 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
287
288 // Set the process method
289 filter->process = filter_process;
290
291 // Set the inerpolation
292 mlt_properties_set( properties, "interpolation", arg == NULL ? "bilinear" : arg );
293
294 // Set the method
295 mlt_properties_set_data( properties, "method", filter_scale, 0, NULL, NULL );
296 }
297
298 return filter;
299 }
300
301