annotate adpcm.c @ 4144:501620ec9b40

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