nb_celp.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903
  1. /* Copyright (C) 2002-2006 Jean-Marc Valin
  2. File: nb_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 "nb_celp.h"
  31. #include "lpc.h"
  32. #include "lsp.h"
  33. #include "ltp.h"
  34. #include "quant_lsp.h"
  35. #include "cb_search.h"
  36. #include "filters.h"
  37. #include "stack_alloc.h"
  38. #include "vq.h"
  39. #include <speex/speex_bits.h>
  40. #include "vbr.h"
  41. #include "arch.h"
  42. #include "math_approx.h"
  43. #include "os_support.h"
  44. #include <speex/speex_callbacks.h>
  45. #ifdef VORBIS_PSYCHO
  46. #include "vorbis_psy.h"
  47. #endif
  48. #ifndef M_PI
  49. #define M_PI 3.14159265358979323846 /* pi */
  50. #endif
  51. #ifndef NULL
  52. #define NULL 0
  53. #endif
  54. #define SUBMODE(x) st->submodes[st->submodeID]->x
  55. /* Default size for the encoder and decoder stack (can be changed at compile time).
  56. This does not apply when using variable-size arrays or alloca. */
  57. #ifndef NB_ENC_STACK
  58. #define NB_ENC_STACK (8000*sizeof(spx_sig_t))
  59. #endif
  60. #ifndef NB_DEC_STACK
  61. #define NB_DEC_STACK (4000*sizeof(spx_sig_t))
  62. #endif
  63. #ifdef FIXED_POINT
  64. const spx_word32_t ol_gain_table[32]={18900, 25150, 33468, 44536, 59265, 78865, 104946, 139653, 185838, 247297, 329081, 437913, 582736, 775454, 1031906, 1373169, 1827293, 2431601, 3235761, 4305867, 5729870, 7624808, 10146425, 13501971, 17967238, 23909222, 31816294, 42338330, 56340132, 74972501, 99766822, 132760927};
  65. const spx_word16_t exc_gain_quant_scal3_bound[7]={1841, 3883, 6051, 8062, 10444, 13580, 18560};
  66. const spx_word16_t exc_gain_quant_scal3[8]={1002, 2680, 5086, 7016, 9108, 11781, 15380, 21740};
  67. const spx_word16_t exc_gain_quant_scal1_bound[1]={14385};
  68. const spx_word16_t exc_gain_quant_scal1[2]={11546, 17224};
  69. #define LSP_MARGIN 16
  70. #define LSP_DELTA1 6553
  71. #define LSP_DELTA2 1638
  72. #else
  73. const float exc_gain_quant_scal3_bound[7]={0.112338f, 0.236980f, 0.369316f, 0.492054f, 0.637471f, 0.828874f, 1.132784f};
  74. const float exc_gain_quant_scal3[8]={0.061130f, 0.163546f, 0.310413f, 0.428220f, 0.555887f, 0.719055f, 0.938694f, 1.326874f};
  75. const float exc_gain_quant_scal1_bound[1]={0.87798f};
  76. const float exc_gain_quant_scal1[2]={0.70469f, 1.05127f};
  77. #define LSP_MARGIN .002f
  78. #define LSP_DELTA1 .2f
  79. #define LSP_DELTA2 .05f
  80. #endif
  81. #ifdef VORBIS_PSYCHO
  82. #define EXTRA_BUFFER 100
  83. #else
  84. #define EXTRA_BUFFER 0
  85. #endif
  86. #define sqr(x) ((x)*(x))
  87. extern const spx_word16_t lag_window[];
  88. extern const spx_word16_t lpc_window[];
  89. void *nb_encoder_init(const SpeexMode *m)
  90. {
  91. EncState *st;
  92. const SpeexNBMode *mode;
  93. int i;
  94. mode=(const SpeexNBMode *)m->mode;
  95. st = (EncState*)speex_alloc(sizeof(EncState));
  96. if (!st)
  97. return NULL;
  98. #if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
  99. st->stack = NULL;
  100. #else
  101. st->stack = (char*)speex_alloc_scratch(NB_ENC_STACK);
  102. #endif
  103. st->mode=m;
  104. st->frameSize = mode->frameSize;
  105. st->nbSubframes=mode->frameSize/mode->subframeSize;
  106. st->subframeSize=mode->subframeSize;
  107. st->windowSize = st->frameSize+st->subframeSize;
  108. st->lpcSize = mode->lpcSize;
  109. st->gamma1=mode->gamma1;
  110. st->gamma2=mode->gamma2;
  111. st->min_pitch=mode->pitchStart;
  112. st->max_pitch=mode->pitchEnd;
  113. st->lpc_floor = mode->lpc_floor;
  114. st->submodes=mode->submodes;
  115. st->submodeID=st->submodeSelect=mode->defaultSubmode;
  116. st->bounded_pitch = 1;
  117. st->encode_submode = 1;
  118. #ifdef VORBIS_PSYCHO
  119. st->psy = vorbis_psy_init(8000, 256);
  120. st->curve = (float*)speex_alloc(128*sizeof(float));
  121. st->old_curve = (float*)speex_alloc(128*sizeof(float));
  122. st->psy_window = (float*)speex_alloc(256*sizeof(float));
  123. #endif
  124. st->cumul_gain = 1024;
  125. /* Allocating input buffer */
  126. st->winBuf = (spx_word16_t*)speex_alloc((st->windowSize-st->frameSize)*sizeof(spx_word16_t));
  127. /* Allocating excitation buffer */
  128. st->excBuf = (spx_word16_t*)speex_alloc((mode->frameSize+mode->pitchEnd+2)*sizeof(spx_word16_t));
  129. st->exc = st->excBuf + mode->pitchEnd + 2;
  130. st->swBuf = (spx_word16_t*)speex_alloc((mode->frameSize+mode->pitchEnd+2)*sizeof(spx_word16_t));
  131. st->sw = st->swBuf + mode->pitchEnd + 2;
  132. st->window= lpc_window;
  133. /* Create the window for autocorrelation (lag-windowing) */
  134. st->lagWindow = lag_window;
  135. st->old_lsp = (spx_lsp_t*)speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));
  136. st->old_qlsp = (spx_lsp_t*)speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));
  137. st->first = 1;
  138. for (i=0;i<st->lpcSize;i++)
  139. st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1);
  140. st->mem_sp = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
  141. st->mem_sw = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
  142. st->mem_sw_whole = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
  143. st->mem_exc = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
  144. st->mem_exc2 = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
  145. st->pi_gain = (spx_word32_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
  146. st->innov_rms_save = NULL;
  147. st->pitch = (int*)speex_alloc((st->nbSubframes)*sizeof(int));
  148. #ifndef DISABLE_VBR
  149. st->vbr = (VBRState*)speex_alloc(sizeof(VBRState));
  150. vbr_init(st->vbr);
  151. st->vbr_quality = 8;
  152. st->vbr_enabled = 0;
  153. st->vbr_max = 0;
  154. st->vad_enabled = 0;
  155. st->dtx_enabled = 0;
  156. st->dtx_count=0;
  157. st->abr_enabled = 0;
  158. st->abr_drift = 0;
  159. st->abr_drift2 = 0;
  160. #endif /* #ifndef DISABLE_VBR */
  161. st->plc_tuning = 2;
  162. st->complexity=2;
  163. st->sampling_rate=8000;
  164. st->isWideband = 0;
  165. st->highpass_enabled = 1;
  166. #ifdef ENABLE_VALGRIND
  167. VALGRIND_MAKE_READABLE(st, NB_ENC_STACK);
  168. #endif
  169. return st;
  170. }
  171. void nb_encoder_destroy(void *state)
  172. {
  173. EncState *st=(EncState *)state;
  174. /* Free all allocated memory */
  175. #if !(defined(VAR_ARRAYS) || defined (USE_ALLOCA))
  176. speex_free_scratch(st->stack);
  177. #endif
  178. speex_free (st->winBuf);
  179. speex_free (st->excBuf);
  180. speex_free (st->old_qlsp);
  181. speex_free (st->swBuf);
  182. speex_free (st->old_lsp);
  183. speex_free (st->mem_sp);
  184. speex_free (st->mem_sw);
  185. speex_free (st->mem_sw_whole);
  186. speex_free (st->mem_exc);
  187. speex_free (st->mem_exc2);
  188. speex_free (st->pi_gain);
  189. speex_free (st->pitch);
  190. #ifndef DISABLE_VBR
  191. vbr_destroy(st->vbr);
  192. speex_free (st->vbr);
  193. #endif /* #ifndef DISABLE_VBR */
  194. #ifdef VORBIS_PSYCHO
  195. vorbis_psy_destroy(st->psy);
  196. speex_free (st->curve);
  197. speex_free (st->old_curve);
  198. speex_free (st->psy_window);
  199. #endif
  200. /*Free state memory... should be last*/
  201. speex_free(st);
  202. }
  203. int nb_encode(void *state, void *vin, SpeexBits *bits)
  204. {
  205. EncState *st;
  206. int i, sub, roots;
  207. int ol_pitch;
  208. spx_word16_t ol_pitch_coef;
  209. spx_word32_t ol_gain;
  210. VARDECL(spx_word16_t *ringing);
  211. VARDECL(spx_word16_t *target);
  212. VARDECL(spx_sig_t *innov);
  213. VARDECL(spx_word32_t *exc32);
  214. VARDECL(spx_mem_t *mem);
  215. VARDECL(spx_coef_t *bw_lpc1);
  216. VARDECL(spx_coef_t *bw_lpc2);
  217. VARDECL(spx_coef_t *lpc);
  218. VARDECL(spx_lsp_t *lsp);
  219. VARDECL(spx_lsp_t *qlsp);
  220. VARDECL(spx_lsp_t *interp_lsp);
  221. VARDECL(spx_lsp_t *interp_qlsp);
  222. VARDECL(spx_coef_t *interp_lpc);
  223. VARDECL(spx_coef_t *interp_qlpc);
  224. char *stack;
  225. VARDECL(spx_word16_t *syn_resp);
  226. VARDECL(spx_word16_t *real_exc);
  227. spx_word32_t ener=0;
  228. spx_word16_t fine_gain;
  229. spx_word16_t *in = (spx_word16_t*)vin;
  230. st=(EncState *)state;
  231. stack=st->stack;
  232. ALLOC(lpc, st->lpcSize, spx_coef_t);
  233. ALLOC(bw_lpc1, st->lpcSize, spx_coef_t);
  234. ALLOC(bw_lpc2, st->lpcSize, spx_coef_t);
  235. ALLOC(lsp, st->lpcSize, spx_lsp_t);
  236. ALLOC(qlsp, st->lpcSize, spx_lsp_t);
  237. ALLOC(interp_lsp, st->lpcSize, spx_lsp_t);
  238. ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
  239. ALLOC(interp_lpc, st->lpcSize, spx_coef_t);
  240. ALLOC(interp_qlpc, st->lpcSize, spx_coef_t);
  241. /* Move signals 1 frame towards the past */
  242. SPEEX_MOVE(st->excBuf, st->excBuf+st->frameSize, st->max_pitch+2);
  243. SPEEX_MOVE(st->swBuf, st->swBuf+st->frameSize, st->max_pitch+2);
  244. if (st->highpass_enabled)
  245. highpass(in, in, st->frameSize, (st->isWideband?HIGHPASS_WIDEBAND:HIGHPASS_NARROWBAND)|HIGHPASS_INPUT, st->mem_hp);
  246. {
  247. VARDECL(spx_word16_t *w_sig);
  248. VARDECL(spx_word16_t *autocorr);
  249. ALLOC(w_sig, st->windowSize, spx_word16_t);
  250. ALLOC(autocorr, st->lpcSize+1, spx_word16_t);
  251. /* Window for analysis */
  252. for (i=0;i<st->windowSize-st->frameSize;i++)
  253. w_sig[i] = EXTRACT16(SHR32(MULT16_16(st->winBuf[i],st->window[i]),SIG_SHIFT));
  254. for (;i<st->windowSize;i++)
  255. w_sig[i] = EXTRACT16(SHR32(MULT16_16(in[i-st->windowSize+st->frameSize],st->window[i]),SIG_SHIFT));
  256. /* Compute auto-correlation */
  257. _spx_autocorr(w_sig, autocorr, st->lpcSize+1, st->windowSize);
  258. autocorr[0] = ADD16(autocorr[0],MULT16_16_Q15(autocorr[0],st->lpc_floor)); /* Noise floor in auto-correlation domain */
  259. /* Lag windowing: equivalent to filtering in the power-spectrum domain */
  260. for (i=0;i<st->lpcSize+1;i++)
  261. autocorr[i] = MULT16_16_Q14(autocorr[i],st->lagWindow[i]);
  262. /* Levinson-Durbin */
  263. _spx_lpc(lpc, autocorr, st->lpcSize);
  264. /* LPC to LSPs (x-domain) transform */
  265. roots=lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA1, stack);
  266. /* Check if we found all the roots */
  267. if (roots!=st->lpcSize)
  268. {
  269. /*If we can't find all LSP's, do some damage control and use previous filter*/
  270. for (i=0;i<st->lpcSize;i++)
  271. {
  272. lsp[i]=st->old_lsp[i];
  273. }
  274. }
  275. }
  276. /* Whole frame analysis (open-loop estimation of pitch and excitation gain) */
  277. {
  278. int diff = st->windowSize-st->frameSize;
  279. if (st->first)
  280. for (i=0;i<st->lpcSize;i++)
  281. interp_lsp[i] = lsp[i];
  282. else
  283. lsp_interpolate(st->old_lsp, lsp, interp_lsp, st->lpcSize, st->nbSubframes, st->nbSubframes<<1);
  284. lsp_enforce_margin(interp_lsp, st->lpcSize, LSP_MARGIN);
  285. /* Compute interpolated LPCs (unquantized) for whole frame*/
  286. lsp_to_lpc(interp_lsp, interp_lpc, st->lpcSize,stack);
  287. /*Open-loop pitch*/
  288. if (!st->submodes[st->submodeID] || (st->complexity>2 && SUBMODE(have_subframe_gain)<3) || SUBMODE(forced_pitch_gain) || SUBMODE(lbr_pitch) != -1
  289. #ifndef DISABLE_VBR
  290. || st->vbr_enabled || st->vad_enabled
  291. #endif
  292. )
  293. {
  294. int nol_pitch[6];
  295. spx_word16_t nol_pitch_coef[6];
  296. bw_lpc(st->gamma1, interp_lpc, bw_lpc1, st->lpcSize);
  297. bw_lpc(st->gamma2, interp_lpc, bw_lpc2, st->lpcSize);
  298. SPEEX_COPY(st->sw, st->winBuf, diff);
  299. SPEEX_COPY(st->sw+diff, in, st->frameSize-diff);
  300. filter_mem16(st->sw, bw_lpc1, bw_lpc2, st->sw, st->frameSize, st->lpcSize, st->mem_sw_whole, stack);
  301. open_loop_nbest_pitch(st->sw, st->min_pitch, st->max_pitch, st->frameSize,
  302. nol_pitch, nol_pitch_coef, 6, stack);
  303. ol_pitch=nol_pitch[0];
  304. ol_pitch_coef = nol_pitch_coef[0];
  305. /*Try to remove pitch multiples*/
  306. for (i=1;i<6;i++)
  307. {
  308. #ifdef FIXED_POINT
  309. if ((nol_pitch_coef[i]>MULT16_16_Q15(nol_pitch_coef[0],27853)) &&
  310. #else
  311. if ((nol_pitch_coef[i]>.85*nol_pitch_coef[0]) &&
  312. #endif
  313. (ABS(2*nol_pitch[i]-ol_pitch)<=2 || ABS(3*nol_pitch[i]-ol_pitch)<=3 ||
  314. ABS(4*nol_pitch[i]-ol_pitch)<=4 || ABS(5*nol_pitch[i]-ol_pitch)<=5))
  315. {
  316. /*ol_pitch_coef=nol_pitch_coef[i];*/
  317. ol_pitch = nol_pitch[i];
  318. }
  319. }
  320. /*if (ol_pitch>50)
  321. ol_pitch/=2;*/
  322. /*ol_pitch_coef = sqrt(ol_pitch_coef);*/
  323. } else {
  324. ol_pitch=0;
  325. ol_pitch_coef=0;
  326. }
  327. /*Compute "real" excitation*/
  328. SPEEX_COPY(st->exc, st->winBuf, diff);
  329. SPEEX_COPY(st->exc+diff, in, st->frameSize-diff);
  330. fir_mem16(st->exc, interp_lpc, st->exc, st->frameSize, st->lpcSize, st->mem_exc, stack);
  331. /* Compute open-loop excitation gain */
  332. {
  333. spx_word16_t g = compute_rms16(st->exc, st->frameSize);
  334. if (st->submodeID!=1 && ol_pitch>0)
  335. ol_gain = MULT16_16(g, MULT16_16_Q14(QCONST16(1.1,14),
  336. spx_sqrt(QCONST32(1.,28)-MULT16_32_Q15(QCONST16(.8,15),SHL32(MULT16_16(ol_pitch_coef,ol_pitch_coef),16)))));
  337. else
  338. ol_gain = SHL32(EXTEND32(g),SIG_SHIFT);
  339. }
  340. }
  341. #ifdef VORBIS_PSYCHO
  342. SPEEX_MOVE(st->psy_window, st->psy_window+st->frameSize, 256-st->frameSize);
  343. SPEEX_COPY(&st->psy_window[256-st->frameSize], in, st->frameSize);
  344. compute_curve(st->psy, st->psy_window, st->curve);
  345. /*print_vec(st->curve, 128, "curve");*/
  346. if (st->first)
  347. SPEEX_COPY(st->old_curve, st->curve, 128);
  348. #endif
  349. /*VBR stuff*/
  350. #ifndef DISABLE_VBR
  351. if (st->vbr && (st->vbr_enabled||st->vad_enabled))
  352. {
  353. float lsp_dist=0;
  354. for (i=0;i<st->lpcSize;i++)
  355. lsp_dist += (st->old_lsp[i] - lsp[i])*(st->old_lsp[i] - lsp[i]);
  356. lsp_dist /= LSP_SCALING*LSP_SCALING;
  357. if (st->abr_enabled)
  358. {
  359. float qual_change=0;
  360. if (st->abr_drift2 * st->abr_drift > 0)
  361. {
  362. /* Only adapt if long-term and short-term drift are the same sign */
  363. qual_change = -.00001*st->abr_drift/(1+st->abr_count);
  364. if (qual_change>.05)
  365. qual_change=.05;
  366. if (qual_change<-.05)
  367. qual_change=-.05;
  368. }
  369. st->vbr_quality += qual_change;
  370. if (st->vbr_quality>10)
  371. st->vbr_quality=10;
  372. if (st->vbr_quality<0)
  373. st->vbr_quality=0;
  374. }
  375. st->relative_quality = vbr_analysis(st->vbr, in, st->frameSize, ol_pitch, GAIN_SCALING_1*ol_pitch_coef);
  376. /*if (delta_qual<0)*/
  377. /* delta_qual*=.1*(3+st->vbr_quality);*/
  378. if (st->vbr_enabled)
  379. {
  380. spx_int32_t mode;
  381. int choice=0;
  382. float min_diff=100;
  383. mode = 8;
  384. while (mode)
  385. {
  386. int v1;
  387. float thresh;
  388. v1=(int)floor(st->vbr_quality);
  389. if (v1==10)
  390. thresh = vbr_nb_thresh[mode][v1];
  391. else
  392. thresh = (st->vbr_quality-v1)*vbr_nb_thresh[mode][v1+1] + (1+v1-st->vbr_quality)*vbr_nb_thresh[mode][v1];
  393. if (st->relative_quality > thresh &&
  394. st->relative_quality-thresh<min_diff)
  395. {
  396. choice = mode;
  397. min_diff = st->relative_quality-thresh;
  398. }
  399. mode--;
  400. }
  401. mode=choice;
  402. if (mode==0)
  403. {
  404. if (st->dtx_count==0 || lsp_dist>.05 || !st->dtx_enabled || st->dtx_count>20)
  405. {
  406. mode=1;
  407. st->dtx_count=1;
  408. } else {
  409. mode=0;
  410. st->dtx_count++;
  411. }
  412. } else {
  413. st->dtx_count=0;
  414. }
  415. speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);
  416. if (st->vbr_max>0)
  417. {
  418. spx_int32_t rate;
  419. speex_encoder_ctl(state, SPEEX_GET_BITRATE, &rate);
  420. if (rate > st->vbr_max)
  421. {
  422. rate = st->vbr_max;
  423. speex_encoder_ctl(state, SPEEX_SET_BITRATE, &rate);
  424. }
  425. }
  426. if (st->abr_enabled)
  427. {
  428. spx_int32_t bitrate;
  429. speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);
  430. st->abr_drift+=(bitrate-st->abr_enabled);
  431. st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);
  432. st->abr_count += 1.0;
  433. }
  434. } else {
  435. /*VAD only case*/
  436. int mode;
  437. if (st->relative_quality<2)
  438. {
  439. if (st->dtx_count==0 || lsp_dist>.05 || !st->dtx_enabled || st->dtx_count>20)
  440. {
  441. st->dtx_count=1;
  442. mode=1;
  443. } else {
  444. mode=0;
  445. st->dtx_count++;
  446. }
  447. } else {
  448. st->dtx_count = 0;
  449. mode=st->submodeSelect;
  450. }
  451. /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
  452. st->submodeID=mode;
  453. }
  454. } else {
  455. st->relative_quality = -1;
  456. }
  457. #endif /* #ifndef DISABLE_VBR */
  458. if (st->encode_submode)
  459. {
  460. /* First, transmit a zero for narrowband */
  461. speex_bits_pack(bits, 0, 1);
  462. /* Transmit the sub-mode we use for this frame */
  463. speex_bits_pack(bits, st->submodeID, NB_SUBMODE_BITS);
  464. }
  465. /* If null mode (no transmission), just set a couple things to zero*/
  466. if (st->submodes[st->submodeID] == NULL)
  467. {
  468. for (i=0;i<st->frameSize;i++)
  469. st->exc[i]=st->sw[i]=VERY_SMALL;
  470. for (i=0;i<st->lpcSize;i++)
  471. st->mem_sw[i]=0;
  472. st->first=1;
  473. st->bounded_pitch = 1;
  474. SPEEX_COPY(st->winBuf, in+2*st->frameSize-st->windowSize, st->windowSize-st->frameSize);
  475. /* Clear memory (no need to really compute it) */
  476. for (i=0;i<st->lpcSize;i++)
  477. st->mem_sp[i] = 0;
  478. return 0;
  479. }
  480. /* LSP Quantization */
  481. if (st->first)
  482. {
  483. for (i=0;i<st->lpcSize;i++)
  484. st->old_lsp[i] = lsp[i];
  485. }
  486. /*Quantize LSPs*/
  487. #if 1 /*0 for unquantized*/
  488. SUBMODE(lsp_quant)(lsp, qlsp, st->lpcSize, bits);
  489. #else
  490. for (i=0;i<st->lpcSize;i++)
  491. qlsp[i]=lsp[i];
  492. #endif
  493. /*If we use low bit-rate pitch mode, transmit open-loop pitch*/
  494. if (SUBMODE(lbr_pitch)!=-1)
  495. {
  496. speex_bits_pack(bits, ol_pitch-st->min_pitch, 7);
  497. }
  498. if (SUBMODE(forced_pitch_gain))
  499. {
  500. int quant;
  501. /* This just damps the pitch a bit, because it tends to be too aggressive when forced */
  502. ol_pitch_coef = MULT16_16_Q15(QCONST16(.9,15), ol_pitch_coef);
  503. #ifdef FIXED_POINT
  504. quant = PSHR16(MULT16_16_16(15, ol_pitch_coef),GAIN_SHIFT);
  505. #else
  506. quant = (int)floor(.5+15*ol_pitch_coef*GAIN_SCALING_1);
  507. #endif
  508. if (quant>15)
  509. quant=15;
  510. if (quant<0)
  511. quant=0;
  512. speex_bits_pack(bits, quant, 4);
  513. ol_pitch_coef=MULT16_16_P15(QCONST16(0.066667,15),SHL16(quant,GAIN_SHIFT));
  514. }
  515. /*Quantize and transmit open-loop excitation gain*/
  516. #ifdef FIXED_POINT
  517. {
  518. int qe = scal_quant32(ol_gain, ol_gain_table, 32);
  519. /*ol_gain = exp(qe/3.5)*SIG_SCALING;*/
  520. ol_gain = MULT16_32_Q15(28406,ol_gain_table[qe]);
  521. speex_bits_pack(bits, qe, 5);
  522. }
  523. #else
  524. {
  525. int qe = (int)(floor(.5+3.5*log(ol_gain*1.0/SIG_SCALING)));
  526. if (qe<0)
  527. qe=0;
  528. if (qe>31)
  529. qe=31;
  530. ol_gain = exp(qe/3.5)*SIG_SCALING;
  531. speex_bits_pack(bits, qe, 5);
  532. }
  533. #endif
  534. /* Special case for first frame */
  535. if (st->first)
  536. {
  537. for (i=0;i<st->lpcSize;i++)
  538. st->old_qlsp[i] = qlsp[i];
  539. }
  540. /* Target signal */
  541. ALLOC(target, st->subframeSize, spx_word16_t);
  542. ALLOC(innov, st->subframeSize, spx_sig_t);
  543. ALLOC(exc32, st->subframeSize, spx_word32_t);
  544. ALLOC(ringing, st->subframeSize, spx_word16_t);
  545. ALLOC(syn_resp, st->subframeSize, spx_word16_t);
  546. ALLOC(real_exc, st->subframeSize, spx_word16_t);
  547. ALLOC(mem, st->lpcSize, spx_mem_t);
  548. /* Loop on sub-frames */
  549. for (sub=0;sub<st->nbSubframes;sub++)
  550. {
  551. int offset;
  552. spx_word16_t *sw;
  553. spx_word16_t *exc;
  554. int pitch;
  555. int response_bound = st->subframeSize;
  556. /* Offset relative to start of frame */
  557. offset = st->subframeSize*sub;
  558. /* Excitation */
  559. exc=st->exc+offset;
  560. /* Weighted signal */
  561. sw=st->sw+offset;
  562. /* LSP interpolation (quantized and unquantized) */
  563. lsp_interpolate(st->old_lsp, lsp, interp_lsp, st->lpcSize, sub, st->nbSubframes);
  564. lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes);
  565. /* Make sure the filters are stable */
  566. lsp_enforce_margin(interp_lsp, st->lpcSize, LSP_MARGIN);
  567. lsp_enforce_margin(interp_qlsp, st->lpcSize, LSP_MARGIN);
  568. /* Compute interpolated LPCs (quantized and unquantized) */
  569. lsp_to_lpc(interp_lsp, interp_lpc, st->lpcSize,stack);
  570. lsp_to_lpc(interp_qlsp, interp_qlpc, st->lpcSize, stack);
  571. /* Compute analysis filter gain at w=pi (for use in SB-CELP) */
  572. {
  573. spx_word32_t pi_g=LPC_SCALING;
  574. for (i=0;i<st->lpcSize;i+=2)
  575. {
  576. /*pi_g += -st->interp_qlpc[i] + st->interp_qlpc[i+1];*/
  577. pi_g = ADD32(pi_g, SUB32(EXTEND32(interp_qlpc[i+1]),EXTEND32(interp_qlpc[i])));
  578. }
  579. st->pi_gain[sub] = pi_g;
  580. }
  581. #ifdef VORBIS_PSYCHO
  582. {
  583. float curr_curve[128];
  584. float fact = ((float)sub+1.0f)/st->nbSubframes;
  585. for (i=0;i<128;i++)
  586. curr_curve[i] = (1.0f-fact)*st->old_curve[i] + fact*st->curve[i];
  587. curve_to_lpc(st->psy, curr_curve, bw_lpc1, bw_lpc2, 10);
  588. }
  589. #else
  590. /* Compute bandwidth-expanded (unquantized) LPCs for perceptual weighting */
  591. bw_lpc(st->gamma1, interp_lpc, bw_lpc1, st->lpcSize);
  592. if (st->gamma2>=0)
  593. bw_lpc(st->gamma2, interp_lpc, bw_lpc2, st->lpcSize);
  594. else
  595. {
  596. for (i=0;i<st->lpcSize;i++)
  597. bw_lpc2[i]=0;
  598. }
  599. /*print_vec(st->bw_lpc1, 10, "bw_lpc");*/
  600. #endif
  601. /*FIXME: This will break if we change the window size */
  602. speex_assert(st->windowSize-st->frameSize == st->subframeSize);
  603. if (sub==0)
  604. {
  605. for (i=0;i<st->subframeSize;i++)
  606. real_exc[i] = sw[i] = st->winBuf[i];
  607. } else {
  608. for (i=0;i<st->subframeSize;i++)
  609. real_exc[i] = sw[i] = in[i+((sub-1)*st->subframeSize)];
  610. }
  611. fir_mem16(real_exc, interp_qlpc, real_exc, st->subframeSize, st->lpcSize, st->mem_exc2, stack);
  612. if (st->complexity==0)
  613. response_bound >>= 1;
  614. compute_impulse_response(interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, response_bound, st->lpcSize, stack);
  615. for (i=response_bound;i<st->subframeSize;i++)
  616. syn_resp[i]=VERY_SMALL;
  617. /* Compute zero response of A(z/g1) / ( A(z/g2) * A(z) ) */
  618. for (i=0;i<st->lpcSize;i++)
  619. mem[i]=SHL32(st->mem_sp[i],1);
  620. for (i=0;i<st->subframeSize;i++)
  621. ringing[i] = VERY_SMALL;
  622. #ifdef SHORTCUTS2
  623. iir_mem16(ringing, interp_qlpc, ringing, response_bound, st->lpcSize, mem, stack);
  624. for (i=0;i<st->lpcSize;i++)
  625. mem[i]=SHL32(st->mem_sw[i],1);
  626. filter_mem16(ringing, st->bw_lpc1, st->bw_lpc2, ringing, response_bound, st->lpcSize, mem, stack);
  627. SPEEX_MEMSET(&ringing[response_bound], 0, st->subframeSize-response_bound);
  628. #else
  629. iir_mem16(ringing, interp_qlpc, ringing, st->subframeSize, st->lpcSize, mem, stack);
  630. for (i=0;i<st->lpcSize;i++)
  631. mem[i]=SHL32(st->mem_sw[i],1);
  632. filter_mem16(ringing, bw_lpc1, bw_lpc2, ringing, st->subframeSize, st->lpcSize, mem, stack);
  633. #endif
  634. /* Compute weighted signal */
  635. for (i=0;i<st->lpcSize;i++)
  636. mem[i]=st->mem_sw[i];
  637. filter_mem16(sw, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, mem, stack);
  638. if (st->complexity==0)
  639. for (i=0;i<st->lpcSize;i++)
  640. st->mem_sw[i]=mem[i];
  641. /* Compute target signal (saturation prevents overflows on clipped input speech) */
  642. for (i=0;i<st->subframeSize;i++)
  643. target[i]=EXTRACT16(SATURATE(SUB32(sw[i],PSHR32(ringing[i],1)),32767));
  644. /* Reset excitation */
  645. SPEEX_MEMSET(exc, 0, st->subframeSize);
  646. /* If we have a long-term predictor (otherwise, something's wrong) */
  647. speex_assert (SUBMODE(ltp_quant));
  648. {
  649. int pit_min, pit_max;
  650. /* Long-term prediction */
  651. if (SUBMODE(lbr_pitch) != -1)
  652. {
  653. /* Low bit-rate pitch handling */
  654. int margin;
  655. margin = SUBMODE(lbr_pitch);
  656. if (margin)
  657. {
  658. if (ol_pitch < st->min_pitch+margin-1)
  659. ol_pitch=st->min_pitch+margin-1;
  660. if (ol_pitch > st->max_pitch-margin)
  661. ol_pitch=st->max_pitch-margin;
  662. pit_min = ol_pitch-margin+1;
  663. pit_max = ol_pitch+margin;
  664. } else {
  665. pit_min=pit_max=ol_pitch;
  666. }
  667. } else {
  668. pit_min = st->min_pitch;
  669. pit_max = st->max_pitch;
  670. }
  671. /* Force pitch to use only the current frame if needed */
  672. if (st->bounded_pitch && pit_max>offset)
  673. pit_max=offset;
  674. /* Perform pitch search */
  675. pitch = SUBMODE(ltp_quant)(target, sw, interp_qlpc, bw_lpc1, bw_lpc2,
  676. exc32, SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef,
  677. st->lpcSize, st->subframeSize, bits, stack,
  678. exc, syn_resp, st->complexity, 0, st->plc_tuning, &st->cumul_gain);
  679. st->pitch[sub]=pitch;
  680. }
  681. /* Quantization of innovation */
  682. SPEEX_MEMSET(innov, 0, st->subframeSize);
  683. /* FIXME: Make sure this is save from overflows (so far so good) */
  684. for (i=0;i<st->subframeSize;i++)
  685. real_exc[i] = EXTRACT16(SUB32(EXTEND32(real_exc[i]), PSHR32(exc32[i],SIG_SHIFT-1)));
  686. ener = SHL32(EXTEND32(compute_rms16(real_exc, st->subframeSize)),SIG_SHIFT);
  687. /*FIXME: Should use DIV32_16 and make sure result fits in 16 bits */
  688. #ifdef FIXED_POINT
  689. {
  690. spx_word32_t f = PDIV32(ener,PSHR32(ol_gain,SIG_SHIFT));
  691. if (f<=32767)
  692. fine_gain = f;
  693. else
  694. fine_gain = 32767;
  695. }
  696. #else
  697. fine_gain = PDIV32_16(ener,PSHR32(ol_gain,SIG_SHIFT));
  698. #endif
  699. /* Calculate gain correction for the sub-frame (if any) */
  700. if (SUBMODE(have_subframe_gain))
  701. {
  702. int qe;
  703. if (SUBMODE(have_subframe_gain)==3)
  704. {
  705. qe = scal_quant(fine_gain, exc_gain_quant_scal3_bound, 8);
  706. speex_bits_pack(bits, qe, 3);
  707. ener=MULT16_32_Q14(exc_gain_quant_scal3[qe],ol_gain);
  708. } else {
  709. qe = scal_quant(fine_gain, exc_gain_quant_scal1_bound, 2);
  710. speex_bits_pack(bits, qe, 1);
  711. ener=MULT16_32_Q14(exc_gain_quant_scal1[qe],ol_gain);
  712. }
  713. } else {
  714. ener=ol_gain;
  715. }
  716. /*printf ("%f %f\n", ener, ol_gain);*/
  717. /* Normalize innovation */
  718. signal_div(target, target, ener, st->subframeSize);
  719. /* Quantize innovation */
  720. speex_assert (SUBMODE(innovation_quant));
  721. {
  722. /* Codebook search */
  723. SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2,
  724. SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
  725. innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
  726. /* De-normalize innovation and update excitation */
  727. signal_mul(innov, innov, ener, st->subframeSize);
  728. for (i=0;i<st->subframeSize;i++)
  729. exc[i] = EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767));
  730. /* In some (rare) modes, we do a second search (more bits) to reduce noise even more */
  731. if (SUBMODE(double_codebook)) {
  732. char *tmp_stack=stack;
  733. VARDECL(spx_sig_t *innov2);
  734. ALLOC(innov2, st->subframeSize, spx_sig_t);
  735. SPEEX_MEMSET(innov2, 0, st->subframeSize);
  736. for (i=0;i<st->subframeSize;i++)
  737. target[i]=MULT16_16_P13(QCONST16(2.2f,13), target[i]);
  738. SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2,
  739. SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
  740. innov2, syn_resp, bits, stack, st->complexity, 0);
  741. signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545f,15),ener), st->subframeSize);
  742. for (i=0;i<st->subframeSize;i++)
  743. innov[i] = ADD32(innov[i],innov2[i]);
  744. stack = tmp_stack;
  745. }
  746. for (i=0;i<st->subframeSize;i++)
  747. exc[i] = EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767));
  748. if (st->innov_rms_save)
  749. {
  750. st->innov_rms_save[sub] = compute_rms(innov, st->subframeSize);
  751. }
  752. }
  753. /* Final signal synthesis from excitation */
  754. iir_mem16(exc, interp_qlpc, sw, st->subframeSize, st->lpcSize, st->mem_sp, stack);
  755. /* Compute weighted signal again, from synthesized speech (not sure it's the right thing) */
  756. if (st->complexity!=0)
  757. filter_mem16(sw, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw, stack);
  758. }
  759. /* Store the LSPs for interpolation in the next frame */
  760. if (st->submodeID>=1)
  761. {
  762. for (i=0;i<st->lpcSize;i++)
  763. st->old_lsp[i] = lsp[i];
  764. for (i=0;i<st->lpcSize;i++)
  765. st->old_qlsp[i] = qlsp[i];
  766. }
  767. #ifdef VORBIS_PSYCHO
  768. if (st->submodeID>=1)
  769. SPEEX_COPY(st->old_curve, st->curve, 128);
  770. #endif
  771. if (st->submodeID==1)
  772. {
  773. #ifndef DISABLE_VBR
  774. if (st->dtx_count)
  775. speex_bits_pack(bits, 15, 4);
  776. else
  777. #endif
  778. speex_bits_pack(bits, 0, 4);
  779. }
  780. /* The next frame will not be the first (Duh!) */
  781. st->first = 0;
  782. SPEEX_COPY(st->winBuf, in+2*st->frameSize-st->windowSize, st->windowSize-st->frameSize);
  783. if (SUBMODE(innovation_quant) == noise_codebook_quant || st->submodeID==0)
  784. st->bounded_pitch = 1;
  785. else
  786. st->bounded_pitch = 0;
  787. return 1;
  788. }
  789. void *nb_decoder_init(const SpeexMode *m)
  790. {
  791. DecState *st;
  792. const SpeexNBMode *mode;
  793. int i;
  794. mode=(const SpeexNBMode*)m->mode;
  795. st = (DecState *)speex_alloc(sizeof(DecState));
  796. if (!st)
  797. return NULL;
  798. #if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
  799. st->stack = NULL;
  800. #else
  801. st->stack = (char*)speex_alloc_scratch(NB_DEC_STACK);
  802. #endif
  803. st->mode=m;
  804. st->encode_submode = 1;
  805. st->first=1;
  806. /* Codec parameters, should eventually have several "modes"*/
  807. st->frameSize = mode->frameSize;
  808. st->nbSubframes=mode->frameSize/mode->subframeSize;
  809. st->subframeSize=mode->subframeSize;
  810. st->lpcSize = mode->lpcSize;
  811. st->min_pitch=mode->pitchStart;
  812. st->max_pitch=mode->pitchEnd;
  813. st->submodes=mode->submodes;
  814. st->submodeID=mode->defaultSubmode;
  815. st->lpc_enh_enabled=1;
  816. st->excBuf = (spx_word16_t*)speex_alloc((st->frameSize + 2*st->max_pitch + st->subframeSize + 12)*sizeof(spx_word16_t));
  817. st->exc = st->excBuf + 2*st->max_pitch + st->subframeSize + 6;
  818. SPEEX_MEMSET(st->excBuf, 0, st->frameSize + st->max_pitch);
  819. st->interp_qlpc = (spx_coef_t*)speex_alloc(st->lpcSize*sizeof(spx_coef_t));
  820. st->old_qlsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
  821. st->mem_sp = (spx_mem_t*)speex_alloc(st->lpcSize*sizeof(spx_mem_t));
  822. st->pi_gain = (spx_word32_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
  823. st->last_pitch = 40;
  824. st->count_lost=0;
  825. st->pitch_gain_buf[0] = st->pitch_gain_buf[1] = st->pitch_gain_buf[2] = 0;
  826. st->pitch_gain_buf_idx = 0;
  827. st->seed = 1000;
  828. st->sampling_rate=8000;
  829. st->last_ol_gain = 0;
  830. st->user_callback.func = &speex_default_user_handler;
  831. st->user_callback.data = NULL;
  832. for (i=0;i<16;i++)
  833. st->speex_callbacks[i].func = NULL;
  834. st->voc_m1=st->voc_m2=st->voc_mean=0;
  835. st->voc_offset=0;
  836. st->dtx_enabled=0;
  837. st->isWideband = 0;
  838. st->highpass_enabled = 1;
  839. #ifdef ENABLE_VALGRIND
  840. VALGRIND_MAKE_READABLE(st, NB_DEC_STACK);
  841. #endif
  842. return st;
  843. }
  844. void nb_decoder_destroy(void *state)
  845. {
  846. DecState *st;
  847. st=(DecState*)state;
  848. #if !(defined(VAR_ARRAYS) || defined (USE_ALLOCA))
  849. speex_free_scratch(st->stack);
  850. #endif
  851. speex_free (st->excBuf);
  852. speex_free (st->interp_qlpc);
  853. speex_free (st->old_qlsp);
  854. speex_free (st->mem_sp);
  855. speex_free (st->pi_gain);
  856. speex_free(state);
  857. }
  858. #define median3(a, b, c) ((a) < (b) ? ((b) < (c) ? (b) : ((a) < (c) ? (c) : (a))) : ((c) < (b) ? (b) : ((c) < (a) ? (c) : (a))))
  859. #ifdef FIXED_POINT
  860. const spx_word16_t attenuation[10] = {32767, 31483, 27923, 22861, 17278, 12055, 7764, 4616, 2533, 1283};
  861. #else
  862. const spx_word16_t attenuation[10] = {1., 0.961, 0.852, 0.698, 0.527, 0.368, 0.237, 0.141, 0.077, 0.039};
  863. #endif
  864. static void nb_decode_lost(DecState *st, spx_word16_t *out, char *stack)
  865. {
  866. int i;
  867. int pitch_val;
  868. spx_word16_t pitch_gain;
  869. spx_word16_t fact;
  870. spx_word16_t gain_med;
  871. spx_word16_t innov_gain;
  872. spx_word16_t noise_gain;
  873. if (st->count_lost<10)
  874. fact = attenuation[st->count_lost];
  875. else
  876. fact = 0;
  877. gain_med = median3(st->pitch_gain_buf[0], st->pitch_gain_buf[1], st->pitch_gain_buf[2]);
  878. if (gain_med < st->last_pitch_gain)
  879. st->last_pitch_gain = gain_med;
  880. #ifdef FIXED_POINT
  881. pitch_gain = st->last_pitch_gain;
  882. if (pitch_gain>54)
  883. pitch_gain = 54;
  884. pitch_gain = SHL16(pitch_gain, 9);
  885. #else
  886. pitch_gain = GAIN_SCALING_1*st->last_pitch_gain;
  887. if (pitch_gain>.85)
  888. pitch_gain=.85;
  889. #endif
  890. pitch_gain = MULT16_16_Q15(fact,pitch_gain) + VERY_SMALL;
  891. /* FIXME: This was rms of innovation (not exc) */
  892. innov_gain = compute_rms16(st->exc, st->frameSize);
  893. noise_gain = MULT16_16_Q15(innov_gain, MULT16_16_Q15(fact, SUB16(Q15ONE,MULT16_16_Q15(pitch_gain,pitch_gain))));
  894. /* Shift all buffers by one frame */
  895. SPEEX_MOVE(st->excBuf, st->excBuf+st->frameSize, 2*st->max_pitch + st->subframeSize + 12);
  896. pitch_val = st->last_pitch + SHR32((spx_int32_t)speex_rand(1+st->count_lost, &st->seed),SIG_SHIFT);
  897. if (pitch_val > st->max_pitch)
  898. pitch_val = st->max_pitch;
  899. if (pitch_val < st->min_pitch)
  900. pitch_val = st->min_pitch;
  901. for (i=0;i<st->frameSize;i++)
  902. {
  903. st->exc[i]= MULT16_16_Q15(pitch_gain, (st->exc[i-pitch_val]+VERY_SMALL)) +
  904. speex_rand(noise_gain, &st->seed);
  905. }
  906. bw_lpc(QCONST16(.98,15), st->interp_qlpc, st->interp_qlpc, st->lpcSize);
  907. iir_mem16(&st->exc[-st->subframeSize], st->interp_qlpc, out, st->frameSize,
  908. st->lpcSize, st->mem_sp, stack);
  909. highpass(out, out, st->frameSize, HIGHPASS_NARROWBAND|HIGHPASS_OUTPUT, st->mem_hp);
  910. st->first = 0;
  911. st->count_lost++;
  912. st->pitch_gain_buf[st->pitch_gain_buf_idx++] = PSHR16(pitch_gain,9);
  913. if (st->pitch_gain_buf_idx > 2) /* rollover */
  914. st->pitch_gain_buf_idx = 0;
  915. }
  916. /* Just so we don't need to carry the complete wideband mode information */
  917. static const int wb_skip_table[8] = {0, 36, 112, 192, 352, 0, 0, 0};
  918. int nb_decode(void *state, SpeexBits *bits, void *vout)
  919. {
  920. DecState *st;
  921. int i, sub;
  922. int pitch;
  923. spx_word16_t pitch_gain[3];
  924. spx_word32_t ol_gain=0;
  925. int ol_pitch=0;
  926. spx_word16_t ol_pitch_coef=0;
  927. int best_pitch=40;
  928. spx_word16_t best_pitch_gain=0;
  929. int wideband;
  930. int m;
  931. char *stack;
  932. VARDECL(spx_sig_t *innov);
  933. VARDECL(spx_word32_t *exc32);
  934. VARDECL(spx_coef_t *ak);
  935. VARDECL(spx_lsp_t *qlsp);
  936. spx_word16_t pitch_average=0;
  937. spx_word16_t *out = (spx_word16_t*)vout;
  938. VARDECL(spx_lsp_t *interp_qlsp);
  939. st=(DecState*)state;
  940. stack=st->stack;
  941. /* Check if we're in DTX mode*/
  942. if (!bits && st->dtx_enabled)
  943. {
  944. st->submodeID=0;
  945. } else
  946. {
  947. /* If bits is NULL, consider the packet to be lost (what could we do anyway) */
  948. if (!bits)
  949. {
  950. nb_decode_lost(st, out, stack);
  951. return 0;
  952. }
  953. if (st->encode_submode)
  954. {
  955. /* Search for next narrowband block (handle requests, skip wideband blocks) */
  956. do {
  957. if (speex_bits_remaining(bits)<5)
  958. return -1;
  959. wideband = speex_bits_unpack_unsigned(bits, 1);
  960. if (wideband) /* Skip wideband block (for compatibility) */
  961. {
  962. int submode;
  963. int advance;
  964. advance = submode = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
  965. /*speex_mode_query(&speex_wb_mode, SPEEX_SUBMODE_BITS_PER_FRAME, &advance);*/
  966. advance = wb_skip_table[submode];
  967. if (advance < 0)
  968. {
  969. speex_notify("Invalid mode encountered. The stream is corrupted.");
  970. return -2;
  971. }
  972. advance -= (SB_SUBMODE_BITS+1);
  973. speex_bits_advance(bits, advance);
  974. if (speex_bits_remaining(bits)<5)
  975. return -1;
  976. wideband = speex_bits_unpack_unsigned(bits, 1);
  977. if (wideband)
  978. {
  979. advance = submode = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
  980. /*speex_mode_query(&speex_wb_mode, SPEEX_SUBMODE_BITS_PER_FRAME, &advance);*/
  981. advance = wb_skip_table[submode];
  982. if (advance < 0)
  983. {
  984. speex_notify("Invalid mode encountered. The stream is corrupted.");
  985. return -2;
  986. }
  987. advance -= (SB_SUBMODE_BITS+1);
  988. speex_bits_advance(bits, advance);
  989. wideband = speex_bits_unpack_unsigned(bits, 1);
  990. if (wideband)
  991. {
  992. speex_notify("More than two wideband layers found. The stream is corrupted.");
  993. return -2;
  994. }
  995. }
  996. }
  997. if (speex_bits_remaining(bits)<4)
  998. return -1;
  999. /* FIXME: Check for overflow */
  1000. m = speex_bits_unpack_unsigned(bits, 4);
  1001. if (m==15) /* We found a terminator */
  1002. {
  1003. return -1;
  1004. } else if (m==14) /* Speex in-band request */
  1005. {
  1006. int ret = speex_inband_handler(bits, st->speex_callbacks, state);
  1007. if (ret)
  1008. return ret;
  1009. } else if (m==13) /* User in-band request */
  1010. {
  1011. int ret = st->user_callback.func(bits, state, st->user_callback.data);
  1012. if (ret)
  1013. return ret;
  1014. } else if (m>8) /* Invalid mode */
  1015. {
  1016. speex_notify("Invalid mode encountered. The stream is corrupted.");
  1017. return -2;
  1018. }
  1019. } while (m>8);
  1020. /* Get the sub-mode that was used */
  1021. st->submodeID = m;
  1022. }
  1023. }
  1024. /* Shift all buffers by one frame */
  1025. SPEEX_MOVE(st->excBuf, st->excBuf+st->frameSize, 2*st->max_pitch + st->subframeSize + 12);
  1026. /* If null mode (no transmission), just set a couple things to zero*/
  1027. if (st->submodes[st->submodeID] == NULL)
  1028. {
  1029. VARDECL(spx_coef_t *lpc);
  1030. ALLOC(lpc, st->lpcSize, spx_coef_t);
  1031. bw_lpc(QCONST16(0.93f,15), st->interp_qlpc, lpc, st->lpcSize);
  1032. {
  1033. spx_word16_t innov_gain=0;
  1034. /* FIXME: This was innov, not exc */
  1035. innov_gain = compute_rms16(st->exc, st->frameSize);
  1036. for (i=0;i<st->frameSize;i++)
  1037. st->exc[i]=speex_rand(innov_gain, &st->seed);
  1038. }
  1039. st->first=1;
  1040. /* Final signal synthesis from excitation */
  1041. iir_mem16(st->exc, lpc, out, st->frameSize, st->lpcSize, st->mem_sp, stack);
  1042. st->count_lost=0;
  1043. return 0;
  1044. }
  1045. ALLOC(qlsp, st->lpcSize, spx_lsp_t);
  1046. /* Unquantize LSPs */
  1047. SUBMODE(lsp_unquant)(qlsp, st->lpcSize, bits);
  1048. /*Damp memory if a frame was lost and the LSP changed too much*/
  1049. if (st->count_lost)
  1050. {
  1051. spx_word16_t fact;
  1052. spx_word32_t lsp_dist=0;
  1053. for (i=0;i<st->lpcSize;i++)
  1054. lsp_dist = ADD32(lsp_dist, EXTEND32(ABS(st->old_qlsp[i] - qlsp[i])));
  1055. #ifdef FIXED_POINT
  1056. fact = SHR16(19661,SHR32(lsp_dist,LSP_SHIFT+2));
  1057. #else
  1058. fact = .6*exp(-.2*lsp_dist);
  1059. #endif
  1060. for (i=0;i<st->lpcSize;i++)
  1061. st->mem_sp[i] = MULT16_32_Q15(fact,st->mem_sp[i]);
  1062. }
  1063. /* Handle first frame and lost-packet case */
  1064. if (st->first || st->count_lost)
  1065. {
  1066. for (i=0;i<st->lpcSize;i++)
  1067. st->old_qlsp[i] = qlsp[i];
  1068. }
  1069. /* Get open-loop pitch estimation for low bit-rate pitch coding */
  1070. if (SUBMODE(lbr_pitch)!=-1)
  1071. {
  1072. ol_pitch = st->min_pitch+speex_bits_unpack_unsigned(bits, 7);
  1073. }
  1074. if (SUBMODE(forced_pitch_gain))
  1075. {
  1076. int quant;
  1077. quant = speex_bits_unpack_unsigned(bits, 4);
  1078. ol_pitch_coef=MULT16_16_P15(QCONST16(0.066667,15),SHL16(quant,GAIN_SHIFT));
  1079. }
  1080. /* Get global excitation gain */
  1081. {
  1082. int qe;
  1083. qe = speex_bits_unpack_unsigned(bits, 5);
  1084. #ifdef FIXED_POINT
  1085. /* FIXME: Perhaps we could slightly lower the gain here when the output is going to saturate? */
  1086. ol_gain = MULT16_32_Q15(28406,ol_gain_table[qe]);
  1087. #else
  1088. ol_gain = SIG_SCALING*exp(qe/3.5);
  1089. #endif
  1090. }
  1091. ALLOC(ak, st->lpcSize, spx_coef_t);
  1092. ALLOC(innov, st->subframeSize, spx_sig_t);
  1093. ALLOC(exc32, st->subframeSize, spx_word32_t);
  1094. if (st->submodeID==1)
  1095. {
  1096. int extra;
  1097. extra = speex_bits_unpack_unsigned(bits, 4);
  1098. if (extra==15)
  1099. st->dtx_enabled=1;
  1100. else
  1101. st->dtx_enabled=0;
  1102. }
  1103. if (st->submodeID>1)
  1104. st->dtx_enabled=0;
  1105. /*Loop on subframes */
  1106. for (sub=0;sub<st->nbSubframes;sub++)
  1107. {
  1108. int offset;
  1109. spx_word16_t *exc;
  1110. spx_word16_t *sp;
  1111. spx_word16_t *innov_save = NULL;
  1112. spx_word16_t tmp;
  1113. /* Offset relative to start of frame */
  1114. offset = st->subframeSize*sub;
  1115. /* Excitation */
  1116. exc=st->exc+offset;
  1117. /* Original signal */
  1118. sp=out+offset;
  1119. if (st->innov_save)
  1120. innov_save = st->innov_save+offset;
  1121. /* Reset excitation */
  1122. SPEEX_MEMSET(exc, 0, st->subframeSize);
  1123. /*Adaptive codebook contribution*/
  1124. speex_assert (SUBMODE(ltp_unquant));
  1125. {
  1126. int pit_min, pit_max;
  1127. /* Handle pitch constraints if any */
  1128. if (SUBMODE(lbr_pitch) != -1)
  1129. {
  1130. int margin;
  1131. margin = SUBMODE(lbr_pitch);
  1132. if (margin)
  1133. {
  1134. /* GT - need optimization?
  1135. if (ol_pitch < st->min_pitch+margin-1)
  1136. ol_pitch=st->min_pitch+margin-1;
  1137. if (ol_pitch > st->max_pitch-margin)
  1138. ol_pitch=st->max_pitch-margin;
  1139. pit_min = ol_pitch-margin+1;
  1140. pit_max = ol_pitch+margin;
  1141. */
  1142. pit_min = ol_pitch-margin+1;
  1143. if (pit_min < st->min_pitch)
  1144. pit_min = st->min_pitch;
  1145. pit_max = ol_pitch+margin;
  1146. if (pit_max > st->max_pitch)
  1147. pit_max = st->max_pitch;
  1148. } else {
  1149. pit_min = pit_max = ol_pitch;
  1150. }
  1151. } else {
  1152. pit_min = st->min_pitch;
  1153. pit_max = st->max_pitch;
  1154. }
  1155. SUBMODE(ltp_unquant)(exc, exc32, pit_min, pit_max, ol_pitch_coef, SUBMODE(ltp_params),
  1156. st->subframeSize, &pitch, &pitch_gain[0], bits, stack,
  1157. st->count_lost, offset, st->last_pitch_gain, 0);
  1158. /* Ensuring that things aren't blowing up as would happen if e.g. an encoder is
  1159. crafting packets to make us produce NaNs and slow down the decoder (vague DoS threat).
  1160. We can probably be even more aggressive and limit to 15000 or so. */
  1161. sanitize_values32(exc32, NEG32(QCONST32(32000,SIG_SHIFT-1)), QCONST32(32000,SIG_SHIFT-1), st->subframeSize);
  1162. tmp = gain_3tap_to_1tap(pitch_gain);
  1163. pitch_average += tmp;
  1164. if ((tmp>best_pitch_gain&&ABS(2*best_pitch-pitch)>=3&&ABS(3*best_pitch-pitch)>=4&&ABS(4*best_pitch-pitch)>=5)
  1165. || (tmp>MULT16_16_Q15(QCONST16(.6,15),best_pitch_gain)&&(ABS(best_pitch-2*pitch)<3||ABS(best_pitch-3*pitch)<4||ABS(best_pitch-4*pitch)<5))
  1166. || (MULT16_16_Q15(QCONST16(.67,15),tmp)>best_pitch_gain&&(ABS(2*best_pitch-pitch)<3||ABS(3*best_pitch-pitch)<4||ABS(4*best_pitch-pitch)<5)) )
  1167. {
  1168. best_pitch = pitch;
  1169. if (tmp > best_pitch_gain)
  1170. best_pitch_gain = tmp;
  1171. }
  1172. }
  1173. /* Unquantize the innovation */
  1174. {
  1175. int q_energy;
  1176. spx_word32_t ener;
  1177. SPEEX_MEMSET(innov, 0, st->subframeSize);
  1178. /* Decode sub-frame gain correction */
  1179. if (SUBMODE(have_subframe_gain)==3)
  1180. {
  1181. q_energy = speex_bits_unpack_unsigned(bits, 3);
  1182. ener = MULT16_32_Q14(exc_gain_quant_scal3[q_energy],ol_gain);
  1183. } else if (SUBMODE(have_subframe_gain)==1)
  1184. {
  1185. q_energy = speex_bits_unpack_unsigned(bits, 1);
  1186. ener = MULT16_32_Q14(exc_gain_quant_scal1[q_energy],ol_gain);
  1187. } else {
  1188. ener = ol_gain;
  1189. }
  1190. speex_assert (SUBMODE(innovation_unquant));
  1191. {
  1192. /*Fixed codebook contribution*/
  1193. SUBMODE(innovation_unquant)(innov, SUBMODE(innovation_params), st->subframeSize, bits, stack, &st->seed);
  1194. /* De-normalize innovation and update excitation */
  1195. signal_mul(innov, innov, ener, st->subframeSize);
  1196. /* Decode second codebook (only for some modes) */
  1197. if (SUBMODE(double_codebook))
  1198. {
  1199. char *tmp_stack=stack;
  1200. VARDECL(spx_sig_t *innov2);
  1201. ALLOC(innov2, st->subframeSize, spx_sig_t);
  1202. SPEEX_MEMSET(innov2, 0, st->subframeSize);
  1203. SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), st->subframeSize, bits, stack, &st->seed);
  1204. signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545f,15),ener), st->subframeSize);
  1205. for (i=0;i<st->subframeSize;i++)
  1206. innov[i] = ADD32(innov[i], innov2[i]);
  1207. stack = tmp_stack;
  1208. }
  1209. for (i=0;i<st->subframeSize;i++)
  1210. exc[i]=EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767));
  1211. /*print_vec(exc, 40, "innov");*/
  1212. if (innov_save)
  1213. {
  1214. for (i=0;i<st->subframeSize;i++)
  1215. innov_save[i] = EXTRACT16(PSHR32(innov[i], SIG_SHIFT));
  1216. }
  1217. }
  1218. /*Vocoder mode*/
  1219. if (st->submodeID==1)
  1220. {
  1221. spx_word16_t g=ol_pitch_coef;
  1222. g=MULT16_16_P14(QCONST16(1.5f,14),(g-QCONST16(.2f,6)));
  1223. if (g<0)
  1224. g=0;
  1225. if (g>GAIN_SCALING)
  1226. g=GAIN_SCALING;
  1227. SPEEX_MEMSET(exc, 0, st->subframeSize);
  1228. while (st->voc_offset<st->subframeSize)
  1229. {
  1230. /* exc[st->voc_offset]= g*sqrt(2*ol_pitch)*ol_gain;
  1231. Not quite sure why we need the factor of two in the sqrt */
  1232. if (st->voc_offset>=0)
  1233. exc[st->voc_offset]=MULT16_16(spx_sqrt(MULT16_16_16(2,ol_pitch)),EXTRACT16(PSHR32(MULT16_16(g,PSHR32(ol_gain,SIG_SHIFT)),6)));
  1234. st->voc_offset+=ol_pitch;
  1235. }
  1236. st->voc_offset -= st->subframeSize;
  1237. for (i=0;i<st->subframeSize;i++)
  1238. {
  1239. spx_word16_t exci=exc[i];
  1240. exc[i]= ADD16(ADD16(MULT16_16_Q15(QCONST16(.7f,15),exc[i]) , MULT16_16_Q15(QCONST16(.3f,15),st->voc_m1)),
  1241. SUB16(MULT16_16_Q15(Q15_ONE-MULT16_16_16(QCONST16(.85f,9),g),EXTRACT16(PSHR32(innov[i],SIG_SHIFT))),
  1242. MULT16_16_Q15(MULT16_16_16(QCONST16(.15f,9),g),EXTRACT16(PSHR32(st->voc_m2,SIG_SHIFT)))
  1243. ));
  1244. st->voc_m1 = exci;
  1245. st->voc_m2=innov[i];
  1246. st->voc_mean = EXTRACT16(PSHR32(ADD32(MULT16_16(QCONST16(.8f,15),st->voc_mean), MULT16_16(QCONST16(.2f,15),exc[i])), 15));
  1247. exc[i]-=st->voc_mean;
  1248. }
  1249. }
  1250. }
  1251. }
  1252. ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
  1253. if (st->lpc_enh_enabled && SUBMODE(comb_gain)>0 && !st->count_lost)
  1254. {
  1255. multicomb(st->exc-st->subframeSize, out, st->interp_qlpc, st->lpcSize, 2*st->subframeSize, best_pitch, 40, SUBMODE(comb_gain), stack);
  1256. multicomb(st->exc+st->subframeSize, out+2*st->subframeSize, st->interp_qlpc, st->lpcSize, 2*st->subframeSize, best_pitch, 40, SUBMODE(comb_gain), stack);
  1257. } else {
  1258. SPEEX_COPY(out, &st->exc[-st->subframeSize], st->frameSize);
  1259. }
  1260. /* If the last packet was lost, re-scale the excitation to obtain the same energy as encoded in ol_gain */
  1261. if (st->count_lost)
  1262. {
  1263. spx_word16_t exc_ener;
  1264. spx_word32_t gain32;
  1265. spx_word16_t gain;
  1266. exc_ener = compute_rms16 (st->exc, st->frameSize);
  1267. gain32 = PDIV32(ol_gain, ADD16(exc_ener,1));
  1268. #ifdef FIXED_POINT
  1269. if (gain32 > 32767)
  1270. gain32 = 32767;
  1271. gain = EXTRACT16(gain32);
  1272. #else
  1273. if (gain32 > 2)
  1274. gain32=2;
  1275. gain = gain32;
  1276. #endif
  1277. for (i=0;i<st->frameSize;i++)
  1278. {
  1279. st->exc[i] = MULT16_16_Q14(gain, st->exc[i]);
  1280. out[i]=st->exc[i-st->subframeSize];
  1281. }
  1282. }
  1283. /*Loop on subframes */
  1284. for (sub=0;sub<st->nbSubframes;sub++)
  1285. {
  1286. int offset;
  1287. spx_word16_t *sp;
  1288. spx_word16_t *exc;
  1289. /* Offset relative to start of frame */
  1290. offset = st->subframeSize*sub;
  1291. /* Original signal */
  1292. sp=out+offset;
  1293. /* Excitation */
  1294. exc=st->exc+offset;
  1295. /* LSP interpolation (quantized and unquantized) */
  1296. lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes);
  1297. /* Make sure the LSP's are stable */
  1298. lsp_enforce_margin(interp_qlsp, st->lpcSize, LSP_MARGIN);
  1299. /* Compute interpolated LPCs (unquantized) */
  1300. lsp_to_lpc(interp_qlsp, ak, st->lpcSize, stack);
  1301. /* Compute analysis filter at w=pi */
  1302. {
  1303. spx_word32_t pi_g=LPC_SCALING;
  1304. for (i=0;i<st->lpcSize;i+=2)
  1305. {
  1306. /*pi_g += -st->interp_qlpc[i] + st->interp_qlpc[i+1];*/
  1307. pi_g = ADD32(pi_g, SUB32(EXTEND32(ak[i+1]),EXTEND32(ak[i])));
  1308. }
  1309. st->pi_gain[sub] = pi_g;
  1310. }
  1311. iir_mem16(sp, st->interp_qlpc, sp, st->subframeSize, st->lpcSize,
  1312. st->mem_sp, stack);
  1313. for (i=0;i<st->lpcSize;i++)
  1314. st->interp_qlpc[i] = ak[i];
  1315. }
  1316. if (st->highpass_enabled)
  1317. highpass(out, out, st->frameSize, (st->isWideband?HIGHPASS_WIDEBAND:HIGHPASS_NARROWBAND)|HIGHPASS_OUTPUT, st->mem_hp);
  1318. /*for (i=0;i<st->frameSize;i++)
  1319. printf ("%d\n", (int)st->frame[i]);*/
  1320. /* Tracking output level */
  1321. st->level = 1+PSHR32(ol_gain,SIG_SHIFT);
  1322. st->max_level = MAX16(MULT16_16_Q15(QCONST16(.99f,15), st->max_level), st->level);
  1323. st->min_level = MIN16(ADD16(1,MULT16_16_Q14(QCONST16(1.01f,14), st->min_level)), st->level);
  1324. if (st->max_level < st->min_level+1)
  1325. st->max_level = st->min_level+1;
  1326. /*printf ("%f %f %f %d\n", og, st->min_level, st->max_level, update);*/
  1327. /* Store the LSPs for interpolation in the next frame */
  1328. for (i=0;i<st->lpcSize;i++)
  1329. st->old_qlsp[i] = qlsp[i];
  1330. /* The next frame will not be the first (Duh!) */
  1331. st->first = 0;
  1332. st->count_lost=0;
  1333. st->last_pitch = best_pitch;
  1334. #ifdef FIXED_POINT
  1335. st->last_pitch_gain = PSHR16(pitch_average,2);
  1336. #else
  1337. st->last_pitch_gain = .25*pitch_average;
  1338. #endif
  1339. st->pitch_gain_buf[st->pitch_gain_buf_idx++] = st->last_pitch_gain;
  1340. if (st->pitch_gain_buf_idx > 2) /* rollover */
  1341. st->pitch_gain_buf_idx = 0;
  1342. st->last_ol_gain = ol_gain;
  1343. return 0;
  1344. }
  1345. int nb_encoder_ctl(void *state, int request, void *ptr)
  1346. {
  1347. EncState *st;
  1348. st=(EncState*)state;
  1349. switch(request)
  1350. {
  1351. case SPEEX_GET_FRAME_SIZE:
  1352. (*(spx_int32_t*)ptr) = st->frameSize;
  1353. break;
  1354. case SPEEX_SET_LOW_MODE:
  1355. case SPEEX_SET_MODE:
  1356. st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr);
  1357. break;
  1358. case SPEEX_GET_LOW_MODE:
  1359. case SPEEX_GET_MODE:
  1360. (*(spx_int32_t*)ptr) = st->submodeID;
  1361. break;
  1362. #ifndef DISABLE_VBR
  1363. case SPEEX_SET_VBR:
  1364. st->vbr_enabled = (*(spx_int32_t*)ptr);
  1365. break;
  1366. case SPEEX_GET_VBR:
  1367. (*(spx_int32_t*)ptr) = st->vbr_enabled;
  1368. break;
  1369. case SPEEX_SET_VAD:
  1370. st->vad_enabled = (*(spx_int32_t*)ptr);
  1371. break;
  1372. case SPEEX_GET_VAD:
  1373. (*(spx_int32_t*)ptr) = st->vad_enabled;
  1374. break;
  1375. case SPEEX_SET_DTX:
  1376. st->dtx_enabled = (*(spx_int32_t*)ptr);
  1377. break;
  1378. case SPEEX_GET_DTX:
  1379. (*(spx_int32_t*)ptr) = st->dtx_enabled;
  1380. break;
  1381. case SPEEX_SET_ABR:
  1382. st->abr_enabled = (*(spx_int32_t*)ptr);
  1383. st->vbr_enabled = st->abr_enabled!=0;
  1384. if (st->vbr_enabled)
  1385. {
  1386. spx_int32_t i=10;
  1387. spx_int32_t rate, target;
  1388. float vbr_qual;
  1389. target = (*(spx_int32_t*)ptr);
  1390. while (i>=0)
  1391. {
  1392. speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
  1393. speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
  1394. if (rate <= target)
  1395. break;
  1396. i--;
  1397. }
  1398. vbr_qual=i;
  1399. if (vbr_qual<0)
  1400. vbr_qual=0;
  1401. speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual);
  1402. st->abr_count=0;
  1403. st->abr_drift=0;
  1404. st->abr_drift2=0;
  1405. }
  1406. break;
  1407. case SPEEX_GET_ABR:
  1408. (*(spx_int32_t*)ptr) = st->abr_enabled;
  1409. break;
  1410. #endif /* #ifndef DISABLE_VBR */
  1411. #if !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API)
  1412. case SPEEX_SET_VBR_QUALITY:
  1413. st->vbr_quality = (*(float*)ptr);
  1414. break;
  1415. case SPEEX_GET_VBR_QUALITY:
  1416. (*(float*)ptr) = st->vbr_quality;
  1417. break;
  1418. #endif /* !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API) */
  1419. case SPEEX_SET_QUALITY:
  1420. {
  1421. int quality = (*(spx_int32_t*)ptr);
  1422. if (quality < 0)
  1423. quality = 0;
  1424. if (quality > 10)
  1425. quality = 10;
  1426. st->submodeSelect = st->submodeID = ((const SpeexNBMode*)(st->mode->mode))->quality_map[quality];
  1427. }
  1428. break;
  1429. case SPEEX_SET_COMPLEXITY:
  1430. st->complexity = (*(spx_int32_t*)ptr);
  1431. if (st->complexity<0)
  1432. st->complexity=0;
  1433. break;
  1434. case SPEEX_GET_COMPLEXITY:
  1435. (*(spx_int32_t*)ptr) = st->complexity;
  1436. break;
  1437. case SPEEX_SET_BITRATE:
  1438. {
  1439. spx_int32_t i=10;
  1440. spx_int32_t rate, target;
  1441. target = (*(spx_int32_t*)ptr);
  1442. while (i>=0)
  1443. {
  1444. speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
  1445. speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
  1446. if (rate <= target)
  1447. break;
  1448. i--;
  1449. }
  1450. }
  1451. break;
  1452. case SPEEX_GET_BITRATE:
  1453. if (st->submodes[st->submodeID])
  1454. (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/st->frameSize;
  1455. else
  1456. (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/st->frameSize;
  1457. break;
  1458. case SPEEX_SET_SAMPLING_RATE:
  1459. st->sampling_rate = (*(spx_int32_t*)ptr);
  1460. break;
  1461. case SPEEX_GET_SAMPLING_RATE:
  1462. (*(spx_int32_t*)ptr)=st->sampling_rate;
  1463. break;
  1464. case SPEEX_RESET_STATE:
  1465. {
  1466. int i;
  1467. st->bounded_pitch = 1;
  1468. st->first = 1;
  1469. for (i=0;i<st->lpcSize;i++)
  1470. st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1);
  1471. for (i=0;i<st->lpcSize;i++)
  1472. st->mem_sw[i]=st->mem_sw_whole[i]=st->mem_sp[i]=st->mem_exc[i]=0;
  1473. for (i=0;i<st->frameSize+st->max_pitch+1;i++)
  1474. st->excBuf[i]=st->swBuf[i]=0;
  1475. for (i=0;i<st->windowSize-st->frameSize;i++)
  1476. st->winBuf[i]=0;
  1477. }
  1478. break;
  1479. case SPEEX_SET_SUBMODE_ENCODING:
  1480. st->encode_submode = (*(spx_int32_t*)ptr);
  1481. break;
  1482. case SPEEX_GET_SUBMODE_ENCODING:
  1483. (*(spx_int32_t*)ptr) = st->encode_submode;
  1484. break;
  1485. case SPEEX_GET_LOOKAHEAD:
  1486. (*(spx_int32_t*)ptr)=(st->windowSize-st->frameSize);
  1487. break;
  1488. case SPEEX_SET_PLC_TUNING:
  1489. st->plc_tuning = (*(spx_int32_t*)ptr);
  1490. if (st->plc_tuning>100)
  1491. st->plc_tuning=100;
  1492. break;
  1493. case SPEEX_GET_PLC_TUNING:
  1494. (*(spx_int32_t*)ptr)=(st->plc_tuning);
  1495. break;
  1496. #ifndef DISABLE_VBR
  1497. case SPEEX_SET_VBR_MAX_BITRATE:
  1498. st->vbr_max = (*(spx_int32_t*)ptr);
  1499. break;
  1500. case SPEEX_GET_VBR_MAX_BITRATE:
  1501. (*(spx_int32_t*)ptr) = st->vbr_max;
  1502. break;
  1503. #endif /* #ifndef DISABLE_VBR */
  1504. case SPEEX_SET_HIGHPASS:
  1505. st->highpass_enabled = (*(spx_int32_t*)ptr);
  1506. break;
  1507. case SPEEX_GET_HIGHPASS:
  1508. (*(spx_int32_t*)ptr) = st->highpass_enabled;
  1509. break;
  1510. /* This is all internal stuff past this point */
  1511. case SPEEX_GET_PI_GAIN:
  1512. {
  1513. int i;
  1514. spx_word32_t *g = (spx_word32_t*)ptr;
  1515. for (i=0;i<st->nbSubframes;i++)
  1516. g[i]=st->pi_gain[i];
  1517. }
  1518. break;
  1519. case SPEEX_GET_EXC:
  1520. {
  1521. int i;
  1522. for (i=0;i<st->nbSubframes;i++)
  1523. ((spx_word16_t*)ptr)[i] = compute_rms16(st->exc+i*st->subframeSize, st->subframeSize);
  1524. }
  1525. break;
  1526. #ifndef DISABLE_VBR
  1527. case SPEEX_GET_RELATIVE_QUALITY:
  1528. (*(float*)ptr)=st->relative_quality;
  1529. break;
  1530. #endif /* #ifndef DISABLE_VBR */
  1531. case SPEEX_SET_INNOVATION_SAVE:
  1532. st->innov_rms_save = (spx_word16_t*)ptr;
  1533. break;
  1534. case SPEEX_SET_WIDEBAND:
  1535. st->isWideband = *((spx_int32_t*)ptr);
  1536. break;
  1537. case SPEEX_GET_STACK:
  1538. *((char**)ptr) = st->stack;
  1539. break;
  1540. default:
  1541. speex_warning_int("Unknown nb_ctl request: ", request);
  1542. return -1;
  1543. }
  1544. return 0;
  1545. }
  1546. int nb_decoder_ctl(void *state, int request, void *ptr)
  1547. {
  1548. DecState *st;
  1549. st=(DecState*)state;
  1550. switch(request)
  1551. {
  1552. case SPEEX_SET_LOW_MODE:
  1553. case SPEEX_SET_MODE:
  1554. st->submodeID = (*(spx_int32_t*)ptr);
  1555. break;
  1556. case SPEEX_GET_LOW_MODE:
  1557. case SPEEX_GET_MODE:
  1558. (*(spx_int32_t*)ptr) = st->submodeID;
  1559. break;
  1560. case SPEEX_SET_ENH:
  1561. st->lpc_enh_enabled = *((spx_int32_t*)ptr);
  1562. break;
  1563. case SPEEX_GET_ENH:
  1564. *((spx_int32_t*)ptr) = st->lpc_enh_enabled;
  1565. break;
  1566. case SPEEX_GET_FRAME_SIZE:
  1567. (*(spx_int32_t*)ptr) = st->frameSize;
  1568. break;
  1569. case SPEEX_GET_BITRATE:
  1570. if (st->submodes[st->submodeID])
  1571. (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/st->frameSize;
  1572. else
  1573. (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/st->frameSize;
  1574. break;
  1575. case SPEEX_SET_SAMPLING_RATE:
  1576. st->sampling_rate = (*(spx_int32_t*)ptr);
  1577. break;
  1578. case SPEEX_GET_SAMPLING_RATE:
  1579. (*(spx_int32_t*)ptr)=st->sampling_rate;
  1580. break;
  1581. case SPEEX_SET_HANDLER:
  1582. {
  1583. SpeexCallback *c = (SpeexCallback*)ptr;
  1584. st->speex_callbacks[c->callback_id].func=c->func;
  1585. st->speex_callbacks[c->callback_id].data=c->data;
  1586. st->speex_callbacks[c->callback_id].callback_id=c->callback_id;
  1587. }
  1588. break;
  1589. case SPEEX_SET_USER_HANDLER:
  1590. {
  1591. SpeexCallback *c = (SpeexCallback*)ptr;
  1592. st->user_callback.func=c->func;
  1593. st->user_callback.data=c->data;
  1594. st->user_callback.callback_id=c->callback_id;
  1595. }
  1596. break;
  1597. case SPEEX_RESET_STATE:
  1598. {
  1599. int i;
  1600. for (i=0;i<st->lpcSize;i++)
  1601. st->mem_sp[i]=0;
  1602. for (i=0;i<st->frameSize + st->max_pitch + 1;i++)
  1603. st->excBuf[i]=0;
  1604. }
  1605. break;
  1606. case SPEEX_SET_SUBMODE_ENCODING:
  1607. st->encode_submode = (*(spx_int32_t*)ptr);
  1608. break;
  1609. case SPEEX_GET_SUBMODE_ENCODING:
  1610. (*(spx_int32_t*)ptr) = st->encode_submode;
  1611. break;
  1612. case SPEEX_GET_LOOKAHEAD:
  1613. (*(spx_int32_t*)ptr)=st->subframeSize;
  1614. break;
  1615. case SPEEX_SET_HIGHPASS:
  1616. st->highpass_enabled = (*(spx_int32_t*)ptr);
  1617. break;
  1618. case SPEEX_GET_HIGHPASS:
  1619. (*(spx_int32_t*)ptr) = st->highpass_enabled;
  1620. break;
  1621. /* FIXME: Convert to fixed-point and re-enable even when float API is disabled */
  1622. #ifndef DISABLE_FLOAT_API
  1623. case SPEEX_GET_ACTIVITY:
  1624. {
  1625. float ret;
  1626. ret = log(st->level/st->min_level)/log(st->max_level/st->min_level);
  1627. if (ret>1)
  1628. ret = 1;
  1629. /* Done in a strange way to catch NaNs as well */
  1630. if (!(ret > 0))
  1631. ret = 0;
  1632. /*printf ("%f %f %f %f\n", st->level, st->min_level, st->max_level, ret);*/
  1633. (*(spx_int32_t*)ptr) = (int)(100*ret);
  1634. }
  1635. break;
  1636. #endif
  1637. case SPEEX_GET_PI_GAIN:
  1638. {
  1639. int i;
  1640. spx_word32_t *g = (spx_word32_t*)ptr;
  1641. for (i=0;i<st->nbSubframes;i++)
  1642. g[i]=st->pi_gain[i];
  1643. }
  1644. break;
  1645. case SPEEX_GET_EXC:
  1646. {
  1647. int i;
  1648. for (i=0;i<st->nbSubframes;i++)
  1649. ((spx_word16_t*)ptr)[i] = compute_rms16(st->exc+i*st->subframeSize, st->subframeSize);
  1650. }
  1651. break;
  1652. case SPEEX_GET_DTX_STATUS:
  1653. *((spx_int32_t*)ptr) = st->dtx_enabled;
  1654. break;
  1655. case SPEEX_SET_INNOVATION_SAVE:
  1656. st->innov_save = (spx_word16_t*)ptr;
  1657. break;
  1658. case SPEEX_SET_WIDEBAND:
  1659. st->isWideband = *((spx_int32_t*)ptr);
  1660. break;
  1661. case SPEEX_GET_STACK:
  1662. *((char**)ptr) = st->stack;
  1663. break;
  1664. default:
  1665. speex_warning_int("Unknown nb_ctl request: ", request);
  1666. return -1;
  1667. }
  1668. return 0;
  1669. }