Mercurial > mplayer.hg
annotate libmpcodecs/ad_imaadpcm.c @ 17874:1973600f8f3e
Small fix with object list (-vf) help.
Tell the user when an object have no option description instead of
falling back to the "normal" help, like with -vf pp=help.
Don't display a useless error message when querying the help of
an object option (as in -vf scale=presize=help).
author | albeu |
---|---|
date | Wed, 15 Mar 2006 17:16:13 +0000 |
parents | 99df5428882f |
children | 1767c271d710 |
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 | |
8103 | 16 0x1100736d: IMA ADPCM coded like in MS AVI/ASF/WAV found in QT files |
5408 | 17 */ |
18 | |
5340
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 <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
|
20 #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
|
21 #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
|
22 |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
23 #include "config.h" |
5408 | 24 #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
|
25 #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
|
26 |
5408 | 27 #define MS_IMA_ADPCM_PREAMBLE_SIZE 4 |
28 | |
29 #define QT_IMA_ADPCM_PREAMBLE_SIZE 2 | |
30 #define QT_IMA_ADPCM_BLOCK_SIZE 0x22 | |
31 #define QT_IMA_ADPCM_SAMPLES_PER_BLOCK 64 | |
32 | |
33 #define BE_16(x) (be2me_16(*(unsigned short *)(x))) | |
34 #define BE_32(x) (be2me_32(*(unsigned int *)(x))) | |
35 #define LE_16(x) (le2me_16(*(unsigned short *)(x))) | |
36 #define LE_32(x) (le2me_32(*(unsigned int *)(x))) | |
37 | |
38 // pertinent tables for IMA ADPCM | |
39 static int adpcm_step[89] = | |
40 { | |
41 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, | |
42 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, | |
43 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, | |
44 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, | |
45 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, | |
46 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, | |
47 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, | |
48 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, | |
49 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 | |
50 }; | |
51 | |
52 static int adpcm_index[16] = | |
53 { | |
54 -1, -1, -1, -1, 2, 4, 6, 8, | |
55 -1, -1, -1, -1, 2, 4, 6, 8 | |
56 }; | |
57 | |
58 // useful macros | |
59 // clamp a number between 0 and 88 | |
60 #define CLAMP_0_TO_88(x) if (x < 0) x = 0; else if (x > 88) x = 88; | |
61 // clamp a number within a signed 16-bit range | |
62 #define CLAMP_S16(x) if (x < -32768) x = -32768; \ | |
63 else if (x > 32767) x = 32767; | |
64 // clamp a number above 16 | |
65 #define CLAMP_ABOVE_16(x) if (x < 16) x = 16; | |
66 // sign extend a 16-bit value | |
67 #define SE_16BIT(x) if (x & 0x8000) x -= 0x10000; | |
68 // sign extend a 4-bit value | |
69 #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
|
70 |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
71 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
|
72 { |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
73 "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
|
74 "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 | |
8103 | 90 if ((sh_audio->format == 0x11) || (sh_audio->format == 0x61) || |
91 (sh_audio->format == 0x1100736d)) | |
5408 | 92 { |
93 sh_audio->ds->ss_div = (sh_audio->wf->nBlockAlign - | |
94 (MS_IMA_ADPCM_PREAMBLE_SIZE * sh_audio->wf->nChannels)) * 2; | |
95 sh_audio->ds->ss_mul = sh_audio->wf->nBlockAlign; | |
96 } | |
97 else | |
98 { | |
99 sh_audio->ds->ss_div = QT_IMA_ADPCM_SAMPLES_PER_BLOCK; | |
100 sh_audio->ds->ss_mul = QT_IMA_ADPCM_BLOCK_SIZE * sh_audio->wf->nChannels; | |
101 } | |
5458 | 102 sh_audio->audio_in_minsize=sh_audio->ds->ss_mul; |
5408 | 103 return 1; |
104 } | |
105 | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
106 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
|
107 { |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
108 /* 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
|
109 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
|
110 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
|
111 /* decodes 34 byte -> 64 short*/ |
5408 | 112 sh_audio->i_bps = |
113 (sh_audio->ds->ss_mul * sh_audio->samplerate) / sh_audio->ds->ss_div; | |
13427
9d0b052c4f74
setting samplesize to 2 in decoders where neccessary.
reimar
parents:
10808
diff
changeset
|
114 sh_audio->samplesize=2; |
5408 | 115 |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
116 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
|
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 |
5408 | 119 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
|
120 { |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
121 } |
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 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
|
124 { |
5481 | 125 if(cmd==ADCTRL_SKIP_FRAME){ |
126 demux_read_data(sh_audio->ds, sh_audio->a_in_buffer,sh_audio->ds->ss_mul); | |
127 return CONTROL_TRUE; | |
128 } | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
129 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
|
130 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
131 |
5408 | 132 static void decode_nibbles(unsigned short *output, |
133 int output_size, int channels, | |
134 int predictor_l, int index_l, | |
135 int predictor_r, int index_r) | |
136 { | |
137 int step[2]; | |
138 int predictor[2]; | |
139 int index[2]; | |
140 int diff; | |
141 int i; | |
142 int sign; | |
143 int delta; | |
144 int channel_number = 0; | |
145 | |
146 step[0] = adpcm_step[index_l]; | |
147 step[1] = adpcm_step[index_r]; | |
148 predictor[0] = predictor_l; | |
149 predictor[1] = predictor_r; | |
150 index[0] = index_l; | |
151 index[1] = index_r; | |
152 | |
153 for (i = 0; i < output_size; i++) | |
154 { | |
155 delta = output[i]; | |
156 | |
157 index[channel_number] += adpcm_index[delta]; | |
158 CLAMP_0_TO_88(index[channel_number]); | |
159 | |
160 sign = delta & 8; | |
161 delta = delta & 7; | |
162 | |
163 diff = step[channel_number] >> 3; | |
164 if (delta & 4) diff += step[channel_number]; | |
165 if (delta & 2) diff += step[channel_number] >> 1; | |
166 if (delta & 1) diff += step[channel_number] >> 2; | |
167 | |
168 if (sign) | |
169 predictor[channel_number] -= diff; | |
170 else | |
171 predictor[channel_number] += diff; | |
172 | |
173 CLAMP_S16(predictor[channel_number]); | |
174 output[i] = predictor[channel_number]; | |
175 step[channel_number] = adpcm_step[index[channel_number]]; | |
176 | |
177 // toggle channel | |
178 channel_number ^= channels - 1; | |
179 | |
180 } | |
181 } | |
182 | |
183 static int qt_ima_adpcm_decode_block(unsigned short *output, | |
184 unsigned char *input, int channels) | |
185 { | |
186 int initial_predictor_l = 0; | |
187 int initial_predictor_r = 0; | |
188 int initial_index_l = 0; | |
189 int initial_index_r = 0; | |
190 int i; | |
191 | |
192 initial_predictor_l = BE_16(&input[0]); | |
193 initial_index_l = initial_predictor_l; | |
194 | |
195 // mask, sign-extend, and clamp the predictor portion | |
196 initial_predictor_l &= 0xFF80; | |
197 SE_16BIT(initial_predictor_l); | |
198 CLAMP_S16(initial_predictor_l); | |
199 | |
200 // mask and clamp the index portion | |
201 initial_index_l &= 0x7F; | |
202 CLAMP_0_TO_88(initial_index_l); | |
203 | |
204 // handle stereo | |
205 if (channels > 1) | |
206 { | |
207 initial_predictor_r = BE_16(&input[QT_IMA_ADPCM_BLOCK_SIZE]); | |
208 initial_index_r = initial_predictor_r; | |
209 | |
210 // mask, sign-extend, and clamp the predictor portion | |
211 initial_predictor_r &= 0xFF80; | |
212 SE_16BIT(initial_predictor_r); | |
213 CLAMP_S16(initial_predictor_r); | |
214 | |
215 // mask and clamp the index portion | |
216 initial_index_r &= 0x7F; | |
217 CLAMP_0_TO_88(initial_index_r); | |
218 } | |
219 | |
220 // break apart all of the nibbles in the block | |
221 if (channels == 1) | |
222 for (i = 0; i < QT_IMA_ADPCM_SAMPLES_PER_BLOCK / 2; i++) | |
223 { | |
224 output[i * 2 + 0] = input[2 + i] & 0x0F; | |
225 output[i * 2 + 1] = input[2 + i] >> 4; | |
226 } | |
227 else | |
15786 | 228 for (i = 0; i < QT_IMA_ADPCM_SAMPLES_PER_BLOCK / 2; i++) |
5408 | 229 { |
230 output[i * 4 + 0] = input[2 + i] & 0x0F; | |
231 output[i * 4 + 1] = input[2 + QT_IMA_ADPCM_BLOCK_SIZE + i] & 0x0F; | |
232 output[i * 4 + 2] = input[2 + i] >> 4; | |
233 output[i * 4 + 3] = input[2 + QT_IMA_ADPCM_BLOCK_SIZE + i] >> 4; | |
234 } | |
235 | |
236 decode_nibbles(output, | |
237 QT_IMA_ADPCM_SAMPLES_PER_BLOCK * channels, channels, | |
238 initial_predictor_l, initial_index_l, | |
239 initial_predictor_r, initial_index_r); | |
240 | |
241 return QT_IMA_ADPCM_SAMPLES_PER_BLOCK * channels; | |
242 } | |
243 | |
244 static int ms_ima_adpcm_decode_block(unsigned short *output, | |
245 unsigned char *input, int channels, int block_size) | |
246 { | |
247 int predictor_l = 0; | |
248 int predictor_r = 0; | |
249 int index_l = 0; | |
250 int index_r = 0; | |
251 int i; | |
252 int channel_counter; | |
253 int channel_index; | |
254 int channel_index_l; | |
255 int channel_index_r; | |
256 | |
257 predictor_l = LE_16(&input[0]); | |
258 SE_16BIT(predictor_l); | |
259 index_l = input[2]; | |
260 if (channels == 2) | |
261 { | |
262 predictor_r = LE_16(&input[4]); | |
263 SE_16BIT(predictor_r); | |
264 index_r = input[6]; | |
265 } | |
266 | |
267 if (channels == 1) | |
268 for (i = 0; | |
10808
9883dfced49c
100l: you have 2 nibbles per byte, don't divide byte count by 2
rtognimp
parents:
8103
diff
changeset
|
269 i < (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels); i++) |
5408 | 270 { |
271 output[i * 2 + 0] = input[MS_IMA_ADPCM_PREAMBLE_SIZE + i] & 0x0F; | |
272 output[i * 2 + 1] = input[MS_IMA_ADPCM_PREAMBLE_SIZE + i] >> 4; | |
273 } | |
274 else | |
275 { | |
276 // encoded as 8 nibbles (4 bytes) per channel; switch channel every | |
277 // 4th byte | |
278 channel_counter = 0; | |
279 channel_index_l = 0; | |
280 channel_index_r = 1; | |
281 channel_index = channel_index_l; | |
282 for (i = 0; | |
283 i < (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels); i++) | |
284 { | |
285 output[channel_index + 0] = | |
286 input[MS_IMA_ADPCM_PREAMBLE_SIZE * 2 + i] & 0x0F; | |
287 output[channel_index + 2] = | |
288 input[MS_IMA_ADPCM_PREAMBLE_SIZE * 2 + i] >> 4; | |
289 channel_index += 4; | |
290 channel_counter++; | |
291 if (channel_counter == 4) | |
292 { | |
293 channel_index_l = channel_index; | |
294 channel_index = channel_index_r; | |
295 } | |
296 else if (channel_counter == 8) | |
297 { | |
298 channel_index_r = channel_index; | |
299 channel_index = channel_index_l; | |
300 channel_counter = 0; | |
301 } | |
302 } | |
303 } | |
304 | |
305 decode_nibbles(output, | |
306 (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2, | |
307 channels, | |
308 predictor_l, index_l, | |
309 predictor_r, index_r); | |
310 | |
311 return (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2; | |
312 } | |
313 | |
314 static int dk4_ima_adpcm_decode_block(unsigned short *output, | |
315 unsigned char *input, int channels, int block_size) | |
316 { | |
317 int i; | |
318 int output_ptr; | |
319 int predictor_l = 0; | |
320 int predictor_r = 0; | |
321 int index_l = 0; | |
322 int index_r = 0; | |
323 | |
324 // the first predictor value goes straight to the output | |
325 predictor_l = output[0] = LE_16(&input[0]); | |
326 SE_16BIT(predictor_l); | |
327 index_l = input[2]; | |
328 if (channels == 2) | |
329 { | |
330 predictor_r = output[1] = LE_16(&input[4]); | |
331 SE_16BIT(predictor_r); | |
332 index_r = input[6]; | |
333 } | |
334 | |
335 output_ptr = channels; | |
336 for (i = MS_IMA_ADPCM_PREAMBLE_SIZE * channels; i < block_size; i++) | |
337 { | |
338 output[output_ptr++] = input[i] >> 4; | |
339 output[output_ptr++] = input[i] & 0x0F; | |
340 } | |
341 | |
342 decode_nibbles(&output[channels], | |
343 (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2 - channels, | |
344 channels, | |
345 predictor_l, index_l, | |
346 predictor_r, index_r); | |
347 | |
348 return (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2 - channels; | |
349 } | |
350 | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
351 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
|
352 { |
5408 | 353 if (demux_read_data(sh_audio->ds, sh_audio->a_in_buffer, |
354 sh_audio->ds->ss_mul) != | |
355 sh_audio->ds->ss_mul) | |
356 return -1; | |
357 | |
8103 | 358 if ((sh_audio->format == 0x11) || (sh_audio->format == 0x1100736d)) |
5408 | 359 { |
360 return 2 * ms_ima_adpcm_decode_block( | |
361 (unsigned short*)buf, sh_audio->a_in_buffer, sh_audio->wf->nChannels, | |
362 sh_audio->ds->ss_mul); | |
363 } | |
364 else if (sh_audio->format == 0x61) | |
365 { | |
366 return 2 * dk4_ima_adpcm_decode_block( | |
367 (unsigned short*)buf, sh_audio->a_in_buffer, sh_audio->wf->nChannels, | |
368 sh_audio->ds->ss_mul); | |
369 } | |
370 else | |
371 { | |
372 return 2 * qt_ima_adpcm_decode_block( | |
373 (unsigned short*)buf, sh_audio->a_in_buffer, sh_audio->wf->nChannels); | |
374 } | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
375 } |