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