2 * filter_swscale.c -- image scaling filter
3 * Copyright (C) 2008-2009 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>
23 #include <framework/mlt_factory.h>
24 #include <framework/mlt_factory.h>
27 // ffmpeg Header files
35 static inline int is_big_endian( )
37 union
{ int i
; char c
[ 4 ]; } big_endian_test
;
38 big_endian_test
.i
= 1;
40 return big_endian_test
.c
[ 0 ] != 1;
43 static inline int convert_mlt_to_av_cs( mlt_image_format format
)
50 value
= PIX_FMT_RGB24
;
52 case mlt_image_rgb24a
:
53 value
= PIX_FMT_RGBA32
;
55 case mlt_image_yuv422
:
56 value
= PIX_FMT_YUV422
;
58 case mlt_image_yuv420p
:
59 value
= PIX_FMT_YUV420P
;
61 case mlt_image_opengl
:
63 fprintf( stderr
, "Invalid format...\n" );
70 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
)
73 mlt_properties properties
= MLT_FRAME_PROPERTIES( this );
75 // Get the requested interpolation method
76 char *interps
= mlt_properties_get( properties
, "rescale.interp" );
78 // Convert to the SwScale flag
79 int interp
= SWS_BILINEAR
;
80 if ( strcmp( interps
, "nearest" ) == 0 || strcmp( interps
, "neighbor" ) == 0 )
82 else if ( strcmp( interps
, "tiles" ) == 0 || strcmp( interps
, "fast_bilinear" ) == 0 )
83 interp
= SWS_FAST_BILINEAR
;
84 else if ( strcmp( interps
, "bilinear" ) == 0 )
85 interp
= SWS_BILINEAR
;
86 else if ( strcmp( interps
, "bicubic" ) == 0 )
88 else if ( strcmp( interps
, "bicublin" ) == 0 )
89 interp
= SWS_BICUBLIN
;
90 else if ( strcmp( interps
, "gauss" ) == 0 )
92 else if ( strcmp( interps
, "sinc" ) == 0 )
94 else if ( strcmp( interps
, "hyper" ) == 0 || strcmp( interps
, "lanczos" ) == 0 )
96 else if ( strcmp( interps
, "spline" ) == 0 )
99 // Convert the pixel formats
100 iformat
= convert_mlt_to_av_cs( iformat
);
101 oformat
= convert_mlt_to_av_cs( oformat
);
103 // Create the context and output image
104 struct SwsContext
*context
= sws_getContext( iwidth
, iheight
, iformat
, owidth
, oheight
, oformat
, interp
, NULL
, NULL
, NULL
);
106 avpicture_fill( &input
, *image
, iformat
, iwidth
, iheight
);
108 uint8_t *outbuf
= mlt_pool_alloc( owidth
* ( oheight
+ 1 ) * 2 );
109 avpicture_fill( &output
, outbuf
, oformat
, owidth
, oheight
);
111 // Perform the scaling
112 sws_scale( context
, input
.data
, input
.linesize
, 0, iheight
, output
.data
, output
.linesize
);
113 sws_freeContext( context
);
115 // Now update the frame
116 mlt_properties_set_data( properties
, "image", outbuf
, owidth
* ( oheight
+ 1 ) * 2, ( mlt_destructor
)mlt_pool_release
, NULL
);
117 mlt_properties_set_int( properties
, "width", owidth
);
118 mlt_properties_set_int( properties
, "height", oheight
);
126 /** Constructor for the filter.
129 mlt_filter
filter_swscale_init( mlt_profile profile
, void *arg
)
131 // Create a new scaler
132 mlt_filter
this = mlt_factory_filter( profile
, "rescale", arg
);
134 // If successful, then initialise it
137 // Get the properties
138 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
140 // Set the inerpolation
141 mlt_properties_set( properties
, "interpolation", arg
== NULL ?
"bilinear" : arg
);
144 mlt_properties_set_data( properties
, "method", filter_scale
, 0, NULL
, NULL
);