]> git.sesse.net Git - vlc/blob - modules/visualization/visual/effects.c
visual: factorize.
[vlc] / modules / visualization / visual / effects.c
1 /*****************************************************************************
2  * effects.c : Effects for the visualization system
3  *****************************************************************************
4  * Copyright (C) 2002-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@via.ecp.fr>
8  *          Adrien Maglo <magsoft@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_vout.h>
34 #include <vlc_aout.h>
35
36 #include "visual.h"
37 #include <math.h>
38
39 #include "fft.h"
40
41 #define PEAK_SPEED 1
42 #define BAR_DECREASE_SPEED 5
43
44 #define GRAD_ANGLE_MIN 0.2
45 #define GRAD_ANGLE_MAX 0.5
46 #define GRAD_INCR 0.01
47
48 /*****************************************************************************
49  * dummy_Run
50  *****************************************************************************/
51 int dummy_Run( visual_effect_t * p_effect, vlc_object_t *p_aout,
52                const block_t * p_buffer , picture_t * p_picture)
53 {
54     VLC_UNUSED(p_effect); VLC_UNUSED(p_aout); VLC_UNUSED(p_buffer);
55     VLC_UNUSED(p_picture);
56     return 0;
57 }
58
59 /*****************************************************************************
60  * spectrum_Run: spectrum analyser
61  *****************************************************************************/
62 int spectrum_Run(visual_effect_t * p_effect, vlc_object_t *p_aout,
63                  const block_t * p_buffer , picture_t * p_picture)
64 {
65     spectrum_data *p_data = p_effect->p_data;
66     float p_output[FFT_BUFFER_SIZE];  /* Raw FFT Result  */
67     int *height;                      /* Bar heights */
68     int *peaks;                       /* Peaks */
69     int *prev_heights;                /* Previous bar heights */
70     int i_80_bands;                   /* number of bands : 80 if true else 20 */
71     int i_nb_bands;                   /* number of bands : 80 or 20 */
72     int i_band_width;                 /* width of bands */
73     int i_start;                      /* first band horizontal position */
74     int i_peak;                       /* Should we draw peaks ? */
75
76     /* Horizontal scale for 20-band equalizer */
77     const int xscale1[]={0,1,2,3,4,5,6,7,8,11,15,20,27,
78                         36,47,62,82,107,141,184,255};
79
80     /* Horizontal scale for 80-band equalizer */
81     const int xscale2[] =
82     {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,
83      19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,
84      35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
85      52,53,54,55,56,57,58,59,61,63,67,72,77,82,87,93,99,105,
86      110,115,121,130,141,152,163,174,185,200,255};
87     const int *xscale;
88
89     fft_state *p_state;                 /* internal FFT data */
90
91     int i , j , y , k;
92     int i_line;
93     int16_t p_dest[FFT_BUFFER_SIZE];      /* Adapted FFT result */
94     int16_t p_buffer1[FFT_BUFFER_SIZE];   /* Buffer on which we perform
95                                              the FFT (first channel) */
96
97     float *p_buffl =                     /* Original buffer */
98             (float*)p_buffer->p_buffer;
99
100     int16_t  *p_buffs;                    /* int16_t converted buffer */
101     int16_t  *p_s16_buff;                 /* int16_t converted buffer */
102
103     p_s16_buff = malloc( p_buffer->i_nb_samples * p_effect->i_nb_chans * sizeof(int16_t));
104     if( !p_s16_buff )
105         return -1;
106
107     p_buffs = p_s16_buff;
108     i_80_bands = config_GetInt ( p_aout, "visual-80-bands" );
109     i_peak     = config_GetInt ( p_aout, "visual-peaks" );
110
111     if( i_80_bands != 0)
112     {
113         xscale = xscale2;
114         i_nb_bands = 80;
115     }
116     else
117     {
118         xscale = xscale1;
119         i_nb_bands = 20;
120     }
121
122     if( !p_data )
123     {
124         p_effect->p_data = p_data = malloc( sizeof( spectrum_data ) );
125         if( !p_data )
126         {
127             free( p_s16_buff );
128             return -1;
129         }
130
131         p_data->peaks = calloc( 80, sizeof(int) );
132         p_data->prev_heights = calloc( 80, sizeof(int) );
133     }
134     peaks = (int *)p_data->peaks;
135     prev_heights = (int *)p_data->prev_heights;
136
137
138     height = malloc( i_nb_bands * sizeof(int) );
139     if( !height )
140     {
141         free( p_s16_buff );
142         return -1;
143     }
144     /* Convert the buffer to int16_t  */
145     /* Pasted from float32tos16.c */
146     for (i = p_buffer->i_nb_samples * p_effect->i_nb_chans; i--; )
147     {
148         union { float f; int32_t i; } u;
149         u.f = *p_buffl + 384.0;
150         if(u.i >  0x43c07fff ) * p_buffs = 32767;
151         else if ( u.i < 0x43bf8000 ) *p_buffs = -32768;
152         else *p_buffs = u.i - 0x43c00000;
153
154         p_buffl++ ; p_buffs++ ;
155     }
156     p_state  = visual_fft_init();
157     if( !p_state)
158     {
159         free( height );
160         free( p_s16_buff );
161         msg_Err(p_aout,"unable to initialize FFT transform");
162         return -1;
163     }
164     p_buffs = p_s16_buff;
165     for ( i = 0 ; i < FFT_BUFFER_SIZE ; i++)
166     {
167         p_output[i]  = 0;
168         p_buffer1[i] = *p_buffs;
169
170         p_buffs += p_effect->i_nb_chans;
171         if( p_buffs >= &p_s16_buff[p_buffer->i_nb_samples * p_effect->i_nb_chans] )
172             p_buffs = p_s16_buff;
173
174     }
175     fft_perform( p_buffer1, p_output, p_state);
176     for( i = 0; i< FFT_BUFFER_SIZE ; i++ )
177         p_dest[i] = p_output[i] *  ( 2 ^ 16 ) / ( ( FFT_BUFFER_SIZE / 2 * 32768 ) ^ 2 );
178
179     /* Compute the horizontal position of the first band */
180     i_band_width = floor( p_effect->i_width / i_nb_bands);
181     i_start = ( p_effect->i_width - i_band_width * i_nb_bands ) / 2;
182
183     for ( i = 0 ; i < i_nb_bands ;i++)
184     {
185         /* We search the maximum on one scale */
186         for( j = xscale[i], y = 0; j< xscale[ i + 1 ]; j++ )
187         {
188             if ( p_dest[j] > y )
189                  y = p_dest[j];
190         }
191         /* Calculate the height of the bar */
192         if( y != 0 )
193         {
194             height[i] = log( y ) * 30;
195             if( height[i] > 380 )
196                 height[i] = 380;
197         }
198         else
199             height[ i ] = 0;
200
201         /* Draw the bar now */
202
203         if( height[i] > peaks[i] )
204         {
205             peaks[i] = height[i];
206         }
207         else if( peaks[i] > 0 )
208         {
209             peaks[i] -= PEAK_SPEED;
210             if( peaks[i] < height[i] )
211             {
212                 peaks[i] = height[i];
213             }
214             if( peaks[i] < 0 )
215             {
216                 peaks[i] = 0;
217             }
218         }
219
220         /* Decrease the bars if needed */
221         if( height[i] <= prev_heights[i] - BAR_DECREASE_SPEED )
222         {
223             height[i] = prev_heights[i];
224             height[i] -= BAR_DECREASE_SPEED;
225         }
226         prev_heights[i] = height[i];
227
228         if( peaks[i] > 0 && i_peak )
229         {
230             if( peaks[i] >= p_effect->i_height )
231                 peaks[i] = p_effect->i_height - 2;
232             i_line = peaks[i];
233
234             for( j = 0; j < i_band_width - 1; j++ )
235             {
236                for( k = 0; k < 3; k ++ )
237                {
238                    /* Draw the peak */
239                    *(p_picture->p[0].p_pixels +
240                     ( p_effect->i_height - i_line -1 -k ) *
241                      p_picture->p[0].i_pitch +
242                      ( i_start + i_band_width*i + j ) )
243                                     = 0xff;
244
245                    *(p_picture->p[1].p_pixels +
246                     ( ( p_effect->i_height - i_line ) / 2 - 1 -k/2 ) *
247                      p_picture->p[1].i_pitch +
248                      ( ( i_start + i_band_width * i + j ) /2  ) )
249                                     = 0x00;
250
251                    if( i_line + k - 0x0f > 0 )
252                    {
253                        if ( i_line + k - 0x0f < 0xff )
254                            *(p_picture->p[2].p_pixels  +
255                             ( ( p_effect->i_height - i_line ) / 2 - 1 -k/2 ) *
256                              p_picture->p[2].i_pitch +
257                              ( ( i_start + i_band_width * i + j ) /2  ) )
258                                     = ( i_line + k ) - 0x0f;
259                        else
260                            *(p_picture->p[2].p_pixels  +
261                             ( ( p_effect->i_height - i_line ) / 2 - 1 -k/2 ) *
262                              p_picture->p[2].i_pitch +
263                              ( ( i_start + i_band_width * i + j ) /2  ) )
264                                     = 0xff;
265                    }
266                    else
267                    {
268                         *(p_picture->p[2].p_pixels  +
269                          ( ( p_effect->i_height - i_line ) / 2 - 1 -k/2 ) *
270                          p_picture->p[2].i_pitch +
271                          ( ( i_start + i_band_width * i + j ) /2  ) )
272                                = 0x10 ;
273                    }
274                }
275             }
276         }
277
278         if(height[i] > p_effect->i_height)
279             height[i] = floor(p_effect->i_height );
280
281         for( i_line = 0; i_line < height[i]; i_line++ )
282         {
283             for( j = 0 ; j < i_band_width - 1; j++)
284             {
285                *(p_picture->p[0].p_pixels +
286                  (p_effect->i_height - i_line - 1) *
287                   p_picture->p[0].i_pitch +
288                   ( i_start + i_band_width*i + j ) ) = 0xff;
289
290                *(p_picture->p[1].p_pixels +
291                  ( ( p_effect->i_height - i_line ) / 2 - 1) *
292                  p_picture->p[1].i_pitch +
293                  ( ( i_start + i_band_width * i + j ) /2  ) ) = 0x00;
294
295                if( i_line - 0x0f > 0 )
296                {
297                     if( i_line - 0x0f < 0xff )
298                          *(p_picture->p[2].p_pixels  +
299                            ( ( p_effect->i_height - i_line ) / 2 - 1) *
300                            p_picture->p[2].i_pitch +
301                            ( ( i_start + i_band_width * i + j ) /2  ) ) =
302                                i_line - 0x0f;
303                     else
304                          *(p_picture->p[2].p_pixels  +
305                            ( ( p_effect->i_height - i_line ) / 2  - 1) *
306                            p_picture->p[2].i_pitch +
307                            ( ( i_start + i_band_width * i + j ) /2  ) ) =
308                                        0xff;
309                }
310                else
311                {
312                     *(p_picture->p[2].p_pixels  +
313                       ( ( p_effect->i_height - i_line ) / 2  - 1) *
314                       p_picture->p[2].i_pitch +
315                       ( ( i_start + i_band_width * i + j ) /2  ) ) =
316                             0x10;
317                }
318             }
319         }
320     }
321
322     fft_close( p_state );
323
324     free( p_s16_buff );
325     free( height );
326
327     return 0;
328 }
329
330
331 /*****************************************************************************
332  * spectrometer_Run: derivative spectrum analysis
333  *****************************************************************************/
334 int spectrometer_Run(visual_effect_t * p_effect, vlc_object_t *p_aout,
335                      const block_t * p_buffer , picture_t * p_picture)
336 {
337 #define Y(R,G,B) ((uint8_t)( (R * .299) + (G * .587) + (B * .114) ))
338 #define U(R,G,B) ((uint8_t)( (R * -.169) + (G * -.332) + (B * .500) + 128 ))
339 #define V(R,G,B) ((uint8_t)( (R * .500) + (G * -.419) + (B * -.0813) + 128 ))
340     float p_output[FFT_BUFFER_SIZE];  /* Raw FFT Result  */
341     int *height;                      /* Bar heights */
342     int *peaks;                       /* Peaks */
343     int i_80_bands;                   /* number of bands : 80 if true else 20 */
344     int i_nb_bands;                   /* number of bands : 80 or 20 */
345     int i_band_width;                 /* width of bands */
346     int i_separ;                      /* Should we let blanks ? */
347     int i_amp;                        /* Vertical amplification */
348     int i_peak;                       /* Should we draw peaks ? */
349
350     int i_original;          /* original spectrum graphic routine */
351     int i_rad;               /* radius of circle of base of bands */
352     int i_sections;          /* sections of spectranalysis */
353     int i_extra_width;       /* extra width on peak */
354     int i_peak_height;       /* height of peak */
355     int c;                   /* sentinel container of total spectral sections */
356     double band_sep_angle;   /* angled separation between beginning of each band */
357     double section_sep_angle;/* "   "    '     "    '    "     "    spectrum section */
358     int max_band_length;     /* try not to go out of screen */
359     int i_show_base;         /* Should we draw base of circle ? */
360     int i_show_bands;        /* Should we draw bands ? */
361     //int i_invert_bands;      /* do the bands point inward ? */
362     double a;                /* for various misc angle situations in radians */
363     int x,y,xx,yy;           /* various misc x/y */
364     char color1;             /* V slide on a YUV color cube */
365     //char color2;             /* U slide.. ?  color2 fade color ? */
366
367     /* Horizontal scale for 20-band equalizer */
368     const int xscale1[]={0,1,2,3,4,5,6,7,8,11,15,20,27,
369                         36,47,62,82,107,141,184,255};
370
371     /* Horizontal scale for 80-band equalizer */
372     const int xscale2[] =
373     {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,
374      19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,
375      35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
376      52,53,54,55,56,57,58,59,61,63,67,72,77,82,87,93,99,105,
377      110,115,121,130,141,152,163,174,185,200,255};
378     const int *xscale;
379     const double y_scale =  3.60673760222;  /* (log 256) */
380
381     fft_state *p_state;                 /* internal FFT data */
382
383     int i , j , k;
384     int i_line;
385     int16_t p_dest[FFT_BUFFER_SIZE];      /* Adapted FFT result */
386     int16_t p_buffer1[FFT_BUFFER_SIZE];   /* Buffer on which we perform
387                                              the FFT (first channel) */
388     float *p_buffl =                     /* Original buffer */
389             (float*)p_buffer->p_buffer;
390
391     int16_t  *p_buffs;                    /* int16_t converted buffer */
392     int16_t  *p_s16_buff;                /* int16_t converted buffer */
393
394     i_line = 0;
395
396     p_s16_buff = malloc( p_buffer->i_nb_samples * p_effect->i_nb_chans * sizeof(int16_t) );
397     if( !p_s16_buff )
398         return -1;
399
400     p_buffs = p_s16_buff;
401     i_original     = config_GetInt ( p_aout, "spect-show-original" );
402     i_80_bands     = config_GetInt ( p_aout, "spect-80-bands" );
403     i_separ        = config_GetInt ( p_aout, "spect-separ" );
404     i_amp          = config_GetInt ( p_aout, "spect-amp" );
405     i_peak         = config_GetInt ( p_aout, "spect-show-peaks" );
406     i_show_base    = config_GetInt ( p_aout, "spect-show-base" );
407     i_show_bands   = config_GetInt ( p_aout, "spect-show-bands" );
408     i_rad          = config_GetInt ( p_aout, "spect-radius" );
409     i_sections     = config_GetInt ( p_aout, "spect-sections" );
410     i_extra_width  = config_GetInt ( p_aout, "spect-peak-width" );
411     i_peak_height  = config_GetInt ( p_aout, "spect-peak-height" );
412     color1         = config_GetInt ( p_aout, "spect-color" );
413
414     if( i_80_bands != 0)
415     {
416         xscale = xscale2;
417         i_nb_bands = 80;
418     }
419     else
420     {
421         xscale = xscale1;
422         i_nb_bands = 20;
423     }
424
425     if( !p_effect->p_data )
426     {
427         p_effect->p_data=(void *)malloc( 80 * sizeof(int) );
428         if( !p_effect->p_data )
429         {
430             free( p_s16_buff );
431             return -1;
432         }
433         peaks = (int *)p_effect->p_data;
434         for( i = 0 ; i < i_nb_bands ; i++ )
435         {
436            peaks[i] = 0;
437         }
438     }
439     else
440     {
441         peaks =(int *)p_effect->p_data;
442     }
443
444     height = (int *)malloc( i_nb_bands * sizeof(int) );
445     if( !height)
446     {
447         free( p_effect->p_data );
448         free( p_s16_buff );
449         return -1;
450     }
451
452     /* Convert the buffer to int16_t  */
453     /* Pasted from float32tos16.c */
454     for (i = p_buffer->i_nb_samples * p_effect->i_nb_chans; i--; )
455     {
456         union { float f; int32_t i; } u;
457         u.f = *p_buffl + 384.0;
458         if(u.i >  0x43c07fff ) * p_buffs = 32767;
459         else if ( u.i < 0x43bf8000 ) *p_buffs = -32768;
460         else *p_buffs = u.i - 0x43c00000;
461
462         p_buffl++ ; p_buffs++ ;
463     }
464     p_state  = visual_fft_init();
465     if( !p_state)
466     {
467         msg_Err(p_aout,"unable to initialize FFT transform");
468         free( height );
469         free( p_effect->p_data );
470         free( p_s16_buff );
471         return -1;
472     }
473     p_buffs = p_s16_buff;
474     for ( i = 0 ; i < FFT_BUFFER_SIZE; i++)
475     {
476         p_output[i]    = 0;
477         p_buffer1[i] = *p_buffs;
478
479         p_buffs += p_effect->i_nb_chans;
480         if( p_buffs >= &p_s16_buff[p_buffer->i_nb_samples * p_effect->i_nb_chans] )
481             p_buffs = p_s16_buff;
482     }
483     fft_perform( p_buffer1, p_output, p_state);
484     for(i = 0; i < FFT_BUFFER_SIZE; i++)
485     {
486         int sqrti = sqrt(p_output[i]);
487         p_dest[i] = sqrti >> 8;
488     }
489
490     i_nb_bands *= i_sections;
491
492     for ( i = 0 ; i< i_nb_bands/i_sections ;i++)
493     {
494         /* We search the maximum on one scale */
495         for( j = xscale[i] , y=0 ; j< xscale[ i + 1 ] ; j++ )
496         {
497             if ( p_dest[j] > y )
498                  y = p_dest[j];
499         }
500         /* Calculate the height of the bar */
501         y >>=7;/* remove some noise */
502         if( y != 0)
503         {
504             int logy = log(y);
505             height[i] = logy * y_scale;
506             if(height[i] > 150)
507                 height[i] = 150;
508         }
509         else
510         {
511             height[i] = 0 ;
512         }
513
514         /* Draw the bar now */
515         i_band_width = floor( p_effect->i_width / (i_nb_bands/i_sections)) ;
516
517         if( i_amp * height[i] > peaks[i])
518         {
519             peaks[i] = i_amp * height[i];
520         }
521         else if (peaks[i] > 0 )
522         {
523             peaks[i] -= PEAK_SPEED;
524             if( peaks[i] < i_amp * height[i] )
525             {
526                 peaks[i] = i_amp * height[i];
527             }
528             if( peaks[i] < 0 )
529             {
530                 peaks[i] = 0;
531             }
532         }
533
534         if( i_original != 0 )
535         {
536         if( peaks[i] > 0 && i_peak )
537         {
538             if( peaks[i] >= p_effect->i_height )
539                 peaks[i] = p_effect->i_height - 2;
540             i_line = peaks[i];
541
542             for( j = 0 ; j< i_band_width - i_separ; j++)
543             {
544                for( k = 0 ; k< 3 ; k ++)
545                {
546                    //* Draw the peak
547                      *(p_picture->p[0].p_pixels +
548                     (p_effect->i_height - i_line -1 -k ) *
549                      p_picture->p[0].i_pitch + (i_band_width*i +j) )
550                                     = 0xff;
551
552                     *(p_picture->p[1].p_pixels +
553                      ( ( p_effect->i_height - i_line ) / 2 -1 -k/2 ) *
554                      p_picture->p[1].i_pitch +
555                     ( ( i_band_width * i + j ) /2  ) )
556                                     = 0x00;
557
558                    if( 0x04 * (i_line + k ) - 0x0f > 0 )
559                    {
560                        if ( 0x04 * (i_line + k ) -0x0f < 0xff)
561                            *(p_picture->p[2].p_pixels  +
562                             ( ( p_effect->i_height - i_line ) / 2 - 1 -k/2 ) *
563                              p_picture->p[2].i_pitch +
564                              ( ( i_band_width * i + j ) /2  ) )
565                                     = ( 0x04 * ( i_line + k ) ) -0x0f ;
566                        else
567                            *(p_picture->p[2].p_pixels  +
568                             ( ( p_effect->i_height - i_line ) / 2 - 1 -k/2 ) *
569                              p_picture->p[2].i_pitch +
570                              ( ( i_band_width * i + j ) /2  ) )
571                                     = 0xff;
572                    }
573                    else
574                    {
575                         *(p_picture->p[2].p_pixels  +
576                          ( ( p_effect->i_height - i_line ) / 2 - 1 -k/2 ) *
577                          p_picture->p[2].i_pitch +
578                          ( ( i_band_width * i + j ) /2  ) )
579                                = 0x10 ;
580                    }
581                }
582             }
583         }
584         if(height[i] * i_amp > p_effect->i_height)
585             height[i] = floor(p_effect->i_height / i_amp );
586
587         for(i_line = 0 ; i_line < i_amp * height[i]; i_line ++ )
588         {
589             for( j = 0 ; j< i_band_width - i_separ ; j++)
590             {
591                *(p_picture->p[0].p_pixels +
592                  (p_effect->i_height - i_line -1) *
593                   p_picture->p[0].i_pitch + (i_band_width*i +j) ) = 0xff;
594
595                 *(p_picture->p[1].p_pixels +
596                  ( ( p_effect->i_height - i_line ) / 2 -1) *
597                  p_picture->p[1].i_pitch +
598                  ( ( i_band_width * i + j ) /2  ) ) = 0x00;
599
600                if( 0x04 * i_line - 0x0f > 0 )
601                {
602                     if( 0x04 * i_line - 0x0f < 0xff )
603                          *(p_picture->p[2].p_pixels  +
604                           ( ( p_effect->i_height - i_line ) / 2 - 1) *
605                            p_picture->p[2].i_pitch +
606                            ( ( i_band_width * i + j ) /2  ) ) =
607                                ( 0x04 * i_line) -0x0f ;
608                     else
609                          *(p_picture->p[2].p_pixels  +
610                           ( ( p_effect->i_height - i_line ) / 2 - 1) *
611                            p_picture->p[2].i_pitch +
612                            ( ( i_band_width * i + j ) /2  ) ) =
613                                        0xff;
614                }
615                else
616                {
617                     *(p_picture->p[2].p_pixels  +
618                      ( ( p_effect->i_height - i_line ) / 2 - 1) *
619                      p_picture->p[2].i_pitch +
620                      ( ( i_band_width * i + j ) /2  ) ) =
621                             0x10 ;
622                }
623             }
624         }
625         }
626     }
627
628     band_sep_angle = 360.0 / i_nb_bands;
629     section_sep_angle = 360.0 / i_sections;
630     if( i_peak_height < 1 )
631         i_peak_height = 1;
632     max_band_length = p_effect->i_height / 2 - ( i_rad + i_peak_height + 1 );
633
634     i_band_width = floor( 360 / i_nb_bands - i_separ );
635     if( i_band_width < 1 )
636         i_band_width = 1;
637
638     for( c = 0 ; c < i_sections ; c++ )
639     for( i = 0 ; i < (i_nb_bands / i_sections) ; i++ )
640     {
641         /* DO A PEAK */
642         if( peaks[i] > 0 && i_peak )
643         {
644             if( peaks[i] >= p_effect->i_height )
645                 peaks[i] = p_effect->i_height - 2;
646             i_line = peaks[i];
647
648             /* circular line pattern(so color blend is more visible) */
649             for( j = 0 ; j < i_peak_height ; j++ )
650             {
651                 //x = p_picture->p[0].i_pitch / 2;
652                 x = p_effect->i_width / 2;
653                 y = p_effect->i_height / 2;
654                 xx = x;
655                 yy = y;
656                 for( k = 0 ; k < (i_band_width + i_extra_width) ; k++ )
657                 {
658                     x = xx;
659                     y = yy;
660                     a = ( (i+1) * band_sep_angle + section_sep_angle * (c+1) + k )
661                         * 3.141592 / 180.0;
662                     x += (double)( cos(a) * (double)( i_line + j + i_rad ) );
663                     y += (double)( -sin(a) * (double)( i_line + j + i_rad ) );
664
665                     *(p_picture->p[0].p_pixels + x + y * p_picture->p[0].i_pitch
666                     ) = 255;/* Y(R,G,B); */
667
668                     x /= 2;
669                     y /= 2;
670
671                     *(p_picture->p[1].p_pixels + x + y * p_picture->p[1].i_pitch
672                     ) = 0;/* U(R,G,B); */
673
674                     if( 0x04 * (i_line + k ) - 0x0f > 0 )
675                     {
676                         if ( 0x04 * (i_line + k ) -0x0f < 0xff)
677                             *(p_picture->p[2].p_pixels + x + y * p_picture->p[2].i_pitch
678                             ) = ( 0x04 * ( i_line + k ) ) -(color1-1);/* -V(R,G,B); */
679                         else
680                             *(p_picture->p[2].p_pixels + x + y * p_picture->p[2].i_pitch
681                             ) = 255;/* V(R,G,B); */
682                     }
683                     else
684                     {
685                         *(p_picture->p[2].p_pixels + x + y * p_picture->p[2].i_pitch
686                         ) = color1;/* V(R,G,B); */
687                     }
688                 }
689             }
690         }
691
692         if( (height[i] * i_amp) > p_effect->i_height )
693             height[i] = floor( p_effect->i_height / i_amp );
694
695         /* DO BASE OF BAND (mostly makes a circle) */
696         if( i_show_base != 0 )
697         {
698             //x = p_picture->p[0].i_pitch / 2;
699             x = p_effect->i_width / 2;
700             y = p_effect->i_height / 2;
701
702             a =  ( (i+1) * band_sep_angle + section_sep_angle * (c+1) )
703                 * 3.141592 / 180.0;
704             x += (double)( cos(a) * (double)i_rad );/* newb-forceful casting */
705             y += (double)( -sin(a) * (double)i_rad );
706
707             *(p_picture->p[0].p_pixels + x + y * p_picture->p[0].i_pitch
708             ) = 255;/* Y(R,G,B); */
709
710             x /= 2;
711             y /= 2;
712
713             *(p_picture->p[1].p_pixels + x + y * p_picture->p[1].i_pitch
714             ) = 0;/* U(R,G,B); */
715
716             if( 0x04 * i_line - 0x0f > 0 )
717             {
718                 if( 0x04 * i_line -0x0f < 0xff)
719                     *(p_picture->p[2].p_pixels + x + y * p_picture->p[2].i_pitch
720                     ) = ( 0x04 * i_line) -(color1-1);/* -V(R,G,B); */
721                 else
722                     *(p_picture->p[2].p_pixels + x + y * p_picture->p[2].i_pitch
723                     ) = 255;/* V(R,G,B); */
724             }
725             else
726             {
727                 *(p_picture->p[2].p_pixels + x + y * p_picture->p[2].i_pitch
728                 ) = color1;/* V(R,G,B); */
729             }
730         }
731
732         /* DO A BAND */
733         if( i_show_bands != 0 )
734         for( j = 0 ; j < i_band_width ; j++ )
735         {
736             x = p_effect->i_width / 2;
737             y = p_effect->i_height / 2;
738             xx = x;
739             yy = y;
740             a = ( (i+1) * band_sep_angle + section_sep_angle * (c+1) + j )
741                 * 3.141592/180.0;
742
743             for( k = (i_rad+1) ; k < max_band_length ; k++ )
744             {
745                 if( (k-i_rad) > height[i] )
746                     break;/* uhh.. */
747
748                 x = xx;
749                 y = yy;
750                 x += (double)( cos(a) * (double)k );/* newbed! */
751                 y += (double)( -sin(a) * (double)k );
752
753                 *(p_picture->p[0].p_pixels + x + y * p_picture->p[0].i_pitch
754                 ) = 255;
755
756                 x /= 2;
757                 y /= 2;
758
759                 *(p_picture->p[1].p_pixels + x + y * p_picture->p[1].i_pitch
760                 ) = 0;
761
762                 if( 0x04 * i_line - 0x0f > 0 )
763                 {
764                     if ( 0x04 * i_line -0x0f < 0xff)
765                         *(p_picture->p[2].p_pixels + x + y * p_picture->p[2].i_pitch
766                         ) = ( 0x04 * i_line) -(color1-1);
767                     else
768                         *(p_picture->p[2].p_pixels + x + y * p_picture->p[2].i_pitch
769                         ) = 255;
770                 }
771                 else
772                 {
773                     *(p_picture->p[2].p_pixels + x + y * p_picture->p[2].i_pitch
774                     ) = color1;
775                 }
776             }
777         }
778     }
779
780     fft_close( p_state );
781
782     free( p_s16_buff );
783     free( height );
784
785     return 0;
786 }
787
788
789 /*****************************************************************************
790  * scope_Run: scope effect
791  *****************************************************************************/
792 int scope_Run(visual_effect_t * p_effect, vlc_object_t *p_aout,
793               const block_t * p_buffer , picture_t * p_picture)
794 {
795     VLC_UNUSED(p_aout);
796
797     int i_index;
798     float *p_sample ;
799     uint8_t *ppp_area[2][3];
800
801     for( i_index = 0 ; i_index < 2 ; i_index++ )
802     {
803         for( int j = 0 ; j < 3 ; j++ )
804         {
805             ppp_area[i_index][j] =
806                 p_picture->p[j].p_pixels + i_index * p_picture->p[j].i_lines
807                 / 2 * p_picture->p[j].i_pitch;
808         }
809     }
810
811     for( i_index = 0, p_sample = (float *)p_buffer->p_buffer;
812             i_index < __MIN( p_effect->i_width, (int)p_buffer->i_nb_samples );
813             i_index++ )
814     {
815         uint8_t i_value;
816
817         /* Left channel */
818         i_value =  p_sample[p_effect->i_idx_left] * 127;
819         *(ppp_area[0][0]
820                 + p_picture->p[0].i_pitch * i_index / p_effect->i_width
821                 + p_picture->p[0].i_lines * i_value / 512
822                 * p_picture->p[0].i_pitch) = 0xbf;
823         *(ppp_area[0][1]
824                 + p_picture->p[1].i_pitch * i_index / p_effect->i_width
825                 + p_picture->p[1].i_lines * i_value / 512
826                 * p_picture->p[1].i_pitch) = 0xff;
827
828
829         /* Right channel */
830         i_value = p_sample[p_effect->i_idx_right] * 127;
831         *(ppp_area[1][0]
832                 + p_picture->p[0].i_pitch * i_index / p_effect->i_width
833                 + p_picture->p[0].i_lines * i_value / 512
834                 * p_picture->p[0].i_pitch) = 0x9f;
835         *(ppp_area[1][2]
836                 + p_picture->p[2].i_pitch * i_index / p_effect->i_width
837                 + p_picture->p[2].i_lines * i_value / 512
838                 * p_picture->p[2].i_pitch) = 0xdd;
839
840         p_sample += p_effect->i_nb_chans;
841     }
842     return 0;
843 }
844
845
846 /*****************************************************************************
847  * vuMeter_Run: vu meter effect
848  *****************************************************************************/
849 int vuMeter_Run(visual_effect_t * p_effect, vlc_object_t *p_aout,
850                 const block_t * p_buffer , picture_t * p_picture)
851 {
852     VLC_UNUSED(p_aout);
853     int j;
854     float i_value_l = 0;
855     float i_value_r = 0;
856
857     /* Compute the peack values */
858     for ( unsigned i = 0 ; i < p_buffer->i_nb_samples; i++ )
859     {
860         const float *p_sample = (float *)p_buffer->p_buffer;
861         float ch;
862
863         ch = p_sample[p_effect->i_idx_left] * 256;
864         if (ch > i_value_l)
865             i_value_l = ch;
866
867         ch = p_sample[p_effect->i_idx_right] * 256;
868         if (ch > i_value_r)
869             i_value_r = ch;
870
871         p_sample += p_effect->i_nb_chans;
872     }
873
874     i_value_l = abs(i_value_l);
875     i_value_r = abs(i_value_r);
876
877     /* Stay under maximum value admited */
878     if ( i_value_l > 200 * M_PI_2 )
879         i_value_l = 200 * M_PI_2;
880     if ( i_value_r > 200 * M_PI_2 )
881         i_value_r = 200 * M_PI_2;
882
883     float *i_value;
884
885     if( !p_effect->p_data )
886     {
887         /* Allocate memory to save hand positions */
888         p_effect->p_data = (void *)malloc( 2 * sizeof(float) );
889         i_value = p_effect->p_data;
890         i_value[0] = i_value_l;
891         i_value[1] = i_value_r;
892     }
893     else
894     {
895         /* Make the hands go down slowly if the current values are slower
896            than the previous */
897         i_value = p_effect->p_data;
898
899         if ( i_value_l > i_value[0] - 6 )
900             i_value[0] = i_value_l;
901         else
902             i_value[0] = i_value[0] - 6;
903
904         if ( i_value_r > i_value[1] - 6 )
905             i_value[1] = i_value_r;
906         else
907             i_value[1] = i_value[1] - 6;
908     }
909
910     int x, y, k;
911     float teta;
912     float teta_grad;
913
914     for ( j = 0; j < 2; j++ )
915     {
916         /* Draw the two scales */
917         k = 0;
918         teta_grad = GRAD_ANGLE_MIN;
919         for ( teta = -M_PI_4; teta <= M_PI_4; teta = teta + 0.003 )
920         {
921             for ( unsigned i = 140; i <= 150; i++ )
922             {
923                 y = i * cos(teta) + 20;
924                 x = i * sin(teta) + 150 + 240 * j;
925                 /* Compute the last color for the gradation */
926                 if (teta >= teta_grad + GRAD_INCR && teta_grad <= GRAD_ANGLE_MAX)
927                 {
928                     teta_grad = teta_grad + GRAD_INCR;
929                     k = k + 5;
930                 }
931                 *(p_picture->p[0].p_pixels +
932                         (p_picture->p[0].i_lines - y - 1 ) * p_picture->p[0].i_pitch
933                         + x ) = 0x45;
934                 *(p_picture->p[1].p_pixels +
935                         (p_picture->p[1].i_lines - y / 2 - 1 ) * p_picture->p[1].i_pitch
936                         + x / 2 ) = 0x0;
937                 *(p_picture->p[2].p_pixels +
938                         (p_picture->p[2].i_lines - y / 2 - 1 ) * p_picture->p[2].i_pitch
939                         + x / 2 ) = 0x4D + k;
940             }
941         }
942
943         /* Draw the two hands */
944         teta = (float)i_value[j] / 200 - M_PI_4;
945         for ( int i = 0; i <= 150; i++ )
946         {
947             y = i * cos(teta) + 20;
948             x = i * sin(teta) + 150 + 240 * j;
949             *(p_picture->p[0].p_pixels +
950                     (p_picture->p[0].i_lines - y - 1 ) * p_picture->p[0].i_pitch
951                     + x ) = 0xAD;
952             *(p_picture->p[1].p_pixels +
953                     (p_picture->p[1].i_lines - y / 2 - 1 ) * p_picture->p[1].i_pitch
954                     + x / 2 ) = 0xFC;
955             *(p_picture->p[2].p_pixels +
956                     (p_picture->p[2].i_lines - y / 2 - 1 ) * p_picture->p[2].i_pitch
957                     + x / 2 ) = 0xAC;
958         }
959
960         /* Draw the hand bases */
961         for ( teta = -M_PI_2; teta <= M_PI_2 + 0.01; teta = teta + 0.003 )
962         {
963             for ( int i = 0; i < 10; i++ )
964             {
965                 y = i * cos(teta) + 20;
966                 x = i * sin(teta) + 150 + 240 * j;
967                 *(p_picture->p[0].p_pixels +
968                         (p_picture->p[0].i_lines - y - 1 ) * p_picture->p[0].i_pitch
969                         + x ) = 0xFF;
970                 *(p_picture->p[1].p_pixels +
971                         (p_picture->p[1].i_lines - y / 2 - 1 ) * p_picture->p[1].i_pitch
972                         + x / 2 ) = 0x80;
973                 *(p_picture->p[2].p_pixels +
974                         (p_picture->p[2].i_lines - y / 2 - 1 ) * p_picture->p[2].i_pitch
975                         + x / 2 ) = 0x80;
976             }
977         }
978
979     }
980
981     return 0;
982 }