Statistics
| Branch: | Revision:

ffmpeg / libavcodec / pcm.c @ 63613fe6

History | View | Annotate | Download (18.5 KB)

1 a96b68b7 Fabrice Bellard
/*
2
 * PCM codecs
3 406792e7 Diego Biurrun
 * Copyright (c) 2001 Fabrice Bellard
4 a96b68b7 Fabrice Bellard
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 ff4ec49e Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 a96b68b7 Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 a96b68b7 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ff4ec49e Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 a96b68b7 Fabrice Bellard
 *
17 ff4ec49e Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 a96b68b7 Fabrice Bellard
 */
21 115329f1 Diego Biurrun
22 1ab3d669 Michael Niedermayer
/**
23 bad5537e Diego Biurrun
 * @file libavcodec/pcm.c
24 1ab3d669 Michael Niedermayer
 * PCM codecs
25
 */
26 115329f1 Diego Biurrun
27 a96b68b7 Fabrice Bellard
#include "avcodec.h"
28 9106a698 Stefano Sabatini
#include "get_bits.h" // for ff_reverse
29 5a2f421a Ramiro Polla
#include "bytestream.h"
30 a96b68b7 Fabrice Bellard
31 725d86bf Aurelien Jacobs
#define MAX_CHANNELS 64
32
33 a96b68b7 Fabrice Bellard
/* from g711.c by SUN microsystems (unrestricted use) */
34
35 bb270c08 Diego Biurrun
#define         SIGN_BIT        (0x80)      /* Sign bit for a A-law byte. */
36
#define         QUANT_MASK      (0xf)       /* Quantization field mask. */
37
#define         NSEGS           (8)         /* Number of A-law segments. */
38
#define         SEG_SHIFT       (4)         /* Left shift for segment number. */
39
#define         SEG_MASK        (0x70)      /* Segment field mask. */
40 a96b68b7 Fabrice Bellard
41 bb270c08 Diego Biurrun
#define         BIAS            (0x84)      /* Bias for linear code. */
42 a96b68b7 Fabrice Bellard
43
/*
44
 * alaw2linear() - Convert an A-law value to 16-bit linear PCM
45
 *
46
 */
47 98a6fff9 Zuxy Meng
static av_cold int alaw2linear(unsigned char a_val)
48 a96b68b7 Fabrice Bellard
{
49 bb270c08 Diego Biurrun
        int t;
50
        int seg;
51 a96b68b7 Fabrice Bellard
52 bb270c08 Diego Biurrun
        a_val ^= 0x55;
53 a96b68b7 Fabrice Bellard
54 bb270c08 Diego Biurrun
        t = a_val & QUANT_MASK;
55
        seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
56
        if(seg) t= (t + t + 1 + 32) << (seg + 2);
57
        else    t= (t + t + 1     ) << 3;
58 cd1f22f9 Michael Niedermayer
59 ccd425e7 Diego Biurrun
        return (a_val & SIGN_BIT) ? t : -t;
60 a96b68b7 Fabrice Bellard
}
61
62 98a6fff9 Zuxy Meng
static av_cold int ulaw2linear(unsigned char u_val)
63 a96b68b7 Fabrice Bellard
{
64 bb270c08 Diego Biurrun
        int t;
65 a96b68b7 Fabrice Bellard
66 bb270c08 Diego Biurrun
        /* Complement to obtain normal u-law value. */
67
        u_val = ~u_val;
68 a96b68b7 Fabrice Bellard
69 bb270c08 Diego Biurrun
        /*
70
         * Extract and bias the quantization bits. Then
71
         * shift up by the segment number and subtract out the bias.
72
         */
73
        t = ((u_val & QUANT_MASK) << 3) + BIAS;
74
        t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
75 a96b68b7 Fabrice Bellard
76 ccd425e7 Diego Biurrun
        return (u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS);
77 a96b68b7 Fabrice Bellard
}
78
79
/* 16384 entries per table */
80 e2e712e7 Michael Niedermayer
static uint8_t linear_to_alaw[16384];
81
static uint8_t linear_to_ulaw[16384];
82 a96b68b7 Fabrice Bellard
83 98a6fff9 Zuxy Meng
static av_cold void build_xlaw_table(uint8_t *linear_to_xlaw,
84 a96b68b7 Fabrice Bellard
                             int (*xlaw2linear)(unsigned char),
85 115329f1 Diego Biurrun
                             int mask)
