pcm.c 21.7 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

Fabrice Bellard's avatar
Fabrice Bellard committed
52 53 54
    return 0;
}

55 56
/**
 * Write PCM samples macro
57
 * @param type   Datatype of native machine format
58
 * @param endian bytestream_put_xxx() suffix
59 60 61 62
 * @param src    Source pointer (variable name)
 * @param dst    Destination pointer (variable name)
 * @param n      Total number of samples (variable name)
 * @param shift  Bitshift (bits)
63 64
 * @param offset Sample value offset
 */
65 66 67 68 69
#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);                             \
70
    }
71

72 73 74 75 76 77 78 79 80 81 82
#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);                         \
        }                                                               \
    }

83 84
static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                            const AVFrame *frame, int *got_packet_ptr)
Fabrice Bellard's avatar
Fabrice Bellard committed
85
{
86
    int n, c, sample_size, v, ret;
87
    const short *samples;
Fabrice Bellard's avatar
Fabrice Bellard committed
88
    unsigned char *dst;
89
    const uint8_t *samples_uint8_t;
90 91 92 93 94
    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
95

96
    sample_size = av_get_bits_per_sample(avctx->codec->id) / 8;
97 98 99
    n           = frame->nb_samples * avctx->channels;
    samples     = (const short *)frame->data[0];

100
    if ((ret = ff_alloc_packet2(avctx, avpkt, n * sample_size)) < 0)
101 102
        return ret;
    dst = avpkt->data;
Fabrice Bellard's avatar
Fabrice Bellard committed
103

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

218 219
    *got_packet_ptr = 1;
    return 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
220 221 222
}

typedef struct PCMDecode {
223
    short   table[256];
Fabrice Bellard's avatar
Fabrice Bellard committed
224 225
} PCMDecode;

226
static av_cold int pcm_decode_init(AVCodecContext *avctx)
Fabrice Bellard's avatar
Fabrice Bellard committed
227 228 229 230
{
    PCMDecode *s = avctx->priv_data;
    int i;

231
    if (avctx->channels <= 0) {
232 233 234 235
        av_log(avctx, AV_LOG_ERROR, "PCM channels out of bounds\n");
        return AVERROR(EINVAL);
    }

236
    switch (avctx->codec_id) {
237
    case AV_CODEC_ID_PCM_ALAW:
238
        for (i = 0; i < 256; i++)
Fabrice Bellard's avatar
Fabrice Bellard committed
239 240
            s->table[i] = alaw2linear(i);
        break;
241
    case AV_CODEC_ID_PCM_MULAW:
242
        for (i = 0; i < 256; i++)
Fabrice Bellard's avatar
Fabrice Bellard committed
243 244 245 246 247
            s->table[i] = ulaw2linear(i);
        break;
    default:
        break;
    }
248

249
    avctx->sample_fmt = avctx->codec->sample_fmts[0];
250

251
    if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
252
        avctx->bits_per_raw_sample = av_get_bits_per_sample(avctx->codec_id);
253

Fabrice Bellard's avatar
Fabrice Bellard committed
254 255 256
    return 0;
}

257 258
/**
 * Read PCM samples macro
259
 * @param size   Data size of native machine format
260
 * @param endian bytestream_get_xxx() endian suffix
261 262 263 264
 * @param src    Source pointer (variable name)
 * @param dst    Destination pointer (variable name)
 * @param n      Total number of samples (variable name)
 * @param shift  Bitshift (bits)
265 266
 * @param offset Sample value offset
 */
267 268 269 270 271
#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;                                                \
272
    }
273

274
#define DECODE_PLANAR(size, endian, src, dst, n, shift, offset)         \
275 276
    n /= avctx->channels;                                               \
    for (c = 0; c < avctx->channels; c++) {                             \
277
        int i;                                                          \
278
        dst = frame->extended_data[c];                                \
279 280 281 282 283
        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;                                            \
        }                                                               \
284
    }
285

286 287
static int pcm_decode_frame(AVCodecContext *avctx, void *data,
                            int *got_frame_ptr, AVPacket *avpkt)
