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