]> git.sesse.net Git - vlc/blob - modules/video_filter/swscale.c
Removed debug code (effectly enable swscale).
[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, 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") };
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
80 /**
81  * Internal swscale filter structure.
82  */
83 struct filter_sys_t
84 {
85     SwsFilter *p_src_filter;
86     SwsFilter *p_dst_filter;
87     int i_cpu_mask, i_sws_flags;
88
89     video_format_t fmt_in;
90     video_format_t fmt_out;
91
92     struct SwsContext *ctx;
93     struct SwsContext *ctxA;
94     picture_t *p_src_a;
95     picture_t *p_dst_a;
96 };
97
98 static picture_t *Filter( filter_t *, picture_t * );
99 static int  Init( filter_t * );
100 static void Clean( filter_t * );
101
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 );
106
107 static int GetSwsCpuMask(void);
108
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)
112
113 /*****************************************************************************
114  * OpenScaler: probe the filter and return score
115  *****************************************************************************/
116 static int OpenScaler( vlc_object_t *p_this )
117 {
118     filter_t *p_filter = (filter_t*)p_this;
119     filter_sys_t *p_sys;
120
121     int i_sws_mode;
122
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;
126
127     if( GetParameters( NULL, NULL, NULL, NULL,
128                        &p_filter->fmt_in.video,
129                        &p_filter->fmt_out.video, 0 ) )
130     {
131         return VLC_EGENERIC;
132     }
133
134     /* */
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 )
138         return VLC_ENOMEM;
139
140     /* FIXME pointer assignment may not be atomic */
141     swscale_fast_memcpy = vlc_memcpy;
142
143     /* Set CPU capabilities */
144     p_sys->i_cpu_mask = GetSwsCpuMask();
145
146     /* */
147     i_sws_mode = var_CreateGetInteger( p_filter, "swscale-mode" );
148     switch( i_sws_mode )
149     {
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;
162     }
163
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;
169
170     /* Misc init */
171     p_sys->ctx = NULL;
172     p_sys->ctxA = 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) );
177
178     if( Init( p_filter ) )
179     {
180         if( p_sys->p_src_filter )
181             sws_freeFilter( p_sys->p_src_filter );
182         free( p_sys );
183         return VLC_EGENERIC;
184     }
185
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] );
192
193     return VLC_SUCCESS;
194 }
195
196 /*****************************************************************************
197  * CloseFilter: clean up the filter
198  *****************************************************************************/
199 static void CloseScaler( vlc_object_t *p_this )
200 {
201     filter_t *p_filter = (filter_t*)p_this;
202     filter_sys_t *p_sys = p_filter->p_sys;
203
204     Clean( p_filter );
205     if( p_sys->p_src_filter )
206         sws_freeFilter( p_sys->p_src_filter );
207     free( p_sys );
208 }
209
210 /*****************************************************************************
211  * Helpers
212  *****************************************************************************/
213 static int GetSwsCpuMask(void)
214 {
215     const unsigned int i_cpu = vlc_CPU();
216     int i_sws_cpu = 0;
217
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;
223 #endif
224     if( i_cpu & CPU_CAPABILITY_3DNOW )
225         i_sws_cpu |= SWS_CPU_CAPS_3DNOW;
226
227     if( i_cpu & CPU_CAPABILITY_ALTIVEC )
228         i_sws_cpu |= SWS_CPU_CAPS_ALTIVEC;
229
230     return i_sws_cpu;
231 }
232 static bool IsFmtSimilar( const video_format_t *p_fmt1, const video_format_t *p_fmt2 )
233 {
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;
237 }
238
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 )
243 {
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 );
247
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 );
251
252     bool b_has_a = false;
253     int i_sws_flags = i_sws_flags_default;
254
255     if( p_fmti->i_chroma == p_fmto->i_chroma )
256     {
257         if( p_fmti->i_chroma == VLC_FOURCC( 'Y', 'U', 'V', 'P' ) && ALLOW_YUVP )
258         {
259             i_fmti = i_fmto = PIX_FMT_GRAY8;
260             i_sws_flags = SWS_POINT;
261         }
262         else if( p_fmti->i_chroma == VLC_FOURCC( 'Y', 'U', 'V', 'A' ) )
263         {
264             i_fmti = i_fmto = PIX_FMT_YUV444P;
265             b_has_a = true;
266         }
267         else if( p_fmti->i_chroma == VLC_FOURCC( 'R', 'G', 'B', 'A' ) )
268         {
269             i_fmti = i_fmto = PIX_FMT_RGBA32;
270             b_has_a = true;
271         }
272     }
273
274     if( pi_fmti )
275         *pi_fmti = i_fmti;
276     if( pi_fmto )
277         *pi_fmto = i_fmto;
278     if( pb_has_a )
279         *pb_has_a = b_has_a;
280     if( pi_sws_flags )
281         *pi_sws_flags = i_sws_flags;
282
283     if( i_fmti < 0 || i_fmto < 0 )
284         return VLC_EGENERIC;
285
286     return VLC_SUCCESS;
287 }
288
289 static int Init( filter_t *p_filter )
290 {
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;
294
295     if( IsFmtSimilar( p_fmti, &p_sys->fmt_in ) &&
296         IsFmtSimilar( p_fmto, &p_sys->fmt_out ) &&
297         p_sys->ctx )
298     {
299         return VLC_SUCCESS;
300     }
301     Clean( p_filter );
302
303     /* Init with new parameters */
304     int i_fmt_in, i_fmt_out;
305     bool b_has_a;
306     int i_sws_flags;
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 ) )
309     {
310         msg_Err( p_filter, "format not supported" );
311         return VLC_EGENERIC;
312     }
313
314     for( int n = 0; n < (b_has_a ? 2 : 1); n++ )
315     {
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;
319
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 );
324         if( n == 0 )
325             p_sys->ctx = ctx;
326         else
327             p_sys->ctxA = ctx;
328     }
329     if( p_sys->ctxA )
330     {
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 );
333     }
334
335     if( !p_sys->ctx ||
336         ( b_has_a && ( !p_sys->ctxA || !p_sys->p_src_a || !p_sys->p_dst_a ) ) )
337     {
338         msg_Err( p_filter, "could not init SwScaler and/or allocate memory" );
339         Clean( p_filter );
340         return VLC_EGENERIC;
341     }
342
343     p_sys->fmt_in  = *p_fmti;
344     p_sys->fmt_out = *p_fmto;
345
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 );
349
350
351     return VLC_SUCCESS;
352 }
353 static void Clean( filter_t *p_filter )
354 {
355     filter_sys_t *p_sys = p_filter->p_sys;
356
357     if( p_sys->p_src_a )
358         picture_Release( p_sys->p_src_a );
359     if( p_sys->p_dst_a )
360         picture_Release( p_sys->p_dst_a );
361     if( p_sys->ctxA )
362         sws_freeContext( p_sys->ctxA );
363
364     if( p_sys->ctx )
365         sws_freeContext( p_sys->ctx );
366
367     /* We have to set it to null has we call be called again :( */
368     p_sys->ctx = NULL;
369     p_sys->ctxA = NULL;
370     p_sys->p_src_a = NULL;
371     p_sys->p_dst_a = NULL;
372 }
373
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 )
377 {
378     for( int n = 0; n < __MIN(i_plane_count, p_picture->i_planes-i_plane_start ); n++ )
379     {
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;
382     }
383 }
384
385 /* XXX is it always 3 even for BIG_ENDIAN (blend.c seems to think so) ? */
386 #define OFFSET_A (3)
387 static void ExtractA( picture_t *p_dst, const picture_t *p_src, const video_format_t *p_fmt )
388 {
389     plane_t *d = &p_dst->p[0];
390     const plane_t *s = &p_src->p[0];
391     
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];
395 }
396 static void InjectA( picture_t *p_dst, const picture_t *p_src, const video_format_t *p_fmt )
397 {
398     plane_t *d = &p_dst->p[0];
399     const plane_t *s = &p_src->p[0];
400     
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];
404 }
405 #undef OFFSET_A
406
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 )
409 {
410     uint8_t *src[3]; int src_stride[3];
411     uint8_t *dst[3]; int dst_stride[3];
412
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 );
415
416 #if LIBSWSCALE_VERSION_INT  >= ((0<<16)+(5<<8)+0)
417     sws_scale( ctx, src, src_stride, 0, p_fmti->i_height,
418                dst, dst_stride );
419 #else
420     sws_scale_ordered( ctx, src, src_stride, 0, p_fmti->i_height,
421                        dst, dst_stride );
422 #endif
423 }
424
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 )
431 {
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;
436
437     /* Check if format properties changed */
438     if( Init( p_filter ) )
439     {
440         picture_Release( p_pic );
441         return NULL;
442     }
443
444     /* Request output picture */
445     p_pic_dst = filter_NewPicture( p_filter );
446     if( !p_pic_dst )
447     {
448         picture_Release( p_pic );
449         return NULL;
450     }
451
452     Convert( p_sys->ctx, p_pic_dst, p_pic, p_fmti, 0, 3 );
453     if( p_sys->ctxA )
454     {
455         if( p_fmto->i_chroma == VLC_FOURCC( 'R', 'G', 'B', 'A' ) )
456         {
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 );
459
460             Convert( p_sys->ctxA, p_sys->p_dst_a, p_sys->p_src_a, p_fmti, 0, 1 );
461
462             InjectA( p_pic_dst, p_sys->p_dst_a, p_fmto );
463         }
464         else
465         {
466             Convert( p_sys->ctx, p_pic_dst, p_pic, p_fmti, 3, 1 );
467         }
468     }
469
470     picture_CopyProperties( p_pic_dst, p_pic );
471     picture_Release( p_pic );
472     return p_pic_dst;
473 }
474
475 #else /* LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0) */
476
477 int OpenScaler( vlc_object_t *p_this )
478 {
479     return VLC_EGENERIC;
480 }
481
482 void CloseScaler( vlc_object_t *p_this )
483 {
484 }
485
486 #endif /* LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0) */