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() */
33 #include <vlc/decoder.h>
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 #define ANGLE_TEXT N_("Angle in degrees")
50 #define ANGLE_LONGTEXT N_("Angle in degrees (0 to 359)")
52 #define FILTER_PREFIX "rotate-"
54 /*****************************************************************************
56 *****************************************************************************/
58 set_description( _("Rotate video filter") );
59 set_shortname( _( "Rotate" ));
60 set_capability( "video filter2", 0 );
61 set_category( CAT_VIDEO );
62 set_subcategory( SUBCAT_VIDEO_VFILTER );
64 add_integer_with_range( FILTER_PREFIX "angle", 0, 0, 359, NULL,
65 ANGLE_TEXT, ANGLE_LONGTEXT, VLC_FALSE );
67 add_shortcut( "rotate" );
68 set_callbacks( Create, Destroy );
71 static const char *ppsz_filter_options[] = {
75 /*****************************************************************************
76 * vout_sys_t: Distort video output method descriptor
77 *****************************************************************************
78 * This structure is part of the video output thread descriptor.
79 * It describes the Distort specific properties of an output thread.
80 *****************************************************************************/
90 /*****************************************************************************
91 * Create: allocates Distort video thread output method
92 *****************************************************************************
93 * This function allocates and initializes a Distort vout method.
94 *****************************************************************************/
95 static int Create( vlc_object_t *p_this )
97 filter_t *p_filter = (filter_t *)p_this;
99 /* Allocate structure */
100 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
101 if( p_filter->p_sys == NULL )
103 msg_Err( p_filter, "out of memory" );
107 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
109 var_Create( p_filter, FILTER_PREFIX "angle",
110 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
112 p_filter->pf_video_filter = Filter;
114 p_filter->p_sys->i_angle = var_GetInteger( p_filter,
115 FILTER_PREFIX "angle" );
116 p_filter->p_sys->last_date = 0;
118 var_Create( p_filter->p_libvlc, "rotate_angle", VLC_VAR_INTEGER );
119 var_AddCallback( p_filter->p_libvlc,
120 "rotate_angle", RotateCallback, p_filter->p_sys );
121 var_SetInteger( p_filter->p_libvlc, "rotate_angle",
122 p_filter->p_sys->i_angle );
127 /*****************************************************************************
128 * Destroy: destroy Distort video thread output method
129 *****************************************************************************
130 * Terminate an output method created by DistortCreateOutputMethod
131 *****************************************************************************/
132 static void Destroy( vlc_object_t *p_this )
134 filter_t *p_filter = (filter_t *)p_this;
136 var_DelCallback( p_filter->p_libvlc,
137 "rotate_angle", RotateCallback, p_filter->p_sys );
138 free( p_filter->p_sys );
141 /*****************************************************************************
142 * Render: displays previously rendered output
143 *****************************************************************************
144 * This function send the currently rendered image to Distort image, waits
145 * until it is displayed and switch the two rendering buffers, preparing next
147 *****************************************************************************/
148 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
151 filter_sys_t *p_sys = p_filter->p_sys;
153 int i_sin = p_sys->i_sin, i_cos = p_sys->i_cos;
154 mtime_t new_date = mdate();
156 if( !p_pic ) return NULL;
158 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
161 msg_Warn( p_filter, "can't get output picture" );
162 if( p_pic->pf_release )
163 p_pic->pf_release( p_pic );
167 p_sys->last_date = new_date;
169 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
171 int i_line, i_num_lines, i_line_center, i_col, i_num_cols, i_col_center;
173 uint8_t *p_in, *p_out;
175 p_in = p_pic->p[i_index].p_pixels;
176 p_out = p_outpic->p[i_index].p_pixels;
178 i_num_lines = p_pic->p[i_index].i_visible_lines;
179 i_num_cols = p_pic->p[i_index].i_visible_pitch;
180 i_line_center = i_num_lines/2;
181 i_col_center = i_num_cols/2;
183 black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
185 for( i_line = 0 ; i_line < i_num_lines ; i_line++ )
187 for( i_col = 0; i_col < i_num_cols ; i_col++ )
189 int i_line_orig, i_col_orig;
190 i_line_orig = ( ( i_cos * (i_line-i_line_center)
191 + i_sin * (i_col-i_col_center)
194 i_col_orig = ( (-i_sin * (i_line-i_line_center)
195 + i_cos * (i_col-i_col_center)
199 if( 0 <= i_line_orig && i_line_orig < i_num_lines
200 && 0 <= i_col_orig && i_col_orig < i_num_cols )
203 p_out[i_line*i_num_cols+i_col] =
204 p_in[i_line_orig*i_num_cols+i_col_orig];
208 p_out[i_line*i_num_cols+i_col] = black_pixel;
214 p_outpic->date = p_pic->date;
215 p_outpic->b_force = p_pic->b_force;
216 p_outpic->i_nb_fields = p_pic->i_nb_fields;
217 p_outpic->b_progressive = p_pic->b_progressive;
218 p_outpic->b_top_field_first = p_pic->b_top_field_first;
220 if( p_pic->pf_release )
221 p_pic->pf_release( p_pic );
226 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
227 vlc_value_t oldval, vlc_value_t newval,
230 filter_sys_t *p_sys = (filter_sys_t *)p_data;
232 if( !strcmp( psz_var, "rotate_angle" ) )
236 p_sys->i_angle = newval.i_int;
238 f_angle = (((double)p_sys->i_angle)*M_PI)/180.;
239 p_sys->i_sin = (int)(sin( f_angle )*256.);
240 p_sys->i_cos = (int)(cos( f_angle )*256.);