6 // Note: DVI & IMA formats are identical
8 #include "wave_ima_adpcm.h"
12 struct IMA_ADPCM_Format
{
17 WORD blockAlign
; /* amount to read for each block */
18 WORD bitsPerSample
; /* 3 or 4 */
20 WORD extraSize
; /* 2 */
24 DEC_SETUPPROTO(SetupIMA_ADPCM
) {
25 struct IMA_ADPCM_Format
* fmt
;
27 fmt
= (struct IMA_ADPCM_Format
*)data
->fmt
;
29 /* check bitsPerSample */
30 if (fmt
->bitsPerSample
!= 3 && fmt
->bitsPerSample
!= 4) {
31 return DTERROR_UNKNOWN_COMPRESSION
;
33 if (data
->chunk
.size
!= sizeof(*fmt
)) {
36 data
->blockFrames
= fmt
->samplesPerBlock
= read_le16(&fmt
->samplesPerBlock
);
40 static const WORD steptab
[89] = {
41 7, 8, 9, 10, 11, 12, 13, 14,
42 16, 17, 19, 21, 23, 25, 28, 31,
43 34, 37, 41, 45, 50, 55, 60, 66,
44 73, 80, 88, 97, 107, 118, 130, 143,
45 157, 173, 190, 209, 230, 253, 279, 307,
46 337, 371, 408, 449, 494, 544, 598, 658,
47 724, 796, 876, 963, 1060, 1166, 1282, 1411,
48 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
49 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484,
50 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
51 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794,
56 static const BYTE indextab3
[8] = {
62 static const BYTE indextab4
[16] = {
63 -1, -1, -1, -1, 2, 4, 6, 8,
64 -1, -1, -1, -1, 2, 4, 6, 8
67 /* Each block has nchannels of these */
68 struct IMA_BlockHeader
{
69 UBYTE isamp0
[2]; /* Prev sample ro start decoding with (int) */
70 BYTE stepTableIndex
; /* Current index in steptable array (0-88) */
74 DECODERPROTO(DecodeIMA_ADPCM
) {
75 LONG Index
[MAX_CHANNELS
],Diff
,Value
[MAX_CHANNELS
];
77 LONG sampleSize
,frame
,frame2
,chan
;
78 LONG bframes
,skip
=0,t
;
82 sampleSize
= fmt
->bitsPerSample
;
84 if (sampleSize
== 3) {
92 for (chan
=0;chan
<fmt
->numChannels
;chan
++) {
93 Value
[chan
] = (WORD
)read_le16(Src
);Src
+=2;
94 Index
[chan
] = *Src
;Src
+=2;
95 *Dst
[chan
]++ = Value
[chan
]>>8;
98 bitpack_init_lsb(&b
, Src
, 0);
100 for (frame
=1;frame
<numFrames
;frame
+=bframes
) {
103 skip
= t
* sampleSize
;
106 for (chan
=0;chan
<fmt
->numChannels
;chan
++) {
107 for (frame2
=0;frame2
<bframes
;frame2
++) {
109 /* Step 1 - Get delta value */
111 Code
=bitpack_read_lsb(&b
, sampleSize
);
113 /* Step 2 - Calculate difference and new expected value */
117 Step
= steptab
[Index
[chan
]];
119 for (mask
=1<<(sampleSize
-2);mask
;mask
>>=1,Step
>>=1) {
125 if (Code
& (1<<(sampleSize
-1))) {
127 if (Value
[chan
]<-32678) Value
[chan
]=-32678;
130 if (Value
[chan
]>32767) Value
[chan
]=32767;
134 /* Step 3 - Find next index value */
136 Index
[chan
]+=indextab
[Code
];
138 if (Index
[chan
]<0) Index
[chan
]=0;
139 if (Index
[chan
]>88) Index
[chan
]=88;
141 /* Step 4 - Output value */
143 *Dst
[chan
]++ = Value
[chan
]>>8;
147 bitpack_seek_lsb(&b
,OFFSET_CURRENT
,skip
);