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)
67 val = av_clip_uint8(val + table[d & 0xF]);
69 val = av_clip_uint8(val + table[d >> 4]);
76 static void raw_decode(uint8_t *dst, const int8_t *src, int src_size)
79 *dst++ = *src++ + 128;
83 static int eightsvx_decode_frame(AVCodecContext *avctx, void *data,
84 int *got_frame_ptr, AVPacket *avpkt)
86 EightSvxContext *esc = avctx->priv_data;
89 int is_compr = (avctx->codec_id != AV_CODEC_ID_PCM_S8_PLANAR);
91 /* for the first packet, copy data to buffer */
93 int hdr_size = is_compr ? 2 : 0;
94 int chan_size = (avpkt->size - hdr_size * avctx->channels) / avctx->channels;
96 if (avpkt->size < hdr_size * avctx->channels) {
97 av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
98 return AVERROR(EINVAL);
101 av_log(avctx, AV_LOG_ERROR, "unexpected data after first packet\n");
102 return AVERROR(EINVAL);
106 esc->fib_acc[0] = avpkt->data[1] + 128;
107 if (avctx->channels == 2)
108 esc->fib_acc[1] = avpkt->data[2+chan_size+1] + 128;
112 esc->data_size = chan_size;
113 if (!(esc->data[0] = av_malloc(chan_size)))
114 return AVERROR(ENOMEM);
115 if (avctx->channels == 2) {
116 if (!(esc->data[1] = av_malloc(chan_size))) {
117 av_freep(&esc->data[0]);
118 return AVERROR(ENOMEM);
121 memcpy(esc->data[0], &avpkt->data[hdr_size], chan_size);
122 if (avctx->channels == 2)
123 memcpy(esc->data[1], &avpkt->data[2*hdr_size+chan_size], chan_size);
126 av_log(avctx, AV_LOG_ERROR, "unexpected empty packet\n");
127 return AVERROR(EINVAL);
130 /* decode next piece of data from the buffer */
131 buf_size = FFMIN(MAX_FRAME_SIZE, esc->data_size - esc->data_idx);
137 /* get output buffer */
138 esc->frame.nb_samples = buf_size * (is_compr + 1);
139 if ((ret = avctx->get_buffer(avctx, &esc->frame)) < 0) {
140 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
144 for (ch = 0; ch < avctx->channels; ch++) {
146 delta_decode(esc->frame.data[ch], &esc->data[ch][esc->data_idx],
147 buf_size, &esc->fib_acc[ch], esc->table);
149 raw_decode(esc->frame.data[ch], &esc->data[ch][esc->data_idx],
154 esc->data_idx += buf_size;
157 *(AVFrame *)data = esc->frame;
162 /** initialize 8svx decoder */
163 static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
165 EightSvxContext *esc = avctx->priv_data;
167 if (avctx->channels < 1 || avctx->channels > 2) {
168 av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
169 return AVERROR(EINVAL);
172 switch(avctx->codec->id) {
173 case AV_CODEC_ID_8SVX_FIB:
174 esc->table = fibonacci;
176 case AV_CODEC_ID_8SVX_EXP:
177 esc->table = exponential;
179 case AV_CODEC_ID_PCM_S8_PLANAR:
184 avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
186 avcodec_get_frame_defaults(&esc->frame);
187 avctx->coded_frame = &esc->frame;
192 static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
194 EightSvxContext *esc = avctx->priv_data;
196 av_freep(&esc->data[0]);
197 av_freep(&esc->data[1]);
202 AVCodec ff_eightsvx_fib_decoder = {
204 .type = AVMEDIA_TYPE_AUDIO,
205 .id = AV_CODEC_ID_8SVX_FIB,
206 .priv_data_size = sizeof (EightSvxContext),
207 .init = eightsvx_decode_init,
208 .close = eightsvx_decode_close,
209 .decode = eightsvx_decode_frame,
210 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
211 .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
212 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
213 AV_SAMPLE_FMT_NONE },
216 AVCodec ff_eightsvx_exp_decoder = {
218 .type = AVMEDIA_TYPE_AUDIO,
219 .id = AV_CODEC_ID_8SVX_EXP,
220 .priv_data_size = sizeof (EightSvxContext),
221 .init = eightsvx_decode_init,
222 .close = eightsvx_decode_close,
223 .decode = eightsvx_decode_frame,
224 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
225 .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
226 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
227 AV_SAMPLE_FMT_NONE },
230 AVCodec ff_pcm_s8_planar_decoder = {
231 .name = "pcm_s8_planar",
232 .type = AVMEDIA_TYPE_AUDIO,
233 .id = AV_CODEC_ID_PCM_S8_PLANAR,
234 .priv_data_size = sizeof(EightSvxContext),
235 .init = eightsvx_decode_init,
236 .close = eightsvx_decode_close,
237 .decode = eightsvx_decode_frame,
238 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
239 .long_name = NULL_IF_CONFIG_SMALL("PCM signed 8-bit planar"),
240 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
241 AV_SAMPLE_FMT_NONE },