pcm.c 23.9 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1 2
/*
 * PCM codecs
3
 * Copyright (c) 2001 Fabrice Bellard
Fabrice Bellard's avatar
Fabrice Bellard committed
4
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Fabrice Bellard's avatar
Fabrice Bellard committed
20
 */
21

Michael Niedermayer's avatar
Michael Niedermayer committed
22
/**
23
 * @file
Michael Niedermayer's avatar
Michael Niedermayer committed
24 25
 * PCM codecs
 */
26

27
#include "libavutil/attributes.h"
28
#include "avcodec.h"
Ramiro Polla's avatar
Ramiro Polla committed
29
#include "bytestream.h"
30
#include "internal.h"
31
#include "mathops.h"
32
#include "pcm_tablegen.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
33

34
static av_cold int pcm_encode_init(AVCodecContext *avctx)
Fabrice Bellard's avatar
Fabrice Bellard committed
35
{
36
    avctx->frame_size = 0;
37
    switch (avctx->codec->id) {
38
    case AV_CODEC_ID_PCM_ALAW:
39
        pcm_alaw_tableinit();
Fabrice Bellard's avatar
Fabrice Bellard committed
40
        break;
41
    case AV_CODEC_ID_PCM_MULAW:
42
        pcm_ulaw_tableinit();
Fabrice Bellard's avatar
Fabrice Bellard committed
43 44 45 46
        break;
    default:
        break;
    }
47

48
    avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
49
    avctx->block_align           = avctx->channels * avctx->bits_per_coded_sample / 8;
50
    avctx->bit_rate              = avctx->block_align * avctx->sample_rate * 8;
51
    avctx->coded_frame           = avcodec_alloc_frame();
52 53
    if (!avctx->coded_frame)
        return AVERROR(ENOMEM);
54

Fabrice Bellard's avatar
Fabrice Bellard committed
55 56 57
    return 0;
}

58
static av_cold int pcm_encode_close(AVCodecContext *avctx)
Fabrice Bellard's avatar
Fabrice Bellard committed
59
{
60 61
    av_freep(&avctx->coded_frame);

Fabrice Bellard's avatar
Fabrice Bellard committed
62 63 64
    return 0;
}

65 66
/**
 * Write PCM samples macro
67
 * @param type   Datatype of native machine format
68
 * @param endian bytestream_put_xxx() suffix
69 70 71 72
 * @param src    Source pointer (variable name)
 * @param dst    Destination pointer (variable name)
 * @param n      Total number of samples (variable name)
 * @param shift  Bitshift (bits)
73 74
 * @param offset Sample value offset
 */
