]> git.sesse.net Git - vlc/blob - modules/video_filter/rotate.c
Don't use doubles when doing the rotation
[vlc] / modules / video_filter / rotate.c
1 /*****************************************************************************
2  * rotate.c : video rotation filter
3  *****************************************************************************
4  * Copyright (C) 2000-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <math.h>                                            /* sin(), cos() */
31
32 #include <vlc/vlc.h>
33 #include <vlc/decoder.h>
34
35 #include "vlc_filter.h"
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int  Create    ( vlc_object_t * );
41 static void Destroy   ( vlc_object_t * );
42
43 static picture_t *Filter( filter_t *, picture_t * );
44
45 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
46                            vlc_value_t oldval, vlc_value_t newval,
47                            void *p_data );
48
49 #define ANGLE_TEXT N_("Angle in degrees")
50 #define ANGLE_LONGTEXT N_("Angle in degrees (0 to 359)")
51
52 #define FILTER_PREFIX "rotate-"
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 vlc_module_begin();
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 );
63
64     add_integer_with_range( FILTER_PREFIX "angle", 0, 0, 359, NULL,
65         ANGLE_TEXT, ANGLE_LONGTEXT, VLC_FALSE );
66
67     add_shortcut( "rotate" );
68     set_callbacks( Create, Destroy );
69 vlc_module_end();
70
71 static const char *ppsz_filter_options[] = {
72     "angle", NULL
73 };
74
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  *****************************************************************************/
81 struct filter_sys_t
82 {
83     int     i_angle;
84     int     i_cos;
85     int     i_sin;
86
87     mtime_t last_date;
88 };
89
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 )
96 {
97     filter_t *p_filter = (filter_t *)p_this;
98
99     /* Allocate structure */
100     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
101     if( p_filter->p_sys == NULL )
102     {
103         msg_Err( p_filter, "out of memory" );
104         return VLC_ENOMEM;
105     }
106
107     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
108                        p_filter->p_cfg );
109     var_Create( p_filter, FILTER_PREFIX "angle",
110                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
111
112     p_filter->pf_video_filter = Filter;
113
114     p_filter->p_sys->i_angle = var_GetInteger( p_filter,
115                                                FILTER_PREFIX "angle" );
116     p_filter->p_sys->last_date = 0;
117
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 );
123
124     return VLC_SUCCESS;
125 }
126
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 )
133 {
134     filter_t *p_filter = (filter_t *)p_this;
135
136     var_DelCallback( p_filter->p_libvlc,
137                      "rotate_angle", RotateCallback, p_filter->p_sys );
138     free( p_filter->p_sys );
139 }
140
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
146  * frame.
147  *****************************************************************************/
148 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
149 {
150     picture_t *p_outpic;
151     filter_sys_t *p_sys = p_filter->p_sys;
152     int i_index;
153     int i_sin = p_sys->i_sin, i_cos = p_sys->i_cos;
154     mtime_t new_date = mdate();
155
156     if( !p_pic ) return NULL;
157
158     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
159     if( !p_outpic )
160     {
161         msg_Warn( p_filter, "can't get output picture" );
162         if( p_pic->pf_release )
163             p_pic->pf_release( p_pic );
164         return NULL;
165     }
166
167     p_sys->last_date = new_date;
168
169     for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
170     {
171         int i_line, i_num_lines, i_line_center, i_col, i_num_cols, i_col_center;
172         uint8_t black_pixel;
173         uint8_t *p_in, *p_out;
174
175         p_in = p_pic->p[i_index].p_pixels;
176         p_out = p_outpic->p[i_index].p_pixels;
177
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;
182
183         black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
184
185         for( i_line = 0 ; i_line < i_num_lines ; i_line++ )
186         {
187             for( i_col = 0; i_col < i_num_cols ; i_col++ )
188             {
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)
192                                 + 128 )>>8 )
193                               + i_line_center;
194                 i_col_orig = ( (-i_sin * (i_line-i_line_center)
195                                + i_cos * (i_col-i_col_center)
196                                + 128 )>>8 )
197                              + i_col_center;
198
199                 if(    0 <= i_line_orig && i_line_orig < i_num_lines
200                     && 0 <= i_col_orig && i_col_orig < i_num_cols )
201                 {
202
203                     p_out[i_line*i_num_cols+i_col] =
204                         p_in[i_line_orig*i_num_cols+i_col_orig];
205                 }
206                 else
207                 {
208                     p_out[i_line*i_num_cols+i_col] = black_pixel;
209                 }
210             }
211         }
212     }
213
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;
219
220     if( p_pic->pf_release )
221         p_pic->pf_release( p_pic );
222
223     return p_outpic;
224 }
225
226 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
227                            vlc_value_t oldval, vlc_value_t newval,
228                            void *p_data )
229 {
230     filter_sys_t *p_sys = (filter_sys_t *)p_data;
231
232     if( !strcmp( psz_var, "rotate_angle" ) )
233     {
234         double f_angle;
235
236         p_sys->i_angle = newval.i_int;
237
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.);
241     }
242     return VLC_SUCCESS;
243 }