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 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include "vlc_filter.h"
38 /*****************************************************************************
39 * filter_sys_t : filter descriptor
40 *****************************************************************************/
47 /****************************************************************************
49 ****************************************************************************/
50 static int OpenFilter ( vlc_object_t * );
51 static void CloseFilter( vlc_object_t * );
53 static picture_t *Filter( filter_t *, picture_t * );
55 /*****************************************************************************
57 *****************************************************************************/
59 set_description( N_("Video scaling filter") );
60 set_capability( "video filter2", 10000 );
61 // set_category( CAT_VIDEO );
62 // set_subcategory( SUBCAT_VIDEO_VFILTER2 );
63 set_callbacks( OpenFilter, CloseFilter );
66 /*****************************************************************************
67 * OpenFilter: probe the filter and return score
68 *****************************************************************************/
69 static int OpenFilter( vlc_object_t *p_this )
71 filter_t *p_filter = (filter_t*)p_this;
74 if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','P') &&
75 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','A') &&
76 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
77 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2') &&
78 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('R','G','B','A') ) ||
79 p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
84 /* Allocate the memory needed to store the decoder's structure */
85 if( ( p_filter->p_sys = p_sys =
86 (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
88 msg_Err( p_filter, "out of memory" );
92 p_filter->pf_video_filter = Filter;
94 msg_Dbg( p_filter, "%ix%i -> %ix%i", p_filter->fmt_in.video.i_width,
95 p_filter->fmt_in.video.i_height, p_filter->fmt_out.video.i_width,
96 p_filter->fmt_out.video.i_height );
101 /*****************************************************************************
102 * CloseFilter: clean up the filter
103 *****************************************************************************/
104 static void CloseFilter( vlc_object_t *p_this )
106 filter_t *p_filter = (filter_t*)p_this;
107 filter_sys_t *p_sys = p_filter->p_sys;
112 /****************************************************************************
113 * Filter: the whole thing
114 ****************************************************************************/
115 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
117 picture_t *p_pic_dst;
120 if( !p_pic ) return NULL;
122 if( (p_filter->fmt_in.video.i_height == 0) ||
123 (p_filter->fmt_in.video.i_width == 0) )
126 if( (p_filter->fmt_out.video.i_height == 0) ||
127 (p_filter->fmt_out.video.i_width == 0) )
130 /* Request output picture */
131 p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
134 msg_Warn( p_filter, "can't get output picture" );
135 if( p_pic->pf_release )
136 p_pic->pf_release( p_pic );
140 if( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('R','G','B','A') )
142 for( i_plane = 0; i_plane < p_pic_dst->i_planes; i_plane++ )
144 const int i_src_pitch = p_pic->p[i_plane].i_pitch;
145 const int i_dst_pitch = p_pic_dst->p[i_plane].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_plane].i_visible_lines;
152 const int i_dst_visible_pitch =
153 p_pic_dst->p[i_plane].i_visible_pitch;
154 const int i_dst_hidden_pitch = i_dst_pitch - i_dst_visible_pitch;
155 #define SHIFT_SIZE 16
156 const int i_height_coef = ( i_src_height << SHIFT_SIZE )
158 const int i_width_coef = ( i_src_width << SHIFT_SIZE )
160 const int i_src_height_1 = i_src_height - 1;
161 const int i_src_width_1 = i_src_width - 1;
163 uint8_t *p_src = p_pic->p[i_plane].p_pixels;
164 uint8_t *p_dst = p_pic_dst->p[i_plane].p_pixels;
165 uint8_t *p_dstendline = p_dst + i_dst_visible_pitch;
166 const uint8_t *p_dstend = p_dst + i_dst_visible_lines*i_dst_pitch;
168 const int i_shift_height = i_dst_height / i_src_height;
169 const int i_shift_width = i_dst_width / i_src_width;
171 int l = 1<<(SHIFT_SIZE-i_shift_height);
172 for( ; p_dst < p_dstend;
173 p_dst += i_dst_hidden_pitch,
174 p_dstendline += i_dst_pitch, l += i_height_coef )
176 int k = 1<<(SHIFT_SIZE-i_shift_width);
177 uint8_t *p_srcl = p_src
178 + (__MIN( i_src_height_1, l >> SHIFT_SIZE )*i_src_pitch);
180 for( ; p_dst < p_dstendline; p_dst++, k += i_width_coef )
182 *p_dst = p_srcl[__MIN( i_src_width_1, k >> SHIFT_SIZE )];
189 const int i_src_pitch = p_pic->p->i_pitch;
190 const int i_dst_pitch = p_pic_dst->p->i_pitch;
191 const int i_src_height = p_filter->fmt_in.video.i_height;
192 const int i_src_width = p_filter->fmt_in.video.i_width;
193 const int i_dst_height = p_filter->fmt_out.video.i_height;
194 const int i_dst_width = p_filter->fmt_out.video.i_width;
195 const int i_dst_visible_lines =
196 p_pic_dst->p->i_visible_lines;
197 const int i_dst_visible_pitch =
198 p_pic_dst->p->i_visible_pitch;
199 const int i_dst_hidden_pitch = i_dst_pitch - i_dst_visible_pitch;
200 const int i_height_coef = ( i_src_height << SHIFT_SIZE )
202 const int i_width_coef = ( i_src_width << SHIFT_SIZE )
204 const int i_src_height_1 = i_src_height - 1;
205 const int i_src_width_1 = i_src_width - 1;
207 uint32_t *p_src = (uint32_t*)p_pic->p->p_pixels;
208 uint32_t *p_dst = (uint32_t*)p_pic_dst->p->p_pixels;
209 uint32_t *p_dstendline = p_dst + (i_dst_visible_pitch>>2);
210 const uint32_t *p_dstend = p_dst + i_dst_visible_lines*(i_dst_pitch>>2);
212 const int i_shift_height = i_dst_height / i_src_height;
213 const int i_shift_width = i_dst_width / i_src_width;
215 int l = 1<<(SHIFT_SIZE-i_shift_height);
216 for( ; p_dst < p_dstend;
217 p_dst += (i_dst_hidden_pitch>>2),
218 p_dstendline += (i_dst_pitch>>2),
221 int k = 1<<(SHIFT_SIZE-i_shift_width);
222 uint32_t *p_srcl = p_src
223 + (__MIN( i_src_height_1, l >> SHIFT_SIZE )*(i_src_pitch>>2));
224 for( ; p_dst < p_dstendline; p_dst++, k += i_width_coef )
226 *p_dst = p_srcl[__MIN( i_src_width_1, k >> SHIFT_SIZE )];
231 p_pic_dst->date = p_pic->date;
232 p_pic_dst->b_force = p_pic->b_force;
233 p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
234 p_pic_dst->b_progressive = p_pic->b_progressive;
235 p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
237 if( p_pic->pf_release )
238 p_pic->pf_release( p_pic );