75 76 77 78 79
#define ENCODE(type, endian, src, dst, n, shift, offset)                \
    samples_ ## type = (const type *) src;                              \
    for (; n > 0; n--) {                                                \
        register type v = (*samples_ ## type++ >> shift) + offset;      \
        bytestream_put_ ## endian(&dst, v);                             \
80
    }
81

82 83 84 85 86 87 88 89 90 91 92
#define ENCODE_PLANAR(type, endian, dst, n, shift, offset)              \
    n /= avctx->channels;                                               \
    for (c = 0; c < avctx->channels; c++) {                             \
        int i;                                                          \
        samples_ ## type = (const type *) frame->extended_data[c];      \
        for (i = n; i > 0; i--) {                                       \
            register type v = (*samples_ ## type++ >> shift) + offset;  \
            bytestream_put_ ## endian(&dst, v);                         \
        }                                                               \
    }

93 94
static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                            const AVFrame *frame, int *got_packet_ptr)
Fabrice Bellard's avatar
Fabrice Bellard committed
95
{
96
    int n, c, sample_size, v, ret;
97
    const short *samples;
Fabrice Bellard's avatar
Fabrice Bellard committed
98
    unsigned char *dst;
99
    const uint8_t *samples_uint8_t;
100 101 102 103 104
    const int16_t *samples_int16_t;
    const int32_t *samples_int32_t;
    const int64_t *samples_int64_t;
    const uint16_t *samples_uint16_t;
    const uint32_t *samples_uint32_t;
Fabrice Bellard's avatar
Fabrice Bellard committed
105

106
    sample_size = av_get_bits_per_sample(avctx->codec->id) / 8;
107 108 109
    n           = frame->nb_samples * avctx->channels;
    samples     = (const short *)frame->data[0];

110
    if ((ret = ff_alloc_packet2(avctx, avpkt, n * sample_size)))
111 112
        return ret;
    dst = avpkt->data;
Fabrice Bellard's avatar
Fabrice Bellard committed
113

114
    switch (avctx->codec->id) {
115
    case AV_CODEC_ID_PCM_U32LE:
116
        ENCODE(uint32_t, le32, samples, dst, n, 0, 0x80000000)
117
        break;
118
    case AV_CODEC_ID_PCM_U32BE:
119
        ENCODE(uint32_t, be32, samples, dst, n, 0, 0x80000000)
120
        break;
121
    case AV_CODEC_ID_PCM_S24LE:
122
        ENCODE(int32_t, le24, samples, dst, n, 8, 0)
123
        break;
124 125 126
    case AV_CODEC_ID_PCM_S24LE_PLANAR:
        ENCODE_PLANAR(int32_t, le24, dst, n, 8, 0)
        break;
127
    case AV_CODEC_ID_PCM_S24BE:
128
        ENCODE(int32_t, be24, samples, dst, n, 8, 0)
129
        break;
130
    case AV_CODEC_ID_PCM_U24LE:
131
        ENCODE(uint32_t, le24, samples, dst, n, 8, 0x800000)
132
        break;
133
    case AV_CODEC_ID_PCM_U24BE:
134
        ENCODE(uint32_t, be24, samples, dst, n, 8, 0x800000)
135
        break;
136
    case AV_CODEC_ID_PCM_S24DAUD:
137
        for (; n > 0; n--) {
138 139
            uint32_t tmp = ff_reverse[(*samples >> 8) & 0xff] +
                           (ff_reverse[*samples & 0xff] << 8);
140
            tmp <<= 4; // sync flags would go here
Ramiro Polla's avatar
Ramiro Polla committed
141
            bytestream_put_be24(&dst, tmp);
142 143 144
            samples++;
        }
        break;
145
    case AV_CODEC_ID_PCM_U16LE:
146
        ENCODE(uint16_t, le16, samples, dst, n, 0, 0x8000)
Fabrice Bellard's avatar
Fabrice Bellard committed
147
        break;
148
    case AV_CODEC_ID_PCM_U16BE:
149
        ENCODE(uint16_t, be16, samples, dst, n, 0, 0x8000)
Fabrice Bellard's avatar
Fabrice Bellard committed
150
        break;
151
    case AV_CODEC_ID_PCM_S8:
152
        ENCODE(uint8_t, byte, samples, dst, n, 0, -128)
Fabrice Bellard's avatar
Fabrice Bellard committed
153
        break;
154 155 156
    case AV_CODEC_ID_PCM_S8_PLANAR:
        ENCODE_PLANAR(uint8_t, byte, dst, n, 0, -128)
        break;
157
#if HAVE_BIGENDIAN
158
    case AV_CODEC_ID_PCM_F64LE:
159 160
        ENCODE(int64_t, le64, samples, dst, n, 0, 0)
        break;
161 162
    case AV_CODEC_ID_PCM_S32LE:
    case AV_CODEC_ID_PCM_F32LE:
163 164
        ENCODE(int32_t, le32, samples, dst, n, 0, 0)
        break;
165 166 167
    case AV_CODEC_ID_PCM_S32LE_PLANAR:
        ENCODE_PLANAR(int32_t, le32, dst, n, 0, 0)
        break;
168
    case AV_CODEC_ID_PCM_S16LE:
169 170
        ENCODE(int16_t, le16, samples, dst, n, 0, 0)
        break;
171 172 173
    case AV_CODEC_ID_PCM_S16LE_PLANAR:
        ENCODE_PLANAR(int16_t, le16, dst, n, 0, 0)
        break;
174 175 176 177
    case AV_CODEC_ID_PCM_F64BE:
    case AV_CODEC_ID_PCM_F32BE:
    case AV_CODEC_ID_PCM_S32BE:
    case AV_CODEC_ID_PCM_S16BE:
178
#else
179
    case AV_CODEC_ID_PCM_F64BE:
180 181
        ENCODE(int64_t, be64, samples, dst, n, 0, 0)
        break;
182 183
    case AV_CODEC_ID_PCM_F32BE:
    case AV_CODEC_ID_PCM_S32BE:
184 185
        ENCODE(int32_t, be32, samples, dst, n, 0, 0)
        break;
186
    case AV_CODEC_ID_PCM_S16BE:
187 188
        ENCODE(int16_t, be16, samples, dst, n, 0, 0)
        break;
189 190 191
    case AV_CODEC_ID_PCM_S16BE_PLANAR:
        ENCODE_PLANAR(int16_t, be16, dst, n, 0, 0)
        break;
192 193 194 195
    case AV_CODEC_ID_PCM_F64LE:
    case AV_CODEC_ID_PCM_F32LE:
    case AV_CODEC_ID_PCM_S32LE:
    case AV_CODEC_ID_PCM_S16LE:
196
#endif /* HAVE_BIGENDIAN */
197
    case AV_CODEC_ID_PCM_U8:
198
        memcpy(dst, samples, n * sample_size);
Fabrice Bellard's avatar
Fabrice Bellard committed
199
        break;
200 201 202 203 204 205 206 207 208 209 210 211
#if HAVE_BIGENDIAN
    case AV_CODEC_ID_PCM_S16BE_PLANAR:
#else
    case AV_CODEC_ID_PCM_S16LE_PLANAR:
    case AV_CODEC_ID_PCM_S32LE_PLANAR:
#endif /* HAVE_BIGENDIAN */
        n /= avctx->channels;
        for (c = 0; c < avctx->channels; c++) {
            const uint8_t *src = frame->extended_data[c];
            bytestream_put_buffer(&dst, src, n * sample_size);
        }
        break;
212
    case AV_CODEC_ID_PCM_ALAW:
213 214
        for (; n > 0; n--) {
            v      = *samples++;
215
            *dst++ = linear_to_alaw[(v + 32768) >> 2];
Fabrice Bellard's avatar
Fabrice Bellard committed
216 217
        }
        break;
218
    case AV_CODEC_ID_PCM_MULAW:
219 220
        for (; n > 0; n--) {
            v      = *samples++;
221
            *dst++ = linear_to_ulaw[(v + 32768) >> 2];
Fabrice Bellard's avatar
Fabrice Bellard committed
222 223 224 225 226
        }
        break;
    default:
        return -1;
    }
227

228 229
    *got_packet_ptr = 1;
    return 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
230 231 232
}

typedef struct PCMDecode {
233
    AVFrame frame;
234
    short   table[256];
Fabrice Bellard's avatar
Fabrice Bellard committed
235 236
} PCMDecode;

237
static av_cold int pcm_decode_init(AVCodecContext *avctx)
Fabrice Bellard's avatar
Fabrice Bellard committed
238 239 240 241
{
    PCMDecode *s = avctx->priv_data;
    int i;

242
    if (avctx->channels <= 0) {
243 244 245 246
        av_log(avctx, AV_LOG_ERROR, "PCM channels out of bounds\n");
        return AVERROR(EINVAL);
    }

247
    switch (avctx->codec_id) {
248
    case AV_CODEC_ID_PCM_ALAW:
249
        for (i = 0; i < 256; i++)
Fabrice Bellard's avatar
Fabrice Bellard committed
250 251
            s->table[i] = alaw2linear(i);
        break;
252
    case AV_CODEC_ID_PCM_MULAW:
253
        for (i = 0; i < 256; i++)
Fabrice Bellard's avatar
Fabrice Bellard committed
254 255 256 257 258
            s->table[i] = ulaw2linear(i);
        break;
    default:
        break;
    }
259

260
    avctx->sample_fmt = avctx->codec->sample_fmts[0];
261

262
    if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
263
        avctx->bits_per_raw_sample = av_get_bits_per_sample(avctx->codec_id);
264

265 266 267
    avcodec_get_frame_defaults(&s->frame);
    avctx->coded_frame = &s->frame;

Fabrice Bellard's avatar
Fabrice Bellard committed
268 269 270
    return 0;
}

271 272
/**
 * Read PCM samples macro
273
 * @param size   Data size of native machine format
274
 * @param endian bytestream_get_xxx() endian suffix
275 276 277 278
 * @param src    Source pointer (variable name)
 * @param dst    Destination pointer (variable name)
 * @param n      Total number of samples (variable name)
 * @param shift  Bitshift (bits)
279 280
 * @param offset Sample value offset
 */
281 282 283 284 285
#define DECODE(size, endian, src, dst, n, shift, offset)                \
    for (; n > 0; n--) {                                                \
        uint ## size ## _t v = bytestream_get_ ## endian(&src);         \
        AV_WN ## size ## A(dst, (v - offset) << shift);                 \
        dst += size / 8;                                                \
286
    }
287

288
#define DECODE_PLANAR(size, endian, src, dst, n, shift, offset)         \
289 290
    n /= avctx->channels;                                               \
    for (c = 0; c < avctx->channels; c++) {                             \
291
        int i;                                                          \
292 293 294 295 296 297
        dst = s->frame.extended_data[c];                                \
        for (i = n; i > 0; i--) {                                       \
            uint ## size ## _t v = bytestream_get_ ## endian(&src);     \
            AV_WN ## size ## A(dst, (v - offset) << shift);             \
            dst += size / 8;                                            \
        }                                                               \
298
    }
299

300 301
static int pcm_decode_frame(AVCodecContext *avctx, void *data,
                            int *got_frame_ptr, AVPacket *avpkt)
Fabrice Bellard's avatar
Fabrice Bellard committed
302
{
303
    const uint8_t *src = avpkt->data;
304 305
    int buf_size       = avpkt->size;
    PCMDecode *s       = avctx->priv_data;
306
    int sample_size, c, n, ret, samples_per_block;
307
    uint8_t *samples;
308
    int32_t *dst_int32_t;
Fabrice Bellard's avatar
Fabrice Bellard committed
309

310
    sample_size = av_get_bits_per_sample(avctx->codec_id) / 8;
311

312
    /* av_get_bits_per_sample returns 0 for AV_CODEC_ID_PCM_DVD */
313
    samples_per_block = 1;
314
    if (AV_CODEC_ID_PCM_DVD == avctx->codec_id) {
315 316
        if (avctx->bits_per_coded_sample != 20 &&
            avctx->bits_per_coded_sample != 24) {
317 318 319
            av_log(avctx, AV_LOG_ERROR,
                   "PCM DVD unsupported sample depth %i\n",
                   avctx->bits_per_coded_sample);
320 321
            return AVERROR(EINVAL);
        }
322
        /* 2 samples are interleaved per block in PCM_DVD */
323
        samples_per_block = 2;
324
        sample_size       = avctx->bits_per_coded_sample * 2 / 8;
325
    } else if (avctx->codec_id == AV_CODEC_ID_PCM_LXF) {
326
        /* we process 40-bit blocks per channel for LXF */
327
        samples_per_block = 2;
328
        sample_size       = 5;
329
    }
330

331 332 333 334 335
    if (sample_size == 0) {
        av_log(avctx, AV_LOG_ERROR, "Invalid sample_size\n");
        return AVERROR(EINVAL);
    }

336 337 338 339 340
    if (avctx->channels == 0) {
        av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
        return AVERROR(EINVAL);
    }

341 342 343 344 345
    if (avctx->codec_id != avctx->codec->id) {
        av_log(avctx, AV_LOG_ERROR, "codec ids mismatch\n");
        return AVERROR(EINVAL);
    }

346
    n = avctx->channels * sample_size;
347

348
    if (n && buf_size % n) {
349
        if (buf_size < n) {
350 351 352 353
            av_log(avctx, AV_LOG_ERROR,
                   "Invalid PCM packet, data has size %d but at least a size of %d was expected\n",
                   buf_size, n);
            return AVERROR_INVALIDDATA;
354
        } else
355
            buf_size -= buf_size % n;
356 357
    }

358
    n = buf_size / sample_size;
359

360 361
    /* get output buffer */
    s->frame.nb_samples = n * samples_per_block / avctx->channels;
362
    if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
363 364
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
        return ret;
365
    }
366
    samples = s->frame.data[0];
367

368
    switch (avctx->codec_id) {
369
    case AV_CODEC_ID_PCM_U32LE:
370
        DECODE(32, le32, src, samples, n, 0, 0x80000000)
371
        break;
372
    case AV_CODEC_ID_PCM_U32BE:
373
        DECODE(32, be32, src, samples, n, 0, 0x80000000)
374
        break;
375
    case AV_CODEC_ID_PCM_S24LE:
376
        DECODE(32, le24, src, samples, n, 8, 0)
377
        break;
378 379 380
    case AV_CODEC_ID_PCM_S24LE_PLANAR:
        DECODE_PLANAR(32, le24, src, samples, n, 8, 0);
        break;
381
    case AV_CODEC_ID_PCM_S24BE:
382
        DECODE(32, be24, src, samples, n, 8, 0)
383
        break;
384
    case AV_CODEC_ID_PCM_U24LE:
385
        DECODE(32, le24, src, samples, n, 8, 0x800000)
386
        break;
387
    case AV_CODEC_ID_PCM_U24BE:
388
        DECODE(32, be24, src, samples, n, 8, 0x800000)
389
        break;
390
    case AV_CODEC_ID_PCM_S24DAUD:
391 392 393
        for (; n > 0; n--) {
            uint32_t v = bytestream_get_be24(&src);
            v >>= 4; // sync flags are here
394 395
            AV_WN16A(samples, ff_reverse[(v >> 8) & 0xff] +
                             (ff_reverse[v        & 0xff] << 8));
396
            samples += 2;
397 398
        }
        break;
399
    case AV_CODEC_ID_PCM_U16LE:
400
        DECODE(16, le16, src, samples, n, 0, 0x8000)
Fabrice Bellard's avatar
Fabrice Bellard committed
401
        break;
402
    case AV_CODEC_ID_PCM_U16BE:
403
        DECODE(16, be16, src, samples, n, 0, 0x8000)
Fabrice Bellard's avatar
Fabrice Bellard committed
404
        break;
405
    case AV_CODEC_ID_PCM_S8:
406
        for (; n > 0; n--)
407
            *samples++ = *src++ + 128;
Fabrice Bellard's avatar
Fabrice Bellard committed
408
        break;
409 410 411 412 413 414 415 416 417
    case AV_CODEC_ID_PCM_S8_PLANAR:
        n /= avctx->channels;
        for (c = 0; c < avctx->channels; c++) {
            int i;
            samples = s->frame.extended_data[c];
            for (i = n; i > 0; i--)
                *samples++ = *src++ + 128;
        }
        break;
418
#if HAVE_BIGENDIAN
419
    case AV_CODEC_ID_PCM_F64LE:
420
        DECODE(64, le64, src, samples, n, 0, 0)
421
        break;
422 423
    case AV_CODEC_ID_PCM_S32LE:
    case AV_CODEC_ID_PCM_F32LE:
424
        DECODE(32, le32, src, samples, n, 0, 0)
425
        break;
426 427 428
    case AV_CODEC_ID_PCM_S32LE_PLANAR:
        DECODE_PLANAR(32, le32, src, samples, n, 0, 0);
        break;
429
    case AV_CODEC_ID_PCM_S16LE:
430
        DECODE(16, le16, src, samples, n, 0, 0)
431
        break;
432 433 434
    case AV_CODEC_ID_PCM_S16LE_PLANAR:
        DECODE_PLANAR(16, le16, src, samples, n, 0, 0);
        break;
435 436 437 438
    case AV_CODEC_ID_PCM_F64BE:
    case AV_CODEC_ID_PCM_F32BE:
    case AV_CODEC_ID_PCM_S32BE:
    case AV_CODEC_ID_PCM_S16BE:
439
#else
440
    case AV_CODEC_ID_PCM_F64BE:
441
        DECODE(64, be64, src, samples, n, 0, 0)
442
        break;
443 444
    case AV_CODEC_ID_PCM_F32BE:
    case AV_CODEC_ID_PCM_S32BE:
445
        DECODE(32, be32, src, samples, n, 0, 0)
446
        break;
447
    case AV_CODEC_ID_PCM_S16BE:
448
        DECODE(16, be16, src, samples, n, 0, 0)
449
        break;
450 451 452
    case AV_CODEC_ID_PCM_S16BE_PLANAR:
        DECODE_PLANAR(16, be16, src, samples, n, 0, 0);
        break;
453 454 455 456
    case AV_CODEC_ID_PCM_F64LE:
    case AV_CODEC_ID_PCM_F32LE:
    case AV_CODEC_ID_PCM_S32LE:
    case AV_CODEC_ID_PCM_S16LE:
457
#endif /* HAVE_BIGENDIAN */
458
    case AV_CODEC_ID_PCM_U8:
459
        memcpy(samples, src, n * sample_size);
Fabrice Bellard's avatar
Fabrice Bellard committed
460
        break;
461 462 463 464 465 466 467 468 469 470 471 472
#if HAVE_BIGENDIAN
    case AV_CODEC_ID_PCM_S16BE_PLANAR:
#else
    case AV_CODEC_ID_PCM_S16LE_PLANAR:
    case AV_CODEC_ID_PCM_S32LE_PLANAR:
#endif /* HAVE_BIGENDIAN */
        n /= avctx->channels;
        for (c = 0; c < avctx->channels; c++) {
            samples = s->frame.extended_data[c];
            bytestream_get_buffer(&src, samples, n * sample_size);
        }
        break;
473
    case AV_CODEC_ID_PCM_ZORK:
474 475 476 477 478
        for (; n > 0; n--) {
            int v = *src++;
            if (v < 128)
                v = 128 - v;
            *samples++ = v;
479 480
        }
        break;
481 482
    case AV_CODEC_ID_PCM_ALAW:
    case AV_CODEC_ID_PCM_MULAW:
483
        for (; n > 0; n--) {
484 485
            AV_WN16A(samples, s->table[*src++]);
            samples += 2;
Fabrice Bellard's avatar
Fabrice Bellard committed
486 487
        }
        break;
488
    case AV_CODEC_ID_PCM_DVD:
489 490
    {
        const uint8_t *src8;
491
        dst_int32_t = (int32_t *)s->frame.data[0];
492
        n /= avctx->channels;
493
        switch (avctx->bits_per_coded_sample) {
494
        case 20:
495
            while (n--) {
496 497
                c    = avctx->channels;
                src8 = src + 4 * c;
498
                while (c--) {
499 500
                    *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8   & 0xf0) <<  8);
                    *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++ & 0x0f) << 12);
501 502
                }
                src = src8;
503
            }
