modes.c 8.3 KB

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