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