annotate win32-msvc/include/opus/opus.h @ 70:9e21af8f0420

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