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