]> git.sesse.net Git - vlc/blob - modules/video_filter/ripple.c
decoder: do not mix and match condidition variable and mutex pairings
[vlc] / modules / video_filter / ripple.c
1 /*****************************************************************************
2  * ripple.c : Ripple video effect plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2006 VLC authors and VideoLAN
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 it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <math.h>                                            /* sin(), cos() */
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_filter.h>
38 #include "filter_picture.h"
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static int  Create    ( vlc_object_t * );
44 static void Destroy   ( vlc_object_t * );
45
46 static picture_t *Filter( filter_t *, picture_t * );
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 vlc_module_begin ()
52     set_description( N_("Ripple video filter") )
53     set_shortname( N_( "Ripple" ))
54     set_capability( "video filter2", 0 )
55     set_category( CAT_VIDEO )
56     set_subcategory( SUBCAT_VIDEO_VFILTER )
57
58     add_shortcut( "ripple" )
59     set_callbacks( Create, Destroy )
60 vlc_module_end ()
61
62 /*****************************************************************************
63  * filter_sys_t: Distort video output method descriptor
64  *****************************************************************************
65  * This structure is part of the video output thread descriptor.
66  * It describes the Distort specific properties of an output thread.
67  *****************************************************************************/
68 struct filter_sys_t
69 {
70     double  f_angle;
71     mtime_t last_date;
72 };
73
74 /*****************************************************************************
75  * Create: allocates Distort video thread output method
76  *****************************************************************************
77  * This function allocates and initializes a Distort vout method.
78  *****************************************************************************/
79 static int Create( vlc_object_t *p_this )
80 {
81     filter_t *p_filter = (filter_t *)p_this;
82
83     const vlc_chroma_description_t *p_chroma =
84         vlc_fourcc_GetChromaDescription( p_filter->fmt_in.video.i_chroma );
85     if( p_chroma == NULL || p_chroma->plane_count == 0 )
86         return VLC_EGENERIC;
87
88     /* Allocate structure */
89     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
90     if( p_filter->p_sys == NULL )
91         return VLC_ENOMEM;
92
93     p_filter->pf_video_filter = Filter;
94
95     p_filter->p_sys->f_angle = 0.0;
96     p_filter->p_sys->last_date = 0;
97
98     return VLC_SUCCESS;
99 }
100
101 /*****************************************************************************
102  * Destroy: destroy Distort video thread output method
103  *****************************************************************************
104  * Terminate an output method created by DistortCreateOutputMethod
105  *****************************************************************************/
106 static void Destroy( vlc_object_t *p_this )
107 {
108     filter_t *p_filter = (filter_t *)p_this;
109     free( p_filter->p_sys );
110 }
111
112 /*****************************************************************************
113  * Render: displays previously rendered output
114  *****************************************************************************
115  * This function send the currently rendered image to Distort image, waits
116  * until it is displayed and switch the two rendering buffers, preparing next
117  * frame.
118  *****************************************************************************/
119 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
120 {
121     picture_t *p_outpic;
122     int i_index;
123     double f_angle;
124     mtime_t new_date = mdate();
125
126     if( !p_pic ) return NULL;
127
128     p_outpic = filter_NewPicture( p_filter );
129     if( !p_outpic )
130     {
131         picture_Release( p_pic );
132         return NULL;
133     }
134
135     p_filter->p_sys->f_angle -= (p_filter->p_sys->last_date - new_date) / 100000.0;
136     p_filter->p_sys->last_date = new_date;
137     f_angle = p_filter->p_sys->f_angle;
138
139     for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
140     {
141         int i_line, i_first_line, i_num_lines, i_offset, i_pixel_pitch,
142             i_visible_pixels;
143         uint8_t black_pixel;
144         uint8_t *p_in, *p_out;
145
146         black_pixel = ( p_pic->i_planes > 1 && i_index == Y_PLANE ) ? 0x00
147                                                                     : 0x80;
148
149         i_num_lines = p_pic->p[i_index].i_visible_lines;
150         i_pixel_pitch = p_pic->p[i_index].i_pixel_pitch;
151         switch( p_filter->fmt_in.video.i_chroma )
152         {
153             CASE_PACKED_YUV_422
154                 // Quick hack to fix u/v inversion occuring with 2 byte pixel pitch
155                 i_pixel_pitch *= 2;
156                 break;
157         }
158
159         i_visible_pixels = p_pic->p[i_index].i_visible_pitch/i_pixel_pitch;
160
161         i_first_line = i_num_lines * 4 / 5;
162
163         p_in = p_pic->p[i_index].p_pixels;
164         p_out = p_outpic->p[i_index].p_pixels;
165
166         for( i_line = 0 ; i_line < i_first_line ; i_line++ )
167         {
168             memcpy( p_out, p_in, p_pic->p[i_index].i_visible_pitch );
169             p_in += p_pic->p[i_index].i_pitch;
170             p_out += p_outpic->p[i_index].i_pitch;
171         }
172
173         /* Ok, we do 3 times the sin() calculation for each line. So what ? */
174         for( i_line = i_first_line ; i_line < i_num_lines ; i_line++ )
175         {
176             /* Calculate today's offset, don't go above 1/20th of the screen */
177             i_offset = (int)( (double)(i_visible_pixels)
178                          * sin( f_angle + 2.0 * (double)i_line
179                                               / (double)( 1 + i_line
180                                                             - i_first_line) )
181                          * (double)(i_line - i_first_line)
182                          / (double)i_num_lines
183                          / 8.0 )*i_pixel_pitch;
184
185             if( i_offset )
186             {
187                 if( i_offset < 0 )
188                 {
189                     memcpy( p_out, p_in - i_offset,
190                                 p_pic->p[i_index].i_visible_pitch + i_offset );
191                     p_in -= p_pic->p[i_index].i_pitch;
192                     p_out += p_outpic->p[i_index].i_pitch;
193                     memset( p_out + i_offset, black_pixel, -i_offset );
194                 }
195                 else
196                 {
197                     memcpy( p_out + i_offset, p_in,
198                                 p_pic->p[i_index].i_visible_pitch - i_offset );
199                     memset( p_out, black_pixel, i_offset );
200                     p_in -= p_pic->p[i_index].i_pitch;
201                     p_out += p_outpic->p[i_index].i_pitch;
202                 }
203             }
204             else
205             {
206                 memcpy( p_out, p_in, p_pic->p[i_index].i_visible_pitch );
207                 p_in -= p_pic->p[i_index].i_pitch;
208                 p_out += p_outpic->p[i_index].i_pitch;
209             }
210
211         }
212     }
213
214     return CopyInfoAndRelease( p_outpic, p_pic );
215 }