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>
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.
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.
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
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
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
);
31 static void scale_alpha( mlt_frame
this, int iwidth
, int iheight
, int owidth
, int oheight
);
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
)
36 mlt_properties properties
= MLT_FRAME_PROPERTIES( this );
38 // Get the rescaling interpolsation
39 char *interps
= mlt_properties_get( properties
, "rescale.interp" );
41 // Carry out the rescaling
42 if ( iformat
== mlt_image_yuv422
&& oformat
== mlt_image_yuv422
)
45 mlt_frame_rescale_yuv422( this, owidth
, oheight
);
48 *image
= mlt_properties_get_data( properties
, "image", NULL
);
50 // Scale the alpha channel only if exists and not correct size
52 mlt_properties_get_data( properties
, "alpha", &alpha_size
);
53 if ( alpha_size
> 0 && alpha_size
!= ( owidth
* oheight
) )
54 scale_alpha( this, iwidth
, iheight
, owidth
, oheight
);
56 else if ( iformat
== mlt_image_rgb24
|| iformat
== mlt_image_rgb24a
)
58 int bpp
= (iformat
== mlt_image_rgb24a ?
4 : 3 );
60 // Create the yuv image
61 uint8_t *output
= mlt_pool_alloc( iwidth
* ( iheight
+ 1 ) * 2 );
63 if ( strcmp( interps
, "none" ) && ( iwidth
!= owidth
|| iheight
!= oheight
) )
65 // Extract YUV422 and alpha
68 // Allocate the alpha mask
69 uint8_t *alpha
= mlt_pool_alloc( iwidth
* ( iheight
+ 1 ) );
71 // Convert the image and extract alpha
72 mlt_convert_rgb24a_to_yuv422( *image
, iwidth
, iheight
, iwidth
* 4, output
, alpha
);
74 mlt_properties_set_data( properties
, "alpha", alpha
, iwidth
* ( iheight
+ 1 ), ( mlt_destructor
)mlt_pool_release
, NULL
);
76 scale_alpha( this, iwidth
, iheight
, owidth
, oheight
);
80 // No alpha to extract
81 mlt_convert_rgb24_to_yuv422( *image
, iwidth
, iheight
, iwidth
* 3, output
);
84 mlt_properties_set_data( properties
, "image", output
, iwidth
* ( iheight
+ 1 ) * 2, ( mlt_destructor
)mlt_pool_release
, NULL
);
87 output
= mlt_frame_rescale_yuv422( this, owidth
, oheight
);
92 // Extract YUV422 and alpha
95 // Allocate the alpha mask
96 uint8_t *alpha
= mlt_pool_alloc( owidth
* ( oheight
+ 1 ) );
98 // Convert the image and extract alpha
99 mlt_convert_rgb24a_to_yuv422( *image
, owidth
, oheight
, owidth
* 4, output
, alpha
);
101 mlt_properties_set_data( properties
, "alpha", alpha
, owidth
* ( oheight
+ 1 ), ( mlt_destructor
)mlt_pool_release
, NULL
);
103 scale_alpha( this, iwidth
, iheight
, owidth
, oheight
);
107 // No alpha to extract
108 mlt_convert_rgb24_to_yuv422( *image
, owidth
, oheight
, owidth
* 3, output
);
112 // Now update the frame
113 mlt_properties_set_data( properties
, "image", output
, owidth
* ( oheight
+ 1 ) * 2, ( mlt_destructor
)mlt_pool_release
, NULL
);
114 mlt_properties_set_int( properties
, "width", owidth
);
115 mlt_properties_set_int( properties
, "height", oheight
);
123 static void scale_alpha( mlt_frame
this, int iwidth
, int iheight
, int owidth
, int oheight
)
126 uint8_t *output
= NULL
;
127 uint8_t *input
= mlt_frame_get_alpha_mask( this );
133 int ox
= ( iwidth
<< 10 ) / owidth
;
134 int oy
= ( iheight
<< 10 ) / oheight
;
136 output
= mlt_pool_alloc( owidth
* oheight
);
139 // Loop for the entirety of our output height.
140 for ( y
= 0; y
< oheight
; y
++ )
141 for ( x
= 0; x
< owidth
; x
++ )
142 *out_line
++ = *( input
+ ( ( 512 + ( y
* oy
* iwidth
) + x
* ox
) >> 10 ) );
144 // Set it back on the frame
145 mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "alpha", output
, owidth
* oheight
, mlt_pool_release
, NULL
);
152 static int filter_get_image( mlt_frame
this, uint8_t **image
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
154 // Get the frame properties
155 mlt_properties properties
= MLT_FRAME_PROPERTIES( this );
157 // Get the filter from the stack
158 mlt_filter filter
= mlt_frame_pop_service( this );
160 // Get the filter properties
161 mlt_properties filter_properties
= MLT_FILTER_PROPERTIES( filter
);
163 // Get the image scaler method
164 image_scaler scaler_method
= mlt_properties_get_data( filter_properties
, "method", NULL
);
166 // Correct Width/height if necessary
167 if ( *width
== 0 || *height
== 0 )
169 *width
= mlt_properties_get_int( properties
, "normalised_width" );
170 *height
= mlt_properties_get_int( properties
, "normalised_height" );
173 // There can be problems with small images - avoid them (by hacking - gah)
174 if ( *width
>= 6 && *height
>= 6 )
177 int iheight
= *height
;
179 int oheight
= *height
;
180 char *interps
= mlt_properties_get( properties
, "rescale.interp" );
181 int wanted_format
= *format
;
183 // Default from the scaler if not specifed on the frame
184 if ( interps
== NULL
)
186 interps
= mlt_properties_get( MLT_FILTER_PROPERTIES( filter
), "interpolation" );
187 mlt_properties_set( properties
, "rescale.interp", interps
);
190 // If real_width/height exist, we want that as minimum information
191 if ( mlt_properties_get_int( properties
, "real_width" ) )
193 iwidth
= mlt_properties_get_int( properties
, "real_width" );
194 iheight
= mlt_properties_get_int( properties
, "real_height" );
197 // Let the producer know what we are actually requested to obtain
198 if ( *format
== mlt_image_yuv422
&& strcmp( interps
, "none" ) )
200 mlt_properties_set_int( properties
, "rescale_width", *width
);
201 mlt_properties_set_int( properties
, "rescale_height", *height
);
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
);
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 );
215 // Get the image as requested
216 mlt_frame_get_image( this, image
, format
, &iwidth
, &iheight
, writable
);
218 // Get rescale interpretation again, in case the producer wishes to override scaling
219 interps
= mlt_properties_get( properties
, "rescale.interp" );
221 if ( *image
!= NULL
&& ( *format
!= mlt_image_yuv422
|| ( iwidth
!= owidth
|| iheight
!= oheight
) ) )
223 // If the colour space is correct and scaling is off, do nothing
224 if ( *format
== mlt_image_yuv422
&& !strcmp( interps
, "none" ) )
229 else if ( *format
== mlt_image_yuv422
)
231 // Call the local scaler
232 scaler_method( this, image
, *format
, mlt_image_yuv422
, iwidth
, iheight
, owidth
, oheight
);
236 else if ( *format
== mlt_image_rgb24
&& wanted_format
== mlt_image_rgb24
)
238 // Call the local scaler
239 scaler_method( this, image
, *format
, mlt_image_rgb24
, iwidth
, iheight
, owidth
, oheight
);
245 else if ( *format
== mlt_image_rgb24
|| *format
== mlt_image_rgb24a
)
247 // Call the local scaler
248 scaler_method( this, image
, *format
, mlt_image_yuv422
, iwidth
, iheight
, owidth
, oheight
);
251 *format
= mlt_image_yuv422
;
269 // Store the requested width/height
271 int iheight
= *height
;
273 // Get the image as requested
274 mlt_frame_get_image( this, image
, format
, &iwidth
, &iheight
, writable
);
276 // Too small - for now just assign as though we got what we wanted
285 /** Filter processing.
288 static mlt_frame
filter_process( mlt_filter
this, mlt_frame frame
)
291 mlt_frame_push_service( frame
, this );
293 // Push the get image method
294 mlt_frame_push_service( frame
, filter_get_image
);
299 /** Constructor for the filter.
302 mlt_filter
filter_rescale_init( mlt_profile profile
, mlt_service_type type
, const char *id
, char *arg
)
304 // Create a new scaler
305 mlt_filter
this = mlt_filter_new( );
307 // If successful, then initialise it
310 // Get the properties
311 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
313 // Set the process method
314 this->process
= filter_process
;
316 // Set the inerpolation
317 mlt_properties_set( properties
, "interpolation", arg
== NULL ?
"bilinear" : arg
);
320 mlt_properties_set_data( properties
, "method", filter_scale
, 0, NULL
, NULL
);