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