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