]> git.sesse.net Git - vlc/blob - modules/audio_filter/compressor.c
mediacodec: handle error_state in one place
[vlc] / modules / audio_filter / compressor.c
1 /*****************************************************************************
2  * compressor.c: dynamic range compressor, ported from plugins from LADSPA SWH
3  *****************************************************************************
4  * Copyright (C) 2010 Ronald Wright
5  * $Id$
6  *
7  * Author: Ronald Wright <logiconcepts819@gmail.com>
8  * Original author: Steve Harris <steve@plugin.org.uk>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <math.h>
34 #include <stdint.h>
35
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38
39 #include <vlc_aout.h>
40 #include <vlc_filter.h>
41
42 /*****************************************************************************
43 * Local prototypes.
44 *****************************************************************************/
45
46 #define A_TBL (256)
47
48 #define DB_TABLE_SIZE   (1024)
49 #define DB_MIN          (-60.0f)
50 #define DB_MAX          (24.0f)
51 #define LIN_TABLE_SIZE  (1024)
52 #define LIN_MIN         (0.0000000002f)
53 #define LIN_MAX         (9.0f)
54 #define DB_DEFAULT_CUBE
55 #define RMS_BUF_SIZE    (960)
56 #define LOOKAHEAD_SIZE  ((RMS_BUF_SIZE)<<1)
57
58 #define LIN_INTERP(f,a,b) ((a) + (f) * ( (b) - (a) ))
59 #define LIMIT(v,l,u)      (v < l ? l : ( v > u ? u : v ))
60
61 typedef struct
62 {
63     float        pf_buf[RMS_BUF_SIZE];
64     unsigned int i_pos;
65     unsigned int i_count;
66     float        f_sum;
67
68 } rms_env;
69
70 typedef struct
71 {
72     struct
73     {
74         float pf_vals[AOUT_CHAN_MAX];
75         float f_lev_in;
76
77     } p_buf[LOOKAHEAD_SIZE];
78     unsigned int i_pos;
79     unsigned int i_count;
80
81 } lookahead;
82
83 struct filter_sys_t
84 {
85     float f_amp;
86     float pf_as[A_TBL];
87     unsigned int i_count;
88     float f_env;
89     float f_env_peak;
90     float f_env_rms;
91     float f_gain;
92     float f_gain_out;
93     rms_env rms;
94     float f_sum;
95     lookahead la;
96
97     float pf_db_data[DB_TABLE_SIZE];
98     float pf_lin_data[LIN_TABLE_SIZE];
99
100     vlc_mutex_t lock;
101
102     float f_rms_peak;
103     float f_attack;
104     float f_release;
105     float f_threshold;
106     float f_ratio;
107     float f_knee;
108     float f_makeup_gain;
109 };
110
111 typedef union
112 {
113     float f;
114     int32_t i;
115
116 } ls_pcast32;
117
118 static int      Open            ( vlc_object_t * );
119 static void     Close           ( vlc_object_t * );
120 static block_t *DoWork          ( filter_t *, block_t * );
121
122 static void     DbInit          ( filter_sys_t * );
123 static float    Db2Lin          ( float, filter_sys_t * );
124 static float    Lin2Db          ( float, filter_sys_t * );
125 #ifdef DB_DEFAULT_CUBE
126 static float    CubeInterp      ( const float, const float, const float,
127                                   const float, const float );
128 #endif
129 static void     RoundToZero     ( float * );
130 static float    Max             ( float, float );
131 static float    Clamp           ( float, float, float );
132 static int      Round           ( float );
133 static float    RmsEnvProcess   ( rms_env *, const float );
134 static void     BufferProcess   ( float *, int, float, float, lookahead * );
135
136 static int RMSPeakCallback      ( vlc_object_t *, char const *, vlc_value_t,
137                                   vlc_value_t, void * );
138 static int AttackCallback       ( vlc_object_t *, char const *, vlc_value_t,
139                                   vlc_value_t, void * );
140 static int ReleaseCallback      ( vlc_object_t *, char const *, vlc_value_t,
141                                   vlc_value_t, void * );
142 static int ThresholdCallback    ( vlc_object_t *, char const *, vlc_value_t,
143                                   vlc_value_t, void * );
144 static int RatioCallback        ( vlc_object_t *, char const *, vlc_value_t,
145                                   vlc_value_t, void * );
146 static int KneeCallback         ( vlc_object_t *, char const *, vlc_value_t,
147                                   vlc_value_t, void * );
148 static int MakeupGainCallback   ( vlc_object_t *, char const *, vlc_value_t,
149                                   vlc_value_t, void * );
150
151 /*****************************************************************************
152  * Module descriptor
153  *****************************************************************************/
154
155 #define RMS_PEAK_TEXT N_( "RMS/peak" )
156 #define RMS_PEAK_LONGTEXT N_( "Set the RMS/peak (0 ... 1)." )
157
158 #define ATTACK_TEXT N_( "Attack time" )
159 #define ATTACK_LONGTEXT N_( \
160         "Set the attack time in milliseconds (1.5 ... 400)." )
161
162 #define RELEASE_TEXT N_( "Release time" )
163 #define RELEASE_LONGTEXT N_( \
164         "Set the release time in milliseconds (2 ... 800)." )
165
166 #define THRESHOLD_TEXT N_( "Threshold level" )
167 #define THRESHOLD_LONGTEXT N_( "Set the threshold level in dB (-30 ... 0)." )
168
169 #define RATIO_TEXT N_( "Ratio" )
170 #define RATIO_LONGTEXT N_( "Set the ratio (n:1) (1 ... 20)." )
171
172 #define KNEE_TEXT N_( "Knee radius" )
173 #define KNEE_LONGTEXT N_( "Set the knee radius in dB (1 ... 10)." )
174
175 #define MAKEUP_GAIN_TEXT N_( "Makeup gain" )
176 #define MAKEUP_GAIN_LONGTEXT N_( "Set the makeup gain in dB (0 ... 24)." )
177
178 vlc_module_begin()
179     set_shortname( _("Compressor") )
180     set_description( _("Dynamic range compressor") )
181     set_capability( "audio filter", 0 )
182     set_category( CAT_AUDIO )
183     set_subcategory( SUBCAT_AUDIO_AFILTER )
184
185     add_float( "compressor-rms-peak", 0.0, RMS_PEAK_TEXT,
186                RMS_PEAK_LONGTEXT, false )
187     add_float( "compressor-attack", 25.0, ATTACK_TEXT,
188                ATTACK_LONGTEXT, false )
189     add_float( "compressor-release", 100.0, RELEASE_TEXT,
190                RELEASE_LONGTEXT, false )
191     add_float( "compressor-threshold", -11.0, THRESHOLD_TEXT,
192                THRESHOLD_LONGTEXT, false )
193     add_float( "compressor-ratio", 8.0, RATIO_TEXT,
194                RATIO_LONGTEXT, false )
195     add_float( "compressor-knee", 2.5, KNEE_TEXT,
196                KNEE_LONGTEXT, false )
197     add_float( "compressor-makeup-gain", 7.0, MAKEUP_GAIN_TEXT,
198                MAKEUP_GAIN_LONGTEXT, false )
199     set_callbacks( Open, Close )
200     add_shortcut( "compressor" )
201 vlc_module_end ()
202
203 /*****************************************************************************
204  * Open: initialize interface
205  *****************************************************************************/
206
207 static int Open( vlc_object_t *p_this )
208 {
209     filter_t *p_filter = (filter_t*)p_this;
210     vlc_object_t *p_aout = p_filter->p_parent;
211     float f_sample_rate = p_filter->fmt_in.audio.i_rate;
212     float f_num;
213
214     /* Initialize the filter parameter structure */
215     filter_sys_t *p_sys = p_filter->p_sys = calloc( 1, sizeof(*p_sys) );
216     if( !p_sys )
217     {
218         return VLC_ENOMEM;
219     }
220
221     /* Initialize the attack lookup table */
222     p_sys->pf_as[0] = 1.0f;
223     for( int i = 1; i < A_TBL; i++ )
224     {
225         p_sys->pf_as[i] = expf( -1.0f / ( f_sample_rate * i / A_TBL ) );
226     }
227
228     /* Calculate the RMS and lookahead sizes from the sample rate */
229     f_num = 0.01f * f_sample_rate;
230     p_sys->rms.i_count = Round( Clamp( 0.5f * f_num, 1.0f, RMS_BUF_SIZE ) );
231     p_sys->la.i_count = Round( Clamp( f_num, 1.0f, LOOKAHEAD_SIZE ) );
232
233     /* Initialize decibel lookup tables */
234     DbInit( p_sys );
235
236     /* Restore the last saved settings */
237     p_sys->f_rms_peak    = var_CreateGetFloat( p_aout, "compressor-rms-peak" );
238     p_sys->f_attack      = var_CreateGetFloat( p_aout, "compressor-attack" );
239     p_sys->f_release     = var_CreateGetFloat( p_aout, "compressor-release" );
240     p_sys->f_threshold   = var_CreateGetFloat( p_aout, "compressor-threshold" );
241     p_sys->f_ratio       = var_CreateGetFloat( p_aout, "compressor-ratio" );
242     p_sys->f_knee        = var_CreateGetFloat( p_aout, "compressor-knee" );
243     p_sys->f_makeup_gain =
244            var_CreateGetFloat( p_aout, "compressor-makeup-gain" );
245
246     /* Initialize the mutex */
247     vlc_mutex_init( &p_sys->lock );
248
249     /* Add our own callbacks */
250     var_AddCallback( p_aout, "compressor-rms-peak", RMSPeakCallback, p_sys );
251     var_AddCallback( p_aout, "compressor-attack", AttackCallback, p_sys );
252     var_AddCallback( p_aout, "compressor-release", ReleaseCallback, p_sys );
253     var_AddCallback( p_aout, "compressor-threshold", ThresholdCallback, p_sys );
254     var_AddCallback( p_aout, "compressor-ratio", RatioCallback, p_sys );
255     var_AddCallback( p_aout, "compressor-knee", KneeCallback, p_sys );
256     var_AddCallback( p_aout, "compressor-makeup-gain", MakeupGainCallback, p_sys );
257
258     /* Set the filter function */
259     p_filter->fmt_in.audio.i_format = VLC_CODEC_FL32;
260     p_filter->fmt_out.audio = p_filter->fmt_in.audio;
261     p_filter->pf_audio_filter = DoWork;
262
263     /* At this stage, we are ready! */
264     msg_Dbg( p_filter, "compressor successfully initialized" );
265     return VLC_SUCCESS;
266 }
267
268 /*****************************************************************************
269  * Close: destroy interface
270  *****************************************************************************/
271
272 static void Close( vlc_object_t *p_this )
273 {
274     filter_t *p_filter = (filter_t*)p_this;
275     vlc_object_t *p_aout = p_filter->p_parent;
276     filter_sys_t *p_sys = p_filter->p_sys;
277
278     /* Remove our callbacks */
279     var_DelCallback( p_aout, "compressor-rms-peak", RMSPeakCallback, p_sys );
280     var_DelCallback( p_aout, "compressor-attack", AttackCallback, p_sys );
281     var_DelCallback( p_aout, "compressor-release", ReleaseCallback, p_sys );
282     var_DelCallback( p_aout, "compressor-threshold", ThresholdCallback, p_sys );
283     var_DelCallback( p_aout, "compressor-ratio", RatioCallback, p_sys );
284     var_DelCallback( p_aout, "compressor-knee", KneeCallback, p_sys );
285     var_DelCallback( p_aout, "compressor-makeup-gain", MakeupGainCallback, p_sys );
286
287     /* Destroy the mutex */
288     vlc_mutex_destroy( &p_sys->lock );
289
290     /* Destroy the filter parameter structure */
291     free( p_sys );
292 }
293
294 /*****************************************************************************
295  * DoWork: process samples buffer
296  *****************************************************************************/
297
298 static block_t * DoWork( filter_t * p_filter, block_t * p_in_buf )
299 {
300     int i_samples = p_in_buf->i_nb_samples;
301     int i_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio );
302     float *pf_buf = (float*)p_in_buf->p_buffer;
303
304     /* Current parameters */
305     filter_sys_t *p_sys = p_filter->p_sys;
306
307     /* Fetch the configurable parameters */
308     vlc_mutex_lock( &p_sys->lock );
309
310     float f_rms_peak    = p_sys->f_rms_peak;     /* RMS/peak */
311     float f_attack      = p_sys->f_attack;       /* Attack time (ms) */
312     float f_release     = p_sys->f_release;      /* Release time (ms) */
313     float f_threshold   = p_sys->f_threshold;    /* Threshold level (dB) */
314     float f_ratio       = p_sys->f_ratio;        /* Ratio (n:1) */
315     float f_knee        = p_sys->f_knee;         /* Knee radius (dB) */
316     float f_makeup_gain = p_sys->f_makeup_gain;  /* Makeup gain (dB) */
317
318     vlc_mutex_unlock( &p_sys->lock );
319
320     /* Fetch the internal parameters */
321     float f_amp      =  p_sys->f_amp;
322     float *pf_as     =  p_sys->pf_as;
323     float f_env      =  p_sys->f_env;
324     float f_env_peak =  p_sys->f_env_peak;
325     float f_env_rms  =  p_sys->f_env_rms;
326     float f_gain     =  p_sys->f_gain;
327     float f_gain_out =  p_sys->f_gain_out;
328     rms_env *p_rms   = &p_sys->rms;
329     float f_sum      =  p_sys->f_sum;
330     lookahead *p_la  = &p_sys->la;
331
332     /* Prepare other compressor parameters */
333     float f_ga       = f_attack < 2.0f ? 0.0f :
334                        pf_as[Round( f_attack  * 0.001f * ( A_TBL - 1 ) )];
335     float f_gr       = pf_as[Round( f_release * 0.001f * ( A_TBL - 1 ) )];
336     float f_rs       = ( f_ratio - 1.0f ) / f_ratio;
337     float f_mug      = Db2Lin( f_makeup_gain, p_sys );
338     float f_knee_min = Db2Lin( f_threshold - f_knee, p_sys );
339     float f_knee_max = Db2Lin( f_threshold + f_knee, p_sys );
340     float f_ef_a     = f_ga * 0.25f;
341     float f_ef_ai    = 1.0f - f_ef_a;
342
343     /* Process the current buffer */
344     for( int i = 0; i < i_samples; i++ )
345     {
346         float f_lev_in_old, f_lev_in_new;
347
348         /* Now, compress the pre-equalized audio (ported from sc4_1882
349          * plugin with a few modifications) */
350
351         /* Fetch the old delayed buffer value */
352         f_lev_in_old = p_la->p_buf[p_la->i_pos].f_lev_in;
353
354         /* Find the peak value of current sample.  This becomes the new delayed
355          * buffer value that replaces the old one in the lookahead array */
356         f_lev_in_new = fabs( pf_buf[0] );
357         for( int i_chan = 1; i_chan < i_channels; i_chan++ )
358         {
359             f_lev_in_new = Max( f_lev_in_new, fabs( pf_buf[i_chan] ) );
360         }
361         p_la->p_buf[p_la->i_pos].f_lev_in = f_lev_in_new;
362
363         /* Add the square of the peak value to a running sum */
364         f_sum += f_lev_in_new * f_lev_in_new;
365
366         /* Update the RMS envelope */
367         if( f_amp > f_env_rms )
368         {
369             f_env_rms = f_env_rms * f_ga + f_amp * ( 1.0f - f_ga );
370         }
371         else
372         {
373             f_env_rms = f_env_rms * f_gr + f_amp * ( 1.0f - f_gr );
374         }
375         RoundToZero( &f_env_rms );
376
377         /* Update the peak envelope */
378         if( f_lev_in_old > f_env_peak )
379         {
380             f_env_peak = f_env_peak * f_ga + f_lev_in_old * ( 1.0f - f_ga );
381         }
382         else
383         {
384             f_env_peak = f_env_peak * f_gr + f_lev_in_old * ( 1.0f - f_gr );
385         }
386         RoundToZero( &f_env_peak );
387
388         /* Process the RMS value and update the output gain every 4 samples */
389         if( ( p_sys->i_count++ & 3 ) == 3 )
390         {
391             /* Process the RMS value by placing in the mean square value, and
392              * reset the running sum */
393             f_amp = RmsEnvProcess( p_rms, f_sum * 0.25f );
394             f_sum = 0.0f;
395             if( isnan( f_env_rms ) )
396             {
397                 /* This can happen sometimes, but I don't know why. */
398                 f_env_rms = 0.0f;
399             }
400
401             /* Find the superposition of the RMS and peak envelopes */
402             f_env = LIN_INTERP( f_rms_peak, f_env_rms, f_env_peak );
403
404             /* Update the output gain */
405             if( f_env <= f_knee_min )
406             {
407                 /* Gain below the knee (and below the threshold) */
408                 f_gain_out = 1.0f;
409             }
410             else if( f_env < f_knee_max )
411             {
412                 /* Gain within the knee */
413                 const float f_x = -( f_threshold
414                                    - f_knee - Lin2Db( f_env, p_sys ) ) / f_knee;
415                 f_gain_out = Db2Lin( -f_knee * f_rs * f_x * f_x * 0.25f,
416                                       p_sys );
417             }
418             else
419             {
420                 /* Gain above the knee (and above the threshold) */
421                 f_gain_out = Db2Lin( ( f_threshold - Lin2Db( f_env, p_sys ) )
422                                      * f_rs, p_sys );
423             }
424         }
425
426         /* Find the total gain */
427         f_gain = f_gain * f_ef_a + f_gain_out * f_ef_ai;
428
429         /* Write the resulting buffer to the output */
430         BufferProcess( pf_buf, i_channels, f_gain, f_mug, p_la );
431         pf_buf += i_channels;
432     }
433
434     /* Update the internal parameters */
435     p_sys->f_sum      = f_sum;
436     p_sys->f_amp      = f_amp;
437     p_sys->f_gain     = f_gain;
438     p_sys->f_gain_out = f_gain_out;
439     p_sys->f_env      = f_env;
440     p_sys->f_env_rms  = f_env_rms;
441     p_sys->f_env_peak = f_env_peak;
442
443     return p_in_buf;
444 }
445
446 /*****************************************************************************
447  * Helper functions for compressor
448  *****************************************************************************/
449
450 static void DbInit( filter_sys_t * p_sys )
451 {
452     float *pf_lin_data = p_sys->pf_lin_data;
453     float *pf_db_data = p_sys->pf_db_data;
454
455     /* Fill linear lookup table */
456     for( int i = 0; i < LIN_TABLE_SIZE; i++ )
457     {
458         pf_lin_data[i] = powf( 10.0f, ( ( DB_MAX - DB_MIN ) *
459                    (float)i / LIN_TABLE_SIZE + DB_MIN ) / 20.0f );
460     }
461
462     /* Fill logarithmic lookup table */
463     for( int i = 0; i < DB_TABLE_SIZE; i++ )
464     {
465         pf_db_data[i] = 20.0f * log10f( ( LIN_MAX - LIN_MIN ) *
466                    (float)i / DB_TABLE_SIZE + LIN_MIN );
467     }
468 }
469
470 static float Db2Lin( float f_db, filter_sys_t * p_sys )
471 {
472     float f_scale = ( f_db - DB_MIN ) * LIN_TABLE_SIZE / ( DB_MAX - DB_MIN );
473     int i_base = Round( f_scale - 0.5f );
474     float f_ofs = f_scale - i_base;
475     float *pf_lin_data = p_sys->pf_lin_data;
476
477     if( i_base < 1 )
478     {
479         return 0.0f;
480     }
481     else if( i_base > LIN_TABLE_SIZE - 3 )
482     {
483         return pf_lin_data[LIN_TABLE_SIZE - 2];
484     }
485
486 #ifdef DB_DEFAULT_CUBE
487     return CubeInterp( f_ofs, pf_lin_data[i_base - 1],
488                               pf_lin_data[i_base],
489                               pf_lin_data[i_base + 1],
490                               pf_lin_data[i_base + 2] );
491 #else
492     return ( 1.0f - f_ofs ) * pf_lin_data[i_base]
493                   + f_ofs   * pf_lin_data[i_base + 1];
494 #endif
495 }
496
497 static float Lin2Db( float f_lin, filter_sys_t * p_sys )
498 {
499     float f_scale = ( f_lin - LIN_MIN ) * DB_TABLE_SIZE / ( LIN_MAX - LIN_MIN );
500     int i_base = Round( f_scale - 0.5f );
501     float f_ofs = f_scale - i_base;
502     float *pf_db_data = p_sys->pf_db_data;
503
504     if( i_base < 2 )
505     {
506         return pf_db_data[2] * f_scale * 0.5f - 23.0f * ( 2.0f - f_scale );
507     }
508     else if( i_base > DB_TABLE_SIZE - 3 )
509     {
510         return pf_db_data[DB_TABLE_SIZE - 2];
511     }
512
513 #ifdef DB_DEFAULT_CUBE
514     return CubeInterp( f_ofs, pf_db_data[i_base - 1],
515                               pf_db_data[i_base],
516                               pf_db_data[i_base + 1],
517                               pf_db_data[i_base + 2] );
518 #else
519     return ( 1.0f - f_ofs ) * pf_db_data[i_base]
520                   + f_ofs   * pf_db_data[i_base + 1];
521 #endif
522 }
523
524 #ifdef DB_DEFAULT_CUBE
525 /* Cubic interpolation function */
526 static float CubeInterp( const float f_fr, const float f_inm1,
527                                            const float f_in,
528                                            const float f_inp1,
529                                            const float f_inp2 )
530 {
531     return f_in + 0.5f * f_fr * ( f_inp1 - f_inm1 +
532          f_fr * ( 4.0f * f_inp1 + 2.0f * f_inm1 - 5.0f * f_in - f_inp2 +
533          f_fr * ( 3.0f * ( f_in - f_inp1 ) - f_inm1 + f_inp2 ) ) );
534 }
535 #endif
536
537 /* Zero out denormals by adding and subtracting a small number, from Laurent
538  * de Soras */
539 static void RoundToZero( float *pf_x )
540 {
541     static const float f_anti_denormal = 1e-18;
542
543     *pf_x += f_anti_denormal;
544     *pf_x -= f_anti_denormal;
545 }
546
547 /* A set of branchless clipping operations from Laurent de Soras */
548
549 static float Max( float f_x, float f_a )
550 {
551     f_x -= f_a;
552     f_x += fabsf( f_x );
553     f_x *= 0.5f;
554     f_x += f_a;
555
556     return f_x;
557 }
558
559 static float Clamp( float f_x, float f_a, float f_b )
560 {
561     const float f_x1 = fabsf( f_x - f_a );
562     const float f_x2 = fabsf( f_x - f_b );
563
564     f_x = f_x1 + f_a + f_b;
565     f_x -= f_x2;
566     f_x *= 0.5f;
567
568     return f_x;
569 }
570
571 /* Round float to int using IEEE int* hack */
572 static int Round( float f_x )
573 {
574     ls_pcast32 p;
575
576     p.f = f_x;
577     p.f += ( 3 << 22 );
578
579     return p.i - 0x4b400000;
580 }
581
582 /* Calculate current level from root-mean-squared of circular buffer ("RMS") */
583 static float RmsEnvProcess( rms_env * p_r, const float f_x )
584 {
585     /* Remove the old term from the sum */
586     p_r->f_sum -= p_r->pf_buf[p_r->i_pos];
587
588     /* Add the new term to the sum */
589     p_r->f_sum += f_x;
590
591     /* If the sum is small enough, make it zero */
592     if( p_r->f_sum < 1.0e-6f )
593     {
594         p_r->f_sum = 0.0f;
595     }
596
597     /* Replace the old term in the array with the new one */
598     p_r->pf_buf[p_r->i_pos] = f_x;
599
600     /* Go to the next position for the next RMS calculation */
601     p_r->i_pos = ( p_r->i_pos + 1 ) % ( p_r->i_count );
602
603     /* Return the RMS value */
604     return sqrt( p_r->f_sum / p_r->i_count );
605 }
606
607 /* Output the compressed delayed buffer and store the current buffer.  Uses a
608  * circular array, just like the one used in calculating the RMS of the buffer
609  */
610 static void BufferProcess( float * pf_buf, int i_channels, float f_gain,
611                            float f_mug, lookahead * p_la )
612 {
613     /* Loop through every channel */
614     for( int i_chan = 0; i_chan < i_channels; i_chan++ )
615     {
616         float f_x = pf_buf[i_chan]; /* Current buffer value */
617
618         /* Output the compressed delayed buffer value */
619         pf_buf[i_chan] = p_la->p_buf[p_la->i_pos].pf_vals[i_chan]
620                        * f_gain * f_mug;
621
622         /* Update the delayed buffer value */
623         p_la->p_buf[p_la->i_pos].pf_vals[i_chan] = f_x;
624     }
625
626     /* Go to the next delayed buffer value for the next run */
627     p_la->i_pos = ( p_la->i_pos + 1 ) % ( p_la->i_count );
628 }
629
630 /*****************************************************************************
631  * Callback functions
632  *****************************************************************************/
633 static int RMSPeakCallback( vlc_object_t *p_this, char const *psz_cmd,
634                             vlc_value_t oldval, vlc_value_t newval,
635                             void * p_data )
636 {
637     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
638     filter_sys_t *p_sys = p_data;
639
640     vlc_mutex_lock( &p_sys->lock );
641     p_sys->f_rms_peak = Clamp( newval.f_float, 0.0f, 1.0f );
642     vlc_mutex_unlock( &p_sys->lock );
643
644     return VLC_SUCCESS;
645 }
646
647 static int AttackCallback( vlc_object_t *p_this, char const *psz_cmd,
648                            vlc_value_t oldval, vlc_value_t newval,
649                            void * p_data )
650 {
651     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
652     filter_sys_t *p_sys = p_data;
653
654     vlc_mutex_lock( &p_sys->lock );
655     p_sys->f_attack = Clamp( newval.f_float, 1.5f, 400.0f );
656     vlc_mutex_unlock( &p_sys->lock );
657
658     return VLC_SUCCESS;
659 }
660
661 static int ReleaseCallback( vlc_object_t *p_this, char const *psz_cmd,
662                             vlc_value_t oldval, vlc_value_t newval,
663                             void * p_data )
664 {
665     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
666     filter_sys_t *p_sys = p_data;
667
668     vlc_mutex_lock( &p_sys->lock );
669     p_sys->f_release = Clamp( newval.f_float, 2.0f, 800.0f );
670     vlc_mutex_unlock( &p_sys->lock );
671
672     return VLC_SUCCESS;
673 }
674
675 static int ThresholdCallback( vlc_object_t *p_this, char const *psz_cmd,
676                               vlc_value_t oldval, vlc_value_t newval,
677                               void * p_data )
678 {
679     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
680     filter_sys_t *p_sys = p_data;
681
682     vlc_mutex_lock( &p_sys->lock );
683     p_sys->f_threshold = Clamp( newval.f_float, -30.0f, 0.0f );
684     vlc_mutex_unlock( &p_sys->lock );
685
686     return VLC_SUCCESS;
687 }
688
689 static int RatioCallback( vlc_object_t *p_this, char const *psz_cmd,
690                           vlc_value_t oldval, vlc_value_t newval,
691                           void * p_data )
692 {
693     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
694     filter_sys_t *p_sys = p_data;
695
696     vlc_mutex_lock( &p_sys->lock );
697     p_sys->f_ratio = Clamp( newval.f_float, 1.0f, 20.0f );
698     vlc_mutex_unlock( &p_sys->lock );
699
700     return VLC_SUCCESS;
701 }
702
703 static int KneeCallback( vlc_object_t *p_this, char const *psz_cmd,
704                          vlc_value_t oldval, vlc_value_t newval,
705                          void * p_data )
706 {
707     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
708     filter_sys_t *p_sys = p_data;
709
710     vlc_mutex_lock( &p_sys->lock );
711     p_sys->f_knee = Clamp( newval.f_float, 1.0f, 10.0f );
712     vlc_mutex_unlock( &p_sys->lock );
713
714     return VLC_SUCCESS;
715 }
716
717 static int MakeupGainCallback( vlc_object_t *p_this, char const *psz_cmd,
718                                vlc_value_t oldval, vlc_value_t newval,
719                                void * p_data )
720 {
721     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
722     filter_sys_t *p_sys = p_data;
723
724     vlc_mutex_lock( &p_sys->lock );
725     p_sys->f_makeup_gain = Clamp( newval.f_float, 0.0f, 24.0f );
726     vlc_mutex_unlock( &p_sys->lock );
727
728     return VLC_SUCCESS;
729 }