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