araw.c 23.4 KB
Newer Older
1 2 3
/*****************************************************************************
 * araw.c: Pseudo audio decoder; for raw pcm data
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2001, 2003 VLC authors and VideoLAN
5
 * $Id$
6 7
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12
 * (at your option) any later version.
13
 *
14 15
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
27 28 29 30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

31 32
#include <assert.h>

33
#include <vlc_common.h>
34
#include <vlc_plugin.h>
35
#include <vlc_codec.h>
36
#include <vlc_aout.h>
37 38 39 40

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
41 42
static int  DecoderOpen ( vlc_object_t * );
static void DecoderClose( vlc_object_t * );
43

44
#ifdef ENABLE_SOUT
45
static int  EncoderOpen ( vlc_object_t * );
46
#endif
47

48
vlc_module_begin ()
49
    /* audio decoder module */
50 51 52 53 54
    set_description( N_("Raw/Log Audio decoder") )
    set_capability( "decoder", 100 )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_ACODEC )
    set_callbacks( DecoderOpen, DecoderClose )
55

56
#ifdef ENABLE_SOUT
57
    /* audio encoder submodule */
58 59 60
    add_submodule ()
    set_description( N_("Raw audio encoder") )
    set_capability( "encoder", 150 )
61
    set_callbacks( EncoderOpen, NULL )
62
#endif
63
vlc_module_end ()
64

65 66 67
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
68
static block_t *DecodeBlock( decoder_t *, block_t ** );
69

70
struct decoder_sys_t
71
{
72
    void (*decode) (void *, const uint8_t *, unsigned);
73
    size_t framebits;
74
    date_t end_date;
75
};
76

77
static const uint16_t pi_channels_maps[] =
78 79
{
    0,
80 81 82
    AOUT_CHAN_CENTER, AOUT_CHANS_2_0, AOUT_CHANS_3_0,
    AOUT_CHANS_4_0,   AOUT_CHANS_5_0, AOUT_CHANS_5_1,
    AOUT_CHANS_7_0,   AOUT_CHANS_7_1, AOUT_CHANS_8_1,
83 84
};

85
static void S8Decode( void *, const uint8_t *, unsigned );
86 87 88
static void U16BDecode( void *, const uint8_t *, unsigned );
static void U16LDecode( void *, const uint8_t *, unsigned );
static void S16IDecode( void *, const uint8_t *, unsigned );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
89
static void S20BDecode( void *, const uint8_t *, unsigned );
90 91 92 93
static void U24BDecode( void *, const uint8_t *, unsigned );
static void U24LDecode( void *, const uint8_t *, unsigned );
static void S24BDecode( void *, const uint8_t *, unsigned );
static void S24LDecode( void *, const uint8_t *, unsigned );
94 95
static void S24B32Decode( void *, const uint8_t *, unsigned );
static void S24L32Decode( void *, const uint8_t *, unsigned );
96 97
static void U32BDecode( void *, const uint8_t *, unsigned );
static void U32LDecode( void *, const uint8_t *, unsigned );
98 99 100
static void S32IDecode( void *, const uint8_t *, unsigned );
static void F32IDecode( void *, const uint8_t *, unsigned );
static void F64IDecode( void *, const uint8_t *, unsigned );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
101
static void DAT12Decode( void *, const uint8_t *, unsigned );
102

103
/*****************************************************************************
104
 * DecoderOpen: probe the decoder and return score
105
 *****************************************************************************/
