Add blending for borders in rotate
[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 #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
196                     && 0 <= i_col_orig  && i_col_orig  < i_visible_pitch )
197                 {
198                     uint8_t i_curpix = black_pixel;
199                     uint8_t i_colpix = black_pixel;
200                     uint8_t i_linpix = black_pixel;
201                     uint8_t i_nexpix = black_pixel;
202                     if( ( 0 < i_line_orig ) && ( 0 < i_col_orig ) )
203                         i_curpix = p_in[i_line_orig*i_pitch+i_col_orig];
204                     if( i_line_orig < i_visible_lines - 1)
205                     {
206                         i_linpix=p_in[(i_line_orig+1)*i_pitch+i_col_orig];
207
208                     }
209                     if( i_col_orig < i_visible_pitch - 1)
210                     {
211                         i_colpix=p_in[i_line_orig*i_pitch+i_col_orig+1];
212                     }
213                     if( ( i_line_orig < i_visible_lines - 1)
214                      && ( i_col_orig  < i_visible_pitch - 1) )
215                         i_nexpix = p_in[(i_line_orig+1)*i_pitch+i_col_orig+1];
216
217                 #define test 1
218                 #undef test
219                 #ifdef test
220                     if( ( i_col_orig > i_visible_pitch/2 ) )
221                 #endif
222                     {
223                         unsigned int temp = 0;
224                         temp+= i_curpix *
225                             (256 - i_line_percent) * ( 256 - i_col_percent );
226                         temp+= i_linpix *
227                             i_line_percent * (256 - i_col_percent );
228                         temp+= i_nexpix *
229                             ( i_col_percent) * ( i_line_percent);
230                         temp+= i_colpix *
231                             i_col_percent * (256 - i_line_percent );
232                         *p_out = temp >> 16;
233                     }
234                 #ifdef test
235                     else if (i_col_orig == i_visible_pitch/2 )
236                     {   *p_out = black_pixel;
237                     }
238                     else
239                         *p_out = p_in[i_line_orig*i_pitch+i_col_orig];
240                 #endif
241                 #undef test
242                 }
243                 else
244                 {
245                     *p_out = black_pixel;
246                 }
247             }
248         }
249     }
250
251     p_outpic->date = p_pic->date;
252     p_outpic->b_force = p_pic->b_force;
253     p_outpic->i_nb_fields = p_pic->i_nb_fields;
254     p_outpic->b_progressive = p_pic->b_progressive;
255     p_outpic->b_top_field_first = p_pic->b_top_field_first;
256
257     if( p_pic->pf_release )
258         p_pic->pf_release( p_pic );
259
260     return p_outpic;
261 }
262
263 /*****************************************************************************
264  *
265  *****************************************************************************/
266 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
267                            vlc_value_t oldval, vlc_value_t newval,
268                            void *p_data )
269 {
270     filter_sys_t *p_sys = (filter_sys_t *)p_data;
271
272     if( !strcmp( psz_var, "rotate-angle" ) )
273     {
274         p_sys->i_angle = newval.i_int*10;
275
276         cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );
277     }
278     return VLC_SUCCESS;
279 }
280 static int PreciseRotateCallback( vlc_object_t *p_this, char const *psz_var,
281                            vlc_value_t oldval, vlc_value_t newval,
282                            void *p_data )
283 {
284     filter_sys_t *p_sys = (filter_sys_t *)p_data;
285
286     if( !strcmp( psz_var, "rotate-deciangle" ) )
287     {
288         p_sys->i_angle = newval.i_int;
289
290         cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );
291     }
292     return VLC_SUCCESS;
293 }