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