libavcodec/pcm.c
a96b68b7
 /*
  * PCM codecs
406792e7
  * Copyright (c) 2001 Fabrice Bellard
a96b68b7
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
ff4ec49e
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
a96b68b7
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
a96b68b7
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
a96b68b7
  *
ff4ec49e
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
a96b68b7
  */
115329f1
 
1ab3d669
 /**
ba87f080
  * @file
1ab3d669
  * PCM codecs
  */
115329f1
 
d5c62122
 #include "libavutil/attributes.h"
6d21f498
 #include "avcodec.h"
5a2f421a
 #include "bytestream.h"
05f95443
 #include "internal.h"
d5c62122
 #include "mathops.h"
fa034b44
 #include "pcm_tablegen.h"
a96b68b7
 
98a6fff9
 static av_cold int pcm_encode_init(AVCodecContext *avctx)
a96b68b7
 {
56f22b7e
     avctx->frame_size = 0;
6d21f498
     switch (avctx->codec->id) {
36ef5369
     case AV_CODEC_ID_PCM_ALAW:
fa034b44
         pcm_alaw_tableinit();
a96b68b7
         break;
36ef5369
     case AV_CODEC_ID_PCM_MULAW:
fa034b44
         pcm_ulaw_tableinit();
a96b68b7
         break;
     default:
         break;
     }
115329f1
 
dd1c8f3e
     avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
6d21f498
     avctx->block_align           = avctx->channels * avctx->bits_per_coded_sample / 8;
7d7b40f4
     avctx->bit_rate              = avctx->block_align * avctx->sample_rate * 8;
6d21f498
     avctx->coded_frame           = avcodec_alloc_frame();
ae8dc203
     if (!avctx->coded_frame)
         return AVERROR(ENOMEM);
115329f1
 
a96b68b7
     return 0;
 }
 
98a6fff9
 static av_cold int pcm_encode_close(AVCodecContext *avctx)
a96b68b7
 {
492cd3a9
     av_freep(&avctx->coded_frame);
 
a96b68b7
     return 0;
 }
 
b461b3bc
 /**
3ee573a3
  * Write PCM samples macro
6d21f498
  * @param type   Datatype of native machine format
3ee573a3
  * @param endian bytestream_put_xxx() suffix
6d21f498
  * @param src    Source pointer (variable name)
  * @param dst    Destination pointer (variable name)
  * @param n      Total number of samples (variable name)
  * @param shift  Bitshift (bits)
3ee573a3
  * @param offset Sample value offset
  */
6d21f498
 #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);                             \
5e8ecfde
     }
3ee573a3
 
dd59f012
 #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);                         \
         }                                                               \
     }
 
05f95443
 static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                             const AVFrame *frame, int *got_packet_ptr)
