* modules/visualization/visual/:
[vlc.git] / modules / visualization / visual / effects.c
1 /*****************************************************************************
2  * effects.c : Effects for the visualization system
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: effects.c,v 1.9 2003/10/24 17:43:51 sam Exp $
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <vlc/vlc.h>
29 #include <vlc/vout.h>
30 #include "audio_output.h"
31 #include "aout_internal.h"
32
33 #include "visual.h"
34 #include <math.h>
35
36 #include "fft.h"
37
38 #define PEAK_SPEED 1
39
40 /*****************************************************************************
41  * dummy_Run
42  *****************************************************************************/
43 int dummy_Run( visual_effect_t * p_effect, aout_instance_t *p_aout,
44                aout_buffer_t * p_buffer , picture_t * p_picture)
45 {
46     return 0;
47 }
48
49 /*****************************************************************************
50  * spectrum_Run: spectrum analyser
51  *****************************************************************************/
52 int spectrum_Run(visual_effect_t * p_effect, aout_instance_t *p_aout,
53                  aout_buffer_t * p_buffer , picture_t * p_picture)
54 {
55     float p_output[FFT_BUFFER_SIZE];  /* Raw FFT Result  */
56     int *height;                      /* Bar heights */
57     int *peaks;                       /* Peaks */
58     int i_nb_bands;                   /* number of bands */
59     int i_band_width;                 /* width of bands */
60     int i_separ;                      /* Should we let blanks ? */
61     int i_amp;                        /* Vertical amplification */
62     int i_peak;                       /* Should we draw peaks ? */
63     char *psz_parse = NULL;           /* Args line */
64
65     /* Horizontal scale for 20-band equalizer */
66     const int xscale1[]={0,1,2,3,4,5,6,7,8,11,15,20,27,
67                         36,47,62,82,107,141,184,255};
68
69     /* Horizontal scale for 80-band equalizer */
70     const int xscale2[] =
71     {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,
72      19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,
73      35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
74      52,53,54,55,56,57,58,59,61,63,67,72,77,82,87,93,99,105,
75      110,115,121,130,141,152,163,174,185,200,255};
76     const int *xscale;
77     const double y_scale =  3.60673760222;  /* (log 256) */
78
79     fft_state *p_state;                 /* internal FFT data */
80
81     int i , j , y , k;
82     int i_line;
83     int16_t p_dest[FFT_BUFFER_SIZE];      /* Adapted FFT result */
84     int16_t p_buffer1[FFT_BUFFER_SIZE];   /* Buffer on which we perform
85                                              the FFT (first channel) */
86
87     float *p_buffl =                     /* Original buffer */
88             (float*)p_buffer->p_buffer;
89
90     int16_t  *p_buffs;                    /* int16_t converted buffer */
91     int16_t  *p_s16_buff = NULL;                /* int16_t converted buffer */
92
93     p_s16_buff = (int16_t*)malloc(
94               p_buffer->i_nb_samples * p_effect->i_nb_chans * sizeof(int16_t));
95
96     if( !p_s16_buff )
97     {
98         msg_Err(p_aout,"Out of memory");
99         return -1;
100     }
101
102     p_buffs = p_s16_buff;
103     i_nb_bands = config_GetInt ( p_aout, "visual-nbbands" );
104     i_separ    = config_GetInt( p_aout, "visual-separ" );
105     i_amp     = config_GetInt ( p_aout, "visual-amp" );
106     i_peak     = config_GetInt ( p_aout, "visual-peaks" );
107
108     if( i_nb_bands == 20)
109     {
110         xscale = xscale1;
111     }
112     else
113     {
114         i_nb_bands = 80;
115         xscale = xscale2;
116     }
117
118     if( !p_effect->p_data )
119     {
120         p_effect->p_data=(void *)malloc(i_nb_bands * sizeof(int) );
121         if( !p_effect->p_data)
122         {
123             msg_Err(p_aout,"Out of memory");
124             return -1;
125         }
126         peaks = (int *)p_effect->p_data;
127         for( i = 0 ; i < i_nb_bands ; i++)
128         {
129            peaks[i] = 0;
130         }
131
132     }
133     else
134     {
135         peaks =(int *)p_effect->p_data;
136     }
137
138
139     height = (int *)malloc( i_nb_bands * sizeof(int) );
140     if( !height)
141     {
142         msg_Err(p_aout,"Out of memory");
143         return -1;
144     }
145     /* Convert the buffer to int16_t  */
146     /* Pasted from float32tos16.c */
147     for (i = p_buffer->i_nb_samples * p_effect->i_nb_chans; i--; )
148     {
149         float f_in = *p_buffl + 384.0;
150         int32_t i_in;
151         i_in = *(int32_t *)&f_in;
152         if(i_in >  0x43c07fff ) * p_buffs = 32767;
153         else if ( i_in < 0x43bf8000 ) *p_buffs = -32768;
154         else *p_buffs = i_in - 0x43c00000;
155
156         p_buffl++ ; p_buffs++ ;
157     }
158     p_state  = visual_fft_init();
159     if( !p_state)
160     {
161         msg_Err(p_aout,"Unable to initialize FFT transform");
162         return -1;
163     }
164     p_buffs = p_s16_buff;
165     for ( i = 0 ; i < FFT_BUFFER_SIZE ; i++)
166     {
167         p_output[i]    = 0;
168         p_buffer1[i] = *p_buffs;
169         p_buffs      = p_buffs + p_effect->i_nb_chans;
170     }
171     fft_perform( p_buffer1, p_output, p_state);
172     for(i= 0; i< FFT_BUFFER_SIZE ; i++ )
173         p_dest[i] = ( (int) sqrt( p_output [ i + 1 ] ) ) >> 8;
174
175     for ( i = 0 ; i< i_nb_bands ;i++)
176     {
177         /* We search the maximum on one scale */
178         for( j = xscale[i] , y=0 ; j< xscale[ i + 1 ] ; j++ )
179         {
180             if ( p_dest[j] > y )
181                  y = p_dest[j];
182         }
183         /* Calculate the height of the bar */
184         y >>=7;/* remove some noise */
185         if( y != 0)
186         {
187             height[i] = (int)log(y)* y_scale;
188                if(height[i] > 150)
189                   height[i] = 150;
190         }
191         else
192         {
193             height[i] = 0 ;
194         }
195
196         /* Draw the bar now */
197         i_band_width = floor( p_effect->i_width / i_nb_bands) ;
198
199         if( i_amp * height[i] > peaks[i])
200         {
201             peaks[i] = i_amp * height[i];
202         }
203         else if (peaks[i] > 0 )
204         {
205             peaks[i] -= PEAK_SPEED;
206             if( peaks[i] < i_amp * height[i] )
207             {
208                 peaks[i] = i_amp * height[i];
209             }
210             if( peaks[i] < 0 )
211             {
212                 peaks[i] = 0;
213             }
214         }
215
216         if( peaks[i] > 0 && i_peak )
217         {
218             if( peaks[i] >= p_effect->i_height )
219                 peaks[i] = p_effect->i_height - 2;
220             i_line = peaks[i];
221
222             for( j = 0 ; j< i_band_width - i_separ; j++)
223             {
224                for( k = 0 ; k< 3 ; k ++)
225                {
226                    /* Draw the peak */
227                      *(p_picture->p[0].p_pixels +
228                     (p_picture->p[0].i_lines - i_line -1 -k ) *
229                      p_picture->p[0].i_pitch + (i_band_width*i +j) )
230                                     = 0xff;
231
232                     *(p_picture->p[1].p_pixels +
233                      (p_picture->p[1].i_lines - i_line /2 -1 -k/2 ) *
234                      p_picture->p[1].i_pitch +
235                     ( ( i_band_width * i + j ) /2  ) )
236                                     = 0x00;
237
238                    if( 0x04 * (i_line + k ) - 0x0f > 0 )
239                    {
240                        if ( 0x04 * (i_line + k ) -0x0f < 0xff)
241                            *(p_picture->p[2].p_pixels  +
242                             (p_picture->p[2].i_lines - i_line /2 - 1 -k/2 ) *
243                              p_picture->p[2].i_pitch +
244                              ( ( i_band_width * i + j ) /2  ) )
245                                     = ( 0x04 * ( i_line + k ) ) -0x0f ;
246                        else
247                            *(p_picture->p[2].p_pixels  +
248                             (p_picture->p[2].i_lines - i_line /2 - 1 -k/2 ) *
249                              p_picture->p[2].i_pitch +
250                              ( ( i_band_width * i + j ) /2  ) )
251                                     = 0xff; 
252                    }
253                    else
254                    {
255                         *(p_picture->p[2].p_pixels  +
256                          (p_picture->p[2].i_lines - i_line /2 - 1 -k/2 ) *
257                          p_picture->p[2].i_pitch + 
258                          ( ( i_band_width * i + j ) /2  ) ) 
259                                = 0x10 ;
260                    }
261                }
262             } 
263         }
264     
265         if(height[i] * i_amp > p_effect->i_height)
266             height[i] = floor(p_effect->i_height / i_amp );
267
268         for(i_line = 0 ; i_line < i_amp * height[i]; i_line ++ )
269         {
270             for( j = 0 ; j< i_band_width - i_separ ; j++)
271             {
272                *(p_picture->p[0].p_pixels +
273                  (p_picture->p[0].i_lines - i_line -1) *
274                   p_picture->p[0].i_pitch + (i_band_width*i +j) ) = 0xff;
275
276                 *(p_picture->p[1].p_pixels +
277                  (p_picture->p[1].i_lines - i_line /2 -1) *
278                  p_picture->p[1].i_pitch +
279                  ( ( i_band_width * i + j ) /2  ) ) = 0x00;
280
281                
282                if( 0x04 * i_line - 0x0f > 0 )
283                {
284                     if( 0x04 * i_line - 0x0f < 0xff )
285                          *(p_picture->p[2].p_pixels  +
286                           (p_picture->p[2].i_lines - i_line /2 - 1) *
287                            p_picture->p[2].i_pitch + 
288                            ( ( i_band_width * i + j ) /2  ) ) = 
289                                ( 0x04 * i_line) -0x0f ;
290                     else
291                          *(p_picture->p[2].p_pixels  +
292                           (p_picture->p[2].i_lines - i_line /2 - 1) *
293                            p_picture->p[2].i_pitch + 
294                            ( ( i_band_width * i + j ) /2  ) ) = 
295                                        0xff;
296                }
297                else
298                {
299                     *(p_picture->p[2].p_pixels  +
300                      (p_picture->p[2].i_lines - i_line /2 - 1) *
301                      p_picture->p[2].i_pitch + 
302                      ( ( i_band_width * i + j ) /2  ) ) = 
303                             0x10 ;
304                }
305             }
306         }
307     }
308        
309     fft_close( p_state );
310    
311     if( p_s16_buff != NULL ) 
312     {
313         free( p_s16_buff );
314         p_s16_buff = NULL;
315     }
316    
317     if(height) free(height);
318    
319     if(psz_parse) free(psz_parse);
320     
321     return 0;
322 }
323
324  
325 /*****************************************************************************
326  * scope_Run: scope effect
327  *****************************************************************************/
328 int scope_Run(visual_effect_t * p_effect, aout_instance_t *p_aout,
329               aout_buffer_t * p_buffer , picture_t * p_picture)
330 {
331     int i_index;
332     float *p_sample ;
333     uint8_t *ppp_area[2][3];
334
335
336         for( i_index = 0 ; i_index < 2 ; i_index++ )
337         {
338             int j;
339             for( j = 0 ; j < 3 ; j++ )
340             {
341                 ppp_area[i_index][j] =
342                     p_picture->p[j].p_pixels + i_index * p_picture->p[j].i_lines
343                                 / 2 * p_picture->p[j].i_pitch;
344             }
345         }
346
347         for( i_index = 0, p_sample = (float *)p_buffer->p_buffer;
348              i_index < p_effect->i_width;
349              i_index++ )
350         {
351             uint8_t i_value;
352
353             /* Left channel */
354             i_value =  (*p_sample++ +1) * 127;
355             *(ppp_area[0][0]
356                + p_picture->p[0].i_pitch * i_index / p_effect->i_width
357                + p_picture->p[0].i_lines * i_value / 512
358                    * p_picture->p[0].i_pitch) = 0xbf;
359             *(ppp_area[0][1]
360                 + p_picture->p[1].i_pitch * i_index / p_effect->i_width
361                 + p_picture->p[1].i_lines * i_value / 512
362                    * p_picture->p[1].i_pitch) = 0xff;
363
364
365            /* Right channel */
366            i_value = ( *p_sample++ +1 ) * 127;
367            *(ppp_area[1][0]
368               + p_picture->p[0].i_pitch * i_index / p_effect->i_width
369               + p_picture->p[0].i_lines * i_value / 512
370                  * p_picture->p[0].i_pitch) = 0x9f;
371            *(ppp_area[1][2]
372               + p_picture->p[2].i_pitch * i_index / p_effect->i_width
373               + p_picture->p[2].i_lines * i_value / 512
374                 * p_picture->p[2].i_pitch) = 0xdd;
375         }
376         return 0;
377 }
378
379
380 /*****************************************************************************
381  * random_Run:  random plots display effect
382  *****************************************************************************/
383 int random_Run(visual_effect_t * p_effect, aout_instance_t *p_aout,
384               aout_buffer_t * p_buffer , picture_t * p_picture)
385 {
386     int i_nb_plots;
387     char *psz_parse= NULL;
388     int i , i_y , i_u , i_v;
389     int i_position;
390     srand((unsigned int)mdate());
391
392     if( p_effect->psz_args )
393     {
394         psz_parse = strdup( p_effect->psz_args );
395         i_nb_plots = config_GetInt ( p_aout, "visual-stars" );
396     }
397     else
398     {
399         i_nb_plots = 200;
400     }
401
402     for( i = 0 ; i < i_nb_plots ; i++ )
403     {
404         i_position = rand() % (p_effect->i_width * p_effect->i_height );
405         i_y = rand() % 256;
406         i_u = rand() % 256;
407         i_v = rand() % 256;
408         *(p_picture->p[0].p_pixels + i_position )= i_u;
409         *(p_picture->p[1].p_pixels + i_position/4) = i_v;
410         *(p_picture->p[2].p_pixels + i_position/4) = i_y;
411     }
412     return 0;
413 }
414
415 /*****************************************************************************
416  * blur_Run:  blur effect
417  *****************************************************************************/
418 #if 0 
419   /* This code is totally crappy */
420 int blur_Run(visual_effect_t * p_effect, aout_instance_t *p_aout,
421               aout_buffer_t * p_buffer , picture_t * p_picture)
422 {
423     uint8_t * p_pictures;
424     int i,j; 
425     int i_size;   /* Total size of one image */
426     
427     i_size = (p_picture->p[0].i_pitch * p_picture->p[0].i_lines +
428               p_picture->p[1].i_pitch * p_picture->p[1].i_lines +
429               p_picture->p[2].i_pitch * p_picture->p[2].i_lines );
430     
431     if( !p_effect->p_data )
432     {
433         p_effect->p_data=(void *)malloc( 5 * i_size *sizeof(uint8_t));
434         
435         if( !p_effect->p_data)
436         {
437             msg_Err(p_aout,"Out of memory");
438             return -1;
439         }
440         p_pictures = (uint8_t *)p_effect->p_data;
441     }
442     else
443     {
444         p_pictures =(uint8_t *)p_effect->p_data;
445     }
446
447     for( i = 0 ; i < 5 ; i++)
448     {
449         for ( j = 0 ; j< p_picture->p[0].i_pitch * p_picture->p[0].i_lines; i++)
450             p_picture->p[0].p_pixels[j] = 
451                     p_pictures[i * i_size + j] * (100 - 20 * i) /100 ;
452         for ( j = 0 ; j< p_picture->p[1].i_pitch * p_picture->p[1].i_lines; i++)
453             p_picture->p[1].p_pixels[j] = 
454                     p_pictures[i * i_size +
455                     p_picture->p[0].i_pitch * p_picture->p[0].i_lines + j ];
456         for ( j = 0 ; j< p_picture->p[2].i_pitch * p_picture->p[2].i_lines; i++)
457             p_picture->p[2].p_pixels[j] = 
458                     p_pictures[i * i_size +
459                     p_picture->p[0].i_pitch * p_picture->p[0].i_lines +
460                     p_picture->p[1].i_pitch * p_picture->p[1].i_lines 
461                     + j ];
462     }
463
464     memcpy ( &p_pictures[ i_size ] , &p_pictures[0] , 4 * i_size * sizeof(uint8_t) );
465 }
466 #endif