1 /*****************************************************************************
2 * distort.c : Misc video effects plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: distort.c,v 1.3 2002/11/23 02:40:30 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 *****************************************************************************/
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 /*****************************************************************************
55 *****************************************************************************/
56 #define MODE_TEXT N_("distort mode")
57 #define MODE_LONGTEXT N_("Distort mode, one of \"wave\" and \"ripple\"")
59 static char *mode_list[] = { "wave", "ripple", NULL };
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 );
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 *****************************************************************************/
80 vout_thread_t *p_vout;
82 /* For the wave mode */
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 )
94 vout_thread_t *p_vout = (vout_thread_t *)p_this;
95 char *psz_method, *psz_method_tmp;
97 /* Allocate structure */
98 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
99 if( p_vout->p_sys == NULL )
101 msg_Err( p_vout, "out of memory" );
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;
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" )) )
115 msg_Err( p_vout, "configuration variable %s empty", "filter" );
118 while( *psz_method && *psz_method != ':' )
123 if( !strcmp( psz_method, ":wave" ) )
125 p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
127 else if( !strcmp( psz_method, ":ripple" ) )
129 p_vout->p_sys->i_mode = DISTORT_MODE_RIPPLE;
131 free( psz_method_tmp );
132 if( !p_vout->p_sys->i_mode )
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" )) )
139 msg_Err( p_vout, "configuration variable %s empty, using 'wave'",
141 p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
145 if( !strcmp( psz_method, "wave" ) )
147 p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
149 else if( !strcmp( psz_method, "ripple" ) )
151 p_vout->p_sys->i_mode = DISTORT_MODE_RIPPLE;
156 msg_Err( p_vout, "no valid distort mode provided, "
158 p_vout->p_sys->i_mode = DISTORT_MODE_WAVE;
162 free( psz_method_tmp );
167 /*****************************************************************************
168 * Init: initialize Distort video thread output method
169 *****************************************************************************/
170 static int Init( vout_thread_t *p_vout )
175 I_OUTPUTPICTURES = 0;
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;
183 /* Try to open the real video output */
184 msg_Dbg( p_vout, "spawning the real video output" );
186 p_vout->p_sys->p_vout =
187 vout_CreateThread( p_vout,
188 p_vout->render.i_width, p_vout->render.i_height,
189 p_vout->render.i_chroma, p_vout->render.i_aspect );
191 /* Everything failed */
192 if( p_vout->p_sys->p_vout == NULL )
194 msg_Err( p_vout, "cannot open vout, aborting" );
199 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
201 p_vout->p_sys->f_angle = 0.0;
202 p_vout->p_sys->last_date = 0;
207 /*****************************************************************************
208 * End: terminate Distort video thread output method
209 *****************************************************************************/
210 static void End( vout_thread_t *p_vout )
214 /* Free the fake output buffers we allocated */
215 for( i_index = I_OUTPUTPICTURES ; i_index ; )
218 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
222 /*****************************************************************************
223 * Destroy: destroy Distort video thread output method
224 *****************************************************************************
225 * Terminate an output method created by DistortCreateOutputMethod
226 *****************************************************************************/
227 static void Destroy( vlc_object_t *p_this )
229 vout_thread_t *p_vout = (vout_thread_t *)p_this;
231 vout_DestroyThread( p_vout->p_sys->p_vout );
233 free( p_vout->p_sys );
236 /*****************************************************************************
237 * Render: displays previously rendered output
238 *****************************************************************************
239 * This function send the currently rendered image to Distort image, waits
240 * until it is displayed and switch the two rendering buffers, preparing next
242 *****************************************************************************/
243 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
247 /* This is a new frame. Get a structure from the video_output. */
248 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
251 if( p_vout->b_die || p_vout->b_error )
255 msleep( VOUT_OUTMEM_SLEEP );
258 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
260 switch( p_vout->p_sys->i_mode )
262 case DISTORT_MODE_WAVE:
263 DistortWave( p_vout, p_pic, p_outpic );
266 case DISTORT_MODE_RIPPLE:
267 DistortRipple( p_vout, p_pic, p_outpic );
274 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
277 /*****************************************************************************
278 * DistortWave: draw a wave effect on the picture
279 *****************************************************************************/
280 static void DistortWave( vout_thread_t *p_vout, picture_t *p_inpic,
281 picture_t *p_outpic )
285 mtime_t new_date = mdate();
287 p_vout->p_sys->f_angle += (new_date - p_vout->p_sys->last_date) / 200000.0;
288 p_vout->p_sys->last_date = new_date;
289 f_angle = p_vout->p_sys->f_angle;
291 for( i_index = 0 ; i_index < p_inpic->i_planes ; i_index++ )
293 int i_line, i_num_lines, i_offset;
297 p_in = p_inpic->p[i_index].p_pixels;
298 p_out = p_outpic->p[i_index].p_pixels;
300 i_num_lines = p_inpic->p[i_index].i_lines;
302 black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
304 /* Ok, we do 3 times the sin() calculation for each line. So what ? */
305 for( i_line = 0 ; i_line < i_num_lines ; i_line++ )
307 /* Calculate today's offset, don't go above 1/20th of the screen */
308 i_offset = (int)( (double)(p_inpic->p[i_index].i_pitch)
309 * sin( f_angle + 10.0 * (double)i_line
310 / (double)i_num_lines )
317 p_vout->p_vlc->pf_memcpy( p_out, p_in - i_offset,
318 p_inpic->p[i_index].i_pitch + i_offset );
319 p_in += p_inpic->p[i_index].i_pitch;
320 p_out += p_outpic->p[i_index].i_pitch;
321 memset( p_out + i_offset, black_pixel, -i_offset );
325 p_vout->p_vlc->pf_memcpy( p_out + i_offset, p_in,
326 p_inpic->p[i_index].i_pitch - i_offset );
327 memset( p_out, black_pixel, i_offset );
328 p_in += p_inpic->p[i_index].i_pitch;
329 p_out += p_outpic->p[i_index].i_pitch;
334 p_vout->p_vlc->pf_memcpy( p_out, p_in,
335 p_inpic->p[i_index].i_pitch );
336 p_in += p_inpic->p[i_index].i_pitch;
337 p_out += p_outpic->p[i_index].i_pitch;
344 /*****************************************************************************
345 * DistortRipple: draw a ripple effect at the bottom of the picture
346 *****************************************************************************/
347 static void DistortRipple( vout_thread_t *p_vout, picture_t *p_inpic,
348 picture_t *p_outpic )
352 mtime_t new_date = mdate();
354 p_vout->p_sys->f_angle -= (p_vout->p_sys->last_date - new_date) / 100000.0;
355 p_vout->p_sys->last_date = new_date;
356 f_angle = p_vout->p_sys->f_angle;
358 for( i_index = 0 ; i_index < p_inpic->i_planes ; i_index++ )
360 int i_line, i_first_line, i_num_lines, i_offset;
364 black_pixel = ( i_index == Y_PLANE ) ? 0x00 : 0x80;
366 i_num_lines = p_inpic->p[i_index].i_lines;
368 i_first_line = i_num_lines * 4 / 5;
370 p_in = p_inpic->p[i_index].p_pixels;
371 p_out = p_outpic->p[i_index].p_pixels;
373 p_vout->p_vlc->pf_memcpy( p_out, p_in,
374 i_first_line * p_inpic->p[i_index].i_pitch );
376 p_in += i_first_line * p_inpic->p[i_index].i_pitch;
377 p_out += i_first_line * p_outpic->p[i_index].i_pitch;
379 /* Ok, we do 3 times the sin() calculation for each line. So what ? */
380 for( i_line = i_first_line ; i_line < i_num_lines ; i_line++ )
382 /* Calculate today's offset, don't go above 1/20th of the screen */
383 i_offset = (int)( (double)(p_inpic->p[i_index].i_pitch)
384 * sin( f_angle + 2.0 * (double)i_line
385 / (double)( 1 + i_line
387 * (double)(i_line - i_first_line)
388 / (double)i_num_lines
395 p_vout->p_vlc->pf_memcpy( p_out, p_in - i_offset,
396 p_inpic->p[i_index].i_pitch + i_offset );
397 p_in -= p_inpic->p[i_index].i_pitch;
398 p_out += p_outpic->p[i_index].i_pitch;
399 memset( p_out + i_offset, black_pixel, -i_offset );
403 p_vout->p_vlc->pf_memcpy( p_out + i_offset, p_in,
404 p_inpic->p[i_index].i_pitch - i_offset );
405 memset( p_out, black_pixel, i_offset );
406 p_in -= p_inpic->p[i_index].i_pitch;
407 p_out += p_outpic->p[i_index].i_pitch;
412 p_vout->p_vlc->pf_memcpy( p_out, p_in,
413 p_inpic->p[i_index].i_pitch );
414 p_in -= p_inpic->p[i_index].i_pitch;
415 p_out += p_outpic->p[i_index].i_pitch;