Properly play a holdtime message if the announce-holdtime option is
[asterisk-bristuff.git] / codecs / lpc10 / encode.c
blobb81799f6be6bcc14241b5333def30fdc34f44a4b
1 /*
3 $Log$
4 Revision 1.15 2004/06/26 03:50:14 markster
5 Merge source cleanups (bug #1911)
7 Revision 1.14 2003/02/12 13:59:15 matteo
8 mer feb 12 14:56:57 CET 2003
10 Revision 1.1.1.1 2003/02/12 13:59:15 matteo
11 mer feb 12 14:56:57 CET 2003
13 Revision 1.2 2000/01/05 08:20:39 markster
14 Some OSS fixes and a few lpc changes to make it actually work
16 * Revision 1.1 1996/08/19 22:32:21 jaf
17 * Initial revision
22 /* -- translated by f2c (version 19951025).
23 You must link the resulting object file with the libraries:
24 -lf2c -lm (in that order)
27 #include "f2c.h"
29 #ifdef P_R_O_T_O_T_Y_P_E_S
30 extern int encode_(integer *voice, integer *pitch, real *rms, real *rc, integer *ipitch, integer *irms, integer *irc);
31 /* comlen contrl_ 12 */
32 #endif
34 /* Common Block Declarations */
36 extern struct {
37 integer order, lframe;
38 logical corrp;
39 } contrl_;
41 #define contrl_1 contrl_
43 /* Table of constant values */
45 static integer c__2 = 2;
47 /* ***************************************************************** */
49 /* ENCODE Version 54 */
51 /* $Log$
52 * Revision 1.15 2004/06/26 03:50:14 markster
53 * Merge source cleanups (bug #1911)
55 * Revision 1.14 2003/02/12 13:59:15 matteo
56 * mer feb 12 14:56:57 CET 2003
58 * Revision 1.1.1.1 2003/02/12 13:59:15 matteo
59 * mer feb 12 14:56:57 CET 2003
61 * Revision 1.2 2000/01/05 08:20:39 markster
62 * Some OSS fixes and a few lpc changes to make it actually work
64 * Revision 1.1 1996/08/19 22:32:21 jaf
65 * Initial revision
66 * */
67 /* Revision 1.5 1996/03/26 19:35:50 jaf */
68 /* Commented out trace statements. */
70 /* Revision 1.4 1996/03/21 00:26:29 jaf */
71 /* Added the comment that this subroutine has no local state. */
73 /* In the last check-in, I forgot to mention that I had added comments */
74 /* explaining which indices of array arguments are read or written. */
76 /* Revision 1.3 1996/03/21 00:22:39 jaf */
77 /* Added comments explaining that all local arrays are effectively */
78 /* constants. */
80 /* Revision 1.2 1996/03/13 18:48:33 jaf */
81 /* Comments added explaining that none of the local variables of this */
82 /* subroutine need to be saved from one invocation to the next. */
84 /* Revision 1.1 1996/02/07 14:45:29 jaf */
85 /* Initial revision */
88 /* ***************************************************************** */
90 /* Quantize LPC parameters for transmission */
92 /* INPUTS: */
93 /* VOICE - Half frame voicing decisions */
94 /* Indices 1 through 2 read. */
95 /* PITCH - Pitch */
96 /* RMS - Energy */
97 /* RC - Reflection coefficients */
98 /* Indices 1 through ORDER read. */
99 /* CORRP - Error Correction: TRUE = yes, FALSE = none */
100 /* (this is defined in file control.fh) */
101 /* OUTPUTS: */
102 /* IPITCH - Coded pitch and voicing */
103 /* IRMS - Quantized energy */
104 /* IRC - Quantized reflection coefficients */
105 /* Indices 1 through MAX(ORDER,2) written. */
106 /* If CORRP is .TRUE., then indices 1 through 10 written */
107 /* for unvoiced frames. */
109 /* This subroutine has no local state. */
111 /* Subroutine */ int encode_(integer *voice, integer *pitch, real *rms, real *
112 rc, integer *ipitch, integer *irms, integer *irc)
114 /* Initialized data */
116 static integer enctab[16] = { 0,7,11,12,13,10,6,1,14,9,5,2,3,4,8,15 };
117 static integer entau[60] = { 19,11,27,25,29,21,23,22,30,14,15,7,39,38,46,
118 42,43,41,45,37,53,49,51,50,54,52,60,56,58,26,90,88,92,84,86,82,83,
119 81,85,69,77,73,75,74,78,70,71,67,99,97,113,112,114,98,106,104,108,
120 100,101,76 };
121 static integer enadd[8] = { 1920,-768,2432,1280,3584,1536,2816,-1152 };
122 static real enscl[8] = { .0204f,.0167f,.0145f,.0147f,.0143f,.0135f,.0125f,
123 .0112f };
124 static integer enbits[8] = { 6,5,4,4,4,4,3,3 };
125 static integer entab6[64] = { 0,0,0,0,0,0,1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,
126 3,3,3,3,3,4,4,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,9,9,
127 9,10,10,11,11,12,13,14,15 };
128 static integer rmst[64] = { 1024,936,856,784,718,656,600,550,502,460,420,
129 384,352,328,294,270,246,226,206,188,172,158,144,132,120,110,102,
130 92,84,78,70,64,60,54,50,46,42,38,34,32,30,26,24,22,20,18,17,16,15,
131 14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 };
133 /* System generated locals */
134 integer i__1, i__2;
136 /* Builtin functions */
137 integer pow_ii(integer *, integer *);
139 /* Local variables */
140 integer idel, nbit, i__, j, i2, i3, mrk;
142 /* $Log$
143 * Revision 1.15 2004/06/26 03:50:14 markster
144 * Merge source cleanups (bug #1911)
146 * Revision 1.14 2003/02/12 13:59:15 matteo
147 * mer feb 12 14:56:57 CET 2003
149 * Revision 1.1.1.1 2003/02/12 13:59:15 matteo
150 * mer feb 12 14:56:57 CET 2003
152 * Revision 1.2 2000/01/05 08:20:39 markster
153 * Some OSS fixes and a few lpc changes to make it actually work
155 * Revision 1.1 1996/08/19 22:32:21 jaf
156 * Initial revision
157 * */
158 /* Revision 1.3 1996/03/29 22:03:47 jaf */
159 /* Removed definitions for any constants that were no longer used. */
161 /* Revision 1.2 1996/03/26 19:34:33 jaf */
162 /* Added comments indicating which constants are not needed in an */
163 /* application that uses the LPC-10 coder. */
165 /* Revision 1.1 1996/02/07 14:43:51 jaf */
166 /* Initial revision */
168 /* LPC Configuration parameters: */
169 /* Frame size, Prediction order, Pitch period */
170 /* Arguments */
171 /* $Log$
172 * Revision 1.15 2004/06/26 03:50:14 markster
173 * Merge source cleanups (bug #1911)
175 * Revision 1.14 2003/02/12 13:59:15 matteo
176 * mer feb 12 14:56:57 CET 2003
178 * Revision 1.1.1.1 2003/02/12 13:59:15 matteo
179 * mer feb 12 14:56:57 CET 2003
181 * Revision 1.2 2000/01/05 08:20:39 markster
182 * Some OSS fixes and a few lpc changes to make it actually work
184 * Revision 1.1 1996/08/19 22:32:21 jaf
185 * Initial revision
186 * */
187 /* Revision 1.3 1996/03/29 22:05:55 jaf */
188 /* Commented out the common block variables that are not needed by the */
189 /* embedded version. */
191 /* Revision 1.2 1996/03/26 19:34:50 jaf */
192 /* Added comments indicating which constants are not needed in an */
193 /* application that uses the LPC-10 coder. */
195 /* Revision 1.1 1996/02/07 14:44:09 jaf */
196 /* Initial revision */
198 /* LPC Processing control variables: */
200 /* *** Read-only: initialized in setup */
202 /* Files for Speech, Parameter, and Bitstream Input & Output, */
203 /* and message and debug outputs. */
205 /* Here are the only files which use these variables: */
207 /* lpcsim.f setup.f trans.f error.f vqsetup.f */
209 /* Many files which use fdebug are not listed, since it is only used in */
210 /* those other files conditionally, to print trace statements. */
211 /* integer fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
212 /* LPC order, Frame size, Quantization rate, Bits per frame, */
213 /* Error correction */
214 /* Subroutine SETUP is the only place where order is assigned a value, */
215 /* and that value is 10. It could increase efficiency 1% or so to */
216 /* declare order as a constant (i.e., a Fortran PARAMETER) instead of as
218 /* a variable in a COMMON block, since it is used in many places in the */
219 /* core of the coding and decoding routines. Actually, I take that back.
221 /* At least when compiling with f2c, the upper bound of DO loops is */
222 /* stored in a local variable before the DO loop begins, and then that is
224 /* compared against on each iteration. */
225 /* Similarly for lframe, which is given a value of MAXFRM in SETUP. */
226 /* Similarly for quant, which is given a value of 2400 in SETUP. quant */
227 /* is used in only a few places, and never in the core coding and */
228 /* decoding routines, so it could be eliminated entirely. */
229 /* nbits is similar to quant, and is given a value of 54 in SETUP. */
230 /* corrp is given a value of .TRUE. in SETUP, and is only used in the */
231 /* subroutines ENCODE and DECODE. It doesn't affect the speed of the */
232 /* coder significantly whether it is .TRUE. or .FALSE., or whether it is
234 /* a constant or a variable, since it is only examined once per frame. */
235 /* Leaving it as a variable that is set to .TRUE. seems like a good */
236 /* idea, since it does enable some error-correction capability for */
237 /* unvoiced frames, with no change in the coding rate, and no noticeable
239 /* quality difference in the decoded speech. */
240 /* integer quant, nbits */
241 /* *** Read/write: variables for debugging, not needed for LPC algorithm
244 /* Current frame, Unstable frames, Output clip count, Max onset buffer,
246 /* Debug listing detail level, Line count on listing page */
248 /* nframe is not needed for an embedded LPC10 at all. */
249 /* nunsfm is initialized to 0 in SETUP, and incremented in subroutine */
250 /* ERROR, which is only called from RCCHK. When LPC10 is embedded into */
251 /* an application, I would recommend removing the call to ERROR in RCCHK,
253 /* and remove ERROR and nunsfm completely. */
254 /* iclip is initialized to 0 in SETUP, and incremented in entry SWRITE in
256 /* sread.f. When LPC10 is embedded into an application, one might want */
257 /* to cause it to be incremented in a routine that takes the output of */
258 /* SYNTHS and sends it to an audio device. It could be optionally */
259 /* displayed, for those that might want to know what it is. */
260 /* maxosp is never initialized to 0 in SETUP, although it probably should
262 /* be, and it is updated in subroutine ANALYS. I doubt that its value */
263 /* would be of much interest to an application in which LPC10 is */
264 /* embedded. */
265 /* listl and lincnt are not needed for an embedded LPC10 at all. */
266 /* integer nframe, nunsfm, iclip, maxosp, listl, lincnt */
267 /* common /contrl/ fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
268 /* common /contrl/ quant, nbits */
269 /* common /contrl/ nframe, nunsfm, iclip, maxosp, listl, lincnt */
270 /* Parameters/constants */
271 /* These arrays are not Fortran PARAMETER's, but they are defined */
272 /* by DATA statements below, and their contents are never altered.
274 /* Local variables that need not be saved */
275 /* Parameter adjustments */
276 --irc;
277 --rc;
278 --voice;
280 /* Function Body */
281 /* Scale RMS and RC's to integers */
282 *irms = (integer)*rms;
283 i__1 = contrl_1.order;
284 for (i__ = 1; i__ <= i__1; ++i__) {
285 irc[i__] = (integer)(rc[i__] * 32768.f);
287 /* IF(LISTL.GE.3)WRITE(FDEBUG,800)VOICE,PITCH,IRMS,(IRC(I),I=1,ORDER) */
288 /* 800 FORMAT(1X,/,' <<ENCODE IN>>',T32,2I3,I6,I5,T50,10I8) */
289 /* Encode pitch and voicing */
290 if (voice[1] != 0 && voice[2] != 0) {
291 *ipitch = entau[*pitch - 1];
292 } else {
293 if (contrl_1.corrp) {
294 *ipitch = 0;
295 if (voice[1] != voice[2]) {
296 *ipitch = 127;
298 } else {
299 *ipitch = (voice[1] << 1) + voice[2];
302 /* Encode RMS by binary table search */
303 j = 32;
304 idel = 16;
305 *irms = min(*irms,1023);
306 while(idel > 0) {
307 if (*irms > rmst[j - 1]) {
308 j -= idel;
310 if (*irms < rmst[j - 1]) {
311 j += idel;
313 idel /= 2;
315 if (*irms > rmst[j - 1]) {
316 --j;
318 *irms = 31 - j / 2;
319 /* Encode RC(1) and (2) as log-area-ratios */
320 for (i__ = 1; i__ <= 2; ++i__) {
321 i2 = irc[i__];
322 mrk = 0;
323 if (i2 < 0) {
324 i2 = -i2;
325 mrk = 1;
327 i2 /= 512;
328 i2 = min(i2,63);
329 i2 = entab6[i2];
330 if (mrk != 0) {
331 i2 = -i2;
333 irc[i__] = i2;
335 /* Encode RC(3) - (10) linearly, remove bias then scale */
336 i__1 = contrl_1.order;
337 for (i__ = 3; i__ <= i__1; ++i__) {
338 i2 = irc[i__] / 2;
339 i2 = (integer)((i2 + enadd[contrl_1.order + 1 - i__ - 1]) * enscl[
340 contrl_1.order + 1 - i__ - 1]);
341 /* Computing MIN */
342 i__2 = max(i2,-127);
343 i2 = min(i__2,127);
344 nbit = enbits[contrl_1.order + 1 - i__ - 1];
345 i3 = 0;
346 if (i2 < 0) {
347 i3 = -1;
349 i2 /= pow_ii(&c__2, &nbit);
350 if (i3 == -1) {
351 --i2;
353 irc[i__] = i2;
355 /* Protect the most significant bits of the most */
356 /* important parameters during non-voiced frames. */
357 /* RC(1) - RC(4) are protected using 20 parity bits */
358 /* replacing RC(5) - RC(10). */
359 if (contrl_1.corrp) {
360 if (*ipitch == 0 || *ipitch == 127) {
361 irc[5] = enctab[(irc[1] & 30) / 2];
362 irc[6] = enctab[(irc[2] & 30) / 2];
363 irc[7] = enctab[(irc[3] & 30) / 2];
364 irc[8] = enctab[(*irms & 30) / 2];
365 irc[9] = enctab[(irc[4] & 30) / 2] / 2;
366 irc[10] = enctab[(irc[4] & 30) / 2] & 1;
369 /* IF(LISTL.GE.3)WRITE(FDEBUG,801)VOICE,IPITCH,IRMS,(IRC(J),J=1,ORDER) */
370 /* 801 FORMAT(1X,'<<ENCODE OUT>>',T32,2I3,I6,I5,T50,10I8) */
371 return 0;
372 } /* encode_ */