]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/mmedia/g721.cpp
This should work on SuSE and Mandrake icon themes.
[wxWidgets.git] / contrib / src / mmedia / g721.cpp
CommitLineData
e8482f24
GL
1/*
2 * This source code is a product of Sun Microsystems, Inc. and is provided
3 * for unrestricted use. Users may copy or modify this source code without
4 * charge.
5 *
6 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
7 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
8 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
9 *
10 * Sun source code is provided with no support and without any obligation on
11 * the part of Sun Microsystems, Inc. to assist in its use, correction,
12 * modification or enhancement.
13 *
14 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
15 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
16 * OR ANY PART THEREOF.
17 *
18 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
19 * or profits or other special, indirect and consequential damages, even if
20 * Sun has been advised of the possibility of such damages.
21 *
22 * Sun Microsystems, Inc.
23 * 2550 Garcia Avenue
24 * Mountain View, California 94043
25 */
26
92a19c2e 27#include "wx/wxprec.h"
e8482f24
GL
28
29/*
30 * g721.c
31 *
32 * Description:
33 *
34 * g721_encoder(), g721_decoder()
35 *
36 * These routines comprise an implementation of the CCITT G.721 ADPCM
37 * coding algorithm. Essentially, this implementation is identical to
38 * the bit level description except for a few deviations which
39 * take advantage of work station attributes, such as hardware 2's
40 * complement arithmetic and large memory. Specifically, certain time
41 * consuming operations such as multiplications are replaced
42 * with lookup tables and software 2's complement operations are
43 * replaced with hardware 2's complement.
44 *
45 * The deviation from the bit level specification (lookup tables)
46 * preserves the bit level performance specifications.
47 *
48 * As outlined in the G.721 Recommendation, the algorithm is broken
49 * down into modules. Each section of code below is preceded by
50 * the name of the module which it is implementing.
51 *
52 */
53#include "wx/mmedia/internal/g72x.h"
54
55static short qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
56/*
57 * Maps G.721 code word to reconstructed scale factor normalized log
58 * magnitude values.
59 */
60static short _dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
61 425, 373, 323, 273, 213, 135, 4, -2048};
62
63/* Maps G.721 code word to log of scale factor multiplier. */
64static short _witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
65 1122, 355, 198, 112, 64, 41, 18, -12};
66/*
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.
70 */
71static short _fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
72 0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
73
74/*
75 * g721_encoder()
76 *
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.
79 */
80int
81g721_encoder(
82 int sl,
83 int in_coding,
84 struct g72x_state *state_ptr)
85{
86 short sezi, se, sez; /* ACCUM */
87 short d; /* SUBTA */
88 short sr; /* ADDB */
89 short y; /* MIX */
90 short dqsez; /* ADDC */
91 short dq, i;
92
93 switch (in_coding) { /* linearize input sample to 14-bit PCM */
94 case AUDIO_ENCODING_ALAW:
95 sl = alaw2linear(sl) >> 2;
96 break;
97 case AUDIO_ENCODING_ULAW:
98 sl = ulaw2linear(sl) >> 2;
99 break;
100 case AUDIO_ENCODING_LINEAR:
101 sl = ((short)sl) >> 2; /* 14-bit dynamic range */
102 break;
103 default:
104 return (-1);
105 }
106
107 sezi = predictor_zero(state_ptr);
108 sez = sezi >> 1;
109 se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
110
111 d = sl - se; /* estimation difference */
112
113 /* quantize the prediction difference */
114 y = step_size(state_ptr); /* quantizer step size */
115 i = quantize(d, y, qtab_721, 7); /* i = ADPCM code */
116
117 dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized est diff */
118
119 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
120
121 dqsez = sr + sez - se; /* pole prediction diff. */
122
123 update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
124
125 return (i);
126}
127
128/*
129 * g721_decoder()
130 *
131 * Description:
132 *
133 * Decodes a 4-bit code of G.721 encoded data of i and
134 * returns the resulting linear PCM, A-law or u-law value.
135 * return -1 for unknown out_coding value.
136 */
137int
138g721_decoder(
139 int i,
140 int out_coding,
141 struct g72x_state *state_ptr)
142{
143 short sezi, sei, sez, se; /* ACCUM */
144 short y; /* MIX */
145 short sr; /* ADDB */
146 short dq;
147 short dqsez;
148
149 i &= 0x0f; /* mask to get proper bits */
150 sezi = predictor_zero(state_ptr);
151 sez = sezi >> 1;
152 sei = sezi + predictor_pole(state_ptr);
153 se = sei >> 1; /* se = estimated signal */
154
155 y = step_size(state_ptr); /* dynamic quantizer step size */
156
157 dq = reconstruct(i & 0x08, _dqlntab[i], y); /* quantized diff. */
158
159 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : se + dq; /* reconst. signal */
160
161 dqsez = sr - se + sez; /* pole prediction diff. */
162
163 update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
164
165 switch (out_coding) {
166 case AUDIO_ENCODING_ALAW:
167 return (tandem_adjust_alaw(sr, se, y, i, 8, qtab_721));
168 case AUDIO_ENCODING_ULAW:
169 return (tandem_adjust_ulaw(sr, se, y, i, 8, qtab_721));
170 case AUDIO_ENCODING_LINEAR:
171 return (sr << 2); /* sr was 14-bit dynamic range */
172 default:
173 return (-1);
174 }
175}