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