504 505 506
            break;
        case 24:
            while (n--) {
507 508
                c    = avctx->channels;
                src8 = src + 4 * c;
509 510 511 512 513 514 515
                while (c--) {
                    *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++) << 8);
                    *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++) << 8);
                }
                src = src8;
            }
            break;
516 517
        }
        break;
518
    }
519
    case AV_CODEC_ID_PCM_LXF:
520 521
    {
        int i;
522
        n /= avctx->channels;
523
        for (c = 0; c < avctx->channels; c++) {
524
            dst_int32_t = (int32_t *)s->frame.extended_data[c];
525
            for (i = 0; i < n; i++) {
526
                // extract low 20 bits and expand to 32 bits
527 528 529 530 531
                *dst_int32_t++ =  (src[2]         << 28) |
                                  (src[1]         << 20) |
                                  (src[0]         << 12) |
                                 ((src[2] & 0x0F) <<  8) |
                                   src[1];
532
                // extract high 20 bits and expand to 32 bits
533 534 535 536 537
                *dst_int32_t++ =  (src[4]         << 24) |
                                  (src[3]         << 16) |
                                 ((src[2] & 0xF0) <<  8) |
                                  (src[4]         <<  4) |
                                  (src[3]         >>  4);
538
                src += 5;
539 540 541
            }
        }
        break;
542
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
543 544 545
    default:
        return -1;
    }
