annotate win32-msvc/include/opus/opus.h @ 156:1bf23f5aebc4

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