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 *****************************************************************************/
87 /*****************************************************************************
88 * Create: allocates Distort video thread output method
89 *****************************************************************************
90 * This function allocates and initializes a Distort vout method.
91 *****************************************************************************/
92 static int Create( vlc_object_t *p_this )
94 filter_t *p_filter = (filter_t *)p_this;
96 /* Allocate structure */
97 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
98 if( p_filter->p_sys == NULL )
100 msg_Err( p_filter, "out of memory" );
104 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
106 var_Create( p_filter, FILTER_PREFIX "angle",
107 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
109 p_filter->pf_video_filter = Filter;
111 p_filter->p_sys->i_angle = var_GetInteger( p_filter,
112 FILTER_PREFIX "angle" );
113 p_filter->p_sys->last_date = 0;
115 var_Create( p_filter->p_libvlc, "rotate_angle", VLC_VAR_INTEGER );
116 var_SetInteger( p_filter->p_libvlc, "rotate_angle",
117 p_filter->p_sys->i_angle );
118 var_AddCallback( p_filter->p_libvlc,
119 "rotate_angle", RotateCallback, p_filter->p_sys );
124 /*****************************************************************************
125 * Destroy: destroy Distort video thread output method
126 *****************************************************************************
127 * Terminate an output method created by DistortCreateOutputMethod
128 *****************************************************************************/
129 static void Destroy( vlc_object_t *p_this )
131 filter_t *p_filter = (filter_t *)p_this;
133 var_DelCallback( p_filter->p_libvlc,
134 "rotate_angle", RotateCallback, p_filter->p_sys );
135 free( p_filter->p_sys );
138 /*****************************************************************************
139 * Render: displays previously rendered output
140 *****************************************************************************
141 * This function send the currently rendered image to Distort image, waits
142 * until it is displayed and switch the two rendering buffers, preparing next
144 *****************************************************************************/
145 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
151 mtime_t new_date = mdate();
153 if( !p_pic ) return NULL;
155 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
158 msg_Warn( p_filter, "can't get output picture" );
159 if( p_pic->pf_release )
160 p_pic->pf_release( p_pic );
164 p_filter->p_sys->last_date = new_date;
165 f_angle = (((double)p_filter->p_sys->i_angle)*M_PI)/180.;
166 f_sin = sin( f_angle );
167 f_cos = cos( f_angle );
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 /* Ok, we do 3 times the sin() calculation for each line. So what ? */
186 for( i_line = 0 ; i_line < i_num_lines ; i_line++ )
188 for( i_col = 0; i_col < i_num_cols ; i_col++ )
190 int i_line_orig, i_col_orig;
191 i_line_orig = (int)( f_cos * (double)(i_line-i_line_center)
192 + f_sin * (double)(i_col-i_col_center)
195 i_col_orig = (int)(-f_sin * (double)(i_line-i_line_center)
196 + f_cos * (double)(i_col-i_col_center)
200 if( 0 <= i_line_orig && i_line_orig < i_num_lines
201 && 0 <= i_col_orig && i_col_orig < i_num_cols )
204 p_out[i_line*i_num_cols+i_col] =
205 p_in[i_line_orig*i_num_cols+i_col_orig];
209 p_out[i_line*i_num_cols+i_col] = black_pixel;
215 p_outpic->date = p_pic->date;
216 p_outpic->b_force = p_pic->b_force;
217 p_outpic->i_nb_fields = p_pic->i_nb_fields;
218 p_outpic->b_progressive = p_pic->b_progressive;
219 p_outpic->b_top_field_first = p_pic->b_top_field_first;
221 if( p_pic->pf_release )
222 p_pic->pf_release( p_pic );
227 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
228 vlc_value_t oldval, vlc_value_t newval,
231 filter_sys_t *p_sys = (filter_sys_t *)p_data;
233 if( !strcmp( psz_var, "rotate_angle" ) )
235 p_sys->i_angle = newval.i_int;