Wave and ripple should now behave correctly for chromas where pixels can take more...
[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
29 #include <math.h>                                            /* sin(), cos() */
30
31 #include <vlc/vlc.h>
32 #include <vlc_vout.h>
33
34 #include "vlc_filter.h"
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static int  Create    ( vlc_object_t * );
40 static void Destroy   ( vlc_object_t * );
41
42 static picture_t *Filter( filter_t *, picture_t * );
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 vlc_module_begin();
48     set_description( _("Wave video filter") );
49     set_shortname( _( "Wave" ));
50     set_capability( "video filter2", 0 );
51     set_category( CAT_VIDEO );
52     set_subcategory( SUBCAT_VIDEO_VFILTER );
53
54     add_shortcut( "wave" );
55     set_callbacks( Create, Destroy );
56 vlc_module_end();
57
58 /*****************************************************************************
59  * vout_sys_t: Distort video output method descriptor
60  *****************************************************************************
61  * This structure is part of the video output thread descriptor.
62  * It describes the Distort specific properties of an output thread.
63  *****************************************************************************/
64 struct filter_sys_t
65 {
66     double  f_angle;
67     mtime_t last_date;
68 };
69
70 /*****************************************************************************
71  * Create: allocates Distort video thread output method
72  *****************************************************************************
73  * This function allocates and initializes a Distort vout method.
74  *****************************************************************************/
75 static int Create( vlc_object_t *p_this )
76 {
77     filter_t *p_filter = (filter_t *)p_this;
78
79     /* Allocate structure */
80     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
81     if( p_filter->p_sys == NULL )
82     {
83         msg_Err( p_filter, "out of memory" );
84         return VLC_ENOMEM;
85     }
86
87     p_filter->pf_video_filter = Filter;
88
89     p_filter->p_sys->f_angle = 0.0;
90     p_filter->p_sys->last_date = 0;
91
92     return VLC_SUCCESS;
93 }
94
95 /*****************************************************************************
96  * Destroy: destroy Distort video thread output method
97  *****************************************************************************
98  * Terminate an output method created by DistortCreateOutputMethod
99  *****************************************************************************/
100 static void Destroy( vlc_object_t *p_this )
101 {
102     filter_t *p_filter = (filter_t *)p_this;
103     free( p_filter->p_sys );
104 }
105
106 /*****************************************************************************
107  * Render: displays previously rendered output
108  *****************************************************************************
109  * This function send the currently rendered image to Distort image, waits
110  * until it is displayed and switch the two rendering buffers, preparing next
111  * frame.
112  *****************************************************************************/
113 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
114 {
115     picture_t *p_outpic;
116     int i_index;
117     double f_angle;
118     mtime_t new_date = mdate();
119
120     if( !p_pic ) return NULL;
121
122     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
123     if( !p_outpic )
124     {
125         msg_Warn( p_filter, "can't get output picture" );
126         if( p_pic->pf_release )
127             p_pic->pf_release( p_pic );
128         return NULL;
129     }
130
131     p_filter->p_sys->f_angle += (new_date - p_filter->p_sys->last_date) / 200000.0;
132     p_filter->p_sys->last_date = new_date;
133     f_angle = p_filter->p_sys->f_angle;
134
135     for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
136     {
137         int i_line, i_num_lines, i_visible_pitch, i_pixel_pitch, i_offset,
138             i_visible_pixels;
139         uint8_t black_pixel;
140         uint8_t *p_in, *p_out;
141
142         p_in = p_pic->p[i_index].p_pixels;
143         p_out = p_outpic->p[i_index].p_pixels;
144
145         i_num_lines = p_pic->p[i_index].i_visible_lines;
146         i_visible_pitch = p_pic->p[i_index].i_visible_pitch;
147         i_pixel_pitch = p_pic->p[i_index].i_pixel_pitch;
148         i_visible_pixels = i_visible_pitch/i_pixel_pitch;
149
150         black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
151
152         /* Ok, we do 3 times the sin() calculation for each line. So what ? */
153         for( i_line = 0 ; i_line < i_num_lines ; i_line++ )
154         {
155             /* Calculate today's offset, don't go above 1/20th of the screen */
156             i_offset = (int)( (double)(i_visible_pixels)
157                          * sin( f_angle + 10.0 * (double)i_line
158                                                / (double)i_num_lines )
159                          / 20.0 )*i_pixel_pitch;
160
161             if( i_offset )
162             {
163                 if( i_offset < 0 )
164                 {
165                     p_filter->p_libvlc->pf_memcpy( p_out, p_in - i_offset,
166                                                    i_visible_pitch + i_offset );
167                     p_in += p_pic->p[i_index].i_pitch;
168                     p_out += p_outpic->p[i_index].i_pitch;
169                     p_filter->p_libvlc->pf_memset( p_out + i_offset,
170                                                    black_pixel, -i_offset );
171                 }
172                 else
173                 {
174                     p_filter->p_libvlc->pf_memcpy( p_out + i_offset, p_in,
175                                                    i_visible_pitch - i_offset );
176                     p_filter->p_libvlc->pf_memset( p_out, black_pixel,
177                                                    i_offset );
178                     p_in += p_pic->p[i_index].i_pitch;
179                     p_out += p_outpic->p[i_index].i_pitch;
180                 }
181             }
182             else
183             {
184                 p_filter->p_libvlc->pf_memcpy( p_out, p_in,
185                                                i_visible_pitch );
186                 p_in += p_pic->p[i_index].i_pitch;
187                 p_out += p_outpic->p[i_index].i_pitch;
188             }
189
190         }
191     }
192
193     p_outpic->date = p_pic->date;
194     p_outpic->b_force = p_pic->b_force;
195     p_outpic->i_nb_fields = p_pic->i_nb_fields;
196     p_outpic->b_progressive = p_pic->b_progressive;
197     p_outpic->b_top_field_first = p_pic->b_top_field_first;
198
199     if( p_pic->pf_release )
200         p_pic->pf_release( p_pic );
201
202     return p_outpic;
203 }