1 /*****************************************************************************
2 * rotate.c : video rotation filter
3 *****************************************************************************
4 * Copyright (C) 2000-2008 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <math.h> /* sin(), cos() */
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
37 #include <vlc_filter.h>
38 #include "filter_picture.h"
39 #include "../control/motionlib.h"
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Create ( vlc_object_t * );
45 static void Destroy ( vlc_object_t * );
47 static picture_t *Filter( filter_t *, picture_t * );
48 static picture_t *FilterPacked( filter_t *, picture_t * );
50 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
51 vlc_value_t oldval, vlc_value_t newval,
54 static int PreciseRotateCallback( vlc_object_t *p_this, char const *psz_var,
55 vlc_value_t oldval, vlc_value_t newval,
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")
64 #define FILTER_PREFIX "rotate-"
66 /*****************************************************************************
68 *****************************************************************************/
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 )
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 )
81 add_shortcut( "rotate" )
82 set_callbacks( Create, Destroy )
85 static const char *const ppsz_filter_options[] = {
86 "angle", "use-motion", NULL
89 /*****************************************************************************
91 *****************************************************************************/
98 motion_sensors_t *p_motion;
101 static inline void cache_trigo( int i_angle, int *i_sin, int *i_cos )
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.);
108 /*****************************************************************************
109 * Create: allocates Distort video filter
110 *****************************************************************************/
111 static int Create( vlc_object_t *p_this )
113 filter_t *p_filter = (filter_t *)p_this;
116 if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
118 msg_Err( p_filter, "Input and output chromas don't match" );
122 switch( p_filter->fmt_in.video.i_chroma )
125 p_filter->pf_video_filter = Filter;
129 p_filter->pf_video_filter = FilterPacked;
133 msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
134 (char*)&(p_filter->fmt_in.video.i_chroma) );
138 /* Allocate structure */
139 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
140 if( p_filter->p_sys == NULL )
142 p_sys = p_filter->p_sys;
144 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
147 p_sys->p_motion = NULL;
148 if( var_InheritBool( p_filter, FILTER_PREFIX "use-motion" ) )
150 p_sys->p_motion = motion_create( VLC_OBJECT( p_filter ) );
151 if( p_sys->p_motion == NULL )
153 free( p_filter->p_sys );
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 );
174 /*****************************************************************************
175 * Destroy: destroy Distort filter
176 *****************************************************************************/
177 static void Destroy( vlc_object_t *p_this )
179 filter_t *p_filter = (filter_t *)p_this;
180 filter_sys_t *p_sys = p_filter->p_sys;
182 if( p_sys->p_motion != NULL )
183 motion_destroy( p_sys->p_motion );
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 );
195 /*****************************************************************************
197 *****************************************************************************/
198 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
201 filter_sys_t *p_sys = p_filter->p_sys;
203 if( !p_pic ) return NULL;
205 p_outpic = filter_NewPicture( p_filter );
208 picture_Release( p_pic );
212 if( p_sys->p_motion != NULL )
214 int i_angle = motion_get_angle( p_sys->p_motion );
215 if( p_sys->i_angle != i_angle )
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 );
223 vlc_spin_lock( &p_sys->lock );
225 const int i_sin = p_sys->i_sin;
226 const int i_cos = p_sys->i_cos;
228 if( p_sys->p_motion == NULL )
229 vlc_spin_unlock( &p_sys->lock );
231 for( int i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
233 plane_t *p_srcp = &p_pic->p[i_plane];
234 plane_t *p_dstp = &p_outpic->p[i_plane];
236 const int i_visible_lines = p_srcp->i_visible_lines;
237 const int i_visible_pitch = p_srcp->i_visible_pitch;
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 */
242 const int i_line_center = i_visible_lines>>1;
243 const int i_col_center = i_visible_pitch>>1;
245 const uint8_t black_pixel = ( i_plane == Y_PLANE ) ? 0x00 : 0x80;
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++)
255 uint8_t *p_out = &p_dstp->p_pixels[y * p_dstp->i_pitch];
257 for( int x = 0; x < i_visible_pitch; x++, p_out++ )
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;
265 if( -1 <= i_line_orig && i_line_orig < i_visible_lines
266 && -1 <= i_col_orig && i_col_orig < i_visible_pitch )
271 if( ( i_col_orig > i_visible_pitch/2 ) )
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;
282 if( ( i_col_orig < i_visible_pitch - 1)
283 && ( i_line_orig >= 0 ) )
284 i_colpix = *p_orig_offset;
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;
292 if( ( i_line_orig < i_visible_lines - 1)
293 && ( i_col_orig >= 0 ) )
294 i_linpix = *p_orig_offset;
296 unsigned int temp = 0;
298 (256 - i_line_percent) * ( 256 - i_col_percent );
300 i_line_percent * (256 - i_col_percent );
302 ( i_col_percent) * ( i_line_percent);
304 i_col_percent * (256 - i_line_percent );
308 else if (i_col_orig == i_visible_pitch/2 )
309 { *p_out = black_pixel;
312 *p_out = *p_orig_offset;
318 *p_out = black_pixel;
321 i_line_orig0 += i_sin;
322 i_col_orig0 += i_cos;
325 i_line_orig0 += i_line_next;
326 i_col_orig0 += i_col_next;
330 return CopyInfoAndRelease( p_outpic, p_pic );
333 /*****************************************************************************
335 *****************************************************************************/
336 static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
339 filter_sys_t *p_sys = p_filter->p_sys;
341 if( !p_pic ) return NULL;
343 int i_u_offset, i_v_offset, i_y_offset;
345 if( GetPackedYuvOffsets( p_pic->format.i_chroma, &i_y_offset,
346 &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
348 msg_Warn( p_filter, "Unsupported input chroma (%4.4s)",
349 (char*)&(p_pic->format.i_chroma) );
350 picture_Release( p_pic );
354 p_outpic = filter_NewPicture( p_filter );
357 picture_Release( p_pic );
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;
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;
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;
374 const int i_line_center = i_visible_lines>>1;
375 const int i_col_center = i_visible_pitch>>1;
377 if( p_sys->p_motion != NULL )
379 int i_angle = motion_get_angle( p_sys->p_motion );
380 if( p_sys->i_angle != i_angle )
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 );
388 vlc_spin_lock( &p_sys->lock );
390 const int i_sin = p_sys->i_sin;
391 const int i_cos = p_sys->i_cos;
393 if( p_sys->p_motion == NULL )
394 vlc_spin_unlock( &p_sys->lock );
397 for( i_line = 0; i_line < i_visible_lines; i_line++ )
399 for( i_col = 0; i_col < i_visible_pitch; i_col++ )
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 )
413 p_out[i_line*i_out_pitch+2*i_col] = p_in[i_line_orig*i_in_pitch+2*i_col_orig];
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];
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;
427 if( i_col >= i_visible_pitch )
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 )
439 p_out[i_line*i_out_pitch+2*i_col] = p_in[i_line_orig*i_in_pitch+2*i_col_orig];
443 p_out[i_line*i_out_pitch+2*i_col] = 0x00;
448 return CopyInfoAndRelease( p_outpic, p_pic );
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,
460 return PreciseRotateCallback( p_this, psz_var, oldval, newval, p_data );
463 static int PreciseRotateCallback( vlc_object_t *p_this, char const *psz_var,
464 vlc_value_t oldval, vlc_value_t newval,
467 VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
468 filter_sys_t *p_sys = (filter_sys_t *)p_data;
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 );