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