2002-03-30 22:27:45 +00:00
|
|
|
/*
|
|
|
|
IMA ADPCM Decoder for MPlayer
|
|
|
|
by Mike Melanson
|
|
|
|
|
|
|
|
This file is in charge of decoding all of the various IMA ADPCM data
|
|
|
|
formats that various entities have created. Details about the data
|
|
|
|
formats can be found here:
|
|
|
|
http://www.pcisys.net/~melanson/codecs/
|
|
|
|
|
|
|
|
So far, this file handles these formats:
|
|
|
|
'ima4': IMA ADPCM found in QT files
|
|
|
|
0x11: IMA ADPCM found in MS AVI/ASF/WAV files
|
|
|
|
0x61: DK4 ADPCM found in certain AVI files on Sega Saturn CD-ROMs;
|
|
|
|
note that this is a 'rogue' format number in that it was
|
|
|
|
never officially registered with Microsoft
|
2002-11-04 20:31:46 +00:00
|
|
|
0x1100736d: IMA ADPCM coded like in MS AVI/ASF/WAV found in QT files
|
2002-03-30 22:27:45 +00:00
|
|
|
*/
|
|
|
|
|
2002-03-25 21:06:01 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2008-07-11 18:10:06 +00:00
|
|
|
#include <inttypes.h>
|
2002-03-25 21:06:01 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
2006-11-29 15:02:45 +00:00
|
|
|
#include "libavutil/common.h"
|
2006-12-07 11:58:07 +00:00
|
|
|
#include "mpbswap.h"
|
2002-03-25 21:06:01 +00:00
|
|
|
#include "ad_internal.h"
|
|
|
|
|
2002-03-30 22:27:45 +00:00
|
|
|
#define MS_IMA_ADPCM_PREAMBLE_SIZE 4
|
|
|
|
|
|
|
|
#define QT_IMA_ADPCM_PREAMBLE_SIZE 2
|
|
|
|
#define QT_IMA_ADPCM_BLOCK_SIZE 0x22
|
|
|
|
#define QT_IMA_ADPCM_SAMPLES_PER_BLOCK 64
|
|
|
|
|
|
|
|
#define BE_16(x) (be2me_16(*(unsigned short *)(x)))
|
|
|
|
#define LE_16(x) (le2me_16(*(unsigned short *)(x)))
|
|
|
|
|
|
|
|
// pertinent tables for IMA ADPCM
|
2008-06-29 08:27:52 +00:00
|
|
|
static const int16_t adpcm_step[89] =
|
2002-03-30 22:27:45 +00:00
|
|
|
{
|
|
|
|
7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
|
|
|
|
19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
|
|
|
|
50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
|
|
|
|
130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
|
|
|
|
337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
|
|
|
|
876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
|
|
|
|
2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
|
|
|
|
5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
|
|
|
|
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
|
|
|
|
};
|
|
|
|
|
2008-06-29 09:14:26 +00:00
|
|
|
static const int8_t adpcm_index[8] =
|
2002-03-30 22:27:45 +00:00
|
|
|
{
|
|
|
|
-1, -1, -1, -1, 2, 4, 6, 8,
|
|
|
|
};
|
|
|
|
|
|
|
|
// useful macros
|
|
|
|
// clamp a number between 0 and 88
|
2008-06-29 08:42:53 +00:00
|
|
|
#define CLAMP_0_TO_88(x) x = av_clip(x, 0, 88);
|
2002-03-30 22:27:45 +00:00
|
|
|
// clamp a number within a signed 16-bit range
|
2008-06-29 08:42:53 +00:00
|
|
|
#define CLAMP_S16(x) x = av_clip_int16(x);
|
2002-03-30 22:27:45 +00:00
|
|
|
// clamp a number above 16
|
|
|
|
#define CLAMP_ABOVE_16(x) if (x < 16) x = 16;
|
2002-03-25 21:06:01 +00:00
|
|
|
|
2008-04-26 13:48:06 +00:00
|
|
|
static const ad_info_t info =
|
2002-03-25 21:06:01 +00:00
|
|
|
{
|
|
|
|
"IMA ADPCM audio decoder",
|
|
|
|
"imaadpcm",
|
|
|
|
"Nick Kurshev",
|
|
|
|
"Mike Melanson",
|
2002-03-30 22:27:45 +00:00
|
|
|
""
|
2002-03-25 21:06:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
LIBAD_EXTERN(imaadpcm)
|
|
|
|
|
2002-03-30 22:27:45 +00:00
|
|
|
static int preinit(sh_audio_t *sh_audio)
|
|
|
|
{
|
|
|
|
// not exactly sure what this field is for
|
|
|
|
sh_audio->audio_out_minsize = 8192;
|
|
|
|
|
|
|
|
// if format is "ima4", assume the audio is coming from a QT file which
|
|
|
|
// indicates constant block size, whereas an AVI/ASF/WAV file will fill
|
|
|
|
// in this field with 0x11
|
2002-11-04 20:31:46 +00:00
|
|
|
if ((sh_audio->format == 0x11) || (sh_audio->format == 0x61) ||
|
|
|
|
(sh_audio->format == 0x1100736d))
|
2002-03-30 22:27:45 +00:00
|
|
|
{
|
|
|
|
sh_audio->ds->ss_div = (sh_audio->wf->nBlockAlign -
|
|
|
|
(MS_IMA_ADPCM_PREAMBLE_SIZE * sh_audio->wf->nChannels)) * 2;
|
|
|
|
sh_audio->ds->ss_mul = sh_audio->wf->nBlockAlign;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sh_audio->ds->ss_div = QT_IMA_ADPCM_SAMPLES_PER_BLOCK;
|
|
|
|
sh_audio->ds->ss_mul = QT_IMA_ADPCM_BLOCK_SIZE * sh_audio->wf->nChannels;
|
|
|
|
}
|
2002-04-01 17:58:04 +00:00
|
|
|
sh_audio->audio_in_minsize=sh_audio->ds->ss_mul;
|
2002-03-30 22:27:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-03-25 21:06:01 +00:00
|
|
|
static int init(sh_audio_t *sh_audio)
|
|
|
|
{
|
|
|
|
/* IMA-ADPCM 4:1 audio codec:*/
|
|
|
|
sh_audio->channels=sh_audio->wf->nChannels;
|
|
|
|
sh_audio->samplerate=sh_audio->wf->nSamplesPerSec;
|
|
|
|
/* decodes 34 byte -> 64 short*/
|
2002-03-30 22:27:45 +00:00
|
|
|
sh_audio->i_bps =
|
|
|
|
(sh_audio->ds->ss_mul * sh_audio->samplerate) / sh_audio->ds->ss_div;
|
2004-09-21 20:34:47 +00:00
|
|
|
sh_audio->samplesize=2;
|
2002-03-30 22:27:45 +00:00
|
|
|
|
2002-03-25 21:06:01 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-03-30 22:27:45 +00:00
|
|
|
static void uninit(sh_audio_t *sh_audio)
|
2002-03-25 21:06:01 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-04-03 21:37:26 +00:00
|
|
|
static int control(sh_audio_t *sh_audio,int cmd,void* arg, ...)
|
2002-03-25 21:06:01 +00:00
|
|
|
{
|
2002-04-03 21:37:26 +00:00
|
|
|
if(cmd==ADCTRL_SKIP_FRAME){
|
|
|
|
demux_read_data(sh_audio->ds, sh_audio->a_in_buffer,sh_audio->ds->ss_mul);
|
|
|
|
return CONTROL_TRUE;
|
|
|
|
}
|
2002-03-25 21:06:01 +00:00
|
|
|
return CONTROL_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2002-03-30 22:27:45 +00:00
|
|
|
static void decode_nibbles(unsigned short *output,
|
|
|
|
int output_size, int channels,
|
2008-06-29 08:35:27 +00:00
|
|
|
int predictor[2], int index[2])
|
2002-03-30 22:27:45 +00:00
|
|
|
{
|
|
|
|
int step[2];
|
|
|
|
int i;
|
|
|
|
int sign;
|
|
|
|
int delta;
|
|
|
|
int channel_number = 0;
|
|
|
|
|
2008-06-29 08:35:27 +00:00
|
|
|
step[0] = adpcm_step[index[0]];
|
|
|
|
step[1] = adpcm_step[index[1]];
|
2002-03-30 22:27:45 +00:00
|
|
|
|
|
|
|
for (i = 0; i < output_size; i++)
|
|
|
|
{
|
|
|
|
delta = output[i];
|
2008-06-29 09:14:26 +00:00
|
|
|
sign = delta & 8;
|
|
|
|
delta = delta & 7;
|
2002-03-30 22:27:45 +00:00
|
|
|
|
|
|
|
index[channel_number] += adpcm_index[delta];
|
|
|
|
CLAMP_0_TO_88(index[channel_number]);
|
|
|
|
|
2008-06-29 09:10:46 +00:00
|
|
|
delta = 2 * delta + 1;
|
|
|
|
if (sign) delta = -delta;
|
2002-03-30 22:27:45 +00:00
|
|
|
|
2008-06-29 09:10:46 +00:00
|
|
|
predictor[channel_number] += (delta * step[channel_number]) >> 3;
|
2002-03-30 22:27:45 +00:00
|
|
|
|
|
|
|
CLAMP_S16(predictor[channel_number]);
|
|
|
|
output[i] = predictor[channel_number];
|
|
|
|
step[channel_number] = adpcm_step[index[channel_number]];
|
|
|
|
|
|
|
|
// toggle channel
|
|
|
|
channel_number ^= channels - 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qt_ima_adpcm_decode_block(unsigned short *output,
|
2008-06-29 07:55:44 +00:00
|
|
|
unsigned char *input, int channels, int block_size)
|
2002-03-30 22:27:45 +00:00
|
|
|
{
|
2008-06-29 08:20:42 +00:00
|
|
|
int initial_predictor[2];
|
|
|
|
int initial_index[2];
|
2002-03-30 22:27:45 +00:00
|
|
|
int i;
|
|
|
|
|
2008-07-11 19:29:41 +00:00
|
|
|
if (channels != 1) channels = 2;
|
2008-06-29 08:08:51 +00:00
|
|
|
if (block_size < channels * QT_IMA_ADPCM_BLOCK_SIZE)
|
|
|
|
return -1;
|
2002-03-30 22:27:45 +00:00
|
|
|
|
2008-06-29 08:20:42 +00:00
|
|
|
for (i = 0; i < channels; i++) {
|
2008-06-29 08:47:56 +00:00
|
|
|
initial_index[i] = initial_predictor[i] = (int16_t)BE_16(&input[i * QT_IMA_ADPCM_BLOCK_SIZE]);
|
2002-03-30 22:27:45 +00:00
|
|
|
|
|
|
|
// mask, sign-extend, and clamp the predictor portion
|
2008-06-29 08:47:56 +00:00
|
|
|
initial_predictor[i] &= ~0x7F;
|
2008-06-29 08:20:42 +00:00
|
|
|
CLAMP_S16(initial_predictor[i]);
|
2002-03-30 22:27:45 +00:00
|
|
|
|
|
|
|
// mask and clamp the index portion
|
2008-06-29 08:20:42 +00:00
|
|
|
initial_index[i] &= 0x7F;
|
|
|
|
CLAMP_0_TO_88(initial_index[i]);
|
2002-03-30 22:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// break apart all of the nibbles in the block
|
|
|
|
if (channels == 1)
|
|
|
|
for (i = 0; i < QT_IMA_ADPCM_SAMPLES_PER_BLOCK / 2; i++)
|
|
|
|
{
|
|
|
|
output[i * 2 + 0] = input[2 + i] & 0x0F;
|
|
|
|
output[i * 2 + 1] = input[2 + i] >> 4;
|
|
|
|
}
|
|
|
|
else
|
2005-06-20 15:10:40 +00:00
|
|
|
for (i = 0; i < QT_IMA_ADPCM_SAMPLES_PER_BLOCK / 2; i++)
|
2002-03-30 22:27:45 +00:00
|
|
|
{
|
|
|
|
output[i * 4 + 0] = input[2 + i] & 0x0F;
|
|
|
|
output[i * 4 + 1] = input[2 + QT_IMA_ADPCM_BLOCK_SIZE + i] & 0x0F;
|
|
|
|
output[i * 4 + 2] = input[2 + i] >> 4;
|
|
|
|
output[i * 4 + 3] = input[2 + QT_IMA_ADPCM_BLOCK_SIZE + i] >> 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
decode_nibbles(output,
|
|
|
|
QT_IMA_ADPCM_SAMPLES_PER_BLOCK * channels, channels,
|
2008-06-29 08:35:27 +00:00
|
|
|
initial_predictor, initial_index);
|
2002-03-30 22:27:45 +00:00
|
|
|
|
|
|
|
return QT_IMA_ADPCM_SAMPLES_PER_BLOCK * channels;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ms_ima_adpcm_decode_block(unsigned short *output,
|
|
|
|
unsigned char *input, int channels, int block_size)
|
|
|
|
{
|
2008-06-29 08:20:42 +00:00
|
|
|
int predictor[2];
|
|
|
|
int index[2];
|
2002-03-30 22:27:45 +00:00
|
|
|
int i;
|
|
|
|
int channel_counter;
|
|
|
|
int channel_index;
|
|
|
|
int channel_index_l;
|
|
|
|
int channel_index_r;
|
|
|
|
|
2008-07-11 19:29:41 +00:00
|
|
|
if (channels != 1) channels = 2;
|
2008-06-29 08:08:51 +00:00
|
|
|
if (block_size < MS_IMA_ADPCM_PREAMBLE_SIZE * channels)
|
|
|
|
return -1;
|
|
|
|
|
2008-06-29 08:20:42 +00:00
|
|
|
for (i = 0; i < channels; i++) {
|
2008-06-29 08:47:56 +00:00
|
|
|
predictor[i] = (int16_t)LE_16(&input[i * 4]);
|
2008-06-29 08:20:42 +00:00
|
|
|
index[i] = input[i * 4 + 2];
|
2002-03-30 22:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (channels == 1)
|
|
|
|
for (i = 0;
|
2003-09-03 22:30:30 +00:00
|
|
|
i < (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels); i++)
|
2002-03-30 22:27:45 +00:00
|
|
|
{
|
|
|
|
output[i * 2 + 0] = input[MS_IMA_ADPCM_PREAMBLE_SIZE + i] & 0x0F;
|
|
|
|
output[i * 2 + 1] = input[MS_IMA_ADPCM_PREAMBLE_SIZE + i] >> 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// encoded as 8 nibbles (4 bytes) per channel; switch channel every
|
|
|
|
// 4th byte
|
|
|
|
channel_counter = 0;
|
|
|
|
channel_index_l = 0;
|
|
|
|
channel_index_r = 1;
|
|
|
|
channel_index = channel_index_l;
|
|
|
|
for (i = 0;
|
|
|
|
i < (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels); i++)
|
|
|
|
{
|
|
|
|
output[channel_index + 0] =
|
|
|
|
input[MS_IMA_ADPCM_PREAMBLE_SIZE * 2 + i] & 0x0F;
|
|
|
|
output[channel_index + 2] =
|
|
|
|
input[MS_IMA_ADPCM_PREAMBLE_SIZE * 2 + i] >> 4;
|
|
|
|
channel_index += 4;
|
|
|
|
channel_counter++;
|
|
|
|
if (channel_counter == 4)
|
|
|
|
{
|
|
|
|
channel_index_l = channel_index;
|
|
|
|
channel_index = channel_index_r;
|
|
|
|
}
|
|
|
|
else if (channel_counter == 8)
|
|
|
|
{
|
|
|
|
channel_index_r = channel_index;
|
|
|
|
channel_index = channel_index_l;
|
|
|
|
channel_counter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
decode_nibbles(output,
|
|
|
|
(block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2,
|
|
|
|
channels,
|
2008-06-29 08:35:27 +00:00
|
|
|
predictor, index);
|
2002-03-30 22:27:45 +00:00
|
|
|
|
|
|
|
return (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dk4_ima_adpcm_decode_block(unsigned short *output,
|
|
|
|
unsigned char *input, int channels, int block_size)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int output_ptr;
|
2008-06-29 08:20:42 +00:00
|
|
|
int predictor[2];
|
|
|
|
int index[2];
|
2002-03-30 22:27:45 +00:00
|
|
|
|
2008-07-11 19:29:41 +00:00
|
|
|
if (channels != 1) channels = 2;
|
2008-06-29 08:08:51 +00:00
|
|
|
if (block_size < MS_IMA_ADPCM_PREAMBLE_SIZE * channels)
|
|
|
|
return -1;
|
|
|
|
|
2008-06-29 08:20:42 +00:00
|
|
|
for (i = 0; i < channels; i++) {
|
|
|
|
// the first predictor value goes straight to the output
|
2008-06-29 08:47:56 +00:00
|
|
|
predictor[i] = output[i] = (int16_t)LE_16(&input[i * 4]);
|
2008-06-29 08:20:42 +00:00
|
|
|
index[i] = input[i * 4 + 2];
|
2002-03-30 22:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
output_ptr = channels;
|
|
|
|
for (i = MS_IMA_ADPCM_PREAMBLE_SIZE * channels; i < block_size; i++)
|
|
|
|
{
|
|
|
|
output[output_ptr++] = input[i] >> 4;
|
|
|
|
output[output_ptr++] = input[i] & 0x0F;
|
|
|
|
}
|
|
|
|
|
|
|
|
decode_nibbles(&output[channels],
|
|
|
|
(block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2 - channels,
|
|
|
|
channels,
|
2008-06-29 08:35:27 +00:00
|
|
|
predictor, index);
|
2002-03-30 22:27:45 +00:00
|
|
|
|
|
|
|
return (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2 - channels;
|
|
|
|
}
|
|
|
|
|
2002-03-25 21:06:01 +00:00
|
|
|
static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
|
|
|
|
{
|
2008-06-29 07:55:44 +00:00
|
|
|
int res = -1;
|
|
|
|
int (*decode_func)(unsigned short *output, unsigned char *input, int channels, int block_size) = qt_ima_adpcm_decode_block;
|
2002-03-30 22:27:45 +00:00
|
|
|
if (demux_read_data(sh_audio->ds, sh_audio->a_in_buffer,
|
|
|
|
sh_audio->ds->ss_mul) !=
|
|
|
|
sh_audio->ds->ss_mul)
|
|
|
|
return -1;
|
|
|
|
|
2002-11-04 20:31:46 +00:00
|
|
|
if ((sh_audio->format == 0x11) || (sh_audio->format == 0x1100736d))
|
2008-06-29 07:55:44 +00:00
|
|
|
decode_func = ms_ima_adpcm_decode_block;
|
2002-03-30 22:27:45 +00:00
|
|
|
else if (sh_audio->format == 0x61)
|
2008-06-29 07:55:44 +00:00
|
|
|
decode_func = dk4_ima_adpcm_decode_block;
|
|
|
|
|
|
|
|
res = decode_func((unsigned short*)buf, sh_audio->a_in_buffer,
|
|
|
|
sh_audio->wf->nChannels, sh_audio->ds->ss_mul);
|
2008-07-11 19:28:22 +00:00
|
|
|
return res < 0 ? res : 2 * res;
|
2002-03-25 21:06:01 +00:00
|
|
|
}
|