]> git.sesse.net Git - vlc/blobdiff - modules/audio_filter/channel_mixer/mono.c
block_t.i_samples -> block_t.i_nb_samples (as aout_buffer_t)
[vlc] / modules / audio_filter / channel_mixer / mono.c
index 7879c7ead1333507031cb469941ef2d0be7aacc6..defb78302f5ac86237deb1a8eef7f8aa0f400e05 100644 (file)
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <math.h>                                        /* sqrt */
-
-#ifdef HAVE_STDINT_H
-#   include <stdint.h>                                         /* int16_t .. */
-#elif defined(HAVE_INTTYPES_H)
-#   include <inttypes.h>                                       /* int16_t .. */
+#ifdef HAVE_CONFIG_H
+# include "config.h"
 #endif
 
+#include <math.h>                                        /* sqrt */
+#include <stdint.h>                                         /* int16_t .. */
+
 #ifdef HAVE_UNISTD_H
 #   include <unistd.h>
 #endif
 
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
-
-#include <vlc/vlc.h>
-#include <vlc_es.h>
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 #include <vlc_block.h>
 #include <vlc_filter.h>
 #include <vlc_aout.h>
@@ -73,21 +68,21 @@ struct atomic_operation_t
 
 struct filter_sys_t
 {
-    vlc_bool_t b_downmix;
+    bool b_downmix;
 
     unsigned int i_nb_channels; /* number of int16_t per sample */
     int i_channel_selected;
     int i_bitspersample;
 
     size_t i_overflow_buffer_size;/* in bytes */
-    byte_t * p_overflow_buffer;
+    uint8_t * p_overflow_buffer;
     unsigned int i_nb_atomic_operations;
     struct atomic_operation_t * p_atomic_operations;
 };
 
 #define MONO_DOWNMIX_TEXT N_("Use downmix algorithm")
 #define MONO_DOWNMIX_LONGTEXT N_("This option selects a stereo to mono " \
-    "downmix algorithm that is used in the headphone channel mixer. It" \
+    "downmix algorithm that is used in the headphone channel mixer. It " \
     "gives the effect of standing in a room full of speakers." )
 
 #define MONO_CHANNEL_TEXT N_("Select channel to keep")
@@ -96,7 +91,7 @@ struct filter_sys_t
     "2=rear left, 3=rear right, 4=center, 5=left front)")
 
 static const int pi_pos_values[] = { 0, 1, 2, 4, 8, 5 };
-static const char *ppsz_pos_descriptions[] =
+static const char *const ppsz_pos_descriptions[] =
 { N_("Left"), N_("Right"), N_("Left rear"), N_("Right rear"), N_("Center"),
   N_("Left front") };
 
@@ -109,21 +104,21 @@ static const uint32_t pi_channels_out[] =
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
-vlc_module_begin();
-    set_description( _("Audio filter for stereo to mono conversion") );
-    set_capability( "audio filter2", 0 );
-
-    add_bool( MONO_CFG "downmix", VLC_FALSE, NULL, MONO_DOWNMIX_TEXT,
-              MONO_DOWNMIX_LONGTEXT, VLC_FALSE );
+vlc_module_begin ()
+    set_description( N_("Audio filter for stereo to mono conversion") )
+    set_capability( "audio filter2", 2 )
+    set_category( CAT_AUDIO )
+    set_subcategory( SUBCAT_AUDIO_MISC )
+    set_callbacks( OpenFilter, CloseFilter )
+    set_shortname( "Mono" )
+
+    add_bool( MONO_CFG "downmix", true, NULL, MONO_DOWNMIX_TEXT,
+              MONO_DOWNMIX_LONGTEXT, false )
     add_integer( MONO_CFG "channel", -1, NULL, MONO_CHANNEL_TEXT,
-        MONO_CHANNEL_LONGTEXT, VLC_FALSE );
-        change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
+        MONO_CHANNEL_LONGTEXT, false )
+        change_integer_list( pi_pos_values, ppsz_pos_descriptions, NULL )
 
-    set_category( CAT_AUDIO );
-    set_subcategory( SUBCAT_AUDIO_MISC );
-    set_callbacks( OpenFilter, CloseFilter );
-    set_shortname( "Mono" );
-vlc_module_end();
+vlc_module_end ()
 
 /* Init() and ComputeChannelOperations() -
  * Code taken from modules/audio_filter/channel_mixer/headphone.c
@@ -218,12 +213,6 @@ static int Init( vlc_object_t *p_this, struct filter_sys_t * p_data,
     int i_source_channel_offset;
     unsigned int i;
 
-    if( p_data == NULL )
-    {
-        msg_Dbg( p_this, "passing a null pointer as argument" );
-        return 0;
-    }
-
     if( config_GetInt( p_this, "headphone-compensate" ) )
     {
         /* minimal distance to any speaker */
