/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1992-2001 by Sun Microsystems, Inc.
* All rights reserved.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Description:
*
* g723_init_state(), g723_encode(), g723_decode()
*
* These routines comprise an implementation of the CCITT G.723 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 look up tables and software 2's complement operations are
* replaced with hardware 2's complement.
*
* The deviation (look up tables) from the bit level
* specification, 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 <stdlib.h>
#include <libaudio.h>
/*
*
* Description:
*
* This file contains statically defined lookup tables for
* use with the G.723 coding routines.
*/
/*
* Maps G.723 code word to reconstructed scale factor normalized log
* magnitude values.
*/
/* Maps G.723 code word to log of scale factor multiplier. */
/*
* Maps G.723 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.
*/
/*
* g723_init_state()
*
* Description:
*
* pointed to by 'state_ptr'.
* All the state initial values are specified in the G.723 standard specs.
*/
void
struct audio_g72x_state *state_ptr)
{
int cnta;
}
}
}
/*
* _g723_fmult()
*
* returns the integer product of the "floating point" an and srn
* by the lookup table _fmultwanmant[].
*
*/
static int
int an,
int srn)
{
short wanexp;
if (an == 0) {
return ((srn >= 0) ?
} else if (an > 0) {
if (srn >= 0) {
return ((wanexp >= 0) ?
& 0x7FFF :
} else {
return ((wanexp >= 0) ?
& 0x7FFF) :
}
} else {
& 07700;
if (srn >= 0) {
return ((wanexp >= 0) ?
& 0x7FFF) :
} else {
return ((wanexp >= 0) ?
& 0x7FFF :
}
}
}
/*
* _g723_update()
*
* updates the state variables for each output code
*
*/
static void
int y,
int i,
int dq,
int sr,
int pk0,
struct audio_g72x_state *state_ptr,
int sigpk)
{
int cnt;
short thr2;
/* TRANS */
tr = 0;
else {
tr = 1;
else
}
/*
* Quantizer scale factor adaptation.
*/
/* FUNCTW & FILTD & DELAY */
/* LIMB */
/* FILTE & DELAY */
/*
* Adaptive predictor coefficients.
*/
if (tr == 1) {
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 {
/* UPA2 */
if (sigpk == 0) {
if (fa1 < -8191)
a2p -= 0x100;
else if (fa1 > 8191)
a2p += 0xFF;
else
/* 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 */
/* UPA1 */
if (sigpk == 0)
if (pks1 == 0)
state_ptr->a[0] += 192;
else
state_ptr->a[0] -= 192;
/* LIMD */
/* UPB : update of b's */
if (dq & 0x3FFF) {
/* XOR */
else
}
}
}
/* FLOAT A */
if (mag == 0) {
} else {
}
/* FLOAT B */
if (sr == 0) {
} else if (sr > 0) {
} else {
}
/* DELAY A */
/* TONE */
if (tr == 1)
else if (a2p < -11776)
else
/*
* Adaptation speed control.
*/
if (tr == 1)
else if (y < 1536) /* SUBTC */
else
}
/*
* _g723_quantize()
*
* Description:
*
* Given a raw sample, 'd', of the difference signal and a
* quantization step size scale factor, 'y', this routine returns the
* G.723 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.
*/
static unsigned int
int d, /* Raw difference signal sample. */
int y) /* Step size multiplier. */
{
/* LOG */
/* SUBTB */
/* QUAN */
unsigned char i; /* G.723 codeword. */
/*
* LOG
*
* Compute base 2 log of 'd', and store in 'dln'.
*
*/
/*
* SUBTB
*
* "Divide" by step size multiplier.
*/
/*
* QUAN
*
* Obtain codword for 'd'.
*/
if (d < 0)
i ^= 7; /* Stuff in sign of 'd'. */
else if (i == 0)
i = 7; /* New in 1988 revision */
return (i);
}
/*
* _g723_reconstr()
*
* Description:
*
* Returns reconstructed difference signal 'dq' obtained from
* G.723 codeword 'i' and quantization step size scale factor 'y'.
* Multiplication is performed in log base 2 domain as addition.
*/
static int
int i, /* G.723 codeword. */
unsigned long y) /* Step size multiplier. */
{
/* ADD A */
/* ANTILOG */
short dqt;
if (dql < 0)
dq = 0;
else { /* ANTILOG */
}
if (i & 4)
dq -= 0x8000;
return (dq);
}
/*
* _tandem_adjust(sr, se, y, i)
*
* Description:
*
* At the end of ADPCM decoding, it simulates an encoder which may be receiving
* the output of this decoder as a tandem process. If the output of the
* simulated encoder differs from the input to this decoder, the decoder output
* is adjusted by one level of A-law or Mu-law codes.
*
* Input:
* sr decoder output linear PCM sample,
* se predictor estimate sample,
* y quantizer step size,
* i decoder input code
*
* Return:
* adjusted A-law or Mu-law compressed sample.
*/
static int
int sr, /* decoder output linear PCM sample */
int se, /* predictor estimate sample */
int y, /* quantizer step size */
int i) /* decoder input code */
{
if (id == i) /* no adjustment on sp */
return (sp);
else { /* sp adjustment needed */
if (sp & 0x80)
else
} else { /* sp adjusted to next higher value */
if (sp & 0x80)
else
}
return (sd);
}
}
static int
int sr, /* decoder output linear PCM sample */
int se, /* predictor estimate sample */
int y, /* quantizer step size */
int i) /* decoder input code */
{
if (id == i)
return (sp);
else {
/* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */
/* u-law codes : 0, 1, ... 7E, 7F, FF, FE, ... 81, 80 */
if (sp & 0x80)
else
} else { /* sp adjusted to next higher value */
if (sp & 0x80)
else
}
return (sd);
}
}
static unsigned char
int sl,
struct audio_g72x_state *state_ptr)
{
short d; /* SUBTA */
short dq, i;
int cnt;
/* ACCUM */
else {
if (dif > 0)
else if (dif < 0)
}
i = _g723_quantize(d, y);
dq = _g723_reconstr(i, y);
if (dqsez == 0) {
pk0 = 0;
sigpk = 1;
} else {
sigpk = 0;
}
return (i);
}
/*
* g723_encode()
*
* Description:
*
* Encodes a buffer of linear PCM, A-law or Mu-law data pointed to by 'in_buf'
* according the G.723 encoding algorithm and packs the resulting code words
* into bytes. The bytes of codewords are written to a buffer
* pointed to by 'out_buf'.
*
* Notes:
*
* In the event that the number packed codes is shorter than a sample unit,
* the remainder is saved in the state stucture till next call. It is then
* packed into the new buffer on the next call.
* The number of valid bytes in 'out_buf' is returned in *out_size. Note that
* this will not always be equal to 3/8 of 'data_size' on input. On the
* final call to 'g723_encode()' the calling program might want to
* check if any code bits was left over. This can be
* done by calling 'g723_encode()' with data_size = 0, which returns in
* *out_size a* 0 if nothing was leftover and the number of bits left over in
* the state structure which now is in out_buf[0].
*
* The 3 lower significant bits of an individual byte in the output byte
* stream is packed with a G.723 code first. Then the 3 higher order
* bits are packed with the next code.
*/
int
void *in_buf,
int data_size,
unsigned char *out_buf,
int *out_size,
struct audio_g72x_state *state_ptr)
{
int i;
unsigned char *out_ptr;
unsigned char *leftover;
unsigned int bits;
unsigned int codes;
int offset;
short *short_ptr;
unsigned char *char_ptr;
/* Dereference the array pointer for faster access */
/* Return all cached leftovers */
if (data_size == 0) {
for (i = 0; state_ptr->leftover_cnt > 0; i++) {
}
if (i > 0) {
/* Round up to a complete sample unit */
for (; i < 3; i++)
*out_buf++ = 0;
}
*out_size = i;
state_ptr->leftover_cnt = 0;
return (AUDIO_SUCCESS);
}
/* XXX - if linear, it had better be 16-bit! */
if (data_size & 1) {
return (AUDIO_ERR_BADFRAME);
} else {
data_size >>= 1;
}
} else {
}
while (data_size--) {
case AUDIO_ENCODING_LINEAR:
break;
case AUDIO_ENCODING_ALAW:
break;
case AUDIO_ENCODING_ULAW:
break;
default:
return (AUDIO_ERR_ENCODING);
}
/* pack the resulting code into leftover buffer */
bits += 3;
if (bits >= 8) {
bits -= 8;
codes >>= 8;
offset++;
}
/* got a whole sample unit so copy it out and reset */
if (bits == 0) {
codes = 0;
state_ptr->leftover_cnt = 0;
offset = 0;
}
}
/* If any residual bits, save them for the next call */
if (bits > 0) {
}
return (AUDIO_SUCCESS);
}
/*
* g723_decode()
*
* Description:
*
* Decodes a buffer of G.723 encoded data pointed to by 'in_buf' and
* writes the resulting linear PCM, A-law or Mu-law words into a buffer
* pointed to by 'out_buf'.
*
*/
int
unsigned char *in_buf, /* Buffer of g723 encoded data. */
int data_size, /* Size in bytes of in_buf. */
void *out_buf, /* Decoded data buffer. */
int *out_size,
{
unsigned char *inbuf_end;
short *linear_ptr;
unsigned int codes;
unsigned int bits;
int cnt;
short dq;
char sigpk;
short dqsez;
unsigned char i;
linear_ptr = (short *)out_buf;
/* Leftovers in decoding are only up to 8 bits */
if (bits < 3) {
bits += 8;
}
/* ACCUM */
else {
if (dif > 0)
else if (dif < 0)
}
i = codes & 7;
dq = _g723_reconstr(i, y);
/* ADDB */
if (dq < 0)
else
switch (out_header->encoding) {
case AUDIO_ENCODING_LINEAR:
break;
case AUDIO_ENCODING_ALAW:
break;
case AUDIO_ENCODING_ULAW:
break;
default:
return (AUDIO_ERR_ENCODING);
}
codes >>= 3;
bits -= 3;
}
if (bits > 0)
/* Calculate number of samples returned */
else
return (AUDIO_SUCCESS);
}