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 "ffmpeg_files/intreadwrite.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 // pertinent tables for IMA ADPCM
53 static const int16_t adpcm_step
[89] =
55 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
56 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
57 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
58 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
59 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
60 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
61 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
62 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
63 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
66 static const int8_t adpcm_index
[8] =
68 -1, -1, -1, -1, 2, 4, 6, 8,
72 // clamp a number between 0 and 88
73 #define CLAMP_0_TO_88(x) x = av_clip(x, 0, 88);
74 // clamp a number within a signed 16-bit range
75 #define CLAMP_S16(x) x = av_clip_int16(x);
76 // clamp a number above 16
77 #define CLAMP_ABOVE_16(x) if (x < 16) x = 16;
79 static const ad_info_t info
=
81 "IMA ADPCM audio decoder",
88 LIBAD_EXTERN(imaadpcm
)
90 static int preinit(sh_audio_t
*sh_audio
)
92 // not exactly sure what this field is for
93 sh_audio
->audio_out_minsize
= 8192;
95 // if format is "ima4", assume the audio is coming from a QT file which
96 // indicates constant block size, whereas an AVI/ASF/WAV file will fill
97 // in this field with 0x11
98 if ((sh_audio
->format
== 0x11) || (sh_audio
->format
== 0x61) ||
99 (sh_audio
->format
== 0x1100736d))
101 sh_audio
->ds
->ss_div
= (sh_audio
->wf
->nBlockAlign
-
102 (MS_IMA_ADPCM_PREAMBLE_SIZE
* sh_audio
->wf
->nChannels
)) * 2;
103 sh_audio
->ds
->ss_mul
= sh_audio
->wf
->nBlockAlign
;
107 sh_audio
->ds
->ss_div
= QT_IMA_ADPCM_SAMPLES_PER_BLOCK
;
108 sh_audio
->ds
->ss_mul
= QT_IMA_ADPCM_BLOCK_SIZE
* sh_audio
->wf
->nChannels
;
110 sh_audio
->audio_in_minsize
=sh_audio
->ds
->ss_mul
;
114 static int init(sh_audio_t
*sh_audio
)
116 /* IMA-ADPCM 4:1 audio codec:*/
117 sh_audio
->channels
=sh_audio
->wf
->nChannels
;
118 sh_audio
->samplerate
=sh_audio
->wf
->nSamplesPerSec
;
119 /* decodes 34 byte -> 64 short*/
121 (sh_audio
->ds
->ss_mul
* sh_audio
->samplerate
) / sh_audio
->ds
->ss_div
;
122 sh_audio
->samplesize
=2;
127 static void uninit(sh_audio_t
*sh_audio
)
131 static int control(sh_audio_t
*sh_audio
,int cmd
,void* arg
, ...)
133 if(cmd
==ADCTRL_SKIP_FRAME
){
134 demux_read_data(sh_audio
->ds
, sh_audio
->a_in_buffer
,sh_audio
->ds
->ss_mul
);
137 return CONTROL_UNKNOWN
;
140 static void decode_nibbles(unsigned short *output
,
141 int output_size
, int channels
,
142 int predictor
[2], int index
[2])
148 int channel_number
= 0;
150 step
[0] = adpcm_step
[index
[0]];
151 step
[1] = adpcm_step
[index
[1]];
153 for (i
= 0; i
< output_size
; i
++)
159 index
[channel_number
] += adpcm_index
[delta
];
160 CLAMP_0_TO_88(index
[channel_number
]);
162 delta
= 2 * delta
+ 1;
163 if (sign
) delta
= -delta
;
165 predictor
[channel_number
] += (delta
* step
[channel_number
]) >> 3;
167 CLAMP_S16(predictor
[channel_number
]);
168 output
[i
] = predictor
[channel_number
];
169 step
[channel_number
] = adpcm_step
[index
[channel_number
]];
172 channel_number
^= channels
- 1;
177 static int qt_ima_adpcm_decode_block(unsigned short *output
,
178 unsigned char *input
, int channels
, int block_size
)
180 int initial_predictor
[2] = {0};
181 int initial_index
[2] = {0};
184 if (channels
!= 1) channels
= 2;
185 if (block_size
< channels
* QT_IMA_ADPCM_BLOCK_SIZE
)
188 for (i
= 0; i
< channels
; i
++) {
189 initial_index
[i
] = initial_predictor
[i
] = (int16_t)AV_RB16(&input
[i
* QT_IMA_ADPCM_BLOCK_SIZE
]);
191 // mask, sign-extend, and clamp the predictor portion
192 initial_predictor
[i
] &= ~0x7F;
193 CLAMP_S16(initial_predictor
[i
]);
195 // mask and clamp the index portion
196 initial_index
[i
] &= 0x7F;
197 CLAMP_0_TO_88(initial_index
[i
]);
200 // break apart all of the nibbles in the block
202 for (i
= 0; i
< QT_IMA_ADPCM_SAMPLES_PER_BLOCK
/ 2; i
++)
204 output
[i
* 2 + 0] = input
[2 + i
] & 0x0F;
205 output
[i
* 2 + 1] = input
[2 + i
] >> 4;
208 for (i
= 0; i
< QT_IMA_ADPCM_SAMPLES_PER_BLOCK
/ 2; i
++)
210 output
[i
* 4 + 0] = input
[2 + i
] & 0x0F;
211 output
[i
* 4 + 1] = input
[2 + QT_IMA_ADPCM_BLOCK_SIZE
+ i
] & 0x0F;
212 output
[i
* 4 + 2] = input
[2 + i
] >> 4;
213 output
[i
* 4 + 3] = input
[2 + QT_IMA_ADPCM_BLOCK_SIZE
+ i
] >> 4;
216 decode_nibbles(output
,
217 QT_IMA_ADPCM_SAMPLES_PER_BLOCK
* channels
, channels
,
218 initial_predictor
, initial_index
);
220 return QT_IMA_ADPCM_SAMPLES_PER_BLOCK
* channels
;
223 static int ms_ima_adpcm_decode_block(unsigned short *output
,
224 unsigned char *input
, int channels
, int block_size
)
234 if (channels
!= 1) channels
= 2;
235 if (block_size
< MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
)
238 for (i
= 0; i
< channels
; i
++) {
239 predictor
[i
] = (int16_t)AV_RL16(&input
[i
* 4]);
240 index
[i
] = input
[i
* 4 + 2];
245 i
< (block_size
- MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
); i
++)
247 output
[i
* 2 + 0] = input
[MS_IMA_ADPCM_PREAMBLE_SIZE
+ i
] & 0x0F;
248 output
[i
* 2 + 1] = input
[MS_IMA_ADPCM_PREAMBLE_SIZE
+ i
] >> 4;
252 // encoded as 8 nibbles (4 bytes) per channel; switch channel every
257 channel_index
= channel_index_l
;
259 i
< (block_size
- MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
); i
++)
261 output
[channel_index
+ 0] =
262 input
[MS_IMA_ADPCM_PREAMBLE_SIZE
* 2 + i
] & 0x0F;
263 output
[channel_index
+ 2] =
264 input
[MS_IMA_ADPCM_PREAMBLE_SIZE
* 2 + i
] >> 4;
267 if (channel_counter
== 4)
269 channel_index_l
= channel_index
;
270 channel_index
= channel_index_r
;
272 else if (channel_counter
== 8)
274 channel_index_r
= channel_index
;
275 channel_index
= channel_index_l
;
281 decode_nibbles(output
,
282 (block_size
- MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
) * 2,
286 return (block_size
- MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
) * 2;
289 static int dk4_ima_adpcm_decode_block(unsigned short *output
,
290 unsigned char *input
, int channels
, int block_size
)
297 if (channels
!= 1) channels
= 2;
298 if (block_size
< MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
)
301 for (i
= 0; i
< channels
; i
++) {
302 // the first predictor value goes straight to the output
303 predictor
[i
] = output
[i
] = (int16_t)AV_RL16(&input
[i
* 4]);
304 index
[i
] = input
[i
* 4 + 2];
307 output_ptr
= channels
;
308 for (i
= MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
; i
< block_size
; i
++)
310 output
[output_ptr
++] = input
[i
] >> 4;
311 output
[output_ptr
++] = input
[i
] & 0x0F;
314 decode_nibbles(&output
[channels
],
315 (block_size
- MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
) * 2 - channels
,
319 return (block_size
- MS_IMA_ADPCM_PREAMBLE_SIZE
* channels
) * 2 - channels
;
322 static int decode_audio(sh_audio_t
*sh_audio
,unsigned char *buf
,int minlen
,int maxlen
)
325 int (*decode_func
)(unsigned short *output
, unsigned char *input
, int channels
, int block_size
) = qt_ima_adpcm_decode_block
;
326 if (demux_read_data(sh_audio
->ds
, sh_audio
->a_in_buffer
,
327 sh_audio
->ds
->ss_mul
) !=
328 sh_audio
->ds
->ss_mul
)
331 if ((sh_audio
->format
== 0x11) || (sh_audio
->format
== 0x1100736d))
332 decode_func
= ms_ima_adpcm_decode_block
;
333 else if (sh_audio
->format
== 0x61)
334 decode_func
= dk4_ima_adpcm_decode_block
;
336 res
= decode_func((unsigned short*)buf
, sh_audio
->a_in_buffer
,
337 sh_audio
->wf
->nChannels
, sh_audio
->ds
->ss_mul
);
338 return res
< 0 ? res
: 2 * res
;