1 /*****************************************************************************
2 * swscale.c: scaling and chroma conversion using libswscale
3 *****************************************************************************
4 * Copyright (C) 1999-2008 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <vlc_filter.h>
37 #ifdef HAVE_LIBSWSCALE_SWSCALE_H
38 # include <libswscale/swscale.h>
39 #elif defined(HAVE_FFMPEG_SWSCALE_H)
40 # include <ffmpeg/swscale.h>
43 /* Gruikkkkkkkkkk!!!!! */
44 #include "../codec/avcodec/chroma.h"
46 /*****************************************************************************
48 *****************************************************************************/
49 static int OpenScaler( vlc_object_t * );
50 static void CloseScaler( vlc_object_t * );
52 #define SCALEMODE_TEXT N_("Scaling mode")
53 #define SCALEMODE_LONGTEXT N_("Scaling mode to use.")
55 static const int pi_mode_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
56 const char *const ppsz_mode_descriptions[] =
57 { N_("Fast bilinear"), N_("Bilinear"), N_("Bicubic (good quality)"),
58 N_("Experimental"), N_("Nearest neighbour (bad quality)"),
59 N_("Area"), N_("Luma bicubic / chroma bilinear"), N_("Gauss"),
60 N_("SincR"), N_("Lanczos"), N_("Bicubic spline") };
63 set_description( N_("Video scaling filter") );
64 set_capability( "video filter2", 1000 );
65 set_category( CAT_VIDEO );
66 set_subcategory( SUBCAT_VIDEO_VFILTER );
67 set_callbacks( OpenScaler, CloseScaler );
68 add_integer( "swscale-mode", 2, NULL, SCALEMODE_TEXT, SCALEMODE_LONGTEXT, true );
69 change_integer_list( pi_mode_values, ppsz_mode_descriptions, 0 );
72 /* Version checking */
73 #if LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0)
74 /****************************************************************************
76 ****************************************************************************/
78 void *( *swscale_fast_memcpy )( void *, const void *, size_t );
79 static picture_t *Filter( filter_t *, picture_t * );
80 static int CheckInit( filter_t * );
82 static int GetParameters( int *pi_fmti, int *pi_fmto,
83 const video_format_t *p_fmti,
84 const video_format_t *p_fmto );
86 /*****************************************************************************
87 * filter_sys_t : filter descriptor
88 *****************************************************************************/
91 struct SwsContext *ctx;
92 SwsFilter *p_src_filter;
93 SwsFilter *p_dst_filter;
94 int i_cpu_mask, i_sws_flags;
101 /*****************************************************************************
102 * OpenScaler: probe the filter and return score
103 *****************************************************************************/
104 static int OpenScaler( vlc_object_t *p_this )
106 filter_t *p_filter = (filter_t*)p_this;
112 float sws_lum_gblur = 0.0, sws_chr_gblur = 0.0;
113 int sws_chr_vshift = 0, sws_chr_hshift = 0;
114 float sws_chr_sharpen = 0.0, sws_lum_sharpen = 0.0;
116 if( GetParameters( NULL, NULL,
117 &p_filter->fmt_in.video,
118 &p_filter->fmt_out.video ) )
123 /* Allocate the memory needed to store the decoder's structure */
124 if( ( p_filter->p_sys = p_sys = malloc(sizeof(filter_sys_t)) ) == NULL )
129 swscale_fast_memcpy = vlc_memcpy; /* FIXME pointer assignment may not be atomic */
131 /* Set CPU capabilities */
133 p_sys->i_cpu_mask = 0;
134 if( i_cpu & CPU_CAPABILITY_MMX )
136 p_sys->i_cpu_mask |= SWS_CPU_CAPS_MMX;
138 #if (LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0))
139 if( i_cpu & CPU_CAPABILITY_MMXEXT )
141 p_sys->i_cpu_mask |= SWS_CPU_CAPS_MMX2;
144 if( i_cpu & CPU_CAPABILITY_3DNOW )
146 p_sys->i_cpu_mask |= SWS_CPU_CAPS_3DNOW;
148 if( i_cpu & CPU_CAPABILITY_ALTIVEC )
150 p_sys->i_cpu_mask |= SWS_CPU_CAPS_ALTIVEC;
153 i_sws_mode = var_CreateGetInteger( p_filter, "swscale-mode" );
157 case 0: p_sys->i_sws_flags = SWS_FAST_BILINEAR; break;
158 case 1: p_sys->i_sws_flags = SWS_BILINEAR; break;
159 case 2: p_sys->i_sws_flags = SWS_BICUBIC; break;
160 case 3: p_sys->i_sws_flags = SWS_X; break;
161 case 4: p_sys->i_sws_flags = SWS_POINT; break;
162 case 5: p_sys->i_sws_flags = SWS_AREA; break;
163 case 6: p_sys->i_sws_flags = SWS_BICUBLIN; break;
164 case 7: p_sys->i_sws_flags = SWS_GAUSS; break;
165 case 8: p_sys->i_sws_flags = SWS_SINC; break;
166 case 9: p_sys->i_sws_flags = SWS_LANCZOS; break;
167 case 10: p_sys->i_sws_flags = SWS_SPLINE; break;
168 default: p_sys->i_sws_flags = SWS_BICUBIC; i_sws_mode = 2; break;
171 p_sys->p_src_filter =
172 sws_getDefaultFilter( sws_lum_gblur, sws_chr_gblur,
173 sws_lum_sharpen, sws_chr_sharpen,
174 sws_chr_hshift, sws_chr_vshift, 0 );
175 p_sys->p_dst_filter = NULL;
179 p_filter->pf_video_filter = Filter;
180 es_format_Init( &p_sys->fmt_in, 0, 0 );
181 es_format_Init( &p_sys->fmt_out, 0, 0 );
183 if( CheckInit( p_filter ) )
185 if( p_sys->p_src_filter )
186 sws_freeFilter( p_sys->p_src_filter );
191 msg_Dbg( p_filter, "%ix%i chroma: %4.4s -> %ix%i chroma: %4.4s with scaling using %s",
192 p_filter->fmt_in.video.i_width, p_filter->fmt_in.video.i_height,
193 (char *)&p_filter->fmt_in.video.i_chroma,
194 p_filter->fmt_out.video.i_width, p_filter->fmt_out.video.i_height,
195 (char *)&p_filter->fmt_out.video.i_chroma,
196 ppsz_mode_descriptions[i_sws_mode] );
201 /*****************************************************************************
202 * CloseFilter: clean up the filter
203 *****************************************************************************/
204 static void CloseScaler( vlc_object_t *p_this )
206 filter_t *p_filter = (filter_t*)p_this;
207 filter_sys_t *p_sys = p_filter->p_sys;
210 sws_freeContext( p_sys->ctx );
211 if( p_sys->p_src_filter )
212 sws_freeFilter( p_sys->p_src_filter );
216 /*****************************************************************************
217 * CheckInit: Initialise filter when necessary
218 *****************************************************************************/
219 static bool IsFmtSimilar( const video_format_t *p_fmt1, const video_format_t *p_fmt2 )
221 return p_fmt1->i_width == p_fmt2->i_width &&
222 p_fmt1->i_height == p_fmt2->i_height;
225 static int GetParameters( int *pi_fmti, int *pi_fmto,
226 const video_format_t *p_fmti,
227 const video_format_t *p_fmto )
229 /* Supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR24,
230 * BGR16, BGR15, RGB32, RGB24, Y8/Y800, YVU9/IF09 */
231 const int i_fmti = GetFfmpegChroma( p_fmti->i_chroma );
233 /* Supported output formats: YV12, I420/IYUV, YUY2, UYVY,
234 * {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09 */
235 const int i_fmto = GetFfmpegChroma( p_fmto->i_chroma );
242 if( i_fmti < 0 || i_fmto < 0 )
248 static int CheckInit( filter_t *p_filter )
250 filter_sys_t *p_sys = p_filter->p_sys;
252 if( IsFmtSimilar( &p_filter->fmt_in.video, &p_sys->fmt_in.video ) &&
253 IsFmtSimilar( &p_filter->fmt_out.video, &p_sys->fmt_out.video ) &&
260 int i_fmt_in, i_fmt_out;
261 if( GetParameters( &i_fmt_in, &i_fmt_out, &p_filter->fmt_in.video, &p_filter->fmt_out.video ) )
263 msg_Err( p_filter, "format not supported" );
268 sws_freeContext( p_sys->ctx );
271 sws_getContext( p_filter->fmt_in.video.i_width,
272 p_filter->fmt_in.video.i_height, i_fmt_in,
273 p_filter->fmt_out.video.i_width,
274 p_filter->fmt_out.video.i_height, i_fmt_out,
275 p_sys->i_sws_flags | p_sys->i_cpu_mask,
276 p_sys->p_src_filter, p_sys->p_dst_filter, 0 );
279 msg_Err( p_filter, "could not init SwScaler" );
283 p_sys->fmt_in = p_filter->fmt_in;
284 p_sys->fmt_out = p_filter->fmt_out;
289 static void GetPixels( uint8_t *pp_pixel[3], int pi_pitch[3], picture_t *p_picture )
292 for( n = 0; n < __MIN(3 , p_picture->i_planes ); n++ )
294 pp_pixel[n] = p_picture->p[n].p_pixels;
295 pi_pitch[n] = p_picture->p[n].i_pitch;
299 /****************************************************************************
300 * Filter: the whole thing
301 ****************************************************************************
302 * This function is called just after the thread is launched.
303 ****************************************************************************/
304 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
306 filter_sys_t *p_sys = p_filter->p_sys;
307 uint8_t *src[3]; int src_stride[3];
308 uint8_t *dst[3]; int dst_stride[3];
309 picture_t *p_pic_dst;
311 /* Check if format properties changed */
312 if( CheckInit( p_filter ) != VLC_SUCCESS )
314 picture_Release( p_pic );
318 /* Request output picture */
319 p_pic_dst = filter_NewPicture( p_filter );
322 picture_Release( p_pic );
326 if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','V','A') )
328 memset( p_pic_dst->p[3].p_pixels, 0xff, p_filter->fmt_out.video.i_height
329 * p_pic_dst->p[3].i_pitch );
332 GetPixels( src, src_stride, p_pic );
333 GetPixels( dst, dst_stride, p_pic_dst );
335 #if LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0)
336 sws_scale( p_sys->ctx, src, src_stride,
337 0, p_filter->fmt_in.video.i_height,
340 sws_scale_ordered( p_sys->ctx, src, src_stride,
341 0, p_filter->fmt_in.video.i_height,
345 picture_CopyProperties( p_pic_dst, p_pic );
346 picture_Release( p_pic );
350 #else /* LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0) */
352 int OpenScaler( vlc_object_t *p_this )
357 void CloseScaler( vlc_object_t *p_this )
361 #endif /* LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0) */