106
static int DecoderOpen( vlc_object_t *p_this )
107
{
108
    decoder_t *p_dec = (decoder_t*)p_this;
109
    vlc_fourcc_t format = p_dec->fmt_in.i_codec;
110 111 112 113

    switch( p_dec->fmt_in.i_codec )
    {
    case VLC_FOURCC('a','r','a','w'):
114
    case VLC_FOURCC('a','f','l','t'):
115
    /* _signed_ big endian samples (mov) */
116
    case VLC_FOURCC('t','w','o','s'):
117
    /* _signed_ little endian samples (mov) */
118
    case VLC_FOURCC('s','o','w','t'):
119 120 121 122 123 124 125 126 127 128
        format =
            vlc_fourcc_GetCodecAudio( p_dec->fmt_in.i_codec,
                                      p_dec->fmt_in.audio.i_bitspersample );
        if( !format )
        {
            msg_Err( p_dec, "bad parameters(bits/sample)" );
            return VLC_EGENERIC;
        }
        break;
    }
129

130 131
    void (*decode) (void *, const uint8_t *, unsigned) = NULL;
    uint_fast8_t bits;
132

133 134
    switch( format )
    {
135
#ifdef WORDS_BIGENDIAN
136
    case VLC_CODEC_F64L:
137
#else
138
    case VLC_CODEC_F64B:
139 140
#endif
        format = VLC_CODEC_FL64;
141
        decode = F64IDecode;
142
    case VLC_CODEC_FL64:
143 144
        bits = 64;
        break;
145
#ifdef WORDS_BIGENDIAN
146
    case VLC_CODEC_F32L:
147
#else
148
    case VLC_CODEC_F32B:
149 150
#endif
        format = VLC_CODEC_FL32;
151
        decode = F32IDecode;
152
    case VLC_CODEC_FL32:
153 154 155 156 157 158 159 160 161 162 163 164 165 166
        bits = 32;
        break;
    case VLC_CODEC_U32B:
        format = VLC_CODEC_S32N;
        decode = U32BDecode;
        bits = 32;
        break;
    case VLC_CODEC_U32L:
        format = VLC_CODEC_S32N;
        decode = U32LDecode;
        bits = 32;
        break;
    case VLC_CODEC_S32I:
        format = VLC_CODEC_S32N;
167
        decode = S32IDecode;
168
    case VLC_CODEC_S32N:
169 170
        bits = 32;
        break;
171 172 173 174 175 176 177 178 179 180
    case VLC_CODEC_S24B32:
        format = VLC_CODEC_S32N;
        decode = S24B32Decode;
        bits = 32;
        break;
    case VLC_CODEC_S24L32:
        format = VLC_CODEC_S32N;
        decode = S24L32Decode;
        bits = 32;
        break;
181 182 183 184 185 186 187 188 189 190
    case VLC_CODEC_U24B:
        format = VLC_CODEC_S32N;
        decode = U24BDecode;
        bits = 24;
        break;
    case VLC_CODEC_U24L:
        format = VLC_CODEC_S32N;
        decode = U24LDecode;
        bits = 24;
        break;
191
    case VLC_CODEC_S24B:
192 193 194 195 196 197 198
        format = VLC_CODEC_S32N;
        decode = S24BDecode;
        bits = 24;
        break;
    case VLC_CODEC_S24L:
        format = VLC_CODEC_S32N;
        decode = S24LDecode;
199 200
        bits = 24;
        break;
201
    case VLC_CODEC_S20B:
202
        format = VLC_CODEC_S32N;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
203
        decode = S20BDecode;
204 205
        bits = 20;
        break;
206 207 208 209 210 211 212 213 214 215 216 217 218 219
    case VLC_CODEC_U16B:
        format = VLC_CODEC_S16N;
        decode = U16BDecode;
        bits = 16;
        break;
    case VLC_CODEC_U16L:
        format = VLC_CODEC_S16N;
        decode = U16LDecode;
        bits = 16;
        break;
    case VLC_CODEC_S16I:
        format = VLC_CODEC_S16N;
        decode = S16IDecode;
    case VLC_CODEC_S16N:
220 221 222 223
        bits = 16;
        break;
    case VLC_CODEC_DAT12:
        format = VLC_CODEC_S16N;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
224
        decode = DAT12Decode;
225 226
        bits = 12;
        break;
227
    case VLC_CODEC_S8:
228 229
        decode = S8Decode;
        format = VLC_CODEC_U8;
230
    case VLC_CODEC_U8:
231 232
        bits = 8;
        break;
233 234 235
    default:
        return VLC_EGENERIC;
    }
236

237
    if( p_dec->fmt_in.audio.i_channels <= 0 ||
238
        p_dec->fmt_in.audio.i_channels > AOUT_CHAN_MAX )
239
    {
240
        msg_Err( p_dec, "bad channels count (1-9): %i",
241
                 p_dec->fmt_in.audio.i_channels );
242
        return VLC_EGENERIC;
243
    }
244 245

    if( p_dec->fmt_in.audio.i_rate <= 0 )
246
    {
247
        msg_Err( p_dec, "bad samplerate: %d Hz", p_dec->fmt_in.audio.i_rate );
248
        return VLC_EGENERIC;
249 250
    }

251 252 253
    msg_Dbg( p_dec, "samplerate:%dHz channels:%d bits/sample:%d",
             p_dec->fmt_in.audio.i_rate, p_dec->fmt_in.audio.i_channels,
             p_dec->fmt_in.audio.i_bitspersample );
254

255 256 257 258
    /* Allocate the memory needed to store the decoder's structure */
    decoder_sys_t *p_sys = malloc(sizeof(*p_sys));
    if( unlikely(p_sys == NULL) )
        return VLC_ENOMEM;
259

260 261
    /* Set output properties */
    p_dec->fmt_out.i_cat = AUDIO_ES;
262 263
    p_dec->fmt_out.i_codec = format;
    p_dec->fmt_out.audio.i_format = format;
264
    p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
265 266
    if( p_dec->fmt_in.audio.i_physical_channels )
        p_dec->fmt_out.audio.i_physical_channels =
267 268 269 270
                                       p_dec->fmt_in.audio.i_physical_channels;
    else
        p_dec->fmt_out.audio.i_physical_channels =
                              pi_channels_maps[p_dec->fmt_in.audio.i_channels];
271 272
    if( p_dec->fmt_in.audio.i_original_channels )
        p_dec->fmt_out.audio.i_original_channels =
273 274 275 276 277 278 279 280 281
                                       p_dec->fmt_in.audio.i_original_channels;
    else
        p_dec->fmt_out.audio.i_original_channels =
                                      p_dec->fmt_out.audio.i_physical_channels;
    aout_FormatPrepare( &p_dec->fmt_out.audio );

    p_sys->decode = decode;
    p_sys->framebits = bits * p_dec->fmt_out.audio.i_channels;
    assert( p_sys->framebits );
282

283 284
    date_Init( &p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1 );
    date_Set( &p_sys->end_date, 0 );
285

286
    p_dec->pf_decode_audio = DecodeBlock;
287
    p_dec->p_sys = p_sys;
288

289
    return VLC_SUCCESS;
290 291
}

