2002-03-30 22:27:45 +00:00
|
|
|
/*
|
|
|
|
MS ADPCM Decoder for MPlayer
|
|
|
|
by Mike Melanson
|
|
|
|
|
|
|
|
This file is responsible for decoding Microsoft ADPCM data.
|
|
|
|
Details about the data format can be found here:
|
|
|
|
http://www.pcisys.net/~melanson/codecs/
|
|
|
|
*/
|
|
|
|
|
2002-03-25 21:06:01 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
2008-07-11 18:36:21 +00:00
|
|
|
#include "libavutil/common.h"
|
2008-07-11 18:51:12 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2006-12-07 11:58:07 +00:00
|
|
|
#include "mpbswap.h"
|
2002-03-25 21:06:01 +00:00
|
|
|
#include "ad_internal.h"
|
|
|
|
|
|
|
|
static ad_info_t info =
|
|
|
|
{
|
|
|
|
"MS ADPCM audio decoder",
|
|
|
|
"msadpcm",
|
|
|
|
"Nick Kurshev",
|
|
|
|
"Mike Melanson",
|
|
|
|
""
|
|
|
|
};
|
|
|
|
|
|
|
|
LIBAD_EXTERN(msadpcm)
|
|
|
|
|
2008-07-11 18:13:11 +00:00
|
|
|
static const int ms_adapt_table[] =
|
2002-03-30 22:27:45 +00:00
|
|
|
{
|
|
|
|
230, 230, 230, 230, 307, 409, 512, 614,
|
|
|
|
768, 614, 512, 409, 307, 230, 230, 230
|
|
|
|
};
|
|
|
|
|
2008-07-11 18:59:03 +00:00
|
|
|
static const int8_t ms_adapt_coeff1[] =
|
2002-03-30 22:27:45 +00:00
|
|
|
{
|
2008-07-11 18:59:03 +00:00
|
|
|
64, 128, 0, 48, 60, 115, 98
|
2002-03-30 22:27:45 +00:00
|
|
|
};
|
|
|
|
|
2008-07-11 18:59:03 +00:00
|
|
|
static const int8_t ms_adapt_coeff2[] =
|
2002-03-30 22:27:45 +00:00
|
|
|
{
|
2008-07-11 18:59:03 +00:00
|
|
|
0, -64, 0, 16, 0, -52, -58
|
2002-03-30 22:27:45 +00:00
|
|
|
};
|
|
|
|
|
2002-06-29 22:10:18 +00:00
|
|
|
#define MS_ADPCM_PREAMBLE_SIZE 6
|
2002-03-26 04:41:43 +00:00
|
|
|
|
2008-07-11 18:51:12 +00:00
|
|
|
#define LE_16(x) ((int16_t)AV_RL16(x))
|
2002-03-30 22:27:45 +00:00
|
|
|
|
|
|
|
// clamp a number between 0 and 88
|
2008-07-11 18:36:21 +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-07-11 18:36:21 +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;
|
|
|
|
// sign extend a 4-bit value
|
|
|
|
#define SE_4BIT(x) if (x & 0x8) x -= 0x10;
|
|
|
|
|
2002-03-26 04:41:43 +00:00
|
|
|
static int preinit(sh_audio_t *sh_audio)
|
|
|
|
{
|
|
|
|
sh_audio->audio_out_minsize = sh_audio->wf->nBlockAlign * 4;
|
|
|
|
sh_audio->ds->ss_div =
|
|
|
|
(sh_audio->wf->nBlockAlign - MS_ADPCM_PREAMBLE_SIZE) * 2;
|
2002-04-01 17:58:04 +00:00
|
|
|
sh_audio->audio_in_minsize =
|
2002-03-26 04:41:43 +00:00
|
|
|
sh_audio->ds->ss_mul = sh_audio->wf->nBlockAlign;
|
|
|
|
return 1;
|
|
|
|
}
|
2002-03-25 21:06:01 +00:00
|
|
|
|
|
|
|
static int init(sh_audio_t *sh_audio)
|
|
|
|
{
|
|
|
|
sh_audio->channels=sh_audio->wf->nChannels;
|
|
|
|
sh_audio->samplerate=sh_audio->wf->nSamplesPerSec;
|
|
|
|
sh_audio->i_bps = sh_audio->wf->nBlockAlign *
|
2002-03-26 04:41:43 +00:00
|
|
|
(sh_audio->channels*sh_audio->samplerate) / sh_audio->ds->ss_div;
|
2004-09-21 20:34:47 +00:00
|
|
|
sh_audio->samplesize=2;
|
2002-03-26 04:41:43 +00:00
|
|
|
|
2002-03-25 21:06:01 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-03-26 04:41:43 +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 int ms_adpcm_decode_block(unsigned short *output, unsigned char *input,
|
|
|
|
int channels, int block_size)
|
|
|
|
{
|
|
|
|
int current_channel = 0;
|
|
|
|
int idelta[2];
|
|
|
|
int sample1[2];
|
|
|
|
int sample2[2];
|
|
|
|
int coeff1[2];
|
|
|
|
int coeff2[2];
|
|
|
|
int stream_ptr = 0;
|
|
|
|
int out_ptr = 0;
|
|
|
|
int upper_nibble = 1;
|
|
|
|
int nibble;
|
|
|
|
int snibble; // signed nibble
|
|
|
|
int predictor;
|
|
|
|
|
|
|
|
// fetch the header information, in stereo if both channels are present
|
|
|
|
if (input[stream_ptr] > 6)
|
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_WARN,
|
|
|
|
"MS ADPCM: coefficient (%d) out of range (should be [0..6])\n",
|
|
|
|
input[stream_ptr]);
|
|
|
|
coeff1[0] = ms_adapt_coeff1[input[stream_ptr]];
|
|
|
|
coeff2[0] = ms_adapt_coeff2[input[stream_ptr]];
|
|
|
|
stream_ptr++;
|
|
|
|
if (channels == 2)
|
|
|
|
{
|
|
|
|
if (input[stream_ptr] > 6)
|
|
|
|
mp_msg(MSGT_DECAUDIO, MSGL_WARN,
|
|
|
|
"MS ADPCM: coefficient (%d) out of range (should be [0..6])\n",
|
|
|
|
input[stream_ptr]);
|
|
|
|
coeff1[1] = ms_adapt_coeff1[input[stream_ptr]];
|
|
|
|
coeff2[1] = ms_adapt_coeff2[input[stream_ptr]];
|
|
|
|
stream_ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
idelta[0] = LE_16(&input[stream_ptr]);
|
|
|
|
stream_ptr += 2;
|
|
|
|
if (channels == 2)
|
|
|
|
{
|
|
|
|
idelta[1] = LE_16(&input[stream_ptr]);
|
|
|
|
stream_ptr += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
sample1[0] = LE_16(&input[stream_ptr]);
|
|
|
|
stream_ptr += 2;
|
|
|
|
if (channels == 2)
|
|
|
|
{
|
|
|
|
sample1[1] = LE_16(&input[stream_ptr]);
|
|
|
|
stream_ptr += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
sample2[0] = LE_16(&input[stream_ptr]);
|
|
|
|
stream_ptr += 2;
|
|
|
|
if (channels == 2)
|
|
|
|
{
|
|
|
|
sample2[1] = LE_16(&input[stream_ptr]);
|
|
|
|
stream_ptr += 2;
|
|
|
|
}
|
|
|
|
|
2002-06-29 22:10:18 +00:00
|
|
|
if (channels == 1)
|
|
|
|
{
|
|
|
|
output[out_ptr++] = sample2[0];
|
|
|
|
output[out_ptr++] = sample1[0];
|
|
|
|
} else {
|
|
|
|
output[out_ptr++] = sample2[0];
|
|
|
|
output[out_ptr++] = sample2[1];
|
|
|
|
output[out_ptr++] = sample1[0];
|
|
|
|
output[out_ptr++] = sample1[1];
|
|
|
|
}
|
|
|
|
|
2002-03-30 22:27:45 +00:00
|
|
|
while (stream_ptr < block_size)
|
|
|
|
{
|
|
|
|
// get the next nibble
|
|
|
|
if (upper_nibble)
|
|
|
|
nibble = snibble = input[stream_ptr] >> 4;
|
|
|
|
else
|
|
|
|
nibble = snibble = input[stream_ptr++] & 0x0F;
|
|
|
|
upper_nibble ^= 1;
|
|
|
|
SE_4BIT(snibble);
|
|
|
|
|
|
|
|
predictor = (
|
|
|
|
((sample1[current_channel] * coeff1[current_channel]) +
|
2008-07-11 18:59:03 +00:00
|
|
|
(sample2[current_channel] * coeff2[current_channel])) / 64) +
|
2002-03-30 22:27:45 +00:00
|
|
|
(snibble * idelta[current_channel]);
|
|
|
|
CLAMP_S16(predictor);
|
|
|
|
sample2[current_channel] = sample1[current_channel];
|
|
|
|
sample1[current_channel] = predictor;
|
|
|
|
output[out_ptr++] = predictor;
|
|
|
|
|
|
|
|
// compute the next adaptive scale factor (a.k.a. the variable idelta)
|
|
|
|
idelta[current_channel] =
|
|
|
|
(ms_adapt_table[nibble] * idelta[current_channel]) / 256;
|
|
|
|
CLAMP_ABOVE_16(idelta[current_channel]);
|
|
|
|
|
|
|
|
// toggle the channel
|
|
|
|
current_channel ^= channels - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (block_size - (MS_ADPCM_PREAMBLE_SIZE * channels)) * 2;
|
|
|
|
}
|
|
|
|
|
2002-03-25 21:06:01 +00:00
|
|
|
static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
|
|
|
|
{
|
2002-03-26 04:41:43 +00:00
|
|
|
if (demux_read_data(sh_audio->ds, sh_audio->a_in_buffer,
|
2002-03-30 22:27:45 +00:00
|
|
|
sh_audio->ds->ss_mul) !=
|
|
|
|
sh_audio->ds->ss_mul)
|
|
|
|
return -1; /* EOF */
|
2002-03-26 04:41:43 +00:00
|
|
|
|
2002-03-25 21:06:01 +00:00
|
|
|
return 2 * ms_adpcm_decode_block(
|
2002-03-30 22:27:45 +00:00
|
|
|
(unsigned short*)buf, sh_audio->a_in_buffer,
|
|
|
|
sh_audio->wf->nChannels, sh_audio->wf->nBlockAlign);
|
2002-03-25 21:06:01 +00:00
|
|
|
}
|