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 void *( *swscale_fast_memcpy )( void *, const void *, size_t );
53 static picture_t *Filter( filter_t *, picture_t * );
54 static int CheckInit( filter_t * );
56 #define SCALEMODE_TEXT N_("Scaling mode")
57 #define SCALEMODE_LONGTEXT N_("Scaling mode to use.")
59 static const int pi_mode_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
60 const char *const ppsz_mode_descriptions[] =
61 { N_("Fast bilinear"), N_("Bilinear"), N_("Bicubic (good quality)"),
62 N_("Experimental"), N_("Nearest neighbour (bad quality)"),
63 N_("Area"), N_("Luma bicubic / chroma bilinear"), N_("Gauss"),
64 N_("SincR"), N_("Lanczos"), N_("Bicubic spline") };
67 /*****************************************************************************
69 *****************************************************************************/
71 set_description( N_("Video scaling filter") );
72 set_capability( "video filter2", 1000 );
73 set_category( CAT_VIDEO );
74 set_subcategory( SUBCAT_VIDEO_VFILTER );
75 set_callbacks( OpenScaler, CloseScaler );
76 add_integer( "swscale-mode", 0, NULL, SCALEMODE_TEXT, SCALEMODE_LONGTEXT, true );
77 change_integer_list( pi_mode_values, ppsz_mode_descriptions, 0 );
80 /* Version checking */
81 #if LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0)
82 /*****************************************************************************
83 * filter_sys_t : filter descriptor
84 *****************************************************************************/
87 struct SwsContext *ctx;
88 SwsFilter *p_src_filter;
89 SwsFilter *p_dst_filter;
90 int i_cpu_mask, i_sws_flags;
96 /*****************************************************************************
97 * OpenScaler: probe the filter and return score
98 *****************************************************************************/
99 static int OpenScaler( vlc_object_t *p_this )
101 filter_t *p_filter = (filter_t*)p_this;
105 int i_fmt_in, i_fmt_out;
109 float sws_lum_gblur = 0.0, sws_chr_gblur = 0.0;
110 int sws_chr_vshift = 0, sws_chr_hshift = 0;
111 float sws_chr_sharpen = 0.0, sws_lum_sharpen = 0.0;
113 /* Supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR24,
114 * BGR16, BGR15, RGB32, RGB24, Y8/Y800, YVU9/IF09 */
115 i_fmt_in = GetFfmpegChroma(p_filter->fmt_in.video.i_chroma);
116 /* Supported output formats: YV12, I420/IYUV, YUY2, UYVY,
117 * {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09 */
118 i_fmt_out = GetFfmpegChroma(p_filter->fmt_out.video.i_chroma);
119 if( ( i_fmt_in < 0 ) || ( i_fmt_out < 0 ) )
124 /* Allocate the memory needed to store the decoder's structure */
125 if( ( p_filter->p_sys = p_sys =
126 (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
131 swscale_fast_memcpy = vlc_memcpy;
133 /* Set CPU capabilities */
135 p_sys->i_cpu_mask = 0;
136 if( i_cpu & CPU_CAPABILITY_MMX )
138 p_sys->i_cpu_mask |= SWS_CPU_CAPS_MMX;
140 #if (LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0))
141 if( i_cpu & CPU_CAPABILITY_MMXEXT )
143 p_sys->i_cpu_mask |= SWS_CPU_CAPS_MMX2;
146 if( i_cpu & CPU_CAPABILITY_3DNOW )
148 p_sys->i_cpu_mask |= SWS_CPU_CAPS_3DNOW;
150 if( i_cpu & CPU_CAPABILITY_ALTIVEC )
152 p_sys->i_cpu_mask |= SWS_CPU_CAPS_ALTIVEC;
155 var_Create( p_filter, "swscale-mode", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
156 var_Get( p_filter, "swscale-mode", &val );
157 i_sws_mode = val.i_int;
161 case 0: p_sys->i_sws_flags = SWS_FAST_BILINEAR; break;
162 case 1: p_sys->i_sws_flags = SWS_BILINEAR; break;
163 case 2: p_sys->i_sws_flags = SWS_BICUBIC; break;
164 case 3: p_sys->i_sws_flags = SWS_X; break;
165 case 4: p_sys->i_sws_flags = SWS_POINT; break;
166 case 5: p_sys->i_sws_flags = SWS_AREA; break;
167 case 6: p_sys->i_sws_flags = SWS_BICUBLIN; break;
168 case 7: p_sys->i_sws_flags = SWS_GAUSS; break;
169 case 8: p_sys->i_sws_flags = SWS_SINC; break;
170 case 9: p_sys->i_sws_flags = SWS_LANCZOS; break;
171 case 10: p_sys->i_sws_flags = SWS_SPLINE; break;
172 default: p_sys->i_sws_flags = SWS_FAST_BILINEAR; i_sws_mode = 0; break;
175 p_sys->p_src_filter = NULL;
176 p_sys->p_dst_filter = NULL;
177 p_sys->p_src_filter =
178 sws_getDefaultFilter( sws_lum_gblur, sws_chr_gblur,
179 sws_lum_sharpen, sws_chr_sharpen,
180 sws_chr_hshift, sws_chr_vshift, 0 );
184 p_filter->pf_video_filter = Filter;
185 es_format_Init( &p_sys->fmt_in, 0, 0 );
186 es_format_Init( &p_sys->fmt_out, 0, 0 );
188 if( CheckInit( p_filter ) != VLC_SUCCESS )
190 if( p_sys->p_src_filter ) sws_freeFilter( p_sys->p_src_filter );
195 msg_Dbg( p_filter, "%ix%i chroma: %4.4s -> %ix%i chroma: %4.4s",
196 p_filter->fmt_in.video.i_width, p_filter->fmt_in.video.i_height,
197 (char *)&p_filter->fmt_in.video.i_chroma,
198 p_filter->fmt_out.video.i_width, p_filter->fmt_out.video.i_height,
199 (char *)&p_filter->fmt_out.video.i_chroma );
201 if( p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width ||
202 p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height )
204 msg_Dbg( p_filter, "scaling mode: %s",
205 ppsz_mode_descriptions[i_sws_mode] );
211 /*****************************************************************************
212 * CloseFilter: clean up the filter
213 *****************************************************************************/
214 static void CloseScaler( vlc_object_t *p_this )
216 filter_t *p_filter = (filter_t*)p_this;
217 filter_sys_t *p_sys = p_filter->p_sys;
219 if( p_sys->ctx ) sws_freeContext( p_sys->ctx );
220 if( p_sys->p_src_filter ) sws_freeFilter( p_sys->p_src_filter );
224 /*****************************************************************************
225 * CheckInit: Initialise filter when necessary
226 *****************************************************************************/
227 static int CheckInit( filter_t *p_filter )
229 filter_sys_t *p_sys = p_filter->p_sys;
231 if( ( p_filter->fmt_in.video.i_width != p_sys->fmt_in.video.i_width ) ||
232 ( p_filter->fmt_in.video.i_height != p_sys->fmt_in.video.i_height ) ||
233 ( p_filter->fmt_out.video.i_width != p_sys->fmt_out.video.i_width ) ||
234 ( p_filter->fmt_out.video.i_height != p_sys->fmt_out.video.i_height ) )
236 int i_fmt_in, i_fmt_out;
238 i_fmt_in = GetFfmpegChroma(p_filter->fmt_in.video.i_chroma);
239 i_fmt_out = GetFfmpegChroma(p_filter->fmt_out.video.i_chroma);
240 if( (i_fmt_in < 0) || (i_fmt_out < 0) )
242 msg_Err( p_filter, "format not supported" );
246 if( p_sys->ctx ) sws_freeContext( p_sys->ctx );
249 sws_getContext( p_filter->fmt_in.video.i_width,
250 p_filter->fmt_in.video.i_height, i_fmt_in,
251 p_filter->fmt_out.video.i_width,
252 p_filter->fmt_out.video.i_height, i_fmt_out,
253 p_sys->i_sws_flags | p_sys->i_cpu_mask,
254 p_sys->p_src_filter, p_sys->p_dst_filter, 0 );
257 msg_Err( p_filter, "could not init SwScaler" );
261 p_sys->fmt_in = p_filter->fmt_in;
262 p_sys->fmt_out = p_filter->fmt_out;
268 /****************************************************************************
269 * Filter: the whole thing
270 ****************************************************************************
271 * This function is called just after the thread is launched.
272 ****************************************************************************/
273 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
275 filter_sys_t *p_sys = p_filter->p_sys;
276 uint8_t *src[3]; int src_stride[3];
277 uint8_t *dst[3]; int dst_stride[3];
278 picture_t *p_pic_dst;
280 int i_nb_planes = p_pic->i_planes;
282 /* Check if format properties changed */
283 if( CheckInit( p_filter ) != VLC_SUCCESS ) return NULL;
285 /* Request output picture */
286 p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
289 msg_Warn( p_filter, "can't get output picture" );
293 if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','V','A') )
296 memset( p_pic_dst->p[3].p_pixels, 0xff, p_filter->fmt_out.video.i_height
297 * p_pic_dst->p[3].i_pitch );
300 for( i_plane = 0; i_plane < __MIN(3, p_pic->i_planes); i_plane++ )
302 src[i_plane] = p_pic->p[i_plane].p_pixels;
303 src_stride[i_plane] = p_pic->p[i_plane].i_pitch;
305 for( i_plane = 0; i_plane < __MIN(3, i_nb_planes); i_plane++ )
307 dst[i_plane] = p_pic_dst->p[i_plane].p_pixels;
308 dst_stride[i_plane] = p_pic_dst->p[i_plane].i_pitch;
311 #if LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0)
312 sws_scale( p_sys->ctx, src, src_stride,
313 0, p_filter->fmt_in.video.i_height,
316 sws_scale_ordered( p_sys->ctx, src, src_stride,
317 0, p_filter->fmt_in.video.i_height,
321 p_pic_dst->date = p_pic->date;
322 p_pic_dst->b_force = p_pic->b_force;
323 p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
324 p_pic_dst->b_progressive = p_pic->b_progressive;
325 p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
327 if( p_pic->pf_release )
328 p_pic->pf_release( p_pic );
332 #else /* LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0) */
334 int OpenScaler( vlc_object_t *p_this )
339 void CloseScaler( vlc_object_t *p_this )
343 #endif /* LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0) */