]> git.sesse.net Git - vlc/blob - modules/video_filter/swscale.c
3c6bc8d34fad71a2fe17c529bb857fa483692b79
[vlc] / modules / video_filter / swscale.c
1 /*****************************************************************************
2  * swscale.c: scaling and chroma conversion using libswscale
3  *****************************************************************************
4  * Copyright (C) 1999-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_vout.h>
35 #include <vlc_filter.h>
36
37 #ifdef HAVE_LIBSWSCALE_SWSCALE_H
38 #   include <libswscale/swscale.h>
39 #elif defined(HAVE_FFMPEG_SWSCALE_H)
40 #   include <ffmpeg/swscale.h>
41 #endif
42
43 /* Gruikkkkkkkkkk!!!!! */
44 #include "../codec/avcodec/chroma.h"
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 static int  OpenScaler( vlc_object_t * );
50 static void CloseScaler( vlc_object_t * );
51
52 #define SCALEMODE_TEXT N_("Scaling mode")
53 #define SCALEMODE_LONGTEXT N_("Scaling mode to use.")
54
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") };
61
62 vlc_module_begin();
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 );
70 vlc_module_end();
71
72 /* Version checking */
73 #if LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0)
74 /****************************************************************************
75  * Local prototypes
76  ****************************************************************************/
77
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 * );
81
82 static int GetParameters( int *pi_fmti, int *pi_fmto,
83                           const video_format_t *p_fmti, 
84                           const video_format_t *p_fmto  );
85
86 /*****************************************************************************
87  * filter_sys_t : filter descriptor
88  *****************************************************************************/
89 struct filter_sys_t
90 {
91     struct SwsContext *ctx;
92     SwsFilter *p_src_filter;
93     SwsFilter *p_dst_filter;
94     int i_cpu_mask, i_sws_flags;
95
96     es_format_t fmt_in;
97     es_format_t fmt_out;
98 };
99
100
101 /*****************************************************************************
102  * OpenScaler: probe the filter and return score
103  *****************************************************************************/
104 static int OpenScaler( vlc_object_t *p_this )
105 {
106     filter_t *p_filter = (filter_t*)p_this;
107     filter_sys_t *p_sys;
108
109     unsigned int i_cpu;
110     int i_sws_mode;
111
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;
115
116     if( GetParameters( NULL, NULL, 
117                        &p_filter->fmt_in.video,
118                        &p_filter->fmt_out.video ) )
119     {
120         return VLC_EGENERIC;
121     }
122
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 )
125     {
126         return VLC_ENOMEM;
127     }
128
129     swscale_fast_memcpy = vlc_memcpy;   /* FIXME pointer assignment may not be atomic */
130
131     /* Set CPU capabilities */
132     i_cpu = vlc_CPU();
133     p_sys->i_cpu_mask = 0;
134     if( i_cpu & CPU_CAPABILITY_MMX )
135     {
136         p_sys->i_cpu_mask |= SWS_CPU_CAPS_MMX;
137     }
138 #if (LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0))
139     if( i_cpu & CPU_CAPABILITY_MMXEXT )
140     {
141         p_sys->i_cpu_mask |= SWS_CPU_CAPS_MMX2;
142     }
143 #endif
144     if( i_cpu & CPU_CAPABILITY_3DNOW )
145     {
146         p_sys->i_cpu_mask |= SWS_CPU_CAPS_3DNOW;
147     }
148     if( i_cpu & CPU_CAPABILITY_ALTIVEC )
149     {
150         p_sys->i_cpu_mask |= SWS_CPU_CAPS_ALTIVEC;
151     }
152
153     i_sws_mode = var_CreateGetInteger( p_filter, "swscale-mode" );
154
155     switch( i_sws_mode )
156     {
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;
169     }
170
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;
176
177     /* Misc init */
178     p_sys->ctx = 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 );
182
183     if( CheckInit( p_filter ) )
184     {
185         if( p_sys->p_src_filter )
186             sws_freeFilter( p_sys->p_src_filter );
187         free( p_sys );
188         return VLC_EGENERIC;
189     }
190
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] );
197
198     return VLC_SUCCESS;
199 }
200
201 /*****************************************************************************
202  * CloseFilter: clean up the filter
203  *****************************************************************************/
204 static void CloseScaler( vlc_object_t *p_this )
205 {
206     filter_t *p_filter = (filter_t*)p_this;
207     filter_sys_t *p_sys = p_filter->p_sys;
208
209     if( p_sys->ctx )
210         sws_freeContext( p_sys->ctx );
211     if( p_sys->p_src_filter )
212         sws_freeFilter( p_sys->p_src_filter );
213     free( p_sys );
214 }
215
216 /*****************************************************************************
217  * CheckInit: Initialise filter when necessary
218  *****************************************************************************/
219 static bool IsFmtSimilar( const video_format_t *p_fmt1, const video_format_t *p_fmt2 )
220 {
221     return p_fmt1->i_width == p_fmt2->i_width &&
222            p_fmt1->i_height == p_fmt2->i_height;
223 }
224
225 static int GetParameters( int *pi_fmti, int *pi_fmto,
226                           const video_format_t *p_fmti, 
227                           const video_format_t *p_fmto  )
228 {
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 );
232
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 );
236
237     if( pi_fmti )
238         *pi_fmti = i_fmti;
239     if( pi_fmto )
240         *pi_fmto = i_fmto;
241
242     if( i_fmti < 0 || i_fmto < 0 )
243         return VLC_EGENERIC;
244
245     return VLC_SUCCESS;
246 }
247
248 static int CheckInit( filter_t *p_filter )
249 {
250     filter_sys_t *p_sys = p_filter->p_sys;
251
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 ) &&
254         p_sys->ctx )
255     {
256         return VLC_SUCCESS;
257     }
258
259     /* */
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 ) )
262     {
263         msg_Err( p_filter, "format not supported" );
264         return VLC_EGENERIC;
265     }
266
267     if( p_sys->ctx )
268         sws_freeContext( p_sys->ctx );
269
270     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 );
277     if( !p_sys->ctx )
278     {
279         msg_Err( p_filter, "could not init SwScaler" );
280         return VLC_EGENERIC;
281     }
282
283     p_sys->fmt_in = p_filter->fmt_in;
284     p_sys->fmt_out = p_filter->fmt_out;
285
286     return VLC_SUCCESS;
287 }
288
289 static void GetPixels( uint8_t *pp_pixel[3], int pi_pitch[3], picture_t *p_picture )
290 {
291     int n;
292     for( n = 0; n < __MIN(3 , p_picture->i_planes ); n++ )
293     {
294         pp_pixel[n] = p_picture->p[n].p_pixels;
295         pi_pitch[n] = p_picture->p[n].i_pitch;
296     }
297 }
298
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 )
305 {
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;
310
311     /* Check if format properties changed */
312     if( CheckInit( p_filter ) != VLC_SUCCESS )
313     {
314         picture_Release( p_pic );
315         return NULL;
316     }
317
318     /* Request output picture */
319     p_pic_dst = filter_NewPicture( p_filter );
320     if( !p_pic_dst )
321     {
322         picture_Release( p_pic );
323         return NULL;
324     }
325
326     if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','V','A') )
327     {
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 );
330     }
331
332     GetPixels( src, src_stride, p_pic );
333     GetPixels( dst, dst_stride, p_pic_dst );
334
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,
338                dst, dst_stride );
339 #else
340     sws_scale_ordered( p_sys->ctx, src, src_stride,
341                0, p_filter->fmt_in.video.i_height,
342                dst, dst_stride );
343 #endif
344
345     picture_CopyProperties( p_pic_dst, p_pic );
346     picture_Release( p_pic );
347     return p_pic_dst;
348 }
349
350 #else /* LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0) */
351
352 int OpenScaler( vlc_object_t *p_this )
353 {
354     return VLC_EGENERIC;
355 }
356
357 void CloseScaler( vlc_object_t *p_this )
358 {
359 }
360
361 #endif /* LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0) */