westley bugfixes and audio normalisation
authorddennedy <ddennedy@d19143bc-622f-0410-bfdd-b5b2a6649095>
Tue, 27 Jan 2004 22:46:16 +0000 (22:46 +0000)
committerddennedy <ddennedy@d19143bc-622f-0410-bfdd-b5b2a6649095>
Tue, 27 Jan 2004 22:46:16 +0000 (22:46 +0000)
git-svn-id: https://mlt.svn.sourceforge.net/svnroot/mlt/trunk/mlt@98 d19143bc-622f-0410-bfdd-b5b2a6649095

src/modules/core/filter_volume.c
src/modules/westley/consumer_westley.c
src/modules/westley/producer_westley.c

index c0ad4dd..719e3ed 100644 (file)
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <math.h>
+#include <ctype.h>
+#include <string.h>
+
+#define MAX_CHANNELS 6
+#define SMOOTH_BUFFER_SIZE 50
+
+/* This utilities and limiter function comes from the normalize utility:
+   Copyright (C) 1999--2002 Chris Vaill */
+
+#define samp_width 16
+
+#ifndef ROUND
+# define ROUND(x) floor((x) + 0.5)
+#endif
+
+#define DBFSTOAMP(x) pow(10,(x)/20.0)
+
+/** Return nonzero if the two strings are equal, ignoring case, up to
+    the first n characters.
+*/
+int strncaseeq(const char *s1, const char *s2, size_t n)
+{
+       for ( ; n > 0; n--)
+       {
+               if (tolower(*s1++) != tolower(*s2++))
+                       return 0;
+       }
+       return 1;
+}
+
+/** Limiter function.
+         / tanh((x + lev) / (1-lev)) * (1-lev) - lev        (for x < -lev)
+         |
+    x' = | x                                                (for |x| <= lev)
+         |
+         \ tanh((x - lev) / (1-lev)) * (1-lev) + lev        (for x > lev)
+  With limiter level = 0, this is equivalent to a tanh() function;
+  with limiter level = 1, this is equivalent to clipping.
+*/
+static inline double limiter( double x, double lmtr_lvl )
+{
+       double xp;
+
+       if (x < -lmtr_lvl)
+               xp = tanh((x + lmtr_lvl) / (1-lmtr_lvl)) * (1-lmtr_lvl) - lmtr_lvl;
+       else if (x <= lmtr_lvl)
+               xp = x;
+       else
+               xp = tanh((x - lmtr_lvl) / (1-lmtr_lvl)) * (1-lmtr_lvl) + lmtr_lvl;
+
+       return xp;
+}
+
+
+/** Takes a full smoothing window, and returns the value of the center
+    element, smoothed.
+
+    Currently, just does a mean filter, but we could do a median or
+    gaussian filter here instead.
+*/
+static inline double get_smoothed_data( double *buf, int count )
+{
+       int i, j;
+       double smoothed = 0;
+
+       for ( i = 0, j = 0; i < count; i++ )
+       {
+               if ( buf[ i ] != -1.0 )
+               {
+                       smoothed += buf[ i ];
+                       j++;
+               }
+       }
+       smoothed /= j;
+//     fprintf( stderr, "smoothed over %d values, result %f\n", j, smoothed );
+
+       return smoothed;
+}
+
+/** Get the max power level (using RMS) and peak level of the audio segment.
+ */
+double signal_max_power( int16_t *buffer, int channels, int samples, int16_t *peak )
+{
+       // Determine numeric limits
+       int bytes_per_samp = (samp_width - 1) / 8 + 1;
+       int16_t max = (1 << (bytes_per_samp * 8 - 1)) - 1;
+       int16_t min = -max - 1;
+       
+       double *sums = (double *) calloc( channels, sizeof(double) );
+       int c, i;
+       int16_t sample;
+       double pow, maxpow = 0;
+
+       /* initialize peaks to effectively -inf and +inf */
+       int16_t max_sample = min;
+       int16_t min_sample = max;
+  
+       for ( i = 0; i < samples; i++ )
+       {
+               for ( c = 0; c < channels; c++ )
+               {
+                       sample = *buffer++;
+                       sums[ c ] += (double) sample * (double) sample;
+                       
+                       /* track peak */
+                       if ( sample > max_sample )
+                               max_sample = sample;
+                       else if ( sample < min_sample )
+                               min_sample = sample;
+               }
+       }
+       for ( c = 0; c < channels; c++ )
+       {
+               pow = sums[ c ] / (double) samples;
+               if ( pow > maxpow )
+                       maxpow = pow;
+       }
+                       
+       free( sums );
+       
+       /* scale the pow value to be in the range 0.0 -- 1.0 */
+       maxpow /= ( (double) min * (double) min);
+
+       if ( -min_sample > max_sample )
+               *peak = min_sample / (double) min;
+       else
+               *peak = max_sample / (double) max;
+
+       return sqrt( maxpow );
+}
 
 /** Get the audio.
 */
