]> git.sesse.net Git - vlc/blob - modules/video_filter/scale.c
video_filter: fix warnings
[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/vlc.h>
34 #include <vlc_vout.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( _("Video scaling filter") );
59     set_capability( "video filter2", 10000 );
60 //    set_category( CAT_VIDEO );
61 //    set_subcategory( SUBCAT_VIDEO_VFILTER2 );
62     set_callbacks( OpenFilter, CloseFilter );
63 vlc_module_end();
64
65 /*****************************************************************************
66  * OpenFilter: probe the filter and return score
67  *****************************************************************************/
68 static int OpenFilter( vlc_object_t *p_this )
69 {
70     filter_t *p_filter = (filter_t*)p_this;
71     filter_sys_t *p_sys;
72
73     if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','P') &&
74           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','A') &&
75           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
76           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','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     {
87         msg_Err( p_filter, "out of memory" );
88         return VLC_EGENERIC;
89     }
90
91     p_filter->pf_video_filter = Filter;
92
93     msg_Dbg( p_filter, "%ix%i -> %ix%i", p_filter->fmt_in.video.i_width,
94              p_filter->fmt_in.video.i_height, p_filter->fmt_out.video.i_width,
95              p_filter->fmt_out.video.i_height );
96
97     return VLC_SUCCESS;
98 }
99
100 /*****************************************************************************
101  * CloseFilter: clean up the filter
102  *****************************************************************************/
103 static void CloseFilter( vlc_object_t *p_this )
104 {
105     filter_t *p_filter = (filter_t*)p_this;
106     filter_sys_t *p_sys = p_filter->p_sys;
107
108     free( p_sys );
109 }
110
111 /****************************************************************************
112  * Filter: the whole thing
113  ****************************************************************************/
114 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
115 {
116     picture_t *p_pic_dst;
117     int i_plane;
118
119     if( !p_pic ) return NULL;
120
121     if( (p_filter->fmt_in.video.i_height == 0) ||
122         (p_filter->fmt_in.video.i_width == 0) )
123         return NULL;
124
125     if( (p_filter->fmt_out.video.i_height == 0) ||
126         (p_filter->fmt_out.video.i_width == 0) )
127         return NULL;
128
129     /* Request output picture */
130     p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
131     if( !p_pic_dst )
132     {
133         msg_Warn( p_filter, "can't get output picture" );
134         if( p_pic->pf_release )
135             p_pic->pf_release( p_pic );
136         return NULL;
137     }
138
139     if( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('R','G','B','A') )
140     {
141         for( i_plane = 0; i_plane < p_pic_dst->i_planes; i_plane++ )
142         {
143             const int i_src_pitch    = p_pic->p[i_plane].i_pitch;
144             const int i_dst_pitch    = p_pic_dst->p[i_plane].i_pitch;
145             const int i_src_height   = p_filter->fmt_in.video.i_height;
146             const int i_src_width    = p_filter->fmt_in.video.i_width;
147             const int i_dst_height   = p_filter->fmt_out.video.i_height;
148             const int i_dst_width    = p_filter->fmt_out.video.i_width;
149             const int i_dst_visible_lines =
150                                        p_pic_dst->p[i_plane].i_visible_lines;
151             const int i_dst_visible_pitch =
152                                        p_pic_dst->p[i_plane].i_visible_pitch;
153             const int i_dst_hidden_pitch  = i_dst_pitch - i_dst_visible_pitch;
154 #define SHIFT_SIZE 16
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             uint8_t *p_src = p_pic->p[i_plane].p_pixels;
163             uint8_t *p_dst = p_pic_dst->p[i_plane].p_pixels;
164             uint8_t *p_dstendline = p_dst + i_dst_visible_pitch;
165             const uint8_t *p_dstend = p_dst + i_dst_visible_lines*i_dst_pitch;
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,
173                  p_dstendline += i_dst_pitch, l += i_height_coef )
174             {
175                 int k = 1<<(SHIFT_SIZE-i_shift_width);
176                 uint8_t *p_srcl = p_src
177                        + (__MIN( i_src_height_1, l >> SHIFT_SIZE )*i_src_pitch);
178
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     }
186     else /* RGBA */
187     {
188         const int i_src_pitch = p_pic->p->i_pitch;
189         const int i_dst_pitch = p_pic_dst->p->i_pitch;
190         const int i_src_height   = p_filter->fmt_in.video.i_height;
191         const int i_src_width    = p_filter->fmt_in.video.i_width;
192         const int i_dst_height   = p_filter->fmt_out.video.i_height;
193         const int i_dst_width    = p_filter->fmt_out.video.i_width;
194         const int i_dst_visible_lines =
195                                    p_pic_dst->p->i_visible_lines;
196         const int i_dst_visible_pitch =
197                                    p_pic_dst->p->i_visible_pitch;
198         const int i_dst_hidden_pitch  = i_dst_pitch - i_dst_visible_pitch;
199         const int i_height_coef  = ( i_src_height << SHIFT_SIZE )
200                                    / i_dst_height;
201         const int i_width_coef   = ( i_src_width << SHIFT_SIZE )
202                                    / i_dst_width;
203         const int i_src_height_1 = i_src_height - 1;
204         const int i_src_width_1  = i_src_width - 1;
205
206         uint32_t *p_src = (uint32_t*)p_pic->p->p_pixels;
207         uint32_t *p_dst = (uint32_t*)p_pic_dst->p->p_pixels;
208         uint32_t *p_dstendline = p_dst + (i_dst_visible_pitch>>2);
209         const uint32_t *p_dstend = p_dst + i_dst_visible_lines*(i_dst_pitch>>2);
210
211         const int i_shift_height = i_dst_height / i_src_height;
212         const int i_shift_width = i_dst_width / i_src_width;
213
214         int l = 1<<(SHIFT_SIZE-i_shift_height);
215         for( ; p_dst < p_dstend;
216              p_dst += (i_dst_hidden_pitch>>2),
217              p_dstendline += (i_dst_pitch>>2),
218              l += i_height_coef )
219         {
220             int k = 1<<(SHIFT_SIZE-i_shift_width);
221             uint32_t *p_srcl = p_src
222                     + (__MIN( i_src_height_1, l >> SHIFT_SIZE )*(i_src_pitch>>2));
223             for( ; p_dst < p_dstendline; p_dst++, k += i_width_coef )
224             {
225                 *p_dst = p_srcl[__MIN( i_src_width_1, k >> SHIFT_SIZE )];
226             }
227         }
228     }
229
230     p_pic_dst->date = p_pic->date;
231     p_pic_dst->b_force = p_pic->b_force;
232     p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
233     p_pic_dst->b_progressive = p_pic->b_progressive;
234     p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
235
236     p_pic->pf_release( p_pic );
237     return p_pic_dst;
238 }