a96b68b7
 {
dd59f012
     int n, c, sample_size, v, ret;
edac49da
     const short *samples;
a96b68b7
     unsigned char *dst;
00e02366
     const uint8_t *samples_uint8_t;
edac49da
     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;
a96b68b7
 
6d21f498
     sample_size = av_get_bits_per_sample(avctx->codec->id) / 8;
05f95443
     n           = frame->nb_samples * avctx->channels;
     samples     = (const short *)frame->data[0];
 
bcaf64b6
     if ((ret = ff_alloc_packet2(avctx, avpkt, n * sample_size)) < 0)
05f95443
         return ret;
     dst = avpkt->data;
a96b68b7
 
6d21f498
     switch (avctx->codec->id) {
36ef5369
     case AV_CODEC_ID_PCM_U32LE:
5e8ecfde
         ENCODE(uint32_t, le32, samples, dst, n, 0, 0x80000000)
b461b3bc
         break;
36ef5369
     case AV_CODEC_ID_PCM_U32BE:
5e8ecfde
         ENCODE(uint32_t, be32, samples, dst, n, 0, 0x80000000)
b461b3bc
         break;
36ef5369
     case AV_CODEC_ID_PCM_S24LE:
5e8ecfde
         ENCODE(int32_t, le24, samples, dst, n, 8, 0)
b461b3bc
         break;
dd59f012
     case AV_CODEC_ID_PCM_S24LE_PLANAR:
         ENCODE_PLANAR(int32_t, le24, dst, n, 8, 0)
         break;
36ef5369
     case AV_CODEC_ID_PCM_S24BE:
5e8ecfde
         ENCODE(int32_t, be24, samples, dst, n, 8, 0)
b461b3bc
         break;
36ef5369
     case AV_CODEC_ID_PCM_U24LE:
5e8ecfde
         ENCODE(uint32_t, le24, samples, dst, n, 8, 0x800000)
b461b3bc
         break;
36ef5369
     case AV_CODEC_ID_PCM_U24BE:
5e8ecfde
         ENCODE(uint32_t, be24, samples, dst, n, 8, 0x800000)
b461b3bc
         break;
36ef5369
     case AV_CODEC_ID_PCM_S24DAUD:
6d21f498
         for (; n > 0; n--) {
d5c62122
             uint32_t tmp = ff_reverse[(*samples >> 8) & 0xff] +
                            (ff_reverse[*samples & 0xff] << 8);
b461b3bc
             tmp <<= 4; // sync flags would go here
5a2f421a
             bytestream_put_be24(&dst, tmp);
b461b3bc
             samples++;
         }
         break;
36ef5369
     case AV_CODEC_ID_PCM_U16LE:
5e8ecfde
         ENCODE(uint16_t, le16, samples, dst, n, 0, 0x8000)
a96b68b7
         break;
36ef5369
     case AV_CODEC_ID_PCM_U16BE:
5e8ecfde
         ENCODE(uint16_t, be16, samples, dst, n, 0, 0x8000)
a96b68b7
         break;
36ef5369
     case AV_CODEC_ID_PCM_S8:
00e02366
         ENCODE(uint8_t, byte, samples, dst, n, 0, -128)
a96b68b7
         break;
dd59f012
     case AV_CODEC_ID_PCM_S8_PLANAR:
         ENCODE_PLANAR(uint8_t, byte, dst, n, 0, -128)
         break;
63613fe6
 #if HAVE_BIGENDIAN
36ef5369
     case AV_CODEC_ID_PCM_F64LE:
143a5d6f
         ENCODE(int64_t, le64, samples, dst, n, 0, 0)
         break;
36ef5369
     case AV_CODEC_ID_PCM_S32LE:
     case AV_CODEC_ID_PCM_F32LE:
38a1c7f2
         ENCODE(int32_t, le32, samples, dst, n, 0, 0)
         break;
dd59f012
     case AV_CODEC_ID_PCM_S32LE_PLANAR:
         ENCODE_PLANAR(int32_t, le32, dst, n, 0, 0)
         break;
36ef5369
     case AV_CODEC_ID_PCM_S16LE:
38a1c7f2
         ENCODE(int16_t, le16, samples, dst, n, 0, 0)
         break;
dd59f012
     case AV_CODEC_ID_PCM_S16LE_PLANAR:
         ENCODE_PLANAR(int16_t, le16, dst, n, 0, 0)
         break;
36ef5369
     case AV_CODEC_ID_PCM_F64BE:
     case AV_CODEC_ID_PCM_F32BE:
     case AV_CODEC_ID_PCM_S32BE:
     case AV_CODEC_ID_PCM_S16BE:
38a1c7f2
 #else
36ef5369
     case AV_CODEC_ID_PCM_F64BE:
143a5d6f
         ENCODE(int64_t, be64, samples, dst, n, 0, 0)
         break;
36ef5369
     case AV_CODEC_ID_PCM_F32BE:
     case AV_CODEC_ID_PCM_S32BE:
38a1c7f2
         ENCODE(int32_t, be32, samples, dst, n, 0, 0)
         break;
36ef5369
     case AV_CODEC_ID_PCM_S16BE:
38a1c7f2
         ENCODE(int16_t, be16, samples, dst, n, 0, 0)
         break;
dd59f012
     case AV_CODEC_ID_PCM_S16BE_PLANAR:
         ENCODE_PLANAR(int16_t, be16, dst, n, 0, 0)
         break;
36ef5369
     case AV_CODEC_ID_PCM_F64LE:
     case AV_CODEC_ID_PCM_F32LE:
     case AV_CODEC_ID_PCM_S32LE:
     case AV_CODEC_ID_PCM_S16LE:
63613fe6
 #endif /* HAVE_BIGENDIAN */
36ef5369
     case AV_CODEC_ID_PCM_U8:
6d21f498
         memcpy(dst, samples, n * sample_size);
a96b68b7
         break;
dd59f012
 #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;
36ef5369
     case AV_CODEC_ID_PCM_ALAW:
6d21f498
         for (; n > 0; n--) {
             v      = *samples++;
a4461664
             *dst++ = linear_to_alaw[(v + 32768) >> 2];
a96b68b7
         }
         break;
36ef5369
     case AV_CODEC_ID_PCM_MULAW:
6d21f498
         for (; n > 0; n--) {
             v      = *samples++;
a4461664
             *dst++ = linear_to_ulaw[(v + 32768) >> 2];
a96b68b7
         }
         break;
     default:
         return -1;
     }
4c3d2e5f
 
05f95443
     *got_packet_ptr = 1;
     return 0;
a96b68b7
 }
 
 typedef struct PCMDecode {
6d21f498
     short   table[256];
a96b68b7
 } PCMDecode;
 