@@ -32,7 +165,14 @@ static int filter_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format
 {
        // Get the properties of the a frame
        mlt_properties properties = mlt_frame_properties( frame );
-       double volume = mlt_properties_get_double( properties, "volume" );
+       double gain = mlt_properties_get_double( properties, "gain" );
+       int use_limiter =  mlt_properties_get_int( properties, "volume.use_limiter" );
+       double limiter_level =  mlt_properties_get_double( properties, "volume.limiter_level" );
+       int normalise =  mlt_properties_get_int( properties, "volume.normalise" );
+       double amplitude =  mlt_properties_get_double( properties, "volume.amplitude" );
+       int i;
+       double sample;
+       int16_t peak;
 
        // Restore the original get_audio
        frame->get_audio = mlt_properties_get_data( properties, "volume.get_audio", NULL );
@@ -40,10 +180,48 @@ static int filter_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format
        // Get the producer's audio
        mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
 
-       // Apply the volume
-       int i;
+       // Determine numeric limits
+       int bytes_per_samp = (samp_width - 1) / 8 + 1;
+       int samplemax = (1 << (bytes_per_samp * 8 - 1)) - 1;
+       int samplemin = -samplemax - 1;
+
+#if 0
+       if ( gain > 1.0 && use_limiter != 0 )
+               fprintf(stderr, "filter_volume: limiting samples greater than %f\n", limiter_level );
+#endif
+
+       if ( normalise )
+       {
+               double *smooth_buffer = mlt_properties_get_data( properties, "volume.smooth_buffer", NULL );
+               int *smooth_index = mlt_properties_get_data( properties, "volume.smooth_index", NULL );
+
+               // Compute the signal power and put into smoothing buffer
+               smooth_buffer[ *smooth_index ] = signal_max_power( *buffer, *channels, *samples, &peak );
+               *smooth_index = ( *smooth_index + 1 ) % SMOOTH_BUFFER_SIZE;
+
+               // Smooth the data and compute the gain
+               gain *= amplitude / get_smoothed_data( smooth_buffer, SMOOTH_BUFFER_SIZE );
+       }
+       
+       // Apply the gain
        for ( i = 0; i < ( *channels * *samples ); i++ )
-               (*buffer)[i] *= volume;
+       {
+               sample = (*buffer)[i] * gain;
+               (*buffer)[i] = ROUND( sample );
+               
+               if ( gain > 1.0 )
+               {
+                       /* use limiter function instead of clipping */
+                       if ( use_limiter != 0 )
+                               (*buffer)[i] = ROUND( samplemax * limiter( sample / (double) samplemax, limiter_level ) );
+                               
+                       /* perform clipping */
+                       else if ( sample > samplemax )
+                               (*buffer)[i] = samplemax;
+                       else if ( sample < samplemin )
+                               (*buffer)[i] = samplemin;
+               }
+       }
        
        return 0;
 }
