1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
#include "avi_ima_adpcm_decoder.h"
#include "../f263/BitReader.h"
#pragma pack(push, 1)
struct ima_adpcm_format
{
nsavi::audio_format format;
uint16_t samples_per_block;
};
#pragma pack(pop)
IMA_ADPCM_AVIDecoder::IMA_ADPCM_AVIDecoder(const ima_adpcm_format *adpcmformat, const nsavi::STRH *stream_header) : adpcmformat(adpcmformat), stream_header(stream_header)
{
}
int IMA_ADPCM_AVIDecoder::OutputFrameSize(size_t *frame_size)
{
int channels = adpcmformat->format.channels;
*frame_size = ((adpcmformat->format.block_align - 7*channels)*2 + 2*channels) * 2;
return AVI_SUCCESS;
}
int IMA_ADPCM_AVIDecoder::GetOutputProperties(unsigned int *sampleRate, unsigned int *channels, unsigned int *bitsPerSample, bool *isFloat)
{
if (adpcmformat)
{
*sampleRate = adpcmformat->format.sample_rate;
*channels = adpcmformat->format.channels;
*bitsPerSample = 16;
*isFloat = false;
return AVI_SUCCESS;
}
else
{
return AVI_FAILURE;
}
}
static int index_table[16] = {
-1, -1, -1, -1, 2, 4, 6, 8,
-1, -1, -1, -1, 2, 4, 6, 8
};
static int step_table[89] = {
7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
};
int IMA_ADPCM_AVIDecoder::DecodeChunk(uint16_t type, void **inputBuffer, size_t *inputBufferBytes, void *outputBuffer, size_t *outputBufferBytes)
{
if (adpcmformat->format.channels == 1)
{
size_t adpcm_stream_length = *inputBufferBytes;
if (adpcm_stream_length < adpcmformat->format.block_align) // i'm not even going to consider the possibility of adpcm frames split across avi chunks
return AVI_FAILURE;
adpcm_stream_length = adpcmformat->format.block_align; // do one block at a time, in_avi will call us again
if (adpcm_stream_length < 7)
return AVI_FAILURE;
int16_t *out16 = (int16_t *)outputBuffer;
size_t out16_length = *outputBufferBytes/2;
const uint8_t *adpcm_data = (const uint8_t *)(*inputBuffer);
int predictor = *(int16_t *)adpcm_data;
*out16++ = predictor;
adpcm_data+=2;
out16_length--;
int step_index = *adpcm_data;
if (step_index > 88)
return AVI_FAILURE;
adpcm_data+=2;
BitReader reader;
reader.data = adpcm_data;
reader.numBits = (uint32_t)(*inputBufferBytes - 4)*8;
while (reader.numBits >= 8 && out16_length)
{
int diff, step, nibble;
step = step_table[step_index];
nibble = reader.getbits(4);
step_index += index_table[nibble];
step_index = min(step_index, 88);
step_index = max(step_index, 0);
diff = step>>3;
if(nibble&4)
diff += step;
if(nibble&2)
diff += step>>1;
if(nibble&1)
diff += step>>2;
if (nibble&8)
predictor -= diff;
else
predictor += diff;
predictor = min(predictor, 32767);
predictor = max(predictor, -32768);
*out16++ = predictor;
out16_length--;
}
*inputBuffer = (uint8_t *)(*inputBuffer) + adpcm_stream_length;
*inputBufferBytes -= adpcm_stream_length;
*outputBufferBytes = adpcmformat->samples_per_block*2;
return AVI_SUCCESS;
}
else if (adpcmformat->format.channels == 2)
{
size_t adpcm_stream_length = *inputBufferBytes;
if (adpcm_stream_length < adpcmformat->format.block_align) // i'm not even going to consider the possibility of adpcm frames split across avi chunks
return AVI_FAILURE;
adpcm_stream_length = adpcmformat->format.block_align; // do one block at a time, in_avi will call us again
if (adpcm_stream_length < 8)
return AVI_FAILURE;
int16_t *out16 = (int16_t *)outputBuffer;
size_t out16_length = *outputBufferBytes/2;
const uint8_t *adpcm_data = (const uint8_t *)(*inputBuffer);
int predictor_left = *(int16_t *)adpcm_data;
*out16++ = predictor_left;
adpcm_data+=2;
out16_length--;
int step_index_left = *adpcm_data;
if (step_index_left > 88)
return AVI_FAILURE;
adpcm_data+=2;
int predictor_right = *(int16_t *)adpcm_data;
*out16++ = predictor_right;
adpcm_data+=2;
out16_length--;
int step_index_right = *adpcm_data;
if (step_index_right > 88)
return AVI_FAILURE;
adpcm_data+=2;
BitReader reader;
reader.data = adpcm_data;
reader.numBits = (uint32_t)(*inputBufferBytes - 8)*8;
while (reader.numBits >= 8 && out16_length > 15)
{
int nibbles_left[8] = {0};
int nibbles_right[8] = {0};
for (int i=0;i<8;i++)
nibbles_left[i] = reader.getbits(4);
for (int i=0;i<8;i++)
nibbles_right[i] = reader.getbits(4);
for (int i=0;i<8;i++)
{
int diff, step, nibble;
step = step_table[step_index_left];
nibble = nibbles_left[i];
step_index_left += index_table[nibble];
step_index_left = min(step_index_left, 88);
step_index_left = max(step_index_left, 0);
diff = step>>3;
if(nibble&4)
diff += step;
if(nibble&2)
diff += step>>1;
if(nibble&1)
diff += step>>2;
if (nibble&8)
predictor_left -= diff;
else
predictor_left += diff;
predictor_left = min(predictor_left, 32767);
predictor_left = max(predictor_left, -32768);
*out16++ = predictor_left;
out16_length--;
step = step_table[step_index_right];
nibble =nibbles_right[i];
step_index_right += index_table[nibble];
step_index_right = min(step_index_right, 88);
step_index_right = max(step_index_right, 0);
diff = step>>3;
if(nibble&4)
diff += step;
if(nibble&2)
diff += step>>1;
if(nibble&1)
diff += step>>2;
if (nibble&8)
predictor_right -= diff;
else
predictor_right += diff;
predictor_right = min(predictor_right, 32767);
predictor_right = max(predictor_right, -32768);
*out16++ = predictor_right;
out16_length--;
}
}
*inputBuffer = (uint8_t *)(*inputBuffer) + adpcm_stream_length;
*inputBufferBytes -= adpcm_stream_length;
*outputBufferBytes = adpcmformat->samples_per_block*4;
return AVI_SUCCESS;
}
return AVI_FAILURE;
}
void IMA_ADPCM_AVIDecoder::Close()
{
delete this;
}
#define CBCLASS IMA_ADPCM_AVIDecoder
START_DISPATCH;
CB(OUTPUT_FRAME_SIZE, OutputFrameSize)
CB(GET_OUTPUT_PROPERTIES, GetOutputProperties)
CB(DECODE_CHUNK, DecodeChunk)
VCB(CLOSE, Close)
END_DISPATCH;
#undef CBCLASS
|