/*
* 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:
*
* g721_encode(), g721_decode(), g721_set_law()
*
* 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 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.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 <stdlib.h>
#include <libaudio.h>
/*
* Maps G.721 code word to reconstructed scale factor normalized log
* magnitude values.
*/
425, 373, 323, 273, 213, 135, 4, -2048};
/* Maps G.721 code word to log of scale factor multiplier. */
35904, 11360, 6336, 3584, 2048, 1312, 576, -384};
/*
* 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.
*/
0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
/*
* g721_init_state()
*
* Description:
*
* pointed to by 'state_ptr'.
* All the initial state values are specified in the G.721 standard specs.
*/
void
struct audio_g72x_state *state_ptr)
{
int cnta;
}
}
}
/*
* _g721_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 :
}
}
}
/*
* _g721_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.
*/
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
}
/*
* _g721_quantize()
*
* Description:
*
* Given a raw sample, 'd', of the difference signal and a
* quantization step size scale factor, 'y', this routine returns the
* G.721 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 */
char i; /* G.721 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 ^= 0xF; /* Stuff in sign of 'd'. */
else if (i == 0)
i = 0xF; /* New in 1988 revision */
return (i);
}
/*
* _g721_reconstr()
*
* Description:
*
* Returns reconstructed difference signal 'dq' obtained from
* G.721 codeword 'i' and quantization step size scale factor 'y'.
* Multiplication is performed in log base 2 domain as addition.
*/
static unsigned long
int i, /* G.721 codeword. */
unsigned long y) /* Step size multiplier. */
{
/* ADD A */
/* ANTILOG */
short dqt;
if (dql < 0)
dq = 0;
else { /* ANTILOG */
}
if (i & 8)
dq -= 0x4000;
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 u-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 u-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 */
/* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */
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 */
if (sp & 0x80)
else
} else { /* sp adjusted to next higher value */
if (sp & 0x80)
else
}
return (sd);
}
}
/*
* g721_encode()
*
* Description:
*
* Encodes a buffer of linear PCM, A-law or u-law data pointed to by
* 'in_buf' according * the G.721 encoding algorithm and packs the
* resulting code words into bytes. The bytes of codeword pairs are
* written to a buffer pointed to by 'out_buf'.
*
* Notes:
*
* In the event that the total number of codewords which have to be
* written is odd, the last unpairable codeword is saved in the
* state structure till the next call. It is then paired off and
* packed with the first codeword of the new buffer. The number of
* valid bytes in 'out_buf' is returned in *out_size. Note that
* *out_size will not always be equal to half * of 'data_size' on input.
* On the final call to 'g721_encode()' the calling program might want to
* check if a codeword was left over. This can be
* done by calling 'g721_encode()' with data_size = 0, which returns in
* *out_size a 0 if nothing was leftover and 1 if a codeword was leftover
* which now is in out_buf[0].
*
* The 4 lower significant bits of an individual byte in the output byte
* stream is packed with a G.721 codeword first. Then the 4 higher order
* bits are packed with the next codeword.
*/
int
void *in_buf,
int data_size,
unsigned char *out_buf,
int *out_size,
struct audio_g72x_state *state_ptr)
{
short d; /* SUBTA */
short dq, i;
int out_leng;
unsigned char *char_in;
unsigned char *char_out;
short *short_ptr;
if (data_size == 0) {
/* Actually, the leftover count will never be more than 4 */
for (i = 0; state_ptr->leftover_cnt > 0; i++) {
}
*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 {
}
} else {
}
if (state_ptr->leftover_cnt > 0) {
state_ptr->leftover_cnt = 0;
data_size += 1;
cnta = 1;
} else {
cnta = 0;
}
/* EXPAND */
case AUDIO_ENCODING_LINEAR:
break;
case AUDIO_ENCODING_ALAW:
break;
case AUDIO_ENCODING_ULAW:
break;
default:
return (AUDIO_ERR_ENCODING);
}
/* ACCUM */
else {
if (dif > 0)
else if (dif < 0)
}
i = _g721_quantize(d, y);
dq = _g721_reconstr(i, y);
/* ADDB */
if (cnta & 1) {
*char_out++ += i << 4;
*char_out = i;
} else {
/*
* save the last codeword which can not be paired into
* a byte in the state stucture and set leftover_flag.
*/
}
if (dqsez == 0) {
pk0 = 0;
sigpk = 1;
} else {
sigpk = 0;
}
}
return (AUDIO_SUCCESS);
}
/*
* g721_decode()
*
* Description:
*
* Decodes a buffer of G.721 encoded data pointed to by 'in_buf' and
* writes the resulting linear PCM, A-law or Mu-law bytes into a buffer
* pointed to by 'out_buf'.
*/
int
unsigned char *in_buf, /* Buffer of g721 encoded data. */
int data_size, /* Size in bytes of in_buf. */
void *out_buf, /* Decoded data buffer. */
int *out_size,
{
short dq;
char sigpk;
short dqsez;
unsigned char *char_in;
unsigned char *char_out;
short *linear_out;
linear_out = (short *)out_buf;
if (cnta & 1)
i = *char_in++ >> 4;
else
i = *char_in & 0xF;
/* ACCUM */
else {
if (dif > 0)
else if (dif < 0)
}
dq = _g721_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);
}
/* ADDC */
}
return (AUDIO_SUCCESS);
}