annotate adpcm.c @ 5162:3cc3a36e3d0d

xp added
author jaf
date Sun, 17 Mar 2002 15:03:25 +0000
parents 4a6dde59834c
children f9cd6381e327
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3756
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
1 /*
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
2 Unified ADPCM Decoder for MPlayer
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
3
3787
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
4 This file is in charge of decoding all of the various ADPCM data
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
5 formats that various entities have created. Details about the data
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
6 formats can be found here:
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
7 http://www.pcisys.net/~melanson/codecs/
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
8
3756
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
9 (C) 2001 Mike Melanson
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
10 */
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
11
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
12 #include "config.h"
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
13 #include "bswap.h"
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
14 #include "adpcm.h"
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
15 #include "mp_msg.h"
3756
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
16
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
17 #define BE_16(x) (be2me_16(*(unsigned short *)(x)))
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
18 #define BE_32(x) (be2me_32(*(unsigned int *)(x)))
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
19 #define LE_16(x) (le2me_16(*(unsigned short *)(x)))
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
20 #define LE_32(x) (le2me_32(*(unsigned int *)(x)))
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
21
3787
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
22 // pertinent tables
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
23 static int adpcm_step[89] =
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
24 {
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
25 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
26 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
27 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
28 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
29 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
30 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
31 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
32 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
33 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
34 };
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
35
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
36 static int adpcm_index[16] =
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
37 {
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
38 -1, -1, -1, -1, 2, 4, 6, 8,
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
39 -1, -1, -1, -1, 2, 4, 6, 8
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
40 };
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
41
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
42 static int ms_adapt_table[] =
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
43 {
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
44 230, 230, 230, 230, 307, 409, 512, 614,
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
45 768, 614, 512, 409, 307, 230, 230, 230
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
46 };
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
47
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
48 static int ms_adapt_coeff1[] =
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
49 {
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
50 256, 512, 0, 192, 240, 460, 392
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
51 };
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
52
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
53 static int ms_adapt_coeff2[] =
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
54 {
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
55 0, -256, 0, 64, 0, -208, -232
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
56 };
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
57
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
58 // useful macros
3756
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
59 // clamp a number between 0 and 88
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
60 #define CLAMP_0_TO_88(x) if (x < 0) x = 0; else if (x > 88) x = 88;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
61 // clamp a number within a signed 16-bit range
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
62 #define CLAMP_S16(x) if (x < -32768) x = -32768; \
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
63 else if (x > 32767) x = 32767;
3787
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
64 // clamp a number above 16
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
65 #define CLAMP_ABOVE_16(x) if (x < 16) x = 16;
3756
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
66 // sign extend a 16-bit value
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
67 #define SE_16BIT(x) if (x & 0x8000) x -= 0x10000;
3787
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
68 // sign extend a 4-bit value
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
69 #define SE_4BIT(x) if (x & 0x8) x -= 0x10;
3756
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
70
3939
6b31db273596 fixed FOX61 ADPCM; still working on FOX62
melanson
parents: 3933
diff changeset
71 void decode_nibbles(unsigned short *output,
6b31db273596 fixed FOX61 ADPCM; still working on FOX62
melanson
parents: 3933
diff changeset
72 int output_size, int channels,
3756
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
73 int predictor_l, int index_l,
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
74 int predictor_r, int index_r)
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
75 {
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
76 int step[2];
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
77 int predictor[2];
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
78 int index[2];
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
79 int diff;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
80 int i;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
81 int sign;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
82 int delta;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
83 int channel_number = 0;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
84
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
85 step[0] = adpcm_step[index_l];
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
86 step[1] = adpcm_step[index_r];
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
87 predictor[0] = predictor_l;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
88 predictor[1] = predictor_r;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
89 index[0] = index_l;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
90 index[1] = index_r;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
91
3939
6b31db273596 fixed FOX61 ADPCM; still working on FOX62
melanson
parents: 3933
diff changeset
92 for (i = 0; i < output_size; i++)
3756
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
93 {
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
94 delta = output[i];
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
95
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
96 index[channel_number] += adpcm_index[delta];
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
97 CLAMP_0_TO_88(index[channel_number]);
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
98
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
99 sign = delta & 8;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
100 delta = delta & 7;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
101
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
102 diff = step[channel_number] >> 3;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
103 if (delta & 4) diff += step[channel_number];
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
104 if (delta & 2) diff += step[channel_number] >> 1;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
105 if (delta & 1) diff += step[channel_number] >> 2;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
106
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
107 if (sign)
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
108 predictor[channel_number] -= diff;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
109 else
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
110 predictor[channel_number] += diff;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
111
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
112 CLAMP_S16(predictor[channel_number]);
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
113 output[i] = predictor[channel_number];
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
114 step[channel_number] = adpcm_step[index[channel_number]];
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
115
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
116 // toggle channel
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
117 channel_number ^= channels - 1;
3939
6b31db273596 fixed FOX61 ADPCM; still working on FOX62
melanson
parents: 3933
diff changeset
118
3756
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
119 }
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
120 }
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
121
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
122 int ima_adpcm_decode_block(unsigned short *output, unsigned char *input,
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
123 int channels)
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
124 {
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
125 int initial_predictor_l = 0;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
126 int initial_predictor_r = 0;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
127 int initial_index_l = 0;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
128 int initial_index_r = 0;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
129 int i;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
130
3763
81d84039dd17 fixed stereo IMA4 decoding
melanson
parents: 3756
diff changeset
131 initial_predictor_l = BE_16(&input[0]);
3756
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
132 initial_index_l = initial_predictor_l;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
133
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
134 // mask, sign-extend, and clamp the predictor portion
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
135 initial_predictor_l &= 0xFF80;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
136 SE_16BIT(initial_predictor_l);
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
137 CLAMP_S16(initial_predictor_l);
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
138
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
139 // mask and clamp the index portion
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
140 initial_index_l &= 0x7F;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
141 CLAMP_0_TO_88(initial_index_l);
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
142
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
143 // handle stereo
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
144 if (channels > 1)
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
145 {
3763
81d84039dd17 fixed stereo IMA4 decoding
melanson
parents: 3756
diff changeset
146 initial_predictor_r = BE_16(&input[IMA_ADPCM_BLOCK_SIZE]);
3756
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
147 initial_index_r = initial_predictor_r;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
148
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
149 // mask, sign-extend, and clamp the predictor portion
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
150 initial_predictor_r &= 0xFF80;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
151 SE_16BIT(initial_predictor_r);
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
152 CLAMP_S16(initial_predictor_r);
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
153
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
154 // mask and clamp the index portion
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
155 initial_index_r &= 0x7F;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
156 CLAMP_0_TO_88(initial_index_r);
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
157 }
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
158
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
159 // break apart all of the nibbles in the block
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
160 if (channels == 1)
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
161 for (i = 0; i < IMA_ADPCM_SAMPLES_PER_BLOCK / 2; i++)
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
162 {
3763
81d84039dd17 fixed stereo IMA4 decoding
melanson
parents: 3756
diff changeset
163 output[i * 2 + 0] = input[2 + i] & 0x0F;
81d84039dd17 fixed stereo IMA4 decoding
melanson
parents: 3756
diff changeset
164 output[i * 2 + 1] = input[2 + i] >> 4;
3756
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
165 }
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
166 else
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
167 for (i = 0; i < IMA_ADPCM_SAMPLES_PER_BLOCK / 2 * 2; i++)
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
168 {
3763
81d84039dd17 fixed stereo IMA4 decoding
melanson
parents: 3756
diff changeset
169 output[i * 4 + 0] = input[2 + i] & 0x0F;
81d84039dd17 fixed stereo IMA4 decoding
melanson
parents: 3756
diff changeset
170 output[i * 4 + 1] = input[2 + IMA_ADPCM_BLOCK_SIZE + i] & 0x0F;
81d84039dd17 fixed stereo IMA4 decoding
melanson
parents: 3756
diff changeset
171 output[i * 4 + 2] = input[2 + i] >> 4;
81d84039dd17 fixed stereo IMA4 decoding
melanson
parents: 3756
diff changeset
172 output[i * 4 + 3] = input[2 + IMA_ADPCM_BLOCK_SIZE + i] >> 4;
3756
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
173 }
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
174
3939
6b31db273596 fixed FOX61 ADPCM; still working on FOX62
melanson
parents: 3933
diff changeset
175 decode_nibbles(output,
6b31db273596 fixed FOX61 ADPCM; still working on FOX62
melanson
parents: 3933
diff changeset
176 IMA_ADPCM_SAMPLES_PER_BLOCK * channels, channels,
3756
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
177 initial_predictor_l, initial_index_l,
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
178 initial_predictor_r, initial_index_r);
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
179
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
180 return IMA_ADPCM_SAMPLES_PER_BLOCK * channels;
734d0c0a8ab0 Initial support for unified ADPCM decoder
melanson
parents:
diff changeset
181 }
3787
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
182
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
183 int ms_adpcm_decode_block(unsigned short *output, unsigned char *input,
3875
e3caff2daa98 fixed stereo MS ADPCM decoder and reinstated opensource decoder as the
melanson
parents: 3826
diff changeset
184 int channels, int block_size)
3787
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
185 {
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
186 int current_channel = 0;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
187 int idelta[2];
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
188 int sample1[2];
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
189 int sample2[2];
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
190 int coeff1[2];
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
191 int coeff2[2];
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
192 int stream_ptr = 0;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
193 int out_ptr = 0;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
194 int upper_nibble = 1;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
195 int nibble;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
196 int snibble; // signed nibble
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
197 int predictor;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
198
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
199 // fetch the header information, in stereo if both channels are present
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
200 if (input[stream_ptr] > 6)
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
201 mp_msg(MSGT_DECAUDIO, MSGL_WARN,
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
202 "MS ADPCM: coefficient (%d) out of range (should be [0..6])\n",
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
203 input[stream_ptr]);
3787
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
204 coeff1[0] = ms_adapt_coeff1[input[stream_ptr]];
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
205 coeff2[0] = ms_adapt_coeff2[input[stream_ptr]];
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
206 stream_ptr++;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
207 if (channels == 2)
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
208 {
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
209 if (input[stream_ptr] > 6)
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
210 mp_msg(MSGT_DECAUDIO, MSGL_WARN,
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
211 "MS ADPCM: coefficient (%d) out of range (should be [0..6])\n",
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
212 input[stream_ptr]);
3787
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
213 coeff1[1] = ms_adapt_coeff1[input[stream_ptr]];
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
214 coeff2[1] = ms_adapt_coeff2[input[stream_ptr]];
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
215 stream_ptr++;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
216 }
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
217
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
218 idelta[0] = LE_16(&input[stream_ptr]);
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
219 stream_ptr += 2;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
220 SE_16BIT(idelta[0]);
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
221 if (channels == 2)
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
222 {
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
223 idelta[1] = LE_16(&input[stream_ptr]);
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
224 stream_ptr += 2;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
225 SE_16BIT(idelta[1]);
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
226 }
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
227
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
228 sample1[0] = LE_16(&input[stream_ptr]);
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
229 stream_ptr += 2;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
230 SE_16BIT(sample1[0]);
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
231 if (channels == 2)
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
232 {
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
233 sample1[1] = LE_16(&input[stream_ptr]);
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
234 stream_ptr += 2;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
235 SE_16BIT(sample1[1]);
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
236 }
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
237
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
238 sample2[0] = LE_16(&input[stream_ptr]);
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
239 stream_ptr += 2;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
240 SE_16BIT(sample2[0]);
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
241 if (channels == 2)
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
242 {
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
243 sample2[1] = LE_16(&input[stream_ptr]);
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
244 stream_ptr += 2;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
245 SE_16BIT(sample2[1]);
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
246 }
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
247
3875
e3caff2daa98 fixed stereo MS ADPCM decoder and reinstated opensource decoder as the
melanson
parents: 3826
diff changeset
248 while (stream_ptr < block_size)
3787
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
249 {
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
250 // get the next nibble
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
251 if (upper_nibble)
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
252 nibble = snibble = input[stream_ptr] >> 4;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
253 else
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
254 nibble = snibble = input[stream_ptr++] & 0x0F;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
255 upper_nibble ^= 1;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
256 SE_4BIT(snibble);
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
257
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
258 predictor = (
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
259 ((sample1[current_channel] * coeff1[current_channel]) +
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
260 (sample2[current_channel] * coeff2[current_channel])) / 256) +
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
261 (snibble * idelta[current_channel]);
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
262 CLAMP_S16(predictor);
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
263 sample2[current_channel] = sample1[current_channel];
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
264 sample1[current_channel] = predictor;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
265 output[out_ptr++] = predictor;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
266
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
267 // compute the next adaptive scale factor (a.k.a. the variable idelta)
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
268 idelta[current_channel] =
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
269 (ms_adapt_table[nibble] * idelta[current_channel]) / 256;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
270 CLAMP_ABOVE_16(idelta[current_channel]);
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
271
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
272 // toggle the channel
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
273 current_channel ^= channels - 1;
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
274 }
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
275
3875
e3caff2daa98 fixed stereo MS ADPCM decoder and reinstated opensource decoder as the
melanson
parents: 3826
diff changeset
276 return (block_size - (MS_ADPCM_PREAMBLE_SIZE * channels)) * 2;
3787
55603340d1b2 implemented open source MS ADPCM decoder
melanson
parents: 3763
diff changeset
277 }
3826
8a88ed2473aa added initial, not-yet-functional, support for fox62 audio
melanson
parents: 3787
diff changeset
278
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
279 int dk4_adpcm_decode_block(unsigned short *output, unsigned char *input,
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
280 int channels, int block_size)
3933
60db4273246d added initial support for format 0x61 ADPCM (sounds good, but still pops)
melanson
parents: 3875
diff changeset
281 {
60db4273246d added initial support for format 0x61 ADPCM (sounds good, but still pops)
melanson
parents: 3875
diff changeset
282 int i;
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
283 int output_ptr;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
284 int predictor_l = 0;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
285 int predictor_r = 0;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
286 int index_l = 0;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
287 int index_r = 0;
3933
60db4273246d added initial support for format 0x61 ADPCM (sounds good, but still pops)
melanson
parents: 3875
diff changeset
288
3939
6b31db273596 fixed FOX61 ADPCM; still working on FOX62
melanson
parents: 3933
diff changeset
289 // the first predictor value goes straight to the output
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
290 predictor_l = output[0] = LE_16(&input[0]);
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
291 SE_16BIT(predictor_l);
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
292 index_l = input[2];
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
293 if (channels == 2)
3933
60db4273246d added initial support for format 0x61 ADPCM (sounds good, but still pops)
melanson
parents: 3875
diff changeset
294 {
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
295 predictor_r = output[1] = LE_16(&input[4]);
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
296 SE_16BIT(predictor_r);
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
297 index_r = input[6];
3939
6b31db273596 fixed FOX61 ADPCM; still working on FOX62
melanson
parents: 3933
diff changeset
298 }
3933
60db4273246d added initial support for format 0x61 ADPCM (sounds good, but still pops)
melanson
parents: 3875
diff changeset
299
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
300 output_ptr = channels;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
301 for (i = DK4_ADPCM_PREAMBLE_SIZE * channels; i < block_size; i++)
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
302 {
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
303 output[output_ptr++] = input[i] >> 4;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
304 output[output_ptr++] = input[i] & 0x0F;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
305 }
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
306
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
307 decode_nibbles(&output[channels],
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
308 (block_size - DK4_ADPCM_PREAMBLE_SIZE * channels) * 2 - channels,
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
309 channels,
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
310 predictor_l, index_l,
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
311 predictor_r, index_r);
3933
60db4273246d added initial support for format 0x61 ADPCM (sounds good, but still pops)
melanson
parents: 3875
diff changeset
312
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
313 return (block_size - DK4_ADPCM_PREAMBLE_SIZE * channels) * 2 - channels;
3933
60db4273246d added initial support for format 0x61 ADPCM (sounds good, but still pops)
melanson
parents: 3875
diff changeset
314 }
60db4273246d added initial support for format 0x61 ADPCM (sounds good, but still pops)
melanson
parents: 3875
diff changeset
315
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
316 #define DK3_GET_NEXT_NIBBLE() \
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
317 if (decode_top_nibble_next) \
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
318 { \
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
319 nibble = (last_byte >> 4) & 0x0F; \
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
320 decode_top_nibble_next = 0; \
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
321 } \
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
322 else \
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
323 { \
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
324 last_byte = input[in_ptr++]; \
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
325 nibble = last_byte & 0x0F; \
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
326 decode_top_nibble_next = 1; \
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
327 }
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
328
3826
8a88ed2473aa added initial, not-yet-functional, support for fox62 audio
melanson
parents: 3787
diff changeset
329 // note: This decoder assumes the format 0x62 data always comes in
8a88ed2473aa added initial, not-yet-functional, support for fox62 audio
melanson
parents: 3787
diff changeset
330 // stereo flavor
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
331 int dk3_adpcm_decode_block(unsigned short *output, unsigned char *input)
3826
8a88ed2473aa added initial, not-yet-functional, support for fox62 audio
melanson
parents: 3787
diff changeset
332 {
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
333 int sum_pred;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
334 int diff_pred;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
335 int sum_index;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
336 int diff_index;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
337 int diff_channel;
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
338 int in_ptr = 0x10;
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
339 int out_ptr = 0;
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
340
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
341 unsigned char last_byte = 0;
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
342 unsigned char nibble;
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
343 int decode_top_nibble_next = 0;
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
344
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
345 // ADPCM work variables
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
346 int sign;
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
347 int delta;
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
348 int step;
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
349 int diff;
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
350
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
351 sum_pred = LE_16(&input[10]);
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
352 diff_pred = LE_16(&input[12]);
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
353 SE_16BIT(sum_pred);
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
354 SE_16BIT(diff_pred);
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
355 diff_channel = diff_pred;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
356 sum_index = input[14];
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
357 diff_index = input[15];
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
358
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
359 while (in_ptr < 2048)
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
360 {
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
361 // process the first predictor of the sum channel
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
362 DK3_GET_NEXT_NIBBLE();
3826
8a88ed2473aa added initial, not-yet-functional, support for fox62 audio
melanson
parents: 3787
diff changeset
363
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
364 step = adpcm_step[sum_index];
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
365
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
366 sign = nibble & 8;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
367 delta = nibble & 7;
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
368
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
369 diff = step >> 3;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
370 if (delta & 4) diff += step;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
371 if (delta & 2) diff += step >> 1;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
372 if (delta & 1) diff += step >> 2;
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
373
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
374 if (sign)
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
375 sum_pred -= diff;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
376 else
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
377 sum_pred += diff;
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
378
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
379 CLAMP_S16(sum_pred);
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
380
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
381 sum_index += adpcm_index[nibble];
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
382 CLAMP_0_TO_88(sum_index);
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
383
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
384 // process the diff channel predictor
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
385 DK3_GET_NEXT_NIBBLE();
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
386
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
387 step = adpcm_step[diff_index];
3826
8a88ed2473aa added initial, not-yet-functional, support for fox62 audio
melanson
parents: 3787
diff changeset
388
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
389 sign = nibble & 8;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
390 delta = nibble & 7;
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
391
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
392 diff = step >> 3;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
393 if (delta & 4) diff += step;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
394 if (delta & 2) diff += step >> 1;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
395 if (delta & 1) diff += step >> 2;
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
396
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
397 if (sign)
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
398 diff_pred -= diff;
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
399 else
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
400 diff_pred += diff;
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
401
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
402 CLAMP_S16(diff_pred);
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
403
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
404 diff_index += adpcm_index[nibble];
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
405 CLAMP_0_TO_88(diff_index);
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
406
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
407 // output the first pair of stereo PCM samples
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
408 diff_channel = (diff_channel + diff_pred) / 2;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
409 output[out_ptr++] = sum_pred + diff_channel;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
410 output[out_ptr++] = sum_pred - diff_channel;
3826
8a88ed2473aa added initial, not-yet-functional, support for fox62 audio
melanson
parents: 3787
diff changeset
411
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
412 // process the second predictor of the sum channel
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
413 DK3_GET_NEXT_NIBBLE();
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
414
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
415 step = adpcm_step[sum_index];
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
416
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
417 sign = nibble & 8;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
418 delta = nibble & 7;
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
419
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
420 diff = step >> 3;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
421 if (delta & 4) diff += step;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
422 if (delta & 2) diff += step >> 1;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
423 if (delta & 1) diff += step >> 2;
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
424
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
425 if (sign)
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
426 sum_pred -= diff;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
427 else
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
428 sum_pred += diff;
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
429
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
430 CLAMP_S16(sum_pred);
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
431
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
432 sum_index += adpcm_index[nibble];
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
433 CLAMP_0_TO_88(sum_index);
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
434
4854
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
435 // output the second pair of stereo PCM samples
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
436 output[out_ptr++] = sum_pred + diff_channel;
4a6dde59834c fixed, strengthened, rewrote, and renamed a variety of the ADPCM decoders
melanson
parents: 4001
diff changeset
437 output[out_ptr++] = sum_pred - diff_channel;
3826
8a88ed2473aa added initial, not-yet-functional, support for fox62 audio
melanson
parents: 3787
diff changeset
438 }
8a88ed2473aa added initial, not-yet-functional, support for fox62 audio
melanson
parents: 3787
diff changeset
439
4001
ae6f97724b84 fixed format 0x62 ADPCM audio
melanson
parents: 3939
diff changeset
440 return out_ptr;
3939
6b31db273596 fixed FOX61 ADPCM; still working on FOX62
melanson
parents: 3933
diff changeset
441 }