sb_celp.c 45 KB

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