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
27b815c7f3Sopenharmony_ci/*
28b815c7f3Sopenharmony_ci * g723_40.c
29b815c7f3Sopenharmony_ci *
30b815c7f3Sopenharmony_ci * Description:
31b815c7f3Sopenharmony_ci *
32b815c7f3Sopenharmony_ci * g723_40_encoder (), g723_40_decoder ()
33b815c7f3Sopenharmony_ci *
34b815c7f3Sopenharmony_ci * These routines comprise an implementation of the CCITT G.723 40Kbps
35b815c7f3Sopenharmony_ci * ADPCM coding algorithm.  Essentially, this implementation is identical to
36b815c7f3Sopenharmony_ci * the bit level description except for a few deviations which
37b815c7f3Sopenharmony_ci * take advantage of workstation attributes, such as hardware 2's
38b815c7f3Sopenharmony_ci * complement arithmetic.
39b815c7f3Sopenharmony_ci *
40b815c7f3Sopenharmony_ci * The deviation from the bit level specification (lookup tables),
41b815c7f3Sopenharmony_ci * preserves the bit level performance specifications.
42b815c7f3Sopenharmony_ci *
43b815c7f3Sopenharmony_ci * As outlined in the G.723 Recommendation, the algorithm is broken
44b815c7f3Sopenharmony_ci * down into modules.  Each section of code below is preceded by
45b815c7f3Sopenharmony_ci * the name of the module which it is implementing.
46b815c7f3Sopenharmony_ci *
47b815c7f3Sopenharmony_ci */
48b815c7f3Sopenharmony_ci
49b815c7f3Sopenharmony_ci#include "g72x.h"
50b815c7f3Sopenharmony_ci#include "g72x_priv.h"
51b815c7f3Sopenharmony_ci
52b815c7f3Sopenharmony_ci/*
53b815c7f3Sopenharmony_ci * Maps G.723_40 code word to ructeconstructed scale factor normalized log
54b815c7f3Sopenharmony_ci * magnitude values.
55b815c7f3Sopenharmony_ci */
56b815c7f3Sopenharmony_cistatic short	_dqlntab [32] = { -2048, -66, 28, 104, 169, 224, 274, 318,
57b815c7f3Sopenharmony_ci				358, 395, 429, 459, 488, 514, 539, 566,
58b815c7f3Sopenharmony_ci				566, 539, 514, 488, 459, 429, 395, 358,
59b815c7f3Sopenharmony_ci				318, 274, 224, 169, 104, 28, -66, -2048 } ;
60b815c7f3Sopenharmony_ci
61b815c7f3Sopenharmony_ci/* Maps G.723_40 code word to log of scale factor multiplier. */
62b815c7f3Sopenharmony_cistatic short	_witab [32] = { 448, 448, 768, 1248, 1280, 1312, 1856, 3200,
63b815c7f3Sopenharmony_ci			4512, 5728, 7008, 8960, 11456, 14080, 16928, 22272,
64b815c7f3Sopenharmony_ci			22272, 16928, 14080, 11456, 8960, 7008, 5728, 4512,
65b815c7f3Sopenharmony_ci			3200, 1856, 1312, 1280, 1248, 768, 448, 448 } ;
66b815c7f3Sopenharmony_ci
67b815c7f3Sopenharmony_ci/*
68b815c7f3Sopenharmony_ci * Maps G.723_40 code words to a set of values whose long and short
69b815c7f3Sopenharmony_ci * term averages are computed and then compared to give an indication
70b815c7f3Sopenharmony_ci * how stationary (steady state) the signal is.
71b815c7f3Sopenharmony_ci */
72b815c7f3Sopenharmony_cistatic short	_fitab [32] = { 0, 0, 0, 0, 0, 0x200, 0x200, 0x200,
73b815c7f3Sopenharmony_ci			0x200, 0x200, 0x400, 0x600, 0x800, 0xA00, 0xC00, 0xC00,
74b815c7f3Sopenharmony_ci			0xC00, 0xC00, 0xA00, 0x800, 0x600, 0x400, 0x200, 0x200,
75b815c7f3Sopenharmony_ci			0x200, 0x200, 0x200, 0, 0, 0, 0, 0 } ;
76b815c7f3Sopenharmony_ci
77b815c7f3Sopenharmony_cistatic short qtab_723_40 [15] = { -122, -16, 68, 139, 198, 250, 298, 339,
78b815c7f3Sopenharmony_ci				378, 413, 445, 475, 502, 528, 553 } ;
79b815c7f3Sopenharmony_ci
80b815c7f3Sopenharmony_ci/*
81b815c7f3Sopenharmony_ci * g723_40_encoder ()
82b815c7f3Sopenharmony_ci *
83b815c7f3Sopenharmony_ci * Encodes a 16-bit linear PCM, A-law or u-law input sample and retuens
84b815c7f3Sopenharmony_ci * the resulting 5-bit CCITT G.723 40Kbps code.
85b815c7f3Sopenharmony_ci * Returns -1 if the input coding value is invalid.
86b815c7f3Sopenharmony_ci */
87b815c7f3Sopenharmony_ciint	g723_40_encoder (int sl, G72x_STATE *state_ptr)
88b815c7f3Sopenharmony_ci{
89b815c7f3Sopenharmony_ci	short		sei, sezi, se, sez ;	/* ACCUM */
90b815c7f3Sopenharmony_ci	short		d ;			/* SUBTA */
91b815c7f3Sopenharmony_ci	short		y ;			/* MIX */
92b815c7f3Sopenharmony_ci	short		sr ;			/* ADDB */
93b815c7f3Sopenharmony_ci	short		dqsez ;			/* ADDC */
94b815c7f3Sopenharmony_ci	short		dq, i ;
95b815c7f3Sopenharmony_ci
96b815c7f3Sopenharmony_ci	/* linearize input sample to 14-bit PCM */
97b815c7f3Sopenharmony_ci	sl >>= 2 ;		/* sl of 14-bit dynamic range */
98b815c7f3Sopenharmony_ci
99b815c7f3Sopenharmony_ci	sezi = predictor_zero (state_ptr) ;
100b815c7f3Sopenharmony_ci	sez = sezi >> 1 ;
101b815c7f3Sopenharmony_ci	sei = sezi + predictor_pole (state_ptr) ;
102b815c7f3Sopenharmony_ci	se = sei >> 1 ;			/* se = estimated signal */
103b815c7f3Sopenharmony_ci
104b815c7f3Sopenharmony_ci	d = sl - se ;			/* d = estimation difference */
105b815c7f3Sopenharmony_ci
106b815c7f3Sopenharmony_ci	/* quantize prediction difference */
107b815c7f3Sopenharmony_ci	y = step_size (state_ptr) ;	/* adaptive quantizer step size */
108b815c7f3Sopenharmony_ci	i = quantize (d, y, qtab_723_40, 15) ;	/* i = ADPCM code */
109b815c7f3Sopenharmony_ci
110b815c7f3Sopenharmony_ci	dq = reconstruct (i & 0x10, _dqlntab [i], y) ;	/* quantized diff */
111b815c7f3Sopenharmony_ci
112b815c7f3Sopenharmony_ci	sr = (dq < 0) ? se - (dq & 0x7FFF) : se + dq ; /* reconstructed signal */
113b815c7f3Sopenharmony_ci
114b815c7f3Sopenharmony_ci	dqsez = sr + sez - se ;		/* dqsez = pole prediction diff. */
115b815c7f3Sopenharmony_ci
116b815c7f3Sopenharmony_ci	update (5, y, _witab [i], _fitab [i], dq, sr, dqsez, state_ptr) ;
117b815c7f3Sopenharmony_ci
118b815c7f3Sopenharmony_ci	return i ;
119b815c7f3Sopenharmony_ci}
120b815c7f3Sopenharmony_ci
121b815c7f3Sopenharmony_ci/*
122b815c7f3Sopenharmony_ci * g723_40_decoder ()
123b815c7f3Sopenharmony_ci *
124b815c7f3Sopenharmony_ci * Decodes a 5-bit CCITT G.723 40Kbps code and returns
125b815c7f3Sopenharmony_ci * the resulting 16-bit linear PCM, A-law or u-law sample value.
126b815c7f3Sopenharmony_ci * -1 is returned if the output coding is unknown.
127b815c7f3Sopenharmony_ci */
128b815c7f3Sopenharmony_ciint	g723_40_decoder	(int i, G72x_STATE *state_ptr)
129b815c7f3Sopenharmony_ci{
130b815c7f3Sopenharmony_ci	short		sezi, sei, sez, se ;	/* ACCUM */
131b815c7f3Sopenharmony_ci	short		y ;			/* MIX */
132b815c7f3Sopenharmony_ci	short		sr ;			/* ADDB */
133b815c7f3Sopenharmony_ci	short		dq ;
134b815c7f3Sopenharmony_ci	short		dqsez ;
135b815c7f3Sopenharmony_ci
136b815c7f3Sopenharmony_ci	i &= 0x1f ;			/* mask to get proper bits */
137b815c7f3Sopenharmony_ci	sezi = predictor_zero (state_ptr) ;
138b815c7f3Sopenharmony_ci	sez = sezi >> 1 ;
139b815c7f3Sopenharmony_ci	sei = sezi + predictor_pole (state_ptr) ;
140b815c7f3Sopenharmony_ci	se = sei >> 1 ;			/* se = estimated signal */
141b815c7f3Sopenharmony_ci
142b815c7f3Sopenharmony_ci	y = step_size (state_ptr) ;	/* adaptive quantizer step size */
143b815c7f3Sopenharmony_ci	dq = reconstruct (i & 0x10, _dqlntab [i], y) ;	/* estimation diff. */
144b815c7f3Sopenharmony_ci
145b815c7f3Sopenharmony_ci	sr = (dq < 0) ? (se - (dq & 0x7FFF)) : (se + dq) ; /* reconst. signal */
146b815c7f3Sopenharmony_ci
147b815c7f3Sopenharmony_ci	dqsez = sr - se + sez ;		/* pole prediction diff. */
148b815c7f3Sopenharmony_ci
149b815c7f3Sopenharmony_ci	update (5, y, _witab [i], _fitab [i], dq, sr, dqsez, state_ptr) ;
150b815c7f3Sopenharmony_ci
151b815c7f3Sopenharmony_ci	return arith_shift_left (sr, 2) ;	/* sr was of 14-bit dynamic range */
152b815c7f3Sopenharmony_ci}
153b815c7f3Sopenharmony_ci
154