Fix ffmpeg swscaler
[vlc.git] / modules / codec / ffmpeg / scale.c
1 /*****************************************************************************
2  * filter.c: video scaling module using the swscale library
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Author: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc_codec.h>
29 #include <vlc_vout.h>
30 #include <vlc_filter.h>
31
32 /* ffmpeg headers */
33 #ifdef HAVE_FFMPEG_AVCODEC_H
34 #   include <ffmpeg/avcodec.h>
35 #else
36 #   include <avcodec.h>
37 #endif
38
39 #ifdef HAVE_FFMPEG_SWSCALE_H
40 #   include <ffmpeg/swscale.h>
41 #elif defined(HAVE_LIBSWSCALE_TREE)
42 #   include <swscale.h>
43 #endif
44
45 #include "ffmpeg.h"
46
47 /* Version checking */
48 #if ( (defined(HAVE_FFMPEG_SWSCALE_H) || defined(HAVE_LIBSWSCALE_TREE)) && (LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0)) )
49
50 /*****************************************************************************
51  * filter_sys_t : filter descriptor
52  *****************************************************************************/
53 struct filter_sys_t
54 {
55     struct SwsContext *ctx;
56     SwsFilter *p_src_filter;
57     SwsFilter *p_dst_filter;
58     int i_cpu_mask, i_sws_flags;
59
60     es_format_t fmt_in;
61     es_format_t fmt_out;
62 };
63
64 /****************************************************************************
65  * Local prototypes
66  ****************************************************************************/
67 void *( *swscale_fast_memcpy )( void *, const void *, size_t );
68 static picture_t *Filter( filter_t *, picture_t * );
69 static int CheckInit( filter_t * );
70
71 static const char *ppsz_mode_descriptions[] =
72 { N_("Fast bilinear"), N_("Bilinear"), N_("Bicubic (good quality)"),
73   N_("Experimental"), N_("Nearest neighbour (bad quality)"),
74   N_("Area"), N_("Luma bicubic / chroma bilinear"), N_("Gauss"),
75   N_("SincR"), N_("Lanczos"), N_("Bicubic spline") };
76
77 /*****************************************************************************
78  * OpenScaler: probe the filter and return score
79  *****************************************************************************/
80 int E_(OpenScaler)( vlc_object_t *p_this )
81 {
82     filter_t *p_filter = (filter_t*)p_this;
83     filter_sys_t *p_sys;
84     vlc_value_t val;
85
86     int i_fmt_in, i_fmt_out;
87     unsigned int i_cpu;
88     int i_sws_mode;
89
90     float sws_lum_gblur = 0.0, sws_chr_gblur = 0.0;
91     int sws_chr_vshift = 0, sws_chr_hshift = 0;
92     float sws_chr_sharpen = 0.0, sws_lum_sharpen = 0.0;
93
94     /* Supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR24,
95      * BGR16, BGR15, RGB32, RGB24, Y8/Y800, YVU9/IF09 */
96     i_fmt_in = E_(GetFfmpegChroma)(p_filter->fmt_in.video.i_chroma);
97     /* Supported output formats: YV12, I420/IYUV, YUY2, UYVY,
98      * {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09 */
99     i_fmt_out = E_(GetFfmpegChroma)(p_filter->fmt_out.video.i_chroma);
100
101     if( ( i_fmt_in < 0 ) || ( i_fmt_out < 0 ) )
102     {
103         return VLC_EGENERIC;
104     }
105
106     /* Allocate the memory needed to store the decoder's structure */
107     if( ( p_filter->p_sys = p_sys =
108           (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
109     {
110         msg_Err( p_filter, "out of memory" );
111         return VLC_EGENERIC;
112     }
113
114     swscale_fast_memcpy = p_filter->p_libvlc->pf_memcpy;
115
116     /* Set CPU capabilities */
117     i_cpu = vlc_CPU();
118     p_sys->i_cpu_mask = 0;
119     if( i_cpu & CPU_CAPABILITY_MMX )
120     {
121         p_sys->i_cpu_mask |= SWS_CPU_CAPS_MMX;
122     }
123     if( i_cpu & CPU_CAPABILITY_MMXEXT )
124     {
125         p_sys->i_cpu_mask |= SWS_CPU_CAPS_MMX2;
126     }
127     if( i_cpu & CPU_CAPABILITY_3DNOW )
128     {
129         p_sys->i_cpu_mask |= SWS_CPU_CAPS_3DNOW;
130     }
131     if( i_cpu & CPU_CAPABILITY_ALTIVEC )
132     {
133         p_sys->i_cpu_mask |= SWS_CPU_CAPS_ALTIVEC;
134     }
135
136     var_Create( p_filter, "swscale-mode", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
137     var_Get( p_filter, "swscale-mode", &val );
138     i_sws_mode = val.i_int;
139
140     switch( i_sws_mode )
141     {
142     case 0:  p_sys->i_sws_flags = SWS_FAST_BILINEAR; break;
143     case 1:  p_sys->i_sws_flags = SWS_BILINEAR; break;
144     case 2:  p_sys->i_sws_flags = SWS_BICUBIC; break;
145     case 3:  p_sys->i_sws_flags = SWS_X; break;
146     case 4:  p_sys->i_sws_flags = SWS_POINT; break;
147     case 5:  p_sys->i_sws_flags = SWS_AREA; break;
148     case 6:  p_sys->i_sws_flags = SWS_BICUBLIN; break;
149     case 7:  p_sys->i_sws_flags = SWS_GAUSS; break;
150     case 8:  p_sys->i_sws_flags = SWS_SINC; break;
151     case 9:  p_sys->i_sws_flags = SWS_LANCZOS; break;
152     case 10: p_sys->i_sws_flags = SWS_SPLINE; break;
153     default: p_sys->i_sws_flags = SWS_FAST_BILINEAR; i_sws_mode = 0; break;
154     }
155
156     p_sys->p_src_filter = NULL;
157     p_sys->p_dst_filter = NULL;
158     p_sys->p_src_filter =
159         sws_getDefaultFilter( sws_lum_gblur, sws_chr_gblur,
160                               sws_lum_sharpen, sws_chr_sharpen,
161                               sws_chr_hshift, sws_chr_vshift, 0 );
162
163     /* Misc init */
164     p_sys->ctx = NULL;
165     p_filter->pf_video_filter = Filter;
166     es_format_Init( &p_sys->fmt_in, 0, 0 );
167     es_format_Init( &p_sys->fmt_out, 0, 0 );
168
169     if( CheckInit( p_filter ) != VLC_SUCCESS )
170     {
171         if( p_sys->p_src_filter ) sws_freeFilter( p_sys->p_src_filter );
172         free( p_sys );
173         return VLC_EGENERIC;
174     }
175
176     msg_Dbg( p_filter, "%ix%i chroma: %4.4s -> %ix%i chroma: %4.4s",
177              p_filter->fmt_in.video.i_width, p_filter->fmt_in.video.i_height,
178              (char *)&p_filter->fmt_in.video.i_chroma,
179              p_filter->fmt_out.video.i_width, p_filter->fmt_out.video.i_height,
180              (char *)&p_filter->fmt_out.video.i_chroma );
181
182     if( p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width ||
183         p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height )
184     {
185         msg_Dbg( p_filter, "scaling mode: %s",
186                  ppsz_mode_descriptions[i_sws_mode] );
187     }
188
189     return VLC_SUCCESS;
190 }
191
192 /*****************************************************************************
193  * CloseFilter: clean up the filter
194  *****************************************************************************/
195 void E_(CloseScaler)( vlc_object_t *p_this )
196 {
197     filter_t *p_filter = (filter_t*)p_this;
198     filter_sys_t *p_sys = p_filter->p_sys;
199
200     if( p_sys->ctx ) sws_freeContext( p_sys->ctx );
201     if( p_sys->p_src_filter ) sws_freeFilter( p_sys->p_src_filter );
202     free( p_sys );
203 }
204
205 /*****************************************************************************
206  * CheckInit: Initialise filter when necessary
207  *****************************************************************************/
208 static int CheckInit( filter_t *p_filter )
209 {
210     filter_sys_t *p_sys = p_filter->p_sys;
211
212     if( ( p_filter->fmt_in.video.i_width != p_sys->fmt_in.video.i_width ) ||
213         ( p_filter->fmt_in.video.i_height != p_sys->fmt_in.video.i_height ) ||
214         ( p_filter->fmt_out.video.i_width != p_sys->fmt_out.video.i_width ) ||
215         ( p_filter->fmt_out.video.i_height != p_sys->fmt_out.video.i_height ) )
216     {
217         int i_fmt_in, i_fmt_out;
218
219         i_fmt_in = E_(GetFfmpegChroma)(p_filter->fmt_in.video.i_chroma);
220         i_fmt_out = E_(GetFfmpegChroma)(p_filter->fmt_out.video.i_chroma);
221         if( (i_fmt_in < 0 ) || ( i_fmt_out < 0 ) )
222         {
223             msg_Err( p_filter, "format not supported" );
224             return VLC_EGENERIC;
225         }
226
227         if( p_sys->ctx ) sws_freeContext( p_sys->ctx );
228
229         p_sys->ctx =
230             sws_getContext( p_filter->fmt_in.video.i_width,
231                             p_filter->fmt_in.video.i_height, i_fmt_in,
232                             p_filter->fmt_out.video.i_width,
233                             p_filter->fmt_out.video.i_height, i_fmt_out,
234                             p_sys->i_sws_flags | p_sys->i_cpu_mask,
235                             p_sys->p_src_filter, p_sys->p_dst_filter, 0 );
236         if( !p_sys->ctx )
237         {
238             msg_Err( p_filter, "could not init SwScaler" );
239             return VLC_EGENERIC;
240         }
241
242         p_sys->fmt_in = p_filter->fmt_in;
243         p_sys->fmt_out = p_filter->fmt_out;
244     }
245
246     return VLC_SUCCESS;
247 }
248
249 /****************************************************************************
250  * Filter: the whole thing
251  ****************************************************************************
252  * This function is called just after the thread is launched.
253  ****************************************************************************/
254 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
255 {
256     filter_sys_t *p_sys = p_filter->p_sys;
257     uint8_t *src[3]; int src_stride[3];
258     uint8_t *dst[3]; int dst_stride[3];
259     picture_t *p_pic_dst;
260     int i_plane;
261     int i_nb_planes = p_pic->i_planes;
262
263     /* Check if format properties changed */
264     if( CheckInit( p_filter ) != VLC_SUCCESS ) return NULL;
265
266     /* Request output picture */
267     p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
268     if( !p_pic_dst )
269     {
270         msg_Warn( p_filter, "can't get output picture" );
271         return NULL;
272     }
273
274     if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','V','A') )
275     {
276         i_nb_planes = 3;
277         memset( p_pic_dst->p[3].p_pixels, 0xff, p_filter->fmt_out.video.i_height
278                                                  * p_pic_dst->p[3].i_pitch );
279     }
280
281     for( i_plane = 0; i_plane < __MIN(3, p_pic->i_planes); i_plane++ )
282     {
283         src[i_plane] = p_pic->p[i_plane].p_pixels;
284         src_stride[i_plane] = p_pic->p[i_plane].i_pitch;
285     }
286     for( i_plane = 0; i_plane < __MIN(3, i_nb_planes); i_plane++ )
287     {
288         dst[i_plane] = p_pic_dst->p[i_plane].p_pixels;
289         dst_stride[i_plane] = p_pic_dst->p[i_plane].i_pitch;
290     }
291
292 #if LIBSWSCALE_VERSION_INT  >= ((0<<16)+(5<<8)+0)
293     sws_scale( p_sys->ctx, src, src_stride,
294                0, p_filter->fmt_in.video.i_height,
295                dst, dst_stride );
296 #else
297     sws_scale_ordered( p_sys->ctx, src, src_stride,
298                0, p_filter->fmt_in.video.i_height,
299                dst, dst_stride );
300 #endif
301
302     p_pic_dst->date = p_pic->date;
303     p_pic_dst->b_force = p_pic->b_force;
304     p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
305     p_pic_dst->b_progressive = p_pic->b_progressive;
306     p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
307
308     p_pic->pf_release( p_pic );
309     return p_pic_dst;
310 }
311
312 #else /* LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0) */
313
314 int E_(OpenScaler)( vlc_object_t *p_this )
315 {
316     return VLC_EGENERIC;
317 }
318
319 void E_(CloseScaler)( vlc_object_t *p_this )
320 {
321 }
322
323 #endif /* LIBSWSCALE_VERSION_INT >= ((0<<16)+(5<<8)+0) */