86 a96b68b7 Fabrice Bellard
{
87
    int i, j, v, v1, v2;
88
89
    j = 0;
90
    for(i=0;i<128;i++) {
91
        if (i != 127) {
92
            v1 = xlaw2linear(i ^ mask);
93
            v2 = xlaw2linear((i + 1) ^ mask);
94
            v = (v1 + v2 + 4) >> 3;
95
        } else {
96
            v = 8192;
97
        }
98
        for(;j<v;j++) {
99
            linear_to_xlaw[8192 + j] = (i ^ mask);
100
            if (j > 0)
101
                linear_to_xlaw[8192 - j] = (i ^ (mask ^ 0x80));
102
        }
103
    }
104
    linear_to_xlaw[0] = linear_to_xlaw[1];
105
}
106
107 98a6fff9 Zuxy Meng
static av_cold int pcm_encode_init(AVCodecContext *avctx)
108 a96b68b7 Fabrice Bellard
{
109
    avctx->frame_size = 1;
110
    switch(avctx->codec->id) {
111
    case CODEC_ID_PCM_ALAW:
112 e2e712e7 Michael Niedermayer
        build_xlaw_table(linear_to_alaw, alaw2linear, 0xd5);
113 a96b68b7 Fabrice Bellard
        break;
114
    case CODEC_ID_PCM_MULAW:
115 e2e712e7 Michael Niedermayer
        build_xlaw_table(linear_to_ulaw, ulaw2linear, 0xff);
116 a96b68b7 Fabrice Bellard
        break;
117
    default:
118
        break;
119
    }
120 115329f1 Diego Biurrun
121 dd1c8f3e Luca Abeni
    avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
122
    avctx->block_align = avctx->channels * avctx->bits_per_coded_sample/8;
123 492cd3a9 Michael Niedermayer
    avctx->coded_frame= avcodec_alloc_frame();
124
    avctx->coded_frame->key_frame= 1;
125 115329f1 Diego Biurrun
126 a96b68b7 Fabrice Bellard
    return 0;
127
}
128
129 98a6fff9 Zuxy Meng
static av_cold int pcm_encode_close(AVCodecContext *avctx)
130 a96b68b7 Fabrice Bellard
{
131 492cd3a9 Michael Niedermayer
    av_freep(&avctx->coded_frame);
132
133 a96b68b7 Fabrice Bellard
    return 0;
134
}
135
136 b461b3bc Reimar Döffinger
/**
137 3ee573a3 Peter Ross
 * Write PCM samples macro
138
 * @param type Datatype of native machine format
139
 * @param endian bytestream_put_xxx() suffix
140
 * @param src Source pointer (variable name)
141
 * @param dst Destination pointer (variable name)
142
 * @param n Total number of samples (variable name)
143 5e8ecfde Peter Ross
 * @param shift Bitshift (bits)
144 3ee573a3 Peter Ross
 * @param offset Sample value offset
145
 */
146 5e8ecfde Peter Ross
#define ENCODE(type, endian, src, dst, n, shift, offset) \
147
    samples_##type = (type*)src; \
