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