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>
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 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(14,1,0))
41 # define ST_SSIZE_MIN SOX_SAMPLE_MIN
43 # define ST_SSIZE_MIN SOX_SSIZE_MIN
45 # define ST_SAMPLE_TO_SIGNED_WORD(d,clips) SOX_SAMPLE_TO_SIGNED_16BIT(d,clips)
50 #define BUFFER_LEN 8192
51 #define AMPLITUDE_NORM 0.2511886431509580 /* -12dBFS */
52 #define AMPLITUDE_MIN 0.00001
54 /** Compute the mean of a set of doubles skipping unset values flagged as -1
56 static inline double mean( double *buf
, int count
)
62 for ( i
= 0; i
< count
; i
++ )
64 if ( buf
[ i
] != -1.0 )
76 /** Create an effect state instance for a channels
78 static int create_effect( mlt_filter
this, char *value
, int count
, int channel
, int frequency
)
80 mlt_tokeniser tokeniser
= mlt_tokeniser_init();
82 eff_t eff
= mlt_pool_alloc( sizeof( sox_effect_t
) );
84 eff_t eff
= mlt_pool_alloc( sizeof( struct st_effect
) );
89 // Tokenise the effect specification
90 mlt_tokeniser_parse_new( tokeniser
, value
, " " );
91 if ( tokeniser
->count
< 1 )
96 //fprintf(stderr, "%s: effect %s count %d\n", __FUNCTION__, tokeniser->tokens[0], tokeniser->count );
97 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(14,1,0))
98 eff
= sox_create_effect( sox_find_effect( tokeniser
->tokens
[0] ) );
100 sox_create_effect( eff
, sox_find_effect( tokeniser
->tokens
[0] ) );
102 int opt_count
= tokeniser
->count
- 1;
104 int opt_count
= st_geteffect_opt( eff
, tokeniser
->count
, tokeniser
->tokens
);
108 if ( opt_count
!= ST_EOF
)
110 // Supply the effect parameters
112 if ( ( * eff
->handler
.getopts
)( eff
, opt_count
, &tokeniser
->tokens
[ tokeniser
->count
> 1 ?
1 : 0 ] ) == ST_SUCCESS
)
114 if ( ( * eff
->h
->getopts
)( eff
, opt_count
, &tokeniser
->tokens
[ tokeniser
->count
- opt_count
] ) == ST_SUCCESS
)
117 // Set the sox signal parameters
118 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(14,1,0))
119 eff
->in_signal
.rate
= frequency
;
120 eff
->out_signal
.rate
= frequency
;
121 eff
->in_signal
.channels
= 1;
122 eff
->out_signal
.channels
= 1;
124 eff
->ininfo
.rate
= frequency
;
125 eff
->outinfo
.rate
= frequency
;
126 eff
->ininfo
.channels
= 1;
127 eff
->outinfo
.channels
= 1;
132 if ( ( * eff
->handler
.start
)( eff
) == ST_SUCCESS
)
134 if ( ( * eff
->h
->start
)( eff
) == ST_SUCCESS
)
138 sprintf( id
, "_effect_%d_%d", count
, channel
);
140 // Save the effect state
141 mlt_properties_set_data( MLT_FILTER_PROPERTIES( this ), id
, eff
, 0, mlt_pool_release
, NULL
);
146 // Some error occurred so delete the temp effect state
148 mlt_pool_release( eff
);
150 mlt_tokeniser_close( tokeniser
);
158 static int filter_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
160 // Get the properties of the frame
161 mlt_properties properties
= MLT_FRAME_PROPERTIES( frame
);
163 // Get the filter service
164 mlt_filter filter
= mlt_frame_pop_audio( frame
);
166 // Get the filter properties
167 mlt_properties filter_properties
= MLT_FILTER_PROPERTIES( filter
);
169 // Get the properties
170 st_sample_t
*input_buffer
= mlt_properties_get_data( filter_properties
, "input_buffer", NULL
);
171 st_sample_t
*output_buffer
= mlt_properties_get_data( filter_properties
, "output_buffer", NULL
);
172 int channels_avail
= *channels
;
174 int count
= mlt_properties_get_int( filter_properties
, "_effect_count" );
176 // Get the producer's audio
177 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, &channels_avail
, samples
);
179 // Duplicate channels as necessary
180 if ( channels_avail
< *channels
)
182 int size
= *channels
* *samples
* sizeof( int16_t );
183 int16_t *new_buffer
= mlt_pool_alloc( size
);
186 // Duplicate the existing channels
187 for ( i
= 0; i
< *samples
; i
++ )
189 for ( j
= 0; j
< *channels
; j
++ )
191 new_buffer
[ ( i
* *channels
) + j
] = (*buffer
)[ ( i
* channels_avail
) + k
];
192 k
= ( k
+ 1 ) % channels_avail
;
196 // Update the audio buffer now - destroys the old
197 mlt_properties_set_data( properties
, "audio", new_buffer
, size
, ( mlt_destructor
)mlt_pool_release
, NULL
);
199 *buffer
= new_buffer
;
201 else if ( channels_avail
== 6 && *channels
== 2 )
203 // Nasty hack for ac3 5.1 audio - may be a cause of failure?
204 int size
= *channels
* *samples
* sizeof( int16_t );
205 int16_t *new_buffer
= mlt_pool_alloc( size
);
207 // Drop all but the first *channels
208 for ( i
= 0; i
< *samples
; i
++ )
210 new_buffer
[ ( i
* *channels
) + 0 ] = (*buffer
)[ ( i
* channels_avail
) + 2 ];
211 new_buffer
[ ( i
* *channels
) + 1 ] = (*buffer
)[ ( i
* channels_avail
) + 3 ];
214 // Update the audio buffer now - destroys the old
215 mlt_properties_set_data( properties
, "audio", new_buffer
, size
, ( mlt_destructor
)mlt_pool_release
, NULL
);
217 *buffer
= new_buffer
;
220 // Even though some effects are multi-channel aware, it is not reliable
221 // We must maintain a separate effect state for each channel
222 for ( i
= 0; i
< *channels
; i
++ )
225 sprintf( id
, "_effect_0_%d", i
);
227 // Get an existing effect state
228 eff_t e
= mlt_properties_get_data( filter_properties
, id
, NULL
);
230 // Validate the existing effect state
231 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(14,1,0))
232 if ( e
!= NULL
&& ( e
->in_signal
.rate
!= *frequency
||
233 e
->out_signal
.rate
!= *frequency
) )
235 if ( e
!= NULL
&& ( e
->ininfo
.rate
!= *frequency
||
236 e
->outinfo
.rate
!= *frequency
) )
240 // (Re)Create the effect state
248 // Loop over all properties
249 for ( j
= 0; j
< mlt_properties_count( filter_properties
); j
++ )
251 // Get the name of this property
252 char *name
= mlt_properties_get_name( filter_properties
, j
);
254 // If the name does not contain a . and matches effect
255 if ( !strncmp( name
, "effect", 6 ) )
257 // Get the effect specification
258 char *value
= mlt_properties_get( filter_properties
, name
);
260 // Create an instance
261 if ( create_effect( filter
, value
, count
, i
, *frequency
) == 0 )
266 // Save the number of filters
267 mlt_properties_set_int( filter_properties
, "_effect_count", count
);
270 if ( *samples
> 0 && count
> 0 )
272 st_sample_t
*p
= input_buffer
;
273 st_sample_t
*end
= p
+ *samples
;
274 int16_t *q
= *buffer
+ i
;
275 st_size_t isamp
= *samples
;
276 st_size_t osamp
= *samples
;
279 char *normalise
= mlt_properties_get( filter_properties
, "normalise" );
280 double normalised_gain
= 1.0;
281 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(13,0,0))
282 st_sample_t dummy_clipped_count
= 0;
285 // Convert to sox encoding
288 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(13,0,0))
289 *p
= ST_SIGNED_WORD_TO_SAMPLE( *q
, dummy_clipped_count
);
291 *p
= ST_SIGNED_WORD_TO_SAMPLE( *q
);
293 // Compute rms amplitude while we are accessing each sample
294 rms
+= ( double )*p
* ( double )*p
;
300 // Compute final rms amplitude
301 rms
= sqrt( rms
/ *samples
/ ST_SSIZE_MIN
/ ST_SSIZE_MIN
);
305 int window
= mlt_properties_get_int( filter_properties
, "window" );
306 double *smooth_buffer
= mlt_properties_get_data( filter_properties
, "smooth_buffer", NULL
);
307 double max_gain
= mlt_properties_get_double( filter_properties
, "max_gain" );
309 // Default the maximum gain factor to 20dBFS
313 // The smoothing buffer prevents radical shifts in the gain level
314 if ( window
> 0 && smooth_buffer
!= NULL
)
316 int smooth_index
= mlt_properties_get_int( filter_properties
, "_smooth_index" );
317 smooth_buffer
[ smooth_index
] = rms
;
319 // Ignore very small values that adversely affect the mean
320 if ( rms
> AMPLITUDE_MIN
)
321 mlt_properties_set_int( filter_properties
, "_smooth_index", ( smooth_index
+ 1 ) % window
);
323 // Smoothing is really just a mean over the past N values
324 normalised_gain
= AMPLITUDE_NORM
/ mean( smooth_buffer
, window
);
328 // Determine gain to apply as current amplitude
329 normalised_gain
= AMPLITUDE_NORM
/ rms
;
332 //printf("filter_sox: rms %.3f gain %.3f\n", rms, normalised_gain );
334 // Govern the maximum gain
335 if ( normalised_gain
> max_gain
)
336 normalised_gain
= max_gain
;
340 for ( j
= 0; j
< count
; j
++ )
342 sprintf( id
, "_effect_%d_%d", j
, i
);
343 e
= mlt_properties_get_data( filter_properties
, id
, NULL
);
345 // We better have this guy
348 float saved_gain
= 1.0;
350 // XXX: hack to apply the normalised gain level to the vol effect
352 if ( normalise
&& strcmp( e
->handler
.name
, "vol" ) == 0 )
354 if ( normalise
&& strcmp( e
->name
, "vol" ) == 0 )
357 float *f
= ( float * )( e
->priv
);
359 *f
= saved_gain
* normalised_gain
;
364 if ( ( * e
->handler
.flow
)( e
, input_buffer
, output_buffer
, &isamp
, &osamp
) == ST_SUCCESS
)
366 if ( ( * e
->h
->flow
)( e
, input_buffer
, output_buffer
, &isamp
, &osamp
) == ST_SUCCESS
)
369 // Swap input and output buffer pointers for subsequent effects
371 input_buffer
= output_buffer
;
375 // XXX: hack to restore the original vol gain to prevent accumulation
377 if ( normalise
&& strcmp( e
->handler
.name
, "vol" ) == 0 )
379 if ( normalise
&& strcmp( e
->name
, "vol" ) == 0 )
382 float *f
= ( float * )( e
->priv
);
388 // Convert back to signed 16bit
394 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(13,0,0))
395 *q
= ST_SAMPLE_TO_SIGNED_WORD( *p
++, dummy_clipped_count
);
397 *q
= ST_SAMPLE_TO_SIGNED_WORD( *p
++ );
407 /** Filter processing.
410 static mlt_frame
filter_process( mlt_filter
this, mlt_frame frame
)
412 if ( mlt_frame_is_test_audio( frame
) == 0 )
414 // Add the filter to the frame
415 mlt_frame_push_audio( frame
, this );
416 mlt_frame_push_audio( frame
, filter_get_audio
);
418 // Parse the window property and allocate smoothing buffer if needed
419 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
420 int window
= mlt_properties_get_int( properties
, "window" );
421 if ( mlt_properties_get( properties
, "smooth_buffer" ) == NULL
&& window
> 1 )
423 // Create a smoothing buffer for the calculated "max power" of frame of audio used in normalisation
424 double *smooth_buffer
= (double*) calloc( window
, sizeof( double ) );
426 for ( i
= 0; i
< window
; i
++ )
427 smooth_buffer
[ i
] = -1.0;
428 mlt_properties_set_data( properties
, "smooth_buffer", smooth_buffer
, 0, free
, NULL
);
435 /** Constructor for the filter.
438 mlt_filter
filter_sox_init( mlt_profile profile
, mlt_service_type type
, const char *id
, char *arg
)
440 mlt_filter
this = mlt_filter_new( );
443 void *input_buffer
= mlt_pool_alloc( BUFFER_LEN
);
444 void *output_buffer
= mlt_pool_alloc( BUFFER_LEN
);
445 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
447 this->process
= filter_process
;
450 mlt_properties_set( properties
, "effect", arg
);
451 mlt_properties_set_data( properties
, "input_buffer", input_buffer
, BUFFER_LEN
, mlt_pool_release
, NULL
);
452 mlt_properties_set_data( properties
, "output_buffer", output_buffer
, BUFFER_LEN
, mlt_pool_release
, NULL
);
453 mlt_properties_set_int( properties
, "window", 75 );
458 // What to do when a libst internal failure occurs
461 // Is there a build problem with my sox-devel package?
463 void gsm_create(void){}
466 void gsm_decode(void){}
469 void gsm_encode(void){}
472 void gsm_destroy(void){}
475 void gsm_option(void){}