6d21f498
 static av_cold int pcm_decode_init(AVCodecContext *avctx)
a96b68b7
 {
     PCMDecode *s = avctx->priv_data;
     int i;
 
bb6941af
     if (avctx->channels <= 0) {
d94e29ca
         av_log(avctx, AV_LOG_ERROR, "PCM channels out of bounds\n");
         return AVERROR(EINVAL);
     }
 
7373b3ad
     switch (avctx->codec_id) {
36ef5369
     case AV_CODEC_ID_PCM_ALAW:
6d21f498
         for (i = 0; i < 256; i++)
a96b68b7
             s->table[i] = alaw2linear(i);
         break;
36ef5369
     case AV_CODEC_ID_PCM_MULAW:
6d21f498
         for (i = 0; i < 256; i++)
a96b68b7
             s->table[i] = ulaw2linear(i);
         break;
     default:
         break;
     }
aa29709e
 
0fd7e57e
     avctx->sample_fmt = avctx->codec->sample_fmts[0];
4dbcfa6f
 
5d6e4c16
     if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
7373b3ad
         avctx->bits_per_raw_sample = av_get_bits_per_sample(avctx->codec_id);
4dbcfa6f
 
a96b68b7
     return 0;
 }
 
b461b3bc
 /**
3ee573a3
  * Read PCM samples macro
6d21f498
  * @param size   Data size of native machine format
3ee573a3
  * @param endian bytestream_get_xxx() endian suffix
6d21f498
  * @param src    Source pointer (variable name)
  * @param dst    Destination pointer (variable name)
  * @param n      Total number of samples (variable name)
  * @param shift  Bitshift (bits)
3ee573a3
  * @param offset Sample value offset
  */
6d21f498
 #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;                                                \
b45eb9d6
     }
3ee573a3
 
6557c46d
 #define DECODE_PLANAR(size, endian, src, dst, n, shift, offset)         \
467dfd5d
     n /= avctx->channels;                                               \
     for (c = 0; c < avctx->channels; c++) {                             \
6557c46d
         int i;                                                          \
0a513869
         dst = frame->extended_data[c];                                \
467dfd5d
         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;                                            \
         }                                                               \
6557c46d
     }
467dfd5d
 
0eea2129
 static int pcm_decode_frame(AVCodecContext *avctx, void *data,
                             int *got_frame_ptr, AVPacket *avpkt)
