Forward port [17015] + a few more i18n fix
[vlc.git] / modules / video_filter / wave.c
1 /*****************************************************************************
2  * wave.c : Wave video effect plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Antoine Cellerier <dionoea -at- videolan -dot- org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #include <math.h>                                            /* sin(), cos() */
32
33 #include <vlc/vlc.h>
34 #include <vlc/decoder.h>
35
36 #include "vlc_filter.h"
37
38 /*****************************************************************************
39  * Local prototypes
40  *****************************************************************************/
41 static int  Create    ( vlc_object_t * );
42 static void Destroy   ( vlc_object_t * );
43
44 static picture_t *Filter( filter_t *, picture_t * );
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 vlc_module_begin();
50     set_description( _("Wave video filter") );
51     set_shortname( _( "Wave" ));
52     set_capability( "video filter2", 0 );
53     set_category( CAT_VIDEO );
54     set_subcategory( SUBCAT_VIDEO_VFILTER );
55
56     add_shortcut( "wave" );
57     set_callbacks( Create, Destroy );
58 vlc_module_end();
59
60 /*****************************************************************************
61  * vout_sys_t: Distort video output method descriptor
62  *****************************************************************************
63  * This structure is part of the video output thread descriptor.
64  * It describes the Distort specific properties of an output thread.
65  *****************************************************************************/
66 struct filter_sys_t
67 {
68     double  f_angle;
69     mtime_t last_date;
70 };
71
72 /*****************************************************************************
73  * Create: allocates Distort video thread output method
74  *****************************************************************************
75  * This function allocates and initializes a Distort vout method.
76  *****************************************************************************/
77 static int Create( vlc_object_t *p_this )
78 {
79     filter_t *p_filter = (filter_t *)p_this;
80
81     /* Allocate structure */
82     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
83     if( p_filter->p_sys == NULL )
84     {
85         msg_Err( p_filter, "out of memory" );
86         return VLC_ENOMEM;
87     }
88
89     p_filter->pf_video_filter = Filter;
90
91     p_filter->p_sys->f_angle = 0.0;
92     p_filter->p_sys->last_date = 0;
93
94     return VLC_SUCCESS;
95 }
96
97 /*****************************************************************************
98  * Destroy: destroy Distort video thread output method
99  *****************************************************************************
100  * Terminate an output method created by DistortCreateOutputMethod
101  *****************************************************************************/
102 static void Destroy( vlc_object_t *p_this )
103 {
104     filter_t *p_filter = (filter_t *)p_this;
105     free( p_filter->p_sys );
106 }
107
108 /*****************************************************************************
109  * Render: displays previously rendered output
110  *****************************************************************************
111  * This function send the currently rendered image to Distort image, waits
112  * until it is displayed and switch the two rendering buffers, preparing next
113  * frame.
114  *****************************************************************************/
115 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
116 {
117     picture_t *p_outpic;
118     int i_index;
119     double f_angle;
120     mtime_t new_date = mdate();
121
122     if( !p_pic ) return NULL;
123
124     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
125     if( !p_outpic )
126     {
127         msg_Warn( p_filter, "can't get output picture" );
128         if( p_pic->pf_release )
129             p_pic->pf_release( p_pic );
130         return NULL;
131     }
132
133     p_filter->p_sys->f_angle += (new_date - p_filter->p_sys->last_date) / 200000.0;
134     p_filter->p_sys->last_date = new_date;
135     f_angle = p_filter->p_sys->f_angle;
136
137     for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
138     {
139         int i_line, i_num_lines, i_offset;
140         uint8_t black_pixel;
141         uint8_t *p_in, *p_out;
142
143         p_in = p_pic->p[i_index].p_pixels;
144         p_out = p_outpic->p[i_index].p_pixels;
145
146         i_num_lines = p_pic->p[i_index].i_visible_lines;
147
148         black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
149
150         /* Ok, we do 3 times the sin() calculation for each line. So what ? */
151         for( i_line = 0 ; i_line < i_num_lines ; i_line++ )
152         {
153             /* Calculate today's offset, don't go above 1/20th of the screen */
154             i_offset = (int)( (double)(p_pic->p[i_index].i_visible_pitch)
155                          * sin( f_angle + 10.0 * (double)i_line
156                                                / (double)i_num_lines )
157                          / 20.0 );
158
159             if( i_offset )
160             {
161                 if( i_offset < 0 )
162                 {
163                     p_filter->p_libvlc->pf_memcpy( p_out, p_in - i_offset,
164                              p_pic->p[i_index].i_visible_pitch + i_offset );
165                     p_in += p_pic->p[i_index].i_pitch;
166                     p_out += p_outpic->p[i_index].i_pitch;
167                     memset( p_out + i_offset, black_pixel, -i_offset );
168                 }
169                 else
170                 {
171                     p_filter->p_libvlc->pf_memcpy( p_out + i_offset, p_in,
172                              p_pic->p[i_index].i_visible_pitch - i_offset );
173                     memset( p_out, black_pixel, i_offset );
174                     p_in += p_pic->p[i_index].i_pitch;
175                     p_out += p_outpic->p[i_index].i_pitch;
176                 }
177             }
178             else
179             {
180                 p_filter->p_libvlc->pf_memcpy( p_out, p_in,
181                                           p_pic->p[i_index].i_visible_pitch );
182                 p_in += p_pic->p[i_index].i_pitch;
183                 p_out += p_outpic->p[i_index].i_pitch;
184             }
185
186         }
187     }
188
189     p_outpic->date = p_pic->date;
190     p_outpic->b_force = p_pic->b_force;
191     p_outpic->i_nb_fields = p_pic->i_nb_fields;
192     p_outpic->b_progressive = p_pic->b_progressive;
193     p_outpic->b_top_field_first = p_pic->b_top_field_first;
194
195     if( p_pic->pf_release )
196         p_pic->pf_release( p_pic );
197
198     return p_outpic;
199 }