1b815c7f3Sopenharmony_ci/*
2b815c7f3Sopenharmony_ci * This source code is a product of Sun Microsystems, Inc. and is provided
3b815c7f3Sopenharmony_ci * for unrestricted use.  Users may copy or modify this source code without
4b815c7f3Sopenharmony_ci * charge.
5b815c7f3Sopenharmony_ci *
6b815c7f3Sopenharmony_ci * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
7b815c7f3Sopenharmony_ci * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
8b815c7f3Sopenharmony_ci * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
9b815c7f3Sopenharmony_ci *
10b815c7f3Sopenharmony_ci * Sun source code is provided with no support and without any obligation on
11b815c7f3Sopenharmony_ci * the part of Sun Microsystems, Inc. to assist in its use, correction,
12b815c7f3Sopenharmony_ci * modification or enhancement.
13b815c7f3Sopenharmony_ci *
14b815c7f3Sopenharmony_ci * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
15b815c7f3Sopenharmony_ci * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
16b815c7f3Sopenharmony_ci * OR ANY PART THEREOF.
17b815c7f3Sopenharmony_ci *
18b815c7f3Sopenharmony_ci * In no event will Sun Microsystems, Inc. be liable for any lost revenue
19b815c7f3Sopenharmony_ci * or profits or other special, indirect and consequential damages, even if
20b815c7f3Sopenharmony_ci * Sun has been advised of the possibility of such damages.
21b815c7f3Sopenharmony_ci *
22b815c7f3Sopenharmony_ci * Sun Microsystems, Inc.
23b815c7f3Sopenharmony_ci * 2550 Garcia Avenue
24b815c7f3Sopenharmony_ci * Mountain View, California  94043
25b815c7f3Sopenharmony_ci */
26b815c7f3Sopenharmony_ci/* 16kbps version created, used 24kbps code and changing as little as possible.
27b815c7f3Sopenharmony_ci * G.726 specs are available from ITU's gopher or WWW site (http://www.itu.ch)
28b815c7f3Sopenharmony_ci * If any errors are found, please contact me at mrand@tamu.edu
29b815c7f3Sopenharmony_ci *      -Marc Randolph
30b815c7f3Sopenharmony_ci */
31b815c7f3Sopenharmony_ci
32b815c7f3Sopenharmony_ci/*
33b815c7f3Sopenharmony_ci * g723_16.c
34b815c7f3Sopenharmony_ci *
35b815c7f3Sopenharmony_ci * Description:
36b815c7f3Sopenharmony_ci *
37b815c7f3Sopenharmony_ci * g723_16_encoder (), g723_16_decoder ()
38b815c7f3Sopenharmony_ci *
39b815c7f3Sopenharmony_ci * These routines comprise an implementation of the CCITT G.726 16 Kbps
40b815c7f3Sopenharmony_ci * ADPCM coding algorithm.  Essentially, this implementation is identical to
41b815c7f3Sopenharmony_ci * the bit level description except for a few deviations which take advantage
42b815c7f3Sopenharmony_ci * of workstation attributes, such as hardware 2's complement arithmetic.
43b815c7f3Sopenharmony_ci *
44b815c7f3Sopenharmony_ci */
45b815c7f3Sopenharmony_ci
46b815c7f3Sopenharmony_ci#include "g72x.h"
47b815c7f3Sopenharmony_ci#include "g72x_priv.h"
48b815c7f3Sopenharmony_ci
49b815c7f3Sopenharmony_ci/*
50b815c7f3Sopenharmony_ci * Maps G.723_16 code word to reconstructed scale factor normalized log
51b815c7f3Sopenharmony_ci * magnitude values.  Comes from Table 11/G.726
52b815c7f3Sopenharmony_ci */
53b815c7f3Sopenharmony_cistatic short _dqlntab [4] = { 116, 365, 365, 116 } ;
54b815c7f3Sopenharmony_ci
55b815c7f3Sopenharmony_ci/* Maps G.723_16 code word to log of scale factor multiplier.
56b815c7f3Sopenharmony_ci *
57b815c7f3Sopenharmony_ci * _witab [4] is actually {-22 , 439, 439, -22}, but FILTD wants it
58b815c7f3Sopenharmony_ci * as WI << 5  (multiplied by 32), so we'll do that here
59b815c7f3Sopenharmony_ci */
60b815c7f3Sopenharmony_cistatic short _witab [4] = { -704, 14048, 14048, -704 } ;
61b815c7f3Sopenharmony_ci
62b815c7f3Sopenharmony_ci/*
63b815c7f3Sopenharmony_ci * Maps G.723_16 code words to a set of values whose long and short
64b815c7f3Sopenharmony_ci * term averages are computed and then compared to give an indication
65b815c7f3Sopenharmony_ci * how stationary (steady state) the signal is.
66b815c7f3Sopenharmony_ci */
67b815c7f3Sopenharmony_ci
68b815c7f3Sopenharmony_ci/* Comes from FUNCTF */
69b815c7f3Sopenharmony_cistatic short _fitab [4] = { 0, 0xE00, 0xE00, 0 } ;
70b815c7f3Sopenharmony_ci
71b815c7f3Sopenharmony_ci/* Comes from quantizer decision level tables (Table 7/G.726)
72b815c7f3Sopenharmony_ci */
73b815c7f3Sopenharmony_cistatic short qtab_723_16 [1] = { 261 } ;
74b815c7f3Sopenharmony_ci
75b815c7f3Sopenharmony_ci
76b815c7f3Sopenharmony_ci/*
77b815c7f3Sopenharmony_ci * g723_16_encoder ()
78b815c7f3Sopenharmony_ci *
79b815c7f3Sopenharmony_ci * Encodes a linear PCM, A-law or u-law input sample and returns its 2-bit code.
80b815c7f3Sopenharmony_ci * Returns -1 if invalid input coding value.
81b815c7f3Sopenharmony_ci */
82b815c7f3Sopenharmony_ciint
83b815c7f3Sopenharmony_cig723_16_encoder (
84b815c7f3Sopenharmony_ci	int			sl,
85b815c7f3Sopenharmony_ci	G72x_STATE *state_ptr)
86b815c7f3Sopenharmony_ci{
87b815c7f3Sopenharmony_ci	short sei, sezi, se, sez ;	/* ACCUM */
88b815c7f3Sopenharmony_ci	short d ;					/* SUBTA */
89b815c7f3Sopenharmony_ci	short y ;					/* MIX */
90b815c7f3Sopenharmony_ci	short sr ;					/* ADDB */
91b815c7f3Sopenharmony_ci	short dqsez ;				/* ADDC */
92b815c7f3Sopenharmony_ci	short dq, i ;
93b815c7f3Sopenharmony_ci
94b815c7f3Sopenharmony_ci	/* linearize input sample to 14-bit PCM */
95b815c7f3Sopenharmony_ci	sl >>= 2 ;	/* sl of 14-bit dynamic range */
96b815c7f3Sopenharmony_ci
97b815c7f3Sopenharmony_ci	sezi = predictor_zero (state_ptr) ;
98b815c7f3Sopenharmony_ci	sez = sezi >> 1 ;
99b815c7f3Sopenharmony_ci	sei = sezi + predictor_pole (state_ptr) ;
100b815c7f3Sopenharmony_ci	se = sei >> 1 ;	/* se = estimated signal */
101b815c7f3Sopenharmony_ci
102b815c7f3Sopenharmony_ci	d = sl - se ;	/* d = estimation diff. */
103b815c7f3Sopenharmony_ci
104b815c7f3Sopenharmony_ci	/* quantize prediction difference d */
105b815c7f3Sopenharmony_ci	y = step_size (state_ptr) ;				/* quantizer step size */
106b815c7f3Sopenharmony_ci	i = quantize (d, y, qtab_723_16, 1) ;	/* i = ADPCM code */
107b815c7f3Sopenharmony_ci
108b815c7f3Sopenharmony_ci	/* Since quantize () only produces a three level output
109b815c7f3Sopenharmony_ci	 * (1, 2, or 3), we must create the fourth one on our own
110b815c7f3Sopenharmony_ci	 */
111b815c7f3Sopenharmony_ci	if (i == 3)					/* i code for the zero region */
112b815c7f3Sopenharmony_ci		if ((d & 0x8000) == 0)	/* If d > 0, i=3 isn't right... */
113b815c7f3Sopenharmony_ci			i = 0 ;
114b815c7f3Sopenharmony_ci
115b815c7f3Sopenharmony_ci	dq = reconstruct (i & 2, _dqlntab [i], y) ; /* quantized diff. */
116b815c7f3Sopenharmony_ci
117b815c7f3Sopenharmony_ci	sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq ; /* reconstructed signal */
118b815c7f3Sopenharmony_ci
119b815c7f3Sopenharmony_ci	dqsez = sr + sez - se ;		/* pole prediction diff. */
120b815c7f3Sopenharmony_ci
121b815c7f3Sopenharmony_ci	update (2, y, _witab [i], _fitab [i], dq, sr, dqsez, state_ptr) ;
122b815c7f3Sopenharmony_ci
123b815c7f3Sopenharmony_ci	return i ;
124b815c7f3Sopenharmony_ci}
125b815c7f3Sopenharmony_ci
126b815c7f3Sopenharmony_ci/*
127b815c7f3Sopenharmony_ci * g723_16_decoder ()
128b815c7f3Sopenharmony_ci *
129b815c7f3Sopenharmony_ci * Decodes a 2-bit CCITT G.723_16 ADPCM code and returns
130b815c7f3Sopenharmony_ci * the resulting 16-bit linear PCM, A-law or u-law sample value.
131b815c7f3Sopenharmony_ci * -1 is returned if the output coding is unknown.
132b815c7f3Sopenharmony_ci */
133b815c7f3Sopenharmony_ciint
134b815c7f3Sopenharmony_cig723_16_decoder (
135b815c7f3Sopenharmony_ci	int			i,
136b815c7f3Sopenharmony_ci	G72x_STATE *state_ptr)
137b815c7f3Sopenharmony_ci{
138b815c7f3Sopenharmony_ci	short sezi, sei, sez, se ;	/* ACCUM */
139b815c7f3Sopenharmony_ci	short y ;					/* MIX */
140b815c7f3Sopenharmony_ci	short sr ;					/* ADDB */
141b815c7f3Sopenharmony_ci	short dq ;
142b815c7f3Sopenharmony_ci	short dqsez ;
143b815c7f3Sopenharmony_ci
144b815c7f3Sopenharmony_ci	i &= 0x03 ;			/* mask to get proper bits */
145b815c7f3Sopenharmony_ci	sezi = predictor_zero (state_ptr) ;
146b815c7f3Sopenharmony_ci	sez = sezi >> 1 ;
147b815c7f3Sopenharmony_ci	sei = sezi + predictor_pole (state_ptr) ;
148b815c7f3Sopenharmony_ci	se = sei >> 1 ;		/* se = estimated signal */
149b815c7f3Sopenharmony_ci
150b815c7f3Sopenharmony_ci	y = step_size (state_ptr) ;	/* adaptive quantizer step size */
151b815c7f3Sopenharmony_ci	dq = reconstruct (i & 0x02, _dqlntab [i], y) ; /* unquantize pred diff */
152b815c7f3Sopenharmony_ci
153b815c7f3Sopenharmony_ci	sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq) ; /* reconst. signal */
154b815c7f3Sopenharmony_ci
155b815c7f3Sopenharmony_ci	dqsez = sr - se + sez ;	/* pole prediction diff. */
156b815c7f3Sopenharmony_ci
157b815c7f3Sopenharmony_ci	update (2, y, _witab [i], _fitab [i], dq, sr, dqsez, state_ptr) ;
158b815c7f3Sopenharmony_ci
159b815c7f3Sopenharmony_ci	/* sr was of 14-bit dynamic range */
160b815c7f3Sopenharmony_ci	return (sr << 2) ;
161b815c7f3Sopenharmony_ci}
162b815c7f3Sopenharmony_ci
163