modes_wb.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* Copyright (C) 2002-2007 Jean-Marc Valin
  2. File: modes.c
  3. Describes the wideband 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. #include "os_support.h"
  40. #ifndef NULL
  41. #define NULL 0
  42. #endif
  43. #if defined(DISABLE_ENCODER) || defined(DISABLE_WIDEBAND)
  44. #define split_cb_search_shape_sign NULL
  45. #define noise_codebook_quant NULL
  46. #define pitch_search_3tap NULL
  47. #define forced_pitch_quant NULL
  48. #define sb_encoder_init NULL
  49. #define sb_encoder_destroy NULL
  50. #define sb_encode NULL
  51. #define sb_encoder_ctl NULL
  52. #define lsp_quant_high NULL
  53. #endif /* DISABLE_ENCODER */
  54. #if defined(DISABLE_DECODER) || defined(DISABLE_WIDEBAND)
  55. #define noise_codebook_unquant NULL
  56. #define split_cb_shape_sign_unquant NULL
  57. #define lsp_unquant_high NULL
  58. #define sb_decoder_init NULL
  59. #define sb_decoder_destroy NULL
  60. #define sb_decode NULL
  61. #define sb_decoder_ctl NULL
  62. #endif /* DISABLE_DECODER */
  63. EXPORT const SpeexMode * const speex_mode_list[SPEEX_NB_MODES] = {&speex_nb_mode, &speex_wb_mode, &speex_uwb_mode};
  64. extern const signed char hexc_table[];
  65. extern const signed char hexc_10_32_table[];
  66. #ifndef DISABLE_WIDEBAND
  67. /* Split-VQ innovation for high-band wideband */
  68. static const split_cb_params split_cb_high = {
  69. 8, /*subvect_size*/
  70. 5, /*nb_subvect*/
  71. hexc_table, /*shape_cb*/
  72. 7, /*shape_bits*/
  73. 1,
  74. };
  75. /* Split-VQ innovation for high-band wideband */
  76. static const split_cb_params split_cb_high_lbr = {
  77. 10, /*subvect_size*/
  78. 4, /*nb_subvect*/
  79. hexc_10_32_table, /*shape_cb*/
  80. 5, /*shape_bits*/
  81. 0,
  82. };
  83. #endif
  84. static const SpeexSubmode wb_submode1 = {
  85. 0,
  86. 0,
  87. 1,
  88. 0,
  89. /*LSP quantization*/
  90. lsp_quant_high,
  91. lsp_unquant_high,
  92. /*Pitch quantization*/
  93. NULL,
  94. NULL,
  95. NULL,
  96. /*No innovation quantization*/
  97. NULL,
  98. NULL,
  99. NULL,
  100. -1,
  101. 36
  102. };
  103. static const SpeexSubmode wb_submode2 = {
  104. 0,
  105. 0,
  106. 1,
  107. 0,
  108. /*LSP quantization*/
  109. lsp_quant_high,
  110. lsp_unquant_high,
  111. /*Pitch quantization*/
  112. NULL,
  113. NULL,
  114. NULL,
  115. /*Innovation quantization*/
  116. split_cb_search_shape_sign,
  117. split_cb_shape_sign_unquant,
  118. #ifdef DISABLE_WIDEBAND
  119. NULL,
  120. #else
  121. &split_cb_high_lbr,
  122. #endif
  123. -1,
  124. 112
  125. };
  126. static const SpeexSubmode wb_submode3 = {
  127. 0,
  128. 0,
  129. 1,
  130. 0,
  131. /*LSP quantization*/
  132. lsp_quant_high,
  133. lsp_unquant_high,
  134. /*Pitch quantization*/
  135. NULL,
  136. NULL,
  137. NULL,
  138. /*Innovation quantization*/
  139. split_cb_search_shape_sign,
  140. split_cb_shape_sign_unquant,
  141. #ifdef DISABLE_WIDEBAND
  142. NULL,
  143. #else
  144. &split_cb_high,
  145. #endif
  146. -1,
  147. 192
  148. };
  149. static const SpeexSubmode wb_submode4 = {
  150. 0,
  151. 0,
  152. 1,
  153. 1,
  154. /*LSP quantization*/
  155. lsp_quant_high,
  156. lsp_unquant_high,
  157. /*Pitch quantization*/
  158. NULL,
  159. NULL,
  160. NULL,
  161. /*Innovation quantization*/
  162. split_cb_search_shape_sign,
  163. split_cb_shape_sign_unquant,
  164. #ifdef DISABLE_WIDEBAND
  165. NULL,
  166. #else
  167. &split_cb_high,
  168. #endif
  169. -1,
  170. 352
  171. };
  172. /* Split-band wideband CELP mode*/
  173. static const SpeexSBMode sb_wb_mode = {
  174. &speex_nb_mode,
  175. 160, /*frameSize*/
  176. 40, /*subframeSize*/
  177. 8, /*lpcSize*/
  178. #ifdef FIXED_POINT
  179. 29491, 19661, /* gamma1, gamma2 */
  180. #else
  181. 0.9, 0.6, /* gamma1, gamma2 */
  182. #endif
  183. QCONST16(.0002,15), /*lpc_floor*/
  184. QCONST16(0.9f,15),
  185. {NULL, &wb_submode1, &wb_submode2, &wb_submode3, &wb_submode4, NULL, NULL, NULL},
  186. 3,
  187. {1, 8, 2, 3, 4, 5, 5, 6, 6, 7, 7},
  188. {1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 4},
  189. #ifndef DISABLE_VBR
  190. vbr_hb_thresh,
  191. #endif
  192. 5
  193. };
  194. EXPORT const SpeexMode speex_wb_mode = {
  195. &sb_wb_mode,
  196. wb_mode_query,
  197. "wideband (sub-band CELP)",
  198. 1,
  199. 4,
  200. sb_encoder_init,
  201. sb_encoder_destroy,
  202. sb_encode,
  203. sb_decoder_init,
  204. sb_decoder_destroy,
  205. sb_decode,
  206. sb_encoder_ctl,
  207. sb_decoder_ctl,
  208. };
  209. /* "Ultra-wideband" mode stuff */
  210. /* Split-band "ultra-wideband" (32 kbps) CELP mode*/
  211. static const SpeexSBMode sb_uwb_mode = {
  212. &speex_wb_mode,
  213. 320, /*frameSize*/
  214. 80, /*subframeSize*/
  215. 8, /*lpcSize*/
  216. #ifdef FIXED_POINT
  217. 29491, 19661, /* gamma1, gamma2 */
  218. #else
  219. 0.9, 0.6, /* gamma1, gamma2 */
  220. #endif
  221. QCONST16(.0002,15), /*lpc_floor*/
  222. QCONST16(0.7f,15),
  223. {NULL, &wb_submode1, NULL, NULL, NULL, NULL, NULL, NULL},
  224. 1,
  225. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
  226. {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  227. #ifndef DISABLE_VBR
  228. vbr_uhb_thresh,
  229. #endif
  230. 2
  231. };
  232. int wb_mode_query(const void *mode, int request, void *ptr)
  233. {
  234. const SpeexSBMode *m = (const SpeexSBMode*)mode;
  235. switch (request)
  236. {
  237. case SPEEX_MODE_FRAME_SIZE:
  238. *((int*)ptr)=2*m->frameSize;
  239. break;
  240. case SPEEX_SUBMODE_BITS_PER_FRAME:
  241. if (*((int*)ptr)==0)
  242. *((int*)ptr) = SB_SUBMODE_BITS+1;
  243. else if (m->submodes[*((int*)ptr)]==NULL)
  244. *((int*)ptr) = -1;
  245. else
  246. *((int*)ptr) = m->submodes[*((int*)ptr)]->bits_per_frame;
  247. break;
  248. default:
  249. speex_warning_int("Unknown wb_mode_query request: ", request);
  250. return -1;
  251. }
  252. return 0;
  253. }
  254. EXPORT const SpeexMode speex_uwb_mode = {
  255. &sb_uwb_mode,
  256. wb_mode_query,
  257. "ultra-wideband (sub-band CELP)",
  258. 2,
  259. 4,
  260. sb_encoder_init,
  261. sb_encoder_destroy,
  262. sb_encode,
  263. sb_decoder_init,
  264. sb_decoder_destroy,
  265. sb_decode,
  266. sb_encoder_ctl,
  267. sb_decoder_ctl,
  268. };
  269. /* We have defined speex_lib_get_mode() as a macro in speex.h */
  270. #undef speex_lib_get_mode
  271. EXPORT const SpeexMode * speex_lib_get_mode (int mode)
  272. {
  273. if (mode < 0 || mode >= SPEEX_NB_MODES) return NULL;
  274. return speex_mode_list[mode];
  275. }