148 3ee573a3 Peter Ross
    for(;n>0;n--) { \
149 5e8ecfde Peter Ross
        register type v = (*samples_##type++ >> shift) + offset; \
150 3ee573a3 Peter Ross
        bytestream_put_##endian(&dst, v); \
151 5e8ecfde Peter Ross
    }
152 3ee573a3 Peter Ross
153 cd4af68a Zdenek Kabelac
static int pcm_encode_frame(AVCodecContext *avctx,
154 bb270c08 Diego Biurrun
                            unsigned char *frame, int buf_size, void *data)
155 a96b68b7 Fabrice Bellard
{
156
    int n, sample_size, v;
157
    short *samples;
158
    unsigned char *dst;
159 5e8ecfde Peter Ross
    uint8_t *srcu8;
160
    int16_t *samples_int16_t;
161
    int32_t *samples_int32_t;
162 143a5d6f Peter Ross
    int64_t *samples_int64_t;
163 5e8ecfde Peter Ross
    uint16_t *samples_uint16_t;
164
    uint32_t *samples_uint32_t;
165 a96b68b7 Fabrice Bellard
166 cc0b6129 Peter Ross
    sample_size = av_get_bits_per_sample(avctx->codec->id)/8;
167 a96b68b7 Fabrice Bellard
    n = buf_size / sample_size;
168
    samples = data;
169
    dst = frame;
170
171 5e8ecfde Peter Ross
    if (avctx->sample_fmt!=avctx->codec->sample_fmts[0]) {
172
        av_log(avctx, AV_LOG_ERROR, "invalid sample_fmt\n");
173
        return -1;
174
    }
175
176 a96b68b7 Fabrice Bellard
    switch(avctx->codec->id) {
177 b461b3bc Reimar Döffinger
    case CODEC_ID_PCM_U32LE:
178 5e8ecfde Peter Ross
        ENCODE(uint32_t, le32, samples, dst, n, 0, 0x80000000)
179 b461b3bc Reimar Döffinger
        break;
180
    case CODEC_ID_PCM_U32BE:
181 5e8ecfde Peter Ross
        ENCODE(uint32_t, be32, samples, dst, n, 0, 0x80000000)
182 b461b3bc Reimar Döffinger
        break;
183
    case CODEC_ID_PCM_S24LE:
184 5e8ecfde Peter Ross
        ENCODE(int32_t, le24, samples, dst, n, 8, 0)
185 b461b3bc Reimar Döffinger
        break;
186
    case CODEC_ID_PCM_S24BE:
187 5e8ecfde Peter Ross
        ENCODE(int32_t, be24, samples, dst, n, 8, 0)
188 b461b3bc Reimar Döffinger
        break;
189
    case CODEC_ID_PCM_U24LE:
190 5e8ecfde Peter Ross
        ENCODE(uint32_t, le24, samples, dst, n, 8, 0x800000)
191 b461b3bc Reimar Döffinger
        break;
192
    case CODEC_ID_PCM_U24BE:
193 5e8ecfde Peter Ross
        ENCODE(uint32_t, be24, samples, dst, n, 8, 0x800000)
194 b461b3bc Reimar Döffinger
        break;
195
    case CODEC_ID_PCM_S24DAUD:
196
        for(;n>0;n--) {
197 13dbd9b7 Peter Ross
            uint32_t tmp = ff_reverse[(*samples >> 8) & 0xff] +
198 b461b3bc Reimar Döffinger
                           (ff_reverse[*samples & 0xff] << 8);
199
            tmp <<= 4; // sync flags would go here
200 5a2f421a Ramiro Polla
            bytestream_put_be24(&dst, tmp);
201 b461b3bc Reimar Döffinger
            samples++;
202
        }
203
        break;
204 a96b68b7 Fabrice Bellard
    case CODEC_ID_PCM_U16LE:
205 5e8ecfde Peter Ross
        ENCODE(uint16_t, le16, samples, dst, n, 0, 0x8000)
206 a96b68b7 Fabrice Bellard
        break;
207
    case CODEC_ID_PCM_U16BE:
208 5e8ecfde Peter Ross
        ENCODE(uint16_t, be16, samples, dst, n, 0, 0x8000)
209 a96b68b7 Fabrice Bellard
        break;
210
    case CODEC_ID_PCM_S8:
211 5e8ecfde Peter Ross
        srcu8= data;
212 a96b68b7 Fabrice Bellard
        for(;n>0;n--) {
213 5e8ecfde Peter Ross
            v = *srcu8++;
214
            *dst++ = v - 128;
215 a96b68b7 Fabrice Bellard
        }
216
        break;
217 63613fe6 Måns Rullgård
#if HAVE_BIGENDIAN
218 143a5d6f Peter Ross
    case CODEC_ID_PCM_F64LE:
219
        ENCODE(int64_t, le64, samples, dst, n, 0, 0)
220
        break;
221 38a1c7f2 Peter Ross
    case CODEC_ID_PCM_S32LE:
222 143a5d6f Peter Ross
    case CODEC_ID_PCM_F32LE:
223 38a1c7f2 Peter Ross
        ENCODE(int32_t, le32, samples, dst, n, 0, 0)
224
        break;
225
    case CODEC_ID_PCM_S16LE:
226
        ENCODE(int16_t, le16, samples, dst, n, 0, 0)
227
        break;
228 143a5d6f Peter Ross
    case CODEC_ID_PCM_F64BE:
229 38a1c7f2 Peter Ross
    case CODEC_ID_PCM_F32BE:
230
    case CODEC_ID_PCM_S32BE:
231
    case CODEC_ID_PCM_S16BE:
232
#else
233 143a5d6f Peter Ross
    case CODEC_ID_PCM_F64BE:
234
        ENCODE(int64_t, be64, samples, dst, n, 0, 0)
235
        break;
236 38a1c7f2 Peter Ross
    case CODEC_ID_PCM_F32BE:
237
    case CODEC_ID_PCM_S32BE:
238
        ENCODE(int32_t, be32, samples, dst, n, 0, 0)
239
        break;
240
    case CODEC_ID_PCM_S16BE:
241
        ENCODE(int16_t, be16, samples, dst, n, 0, 0)
242
        break;
243 143a5d6f Peter Ross
    case CODEC_ID_PCM_F64LE:
244
    case CODEC_ID_PCM_F32LE:
245 38a1c7f2 Peter Ross
    case CODEC_ID_PCM_S32LE:
246
    case CODEC_ID_PCM_S16LE:
247 63613fe6 Måns Rullgård
#endif /* HAVE_BIGENDIAN */
248 a96b68b7 Fabrice Bellard
    case CODEC_ID_PCM_U8:
249 38a1c7f2 Peter Ross
        memcpy(dst, samples, n*sample_size);
250
        dst += n*sample_size;
251 a96b68b7 Fabrice Bellard
        break;
252 a11c2a2c Michael Niedermayer
    case CODEC_ID_PCM_ZORK:
253
        for(;n>0;n--) {
254
            v= *samples++ >> 8;
255
            if(v<0)   v = -v;
256
            else      v+= 128;
257
            *dst++ = v;
258
        }
259
        break;
260 a96b68b7 Fabrice Bellard
    case CODEC_ID_PCM_ALAW:
261
        for(;n>0;n--) {
262
            v = *samples++;
263 a4461664 Ramiro Polla
            *dst++ = linear_to_alaw[(v + 32768) >> 2];
264 a96b68b7 Fabrice Bellard
        }
265
        break;
266
    case CODEC_ID_PCM_MULAW:
267
        for(;n>0;n--) {
268
            v = *samples++;
269 a4461664 Ramiro Polla
            *dst++ = linear_to_ulaw[(v + 32768) >> 2];
270 a96b68b7 Fabrice Bellard
        }
271
        break;
272
    default:
273
        return -1;
274
    }
275 13a0314f Philip Gladstone
    //avctx->frame_size = (dst - frame) / (sample_size * avctx->channels);
276 4c3d2e5f Philip Gladstone
277 a96b68b7 Fabrice Bellard
    return dst - frame;
278
}
279
280
typedef struct PCMDecode {
281
    short table[256];
282
} PCMDecode;
283
284 98a6fff9 Zuxy Meng
static av_cold int pcm_decode_init(AVCodecContext * avctx)
285 a96b68b7 Fabrice Bellard
{
286
    PCMDecode *s = avctx->priv_data;
287
    int i;
288
289
    switch(avctx->codec->id) {
290
    case CODEC_ID_PCM_ALAW:
291
        for(i=0;i<256;i++)
292
            s->table[i] = alaw2linear(i);
293
        break;
294
    case CODEC_ID_PCM_MULAW:
295
        for(i=0;i<256;i++)
296
            s->table[i] = ulaw2linear(i);
297
        break;
298
    default:
299
        break;
300
    }
301 aa29709e Peter Ross
302 0fd7e57e Peter Ross
    avctx->sample_fmt = avctx->codec->sample_fmts[0];
303 a96b68b7 Fabrice Bellard
    return 0;
304
}
305
306 b461b3bc Reimar Döffinger
/**
307 3ee573a3 Peter Ross
 * Read PCM samples macro
308
 * @param type Datatype of native machine format
309
 * @param endian bytestream_get_xxx() endian suffix
310
 * @param src Source pointer (variable name)
311
 * @param dst Destination pointer (variable name)
312
 * @param n Total number of samples (variable name)
313 5e8ecfde Peter Ross
 * @param shift Bitshift (bits)
314 3ee573a3 Peter Ross
 * @param offset Sample value offset
315
 */
316 5e8ecfde Peter Ross
#define DECODE(type, endian, src, dst, n, shift, offset) \
317
    dst_##type = (type*)dst; \
318 3ee573a3 Peter Ross
    for(;n>0;n--) { \
319
        register type v = bytestream_get_##endian(&src); \
320 5e8ecfde Peter Ross
        *dst_##type++ = (v - offset) << shift; \
321 3ee573a3 Peter Ross
    } \
322 5e8ecfde Peter Ross
    dst = (short*)dst_##type;
323 3ee573a3 Peter Ross
324 cd4af68a Zdenek Kabelac
static int pcm_decode_frame(AVCodecContext *avctx,
325 bb270c08 Diego Biurrun
                            void *data, int *data_size,
326 7a00bbad Thilo Borgmann
                            AVPacket *avpkt)
327 a96b68b7 Fabrice Bellard
{
328 7a00bbad Thilo Borgmann
    const uint8_t *buf = avpkt->data;
329
    int buf_size = avpkt->size;
330 a96b68b7 Fabrice Bellard
    PCMDecode *s = avctx->priv_data;
331 ff66caab Peter Ross
    int sample_size, c, n;
332 a96b68b7 Fabrice Bellard
    short *samples;
333 0cdc6ec9 Peter Ross
    const uint8_t *src, *src8, *src2[MAX_CHANNELS];
334 5e8ecfde Peter Ross
    uint8_t *dstu8;
335
    int16_t *dst_int16_t;
336
    int32_t *dst_int32_t;
337 143a5d6f Peter Ross
    int64_t *dst_int64_t;
338 5e8ecfde Peter Ross
    uint16_t *dst_uint16_t;
339
    uint32_t *dst_uint32_t;
340 a96b68b7 Fabrice Bellard
341
    samples = data;
342
    src = buf;
343
344 5e8ecfde Peter Ross
    if (avctx->sample_fmt!=avctx->codec->sample_fmts[0]) {
345
        av_log(avctx, AV_LOG_ERROR, "invalid sample_fmt\n");
346
        return -1;
347
    }
348
349 3426d575 wg
    if(avctx->channels <= 0 || avctx->channels > MAX_CHANNELS){
350
        av_log(avctx, AV_LOG_ERROR, "PCM channels out of bounds\n");
351
        return -1;
352
    }
353 b2a581dc Michael Niedermayer
354 ff66caab Peter Ross
    sample_size = av_get_bits_per_sample(avctx->codec_id)/8;
355
356 1472b7dd Lars Täuber
    /* av_get_bits_per_sample returns 0 for CODEC_ID_PCM_DVD */
357
    if (CODEC_ID_PCM_DVD == avctx->codec_id)
358
        /* 2 samples are interleaved per block in PCM_DVD */
359 dd1c8f3e Luca Abeni
        sample_size = avctx->bits_per_coded_sample * 2 / 8;
360 2cd04cf9 Peter Ross
361
    n = avctx->channels * sample_size;
362 1472b7dd Lars Täuber
363
    if(n && buf_size % n){
364 012e4f5a Michael Niedermayer
        if (buf_size < n) {
365 34365443 Michael Niedermayer
            av_log(avctx, AV_LOG_ERROR, "invalid PCM packet\n");
366
            return -1;
367 012e4f5a Michael Niedermayer
        }else
368
            buf_size -= buf_size % n;
369 1472b7dd Lars Täuber
    }
370
371 ac66834c Michael Niedermayer
    buf_size= FFMIN(buf_size, *data_size/2);
372
    *data_size=0;
373 a8d02f2b Michael Niedermayer
374 ff66caab Peter Ross
    n = buf_size/sample_size;
375 725d86bf Aurelien Jacobs
376 a96b68b7 Fabrice Bellard
    switch(avctx->codec->id) {
377 b461b3bc Reimar Döffinger
    case CODEC_ID_PCM_U32LE:
378 5e8ecfde Peter Ross
        DECODE(uint32_t, le32, src, samples, n, 0, 0x80000000)
379 b461b3bc Reimar Döffinger
        break;
380
    case CODEC_ID_PCM_U32BE:
381 5e8ecfde Peter Ross
        DECODE(uint32_t, be32, src, samples, n, 0, 0x80000000)
382 b461b3bc Reimar Döffinger
        break;
383
    case CODEC_ID_PCM_S24LE:
384 5e8ecfde Peter Ross
        DECODE(int32_t, le24, src, samples, n, 8, 0)
385 b461b3bc Reimar Döffinger
        break;
386
    case CODEC_ID_PCM_S24BE:
387 5e8ecfde Peter Ross
        DECODE(int32_t, be24, src, samples, n, 8, 0)
388 b461b3bc Reimar Döffinger
        break;
389
    case CODEC_ID_PCM_U24LE:
390 5e8ecfde Peter Ross
        DECODE(uint32_t, le24, src, samples, n, 8, 0x800000)
391 b461b3bc Reimar Döffinger
        break;
392
    case CODEC_ID_PCM_U24BE:
393 5e8ecfde Peter Ross
        DECODE(uint32_t, be24, src, samples, n, 8, 0x800000)
394 b461b3bc Reimar Döffinger
        break;
395
    case CODEC_ID_PCM_S24DAUD:
396
        for(;n>0;n--) {
397 5a2f421a Ramiro Polla
          uint32_t v = bytestream_get_be24(&src);
398 b461b3bc Reimar Döffinger
          v >>= 4; // sync flags are here
399
          *samples++ = ff_reverse[(v >> 8) & 0xff] +
400
                       (ff_reverse[v & 0xff] << 8);
401
        }
402
        break;
403 725d86bf Aurelien Jacobs
    case CODEC_ID_PCM_S16LE_PLANAR:
404 ff66caab Peter Ross
        n /= avctx->channels;
405
        for(c=0;c<avctx->channels;c++)
406 4efbd755 Peter Ross
            src2[c] = &src[c*n*2];
407
        for(;n>0;n--)
408 725d86bf Aurelien Jacobs
            for(c=0;c<avctx->channels;c++)
409
                *samples++ = bytestream_get_le16(&src2[c]);
410
        src = src2[avctx->channels-1];
411
        break;
412 a96b68b7 Fabrice Bellard
    case CODEC_ID_PCM_U16LE:
413 5e8ecfde Peter Ross
        DECODE(uint16_t, le16, src, samples, n, 0, 0x8000)
414 a96b68b7 Fabrice Bellard
        break;
415
    case CODEC_ID_PCM_U16BE:
416 5e8ecfde Peter Ross
        DECODE(uint16_t, be16, src, samples, n, 0, 0x8000)
417 a96b68b7 Fabrice Bellard
        break;
418
    case CODEC_ID_PCM_S8:
419 5e8ecfde Peter Ross
        dstu8= (uint8_t*)samples;
420 a96b68b7 Fabrice Bellard
        for(;n>0;n--) {
421 5e8ecfde Peter Ross
            *dstu8++ = *src++ + 128;
422 a96b68b7 Fabrice Bellard
        }
423 5e8ecfde Peter Ross
        samples= (short*)dstu8;
424 a96b68b7 Fabrice Bellard
        break;
425 63613fe6 Måns Rullgård
#if HAVE_BIGENDIAN
426 143a5d6f Peter Ross
    case CODEC_ID_PCM_F64LE:
427
        DECODE(int64_t, le64, src, samples, n, 0, 0)
428
        break;
429 38a1c7f2 Peter Ross
    case CODEC_ID_PCM_S32LE:
430 143a5d6f Peter Ross
    case CODEC_ID_PCM_F32LE:
431 38a1c7f2 Peter Ross
        DECODE(int32_t, le32, src, samples, n, 0, 0)
432
        break;
433
    case CODEC_ID_PCM_S16LE:
434
        DECODE(int16_t, le16, src, samples, n, 0, 0)
435
        break;
436 143a5d6f Peter Ross
    case CODEC_ID_PCM_F64BE:
437 38a1c7f2 Peter Ross
    case CODEC_ID_PCM_F32BE:
438
    case CODEC_ID_PCM_S32BE:
439
    case CODEC_ID_PCM_S16BE:
440
#else
441 143a5d6f Peter Ross
    case CODEC_ID_PCM_F64BE:
442
        DECODE(int64_t, be64, src, samples, n, 0, 0)
443
        break;
444 38a1c7f2 Peter Ross
    case CODEC_ID_PCM_F32BE:
445
    case CODEC_ID_PCM_S32BE:
446
        DECODE(int32_t, be32, src, samples, n, 0, 0)
447
        break;
448
    case CODEC_ID_PCM_S16BE:
449
        DECODE(int16_t, be16, src, samples, n, 0, 0)
450
        break;
451 143a5d6f Peter Ross
    case CODEC_ID_PCM_F64LE:
452
    case CODEC_ID_PCM_F32LE:
453 38a1c7f2 Peter Ross
    case CODEC_ID_PCM_S32LE:
454
    case CODEC_ID_PCM_S16LE:
455 63613fe6 Måns Rullgård
#endif /* HAVE_BIGENDIAN */
456 a96b68b7 Fabrice Bellard
    case CODEC_ID_PCM_U8:
457 38a1c7f2 Peter Ross
        memcpy(samples, src, n*sample_size);
458
        src += n*sample_size;
459
        samples = (short*)((uint8_t*)data + n*sample_size);
460 a96b68b7 Fabrice Bellard
        break;
461 a11c2a2c Michael Niedermayer
    case CODEC_ID_PCM_ZORK:
462
        for(;n>0;n--) {
463
            int x= *src++;
464
            if(x&128) x-= 128;
465
            else      x = -x;
466
            *samples++ = x << 8;
467
        }
468
        break;
469 a96b68b7 Fabrice Bellard
    case CODEC_ID_PCM_ALAW:
470
    case CODEC_ID_PCM_MULAW:
471
        for(;n>0;n--) {
472 a4461664 Ramiro Polla
            *samples++ = s->table[*src++];
473 a96b68b7 Fabrice Bellard
        }
474
        break;
475 1472b7dd Lars Täuber
    case CODEC_ID_PCM_DVD:
476 0cdc6ec9 Peter Ross
        dst_int32_t = data;
477
        n /= avctx->channels;
478 dd1c8f3e Luca Abeni
        switch (avctx->bits_per_coded_sample) {
479 0cdc6ec9 Peter Ross
        case 20:
480 1472b7dd Lars Täuber
            while (n--) {
481 0cdc6ec9 Peter Ross
                c = avctx->channels;
482
                src8 = src + 4*c;
483
                while (c--) {
484
                    *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8   &0xf0) << 8);
485
                    *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++ &0x0f) << 12);
486
                }
487
                src = src8;
488 1472b7dd Lars Täuber
            }
