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