a96b68b7
 {
0093f96d
     const uint8_t *src = avpkt->data;
6d21f498
     int buf_size       = avpkt->size;
     PCMDecode *s       = avctx->priv_data;
a6bb39ad
     AVFrame *frame     = data;
0eea2129
     int sample_size, c, n, ret, samples_per_block;
b45eb9d6
     uint8_t *samples;
5e8ecfde
     int32_t *dst_int32_t;
a96b68b7
 
6d21f498
     sample_size = av_get_bits_per_sample(avctx->codec_id) / 8;
ff66caab
 
36ef5369
     /* av_get_bits_per_sample returns 0 for AV_CODEC_ID_PCM_DVD */
0eea2129
     samples_per_block = 1;
36ef5369
     if (AV_CODEC_ID_PCM_DVD == avctx->codec_id) {
381e195b
         if (avctx->bits_per_coded_sample != 20 &&
             avctx->bits_per_coded_sample != 24) {
173715d2
             av_log(avctx, AV_LOG_ERROR,
                    "PCM DVD unsupported sample depth %i\n",
                    avctx->bits_per_coded_sample);
381e195b
             return AVERROR(EINVAL);
         }
1472b7dd
         /* 2 samples are interleaved per block in PCM_DVD */
0eea2129
         samples_per_block = 2;
6d21f498
         sample_size       = avctx->bits_per_coded_sample * 2 / 8;
36ef5369
     } else if (avctx->codec_id == AV_CODEC_ID_PCM_LXF) {
fbeabfca
         /* we process 40-bit blocks per channel for LXF */
0eea2129
         samples_per_block = 2;
6d21f498
         sample_size       = 5;
0eea2129
     }
2cd04cf9
 
e048a9ca
     if (sample_size == 0) {
         av_log(avctx, AV_LOG_ERROR, "Invalid sample_size\n");
         return AVERROR(EINVAL);
     }
 
b8551f8e
     if (avctx->channels == 0) {
         av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
         return AVERROR(EINVAL);
     }
 
f0695b09
     if (avctx->codec_id != avctx->codec->id) {
         av_log(avctx, AV_LOG_ERROR, "codec ids mismatch\n");
         return AVERROR(EINVAL);
     }
 
2cd04cf9
     n = avctx->channels * sample_size;
1472b7dd
 
6d21f498
     if (n && buf_size % n) {
012e4f5a
         if (buf_size < n) {
201b409d
             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;
6d21f498
         } else
012e4f5a
             buf_size -= buf_size % n;
1472b7dd
     }
 
6d21f498
     n = buf_size / sample_size;
725d86bf
 
0eea2129
     /* get output buffer */
a6bb39ad
     frame->nb_samples = n * samples_per_block / avctx->channels;
     if ((ret = ff_get_buffer(avctx, frame)) < 0) {
0eea2129
         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
         return ret;
f1901180
     }
a6bb39ad
     samples = frame->data[0];
f1901180
 
7373b3ad
     switch (avctx->codec_id) {
36ef5369
     case AV_CODEC_ID_PCM_U32LE:
b45eb9d6
         DECODE(32, le32, src, samples, n, 0, 0x80000000)
b461b3bc
         break;
36ef5369
     case AV_CODEC_ID_PCM_U32BE:
b45eb9d6
         DECODE(32, be32, src, samples, n, 0, 0x80000000)
b461b3bc
         break;
36ef5369
     case AV_CODEC_ID_PCM_S24LE:
b45eb9d6
         DECODE(32, le24, src, samples, n, 8, 0)
b461b3bc
         break;
f17f7595
     case AV_CODEC_ID_PCM_S24LE_PLANAR:
         DECODE_PLANAR(32, le24, src, samples, n, 8, 0);
         break;
36ef5369
     case AV_CODEC_ID_PCM_S24BE:
b45eb9d6
         DECODE(32, be24, src, samples, n, 8, 0)
b461b3bc
         break;
36ef5369
     case AV_CODEC_ID_PCM_U24LE:
b45eb9d6
         DECODE(32, le24, src, samples, n, 8, 0x800000)
b461b3bc
         break;
36ef5369
     case AV_CODEC_ID_PCM_U24BE:
b45eb9d6
         DECODE(32, be24, src, samples, n, 8, 0x800000)
b461b3bc
         break;
36ef5369
     case AV_CODEC_ID_PCM_S24DAUD:
6d21f498
         for (; n > 0; n--) {
             uint32_t v = bytestream_get_be24(&src);
             v >>= 4; // sync flags are here
d5c62122
             AV_WN16A(samples, ff_reverse[(v >> 8) & 0xff] +
                              (ff_reverse[v        & 0xff] << 8));
6d21f498
             samples += 2;
b461b3bc
         }
         break;
36ef5369
     case AV_CODEC_ID_PCM_U16LE:
b45eb9d6
         DECODE(16, le16, src, samples, n, 0, 0x8000)
a96b68b7
         break;
36ef5369
     case AV_CODEC_ID_PCM_U16BE:
b45eb9d6
         DECODE(16, be16, src, samples, n, 0, 0x8000)
a96b68b7
         break;
36ef5369
     case AV_CODEC_ID_PCM_S8:
6d21f498
         for (; n > 0; n--)
b45eb9d6
             *samples++ = *src++ + 128;
a96b68b7
         break;
da8242e2
     case AV_CODEC_ID_PCM_S8_PLANAR:
         n /= avctx->channels;
         for (c = 0; c < avctx->channels; c++) {
             int i;
0a513869
             samples = frame->extended_data[c];
da8242e2
             for (i = n; i > 0; i--)
                 *samples++ = *src++ + 128;
         }
         break;
63613fe6
 #if HAVE_BIGENDIAN
36ef5369
     case AV_CODEC_ID_PCM_F64LE:
b45eb9d6
         DECODE(64, le64, src, samples, n, 0, 0)
143a5d6f
         break;
36ef5369
     case AV_CODEC_ID_PCM_S32LE:
     case AV_CODEC_ID_PCM_F32LE:
b45eb9d6
         DECODE(32, le32, src, samples, n, 0, 0)
38a1c7f2
         break;
f17f7595
     case AV_CODEC_ID_PCM_S32LE_PLANAR:
         DECODE_PLANAR(32, le32, src, samples, n, 0, 0);
         break;
36ef5369
     case AV_CODEC_ID_PCM_S16LE:
b45eb9d6
         DECODE(16, le16, src, samples, n, 0, 0)
38a1c7f2
         break;
f17f7595
     case AV_CODEC_ID_PCM_S16LE_PLANAR:
         DECODE_PLANAR(16, le16, src, samples, n, 0, 0);
         break;
36ef5369
     case AV_CODEC_ID_PCM_F64BE:
     case AV_CODEC_ID_PCM_F32BE:
     case AV_CODEC_ID_PCM_S32BE:
     case AV_CODEC_ID_PCM_S16BE:
38a1c7f2
 #else
36ef5369
     case AV_CODEC_ID_PCM_F64BE:
b45eb9d6
         DECODE(64, be64, src, samples, n, 0, 0)
143a5d6f
         break;
36ef5369
     case AV_CODEC_ID_PCM_F32BE:
     case AV_CODEC_ID_PCM_S32BE:
b45eb9d6
         DECODE(32, be32, src, samples, n, 0, 0)
38a1c7f2
         break;
36ef5369
     case AV_CODEC_ID_PCM_S16BE:
b45eb9d6
         DECODE(16, be16, src, samples, n, 0, 0)
38a1c7f2
         break;
f17f7595
     case AV_CODEC_ID_PCM_S16BE_PLANAR:
         DECODE_PLANAR(16, be16, src, samples, n, 0, 0);
         break;
36ef5369
     case AV_CODEC_ID_PCM_F64LE:
     case AV_CODEC_ID_PCM_F32LE:
     case AV_CODEC_ID_PCM_S32LE:
     case AV_CODEC_ID_PCM_S16LE:
63613fe6
 #endif /* HAVE_BIGENDIAN */
36ef5369
     case AV_CODEC_ID_PCM_U8:
6d21f498
         memcpy(samples, src, n * sample_size);
a96b68b7
         break;
f17f7595
 #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++) {
0a513869
             samples = frame->extended_data[c];
f17f7595
             bytestream_get_buffer(&src, samples, n * sample_size);
         }
         break;
36ef5369
     case AV_CODEC_ID_PCM_ZORK:
67a3b67c
         for (; n > 0; n--) {
             int v = *src++;
             if (v < 128)
                 v = 128 - v;
             *samples++ = v;
a11c2a2c
         }
         break;
36ef5369
     case AV_CODEC_ID_PCM_ALAW:
     case AV_CODEC_ID_PCM_MULAW:
6d21f498
         for (; n > 0; n--) {
b45eb9d6
             AV_WN16A(samples, s->table[*src++]);
             samples += 2;
a96b68b7
         }
         break;
36ef5369
     case AV_CODEC_ID_PCM_DVD:
154cd253
     {
         const uint8_t *src8;
a6bb39ad
         dst_int32_t = (int32_t *)frame->data[0];
0cdc6ec9
         n /= avctx->channels;
dd1c8f3e
         switch (avctx->bits_per_coded_sample) {
0cdc6ec9
         case 20:
1472b7dd
             while (n--) {
6d21f498
                 c    = avctx->channels;
                 src8 = src + 4 * c;
0cdc6ec9
                 while (c--) {
6d21f498
                     *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8   & 0xf0) <<  8);
                     *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++ & 0x0f) << 12);
0cdc6ec9
                 }
                 src = src8;
