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