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