yading@10: /* yading@10: * Copyright (C) 2008 Jaikrishnan Menon yading@10: * Copyright (C) 2011 Stefano Sabatini yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * 8svx audio decoder yading@10: * @author Jaikrishnan Menon yading@10: * yading@10: * supports: fibonacci delta encoding yading@10: * : exponential encoding yading@10: * yading@10: * For more information about the 8SVX format: yading@10: * http://netghost.narod.ru/gff/vendspec/iff/iff.txt yading@10: * http://sox.sourceforge.net/AudioFormats-11.html yading@10: * http://aminet.net/package/mus/misc/wavepak yading@10: * http://amigan.1emu.net/reg/8SVX.txt yading@10: * yading@10: * Samples can be found here: yading@10: * http://aminet.net/mods/smpl/ yading@10: */ yading@10: yading@10: #include "libavutil/avassert.h" yading@10: #include "avcodec.h" yading@10: #include "internal.h" yading@10: #include "libavutil/common.h" yading@10: yading@10: /** decoder context */ yading@10: typedef struct EightSvxContext { yading@10: uint8_t fib_acc[2]; yading@10: const int8_t *table; yading@10: yading@10: /* buffer used to store the whole first packet. yading@10: data is only sent as one large packet */ yading@10: uint8_t *data[2]; yading@10: int data_size; yading@10: int data_idx; yading@10: } EightSvxContext; yading@10: yading@10: static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1, 0, 1, 2, 3, 5, 8, 13, 21 }; yading@10: static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1, 0, 1, 2, 4, 8, 16, 32, 64 }; yading@10: yading@10: #define MAX_FRAME_SIZE 2048 yading@10: yading@10: /** yading@10: * Delta decode the compressed values in src, and put the resulting yading@10: * decoded samples in dst. yading@10: * yading@10: * @param[in,out] state starting value. it is saved for use in the next call. yading@10: * @param table delta sequence table yading@10: */ yading@10: static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size, yading@10: uint8_t *state, const int8_t *table) yading@10: { yading@10: uint8_t val = *state; yading@10: yading@10: while (src_size--) { yading@10: uint8_t d = *src++; yading@10: val = av_clip_uint8(val + table[d & 0xF]); yading@10: *dst++ = val; yading@10: val = av_clip_uint8(val + table[d >> 4]); yading@10: *dst++ = val; yading@10: } yading@10: yading@10: *state = val; yading@10: } yading@10: yading@10: /** decode a frame */ yading@10: static int eightsvx_decode_frame(AVCodecContext *avctx, void *data, yading@10: int *got_frame_ptr, AVPacket *avpkt) yading@10: { yading@10: EightSvxContext *esc = avctx->priv_data; yading@10: AVFrame *frame = data; yading@10: int buf_size; yading@10: int ch, ret; yading@10: int hdr_size = 2; yading@10: yading@10: /* decode and interleave the first packet */ yading@10: if (!esc->data[0] && avpkt) { yading@10: int chan_size = avpkt->size / avctx->channels - hdr_size; yading@10: yading@10: if (avpkt->size % avctx->channels) { yading@10: av_log(avctx, AV_LOG_WARNING, "Packet with odd size, ignoring last byte\n"); yading@10: } yading@10: if (avpkt->size < (hdr_size + 1) * avctx->channels) { yading@10: av_log(avctx, AV_LOG_ERROR, "packet size is too small\n"); yading@10: return AVERROR(EINVAL); yading@10: } yading@10: yading@10: esc->fib_acc[0] = avpkt->data[1] + 128; yading@10: if (avctx->channels == 2) yading@10: esc->fib_acc[1] = avpkt->data[2+chan_size+1] + 128; yading@10: yading@10: esc->data_idx = 0; yading@10: esc->data_size = chan_size; yading@10: if (!(esc->data[0] = av_malloc(chan_size))) yading@10: return AVERROR(ENOMEM); yading@10: if (avctx->channels == 2) { yading@10: if (!(esc->data[1] = av_malloc(chan_size))) { yading@10: av_freep(&esc->data[0]); yading@10: return AVERROR(ENOMEM); yading@10: } yading@10: } yading@10: memcpy(esc->data[0], &avpkt->data[hdr_size], chan_size); yading@10: if (avctx->channels == 2) yading@10: memcpy(esc->data[1], &avpkt->data[2*hdr_size+chan_size], chan_size); yading@10: } yading@10: if (!esc->data[0]) { yading@10: av_log(avctx, AV_LOG_ERROR, "unexpected empty packet\n"); yading@10: return AVERROR(EINVAL); yading@10: } yading@10: yading@10: /* decode next piece of data from the buffer */ yading@10: buf_size = FFMIN(MAX_FRAME_SIZE, esc->data_size - esc->data_idx); yading@10: if (buf_size <= 0) { yading@10: *got_frame_ptr = 0; yading@10: return avpkt->size; yading@10: } yading@10: yading@10: /* get output buffer */ yading@10: frame->nb_samples = buf_size * 2; yading@10: if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) yading@10: return ret; yading@10: yading@10: for (ch = 0; ch < avctx->channels; ch++) { yading@10: delta_decode(frame->data[ch], &esc->data[ch][esc->data_idx], yading@10: buf_size, &esc->fib_acc[ch], esc->table); yading@10: } yading@10: yading@10: esc->data_idx += buf_size; yading@10: yading@10: *got_frame_ptr = 1; yading@10: yading@10: return ((avctx->frame_number == 0)*hdr_size + buf_size)*avctx->channels; yading@10: } yading@10: yading@10: static av_cold int eightsvx_decode_init(AVCodecContext *avctx) yading@10: { yading@10: EightSvxContext *esc = avctx->priv_data; yading@10: yading@10: if (avctx->channels < 1 || avctx->channels > 2) { yading@10: av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n"); yading@10: return AVERROR_INVALIDDATA; yading@10: } yading@10: yading@10: switch (avctx->codec->id) { yading@10: case AV_CODEC_ID_8SVX_FIB: esc->table = fibonacci; break; yading@10: case AV_CODEC_ID_8SVX_EXP: esc->table = exponential; break; yading@10: default: yading@10: av_log(avctx, AV_LOG_ERROR, "Invalid codec id %d.\n", avctx->codec->id); yading@10: return AVERROR_INVALIDDATA; yading@10: } yading@10: avctx->sample_fmt = AV_SAMPLE_FMT_U8P; yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static av_cold int eightsvx_decode_close(AVCodecContext *avctx) yading@10: { yading@10: EightSvxContext *esc = avctx->priv_data; yading@10: yading@10: av_freep(&esc->data[0]); yading@10: av_freep(&esc->data[1]); yading@10: esc->data_size = 0; yading@10: esc->data_idx = 0; yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: #if CONFIG_EIGHTSVX_FIB_DECODER yading@10: AVCodec ff_eightsvx_fib_decoder = { yading@10: .name = "8svx_fib", yading@10: .type = AVMEDIA_TYPE_AUDIO, yading@10: .id = AV_CODEC_ID_8SVX_FIB, yading@10: .priv_data_size = sizeof (EightSvxContext), yading@10: .init = eightsvx_decode_init, yading@10: .decode = eightsvx_decode_frame, yading@10: .close = eightsvx_decode_close, yading@10: .capabilities = CODEC_CAP_DR1, yading@10: .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"), yading@10: .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P, yading@10: AV_SAMPLE_FMT_NONE }, yading@10: }; yading@10: #endif yading@10: #if CONFIG_EIGHTSVX_EXP_DECODER yading@10: AVCodec ff_eightsvx_exp_decoder = { yading@10: .name = "8svx_exp", yading@10: .type = AVMEDIA_TYPE_AUDIO, yading@10: .id = AV_CODEC_ID_8SVX_EXP, yading@10: .priv_data_size = sizeof (EightSvxContext), yading@10: .init = eightsvx_decode_init, yading@10: .decode = eightsvx_decode_frame, yading@10: .close = eightsvx_decode_close, yading@10: .capabilities = CODEC_CAP_DR1, yading@10: .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"), yading@10: .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P, yading@10: AV_SAMPLE_FMT_NONE }, yading@10: }; yading@10: #endif