📄 celp.c
字号:
if (lspflag) {
#ifdef CELPDIAG
fprintf(stderr, "celp: Bad \"new\" lsp at frame: %d\n", frame);
fprintf(stderr, "lsp: ");
for (i = 0; i < no; i++) {
fprintf(stderr, "%9.5f", newfreq[i]);
}
fprintf(stderr, "\npc: ");
for (i = 0; i < no + 1; i++) {
fprintf(stderr, "%9.5f", fcn[i]);
}
fprintf(stderr, "\nrc: ");
for (i = 0; i < no; i++) {
fprintf(stderr, "%9.5f", rcn[i]);
}
fprintf(stderr, "\n");
#endif
return CELP_LSPERR;
}
/* Save unquantized lsp. */
for (i = 0; i < no; i++) {
unqfreq[i] = newfreq[i];
}
/* Quantize lsp's. */
lsp34(newfreq, no, sbits, findex);
/* Pack lsp indices in bit stream array. */
for (i = 0; i < no; i++) {
pack(findex[i], sbits[i], stream, &l_pointer);
}
/* Linearly interpolate LSP's for each subframe. */
intanaly(newfreq, nn, lsp);
/* For each subframe, search stochastic & adaptive code books. */
k = 0;
for (i = 0; i < nn; i++) {
lsptopc(&lsp[i][0], fci);
for (j = 0; j < no + 1; j++) {
fc[j] = fci[j];
}
nseg++;
/* Code book & pitch searches. */
csub(&ssub[k], &v[k], l, lp);
/* Pitch quantization tau. */
/* Pack parameter indices in bit stream array. */
if (((i + 1) % 2) != 0) {
packtau(tauptr-minptr, pbits[0], pdencode, stream, &l_pointer);
} else {
pack(tauptr-minptr, pbits[1], stream, &l_pointer);
}
pack(pindex, pbits[2], stream, &l_pointer);
cbindex--;
pack(cbindex, cbbits, stream, &l_pointer);
pack(gindex, cbgbits, stream, &l_pointer);
/* Decode parameters for analysis by synthesis. */
cbindex++;
k += l;
}
/* Bit error protection
Extract bits to protect from stream array. */
#ifdef CELP_PROTECT
if (protect) {
for (i = 0; i < CODELENGTH2; i++) {
codeword[i] = stream[bitprotect[i] - 1];
}
/* Hamming encode. */
encodeham(CODELENGTH1, CODELENGTH2, hmatrix, &paritybit, codeword);
/* Pack future bit. */
pack(0, 1, stream, &l_pointer);
/* Pack parity bits. */
for (i = 0; i < PARITYLENGTH; i++) {
pack(codeword[CODELENGTH2 + i], 1, stream, &l_pointer);
}
/* Toggle and pack the sync bit. */
syncBit = syncBit ^ 1;
pack(syncBit, 1, stream, &l_pointer);
}
#endif
/* At this time "stream" contains the CELP encoded bitstream. The
stream array consists of one bit per int element. */
i2 = 0x80;
i3 = 0;
memset(packedbits, 0, STREAMBITS / 8);
for (i1 = 0; i1 < STREAMBITS; i1++) {
packedbits[i3] |= (stream[i1] ? i2 : 0);
i2 >>= 1;
if (i2 == 0) {
i2 = 0x80;
i3++;
}
}
/* Shift new speech buffer into old speech buffer
sold snew
|-------------------|-------------------| snew
|-------------------|
ssub */
for (i = 0; i < ll; i++) {
sold[i] = snew[i];
}
return celp_error;
}
/* CELP_DECODE -- Decode a CELP-encoded frame into PCM audio. */
#ifdef CELP_USE_CONTEXT
int celp_decode(struct celp_context *c, char packedbits[STREAMBITS / 8], short pf[MAXLL])
#else
int celp_decode(char packedbits[STREAMBITS / 8], short pf[MAXLL])
#endif
{
int i1, i2, i3;
int l_pointer;
#ifdef CELP_USE_CONTEXT
ctx = c;
#endif
i2 = 0x80;
i3 = 0;
for (i1 = 0; i1 < STREAMBITS; i1++) {
stream[i1] = (packedbits[i3] & i2) ? 1 : 0;
i2 >>= 1;
if (i2 == 0) {
i2 = 0x80;
i3++;
}
}
frame++;
l_pointer = -1;
celp_error = CELP_OK;
/* Unpack parity bits. */
#ifdef CELP_PROTECT
if (protect) {
l_pointer = l_pointer - PARITYLENGTH - 2;
l_pointer = 138;
for (i = 0; i < PARITYLENGTH; i++) {
unpack(stream, 1, &codeword[CODELENGTH2 + i], &l_pointer);
}
/* Extract code word from stream array. */
for (i = 0; i < CODELENGTH2; i++) {
codeword[i] = stream[bitprotect[i] - 1];
}
/* Repack Bisnu bit (remains constant for now). */
codeword[10] = 0;
/* Hamming decode. */
decodeham(CODELENGTH1, hmatrix, syndrometable, paritybit, codeword,
&twoerror, &syndrome);
/* Disable parity check (if parity not used). */
twoerror = FALSE;
/* Bit error rate estimator (running avg of bad syndromes). */
if (syndrome != 0) {
syndrome = 1;
}
syndavg = (float) ((1.0 - (1.0 / SYNDRUN)) * syndavg + (1.0 / SYNDRUN) * (float) syndrome);
/* Repack protected bits. */
for (i = 0; i < CODELENGTH2; i++) {
stream[bitprotect[i] - 1] = codeword[i];
}
/* Frame repeat if two errors detected in code word. */
if (twoerror) {
#ifdef CELPDIAG
fprintf(stderr, "celp: two errors have occured in frame %d\n", frame);
#endif
return CELP_TWOERR;
}
}
#endif /* CELP_PROTECT */
l_pointer = -1;
/* Unpack data stream. */
for (i = 0; i < no; i++) {
unpack(stream, sbits[i], &findex[i], &l_pointer);
}
/* Decode lsp's. */
lspdecode34(findex, no, newfreq);
/* Interpolate spectrum lsp's for nn subframes. */
intsynth(newfreq, nn, lsp, twoerror, syndavg);
/* Decode all code book and pitch parameters. */
bitpointer = l_pointer;
dcodtau(pbits[0], pbits[1], bitsum1, bitsum2, &bitpointer, nn, stream, pddecode, pdtabi, taus);
dcodpg(pbits[2], bitsum1, bitsum2, &bitpointer, nn, stream, pgs);
if (celp_error) {
return celp_error;
}
dcodcbi(cbbits, bitsum1, bitsum2, &bitpointer, nn, stream, cbi);
if (celp_error) {
return celp_error;
}
dcodcbg(cbgbits, bitsum1, bitsum2, &bitpointer, nn, stream, cbg);
if (celp_error) {
return celp_error;
}
/* *** synthesize each subframe */
nseg -= nn;
k = 0;
for (i = 0; i < nn; i++) {
nseg++;
/* Decode values for subframe. */
cbindex = cbi[i];
decodedgain = cbg[i];
#ifdef CELP_PROTECT
if (protect) {
smoothcbgain(&decodedgain, twoerror, syndavg, cbg, i + 1);
}
#endif
/* Code book synthesis. */
vdecode(decodedgain, l, &vdecoded[k]);
#ifdef CELP_PROTECT
if (protect) {
smoothtau(&taus[i], twoerror, syndavg, taus[2], i + 1);
}
#endif
bb[0] = taus[i];
bb[2] = pgs[i];
#ifdef CELP_PROTECT
if (protect) {
smoothpgain(&bb[2], twoerror, syndavg, pgs, i + 1);
}
#endif
/* Pitch synthesis. */
pitchvq(&vdecoded[k], l, dps, idb, bb, "long");
/* Pitch pre-filter (synthesis). */
if (prewt != 0.0) {
prefilt(&vdecoded[k], l, dpps);
}
/* Convert lsp's to pc's. */
lsptopc(&lsp[i][0], fci);
/* LPC synthesis. */
polefilt(fci, no, dss, &vdecoded[k], l);
if (celp_error) {
return celp_error;
}
/* Post-filtering */
postfilt(&vdecoded[k], l, ALPHA, BETA, &ip, &op, dp1, dp2, dp3);
/* Test for output speech clipping. */
while (clip(&vdecoded[k], l)) {
/* Frame repeat & recall synthesizer?
or scale vdecoded? */
#ifdef CELPDIAG
fprintf(stderr, "celp: Clipping detected at frame %d\n", frame);
#endif
for (j = 0; j < l; j++) {
vdecoded[k + j] = (float) (0.05 * vdecoded[k + j]);
}
}
/* Write postfiltered output speech disk files. */
for (j = 0; j < l; j++) {
pf[k + j] = round(mmax(-32768.0, mmin(32767.0, vdecoded[k + j])));
}
k += l;
}
return celp_error;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -