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 $
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 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
31 #include <math.h> /* sin(), cos() */
36 #include "filter_common.h"
38 #define DISTORT_MODE_WAVE 1
39 #define DISTORT_MODE_RIPPLE 2
41 /*****************************************************************************
42 * Capabilities defined in the other files.
43 *****************************************************************************/
44 static void vout_getfunctions( function_list_t * p_function_list );
46 /*****************************************************************************
47 * Build configuration tree.
48 *****************************************************************************/
50 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
51 ADD_STRING ( "distort-mode", "wave", NULL, N_("distort mode"),
52 N_("one of \"wave\" and \"ripple\"") )
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" )
64 vout_getfunctions( &p_module->p_functions->vout );
67 MODULE_DEACTIVATE_START
68 MODULE_DEACTIVATE_STOP
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 *****************************************************************************/
79 vout_thread_t *p_vout;
81 /* For the wave mode */
86 /*****************************************************************************
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 * );
97 static void DistortWave ( vout_thread_t *, picture_t *, picture_t * );
98 static void DistortRipple ( vout_thread_t *, picture_t *, picture_t * );
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 )
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;
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 )
122 char *psz_method, *psz_method_tmp;
124 /* Allocate structure */
125 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
126 if( p_vout->p_sys == NULL )
128 msg_Err( p_vout, "out of memory" );
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" )) )
135 msg_Err( p_vout, "configuration variable %s empty", "filter" );
138 while( *psz_method && *psz_method != ':' )
143 if( !strcmp( psz_method, ":wave" ) )
145 p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
147 else if( !strcmp( psz_method, ":ripple" ) )
149 p_vout->p_sys->i_mode = DISTORT_MODE_RIPPLE;
151 free( psz_method_tmp );
152 if( !p_vout->p_sys->i_mode )
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" )) )
159 msg_Err( p_vout, "configuration variable %s empty, using 'wave'",
161 p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
165 if( !strcmp( psz_method, "wave" ) )
167 p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
169 else if( !strcmp( psz_method, "ripple" ) )
171 p_vout->p_sys->i_mode = DISTORT_MODE_RIPPLE;
176 msg_Err( p_vout, "no valid distort mode provided, "
178 p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
182 free( psz_method_tmp );
187 /*****************************************************************************
188 * vout_Init: initialize Distort video thread output method
189 *****************************************************************************/
190 static int vout_Init( vout_thread_t *p_vout )
196 I_OUTPUTPICTURES = 0;
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;
204 /* Try to open the real video output */
205 psz_filter = config_GetPsz( p_vout, "filter" );
206 config_PutPsz( p_vout, "filter", NULL );
208 msg_Dbg( p_vout, "spawning the real video output" );
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 );
215 config_PutPsz( p_vout, "filter", psz_filter );
216 if( psz_filter ) free( psz_filter );
218 /* Everything failed */
219 if( p_vout->p_sys->p_vout == NULL )
221 msg_Err( p_vout, "cannot open vout, aborting" );
226 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
228 p_vout->p_sys->f_angle = 0.0;
229 p_vout->p_sys->last_date = 0;
234 /*****************************************************************************
235 * vout_End: terminate Distort video thread output method
236 *****************************************************************************/
237 static void vout_End( vout_thread_t *p_vout )
241 /* Free the fake output buffers we allocated */
242 for( i_index = I_OUTPUTPICTURES ; i_index ; )
245 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
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 )
256 vout_DestroyThread( p_vout->p_sys->p_vout );
258 free( p_vout->p_sys );
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 )
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
278 *****************************************************************************/
279 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
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 ) )
287 if( p_vout->b_die || p_vout->b_error )
291 msleep( VOUT_OUTMEM_SLEEP );
294 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
296 switch( p_vout->p_sys->i_mode )
298 case DISTORT_MODE_WAVE:
299 DistortWave( p_vout, p_pic, p_outpic );
302 case DISTORT_MODE_RIPPLE:
303 DistortRipple( p_vout, p_pic, p_outpic );
310 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
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
319 *****************************************************************************/
320 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
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 )
333 mtime_t new_date = mdate();
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;
339 for( i_index = 0 ; i_index < p_inpic->i_planes ; i_index++ )
341 int i_line, i_num_lines, i_offset;
345 p_in = p_inpic->p[i_index].p_pixels;
346 p_out = p_outpic->p[i_index].p_pixels;
348 i_num_lines = p_inpic->p[i_index].i_lines;
350 black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
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++ )
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 )
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 );
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;
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;
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 )
400 mtime_t new_date = mdate();
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;
406 for( i_index = 0 ; i_index < p_inpic->i_planes ; i_index++ )
408 int i_line, i_first_line, i_num_lines, i_offset;
412 black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
414 i_num_lines = p_inpic->p[i_index].i_lines;
416 i_first_line = i_num_lines * 4 / 5;
418 p_in = p_inpic->p[i_index].p_pixels;
419 p_out = p_outpic->p[i_index].p_pixels;
421 p_vout->p_vlc->pf_memcpy( p_out, p_in,
422 i_first_line * p_inpic->p[i_index].i_pitch );
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;
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++ )
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
435 * (double)(i_line - i_first_line)
436 / (double)i_num_lines
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 );
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;
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;