]> git.sesse.net Git - vlc/blob - modules/video_filter/scale.c
01033b8d00e4112357091411161c0b10cbc1a9e9
[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_vout.h>
36 #include "vlc_filter.h"
37
38 /*****************************************************************************
39  * filter_sys_t : filter descriptor
40  *****************************************************************************/
41 struct filter_sys_t
42 {
43     es_format_t fmt_in;
44     es_format_t fmt_out;
45 };
46
47 /****************************************************************************
48  * Local prototypes
49  ****************************************************************************/
50 static int  OpenFilter ( vlc_object_t * );
51 static void CloseFilter( vlc_object_t * );
52
53 static picture_t *Filter( filter_t *, picture_t * );
54
55 /*****************************************************************************
56  * Module descriptor
57  *****************************************************************************/
58 vlc_module_begin();
59     set_description( N_("Video scaling filter") );
60     set_capability( "video filter2", 10 );
61     set_callbacks( OpenFilter, CloseFilter );
62 vlc_module_end();
63
64 /*****************************************************************************
65  * OpenFilter: probe the filter and return score
66  *****************************************************************************/
67 static int OpenFilter( vlc_object_t *p_this )
68 {
69     filter_t *p_filter = (filter_t*)p_this;
70     filter_sys_t *p_sys;
71
72     if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','P') &&
73           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','A') &&
74           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
75           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2') &&
76           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('R','V','3','2') &&
77           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('R','G','B','A') ) ||
78         p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
79     {
80         return VLC_EGENERIC;
81     }
82
83     /* Allocate the memory needed to store the decoder's structure */
84     if( ( p_filter->p_sys = p_sys =
85           (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
86         return VLC_ENOMEM;
87
88     p_filter->pf_video_filter = Filter;
89
90     msg_Dbg( p_filter, "%ix%i -> %ix%i", p_filter->fmt_in.video.i_width,
91              p_filter->fmt_in.video.i_height, p_filter->fmt_out.video.i_width,
92              p_filter->fmt_out.video.i_height );
93
94     return VLC_SUCCESS;
95 }
96
97 /*****************************************************************************
98  * CloseFilter: clean up the filter
99  *****************************************************************************/
100 static void CloseFilter( vlc_object_t *p_this )
101 {
102     filter_t *p_filter = (filter_t*)p_this;
103     filter_sys_t *p_sys = p_filter->p_sys;
104
105     free( p_sys );
106 }
107
108 /****************************************************************************
109  * Filter: the whole thing
110  ****************************************************************************/
111 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
112 {
113     picture_t *p_pic_dst;
114     int i_plane;
115
116     if( !p_pic ) return NULL;
117
118     if( (p_filter->fmt_in.video.i_height == 0) ||
119         (p_filter->fmt_in.video.i_width == 0) )
120         return NULL;
121
122     if( (p_filter->fmt_out.video.i_height == 0) ||
123         (p_filter->fmt_out.video.i_width == 0) )
124         return NULL;
125
126     /* Request output picture */
127     p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
128     if( !p_pic_dst )
129     {
130         msg_Warn( p_filter, "can't get output picture" );
131         picture_Release( p_pic );
132         return NULL;
133     }
134
135     if( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('R','G','B','A') &&
136         p_filter->fmt_in.video.i_chroma != VLC_FOURCC('R','V','3','2') )
137     {
138         for( i_plane = 0; i_plane < p_pic_dst->i_planes; i_plane++ )
139         {
140             const int i_src_pitch    = p_pic->p[i_plane].i_pitch;
141             const int i_dst_pitch    = p_pic_dst->p[i_plane].i_pitch;
142             const int i_src_height   = p_filter->fmt_in.video.i_height;
143             const int i_src_width    = p_filter->fmt_in.video.i_width;
144             const int i_dst_height   = p_filter->fmt_out.video.i_height;
145             const int i_dst_width    = p_filter->fmt_out.video.i_width;
146             const int i_dst_visible_lines =
147                                        p_pic_dst->p[i_plane].i_visible_lines;
148             const int i_dst_visible_pitch =
149                                        p_pic_dst->p[i_plane].i_visible_pitch;
150             const int i_dst_hidden_pitch  = i_dst_pitch - i_dst_visible_pitch;
151 #define SHIFT_SIZE 16
152             const int i_height_coef  = ( i_src_height << SHIFT_SIZE )
153                                        / i_dst_height;
154             const int i_width_coef   = ( i_src_width << SHIFT_SIZE )
155                                        / i_dst_width;
156             const int i_src_height_1 = i_src_height - 1;
157             const int i_src_width_1  = i_src_width - 1;
158
159             uint8_t *p_src = p_pic->p[i_plane].p_pixels;
160             uint8_t *p_dst = p_pic_dst->p[i_plane].p_pixels;
161             uint8_t *p_dstendline = p_dst + i_dst_visible_pitch;
162             const uint8_t *p_dstend = p_dst + i_dst_visible_lines*i_dst_pitch;
163
164             const int i_shift_height = i_dst_height / i_src_height;
165             const int i_shift_width = i_dst_width / i_src_width;
166
167             int l = 1<<(SHIFT_SIZE-i_shift_height);
168             for( ; p_dst < p_dstend;
169                  p_dst += i_dst_hidden_pitch,
170                  p_dstendline += i_dst_pitch, l += i_height_coef )
171             {
172                 int k = 1<<(SHIFT_SIZE-i_shift_width);
173                 uint8_t *p_srcl = p_src
174                        + (__MIN( i_src_height_1, l >> SHIFT_SIZE )*i_src_pitch);
175
176                 for( ; p_dst < p_dstendline; p_dst++, k += i_width_coef )
177                 {
178                     *p_dst = p_srcl[__MIN( i_src_width_1, k >> SHIFT_SIZE )];
179                 }
180             }
181         }
182     }
183     else /* RGBA */
184     {
185         const int i_src_pitch = p_pic->p->i_pitch;
186         const int i_dst_pitch = p_pic_dst->p->i_pitch;
187         const int i_src_height   = p_filter->fmt_in.video.i_height;
188         const int i_src_width    = p_filter->fmt_in.video.i_width;
189         const int i_dst_height   = p_filter->fmt_out.video.i_height;
190         const int i_dst_width    = p_filter->fmt_out.video.i_width;
191         const int i_dst_visible_lines =
192                                    p_pic_dst->p->i_visible_lines;
193         const int i_dst_visible_pitch =
194                                    p_pic_dst->p->i_visible_pitch;
195         const int i_dst_hidden_pitch  = i_dst_pitch - i_dst_visible_pitch;
196         const int i_height_coef  = ( i_src_height << SHIFT_SIZE )
197                                    / i_dst_height;
198         const int i_width_coef   = ( i_src_width << SHIFT_SIZE )
199                                    / i_dst_width;
200         const int i_src_height_1 = i_src_height - 1;
201         const int i_src_width_1  = i_src_width - 1;
202
203         uint32_t *p_src = (uint32_t*)p_pic->p->p_pixels;
204         uint32_t *p_dst = (uint32_t*)p_pic_dst->p->p_pixels;
205         uint32_t *p_dstendline = p_dst + (i_dst_visible_pitch>>2);
206         const uint32_t *p_dstend = p_dst + i_dst_visible_lines*(i_dst_pitch>>2);
207
208         const int i_shift_height = i_dst_height / i_src_height;
209         const int i_shift_width = i_dst_width / i_src_width;
210
211         int l = 1<<(SHIFT_SIZE-i_shift_height);
212         for( ; p_dst < p_dstend;
213              p_dst += (i_dst_hidden_pitch>>2),
214              p_dstendline += (i_dst_pitch>>2),
215              l += i_height_coef )
216         {
217             int k = 1<<(SHIFT_SIZE-i_shift_width);
218             uint32_t *p_srcl = p_src
219                     + (__MIN( i_src_height_1, l >> SHIFT_SIZE )*(i_src_pitch>>2));
220             for( ; p_dst < p_dstendline; p_dst++, k += i_width_coef )
221             {
222                 *p_dst = p_srcl[__MIN( i_src_width_1, k >> SHIFT_SIZE )];
223             }
224         }
225     }
226
227     picture_CopyProperties( p_pic_dst, p_pic );
228     picture_Release( p_pic );
229     return p_pic_dst;
230 }