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