libpcm: add missing #include and set svn:keywords
[kugel-rb.git] / apps / codecs / libpcm / dvi_adpcm.c
blob3df5e901be081c22e32fd22ac5b99579ba4f7db7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
11 * Copyright (C) 2009 Yoshihisa Uchida
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #include "codeclib.h"
23 #include "pcm_common.h"
24 #include "support_formats.h"
27 * Intel DVI ADPCM
30 static const uint16_t dvi_adpcm_steptab[89] ICONST_ATTR = {
31 7, 8, 9, 10, 11, 12, 13, 14,
32 16, 17, 19, 21, 23, 25, 28, 31,
33 34, 37, 41, 45, 50, 55, 60, 66,
34 73, 80, 88, 97, 107, 118, 130, 143,
35 157, 173, 190, 209, 230, 253, 279, 307,
36 337, 371, 408, 449, 494, 544, 598, 658,
37 724, 796, 876, 963, 1060, 1166, 1282, 1411,
38 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
39 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484,
40 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
41 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794,
42 32767 };
44 static const int dvi_adpcm_indextab4[8] ICONST_ATTR = {
45 -1, -1, -1, -1, 2, 4, 6, 8 };
47 static const int dvi_adpcm_indextab3[4] ICONST_ATTR = { -1, -1, 1, 2 };
49 static struct pcm_format *fmt;
51 static bool set_format(struct pcm_format *format, const unsigned char *fmtpos)
53 fmt = format;
55 (void)fmtpos;
57 if (fmt->bitspersample != 4 && fmt->bitspersample != 3)
59 DEBUGF("CODEC_ERROR: dvi_adpcm must have 3 or 4 bitspersample\n");
60 return false;
63 if (fmt->size < 2) {
64 DEBUGF("CODEC_ERROR: dvi_adpcm is missing SamplesPerBlock value\n");
65 return false;
68 /* chunksize is computed so that one chunk is about 1/50s.
69 * this make 4096 for 44.1kHz 16bits stereo.
70 * It also has to be a multiple of blockalign */
71 fmt->chunksize = (1 + fmt->avgbytespersec / (50*fmt->blockalign))*fmt->blockalign;
73 /* check that the output buffer is big enough (convert to samplespersec,
74 then round to the blockalign multiple below) */
75 if ((((uint64_t)fmt->chunksize * ci->id3->frequency * fmt->channels * fmt->bitspersample)>>3)
76 /(uint64_t)fmt->avgbytespersec >= PCM_CHUNK_SIZE)
77 fmt->chunksize = ((uint64_t)PCM_CHUNK_SIZE * fmt->avgbytespersec
78 /((uint64_t)ci->id3->frequency * fmt->channels * 2
79 * fmt->blockalign)) * fmt->blockalign;
81 return true;
84 static uint32_t get_seek_pos(long seek_time)
86 uint32_t newpos;
88 /* use avgbytespersec to round to the closest blockalign multiple,
89 add firstblockposn. 64-bit casts to avoid overflows. */
90 newpos = (((uint64_t)fmt->avgbytespersec*(seek_time - 1))
91 / (1000LL*fmt->blockalign))*fmt->blockalign;
92 return newpos;
95 static int decode_dvi_adpcm(const uint8_t *inbuf, size_t inbufsize,
96 int32_t *outbuf, size_t *outbufcount)
98 size_t nsamples = 0;
99 int sample[2];
100 int samplecode[32][2];
101 int i;
102 int stepindex[2];
103 int c;
104 int diff;
105 int step;
106 int codem;
107 int code;
109 if (fmt->bitspersample != 4 && fmt->bitspersample != 3) {
110 DEBUGF("decode_dvi_adpcm: wrong bitspersample\n");
111 return CODEC_ERROR;
114 /* decode block header */
115 for (c = 0; c < fmt->channels && inbufsize >= 4; c++) {
116 /* decode + push first sample */
117 sample[c] = (short)(inbuf[0]|(inbuf[1]<<8));/* need cast for sign-extend */
118 outbuf[c] = sample[c] << 13;
119 nsamples++;
120 stepindex[c] = inbuf[2];
121 /* check for step table index overflow */
122 if (stepindex[c] > 88) {
123 DEBUGF("decode_dvi_adpcm: stepindex[%d]=%d>88\n",c,stepindex[c]);
124 return CODEC_ERROR;
127 inbuf += 4;
128 inbufsize -= 4;
130 if (fmt->bitspersample == 4) {
131 while (inbufsize >= (size_t)(fmt->channels*4) &&
132 (nsamples + (fmt->channels*8) <= *outbufcount))
134 for (c = 0; c < fmt->channels; c++)
136 samplecode[0][c] = inbuf[0]&0xf;
137 samplecode[1][c] = inbuf[0]>>4;
138 samplecode[2][c] = inbuf[1]&0xf;
139 samplecode[3][c] = inbuf[1]>>4;
140 samplecode[4][c] = inbuf[2]&0xf;
141 samplecode[5][c] = inbuf[2]>>4;
142 samplecode[6][c] = inbuf[3]&0xf;
143 samplecode[7][c] = inbuf[3]>>4;
144 inbuf += 4;
145 inbufsize -= 4;
147 for (i = 0; i < 8; i++)
149 for (c = 0; c < fmt->channels; c++)
151 step = dvi_adpcm_steptab[stepindex[c]];
152 codem = samplecode[i][c];
153 code = codem & 0x07;
155 /* adjust the step table index */
156 stepindex[c] += dvi_adpcm_indextab4[code];
157 /* check for step table index overflow and underflow */
158 if (stepindex[c] > 88)
159 stepindex[c] = 88;
160 else if (stepindex[c] < 0)
161 stepindex[c] = 0;
162 /* calculate the difference */
163 #ifdef STRICT_IMA
164 diff = 0;
165 if (code & 4)
166 diff += step;
167 step = step >> 1;
168 if (code & 2)
169 diff += step;
170 step = step >> 1;
171 if (code & 1)
172 diff += step;
173 step = step >> 1;
174 diff += step;
175 #else
176 diff = ((code + code + 1) * step) >> 3; /* faster */
177 #endif
178 /* check the sign bit */
179 /* check for overflow and underflow errors */
180 if (code != codem)
182 sample[c] -= diff;
183 if (sample[c] < -32768)
184 sample[c] = -32768;
186 else
188 sample[c] += diff;
189 if (sample[c] > 32767)
190 sample[c] = 32767;
192 /* output the new sample */
193 outbuf[nsamples] = sample[c] << 13;
194 nsamples++;
198 } else { /* bitspersample == 3 */
199 while (inbufsize >= (uint32_t)(fmt->channels*12) &&
200 (nsamples + 32*fmt->channels) <= *outbufcount) {
201 for (c = 0; c < fmt->channels; c++) {
202 uint16_t bitstream = 0;
203 int bitsread = 0;
204 for (i = 0; i < 32 && inbufsize > 0; i++) {
205 if (bitsread < 3) {
206 /* read 8 more bits */
207 bitstream |= inbuf[0]<<bitsread;
208 bitsread += 8;
209 inbufsize--;
210 inbuf++;
212 samplecode[i][c] = bitstream & 7;
213 bitstream = bitstream>>3;
214 bitsread -= 3;
216 if (bitsread != 0) {
217 /* 32*3 = 3 words, so we should end with bitsread==0 */
218 DEBUGF("decode_dvi_adpcm: error in implementation\n");
219 return CODEC_ERROR;
223 for (i = 0; i < 32; i++) {
224 for (c = 0; c < fmt->channels; c++) {
225 step = dvi_adpcm_steptab[stepindex[c]];
226 codem = samplecode[i][c];
227 code = codem & 0x03;
229 /* adjust the step table index */
230 stepindex[c] += dvi_adpcm_indextab3[code];
231 /* check for step table index overflow and underflow */
232 if (stepindex[c] > 88)
233 stepindex[c] = 88;
234 else if (stepindex[c] < 0)
235 stepindex[c] = 0;
236 /* calculate the difference */
237 #ifdef STRICT_IMA
238 diff = 0;
239 if (code & 2)
240 diff += step;
241 step = step >> 1;
242 if (code & 1)
243 diff += step;
244 step = step >> 1;
245 diff += step;
246 #else
247 diff = ((code + code + 1) * step) >> 3; /* faster */
248 #endif
249 /* check the sign bit */
250 /* check for overflow and underflow errors */
251 if (code != codem) {
252 sample[c] -= diff;
253 if (sample[c] < -32768)
254 sample[c] = -32768;
256 else {
257 sample[c] += diff;
258 if (sample[c] > 32767)
259 sample[c] = 32767;
261 /* output the new sample */
262 outbuf[nsamples] = sample[c] << 13;
263 nsamples++;
269 if (nsamples > *outbufcount) {
270 DEBUGF("decode_dvi_adpcm: output buffer overflow!\n");
271 return CODEC_ERROR;
273 *outbufcount = nsamples;
274 if (inbufsize != 0) {
275 DEBUGF("decode_dvi_adpcm: n=%d unprocessed bytes\n", (int)inbufsize);
277 return CODEC_OK;
280 static int decode(const uint8_t *inbuf, size_t inbufsize,
281 int32_t *outbuf, int *outbufsize)
283 unsigned int i;
284 unsigned int nblocks = fmt->chunksize / fmt->blockalign;
286 (void)inbufsize;
288 for (i = 0; i < nblocks; i++)
290 size_t decodedsize = fmt->samplesperblock * fmt->channels;
291 if (decode_dvi_adpcm(inbuf + i * fmt->blockalign, fmt->blockalign,
292 outbuf + i * fmt->samplesperblock * fmt->channels,
293 &decodedsize) != CODEC_OK) {
294 return CODEC_ERROR;
297 *outbufsize = nblocks * fmt->samplesperblock;
298 return CODEC_OK;
301 static const struct pcm_codec codec = {
302 set_format,
303 get_seek_pos,
304 decode,
307 const struct pcm_codec *get_dvi_adpcm_codec(void)
309 return &codec;