]> git.sesse.net Git - vlc/blob - modules/video_filter/scale.c
Merge branch 1.0-bugfix
[vlc] / modules / video_filter / scale.c
1 /*****************************************************************************
2  * resize.c: video scaling module for YUVP/A, I420 and RGBA pictures
3  *  Uses the low quality "nearest neighbour" algorithm.
4  *****************************************************************************
5  * Copyright (C) 2003-2007 the VideoLAN team
6  * $Id$
7  *
8  * Authors: Gildas Bazin <gbazin@videolan.org>
9  *          Antoine Cellerier <dionoea @t videolan dot org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include "vlc_filter.h"
36
37 /*****************************************************************************
38  * filter_sys_t : filter descriptor
39  *****************************************************************************/
40 struct filter_sys_t
41 {
42     es_format_t fmt_in;
43     es_format_t fmt_out;
44 };
45
46 /****************************************************************************
47  * Local prototypes
48  ****************************************************************************/
49 static int  OpenFilter ( vlc_object_t * );
50 static void CloseFilter( vlc_object_t * );
51
52 static picture_t *Filter( filter_t *, picture_t * );
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 vlc_module_begin ()
58     set_description( N_("Video scaling filter") )
59     set_capability( "video filter2", 10 )
60     set_callbacks( OpenFilter, CloseFilter )
61 vlc_module_end ()
62
63 /*****************************************************************************
64  * OpenFilter: probe the filter and return score
65  *****************************************************************************/
66 static int OpenFilter( vlc_object_t *p_this )
67 {
68     filter_t *p_filter = (filter_t*)p_this;
69     filter_sys_t *p_sys;
70
71     if( ( p_filter->fmt_in.video.i_chroma != VLC_CODEC_YUVP &&
72           p_filter->fmt_in.video.i_chroma != VLC_CODEC_YUVA &&
73           p_filter->fmt_in.video.i_chroma != VLC_CODEC_I420 &&
74           p_filter->fmt_in.video.i_chroma != VLC_CODEC_YV12 &&
75           p_filter->fmt_in.video.i_chroma != VLC_CODEC_RGB32 &&
76           p_filter->fmt_in.video.i_chroma != VLC_CODEC_RGBA ) ||
77         p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
78     {
79         return VLC_EGENERIC;
80     }
81
82     /* Allocate the memory needed to store the decoder's structure */
83     if( ( p_filter->p_sys = p_sys =
84           (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
85         return VLC_ENOMEM;
86
87     p_filter->pf_video_filter = Filter;
88
89     msg_Dbg( p_filter, "%ix%i -> %ix%i", p_filter->fmt_in.video.i_width,
90              p_filter->fmt_in.video.i_height, p_filter->fmt_out.video.i_width,
91              p_filter->fmt_out.video.i_height );
92
93     return VLC_SUCCESS;
94 }
95
96 /*****************************************************************************
97  * CloseFilter: clean up the filter
98  *****************************************************************************/
99 static void CloseFilter( vlc_object_t *p_this )
100 {
101     filter_t *p_filter = (filter_t*)p_this;
102     filter_sys_t *p_sys = p_filter->p_sys;
103
104     free( p_sys );
105 }
106
107 /****************************************************************************
108  * Filter: the whole thing
109  ****************************************************************************/
110 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
111 {
112     picture_t *p_pic_dst;
113     int i_plane;
114
115     if( !p_pic ) return NULL;
116
117     if( (p_filter->fmt_in.video.i_height == 0) ||
118         (p_filter->fmt_in.video.i_width == 0) )
119         return NULL;
120
121     if( (p_filter->fmt_out.video.i_height == 0) ||
122         (p_filter->fmt_out.video.i_width == 0) )
123         return NULL;
124
125     /* Request output picture */
126     p_pic_dst = filter_NewPicture( p_filter );
127     if( !p_pic_dst )
128     {
129         picture_Release( p_pic );
130         return NULL;
131     }
132
133     if( p_filter->fmt_in.video.i_chroma != VLC_CODEC_RGBA &&
134         p_filter->fmt_in.video.i_chroma != VLC_CODEC_RGB32 )
135     {
136         for( i_plane = 0; i_plane < p_pic_dst->i_planes; i_plane++ )
137         {
138             const int i_src_pitch    = p_pic->p[i_plane].i_pitch;
139             const int i_dst_pitch    = p_pic_dst->p[i_plane].i_pitch;
140             const int i_src_height   = p_filter->fmt_in.video.i_height;
141             const int i_src_width    = p_filter->fmt_in.video.i_width;
142             const int i_dst_height   = p_filter->fmt_out.video.i_height;
143             const int i_dst_width    = p_filter->fmt_out.video.i_width;
144             const int i_dst_visible_lines =
145                                        p_pic_dst->p[i_plane].i_visible_lines;
146             const int i_dst_visible_pitch =
147                                        p_pic_dst->p[i_plane].i_visible_pitch;
148             const int i_dst_hidden_pitch  = i_dst_pitch - i_dst_visible_pitch;
149 #define SHIFT_SIZE 16
150             const int i_height_coef  = ( i_src_height << SHIFT_SIZE )
151                                        / i_dst_height;
152             const int i_width_coef   = ( i_src_width << SHIFT_SIZE )
153                                        / i_dst_width;
154             const int i_src_height_1 = i_src_height - 1;
155             const int i_src_width_1  = i_src_width - 1;
156
157             uint8_t *p_src = p_pic->p[i_plane].p_pixels;
158             uint8_t *p_dst = p_pic_dst->p[i_plane].p_pixels;
159             uint8_t *p_dstendline = p_dst + i_dst_visible_pitch;
160             const uint8_t *p_dstend = p_dst + i_dst_visible_lines*i_dst_pitch;
161
162             const int i_shift_height = i_dst_height / i_src_height;
163             const int i_shift_width = i_dst_width / i_src_width;
164
165             int l = 1<<(SHIFT_SIZE-i_shift_height);
166             for( ; p_dst < p_dstend;
167                  p_dst += i_dst_hidden_pitch,
168                  p_dstendline += i_dst_pitch, l += i_height_coef )
169             {
170                 int k = 1<<(SHIFT_SIZE-i_shift_width);
171                 uint8_t *p_srcl = p_src
172                        + (__MIN( i_src_height_1, l >> SHIFT_SIZE )*i_src_pitch);
173
174                 for( ; p_dst < p_dstendline; p_dst++, k += i_width_coef )
175                 {
176                     *p_dst = p_srcl[__MIN( i_src_width_1, k >> SHIFT_SIZE )];
177                 }
178             }
179         }
180     }
181     else /* RGBA */
182     {
183         const int i_src_pitch = p_pic->p->i_pitch;
184         const int i_dst_pitch = p_pic_dst->p->i_pitch;
185         const int i_src_height   = p_filter->fmt_in.video.i_height;
186         const int i_src_width    = p_filter->fmt_in.video.i_width;
187         const int i_dst_height   = p_filter->fmt_out.video.i_height;
188         const int i_dst_width    = p_filter->fmt_out.video.i_width;
189         const int i_dst_visible_lines =
190                                    p_pic_dst->p->i_visible_lines;
191         const int i_dst_visible_pitch =
192                                    p_pic_dst->p->i_visible_pitch;
193         const int i_dst_hidden_pitch  = i_dst_pitch - i_dst_visible_pitch;
194         const int i_height_coef  = ( i_src_height << SHIFT_SIZE )
195                                    / i_dst_height;
196         const int i_width_coef   = ( i_src_width << SHIFT_SIZE )
197                                    / i_dst_width;
198         const int i_src_height_1 = i_src_height - 1;
199         const int i_src_width_1  = i_src_width - 1;
200
201         uint32_t *p_src = (uint32_t*)p_pic->p->p_pixels;
202         uint32_t *p_dst = (uint32_t*)p_pic_dst->p->p_pixels;
203         uint32_t *p_dstendline = p_dst + (i_dst_visible_pitch>>2);
204         const uint32_t *p_dstend = p_dst + i_dst_visible_lines*(i_dst_pitch>>2);
205
206         const int i_shift_height = i_dst_height / i_src_height;
207         const int i_shift_width = i_dst_width / i_src_width;
208
209         int l = 1<<(SHIFT_SIZE-i_shift_height);
210         for( ; p_dst < p_dstend;
211              p_dst += (i_dst_hidden_pitch>>2),
212              p_dstendline += (i_dst_pitch>>2),
213              l += i_height_coef )
214         {
215             int k = 1<<(SHIFT_SIZE-i_shift_width);
216             uint32_t *p_srcl = p_src
217                     + (__MIN( i_src_height_1, l >> SHIFT_SIZE )*(i_src_pitch>>2));
218             for( ; p_dst < p_dstendline; p_dst++, k += i_width_coef )
219             {
220                 *p_dst = p_srcl[__MIN( i_src_width_1, k >> SHIFT_SIZE )];
221             }
222         }
223     }
224
225     picture_CopyProperties( p_pic_dst, p_pic );
226     picture_Release( p_pic );
227     return p_pic_dst;
228 }