- int cnt;
- short mag; /* Adaptive predictor, FLOAT A */
- short a2p; /* LIMC */
- short a1ul; /* UPA1 */
- short pks1; /* UPA2 */
- short fa1;
- char tr; /* tone/transition detector */
- short ylint, thr2, dqthr;
- short ylfrac, thr1;
- short pk0;
-
- pk0 = (dqsez < 0) ? 1 : 0; /* needed in updating predictor poles */
-
- mag = dq & 0x7FFF; /* prediction difference magnitude */
- /* TRANS */
- ylint = short (state_ptr->yl >> 15); /* exponent part of yl */
- ylfrac = (state_ptr->yl >> 10) & 0x1F; /* fractional part of yl */
- thr1 = (32 + ylfrac) << ylint; /* threshold */
- thr2 = (ylint > 9) ? 31 << 10 : thr1; /* limit thr2 to 31 << 10 */
- dqthr = (thr2 + (thr2 >> 1)) >> 1; /* dqthr = 0.75 * thr2 */
- if (state_ptr->td == 0) /* signal supposed voice */
- tr = 0;
- else if (mag <= dqthr) /* supposed data, but small mag */
- tr = 0; /* treated as voice */
- else /* signal is data (modem) */
- tr = 1;
-
- /*
- * Quantizer scale factor adaptation.
- */
-
- /* FUNCTW & FILTD & DELAY */
- /* update non-steady state step size multiplier */
- state_ptr->yu = y + ((wi - y) >> 5);
-
- /* LIMB */
- if (state_ptr->yu < 544) /* 544 <= yu <= 5120 */
- state_ptr->yu = 544;
- else if (state_ptr->yu > 5120)
- state_ptr->yu = 5120;
-
- /* FILTE & DELAY */
- /* update steady state step size multiplier */
- state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
-
- /*
- * Adaptive predictor coefficients.
- */
- if (tr == 1) { /* reset a's and b's for modem signal */
- state_ptr->a[0] = 0;
- state_ptr->a[1] = 0;
- state_ptr->b[0] = 0;
- state_ptr->b[1] = 0;
- state_ptr->b[2] = 0;
- state_ptr->b[3] = 0;
- state_ptr->b[4] = 0;
- state_ptr->b[5] = 0;
-
- a2p = 0; /* eliminate Compiler Warnings */
- } else { /* update a's and b's */
- pks1 = pk0 ^ state_ptr->pk[0]; /* UPA2 */
-
- /* update predictor pole a[1] */
- a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
- if (dqsez != 0) {
- fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
- if (fa1 < -8191) /* a2p = function of fa1 */
- a2p -= 0x100;
- else if (fa1 > 8191)
- a2p += 0xFF;
- else
- a2p += fa1 >> 5;
-
- if (pk0 ^ state_ptr->pk[1])
- /* LIMC */
- if (a2p <= -12160)
- a2p = -12288;
- else if (a2p >= 12416)
- a2p = 12288;
- else
- a2p -= 0x80;
- else if (a2p <= -12416)
- a2p = -12288;
- else if (a2p >= 12160)
- a2p = 12288;
- else
- a2p += 0x80;
- }
-
- /* TRIGB & DELAY */
- state_ptr->a[1] = a2p;
-
- /* UPA1 */
- /* update predictor pole a[0] */
- state_ptr->a[0] -= state_ptr->a[0] >> 8;
- if (dqsez != 0)
- if (pks1 == 0)
- state_ptr->a[0] += 192;
- else
- state_ptr->a[0] -= 192;
-
- /* LIMD */
- a1ul = 15360 - a2p;
- if (state_ptr->a[0] < -a1ul)
- state_ptr->a[0] = -a1ul;
- else if (state_ptr->a[0] > a1ul)
- state_ptr->a[0] = a1ul;
-
- /* UPB : update predictor zeros b[6] */
- for (cnt = 0; cnt < 6; cnt++) {
- if (code_size == 5) /* for 40Kbps G.723 */
- state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
- else /* for G.721 and 24Kbps G.723 */
- state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
- if (dq & 0x7FFF) { /* XOR */
- if ((dq ^ state_ptr->dq[cnt]) >= 0)
- state_ptr->b[cnt] += 128;
- else
- state_ptr->b[cnt] -= 128;
- }
- }
- }
-
- for (cnt = 5; cnt > 0; cnt--)
- state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
- /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
- if (mag == 0) {
- state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0xFC20;
- } else {
- state_ptr->dq[0] = (dq >= 0) ?
- base2 (mag) : base2 (mag) - 0x400;
- }
-
- state_ptr->sr[1] = state_ptr->sr[0];
- /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
- if (sr == 0) {
- state_ptr->sr[0] = 0x20;
- } else if (sr > 0) {
- state_ptr->sr[0] = base2(sr);
- } else if (sr > -32768) {
- mag = -sr;
- state_ptr->sr[0] = base2(mag) - 0x400;
- } else
- state_ptr->sr[0] = short (0xFC20);
-
- /* DELAY A */
- state_ptr->pk[1] = state_ptr->pk[0];
- state_ptr->pk[0] = pk0;
-
- /* TONE */
- if (tr == 1) /* this sample has been treated as data */
- state_ptr->td = 0; /* next one will be treated as voice */
- else if (a2p < -11776) /* small sample-to-sample correlation */
- state_ptr->td = 1; /* signal may be data */
- else /* signal is voice */
- state_ptr->td = 0;
-
- /*
- * Adaptation speed control.
- */
- state_ptr->dms += (fi - state_ptr->dms) >> 5; /* FILTA */
- state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7); /* FILTB */
-
- if (tr == 1)
- state_ptr->ap = 256;
- else if (y < 1536) /* SUBTC */
- state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
- else if (state_ptr->td == 1)
- state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
- else if (abs((state_ptr->dms << 2) - state_ptr->dml) >=
- (state_ptr->dml >> 3))
- state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
- else
- state_ptr->ap += (-state_ptr->ap) >> 4;
+ int cnt;
+ short mag; /* Adaptive predictor, FLOAT A */
+ short a2p; /* LIMC */
+ short a1ul; /* UPA1 */
+ short pks1; /* UPA2 */
+ short fa1;
+ char tr; /* tone/transition detector */
+ short ylint, thr2, dqthr;
+ short ylfrac, thr1;
+ short pk0;
+
+ pk0 = (dqsez < 0) ? 1 : 0; /* needed in updating predictor poles */
+
+ mag = dq & 0x7FFF; /* prediction difference magnitude */
+ /* TRANS */
+ ylint = short (state_ptr->yl >> 15); /* exponent part of yl */
+ ylfrac = (state_ptr->yl >> 10) & 0x1F; /* fractional part of yl */
+ thr1 = (32 + ylfrac) << ylint; /* threshold */
+ thr2 = (ylint > 9) ? 31 << 10 : thr1; /* limit thr2 to 31 << 10 */
+ dqthr = (thr2 + (thr2 >> 1)) >> 1; /* dqthr = 0.75 * thr2 */
+ if (state_ptr->td == 0) /* signal supposed voice */
+ tr = 0;
+ else if (mag <= dqthr) /* supposed data, but small mag */
+ tr = 0; /* treated as voice */
+ else /* signal is data (modem) */
+ tr = 1;
+
+ /*
+ * Quantizer scale factor adaptation.
+ */
+
+ /* FUNCTW & FILTD & DELAY */
+ /* update non-steady state step size multiplier */
+ state_ptr->yu = y + ((wi - y) >> 5);
+
+ /* LIMB */
+ if (state_ptr->yu < 544) /* 544 <= yu <= 5120 */
+ state_ptr->yu = 544;
+ else if (state_ptr->yu > 5120)
+ state_ptr->yu = 5120;
+
+ /* FILTE & DELAY */
+ /* update steady state step size multiplier */
+ state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
+
+ /*
+ * Adaptive predictor coefficients.
+ */
+ if (tr == 1) { /* reset a's and b's for modem signal */
+ state_ptr->a[0] = 0;
+ state_ptr->a[1] = 0;
+ state_ptr->b[0] = 0;
+ state_ptr->b[1] = 0;
+ state_ptr->b[2] = 0;
+ state_ptr->b[3] = 0;
+ state_ptr->b[4] = 0;
+ state_ptr->b[5] = 0;
+
+ a2p = 0; /* eliminate Compiler Warnings */
+ } else { /* update a's and b's */
+ pks1 = pk0 ^ state_ptr->pk[0]; /* UPA2 */
+
+ /* update predictor pole a[1] */
+ a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
+ if (dqsez != 0) {
+ fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
+ if (fa1 < -8191) /* a2p = function of fa1 */
+ a2p -= 0x100;
+ else if (fa1 > 8191)
+ a2p += 0xFF;
+ else
+ a2p += fa1 >> 5;
+
+ if (pk0 ^ state_ptr->pk[1])
+ /* LIMC */
+ if (a2p <= -12160)
+ a2p = -12288;
+ else if (a2p >= 12416)
+ a2p = 12288;
+ else
+ a2p -= 0x80;
+ else if (a2p <= -12416)
+ a2p = -12288;
+ else if (a2p >= 12160)
+ a2p = 12288;
+ else
+ a2p += 0x80;
+ }
+
+ /* TRIGB & DELAY */
+ state_ptr->a[1] = a2p;
+
+ /* UPA1 */
+ /* update predictor pole a[0] */
+ state_ptr->a[0] -= state_ptr->a[0] >> 8;
+ if (dqsez != 0)
+ if (pks1 == 0)
+ state_ptr->a[0] += 192;
+ else
+ state_ptr->a[0] -= 192;
+
+ /* LIMD */
+ a1ul = 15360 - a2p;
+ if (state_ptr->a[0] < -a1ul)
+ state_ptr->a[0] = -a1ul;
+ else if (state_ptr->a[0] > a1ul)
+ state_ptr->a[0] = a1ul;
+
+ /* UPB : update predictor zeros b[6] */
+ for (cnt = 0; cnt < 6; cnt++) {
+ if (code_size == 5) /* for 40Kbps G.723 */
+ state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
+ else /* for G.721 and 24Kbps G.723 */
+ state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
+ if (dq & 0x7FFF) { /* XOR */
+ if ((dq ^ state_ptr->dq[cnt]) >= 0)
+ state_ptr->b[cnt] += 128;
+ else
+ state_ptr->b[cnt] -= 128;
+ }
+ }
+ }
+
+ for (cnt = 5; cnt > 0; cnt--)
+ state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
+ /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
+ if (mag == 0) {
+ state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0xFC20;
+ } else {
+ state_ptr->dq[0] = (dq >= 0) ?
+ base2 (mag) : base2 (mag) - 0x400;
+ }
+
+ state_ptr->sr[1] = state_ptr->sr[0];
+ /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
+ if (sr == 0) {
+ state_ptr->sr[0] = 0x20;
+ } else if (sr > 0) {
+ state_ptr->sr[0] = base2(sr);
+ } else if (sr > -32768) {
+ mag = -sr;
+ state_ptr->sr[0] = base2(mag) - 0x400;
+ } else
+ state_ptr->sr[0] = short (0xFC20);
+
+ /* DELAY A */
+ state_ptr->pk[1] = state_ptr->pk[0];
+ state_ptr->pk[0] = pk0;
+
+ /* TONE */
+ if (tr == 1) /* this sample has been treated as data */
+ state_ptr->td = 0; /* next one will be treated as voice */
+ else if (a2p < -11776) /* small sample-to-sample correlation */
+ state_ptr->td = 1; /* signal may be data */
+ else /* signal is voice */
+ state_ptr->td = 0;
+
+ /*
+ * Adaptation speed control.
+ */
+ state_ptr->dms += (fi - state_ptr->dms) >> 5; /* FILTA */
+ state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7); /* FILTB */
+
+ if (tr == 1)
+ state_ptr->ap = 256;
+ else if (y < 1536) /* SUBTC */
+ state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
+ else if (state_ptr->td == 1)
+ state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
+ else if (abs((state_ptr->dms << 2) - state_ptr->dml) >=
+ (state_ptr->dml >> 3))
+ state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
+ else
+ state_ptr->ap += (-state_ptr->ap) >> 4;