1472b7dd
             }
0cdc6ec9
             break;
         case 24:
             while (n--) {
6d21f498
                 c    = avctx->channels;
                 src8 = src + 4 * c;
0cdc6ec9
                 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;
1472b7dd
         }
         break;
154cd253
     }
36ef5369
     case AV_CODEC_ID_PCM_LXF:
154cd253
     {
         int i;
fbeabfca
         n /= avctx->channels;
32a5775a
         for (c = 0; c < avctx->channels; c++) {
a6bb39ad
             dst_int32_t = (int32_t *)frame->extended_data[c];
32a5775a
             for (i = 0; i < n; i++) {
6d21f498
                 // extract low 20 bits and expand to 32 bits
7e5f0450
                 *dst_int32_t++ =  (src[2]         << 28) |
                                   (src[1]         << 20) |
                                   (src[0]         << 12) |
                                  ((src[2] & 0x0F) <<  8) |
                                    src[1];
6d21f498
                 // extract high 20 bits and expand to 32 bits
7e5f0450
                 *dst_int32_t++ =  (src[4]         << 24) |
                                   (src[3]         << 16) |
                                  ((src[2] & 0xF0) <<  8) |
                                   (src[4]         <<  4) |
                                   (src[3]         >>  4);
32a5775a
                 src += 5;
fbeabfca
             }
         }
         break;
154cd253
     }
