2 IMA ADPCM Decoder for MPlayer
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/
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 0x1100736d: IMA ADPCM coded like in MS AVI/ASF/WAV found in QT files
24 #include "libavutil/common.h"
26 #include "ad_internal.h"
28 #define MS_IMA_ADPCM_PREAMBLE_SIZE 4
30 #define QT_IMA_ADPCM_PREAMBLE_SIZE 2
31 #define QT_IMA_ADPCM_BLOCK_SIZE 0x22
32 #define QT_IMA_ADPCM_SAMPLES_PER_BLOCK 64
34 #define BE_16(x) (be2me_16(*(unsigned short *)(x)))
35 #define BE_32(x) (be2me_32(*(unsigned int *)(x)))
36 #define LE_16(x) (le2me_16(*(unsigned short *)(x)))
37 #define LE_32(x) (le2me_32(*(unsigned int *)(x)))
39 // pertinent tables for IMA ADPCM
40 static int adpcm_step
[89] =
42 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
43 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
44 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
45 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
46 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
47 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
48 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
49 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
50 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
53 static int adpcm_index
[16] =
55 -1, -1, -1, -1, 2, 4, 6, 8,
56 -1, -1, -1, -1, 2, 4, 6, 8
60 // clamp a number between 0 and 88
61 #define CLAMP_0_TO_88(x) if (x < 0) x = 0; else if (x > 88) x = 88;
62 // clamp a number within a signed 16-bit range
63 #define CLAMP_S16(x) if (x < -32768) x = -32768; \
64 else if (x > 32767) x = 32767;
65 // clamp a number above 16
66 #define CLAMP_ABOVE_16(x) if (x < 16) x = 16;
67 // sign extend a 16-bit value
68 #define SE_16BIT(x) if (x & 0x8000) x -= 0x10000;
69 // sign extend a 4-bit value
70 #define SE_4BIT(x) if (x & 0x8) x -= 0x10;
72 static ad_info_t info
=
74 "IMA ADPCM audio decoder",
81 LIBAD_EXTERN(imaadpcm
)
83 static int preinit(sh_audio_t
*sh_audio
)
85 // not exactly sure what this field is for
86 sh_audio
->audio_out_minsize
= 8192;
88 // if format is "ima4", assume the audio is coming from a QT file which
89 // indicates constant block size, whereas an AVI/ASF/WAV file will fill
90 // in this field with 0x11
91 if ((sh_audio
->format
== 0x11) || (sh_audio
->format
== 0x61) ||
92 (sh_audio
->format
== 0x1100736d))
94 sh_audio
->ds
->ss_div
= (sh_audio
->wf
->nBlockAlign
-
95 (MS_IMA_ADPCM_PREAMBLE_SIZE
* sh_audio
->wf
->nChannels
)) * 2;
96 sh_audio
->ds
->ss_mul
= sh_audio
->wf
->nBlockAlign
;
100 sh_audio
->ds
->ss_div
= QT_IMA_ADPCM_SAMPLES_PER_BLOCK
;
101 sh_audio
->ds
->ss_mul
= QT_IMA_ADPCM_BLOCK_SIZE
* sh_audio
->wf
->nChannels
;
103 sh_audio
->audio_in_minsize
=sh_audio
->ds
->ss_mul
;
107 static int init(sh_audio_t
*sh_audio
)
109 /* IMA-ADPCM 4:1 audio codec:*/
110 sh_audio
->channels
=sh_audio
->wf
->nChannels
;
111 sh_audio
->samplerate
=sh_audio
->wf
->nSamplesPerSec
;
112 /* decodes 34 byte -> 64 short*/
114 (sh_audio
->ds
->ss_mul
* sh_audio
->samplerate
) / sh_audio
->ds
->ss_div
;
115 sh_audio
->samplesize
=2;
120 static void uninit(sh_audio_t
*sh_audio
)
124 static int control(sh_audio_t
*sh_audio
,int cmd
,void* arg
, ...)
126 if(cmd
==ADCTRL_SKIP_FRAME
){
127 demux_read_data(sh_audio
->ds
, sh_audio
->a_in_buffer
,sh_audio
->ds
->ss_mul
);
130 return CONTROL_UNKNOWN
;
133 static void decode_nibbles(unsigned short *output
,
134 int output_size
, int channels
,
135 int predictor_l
, int index_l
,
136 int predictor_r
, int index_r
)
145 int channel_number
= 0;
147 step
[0] = adpcm_step
[index_l
];
148 step
[1] = adpcm_step
[index_r
];
149 predictor
[0] = predictor_l
;
150 predictor
[1] = predictor_r
;
154 for (i
= 0; i
< output_size
; i
++)
158 index
[channel_number
] += adpcm_index
[delta
];
159 CLAMP_0_TO_88(index
[channel_number
]);
164 diff
= step
[channel_number
] >> 3;
165 if (delta
& 4) diff
+= step
[channel_number
];
166 if (delta
& 2) diff
+= step
[channel_number
] >> 1;
167 if (delta
& 1) diff
+= step
[channel_number
] >> 2;
170 predictor
[channel_number
] -= diff
;
172 predictor
[channel_number
] += diff
;
174 CLAMP_S16(predictor
[channel_number
]);
175 output
[i
] = predictor
[channel_number
];
176 step
[channel_number
] = adpcm_step
[index
[channel_number
]];
179 channel_number
^= channels
- 1;
184 static int qt_ima_adpcm_decode_block(unsigned short *output
,
185 unsigned char *input
, int channels
)
187 int initial_predictor_l
= 0;
188 int initial_predictor_r
= 0;
189 int initial_index_l
= 0;
190 int initial_index_r
= 0;
193 initial_predictor_l
= BE_16(&input
[0]);
194 initial_index_l
= initial_predictor_l
;
196 // mask, sign-extend, and clamp the predictor portion
197 initial_predictor_l
&= 0xFF80;
198 SE_16BIT(initial_predictor_l
);
199 CLAMP_S16(initial_predictor_l
);
201 // mask and clamp the index portion
202 initial_index_l
&= 0x7F;
203 CLAMP_0_TO_88(initial_index_l
);
208 initial_predictor_r
= BE_16(&input
[QT_IMA_ADPCM_BLOCK_SIZE
]);
209 initial_index_r
= initial_predictor_r
;
211 // mask, sign-extend, and clamp the predictor portion
212 initial_predictor_r
&= 0xFF80;
213 SE_16BIT(initial_predictor_r
);
214 CLAMP_S16(initial_predictor_r
);
216 // mask and clamp the index portion
217 initial_index_r
&= 0x7F;
218 CLAMP_0_TO_88(initial_index_r
);
221 // break apart all of the nibbles in the block
223 for (i
= 0; i
< QT_IMA_ADPCM_SAMPLES_PER_BLOCK
/ 2; i
++)
225 output
[i
* 2 + 0] = input
[2 + i
] & 0x0F;
226 output
[i
* 2 + 1] = input
[2 + i
] >> 4;
229 for (i
= 0; i
< QT_IMA_ADPCM_SAMPLES_PER_BLOCK
/ 2; i
++)
231 output
[i
* 4 + 0] = input
[2 + i
] & 0x0F;
232 output
[i
* 4 + 1] = input
[2 + QT_IMA_ADPCM_BLOCK_SIZE
+ i
] & 0x0F;
233 output
[i
* 4 + 2] = input
[2 + i
] >> 4;
234 output
[i
* 4 + 3] = input
[2 + QT_IMA_ADPCM_BLOCK_SIZE
+ i
] >> 4;
237 decode_nibbles(output
,
238 QT_IMA_ADPCM_SAMPLES_PER_BLOCK
* channels
, channels
,
239 initial_predictor_l
, initial_index_l
,
240 initial_predictor_r
, initial_index_r
);
242 return QT_IMA_ADPCM_SAMPLES_PER_BLOCK
* channels
;
245 static int ms_ima_adpcm_decode_block(unsigned short *output
,
246 unsigned char *input
, int channels
, int block_size
)
258 predictor_l
= LE_16(&input
[0]);
259 SE_16BIT(predictor_l
);
263 predictor_r
= LE_16(&input
[4]);
264 SE_16BIT(predictor_r
);
270 i
< (block_size
- MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
); i
++)
272 output
[i
* 2 + 0] = input
[MS_IMA_ADPCM_PREAMBLE_SIZE
+ i
] & 0x0F;
273 output
[i
* 2 + 1] = input
[MS_IMA_ADPCM_PREAMBLE_SIZE
+ i
] >> 4;
277 // encoded as 8 nibbles (4 bytes) per channel; switch channel every
282 channel_index
= channel_index_l
;
284 i
< (block_size
- MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
); i
++)
286 output
[channel_index
+ 0] =
287 input
[MS_IMA_ADPCM_PREAMBLE_SIZE
* 2 + i
] & 0x0F;
288 output
[channel_index
+ 2] =
289 input
[MS_IMA_ADPCM_PREAMBLE_SIZE
* 2 + i
] >> 4;
292 if (channel_counter
== 4)
294 channel_index_l
= channel_index
;
295 channel_index
= channel_index_r
;
297 else if (channel_counter
== 8)
299 channel_index_r
= channel_index
;
300 channel_index
= channel_index_l
;
306 decode_nibbles(output
,
307 (block_size
- MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
) * 2,
309 predictor_l
, index_l
,
310 predictor_r
, index_r
);
312 return (block_size
- MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
) * 2;
315 static int dk4_ima_adpcm_decode_block(unsigned short *output
,
316 unsigned char *input
, int channels
, int block_size
)
325 // the first predictor value goes straight to the output
326 predictor_l
= output
[0] = LE_16(&input
[0]);
327 SE_16BIT(predictor_l
);
331 predictor_r
= output
[1] = LE_16(&input
[4]);
332 SE_16BIT(predictor_r
);
336 output_ptr
= channels
;
337 for (i
= MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
; i
< block_size
; i
++)
339 output
[output_ptr
++] = input
[i
] >> 4;
340 output
[output_ptr
++] = input
[i
] & 0x0F;
343 decode_nibbles(&output
[channels
],
344 (block_size
- MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
) * 2 - channels
,
346 predictor_l
, index_l
,
347 predictor_r
, index_r
);
349 return (block_size
- MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
) * 2 - channels
;
352 static int decode_audio(sh_audio_t
*sh_audio
,unsigned char *buf
,int minlen
,int maxlen
)
354 if (demux_read_data(sh_audio
->ds
, sh_audio
->a_in_buffer
,
355 sh_audio
->ds
->ss_mul
) !=
356 sh_audio
->ds
->ss_mul
)
359 if ((sh_audio
->format
== 0x11) || (sh_audio
->format
== 0x1100736d))
361 return 2 * ms_ima_adpcm_decode_block(
362 (unsigned short*)buf
, sh_audio
->a_in_buffer
, sh_audio
->wf
->nChannels
,
363 sh_audio
->ds
->ss_mul
);
365 else if (sh_audio
->format
== 0x61)
367 return 2 * dk4_ima_adpcm_decode_block(
368 (unsigned short*)buf
, sh_audio
->a_in_buffer
, sh_audio
->wf
->nChannels
,
369 sh_audio
->ds
->ss_mul
);
373 return 2 * qt_ima_adpcm_decode_block(
374 (unsigned short*)buf
, sh_audio
->a_in_buffer
, sh_audio
->wf
->nChannels
);