Mercurial > mplayer.hg
annotate libmpcodecs/ad_imaadpcm.c @ 6979:e7748ba18900
Cosmetics: Properly indented table and fixed <TR> structure.
author | diego |
---|---|
date | Sun, 11 Aug 2002 20:38:34 +0000 |
parents | 8ae2bf330ad5 |
children | 28677d779205 |
rev | line source |
---|---|
5408 | 1 /* |
2 IMA ADPCM Decoder for MPlayer | |
3 by Mike Melanson | |
4 | |
5 This file is in charge of decoding all of the various IMA ADPCM data | |
6 formats that various entities have created. Details about the data | |
7 formats can be found here: | |
8 http://www.pcisys.net/~melanson/codecs/ | |
9 | |
10 So far, this file handles these formats: | |
11 'ima4': IMA ADPCM found in QT files | |
12 0x11: IMA ADPCM found in MS AVI/ASF/WAV files | |
13 0x61: DK4 ADPCM found in certain AVI files on Sega Saturn CD-ROMs; | |
14 note that this is a 'rogue' format number in that it was | |
15 never officially registered with Microsoft | |
16 */ | |
17 | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
18 #include <stdio.h> |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
19 #include <stdlib.h> |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
20 #include <unistd.h> |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
21 |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
22 #include "config.h" |
5408 | 23 #include "bswap.h" |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
24 #include "ad_internal.h" |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
25 |
5408 | 26 #define MS_IMA_ADPCM_PREAMBLE_SIZE 4 |
27 | |
28 #define QT_IMA_ADPCM_PREAMBLE_SIZE 2 | |
29 #define QT_IMA_ADPCM_BLOCK_SIZE 0x22 | |
30 #define QT_IMA_ADPCM_SAMPLES_PER_BLOCK 64 | |
31 | |
32 #define BE_16(x) (be2me_16(*(unsigned short *)(x))) | |
33 #define BE_32(x) (be2me_32(*(unsigned int *)(x))) | |
34 #define LE_16(x) (le2me_16(*(unsigned short *)(x))) | |
35 #define LE_32(x) (le2me_32(*(unsigned int *)(x))) | |
36 | |
37 // pertinent tables for IMA ADPCM | |
38 static int adpcm_step[89] = | |
39 { | |
40 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, | |
41 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, | |
42 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, | |
43 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, | |
44 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, | |
45 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, | |
46 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, | |
47 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, | |
48 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 | |
49 }; | |
50 | |
51 static int adpcm_index[16] = | |
52 { | |
53 -1, -1, -1, -1, 2, 4, 6, 8, | |
54 -1, -1, -1, -1, 2, 4, 6, 8 | |
55 }; | |
56 | |
57 // useful macros | |
58 // clamp a number between 0 and 88 | |
59 #define CLAMP_0_TO_88(x) if (x < 0) x = 0; else if (x > 88) x = 88; | |
60 // clamp a number within a signed 16-bit range | |
61 #define CLAMP_S16(x) if (x < -32768) x = -32768; \ | |
62 else if (x > 32767) x = 32767; | |
63 // clamp a number above 16 | |
64 #define CLAMP_ABOVE_16(x) if (x < 16) x = 16; | |
65 // sign extend a 16-bit value | |
66 #define SE_16BIT(x) if (x & 0x8000) x -= 0x10000; | |
67 // sign extend a 4-bit value | |
68 #define SE_4BIT(x) if (x & 0x8) x -= 0x10; | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
69 |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
70 static ad_info_t info = |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
71 { |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
72 "IMA ADPCM audio decoder", |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
73 "imaadpcm", |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
74 AFM_IMAADPCM, |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
75 "Nick Kurshev", |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
76 "Mike Melanson", |
5408 | 77 "" |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
78 }; |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
79 |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
80 LIBAD_EXTERN(imaadpcm) |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
81 |
5408 | 82 static int preinit(sh_audio_t *sh_audio) |
83 { | |
84 // not exactly sure what this field is for | |
85 sh_audio->audio_out_minsize = 8192; | |
86 | |
87 // if format is "ima4", assume the audio is coming from a QT file which | |
88 // indicates constant block size, whereas an AVI/ASF/WAV file will fill | |
89 // in this field with 0x11 | |
90 if ((sh_audio->format == 0x11) || (sh_audio->format == 0x61)) | |
91 { | |
92 sh_audio->ds->ss_div = (sh_audio->wf->nBlockAlign - | |
93 (MS_IMA_ADPCM_PREAMBLE_SIZE * sh_audio->wf->nChannels)) * 2; | |
94 sh_audio->ds->ss_mul = sh_audio->wf->nBlockAlign; | |
95 } | |
96 else | |
97 { | |
98 sh_audio->ds->ss_div = QT_IMA_ADPCM_SAMPLES_PER_BLOCK; | |
99 sh_audio->ds->ss_mul = QT_IMA_ADPCM_BLOCK_SIZE * sh_audio->wf->nChannels; | |
100 } | |
5458 | 101 sh_audio->audio_in_minsize=sh_audio->ds->ss_mul; |
5408 | 102 return 1; |
103 } | |
104 | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
105 static int init(sh_audio_t *sh_audio) |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
106 { |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
107 /* IMA-ADPCM 4:1 audio codec:*/ |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
108 sh_audio->channels=sh_audio->wf->nChannels; |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
109 sh_audio->samplerate=sh_audio->wf->nSamplesPerSec; |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
110 /* decodes 34 byte -> 64 short*/ |
5408 | 111 sh_audio->i_bps = |
112 (sh_audio->ds->ss_mul * sh_audio->samplerate) / sh_audio->ds->ss_div; | |
113 | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
114 return 1; |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
115 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
116 |
5408 | 117 static void uninit(sh_audio_t *sh_audio) |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
118 { |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
119 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
120 |
5481 | 121 static int control(sh_audio_t *sh_audio,int cmd,void* arg, ...) |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
122 { |
5481 | 123 if(cmd==ADCTRL_SKIP_FRAME){ |
124 demux_read_data(sh_audio->ds, sh_audio->a_in_buffer,sh_audio->ds->ss_mul); | |
125 return CONTROL_TRUE; | |
126 } | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
127 return CONTROL_UNKNOWN; |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
128 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
129 |
5408 | 130 static void decode_nibbles(unsigned short *output, |
131 int output_size, int channels, | |
132 int predictor_l, int index_l, | |
133 int predictor_r, int index_r) | |
134 { | |
135 int step[2]; | |
136 int predictor[2]; | |
137 int index[2]; | |
138 int diff; | |
139 int i; | |
140 int sign; | |
141 int delta; | |
142 int channel_number = 0; | |
143 | |
144 step[0] = adpcm_step[index_l]; | |
145 step[1] = adpcm_step[index_r]; | |
146 predictor[0] = predictor_l; | |
147 predictor[1] = predictor_r; | |
148 index[0] = index_l; | |
149 index[1] = index_r; | |
150 | |
151 for (i = 0; i < output_size; i++) | |
152 { | |
153 delta = output[i]; | |
154 | |
155 index[channel_number] += adpcm_index[delta]; | |
156 CLAMP_0_TO_88(index[channel_number]); | |
157 | |
158 sign = delta & 8; | |
159 delta = delta & 7; | |
160 | |
161 diff = step[channel_number] >> 3; | |
162 if (delta & 4) diff += step[channel_number]; | |
163 if (delta & 2) diff += step[channel_number] >> 1; | |
164 if (delta & 1) diff += step[channel_number] >> 2; | |
165 | |
166 if (sign) | |
167 predictor[channel_number] -= diff; | |
168 else | |
169 predictor[channel_number] += diff; | |
170 | |
171 CLAMP_S16(predictor[channel_number]); | |
172 output[i] = predictor[channel_number]; | |
173 step[channel_number] = adpcm_step[index[channel_number]]; | |
174 | |
175 // toggle channel | |
176 channel_number ^= channels - 1; | |
177 | |
178 } | |
179 } | |
180 | |
181 static int qt_ima_adpcm_decode_block(unsigned short *output, | |
182 unsigned char *input, int channels) | |
183 { | |
184 int initial_predictor_l = 0; | |
185 int initial_predictor_r = 0; | |
186 int initial_index_l = 0; | |
187 int initial_index_r = 0; | |
188 int i; | |
189 | |
190 initial_predictor_l = BE_16(&input[0]); | |
191 initial_index_l = initial_predictor_l; | |
192 | |
193 // mask, sign-extend, and clamp the predictor portion | |
194 initial_predictor_l &= 0xFF80; | |
195 SE_16BIT(initial_predictor_l); | |
196 CLAMP_S16(initial_predictor_l); | |
197 | |
198 // mask and clamp the index portion | |
199 initial_index_l &= 0x7F; | |
200 CLAMP_0_TO_88(initial_index_l); | |
201 | |
202 // handle stereo | |
203 if (channels > 1) | |
204 { | |
205 initial_predictor_r = BE_16(&input[QT_IMA_ADPCM_BLOCK_SIZE]); | |
206 initial_index_r = initial_predictor_r; | |
207 | |
208 // mask, sign-extend, and clamp the predictor portion | |
209 initial_predictor_r &= 0xFF80; | |
210 SE_16BIT(initial_predictor_r); | |
211 CLAMP_S16(initial_predictor_r); | |
212 | |
213 // mask and clamp the index portion | |
214 initial_index_r &= 0x7F; | |
215 CLAMP_0_TO_88(initial_index_r); | |
216 } | |
217 | |
218 // break apart all of the nibbles in the block | |
219 if (channels == 1) | |
220 for (i = 0; i < QT_IMA_ADPCM_SAMPLES_PER_BLOCK / 2; i++) | |
221 { | |
222 output[i * 2 + 0] = input[2 + i] & 0x0F; | |
223 output[i * 2 + 1] = input[2 + i] >> 4; | |
224 } | |
225 else | |
226 for (i = 0; i < QT_IMA_ADPCM_SAMPLES_PER_BLOCK / 2 * 2; i++) | |
227 { | |
228 output[i * 4 + 0] = input[2 + i] & 0x0F; | |
229 output[i * 4 + 1] = input[2 + QT_IMA_ADPCM_BLOCK_SIZE + i] & 0x0F; | |
230 output[i * 4 + 2] = input[2 + i] >> 4; | |
231 output[i * 4 + 3] = input[2 + QT_IMA_ADPCM_BLOCK_SIZE + i] >> 4; | |
232 } | |
233 | |
234 decode_nibbles(output, | |
235 QT_IMA_ADPCM_SAMPLES_PER_BLOCK * channels, channels, | |
236 initial_predictor_l, initial_index_l, | |
237 initial_predictor_r, initial_index_r); | |
238 | |
239 return QT_IMA_ADPCM_SAMPLES_PER_BLOCK * channels; | |
240 } | |
241 | |
242 static int ms_ima_adpcm_decode_block(unsigned short *output, | |
243 unsigned char *input, int channels, int block_size) | |
244 { | |
245 int predictor_l = 0; | |
246 int predictor_r = 0; | |
247 int index_l = 0; | |
248 int index_r = 0; | |
249 int i; | |
250 int channel_counter; | |
251 int channel_index; | |
252 int channel_index_l; | |
253 int channel_index_r; | |
254 | |
255 predictor_l = LE_16(&input[0]); | |
256 SE_16BIT(predictor_l); | |
257 index_l = input[2]; | |
258 if (channels == 2) | |
259 { | |
260 predictor_r = LE_16(&input[4]); | |
261 SE_16BIT(predictor_r); | |
262 index_r = input[6]; | |
263 } | |
264 | |
265 if (channels == 1) | |
266 for (i = 0; | |
267 i < (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) / 2; i++) | |
268 { | |
269 output[i * 2 + 0] = input[MS_IMA_ADPCM_PREAMBLE_SIZE + i] & 0x0F; | |
270 output[i * 2 + 1] = input[MS_IMA_ADPCM_PREAMBLE_SIZE + i] >> 4; | |
271 } | |
272 else | |
273 { | |
274 // encoded as 8 nibbles (4 bytes) per channel; switch channel every | |
275 // 4th byte | |
276 channel_counter = 0; | |
277 channel_index_l = 0; | |
278 channel_index_r = 1; | |
279 channel_index = channel_index_l; | |
280 for (i = 0; | |
281 i < (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels); i++) | |
282 { | |
283 output[channel_index + 0] = | |
284 input[MS_IMA_ADPCM_PREAMBLE_SIZE * 2 + i] & 0x0F; | |
285 output[channel_index + 2] = | |
286 input[MS_IMA_ADPCM_PREAMBLE_SIZE * 2 + i] >> 4; | |
287 channel_index += 4; | |
288 channel_counter++; | |
289 if (channel_counter == 4) | |
290 { | |
291 channel_index_l = channel_index; | |
292 channel_index = channel_index_r; | |
293 } | |
294 else if (channel_counter == 8) | |
295 { | |
296 channel_index_r = channel_index; | |
297 channel_index = channel_index_l; | |
298 channel_counter = 0; | |
299 } | |
300 } | |
301 } | |
302 | |
303 decode_nibbles(output, | |
304 (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2, | |
305 channels, | |
306 predictor_l, index_l, | |
307 predictor_r, index_r); | |
308 | |
309 return (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2; | |
310 } | |
311 | |
312 static int dk4_ima_adpcm_decode_block(unsigned short *output, | |
313 unsigned char *input, int channels, int block_size) | |
314 { | |
315 int i; | |
316 int output_ptr; | |
317 int predictor_l = 0; | |
318 int predictor_r = 0; | |
319 int index_l = 0; | |
320 int index_r = 0; | |
321 | |
322 // the first predictor value goes straight to the output | |
323 predictor_l = output[0] = LE_16(&input[0]); | |
324 SE_16BIT(predictor_l); | |
325 index_l = input[2]; | |
326 if (channels == 2) | |
327 { | |
328 predictor_r = output[1] = LE_16(&input[4]); | |
329 SE_16BIT(predictor_r); | |
330 index_r = input[6]; | |
331 } | |
332 | |
333 output_ptr = channels; | |
334 for (i = MS_IMA_ADPCM_PREAMBLE_SIZE * channels; i < block_size; i++) | |
335 { | |
336 output[output_ptr++] = input[i] >> 4; | |
337 output[output_ptr++] = input[i] & 0x0F; | |
338 } | |
339 | |
340 decode_nibbles(&output[channels], | |
341 (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2 - channels, | |
342 channels, | |
343 predictor_l, index_l, | |
344 predictor_r, index_r); | |
345 | |
346 return (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2 - channels; | |
347 } | |
348 | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
349 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen) |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
350 { |
5408 | 351 if (demux_read_data(sh_audio->ds, sh_audio->a_in_buffer, |
352 sh_audio->ds->ss_mul) != | |
353 sh_audio->ds->ss_mul) | |
354 return -1; | |
355 | |
356 if (sh_audio->format == 0x11) | |
357 { | |
358 return 2 * ms_ima_adpcm_decode_block( | |
359 (unsigned short*)buf, sh_audio->a_in_buffer, sh_audio->wf->nChannels, | |
360 sh_audio->ds->ss_mul); | |
361 } | |
362 else if (sh_audio->format == 0x61) | |
363 { | |
364 return 2 * dk4_ima_adpcm_decode_block( | |
365 (unsigned short*)buf, sh_audio->a_in_buffer, sh_audio->wf->nChannels, | |
366 sh_audio->ds->ss_mul); | |
367 } | |
368 else | |
369 { | |
370 return 2 * qt_ima_adpcm_decode_block( | |
371 (unsigned short*)buf, sh_audio->a_in_buffer, sh_audio->wf->nChannels); | |
372 } | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
373 } |