]> git.sesse.net Git - vlc/blob - modules/video_filter/rotate.c
Motion control interface can now be used with the rotate video filter. (Should be...
[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     mtime_t last_date;
85 };
86
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 )
93 {
94     filter_t *p_filter = (filter_t *)p_this;
95
96     /* Allocate structure */
97     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
98     if( p_filter->p_sys == NULL )
99     {
100         msg_Err( p_filter, "out of memory" );
101         return VLC_ENOMEM;
102     }
103
104     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
105                        p_filter->p_cfg );
106     var_Create( p_filter, FILTER_PREFIX "angle",
107                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
108
109     p_filter->pf_video_filter = Filter;
110
111     p_filter->p_sys->i_angle = var_GetInteger( p_filter,
112                                                FILTER_PREFIX "angle" );
113     p_filter->p_sys->last_date = 0;
114
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 );
120
121     return VLC_SUCCESS;
122 }
123
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 )
130 {
131     filter_t *p_filter = (filter_t *)p_this;
132
133     var_DelCallback( p_filter->p_libvlc,
134                      "rotate_angle", RotateCallback, p_filter->p_sys );
135     free( p_filter->p_sys );
136 }
137
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
143  * frame.
144  *****************************************************************************/
145 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
146 {
147     picture_t *p_outpic;
148     int i_index;
149     double f_angle;
150     double f_sin, f_cos;
151     mtime_t new_date = mdate();
152
153     if( !p_pic ) return NULL;
154
155     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
156     if( !p_outpic )
157     {
158         msg_Warn( p_filter, "can't get output picture" );
159         if( p_pic->pf_release )
160             p_pic->pf_release( p_pic );
161         return NULL;
162     }
163
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 );
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         /* 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++ )
187         {
188             for( i_col = 0; i_col < i_num_cols ; i_col++ )
189             {
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)
193                                    + 0.5 )
194                               + i_line_center;
195                 i_col_orig = (int)(-f_sin * (double)(i_line-i_line_center)
196                                   + f_cos * (double)(i_col-i_col_center)
197                                   + 0.5 )
198                              + i_col_center;
199
200                 if(    0 <= i_line_orig && i_line_orig < i_num_lines
201                     && 0 <= i_col_orig && i_col_orig < i_num_cols )
202                 {
203
204                     p_out[i_line*i_num_cols+i_col] =
205                         p_in[i_line_orig*i_num_cols+i_col_orig];
206                 }
207                 else
208                 {
209                     p_out[i_line*i_num_cols+i_col] = black_pixel;
210                 }
211             }
212         }
213     }
214
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;
220
221     if( p_pic->pf_release )
222         p_pic->pf_release( p_pic );
223
224     return p_outpic;
225 }
226
227 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
228                            vlc_value_t oldval, vlc_value_t newval,
229                            void *p_data )
230 {
231     filter_sys_t *p_sys = (filter_sys_t *)p_data;
232
233     if( !strcmp( psz_var, "rotate_angle" ) )
234     {
235         p_sys->i_angle = newval.i_int;
236     }
237     return VLC_SUCCESS;
238 }