]> git.sesse.net Git - vlc/blob - modules/video_filter/rotate.c
Remove _GNU_SOURCE and string.h too
[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
28 #include <math.h>                                            /* sin(), cos() */
29
30 #include <vlc/vlc.h>
31 #include <vlc_vout.h>
32
33 #include "vlc_filter.h"
34
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 static int  Create    ( vlc_object_t * );
39 static void Destroy   ( vlc_object_t * );
40
41 static picture_t *Filter( filter_t *, picture_t * );
42
43 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
44                            vlc_value_t oldval, vlc_value_t newval,
45                            void *p_data );
46
47 static int PreciseRotateCallback( vlc_object_t *p_this, char const *psz_var,
48                            vlc_value_t oldval, vlc_value_t newval,
49                            void *p_data );
50
51 #define ANGLE_TEXT N_("Angle in degrees")
52 #define ANGLE_LONGTEXT N_("Angle in degrees (0 to 359)")
53
54 #define FILTER_PREFIX "rotate-"
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59 vlc_module_begin();
60     set_description( _("Rotate video filter") );
61     set_shortname( _( "Rotate" ));
62     set_capability( "video filter2", 0 );
63     set_category( CAT_VIDEO );
64     set_subcategory( SUBCAT_VIDEO_VFILTER );
65
66     add_integer_with_range( FILTER_PREFIX "angle", 0, 0, 359, NULL,
67         ANGLE_TEXT, ANGLE_LONGTEXT, VLC_FALSE );
68
69     add_shortcut( "rotate" );
70     set_callbacks( Create, Destroy );
71 vlc_module_end();
72
73 static const char *ppsz_filter_options[] = {
74     "angle", NULL
75 };
76
77 /*****************************************************************************
78  * filter_sys_t
79  *****************************************************************************/
80 struct filter_sys_t
81 {
82     int     i_angle;
83     int     i_cos;
84     int     i_sin;
85 };
86
87 static inline void cache_trigo( int i_angle, int *i_sin, int *i_cos )
88 {
89     const double f_angle = (((double)i_angle)*M_PI)/1800.;
90     *i_sin = (int)(sin( f_angle )*4096.);
91     *i_cos = (int)(cos( f_angle )*4096.);
92 }
93
94 /*****************************************************************************
95  * Create: allocates Distort video filter
96  *****************************************************************************/
97 static int Create( vlc_object_t *p_this )
98 {
99     filter_t *p_filter = (filter_t *)p_this;
100     filter_sys_t *p_sys;
101
102     /* Allocate structure */
103     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
104     if( p_filter->p_sys == NULL )
105     {
106         msg_Err( p_filter, "out of memory" );
107         return VLC_ENOMEM;
108     }
109     p_sys = p_filter->p_sys;
110
111     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
112                        p_filter->p_cfg );
113
114     p_sys->i_angle = var_CreateGetIntegerCommand( p_filter,
115                                                   FILTER_PREFIX "angle" ) * 10;
116     var_CreateGetIntegerCommand( p_filter, FILTER_PREFIX "deciangle" );
117     var_AddCallback( p_filter, FILTER_PREFIX "angle", RotateCallback, p_sys );
118     var_AddCallback( p_filter, FILTER_PREFIX "deciangle", PreciseRotateCallback, p_sys );
119
120     cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );
121
122     p_filter->pf_video_filter = Filter;
123
124     return VLC_SUCCESS;
125 }
126
127 /*****************************************************************************
128  * Destroy: destroy Distort filter
129  *****************************************************************************/
130 static void Destroy( vlc_object_t *p_this )
131 {
132     filter_t *p_filter = (filter_t *)p_this;
133
134     free( p_filter->p_sys );
135 }
136
137 /*****************************************************************************
138  *
139  *****************************************************************************/
140 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
141 {
142     picture_t *p_outpic;
143     filter_sys_t *p_sys = p_filter->p_sys;
144     int i_plane;
145     const int i_sin = p_sys->i_sin, i_cos = p_sys->i_cos;
146
147     if( !p_pic ) return NULL;
148
149     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
150     if( !p_outpic )
151     {
152         msg_Warn( p_filter, "can't get output picture" );
153         if( p_pic->pf_release )
154             p_pic->pf_release( p_pic );
155         return NULL;
156     }
157
158     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
159     {
160         const int i_visible_lines = p_pic->p[i_plane].i_visible_lines;
161         const int i_visible_pitch = p_pic->p[i_plane].i_visible_pitch;
162         const int i_pitch         = p_pic->p[i_plane].i_pitch;
163         const int i_hidden_pitch  = i_pitch - i_visible_pitch;
164
165         const int i_line_center = i_visible_lines>>1;
166         const int i_col_center  = i_visible_pitch>>1;
167
168         const uint8_t *p_in = p_pic->p[i_plane].p_pixels;
169         uint8_t *p_out = p_outpic->p[i_plane].p_pixels;
170         uint8_t *p_outendline = p_out + i_visible_pitch;
171         const uint8_t *p_outend = p_out + i_visible_lines * i_pitch;
172
173         const uint8_t black_pixel = ( i_plane == Y_PLANE ) ? 0x00 : 0x80;
174
175         const int i_line_next =  i_cos-i_sin*i_visible_pitch;
176         const int i_col_next  = -i_sin-i_cos*i_visible_pitch;
177         int i_line_orig0 = - i_cos * i_line_center
178                            - i_sin * i_col_center + (1<<11);
179         int i_col_orig0 =    i_sin * i_line_center
180                            - i_cos * i_col_center + (1<<11);
181         for( ; p_outendline < p_outend;
182              p_out += i_hidden_pitch, p_outendline += i_pitch,
183              i_line_orig0 += i_line_next, i_col_orig0 += i_col_next )
184         {
185             for( ; p_out < p_outendline;
186                  p_out++, i_line_orig0 += i_sin, i_col_orig0 += i_cos )
187             {
188                 const int i_line_orig = (i_line_orig0>>12) + i_line_center;
189                 const int i_col_orig  = (i_col_orig0>>12)  + i_col_center;
190                 const uint8_t* p_orig_offset = p_in + i_line_orig * i_pitch
191                                                 + i_col_orig;
192                 const uint8_t i_line_percent = (i_line_orig0>>4) & 255;
193                 const uint8_t i_col_percent  = (i_col_orig0 >>4) & 255;
194
195                 if(    -1 <= i_line_orig && i_line_orig < i_visible_lines
196                     && -1 <= i_col_orig  && i_col_orig  < i_visible_pitch )
197                 {
198                 #define test 1
199                 #undef test
200                 #ifdef test
201                     if( ( i_col_orig > i_visible_pitch/2 ) )
202                 #endif
203                     {
204                         uint8_t i_curpix = black_pixel;
205                         uint8_t i_colpix = black_pixel;
206                         uint8_t i_linpix = black_pixel;
207                         uint8_t i_nexpix = black_pixel;
208                         if( ( 0 <= i_line_orig ) && ( 0 <= i_col_orig ) )
209                             i_curpix = *p_orig_offset;
210                         p_orig_offset++;
211
212                         if(  ( i_col_orig < i_visible_pitch - 1)
213                              && ( i_line_orig >= 0 ) )
214                             i_colpix=*p_orig_offset;
215
216                         p_orig_offset+=i_pitch;
217                         if( ( i_line_orig < i_visible_lines - 1)
218                             && ( i_col_orig  < i_visible_pitch - 1) )
219                             i_nexpix=*p_orig_offset;
220
221                         p_orig_offset--;
222                         if(  ( i_line_orig < i_visible_lines - 1)
223                              && ( i_col_orig >= 0 ) )
224                             i_linpix=*p_orig_offset;
225
226                         unsigned int temp = 0;
227                         temp+= i_curpix *
228                             (256 - i_line_percent) * ( 256 - i_col_percent );
229                         temp+= i_linpix *
230                             i_line_percent * (256 - i_col_percent );
231                         temp+= i_nexpix *
232                             ( i_col_percent) * ( i_line_percent);
233                         temp+= i_colpix *
234                             i_col_percent * (256 - i_line_percent );
235                         *p_out = temp >> 16;
236                     }
237                 #ifdef test
238                     else if (i_col_orig == i_visible_pitch/2 )
239                     {   *p_out = black_pixel;
240                     }
241                     else
242                         *p_out = *p_orig_offset;
243                 #endif
244                 #undef test
245                 }
246                 else
247                 {
248                     *p_out = black_pixel;
249                 }
250             }
251         }
252     }
253
254     p_outpic->date = p_pic->date;
255     p_outpic->b_force = p_pic->b_force;
256     p_outpic->i_nb_fields = p_pic->i_nb_fields;
257     p_outpic->b_progressive = p_pic->b_progressive;
258     p_outpic->b_top_field_first = p_pic->b_top_field_first;
259
260     if( p_pic->pf_release )
261         p_pic->pf_release( p_pic );
262
263     return p_outpic;
264 }
265
266 /*****************************************************************************
267  *
268  *****************************************************************************/
269 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
270                            vlc_value_t oldval, vlc_value_t newval,
271                            void *p_data )
272 {
273     filter_sys_t *p_sys = (filter_sys_t *)p_data;
274
275     if( !strcmp( psz_var, "rotate-angle" ) )
276     {
277         p_sys->i_angle = newval.i_int*10;
278
279         cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );
280     }
281     return VLC_SUCCESS;
282 }
283 static int PreciseRotateCallback( vlc_object_t *p_this, char const *psz_var,
284                            vlc_value_t oldval, vlc_value_t newval,
285                            void *p_data )
286 {
287     filter_sys_t *p_sys = (filter_sys_t *)p_data;
288
289     if( !strcmp( psz_var, "rotate-deciangle" ) )
290     {
291         p_sys->i_angle = newval.i_int;
292         cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );
293     }
294     return VLC_SUCCESS;
295 }