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
8 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * Antoine Cellerier <dionoea @t videolan dot org>
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #include "vlc_filter.h"
33 /*****************************************************************************
34 * filter_sys_t : filter descriptor
35 *****************************************************************************/
42 /****************************************************************************
44 ****************************************************************************/
45 static int OpenFilter ( vlc_object_t * );
46 static void CloseFilter( vlc_object_t * );
48 static picture_t *Filter( filter_t *, picture_t * );
50 /*****************************************************************************
52 *****************************************************************************/
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 );
61 /*****************************************************************************
62 * OpenFilter: probe the filter and return score
63 *****************************************************************************/
64 static int OpenFilter( vlc_object_t *p_this )
66 filter_t *p_filter = (filter_t*)p_this;
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 )
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 )
83 msg_Err( p_filter, "out of memory" );
87 p_filter->pf_video_filter = Filter;
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 );
96 /*****************************************************************************
97 * CloseFilter: clean up the filter
98 *****************************************************************************/
99 static void CloseFilter( vlc_object_t *p_this )
101 filter_t *p_filter = (filter_t*)p_this;
102 filter_sys_t *p_sys = p_filter->p_sys;
107 /****************************************************************************
108 * Filter: the whole thing
109 ****************************************************************************/
110 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
112 picture_t *p_pic_dst;
115 if( !p_pic ) return NULL;
117 if( (p_filter->fmt_in.video.i_height == 0) ||
118 (p_filter->fmt_in.video.i_width == 0) )
121 if( (p_filter->fmt_out.video.i_height == 0) ||
122 (p_filter->fmt_out.video.i_width == 0) )
125 /* Request output picture */
126 p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
129 msg_Warn( p_filter, "can't get output picture" );
130 if( p_pic->pf_release )
131 p_pic->pf_release( p_pic );
135 if( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('R','G','B','A') )
137 for( i_plane = 0; i_plane < p_pic_dst->i_planes; i_plane++ )
139 const int i_src_pitch = p_pic->p[i_plane].i_pitch;
140 const int i_dst_pitch = p_pic_dst->p[i_plane].i_pitch;
141 const int i_src_height = p_filter->fmt_in.video.i_height;
142 const int i_src_width = p_filter->fmt_in.video.i_width;
143 const int i_dst_height = p_filter->fmt_out.video.i_height;
144 const int i_dst_width = p_filter->fmt_out.video.i_width;
145 const int i_dst_visible_lines =
146 p_pic_dst->p[i_plane].i_visible_lines;
147 const int i_dst_visible_pitch =
148 p_pic_dst->p[i_plane].i_visible_pitch;
149 const int i_dst_hidden_pitch = i_dst_pitch - i_dst_visible_pitch;
150 #define SHIFT_SIZE 16
151 const int i_height_coef = ( i_src_height << SHIFT_SIZE )
153 const int i_width_coef = ( i_src_width << SHIFT_SIZE )
155 const int i_src_height_1 = i_src_height - 1;
156 const int i_src_width_1 = i_src_width - 1;
158 uint8_t *p_src = p_pic->p[i_plane].p_pixels;
159 uint8_t *p_dst = p_pic_dst->p[i_plane].p_pixels;
160 uint8_t *p_dstendline = p_dst + i_dst_visible_pitch;
161 const uint8_t *p_dstend = p_dst + i_dst_visible_lines*i_dst_pitch;
163 const int i_shift_height = i_dst_height / i_src_height;
164 const int i_shift_width = i_dst_width / i_src_width;
166 int l = 1<<(SHIFT_SIZE-i_shift_height);
167 for( ; p_dst < p_dstend;
168 p_dst += i_dst_hidden_pitch,
169 p_dstendline += i_dst_pitch, l += i_height_coef )
171 int k = 1<<(SHIFT_SIZE-i_shift_width);
172 uint8_t *p_srcl = p_src
173 + (__MIN( i_src_height_1, l >> SHIFT_SIZE )*i_src_pitch);
175 for( ; p_dst < p_dstendline; p_dst++, k += i_width_coef )
177 *p_dst = p_srcl[__MIN( i_src_width_1, k >> SHIFT_SIZE )];
184 const int i_src_pitch = p_pic->p->i_pitch;
185 const int i_dst_pitch = p_pic_dst->p->i_pitch;
186 const int i_src_height = p_filter->fmt_in.video.i_height;
187 const int i_src_width = p_filter->fmt_in.video.i_width;
188 const int i_dst_height = p_filter->fmt_out.video.i_height;
189 const int i_dst_width = p_filter->fmt_out.video.i_width;
190 const int i_dst_visible_lines =
191 p_pic_dst->p->i_visible_lines;
192 const int i_dst_visible_pitch =
193 p_pic_dst->p->i_visible_pitch;
194 const int i_dst_hidden_pitch = i_dst_pitch - i_dst_visible_pitch;
195 const int i_height_coef = ( i_src_height << SHIFT_SIZE )
197 const int i_width_coef = ( i_src_width << SHIFT_SIZE )
199 const int i_src_height_1 = i_src_height - 1;
200 const int i_src_width_1 = i_src_width - 1;
202 uint32_t *p_src = (uint32_t*)p_pic->p->p_pixels;
203 uint32_t *p_dst = (uint32_t*)p_pic_dst->p->p_pixels;
204 uint32_t *p_dstendline = p_dst + (i_dst_visible_pitch>>2);
205 const uint32_t *p_dstend = p_dst + i_dst_visible_lines*(i_dst_pitch>>2);
207 const int i_shift_height = i_dst_height / i_src_height;
208 const int i_shift_width = i_dst_width / i_src_width;
210 int l = 1<<(SHIFT_SIZE-i_shift_height);
211 for( ; p_dst < p_dstend;
212 p_dst += (i_dst_hidden_pitch>>2),
213 p_dstendline += (i_dst_pitch>>2),
216 int k = 1<<(SHIFT_SIZE-i_shift_width);
217 uint32_t *p_srcl = p_src
218 + (__MIN( i_src_height_1, l >> SHIFT_SIZE )*(i_src_pitch>>2));
219 for( ; p_dst < p_dstendline; p_dst++, k += i_width_coef )
221 *p_dst = p_srcl[__MIN( i_src_width_1, k >> SHIFT_SIZE )];
226 p_pic_dst->date = p_pic->date;
227 p_pic_dst->b_force = p_pic->b_force;
228 p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
229 p_pic_dst->b_progressive = p_pic->b_progressive;
230 p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
232 p_pic->pf_release( p_pic );