annotate ffmpeg/libavcodec/8svx.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * Copyright (C) 2008 Jaikrishnan Menon
yading@10 3 * Copyright (C) 2011 Stefano Sabatini
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 /**
yading@10 23 * @file
yading@10 24 * 8svx audio decoder
yading@10 25 * @author Jaikrishnan Menon
yading@10 26 *
yading@10 27 * supports: fibonacci delta encoding
yading@10 28 * : exponential encoding
yading@10 29 *
yading@10 30 * For more information about the 8SVX format:
yading@10 31 * http://netghost.narod.ru/gff/vendspec/iff/iff.txt
yading@10 32 * http://sox.sourceforge.net/AudioFormats-11.html
yading@10 33 * http://aminet.net/package/mus/misc/wavepak
yading@10 34 * http://amigan.1emu.net/reg/8SVX.txt
yading@10 35 *
yading@10 36 * Samples can be found here:
yading@10 37 * http://aminet.net/mods/smpl/
yading@10 38 */
yading@10 39
yading@10 40 #include "libavutil/avassert.h"
yading@10 41 #include "avcodec.h"
yading@10 42 #include "internal.h"
yading@10 43 #include "libavutil/common.h"
yading@10 44
yading@10 45 /** decoder context */
yading@10 46 typedef struct EightSvxContext {
yading@10 47 uint8_t fib_acc[2];
yading@10 48 const int8_t *table;
yading@10 49
yading@10 50 /* buffer used to store the whole first packet.
yading@10 51 data is only sent as one large packet */
yading@10 52 uint8_t *data[2];
yading@10 53 int data_size;
yading@10 54 int data_idx;
yading@10 55 } EightSvxContext;
yading@10 56
yading@10 57 static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1, 0, 1, 2, 3, 5, 8, 13, 21 };
yading@10 58 static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1, 0, 1, 2, 4, 8, 16, 32, 64 };
yading@10 59
yading@10 60 #define MAX_FRAME_SIZE 2048
yading@10 61
yading@10 62 /**
yading@10 63 * Delta decode the compressed values in src, and put the resulting
yading@10 64 * decoded samples in dst.
yading@10 65 *
yading@10 66 * @param[in,out] state starting value. it is saved for use in the next call.
yading@10 67 * @param table delta sequence table
yading@10 68 */
yading@10 69 static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size,
yading@10 70 uint8_t *state, const int8_t *table)
yading@10 71 {
yading@10 72 uint8_t val = *state;
yading@10 73
yading@10 74 while (src_size--) {
yading@10 75 uint8_t d = *src++;
yading@10 76 val = av_clip_uint8(val + table[d & 0xF]);
yading@10 77 *dst++ = val;
yading@10 78 val = av_clip_uint8(val + table[d >> 4]);
yading@10 79 *dst++ = val;
yading@10 80 }
yading@10 81
yading@10 82 *state = val;
yading@10 83 }
yading@10 84
yading@10 85 /** decode a frame */
yading@10 86 static int eightsvx_decode_frame(AVCodecContext *avctx, void *data,
yading@10 87 int *got_frame_ptr, AVPacket *avpkt)
yading@10 88 {
yading@10 89 EightSvxContext *esc = avctx->priv_data;
yading@10 90 AVFrame *frame = data;
yading@10 91 int buf_size;
yading@10 92 int ch, ret;
yading@10 93 int hdr_size = 2;
yading@10 94
yading@10 95 /* decode and interleave the first packet */
yading@10 96 if (!esc->data[0] && avpkt) {
yading@10 97 int chan_size = avpkt->size / avctx->channels - hdr_size;
yading@10 98
yading@10 99 if (avpkt->size % avctx->channels) {
yading@10 100 av_log(avctx, AV_LOG_WARNING, "Packet with odd size, ignoring last byte\n");
yading@10 101 }
yading@10 102 if (avpkt->size < (hdr_size + 1) * avctx->channels) {
yading@10 103 av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
yading@10 104 return AVERROR(EINVAL);
yading@10 105 }
yading@10 106
yading@10 107 esc->fib_acc[0] = avpkt->data[1] + 128;
yading@10 108 if (avctx->channels == 2)
yading@10 109 esc->fib_acc[1] = avpkt->data[2+chan_size+1] + 128;
yading@10 110
yading@10 111 esc->data_idx = 0;
yading@10 112 esc->data_size = chan_size;
yading@10 113 if (!(esc->data[0] = av_malloc(chan_size)))
yading@10 114 return AVERROR(ENOMEM);
yading@10 115 if (avctx->channels == 2) {
yading@10 116 if (!(esc->data[1] = av_malloc(chan_size))) {
yading@10 117 av_freep(&esc->data[0]);
yading@10 118 return AVERROR(ENOMEM);
yading@10 119 }
yading@10 120 }
yading@10 121 memcpy(esc->data[0], &avpkt->data[hdr_size], chan_size);
yading@10 122 if (avctx->channels == 2)
yading@10 123 memcpy(esc->data[1], &avpkt->data[2*hdr_size+chan_size], chan_size);
yading@10 124 }
yading@10 125 if (!esc->data[0]) {
yading@10 126 av_log(avctx, AV_LOG_ERROR, "unexpected empty packet\n");
yading@10 127 return AVERROR(EINVAL);
yading@10 128 }
yading@10 129
yading@10 130 /* decode next piece of data from the buffer */
yading@10 131 buf_size = FFMIN(MAX_FRAME_SIZE, esc->data_size - esc->data_idx);
yading@10 132 if (buf_size <= 0) {
yading@10 133 *got_frame_ptr = 0;
yading@10 134 return avpkt->size;
yading@10 135 }
yading@10 136
yading@10 137 /* get output buffer */
yading@10 138 frame->nb_samples = buf_size * 2;
yading@10 139 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
yading@10 140 return ret;
yading@10 141
yading@10 142 for (ch = 0; ch < avctx->channels; ch++) {
yading@10 143 delta_decode(frame->data[ch], &esc->data[ch][esc->data_idx],
yading@10 144 buf_size, &esc->fib_acc[ch], esc->table);
yading@10 145 }
yading@10 146
yading@10 147 esc->data_idx += buf_size;
yading@10 148
yading@10 149 *got_frame_ptr = 1;
yading@10 150
yading@10 151 return ((avctx->frame_number == 0)*hdr_size + buf_size)*avctx->channels;
yading@10 152 }
yading@10 153
yading@10 154 static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
yading@10 155 {
yading@10 156 EightSvxContext *esc = avctx->priv_data;
yading@10 157
yading@10 158 if (avctx->channels < 1 || avctx->channels > 2) {
yading@10 159 av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
yading@10 160 return AVERROR_INVALIDDATA;
yading@10 161 }
yading@10 162
yading@10 163 switch (avctx->codec->id) {
yading@10 164 case AV_CODEC_ID_8SVX_FIB: esc->table = fibonacci; break;
yading@10 165 case AV_CODEC_ID_8SVX_EXP: esc->table = exponential; break;
yading@10 166 default:
yading@10 167 av_log(avctx, AV_LOG_ERROR, "Invalid codec id %d.\n", avctx->codec->id);
yading@10 168 return AVERROR_INVALIDDATA;
yading@10 169 }
yading@10 170 avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
yading@10 171
yading@10 172 return 0;
yading@10 173 }
yading@10 174
yading@10 175 static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
yading@10 176 {
yading@10 177 EightSvxContext *esc = avctx->priv_data;
yading@10 178
yading@10 179 av_freep(&esc->data[0]);
yading@10 180 av_freep(&esc->data[1]);
yading@10 181 esc->data_size = 0;
yading@10 182 esc->data_idx = 0;
yading@10 183
yading@10 184 return 0;
yading@10 185 }
yading@10 186
yading@10 187 #if CONFIG_EIGHTSVX_FIB_DECODER
yading@10 188 AVCodec ff_eightsvx_fib_decoder = {
yading@10 189 .name = "8svx_fib",
yading@10 190 .type = AVMEDIA_TYPE_AUDIO,
yading@10 191 .id = AV_CODEC_ID_8SVX_FIB,
yading@10 192 .priv_data_size = sizeof (EightSvxContext),
yading@10 193 .init = eightsvx_decode_init,
yading@10 194 .decode = eightsvx_decode_frame,
yading@10 195 .close = eightsvx_decode_close,
yading@10 196 .capabilities = CODEC_CAP_DR1,
yading@10 197 .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
yading@10 198 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
yading@10 199 AV_SAMPLE_FMT_NONE },
yading@10 200 };
yading@10 201 #endif
yading@10 202 #if CONFIG_EIGHTSVX_EXP_DECODER
yading@10 203 AVCodec ff_eightsvx_exp_decoder = {
yading@10 204 .name = "8svx_exp",
yading@10 205 .type = AVMEDIA_TYPE_AUDIO,
yading@10 206 .id = AV_CODEC_ID_8SVX_EXP,
yading@10 207 .priv_data_size = sizeof (EightSvxContext),
yading@10 208 .init = eightsvx_decode_init,
yading@10 209 .decode = eightsvx_decode_frame,
yading@10 210 .close = eightsvx_decode_close,
yading@10 211 .capabilities = CODEC_CAP_DR1,
yading@10 212 .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
yading@10 213 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
yading@10 214 AV_SAMPLE_FMT_NONE },
yading@10 215 };
yading@10 216 #endif