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