]> git.sesse.net Git - vlc/blob - modules/video_filter/scale.c
9b62aa9d65032b2613fdf810377fa1aed19bbba1
[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 #include <vlc/vlc.h>
30 #include <vlc_vout.h>
31 #include "vlc_filter.h"
32
33 /*****************************************************************************
34  * filter_sys_t : filter descriptor
35  *****************************************************************************/
36 struct filter_sys_t
37 {
38     es_format_t fmt_in;
39     es_format_t fmt_out;
40 };
41
42 /****************************************************************************
43  * Local prototypes
44  ****************************************************************************/
45 static int  OpenFilter ( vlc_object_t * );
46 static void CloseFilter( vlc_object_t * );
47
48 static picture_t *Filter( filter_t *, picture_t * );
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 vlc_module_begin();
54     set_description( _("Video scaling filter") );
55     set_capability( "video filter2", 10000 );
56 //    set_category( CAT_VIDEO );
57 //    set_subcategory( SUBCAT_VIDEO_VFILTER2 );
58     set_callbacks( OpenFilter, CloseFilter );
59 vlc_module_end();
60
61 /*****************************************************************************
62  * OpenFilter: probe the filter and return score
63  *****************************************************************************/
64 static int OpenFilter( vlc_object_t *p_this )
65 {
66     filter_t *p_filter = (filter_t*)p_this;
67     filter_sys_t *p_sys;
68
69     if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','P') &&
70           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','A') &&
71           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
72           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2') &&
73           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('R','G','B','A') ) ||
74         p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
75     {
76         return VLC_EGENERIC;
77     }
78
79     /* Allocate the memory needed to store the decoder's structure */
80     if( ( p_filter->p_sys = p_sys =
81           (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
82     {
83         msg_Err( p_filter, "out of memory" );
84         return VLC_EGENERIC;
85     }
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     /* Request output picture */
118     p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
119     if( !p_pic_dst )
120     {
121         msg_Warn( p_filter, "can't get output picture" );
122         if( p_pic->pf_release )
123             p_pic->pf_release( p_pic );
124         return NULL;
125     }
126
127     if( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('R','G','B','A') )
128     {
129         for( i_plane = 0; i_plane < p_pic_dst->i_planes; i_plane++ )
130         {
131             const int i_src_pitch    = p_pic->p[i_plane].i_pitch;
132             const int i_dst_pitch    = p_pic_dst->p[i_plane].i_pitch;
133             const int i_src_height   = p_filter->fmt_in.video.i_height;
134             const int i_src_width    = p_filter->fmt_in.video.i_width;
135             const int i_dst_height   = p_filter->fmt_out.video.i_height;
136             const int i_dst_width    = p_filter->fmt_out.video.i_width;
137             const int i_dst_visible_lines =
138                                        p_pic_dst->p[i_plane].i_visible_lines;
139             const int i_dst_visible_pitch =
140                                        p_pic_dst->p[i_plane].i_visible_pitch;
141             const int i_dst_hidden_pitch  = i_dst_pitch - i_dst_visible_pitch;
142 #define SHIFT_SIZE 16
143             const int i_height_coef  = ( i_src_height << SHIFT_SIZE )
144                                        / i_dst_height;
145             const int i_width_coef   = ( i_src_width << SHIFT_SIZE )
146                                        / i_dst_width;
147             const int i_src_height_1 = i_src_height - 1;
148             const int i_src_width_1  = i_src_width - 1;
149
150             uint8_t *p_src = p_pic->p[i_plane].p_pixels;
151             uint8_t *p_dst = p_pic_dst->p[i_plane].p_pixels;
152             uint8_t *p_dstendline = p_dst + i_dst_visible_pitch;
153             const uint8_t *p_dstend = p_dst + i_dst_visible_lines*i_dst_pitch;
154
155             int l = 1<<(SHIFT_SIZE-1);
156             for( ; p_dst < p_dstend;
157                  p_dst += i_dst_hidden_pitch,
158                  p_dstendline += i_dst_pitch, l += i_height_coef )
159             {
160                 int k = 1<<(SHIFT_SIZE-1);
161                 uint8_t *p_srcl = p_src
162                        + (__MIN( i_src_height_1, l >> SHIFT_SIZE )*i_src_pitch);
163
164                 for( ; p_dst < p_dstendline; p_dst++, k += i_width_coef )
165                 {
166                     *p_dst = p_srcl[__MIN( i_src_width_1, k >> SHIFT_SIZE )];
167                 }
168             }
169         }
170     }
171     else /* RGBA */
172     {
173         const int i_src_pitch = p_pic->p->i_pitch;
174         const int i_dst_pitch = p_pic_dst->p->i_pitch;
175         const int i_src_height   = p_filter->fmt_in.video.i_height;
176         const int i_src_width    = p_filter->fmt_in.video.i_width;
177         const int i_dst_height   = p_filter->fmt_out.video.i_height;
178         const int i_dst_width    = p_filter->fmt_out.video.i_width;
179         const int i_dst_visible_lines =
180                                    p_pic_dst->p->i_visible_lines;
181         const int i_dst_visible_pitch =
182                                    p_pic_dst->p->i_visible_pitch;
183         const int i_dst_hidden_pitch  = i_dst_pitch - i_dst_visible_pitch;
184         const int i_height_coef  = ( i_src_height << SHIFT_SIZE )
185                                    / i_dst_height;
186         const int i_width_coef   = ( i_src_width << SHIFT_SIZE )
187                                    / i_dst_width;
188         const int i_src_height_1 = i_src_height - 1;
189         const int i_src_width_1  = i_src_width - 1;
190
191         uint32_t *p_src = (uint32_t*)p_pic->p->p_pixels;
192         uint32_t *p_dst = (uint32_t*)p_pic_dst->p->p_pixels;
193         uint32_t *p_dstendline = p_dst + (i_dst_visible_pitch>>2);
194         const uint32_t *p_dstend = p_dst + i_dst_visible_lines*(i_dst_pitch>>2);
195
196         int l = 1<<(SHIFT_SIZE-1);
197         for( ; p_dst < p_dstend;
198              p_dst += (i_dst_hidden_pitch>>2),
199              p_dstendline += (i_dst_pitch>>2),
200              l += i_height_coef )
201         {
202             int k = 1<<(SHIFT_SIZE-1);
203             uint32_t *p_srcl = p_src
204                     + (__MIN( i_src_height_1, l >> SHIFT_SIZE )*(i_src_pitch>>2));
205             for( ; p_dst < p_dstendline; p_dst++, k += i_width_coef )
206             {
207                 *p_dst = p_srcl[__MIN( i_src_width_1, k >> SHIFT_SIZE )];
208             }
209         }
210     }
211
212     p_pic_dst->date = p_pic->date;
213     p_pic_dst->b_force = p_pic->b_force;
214     p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
215     p_pic_dst->b_progressive = p_pic->b_progressive;
216     p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
217
218     p_pic->pf_release( p_pic );
219     return p_pic_dst;
220 }