Newsgroups: comp.speech
Path: cantaloupe.srv.cs.cmu.edu!rochester!udel!gatech!howland.reston.ans.net!vixen.cso.uiuc.edu!news.ksu.ksu.edu!lazrus.cca.rockwell.com!dz14!rwdehoed
From: rwdehoed@dz12.cca.rockwell.com (R.W. DeHoedt)
Subject: C program G711_G721_G723 
Message-ID: <D5LoMs.D5G@lazrus.cca.rockwell.com>
Sender: news@lazrus.cca.rockwell.com
Nntp-Posting-Host: dz14.cca.rockwell.com
Reply-To: rwdehoed@dz12.cca.rockwell.com
Organization: Rockwell International
Date: Fri, 17 Mar 1995 19:54:27 GMT
Lines: 110

Hi All,

We are experiancing a problem that appears to be caused by a error in
the C program (see below).  The error is related to how the program
translates sr to sp when sr is outside the range defined by the virtual
decision level.  We're  a little concerned that this hasn't been
noticed before, so We're not entirely convinced that we don't
understand something, or that we are doing something wrong.  Any help
or comments would be appreciated.

Reference: 
CCITT G.726 Compress, pg. 48-49.
G711_G721_G723.tar located at ftp://svr-ftp.eng.cam.ac.uk/pub/comp.speech
G711_G721_G723.patch1 located at ftp://svr-ftp.eng.cam.ac.uk/pub/comp.speech

We are using the changes in patch1.

The error shows up during 32 Kbit/s u-law operation.  In the following
code (in tandem_adjust_ulaw),

sp = linear2ulaw(sr << 2);	/* short to u-law compression */

the way the (sr << 2) is represented outside the linear2ulaw function
vs. how it is represented within the function is causing an error for
certain ranges of data.

For example, if sr is -24608, hFFFF9FE0
the parameter (outside) passed to linear2ulaw (sr << 2) is hFFFE7F80. 

Note: sr has been declared as an integer and on our sun is represented
as 32 bits.  

Internal to the linear2ulaw function, the "parameter", called pcm_val,
has been declared as short (which is only 16 bits).  So after the
hFFE7F80 is passed in, it becomes a h7F80 which is now a positive value
(32640) !!!!!

The 32640 is processed and returns a value of h80, which is the maximum
positive value (which is correct for a 32640).  BUT SINCE THE ORGINIAL
VALUE WAS -24608, THE VALUE RETURNED SHOULD HAVE BEEN h00, which is the
the most negative value.

Though the above is just one value, there is numerous other examples
the exhibit the same problem.

The code is easily fixed by changing the declaration for pcm_val from
short to int in the linear2ulaw function.

linear2ulaw(pcm_val)
	int		pcm_val;	/* 2's complement (16-bit range) */

Is this real a problem or are we just doing something wrong?

<<<<  Snippetts of the code  >>>>>>>

From Patch 1 code (orginial)

linear2ulaw(pcm_val)
	short		pcm_val;	/* 2's complement (16-bit range) */
{
	short		mask;
	short		seg;
	unsigned char	uval;

	/* Get the sign and the magnitude of the value. */
	pcm_val = pcm_val >> 2;

	if (pcm_val < 0) {
		pcm_val = -pcm_val;
		mask = 0x7F;
	} else {
		mask = 0xFF;
	}
        if ( pcm_val > CLIP ) pcm_val = CLIP;	/* clip the magnitude */
	pcm_val += (BIAS >> 2);



tandem_adjust_ulaw(sr, se, y, i, sign, qtab)
	int		sr;	/* decoder output linear PCM sample */
	int		se;	/* predictor estimate sample */
	int		y;	/* quantizer step size */
	int		i;	/* decoder input code */
	int		sign;
	short		*qtab;
{
	unsigned char	sp;	/* u-law compressed 8-bit code */
	short		dx;	/* prediction error */
	char		id;	/* quantized prediction error */
	int		sd;	/* adjusted u-law decoded sample value */
	int		im;	/* biased magnitude of i */
	int		imx;	/* biased magnitude of id */

	if (sr <= -32768)
		sr = 0;
	sp = linear2ulaw(sr << 2);	/* short to u-law compression */



Thanks, Russ

---
=============================================       \         /
Russ W. De Hoedt        Phone:  319-395-3059         \   _   /        
Rockwell                Fax:    319-395-4068  ________\_( )_/_________
E-mail: rwdehoed@cca.rockwell.com                    \_( o )_/        
=============  ASICS - R - US ===============           \_/
library rockwell; use rockwell.disclaimer.all
   I speak only for myself, not Rockwell.

