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