speex.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* Copyright (C) 2002-2006 Jean-Marc Valin*/
  2. /**
  3. @file speex.h
  4. @brief Describes the different modes of the codec
  5. */
  6. /*
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions
  9. are met:
  10. - Redistributions of source code must retain the above copyright
  11. notice, this list of conditions and the following disclaimer.
  12. - Redistributions in binary form must reproduce the above copyright
  13. notice, this list of conditions and the following disclaimer in the
  14. documentation and/or other materials provided with the distribution.
  15. - Neither the name of the Xiph.org Foundation nor the names of its
  16. contributors may be used to endorse or promote products derived from
  17. this software without specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  22. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef SPEEX_H
  31. #define SPEEX_H
  32. /** @defgroup Codec Speex encoder and decoder
  33. * This is the Speex codec itself.
  34. * @{
  35. */
  36. #include "speex_types.h"
  37. #include "speex_bits.h"
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /* Values allowed for *ctl() requests */
  42. /** Set enhancement on/off (decoder only) */
  43. #define SPEEX_SET_ENH 0
  44. /** Get enhancement state (decoder only) */
  45. #define SPEEX_GET_ENH 1
  46. /*Would be SPEEX_SET_FRAME_SIZE, but it's (currently) invalid*/
  47. /** Obtain frame size used by encoder/decoder */
  48. #define SPEEX_GET_FRAME_SIZE 3
  49. /** Set quality value */
  50. #define SPEEX_SET_QUALITY 4
  51. /** Get current quality setting */
  52. /* #define SPEEX_GET_QUALITY 5 -- Doesn't make much sense, does it? */
  53. /** Set sub-mode to use */
  54. #define SPEEX_SET_MODE 6
  55. /** Get current sub-mode in use */
  56. #define SPEEX_GET_MODE 7
  57. /** Set low-band sub-mode to use (wideband only)*/
  58. #define SPEEX_SET_LOW_MODE 8
  59. /** Get current low-band mode in use (wideband only)*/
  60. #define SPEEX_GET_LOW_MODE 9
  61. /** Set high-band sub-mode to use (wideband only)*/
  62. #define SPEEX_SET_HIGH_MODE 10
  63. /** Get current high-band mode in use (wideband only)*/
  64. #define SPEEX_GET_HIGH_MODE 11
  65. /** Set VBR on (1) or off (0) */
  66. #define SPEEX_SET_VBR 12
  67. /** Get VBR status (1 for on, 0 for off) */
  68. #define SPEEX_GET_VBR 13
  69. /** Set quality value for VBR encoding (0-10) */
  70. #define SPEEX_SET_VBR_QUALITY 14
  71. /** Get current quality value for VBR encoding (0-10) */
  72. #define SPEEX_GET_VBR_QUALITY 15
  73. /** Set complexity of the encoder (0-10) */
  74. #define SPEEX_SET_COMPLEXITY 16
  75. /** Get current complexity of the encoder (0-10) */
  76. #define SPEEX_GET_COMPLEXITY 17
  77. /** Set bit-rate used by the encoder (or lower) */
  78. #define SPEEX_SET_BITRATE 18
  79. /** Get current bit-rate used by the encoder or decoder */
  80. #define SPEEX_GET_BITRATE 19
  81. /** Define a handler function for in-band Speex request*/
  82. #define SPEEX_SET_HANDLER 20
  83. /** Define a handler function for in-band user-defined request*/
  84. #define SPEEX_SET_USER_HANDLER 22
  85. /** Set sampling rate used in bit-rate computation */
  86. #define SPEEX_SET_SAMPLING_RATE 24
  87. /** Get sampling rate used in bit-rate computation */
  88. #define SPEEX_GET_SAMPLING_RATE 25
  89. /** Reset the encoder/decoder memories to zero*/
  90. #define SPEEX_RESET_STATE 26
  91. /** Get VBR info (mostly used internally) */
  92. #define SPEEX_GET_RELATIVE_QUALITY 29
  93. /** Set VAD status (1 for on, 0 for off) */
  94. #define SPEEX_SET_VAD 30
  95. /** Get VAD status (1 for on, 0 for off) */
  96. #define SPEEX_GET_VAD 31
  97. /** Set Average Bit-Rate (ABR) to n bits per seconds */
  98. #define SPEEX_SET_ABR 32
  99. /** Get Average Bit-Rate (ABR) setting (in bps) */
  100. #define SPEEX_GET_ABR 33
  101. /** Set DTX status (1 for on, 0 for off) */
  102. #define SPEEX_SET_DTX 34
  103. /** Get DTX status (1 for on, 0 for off) */
  104. #define SPEEX_GET_DTX 35
  105. /** Set submode encoding in each frame (1 for yes, 0 for no, setting to no breaks the standard) */
  106. #define SPEEX_SET_SUBMODE_ENCODING 36
  107. /** Get submode encoding in each frame */
  108. #define SPEEX_GET_SUBMODE_ENCODING 37
  109. /*#define SPEEX_SET_LOOKAHEAD 38*/
  110. /** Returns the lookahead used by Speex separately for an encoder and a decoder.
  111. * Sum encoder and decoder lookahead values to get the total codec lookahead. */
  112. #define SPEEX_GET_LOOKAHEAD 39
  113. /** Sets tuning for packet-loss concealment (expected loss rate) */
  114. #define SPEEX_SET_PLC_TUNING 40
  115. /** Gets tuning for PLC */
  116. #define SPEEX_GET_PLC_TUNING 41
  117. /** Sets the max bit-rate allowed in VBR mode */
  118. #define SPEEX_SET_VBR_MAX_BITRATE 42
  119. /** Gets the max bit-rate allowed in VBR mode */
  120. #define SPEEX_GET_VBR_MAX_BITRATE 43
  121. /** Turn on/off input/output high-pass filtering */
  122. #define SPEEX_SET_HIGHPASS 44
  123. /** Get status of input/output high-pass filtering */
  124. #define SPEEX_GET_HIGHPASS 45
  125. /** Get "activity level" of the last decoded frame, i.e.
  126. how much damage we cause if we remove the frame */
  127. #define SPEEX_GET_ACTIVITY 47
  128. /* Preserving compatibility:*/
  129. /** Equivalent to SPEEX_SET_ENH */
  130. #define SPEEX_SET_PF 0
  131. /** Equivalent to SPEEX_GET_ENH */
  132. #define SPEEX_GET_PF 1
  133. /* Values allowed for mode queries */
  134. /** Query the frame size of a mode */
  135. #define SPEEX_MODE_FRAME_SIZE 0
  136. /** Query the size of an encoded frame for a particular sub-mode */
  137. #define SPEEX_SUBMODE_BITS_PER_FRAME 1
  138. /** Get major Speex version */
  139. #define SPEEX_LIB_GET_MAJOR_VERSION 1
  140. /** Get minor Speex version */
  141. #define SPEEX_LIB_GET_MINOR_VERSION 3
  142. /** Get micro Speex version */
  143. #define SPEEX_LIB_GET_MICRO_VERSION 5
  144. /** Get extra Speex version */
  145. #define SPEEX_LIB_GET_EXTRA_VERSION 7
  146. /** Get Speex version string */
  147. #define SPEEX_LIB_GET_VERSION_STRING 9
  148. /*#define SPEEX_LIB_SET_ALLOC_FUNC 10
  149. #define SPEEX_LIB_GET_ALLOC_FUNC 11
  150. #define SPEEX_LIB_SET_FREE_FUNC 12
  151. #define SPEEX_LIB_GET_FREE_FUNC 13
  152. #define SPEEX_LIB_SET_WARNING_FUNC 14
  153. #define SPEEX_LIB_GET_WARNING_FUNC 15
  154. #define SPEEX_LIB_SET_ERROR_FUNC 16
  155. #define SPEEX_LIB_GET_ERROR_FUNC 17
  156. */
  157. /** Number of defined modes in Speex */
  158. #define SPEEX_NB_MODES 3
  159. /** modeID for the defined narrowband mode */
  160. #define SPEEX_MODEID_NB 0
  161. /** modeID for the defined wideband mode */
  162. #define SPEEX_MODEID_WB 1
  163. /** modeID for the defined ultra-wideband mode */
  164. #define SPEEX_MODEID_UWB 2
  165. struct SpeexMode;
  166. /* Prototypes for mode function pointers */
  167. /** Encoder state initialization function */
  168. typedef void *(*encoder_init_func)(const struct SpeexMode *mode);
  169. /** Encoder state destruction function */
  170. typedef void (*encoder_destroy_func)(void *st);
  171. /** Main encoding function */
  172. typedef int (*encode_func)(void *state, void *in, SpeexBits *bits);
  173. /** Function for controlling the encoder options */
  174. typedef int (*encoder_ctl_func)(void *state, int request, void *ptr);
  175. /** Decoder state initialization function */
  176. typedef void *(*decoder_init_func)(const struct SpeexMode *mode);
  177. /** Decoder state destruction function */
  178. typedef void (*decoder_destroy_func)(void *st);
  179. /** Main decoding function */
  180. typedef int (*decode_func)(void *state, SpeexBits *bits, void *out);
  181. /** Function for controlling the decoder options */
  182. typedef int (*decoder_ctl_func)(void *state, int request, void *ptr);
  183. /** Query function for a mode */
  184. typedef int (*mode_query_func)(const void *mode, int request, void *ptr);
  185. /** Struct defining a Speex mode */
  186. typedef struct SpeexMode {
  187. /** Pointer to the low-level mode data */
  188. const void *mode;
  189. /** Pointer to the mode query function */
  190. mode_query_func query;
  191. /** The name of the mode (you should not rely on this to identify the mode)*/
  192. const char *modeName;
  193. /**ID of the mode*/
  194. int modeID;
  195. /**Version number of the bitstream (incremented every time we break
  196. bitstream compatibility*/
  197. int bitstream_version;
  198. /** Pointer to encoder initialization function */
  199. encoder_init_func enc_init;
  200. /** Pointer to encoder destruction function */
  201. encoder_destroy_func enc_destroy;
  202. /** Pointer to frame encoding function */
  203. encode_func enc;
  204. /** Pointer to decoder initialization function */
  205. decoder_init_func dec_init;
  206. /** Pointer to decoder destruction function */
  207. decoder_destroy_func dec_destroy;
  208. /** Pointer to frame decoding function */
  209. decode_func dec;
  210. /** ioctl-like requests for encoder */
  211. encoder_ctl_func enc_ctl;
  212. /** ioctl-like requests for decoder */
  213. decoder_ctl_func dec_ctl;
  214. } SpeexMode;
  215. /**
  216. * Returns a handle to a newly created Speex encoder state structure. For now,
  217. * the "mode" argument can be &nb_mode or &wb_mode . In the future, more modes
  218. * may be added. Note that for now if you have more than one channels to
  219. * encode, you need one state per channel.
  220. *
  221. * @param mode The mode to use (either speex_nb_mode or speex_wb.mode)
  222. * @return A newly created encoder state or NULL if state allocation fails
  223. */
  224. void *speex_encoder_init(const SpeexMode *mode);
  225. /** Frees all resources associated to an existing Speex encoder state.
  226. * @param state Encoder state to be destroyed */
  227. void speex_encoder_destroy(void *state);
  228. /** Uses an existing encoder state to encode one frame of speech pointed to by
  229. "in". The encoded bit-stream is saved in "bits".
  230. @param state Encoder state
  231. @param in Frame that will be encoded with a +-2^15 range. This data MAY be
  232. overwritten by the encoder and should be considered uninitialised
  233. after the call.
  234. @param bits Bit-stream where the data will be written
  235. @return 0 if frame needs not be transmitted (DTX only), 1 otherwise
  236. */
  237. int speex_encode(void *state, float *in, SpeexBits *bits);
  238. /** Uses an existing encoder state to encode one frame of speech pointed to by
  239. "in". The encoded bit-stream is saved in "bits".
  240. @param state Encoder state
  241. @param in Frame that will be encoded with a +-2^15 range
  242. @param bits Bit-stream where the data will be written
  243. @return 0 if frame needs not be transmitted (DTX only), 1 otherwise
  244. */
  245. int speex_encode_int(void *state, spx_int16_t *in, SpeexBits *bits);
  246. /** Used like the ioctl function to control the encoder parameters
  247. *
  248. * @param state Encoder state
  249. * @param request ioctl-type request (one of the SPEEX_* macros)
  250. * @param ptr Data exchanged to-from function
  251. * @return 0 if no error, -1 if request in unknown, -2 for invalid parameter
  252. */
  253. int speex_encoder_ctl(void *state, int request, void *ptr);
  254. /** Returns a handle to a newly created decoder state structure. For now,
  255. * the mode argument can be &nb_mode or &wb_mode . In the future, more modes
  256. * may be added. Note that for now if you have more than one channels to
  257. * decode, you need one state per channel.
  258. *
  259. * @param mode Speex mode (one of speex_nb_mode or speex_wb_mode)
  260. * @return A newly created decoder state or NULL if state allocation fails
  261. */
  262. void *speex_decoder_init(const SpeexMode *mode);
  263. /** Frees all resources associated to an existing decoder state.
  264. *
  265. * @param state State to be destroyed
  266. */
  267. void speex_decoder_destroy(void *state);
  268. /** Uses an existing decoder state to decode one frame of speech from
  269. * bit-stream bits. The output speech is saved written to out.
  270. *
  271. * @param state Decoder state
  272. * @param bits Bit-stream from which to decode the frame (NULL if the packet was lost)
  273. * @param out Where to write the decoded frame
  274. * @return return status (0 for no error, -1 for end of stream, -2 corrupt stream)
  275. */
  276. int speex_decode(void *state, SpeexBits *bits, float *out);
  277. /** Uses an existing decoder state to decode one frame of speech from
  278. * bit-stream bits. The output speech is saved written to out.
  279. *
  280. * @param state Decoder state
  281. * @param bits Bit-stream from which to decode the frame (NULL if the packet was lost)
  282. * @param out Where to write the decoded frame
  283. * @return return status (0 for no error, -1 for end of stream, -2 corrupt stream)
  284. */
  285. int speex_decode_int(void *state, SpeexBits *bits, spx_int16_t *out);
  286. /** Used like the ioctl function to control the encoder parameters
  287. *
  288. * @param state Decoder state
  289. * @param request ioctl-type request (one of the SPEEX_* macros)
  290. * @param ptr Data exchanged to-from function
  291. * @return 0 if no error, -1 if request in unknown, -2 for invalid parameter
  292. */
  293. int speex_decoder_ctl(void *state, int request, void *ptr);
  294. /** Query function for mode information
  295. *
  296. * @param mode Speex mode
  297. * @param request ioctl-type request (one of the SPEEX_* macros)
  298. * @param ptr Data exchanged to-from function
  299. * @return 0 if no error, -1 if request in unknown, -2 for invalid parameter
  300. */
  301. int speex_mode_query(const SpeexMode *mode, int request, void *ptr);
  302. /** Functions for controlling the behavior of libspeex
  303. * @param request ioctl-type request (one of the SPEEX_LIB_* macros)
  304. * @param ptr Data exchanged to-from function
  305. * @return 0 if no error, -1 if request in unknown, -2 for invalid parameter
  306. */
  307. int speex_lib_ctl(int request, void *ptr);
  308. /** Default narrowband mode */
  309. extern const SpeexMode speex_nb_mode;
  310. /** Default wideband mode */
  311. extern const SpeexMode speex_wb_mode;
  312. /** Default "ultra-wideband" mode */
  313. extern const SpeexMode speex_uwb_mode;
  314. /** List of all modes available */
  315. extern const SpeexMode * const speex_mode_list[SPEEX_NB_MODES];
  316. /** Obtain one of the modes available */
  317. const SpeexMode * speex_lib_get_mode (int mode);
  318. #ifndef WIN32
  319. /* We actually override the function in the narrowband case so that we can avoid linking in the wideband stuff */
  320. #define speex_lib_get_mode(mode) ((mode)==SPEEX_MODEID_NB ? &speex_nb_mode : speex_lib_get_mode (mode))
  321. #endif
  322. #ifdef __cplusplus
  323. }
  324. #endif
  325. /** @}*/
  326. #endif