4 * This file is in charge of decoding all of the various IMA ADPCM data
5 * formats that various entities have created. Details about the data
6 * formats can be found here:
7 * http://www.pcisys.net/~melanson/codecs/
9 * So far, this file handles these formats:
10 * 'ima4': IMA ADPCM found in QT files
11 * 0x11: IMA ADPCM found in MS AVI/ASF/WAV files
12 * 0x61: DK4 ADPCM found in certain AVI files on Sega Saturn CD-ROMs;
13 * note that this is a 'rogue' format number in that it was
14 * never officially registered with Microsoft
15 * 0x1100736d: IMA ADPCM coded like in MS AVI/ASF/WAV found in QT files
17 * Copyright (c) 2002 Mike Melanson
19 * This file is part of MPlayer.
21 * MPlayer is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
26 * MPlayer is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License along
32 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
33 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
42 #include "libavutil/common.h"
44 #include "ad_internal.h"
46 #define MS_IMA_ADPCM_PREAMBLE_SIZE 4
48 #define QT_IMA_ADPCM_PREAMBLE_SIZE 2
49 #define QT_IMA_ADPCM_BLOCK_SIZE 0x22
50 #define QT_IMA_ADPCM_SAMPLES_PER_BLOCK 64
52 #define BE_16(x) (be2me_16(*(unsigned short *)(x)))
53 #define LE_16(x) (le2me_16(*(unsigned short *)(x)))
55 // pertinent tables for IMA ADPCM
56 static const int16_t adpcm_step
[89] =
58 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
59 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
60 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
61 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
62 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
63 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
64 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
65 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
66 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
69 static const int8_t adpcm_index
[8] =
71 -1, -1, -1, -1, 2, 4, 6, 8,
75 // clamp a number between 0 and 88
76 #define CLAMP_0_TO_88(x) x = av_clip(x, 0, 88);
77 // clamp a number within a signed 16-bit range
78 #define CLAMP_S16(x) x = av_clip_int16(x);
79 // clamp a number above 16
80 #define CLAMP_ABOVE_16(x) if (x < 16) x = 16;
82 static const ad_info_t info
=
84 "IMA ADPCM audio decoder",
91 LIBAD_EXTERN(imaadpcm
)
93 static int preinit(sh_audio_t
*sh_audio
)
95 // not exactly sure what this field is for
96 sh_audio
->audio_out_minsize
= 8192;
98 // if format is "ima4", assume the audio is coming from a QT file which
99 // indicates constant block size, whereas an AVI/ASF/WAV file will fill
100 // in this field with 0x11
101 if ((sh_audio
->format
== 0x11) || (sh_audio
->format
== 0x61) ||
102 (sh_audio
->format
== 0x1100736d))
104 sh_audio
->ds
->ss_div
= (sh_audio
->wf
->nBlockAlign
-
105 (MS_IMA_ADPCM_PREAMBLE_SIZE
* sh_audio
->wf
->nChannels
)) * 2;
106 sh_audio
->ds
->ss_mul
= sh_audio
->wf
->nBlockAlign
;
110 sh_audio
->ds
->ss_div
= QT_IMA_ADPCM_SAMPLES_PER_BLOCK
;
111 sh_audio
->ds
->ss_mul
= QT_IMA_ADPCM_BLOCK_SIZE
* sh_audio
->wf
->nChannels
;
113 sh_audio
->audio_in_minsize
=sh_audio
->ds
->ss_mul
;
117 static int init(sh_audio_t
*sh_audio
)
119 /* IMA-ADPCM 4:1 audio codec:*/
120 sh_audio
->channels
=sh_audio
->wf
->nChannels
;
121 sh_audio
->samplerate
=sh_audio
->wf
->nSamplesPerSec
;
122 /* decodes 34 byte -> 64 short*/
124 (sh_audio
->ds
->ss_mul
* sh_audio
->samplerate
) / sh_audio
->ds
->ss_div
;
125 sh_audio
->samplesize
=2;
130 static void uninit(sh_audio_t
*sh_audio
)
134 static int control(sh_audio_t
*sh_audio
,int cmd
,void* arg
, ...)
136 if(cmd
==ADCTRL_SKIP_FRAME
){
137 demux_read_data(sh_audio
->ds
, sh_audio
->a_in_buffer
,sh_audio
->ds
->ss_mul
);
140 return CONTROL_UNKNOWN
;
143 static void decode_nibbles(unsigned short *output
,
144 int output_size
, int channels
,
145 int predictor
[2], int index
[2])
151 int channel_number
= 0;
153 step
[0] = adpcm_step
[index
[0]];
154 step
[1] = adpcm_step
[index
[1]];
156 for (i
= 0; i
< output_size
; i
++)
162 index
[channel_number
] += adpcm_index
[delta
];
163 CLAMP_0_TO_88(index
[channel_number
]);
165 delta
= 2 * delta
+ 1;
166 if (sign
) delta
= -delta
;
168 predictor
[channel_number
] += (delta
* step
[channel_number
]) >> 3;
170 CLAMP_S16(predictor
[channel_number
]);
171 output
[i
] = predictor
[channel_number
];
172 step
[channel_number
] = adpcm_step
[index
[channel_number
]];
175 channel_number
^= channels
- 1;
180 static int qt_ima_adpcm_decode_block(unsigned short *output
,
181 unsigned char *input
, int channels
, int block_size
)
183 int initial_predictor
[2] = {0};
184 int initial_index
[2] = {0};
187 if (channels
!= 1) channels
= 2;
188 if (block_size
< channels
* QT_IMA_ADPCM_BLOCK_SIZE
)
191 for (i
= 0; i
< channels
; i
++) {
192 initial_index
[i
] = initial_predictor
[i
] = (int16_t)BE_16(&input
[i
* QT_IMA_ADPCM_BLOCK_SIZE
]);
194 // mask, sign-extend, and clamp the predictor portion
195 initial_predictor
[i
] &= ~0x7F;
196 CLAMP_S16(initial_predictor
[i
]);
198 // mask and clamp the index portion
199 initial_index
[i
] &= 0x7F;
200 CLAMP_0_TO_88(initial_index
[i
]);
203 // break apart all of the nibbles in the block
205 for (i
= 0; i
< QT_IMA_ADPCM_SAMPLES_PER_BLOCK
/ 2; i
++)
207 output
[i
* 2 + 0] = input
[2 + i
] & 0x0F;
208 output
[i
* 2 + 1] = input
[2 + i
] >> 4;
211 for (i
= 0; i
< QT_IMA_ADPCM_SAMPLES_PER_BLOCK
/ 2; i
++)
213 output
[i
* 4 + 0] = input
[2 + i
] & 0x0F;
214 output
[i
* 4 + 1] = input
[2 + QT_IMA_ADPCM_BLOCK_SIZE
+ i
] & 0x0F;
215 output
[i
* 4 + 2] = input
[2 + i
] >> 4;
216 output
[i
* 4 + 3] = input
[2 + QT_IMA_ADPCM_BLOCK_SIZE
+ i
] >> 4;
219 decode_nibbles(output
,
220 QT_IMA_ADPCM_SAMPLES_PER_BLOCK
* channels
, channels
,
221 initial_predictor
, initial_index
);
223 return QT_IMA_ADPCM_SAMPLES_PER_BLOCK
* channels
;
226 static int ms_ima_adpcm_decode_block(unsigned short *output
,
227 unsigned char *input
, int channels
, int block_size
)
237 if (channels
!= 1) channels
= 2;
238 if (block_size
< MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
)
241 for (i
= 0; i
< channels
; i
++) {
242 predictor
[i
] = (int16_t)LE_16(&input
[i
* 4]);
243 index
[i
] = input
[i
* 4 + 2];
248 i
< (block_size
- MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
); i
++)
250 output
[i
* 2 + 0] = input
[MS_IMA_ADPCM_PREAMBLE_SIZE
+ i
] & 0x0F;
251 output
[i
* 2 + 1] = input
[MS_IMA_ADPCM_PREAMBLE_SIZE
+ i
] >> 4;
255 // encoded as 8 nibbles (4 bytes) per channel; switch channel every
260 channel_index
= channel_index_l
;
262 i
< (block_size
- MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
); i
++)
264 output
[channel_index
+ 0] =
265 input
[MS_IMA_ADPCM_PREAMBLE_SIZE
* 2 + i
] & 0x0F;
266 output
[channel_index
+ 2] =
267 input
[MS_IMA_ADPCM_PREAMBLE_SIZE
* 2 + i
] >> 4;
270 if (channel_counter
== 4)
272 channel_index_l
= channel_index
;
273 channel_index
= channel_index_r
;
275 else if (channel_counter
== 8)
277 channel_index_r
= channel_index
;
278 channel_index
= channel_index_l
;
284 decode_nibbles(output
,
285 (block_size
- MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
) * 2,
289 return (block_size
- MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
) * 2;
292 static int dk4_ima_adpcm_decode_block(unsigned short *output
,
293 unsigned char *input
, int channels
, int block_size
)
300 if (channels
!= 1) channels
= 2;
301 if (block_size
< MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
)
304 for (i
= 0; i
< channels
; i
++) {
305 // the first predictor value goes straight to the output
306 predictor
[i
] = output
[i
] = (int16_t)LE_16(&input
[i
* 4]);
307 index
[i
] = input
[i
* 4 + 2];
310 output_ptr
= channels
;
311 for (i
= MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
; i
< block_size
; i
++)
313 output
[output_ptr
++] = input
[i
] >> 4;
314 output
[output_ptr
++] = input
[i
] & 0x0F;
317 decode_nibbles(&output
[channels
],
318 (block_size
- MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
) * 2 - channels
,
322 return (block_size
- MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
) * 2 - channels
;
325 static int decode_audio(sh_audio_t
*sh_audio
,unsigned char *buf
,int minlen
,int maxlen
)
328 int (*decode_func
)(unsigned short *output
, unsigned char *input
, int channels
, int block_size
) = qt_ima_adpcm_decode_block
;
329 if (demux_read_data(sh_audio
->ds
, sh_audio
->a_in_buffer
,
330 sh_audio
->ds
->ss_mul
) !=
331 sh_audio
->ds
->ss_mul
)
334 if ((sh_audio
->format
== 0x11) || (sh_audio
->format
== 0x1100736d))
335 decode_func
= ms_ima_adpcm_decode_block
;
336 else if (sh_audio
->format
== 0x61)
337 decode_func
= dk4_ima_adpcm_decode_block
;
339 res
= decode_func((unsigned short*)buf
, sh_audio
->a_in_buffer
,
340 sh_audio
->wf
->nChannels
, sh_audio
->ds
->ss_mul
);
341 return res
< 0 ? res
: 2 * res
;