]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/format.c
b9743a4bc304b34045df6062cb342f1a96dfade3
[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 =
324         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer * 4 / 3 );
325     if( !p_block_out )
326     {
327         msg_Warn( p_filter, "can't get output buffer" );
328         return NULL;
329     }
330
331     p_in = p_block->p_buffer;
332     p_out = (float *)p_block_out->p_buffer;
333
334     for( i = p_block->i_buffer / 3; i--; )
335     {
336         /* FIXME: unaligned reads */
337 #ifdef WORDS_BIGENDIAN
338         *p_out = ((float)( (((int32_t)*(int16_t *)(p_in)) << 8) + p_in[2]))
339 #else
340         *p_out = ((float)( (((int32_t)*(int16_t *)(p_in+1)) << 8) + p_in[0]))
341 #endif
342             / 8388608.0;
343
344         p_in += 3; p_out++;
345     }
346
347     p_block_out->i_nb_samples = p_block->i_nb_samples;
348     p_block_out->i_dts = p_block->i_dts;
349     p_block_out->i_pts = p_block->i_pts;
350     p_block_out->i_length = p_block->i_length;
351     p_block_out->i_rate = p_block->i_rate;
352
353     block_Release( p_block );
354     return p_block_out;
355 }
356
357 static block_t *S24toS16( filter_t *p_filter, block_t *p_block )
358 {
359     VLC_UNUSED(p_filter);
360     int i;
361     uint8_t *p_in = (uint8_t *)p_block->p_buffer;
362     uint8_t *p_out = (uint8_t *)p_in;
363
364     for( i = p_block->i_buffer / 3; i--; )
365     {
366 #ifdef WORDS_BIGENDIAN
367         *p_out++ = *p_in++;
368         *p_out++ = *p_in++;
369         p_in++;
370 #else
371         p_in++;
372         *p_out++ = *p_in++;
373         *p_out++ = *p_in++;
374 #endif
375     }
376
377     p_block->i_buffer = p_block->i_buffer * 2 / 3;
378     return p_block;
379 }
380
381 static block_t *S16toFloat32( filter_t *p_filter, block_t *p_block )
382 {
383     block_t *p_block_out;
384     int16_t *p_in;
385     float *p_out;
386     int i;
387
388     p_block_out =
389         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
390     if( !p_block_out )
391     {
392         msg_Warn( p_filter, "can't get output buffer" );
393         return NULL;
394     }
395
396     p_in = (int16_t *)p_block->p_buffer;
397     p_out = (float *)p_block_out->p_buffer;
398
399     for( i = p_block->i_buffer / 2; i--; )
400     {
401 #if 0
402         /* Slow version */
403         *p_out = (float)*p_in / 32768.0;
404 #else
405         /* This is walken's trick based on IEEE float format. On my PIII
406          * this takes 16 seconds to perform one billion conversions, instead
407          * of 19 seconds for the above division. */
408         union { float f; int32_t i; } u;
409         u.i = *p_in + 0x43c00000;
410         *p_out = u.f - 384.0;
411 #endif
412
413         p_in++; p_out++;
414     }
415
416     p_block_out->i_nb_samples = p_block->i_nb_samples;
417     p_block_out->i_dts = p_block->i_dts;
418     p_block_out->i_pts = p_block->i_pts;
419     p_block_out->i_length = p_block->i_length;
420     p_block_out->i_rate = p_block->i_rate;
421
422     block_Release( p_block );
423     return p_block_out;
424 }
425
426 static block_t *U16toFloat32( filter_t *p_filter, block_t *p_block )
427 {
428     block_t *p_block_out;
429     uint16_t *p_in;
430     float *p_out;
431     int i;
432
433     p_block_out =
434         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
435     if( !p_block_out )
436     {
437         msg_Warn( p_filter, "can't get output buffer" );
438         return NULL;
439     }
440
441     p_in = (uint16_t *)p_block->p_buffer;
442     p_out = (float *)p_block_out->p_buffer;
443
444     for( i = p_block->i_buffer / 2; i--; )
445     {
446         *p_out++ = (float)(*p_in++ - 32768) / 32768.0;
447     }
448
449     p_block_out->i_nb_samples = p_block->i_nb_samples;
450     p_block_out->i_dts = p_block->i_dts;
451     p_block_out->i_pts = p_block->i_pts;
452     p_block_out->i_length = p_block->i_length;
453     p_block_out->i_rate = p_block->i_rate;
454
455     block_Release( p_block );
456     return p_block_out;
457 }
458
459 static block_t *S16toS24( filter_t *p_filter, block_t *p_block )
460 {
461     block_t *p_block_out;
462     uint8_t *p_in, *p_out;
463     int i;
464
465     p_block_out =
466         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*3/2 );
467     if( !p_block_out )
468     {
469         msg_Warn( p_filter, "can't get output buffer" );
470         return NULL;
471     }
472
473     p_in = (uint8_t *)p_block->p_buffer;
474     p_out = (uint8_t *)p_block_out->p_buffer;
475
476     for( i = p_block->i_buffer / 2; i--; )
477     {
478 #ifdef WORDS_BIGENDIAN
479         *p_out++ = *p_in++;
480         *p_out++ = *p_in++;
481         *p_out++ = 0;
482 #else
483         *p_out++ = 0;
484         *p_out++ = *p_in++;
485         *p_out++ = *p_in++;
486 #endif
487     }
488
489     p_block_out->i_nb_samples = p_block->i_nb_samples;
490     p_block_out->i_dts = p_block->i_dts;
491     p_block_out->i_pts = p_block->i_pts;
492     p_block_out->i_length = p_block->i_length;
493     p_block_out->i_rate = p_block->i_rate;
494
495     block_Release( p_block );
496     return p_block_out;
497 }
498
499 static block_t *S16toS8( filter_t *p_filter, block_t *p_block )
500 {
501     VLC_UNUSED(p_filter);
502     int i;
503     int16_t *p_in = (int16_t *)p_block->p_buffer;
504     int8_t *p_out = (int8_t *)p_in;
505
506     for( i = p_block->i_buffer / 2; i--; )
507         *p_out++ = (*p_in++) >> 8;
508
509     p_block->i_buffer /= 2;
510     return p_block;
511 }
512 static block_t *S16toU8( filter_t *p_filter, block_t *p_block )
513 {
514     VLC_UNUSED(p_filter);
515     int i;
516     int16_t *p_in = (int16_t *)p_block->p_buffer;
517     uint8_t *p_out = (uint8_t *)p_in;
518
519     for( i = p_block->i_buffer / 2; i--; )
520         *p_out++ = ((*p_in++) + 32768) >> 8;
521
522     p_block->i_buffer /= 2;
523     return p_block;
524 }
525 static block_t *S16toU16( filter_t *p_filter, block_t *p_block )
526 {
527     VLC_UNUSED(p_filter);
528     int i;
529     int16_t *p_in = (int16_t *)p_block->p_buffer;
530     uint16_t *p_out = (uint16_t *)p_in;
531
532     for( i = p_block->i_buffer / 2; i--; )
533         *p_out++ = (*p_in++) + 32768;
534
535     return p_block;
536 }
537
538 static block_t *U16toS8( filter_t *p_filter, block_t *p_block )
539 {
540     VLC_UNUSED(p_filter);
541     int i;
542     uint16_t *p_in = (uint16_t *)p_block->p_buffer;
543     int8_t *p_out = (int8_t *)p_in;
544
545     for( i = p_block->i_buffer / 2; i--; )
546         *p_out++ = ((int)(*p_in++) - 32768) >> 8;
547
548     p_block->i_buffer /= 2;
549     return p_block;
550 }
551 static block_t *U16toU8( filter_t *p_filter, block_t *p_block )
552 {
553     VLC_UNUSED(p_filter);
554     int i;
555     uint16_t *p_in = (uint16_t *)p_block->p_buffer;
556     uint8_t *p_out = (uint8_t *)p_in;
557
558     for( i = p_block->i_buffer / 2; i--; )
559         *p_out++ = (*p_in++) >> 8;
560
561     p_block->i_buffer /= 2;
562     return p_block;
563 }
564 static block_t *U16toS16( filter_t *p_filter, block_t *p_block )
565 {
566     VLC_UNUSED(p_filter);
567     int i;
568     uint16_t *p_in = (uint16_t *)p_block->p_buffer;
569     int16_t *p_out = (int16_t *)p_in;
570
571     for( i = p_block->i_buffer / 2; i--; )
572         *p_out++ = (int)(*p_in++) - 32768;
573
574     return p_block;
575 }
576
577 static block_t *S8toU8( filter_t *p_filter, block_t *p_block )
578 {
579     VLC_UNUSED(p_filter);
580     int i;
581     int8_t *p_in = (int8_t *)p_block->p_buffer;
582     uint8_t *p_out = (uint8_t *)p_in;
583
584     for( i = p_block->i_buffer; i--; )
585         *p_out++ = ((*p_in++) + 128);
586
587     return p_block;
588 }
589 static block_t *U8toS8( filter_t *p_filter, block_t *p_block )
590 {
591     VLC_UNUSED(p_filter);
592     int i;
593     uint8_t *p_in = (uint8_t *)p_block->p_buffer;
594     int8_t *p_out = (int8_t *)p_in;
595
596     for( i = p_block->i_buffer; i--; )
597         *p_out++ = ((*p_in++) - 128);
598
599     return p_block;
600 }
601
602 /* */
603 static block_t *S8toU16( filter_t *p_filter, block_t *p_block )
604 {
605     block_t *p_block_out;
606     int8_t *p_in;
607     uint16_t *p_out;
608     int i;
609
610     p_block_out =
611         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
612     if( !p_block_out )
613     {
614         msg_Warn( p_filter, "can't get output buffer" );
615         return NULL;
616     }
617
618     p_in = (int8_t *)p_block->p_buffer;
619     p_out = (uint16_t *)p_block_out->p_buffer;
620
621     for( i = p_block->i_buffer; i--; )
622         *p_out++ = ((*p_in++) + 128) << 8;
623
624     p_block_out->i_nb_samples = p_block->i_nb_samples;
625     p_block_out->i_dts = p_block->i_dts;
626     p_block_out->i_pts = p_block->i_pts;
627     p_block_out->i_length = p_block->i_length;
628     p_block_out->i_rate = p_block->i_rate;
629
630     block_Release( p_block );
631     return p_block_out;
632 }
633
634 static block_t *U8toS16( filter_t *p_filter, block_t *p_block )
635 {
636     block_t *p_block_out;
637     uint8_t *p_in;
638     int16_t *p_out;
639     int i;
640
641     p_block_out =
642         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
643     if( !p_block_out )
644     {
645         msg_Warn( p_filter, "can't get output buffer" );
646         return NULL;
647     }
648
649     p_in = (uint8_t *)p_block->p_buffer;
650     p_out = (int16_t *)p_block_out->p_buffer;
651
652     for( i = p_block->i_buffer; i--; )
653         *p_out++ = ((*p_in++) - 128) << 8;
654
655     p_block_out->i_nb_samples = p_block->i_nb_samples;
656     p_block_out->i_dts = p_block->i_dts;
657     p_block_out->i_pts = p_block->i_pts;
658     p_block_out->i_length = p_block->i_length;
659     p_block_out->i_rate = p_block->i_rate;
660
661     block_Release( p_block );
662     return p_block_out;
663 }
664
665
666 static block_t *S8toS16( filter_t *p_filter, block_t *p_block )
667 {
668     block_t *p_block_out;
669     int8_t *p_in;
670     int16_t *p_out;
671     int i;
672
673     p_block_out =
674         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
675     if( !p_block_out )
676     {
677         msg_Warn( p_filter, "can't get output buffer" );
678         return NULL;
679     }
680
681     p_in = (int8_t *)p_block->p_buffer;
682     p_out = (int16_t *)p_block_out->p_buffer;
683
684     for( i = p_block->i_buffer; i--; )
685         *p_out++ = (*p_in++) << 8;
686
687     p_block_out->i_nb_samples = p_block->i_nb_samples;
688     p_block_out->i_dts = p_block->i_dts;
689     p_block_out->i_pts = p_block->i_pts;
690     p_block_out->i_length = p_block->i_length;
691     p_block_out->i_rate = p_block->i_rate;
692
693     block_Release( p_block );
694     return p_block_out;
695 }
696
697 static block_t *U8toU16( filter_t *p_filter, block_t *p_block )
698 {
699     block_t *p_block_out;
700     uint8_t *p_in;
701     uint16_t *p_out;
702     int i;
703
704     p_block_out =
705         p_filter->pf_audio_buffer_new( p_filter, p_block->i_buffer*2 );
706     if( !p_block_out )
707     {
708         msg_Warn( p_filter, "can't get output buffer" );
709         return NULL;
710     }
711
712     p_in = (uint8_t *)p_block->p_buffer;
713     p_out = (uint16_t *)p_block_out->p_buffer;
714
715     for( i = p_block->i_buffer; i--; )
716         *p_out++ = (*p_in++) << 8;
717
718     p_block_out->i_nb_samples = p_block->i_nb_samples;
719     p_block_out->i_dts = p_block->i_dts;
720     p_block_out->i_pts = p_block->i_pts;
721     p_block_out->i_length = p_block->i_length;
722     p_block_out->i_rate = p_block->i_rate;
723
724     block_Release( p_block );
725     return p_block_out;
726 }
727
728 /*****************************************************************************
729  * Swap a buffer of words
730  *****************************************************************************/
731 static block_t *Swap16( filter_t *p_filter, block_t *p_block )
732 {
733     VLC_UNUSED(p_filter);
734     size_t i;
735     uint8_t *p_in = (uint8_t *)p_block->p_buffer;
736     uint8_t tmp;
737
738     for( i = 0; i < p_block->i_buffer / 2; i++ )
739     {
740         tmp = p_in[0];
741         p_in[0] = p_in[1];
742         p_in[1] = tmp;
743         p_in += 2;
744     }
745
746     return p_block;
747 }
748
749 static block_t *Swap24( filter_t *p_filter, block_t *p_block )
750 {
751     VLC_UNUSED(p_filter);
752     size_t i;
753     uint8_t *p_in = (uint8_t *)p_block->p_buffer;
754     uint8_t tmp;
755
756     for( i = 0; i < p_block->i_buffer / 3; i++ )
757     {
758         tmp = p_in[0];
759         p_in[0] = p_in[2];
760         p_in[2] = tmp;
761         p_in += 3;
762     }
763
764     return p_block;
765 }
766
767 #define CONVERT_NN( func, f_in, f_out, b_pre_invert, b_post_invert, swapa, swapb ) \
768 static block_t *func( filter_t *p_filter, block_t *p_block ) \
769 {                                                   \
770     if( b_pre_invert )                              \
771         swapa( p_filter, p_block );                  \
772                                                     \
773     p_block = f_in##to##f_out( p_filter, p_block ); \
774                                                     \
775     if( b_post_invert )                             \
776         swapb( p_filter, p_block );                  \
777                                                     \
778     return p_block;                                 \
779 }
780
781 CONVERT_NN( Float32toS24Invert, Float32, S24, 0, 1, Swap24, Swap24 )
782 CONVERT_NN( Float32toS16Invert, Float32, S16, 0, 1, Swap16, Swap16 )
783 CONVERT_NN( Float32toU16Invert, Float32, U16, 0, 1, Swap16, Swap16 )
784
785 CONVERT_NN( S24InverttoFloat32, S24, Float32, 1, 0, Swap24, Swap24 )
786 CONVERT_NN( S24InverttoS16,     S24, S16,     1, 0, Swap24, Swap16 )
787 CONVERT_NN( S24InverttoS16Invert, S24, S16,   1, 1, Swap24, Swap16 )
788 CONVERT_NN( S24toS16Invert,     S24, S16,     0, 1, Swap24, Swap16 )
789
790 CONVERT_NN( S16InverttoFloat32, S16, Float32, 1, 0, Swap16, Swap16 )
791 CONVERT_NN( S16InverttoS24,     S16, S24,     1, 0, Swap16, Swap24 )
792 CONVERT_NN( S16toS24Invert,     S16, S24,     0, 1, Swap16, Swap24 )
793 CONVERT_NN( S16InverttoS24Invert, S16, S24,   1, 1, Swap16, Swap24 )
794 CONVERT_NN( S16InverttoS8,      S16, S8,      1, 0, Swap16, Swap16 )
795 CONVERT_NN( S16InverttoU8,      S16, U8,      1, 0, Swap16, Swap16 )
796 CONVERT_NN( S16InverttoU16,     S16, U16,     1, 0, Swap16, Swap16 )
797
798 CONVERT_NN( U16InverttoFloat32, U16, Float32, 1, 0, Swap16, Swap16 )
799 CONVERT_NN( U16InverttoS8,      U16, S8,      1, 0, Swap16, Swap16 )
800 CONVERT_NN( U16InverttoU8,      U16, U8,      1, 0, Swap16, Swap16 )
801 CONVERT_NN( U16InverttoS16,     U16, S16,     1, 0, Swap16, Swap16 )
802
803 #undef CONVERT_NN
804
805 #define CONVERT_INDIRECT( func, f_in, f_mid, f_out )                    \
806 static block_t *func( filter_t *p_filter, block_t *p_block )            \
807 {                                                                       \
808     return f_mid##to##f_out( p_filter,                                  \
809                              f_in##to##f_mid( p_filter, p_block ) );    \
810 }
811
812 CONVERT_INDIRECT( Float32toS8,   Float32, S16, U8 )
813 CONVERT_INDIRECT( Float32toU8,   Float32, U16, U8 )
814 CONVERT_INDIRECT( S8toFloat32,   S8,      S16, Float32 )
815 CONVERT_INDIRECT( U8toFloat32,   U8,      U16, Float32 )
816
817 #define S16toS16Invert Swap16
818 #define U16toU16Invert Swap16
819
820 CONVERT_INDIRECT( U8toS16Invert, U8,      S16, S16Invert )
821 CONVERT_INDIRECT( S8toU16Invert, S8,      U16, U16Invert )
822
823 CONVERT_INDIRECT( U8toU16Invert, U8,      U16, U16Invert )
824 CONVERT_INDIRECT( S8toS16Invert, S8,      S16, S16Invert )
825
826 #undef CONVERT_INDIRECT