Fabrice Bellard's avatar
Fabrice Bellard committed
288
{
289
    const uint8_t *src = avpkt->data;
290 291
    int buf_size       = avpkt->size;
    PCMDecode *s       = avctx->priv_data;
292
    AVFrame *frame     = data;
293
    int sample_size, c, n, ret, samples_per_block;
294
    uint8_t *samples;
295
    int32_t *dst_int32_t;
Fabrice Bellard's avatar
Fabrice Bellard committed
296

297
    sample_size = av_get_bits_per_sample(avctx->codec_id) / 8;
298

299
    /* av_get_bits_per_sample returns 0 for AV_CODEC_ID_PCM_DVD */
300
    samples_per_block = 1;
301
    if (avctx->codec_id == AV_CODEC_ID_PCM_LXF) {
302
        /* we process 40-bit blocks per channel for LXF */
303
        samples_per_block = 2;
304
        sample_size       = 5;
305
    }
306

307 308 309 310 311
    if (sample_size == 0) {
        av_log(avctx, AV_LOG_ERROR, "Invalid sample_size\n");
        return AVERROR(EINVAL);
    }

312 313 314 315 316
    if (avctx->channels == 0) {
        av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
        return AVERROR(EINVAL);
    }

317 318 319 320 321
    if (avctx->codec_id != avctx->codec->id) {
        av_log(avctx, AV_LOG_ERROR, "codec ids mismatch\n");
        return AVERROR(EINVAL);
    }

322
    n = avctx->channels * sample_size;
323

324
    if (n && buf_size % n) {
325
        if (buf_size < n) {
326 327 328 329
            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;
330
        } else
331
            buf_size -= buf_size % n;
332 333
    }

334
    n = buf_size / sample_size;
335

336
    /* get output buffer */
337
    frame->nb_samples = n * samples_per_block / avctx->channels;
338
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
339
        return ret;
340
    samples = frame->data[0];
341

342
    switch (avctx->codec_id) {
343
    case AV_CODEC_ID_PCM_U32LE:
344
        DECODE(32, le32, src, samples, n, 0, 0x80000000)
345
        break;
346
    case AV_CODEC_ID_PCM_U32BE:
347
        DECODE(32, be32, src, samples, n, 0, 0x80000000)
348
        break;
349
    case AV_CODEC_ID_PCM_S24LE:
350
        DECODE(32, le24, src, samples, n, 8, 0)
351
        break;
352 353 354
    case AV_CODEC_ID_PCM_S24LE_PLANAR:
        DECODE_PLANAR(32, le24, src, samples, n, 8, 0);
        break;
355
    case AV_CODEC_ID_PCM_S24BE:
356
        DECODE(32, be24, src, samples, n, 8, 0)
357
        break;
358
    case AV_CODEC_ID_PCM_U24LE:
359
        DECODE(32, le24, src, samples, n, 8, 0x800000)
360
        break;
361
    case AV_CODEC_ID_PCM_U24BE:
362
        DECODE(32, be24, src, samples, n, 8, 0x800000)
363
        break;
364
    case AV_CODEC_ID_PCM_S24DAUD:
365 366 367
        for (; n > 0; n--) {
            uint32_t v = bytestream_get_be24(&src);
            v >>= 4; // sync flags are here
368 369
            AV_WN16A(samples, ff_reverse[(v >> 8) & 0xff] +
                             (ff_reverse[v        & 0xff] << 8));
370
            samples += 2;
371 372
        }
        break;
373
    case AV_CODEC_ID_PCM_U16LE:
374
        DECODE(16, le16, src, samples, n, 0, 0x8000)
Fabrice Bellard's avatar
Fabrice Bellard committed
375
        break;
376
    case AV_CODEC_ID_PCM_U16BE:
377
        DECODE(16, be16, src, samples, n, 0, 0x8000)
Fabrice Bellard's avatar
Fabrice Bellard committed
378
        break;
379
    case AV_CODEC_ID_PCM_S8:
380
        for (; n > 0; n--)
381
            *samples++ = *src++ + 128;
Fabrice Bellard's avatar
Fabrice Bellard committed
382
        break;
383 384 385 386
    case AV_CODEC_ID_PCM_S8_PLANAR:
        n /= avctx->channels;
        for (c = 0; c < avctx->channels; c++) {
            int i;
387
            samples = frame->extended_data[c];
388 389 390 391
            for (i = n; i > 0; i--)
                *samples++ = *src++ + 128;
        }
        break;
392
#if HAVE_BIGENDIAN
393
    case AV_CODEC_ID_PCM_F64LE:
394
        DECODE(64, le64, src, samples, n, 0, 0)
395
        break;
396 397
    case AV_CODEC_ID_PCM_S32LE:
    case AV_CODEC_ID_PCM_F32LE:
398
        DECODE(32, le32, src, samples, n, 0, 0)
399
        break;
400 401 402
    case AV_CODEC_ID_PCM_S32LE_PLANAR:
        DECODE_PLANAR(32, le32, src, samples, n, 0, 0);
        break;
403
    case AV_CODEC_ID_PCM_S16LE:
404
        DECODE(16, le16, src, samples, n, 0, 0)
405
        break;
406 407 408
    case AV_CODEC_ID_PCM_S16LE_PLANAR:
        DECODE_PLANAR(16, le16, src, samples, n, 0, 0);
        break;
409 410 411 412
    case AV_CODEC_ID_PCM_F64BE:
    case AV_CODEC_ID_PCM_F32BE:
    case AV_CODEC_ID_PCM_S32BE:
    case AV_CODEC_ID_PCM_S16BE:
413
#else
414
    case AV_CODEC_ID_PCM_F64BE:
415
        DECODE(64, be64, src, samples, n, 0, 0)
416
        break;
417 418
    case AV_CODEC_ID_PCM_F32BE:
    case AV_CODEC_ID_PCM_S32BE:
419
        DECODE(32, be32, src, samples, n, 0, 0)
420
        break;
421
    case AV_CODEC_ID_PCM_S16BE:
422
        DECODE(16, be16, src, samples, n, 0, 0)
423
        break;
424 425 426
    case AV_CODEC_ID_PCM_S16BE_PLANAR:
        DECODE_PLANAR(16, be16, src, samples, n, 0, 0);
        break;
427 428 429 430
    case AV_CODEC_ID_PCM_F64LE:
    case AV_CODEC_ID_PCM_F32LE:
    case AV_CODEC_ID_PCM_S32LE:
    case AV_CODEC_ID_PCM_S16LE:
431
#endif /* HAVE_BIGENDIAN */
432
    case AV_CODEC_ID_PCM_U8:
433
        memcpy(samples, src, n * sample_size);
Fabrice Bellard's avatar
Fabrice Bellard committed
434
        break;
435 436 437 438 439 440 441 442
#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++) {
443
            samples = frame->extended_data[c];
444 445 446
            bytestream_get_buffer(&src, samples, n * sample_size);
        }
        break;
447
    case AV_CODEC_ID_PCM_ZORK:
448 449 450 451 452
        for (; n > 0; n--) {
            int v = *src++;
            if (v < 128)
                v = 128 - v;
            *samples++ = v;
453 454
        }
        break;
455 456
    case AV_CODEC_ID_PCM_ALAW:
    case AV_CODEC_ID_PCM_MULAW:
457
        for (; n > 0; n--) {
458 459
            AV_WN16A(samples, s->table[*src++]);
            samples += 2;
Fabrice Bellard's avatar
Fabrice Bellard committed
460 461
        }
        break;
462
    case AV_CODEC_ID_PCM_LXF:
463 464
    {
        int i;
465
        n /= avctx->channels;
466
        for (c = 0; c < avctx->channels; c++) {
467
            dst_int32_t = (int32_t *)frame->extended_data[c];
468
            for (i = 0; i < n; i++) {
469
                // extract low 20 bits and expand to 32 bits
470 471 472 473 474
                *dst_int32_t++ =  (src[2]         << 28) |
                                  (src[1]         << 20) |
                                  (src[0]         << 12) |
                                 ((src[2] & 0x0F) <<  8) |
                                   src[1];
475
                // extract high 20 bits and expand to 32 bits
476 477 478 479 480
                *dst_int32_t++ =  (src[4]         << 24) |
                                  (src[3]         << 16) |
                                 ((src[2] & 0xF0) <<  8) |
                                  (src[4]         <<  4) |
                                  (src[3]         >>  4);
481
                src += 5;
482 483 484
            }
        }
        break;
485
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
486 487 488
    default:
        return -1;
    }
489

490
    *got_frame_ptr = 1;
491

492
    return buf_size;
Fabrice Bellard's avatar
Fabrice Bellard committed
493 494
}

