nb_celp.c 57 KB

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