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 {
50 /* buffer used to store the whole first packet.
51 data is only sent as one large packet */
57 static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1, 0, 1, 2, 3, 5, 8, 13, 21 };
58 static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1, 0, 1, 2, 4, 8, 16, 32, 64 };
60 #define MAX_FRAME_SIZE 2048
63 * Delta decode the compressed values in src, and put the resulting
64 * decoded samples in dst.
66 * @param[in,out] state starting value. it is saved for use in the next call.
67 * @param table delta sequence table
69 static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size,
70 uint8_t *state, const int8_t *table)
76 val = av_clip_uint8(val + table[d & 0xF]);
78 val = av_clip_uint8(val + table[d >> 4]);
85 static void raw_decode(uint8_t *dst, const int8_t *src, int src_size)
88 *dst++ = *src++ + 128;
92 static int eightsvx_decode_frame(AVCodecContext *avctx, void *data,
93 int *got_frame_ptr, AVPacket *avpkt)
95 EightSvxContext *esc = avctx->priv_data;
98 int is_compr = (avctx->codec_id != AV_CODEC_ID_PCM_S8_PLANAR);
99 int hdr_size = is_compr ? 2 : 0;
101 /* decode and interleave the first packet */
102 if (!esc->data[0] && avpkt) {
103 int chan_size = avpkt->size / avctx->channels - hdr_size;
105 if (avpkt->size % avctx->channels) {
106 av_log(avctx, AV_LOG_WARNING, "Packet with odd size, ignoring last byte\n");
108 if (avpkt->size < (hdr_size + 1) * avctx->channels) {
109 av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
110 return AVERROR(EINVAL);
114 esc->fib_acc[0] = avpkt->data[1] + 128;
115 if (avctx->channels == 2)
116 esc->fib_acc[1] = avpkt->data[2+chan_size+1] + 128;
120 esc->data_size = chan_size;
121 if (!(esc->data[0] = av_malloc(chan_size)))
122 return AVERROR(ENOMEM);
123 if (avctx->channels == 2) {
124 if (!(esc->data[1] = av_malloc(chan_size))) {
125 av_freep(&esc->data[0]);
126 return AVERROR(ENOMEM);
129 memcpy(esc->data[0], &avpkt->data[hdr_size], chan_size);
130 if (avctx->channels == 2)
131 memcpy(esc->data[1], &avpkt->data[2*hdr_size+chan_size], chan_size);
134 av_log(avctx, AV_LOG_ERROR, "unexpected empty packet\n");
135 return AVERROR(EINVAL);
138 /* decode next piece of data from the buffer */
139 buf_size = FFMIN(MAX_FRAME_SIZE, esc->data_size - esc->data_idx);
145 /* get output buffer */
146 esc->frame.nb_samples = buf_size * (is_compr + 1);
147 if ((ret = avctx->get_buffer(avctx, &esc->frame)) < 0) {
148 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
152 for (ch = 0; ch < avctx->channels; ch++) {
154 delta_decode(esc->frame.data[ch], &esc->data[ch][esc->data_idx],
155 buf_size, &esc->fib_acc[ch], esc->table);
157 raw_decode(esc->frame.data[ch], &esc->data[ch][esc->data_idx],
162 esc->data_idx += buf_size;
165 *(AVFrame *)data = esc->frame;
167 return ((avctx->frame_number == 0)*hdr_size + buf_size)*avctx->channels;
170 static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
172 EightSvxContext *esc = avctx->priv_data;
174 if (avctx->channels < 1 || avctx->channels > 2) {
175 av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
176 return AVERROR_INVALIDDATA;
179 switch (avctx->codec->id) {
180 case AV_CODEC_ID_8SVX_FIB: esc->table = fibonacci; break;
181 case AV_CODEC_ID_8SVX_EXP: esc->table = exponential; break;
182 case AV_CODEC_ID_PCM_S8_PLANAR:
183 case AV_CODEC_ID_8SVX_RAW: esc->table = NULL; break;
185 av_log(avctx, AV_LOG_ERROR, "Invalid codec id %d.\n", avctx->codec->id);
186 return AVERROR_INVALIDDATA;
188 avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
190 avcodec_get_frame_defaults(&esc->frame);
191 avctx->coded_frame = &esc->frame;
196 static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
198 EightSvxContext *esc = avctx->priv_data;
200 av_freep(&esc->data[0]);
201 av_freep(&esc->data[1]);
208 #if CONFIG_EIGHTSVX_FIB_DECODER
209 AVCodec ff_eightsvx_fib_decoder = {
211 .type = AVMEDIA_TYPE_AUDIO,
212 .id = AV_CODEC_ID_8SVX_FIB,
213 .priv_data_size = sizeof (EightSvxContext),
214 .init = eightsvx_decode_init,
215 .decode = eightsvx_decode_frame,
216 .close = eightsvx_decode_close,
217 .capabilities = CODEC_CAP_DR1,
218 .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
219 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
220 AV_SAMPLE_FMT_NONE },
223 #if CONFIG_EIGHTSVX_EXP_DECODER
224 AVCodec ff_eightsvx_exp_decoder = {
226 .type = AVMEDIA_TYPE_AUDIO,
227 .id = AV_CODEC_ID_8SVX_EXP,
228 .priv_data_size = sizeof (EightSvxContext),
229 .init = eightsvx_decode_init,
230 .decode = eightsvx_decode_frame,
231 .close = eightsvx_decode_close,
232 .capabilities = CODEC_CAP_DR1,
233 .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
234 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
235 AV_SAMPLE_FMT_NONE },
238 #if CONFIG_PCM_S8_PLANAR_DECODER
239 AVCodec ff_pcm_s8_planar_decoder = {
240 .name = "pcm_s8_planar",
241 .type = AVMEDIA_TYPE_AUDIO,
242 .id = AV_CODEC_ID_PCM_S8_PLANAR,
243 .priv_data_size = sizeof(EightSvxContext),
244 .init = eightsvx_decode_init,
245 .close = eightsvx_decode_close,
246 .decode = eightsvx_decode_frame,
247 .capabilities = CODEC_CAP_DR1,
248 .long_name = NULL_IF_CONFIG_SMALL("PCM signed 8-bit planar"),
249 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
250 AV_SAMPLE_FMT_NONE },