b3836b2ab85791d47683a136313be7430523d841
[vlc.git] / modules / video_filter / scale.c
1 /*****************************************************************************
2  * scale.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 VLC authors and VideoLAN
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          Antoine Cellerier <dionoea @t videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_filter.h>
35 #include <vlc_picture.h>
36
37 /****************************************************************************
38  * Local prototypes
39  ****************************************************************************/
40 static int  OpenFilter ( filter_t * );
41 VIDEO_FILTER_WRAPPER(Filter)
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 vlc_module_begin ()
47     set_description( N_("Video scaling filter") )
48     set_callback_video_converter( OpenFilter, 10 )
49 vlc_module_end ()
50
51 /*****************************************************************************
52  * OpenFilter: probe the filter and return score
53  *****************************************************************************/
54 static int OpenFilter( filter_t *p_filter )
55 {
56     if( ( p_filter->fmt_in.video.i_chroma != VLC_CODEC_YUVP &&
57           p_filter->fmt_in.video.i_chroma != VLC_CODEC_YUVA &&
58           p_filter->fmt_in.video.i_chroma != VLC_CODEC_I420 &&
59           p_filter->fmt_in.video.i_chroma != VLC_CODEC_YV12 &&
60           p_filter->fmt_in.video.i_chroma != VLC_CODEC_RGB32 &&
61           p_filter->fmt_in.video.i_chroma != VLC_CODEC_RGBA &&
62           p_filter->fmt_in.video.i_chroma != VLC_CODEC_ARGB &&
63           p_filter->fmt_in.video.i_chroma != VLC_CODEC_BGRA ) ||
64         p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
65     {
66         return VLC_EGENERIC;
67     }
68
69     if( p_filter->fmt_in.video.orientation != p_filter->fmt_out.video.orientation )
70         return VLC_EGENERIC;
71
72 #warning Converter cannot (really) change output format.
73     video_format_ScaleCropAr( &p_filter->fmt_out.video, &p_filter->fmt_in.video );
74
75     p_filter->ops = &Filter_ops;
76
77     msg_Dbg( p_filter, "%ix%i -> %ix%i", p_filter->fmt_in.video.i_width,
78              p_filter->fmt_in.video.i_height, p_filter->fmt_out.video.i_width,
79              p_filter->fmt_out.video.i_height );
80
81     return VLC_SUCCESS;
82 }
83
84 /****************************************************************************
85  * Filter: the whole thing
86  ****************************************************************************/
87 static void Filter( filter_t *p_filter, picture_t *p_pic, picture_t *p_pic_dst )
88 {
89 #warning Converter cannot (really) change output format.
90     video_format_ScaleCropAr( &p_filter->fmt_out.video, &p_filter->fmt_in.video );
91
92     if( p_filter->fmt_in.video.i_chroma != VLC_CODEC_RGBA &&
93         p_filter->fmt_in.video.i_chroma != VLC_CODEC_ARGB &&
94         p_filter->fmt_in.video.i_chroma != VLC_CODEC_BGRA &&
95         p_filter->fmt_in.video.i_chroma != VLC_CODEC_RGB32 )
96     {
97         for( int i_plane = 0; i_plane < p_pic_dst->i_planes; i_plane++ )
98         {
99             const int i_src_pitch    = p_pic->p[i_plane].i_pitch;
100             const int i_dst_pitch    = p_pic_dst->p[i_plane].i_pitch;
101             const int i_src_height   = p_filter->fmt_in.video.i_height;
102             const int i_src_width    = p_filter->fmt_in.video.i_width;
103             const int i_dst_height   = p_filter->fmt_out.video.i_height;
104             const int i_dst_width    = p_filter->fmt_out.video.i_width;
105             const int i_dst_visible_lines =
106                                        p_pic_dst->p[i_plane].i_visible_lines;
107             const int i_dst_visible_pitch =
108                                        p_pic_dst->p[i_plane].i_visible_pitch;
109             const int i_dst_hidden_pitch  = i_dst_pitch - i_dst_visible_pitch;
110 #define SHIFT_SIZE 16
111             const int i_height_coef  = ( i_src_height << SHIFT_SIZE )
112                                        / i_dst_height;
113             const int i_width_coef   = ( i_src_width << SHIFT_SIZE )
114                                        / i_dst_width;
115             const int i_src_height_1 = i_src_height - 1;
116             const int i_src_width_1  = i_src_width - 1;
117
118             uint8_t *p_src = p_pic->p[i_plane].p_pixels;
119             uint8_t *p_dst = p_pic_dst->p[i_plane].p_pixels;
120             uint8_t *p_dstendline = p_dst + i_dst_visible_pitch;
121             const uint8_t *p_dstend = p_dst + i_dst_visible_lines*i_dst_pitch;
122
123             const int i_shift_height = i_dst_height / i_src_height;
124             const int i_shift_width = i_dst_width / i_src_width;
125
126             int l = 1<<(SHIFT_SIZE-i_shift_height);
127             for( ; p_dst < p_dstend;
128                  p_dst += i_dst_hidden_pitch,
129                  p_dstendline += i_dst_pitch, l += i_height_coef )
130             {
131                 int k = 1<<(SHIFT_SIZE-i_shift_width);
132                 uint8_t *p_srcl = p_src
133                        + (__MIN( i_src_height_1, l >> SHIFT_SIZE )*i_src_pitch);
134
135                 for( ; p_dst < p_dstendline; p_dst++, k += i_width_coef )
136                 {
137                     *p_dst = p_srcl[__MIN( i_src_width_1, k >> SHIFT_SIZE )];
138                 }
139             }
140         }
141     }
142     else /* RGBA */
143     {
144         const int i_src_pitch = p_pic->p->i_pitch;
145         const int i_dst_pitch = p_pic_dst->p->i_pitch;
146         const int i_src_height   = p_filter->fmt_in.video.i_height;
147         const int i_src_width    = p_filter->fmt_in.video.i_width;
148         const int i_dst_height   = p_filter->fmt_out.video.i_height;
149         const int i_dst_width    = p_filter->fmt_out.video.i_width;
150         const int i_dst_visible_lines =
151                                    p_pic_dst->p->i_visible_lines;
152         const int i_dst_visible_pitch =
153                                    p_pic_dst->p->i_visible_pitch;
154         const int i_dst_hidden_pitch  = i_dst_pitch - i_dst_visible_pitch;
155         const int i_height_coef  = ( i_src_height << SHIFT_SIZE )
156                                    / i_dst_height;
157         const int i_width_coef   = ( i_src_width << SHIFT_SIZE )
158                                    / i_dst_width;
159         const int i_src_height_1 = i_src_height - 1;
160         const int i_src_width_1  = i_src_width - 1;
161
162         uint32_t *p_src = (uint32_t*)p_pic->p->p_pixels;
163         uint32_t *p_dst = (uint32_t*)p_pic_dst->p->p_pixels;
164         uint32_t *p_dstendline = p_dst + (i_dst_visible_pitch>>2);
165         const uint32_t *p_dstend = p_dst + i_dst_visible_lines*(i_dst_pitch>>2);
166
167         const int i_shift_height = i_dst_height / i_src_height;
168         const int i_shift_width = i_dst_width / i_src_width;
169
170         int l = 1<<(SHIFT_SIZE-i_shift_height);
171         for( ; p_dst < p_dstend;
172              p_dst += (i_dst_hidden_pitch>>2),
173              p_dstendline += (i_dst_pitch>>2),
174              l += i_height_coef )
175         {
176             int k = 1<<(SHIFT_SIZE-i_shift_width);
177             uint32_t *p_srcl = p_src
178                     + (__MIN( i_src_height_1, l >> SHIFT_SIZE )*(i_src_pitch>>2));
179             for( ; p_dst < p_dstendline; p_dst++, k += i_width_coef )
180             {
181                 *p_dst = p_srcl[__MIN( i_src_width_1, k >> SHIFT_SIZE )];
182             }
183         }
184     }
185 }