@@ -54,12 +232,81 @@ static int filter_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format
 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
 {
        mlt_properties properties = mlt_frame_properties( frame );
+       mlt_properties filter_props = mlt_filter_properties( this );
 
-       // Propogate the level property
-       if ( mlt_properties_get( mlt_filter_properties( this ), "volume" ) != NULL )
-               mlt_properties_set_double( properties, "volume",
-                       mlt_properties_get_double( mlt_filter_properties( this ), "volume" ) );
+       // Propogate the volume/gain property
+       if ( mlt_properties_get( properties, "gain" ) == NULL )
+       {
+               double gain = 1.0; // none
+               if ( mlt_properties_get( filter_props, "volume" ) != NULL )
+                       gain = mlt_properties_get_double( filter_props, "volume" );
+               if ( mlt_properties_get( filter_props, "gain" ) != NULL )
+                       gain = mlt_properties_get_double( filter_props, "gain" );
+               mlt_properties_set_double( properties, "gain", gain );
+       }
        
+       // Parse and propogate the limiter property
+       if ( mlt_properties_get( filter_props, "limiter" ) != NULL )
+       {
+               char *p = mlt_properties_get( filter_props, "limiter" );
+               double level = 0.5; /* -6dBFS */ 
+               if ( strcmp( p, "" ) != 0 )
+                       level = strtod( p, &p);
+               
+               /* check if "dB" is given after number */
+               while ( isspace( *p ) )
+                       p++;
+               
+               if ( strncaseeq( p, "db", 2 ) )
+               {
+                       if ( level > 0 )
+                               level = -level;
+                       level = DBFSTOAMP( level );
+               }
+               else
+               {
+                       if ( level < 0 )
+                               level = -level;
+               }
+               mlt_properties_set_int( properties, "volume.use_limiter", 1 );
+               mlt_properties_set_double( properties, "volume.limiter_level", level );
+       }
+
+       // Parse and propogate the normalise property
+       if ( mlt_properties_get( filter_props, "normalise" ) != NULL )
+       {
+               char *p = mlt_properties_get( filter_props, "normalise" );
+               double amplitude = 0.2511886431509580; /* -12dBFS */
+               if ( strcmp( p, "" ) != 0 )
+                       amplitude = strtod( p, &p);
+
+               /* check if "dB" is given after number */
+               while ( isspace( *p ) )
+                       p++;
+
+               if ( strncaseeq( p, "db", 2 ) )
+               {
+                       if ( amplitude > 0 )
+                               amplitude = -amplitude;
+                       amplitude = DBFSTOAMP( amplitude );
+               }
+               else
+               {
+                       if ( amplitude < 0 )
+                               amplitude = -amplitude;
+                       if ( amplitude > 1.0 )
+                               amplitude = 1.0;
+               }
+               mlt_properties_set_int( properties, "volume.normalise", 1 );
+               mlt_properties_set_double( properties, "volume.amplitude", amplitude );
+       }
+
+       // Propogate the smoothing buffer properties
+       mlt_properties_set_data( properties, "volume.smooth_buffer",
+               mlt_properties_get_data( filter_props, "smooth_buffer", NULL ), 0, NULL, NULL );
+       mlt_properties_set_data( properties, "volume.smooth_index",
+               mlt_properties_get_data( filter_props, "smooth_index", NULL ), 0, NULL, NULL );
+               
        // Backup the original get_audio (it's still needed)
        mlt_properties_set_data( properties, "volume.get_audio", frame->get_audio, 0, NULL, NULL );
 
@@ -80,6 +327,15 @@ mlt_filter filter_volume_init( char *arg )
                this->process = filter_process;
                if ( arg != NULL )
                        mlt_properties_set_double( mlt_filter_properties( this ), "volume", atof( arg ) );
+
+               // Create a smoothing buffer for the calculated "max power" of frame of audio used in normalisation
+               double *smooth_buffer = (double*) calloc( SMOOTH_BUFFER_SIZE, sizeof( double ) );
+               int i;
+               for ( i = 0; i < SMOOTH_BUFFER_SIZE; i++ )
+                       smooth_buffer[ i ] = -1.0;
+               mlt_properties_set_data( mlt_filter_properties( this ), "smooth_buffer", smooth_buffer, 0, free, NULL );
+               int *smooth_index = calloc( 1, sizeof( int ) );
+               mlt_properties_set_data( mlt_filter_properties( this ), "smooth_index", smooth_index, 0, free, NULL );
        }
        return this;
 }
index d196ffb..75932a5 100644 (file)
@@ -355,7 +355,7 @@ static int consumer_start( mlt_consumer this )
                free( context );
                
                if ( mlt_properties_get( mlt_consumer_properties( this ), "resource" ) == NULL )
-                       xmlDocFormatDump( stderr, doc, 1 );
+                       xmlDocFormatDump( stdout, doc, 1 );
                else
                        xmlSaveFormatFile( mlt_properties_get( mlt_consumer_properties( this ), "resource" ), doc, 1 );
        }
index ff5ba41..2bd3204 100644 (file)
@@ -342,6 +342,18 @@ static void on_end_entry( deserialise_context context, const xmlChar *name )
        context_push_service( context, service );
 }
 
+static void on_end_tractor( deserialise_context context, const xmlChar *name )
+{
+       // Discard the last producer
+       mlt_producer multitrack = MLT_PRODUCER( context_pop_service( context ) );
+
+       // Inherit the producer's properties
+       mlt_properties properties = mlt_producer_properties( multitrack );
+       mlt_properties_set_position( properties, "length", mlt_producer_get_out( multitrack ) + 1 );
+       mlt_producer_set_in_and_out( multitrack, 0, mlt_producer_get_out( multitrack ) );
+       mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( multitrack ) );
+}
+
 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
 {
        deserialise_context context = ( deserialise_context ) ctx;
@@ -377,10 +389,7 @@ static void on_end_element( void *ctx, const xmlChar *name )
        else if ( strcmp( name, "entry" ) == 0 )
                on_end_entry( context, name );
        else if ( strcmp( name, "tractor" ) == 0 )
-       {
-               // Discard the last producer
-               context_pop_service( context );
-       }
+               on_end_tractor( context, name );
 }
 
 
@@ -408,6 +417,8 @@ mlt_producer producer_westley_init( char *filename )
        mlt_properties_set_data( mlt_service_properties( service ), "__destructors__", context->destructors, 0, (mlt_destructor) mlt_properties_close, NULL );
        free( context );
 
+       mlt_properties_set( mlt_service_properties( service ), "resource", filename );
+
        return MLT_PRODUCER( service );
 }