📄 sb_celp.c
字号:
/* Copyright (C) 2002 Jean-Marc Valin File: sb_celp.c Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of the Xiph.org Foundation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/#include <math.h>#include "sb_celp.h"#include "stdlib.h"#include "filters.h"#include "lpc.h"#include "lsp.h"#include "stack_alloc.h"#include "cb_search.h"#include "quant_lsp.h"#include "vq.h"#include "ltp.h"#include "misc.h"#include <stdio.h>#ifndef M_PI#define M_PI 3.14159265358979323846 /* pi */#endif#define sqr(x) ((x)*(x))#define SUBMODE(x) st->submodes[st->submodeID]->x#ifdef FIXED_POINTstatic const spx_word16_t gc_quant_bound[16] = {125, 164, 215, 282, 370, 484, 635, 832, 1090, 1428, 1871, 2452, 3213, 4210, 5516, 7228};#define LSP_MARGIN 410#define LSP_DELTA1 6553#define LSP_DELTA2 1638#else#define LSP_MARGIN .05#define LSP_DELTA1 .2#define LSP_DELTA2 .05#endif#define QMF_ORDER 64#ifdef FIXED_POINTstatic const spx_word16_t h0[64] = {2, -7, -7, 18, 15, -39, -25, 75, 35, -130, -41, 212, 38, -327, -17, 483, -32, -689, 124, 956, -283, -1307, 543, 1780, -973, -2467, 1733, 3633, -3339, -6409, 9059, 30153, 30153, 9059, -6409, -3339, 3633, 1733, -2467, -973, 1780, 543, -1307, -283, 956, 124, -689, -32, 483, -17, -327, 38, 212, -41, -130, 35, 75, -25, -39, 15, 18, -7, -7, 2};static const spx_word16_t h1[64] = {2, 7, -7, -18, 15, 39, -25, -75, 35, 130, -41, -212, 38, 327, -17, -483, -32, 689, 124, -956, -283, 1307, 543, -1780, -973, 2467, 1733, -3633, -3339, 6409, 9059, -30153, 30153, -9059, -6409, 3339, 3633, -1733, -2467, 973, 1780, -543, -1307, 283, 956, -124, -689, 32, 483, 17, -327, -38, 212, 41, -130, -35, 75, 25, -39, -15, 18, 7, -7, -2};#elsestatic const float h0[64] = { 3.596189e-05, -0.0001123515, -0.0001104587, 0.0002790277, 0.0002298438, -0.0005953563, -0.0003823631, 0.00113826, 0.0005308539, -0.001986177, -0.0006243724, 0.003235877, 0.0005743159, -0.004989147, -0.0002584767, 0.007367171, -0.0004857935, -0.01050689, 0.001894714, 0.01459396, -0.004313674, -0.01994365, 0.00828756, 0.02716055, -0.01485397, -0.03764973, 0.026447, 0.05543245, -0.05095487, -0.09779096, 0.1382363, 0.4600981, 0.4600981, 0.1382363, -0.09779096, -0.05095487, 0.05543245, 0.026447, -0.03764973, -0.01485397, 0.02716055, 0.00828756, -0.01994365, -0.004313674, 0.01459396, 0.001894714, -0.01050689, -0.0004857935, 0.007367171, -0.0002584767, -0.004989147, 0.0005743159, 0.003235877, -0.0006243724, -0.001986177, 0.0005308539, 0.00113826, -0.0003823631, -0.0005953563, 0.0002298438, 0.0002790277, -0.0001104587, -0.0001123515, 3.596189e-05};static const float h1[64] = { 3.596189e-05, 0.0001123515, -0.0001104587, -0.0002790277, 0.0002298438, 0.0005953563, -0.0003823631, -0.00113826, 0.0005308539, 0.001986177, -0.0006243724, -0.003235877, 0.0005743159, 0.004989147, -0.0002584767, -0.007367171, -0.0004857935, 0.01050689, 0.001894714, -0.01459396, -0.004313674, 0.01994365, 0.00828756, -0.02716055, -0.01485397, 0.03764973, 0.026447, -0.05543245, -0.05095487, 0.09779096, 0.1382363, -0.4600981, 0.4600981, -0.1382363, -0.09779096, 0.05095487, 0.05543245, -0.026447, -0.03764973, 0.01485397, 0.02716055, -0.00828756, -0.01994365, 0.004313674, 0.01459396, -0.001894714, -0.01050689, 0.0004857935, 0.007367171, 0.0002584767, -0.004989147, -0.0005743159, 0.003235877, 0.0006243724, -0.001986177, -0.0005308539, 0.00113826, 0.0003823631, -0.0005953563, -0.0002298438, 0.0002790277, 0.0001104587, -0.0001123515, -3.596189e-05};#endifstatic void mix_and_saturate(spx_word32_t *y0, spx_word32_t *y1, spx_word16_t *out, int len){ int i; for (i=0;i<len;i++) { spx_word32_t tmp;#ifdef FIXED_POINT tmp=PSHR(y0[i]-y1[i],SIG_SHIFT-1);#else tmp=2*(y0[i]-y1[i]);#endif if (tmp>32767) out[i] = 32767; else if (tmp<-32767) out[i] = -32767; else out[i] = tmp; }}void *sb_encoder_init(const SpeexMode *m){ int i; SBEncState *st; const SpeexSBMode *mode; st = (SBEncState*)speex_alloc(sizeof(SBEncState)+10000*sizeof(spx_sig_t)); st->mode = m; mode = (SpeexSBMode*)m->mode; st->stack = ((char*)st) + sizeof(SBEncState); st->st_low = speex_encoder_init(mode->nb_mode); st->full_frame_size = 2*mode->frameSize; st->frame_size = mode->frameSize; st->subframeSize = mode->subframeSize; st->nbSubframes = mode->frameSize/mode->subframeSize; st->windowSize = st->frame_size*3/2; st->lpcSize=mode->lpcSize; st->bufSize=mode->bufSize; st->encode_submode = 1; st->submodes=mode->submodes; st->submodeSelect = st->submodeID=mode->defaultSubmode; i=9; speex_encoder_ctl(st->st_low, SPEEX_SET_QUALITY, &i); st->lag_factor = mode->lag_factor; st->lpc_floor = mode->lpc_floor; st->gamma1=mode->gamma1; st->gamma2=mode->gamma2; st->first=1; st->x0d=PUSH(st->stack, st->frame_size, spx_sig_t); st->x1d=PUSH(st->stack, st->frame_size, spx_sig_t); st->high=PUSH(st->stack, st->full_frame_size, spx_sig_t); st->y0=PUSH(st->stack, st->full_frame_size, spx_sig_t); st->y1=PUSH(st->stack, st->full_frame_size, spx_sig_t); st->h0_mem=PUSH(st->stack, QMF_ORDER, spx_word16_t); st->h1_mem=PUSH(st->stack, QMF_ORDER, spx_word16_t); st->g0_mem=PUSH(st->stack, QMF_ORDER, spx_word32_t); st->g1_mem=PUSH(st->stack, QMF_ORDER, spx_word32_t); st->buf=PUSH(st->stack, st->windowSize, spx_sig_t); st->excBuf=PUSH(st->stack, st->bufSize, spx_sig_t); st->exc = st->excBuf + st->bufSize - st->windowSize; st->res=PUSH(st->stack, st->frame_size, spx_sig_t); st->sw=PUSH(st->stack, st->frame_size, spx_sig_t); st->target=PUSH(st->stack, st->frame_size, spx_sig_t); /*Asymmetric "pseudo-Hamming" window*/ { int part1, part2; part1 = st->subframeSize*7/2; part2 = st->subframeSize*5/2; st->window = PUSH(st->stack, st->windowSize, spx_word16_t); for (i=0;i<part1;i++) st->window[i]=(spx_word16_t)(SIG_SCALING*(.54-.46*cos(M_PI*i/part1))); for (i=0;i<part2;i++) st->window[part1+i]=(spx_word16_t)(SIG_SCALING*(.54+.46*cos(M_PI*i/part2))); } st->lagWindow = PUSH(st->stack, st->lpcSize+1, spx_word16_t); for (i=0;i<st->lpcSize+1;i++) st->lagWindow[i]=16384*exp(-.5*sqr(2*M_PI*st->lag_factor*i)); st->autocorr = PUSH(st->stack, st->lpcSize+1, spx_word16_t); st->lpc = PUSH(st->stack, st->lpcSize+1, spx_coef_t); st->bw_lpc1 = PUSH(st->stack, st->lpcSize+1, spx_coef_t); st->bw_lpc2 = PUSH(st->stack, st->lpcSize+1, spx_coef_t); st->lsp = PUSH(st->stack, st->lpcSize, spx_lsp_t); st->qlsp = PUSH(st->stack, st->lpcSize, spx_lsp_t); st->old_lsp = PUSH(st->stack, st->lpcSize, spx_lsp_t); st->old_qlsp = PUSH(st->stack, st->lpcSize, spx_lsp_t); st->interp_lsp = PUSH(st->stack, st->lpcSize, spx_lsp_t); st->interp_qlsp = PUSH(st->stack, st->lpcSize, spx_lsp_t); st->interp_lpc = PUSH(st->stack, st->lpcSize+1, spx_coef_t); st->interp_qlpc = PUSH(st->stack, st->lpcSize+1, spx_coef_t); st->pi_gain = PUSH(st->stack, st->nbSubframes, spx_word32_t); st->mem_sp = PUSH(st->stack, st->lpcSize, spx_mem_t); st->mem_sp2 = PUSH(st->stack, st->lpcSize, spx_mem_t); st->mem_sw = PUSH(st->stack, st->lpcSize, spx_mem_t); st->vbr_quality = 8; st->vbr_enabled = 0; st->vad_enabled = 0; st->abr_enabled = 0; st->relative_quality=0; st->complexity=2; speex_encoder_ctl(st->st_low, SPEEX_GET_SAMPLING_RATE, &st->sampling_rate); st->sampling_rate*=2;#ifdef ENABLE_VALGRIND VALGRIND_MAKE_READABLE(st, (st->stack-(char*)st));#endif return st;}void sb_encoder_destroy(void *state){ SBEncState *st=(SBEncState*)state; speex_encoder_destroy(st->st_low); speex_free(st);}int sb_encode(void *state, void *vin, SpeexBits *bits){ SBEncState *st; int i, roots, sub; char *stack; spx_mem_t *mem; spx_sig_t *innov, *syn_resp; spx_word32_t *low_pi_gain; spx_sig_t *low_exc, *low_innov; SpeexSBMode *mode; int dtx; spx_word16_t *in = vin; st = (SBEncState*)state; stack=st->stack; mode = (SpeexSBMode*)(st->mode->mode); { spx_word16_t *low = PUSH(stack, st->frame_size, spx_word16_t); /* Compute the two sub-bands by filtering with h0 and h1*/ qmf_decomp(in, h0, st->x0d, st->x1d, st->full_frame_size, QMF_ORDER, st->h0_mem, stack); for (i=0;i<st->frame_size;i++) low[i] = PSHR(st->x0d[i],SIG_SHIFT); /* Encode the narrowband part*/ speex_encode_native(st->st_low, low, bits); for (i=0;i<st->frame_size;i++) st->x0d[i] = SHL(low[i],SIG_SHIFT); } /* High-band buffering / sync with low band */ for (i=0;i<st->windowSize-st->frame_size;i++) st->high[i] = st->high[st->frame_size+i]; for (i=0;i<st->frame_size;i++) st->high[st->windowSize-st->frame_size+i]=SATURATE(st->x1d[i],536854528); speex_move(st->excBuf, st->excBuf+st->frame_size, (st->bufSize-st->frame_size)*sizeof(spx_sig_t)); low_pi_gain = PUSH(stack, st->nbSubframes, spx_word32_t); low_exc = PUSH(stack, st->frame_size, spx_sig_t); low_innov = PUSH(stack, st->frame_size, spx_sig_t); speex_encoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain); speex_encoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc); speex_encoder_ctl(st->st_low, SPEEX_GET_INNOV, low_innov); speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, &dtx); if (dtx==0) dtx=1; else dtx=0; { spx_word16_t *w_sig; w_sig = PUSH(stack, st->windowSize, spx_word16_t); /* Window for analysis */ for (i=0;i<st->windowSize;i++) w_sig[i] = SHR(MULT16_16(SHR((spx_word32_t)(st->high[i]),SIG_SHIFT),st->window[i]),SIG_SHIFT); /* Compute auto-correlation */ _spx_autocorr(w_sig, st->autocorr, st->lpcSize+1, st->windowSize); } st->autocorr[0] = (spx_word16_t)(st->autocorr[0]*st->lpc_floor); /* Noise floor in auto-correlation domain */ /* Lag windowing: equivalent to filtering in the power-spectrum domain */ for (i=0;i<st->lpcSize+1;i++) st->autocorr[i] = MULT16_16_Q14(st->autocorr[i],st->lagWindow[i]); /* Levinson-Durbin */ _spx_lpc(st->lpc+1, st->autocorr, st->lpcSize); st->lpc[0] = (spx_coef_t)LPC_SCALING; /* LPC to LSPs (x-domain) transform */ roots=lpc_to_lsp (st->lpc, st->lpcSize, st->lsp, 15, LSP_DELTA1, stack); if (roots!=st->lpcSize) { roots = lpc_to_lsp (st->lpc, st->lpcSize, st->lsp, 11, LSP_DELTA2, stack); if (roots!=st->lpcSize) { /*If we can't find all LSP's, do some damage control and use a flat filter*/ for (i=0;i<st->lpcSize;i++) { st->lsp[i]=M_PI*((float)(i+1))/(st->lpcSize+1); } } } /* VBR code */ if ((st->vbr_enabled || st->vad_enabled) && !dtx) { float e_low=0, e_high=0; float ratio; if (st->abr_enabled) { float qual_change=0; if (st->abr_drift2 * st->abr_drift > 0) { /* Only adapt if long-term and short-term drift are the same sign */ qual_change = -.00001*st->abr_drift/(1+st->abr_count); if (qual_change>.1) qual_change=.1; if (qual_change<-.1) qual_change=-.1; } st->vbr_quality += qual_change; if (st->vbr_quality>10) st->vbr_quality=10; if (st->vbr_quality<0) st->vbr_quality=0; } /*FIXME: Are the two signals (low, high) in sync? */ e_low = compute_rms(st->x0d, st->frame_size); e_high = compute_rms(st->high, st->frame_size); ratio = 2*log((1+e_high)/(1+e_low)); speex_encoder_ctl(st->st_low, SPEEX_GET_RELATIVE_QUALITY, &st->relative_quality); if (ratio<-4) ratio=-4; if (ratio>2) ratio=2; /*if (ratio>-2)*/ if (st->vbr_enabled) { int modeid; modeid = mode->nb_modes-1; st->relative_quality+=1.0*(ratio+2); if (st->relative_quality<-1) st->relative_quality=-1; while (modeid) { int v1; float thresh; v1=(int)floor(st->vbr_quality); if (v1==10) thresh = mode->vbr_thresh[modeid][v1]; else thresh = (st->vbr_quality-v1) * mode->vbr_thresh[modeid][v1+1] + (1+v1-st->vbr_quality) * mode->vbr_thresh[modeid][v1]; if (st->relative_quality >= thresh) break; modeid--; } speex_encoder_ctl(state, SPEEX_SET_HIGH_MODE, &modeid); if (st->abr_enabled) { int bitrate; speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate); st->abr_drift+=(bitrate-st->abr_enabled); st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled); st->abr_count += 1.0; } } else { /* VAD only */ int modeid; if (st->relative_quality<2.0) modeid=1; else modeid=st->submodeSelect; /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/ st->submodeID=modeid; } /*fprintf (stderr, "%f %f\n", ratio, low_qual);*/ } if (st->encode_submode) { speex_bits_pack(bits, 1, 1); if (dtx) speex_bits_pack(bits, 0, SB_SUBMODE_BITS); else speex_bits_pack(bits, st->submodeID, SB_SUBMODE_BITS); } /* If null mode (no transmission), just set a couple things to zero*/ if (dtx || st->submodes[st->submodeID] == NULL) { for (i=0;i<st->frame_size;i++) st->exc[i]=st->sw[i]=VERY_SMALL; for (i=0;i<st->lpcSize;i++) st->mem_sw[i]=0; st->first=1; /* Final signal synthesis from excitation */ iir_mem2(st->exc, st->interp_qlpc, st->high, st->frame_size, st->lpcSize, st->mem_sp);#ifndef RELEASE /* Reconstruct the original */ fir_mem_up(st->x0d, h0, st->y0, st->full_frame_size, QMF_ORDER, st->g0_mem, stack);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -