Mercurial > hg > audiostuff
comparison spandsp-0.0.3/spandsp-0.0.3/src/lpc10_encode.c @ 5:f762bf195c4b
import spandsp-0.0.3
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Fri, 25 Jun 2010 16:00:21 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
4:26cd8f1ef0b1 | 5:f762bf195c4b |
---|---|
1 /* | |
2 * SpanDSP - a series of DSP components for telephony | |
3 * | |
4 * lpc10_encode.c - LPC10 low bit rate speech codec. | |
5 * | |
6 * Written by Steve Underwood <steveu@coppice.org> | |
7 * | |
8 * Copyright (C) 2006 Steve Underwood | |
9 * | |
10 * All rights reserved. | |
11 * | |
12 * This program is free software; you can redistribute it and/or modify | |
13 * it under the terms of the GNU General Public License version 2, as | |
14 * published by the Free Software Foundation. | |
15 * | |
16 * This program is distributed in the hope that it will be useful, | |
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 * GNU General Public License for more details. | |
20 * | |
21 * You should have received a copy of the GNU General Public License | |
22 * along with this program; if not, write to the Free Software | |
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
24 * | |
25 * This code is based on the U.S. Department of Defense reference | |
26 * implementation of the LPC-10 2400 bps Voice Coder. They do not | |
27 * exert copyright claims on their code, and it may be freely used. | |
28 * | |
29 * $Id: lpc10_encode.c,v 1.15 2006/11/21 13:57:40 steveu Exp $ | |
30 */ | |
31 | |
32 #ifdef HAVE_CONFIG_H | |
33 #include <config.h> | |
34 #endif | |
35 | |
36 #include <stdlib.h> | |
37 #include <stdio.h> | |
38 #include <inttypes.h> | |
39 #include <memory.h> | |
40 #if defined(HAVE_TGMATH_H) | |
41 #include <tgmath.h> | |
42 #endif | |
43 #if defined(HAVE_MATH_H) | |
44 #include <math.h> | |
45 #endif | |
46 | |
47 #include "spandsp/telephony.h" | |
48 #include "spandsp/dc_restore.h" | |
49 #include "spandsp/lpc10.h" | |
50 | |
51 #include "lpc10_encdecs.h" | |
52 | |
53 static void lpc10_pack(lpc10_encode_state_t *s, uint8_t ibits[], lpc10_frame_t *t) | |
54 { | |
55 static const int iblist[53] = | |
56 { | |
57 13, 12, 11, 1, 2, 13, 12, 11, 1, 2, | |
58 13, 10, 11, 2, 1, 10, 13, 12, 11, 10, | |
59 2, 13, 12, 11, 10, 2, 1, 12, 7, 6, | |
60 1, 10, 9, 8, 7, 4, 6, 9, 8, 7, | |
61 5, 1, 9, 8, 4, 6, 1, 5, 9, 8, | |
62 7, 5, 6 | |
63 }; | |
64 int32_t itab[13]; | |
65 int x; | |
66 int i; | |
67 | |
68 /* ibits is 54 bits of LPC data ordered as follows: */ | |
69 /* R1-0, R2-0, R3-0, P-0, A-0, */ | |
70 /* R1-1, R2-1, R3-1, P-1, A-1, */ | |
71 /* R1-2, R4-0, R3-2, A-2, P-2, R4-1, */ | |
72 /* R1-3, R2-2, R3-3, R4-2, A-3, */ | |
73 /* R1-4, R2-3, R3-4, R4-3, A-4, */ | |
74 /* P-3, R2-4, R7-0, R8-0, P-4, R4-4, */ | |
75 /* R5-0, R6-0, R7-1,R10-0, R8-1, */ | |
76 /* R5-1, R6-1, R7-2, R9-0, P-5, */ | |
77 /* R5-2, R6-2,R10-1, R8-2, P-6, R9-1, */ | |
78 /* R5-3, R6-3, R7-3, R9-2, R8-3, SYNC */ | |
79 | |
80 itab[0] = t->ipitch; | |
81 itab[1] = t->irms; | |
82 itab[2] = 0; | |
83 for (i = 0; i < LPC10_ORDER; i++) | |
84 itab[i + 3] = t->irc[LPC10_ORDER - 1 - i] & 0x7FFF; | |
85 /* Put 54 bits into the output buffer */ | |
86 x = 0; | |
87 for (i = 0; i < 53; i++) | |
88 { | |
89 x = (x << 1) | (itab[iblist[i] - 1] & 1); | |
90 if ((i & 7) == 7) | |
91 ibits[i >> 3] = x & 0xFF; | |
92 itab[iblist[i] - 1] >>= 1; | |
93 } | |
94 x = (x << 1) | (s->isync & 1); | |
95 s->isync ^= 1; | |
96 x <<= 2; | |
97 ibits[6] = x & 0xFF; | |
98 } | |
99 /*- End of function --------------------------------------------------------*/ | |
100 | |
101 /* Quantize LPC parameters for transmission */ | |
102 static int encode(lpc10_encode_state_t *s, | |
103 lpc10_frame_t *t, | |
104 int32_t *voice, | |
105 int32_t pitch, | |
106 float rms, | |
107 float *rc) | |
108 { | |
109 static const int32_t enctab[16] = | |
110 { | |
111 0, 7, 11, 12, 13, 10, 6, 1, 14, 9, 5, 2, 3, 4, 8, 15 | |
112 }; | |
113 static const int32_t entau[60] = | |
114 { | |
115 19, 11, 27, 25, 29, 21, 23, 22, 30, 14, 15, 7, 39, 38, 46, | |
116 42, 43, 41, 45, 37, 53, 49, 51, 50, 54, 52, 60, 56, 58, 26, | |
117 90, 88, 92, 84, 86, 82, 83, 81, 85, 69, 77, 73, 75, 74, 78, | |
118 70, 71, 67, 99, 97, 113, 112, 114, 98, 106, 104, 108, 100, | |
119 101, 76 | |
120 }; | |
121 static const int32_t enadd[8] = | |
122 { | |
123 1920, -768, 2432, 1280, 3584, 1536, 2816, -1152 | |
124 }; | |
125 static const float enscl[8] = | |
126 { | |
127 0.0204f, 0.0167f, 0.0145f, 0.0147f, 0.0143f, 0.0135f, 0.0125f, 0.0112f | |
128 }; | |
129 static const int32_t enbits[8] = | |
130 { | |
131 6, 5, 4, 4, 4, 4, 3, 3 | |
132 }; | |
133 static const int32_t entab6[64] = | |
134 { | |
135 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, | |
136 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, | |
137 7, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 10, 10, 11, 11, 12, 13, 14, 15 | |
138 }; | |
139 static const int32_t rmst[64] = | |
140 { | |
141 1024, 936, 856, 784, 718, 656, 600, 550, 502, | |
142 460, 420, 384, 352, 328, 294, 270, 246, 226, | |
143 206, 188, 172, 158, 144, 132, 120, 110, 102, | |
144 92, 84, 78, 70, 64, 60, 54, 50, | |
145 46, 42, 38, 34, 32, 30, 26, 24, | |
146 22, 20, 18, 17, 16, 15, 14, 13, | |
147 12, 11, 10, 9, 8, 7, 6, 5, 4, | |
148 3, 2, 1, 0 | |
149 }; | |
150 | |
151 int32_t idel; | |
152 int32_t nbit; | |
153 int32_t i; | |
154 int32_t j; | |
155 int32_t i2; | |
156 int32_t i3; | |
157 int32_t mrk; | |
158 | |
159 /* Scale RMS and RC's to int32_ts */ | |
160 t->irms = rms; | |
161 for (i = 0; i < LPC10_ORDER; i++) | |
162 t->irc[i] = rc[i]*32768.0f; | |
163 if (voice[0] != 0 && voice[1] != 0) | |
164 { | |
165 t->ipitch = entau[pitch - 1]; | |
166 } | |
167 else | |
168 { | |
169 if (s->error_correction) | |
170 { | |
171 t->ipitch = 0; | |
172 if (voice[0] != voice[1]) | |
173 t->ipitch = 127; | |
174 } | |
175 else | |
176 { | |
177 t->ipitch = (voice[0] << 1) + voice[1]; | |
178 } | |
179 } | |
180 /* Encode RMS by binary table search */ | |
181 j = 32; | |
182 idel = 16; | |
183 t->irms = min(t->irms, 1023); | |
184 while (idel > 0) | |
185 { | |
186 if (t->irms > rmst[j - 1]) | |
187 j -= idel; | |
188 if (t->irms < rmst[j - 1]) | |
189 j += idel; | |
190 idel /= 2; | |
191 } | |
192 if (t->irms > rmst[j - 1]) | |
193 --j; | |
194 t->irms = 31 - j/2; | |
195 /* Encode RC(1) and (2) as log-area-ratios */ | |
196 for (i = 0; i < 2; i++) | |
197 { | |
198 i2 = t->irc[i]; | |
199 mrk = 0; | |
200 if (i2 < 0) | |
201 { | |
202 i2 = -i2; | |
203 mrk = 1; | |
204 } | |
205 i2 = min(i2/512, 63); | |
206 i2 = entab6[i2]; | |
207 if (mrk != 0) | |
208 i2 = -i2; | |
209 t->irc[i] = i2; | |
210 } | |
211 /* Encode RC(3) - (10) linearly, remove bias then scale */ | |
212 for (i = 2; i < LPC10_ORDER; i++) | |
213 { | |
214 i2 = (t->irc[i]/2 + enadd[LPC10_ORDER - 1 - i])*enscl[LPC10_ORDER - 1 - i]; | |
215 i2 = max(i2, -127); | |
216 i2 = min(i2, 127); | |
217 nbit = enbits[LPC10_ORDER - 1 - i]; | |
218 i3 = (i2 < 0); | |
219 i2 /= pow_ii(2, nbit); | |
220 if (i3) | |
221 i2--; | |
222 t->irc[i] = i2; | |
223 } | |
224 /* Protect the most significant bits of the most | |
225 important parameters during non-voiced frames. | |
226 RC(1) - RC(4) are protected using 20 parity bits | |
227 replacing RC(5) - RC(10). */ | |
228 if (s->error_correction) | |
229 { | |
230 if (t->ipitch == 0 || t->ipitch == 127) | |
231 { | |
232 t->irc[4] = enctab[(t->irc[0] & 0x1E) >> 1]; | |
233 t->irc[5] = enctab[(t->irc[1] & 0x1E) >> 1]; | |
234 t->irc[6] = enctab[(t->irc[2] & 0x1E) >> 1]; | |
235 t->irc[7] = enctab[(t->irms & 0x1E) >> 1]; | |
236 t->irc[8] = enctab[(t->irc[3] & 0x1E) >> 1] >> 1; | |
237 t->irc[9] = enctab[(t->irc[3] & 0x1E) >> 1] & 1; | |
238 } | |
239 } | |
240 return 0; | |
241 } | |
242 /*- End of function --------------------------------------------------------*/ | |
243 | |
244 static void high_pass_100hz(lpc10_encode_state_t *s, float speech[], int start, int len) | |
245 { | |
246 float si; | |
247 float err; | |
248 int i; | |
249 | |
250 /* 100 Hz high pass filter */ | |
251 for (i = start; i < len; i++) | |
252 { | |
253 si = speech[i]; | |
254 err = si + s->z11*1.859076f - s->z21*0.8648249f; | |
255 si = err - s->z11*2.0f + s->z21; | |
256 s->z21 = s->z11; | |
257 s->z11 = err; | |
258 err = si + s->z12*1.935715f - s->z22*0.9417004f; | |
259 si = err - s->z12*2.0f + s->z22; | |
260 s->z22 = s->z12; | |
261 s->z12 = err; | |
262 speech[i] = si*0.902428f; | |
263 } | |
264 } | |
265 /*- End of function --------------------------------------------------------*/ | |
266 | |
267 lpc10_encode_state_t *lpc10_encode_init(lpc10_encode_state_t *s, int error_correction) | |
268 { | |
269 int i; | |
270 int j; | |
271 | |
272 if (s == NULL) | |
273 { | |
274 if ((s = (lpc10_encode_state_t *) malloc(sizeof(lpc10_encode_state_t))) == NULL) | |
275 return NULL; | |
276 } | |
277 | |
278 s->error_correction = error_correction; | |
279 | |
280 /* State used only by function high_pass_100hz */ | |
281 s->z11 = 0.0f; | |
282 s->z21 = 0.0f; | |
283 s->z12 = 0.0f; | |
284 s->z22 = 0.0f; | |
285 | |
286 /* State used by function lpc10_analyse */ | |
287 for (i = 0; i < 540; i++) | |
288 { | |
289 s->inbuf[i] = 0.0f; | |
290 s->pebuf[i] = 0.0f; | |
291 } | |
292 for (i = 0; i < 696; i++) | |
293 s->lpbuf[i] = 0.0f; | |
294 for (i = 0; i < 312; i++) | |
295 s->ivbuf[i] = 0.0f; | |
296 s->bias = 0.0f; | |
297 s->osptr = 1; | |
298 for (i = 0; i < 3; i++) | |
299 s->obound[i] = 0; | |
300 s->vwin[2][0] = 307; | |
301 s->vwin[2][1] = 462; | |
302 s->awin[2][0] = 307; | |
303 s->awin[2][1] = 462; | |
304 for (i = 0; i < 4; i++) | |
305 { | |
306 s->voibuf[i][0] = 0; | |
307 s->voibuf[i][1] = 0; | |
308 } | |
309 for (i = 0; i < 3; i++) | |
310 s->rmsbuf[i] = 0.0f; | |
311 for (i = 0; i < 3; i++) | |
312 { | |
313 for (j = 0; j < 10; j++) | |
314 s->rcbuf[i][j] = 0.0f; | |
315 } | |
316 s->zpre = 0.0f; | |
317 | |
318 /* State used by function onset */ | |
319 s->n = 0.0f; | |
320 s->d__ = 1.0f; | |
321 for (i = 0; i < 16; i++) | |
322 s->l2buf[i] = 0.0f; | |
323 s->l2sum1 = 0.0f; | |
324 s->l2ptr1 = 1; | |
325 s->l2ptr2 = 9; | |
326 s->hyst = FALSE; | |
327 | |
328 /* State used by function lpc10_voicing */ | |
329 s->dither = 20.0f; | |
330 s->maxmin = 0.0f; | |
331 for (i = 0; i < 3; i++) | |
332 { | |
333 s->voice[i][0] = 0.0f; | |
334 s->voice[i][1] = 0.0f; | |
335 } | |
336 s->lbve = 3000; | |
337 s->fbve = 3000; | |
338 s->fbue = 187; | |
339 s->ofbue = 187; | |
340 s->sfbue = 187; | |
341 s->lbue = 93; | |
342 s->olbue = 93; | |
343 s->slbue = 93; | |
344 s->snr = (float) (s->fbve / s->fbue << 6); | |
345 | |
346 /* State used by function dynamic_pitch_tracking */ | |
347 for (i = 0; i < 60; i++) | |
348 s->s[i] = 0.0f; | |
349 for (i = 0; i < 2; i++) | |
350 { | |
351 for (j = 0; j < 60; j++) | |
352 s->p[i][j] = 0; | |
353 } | |
354 s->ipoint = 0; | |
355 s->alphax = 0.0f; | |
356 | |
357 /* State used by function lpc10_pack */ | |
358 s->isync = 0; | |
359 | |
360 return s; | |
361 } | |
362 /*- End of function --------------------------------------------------------*/ | |
363 | |
364 int lpc10_encode_release(lpc10_encode_state_t *s) | |
365 { | |
366 free(s); | |
367 return 0; | |
368 } | |
369 /*- End of function --------------------------------------------------------*/ | |
370 | |
371 int lpc10_encode(lpc10_encode_state_t *s, uint8_t code[], const int16_t amp[], int quant) | |
372 { | |
373 int32_t voice[2]; | |
374 int32_t pitch; | |
375 float speech[LPC10_SAMPLES_PER_FRAME]; | |
376 float rc[LPC10_ORDER]; | |
377 float rms; | |
378 lpc10_frame_t frame; | |
379 int i; | |
380 int j; | |
381 | |
382 for (i = 0; i < quant; i++) | |
383 { | |
384 for (j = 0; j < LPC10_SAMPLES_PER_FRAME; j++) | |
385 speech[j] = (float) amp[i*LPC10_SAMPLES_PER_FRAME + j]/32768.0f; | |
386 high_pass_100hz(s, speech, 0, LPC10_SAMPLES_PER_FRAME); | |
387 lpc10_analyse(s, speech, voice, &pitch, &rms, rc); | |
388 encode(s, &frame, voice, pitch, rms, rc); | |
389 lpc10_pack(s, &code[7*i], &frame); | |
390 } | |
391 return quant*7; | |
392 } | |
393 /*- End of function --------------------------------------------------------*/ | |
394 /*- End of file ------------------------------------------------------------*/ |