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