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, 3, 4, 5, 6, 7, 8, 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 );
81 * Internal swscale filter structure.
85 SwsFilter *p_src_filter;
86 SwsFilter *p_dst_filter;
87 int i_cpu_mask, i_sws_flags;
89 video_format_t fmt_in;
90 video_format_t fmt_out;
92 struct SwsContext *ctx;
93 struct SwsContext *ctxA;
98 static picture_t *Filter( filter_t *, picture_t * );
99 static int Init( filter_t * );
100 static void Clean( filter_t * );
102 static int GetParameters( int *pi_fmti, int *pi_fmto, bool *pb_has_a, int *pi_sws_flags,
103 const video_format_t *p_fmti,
104 const video_format_t *p_fmto,
105 int i_sws_flags_default );
107 static int GetSwsCpuMask(void);
109 /* FFmpeg point resize quality seems really bad, let our scale module do it
110 * (change it to true to try) */
111 #define ALLOW_YUVP (false)
113 /*****************************************************************************
114 * OpenScaler: probe the filter and return score
115 *****************************************************************************/
116 static int OpenScaler( vlc_object_t *p_this )
118 filter_t *p_filter = (filter_t*)p_this;
123 float sws_lum_gblur = 0.0, sws_chr_gblur = 0.0;
124 int sws_chr_vshift = 0, sws_chr_hshift = 0;
125 float sws_chr_sharpen = 0.0, sws_lum_sharpen = 0.0;
127 if( GetParameters( NULL, NULL, NULL, NULL,
128 &p_filter->fmt_in.video,
129 &p_filter->fmt_out.video, 0 ) )
135 p_filter->pf_video_filter = Filter;
136 /* Allocate the memory needed to store the decoder's structure */
137 if( ( p_filter->p_sys = p_sys = malloc(sizeof(filter_sys_t)) ) == NULL )
140 /* FIXME pointer assignment may not be atomic */
141 swscale_fast_memcpy = vlc_memcpy;
143 /* Set CPU capabilities */
144 p_sys->i_cpu_mask = GetSwsCpuMask();
147 i_sws_mode = var_CreateGetInteger( p_filter, "swscale-mode" );
150 case 0: p_sys->i_sws_flags = SWS_FAST_BILINEAR; break;
151 case 1: p_sys->i_sws_flags = SWS_BILINEAR; break;
152 case 2: p_sys->i_sws_flags = SWS_BICUBIC; break;
153 case 3: p_sys->i_sws_flags = SWS_X; break;
154 case 4: p_sys->i_sws_flags = SWS_POINT; break;
155 case 5: p_sys->i_sws_flags = SWS_AREA; break;
156 case 6: p_sys->i_sws_flags = SWS_BICUBLIN; break;
157 case 7: p_sys->i_sws_flags = SWS_GAUSS; break;
158 case 8: p_sys->i_sws_flags = SWS_SINC; break;
159 case 9: p_sys->i_sws_flags = SWS_LANCZOS; break;
160 case 10: p_sys->i_sws_flags = SWS_SPLINE; break;
161 default: p_sys->i_sws_flags = SWS_BICUBIC; i_sws_mode = 2; break;
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 );
168 p_sys->p_dst_filter = NULL;
173 p_sys->p_src_a = NULL;
174 p_sys->p_dst_a = NULL;
175 memset( &p_sys->fmt_in, 0, sizeof(p_sys->fmt_in) );
176 memset( &p_sys->fmt_out, 0, sizeof(p_sys->fmt_out) );
178 if( Init( p_filter ) )
180 if( p_sys->p_src_filter )
181 sws_freeFilter( p_sys->p_src_filter );
186 msg_Dbg( p_filter, "%ix%i chroma: %4.4s -> %ix%i chroma: %4.4s with scaling using %s",
187 p_filter->fmt_in.video.i_width, p_filter->fmt_in.video.i_height,
188 (char *)&p_filter->fmt_in.video.i_chroma,
189 p_filter->fmt_out.video.i_width, p_filter->fmt_out.video.i_height,
190 (char *)&p_filter->fmt_out.video.i_chroma,
191 ppsz_mode_descriptions[i_sws_mode] );
196 /*****************************************************************************
197 * CloseFilter: clean up the filter
198 *****************************************************************************/
199 static void CloseScaler( vlc_object_t *p_this )
201 filter_t *p_filter = (filter_t*)p_this;
202 filter_sys_t *p_sys = p_filter->p_sys;
205 if( p_sys->p_src_filter )
206 sws_freeFilter( p_sys->p_src_filter );
210 /*****************************************************************************
212 *****************************************************************************/
213 static int GetSwsCpuMask(void)
215 const unsigned int i_cpu = vlc_CPU();
218 if( i_cpu & CPU_CAPABILITY_MMX )
219 i_sws_cpu |= SWS_CPU_CAPS_MMX;
220 #if (LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0))
221 if( i_cpu & CPU_CAPABILITY_MMXEXT )
222 i_sws_cpu |= SWS_CPU_CAPS_MMX2;
224 if( i_cpu & CPU_CAPABILITY_3DNOW )
225 i_sws_cpu |= SWS_CPU_CAPS_3DNOW;
227 if( i_cpu & CPU_CAPABILITY_ALTIVEC )
228 i_sws_cpu |= SWS_CPU_CAPS_ALTIVEC;
232 static bool IsFmtSimilar( const video_format_t *p_fmt1, const video_format_t *p_fmt2 )
234 return p_fmt1->i_chroma == p_fmt2->i_chroma &&
235 p_fmt1->i_width == p_fmt2->i_width &&
236 p_fmt1->i_height == p_fmt2->i_height;
239 static int GetParameters( int *pi_fmti, int *pi_fmto, bool *pb_has_a, int *pi_sws_flags,
240 const video_format_t *p_fmti,
241 const video_format_t *p_fmto,
242 int i_sws_flags_default )
244 /* Supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR24,
245 * BGR16, BGR15, RGB32, RGB24, Y8/Y800, YVU9/IF09 */
246 int i_fmti = GetFfmpegChroma( p_fmti->i_chroma );
248 /* Supported output formats: YV12, I420/IYUV, YUY2, UYVY,
249 * {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09 */
250 int i_fmto = GetFfmpegChroma( p_fmto->i_chroma );
252 bool b_has_a = false;
253 int i_sws_flags = i_sws_flags_default;
255 if( p_fmti->i_chroma == p_fmto->i_chroma && 0 )
257 if( p_fmti->i_chroma == VLC_FOURCC( 'Y', 'U', 'V', 'P' ) && ALLOW_YUVP )
259 i_fmti = i_fmto = PIX_FMT_GRAY8;
260 i_sws_flags = SWS_POINT;
262 else if( p_fmti->i_chroma == VLC_FOURCC( 'Y', 'U', 'V', 'A' ) )
264 i_fmti = i_fmto = PIX_FMT_YUV444P;
267 else if( p_fmti->i_chroma == VLC_FOURCC( 'R', 'G', 'B', 'A' ) )
269 i_fmti = i_fmto = PIX_FMT_RGBA32;
281 *pi_sws_flags = i_sws_flags;
283 if( i_fmti < 0 || i_fmto < 0 )
289 static int Init( filter_t *p_filter )
291 filter_sys_t *p_sys = p_filter->p_sys;
292 const video_format_t *p_fmti = &p_filter->fmt_in.video;
293 const video_format_t *p_fmto = &p_filter->fmt_out.video;
295 if( IsFmtSimilar( p_fmti, &p_sys->fmt_in ) &&
296 IsFmtSimilar( p_fmto, &p_sys->fmt_out ) &&
303 /* Init with new parameters */
304 int i_fmt_in, i_fmt_out;
307 if( GetParameters( &i_fmt_in, &i_fmt_out, &b_has_a, &i_sws_flags,
308 p_fmti, p_fmto, p_sys->i_sws_flags ) )
310 msg_Err( p_filter, "format not supported" );
314 for( int n = 0; n < (b_has_a ? 2 : 1); n++ )
316 const int i_fmti = n == 0 ? i_fmt_in : PIX_FMT_GRAY8;
317 const int i_fmto = n == 0 ? i_fmt_out : PIX_FMT_GRAY8;
318 struct SwsContext *ctx;
320 ctx = sws_getContext( p_fmti->i_width, p_fmti->i_height, i_fmti,
321 p_fmto->i_width, p_fmto->i_height, i_fmto,
322 i_sws_flags | p_sys->i_cpu_mask,
323 p_sys->p_src_filter, p_sys->p_dst_filter, 0 );
331 p_sys->p_src_a = picture_New( VLC_FOURCC( 'G', 'R', 'E', 'Y' ), p_fmti->i_width, p_fmti->i_height, 0 );
332 p_sys->p_dst_a = picture_New( VLC_FOURCC( 'G', 'R', 'E', 'Y' ), p_fmto->i_width, p_fmto->i_height, 0 );
336 ( b_has_a && ( !p_sys->ctxA || !p_sys->p_src_a || !p_sys->p_dst_a ) ) )
338 msg_Err( p_filter, "could not init SwScaler and/or allocate memory" );
343 p_sys->fmt_in = *p_fmti;
344 p_sys->fmt_out = *p_fmto;
346 msg_Err( p_filter, "%ix%i chroma: %4.4s -> %ix%i chroma: %4.4s",
347 p_fmti->i_width, p_fmti->i_height, (char *)&p_fmti->i_chroma,
348 p_fmto->i_width, p_fmto->i_height, (char *)&p_fmto->i_chroma );
353 static void Clean( filter_t *p_filter )
355 filter_sys_t *p_sys = p_filter->p_sys;
358 picture_Release( p_sys->p_src_a );
360 picture_Release( p_sys->p_dst_a );
362 sws_freeContext( p_sys->ctxA );
365 sws_freeContext( p_sys->ctx );
367 /* We have to set it to null has we call be called again :( */
370 p_sys->p_src_a = NULL;
371 p_sys->p_dst_a = NULL;
374 static void GetPixels( uint8_t *pp_pixel[3], int pi_pitch[3],
375 const picture_t *p_picture,
376 int i_plane_start, int i_plane_count )
378 for( int n = 0; n < __MIN(i_plane_count, p_picture->i_planes-i_plane_start ); n++ )
380 pp_pixel[n] = p_picture->p[i_plane_start+n].p_pixels;
381 pi_pitch[n] = p_picture->p[i_plane_start+n].i_pitch;
385 /* XXX is it always 3 even for BIG_ENDIAN (blend.c seems to think so) ? */
387 static void ExtractA( picture_t *p_dst, const picture_t *p_src, const video_format_t *p_fmt )
389 plane_t *d = &p_dst->p[0];
390 const plane_t *s = &p_src->p[0];
392 for( unsigned y = 0; y < p_fmt->i_height; y++ )
393 for( unsigned x = 0; x < p_fmt->i_width; x++ )
394 d->p_pixels[y*d->i_pitch+x] = s->p_pixels[y*s->i_pitch+4*x+OFFSET_A];
396 static void InjectA( picture_t *p_dst, const picture_t *p_src, const video_format_t *p_fmt )
398 plane_t *d = &p_dst->p[0];
399 const plane_t *s = &p_src->p[0];
401 for( unsigned y = 0; y < p_fmt->i_height; y++ )
402 for( unsigned x = 0; x < p_fmt->i_width; x++ )
403 d->p_pixels[y*d->i_pitch+4*x+OFFSET_A] = s->p_pixels[y*s->i_pitch+x];
407 static void Convert( struct SwsContext *ctx,
408 picture_t *p_dst, picture_t *p_src, const video_format_t *p_fmti, int i_plane_start, int i_plane_count )
410 uint8_t *src[3]; int src_stride[3];
411 uint8_t *dst[3]; int dst_stride[3];
413 GetPixels( src, src_stride, p_src, i_plane_start, i_plane_count );
414 GetPixels( dst, dst_stride, p_dst, i_plane_start, i_plane_count );
416 #if LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0)
417 sws_scale( ctx, src, src_stride, 0, p_fmti->i_height,
420 sws_scale_ordered( ctx, src, src_stride, 0, p_fmti->i_height,
425 /****************************************************************************
426 * Filter: the whole thing
427 ****************************************************************************
428 * This function is called just after the thread is launched.
429 ****************************************************************************/
430 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
432 filter_sys_t *p_sys = p_filter->p_sys;
433 const video_format_t *p_fmti = &p_filter->fmt_in.video;
434 const video_format_t *p_fmto = &p_filter->fmt_out.video;
435 picture_t *p_pic_dst;
437 /* Check if format properties changed */
438 if( Init( p_filter ) )
440 picture_Release( p_pic );
444 /* Request output picture */
445 p_pic_dst = filter_NewPicture( p_filter );
448 picture_Release( p_pic );
452 Convert( p_sys->ctx, p_pic_dst, p_pic, p_fmti, 0, 3 );
455 if( p_fmto->i_chroma == VLC_FOURCC( 'R', 'G', 'B', 'A' ) )
457 /* We extract the A plane to rescale it, and then we reinject it. */
458 ExtractA( p_sys->p_src_a, p_pic, p_fmti );
460 Convert( p_sys->ctxA, p_sys->p_dst_a, p_sys->p_src_a, p_fmti, 0, 1 );
462 InjectA( p_pic_dst, p_sys->p_dst_a, p_fmto );
466 Convert( p_sys->ctx, p_pic_dst, p_pic, p_fmti, 3, 1 );
470 picture_CopyProperties( p_pic_dst, p_pic );
471 picture_Release( p_pic );
475 #else /* LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0) */
477 int OpenScaler( vlc_object_t *p_this )
482 void CloseScaler( vlc_object_t *p_this )
486 #endif /* LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0) */