489 0cdc6ec9 Peter Ross
            break;
490
        case 24:
491
            while (n--) {
492
                c = avctx->channels;
493
                src8 = src + 4*c;
494
                while (c--) {
495
                    *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++) << 8);
496
                    *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++) << 8);
497
                }
498
                src = src8;
499
            }
500
            break;
501
        default:
502
            av_log(avctx, AV_LOG_ERROR, "PCM DVD unsupported sample depth\n");
503
            return -1;
504
            break;
505 1472b7dd Lars Täuber
        }
506 0cdc6ec9 Peter Ross
        samples = (short *) dst_int32_t;
507 1472b7dd Lars Täuber
        break;
508 a96b68b7 Fabrice Bellard
    default:
509
        return -1;
510
    }
511 0c1a9eda Zdenek Kabelac
    *data_size = (uint8_t *)samples - (uint8_t *)data;
512 a96b68b7 Fabrice Bellard
    return src - buf;
513
}
514
515 b250f9c6 Aurelien Jacobs
#if CONFIG_ENCODERS
516 fd76c37f Peter Ross
#define PCM_ENCODER(id,sample_fmt_,name,long_name_) \
517 a96b68b7 Fabrice Bellard
AVCodec name ## _encoder = {                    \
518
    #name,                                      \
519
    CODEC_TYPE_AUDIO,                           \