@@ -246,10 +235,7 @@ static int Init( vlc_object_t *p_this, struct filter_sys_t * p_data,
     p_data->p_atomic_operations = malloc( sizeof(struct atomic_operation_t)
             * p_data->i_nb_atomic_operations );
     if( p_data->p_atomic_operations == NULL )
-    {
-        msg_Err( p_this, "out of memory" );
         return -1;
-    }
 
     /* For each virtual speaker, computes elementary wave propagation time
      * to each ear */
@@ -346,9 +332,9 @@ static int Init( vlc_object_t *p_this, struct filter_sys_t * p_data,
         }
     }
     p_data->p_overflow_buffer = malloc( p_data->i_overflow_buffer_size );
-    if( p_data->p_atomic_operations == NULL )
+    if( p_data->p_overflow_buffer == NULL )
     {
-        msg_Err( p_this, "out of memory" );
+        free( p_data->p_atomic_operations );
         return -1;
     }
     memset( p_data->p_overflow_buffer, 0, p_data->i_overflow_buffer_size );
@@ -367,44 +353,36 @@ static int OpenFilter( vlc_object_t *p_this )
 
     if( aout_FormatNbChannels( &(p_filter->fmt_in.audio) ) == 1 )
     {
-        msg_Dbg( p_filter, "filter discarded (incompatible format)" );
+        /*msg_Dbg( p_filter, "filter discarded (incompatible format)" );*/
         return VLC_EGENERIC;
     }
 
-    if( (p_filter->fmt_in.i_codec != AOUT_FMT_S16_NE) ||
-        (p_filter->fmt_out.i_codec != AOUT_FMT_S16_NE) )
+    if( (p_filter->fmt_in.i_codec != VLC_CODEC_S16N) ||
+        (p_filter->fmt_out.i_codec != VLC_CODEC_S16N) )
     {
-        msg_Err( p_this, "filter discarded (invalid format)" );
-        return -1;
+        /*msg_Err( p_this, "filter discarded (invalid format)" );*/
+        return VLC_EGENERIC;
     }
 
     if( (p_filter->fmt_in.audio.i_format != p_filter->fmt_out.audio.i_format) &&
         (p_filter->fmt_in.audio.i_rate != p_filter->fmt_out.audio.i_rate) &&
-        (p_filter->fmt_in.audio.i_format != AOUT_FMT_S16_NE) &&
-        (p_filter->fmt_out.audio.i_format != AOUT_FMT_S16_NE) &&
+        (p_filter->fmt_in.audio.i_format != VLC_CODEC_S16N) &&
+        (p_filter->fmt_out.audio.i_format != VLC_CODEC_S16N) &&
         (p_filter->fmt_in.audio.i_bitspersample !=
                                     p_filter->fmt_out.audio.i_bitspersample))
     {
-        msg_Err( p_this, "couldn't load mono filter" );
+        /*msg_Err( p_this, "couldn't load mono filter" );*/
         return VLC_EGENERIC;
     }
 
     /* Allocate the memory needed to store the module's structure */
     p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
     if( p_sys == NULL )
-    {
-        msg_Err( p_filter, "out of memory" );
         return VLC_EGENERIC;
-    }
-
-    var_Create( p_this, MONO_CFG "downmix",
-                VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
-    p_sys->b_downmix = var_GetBool( p_this, MONO_CFG "downmix" );
 
-    var_Create( p_this, MONO_CFG "channel",
-                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
+    p_sys->b_downmix = var_CreateGetBool( p_this, MONO_CFG "downmix" );
     p_sys->i_channel_selected =
-            (unsigned int) var_GetInteger( p_this, MONO_CFG "channel" );
+            (unsigned int) var_CreateGetInteger( p_this, MONO_CFG "channel" );
 
     if( p_sys->b_downmix )
     {
@@ -436,6 +414,9 @@ static int OpenFilter( vlc_object_t *p_this )
               p_filter->fmt_in.audio.i_physical_channels,
               p_filter->fmt_in.audio.i_rate ) < 0 )
     {
+        var_Destroy( p_this, MONO_CFG "channel" );
+        var_Destroy( p_this, MONO_CFG "downmix" );
+        free( p_sys );
         return VLC_EGENERIC;
     }
 
@@ -462,6 +443,8 @@ static void CloseFilter( vlc_object_t *p_this)
 
     var_Destroy( p_this, MONO_CFG "channel" );
     var_Destroy( p_this, MONO_CFG "downmix" );
+    free( p_sys->p_atomic_operations );
+    free( p_sys->p_overflow_buffer );
     free( p_sys );
 }
 