292 293 294 295 296
/****************************************************************************
 * DecodeBlock: the whole thing
 ****************************************************************************
 * This function must be fed with whole samples (see nBlockAlign).
 ****************************************************************************/
297
static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
298
{
299
    decoder_sys_t *p_sys = p_dec->p_sys;
300 301
    if( pp_block == NULL )
        return NULL;
302 303

    block_t *p_block = *pp_block;
304 305 306
    if( p_block == NULL )
        return NULL;
    *pp_block = NULL;
307

308
    if( p_block->i_pts > VLC_TS_INVALID &&
309
        p_block->i_pts != date_Get( &p_sys->end_date ) )
310
    {
311
        date_Set( &p_sys->end_date, p_block->i_pts );
312
    }
313
    else if( !date_Get( &p_sys->end_date ) )
314
        /* We've just started the stream, wait for the first PTS. */
315
        goto skip;
316

317
    unsigned samples = (8 * p_block->i_buffer) / p_sys->framebits;
318
    if( samples == 0 )
319
        goto skip;
320

321
    if( p_sys->decode != NULL )
322
    {
323 324 325
        block_t *p_out = decoder_NewAudioBuffer( p_dec, samples );
        if( p_out == NULL )
            goto skip;
326

327 328
        p_sys->decode( p_out->p_buffer, p_block->p_buffer,
                       samples * p_dec->fmt_in.audio.i_channels );
329 330 331
        block_Release( p_block );
        p_block = p_out;
    }
332
    else
333 334
    {
        decoder_UpdateAudioFormat( p_dec );
335
        p_block->i_nb_samples = samples;
336 337
        p_block->i_buffer = samples * (p_sys->framebits / 8);
    }
338

339 340 341 342 343 344 345
    p_block->i_pts = date_Get( &p_sys->end_date );
    p_block->i_length = date_Increment( &p_sys->end_date, samples )
                      - p_block->i_pts;
    return p_block;
skip:
    block_Release( p_block );
    return NULL;
346 347
}

