]> git.sesse.net Git - vlc/blob - modules/video_filter/distort.c
* Stringreview !!!
[vlc] / modules / video_filter / distort.c
1 /*****************************************************************************
2  * distort.c : Misc video effects plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001, 2002, 2003 VideoLAN
5  * $Id: distort.c,v 1.14 2004/01/25 20:05:28 hartman Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <math.h>                                            /* sin(), cos() */
31
32 #include <vlc/vlc.h>
33 #include <vlc/vout.h>
34
35 #include "filter_common.h"
36
37 #define DISTORT_MODE_WAVE    1
38 #define DISTORT_MODE_RIPPLE  2
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static int  Create    ( vlc_object_t * );
44 static void Destroy   ( vlc_object_t * );
45
46 static int  Init      ( vout_thread_t * );
47 static void End       ( vout_thread_t * );
48 static void Render    ( vout_thread_t *, picture_t * );
49
50 static void DistortWave    ( vout_thread_t *, picture_t *, picture_t * );
51 static void DistortRipple  ( vout_thread_t *, picture_t *, picture_t * );
52
53 static int  SendEvents   ( vlc_object_t *, char const *,
54                            vlc_value_t, vlc_value_t, void * );
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59 #define MODE_TEXT N_("Distort mode")
60 #define MODE_LONGTEXT N_("Distort mode, one of \"wave\" and \"ripple\"")
61
62 static char *mode_list[] = { "wave", "ripple" };
63 static char *mode_list_text[] = { N_("Wave"), N_("Ripple") };
64
65 vlc_module_begin();
66     set_description( _("Distort video filter") );
67     set_capability( "video filter", 0 );
68
69     add_string( "distort-mode", "wave", NULL, MODE_TEXT, MODE_LONGTEXT,
70                 VLC_FALSE );
71         change_string_list( mode_list, mode_list_text, 0 );
72
73     add_shortcut( "distort" );
74     set_callbacks( Create, Destroy );
75 vlc_module_end();
76
77 /*****************************************************************************
78  * vout_sys_t: Distort video output method descriptor
79  *****************************************************************************
80  * This structure is part of the video output thread descriptor.
81  * It describes the Distort specific properties of an output thread.
82  *****************************************************************************/
83 struct vout_sys_t
84 {
85     int i_mode;
86     vout_thread_t *p_vout;
87
88     /* For the wave mode */
89     double  f_angle;
90     mtime_t last_date;
91 };
92
93 /*****************************************************************************
94  * Create: allocates Distort video thread output method
95  *****************************************************************************
96  * This function allocates and initializes a Distort vout method.
97  *****************************************************************************/
98 static int Create( vlc_object_t *p_this )
99 {
100     vout_thread_t *p_vout = (vout_thread_t *)p_this;
101     char *psz_method, *psz_method_tmp;
102
103     /* Allocate structure */
104     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
105     if( p_vout->p_sys == NULL )
106     {
107         msg_Err( p_vout, "out of memory" );
108         return VLC_ENOMEM;
109     }
110
111     p_vout->pf_init = Init;
112     p_vout->pf_end = End;
113     p_vout->pf_manage = NULL;
114     p_vout->pf_render = Render;
115     p_vout->pf_display = NULL;
116
117     p_vout->p_sys->i_mode = 0;
118
119     if( !(psz_method = psz_method_tmp
120           = config_GetPsz( p_vout, "distort-mode" )) )
121     {
122         msg_Err( p_vout, "configuration variable %s empty, using 'wave'",
123                          "distort-mode" );
124         p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
125     }
126     else
127     {
128
129         if( !strcmp( psz_method, "wave" ) )
130         {
131             p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
132         }
133         else if( !strcmp( psz_method, "ripple" ) )
134         {
135             p_vout->p_sys->i_mode = DISTORT_MODE_RIPPLE;
136         }
137         else
138         {
139             msg_Err( p_vout, "no valid distort mode provided, "
140                              "using wave" );
141             p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
142         }
143     }
144     free( psz_method_tmp );
145
146     return VLC_SUCCESS;
147 }
148
149 /*****************************************************************************
150  * Init: initialize Distort video thread output method
151  *****************************************************************************/
152 static int Init( vout_thread_t *p_vout )
153 {
154     int i_index;
155     picture_t *p_pic;
156
157     I_OUTPUTPICTURES = 0;
158
159     /* Initialize the output structure */
160     p_vout->output.i_chroma = p_vout->render.i_chroma;
161     p_vout->output.i_width  = p_vout->render.i_width;
162     p_vout->output.i_height = p_vout->render.i_height;
163     p_vout->output.i_aspect = p_vout->render.i_aspect;
164
165     /* Try to open the real video output */
166     msg_Dbg( p_vout, "spawning the real video output" );
167
168     p_vout->p_sys->p_vout = vout_Create( p_vout,
169                            p_vout->render.i_width, p_vout->render.i_height,
170                            p_vout->render.i_chroma, p_vout->render.i_aspect );
171
172     /* Everything failed */
173     if( p_vout->p_sys->p_vout == NULL )
174     {
175         msg_Err( p_vout, "cannot open vout, aborting" );
176
177         return VLC_EGENERIC;
178     }
179
180     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
181
182     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
183
184     ADD_PARENT_CALLBACKS( SendEventsToChild );
185
186     p_vout->p_sys->f_angle = 0.0;
187     p_vout->p_sys->last_date = 0;
188
189     return VLC_SUCCESS;
190 }
191
192 /*****************************************************************************
193  * End: terminate Distort video thread output method
194  *****************************************************************************/
195 static void End( vout_thread_t *p_vout )
196 {
197     int i_index;
198
199     /* Free the fake output buffers we allocated */
200     for( i_index = I_OUTPUTPICTURES ; i_index ; )
201     {
202         i_index--;
203         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
204     }
205 }
206
207 /*****************************************************************************
208  * Destroy: destroy Distort video thread output method
209  *****************************************************************************
210  * Terminate an output method created by DistortCreateOutputMethod
211  *****************************************************************************/
212 static void Destroy( vlc_object_t *p_this )
213 {
214     vout_thread_t *p_vout = (vout_thread_t *)p_this;
215
216     DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
217     vlc_object_detach( p_vout->p_sys->p_vout );
218     vout_Destroy( p_vout->p_sys->p_vout );
219
220     DEL_PARENT_CALLBACKS( SendEventsToChild );
221
222     free( p_vout->p_sys );
223 }
224
225 /*****************************************************************************
226  * Render: displays previously rendered output
227  *****************************************************************************
228  * This function send the currently rendered image to Distort image, waits
229  * until it is displayed and switch the two rendering buffers, preparing next
230  * frame.
231  *****************************************************************************/
232 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
233 {
234     picture_t *p_outpic;
235
236     /* This is a new frame. Get a structure from the video_output. */
237     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
238               == NULL )
239     {
240         if( p_vout->b_die || p_vout->b_error )
241         {
242             return;
243         }
244         msleep( VOUT_OUTMEM_SLEEP );
245     }
246
247     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
248
249     switch( p_vout->p_sys->i_mode )
250     {
251         case DISTORT_MODE_WAVE:
252             DistortWave( p_vout, p_pic, p_outpic );
253             break;
254
255         case DISTORT_MODE_RIPPLE:
256             DistortRipple( p_vout, p_pic, p_outpic );
257             break;
258
259         default:
260             break;
261     }
262
263     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
264 }
265
266 /*****************************************************************************
267  * DistortWave: draw a wave effect on the picture
268  *****************************************************************************/
269 static void DistortWave( vout_thread_t *p_vout, picture_t *p_inpic,
270                                                 picture_t *p_outpic )
271 {
272     int i_index;
273     double f_angle;
274     mtime_t new_date = mdate();
275
276     p_vout->p_sys->f_angle += (new_date - p_vout->p_sys->last_date) / 200000.0;
277     p_vout->p_sys->last_date = new_date;
278     f_angle = p_vout->p_sys->f_angle;
279
280     for( i_index = 0 ; i_index < p_inpic->i_planes ; i_index++ )
281     {
282         int i_line, i_num_lines, i_offset;
283         uint8_t black_pixel;
284         uint8_t *p_in, *p_out;
285
286         p_in = p_inpic->p[i_index].p_pixels;
287         p_out = p_outpic->p[i_index].p_pixels;
288
289         i_num_lines = p_inpic->p[i_index].i_lines;
290
291         black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
292
293         /* Ok, we do 3 times the sin() calculation for each line. So what ? */
294         for( i_line = 0 ; i_line < i_num_lines ; i_line++ )
295         {
296             /* Calculate today's offset, don't go above 1/20th of the screen */
297             i_offset = (int)( (double)(p_inpic->p[i_index].i_visible_pitch)
298                          * sin( f_angle + 10.0 * (double)i_line
299                                                / (double)i_num_lines )
300                          / 20.0 );
301
302             if( i_offset )
303             {
304                 if( i_offset < 0 )
305                 {
306                     p_vout->p_vlc->pf_memcpy( p_out, p_in - i_offset,
307                              p_inpic->p[i_index].i_visible_pitch + i_offset );
308                     p_in += p_inpic->p[i_index].i_pitch;
309                     p_out += p_outpic->p[i_index].i_pitch;
310                     memset( p_out + i_offset, black_pixel, -i_offset );
311                 }
312                 else
313                 {
314                     p_vout->p_vlc->pf_memcpy( p_out + i_offset, p_in,
315                              p_inpic->p[i_index].i_visible_pitch - i_offset );
316                     memset( p_out, black_pixel, i_offset );
317                     p_in += p_inpic->p[i_index].i_pitch;
318                     p_out += p_outpic->p[i_index].i_pitch;
319                 }
320             }
321             else
322             {
323                 p_vout->p_vlc->pf_memcpy( p_out, p_in,
324                                           p_inpic->p[i_index].i_visible_pitch );
325                 p_in += p_inpic->p[i_index].i_pitch;
326                 p_out += p_outpic->p[i_index].i_pitch;
327             }
328
329         }
330     }
331 }
332
333 /*****************************************************************************
334  * DistortRipple: draw a ripple effect at the bottom of the picture
335  *****************************************************************************/
336 static void DistortRipple( vout_thread_t *p_vout, picture_t *p_inpic,
337                                                   picture_t *p_outpic )
338 {
339     int i_index;
340     double f_angle;
341     mtime_t new_date = mdate();
342
343     p_vout->p_sys->f_angle -= (p_vout->p_sys->last_date - new_date) / 100000.0;
344     p_vout->p_sys->last_date = new_date;
345     f_angle = p_vout->p_sys->f_angle;
346
347     for( i_index = 0 ; i_index < p_inpic->i_planes ; i_index++ )
348     {
349         int i_line, i_first_line, i_num_lines, i_offset;
350         uint8_t black_pixel;
351         uint8_t *p_in, *p_out;
352
353         black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
354
355         i_num_lines = p_inpic->p[i_index].i_lines;
356
357         i_first_line = i_num_lines * 4 / 5;
358
359         p_in = p_inpic->p[i_index].p_pixels;
360         p_out = p_outpic->p[i_index].p_pixels;
361
362         for( i_line = 0 ; i_line < i_first_line ; i_line++ )
363         {
364             p_vout->p_vlc->pf_memcpy( p_out, p_in,
365                                       p_inpic->p[i_index].i_visible_pitch );
366             p_in += p_inpic->p[i_index].i_pitch;
367             p_out += p_outpic->p[i_index].i_pitch;
368         }
369
370         /* Ok, we do 3 times the sin() calculation for each line. So what ? */
371         for( i_line = i_first_line ; i_line < i_num_lines ; i_line++ )
372         {
373             /* Calculate today's offset, don't go above 1/20th of the screen */
374             i_offset = (int)( (double)(p_inpic->p[i_index].i_pitch)
375                          * sin( f_angle + 2.0 * (double)i_line
376                                               / (double)( 1 + i_line
377                                                             - i_first_line) )
378                          * (double)(i_line - i_first_line)
379                          / (double)i_num_lines
380                          / 8.0 );
381
382             if( i_offset )
383             {
384                 if( i_offset < 0 )
385                 {
386                     p_vout->p_vlc->pf_memcpy( p_out, p_in - i_offset,
387                              p_inpic->p[i_index].i_visible_pitch + i_offset );
388                     p_in -= p_inpic->p[i_index].i_pitch;
389                     p_out += p_outpic->p[i_index].i_pitch;
390                     memset( p_out + i_offset, black_pixel, -i_offset );
391                 }
392                 else
393                 {
394                     p_vout->p_vlc->pf_memcpy( p_out + i_offset, p_in,
395                              p_inpic->p[i_index].i_visible_pitch - i_offset );
396                     memset( p_out, black_pixel, i_offset );
397                     p_in -= p_inpic->p[i_index].i_pitch;
398                     p_out += p_outpic->p[i_index].i_pitch;
399                 }
400             }
401             else
402             {
403                 p_vout->p_vlc->pf_memcpy( p_out, p_in,
404                                           p_inpic->p[i_index].i_visible_pitch );
405                 p_in -= p_inpic->p[i_index].i_pitch;
406                 p_out += p_outpic->p[i_index].i_pitch;
407             }
408
409         }
410     }
411 }
412
413 /*****************************************************************************
414  * SendEvents: forward mouse and keyboard events to the parent p_vout
415  *****************************************************************************/
416 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
417                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
418 {
419     var_Set( (vlc_object_t *)p_data, psz_var, newval );
420
421     return VLC_SUCCESS;
422 }
423
424 /*****************************************************************************
425  * SendEventsToChild: forward events to the child/children vout
426  *****************************************************************************/
427 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
428                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
429 {
430     vout_thread_t *p_vout = (vout_thread_t *)p_this;
431     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
432     return VLC_SUCCESS;
433 }