1 /*****************************************************************************
2 * rotate.c : video rotation filter
3 *****************************************************************************
4 * Copyright (C) 2000-2006 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <math.h> /* sin(), cos() */
33 #include "vlc_filter.h"
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Create ( vlc_object_t * );
39 static void Destroy ( vlc_object_t * );
41 static picture_t *Filter( filter_t *, picture_t * );
43 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
44 vlc_value_t oldval, vlc_value_t newval,
47 static int PreciseRotateCallback( vlc_object_t *p_this, char const *psz_var,
48 vlc_value_t oldval, vlc_value_t newval,
51 #define ANGLE_TEXT N_("Angle in degrees")
52 #define ANGLE_LONGTEXT N_("Angle in degrees (0 to 359)")
54 #define FILTER_PREFIX "rotate-"
56 /*****************************************************************************
58 *****************************************************************************/
60 set_description( _("Rotate video filter") );
61 set_shortname( _( "Rotate" ));
62 set_capability( "video filter2", 0 );
63 set_category( CAT_VIDEO );
64 set_subcategory( SUBCAT_VIDEO_VFILTER );
66 add_integer_with_range( FILTER_PREFIX "angle", 30, 0, 359, NULL,
67 ANGLE_TEXT, ANGLE_LONGTEXT, VLC_FALSE );
69 add_shortcut( "rotate" );
70 set_callbacks( Create, Destroy );
73 static const char *ppsz_filter_options[] = {
77 /*****************************************************************************
79 *****************************************************************************/
87 static inline void cache_trigo( int i_angle, int *i_sin, int *i_cos )
89 const double f_angle = (((double)i_angle)*M_PI)/1800.;
90 *i_sin = (int)(sin( f_angle )*4096.);
91 *i_cos = (int)(cos( f_angle )*4096.);
94 /*****************************************************************************
95 * Create: allocates Distort video filter
96 *****************************************************************************/
97 static int Create( vlc_object_t *p_this )
99 filter_t *p_filter = (filter_t *)p_this;
102 if( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0')
103 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','Y','U','V')
104 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('J','4','2','0')
105 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2')
107 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','2')
108 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('J','4','2','2')
111 /* We only want planar YUV 4:2:0 or 4:2:2 */
112 msg_Err( p_filter, "Unsupported input chroma (%4s)",
113 (char*)&(p_filter->fmt_in.video.i_chroma) );
117 if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
119 msg_Err( p_filter, "Input and output chromas don't match" );
123 /* Allocate structure */
124 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
125 if( p_filter->p_sys == NULL )
127 msg_Err( p_filter, "out of memory" );
130 p_sys = p_filter->p_sys;
132 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
135 p_sys->i_angle = var_CreateGetIntegerCommand( p_filter,
136 FILTER_PREFIX "angle" ) * 10;
137 var_Create( p_filter, FILTER_PREFIX "deciangle",
138 VLC_VAR_INTEGER|VLC_VAR_ISCOMMAND );
139 var_AddCallback( p_filter, FILTER_PREFIX "angle", RotateCallback, p_sys );
140 var_AddCallback( p_filter, FILTER_PREFIX "deciangle",
141 PreciseRotateCallback, p_sys );
143 cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );
145 p_filter->pf_video_filter = Filter;
150 /*****************************************************************************
151 * Destroy: destroy Distort filter
152 *****************************************************************************/
153 static void Destroy( vlc_object_t *p_this )
155 filter_t *p_filter = (filter_t *)p_this;
157 free( p_filter->p_sys );
160 /*****************************************************************************
162 *****************************************************************************/
163 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
166 filter_sys_t *p_sys = p_filter->p_sys;
168 const int i_sin = p_sys->i_sin, i_cos = p_sys->i_cos;
170 if( !p_pic ) return NULL;
172 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
175 msg_Warn( p_filter, "can't get output picture" );
176 if( p_pic->pf_release )
177 p_pic->pf_release( p_pic );
181 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
183 const int i_visible_lines = p_pic->p[i_plane].i_visible_lines;
184 const int i_visible_pitch = p_pic->p[i_plane].i_visible_pitch;
185 const int i_pitch = p_pic->p[i_plane].i_pitch;
186 const int i_hidden_pitch = i_pitch - i_visible_pitch;
188 const int i_aspect = ( i_visible_lines * p_pic->p[Y_PLANE].i_visible_pitch ) / ( p_pic->p[Y_PLANE].i_visible_lines * i_visible_pitch );
189 /* = 2 for U and V planes in YUV 4:2:2, = 1 otherwise */
191 const int i_line_center = i_visible_lines>>1;
192 const int i_col_center = i_visible_pitch>>1;
194 const uint8_t *p_in = p_pic->p[i_plane].p_pixels;
195 uint8_t *p_out = p_outpic->p[i_plane].p_pixels;
196 uint8_t *p_outendline = p_out + i_visible_pitch;
197 const uint8_t *p_outend = p_out + i_visible_lines * i_pitch;
199 const uint8_t black_pixel = ( i_plane == Y_PLANE ) ? 0x00 : 0x80;
201 const int i_line_next = i_cos / i_aspect -i_sin*i_visible_pitch;
202 const int i_col_next = -i_sin / i_aspect -i_cos*i_visible_pitch;
203 int i_line_orig0 = ( - i_cos * i_line_center / i_aspect
204 - i_sin * i_col_center + (1<<11) );
205 int i_col_orig0 = i_sin * i_line_center / i_aspect
206 - i_cos * i_col_center + (1<<11);
207 for( ; p_outendline < p_outend;
208 p_out += i_hidden_pitch, p_outendline += i_pitch,
209 i_line_orig0 += i_line_next, i_col_orig0 += i_col_next )
211 for( ; p_out < p_outendline;
212 p_out++, i_line_orig0 += i_sin, i_col_orig0 += i_cos )
214 const int i_line_orig = (i_line_orig0>>12)*i_aspect + i_line_center;
215 const int i_col_orig = (i_col_orig0>>12) + i_col_center;
216 const uint8_t* p_orig_offset = p_in + i_line_orig * i_pitch
218 const uint8_t i_line_percent = (i_line_orig0>>4) & 255;
219 const uint8_t i_col_percent = (i_col_orig0 >>4) & 255;
221 if( -1 <= i_line_orig && i_line_orig < i_visible_lines
222 && -1 <= i_col_orig && i_col_orig < i_visible_pitch )
227 if( ( i_col_orig > i_visible_pitch/2 ) )
230 uint8_t i_curpix = black_pixel;
231 uint8_t i_colpix = black_pixel;
232 uint8_t i_linpix = black_pixel;
233 uint8_t i_nexpix = black_pixel;
234 if( ( 0 <= i_line_orig ) && ( 0 <= i_col_orig ) )
235 i_curpix = *p_orig_offset;
238 if( ( i_col_orig < i_visible_pitch - 1)
239 && ( i_line_orig >= 0 ) )
240 i_colpix = *p_orig_offset;
242 p_orig_offset+=i_pitch;
243 if( ( i_line_orig < i_visible_lines - 1)
244 && ( i_col_orig < i_visible_pitch - 1) )
245 i_nexpix = *p_orig_offset;
248 if( ( i_line_orig < i_visible_lines - 1)
249 && ( i_col_orig >= 0 ) )
250 i_linpix = *p_orig_offset;
252 unsigned int temp = 0;
254 (256 - i_line_percent) * ( 256 - i_col_percent );
256 i_line_percent * (256 - i_col_percent );
258 ( i_col_percent) * ( i_line_percent);
260 i_col_percent * (256 - i_line_percent );
264 else if (i_col_orig == i_visible_pitch/2 )
265 { *p_out = black_pixel;
268 *p_out = *p_orig_offset;
274 *p_out = black_pixel;
280 p_outpic->date = p_pic->date;
281 p_outpic->b_force = p_pic->b_force;
282 p_outpic->i_nb_fields = p_pic->i_nb_fields;
283 p_outpic->b_progressive = p_pic->b_progressive;
284 p_outpic->b_top_field_first = p_pic->b_top_field_first;
286 if( p_pic->pf_release )
287 p_pic->pf_release( p_pic );
292 /*****************************************************************************
293 * Angle modification callbacks.
294 *****************************************************************************/
295 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
296 vlc_value_t oldval, vlc_value_t newval,
299 filter_sys_t *p_sys = (filter_sys_t *)p_data;
300 p_sys->i_angle = newval.i_int*10;
301 cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );
305 static int PreciseRotateCallback( vlc_object_t *p_this, char const *psz_var,
306 vlc_value_t oldval, vlc_value_t newval,
309 filter_sys_t *p_sys = (filter_sys_t *)p_data;
310 p_sys->i_angle = newval.i_int;
311 cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );