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