changeset 3756:734d0c0a8ab0

Initial support for unified ADPCM decoder
author melanson
date Wed, 26 Dec 2001 06:13:17 +0000
parents b805040d6645
children 0abaa09fde3b
files Makefile adpcm.c adpcm.h codec-cfg.c codec-cfg.h dec_audio.c
diffstat 6 files changed, 186 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Wed Dec 26 02:42:44 2001 +0000
+++ b/Makefile	Wed Dec 26 06:13:17 2001 +0000
@@ -22,7 +22,7 @@
 # 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
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/adpcm.c	Wed Dec 26 06:13:17 2001 +0000
@@ -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;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/adpcm.h	Wed Dec 26 06:13:17 2001 +0000
@@ -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
--- a/codec-cfg.c	Wed Dec 26 02:42:44 2001 +0000
+++ b/codec-cfg.c	Wed Dec 26 06:13:17 2001 +0000
@@ -215,6 +215,7 @@
 		"ima4",
 		"liba52",
 		"g72x",
+		"adpcm",
 		NULL
 	};
 	static char *videodrv[] = {
--- a/codec-cfg.h	Wed Dec 26 02:42:44 2001 +0000
+++ b/codec-cfg.h	Wed Dec 26 06:13:17 2001 +0000
@@ -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
--- a/dec_audio.c	Wed Dec 26 02:42:44 2001 +0000
+++ b/dec_audio.c	Wed Dec 26 06:13:17 2001 +0000
@@ -39,7 +39,7 @@
 
 #include "ac3-iec958.h"
 
-#include "ima4.h"
+#include "adpcm.h"
 
 #include "cpudetect.h"
 
@@ -279,10 +279,11 @@
   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 @@
   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 @@
 	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