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 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.
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.
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.
21 #include "filter_sox.h"
23 #include <framework/mlt_frame.h>
24 #include <framework/mlt_tokeniser.h>
33 #define BUFFER_LEN 8192
34 #define AMPLITUDE_NORM 0.2511886431509580 /* -12dBFS */
35 #define AMPLITUDE_MIN 0.00001
37 /** Compute the mean of a set of doubles skipping unset values flagged as -1
39 static inline double mean( double *buf, int count )
45 for ( i = 0; i < count; i++ )
47 if ( buf[ i ] != -1.0 )
59 /** Create an effect state instance for a channels
61 static int create_effect( mlt_filter this, char *value, int count, int channel, int frequency )
63 mlt_tokeniser tokeniser = mlt_tokeniser_init();
64 eff_t eff = mlt_pool_alloc( sizeof( struct st_effect ) );
68 // Tokenise the effect specification
69 mlt_tokeniser_parse_new( tokeniser, value, " " );
72 int opt_count = st_geteffect_opt( eff, tokeniser->count, tokeniser->tokens );
75 if ( opt_count != ST_EOF )
77 // Supply the effect parameters
78 if ( ( * eff->h->getopts )( eff, opt_count, &tokeniser->tokens[ tokeniser->count - opt_count ] ) == ST_SUCCESS )
80 // Set the sox signal parameters
81 eff->ininfo.rate = frequency;
82 eff->outinfo.rate = frequency;
83 eff->ininfo.channels = 1;
84 eff->outinfo.channels = 1;
87 if ( ( * eff->h->start )( eff ) == ST_SUCCESS )
90 sprintf( id, "_effect_%d_%d", count, channel );
92 // Save the effect state
93 mlt_properties_set_data( MLT_FILTER_PROPERTIES( this ), id, eff, 0, mlt_pool_release, NULL );
98 // Some error occurred so delete the temp effect state
100 mlt_pool_release( eff );
102 mlt_tokeniser_close( tokeniser );
110 static int filter_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
112 // Get the properties of the frame
113 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
115 // Get the filter service
116 mlt_filter filter = mlt_frame_pop_audio( frame );
118 // Get the filter properties
119 mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
121 // Get the properties
122 st_sample_t *input_buffer = mlt_properties_get_data( filter_properties, "input_buffer", NULL );
123 st_sample_t *output_buffer = mlt_properties_get_data( filter_properties, "output_buffer", NULL );
124 int channels_avail = *channels;
126 int count = mlt_properties_get_int( filter_properties, "effect_count" );
128 // Get the producer's audio
129 mlt_frame_get_audio( frame, buffer, format, frequency, &channels_avail, samples );
131 // Duplicate channels as necessary
132 if ( channels_avail < *channels )
134 int size = *channels * *samples * sizeof( int16_t );
135 int16_t *new_buffer = mlt_pool_alloc( size );
138 // Duplicate the existing channels
139 for ( i = 0; i < *samples; i++ )
141 for ( j = 0; j < *channels; j++ )
143 new_buffer[ ( i * *channels ) + j ] = (*buffer)[ ( i * channels_avail ) + k ];
144 k = ( k + 1 ) % channels_avail;
148 // Update the audio buffer now - destroys the old
149 mlt_properties_set_data( properties, "audio", new_buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
151 *buffer = new_buffer;
153 else if ( channels_avail == 6 && *channels == 2 )
155 // Nasty hack for ac3 5.1 audio - may be a cause of failure?
156 int size = *channels * *samples * sizeof( int16_t );
157 int16_t *new_buffer = mlt_pool_alloc( size );
159 // Drop all but the first *channels
160 for ( i = 0; i < *samples; i++ )
162 new_buffer[ ( i * *channels ) + 0 ] = (*buffer)[ ( i * channels_avail ) + 2 ];
163 new_buffer[ ( i * *channels ) + 1 ] = (*buffer)[ ( i * channels_avail ) + 3 ];
166 // Update the audio buffer now - destroys the old
167 mlt_properties_set_data( properties, "audio", new_buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
169 *buffer = new_buffer;
172 // Even though some effects are multi-channel aware, it is not reliable
173 // We must maintain a separate effect state for each channel
174 for ( i = 0; i < *channels; i++ )
177 sprintf( id, "_effect_0_%d", i );
179 // Get an existing effect state
180 eff_t e = mlt_properties_get_data( filter_properties, id, NULL );
182 // Validate the existing effect state
183 if ( e != NULL && ( e->ininfo.rate != *frequency ||
184 e->outinfo.rate != *frequency ) )
187 // (Re)Create the effect state
195 // Loop over all properties
196 for ( j = 0; j < mlt_properties_count( filter_properties ); j ++ )
198 // Get the name of this property
199 char *name = mlt_properties_get_name( filter_properties, j );
201 // If the name does not contain a . and matches effect
202 if ( !strncmp( name, "effect", 6 ) )
204 // Get the effect specification
205 char *value = mlt_properties_get( filter_properties, name );
207 // Create an instance
208 if ( create_effect( filter, value, count, i, *frequency ) == 0 )
213 // Save the number of filters
214 mlt_properties_set_int( filter_properties, "effect_count", count );
217 if ( *samples > 0 && count > 0 )
219 st_sample_t *p = input_buffer;
220 st_sample_t *end = p + *samples;
221 int16_t *q = *buffer + i;
222 st_size_t isamp = *samples;
223 st_size_t osamp = *samples;
226 char *normalise = mlt_properties_get( filter_properties, "normalise" );
227 double normalised_gain = 1.0;
229 // Convert to sox encoding
232 *p = ST_SIGNED_WORD_TO_SAMPLE( *q );
234 // Compute rms amplitude while we are accessing each sample
235 rms += ( double )*p * ( double )*p;
241 // Compute final rms amplitude
242 rms = sqrt( rms / *samples / ST_SSIZE_MIN / ST_SSIZE_MIN );
246 int window = mlt_properties_get_int( filter_properties, "window" );
247 double *smooth_buffer = mlt_properties_get_data( filter_properties, "smooth_buffer", NULL );
248 double max_gain = mlt_properties_get_double( filter_properties, "max_gain" );
250 // Default the maximum gain factor to 20dBFS
254 // The smoothing buffer prevents radical shifts in the gain level
255 if ( window > 0 && smooth_buffer != NULL )
257 int smooth_index = mlt_properties_get_int( filter_properties, "_smooth_index" );
258 smooth_buffer[ smooth_index ] = rms;
260 // Ignore very small values that adversely affect the mean
261 if ( rms > AMPLITUDE_MIN )
262 mlt_properties_set_int( filter_properties, "_smooth_index", ( smooth_index + 1 ) % window );
264 // Smoothing is really just a mean over the past N values
265 normalised_gain = AMPLITUDE_NORM / mean( smooth_buffer, window );
269 // Determine gain to apply as current amplitude
270 normalised_gain = AMPLITUDE_NORM / rms;
273 //printf("filter_sox: rms %.3f gain %.3f\n", rms, normalised_gain );
275 // Govern the maximum gain
276 if ( normalised_gain > max_gain )
277 normalised_gain = max_gain;
281 for ( j = 0; j < count; j++ )
283 sprintf( id, "_effect_%d_%d", j, i );
284 e = mlt_properties_get_data( filter_properties, id, NULL );
286 // We better have this guy
289 float saved_gain = 1.0;
291 // XXX: hack to apply the normalised gain level to the vol effect
292 if ( normalise && strcmp( e->name, "vol" ) == 0 )
294 float *f = ( float * )( e->priv );
296 *f = saved_gain * normalised_gain;
300 if ( ( * e->h->flow )( e, input_buffer, output_buffer, &isamp, &osamp ) == ST_SUCCESS )
302 // Swap input and output buffer pointers for subsequent effects
304 input_buffer = output_buffer;
308 // XXX: hack to restore the original vol gain to prevent accumulation
309 if ( normalise && strcmp( e->name, "vol" ) == 0 )
311 float *f = ( float * )( e->priv );
317 // Convert back to signed 16bit
323 *q = ST_SAMPLE_TO_SIGNED_WORD( *p ++ );
332 /** Filter processing.
335 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
337 if ( mlt_frame_is_test_audio( frame ) != 0 )
339 // Add the filter to the frame
340 mlt_frame_push_audio( frame, this );
341 mlt_frame_push_audio( frame, filter_get_audio );
343 // Parse the window property and allocate smoothing buffer if needed
344 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
345 int window = mlt_properties_get_int( properties, "window" );
346 if ( mlt_properties_get( properties, "smooth_buffer" ) == NULL && window > 1 )
348 // Create a smoothing buffer for the calculated "max power" of frame of audio used in normalisation
349 double *smooth_buffer = (double*) calloc( window, sizeof( double ) );
351 for ( i = 0; i < window; i++ )
352 smooth_buffer[ i ] = -1.0;
353 mlt_properties_set_data( properties, "smooth_buffer", smooth_buffer, 0, free, NULL );
360 /** Constructor for the filter.
363 mlt_filter filter_sox_init( char *arg )
365 mlt_filter this = mlt_filter_new( );
368 void *input_buffer = mlt_pool_alloc( BUFFER_LEN );
369 void *output_buffer = mlt_pool_alloc( BUFFER_LEN );
370 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
372 this->process = filter_process;
375 mlt_properties_set( properties, "effect", arg );
376 mlt_properties_set_data( properties, "input_buffer", input_buffer, BUFFER_LEN, mlt_pool_release, NULL );
377 mlt_properties_set_data( properties, "output_buffer", output_buffer, BUFFER_LEN, mlt_pool_release, NULL );
378 mlt_properties_set_int( properties, "window", 75 );
383 // What to do when a libst internal failure occurs
386 // Is there a build problem with my sox-devel package?
388 void gsm_create(void){}
391 void gsm_decode(void){}
394 void gsm_encode(void){}
397 void gsm_destroy(void){}
400 void gsm_option(void){}