Coverage Report

Created: 2025-08-28 07:12

/src/ffmpeg/libavcodec/pcm.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * PCM codecs
3
 * Copyright (c) 2001 Fabrice Bellard
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
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
20
 */
21
22
/**
23
 * @file
24
 * PCM codecs
25
 */
26
27
#include "config.h"
28
#include "config_components.h"
29
#include "libavutil/attributes.h"
30
#include "libavutil/float_dsp.h"
31
#include "libavutil/mem.h"
32
#include "libavutil/reverse.h"
33
#include "libavutil/thread.h"
34
#include "avcodec.h"
35
#include "bytestream.h"
36
#include "codec_internal.h"
37
#include "decode.h"
38
#include "encode.h"
39
#include "pcm_tablegen.h"
40
41
static av_cold int pcm_encode_init(AVCodecContext *avctx)
42
0
{
43
0
    avctx->frame_size = 0;
44
0
#if !CONFIG_HARDCODED_TABLES
45
0
    switch (avctx->codec->id) {
46
0
#define INIT_ONCE(id, name)                                                 \
47
0
    case AV_CODEC_ID_PCM_ ## id:                                            \
48
0
        if (CONFIG_PCM_ ## id ## _ENCODER) {                                \
49
0
            static AVOnce init_static_once = AV_ONCE_INIT;                  \
50
0
            ff_thread_once(&init_static_once, pcm_ ## name ## _tableinit);  \
51
0
        }                                                                   \
52
0
        break
53
0
        INIT_ONCE(ALAW,  alaw);
54
0
        INIT_ONCE(MULAW, ulaw);
55
0
        INIT_ONCE(VIDC,  vidc);
56
0
    default:
57
0
        break;
58
0
    }
59
0
#endif
60
61
0
    avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
62
0
    avctx->block_align           = avctx->ch_layout.nb_channels * avctx->bits_per_coded_sample / 8;
63
0
    avctx->bit_rate              = avctx->block_align * 8LL * avctx->sample_rate;
64
65
0
    return 0;
66
0
}
67
68
/**
69
 * Write PCM samples macro
70
 * @param type   Datatype of native machine format
71
 * @param endian bytestream_put_xxx() suffix
72
 * @param src    Source pointer (variable name)
73
 * @param dst    Destination pointer (variable name)
74
 * @param n      Total number of samples (variable name)
75
 * @param shift  Bitshift (bits)
76
 * @param offset Sample value offset
77
 */
78
#define ENCODE(type, endian, src, dst, n, shift, offset)                \
79
0
    samples_ ## type = (const type *) src;                              \
80
0
    for (; n > 0; n--) {                                                \
81
0
        register type v = (*samples_ ## type++ >> shift) + offset;      \
82
0
        bytestream_put_ ## endian(&dst, v);                             \
83
0
    }
84
85
#define ENCODE_PLANAR(type, endian, dst, n, shift, offset)              \
86
0
    n /= avctx->ch_layout.nb_channels;                                  \
87
0
    for (c = 0; c < avctx->ch_layout.nb_channels; c++) {                \
88
0
        int i;                                                          \
89
0
        samples_ ## type = (const type *) frame->extended_data[c];      \
90
0
        for (i = n; i > 0; i--) {                                       \
91
0
            register type v = (*samples_ ## type++ >> shift) + offset;  \
92
0
            bytestream_put_ ## endian(&dst, v);                         \
93
0
        }                                                               \
94
0
    }
95
96
static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
97
                            const AVFrame *frame, int *got_packet_ptr)
98
0
{
99
0
    int n, c, sample_size, v, ret;
100
0
    const short *samples;
101
0
    unsigned char *dst;
102
0
    const uint8_t *samples_uint8_t;
103
0
    const int16_t *samples_int16_t;
104
0
    const int32_t *samples_int32_t;
105
0
    const int64_t *samples_int64_t;
106
0
    const uint16_t *samples_uint16_t;
107
0
    const uint32_t *samples_uint32_t;
108
109
0
    sample_size = av_get_bits_per_sample(avctx->codec->id) / 8;
110
0
    n           = frame->nb_samples * avctx->ch_layout.nb_channels;
111
0
    samples     = (const short *)frame->data[0];
112
113
0
    if ((ret = ff_get_encode_buffer(avctx, avpkt, n * sample_size, 0)) < 0)
114
0
        return ret;
115
0
    dst = avpkt->data;
116
117
0
    switch (avctx->codec->id) {
118
0
    case AV_CODEC_ID_PCM_U32LE:
119
0
        ENCODE(uint32_t, le32, samples, dst, n, 0, 0x80000000)
120
0
        break;
121
0
    case AV_CODEC_ID_PCM_U32BE:
122
0
        ENCODE(uint32_t, be32, samples, dst, n, 0, 0x80000000)
123
0
        break;
124
0
    case AV_CODEC_ID_PCM_S24LE:
125
0
        ENCODE(int32_t, le24, samples, dst, n, 8, 0)
126
0
        break;
127
0
    case AV_CODEC_ID_PCM_S24LE_PLANAR:
128
0
        ENCODE_PLANAR(int32_t, le24, dst, n, 8, 0)
129
0
        break;
130
0
    case AV_CODEC_ID_PCM_S24BE:
131
0
        ENCODE(int32_t, be24, samples, dst, n, 8, 0)
132
0
        break;
133
0
    case AV_CODEC_ID_PCM_U24LE:
134
0
        ENCODE(uint32_t, le24, samples, dst, n, 8, 0x800000)
135
0
        break;
136
0
    case AV_CODEC_ID_PCM_U24BE:
137
0
        ENCODE(uint32_t, be24, samples, dst, n, 8, 0x800000)
138
0
        break;
139
0
    case AV_CODEC_ID_PCM_S24DAUD:
140
0
        for (; n > 0; n--) {
141
0
            uint32_t tmp = ff_reverse[(*samples >> 8) & 0xff] +
142
0
                           (ff_reverse[*samples & 0xff] << 8);
143
0
            tmp <<= 4; // sync flags would go here
144
0
            bytestream_put_be24(&dst, tmp);
145
0
            samples++;
146
0
        }
147
0
        break;
148
0
    case AV_CODEC_ID_PCM_U16LE:
149
0
        ENCODE(uint16_t, le16, samples, dst, n, 0, 0x8000)
150
0
        break;
151
0
    case AV_CODEC_ID_PCM_U16BE:
152
0
        ENCODE(uint16_t, be16, samples, dst, n, 0, 0x8000)
153
0
        break;
154
0
    case AV_CODEC_ID_PCM_S8:
155
0
        ENCODE(uint8_t, byte, samples, dst, n, 0, -128)
156
0
        break;
157
0
    case AV_CODEC_ID_PCM_S8_PLANAR:
158
0
        ENCODE_PLANAR(uint8_t, byte, dst, n, 0, -128)
159
0
        break;
160
#if HAVE_BIGENDIAN
161
    case AV_CODEC_ID_PCM_S64LE:
162
    case AV_CODEC_ID_PCM_F64LE:
163
        ENCODE(int64_t, le64, samples, dst, n, 0, 0)
164
        break;
165
    case AV_CODEC_ID_PCM_S32LE:
166
    case AV_CODEC_ID_PCM_F32LE:
167
        ENCODE(int32_t, le32, samples, dst, n, 0, 0)
168
        break;
169
    case AV_CODEC_ID_PCM_S32LE_PLANAR:
170
        ENCODE_PLANAR(int32_t, le32, dst, n, 0, 0)
171
        break;
172
    case AV_CODEC_ID_PCM_S16LE:
173
        ENCODE(int16_t, le16, samples, dst, n, 0, 0)
174
        break;
175
    case AV_CODEC_ID_PCM_S16LE_PLANAR:
176
        ENCODE_PLANAR(int16_t, le16, dst, n, 0, 0)
177
        break;
178
    case AV_CODEC_ID_PCM_F64BE:
179
    case AV_CODEC_ID_PCM_F32BE:
180
    case AV_CODEC_ID_PCM_S64BE:
181
    case AV_CODEC_ID_PCM_S32BE:
182
    case AV_CODEC_ID_PCM_S16BE:
183
#else
184
0
    case AV_CODEC_ID_PCM_S64BE:
185
0
    case AV_CODEC_ID_PCM_F64BE:
186
0
        ENCODE(int64_t, be64, samples, dst, n, 0, 0)
187
0
        break;
188
0
    case AV_CODEC_ID_PCM_F32BE:
189
0
    case AV_CODEC_ID_PCM_S32BE:
190
0
        ENCODE(int32_t, be32, samples, dst, n, 0, 0)
191
0
        break;
192
0
    case AV_CODEC_ID_PCM_S16BE:
193
0
        ENCODE(int16_t, be16, samples, dst, n, 0, 0)
194
0
        break;
195
0
    case AV_CODEC_ID_PCM_S16BE_PLANAR:
196
0
        ENCODE_PLANAR(int16_t, be16, dst, n, 0, 0)
197
0
        break;
198
0
    case AV_CODEC_ID_PCM_F64LE:
199
0
    case AV_CODEC_ID_PCM_F32LE:
200
0
    case AV_CODEC_ID_PCM_S64LE:
201
0
    case AV_CODEC_ID_PCM_S32LE:
202
0
    case AV_CODEC_ID_PCM_S16LE:
203
0
#endif /* HAVE_BIGENDIAN */
204
0
    case AV_CODEC_ID_PCM_U8:
205
0
        memcpy(dst, samples, n * sample_size);
206
0
        break;
207
#if HAVE_BIGENDIAN
208
    case AV_CODEC_ID_PCM_S16BE_PLANAR:
209
#else
210
0
    case AV_CODEC_ID_PCM_S16LE_PLANAR:
211
0
    case AV_CODEC_ID_PCM_S32LE_PLANAR:
212
0
#endif /* HAVE_BIGENDIAN */
213
0
        n /= avctx->ch_layout.nb_channels;
214
0
        for (c = 0; c < avctx->ch_layout.nb_channels; c++) {
215
0
            const uint8_t *src = frame->extended_data[c];
216
0
            bytestream_put_buffer(&dst, src, n * sample_size);
217
0
        }
218
0
        break;
219
0
    case AV_CODEC_ID_PCM_ALAW:
220
0
        for (; n > 0; n--) {
221
0
            v      = *samples++;
222
0
            *dst++ = linear_to_alaw[(v + 32768) >> 2];
223
0
        }
224
0
        break;
225
0
    case AV_CODEC_ID_PCM_MULAW:
226
0
        for (; n > 0; n--) {
227
0
            v      = *samples++;
228
0
            *dst++ = linear_to_ulaw[(v + 32768) >> 2];
229
0
        }
230
0
        break;
231
0
    case AV_CODEC_ID_PCM_VIDC:
232
0
        for (; n > 0; n--) {
233
0
            v      = *samples++;
234
0
            *dst++ = linear_to_vidc[(v + 32768) >> 2];
235
0
        }
236
0
        break;
237
0
    default:
238
0
        return -1;
239
0
    }
240
241
0
    *got_packet_ptr = 1;
242
0
    return 0;
243
0
}
244
245
typedef struct PCMDecode {
246
    int sample_size;
247
} PCMDecode;
248
249
static av_cold av_unused int pcm_decode_init(AVCodecContext *avctx)
250
16.5k
{
251
16.5k
    PCMDecode *s = avctx->priv_data;
252
16.5k
    static const struct {
253
16.5k
        enum AVCodecID codec_id;
254
16.5k
        int8_t sample_fmt;
255
16.5k
        uint8_t sample_size;
256
16.5k
        uint8_t bits_per_sample;
257
16.5k
    } codec_id_to_samplefmt[] = {
258
16.5k
    #define ENTRY(CODEC_ID, SAMPLE_FMT, BITS_PER_SAMPLE) \
259
447k
        { AV_CODEC_ID_PCM_ ## CODEC_ID, AV_SAMPLE_FMT_ ## SAMPLE_FMT, \
260
447k
        BITS_PER_SAMPLE / 8, BITS_PER_SAMPLE }
261
16.5k
        ENTRY(S8, U8, 8),        ENTRY(S8_PLANAR, U8P, 8),
262
16.5k
        ENTRY(S16BE, S16, 16),   ENTRY(S16BE_PLANAR, S16P, 16),
263
16.5k
        ENTRY(S16LE, S16, 16),   ENTRY(S16LE_PLANAR, S16P, 16),
264
16.5k
        ENTRY(S24DAUD, S16, 24), ENTRY(S24BE, S32, 24),
265
16.5k
        ENTRY(S24LE, S32, 24),   ENTRY(S24LE_PLANAR, S32P, 24),
266
16.5k
        ENTRY(S32BE, S32, 32),   ENTRY(S32LE, S32, 32),
267
16.5k
        ENTRY(S32LE_PLANAR, S32P, 32),
268
16.5k
        ENTRY(S64BE, S64, 64),   ENTRY(S64LE, S64, 64),
269
16.5k
        ENTRY(SGA, U8, 8),       ENTRY(U8, U8, 8),
270
16.5k
        ENTRY(U16BE, S16, 16),   ENTRY(U16LE, S16, 16),
271
16.5k
        ENTRY(U24BE, S32, 24),   ENTRY(U24LE, S32, 24),
272
16.5k
        ENTRY(U32BE, S32, 32),   ENTRY(U32LE, S32, 32),
273
16.5k
        ENTRY(F32BE, FLT, 32),   ENTRY(F32LE, FLT, 32),
274
16.5k
        ENTRY(F64BE, DBL, 64),   ENTRY(F64LE, DBL, 64),
275
16.5k
        { .codec_id = AV_CODEC_ID_PCM_LXF, .sample_fmt = AV_SAMPLE_FMT_S32P, .sample_size = 5 },
276
16.5k
    };
277
278
239k
    for (unsigned i = 0; i < FF_ARRAY_ELEMS(codec_id_to_samplefmt); ++i) {
279
239k
        if (codec_id_to_samplefmt[i].codec_id == avctx->codec_id) {
280
16.5k
            s->sample_size    = codec_id_to_samplefmt[i].sample_size;
281
16.5k
            avctx->sample_fmt = codec_id_to_samplefmt[i].sample_fmt;
282
16.5k
            if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
283
4.73k
                avctx->bits_per_raw_sample = codec_id_to_samplefmt[i].bits_per_sample;
284
16.5k
            break;
285
16.5k
        }
286
223k
        av_assert1(i + 1 < FF_ARRAY_ELEMS(codec_id_to_samplefmt));
287
223k
    }
288
289
16.5k
    return 0;
290
16.5k
}
291
292
typedef struct PCMScaleDecode {
293
    PCMDecode base;
294
    void (*vector_fmul_scalar)(float *dst, const float *src, float mul,
295
                               int len);
296
    float   scale;
297
} PCMScaleDecode;
298
299
static av_cold av_unused int pcm_scale_decode_init(AVCodecContext *avctx)
300
1.13k
{
301
1.13k
    PCMScaleDecode *s = avctx->priv_data;
302
1.13k
    AVFloatDSPContext *fdsp;
303
304
1.13k
    avctx->sample_fmt   = AV_SAMPLE_FMT_FLT;
305
1.13k
    s->base.sample_size = 4;
306
307
1.13k
    if (avctx->bits_per_coded_sample < 1 || avctx->bits_per_coded_sample > 24)
308
176
        return AVERROR_INVALIDDATA;
309
310
954
    s->scale = 1. / (1 << (avctx->bits_per_coded_sample - 1));
311
954
    fdsp = avpriv_float_dsp_alloc(0);
312
954
    if (!fdsp)
313
0
        return AVERROR(ENOMEM);
314
954
    s->vector_fmul_scalar = fdsp->vector_fmul_scalar;
315
954
    av_free(fdsp);
316
317
954
    return 0;
318
954
}
319
320
typedef struct PCMLUTDecode {
321
    PCMDecode base;
322
    int16_t   table[256];
323
} PCMLUTDecode;
324
325
static av_cold av_unused int pcm_lut_decode_init(AVCodecContext *avctx)
326
1.71k
{
327
1.71k
    PCMLUTDecode *s = avctx->priv_data;
328
329
1.71k
    switch (avctx->codec_id) {
330
0
    default:
331
0
        av_unreachable("pcm_lut_decode_init() only used with alaw, mulaw and vidc");
332
565
    case AV_CODEC_ID_PCM_ALAW:
333
145k
        for (int i = 0; i < 256; i++)
334
144k
            s->table[i] = alaw2linear(i);
335
565
        break;
336
565
    case AV_CODEC_ID_PCM_MULAW:
337
145k
        for (int i = 0; i < 256; i++)
338
144k
            s->table[i] = ulaw2linear(i);
339
565
        break;
340
586
    case AV_CODEC_ID_PCM_VIDC:
341
150k
        for (int i = 0; i < 256; i++)
342
150k
            s->table[i] = vidc2linear(i);
343
586
        break;
344
1.71k
    }
345
346
1.71k
    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
347
1.71k
    s->base.sample_size = 1;
348
349
1.71k
    return 0;
350
1.71k
}
351
352
/**
353
 * Read PCM samples macro
354
 * @param size   Data size of native machine format
355
 * @param endian bytestream_get_xxx() endian suffix
356
 * @param src    Source pointer (variable name)
357
 * @param dst    Destination pointer (variable name)
358
 * @param n      Total number of samples (variable name)
359
 * @param shift  Bitshift (bits)
360
 * @param offset Sample value offset
361
 */
362
#define DECODE(size, endian, src, dst, n, shift, offset)                       \
363
85.5M
    for (; n > 0; n--) {                                                       \
364
83.1M
        uint ## size ## _t v = bytestream_get_ ## endian(&src);                \
365
83.1M
        AV_WN ## size ## A(dst, (uint ## size ## _t)(v - offset) << shift);    \
366
83.1M
        dst += size / 8;                                                       \
367
83.1M
    }
368
369
#define DECODE_PLANAR(size, endian, src, dst, n, shift, offset)                \
370
384k
    n /= channels;                                                             \
371
871k
    for (c = 0; c < avctx->ch_layout.nb_channels; c++) {                       \
372
487k
        int i;                                                                 \
373
487k
        dst = frame->extended_data[c];                                         \
374
18.5M
        for (i = n; i > 0; i--) {                                              \
375
18.0M
            uint ## size ## _t v = bytestream_get_ ## endian(&src);            \
376
18.0M
            AV_WN ## size ## A(dst, (uint ## size ##_t)(v - offset) << shift); \
377
18.0M
            dst += size / 8;                                                   \
378
18.0M
        }                                                                      \
379
487k
    }
380
381
static int pcm_decode_frame(AVCodecContext *avctx, AVFrame *frame,
382
            int *got_frame_ptr, AVPacket *avpkt)
383
14.9M
{
384
14.9M
    const uint8_t *src = avpkt->data;
385
14.9M
    int buf_size       = avpkt->size;
386
14.9M
    PCMDecode *s       = avctx->priv_data;
387
14.9M
    int channels       = avctx->ch_layout.nb_channels;
388
14.9M
    int sample_size    = s->sample_size;
389
14.9M
    int c, n, ret, samples_per_block;
390
14.9M
    uint8_t *samples;
391
14.9M
    int32_t *dst_int32_t;
392
393
14.9M
    samples_per_block = 1;
394
14.9M
    if (avctx->codec_id == AV_CODEC_ID_PCM_LXF) {
395
        /* we process 40-bit blocks per channel for LXF */
396
474k
        samples_per_block = 2;
397
474k
    }
398
399
14.9M
    if (channels == 0) {
400
0
        av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
401
0
        return AVERROR(EINVAL);
402
0
    }
403
404
14.9M
    if (avctx->codec_id != avctx->codec->id) {
405
0
        av_log(avctx, AV_LOG_ERROR, "codec ids mismatch\n");
406
0
        return AVERROR(EINVAL);
407
0
    }
408
409
14.9M
    n = channels * sample_size;
410
411
14.9M
    if (n && buf_size % n) {
412
9.90M
        if (buf_size < n) {
413
8.26M
            av_log(avctx, AV_LOG_ERROR,
414
8.26M
                   "Invalid PCM packet, data has size %d but at least a size of %d was expected\n",
415
8.26M
                   buf_size, n);
416
8.26M
            return AVERROR_INVALIDDATA;
417
8.26M
        } else
418
1.63M
            buf_size -= buf_size % n;
419
9.90M
    }
420
421
6.64M
    n = buf_size / sample_size;
422
423
    /* get output buffer */
424
6.64M
    frame->nb_samples = n * samples_per_block / channels;
425
6.64M
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
426
0
        return ret;
427
6.64M
    samples = frame->data[0];
428
429
6.64M
    switch (avctx->codec_id) {
430
180k
    case AV_CODEC_ID_PCM_U32LE:
431
180k
        DECODE(32, le32, src, samples, n, 0, 0x80000000)
432
180k
        break;
433
189k
    case AV_CODEC_ID_PCM_U32BE:
434
189k
        DECODE(32, be32, src, samples, n, 0, 0x80000000)
435
189k
        break;
436
193k
    case AV_CODEC_ID_PCM_S24LE:
437
193k
        DECODE(32, le24, src, samples, n, 8, 0)
438
193k
        break;
439
200k
    case AV_CODEC_ID_PCM_S24LE_PLANAR:
440
200k
        DECODE_PLANAR(32, le24, src, samples, n, 8, 0);
441
200k
        break;
442
193k
    case AV_CODEC_ID_PCM_S24BE:
443
193k
        DECODE(32, be24, src, samples, n, 8, 0)
444
193k
        break;
445
157k
    case AV_CODEC_ID_PCM_U24LE:
446
157k
        DECODE(32, le24, src, samples, n, 8, 0x800000)
447
157k
        break;
448
157k
    case AV_CODEC_ID_PCM_U24BE:
449
157k
        DECODE(32, be24, src, samples, n, 8, 0x800000)
450
157k
        break;
451
221k
    case AV_CODEC_ID_PCM_S24DAUD:
452
9.02M
        for (; n > 0; n--) {
453
8.80M
            uint32_t v = bytestream_get_be24(&src);
454
8.80M
            v >>= 4; // sync flags are here
455
8.80M
            AV_WN16A(samples, ff_reverse[(v >> 8) & 0xff] +
456
8.80M
                             (ff_reverse[v        & 0xff] << 8));
457
8.80M
            samples += 2;
458
8.80M
        }
459
221k
        break;
460
131k
    case AV_CODEC_ID_PCM_U16LE:
461
131k
        DECODE(16, le16, src, samples, n, 0, 0x8000)
462
131k
        break;
463
173k
    case AV_CODEC_ID_PCM_U16BE:
464
173k
        DECODE(16, be16, src, samples, n, 0, 0x8000)
465
173k
        break;
466
209k
    case AV_CODEC_ID_PCM_S8:
467
17.2M
        for (; n > 0; n--)
468
17.0M
            *samples++ = *src++ + 128;
469
209k
        break;
470
231k
    case AV_CODEC_ID_PCM_SGA:
471
16.9M
        for (; n > 0; n--) {
472
16.6M
            int sign = *src >> 7;
473
16.6M
            int magn = *src & 0x7f;
474
16.6M
            *samples++ = sign ? 128 - magn : 128 + magn;
475
16.6M
            src++;
476
16.6M
        }
477
231k
        break;
478
247k
    case AV_CODEC_ID_PCM_S8_PLANAR:
479
247k
        n /= avctx->ch_layout.nb_channels;
480
642k
        for (c = 0; c < avctx->ch_layout.nb_channels; c++) {
481
394k
            int i;
482
394k
            samples = frame->extended_data[c];
483
19.3M
            for (i = n; i > 0; i--)
484
18.9M
                *samples++ = *src++ + 128;
485
394k
        }
486
247k
        break;
487
#if HAVE_BIGENDIAN
488
    case AV_CODEC_ID_PCM_S64LE:
489
    case AV_CODEC_ID_PCM_F64LE:
490
        DECODE(64, le64, src, samples, n, 0, 0)
491
        break;
492
    case AV_CODEC_ID_PCM_S32LE:
493
    case AV_CODEC_ID_PCM_F32LE:
494
    case AV_CODEC_ID_PCM_F24LE:
495
    case AV_CODEC_ID_PCM_F16LE:
496
        DECODE(32, le32, src, samples, n, 0, 0)
497
        break;
498
    case AV_CODEC_ID_PCM_S32LE_PLANAR:
499
        DECODE_PLANAR(32, le32, src, samples, n, 0, 0);
500
        break;
501
    case AV_CODEC_ID_PCM_S16LE:
502
        DECODE(16, le16, src, samples, n, 0, 0)
503
        break;
504
    case AV_CODEC_ID_PCM_S16LE_PLANAR:
505
        DECODE_PLANAR(16, le16, src, samples, n, 0, 0);
506
        break;
507
    case AV_CODEC_ID_PCM_F64BE:
508
    case AV_CODEC_ID_PCM_F32BE:
509
    case AV_CODEC_ID_PCM_S64BE:
510
    case AV_CODEC_ID_PCM_S32BE:
511
    case AV_CODEC_ID_PCM_S16BE:
512
#else
513
193k
    case AV_CODEC_ID_PCM_S64BE:
514
443k
    case AV_CODEC_ID_PCM_F64BE:
515
443k
        DECODE(64, be64, src, samples, n, 0, 0)
516
443k
        break;
517
176k
    case AV_CODEC_ID_PCM_F32BE:
518
354k
    case AV_CODEC_ID_PCM_S32BE:
519
354k
        DECODE(32, be32, src, samples, n, 0, 0)
520
354k
        break;
521
187k
    case AV_CODEC_ID_PCM_S16BE:
522
187k
        DECODE(16, be16, src, samples, n, 0, 0)
523
187k
        break;
524
183k
    case AV_CODEC_ID_PCM_S16BE_PLANAR:
525
183k
        DECODE_PLANAR(16, be16, src, samples, n, 0, 0);
526
183k
        break;
527
200k
    case AV_CODEC_ID_PCM_F64LE:
528
382k
    case AV_CODEC_ID_PCM_F32LE:
529
571k
    case AV_CODEC_ID_PCM_F24LE:
530
790k
    case AV_CODEC_ID_PCM_F16LE:
531
991k
    case AV_CODEC_ID_PCM_S64LE:
532
1.19M
    case AV_CODEC_ID_PCM_S32LE:
533
1.41M
    case AV_CODEC_ID_PCM_S16LE:
534
1.41M
#endif /* HAVE_BIGENDIAN */
535
1.65M
    case AV_CODEC_ID_PCM_U8:
536
1.65M
        memcpy(samples, src, n * sample_size);
537
1.65M
        break;
538
#if HAVE_BIGENDIAN
539
    case AV_CODEC_ID_PCM_S16BE_PLANAR:
540
#else
541
234k
    case AV_CODEC_ID_PCM_S16LE_PLANAR:
542
400k
    case AV_CODEC_ID_PCM_S32LE_PLANAR:
543
400k
#endif /* HAVE_BIGENDIAN */
544
400k
        n /= avctx->ch_layout.nb_channels;
545
883k
        for (c = 0; c < avctx->ch_layout.nb_channels; c++) {
546
482k
            samples = frame->extended_data[c];
547
482k
            bytestream_get_buffer(&src, samples, n * sample_size);
548
482k
        }
549
400k
        break;
550
241k
    case AV_CODEC_ID_PCM_ALAW:
551
478k
    case AV_CODEC_ID_PCM_MULAW:
552
725k
    case AV_CODEC_ID_PCM_VIDC: {
553
725k
        const int16_t *const lut = ((PCMLUTDecode*)avctx->priv_data)->table;
554
725k
        int16_t *restrict samples_16 = (int16_t*)samples;
555
556
54.7M
        for (; n > 0; n--)
557
54.0M
            *samples_16++ = lut[*src++];
558
725k
        break;
559
478k
    }
560
205k
    case AV_CODEC_ID_PCM_LXF:
561
205k
    {
562
205k
        int i;
563
205k
        n /= channels;
564
448k
        for (c = 0; c < channels; c++) {
565
242k
            dst_int32_t = (int32_t *)frame->extended_data[c];
566
4.30M
            for (i = 0; i < n; i++) {
567
                // extract low 20 bits and expand to 32 bits
568
4.06M
                *dst_int32_t++ =  ((uint32_t)src[2]<<28) |
569
4.06M
                                  (src[1]         << 20) |
570
4.06M
                                  (src[0]         << 12) |
571
4.06M
                                 ((src[2] & 0x0F) <<  8) |
572
4.06M
                                   src[1];
573
                // extract high 20 bits and expand to 32 bits
574
4.06M
                *dst_int32_t++ =  ((uint32_t)src[4]<<24) |
575
4.06M
                                  (src[3]         << 16) |
576
4.06M
                                 ((src[2] & 0xF0) <<  8) |
577
4.06M
                                  (src[4]         <<  4) |
578
4.06M
                                  (src[3]         >>  4);
579
4.06M
                src += 5;
580
4.06M
            }
581
242k
        }
582
205k
        break;
583
478k
    }
584
0
    default:
585
0
        return -1;
586
6.64M
    }
587
588
6.64M
    if (avctx->codec_id == AV_CODEC_ID_PCM_F16LE ||
589
6.64M
        avctx->codec_id == AV_CODEC_ID_PCM_F24LE) {
590
407k
        PCMScaleDecode *s2 = avctx->priv_data;
591
407k
        s2->vector_fmul_scalar((float *)frame->extended_data[0],
592
407k
                               (const float *)frame->extended_data[0],
593
407k
                               s2->scale, FFALIGN(frame->nb_samples * avctx->ch_layout.nb_channels, 4));
594
407k
    }
595
596
6.64M
    *got_frame_ptr = 1;
597
598
6.64M
    return buf_size;
599
6.64M
}
600
601
#define PCM_ENCODER_0(id_, sample_fmt_, name_, long_name_)
602
#define PCM_ENCODER_1(id_, sample_fmt_, name_, long_name_)                  \
603
const FFCodec ff_ ## name_ ## _encoder = {                                  \
604
    .p.name       = #name_,                                                 \
605
    CODEC_LONG_NAME(long_name_),                                            \
606
    .p.type       = AVMEDIA_TYPE_AUDIO,                                     \
607
    .p.id         = id_,                                                    \
608
    .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_VARIABLE_FRAME_SIZE | \
609
                      AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,                \
610
    .init         = pcm_encode_init,                                        \
611
    FF_CODEC_ENCODE_CB(pcm_encode_frame),                                   \
612
    CODEC_SAMPLEFMTS(sample_fmt_),                                          \
613
}
614
615
#define PCM_ENCODER_2(cf, id, sample_fmt, name, long_name)                  \
616
    PCM_ENCODER_ ## cf(id, sample_fmt, name, long_name)
617
#define PCM_ENCODER_3(cf, id, sample_fmt, name, long_name)                  \
618
    PCM_ENCODER_2(cf, id, sample_fmt, name, long_name)
619
#define PCM_ENCODER(id, sample_fmt, name, long_name)                        \
620
    PCM_ENCODER_3(CONFIG_PCM_ ## id ## _ENCODER, AV_CODEC_ID_PCM_ ## id,    \
621
                  AV_SAMPLE_FMT_ ## sample_fmt, pcm_ ## name, long_name)
622
623
#define PCM_DECODER_0(id, sample_fmt, name, long_name, Context, init_func)
624
#define PCM_DECODER_1(id_, sample_fmt, name_, long_name, Context, init_func)\
625
const FFCodec ff_ ## name_ ## _decoder = {                                  \
626
    .p.name         = #name_,                                               \
627
    CODEC_LONG_NAME(long_name),                                             \
628
    .p.type         = AVMEDIA_TYPE_AUDIO,                                   \
629
    .p.id           = id_,                                                  \
630
    .priv_data_size = sizeof(Context),                                      \
631
    .init           = init_func,                                            \
632
    FF_CODEC_DECODE_CB(pcm_decode_frame),                                    \
633
    .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_PARAM_CHANGE,         \
634
}
635
636
#define PCM_DECODER_2(cf, id, sample_fmt, name, long_name, Context, init_func) \
637
    PCM_DECODER_ ## cf(id, sample_fmt, name, long_name, Context, init_func)
638
#define PCM_DECODER_3(cf, id, sample_fmt, name, long_name, Context, init_func) \
639
    PCM_DECODER_2(cf, id, sample_fmt, name, long_name, Context, init_func)
640
#define PCM_DEC_EXT(id, sample_fmt, name, long_name, Context, init_func)    \
641
    PCM_DECODER_3(CONFIG_PCM_ ## id ## _DECODER, AV_CODEC_ID_PCM_ ## id,    \
642
                  AV_SAMPLE_FMT_ ## sample_fmt, pcm_ ## name, long_name,    \
643
                  Context, init_func)
644
645
#define PCM_DECODER(id, sample_fmt, name, long_name)    \
646
    PCM_DEC_EXT(id, sample_fmt, name, long_name, PCMDecode, pcm_decode_init)
647
648
#define PCM_CODEC(id, sample_fmt_, name, long_name_)                    \
649
    PCM_ENCODER(id, sample_fmt_, name, long_name_);                     \
650
    PCM_DECODER(id, sample_fmt_, name, long_name_)
651
652
#define PCM_CODEC_EXT(id, sample_fmt, name, long_name, DecContext, dec_init_func) \
653
    PCM_DEC_EXT(id, sample_fmt, name, long_name, DecContext, dec_init_func);      \
654
    PCM_ENCODER(id, sample_fmt, name, long_name)
655
656
/* Note: Do not forget to add new entries to the Makefile and
657
 *       to the table in pcm_decode_init() as well. */
658
//            AV_CODEC_ID_*      pcm_* name
659
//                          AV_SAMPLE_FMT_*    long name                                DecodeContext   decode init func
660
PCM_CODEC_EXT(ALAW,         S16, alaw,         "PCM A-law / G.711 A-law",               PCMLUTDecode,   pcm_lut_decode_init);
661
PCM_DEC_EXT  (F16LE,        FLT, f16le,        "PCM 16.8 floating point little-endian", PCMScaleDecode, pcm_scale_decode_init);
662
PCM_DEC_EXT  (F24LE,        FLT, f24le,        "PCM 24.0 floating point little-endian", PCMScaleDecode, pcm_scale_decode_init);
663
PCM_CODEC    (F32BE,        FLT, f32be,        "PCM 32-bit floating point big-endian");
664
PCM_CODEC    (F32LE,        FLT, f32le,        "PCM 32-bit floating point little-endian");
665
PCM_CODEC    (F64BE,        DBL, f64be,        "PCM 64-bit floating point big-endian");
666
PCM_CODEC    (F64LE,        DBL, f64le,        "PCM 64-bit floating point little-endian");
667
PCM_DECODER  (LXF,          S32P,lxf,          "PCM signed 20-bit little-endian planar");
668
PCM_CODEC_EXT(MULAW,        S16, mulaw,        "PCM mu-law / G.711 mu-law",             PCMLUTDecode,   pcm_lut_decode_init);
669
PCM_CODEC    (S8,           U8,  s8,           "PCM signed 8-bit");
670
PCM_CODEC    (S8_PLANAR,    U8P, s8_planar,    "PCM signed 8-bit planar");
671
PCM_CODEC    (S16BE,        S16, s16be,        "PCM signed 16-bit big-endian");
672
PCM_CODEC    (S16BE_PLANAR, S16P,s16be_planar, "PCM signed 16-bit big-endian planar");
673
PCM_CODEC    (S16LE,        S16, s16le,        "PCM signed 16-bit little-endian");
674
PCM_CODEC    (S16LE_PLANAR, S16P,s16le_planar, "PCM signed 16-bit little-endian planar");
675
PCM_CODEC    (S24BE,        S32, s24be,        "PCM signed 24-bit big-endian");
676
PCM_CODEC    (S24DAUD,      S16, s24daud,      "PCM D-Cinema audio signed 24-bit");
677
PCM_CODEC    (S24LE,        S32, s24le,        "PCM signed 24-bit little-endian");
678
PCM_CODEC    (S24LE_PLANAR, S32P,s24le_planar, "PCM signed 24-bit little-endian planar");
679
PCM_CODEC    (S32BE,        S32, s32be,        "PCM signed 32-bit big-endian");
680
PCM_CODEC    (S32LE,        S32, s32le,        "PCM signed 32-bit little-endian");
681
PCM_CODEC    (S32LE_PLANAR, S32P,s32le_planar, "PCM signed 32-bit little-endian planar");
682
PCM_CODEC    (U8,           U8,  u8,           "PCM unsigned 8-bit");
683
PCM_CODEC    (U16BE,        S16, u16be,        "PCM unsigned 16-bit big-endian");
684
PCM_CODEC    (U16LE,        S16, u16le,        "PCM unsigned 16-bit little-endian");
685
PCM_CODEC    (U24BE,        S32, u24be,        "PCM unsigned 24-bit big-endian");
686
PCM_CODEC    (U24LE,        S32, u24le,        "PCM unsigned 24-bit little-endian");
687
PCM_CODEC    (U32BE,        S32, u32be,        "PCM unsigned 32-bit big-endian");
688
PCM_CODEC    (U32LE,        S32, u32le,        "PCM unsigned 32-bit little-endian");
689
PCM_CODEC    (S64BE,        S64, s64be,        "PCM signed 64-bit big-endian");
690
PCM_CODEC    (S64LE,        S64, s64le,        "PCM signed 64-bit little-endian");
691
PCM_CODEC_EXT(VIDC,         S16, vidc,         "PCM Archimedes VIDC",                   PCMLUTDecode,   pcm_lut_decode_init);
692
PCM_DECODER  (SGA,          U8,  sga,          "PCM SGA");