0f72c5bd24bb0504e9e772be6f563aa618f63203
[vlc.git] / 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  * Local prototypes
39  ****************************************************************************/
40 static int  OpenFilter ( vlc_object_t * );
41 static picture_t *Filter( filter_t *, picture_t * );
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 vlc_module_begin ()
47     set_description( N_("Video scaling filter") )
48     set_capability( "video filter2", 10 )
49     set_callbacks( OpenFilter, NULL )
50 vlc_module_end ()
51
52 /*****************************************************************************
53  * OpenFilter: probe the filter and return score
54  *****************************************************************************/
55 static int OpenFilter( vlc_object_t *p_this )
56 {
57     filter_t *p_filter = (filter_t*)p_this;
58
59     if( ( p_filter->fmt_in.video.i_chroma != VLC_CODEC_YUVP &&
60           p_filter->fmt_in.video.i_chroma != VLC_CODEC_YUVA &&
61           p_filter->fmt_in.video.i_chroma != VLC_CODEC_I420 &&
62           p_filter->fmt_in.video.i_chroma != VLC_CODEC_YV12 &&
63           p_filter->fmt_in.video.i_chroma != VLC_CODEC_RGB32 &&
64           p_filter->fmt_in.video.i_chroma != VLC_CODEC_RGBA ) ||
65         p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
66     {
67         return VLC_EGENERIC;
68     }
69
70     p_filter->pf_video_filter = Filter;
71
72     msg_Dbg( p_filter, "%ix%i -> %ix%i", p_filter->fmt_in.video.i_width,
73              p_filter->fmt_in.video.i_height, p_filter->fmt_out.video.i_width,
74              p_filter->fmt_out.video.i_height );
75
76     return VLC_SUCCESS;
77 }
78
79 /****************************************************************************
80  * Filter: the whole thing
81  ****************************************************************************/
82 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
83 {
84     picture_t *p_pic_dst;
85     int i_plane;
86
87     if( !p_pic ) return NULL;
88
89     if( (p_filter->fmt_in.video.i_height == 0) ||
90         (p_filter->fmt_in.video.i_width == 0) )
91         return NULL;
92
93     if( (p_filter->fmt_out.video.i_height == 0) ||
94         (p_filter->fmt_out.video.i_width == 0) )
95         return NULL;
96
97     /* Request output picture */
98     p_pic_dst = filter_NewPicture( p_filter );
99     if( !p_pic_dst )
100     {
101         picture_Release( p_pic );
102         return NULL;
103     }
104
105     if( p_filter->fmt_in.video.i_chroma != VLC_CODEC_RGBA &&
106         p_filter->fmt_in.video.i_chroma != VLC_CODEC_RGB32 )
107     {
108         for( i_plane = 0; i_plane < p_pic_dst->i_planes; i_plane++ )
109         {
110             const int i_src_pitch    = p_pic->p[i_plane].i_pitch;
111             const int i_dst_pitch    = p_pic_dst->p[i_plane].i_pitch;
112             const int i_src_height   = p_filter->fmt_in.video.i_height;
113             const int i_src_width    = p_filter->fmt_in.video.i_width;
114             const int i_dst_height   = p_filter->fmt_out.video.i_height;
115             const int i_dst_width    = p_filter->fmt_out.video.i_width;
116             const int i_dst_visible_lines =
117                                        p_pic_dst->p[i_plane].i_visible_lines;
118             const int i_dst_visible_pitch =
119                                        p_pic_dst->p[i_plane].i_visible_pitch;
120             const int i_dst_hidden_pitch  = i_dst_pitch - i_dst_visible_pitch;
121 #define SHIFT_SIZE 16
122             const int i_height_coef  = ( i_src_height << SHIFT_SIZE )
123                                        / i_dst_height;
124             const int i_width_coef   = ( i_src_width << SHIFT_SIZE )
125                                        / i_dst_width;
126             const int i_src_height_1 = i_src_height - 1;
127             const int i_src_width_1  = i_src_width - 1;
128
129             uint8_t *p_src = p_pic->p[i_plane].p_pixels;
130             uint8_t *p_dst = p_pic_dst->p[i_plane].p_pixels;
131             uint8_t *p_dstendline = p_dst + i_dst_visible_pitch;
132             const uint8_t *p_dstend = p_dst + i_dst_visible_lines*i_dst_pitch;
133
134             const int i_shift_height = i_dst_height / i_src_height;
135             const int i_shift_width = i_dst_width / i_src_width;
136
137             int l = 1<<(SHIFT_SIZE-i_shift_height);
138             for( ; p_dst < p_dstend;
139                  p_dst += i_dst_hidden_pitch,
140                  p_dstendline += i_dst_pitch, l += i_height_coef )
141             {
142                 int k = 1<<(SHIFT_SIZE-i_shift_width);
143                 uint8_t *p_srcl = p_src
144                        + (__MIN( i_src_height_1, l >> SHIFT_SIZE )*i_src_pitch);
145
146                 for( ; p_dst < p_dstendline; p_dst++, k += i_width_coef )
147                 {
148                     *p_dst = p_srcl[__MIN( i_src_width_1, k >> SHIFT_SIZE )];
149                 }
150             }
151         }
152     }
153     else /* RGBA */
154     {
155         const int i_src_pitch = p_pic->p->i_pitch;
156         const int i_dst_pitch = p_pic_dst->p->i_pitch;
157         const int i_src_height   = p_filter->fmt_in.video.i_height;
158         const int i_src_width    = p_filter->fmt_in.video.i_width;
159         const int i_dst_height   = p_filter->fmt_out.video.i_height;
160         const int i_dst_width    = p_filter->fmt_out.video.i_width;
161         const int i_dst_visible_lines =
162                                    p_pic_dst->p->i_visible_lines;
163         const int i_dst_visible_pitch =
164                                    p_pic_dst->p->i_visible_pitch;
165         const int i_dst_hidden_pitch  = i_dst_pitch - i_dst_visible_pitch;
166         const int i_height_coef  = ( i_src_height << SHIFT_SIZE )
167                                    / i_dst_height;
168         const int i_width_coef   = ( i_src_width << SHIFT_SIZE )
169                                    / i_dst_width;
170         const int i_src_height_1 = i_src_height - 1;
171         const int i_src_width_1  = i_src_width - 1;
172
173         uint32_t *p_src = (uint32_t*)p_pic->p->p_pixels;
174         uint32_t *p_dst = (uint32_t*)p_pic_dst->p->p_pixels;
175         uint32_t *p_dstendline = p_dst + (i_dst_visible_pitch>>2);
176         const uint32_t *p_dstend = p_dst + i_dst_visible_lines*(i_dst_pitch>>2);
177
178         const int i_shift_height = i_dst_height / i_src_height;
179         const int i_shift_width = i_dst_width / i_src_width;
180
181         int l = 1<<(SHIFT_SIZE-i_shift_height);
182         for( ; p_dst < p_dstend;
183              p_dst += (i_dst_hidden_pitch>>2),
184              p_dstendline += (i_dst_pitch>>2),
185              l += i_height_coef )
186         {
187             int k = 1<<(SHIFT_SIZE-i_shift_width);
188             uint32_t *p_srcl = p_src
189                     + (__MIN( i_src_height_1, l >> SHIFT_SIZE )*(i_src_pitch>>2));
190             for( ; p_dst < p_dstendline; p_dst++, k += i_width_coef )
191             {
192                 *p_dst = p_srcl[__MIN( i_src_width_1, k >> SHIFT_SIZE )];
193             }
194         }
195     }
196
197     picture_CopyProperties( p_pic_dst, p_pic );
198     picture_Release( p_pic );
199     return p_pic_dst;
200 }