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