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