546 547 548 549

    *got_frame_ptr   = 1;
    *(AVFrame *)data = s->frame;

550
    return buf_size;
Fabrice Bellard's avatar
Fabrice Bellard committed
551 552
}

553 554
#define PCM_ENCODER_0(id_, sample_fmt_, name_, long_name_)
#define PCM_ENCODER_1(id_, sample_fmt_, name_, long_name_)                  \
555 556 557
AVCodec ff_ ## name_ ## _encoder = {                                        \
    .name         = #name_,                                                 \
    .type         = AVMEDIA_TYPE_AUDIO,                                     \
558
    .id           = AV_CODEC_ID_ ## id_,                                    \
559 560 561 562 563 564 565
    .init         = pcm_encode_init,                                        \
    .encode2      = pcm_encode_frame,                                       \
    .close        = pcm_encode_close,                                       \
    .capabilities = CODEC_CAP_VARIABLE_FRAME_SIZE,                          \
    .sample_fmts  = (const enum AVSampleFormat[]){ sample_fmt_,             \
                                                   AV_SAMPLE_FMT_NONE },    \
    .long_name    = NULL_IF_CONFIG_SMALL(long_name_),                       \
566
}
567

568 569 570 571 572 573 574 575 576
#define PCM_ENCODER_2(cf, id, sample_fmt, name, long_name)                  \
    PCM_ENCODER_ ## cf(id, sample_fmt, name, long_name)
