Global headers.
[vlc.git] / modules / video_filter / rotate.c
1 /*****************************************************************************
2  * rotate.c : video rotation filter
3  *****************************************************************************
4  * Copyright (C) 2000-2008 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 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <math.h>                                            /* sin(), cos() */
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36
37 #include <vlc_filter.h>
38 #include "filter_picture.h"
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static int  Create    ( vlc_object_t * );
44 static void Destroy   ( vlc_object_t * );
45
46 static picture_t *Filter( filter_t *, picture_t * );
47 static picture_t *FilterPacked( 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     vlc_spinlock_t lock;
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     if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
109     {
110         msg_Err( p_filter, "Input and output chromas don't match" );
111         return VLC_EGENERIC;
112     }
113
114     switch( p_filter->fmt_in.video.i_chroma )
115     {
116         CASE_PLANAR_YUV
117             p_filter->pf_video_filter = Filter;
118             break;
119
120         CASE_PACKED_YUV_422
121             p_filter->pf_video_filter = FilterPacked;
122             break;
123
124         default:
125             msg_Err( p_filter, "Unsupported input chroma (%4s)",
126                      (char*)&(p_filter->fmt_in.video.i_chroma) );
127             return VLC_EGENERIC;
128     }
129
130     /* Allocate structure */
131     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
132     if( p_filter->p_sys == NULL )
133         return VLC_ENOMEM;
134     p_sys = p_filter->p_sys;
135
136     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
137                        p_filter->p_cfg );
138
139     int i_angle = var_CreateGetIntegerCommand( p_filter,
140                                                FILTER_PREFIX "angle" ) * 10;
141     cache_trigo( i_angle, &p_sys->i_sin, &p_sys->i_cos );
142     var_Create( p_filter, FILTER_PREFIX "deciangle",
143                 VLC_VAR_INTEGER|VLC_VAR_ISCOMMAND );
144     vlc_spin_init( &p_sys->lock );
145     var_AddCallback( p_filter, FILTER_PREFIX "angle", RotateCallback, p_sys );
146     var_AddCallback( p_filter, FILTER_PREFIX "deciangle",
147                      PreciseRotateCallback, p_sys );
148
149
150     return VLC_SUCCESS;
151 }
152
153 /*****************************************************************************
154  * Destroy: destroy Distort filter
155  *****************************************************************************/
156 static void Destroy( vlc_object_t *p_this )
157 {
158     filter_t *p_filter = (filter_t *)p_this;
159
160     var_DelCallback( p_filter, FILTER_PREFIX "angle", RotateCallback, p_filter->p_sys );
161     var_DelCallback( p_filter, FILTER_PREFIX "deciangle",
162                      PreciseRotateCallback, p_filter->p_sys );
163     vlc_spin_destroy( &p_filter->p_sys->lock );
164     free( p_filter->p_sys );
165 }
166
167 /*****************************************************************************
168  *
169  *****************************************************************************/
170 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
171 {
172     picture_t *p_outpic;
173     filter_sys_t *p_sys = p_filter->p_sys;
174     int i_sin, i_cos;
175
176     if( !p_pic ) return NULL;
177
178     p_outpic = filter_NewPicture( p_filter );
179     if( !p_outpic )
180     {
181         picture_Release( p_pic );
182         return NULL;
183     }
184
185     vlc_spin_lock( &p_sys->lock );
186     i_sin = p_sys->i_sin;
187     i_cos = p_sys->i_cos;
188     vlc_spin_unlock( &p_sys->lock );
189
190     for( int i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
191     {
192         const int i_visible_lines = p_pic->p[i_plane].i_visible_lines;
193         const int i_visible_pitch = p_pic->p[i_plane].i_visible_pitch;
194         const int i_pitch         = p_pic->p[i_plane].i_pitch;
195         const int i_hidden_pitch  = i_pitch - i_visible_pitch;
196
197         const int i_aspect = __MAX( 1, ( i_visible_lines * p_pic->p[Y_PLANE].i_visible_pitch ) / ( p_pic->p[Y_PLANE].i_visible_lines * i_visible_pitch ));
198         /* = 2 for U and V planes in YUV 4:2:2, = 1 otherwise */
199
200         const int i_line_center = i_visible_lines>>1;
201         const int i_col_center  = i_visible_pitch>>1;
202
203         const uint8_t *p_in = p_pic->p[i_plane].p_pixels;
204         uint8_t *p_out = p_outpic->p[i_plane].p_pixels;
205         uint8_t *p_outendline = p_out + i_visible_pitch;
206         const uint8_t *p_outend = p_out + i_visible_lines * i_pitch;
207
208         const uint8_t black_pixel = ( i_plane == Y_PLANE ) ? 0x00 : 0x80;
209
210         const int i_line_next =  i_cos / i_aspect -i_sin*i_visible_pitch;
211         const int i_col_next  = -i_sin / i_aspect -i_cos*i_visible_pitch;
212         int i_line_orig0 = ( - i_cos * i_line_center / i_aspect
213                              - i_sin * i_col_center + (1<<11) );
214         int i_col_orig0 =    i_sin * i_line_center / i_aspect
215                            - i_cos * i_col_center + (1<<11);
216         for( ; p_outendline < p_outend;
217              p_out += i_hidden_pitch, p_outendline += i_pitch,
218              i_line_orig0 += i_line_next, i_col_orig0 += i_col_next )
219         {
220             for( ; p_out < p_outendline;
221                  p_out++, i_line_orig0 += i_sin, i_col_orig0 += i_cos )
222             {
223                 const int i_line_orig = (i_line_orig0>>12)*i_aspect + i_line_center;
224                 const int i_col_orig  = (i_col_orig0>>12)  + i_col_center;
225                 const uint8_t* p_orig_offset = p_in + i_line_orig * i_pitch
226                                                 + i_col_orig;
227                 const uint8_t i_line_percent = (i_line_orig0>>4) & 255;
228                 const uint8_t i_col_percent  = (i_col_orig0 >>4) & 255;
229
230                 if(    -1 <= i_line_orig && i_line_orig < i_visible_lines
231                     && -1 <= i_col_orig  && i_col_orig  < i_visible_pitch )
232                 {
233                 #define test 1
234                 #undef test
235                 #ifdef test
236                     if( ( i_col_orig > i_visible_pitch/2 ) )
237                 #endif
238                     {
239                         uint8_t i_curpix = black_pixel;
240                         uint8_t i_colpix = black_pixel;
241                         uint8_t i_linpix = black_pixel;
242                         uint8_t i_nexpix = black_pixel;
243                         if( ( 0 <= i_line_orig ) && ( 0 <= i_col_orig ) )
244                             i_curpix = *p_orig_offset;
245                         p_orig_offset++;
246
247                         if(  ( i_col_orig < i_visible_pitch - 1)
248                              && ( i_line_orig >= 0 ) )
249                             i_colpix = *p_orig_offset;
250
251                         p_orig_offset+=i_pitch;
252                         if( ( i_line_orig < i_visible_lines - 1)
253                             && ( i_col_orig  < i_visible_pitch - 1) )
254                             i_nexpix = *p_orig_offset;
255
256                         p_orig_offset--;
257                         if(  ( i_line_orig < i_visible_lines - 1)
258                              && ( i_col_orig >= 0 ) )
259                             i_linpix = *p_orig_offset;
260
261                         unsigned int temp = 0;
262                         temp+= i_curpix *
263                             (256 - i_line_percent) * ( 256 - i_col_percent );
264                         temp+= i_linpix *
265                             i_line_percent * (256 - i_col_percent );
266                         temp+= i_nexpix *
267                             ( i_col_percent) * ( i_line_percent);
268                         temp+= i_colpix *
269                             i_col_percent * (256 - i_line_percent );
270                         *p_out = temp >> 16;
271                     }
272                 #ifdef test
273                     else if (i_col_orig == i_visible_pitch/2 )
274                     {   *p_out = black_pixel;
275                     }
276                     else
277                         *p_out = *p_orig_offset;
278                 #endif
279                 #undef test
280                 }
281                 else
282                 {
283                     *p_out = black_pixel;
284                 }
285             }
286         }
287     }
288
289     return CopyInfoAndRelease( p_outpic, p_pic );
290 }
291
292 /*****************************************************************************
293  *
294  *****************************************************************************/
295 static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
296 {
297     picture_t *p_outpic;
298     filter_sys_t *p_sys = p_filter->p_sys;
299     const int i_sin = p_sys->i_sin, i_cos = p_sys->i_cos;
300
301     if( !p_pic ) return NULL;
302
303     int i_u_offset, i_v_offset, i_y_offset;
304
305     if( GetPackedYuvOffsets( p_pic->format.i_chroma, &i_y_offset,
306                              &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
307     {
308         msg_Warn( p_filter, "Unsupported input chroma (%4s)",
309                   (char*)&(p_pic->format.i_chroma) );
310         picture_Release( p_pic );
311         return NULL;
312     }
313
314     p_outpic = filter_NewPicture( p_filter );
315     if( !p_outpic )
316     {
317         picture_Release( p_pic );
318         return NULL;
319     }
320
321     const uint8_t *p_in   = p_pic->p->p_pixels+i_y_offset;
322     const uint8_t *p_in_u = p_pic->p->p_pixels+i_u_offset;
323     const uint8_t *p_in_v = p_pic->p->p_pixels+i_v_offset;
324
325     const int i_pitch         = p_pic->p->i_pitch;
326     const int i_visible_pitch = p_pic->p->i_visible_pitch>>1; /* In fact it's i_visible_pixels */
327     const int i_visible_lines = p_pic->p->i_visible_lines;
328
329     uint8_t *p_out   = p_outpic->p->p_pixels+i_y_offset;
330     uint8_t *p_out_u = p_outpic->p->p_pixels+i_u_offset;
331     uint8_t *p_out_v = p_outpic->p->p_pixels+i_v_offset;
332
333     const int i_line_center = i_visible_lines>>1;
334     const int i_col_center  = i_visible_pitch>>1;
335
336     int i_col, i_line;
337     for( i_line = 0; i_line < i_visible_lines; i_line++ )
338     {
339         for( i_col = 0; i_col < i_visible_pitch; i_col++ )
340         {
341             int i_line_orig;
342             int i_col_orig;
343             /* Handle "1st Y", U and V */
344             i_line_orig = i_line_center +
345                 ( ( i_sin * ( i_col - i_col_center )
346                   + i_cos * ( i_line - i_line_center ) )>>12 );
347             i_col_orig = i_col_center +
348                 ( ( i_cos * ( i_col - i_col_center )
349                   - i_sin * ( i_line - i_line_center ) )>>12 );
350             if( 0 <= i_col_orig && i_col_orig < i_visible_pitch
351              && 0 <= i_line_orig && i_line_orig < i_visible_lines )
352             {
353                 p_out[i_line*i_pitch+2*i_col] = p_in[i_line_orig*i_pitch+2*i_col_orig];
354                 i_col_orig /= 2;
355                 p_out_u[i_line*i_pitch+2*i_col] = p_in_u[i_line_orig*i_pitch+4*i_col_orig];
356                 p_out_v[i_line*i_pitch+2*i_col] = p_in_v[i_line_orig*i_pitch+4*i_col_orig];
357             }
358             else
359             {
360                 p_out[i_line*i_pitch+2*i_col] = 0x00;
361                 p_out_u[i_line*i_pitch+2*i_col] = 0x80;
362                 p_out_v[i_line*i_pitch+2*i_col] = 0x80;
363             }
364
365             /* Handle "2nd Y" */
366             i_col++;
367             if( i_col >= i_visible_pitch )
368                 break;
369
370             i_line_orig = i_line_center +
371                 ( ( i_sin * ( i_col - i_col_center )
372                   + i_cos * ( i_line - i_line_center ) )>>12 );
373             i_col_orig = i_col_center +
374                 ( ( i_cos * ( i_col - i_col_center )
375                   - i_sin * ( i_line - i_line_center ) )>>12 );
376             if( 0 <= i_col_orig && i_col_orig < i_visible_pitch
377              && 0 <= i_line_orig && i_line_orig < i_visible_lines )
378             {
379                 p_out[i_line*i_pitch+2*i_col] = p_in[i_line_orig*i_pitch+2*i_col_orig];
380             }
381             else
382             {
383                 p_out[i_line*i_pitch+2*i_col] = 0x00;
384             }
385         }
386     }
387
388     return CopyInfoAndRelease( p_outpic, p_pic );
389 }
390
391 /*****************************************************************************
392  * Angle modification callbacks.
393  *****************************************************************************/
394 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
395                            vlc_value_t oldval, vlc_value_t newval,
396                            void *p_data )
397 {
398     oldval.i_int *= 10;
399     newval.i_int *= 10;
400     return PreciseRotateCallback( p_this, psz_var, oldval, newval, p_data );
401 }
402
403 static int PreciseRotateCallback( vlc_object_t *p_this, char const *psz_var,
404                            vlc_value_t oldval, vlc_value_t newval,
405                            void *p_data )
406 {
407     VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
408     filter_sys_t *p_sys = (filter_sys_t *)p_data;
409     int i_sin, i_cos;
410
411     cache_trigo( newval.i_int, &i_sin, &i_cos );
412     vlc_spin_lock( &p_sys->lock );
413     p_sys->i_sin = i_sin;
414     p_sys->i_cos = i_cos;
415     vlc_spin_unlock( &p_sys->lock );
416     return VLC_SUCCESS;
417 }