719e3ed7abe4130967730f3b3320e541d655024f
[melted] / src / modules / core / 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 SMOOTH_BUFFER_SIZE 50
33
34 /* This utilities and limiter function comes 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;
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 = x;
77 else
78 xp = tanh((x - lmtr_lvl) / (1-lmtr_lvl)) * (1-lmtr_lvl) + lmtr_lvl;
79
80 return xp;
81 }
82
83
84 /** Takes a full smoothing window, and returns the value of the center
85 element, smoothed.
86
87 Currently, just does a mean filter, but we could do a median or
88 gaussian filter here instead.
89 */
90 static inline double get_smoothed_data( double *buf, int count )
91 {
92 int i, j;
93 double smoothed = 0;
94
95 for ( i = 0, j = 0; i < count; i++ )
96 {
97 if ( buf[ i ] != -1.0 )
98 {
99 smoothed += buf[ i ];
100 j++;
101 }
102 }
103 smoothed /= j;
104 // fprintf( stderr, "smoothed over %d values, result %f\n", j, smoothed );
105
106 return smoothed;
107 }
108
109 /** Get the max power level (using RMS) and peak level of the audio segment.
110 */
111 double signal_max_power( int16_t *buffer, int channels, int samples, int16_t *peak )
112 {
113 // Determine numeric limits
114 int bytes_per_samp = (samp_width - 1) / 8 + 1;
115 int16_t max = (1 << (bytes_per_samp * 8 - 1)) - 1;
116 int16_t min = -max - 1;
117
118 double *sums = (double *) calloc( channels, sizeof(double) );
119 int c, i;
120 int16_t sample;
121 double pow, maxpow = 0;
122
123 /* initialize peaks to effectively -inf and +inf */
124 int16_t max_sample = min;
125 int16_t min_sample = max;
126
127 for ( i = 0; i < samples; i++ )
128 {
129 for ( c = 0; c < channels; c++ )
130 {
131 sample = *buffer++;
132 sums[ c ] += (double) sample * (double) sample;
133
134 /* track peak */
135 if ( sample > max_sample )
136 max_sample = sample;
137 else if ( sample < min_sample )
138 min_sample = sample;
139 }
140 }
141 for ( c = 0; c < channels; c++ )
142 {
143 pow = sums[ c ] / (double) samples;
144 if ( pow > maxpow )
145 maxpow = pow;
146 }
147
148 free( sums );
149
150 /* scale the pow value to be in the range 0.0 -- 1.0 */
151 maxpow /= ( (double) min * (double) min);
152
153 if ( -min_sample > max_sample )
154 *peak = min_sample / (double) min;
155 else
156 *peak = max_sample / (double) max;
157
158 return sqrt( maxpow );
159 }
160
161 /** Get the audio.
162 */
163
164 static int filter_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
165 {
166 // Get the properties of the a frame
167 mlt_properties properties = mlt_frame_properties( frame );
168 double gain = mlt_properties_get_double( properties, "gain" );
169 int use_limiter = mlt_properties_get_int( properties, "volume.use_limiter" );
170 double limiter_level = mlt_properties_get_double( properties, "volume.limiter_level" );
171 int normalise = mlt_properties_get_int( properties, "volume.normalise" );
172 double amplitude = mlt_properties_get_double( properties, "volume.amplitude" );
173 int i;
174 double sample;
175 int16_t peak;
176
177 // Restore the original get_audio
178 frame->get_audio = mlt_properties_get_data( properties, "volume.get_audio", NULL );
179
180 // Get the producer's audio
181 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
182
183 // Determine numeric limits
184 int bytes_per_samp = (samp_width - 1) / 8 + 1;
185 int samplemax = (1 << (bytes_per_samp * 8 - 1)) - 1;
186 int samplemin = -samplemax - 1;
187
188 #if 0
189 if ( gain > 1.0 && use_limiter != 0 )
190 fprintf(stderr, "filter_volume: limiting samples greater than %f\n", limiter_level );
191 #endif
192
193 if ( normalise )
194 {
195 double *smooth_buffer = mlt_properties_get_data( properties, "volume.smooth_buffer", NULL );
196 int *smooth_index = mlt_properties_get_data( properties, "volume.smooth_index", NULL );
197
198 // Compute the signal power and put into smoothing buffer
199 smooth_buffer[ *smooth_index ] = signal_max_power( *buffer, *channels, *samples, &peak );
200 *smooth_index = ( *smooth_index + 1 ) % SMOOTH_BUFFER_SIZE;
201
202 // Smooth the data and compute the gain
203 gain *= amplitude / get_smoothed_data( smooth_buffer, SMOOTH_BUFFER_SIZE );
204 }
205
206 // Apply the gain
207 for ( i = 0; i < ( *channels * *samples ); i++ )
208 {
209 sample = (*buffer)[i] * gain;
210 (*buffer)[i] = ROUND( sample );
211
212 if ( gain > 1.0 )
213 {
214 /* use limiter function instead of clipping */
215 if ( use_limiter != 0 )
216 (*buffer)[i] = ROUND( samplemax * limiter( sample / (double) samplemax, limiter_level ) );
217
218 /* perform clipping */
219 else if ( sample > samplemax )
220 (*buffer)[i] = samplemax;
221 else if ( sample < samplemin )
222 (*buffer)[i] = samplemin;
223 }
224 }
225
226 return 0;
227 }
228
229 /** Filter processing.
230 */
231
232 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
233 {
234 mlt_properties properties = mlt_frame_properties( frame );
235 mlt_properties filter_props = mlt_filter_properties( this );
236
237 // Propogate the volume/gain property
238 if ( mlt_properties_get( properties, "gain" ) == NULL )
239 {
240 double gain = 1.0; // none
241 if ( mlt_properties_get( filter_props, "volume" ) != NULL )
242 gain = mlt_properties_get_double( filter_props, "volume" );
243 if ( mlt_properties_get( filter_props, "gain" ) != NULL )
244 gain = mlt_properties_get_double( filter_props, "gain" );
245 mlt_properties_set_double( properties, "gain", gain );
246 }
247
248 // Parse and propogate the limiter property
249 if ( mlt_properties_get( filter_props, "limiter" ) != NULL )
250 {
251 char *p = mlt_properties_get( filter_props, "limiter" );
252 double level = 0.5; /* -6dBFS */
253 if ( strcmp( p, "" ) != 0 )
254 level = strtod( p, &p);
255
256 /* check if "dB" is given after number */
257 while ( isspace( *p ) )
258 p++;
259
260 if ( strncaseeq( p, "db", 2 ) )
261 {
262 if ( level > 0 )
263 level = -level;
264 level = DBFSTOAMP( level );
265 }
266 else
267 {
268 if ( level < 0 )
269 level = -level;
270 }
271 mlt_properties_set_int( properties, "volume.use_limiter", 1 );
272 mlt_properties_set_double( properties, "volume.limiter_level", level );
273 }
274
275 // Parse and propogate the normalise property
276 if ( mlt_properties_get( filter_props, "normalise" ) != NULL )
277 {
278 char *p = mlt_properties_get( filter_props, "normalise" );
279 double amplitude = 0.2511886431509580; /* -12dBFS */
280 if ( strcmp( p, "" ) != 0 )
281 amplitude = strtod( p, &p);
282
283 /* check if "dB" is given after number */
284 while ( isspace( *p ) )
285 p++;
286
287 if ( strncaseeq( p, "db", 2 ) )
288 {
289 if ( amplitude > 0 )
290 amplitude = -amplitude;
291 amplitude = DBFSTOAMP( amplitude );
292 }
293 else
294 {
295 if ( amplitude < 0 )
296 amplitude = -amplitude;
297 if ( amplitude > 1.0 )
298 amplitude = 1.0;
299 }
300 mlt_properties_set_int( properties, "volume.normalise", 1 );
301 mlt_properties_set_double( properties, "volume.amplitude", amplitude );
302 }
303
304 // Propogate the smoothing buffer properties
305 mlt_properties_set_data( properties, "volume.smooth_buffer",
306 mlt_properties_get_data( filter_props, "smooth_buffer", NULL ), 0, NULL, NULL );
307 mlt_properties_set_data( properties, "volume.smooth_index",
308 mlt_properties_get_data( filter_props, "smooth_index", NULL ), 0, NULL, NULL );
309
310 // Backup the original get_audio (it's still needed)
311 mlt_properties_set_data( properties, "volume.get_audio", frame->get_audio, 0, NULL, NULL );
312
313 // Override the get_audio method
314 frame->get_audio = filter_get_audio;
315
316 return frame;
317 }
318
319 /** Constructor for the filter.
320 */
321
322 mlt_filter filter_volume_init( char *arg )
323 {
324 mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
325 if ( this != NULL && mlt_filter_init( this, NULL ) == 0 )
326 {
327 this->process = filter_process;
328 if ( arg != NULL )
329 mlt_properties_set_double( mlt_filter_properties( this ), "volume", atof( arg ) );
330
331 // Create a smoothing buffer for the calculated "max power" of frame of audio used in normalisation
332 double *smooth_buffer = (double*) calloc( SMOOTH_BUFFER_SIZE, sizeof( double ) );
333 int i;
334 for ( i = 0; i < SMOOTH_BUFFER_SIZE; i++ )
335 smooth_buffer[ i ] = -1.0;
336 mlt_properties_set_data( mlt_filter_properties( this ), "smooth_buffer", smooth_buffer, 0, free, NULL );
337 int *smooth_index = calloc( 1, sizeof( int ) );
338 mlt_properties_set_data( mlt_filter_properties( this ), "smooth_index", smooth_index, 0, free, NULL );
339 }
340 return this;
341 }
342