* rotate.c: Fix a few bugs + don't use p_libvlc_global to store instance specific...
[vlc.git] / 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_vout.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  * filter_sys_t
77  *****************************************************************************/
78 struct filter_sys_t
79 {
80     int     i_angle;
81     int     i_cos;
82     int     i_sin;
83 };
84
85 static inline void cache_trigo( int i_angle, int *i_sin, int *i_cos )
86 {
87     const double f_angle = (((double)i_angle)*M_PI)/180.;
88     *i_sin = (int)(sin( f_angle )*256.);
89     *i_cos = (int)(cos( f_angle )*256.);
90 }
91
92 /*****************************************************************************
93  * Create: allocates Distort video filter
94  *****************************************************************************/
95 static int Create( vlc_object_t *p_this )
96 {
97     filter_t *p_filter = (filter_t *)p_this;
98     filter_sys_t *p_sys;
99
100     /* Allocate structure */
101     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
102     if( p_filter->p_sys == NULL )
103     {
104         msg_Err( p_filter, "out of memory" );
105         return VLC_ENOMEM;
106     }
107     p_sys = p_filter->p_sys;
108
109     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
110                        p_filter->p_cfg );
111
112     p_sys->i_angle = var_CreateGetIntegerCommand( p_filter,
113                                                   FILTER_PREFIX "angle" );
114     var_AddCallback( p_filter, FILTER_PREFIX "angle", RotateCallback, p_sys );
115
116     cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );
117
118     p_filter->pf_video_filter = Filter;
119
120     return VLC_SUCCESS;
121 }
122
123 /*****************************************************************************
124  * Destroy: destroy Distort filter
125  *****************************************************************************/
126 static void Destroy( vlc_object_t *p_this )
127 {
128     filter_t *p_filter = (filter_t *)p_this;
129
130     free( p_filter->p_sys );
131 }
132
133 /*****************************************************************************
134  *
135  *****************************************************************************/
136 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
137 {
138     picture_t *p_outpic;
139     filter_sys_t *p_sys = p_filter->p_sys;
140     int i_plane;
141     const int i_sin = p_sys->i_sin, i_cos = p_sys->i_cos;
142
143     if( !p_pic ) return NULL;
144
145     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
146     if( !p_outpic )
147     {
148         msg_Warn( p_filter, "can't get output picture" );
149         if( p_pic->pf_release )
150             p_pic->pf_release( p_pic );
151         return NULL;
152     }
153
154     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
155     {
156         const int i_visible_lines = p_pic->p[i_plane].i_visible_lines;
157         const int i_visible_pitch = p_pic->p[i_plane].i_visible_pitch;
158         const int i_pitch         = p_pic->p[i_plane].i_pitch;
159         const int i_hidden_pitch  = i_pitch - i_visible_pitch;
160
161         const int i_line_center = i_visible_lines>>1;
162         const int i_col_center  = i_visible_pitch>>1;
163
164         const uint8_t *p_in = p_pic->p[i_plane].p_pixels;
165         uint8_t *p_out = p_outpic->p[i_plane].p_pixels;
166         uint8_t *p_outendline = p_out + i_visible_pitch;
167         const uint8_t *p_outend = p_out + i_visible_lines * i_pitch;
168
169         const uint8_t black_pixel = ( i_plane == Y_PLANE ) ? 0x00 : 0x80;
170
171         const int i_line_next =  i_cos-i_sin*i_visible_pitch;
172         const int i_col_next  = -i_sin-i_cos*i_visible_pitch;
173         int i_line_orig0 = - i_cos * i_line_center
174                            - i_sin * i_col_center + (1<<7);
175         int i_col_orig0 =    i_sin * i_line_center
176                            - i_cos * i_col_center + (1<<7);
177         for( ; p_outendline < p_outend;
178              p_out += i_hidden_pitch, p_outendline += i_pitch,
179              i_line_orig0 += i_line_next, i_col_orig0 += i_col_next )
180         {
181             for( ; p_out < p_outendline;
182                  p_out++, i_line_orig0 += i_sin, i_col_orig0 += i_cos )
183             {
184                 const int i_line_orig = (i_line_orig0>>8) + i_line_center;
185                 const int i_col_orig  = (i_col_orig0>>8)  + i_col_center;
186
187                 if(    0 <= i_line_orig && i_line_orig < i_visible_lines
188                     && 0 <= i_col_orig  && i_col_orig  < i_visible_pitch )
189                 {
190                     *p_out = p_in[i_line_orig*i_pitch+i_col_orig];
191                 }
192                 else
193                 {
194                     *p_out = black_pixel;
195                 }
196             }
197         }
198     }
199
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;
205
206     if( p_pic->pf_release )
207         p_pic->pf_release( p_pic );
208
209     return p_outpic;
210 }
211
212 /*****************************************************************************
213  *
214  *****************************************************************************/
215 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
216                            vlc_value_t oldval, vlc_value_t newval,
217                            void *p_data )
218 {
219     filter_sys_t *p_sys = (filter_sys_t *)p_data;
220
221     if( !strcmp( psz_var, "rotate-angle" ) )
222     {
223         p_sys->i_angle = newval.i_int;
224
225         cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );
226     }
227     return VLC_SUCCESS;
228 }