upnp: remove m_server_list_lock, p_server_list is already protected by s_lock
[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 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_atomic.h>
38 #include <vlc_filter.h>
39 #include "filter_picture.h"
40 #include "../control/motionlib.h"
41
42 /*****************************************************************************
43  * Local prototypes
44  *****************************************************************************/
45 static int  Create    ( vlc_object_t * );
46 static void Destroy   ( vlc_object_t * );
47
48 static picture_t *Filter( filter_t *, picture_t * );
49 static picture_t *FilterPacked( filter_t *, picture_t * );
50
51 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
52                            vlc_value_t oldval, vlc_value_t newval,
53                            void *p_data );
54
55 #define ANGLE_TEXT N_("Angle in degrees")
56 #define ANGLE_LONGTEXT N_("Angle in degrees (0 to 359)")
57 #define MOTION_TEXT N_("Use motion sensors")
58 #define MOTION_LONGTEXT N_("Use HDAPS, AMS, APPLESMC or UNIMOTION motion " \
59     "sensors to rotate the video")
60
61 #define FILTER_PREFIX "rotate-"
62
63 /*****************************************************************************
64  * Module descriptor
65  *****************************************************************************/
66 vlc_module_begin ()
67     set_description( N_("Rotate video filter") )
68     set_shortname( N_( "Rotate" ))
69     set_capability( "video filter2", 0 )
70     set_category( CAT_VIDEO )
71     set_subcategory( SUBCAT_VIDEO_VFILTER )
72
73     add_float( FILTER_PREFIX "angle", 30., ANGLE_TEXT, ANGLE_LONGTEXT, false )
74     add_bool( FILTER_PREFIX "use-motion", false, MOTION_TEXT,
75               MOTION_LONGTEXT, false )
76
77     add_shortcut( "rotate" )
78     set_callbacks( Create, Destroy )
79 vlc_module_end ()
80
81 static const char *const ppsz_filter_options[] = {
82     "angle", "use-motion", NULL
83 };
84
85 /*****************************************************************************
86  * filter_sys_t
87  *****************************************************************************/
88 struct filter_sys_t
89 {
90     atomic_uint_fast32_t sincos;
91     motion_sensors_t *p_motion;
92 };
93
94 static void store_trigo( struct filter_sys_t *sys, float f_angle )
95 {
96     f_angle *= (float)(M_PI / 180.); /* degrees -> radians */
97
98     uint16_t i_sin = lroundf(sinf(f_angle) * 4096.f);
99     uint16_t i_cos = lroundf(cosf(f_angle) * 4096.f);
100     atomic_store( &sys->sincos, (i_cos << 16u) | (i_sin << 0u));
101 }
102
103 static void fetch_trigo( struct filter_sys_t *sys, int *i_sin, int *i_cos )
104 {
105     uint32_t sincos = atomic_load( &sys->sincos );
106
107     *i_sin = (int16_t)(sincos & 0xFFFF);
108     *i_cos = (int16_t)(sincos >> 16);
109 }
110
111 /*****************************************************************************
112  * Create: allocates Distort video filter
113  *****************************************************************************/
114 static int Create( vlc_object_t *p_this )
115 {
116     filter_t *p_filter = (filter_t *)p_this;
117     filter_sys_t *p_sys;
118
119     if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
120     {
121         msg_Err( p_filter, "Input and output chromas don't match" );
122         return VLC_EGENERIC;
123     }
124
125     switch( p_filter->fmt_in.video.i_chroma )
126     {
127         CASE_PLANAR_YUV
128             p_filter->pf_video_filter = Filter;
129             break;
130
131         CASE_PACKED_YUV_422
132             p_filter->pf_video_filter = FilterPacked;
133             break;
134
135         default:
136             msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
137                      (char*)&(p_filter->fmt_in.video.i_chroma) );
138             return VLC_EGENERIC;
139     }
140
141     /* Allocate structure */
142     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
143     if( p_filter->p_sys == NULL )
144         return VLC_ENOMEM;
145     p_sys = p_filter->p_sys;
146
147     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
148                        p_filter->p_cfg );
149
150     if( var_InheritBool( p_filter, FILTER_PREFIX "use-motion" ) )
151     {
152         p_sys->p_motion = motion_create( VLC_OBJECT( p_filter ) );
153         if( p_sys->p_motion == NULL )
154         {
155             free( p_sys );
156             return VLC_EGENERIC;
157         }
158     }
159     else
160     {
161         float f_angle = var_CreateGetFloatCommand( p_filter,
162                                                    FILTER_PREFIX "angle" );
163         store_trigo( p_sys, f_angle );
164         var_AddCallback( p_filter, FILTER_PREFIX "angle",
165                          RotateCallback, p_sys );
166         p_sys->p_motion = NULL;
167     }
168
169     return VLC_SUCCESS;
170 }
171
172 /*****************************************************************************
173  * Destroy: destroy Distort filter
174  *****************************************************************************/
175 static void Destroy( vlc_object_t *p_this )
176 {
177     filter_t *p_filter = (filter_t *)p_this;
178     filter_sys_t *p_sys = p_filter->p_sys;
179
180     if( p_sys->p_motion != NULL )
181         motion_destroy( p_sys->p_motion );
182     else
183     {
184         var_DelCallback( p_filter, FILTER_PREFIX "angle",
185                          RotateCallback, p_sys );
186     }
187     free( p_sys );
188 }
189
190 /*****************************************************************************
191  *
192  *****************************************************************************/
193 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
194 {
195     picture_t *p_outpic;
196     filter_sys_t *p_sys = p_filter->p_sys;
197
198     if( !p_pic ) return NULL;
199
200     p_outpic = filter_NewPicture( p_filter );
201     if( !p_outpic )
202     {
203         picture_Release( p_pic );
204         return NULL;
205     }
206
207     if( p_sys->p_motion != NULL )
208     {
209         int i_angle = motion_get_angle( p_sys->p_motion );
210         store_trigo( p_sys, i_angle / 20.f );
211     }
212
213     int i_sin, i_cos;
214     fetch_trigo( p_sys, &i_sin, &i_cos );
215
216     for( int i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
217     {
218         plane_t *p_srcp = &p_pic->p[i_plane];
219         plane_t *p_dstp = &p_outpic->p[i_plane];
220
221         const int i_visible_lines = p_srcp->i_visible_lines;
222         const int i_visible_pitch = p_srcp->i_visible_pitch;
223
224         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 ));
225         /* = 2 for U and V planes in YUV 4:2:2, = 1 otherwise */
226
227         const int i_line_center = i_visible_lines>>1;
228         const int i_col_center  = i_visible_pitch>>1;
229
230         const uint8_t black_pixel = ( i_plane == Y_PLANE ) ? 0x00 : 0x80;
231
232         const int i_line_next =  i_cos / i_aspect -i_sin*i_visible_pitch;
233         const int i_col_next  = -i_sin / i_aspect -i_cos*i_visible_pitch;
234         int i_line_orig0 = ( - i_cos * i_line_center / i_aspect
235                              - i_sin * i_col_center + (1<<11) );
236         int i_col_orig0 =    i_sin * i_line_center / i_aspect
237                            - i_cos * i_col_center + (1<<11);
238         for( int y = 0; y < i_visible_lines; y++)
239         {
240             uint8_t *p_out = &p_dstp->p_pixels[y * p_dstp->i_pitch];
241
242             for( int x = 0; x < i_visible_pitch; x++, p_out++ )
243             {
244                 const int i_line_orig = (i_line_orig0>>12)*i_aspect + i_line_center;
245                 const int i_col_orig  = (i_col_orig0>>12)  + i_col_center;
246                 const uint8_t *p_orig_offset = &p_srcp->p_pixels[i_line_orig * p_srcp->i_pitch + i_col_orig];
247                 const uint8_t i_line_percent = (i_line_orig0>>4) & 255;
248                 const uint8_t i_col_percent  = (i_col_orig0 >>4) & 255;
249
250                 if(    -1 <= i_line_orig && i_line_orig < i_visible_lines
251                     && -1 <= i_col_orig  && i_col_orig  < i_visible_pitch )
252                 {
253                 #define test 1
254                 #undef test
255                 #ifdef test
256                     if( ( i_col_orig > i_visible_pitch/2 ) )
257                 #endif
258                     {
259                         uint8_t i_curpix = black_pixel;
260                         uint8_t i_colpix = black_pixel;
261                         uint8_t i_linpix = black_pixel;
262                         uint8_t i_nexpix = black_pixel;
263                         if( ( 0 <= i_line_orig ) && ( 0 <= i_col_orig ) )
264                             i_curpix = *p_orig_offset;
265                         p_orig_offset++;
266
267                         if(  ( i_col_orig < i_visible_pitch - 1)
268                              && ( i_line_orig >= 0 ) )
269                             i_colpix = *p_orig_offset;
270
271                         p_orig_offset += p_srcp->i_pitch;
272                         if( ( i_line_orig < i_visible_lines - 1)
273                             && ( i_col_orig  < i_visible_pitch - 1) )
274                             i_nexpix = *p_orig_offset;
275
276                         p_orig_offset--;
277                         if(  ( i_line_orig < i_visible_lines - 1)
278                              && ( i_col_orig >= 0 ) )
279                             i_linpix = *p_orig_offset;
280
281                         unsigned int temp = 0;
282                         temp+= i_curpix *
283                             (256 - i_line_percent) * ( 256 - i_col_percent );
284                         temp+= i_linpix *
285                             i_line_percent * (256 - i_col_percent );
286                         temp+= i_nexpix *
287                             ( i_col_percent) * ( i_line_percent);
288                         temp+= i_colpix *
289                             i_col_percent * (256 - i_line_percent );
290                         *p_out = temp >> 16;
291                     }
292                 #ifdef test
293                     else if (i_col_orig == i_visible_pitch/2 )
294                     {   *p_out = black_pixel;
295                     }
296                     else
297                         *p_out = *p_orig_offset;
298                 #endif
299                 #undef test
300                 }
301                 else
302                 {
303                     *p_out = black_pixel;
304                 }
305
306                 i_line_orig0 += i_sin;
307                 i_col_orig0 += i_cos;
308             }
309
310             i_line_orig0 += i_line_next;
311             i_col_orig0 += i_col_next;
312         }
313     }
314
315     return CopyInfoAndRelease( p_outpic, p_pic );
316 }
317
318 /*****************************************************************************
319  *
320  *****************************************************************************/
321 static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
322 {
323     picture_t *p_outpic;
324     filter_sys_t *p_sys = p_filter->p_sys;
325
326     if( !p_pic ) return NULL;
327
328     int i_u_offset, i_v_offset, i_y_offset;
329
330     if( GetPackedYuvOffsets( p_pic->format.i_chroma, &i_y_offset,
331                              &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
332     {
333         msg_Warn( p_filter, "Unsupported input chroma (%4.4s)",
334                   (char*)&(p_pic->format.i_chroma) );
335         picture_Release( p_pic );
336         return NULL;
337     }
338
339     p_outpic = filter_NewPicture( p_filter );
340     if( !p_outpic )
341     {
342         picture_Release( p_pic );
343         return NULL;
344     }
345
346     const int i_visible_pitch = p_pic->p->i_visible_pitch>>1; /* In fact it's i_visible_pixels */
347     const int i_visible_lines = p_pic->p->i_visible_lines;
348
349     const uint8_t *p_in   = p_pic->p->p_pixels+i_y_offset;
350     const uint8_t *p_in_u = p_pic->p->p_pixels+i_u_offset;
351     const uint8_t *p_in_v = p_pic->p->p_pixels+i_v_offset;
352     const int i_in_pitch  = p_pic->p->i_pitch;
353
354     uint8_t *p_out   = p_outpic->p->p_pixels+i_y_offset;
355     uint8_t *p_out_u = p_outpic->p->p_pixels+i_u_offset;
356     uint8_t *p_out_v = p_outpic->p->p_pixels+i_v_offset;
357     const int i_out_pitch = p_outpic->p->i_pitch;
358
359     const int i_line_center = i_visible_lines>>1;
360     const int i_col_center  = i_visible_pitch>>1;
361
362     if( p_sys->p_motion != NULL )
363     {
364         int i_angle = motion_get_angle( p_sys->p_motion );
365         store_trigo( p_sys, i_angle / 20.f );
366     }
367
368     int i_sin, i_cos;
369     fetch_trigo( p_sys, &i_sin, &i_cos );
370
371     for( int i_line = 0; i_line < i_visible_lines; i_line++ )
372     {
373         for( int 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 }