1 /*****************************************************************************
2 * effects.c : Effects for the visualization system
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include "audio_output.h"
31 #include "aout_internal.h"
40 /*****************************************************************************
42 *****************************************************************************/
43 int dummy_Run( visual_effect_t * p_effect, aout_instance_t *p_aout,
44 aout_buffer_t * p_buffer , picture_t * p_picture)
49 /*****************************************************************************
50 * spectrum_Run: spectrum analyser
51 *****************************************************************************/
52 int spectrum_Run(visual_effect_t * p_effect, aout_instance_t *p_aout,
53 aout_buffer_t * p_buffer , picture_t * p_picture)
55 float p_output[FFT_BUFFER_SIZE]; /* Raw FFT Result */
56 int *height; /* Bar heights */
57 int *peaks; /* Peaks */
58 int i_nb_bands; /* number of bands */
59 int i_band_width; /* width of bands */
60 int i_separ; /* Should we let blanks ? */
61 int i_amp; /* Vertical amplification */
62 int i_peak; /* Should we draw peaks ? */
63 char *psz_parse = NULL; /* Args line */
65 /* Horizontal scale for 20-band equalizer */
66 const int xscale1[]={0,1,2,3,4,5,6,7,8,11,15,20,27,
67 36,47,62,82,107,141,184,255};
69 /* Horizontal scale for 80-band equalizer */
71 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,
72 19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,
73 35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
74 52,53,54,55,56,57,58,59,61,63,67,72,77,82,87,93,99,105,
75 110,115,121,130,141,152,163,174,185,200,255};
77 const double y_scale = 3.60673760222; /* (log 256) */
79 fft_state *p_state; /* internal FFT data */
83 int16_t p_dest[FFT_BUFFER_SIZE]; /* Adapted FFT result */
84 int16_t p_buffer1[FFT_BUFFER_SIZE]; /* Buffer on which we perform
85 the FFT (first channel) */
87 float *p_buffl = /* Original buffer */
88 (float*)p_buffer->p_buffer;
90 int16_t *p_buffs; /* int16_t converted buffer */
91 int16_t *p_s16_buff = NULL; /* int16_t converted buffer */
93 p_s16_buff = (int16_t*)malloc(
94 p_buffer->i_nb_samples * p_effect->i_nb_chans * sizeof(int16_t));
98 msg_Err(p_aout,"Out of memory");
102 p_buffs = p_s16_buff;
103 i_nb_bands = config_GetInt ( p_aout, "visual-nbbands" );
104 i_separ = config_GetInt( p_aout, "visual-separ" );
105 i_amp = config_GetInt ( p_aout, "visual-amp" );
106 i_peak = config_GetInt ( p_aout, "visual-peaks" );
108 if( i_nb_bands == 20)
118 if( !p_effect->p_data )
120 p_effect->p_data=(void *)malloc(i_nb_bands * sizeof(int) );
121 if( !p_effect->p_data)
123 msg_Err(p_aout,"Out of memory");
126 peaks = (int *)p_effect->p_data;
127 for( i = 0 ; i < i_nb_bands ; i++)
135 peaks =(int *)p_effect->p_data;
139 height = (int *)malloc( i_nb_bands * sizeof(int) );
142 msg_Err(p_aout,"Out of memory");
145 /* Convert the buffer to int16_t */
146 /* Pasted from float32tos16.c */
147 for (i = p_buffer->i_nb_samples * p_effect->i_nb_chans; i--; )
149 union { float f; int32_t i; } u;
150 u.f = *p_buffl + 384.0;
151 if(u.i > 0x43c07fff ) * p_buffs = 32767;
152 else if ( u.i < 0x43bf8000 ) *p_buffs = -32768;
153 else *p_buffs = u.i - 0x43c00000;
155 p_buffl++ ; p_buffs++ ;
157 p_state = visual_fft_init();
160 msg_Err(p_aout,"Unable to initialize FFT transform");
163 p_buffs = p_s16_buff;
164 for ( i = 0 ; i < FFT_BUFFER_SIZE ; i++)
167 p_buffer1[i] = *p_buffs;
168 p_buffs = p_buffs + p_effect->i_nb_chans;
170 fft_perform( p_buffer1, p_output, p_state);
171 for(i= 0; i< FFT_BUFFER_SIZE ; i++ )
172 p_dest[i] = ( (int) sqrt( p_output [ i + 1 ] ) ) >> 8;
174 for ( i = 0 ; i< i_nb_bands ;i++)
176 /* We search the maximum on one scale */
177 for( j = xscale[i] , y=0 ; j< xscale[ i + 1 ] ; j++ )
182 /* Calculate the height of the bar */
183 y >>=7;/* remove some noise */
186 height[i] = (int)log(y)* y_scale;
195 /* Draw the bar now */
196 i_band_width = floor( p_effect->i_width / i_nb_bands) ;
198 if( i_amp * height[i] > peaks[i])
200 peaks[i] = i_amp * height[i];
202 else if (peaks[i] > 0 )
204 peaks[i] -= PEAK_SPEED;
205 if( peaks[i] < i_amp * height[i] )
207 peaks[i] = i_amp * height[i];
215 if( peaks[i] > 0 && i_peak )
217 if( peaks[i] >= p_effect->i_height )
218 peaks[i] = p_effect->i_height - 2;
221 for( j = 0 ; j< i_band_width - i_separ; j++)
223 for( k = 0 ; k< 3 ; k ++)
226 *(p_picture->p[0].p_pixels +
227 (p_picture->p[0].i_lines - i_line -1 -k ) *
228 p_picture->p[0].i_pitch + (i_band_width*i +j) )
231 *(p_picture->p[1].p_pixels +
232 (p_picture->p[1].i_lines - i_line /2 -1 -k/2 ) *
233 p_picture->p[1].i_pitch +
234 ( ( i_band_width * i + j ) /2 ) )
237 if( 0x04 * (i_line + k ) - 0x0f > 0 )
239 if ( 0x04 * (i_line + k ) -0x0f < 0xff)
240 *(p_picture->p[2].p_pixels +
241 (p_picture->p[2].i_lines - i_line /2 - 1 -k/2 ) *
242 p_picture->p[2].i_pitch +
243 ( ( i_band_width * i + j ) /2 ) )
244 = ( 0x04 * ( i_line + k ) ) -0x0f ;
246 *(p_picture->p[2].p_pixels +
247 (p_picture->p[2].i_lines - i_line /2 - 1 -k/2 ) *
248 p_picture->p[2].i_pitch +
249 ( ( i_band_width * i + j ) /2 ) )
254 *(p_picture->p[2].p_pixels +
255 (p_picture->p[2].i_lines - i_line /2 - 1 -k/2 ) *
256 p_picture->p[2].i_pitch +
257 ( ( i_band_width * i + j ) /2 ) )
264 if(height[i] * i_amp > p_effect->i_height)
265 height[i] = floor(p_effect->i_height / i_amp );
267 for(i_line = 0 ; i_line < i_amp * height[i]; i_line ++ )
269 for( j = 0 ; j< i_band_width - i_separ ; j++)
271 *(p_picture->p[0].p_pixels +
272 (p_picture->p[0].i_lines - i_line -1) *
273 p_picture->p[0].i_pitch + (i_band_width*i +j) ) = 0xff;
275 *(p_picture->p[1].p_pixels +
276 (p_picture->p[1].i_lines - i_line /2 -1) *
277 p_picture->p[1].i_pitch +
278 ( ( i_band_width * i + j ) /2 ) ) = 0x00;
280 if( 0x04 * i_line - 0x0f > 0 )
282 if( 0x04 * i_line - 0x0f < 0xff )
283 *(p_picture->p[2].p_pixels +
284 (p_picture->p[2].i_lines - i_line /2 - 1) *
285 p_picture->p[2].i_pitch +
286 ( ( i_band_width * i + j ) /2 ) ) =
287 ( 0x04 * i_line) -0x0f ;
289 *(p_picture->p[2].p_pixels +
290 (p_picture->p[2].i_lines - i_line /2 - 1) *
291 p_picture->p[2].i_pitch +
292 ( ( i_band_width * i + j ) /2 ) ) =
297 *(p_picture->p[2].p_pixels +
298 (p_picture->p[2].i_lines - i_line /2 - 1) *
299 p_picture->p[2].i_pitch +
300 ( ( i_band_width * i + j ) /2 ) ) =
307 fft_close( p_state );
309 if( p_s16_buff != NULL )
315 if(height) free(height);
317 if(psz_parse) free(psz_parse);
323 /*****************************************************************************
324 * scope_Run: scope effect
325 *****************************************************************************/
326 int scope_Run(visual_effect_t * p_effect, aout_instance_t *p_aout,
327 aout_buffer_t * p_buffer , picture_t * p_picture)
331 uint8_t *ppp_area[2][3];
334 for( i_index = 0 ; i_index < 2 ; i_index++ )
337 for( j = 0 ; j < 3 ; j++ )
339 ppp_area[i_index][j] =
340 p_picture->p[j].p_pixels + i_index * p_picture->p[j].i_lines
341 / 2 * p_picture->p[j].i_pitch;
345 for( i_index = 0, p_sample = (float *)p_buffer->p_buffer;
346 i_index < p_effect->i_width;
352 i_value = (*p_sample++ +1) * 127;
354 + p_picture->p[0].i_pitch * i_index / p_effect->i_width
355 + p_picture->p[0].i_lines * i_value / 512
356 * p_picture->p[0].i_pitch) = 0xbf;
358 + p_picture->p[1].i_pitch * i_index / p_effect->i_width
359 + p_picture->p[1].i_lines * i_value / 512
360 * p_picture->p[1].i_pitch) = 0xff;
364 i_value = ( *p_sample++ +1 ) * 127;
366 + p_picture->p[0].i_pitch * i_index / p_effect->i_width
367 + p_picture->p[0].i_lines * i_value / 512
368 * p_picture->p[0].i_pitch) = 0x9f;
370 + p_picture->p[2].i_pitch * i_index / p_effect->i_width
371 + p_picture->p[2].i_lines * i_value / 512
372 * p_picture->p[2].i_pitch) = 0xdd;
378 /*****************************************************************************
379 * random_Run: random plots display effect
380 *****************************************************************************/
381 int random_Run(visual_effect_t * p_effect, aout_instance_t *p_aout,
382 aout_buffer_t * p_buffer , picture_t * p_picture)
385 char *psz_parse= NULL;
386 int i , i_y , i_u , i_v;
388 srand((unsigned int)mdate());
390 if( p_effect->psz_args )
392 psz_parse = strdup( p_effect->psz_args );
393 i_nb_plots = config_GetInt ( p_aout, "visual-stars" );
400 for( i = 0 ; i < i_nb_plots ; i++ )
402 i_position = rand() % (p_effect->i_width * p_effect->i_height );
406 *(p_picture->p[0].p_pixels + i_position )= i_u;
407 *(p_picture->p[1].p_pixels + i_position/4) = i_v;
408 *(p_picture->p[2].p_pixels + i_position/4) = i_y;
413 /*****************************************************************************
414 * blur_Run: blur effect
415 *****************************************************************************/
417 /* This code is totally crappy */
418 int blur_Run(visual_effect_t * p_effect, aout_instance_t *p_aout,
419 aout_buffer_t * p_buffer , picture_t * p_picture)
421 uint8_t * p_pictures;
423 int i_size; /* Total size of one image */
425 i_size = (p_picture->p[0].i_pitch * p_picture->p[0].i_lines +
426 p_picture->p[1].i_pitch * p_picture->p[1].i_lines +
427 p_picture->p[2].i_pitch * p_picture->p[2].i_lines );
429 if( !p_effect->p_data )
431 p_effect->p_data=(void *)malloc( 5 * i_size *sizeof(uint8_t));
433 if( !p_effect->p_data)
435 msg_Err(p_aout,"Out of memory");
438 p_pictures = (uint8_t *)p_effect->p_data;
442 p_pictures =(uint8_t *)p_effect->p_data;
445 for( i = 0 ; i < 5 ; i++)
447 for ( j = 0 ; j< p_picture->p[0].i_pitch * p_picture->p[0].i_lines; i++)
448 p_picture->p[0].p_pixels[j] =
449 p_pictures[i * i_size + j] * (100 - 20 * i) /100 ;
450 for ( j = 0 ; j< p_picture->p[1].i_pitch * p_picture->p[1].i_lines; i++)
451 p_picture->p[1].p_pixels[j] =
452 p_pictures[i * i_size +
453 p_picture->p[0].i_pitch * p_picture->p[0].i_lines + j ];
454 for ( j = 0 ; j< p_picture->p[2].i_pitch * p_picture->p[2].i_lines; i++)
455 p_picture->p[2].p_pixels[j] =
456 p_pictures[i * i_size +
457 p_picture->p[0].i_pitch * p_picture->p[0].i_lines +
458 p_picture->p[1].i_pitch * p_picture->p[1].i_lines
462 memcpy ( &p_pictures[ i_size ] , &p_pictures[0] , 4 * i_size * sizeof(uint8_t) );