c1676bc377339686c673de48ec5080a63165e126
[melted] / src / modules / sox / filter_sox.c
1 /*
2 * filter_sox.c -- apply any number of SOX effects using libst
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_tokeniser.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <math.h>
29
30 #ifdef SOX14
31 # include <sox.h>
32 # define ST_EOF SOX_EOF
33 # define ST_SUCCESS SOX_SUCCESS
34 # define st_sample_t sox_sample_t
35 # define eff_t sox_effect_t*
36 # define st_size_t sox_size_t
37 # define ST_LIB_VERSION_CODE SOX_LIB_VERSION_CODE
38 # define ST_LIB_VERSION SOX_LIB_VERSION
39 # define ST_SIGNED_WORD_TO_SAMPLE(d,clips) SOX_SIGNED_16BIT_TO_SAMPLE(d,clips)
40 # define ST_SSIZE_MIN SOX_SSIZE_MIN
41 # define ST_SAMPLE_TO_SIGNED_WORD(d,clips) SOX_SAMPLE_TO_SIGNED_16BIT(d,clips)
42 #else
43 # include <st.h>
44 #endif
45
46 #define BUFFER_LEN 8192
47 #define AMPLITUDE_NORM 0.2511886431509580 /* -12dBFS */
48 #define AMPLITUDE_MIN 0.00001
49
50 /** Compute the mean of a set of doubles skipping unset values flagged as -1
51 */
52 static inline double mean( double *buf, int count )
53 {
54 double mean = 0;
55 int i;
56 int j = 0;
57
58 for ( i = 0; i < count; i++ )
59 {
60 if ( buf[ i ] != -1.0 )
61 {
62 mean += buf[ i ];
63 j ++;
64 }
65 }
66 if ( j > 0 )
67 mean /= j;
68
69 return mean;
70 }
71
72 /** Create an effect state instance for a channels
73 */
74 static int create_effect( mlt_filter this, char *value, int count, int channel, int frequency )
75 {
76 mlt_tokeniser tokeniser = mlt_tokeniser_init();
77 #ifdef SOX14
78 eff_t eff = mlt_pool_alloc( sizeof( sox_effect_t ) );
79 #else
80 eff_t eff = mlt_pool_alloc( sizeof( struct st_effect ) );
81 #endif
82 char id[ 256 ];
83 int error = 1;
84
85 // Tokenise the effect specification
86 mlt_tokeniser_parse_new( tokeniser, value, " " );
87 if ( tokeniser->count < 1 )
88 return error;
89
90 // Locate the effect
91 #ifdef SOX14
92 //fprintf(stderr, "%s: effect %s count %d\n", __FUNCTION__, tokeniser->tokens[0], tokeniser->count );
93 sox_create_effect( eff, sox_find_effect( tokeniser->tokens[0] ) );
94 int opt_count = tokeniser->count - 1;
95 #else
96 int opt_count = st_geteffect_opt( eff, tokeniser->count, tokeniser->tokens );
97 #endif
98
99 // If valid effect
100 if ( opt_count != ST_EOF )
101 {
102 // Supply the effect parameters
103 #ifdef SOX14
104 if ( ( * eff->handler.getopts )( eff, opt_count, &tokeniser->tokens[ tokeniser->count > 1 ? 1 : 0 ] ) == ST_SUCCESS )
105 #else
106 if ( ( * eff->h->getopts )( eff, opt_count, &tokeniser->tokens[ tokeniser->count - opt_count ] ) == ST_SUCCESS )
107 #endif
108 {
109 // Set the sox signal parameters
110 eff->ininfo.rate = frequency;
111 eff->outinfo.rate = frequency;
112 eff->ininfo.channels = 1;
113 eff->outinfo.channels = 1;
114
115 // Start the effect
116 #ifdef SOX14
117 if ( ( * eff->handler.start )( eff ) == ST_SUCCESS )
118 #else
119 if ( ( * eff->h->start )( eff ) == ST_SUCCESS )
120 #endif
121 {
122 // Construct id
123 sprintf( id, "_effect_%d_%d", count, channel );
124
125 // Save the effect state
126 mlt_properties_set_data( MLT_FILTER_PROPERTIES( this ), id, eff, 0, mlt_pool_release, NULL );
127 error = 0;
128 }
129 }
130 }
131 // Some error occurred so delete the temp effect state
132 if ( error == 1 )
133 mlt_pool_release( eff );
134
135 mlt_tokeniser_close( tokeniser );
136
137 return error;
138 }
139
140 /** Get the audio.
141 */
142
143 static int filter_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
144 {
145 // Get the properties of the frame
146 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
147
148 // Get the filter service
149 mlt_filter filter = mlt_frame_pop_audio( frame );
150
151 // Get the filter properties
152 mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
153
154 // Get the properties
155 st_sample_t *input_buffer = mlt_properties_get_data( filter_properties, "input_buffer", NULL );
156 st_sample_t *output_buffer = mlt_properties_get_data( filter_properties, "output_buffer", NULL );
157 int channels_avail = *channels;
158 int i; // channel
159 int count = mlt_properties_get_int( filter_properties, "_effect_count" );
160
161 // Get the producer's audio
162 mlt_frame_get_audio( frame, buffer, format, frequency, &channels_avail, samples );
163
164 // Duplicate channels as necessary
165 if ( channels_avail < *channels )
166 {
167 int size = *channels * *samples * sizeof( int16_t );
168 int16_t *new_buffer = mlt_pool_alloc( size );
169 int j, k = 0;
170
171 // Duplicate the existing channels
172 for ( i = 0; i < *samples; i++ )
173 {
174 for ( j = 0; j < *channels; j++ )
175 {
176 new_buffer[ ( i * *channels ) + j ] = (*buffer)[ ( i * channels_avail ) + k ];
177 k = ( k + 1 ) % channels_avail;
178 }
179 }
180
181 // Update the audio buffer now - destroys the old
182 mlt_properties_set_data( properties, "audio", new_buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
183
184 *buffer = new_buffer;
185 }
186 else if ( channels_avail == 6 && *channels == 2 )
187 {
188 // Nasty hack for ac3 5.1 audio - may be a cause of failure?
189 int size = *channels * *samples * sizeof( int16_t );
190 int16_t *new_buffer = mlt_pool_alloc( size );
191
192 // Drop all but the first *channels
193 for ( i = 0; i < *samples; i++ )
194 {
195 new_buffer[ ( i * *channels ) + 0 ] = (*buffer)[ ( i * channels_avail ) + 2 ];
196 new_buffer[ ( i * *channels ) + 1 ] = (*buffer)[ ( i * channels_avail ) + 3 ];
197 }
198
199 // Update the audio buffer now - destroys the old
200 mlt_properties_set_data( properties, "audio", new_buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
201
202 *buffer = new_buffer;
203 }
204
205 // Even though some effects are multi-channel aware, it is not reliable
206 // We must maintain a separate effect state for each channel
207 for ( i = 0; i < *channels; i++ )
208 {
209 char id[ 256 ];
210 sprintf( id, "_effect_0_%d", i );
211
212 // Get an existing effect state
213 eff_t e = mlt_properties_get_data( filter_properties, id, NULL );
214
215 // Validate the existing effect state
216 if ( e != NULL && ( e->ininfo.rate != *frequency ||
217 e->outinfo.rate != *frequency ) )
218 e = NULL;
219
220 // (Re)Create the effect state
221 if ( e == NULL )
222 {
223 int j = 0;
224
225 // Reset the count
226 count = 0;
227
228 // Loop over all properties
229 for ( j = 0; j < mlt_properties_count( filter_properties ); j ++ )
230 {
231 // Get the name of this property
232 char *name = mlt_properties_get_name( filter_properties, j );
233
234 // If the name does not contain a . and matches effect
235 if ( !strncmp( name, "effect", 6 ) )
236 {
237 // Get the effect specification
238 char *value = mlt_properties_get( filter_properties, name );
239
240 // Create an instance
241 if ( create_effect( filter, value, count, i, *frequency ) == 0 )
242 count ++;
243 }
244 }
245
246 // Save the number of filters
247 mlt_properties_set_int( filter_properties, "_effect_count", count );
248
249 }
250 if ( *samples > 0 && count > 0 )
251 {
252 st_sample_t *p = input_buffer;
253 st_sample_t *end = p + *samples;
254 int16_t *q = *buffer + i;
255 st_size_t isamp = *samples;
256 st_size_t osamp = *samples;
257 double rms = 0;
258 int j;
259 char *normalise = mlt_properties_get( filter_properties, "normalise" );
260 double normalised_gain = 1.0;
261 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(13,0,0))
262 st_sample_t dummy_clipped_count = 0;
263 #endif
264
265 // Convert to sox encoding
266 while( p != end )
267 {
268 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(13,0,0))
269 *p = ST_SIGNED_WORD_TO_SAMPLE( *q, dummy_clipped_count );
270 #else
271 *p = ST_SIGNED_WORD_TO_SAMPLE( *q );
272 #endif
273 // Compute rms amplitude while we are accessing each sample
274 rms += ( double )*p * ( double )*p;
275
276 p ++;
277 q += *channels;
278 }
279
280 // Compute final rms amplitude
281 rms = sqrt( rms / *samples / ST_SSIZE_MIN / ST_SSIZE_MIN );
282
283 if ( normalise )
284 {
285 int window = mlt_properties_get_int( filter_properties, "window" );
286 double *smooth_buffer = mlt_properties_get_data( filter_properties, "smooth_buffer", NULL );
287 double max_gain = mlt_properties_get_double( filter_properties, "max_gain" );
288
289 // Default the maximum gain factor to 20dBFS
290 if ( max_gain == 0 )
291 max_gain = 10.0;
292
293 // The smoothing buffer prevents radical shifts in the gain level
294 if ( window > 0 && smooth_buffer != NULL )
295 {
296 int smooth_index = mlt_properties_get_int( filter_properties, "_smooth_index" );
297 smooth_buffer[ smooth_index ] = rms;
298
299 // Ignore very small values that adversely affect the mean
300 if ( rms > AMPLITUDE_MIN )
301 mlt_properties_set_int( filter_properties, "_smooth_index", ( smooth_index + 1 ) % window );
302
303 // Smoothing is really just a mean over the past N values
304 normalised_gain = AMPLITUDE_NORM / mean( smooth_buffer, window );
305 }
306 else if ( rms > 0 )
307 {
308 // Determine gain to apply as current amplitude
309 normalised_gain = AMPLITUDE_NORM / rms;
310 }
311
312 //printf("filter_sox: rms %.3f gain %.3f\n", rms, normalised_gain );
313
314 // Govern the maximum gain
315 if ( normalised_gain > max_gain )
316 normalised_gain = max_gain;
317 }
318
319 // For each effect
320 for ( j = 0; j < count; j++ )
321 {
322 sprintf( id, "_effect_%d_%d", j, i );
323 e = mlt_properties_get_data( filter_properties, id, NULL );
324
325 // We better have this guy
326 if ( e != NULL )
327 {
328 float saved_gain = 1.0;
329
330 // XXX: hack to apply the normalised gain level to the vol effect
331 #ifdef SOX14
332 if ( normalise && strcmp( e->handler.name, "vol" ) == 0 )
333 #else
334 if ( normalise && strcmp( e->name, "vol" ) == 0 )
335 #endif
336 {
337 float *f = ( float * )( e->priv );
338 saved_gain = *f;
339 *f = saved_gain * normalised_gain;
340 }
341
342 // Apply the effect
343 #ifdef SOX14
344 if ( ( * e->handler.flow )( e, input_buffer, output_buffer, &isamp, &osamp ) == ST_SUCCESS )
345 #else
346 if ( ( * e->h->flow )( e, input_buffer, output_buffer, &isamp, &osamp ) == ST_SUCCESS )
347 #endif
348 {
349 // Swap input and output buffer pointers for subsequent effects
350 p = input_buffer;
351 input_buffer = output_buffer;
352 output_buffer = p;
353 }
354
355 // XXX: hack to restore the original vol gain to prevent accumulation
356 #ifdef SOX14
357 if ( normalise && strcmp( e->handler.name, "vol" ) == 0 )
358 #else
359 if ( normalise && strcmp( e->name, "vol" ) == 0 )
360 #endif
361 {
362 float *f = ( float * )( e->priv );
363 *f = saved_gain;
364 }
365 }
366 }
367
368 // Convert back to signed 16bit
369 p = input_buffer;
370 q = *buffer + i;
371 end = p + *samples;
372 while ( p != end )
373 {
374 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(13,0,0))
375 *q = ST_SAMPLE_TO_SIGNED_WORD( *p ++, dummy_clipped_count );
376 #else
377 *q = ST_SAMPLE_TO_SIGNED_WORD( *p ++ );
378 #endif
379 q += *channels;
380 }
381 }
382 }
383
384 return 0;
385 }
386
387 /** Filter processing.
388 */
389
390 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
391 {
392 if ( mlt_frame_is_test_audio( frame ) == 0 )
393 {
394 // Add the filter to the frame
395 mlt_frame_push_audio( frame, this );
396 mlt_frame_push_audio( frame, filter_get_audio );
397
398 // Parse the window property and allocate smoothing buffer if needed
399 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
400 int window = mlt_properties_get_int( properties, "window" );
401 if ( mlt_properties_get( properties, "smooth_buffer" ) == NULL && window > 1 )
402 {
403 // Create a smoothing buffer for the calculated "max power" of frame of audio used in normalisation
404 double *smooth_buffer = (double*) calloc( window, sizeof( double ) );
405 int i;
406 for ( i = 0; i < window; i++ )
407 smooth_buffer[ i ] = -1.0;
408 mlt_properties_set_data( properties, "smooth_buffer", smooth_buffer, 0, free, NULL );
409 }
410 }
411
412 return frame;
413 }
414
415 /** Constructor for the filter.
416 */
417
418 mlt_filter filter_sox_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
419 {
420 mlt_filter this = mlt_filter_new( );
421 if ( this != NULL )
422 {
423 void *input_buffer = mlt_pool_alloc( BUFFER_LEN );
424 void *output_buffer = mlt_pool_alloc( BUFFER_LEN );
425 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
426
427 this->process = filter_process;
428
429 if ( arg != NULL )
430 mlt_properties_set( properties, "effect", arg );
431 mlt_properties_set_data( properties, "input_buffer", input_buffer, BUFFER_LEN, mlt_pool_release, NULL );
432 mlt_properties_set_data( properties, "output_buffer", output_buffer, BUFFER_LEN, mlt_pool_release, NULL );
433 mlt_properties_set_int( properties, "window", 75 );
434 }
435 return this;
436 }
437
438 // What to do when a libst internal failure occurs
439 void cleanup(void){}
440
441 // Is there a build problem with my sox-devel package?
442 #ifndef gsm_create
443 void gsm_create(void){}
444 #endif
445 #ifndef gsm_decode
446 void gsm_decode(void){}
447 #endif
448 #ifndef gdm_encode
449 void gsm_encode(void){}
450 #endif
451 #ifndef gsm_destroy
452 void gsm_destroy(void){}
453 #endif
454 #ifndef gsm_option
455 void gsm_option(void){}
456 #endif