520
    id,                                         \
521
    0,                                          \
522 bb270c08 Diego Biurrun
    pcm_encode_init,                            \
523
    pcm_encode_frame,                           \
524
    pcm_encode_close,                           \
525 a96b68b7 Fabrice Bellard
    NULL,                                       \
526 fd76c37f Peter Ross
    .sample_fmts = (enum SampleFormat[]){sample_fmt_,SAMPLE_FMT_NONE}, \
527 fe4bf374 Stefano Sabatini
    .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
528 2b045c9c Aurelien Jacobs
};
529
#else
530 fd76c37f Peter Ross
#define PCM_ENCODER(id,sample_fmt_,name,long_name_)
531 2b045c9c Aurelien Jacobs
#endif
532
533 b250f9c6 Aurelien Jacobs
#if CONFIG_DECODERS
534 0fd7e57e Peter Ross
#define PCM_DECODER(id,sample_fmt_,name,long_name_)         \
535 a96b68b7 Fabrice Bellard
AVCodec name ## _decoder = {                    \
536
    #name,                                      \
537
    CODEC_TYPE_AUDIO,                           \
538
    id,                                         \
539
    sizeof(PCMDecode),                          \
540 bb270c08 Diego Biurrun
    pcm_decode_init,                            \
541 a96b68b7 Fabrice Bellard
    NULL,                                       \
