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