4 Revision 1.16 2004/06/26 03:50:14 markster
5 Merge source cleanups (bug #1911)
7 Revision 1.15 2003/09/19 01:20:22 markster
8 Code cleanups (bug #66)
10 Revision 1.2 2003/09/19 01:20:22 markster
11 Code cleanups (bug #66)
13 Revision 1.1.1.1 2003/02/12 13:59:14 matteo
14 mer feb 12 14:56:57 CET 2003
16 Revision 1.2 2000/01/05 08:20:39 markster
17 Some OSS fixes and a few lpc changes to make it actually work
19 * Revision 1.2 1996/08/20 20:22:39 jaf
20 * Removed all static local variables that were SAVE'd in the Fortran
21 * code, and put them in struct lpc10_decoder_state that is passed as an
24 * Removed init function, since all initialization is now done in
25 * init_lpc10_decoder_state().
27 * Revision 1.1 1996/08/19 22:32:38 jaf
33 /* -- translated by f2c (version 19951025).
34 You must link the resulting object file with the libraries:
35 -lf2c -lm (in that order)
40 #ifdef P_R_O_T_O_T_Y_P_E_S
41 extern int decode_(integer
*ipitv
, integer
*irms
, integer
*irc
, integer
*voice
, integer
*pitch
, real
*rms
, real
*rc
, struct lpc10_decoder_state
*st
);
42 /* comlen contrl_ 12 */
43 /*:ref: ham84_ 14 3 4 4 4 */
44 /*:ref: median_ 4 3 4 4 4 */
47 /* Common Block Declarations */
50 integer order
, lframe
;
54 #define contrl_1 contrl_
56 /* Table of constant values */
58 static integer c__2
= 2;
60 /* ***************************************************************** */
62 /* DECODE Version 54 */
65 * Revision 1.16 2004/06/26 03:50:14 markster
66 * Merge source cleanups (bug #1911)
68 * Revision 1.15 2003/09/19 01:20:22 markster
69 * Code cleanups (bug #66)
71 * Revision 1.2 2003/09/19 01:20:22 markster
72 * Code cleanups (bug #66)
74 * Revision 1.1.1.1 2003/02/12 13:59:14 matteo
75 * mer feb 12 14:56:57 CET 2003
77 * Revision 1.2 2000/01/05 08:20:39 markster
78 * Some OSS fixes and a few lpc changes to make it actually work
80 * Revision 1.2 1996/08/20 20:22:39 jaf
81 * Removed all static local variables that were SAVE'd in the Fortran
82 * code, and put them in struct lpc10_decoder_state that is passed as an
85 * Removed init function, since all initialization is now done in
86 * init_lpc10_decoder_state().
88 * Revision 1.1 1996/08/19 22:32:38 jaf
91 /* Revision 1.5 1996/05/23 20:06:03 jaf */
92 /* Assigned PITCH a "default" value on the first call, since otherwise it */
93 /* would be left uninitialized. */
95 /* Revision 1.4 1996/03/26 19:35:18 jaf */
96 /* Commented out trace statements. */
98 /* Revision 1.3 1996/03/21 21:10:50 jaf */
99 /* Added entry INITDECODE to reinitialize the local state of subroutine */
102 /* Revision 1.2 1996/03/21 21:04:50 jaf */
103 /* Determined which local variables should be saved from one invocation */
104 /* to the next, and guessed initial values for some that should have been */
105 /* saved, but weren't given initial values. Many of the arrays are */
106 /* "constants", and many local variables are only used if the "global" */
107 /* variable CORRP is .TRUE. */
109 /* Added comments explaining which indices of array arguments are read or */
112 /* Revision 1.1 1996/02/12 03:21:10 jaf */
113 /* Initial revision */
116 /* ***************************************************************** */
118 /* This subroutine provides error correction and decoding */
119 /* for all LPC parameters */
122 /* IPITV - Index value of pitch */
123 /* IRMS - Coded Energy */
124 /* CORRP - Error correction: */
125 /* If FALSE, parameters are decoded directly with no delay. If TRUE, */
126 /* most important parameter bits are protected by Hamming code and */
127 /* median smoothed. This requires an additional frame of delay. */
129 /* IRC - Coded Reflection Coefficients */
130 /* Indices 1 through ORDER always read, then written. */
132 /* VOICE - Half frame voicing decisions */
133 /* Indices 1 through 2 written. */
134 /* PITCH - Decoded pitch */
136 /* RC - Reflection coefficients */
137 /* Indices 1 through ORDER written. */
139 /* NOTE: Zero RC's should be done more directly, but this would affect */
140 /* coded parameter printout. */
142 /* This subroutine maintains local state from one call to the next. If */
143 /* you want to switch to using a new audio stream for this filter, or */
144 /* reinitialize its state for any other reason, call the ENTRY */
147 /* Subroutine */ int decode_(integer
*ipitv
, integer
*irms
,
148 integer
*irc
, integer
*voice
, integer
*pitch
, real
*rms
, real
*rc
,
149 struct lpc10_decoder_state
*st
)
151 /* Initialized data */
154 static integer ethrs
= 2048;
155 static integer ethrs1
= 128;
156 static integer ethrs2
= 1024;
157 static integer ethrs3
= 2048;
158 static integer ivtab
[32] = { 24960,24960,24960,24960,25480,25480,25483,
159 25480,16640,1560,1560,1560,16640,1816,1563,1560,24960,24960,24859,
160 24856,26001,25881,25915,25913,1560,1560,7800,3640,1561,1561,3643,
162 static real corth
[32] /* was [4][8] */ = { 32767.f
,10.f
,5.f
,0.f
,
163 32767.f
,8.f
,4.f
,0.f
,32.f
,6.4f
,3.2f
,0.f
,32.f
,6.4f
,3.2f
,0.f
,32.f
,
164 11.2f
,6.4f
,0.f
,32.f
,11.2f
,6.4f
,0.f
,16.f
,5.6f
,3.2f
,0.f
,16.f
,5.6f
,
166 static integer detau
[128] = { 0,0,0,3,0,3,3,31,0,3,3,21,3,3,29,30,0,3,3,
167 20,3,25,27,26,3,23,58,22,3,24,28,3,0,3,3,3,3,39,33,32,3,37,35,36,
168 3,38,34,3,3,42,46,44,50,40,48,3,54,3,56,3,52,3,3,1,0,3,3,108,3,78,
169 100,104,3,84,92,88,156,80,96,3,3,74,70,72,66,76,68,3,62,3,60,3,64,
170 3,3,1,3,116,132,112,148,152,3,3,140,3,136,3,144,3,3,1,124,120,128,
171 3,3,3,3,1,3,3,3,1,3,1,1,1 };
172 static integer rmst
[64] = { 1024,936,856,784,718,656,600,550,502,460,420,
173 384,352,328,294,270,246,226,206,188,172,158,144,132,120,110,102,
174 92,84,78,70,64,60,54,50,46,42,38,34,32,30,26,24,22,20,18,17,16,15,
175 14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 };
176 static integer detab7
[32] = { 4,11,18,25,32,39,46,53,60,66,72,77,82,87,92,
177 96,101,104,108,111,114,115,117,119,121,122,123,124,125,126,127,
179 static real descl
[8] = { .6953f
,.625f
,.5781f
,.5469f
,.5312f
,.5391f
,.4688f
,
182 static integer deadd
[8] = { 1152,-2816,-1536,-3584,-1280,-2432,768,-1920 }
184 static integer qb
[8] = { 511,511,1023,1023,1023,1023,2047,4095 };
185 static integer nbit
[10] = { 8,8,5,5,4,4,4,4,3,2 };
186 static integer zrc
[10] = { 0,0,0,0,0,3,0,2,0,0 };
187 static integer bit
[5] = { 2,4,8,16,32 };
196 /* System generated locals */
199 /* Builtin functions */
200 integer
pow_ii(integer
*, integer
*);
202 /* Local variables */
203 extern /* Subroutine */ int ham84_(integer
*, integer
*, integer
*);
204 integer ipit
, iout
, i__
, icorf
, index
, ivoic
, ixcor
, i1
, i2
, i4
;
205 extern integer
median_(integer
*, integer
*, integer
*);
206 integer ishift
, errcnt
, lsb
;
209 * Revision 1.16 2004/06/26 03:50:14 markster
210 * Merge source cleanups (bug #1911)
212 * Revision 1.15 2003/09/19 01:20:22 markster
213 * Code cleanups (bug #66)
215 * Revision 1.2 2003/09/19 01:20:22 markster
216 * Code cleanups (bug #66)
218 * Revision 1.1.1.1 2003/02/12 13:59:14 matteo
219 * mer feb 12 14:56:57 CET 2003
221 * Revision 1.2 2000/01/05 08:20:39 markster
222 * Some OSS fixes and a few lpc changes to make it actually work
224 * Revision 1.2 1996/08/20 20:22:39 jaf
225 * Removed all static local variables that were SAVE'd in the Fortran
226 * code, and put them in struct lpc10_decoder_state that is passed as an
229 * Removed init function, since all initialization is now done in
230 * init_lpc10_decoder_state().
232 * Revision 1.1 1996/08/19 22:32:38 jaf
235 /* Revision 1.3 1996/03/29 22:03:47 jaf */
236 /* Removed definitions for any constants that were no longer used. */
238 /* Revision 1.2 1996/03/26 19:34:33 jaf */
239 /* Added comments indicating which constants are not needed in an */
240 /* application that uses the LPC-10 coder. */
242 /* Revision 1.1 1996/02/07 14:43:51 jaf */
243 /* Initial revision */
245 /* LPC Configuration parameters: */
246 /* Frame size, Prediction order, Pitch period */
249 * Revision 1.16 2004/06/26 03:50:14 markster
250 * Merge source cleanups (bug #1911)
252 * Revision 1.15 2003/09/19 01:20:22 markster
253 * Code cleanups (bug #66)
255 * Revision 1.2 2003/09/19 01:20:22 markster
256 * Code cleanups (bug #66)
258 * Revision 1.1.1.1 2003/02/12 13:59:14 matteo
259 * mer feb 12 14:56:57 CET 2003
261 * Revision 1.2 2000/01/05 08:20:39 markster
262 * Some OSS fixes and a few lpc changes to make it actually work
264 * Revision 1.2 1996/08/20 20:22:39 jaf
265 * Removed all static local variables that were SAVE'd in the Fortran
266 * code, and put them in struct lpc10_decoder_state that is passed as an
269 * Removed init function, since all initialization is now done in
270 * init_lpc10_decoder_state().
272 * Revision 1.1 1996/08/19 22:32:38 jaf
275 /* Revision 1.3 1996/03/29 22:05:55 jaf */
276 /* Commented out the common block variables that are not needed by the */
277 /* embedded version. */
279 /* Revision 1.2 1996/03/26 19:34:50 jaf */
280 /* Added comments indicating which constants are not needed in an */
281 /* application that uses the LPC-10 coder. */
283 /* Revision 1.1 1996/02/07 14:44:09 jaf */
284 /* Initial revision */
286 /* LPC Processing control variables: */
288 /* *** Read-only: initialized in setup */
290 /* Files for Speech, Parameter, and Bitstream Input & Output, */
291 /* and message and debug outputs. */
293 /* Here are the only files which use these variables: */
295 /* lpcsim.f setup.f trans.f error.f vqsetup.f */
297 /* Many files which use fdebug are not listed, since it is only used in */
298 /* those other files conditionally, to print trace statements. */
299 /* integer fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
300 /* LPC order, Frame size, Quantization rate, Bits per frame, */
301 /* Error correction */
302 /* Subroutine SETUP is the only place where order is assigned a value, */
303 /* and that value is 10. It could increase efficiency 1% or so to */
304 /* declare order as a constant (i.e., a Fortran PARAMETER) instead of as
306 /* a variable in a COMMON block, since it is used in many places in the */
307 /* core of the coding and decoding routines. Actually, I take that back.
309 /* At least when compiling with f2c, the upper bound of DO loops is */
310 /* stored in a local variable before the DO loop begins, and then that is
312 /* compared against on each iteration. */
313 /* Similarly for lframe, which is given a value of MAXFRM in SETUP. */
314 /* Similarly for quant, which is given a value of 2400 in SETUP. quant */
315 /* is used in only a few places, and never in the core coding and */
316 /* decoding routines, so it could be eliminated entirely. */
317 /* nbits is similar to quant, and is given a value of 54 in SETUP. */
318 /* corrp is given a value of .TRUE. in SETUP, and is only used in the */
319 /* subroutines ENCODE and DECODE. It doesn't affect the speed of the */
320 /* coder significantly whether it is .TRUE. or .FALSE., or whether it is
322 /* a constant or a variable, since it is only examined once per frame. */
323 /* Leaving it as a variable that is set to .TRUE. seems like a good */
324 /* idea, since it does enable some error-correction capability for */
325 /* unvoiced frames, with no change in the coding rate, and no noticeable
327 /* quality difference in the decoded speech. */
328 /* integer quant, nbits */
329 /* *** Read/write: variables for debugging, not needed for LPC algorithm
332 /* Current frame, Unstable frames, Output clip count, Max onset buffer,
334 /* Debug listing detail level, Line count on listing page */
336 /* nframe is not needed for an embedded LPC10 at all. */
337 /* nunsfm is initialized to 0 in SETUP, and incremented in subroutine */
338 /* ERROR, which is only called from RCCHK. When LPC10 is embedded into */
339 /* an application, I would recommend removing the call to ERROR in RCCHK,
341 /* and remove ERROR and nunsfm completely. */
342 /* iclip is initialized to 0 in SETUP, and incremented in entry SWRITE in
344 /* sread.f. When LPC10 is embedded into an application, one might want */
345 /* to cause it to be incremented in a routine that takes the output of */
346 /* SYNTHS and sends it to an audio device. It could be optionally */
347 /* displayed, for those that might want to know what it is. */
348 /* maxosp is never initialized to 0 in SETUP, although it probably should
350 /* be, and it is updated in subroutine ANALYS. I doubt that its value */
351 /* would be of much interest to an application in which LPC10 is */
353 /* listl and lincnt are not needed for an embedded LPC10 at all. */
354 /* integer nframe, nunsfm, iclip, maxosp, listl, lincnt */
355 /* common /contrl/ fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
356 /* common /contrl/ quant, nbits */
357 /* common /contrl/ nframe, nunsfm, iclip, maxosp, listl, lincnt */
358 /* Function return value definitions */
360 /* Parameters/constants */
362 /* The variables below that are not Fortran PARAMETER's are */
363 /* initialized with DATA statements, and then never modified. */
364 /* The following are used regardless of CORRP's value. */
366 /* DETAU, NBIT, QB, DEADD, DETAB7, RMST, DESCL */
368 /* The following are used only if CORRP is .TRUE. */
370 /* ETHRS, ETHRS1, ETHRS2, ETHRS3, IVTAB, BIT, CORTH, ZRC */
372 /* Local variables that need not be saved */
374 /* The following are used regardless of CORRP's value */
375 /* The following are used only if CORRP is .TRUE. */
379 /* The following are used regardless of CORRP's value */
380 /* The following are used only if CORRP is .TRUE. */
381 /* I am guessing the initial values for IVP2H, IOVOIC, DRC, DPIT, */
382 /* and DRMS. They should be checked to see if they are reasonable.
384 /* I'm also guessing for ERATE, but I think 0 is the right initial
387 /* Parameter adjustments */
400 iptold
= &(st
->iptold
);
401 first
= &(st
->first
);
402 ivp2h
= &(st
->ivp2h
);
403 iovoic
= &(st
->iovoic
);
404 iavgp
= &(st
->iavgp
);
405 erate
= &(st
->erate
);
407 dpit
= &(st
->dpit
[0]);
408 drms
= &(st
->drms
[0]);
410 /* DATA statements for "constants" defined above. */
411 /* IF (LISTL.GE.3) WRITE(FDEBUG,800) IPITV,IRMS,(IRC(J),J=1,ORDER) */
412 /* 800 FORMAT(1X,' <<ERRCOR IN>>',T32,6X,I6,I5,T50,10I8) */
413 /* If no error correction, do pitch and voicing then jump to decode */
415 if (! contrl_1
.corrp
) {
421 if (*ipitv
== 0 || *ipitv
== 2) {
428 if (voice
[1] == 1 && voice
[2] == 1) {
431 if (voice
[1] != voice
[2]) {
436 /* Do error correction pitch and voicing */
440 *iavgp
= (*iavgp
* 15 + i4
+ 8) / 16;
446 i__1
= contrl_1
.order
;
447 for (i__
= 1; i__
<= i__1
; ++i__
) {
448 drc
[i__
* 3 - 3] = irc
[i__
];
450 /* Determine index to IVTAB from V/UV decision */
451 /* If error rate is high then use alternate table */
452 index
= (*ivp2h
<< 4) + (*iovoic
<< 2) + ivoic
+ 1;
453 i1
= ivtab
[index
- 1];
456 if (*erate
< ethrs
) {
459 /* Determine error rate: 4=high 1=low */
461 if (*erate
< ethrs3
) {
464 if (*erate
< ethrs2
) {
467 if (*erate
< ethrs1
) {
470 /* Voice/unvoice decision determined from bits 0 and 1 of IVTAB */
471 voice
[1] = icorf
/ 2 & 1;
472 voice
[2] = icorf
& 1;
473 /* Skip decoding on first frame because present data not yet available */
476 /* Assign PITCH a "default" value on the first call, since */
477 /* otherwise it would be left uninitialized. The two lines
479 /* below were copied from above, since it seemed like a */
480 /* reasonable thing to do for the first call. */
487 /* If bit 4 of ICORF is set then correct RMS and RC(1) - RC(4). */
488 /* Determine error rate and correct errors using a Hamming 8,4 code */
489 /* during transition or unvoiced frame. If IOUT is negative, */
490 /* more than 1 error occurred, use previous frame's parameters. */
491 if ((icorf
& bit
[3]) != 0) {
494 index
= (drc
[22] << 4) + drms
[1] / 2;
495 ham84_(&index
, &iout
, &errcnt
);
498 drms
[1] = (iout
<< 1) + lsb
;
500 for (i__
= 1; i__
<= 4; ++i__
) {
502 i1
= ((drc
[25] & 7) << 1) + (drc
[28] & 1);
504 i1
= drc
[(9 - i__
) * 3 - 2] & 15;
506 i2
= drc
[(5 - i__
) * 3 - 2] & 31;
508 index
= (i1
<< 4) + i2
/ 2;
509 ham84_(&index
, &iout
, &errcnt
);
511 iout
= (iout
<< 1) + lsb
;
512 if ((iout
& 16) == 16) {
516 iout
= drc
[(5 - i__
) * 3 - 1];
518 drc
[(5 - i__
) * 3 - 2] = iout
;
520 /* Determine error rate */
521 *erate
= (integer
)(*erate
* .96875f
+ errcnt
* 102);
523 /* Get unsmoothed RMS, RC's, and PITCH */
525 i__1
= contrl_1
.order
;
526 for (i__
= 1; i__
<= i__1
; ++i__
) {
527 irc
[i__
] = drc
[i__
* 3 - 2];
536 /* If bit 2 of ICORF is set then smooth RMS and RC's, */
537 if ((icorf
& bit
[1]) != 0) {
538 if ((i__1
= drms
[1] - drms
[0], (real
) abs(i__1
)) >= corth
[ixcor
+ 3]
539 && (i__2
= drms
[1] - drms
[2], (real
) abs(i__2
)) >= corth
[
541 *irms
= median_(&drms
[2], &drms
[1], drms
);
543 for (i__
= 1; i__
<= 6; ++i__
) {
544 if ((i__1
= drc
[i__
* 3 - 2] - drc
[i__
* 3 - 3], (real
) abs(i__1
))
545 >= corth
[ixcor
+ ((i__
+ 2) << 2) - 5] && (i__2
= drc
[i__
*
546 3 - 2] - drc
[i__
* 3 - 1], (real
) abs(i__2
)) >= corth
[
547 ixcor
+ ((i__
+ 2) << 2) - 5]) {
548 irc
[i__
] = median_(&drc
[i__
* 3 - 1], &drc
[i__
* 3 - 2], &drc
[
553 /* If bit 3 of ICORF is set then smooth pitch */
554 if ((icorf
& bit
[2]) != 0) {
555 if ((i__1
= dpit
[1] - dpit
[0], (real
) abs(i__1
)) >= corth
[ixcor
- 1]
556 && (i__2
= dpit
[1] - dpit
[2], (real
) abs(i__2
)) >= corth
[
558 *pitch
= median_(&dpit
[2], &dpit
[1], dpit
);
561 /* If bit 5 of ICORF is set then RC(5) - RC(10) are loaded with */
562 /* values so that after quantization bias is removed in decode */
563 /* the values will be zero. */
565 if ((icorf
& bit
[4]) != 0) {
566 i__1
= contrl_1
.order
;
567 for (i__
= 5; i__
<= i__1
; ++i__
) {
568 irc
[i__
] = zrc
[i__
- 1];
571 /* House keeping - one frame delay */
578 i__1
= contrl_1
.order
;
579 for (i__
= 1; i__
<= i__1
; ++i__
) {
580 drc
[i__
* 3 - 1] = drc
[i__
* 3 - 2];
581 drc
[i__
* 3 - 2] = drc
[i__
* 3 - 3];
584 /* IF (LISTL.GE.3)WRITE(FDEBUG,801)VOICE,PITCH,IRMS,(IRC(J),J=1,ORDER) */
585 /* 801 FORMAT(1X,'<<ERRCOR OUT>>',T32,2I3,I6,I5,T50,10I8) */
587 *irms
= rmst
[(31 - *irms
) * 2];
588 /* Decode RC(1) and RC(2) from log-area-ratios */
589 /* Protect from illegal coded value (-16) caused by bit errors */
590 for (i__
= 1; i__
<= 2; ++i__
) {
604 ishift
= 15 - nbit
[i__
- 1];
605 irc
[i__
] = i2
* pow_ii(&c__2
, &ishift
);
607 /* Decode RC(3)-RC(10) to sign plus 14 bits */
608 i__1
= contrl_1
.order
;
609 for (i__
= 3; i__
<= i__1
; ++i__
) {
611 ishift
= 15 - nbit
[i__
- 1];
612 i2
*= pow_ii(&c__2
, &ishift
);
614 irc
[i__
] = (integer
)(i2
* descl
[i__
- 3] + deadd
[i__
- 3]);
616 /* IF (LISTL.GE.3) WRITE(FDEBUG,811) IRMS, (IRC(I),I=1,ORDER) */
617 /* 811 FORMAT(1X,'<<DECODE OUT>>',T45,I4,1X,10I8) */
618 /* Scale RMS and RC's to reals */
619 *rms
= (real
) (*irms
);
620 i__1
= contrl_1
.order
;
621 for (i__
= 1; i__
<= i__1
; ++i__
) {
622 rc
[i__
] = irc
[i__
] / 16384.f
;