@@ -476,24 +459,25 @@ static block_t *Convert( filter_t *p_filter, block_t *p_block )
     unsigned int i_samples;
     int i_out_size;
 
-    if( !p_block || !p_block->i_samples )
+    if( !p_block || !p_block->i_nb_samples )
     {
         if( p_block )
-            p_block->pf_release( p_block );
+            block_Release( p_block );
         return NULL;
     }
 
-    i_out_size = p_block->i_samples * p_filter->p_sys->i_bitspersample/8 *
+    i_out_size = p_block->i_nb_samples * p_filter->p_sys->i_bitspersample/8 *
                  aout_FormatNbChannels( &(p_filter->fmt_out.audio) );
 
     p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
     if( !p_out )
     {
         msg_Warn( p_filter, "can't get output buffer" );
-        p_block->pf_release( p_block );
+        block_Release( p_block );
         return NULL;
     }
-    p_out->i_samples = (p_block->i_samples / p_filter->p_sys->i_nb_channels) *
+    p_out->i_nb_samples =
+                  (p_block->i_nb_samples / p_filter->p_sys->i_nb_channels) *
                        aout_FormatNbChannels( &(p_filter->fmt_out.audio) );
     p_out->i_dts = p_block->i_dts;
     p_out->i_pts = p_block->i_pts;
@@ -507,7 +491,7 @@ static block_t *Convert( filter_t *p_filter, block_t *p_block )
 
     in_buf.p_buffer = p_block->p_buffer;
     in_buf.i_nb_bytes = p_block->i_buffer;
-    in_buf.i_nb_samples = p_block->i_samples;
+    in_buf.i_nb_samples = p_block->i_nb_samples;
 
 #if 0
     unsigned int i_in_size = in_buf.i_nb_samples  * (p_filter->p_sys->i_bitspersample/8) *
@@ -521,7 +505,7 @@ static block_t *Convert( filter_t *p_filter, block_t *p_block )
 
     out_buf.p_buffer = p_out->p_buffer;
     out_buf.i_nb_bytes = p_out->i_buffer;
-    out_buf.i_nb_samples = p_out->i_samples;
+    out_buf.i_nb_samples = p_out->i_nb_samples;
 
     memset( p_out->p_buffer, 0, i_out_size );
     if( p_filter->p_sys->b_downmix )
@@ -535,9 +519,9 @@ static block_t *Convert( filter_t *p_filter, block_t *p_block )
     }
 
     p_out->i_buffer = out_buf.i_nb_bytes;
-    p_out->i_samples = out_buf.i_nb_samples;
+    p_out->i_nb_samples = out_buf.i_nb_samples;
 
-    p_block->pf_release( p_block );
+    block_Release( p_block );
     return p_out;
 }
 
