summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--adpcm.c136
-rw-r--r--adpcm.h32
-rw-r--r--codec-cfg.c1
-rw-r--r--codec-cfg.h1
-rw-r--r--dec_audio.c23
6 files changed, 186 insertions, 9 deletions
diff --git a/Makefile b/Makefile
index 2bc13f09dc..bc2d55b733 100644
--- a/Makefile
+++ b/Makefile
@@ -22,7 +22,7 @@ MANDIR = ${prefix}/man
# a BSD compatible 'install' program
INSTALL = install
-SRCS_COMMON = ima4.c xacodec.c cpudetect.c mp_msg.c ac3-iec958.c dec_audio.c dec_video.c msvidc.c cinepak.c fli.c qtrle.c codec-cfg.c cfgparser.c my_profile.c
+SRCS_COMMON = ima4.c adpcm.c xacodec.c cpudetect.c mp_msg.c ac3-iec958.c dec_audio.c dec_video.c msvidc.c cinepak.c fli.c qtrle.c codec-cfg.c cfgparser.c my_profile.c
SRCS_MENCODER = mencoder.c $(SRCS_COMMON) libao2/afmt.c divx4_vbr.c libvo/aclib.c libvo/img_format.c
SRCS_MPLAYER = mplayer.c $(SRCS_COMMON) find_sub.c subreader.c lirc_mp.c mixer.c spudec.c
diff --git a/adpcm.c b/adpcm.c
new file mode 100644
index 0000000000..91d56a6e0c
--- /dev/null
+++ b/adpcm.c
@@ -0,0 +1,136 @@
+/*
+ Unified ADPCM Decoder for MPlayer
+
+ (C) 2001 Mike Melanson
+*/
+
+#include "config.h"
+#include "bswap.h"
+#include "adpcm.h"
+
+#define BE_16(x) (be2me_16(*(unsigned short *)(x)))
+#define BE_32(x) (be2me_32(*(unsigned int *)(x)))
+#define LE_16(x) (le2me_16(*(unsigned short *)(x)))
+#define LE_32(x) (le2me_32(*(unsigned int *)(x)))
+
+// clamp a number between 0 and 88
+#define CLAMP_0_TO_88(x) if (x < 0) x = 0; else if (x > 88) x = 88;
+// clamp a number within a signed 16-bit range
+#define CLAMP_S16(x) if (x < -32768) x = -32768; \
+ else if (x > 32767) x = 32767;
+// sign extend a 16-bit value
+#define SE_16BIT(x) if (x & 0x8000) x -= 0x10000;
+
+void ima_dvi_decode_nibbles(unsigned short *output, int channels,
+ int predictor_l, int index_l,
+ int predictor_r, int index_r)
+{
+ int step[2];
+ int predictor[2];
+ int index[2];
+ int diff;
+ int i;
+ int sign;
+ int delta;
+ int channel_number = 0;
+
+ step[0] = adpcm_step[index_l];
+ step[1] = adpcm_step[index_r];
+ predictor[0] = predictor_l;
+ predictor[1] = predictor_r;
+ index[0] = index_l;
+ index[1] = index_r;
+
+ for (i = 0; i < IMA_ADPCM_SAMPLES_PER_BLOCK * channels; i++)
+ {
+ delta = output[i];
+
+ index[channel_number] += adpcm_index[delta];
+ CLAMP_0_TO_88(index[channel_number]);
+
+ sign = delta & 8;
+ delta = delta & 7;
+
+ diff = step[channel_number] >> 3;
+ if (delta & 4) diff += step[channel_number];
+ if (delta & 2) diff += step[channel_number] >> 1;
+ if (delta & 1) diff += step[channel_number] >> 2;
+
+ if (sign)
+ predictor[channel_number] -= diff;
+ else
+ predictor[channel_number] += diff;
+
+ CLAMP_S16(predictor[channel_number]);
+ output[i] = predictor[channel_number];
+ step[channel_number] = adpcm_step[index[channel_number]];
+
+ // toggle channel
+ channel_number ^= channels - 1;
+ }
+}
+
+int ima_adpcm_decode_block(unsigned short *output, unsigned char *input,
+ int channels)
+{
+ int initial_predictor_l = 0;
+ int initial_predictor_r = 0;
+ int initial_index_l = 0;
+ int initial_index_r = 0;
+ int stream_ptr = 0;
+ int i;
+
+ initial_predictor_l = BE_16(&input[stream_ptr]);
+ stream_ptr += 2;
+ initial_index_l = initial_predictor_l;
+
+ // mask, sign-extend, and clamp the predictor portion
+ initial_predictor_l &= 0xFF80;
+ SE_16BIT(initial_predictor_l);
+ CLAMP_S16(initial_predictor_l);
+
+ // mask and clamp the index portion
+ initial_index_l &= 0x7F;
+ CLAMP_0_TO_88(initial_index_l);
+
+ // handle stereo
+ if (channels > 1)
+ {
+ initial_predictor_r = BE_16(&input[stream_ptr]);
+ stream_ptr += 2;
+ initial_index_r = initial_predictor_r;
+
+ // mask, sign-extend, and clamp the predictor portion
+ initial_predictor_r &= 0xFF80;
+ SE_16BIT(initial_predictor_r);
+ CLAMP_S16(initial_predictor_r);
+
+ // mask and clamp the index portion
+ initial_index_r &= 0x7F;
+ CLAMP_0_TO_88(initial_index_r);
+ }
+
+ // break apart all of the nibbles in the block
+ if (channels == 1)
+ for (i = 0; i < IMA_ADPCM_SAMPLES_PER_BLOCK / 2; i++)
+ {
+ output[i * 2 + 0] = input[stream_ptr] & 0x0F;
+ output[i * 2 + 1] = input[stream_ptr] >> 4;
+ stream_ptr++;
+ }
+ else
+ for (i = 0; i < IMA_ADPCM_SAMPLES_PER_BLOCK / 2 * 2; i++)
+ {
+ output[i * 4 + 0] = input[stream_ptr] & 0x0F;
+ output[i * 4 + 1] = input[stream_ptr + 1] & 0x0F;
+ output[i * 4 + 2] = input[stream_ptr] >> 4;
+ output[i * 4 + 3] = input[stream_ptr + 1] >> 4;
+ stream_ptr++;
+ }
+
+ ima_dvi_decode_nibbles(output, channels,
+ initial_predictor_l, initial_index_l,
+ initial_predictor_r, initial_index_r);
+
+ return IMA_ADPCM_SAMPLES_PER_BLOCK * channels;
+}
diff --git a/adpcm.h b/adpcm.h
new file mode 100644
index 0000000000..c8b23d2264
--- /dev/null
+++ b/adpcm.h
@@ -0,0 +1,32 @@
+#ifndef ADPCM_H
+#define ADPCM_H
+
+#define IMA_ADPCM_BLOCK_SIZE 0x22
+#define IMA_ADPCM_SAMPLES_PER_BLOCK 0x40
+
+static int adpcm_step[89] =
+{
+ 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
+};
+
+static int adpcm_index[16] =
+{
+ -1, -1, -1, -1, 2, 4, 6, 8,
+ -1, -1, -1, -1, 2, 4, 6, 8
+};
+
+static int format_0x62_table[16] =
+{
+ 1, 3, 5, 7, 9, 11, 13, 15,
+ -1, -3, -5, -7, -9, -11, -13, -15
+};
+
+#endif
diff --git a/codec-cfg.c b/codec-cfg.c
index 101d800cf5..2f9e4cbd44 100644
--- a/codec-cfg.c
+++ b/codec-cfg.c
@@ -215,6 +215,7 @@ static short get_driver(char *s,int audioflag)
"ima4",
"liba52",
"g72x",
+ "adpcm",
NULL
};
static char *videodrv[] = {
diff --git a/codec-cfg.h b/codec-cfg.h
index 5b1eb5fa94..c9ee62e2d7 100644
--- a/codec-cfg.h
+++ b/codec-cfg.h
@@ -33,6 +33,7 @@
#define AFM_IMA4 13
#define AFM_A52 14
#define AFM_G72X 15
+#define AFM_ADPCM 16
#define VFM_MPEG 1
#define VFM_VFW 2
diff --git a/dec_audio.c b/dec_audio.c
index d69086f864..c2f0a8b5d6 100644
--- a/dec_audio.c
+++ b/dec_audio.c
@@ -39,7 +39,7 @@ static G72x_DATA g72x_data;
#include "ac3-iec958.h"
-#include "ima4.h"
+#include "adpcm.h"
#include "cpudetect.h"
@@ -279,10 +279,11 @@ case AFM_GSM:
sh_audio->audio_out_minsize=4*320;
break;
case AFM_IMA4:
+case AFM_ADPCM:
// IMA-ADPCM 4:1 audio codec:
- sh_audio->audio_out_minsize=4096; //4*IMA4_SAMPLES_PER_BLOCK;
- sh_audio->ds->ss_div=IMA4_SAMPLES_PER_BLOCK;
- sh_audio->ds->ss_mul=IMA4_BLOCK_SIZE;
+ sh_audio->audio_out_minsize=4096;
+ sh_audio->ds->ss_div=IMA_ADPCM_SAMPLES_PER_BLOCK;
+ sh_audio->ds->ss_mul=IMA_ADPCM_BLOCK_SIZE;
break;
case AFM_MPEG:
// MPEG Audio:
@@ -502,12 +503,13 @@ case AFM_GSM: {
sh_audio->i_bps=65*(sh_audio->channels*sh_audio->samplerate)/320; // 1:10
break;
}
+case AFM_ADPCM:
case AFM_IMA4: {
// 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
- sh_audio->i_bps=IMA4_BLOCK_SIZE*(sh_audio->channels*sh_audio->samplerate)/IMA4_SAMPLES_PER_BLOCK; // 1:4
+ sh_audio->i_bps=IMA_ADPCM_BLOCK_SIZE*(sh_audio->channels*sh_audio->samplerate)/IMA_ADPCM_SAMPLES_PER_BLOCK; // 1:4
break;
}
case AFM_MPEG: {
@@ -904,10 +906,15 @@ int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen){
memcpy(buf,g72x_data.samples,len);
break;
}
+ case AFM_ADPCM:
case AFM_IMA4: // IMA-ADPCM 4:1 audio codec:
- { unsigned char ibuf[IMA4_BLOCK_SIZE]; // bytes / frame
- if(demux_read_data(sh_audio->ds,ibuf,IMA4_BLOCK_SIZE)!=IMA4_BLOCK_SIZE) break; // EOF
- len=2*ima4_decode_block((unsigned short*)buf,ibuf,2*IMA4_SAMPLES_PER_BLOCK);
+ { unsigned char ibuf[IMA_ADPCM_BLOCK_SIZE * 2]; // bytes / stereo frame
+ if (demux_read_data(sh_audio->ds, ibuf,
+ IMA_ADPCM_BLOCK_SIZE * sh_audio->wf->nChannels) !=
+ IMA_ADPCM_BLOCK_SIZE * sh_audio->wf->nChannels)
+ break; // EOF
+ len=2*ima_adpcm_decode_block((unsigned short*)buf,ibuf, sh_audio->wf->nChannels);
+// len=2*ima4_decode_block((unsigned short*)buf,ibuf,2*IMA_ADPCM_SAMPLES_PER_BLOCK);
break;
}
case AFM_AC3: // AC3 decoder