rotate: fix uninitialized motion angle
[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 #include "../control/motionlib.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 static picture_t *FilterPacked( filter_t *, picture_t * );
49
50 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
51                            vlc_value_t oldval, vlc_value_t newval,
52                            void *p_data );
53
54 static int PreciseRotateCallback( vlc_object_t *p_this, char const *psz_var,
55                            vlc_value_t oldval, vlc_value_t newval,
56                            void *p_data );
57
58 #define ANGLE_TEXT N_("Angle in degrees")
59 #define ANGLE_LONGTEXT N_("Angle in degrees (0 to 359)")
60 #define MOTION_TEXT N_("Use motion sensors")
61 #define MOTION_LONGTEXT N_("Use HDAPS, AMS, APPLESMC or UNIMOTION motion " \
62     "sensors to rotate the video")
63
64 #define FILTER_PREFIX "rotate-"
65
66 /*****************************************************************************
67  * Module descriptor
68  *****************************************************************************/
69 vlc_module_begin ()
70     set_description( N_("Rotate video filter") )
71     set_shortname( N_( "Rotate" ))
72     set_capability( "video filter2", 0 )
73     set_category( CAT_VIDEO )
74     set_subcategory( SUBCAT_VIDEO_VFILTER )
75
76     add_integer_with_range( FILTER_PREFIX "angle", 30, 0, 359,
77         ANGLE_TEXT, ANGLE_LONGTEXT, false )
78     add_bool( FILTER_PREFIX "use-motion", false, MOTION_TEXT,
79               MOTION_LONGTEXT, false )
80
81     add_shortcut( "rotate" )
82     set_callbacks( Create, Destroy )
83 vlc_module_end ()
84
85 static const char *const ppsz_filter_options[] = {
86     "angle", "use-motion", NULL
87 };
88
89 /*****************************************************************************
90  * filter_sys_t
91  *****************************************************************************/
92 struct filter_sys_t
93 {
94     vlc_spinlock_t lock;
95     int            i_cos;
96     int            i_sin;
97     int            i_angle;
98     motion_sensors_t *p_motion;
99 };
100
101 static inline void cache_trigo( int i_angle, int *i_sin, int *i_cos )
102 {
103     const double f_angle = (((double)i_angle)*M_PI)/1800.;
104     *i_sin = (int)(sin( f_angle )*4096.);
105     *i_cos = (int)(cos( f_angle )*4096.);
106 }
107
108 /*****************************************************************************
109  * Create: allocates Distort video filter
110  *****************************************************************************/
111 static int Create( vlc_object_t *p_this )
112 {
113     filter_t *p_filter = (filter_t *)p_this;
114     filter_sys_t *p_sys;
115
116     if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
117     {
118         msg_Err( p_filter, "Input and output chromas don't match" );
119         return VLC_EGENERIC;
120     }
121
122     switch( p_filter->fmt_in.video.i_chroma )
123     {
124         CASE_PLANAR_YUV
125             p_filter->pf_video_filter = Filter;
126             break;
127
128         CASE_PACKED_YUV_422
129             p_filter->pf_video_filter = FilterPacked;
130             break;
131
132         default:
133             msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
134                      (char*)&(p_filter->fmt_in.video.i_chroma) );
135             return VLC_EGENERIC;
136     }
137
138     /* Allocate structure */
139     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
140     if( p_filter->p_sys == NULL )
141         return VLC_ENOMEM;
142     p_sys = p_filter->p_sys;
143
144     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
145                        p_filter->p_cfg );
146
147     p_sys->p_motion = NULL;
148     if( var_InheritBool( p_filter, FILTER_PREFIX "use-motion" ) )
149     {
150         p_sys->p_motion = motion_create( VLC_OBJECT( p_filter ) );
151         if( p_sys->p_motion == NULL )
152         {
153             free( p_filter->p_sys );
154             return VLC_EGENERIC;
155         }
156         p_sys->i_angle = 0;
157         cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );
158     }
159     else
160     {
161         int i_angle = var_CreateGetIntegerCommand( p_filter,
162                                                    FILTER_PREFIX "angle" ) * 10;
163         cache_trigo( i_angle, &p_sys->i_sin, &p_sys->i_cos );
164         var_Create( p_filter, FILTER_PREFIX "deciangle",
165                     VLC_VAR_INTEGER|VLC_VAR_ISCOMMAND );
166         vlc_spin_init( &p_sys->lock );
167         var_AddCallback( p_filter, FILTER_PREFIX "angle",
168                          RotateCallback, p_sys );
169         var_AddCallback( p_filter, FILTER_PREFIX "deciangle",
170                          PreciseRotateCallback, p_sys );
171     }
172
173     return VLC_SUCCESS;
174 }
175
176 /*****************************************************************************
177  * Destroy: destroy Distort filter
178  *****************************************************************************/
179 static void Destroy( vlc_object_t *p_this )
180 {
181     filter_t *p_filter = (filter_t *)p_this;
182     filter_sys_t *p_sys = p_filter->p_sys;
183
184     if( p_sys->p_motion != NULL )
185         motion_destroy( p_sys->p_motion );
186     else
187     {
188         var_DelCallback( p_filter, FILTER_PREFIX "angle",
189                          RotateCallback, p_sys );
190         var_DelCallback( p_filter, FILTER_PREFIX "deciangle",
191                          PreciseRotateCallback, p_sys );
192         vlc_spin_destroy( &p_sys->lock );
193     }
194     free( p_sys );
195 }
196
197 /*****************************************************************************
198  *
199  *****************************************************************************/
200 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
201 {
202     picture_t *p_outpic;
203     filter_sys_t *p_sys = p_filter->p_sys;
204
205     if( !p_pic ) return NULL;
206
207     p_outpic = filter_NewPicture( p_filter );
208     if( !p_outpic )
209     {
210         picture_Release( p_pic );
211         return NULL;
212     }
213
214     int s, c;
215     if( p_sys->p_motion != NULL )
216     {
217         int i_angle = motion_get_angle( p_sys->p_motion ) / 2;
218         if( p_sys->i_angle != i_angle )
219         {
220             p_sys->i_angle = i_angle;
221             cache_trigo( i_angle, &p_sys->i_sin, &p_sys->i_cos );
222         }
223
224         s = p_sys->i_sin;
225         c = p_sys->i_cos;
226     }
227     else
228     {
229         vlc_spin_lock( &p_sys->lock );
230         s = p_sys->i_sin;
231         c = p_sys->i_cos;
232         vlc_spin_unlock( &p_sys->lock );
233     }
234
235     const int i_sin = s;
236     const int i_cos = c;
237
238     for( int i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
239     {
240         plane_t *p_srcp = &p_pic->p[i_plane];
241         plane_t *p_dstp = &p_outpic->p[i_plane];
242
243         const int i_visible_lines = p_srcp->i_visible_lines;
244         const int i_visible_pitch = p_srcp->i_visible_pitch;
245
246         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 ));
247         /* = 2 for U and V planes in YUV 4:2:2, = 1 otherwise */
248
249         const int i_line_center = i_visible_lines>>1;
250         const int i_col_center  = i_visible_pitch>>1;
251
252         const uint8_t black_pixel = ( i_plane == Y_PLANE ) ? 0x00 : 0x80;
253
254         const int i_line_next =  i_cos / i_aspect -i_sin*i_visible_pitch;
255         const int i_col_next  = -i_sin / i_aspect -i_cos*i_visible_pitch;
256         int i_line_orig0 = ( - i_cos * i_line_center / i_aspect
257                              - i_sin * i_col_center + (1<<11) );
258         int i_col_orig0 =    i_sin * i_line_center / i_aspect
259                            - i_cos * i_col_center + (1<<11);
260         for( int y = 0; y < i_visible_lines; y++)
261         {
262             uint8_t *p_out = &p_dstp->p_pixels[y * p_dstp->i_pitch];
263
264             for( int x = 0; x < i_visible_pitch; x++, p_out++ )
265             {
266                 const int i_line_orig = (i_line_orig0>>12)*i_aspect + i_line_center;
267                 const int i_col_orig  = (i_col_orig0>>12)  + i_col_center;
268                 const uint8_t *p_orig_offset = &p_srcp->p_pixels[i_line_orig * p_srcp->i_pitch + i_col_orig];
269                 const uint8_t i_line_percent = (i_line_orig0>>4) & 255;
270                 const uint8_t i_col_percent  = (i_col_orig0 >>4) & 255;
271
272                 if(    -1 <= i_line_orig && i_line_orig < i_visible_lines
273                     && -1 <= i_col_orig  && i_col_orig  < i_visible_pitch )
274                 {
275                 #define test 1
276                 #undef test
277                 #ifdef test
278                     if( ( i_col_orig > i_visible_pitch/2 ) )
279                 #endif
280                     {
281                         uint8_t i_curpix = black_pixel;
282                         uint8_t i_colpix = black_pixel;
283                         uint8_t i_linpix = black_pixel;
284                         uint8_t i_nexpix = black_pixel;
285                         if( ( 0 <= i_line_orig ) && ( 0 <= i_col_orig ) )
286                             i_curpix = *p_orig_offset;
287                         p_orig_offset++;
288
289                         if(  ( i_col_orig < i_visible_pitch - 1)
290                              && ( i_line_orig >= 0 ) )
291                             i_colpix = *p_orig_offset;
292
293                         p_orig_offset += p_srcp->i_pitch;
294                         if( ( i_line_orig < i_visible_lines - 1)
295                             && ( i_col_orig  < i_visible_pitch - 1) )
296                             i_nexpix = *p_orig_offset;
297
298                         p_orig_offset--;
299                         if(  ( i_line_orig < i_visible_lines - 1)
300                              && ( i_col_orig >= 0 ) )
301                             i_linpix = *p_orig_offset;
302
303                         unsigned int temp = 0;
304                         temp+= i_curpix *
305                             (256 - i_line_percent) * ( 256 - i_col_percent );
306                         temp+= i_linpix *
307                             i_line_percent * (256 - i_col_percent );
308                         temp+= i_nexpix *
309                             ( i_col_percent) * ( i_line_percent);
310                         temp+= i_colpix *
311                             i_col_percent * (256 - i_line_percent );
312                         *p_out = temp >> 16;
313                     }
314                 #ifdef test
315                     else if (i_col_orig == i_visible_pitch/2 )
316                     {   *p_out = black_pixel;
317                     }
318                     else
319                         *p_out = *p_orig_offset;
320                 #endif
321                 #undef test
322                 }
323                 else
324                 {
325                     *p_out = black_pixel;
326                 }
327
328                 i_line_orig0 += i_sin;
329                 i_col_orig0 += i_cos;
330             }
331
332             i_line_orig0 += i_line_next;
333             i_col_orig0 += i_col_next;
334         }
335     }
336
337     return CopyInfoAndRelease( p_outpic, p_pic );
338 }
339
340 /*****************************************************************************
341  *
342  *****************************************************************************/
343 static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
344 {
345     picture_t *p_outpic;
346     filter_sys_t *p_sys = p_filter->p_sys;
347
348     if( !p_pic ) return NULL;
349
350     int i_u_offset, i_v_offset, i_y_offset;
351
352     if( GetPackedYuvOffsets( p_pic->format.i_chroma, &i_y_offset,
353                              &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
354     {
355         msg_Warn( p_filter, "Unsupported input chroma (%4.4s)",
356                   (char*)&(p_pic->format.i_chroma) );
357         picture_Release( p_pic );
358         return NULL;
359     }
360
361     p_outpic = filter_NewPicture( p_filter );
362     if( !p_outpic )
363     {
364         picture_Release( p_pic );
365         return NULL;
366     }
367
368     const int i_visible_pitch = p_pic->p->i_visible_pitch>>1; /* In fact it's i_visible_pixels */
369     const int i_visible_lines = p_pic->p->i_visible_lines;
370
371     const uint8_t *p_in   = p_pic->p->p_pixels+i_y_offset;
372     const uint8_t *p_in_u = p_pic->p->p_pixels+i_u_offset;
373     const uint8_t *p_in_v = p_pic->p->p_pixels+i_v_offset;
374     const int i_in_pitch  = p_pic->p->i_pitch;
375
376     uint8_t *p_out   = p_outpic->p->p_pixels+i_y_offset;
377     uint8_t *p_out_u = p_outpic->p->p_pixels+i_u_offset;
378     uint8_t *p_out_v = p_outpic->p->p_pixels+i_v_offset;
379     const int i_out_pitch = p_outpic->p->i_pitch;
380
381     const int i_line_center = i_visible_lines>>1;
382     const int i_col_center  = i_visible_pitch>>1;
383
384     int s, c;
385     if( p_sys->p_motion != NULL )
386     {
387         int i_angle = motion_get_angle( p_sys->p_motion ) / 2;
388         if( p_sys->i_angle != i_angle )
389         {
390             p_sys->i_angle = i_angle;
391             cache_trigo( i_angle, &p_sys->i_sin, &p_sys->i_cos );
392         }
393
394         s = p_sys->i_sin;
395         c = p_sys->i_cos;
396     }
397     else
398     {
399         vlc_spin_lock( &p_sys->lock );
400         s = p_sys->i_sin;
401         c = p_sys->i_cos;
402         vlc_spin_unlock( &p_sys->lock );
403     }
404
405     const int i_sin = s;
406     const int i_cos = c;
407
408     int i_col, i_line;
409     for( i_line = 0; i_line < i_visible_lines; i_line++ )
410     {
411         for( i_col = 0; i_col < i_visible_pitch; i_col++ )
412         {
413             int i_line_orig;
414             int i_col_orig;
415             /* Handle "1st Y", U and V */
416             i_line_orig = i_line_center +
417                 ( ( i_sin * ( i_col - i_col_center )
418                   + i_cos * ( i_line - i_line_center ) )>>12 );
419             i_col_orig = i_col_center +
420                 ( ( i_cos * ( i_col - i_col_center )
421                   - i_sin * ( i_line - i_line_center ) )>>12 );
422             if( 0 <= i_col_orig && i_col_orig < i_visible_pitch
423              && 0 <= i_line_orig && i_line_orig < i_visible_lines )
424             {
425                 p_out[i_line*i_out_pitch+2*i_col] = p_in[i_line_orig*i_in_pitch+2*i_col_orig];
426                 i_col_orig /= 2;
427                 p_out_u[i_line*i_out_pitch+2*i_col] = p_in_u[i_line_orig*i_in_pitch+4*i_col_orig];
428                 p_out_v[i_line*i_out_pitch+2*i_col] = p_in_v[i_line_orig*i_in_pitch+4*i_col_orig];
429             }
430             else
431             {
432                 p_out[i_line*i_out_pitch+2*i_col] = 0x00;
433                 p_out_u[i_line*i_out_pitch+2*i_col] = 0x80;
434                 p_out_v[i_line*i_out_pitch+2*i_col] = 0x80;
435             }
436
437             /* Handle "2nd Y" */
438             i_col++;
439             if( i_col >= i_visible_pitch )
440                 break;
441
442             i_line_orig = i_line_center +
443                 ( ( i_sin * ( i_col - i_col_center )
444                   + i_cos * ( i_line - i_line_center ) )>>12 );
445             i_col_orig = i_col_center +
446                 ( ( i_cos * ( i_col - i_col_center )
447                   - i_sin * ( i_line - i_line_center ) )>>12 );
448             if( 0 <= i_col_orig && i_col_orig < i_visible_pitch
449              && 0 <= i_line_orig && i_line_orig < i_visible_lines )
450             {
451                 p_out[i_line*i_out_pitch+2*i_col] = p_in[i_line_orig*i_in_pitch+2*i_col_orig];
452             }
453             else
454             {
455                 p_out[i_line*i_out_pitch+2*i_col] = 0x00;
456             }
457         }
458     }
459
460     return CopyInfoAndRelease( p_outpic, p_pic );
461 }
462
463 /*****************************************************************************
464  * Angle modification callbacks.
465  *****************************************************************************/
466 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
467                            vlc_value_t oldval, vlc_value_t newval,
468                            void *p_data )
469 {
470     oldval.i_int *= 10;
471     newval.i_int *= 10;
472     return PreciseRotateCallback( p_this, psz_var, oldval, newval, p_data );
473 }
474
475 static int PreciseRotateCallback( vlc_object_t *p_this, char const *psz_var,
476                            vlc_value_t oldval, vlc_value_t newval,
477                            void *p_data )
478 {
479     VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
480     filter_sys_t *p_sys = (filter_sys_t *)p_data;
481     int i_sin, i_cos;
482
483     cache_trigo( newval.i_int, &i_sin, &i_cos );
484     vlc_spin_lock( &p_sys->lock );
485     p_sys->i_sin = i_sin;
486     p_sys->i_cos = i_cos;
487     vlc_spin_unlock( &p_sys->lock );
488     return VLC_SUCCESS;
489 }