avcodec: change number of plane pointers from 4 to 8 at next major bump.
[libav.git] / libavcodec / 8svx.c
CommitLineData
cb21e0a7 1/*
4477dedc 2 * 8SVX audio decoder
cb21e0a7
JM
3 * Copyright (C) 2008 Jaikrishnan Menon
4 *
2912e87a 5 * This file is part of Libav.
cb21e0a7 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
cb21e0a7
JM
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.
11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
cb21e0a7
JM
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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
cb21e0a7
JM
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
ba87f080 23 * @file
cb21e0a7
JM
24 * 8svx audio decoder
25 * @author Jaikrishnan Menon
ec679eb4 26 *
cb21e0a7
JM
27 * supports: fibonacci delta encoding
28 * : exponential encoding
29 */
30
31#include "avcodec.h"
32
1b41f260 33/** decoder context */
cb21e0a7 34typedef struct EightSvxContext {
1993c684 35 uint8_t fib_acc[2];
e3718784 36 const int8_t *table;
1993c684
JR
37
38 /* buffer used to store the whole first packet.
39 data is only sent as one large packet */
40 uint8_t *data[2];
41 int data_size;
42 int data_idx;
cb21e0a7
JM
43} EightSvxContext;
44
e3718784
JR
45static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1,
46 0, 1, 2, 3, 5, 8, 13, 21 };
47static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1,
48 0, 1, 2, 4, 8, 16, 32, 64 };
cb21e0a7 49
1993c684
JR
50#define MAX_FRAME_SIZE 32768
51
ac68607b
JR
52/**
53 * Delta decode the compressed values in src, and put the resulting
54 * decoded samples in dst.
55 *
56 * @param[in,out] state starting value. it is saved for use in the next call.
57 */
e3718784 58static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size,
1993c684 59 uint8_t *state, const int8_t *table, int channels)
ac68607b 60{
e3718784 61 uint8_t val = *state;
ac68607b
JR
62
63 while (src_size--) {
64 uint8_t d = *src++;
e3718784 65 val = av_clip_uint8(val + table[d & 0xF]);
1993c684
JR
66 *dst = val;
67 dst += channels;
e3718784 68 val = av_clip_uint8(val + table[d >> 4]);
1993c684
JR
69 *dst = val;
70 dst += channels;
ac68607b
JR
71 }
72
73 *state = val;
74}
75
f1f6d361
JR
76static void raw_decode(uint8_t *dst, const int8_t *src, int src_size,
77 int channels)
78{
79 while (src_size--) {
80 *dst = *src++ + 128;
81 dst += channels;
82 }
83}
84
1b41f260 85/** decode a frame */
cb21e0a7 86static int eightsvx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
7a00bbad 87 AVPacket *avpkt)
cb21e0a7
JM
88{
89 EightSvxContext *esc = avctx->priv_data;
1993c684 90 int buf_size;
e3718784 91 uint8_t *out_data = data;
1993c684 92 int out_data_size;
f1f6d361 93 int is_compr = (avctx->codec_id != CODEC_ID_PCM_S8_PLANAR);
1993c684
JR
94
95 /* for the first packet, copy data to buffer */
96 if (avpkt->data) {
f1f6d361
JR
97 int hdr_size = is_compr ? 2 : 0;
98 int chan_size = (avpkt->size - hdr_size * avctx->channels) / avctx->channels;
cb21e0a7 99
f1f6d361 100 if (avpkt->size < hdr_size * avctx->channels) {
0ac3b8fc
JR
101 av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
102 return AVERROR(EINVAL);
103 }
1993c684
JR
104 if (esc->data[0]) {
105 av_log(avctx, AV_LOG_ERROR, "unexpected data after first packet\n");
106 return AVERROR(EINVAL);
107 }
108
f1f6d361 109 if (is_compr) {
1993c684
JR
110 esc->fib_acc[0] = avpkt->data[1] + 128;
111 if (avctx->channels == 2)
112 esc->fib_acc[1] = avpkt->data[2+chan_size+1] + 128;
f1f6d361 113 }
1993c684
JR
114
115 esc->data_idx = 0;
116 esc->data_size = chan_size;
117 if (!(esc->data[0] = av_malloc(chan_size)))
118 return AVERROR(ENOMEM);
119 if (avctx->channels == 2) {
120 if (!(esc->data[1] = av_malloc(chan_size))) {
121 av_freep(&esc->data[0]);
122 return AVERROR(ENOMEM);
123 }
124 }
f1f6d361 125 memcpy(esc->data[0], &avpkt->data[hdr_size], chan_size);
1993c684 126 if (avctx->channels == 2)
f1f6d361 127 memcpy(esc->data[1], &avpkt->data[2*hdr_size+chan_size], chan_size);
1993c684
JR
128 }
129 if (!esc->data[0]) {
130 av_log(avctx, AV_LOG_ERROR, "unexpected empty packet\n");
131 return AVERROR(EINVAL);
cb21e0a7
JM
132 }
133
1993c684
JR
134 /* decode next piece of data from the buffer */
135 buf_size = FFMIN(MAX_FRAME_SIZE, esc->data_size - esc->data_idx);
136 if (buf_size <= 0) {
137 *data_size = 0;
138 return avpkt->size;
139 }
f1f6d361 140 out_data_size = buf_size * (is_compr + 1) * avctx->channels;
1993c684 141 if (*data_size < out_data_size) {
fda459ce
JR
142 av_log(avctx, AV_LOG_ERROR, "Provided buffer with size %d is too small.\n",
143 *data_size);
e3718784 144 return AVERROR(EINVAL);
fda459ce 145 }
f1f6d361 146 if (is_compr) {
1993c684
JR
147 delta_decode(out_data, &esc->data[0][esc->data_idx], buf_size,
148 &esc->fib_acc[0], esc->table, avctx->channels);
149 if (avctx->channels == 2) {
150 delta_decode(&out_data[1], &esc->data[1][esc->data_idx], buf_size,
151 &esc->fib_acc[1], esc->table, avctx->channels);
152 }
f1f6d361
JR
153 } else {
154 int ch;
155 for (ch = 0; ch < avctx->channels; ch++) {
156 raw_decode((int8_t *)&out_data[ch], &esc->data[ch][esc->data_idx],
157 buf_size, avctx->channels);
158 }
159 }
1993c684
JR
160 esc->data_idx += buf_size;
161 *data_size = out_data_size;
cb21e0a7 162
1993c684 163 return avpkt->size;
cb21e0a7
JM
164}
165
1b41f260 166/** initialize 8svx decoder */
cb21e0a7
JM
167static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
168{
169 EightSvxContext *esc = avctx->priv_data;
170
1993c684
JR
171 if (avctx->channels < 1 || avctx->channels > 2) {
172 av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
173 return AVERROR(EINVAL);
174 }
175
cb21e0a7
JM
176 switch(avctx->codec->id) {
177 case CODEC_ID_8SVX_FIB:
178 esc->table = fibonacci;
179 break;
180 case CODEC_ID_8SVX_EXP:
181 esc->table = exponential;
182 break;
f1f6d361
JR
183 case CODEC_ID_PCM_S8_PLANAR:
184 break;
cb21e0a7
JM
185 default:
186 return -1;
187 }
e3718784 188 avctx->sample_fmt = AV_SAMPLE_FMT_U8;
cb21e0a7
JM
189 return 0;
190}
191
1993c684
JR
192static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
193{
194 EightSvxContext *esc = avctx->priv_data;
195
196 av_freep(&esc->data[0]);
197 av_freep(&esc->data[1]);
198
199 return 0;
200}
201
d36beb3f 202AVCodec ff_eightsvx_fib_decoder = {
99e3913d 203 .name = "8svx_fib",
72415b2a 204 .type = AVMEDIA_TYPE_AUDIO,
cb21e0a7
JM
205 .id = CODEC_ID_8SVX_FIB,
206 .priv_data_size = sizeof (EightSvxContext),
207 .init = eightsvx_decode_init,
1993c684 208 .close = eightsvx_decode_close,
cb21e0a7 209 .decode = eightsvx_decode_frame,
1993c684 210 .capabilities = CODEC_CAP_DELAY,
fe4bf374 211 .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
cb21e0a7
JM
212};
213
d36beb3f 214AVCodec ff_eightsvx_exp_decoder = {
99e3913d 215 .name = "8svx_exp",
72415b2a 216 .type = AVMEDIA_TYPE_AUDIO,
cb21e0a7
JM
217 .id = CODEC_ID_8SVX_EXP,
218 .priv_data_size = sizeof (EightSvxContext),
219 .init = eightsvx_decode_init,
1993c684 220 .close = eightsvx_decode_close,
cb21e0a7 221 .decode = eightsvx_decode_frame,
1993c684 222 .capabilities = CODEC_CAP_DELAY,
fe4bf374 223 .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
cb21e0a7 224};
f1f6d361
JR
225
226AVCodec ff_pcm_s8_planar_decoder = {
227 .name = "pcm_s8_planar",
228 .type = AVMEDIA_TYPE_AUDIO,
229 .id = CODEC_ID_PCM_S8_PLANAR,
230 .priv_data_size = sizeof(EightSvxContext),
231 .init = eightsvx_decode_init,
232 .close = eightsvx_decode_close,
233 .decode = eightsvx_decode_frame,
234 .capabilities = CODEC_CAP_DELAY,
235 .long_name = NULL_IF_CONFIG_SMALL("PCM signed 8-bit planar"),
236};