348 349 350 351 352 353 354 355
static void S8Decode( void *outp, const uint8_t *in, unsigned samples )
{
    uint8_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
        out[i] = in[i] ^ 0x80;
}

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
static void U16BDecode( void *outp, const uint8_t *in, unsigned samples )
{
    uint16_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
    {
        *(out++) = GetWBE( in ) - 0x8000;
        in += 2;
    }
}

static void U16LDecode( void *outp, const uint8_t *in, unsigned samples )
{
    uint16_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
    {
        *(out++) = GetWLE( in ) - 0x8000;
        in += 2;
    }
}

static void S16IDecode( void *out, const uint8_t *in, unsigned samples )
{
    swab( in, out, samples * 2 );
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
383
static void S20BDecode( void *outp, const uint8_t *in, unsigned samples )
384 385 386 387 388
{
    int32_t *out = outp;

    while( samples >= 2 )
    {
389 390 391 392 393
        uint32_t dw = U32_AT(in);
        in += 4;
        *(out++) = dw & ~0xFFF;
        *(out++) = (dw << 20) | (*in << 12);
        in++;
394 395 396 397 398
        samples -= 2;
    }

    /* No U32_AT() for the last odd sample: avoid off-by-one overflow! */
    if( samples )
399
        *(out++) = (U16_AT(in) << 16) | ((in[2] & 0xF0) << 8);
400 401
}

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
static void U24BDecode( void *outp, const uint8_t *in, unsigned samples )
{
    uint32_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
    {
        uint32_t s = ((in[0] << 24) | (in[1] << 16) | (in[2] << 8)) - 0x80000000;
        *(out++) = s;
        in += 3;
    }
}

static void U24LDecode( void *outp, const uint8_t *in, unsigned samples )
{
    uint32_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
    {
        uint32_t s = ((in[2] << 24) | (in[1] << 16) | (in[0] << 8)) - 0x80000000;
        *(out++) = s;
        in += 3;
    }
}

static void S24BDecode( void *outp, const uint8_t *in, unsigned samples )
{
    uint32_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
    {
        uint32_t s = ((in[0] << 24) | (in[1] << 16) | (in[2] << 8));
        *(out++) = s;
        in += 3;
    }
}

static void S24LDecode( void *outp, const uint8_t *in, unsigned samples )
{
    uint32_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
    {
        uint32_t s = ((in[2] << 24) | (in[1] << 16) | (in[0] << 8));
        *(out++) = s;
        in += 3;
    }
}

450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
static void S24B32Decode( void *outp, const uint8_t *in, unsigned samples )
{
    uint32_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
    {
        *(out++) = GetDWBE( in ) << 8;
        in += 4;
    }
}

static void S24L32Decode( void *outp, const uint8_t *in, unsigned samples )
{
    uint32_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
    {
        *(out++) = GetDWLE( in ) << 8;
        in += 4;
    }
}

472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
static void U32BDecode( void *outp, const uint8_t *in, unsigned samples )
{
    uint32_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
    {
        *(out++) = GetDWBE( in ) - 0x80000000;
        in += 4;
    }
}

static void U32LDecode( void *outp, const uint8_t *in, unsigned samples )
{
    uint32_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
    {
        *(out++) = GetDWLE( in ) - 0x80000000;
        in += 4;
    }
}

494
static void S32IDecode( void *outp, const uint8_t *in, unsigned samples )
495 496 497 498 499 500 501 502 503 504 505 506 507 508
{
    int32_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
    {
#ifdef WORDS_BIGENDIAN
        *(out++) = GetDWLE( in );
#else
        *(out++) = GetDWBE( in );
#endif
        in += 4;
    }
}

509
static void F32IDecode( void *outp, const uint8_t *in, unsigned samples )
510
{
511
    float *out = outp;
512 513 514

    for( size_t i = 0; i < samples; i++ )
    {
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
        union { float f; uint32_t u; } s;

#ifdef WORDS_BIGENDIAN
        s.u = GetDWLE( in );
#else
        s.u = GetDWBE( in );
#endif
        *(out++) = s.f;
        in += 4;
    }
}

static void F64IDecode( void *outp, const uint8_t *in, unsigned samples )
{
    double *out = outp;

    for( size_t i = 0; i < samples; i++ )
    {
        union { double d; uint64_t u; } s;

535
#ifdef WORDS_BIGENDIAN
536
        s.u = GetQWLE( in );
537
#else
538
        s.u = GetQWBE( in );
539
#endif
540
        *(out++) = s.d;
541 542 543 544
        in += 8;
    }
}

545 546 547 548 549 550 551 552 553 554 555 556
static int16_t dat12tos16( uint16_t y )
{
    static const uint16_t diff[16] = {
       0x0000, 0x0000, 0x0100, 0x0200, 0x0300, 0x0400, 0x0500, 0x0600,
       0x0A00, 0x0B00, 0x0C00, 0x0D00, 0x0E00, 0x0F00, 0x1000, 0x1000 };
    static const uint8_t shift[16] = {
        0, 0, 1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1, 0, 0 };

    int d = y >> 8;
    return (y - diff[d]) << shift[d];
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
557
static void DAT12Decode( void *outp, const uint8_t *in, unsigned samples )
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
{
    int32_t *out = outp;

    while( samples >= 2 )
    {
        *(out++) = dat12tos16(U16_AT(in) >> 4);
        *(out++) = dat12tos16(U16_AT(in + 1) & ~0xF000);
        in += 3;
        samples -= 2;
    }

    if( samples )
        *(out++) = dat12tos16(U16_AT(in) >> 4);
}

573
/*****************************************************************************
574
 * DecoderClose: decoder destruction
575
 *****************************************************************************/
576
static void DecoderClose( vlc_object_t *p_this )
577
{
578
    decoder_t *p_dec = (decoder_t *)p_this;
579

580
    free( p_dec->p_sys );
581
}
582

583
#ifdef ENABLE_SOUT
584 585 586 587
/* NOTE: Output buffers are always aligned since they are allocated by the araw plugin.
 * Contrary to the decoder, the encoder can also assume that input buffers are aligned,
 * since decoded audio blocks must always be aligned. */

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
static void U16IEncode( void *outp, const uint8_t *inp, unsigned samples )
{
    const uint16_t *in = (const uint16_t *)inp;
    uint16_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
        *(out++) =  bswap16( *(in++) + 0x8000 );
}

static void U16NEncode( void *outp, const uint8_t *inp, unsigned samples )
{
    const uint16_t *in = (const uint16_t *)inp;
    uint16_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
        *(out++) =  *(in++) + 0x8000;
}

606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
static void U24BEncode( void *outp, const uint8_t *inp, unsigned samples )
{
    const uint32_t *in = (const uint32_t *)inp;
    uint8_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
    {
        uint32_t s = *(in++);
        *(out++) = (s >> 24) + 0x80;
        *(out++) = (s >> 16);
        *(out++) = (s >>  8);
    }
}

static void U24LEncode( void *outp, const uint8_t *inp, unsigned samples )
{
    const uint32_t *in = (const uint32_t *)inp;
    uint8_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
    {
        uint32_t s = *(in++);
        *(out++) = (s >>  8);
        *(out++) = (s >> 16);
        *(out++) = (s >> 24) + 0x80;
    }
}

static void S24BEncode( void *outp, const uint8_t *inp, unsigned samples )
{
    const uint32_t *in = (const uint32_t *)inp;
    uint8_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
    {
        uint32_t s = *(in++);
        *(out++) = (s >> 24);
        *(out++) = (s >> 16);
        *(out++) = (s >>  8);
    }
}

static void S24LEncode( void *outp, const uint8_t *inp, unsigned samples )
{
    const uint32_t *in = (const uint32_t *)inp;
    uint8_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
    {
        uint32_t s = *(in++);
        *(out++) = (s >>  8);
        *(out++) = (s >> 16);
        *(out++) = (s >> 24);
    }
}

662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
static void U32IEncode( void *outp, const uint8_t *inp, unsigned samples )
{
    const uint32_t *in = (const uint32_t *)inp;
    uint32_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
        *(out++) =  bswap32( *(in++) + 0x80000000 );
}

static void U32NEncode( void *outp, const uint8_t *inp, unsigned samples )
{
    const uint32_t *in = (const uint32_t *)inp;
    uint32_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
        *(out++) =  *(in++) + 0x80000000;
}

680
static void S32IEncode( void *outp, const uint8_t *inp, unsigned samples )
681 682 683 684 685 686 687 688
{
    const int32_t *in = (const int32_t *)inp;
    int32_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
        *(out++) = bswap32( *(in++) );
}

689
static void F32IEncode( void *outp, const uint8_t *inp, unsigned samples )
690
{
691 692
    const float *in = (const float *)inp;
    uint8_t *out = outp;
693 694

    for( size_t i = 0; i < samples; i++ )
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
    {
        union { float f; uint32_t u; char b[4]; } s;

        s.f = *(in++);
        s.u = bswap32( s.u );
        memcpy( out, s.b, 4 );
        out += 4;
    }
}

static void F64IEncode( void *outp, const uint8_t *inp, unsigned samples )
{
    const double *in = (const double *)inp;
    uint8_t *out = outp;

    for( size_t i = 0; i < samples; i++ )
    {
        union { double d; uint64_t u; char b[8]; } s;

        s.d = *(in++);
        s.u = bswap64( s.u );
        memcpy( out, s.b, 8 );
        out += 8;
    }
719 720
}

721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
static block_t *Encode( encoder_t *enc, block_t *in )
{
    if( in == NULL )
        return NULL;

    block_t *out = block_Alloc( in->i_nb_samples
                                * enc->fmt_out.audio.i_bytes_per_frame );
    if( unlikely(out == NULL) )
        return NULL;

    out->i_flags      = in->i_flags;
    out->i_nb_samples = in->i_nb_samples;
    out->i_dts        = in->i_dts;
    out->i_pts        = in->i_pts;
    out->i_length     = in->i_length;
    out->i_nb_samples = in->i_nb_samples;

    void (*encode)(void *, const uint8_t *, unsigned) = (void *)enc->p_sys;
    if( encode != NULL )
        encode( out->p_buffer, in->p_buffer, in->i_nb_samples
                                             * enc->fmt_out.audio.i_channels );
    else
        memcpy( out->p_buffer, in->p_buffer, in->i_buffer );
    return out;
}

/**
 * Probes the PCM audio encoder.
 */
750
static int EncoderOpen( vlc_object_t *p_this )
751 752
{
    encoder_t *p_enc = (encoder_t *)p_this;
753
    void (*encode)(void *, const uint8_t *, unsigned) = NULL;
754

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
755
    switch( p_enc->fmt_out.i_codec )
756
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
757
    case VLC_CODEC_S8:
758 759
        encode = S8Decode;
    case VLC_CODEC_U8:
760
        p_enc->fmt_in.i_codec = VLC_CODEC_U8;
761
        p_enc->fmt_out.audio.i_bitspersample = 8;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
762
        break;
763 764 765 766 767 768 769 770 771 772 773 774
    case VLC_CODEC_U16I:
        encode = U16IEncode;
        p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
        p_enc->fmt_out.audio.i_bitspersample = 16;
        break;
    case VLC_CODEC_U16N:
        encode = U16NEncode;
        p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
        p_enc->fmt_out.audio.i_bitspersample = 16;
        break;
    case VLC_CODEC_S16I:
        encode = S16IDecode;
775
    case VLC_CODEC_S16N:
776 777 778
        p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
        p_enc->fmt_out.audio.i_bitspersample = 16;
        break;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
779
    case VLC_CODEC_U24B:
780 781 782 783 784 785 786 787 788
        encode = U24BEncode;
        p_enc->fmt_in.i_codec = VLC_CODEC_S32N;
        p_enc->fmt_out.audio.i_bitspersample = 24;
        break;
    case VLC_CODEC_U24L:
        encode = U24LEncode;
        p_enc->fmt_in.i_codec = VLC_CODEC_S32N;
        p_enc->fmt_out.audio.i_bitspersample = 24;
        break;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
789
    case VLC_CODEC_S24B:
790 791 792 793 794 795 796
        encode = S24BEncode;
        p_enc->fmt_in.i_codec = VLC_CODEC_S32N;
        p_enc->fmt_out.audio.i_bitspersample = 24;
        break;
    case VLC_CODEC_S24L:
        encode = S24LEncode;
        p_enc->fmt_in.i_codec = VLC_CODEC_S32N;
797
        p_enc->fmt_out.audio.i_bitspersample = 24;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
798
        break;
799 800 801 802 803 804 805 806 807 808 809
    case VLC_CODEC_U32I:
        encode = U32IEncode;
        p_enc->fmt_in.i_codec = VLC_CODEC_S32N;
        p_enc->fmt_out.audio.i_bitspersample = 32;
        break;
    case VLC_CODEC_U32N:
        encode = U32NEncode;
        p_enc->fmt_in.i_codec = VLC_CODEC_S32N;
        p_enc->fmt_out.audio.i_bitspersample = 32;
        break;
    case VLC_CODEC_S32I:
810
        encode = S32IEncode;
811 812 813 814
    case VLC_CODEC_S32N:
        p_enc->fmt_in.i_codec = VLC_CODEC_S32N;
        p_enc->fmt_out.audio.i_bitspersample = 32;
        break;
815 816 817 818 819
#ifdef WORDS_BIGENDIAN
    case VLC_CODEC_F32L:
#else
    case VLC_CODEC_F32B:
#endif
820
        encode = F32IEncode;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
821
    case VLC_CODEC_FL32:
822
        p_enc->fmt_in.i_codec = VLC_CODEC_FL32;
823
        p_enc->fmt_out.audio.i_bitspersample = 32;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
824
        break;
825 826 827 828 829
#ifdef WORDS_BIGENDIAN
    case VLC_CODEC_F64L:
#else
    case VLC_CODEC_F64B:
#endif
830
        encode = F64IEncode;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
831
    case VLC_CODEC_FL64:
832
        p_enc->fmt_in.i_codec = VLC_CODEC_FL64;
833
        p_enc->fmt_out.audio.i_bitspersample = 64;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
834 835
        break;
    default:
836
        return VLC_EGENERIC;
837 838
    }

839 840 841 842 843
    p_enc->p_sys = (void *)encode;
    p_enc->pf_encode_audio = Encode;
    p_enc->fmt_out.audio.i_bytes_per_frame =
        (p_enc->fmt_out.audio.i_bitspersample / 8) *
        p_enc->fmt_in.audio.i_channels;
844 845 846
    p_enc->fmt_out.i_bitrate =
        p_enc->fmt_in.audio.i_channels *
        p_enc->fmt_in.audio.i_rate *
847
        p_enc->fmt_out.audio.i_bitspersample;
848

849 850 851
    msg_Dbg( p_enc, "samplerate:%dHz channels:%d bits/sample:%d",
             p_enc->fmt_out.audio.i_rate, p_enc->fmt_out.audio.i_channels,
             p_enc->fmt_out.audio.i_bitspersample );
852 853 854

    return VLC_SUCCESS;
}
855
#endif /* ENABLE_SOUT */