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