]> git.sesse.net Git - vlc/blob - modules/video_filter/rotate.c
New rotate video filter. Can rotate video by any integer angle between 0 and 359...
[vlc] / modules / video_filter / rotate.c
1 /*****************************************************************************
2  * rotate.c : video rotation filter
3  *****************************************************************************
4  * Copyright (C) 2000-2006 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 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <math.h>                                            /* sin(), cos() */
31
32 #include <vlc/vlc.h>
33 #include <vlc/decoder.h>
34
35 #include "vlc_filter.h"
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int  Create    ( vlc_object_t * );
41 static void Destroy   ( vlc_object_t * );
42
43 static picture_t *Filter( filter_t *, picture_t * );
44
45 #define ANGLE_TEXT N_("Angle in degrees")
46 #define ANGLE_LONGTEXT N_("Angle in degrees (0 to 359)")
47
48 #define FILTER_PREFIX "rotate-"
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 vlc_module_begin();
54     set_description( _("Rotate video filter") );
55     set_shortname( _( "Rotate" ));
56     set_capability( "video filter2", 0 );
57     set_category( CAT_VIDEO );
58     set_subcategory( SUBCAT_VIDEO_VFILTER );
59
60     add_integer_with_range( FILTER_PREFIX "angle", 0, 0, 359, NULL,
61         ANGLE_TEXT, ANGLE_LONGTEXT, VLC_FALSE );
62
63     add_shortcut( "rotate" );
64     set_callbacks( Create, Destroy );
65 vlc_module_end();
66
67 static const char *ppsz_filter_options[] = {
68     "angle", NULL
69 };
70
71 /*****************************************************************************
72  * vout_sys_t: Distort video output method descriptor
73  *****************************************************************************
74  * This structure is part of the video output thread descriptor.
75  * It describes the Distort specific properties of an output thread.
76  *****************************************************************************/
77 struct filter_sys_t
78 {
79     int     i_angle;
80     mtime_t last_date;
81 };
82
83 /*****************************************************************************
84  * Create: allocates Distort video thread output method
85  *****************************************************************************
86  * This function allocates and initializes a Distort vout method.
87  *****************************************************************************/
88 static int Create( vlc_object_t *p_this )
89 {
90     filter_t *p_filter = (filter_t *)p_this;
91
92     /* Allocate structure */
93     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
94     if( p_filter->p_sys == NULL )
95     {
96         msg_Err( p_filter, "out of memory" );
97         return VLC_ENOMEM;
98     }
99
100     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
101                        p_filter->p_cfg );
102     var_Create( p_filter, FILTER_PREFIX "angle",
103                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
104
105     p_filter->pf_video_filter = Filter;
106
107     p_filter->p_sys->i_angle = var_GetInteger( p_filter,
108                                                FILTER_PREFIX "angle" );
109     p_filter->p_sys->last_date = 0;
110
111     return VLC_SUCCESS;
112 }
113
114 /*****************************************************************************
115  * Destroy: destroy Distort video thread output method
116  *****************************************************************************
117  * Terminate an output method created by DistortCreateOutputMethod
118  *****************************************************************************/
119 static void Destroy( vlc_object_t *p_this )
120 {
121     filter_t *p_filter = (filter_t *)p_this;
122     free( p_filter->p_sys );
123 }
124
125 /*****************************************************************************
126  * Render: displays previously rendered output
127  *****************************************************************************
128  * This function send the currently rendered image to Distort image, waits
129  * until it is displayed and switch the two rendering buffers, preparing next
130  * frame.
131  *****************************************************************************/
132 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
133 {
134     picture_t *p_outpic;
135     int i_index;
136     double f_angle;
137     double f_sin, f_cos;
138     mtime_t new_date = mdate();
139
140     if( !p_pic ) return NULL;
141
142     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
143     if( !p_outpic )
144     {
145         msg_Warn( p_filter, "can't get output picture" );
146         if( p_pic->pf_release )
147             p_pic->pf_release( p_pic );
148         return NULL;
149     }
150
151     p_filter->p_sys->last_date = new_date;
152     f_angle = (double)p_filter->p_sys->i_angle;
153     f_sin = sin( f_angle );
154     f_cos = cos( f_angle );
155
156     for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
157     {
158         int i_line, i_num_lines, i_line_center, i_col, i_num_cols, i_col_center;
159         uint8_t black_pixel;
160         uint8_t *p_in, *p_out;
161
162         p_in = p_pic->p[i_index].p_pixels;
163         p_out = p_outpic->p[i_index].p_pixels;
164
165         i_num_lines = p_pic->p[i_index].i_visible_lines;
166         i_num_cols = p_pic->p[i_index].i_visible_pitch;
167         i_line_center = i_num_lines/2;
168         i_col_center = i_num_cols/2;
169
170         black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
171
172         /* Ok, we do 3 times the sin() calculation for each line. So what ? */
173         for( i_line = 0 ; i_line < i_num_lines ; i_line++ )
174         {
175             for( i_col = 0; i_col < i_num_cols ; i_col++ )
176             {
177                 int i_line_orig, i_col_orig;
178                 i_line_orig = (int)( f_cos * (double)(i_line-i_line_center)
179                                    + f_sin * (double)(i_col-i_col_center) )
180                               + i_line_center;
181                 i_col_orig = (int)(-f_sin * (double)(i_line-i_line_center)
182                                   + f_cos * (double)(i_col-i_col_center) )
183                              + i_col_center;
184
185                 if(    0 <= i_line_orig && i_line_orig < i_num_lines
186                     && 0 <= i_col_orig && i_col_orig < i_num_cols )
187                 {
188
189                     p_out[i_line*i_num_cols+i_col] =
190                         p_in[i_line_orig*i_num_cols+i_col_orig];
191                 }
192                 else
193                 {
194                     p_out[i_line*i_num_cols+i_col] = black_pixel;
195                 }
196             }
197         }
198     }
199
200     p_outpic->date = p_pic->date;
201     p_outpic->b_force = p_pic->b_force;
202     p_outpic->i_nb_fields = p_pic->i_nb_fields;
203     p_outpic->b_progressive = p_pic->b_progressive;
204     p_outpic->b_top_field_first = p_pic->b_top_field_first;
205
206     if( p_pic->pf_release )
207         p_pic->pf_release( p_pic );
208
209     return p_outpic;
210 }