opus_custom.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* Copyright (c) 2007-2008 CSIRO
  2. Copyright (c) 2007-2009 Xiph.Org Foundation
  3. Copyright (c) 2008 Gregory Maxwell
  4. Written by Jean-Marc Valin and Gregory Maxwell */
  5. /**
  6. @file celt.h
  7. @brief Contains all the functions for encoding and decoding audio
  8. */
  9. /*
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. - Redistributions of source code must retain the above copyright
  14. notice, this list of conditions and the following disclaimer.
  15. - Redistributions in binary form must reproduce the above copyright
  16. notice, this list of conditions and the following disclaimer in the
  17. documentation and/or other materials provided with the distribution.
  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 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. #else
  39. #define OPUS_CUSTOM_EXPORT
  40. #endif
  41. /** Contains the state of an encoder. One encoder state is needed
  42. for each stream. It is initialised once at the beginning of the
  43. stream. Do *not* re-initialise the state for every frame.
  44. @brief Encoder state
  45. */
  46. typedef struct CELTEncoder CELTEncoder;
  47. /** State of the decoder. One decoder state is needed for each stream.
  48. It is initialised once at the beginning of the stream. Do *not*
  49. re-initialise the state for every frame */
  50. typedef struct CELTDecoder CELTDecoder;
  51. /** The mode contains all the information necessary to create an
  52. encoder. Both the encoder and decoder need to be initialised
  53. with exactly the same mode, otherwise the quality will be very
  54. bad */
  55. typedef struct CELTMode CELTMode;
  56. #define OpusCustomEncoder CELTEncoder
  57. #define OpusCustomDecoder CELTDecoder
  58. #define OpusCustomMode CELTMode
  59. /** Creates a new mode struct. This will be passed to an encoder or
  60. decoder. The mode MUST NOT BE DESTROYED until the encoders and
  61. decoders that use it are destroyed as well.
  62. @param Fs Sampling rate (32000 to 96000 Hz)
  63. @param frame_size Number of samples (per channel) to encode in each
  64. packet (even values; 64 - 512)
  65. @param error Returned error code (if NULL, no error will be returned)
  66. @return A newly created mode
  67. */
  68. OPUS_CUSTOM_EXPORT CELTMode *opus_custom_mode_create(opus_int32 Fs, int frame_size, int *error);
  69. /** Destroys a mode struct. Only call this after all encoders and
  70. decoders using this mode are destroyed as well.
  71. @param mode Mode to be destroyed
  72. */
  73. OPUS_CUSTOM_EXPORT void opus_custom_mode_destroy(CELTMode *mode);
  74. /* Encoder */
  75. OPUS_CUSTOM_EXPORT int opus_custom_encoder_get_size(const CELTMode *mode, int channels);
  76. /** Creates a new encoder state. Each stream needs its own encoder
  77. state (can't be shared across simultaneous streams).
  78. @param mode Contains all the information about the characteristics of
  79. * the stream (must be the same characteristics as used for the
  80. * decoder)
  81. @param channels Number of channels
  82. @param error Returns an error code
  83. @return Newly created encoder state.
  84. */
  85. OPUS_CUSTOM_EXPORT CELTEncoder *opus_custom_encoder_create(const CELTMode *mode, int channels, int *error);
  86. OPUS_CUSTOM_EXPORT int opus_custom_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels);
  87. /** Destroys a an encoder state.
  88. @param st Encoder state to be destroyed
  89. */
  90. OPUS_CUSTOM_EXPORT void opus_custom_encoder_destroy(CELTEncoder *st);
  91. /** Encodes a frame of audio.
  92. @param st Encoder state
  93. @param pcm PCM audio in float format, with a normal range of +/-1.0.
  94. * Samples with a range beyond +/-1.0 are supported but will
  95. * be clipped by decoders using the integer API and should
  96. * only be used if it is known that the far end supports
  97. * extended dynmaic range. There must be exactly
  98. * frame_size samples per channel.
  99. @param compressed The compressed data is written here. This may not alias pcm or
  100. * optional_synthesis.
  101. @param nbCompressedBytes Maximum number of bytes to use for compressing the frame
  102. * (can change from one frame to another)
  103. @return Number of bytes written to "compressed". Will be the same as
  104. * "nbCompressedBytes" unless the stream is VBR and will never be larger.
  105. * If negative, an error has occurred (see error codes). It is IMPORTANT that
  106. * the length returned be somehow transmitted to the decoder. Otherwise, no
  107. * decoding is possible.
  108. */
  109. OPUS_CUSTOM_EXPORT int opus_custom_encode_float(CELTEncoder *st, const float *pcm, int frame_size, unsigned char *compressed, int maxCompressedBytes);
  110. /** Encodes a frame of audio.
  111. @param st Encoder state
  112. @param pcm PCM audio in signed 16-bit format (native endian). There must be
  113. * exactly frame_size samples per channel.
  114. @param compressed The compressed data is written here. This may not alias pcm or
  115. * optional_synthesis.
  116. @param nbCompressedBytes Maximum number of bytes to use for compressing the frame
  117. * (can change from one frame to another)
  118. @return Number of bytes written to "compressed". Will be the same as
  119. * "nbCompressedBytes" unless the stream is VBR and will never be larger.
  120. * If negative, an error has occurred (see error codes). It is IMPORTANT that
  121. * the length returned be somehow transmitted to the decoder. Otherwise, no
  122. * decoding is possible.
  123. */
  124. OPUS_CUSTOM_EXPORT int opus_custom_encode(CELTEncoder *st, const opus_int16 *pcm, int frame_size, unsigned char *compressed, int maxCompressedBytes);
  125. /** Query and set encoder parameters
  126. @param st Encoder state
  127. @param request Parameter to change or query
  128. @param value Pointer to a 32-bit int value
  129. @return Error code
  130. */
  131. OPUS_CUSTOM_EXPORT int opus_custom_encoder_ctl(CELTEncoder * restrict st, int request, ...);
  132. /* Decoder */
  133. OPUS_CUSTOM_EXPORT int opus_custom_decoder_get_size(const CELTMode *mode, int channels);
  134. /** Creates a new decoder state. Each stream needs its own decoder state (can't
  135. be shared across simultaneous streams).
  136. @param mode Contains all the information about the characteristics of the
  137. stream (must be the same characteristics as used for the encoder)
  138. @param channels Number of channels
  139. @param error Returns an error code
  140. @return Newly created decoder state.
  141. */
  142. OPUS_CUSTOM_EXPORT CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error);
  143. OPUS_CUSTOM_EXPORT int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels);
  144. /** Destroys a a decoder state.
  145. @param st Decoder state to be destroyed
  146. */
  147. OPUS_CUSTOM_EXPORT void opus_custom_decoder_destroy(CELTDecoder *st);
  148. /** Decodes a frame of audio.
  149. @param st Decoder state
  150. @param data Compressed data produced by an encoder
  151. @param len Number of bytes to read from "data". This MUST be exactly the number
  152. of bytes returned by the encoder. Using a larger value WILL NOT WORK.
  153. @param pcm One frame (frame_size samples per channel) of decoded PCM will be
  154. returned here in float format.
  155. @return Error code.
  156. */
  157. OPUS_CUSTOM_EXPORT int opus_custom_decode_float(CELTDecoder *st, const unsigned char *data, int len, float *pcm, int frame_size);
  158. /** Decodes a frame of audio.
  159. @param st Decoder state
  160. @param data Compressed data produced by an encoder
  161. @param len Number of bytes to read from "data". This MUST be exactly the number
  162. of bytes returned by the encoder. Using a larger value WILL NOT WORK.
  163. @param pcm One frame (frame_size samples per channel) of decoded PCM will be
  164. returned here in 16-bit PCM format (native endian).
  165. @return Error code.
  166. */
  167. OPUS_CUSTOM_EXPORT int opus_custom_decode(CELTDecoder *st, const unsigned char *data, int len, opus_int16 *pcm, int frame_size);
  168. /** Query and set decoder parameters
  169. @param st Decoder state
  170. @param request Parameter to change or query
  171. @param value Pointer to a 32-bit int value
  172. @return Error code
  173. */
  174. OPUS_CUSTOM_EXPORT int opus_custom_decoder_ctl(CELTDecoder * restrict st, int request, ...);
  175. #ifdef __cplusplus
  176. }
  177. #endif
  178. #endif /* OPUS_CUSTOM_H */