opus_custom.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* Copyright (c) 2007-2012 IETF Trust, CSIRO, Xiph.Org Foundation,
  2. Gregory Maxwell. All rights reserved.
  3. Written by Jean-Marc Valin and Gregory Maxwell */
  4. /**
  5. @file celt.h
  6. @brief Contains all the functions for encoding and decoding audio
  7. */
  8. /*
  9. This file is extracted from RFC6716. Please see that RFC for additional
  10. information.
  11. Redistribution and use in source and binary forms, with or without
  12. modification, are permitted provided that the following conditions
  13. are met:
  14. - Redistributions of source code must retain the above copyright
  15. notice, this list of conditions and the following disclaimer.
  16. - Redistributions in binary form must reproduce the above copyright
  17. notice, this list of conditions and the following disclaimer in the
  18. documentation and/or other materials provided with the distribution.
  19. - Neither the name of Internet Society, IETF or IETF Trust, nor the
  20. names of specific contributors, may be used to endorse or promote
  21. products derived from this software without specific prior written
  22. permission.
  23. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  27. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #ifndef OPUS_CUSTOM_H
  36. #define OPUS_CUSTOM_H
  37. #include "opus_defines.h"
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. #ifdef CUSTOM_MODES
  42. #define OPUS_CUSTOM_EXPORT OPUS_EXPORT
  43. #define OPUS_CUSTOM_EXPORT_STATIC OPUS_EXPORT
  44. #else
  45. #define OPUS_CUSTOM_EXPORT
  46. #ifdef CELT_C
  47. #define OPUS_CUSTOM_EXPORT_STATIC static inline
  48. #else
  49. #define OPUS_CUSTOM_EXPORT_STATIC
  50. #endif
  51. #endif
  52. /** Contains the state of an encoder. One encoder state is needed
  53. for each stream. It is initialised once at the beginning of the
  54. stream. Do *not* re-initialise the state for every frame.
  55. @brief Encoder state
  56. */
  57. typedef struct OpusCustomEncoder OpusCustomEncoder;
  58. /** State of the decoder. One decoder state is needed for each stream.
  59. It is initialised once at the beginning of the stream. Do *not*
  60. re-initialise the state for every frame */
  61. typedef struct OpusCustomDecoder OpusCustomDecoder;
  62. /** The mode contains all the information necessary to create an
  63. encoder. Both the encoder and decoder need to be initialised
  64. with exactly the same mode, otherwise the quality will be very
  65. bad */
  66. typedef struct OpusCustomMode OpusCustomMode;
  67. /** Creates a new mode struct. This will be passed to an encoder or
  68. decoder. The mode MUST NOT BE DESTROYED until the encoders and
  69. decoders that use it are destroyed as well.
  70. @param Fs Sampling rate (32000 to 96000 Hz)
  71. @param frame_size Number of samples (per channel) to encode in each
  72. packet (even values; 64 - 512)
  73. @param error Returned error code (if NULL, no error will be returned)
  74. @return A newly created mode
  75. */
  76. OPUS_CUSTOM_EXPORT OpusCustomMode *opus_custom_mode_create(opus_int32 Fs, int frame_size, int *error);
  77. /** Destroys a mode struct. Only call this after all encoders and
  78. decoders using this mode are destroyed as well.
  79. @param mode Mode to be destroyed
  80. */
  81. OPUS_CUSTOM_EXPORT void opus_custom_mode_destroy(OpusCustomMode *mode);
  82. /* Encoder */
  83. OPUS_CUSTOM_EXPORT_STATIC int opus_custom_encoder_get_size(const OpusCustomMode *mode, int channels);
  84. /** Creates a new encoder state. Each stream needs its own encoder
  85. state (can't be shared across simultaneous streams).
  86. @param mode Contains all the information about the characteristics of
  87. * the stream (must be the same characteristics as used for the
  88. * decoder)
  89. @param channels Number of channels
  90. @param error Returns an error code
  91. @return Newly created encoder state.
  92. */
  93. OPUS_CUSTOM_EXPORT OpusCustomEncoder *opus_custom_encoder_create(const OpusCustomMode *mode, int channels, int *error);
  94. OPUS_CUSTOM_EXPORT_STATIC int opus_custom_encoder_init(OpusCustomEncoder *st, const OpusCustomMode *mode, int channels);
  95. /** Destroys a an encoder state.
  96. @param st Encoder state to be destroyed
  97. */
  98. OPUS_CUSTOM_EXPORT void opus_custom_encoder_destroy(OpusCustomEncoder *st);
  99. /** Encodes a frame of audio.
  100. @param st Encoder state
  101. @param pcm PCM audio in float format, with a normal range of +/-1.0.
  102. * Samples with a range beyond +/-1.0 are supported but will
  103. * be clipped by decoders using the integer API and should
  104. * only be used if it is known that the far end supports
  105. * extended dynmaic range. There must be exactly
  106. * frame_size samples per channel.
  107. @param compressed The compressed data is written here. This may not alias pcm or
  108. * optional_synthesis.
  109. @param nbCompressedBytes Maximum number of bytes to use for compressing the frame
  110. * (can change from one frame to another)
  111. @return Number of bytes written to "compressed". Will be the same as
  112. * "nbCompressedBytes" unless the stream is VBR and will never be larger.
  113. * If negative, an error has occurred (see error codes). It is IMPORTANT that
  114. * the length returned be somehow transmitted to the decoder. Otherwise, no
  115. * decoding is possible.
  116. */
  117. OPUS_CUSTOM_EXPORT int opus_custom_encode_float(OpusCustomEncoder *st, const float *pcm, int frame_size, unsigned char *compressed, int maxCompressedBytes);
  118. /** Encodes a frame of audio.
  119. @param st Encoder state
  120. @param pcm PCM audio in signed 16-bit format (native endian). There must be
  121. * exactly frame_size samples per channel.
  122. @param compressed The compressed data is written here. This may not alias pcm or
  123. * optional_synthesis.
  124. @param nbCompressedBytes Maximum number of bytes to use for compressing the frame
  125. * (can change from one frame to another)
  126. @return Number of bytes written to "compressed". Will be the same as
  127. * "nbCompressedBytes" unless the stream is VBR and will never be larger.
  128. * If negative, an error has occurred (see error codes). It is IMPORTANT that
  129. * the length returned be somehow transmitted to the decoder. Otherwise, no
  130. * decoding is possible.
  131. */
  132. OPUS_CUSTOM_EXPORT int opus_custom_encode(OpusCustomEncoder *st, const opus_int16 *pcm, int frame_size, unsigned char *compressed, int maxCompressedBytes);
  133. /** Query and set encoder parameters
  134. @param st Encoder state
  135. @param request Parameter to change or query
  136. @param value Pointer to a 32-bit int value
  137. @return Error code
  138. */
  139. OPUS_CUSTOM_EXPORT int opus_custom_encoder_ctl(OpusCustomEncoder * restrict st, int request, ...);
  140. /* Decoder */
  141. OPUS_CUSTOM_EXPORT_STATIC int opus_custom_decoder_get_size(const OpusCustomMode *mode, int channels);
  142. /** Creates a new decoder state. Each stream needs its own decoder state (can't
  143. be shared across simultaneous streams).
  144. @param mode Contains all the information about the characteristics of the
  145. stream (must be the same characteristics as used for the encoder)
  146. @param channels Number of channels
  147. @param error Returns an error code
  148. @return Newly created decoder state.
  149. */
  150. OPUS_CUSTOM_EXPORT OpusCustomDecoder *opus_custom_decoder_create(const OpusCustomMode *mode, int channels, int *error);
  151. OPUS_CUSTOM_EXPORT_STATIC int opus_custom_decoder_init(OpusCustomDecoder *st, const OpusCustomMode *mode, int channels);
  152. /** Destroys a a decoder state.
  153. @param st Decoder state to be destroyed
  154. */
  155. OPUS_CUSTOM_EXPORT void opus_custom_decoder_destroy(OpusCustomDecoder *st);
  156. /** Decodes a frame of audio.
  157. @param st Decoder state
  158. @param data Compressed data produced by an encoder
  159. @param len Number of bytes to read from "data". This MUST be exactly the number
  160. of bytes returned by the encoder. Using a larger value WILL NOT WORK.
  161. @param pcm One frame (frame_size samples per channel) of decoded PCM will be
  162. returned here in float format.
  163. @return Error code.
  164. */
  165. OPUS_CUSTOM_EXPORT int opus_custom_decode_float(OpusCustomDecoder *st, const unsigned char *data, int len, float *pcm, int frame_size);
  166. /** Decodes a frame of audio.
  167. @param st Decoder state
  168. @param data Compressed data produced by an encoder
  169. @param len Number of bytes to read from "data". This MUST be exactly the number
  170. of bytes returned by the encoder. Using a larger value WILL NOT WORK.
  171. @param pcm One frame (frame_size samples per channel) of decoded PCM will be
  172. returned here in 16-bit PCM format (native endian).
  173. @return Error code.
  174. */
  175. OPUS_CUSTOM_EXPORT int opus_custom_decode(OpusCustomDecoder *st, const unsigned char *data, int len, opus_int16 *pcm, int frame_size);
  176. /** Query and set decoder parameters
  177. @param st Decoder state
  178. @param request Parameter to change or query
  179. @param value Pointer to a 32-bit int value
  180. @return Error code
  181. */
  182. OPUS_CUSTOM_EXPORT int opus_custom_decoder_ctl(OpusCustomDecoder * restrict st, int request, ...);
  183. #ifdef __cplusplus
  184. }
  185. #endif
  186. #endif /* OPUS_CUSTOM_H */