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