1 /*****************************************************************************
2 * format.c : PCM format converter
3 *****************************************************************************
4 * Copyright (C) 2002-2005 VLC authors and VideoLAN
5 * Copyright (C) 2010 Laurent Aimar
8 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * Gildas Bazin <gbazin@videolan.org>
10 * Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
40 #include <vlc_block.h>
41 #include <vlc_filter.h>
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open(vlc_object_t *);
49 set_description(N_("Audio filter for PCM format conversion"))
50 set_category(CAT_AUDIO)
51 set_subcategory(SUBCAT_AUDIO_MISC)
52 set_capability("audio converter", 1)
53 set_callbacks(Open, NULL)
56 /*****************************************************************************
58 *****************************************************************************/
60 typedef block_t *(*cvt_t)(filter_t *, block_t *);
61 static cvt_t FindConversion(vlc_fourcc_t src, vlc_fourcc_t dst);
63 static int Open(vlc_object_t *object)
65 filter_t *filter = (filter_t *)object;
67 const es_format_t *src = &filter->fmt_in;
68 es_format_t *dst = &filter->fmt_out;
70 if (!AOUT_FMTS_SIMILAR(&src->audio, &dst->audio))
72 if (src->i_codec == dst->i_codec)
75 filter->pf_audio_filter = FindConversion(src->i_codec, dst->i_codec);
76 if (filter->pf_audio_filter == NULL)
79 msg_Dbg(filter, "%4.4s->%4.4s, bits per sample: %i->%i",
80 (char *)&src->i_codec, (char *)&dst->i_codec,
81 src->audio.i_bitspersample, dst->audio.i_bitspersample);
87 static block_t *U8toS16(filter_t *filter, block_t *bsrc)
89 block_t *bdst = block_Alloc(bsrc->i_buffer * 2);
90 if (unlikely(bdst == NULL))
93 block_CopyProperties(bdst, bsrc);
94 uint8_t *src = (uint8_t *)bsrc->p_buffer;
95 int16_t *dst = (int16_t *)bdst->p_buffer;
96 for (size_t i = bsrc->i_buffer; i--;)
97 *dst++ = ((*src++) - 128) << 8;
104 static block_t *U8toFl32(filter_t *filter, block_t *bsrc)
106 block_t *bdst = block_Alloc(bsrc->i_buffer * 4);
107 if (unlikely(bdst == NULL))
110 block_CopyProperties(bdst, bsrc);
111 uint8_t *src = (uint8_t *)bsrc->p_buffer;
112 float *dst = (float *)bdst->p_buffer;
113 for (size_t i = bsrc->i_buffer; i--;)
114 *dst++ = ((float)((*src++) - 128)) / 128.f;
121 static block_t *U8toS32(filter_t *filter, block_t *bsrc)
123 block_t *bdst = block_Alloc(bsrc->i_buffer * 4);
124 if (unlikely(bdst == NULL))
127 block_CopyProperties(bdst, bsrc);
128 uint8_t *src = (uint8_t *)bsrc->p_buffer;
129 int32_t *dst = (int32_t *)bdst->p_buffer;
130 for (size_t i = bsrc->i_buffer; i--;)
131 *dst++ = ((*src++) - 128) << 24;
138 static block_t *U8toFl64(filter_t *filter, block_t *bsrc)
140 block_t *bdst = block_Alloc(bsrc->i_buffer * 8);
141 if (unlikely(bdst == NULL))
144 block_CopyProperties(bdst, bsrc);
145 uint8_t *src = (uint8_t *)bsrc->p_buffer;
146 double *dst = (double *)bdst->p_buffer;
147 for (size_t i = bsrc->i_buffer; i--;)
148 *dst++ = ((double)((*src++) - 128)) / 128.;
157 static block_t *S16toU8(filter_t *filter, block_t *b)
160 int16_t *src = (int16_t *)b->p_buffer;
161 uint8_t *dst = (uint8_t *)src;
162 for (size_t i = b->i_buffer / 2; i--;)
163 *dst++ = ((*src++) + 32768) >> 8;
169 static block_t *S16toFl32(filter_t *filter, block_t *bsrc)
171 block_t *bdst = block_Alloc(bsrc->i_buffer * 2);
172 if (unlikely(bdst == NULL))
175 block_CopyProperties(bdst, bsrc);
176 int16_t *src = (int16_t *)bsrc->p_buffer;
177 float *dst = (float *)bdst->p_buffer;
178 for (size_t i = bsrc->i_buffer / 2; i--;)
181 *dst++ = (float)*src++ / 32768.f;
183 { /* This is Walken's trick based on IEEE float format. On my PIII
184 * this takes 16 seconds to perform one billion conversions, instead
185 * of 19 seconds for the above division. */
186 union { float f; int32_t i; } u;
187 u.i = *src++ + 0x43c00000;
188 *dst++ = u.f - 384.f;
197 static block_t *S16toS32(filter_t *filter, block_t *bsrc)
199 block_t *bdst = block_Alloc(bsrc->i_buffer * 2);
200 if (unlikely(bdst == NULL))
203 block_CopyProperties(bdst, bsrc);
204 int16_t *src = (int16_t *)bsrc->p_buffer;
205 int32_t *dst = (int32_t *)bdst->p_buffer;
206 for (int i = bsrc->i_buffer / 2; i--;)
207 *dst++ = *src++ << 16;
214 static block_t *S16toFl64(filter_t *filter, block_t *bsrc)
216 block_t *bdst = block_Alloc(bsrc->i_buffer * 4);
217 if (unlikely(bdst == NULL))
220 block_CopyProperties(bdst, bsrc);
221 int16_t *src = (int16_t *)bsrc->p_buffer;
222 float *dst = (float *)bdst->p_buffer;
223 for (size_t i = bsrc->i_buffer / 2; i--;)
224 *dst++ = (double)*src++ / 32768.;
233 static block_t *Fl32toU8(filter_t *filter, block_t *b)
235 float *src = (float *)b->p_buffer;
236 uint8_t *dst = (uint8_t *)src;
237 for (size_t i = b->i_buffer / 4; i--;)
239 float s = *(src++) * 128.f;
246 *(dst++) = lroundf(s) + 128;
253 static block_t *Fl32toS16(filter_t *filter, block_t *b)
256 float *src = (float *)b->p_buffer;
257 int16_t *dst = (int16_t *)src;
258 for (int i = b->i_buffer / 4; i--;) {
261 if (*src >= 1.0) *dst = 32767;
262 else if (*src < -1.0) *dst = -32768;
263 else *dst = lroundf(*src * 32768.f);
266 /* This is Walken's trick based on IEEE float format. */
267 union { float f; int32_t i; } u;
268 u.f = *src++ + 384.f;
269 if (u.i > 0x43c07fff)
271 else if (u.i < 0x43bf8000)
274 *dst++ = u.i - 0x43c00000;
281 static block_t *Fl32toS32(filter_t *filter, block_t *b)
283 float *src = (float *)b->p_buffer;
284 int32_t *dst = (int32_t *)src;
285 for (size_t i = b->i_buffer / 4; i--;)
287 float s = *(src++) * 2147483648.f;
288 if (s >= 2147483647.f)
289 *(dst++) = 2147483647;
291 if (s <= -2147483648.f)
292 *(dst++) = -2147483648;
294 *(dst++) = lroundf(s);
300 static block_t *Fl32toFl64(filter_t *filter, block_t *bsrc)
302 block_t *bdst = block_Alloc(bsrc->i_buffer * 2);
303 if (unlikely(bdst == NULL))
306 block_CopyProperties(bdst, bsrc);
307 float *src = (float *)bsrc->p_buffer;
308 double *dst = (double *)bdst->p_buffer;
309 for (size_t i = bsrc->i_buffer / 4; i--;)
319 static block_t *S32toU8(filter_t *filter, block_t *b)
322 int32_t *src = (int32_t *)b->p_buffer;
323 uint8_t *dst = (uint8_t *)src;
324 for (size_t i = b->i_buffer / 4; i--;)
325 *dst++ = ((*src++) >> 24) + 128;
331 static block_t *S32toS16(filter_t *filter, block_t *b)
334 int32_t *src = (int32_t *)b->p_buffer;
335 int16_t *dst = (int16_t *)src;
336 for (size_t i = b->i_buffer / 4; i--;)
337 *dst++ = (*src++) >> 16;
343 static block_t *S32toFl32(filter_t *filter, block_t *b)
346 int32_t *src = (int32_t*)b->p_buffer;
347 float *dst = (float *)src;
348 for (int i = b->i_buffer / 4; i--;)
349 *dst++ = (float)(*src++) / 2147483648.f;
353 static block_t *S32toFl64(filter_t *filter, block_t *bsrc)
355 block_t *bdst = block_Alloc(bsrc->i_buffer * 2);
356 if (unlikely(bdst == NULL))
359 block_CopyProperties(bdst, bsrc);
360 int32_t *src = (int32_t*)bsrc->p_buffer;
361 double *dst = (double *)bdst->p_buffer;
362 for (size_t i = bsrc->i_buffer / 4; i--;)
363 *dst++ = (double)(*src++) / 2147483648.;
372 static block_t *Fl64toU8(filter_t *filter, block_t *b)
374 double *src = (double *)b->p_buffer;
375 uint8_t *dst = (uint8_t *)src;
376 for (size_t i = b->i_buffer / 8; i--;)
378 float s = *(src++) * 128.;
385 *(dst++) = lround(s) + 128;
392 static block_t *Fl64toS16(filter_t *filter, block_t *b)
395 double *src = (double *)b->p_buffer;
396 int16_t *dst = (int16_t *)src;
397 for (size_t i = b->i_buffer / 8; i--;) {
398 const double v = *src++ * 32768.;
402 else if (v < -32768.)
411 static block_t *Fl64toFl32(filter_t *filter, block_t *b)
413 double *src = (double *)b->p_buffer;
414 float *dst = (float *)src;
415 for (size_t i = b->i_buffer / 8; i--;)
422 static block_t *Fl64toS32(filter_t *filter, block_t *b)
424 double *src = (double *)b->p_buffer;
425 int32_t *dst = (int32_t *)src;
426 for (size_t i = b->i_buffer / 8; i--;)
428 float s = *(src++) * 2147483648.;
429 if (s >= 2147483647.f)
430 *(dst++) = 2147483647;
432 if (s <= -2147483648.f)
433 *(dst++) = -2147483648;
435 *(dst++) = lround(s);
444 static const struct {
449 { VLC_CODEC_U8, VLC_CODEC_S16N, U8toS16 },
450 { VLC_CODEC_U8, VLC_CODEC_FL32, U8toFl32 },
451 { VLC_CODEC_U8, VLC_CODEC_S32N, U8toS32 },
452 { VLC_CODEC_U8, VLC_CODEC_FL64, U8toFl64 },
454 { VLC_CODEC_S16N, VLC_CODEC_U8, S16toU8 },
455 { VLC_CODEC_S16N, VLC_CODEC_FL32, S16toFl32 },
456 { VLC_CODEC_S16N, VLC_CODEC_S32N, S16toS32 },
457 { VLC_CODEC_S16N, VLC_CODEC_FL64, S16toFl64 },
459 { VLC_CODEC_FL32, VLC_CODEC_U8, Fl32toU8 },
460 { VLC_CODEC_FL32, VLC_CODEC_S16N, Fl32toS16 },
461 { VLC_CODEC_FL32, VLC_CODEC_S32N, Fl32toS32 },
462 { VLC_CODEC_FL32, VLC_CODEC_FL64, Fl32toFl64 },
464 { VLC_CODEC_S32N, VLC_CODEC_U8, S32toU8 },
465 { VLC_CODEC_S32N, VLC_CODEC_S16N, S32toS16 },
466 { VLC_CODEC_S32N, VLC_CODEC_FL32, S32toFl32 },
467 { VLC_CODEC_S32N, VLC_CODEC_FL64, S32toFl64 },
469 { VLC_CODEC_FL64, VLC_CODEC_U8, Fl64toU8 },
470 { VLC_CODEC_FL64, VLC_CODEC_S16N, Fl64toS16 },
471 { VLC_CODEC_FL64, VLC_CODEC_FL32, Fl64toFl32 },
472 { VLC_CODEC_FL64, VLC_CODEC_S32N, Fl64toS32 },
477 static cvt_t FindConversion(vlc_fourcc_t src, vlc_fourcc_t dst)
479 for (int i = 0; cvt_directs[i].convert; i++) {
480 if (cvt_directs[i].src == src &&
481 cvt_directs[i].dst == dst)
482 return cvt_directs[i].convert;