#define PCM_ENCODER_3(cf, id, sample_fmt, name, long_name)                  \
    PCM_ENCODER_2(cf, id, sample_fmt, name, long_name)
#define PCM_ENCODER(id, sample_fmt, name, long_name)                        \
    PCM_ENCODER_3(CONFIG_ ## id ## _ENCODER, id, sample_fmt, name, long_name)

#define PCM_DECODER_0(id, sample_fmt, name, long_name)
#define PCM_DECODER_1(id_, sample_fmt_, name_, long_name_)                  \
577 578 579
AVCodec ff_ ## name_ ## _decoder = {                                        \
    .name           = #name_,                                               \
    .type           = AVMEDIA_TYPE_AUDIO,                                   \
580
    .id             = AV_CODEC_ID_ ## id_,                                  \
581 582 583 584 585 586 587
    .priv_data_size = sizeof(PCMDecode),                                    \
    .init           = pcm_decode_init,                                      \
    .decode         = pcm_decode_frame,                                     \
    .capabilities   = CODEC_CAP_DR1,                                        \
    .sample_fmts    = (const enum AVSampleFormat[]){ sample_fmt_,           \
                                                     AV_SAMPLE_FMT_NONE },  \
    .long_name      = NULL_IF_CONFIG_SMALL(long_name_),                     \
588
}
589 590 591 592 593 594 595

