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 #define ANGLE_TEXT N_("Angle in degrees")
46 #define ANGLE_LONGTEXT N_("Angle in degrees (0 to 359)")
48 #define FILTER_PREFIX "rotate-"
50 /*****************************************************************************
52 *****************************************************************************/
54 set_description( _("Rotate video filter") );
55 set_shortname( _( "Rotate" ));
56 set_capability( "video filter2", 0 );
57 set_category( CAT_VIDEO );
58 set_subcategory( SUBCAT_VIDEO_VFILTER );
60 add_integer_with_range( FILTER_PREFIX "angle", 0, 0, 359, NULL,
61 ANGLE_TEXT, ANGLE_LONGTEXT, VLC_FALSE );
63 add_shortcut( "rotate" );
64 set_callbacks( Create, Destroy );
67 static const char *ppsz_filter_options[] = {
71 /*****************************************************************************
72 * vout_sys_t: Distort video output method descriptor
73 *****************************************************************************
74 * This structure is part of the video output thread descriptor.
75 * It describes the Distort specific properties of an output thread.
76 *****************************************************************************/
83 /*****************************************************************************
84 * Create: allocates Distort video thread output method
85 *****************************************************************************
86 * This function allocates and initializes a Distort vout method.
87 *****************************************************************************/
88 static int Create( vlc_object_t *p_this )
90 filter_t *p_filter = (filter_t *)p_this;
92 /* Allocate structure */
93 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
94 if( p_filter->p_sys == NULL )
96 msg_Err( p_filter, "out of memory" );
100 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
102 var_Create( p_filter, FILTER_PREFIX "angle",
103 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
105 p_filter->pf_video_filter = Filter;
107 p_filter->p_sys->i_angle = var_GetInteger( p_filter,
108 FILTER_PREFIX "angle" );
109 p_filter->p_sys->last_date = 0;
114 /*****************************************************************************
115 * Destroy: destroy Distort video thread output method
116 *****************************************************************************
117 * Terminate an output method created by DistortCreateOutputMethod
118 *****************************************************************************/
119 static void Destroy( vlc_object_t *p_this )
121 filter_t *p_filter = (filter_t *)p_this;
122 free( p_filter->p_sys );
125 /*****************************************************************************
126 * Render: displays previously rendered output
127 *****************************************************************************
128 * This function send the currently rendered image to Distort image, waits
129 * until it is displayed and switch the two rendering buffers, preparing next
131 *****************************************************************************/
132 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
138 mtime_t new_date = mdate();
140 if( !p_pic ) return NULL;
142 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
145 msg_Warn( p_filter, "can't get output picture" );
146 if( p_pic->pf_release )
147 p_pic->pf_release( p_pic );
151 p_filter->p_sys->last_date = new_date;
152 f_angle = (double)p_filter->p_sys->i_angle;
153 f_sin = sin( f_angle );
154 f_cos = cos( f_angle );
156 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
158 int i_line, i_num_lines, i_line_center, i_col, i_num_cols, i_col_center;
160 uint8_t *p_in, *p_out;
162 p_in = p_pic->p[i_index].p_pixels;
163 p_out = p_outpic->p[i_index].p_pixels;
165 i_num_lines = p_pic->p[i_index].i_visible_lines;
166 i_num_cols = p_pic->p[i_index].i_visible_pitch;
167 i_line_center = i_num_lines/2;
168 i_col_center = i_num_cols/2;
170 black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
172 /* Ok, we do 3 times the sin() calculation for each line. So what ? */
173 for( i_line = 0 ; i_line < i_num_lines ; i_line++ )
175 for( i_col = 0; i_col < i_num_cols ; i_col++ )
177 int i_line_orig, i_col_orig;
178 i_line_orig = (int)( f_cos * (double)(i_line-i_line_center)
179 + f_sin * (double)(i_col-i_col_center) )
181 i_col_orig = (int)(-f_sin * (double)(i_line-i_line_center)
182 + f_cos * (double)(i_col-i_col_center) )
185 if( 0 <= i_line_orig && i_line_orig < i_num_lines
186 && 0 <= i_col_orig && i_col_orig < i_num_cols )
189 p_out[i_line*i_num_cols+i_col] =
190 p_in[i_line_orig*i_num_cols+i_col_orig];
194 p_out[i_line*i_num_cols+i_col] = black_pixel;
200 p_outpic->date = p_pic->date;
201 p_outpic->b_force = p_pic->b_force;
202 p_outpic->i_nb_fields = p_pic->i_nb_fields;
203 p_outpic->b_progressive = p_pic->b_progressive;
204 p_outpic->b_top_field_first = p_pic->b_top_field_first;
206 if( p_pic->pf_release )
207 p_pic->pf_release( p_pic );