⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sb_celp.c

📁 mediastreamer2是开源的网络传输媒体流的库
💻 C
📖 第 1 页 / 共 4 页
字号:
/* Copyright (C) 2002-2006 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.*/#ifdef HAVE_CONFIG_H#include "config.h"#endif#include <math.h>#include "sb_celp.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 "arch.h"#include "math_approx.h"#include "os_support.h"#ifndef NULL#define NULL 0#endif/* Default size for the encoder and decoder stack (can be changed at compile time).   This does not apply when using variable-size arrays or alloca. */#ifndef SB_ENC_STACK#define SB_ENC_STACK (10000*sizeof(spx_sig_t))#endif#ifndef SB_DEC_STACK#define SB_DEC_STACK (6000*sizeof(spx_sig_t))#endif#ifdef DISABLE_WIDEBANDvoid *sb_encoder_init(const SpeexMode *m){   speex_fatal("Wideband and Ultra-wideband are disabled");   return NULL;}void sb_encoder_destroy(void *state){   speex_fatal("Wideband and Ultra-wideband are disabled");}int sb_encode(void *state, void *vin, SpeexBits *bits){   speex_fatal("Wideband and Ultra-wideband are disabled");   return -2;}void *sb_decoder_init(const SpeexMode *m){   speex_fatal("Wideband and Ultra-wideband are disabled");   return NULL;}void sb_decoder_destroy(void *state){   speex_fatal("Wideband and Ultra-wideband are disabled");}int sb_decode(void *state, SpeexBits *bits, void *vout){   speex_fatal("Wideband and Ultra-wideband are disabled");   return -2;}int sb_encoder_ctl(void *state, int request, void *ptr){   speex_fatal("Wideband and Ultra-wideband are disabled");   return -2;}int sb_decoder_ctl(void *state, int request, void *ptr){   speex_fatal("Wideband and Ultra-wideband are disabled");   return -2;}#else#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};static const spx_word16_t fold_quant_bound[32] = {   39, 44, 50, 57, 64, 73, 83, 94,   106, 120, 136, 154, 175, 198, 225, 255,   288, 327, 370, 420, 476, 539, 611, 692,   784, 889, 1007, 1141, 1293, 1465, 1660, 1881};#define LSP_MARGIN 410#define LSP_DELTA1 6553#define LSP_DELTA2 1638#elsestatic const spx_word16_t gc_quant_bound[16] = {      0.97979, 1.28384, 1.68223, 2.20426, 2.88829, 3.78458, 4.95900, 6.49787,       8.51428, 11.15642, 14.61846, 19.15484, 25.09895, 32.88761, 43.09325, 56.46588};static const spx_word16_t fold_quant_bound[32] = {   0.30498, 0.34559, 0.39161, 0.44375, 0.50283, 0.56979, 0.64565, 0.73162,   0.82903, 0.93942, 1.06450, 1.20624, 1.36685, 1.54884, 1.75506, 1.98875,   2.25355, 2.55360, 2.89361, 3.27889, 3.71547, 4.21018, 4.77076, 5.40598,   6.12577, 6.94141, 7.86565, 8.91295, 10.09969, 11.44445, 12.96826, 14.69497};#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};#elsestatic const float h0[64] = {   3.596189e-05f, -0.0001123515f,   -0.0001104587f, 0.0002790277f,   0.0002298438f, -0.0005953563f,   -0.0003823631f, 0.00113826f,   0.0005308539f, -0.001986177f,   -0.0006243724f, 0.003235877f,   0.0005743159f, -0.004989147f,   -0.0002584767f, 0.007367171f,   -0.0004857935f, -0.01050689f,   0.001894714f, 0.01459396f,   -0.004313674f, -0.01994365f,   0.00828756f, 0.02716055f,   -0.01485397f, -0.03764973f,   0.026447f, 0.05543245f,   -0.05095487f, -0.09779096f,   0.1382363f, 0.4600981f,   0.4600981f, 0.1382363f,   -0.09779096f, -0.05095487f,   0.05543245f, 0.026447f,   -0.03764973f, -0.01485397f,   0.02716055f, 0.00828756f,   -0.01994365f, -0.004313674f,   0.01459396f, 0.001894714f,   -0.01050689f, -0.0004857935f,   0.007367171f, -0.0002584767f,   -0.004989147f, 0.0005743159f,   0.003235877f, -0.0006243724f,   -0.001986177f, 0.0005308539f,   0.00113826f, -0.0003823631f,   -0.0005953563f, 0.0002298438f,   0.0002790277f, -0.0001104587f,   -0.0001123515f, 3.596189e-05f};#endifextern const spx_word16_t lag_window[];extern const spx_word16_t lpc_window[];void *sb_encoder_init(const SpeexMode *m){   int i;   spx_int32_t tmp;   SBEncState *st;   const SpeexSBMode *mode;   st = (SBEncState*)speex_alloc(sizeof(SBEncState));   if (!st)      return NULL;   st->mode = m;   mode = (const SpeexSBMode*)m->mode;   st->st_low = speex_encoder_init(mode->nb_mode);#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)   st->stack = NULL;#else   /*st->stack = (char*)speex_alloc_scratch(SB_ENC_STACK);*/   speex_encoder_ctl(st->st_low, SPEEX_GET_STACK, &st->stack);#endif   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+st->subframeSize;   st->lpcSize=mode->lpcSize;   st->encode_submode = 1;   st->submodes=mode->submodes;   st->submodeSelect = st->submodeID=mode->defaultSubmode;      tmp=9;   speex_encoder_ctl(st->st_low, SPEEX_SET_QUALITY, &tmp);   tmp=1;   speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, &tmp);   st->lpc_floor = mode->lpc_floor;   st->gamma1=mode->gamma1;   st->gamma2=mode->gamma2;   st->first=1;   st->high=(spx_word16_t*)speex_alloc((st->windowSize-st->frame_size)*sizeof(spx_word16_t));   st->h0_mem=(spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));   st->h1_mem=(spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));   st->window= lpc_window;   st->lagWindow = lag_window;   st->old_lsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));   st->old_qlsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));   st->interp_qlpc = (spx_coef_t*)speex_alloc(st->lpcSize*sizeof(spx_coef_t));   st->pi_gain = (spx_word32_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));   st->exc_rms = (spx_word16_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word16_t));   st->innov_rms_save = NULL;      st->mem_sp = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));   st->mem_sp2 = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));   st->mem_sw = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));   for (i=0;i<st->lpcSize;i++)      st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1);#ifndef DISABLE_VBR   st->vbr_quality = 8;   st->vbr_enabled = 0;   st->vbr_max = 0;   st->vbr_max_high = 20000;  /* We just need a big value here */   st->vad_enabled = 0;   st->abr_enabled = 0;   st->relative_quality=0;#endif /* #ifndef DISABLE_VBR */   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);#if !(defined(VAR_ARRAYS) || defined (USE_ALLOCA))   /*speex_free_scratch(st->stack);*/#endif   speex_free(st->high);   speex_free(st->h0_mem);   speex_free(st->h1_mem);   speex_free(st->old_lsp);   speex_free(st->old_qlsp);   speex_free(st->interp_qlpc);   speex_free(st->pi_gain);   speex_free(st->exc_rms);   speex_free(st->mem_sp);   speex_free(st->mem_sp2);   speex_free(st->mem_sw);      speex_free(st);}int sb_encode(void *state, void *vin, SpeexBits *bits){   SBEncState *st;   int i, roots, sub;   char *stack;   VARDECL(spx_mem_t *mem);   VARDECL(spx_sig_t *innov);   VARDECL(spx_word16_t *target);   VARDECL(spx_word16_t *syn_resp);   VARDECL(spx_word32_t *low_pi_gain);   spx_word16_t *low;   spx_word16_t *high;   VARDECL(spx_word16_t *low_exc_rms);   VARDECL(spx_word16_t *low_innov_rms);   const SpeexSBMode *mode;   spx_int32_t dtx;   spx_word16_t *in = (spx_word16_t*)vin;   spx_word16_t e_low=0, e_high=0;   VARDECL(spx_coef_t *lpc);   VARDECL(spx_coef_t *interp_lpc);   VARDECL(spx_coef_t *bw_lpc1);   VARDECL(spx_coef_t *bw_lpc2);   VARDECL(spx_lsp_t *lsp);   VARDECL(spx_lsp_t *qlsp);   VARDECL(spx_lsp_t *interp_lsp);   VARDECL(spx_lsp_t *interp_qlsp);         st = (SBEncState*)state;   stack=st->stack;   mode = (const SpeexSBMode*)(st->mode->mode);   low = in;   high = in+st->frame_size;      /* High-band buffering / sync with low band */   /* Compute the two sub-bands by filtering with QMF h0*/   qmf_decomp(in, h0, low, high, st->full_frame_size, QMF_ORDER, st->h0_mem, stack);   #ifndef DISABLE_VBR   if (st->vbr_enabled || st->vad_enabled)   {      /* Need to compute things here before the signal is trashed by the encoder */      /*FIXME: Are the two signals (low, high) in sync? */      e_low = compute_rms16(low, st->frame_size);      e_high = compute_rms16(high, st->frame_size);   }#endif /* #ifndef DISABLE_VBR */   ALLOC(low_innov_rms, st->nbSubframes, spx_word16_t);   speex_encoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_rms);   /* Encode the narrowband part*/   speex_encode_native(st->st_low, low, bits);   high = high - (st->windowSize-st->frame_size);   SPEEX_COPY(high, st->high, st->windowSize-st->frame_size);   SPEEX_COPY(st->high, &high[st->frame_size], st->windowSize-st->frame_size);      ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_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_rms);      speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, &dtx);   if (dtx==0)      dtx=1;   else      dtx=0;   ALLOC(lpc, st->lpcSize, spx_coef_t);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -