mp_msg: print messages to stdout, statusline to stderr
[mplayer.git] / libmpcodecs / ad_imaadpcm.c
blob84f667ecd684f3a0818a51bcda1f4b261de32995
1 /*
2 * IMA ADPCM decoder
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.
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <inttypes.h>
41 #include <libavutil/intreadwrite.h>
43 #include "config.h"
44 #include "mpbswap.h"
45 #include "ad_internal.h"
47 #define MS_IMA_ADPCM_PREAMBLE_SIZE 4
49 #define QT_IMA_ADPCM_PREAMBLE_SIZE 2
50 #define QT_IMA_ADPCM_BLOCK_SIZE 0x22
51 #define QT_IMA_ADPCM_SAMPLES_PER_BLOCK 64
53 // pertinent tables for IMA ADPCM
54 static const int16_t adpcm_step[89] =
56 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
57 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
58 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
59 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
60 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
61 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
62 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
63 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
64 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
67 static const int8_t adpcm_index[8] =
69 -1, -1, -1, -1, 2, 4, 6, 8,
72 // useful macros
73 // clamp a number between 0 and 88
74 #define CLAMP_0_TO_88(x) x = av_clip(x, 0, 88);
75 // clamp a number within a signed 16-bit range
76 #define CLAMP_S16(x) x = av_clip_int16(x);
77 // clamp a number above 16
78 #define CLAMP_ABOVE_16(x) if (x < 16) x = 16;
80 static const ad_info_t info =
82 "IMA ADPCM audio decoder",
83 "imaadpcm",
84 "Nick Kurshev",
85 "Mike Melanson",
89 LIBAD_EXTERN(imaadpcm)
91 static int preinit(sh_audio_t *sh_audio)
93 // not exactly sure what this field is for
94 sh_audio->audio_out_minsize = 8192;
96 // if format is "ima4", assume the audio is coming from a QT file which
97 // indicates constant block size, whereas an AVI/ASF/WAV file will fill
98 // in this field with 0x11
99 if ((sh_audio->format == 0x11) || (sh_audio->format == 0x61) ||
100 (sh_audio->format == 0x1100736d))
102 sh_audio->ds->ss_div = (sh_audio->wf->nBlockAlign -
103 (MS_IMA_ADPCM_PREAMBLE_SIZE * sh_audio->wf->nChannels)) * 2;
104 sh_audio->ds->ss_mul = sh_audio->wf->nBlockAlign;
106 else
108 sh_audio->ds->ss_div = QT_IMA_ADPCM_SAMPLES_PER_BLOCK;
109 sh_audio->ds->ss_mul = QT_IMA_ADPCM_BLOCK_SIZE * sh_audio->wf->nChannels;
111 sh_audio->audio_in_minsize=sh_audio->ds->ss_mul;
112 return 1;
115 static int init(sh_audio_t *sh_audio)
117 /* IMA-ADPCM 4:1 audio codec:*/
118 sh_audio->channels=sh_audio->wf->nChannels;
119 sh_audio->samplerate=sh_audio->wf->nSamplesPerSec;
120 /* decodes 34 byte -> 64 short*/
121 sh_audio->i_bps =
122 (sh_audio->ds->ss_mul * sh_audio->samplerate) / sh_audio->ds->ss_div;
123 sh_audio->samplesize=2;
125 return 1;
128 static void uninit(sh_audio_t *sh_audio)
132 static int control(sh_audio_t *sh_audio,int cmd,void* arg, ...)
134 if(cmd==ADCTRL_SKIP_FRAME){
135 demux_read_data(sh_audio->ds, sh_audio->a_in_buffer,sh_audio->ds->ss_mul);
136 return CONTROL_TRUE;
138 return CONTROL_UNKNOWN;
141 static void decode_nibbles(unsigned short *output,
142 int output_size, int channels,
143 int predictor[2], int index[2])
145 int step[2];
146 int i;
147 int sign;
148 int delta;
149 int channel_number = 0;
151 step[0] = adpcm_step[index[0]];
152 step[1] = adpcm_step[index[1]];
154 for (i = 0; i < output_size; i++)
156 delta = output[i];
157 sign = delta & 8;
158 delta = delta & 7;
160 index[channel_number] += adpcm_index[delta];
161 CLAMP_0_TO_88(index[channel_number]);
163 delta = 2 * delta + 1;
164 if (sign) delta = -delta;
166 predictor[channel_number] += (delta * step[channel_number]) >> 3;
168 CLAMP_S16(predictor[channel_number]);
169 output[i] = predictor[channel_number];
170 step[channel_number] = adpcm_step[index[channel_number]];
172 // toggle channel
173 channel_number ^= channels - 1;
178 static int qt_ima_adpcm_decode_block(unsigned short *output,
179 unsigned char *input, int channels, int block_size)
181 int initial_predictor[2] = {0};
182 int initial_index[2] = {0};
183 int i;
185 if (channels != 1) channels = 2;
186 if (block_size < channels * QT_IMA_ADPCM_BLOCK_SIZE)
187 return -1;
189 for (i = 0; i < channels; i++) {
190 initial_index[i] = initial_predictor[i] = (int16_t)AV_RB16(&input[i * QT_IMA_ADPCM_BLOCK_SIZE]);
192 // mask, sign-extend, and clamp the predictor portion
193 initial_predictor[i] &= ~0x7F;
194 CLAMP_S16(initial_predictor[i]);
196 // mask and clamp the index portion
197 initial_index[i] &= 0x7F;
198 CLAMP_0_TO_88(initial_index[i]);
201 // break apart all of the nibbles in the block
202 if (channels == 1)
203 for (i = 0; i < QT_IMA_ADPCM_SAMPLES_PER_BLOCK / 2; i++)
205 output[i * 2 + 0] = input[2 + i] & 0x0F;
206 output[i * 2 + 1] = input[2 + i] >> 4;
208 else
209 for (i = 0; i < QT_IMA_ADPCM_SAMPLES_PER_BLOCK / 2; i++)
211 output[i * 4 + 0] = input[2 + i] & 0x0F;
212 output[i * 4 + 1] = input[2 + QT_IMA_ADPCM_BLOCK_SIZE + i] & 0x0F;
213 output[i * 4 + 2] = input[2 + i] >> 4;
214 output[i * 4 + 3] = input[2 + QT_IMA_ADPCM_BLOCK_SIZE + i] >> 4;
217 decode_nibbles(output,
218 QT_IMA_ADPCM_SAMPLES_PER_BLOCK * channels, channels,
219 initial_predictor, initial_index);
221 return QT_IMA_ADPCM_SAMPLES_PER_BLOCK * channels;
224 static int ms_ima_adpcm_decode_block(unsigned short *output,
225 unsigned char *input, int channels, int block_size)
227 int predictor[2];
228 int index[2];
229 int i;
230 int channel_counter;
231 int channel_index;
232 int channel_index_l;
233 int channel_index_r;
235 if (channels != 1) channels = 2;
236 if (block_size < MS_IMA_ADPCM_PREAMBLE_SIZE * channels)
237 return -1;
239 for (i = 0; i < channels; i++) {
240 predictor[i] = (int16_t)AV_RL16(&input[i * 4]);
241 index[i] = input[i * 4 + 2];
244 if (channels == 1)
245 for (i = 0;
246 i < (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels); i++)
248 output[i * 2 + 0] = input[MS_IMA_ADPCM_PREAMBLE_SIZE + i] & 0x0F;
249 output[i * 2 + 1] = input[MS_IMA_ADPCM_PREAMBLE_SIZE + i] >> 4;
251 else
253 // encoded as 8 nibbles (4 bytes) per channel; switch channel every
254 // 4th byte
255 channel_counter = 0;
256 channel_index_l = 0;
257 channel_index_r = 1;
258 channel_index = channel_index_l;
259 for (i = 0;
260 i < (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels); i++)
262 output[channel_index + 0] =
263 input[MS_IMA_ADPCM_PREAMBLE_SIZE * 2 + i] & 0x0F;
264 output[channel_index + 2] =
265 input[MS_IMA_ADPCM_PREAMBLE_SIZE * 2 + i] >> 4;
266 channel_index += 4;
267 channel_counter++;
268 if (channel_counter == 4)
270 channel_index_l = channel_index;
271 channel_index = channel_index_r;
273 else if (channel_counter == 8)
275 channel_index_r = channel_index;
276 channel_index = channel_index_l;
277 channel_counter = 0;
282 decode_nibbles(output,
283 (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2,
284 channels,
285 predictor, index);
287 return (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2;
290 static int dk4_ima_adpcm_decode_block(unsigned short *output,
291 unsigned char *input, int channels, int block_size)
293 int i;
294 int output_ptr;
295 int predictor[2];
296 int index[2];
298 if (channels != 1) channels = 2;
299 if (block_size < MS_IMA_ADPCM_PREAMBLE_SIZE * channels)
300 return -1;
302 for (i = 0; i < channels; i++) {
303 // the first predictor value goes straight to the output
304 predictor[i] = output[i] = (int16_t)AV_RL16(&input[i * 4]);
305 index[i] = input[i * 4 + 2];
308 output_ptr = channels;
309 for (i = MS_IMA_ADPCM_PREAMBLE_SIZE * channels; i < block_size; i++)
311 output[output_ptr++] = input[i] >> 4;
312 output[output_ptr++] = input[i] & 0x0F;
315 decode_nibbles(&output[channels],
316 (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2 - channels,
317 channels,
318 predictor, index);
320 return (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2 - channels;
323 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
325 int res = -1;
326 int (*decode_func)(unsigned short *output, unsigned char *input, int channels, int block_size) = qt_ima_adpcm_decode_block;
327 if (demux_read_data(sh_audio->ds, sh_audio->a_in_buffer,
328 sh_audio->ds->ss_mul) !=
329 sh_audio->ds->ss_mul)
330 return -1;
332 if ((sh_audio->format == 0x11) || (sh_audio->format == 0x1100736d))
333 decode_func = ms_ima_adpcm_decode_block;
334 else if (sh_audio->format == 0x61)
335 decode_func = dk4_ima_adpcm_decode_block;
337 res = decode_func((unsigned short*)buf, sh_audio->a_in_buffer,
338 sh_audio->wf->nChannels, sh_audio->ds->ss_mul);
339 return res < 0 ? res : 2 * res;