2 * Copyright (C) 2008 Jaikrishnan Menon
3 * Copyright (C) 2011 Stefano Sabatini
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * @author Jaikrishnan Menon
27 * supports: fibonacci delta encoding
28 * : exponential encoding
30 * For more information about the 8SVX format:
31 * http://netghost.narod.ru/gff/vendspec/iff/iff.txt
32 * http://sox.sourceforge.net/AudioFormats-11.html
33 * http://aminet.net/package/mus/misc/wavepak
34 * http://amigan.1emu.net/reg/8SVX.txt
36 * Samples can be found here:
37 * http://aminet.net/mods/smpl/
40 #include "libavutil/avassert.h"
42 #include "libavutil/common.h"
44 /** decoder context */
45 typedef struct EightSvxContext {
49 /* buffer used to store the whole audio decoded/interleaved chunk,
50 * which is sent with the first packet */
56 static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1, 0, 1, 2, 3, 5, 8, 13, 21 };
57 static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1, 0, 1, 2, 4, 8, 16, 32, 64 };
59 #define MAX_FRAME_SIZE 2048
62 * Delta decode the compressed values in src, and put the resulting
63 * decoded n samples in dst.
65 * @param val starting value assumed by the delta sequence
66 * @param table delta sequence table
67 * @return size in bytes of the decoded data, must be src_size*2
69 static int delta_decode(int8_t *dst, const uint8_t *src, int src_size,
70 int8_t val, const int8_t *table)
77 val = av_clip(val + table[d & 0x0f], -127, 128);
79 val = av_clip(val + table[d >> 4] , -127, 128);
87 static int eightsvx_decode_frame(AVCodecContext *avctx, void *data,
88 int *got_frame_ptr, AVPacket *avpkt)
90 EightSvxContext *esc = avctx->priv_data;
95 /* decode and interleave the first packet */
96 if (!esc->samples && avpkt) {
97 uint8_t *deinterleaved_samples, *p = NULL;
98 int packet_size = avpkt->size;
100 if (packet_size % avctx->channels) {
101 av_log(avctx, AV_LOG_WARNING, "Packet with odd size, ignoring last byte\n");
102 if (packet_size < avctx->channels)
104 packet_size -= packet_size % avctx->channels;
106 esc->samples_size = !esc->table ?
107 packet_size : avctx->channels + (packet_size-avctx->channels) * 2;
108 if (!(esc->samples = av_malloc(esc->samples_size)))
109 return AVERROR(ENOMEM);
113 const uint8_t *buf = avpkt->data;
115 int buf_size = avpkt->size;
116 int i, n = esc->samples_size;
119 av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
120 return AVERROR(EINVAL);
122 if (!(deinterleaved_samples = av_mallocz(n)))
123 return AVERROR(ENOMEM);
124 dst = p = deinterleaved_samples;
126 /* the uncompressed starting value is contained in the first byte */
127 dst = deinterleaved_samples;
128 for (i = 0; i < avctx->channels; i++) {
129 delta_decode(dst, buf + 1, buf_size / avctx->channels - 1, buf[0], esc->table);
130 buf += buf_size / avctx->channels;
131 dst += n / avctx->channels - 1;
134 deinterleaved_samples = avpkt->data;
137 memcpy(esc->samples, deinterleaved_samples, esc->samples_size);
141 /* get output buffer */
142 av_assert1(!(esc->samples_size % avctx->channels || esc->samples_idx % avctx->channels));
143 esc->frame.nb_samples = FFMIN(MAX_FRAME_SIZE, esc->samples_size - esc->samples_idx) / avctx->channels;
144 if ((ret = avctx->get_buffer(avctx, &esc->frame)) < 0) {
145 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
150 *(AVFrame *)data = esc->frame;
152 out_data_size = esc->frame.nb_samples;
153 for (ch = 0; ch<avctx->channels; ch++) {
154 dst = esc->frame.data[ch];
155 src = esc->samples + esc->samples_idx / avctx->channels + ch * esc->samples_size / avctx->channels;
156 for (n = out_data_size; n > 0; n--)
157 *dst++ = *src++ + 128;
159 out_data_size *= avctx->channels;
160 esc->samples_idx += out_data_size;
163 (avctx->frame_number == 0)*2 + out_data_size / 2 :
167 static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
169 EightSvxContext *esc = avctx->priv_data;
171 if (avctx->channels < 1 || avctx->channels > 2) {
172 av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
173 return AVERROR_INVALIDDATA;
176 switch (avctx->codec->id) {
177 case AV_CODEC_ID_8SVX_FIB: esc->table = fibonacci; break;
178 case AV_CODEC_ID_8SVX_EXP: esc->table = exponential; break;
179 case AV_CODEC_ID_PCM_S8_PLANAR:
180 case AV_CODEC_ID_8SVX_RAW: esc->table = NULL; break;
182 av_log(avctx, AV_LOG_ERROR, "Invalid codec id %d.\n", avctx->codec->id);
183 return AVERROR_INVALIDDATA;
185 avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
187 avcodec_get_frame_defaults(&esc->frame);
188 avctx->coded_frame = &esc->frame;
193 static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
195 EightSvxContext *esc = avctx->priv_data;
197 av_freep(&esc->samples);
198 esc->samples_size = 0;
199 esc->samples_idx = 0;
204 #if CONFIG_EIGHTSVX_FIB_DECODER
205 AVCodec ff_eightsvx_fib_decoder = {
207 .type = AVMEDIA_TYPE_AUDIO,
208 .id = AV_CODEC_ID_8SVX_FIB,
209 .priv_data_size = sizeof (EightSvxContext),
210 .init = eightsvx_decode_init,
211 .decode = eightsvx_decode_frame,
212 .close = eightsvx_decode_close,
213 .capabilities = CODEC_CAP_DR1,
214 .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
215 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
216 AV_SAMPLE_FMT_NONE },
219 #if CONFIG_EIGHTSVX_EXP_DECODER
220 AVCodec ff_eightsvx_exp_decoder = {
222 .type = AVMEDIA_TYPE_AUDIO,
223 .id = AV_CODEC_ID_8SVX_EXP,
224 .priv_data_size = sizeof (EightSvxContext),
225 .init = eightsvx_decode_init,
226 .decode = eightsvx_decode_frame,
227 .close = eightsvx_decode_close,
228 .capabilities = CODEC_CAP_DR1,
229 .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
230 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
231 AV_SAMPLE_FMT_NONE },
234 #if CONFIG_PCM_S8_PLANAR_DECODER
235 AVCodec ff_pcm_s8_planar_decoder = {
236 .name = "pcm_s8_planar",
237 .type = AVMEDIA_TYPE_AUDIO,
238 .id = AV_CODEC_ID_PCM_S8_PLANAR,
239 .priv_data_size = sizeof(EightSvxContext),
240 .init = eightsvx_decode_init,
241 .close = eightsvx_decode_close,
242 .decode = eightsvx_decode_frame,
243 .capabilities = CODEC_CAP_DR1,
244 .long_name = NULL_IF_CONFIG_SMALL("PCM signed 8-bit planar"),
245 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
246 AV_SAMPLE_FMT_NONE },