]> git.sesse.net Git - vlc/blob - modules/video_filter/rotate.c
Add some antialiasing to rotate
[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                 int i_line_percent = (i_line_orig0>>4) & 255;
193                 int i_col_percent  = (i_col_orig0 >>4) & 255;
194
195                 if(    0 < i_line_orig && i_line_orig < i_visible_lines - 1
196                     && 0 < i_col_orig  && i_col_orig  < i_visible_pitch - 1)
197                 {
198                 #define test 1
199                 #undef test
200                 #ifdef test
201                     if( ( i_col_orig > i_visible_pitch/2 ) )
202                 #endif
203                     {
204                         unsigned int temp = 0;
205                         temp+= p_in[i_line_orig*i_pitch+i_col_orig] *
206                             (256 - i_line_percent) * ( 256 - i_col_percent );
207                         temp+= p_in[(i_line_orig+1)*i_pitch+i_col_orig] *
208                             i_line_percent * (256 - i_col_percent );
209                         temp+= p_in[(i_line_orig+1)*i_pitch+i_col_orig+1] *
210                             ( i_col_percent) * ( i_line_percent);
211                         temp+= p_in[i_line_orig*i_pitch+i_col_orig+1] *
212                             i_col_percent * (256 - i_line_percent );
213                         *p_out = temp >> 16;
214                     }
215                 #ifdef test
216                     else if (i_col_orig == i_visible_pitch/2 )
217                     {   *p_out = black_pixel;
218                     }
219                     else
220                         *p_out = p_in[i_line_orig*i_pitch+i_col_orig];
221                 #endif
222                 #undef test
223                 }
224                 else
225                 {
226                     *p_out = black_pixel;
227                 }
228             }
229         }
230     }
231
232     p_outpic->date = p_pic->date;
233     p_outpic->b_force = p_pic->b_force;
234     p_outpic->i_nb_fields = p_pic->i_nb_fields;
235     p_outpic->b_progressive = p_pic->b_progressive;
236     p_outpic->b_top_field_first = p_pic->b_top_field_first;
237
238     if( p_pic->pf_release )
239         p_pic->pf_release( p_pic );
240
241     return p_outpic;
242 }
243
244 /*****************************************************************************
245  *
246  *****************************************************************************/
247 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
248                            vlc_value_t oldval, vlc_value_t newval,
249                            void *p_data )
250 {
251     filter_sys_t *p_sys = (filter_sys_t *)p_data;
252
253     if( !strcmp( psz_var, "rotate-angle" ) )
254     {
255         p_sys->i_angle = newval.i_int*10;
256
257         cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );
258     }
259     return VLC_SUCCESS;
260 }
261 static int PreciseRotateCallback( vlc_object_t *p_this, char const *psz_var,
262                            vlc_value_t oldval, vlc_value_t newval,
263                            void *p_data )
264 {
265     filter_sys_t *p_sys = (filter_sys_t *)p_data;
266
267     if( !strcmp( psz_var, "rotate-deciangle" ) )
268     {
269         p_sys->i_angle = newval.i_int;
270
271         cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );
272     }
273     return VLC_SUCCESS;
274 }