diff options
Diffstat (limited to 'libavcodec/pcm.c')
-rw-r--r-- | libavcodec/pcm.c | 161 |
1 files changed, 120 insertions, 41 deletions
diff --git a/libavcodec/pcm.c b/libavcodec/pcm.c index d6899cf52d..ea6fabd47b 100644 --- a/libavcodec/pcm.c +++ b/libavcodec/pcm.c @@ -2,20 +2,20 @@ * PCM codecs * Copyright (c) 2001 Fabrice Bellard * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -79,13 +79,24 @@ static av_cold int pcm_encode_close(AVCodecContext *avctx) bytestream_put_ ## endian(&dst, v); \ } +#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); \ + } \ + } + static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr) { - int n, sample_size, v, ret; + int n, c, sample_size, v, ret; const short *samples; unsigned char *dst; - const uint8_t *srcu8; + const uint8_t *samples_uint8_t; const int16_t *samples_int16_t; const int32_t *samples_int32_t; const int64_t *samples_int64_t; @@ -96,10 +107,8 @@ static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, n = frame->nb_samples * avctx->channels; samples = (const short *)frame->data[0]; - if ((ret = ff_alloc_packet(avpkt, n * sample_size))) { - av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n"); + if ((ret = ff_alloc_packet2(avctx, avpkt, n * sample_size))) return ret; - } dst = avpkt->data; switch (avctx->codec->id) { @@ -112,6 +121,9 @@ static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, case AV_CODEC_ID_PCM_S24LE: ENCODE(int32_t, le24, samples, dst, n, 8, 0) break; + case AV_CODEC_ID_PCM_S24LE_PLANAR: + ENCODE_PLANAR(int32_t, le24, dst, n, 8, 0) + break; case AV_CODEC_ID_PCM_S24BE: ENCODE(int32_t, be24, samples, dst, n, 8, 0) break; @@ -137,11 +149,10 @@ static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, ENCODE(uint16_t, be16, samples, dst, n, 0, 0x8000) break; case AV_CODEC_ID_PCM_S8: - srcu8 = frame->data[0]; - for (; n > 0; n--) { - v = *srcu8++; - *dst++ = v - 128; - } + ENCODE(uint8_t, byte, samples, dst, n, 0, -128) + break; + case AV_CODEC_ID_PCM_S8_PLANAR: + ENCODE_PLANAR(uint8_t, byte, dst, n, 0, -128) break; #if HAVE_BIGENDIAN case AV_CODEC_ID_PCM_F64LE: @@ -151,9 +162,15 @@ static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, case AV_CODEC_ID_PCM_F32LE: ENCODE(int32_t, le32, samples, dst, n, 0, 0) break; + case AV_CODEC_ID_PCM_S32LE_PLANAR: + ENCODE_PLANAR(int32_t, le32, dst, n, 0, 0) + break; case AV_CODEC_ID_PCM_S16LE: ENCODE(int16_t, le16, samples, dst, n, 0, 0) break; + case AV_CODEC_ID_PCM_S16LE_PLANAR: + ENCODE_PLANAR(int16_t, le16, dst, n, 0, 0) + break; case AV_CODEC_ID_PCM_F64BE: case AV_CODEC_ID_PCM_F32BE: case AV_CODEC_ID_PCM_S32BE: @@ -169,6 +186,9 @@ static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, case AV_CODEC_ID_PCM_S16BE: ENCODE(int16_t, be16, samples, dst, n, 0, 0) break; + case AV_CODEC_ID_PCM_S16BE_PLANAR: + ENCODE_PLANAR(int16_t, be16, dst, n, 0, 0) + break; case AV_CODEC_ID_PCM_F64LE: case AV_CODEC_ID_PCM_F32LE: case AV_CODEC_ID_PCM_S32LE: @@ -176,7 +196,18 @@ static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, #endif /* HAVE_BIGENDIAN */ case AV_CODEC_ID_PCM_U8: memcpy(dst, samples, n * sample_size); - dst += n * sample_size; + break; +#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; case AV_CODEC_ID_PCM_ALAW: for (; n > 0; n--) { @@ -213,7 +244,7 @@ static av_cold int pcm_decode_init(AVCodecContext *avctx) return AVERROR(EINVAL); } - switch (avctx->codec->id) { + switch (avctx->codec_id) { case AV_CODEC_ID_PCM_ALAW: for (i = 0; i < 256; i++) s->table[i] = alaw2linear(i); @@ -229,7 +260,7 @@ static av_cold int pcm_decode_init(AVCodecContext *avctx) avctx->sample_fmt = avctx->codec->sample_fmts[0]; if (avctx->sample_fmt == AV_SAMPLE_FMT_S32) - avctx->bits_per_raw_sample = av_get_bits_per_sample(avctx->codec->id); + avctx->bits_per_raw_sample = av_get_bits_per_sample(avctx->codec_id); avcodec_get_frame_defaults(&s->frame); avctx->coded_frame = &s->frame; @@ -254,6 +285,18 @@ static av_cold int pcm_decode_init(AVCodecContext *avctx) dst += size / 8; \ } +#define DECODE_PLANAR(size, endian, src, dst, n, shift, offset) \ + n /= avctx->channels; \ + for (c = 0; c < avctx->channels; c++) { \ + int i; \ + dst = s->frame.extended_data[c]; \ + for (i = n; i > 0; i--) { \ + uint ## size ## _t v = bytestream_get_ ## endian(&src); \ + AV_WN ## size ## A(dst, (v - offset) << shift); \ + dst += size / 8; \ + } \ + } + static int pcm_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt) { @@ -271,7 +314,9 @@ static int pcm_decode_frame(AVCodecContext *avctx, void *data, if (AV_CODEC_ID_PCM_DVD == avctx->codec_id) { if (avctx->bits_per_coded_sample != 20 && avctx->bits_per_coded_sample != 24) { - av_log(avctx, AV_LOG_ERROR, "PCM DVD unsupported sample depth\n"); + av_log(avctx, AV_LOG_ERROR, + "PCM DVD unsupported sample depth %i\n", + avctx->bits_per_coded_sample); return AVERROR(EINVAL); } /* 2 samples are interleaved per block in PCM_DVD */ @@ -288,12 +333,24 @@ static int pcm_decode_frame(AVCodecContext *avctx, void *data, return AVERROR(EINVAL); } + if (avctx->channels == 0) { + av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n"); + return AVERROR(EINVAL); + } + + if (avctx->codec_id != avctx->codec->id) { + av_log(avctx, AV_LOG_ERROR, "codec ids mismatch\n"); + return AVERROR(EINVAL); + } + n = avctx->channels * sample_size; if (n && buf_size % n) { if (buf_size < n) { - av_log(avctx, AV_LOG_ERROR, "invalid PCM packet\n"); - return -1; + 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; } else buf_size -= buf_size % n; } @@ -308,7 +365,7 @@ static int pcm_decode_frame(AVCodecContext *avctx, void *data, } samples = s->frame.data[0]; - switch (avctx->codec->id) { + switch (avctx->codec_id) { case AV_CODEC_ID_PCM_U32LE: DECODE(32, le32, src, samples, n, 0, 0x80000000) break; @@ -318,6 +375,9 @@ static int pcm_decode_frame(AVCodecContext *avctx, void *data, case AV_CODEC_ID_PCM_S24LE: DECODE(32, le24, src, samples, n, 8, 0) break; + case AV_CODEC_ID_PCM_S24LE_PLANAR: + DECODE_PLANAR(32, le24, src, samples, n, 8, 0); + break; case AV_CODEC_ID_PCM_S24BE: DECODE(32, be24, src, samples, n, 8, 0) break; @@ -336,22 +396,6 @@ static int pcm_decode_frame(AVCodecContext *avctx, void *data, samples += 2; } break; - case AV_CODEC_ID_PCM_S16LE_PLANAR: - { - int av_unused n2; - n /= avctx->channels; - for (c = 0; c < avctx->channels; c++) { - samples = s->frame.extended_data[c]; -#if HAVE_BIGENDIAN - n2 = n; - DECODE(16, le16, src, samples, n2, 0, 0) -#else - memcpy(samples, src, n * 2); - src += n * 2; -#endif - } - break; - } case AV_CODEC_ID_PCM_U16LE: DECODE(16, le16, src, samples, n, 0, 0x8000) break; @@ -362,6 +406,15 @@ static int pcm_decode_frame(AVCodecContext *avctx, void *data, for (; n > 0; n--) *samples++ = *src++ + 128; break; + case AV_CODEC_ID_PCM_S8_PLANAR: + n /= avctx->channels; + for (c = 0; c < avctx->channels; c++) { + int i; + samples = s->frame.extended_data[c]; + for (i = n; i > 0; i--) + *samples++ = *src++ + 128; + } + break; #if HAVE_BIGENDIAN case AV_CODEC_ID_PCM_F64LE: DECODE(64, le64, src, samples, n, 0, 0) @@ -370,9 +423,15 @@ static int pcm_decode_frame(AVCodecContext *avctx, void *data, case AV_CODEC_ID_PCM_F32LE: DECODE(32, le32, src, samples, n, 0, 0) break; + case AV_CODEC_ID_PCM_S32LE_PLANAR: + DECODE_PLANAR(32, le32, src, samples, n, 0, 0); + break; case AV_CODEC_ID_PCM_S16LE: DECODE(16, le16, src, samples, n, 0, 0) break; + case AV_CODEC_ID_PCM_S16LE_PLANAR: + DECODE_PLANAR(16, le16, src, samples, n, 0, 0); + break; case AV_CODEC_ID_PCM_F64BE: case AV_CODEC_ID_PCM_F32BE: case AV_CODEC_ID_PCM_S32BE: @@ -388,6 +447,9 @@ static int pcm_decode_frame(AVCodecContext *avctx, void *data, case AV_CODEC_ID_PCM_S16BE: DECODE(16, be16, src, samples, n, 0, 0) break; + case AV_CODEC_ID_PCM_S16BE_PLANAR: + DECODE_PLANAR(16, be16, src, samples, n, 0, 0); + break; case AV_CODEC_ID_PCM_F64LE: case AV_CODEC_ID_PCM_F32LE: case AV_CODEC_ID_PCM_S32LE: @@ -396,6 +458,18 @@ static int pcm_decode_frame(AVCodecContext *avctx, void *data, case AV_CODEC_ID_PCM_U8: memcpy(samples, src, n * sample_size); break; +#if HAVE_BIGENDIAN + case AV_CODEC_ID_PCM_S16BE_PLANAR: +#else + case AV_CODEC_ID_PCM_S16LE_PLANAR: + case AV_CODEC_ID_PCM_S32LE_PLANAR: +#endif /* HAVE_BIGENDIAN */ + n /= avctx->channels; + for (c = 0; c < avctx->channels; c++) { + samples = s->frame.extended_data[c]; + bytestream_get_buffer(&src, samples, n * sample_size); + } + break; case AV_CODEC_ID_PCM_ZORK: for (; n > 0; n--) { int v = *src++; @@ -525,23 +599,27 @@ AVCodec ff_ ## name_ ## _decoder = { \ PCM_DECODER(id, sample_fmt_, name, long_name_) /* Note: Do not forget to add new entries to the Makefile as well. */ -PCM_CODEC (PCM_ALAW, AV_SAMPLE_FMT_S16, pcm_alaw, "PCM A-law"); +PCM_CODEC (PCM_ALAW, AV_SAMPLE_FMT_S16, pcm_alaw, "PCM A-law / G.711 A-law"); 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"); -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"); +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"); PCM_CODEC (PCM_S8, AV_SAMPLE_FMT_U8, pcm_s8, "PCM signed 8-bit"); +PCM_CODEC (PCM_S8_PLANAR, AV_SAMPLE_FMT_U8P, pcm_s8_planar, "PCM signed 8-bit planar"); PCM_CODEC (PCM_S16BE, AV_SAMPLE_FMT_S16, pcm_s16be, "PCM signed 16-bit big-endian"); +PCM_CODEC (PCM_S16BE_PLANAR, AV_SAMPLE_FMT_S16P,pcm_s16be_planar, "PCM signed 16-bit big-endian planar"); PCM_CODEC (PCM_S16LE, AV_SAMPLE_FMT_S16, pcm_s16le, "PCM signed 16-bit little-endian"); -PCM_DECODER(PCM_S16LE_PLANAR, AV_SAMPLE_FMT_S16P, pcm_s16le_planar, "PCM 16-bit little-endian planar"); +PCM_CODEC (PCM_S16LE_PLANAR, AV_SAMPLE_FMT_S16P,pcm_s16le_planar, "PCM signed 16-bit little-endian planar"); 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"); +PCM_CODEC (PCM_S24LE_PLANAR, AV_SAMPLE_FMT_S32P,pcm_s24le_planar, "PCM signed 24-bit little-endian planar"); 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"); +PCM_CODEC (PCM_S32LE_PLANAR, AV_SAMPLE_FMT_S32P,pcm_s32le_planar, "PCM signed 32-bit little-endian planar"); 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"); @@ -550,3 +628,4 @@ PCM_CODEC (PCM_U24LE, AV_SAMPLE_FMT_S32, pcm_u24le, "PCM unsigned 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"); + |