a96b68b7
     default:
         return -1;
     }
0eea2129
 
a6bb39ad
     *got_frame_ptr = 1;
0eea2129
 
0093f96d
     return buf_size;
a96b68b7
 }
 
abdee952
 #define PCM_ENCODER_0(id_, sample_fmt_, name_, long_name_)
 #define PCM_ENCODER_1(id_, sample_fmt_, name_, long_name_)                  \
6d21f498
 AVCodec ff_ ## name_ ## _encoder = {                                        \
     .name         = #name_,                                                 \
     .type         = AVMEDIA_TYPE_AUDIO,                                     \
abdee952
     .id           = AV_CODEC_ID_ ## id_,                                    \
6d21f498
     .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_),                       \
98ec8287
 }
2b045c9c
 
abdee952
 #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_)                  \
6d21f498
 AVCodec ff_ ## name_ ## _decoder = {                                        \
     .name           = #name_,                                               \
     .type           = AVMEDIA_TYPE_AUDIO,                                   \
abdee952
     .id             = AV_CODEC_ID_ ## id_,                                  \
6d21f498
     .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_),                     \
98ec8287
 }
abdee952
 
 #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)
2b045c9c
 
6d21f498
 #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_)
44caa86d
 
bbdfa06d
 /* Note: Do not forget to add new entries to the Makefile as well. */
dcb0d119
 PCM_CODEC  (PCM_ALAW,         AV_SAMPLE_FMT_S16, pcm_alaw,         "PCM A-law / G.711 A-law");
abdee952
 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");
dcb0d119
 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");
abdee952
 PCM_CODEC  (PCM_S8,           AV_SAMPLE_FMT_U8,  pcm_s8,           "PCM signed 8-bit");
dd59f012
 PCM_CODEC  (PCM_S8_PLANAR,    AV_SAMPLE_FMT_U8P, pcm_s8_planar,    "PCM signed 8-bit planar");
abdee952
 PCM_CODEC  (PCM_S16BE,        AV_SAMPLE_FMT_S16, pcm_s16be,        "PCM signed 16-bit big-endian");
dd59f012
 PCM_CODEC  (PCM_S16BE_PLANAR, AV_SAMPLE_FMT_S16P,pcm_s16be_planar, "PCM signed 16-bit big-endian planar");
abdee952
 PCM_CODEC  (PCM_S16LE,        AV_SAMPLE_FMT_S16, pcm_s16le,        "PCM signed 16-bit little-endian");
dd59f012
 PCM_CODEC  (PCM_S16LE_PLANAR, AV_SAMPLE_FMT_S16P,pcm_s16le_planar, "PCM signed 16-bit little-endian planar");
abdee952
 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");
dd59f012
 PCM_CODEC  (PCM_S24LE_PLANAR, AV_SAMPLE_FMT_S32P,pcm_s24le_planar, "PCM signed 24-bit little-endian planar");
abdee952
 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");
dd59f012
 PCM_CODEC  (PCM_S32LE_PLANAR, AV_SAMPLE_FMT_S32P,pcm_s32le_planar, "PCM signed 32-bit little-endian planar");
abdee952
 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");
4480edcf