]> git.sesse.net Git - vlc/blob - modules/audio_filter/normvol.c
Remove _GNU_SOURCE and string.h too
[vlc] / modules / audio_filter / normvol.c
1 /*****************************************************************************
2  * normvol.c: volume normalizer
3  *****************************************************************************
4  * Copyright (C) 2001, 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*
25  * TODO:
26  *
27  * We should detect fast power increases and react faster to these
28  * This way, we can increase the buffer size to get a more stable filter */
29
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34
35 #include <errno.h>                                                 /* ENOMEM */
36 #include <stdio.h>
37 #include <ctype.h>
38 #include <signal.h>
39
40 #include <math.h>
41
42
43 #include <vlc/vlc.h>
44
45 #include <vlc_aout.h>
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50
51 static int  Open     ( vlc_object_t * );
52 static void Close    ( vlc_object_t * );
53 static void DoWork   ( aout_instance_t * , aout_filter_t *,
54                 aout_buffer_t * , aout_buffer_t *);
55
56 typedef struct aout_filter_sys_t
57 {
58     int i_nb;
59     float *p_last;
60     float f_max;
61 } aout_filter_sys_t;
62
63 /*****************************************************************************
64  * Module descriptor
65  *****************************************************************************/
66 #define BUFF_TEXT N_("Number of audio buffers" )
67 #define BUFF_LONGTEXT N_("This is the number of audio buffers on which the " \
68                 "power measurement is made. A higher number of buffers will " \
69                 "increase the response time of the filter to a spike " \
70                 "but will make it less sensitive to short variations." )
71
72 #define LEVEL_TEXT N_("Max level" )
73 #define LEVEL_LONGTEXT N_("If the average power over the last N buffers " \
74                "is higher than this value, the volume will be normalized. " \
75                "This value is a positive floating point number. A value " \
76                "between 0.5 and 10 seems sensible." )
77
78 vlc_module_begin();
79     set_description( _("Volume normalizer") );
80     set_shortname( _("Volume normalizer") );
81     set_category( CAT_AUDIO );
82     set_subcategory( SUBCAT_AUDIO_AFILTER );
83     add_shortcut( "volnorm" );
84     add_integer( "norm-buff-size", 20 ,NULL ,BUFF_TEXT, BUFF_LONGTEXT,
85                  VLC_TRUE);
86     add_float( "norm-max-level", 2.0, NULL, LEVEL_TEXT,
87                LEVEL_LONGTEXT, VLC_TRUE );
88     set_capability( "audio filter", 0 );
89     set_callbacks( Open, Close );
90 vlc_module_end();
91
92 /*****************************************************************************
93  * Open: initialize and create stuff
94  *****************************************************************************/
95 static int Open( vlc_object_t *p_this )
96 {
97     aout_filter_t *p_filter = (aout_filter_t*)p_this;
98     vlc_bool_t b_fit = VLC_TRUE;
99     int i_channels;
100     aout_filter_sys_t *p_sys;
101
102     if( p_filter->input.i_format != VLC_FOURCC('f','l','3','2' ) ||
103         p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
104     {
105         b_fit = VLC_FALSE;
106         p_filter->input.i_format = VLC_FOURCC('f','l','3','2');
107         p_filter->output.i_format = VLC_FOURCC('f','l','3','2');
108         msg_Warn( p_filter, "bad input or output format" );
109     }
110
111     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
112     {
113         b_fit = VLC_FALSE;
114         memcpy( &p_filter->output, &p_filter->input,
115                 sizeof(audio_sample_format_t) );
116         msg_Warn( p_filter, "input and output formats are not similar" );
117     }
118
119     if ( ! b_fit )
120     {
121         return VLC_EGENERIC;
122     }
123
124     p_filter->pf_do_work = DoWork;
125     p_filter->b_in_place = VLC_TRUE;
126
127     i_channels = aout_FormatNbChannels( &p_filter->input );
128
129     p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) );
130     p_sys->i_nb = var_CreateGetInteger( p_filter->p_parent, "norm-buff-size" );
131     p_sys->f_max = var_CreateGetFloat( p_filter->p_parent, "norm-max-level" );
132
133     if( p_sys->f_max <= 0 ) p_sys->f_max = 0.01;
134
135     /* We need to store (nb_buffers+1)*nb_channels floats */
136     p_sys->p_last = malloc( sizeof( float ) * (i_channels) *
137                             (p_filter->p_sys->i_nb + 2) );
138     memset( p_sys->p_last, 0 ,sizeof( float ) * (i_channels) *
139             (p_filter->p_sys->i_nb + 2) );
140     return VLC_SUCCESS;
141 }
142
143 /*****************************************************************************
144  * DoWork : normalizes and sends a buffer
145  *****************************************************************************/
146  static void DoWork( aout_instance_t *p_aout, aout_filter_t *p_filter,
147                      aout_buffer_t *p_in_buf, aout_buffer_t *p_out_buf )
148 {
149     float *pf_sum;
150     float *pf_gain;
151     float f_average = 0;
152     int i, i_chan;
153
154     int i_samples = p_in_buf->i_nb_samples;
155     int i_channels = aout_FormatNbChannels( &p_filter->input );
156     float *p_out = (float*)p_out_buf->p_buffer;
157     float *p_in =  (float*)p_in_buf->p_buffer;
158
159     struct aout_filter_sys_t *p_sys = p_filter->p_sys;
160
161     pf_sum = (float *)malloc( sizeof(float) * i_channels );
162     memset( pf_sum, 0, sizeof(float) * i_channels );
163
164     pf_gain = (float *)malloc( sizeof(float) * i_channels );
165
166     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
167     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes;
168
169     /* Calculate the average power level on this buffer */
170     for( i = 0 ; i < i_samples; i++ )
171     {
172         for( i_chan = 0; i_chan < i_channels; i_chan++ )
173         {
174             float f_sample = p_in[i_chan];
175             float f_square = pow( f_sample, 2 );
176             pf_sum[i_chan] += f_square;
177         }
178         p_in += i_channels;
179     }
180
181     /* sum now contains for each channel the sigma(value²) */
182     for( i_chan = 0; i_chan < i_channels; i_chan++ )
183     {
184         /* Shift our lastbuff */
185         memmove( &p_sys->p_last[ i_chan * p_sys->i_nb],
186                         &p_sys->p_last[i_chan * p_sys->i_nb + 1],
187                  (p_sys->i_nb-1) * sizeof( float ) );
188
189         /* Insert the new average : sqrt(sigma(value²)) */
190         p_sys->p_last[ i_chan * p_sys->i_nb + p_sys->i_nb - 1] =
191                 sqrt( pf_sum[i_chan] );
192
193         pf_sum[i_chan] = 0;
194
195         /* Get the average power on the lastbuff */
196         f_average = 0;
197         for( i = 0; i < p_sys->i_nb ; i++)
198         {
199             f_average += p_sys->p_last[ i_chan * p_sys->i_nb + i ];
200         }
201         f_average = f_average / p_sys->i_nb;
202
203         /* Seuil arbitraire */
204         p_sys->f_max = var_GetFloat( p_aout, "norm-max-level" );
205
206         //fprintf(stderr,"Average %f, max %f\n", f_average, p_sys->f_max );
207         if( f_average > p_sys->f_max )
208         {
209              pf_gain[i_chan] = f_average / p_sys->f_max;
210         }
211         else
212         {
213            pf_gain[i_chan] = 1;
214         }
215     }
216
217     /* Apply gain */
218     for( i = 0; i < i_samples; i++)
219     {
220         for( i_chan = 0; i_chan < i_channels; i_chan++ )
221         {
222             p_out[i_chan] /= pf_gain[i_chan];
223         }
224         p_out += i_channels;
225     }
226
227     free( pf_sum );
228     free( pf_gain );
229
230     return;
231 }
232
233 /**********************************************************************
234  * Close
235  **********************************************************************/
236 static void Close( vlc_object_t *p_this )
237 {
238     aout_filter_t *p_filter = (aout_filter_t*)p_this;
239     aout_filter_sys_t *p_sys = p_filter->p_sys;
240
241     if( p_sys )
242     {
243         if( p_sys->p_last) free( p_sys->p_last );
244         free( p_sys );
245     }
246 }