Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

650 lines
14 KiB

  1. /* Copyright (C) 2002 Jean-Marc Valin
  2. File: modes.c
  3. Describes the different modes of the codec
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. - Neither the name of the Xiph.org Foundation nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "modes.h"
  28. #include "ltp.h"
  29. #include "quant_lsp.h"
  30. #include "cb_search.h"
  31. #include "sb_celp.h"
  32. #include "nb_celp.h"
  33. #include "vbr.h"
  34. #include "misc.h"
  35. #ifndef NULL
  36. #define NULL 0
  37. #endif
  38. SpeexMode *speex_mode_list[SPEEX_NB_MODES] = {&speex_nb_mode, &speex_wb_mode, &speex_uwb_mode};
  39. /* Extern declarations for all codebooks we use here */
  40. extern signed char gain_cdbk_nb[];
  41. extern signed char gain_cdbk_lbr[];
  42. extern signed char hexc_table[];
  43. extern signed char exc_5_256_table[];
  44. extern signed char exc_5_64_table[];
  45. extern signed char exc_8_128_table[];
  46. extern signed char exc_10_32_table[];
  47. extern signed char exc_10_16_table[];
  48. extern signed char exc_20_32_table[];
  49. extern signed char hexc_10_32_table[];
  50. static int nb_mode_query(void *mode, int request, void *ptr);
  51. static int wb_mode_query(void *mode, int request, void *ptr);
  52. /* Parameters for Long-Term Prediction (LTP)*/
  53. static ltp_params ltp_params_nb = {
  54. gain_cdbk_nb,
  55. 7,
  56. 7
  57. };
  58. /* Parameters for Long-Term Prediction (LTP)*/
  59. static ltp_params ltp_params_vlbr = {
  60. gain_cdbk_lbr,
  61. 5,
  62. 0
  63. };
  64. /* Parameters for Long-Term Prediction (LTP)*/
  65. static ltp_params ltp_params_lbr = {
  66. gain_cdbk_lbr,
  67. 5,
  68. 7
  69. };
  70. /* Parameters for Long-Term Prediction (LTP)*/
  71. static ltp_params ltp_params_med = {
  72. gain_cdbk_lbr,
  73. 5,
  74. 7
  75. };
  76. /* Split-VQ innovation parameters for very low bit-rate narrowband */
  77. static split_cb_params split_cb_nb_vlbr = {
  78. 10, /*subvect_size*/
  79. 4, /*nb_subvect*/
  80. exc_10_16_table, /*shape_cb*/
  81. 4, /*shape_bits*/
  82. 0,
  83. };
  84. /* Split-VQ innovation parameters for very low bit-rate narrowband */
  85. static split_cb_params split_cb_nb_ulbr = {
  86. 20, /*subvect_size*/
  87. 2, /*nb_subvect*/
  88. exc_20_32_table, /*shape_cb*/
  89. 5, /*shape_bits*/
  90. 0,
  91. };
  92. /* Split-VQ innovation parameters for low bit-rate narrowband */
  93. static split_cb_params split_cb_nb_lbr = {
  94. 10, /*subvect_size*/
  95. 4, /*nb_subvect*/
  96. exc_10_32_table, /*shape_cb*/
  97. 5, /*shape_bits*/
  98. 0,
  99. };
  100. /* Split-VQ innovation parameters narrowband */
  101. static split_cb_params split_cb_nb = {
  102. 5, /*subvect_size*/
  103. 8, /*nb_subvect*/
  104. exc_5_64_table, /*shape_cb*/
  105. 6, /*shape_bits*/
  106. 0,
  107. };
  108. /* Split-VQ innovation parameters narrowband */
  109. static split_cb_params split_cb_nb_med = {
  110. 8, /*subvect_size*/
  111. 5, /*nb_subvect*/
  112. exc_8_128_table, /*shape_cb*/
  113. 7, /*shape_bits*/
  114. 0,
  115. };
  116. /* Split-VQ innovation for low-band wideband */
  117. static split_cb_params split_cb_sb = {
  118. 5, /*subvect_size*/
  119. 8, /*nb_subvect*/
  120. exc_5_256_table, /*shape_cb*/
  121. 8, /*shape_bits*/
  122. 0,
  123. };
  124. /* Split-VQ innovation for high-band wideband */
  125. static split_cb_params split_cb_high = {
  126. 8, /*subvect_size*/
  127. 5, /*nb_subvect*/
  128. hexc_table, /*shape_cb*/
  129. 7, /*shape_bits*/
  130. 1,
  131. };
  132. /* Split-VQ innovation for high-band wideband */
  133. static split_cb_params split_cb_high_lbr = {
  134. 10, /*subvect_size*/
  135. 4, /*nb_subvect*/
  136. hexc_10_32_table, /*shape_cb*/
  137. 5, /*shape_bits*/
  138. 0,
  139. };
  140. /* 2150 bps "vocoder-like" mode for comfort noise */
  141. static SpeexSubmode nb_submode1 = {
  142. 0,
  143. 1,
  144. 0,
  145. 0,
  146. /* LSP quantization */
  147. lsp_quant_lbr,
  148. lsp_unquant_lbr,
  149. /* No pitch quantization */
  150. forced_pitch_quant,
  151. forced_pitch_unquant,
  152. NULL,
  153. /* No innovation quantization (noise only) */
  154. noise_codebook_quant,
  155. noise_codebook_unquant,
  156. NULL,
  157. .7, .7, -1,
  158. 43
  159. };
  160. /* 3.95 kbps very low bit-rate mode */
  161. static SpeexSubmode nb_submode8 = {
  162. 0,
  163. 1,
  164. 0,
  165. 0,
  166. /*LSP quantization*/
  167. lsp_quant_lbr,
  168. lsp_unquant_lbr,
  169. /*No pitch quantization*/
  170. forced_pitch_quant,
  171. forced_pitch_unquant,
  172. NULL,
  173. /*Innovation quantization*/
  174. split_cb_search_shape_sign,
  175. split_cb_shape_sign_unquant,
  176. &split_cb_nb_ulbr,
  177. 0.7, 0.5, .65,
  178. 79
  179. };
  180. /* 5.95 kbps very low bit-rate mode */
  181. static SpeexSubmode nb_submode2 = {
  182. 0,
  183. 0,
  184. 0,
  185. 0,
  186. /*LSP quantization*/
  187. lsp_quant_lbr,
  188. lsp_unquant_lbr,
  189. /*No pitch quantization*/
  190. pitch_search_3tap,
  191. pitch_unquant_3tap,
  192. &ltp_params_vlbr,
  193. /*Innovation quantization*/
  194. split_cb_search_shape_sign,
  195. split_cb_shape_sign_unquant,
  196. &split_cb_nb_vlbr,
  197. 0.7, 0.5, .55,
  198. 119
  199. };
  200. /* 8 kbps low bit-rate mode */
  201. static SpeexSubmode nb_submode3 = {
  202. -1,
  203. 0,
  204. 1,
  205. 0,
  206. /*LSP quantization*/
  207. lsp_quant_lbr,
  208. lsp_unquant_lbr,
  209. /*Pitch quantization*/
  210. pitch_search_3tap,
  211. pitch_unquant_3tap,
  212. &ltp_params_lbr,
  213. /*Innovation quantization*/
  214. split_cb_search_shape_sign,
  215. split_cb_shape_sign_unquant,
  216. &split_cb_nb_lbr,
  217. 0.7, 0.55, .45,
  218. 160
  219. };
  220. /* 11 kbps medium bit-rate mode */
  221. static SpeexSubmode nb_submode4 = {
  222. -1,
  223. 0,
  224. 1,
  225. 0,
  226. /*LSP quantization*/
  227. lsp_quant_lbr,
  228. lsp_unquant_lbr,
  229. /*Pitch quantization*/
  230. pitch_search_3tap,
  231. pitch_unquant_3tap,
  232. &ltp_params_med,
  233. /*Innovation quantization*/
  234. split_cb_search_shape_sign,
  235. split_cb_shape_sign_unquant,
  236. &split_cb_nb_med,
  237. 0.7, 0.63, .35,
  238. 220
  239. };
  240. /* 15 kbps high bit-rate mode */
  241. static SpeexSubmode nb_submode5 = {
  242. -1,
  243. 0,
  244. 3,
  245. 0,
  246. /*LSP quantization*/
  247. lsp_quant_nb,
  248. lsp_unquant_nb,
  249. /*Pitch quantization*/
  250. pitch_search_3tap,
  251. pitch_unquant_3tap,
  252. &ltp_params_nb,
  253. /*Innovation quantization*/
  254. split_cb_search_shape_sign,
  255. split_cb_shape_sign_unquant,
  256. &split_cb_nb,
  257. 0.7, 0.65, .25,
  258. 300
  259. };
  260. /* 18.2 high bit-rate mode */
  261. static SpeexSubmode nb_submode6 = {
  262. -1,
  263. 0,
  264. 3,
  265. 0,
  266. /*LSP quantization*/
  267. lsp_quant_nb,
  268. lsp_unquant_nb,
  269. /*Pitch quantization*/
  270. pitch_search_3tap,
  271. pitch_unquant_3tap,
  272. &ltp_params_nb,
  273. /*Innovation quantization*/
  274. split_cb_search_shape_sign,
  275. split_cb_shape_sign_unquant,
  276. &split_cb_sb,
  277. 0.68, 0.65, .1,
  278. 364
  279. };
  280. /* 24.6 kbps high bit-rate mode */
  281. static SpeexSubmode nb_submode7 = {
  282. -1,
  283. 0,
  284. 3,
  285. 1,
  286. /*LSP quantization*/
  287. lsp_quant_nb,
  288. lsp_unquant_nb,
  289. /*Pitch quantization*/
  290. pitch_search_3tap,
  291. pitch_unquant_3tap,
  292. &ltp_params_nb,
  293. /*Innovation quantization*/
  294. split_cb_search_shape_sign,
  295. split_cb_shape_sign_unquant,
  296. &split_cb_nb,
  297. 0.65, 0.65, -1,
  298. 492
  299. };
  300. /* Default mode for narrowband */
  301. static SpeexNBMode nb_mode = {
  302. 160, /*frameSize*/
  303. 40, /*subframeSize*/
  304. 10, /*lpcSize*/
  305. 640, /*bufSize*/
  306. 17, /*pitchStart*/
  307. 144, /*pitchEnd*/
  308. 0.9, /*gamma1*/
  309. 0.6, /*gamma2*/
  310. .01, /*lag_factor*/
  311. 1.0001, /*lpc_floor*/
  312. 0.0, /*preemph*/
  313. {NULL, &nb_submode1, &nb_submode2, &nb_submode3, &nb_submode4, &nb_submode5, &nb_submode6, &nb_submode7,
  314. &nb_submode8, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
  315. 5,
  316. {1, 8, 2, 3, 3, 4, 4, 5, 5, 6, 7}
  317. };
  318. /* Default mode for narrowband */
  319. SpeexMode speex_nb_mode = {
  320. &nb_mode,
  321. nb_mode_query,
  322. "narrowband",
  323. 0,
  324. 4,
  325. &nb_encoder_init,
  326. &nb_encoder_destroy,
  327. &nb_encode,
  328. &nb_decoder_init,
  329. &nb_decoder_destroy,
  330. &nb_decode,
  331. &nb_encoder_ctl,
  332. &nb_decoder_ctl,
  333. };
  334. /* Wideband part */
  335. static SpeexSubmode wb_submode1 = {
  336. 0,
  337. 0,
  338. 1,
  339. 0,
  340. /*LSP quantization*/
  341. lsp_quant_high,
  342. lsp_unquant_high,
  343. /*Pitch quantization*/
  344. NULL,
  345. NULL,
  346. NULL,
  347. /*No innovation quantization*/
  348. NULL,
  349. NULL,
  350. NULL,
  351. .75, .75, -1,
  352. 36
  353. };
  354. static SpeexSubmode wb_submode2 = {
  355. 0,
  356. 0,
  357. 1,
  358. 0,
  359. /*LSP quantization*/
  360. lsp_quant_high,
  361. lsp_unquant_high,
  362. /*Pitch quantization*/
  363. NULL,
  364. NULL,
  365. NULL,
  366. /*Innovation quantization*/
  367. split_cb_search_shape_sign,
  368. split_cb_shape_sign_unquant,
  369. &split_cb_high_lbr,
  370. .85, .6, -1,
  371. 112
  372. };
  373. static SpeexSubmode wb_submode3 = {
  374. 0,
  375. 0,
  376. 1,
  377. 0,
  378. /*LSP quantization*/
  379. lsp_quant_high,
  380. lsp_unquant_high,
  381. /*Pitch quantization*/
  382. NULL,
  383. NULL,
  384. NULL,
  385. /*Innovation quantization*/
  386. split_cb_search_shape_sign,
  387. split_cb_shape_sign_unquant,
  388. &split_cb_high,
  389. .75, .7, -1,
  390. 192
  391. };
  392. static SpeexSubmode wb_submode4 = {
  393. 0,
  394. 0,
  395. 1,
  396. 1,
  397. /*LSP quantization*/
  398. lsp_quant_high,
  399. lsp_unquant_high,
  400. /*Pitch quantization*/
  401. NULL,
  402. NULL,
  403. NULL,
  404. /*Innovation quantization*/
  405. split_cb_search_shape_sign,
  406. split_cb_shape_sign_unquant,
  407. &split_cb_high,
  408. .75, .75, -1,
  409. 352
  410. };
  411. /* Split-band wideband CELP mode*/
  412. static SpeexSBMode sb_wb_mode = {
  413. &speex_nb_mode,
  414. 160, /*frameSize*/
  415. 40, /*subframeSize*/
  416. 8, /*lpcSize*/
  417. 640, /*bufSize*/
  418. .9, /*gamma1*/
  419. 0.6, /*gamma2*/
  420. .002, /*lag_factor*/
  421. 1.0001, /*lpc_floor*/
  422. 0.0, /*preemph*/
  423. 0.9,
  424. {NULL, &wb_submode1, &wb_submode2, &wb_submode3, &wb_submode4, NULL, NULL, NULL},
  425. 3,
  426. {1, 8, 2, 3, 4, 5, 5, 6, 6, 7, 7},
  427. {1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 4},
  428. vbr_hb_thresh,
  429. 5
  430. };
  431. SpeexMode speex_wb_mode = {
  432. &sb_wb_mode,
  433. wb_mode_query,
  434. "wideband (sub-band CELP)",
  435. 1,
  436. 4,
  437. &sb_encoder_init,
  438. &sb_encoder_destroy,
  439. &sb_encode,
  440. &sb_decoder_init,
  441. &sb_decoder_destroy,
  442. &sb_decode,
  443. &sb_encoder_ctl,
  444. &sb_decoder_ctl,
  445. };
  446. /* "Ultra-wideband" mode stuff */
  447. /* Split-band "ultra-wideband" (32 kbps) CELP mode*/
  448. static SpeexSBMode sb_uwb_mode = {
  449. &speex_wb_mode,
  450. 320, /*frameSize*/
  451. 80, /*subframeSize*/
  452. 8, /*lpcSize*/
  453. 1280, /*bufSize*/
  454. .9, /*gamma1*/
  455. 0.6, /*gamma2*/
  456. .002, /*lag_factor*/
  457. 1.0001, /*lpc_floor*/
  458. 0.0, /*preemph*/
  459. 0.7,
  460. {NULL, &wb_submode1, NULL, NULL, NULL, NULL, NULL, NULL},
  461. 1,
  462. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
  463. {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  464. vbr_uhb_thresh,
  465. 2
  466. };
  467. SpeexMode speex_uwb_mode = {
  468. &sb_uwb_mode,
  469. wb_mode_query,
  470. "ultra-wideband (sub-band CELP)",
  471. 2,
  472. 4,
  473. &sb_encoder_init,
  474. &sb_encoder_destroy,
  475. &sb_encode,
  476. &sb_decoder_init,
  477. &sb_decoder_destroy,
  478. &sb_decode,
  479. &sb_encoder_ctl,
  480. &sb_decoder_ctl,
  481. };
  482. void *speex_encoder_init(SpeexMode *mode)
  483. {
  484. return mode->enc_init(mode);
  485. }
  486. void *speex_decoder_init(SpeexMode *mode)
  487. {
  488. return mode->dec_init(mode);
  489. }
  490. void speex_encoder_destroy(void *state)
  491. {
  492. (*((SpeexMode**)state))->enc_destroy(state);
  493. }
  494. int speex_encode(void *state, float *in, SpeexBits *bits)
  495. {
  496. return (*((SpeexMode**)state))->enc(state, in, bits);
  497. }
  498. void speex_decoder_destroy(void *state)
  499. {
  500. (*((SpeexMode**)state))->dec_destroy(state);
  501. }
  502. int speex_decode(void *state, SpeexBits *bits, float *out)
  503. {
  504. return (*((SpeexMode**)state))->dec(state, bits, out);
  505. }
  506. int speex_encoder_ctl(void *state, int request, void *ptr)
  507. {
  508. return (*((SpeexMode**)state))->enc_ctl(state, request, ptr);
  509. }
  510. int speex_decoder_ctl(void *state, int request, void *ptr)
  511. {
  512. return (*((SpeexMode**)state))->dec_ctl(state, request, ptr);
  513. }
  514. static int nb_mode_query(void *mode, int request, void *ptr)
  515. {
  516. SpeexNBMode *m = (SpeexNBMode*)mode;
  517. switch (request)
  518. {
  519. case SPEEX_MODE_FRAME_SIZE:
  520. *((int*)ptr)=m->frameSize;
  521. break;
  522. case SPEEX_SUBMODE_BITS_PER_FRAME:
  523. if (*((int*)ptr)==0)
  524. *((int*)ptr) = NB_SUBMODE_BITS+1;
  525. else if (m->submodes[*((int*)ptr)]==NULL)
  526. *((int*)ptr) = -1;
  527. else
  528. *((int*)ptr) = m->submodes[*((int*)ptr)]->bits_per_frame;
  529. break;
  530. default:
  531. speex_warning_int("Unknown nb_mode_query request: ", request);
  532. return -1;
  533. }
  534. return 0;
  535. }
  536. static int wb_mode_query(void *mode, int request, void *ptr)
  537. {
  538. SpeexSBMode *m = (SpeexSBMode*)mode;
  539. switch (request)
  540. {
  541. case SPEEX_MODE_FRAME_SIZE:
  542. *((int*)ptr)=2*m->frameSize;
  543. break;
  544. case SPEEX_SUBMODE_BITS_PER_FRAME:
  545. if (*((int*)ptr)==0)
  546. *((int*)ptr) = SB_SUBMODE_BITS+1;
  547. else if (m->submodes[*((int*)ptr)]==NULL)
  548. *((int*)ptr) = -1;
  549. else
  550. *((int*)ptr) = m->submodes[*((int*)ptr)]->bits_per_frame;
  551. break;
  552. default:
  553. speex_warning_int("Unknown wb_mode_query request: ", request);
  554. return -1;
  555. }
  556. return 0;
  557. }
  558. int speex_mode_query(SpeexMode *mode, int request, void *ptr)
  559. {
  560. return mode->query(mode->mode, request, ptr);
  561. }