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