1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
23 #include "pcm_common.h"
24 #include "support_formats.h"
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,
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
)
57 if (fmt
->bitspersample
!= 4 && fmt
->bitspersample
!= 3)
59 DEBUGF("CODEC_ERROR: dvi_adpcm must have 3 or 4 bitspersample\n");
64 DEBUGF("CODEC_ERROR: dvi_adpcm is missing SamplesPerBlock value\n");
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
;
84 static uint32_t get_seek_pos(long seek_time
)
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
;
95 static int decode_dvi_adpcm(const uint8_t *inbuf
, size_t inbufsize
,
96 int32_t *outbuf
, size_t *outbufcount
)
100 int samplecode
[32][2];
109 if (fmt
->bitspersample
!= 4 && fmt
->bitspersample
!= 3) {
110 DEBUGF("decode_dvi_adpcm: wrong bitspersample\n");
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;
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
]);
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;
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
];
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)
160 else if (stepindex
[c
] < 0)
162 /* calculate the difference */
176 diff
= ((code
+ code
+ 1) * step
) >> 3; /* faster */
178 /* check the sign bit */
179 /* check for overflow and underflow errors */
183 if (sample
[c
] < -32768)
189 if (sample
[c
] > 32767)
192 /* output the new sample */
193 outbuf
[nsamples
] = sample
[c
] << 13;
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;
204 for (i
= 0; i
< 32 && inbufsize
> 0; i
++) {
206 /* read 8 more bits */
207 bitstream
|= inbuf
[0]<<bitsread
;
212 samplecode
[i
][c
] = bitstream
& 7;
213 bitstream
= bitstream
>>3;
217 /* 32*3 = 3 words, so we should end with bitsread==0 */
218 DEBUGF("decode_dvi_adpcm: error in implementation\n");
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
];
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)
234 else if (stepindex
[c
] < 0)
236 /* calculate the difference */
247 diff
= ((code
+ code
+ 1) * step
) >> 3; /* faster */
249 /* check the sign bit */
250 /* check for overflow and underflow errors */
253 if (sample
[c
] < -32768)
258 if (sample
[c
] > 32767)
261 /* output the new sample */
262 outbuf
[nsamples
] = sample
[c
] << 13;
269 if (nsamples
> *outbufcount
) {
270 DEBUGF("decode_dvi_adpcm: output buffer overflow!\n");
273 *outbufcount
= nsamples
;
274 if (inbufsize
!= 0) {
275 DEBUGF("decode_dvi_adpcm: n=%d unprocessed bytes\n", (int)inbufsize
);
280 static int decode(const uint8_t *inbuf
, size_t inbufsize
,
281 int32_t *outbuf
, int *outbufsize
)
284 unsigned int nblocks
= fmt
->chunksize
/ fmt
->blockalign
;
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
) {
297 *outbufsize
= nblocks
* fmt
->samplesperblock
;
301 static const struct pcm_codec codec
= {
307 const struct pcm_codec
*get_dvi_adpcm_codec(void)