Remaining audio handling switched to stacks; Minor corrections to compositing and...
[melted] / src / modules / normalize / filter_volume.c
1 /*
2 * filter_volume.c -- adjust audio volume
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include "filter_volume.h"
22
23 #include <framework/mlt_frame.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <math.h>
28 #include <ctype.h>
29 #include <string.h>
30
31 #define MAX_CHANNELS 6
32 #define EPSILON 0.00001
33
34 /* The normalise functions come from the normalize utility:
35 Copyright (C) 1999--2002 Chris Vaill */
36
37 #define samp_width 16
38
39 #ifndef ROUND
40 # define ROUND(x) floor((x) + 0.5)
41 #endif
42
43 #define DBFSTOAMP(x) pow(10,(x)/20.0)
44
45 /** Return nonzero if the two strings are equal, ignoring case, up to
46 the first n characters.
47 */
48 int strncaseeq(const char *s1, const char *s2, size_t n)
49 {
50 for ( ; n > 0; n--)
51 {
52 if (tolower(*s1++) != tolower(*s2++))
53 return 0;
54 }
55 return 1;
56 }
57
58 /** Limiter function.
59
60 / tanh((x + lev) / (1-lev)) * (1-lev) - lev (for x < -lev)
61 |
62 x' = | x (for |x| <= lev)
63 |
64 \ tanh((x - lev) / (1-lev)) * (1-lev) + lev (for x > lev)
65
66 With limiter level = 0, this is equivalent to a tanh() function;
67 with limiter level = 1, this is equivalent to clipping.
68 */
69 static inline double limiter( double x, double lmtr_lvl )
70 {
71 double xp = x;
72
73 if (x < -lmtr_lvl)
74 xp = tanh((x + lmtr_lvl) / (1-lmtr_lvl)) * (1-lmtr_lvl) - lmtr_lvl;
75 else if (x > lmtr_lvl)
76 xp = tanh((x - lmtr_lvl) / (1-lmtr_lvl)) * (1-lmtr_lvl) + lmtr_lvl;
77
78 // if ( x != xp )
79 // fprintf( stderr, "filter_volume: sample %f limited %f\n", x, xp );
80
81 return xp;
82 }
83
84
85 /** Takes a full smoothing window, and returns the value of the center
86 element, smoothed.
87
88 Currently, just does a mean filter, but we could do a median or
89 gaussian filter here instead.
90 */
91 static inline double get_smoothed_data( double *buf, int count )
92 {
93 int i, j;
94 double smoothed = 0;
95
96 for ( i = 0, j = 0; i < count; i++ )
97 {
98 if ( buf[ i ] != -1.0 )
99 {
100 smoothed += buf[ i ];
101 j++;
102 }
103 }
104 smoothed /= j;
105 // fprintf( stderr, "smoothed over %d values, result %f\n", j, smoothed );
106
107 return smoothed;
108 }
109
110 /** Get the max power level (using RMS) and peak level of the audio segment.
111 */
112 double signal_max_power( int16_t *buffer, int channels, int samples, int16_t *peak )
113 {
114 // Determine numeric limits
115 int bytes_per_samp = (samp_width - 1) / 8 + 1;
116 int16_t max = (1 << (bytes_per_samp * 8 - 1)) - 1;
117 int16_t min = -max - 1;
118
119 double *sums = (double *) calloc( channels, sizeof(double) );
120 int c, i;
121 int16_t sample;
122 double pow, maxpow = 0;
123
124 /* initialize peaks to effectively -inf and +inf */
125 int16_t max_sample = min;
126 int16_t min_sample = max;
127
128 for ( i = 0; i < samples; i++ )
129 {
130 for ( c = 0; c < channels; c++ )
131 {
132 sample = *buffer++;
133 sums[ c ] += (double) sample * (double) sample;
134
135 /* track peak */
136 if ( sample > max_sample )
137 max_sample = sample;
138 else if ( sample < min_sample )
139 min_sample = sample;
140 }
141 }
142 for ( c = 0; c < channels; c++ )
143 {
144 pow = sums[ c ] / (double) samples;
145 if ( pow > maxpow )
146 maxpow = pow;
147 }
148
149 free( sums );
150
151 /* scale the pow value to be in the range 0.0 -- 1.0 */
152 maxpow /= ( (double) min * (double) min);
153
154 if ( -min_sample > max_sample )
155 *peak = min_sample / (double) min;
156 else
157 *peak = max_sample / (double) max;
158
159 return sqrt( maxpow );
160 }
161
162 /* ------ End normalize functions --------------------------------------- */
163
164 /** Get the audio.
165 */
166
167 static int filter_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
168 {
169 // Get the properties of the a frame
170 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
171 double gain = mlt_properties_get_double( properties, "volume.gain" );
172 double max_gain = mlt_properties_get_double( properties, "volume.max_gain" );
173 double limiter_level = 0.5; /* -6 dBFS */
174 int normalise = mlt_properties_get_int( properties, "volume.normalise" );
175 double amplitude = mlt_properties_get_double( properties, "volume.amplitude" );
176 int i, j;
177 double sample;
178 int16_t peak;
179
180 // Get the filter from the frame
181 mlt_filter this = mlt_properties_get_data( properties, "filter_volume", NULL );
182
183 // Get the properties from the filter
184 mlt_properties filter_props = MLT_FILTER_PROPERTIES( this );
185
186 if ( mlt_properties_get( properties, "volume.limiter" ) != NULL )
187 limiter_level = mlt_properties_get_double( properties, "volume.limiter" );
188
189 // Get the producer's audio
190 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
191 // fprintf( stderr, "filter_volume: frequency %d\n", *frequency );
192
193 // Determine numeric limits
194 int bytes_per_samp = (samp_width - 1) / 8 + 1;
195 int samplemax = (1 << (bytes_per_samp * 8 - 1)) - 1;
196 int samplemin = -samplemax - 1;
197
198 if ( normalise )
199 {
200 int window = mlt_properties_get_int( filter_props, "window" );
201 double *smooth_buffer = mlt_properties_get_data( filter_props, "smooth_buffer", NULL );
202
203 if ( window > 0 && smooth_buffer != NULL )
204 {
205 int smooth_index = mlt_properties_get_int( filter_props, "_smooth_index" );
206
207 // Compute the signal power and put into smoothing buffer
208 smooth_buffer[ smooth_index ] = signal_max_power( *buffer, *channels, *samples, &peak );
209 // fprintf( stderr, "filter_volume: raw power %f ", smooth_buffer[ smooth_index ] );
210 if ( smooth_buffer[ smooth_index ] > EPSILON )
211 {
212 mlt_properties_set_int( filter_props, "_smooth_index", ( smooth_index + 1 ) % window );
213
214 // Smooth the data and compute the gain
215 // fprintf( stderr, "smoothed %f over %d frames\n", get_smoothed_data( smooth_buffer, window ), window );
216 gain *= amplitude / get_smoothed_data( smooth_buffer, window );
217 }
218 }
219 else
220 {
221 gain *= amplitude / signal_max_power( *buffer, *channels, *samples, &peak );
222 }
223 }
224
225 // if ( gain > 1.0 && normalise )
226 // fprintf(stderr, "filter_volume: limiter level %f gain %f\n", limiter_level, gain );
227
228 if ( max_gain > 0 && gain > max_gain )
229 gain = max_gain;
230
231 // Initialise filter's previous gain value to prevent an inadvertant jump from 0
232 if ( mlt_properties_get( filter_props, "previous_gain" ) == NULL )
233 mlt_properties_set_double( filter_props, "previous_gain", gain );
234
235 // Start the gain out at the previous
236 double previous_gain = mlt_properties_get_double( filter_props, "previous_gain" );
237
238 // Determine ramp increment
239 double gain_step = ( gain - previous_gain ) / *samples;
240 // fprintf( stderr, "filter_volume: previous gain %f current gain %f step %f\n", previous_gain, gain, gain_step );
241
242 // Save the current gain for the next iteration
243 mlt_properties_set_double( filter_props, "previous_gain", gain );
244
245 // Ramp from the previous gain to the current
246 gain = previous_gain;
247
248 int16_t *p = *buffer;
249
250 // Apply the gain
251 for ( i = 0; i < *samples; i++ )
252 {
253 for ( j = 0; j < *channels; j++ )
254 {
255 sample = *p * gain;
256 *p = ROUND( sample );
257
258 if ( gain > 1.0 )
259 {
260 /* use limiter function instead of clipping */
261 if ( normalise )
262 *p = ROUND( samplemax * limiter( sample / (double) samplemax, limiter_level ) );
263
264 /* perform clipping */
265 else if ( sample > samplemax )
266 *p = samplemax;
267 else if ( sample < samplemin )
268 *p = samplemin;
269 }
270 p++;
271 }
272 gain += gain_step;
273 }
274
275 return 0;
276 }
277
278 /** Filter processing.
279 */
280
281 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
282 {
283 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
284 mlt_properties filter_props = MLT_FILTER_PROPERTIES( this );
285
286 // Parse the gain property
287 if ( mlt_properties_get( properties, "gain" ) == NULL )
288 {
289 double gain = 1.0; // no adjustment
290
291 if ( mlt_properties_get( filter_props, "gain" ) != NULL )
292 {
293 char *p = mlt_properties_get( filter_props, "gain" );
294
295 if ( strncaseeq( p, "normalise", 9 ) )
296 mlt_properties_set( filter_props, "normalise", "" );
297 else
298 {
299 if ( strcmp( p, "" ) != 0 )
300 gain = fabs( strtod( p, &p) );
301
302 while ( isspace( *p ) )
303 p++;
304
305 /* check if "dB" is given after number */
306 if ( strncaseeq( p, "db", 2 ) )
307 gain = DBFSTOAMP( gain );
308
309 // If there is an end adjust gain to the range
310 if ( mlt_properties_get( filter_props, "end" ) != NULL )
311 {
312 // Determine the time position of this frame in the transition duration
313 mlt_position in = mlt_filter_get_in( this );
314 mlt_position out = mlt_filter_get_out( this );
315 mlt_position time = mlt_frame_get_position( frame );
316 double position = ( double )( time - in ) / ( double )( out - in + 1 );
317
318 double end = -1;
319 char *p = mlt_properties_get( filter_props, "end" );
320 if ( strcmp( p, "" ) != 0 )
321 end = fabs( strtod( p, &p) );
322
323 while ( isspace( *p ) )
324 p++;
325
326 /* check if "dB" is given after number */
327 if ( strncaseeq( p, "db", 2 ) )
328 end = DBFSTOAMP( gain );
329
330 if ( end != -1 )
331 gain += ( end - gain ) * position;
332 }
333 }
334 }
335 mlt_properties_set_double( properties, "volume.gain", gain );
336 }
337
338 // Parse the maximum gain property
339 if ( mlt_properties_get( filter_props, "max_gain" ) != NULL )
340 {
341 char *p = mlt_properties_get( filter_props, "max_gain" );
342 double gain = fabs( strtod( p, &p) ); // 0 = no max
343
344 while ( isspace( *p ) )
345 p++;
346
347 /* check if "dB" is given after number */
348 if ( strncaseeq( p, "db", 2 ) )
349 gain = DBFSTOAMP( gain );
350
351 mlt_properties_set_double( properties, "volume.max_gain", gain );
352 }
353
354 // Parse the limiter property
355 if ( mlt_properties_get( filter_props, "limiter" ) != NULL )
356 {
357 char *p = mlt_properties_get( filter_props, "limiter" );
358 double level = 0.5; /* -6dBFS */
359 if ( strcmp( p, "" ) != 0 )
360 level = strtod( p, &p);
361
362 while ( isspace( *p ) )
363 p++;
364
365 /* check if "dB" is given after number */
366 if ( strncaseeq( p, "db", 2 ) )
367 {
368 if ( level > 0 )
369 level = -level;
370 level = DBFSTOAMP( level );
371 }
372 else
373 {
374 if ( level < 0 )
375 level = -level;
376 }
377 mlt_properties_set_double( properties, "volume.limiter", level );
378 }
379
380 // Parse the normalise property
381 if ( mlt_properties_get( filter_props, "normalise" ) != NULL )
382 {
383 char *p = mlt_properties_get( filter_props, "normalise" );
384 double amplitude = 0.2511886431509580; /* -12dBFS */
385 if ( strcmp( p, "" ) != 0 )
386 amplitude = strtod( p, &p);
387
388 while ( isspace( *p ) )
389 p++;
390
391 /* check if "dB" is given after number */
392 if ( strncaseeq( p, "db", 2 ) )
393 {
394 if ( amplitude > 0 )
395 amplitude = -amplitude;
396 amplitude = DBFSTOAMP( amplitude );
397 }
398 else
399 {
400 if ( amplitude < 0 )
401 amplitude = -amplitude;
402 if ( amplitude > 1.0 )
403 amplitude = 1.0;
404 }
405
406 // If there is an end adjust gain to the range
407 if ( mlt_properties_get( filter_props, "end" ) != NULL )
408 {
409 // Determine the time position of this frame in the transition duration
410 mlt_position in = mlt_filter_get_in( this );
411 mlt_position out = mlt_filter_get_out( this );
412 mlt_position time = mlt_frame_get_position( frame );
413 double position = ( double )( time - in ) / ( double )( out - in + 1 );
414 amplitude *= position;
415 }
416 mlt_properties_set_int( properties, "volume.normalise", 1 );
417 mlt_properties_set_double( properties, "volume.amplitude", amplitude );
418 }
419
420 // Parse the window property and allocate smoothing buffer if needed
421 int window = mlt_properties_get_int( filter_props, "window" );
422 if ( mlt_properties_get( filter_props, "smooth_buffer" ) == NULL && window > 1 )
423 {
424 // Create a smoothing buffer for the calculated "max power" of frame of audio used in normalisation
425 double *smooth_buffer = (double*) calloc( window, sizeof( double ) );
426 int i;
427 for ( i = 0; i < window; i++ )
428 smooth_buffer[ i ] = -1.0;
429 mlt_properties_set_data( filter_props, "smooth_buffer", smooth_buffer, 0, free, NULL );
430 }
431
432 // Put a filter reference onto the frame
433 mlt_properties_set_data( properties, "filter_volume", this, 0, NULL, NULL );
434
435 // Override the get_audio method
436 mlt_frame_push_audio( frame, filter_get_audio );
437
438 return frame;
439 }
440
441 /** Constructor for the filter.
442 */
443
444 mlt_filter filter_volume_init( char *arg )
445 {
446 mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
447 if ( this != NULL && mlt_filter_init( this, NULL ) == 0 )
448 {
449 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
450 this->process = filter_process;
451 if ( arg != NULL )
452 mlt_properties_set( properties, "gain", arg );
453
454 mlt_properties_set_int( properties, "window", 75 );
455 mlt_properties_set( properties, "max_gain", "20dB" );
456 }
457 return this;
458 }