Mercurial > mplayer.hg
annotate libmpcodecs/ad_imaadpcm.c @ 7359:7b929a5d753e
GTF update: Fixes "known parametrs" even when fbdev unused
patch by Rudolf Marek <MAREKR2@cs.felk.cvut.cz>
author | arpi |
---|---|
date | Tue, 10 Sep 2002 20:40:49 +0000 |
parents | 28677d779205 |
children | d2d2e01343ca |
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 "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
|
75 "Mike Melanson", |
5408 | 76 "" |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
77 }; |
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 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
|
80 |
5408 | 81 static int preinit(sh_audio_t *sh_audio) |
82 { | |
83 // not exactly sure what this field is for | |
84 sh_audio->audio_out_minsize = 8192; | |
85 | |
86 // if format is "ima4", assume the audio is coming from a QT file which | |
87 // indicates constant block size, whereas an AVI/ASF/WAV file will fill | |
88 // in this field with 0x11 | |
89 if ((sh_audio->format == 0x11) || (sh_audio->format == 0x61)) | |
90 { | |
91 sh_audio->ds->ss_div = (sh_audio->wf->nBlockAlign - | |
92 (MS_IMA_ADPCM_PREAMBLE_SIZE * sh_audio->wf->nChannels)) * 2; | |
93 sh_audio->ds->ss_mul = sh_audio->wf->nBlockAlign; | |
94 } | |
95 else | |
96 { | |
97 sh_audio->ds->ss_div = QT_IMA_ADPCM_SAMPLES_PER_BLOCK; | |
98 sh_audio->ds->ss_mul = QT_IMA_ADPCM_BLOCK_SIZE * sh_audio->wf->nChannels; | |
99 } | |
5458 | 100 sh_audio->audio_in_minsize=sh_audio->ds->ss_mul; |
5408 | 101 return 1; |
102 } | |
103 | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
104 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
|
105 { |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
106 /* 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
|
107 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
|
108 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
|
109 /* decodes 34 byte -> 64 short*/ |
5408 | 110 sh_audio->i_bps = |
111 (sh_audio->ds->ss_mul * sh_audio->samplerate) / sh_audio->ds->ss_div; | |
112 | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
113 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
|
114 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
115 |
5408 | 116 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
|
117 { |
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 |
5481 | 120 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
|
121 { |
5481 | 122 if(cmd==ADCTRL_SKIP_FRAME){ |
123 demux_read_data(sh_audio->ds, sh_audio->a_in_buffer,sh_audio->ds->ss_mul); | |
124 return CONTROL_TRUE; | |
125 } | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
126 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
|
127 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
128 |
5408 | 129 static void decode_nibbles(unsigned short *output, |
130 int output_size, int channels, | |
131 int predictor_l, int index_l, | |
132 int predictor_r, int index_r) | |
133 { | |
134 int step[2]; | |
135 int predictor[2]; | |
136 int index[2]; | |
137 int diff; | |
138 int i; | |
139 int sign; | |
140 int delta; | |
141 int channel_number = 0; | |
142 | |
143 step[0] = adpcm_step[index_l]; | |
144 step[1] = adpcm_step[index_r]; | |
145 predictor[0] = predictor_l; | |
146 predictor[1] = predictor_r; | |
147 index[0] = index_l; | |
148 index[1] = index_r; | |
149 | |
150 for (i = 0; i < output_size; i++) | |
151 { | |
152 delta = output[i]; | |
153 | |
154 index[channel_number] += adpcm_index[delta]; | |
155 CLAMP_0_TO_88(index[channel_number]); | |
156 | |
157 sign = delta & 8; | |
158 delta = delta & 7; | |
159 | |
160 diff = step[channel_number] >> 3; | |
161 if (delta & 4) diff += step[channel_number]; | |
162 if (delta & 2) diff += step[channel_number] >> 1; | |
163 if (delta & 1) diff += step[channel_number] >> 2; | |
164 | |
165 if (sign) | |
166 predictor[channel_number] -= diff; | |
167 else | |
168 predictor[channel_number] += diff; | |
169 | |
170 CLAMP_S16(predictor[channel_number]); | |
171 output[i] = predictor[channel_number]; | |
172 step[channel_number] = adpcm_step[index[channel_number]]; | |
173 | |
174 // toggle channel | |
175 channel_number ^= channels - 1; | |
176 | |
177 } | |
178 } | |
179 | |
180 static int qt_ima_adpcm_decode_block(unsigned short *output, | |
181 unsigned char *input, int channels) | |
182 { | |
183 int initial_predictor_l = 0; | |
184 int initial_predictor_r = 0; | |
185 int initial_index_l = 0; | |
186 int initial_index_r = 0; | |
187 int i; | |
188 | |
189 initial_predictor_l = BE_16(&input[0]); | |
190 initial_index_l = initial_predictor_l; | |
191 | |
192 // mask, sign-extend, and clamp the predictor portion | |
193 initial_predictor_l &= 0xFF80; | |
194 SE_16BIT(initial_predictor_l); | |
195 CLAMP_S16(initial_predictor_l); | |
196 | |
197 // mask and clamp the index portion | |
198 initial_index_l &= 0x7F; | |
199 CLAMP_0_TO_88(initial_index_l); | |
200 | |
201 // handle stereo | |
202 if (channels > 1) | |
203 { | |
204 initial_predictor_r = BE_16(&input[QT_IMA_ADPCM_BLOCK_SIZE]); | |
205 initial_index_r = initial_predictor_r; | |
206 | |
207 // mask, sign-extend, and clamp the predictor portion | |
208 initial_predictor_r &= 0xFF80; | |
209 SE_16BIT(initial_predictor_r); | |
210 CLAMP_S16(initial_predictor_r); | |
211 | |
212 // mask and clamp the index portion | |
213 initial_index_r &= 0x7F; | |
214 CLAMP_0_TO_88(initial_index_r); | |
215 } | |
216 | |
217 // break apart all of the nibbles in the block | |
218 if (channels == 1) | |
219 for (i = 0; i < QT_IMA_ADPCM_SAMPLES_PER_BLOCK / 2; i++) | |
220 { | |
221 output[i * 2 + 0] = input[2 + i] & 0x0F; | |
222 output[i * 2 + 1] = input[2 + i] >> 4; | |
223 } | |
224 else | |
225 for (i = 0; i < QT_IMA_ADPCM_SAMPLES_PER_BLOCK / 2 * 2; i++) | |
226 { | |
227 output[i * 4 + 0] = input[2 + i] & 0x0F; | |
228 output[i * 4 + 1] = input[2 + QT_IMA_ADPCM_BLOCK_SIZE + i] & 0x0F; | |
229 output[i * 4 + 2] = input[2 + i] >> 4; | |
230 output[i * 4 + 3] = input[2 + QT_IMA_ADPCM_BLOCK_SIZE + i] >> 4; | |
231 } | |
232 | |
233 decode_nibbles(output, | |
234 QT_IMA_ADPCM_SAMPLES_PER_BLOCK * channels, channels, | |
235 initial_predictor_l, initial_index_l, | |
236 initial_predictor_r, initial_index_r); | |
237 | |
238 return QT_IMA_ADPCM_SAMPLES_PER_BLOCK * channels; | |
239 } | |
240 | |
241 static int ms_ima_adpcm_decode_block(unsigned short *output, | |
242 unsigned char *input, int channels, int block_size) | |
243 { | |
244 int predictor_l = 0; | |
245 int predictor_r = 0; | |
246 int index_l = 0; | |
247 int index_r = 0; | |
248 int i; | |
249 int channel_counter; | |
250 int channel_index; | |
251 int channel_index_l; | |
252 int channel_index_r; | |
253 | |
254 predictor_l = LE_16(&input[0]); | |
255 SE_16BIT(predictor_l); | |
256 index_l = input[2]; | |
257 if (channels == 2) | |
258 { | |
259 predictor_r = LE_16(&input[4]); | |
260 SE_16BIT(predictor_r); | |
261 index_r = input[6]; | |
262 } | |
263 | |
264 if (channels == 1) | |
265 for (i = 0; | |
266 i < (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) / 2; i++) | |
267 { | |
268 output[i * 2 + 0] = input[MS_IMA_ADPCM_PREAMBLE_SIZE + i] & 0x0F; | |
269 output[i * 2 + 1] = input[MS_IMA_ADPCM_PREAMBLE_SIZE + i] >> 4; | |
270 } | |
271 else | |
272 { | |
273 // encoded as 8 nibbles (4 bytes) per channel; switch channel every | |
274 // 4th byte | |
275 channel_counter = 0; | |
276 channel_index_l = 0; | |
277 channel_index_r = 1; | |
278 channel_index = channel_index_l; | |
279 for (i = 0; | |
280 i < (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels); i++) | |
281 { | |
282 output[channel_index + 0] = | |
283 input[MS_IMA_ADPCM_PREAMBLE_SIZE * 2 + i] & 0x0F; | |
284 output[channel_index + 2] = | |
285 input[MS_IMA_ADPCM_PREAMBLE_SIZE * 2 + i] >> 4; | |
286 channel_index += 4; | |
287 channel_counter++; | |
288 if (channel_counter == 4) | |
289 { | |
290 channel_index_l = channel_index; | |
291 channel_index = channel_index_r; | |
292 } | |
293 else if (channel_counter == 8) | |
294 { | |
295 channel_index_r = channel_index; | |
296 channel_index = channel_index_l; | |
297 channel_counter = 0; | |
298 } | |
299 } | |
300 } | |
301 | |
302 decode_nibbles(output, | |
303 (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2, | |
304 channels, | |
305 predictor_l, index_l, | |
306 predictor_r, index_r); | |
307 | |
308 return (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2; | |
309 } | |
310 | |
311 static int dk4_ima_adpcm_decode_block(unsigned short *output, | |
312 unsigned char *input, int channels, int block_size) | |
313 { | |
314 int i; | |
315 int output_ptr; | |
316 int predictor_l = 0; | |
317 int predictor_r = 0; | |
318 int index_l = 0; | |
319 int index_r = 0; | |
320 | |
321 // the first predictor value goes straight to the output | |
322 predictor_l = output[0] = LE_16(&input[0]); | |
323 SE_16BIT(predictor_l); | |
324 index_l = input[2]; | |
325 if (channels == 2) | |
326 { | |
327 predictor_r = output[1] = LE_16(&input[4]); | |
328 SE_16BIT(predictor_r); | |
329 index_r = input[6]; | |
330 } | |
331 | |
332 output_ptr = channels; | |
333 for (i = MS_IMA_ADPCM_PREAMBLE_SIZE * channels; i < block_size; i++) | |
334 { | |
335 output[output_ptr++] = input[i] >> 4; | |
336 output[output_ptr++] = input[i] & 0x0F; | |
337 } | |
338 | |
339 decode_nibbles(&output[channels], | |
340 (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2 - channels, | |
341 channels, | |
342 predictor_l, index_l, | |
343 predictor_r, index_r); | |
344 | |
345 return (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2 - channels; | |
346 } | |
347 | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
348 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
|
349 { |
5408 | 350 if (demux_read_data(sh_audio->ds, sh_audio->a_in_buffer, |
351 sh_audio->ds->ss_mul) != | |
352 sh_audio->ds->ss_mul) | |
353 return -1; | |
354 | |
355 if (sh_audio->format == 0x11) | |
356 { | |
357 return 2 * ms_ima_adpcm_decode_block( | |
358 (unsigned short*)buf, sh_audio->a_in_buffer, sh_audio->wf->nChannels, | |
359 sh_audio->ds->ss_mul); | |
360 } | |
361 else if (sh_audio->format == 0x61) | |
362 { | |
363 return 2 * dk4_ima_adpcm_decode_block( | |
364 (unsigned short*)buf, sh_audio->a_in_buffer, sh_audio->wf->nChannels, | |
365 sh_audio->ds->ss_mul); | |
366 } | |
367 else | |
368 { | |
369 return 2 * qt_ima_adpcm_decode_block( | |
370 (unsigned short*)buf, sh_audio->a_in_buffer, sh_audio->wf->nChannels); | |
371 } | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
372 } |