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 "ima_adpcm_common.h"
24 #include "support_formats.h"
27 * Intel DVI ADPCM (IMA ADPCM)
30 * [1] The IMA Digital Audio Focus and Technical Working Groups,
31 * Recommended Practices for Enhancing Digital Audio Compatibility
32 * in Multimedia Systems Revision 3.00, 1992
33 * [2] Microsoft Corporation, New Multimedia Data Types and Data Techniques,
35 * [3] ffmpeg source code, libavcodec/adpcm.c
38 static struct pcm_format
*fmt
;
40 static bool set_format(struct pcm_format
*format
)
44 if (fmt
->bitspersample
< 2 || fmt
->bitspersample
> 5)
46 DEBUGF("CODEC_ERROR: dvi adpcm must be 2, 3, 4 or 5 bitspersample: %d\n",
51 fmt
->chunksize
= fmt
->blockalign
;
53 init_ima_adpcm_decoder(fmt
->bitspersample
, NULL
);
57 static struct pcm_pos
*get_seek_pos(long seek_time
,
58 uint8_t *(*read_buffer
)(size_t *realsize
))
60 static struct pcm_pos newpos
;
61 uint32_t newblock
= ((uint64_t)seek_time
* ci
->id3
->frequency
)
62 / (1000LL * fmt
->samplesperblock
);
65 newpos
.pos
= newblock
* fmt
->blockalign
;
66 newpos
.samples
= newblock
* fmt
->samplesperblock
;
70 static inline void decode_2bit(const uint8_t **inbuf
, size_t inbufsize
,
71 int32_t **outbuf
, int *outbufcount
)
78 samples
= inbufsize
/ (4 * fmt
->channels
) - 1;
79 *outbufcount
+= (samples
<< 4);
82 for (ch
= 0; ch
< fmt
->channels
; ch
++)
84 pcmbuf
= *outbuf
+ ch
;
85 for (i
= 0; i
< 4; i
++)
87 *pcmbuf
= create_pcmdata(ch
, **inbuf
) << IMA_ADPCM_INC_DEPTH
;
88 pcmbuf
+= fmt
->channels
;
89 *pcmbuf
= create_pcmdata(ch
, **inbuf
>> 2) << IMA_ADPCM_INC_DEPTH
;
90 pcmbuf
+= fmt
->channels
;
91 *pcmbuf
= create_pcmdata(ch
, **inbuf
>> 4) << IMA_ADPCM_INC_DEPTH
;
92 pcmbuf
+= fmt
->channels
;
93 *pcmbuf
= create_pcmdata(ch
, **inbuf
>> 6) << IMA_ADPCM_INC_DEPTH
;
94 pcmbuf
+= fmt
->channels
;
98 *outbuf
+= 16 * fmt
->channels
;
102 static inline void decode_3bit(const uint8_t **inbuf
, size_t inbufsize
,
103 int32_t **outbuf
, int *outbufcount
)
105 const uint8_t *adpcmbuf
;
112 samples
= (inbufsize
- 4 * fmt
->channels
) / (12 * fmt
->channels
);
113 *outbufcount
+= (samples
<< 5);
116 for (ch
= 0; ch
< fmt
->channels
; ch
++)
118 adpcmbuf
= *inbuf
+ ch
* 4;
119 pcmbuf
= *outbuf
+ ch
;
120 adpcms
= *adpcmbuf
++;
121 adpcms
|= (*adpcmbuf
++) << 8;
122 adpcms
|= (*adpcmbuf
++) << 16;
123 for (i
= 0; i
< 8; i
++)
125 *pcmbuf
= create_pcmdata(ch
, adpcms
>> (3 * i
)) << IMA_ADPCM_INC_DEPTH
;
126 pcmbuf
+= fmt
->channels
;
128 adpcms
= *adpcmbuf
++;
129 adpcmbuf
+= (fmt
->channels
- 1) * 4;
130 adpcms
|= (*adpcmbuf
++) << 8;
131 adpcms
|= (*adpcmbuf
++) << 16;
132 for (i
= 0; i
< 8; i
++)
134 *pcmbuf
= create_pcmdata(ch
, adpcms
>> (3 * i
)) << IMA_ADPCM_INC_DEPTH
;
135 pcmbuf
+= fmt
->channels
;
137 adpcms
= *adpcmbuf
++;
138 adpcms
|= (*adpcmbuf
++) << 8;
139 adpcmbuf
+= (fmt
->channels
- 1) * 4;
140 adpcms
|= (*adpcmbuf
++) << 16;
141 for (i
= 0; i
< 8; i
++)
143 *pcmbuf
= create_pcmdata(ch
, adpcms
>> (3 * i
)) << IMA_ADPCM_INC_DEPTH
;
144 pcmbuf
+= fmt
->channels
;
146 adpcms
= *adpcmbuf
++;
147 adpcms
|= (*adpcmbuf
++) << 8;
148 adpcms
|= (*adpcmbuf
++) << 16;
149 for (i
= 0; i
< 8; i
++)
151 *pcmbuf
= create_pcmdata(ch
, adpcms
>> (3 * i
)) << IMA_ADPCM_INC_DEPTH
;
152 pcmbuf
+= fmt
->channels
;
155 *outbuf
+= 32 * fmt
->channels
;
156 *inbuf
+= 12 * fmt
->channels
;
160 static inline void decode_4bit(const uint8_t **inbuf
, size_t inbufsize
,
161 int32_t **outbuf
, int *outbufcount
)
168 samples
= inbufsize
/ (4 * fmt
->channels
) - 1;
169 *outbufcount
+= (samples
<< 3);
170 while (samples
-- > 0)
172 for (ch
= 0; ch
< fmt
->channels
; ch
++)
174 pcmbuf
= *outbuf
+ ch
;
175 for (i
= 0; i
< 4; i
++)
177 *pcmbuf
= create_pcmdata_size4(ch
, **inbuf
) << IMA_ADPCM_INC_DEPTH
;
178 pcmbuf
+= fmt
->channels
;
179 *pcmbuf
= create_pcmdata_size4(ch
, **inbuf
>> 4) << IMA_ADPCM_INC_DEPTH
;
180 pcmbuf
+= fmt
->channels
;
184 *outbuf
+= 8 * fmt
->channels
;
188 static inline void decode_5bit(const uint8_t **inbuf
, size_t inbufsize
,
189 int32_t **outbuf
, int *outbufcount
)
191 const uint8_t *adpcmbuf
;
198 samples
= (inbufsize
- 4 * fmt
->channels
) / (20 * fmt
->channels
);
199 *outbufcount
+= (samples
<< 5);
202 for (ch
= 0; ch
< fmt
->channels
; ch
++)
204 adpcmbuf
= *inbuf
+ ch
* 4;
205 pcmbuf
= *outbuf
+ ch
;
206 adpcms
= *adpcmbuf
++;
207 adpcms
|= (*adpcmbuf
++) << 8;
208 adpcms
|= (*adpcmbuf
++) << 16;
209 adpcms
|= (uint64_t)(*adpcmbuf
++) << 24;
210 adpcmbuf
+= (fmt
->channels
- 1) * 4;
211 adpcms
|= (uint64_t)(*adpcmbuf
++) << 32;
212 for (i
= 0; i
< 8; i
++)
214 *pcmbuf
= create_pcmdata(ch
, adpcms
>> (5 * i
)) << IMA_ADPCM_INC_DEPTH
;
215 pcmbuf
+= fmt
->channels
;
217 adpcms
= *adpcmbuf
++;
218 adpcms
|= (*adpcmbuf
++) << 8;
219 adpcms
|= (*adpcmbuf
++) << 16;
220 adpcmbuf
+= (fmt
->channels
- 1) * 4;
221 adpcms
|= (uint64_t)(*adpcmbuf
++) << 24;
222 adpcms
|= (uint64_t)(*adpcmbuf
++) << 32;
223 for (i
= 0; i
< 8; i
++)
225 *pcmbuf
= create_pcmdata(ch
, adpcms
>> (5 * i
)) << IMA_ADPCM_INC_DEPTH
;
226 pcmbuf
+= fmt
->channels
;
228 adpcms
= *adpcmbuf
++;
229 adpcms
|= (*adpcmbuf
++) << 8;
230 adpcmbuf
+= (fmt
->channels
- 1) * 4;
231 adpcms
|= (*adpcmbuf
++) << 16;
232 adpcms
|= (uint64_t)(*adpcmbuf
++) << 24;
233 adpcms
|= (uint64_t)(*adpcmbuf
++) << 32;
234 for (i
= 0; i
< 8; i
++)
236 *pcmbuf
= create_pcmdata(ch
, adpcms
>> (5 * i
)) << IMA_ADPCM_INC_DEPTH
;
237 pcmbuf
+= fmt
->channels
;
239 adpcms
= *adpcmbuf
++;
240 adpcmbuf
+= (fmt
->channels
- 1) * 4;
241 adpcms
|= (*adpcmbuf
++) << 8;
242 adpcms
|= (*adpcmbuf
++) << 16;
243 adpcms
|= (uint64_t)(*adpcmbuf
++) << 24;
244 adpcms
|= (uint64_t)(*adpcmbuf
++) << 32;
245 for (i
= 0; i
< 8; i
++)
247 *pcmbuf
= create_pcmdata(ch
, adpcms
>> (5 * i
)) << IMA_ADPCM_INC_DEPTH
;
248 pcmbuf
+= fmt
->channels
;
251 *outbuf
+= 32 * fmt
->channels
;
252 *inbuf
+= 20 * fmt
->channels
;
256 static int decode(const uint8_t *inbuf
, size_t inbufsize
,
257 int32_t *outbuf
, int *outbufcount
)
260 int32_t init_pcmdata
[2];
261 int8_t init_index
[2];
264 for (ch
= 0; ch
< fmt
->channels
; ch
++)
266 init_pcmdata
[ch
] = inbuf
[0] | (inbuf
[1] << 8);
267 if (init_pcmdata
[ch
] > 32767)
268 init_pcmdata
[ch
] -= 65536;
270 init_index
[ch
] = inbuf
[2];
271 if (init_index
[ch
] > 88 || init_index
[ch
] < 0)
273 DEBUGF("CODEC_ERROR: dvi adpcm illegal step index=%d > 88\n",
279 *outbuf
++ = init_pcmdata
[ch
] << IMA_ADPCM_INC_DEPTH
;
283 set_decode_parameters(fmt
->channels
, init_pcmdata
, init_index
);
285 if (fmt
->bitspersample
== 4)
286 decode_4bit(&inbuf
, inbufsize
, &outbuf
, outbufcount
);
287 else if (fmt
->bitspersample
== 3)
288 decode_3bit(&inbuf
, inbufsize
, &outbuf
, outbufcount
);
289 else if (fmt
->bitspersample
== 5)
290 decode_5bit(&inbuf
, inbufsize
, &outbuf
, outbufcount
);
291 else /* fmt->bitspersample == 2 */
292 decode_2bit(&inbuf
, inbufsize
, &outbuf
, outbufcount
);
297 static const struct pcm_codec codec
= {
303 const struct pcm_codec
*get_dvi_adpcm_codec(void)