#define PCM_DECODER_2(cf, id, sample_fmt, name, long_name)                  \
    PCM_DECODER_ ## cf(id, sample_fmt, name, long_name)
#define PCM_DECODER_3(cf, id, sample_fmt, name, long_name)                  \
    PCM_DECODER_2(cf, id, sample_fmt, name, long_name)
#define PCM_DECODER(id, sample_fmt, name, long_name)                        \
    PCM_DECODER_3(CONFIG_ ## id ## _DECODER, id, sample_fmt, name, long_name)
596

597 598 599
#define PCM_CODEC(id, sample_fmt_, name, long_name_)                    \
    PCM_ENCODER(id, sample_fmt_, name, long_name_);                     \
    PCM_DECODER(id, sample_fmt_, name, long_name_)
600

601
/* Note: Do not forget to add new entries to the Makefile as well. */
602
PCM_CODEC  (PCM_ALAW,         AV_SAMPLE_FMT_S16, pcm_alaw,         "PCM A-law / G.711 A-law");
603 604 605 606 607
PCM_DECODER(PCM_DVD,          AV_SAMPLE_FMT_S32, pcm_dvd,          "PCM signed 20|24-bit big-endian");
PCM_CODEC  (PCM_F32BE,        AV_SAMPLE_FMT_FLT, pcm_f32be,        "PCM 32-bit floating point big-endian");
PCM_CODEC  (PCM_F32LE,        AV_SAMPLE_FMT_FLT, pcm_f32le,        "PCM 32-bit floating point little-endian");
PCM_CODEC  (PCM_F64BE,        AV_SAMPLE_FMT_DBL, pcm_f64be,        "PCM 64-bit floating point big-endian");
PCM_CODEC  (PCM_F64LE,        AV_SAMPLE_FMT_DBL, pcm_f64le,        "PCM 64-bit floating point little-endian");
608 609
PCM_DECODER(PCM_LXF,          AV_SAMPLE_FMT_S32P,pcm_lxf,          "PCM signed 20-bit little-endian planar");
PCM_CODEC  (PCM_MULAW,        AV_SAMPLE_FMT_S16, pcm_mulaw,        "PCM mu-law / G.711 mu-law");
610
PCM_CODEC  (PCM_S8,           AV_SAMPLE_FMT_U8,  pcm_s8,           "PCM signed 8-bit");
611
PCM_CODEC  (PCM_S8_PLANAR,    AV_SAMPLE_FMT_U8P, pcm_s8_planar,    "PCM signed 8-bit planar");
612
PCM_CODEC  (PCM_S16BE,        AV_SAMPLE_FMT_S16, pcm_s16be,        "PCM signed 16-bit big-endian");
613
PCM_CODEC  (PCM_S16BE_PLANAR, AV_SAMPLE_FMT_S16P,pcm_s16be_planar, "PCM signed 16-bit big-endian planar");
614
PCM_CODEC  (PCM_S16LE,        AV_SAMPLE_FMT_S16, pcm_s16le,        "PCM signed 16-bit little-endian");
615
PCM_CODEC  (PCM_S16LE_PLANAR, AV_SAMPLE_FMT_S16P,pcm_s16le_planar, "PCM signed 16-bit little-endian planar");
616 617 618
PCM_CODEC  (PCM_S24BE,        AV_SAMPLE_FMT_S32, pcm_s24be,        "PCM signed 24-bit big-endian");
PCM_CODEC  (PCM_S24DAUD,      AV_SAMPLE_FMT_S16, pcm_s24daud,      "PCM D-Cinema audio signed 24-bit");
PCM_CODEC  (PCM_S24LE,        AV_SAMPLE_FMT_S32, pcm_s24le,        "PCM signed 24-bit little-endian");
619
PCM_CODEC  (PCM_S24LE_PLANAR, AV_SAMPLE_FMT_S32P,pcm_s24le_planar, "PCM signed 24-bit little-endian planar");
620 621
PCM_CODEC  (PCM_S32BE,        AV_SAMPLE_FMT_S32, pcm_s32be,        "PCM signed 32-bit big-endian");
PCM_CODEC  (PCM_S32LE,        AV_SAMPLE_FMT_S32, pcm_s32le,        "PCM signed 32-bit little-endian");
622
PCM_CODEC  (PCM_S32LE_PLANAR, AV_SAMPLE_FMT_S32P,pcm_s32le_planar, "PCM signed 32-bit little-endian planar");
623 624 625 626 627 628 629 630
PCM_CODEC  (PCM_U8,           AV_SAMPLE_FMT_U8,  pcm_u8,           "PCM unsigned 8-bit");
PCM_CODEC  (PCM_U16BE,        AV_SAMPLE_FMT_S16, pcm_u16be,        "PCM unsigned 16-bit big-endian");
PCM_CODEC  (PCM_U16LE,        AV_SAMPLE_FMT_S16, pcm_u16le,        "PCM unsigned 16-bit little-endian");
PCM_CODEC  (PCM_U24BE,        AV_SAMPLE_FMT_S32, pcm_u24be,        "PCM unsigned 24-bit big-endian");
PCM_CODEC  (PCM_U24LE,        AV_SAMPLE_FMT_S32, pcm_u24le,        "PCM unsigned 24-bit little-endian");
PCM_CODEC  (PCM_U32BE,        AV_SAMPLE_FMT_S32, pcm_u32be,        "PCM unsigned 32-bit big-endian");
PCM_CODEC  (PCM_U32LE,        AV_SAMPLE_FMT_S32, pcm_u32le,        "PCM unsigned 32-bit little-endian");
PCM_DECODER(PCM_ZORK,         AV_SAMPLE_FMT_U8,  pcm_zork,         "PCM Zork");
631