rotate: avoid sign extension
[vlc.git] / modules / video_filter / rotate.c
1 /*****************************************************************************
2  * rotate.c : video rotation filter
3  *****************************************************************************
4  * Copyright (C) 2000-2008 VLC authors and VideoLAN
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 #include <vlc_atomic.h>
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 #define ANGLE_TEXT N_("Angle in degrees")
55 #define ANGLE_LONGTEXT N_("Angle in degrees (0 to 359)")
56 #define MOTION_TEXT N_("Use motion sensors")
57 #define MOTION_LONGTEXT N_("Use HDAPS, AMS, APPLESMC or UNIMOTION motion " \
58     "sensors to rotate the video")
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_float( FILTER_PREFIX "angle", 30., ANGLE_TEXT, ANGLE_LONGTEXT, false )
73     add_bool( FILTER_PREFIX "use-motion", false, MOTION_TEXT,
74               MOTION_LONGTEXT, false )
75
76     add_shortcut( "rotate" )
77     set_callbacks( Create, Destroy )
78 vlc_module_end ()
79
80 static const char *const ppsz_filter_options[] = {
81     "angle", "use-motion", NULL
82 };
83
84 /*****************************************************************************
85  * filter_sys_t
86  *****************************************************************************/
87 struct filter_sys_t
88 {
89     atomic_uint_fast32_t sincos;
90     motion_sensors_t *p_motion;
91 };
92
93 static void store_trigo( struct filter_sys_t *sys, float f_angle )
94 {
95     f_angle *= (float)(M_PI / 180.); /* degrees -> radians */
96
97     uint16_t i_sin = lroundf(sinf(f_angle) * 4096.f);
98     uint16_t i_cos = lroundf(cosf(f_angle) * 4096.f);
99     atomic_store( &sys->sincos, (i_cos << 16u) | (i_sin << 0u));
100 }
101
102 static void fetch_trigo( struct filter_sys_t *sys, int *i_sin, int *i_cos )
103 {
104     uint32_t sincos = atomic_load( &sys->sincos );
105
106     *i_sin = (int16_t)(sincos & 0xFFFF);
107     *i_cos = (int16_t)(sincos >> 16);
108 }
109
110 /*****************************************************************************
111  * Create: allocates Distort video filter
112  *****************************************************************************/
113 static int Create( vlc_object_t *p_this )
114 {
115     filter_t *p_filter = (filter_t *)p_this;
116     filter_sys_t *p_sys;
117
118     if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
119     {
120         msg_Err( p_filter, "Input and output chromas don't match" );
121         return VLC_EGENERIC;
122     }
123
124     switch( p_filter->fmt_in.video.i_chroma )
125     {
126         CASE_PLANAR_YUV
127             p_filter->pf_video_filter = Filter;
128             break;
129
130         CASE_PACKED_YUV_422
131             p_filter->pf_video_filter = FilterPacked;
132             break;
133
134         default:
135             msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
136                      (char*)&(p_filter->fmt_in.video.i_chroma) );
137             return VLC_EGENERIC;
138     }
139
140     /* Allocate structure */
141     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
142     if( p_filter->p_sys == NULL )
143         return VLC_ENOMEM;
144     p_sys = p_filter->p_sys;
145
146     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
147                        p_filter->p_cfg );
148
149     if( var_InheritBool( p_filter, FILTER_PREFIX "use-motion" ) )
150     {
151         p_sys->p_motion = motion_create( VLC_OBJECT( p_filter ) );
152         if( p_sys->p_motion == NULL )
153         {
154             free( p_sys );
155             return VLC_EGENERIC;
156         }
157     }
158     else
159     {
160         float f_angle = var_CreateGetFloatCommand( p_filter,
161                                                    FILTER_PREFIX "angle" );
162         store_trigo( p_sys, f_angle );
163         var_AddCallback( p_filter, FILTER_PREFIX "angle",
164                          RotateCallback, p_sys );
165         p_sys->p_motion = NULL;
166     }
167
168     return VLC_SUCCESS;
169 }
170
171 /*****************************************************************************
172  * Destroy: destroy Distort filter
173  *****************************************************************************/
174 static void Destroy( vlc_object_t *p_this )
175 {
176     filter_t *p_filter = (filter_t *)p_this;
177     filter_sys_t *p_sys = p_filter->p_sys;
178
179     if( p_sys->p_motion != NULL )
180         motion_destroy( p_sys->p_motion );
181     else
182     {
183         var_DelCallback( p_filter, FILTER_PREFIX "angle",
184                          RotateCallback, p_sys );
185     }
186     free( p_sys );
187 }
188
189 /*****************************************************************************
190  *
191  *****************************************************************************/
192 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
193 {
194     picture_t *p_outpic;
195     filter_sys_t *p_sys = p_filter->p_sys;
196
197     if( !p_pic ) return NULL;
198
199     p_outpic = filter_NewPicture( p_filter );
200     if( !p_outpic )
201     {
202         picture_Release( p_pic );
203         return NULL;
204     }
205
206     if( p_sys->p_motion != NULL )
207     {
208         int i_angle = motion_get_angle( p_sys->p_motion );
209         store_trigo( p_sys, i_angle / 20.f );
210     }
211
212     int i_sin, i_cos;
213     fetch_trigo( p_sys, &i_sin, &i_cos );
214
215     for( int i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
216     {
217         plane_t *p_srcp = &p_pic->p[i_plane];
218         plane_t *p_dstp = &p_outpic->p[i_plane];
219
220         const int i_visible_lines = p_srcp->i_visible_lines;
221         const int i_visible_pitch = p_srcp->i_visible_pitch;
222
223         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 ));
224         /* = 2 for U and V planes in YUV 4:2:2, = 1 otherwise */
225
226         const int i_line_center = i_visible_lines>>1;
227         const int i_col_center  = i_visible_pitch>>1;
228
229         const uint8_t black_pixel = ( i_plane == Y_PLANE ) ? 0x00 : 0x80;
230
231         const int i_line_next =  i_cos / i_aspect -i_sin*i_visible_pitch;
232         const int i_col_next  = -i_sin / i_aspect -i_cos*i_visible_pitch;
233         int i_line_orig0 = ( - i_cos * i_line_center / i_aspect
234                              - i_sin * i_col_center + (1<<11) );
235         int i_col_orig0 =    i_sin * i_line_center / i_aspect
236                            - i_cos * i_col_center + (1<<11);
237         for( int y = 0; y < i_visible_lines; y++)
238         {
239             uint8_t *p_out = &p_dstp->p_pixels[y * p_dstp->i_pitch];
240
241             for( int x = 0; x < i_visible_pitch; x++, p_out++ )
242             {
243                 const int i_line_orig = (i_line_orig0>>12)*i_aspect + i_line_center;
244                 const int i_col_orig  = (i_col_orig0>>12)  + i_col_center;
245                 const uint8_t *p_orig_offset = &p_srcp->p_pixels[i_line_orig * p_srcp->i_pitch + i_col_orig];
246                 const uint8_t i_line_percent = (i_line_orig0>>4) & 255;
247                 const uint8_t i_col_percent  = (i_col_orig0 >>4) & 255;
248
249                 if(    -1 <= i_line_orig && i_line_orig < i_visible_lines
250                     && -1 <= i_col_orig  && i_col_orig  < i_visible_pitch )
251                 {
252                 #define test 1
253                 #undef test
254                 #ifdef test
255                     if( ( i_col_orig > i_visible_pitch/2 ) )
256                 #endif
257                     {
258                         uint8_t i_curpix = black_pixel;
259                         uint8_t i_colpix = black_pixel;
260                         uint8_t i_linpix = black_pixel;
261                         uint8_t i_nexpix = black_pixel;
262                         if( ( 0 <= i_line_orig ) && ( 0 <= i_col_orig ) )
263                             i_curpix = *p_orig_offset;
264                         p_orig_offset++;
265
266                         if(  ( i_col_orig < i_visible_pitch - 1)
267                              && ( i_line_orig >= 0 ) )
268                             i_colpix = *p_orig_offset;
269
270                         p_orig_offset += p_srcp->i_pitch;
271                         if( ( i_line_orig < i_visible_lines - 1)
272                             && ( i_col_orig  < i_visible_pitch - 1) )
273                             i_nexpix = *p_orig_offset;
274
275                         p_orig_offset--;
276                         if(  ( i_line_orig < i_visible_lines - 1)
277                              && ( i_col_orig >= 0 ) )
278                             i_linpix = *p_orig_offset;
279
280                         unsigned int temp = 0;
281                         temp+= i_curpix *
282                             (256 - i_line_percent) * ( 256 - i_col_percent );
283                         temp+= i_linpix *
284                             i_line_percent * (256 - i_col_percent );
285                         temp+= i_nexpix *
286                             ( i_col_percent) * ( i_line_percent);
287                         temp+= i_colpix *
288                             i_col_percent * (256 - i_line_percent );
289                         *p_out = temp >> 16;
290                     }
291                 #ifdef test
292                     else if (i_col_orig == i_visible_pitch/2 )
293                     {   *p_out = black_pixel;
294                     }
295                     else
296                         *p_out = *p_orig_offset;
297                 #endif
298                 #undef test
299                 }
300                 else
301                 {
302                     *p_out = black_pixel;
303                 }
304
305                 i_line_orig0 += i_sin;
306                 i_col_orig0 += i_cos;
307             }
308
309             i_line_orig0 += i_line_next;
310             i_col_orig0 += i_col_next;
311         }
312     }
313
314     return CopyInfoAndRelease( p_outpic, p_pic );
315 }
316
317 /*****************************************************************************
318  *
319  *****************************************************************************/
320 static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
321 {
322     picture_t *p_outpic;
323     filter_sys_t *p_sys = p_filter->p_sys;
324
325     if( !p_pic ) return NULL;
326
327     int i_u_offset, i_v_offset, i_y_offset;
328
329     if( GetPackedYuvOffsets( p_pic->format.i_chroma, &i_y_offset,
330                              &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
331     {
332         msg_Warn( p_filter, "Unsupported input chroma (%4.4s)",
333                   (char*)&(p_pic->format.i_chroma) );
334         picture_Release( p_pic );
335         return NULL;
336     }
337
338     p_outpic = filter_NewPicture( p_filter );
339     if( !p_outpic )
340     {
341         picture_Release( p_pic );
342         return NULL;
343     }
344
345     const int i_visible_pitch = p_pic->p->i_visible_pitch>>1; /* In fact it's i_visible_pixels */
346     const int i_visible_lines = p_pic->p->i_visible_lines;
347
348     const uint8_t *p_in   = p_pic->p->p_pixels+i_y_offset;
349     const uint8_t *p_in_u = p_pic->p->p_pixels+i_u_offset;
350     const uint8_t *p_in_v = p_pic->p->p_pixels+i_v_offset;
351     const int i_in_pitch  = p_pic->p->i_pitch;
352
353     uint8_t *p_out   = p_outpic->p->p_pixels+i_y_offset;
354     uint8_t *p_out_u = p_outpic->p->p_pixels+i_u_offset;
355     uint8_t *p_out_v = p_outpic->p->p_pixels+i_v_offset;
356     const int i_out_pitch = p_outpic->p->i_pitch;
357
358     const int i_line_center = i_visible_lines>>1;
359     const int i_col_center  = i_visible_pitch>>1;
360
361     if( p_sys->p_motion != NULL )
362     {
363         int i_angle = motion_get_angle( p_sys->p_motion );
364         store_trigo( p_sys, i_angle / 20.f );
365     }
366
367     int i_sin, i_cos;
368     fetch_trigo( p_sys, &i_sin, &i_cos );
369
370     int i_col, i_line;
371     for( i_line = 0; i_line < i_visible_lines; i_line++ )
372     {
373         for( i_col = 0; i_col < i_visible_pitch; i_col++ )
374         {
375             int i_line_orig;
376             int i_col_orig;
377             /* Handle "1st Y", U and V */
378             i_line_orig = i_line_center +
379                 ( ( i_sin * ( i_col - i_col_center )
380                   + i_cos * ( i_line - i_line_center ) )>>12 );
381             i_col_orig = i_col_center +
382                 ( ( i_cos * ( i_col - i_col_center )
383                   - i_sin * ( i_line - i_line_center ) )>>12 );
384             if( 0 <= i_col_orig && i_col_orig < i_visible_pitch
385              && 0 <= i_line_orig && i_line_orig < i_visible_lines )
386             {
387                 p_out[i_line*i_out_pitch+2*i_col] = p_in[i_line_orig*i_in_pitch+2*i_col_orig];
388                 i_col_orig /= 2;
389                 p_out_u[i_line*i_out_pitch+2*i_col] = p_in_u[i_line_orig*i_in_pitch+4*i_col_orig];
390                 p_out_v[i_line*i_out_pitch+2*i_col] = p_in_v[i_line_orig*i_in_pitch+4*i_col_orig];
391             }
392             else
393             {
394                 p_out[i_line*i_out_pitch+2*i_col] = 0x00;
395                 p_out_u[i_line*i_out_pitch+2*i_col] = 0x80;
396                 p_out_v[i_line*i_out_pitch+2*i_col] = 0x80;
397             }
398
399             /* Handle "2nd Y" */
400             i_col++;
401             if( i_col >= i_visible_pitch )
402                 break;
403
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             }
415             else
416             {
417                 p_out[i_line*i_out_pitch+2*i_col] = 0x00;
418             }
419         }
420     }
421
422     return CopyInfoAndRelease( p_outpic, p_pic );
423 }
424
425 /*****************************************************************************
426  * Angle modification callback.
427  *****************************************************************************/
428 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
429                            vlc_value_t oldval, vlc_value_t newval, void *data )
430 {
431     filter_sys_t *p_sys = data;
432
433     store_trigo( p_sys, newval.f_float );
434     (void) p_this; (void) psz_var; (void) oldval;
435     return VLC_SUCCESS;
436 }