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