]> git.sesse.net Git - vlc/blob - plugins/filter/distort.c
* ALL: the first libvlc commit.
[vlc] / plugins / 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.14 2002/06/01 12:31:59 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  * Capabilities defined in the other files.
43  *****************************************************************************/
44 static void vout_getfunctions( function_list_t * p_function_list );
45
46 /*****************************************************************************
47  * Build configuration tree.
48  *****************************************************************************/
49 MODULE_CONFIG_START
50 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
51 ADD_STRING  ( "distort-mode", "wave", NULL, N_("distort mode"),
52               N_("one of \"wave\" and \"ripple\"") )
53 MODULE_CONFIG_STOP
54
55 MODULE_INIT_START
56     SET_DESCRIPTION( _("miscellaneous video effects module") )
57     /* Capability score set to 0 because we don't want to be spawned
58      * as a video output unless explicitly requested to */
59     ADD_CAPABILITY( VOUT, 0 )
60     ADD_SHORTCUT( "distort" )
61 MODULE_INIT_STOP
62
63 MODULE_ACTIVATE_START
64     vout_getfunctions( &p_module->p_functions->vout );
65 MODULE_ACTIVATE_STOP
66
67 MODULE_DEACTIVATE_START
68 MODULE_DEACTIVATE_STOP
69
70 /*****************************************************************************
71  * vout_sys_t: Distort video output method descriptor
72  *****************************************************************************
73  * This structure is part of the video output thread descriptor.
74  * It describes the Distort specific properties of an output thread.
75  *****************************************************************************/
76 struct vout_sys_s
77 {
78     int i_mode;
79     vout_thread_t *p_vout;
80
81     /* For the wave mode */
82     double  f_angle;
83     mtime_t last_date;
84 };
85
86 /*****************************************************************************
87  * Local prototypes
88  *****************************************************************************/
89 static int  vout_Create    ( vout_thread_t * );
90 static int  vout_Init      ( vout_thread_t * );
91 static void vout_End       ( vout_thread_t * );
92 static void vout_Destroy   ( vout_thread_t * );
93 static int  vout_Manage    ( vout_thread_t * );
94 static void vout_Render    ( vout_thread_t *, picture_t * );
95 static void vout_Display   ( vout_thread_t *, picture_t * );
96
97 static void DistortWave    ( vout_thread_t *, picture_t *, picture_t * );
98 static void DistortRipple  ( vout_thread_t *, picture_t *, picture_t * );
99
100 /*****************************************************************************
101  * Functions exported as capabilities. They are declared as static so that
102  * we don't pollute the namespace too much.
103  *****************************************************************************/
104 static void vout_getfunctions( function_list_t * p_function_list )
105 {
106     p_function_list->functions.vout.pf_create     = vout_Create;
107     p_function_list->functions.vout.pf_init       = vout_Init;
108     p_function_list->functions.vout.pf_end        = vout_End;
109     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
110     p_function_list->functions.vout.pf_manage     = vout_Manage;
111     p_function_list->functions.vout.pf_render     = vout_Render;
112     p_function_list->functions.vout.pf_display    = vout_Display;
113 }
114
115 /*****************************************************************************
116  * vout_Create: allocates Distort video thread output method
117  *****************************************************************************
118  * This function allocates and initializes a Distort vout method.
119  *****************************************************************************/
120 static int vout_Create( vout_thread_t *p_vout )
121 {
122     char *psz_method, *psz_method_tmp;
123
124     /* Allocate structure */
125     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
126     if( p_vout->p_sys == NULL )
127     {
128         msg_Err( p_vout, "out of memory" );
129         return( 1 );
130     }
131     p_vout->p_sys->i_mode = 0;
132     /* Look what method was requested from command line*/
133     if( !(psz_method = psz_method_tmp = config_GetPsz( p_vout, "filter" )) )
134     {
135         msg_Err( p_vout, "configuration variable %s empty", "filter" );
136         return( 1 );
137     }
138     while( *psz_method && *psz_method != ':' )
139     {
140         psz_method++;
141     }
142
143     if( !strcmp( psz_method, ":wave" ) )
144     {
145         p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
146     }
147     else if( !strcmp( psz_method, ":ripple" ) )
148     {
149         p_vout->p_sys->i_mode = DISTORT_MODE_RIPPLE;
150     }
151     free( psz_method_tmp );
152     if( !p_vout->p_sys->i_mode )
153     {
154         /* No method given in commandline. Look what method was
155          requested in configuration system */
156         if( !(psz_method = psz_method_tmp
157               = config_GetPsz( p_vout, "distort_mode" )) )
158         {
159             msg_Err( p_vout, "configuration variable %s empty, using 'wave'",
160                              "distort_mode" );
161             p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
162         }
163         else {
164         
165             if( !strcmp( psz_method, "wave" ) )
166             {
167                 p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
168             }
169             else if( !strcmp( psz_method, "ripple" ) )
170             {
171                 p_vout->p_sys->i_mode = DISTORT_MODE_RIPPLE;
172             }
173             
174             else
175             {
176                 msg_Err( p_vout, "no valid distort mode provided, "
177                                  "using wave" );
178                 p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
179             }
180         }
181     }
182     free( psz_method_tmp );
183     
184     return( 0 );
185 }
186     
187 /*****************************************************************************
188  * vout_Init: initialize Distort video thread output method
189  *****************************************************************************/
190 static int vout_Init( vout_thread_t *p_vout )
191 {
192     int i_index;
193     char *psz_filter;
194     picture_t *p_pic;
195
196     I_OUTPUTPICTURES = 0;
197
198     /* Initialize the output structure */
199     p_vout->output.i_chroma = p_vout->render.i_chroma;
200     p_vout->output.i_width  = p_vout->render.i_width;
201     p_vout->output.i_height = p_vout->render.i_height;
202     p_vout->output.i_aspect = p_vout->render.i_aspect;
203
204     /* Try to open the real video output */
205     psz_filter = config_GetPsz( p_vout, "filter" );
206     config_PutPsz( p_vout, "filter", NULL );
207
208     msg_Dbg( p_vout, "spawning the real video output" );
209
210     p_vout->p_sys->p_vout =
211         vout_CreateThread( p_vout->p_this,
212                            p_vout->render.i_width, p_vout->render.i_height,
213                            p_vout->render.i_chroma, p_vout->render.i_aspect );
214
215     config_PutPsz( p_vout, "filter", psz_filter );
216     if( psz_filter ) free( psz_filter );
217
218     /* Everything failed */
219     if( p_vout->p_sys->p_vout == NULL )
220     {
221         msg_Err( p_vout, "cannot open vout, aborting" );
222
223         return( 0 );
224     }
225  
226     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
227
228     p_vout->p_sys->f_angle = 0.0;
229     p_vout->p_sys->last_date = 0;
230
231     return( 0 );
232 }
233
234 /*****************************************************************************
235  * vout_End: terminate Distort video thread output method
236  *****************************************************************************/
237 static void vout_End( vout_thread_t *p_vout )
238 {
239     int i_index;
240
241     /* Free the fake output buffers we allocated */
242     for( i_index = I_OUTPUTPICTURES ; i_index ; )
243     {
244         i_index--;
245         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
246     }
247 }
248
249 /*****************************************************************************
250  * vout_Destroy: destroy Distort video thread output method
251  *****************************************************************************
252  * Terminate an output method created by DistortCreateOutputMethod
253  *****************************************************************************/
254 static void vout_Destroy( vout_thread_t *p_vout )
255 {
256     vout_DestroyThread( p_vout->p_sys->p_vout );
257
258     free( p_vout->p_sys );
259 }
260
261 /*****************************************************************************
262  * vout_Manage: handle Distort events
263  *****************************************************************************
264  * This function should be called regularly by video output thread. It manages
265  * console events. It returns a non null value on error.
266  *****************************************************************************/
267 static int vout_Manage( vout_thread_t *p_vout )
268 {
269     return( 0 );
270 }
271
272 /*****************************************************************************
273  * vout_Render: displays previously rendered output
274  *****************************************************************************
275  * This function send the currently rendered image to Distort image, waits
276  * until it is displayed and switch the two rendering buffers, preparing next
277  * frame.
278  *****************************************************************************/
279 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
280 {
281     picture_t *p_outpic;
282
283     /* This is a new frame. Get a structure from the video_output. */
284     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
285               == NULL )
286     {
287         if( p_vout->b_die || p_vout->b_error )
288         {
289             return;
290         }
291         msleep( VOUT_OUTMEM_SLEEP );
292     }
293
294     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
295
296     switch( p_vout->p_sys->i_mode )
297     {
298         case DISTORT_MODE_WAVE:
299             DistortWave( p_vout, p_pic, p_outpic );
300             break;
301
302         case DISTORT_MODE_RIPPLE:
303             DistortRipple( p_vout, p_pic, p_outpic );
304             break;
305
306         default:
307             break;
308     }
309
310     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
311 }
312
313 /*****************************************************************************
314  * vout_Display: displays previously rendered output
315  *****************************************************************************
316  * This function send the currently rendered image to Invert image, waits
317  * until it is displayed and switch the two rendering buffers, preparing next
318  * frame.
319  *****************************************************************************/
320 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
321 {
322     ;
323 }
324
325 /*****************************************************************************
326  * DistortWave: draw a wave effect on the picture
327  *****************************************************************************/
328 static void DistortWave( vout_thread_t *p_vout, picture_t *p_inpic,
329                                                 picture_t *p_outpic )
330 {
331     int i_index;
332     double f_angle;
333     mtime_t new_date = mdate();
334
335     p_vout->p_sys->f_angle += (new_date - p_vout->p_sys->last_date) / 200000.0;
336     p_vout->p_sys->last_date = new_date;
337     f_angle = p_vout->p_sys->f_angle;
338
339     for( i_index = 0 ; i_index < p_inpic->i_planes ; i_index++ )
340     {
341         int i_line, i_num_lines, i_offset;
342         u8 black_pixel;
343         u8 *p_in, *p_out;
344
345         p_in = p_inpic->p[i_index].p_pixels;
346         p_out = p_outpic->p[i_index].p_pixels;
347
348         i_num_lines = p_inpic->p[i_index].i_lines;
349
350         black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
351
352         /* Ok, we do 3 times the sin() calculation for each line. So what ? */
353         for( i_line = 0 ; i_line < i_num_lines ; i_line++ )
354         {
355             /* Calculate today's offset, don't go above 1/20th of the screen */
356             i_offset = (double)(p_inpic->p[i_index].i_pitch)
357                          * sin( f_angle + 10.0 * (double)i_line
358                                                / (double)i_num_lines )
359                          / 20.0;
360
361             if( i_offset )
362             {
363                 if( i_offset < 0 )
364                 {
365                     p_vout->p_vlc->pf_memcpy( p_out, p_in - i_offset,
366                                  p_inpic->p[i_index].i_pitch + i_offset );
367                     p_in += p_inpic->p[i_index].i_pitch;
368                     p_out += p_outpic->p[i_index].i_pitch;
369                     memset( p_out + i_offset, black_pixel, -i_offset );
370                 }
371                 else
372                 {
373                     p_vout->p_vlc->pf_memcpy( p_out + i_offset, p_in,
374                                  p_inpic->p[i_index].i_pitch - i_offset );
375                     memset( p_out, black_pixel, i_offset );
376                     p_in += p_inpic->p[i_index].i_pitch;
377                     p_out += p_outpic->p[i_index].i_pitch;
378                 }
379             }
380             else
381             {
382                 p_vout->p_vlc->pf_memcpy( p_out, p_in,
383                                           p_inpic->p[i_index].i_pitch );
384                 p_in += p_inpic->p[i_index].i_pitch;
385                 p_out += p_outpic->p[i_index].i_pitch;
386             }
387
388         }
389     }
390 }
391
392 /*****************************************************************************
393  * DistortRipple: draw a ripple effect at the bottom of the picture
394  *****************************************************************************/
395 static void DistortRipple( vout_thread_t *p_vout, picture_t *p_inpic,
396                                                   picture_t *p_outpic )
397 {
398     int i_index;
399     double f_angle;
400     mtime_t new_date = mdate();
401
402     p_vout->p_sys->f_angle -= (p_vout->p_sys->last_date - new_date) / 100000.0;
403     p_vout->p_sys->last_date = new_date;
404     f_angle = p_vout->p_sys->f_angle;
405
406     for( i_index = 0 ; i_index < p_inpic->i_planes ; i_index++ )
407     {
408         int i_line, i_first_line, i_num_lines, i_offset;
409         u8 black_pixel;
410         u8 *p_in, *p_out;
411
412         black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
413
414         i_num_lines = p_inpic->p[i_index].i_lines;
415
416         i_first_line = i_num_lines * 4 / 5;
417
418         p_in = p_inpic->p[i_index].p_pixels;
419         p_out = p_outpic->p[i_index].p_pixels;
420
421         p_vout->p_vlc->pf_memcpy( p_out, p_in,
422                                   i_first_line * p_inpic->p[i_index].i_pitch );
423
424         p_in += i_first_line * p_inpic->p[i_index].i_pitch;
425         p_out += i_first_line * p_outpic->p[i_index].i_pitch;
426
427         /* Ok, we do 3 times the sin() calculation for each line. So what ? */
428         for( i_line = i_first_line ; i_line < i_num_lines ; i_line++ )
429         {
430             /* Calculate today's offset, don't go above 1/20th of the screen */
431             i_offset = (double)(p_inpic->p[i_index].i_pitch)
432                          * sin( f_angle + 2.0 * (double)i_line
433                                               / (double)( 1 + i_line
434                                                             - i_first_line) )
435                          * (double)(i_line - i_first_line)
436                          / (double)i_num_lines
437                          / 8.0;
438
439             if( i_offset )
440             {
441                 if( i_offset < 0 )
442                 {
443                     p_vout->p_vlc->pf_memcpy( p_out, p_in - i_offset,
444                                  p_inpic->p[i_index].i_pitch + i_offset );
445                     p_in -= p_inpic->p[i_index].i_pitch;
446                     p_out += p_outpic->p[i_index].i_pitch;
447                     memset( p_out + i_offset, black_pixel, -i_offset );
448                 }
449                 else
450                 {
451                     p_vout->p_vlc->pf_memcpy( p_out + i_offset, p_in,
452                                  p_inpic->p[i_index].i_pitch - i_offset );
453                     memset( p_out, black_pixel, i_offset );
454                     p_in -= p_inpic->p[i_index].i_pitch;
455                     p_out += p_outpic->p[i_index].i_pitch;
456                 }
457             }
458             else
459             {
460                 p_vout->p_vlc->pf_memcpy( p_out, p_in,
461                                           p_inpic->p[i_index].i_pitch );
462                 p_in -= p_inpic->p[i_index].i_pitch;
463                 p_out += p_outpic->p[i_index].i_pitch;
464             }
465
466         }
467     }
468 }
469