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