542
    NULL,                                       \
543 cd4af68a Zdenek Kabelac
    pcm_decode_frame,                           \
544 0fd7e57e Peter Ross
    .sample_fmts = (enum SampleFormat[]){sample_fmt_,SAMPLE_FMT_NONE}, \
545 fe4bf374 Stefano Sabatini
    .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
546 2b045c9c Aurelien Jacobs
};
547
#else
548 0fd7e57e Peter Ross
#define PCM_DECODER(id,sample_fmt_,name,long_name_)
549 2b045c9c Aurelien Jacobs
#endif
550
551 fd76c37f Peter Ross
#define PCM_CODEC(id, sample_fmt_, name, long_name_)         \
552 0fd7e57e Peter Ross
    PCM_ENCODER(id,sample_fmt_,name,long_name_) PCM_DECODER(id,sample_fmt_,name,long_name_)
553 44caa86d Diego Biurrun
554 bbdfa06d Diego Biurrun
/* Note: Do not forget to add new entries to the Makefile as well. */
555 655463b9 Diego Biurrun
PCM_CODEC  (CODEC_ID_PCM_ALAW,  SAMPLE_FMT_S16, pcm_alaw, "PCM A-law");
556
PCM_CODEC  (CODEC_ID_PCM_DVD,   SAMPLE_FMT_S32, pcm_dvd, "PCM signed 20|24-bit big-endian");
557
PCM_CODEC  (CODEC_ID_PCM_F32BE, SAMPLE_FMT_FLT, pcm_f32be, "PCM 32-bit floating point big-endian");
558
PCM_CODEC  (CODEC_ID_PCM_F32LE, SAMPLE_FMT_FLT, pcm_f32le, "PCM 32-bit floating point little-endian");
559
PCM_CODEC  (CODEC_ID_PCM_F64BE, SAMPLE_FMT_DBL, pcm_f64be, "PCM 64-bit floating point big-endian");
560
PCM_CODEC  (CODEC_ID_PCM_F64LE, SAMPLE_FMT_DBL, pcm_f64le, "PCM 64-bit floating point little-endian");
561
PCM_CODEC  (CODEC_ID_PCM_MULAW, SAMPLE_FMT_S16, pcm_mulaw, "PCM mu-law");
562
PCM_CODEC  (CODEC_ID_PCM_S8,    SAMPLE_FMT_U8,  pcm_s8, "PCM signed 8-bit");
563
PCM_CODEC  (CODEC_ID_PCM_S16BE, SAMPLE_FMT_S16, pcm_s16be, "PCM signed 16-bit big-endian");
564
PCM_CODEC  (CODEC_ID_PCM_S16LE, SAMPLE_FMT_S16, pcm_s16le, "PCM signed 16-bit little-endian");
565
PCM_DECODER(CODEC_ID_PCM_S16LE_PLANAR, SAMPLE_FMT_S16, pcm_s16le_planar, "PCM 16-bit little-endian planar");
566
PCM_CODEC  (CODEC_ID_PCM_S24BE, SAMPLE_FMT_S32, pcm_s24be, "PCM signed 24-bit big-endian");
567
PCM_CODEC  (CODEC_ID_PCM_S24DAUD, SAMPLE_FMT_S16,  pcm_s24daud, "PCM D-Cinema audio signed 24-bit");
568
PCM_CODEC  (CODEC_ID_PCM_S24LE, SAMPLE_FMT_S32, pcm_s24le, "PCM signed 24-bit little-endian");
569
PCM_CODEC  (CODEC_ID_PCM_S32BE, SAMPLE_FMT_S32, pcm_s32be, "PCM signed 32-bit big-endian");
570
PCM_CODEC  (CODEC_ID_PCM_S32LE, SAMPLE_FMT_S32, pcm_s32le, "PCM signed 32-bit little-endian");
571
PCM_CODEC  (CODEC_ID_PCM_U8,    SAMPLE_FMT_U8,  pcm_u8, "PCM unsigned 8-bit");
572
PCM_CODEC  (CODEC_ID_PCM_U16BE, SAMPLE_FMT_S16, pcm_u16be, "PCM unsigned 16-bit big-endian");
573
PCM_CODEC  (CODEC_ID_PCM_U16LE, SAMPLE_FMT_S16, pcm_u16le, "PCM unsigned 16-bit little-endian");
574
PCM_CODEC  (CODEC_ID_PCM_U24BE, SAMPLE_FMT_S32, pcm_u24be, "PCM unsigned 24-bit big-endian");
575
PCM_CODEC  (CODEC_ID_PCM_U24LE, SAMPLE_FMT_S32, pcm_u24le, "PCM unsigned 24-bit little-endian");
576
PCM_CODEC  (CODEC_ID_PCM_U32BE, SAMPLE_FMT_S32, pcm_u32be, "PCM unsigned 32-bit big-endian");
577
PCM_CODEC  (CODEC_ID_PCM_U32LE, SAMPLE_FMT_S32, pcm_u32le, "PCM unsigned 32-bit little-endian");
578
PCM_CODEC  (CODEC_ID_PCM_ZORK,  SAMPLE_FMT_S16, pcm_zork, "PCM Zork");