modes.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* Copyright (C) 2002-2006 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. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "modes.h"
  31. #include "ltp.h"
  32. #include "quant_lsp.h"
  33. #include "cb_search.h"
  34. #include "sb_celp.h"
  35. #include "nb_celp.h"
  36. #include "vbr.h"
  37. #include "arch.h"
  38. #include <math.h>
  39. #ifndef NULL
  40. #define NULL 0
  41. #endif
  42. #ifdef DISABLE_ENCODER
  43. #define nb_encoder_init NULL
  44. #define nb_encoder_destroy NULL
  45. #define nb_encode NULL
  46. #define nb_encoder_ctl NULL
  47. #define split_cb_search_shape_sign NULL
  48. #define noise_codebook_quant NULL
  49. #define pitch_search_3tap NULL
  50. #define forced_pitch_quant NULL
  51. #define lsp_quant_nb NULL
  52. #define lsp_quant_lbr NULL
  53. #endif /* DISABLE_ENCODER */
  54. #ifdef DISABLE_DECODER
  55. #define nb_decoder_init NULL
  56. #define nb_decoder_destroy NULL
  57. #define nb_decode NULL
  58. #define nb_decoder_ctl NULL
  59. #define noise_codebook_unquant NULL
  60. #define split_cb_shape_sign_unquant NULL
  61. #define lsp_unquant_nb NULL
  62. #define lsp_unquant_lbr NULL
  63. #define pitch_unquant_3tap NULL
  64. #define forced_pitch_unquant NULL
  65. #endif /* DISABLE_DECODER */
  66. /* Extern declarations for all codebooks we use here */
  67. extern const signed char gain_cdbk_nb[];
  68. extern const signed char gain_cdbk_lbr[];
  69. extern const signed char exc_5_256_table[];
  70. extern const signed char exc_5_64_table[];
  71. extern const signed char exc_8_128_table[];
  72. extern const signed char exc_10_32_table[];
  73. extern const signed char exc_10_16_table[];
  74. extern const signed char exc_20_32_table[];
  75. /* Parameters for Long-Term Prediction (LTP)*/
  76. static const ltp_params ltp_params_nb = {
  77. gain_cdbk_nb,
  78. 7,
  79. 7
  80. };
  81. /* Parameters for Long-Term Prediction (LTP)*/
  82. static const ltp_params ltp_params_vlbr = {
  83. gain_cdbk_lbr,
  84. 5,
  85. 0
  86. };
  87. /* Parameters for Long-Term Prediction (LTP)*/
  88. static const ltp_params ltp_params_lbr = {
  89. gain_cdbk_lbr,
  90. 5,
  91. 7
  92. };
  93. /* Parameters for Long-Term Prediction (LTP)*/
  94. static const ltp_params ltp_params_med = {
  95. gain_cdbk_lbr,
  96. 5,
  97. 7
  98. };
  99. /* Split-VQ innovation parameters for very low bit-rate narrowband */
  100. static const split_cb_params split_cb_nb_vlbr = {
  101. 10, /*subvect_size*/
  102. 4, /*nb_subvect*/
  103. exc_10_16_table, /*shape_cb*/
  104. 4, /*shape_bits*/
  105. 0,
  106. };
  107. /* Split-VQ innovation parameters for very low bit-rate narrowband */
  108. static const split_cb_params split_cb_nb_ulbr = {
  109. 20, /*subvect_size*/
  110. 2, /*nb_subvect*/
  111. exc_20_32_table, /*shape_cb*/
  112. 5, /*shape_bits*/
  113. 0,
  114. };
  115. /* Split-VQ innovation parameters for low bit-rate narrowband */
  116. static const split_cb_params split_cb_nb_lbr = {
  117. 10, /*subvect_size*/
  118. 4, /*nb_subvect*/
  119. exc_10_32_table, /*shape_cb*/
  120. 5, /*shape_bits*/
  121. 0,
  122. };
  123. /* Split-VQ innovation parameters narrowband */
  124. static const split_cb_params split_cb_nb = {
  125. 5, /*subvect_size*/
  126. 8, /*nb_subvect*/
  127. exc_5_64_table, /*shape_cb*/
  128. 6, /*shape_bits*/
  129. 0,
  130. };
  131. /* Split-VQ innovation parameters narrowband */
  132. static const split_cb_params split_cb_nb_med = {
  133. 8, /*subvect_size*/
  134. 5, /*nb_subvect*/
  135. exc_8_128_table, /*shape_cb*/
  136. 7, /*shape_bits*/
  137. 0,
  138. };
  139. /* Split-VQ innovation for low-band wideband */
  140. static const split_cb_params split_cb_sb = {
  141. 5, /*subvect_size*/
  142. 8, /*nb_subvect*/
  143. exc_5_256_table, /*shape_cb*/
  144. 8, /*shape_bits*/
  145. 0,
  146. };
  147. /* 2150 bps "vocoder-like" mode for comfort noise */
  148. static const SpeexSubmode nb_submode1 = {
  149. 0,
  150. 1,
  151. 0,
  152. 0,
  153. /* LSP quantization */
  154. lsp_quant_lbr,
  155. lsp_unquant_lbr,
  156. /* No pitch quantization */
  157. forced_pitch_quant,
  158. forced_pitch_unquant,
  159. NULL,
  160. /* No innovation quantization (noise only) */
  161. noise_codebook_quant,
  162. noise_codebook_unquant,
  163. NULL,
  164. -1,
  165. 43
  166. };
  167. /* 3.95 kbps very low bit-rate mode */
  168. static const SpeexSubmode nb_submode8 = {
  169. 0,
  170. 1,
  171. 0,
  172. 0,
  173. /*LSP quantization*/
  174. lsp_quant_lbr,
  175. lsp_unquant_lbr,
  176. /*No pitch quantization*/
  177. forced_pitch_quant,
  178. forced_pitch_unquant,
  179. NULL,
  180. /*Innovation quantization*/
  181. split_cb_search_shape_sign,
  182. split_cb_shape_sign_unquant,
  183. &split_cb_nb_ulbr,
  184. QCONST16(.5,15),
  185. 79
  186. };
  187. /* 5.95 kbps very low bit-rate mode */
  188. static const SpeexSubmode nb_submode2 = {
  189. 0,
  190. 0,
  191. 0,
  192. 0,
  193. /*LSP quantization*/
  194. lsp_quant_lbr,
  195. lsp_unquant_lbr,
  196. /*No pitch quantization*/
  197. pitch_search_3tap,
  198. pitch_unquant_3tap,
  199. &ltp_params_vlbr,
  200. /*Innovation quantization*/
  201. split_cb_search_shape_sign,
  202. split_cb_shape_sign_unquant,
  203. &split_cb_nb_vlbr,
  204. QCONST16(.6,15),
  205. 119
  206. };
  207. /* 8 kbps low bit-rate mode */
  208. static const SpeexSubmode nb_submode3 = {
  209. -1,
  210. 0,
  211. 1,
  212. 0,
  213. /*LSP quantization*/
  214. lsp_quant_lbr,
  215. lsp_unquant_lbr,
  216. /*Pitch quantization*/
  217. pitch_search_3tap,
  218. pitch_unquant_3tap,
  219. &ltp_params_lbr,
  220. /*Innovation quantization*/
  221. split_cb_search_shape_sign,
  222. split_cb_shape_sign_unquant,
  223. &split_cb_nb_lbr,
  224. QCONST16(.55,15),
  225. 160
  226. };
  227. /* 11 kbps medium bit-rate mode */
  228. static const SpeexSubmode nb_submode4 = {
  229. -1,
  230. 0,
  231. 1,
  232. 0,
  233. /*LSP quantization*/
  234. lsp_quant_lbr,
  235. lsp_unquant_lbr,
  236. /*Pitch quantization*/
  237. pitch_search_3tap,
  238. pitch_unquant_3tap,
  239. &ltp_params_med,
  240. /*Innovation quantization*/
  241. split_cb_search_shape_sign,
  242. split_cb_shape_sign_unquant,
  243. &split_cb_nb_med,
  244. QCONST16(.45,15),
  245. 220
  246. };
  247. /* 15 kbps high bit-rate mode */
  248. static const SpeexSubmode nb_submode5 = {
  249. -1,
  250. 0,
  251. 3,
  252. 0,
  253. /*LSP quantization*/
  254. lsp_quant_nb,
  255. lsp_unquant_nb,
  256. /*Pitch quantization*/
  257. pitch_search_3tap,
  258. pitch_unquant_3tap,
  259. &ltp_params_nb,
  260. /*Innovation quantization*/
  261. split_cb_search_shape_sign,
  262. split_cb_shape_sign_unquant,
  263. &split_cb_nb,
  264. QCONST16(.25,15),
  265. 300
  266. };
  267. /* 18.2 high bit-rate mode */
  268. static const SpeexSubmode nb_submode6 = {
  269. -1,
  270. 0,
  271. 3,
  272. 0,
  273. /*LSP quantization*/
  274. lsp_quant_nb,
  275. lsp_unquant_nb,
  276. /*Pitch quantization*/
  277. pitch_search_3tap,
  278. pitch_unquant_3tap,
  279. &ltp_params_nb,
  280. /*Innovation quantization*/
  281. split_cb_search_shape_sign,
  282. split_cb_shape_sign_unquant,
  283. &split_cb_sb,
  284. QCONST16(.15,15),
  285. 364
  286. };
  287. /* 24.6 kbps high bit-rate mode */
  288. static const SpeexSubmode nb_submode7 = {
  289. -1,
  290. 0,
  291. 3,
  292. 1,
  293. /*LSP quantization*/
  294. lsp_quant_nb,
  295. lsp_unquant_nb,
  296. /*Pitch quantization*/
  297. pitch_search_3tap,
  298. pitch_unquant_3tap,
  299. &ltp_params_nb,
  300. /*Innovation quantization*/
  301. split_cb_search_shape_sign,
  302. split_cb_shape_sign_unquant,
  303. &split_cb_nb,
  304. QCONST16(.05,15),
  305. 492
  306. };
  307. /* Default mode for narrowband */
  308. static const SpeexNBMode nb_mode = {
  309. NB_FRAME_SIZE, /*frameSize*/
  310. NB_SUBFRAME_SIZE, /*subframeSize*/
  311. NB_ORDER, /*lpcSize*/
  312. NB_PITCH_START, /*pitchStart*/
  313. NB_PITCH_END, /*pitchEnd*/
  314. QCONST16(0.92,15), /* gamma1 */
  315. QCONST16(0.6,15), /* gamma2 */
  316. QCONST16(.0002,15), /*lpc_floor*/
  317. {NULL, &nb_submode1, &nb_submode2, &nb_submode3, &nb_submode4, &nb_submode5, &nb_submode6, &nb_submode7,
  318. &nb_submode8, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
  319. 5,
  320. {1, 8, 2, 3, 3, 4, 4, 5, 5, 6, 7}
  321. };
  322. /* Default mode for narrowband */
  323. EXPORT const SpeexMode speex_nb_mode = {
  324. &nb_mode,
  325. nb_mode_query,
  326. "narrowband",
  327. 0,
  328. 4,
  329. nb_encoder_init,
  330. nb_encoder_destroy,
  331. nb_encode,
  332. nb_decoder_init,
  333. nb_decoder_destroy,
  334. nb_decode,
  335. nb_encoder_ctl,
  336. nb_decoder_ctl,
  337. };
  338. EXPORT int speex_mode_query(const SpeexMode *mode, int request, void *ptr)
  339. {
  340. return mode->query(mode->mode, request, ptr);
  341. }
  342. #ifdef FIXED_DEBUG
  343. long long spx_mips=0;
  344. #endif