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 #include "libavutil/common.h"
35 /** decoder context */
36 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
;
87 AVFrame
*frame
= data
;
90 int is_compr
= (avctx
->codec_id
!= AV_CODEC_ID_PCM_S8_PLANAR
);
92 /* for the first packet, copy data to buffer */
94 int hdr_size
= is_compr
? 2 : 0;
95 int chan_size
= (avpkt
->size
- hdr_size
* avctx
->channels
) / avctx
->channels
;
97 if (avpkt
->size
< hdr_size
* avctx
->channels
) {
98 av_log(avctx
, AV_LOG_ERROR
, "packet size is too small\n");
99 return AVERROR(EINVAL
);
102 av_log(avctx
, AV_LOG_ERROR
, "unexpected data after first packet\n");
103 return AVERROR(EINVAL
);
107 esc
->fib_acc
[0] = avpkt
->data
[1] + 128;
108 if (avctx
->channels
== 2)
109 esc
->fib_acc
[1] = avpkt
->data
[2+chan_size
+1] + 128;
113 esc
->data_size
= chan_size
;
114 if (!(esc
->data
[0] = av_malloc(chan_size
)))
115 return AVERROR(ENOMEM
);
116 if (avctx
->channels
== 2) {
117 if (!(esc
->data
[1] = av_malloc(chan_size
))) {
118 av_freep(&esc
->data
[0]);
119 return AVERROR(ENOMEM
);
122 memcpy(esc
->data
[0], &avpkt
->data
[hdr_size
], chan_size
);
123 if (avctx
->channels
== 2)
124 memcpy(esc
->data
[1], &avpkt
->data
[2*hdr_size
+chan_size
], chan_size
);
127 av_log(avctx
, AV_LOG_ERROR
, "unexpected empty packet\n");
128 return AVERROR(EINVAL
);
131 /* decode next piece of data from the buffer */
132 buf_size
= FFMIN(MAX_FRAME_SIZE
, esc
->data_size
- esc
->data_idx
);
138 /* get output buffer */
139 frame
->nb_samples
= buf_size
* (is_compr
+ 1);
140 if ((ret
= ff_get_buffer(avctx
, frame
, 0)) < 0) {
141 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
145 for (ch
= 0; ch
< avctx
->channels
; ch
++) {
147 delta_decode(frame
->data
[ch
], &esc
->data
[ch
][esc
->data_idx
],
148 buf_size
, &esc
->fib_acc
[ch
], esc
->table
);
150 raw_decode(frame
->data
[ch
], &esc
->data
[ch
][esc
->data_idx
],
155 esc
->data_idx
+= buf_size
;
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
;
189 static av_cold
int eightsvx_decode_close(AVCodecContext
*avctx
)
191 EightSvxContext
*esc
= avctx
->priv_data
;
193 av_freep(&esc
->data
[0]);
194 av_freep(&esc
->data
[1]);
199 AVCodec ff_eightsvx_fib_decoder
= {
201 .type
= AVMEDIA_TYPE_AUDIO
,
202 .id
= AV_CODEC_ID_8SVX_FIB
,
203 .priv_data_size
= sizeof (EightSvxContext
),
204 .init
= eightsvx_decode_init
,
205 .close
= eightsvx_decode_close
,
206 .decode
= eightsvx_decode_frame
,
207 .capabilities
= CODEC_CAP_DELAY
| CODEC_CAP_DR1
,
208 .long_name
= NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
209 .sample_fmts
= (const enum AVSampleFormat
[]) { AV_SAMPLE_FMT_U8P
,
210 AV_SAMPLE_FMT_NONE
},
213 AVCodec ff_eightsvx_exp_decoder
= {
215 .type
= AVMEDIA_TYPE_AUDIO
,
216 .id
= AV_CODEC_ID_8SVX_EXP
,
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 exponential"),
223 .sample_fmts
= (const enum AVSampleFormat
[]) { AV_SAMPLE_FMT_U8P
,
224 AV_SAMPLE_FMT_NONE
},
227 AVCodec ff_pcm_s8_planar_decoder
= {
228 .name
= "pcm_s8_planar",
229 .type
= AVMEDIA_TYPE_AUDIO
,
230 .id
= AV_CODEC_ID_PCM_S8_PLANAR
,
231 .priv_data_size
= sizeof(EightSvxContext
),
232 .init
= eightsvx_decode_init
,
233 .close
= eightsvx_decode_close
,
234 .decode
= eightsvx_decode_frame
,
235 .capabilities
= CODEC_CAP_DELAY
| CODEC_CAP_DR1
,
236 .long_name
= NULL_IF_CONFIG_SMALL("PCM signed 8-bit planar"),
237 .sample_fmts
= (const enum AVSampleFormat
[]) { AV_SAMPLE_FMT_U8P
,
238 AV_SAMPLE_FMT_NONE
},