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(uint32_t seek_val
, int seek_mode
,
58 uint8_t *(*read_buffer
)(size_t *realsize
))
60 static struct pcm_pos newpos
;
61 uint32_t newblock
= (seek_mode
== PCM_SEEK_TIME
) ?
62 ((uint64_t)seek_val
* ci
->id3
->frequency
/ 1000LL)
63 / fmt
->samplesperblock
:
64 seek_val
/ fmt
->blockalign
;
67 newpos
.pos
= newblock
* fmt
->blockalign
;
68 newpos
.samples
= newblock
* fmt
->samplesperblock
;
72 static inline void decode_2bit(const uint8_t **inbuf
, size_t inbufsize
,
73 int32_t **outbuf
, int *outbufcount
)
80 samples
= inbufsize
/ (4 * fmt
->channels
) - 1;
81 *outbufcount
+= (samples
<< 4);
84 for (ch
= 0; ch
< fmt
->channels
; ch
++)
86 pcmbuf
= *outbuf
+ ch
;
87 for (i
= 0; i
< 4; i
++)
89 *pcmbuf
= create_pcmdata(ch
, **inbuf
) << IMA_ADPCM_INC_DEPTH
;
90 pcmbuf
+= fmt
->channels
;
91 *pcmbuf
= create_pcmdata(ch
, **inbuf
>> 2) << IMA_ADPCM_INC_DEPTH
;
92 pcmbuf
+= fmt
->channels
;
93 *pcmbuf
= create_pcmdata(ch
, **inbuf
>> 4) << IMA_ADPCM_INC_DEPTH
;
94 pcmbuf
+= fmt
->channels
;
95 *pcmbuf
= create_pcmdata(ch
, **inbuf
>> 6) << IMA_ADPCM_INC_DEPTH
;
96 pcmbuf
+= fmt
->channels
;
100 *outbuf
+= 16 * fmt
->channels
;
104 static inline void decode_3bit(const uint8_t **inbuf
, size_t inbufsize
,
105 int32_t **outbuf
, int *outbufcount
)
107 const uint8_t *adpcmbuf
;
114 samples
= (inbufsize
- 4 * fmt
->channels
) / (12 * fmt
->channels
);
115 *outbufcount
+= (samples
<< 5);
118 for (ch
= 0; ch
< fmt
->channels
; ch
++)
120 adpcmbuf
= *inbuf
+ ch
* 4;
121 pcmbuf
= *outbuf
+ ch
;
122 adpcms
= *adpcmbuf
++;
123 adpcms
|= (*adpcmbuf
++) << 8;
124 adpcms
|= (*adpcmbuf
++) << 16;
125 for (i
= 0; i
< 8; i
++)
127 *pcmbuf
= create_pcmdata(ch
, adpcms
>> (3 * i
)) << IMA_ADPCM_INC_DEPTH
;
128 pcmbuf
+= fmt
->channels
;
130 adpcms
= *adpcmbuf
++;
131 adpcmbuf
+= (fmt
->channels
- 1) * 4;
132 adpcms
|= (*adpcmbuf
++) << 8;
133 adpcms
|= (*adpcmbuf
++) << 16;
134 for (i
= 0; i
< 8; i
++)
136 *pcmbuf
= create_pcmdata(ch
, adpcms
>> (3 * i
)) << IMA_ADPCM_INC_DEPTH
;
137 pcmbuf
+= fmt
->channels
;
139 adpcms
= *adpcmbuf
++;
140 adpcms
|= (*adpcmbuf
++) << 8;
141 adpcmbuf
+= (fmt
->channels
- 1) * 4;
142 adpcms
|= (*adpcmbuf
++) << 16;
143 for (i
= 0; i
< 8; i
++)
145 *pcmbuf
= create_pcmdata(ch
, adpcms
>> (3 * i
)) << IMA_ADPCM_INC_DEPTH
;
146 pcmbuf
+= fmt
->channels
;
148 adpcms
= *adpcmbuf
++;
149 adpcms
|= (*adpcmbuf
++) << 8;
150 adpcms
|= (*adpcmbuf
++) << 16;
151 for (i
= 0; i
< 8; i
++)
153 *pcmbuf
= create_pcmdata(ch
, adpcms
>> (3 * i
)) << IMA_ADPCM_INC_DEPTH
;
154 pcmbuf
+= fmt
->channels
;
157 *outbuf
+= 32 * fmt
->channels
;
158 *inbuf
+= 12 * fmt
->channels
;
162 static inline void decode_4bit(const uint8_t **inbuf
, size_t inbufsize
,
163 int32_t **outbuf
, int *outbufcount
)
170 samples
= inbufsize
/ (4 * fmt
->channels
) - 1;
171 *outbufcount
+= (samples
<< 3);
172 while (samples
-- > 0)
174 for (ch
= 0; ch
< fmt
->channels
; ch
++)
176 pcmbuf
= *outbuf
+ ch
;
177 for (i
= 0; i
< 4; i
++)
179 *pcmbuf
= create_pcmdata_size4(ch
, **inbuf
) << IMA_ADPCM_INC_DEPTH
;
180 pcmbuf
+= fmt
->channels
;
181 *pcmbuf
= create_pcmdata_size4(ch
, **inbuf
>> 4) << IMA_ADPCM_INC_DEPTH
;
182 pcmbuf
+= fmt
->channels
;
186 *outbuf
+= 8 * fmt
->channels
;
190 static inline void decode_5bit(const uint8_t **inbuf
, size_t inbufsize
,
191 int32_t **outbuf
, int *outbufcount
)
193 const uint8_t *adpcmbuf
;
200 samples
= (inbufsize
- 4 * fmt
->channels
) / (20 * fmt
->channels
);
201 *outbufcount
+= (samples
<< 5);
204 for (ch
= 0; ch
< fmt
->channels
; ch
++)
206 adpcmbuf
= *inbuf
+ ch
* 4;
207 pcmbuf
= *outbuf
+ ch
;
208 adpcms
= *adpcmbuf
++;
209 adpcms
|= (*adpcmbuf
++) << 8;
210 adpcms
|= (*adpcmbuf
++) << 16;
211 adpcms
|= (uint64_t)(*adpcmbuf
++) << 24;
212 adpcmbuf
+= (fmt
->channels
- 1) * 4;
213 adpcms
|= (uint64_t)(*adpcmbuf
++) << 32;
214 for (i
= 0; i
< 8; i
++)
216 *pcmbuf
= create_pcmdata(ch
, adpcms
>> (5 * i
)) << IMA_ADPCM_INC_DEPTH
;
217 pcmbuf
+= fmt
->channels
;
219 adpcms
= *adpcmbuf
++;
220 adpcms
|= (*adpcmbuf
++) << 8;
221 adpcms
|= (*adpcmbuf
++) << 16;
222 adpcmbuf
+= (fmt
->channels
- 1) * 4;
223 adpcms
|= (uint64_t)(*adpcmbuf
++) << 24;
224 adpcms
|= (uint64_t)(*adpcmbuf
++) << 32;
225 for (i
= 0; i
< 8; i
++)
227 *pcmbuf
= create_pcmdata(ch
, adpcms
>> (5 * i
)) << IMA_ADPCM_INC_DEPTH
;
228 pcmbuf
+= fmt
->channels
;
230 adpcms
= *adpcmbuf
++;
231 adpcms
|= (*adpcmbuf
++) << 8;
232 adpcmbuf
+= (fmt
->channels
- 1) * 4;
233 adpcms
|= (*adpcmbuf
++) << 16;
234 adpcms
|= (uint64_t)(*adpcmbuf
++) << 24;
235 adpcms
|= (uint64_t)(*adpcmbuf
++) << 32;
236 for (i
= 0; i
< 8; i
++)
238 *pcmbuf
= create_pcmdata(ch
, adpcms
>> (5 * i
)) << IMA_ADPCM_INC_DEPTH
;
239 pcmbuf
+= fmt
->channels
;
241 adpcms
= *adpcmbuf
++;
242 adpcmbuf
+= (fmt
->channels
- 1) * 4;
243 adpcms
|= (*adpcmbuf
++) << 8;
244 adpcms
|= (*adpcmbuf
++) << 16;
245 adpcms
|= (uint64_t)(*adpcmbuf
++) << 24;
246 adpcms
|= (uint64_t)(*adpcmbuf
++) << 32;
247 for (i
= 0; i
< 8; i
++)
249 *pcmbuf
= create_pcmdata(ch
, adpcms
>> (5 * i
)) << IMA_ADPCM_INC_DEPTH
;
250 pcmbuf
+= fmt
->channels
;
253 *outbuf
+= 32 * fmt
->channels
;
254 *inbuf
+= 20 * fmt
->channels
;
258 static int decode(const uint8_t *inbuf
, size_t inbufsize
,
259 int32_t *outbuf
, int *outbufcount
)
262 int32_t init_pcmdata
[2];
263 int8_t init_index
[2];
266 for (ch
= 0; ch
< fmt
->channels
; ch
++)
268 init_pcmdata
[ch
] = inbuf
[0] | (inbuf
[1] << 8);
269 if (init_pcmdata
[ch
] > 32767)
270 init_pcmdata
[ch
] -= 65536;
272 init_index
[ch
] = inbuf
[2];
273 if (init_index
[ch
] > 88 || init_index
[ch
] < 0)
275 DEBUGF("CODEC_ERROR: dvi adpcm illegal step index=%d > 88\n",
281 *outbuf
++ = init_pcmdata
[ch
] << IMA_ADPCM_INC_DEPTH
;
285 set_decode_parameters(fmt
->channels
, init_pcmdata
, init_index
);
287 if (fmt
->bitspersample
== 4)
288 decode_4bit(&inbuf
, inbufsize
, &outbuf
, outbufcount
);
289 else if (fmt
->bitspersample
== 3)
290 decode_3bit(&inbuf
, inbufsize
, &outbuf
, outbufcount
);
291 else if (fmt
->bitspersample
== 5)
292 decode_5bit(&inbuf
, inbufsize
, &outbuf
, outbufcount
);
293 else /* fmt->bitspersample == 2 */
294 decode_2bit(&inbuf
, inbufsize
, &outbuf
, outbufcount
);
299 static const struct pcm_codec codec
= {
305 const struct pcm_codec
*get_dvi_adpcm_codec(void)