* format: added a lot of conversion (nearly all case between
authorLaurent Aimar <fenrir@videolan.org>
Mon, 15 Nov 2004 17:11:33 +0000 (17:11 +0000)
committerLaurent Aimar <fenrir@videolan.org>
Mon, 15 Nov 2004 17:11:33 +0000 (17:11 +0000)
s8,16,f32,u8,16,f32 and little endian).

modules/audio_filter/format.c

index 2df76b8..e139492 100644 (file)
@@ -2,7 +2,7 @@
  * format.c : PCM format converter
  *****************************************************************************
  * Copyright (C) 2002 VideoLAN
- * $Id: float32tos16.c 8391 2004-08-06 17:28:36Z sam $
+ * $Id$
  *
  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  *          Gildas Bazin <gbazin@videolan.org>
 #include <vlc/decoder.h>
 #include "vlc_filter.h"
 
+#ifdef WORDS_BIGENDIAN
+#   define AOUT_FMT_S16_IE VLC_FOURCC('s','1','6','l')
+#   define AOUT_FMT_U16_IE VLC_FOURCC('u','1','6','l')
+#else
+#   define AOUT_FMT_S16_IE VLC_FOURCC('s','1','6','b')
+#   define AOUT_FMT_U16_IE VLC_FOURCC('u','1','6','b')
+#endif
+
+
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
@@ -39,8 +48,116 @@ static int  Open ( vlc_object_t * );
 
 static block_t *Float32toS16( filter_t *, block_t * );
 static block_t *Float32toU16( filter_t *, block_t * );
+static block_t *Float32toS8 ( filter_t *, block_t * );
+static block_t *Float32toU8 ( filter_t *, block_t * );
+
 static block_t *S16toFloat32( filter_t *, block_t * );
