Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

format.c

00001 /*****************************************************************************
00002  * format.c : PCM format converter
00003  *****************************************************************************
00004  * Copyright (C) 2002-2005 the VideoLAN team
00005  * $Id: format.c 11664 2005-07-09 06:17:09Z courmisch $
00006  *
00007  * Authors: Christophe Massiot <[email protected]>
00008  *          Gildas Bazin <[email protected]>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 
00025 /*****************************************************************************
00026  * Preamble
00027  *****************************************************************************/
00028 #include <stdlib.h>                                      /* malloc(), free() */
00029 #include <string.h>
00030 
00031 #include <vlc/vlc.h>
00032 #include <vlc/decoder.h>
00033 #include "vlc_filter.h"
00034 
00035 #ifdef WORDS_BIGENDIAN
00036 #   define AOUT_FMT_S24_IE VLC_FOURCC('s','2','4','l')
00037 #   define AOUT_FMT_S16_IE VLC_FOURCC('s','1','6','l')
00038 #   define AOUT_FMT_U16_IE VLC_FOURCC('u','1','6','l')
00039 #else
00040 #   define AOUT_FMT_S24_IE VLC_FOURCC('s','2','4','b')
00041 #   define AOUT_FMT_S16_IE VLC_FOURCC('s','1','6','b')
00042 #   define AOUT_FMT_U16_IE VLC_FOURCC('u','1','6','b')
00043 #endif
00044 
00045 
00046 /*****************************************************************************
00047  * Local prototypes
00048  *****************************************************************************/
00049 static int  Open ( vlc_object_t * );
00050 
00051 static block_t *Float32toS24( filter_t *, block_t * );
00052 static block_t *Float32toS16( filter_t *, block_t * );
00053 static block_t *Float32toU16( filter_t *, block_t * );
00054 static block_t *Float32toS8 ( filter_t *, block_t * );
00055 static block_t *Float32toU8 ( filter_t *, block_t * );
00056 
00057 static block_t *S24toFloat32  ( filter_t *, block_t * );
00058 static block_t *S24toS16      ( filter_t *, block_t * );
00059 static block_t *S24toS16Invert( filter_t *, block_t * );
00060 
00061 static block_t *S16toFloat32  ( filter_t *, block_t * );
00062 static block_t *S16toS24      ( filter_t *, block_t * );
00063 static block_t *S16toS24Invert( filter_t *, block_t * );
00064 static block_t *S16toS8       ( filter_t *, block_t * );
00065 static block_t *S16toU8       ( filter_t *, block_t * );
00066 static block_t *S16toU16      ( filter_t *, block_t * );
00067 
00068 static block_t *U16toFloat32( filter_t *, block_t * );
00069 static block_t *U16toS8     ( filter_t *, block_t * );
00070 static block_t *U16toU8     ( filter_t *, block_t * );
00071 static block_t *U16toS16    ( filter_t *, block_t * );
00072 
00073 static block_t *Float32toS24Invert( filter_t *, block_t * );
00074 static block_t *Float32toS16Invert( filter_t *, block_t * );
00075 static block_t *Float32toU16Invert( filter_t *, block_t * );
00076 
00077 static block_t *S24InverttoFloat32  ( filter_t *, block_t * );
00078 static block_t *S24InverttoS16      ( filter_t *, block_t * );
00079 static block_t *S24InverttoS16Invert( filter_t *, block_t * );
00080 
00081 static block_t *S16InverttoFloat32  ( filter_t *, block_t * );
00082 static block_t *S16InverttoS24      ( filter_t *, block_t * );
00083 static block_t *S16InverttoS24Invert( filter_t *, block_t * );
00084 static block_t *S16InverttoS8       ( filter_t *, block_t * );
00085 static block_t *S16InverttoU8       ( filter_t *, block_t * );
00086 static block_t *S16InverttoU16      ( filter_t *, block_t * );
00087 
00088 static block_t *U16InverttoFloat32( filter_t *, block_t * );
00089 static block_t *U16InverttoS8     ( filter_t *, block_t * );
00090 static block_t *U16InverttoU8     ( filter_t *, block_t * );
00091 static block_t *U16InverttoS16    ( filter_t *, block_t * );
00092 
00093 static block_t *S8toFloat32  ( filter_t *, block_t * );
00094 static block_t *S8toS16      ( filter_t *, block_t * );
00095 static block_t *S8toU16      ( filter_t *, block_t * );
00096 static block_t *S8toU8       ( filter_t *, block_t * );
00097 static block_t *S8toS16Invert( filter_t *, block_t * );
00098 static block_t *S8toU16Invert( filter_t *, block_t * );
00099 
00100 static block_t *U8toFloat32  ( filter_t *, block_t * );
00101 static block_t *U8toFloat32  ( filter_t *, block_t * );
00102 static block_t *U8toS16      ( filter_t *, block_t * );
00103 static block_t *U8toU16      ( filter_t *, block_t * );
00104 static block_t *U8toS8       ( filter_t *, block_t * );
00105 static block_t *U8toS16Invert( filter_t *, block_t * );
00106 static block_t *U8toU16Invert( filter_t *, block_t * );
00107 
00108 
00109 static block_t *U8toS8( filter_t *, block_t * );
00110 static block_t *S8toU8( filter_t *, block_t * );
00111 
00112 
00113 static block_t *Swap16( filter_t *, block_t * );
00114 static block_t *Swap24( filter_t *, block_t * );
00115 
00116 static struct
00117 {
00118     vlc_fourcc_t i_src;
00119     vlc_fourcc_t i_dst;
00120     block_t *(*pf_convert)( filter_t *, block_t *);
00121 } ConvertTable[] =
00122 {
00123     /* From fl32 */
00124     { VLC_FOURCC('f','l','3','2'), AOUT_FMT_S24_NE, Float32toS24 },
00125     { VLC_FOURCC('f','l','3','2'), AOUT_FMT_S16_NE, Float32toS16 },
00126     { VLC_FOURCC('f','l','3','2'), AOUT_FMT_U16_NE, Float32toU16 },
00127     { VLC_FOURCC('f','l','3','2'), AOUT_FMT_S24_IE, Float32toS24Invert },
00128     { VLC_FOURCC('f','l','3','2'), AOUT_FMT_S16_IE, Float32toS16Invert },
00129     { VLC_FOURCC('f','l','3','2'), AOUT_FMT_U16_IE, Float32toU16Invert },
00130     { VLC_FOURCC('f','l','3','2'), VLC_FOURCC('s','8',' ',' '), Float32toS8 },
00131     { VLC_FOURCC('f','l','3','2'), VLC_FOURCC('u','8',' ',' '), Float32toU8 },
00132 
00133     /* From s24 invert */
00134     { AOUT_FMT_S24_NE, VLC_FOURCC('f','l','3','2'), S24toFloat32 },
00135     { AOUT_FMT_S24_NE, AOUT_FMT_S24_IE,             Swap24 },
00136     { AOUT_FMT_S24_NE, AOUT_FMT_S16_NE,             S24toS16 },
00137     { AOUT_FMT_S24_NE, AOUT_FMT_S16_IE,             S24toS16Invert },
00138 
00139     /* From s16 */
00140     { AOUT_FMT_S16_NE, VLC_FOURCC('f','l','3','2'), S16toFloat32 },
00141     { AOUT_FMT_S16_NE, AOUT_FMT_S24_NE,             S16toS24 },
00142     { AOUT_FMT_S16_NE, AOUT_FMT_S24_IE,             S16toS24Invert },
00143     { AOUT_FMT_S16_NE, AOUT_FMT_S16_IE,             Swap16 },
00144     { AOUT_FMT_S16_NE, AOUT_FMT_U16_IE,             S16toU16 },
00145     { AOUT_FMT_S16_NE, VLC_FOURCC('s','8',' ',' '), S16toS8 },
00146     { AOUT_FMT_S16_NE, VLC_FOURCC('u','8',' ',' '), S16toU8 },
00147 
00148     /* From u16 */
00149     { AOUT_FMT_U16_NE, VLC_FOURCC('f','l','3','2'), U16toFloat32 },
00150     { AOUT_FMT_U16_NE, AOUT_FMT_U16_IE,             Swap16 },
00151     { AOUT_FMT_U16_NE, AOUT_FMT_S16_IE,             U16toS16 },
00152     { AOUT_FMT_U16_NE, VLC_FOURCC('s','8',' ',' '), U16toS8 },
00153     { AOUT_FMT_U16_NE, VLC_FOURCC('u','8',' ',' '), U16toU8 },
00154 
00155     /* From s8 */
00156     { VLC_FOURCC('s','8',' ',' '), VLC_FOURCC('f','l','3','2'), S8toFloat32 },
00157     { VLC_FOURCC('s','8',' ',' '), AOUT_FMT_S16_NE,             S8toS16 },
00158     { VLC_FOURCC('s','8',' ',' '), AOUT_FMT_S16_IE,             S8toS16Invert },
00159     { VLC_FOURCC('s','8',' ',' '), AOUT_FMT_U16_NE,             S8toU16 },
00160     { VLC_FOURCC('s','8',' ',' '), AOUT_FMT_U16_IE,             S8toU16Invert },
00161     { VLC_FOURCC('s','8',' ',' '), VLC_FOURCC('u','8',' ',' '), S8toU8 },
00162     
00163     /* From u8 */
00164     { VLC_FOURCC('u','8',' ',' '), VLC_FOURCC('f','l','3','2'), U8toFloat32 },
00165     { VLC_FOURCC('u','8',' ',' '), AOUT_FMT_S16_NE,             U8toS16 },
00166     { VLC_FOURCC('u','8',' ',' '), AOUT_FMT_S16_IE,             U8toS16Invert },
00167     { VLC_FOURCC('u','8',' ',' '), AOUT_FMT_U16_NE,             U8toU16 },
00168     { VLC_FOURCC('u','8',' ',' '), AOUT_FMT_U16_IE,             U8toU16Invert },
00169     { VLC_FOURCC('u','8',' ',' '), VLC_FOURCC('s','8',' ',' '), U8toS8 },
00170 
00171     /* From s24 invert */
00172     { AOUT_FMT_S24_IE, VLC_FOURCC('f','l','3','2'), S24InverttoFloat32 },
00173     { AOUT_FMT_S24_IE, AOUT_FMT_S24_NE,             Swap24 },
00174     { AOUT_FMT_S24_IE, AOUT_FMT_S16_NE,             S24InverttoS16 },
00175     { AOUT_FMT_S24_IE, AOUT_FMT_S16_IE,             S24InverttoS16Invert },
00176 
00177     /* From s16 invert */
00178     { AOUT_FMT_S16_IE, VLC_FOURCC('f','l','3','2'), S16InverttoFloat32 },
00179     { AOUT_FMT_S16_IE, AOUT_FMT_S24_NE,             S16InverttoS24 },
00180     { AOUT_FMT_S16_IE, AOUT_FMT_S24_IE,             S16InverttoS24Invert },
00181     { AOUT_FMT_S16_IE, AOUT_FMT_S16_NE,             Swap16 },
00182     { AOUT_FMT_S16_IE, AOUT_FMT_U16_NE,             S16InverttoU16 },
00183     { AOUT_FMT_S16_IE, VLC_FOURCC('s','8',' ',' '), S16InverttoS8 },
00184     { AOUT_FMT_S16_IE, VLC_FOURCC('u','8',' ',' '), S16InverttoU8 },
00185 
00186     /* From u16 invert */
00187     { AOUT_FMT_U16_IE, VLC_FOURCC('f','l','3','2'), U16InverttoFloat32 },
00188     { AOUT_FMT_U16_IE, AOUT_FMT_U16_NE,             Swap16 },
00189     { AOUT_FMT_U16_IE, AOUT_FMT_S16_NE,             U16InverttoS16 },
00190     { AOUT_FMT_U16_IE, VLC_FOURCC('s','8',' ',' '), U16InverttoS8 },
00191     { AOUT_FMT_U16_IE, VLC_FOURCC('u','8',' ',' '), U16InverttoU8 },
00192 
00193     { 0, 0, NULL },
00194 };
00195 
00196 
00197 /*****************************************************************************
00198  * Module descriptor
00199  *****************************************************************************/
00200 vlc_module_begin();
00201     set_description( _("audio filter for PCM format conversion") );
00202     set_category( CAT_AUDIO );
00203     set_subcategory( SUBCAT_AUDIO_MISC );
00204     set_capability( "audio filter2", 1 );
00205     set_callbacks( Open, NULL );
00206 vlc_module_end();
00207 
00208 /*****************************************************************************
00209  * Open:
00210  *****************************************************************************/
00211 static int Open( vlc_object_t *p_this )
00212 {
00213     filter_t *p_filter = (filter_t *)p_this;
00214     int i;
00215 
00216     for( i = 0; ConvertTable[i].pf_convert != NULL; i++ )
00217     {
00218         if( ConvertTable[i].i_src == p_filter->fmt_in.i_codec &&
00219             ConvertTable[i].i_dst == p_filter->fmt_out.i_codec )
00220             break;
00221     }
00222     if( ConvertTable[i].pf_convert == NULL )
00223         return VLC_EGENERIC;
00224 
00225     p_filter->pf_audio_filter = ConvertTable[i].pf_convert;
00226     p_filter->fmt_out.audio = p_filter->fmt_in.audio;
00227     p_filter->fmt_out.audio.i_format = p_filter->fmt_out.i_codec;
00228 
00229     msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
00230              (char *)&p_filter->fmt_in.i_codec,
00231              (char *)&p_filter->fmt_out.i_codec,
00232              p_filter->fmt_in.audio.i_bitspersample );
00233 
00234     return VLC_SUCCESS;
00235 }
00236 
00237 /*****************************************************************************
00238  * Convert a buffer
00239  *****************************************************************************/
00240 static block_t *Float32toS24( filter_t *p_filter, block_t *p_block )
00241 {
00242     int i;
00243     float *p_in = (float *)p_block->p_buffer;
00244     uint8_t *p_out = (uint8_t *)p_in;
00245     int32_t out;
00246 
00247     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
00248     {
00249         if ( *p_in >= 1.0 ) out = 8388607;
00250         else if ( *p_in < -1.0 ) out = -8388608;
00251         else out = *p_in * 8388608.0;
00252 
00253 #ifdef WORDS_BIGENDIAN
00254         *((int16_t *)p_out) = out >> 8;
00255         p_out[2] = out & 0xFF;
00256 #else
00257         *((int16_t *)(p_out+1)) = out >> 8;
00258         p_out[0] = out & 0xFF;
00259 #endif
00260 
00261         p_in++; p_out += 3;
00262     }
00263 
00264     p_block->i_buffer = p_block->i_buffer * 3 / 4;
00265     return p_block;
00266 }
00267 
00268 static block_t *Float32toS16( filter_t *p_filter, block_t *p_block )
00269 {
00270     int i;
00271     float *p_in = (float *)p_block->p_buffer;
00272     int16_t *p_out = (int16_t *)p_in;
00273 
00274     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
00275     {
00276 #if 0
00277         /* Slow version. */
00278         if ( *p_in >= 1.0 ) *p_out = 32767;
00279         else if ( *p_in < -1.0 ) *p_out = -32768;
00280         else *p_out = *p_in * 32768.0;
00281 #else
00282         /* This is walken's trick based on IEEE float format. */
00283         union { float f; int32_t i; } u;
00284         u.f = *p_in + 384.0;
00285         if ( u.i > 0x43c07fff ) *p_out = 32767;
00286         else if ( u.i < 0x43bf8000 ) *p_out = -32768;
00287         else *p_out = u.i - 0x43c00000;
00288 #endif
00289         p_in++; p_out++;
00290     }
00291 
00292     p_block->i_buffer /= 2;
00293     return p_block;
00294 }
00295 
00296 static block_t *Float32toU16( filter_t *p_filter, block_t *p_block )
00297 {
00298     int i;
00299     float *p_in = (float *)p_block->p_buffer;
00300     uint16_t *p_out = (uint16_t *)p_in;
00301 
00302     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
00303     {
00304         if ( *p_in >= 1.0 ) *p_out = 65535;
00305         else if ( *p_in < -1.0 ) *p_out = 0;
00306         else *p_out = (uint16_t)(32768 + *p_in * 32768);
00307         p_in++; p_out++;
00308     }
00309 
00310     p_block->i_buffer /= 2;
00311     return p_block;
00312 }
00313 
00314 static block_t *S24toFloat32( filter_t *p_filter, block_t *p_block )
00315 {
00316     block_t *p_block_out;
00317     uint8_t *p_in;
00318     float *p_out;
00319     int i;
00320 
00321     p_block_out =
00322         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*4/3 );
00323     if( !p_block_out )
00324     {
00325         msg_Warn( p_filter, "can't get output buffer" );
00326         return NULL;
00327     }
00328 
00329     p_in = p_block->p_buffer;
00330     p_out = (float *)p_block_out->p_buffer;
00331 
00332     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
00333     {
00334 #ifdef WORDS_BIGENDIAN
00335         *p_out = ((float)( (((int32_t)*(int16_t *)(p_in)) << 8) + p_in[2]))
00336 #else
00337         *p_out = ((float)( (((int32_t)*(int16_t *)(p_in+1)) << 8) + p_in[0]))
00338 #endif
00339             / 8388608.0;
00340 
00341         p_in += 3; p_out++;
00342     }
00343 
00344     p_block_out->i_samples = p_block->i_samples;
00345     p_block_out->i_dts = p_block->i_dts;
00346     p_block_out->i_pts = p_block->i_pts;
00347     p_block_out->i_length = p_block->i_length;
00348     p_block_out->i_rate = p_block->i_rate;
00349 
00350     p_block->pf_release( p_block );
00351     return p_block_out;
00352 }
00353 
00354 static block_t *S24toS16( filter_t *p_filter, block_t *p_block )
00355 {
00356     int i;
00357     uint8_t *p_in = (uint8_t *)p_block->p_buffer;
00358     uint8_t *p_out = (uint8_t *)p_in;
00359 
00360     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
00361     {
00362 #ifdef WORDS_BIGENDIAN
00363         *p_out++ = *p_in++;
00364         *p_out++ = *p_in++;
00365         p_in++;
00366 #else
00367         p_in++;
00368         *p_out++ = *p_in++;
00369         *p_out++ = *p_in++;
00370 #endif
00371     }
00372 
00373     p_block->i_buffer = p_block->i_buffer * 2 / 3;
00374     return p_block;
00375 }
00376 
00377 static block_t *S16toFloat32( filter_t *p_filter, block_t *p_block )
00378 {
00379     block_t *p_block_out;
00380     int16_t *p_in;
00381     float *p_out;
00382     int i;
00383 
00384     p_block_out =
00385         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
00386     if( !p_block_out )
00387     {
00388         msg_Warn( p_filter, "can't get output buffer" );
00389         return NULL;
00390     }
00391 
00392     p_in = (int16_t *)p_block->p_buffer;
00393     p_out = (float *)p_block_out->p_buffer;
00394 
00395     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
00396     {
00397 #if 0
00398         /* Slow version */
00399         *p_out = (float)*p_in / 32768.0;
00400 #else
00401         /* This is walken's trick based on IEEE float format. On my PIII
00402          * this takes 16 seconds to perform one billion conversions, instead
00403          * of 19 seconds for the above division. */
00404         union { float f; int32_t i; } u;
00405         u.i = *p_in + 0x43c00000;
00406         *p_out = u.f - 384.0;
00407 #endif
00408 
00409         p_in++; p_out++;
00410     }
00411 
00412     p_block_out->i_samples = p_block->i_samples;
00413     p_block_out->i_dts = p_block->i_dts;
00414     p_block_out->i_pts = p_block->i_pts;
00415     p_block_out->i_length = p_block->i_length;
00416     p_block_out->i_rate = p_block->i_rate;
00417 
00418     p_block->pf_release( p_block );
00419     return p_block_out;
00420 }
00421 
00422 static block_t *U16toFloat32( filter_t *p_filter, block_t *p_block )
00423 {
00424     block_t *p_block_out;
00425     int16_t *p_in;
00426     float *p_out;
00427     int i;
00428 
00429     p_block_out =
00430         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
00431     if( !p_block_out )
00432     {
00433         msg_Warn( p_filter, "can't get output buffer" );
00434         return NULL;
00435     }
00436 
00437     p_in = (int16_t *)p_block->p_buffer;
00438     p_out = (float *)p_block_out->p_buffer;
00439 
00440     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
00441     {
00442         *p_out++ = (float)(*p_in++ - 32768) / 32768.0;
00443     }
00444 
00445     p_block_out->i_samples = p_block->i_samples;
00446     p_block_out->i_dts = p_block->i_dts;
00447     p_block_out->i_pts = p_block->i_pts;
00448     p_block_out->i_length = p_block->i_length;
00449     p_block_out->i_rate = p_block->i_rate;
00450 
00451     p_block->pf_release( p_block );
00452     return p_block_out;
00453 }
00454 
00455 static block_t *S16toS24( filter_t *p_filter, block_t *p_block )
00456 {
00457     block_t *p_block_out;
00458     uint8_t *p_in, *p_out;
00459     int i;
00460 
00461     p_block_out =
00462         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*3/2 );
00463     if( !p_block_out )
00464     {
00465         msg_Warn( p_filter, "can't get output buffer" );
00466         return NULL;
00467     }
00468 
00469     p_in = (uint8_t *)p_block->p_buffer;
00470     p_out = (uint8_t *)p_block_out->p_buffer;
00471 
00472     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
00473     {
00474 #ifdef WORDS_BIGENDIAN
00475         *p_out++ = *p_in++;
00476         *p_out++ = *p_in++;
00477         *p_out++ = 0;
00478 #else
00479         *p_out++ = 0;
00480         *p_out++ = *p_in++;
00481         *p_out++ = *p_in++;
00482 #endif
00483     }
00484 
00485     p_block_out->i_samples = p_block->i_samples;
00486     p_block_out->i_dts = p_block->i_dts;
00487     p_block_out->i_pts = p_block->i_pts;
00488     p_block_out->i_length = p_block->i_length;
00489     p_block_out->i_rate = p_block->i_rate;
00490 
00491     p_block->pf_release( p_block );
00492     return p_block_out;
00493 }
00494 
00495 static block_t *S16toS8( filter_t *p_filter, block_t *p_block )
00496 {
00497     int i;
00498     int16_t *p_in = (int16_t *)p_block->p_buffer;
00499     int8_t *p_out = (int8_t *)p_in;
00500 
00501     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
00502         *p_out++ = (*p_in++) >> 8;
00503 
00504     p_block->i_buffer /= 2;
00505     return p_block;
00506 }
00507 static block_t *S16toU8( filter_t *p_filter, block_t *p_block )
00508 {
00509     int i;
00510     int16_t *p_in = (int16_t *)p_block->p_buffer;
00511     uint8_t *p_out = (uint8_t *)p_in;
00512 
00513     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
00514         *p_out++ = ((*p_in++) + 32768) >> 8;
00515 
00516     p_block->i_buffer /= 2;
00517     return p_block;
00518 }
00519 static block_t *S16toU16( filter_t *p_filter, block_t *p_block )
00520 {
00521     int i;
00522     int16_t *p_in = (int16_t *)p_block->p_buffer;
00523     uint16_t *p_out = (uint16_t *)p_in;
00524 
00525     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
00526         *p_out++ = (*p_in++) + 32768;
00527 
00528     return p_block;
00529 }
00530 
00531 static block_t *U16toS8( filter_t *p_filter, block_t *p_block )
00532 {
00533     int i;
00534     uint16_t *p_in = (uint16_t *)p_block->p_buffer;
00535     int8_t *p_out = (int8_t *)p_in;
00536 
00537     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
00538         *p_out++ = ((int)(*p_in++) - 32768) >> 8;
00539 
00540     p_block->i_buffer /= 2;
00541     return p_block;
00542 }
00543 static block_t *U16toU8( filter_t *p_filter, block_t *p_block )
00544 {
00545     int i;
00546     uint16_t *p_in = (uint16_t *)p_block->p_buffer;
00547     uint8_t *p_out = (uint8_t *)p_in;
00548 
00549     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
00550         *p_out++ = (*p_in++) >> 8;
00551 
00552     p_block->i_buffer /= 2;
00553     return p_block;
00554 }
00555 static block_t *U16toS16( filter_t *p_filter, block_t *p_block )
00556 {
00557     int i;
00558     int16_t *p_in = (int16_t *)p_block->p_buffer;
00559     uint16_t *p_out = (uint16_t *)p_in;
00560 
00561     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
00562         *p_out++ = (int)(*p_in++) - 32768;
00563 
00564     return p_block;
00565 }
00566 
00567 static block_t *S8toU8( filter_t *p_filter, block_t *p_block )
00568 {
00569     int i;
00570     int8_t *p_in = (int8_t *)p_block->p_buffer;
00571     uint8_t *p_out = (uint8_t *)p_in;
00572 
00573     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
00574         *p_out++ = ((*p_in++) + 128);
00575 
00576     return p_block;
00577 }
00578 static block_t *U8toS8( filter_t *p_filter, block_t *p_block )
00579 {
00580     int i;
00581     uint8_t *p_in = (uint8_t *)p_block->p_buffer;
00582     int8_t *p_out = (int8_t *)p_in;
00583 
00584     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
00585         *p_out++ = ((*p_in++) - 128);
00586 
00587     return p_block;
00588 }
00589 
00590 /* */
00591 static block_t *S8toU16( filter_t *p_filter, block_t *p_block )
00592 {
00593     block_t *p_block_out;
00594     int8_t *p_in;
00595     uint16_t *p_out;
00596     int i;
00597 
00598     p_block_out =
00599         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
00600     if( !p_block_out )
00601     {
00602         msg_Warn( p_filter, "can't get output buffer" );
00603         return NULL;
00604     }
00605 
00606     p_in = (int8_t *)p_block->p_buffer;
00607     p_out = (uint16_t *)p_block_out->p_buffer;
00608 
00609     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
00610         *p_out++ = ((*p_in++) + 128) << 8;
00611 
00612     p_block_out->i_samples = p_block->i_samples;
00613     p_block_out->i_dts = p_block->i_dts;
00614     p_block_out->i_pts = p_block->i_pts;
00615     p_block_out->i_length = p_block->i_length;
00616     p_block_out->i_rate = p_block->i_rate;
00617 
00618     p_block->pf_release( p_block );
00619     return p_block_out;
00620 }
00621 
00622 static block_t *U8toS16( filter_t *p_filter, block_t *p_block )
00623 {
00624     block_t *p_block_out;
00625     uint8_t *p_in;
00626     int16_t *p_out;
00627     int i;
00628 
00629     p_block_out =
00630         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
00631     if( !p_block_out )
00632     {
00633         msg_Warn( p_filter, "can't get output buffer" );
00634         return NULL;
00635     }
00636 
00637     p_in = (uint8_t *)p_block->p_buffer;
00638     p_out = (int16_t *)p_block_out->p_buffer;
00639 
00640     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
00641         *p_out++ = ((*p_in++) - 128) << 8;
00642 
00643     p_block_out->i_samples = p_block->i_samples;
00644     p_block_out->i_dts = p_block->i_dts;
00645     p_block_out->i_pts = p_block->i_pts;
00646     p_block_out->i_length = p_block->i_length;
00647     p_block_out->i_rate = p_block->i_rate;
00648 
00649     p_block->pf_release( p_block );
00650     return p_block_out;
00651 }
00652 
00653 
00654 static block_t *S8toS16( filter_t *p_filter, block_t *p_block )
00655 {
00656     block_t *p_block_out;
00657     int8_t *p_in;
00658     int16_t *p_out;
00659     int i;
00660 
00661     p_block_out =
00662         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
00663     if( !p_block_out )
00664     {
00665         msg_Warn( p_filter, "can't get output buffer" );
00666         return NULL;
00667     }
00668 
00669     p_in = (int8_t *)p_block->p_buffer;
00670     p_out = (int16_t *)p_block_out->p_buffer;
00671 
00672     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
00673         *p_out++ = (*p_in++) << 8;
00674 
00675     p_block_out->i_samples = p_block->i_samples;
00676     p_block_out->i_dts = p_block->i_dts;
00677     p_block_out->i_pts = p_block->i_pts;
00678     p_block_out->i_length = p_block->i_length;
00679     p_block_out->i_rate = p_block->i_rate;
00680 
00681     p_block->pf_release( p_block );
00682     return p_block_out;
00683 }
00684 
00685 static block_t *U8toU16( filter_t *p_filter, block_t *p_block )
00686 {
00687     block_t *p_block_out;
00688     uint8_t *p_in;
00689     uint16_t *p_out;
00690     int i;
00691 
00692     p_block_out =
00693         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
00694     if( !p_block_out )
00695     {
00696         msg_Warn( p_filter, "can't get output buffer" );
00697         return NULL;
00698     }
00699 
00700     p_in = (uint8_t *)p_block->p_buffer;
00701     p_out = (uint16_t *)p_block_out->p_buffer;
00702 
00703     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
00704         *p_out++ = (*p_in++) << 8;
00705 
00706     p_block_out->i_samples = p_block->i_samples;
00707     p_block_out->i_dts = p_block->i_dts;
00708     p_block_out->i_pts = p_block->i_pts;
00709     p_block_out->i_length = p_block->i_length;
00710     p_block_out->i_rate = p_block->i_rate;
00711 
00712     p_block->pf_release( p_block );
00713     return p_block_out;
00714 }
00715 
00716 /*****************************************************************************
00717  * Swap a buffer of words
00718  *****************************************************************************/
00719 static block_t *Swap16( filter_t *p_filter, block_t *p_block )
00720 {
00721     int i;
00722     uint8_t *p_in = (uint8_t *)p_block->p_buffer;
00723     uint8_t tmp;
00724 
00725     for( i = 0; i < p_block->i_buffer / 2; i++ )
00726     {
00727         tmp = p_in[0];
00728         p_in[0] = p_in[1];
00729         p_in[1] = tmp;
00730         p_in += 2;
00731     }
00732 
00733     return p_block;
00734 }
00735 
00736 static block_t *Swap24( filter_t *p_filter, block_t *p_block )
00737 {
00738     int i;
00739     uint8_t *p_in = (uint8_t *)p_block->p_buffer;
00740     uint8_t tmp;
00741 
00742     for( i = 0; i < p_block->i_buffer / 3; i++ )
00743     {
00744         tmp = p_in[0];
00745         p_in[0] = p_in[2];
00746         p_in[2] = tmp;
00747         p_in += 3;
00748     }
00749 
00750     return p_block;
00751 }
00752 
00753 #define CONVERT_NN( func, f_in, f_out, b_pre_invert, b_post_invert, swapa, swapb ) \
00754 static block_t *func( filter_t *p_filter, block_t *p_block ) \
00755 {                                                   \
00756     if( b_pre_invert )                              \
00757         swapa( p_filter, p_block );                  \
00758                                                     \
00759     p_block = f_in##to##f_out( p_filter, p_block ); \
00760                                                     \
00761     if( b_post_invert )                             \
00762         swapb( p_filter, p_block );                  \
00763                                                     \
00764     return p_block;                                 \
00765 }
00766 
00767 CONVERT_NN( Float32toS24Invert, Float32, S24, 0, 1, Swap24, Swap24 )
00768 CONVERT_NN( Float32toS16Invert, Float32, S16, 0, 1, Swap16, Swap16 )
00769 CONVERT_NN( Float32toU16Invert, Float32, U16, 0, 1, Swap16, Swap16 )
00770 
00771 CONVERT_NN( S24InverttoFloat32, S24, Float32, 1, 0, Swap24, Swap24 )
00772 CONVERT_NN( S24InverttoS16,     S24, S16,     1, 0, Swap24, Swap16 )
00773 CONVERT_NN( S24InverttoS16Invert, S24, S16,   1, 1, Swap24, Swap16 )
00774 CONVERT_NN( S24toS16Invert,     S24, S16,     0, 1, Swap24, Swap16 )
00775 
00776 CONVERT_NN( S16InverttoFloat32, S16, Float32, 1, 0, Swap16, Swap16 )
00777 CONVERT_NN( S16InverttoS24,     S16, S24,     1, 0, Swap16, Swap24 )
00778 CONVERT_NN( S16toS24Invert,     S16, S24,     0, 1, Swap16, Swap24 )
00779 CONVERT_NN( S16InverttoS24Invert, S16, S24,   1, 1, Swap16, Swap24 )
00780 CONVERT_NN( S16InverttoS8,      S16, S8,      1, 0, Swap16, Swap16 )
00781 CONVERT_NN( S16InverttoU8,      S16, U8,      1, 0, Swap16, Swap16 )
00782 CONVERT_NN( S16InverttoU16,     S16, U16,     1, 0, Swap16, Swap16 )
00783 
00784 CONVERT_NN( U16InverttoFloat32, U16, Float32, 1, 0, Swap16, Swap16 )
00785 CONVERT_NN( U16InverttoS8,      U16, S8,      1, 0, Swap16, Swap16 )
00786 CONVERT_NN( U16InverttoU8,      U16, U8,      1, 0, Swap16, Swap16 )
00787 CONVERT_NN( U16InverttoS16,     U16, S16,     1, 0, Swap16, Swap16 )
00788 
00789 #undef CONVERT_NN
00790 
00791 #define CONVERT_INDIRECT( func, f_in, f_mid, f_out )                    \
00792 static block_t *func( filter_t *p_filter, block_t *p_block )            \
00793 {                                                                       \
00794     return f_mid##to##f_out( p_filter,                                  \
00795                              f_in##to##f_mid( p_filter, p_block ) );    \
00796 }
00797 
00798 CONVERT_INDIRECT( Float32toS8,   Float32, S16, U8 )
00799 CONVERT_INDIRECT( Float32toU8,   Float32, U16, U8 )
00800 CONVERT_INDIRECT( S8toFloat32,   S8,      S16, Float32 )
00801 CONVERT_INDIRECT( U8toFloat32,   U8,      U16, Float32 )
00802 
00803 #define S16toS16Invert Swap16
00804 #define U16toU16Invert Swap16
00805 
00806 CONVERT_INDIRECT( U8toS16Invert, U8,      S16, S16Invert )
00807 CONVERT_INDIRECT( S8toU16Invert, S8,      U16, U16Invert )
00808 
00809 CONVERT_INDIRECT( U8toU16Invert, U8,      U16, U16Invert )
00810 CONVERT_INDIRECT( S8toS16Invert, S8,      S16, S16Invert )
00811 
00812 #undef CONVERT_INDIRECT

Generated on Tue Dec 20 10:14:27 2005 for vlc-0.8.4a by  doxygen 1.4.2