1 /*****************************************************************************
2 * effects.c : Effects for the visualization system
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: effects.c,v 1.5 2003/09/03 10:00:23 zorglub Exp $
7 * Authors: Clément Stenac <zorglub@via.ecp.fr>
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 *****************************************************************************/
34 /*****************************************************************************
36 *****************************************************************************/
37 int dummy_Run( visual_effect_t * p_effect, aout_instance_t *p_aout,
38 aout_buffer_t * p_buffer , picture_t * p_picture)
43 /*****************************************************************************
44 * spectrum_Run: spectrum analyser
45 *****************************************************************************/
46 int spectrum_Run(visual_effect_t * p_effect, aout_instance_t *p_aout,
47 aout_buffer_t * p_buffer , picture_t * p_picture)
49 float p_output[FFT_BUFFER_SIZE]; /* Raw FFT Result */
50 int *height; /* Bar heights */
51 int *peaks; /* Peaks */
52 int i_nb_bands; /* number of bands */
53 int i_band_width; /* width of bands */
54 int i_separ; /* Should we let blanks ? */
55 int i_amp; /* Vertical amplification */
56 int i_peak; /* Should we draw peaks ? */
57 char *psz_parse = NULL; /* Args line */
59 /* Horizontal scale for 20-band equalizer */
60 const int xscale1[]={0,1,2,3,4,5,6,7,8,11,15,20,27,
61 36,47,62,82,107,141,184,255};
63 /* Horizontal scale for 80-band equalizer */
65 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,
66 19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,
67 35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
68 52,53,54,55,56,57,58,59,61,63,67,72,77,82,87,93,99,105,
69 110,115,121,130,141,152,163,174,185,200,255};
71 const double y_scale = 3.60673760222; /* (log 256) */
73 fft_state *p_state; /* internal FFT data */
77 s16 p_dest[FFT_BUFFER_SIZE]; /* Adapted FFT result */
78 s16 p_buffer1[FFT_BUFFER_SIZE]; /* Buffer on which we perform
79 the FFT (first channel) */
81 float *p_buffl = /* Original buffer */
82 (float*)p_buffer->p_buffer;
84 s16 *p_buffs; /* s16 converted buffer */
85 s16 *p_s16_buff = NULL; /* s16 converted buffer */
87 p_s16_buff = (s16*)malloc(
88 p_buffer->i_nb_samples * p_effect->i_nb_chans * sizeof(s16));
92 msg_Err(p_aout,"Out of memory");
97 i_nb_bands = config_GetInt ( p_aout, "visual-nbbands" );
98 i_separ = config_GetInt( p_aout, "visual-separ" );
99 i_amp = config_GetInt ( p_aout, "visual-amp" );
100 i_peak = config_GetInt ( p_aout, "visual-peaks" );
102 if( i_nb_bands == 20)
112 if( !p_effect->p_data )
114 p_effect->p_data=(void *)malloc(i_nb_bands * sizeof(int) );
115 if( !p_effect->p_data)
117 msg_Err(p_aout,"Out of memory");
120 peaks = (int *)p_effect->p_data;
121 for( i = 0 ; i < i_nb_bands ; i++)
129 peaks =(int *)p_effect->p_data;
133 height = (int *)malloc( i_nb_bands * sizeof(int) );
136 msg_Err(p_aout,"Out of memory");
139 /* Convert the buffer to s16 */
140 /* Pasted from float32tos16.c */
141 for (i = p_buffer->i_nb_samples * p_effect->i_nb_chans; i--; )
143 float f_in = *p_buffl + 384.0;
145 i_in = *(s32 *)&f_in;
146 if(i_in > 0x43c07fff ) * p_buffs = 32767;
147 else if ( i_in < 0x43bf8000 ) *p_buffs = -32768;
148 else *p_buffs = i_in - 0x43c00000;
150 p_buffl++ ; p_buffs++ ;
152 p_state = fft_init();
155 msg_Err(p_aout,"Unable to initialize FFT transform");
158 p_buffs = p_s16_buff;
159 for ( i = 0 ; i < FFT_BUFFER_SIZE ; i++)
162 p_buffer1[i] = *p_buffs;
163 p_buffs = p_buffs + p_effect->i_nb_chans;
165 fft_perform( p_buffer1, p_output, p_state);
166 for(i= 0; i< FFT_BUFFER_SIZE ; i++ )
167 p_dest[i] = ( (int) sqrt( p_output [ i + 1 ] ) ) >> 8;
169 for ( i = 0 ; i< i_nb_bands ;i++)
171 /* We search the maximum on one scale */
172 for( j = xscale[i] , y=0 ; j< xscale[ i + 1 ] ; j++ )
177 /* Calculate the height of the bar */
178 y >>=5; /* remove some noise */
181 height[i] = (int)log(y)* y_scale;
190 /* Draw the bar now */
191 i_band_width = floor( p_effect->i_width / i_nb_bands) ;
193 if( i_amp * height[i] > peaks[i])
195 peaks[i] = i_amp * height[i];
197 else if (peaks[i] > 0 )
199 peaks[i] -= PEAK_SPEED;
200 if( peaks[i] < i_amp * height[i] )
202 peaks[i] = i_amp * height[i];
210 if( peaks[i] > 0 && i_peak )
212 if( peaks[i] >= p_effect->i_height )
213 peaks[i] = p_effect->i_height - 2;
216 for( j = 0 ; j< i_band_width - i_separ; j++)
218 for( k = 0 ; k< 3 ; k ++)
221 *(p_picture->p[0].p_pixels +
222 (p_picture->p[0].i_lines - i_line -1 -k ) *
223 p_picture->p[0].i_pitch + (i_band_width*i +j) )
226 *(p_picture->p[1].p_pixels +
227 (p_picture->p[1].i_lines - i_line /2 -1 -k/2 ) *
228 p_picture->p[1].i_pitch +
229 ( ( i_band_width * i + j ) /2 ) )
232 if( 0x04 * (i_line + k ) - 0x0f > 0 )
234 if ( 0x04 * (i_line + k ) -0x0f < 0xff)
235 *(p_picture->p[2].p_pixels +
236 (p_picture->p[2].i_lines - i_line /2 - 1 -k/2 ) *
237 p_picture->p[2].i_pitch +
238 ( ( i_band_width * i + j ) /2 ) )
239 = ( 0x04 * ( i_line + k ) ) -0x0f ;
241 *(p_picture->p[2].p_pixels +
242 (p_picture->p[2].i_lines - i_line /2 - 1 -k/2 ) *
243 p_picture->p[2].i_pitch +
244 ( ( i_band_width * i + j ) /2 ) )
249 *(p_picture->p[2].p_pixels +
250 (p_picture->p[2].i_lines - i_line /2 - 1 -k/2 ) *
251 p_picture->p[2].i_pitch +
252 ( ( i_band_width * i + j ) /2 ) )
259 if(height[i] * i_amp > p_effect->i_height)
260 height[i] = floor(p_effect->i_height / i_amp );
262 for(i_line = 0 ; i_line < i_amp * height[i]; i_line ++ )
264 for( j = 0 ; j< i_band_width - i_separ ; j++)
266 *(p_picture->p[0].p_pixels +
267 (p_picture->p[0].i_lines - i_line -1) *
268 p_picture->p[0].i_pitch + (i_band_width*i +j) ) = 0xff;
270 *(p_picture->p[1].p_pixels +
271 (p_picture->p[1].i_lines - i_line /2 -1) *
272 p_picture->p[1].i_pitch +
273 ( ( i_band_width * i + j ) /2 ) ) = 0x00;
276 if( 0x04 * i_line - 0x0f > 0 )
278 if( 0x04 * i_line - 0x0f < 0xff )
279 *(p_picture->p[2].p_pixels +
280 (p_picture->p[2].i_lines - i_line /2 - 1) *
281 p_picture->p[2].i_pitch +
282 ( ( i_band_width * i + j ) /2 ) ) =
283 ( 0x04 * i_line) -0x0f ;
285 *(p_picture->p[2].p_pixels +
286 (p_picture->p[2].i_lines - i_line /2 - 1) *
287 p_picture->p[2].i_pitch +
288 ( ( i_band_width * i + j ) /2 ) ) =
293 *(p_picture->p[2].p_pixels +
294 (p_picture->p[2].i_lines - i_line /2 - 1) *
295 p_picture->p[2].i_pitch +
296 ( ( i_band_width * i + j ) /2 ) ) =
303 fft_close( p_state );
305 if( p_s16_buff != NULL )
311 if(height) free(height);
313 if(psz_parse) free(psz_parse);
319 /*****************************************************************************
320 * scope_Run: scope effect
321 *****************************************************************************/
322 int scope_Run(visual_effect_t * p_effect, aout_instance_t *p_aout,
323 aout_buffer_t * p_buffer , picture_t * p_picture)
330 for( i_index = 0 ; i_index < 2 ; i_index++ )
333 for( j = 0 ; j < 3 ; j++ )
335 ppp_area[i_index][j] =
336 p_picture->p[j].p_pixels + i_index * p_picture->p[j].i_lines
337 / 2 * p_picture->p[j].i_pitch;
341 for( i_index = 0, p_sample = (float *)p_buffer->p_buffer;
342 i_index < p_effect->i_width;
348 i_value = (*p_sample++ +1) * 127;
350 + p_picture->p[0].i_pitch * i_index / p_effect->i_width
351 + p_picture->p[0].i_lines * i_value / 512
352 * p_picture->p[0].i_pitch) = 0xbf;
354 + p_picture->p[1].i_pitch * i_index / p_effect->i_width
355 + p_picture->p[1].i_lines * i_value / 512
356 * p_picture->p[1].i_pitch) = 0xff;
360 i_value = ( *p_sample++ +1 ) * 127;
362 + p_picture->p[0].i_pitch * i_index / p_effect->i_width
363 + p_picture->p[0].i_lines * i_value / 512
364 * p_picture->p[0].i_pitch) = 0x9f;
366 + p_picture->p[2].i_pitch * i_index / p_effect->i_width
367 + p_picture->p[2].i_lines * i_value / 512
368 * p_picture->p[2].i_pitch) = 0xdd;
374 /*****************************************************************************
375 * random_Run: random plots display effect
376 *****************************************************************************/
377 int random_Run(visual_effect_t * p_effect, aout_instance_t *p_aout,
378 aout_buffer_t * p_buffer , picture_t * p_picture)
381 char *psz_parse= NULL;
382 int i , i_y , i_u , i_v;
384 srand((unsigned int)mdate());
386 if( p_effect->psz_args )
388 psz_parse = strdup( p_effect->psz_args );
389 i_nb_plots = config_GetInt ( p_aout, "visual-stars" );
396 for( i = 0 ; i < i_nb_plots ; i++ )
398 i_position = rand() % (p_effect->i_width * p_effect->i_height );
402 *(p_picture->p[0].p_pixels + i_position )= i_u;
403 *(p_picture->p[1].p_pixels + i_position/4) = i_v;
404 *(p_picture->p[2].p_pixels + i_position/4) = i_y;
409 /*****************************************************************************
410 * blur_Run: blur effect
411 *****************************************************************************/
413 /* This code is totally crappy */
414 int blur_Run(visual_effect_t * p_effect, aout_instance_t *p_aout,
415 aout_buffer_t * p_buffer , picture_t * p_picture)
417 uint8_t * p_pictures;
419 int i_size; /* Total size of one image */
421 i_size = (p_picture->p[0].i_pitch * p_picture->p[0].i_lines +
422 p_picture->p[1].i_pitch * p_picture->p[1].i_lines +
423 p_picture->p[2].i_pitch * p_picture->p[2].i_lines );
425 if( !p_effect->p_data )
427 p_effect->p_data=(void *)malloc( 5 * i_size *sizeof(uint8_t));
429 if( !p_effect->p_data)
431 msg_Err(p_aout,"Out of memory");
434 p_pictures = (uint8_t *)p_effect->p_data;
438 p_pictures =(uint8_t *)p_effect->p_data;
441 for( i = 0 ; i < 5 ; i++)
443 for ( j = 0 ; j< p_picture->p[0].i_pitch * p_picture->p[0].i_lines; i++)
444 p_picture->p[0].p_pixels[j] =
445 p_pictures[i * i_size + j] * (100 - 20 * i) /100 ;
446 for ( j = 0 ; j< p_picture->p[1].i_pitch * p_picture->p[1].i_lines; i++)
447 p_picture->p[1].p_pixels[j] =
448 p_pictures[i * i_size +
449 p_picture->p[0].i_pitch * p_picture->p[0].i_lines + j ];
450 for ( j = 0 ; j< p_picture->p[2].i_pitch * p_picture->p[2].i_lines; i++)
451 p_picture->p[2].p_pixels[j] =
452 p_pictures[i * i_size +
453 p_picture->p[0].i_pitch * p_picture->p[0].i_lines +
454 p_picture->p[1].i_pitch * p_picture->p[1].i_lines
458 memcpy ( &p_pictures[ i_size ] , &p_pictures[0] , 4 * i_size * sizeof(uint8_t) );