1 /* This source code is a product of Sun Microsystems, Inc. and is provided
2  * for unrestricted use.  Users may copy or modify this source code without
3  * charge.
4  *
5  * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
6  * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
7  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
8  *
9  * Sun source code is provided with no support and without any obligation on
10  * the part of Sun Microsystems, Inc. to assist in its use, correction,
11  * modification or enhancement.
12  *
13  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
14  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
15  * OR ANY PART THEREOF.
16  *
17  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
18  * or profits or other special, indirect and consequential damages, even if
19  * Sun has been advised of the possibility of such damages.
20  *
21  * Sun Microsystems, Inc.
22  * 2550 Garcia Avenue
23  * Mountain View, California  94043
24  */
25 
26 /*
27  * g723_24.c
28  *
29  * Description:
30  *
31  * g723_24_encoder(), g723_24_decoder()
32  *
33  * These routines comprise an implementation of the CCITT G.723 24 Kbps
34  * ADPCM coding algorithm.  Essentially, this implementation is identical to
35  * the bit level description except for a few deviations which take advantage
36  * of workstation attributes, such as hardware 2's complement arithmetic.
37  *
38  */
39 #include "sox_i.h"
40 #include "g711.h"
41 #include "g72x.h"
42 
43 /*
44  * Maps G.723_24 code word to reconstructed scale factor normalized log
45  * magnitude values.
46  */
47 static const short	_dqlntab[8] = {-2048, 135, 273, 373, 373, 273, 135, -2048};
48 
49 /* Maps G.723_24 code word to log of scale factor multiplier. */
50 static const short	_witab[8] = {-128, 960, 4384, 18624, 18624, 4384, 960, -128};
51 
52 /*
53  * Maps G.723_24 code words to a set of values whose long and short
54  * term averages are computed and then compared to give an indication
55  * how stationary (steady state) the signal is.
56  */
57 static const short	_fitab[8] = {0, 0x200, 0x400, 0xE00, 0xE00, 0x400, 0x200, 0};
58 
59 static const short qtab_723_24[3] = {8, 218, 331};
60 
61 /*
62  * g723_24_encoder()
63  *
64  * Encodes a linear PCM, A-law or u-law input sample and returns its 3-bit code.
65  * Returns -1 if invalid input coding value.
66  */
g723_24_encoder(int sl,int in_coding,struct g72x_state * state_ptr)67 int g723_24_encoder(int sl, int in_coding, struct g72x_state *state_ptr)
68 {
69 	short		sei, sezi, se, sez;	/* ACCUM */
70 	short		d;			/* SUBTA */
71 	short		y;			/* MIX */
72 	short		sr;			/* ADDB */
73 	short		dqsez;			/* ADDC */
74 	short		dq, i;
75 
76 	switch (in_coding) {	/* linearize input sample to 14-bit PCM */
77 	case AUDIO_ENCODING_ALAW:
78 		sl = sox_alaw2linear16(sl) >> 2;
79 		break;
80 	case AUDIO_ENCODING_ULAW:
81 		sl = sox_ulaw2linear16(sl) >> 2;
82 		break;
83 	case AUDIO_ENCODING_LINEAR:
84 		sl >>= 2;		/* sl of 14-bit dynamic range */
85 		break;
86 	default:
87 		return (-1);
88 	}
89 
90 	sezi = predictor_zero(state_ptr);
91 	sez = sezi >> 1;
92 	sei = sezi + predictor_pole(state_ptr);
93 	se = sei >> 1;			/* se = estimated signal */
94 
95 	d = sl - se;			/* d = estimation diff. */
96 
97 	/* quantize prediction difference d */
98 	y = step_size(state_ptr);	/* quantizer step size */
99 	i = quantize(d, y, qtab_723_24, 3);	/* i = ADPCM code */
100 	dq = reconstruct(i & 4, _dqlntab[i], y); /* quantized diff. */
101 
102 	sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconstructed signal */
103 
104 	dqsez = sr + sez - se;		/* pole prediction diff. */
105 
106 	update(3, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
107 
108 	return (i);
109 }
110 
111 /*
112  * g723_24_decoder()
113  *
114  * Decodes a 3-bit CCITT G.723_24 ADPCM code and returns
115  * the resulting 16-bit linear PCM, A-law or u-law sample value.
116  * -1 is returned if the output coding is unknown.
117  */
g723_24_decoder(int i,int out_coding,struct g72x_state * state_ptr)118 int g723_24_decoder(int i, int out_coding, struct g72x_state *state_ptr)
119 {
120 	short		sezi, sei, sez, se;	/* ACCUM */
121 	short		y;			/* MIX */
122 	short		sr;			/* ADDB */
123 	short		dq;
124 	short		dqsez;
125 
126 	i &= 0x07;			/* mask to get proper bits */
127 	sezi = predictor_zero(state_ptr);
128 	sez = sezi >> 1;
129 	sei = sezi + predictor_pole(state_ptr);
130 	se = sei >> 1;			/* se = estimated signal */
131 
132 	y = step_size(state_ptr);	/* adaptive quantizer step size */
133 	dq = reconstruct(i & 0x04, _dqlntab[i], y); /* unquantize pred diff */
134 
135 	sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq); /* reconst. signal */
136 
137 	dqsez = sr - se + sez;			/* pole prediction diff. */
138 
139 	update(3, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
140 
141 	switch (out_coding) {
142 	case AUDIO_ENCODING_ALAW:
143 		return (tandem_adjust_alaw(sr, se, y, i, 4, qtab_723_24));
144 	case AUDIO_ENCODING_ULAW:
145 		return (tandem_adjust_ulaw(sr, se, y, i, 4, qtab_723_24));
146 	case AUDIO_ENCODING_LINEAR:
147 		return (sr << 2);	/* sr was of 14-bit dynamic range */
148 	default:
149 		return (-1);
150 	}
151 }
152