sb_celp.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  1. /* Copyright (C) 2002-2006 Jean-Marc Valin
  2. File: sb_celp.c
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. - Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. - Neither the name of the Xiph.org Foundation nor the names of its
  12. contributors may be used to endorse or promote products derived from
  13. this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  22. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #include <math.h>
  30. #include "sb_celp.h"
  31. #include "filters.h"
  32. #include "lpc.h"
  33. #include "lsp.h"
  34. #include "stack_alloc.h"
  35. #include "cb_search.h"
  36. #include "quant_lsp.h"
  37. #include "vq.h"
  38. #include "ltp.h"
  39. #include "arch.h"
  40. #include "math_approx.h"
  41. #include "os_support.h"
  42. #ifndef NULL
  43. #define NULL 0
  44. #endif
  45. /* Default size for the encoder and decoder stack (can be changed at compile time).
  46. This does not apply when using variable-size arrays or alloca. */
  47. #ifndef SB_ENC_STACK
  48. #define SB_ENC_STACK (10000*sizeof(spx_sig_t))
  49. #endif
  50. #ifndef SB_DEC_STACK
  51. #define SB_DEC_STACK (6000*sizeof(spx_sig_t))
  52. #endif
  53. #ifdef DISABLE_WIDEBAND
  54. void *sb_encoder_init(const SpeexMode *m)
  55. {
  56. speex_fatal("Wideband and Ultra-wideband are disabled");
  57. return NULL;
  58. }
  59. void sb_encoder_destroy(void *state)
  60. {
  61. speex_fatal("Wideband and Ultra-wideband are disabled");
  62. }
  63. int sb_encode(void *state, void *vin, SpeexBits *bits)
  64. {
  65. speex_fatal("Wideband and Ultra-wideband are disabled");
  66. return -2;
  67. }
  68. void *sb_decoder_init(const SpeexMode *m)
  69. {
  70. speex_fatal("Wideband and Ultra-wideband are disabled");
  71. return NULL;
  72. }
  73. void sb_decoder_destroy(void *state)
  74. {
  75. speex_fatal("Wideband and Ultra-wideband are disabled");
  76. }
  77. int sb_decode(void *state, SpeexBits *bits, void *vout)
  78. {
  79. speex_fatal("Wideband and Ultra-wideband are disabled");
  80. return -2;
  81. }
  82. int sb_encoder_ctl(void *state, int request, void *ptr)
  83. {
  84. speex_fatal("Wideband and Ultra-wideband are disabled");
  85. return -2;
  86. }
  87. int sb_decoder_ctl(void *state, int request, void *ptr)
  88. {
  89. speex_fatal("Wideband and Ultra-wideband are disabled");
  90. return -2;
  91. }
  92. #else
  93. #ifndef M_PI
  94. #define M_PI 3.14159265358979323846 /* pi */
  95. #endif
  96. #define sqr(x) ((x)*(x))
  97. #define SUBMODE(x) st->submodes[st->submodeID]->x
  98. #ifdef FIXED_POINT
  99. static const spx_word16_t gc_quant_bound[16] = {125, 164, 215, 282, 370, 484, 635, 832, 1090, 1428, 1871, 2452, 3213, 4210, 5516, 7228};
  100. static const spx_word16_t fold_quant_bound[32] = {
  101. 39, 44, 50, 57, 64, 73, 83, 94,
  102. 106, 120, 136, 154, 175, 198, 225, 255,
  103. 288, 327, 370, 420, 476, 539, 611, 692,
  104. 784, 889, 1007, 1141, 1293, 1465, 1660, 1881};
  105. #define LSP_MARGIN 410
  106. #define LSP_DELTA1 6553
  107. #define LSP_DELTA2 1638
  108. #else
  109. static const spx_word16_t gc_quant_bound[16] = {
  110. 0.97979, 1.28384, 1.68223, 2.20426, 2.88829, 3.78458, 4.95900, 6.49787,
  111. 8.51428, 11.15642, 14.61846, 19.15484, 25.09895, 32.88761, 43.09325, 56.46588};
  112. static const spx_word16_t fold_quant_bound[32] = {
  113. 0.30498, 0.34559, 0.39161, 0.44375, 0.50283, 0.56979, 0.64565, 0.73162,
  114. 0.82903, 0.93942, 1.06450, 1.20624, 1.36685, 1.54884, 1.75506, 1.98875,
  115. 2.25355, 2.55360, 2.89361, 3.27889, 3.71547, 4.21018, 4.77076, 5.40598,
  116. 6.12577, 6.94141, 7.86565, 8.91295, 10.09969, 11.44445, 12.96826, 14.69497};
  117. #define LSP_MARGIN .05
  118. #define LSP_DELTA1 .2
  119. #define LSP_DELTA2 .05
  120. #endif
  121. #define QMF_ORDER 64
  122. #ifdef FIXED_POINT
  123. static 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};
  124. #else
  125. static const float h0[64] = {
  126. 3.596189e-05f, -0.0001123515f,
  127. -0.0001104587f, 0.0002790277f,
  128. 0.0002298438f, -0.0005953563f,
  129. -0.0003823631f, 0.00113826f,
  130. 0.0005308539f, -0.001986177f,
  131. -0.0006243724f, 0.003235877f,
  132. 0.0005743159f, -0.004989147f,
  133. -0.0002584767f, 0.007367171f,
  134. -0.0004857935f, -0.01050689f,
  135. 0.001894714f, 0.01459396f,
  136. -0.004313674f, -0.01994365f,
  137. 0.00828756f, 0.02716055f,
  138. -0.01485397f, -0.03764973f,
  139. 0.026447f, 0.05543245f,
  140. -0.05095487f, -0.09779096f,
  141. 0.1382363f, 0.4600981f,
  142. 0.4600981f, 0.1382363f,
  143. -0.09779096f, -0.05095487f,
  144. 0.05543245f, 0.026447f,
  145. -0.03764973f, -0.01485397f,
  146. 0.02716055f, 0.00828756f,
  147. -0.01994365f, -0.004313674f,
  148. 0.01459396f, 0.001894714f,
  149. -0.01050689f, -0.0004857935f,
  150. 0.007367171f, -0.0002584767f,
  151. -0.004989147f, 0.0005743159f,
  152. 0.003235877f, -0.0006243724f,
  153. -0.001986177f, 0.0005308539f,
  154. 0.00113826f, -0.0003823631f,
  155. -0.0005953563f, 0.0002298438f,
  156. 0.0002790277f, -0.0001104587f,
  157. -0.0001123515f, 3.596189e-05f
  158. };
  159. #endif
  160. extern const spx_word16_t lag_window[];
  161. extern const spx_word16_t lpc_window[];
  162. void *sb_encoder_init(const SpeexMode *m)
  163. {
  164. int i;
  165. spx_int32_t tmp;
  166. SBEncState *st;
  167. const SpeexSBMode *mode;
  168. st = (SBEncState*)speex_alloc(sizeof(SBEncState));
  169. if (!st)
  170. return NULL;
  171. st->mode = m;
  172. mode = (const SpeexSBMode*)m->mode;
  173. st->st_low = speex_encoder_init(mode->nb_mode);
  174. #if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
  175. st->stack = NULL;
  176. #else
  177. /*st->stack = (char*)speex_alloc_scratch(SB_ENC_STACK);*/
  178. speex_encoder_ctl(st->st_low, SPEEX_GET_STACK, &st->stack);
  179. #endif
  180. st->full_frame_size = 2*mode->frameSize;
  181. st->frame_size = mode->frameSize;
  182. st->subframeSize = mode->subframeSize;
  183. st->nbSubframes = mode->frameSize/mode->subframeSize;
  184. st->windowSize = st->frame_size+st->subframeSize;
  185. st->lpcSize=mode->lpcSize;
  186. st->encode_submode = 1;
  187. st->submodes=mode->submodes;
  188. st->submodeSelect = st->submodeID=mode->defaultSubmode;
  189. tmp=9;
  190. speex_encoder_ctl(st->st_low, SPEEX_SET_QUALITY, &tmp);
  191. tmp=1;
  192. speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, &tmp);
  193. st->lpc_floor = mode->lpc_floor;
  194. st->gamma1=mode->gamma1;
  195. st->gamma2=mode->gamma2;
  196. st->first=1;
  197. st->high=(spx_word16_t*)speex_alloc((st->windowSize-st->frame_size)*sizeof(spx_word16_t));
  198. st->h0_mem=(spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
  199. st->h1_mem=(spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
  200. st->window= lpc_window;
  201. st->lagWindow = lag_window;
  202. st->old_lsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
  203. st->old_qlsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
  204. st->interp_qlpc = (spx_coef_t*)speex_alloc(st->lpcSize*sizeof(spx_coef_t));
  205. st->pi_gain = (spx_word32_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
  206. st->exc_rms = (spx_word16_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word16_t));
  207. st->innov_rms_save = NULL;
  208. st->mem_sp = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
  209. st->mem_sp2 = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
  210. st->mem_sw = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
  211. for (i=0;i<st->lpcSize;i++)
  212. st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1);
  213. #ifndef DISABLE_VBR
  214. st->vbr_quality = 8;
  215. st->vbr_enabled = 0;
  216. st->vbr_max = 0;
  217. st->vbr_max_high = 20000; /* We just need a big value here */
  218. st->vad_enabled = 0;
  219. st->abr_enabled = 0;
  220. st->relative_quality=0;
  221. #endif /* #ifndef DISABLE_VBR */
  222. st->complexity=2;
  223. speex_encoder_ctl(st->st_low, SPEEX_GET_SAMPLING_RATE, &st->sampling_rate);
  224. st->sampling_rate*=2;
  225. #ifdef ENABLE_VALGRIND
  226. VALGRIND_MAKE_READABLE(st, (st->stack-(char*)st));
  227. #endif
  228. return st;
  229. }
  230. void sb_encoder_destroy(void *state)
  231. {
  232. SBEncState *st=(SBEncState*)state;
  233. speex_encoder_destroy(st->st_low);
  234. #if !(defined(VAR_ARRAYS) || defined (USE_ALLOCA))
  235. /*speex_free_scratch(st->stack);*/
  236. #endif
  237. speex_free(st->high);
  238. speex_free(st->h0_mem);
  239. speex_free(st->h1_mem);
  240. speex_free(st->old_lsp);
  241. speex_free(st->old_qlsp);
  242. speex_free(st->interp_qlpc);
  243. speex_free(st->pi_gain);
  244. speex_free(st->exc_rms);
  245. speex_free(st->mem_sp);
  246. speex_free(st->mem_sp2);
  247. speex_free(st->mem_sw);
  248. speex_free(st);
  249. }
  250. int sb_encode(void *state, void *vin, SpeexBits *bits)
  251. {
  252. SBEncState *st;
  253. int i, roots, sub;
  254. char *stack;
  255. VARDECL(spx_mem_t *mem);
  256. VARDECL(spx_sig_t *innov);
  257. VARDECL(spx_word16_t *target);
  258. VARDECL(spx_word16_t *syn_resp);
  259. VARDECL(spx_word32_t *low_pi_gain);
  260. spx_word16_t *low;
  261. spx_word16_t *high;
  262. VARDECL(spx_word16_t *low_exc_rms);
  263. VARDECL(spx_word16_t *low_innov_rms);
  264. const SpeexSBMode *mode;
  265. spx_int32_t dtx;
  266. spx_word16_t *in = (spx_word16_t*)vin;
  267. spx_word16_t e_low=0, e_high=0;
  268. VARDECL(spx_coef_t *lpc);
  269. VARDECL(spx_coef_t *interp_lpc);
  270. VARDECL(spx_coef_t *bw_lpc1);
  271. VARDECL(spx_coef_t *bw_lpc2);
  272. VARDECL(spx_lsp_t *lsp);
  273. VARDECL(spx_lsp_t *qlsp);
  274. VARDECL(spx_lsp_t *interp_lsp);
  275. VARDECL(spx_lsp_t *interp_qlsp);
  276. st = (SBEncState*)state;
  277. stack=st->stack;
  278. mode = (const SpeexSBMode*)(st->mode->mode);
  279. low = in;
  280. high = in+st->frame_size;
  281. /* High-band buffering / sync with low band */
  282. /* Compute the two sub-bands by filtering with QMF h0*/
  283. qmf_decomp(in, h0, low, high, st->full_frame_size, QMF_ORDER, st->h0_mem, stack);
  284. #ifndef DISABLE_VBR
  285. if (st->vbr_enabled || st->vad_enabled)
  286. {
  287. /* Need to compute things here before the signal is trashed by the encoder */
  288. /*FIXME: Are the two signals (low, high) in sync? */
  289. e_low = compute_rms16(low, st->frame_size);
  290. e_high = compute_rms16(high, st->frame_size);
  291. }
  292. #endif /* #ifndef DISABLE_VBR */
  293. ALLOC(low_innov_rms, st->nbSubframes, spx_word16_t);
  294. speex_encoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_rms);
  295. /* Encode the narrowband part*/
  296. speex_encode_native(st->st_low, low, bits);
  297. high = high - (st->windowSize-st->frame_size);
  298. SPEEX_COPY(high, st->high, st->windowSize-st->frame_size);
  299. SPEEX_COPY(st->high, &high[st->frame_size], st->windowSize-st->frame_size);
  300. ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
  301. ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
  302. speex_encoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
  303. speex_encoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
  304. speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, &dtx);
  305. if (dtx==0)
  306. dtx=1;
  307. else
  308. dtx=0;
  309. ALLOC(lpc, st->lpcSize, spx_coef_t);
  310. ALLOC(interp_lpc, st->lpcSize, spx_coef_t);
  311. ALLOC(bw_lpc1, st->lpcSize, spx_coef_t);
  312. ALLOC(bw_lpc2, st->lpcSize, spx_coef_t);
  313. ALLOC(lsp, st->lpcSize, spx_lsp_t);
  314. ALLOC(qlsp, st->lpcSize, spx_lsp_t);
  315. ALLOC(interp_lsp, st->lpcSize, spx_lsp_t);
  316. ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
  317. {
  318. VARDECL(spx_word16_t *autocorr);
  319. VARDECL(spx_word16_t *w_sig);
  320. ALLOC(autocorr, st->lpcSize+1, spx_word16_t);
  321. ALLOC(w_sig, st->windowSize, spx_word16_t);
  322. /* Window for analysis */
  323. /* FIXME: This is a kludge */
  324. if (st->subframeSize==80)
  325. {
  326. for (i=0;i<st->windowSize;i++)
  327. w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i>>1]),SIG_SHIFT));
  328. } else {
  329. for (i=0;i<st->windowSize;i++)
  330. w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i]),SIG_SHIFT));
  331. }
  332. /* Compute auto-correlation */
  333. _spx_autocorr(w_sig, autocorr, st->lpcSize+1, st->windowSize);
  334. autocorr[0] = ADD16(autocorr[0],MULT16_16_Q15(autocorr[0],st->lpc_floor)); /* Noise floor in auto-correlation domain */
  335. /* Lag windowing: equivalent to filtering in the power-spectrum domain */
  336. for (i=0;i<st->lpcSize+1;i++)
  337. autocorr[i] = MULT16_16_Q14(autocorr[i],st->lagWindow[i]);
  338. /* Levinson-Durbin */
  339. _spx_lpc(lpc, autocorr, st->lpcSize);
  340. }
  341. /* LPC to LSPs (x-domain) transform */
  342. roots=lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA1, stack);
  343. if (roots!=st->lpcSize)
  344. {
  345. roots = lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA2, stack);
  346. if (roots!=st->lpcSize) {
  347. /*If we can't find all LSP's, do some damage control and use a flat filter*/
  348. for (i=0;i<st->lpcSize;i++)
  349. {
  350. lsp[i]=st->old_lsp[i];
  351. }
  352. }
  353. }
  354. #ifndef DISABLE_VBR
  355. /* VBR code */
  356. if ((st->vbr_enabled || st->vad_enabled) && !dtx)
  357. {
  358. float ratio;
  359. if (st->abr_enabled)
  360. {
  361. float qual_change=0;
  362. if (st->abr_drift2 * st->abr_drift > 0)
  363. {
  364. /* Only adapt if long-term and short-term drift are the same sign */
  365. qual_change = -.00001*st->abr_drift/(1+st->abr_count);
  366. if (qual_change>.1)
  367. qual_change=.1;
  368. if (qual_change<-.1)
  369. qual_change=-.1;
  370. }
  371. st->vbr_quality += qual_change;
  372. if (st->vbr_quality>10)
  373. st->vbr_quality=10;
  374. if (st->vbr_quality<0)
  375. st->vbr_quality=0;
  376. }
  377. ratio = 2*log((1.f+e_high)/(1.f+e_low));
  378. speex_encoder_ctl(st->st_low, SPEEX_GET_RELATIVE_QUALITY, &st->relative_quality);
  379. if (ratio<-4)
  380. ratio=-4;
  381. if (ratio>2)
  382. ratio=2;
  383. /*if (ratio>-2)*/
  384. if (st->vbr_enabled)
  385. {
  386. spx_int32_t modeid;
  387. modeid = mode->nb_modes-1;
  388. st->relative_quality+=1.0*(ratio+2);
  389. if (st->relative_quality<-1)
  390. st->relative_quality=-1;
  391. while (modeid)
  392. {
  393. int v1;
  394. float thresh;
  395. v1=(int)floor(st->vbr_quality);
  396. if (v1==10)
  397. thresh = mode->vbr_thresh[modeid][v1];
  398. else
  399. thresh = (st->vbr_quality-v1) * mode->vbr_thresh[modeid][v1+1] +
  400. (1+v1-st->vbr_quality) * mode->vbr_thresh[modeid][v1];
  401. if (st->relative_quality >= thresh && st->sampling_rate*st->submodes[modeid]->bits_per_frame/st->full_frame_size <= st->vbr_max_high)
  402. break;
  403. modeid--;
  404. }
  405. speex_encoder_ctl(state, SPEEX_SET_HIGH_MODE, &modeid);
  406. if (st->abr_enabled)
  407. {
  408. spx_int32_t bitrate;
  409. speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);
  410. st->abr_drift+=(bitrate-st->abr_enabled);
  411. st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);
  412. st->abr_count += 1.0;
  413. }
  414. } else {
  415. /* VAD only */
  416. int modeid;
  417. if (st->relative_quality<2.0)
  418. modeid=1;
  419. else
  420. modeid=st->submodeSelect;
  421. /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
  422. st->submodeID=modeid;
  423. }
  424. /*fprintf (stderr, "%f %f\n", ratio, low_qual);*/
  425. }
  426. #endif /* #ifndef DISABLE_VBR */
  427. if (st->encode_submode)
  428. {
  429. speex_bits_pack(bits, 1, 1);
  430. if (dtx)
  431. speex_bits_pack(bits, 0, SB_SUBMODE_BITS);
  432. else
  433. speex_bits_pack(bits, st->submodeID, SB_SUBMODE_BITS);
  434. }
  435. /* If null mode (no transmission), just set a couple things to zero*/
  436. if (dtx || st->submodes[st->submodeID] == NULL)
  437. {
  438. for (i=0;i<st->frame_size;i++)
  439. high[i]=VERY_SMALL;
  440. for (i=0;i<st->lpcSize;i++)
  441. st->mem_sw[i]=0;
  442. st->first=1;
  443. /* Final signal synthesis from excitation */
  444. iir_mem16(high, st->interp_qlpc, high, st->frame_size, st->lpcSize, st->mem_sp, stack);
  445. if (dtx)
  446. return 0;
  447. else
  448. return 1;
  449. }
  450. /* LSP quantization */
  451. SUBMODE(lsp_quant)(lsp, qlsp, st->lpcSize, bits);
  452. if (st->first)
  453. {
  454. for (i=0;i<st->lpcSize;i++)
  455. st->old_lsp[i] = lsp[i];
  456. for (i=0;i<st->lpcSize;i++)
  457. st->old_qlsp[i] = qlsp[i];
  458. }
  459. ALLOC(mem, st->lpcSize, spx_mem_t);
  460. ALLOC(syn_resp, st->subframeSize, spx_word16_t);
  461. ALLOC(innov, st->subframeSize, spx_sig_t);
  462. ALLOC(target, st->subframeSize, spx_word16_t);
  463. for (sub=0;sub<st->nbSubframes;sub++)
  464. {
  465. VARDECL(spx_word16_t *exc);
  466. VARDECL(spx_word16_t *res);
  467. VARDECL(spx_word16_t *sw);
  468. spx_word16_t *sp;
  469. spx_word16_t filter_ratio; /*Q7*/
  470. int offset;
  471. spx_word32_t rl, rh; /*Q13*/
  472. spx_word16_t eh=0;
  473. offset = st->subframeSize*sub;
  474. sp=high+offset;
  475. ALLOC(exc, st->subframeSize, spx_word16_t);
  476. ALLOC(res, st->subframeSize, spx_word16_t);
  477. ALLOC(sw, st->subframeSize, spx_word16_t);
  478. /* LSP interpolation (quantized and unquantized) */
  479. lsp_interpolate(st->old_lsp, lsp, interp_lsp, st->lpcSize, sub, st->nbSubframes);
  480. lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes);
  481. lsp_enforce_margin(interp_lsp, st->lpcSize, LSP_MARGIN);
  482. lsp_enforce_margin(interp_qlsp, st->lpcSize, LSP_MARGIN);
  483. lsp_to_lpc(interp_lsp, interp_lpc, st->lpcSize,stack);
  484. lsp_to_lpc(interp_qlsp, st->interp_qlpc, st->lpcSize, stack);
  485. bw_lpc(st->gamma1, interp_lpc, bw_lpc1, st->lpcSize);
  486. bw_lpc(st->gamma2, interp_lpc, bw_lpc2, st->lpcSize);
  487. /* Compute mid-band (4000 Hz for wideband) response of low-band and high-band
  488. filters */
  489. st->pi_gain[sub]=LPC_SCALING;
  490. rh = LPC_SCALING;
  491. for (i=0;i<st->lpcSize;i+=2)
  492. {
  493. rh += st->interp_qlpc[i+1] - st->interp_qlpc[i];
  494. st->pi_gain[sub] += st->interp_qlpc[i] + st->interp_qlpc[i+1];
  495. }
  496. rl = low_pi_gain[sub];
  497. #ifdef FIXED_POINT
  498. filter_ratio=EXTRACT16(SATURATE(PDIV32(SHL32(ADD32(rl,82),7),ADD32(82,rh)),32767));
  499. #else
  500. filter_ratio=(rl+.01)/(rh+.01);
  501. #endif
  502. /* Compute "real excitation" */
  503. fir_mem16(sp, st->interp_qlpc, exc, st->subframeSize, st->lpcSize, st->mem_sp2, stack);
  504. /* Compute energy of low-band and high-band excitation */
  505. eh = compute_rms16(exc, st->subframeSize);
  506. if (!SUBMODE(innovation_quant)) {/* 1 for spectral folding excitation, 0 for stochastic */
  507. spx_word32_t g; /*Q7*/
  508. spx_word16_t el; /*Q0*/
  509. el = low_innov_rms[sub];
  510. /* Gain to use if we want to use the low-band excitation for high-band */
  511. g=PDIV32(MULT16_16(filter_ratio,eh),EXTEND32(ADD16(1,el)));
  512. #if 0
  513. {
  514. char *tmp_stack=stack;
  515. float *tmp_sig;
  516. float g2;
  517. ALLOC(tmp_sig, st->subframeSize, spx_sig_t);
  518. for (i=0;i<st->lpcSize;i++)
  519. mem[i]=st->mem_sp[i];
  520. iir_mem2(st->low_innov+offset, st->interp_qlpc, tmp_sig, st->subframeSize, st->lpcSize, mem);
  521. g2 = compute_rms(sp, st->subframeSize)/(.01+compute_rms(tmp_sig, st->subframeSize));
  522. /*fprintf (stderr, "gains: %f %f\n", g, g2);*/
  523. g = g2;
  524. stack = tmp_stack;
  525. }
  526. #endif
  527. /*print_vec(&g, 1, "gain factor");*/
  528. /* Gain quantization */
  529. {
  530. int quant = scal_quant(g, fold_quant_bound, 32);
  531. /*speex_warning_int("tata", quant);*/
  532. if (quant<0)
  533. quant=0;
  534. if (quant>31)
  535. quant=31;
  536. speex_bits_pack(bits, quant, 5);
  537. }
  538. if (st->innov_rms_save)
  539. {
  540. st->innov_rms_save[sub] = eh;
  541. }
  542. st->exc_rms[sub] = eh;
  543. } else {
  544. spx_word16_t gc; /*Q7*/
  545. spx_word32_t scale; /*Q14*/
  546. spx_word16_t el; /*Q0*/
  547. el = low_exc_rms[sub]; /*Q0*/
  548. gc = PDIV32_16(MULT16_16(filter_ratio,1+eh),1+el);
  549. /* This is a kludge that cleans up a historical bug */
  550. if (st->subframeSize==80)
  551. gc = MULT16_16_P15(QCONST16(0.70711f,15),gc);
  552. /*printf ("%f %f %f %f\n", el, eh, filter_ratio, gc);*/
  553. {
  554. int qgc = scal_quant(gc, gc_quant_bound, 16);
  555. speex_bits_pack(bits, qgc, 4);
  556. gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
  557. }
  558. if (st->subframeSize==80)
  559. gc = MULT16_16_P14(QCONST16(1.4142f,14), gc);
  560. scale = SHL32(MULT16_16(PDIV32_16(SHL32(EXTEND32(gc),SIG_SHIFT-6),filter_ratio),(1+el)),6);
  561. compute_impulse_response(st->interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, st->subframeSize, st->lpcSize, stack);
  562. /* Reset excitation */
  563. for (i=0;i<st->subframeSize;i++)
  564. res[i]=VERY_SMALL;
  565. /* Compute zero response (ringing) of A(z/g1) / ( A(z/g2) * Aq(z) ) */
  566. for (i=0;i<st->lpcSize;i++)
  567. mem[i]=st->mem_sp[i];
  568. iir_mem16(res, st->interp_qlpc, res, st->subframeSize, st->lpcSize, mem, stack);
  569. for (i=0;i<st->lpcSize;i++)
  570. mem[i]=st->mem_sw[i];
  571. filter_mem16(res, bw_lpc1, bw_lpc2, res, st->subframeSize, st->lpcSize, mem, stack);
  572. /* Compute weighted signal */
  573. for (i=0;i<st->lpcSize;i++)
  574. mem[i]=st->mem_sw[i];
  575. filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, mem, stack);
  576. /* Compute target signal */
  577. for (i=0;i<st->subframeSize;i++)
  578. target[i]=SUB16(sw[i],res[i]);
  579. signal_div(target, target, scale, st->subframeSize);
  580. /* Reset excitation */
  581. SPEEX_MEMSET(innov, 0, st->subframeSize);
  582. /*print_vec(target, st->subframeSize, "\ntarget");*/
  583. SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
  584. SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
  585. innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
  586. /*print_vec(target, st->subframeSize, "after");*/
  587. signal_mul(innov, innov, scale, st->subframeSize);
  588. if (SUBMODE(double_codebook)) {
  589. char *tmp_stack=stack;
  590. VARDECL(spx_sig_t *innov2);
  591. ALLOC(innov2, st->subframeSize, spx_sig_t);
  592. SPEEX_MEMSET(innov2, 0, st->subframeSize);
  593. for (i=0;i<st->subframeSize;i++)
  594. target[i]=MULT16_16_P13(QCONST16(2.5f,13), target[i]);
  595. SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
  596. SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
  597. innov2, syn_resp, bits, stack, st->complexity, 0);
  598. signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
  599. for (i=0;i<st->subframeSize;i++)
  600. innov[i] = ADD32(innov[i],innov2[i]);
  601. stack = tmp_stack;
  602. }
  603. for (i=0;i<st->subframeSize;i++)
  604. exc[i] = PSHR32(innov[i],SIG_SHIFT);
  605. if (st->innov_rms_save)
  606. {
  607. st->innov_rms_save[sub] = MULT16_16_Q15(QCONST16(.70711f, 15), compute_rms(innov, st->subframeSize));
  608. }
  609. st->exc_rms[sub] = compute_rms16(exc, st->subframeSize);
  610. }
  611. /*Keep the previous memory*/
  612. for (i=0;i<st->lpcSize;i++)
  613. mem[i]=st->mem_sp[i];
  614. /* Final signal synthesis from excitation */
  615. iir_mem16(exc, st->interp_qlpc, sp, st->subframeSize, st->lpcSize, st->mem_sp, stack);
  616. /* Compute weighted signal again, from synthesized speech (not sure it's the right thing) */
  617. filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw, stack);
  618. }
  619. for (i=0;i<st->lpcSize;i++)
  620. st->old_lsp[i] = lsp[i];
  621. for (i=0;i<st->lpcSize;i++)
  622. st->old_qlsp[i] = qlsp[i];
  623. st->first=0;
  624. return 1;
  625. }
  626. void *sb_decoder_init(const SpeexMode *m)
  627. {
  628. spx_int32_t tmp;
  629. SBDecState *st;
  630. const SpeexSBMode *mode;
  631. st = (SBDecState*)speex_alloc(sizeof(SBDecState));
  632. if (!st)
  633. return NULL;
  634. st->mode = m;
  635. mode=(const SpeexSBMode*)m->mode;
  636. st->encode_submode = 1;
  637. st->st_low = speex_decoder_init(mode->nb_mode);
  638. #if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
  639. st->stack = NULL;
  640. #else
  641. /*st->stack = (char*)speex_alloc_scratch(SB_DEC_STACK);*/
  642. speex_decoder_ctl(st->st_low, SPEEX_GET_STACK, &st->stack);
  643. #endif
  644. st->full_frame_size = 2*mode->frameSize;
  645. st->frame_size = mode->frameSize;
  646. st->subframeSize = mode->subframeSize;
  647. st->nbSubframes = mode->frameSize/mode->subframeSize;
  648. st->lpcSize=mode->lpcSize;
  649. speex_decoder_ctl(st->st_low, SPEEX_GET_SAMPLING_RATE, &st->sampling_rate);
  650. st->sampling_rate*=2;
  651. tmp=1;
  652. speex_decoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, &tmp);
  653. st->submodes=mode->submodes;
  654. st->submodeID=mode->defaultSubmode;
  655. st->first=1;
  656. st->g0_mem = (spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
  657. st->g1_mem = (spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
  658. st->excBuf = (spx_word16_t*)speex_alloc((st->subframeSize)*sizeof(spx_word16_t));
  659. st->old_qlsp = (spx_lsp_t*)speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));
  660. st->interp_qlpc = (spx_coef_t*)speex_alloc(st->lpcSize*sizeof(spx_coef_t));
  661. st->pi_gain = (spx_word32_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
  662. st->exc_rms = (spx_word16_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word16_t));
  663. st->mem_sp = (spx_mem_t*)speex_alloc((2*st->lpcSize)*sizeof(spx_mem_t));
  664. st->innov_save = NULL;
  665. st->lpc_enh_enabled=0;
  666. st->seed = 1000;
  667. #ifdef ENABLE_VALGRIND
  668. VALGRIND_MAKE_READABLE(st, (st->stack-(char*)st));
  669. #endif
  670. return st;
  671. }
  672. void sb_decoder_destroy(void *state)
  673. {
  674. SBDecState *st;
  675. st = (SBDecState*)state;
  676. speex_decoder_destroy(st->st_low);
  677. #if !(defined(VAR_ARRAYS) || defined (USE_ALLOCA))
  678. /*speex_free_scratch(st->stack);*/
  679. #endif
  680. speex_free(st->g0_mem);
  681. speex_free(st->g1_mem);
  682. speex_free(st->excBuf);
  683. speex_free(st->old_qlsp);
  684. speex_free(st->interp_qlpc);
  685. speex_free(st->pi_gain);
  686. speex_free(st->exc_rms);
  687. speex_free(st->mem_sp);
  688. speex_free(state);
  689. }
  690. static void sb_decode_lost(SBDecState *st, spx_word16_t *out, int dtx, char *stack)
  691. {
  692. int i;
  693. int saved_modeid=0;
  694. if (dtx)
  695. {
  696. saved_modeid=st->submodeID;
  697. st->submodeID=1;
  698. } else {
  699. bw_lpc(QCONST16(0.99f,15), st->interp_qlpc, st->interp_qlpc, st->lpcSize);
  700. }
  701. st->first=1;
  702. /* Final signal synthesis from excitation */
  703. if (!dtx)
  704. {
  705. st->last_ener = MULT16_16_Q15(QCONST16(.9f,15),st->last_ener);
  706. }
  707. for (i=0;i<st->frame_size;i++)
  708. out[i+st->frame_size] = speex_rand(st->last_ener, &st->seed);
  709. iir_mem16(out+st->frame_size, st->interp_qlpc, out+st->frame_size, st->frame_size, st->lpcSize,
  710. st->mem_sp, stack);
  711. /* Reconstruct the original */
  712. qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
  713. if (dtx)
  714. {
  715. st->submodeID=saved_modeid;
  716. }
  717. return;
  718. }
  719. int sb_decode(void *state, SpeexBits *bits, void *vout)
  720. {
  721. int i, sub;
  722. SBDecState *st;
  723. int wideband;
  724. int ret;
  725. char *stack;
  726. VARDECL(spx_word32_t *low_pi_gain);
  727. VARDECL(spx_word16_t *low_exc_rms);
  728. VARDECL(spx_coef_t *ak);
  729. VARDECL(spx_lsp_t *qlsp);
  730. VARDECL(spx_lsp_t *interp_qlsp);
  731. spx_int32_t dtx;
  732. const SpeexSBMode *mode;
  733. spx_word16_t *out = (spx_word16_t*)vout;
  734. spx_word16_t *low_innov_alias;
  735. spx_word32_t exc_ener_sum = 0;
  736. st = (SBDecState*)state;
  737. stack=st->stack;
  738. mode = (const SpeexSBMode*)(st->mode->mode);
  739. low_innov_alias = out+st->frame_size;
  740. speex_decoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_alias);
  741. /* Decode the low-band */
  742. ret = speex_decode_native(st->st_low, bits, out);
  743. speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, &dtx);
  744. /* If error decoding the narrowband part, propagate error */
  745. if (ret!=0)
  746. {
  747. return ret;
  748. }
  749. if (!bits)
  750. {
  751. sb_decode_lost(st, out, dtx, stack);
  752. return 0;
  753. }
  754. if (st->encode_submode)
  755. {
  756. /*Check "wideband bit"*/
  757. if (speex_bits_remaining(bits)>0)
  758. wideband = speex_bits_peek(bits);
  759. else
  760. wideband = 0;
  761. if (wideband)
  762. {
  763. /*Regular wideband frame, read the submode*/
  764. wideband = speex_bits_unpack_unsigned(bits, 1);
  765. st->submodeID = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
  766. } else
  767. {
  768. /*Was a narrowband frame, set "null submode"*/
  769. st->submodeID = 0;
  770. }
  771. if (st->submodeID != 0 && st->submodes[st->submodeID] == NULL)
  772. {
  773. speex_notify("Invalid mode encountered. The stream is corrupted.");
  774. return -2;
  775. }
  776. }
  777. /* If null mode (no transmission), just set a couple things to zero*/
  778. if (st->submodes[st->submodeID] == NULL)
  779. {
  780. if (dtx)
  781. {
  782. sb_decode_lost(st, out, 1, stack);
  783. return 0;
  784. }
  785. for (i=0;i<st->frame_size;i++)
  786. out[st->frame_size+i]=VERY_SMALL;
  787. st->first=1;
  788. /* Final signal synthesis from excitation */
  789. iir_mem16(out+st->frame_size, st->interp_qlpc, out+st->frame_size, st->frame_size, st->lpcSize, st->mem_sp, stack);
  790. qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
  791. return 0;
  792. }
  793. ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
  794. ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
  795. speex_decoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
  796. speex_decoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
  797. ALLOC(qlsp, st->lpcSize, spx_lsp_t);
  798. ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
  799. SUBMODE(lsp_unquant)(qlsp, st->lpcSize, bits);
  800. if (st->first)
  801. {
  802. for (i=0;i<st->lpcSize;i++)
  803. st->old_qlsp[i] = qlsp[i];
  804. }
  805. ALLOC(ak, st->lpcSize, spx_coef_t);
  806. for (sub=0;sub<st->nbSubframes;sub++)
  807. {
  808. VARDECL(spx_word32_t *exc);
  809. spx_word16_t *innov_save=NULL;
  810. spx_word16_t *sp;
  811. spx_word16_t filter_ratio;
  812. spx_word16_t el=0;
  813. int offset;
  814. spx_word32_t rl=0,rh=0;
  815. offset = st->subframeSize*sub;
  816. sp=out+st->frame_size+offset;
  817. ALLOC(exc, st->subframeSize, spx_word32_t);
  818. /* Pointer for saving innovation */
  819. if (st->innov_save)
  820. {
  821. innov_save = st->innov_save+2*offset;
  822. SPEEX_MEMSET(innov_save, 0, 2*st->subframeSize);
  823. }
  824. /* LSP interpolation */
  825. lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes);
  826. lsp_enforce_margin(interp_qlsp, st->lpcSize, LSP_MARGIN);
  827. /* LSP to LPC */
  828. lsp_to_lpc(interp_qlsp, ak, st->lpcSize, stack);
  829. /* Calculate reponse ratio between the low and high filter in the middle
  830. of the band (4000 Hz) */
  831. st->pi_gain[sub]=LPC_SCALING;
  832. rh = LPC_SCALING;
  833. for (i=0;i<st->lpcSize;i+=2)
  834. {
  835. rh += ak[i+1] - ak[i];
  836. st->pi_gain[sub] += ak[i] + ak[i+1];
  837. }
  838. rl = low_pi_gain[sub];
  839. #ifdef FIXED_POINT
  840. filter_ratio=EXTRACT16(SATURATE(PDIV32(SHL32(ADD32(rl,82),7),ADD32(82,rh)),32767));
  841. #else
  842. filter_ratio=(rl+.01)/(rh+.01);
  843. #endif
  844. SPEEX_MEMSET(exc, 0, st->subframeSize);
  845. if (!SUBMODE(innovation_unquant))
  846. {
  847. spx_word32_t g;
  848. int quant;
  849. quant = speex_bits_unpack_unsigned(bits, 5);
  850. g= spx_exp(MULT16_16(QCONST16(.125f,11),(quant-10)));
  851. g = PDIV32(g, filter_ratio);
  852. for (i=0;i<st->subframeSize;i+=2)
  853. {
  854. exc[i]=SHL32(MULT16_32_P15(MULT16_16_Q15(mode->folding_gain,low_innov_alias[offset+i]),SHL32(g,6)),SIG_SHIFT);
  855. exc[i+1]=NEG32(SHL32(MULT16_32_P15(MULT16_16_Q15(mode->folding_gain,low_innov_alias[offset+i+1]),SHL32(g,6)),SIG_SHIFT));
  856. }
  857. } else {
  858. spx_word16_t gc;
  859. spx_word32_t scale;
  860. int qgc = speex_bits_unpack_unsigned(bits, 4);
  861. el = low_exc_rms[sub];
  862. gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
  863. if (st->subframeSize==80)
  864. gc = MULT16_16_P14(QCONST16(1.4142f,14),gc);
  865. scale = SHL32(PDIV32(SHL32(MULT16_16(gc, el),3), filter_ratio),SIG_SHIFT-3);
  866. SUBMODE(innovation_unquant)(exc, SUBMODE(innovation_params), st->subframeSize,
  867. bits, stack, &st->seed);
  868. signal_mul(exc,exc,scale,st->subframeSize);
  869. if (SUBMODE(double_codebook)) {
  870. char *tmp_stack=stack;
  871. VARDECL(spx_sig_t *innov2);
  872. ALLOC(innov2, st->subframeSize, spx_sig_t);
  873. SPEEX_MEMSET(innov2, 0, st->subframeSize);
  874. SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), st->subframeSize,
  875. bits, stack, &st->seed);
  876. signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
  877. for (i=0;i<st->subframeSize;i++)
  878. exc[i] = ADD32(exc[i],innov2[i]);
  879. stack = tmp_stack;
  880. }
  881. }
  882. if (st->innov_save)
  883. {
  884. for (i=0;i<st->subframeSize;i++)
  885. innov_save[2*i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
  886. }
  887. iir_mem16(st->excBuf, st->interp_qlpc, sp, st->subframeSize, st->lpcSize,
  888. st->mem_sp, stack);
  889. for (i=0;i<st->subframeSize;i++)
  890. st->excBuf[i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
  891. for (i=0;i<st->lpcSize;i++)
  892. st->interp_qlpc[i] = ak[i];
  893. st->exc_rms[sub] = compute_rms16(st->excBuf, st->subframeSize);
  894. exc_ener_sum = ADD32(exc_ener_sum, DIV32(MULT16_16(st->exc_rms[sub],st->exc_rms[sub]), st->nbSubframes));
  895. }
  896. st->last_ener = spx_sqrt(exc_ener_sum);
  897. qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
  898. for (i=0;i<st->lpcSize;i++)
  899. st->old_qlsp[i] = qlsp[i];
  900. st->first=0;
  901. return 0;
  902. }
  903. int sb_encoder_ctl(void *state, int request, void *ptr)
  904. {
  905. SBEncState *st;
  906. st=(SBEncState*)state;
  907. switch(request)
  908. {
  909. case SPEEX_GET_FRAME_SIZE:
  910. (*(spx_int32_t*)ptr) = st->full_frame_size;
  911. break;
  912. case SPEEX_SET_HIGH_MODE:
  913. st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr);
  914. break;
  915. case SPEEX_SET_LOW_MODE:
  916. speex_encoder_ctl(st->st_low, SPEEX_SET_LOW_MODE, ptr);
  917. break;
  918. case SPEEX_SET_DTX:
  919. speex_encoder_ctl(st->st_low, SPEEX_SET_DTX, ptr);
  920. break;
  921. case SPEEX_GET_DTX:
  922. speex_encoder_ctl(st->st_low, SPEEX_GET_DTX, ptr);
  923. break;
  924. case SPEEX_GET_LOW_MODE:
  925. speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, ptr);
  926. break;
  927. case SPEEX_SET_MODE:
  928. speex_encoder_ctl(st, SPEEX_SET_QUALITY, ptr);
  929. break;
  930. #ifndef DISABLE_VBR
  931. case SPEEX_SET_VBR:
  932. st->vbr_enabled = (*(spx_int32_t*)ptr);
  933. speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, ptr);
  934. break;
  935. case SPEEX_GET_VBR:
  936. (*(spx_int32_t*)ptr) = st->vbr_enabled;
  937. break;
  938. case SPEEX_SET_VAD:
  939. st->vad_enabled = (*(spx_int32_t*)ptr);
  940. speex_encoder_ctl(st->st_low, SPEEX_SET_VAD, ptr);
  941. break;
  942. case SPEEX_GET_VAD:
  943. (*(spx_int32_t*)ptr) = st->vad_enabled;
  944. break;
  945. #endif /* #ifndef DISABLE_VBR */
  946. #if !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API)
  947. case SPEEX_SET_VBR_QUALITY:
  948. {
  949. spx_int32_t q;
  950. float qual = (*(float*)ptr)+.6;
  951. st->vbr_quality = (*(float*)ptr);
  952. if (qual>10)
  953. qual=10;
  954. q=(int)floor(.5+*(float*)ptr);
  955. if (q>10)
  956. q=10;
  957. speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_QUALITY, &qual);
  958. speex_encoder_ctl(state, SPEEX_SET_QUALITY, &q);
  959. break;
  960. }
  961. case SPEEX_GET_VBR_QUALITY:
  962. (*(float*)ptr) = st->vbr_quality;
  963. break;
  964. #endif /* #if !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API) */
  965. #ifndef DISABLE_VBR
  966. case SPEEX_SET_ABR:
  967. st->abr_enabled = (*(spx_int32_t*)ptr);
  968. st->vbr_enabled = st->abr_enabled!=0;
  969. speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, &st->vbr_enabled);
  970. if (st->vbr_enabled)
  971. {
  972. spx_int32_t i=10, rate, target;
  973. float vbr_qual;
  974. target = (*(spx_int32_t*)ptr);
  975. while (i>=0)
  976. {
  977. speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
  978. speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
  979. if (rate <= target)
  980. break;
  981. i--;
  982. }
  983. vbr_qual=i;
  984. if (vbr_qual<0)
  985. vbr_qual=0;
  986. speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual);
  987. st->abr_count=0;
  988. st->abr_drift=0;
  989. st->abr_drift2=0;
  990. }
  991. break;
  992. case SPEEX_GET_ABR:
  993. (*(spx_int32_t*)ptr) = st->abr_enabled;
  994. break;
  995. #endif /* #ifndef DISABLE_VBR */
  996. case SPEEX_SET_QUALITY:
  997. {
  998. spx_int32_t nb_qual;
  999. int quality = (*(spx_int32_t*)ptr);
  1000. if (quality < 0)
  1001. quality = 0;
  1002. if (quality > 10)
  1003. quality = 10;
  1004. st->submodeSelect = st->submodeID = ((const SpeexSBMode*)(st->mode->mode))->quality_map[quality];
  1005. nb_qual = ((const SpeexSBMode*)(st->mode->mode))->low_quality_map[quality];
  1006. speex_encoder_ctl(st->st_low, SPEEX_SET_MODE, &nb_qual);
  1007. }
  1008. break;
  1009. case SPEEX_SET_COMPLEXITY:
  1010. speex_encoder_ctl(st->st_low, SPEEX_SET_COMPLEXITY, ptr);
  1011. st->complexity = (*(spx_int32_t*)ptr);
  1012. if (st->complexity<1)
  1013. st->complexity=1;
  1014. break;
  1015. case SPEEX_GET_COMPLEXITY:
  1016. (*(spx_int32_t*)ptr) = st->complexity;
  1017. break;
  1018. case SPEEX_SET_BITRATE:
  1019. {
  1020. spx_int32_t i=10;
  1021. spx_int32_t rate, target;
  1022. target = (*(spx_int32_t*)ptr);
  1023. while (i>=0)
  1024. {
  1025. speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
  1026. speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
  1027. if (rate <= target)
  1028. break;
  1029. i--;
  1030. }
  1031. }
  1032. break;
  1033. case SPEEX_GET_BITRATE:
  1034. speex_encoder_ctl(st->st_low, request, ptr);
  1035. /*fprintf (stderr, "before: %d\n", (*(int*)ptr));*/
  1036. if (st->submodes[st->submodeID])
  1037. (*(spx_int32_t*)ptr) += st->sampling_rate*SUBMODE(bits_per_frame)/st->full_frame_size;
  1038. else
  1039. (*(spx_int32_t*)ptr) += st->sampling_rate*(SB_SUBMODE_BITS+1)/st->full_frame_size;
  1040. /*fprintf (stderr, "after: %d\n", (*(int*)ptr));*/
  1041. break;
  1042. case SPEEX_SET_SAMPLING_RATE:
  1043. {
  1044. spx_int32_t tmp=(*(spx_int32_t*)ptr);
  1045. st->sampling_rate = tmp;
  1046. tmp>>=1;
  1047. speex_encoder_ctl(st->st_low, SPEEX_SET_SAMPLING_RATE, &tmp);
  1048. }
  1049. break;
  1050. case SPEEX_GET_SAMPLING_RATE:
  1051. (*(spx_int32_t*)ptr)=st->sampling_rate;
  1052. break;
  1053. case SPEEX_RESET_STATE:
  1054. {
  1055. int i;
  1056. st->first = 1;
  1057. for (i=0;i<st->lpcSize;i++)
  1058. st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1);
  1059. for (i=0;i<st->lpcSize;i++)
  1060. st->mem_sw[i]=st->mem_sp[i]=st->mem_sp2[i]=0;
  1061. for (i=0;i<QMF_ORDER;i++)
  1062. st->h0_mem[i]=st->h1_mem[i]=0;
  1063. }
  1064. break;
  1065. case SPEEX_SET_SUBMODE_ENCODING:
  1066. st->encode_submode = (*(spx_int32_t*)ptr);
  1067. speex_encoder_ctl(st->st_low, SPEEX_SET_SUBMODE_ENCODING, ptr);
  1068. break;
  1069. case SPEEX_GET_SUBMODE_ENCODING:
  1070. (*(spx_int32_t*)ptr) = st->encode_submode;
  1071. break;
  1072. case SPEEX_GET_LOOKAHEAD:
  1073. speex_encoder_ctl(st->st_low, SPEEX_GET_LOOKAHEAD, ptr);
  1074. (*(spx_int32_t*)ptr) = 2*(*(spx_int32_t*)ptr) + QMF_ORDER - 1;
  1075. break;
  1076. case SPEEX_SET_PLC_TUNING:
  1077. speex_encoder_ctl(st->st_low, SPEEX_SET_PLC_TUNING, ptr);
  1078. break;
  1079. case SPEEX_GET_PLC_TUNING:
  1080. speex_encoder_ctl(st->st_low, SPEEX_GET_PLC_TUNING, ptr);
  1081. break;
  1082. #ifndef DISABLE_VBR
  1083. case SPEEX_SET_VBR_MAX_BITRATE:
  1084. {
  1085. st->vbr_max = (*(spx_int32_t*)ptr);
  1086. if (SPEEX_SET_VBR_MAX_BITRATE<1)
  1087. {
  1088. speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_MAX_BITRATE, &st->vbr_max);
  1089. st->vbr_max_high = 17600;
  1090. } else {
  1091. spx_int32_t low_rate;
  1092. if (st->vbr_max >= 42200)
  1093. {
  1094. st->vbr_max_high = 17600;
  1095. } else if (st->vbr_max >= 27800)
  1096. {
  1097. st->vbr_max_high = 9600;
  1098. } else if (st->vbr_max > 20600)
  1099. {
  1100. st->vbr_max_high = 5600;
  1101. } else {
  1102. st->vbr_max_high = 1800;
  1103. }
  1104. if (st->subframeSize==80)
  1105. st->vbr_max_high = 1800;
  1106. low_rate = st->vbr_max - st->vbr_max_high;
  1107. speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_MAX_BITRATE, &low_rate);
  1108. }
  1109. }
  1110. break;
  1111. case SPEEX_GET_VBR_MAX_BITRATE:
  1112. (*(spx_int32_t*)ptr) = st->vbr_max;
  1113. break;
  1114. #endif /* #ifndef DISABLE_VBR */
  1115. case SPEEX_SET_HIGHPASS:
  1116. speex_encoder_ctl(st->st_low, SPEEX_SET_HIGHPASS, ptr);
  1117. break;
  1118. case SPEEX_GET_HIGHPASS:
  1119. speex_encoder_ctl(st->st_low, SPEEX_GET_HIGHPASS, ptr);
  1120. break;
  1121. /* This is all internal stuff past this point */
  1122. case SPEEX_GET_PI_GAIN:
  1123. {
  1124. int i;
  1125. spx_word32_t *g = (spx_word32_t*)ptr;
  1126. for (i=0;i<st->nbSubframes;i++)
  1127. g[i]=st->pi_gain[i];
  1128. }
  1129. break;
  1130. case SPEEX_GET_EXC:
  1131. {
  1132. int i;
  1133. for (i=0;i<st->nbSubframes;i++)
  1134. ((spx_word16_t*)ptr)[i] = st->exc_rms[i];
  1135. }
  1136. break;
  1137. #ifndef DISABLE_VBR
  1138. case SPEEX_GET_RELATIVE_QUALITY:
  1139. (*(float*)ptr)=st->relative_quality;
  1140. break;
  1141. #endif /* #ifndef DISABLE_VBR */
  1142. case SPEEX_SET_INNOVATION_SAVE:
  1143. st->innov_rms_save = (spx_word16_t*)ptr;
  1144. break;
  1145. case SPEEX_SET_WIDEBAND:
  1146. speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, ptr);
  1147. break;
  1148. case SPEEX_GET_STACK:
  1149. *((char**)ptr) = st->stack;
  1150. break;
  1151. default:
  1152. speex_warning_int("Unknown nb_ctl request: ", request);
  1153. return -1;
  1154. }
  1155. return 0;
  1156. }
  1157. int sb_decoder_ctl(void *state, int request, void *ptr)
  1158. {
  1159. SBDecState *st;
  1160. st=(SBDecState*)state;
  1161. switch(request)
  1162. {
  1163. case SPEEX_SET_HIGH_MODE:
  1164. st->submodeID = (*(spx_int32_t*)ptr);
  1165. break;
  1166. case SPEEX_SET_LOW_MODE:
  1167. speex_decoder_ctl(st->st_low, SPEEX_SET_LOW_MODE, ptr);
  1168. break;
  1169. case SPEEX_GET_LOW_MODE:
  1170. speex_decoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, ptr);
  1171. break;
  1172. case SPEEX_GET_FRAME_SIZE:
  1173. (*(spx_int32_t*)ptr) = st->full_frame_size;
  1174. break;
  1175. case SPEEX_SET_ENH:
  1176. speex_decoder_ctl(st->st_low, request, ptr);
  1177. st->lpc_enh_enabled = *((spx_int32_t*)ptr);
  1178. break;
  1179. case SPEEX_GET_ENH:
  1180. *((spx_int32_t*)ptr) = st->lpc_enh_enabled;
  1181. break;
  1182. case SPEEX_SET_MODE:
  1183. case SPEEX_SET_QUALITY:
  1184. {
  1185. spx_int32_t nb_qual;
  1186. int quality = (*(spx_int32_t*)ptr);
  1187. if (quality < 0)
  1188. quality = 0;
  1189. if (quality > 10)
  1190. quality = 10;
  1191. st->submodeID = ((const SpeexSBMode*)(st->mode->mode))->quality_map[quality];
  1192. nb_qual = ((const SpeexSBMode*)(st->mode->mode))->low_quality_map[quality];
  1193. speex_decoder_ctl(st->st_low, SPEEX_SET_MODE, &nb_qual);
  1194. }
  1195. break;
  1196. case SPEEX_GET_BITRATE:
  1197. speex_decoder_ctl(st->st_low, request, ptr);
  1198. if (st->submodes[st->submodeID])
  1199. (*(spx_int32_t*)ptr) += st->sampling_rate*SUBMODE(bits_per_frame)/st->full_frame_size;
  1200. else
  1201. (*(spx_int32_t*)ptr) += st->sampling_rate*(SB_SUBMODE_BITS+1)/st->full_frame_size;
  1202. break;
  1203. case SPEEX_SET_SAMPLING_RATE:
  1204. {
  1205. spx_int32_t tmp=(*(spx_int32_t*)ptr);
  1206. st->sampling_rate = tmp;
  1207. tmp>>=1;
  1208. speex_decoder_ctl(st->st_low, SPEEX_SET_SAMPLING_RATE, &tmp);
  1209. }
  1210. break;
  1211. case SPEEX_GET_SAMPLING_RATE:
  1212. (*(spx_int32_t*)ptr)=st->sampling_rate;
  1213. break;
  1214. case SPEEX_SET_HANDLER:
  1215. speex_decoder_ctl(st->st_low, SPEEX_SET_HANDLER, ptr);
  1216. break;
  1217. case SPEEX_SET_USER_HANDLER:
  1218. speex_decoder_ctl(st->st_low, SPEEX_SET_USER_HANDLER, ptr);
  1219. break;
  1220. case SPEEX_RESET_STATE:
  1221. {
  1222. int i;
  1223. for (i=0;i<2*st->lpcSize;i++)
  1224. st->mem_sp[i]=0;
  1225. for (i=0;i<QMF_ORDER;i++)
  1226. st->g0_mem[i]=st->g1_mem[i]=0;
  1227. st->last_ener=0;
  1228. }
  1229. break;
  1230. case SPEEX_SET_SUBMODE_ENCODING:
  1231. st->encode_submode = (*(spx_int32_t*)ptr);
  1232. speex_decoder_ctl(st->st_low, SPEEX_SET_SUBMODE_ENCODING, ptr);
  1233. break;
  1234. case SPEEX_GET_SUBMODE_ENCODING:
  1235. (*(spx_int32_t*)ptr) = st->encode_submode;
  1236. break;
  1237. case SPEEX_GET_LOOKAHEAD:
  1238. speex_decoder_ctl(st->st_low, SPEEX_GET_LOOKAHEAD, ptr);
  1239. (*(spx_int32_t*)ptr) = 2*(*(spx_int32_t*)ptr);
  1240. break;
  1241. case SPEEX_SET_HIGHPASS:
  1242. speex_decoder_ctl(st->st_low, SPEEX_SET_HIGHPASS, ptr);
  1243. break;
  1244. case SPEEX_GET_HIGHPASS:
  1245. speex_decoder_ctl(st->st_low, SPEEX_GET_HIGHPASS, ptr);
  1246. break;
  1247. case SPEEX_GET_ACTIVITY:
  1248. speex_decoder_ctl(st->st_low, SPEEX_GET_ACTIVITY, ptr);
  1249. break;
  1250. case SPEEX_GET_PI_GAIN:
  1251. {
  1252. int i;
  1253. spx_word32_t *g = (spx_word32_t*)ptr;
  1254. for (i=0;i<st->nbSubframes;i++)
  1255. g[i]=st->pi_gain[i];
  1256. }
  1257. break;
  1258. case SPEEX_GET_EXC:
  1259. {
  1260. int i;
  1261. for (i=0;i<st->nbSubframes;i++)
  1262. ((spx_word16_t*)ptr)[i] = st->exc_rms[i];
  1263. }
  1264. break;
  1265. case SPEEX_GET_DTX_STATUS:
  1266. speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, ptr);
  1267. break;
  1268. case SPEEX_SET_INNOVATION_SAVE:
  1269. st->innov_save = (spx_word16_t*)ptr;
  1270. break;
  1271. case SPEEX_SET_WIDEBAND:
  1272. speex_decoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, ptr);
  1273. break;
  1274. case SPEEX_GET_STACK:
  1275. *((char**)ptr) = st->stack;
  1276. break;
  1277. default:
  1278. speex_warning_int("Unknown nb_ctl request: ", request);
  1279. return -1;
  1280. }
  1281. return 0;
  1282. }
  1283. #endif