* modules/codec/lpcm.c: support for 20/24 bits LPCM.
[vlc.git] / modules / audio_filter / format.c
1 /*****************************************************************************
2  * format.c : PCM format converter
3  *****************************************************************************
4  * Copyright (C) 2002-2005 VideoLAN
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/decoder.h>
33 #include "vlc_filter.h"
34
35 #ifdef WORDS_BIGENDIAN
36 #   define AOUT_FMT_S24_IE VLC_FOURCC('s','2','4','l')
37 #   define AOUT_FMT_S16_IE VLC_FOURCC('s','1','6','l')
38 #   define AOUT_FMT_U16_IE VLC_FOURCC('u','1','6','l')
39 #else
40 #   define AOUT_FMT_S24_IE VLC_FOURCC('s','2','4','b')
41 #   define AOUT_FMT_S16_IE VLC_FOURCC('s','1','6','b')
42 #   define AOUT_FMT_U16_IE VLC_FOURCC('u','1','6','b')
43 #endif
44
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 static int  Open ( vlc_object_t * );
50
51 static block_t *Float32toS24( filter_t *, block_t * );
52 static block_t *Float32toS16( filter_t *, block_t * );
53 static block_t *Float32toU16( filter_t *, block_t * );
54 static block_t *Float32toS8 ( filter_t *, block_t * );
55 static block_t *Float32toU8 ( filter_t *, block_t * );
56
57 static block_t *S24toFloat32  ( filter_t *, block_t * );
58 static block_t *S24toS16      ( filter_t *, block_t * );
59 static block_t *S24toS16Invert( filter_t *, block_t * );
60
61 static block_t *S16toFloat32  ( filter_t *, block_t * );
62 static block_t *S16toS24      ( filter_t *, block_t * );
63 static block_t *S16toS24Invert( filter_t *, block_t * );
64 static block_t *S16toS8       ( filter_t *, block_t * );
65 static block_t *S16toU8       ( filter_t *, block_t * );
66 static block_t *S16toU16      ( filter_t *, block_t * );
67
68 static block_t *U16toFloat32( filter_t *, block_t * );
69 static block_t *U16toS8     ( filter_t *, block_t * );
70 static block_t *U16toU8     ( filter_t *, block_t * );
71 static block_t *U16toS16    ( filter_t *, block_t * );
72
73 static block_t *Float32toS24Invert( filter_t *, block_t * );
74 static block_t *Float32toS16Invert( filter_t *, block_t * );
75 static block_t *Float32toU16Invert( filter_t *, block_t * );
76
77 static block_t *S24InverttoFloat32  ( filter_t *, block_t * );
78 static block_t *S24InverttoS16      ( filter_t *, block_t * );
79 static block_t *S24InverttoS16Invert( filter_t *, block_t * );
80
81 static block_t *S16InverttoFloat32  ( filter_t *, block_t * );
82 static block_t *S16InverttoS24      ( filter_t *, block_t * );
83 static block_t *S16InverttoS24Invert( filter_t *, block_t * );
84 static block_t *S16InverttoS8       ( filter_t *, block_t * );
85 static block_t *S16InverttoU8       ( filter_t *, block_t * );
86 static block_t *S16InverttoU16      ( filter_t *, block_t * );
87
88 static block_t *U16InverttoFloat32( filter_t *, block_t * );
89 static block_t *U16InverttoS8     ( filter_t *, block_t * );
90 static block_t *U16InverttoU8     ( filter_t *, block_t * );
91 static block_t *U16InverttoS16    ( filter_t *, block_t * );
92
93 static block_t *S8toFloat32  ( filter_t *, block_t * );
94 static block_t *S8toS16      ( filter_t *, block_t * );
95 static block_t *S8toU16      ( filter_t *, block_t * );
96 static block_t *S8toU8       ( filter_t *, block_t * );
97 static block_t *S8toS16Invert( filter_t *, block_t * );
98 static block_t *S8toU16Invert( filter_t *, block_t * );
99
100 static block_t *U8toFloat32  ( filter_t *, block_t * );
101 static block_t *U8toFloat32  ( filter_t *, block_t * );
102 static block_t *U8toS16      ( filter_t *, block_t * );
103 static block_t *U8toU16      ( filter_t *, block_t * );
104 static block_t *U8toS8       ( filter_t *, block_t * );
105 static block_t *U8toS16Invert( filter_t *, block_t * );
106 static block_t *U8toU16Invert( filter_t *, block_t * );
107
108
109 static block_t *U8toS8( filter_t *, block_t * );
110 static block_t *S8toU8( filter_t *, block_t * );
111
112
113 static block_t *Swap16( filter_t *, block_t * );
114 static block_t *Swap24( filter_t *, block_t * );
115
116 static struct
117 {
118     vlc_fourcc_t i_src;
119     vlc_fourcc_t i_dst;
120     block_t *(*pf_convert)( filter_t *, block_t *);
121 } ConvertTable[] =
122 {
123     /* From fl32 */
124     { VLC_FOURCC('f','l','3','2'), AOUT_FMT_S24_NE, Float32toS24 },
125     { VLC_FOURCC('f','l','3','2'), AOUT_FMT_S16_NE, Float32toS16 },
126     { VLC_FOURCC('f','l','3','2'), AOUT_FMT_U16_NE, Float32toU16 },
127     { VLC_FOURCC('f','l','3','2'), AOUT_FMT_S24_IE, Float32toS24Invert },
128     { VLC_FOURCC('f','l','3','2'), AOUT_FMT_S16_IE, Float32toS16Invert },
129     { VLC_FOURCC('f','l','3','2'), AOUT_FMT_U16_IE, Float32toU16Invert },
130     { VLC_FOURCC('f','l','3','2'), VLC_FOURCC('s','8',' ',' '), Float32toS8 },
131     { VLC_FOURCC('f','l','3','2'), VLC_FOURCC('u','8',' ',' '), Float32toU8 },
132
133     /* From s24 invert */
134     { AOUT_FMT_S24_NE, VLC_FOURCC('f','l','3','2'), S24toFloat32 },
135     { AOUT_FMT_S24_NE, AOUT_FMT_S24_IE,             Swap24 },
136     { AOUT_FMT_S24_NE, AOUT_FMT_S16_NE,             S24toS16 },
137     { AOUT_FMT_S24_NE, AOUT_FMT_S16_IE,             S24toS16Invert },
138
139     /* From s16 */
140     { AOUT_FMT_S16_NE, VLC_FOURCC('f','l','3','2'), S16toFloat32 },
141     { AOUT_FMT_S16_NE, AOUT_FMT_S24_NE,             S16toS24 },
142     { AOUT_FMT_S16_NE, AOUT_FMT_S24_IE,             S16toS24Invert },
143     { AOUT_FMT_S16_NE, AOUT_FMT_S16_IE,             Swap16 },
144     { AOUT_FMT_S16_NE, AOUT_FMT_U16_IE,             S16toU16 },
145     { AOUT_FMT_S16_NE, VLC_FOURCC('s','8',' ',' '), S16toS8 },
146     { AOUT_FMT_S16_NE, VLC_FOURCC('u','8',' ',' '), S16toU8 },
147
148     /* From u16 */
149     { AOUT_FMT_U16_NE, VLC_FOURCC('f','l','3','2'), U16toFloat32 },
150     { AOUT_FMT_U16_NE, AOUT_FMT_U16_IE,             Swap16 },
151     { AOUT_FMT_U16_NE, AOUT_FMT_S16_IE,             U16toS16 },
152     { AOUT_FMT_U16_NE, VLC_FOURCC('s','8',' ',' '), U16toS8 },
153     { AOUT_FMT_U16_NE, VLC_FOURCC('u','8',' ',' '), U16toU8 },
154
155     /* From s8 */
156     { VLC_FOURCC('s','8',' ',' '), VLC_FOURCC('f','l','3','2'), S8toFloat32 },
157     { VLC_FOURCC('s','8',' ',' '), AOUT_FMT_S16_NE,             S8toS16 },
158     { VLC_FOURCC('s','8',' ',' '), AOUT_FMT_S16_IE,             S8toS16Invert },
159     { VLC_FOURCC('s','8',' ',' '), AOUT_FMT_U16_NE,             S8toU16 },
160     { VLC_FOURCC('s','8',' ',' '), AOUT_FMT_U16_IE,             S8toU16Invert },
161     { VLC_FOURCC('s','8',' ',' '), VLC_FOURCC('u','8',' ',' '), S8toU8 },
162     
163     /* From u8 */
164     { VLC_FOURCC('u','8',' ',' '), VLC_FOURCC('f','l','3','2'), U8toFloat32 },
165     { VLC_FOURCC('u','8',' ',' '), AOUT_FMT_S16_NE,             U8toS16 },
166     { VLC_FOURCC('u','8',' ',' '), AOUT_FMT_S16_IE,             U8toS16Invert },
167     { VLC_FOURCC('u','8',' ',' '), AOUT_FMT_U16_NE,             U8toU16 },
168     { VLC_FOURCC('u','8',' ',' '), AOUT_FMT_U16_IE,             U8toU16Invert },
169     { VLC_FOURCC('u','8',' ',' '), VLC_FOURCC('s','8',' ',' '), U8toS8 },
170
171     /* From s24 invert */
172     { AOUT_FMT_S24_IE, VLC_FOURCC('f','l','3','2'), S24InverttoFloat32 },
173     { AOUT_FMT_S24_IE, AOUT_FMT_S24_NE,             Swap24 },
174     { AOUT_FMT_S24_IE, AOUT_FMT_S16_NE,             S24InverttoS16 },
175     { AOUT_FMT_S24_IE, AOUT_FMT_S16_IE,             S24InverttoS16Invert },
176
177     /* From s16 invert */
178     { AOUT_FMT_S16_IE, VLC_FOURCC('f','l','3','2'), S16InverttoFloat32 },
179     { AOUT_FMT_S16_IE, AOUT_FMT_S24_NE,             S16InverttoS24 },
180     { AOUT_FMT_S16_IE, AOUT_FMT_S24_IE,             S16InverttoS24Invert },
181     { AOUT_FMT_S16_IE, AOUT_FMT_S16_NE,             Swap16 },
182     { AOUT_FMT_S16_IE, AOUT_FMT_U16_NE,             S16InverttoU16 },
183     { AOUT_FMT_S16_IE, VLC_FOURCC('s','8',' ',' '), S16InverttoS8 },
184     { AOUT_FMT_S16_IE, VLC_FOURCC('u','8',' ',' '), S16InverttoU8 },
185
186     /* From u16 invert */
187     { AOUT_FMT_U16_IE, VLC_FOURCC('f','l','3','2'), U16InverttoFloat32 },
188     { AOUT_FMT_U16_IE, AOUT_FMT_U16_NE,             Swap16 },
189     { AOUT_FMT_U16_IE, AOUT_FMT_S16_NE,             U16InverttoS16 },
190     { AOUT_FMT_U16_IE, VLC_FOURCC('s','8',' ',' '), U16InverttoS8 },
191     { AOUT_FMT_U16_IE, VLC_FOURCC('u','8',' ',' '), U16InverttoU8 },
192
193     { 0, 0, NULL },
194 };
195
196
197 /*****************************************************************************
198  * Module descriptor
199  *****************************************************************************/
200 vlc_module_begin();
201     set_description( _("audio filter for PCM format conversion") );
202     set_capability( "audio filter2", 1 );
203     set_callbacks( Open, NULL );
204 vlc_module_end();
205
206 /*****************************************************************************
207  * Open:
208  *****************************************************************************/
209 static int Open( vlc_object_t *p_this )
210 {
211     filter_t *p_filter = (filter_t *)p_this;
212     int i;
213
214     for( i = 0; ConvertTable[i].pf_convert != NULL; i++ )
215     {
216         if( ConvertTable[i].i_src == p_filter->fmt_in.i_codec &&
217             ConvertTable[i].i_dst == p_filter->fmt_out.i_codec )
218             break;
219     }
220     if( ConvertTable[i].pf_convert == NULL )
221         return VLC_EGENERIC;
222
223     
224     p_filter->pf_audio_filter = ConvertTable[i].pf_convert;
225
226     msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
227              (char *)&p_filter->fmt_in.i_codec,
228              (char *)&p_filter->fmt_out.i_codec,
229              p_filter->fmt_in.audio.i_bitspersample );
230
231     return VLC_SUCCESS;
232 }
233
234 /*****************************************************************************
235  * Convert a buffer
236  *****************************************************************************/
237 static block_t *Float32toS24( filter_t *p_filter, block_t *p_block )
238 {
239     int i;
240     float *p_in = (float *)p_block->p_buffer;
241     uint8_t *p_out = (uint8_t *)p_in;
242     int32_t out;
243
244     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
245     {
246         if ( *p_in >= 1.0 ) out = 8388607;
247         else if ( *p_in < -1.0 ) out = -8388608;
248         else out = *p_in * 8388608.0;
249
250 #ifdef WORDS_BIGENDIAN
251         *((int16_t *)p_out) = out >> 8;
252         p_out[2] = out & 0xFF;
253 #else
254         *((int16_t *)(p_out+1)) = out >> 8;
255         p_out[0] = out & 0xFF;
256 #endif
257
258         p_in++; p_out += 3;
259     }
260
261     p_block->i_buffer = p_block->i_buffer * 3 / 4;
262     return p_block;
263 }
264
265 static block_t *Float32toS16( filter_t *p_filter, block_t *p_block )
266 {
267     int i;
268     float *p_in = (float *)p_block->p_buffer;
269     int16_t *p_out = (int16_t *)p_in;
270
271     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
272     {
273 #if 0
274         /* Slow version. */
275         if ( *p_in >= 1.0 ) *p_out = 32767;
276         else if ( *p_in < -1.0 ) *p_out = -32768;
277         else *p_out = *p_in * 32768.0;
278 #else
279         /* This is walken's trick based on IEEE float format. */
280         union { float f; int32_t i; } u;
281         u.f = *p_in + 384.0;
282         if ( u.i > 0x43c07fff ) *p_out = 32767;
283         else if ( u.i < 0x43bf8000 ) *p_out = -32768;
284         else *p_out = u.i - 0x43c00000;
285 #endif
286         p_in++; p_out++;
287     }
288
289     p_block->i_buffer /= 2;
290     return p_block;
291 }
292
293 static block_t *Float32toU16( filter_t *p_filter, block_t *p_block )
294 {
295     int i;
296     float *p_in = (float *)p_block->p_buffer;
297     uint16_t *p_out = (uint16_t *)p_in;
298
299     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
300     {
301         if ( *p_in >= 1.0 ) *p_out = 65535;
302         else if ( *p_in < -1.0 ) *p_out = 0;
303         else *p_out = (uint16_t)(32768 + *p_in * 32768);
304         p_in++; p_out++;
305     }
306
307     p_block->i_buffer /= 2;
308     return p_block;
309 }
310
311 static block_t *S24toFloat32( filter_t *p_filter, block_t *p_block )
312 {
313     block_t *p_block_out;
314     uint8_t *p_in;
315     float *p_out;
316     int i;
317
318     p_block_out =
319         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*4/3 );
320     if( !p_block_out )
321     {
322         msg_Warn( p_filter, "can't get output buffer" );
323         return NULL;
324     }
325
326     p_in = p_block->p_buffer;
327     p_out = (float *)p_block_out->p_buffer;
328
329     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
330     {
331 #ifdef WORDS_BIGENDIAN
332         *p_out = ((float)( (((int32_t)*(int16_t *)(p_in)) << 8) + p_in[2]))
333 #else
334         *p_out = ((float)( (((int32_t)*(int16_t *)(p_in+1)) << 8) + p_in[0]))
335 #endif
336             / 8388608.0;
337
338         p_in += 3; p_out++;
339     }
340
341     p_block_out->i_samples = p_block->i_samples;
342     p_block_out->i_dts = p_block->i_dts;
343     p_block_out->i_pts = p_block->i_pts;
344     p_block_out->i_length = p_block->i_length;
345     p_block_out->i_rate = p_block->i_rate;
346
347     p_block->pf_release( p_block );
348     return p_block_out;
349 }
350
351 static block_t *S24toS16( filter_t *p_filter, block_t *p_block )
352 {
353     int i;
354     uint8_t *p_in = (uint8_t *)p_block->p_buffer;
355     uint8_t *p_out = (uint8_t *)p_in;
356
357     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
358     {
359 #ifdef WORDS_BIGENDIAN
360         *p_out++ = *p_in++;
361         *p_out++ = *p_in++;
362         p_in++;
363 #else
364         p_in++;
365         *p_out++ = *p_in++;
366         *p_out++ = *p_in++;
367 #endif
368     }
369
370     p_block->i_buffer = p_block->i_buffer * 2 / 3;
371     return p_block;
372 }
373
374 static block_t *S16toFloat32( filter_t *p_filter, block_t *p_block )
375 {
376     block_t *p_block_out;
377     int16_t *p_in;
378     float *p_out;
379     int i;
380
381     p_block_out =
382         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
383     if( !p_block_out )
384     {
385         msg_Warn( p_filter, "can't get output buffer" );
386         return NULL;
387     }
388
389     p_in = (int16_t *)p_block->p_buffer;
390     p_out = (float *)p_block_out->p_buffer;
391
392     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
393     {
394 #if 0
395         /* Slow version */
396         *p_out = (float)*p_in / 32768.0;
397 #else
398         /* This is walken's trick based on IEEE float format. On my PIII
399          * this takes 16 seconds to perform one billion conversions, instead
400          * of 19 seconds for the above division. */
401         union { float f; int32_t i; } u;
402         u.i = *p_in + 0x43c00000;
403         *p_out = u.f - 384.0;
404 #endif
405
406         p_in++; p_out++;
407     }
408
409     p_block_out->i_samples = p_block->i_samples;
410     p_block_out->i_dts = p_block->i_dts;
411     p_block_out->i_pts = p_block->i_pts;
412     p_block_out->i_length = p_block->i_length;
413     p_block_out->i_rate = p_block->i_rate;
414
415     p_block->pf_release( p_block );
416     return p_block_out;
417 }
418
419 static block_t *U16toFloat32( filter_t *p_filter, block_t *p_block )
420 {
421     block_t *p_block_out;
422     int16_t *p_in;
423     float *p_out;
424     int i;
425
426     p_block_out =
427         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
428     if( !p_block_out )
429     {
430         msg_Warn( p_filter, "can't get output buffer" );
431         return NULL;
432     }
433
434     p_in = (int16_t *)p_block->p_buffer;
435     p_out = (float *)p_block_out->p_buffer;
436
437     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
438     {
439         *p_out++ = (float)(*p_in++ - 32768) / 32768.0;
440     }
441
442     p_block_out->i_samples = p_block->i_samples;
443     p_block_out->i_dts = p_block->i_dts;
444     p_block_out->i_pts = p_block->i_pts;
445     p_block_out->i_length = p_block->i_length;
446     p_block_out->i_rate = p_block->i_rate;
447
448     p_block->pf_release( p_block );
449     return p_block_out;
450 }
451
452 static block_t *S16toS24( filter_t *p_filter, block_t *p_block )
453 {
454     block_t *p_block_out;
455     uint8_t *p_in, *p_out;
456     int i;
457
458     p_block_out =
459         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*3/2 );
460     if( !p_block_out )
461     {
462         msg_Warn( p_filter, "can't get output buffer" );
463         return NULL;
464     }
465
466     p_in = (uint8_t *)p_block->p_buffer;
467     p_out = (uint8_t *)p_block_out->p_buffer;
468
469     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
470     {
471 #ifdef WORDS_BIGENDIAN
472         *p_out++ = *p_in++;
473         *p_out++ = *p_in++;
474         *p_out++ = 0;
475 #else
476         *p_out++ = 0;
477         *p_out++ = *p_in++;
478         *p_out++ = *p_in++;
479 #endif
480     }
481
482     p_block_out->i_samples = p_block->i_samples;
483     p_block_out->i_dts = p_block->i_dts;
484     p_block_out->i_pts = p_block->i_pts;
485     p_block_out->i_length = p_block->i_length;
486     p_block_out->i_rate = p_block->i_rate;
487
488     p_block->pf_release( p_block );
489     return p_block_out;
490 }
491
492 static block_t *S16toS8( filter_t *p_filter, block_t *p_block )
493 {
494     int i;
495     int16_t *p_in = (int16_t *)p_block->p_buffer;
496     int8_t *p_out = (int8_t *)p_in;
497
498     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
499         *p_out++ = (*p_in++) >> 8;
500
501     p_block->i_buffer /= 2;
502     return p_block;
503 }
504 static block_t *S16toU8( filter_t *p_filter, block_t *p_block )
505 {
506     int i;
507     int16_t *p_in = (int16_t *)p_block->p_buffer;
508     uint8_t *p_out = (uint8_t *)p_in;
509
510     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
511         *p_out++ = ((*p_in++) + 32768) >> 8;
512
513     p_block->i_buffer /= 2;
514     return p_block;
515 }
516 static block_t *S16toU16( filter_t *p_filter, block_t *p_block )
517 {
518     int i;
519     int16_t *p_in = (int16_t *)p_block->p_buffer;
520     uint16_t *p_out = (uint16_t *)p_in;
521
522     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
523         *p_out++ = (*p_in++) + 32768;
524
525     return p_block;
526 }
527
528 static block_t *U16toS8( filter_t *p_filter, block_t *p_block )
529 {
530     int i;
531     uint16_t *p_in = (uint16_t *)p_block->p_buffer;
532     int8_t *p_out = (int8_t *)p_in;
533
534     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
535         *p_out++ = ((int)(*p_in++) - 32768) >> 8;
536
537     p_block->i_buffer /= 2;
538     return p_block;
539 }
540 static block_t *U16toU8( filter_t *p_filter, block_t *p_block )
541 {
542     int i;
543     uint16_t *p_in = (uint16_t *)p_block->p_buffer;
544     uint8_t *p_out = (uint8_t *)p_in;
545
546     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
547         *p_out++ = (*p_in++) >> 8;
548
549     p_block->i_buffer /= 2;
550     return p_block;
551 }
552 static block_t *U16toS16( filter_t *p_filter, block_t *p_block )
553 {
554     int i;
555     int16_t *p_in = (int16_t *)p_block->p_buffer;
556     uint16_t *p_out = (uint16_t *)p_in;
557
558     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
559         *p_out++ = (int)(*p_in++) - 32768;
560
561     return p_block;
562 }
563
564 static block_t *S8toU8( filter_t *p_filter, block_t *p_block )
565 {
566     int i;
567     int8_t *p_in = (int8_t *)p_block->p_buffer;
568     uint8_t *p_out = (uint8_t *)p_in;
569
570     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
571         *p_out++ = ((*p_in++) + 128);
572
573     return p_block;
574 }
575 static block_t *U8toS8( filter_t *p_filter, block_t *p_block )
576 {
577     int i;
578     uint8_t *p_in = (uint8_t *)p_block->p_buffer;
579     int8_t *p_out = (int8_t *)p_in;
580
581     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
582         *p_out++ = ((*p_in++) - 128);
583
584     return p_block;
585 }
586
587 /* */
588 static block_t *S8toU16( filter_t *p_filter, block_t *p_block )
589 {
590     block_t *p_block_out;
591     int8_t *p_in;
592     uint16_t *p_out;
593     int i;
594
595     p_block_out =
596         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
597     if( !p_block_out )
598     {
599         msg_Warn( p_filter, "can't get output buffer" );
600         return NULL;
601     }
602
603     p_in = (int8_t *)p_block->p_buffer;
604     p_out = (uint16_t *)p_block_out->p_buffer;
605
606     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
607         *p_out++ = ((*p_in++) + 128) << 8;
608
609     p_block_out->i_samples = p_block->i_samples;
610     p_block_out->i_dts = p_block->i_dts;
611     p_block_out->i_pts = p_block->i_pts;
612     p_block_out->i_length = p_block->i_length;
613     p_block_out->i_rate = p_block->i_rate;
614
615     p_block->pf_release( p_block );
616     return p_block_out;
617 }
618
619 static block_t *U8toS16( filter_t *p_filter, block_t *p_block )
620 {
621     block_t *p_block_out;
622     uint8_t *p_in;
623     int16_t *p_out;
624     int i;
625
626     p_block_out =
627         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
628     if( !p_block_out )
629     {
630         msg_Warn( p_filter, "can't get output buffer" );
631         return NULL;
632     }
633
634     p_in = (uint8_t *)p_block->p_buffer;
635     p_out = (int16_t *)p_block_out->p_buffer;
636
637     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
638         *p_out++ = ((*p_in++) - 128) << 8;
639
640     p_block_out->i_samples = p_block->i_samples;
641     p_block_out->i_dts = p_block->i_dts;
642     p_block_out->i_pts = p_block->i_pts;
643     p_block_out->i_length = p_block->i_length;
644     p_block_out->i_rate = p_block->i_rate;
645
646     p_block->pf_release( p_block );
647     return p_block_out;
648 }
649
650
651 static block_t *S8toS16( filter_t *p_filter, block_t *p_block )
652 {
653     block_t *p_block_out;
654     int8_t *p_in;
655     int16_t *p_out;
656     int i;
657
658     p_block_out =
659         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
660     if( !p_block_out )
661     {
662         msg_Warn( p_filter, "can't get output buffer" );
663         return NULL;
664     }
665
666     p_in = (int8_t *)p_block->p_buffer;
667     p_out = (int16_t *)p_block_out->p_buffer;
668
669     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
670         *p_out++ = (*p_in++) << 8;
671
672     p_block_out->i_samples = p_block->i_samples;
673     p_block_out->i_dts = p_block->i_dts;
674     p_block_out->i_pts = p_block->i_pts;
675     p_block_out->i_length = p_block->i_length;
676     p_block_out->i_rate = p_block->i_rate;
677
678     p_block->pf_release( p_block );
679     return p_block_out;
680 }
681
682 static block_t *U8toU16( filter_t *p_filter, block_t *p_block )
683 {
684     block_t *p_block_out;
685     uint8_t *p_in;
686     uint16_t *p_out;
687     int i;
688
689     p_block_out =
690         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
691     if( !p_block_out )
692     {
693         msg_Warn( p_filter, "can't get output buffer" );
694         return NULL;
695     }
696
697     p_in = (uint8_t *)p_block->p_buffer;
698     p_out = (uint16_t *)p_block_out->p_buffer;
699
700     for( i = p_block->i_buffer*8/p_filter->fmt_in.audio.i_bitspersample; i--; )
701         *p_out++ = (*p_in++) << 8;
702
703     p_block_out->i_samples = p_block->i_samples;
704     p_block_out->i_dts = p_block->i_dts;
705     p_block_out->i_pts = p_block->i_pts;
706     p_block_out->i_length = p_block->i_length;
707     p_block_out->i_rate = p_block->i_rate;
708
709     p_block->pf_release( p_block );
710     return p_block_out;
711 }
712
713 /*****************************************************************************
714  * Swap a buffer of words
715  *****************************************************************************/
716 static block_t *Swap16( filter_t *p_filter, block_t *p_block )
717 {
718     int i;
719     uint8_t *p_in = (uint8_t *)p_block->p_buffer;
720     uint8_t tmp;
721
722     for( i = 0; i < p_block->i_buffer / 2; i++ )
723     {
724         tmp = p_in[0];
725         p_in[0] = p_in[1];
726         p_in[1] = tmp;
727         p_in += 2;
728     }
729
730     return p_block;
731 }
732
733 static block_t *Swap24( filter_t *p_filter, block_t *p_block )
734 {
735     int i;
736     uint8_t *p_in = (uint8_t *)p_block->p_buffer;
737     uint8_t tmp;
738
739     for( i = 0; i < p_block->i_buffer / 3; i++ )
740     {
741         tmp = p_in[0];
742         p_in[0] = p_in[2];
743         p_in[2] = tmp;
744         p_in += 3;
745     }
746
747     return p_block;
748 }
749
750 #define CONVERT_NN( func, f_in, f_out, b_pre_invert, b_post_invert, swapa, swapb ) \
751 static block_t *func( filter_t *p_filter, block_t *p_block ) \
752 {                                                   \
753     if( b_pre_invert )                              \
754         swapa( p_filter, p_block );                  \
755                                                     \
756     p_block = f_in##to##f_out( p_filter, p_block ); \
757                                                     \
758     if( b_post_invert )                             \
759         swapb( p_filter, p_block );                  \
760                                                     \
761     return p_block;                                 \
762 }
763
764 CONVERT_NN( Float32toS24Invert, Float32, S24, 0, 1, Swap24, Swap24 )
765 CONVERT_NN( Float32toS16Invert, Float32, S16, 0, 1, Swap16, Swap16 )
766 CONVERT_NN( Float32toU16Invert, Float32, U16, 0, 1, Swap16, Swap16 )
767
768 CONVERT_NN( S24InverttoFloat32, S24, Float32, 1, 0, Swap24, Swap24 )
769 CONVERT_NN( S24InverttoS16,     S24, S16,     1, 0, Swap24, Swap16 )
770 CONVERT_NN( S24InverttoS16Invert, S24, S16,   1, 1, Swap24, Swap16 )
771 CONVERT_NN( S24toS16Invert,     S24, S16,     0, 1, Swap24, Swap16 )
772
773 CONVERT_NN( S16InverttoFloat32, S16, Float32, 1, 0, Swap16, Swap16 )
774 CONVERT_NN( S16InverttoS24,     S16, S24,     1, 0, Swap16, Swap24 )
775 CONVERT_NN( S16toS24Invert,     S16, S24,     0, 1, Swap16, Swap24 )
776 CONVERT_NN( S16InverttoS24Invert, S16, S24,   1, 1, Swap16, Swap24 )
777 CONVERT_NN( S16InverttoS8,      S16, S8,      1, 0, Swap16, Swap16 )
778 CONVERT_NN( S16InverttoU8,      S16, U8,      1, 0, Swap16, Swap16 )
779 CONVERT_NN( S16InverttoU16,     S16, U16,     1, 0, Swap16, Swap16 )
780
781 CONVERT_NN( U16InverttoFloat32, U16, Float32, 1, 0, Swap16, Swap16 )
782 CONVERT_NN( U16InverttoS8,      U16, S8,      1, 0, Swap16, Swap16 )
783 CONVERT_NN( U16InverttoU8,      U16, U8,      1, 0, Swap16, Swap16 )
784 CONVERT_NN( U16InverttoS16,     U16, S16,     1, 0, Swap16, Swap16 )
785
786 #undef CONVERT_NN
787
788 #define CONVERT_INDIRECT( func, f_in, f_mid, f_out )                    \
789 static block_t *func( filter_t *p_filter, block_t *p_block )            \
790 {                                                                       \
791     return f_mid##to##f_out( p_filter,                                  \
792                              f_in##to##f_mid( p_filter, p_block ) );    \
793 }
794
795 CONVERT_INDIRECT( Float32toS8,   Float32, S16, U8 )
796 CONVERT_INDIRECT( Float32toU8,   Float32, U16, U8 )
797 CONVERT_INDIRECT( S8toFloat32,   S8,      S16, Float32 )
798 CONVERT_INDIRECT( U8toFloat32,   U8,      U16, Float32 )
799
800 #define S16toS16Invert Swap16
801 #define U16toU16Invert Swap16
802
803 CONVERT_INDIRECT( U8toS16Invert, U8,      S16, S16Invert )
804 CONVERT_INDIRECT( S8toU16Invert, S8,      U16, U16Invert )
805
806 CONVERT_INDIRECT( U8toU16Invert, U8,      U16, U16Invert )
807 CONVERT_INDIRECT( S8toS16Invert, S8,      S16, S16Invert )
808
809 #undef CONVERT_INDIRECT