* ./modules/*: moved plugins to the new tree. Yet untested builds include
[vlc.git] / modules / video_filter / distort.c
1 /*****************************************************************************
2  * distort.c : Misc video effects plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: distort.c,v 1.1 2002/08/04 17:23:43 sam 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 <errno.h>
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #include <math.h>                                            /* sin(), cos() */
32
33 #include <vlc/vlc.h>
34 #include <vlc/vout.h>
35
36 #include "filter_common.h"
37
38 #define DISTORT_MODE_WAVE    1
39 #define DISTORT_MODE_RIPPLE  2
40
41 /*****************************************************************************
42  * Local prototypes
43  *****************************************************************************/
44 static int  Create    ( vlc_object_t * );
45 static void Destroy   ( vlc_object_t * );
46
47 static int  Init      ( vout_thread_t * );
48 static void End       ( vout_thread_t * );
49 static void Render    ( vout_thread_t *, picture_t * );
50
51 static void DistortWave    ( vout_thread_t *, picture_t *, picture_t * );
52 static void DistortRipple  ( vout_thread_t *, picture_t *, picture_t * );
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 #define MODE_TEXT N_("Distort mode")
58 #define MODE_LONGTEXT N_("one of \"wave\" and \"ripple\"")
59
60 static char *mode_list[] = { "wave", "ripple", NULL };
61
62 vlc_module_begin();
63     add_category_hint( N_("Miscellaneous"), NULL );
64     add_string_from_list( "distort-mode", "wave", mode_list, NULL,
65                           MODE_TEXT, MODE_LONGTEXT );
66     set_description( _("miscellaneous video effects module") );
67     set_capability( "video filter", 0 );
68     add_shortcut( "distort" );
69     set_callbacks( Create, Destroy );
70 vlc_module_end();
71
72 /*****************************************************************************
73  * vout_sys_t: Distort video output method descriptor
74  *****************************************************************************
75  * This structure is part of the video output thread descriptor.
76  * It describes the Distort specific properties of an output thread.
77  *****************************************************************************/
78 struct vout_sys_t
79 {
80     int i_mode;
81     vout_thread_t *p_vout;
82
83     /* For the wave mode */
84     double  f_angle;
85     mtime_t last_date;
86 };
87
88 /*****************************************************************************
89  * Create: allocates Distort video thread output method
90  *****************************************************************************
91  * This function allocates and initializes a Distort vout method.
92  *****************************************************************************/
93 static int Create( vlc_object_t *p_this )
94 {
95     vout_thread_t *p_vout = (vout_thread_t *)p_this;
96     char *psz_method, *psz_method_tmp;
97
98     /* Allocate structure */
99     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
100     if( p_vout->p_sys == NULL )
101     {
102         msg_Err( p_vout, "out of memory" );
103         return( 1 );
104     }
105
106     p_vout->pf_init = Init;
107     p_vout->pf_end = End;
108     p_vout->pf_manage = NULL;
109     p_vout->pf_render = Render;
110     p_vout->pf_display = NULL;
111
112     p_vout->p_sys->i_mode = 0;
113     /* Look what method was requested from command line*/
114     if( !(psz_method = psz_method_tmp = config_GetPsz( p_vout, "filter" )) )
115     {
116         msg_Err( p_vout, "configuration variable %s empty", "filter" );
117         return( 1 );
118     }
119     while( *psz_method && *psz_method != ':' )
120     {
121         psz_method++;
122     }
123
124     if( !strcmp( psz_method, ":wave" ) )
125     {
126         p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
127     }
128     else if( !strcmp( psz_method, ":ripple" ) )
129     {
130         p_vout->p_sys->i_mode = DISTORT_MODE_RIPPLE;
131     }
132     free( psz_method_tmp );
133     if( !p_vout->p_sys->i_mode )
134     {
135         /* No method given in commandline. Look what method was
136          requested in configuration system */
137         if( !(psz_method = psz_method_tmp
138               = config_GetPsz( p_vout, "distort-mode" )) )
139         {
140             msg_Err( p_vout, "configuration variable %s empty, using 'wave'",
141                              "distort-mode" );
142             p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
143         }
144         else {
145         
146             if( !strcmp( psz_method, "wave" ) )
147             {
148                 p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
149             }
150             else if( !strcmp( psz_method, "ripple" ) )
151             {
152                 p_vout->p_sys->i_mode = DISTORT_MODE_RIPPLE;
153             }
154             
155             else
156             {
157                 msg_Err( p_vout, "no valid distort mode provided, "
158                                  "using wave" );
159                 p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
160             }
161         }
162     }
163     free( psz_method_tmp );
164     
165     return( 0 );
166 }
167     
168 /*****************************************************************************
169  * Init: initialize Distort video thread output method
170  *****************************************************************************/
171 static int Init( vout_thread_t *p_vout )
172 {
173     int i_index;
174     picture_t *p_pic;
175
176     I_OUTPUTPICTURES = 0;
177
178     /* Initialize the output structure */
179     p_vout->output.i_chroma = p_vout->render.i_chroma;
180     p_vout->output.i_width  = p_vout->render.i_width;
181     p_vout->output.i_height = p_vout->render.i_height;
182     p_vout->output.i_aspect = p_vout->render.i_aspect;
183
184     /* Try to open the real video output */
185     msg_Dbg( p_vout, "spawning the real video output" );
186
187     p_vout->p_sys->p_vout =
188         vout_CreateThread( 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( 0 );
198     }
199  
200     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
201
202     p_vout->p_sys->f_angle = 0.0;
203     p_vout->p_sys->last_date = 0;
204
205     return( 0 );
206 }
207
208 /*****************************************************************************
209  * End: terminate Distort video thread output method
210  *****************************************************************************/
211 static void End( vout_thread_t *p_vout )
212 {
213     int i_index;
214
215     /* Free the fake output buffers we allocated */
216     for( i_index = I_OUTPUTPICTURES ; i_index ; )
217     {
218         i_index--;
219         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
220     }
221 }
222
223 /*****************************************************************************
224  * Destroy: destroy Distort video thread output method
225  *****************************************************************************
226  * Terminate an output method created by DistortCreateOutputMethod
227  *****************************************************************************/
228 static void Destroy( vlc_object_t *p_this )
229 {
230     vout_thread_t *p_vout = (vout_thread_t *)p_this;
231
232     vout_DestroyThread( p_vout->p_sys->p_vout );
233
234     free( p_vout->p_sys );
235 }
236
237 /*****************************************************************************
238  * Render: displays previously rendered output
239  *****************************************************************************
240  * This function send the currently rendered image to Distort image, waits
241  * until it is displayed and switch the two rendering buffers, preparing next
242  * frame.
243  *****************************************************************************/
244 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
245 {
246     picture_t *p_outpic;
247
248     /* This is a new frame. Get a structure from the video_output. */
249     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
250               == NULL )
251     {
252         if( p_vout->b_die || p_vout->b_error )
253         {
254             return;
255         }
256         msleep( VOUT_OUTMEM_SLEEP );
257     }
258
259     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
260
261     switch( p_vout->p_sys->i_mode )
262     {
263         case DISTORT_MODE_WAVE:
264             DistortWave( p_vout, p_pic, p_outpic );
265             break;
266
267         case DISTORT_MODE_RIPPLE:
268             DistortRipple( p_vout, p_pic, p_outpic );
269             break;
270
271         default:
272             break;
273     }
274
275     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
276 }
277
278 /*****************************************************************************
279  * DistortWave: draw a wave effect on the picture
280  *****************************************************************************/
281 static void DistortWave( vout_thread_t *p_vout, picture_t *p_inpic,
282                                                 picture_t *p_outpic )
283 {
284     int i_index;
285     double f_angle;
286     mtime_t new_date = mdate();
287
288     p_vout->p_sys->f_angle += (new_date - p_vout->p_sys->last_date) / 200000.0;
289     p_vout->p_sys->last_date = new_date;
290     f_angle = p_vout->p_sys->f_angle;
291
292     for( i_index = 0 ; i_index < p_inpic->i_planes ; i_index++ )
293     {
294         int i_line, i_num_lines, i_offset;
295         u8 black_pixel;
296         u8 *p_in, *p_out;
297
298         p_in = p_inpic->p[i_index].p_pixels;
299         p_out = p_outpic->p[i_index].p_pixels;
300
301         i_num_lines = p_inpic->p[i_index].i_lines;
302
303         black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
304
305         /* Ok, we do 3 times the sin() calculation for each line. So what ? */
306         for( i_line = 0 ; i_line < i_num_lines ; i_line++ )
307         {
308             /* Calculate today's offset, don't go above 1/20th of the screen */
309             i_offset = (double)(p_inpic->p[i_index].i_pitch)
310                          * sin( f_angle + 10.0 * (double)i_line
311                                                / (double)i_num_lines )
312                          / 20.0;
313
314             if( i_offset )
315             {
316                 if( i_offset < 0 )
317                 {
318                     p_vout->p_vlc->pf_memcpy( p_out, p_in - i_offset,
319                                  p_inpic->p[i_index].i_pitch + i_offset );
320                     p_in += p_inpic->p[i_index].i_pitch;
321                     p_out += p_outpic->p[i_index].i_pitch;
322                     memset( p_out + i_offset, black_pixel, -i_offset );
323                 }
324                 else
325                 {
326                     p_vout->p_vlc->pf_memcpy( p_out + i_offset, p_in,
327                                  p_inpic->p[i_index].i_pitch - i_offset );
328                     memset( p_out, black_pixel, i_offset );
329                     p_in += p_inpic->p[i_index].i_pitch;
330                     p_out += p_outpic->p[i_index].i_pitch;
331                 }
332             }
333             else
334             {
335                 p_vout->p_vlc->pf_memcpy( p_out, p_in,
336                                           p_inpic->p[i_index].i_pitch );
337                 p_in += p_inpic->p[i_index].i_pitch;
338                 p_out += p_outpic->p[i_index].i_pitch;
339             }
340
341         }
342     }
343 }
344
345 /*****************************************************************************
346  * DistortRipple: draw a ripple effect at the bottom of the picture
347  *****************************************************************************/
348 static void DistortRipple( vout_thread_t *p_vout, picture_t *p_inpic,
349                                                   picture_t *p_outpic )
350 {
351     int i_index;
352     double f_angle;
353     mtime_t new_date = mdate();
354
355     p_vout->p_sys->f_angle -= (p_vout->p_sys->last_date - new_date) / 100000.0;
356     p_vout->p_sys->last_date = new_date;
357     f_angle = p_vout->p_sys->f_angle;
358
359     for( i_index = 0 ; i_index < p_inpic->i_planes ; i_index++ )
360     {
361         int i_line, i_first_line, i_num_lines, i_offset;
362         u8 black_pixel;
363         u8 *p_in, *p_out;
364
365         black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
366
367         i_num_lines = p_inpic->p[i_index].i_lines;
368
369         i_first_line = i_num_lines * 4 / 5;
370
371         p_in = p_inpic->p[i_index].p_pixels;
372         p_out = p_outpic->p[i_index].p_pixels;
373
374         p_vout->p_vlc->pf_memcpy( p_out, p_in,
375                                   i_first_line * p_inpic->p[i_index].i_pitch );
376
377         p_in += i_first_line * p_inpic->p[i_index].i_pitch;
378         p_out += i_first_line * p_outpic->p[i_index].i_pitch;
379
380         /* Ok, we do 3 times the sin() calculation for each line. So what ? */
381         for( i_line = i_first_line ; i_line < i_num_lines ; i_line++ )
382         {
383             /* Calculate today's offset, don't go above 1/20th of the screen */
384             i_offset = (double)(p_inpic->p[i_index].i_pitch)
385                          * sin( f_angle + 2.0 * (double)i_line
386                                               / (double)( 1 + i_line
387                                                             - i_first_line) )
388                          * (double)(i_line - i_first_line)
389                          / (double)i_num_lines
390                          / 8.0;
391
392             if( i_offset )
393             {
394                 if( i_offset < 0 )
395                 {
396                     p_vout->p_vlc->pf_memcpy( p_out, p_in - i_offset,
397                                  p_inpic->p[i_index].i_pitch + i_offset );
398                     p_in -= p_inpic->p[i_index].i_pitch;
399                     p_out += p_outpic->p[i_index].i_pitch;
400                     memset( p_out + i_offset, black_pixel, -i_offset );
401                 }
402                 else
403                 {
404                     p_vout->p_vlc->pf_memcpy( p_out + i_offset, p_in,
405                                  p_inpic->p[i_index].i_pitch - i_offset );
406                     memset( p_out, black_pixel, i_offset );
407                     p_in -= p_inpic->p[i_index].i_pitch;
408                     p_out += p_outpic->p[i_index].i_pitch;
409                 }
410             }
411             else
412             {
413                 p_vout->p_vlc->pf_memcpy( p_out, p_in,
414                                           p_inpic->p[i_index].i_pitch );
415                 p_in -= p_inpic->p[i_index].i_pitch;
416                 p_out += p_outpic->p[i_index].i_pitch;
417             }
418
419         }
420     }
421 }