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>
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.
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.
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
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_tokeniser.h>
24 #include <framework/mlt_log.h>
31 // TODO: does not support multiple effects with SoX v14.1.0+
35 # define ST_EOF SOX_EOF
36 # define ST_SUCCESS SOX_SUCCESS
37 # define st_sample_t sox_sample_t
38 # define eff_t sox_effect_t*
39 # define ST_LIB_VERSION_CODE SOX_LIB_VERSION_CODE
40 # define ST_LIB_VERSION SOX_LIB_VERSION
41 # if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(14,2,0))
42 # define st_size_t size_t
44 # define st_size_t sox_size_t
46 # define ST_SIGNED_WORD_TO_SAMPLE(d,clips) SOX_SIGNED_16BIT_TO_SAMPLE(d,clips)
47 # if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(14,1,0))
48 # define ST_SSIZE_MIN SOX_SAMPLE_MIN
50 # define ST_SSIZE_MIN SOX_SSIZE_MIN
52 # define ST_SAMPLE_TO_SIGNED_WORD(d,clips) SOX_SAMPLE_TO_SIGNED_16BIT(d,clips)
57 #define BUFFER_LEN 8192
58 #define AMPLITUDE_NORM 0.2511886431509580 /* -12dBFS */
59 #define AMPLITUDE_MIN 0.00001
61 /** Compute the mean of a set of doubles skipping unset values flagged as -1
63 static inline double mean( double *buf, int count )
69 for ( i = 0; i < count; i++ )
71 if ( buf[ i ] != -1.0 )
83 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(14,1,0))
84 static void delete_effect( eff_t effp )
87 free( (void*)effp->in_encoding );
92 /** Create an effect state instance for a channels
94 static int create_effect( mlt_filter this, char *value, int count, int channel, int frequency )
96 mlt_tokeniser tokeniser = mlt_tokeniser_init();
100 // Tokenise the effect specification
101 mlt_tokeniser_parse_new( tokeniser, value, " " );
102 if ( tokeniser->count < 1 )
106 mlt_destructor effect_destructor = mlt_pool_release;
108 //fprintf(stderr, "%s: effect %s count %d\n", __FUNCTION__, tokeniser->tokens[0], tokeniser->count );
109 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(14,1,0))
110 sox_effect_handler_t const *eff_handle = sox_find_effect( tokeniser->tokens[0] );
111 if (eff_handle == NULL ) return error;
112 eff_t eff = sox_create_effect( eff_handle );
113 effect_destructor = ( mlt_destructor ) delete_effect;
114 sox_encodinginfo_t *enc = calloc( 1, sizeof( sox_encodinginfo_t ) );
115 enc->encoding = SOX_ENCODING_SIGN2;
116 enc->bits_per_sample = 16;
117 eff->in_encoding = eff->out_encoding = enc;
119 eff_t eff = mlt_pool_alloc( sizeof( sox_effect_t ) );
120 sox_create_effect( eff, sox_find_effect( tokeniser->tokens[0] ) );
122 int opt_count = tokeniser->count - 1;
124 eff_t eff = mlt_pool_alloc( sizeof( struct st_effect ) );
125 int opt_count = st_geteffect_opt( eff, tokeniser->count, tokeniser->tokens );
129 if ( opt_count != ST_EOF )
131 // Supply the effect parameters
133 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(14,2,0))
134 if ( sox_effect_options( eff, opt_count, &tokeniser->tokens[ tokeniser->count > 1 ? 1 : 0 ] ) == ST_SUCCESS )
136 if ( ( * eff->handler.getopts )( eff, opt_count, &tokeniser->tokens[ tokeniser->count > 1 ? 1 : 0 ] ) == ST_SUCCESS )
139 if ( ( * eff->h->getopts )( eff, opt_count, &tokeniser->tokens[ tokeniser->count - opt_count ] ) == ST_SUCCESS )
142 // Set the sox signal parameters
143 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(14,1,0))
144 eff->in_signal.rate = frequency;
145 eff->out_signal.rate = frequency;
146 eff->in_signal.channels = 1;
147 eff->out_signal.channels = 1;
148 eff->in_signal.precision = 16;
149 eff->out_signal.precision = 16;
150 eff->in_signal.length = 0;
151 eff->out_signal.length = 0;
153 eff->ininfo.rate = frequency;
154 eff->outinfo.rate = frequency;
155 eff->ininfo.channels = 1;
156 eff->outinfo.channels = 1;
161 if ( ( * eff->handler.start )( eff ) == ST_SUCCESS )
163 if ( ( * eff->h->start )( eff ) == ST_SUCCESS )
167 sprintf( id, "_effect_%d_%d", count, channel );
169 // Save the effect state
170 mlt_properties_set_data( MLT_FILTER_PROPERTIES( this ), id, eff, 0, effect_destructor, NULL );
175 // Some error occurred so delete the temp effect state
177 effect_destructor( eff );
179 mlt_tokeniser_close( tokeniser );
187 static int filter_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
189 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(14,3,0))
192 // Get the filter service
193 mlt_filter filter = mlt_frame_pop_audio( frame );
195 // Get the filter properties
196 mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
198 mlt_service_lock( MLT_FILTER_SERVICE( filter ) );
200 // Get the properties
201 st_sample_t *input_buffer;// = mlt_properties_get_data( filter_properties, "input_buffer", NULL );
202 st_sample_t *output_buffer = mlt_properties_get_data( filter_properties, "output_buffer", NULL );
204 int count = mlt_properties_get_int( filter_properties, "_effect_count" );
206 // Get the producer's audio
207 *format = mlt_audio_s32;
208 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
210 // Even though some effects are multi-channel aware, it is not reliable
211 // We must maintain a separate effect state for each channel
212 for ( i = 0; i < *channels; i++ )
215 sprintf( id, "_effect_0_%d", i );
217 // Get an existing effect state
218 eff_t e = mlt_properties_get_data( filter_properties, id, NULL );
220 // Validate the existing effect state
221 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(14,1,0))
222 if ( e != NULL && ( e->in_signal.rate != *frequency ||
223 e->out_signal.rate != *frequency ) )
225 if ( e != NULL && ( e->ininfo.rate != *frequency ||
226 e->outinfo.rate != *frequency ) )
230 // (Re)Create the effect state
238 // Loop over all properties
239 for ( j = 0; j < mlt_properties_count( filter_properties ); j ++ )
241 // Get the name of this property
242 char *name = mlt_properties_get_name( filter_properties, j );
244 // If the name does not contain a . and matches effect
245 if ( !strncmp( name, "effect", 6 ) )
247 // Get the effect specification
248 char *value = mlt_properties_get( filter_properties, name );
250 // Create an instance
251 if ( create_effect( filter, value, count, i, *frequency ) == 0 )
256 // Save the number of filters
257 mlt_properties_set_int( filter_properties, "_effect_count", count );
260 if ( *samples > 0 && count > 0 )
262 input_buffer = (st_sample_t*) *buffer + i * *samples;
263 st_sample_t *p = input_buffer;
264 st_size_t isamp = *samples;
265 st_size_t osamp = *samples;
267 int j = *samples + 1;
268 char *normalise = mlt_properties_get( filter_properties, "normalise" );
269 double normalised_gain = 1.0;
271 // Convert from interleaved
274 // Compute rms amplitude while we are accessing each sample
275 rms += ( double )*p * ( double )*p;
279 // Compute final rms amplitude
280 rms = sqrt( rms / *samples / ST_SSIZE_MIN / ST_SSIZE_MIN );
284 int window = mlt_properties_get_int( filter_properties, "window" );
285 double *smooth_buffer = mlt_properties_get_data( filter_properties, "smooth_buffer", NULL );
286 double max_gain = mlt_properties_get_double( filter_properties, "max_gain" );
288 // Default the maximum gain factor to 20dBFS
292 // The smoothing buffer prevents radical shifts in the gain level
293 if ( window > 0 && smooth_buffer != NULL )
295 int smooth_index = mlt_properties_get_int( filter_properties, "_smooth_index" );
296 smooth_buffer[ smooth_index ] = rms;
298 // Ignore very small values that adversely affect the mean
299 if ( rms > AMPLITUDE_MIN )
300 mlt_properties_set_int( filter_properties, "_smooth_index", ( smooth_index + 1 ) % window );
302 // Smoothing is really just a mean over the past N values
303 normalised_gain = AMPLITUDE_NORM / mean( smooth_buffer, window );
307 // Determine gain to apply as current amplitude
308 normalised_gain = AMPLITUDE_NORM / rms;
311 //printf("filter_sox: rms %.3f gain %.3f\n", rms, normalised_gain );
313 // Govern the maximum gain
314 if ( normalised_gain > max_gain )
315 normalised_gain = max_gain;
319 for ( j = 0; j < count; j++ )
321 sprintf( id, "_effect_%d_%d", j, i );
322 e = mlt_properties_get_data( filter_properties, id, NULL );
324 // We better have this guy
327 float saved_gain = 1.0;
329 // XXX: hack to apply the normalised gain level to the vol effect
331 if ( normalise && strcmp( e->handler.name, "vol" ) == 0 )
333 if ( normalise && strcmp( e->name, "vol" ) == 0 )
336 float *f = ( float * )( e->priv );
338 *f = saved_gain * normalised_gain;
343 if ( ( * e->handler.flow )( e, input_buffer, output_buffer, &isamp, &osamp ) != ST_SUCCESS )
345 if ( ( * e->h->flow )( e, input_buffer, output_buffer, &isamp, &osamp ) != ST_SUCCESS )
348 mlt_log_warning( MLT_FILTER_SERVICE(filter), "effect processing failed\n" );
351 // XXX: hack to restore the original vol gain to prevent accumulation
353 if ( normalise && strcmp( e->handler.name, "vol" ) == 0 )
355 if ( normalise && strcmp( e->name, "vol" ) == 0 )
358 float *f = ( float * )( e->priv );
365 memcpy( input_buffer, output_buffer, *samples * sizeof(st_sample_t) );
369 mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
374 /** Filter processing.
377 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
379 if ( mlt_frame_is_test_audio( frame ) == 0 )
381 // Add the filter to the frame
382 mlt_frame_push_audio( frame, this );
383 mlt_frame_push_audio( frame, filter_get_audio );
385 // Parse the window property and allocate smoothing buffer if needed
386 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
387 int window = mlt_properties_get_int( properties, "window" );
388 if ( mlt_properties_get( properties, "smooth_buffer" ) == NULL && window > 1 )
390 // Create a smoothing buffer for the calculated "max power" of frame of audio used in normalisation
391 double *smooth_buffer = (double*) calloc( window, sizeof( double ) );
393 for ( i = 0; i < window; i++ )
394 smooth_buffer[ i ] = -1.0;
395 mlt_properties_set_data( properties, "smooth_buffer", smooth_buffer, 0, free, NULL );
402 /** Constructor for the filter.
405 mlt_filter filter_sox_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
407 mlt_filter this = mlt_filter_new( );
410 void *input_buffer = mlt_pool_alloc( BUFFER_LEN );
411 void *output_buffer = mlt_pool_alloc( BUFFER_LEN );
412 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
414 this->process = filter_process;
417 mlt_properties_set( properties, "effect", arg );
418 mlt_properties_set_data( properties, "input_buffer", input_buffer, BUFFER_LEN, mlt_pool_release, NULL );
419 mlt_properties_set_data( properties, "output_buffer", output_buffer, BUFFER_LEN, mlt_pool_release, NULL );
420 mlt_properties_set_int( properties, "window", 75 );
425 // What to do when a libst internal failure occurs
428 // Is there a build problem with my sox-devel package?
430 void gsm_create(void){}
433 void gsm_decode(void){}
436 void gsm_encode(void){}
439 void gsm_destroy(void){}
442 void gsm_option(void){}