3 * Copyright (C) 2008 Jaikrishnan Menon
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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
33 /** decoder context */
34 typedef struct EightSvxContext {
38 /* buffer used to store the whole first packet.
39 data is only sent as one large packet */
45 static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1,
46 0, 1, 2, 3, 5, 8, 13, 21 };
47 static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1,
48 0, 1, 2, 4, 8, 16, 32, 64 };
50 #define MAX_FRAME_SIZE 32768
53 * Delta decode the compressed values in src, and put the resulting
54 * decoded samples in dst.
56 * @param[in,out] state starting value. it is saved for use in the next call.
58 static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size,
59 uint8_t *state, const int8_t *table, int channels)
65 val = av_clip_uint8(val + table[d & 0xF]);
68 val = av_clip_uint8(val + table[d >> 4]);
77 static int eightsvx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
80 EightSvxContext *esc = avctx->priv_data;
82 uint8_t *out_data = data;
85 /* for the first packet, copy data to buffer */
87 int chan_size = (avpkt->size / avctx->channels) - 2;
89 if (avpkt->size < 2) {
90 av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
91 return AVERROR(EINVAL);
94 av_log(avctx, AV_LOG_ERROR, "unexpected data after first packet\n");
95 return AVERROR(EINVAL);
98 esc->fib_acc[0] = avpkt->data[1] + 128;
99 if (avctx->channels == 2)
100 esc->fib_acc[1] = avpkt->data[2+chan_size+1] + 128;
103 esc->data_size = chan_size;
104 if (!(esc->data[0] = av_malloc(chan_size)))
105 return AVERROR(ENOMEM);
106 if (avctx->channels == 2) {
107 if (!(esc->data[1] = av_malloc(chan_size))) {
108 av_freep(&esc->data[0]);
109 return AVERROR(ENOMEM);
112 memcpy(esc->data[0], &avpkt->data[2], chan_size);
113 if (avctx->channels == 2)
114 memcpy(esc->data[1], &avpkt->data[2+chan_size+2], chan_size);
117 av_log(avctx, AV_LOG_ERROR, "unexpected empty packet\n");
118 return AVERROR(EINVAL);
121 /* decode next piece of data from the buffer */
122 buf_size = FFMIN(MAX_FRAME_SIZE, esc->data_size - esc->data_idx);
127 out_data_size = buf_size * 2 * avctx->channels;
128 if (*data_size < out_data_size) {
129 av_log(avctx, AV_LOG_ERROR, "Provided buffer with size %d is too small.\n",
131 return AVERROR(EINVAL);
133 delta_decode(out_data, &esc->data[0][esc->data_idx], buf_size,
134 &esc->fib_acc[0], esc->table, avctx->channels);
135 if (avctx->channels == 2) {
136 delta_decode(&out_data[1], &esc->data[1][esc->data_idx], buf_size,
137 &esc->fib_acc[1], esc->table, avctx->channels);
139 esc->data_idx += buf_size;
140 *data_size = out_data_size;
145 /** initialize 8svx decoder */
146 static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
148 EightSvxContext *esc = avctx->priv_data;
150 if (avctx->channels < 1 || avctx->channels > 2) {
151 av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
152 return AVERROR(EINVAL);
155 switch(avctx->codec->id) {
156 case CODEC_ID_8SVX_FIB:
157 esc->table = fibonacci;
159 case CODEC_ID_8SVX_EXP:
160 esc->table = exponential;
165 avctx->sample_fmt = AV_SAMPLE_FMT_U8;
169 static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
171 EightSvxContext *esc = avctx->priv_data;
173 av_freep(&esc->data[0]);
174 av_freep(&esc->data[1]);
179 AVCodec ff_eightsvx_fib_decoder = {
181 .type = AVMEDIA_TYPE_AUDIO,
182 .id = CODEC_ID_8SVX_FIB,
183 .priv_data_size = sizeof (EightSvxContext),
184 .init = eightsvx_decode_init,
185 .close = eightsvx_decode_close,
186 .decode = eightsvx_decode_frame,
187 .capabilities = CODEC_CAP_DELAY,
188 .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
191 AVCodec ff_eightsvx_exp_decoder = {
193 .type = AVMEDIA_TYPE_AUDIO,
194 .id = CODEC_ID_8SVX_EXP,
195 .priv_data_size = sizeof (EightSvxContext),
196 .init = eightsvx_decode_init,
197 .close = eightsvx_decode_close,
198 .decode = eightsvx_decode_frame,
199 .capabilities = CODEC_CAP_DELAY,
200 .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),