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