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