📄 encode.c
字号:
{
/* Clear Frame Energy Gain MSB if output packet == all ones */
idxcbg=0x7f;
}
Bitpack(idxcbg, (unsigned short *) PackedWords, 8, PackWdsPtr);
}
else
{ /* bit rates 4 or 8kbps */
/* Send indication on the spectrum transition */
if (bit_rate == 4)
Bitpack(LPCflag, (unsigned short *) PackedWords, 1, PackWdsPtr);
/* Quantize the LSPs */
lspmaq(lsp_nq, ORDER, 1, knum, nsub, nsize, 16384, lsp, SScratch, bit_rate, lsptab);
/* Bit-pack the quantization indices */
for (i = 0; i < knum; i++)
Bitpack(SScratch[i], (unsigned short *) PackedWords,
lognsize[i], PackWdsPtr);
/* Update shiftSTATE with hysteresis */
if (beta < 3277)
{
accshift = 0;
dpm = 0;
shiftSTATE = 0;
}
if (accshift > 5120)
shiftSTATE = -1;
if (accshift < -5120)
shiftSTATE = 1;
if (accshift <= 2560 && shiftSTATE == -1)
shiftSTATE = 0;
if (accshift >= -2560 && shiftSTATE == 1)
shiftSTATE = 0;
/* Control accshift */
if (shiftSTATE == 1 && beta < 13107)
delay = add(delay, 1);
else if (shiftSTATE == -1 && beta < 13107)
delay = sub(delay, 1);
if (delay > DMAX)
delay = DMAX;
if (delay < DMIN)
delay = DMIN;
if( tty_option == TTY_NO_GAIN && tty_enc_flag != 0 )
{
/* Pack TTY information */
Bitpack((short) tty_enc_header, (unsigned short *) PackedWords, 2, PackWdsPtr);
Bitpack((short) tty_enc_char, (unsigned short *) PackedWords, 5, PackWdsPtr);
}
else
{
Bitpack(sub(delay, DMIN), (unsigned short *) PackedWords, 7, PackWdsPtr);
}
if (bit_rate == 4)
{
j = sub(delay, pdelay);
if (abs(j) > 15)
j = 0;
else
j = add(j, 16);
Bitpack(j, (unsigned short *) PackedWords, 5, PackWdsPtr);
}
/* Smooth interpolation if the difference between delays is too big */
if (abs_s(sub(delay, pdelay)) > 15)
pdelay = delay;
Aveidxppg = 0;
/*********************************
* CELP codebook search procedure *
*********************************/
for (i = 0; i < NoOfSubFrames; i++)
{
if (i < 2)
subframesize = sub(SubFrameSize, 1);
else
subframesize = SubFrameSize;
/* interpolate lsp */
Interpol(lspi, OldlspE, lsp, i, ORDER);
Interpol(lspi_nq, Oldlsp_nq, lsp_nq, i, ORDER);
/* Convert lsp to PC */
lsp2a(pci, lspi);
lsp2a(pci_nq, lspi_nq);
/* Get zir */
ZeroInput(zir, pci_nq, pci, Excitation + ACBMemSize, GAMMA1, GAMMA2, ORDER, subframesize, 0);
/* Calculate impulse response of 1/A(z) * A(z/g1) / A(z/g2) */
ImpulseRzp(H, pci_nq, pci, GAMMA1, GAMMA2, ORDER, Hlength);
/* Interpolate delay */
Interpol_delay(delayi, &pdelay, &delay, i);
ComputeACB(residualm, Excitation + ACBMemSize, delayi,
residual + GUARD + i * (SubFrameSize - 1),
FrameSize + GUARD - i * (SubFrameSize - 1), &dpm,
&accshift, beta, subframesize, RSHIFT);
/* Get weighted speech */
/* ORIGM */
SynthesisFilter(origm, residualm, pci_nq, SynMemoryM, ORDER, subframesize);
/* Weighting filter */
weight(wpci, pci_nq, GAMMA1, ORDER);
fir(Scratch, origm, wpci, WFmemFIR, ORDER, subframesize);
weight(wpci, pci_nq, GAMMA2, ORDER);
iir(worigm, Scratch, wpci, WFmemIIR, ORDER, subframesize);
/* Remove Zero input response from weighted speech */
for (j = 0; j < subframesize; j++)
worigm[j] = sub(worigm[j], zir[j]);
/* Calculate closed loop gain */
getgain(Excitation + ACBMemSize, &sum1, H, &idxppg, ppvq, ppvq_mid, ACBGainSize, 1, worigm, subframesize, Hlength);
Aveidxppg = add(Aveidxppg, idxppg);
/* Get TARGET for fixed c.b. */
/* Convolve excitation with H */
/* ExconvH stored in Scratch memory */
ConvolveImpulseR(ExconvH, Excitation + ACBMemSize, H, Hlength, subframesize);
for (j = 0; j < subframesize; j++)
TARGETw[j] = sub(worigm[j], ExconvH[j]);
/* Convert TARGET from weighted domain to residual domain */
Weight2Res(TARGET, TARGETw, pci_nq, pci, GAMMA1, GAMMA2, ORDER, SubFrameSize);
if (subframesize < SubFrameSize)
TARGETw[subframesize] = TARGET[subframesize] = Scratch[subframesize] = 0;
/* get delay for current subframe */
n = extract_h(L_add(L_shr(L_add(delayi[1], delayi[0]), 1), 32768));
/* Compute fixed codebook contribution */
if (n > subframesize)
n = 200;
/* ACELP fixed codebook search */
if (bit_rate == 4)
{
ACELP_Code(TARGETw, TARGET, H, n, sum1, subframesize, Scratch,
&fcbGain, y2, fcbIndexVector, 1);
/* constrain fcb gain */
/* fcbGain *= (1.0 - ppvq[idxppg] * 0.15); */
l_fcbGain = L_mpy_ls(L_sub(2147483647, L_mult(ppvq[idxppg], 9830)), fcbGain);
}
else
{
/** Processing half rate case **/
ACELP_Code(TARGETw, TARGET, H, n, sum1, subframesize, Scratch,
&fcbGain, y2, fcbIndexVector, 0);
/* Constrain fcb gain */
/* fcbGain *= (0.9 - ppvq[idxppg] * 0.1); */
l_fcbGain = L_mpy_ls(L_sub(1932735283, L_mult(ppvq[idxppg], 6554)), fcbGain);
}
/* scale fcbGain +6db to account for 6db loss at input */
l_fcbGain = L_shl(l_fcbGain, 1);
/* Quantize FCB Gain : */
fcb_gainq(&idxcbg, &fcbGain, l_fcbGain, gnvq, FCBGainSize);
/* adjust fcbGain -6db */
fcbGain = shr(fcbGain, 1);
/* Add to total excitation */
for (j = 0; j < subframesize; j++)
Excitation[j + ACBMemSize] = add(Excitation[j + ACBMemSize], round32(L_mult(Scratch[j], fcbGain)));
/* Update filters memory */
ZeroInput(zir, pci_nq, pci, Excitation + ACBMemSize, GAMMA1, GAMMA2, ORDER, subframesize, 1);
/* checking outputs */
/* Update residualm */
for (j = 0; j < dpm; j++)
residualm[j] = residualm[j + subframesize];
/* Update excitation */
for (j = 0; j < ACBMemSize; j++)
Excitation[j] = Excitation[j + subframesize];
/* Pack bits */
/* ACB gain index */
Bitpack(idxppg, (unsigned short *) PackedWords, 3, PackWdsPtr);
if (bit_rate == 4)
{
idxcb = fcbIndexVector[0];
Bitpack(idxcb, (unsigned short *) PackedWords, 8, PackWdsPtr);
idxcb = fcbIndexVector[1];
Bitpack(idxcb, (unsigned short *) PackedWords, 8, PackWdsPtr);
idxcb = fcbIndexVector[2];
Bitpack(idxcb, (unsigned short *) PackedWords, 8, PackWdsPtr);
idxcb = fcbIndexVector[3];
Bitpack(idxcb, (unsigned short *) PackedWords, 11, PackWdsPtr);
/* FCB gain index */
Bitpack(idxcbg, (unsigned short *) PackedWords, 5, PackWdsPtr);
}
else
{
/** Packing half-rate bits **/
idxcb = fcbIndexVector[0];
/* FCB shape index 10 bits */
Bitpack(idxcb, (unsigned short *) PackedWords, 10, PackWdsPtr);
/* FCB gain index */
Bitpack(idxcbg, (unsigned short *) PackedWords, 4, PackWdsPtr);
}
}
/* prevent overflow */
if (Aveidxppg <= 1 && idxppg != 1)
{
accshift = 0;
dpm = 0;
}
/* Update encoder variables */
pdelay = delay;
if( tty_option == TTY_NO_GAIN && tty_enc_flag != 0 )
{
if( bit_rate == 4 )
{
/* Zero the delta delay when TTY is in the lag bits */
PackedWords[2] = PackedWords[2] & ~0x0F80;
/* Pack TTY Baud Rate information in Bit 171*/
Bitpack((short) tty_enc_baud_rate, (unsigned short *) PackedWords, 1, PackWdsPtr);
}
}
} /* Ends if for rate of 4 or 8kbps */
lastrateE = bit_rate;
for (i = 0; i < PACKWDSNUM; i++)
codeBuf[i] = PackedWords[i];
encode_fcnt++;
}
/*======================================================================*/
/* ..Save LSPs. */
/*----------------------------------------------------------------------*/
void post_encode()
{
/*....(local) variables.... */
register int j;
/*....execute.... */
for (j = 0; j < ORDER; j++)
{
OldlspE[j] = lsp[j];
Oldlsp_nq[j] = lsp_nq[j];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -