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