-static block_t *S16Invert   ( filter_t *, block_t * );
+static block_t *S16toS8     ( filter_t *p_filter, block_t *p_block );
+static block_t *S16toU8     ( filter_t *p_filter, block_t *p_block );
+static block_t *S16toU16    ( filter_t *p_filter, block_t *p_block );
+
+static block_t *U16toFloat32( filter_t *, block_t * );
+static block_t *U16toS8     ( filter_t *p_filter, block_t *p_block );
+static block_t *U16toU8     ( filter_t *p_filter, block_t *p_block );
+static block_t *U16toS16    ( filter_t *p_filter, block_t *p_block );
+
+static block_t *Float32toS16Invert( filter_t *, block_t * );
+static block_t *Float32toU16Invert( filter_t *, block_t * );
+
+static block_t *S16InverttoFloat32( filter_t *, block_t * );
+static block_t *S16InverttoS8( filter_t *p_filter, block_t *p_block );
+static block_t *S16InverttoU8( filter_t *p_filter, block_t *p_block );
+static block_t *S16InverttoU16( filter_t *p_filter, block_t *p_block );
+
+static block_t *U16InverttoFloat32( filter_t *, block_t * );
+static block_t *U16InverttoS8( filter_t *p_filter, block_t *p_block );
+static block_t *U16InverttoU8( filter_t *p_filter, block_t *p_block );
+static block_t *U16InverttoS16( filter_t *p_filter, block_t *p_block );
+
+static block_t *S8toFloat32( filter_t *, block_t * );
+static block_t *S8toS16( filter_t *, block_t * );
+static block_t *S8toU16( filter_t *, block_t * );
+static block_t *S8toU8( filter_t *, block_t * );
+static block_t *S8toS16Invert( filter_t *, block_t * );
+static block_t *S8toU16Invert( filter_t *, block_t * );
+
+static block_t *U8toFloat32( filter_t *, block_t * );
+static block_t *U8toFloat32( filter_t *, block_t * );
+static block_t *U8toS16( filter_t *, block_t * );
+static block_t *U8toU16( filter_t *, block_t * );
+static block_t *U8toS8( filter_t *, block_t * );
+static block_t *U8toS16Invert( filter_t *, block_t * );
+static block_t *U8toU16Invert( filter_t *, block_t * );
+
+
+static block_t *U8toS8( filter_t *, block_t * );
+static block_t *S8toU8( filter_t *, block_t * );
+
+
+static block_t *Swap16   ( filter_t *, block_t * );
+
+static struct
+{
+    vlc_fourcc_t i_src;
+    vlc_fourcc_t i_dst;
+    block_t *(*pf_convert)( filter_t *, block_t *);
+} ConvertTable[] =
+{
+    /* From fl32 */
+    { VLC_FOURCC('f','l','3','2'), AOUT_FMT_S16_NE, Float32toS16 },
+    { VLC_FOURCC('f','l','3','2'), AOUT_FMT_U16_NE, Float32toU16 },
+    { VLC_FOURCC('f','l','3','2'), AOUT_FMT_S16_IE, Float32toS16Invert },
+    { VLC_FOURCC('f','l','3','2'), AOUT_FMT_U16_IE, Float32toU16Invert },
+    { VLC_FOURCC('f','l','3','2'), VLC_FOURCC('s','8',' ',' '), Float32toS8 },
+    { VLC_FOURCC('f','l','3','2'), VLC_FOURCC('u','8',' ',' '), Float32toU8 },
+
+    /* From s16 */
+    { AOUT_FMT_S16_NE, VLC_FOURCC('f','l','3','2'), S16toFloat32 },
+    { AOUT_FMT_S16_NE, AOUT_FMT_S16_IE,            Swap16 },
+    { AOUT_FMT_S16_NE, AOUT_FMT_U16_IE,            S16toU16 },
+    { AOUT_FMT_S16_NE, VLC_FOURCC('s','8',' ',' '), S16toS8 },
+    { AOUT_FMT_S16_NE, VLC_FOURCC('u','8',' ',' '), S16toU8 },
+
+    /* From u16 */
+    { AOUT_FMT_U16_NE, VLC_FOURCC('f','l','3','2'), U16toFloat32 },
+    { AOUT_FMT_U16_NE, AOUT_FMT_U16_IE,            Swap16 },
+    { AOUT_FMT_U16_NE, AOUT_FMT_S16_IE,            U16toS16 },
+    { AOUT_FMT_U16_NE, VLC_FOURCC('s','8',' ',' '), U16toS8 },
+    { AOUT_FMT_U16_NE, VLC_FOURCC('u','8',' ',' '), U16toU8 },
+
+    /* From s8 */
+    { VLC_FOURCC('s','8',' ',' '), VLC_FOURCC('f','l','3','2'), S8toFloat32 },
+    { VLC_FOURCC('s','8',' ',' '), AOUT_FMT_S16_NE,             S8toS16 },
+    { VLC_FOURCC('s','8',' ',' '), AOUT_FMT_S16_IE,             S8toS16Invert },
+    { VLC_FOURCC('s','8',' ',' '), AOUT_FMT_U16_NE,             S8toU16 },
+    { VLC_FOURCC('s','8',' ',' '), AOUT_FMT_U16_IE,             S8toU16Invert },
+    { VLC_FOURCC('s','8',' ',' '), VLC_FOURCC('u','8',' ',' '), S8toU8 },
+    
+    /* From u8 */
+    { VLC_FOURCC('u','8',' ',' '), VLC_FOURCC('f','l','3','2'), U8toFloat32 },
+    { VLC_FOURCC('u','8',' ',' '), AOUT_FMT_S16_NE,             U8toS16 },
+    { VLC_FOURCC('u','8',' ',' '), AOUT_FMT_S16_IE,             U8toS16Invert },
+    { VLC_FOURCC('u','8',' ',' '), AOUT_FMT_U16_NE,             U8toU16 },
+    { VLC_FOURCC('u','8',' ',' '), AOUT_FMT_U16_IE,             U8toU16Invert },
+    { VLC_FOURCC('u','8',' ',' '), VLC_FOURCC('s','8',' ',' '), U8toS8 },
+
+    /* From s16 invert */
+    { AOUT_FMT_S16_IE, VLC_FOURCC('f','l','3','2'), S16InverttoFloat32 },
+    { AOUT_FMT_S16_IE, AOUT_FMT_S16_NE,            Swap16 },
+    { AOUT_FMT_S16_IE, AOUT_FMT_U16_NE,            S16InverttoU16 },
+    { AOUT_FMT_S16_IE, VLC_FOURCC('s','8',' ',' '), S16InverttoS8 },
+    { AOUT_FMT_S16_IE, VLC_FOURCC('u','8',' ',' '), S16InverttoU8 },
+
+    /* From u16 invert */
+    { AOUT_FMT_U16_IE, VLC_FOURCC('f','l','3','2'), U16InverttoFloat32 },
+    { AOUT_FMT_U16_IE, AOUT_FMT_U16_NE,            Swap16 },
+    { AOUT_FMT_U16_IE, AOUT_FMT_S16_NE,            U16InverttoS16 },
+    { AOUT_FMT_U16_IE, VLC_FOURCC('s','8',' ',' '), U16InverttoS8 },
+    { AOUT_FMT_U16_IE, VLC_FOURCC('u','8',' ',' '), U16InverttoU8 },
+
+    { 0, 0, NULL },
+};
+
 
 /*****************************************************************************
  * Module descriptor
@@ -57,31 +174,20 @@ vlc_module_end();
 static int Open( vlc_object_t *p_this )
 {
     filter_t *p_filter = (filter_t *)p_this;
+    int i;
 
-    if( p_filter->fmt_in.i_codec == VLC_FOURCC('f','l','3','2') &&
-        p_filter->fmt_out.i_codec == AUDIO_FMT_S16_NE )
-    {
-        p_filter->pf_audio_filter = Float32toS16;
-    }
-    else if( p_filter->fmt_in.i_codec == VLC_FOURCC('f','l','3','2') &&
-             p_filter->fmt_out.i_codec == AUDIO_FMT_U16_NE )
-    {
-        p_filter->pf_audio_filter = Float32toU16;
-    }
-    else if( p_filter->fmt_in.i_codec == AUDIO_FMT_S16_NE &&
-             p_filter->fmt_out.i_codec == VLC_FOURCC('f','l','3','2') )
+    for( i = 0; ConvertTable[i].pf_convert != NULL; i++ )
     {
-        p_filter->pf_audio_filter = S16toFloat32;
+        if( ConvertTable[i].i_src == p_filter->fmt_in.i_codec &&
+            ConvertTable[i].i_dst == p_filter->fmt_out.i_codec )
+            break;
     }
-    else if( ( p_filter->fmt_in.i_codec == VLC_FOURCC('s','1','6','l') &&
-               p_filter->fmt_out.i_codec == VLC_FOURCC('s','1','6','b') ) ||
-             ( p_filter->fmt_in.i_codec == VLC_FOURCC('s','1','6','b') &&
-               p_filter->fmt_out.i_codec == VLC_FOURCC('s','1','6','l') ) )
-    {
-        p_filter->pf_audio_filter = S16Invert;
-    }
-    else return VLC_EGENERIC;
+    if( ConvertTable[i].pf_convert == NULL )
+        return VLC_EGENERIC;
+
     
+    p_filter->pf_audio_filter = ConvertTable[i].pf_convert;
+
     msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
              (char *)&p_filter->fmt_in.i_codec,
              (char *)&p_filter->fmt_out.i_codec,
@@ -184,7 +290,264 @@ static block_t *S16toFloat32( filter_t *p_filter, block_t *p_block )
     return p_block_out;
 }
 
-static block_t *S16Invert( filter_t *p_filter, block_t *p_block )
+static block_t *U16toFloat32( filter_t *p_filter, block_t *p_block )
+{
+    block_t *p_block_out;
+    int16_t *p_in;
+    float *p_out;
+    int i;
+
+    p_block_out =
+        p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
+    if( !p_block_out )
+    {
+        msg_Warn( p_filter, "can't get output buffer" );
+        return NULL;
+    }
+
+    p_in = (int16_t *)p_block->p_buffer;
+    p_out = (float *)p_block_out->p_buffer;
+
+    for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
+    {
+        *p_out++ = (float)(*p_in++ - 32768) / 32768.0;
+    }
+
+    p_block_out->i_samples = p_block->i_samples;
+    p_block_out->i_dts = p_block->i_dts;
+    p_block_out->i_pts = p_block->i_pts;
+    p_block_out->i_length = p_block->i_length;
+    p_block_out->i_rate = p_block->i_rate;
+
+    p_block->pf_release( p_block );
+    return p_block_out;
+}
+
+static block_t *S16toS8( filter_t *p_filter, block_t *p_block )
+{
+    int i;
+    int16_t *p_in = (int16_t *)p_block->p_buffer;
+    int8_t *p_out = (int8_t *)p_in;
+
+    for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
+        *p_out++ = (*p_in++) >> 8;
+
+    p_block->i_buffer /= 2;
+    return p_block;
+}
+static block_t *S16toU8( filter_t *p_filter, block_t *p_block )
+{
+    int i;
+    int16_t *p_in = (int16_t *)p_block->p_buffer;
+    uint8_t *p_out = (uint8_t *)p_in;
+
+    for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
+        *p_out++ = ((*p_in++) + 32768) >> 8;
+
+    p_block->i_buffer /= 2;
+    return p_block;
+}
+static block_t *S16toU16( filter_t *p_filter, block_t *p_block )
+{
+    int i;
+    int16_t *p_in = (int16_t *)p_block->p_buffer;
+    uint16_t *p_out = (uint16_t *)p_in;
+
+    for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
+        *p_out++ = (*p_in++) + 32768;
+
+    return p_block;
+}
+
+static block_t *U16toS8( filter_t *p_filter, block_t *p_block )
+{
+    int i;
+    uint16_t *p_in = (uint16_t *)p_block->p_buffer;
+    int8_t *p_out = (int8_t *)p_in;
+
+    for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
+        *p_out++ = ((int)(*p_in++) - 32768) >> 8;
+
+    p_block->i_buffer /= 2;
+    return p_block;
+}
+static block_t *U16toU8( filter_t *p_filter, block_t *p_block )
+{
+    int i;
+    uint16_t *p_in = (uint16_t *)p_block->p_buffer;
+    uint8_t *p_out = (uint8_t *)p_in;
+
+    for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
+        *p_out++ = (*p_in++) >> 8;
+
+    p_block->i_buffer /= 2;
+    return p_block;
+}
+static block_t *U16toS16( filter_t *p_filter, block_t *p_block )
+{
+    int i;
+    int16_t *p_in = (int16_t *)p_block->p_buffer;
+    uint16_t *p_out = (uint16_t *)p_in;
+
+    for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
+        *p_out++ = (int)(*p_in++) - 32768;
+
+    return p_block;
+}
+
+static block_t *S8toU8( filter_t *p_filter, block_t *p_block )
+{
+    int i;
+    int8_t *p_in = (int8_t *)p_block->p_buffer;
+    uint8_t *p_out = (uint8_t *)p_in;
+
+    for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
+        *p_out++ = ((*p_in++) + 128);
+
+    return p_block;
+}
+static block_t *U8toS8( filter_t *p_filter, block_t *p_block )
+{
+    int i;
+    uint8_t *p_in = (uint8_t *)p_block->p_buffer;
+    int8_t *p_out = (int8_t *)p_in;
+
+    for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
+        *p_out++ = ((*p_in++) - 128);
+
+    return p_block;
+}
+
+/* */
+static block_t *S8toU16( filter_t *p_filter, block_t *p_block )
+{
+    block_t *p_block_out;
+    int8_t *p_in;
+    uint16_t *p_out;
+    int i;
+
+    p_block_out =
+        p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
+    if( !p_block_out )
+    {
+        msg_Warn( p_filter, "can't get output buffer" );
+        return NULL;
+    }
+
+    p_in = (int8_t *)p_block->p_buffer;
+    p_out = (uint16_t *)p_block_out->p_buffer;
+
+    for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
+        *p_out++ = ((*p_in++) + 128) << 8;
+
+    p_block_out->i_samples = p_block->i_samples;
+    p_block_out->i_dts = p_block->i_dts;
+    p_block_out->i_pts = p_block->i_pts;
+    p_block_out->i_length = p_block->i_length;
+    p_block_out->i_rate = p_block->i_rate;
+
+    p_block->pf_release( p_block );
+    return p_block_out;
+}
+
+static block_t *U8toS16( filter_t *p_filter, block_t *p_block )
+{
+    block_t *p_block_out;
+    uint8_t *p_in;
+    int16_t *p_out;
+    int i;
+
+    p_block_out =
+        p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
+    if( !p_block_out )
+    {
+        msg_Warn( p_filter, "can't get output buffer" );
+        return NULL;
+    }
+
+    p_in = (uint8_t *)p_block->p_buffer;
+    p_out = (int16_t *)p_block_out->p_buffer;
+
+    for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
+        *p_out++ = ((*p_in++) - 128) << 8;
+
+    p_block_out->i_samples = p_block->i_samples;
+    p_block_out->i_dts = p_block->i_dts;
+    p_block_out->i_pts = p_block->i_pts;
+    p_block_out->i_length = p_block->i_length;
+    p_block_out->i_rate = p_block->i_rate;
+
+    p_block->pf_release( p_block );
+    return p_block_out;
+}
+
+
+static block_t *S8toS16( filter_t *p_filter, block_t *p_block )
+{
+    block_t *p_block_out;
+    int8_t *p_in;
+    int16_t *p_out;
+    int i;
+
+    p_block_out =
+        p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
+    if( !p_block_out )
+    {
+        msg_Warn( p_filter, "can't get output buffer" );
+        return NULL;
+    }
+
+    p_in = (int8_t *)p_block->p_buffer;
+    p_out = (int16_t *)p_block_out->p_buffer;
+
+    for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
+        *p_out++ = (*p_in++) << 8;
+
+    p_block_out->i_samples = p_block->i_samples;
+    p_block_out->i_dts = p_block->i_dts;
+    p_block_out->i_pts = p_block->i_pts;
+    p_block_out->i_length = p_block->i_length;
+    p_block_out->i_rate = p_block->i_rate;
+
+    p_block->pf_release( p_block );
+    return p_block_out;
+}
+
+static block_t *U8toU16( filter_t *p_filter, block_t *p_block )
+{
+    block_t *p_block_out;
+    uint8_t *p_in;
+    uint16_t *p_out;
+    int i;
+
+    p_block_out =
+        p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
+    if( !p_block_out )
+    {
+        msg_Warn( p_filter, "can't get output buffer" );
+        return NULL;
+    }
+
+    p_in = (uint8_t *)p_block->p_buffer;
+    p_out = (uint16_t *)p_block_out->p_buffer;
+
+    for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
+        *p_out++ = (*p_in++) << 8;
+
+    p_block_out->i_samples = p_block->i_samples;
+    p_block_out->i_dts = p_block->i_dts;
+    p_block_out->i_pts = p_block->i_pts;
+    p_block_out->i_length = p_block->i_length;
+    p_block_out->i_rate = p_block->i_rate;
+
+    p_block->pf_release( p_block );
+    return p_block_out;
+}
+
+/*****************************************************************************
+ * Swap a buffer of word
+ *****************************************************************************/
+static block_t *Swap16( filter_t *p_filter, block_t *p_block )
 {
     int i;
     uint8_t *p_in = (uint8_t *)p_block->p_buffer;
@@ -200,3 +563,58 @@ static block_t *S16Invert( filter_t *p_filter, block_t *p_block )
 
     return p_block;
 }
