]> git.sesse.net Git - vlc/blob - modules/visualization/visual/effects.c
* modules/codec/ffmpeg/video_filter.c: resampling fix for when source or dest is...
[vlc] / modules / visualization / visual / effects.c
1 /*****************************************************************************
2  * effects.c : Effects for the visualization system
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id$
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         union { float f; int32_t i; } u;
150         u.f = *p_buffl + 384.0;
151         if(u.i >  0x43c07fff ) * p_buffs = 32767;
152         else if ( u.i < 0x43bf8000 ) *p_buffs = -32768;
153         else *p_buffs = u.i - 0x43c00000;
154
155         p_buffl++ ; p_buffs++ ;
156     }
157     p_state  = visual_fft_init();
158     if( !p_state)
159     {
160         msg_Err(p_aout,"Unable to initialize FFT transform");
161         return -1;
162     }
163     p_buffs = p_s16_buff;
164     for ( i = 0 ; i < FFT_BUFFER_SIZE ; i++)
165     {
166         p_output[i]    = 0;
167         p_buffer1[i] = *p_buffs;
168         p_buffs      = p_buffs + p_effect->i_nb_chans;
169     }
170     fft_perform( p_buffer1, p_output, p_state);
171     for(i= 0; i< FFT_BUFFER_SIZE ; i++ )
172         p_dest[i] = ( (int) sqrt( p_output [ i + 1 ] ) ) >> 8;
173
174     for ( i = 0 ; i< i_nb_bands ;i++)
175     {
176         /* We search the maximum on one scale */
177         for( j = xscale[i] , y=0 ; j< xscale[ i + 1 ] ; j++ )
178         {
179             if ( p_dest[j] > y )
180                  y = p_dest[j];
181         }
182         /* Calculate the height of the bar */
183         y >>=7;/* remove some noise */
184         if( y != 0)
185         {
186             height[i] = (int)log(y)* y_scale;
187                if(height[i] > 150)
188                   height[i] = 150;
189         }
190         else
191         {
192             height[i] = 0 ;
193         }
194
195         /* Draw the bar now */
196         i_band_width = floor( p_effect->i_width / i_nb_bands) ;
197
198         if( i_amp * height[i] > peaks[i])
199         {
200             peaks[i] = i_amp * height[i];
201         }
202         else if (peaks[i] > 0 )
203         {
204             peaks[i] -= PEAK_SPEED;
205             if( peaks[i] < i_amp * height[i] )
206             {
207                 peaks[i] = i_amp * height[i];
208             }
209             if( peaks[i] < 0 )
210             {
211                 peaks[i] = 0;
212             }
213         }
214
215         if( peaks[i] > 0 && i_peak )
216         {
217             if( peaks[i] >= p_effect->i_height )
218                 peaks[i] = p_effect->i_height - 2;
219             i_line = peaks[i];
220
221             for( j = 0 ; j< i_band_width - i_separ; j++)
222             {
223                for( k = 0 ; k< 3 ; k ++)
224                {
225                    /* Draw the peak */
226                      *(p_picture->p[0].p_pixels +
227                     (p_picture->p[0].i_lines - i_line -1 -k ) *
228                      p_picture->p[0].i_pitch + (i_band_width*i +j) )
229                                     = 0xff;
230
231                     *(p_picture->p[1].p_pixels +
232                      (p_picture->p[1].i_lines - i_line /2 -1 -k/2 ) *
233                      p_picture->p[1].i_pitch +
234                     ( ( i_band_width * i + j ) /2  ) )
235                                     = 0x00;
236
237                    if( 0x04 * (i_line + k ) - 0x0f > 0 )
238                    {
239                        if ( 0x04 * (i_line + k ) -0x0f < 0xff)
240                            *(p_picture->p[2].p_pixels  +
241                             (p_picture->p[2].i_lines - i_line /2 - 1 -k/2 ) *
242                              p_picture->p[2].i_pitch +
243                              ( ( i_band_width * i + j ) /2  ) )
244                                     = ( 0x04 * ( i_line + k ) ) -0x0f ;
245                        else
246                            *(p_picture->p[2].p_pixels  +
247                             (p_picture->p[2].i_lines - i_line /2 - 1 -k/2 ) *
248                              p_picture->p[2].i_pitch +
249                              ( ( i_band_width * i + j ) /2  ) )
250                                     = 0xff;
251                    }
252                    else
253                    {
254                         *(p_picture->p[2].p_pixels  +
255                          (p_picture->p[2].i_lines - i_line /2 - 1 -k/2 ) *
256                          p_picture->p[2].i_pitch +
257                          ( ( i_band_width * i + j ) /2  ) )
258                                = 0x10 ;
259                    }
260                }
261             }
262         }
263
264         if(height[i] * i_amp > p_effect->i_height)
265             height[i] = floor(p_effect->i_height / i_amp );
266
267         for(i_line = 0 ; i_line < i_amp * height[i]; i_line ++ )
268         {
269             for( j = 0 ; j< i_band_width - i_separ ; j++)
270             {
271                *(p_picture->p[0].p_pixels +
272                  (p_picture->p[0].i_lines - i_line -1) *
273                   p_picture->p[0].i_pitch + (i_band_width*i +j) ) = 0xff;
274
275                 *(p_picture->p[1].p_pixels +
276                  (p_picture->p[1].i_lines - i_line /2 -1) *
277                  p_picture->p[1].i_pitch +
278                  ( ( i_band_width * i + j ) /2  ) ) = 0x00;
279
280                if( 0x04 * i_line - 0x0f > 0 )
281                {
282                     if( 0x04 * i_line - 0x0f < 0xff )
283                          *(p_picture->p[2].p_pixels  +
284                           (p_picture->p[2].i_lines - i_line /2 - 1) *
285                            p_picture->p[2].i_pitch +
286                            ( ( i_band_width * i + j ) /2  ) ) =
287                                ( 0x04 * i_line) -0x0f ;
288                     else
289                          *(p_picture->p[2].p_pixels  +
290                           (p_picture->p[2].i_lines - i_line /2 - 1) *
291                            p_picture->p[2].i_pitch +
292                            ( ( i_band_width * i + j ) /2  ) ) =
293                                        0xff;
294                }
295                else
296                {
297                     *(p_picture->p[2].p_pixels  +
298                      (p_picture->p[2].i_lines - i_line /2 - 1) *
299                      p_picture->p[2].i_pitch +
300                      ( ( i_band_width * i + j ) /2  ) ) =
301                             0x10 ;
302                }
303             }
304         }
305     }
306
307     fft_close( p_state );
308
309     if( p_s16_buff != NULL )
310     {
311         free( p_s16_buff );
312         p_s16_buff = NULL;
313     }
314
315     if(height) free(height);
316
317     if(psz_parse) free(psz_parse);
318
319     return 0;
320 }
321
322
323 /*****************************************************************************
324  * scope_Run: scope effect
325  *****************************************************************************/
326 int scope_Run(visual_effect_t * p_effect, aout_instance_t *p_aout,
327               aout_buffer_t * p_buffer , picture_t * p_picture)
328 {
329     int i_index;
330     float *p_sample ;
331     uint8_t *ppp_area[2][3];
332
333
334         for( i_index = 0 ; i_index < 2 ; i_index++ )
335         {
336             int j;
337             for( j = 0 ; j < 3 ; j++ )
338             {
339                 ppp_area[i_index][j] =
340                     p_picture->p[j].p_pixels + i_index * p_picture->p[j].i_lines
341                                 / 2 * p_picture->p[j].i_pitch;
342             }
343         }
344
345         for( i_index = 0, p_sample = (float *)p_buffer->p_buffer;
346              i_index < p_effect->i_width;
347              i_index++ )
348         {
349             uint8_t i_value;
350
351             /* Left channel */
352             i_value =  (*p_sample++ +1) * 127;
353             *(ppp_area[0][0]
354                + p_picture->p[0].i_pitch * i_index / p_effect->i_width
355                + p_picture->p[0].i_lines * i_value / 512
356                    * p_picture->p[0].i_pitch) = 0xbf;
357             *(ppp_area[0][1]
358                 + p_picture->p[1].i_pitch * i_index / p_effect->i_width
359                 + p_picture->p[1].i_lines * i_value / 512
360                    * p_picture->p[1].i_pitch) = 0xff;
361
362
363            /* Right channel */
364            i_value = ( *p_sample++ +1 ) * 127;
365            *(ppp_area[1][0]
366               + p_picture->p[0].i_pitch * i_index / p_effect->i_width
367               + p_picture->p[0].i_lines * i_value / 512
368                  * p_picture->p[0].i_pitch) = 0x9f;
369            *(ppp_area[1][2]
370               + p_picture->p[2].i_pitch * i_index / p_effect->i_width
371               + p_picture->p[2].i_lines * i_value / 512
372                 * p_picture->p[2].i_pitch) = 0xdd;
373         }
374         return 0;
375 }
376
377
378 /*****************************************************************************
379  * random_Run:  random plots display effect
380  *****************************************************************************/
381 int random_Run(visual_effect_t * p_effect, aout_instance_t *p_aout,
382               aout_buffer_t * p_buffer , picture_t * p_picture)
383 {
384     int i_nb_plots;
385     char *psz_parse= NULL;
386     int i , i_y , i_u , i_v;
387     int i_position;
388     srand((unsigned int)mdate());
389
390     if( p_effect->psz_args )
391     {
392         psz_parse = strdup( p_effect->psz_args );
393         i_nb_plots = config_GetInt ( p_aout, "visual-stars" );
394     }
395     else
396     {
397         i_nb_plots = 200;
398     }
399
400     for( i = 0 ; i < i_nb_plots ; i++ )
401     {
402         i_position = rand() % (p_effect->i_width * p_effect->i_height );
403         i_y = rand() % 256;
404         i_u = rand() % 256;
405         i_v = rand() % 256;
406         *(p_picture->p[0].p_pixels + i_position )= i_u;
407         *(p_picture->p[1].p_pixels + i_position/4) = i_v;
408         *(p_picture->p[2].p_pixels + i_position/4) = i_y;
409     }
410     return 0;
411 }
412
413 /*****************************************************************************
414  * blur_Run:  blur effect
415  *****************************************************************************/
416 #if 0
417   /* This code is totally crappy */
418 int blur_Run(visual_effect_t * p_effect, aout_instance_t *p_aout,
419               aout_buffer_t * p_buffer , picture_t * p_picture)
420 {
421     uint8_t * p_pictures;
422     int i,j;
423     int i_size;   /* Total size of one image */
424
425     i_size = (p_picture->p[0].i_pitch * p_picture->p[0].i_lines +
426               p_picture->p[1].i_pitch * p_picture->p[1].i_lines +
427               p_picture->p[2].i_pitch * p_picture->p[2].i_lines );
428
429     if( !p_effect->p_data )
430     {
431         p_effect->p_data=(void *)malloc( 5 * i_size *sizeof(uint8_t));
432
433         if( !p_effect->p_data)
434         {
435             msg_Err(p_aout,"Out of memory");
436             return -1;
437         }
438         p_pictures = (uint8_t *)p_effect->p_data;
439     }
440     else
441     {
442         p_pictures =(uint8_t *)p_effect->p_data;
443     }
444
445     for( i = 0 ; i < 5 ; i++)
446     {
447         for ( j = 0 ; j< p_picture->p[0].i_pitch * p_picture->p[0].i_lines; i++)
448             p_picture->p[0].p_pixels[j] =
449                     p_pictures[i * i_size + j] * (100 - 20 * i) /100 ;
450         for ( j = 0 ; j< p_picture->p[1].i_pitch * p_picture->p[1].i_lines; i++)
451             p_picture->p[1].p_pixels[j] =
452                     p_pictures[i * i_size +
453                     p_picture->p[0].i_pitch * p_picture->p[0].i_lines + j ];
454         for ( j = 0 ; j< p_picture->p[2].i_pitch * p_picture->p[2].i_lines; i++)
455             p_picture->p[2].p_pixels[j] =
456                     p_pictures[i * i_size +
457                     p_picture->p[0].i_pitch * p_picture->p[0].i_lines +
458                     p_picture->p[1].i_pitch * p_picture->p[1].i_lines
459                     + j ];
460     }
461
462     memcpy ( &p_pictures[ i_size ] , &p_pictures[0] , 4 * i_size * sizeof(uint8_t) );
463 }
464 #endif