wav: prettify wav_read_fmt() function
[sox.git] / lpc10 / decode.c
blob2007300eb867205c1965373896d66e5d08b01aeb
1 /*
3 * Revision 1.2 1996/08/20 20:22:39 jaf
4 * Removed all static local variables that were SAVE'd in the Fortran
5 * code, and put them in struct lpc10_decoder_state that is passed as an
6 * argument.
8 * Removed init function, since all initialization is now done in
9 * init_lpc10_decoder_state().
11 * Revision 1.1 1996/08/19 22:32:38 jaf
12 * Initial revision
17 /* -- translated by f2c (version 19951025).
18 You must link the resulting object file with the libraries:
19 -lf2c -lm (in that order)
22 #include "f2c.h"
24 extern int decode_(integer *ipitv, integer *irms, integer *irc, integer *voice, integer *pitch, real *rms, real *rc, struct lpc10_decoder_state *st);
26 /* Common Block Declarations */
28 extern struct {
29 integer order, lframe;
30 logical corrp;
31 } contrl_;
33 #define contrl_1 contrl_
35 /* Table of constant values */
37 static integer c__2 = 2;
39 /* ***************************************************************** */
41 /* DECODE Version 54 */
44 * Revision 1.2 1996/08/20 20:22:39 jaf
45 * Removed all static local variables that were SAVE'd in the Fortran
46 * code, and put them in struct lpc10_decoder_state that is passed as an
47 * argument.
49 * Removed init function, since all initialization is now done in
50 * init_lpc10_decoder_state().
52 * Revision 1.1 1996/08/19 22:32:38 jaf
53 * Initial revision
54 * */
55 /* Revision 1.5 1996/05/23 20:06:03 jaf */
56 /* Assigned PITCH a "default" value on the first call, since otherwise it */
57 /* would be left uninitialized. */
59 /* Revision 1.4 1996/03/26 19:35:18 jaf */
60 /* Commented out trace statements. */
62 /* Revision 1.3 1996/03/21 21:10:50 jaf */
63 /* Added entry INITDECODE to reinitialize the local state of subroutine */
64 /* DECODE. */
66 /* Revision 1.2 1996/03/21 21:04:50 jaf */
67 /* Determined which local variables should be saved from one invocation */
68 /* to the next, and guessed initial values for some that should have been */
69 /* saved, but weren't given initial values. Many of the arrays are */
70 /* "constants", and many local variables are only used if the "global" */
71 /* variable CORRP is .TRUE. */
73 /* Added comments explaining which indices of array arguments are read or */
74 /* written. */
76 /* Revision 1.1 1996/02/12 03:21:10 jaf */
77 /* Initial revision */
80 /* ***************************************************************** */
82 /* This subroutine provides error correction and decoding */
83 /* for all LPC parameters */
85 /* Input: */
86 /* IPITV - Index value of pitch */
87 /* IRMS - Coded Energy */
88 /* CORRP - Error correction: */
89 /* If FALSE, parameters are decoded directly with no delay. If TRUE, */
90 /* most important parameter bits are protected by Hamming code and */
91 /* median smoothed. This requires an additional frame of delay. */
92 /* Input/Output: */
93 /* IRC - Coded Reflection Coefficients */
94 /* Indices 1 through ORDER always read, then written. */
95 /* Output: */
96 /* VOICE - Half frame voicing decisions */
97 /* Indices 1 through 2 written. */
98 /* PITCH - Decoded pitch */
99 /* RMS - Energy */
100 /* RC - Reflection coefficients */
101 /* Indices 1 through ORDER written. */
103 /* NOTE: Zero RC's should be done more directly, but this would affect */
104 /* coded parameter printout. */
106 /* This subroutine maintains local state from one call to the next. If */
107 /* you want to switch to using a new audio stream for this filter, or */
108 /* reinitialize its state for any other reason, call the ENTRY */
109 /* INITDECODE. */
111 /* Subroutine */ int decode_(integer *ipitv, integer *irms,
112 integer *irc, integer *voice, integer *pitch, real *rms, real *rc,
113 struct lpc10_decoder_state *st)
115 /* Initialized data */
117 logical *first;
118 static integer ethrs = 2048;
119 static integer ethrs1 = 128;
120 static integer ethrs2 = 1024;
121 static integer ethrs3 = 2048;
122 static integer ivtab[32] = { 24960,24960,24960,24960,25480,25480,25483,
123 25480,16640,1560,1560,1560,16640,1816,1563,1560,24960,24960,24859,
124 24856,26001,25881,25915,25913,1560,1560,7800,3640,1561,1561,3643,
125 3641 };
126 static real corth[32] /* was [4][8] */ = { 32767.f,10.f,5.f,0.f,
127 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,
128 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,
129 3.2f,0.f };
130 static integer detau[128] = { 0,0,0,3,0,3,3,31,0,3,3,21,3,3,29,30,0,3,3,
131 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,
132 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,
133 100,104,3,84,92,88,156,80,96,3,3,74,70,72,66,76,68,3,62,3,60,3,64,
134 3,3,1,3,116,132,112,148,152,3,3,140,3,136,3,144,3,3,1,124,120,128,
135 3,3,3,3,1,3,3,3,1,3,1,1,1 };
136 static integer rmst[64] = { 1024,936,856,784,718,656,600,550,502,460,420,
137 384,352,328,294,270,246,226,206,188,172,158,144,132,120,110,102,
138 92,84,78,70,64,60,54,50,46,42,38,34,32,30,26,24,22,20,18,17,16,15,
139 14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 };
140 static integer detab7[32] = { 4,11,18,25,32,39,46,53,60,66,72,77,82,87,92,
141 96,101,104,108,111,114,115,117,119,121,122,123,124,125,126,127,
142 127 };
143 static real descl[8] = { .6953f,.625f,.5781f,.5469f,.5312f,.5391f,.4688f,
144 .3828f };
145 integer *ivp2h;
146 static integer deadd[8] = { 1152,-2816,-1536,-3584,-1280,-2432,768,-1920 }
148 static integer qb[8] = { 511,511,1023,1023,1023,1023,2047,4095 };
149 static integer nbit[10] = { 8,8,5,5,4,4,4,4,3,2 };
150 static integer zrc[10] = { 0,0,0,0,0,3,0,2,0,0 };
151 static integer bit[5] = { 2,4,8,16,32 };
152 integer *iovoic;
153 integer *iavgp;
154 integer *iptold;
155 integer *erate;
156 integer *drc;
157 integer *dpit;
158 integer *drms;
160 /* System generated locals */
161 integer i__1, i__2;
163 /* Builtin functions */
164 integer pow_ii(integer *, integer *);
166 /* Local variables */
167 extern /* Subroutine */ int ham84_(integer *, integer *, integer *);
168 integer ipit, iout, i__, icorf, index, ivoic, ixcor, i1, i2, i4;
169 extern integer median_(integer *, integer *, integer *);
170 integer ishift, errcnt, lsb;
173 /* LPC Configuration parameters: */
174 /* Frame size, Prediction order, Pitch period */
175 /* Arguments */
177 /* LPC Processing control variables: */
179 /* *** Read-only: initialized in setup */
181 /* Files for Speech, Parameter, and Bitstream Input & Output, */
182 /* and message and debug outputs. */
184 /* Here are the only files which use these variables: */
186 /* lpcsim.f setup.f trans.f error.f vqsetup.f */
188 /* Many files which use fdebug are not listed, since it is only used in */
189 /* those other files conditionally, to print trace statements. */
190 /* integer fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
191 /* LPC order, Frame size, Quantization rate, Bits per frame, */
192 /* Error correction */
193 /* Subroutine SETUP is the only place where order is assigned a value, */
194 /* and that value is 10. It could increase efficiency 1% or so to */
195 /* declare order as a constant (i.e., a Fortran PARAMETER) instead of as
197 /* a variable in a COMMON block, since it is used in many places in the */
198 /* core of the coding and decoding routines. Actually, I take that back.
200 /* At least when compiling with f2c, the upper bound of DO loops is */
201 /* stored in a local variable before the DO loop begins, and then that is */
202 /* compared against on each iteration. */
203 /* Similarly for lframe, which is given a value of MAXFRM in SETUP. */
204 /* Similarly for quant, which is given a value of 2400 in SETUP. quant */
205 /* is used in only a few places, and never in the core coding and */
206 /* decoding routines, so it could be eliminated entirely. */
207 /* nbits is similar to quant, and is given a value of 54 in SETUP. */
208 /* corrp is given a value of .TRUE. in SETUP, and is only used in the */
209 /* subroutines ENCODE and DECODE. It doesn't affect the speed of the */
210 /* coder significantly whether it is .TRUE. or .FALSE., or whether it is
212 /* a constant or a variable, since it is only examined once per frame. */
213 /* Leaving it as a variable that is set to .TRUE. seems like a good */
214 /* idea, since it does enable some error-correction capability for */
215 /* unvoiced frames, with no change in the coding rate, and no noticeable
217 /* quality difference in the decoded speech. */
218 /* integer quant, nbits */
219 /* *** Read/write: variables for debugging, not needed for LPC algorithm
222 /* Current frame, Unstable frames, Output clip count, Max onset buffer,
224 /* Debug listing detail level, Line count on listing page */
226 /* nframe is not needed for an embedded LPC10 at all. */
227 /* nunsfm is initialized to 0 in SETUP, and incremented in subroutine */
228 /* ERROR, which is only called from RCCHK. When LPC10 is embedded into */
229 /* an application, I would recommend removing the call to ERROR in RCCHK,
231 /* and remove ERROR and nunsfm completely. */
232 /* iclip is initialized to 0 in SETUP, and incremented in entry SWRITE in
234 /* sread.f. When LPC10 is embedded into an application, one might want */
235 /* to cause it to be incremented in a routine that takes the output of */
236 /* SYNTHS and sends it to an audio device. It could be optionally */
237 /* displayed, for those that might want to know what it is. */
238 /* maxosp is never initialized to 0 in SETUP, although it probably should
240 /* be, and it is updated in subroutine ANALYS. I doubt that its value */
241 /* would be of much interest to an application in which LPC10 is */
242 /* embedded. */
243 /* listl and lincnt are not needed for an embedded LPC10 at all. */
244 /* integer nframe, nunsfm, iclip, maxosp, listl, lincnt */
245 /* common /contrl/ fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
246 /* common /contrl/ quant, nbits */
247 /* common /contrl/ nframe, nunsfm, iclip, maxosp, listl, lincnt */
248 /* Function return value definitions */
250 /* Parameters/constants */
252 /* The variables below that are not Fortran PARAMETER's are */
253 /* initialized with DATA statements, and then never modified. */
254 /* The following are used regardless of CORRP's value. */
256 /* DETAU, NBIT, QB, DEADD, DETAB7, RMST, DESCL */
258 /* The following are used only if CORRP is .TRUE. */
260 /* ETHRS, ETHRS1, ETHRS2, ETHRS3, IVTAB, BIT, CORTH, ZRC */
262 /* Local variables that need not be saved */
264 /* The following are used regardless of CORRP's value */
265 /* The following are used only if CORRP is .TRUE. */
267 /* Local state */
269 /* The following are used regardless of CORRP's value */
270 /* The following are used only if CORRP is .TRUE. */
271 /* I am guessing the initial values for IVP2H, IOVOIC, DRC, DPIT, */
272 /* and DRMS. They should be checked to see if they are reasonable.
274 /* I'm also guessing for ERATE, but I think 0 is the right initial
276 /* value. */
277 /* Parameter adjustments */
278 if (irc) {
279 --irc;
281 if (voice) {
282 --voice;
284 if (rc) {
285 --rc;
288 /* Function Body */
290 iptold = &(st->iptold);
291 first = &(st->first);
292 ivp2h = &(st->ivp2h);
293 iovoic = &(st->iovoic);
294 iavgp = &(st->iavgp);
295 erate = &(st->erate);
296 drc = &(st->drc[0]);
297 dpit = &(st->dpit[0]);
298 drms = &(st->drms[0]);
300 /* DATA statements for "constants" defined above. */
301 /* IF (LISTL.GE.3) WRITE(FDEBUG,800) IPITV,IRMS,(IRC(J),J=1,ORDER) */
302 /* 800 FORMAT(1X,' <<ERRCOR IN>>',T32,6X,I6,I5,T50,10I8) */
303 /* If no error correction, do pitch and voicing then jump to decode */
304 i4 = detau[*ipitv];
305 if (! contrl_1.corrp) {
306 voice[1] = 1;
307 voice[2] = 1;
308 if (*ipitv <= 1) {
309 voice[1] = 0;
311 if (*ipitv == 0 || *ipitv == 2) {
312 voice[2] = 0;
314 *pitch = i4;
315 if (*pitch <= 4) {
316 *pitch = *iptold;
318 if (voice[1] == 1 && voice[2] == 1) {
319 *iptold = *pitch;
321 if (voice[1] != voice[2]) {
322 *pitch = *iptold;
324 goto L900;
326 /* Do error correction pitch and voicing */
327 if (i4 > 4) {
328 dpit[0] = i4;
329 ivoic = 2;
330 *iavgp = (*iavgp * 15 + i4 + 8) / 16;
331 } else {
332 ivoic = i4;
333 dpit[0] = *iavgp;
335 drms[0] = *irms;
336 i__1 = contrl_1.order;
337 for (i__ = 1; i__ <= i__1; ++i__) {
338 drc[i__ * 3 - 3] = irc[i__];
340 /* Determine index to IVTAB from V/UV decision */
341 /* If error rate is high then use alternate table */
342 index = (*ivp2h << 4) + (*iovoic << 2) + ivoic + 1;
343 i1 = ivtab[index - 1];
344 ipit = i1 & 3;
345 icorf = i1 / 8;
346 if (*erate < ethrs) {
347 icorf /= 64;
349 /* Determine error rate: 4=high 1=low */
350 ixcor = 4;
351 if (*erate < ethrs3) {
352 ixcor = 3;
354 if (*erate < ethrs2) {
355 ixcor = 2;
357 if (*erate < ethrs1) {
358 ixcor = 1;
360 /* Voice/unvoice decision determined from bits 0 and 1 of IVTAB */
361 voice[1] = icorf / 2 & 1;
362 voice[2] = icorf & 1;
363 /* Skip decoding on first frame because present data not yet available */
364 if (*first) {
365 *first = FALSE_;
366 /* Assign PITCH a "default" value on the first call, since */
367 /* otherwise it would be left uninitialized. The two lines
369 /* below were copied from above, since it seemed like a */
370 /* reasonable thing to do for the first call. */
371 *pitch = i4;
372 if (*pitch <= 4) {
373 *pitch = *iptold;
375 goto L500;
377 /* If bit 4 of ICORF is set then correct RMS and RC(1) - RC(4). */
378 /* Determine error rate and correct errors using a Hamming 8,4 code */
379 /* during transition or unvoiced frame. If IOUT is negative, */
380 /* more than 1 error occurred, use previous frame's parameters. */
381 if ((icorf & bit[3]) != 0) {
382 errcnt = 0;
383 lsb = drms[1] & 1;
384 index = (drc[22] << 4) + drms[1] / 2;
385 ham84_(&index, &iout, &errcnt);
386 drms[1] = drms[2];
387 if (iout >= 0) {
388 drms[1] = (iout << 1) + lsb;
390 for (i__ = 1; i__ <= 4; ++i__) {
391 if (i__ == 1) {
392 i1 = ((drc[25] & 7) << 1) + (drc[28] & 1);
393 } else {
394 i1 = drc[(9 - i__) * 3 - 2] & 15;
396 i2 = drc[(5 - i__) * 3 - 2] & 31;
397 lsb = i2 & 1;
398 index = (i1 << 4) + i2 / 2;
399 ham84_(&index, &iout, &errcnt);
400 if (iout >= 0) {
401 iout = (iout << 1) + lsb;
402 if ((iout & 16) == 16) {
403 iout += -32;
405 } else {
406 iout = drc[(5 - i__) * 3 - 1];
408 drc[(5 - i__) * 3 - 2] = iout;
410 /* Determine error rate */
411 *erate = *erate * .96875f + errcnt * 102;
413 /* Get unsmoothed RMS, RC's, and PITCH */
414 *irms = drms[1];
415 i__1 = contrl_1.order;
416 for (i__ = 1; i__ <= i__1; ++i__) {
417 irc[i__] = drc[i__ * 3 - 2];
419 if (ipit == 1) {
420 dpit[1] = dpit[2];
422 if (ipit == 3) {
423 dpit[1] = dpit[0];
425 *pitch = dpit[1];
426 /* If bit 2 of ICORF is set then smooth RMS and RC's, */
427 if ((icorf & bit[1]) != 0) {
428 if ((i__1 = drms[1] - drms[0], (real) abs(i__1)) >= corth[ixcor + 3]
429 && (i__2 = drms[1] - drms[2], (real) abs(i__2)) >= corth[
430 ixcor + 3]) {
431 *irms = median_(&drms[2], &drms[1], drms);
433 for (i__ = 1; i__ <= 6; ++i__) {
434 if ((i__1 = drc[i__ * 3 - 2] - drc[i__ * 3 - 3], (real) abs(i__1))
435 >= corth[ixcor + ((i__ + 2) << 2) - 5] && (i__2 = drc[i__ *
436 3 - 2] - drc[i__ * 3 - 1], (real) abs(i__2)) >= corth[
437 ixcor + ((i__ + 2) << 2) - 5]) {
438 irc[i__] = median_(&drc[i__ * 3 - 1], &drc[i__ * 3 - 2], &drc[
439 i__ * 3 - 3]);
443 /* If bit 3 of ICORF is set then smooth pitch */
444 if ((icorf & bit[2]) != 0) {
445 if ((i__1 = dpit[1] - dpit[0], (real) abs(i__1)) >= corth[ixcor - 1]
446 && (i__2 = dpit[1] - dpit[2], (real) abs(i__2)) >= corth[
447 ixcor - 1]) {
448 *pitch = median_(&dpit[2], &dpit[1], dpit);
451 /* If bit 5 of ICORF is set then RC(5) - RC(10) are loaded with */
452 /* values so that after quantization bias is removed in decode */
453 /* the values will be zero. */
454 L500:
455 if ((icorf & bit[4]) != 0) {
456 i__1 = contrl_1.order;
457 for (i__ = 5; i__ <= i__1; ++i__) {
458 irc[i__] = zrc[i__ - 1];
461 /* House keeping - one frame delay */
462 *iovoic = ivoic;
463 *ivp2h = voice[2];
464 dpit[2] = dpit[1];
465 dpit[1] = dpit[0];
466 drms[2] = drms[1];
467 drms[1] = drms[0];
468 i__1 = contrl_1.order;
469 for (i__ = 1; i__ <= i__1; ++i__) {
470 drc[i__ * 3 - 1] = drc[i__ * 3 - 2];
471 drc[i__ * 3 - 2] = drc[i__ * 3 - 3];
473 L900:
474 /* IF (LISTL.GE.3)WRITE(FDEBUG,801)VOICE,PITCH,IRMS,(IRC(J),J=1,ORDER) */
475 /* 801 FORMAT(1X,'<<ERRCOR OUT>>',T32,2I3,I6,I5,T50,10I8) */
476 /* Decode RMS */
477 *irms = rmst[(31 - *irms) * 2];
478 /* Decode RC(1) and RC(2) from log-area-ratios */
479 /* Protect from illegal coded value (-16) caused by bit errors */
480 for (i__ = 1; i__ <= 2; ++i__) {
481 i2 = irc[i__];
482 i1 = 0;
483 if (i2 < 0) {
484 i1 = 1;
485 i2 = -i2;
486 if (i2 > 15) {
487 i2 = 0;
490 i2 = detab7[i2 * 2];
491 if (i1 == 1) {
492 i2 = -i2;
494 ishift = 15 - nbit[i__ - 1];
495 irc[i__] = i2 * pow_ii(&c__2, &ishift);
497 /* Decode RC(3)-RC(10) to sign plus 14 bits */
498 i__1 = contrl_1.order;
499 for (i__ = 3; i__ <= i__1; ++i__) {
500 i2 = irc[i__];
501 ishift = 15 - nbit[i__ - 1];
502 i2 *= pow_ii(&c__2, &ishift);
503 i2 += qb[i__ - 3];
504 irc[i__] = i2 * descl[i__ - 3] + deadd[i__ - 3];
506 /* IF (LISTL.GE.3) WRITE(FDEBUG,811) IRMS, (IRC(I),I=1,ORDER) */
507 /* 811 FORMAT(1X,'<<DECODE OUT>>',T45,I4,1X,10I8) */
508 /* Scale RMS and RC's to reals */
509 *rms = (real) (*irms);
510 i__1 = contrl_1.order;
511 for (i__ = 1; i__ <= i__1; ++i__) {
512 rc[i__] = irc[i__] / 16384.f;
514 return 0;
515 } /* decode_ */