495 496
#define PCM_ENCODER_0(id_, sample_fmt_, name_, long_name_)
#define PCM_ENCODER_1(id_, sample_fmt_, name_, long_name_)                  \
497 498
AVCodec ff_ ## name_ ## _encoder = {                                        \
    .name         = #name_,                                                 \
499
    .long_name    = NULL_IF_CONFIG_SMALL(long_name_),                       \
500
    .type         = AVMEDIA_TYPE_AUDIO,                                     \
501
    .id           = AV_CODEC_ID_ ## id_,                                    \
502 503 504 505 506
    .init         = pcm_encode_init,                                        \
    .encode2      = pcm_encode_frame,                                       \
    .capabilities = CODEC_CAP_VARIABLE_FRAME_SIZE,                          \
    .sample_fmts  = (const enum AVSampleFormat[]){ sample_fmt_,             \
                                                   AV_SAMPLE_FMT_NONE },    \
507
}
508

509 510 511 512 513 514 515 516 517
#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_)                  \
518 519
AVCodec ff_ ## name_ ## _decoder = {                                        \
    .name           = #name_,                                               \
520
    .long_name      = NULL_IF_CONFIG_SMALL(long_name_),                     \
521
    .type           = AVMEDIA_TYPE_AUDIO,                                   \
522
    .id             = AV_CODEC_ID_ ## id_,                                  \
