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