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