Use filter helpers.
[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 #include <vlc_vout.h>
37
38 #include "vlc_filter.h"
39 #include "filter_picture.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
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_integer_with_range( FILTER_PREFIX "angle", 30, 0, 359, NULL,
74         ANGLE_TEXT, ANGLE_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", NULL
82 };
83
84 /*****************************************************************************
85  * filter_sys_t
86  *****************************************************************************/
87 struct filter_sys_t
88 {
89     int     i_angle;
90     int     i_cos;
91     int     i_sin;
92 };
93
94 static inline void cache_trigo( int i_angle, int *i_sin, int *i_cos )
95 {
96     const double f_angle = (((double)i_angle)*M_PI)/1800.;
97     *i_sin = (int)(sin( f_angle )*4096.);
98     *i_cos = (int)(cos( f_angle )*4096.);
99 }
100
101 /*****************************************************************************
102  * Create: allocates Distort video filter
103  *****************************************************************************/
104 static int Create( vlc_object_t *p_this )
105 {
106     filter_t *p_filter = (filter_t *)p_this;
107     filter_sys_t *p_sys;
108
109     if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
110     {
111         msg_Err( p_filter, "Input and output chromas don't match" );
112         return VLC_EGENERIC;
113     }
114
115     switch( p_filter->fmt_in.video.i_chroma )
116     {
117         CASE_PLANAR_YUV
118             p_filter->pf_video_filter = Filter;
119             break;
120
121         CASE_PACKED_YUV_422
122             p_filter->pf_video_filter = FilterPacked;
123             break;
124
125         default:
126             msg_Err( p_filter, "Unsupported input chroma (%4s)",
127                      (char*)&(p_filter->fmt_in.video.i_chroma) );
128             return VLC_EGENERIC;
129     }
130
131     /* Allocate structure */
132     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
133     if( p_filter->p_sys == NULL )
134         return VLC_ENOMEM;
135     p_sys = p_filter->p_sys;
136
137     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
138                        p_filter->p_cfg );
139
140     p_sys->i_angle = var_CreateGetIntegerCommand( p_filter,
141                                                   FILTER_PREFIX "angle" ) * 10;
142     var_Create( p_filter, FILTER_PREFIX "deciangle",
143                 VLC_VAR_INTEGER|VLC_VAR_ISCOMMAND );
144     var_AddCallback( p_filter, FILTER_PREFIX "angle", RotateCallback, p_sys );
145     var_AddCallback( p_filter, FILTER_PREFIX "deciangle",
146                      PreciseRotateCallback, p_sys );
147
148     cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );
149
150     return VLC_SUCCESS;
151 }
152
153 /*****************************************************************************
154  * Destroy: destroy Distort filter
155  *****************************************************************************/
156 static void Destroy( vlc_object_t *p_this )
157 {
158     filter_t *p_filter = (filter_t *)p_this;
159
160     free( p_filter->p_sys );
161 }
162
163 /*****************************************************************************
164  *
165  *****************************************************************************/
166 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
167 {
168     picture_t *p_outpic;
169     filter_sys_t *p_sys = p_filter->p_sys;
170     int i_plane;
171     const int i_sin = p_sys->i_sin, i_cos = p_sys->i_cos;
172
173     if( !p_pic ) return NULL;
174
175     p_outpic = filter_NewPicture( p_filter );
176     if( !p_outpic )
177     {
178         picture_Release( p_pic );
179         return NULL;
180     }
181
182     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
183     {
184         const int i_visible_lines = p_pic->p[i_plane].i_visible_lines;
185         const int i_visible_pitch = p_pic->p[i_plane].i_visible_pitch;
186         const int i_pitch         = p_pic->p[i_plane].i_pitch;
187         const int i_hidden_pitch  = i_pitch - i_visible_pitch;
188
189         const int i_aspect = ( i_visible_lines * p_pic->p[Y_PLANE].i_visible_pitch ) / ( p_pic->p[Y_PLANE].i_visible_lines * i_visible_pitch );
190         /* = 2 for U and V planes in YUV 4:2:2, = 1 otherwise */
191
192         const int i_line_center = i_visible_lines>>1;
193         const int i_col_center  = i_visible_pitch>>1;
194
195         const uint8_t *p_in = p_pic->p[i_plane].p_pixels;
196         uint8_t *p_out = p_outpic->p[i_plane].p_pixels;
197         uint8_t *p_outendline = p_out + i_visible_pitch;
198         const uint8_t *p_outend = p_out + i_visible_lines * i_pitch;
199
200         const uint8_t black_pixel = ( i_plane == Y_PLANE ) ? 0x00 : 0x80;
201
202         const int i_line_next =  i_cos / i_aspect -i_sin*i_visible_pitch;
203         const int i_col_next  = -i_sin / i_aspect -i_cos*i_visible_pitch;
204         int i_line_orig0 = ( - i_cos * i_line_center / i_aspect
205                              - i_sin * i_col_center + (1<<11) );
206         int i_col_orig0 =    i_sin * i_line_center / i_aspect
207                            - i_cos * i_col_center + (1<<11);
208         for( ; p_outendline < p_outend;
209              p_out += i_hidden_pitch, p_outendline += i_pitch,
210              i_line_orig0 += i_line_next, i_col_orig0 += i_col_next )
211         {
212             for( ; p_out < p_outendline;
213                  p_out++, i_line_orig0 += i_sin, i_col_orig0 += i_cos )
214             {
215                 const int i_line_orig = (i_line_orig0>>12)*i_aspect + i_line_center;
216                 const int i_col_orig  = (i_col_orig0>>12)  + i_col_center;
217                 const uint8_t* p_orig_offset = p_in + i_line_orig * i_pitch
218                                                 + i_col_orig;
219                 const uint8_t i_line_percent = (i_line_orig0>>4) & 255;
220                 const uint8_t i_col_percent  = (i_col_orig0 >>4) & 255;
221
222                 if(    -1 <= i_line_orig && i_line_orig < i_visible_lines
223                     && -1 <= i_col_orig  && i_col_orig  < i_visible_pitch )
224                 {
225                 #define test 1
226                 #undef test
227                 #ifdef test
228                     if( ( i_col_orig > i_visible_pitch/2 ) )
229                 #endif
230                     {
231                         uint8_t i_curpix = black_pixel;
232                         uint8_t i_colpix = black_pixel;
233                         uint8_t i_linpix = black_pixel;
234                         uint8_t i_nexpix = black_pixel;
235                         if( ( 0 <= i_line_orig ) && ( 0 <= i_col_orig ) )
236                             i_curpix = *p_orig_offset;
237                         p_orig_offset++;
238
239                         if(  ( i_col_orig < i_visible_pitch - 1)
240                              && ( i_line_orig >= 0 ) )
241                             i_colpix = *p_orig_offset;
242
243                         p_orig_offset+=i_pitch;
244                         if( ( i_line_orig < i_visible_lines - 1)
245                             && ( i_col_orig  < i_visible_pitch - 1) )
246                             i_nexpix = *p_orig_offset;
247
248                         p_orig_offset--;
249                         if(  ( i_line_orig < i_visible_lines - 1)
250                              && ( i_col_orig >= 0 ) )
251                             i_linpix = *p_orig_offset;
252
253                         unsigned int temp = 0;
254                         temp+= i_curpix *
255                             (256 - i_line_percent) * ( 256 - i_col_percent );
256                         temp+= i_linpix *
257                             i_line_percent * (256 - i_col_percent );
258                         temp+= i_nexpix *
259                             ( i_col_percent) * ( i_line_percent);
260                         temp+= i_colpix *
261                             i_col_percent * (256 - i_line_percent );
262                         *p_out = temp >> 16;
263                     }
264                 #ifdef test
265                     else if (i_col_orig == i_visible_pitch/2 )
266                     {   *p_out = black_pixel;
267                     }
268                     else
269                         *p_out = *p_orig_offset;
270                 #endif
271                 #undef test
272                 }
273                 else
274                 {
275                     *p_out = black_pixel;
276                 }
277             }
278         }
279     }
280
281     return CopyInfoAndRelease( p_outpic, p_pic );
282 }
283
284 /*****************************************************************************
285  *
286  *****************************************************************************/
287 static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
288 {
289     picture_t *p_outpic;
290     filter_sys_t *p_sys = p_filter->p_sys;
291     const int i_sin = p_sys->i_sin, i_cos = p_sys->i_cos;
292
293     if( !p_pic ) return NULL;
294
295     int i_u_offset, i_v_offset, i_y_offset;
296
297     if( GetPackedYuvOffsets( p_pic->format.i_chroma, &i_y_offset,
298                              &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
299     {
300         msg_Warn( p_filter, "Unsupported input chroma (%4s)",
301                   (char*)&(p_pic->format.i_chroma) );
302         picture_Release( p_pic );
303         return NULL;
304     }
305
306     p_outpic = filter_NewPicture( p_filter );
307     if( !p_outpic )
308     {
309         picture_Release( p_pic );
310         return NULL;
311     }
312
313     const uint8_t *p_in   = p_pic->p->p_pixels+i_y_offset;
314     const uint8_t *p_in_u = p_pic->p->p_pixels+i_u_offset;
315     const uint8_t *p_in_v = p_pic->p->p_pixels+i_v_offset;
316
317     const int i_pitch         = p_pic->p->i_pitch;
318     const int i_visible_pitch = p_pic->p->i_visible_pitch>>1; /* In fact it's i_visible_pixels */
319     const int i_visible_lines = p_pic->p->i_visible_lines;
320
321     uint8_t *p_out   = p_outpic->p->p_pixels+i_y_offset;
322     uint8_t *p_out_u = p_outpic->p->p_pixels+i_u_offset;
323     uint8_t *p_out_v = p_outpic->p->p_pixels+i_v_offset;
324
325     const int i_line_center = i_visible_lines>>1;
326     const int i_col_center  = i_visible_pitch>>1;
327
328     int i_col, i_line;
329     for( i_line = 0; i_line < i_visible_lines; i_line++ )
330     {
331         for( i_col = 0; i_col < i_visible_pitch; i_col++ )
332         {
333             int i_line_orig;
334             int i_col_orig;
335             /* Handle "1st Y", U and V */
336             i_line_orig = i_line_center +
337                 ( ( i_sin * ( i_col - i_col_center )
338                   + i_cos * ( i_line - i_line_center ) )>>12 );
339             i_col_orig = i_col_center +
340                 ( ( i_cos * ( i_col - i_col_center )
341                   - i_sin * ( i_line - i_line_center ) )>>12 );
342             if( 0 <= i_col_orig && i_col_orig < i_visible_pitch
343              && 0 <= i_line_orig && i_line_orig < i_visible_lines )
344             {
345                 p_out[i_line*i_pitch+2*i_col] = p_in[i_line_orig*i_pitch+2*i_col_orig];
346                 i_col_orig /= 2;
347                 p_out_u[i_line*i_pitch+2*i_col] = p_in_u[i_line_orig*i_pitch+4*i_col_orig];
348                 p_out_v[i_line*i_pitch+2*i_col] = p_in_v[i_line_orig*i_pitch+4*i_col_orig];
349             }
350             else
351             {
352                 p_out[i_line*i_pitch+2*i_col] = 0x00;
353                 p_out_u[i_line*i_pitch+2*i_col] = 0x80;
354                 p_out_v[i_line*i_pitch+2*i_col] = 0x80;
355             }
356
357             /* Handle "2nd Y" */
358             i_col++;
359             if( i_col >= i_visible_pitch )
360                 break;
361
362             i_line_orig = i_line_center +
363                 ( ( i_sin * ( i_col - i_col_center )
364                   + i_cos * ( i_line - i_line_center ) )>>12 );
365             i_col_orig = i_col_center +
366                 ( ( i_cos * ( i_col - i_col_center )
367                   - i_sin * ( i_line - i_line_center ) )>>12 );
368             if( 0 <= i_col_orig && i_col_orig < i_visible_pitch
369              && 0 <= i_line_orig && i_line_orig < i_visible_lines )
370             {
371                 p_out[i_line*i_pitch+2*i_col] = p_in[i_line_orig*i_pitch+2*i_col_orig];
372             }
373             else
374             {
375                 p_out[i_line*i_pitch+2*i_col] = 0x00;
376             }
377         }
378     }
379
380     return CopyInfoAndRelease( p_outpic, p_pic );
381 }
382
383 /*****************************************************************************
384  * Angle modification callbacks.
385  *****************************************************************************/
386 static int RotateCallback( vlc_object_t *p_this, char const *psz_var,
387                            vlc_value_t oldval, vlc_value_t newval,
388                            void *p_data )
389 {
390     VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
391     filter_sys_t *p_sys = (filter_sys_t *)p_data;
392     p_sys->i_angle = newval.i_int*10;
393     cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );
394     return VLC_SUCCESS;
395 }
396
397 static int PreciseRotateCallback( vlc_object_t *p_this, char const *psz_var,
398                            vlc_value_t oldval, vlc_value_t newval,
399                            void *p_data )
400 {
401     VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
402     filter_sys_t *p_sys = (filter_sys_t *)p_data;
403     p_sys->i_angle = newval.i_int;
404     cache_trigo( p_sys->i_angle, &p_sys->i_sin, &p_sys->i_cos );
405     return VLC_SUCCESS;
406 }