@@ -555,9 +539,9 @@ static void stereo2mono_downmix( aout_filter_t * p_filter,
     int i_output_nb = aout_FormatNbChannels( &p_filter->output );
 
     int16_t * p_in = (int16_t*) p_in_buf->p_buffer;
-    byte_t * p_out;
-    byte_t * p_overflow;
-    byte_t * p_slide;
+    uint8_t * p_out;
+    uint8_t * p_overflow;
+    uint8_t * p_slide;
 
     size_t i_overflow_size;     /* in bytes */
     size_t i_out_size;          /* in bytes */
@@ -575,84 +559,77 @@ static void stereo2mono_downmix( aout_filter_t * p_filter,
     p_out = p_out_buf->p_buffer;
     i_out_size = p_out_buf->i_nb_bytes;
 
-    if( p_sys != NULL )
-    {
-        /* Slide the overflow buffer */
-        p_overflow = p_sys->p_overflow_buffer;
-        i_overflow_size = p_sys->i_overflow_buffer_size;
+    /* Slide the overflow buffer */
+    p_overflow = p_sys->p_overflow_buffer;
+    i_overflow_size = p_sys->i_overflow_buffer_size;
+
+    if ( i_out_size > i_overflow_size )
+        memcpy( p_out, p_overflow, i_overflow_size );
+    else
+        memcpy( p_out, p_overflow, i_out_size );
 
-        if ( i_out_size > i_overflow_size )
-            memcpy( p_out, p_overflow, i_overflow_size );
+    p_slide = p_sys->p_overflow_buffer;
+    while( p_slide < p_overflow + i_overflow_size )
+    {
+        if( p_slide + i_out_size < p_overflow + i_overflow_size )
+        {
+            memset( p_slide, 0, i_out_size );
+            if( p_slide + 2 * i_out_size < p_overflow + i_overflow_size )
+                memcpy( p_slide, p_slide + i_out_size, i_out_size );
+            else
+                memcpy( p_slide, p_slide + i_out_size,
+                        p_overflow + i_overflow_size - ( p_slide + i_out_size ) );
+        }
         else
-            memcpy( p_out, p_overflow, i_out_size );
+        {
+            memset( p_slide, 0, p_overflow + i_overflow_size - p_slide );
+        }
+        p_slide += i_out_size;
+    }
+
+    /* apply the atomic operations */
+    for( i = 0; i < p_sys->i_nb_atomic_operations; i++ )
+    {
+        /* shorter variable names */
+        i_source_channel_offset
+            = p_sys->p_atomic_operations[i].i_source_channel_offset;
+        i_dest_channel_offset
+            = p_sys->p_atomic_operations[i].i_dest_channel_offset;
+        i_delay = p_sys->p_atomic_operations[i].i_delay;
+        d_amplitude_factor
+            = p_sys->p_atomic_operations[i].d_amplitude_factor;
 
-        p_slide = p_sys->p_overflow_buffer;
-        while( p_slide < p_overflow + i_overflow_size )
+        if( p_out_buf->i_nb_samples > i_delay )
         {
-            if( p_slide + i_out_size < p_overflow + i_overflow_size )
+            /* current buffer coefficients */
+            for( j = 0; j < p_out_buf->i_nb_samples - i_delay; j++ )
             {
-                memset( p_slide, 0, i_out_size );
-                if( p_slide + 2 * i_out_size < p_overflow + i_overflow_size )
-                    memcpy( p_slide, p_slide + i_out_size, i_out_size );
-                else
-                    memcpy( p_slide, p_slide + i_out_size,
-                            p_overflow + i_overflow_size - ( p_slide + i_out_size ) );
+                ((int16_t*)p_out)[ (i_delay+j)*i_output_nb + i_dest_channel_offset ]
+                    += p_in[ j * i_input_nb + i_source_channel_offset ]
+                       * d_amplitude_factor;
             }
-            else
+
+            /* overflow buffer coefficients */
+            for( j = 0; j < i_delay; j++ )
             {
-                memset( p_slide, 0, p_overflow + i_overflow_size - p_slide );
+                ((int16_t*)p_overflow)[ j*i_output_nb + i_dest_channel_offset ]
+                    += p_in[ (p_out_buf->i_nb_samples - i_delay + j)
+                       * i_input_nb + i_source_channel_offset ]
+                       * d_amplitude_factor;
             }
-            p_slide += i_out_size;
         }
-
-        /* apply the atomic operations */
-        for( i = 0; i < p_sys->i_nb_atomic_operations; i++ )
+        else
         {
-            /* shorter variable names */
-            i_source_channel_offset
-                = p_sys->p_atomic_operations[i].i_source_channel_offset;
-            i_dest_channel_offset
-                = p_sys->p_atomic_operations[i].i_dest_channel_offset;
-            i_delay = p_sys->p_atomic_operations[i].i_delay;
-            d_amplitude_factor
-                = p_sys->p_atomic_operations[i].d_amplitude_factor;
-
-            if( p_out_buf->i_nb_samples > i_delay )
+            /* overflow buffer coefficients only */
+            for( j = 0; j < p_out_buf->i_nb_samples; j++ )
             {
-                /* current buffer coefficients */
-                for( j = 0; j < p_out_buf->i_nb_samples - i_delay; j++ )
-                {
-                    ((int16_t*)p_out)[ (i_delay+j)*i_output_nb + i_dest_channel_offset ]
-                        += p_in[ j * i_input_nb + i_source_channel_offset ]
-                           * d_amplitude_factor;
-                }
-
-                /* overflow buffer coefficients */
-                for( j = 0; j < i_delay; j++ )
-                {
-                    ((int16_t*)p_overflow)[ j*i_output_nb + i_dest_channel_offset ]
-                        += p_in[ (p_out_buf->i_nb_samples - i_delay + j)
-                           * i_input_nb + i_source_channel_offset ]
-                           * d_amplitude_factor;
-                }
-            }
-            else
-            {
-                /* overflow buffer coefficients only */
-                for( j = 0; j < p_out_buf->i_nb_samples; j++ )
-                {
-                    ((int16_t*)p_overflow)[ (i_delay - p_out_buf->i_nb_samples + j)
-                        * i_output_nb + i_dest_channel_offset ]
-                        += p_in[ j * i_input_nb + i_source_channel_offset ]
-                           * d_amplitude_factor;
-                }
+                ((int16_t*)p_overflow)[ (i_delay - p_out_buf->i_nb_samples + j)
+                                        * i_output_nb + i_dest_channel_offset ]
+                    += p_in[ j * i_input_nb + i_source_channel_offset ]
+                       * d_amplitude_factor;
             }
         }
     }
-    else
-    {
-        memset( p_out, 0, i_out_size );
-    }
 }
 
 /* Simple stereo to mono mixing. */