2
0

opus.h 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited
  2. Written by Jean-Marc Valin and Koen Vos */
  3. /*
  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. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  13. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  16. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  17. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. /**
  25. * @file opus.h
  26. * @brief Opus reference implementation API
  27. */
  28. #ifndef OPUS_H
  29. #define OPUS_H
  30. #include "opus_types.h"
  31. #include "opus_defines.h"
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /**
  36. * @mainpage Opus
  37. *
  38. * The Opus codec is designed for interactive speech and audio transmission over the Internet.
  39. * It is designed by the IETF Codec Working Group and incorporates technology from
  40. * Skype's SILK codec and Xiph.Org's CELT codec.
  41. *
  42. * The Opus codec is designed to handle a wide range of interactive audio applications,
  43. * including Voice over IP, videoconferencing, in-game chat, and even remote live music
  44. * performances. It can scale from low bit-rate narrowband speech to very high quality
  45. * stereo music. Its main features are:
  46. * @li Sampling rates from 8 to 48 kHz
  47. * @li Bit-rates from 6 kb/s to 510 kb/s
  48. * @li Support for both constant bit-rate (CBR) and variable bit-rate (VBR)
  49. * @li Audio bandwidth from narrowband to full-band
  50. * @li Support for speech and music
  51. * @li Support for mono and stereo
  52. * @li Support for multichannel (up to 255 channels)
  53. * @li Frame sizes from 2.5 ms to 60 ms
  54. * @li Good loss robustness and packet loss concealment (PLC)
  55. * @li Floating point and fixed-point implementation
  56. *
  57. * Documentation sections:
  58. * @li @ref opus_encoder
  59. * @li @ref opus_decoder
  60. * @li @ref opus_repacketizer
  61. * @li @ref opus_multistream
  62. * @li @ref opus_libinfo
  63. * @li @ref opus_custom
  64. */
  65. /** @defgroup opus_encoder Opus Encoder
  66. * @{
  67. *
  68. * @brief This page describes the process and functions used to encode Opus.
  69. *
  70. * Since Opus is a stateful codec, the encoding process starts with creating an encoder
  71. * state. This can be done with:
  72. *
  73. * @code
  74. * int error;
  75. * OpusEncoder *enc;
  76. * enc = opus_encoder_create(Fs, channels, application, &error);
  77. * @endcode
  78. *
  79. * From this point, @c enc can be used for encoding an audio stream. An encoder state
  80. * @b must @b not be used for more than one stream at the same time. Similarly, the encoder
  81. * state @b must @b not be re-initialized for each frame.
  82. *
  83. * While opus_encoder_create() allocates memory for the state, it's also possible
  84. * to initialize pre-allocated memory:
  85. *
  86. * @code
  87. * int size;
  88. * int error;
  89. * OpusEncoder *enc;
  90. * size = opus_encoder_get_size(channels);
  91. * enc = malloc(size);
  92. * error = opus_encoder_init(enc, Fs, channels, application);
  93. * @endcode
  94. *
  95. * where opus_encoder_get_size() returns the required size for the encoder state. Note that
  96. * future versions of this code may change the size, so no assuptions should be made about it.
  97. *
  98. * The encoder state is always continuous in memory and only a shallow copy is sufficient
  99. * to copy it (e.g. memcpy())
  100. *
  101. * It is possible to change some of the encoder's settings using the opus_encoder_ctl()
  102. * interface. All these settings already default to the recommended value, so they should
  103. * only be changed when necessary. The most common settings one may want to change are:
  104. *
  105. * @code
  106. * opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrate));
  107. * opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(complexity));
  108. * opus_encoder_ctl(enc, OPUS_SET_SIGNAL(signal_type));
  109. * @endcode
  110. *
  111. * where
  112. *
  113. * @arg bitrate is in bits per second (b/s)
  114. * @arg complexity is a value from 1 to 10, where 1 is the lowest complexity and 10 is the highest
  115. * @arg signal_type is either OPUS_AUTO (default), OPUS_SIGNAL_VOICE, or OPUS_SIGNAL_MUSIC
  116. *
  117. * See @ref opus_encoderctls and @ref opus_genericctls for a complete list of parameters that can be set or queried. Most parameters can be set or changed at any time during a stream.
  118. *
  119. * To encode a frame, opus_encode() or opus_encode_float() must be called with exactly one frame (2.5, 5, 10, 20, 40 or 60 ms) of audio data:
  120. * @code
  121. * len = opus_encode(enc, audio_frame, frame_size, packet, max_packet);
  122. * @endcode
  123. *
  124. * where
  125. * <ul>
  126. * <li>audio_frame is the audio data in opus_int16 (or float for opus_encode_float())</li>
  127. * <li>frame_size is the duration of the frame in samples (per channel)</li>
  128. * <li>packet is the byte array to which the compressed data is written</li>
  129. * <li>max_packet is the maximum number of bytes that can be written in the packet (4000 bytes is recommended).
  130. * Do not use max_packet to control VBR target bitrate, instead use the #OPUS_SET_BITRATE CTL.</li>
  131. * </ul>
  132. *
  133. * opus_encode() and opus_encode_float() return the number of bytes actually written to the packet.
  134. * The return value <b>can be negative</b>, which indicates that an error has occurred. If the return value
  135. * is 2 bytes or less, then the packet does not need to be transmitted (DTX).
  136. *
  137. * Once the encoder state if no longer needed, it can be destroyed with
  138. *
  139. * @code
  140. * opus_encoder_destroy(enc);
  141. * @endcode
  142. *
  143. * If the encoder was created with opus_encoder_init() rather than opus_encoder_create(),
  144. * then no action is required aside from potentially freeing the memory that was manually
  145. * allocated for it (calling free(enc) for the example above)
  146. *
  147. */
  148. /** Opus encoder state.
  149. * This contains the complete state of an Opus encoder.
  150. * It is position independent and can be freely copied.
  151. * @see opus_encoder_create,opus_encoder_init
  152. */
  153. typedef struct OpusEncoder OpusEncoder;
  154. /** Gets the size of an <code>OpusEncoder</code> structure.
  155. * @param[in] channels <tt>int</tt>: Number of channels.
  156. * This must be 1 or 2.
  157. * @returns The size in bytes.
  158. */
  159. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_encoder_get_size(int channels);
  160. /**
  161. */
  162. /** Allocates and initializes an encoder state.
  163. * There are three coding modes:
  164. *
  165. * @ref OPUS_APPLICATION_VOIP gives best quality at a given bitrate for voice
  166. * signals. It enhances the input signal by high-pass filtering and
  167. * emphasizing formants and harmonics. Optionally it includes in-band
  168. * forward error correction to protect against packet loss. Use this
  169. * mode for typical VoIP applications. Because of the enhancement,
  170. * even at high bitrates the output may sound different from the input.
  171. *
  172. * @ref OPUS_APPLICATION_AUDIO gives best quality at a given bitrate for most
  173. * non-voice signals like music. Use this mode for music and mixed
  174. * (music/voice) content, broadcast, and applications requiring less
  175. * than 15 ms of coding delay.
  176. *
  177. * @ref OPUS_APPLICATION_RESTRICTED_LOWDELAY configures low-delay mode that
  178. * disables the speech-optimized mode in exchange for slightly reduced delay.
  179. * This mode can only be set on an newly initialized or freshly reset encoder
  180. * because it changes the codec delay.
  181. *
  182. * This is useful when the caller knows that the speech-optimized modes will not be needed (use with caution).
  183. * @param [in] Fs <tt>opus_int32</tt>: Sampling rate of input signal (Hz)
  184. * This must be one of 8000, 12000, 16000,
  185. * 24000, or 48000.
  186. * @param [in] channels <tt>int</tt>: Number of channels (1 or 2) in input signal
  187. * @param [in] application <tt>int</tt>: Coding mode (one of @ref OPUS_APPLICATION_VOIP, @ref OPUS_APPLICATION_AUDIO, or @ref OPUS_APPLICATION_RESTRICTED_LOWDELAY)
  188. * @param [out] error <tt>int*</tt>: @ref opus_errorcodes
  189. * @note Regardless of the sampling rate and number channels selected, the Opus encoder
  190. * can switch to a lower audio bandwidth or number of channels if the bitrate
  191. * selected is too low. This also means that it is safe to always use 48 kHz stereo input
  192. * and let the encoder optimize the encoding.
  193. */
  194. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusEncoder *opus_encoder_create(
  195. opus_int32 Fs,
  196. int channels,
  197. int application,
  198. int *error
  199. );
  200. /** Initializes a previously allocated encoder state
  201. * The memory pointed to by st must be at least the size returned by opus_encoder_get_size().
  202. * This is intended for applications which use their own allocator instead of malloc.
  203. * @see opus_encoder_create(),opus_encoder_get_size()
  204. * To reset a previously initialized state, use the #OPUS_RESET_STATE CTL.
  205. * @param [in] st <tt>OpusEncoder*</tt>: Encoder state
  206. * @param [in] Fs <tt>opus_int32</tt>: Sampling rate of input signal (Hz)
  207. * This must be one of 8000, 12000, 16000,
  208. * 24000, or 48000.
  209. * @param [in] channels <tt>int</tt>: Number of channels (1 or 2) in input signal
  210. * @param [in] application <tt>int</tt>: Coding mode (one of OPUS_APPLICATION_VOIP, OPUS_APPLICATION_AUDIO, or OPUS_APPLICATION_RESTRICTED_LOWDELAY)
  211. * @retval #OPUS_OK Success or @ref opus_errorcodes
  212. */
  213. OPUS_EXPORT int opus_encoder_init(
  214. OpusEncoder *st,
  215. opus_int32 Fs,
  216. int channels,
  217. int application
  218. ) OPUS_ARG_NONNULL(1);
  219. /** Encodes an Opus frame.
  220. * @param [in] st <tt>OpusEncoder*</tt>: Encoder state
  221. * @param [in] pcm <tt>opus_int16*</tt>: Input signal (interleaved if 2 channels). length is frame_size*channels*sizeof(opus_int16)
  222. * @param [in] frame_size <tt>int</tt>: Number of samples per channel in the
  223. * input signal.
  224. * This must be an Opus frame size for
  225. * the encoder's sampling rate.
  226. * For example, at 48 kHz the permitted
  227. * values are 120, 240, 480, 960, 1920,
  228. * and 2880.
  229. * Passing in a duration of less than
  230. * 10 ms (480 samples at 48 kHz) will
  231. * prevent the encoder from using the LPC
  232. * or hybrid modes.
  233. * @param [out] data <tt>unsigned char*</tt>: Output payload.
  234. * This must contain storage for at
  235. * least \a max_data_bytes.
  236. * @param [in] max_data_bytes <tt>opus_int32</tt>: Size of the allocated
  237. * memory for the output
  238. * payload. This may be
  239. * used to impose an upper limit on
  240. * the instant bitrate, but should
  241. * not be used as the only bitrate
  242. * control. Use #OPUS_SET_BITRATE to
  243. * control the bitrate.
  244. * @returns The length of the encoded packet (in bytes) on success or a
  245. * negative error code (see @ref opus_errorcodes) on failure.
  246. */
  247. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_encode(
  248. OpusEncoder *st,
  249. const opus_int16 *pcm,
  250. int frame_size,
  251. unsigned char *data,
  252. opus_int32 max_data_bytes
  253. ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4);
  254. /** Encodes an Opus frame from floating point input.
  255. * @param [in] st <tt>OpusEncoder*</tt>: Encoder state
  256. * @param [in] pcm <tt>float*</tt>: Input in float format (interleaved if 2 channels), with a normal range of +/-1.0.
  257. * Samples with a range beyond +/-1.0 are supported but will
  258. * be clipped by decoders using the integer API and should
  259. * only be used if it is known that the far end supports
  260. * extended dynamic range.
  261. * length is frame_size*channels*sizeof(float)
  262. * @param [in] frame_size <tt>int</tt>: Number of samples per channel in the
  263. * input signal.
  264. * This must be an Opus frame size for
  265. * the encoder's sampling rate.
  266. * For example, at 48 kHz the permitted
  267. * values are 120, 240, 480, 960, 1920,
  268. * and 2880.
  269. * Passing in a duration of less than
  270. * 10 ms (480 samples at 48 kHz) will
  271. * prevent the encoder from using the LPC
  272. * or hybrid modes.
  273. * @param [out] data <tt>unsigned char*</tt>: Output payload.
  274. * This must contain storage for at
  275. * least \a max_data_bytes.
  276. * @param [in] max_data_bytes <tt>opus_int32</tt>: Size of the allocated
  277. * memory for the output
  278. * payload. This may be
  279. * used to impose an upper limit on
  280. * the instant bitrate, but should
  281. * not be used as the only bitrate
  282. * control. Use #OPUS_SET_BITRATE to
  283. * control the bitrate.
  284. * @returns The length of the encoded packet (in bytes) on success or a
  285. * negative error code (see @ref opus_errorcodes) on failure.
  286. */
  287. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_encode_float(
  288. OpusEncoder *st,
  289. const float *pcm,
  290. int frame_size,
  291. unsigned char *data,
  292. opus_int32 max_data_bytes
  293. ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4);
  294. /** Frees an <code>OpusEncoder</code> allocated by opus_encoder_create().
  295. * @param[in] st <tt>OpusEncoder*</tt>: State to be freed.
  296. */
  297. OPUS_EXPORT void opus_encoder_destroy(OpusEncoder *st);
  298. /** Perform a CTL function on an Opus encoder.
  299. *
  300. * Generally the request and subsequent arguments are generated
  301. * by a convenience macro.
  302. * @param st <tt>OpusEncoder*</tt>: Encoder state.
  303. * @param request This and all remaining parameters should be replaced by one
  304. * of the convenience macros in @ref opus_genericctls or
  305. * @ref opus_encoderctls.
  306. * @see opus_genericctls
  307. * @see opus_encoderctls
  308. */
  309. OPUS_EXPORT int opus_encoder_ctl(OpusEncoder *st, int request, ...) OPUS_ARG_NONNULL(1);
  310. /**@}*/
  311. /** @defgroup opus_decoder Opus Decoder
  312. * @{
  313. *
  314. * @brief This page describes the process and functions used to decode Opus.
  315. *
  316. * The decoding process also starts with creating a decoder
  317. * state. This can be done with:
  318. * @code
  319. * int error;
  320. * OpusDecoder *dec;
  321. * dec = opus_decoder_create(Fs, channels, &error);
  322. * @endcode
  323. * where
  324. * @li Fs is the sampling rate and must be 8000, 12000, 16000, 24000, or 48000
  325. * @li channels is the number of channels (1 or 2)
  326. * @li error will hold the error code in case of failure (or #OPUS_OK on success)
  327. * @li the return value is a newly created decoder state to be used for decoding
  328. *
  329. * While opus_decoder_create() allocates memory for the state, it's also possible
  330. * to initialize pre-allocated memory:
  331. * @code
  332. * int size;
  333. * int error;
  334. * OpusDecoder *dec;
  335. * size = opus_decoder_get_size(channels);
  336. * dec = malloc(size);
  337. * error = opus_decoder_init(dec, Fs, channels);
  338. * @endcode
  339. * where opus_decoder_get_size() returns the required size for the decoder state. Note that
  340. * future versions of this code may change the size, so no assuptions should be made about it.
  341. *
  342. * The decoder state is always continuous in memory and only a shallow copy is sufficient
  343. * to copy it (e.g. memcpy())
  344. *
  345. * To decode a frame, opus_decode() or opus_decode_float() must be called with a packet of compressed audio data:
  346. * @code
  347. * frame_size = opus_decode(dec, packet, len, decoded, max_size, 0);
  348. * @endcode
  349. * where
  350. *
  351. * @li packet is the byte array containing the compressed data
  352. * @li len is the exact number of bytes contained in the packet
  353. * @li decoded is the decoded audio data in opus_int16 (or float for opus_decode_float())
  354. * @li max_size is the max duration of the frame in samples (per channel) that can fit into the decoded_frame array
  355. *
  356. * opus_decode() and opus_decode_float() return the number of samples (per channel) decoded from the packet.
  357. * If that value is negative, then an error has occurred. This can occur if the packet is corrupted or if the audio
  358. * buffer is too small to hold the decoded audio.
  359. *
  360. * Opus is a stateful codec with overlapping blocks and as a result Opus
  361. * packets are not coded independently of each other. Packets must be
  362. * passed into the decoder serially and in the correct order for a correct
  363. * decode. Lost packets can be replaced with loss concealment by calling
  364. * the decoder with a null pointer and zero length for the missing packet.
  365. *
  366. * A single codec state may only be accessed from a single thread at
  367. * a time and any required locking must be performed by the caller. Separate
  368. * streams must be decoded with separate decoder states and can be decoded
  369. * in parallel unless the library was compiled with NONTHREADSAFE_PSEUDOSTACK
  370. * defined.
  371. *
  372. */
  373. /** Opus decoder state.
  374. * This contains the complete state of an Opus decoder.
  375. * It is position independent and can be freely copied.
  376. * @see opus_decoder_create,opus_decoder_init
  377. */
  378. typedef struct OpusDecoder OpusDecoder;
  379. /** Gets the size of an <code>OpusDecoder</code> structure.
  380. * @param [in] channels <tt>int</tt>: Number of channels.
  381. * This must be 1 or 2.
  382. * @returns The size in bytes.
  383. */
  384. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decoder_get_size(int channels);
  385. /** Allocates and initializes a decoder state.
  386. * @param [in] Fs <tt>opus_int32</tt>: Sample rate to decode at (Hz).
  387. * This must be one of 8000, 12000, 16000,
  388. * 24000, or 48000.
  389. * @param [in] channels <tt>int</tt>: Number of channels (1 or 2) to decode
  390. * @param [out] error <tt>int*</tt>: #OPUS_OK Success or @ref opus_errorcodes
  391. *
  392. * Internally Opus stores data at 48000 Hz, so that should be the default
  393. * value for Fs. However, the decoder can efficiently decode to buffers
  394. * at 8, 12, 16, and 24 kHz so if for some reason the caller cannot use
  395. * data at the full sample rate, or knows the compressed data doesn't
  396. * use the full frequency range, it can request decoding at a reduced
  397. * rate. Likewise, the decoder is capable of filling in either mono or
  398. * interleaved stereo pcm buffers, at the caller's request.
  399. */
  400. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusDecoder *opus_decoder_create(
  401. opus_int32 Fs,
  402. int channels,
  403. int *error
  404. );
  405. /** Initializes a previously allocated decoder state.
  406. * The state must be at least the size returned by opus_decoder_get_size().
  407. * This is intended for applications which use their own allocator instead of malloc. @see opus_decoder_create,opus_decoder_get_size
  408. * To reset a previously initialized state, use the #OPUS_RESET_STATE CTL.
  409. * @param [in] st <tt>OpusDecoder*</tt>: Decoder state.
  410. * @param [in] Fs <tt>opus_int32</tt>: Sampling rate to decode to (Hz).
  411. * This must be one of 8000, 12000, 16000,
  412. * 24000, or 48000.
  413. * @param [in] channels <tt>int</tt>: Number of channels (1 or 2) to decode
  414. * @retval #OPUS_OK Success or @ref opus_errorcodes
  415. */
  416. OPUS_EXPORT int opus_decoder_init(
  417. OpusDecoder *st,
  418. opus_int32 Fs,
  419. int channels
  420. ) OPUS_ARG_NONNULL(1);
  421. /** Decode an Opus packet.
  422. * @param [in] st <tt>OpusDecoder*</tt>: Decoder state
  423. * @param [in] data <tt>char*</tt>: Input payload. Use a NULL pointer to indicate packet loss
  424. * @param [in] len <tt>opus_int32</tt>: Number of bytes in payload*
  425. * @param [out] pcm <tt>opus_int16*</tt>: Output signal (interleaved if 2 channels). length
  426. * is frame_size*channels*sizeof(opus_int16)
  427. * @param [in] frame_size Number of samples per channel of available space in \a pcm.
  428. * If this is less than the maximum packet duration (120ms; 5760 for 48kHz), this function will
  429. * not be capable of decoding some packets. In the case of PLC (data==NULL) or FEC (decode_fec=1),
  430. * then frame_size needs to be exactly the duration of audio that is missing, otherwise the
  431. * decoder will not be in the optimal state to decode the next incoming packet. For the PLC and
  432. * FEC cases, frame_size <b>must</b> be a multiple of 2.5 ms.
  433. * @param [in] decode_fec <tt>int</tt>: Flag (0 or 1) to request that any in-band forward error correction data be
  434. * decoded. If no such data is available, the frame is decoded as if it were lost.
  435. * @returns Number of decoded samples or @ref opus_errorcodes
  436. */
  437. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decode(
  438. OpusDecoder *st,
  439. const unsigned char *data,
  440. opus_int32 len,
  441. opus_int16 *pcm,
  442. int frame_size,
  443. int decode_fec
  444. ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);
  445. /** Decode an Opus packet with floating point output.
  446. * @param [in] st <tt>OpusDecoder*</tt>: Decoder state
  447. * @param [in] data <tt>char*</tt>: Input payload. Use a NULL pointer to indicate packet loss
  448. * @param [in] len <tt>opus_int32</tt>: Number of bytes in payload
  449. * @param [out] pcm <tt>float*</tt>: Output signal (interleaved if 2 channels). length
  450. * is frame_size*channels*sizeof(float)
  451. * @param [in] frame_size Number of samples per channel of available space in \a pcm.
  452. * If this is less than the maximum packet duration (120ms; 5760 for 48kHz), this function will
  453. * not be capable of decoding some packets. In the case of PLC (data==NULL) or FEC (decode_fec=1),
  454. * then frame_size needs to be exactly the duration of audio that is missing, otherwise the
  455. * decoder will not be in the optimal state to decode the next incoming packet. For the PLC and
  456. * FEC cases, frame_size <b>must</b> be a multiple of 2.5 ms.
  457. * @param [in] decode_fec <tt>int</tt>: Flag (0 or 1) to request that any in-band forward error correction data be
  458. * decoded. If no such data is available the frame is decoded as if it were lost.
  459. * @returns Number of decoded samples or @ref opus_errorcodes
  460. */
  461. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decode_float(
  462. OpusDecoder *st,
  463. const unsigned char *data,
  464. opus_int32 len,
  465. float *pcm,
  466. int frame_size,
  467. int decode_fec
  468. ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);
  469. /** Perform a CTL function on an Opus decoder.
  470. *
  471. * Generally the request and subsequent arguments are generated
  472. * by a convenience macro.
  473. * @param st <tt>OpusDecoder*</tt>: Decoder state.
  474. * @param request This and all remaining parameters should be replaced by one
  475. * of the convenience macros in @ref opus_genericctls or
  476. * @ref opus_decoderctls.
  477. * @see opus_genericctls
  478. * @see opus_decoderctls
  479. */
  480. OPUS_EXPORT int opus_decoder_ctl(OpusDecoder *st, int request, ...) OPUS_ARG_NONNULL(1);
  481. /** Frees an <code>OpusDecoder</code> allocated by opus_decoder_create().
  482. * @param[in] st <tt>OpusDecoder*</tt>: State to be freed.
  483. */
  484. OPUS_EXPORT void opus_decoder_destroy(OpusDecoder *st);
  485. /** Parse an opus packet into one or more frames.
  486. * Opus_decode will perform this operation internally so most applications do
  487. * not need to use this function.
  488. * This function does not copy the frames, the returned pointers are pointers into
  489. * the input packet.
  490. * @param [in] data <tt>char*</tt>: Opus packet to be parsed
  491. * @param [in] len <tt>opus_int32</tt>: size of data
  492. * @param [out] out_toc <tt>char*</tt>: TOC pointer
  493. * @param [out] frames <tt>char*[48]</tt> encapsulated frames
  494. * @param [out] size <tt>opus_int16[48]</tt> sizes of the encapsulated frames
  495. * @param [out] payload_offset <tt>int*</tt>: returns the position of the payload within the packet (in bytes)
  496. * @returns number of frames
  497. */
  498. OPUS_EXPORT int opus_packet_parse(
  499. const unsigned char *data,
  500. opus_int32 len,
  501. unsigned char *out_toc,
  502. const unsigned char *frames[48],
  503. opus_int16 size[48],
  504. int *payload_offset
  505. ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(5);
  506. /** Gets the bandwidth of an Opus packet.
  507. * @param [in] data <tt>char*</tt>: Opus packet
  508. * @retval OPUS_BANDWIDTH_NARROWBAND Narrowband (4kHz bandpass)
  509. * @retval OPUS_BANDWIDTH_MEDIUMBAND Mediumband (6kHz bandpass)
  510. * @retval OPUS_BANDWIDTH_WIDEBAND Wideband (8kHz bandpass)
  511. * @retval OPUS_BANDWIDTH_SUPERWIDEBAND Superwideband (12kHz bandpass)
  512. * @retval OPUS_BANDWIDTH_FULLBAND Fullband (20kHz bandpass)
  513. * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type
  514. */
  515. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_bandwidth(const unsigned char *data) OPUS_ARG_NONNULL(1);
  516. /** Gets the number of samples per frame from an Opus packet.
  517. * @param [in] data <tt>char*</tt>: Opus packet.
  518. * This must contain at least one byte of
  519. * data.
  520. * @param [in] Fs <tt>opus_int32</tt>: Sampling rate in Hz.
  521. * This must be a multiple of 400, or
  522. * inaccurate results will be returned.
  523. * @returns Number of samples per frame.
  524. */
  525. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_samples_per_frame(const unsigned char *data, opus_int32 Fs) OPUS_ARG_NONNULL(1);
  526. /** Gets the number of channels from an Opus packet.
  527. * @param [in] data <tt>char*</tt>: Opus packet
  528. * @returns Number of channels
  529. * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type
  530. */
  531. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_channels(const unsigned char *data) OPUS_ARG_NONNULL(1);
  532. /** Gets the number of frames in an Opus packet.
  533. * @param [in] packet <tt>char*</tt>: Opus packet
  534. * @param [in] len <tt>opus_int32</tt>: Length of packet
  535. * @returns Number of frames
  536. * @retval OPUS_BAD_ARG Insufficient data was passed to the function
  537. * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type
  538. */
  539. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_frames(const unsigned char packet[], opus_int32 len) OPUS_ARG_NONNULL(1);
  540. /** Gets the number of samples of an Opus packet.
  541. * @param [in] packet <tt>char*</tt>: Opus packet
  542. * @param [in] len <tt>opus_int32</tt>: Length of packet
  543. * @param [in] Fs <tt>opus_int32</tt>: Sampling rate in Hz.
  544. * This must be a multiple of 400, or
  545. * inaccurate results will be returned.
  546. * @returns Number of samples
  547. * @retval OPUS_BAD_ARG Insufficient data was passed to the function
  548. * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type
  549. */
  550. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_samples(const unsigned char packet[], opus_int32 len, opus_int32 Fs) OPUS_ARG_NONNULL(1);
  551. /** Gets the number of samples of an Opus packet.
  552. * @param [in] dec <tt>OpusDecoder*</tt>: Decoder state
  553. * @param [in] packet <tt>char*</tt>: Opus packet
  554. * @param [in] len <tt>opus_int32</tt>: Length of packet
  555. * @returns Number of samples
  556. * @retval OPUS_BAD_ARG Insufficient data was passed to the function
  557. * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type
  558. */
  559. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decoder_get_nb_samples(const OpusDecoder *dec, const unsigned char packet[], opus_int32 len) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2);
  560. /** Applies soft-clipping to bring a float signal within the [-1,1] range. If
  561. * the signal is already in that range, nothing is done. If there are values
  562. * outside of [-1,1], then the signal is clipped as smoothly as possible to
  563. * both fit in the range and avoid creating excessive distortion in the
  564. * process.
  565. * @param [in,out] pcm <tt>float*</tt>: Input PCM and modified PCM
  566. * @param [in] frame_size <tt>int</tt> Number of samples per channel to process
  567. * @param [in] channels <tt>int</tt>: Number of channels
  568. * @param [in,out] softclip_mem <tt>float*</tt>: State memory for the soft clipping process (one float per channel, initialized to zero)
  569. */
  570. OPUS_EXPORT void opus_pcm_soft_clip(float *pcm, int frame_size, int channels, float *softclip_mem);
  571. /**@}*/
  572. /** @defgroup opus_repacketizer Repacketizer
  573. * @{
  574. *
  575. * The repacketizer can be used to merge multiple Opus packets into a single
  576. * packet or alternatively to split Opus packets that have previously been
  577. * merged. Splitting valid Opus packets is always guaranteed to succeed,
  578. * whereas merging valid packets only succeeds if all frames have the same
  579. * mode, bandwidth, and frame size, and when the total duration of the merged
  580. * packet is no more than 120 ms. The 120 ms limit comes from the
  581. * specification and limits decoder memory requirements at a point where
  582. * framing overhead becomes negligible.
  583. *
  584. * The repacketizer currently only operates on elementary Opus
  585. * streams. It will not manipualte multistream packets successfully, except in
  586. * the degenerate case where they consist of data from a single stream.
  587. *
  588. * The repacketizing process starts with creating a repacketizer state, either
  589. * by calling opus_repacketizer_create() or by allocating the memory yourself,
  590. * e.g.,
  591. * @code
  592. * OpusRepacketizer *rp;
  593. * rp = (OpusRepacketizer*)malloc(opus_repacketizer_get_size());
  594. * if (rp != NULL)
  595. * opus_repacketizer_init(rp);
  596. * @endcode
  597. *
  598. * Then the application should submit packets with opus_repacketizer_cat(),
  599. * extract new packets with opus_repacketizer_out() or
  600. * opus_repacketizer_out_range(), and then reset the state for the next set of
  601. * input packets via opus_repacketizer_init().
  602. *
  603. * For example, to split a sequence of packets into individual frames:
  604. * @code
  605. * unsigned char *data;
  606. * int len;
  607. * while (get_next_packet(&data, &len))
  608. * {
  609. * unsigned char out[1276];
  610. * opus_int32 out_len;
  611. * int nb_frames;
  612. * int err;
  613. * int i;
  614. * err = opus_repacketizer_cat(rp, data, len);
  615. * if (err != OPUS_OK)
  616. * {
  617. * release_packet(data);
  618. * return err;
  619. * }
  620. * nb_frames = opus_repacketizer_get_nb_frames(rp);
  621. * for (i = 0; i < nb_frames; i++)
  622. * {
  623. * out_len = opus_repacketizer_out_range(rp, i, i+1, out, sizeof(out));
  624. * if (out_len < 0)
  625. * {
  626. * release_packet(data);
  627. * return (int)out_len;
  628. * }
  629. * output_next_packet(out, out_len);
  630. * }
  631. * opus_repacketizer_init(rp);
  632. * release_packet(data);
  633. * }
  634. * @endcode
  635. *
  636. * Alternatively, to combine a sequence of frames into packets that each
  637. * contain up to <code>TARGET_DURATION_MS</code> milliseconds of data:
  638. * @code
  639. * // The maximum number of packets with duration TARGET_DURATION_MS occurs
  640. * // when the frame size is 2.5 ms, for a total of (TARGET_DURATION_MS*2/5)
  641. * // packets.
  642. * unsigned char *data[(TARGET_DURATION_MS*2/5)+1];
  643. * opus_int32 len[(TARGET_DURATION_MS*2/5)+1];
  644. * int nb_packets;
  645. * unsigned char out[1277*(TARGET_DURATION_MS*2/2)];
  646. * opus_int32 out_len;
  647. * int prev_toc;
  648. * nb_packets = 0;
  649. * while (get_next_packet(data+nb_packets, len+nb_packets))
  650. * {
  651. * int nb_frames;
  652. * int err;
  653. * nb_frames = opus_packet_get_nb_frames(data[nb_packets], len[nb_packets]);
  654. * if (nb_frames < 1)
  655. * {
  656. * release_packets(data, nb_packets+1);
  657. * return nb_frames;
  658. * }
  659. * nb_frames += opus_repacketizer_get_nb_frames(rp);
  660. * // If adding the next packet would exceed our target, or it has an
  661. * // incompatible TOC sequence, output the packets we already have before
  662. * // submitting it.
  663. * // N.B., The nb_packets > 0 check ensures we've submitted at least one
  664. * // packet since the last call to opus_repacketizer_init(). Otherwise a
  665. * // single packet longer than TARGET_DURATION_MS would cause us to try to
  666. * // output an (invalid) empty packet. It also ensures that prev_toc has
  667. * // been set to a valid value. Additionally, len[nb_packets] > 0 is
  668. * // guaranteed by the call to opus_packet_get_nb_frames() above, so the
  669. * // reference to data[nb_packets][0] should be valid.
  670. * if (nb_packets > 0 && (
  671. * ((prev_toc & 0xFC) != (data[nb_packets][0] & 0xFC)) ||
  672. * opus_packet_get_samples_per_frame(data[nb_packets], 48000)*nb_frames >
  673. * TARGET_DURATION_MS*48))
  674. * {
  675. * out_len = opus_repacketizer_out(rp, out, sizeof(out));
  676. * if (out_len < 0)
  677. * {
  678. * release_packets(data, nb_packets+1);
  679. * return (int)out_len;
  680. * }
  681. * output_next_packet(out, out_len);
  682. * opus_repacketizer_init(rp);
  683. * release_packets(data, nb_packets);
  684. * data[0] = data[nb_packets];
  685. * len[0] = len[nb_packets];
  686. * nb_packets = 0;
  687. * }
  688. * err = opus_repacketizer_cat(rp, data[nb_packets], len[nb_packets]);
  689. * if (err != OPUS_OK)
  690. * {
  691. * release_packets(data, nb_packets+1);
  692. * return err;
  693. * }
  694. * prev_toc = data[nb_packets][0];
  695. * nb_packets++;
  696. * }
  697. * // Output the final, partial packet.
  698. * if (nb_packets > 0)
  699. * {
  700. * out_len = opus_repacketizer_out(rp, out, sizeof(out));
  701. * release_packets(data, nb_packets);
  702. * if (out_len < 0)
  703. * return (int)out_len;
  704. * output_next_packet(out, out_len);
  705. * }
  706. * @endcode
  707. *
  708. * An alternate way of merging packets is to simply call opus_repacketizer_cat()
  709. * unconditionally until it fails. At that point, the merged packet can be
  710. * obtained with opus_repacketizer_out() and the input packet for which
  711. * opus_repacketizer_cat() needs to be re-added to a newly reinitialized
  712. * repacketizer state.
  713. */
  714. typedef struct OpusRepacketizer OpusRepacketizer;
  715. /** Gets the size of an <code>OpusRepacketizer</code> structure.
  716. * @returns The size in bytes.
  717. */
  718. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_repacketizer_get_size(void);
  719. /** (Re)initializes a previously allocated repacketizer state.
  720. * The state must be at least the size returned by opus_repacketizer_get_size().
  721. * This can be used for applications which use their own allocator instead of
  722. * malloc().
  723. * It must also be called to reset the queue of packets waiting to be
  724. * repacketized, which is necessary if the maximum packet duration of 120 ms
  725. * is reached or if you wish to submit packets with a different Opus
  726. * configuration (coding mode, audio bandwidth, frame size, or channel count).
  727. * Failure to do so will prevent a new packet from being added with
  728. * opus_repacketizer_cat().
  729. * @see opus_repacketizer_create
  730. * @see opus_repacketizer_get_size
  731. * @see opus_repacketizer_cat
  732. * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state to
  733. * (re)initialize.
  734. * @returns A pointer to the same repacketizer state that was passed in.
  735. */
  736. OPUS_EXPORT OpusRepacketizer *opus_repacketizer_init(OpusRepacketizer *rp) OPUS_ARG_NONNULL(1);
  737. /** Allocates memory and initializes the new repacketizer with
  738. * opus_repacketizer_init().
  739. */
  740. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusRepacketizer *opus_repacketizer_create(void);
  741. /** Frees an <code>OpusRepacketizer</code> allocated by
  742. * opus_repacketizer_create().
  743. * @param[in] rp <tt>OpusRepacketizer*</tt>: State to be freed.
  744. */
  745. OPUS_EXPORT void opus_repacketizer_destroy(OpusRepacketizer *rp);
  746. /** Add a packet to the current repacketizer state.
  747. * This packet must match the configuration of any packets already submitted
  748. * for repacketization since the last call to opus_repacketizer_init().
  749. * This means that it must have the same coding mode, audio bandwidth, frame
  750. * size, and channel count.
  751. * This can be checked in advance by examining the top 6 bits of the first
  752. * byte of the packet, and ensuring they match the top 6 bits of the first
  753. * byte of any previously submitted packet.
  754. * The total duration of audio in the repacketizer state also must not exceed
  755. * 120 ms, the maximum duration of a single packet, after adding this packet.
  756. *
  757. * The contents of the current repacketizer state can be extracted into new
  758. * packets using opus_repacketizer_out() or opus_repacketizer_out_range().
  759. *
  760. * In order to add a packet with a different configuration or to add more
  761. * audio beyond 120 ms, you must clear the repacketizer state by calling
  762. * opus_repacketizer_init().
  763. * If a packet is too large to add to the current repacketizer state, no part
  764. * of it is added, even if it contains multiple frames, some of which might
  765. * fit.
  766. * If you wish to be able to add parts of such packets, you should first use
  767. * another repacketizer to split the packet into pieces and add them
  768. * individually.
  769. * @see opus_repacketizer_out_range
  770. * @see opus_repacketizer_out
  771. * @see opus_repacketizer_init
  772. * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state to which to
  773. * add the packet.
  774. * @param[in] data <tt>const unsigned char*</tt>: The packet data.
  775. * The application must ensure
  776. * this pointer remains valid
  777. * until the next call to
  778. * opus_repacketizer_init() or
  779. * opus_repacketizer_destroy().
  780. * @param len <tt>opus_int32</tt>: The number of bytes in the packet data.
  781. * @returns An error code indicating whether or not the operation succeeded.
  782. * @retval #OPUS_OK The packet's contents have been added to the repacketizer
  783. * state.
  784. * @retval #OPUS_INVALID_PACKET The packet did not have a valid TOC sequence,
  785. * the packet's TOC sequence was not compatible
  786. * with previously submitted packets (because
  787. * the coding mode, audio bandwidth, frame size,
  788. * or channel count did not match), or adding
  789. * this packet would increase the total amount of
  790. * audio stored in the repacketizer state to more
  791. * than 120 ms.
  792. */
  793. OPUS_EXPORT int opus_repacketizer_cat(OpusRepacketizer *rp, const unsigned char *data, opus_int32 len) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2);
  794. /** Construct a new packet from data previously submitted to the repacketizer
  795. * state via opus_repacketizer_cat().
  796. * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state from which to
  797. * construct the new packet.
  798. * @param begin <tt>int</tt>: The index of the first frame in the current
  799. * repacketizer state to include in the output.
  800. * @param end <tt>int</tt>: One past the index of the last frame in the
  801. * current repacketizer state to include in the
  802. * output.
  803. * @param[out] data <tt>const unsigned char*</tt>: The buffer in which to
  804. * store the output packet.
  805. * @param maxlen <tt>opus_int32</tt>: The maximum number of bytes to store in
  806. * the output buffer. In order to guarantee
  807. * success, this should be at least
  808. * <code>1276</code> for a single frame,
  809. * or for multiple frames,
  810. * <code>1277*(end-begin)</code>.
  811. * However, <code>1*(end-begin)</code> plus
  812. * the size of all packet data submitted to
  813. * the repacketizer since the last call to
  814. * opus_repacketizer_init() or
  815. * opus_repacketizer_create() is also
  816. * sufficient, and possibly much smaller.
  817. * @returns The total size of the output packet on success, or an error code
  818. * on failure.
  819. * @retval #OPUS_BAD_ARG <code>[begin,end)</code> was an invalid range of
  820. * frames (begin < 0, begin >= end, or end >
  821. * opus_repacketizer_get_nb_frames()).
  822. * @retval #OPUS_BUFFER_TOO_SMALL \a maxlen was insufficient to contain the
  823. * complete output packet.
  824. */
  825. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_repacketizer_out_range(OpusRepacketizer *rp, int begin, int end, unsigned char *data, opus_int32 maxlen) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);
  826. /** Return the total number of frames contained in packet data submitted to
  827. * the repacketizer state so far via opus_repacketizer_cat() since the last
  828. * call to opus_repacketizer_init() or opus_repacketizer_create().
  829. * This defines the valid range of packets that can be extracted with
  830. * opus_repacketizer_out_range() or opus_repacketizer_out().
  831. * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state containing the
  832. * frames.
  833. * @returns The total number of frames contained in the packet data submitted
  834. * to the repacketizer state.
  835. */
  836. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_repacketizer_get_nb_frames(OpusRepacketizer *rp) OPUS_ARG_NONNULL(1);
  837. /** Construct a new packet from data previously submitted to the repacketizer
  838. * state via opus_repacketizer_cat().
  839. * This is a convenience routine that returns all the data submitted so far
  840. * in a single packet.
  841. * It is equivalent to calling
  842. * @code
  843. * opus_repacketizer_out_range(rp, 0, opus_repacketizer_get_nb_frames(rp),
  844. * data, maxlen)
  845. * @endcode
  846. * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state from which to
  847. * construct the new packet.
  848. * @param[out] data <tt>const unsigned char*</tt>: The buffer in which to
  849. * store the output packet.
  850. * @param maxlen <tt>opus_int32</tt>: The maximum number of bytes to store in
  851. * the output buffer. In order to guarantee
  852. * success, this should be at least
  853. * <code>1277*opus_repacketizer_get_nb_frames(rp)</code>.
  854. * However,
  855. * <code>1*opus_repacketizer_get_nb_frames(rp)</code>
  856. * plus the size of all packet data
  857. * submitted to the repacketizer since the
  858. * last call to opus_repacketizer_init() or
  859. * opus_repacketizer_create() is also
  860. * sufficient, and possibly much smaller.
  861. * @returns The total size of the output packet on success, or an error code
  862. * on failure.
  863. * @retval #OPUS_BUFFER_TOO_SMALL \a maxlen was insufficient to contain the
  864. * complete output packet.
  865. */
  866. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_repacketizer_out(OpusRepacketizer *rp, unsigned char *data, opus_int32 maxlen) OPUS_ARG_NONNULL(1);
  867. /** Pads a given Opus packet to a larger size (possibly changing the TOC sequence).
  868. * @param[in,out] data <tt>const unsigned char*</tt>: The buffer containing the
  869. * packet to pad.
  870. * @param len <tt>opus_int32</tt>: The size of the packet.
  871. * This must be at least 1.
  872. * @param new_len <tt>opus_int32</tt>: The desired size of the packet after padding.
  873. * This must be at least as large as len.
  874. * @returns an error code
  875. * @retval #OPUS_OK \a on success.
  876. * @retval #OPUS_BAD_ARG \a len was less than 1 or new_len was less than len.
  877. * @retval #OPUS_INVALID_PACKET \a data did not contain a valid Opus packet.
  878. */
  879. OPUS_EXPORT int opus_packet_pad(unsigned char *data, opus_int32 len, opus_int32 new_len);
  880. /** Remove all padding from a given Opus packet and rewrite the TOC sequence to
  881. * minimize space usage.
  882. * @param[in,out] data <tt>const unsigned char*</tt>: The buffer containing the
  883. * packet to strip.
  884. * @param len <tt>opus_int32</tt>: The size of the packet.
  885. * This must be at least 1.
  886. * @returns The new size of the output packet on success, or an error code
  887. * on failure.
  888. * @retval #OPUS_BAD_ARG \a len was less than 1.
  889. * @retval #OPUS_INVALID_PACKET \a data did not contain a valid Opus packet.
  890. */
  891. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_packet_unpad(unsigned char *data, opus_int32 len);
  892. /** Pads a given Opus multi-stream packet to a larger size (possibly changing the TOC sequence).
  893. * @param[in,out] data <tt>const unsigned char*</tt>: The buffer containing the
  894. * packet to pad.
  895. * @param len <tt>opus_int32</tt>: The size of the packet.
  896. * This must be at least 1.
  897. * @param new_len <tt>opus_int32</tt>: The desired size of the packet after padding.
  898. * This must be at least 1.
  899. * @param nb_streams <tt>opus_int32</tt>: The number of streams (not channels) in the packet.
  900. * This must be at least as large as len.
  901. * @returns an error code
  902. * @retval #OPUS_OK \a on success.
  903. * @retval #OPUS_BAD_ARG \a len was less than 1.
  904. * @retval #OPUS_INVALID_PACKET \a data did not contain a valid Opus packet.
  905. */
  906. OPUS_EXPORT int opus_multistream_packet_pad(unsigned char *data, opus_int32 len, opus_int32 new_len, int nb_streams);
  907. /** Remove all padding from a given Opus multi-stream packet and rewrite the TOC sequence to
  908. * minimize space usage.
  909. * @param[in,out] data <tt>const unsigned char*</tt>: The buffer containing the
  910. * packet to strip.
  911. * @param len <tt>opus_int32</tt>: The size of the packet.
  912. * This must be at least 1.
  913. * @param nb_streams <tt>opus_int32</tt>: The number of streams (not channels) in the packet.
  914. * This must be at least 1.
  915. * @returns The new size of the output packet on success, or an error code
  916. * on failure.
  917. * @retval #OPUS_BAD_ARG \a len was less than 1 or new_len was less than len.
  918. * @retval #OPUS_INVALID_PACKET \a data did not contain a valid Opus packet.
  919. */
  920. OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_multistream_packet_unpad(unsigned char *data, opus_int32 len, int nb_streams);
  921. /**@}*/
  922. #ifdef __cplusplus
  923. }
  924. #endif
  925. #endif /* OPUS_H */