]> git.sesse.net Git - mlt/blob - src/modules/sox/filter_sox.c
b707f5b3d074d51169b1461990b1700f26576822
[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 program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "filter_sox.h"
22
23 #include <framework/mlt_frame.h>
24 #include <framework/mlt_tokeniser.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <math.h>
30
31 #include <st.h>
32
33 #define BUFFER_LEN 8192
34 #define AMPLITUDE_NORM 0.2511886431509580 /* -12dBFS */
35 #define AMPLITUDE_MIN 0.00001
36
37 /** Compute the mean of a set of doubles skipping unset values flagged as -1
38 */
39 static inline double mean( double *buf, int count )
40 {
41         double mean = 0;
42         int i;
43         int j = 0;
44         
45         for ( i = 0; i < count; i++ )
46         {
47                 if ( buf[ i ] != -1.0 )
48                 {
49                         mean += buf[ i ];
50                         j ++;
51                 }
52         }
53         if ( j > 0 )
54                 mean /= j;
55         
56         return mean;
57 }
58
59 /** Create an effect state instance for a channels
60 */
61 static int create_effect( mlt_filter this, char *value, int count, int channel, int frequency )
62 {
63         mlt_tokeniser tokeniser = mlt_tokeniser_init();
64         eff_t eff = mlt_pool_alloc( sizeof( struct st_effect ) );
65         char id[ 256 ];
66         int error = 1;
67
68         // Tokenise the effect specification
69         mlt_tokeniser_parse_new( tokeniser, value, " " );
70
71         // Locate the effect
72         int opt_count = st_geteffect_opt( eff, tokeniser->count, tokeniser->tokens );
73         
74         // If valid effect
75         if ( opt_count != ST_EOF )
76         {
77                 // Supply the effect parameters
78                 if ( ( * eff->h->getopts )( eff, opt_count, &tokeniser->tokens[ tokeniser->count - opt_count ] ) == ST_SUCCESS )
79                 {
80                         // Set the sox signal parameters
81                         eff->ininfo.rate = frequency;
82                         eff->outinfo.rate = frequency;
83                         eff->ininfo.channels = 1;
84                         eff->outinfo.channels = 1;
85                         
86                         // Start the effect
87                         if ( ( * eff->h->start )( eff ) == ST_SUCCESS )
88                         {
89                                 // Construct id
90                                 sprintf( id, "_effect_%d_%d", count, channel );
91
92                                 // Save the effect state
93                                 mlt_properties_set_data( mlt_filter_properties( this ), id, eff, 0, mlt_pool_release, NULL );
94                                 error = 0;
95                         }
96                 }
97         }
98         // Some error occurred so delete the temp effect state
99         if ( error == 1 )
100                 mlt_pool_release( eff );
101         
102         mlt_tokeniser_close( tokeniser );
103         
104         return error;
105 }
106
107 /** Get the audio.
108 */
109
110 static int filter_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
111 {
112         // Get the properties of the frame
113         mlt_properties properties = mlt_frame_properties( frame );
114
115         // Get the filter service
116         mlt_filter filter = mlt_frame_pop_audio( frame );
117
118         // Get the filter properties
119         mlt_properties filter_properties = mlt_filter_properties( filter );
120
121         // Get the properties
122         st_sample_t *input_buffer = mlt_properties_get_data( filter_properties, "input_buffer", NULL );
123         st_sample_t *output_buffer = mlt_properties_get_data( filter_properties, "output_buffer", NULL );
124         int channels_avail = *channels;
125         int i; // channel
126         int count = mlt_properties_get_int( filter_properties, "effect_count" );
127
128         // Restore the original get_audio
129         frame->get_audio = mlt_frame_pop_audio( frame );
130
131         // Get the producer's audio
132         mlt_frame_get_audio( frame, buffer, format, frequency, &channels_avail, samples );
133
134         // Duplicate channels as necessary
135         if ( channels_avail < *channels )
136         {
137                 int size = *channels * *samples * sizeof( int16_t );
138                 int16_t *new_buffer = mlt_pool_alloc( size );
139                 int j, k = 0;
140                 
141                 // Duplicate the existing channels
142                 for ( i = 0; i < *samples; i++ )
143                 {
144                         for ( j = 0; j < *channels; j++ )
145                         {
146                                 new_buffer[ ( i * *channels ) + j ] = (*buffer)[ ( i * channels_avail ) + k ];
147                                 k = ( k + 1 ) % channels_avail;
148                         }
149                 }
150                 
151                 // Update the audio buffer now - destroys the old
152                 mlt_properties_set_data( properties, "audio", new_buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
153                 
154                 *buffer = new_buffer;
155         }
156         else if ( channels_avail == 6 && *channels == 2 )
157         {
158                 // Nasty hack for ac3 5.1 audio - may be a cause of failure?
159                 int size = *channels * *samples * sizeof( int16_t );
160                 int16_t *new_buffer = mlt_pool_alloc( size );
161                 
162                 // Drop all but the first *channels
163                 for ( i = 0; i < *samples; i++ )
164                 {
165                         new_buffer[ ( i * *channels ) + 0 ] = (*buffer)[ ( i * channels_avail ) + 2 ];
166                         new_buffer[ ( i * *channels ) + 1 ] = (*buffer)[ ( i * channels_avail ) + 3 ];
167                 }
168
169                 // Update the audio buffer now - destroys the old
170                 mlt_properties_set_data( properties, "audio", new_buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
171                 
172                 *buffer = new_buffer;
173         }
174
175         // Even though some effects are multi-channel aware, it is not reliable
176         // We must maintain a separate effect state for each channel
177         for ( i = 0; i < *channels; i++ )
178         {
179                 char id[ 256 ];
180                 sprintf( id, "_effect_0_%d", i );
181                 
182                 // Get an existing effect state
183                 eff_t e = mlt_properties_get_data( filter_properties, id, NULL );
184                 
185                 // Validate the existing effect state
186                 if ( e != NULL && ( e->ininfo.rate != *frequency || 
187                                                         e->outinfo.rate != *frequency ) )
188                         e = NULL;
189                 
190                 // (Re)Create the effect state
191                 if ( e == NULL )
192                 {
193                         int j = 0;
194                         
195                         // Reset the count
196                         count = 0;
197         
198                         // Loop over all properties
199                         for ( j = 0; j < mlt_properties_count( filter_properties ); j ++ )
200                         {
201                                 // Get the name of this property
202                                 char *name = mlt_properties_get_name( filter_properties, j );
203         
204                                 // If the name does not contain a . and matches effect
205                                 if ( !strncmp( name, "effect", 6 ) )
206                                 {
207                                         // Get the effect specification
208                                         char *value = mlt_properties_get( filter_properties, name );
209         
210                                         // Create an instance
211                                         if ( create_effect( filter, value, count, i, *frequency ) == 0 )
212                                                 count ++;
213                                 }
214                         }
215                         
216                         // Save the number of filters
217                         mlt_properties_set_int( filter_properties, "effect_count", count );
218                         
219                 }
220                 if ( *samples > 0 && count > 0 )
221                 {
222                         st_sample_t *p = input_buffer;
223                         st_sample_t *end = p + *samples;
224                         int16_t *q = *buffer + i;
225                         st_size_t isamp = *samples;
226                         st_size_t osamp = *samples;
227                         double rms = 0;
228                         int j;
229                         char *normalise = mlt_properties_get( filter_properties, "normalise" );
230                         double normalised_gain = 1.0;
231                         
232                         // Convert to sox encoding
233                         while( p != end )
234                         {
235                                 *p = ST_SIGNED_WORD_TO_SAMPLE( *q );
236                                 
237                                 // Compute rms amplitude while we are accessing each sample
238                                 rms += ( double )*p * ( double )*p;
239                                 
240                                 p ++;
241                                 q += *channels;
242                         }
243                         
244                         // Compute final rms amplitude
245                         rms = sqrt( rms / *samples / ST_SSIZE_MIN / ST_SSIZE_MIN );
246                         
247                         if ( normalise )
248                         {
249                                 int window = mlt_properties_get_int( filter_properties, "window" );
250                                 double *smooth_buffer = mlt_properties_get_data( filter_properties, "smooth_buffer", NULL );
251                                 double max_gain = mlt_properties_get_double( filter_properties, "max_gain" );
252                                 
253                                 // Default the maximum gain factor to 20dBFS
254                                 if ( max_gain == 0 )
255                                         max_gain = 10.0;
256                                 
257                                 // The smoothing buffer prevents radical shifts in the gain level
258                                 if ( window > 0 && smooth_buffer != NULL )
259                                 {
260                                         int smooth_index = mlt_properties_get_int( filter_properties, "_smooth_index" );
261                                         smooth_buffer[ smooth_index ] = rms;
262                                         
263                                         // Ignore very small values that adversely affect the mean
264                                         if ( rms > AMPLITUDE_MIN )
265                                                 mlt_properties_set_int( filter_properties, "_smooth_index", ( smooth_index + 1 ) % window );
266                                         
267                                         // Smoothing is really just a mean over the past N values
268                                         normalised_gain = AMPLITUDE_NORM / mean( smooth_buffer, window );
269                                 }
270                                 else if ( rms > 0 )
271                                 {
272                                         // Determine gain to apply as current amplitude
273                                         normalised_gain = AMPLITUDE_NORM / rms;
274                                 }
275                                         
276                                 //printf("filter_sox: rms %.3f gain %.3f\n", rms, normalised_gain );
277                                 
278                                 // Govern the maximum gain
279                                 if ( normalised_gain > max_gain )
280                                         normalised_gain = max_gain;
281                         }
282                         
283                         // For each effect
284                         for ( j = 0; j < count; j++ )
285                         {
286                                 sprintf( id, "_effect_%d_%d", j, i );
287                                 e = mlt_properties_get_data( filter_properties, id, NULL );
288                                 
289                                 // We better have this guy
290                                 if ( e != NULL )
291                                 {
292                                         float saved_gain = 1.0;
293                                         
294                                         // XXX: hack to apply the normalised gain level to the vol effect
295                                         if ( normalise && strcmp( e->name, "vol" ) == 0 )
296                                         {
297                                                 float *f = ( float * )( e->priv );
298                                                 saved_gain = *f;
299                                                 *f = saved_gain * normalised_gain;
300                                         }
301                                         
302                                         // Apply the effect
303                                         if ( ( * e->h->flow )( e, input_buffer, output_buffer, &isamp, &osamp ) == ST_SUCCESS )
304                                         {
305                                                 // Swap input and output buffer pointers for subsequent effects
306                                                 p = input_buffer;
307                                                 input_buffer = output_buffer;
308                                                 output_buffer = p;
309                                         }
310                                         
311                                         // XXX: hack to restore the original vol gain to prevent accumulation
312                                         if ( normalise && strcmp( e->name, "vol" ) == 0 )
313                                         {
314                                                 float *f = ( float * )( e->priv );
315                                                 *f = saved_gain;
316                                         }
317                                 }
318                         }
319                         
320                         // Convert back to signed 16bit
321                         p = input_buffer;
322                         q = *buffer + i;
323                         end = p + *samples;
324                         while ( p != end )
325                         {
326                                 *q = ST_SAMPLE_TO_SIGNED_WORD( *p ++ );
327                                 q += *channels;
328                         }
329                 }
330         }
331
332         return 0;
333 }
334
335 /** Filter processing.
336 */
337
338 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
339 {
340         if ( frame->get_audio != NULL )
341         {
342                 // Add the filter to the frame
343                 mlt_frame_push_audio( frame, frame->get_audio );
344                 mlt_frame_push_audio( frame, this );
345                 frame->get_audio = filter_get_audio;
346                 
347                 // Parse the window property and allocate smoothing buffer if needed
348                 mlt_properties properties = mlt_filter_properties( this );
349                 int window = mlt_properties_get_int( properties, "window" );
350                 if ( mlt_properties_get( properties, "smooth_buffer" ) == NULL && window > 1 )
351                 {
352                         // Create a smoothing buffer for the calculated "max power" of frame of audio used in normalisation
353                         double *smooth_buffer = (double*) calloc( window, sizeof( double ) );
354                         int i;
355                         for ( i = 0; i < window; i++ )
356                                 smooth_buffer[ i ] = -1.0;
357                         mlt_properties_set_data( properties, "smooth_buffer", smooth_buffer, 0, free, NULL );
358                 }
359         }
360
361         return frame;
362 }
363
364 /** Constructor for the filter.
365 */
366
367 mlt_filter filter_sox_init( char *arg )
368 {
369         mlt_filter this = mlt_filter_new( );
370         if ( this != NULL )
371         {
372                 void *input_buffer = mlt_pool_alloc( BUFFER_LEN );
373                 void *output_buffer = mlt_pool_alloc( BUFFER_LEN );
374                 mlt_properties properties = mlt_filter_properties( this );
375                 
376                 this->process = filter_process;
377                 
378                 if ( arg != NULL )
379                         mlt_properties_set( properties, "effect", arg );
380                 mlt_properties_set_data( properties, "input_buffer", input_buffer, BUFFER_LEN, mlt_pool_release, NULL );
381                 mlt_properties_set_data( properties, "output_buffer", output_buffer, BUFFER_LEN, mlt_pool_release, NULL );
382                 mlt_properties_set_int( properties, "window", 75 );
383         }
384         return this;
385 }
386
387 // What to do when a libst internal failure occurs
388 void cleanup(void){}
389
390 // Is there a build problem with my sox-devel package?
391 #ifndef gsm_create
392 void gsm_create(void){}
393 #endif
394 #ifndef gsm_decode
395 void gsm_decode(void){}
396 #endif
397 #ifndef gdm_encode
398 void gsm_encode(void){}
399 #endif
400 #ifndef gsm_destroy
401 void gsm_destroy(void){}
402 #endif
403 #ifndef gsm_option
404 void gsm_option(void){}
405 #endif