formats: clarify setting of reverse_bytes
[sox.git] / src / g721.c
blob3a220b9fc56239e3ca5a4bd894d09e4cfe207335
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 * g721.c
29 * Description:
31 * g721_encoder(), g721_decoder()
33 * These routines comprise an implementation of the CCITT G.721 ADPCM
34 * coding algorithm. Essentially, this implementation is identical to
35 * the bit level description except for a few deviations which
36 * take advantage of work station attributes, such as hardware 2's
37 * complement arithmetic and large memory. Specifically, certain time
38 * consuming operations such as multiplications are replaced
39 * with lookup tables and software 2's complement operations are
40 * replaced with hardware 2's complement.
42 * The deviation from the bit level specification (lookup tables)
43 * preserves the bit level performance specifications.
45 * As outlined in the G.721 Recommendation, the algorithm is broken
46 * down into modules. Each section of code below is preceded by
47 * the name of the module which it is implementing.
51 #include "sox_i.h"
52 #include "g72x.h"
53 #include "g711.h"
55 static const short qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
57 * Maps G.721 code word to reconstructed scale factor normalized log
58 * magnitude values.
60 static const short _dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
61 425, 373, 323, 273, 213, 135, 4, -2048};
63 /* Maps G.721 code word to log of scale factor multiplier. */
64 static const short _witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
65 1122, 355, 198, 112, 64, 41, 18, -12};
67 * Maps G.721 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[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
72 0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
75 * g721_encoder()
77 * Encodes the input vale of linear PCM, A-law or u-law data sl and returns
78 * the resulting code. -1 is returned for unknown input coding value.
80 int g721_encoder(int sl, int in_coding, struct g72x_state *state_ptr)
82 short sezi, se, sez; /* ACCUM */
83 short d; /* SUBTA */
84 short sr; /* ADDB */
85 short y; /* MIX */
86 short dqsez; /* ADDC */
87 short dq, i;
89 switch (in_coding) { /* linearize input sample to 14-bit PCM */
90 case AUDIO_ENCODING_ALAW:
91 sl = sox_alaw2linear16(sl) >> 2;
92 break;
93 case AUDIO_ENCODING_ULAW:
94 sl = sox_ulaw2linear16(sl) >> 2;
95 break;
96 case AUDIO_ENCODING_LINEAR:
97 sl >>= 2; /* 14-bit dynamic range */
98 break;
99 default:
100 return (-1);
103 sezi = predictor_zero(state_ptr);
104 sez = sezi >> 1;
105 se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
107 d = sl - se; /* estimation difference */
109 /* quantize the prediction difference */
110 y = step_size(state_ptr); /* quantizer step size */
111 i = quantize(d, y, qtab_721, 7); /* i = ADPCM code */
113 dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized est diff */
115 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
117 dqsez = sr + sez - se; /* pole prediction diff. */
119 update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
121 return (i);
125 * g721_decoder()
127 * Description:
129 * Decodes a 4-bit code of G.721 encoded data of i and
130 * returns the resulting linear PCM, A-law or u-law value.
131 * return -1 for unknown out_coding value.
133 int g721_decoder(int i, int out_coding, struct g72x_state *state_ptr)
135 short sezi, sei, sez, se; /* ACCUM */
136 short y; /* MIX */
137 short sr; /* ADDB */
138 short dq;
139 short dqsez;
141 i &= 0x0f; /* mask to get proper bits */
142 sezi = predictor_zero(state_ptr);
143 sez = sezi >> 1;
144 sei = sezi + predictor_pole(state_ptr);
145 se = sei >> 1; /* se = estimated signal */
147 y = step_size(state_ptr); /* dynamic quantizer step size */
149 dq = reconstruct(i & 0x08, _dqlntab[i], y); /* quantized diff. */
151 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : se + dq; /* reconst. signal */
153 dqsez = sr - se + sez; /* pole prediction diff. */
155 update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
157 switch (out_coding) {
158 case AUDIO_ENCODING_ALAW:
159 return (tandem_adjust_alaw(sr, se, y, i, 8, qtab_721));
160 case AUDIO_ENCODING_ULAW:
161 return (tandem_adjust_ulaw(sr, se, y, i, 8, qtab_721));
162 case AUDIO_ENCODING_LINEAR:
163 return (sr << 2); /* sr was 14-bit dynamic range */
164 default:
165 return (-1);