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
32 #include "libavutil/common.h"
34 /** decoder context */
35 typedef struct EightSvxContext {
40 /* buffer used to store the whole first packet.
41 data is only sent as one large packet */
47 static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1,
48 0, 1, 2, 3, 5, 8, 13, 21 };
49 static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1,
50 0, 1, 2, 4, 8, 16, 32, 64 };
52 #define MAX_FRAME_SIZE 32768
55 * Delta decode the compressed values in src, and put the resulting
56 * decoded samples in dst.
58 * @param[in,out] state starting value. it is saved for use in the next call.
60 static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size,
61 uint8_t *state, const int8_t *table, int channels)
67 val = av_clip_uint8(val + table[d & 0xF]);
70 val = av_clip_uint8(val + table[d >> 4]);
78 static void raw_decode(uint8_t *dst, const int8_t *src, int src_size,
88 static int eightsvx_decode_frame(AVCodecContext *avctx, void *data,
89 int *got_frame_ptr, AVPacket *avpkt)
91 EightSvxContext *esc = avctx->priv_data;
95 int is_compr = (avctx->codec_id != AV_CODEC_ID_PCM_S8_PLANAR);
97 /* for the first packet, copy data to buffer */
99 int hdr_size = is_compr ? 2 : 0;
100 int chan_size = (avpkt->size - hdr_size * avctx->channels) / avctx->channels;
102 if (avpkt->size < hdr_size * avctx->channels) {
103 av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
104 return AVERROR(EINVAL);
107 av_log(avctx, AV_LOG_ERROR, "unexpected data after first packet\n");
108 return AVERROR(EINVAL);
112 esc->fib_acc[0] = avpkt->data[1] + 128;
113 if (avctx->channels == 2)
114 esc->fib_acc[1] = avpkt->data[2+chan_size+1] + 128;
118 esc->data_size = chan_size;
119 if (!(esc->data[0] = av_malloc(chan_size)))
120 return AVERROR(ENOMEM);
121 if (avctx->channels == 2) {
122 if (!(esc->data[1] = av_malloc(chan_size))) {
123 av_freep(&esc->data[0]);
124 return AVERROR(ENOMEM);
127 memcpy(esc->data[0], &avpkt->data[hdr_size], chan_size);
128 if (avctx->channels == 2)
129 memcpy(esc->data[1], &avpkt->data[2*hdr_size+chan_size], chan_size);
132 av_log(avctx, AV_LOG_ERROR, "unexpected empty packet\n");
133 return AVERROR(EINVAL);
136 /* decode next piece of data from the buffer */
137 buf_size = FFMIN(MAX_FRAME_SIZE, esc->data_size - esc->data_idx);
143 /* get output buffer */
144 esc->frame.nb_samples = buf_size * (is_compr + 1);
145 if ((ret = avctx->get_buffer(avctx, &esc->frame)) < 0) {
146 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
149 out_data = esc->frame.data[0];
152 delta_decode(out_data, &esc->data[0][esc->data_idx], buf_size,
153 &esc->fib_acc[0], esc->table, avctx->channels);
154 if (avctx->channels == 2) {
155 delta_decode(&out_data[1], &esc->data[1][esc->data_idx], buf_size,
156 &esc->fib_acc[1], esc->table, avctx->channels);
160 for (ch = 0; ch < avctx->channels; ch++) {
161 raw_decode((int8_t *)&out_data[ch], &esc->data[ch][esc->data_idx],
162 buf_size, avctx->channels);
165 esc->data_idx += buf_size;
168 *(AVFrame *)data = esc->frame;
173 /** initialize 8svx decoder */
174 static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
176 EightSvxContext *esc = avctx->priv_data;
178 if (avctx->channels < 1 || avctx->channels > 2) {
179 av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
180 return AVERROR(EINVAL);
183 switch(avctx->codec->id) {
184 case AV_CODEC_ID_8SVX_FIB:
185 esc->table = fibonacci;
187 case AV_CODEC_ID_8SVX_EXP:
188 esc->table = exponential;
190 case AV_CODEC_ID_PCM_S8_PLANAR:
195 avctx->sample_fmt = AV_SAMPLE_FMT_U8;
197 avcodec_get_frame_defaults(&esc->frame);
198 avctx->coded_frame = &esc->frame;
203 static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
205 EightSvxContext *esc = avctx->priv_data;
207 av_freep(&esc->data[0]);
208 av_freep(&esc->data[1]);
213 AVCodec ff_eightsvx_fib_decoder = {
215 .type = AVMEDIA_TYPE_AUDIO,
216 .id = AV_CODEC_ID_8SVX_FIB,
217 .priv_data_size = sizeof (EightSvxContext),
218 .init = eightsvx_decode_init,
219 .close = eightsvx_decode_close,
220 .decode = eightsvx_decode_frame,
221 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
222 .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
225 AVCodec ff_eightsvx_exp_decoder = {
227 .type = AVMEDIA_TYPE_AUDIO,
228 .id = AV_CODEC_ID_8SVX_EXP,
229 .priv_data_size = sizeof (EightSvxContext),
230 .init = eightsvx_decode_init,
231 .close = eightsvx_decode_close,
232 .decode = eightsvx_decode_frame,
233 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
234 .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
237 AVCodec ff_pcm_s8_planar_decoder = {
238 .name = "pcm_s8_planar",
239 .type = AVMEDIA_TYPE_AUDIO,
240 .id = AV_CODEC_ID_PCM_S8_PLANAR,
241 .priv_data_size = sizeof(EightSvxContext),
242 .init = eightsvx_decode_init,
243 .close = eightsvx_decode_close,
244 .decode = eightsvx_decode_frame,
245 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
246 .long_name = NULL_IF_CONFIG_SMALL("PCM signed 8-bit planar"),