summaryrefslogtreecommitdiff
path: root/src/G72x
diff options
context:
space:
mode:
authorLorry <lorry@roadtrain.codethink.co.uk>2012-07-18 20:35:05 +0100
committerLorry <lorry@roadtrain.codethink.co.uk>2012-07-18 20:35:05 +0100
commitaceb1f30497619786f1827e4c65992b890a07410 (patch)
treece33ac9d0d076a8ec4f993e614cf71a4d5daa7e7 /src/G72x
downloadlibsndfile-aceb1f30497619786f1827e4c65992b890a07410.tar.gz
Tarball conversion
Diffstat (limited to 'src/G72x')
-rw-r--r--src/G72x/ChangeLog50
-rw-r--r--src/G72x/README0
-rw-r--r--src/G72x/README.original94
-rw-r--r--src/G72x/g721.c155
-rw-r--r--src/G72x/g723_16.c162
-rw-r--r--src/G72x/g723_24.c139
-rw-r--r--src/G72x/g723_40.c153
-rw-r--r--src/G72x/g72x.c644
-rw-r--r--src/G72x/g72x.h91
-rw-r--r--src/G72x/g72x_priv.h109
-rw-r--r--src/G72x/g72x_test.c214
11 files changed, 1811 insertions, 0 deletions
diff --git a/src/G72x/ChangeLog b/src/G72x/ChangeLog
new file mode 100644
index 0000000..aa108df
--- /dev/null
+++ b/src/G72x/ChangeLog
@@ -0,0 +1,50 @@
+2001-06-05 Erik de Castro Lopo <erikd@mega-nerd.com>
+
+ * g72x.c
+ Added {} in function update () to prevent 'ambiguous else' warning messages.
+
+2000-07-14 Erik de Castro Lopo <erikd@mega-nerd.com>
+
+ * g72x.c
+ Modified g72x_init_state () to fit in with the new structure of the code.
+ Implemented g72x_encode_block () and g72x_decode_block ().
+
+2000-07-12 Erik de Castro Lopo <erikd@mega-nerd.com>
+
+ * g72x.h
+ Moved nearly all definitions and function prototypes from this file have been
+ moved to private.h.
+ Added an enum defining the 4 different G72x ADPCM codecs.
+ Added new function prototypes to define a cleaner interface to the encoder
+ and decoder. This new interface also allows samples to be processed in blocks
+ rather than on a sample by sample basis like the original code.
+
+ * private.h
+ Added prototypes moved from g72x.h.
+ Changed struct g72x_state to a typedef struct { .. } G72x_PRIVATE.
+ Added fields to G72x_PRIVATE required for working on blocks of samples.
+
+2000-06-07 Erik de Castro Lopo <erikd@mega-nerd.com>
+
+ * g72x.c
+ Fixed all compiler warnings.
+ Removed functions tandem_adjust() which is not required by libsndfile.
+
+ * g721.c
+ Fixed all compiler warnings.
+ Removed functions tandem_adjust_alaw() and tandem_adjust_ulaw () which are not
+ required by libsndfile.
+ Removed second parameter to g721_encoder () which is not required.
+
+ * g72x.h
+ Removed in_coding and out_coding parameters from all functions. These allowed
+ g72x encoding/decoding to/from A-law or u-law and are not required by libsndfile.
+ Removed unneeded defines for A-law, u-law and linear encoding.
+
+ * g723_16.c
+ Removed second parameter (in_coding) for g723_16_encoder().
+ Removed second parameter (out_coding) for g723_16_decoder().
+
+ * private.h
+ New file containing prototypes and tyepdefs private to G72x code.
+
diff --git a/src/G72x/README b/src/G72x/README
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/G72x/README
diff --git a/src/G72x/README.original b/src/G72x/README.original
new file mode 100644
index 0000000..23b0e7d
--- /dev/null
+++ b/src/G72x/README.original
@@ -0,0 +1,94 @@
+The files in this directory comprise ANSI-C language reference implementations
+of the CCITT (International Telegraph and Telephone Consultative Committee)
+G.711, G.721 and G.723 voice compressions. They have been tested on Sun
+SPARCstations and passed 82 out of 84 test vectors published by CCITT
+(Dec. 20, 1988) for G.721 and G.723. [The two remaining test vectors,
+which the G.721 decoder implementation for u-law samples did not pass,
+may be in error because they are identical to two other vectors for G.723_40.]
+
+This source code is released by Sun Microsystems, Inc. to the public domain.
+Please give your acknowledgement in product literature if this code is used
+in your product implementation.
+
+Sun Microsystems supports some CCITT audio formats in Solaris 2.0 system
+software. However, Sun's implementations have been optimized for higher
+performance on SPARCstations.
+
+
+The source files for CCITT conversion routines in this directory are:
+
+ g72x.h header file for g721.c, g723_24.c and g723_40.c
+ g711.c CCITT G.711 u-law and A-law compression
+ g72x.c common denominator of G.721 and G.723 ADPCM codes
+ g721.c CCITT G.721 32Kbps ADPCM coder (with g72x.c)
+ g723_24.c CCITT G.723 24Kbps ADPCM coder (with g72x.c)
+ g723_40.c CCITT G.723 40Kbps ADPCM coder (with g72x.c)
+
+
+Simple conversions between u-law, A-law, and 16-bit linear PCM are invoked
+as follows:
+
+ unsigned char ucode, acode;
+ short pcm_val;
+
+ ucode = linear2ulaw(pcm_val);
+ ucode = alaw2ulaw(acode);
+
+ acode = linear2alaw(pcm_val);
+ acode = ulaw2alaw(ucode);
+
+ pcm_val = ulaw2linear(ucode);
+ pcm_val = alaw2linear(acode);
+
+
+The other CCITT compression routines are invoked as follows:
+
+ #include "g72x.h"
+
+ struct g72x_state state;
+ int sample, code;
+
+ g72x_init_state(&state);
+ code = {g721,g723_24,g723_40}_encoder(sample, coding, &state);
+ sample = {g721,g723_24,g723_40}_decoder(code, coding, &state);
+
+where
+ coding = AUDIO_ENCODING_ULAW for 8-bit u-law samples
+ AUDIO_ENCODING_ALAW for 8-bit A-law samples
+ AUDIO_ENCODING_LINEAR for 16-bit linear PCM samples
+
+
+
+This directory also includes the following sample programs:
+
+ encode.c CCITT ADPCM encoder
+ decode.c CCITT ADPCM decoder
+ Makefile makefile for the sample programs
+
+
+The sample programs contain examples of how to call the various compression
+routines and pack/unpack the bits. The sample programs read byte streams from
+stdin and write to stdout. The input/output data is raw data (no file header
+or other identifying information is embedded). The sample programs are
+invoked as follows:
+
+ encode [-3|4|5] [-a|u|l] <infile >outfile
+ decode [-3|4|5] [-a|u|l] <infile >outfile
+where:
+ -3 encode to (decode from) G.723 24kbps (3-bit) data
+ -4 encode to (decode from) G.721 32kbps (4-bit) data [the default]
+ -5 encode to (decode from) G.723 40kbps (5-bit) data
+ -a encode from (decode to) A-law data
+ -u encode from (decode to) u-law data [the default]
+ -l encode from (decode to) 16-bit linear data
+
+Examples:
+ # Read 16-bit linear and output G.721
+ encode -4 -l <pcmfile >g721file
+
+ # Read 40Kbps G.723 and output A-law
+ decode -5 -a <g723file >alawfile
+
+ # Compress and then decompress u-law data using 24Kbps G.723
+ encode -3 <ulawin | deoced -3 >ulawout
+
diff --git a/src/G72x/g721.c b/src/G72x/g721.c
new file mode 100644
index 0000000..d305ba8
--- /dev/null
+++ b/src/G72x/g721.c
@@ -0,0 +1,155 @@
+/*
+ * This source code is a product of Sun Microsystems, Inc. and is provided
+ * for unrestricted use. Users may copy or modify this source code without
+ * charge.
+ *
+ * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
+ * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun source code is provided with no support and without any obligation on
+ * the part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+
+/*
+ * g721.c
+ *
+ * Description:
+ *
+ * g721_encoder(), g721_decoder()
+ *
+ * These routines comprise an implementation of the CCITT G.721 ADPCM
+ * coding algorithm. Essentially, this implementation is identical to
+ * the bit level description except for a few deviations which
+ * take advantage of work station attributes, such as hardware 2's
+ * complement arithmetic and large memory. Specifically, certain time
+ * consuming operations such as multiplications are replaced
+ * with lookup tables and software 2's complement operations are
+ * replaced with hardware 2's complement.
+ *
+ * The deviation from the bit level specification (lookup tables)
+ * preserves the bit level performance specifications.
+ *
+ * As outlined in the G.721 Recommendation, the algorithm is broken
+ * down into modules. Each section of code below is preceded by
+ * the name of the module which it is implementing.
+ *
+ */
+
+#include "g72x.h"
+#include "g72x_priv.h"
+
+static short qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
+/*
+ * Maps G.721 code word to reconstructed scale factor normalized log
+ * magnitude values.
+ */
+static short _dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
+ 425, 373, 323, 273, 213, 135, 4, -2048};
+
+/* Maps G.721 code word to log of scale factor multiplier. */
+static short _witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
+ 1122, 355, 198, 112, 64, 41, 18, -12};
+/*
+ * Maps G.721 code words to a set of values whose long and short
+ * term averages are computed and then compared to give an indication
+ * how stationary (steady state) the signal is.
+ */
+static short _fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
+ 0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
+
+/*
+ * g721_encoder()
+ *
+ * Encodes the input vale of linear PCM, A-law or u-law data sl and returns
+ * the resulting code. -1 is returned for unknown input coding value.
+ */
+int
+g721_encoder(
+ int sl,
+ G72x_STATE *state_ptr)
+{
+ short sezi, se, sez; /* ACCUM */
+ short d; /* SUBTA */
+ short sr; /* ADDB */
+ short y; /* MIX */
+ short dqsez; /* ADDC */
+ short dq, i;
+
+ /* linearize input sample to 14-bit PCM */
+ sl >>= 2; /* 14-bit dynamic range */
+
+ sezi = predictor_zero(state_ptr);
+ sez = sezi >> 1;
+ se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
+
+ d = sl - se; /* estimation difference */
+
+ /* quantize the prediction difference */
+ y = step_size(state_ptr); /* quantizer step size */
+ i = quantize(d, y, qtab_721, 7); /* i = ADPCM code */
+
+ dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized est diff */
+
+ sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
+
+ dqsez = sr + sez - se; /* pole prediction diff. */
+
+ update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
+
+ return (i);
+}
+
+/*
+ * g721_decoder()
+ *
+ * Description:
+ *
+ * Decodes a 4-bit code of G.721 encoded data of i and
+ * returns the resulting linear PCM, A-law or u-law value.
+ * return -1 for unknown out_coding value.
+ */
+int
+g721_decoder(
+ int i,
+ G72x_STATE *state_ptr)
+{
+ short sezi, sei, sez, se; /* ACCUM */
+ short y; /* MIX */
+ short sr; /* ADDB */
+ short dq;
+ short dqsez;
+
+ i &= 0x0f; /* mask to get proper bits */
+ sezi = predictor_zero(state_ptr);
+ sez = sezi >> 1;
+ sei = sezi + predictor_pole(state_ptr);
+ se = sei >> 1; /* se = estimated signal */
+
+ y = step_size(state_ptr); /* dynamic quantizer step size */
+
+ dq = reconstruct(i & 0x08, _dqlntab[i], y); /* quantized diff. */
+
+ sr = (dq < 0) ? (se - (dq & 0x3FFF)) : se + dq; /* reconst. signal */
+
+ dqsez = sr - se + sez; /* pole prediction diff. */
+
+ update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
+
+ /* sr was 14-bit dynamic range */
+ return (sr << 2);
+}
+
diff --git a/src/G72x/g723_16.c b/src/G72x/g723_16.c
new file mode 100644
index 0000000..ae90b6c
--- /dev/null
+++ b/src/G72x/g723_16.c
@@ -0,0 +1,162 @@
+/*
+ * This source code is a product of Sun Microsystems, Inc. and is provided
+ * for unrestricted use. Users may copy or modify this source code without
+ * charge.
+ *
+ * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
+ * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun source code is provided with no support and without any obligation on
+ * the part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+/* 16kbps version created, used 24kbps code and changing as little as possible.
+ * G.726 specs are available from ITU's gopher or WWW site (http://www.itu.ch)
+ * If any errors are found, please contact me at mrand@tamu.edu
+ * -Marc Randolph
+ */
+
+/*
+ * g723_16.c
+ *
+ * Description:
+ *
+ * g723_16_encoder(), g723_16_decoder()
+ *
+ * These routines comprise an implementation of the CCITT G.726 16 Kbps
+ * ADPCM coding algorithm. Essentially, this implementation is identical to
+ * the bit level description except for a few deviations which take advantage
+ * of workstation attributes, such as hardware 2's complement arithmetic.
+ *
+ */
+
+#include "g72x.h"
+#include "g72x_priv.h"
+
+/*
+ * Maps G.723_16 code word to reconstructed scale factor normalized log
+ * magnitude values. Comes from Table 11/G.726
+ */
+static short _dqlntab[4] = { 116, 365, 365, 116};
+
+/* Maps G.723_16 code word to log of scale factor multiplier.
+ *
+ * _witab[4] is actually {-22 , 439, 439, -22}, but FILTD wants it
+ * as WI << 5 (multiplied by 32), so we'll do that here
+ */
+static short _witab[4] = {-704, 14048, 14048, -704};
+
+/*
+ * Maps G.723_16 code words to a set of values whose long and short
+ * term averages are computed and then compared to give an indication
+ * how stationary (steady state) the signal is.
+ */
+
+/* Comes from FUNCTF */
+static short _fitab[4] = {0, 0xE00, 0xE00, 0};
+
+/* Comes from quantizer decision level tables (Table 7/G.726)
+ */
+static short qtab_723_16[1] = {261};
+
+
+/*
+ * g723_16_encoder()
+ *
+ * Encodes a linear PCM, A-law or u-law input sample and returns its 2-bit code.
+ * Returns -1 if invalid input coding value.
+ */
+int
+g723_16_encoder(
+ int sl,
+ G72x_STATE *state_ptr)
+{
+ short sei, sezi, se, sez; /* ACCUM */
+ short d; /* SUBTA */
+ short y; /* MIX */
+ short sr; /* ADDB */
+ short dqsez; /* ADDC */
+ short dq, i;
+
+ /* linearize input sample to 14-bit PCM */
+ sl >>= 2; /* sl of 14-bit dynamic range */
+
+ sezi = predictor_zero(state_ptr);
+ sez = sezi >> 1;
+ sei = sezi + predictor_pole(state_ptr);
+ se = sei >> 1; /* se = estimated signal */
+
+ d = sl - se; /* d = estimation diff. */
+
+ /* quantize prediction difference d */
+ y = step_size(state_ptr); /* quantizer step size */
+ i = quantize(d, y, qtab_723_16, 1); /* i = ADPCM code */
+
+ /* Since quantize() only produces a three level output
+ * (1, 2, or 3), we must create the fourth one on our own
+ */
+ if (i == 3) /* i code for the zero region */
+ if ((d & 0x8000) == 0) /* If d > 0, i=3 isn't right... */
+ i = 0;
+
+ dq = reconstruct(i & 2, _dqlntab[i], y); /* quantized diff. */
+
+ sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconstructed signal */
+
+ dqsez = sr + sez - se; /* pole prediction diff. */
+
+ update(2, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
+
+ return (i);
+}
+
+/*
+ * g723_16_decoder()
+ *
+ * Decodes a 2-bit CCITT G.723_16 ADPCM code and returns
+ * the resulting 16-bit linear PCM, A-law or u-law sample value.
+ * -1 is returned if the output coding is unknown.
+ */
+int
+g723_16_decoder(
+ int i,
+ G72x_STATE *state_ptr)
+{
+ short sezi, sei, sez, se; /* ACCUM */
+ short y; /* MIX */
+ short sr; /* ADDB */
+ short dq;
+ short dqsez;
+
+ i &= 0x03; /* mask to get proper bits */
+ sezi = predictor_zero(state_ptr);
+ sez = sezi >> 1;
+ sei = sezi + predictor_pole(state_ptr);
+ se = sei >> 1; /* se = estimated signal */
+
+ y = step_size(state_ptr); /* adaptive quantizer step size */
+ dq = reconstruct(i & 0x02, _dqlntab[i], y); /* unquantize pred diff */
+
+ sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq); /* reconst. signal */
+
+ dqsez = sr - se + sez; /* pole prediction diff. */
+
+ update(2, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
+
+ /* sr was of 14-bit dynamic range */
+ return (sr << 2);
+}
+
diff --git a/src/G72x/g723_24.c b/src/G72x/g723_24.c
new file mode 100644
index 0000000..02b6c24
--- /dev/null
+++ b/src/G72x/g723_24.c
@@ -0,0 +1,139 @@
+/*
+ * This source code is a product of Sun Microsystems, Inc. and is provided
+ * for unrestricted use. Users may copy or modify this source code without
+ * charge.
+ *
+ * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
+ * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun source code is provided with no support and without any obligation on
+ * the part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+
+/*
+ * g723_24.c
+ *
+ * Description:
+ *
+ * g723_24_encoder(), g723_24_decoder()
+ *
+ * These routines comprise an implementation of the CCITT G.723 24 Kbps
+ * ADPCM coding algorithm. Essentially, this implementation is identical to
+ * the bit level description except for a few deviations which take advantage
+ * of workstation attributes, such as hardware 2's complement arithmetic.
+ *
+ */
+
+#include "g72x.h"
+#include "g72x_priv.h"
+
+/*
+ * Maps G.723_24 code word to reconstructed scale factor normalized log
+ * magnitude values.
+ */
+static short _dqlntab[8] = {-2048, 135, 273, 373, 373, 273, 135, -2048};
+
+/* Maps G.723_24 code word to log of scale factor multiplier. */
+static short _witab[8] = {-128, 960, 4384, 18624, 18624, 4384, 960, -128};
+
+/*
+ * Maps G.723_24 code words to a set of values whose long and short
+ * term averages are computed and then compared to give an indication
+ * how stationary (steady state) the signal is.
+ */
+static short _fitab[8] = {0, 0x200, 0x400, 0xE00, 0xE00, 0x400, 0x200, 0};
+
+static short qtab_723_24[3] = {8, 218, 331};
+
+/*
+ * g723_24_encoder()
+ *
+ * Encodes a linear PCM, A-law or u-law input sample and returns its 3-bit code.
+ * Returns -1 if invalid input coding value.
+ */
+int
+g723_24_encoder(
+ int sl,
+ G72x_STATE *state_ptr)
+{
+ short sei, sezi, se, sez; /* ACCUM */
+ short d; /* SUBTA */
+ short y; /* MIX */
+ short sr; /* ADDB */
+ short dqsez; /* ADDC */
+ short dq, i;
+
+ /* linearize input sample to 14-bit PCM */
+ sl >>= 2; /* sl of 14-bit dynamic range */
+
+ sezi = predictor_zero(state_ptr);
+ sez = sezi >> 1;
+ sei = sezi + predictor_pole(state_ptr);
+ se = sei >> 1; /* se = estimated signal */
+
+ d = sl - se; /* d = estimation diff. */
+
+ /* quantize prediction difference d */
+ y = step_size(state_ptr); /* quantizer step size */
+ i = quantize(d, y, qtab_723_24, 3); /* i = ADPCM code */
+ dq = reconstruct(i & 4, _dqlntab[i], y); /* quantized diff. */
+
+ sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconstructed signal */
+
+ dqsez = sr + sez - se; /* pole prediction diff. */
+
+ update(3, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
+
+ return (i);
+}
+
+/*
+ * g723_24_decoder()
+ *
+ * Decodes a 3-bit CCITT G.723_24 ADPCM code and returns
+ * the resulting 16-bit linear PCM, A-law or u-law sample value.
+ * -1 is returned if the output coding is unknown.
+ */
+int
+g723_24_decoder(
+ int i,
+ G72x_STATE *state_ptr)
+{
+ short sezi, sei, sez, se; /* ACCUM */
+ short y; /* MIX */
+ short sr; /* ADDB */
+ short dq;
+ short dqsez;
+
+ i &= 0x07; /* mask to get proper bits */
+ sezi = predictor_zero(state_ptr);
+ sez = sezi >> 1;
+ sei = sezi + predictor_pole(state_ptr);
+ se = sei >> 1; /* se = estimated signal */
+
+ y = step_size(state_ptr); /* adaptive quantizer step size */
+ dq = reconstruct(i & 0x04, _dqlntab[i], y); /* unquantize pred diff */
+
+ sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq); /* reconst. signal */
+
+ dqsez = sr - se + sez; /* pole prediction diff. */
+
+ update(3, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
+
+ return (sr << 2); /* sr was of 14-bit dynamic range */
+}
+
diff --git a/src/G72x/g723_40.c b/src/G72x/g723_40.c
new file mode 100644
index 0000000..d520395
--- /dev/null
+++ b/src/G72x/g723_40.c
@@ -0,0 +1,153 @@
+/*
+ * This source code is a product of Sun Microsystems, Inc. and is provided
+ * for unrestricted use. Users may copy or modify this source code without
+ * charge.
+ *
+ * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
+ * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun source code is provided with no support and without any obligation on
+ * the part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+
+/*
+ * g723_40.c
+ *
+ * Description:
+ *
+ * g723_40_encoder(), g723_40_decoder()
+ *
+ * These routines comprise an implementation of the CCITT G.723 40Kbps
+ * ADPCM coding algorithm. Essentially, this implementation is identical to
+ * the bit level description except for a few deviations which
+ * take advantage of workstation attributes, such as hardware 2's
+ * complement arithmetic.
+ *
+ * The deviation from the bit level specification (lookup tables),
+ * preserves the bit level performance specifications.
+ *
+ * As outlined in the G.723 Recommendation, the algorithm is broken
+ * down into modules. Each section of code below is preceded by
+ * the name of the module which it is implementing.
+ *
+ */
+
+#include "g72x.h"
+#include "g72x_priv.h"
+
+/*
+ * Maps G.723_40 code word to ructeconstructed scale factor normalized log
+ * magnitude values.
+ */
+static short _dqlntab[32] = {-2048, -66, 28, 104, 169, 224, 274, 318,
+ 358, 395, 429, 459, 488, 514, 539, 566,
+ 566, 539, 514, 488, 459, 429, 395, 358,
+ 318, 274, 224, 169, 104, 28, -66, -2048};
+
+/* Maps G.723_40 code word to log of scale factor multiplier. */
+static short _witab[32] = {448, 448, 768, 1248, 1280, 1312, 1856, 3200,
+ 4512, 5728, 7008, 8960, 11456, 14080, 16928, 22272,
+ 22272, 16928, 14080, 11456, 8960, 7008, 5728, 4512,
+ 3200, 1856, 1312, 1280, 1248, 768, 448, 448};
+
+/*
+ * Maps G.723_40 code words to a set of values whose long and short
+ * term averages are computed and then compared to give an indication
+ * how stationary (steady state) the signal is.
+ */
+static short _fitab[32] = {0, 0, 0, 0, 0, 0x200, 0x200, 0x200,
+ 0x200, 0x200, 0x400, 0x600, 0x800, 0xA00, 0xC00, 0xC00,
+ 0xC00, 0xC00, 0xA00, 0x800, 0x600, 0x400, 0x200, 0x200,
+ 0x200, 0x200, 0x200, 0, 0, 0, 0, 0};
+
+static short qtab_723_40[15] = {-122, -16, 68, 139, 198, 250, 298, 339,
+ 378, 413, 445, 475, 502, 528, 553};
+
+/*
+ * g723_40_encoder()
+ *
+ * Encodes a 16-bit linear PCM, A-law or u-law input sample and retuens
+ * the resulting 5-bit CCITT G.723 40Kbps code.
+ * Returns -1 if the input coding value is invalid.
+ */
+int g723_40_encoder (int sl, G72x_STATE *state_ptr)
+{
+ short sei, sezi, se, sez; /* ACCUM */
+ short d; /* SUBTA */
+ short y; /* MIX */
+ short sr; /* ADDB */
+ short dqsez; /* ADDC */
+ short dq, i;
+
+ /* linearize input sample to 14-bit PCM */
+ sl >>= 2; /* sl of 14-bit dynamic range */
+
+ sezi = predictor_zero(state_ptr);
+ sez = sezi >> 1;
+ sei = sezi + predictor_pole(state_ptr);
+ se = sei >> 1; /* se = estimated signal */
+
+ d = sl - se; /* d = estimation difference */
+
+ /* quantize prediction difference */
+ y = step_size(state_ptr); /* adaptive quantizer step size */
+ i = quantize(d, y, qtab_723_40, 15); /* i = ADPCM code */
+
+ dq = reconstruct(i & 0x10, _dqlntab[i], y); /* quantized diff */
+
+ sr = (dq < 0) ? se - (dq & 0x7FFF) : se + dq; /* reconstructed signal */
+
+ dqsez = sr + sez - se; /* dqsez = pole prediction diff. */
+
+ update(5, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
+
+ return (i);
+}
+
+/*
+ * g723_40_decoder()
+ *
+ * Decodes a 5-bit CCITT G.723 40Kbps code and returns
+ * the resulting 16-bit linear PCM, A-law or u-law sample value.
+ * -1 is returned if the output coding is unknown.
+ */
+int g723_40_decoder (int i, G72x_STATE *state_ptr)
+{
+ short sezi, sei, sez, se; /* ACCUM */
+ short y ; /* MIX */
+ short sr; /* ADDB */
+ short dq;
+ short dqsez;
+
+ i &= 0x1f; /* mask to get proper bits */
+ sezi = predictor_zero(state_ptr);
+ sez = sezi >> 1;
+ sei = sezi + predictor_pole(state_ptr);
+ se = sei >> 1; /* se = estimated signal */
+
+ y = step_size(state_ptr); /* adaptive quantizer step size */
+ dq = reconstruct(i & 0x10, _dqlntab[i], y); /* estimation diff. */
+
+ sr = (dq < 0) ? (se - (dq & 0x7FFF)) : (se + dq); /* reconst. signal */
+
+ dqsez = sr - se + sez; /* pole prediction diff. */
+
+ update(5, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
+
+ return (sr << 2); /* sr was of 14-bit dynamic range */
+}
+
diff --git a/src/G72x/g72x.c b/src/G72x/g72x.c
new file mode 100644
index 0000000..3fae81a
--- /dev/null
+++ b/src/G72x/g72x.c
@@ -0,0 +1,644 @@
+/*
+ * This source code is a product of Sun Microsystems, Inc. and is provided
+ * for unrestricted use. Users may copy or modify this source code without
+ * charge.
+ *
+ * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
+ * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun source code is provided with no support and without any obligation on
+ * the part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+
+/*
+ * g72x.c
+ *
+ * Common routines for G.721 and G.723 conversions.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "g72x.h"
+#include "g72x_priv.h"
+
+static G72x_STATE * g72x_state_new (void) ;
+static int unpack_bytes (int bits, int blocksize, const unsigned char * block, short * samples) ;
+static int pack_bytes (int bits, const short * samples, unsigned char * block) ;
+
+static
+short power2 [15] =
+{ 1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80,
+ 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000
+} ;
+
+/*
+ * quan()
+ *
+ * quantizes the input val against the table of size short integers.
+ * It returns i if table[i - 1] <= val < table[i].
+ *
+ * Using linear search for simple coding.
+ */
+static
+int quan (int val, short *table, int size)
+{
+ int i;
+
+ for (i = 0; i < size; i++)
+ if (val < *table++)
+ break;
+ return (i);
+}
+
+/*
+ * fmult()
+ *
+ * returns the integer product of the 14-bit integer "an" and
+ * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
+ */
+static
+int fmult (int an, int srn)
+{
+ short anmag, anexp, anmant;
+ short wanexp, wanmant;
+ short retval;
+
+ anmag = (an > 0) ? an : ((-an) & 0x1FFF);
+ anexp = quan(anmag, power2, 15) - 6;
+ anmant = (anmag == 0) ? 32 :
+ (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
+ wanexp = anexp + ((srn >> 6) & 0xF) - 13;
+
+ /*
+ ** The original was :
+ ** wanmant = (anmant * (srn & 0x3F) + 0x30) >> 4 ;
+ ** but could see no valid reason for the + 0x30.
+ ** Removed it and it improved the SNR of the codec.
+ */
+
+ wanmant = (anmant * (srn & 0x3F)) >> 4 ;
+
+ retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) :
+ (wanmant >> -wanexp);
+
+ return (((an ^ srn) < 0) ? -retval : retval);
+}
+
+static G72x_STATE * g72x_state_new (void)
+{ return calloc (1, sizeof (G72x_STATE)) ;
+}
+
+/*
+ * private_init_state()
+ *
+ * This routine initializes and/or resets the G72x_PRIVATE structure
+ * pointed to by 'state_ptr'.
+ * All the initial state values are specified in the CCITT G.721 document.
+ */
+void private_init_state (G72x_STATE *state_ptr)
+{
+ int cnta;
+
+ state_ptr->yl = 34816;
+ state_ptr->yu = 544;
+ state_ptr->dms = 0;
+ state_ptr->dml = 0;
+ state_ptr->ap = 0;
+ for (cnta = 0; cnta < 2; cnta++) {
+ state_ptr->a[cnta] = 0;
+ state_ptr->pk[cnta] = 0;
+ state_ptr->sr[cnta] = 32;
+ }
+ for (cnta = 0; cnta < 6; cnta++) {
+ state_ptr->b[cnta] = 0;
+ state_ptr->dq[cnta] = 32;
+ }
+ state_ptr->td = 0;
+} /* private_init_state */
+
+struct g72x_state * g72x_reader_init (int codec, int *blocksize, int *samplesperblock)
+{ G72x_STATE *pstate ;
+
+ if ((pstate = g72x_state_new ()) == NULL)
+ return NULL ;
+
+ private_init_state (pstate) ;
+
+ pstate->encoder = NULL ;
+
+ switch (codec)
+ { case G723_16_BITS_PER_SAMPLE : /* 2 bits per sample. */
+ pstate->decoder = g723_16_decoder ;
+ *blocksize = G723_16_BYTES_PER_BLOCK ;
+ *samplesperblock = G723_16_SAMPLES_PER_BLOCK ;
+ pstate->codec_bits = 2 ;
+ pstate->blocksize = G723_16_BYTES_PER_BLOCK ;
+ pstate->samplesperblock = G723_16_SAMPLES_PER_BLOCK ;
+ break ;
+
+ case G723_24_BITS_PER_SAMPLE : /* 3 bits per sample. */
+ pstate->decoder = g723_24_decoder ;
+ *blocksize = G723_24_BYTES_PER_BLOCK ;
+ *samplesperblock = G723_24_SAMPLES_PER_BLOCK ;
+ pstate->codec_bits = 3 ;
+ pstate->blocksize = G723_24_BYTES_PER_BLOCK ;
+ pstate->samplesperblock = G723_24_SAMPLES_PER_BLOCK ;
+ break ;
+
+ case G721_32_BITS_PER_SAMPLE : /* 4 bits per sample. */
+ pstate->decoder = g721_decoder ;
+ *blocksize = G721_32_BYTES_PER_BLOCK ;
+ *samplesperblock = G721_32_SAMPLES_PER_BLOCK ;
+ pstate->codec_bits = 4 ;
+ pstate->blocksize = G721_32_BYTES_PER_BLOCK ;
+ pstate->samplesperblock = G721_32_SAMPLES_PER_BLOCK ;
+ break ;
+
+ case G721_40_BITS_PER_SAMPLE : /* 5 bits per sample. */
+ pstate->decoder = g723_40_decoder ;
+ *blocksize = G721_40_BYTES_PER_BLOCK ;
+ *samplesperblock = G721_40_SAMPLES_PER_BLOCK ;
+ pstate->codec_bits = 5 ;
+ pstate->blocksize = G721_40_BYTES_PER_BLOCK ;
+ pstate->samplesperblock = G721_40_SAMPLES_PER_BLOCK ;
+ break ;
+
+ default :
+ free (pstate) ;
+ return NULL ;
+ } ;
+
+ return pstate ;
+} /* g72x_reader_init */
+
+struct g72x_state * g72x_writer_init (int codec, int *blocksize, int *samplesperblock)
+{ G72x_STATE *pstate ;
+
+ if ((pstate = g72x_state_new ()) == NULL)
+ return NULL ;
+
+ private_init_state (pstate) ;
+ pstate->decoder = NULL ;
+
+ switch (codec)
+ { case G723_16_BITS_PER_SAMPLE : /* 2 bits per sample. */
+ pstate->encoder = g723_16_encoder ;
+ *blocksize = G723_16_BYTES_PER_BLOCK ;
+ *samplesperblock = G723_16_SAMPLES_PER_BLOCK ;
+ pstate->codec_bits = 2 ;
+ pstate->blocksize = G723_16_BYTES_PER_BLOCK ;
+ pstate->samplesperblock = G723_16_SAMPLES_PER_BLOCK ;
+ break ;
+
+ case G723_24_BITS_PER_SAMPLE : /* 3 bits per sample. */
+ pstate->encoder = g723_24_encoder ;
+ *blocksize = G723_24_BYTES_PER_BLOCK ;
+ *samplesperblock = G723_24_SAMPLES_PER_BLOCK ;
+ pstate->codec_bits = 3 ;
+ pstate->blocksize = G723_24_BYTES_PER_BLOCK ;
+ pstate->samplesperblock = G723_24_SAMPLES_PER_BLOCK ;
+ break ;
+
+ case G721_32_BITS_PER_SAMPLE : /* 4 bits per sample. */
+ pstate->encoder = g721_encoder ;
+ *blocksize = G721_32_BYTES_PER_BLOCK ;
+ *samplesperblock = G721_32_SAMPLES_PER_BLOCK ;
+ pstate->codec_bits = 4 ;
+ pstate->blocksize = G721_32_BYTES_PER_BLOCK ;
+ pstate->samplesperblock = G721_32_SAMPLES_PER_BLOCK ;
+ break ;
+
+ case G721_40_BITS_PER_SAMPLE : /* 5 bits per sample. */
+ pstate->encoder = g723_40_encoder ;
+ *blocksize = G721_40_BYTES_PER_BLOCK ;
+ *samplesperblock = G721_40_SAMPLES_PER_BLOCK ;
+ pstate->codec_bits = 5 ;
+ pstate->blocksize = G721_40_BYTES_PER_BLOCK ;
+ pstate->samplesperblock = G721_40_SAMPLES_PER_BLOCK ;
+ break ;
+
+ default :
+ free (pstate) ;
+ return NULL ;
+ } ;
+
+ return pstate ;
+} /* g72x_writer_init */
+
+int g72x_decode_block (G72x_STATE *pstate, const unsigned char *block, short *samples)
+{ int k, count ;
+
+ count = unpack_bytes (pstate->codec_bits, pstate->blocksize, block, samples) ;
+
+ for (k = 0 ; k < count ; k++)
+ samples [k] = pstate->decoder (samples [k], pstate) ;
+
+ return 0 ;
+} /* g72x_decode_block */
+
+int g72x_encode_block (G72x_STATE *pstate, short *samples, unsigned char *block)
+{ int k, count ;
+
+ for (k = 0 ; k < pstate->samplesperblock ; k++)
+ samples [k] = pstate->encoder (samples [k], pstate) ;
+
+ count = pack_bytes (pstate->codec_bits, samples, block) ;
+
+ return count ;
+} /* g72x_encode_block */
+
+/*
+ * predictor_zero()
+ *
+ * computes the estimated signal from 6-zero predictor.
+ *
+ */
+int predictor_zero (G72x_STATE *state_ptr)
+{
+ int i;
+ int sezi;
+
+ sezi = fmult(state_ptr->b[0] >> 2, state_ptr->dq[0]);
+ for (i = 1; i < 6; i++) /* ACCUM */
+ sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
+ return (sezi);
+}
+/*
+ * predictor_pole()
+ *
+ * computes the estimated signal from 2-pole predictor.
+ *
+ */
+int predictor_pole(G72x_STATE *state_ptr)
+{
+ return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +
+ fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
+}
+/*
+ * step_size()
+ *
+ * computes the quantization step size of the adaptive quantizer.
+ *
+ */
+int step_size (G72x_STATE *state_ptr)
+{
+ int y;
+ int dif;
+ int al;
+
+ if (state_ptr->ap >= 256)
+ return (state_ptr->yu);
+ else {
+ y = state_ptr->yl >> 6;
+ dif = state_ptr->yu - y;
+ al = state_ptr->ap >> 2;
+ if (dif > 0)
+ y += (dif * al) >> 6;
+ else if (dif < 0)
+ y += (dif * al + 0x3F) >> 6;
+ return (y);
+ }
+}
+
+/*
+ * quantize()
+ *
+ * Given a raw sample, 'd', of the difference signal and a
+ * quantization step size scale factor, 'y', this routine returns the
+ * ADPCM codeword to which that sample gets quantized. The step
+ * size scale factor division operation is done in the log base 2 domain
+ * as a subtraction.
+ */
+int quantize(
+ int d, /* Raw difference signal sample */
+ int y, /* Step size multiplier */
+ short *table, /* quantization table */
+ int size) /* table size of short integers */
+{
+ short dqm; /* Magnitude of 'd' */
+ short expon; /* Integer part of base 2 log of 'd' */
+ short mant; /* Fractional part of base 2 log */
+ short dl; /* Log of magnitude of 'd' */
+ short dln; /* Step size scale factor normalized log */
+ int i;
+
+ /*
+ * LOG
+ *
+ * Compute base 2 log of 'd', and store in 'dl'.
+ */
+ dqm = abs(d);
+ expon = quan(dqm >> 1, power2, 15);
+ mant = ((dqm << 7) >> expon) & 0x7F; /* Fractional portion. */
+ dl = (expon << 7) + mant;
+
+ /*
+ * SUBTB
+ *
+ * "Divide" by step size multiplier.
+ */
+ dln = dl - (y >> 2);
+
+ /*
+ * QUAN
+ *
+ * Obtain codword i for 'd'.
+ */
+ i = quan(dln, table, size);
+ if (d < 0) /* take 1's complement of i */
+ return ((size << 1) + 1 - i);
+ else if (i == 0) /* take 1's complement of 0 */
+ return ((size << 1) + 1); /* new in 1988 */
+ else
+ return (i);
+}
+/*
+ * reconstruct()
+ *
+ * Returns reconstructed difference signal 'dq' obtained from
+ * codeword 'i' and quantization step size scale factor 'y'.
+ * Multiplication is performed in log base 2 domain as addition.
+ */
+int
+reconstruct(
+ int sign, /* 0 for non-negative value */
+ int dqln, /* G.72x codeword */
+ int y) /* Step size multiplier */
+{
+ short dql; /* Log of 'dq' magnitude */
+ short dex; /* Integer part of log */
+ short dqt;
+ short dq; /* Reconstructed difference signal sample */
+
+ dql = dqln + (y >> 2); /* ADDA */
+
+ if (dql < 0) {
+ return ((sign) ? -0x8000 : 0);
+ } else { /* ANTILOG */
+ dex = (dql >> 7) & 15;
+ dqt = 128 + (dql & 127);
+ dq = (dqt << 7) >> (14 - dex);
+ return ((sign) ? (dq - 0x8000) : dq);
+ }
+}
+
+
+/*
+ * update()
+ *
+ * updates the state variables for each output code
+ */
+void
+update(
+ int code_size, /* distinguish 723_40 with others */
+ int y, /* quantizer step size */
+ int wi, /* scale factor multiplier */
+ int fi, /* for long/short term energies */
+ int dq, /* quantized prediction difference */
+ int sr, /* reconstructed signal */
+ int dqsez, /* difference from 2-pole predictor */
+ G72x_STATE *state_ptr) /* coder state pointer */
+{
+ int cnt;
+ short mag, expon; /* Adaptive predictor, FLOAT A */
+ short a2p = 0; /* LIMC */
+ short a1ul; /* UPA1 */
+ short pks1; /* UPA2 */
+ short fa1;
+ char tr; /* tone/transition detector */
+ short ylint, thr2, dqthr;
+ short ylfrac, thr1;
+ short pk0;
+
+ pk0 = (dqsez < 0) ? 1 : 0; /* needed in updating predictor poles */
+
+ mag = dq & 0x7FFF; /* prediction difference magnitude */
+ /* TRANS */
+ ylint = state_ptr->yl >> 15; /* exponent part of yl */
+ ylfrac = (state_ptr->yl >> 10) & 0x1F; /* fractional part of yl */
+ thr1 = (32 + ylfrac) << ylint; /* threshold */
+ thr2 = (ylint > 9) ? 31 << 10 : thr1; /* limit thr2 to 31 << 10 */
+ dqthr = (thr2 + (thr2 >> 1)) >> 1; /* dqthr = 0.75 * thr2 */
+ if (state_ptr->td == 0) /* signal supposed voice */
+ tr = 0;
+ else if (mag <= dqthr) /* supposed data, but small mag */
+ tr = 0; /* treated as voice */
+ else /* signal is data (modem) */
+ tr = 1;
+
+ /*
+ * Quantizer scale factor adaptation.
+ */
+
+ /* FUNCTW & FILTD & DELAY */
+ /* update non-steady state step size multiplier */
+ state_ptr->yu = y + ((wi - y) >> 5);
+
+ /* LIMB */
+ if (state_ptr->yu < 544) /* 544 <= yu <= 5120 */
+ state_ptr->yu = 544;
+ else if (state_ptr->yu > 5120)
+ state_ptr->yu = 5120;
+
+ /* FILTE & DELAY */
+ /* update steady state step size multiplier */
+ state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
+
+ /*
+ * Adaptive predictor coefficients.
+ */
+ if (tr == 1) { /* reset a's and b's for modem signal */
+ state_ptr->a[0] = 0;
+ state_ptr->a[1] = 0;
+ state_ptr->b[0] = 0;
+ state_ptr->b[1] = 0;
+ state_ptr->b[2] = 0;
+ state_ptr->b[3] = 0;
+ state_ptr->b[4] = 0;
+ state_ptr->b[5] = 0;
+ } else { /* update a's and b's */
+ pks1 = pk0 ^ state_ptr->pk[0]; /* UPA2 */
+
+ /* update predictor pole a[1] */
+ a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
+ if (dqsez != 0) {
+ fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
+ if (fa1 < -8191) /* a2p = function of fa1 */
+ a2p -= 0x100;
+ else if (fa1 > 8191)
+ a2p += 0xFF;
+ else
+ a2p += fa1 >> 5;
+
+ if (pk0 ^ state_ptr->pk[1])
+ { /* LIMC */
+ if (a2p <= -12160)
+ a2p = -12288;
+ else if (a2p >= 12416)
+ a2p = 12288;
+ else
+ a2p -= 0x80;
+ }
+ else if (a2p <= -12416)
+ a2p = -12288;
+ else if (a2p >= 12160)
+ a2p = 12288;
+ else
+ a2p += 0x80;
+ }
+
+ /* TRIGB & DELAY */
+ state_ptr->a[1] = a2p;
+
+ /* UPA1 */
+ /* update predictor pole a[0] */
+ state_ptr->a[0] -= state_ptr->a[0] >> 8;
+ if (dqsez != 0)
+ { if (pks1 == 0)
+ state_ptr->a[0] += 192;
+ else
+ state_ptr->a[0] -= 192;
+ } ;
+
+ /* LIMD */
+ a1ul = 15360 - a2p;
+ if (state_ptr->a[0] < -a1ul)
+ state_ptr->a[0] = -a1ul;
+ else if (state_ptr->a[0] > a1ul)
+ state_ptr->a[0] = a1ul;
+
+ /* UPB : update predictor zeros b[6] */
+ for (cnt = 0; cnt < 6; cnt++) {
+ if (code_size == 5) /* for 40Kbps G.723 */
+ state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
+ else /* for G.721 and 24Kbps G.723 */
+ state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
+ if (dq & 0x7FFF) { /* XOR */
+ if ((dq ^ state_ptr->dq[cnt]) >= 0)
+ state_ptr->b[cnt] += 128;
+ else
+ state_ptr->b[cnt] -= 128;
+ }
+ }
+ }
+
+ for (cnt = 5; cnt > 0; cnt--)
+ state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
+ /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
+ if (mag == 0) {
+ state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0xFC20;
+ } else {
+ expon = quan(mag, power2, 15);
+ state_ptr->dq[0] = (dq >= 0) ?
+ (expon << 6) + ((mag << 6) >> expon) :
+ (expon << 6) + ((mag << 6) >> expon) - 0x400;
+ }
+
+ state_ptr->sr[1] = state_ptr->sr[0];
+ /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
+ if (sr == 0) {
+ state_ptr->sr[0] = 0x20;
+ } else if (sr > 0) {
+ expon = quan(sr, power2, 15);
+ state_ptr->sr[0] = (expon << 6) + ((sr << 6) >> expon);
+ } else if (sr > -32768) {
+ mag = -sr;
+ expon = quan(mag, power2, 15);
+ state_ptr->sr[0] = (expon << 6) + ((mag << 6) >> expon) - 0x400;
+ } else
+ state_ptr->sr[0] = (short) 0xFC20;
+
+ /* DELAY A */
+ state_ptr->pk[1] = state_ptr->pk[0];
+ state_ptr->pk[0] = pk0;
+
+ /* TONE */
+ if (tr == 1) /* this sample has been treated as data */
+ state_ptr->td = 0; /* next one will be treated as voice */
+ else if (a2p < -11776) /* small sample-to-sample correlation */
+ state_ptr->td = 1; /* signal may be data */
+ else /* signal is voice */
+ state_ptr->td = 0;
+
+ /*
+ * Adaptation speed control.
+ */
+ state_ptr->dms += (fi - state_ptr->dms) >> 5; /* FILTA */
+ state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7); /* FILTB */
+
+ if (tr == 1)
+ state_ptr->ap = 256;
+ else if (y < 1536) /* SUBTC */
+ state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
+ else if (state_ptr->td == 1)
+ state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
+ else if (abs((state_ptr->dms << 2) - state_ptr->dml) >=
+ (state_ptr->dml >> 3))
+ state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
+ else
+ state_ptr->ap += (-state_ptr->ap) >> 4;
+
+ return ;
+} /* update */
+
+/*------------------------------------------------------------------------------
+*/
+
+static int
+unpack_bytes (int bits, int blocksize, const unsigned char * block, short * samples)
+{ unsigned int in_buffer = 0 ;
+ unsigned char in_byte ;
+ int k, in_bits = 0, bindex = 0 ;
+
+ for (k = 0 ; bindex <= blocksize && k < G72x_BLOCK_SIZE ; k++)
+ { if (in_bits < bits)
+ { in_byte = block [bindex++] ;
+
+ in_buffer |= (in_byte << in_bits);
+ in_bits += 8;
+ }
+ samples [k] = in_buffer & ((1 << bits) - 1);
+ in_buffer >>= bits;
+ in_bits -= bits;
+ } ;
+
+ return k ;
+} /* unpack_bytes */
+
+static int
+pack_bytes (int bits, const short * samples, unsigned char * block)
+{
+ unsigned int out_buffer = 0 ;
+ int k, bindex = 0, out_bits = 0 ;
+ unsigned char out_byte ;
+
+ for (k = 0 ; k < G72x_BLOCK_SIZE ; k++)
+ { out_buffer |= (samples [k] << out_bits) ;
+ out_bits += bits ;
+ if (out_bits >= 8)
+ { out_byte = out_buffer & 0xFF ;
+ out_bits -= 8 ;
+ out_buffer >>= 8 ;
+ block [bindex++] = out_byte ;
+ }
+ } ;
+
+ return bindex ;
+} /* pack_bytes */
+
diff --git a/src/G72x/g72x.h b/src/G72x/g72x.h
new file mode 100644
index 0000000..d7631e6
--- /dev/null
+++ b/src/G72x/g72x.h
@@ -0,0 +1,91 @@
+/*
+** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
+**
+** This program is free software; you can redistribute it and/or modify
+** it under the terms of the GNU Lesser General Public License as published by
+** the Free Software Foundation; either version 2.1 of the License, or
+** (at your option) any later version.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU Lesser General Public License for more details.
+**
+** You should have received a copy of the GNU Lesser General Public License
+** along with this program; if not, write to the Free Software
+** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+*/
+
+/*
+** This file is not the same as the original file from Sun Microsystems. Nearly
+** all the original definitions and function prototypes that were in the file
+** of this name have been moved to g72x_priv.h.
+*/
+
+#ifndef G72X_HEADER_FILE
+#define G72X_HEADER_FILE
+
+/*
+** Number of samples per block to process.
+** Must be a common multiple of possible bits per sample : 2, 3, 4, 5 and 8.
+*/
+#define G72x_BLOCK_SIZE (3 * 5 * 8)
+
+/*
+** Identifiers for the differing kinds of G72x ADPCM codecs.
+** The identifiers also define the number of encoded bits per sample.
+*/
+
+enum
+{ G723_16_BITS_PER_SAMPLE = 2,
+ G723_24_BITS_PER_SAMPLE = 3,
+ G723_40_BITS_PER_SAMPLE = 5,
+
+ G721_32_BITS_PER_SAMPLE = 4,
+ G721_40_BITS_PER_SAMPLE = 5,
+
+ G723_16_SAMPLES_PER_BLOCK = G72x_BLOCK_SIZE,
+ G723_24_SAMPLES_PER_BLOCK = G723_24_BITS_PER_SAMPLE * (G72x_BLOCK_SIZE / G723_24_BITS_PER_SAMPLE),
+ G723_40_SAMPLES_PER_BLOCK = G723_40_BITS_PER_SAMPLE * (G72x_BLOCK_SIZE / G723_40_BITS_PER_SAMPLE),
+
+ G721_32_SAMPLES_PER_BLOCK = G72x_BLOCK_SIZE,
+ G721_40_SAMPLES_PER_BLOCK = G721_40_BITS_PER_SAMPLE * (G72x_BLOCK_SIZE / G721_40_BITS_PER_SAMPLE),
+
+ G723_16_BYTES_PER_BLOCK = (G723_16_BITS_PER_SAMPLE * G72x_BLOCK_SIZE) / 8,
+ G723_24_BYTES_PER_BLOCK = (G723_24_BITS_PER_SAMPLE * G72x_BLOCK_SIZE) / 8,
+ G723_40_BYTES_PER_BLOCK = (G723_40_BITS_PER_SAMPLE * G72x_BLOCK_SIZE) / 8,
+
+ G721_32_BYTES_PER_BLOCK = (G721_32_BITS_PER_SAMPLE * G72x_BLOCK_SIZE) / 8,
+ G721_40_BYTES_PER_BLOCK = (G721_40_BITS_PER_SAMPLE * G72x_BLOCK_SIZE) / 8
+} ;
+
+/* Forward declaration of of g72x_state. */
+
+struct g72x_state ;
+
+/* External function definitions. */
+
+struct g72x_state * g72x_reader_init (int codec, int *blocksize, int *samplesperblock) ;
+struct g72x_state * g72x_writer_init (int codec, int *blocksize, int *samplesperblock) ;
+/*
+** Initialize the ADPCM state table for the given codec.
+** Return 0 on success, 1 on fail.
+*/
+
+int g72x_decode_block (struct g72x_state *pstate, const unsigned char *block, short *samples) ;
+/*
+** The caller fills data->block with data->bytes bytes before calling the
+** function. The value data->bytes must be an integer multiple of
+** data->blocksize and be <= data->max_bytes.
+** When it returns, the caller can read out data->samples samples.
+*/
+
+int g72x_encode_block (struct g72x_state *pstate, short *samples, unsigned char *block) ;
+/*
+** The caller fills state->samples some integer multiple data->samples_per_block
+** (up to G72x_BLOCK_SIZE) samples before calling the function.
+** When it returns, the caller can read out bytes encoded bytes.
+*/
+
+#endif /* !G72X_HEADER_FILE */
+
diff --git a/src/G72x/g72x_priv.h b/src/G72x/g72x_priv.h
new file mode 100644
index 0000000..867c64b
--- /dev/null
+++ b/src/G72x/g72x_priv.h
@@ -0,0 +1,109 @@
+/*
+ * This source code is a product of Sun Microsystems, Inc. and is provided
+ * for unrestricted use. Users may copy or modify this source code without
+ * charge.
+ *
+ * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
+ * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun source code is provided with no support and without any obligation on
+ * the part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+
+#ifndef G72X_PRIVATE_H
+#define G72X_PRIVATE_H
+
+#ifdef __cplusplus
+#error "This code is not designed to be compiled with a C++ compiler."
+#endif
+
+/*
+** The following is the definition of the state structure used by the
+** G.721/G.723 encoder and decoder to preserve their internal state
+** between successive calls. The meanings of the majority of the state
+** structure fields are explained in detail in the CCITT Recommendation
+** G.721. The field names are essentially identical to variable names
+** in the bit level description of the coding algorithm included in this
+** Recommendation.
+*/
+
+struct g72x_state
+{ long yl; /* Locked or steady state step size multiplier. */
+ short yu; /* Unlocked or non-steady state step size multiplier. */
+ short dms; /* Short term energy estimate. */
+ short dml; /* Long term energy estimate. */
+ short ap; /* Linear weighting coefficient of 'yl' and 'yu'. */
+
+ short a[2]; /* Coefficients of pole portion of prediction filter. */
+ short b[6]; /* Coefficients of zero portion of prediction filter. */
+ short pk[2]; /*
+ ** Signs of previous two samples of a partially
+ ** reconstructed signal.
+ **/
+ short dq[6]; /*
+ ** Previous 6 samples of the quantized difference
+ ** signal represented in an internal floating point
+ ** format.
+ **/
+ short sr[2]; /*
+ ** Previous 2 samples of the quantized difference
+ ** signal represented in an internal floating point
+ ** format.
+ */
+ char td; /* delayed tone detect, new in 1988 version */
+
+ /* The following struct members were added for libsndfile. The original
+ ** code worked by calling a set of functions on a sample by sample basis
+ ** which is slow on architectures like Intel x86. For libsndfile, this
+ ** was changed so that the encoding and decoding routines could work on
+ ** a block of samples at a time to reduce the function call overhead.
+ */
+ int (*encoder) (int, struct g72x_state* state) ;
+ int (*decoder) (int, struct g72x_state* state) ;
+
+ int codec_bits, blocksize, samplesperblock ;
+} ;
+
+typedef struct g72x_state G72x_STATE ;
+
+int predictor_zero (G72x_STATE *state_ptr);
+
+int predictor_pole (G72x_STATE *state_ptr);
+
+int step_size (G72x_STATE *state_ptr);
+
+int quantize (int d, int y, short *table, int size);
+
+int reconstruct (int sign, int dqln, int y);
+
+void update (int code_size, int y, int wi, int fi, int dq, int sr, int dqsez, G72x_STATE *state_ptr);
+
+int g721_encoder (int sample, G72x_STATE *state_ptr);
+int g721_decoder (int code, G72x_STATE *state_ptr);
+
+int g723_16_encoder (int sample, G72x_STATE *state_ptr);
+int g723_16_decoder (int code, G72x_STATE *state_ptr);
+
+int g723_24_encoder (int sample, G72x_STATE *state_ptr);
+int g723_24_decoder (int code, G72x_STATE *state_ptr);
+
+int g723_40_encoder (int sample, G72x_STATE *state_ptr);
+int g723_40_decoder (int code, G72x_STATE *state_ptr);
+
+void private_init_state (G72x_STATE *state_ptr) ;
+
+#endif /* G72X_PRIVATE_H */
diff --git a/src/G72x/g72x_test.c b/src/G72x/g72x_test.c
new file mode 100644
index 0000000..79cabce
--- /dev/null
+++ b/src/G72x/g72x_test.c
@@ -0,0 +1,214 @@
+/*
+** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
+**
+** This program is free software; you can redistribute it and/or modify
+** it under the terms of the GNU General Public License as published by
+** the Free Software Foundation; either version 2 of the License, or
+** (at your option) any later version.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program; if not, write to the Free Software
+** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+*/
+
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+
+#include "g72x.h"
+#include "g72x_priv.h"
+
+#ifndef M_PI
+#define M_PI 3.14159265358979323846264338
+#endif
+
+#define BUFFER_SIZE (1<<14) /* Should be (1<<14) */
+#define SAMPLE_RATE 11025
+
+
+static void g721_test (void) ;
+static void g723_test (double margin) ;
+
+static void gen_signal_double (double *data, double scale, int datalen) ;
+static int error_function (double data, double orig, double margin) ;
+
+static int oct_save_short (short *a, short *b, int len) ;
+
+int
+main (int argc, char *argv [])
+{ int bDoAll = 0 ;
+ int nTests = 0 ;
+
+ if (argc != 2)
+ { printf ("Usage : %s <test>\n", argv [0]) ;
+ printf (" Where <test> is one of the following:\n") ;
+ printf (" g721 - test G721 encoder and decoder\n") ;
+ printf (" g723 - test G721 encoder and decoder\n") ;
+ printf (" all - perform all tests\n") ;
+ exit (1) ;
+ } ;
+
+ bDoAll=!strcmp (argv [1], "all");
+
+ if (bDoAll || ! strcmp (argv [1], "g721"))
+ { g721_test () ;
+ nTests++ ;
+ } ;
+
+ if (bDoAll || ! strcmp (argv [1], "g723"))
+ { g723_test (0.53) ;
+ nTests++ ;
+ } ;
+
+ if (nTests == 0)
+ { printf ("Mono : ************************************\n") ;
+ printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
+ printf ("Mono : ************************************\n") ;
+ return 1 ;
+ } ;
+
+ return 0 ;
+} /* main */
+
+static void
+g721_test (void)
+{
+ return ;
+} /* g721_test */
+
+static void
+g723_test (double margin)
+{ static double orig_buffer [BUFFER_SIZE] ;
+ static short orig [BUFFER_SIZE] ;
+ static short data [BUFFER_SIZE] ;
+
+ G72x_STATE encoder_state, decoder_state ;
+
+ long k ;
+ int code, position, max_err ;
+
+ private_init_state (&encoder_state) ;
+ encoder_state.encoder = g723_24_encoder ;
+ encoder_state.codec_bits = 3 ;
+
+ private_init_state (&decoder_state) ;
+ decoder_state.decoder = g723_24_decoder ;
+ decoder_state.codec_bits = 3 ;
+
+ memset (data, 0, BUFFER_SIZE * sizeof (short)) ;
+ memset (orig, 0, BUFFER_SIZE * sizeof (short)) ;
+
+ printf (" g723_test : ") ;
+ fflush (stdout) ;
+
+ gen_signal_double (orig_buffer, 32000.0, BUFFER_SIZE) ;
+ for (k = 0 ; k < BUFFER_SIZE ; k++)
+ orig [k] = (short) orig_buffer [k] ;
+
+ /* Write and read data here. */
+ position = 0 ;
+ max_err = 0 ;
+ for (k = 0 ; k < BUFFER_SIZE ; k++)
+ { code = encoder_state.encoder (orig [k], &encoder_state) ;
+ data [k] = decoder_state.decoder (code, &decoder_state) ;
+ if (abs (orig [k] - data [k]) > max_err)
+ { position = k ;
+ max_err = abs (orig [k] - data [k]) ;
+ } ;
+ } ;
+
+ printf ("\n\nMax error of %d at postion %d.\n", max_err, position) ;
+
+ for (k = 0 ; k < BUFFER_SIZE ; k++)
+ { if (error_function (data [k], orig [k], margin))
+ { printf ("Line %d: Incorrect sample A (#%ld : %d should be %d).\n", __LINE__, k, data [k], orig [k]) ;
+ oct_save_short (orig, data, BUFFER_SIZE) ;
+ exit (1) ;
+ } ;
+ } ;
+
+
+ printf ("ok\n") ;
+
+ return ;
+} /* g723_test */
+
+
+#define SIGNAL_MAXVAL 30000.0
+#define DECAY_COUNT 1000
+
+static void
+gen_signal_double (double *gendata, double scale, int gendatalen)
+{ int k, ramplen ;
+ double amp = 0.0 ;
+
+ ramplen = DECAY_COUNT ;
+
+ for (k = 0 ; k < gendatalen ; k++)
+ { if (k <= ramplen)
+ amp = scale * k / ((double) ramplen) ;
+ else if (k > gendatalen - ramplen)
+ amp = scale * (gendatalen - k) / ((double) ramplen) ;
+
+ gendata [k] = amp * (0.4 * sin (33.3 * 2.0 * M_PI * ((double) (k+1)) / ((double) SAMPLE_RATE))
+ + 0.3 * cos (201.1 * 2.0 * M_PI * ((double) (k+1)) / ((double) SAMPLE_RATE))) ;
+ } ;
+
+ return ;
+} /* gen_signal_double */
+
+static int
+error_function (double data, double orig, double margin)
+{ double error ;
+
+ if (fabs (orig) <= 500.0)
+ error = fabs (fabs (data) - fabs(orig)) / 2000.0 ;
+ else if (fabs (orig) <= 1000.0)
+ error = fabs (data - orig) / 3000.0 ;
+ else
+ error = fabs (data - orig) / fabs (orig) ;
+
+ if (error > margin)
+ { printf ("\n\n*******************\nError : %f\n", error) ;
+ return 1 ;
+ } ;
+ return 0 ;
+} /* error_function */
+
+static int
+oct_save_short (short *a, short *b, int len)
+{ FILE *file ;
+ int k ;
+
+ if (! (file = fopen ("error.dat", "w")))
+ return 1 ;
+
+ fprintf (file, "# Not created by Octave\n") ;
+
+ fprintf (file, "# name: a\n") ;
+ fprintf (file, "# type: matrix\n") ;
+ fprintf (file, "# rows: %d\n", len) ;
+ fprintf (file, "# columns: 1\n") ;
+
+ for (k = 0 ; k < len ; k++)
+ fprintf (file, "% d\n", a [k]) ;
+
+ fprintf (file, "# name: b\n") ;
+ fprintf (file, "# type: matrix\n") ;
+ fprintf (file, "# rows: %d\n", len) ;
+ fprintf (file, "# columns: 1\n") ;
+
+ for (k = 0 ; k < len ; k++)
+ fprintf (file, "% d\n", b [k]) ;
+
+ fclose (file) ;
+ return 0 ;
+} /* oct_save_short */
+