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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <math.h> /* sin(), cos() */
35 #include "vlc_filter.h"
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Create ( vlc_object_t * );
41 static void Destroy ( vlc_object_t * );
43 static picture_t *Filter( filter_t *, picture_t * );
45 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
46 vlc_value_t oldval, vlc_value_t newval,
49 static int PreciseRotateCallback( vlc_object_t *p_this, char const *psz_var,
50 vlc_value_t oldval, vlc_value_t newval,
53 #define ANGLE_TEXT N_("Angle in degrees")
54 #define ANGLE_LONGTEXT N_("Angle in degrees (0 to 359)")
56 #define FILTER_PREFIX "rotate-"
58 /*****************************************************************************
60 *****************************************************************************/
62 set_description( _("Rotate video filter") );
63 set_shortname( _( "Rotate" ));
64 set_capability( "video filter2", 0 );
65 set_category( CAT_VIDEO );
66 set_subcategory( SUBCAT_VIDEO_VFILTER );
68 add_integer_with_range( FILTER_PREFIX "angle", 0, 0, 359, NULL,
69 ANGLE_TEXT, ANGLE_LONGTEXT, VLC_FALSE );
71 add_shortcut( "rotate" );
72 set_callbacks( Create, Destroy );
75 static const char *ppsz_filter_options[] = {
79 /*****************************************************************************
81 *****************************************************************************/
89 static inline void cache_trigo( int i_angle, int *i_sin, int *i_cos )
91 const double f_angle = (((double)i_angle)*M_PI)/1800.;
92 *i_sin = (int)(sin( f_angle )*4096.);
93 *i_cos = (int)(cos( f_angle )*4096.);
96 /*****************************************************************************
97 * Create: allocates Distort video filter
98 *****************************************************************************/
99 static int Create( vlc_object_t *p_this )
101 filter_t *p_filter = (filter_t *)p_this;
104 /* Allocate structure */
105 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
106 if( p_filter->p_sys == NULL )
108 msg_Err( p_filter, "out of memory" );
111 p_sys = p_filter->p_sys;
113 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
116 p_sys->i_angle = var_CreateGetIntegerCommand( p_filter,
117 FILTER_PREFIX "angle" ) * 10;
118 var_CreateGetIntegerCommand( p_filter, FILTER_PREFIX "deciangle" );
119 var_AddCallback( p_filter, FILTER_PREFIX "angle", RotateCallback, p_sys );
120 var_AddCallback( p_filter, FILTER_PREFIX "deciangle", PreciseRotateCallback, p_sys );
122 cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );
124 p_filter->pf_video_filter = Filter;
129 /*****************************************************************************
130 * Destroy: destroy Distort filter
131 *****************************************************************************/
132 static void Destroy( vlc_object_t *p_this )
134 filter_t *p_filter = (filter_t *)p_this;
136 free( p_filter->p_sys );
139 /*****************************************************************************
141 *****************************************************************************/
142 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
145 filter_sys_t *p_sys = p_filter->p_sys;
147 const int i_sin = p_sys->i_sin, i_cos = p_sys->i_cos;
149 if( !p_pic ) return NULL;
151 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
154 msg_Warn( p_filter, "can't get output picture" );
155 if( p_pic->pf_release )
156 p_pic->pf_release( p_pic );
160 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
162 const int i_visible_lines = p_pic->p[i_plane].i_visible_lines;
163 const int i_visible_pitch = p_pic->p[i_plane].i_visible_pitch;
164 const int i_pitch = p_pic->p[i_plane].i_pitch;
165 const int i_hidden_pitch = i_pitch - i_visible_pitch;
167 const int i_line_center = i_visible_lines>>1;
168 const int i_col_center = i_visible_pitch>>1;
170 const uint8_t *p_in = p_pic->p[i_plane].p_pixels;
171 uint8_t *p_out = p_outpic->p[i_plane].p_pixels;
172 uint8_t *p_outendline = p_out + i_visible_pitch;
173 const uint8_t *p_outend = p_out + i_visible_lines * i_pitch;
175 const uint8_t black_pixel = ( i_plane == Y_PLANE ) ? 0x00 : 0x80;
177 const int i_line_next = i_cos-i_sin*i_visible_pitch;
178 const int i_col_next = -i_sin-i_cos*i_visible_pitch;
179 int i_line_orig0 = - i_cos * i_line_center
180 - i_sin * i_col_center + (1<<11);
181 int i_col_orig0 = i_sin * i_line_center
182 - i_cos * i_col_center + (1<<11);
183 for( ; p_outendline < p_outend;
184 p_out += i_hidden_pitch, p_outendline += i_pitch,
185 i_line_orig0 += i_line_next, i_col_orig0 += i_col_next )
187 for( ; p_out < p_outendline;
188 p_out++, i_line_orig0 += i_sin, i_col_orig0 += i_cos )
190 const int i_line_orig = (i_line_orig0>>12) + i_line_center;
191 const int i_col_orig = (i_col_orig0>>12) + i_col_center;
192 int i_line_percent = (i_line_orig0>>4) & 255;
193 int i_col_percent = (i_col_orig0 >>4) & 255;
195 if( -1 <= i_line_orig && i_line_orig < i_visible_lines
196 && -1 <= i_col_orig && i_col_orig < i_visible_pitch )
198 uint8_t i_curpix = black_pixel;
199 uint8_t i_colpix = black_pixel;
200 uint8_t i_linpix = black_pixel;
201 uint8_t i_nexpix = black_pixel;
202 if( ( 0 <= i_line_orig ) && ( 0 <= i_col_orig ) )
203 i_curpix = p_in[i_line_orig*i_pitch+i_col_orig];
204 if( ( i_line_orig < i_visible_lines - 1)
205 && ( i_col_orig >= 0 ) )
207 i_linpix=p_in[(i_line_orig+1)*i_pitch+i_col_orig];
210 if( ( i_col_orig < i_visible_pitch - 1)
211 && ( i_line_orig >= 0 ) )
213 i_colpix=p_in[i_line_orig*i_pitch+i_col_orig+1];
215 if( ( i_line_orig < i_visible_lines - 1)
216 && ( i_col_orig < i_visible_pitch - 1) )
217 i_nexpix = p_in[(i_line_orig+1)*i_pitch+i_col_orig+1];
222 if( ( i_col_orig > i_visible_pitch/2 ) )
225 unsigned int temp = 0;
227 (256 - i_line_percent) * ( 256 - i_col_percent );
229 i_line_percent * (256 - i_col_percent );
231 ( i_col_percent) * ( i_line_percent);
233 i_col_percent * (256 - i_line_percent );
237 else if (i_col_orig == i_visible_pitch/2 )
238 { *p_out = black_pixel;
241 *p_out = p_in[i_line_orig*i_pitch+i_col_orig];
247 *p_out = black_pixel;
253 p_outpic->date = p_pic->date;
254 p_outpic->b_force = p_pic->b_force;
255 p_outpic->i_nb_fields = p_pic->i_nb_fields;
256 p_outpic->b_progressive = p_pic->b_progressive;
257 p_outpic->b_top_field_first = p_pic->b_top_field_first;
259 if( p_pic->pf_release )
260 p_pic->pf_release( p_pic );
265 /*****************************************************************************
267 *****************************************************************************/
268 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
269 vlc_value_t oldval, vlc_value_t newval,
272 filter_sys_t *p_sys = (filter_sys_t *)p_data;
274 if( !strcmp( psz_var, "rotate-angle" ) )
276 p_sys->i_angle = newval.i_int*10;
278 cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );
282 static int PreciseRotateCallback( vlc_object_t *p_this, char const *psz_var,
283 vlc_value_t oldval, vlc_value_t newval,
286 filter_sys_t *p_sys = (filter_sys_t *)p_data;
288 if( !strcmp( psz_var, "rotate-deciangle" ) )
290 p_sys->i_angle = newval.i_int;
291 cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );