opus_custom.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* Copyright (c) 2007-2008 CSIRO
  2. Copyright (c) 2007-2009 Xiph.Org Foundation
  3. Copyright (c) 2008-2012 Gregory Maxwell
  4. Written by Jean-Marc Valin and Gregory Maxwell */
  5. /*
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions
  8. are met:
  9. - Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. - Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in the
  13. documentation and/or other materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  18. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  22. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /**
  27. @file opus_custom.h
  28. @brief Opus-Custom reference implementation API
  29. */
  30. #ifndef OPUS_CUSTOM_H
  31. #define OPUS_CUSTOM_H
  32. #include "opus_defines.h"
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. #ifdef CUSTOM_MODES
  37. #define OPUS_CUSTOM_EXPORT OPUS_EXPORT
  38. #define OPUS_CUSTOM_EXPORT_STATIC OPUS_EXPORT
  39. #else
  40. #define OPUS_CUSTOM_EXPORT
  41. #ifdef CELT_C
  42. #define OPUS_CUSTOM_EXPORT_STATIC static inline
  43. #else
  44. #define OPUS_CUSTOM_EXPORT_STATIC
  45. #endif
  46. #endif
  47. /** @defgroup opus_custom Opus Custom
  48. * @{
  49. * Opus Custom is an optional part of the Opus specification and
  50. * reference implementation which uses a distinct API from the regular
  51. * API and supports frame sizes that are not normally supported.\ Use
  52. * of Opus Custom is discouraged for all but very special applications
  53. * for which a frame size different from 2.5, 5, 10, or 20 ms is needed
  54. * (for either complexity or latency reasons) and where interoperability
  55. * is less important.
  56. *
  57. * In addition to the interoperability limitations the use of Opus custom
  58. * disables a substantial chunk of the codec and generally lowers the
  59. * quality available at a given bitrate. Normally when an application needs
  60. * a different frame size from the codec it should buffer to match the
  61. * sizes but this adds a small amount of delay which may be important
  62. * in some very low latency applications. Some transports (especially
  63. * constant rate RF transports) may also work best with frames of
  64. * particular durations.
  65. *
  66. * Libopus only supports custom modes if they are enabled at compile time.
  67. *
  68. * The Opus Custom API is similar to the regular API but the
  69. * @ref opus_encoder_create and @ref opus_decoder_create calls take
  70. * an additional mode parameter which is a structure produced by
  71. * a call to @ref opus_custom_mode_create. Both the encoder and decoder
  72. * must create a mode using the same sample rate (fs) and frame size
  73. * (frame size) so these parameters must either be signaled out of band
  74. * or fixed in a particular implementation.
  75. *
  76. * Similar to regular Opus the custom modes support on the fly frame size
  77. * switching, but the sizes available depend on the particular frame size in
  78. * use. For some initial frame sizes on a single on the fly size is available.
  79. */
  80. /** Contains the state of an encoder. One encoder state is needed
  81. for each stream. It is initialized once at the beginning of the
  82. stream. Do *not* re-initialize the state for every frame.
  83. @brief Encoder state
  84. */
  85. typedef struct OpusCustomEncoder OpusCustomEncoder;
  86. /** State of the decoder. One decoder state is needed for each stream.
  87. It is initialized once at the beginning of the stream. Do *not*
  88. re-initialize the state for every frame.
  89. @brief Decoder state
  90. */
  91. typedef struct OpusCustomDecoder OpusCustomDecoder;
  92. /** The mode contains all the information necessary to create an
  93. encoder. Both the encoder and decoder need to be initialized
  94. with exactly the same mode, otherwise the output will be
  95. corrupted.
  96. @brief Mode configuration
  97. */
  98. typedef struct OpusCustomMode OpusCustomMode;
  99. /** Creates a new mode struct. This will be passed to an encoder or
  100. * decoder. The mode MUST NOT BE DESTROYED until the encoders and
  101. * decoders that use it are destroyed as well.
  102. * @param [in] Fs <tt>int</tt>: Sampling rate (8000 to 96000 Hz)
  103. * @param [in] frame_size <tt>int</tt>: Number of samples (per channel) to encode in each
  104. * packet (64 - 1024, prime factorization must contain zero or more 2s, 3s, or 5s and no other primes)
  105. * @param [out] error <tt>int*</tt>: Returned error code (if NULL, no error will be returned)
  106. * @return A newly created mode
  107. */
  108. OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT OpusCustomMode *opus_custom_mode_create(opus_int32 Fs, int frame_size, int *error);
  109. /** Destroys a mode struct. Only call this after all encoders and
  110. * decoders using this mode are destroyed as well.
  111. * @param [in] mode <tt>OpusCustomMode*</tt>: Mode to be freed.
  112. */
  113. OPUS_CUSTOM_EXPORT void opus_custom_mode_destroy(OpusCustomMode *mode);
  114. /* Encoder */
  115. /** Gets the size of an OpusCustomEncoder structure.
  116. * @param [in] mode <tt>OpusCustomMode *</tt>: Mode configuration
  117. * @param [in] channels <tt>int</tt>: Number of channels
  118. * @returns size
  119. */
  120. OPUS_CUSTOM_EXPORT_STATIC OPUS_WARN_UNUSED_RESULT int opus_custom_encoder_get_size(
  121. const OpusCustomMode *mode,
  122. int channels
  123. ) OPUS_ARG_NONNULL(1);
  124. /** Creates a new encoder state. Each stream needs its own encoder
  125. * state (can't be shared across simultaneous streams).
  126. * @param [in] mode <tt>OpusCustomMode*</tt>: Contains all the information about the characteristics of
  127. * the stream (must be the same characteristics as used for the
  128. * decoder)
  129. * @param [in] channels <tt>int</tt>: Number of channels
  130. * @param [out] error <tt>int*</tt>: Returns an error code
  131. * @return Newly created encoder state.
  132. */
  133. OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT OpusCustomEncoder *opus_custom_encoder_create(
  134. const OpusCustomMode *mode,
  135. int channels,
  136. int *error
  137. ) OPUS_ARG_NONNULL(1);
  138. /** Initializes a previously allocated encoder state
  139. * The memory pointed to by st must be the size returned by opus_custom_encoder_get_size.
  140. * This is intended for applications which use their own allocator instead of malloc.
  141. * @see opus_custom_encoder_create(),opus_custom_encoder_get_size()
  142. * To reset a previously initialized state use the OPUS_RESET_STATE CTL.
  143. * @param [in] st <tt>OpusCustomEncoder*</tt>: Encoder state
  144. * @param [in] mode <tt>OpusCustomMode *</tt>: Contains all the information about the characteristics of
  145. * the stream (must be the same characteristics as used for the
  146. * decoder)
  147. * @param [in] channels <tt>int</tt>: Number of channels
  148. * @return OPUS_OK Success or @ref opus_errorcodes
  149. */
  150. OPUS_CUSTOM_EXPORT_STATIC int opus_custom_encoder_init(
  151. OpusCustomEncoder *st,
  152. const OpusCustomMode *mode,
  153. int channels
  154. ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2);
  155. /** Destroys a an encoder state.
  156. * @param[in] st <tt>OpusCustomEncoder*</tt>: State to be freed.
  157. */
  158. OPUS_CUSTOM_EXPORT void opus_custom_encoder_destroy(OpusCustomEncoder *st);
  159. /** Encodes a frame of audio.
  160. * @param [in] st <tt>OpusCustomEncoder*</tt>: Encoder state
  161. * @param [in] pcm <tt>float*</tt>: PCM audio in float format, with a normal range of +/-1.0.
  162. * Samples with a range beyond +/-1.0 are supported but will
  163. * be clipped by decoders using the integer API and should
  164. * only be used if it is known that the far end supports
  165. * extended dynamic range. There must be exactly
  166. * frame_size samples per channel.
  167. * @param [in] frame_size <tt>int</tt>: Number of samples per frame of input signal
  168. * @param [out] compressed <tt>char *</tt>: The compressed data is written here. This may not alias pcm and must be at least maxCompressedBytes long.
  169. * @param [in] maxCompressedBytes <tt>int</tt>: Maximum number of bytes to use for compressing the frame
  170. * (can change from one frame to another)
  171. * @return Number of bytes written to "compressed".
  172. * If negative, an error has occurred (see error codes). It is IMPORTANT that
  173. * the length returned be somehow transmitted to the decoder. Otherwise, no
  174. * decoding is possible.
  175. */
  176. OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT int opus_custom_encode_float(
  177. OpusCustomEncoder *st,
  178. const float *pcm,
  179. int frame_size,
  180. unsigned char *compressed,
  181. int maxCompressedBytes
  182. ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4);
  183. /** Encodes a frame of audio.
  184. * @param [in] st <tt>OpusCustomEncoder*</tt>: Encoder state
  185. * @param [in] pcm <tt>opus_int16*</tt>: PCM audio in signed 16-bit format (native endian).
  186. * There must be exactly frame_size samples per channel.
  187. * @param [in] frame_size <tt>int</tt>: Number of samples per frame of input signal
  188. * @param [out] compressed <tt>char *</tt>: The compressed data is written here. This may not alias pcm and must be at least maxCompressedBytes long.
  189. * @param [in] maxCompressedBytes <tt>int</tt>: Maximum number of bytes to use for compressing the frame
  190. * (can change from one frame to another)
  191. * @return Number of bytes written to "compressed".
  192. * If negative, an error has occurred (see error codes). It is IMPORTANT that
  193. * the length returned be somehow transmitted to the decoder. Otherwise, no
  194. * decoding is possible.
  195. */
  196. OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT int opus_custom_encode(
  197. OpusCustomEncoder *st,
  198. const opus_int16 *pcm,
  199. int frame_size,
  200. unsigned char *compressed,
  201. int maxCompressedBytes
  202. ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4);
  203. /** Perform a CTL function on an Opus custom encoder.
  204. *
  205. * Generally the request and subsequent arguments are generated
  206. * by a convenience macro.
  207. * @see opus_encoderctls
  208. */
  209. OPUS_CUSTOM_EXPORT int opus_custom_encoder_ctl(OpusCustomEncoder * OPUS_RESTRICT st, int request, ...) OPUS_ARG_NONNULL(1);
  210. /* Decoder */
  211. /** Gets the size of an OpusCustomDecoder structure.
  212. * @param [in] mode <tt>OpusCustomMode *</tt>: Mode configuration
  213. * @param [in] channels <tt>int</tt>: Number of channels
  214. * @returns size
  215. */
  216. OPUS_CUSTOM_EXPORT_STATIC OPUS_WARN_UNUSED_RESULT int opus_custom_decoder_get_size(
  217. const OpusCustomMode *mode,
  218. int channels
  219. ) OPUS_ARG_NONNULL(1);
  220. /** Creates a new decoder state. Each stream needs its own decoder state (can't
  221. * be shared across simultaneous streams).
  222. * @param [in] mode <tt>OpusCustomMode</tt>: Contains all the information about the characteristics of the
  223. * stream (must be the same characteristics as used for the encoder)
  224. * @param [in] channels <tt>int</tt>: Number of channels
  225. * @param [out] error <tt>int*</tt>: Returns an error code
  226. * @return Newly created decoder state.
  227. */
  228. OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT OpusCustomDecoder *opus_custom_decoder_create(
  229. const OpusCustomMode *mode,
  230. int channels,
  231. int *error
  232. ) OPUS_ARG_NONNULL(1);
  233. /** Initializes a previously allocated decoder state
  234. * The memory pointed to by st must be the size returned by opus_custom_decoder_get_size.
  235. * This is intended for applications which use their own allocator instead of malloc.
  236. * @see opus_custom_decoder_create(),opus_custom_decoder_get_size()
  237. * To reset a previously initialized state use the OPUS_RESET_STATE CTL.
  238. * @param [in] st <tt>OpusCustomDecoder*</tt>: Decoder state
  239. * @param [in] mode <tt>OpusCustomMode *</tt>: Contains all the information about the characteristics of
  240. * the stream (must be the same characteristics as used for the
  241. * encoder)
  242. * @param [in] channels <tt>int</tt>: Number of channels
  243. * @return OPUS_OK Success or @ref opus_errorcodes
  244. */
  245. OPUS_CUSTOM_EXPORT_STATIC int opus_custom_decoder_init(
  246. OpusCustomDecoder *st,
  247. const OpusCustomMode *mode,
  248. int channels
  249. ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2);
  250. /** Destroys a an decoder state.
  251. * @param[in] st <tt>OpusCustomDecoder*</tt>: State to be freed.
  252. */
  253. OPUS_CUSTOM_EXPORT void opus_custom_decoder_destroy(OpusCustomDecoder *st);
  254. /** Decode an opus custom frame with floating point output
  255. * @param [in] st <tt>OpusCustomDecoder*</tt>: Decoder state
  256. * @param [in] data <tt>char*</tt>: Input payload. Use a NULL pointer to indicate packet loss
  257. * @param [in] len <tt>int</tt>: Number of bytes in payload
  258. * @param [out] pcm <tt>float*</tt>: Output signal (interleaved if 2 channels). length
  259. * is frame_size*channels*sizeof(float)
  260. * @param [in] frame_size Number of samples per channel of available space in *pcm.
  261. * @returns Number of decoded samples or @ref opus_errorcodes
  262. */
  263. OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT int opus_custom_decode_float(
  264. OpusCustomDecoder *st,
  265. const unsigned char *data,
  266. int len,
  267. float *pcm,
  268. int frame_size
  269. ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);
  270. /** Decode an opus custom frame
  271. * @param [in] st <tt>OpusCustomDecoder*</tt>: Decoder state
  272. * @param [in] data <tt>char*</tt>: Input payload. Use a NULL pointer to indicate packet loss
  273. * @param [in] len <tt>int</tt>: Number of bytes in payload
  274. * @param [out] pcm <tt>opus_int16*</tt>: Output signal (interleaved if 2 channels). length
  275. * is frame_size*channels*sizeof(opus_int16)
  276. * @param [in] frame_size Number of samples per channel of available space in *pcm.
  277. * @returns Number of decoded samples or @ref opus_errorcodes
  278. */
  279. OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT int opus_custom_decode(
  280. OpusCustomDecoder *st,
  281. const unsigned char *data,
  282. int len,
  283. opus_int16 *pcm,
  284. int frame_size
  285. ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);
  286. /** Perform a CTL function on an Opus custom decoder.
  287. *
  288. * Generally the request and subsequent arguments are generated
  289. * by a convenience macro.
  290. * @see opus_genericctls
  291. */
  292. OPUS_CUSTOM_EXPORT int opus_custom_decoder_ctl(OpusCustomDecoder * OPUS_RESTRICT st, int request, ...) OPUS_ARG_NONNULL(1);
  293. /**@}*/
  294. #ifdef __cplusplus
  295. }
  296. #endif
  297. #endif /* OPUS_CUSTOM_H */