+
+#define CONVERT_NN( func, f_in, f_out, b_pre_invert, b_post_invert ) \
+static block_t *func( filter_t *p_filter, block_t *p_block ) \
+{                                                   \
+    if( b_pre_invert )                              \
+        Swap16( p_filter, p_block );                \
+                                                    \
+    p_block = f_in##to##f_out( p_filter, p_block ); \
+                                                    \
+    if( b_post_invert )                             \
+        Swap16( p_filter, p_block );                \
+                                                    \
+    return p_block;                                 \
+}
+
+CONVERT_NN( Float32toS16Invert, Float32, S16, 0, 1 )
+CONVERT_NN( Float32toU16Invert, Float32, U16, 0, 1 )
+
+CONVERT_NN( S16InverttoFloat32, S16, Float32, 1, 0 )
+CONVERT_NN( S16InverttoS8,      S16, S8,      1, 0 )
+CONVERT_NN( S16InverttoU8,      S16, U8,      1, 0 )
+CONVERT_NN( S16InverttoU16,     S16, U16,     1, 0 )
+
+CONVERT_NN( U16InverttoFloat32, U16, Float32, 1, 0 )
+CONVERT_NN( U16InverttoS8,      U16, S8,      1, 0 )
+CONVERT_NN( U16InverttoU8,      U16, U8,      1, 0 )
+CONVERT_NN( U16InverttoS16,     U16, S16,     1, 0 )
+
+#undef CONVERT_NN
+
+#define CONVERT_INDIRECT( func, f_in, f_mid, f_out )                    \
+static block_t *func( filter_t *p_filter, block_t *p_block )            \
+{                                                                       \
+    return f_mid##to##f_out( p_filter,                                  \
+                             f_in##to##f_mid( p_filter, p_block ) );    \
+}
+
+CONVERT_INDIRECT( Float32toS8,   Float32, S16, U8 )
+CONVERT_INDIRECT( Float32toU8,   Float32, U16, U8 )
+CONVERT_INDIRECT( S8toFloat32,   S8,      S16, Float32 )
+CONVERT_INDIRECT( U8toFloat32,   U8,      U16, Float32 )
+
+#define S16toS16Invert Swap16
+#define U16toU16Invert Swap16
+
+CONVERT_INDIRECT( U8toS16Invert, U8,      S16, S16Invert )
+CONVERT_INDIRECT( S8toU16Invert, S8,      U16, U16Invert )
+
+CONVERT_INDIRECT( U8toU16Invert, U8,      U16, U16Invert )
+CONVERT_INDIRECT( S8toS16Invert, S8,      S16, S16Invert )
+
+#undef CONVERT_INDIRECT
+
+
+