1 /*****************************************************************************
2 * distort.c : Misc video effects plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001, 2002, 2003 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <math.h> /* sin(), cos() */
35 #include "filter_common.h"
37 #define DISTORT_MODE_WAVE 1
38 #define DISTORT_MODE_RIPPLE 2
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Create ( vlc_object_t * );
44 static void Destroy ( vlc_object_t * );
46 static int Init ( vout_thread_t * );
47 static void End ( vout_thread_t * );
48 static void Render ( vout_thread_t *, picture_t * );
50 static void DistortWave ( vout_thread_t *, picture_t *, picture_t * );
51 static void DistortRipple ( vout_thread_t *, picture_t *, picture_t * );
53 static int SendEvents ( vlc_object_t *, char const *,
54 vlc_value_t, vlc_value_t, void * );
56 /*****************************************************************************
58 *****************************************************************************/
59 #define MODE_TEXT N_("Distort mode")
60 #define MODE_LONGTEXT N_("Distort mode, one of \"wave\" and \"ripple\"")
62 static char *mode_list[] = { "wave", "ripple" };
63 static char *mode_list_text[] = { N_("Wave"), N_("Ripple") };
66 set_description( _("Distort video filter") );
67 set_shortname( N_( "Distortion" ));
68 set_capability( "video filter", 0 );
69 set_category( CAT_VIDEO );
70 set_subcategory( SUBCAT_VIDEO_VFILTER );
72 add_string( "distort-mode", "wave", NULL, MODE_TEXT, MODE_LONGTEXT,
74 change_string_list( mode_list, mode_list_text, 0 );
76 add_shortcut( "distort" );
77 set_callbacks( Create, Destroy );
80 /*****************************************************************************
81 * vout_sys_t: Distort video output method descriptor
82 *****************************************************************************
83 * This structure is part of the video output thread descriptor.
84 * It describes the Distort specific properties of an output thread.
85 *****************************************************************************/
89 vout_thread_t *p_vout;
91 /* For the wave mode */
96 /*****************************************************************************
97 * Control: control facility for the vout (forwards to child vout)
98 *****************************************************************************/
99 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
101 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
104 /*****************************************************************************
105 * Create: allocates Distort video thread output method
106 *****************************************************************************
107 * This function allocates and initializes a Distort vout method.
108 *****************************************************************************/
109 static int Create( vlc_object_t *p_this )
111 vout_thread_t *p_vout = (vout_thread_t *)p_this;
112 char *psz_method, *psz_method_tmp;
114 /* Allocate structure */
115 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
116 if( p_vout->p_sys == NULL )
118 msg_Err( p_vout, "out of memory" );
122 p_vout->pf_init = Init;
123 p_vout->pf_end = End;
124 p_vout->pf_manage = NULL;
125 p_vout->pf_render = Render;
126 p_vout->pf_display = NULL;
127 p_vout->pf_control = Control;
129 p_vout->p_sys->i_mode = 0;
131 if( !(psz_method = psz_method_tmp
132 = config_GetPsz( p_vout, "distort-mode" )) )
134 msg_Err( p_vout, "configuration variable %s empty, using 'wave'",
136 p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
141 if( !strcmp( psz_method, "wave" ) )
143 p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
145 else if( !strcmp( psz_method, "ripple" ) )
147 p_vout->p_sys->i_mode = DISTORT_MODE_RIPPLE;
151 msg_Err( p_vout, "no valid distort mode provided, "
153 p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
156 free( psz_method_tmp );
161 /*****************************************************************************
162 * Init: initialize Distort video thread output method
163 *****************************************************************************/
164 static int Init( vout_thread_t *p_vout )
168 video_format_t fmt = {0};
170 I_OUTPUTPICTURES = 0;
172 /* Initialize the output structure */
173 p_vout->output.i_chroma = p_vout->render.i_chroma;
174 p_vout->output.i_width = p_vout->render.i_width;
175 p_vout->output.i_height = p_vout->render.i_height;
176 p_vout->output.i_aspect = p_vout->render.i_aspect;
178 fmt.i_width = fmt.i_visible_width = p_vout->render.i_width;
179 fmt.i_height = fmt.i_visible_height = p_vout->render.i_height;
180 fmt.i_x_offset = fmt.i_y_offset = 0;
181 fmt.i_chroma = p_vout->render.i_chroma;
182 fmt.i_aspect = p_vout->render.i_aspect;
183 fmt.i_sar_num = p_vout->render.i_aspect * fmt.i_height / fmt.i_width;
184 fmt.i_sar_den = VOUT_ASPECT_FACTOR;
186 /* Try to open the real video output */
187 msg_Dbg( p_vout, "spawning the real video output" );
189 p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
191 /* Everything failed */
192 if( p_vout->p_sys->p_vout == NULL )
194 msg_Err( p_vout, "cannot open vout, aborting" );
198 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
200 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
202 ADD_PARENT_CALLBACKS( SendEventsToChild );
204 p_vout->p_sys->f_angle = 0.0;
205 p_vout->p_sys->last_date = 0;
210 /*****************************************************************************
211 * End: terminate Distort video thread output method
212 *****************************************************************************/
213 static void End( vout_thread_t *p_vout )
217 /* Free the fake output buffers we allocated */
218 for( i_index = I_OUTPUTPICTURES ; i_index ; )
221 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
225 /*****************************************************************************
226 * Destroy: destroy Distort video thread output method
227 *****************************************************************************
228 * Terminate an output method created by DistortCreateOutputMethod
229 *****************************************************************************/
230 static void Destroy( vlc_object_t *p_this )
232 vout_thread_t *p_vout = (vout_thread_t *)p_this;
234 if( p_vout->p_sys->p_vout )
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 );
241 DEL_PARENT_CALLBACKS( SendEventsToChild );
243 free( p_vout->p_sys );
246 /*****************************************************************************
247 * Render: displays previously rendered output
248 *****************************************************************************
249 * This function send the currently rendered image to Distort image, waits
250 * until it is displayed and switch the two rendering buffers, preparing next
252 *****************************************************************************/
253 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
257 /* This is a new frame. Get a structure from the video_output. */
258 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
261 if( p_vout->b_die || p_vout->b_error )
265 msleep( VOUT_OUTMEM_SLEEP );
268 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
270 switch( p_vout->p_sys->i_mode )
272 case DISTORT_MODE_WAVE:
273 DistortWave( p_vout, p_pic, p_outpic );
276 case DISTORT_MODE_RIPPLE:
277 DistortRipple( p_vout, p_pic, p_outpic );
284 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
287 /*****************************************************************************
288 * DistortWave: draw a wave effect on the picture
289 *****************************************************************************/
290 static void DistortWave( vout_thread_t *p_vout, picture_t *p_inpic,
291 picture_t *p_outpic )
295 mtime_t new_date = mdate();
297 p_vout->p_sys->f_angle += (new_date - p_vout->p_sys->last_date) / 200000.0;
298 p_vout->p_sys->last_date = new_date;
299 f_angle = p_vout->p_sys->f_angle;
301 for( i_index = 0 ; i_index < p_inpic->i_planes ; i_index++ )
303 int i_line, i_num_lines, i_offset;
305 uint8_t *p_in, *p_out;
307 p_in = p_inpic->p[i_index].p_pixels;
308 p_out = p_outpic->p[i_index].p_pixels;
310 i_num_lines = p_inpic->p[i_index].i_visible_lines;
312 black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
314 /* Ok, we do 3 times the sin() calculation for each line. So what ? */
315 for( i_line = 0 ; i_line < i_num_lines ; i_line++ )
317 /* Calculate today's offset, don't go above 1/20th of the screen */
318 i_offset = (int)( (double)(p_inpic->p[i_index].i_visible_pitch)
319 * sin( f_angle + 10.0 * (double)i_line
320 / (double)i_num_lines )
327 p_vout->p_vlc->pf_memcpy( p_out, p_in - i_offset,
328 p_inpic->p[i_index].i_visible_pitch + i_offset );
329 p_in += p_inpic->p[i_index].i_pitch;
330 p_out += p_outpic->p[i_index].i_pitch;
331 memset( p_out + i_offset, black_pixel, -i_offset );
335 p_vout->p_vlc->pf_memcpy( p_out + i_offset, p_in,
336 p_inpic->p[i_index].i_visible_pitch - i_offset );
337 memset( p_out, black_pixel, i_offset );
338 p_in += p_inpic->p[i_index].i_pitch;
339 p_out += p_outpic->p[i_index].i_pitch;
344 p_vout->p_vlc->pf_memcpy( p_out, p_in,
345 p_inpic->p[i_index].i_visible_pitch );
346 p_in += p_inpic->p[i_index].i_pitch;
347 p_out += p_outpic->p[i_index].i_pitch;
354 /*****************************************************************************
355 * DistortRipple: draw a ripple effect at the bottom of the picture
356 *****************************************************************************/
357 static void DistortRipple( vout_thread_t *p_vout, picture_t *p_inpic,
358 picture_t *p_outpic )
362 mtime_t new_date = mdate();
364 p_vout->p_sys->f_angle -= (p_vout->p_sys->last_date - new_date) / 100000.0;
365 p_vout->p_sys->last_date = new_date;
366 f_angle = p_vout->p_sys->f_angle;
368 for( i_index = 0 ; i_index < p_inpic->i_planes ; i_index++ )
370 int i_line, i_first_line, i_num_lines, i_offset;
372 uint8_t *p_in, *p_out;
374 black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
376 i_num_lines = p_inpic->p[i_index].i_visible_lines;
378 i_first_line = i_num_lines * 4 / 5;
380 p_in = p_inpic->p[i_index].p_pixels;
381 p_out = p_outpic->p[i_index].p_pixels;
383 for( i_line = 0 ; i_line < i_first_line ; i_line++ )
385 p_vout->p_vlc->pf_memcpy( p_out, p_in,
386 p_inpic->p[i_index].i_visible_pitch );
387 p_in += p_inpic->p[i_index].i_pitch;
388 p_out += p_outpic->p[i_index].i_pitch;
391 /* Ok, we do 3 times the sin() calculation for each line. So what ? */
392 for( i_line = i_first_line ; i_line < i_num_lines ; i_line++ )
394 /* Calculate today's offset, don't go above 1/20th of the screen */
395 i_offset = (int)( (double)(p_inpic->p[i_index].i_pitch)
396 * sin( f_angle + 2.0 * (double)i_line
397 / (double)( 1 + i_line
399 * (double)(i_line - i_first_line)
400 / (double)i_num_lines
407 p_vout->p_vlc->pf_memcpy( p_out, p_in - i_offset,
408 p_inpic->p[i_index].i_visible_pitch + i_offset );
409 p_in -= p_inpic->p[i_index].i_pitch;
410 p_out += p_outpic->p[i_index].i_pitch;
411 memset( p_out + i_offset, black_pixel, -i_offset );
415 p_vout->p_vlc->pf_memcpy( p_out + i_offset, p_in,
416 p_inpic->p[i_index].i_visible_pitch - i_offset );
417 memset( p_out, black_pixel, i_offset );
418 p_in -= p_inpic->p[i_index].i_pitch;
419 p_out += p_outpic->p[i_index].i_pitch;
424 p_vout->p_vlc->pf_memcpy( p_out, p_in,
425 p_inpic->p[i_index].i_visible_pitch );
426 p_in -= p_inpic->p[i_index].i_pitch;
427 p_out += p_outpic->p[i_index].i_pitch;
434 /*****************************************************************************
435 * SendEvents: forward mouse and keyboard events to the parent p_vout
436 *****************************************************************************/
437 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
438 vlc_value_t oldval, vlc_value_t newval, void *p_data )
440 var_Set( (vlc_object_t *)p_data, psz_var, newval );
445 /*****************************************************************************
446 * SendEventsToChild: forward events to the child/children vout
447 *****************************************************************************/
448 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
449 vlc_value_t oldval, vlc_value_t newval, void *p_data )
451 vout_thread_t *p_vout = (vout_thread_t *)p_this;
452 var_Set( p_vout->p_sys->p_vout, psz_var, newval );