1 /*****************************************************************************
2 * filter.c: video scaling module using the swscale library
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Author: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_codec.h>
34 #include <vlc_filter.h>
37 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
38 # include <libavcodec/avcodec.h>
39 #elif defined(HAVE_FFMPEG_AVCODEC_H)
40 # include <ffmpeg/avcodec.h>
45 #ifdef HAVE_LIBSWSCALE_SWSCALE_H
46 # include <libswscale/swscale.h>
47 #elif defined(HAVE_FFMPEG_SWSCALE_H)
48 # include <ffmpeg/swscale.h>
53 /* Version checking */
54 #if LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0)
56 /*****************************************************************************
57 * filter_sys_t : filter descriptor
58 *****************************************************************************/
61 struct SwsContext *ctx;
62 SwsFilter *p_src_filter;
63 SwsFilter *p_dst_filter;
64 int i_cpu_mask, i_sws_flags;
70 /****************************************************************************
72 ****************************************************************************/
73 void *( *swscale_fast_memcpy )( void *, const void *, size_t );
74 static picture_t *Filter( filter_t *, picture_t * );
75 static int CheckInit( filter_t * );
77 static const char *ppsz_mode_descriptions[] =
78 { N_("Fast bilinear"), N_("Bilinear"), N_("Bicubic (good quality)"),
79 N_("Experimental"), N_("Nearest neighbour (bad quality)"),
80 N_("Area"), N_("Luma bicubic / chroma bilinear"), N_("Gauss"),
81 N_("SincR"), N_("Lanczos"), N_("Bicubic spline") };
83 /*****************************************************************************
84 * OpenScaler: probe the filter and return score
85 *****************************************************************************/
86 int OpenScaler( vlc_object_t *p_this )
88 filter_t *p_filter = (filter_t*)p_this;
92 int i_fmt_in, i_fmt_out;
96 float sws_lum_gblur = 0.0, sws_chr_gblur = 0.0;
97 int sws_chr_vshift = 0, sws_chr_hshift = 0;
98 float sws_chr_sharpen = 0.0, sws_lum_sharpen = 0.0;
100 /* Supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR24,
101 * BGR16, BGR15, RGB32, RGB24, Y8/Y800, YVU9/IF09 */
102 i_fmt_in = GetFfmpegChroma(p_filter->fmt_in.video.i_chroma);
103 /* Supported output formats: YV12, I420/IYUV, YUY2, UYVY,
104 * {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09 */
105 i_fmt_out = GetFfmpegChroma(p_filter->fmt_out.video.i_chroma);
106 if( ( i_fmt_in < 0 ) || ( i_fmt_out < 0 ) )
111 /* Allocate the memory needed to store the decoder's structure */
112 if( ( p_filter->p_sys = p_sys =
113 (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
118 swscale_fast_memcpy = vlc_memcpy;
120 /* Set CPU capabilities */
122 p_sys->i_cpu_mask = 0;
123 if( i_cpu & CPU_CAPABILITY_MMX )
125 p_sys->i_cpu_mask |= SWS_CPU_CAPS_MMX;
127 #if (LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0))
128 if( i_cpu & CPU_CAPABILITY_MMXEXT )
130 p_sys->i_cpu_mask |= SWS_CPU_CAPS_MMX2;
133 if( i_cpu & CPU_CAPABILITY_3DNOW )
135 p_sys->i_cpu_mask |= SWS_CPU_CAPS_3DNOW;
137 if( i_cpu & CPU_CAPABILITY_ALTIVEC )
139 p_sys->i_cpu_mask |= SWS_CPU_CAPS_ALTIVEC;
142 var_Create( p_filter, "swscale-mode", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
143 var_Get( p_filter, "swscale-mode", &val );
144 i_sws_mode = val.i_int;
148 case 0: p_sys->i_sws_flags = SWS_FAST_BILINEAR; break;
149 case 1: p_sys->i_sws_flags = SWS_BILINEAR; break;
150 case 2: p_sys->i_sws_flags = SWS_BICUBIC; break;
151 case 3: p_sys->i_sws_flags = SWS_X; break;
152 case 4: p_sys->i_sws_flags = SWS_POINT; break;
153 case 5: p_sys->i_sws_flags = SWS_AREA; break;
154 case 6: p_sys->i_sws_flags = SWS_BICUBLIN; break;
155 case 7: p_sys->i_sws_flags = SWS_GAUSS; break;
156 case 8: p_sys->i_sws_flags = SWS_SINC; break;
157 case 9: p_sys->i_sws_flags = SWS_LANCZOS; break;
158 case 10: p_sys->i_sws_flags = SWS_SPLINE; break;
159 default: p_sys->i_sws_flags = SWS_FAST_BILINEAR; i_sws_mode = 0; break;
162 p_sys->p_src_filter = NULL;
163 p_sys->p_dst_filter = NULL;
164 p_sys->p_src_filter =
165 sws_getDefaultFilter( sws_lum_gblur, sws_chr_gblur,
166 sws_lum_sharpen, sws_chr_sharpen,
167 sws_chr_hshift, sws_chr_vshift, 0 );
171 p_filter->pf_video_filter = Filter;
172 es_format_Init( &p_sys->fmt_in, 0, 0 );
173 es_format_Init( &p_sys->fmt_out, 0, 0 );
175 if( CheckInit( p_filter ) != VLC_SUCCESS )
177 if( p_sys->p_src_filter ) sws_freeFilter( p_sys->p_src_filter );
182 msg_Dbg( p_filter, "%ix%i chroma: %4.4s -> %ix%i chroma: %4.4s",
183 p_filter->fmt_in.video.i_width, p_filter->fmt_in.video.i_height,
184 (char *)&p_filter->fmt_in.video.i_chroma,
185 p_filter->fmt_out.video.i_width, p_filter->fmt_out.video.i_height,
186 (char *)&p_filter->fmt_out.video.i_chroma );
188 if( p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width ||
189 p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height )
191 msg_Dbg( p_filter, "scaling mode: %s",
192 ppsz_mode_descriptions[i_sws_mode] );
198 /*****************************************************************************
199 * CloseFilter: clean up the filter
200 *****************************************************************************/
201 void CloseScaler( vlc_object_t *p_this )
203 filter_t *p_filter = (filter_t*)p_this;
204 filter_sys_t *p_sys = p_filter->p_sys;
206 if( p_sys->ctx ) sws_freeContext( p_sys->ctx );
207 if( p_sys->p_src_filter ) sws_freeFilter( p_sys->p_src_filter );
211 /*****************************************************************************
212 * CheckInit: Initialise filter when necessary
213 *****************************************************************************/
214 static int CheckInit( filter_t *p_filter )
216 filter_sys_t *p_sys = p_filter->p_sys;
218 if( ( p_filter->fmt_in.video.i_width != p_sys->fmt_in.video.i_width ) ||
219 ( p_filter->fmt_in.video.i_height != p_sys->fmt_in.video.i_height ) ||
220 ( p_filter->fmt_out.video.i_width != p_sys->fmt_out.video.i_width ) ||
221 ( p_filter->fmt_out.video.i_height != p_sys->fmt_out.video.i_height ) )
223 int i_fmt_in, i_fmt_out;
225 i_fmt_in = GetFfmpegChroma(p_filter->fmt_in.video.i_chroma);
226 i_fmt_out = GetFfmpegChroma(p_filter->fmt_out.video.i_chroma);
227 if( (i_fmt_in < 0) || (i_fmt_out < 0) )
229 msg_Err( p_filter, "format not supported" );
233 if( p_sys->ctx ) sws_freeContext( p_sys->ctx );
236 sws_getContext( p_filter->fmt_in.video.i_width,
237 p_filter->fmt_in.video.i_height, i_fmt_in,
238 p_filter->fmt_out.video.i_width,
239 p_filter->fmt_out.video.i_height, i_fmt_out,
240 p_sys->i_sws_flags | p_sys->i_cpu_mask,
241 p_sys->p_src_filter, p_sys->p_dst_filter, 0 );
244 msg_Err( p_filter, "could not init SwScaler" );
248 p_sys->fmt_in = p_filter->fmt_in;
249 p_sys->fmt_out = p_filter->fmt_out;
255 /****************************************************************************
256 * Filter: the whole thing
257 ****************************************************************************
258 * This function is called just after the thread is launched.
259 ****************************************************************************/
260 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
262 filter_sys_t *p_sys = p_filter->p_sys;
263 uint8_t *src[3]; int src_stride[3];
264 uint8_t *dst[3]; int dst_stride[3];
265 picture_t *p_pic_dst;
267 int i_nb_planes = p_pic->i_planes;
269 /* Check if format properties changed */
270 if( CheckInit( p_filter ) != VLC_SUCCESS ) return NULL;
272 /* Request output picture */
273 p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
276 msg_Warn( p_filter, "can't get output picture" );
280 if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','V','A') )
283 memset( p_pic_dst->p[3].p_pixels, 0xff, p_filter->fmt_out.video.i_height
284 * p_pic_dst->p[3].i_pitch );
287 for( i_plane = 0; i_plane < __MIN(3, p_pic->i_planes); i_plane++ )
289 src[i_plane] = p_pic->p[i_plane].p_pixels;
290 src_stride[i_plane] = p_pic->p[i_plane].i_pitch;
292 for( i_plane = 0; i_plane < __MIN(3, i_nb_planes); i_plane++ )
294 dst[i_plane] = p_pic_dst->p[i_plane].p_pixels;
295 dst_stride[i_plane] = p_pic_dst->p[i_plane].i_pitch;
298 #if LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0)
299 sws_scale( p_sys->ctx, src, src_stride,
300 0, p_filter->fmt_in.video.i_height,
303 sws_scale_ordered( p_sys->ctx, src, src_stride,
304 0, p_filter->fmt_in.video.i_height,
308 p_pic_dst->date = p_pic->date;
309 p_pic_dst->b_force = p_pic->b_force;
310 p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
311 p_pic_dst->b_progressive = p_pic->b_progressive;
312 p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
314 if( p_pic->pf_release )
315 p_pic->pf_release( p_pic );
319 #else /* LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0) */
321 int OpenScaler( vlc_object_t *p_this )
326 void CloseScaler( vlc_object_t *p_this )
330 #endif /* LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0) */