sox.1: fix example for mcompand
[sox.git] / src / g723_40.c
blobb54ff80833b847eed118bd0ee4fb93ce58be7eae
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.
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.
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.
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.
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.
21 * Sun Microsystems, Inc.
22 * 2550 Garcia Avenue
23 * Mountain View, California 94043
27 * g723_40.c
29 * Description:
31 * g723_40_encoder(), g723_40_decoder()
33 * These routines comprise an implementation of the CCITT G.723 40Kbps
34 * ADPCM coding algorithm. Essentially, this implementation is identical to
35 * the bit level description except for a few deviations which
36 * take advantage of workstation attributes, such as hardware 2's
37 * complement arithmetic.
39 * The deviation from the bit level specification (lookup tables),
40 * preserves the bit level performance specifications.
42 * As outlined in the G.723 Recommendation, the algorithm is broken
43 * down into modules. Each section of code below is preceded by
44 * the name of the module which it is implementing.
47 #include "sox_i.h"
48 #include "g711.h"
49 #include "g72x.h"
52 * Maps G.723_40 code word to ructeconstructed scale factor normalized log
53 * magnitude values.
55 static const short _dqlntab[32] = {-2048, -66, 28, 104, 169, 224, 274, 318,
56 358, 395, 429, 459, 488, 514, 539, 566,
57 566, 539, 514, 488, 459, 429, 395, 358,
58 318, 274, 224, 169, 104, 28, -66, -2048};
60 /* Maps G.723_40 code word to log of scale factor multiplier. */
61 static const short _witab[32] = {448, 448, 768, 1248, 1280, 1312, 1856, 3200,
62 4512, 5728, 7008, 8960, 11456, 14080, 16928, 22272,
63 22272, 16928, 14080, 11456, 8960, 7008, 5728, 4512,
64 3200, 1856, 1312, 1280, 1248, 768, 448, 448};
67 * Maps G.723_40 code words to a set of values whose long and short
68 * term averages are computed and then compared to give an indication
69 * how stationary (steady state) the signal is.
71 static const short _fitab[32] = {0, 0, 0, 0, 0, 0x200, 0x200, 0x200,
72 0x200, 0x200, 0x400, 0x600, 0x800, 0xA00, 0xC00, 0xC00,
73 0xC00, 0xC00, 0xA00, 0x800, 0x600, 0x400, 0x200, 0x200,
74 0x200, 0x200, 0x200, 0, 0, 0, 0, 0};
76 static const short qtab_723_40[15] = {-122, -16, 68, 139, 198, 250, 298, 339,
77 378, 413, 445, 475, 502, 528, 553};
80 * g723_40_encoder()
82 * Encodes a 16-bit linear PCM, A-law or u-law input sample and retuens
83 * the resulting 5-bit CCITT G.723 40Kbps code.
84 * Returns -1 if the input coding value is invalid.
86 int g723_40_encoder(int sl, int in_coding, struct g72x_state *state_ptr)
88 short sei, sezi, se, sez; /* ACCUM */
89 short d; /* SUBTA */
90 short y; /* MIX */
91 short sr; /* ADDB */
92 short dqsez; /* ADDC */
93 short dq, i;
95 switch (in_coding) { /* linearize input sample to 14-bit PCM */
96 case AUDIO_ENCODING_ALAW:
97 sl = sox_alaw2linear16(sl) >> 2;
98 break;
99 case AUDIO_ENCODING_ULAW:
100 sl = sox_ulaw2linear16(sl) >> 2;
101 break;
102 case AUDIO_ENCODING_LINEAR:
103 sl >>= 2; /* sl of 14-bit dynamic range */
104 break;
105 default:
106 return (-1);
109 sezi = predictor_zero(state_ptr);
110 sez = sezi >> 1;
111 sei = sezi + predictor_pole(state_ptr);
112 se = sei >> 1; /* se = estimated signal */
114 d = sl - se; /* d = estimation difference */
116 /* quantize prediction difference */
117 y = step_size(state_ptr); /* adaptive quantizer step size */
118 i = quantize(d, y, qtab_723_40, 15); /* i = ADPCM code */
120 dq = reconstruct(i & 0x10, _dqlntab[i], y); /* quantized diff */
122 sr = (dq < 0) ? se - (dq & 0x7FFF) : se + dq; /* reconstructed signal */
124 dqsez = sr + sez - se; /* dqsez = pole prediction diff. */
126 update(5, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
128 return (i);
132 * g723_40_decoder()
134 * Decodes a 5-bit CCITT G.723 40Kbps code and returns
135 * the resulting 16-bit linear PCM, A-law or u-law sample value.
136 * -1 is returned if the output coding is unknown.
138 int g723_40_decoder(int i, int out_coding, struct g72x_state *state_ptr)
140 short sezi, sei, sez, se; /* ACCUM */
141 short y; /* MIX */
142 short sr; /* ADDB */
143 short dq;
144 short dqsez;
146 i &= 0x1f; /* mask to get proper bits */
147 sezi = predictor_zero(state_ptr);
148 sez = sezi >> 1;
149 sei = sezi + predictor_pole(state_ptr);
150 se = sei >> 1; /* se = estimated signal */
152 y = step_size(state_ptr); /* adaptive quantizer step size */
153 dq = reconstruct(i & 0x10, _dqlntab[i], y); /* estimation diff. */
155 sr = (dq < 0) ? (se - (dq & 0x7FFF)) : (se + dq); /* reconst. signal */
157 dqsez = sr - se + sez; /* pole prediction diff. */
159 update(5, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
161 switch (out_coding) {
162 case AUDIO_ENCODING_ALAW:
163 return (tandem_adjust_alaw(sr, se, y, i, 0x10, qtab_723_40));
164 case AUDIO_ENCODING_ULAW:
165 return (tandem_adjust_ulaw(sr, se, y, i, 0x10, qtab_723_40));
166 case AUDIO_ENCODING_LINEAR:
167 return (sr << 2); /* sr was of 14-bit dynamic range */
168 default:
169 return (-1);