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