523 524 525 526 527 528
    .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 },  \
529
}
530 531 532 533 534 535 536

#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)
537

538 539 540
#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_)
541

542
/* Note: Do not forget to add new entries to the Makefile as well. */
543
PCM_CODEC  (PCM_ALAW,         AV_SAMPLE_FMT_S16, pcm_alaw,         "PCM A-law / G.711 A-law");
544 545 546 547
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");
548 549
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");
550
PCM_CODEC  (PCM_S8,           AV_SAMPLE_FMT_U8,  pcm_s8,           "PCM signed 8-bit");
551
PCM_CODEC  (PCM_S8_PLANAR,    AV_SAMPLE_FMT_U8P, pcm_s8_planar,    "PCM signed 8-bit planar");
552
PCM_CODEC  (PCM_S16BE,        AV_SAMPLE_FMT_S16, pcm_s16be,        "PCM signed 16-bit big-endian");
553
PCM_CODEC  (PCM_S16BE_PLANAR, AV_SAMPLE_FMT_S16P,pcm_s16be_planar, "PCM signed 16-bit big-endian planar");
554
PCM_CODEC  (PCM_S16LE,        AV_SAMPLE_FMT_S16, pcm_s16le,        "PCM signed 16-bit little-endian");
555
PCM_CODEC  (PCM_S16LE_PLANAR, AV_SAMPLE_FMT_S16P,pcm_s16le_planar, "PCM signed 16-bit little-endian planar");
556 557 558
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");
559
PCM_CODEC  (PCM_S24LE_PLANAR, AV_SAMPLE_FMT_S32P,pcm_s24le_planar, "PCM signed 24-bit little-endian planar");
560 561
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");
562
PCM_CODEC  (PCM_S32LE_PLANAR, AV_SAMPLE_FMT_S32P,pcm_s32le_planar, "PCM signed 32-bit little-endian planar");
563 564 565 566 567 568 569 570
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");
571