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