]> git.sesse.net Git - vlc/blob - modules/video_filter/distort.c
Fix a bug with preferences
[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$
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_shortname( N_( "Distortion" ));
68     set_capability( "video filter", 0 );
69     set_category( CAT_VIDEO );
70     set_subcategory( SUBCAT_VIDEO_VFILTER );
71
72     add_string( "distort-mode", "wave", NULL, MODE_TEXT, MODE_LONGTEXT,
73                 VLC_FALSE );
74         change_string_list( mode_list, mode_list_text, 0 );
75
76     add_shortcut( "distort" );
77     set_callbacks( Create, Destroy );
78 vlc_module_end();
79
80 /*****************************************************************************
81  * vout_sys_t: Distort video output method descriptor
82  *****************************************************************************
83  * This structure is part of the video output thread descriptor.
84  * It describes the Distort specific properties of an output thread.
85  *****************************************************************************/
86 struct vout_sys_t
87 {
88     int i_mode;
89     vout_thread_t *p_vout;
90
91     /* For the wave mode */
92     double  f_angle;
93     mtime_t last_date;
94 };
95
96 /*****************************************************************************
97  * Control: control facility for the vout (forwards to child vout)
98  *****************************************************************************/
99 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
100 {
101     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
102 }
103
104 /*****************************************************************************
105  * Create: allocates Distort video thread output method
106  *****************************************************************************
107  * This function allocates and initializes a Distort vout method.
108  *****************************************************************************/
109 static int Create( vlc_object_t *p_this )
110 {
111     vout_thread_t *p_vout = (vout_thread_t *)p_this;
112     char *psz_method, *psz_method_tmp;
113
114     /* Allocate structure */
115     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
116     if( p_vout->p_sys == NULL )
117     {
118         msg_Err( p_vout, "out of memory" );
119         return VLC_ENOMEM;
120     }
121
122     p_vout->pf_init = Init;
123     p_vout->pf_end = End;
124     p_vout->pf_manage = NULL;
125     p_vout->pf_render = Render;
126     p_vout->pf_display = NULL;
127     p_vout->pf_control = Control;
128
129     p_vout->p_sys->i_mode = 0;
130
131     if( !(psz_method = psz_method_tmp
132           = config_GetPsz( p_vout, "distort-mode" )) )
133     {
134         msg_Err( p_vout, "configuration variable %s empty, using 'wave'",
135                          "distort-mode" );
136         p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
137     }
138     else
139     {
140
141         if( !strcmp( psz_method, "wave" ) )
142         {
143             p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
144         }
145         else if( !strcmp( psz_method, "ripple" ) )
146         {
147             p_vout->p_sys->i_mode = DISTORT_MODE_RIPPLE;
148         }
149         else
150         {
151             msg_Err( p_vout, "no valid distort mode provided, "
152                              "using wave" );
153             p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
154         }
155     }
156     free( psz_method_tmp );
157
158     return VLC_SUCCESS;
159 }
160
161 /*****************************************************************************
162  * Init: initialize Distort video thread output method
163  *****************************************************************************/
164 static int Init( vout_thread_t *p_vout )
165 {
166     int i_index;
167     picture_t *p_pic;
168
169     I_OUTPUTPICTURES = 0;
170
171     /* Initialize the output structure */
172     p_vout->output.i_chroma = p_vout->render.i_chroma;
173     p_vout->output.i_width  = p_vout->render.i_width;
174     p_vout->output.i_height = p_vout->render.i_height;
175     p_vout->output.i_aspect = p_vout->render.i_aspect;
176
177     /* Try to open the real video output */
178     msg_Dbg( p_vout, "spawning the real video output" );
179
180     p_vout->p_sys->p_vout = vout_Create( p_vout,
181                            p_vout->render.i_width, p_vout->render.i_height,
182                            p_vout->render.i_chroma, p_vout->render.i_aspect );
183
184     /* Everything failed */
185     if( p_vout->p_sys->p_vout == NULL )
186     {
187         msg_Err( p_vout, "cannot open vout, aborting" );
188         return VLC_EGENERIC;
189     }
190
191     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
192
193     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
194
195     ADD_PARENT_CALLBACKS( SendEventsToChild );
196
197     p_vout->p_sys->f_angle = 0.0;
198     p_vout->p_sys->last_date = 0;
199
200     return VLC_SUCCESS;
201 }
202
203 /*****************************************************************************
204  * End: terminate Distort video thread output method
205  *****************************************************************************/
206 static void End( vout_thread_t *p_vout )
207 {
208     int i_index;
209
210     /* Free the fake output buffers we allocated */
211     for( i_index = I_OUTPUTPICTURES ; i_index ; )
212     {
213         i_index--;
214         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
215     }
216 }
217
218 /*****************************************************************************
219  * Destroy: destroy Distort video thread output method
220  *****************************************************************************
221  * Terminate an output method created by DistortCreateOutputMethod
222  *****************************************************************************/
223 static void Destroy( vlc_object_t *p_this )
224 {
225     vout_thread_t *p_vout = (vout_thread_t *)p_this;
226
227     if( p_vout->p_sys->p_vout )
228     {
229         DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
230         vlc_object_detach( p_vout->p_sys->p_vout );
231         vout_Destroy( p_vout->p_sys->p_vout );
232     }
233
234     DEL_PARENT_CALLBACKS( SendEventsToChild );
235
236     free( p_vout->p_sys );
237 }
238
239 /*****************************************************************************
240  * Render: displays previously rendered output
241  *****************************************************************************
242  * This function send the currently rendered image to Distort image, waits
243  * until it is displayed and switch the two rendering buffers, preparing next
244  * frame.
245  *****************************************************************************/
246 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
247 {
248     picture_t *p_outpic;
249
250     /* This is a new frame. Get a structure from the video_output. */
251     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
252               == NULL )
253     {
254         if( p_vout->b_die || p_vout->b_error )
255         {
256             return;
257         }
258         msleep( VOUT_OUTMEM_SLEEP );
259     }
260
261     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
262
263     switch( p_vout->p_sys->i_mode )
264     {
265         case DISTORT_MODE_WAVE:
266             DistortWave( p_vout, p_pic, p_outpic );
267             break;
268
269         case DISTORT_MODE_RIPPLE:
270             DistortRipple( p_vout, p_pic, p_outpic );
271             break;
272
273         default:
274             break;
275     }
276
277     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
278 }
279
280 /*****************************************************************************
281  * DistortWave: draw a wave effect on the picture
282  *****************************************************************************/
283 static void DistortWave( vout_thread_t *p_vout, picture_t *p_inpic,
284                                                 picture_t *p_outpic )
285 {
286     int i_index;
287     double f_angle;
288     mtime_t new_date = mdate();
289
290     p_vout->p_sys->f_angle += (new_date - p_vout->p_sys->last_date) / 200000.0;
291     p_vout->p_sys->last_date = new_date;
292     f_angle = p_vout->p_sys->f_angle;
293
294     for( i_index = 0 ; i_index < p_inpic->i_planes ; i_index++ )
295     {
296         int i_line, i_num_lines, i_offset;
297         uint8_t black_pixel;
298         uint8_t *p_in, *p_out;
299
300         p_in = p_inpic->p[i_index].p_pixels;
301         p_out = p_outpic->p[i_index].p_pixels;
302
303         i_num_lines = p_inpic->p[i_index].i_visible_lines;
304
305         black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
306
307         /* Ok, we do 3 times the sin() calculation for each line. So what ? */
308         for( i_line = 0 ; i_line < i_num_lines ; i_line++ )
309         {
310             /* Calculate today's offset, don't go above 1/20th of the screen */
311             i_offset = (int)( (double)(p_inpic->p[i_index].i_visible_pitch)
312                          * sin( f_angle + 10.0 * (double)i_line
313                                                / (double)i_num_lines )
314                          / 20.0 );
315
316             if( i_offset )
317             {
318                 if( i_offset < 0 )
319                 {
320                     p_vout->p_vlc->pf_memcpy( p_out, p_in - i_offset,
321                              p_inpic->p[i_index].i_visible_pitch + i_offset );
322                     p_in += p_inpic->p[i_index].i_pitch;
323                     p_out += p_outpic->p[i_index].i_pitch;
324                     memset( p_out + i_offset, black_pixel, -i_offset );
325                 }
326                 else
327                 {
328                     p_vout->p_vlc->pf_memcpy( p_out + i_offset, p_in,
329                              p_inpic->p[i_index].i_visible_pitch - i_offset );
330                     memset( p_out, black_pixel, i_offset );
331                     p_in += p_inpic->p[i_index].i_pitch;
332                     p_out += p_outpic->p[i_index].i_pitch;
333                 }
334             }
335             else
336             {
337                 p_vout->p_vlc->pf_memcpy( p_out, p_in,
338                                           p_inpic->p[i_index].i_visible_pitch );
339                 p_in += p_inpic->p[i_index].i_pitch;
340                 p_out += p_outpic->p[i_index].i_pitch;
341             }
342
343         }
344     }
345 }
346
347 /*****************************************************************************
348  * DistortRipple: draw a ripple effect at the bottom of the picture
349  *****************************************************************************/
350 static void DistortRipple( vout_thread_t *p_vout, picture_t *p_inpic,
351                                                   picture_t *p_outpic )
352 {
353     int i_index;
354     double f_angle;
355     mtime_t new_date = mdate();
356
357     p_vout->p_sys->f_angle -= (p_vout->p_sys->last_date - new_date) / 100000.0;
358     p_vout->p_sys->last_date = new_date;
359     f_angle = p_vout->p_sys->f_angle;
360
361     for( i_index = 0 ; i_index < p_inpic->i_planes ; i_index++ )
362     {
363         int i_line, i_first_line, i_num_lines, i_offset;
364         uint8_t black_pixel;
365         uint8_t *p_in, *p_out;
366
367         black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
368
369         i_num_lines = p_inpic->p[i_index].i_visible_lines;
370
371         i_first_line = i_num_lines * 4 / 5;
372
373         p_in = p_inpic->p[i_index].p_pixels;
374         p_out = p_outpic->p[i_index].p_pixels;
375
376         for( i_line = 0 ; i_line < i_first_line ; i_line++ )
377         {
378             p_vout->p_vlc->pf_memcpy( p_out, p_in,
379                                       p_inpic->p[i_index].i_visible_pitch );
380             p_in += p_inpic->p[i_index].i_pitch;
381             p_out += p_outpic->p[i_index].i_pitch;
382         }
383
384         /* Ok, we do 3 times the sin() calculation for each line. So what ? */
385         for( i_line = i_first_line ; i_line < i_num_lines ; i_line++ )
386         {
387             /* Calculate today's offset, don't go above 1/20th of the screen */
388             i_offset = (int)( (double)(p_inpic->p[i_index].i_pitch)
389                          * sin( f_angle + 2.0 * (double)i_line
390                                               / (double)( 1 + i_line
391                                                             - i_first_line) )
392                          * (double)(i_line - i_first_line)
393                          / (double)i_num_lines
394                          / 8.0 );
395
396             if( i_offset )
397             {
398                 if( i_offset < 0 )
399                 {
400                     p_vout->p_vlc->pf_memcpy( p_out, p_in - i_offset,
401                              p_inpic->p[i_index].i_visible_pitch + i_offset );
402                     p_in -= p_inpic->p[i_index].i_pitch;
403                     p_out += p_outpic->p[i_index].i_pitch;
404                     memset( p_out + i_offset, black_pixel, -i_offset );
405                 }
406                 else
407                 {
408                     p_vout->p_vlc->pf_memcpy( p_out + i_offset, p_in,
409                              p_inpic->p[i_index].i_visible_pitch - i_offset );
410                     memset( p_out, black_pixel, i_offset );
411                     p_in -= p_inpic->p[i_index].i_pitch;
412                     p_out += p_outpic->p[i_index].i_pitch;
413                 }
414             }
415             else
416             {
417                 p_vout->p_vlc->pf_memcpy( p_out, p_in,
418                                           p_inpic->p[i_index].i_visible_pitch );
419                 p_in -= p_inpic->p[i_index].i_pitch;
420                 p_out += p_outpic->p[i_index].i_pitch;
421             }
422
423         }
424     }
425 }
426
427 /*****************************************************************************
428  * SendEvents: forward mouse and keyboard events to the parent p_vout
429  *****************************************************************************/
430 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
431                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
432 {
433     var_Set( (vlc_object_t *)p_data, psz_var, newval );
434
435     return VLC_SUCCESS;
436 }
437
438 /*****************************************************************************
439  * SendEventsToChild: forward events to the child/children vout
440  *****************************************************************************/
441 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
442                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
443 {
444     vout_thread_t *p_vout = (vout_thread_t *)p_this;
445     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
446     return VLC_SUCCESS;
447 }