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