1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libopus/include/opus.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,978 @@ 1.4 +/* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited 1.5 + Written by Jean-Marc Valin and Koen Vos */ 1.6 +/* 1.7 + Redistribution and use in source and binary forms, with or without 1.8 + modification, are permitted provided that the following conditions 1.9 + are met: 1.10 + 1.11 + - Redistributions of source code must retain the above copyright 1.12 + notice, this list of conditions and the following disclaimer. 1.13 + 1.14 + - Redistributions in binary form must reproduce the above copyright 1.15 + notice, this list of conditions and the following disclaimer in the 1.16 + documentation and/or other materials provided with the distribution. 1.17 + 1.18 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.19 + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.20 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.21 + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 1.22 + OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.23 + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.24 + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.25 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.26 + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.27 + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.28 + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.29 +*/ 1.30 + 1.31 +/** 1.32 + * @file opus.h 1.33 + * @brief Opus reference implementation API 1.34 + */ 1.35 + 1.36 +#ifndef OPUS_H 1.37 +#define OPUS_H 1.38 + 1.39 +#include "opus_types.h" 1.40 +#include "opus_defines.h" 1.41 + 1.42 +#ifdef __cplusplus 1.43 +extern "C" { 1.44 +#endif 1.45 + 1.46 +/** 1.47 + * @mainpage Opus 1.48 + * 1.49 + * The Opus codec is designed for interactive speech and audio transmission over the Internet. 1.50 + * It is designed by the IETF Codec Working Group and incorporates technology from 1.51 + * Skype's SILK codec and Xiph.Org's CELT codec. 1.52 + * 1.53 + * The Opus codec is designed to handle a wide range of interactive audio applications, 1.54 + * including Voice over IP, videoconferencing, in-game chat, and even remote live music 1.55 + * performances. It can scale from low bit-rate narrowband speech to very high quality 1.56 + * stereo music. Its main features are: 1.57 + 1.58 + * @li Sampling rates from 8 to 48 kHz 1.59 + * @li Bit-rates from 6 kb/s to 510 kb/s 1.60 + * @li Support for both constant bit-rate (CBR) and variable bit-rate (VBR) 1.61 + * @li Audio bandwidth from narrowband to full-band 1.62 + * @li Support for speech and music 1.63 + * @li Support for mono and stereo 1.64 + * @li Support for multichannel (up to 255 channels) 1.65 + * @li Frame sizes from 2.5 ms to 60 ms 1.66 + * @li Good loss robustness and packet loss concealment (PLC) 1.67 + * @li Floating point and fixed-point implementation 1.68 + * 1.69 + * Documentation sections: 1.70 + * @li @ref opus_encoder 1.71 + * @li @ref opus_decoder 1.72 + * @li @ref opus_repacketizer 1.73 + * @li @ref opus_multistream 1.74 + * @li @ref opus_libinfo 1.75 + * @li @ref opus_custom 1.76 + */ 1.77 + 1.78 +/** @defgroup opus_encoder Opus Encoder 1.79 + * @{ 1.80 + * 1.81 + * @brief This page describes the process and functions used to encode Opus. 1.82 + * 1.83 + * Since Opus is a stateful codec, the encoding process starts with creating an encoder 1.84 + * state. This can be done with: 1.85 + * 1.86 + * @code 1.87 + * int error; 1.88 + * OpusEncoder *enc; 1.89 + * enc = opus_encoder_create(Fs, channels, application, &error); 1.90 + * @endcode 1.91 + * 1.92 + * From this point, @c enc can be used for encoding an audio stream. An encoder state 1.93 + * @b must @b not be used for more than one stream at the same time. Similarly, the encoder 1.94 + * state @b must @b not be re-initialized for each frame. 1.95 + * 1.96 + * While opus_encoder_create() allocates memory for the state, it's also possible 1.97 + * to initialize pre-allocated memory: 1.98 + * 1.99 + * @code 1.100 + * int size; 1.101 + * int error; 1.102 + * OpusEncoder *enc; 1.103 + * size = opus_encoder_get_size(channels); 1.104 + * enc = malloc(size); 1.105 + * error = opus_encoder_init(enc, Fs, channels, application); 1.106 + * @endcode 1.107 + * 1.108 + * where opus_encoder_get_size() returns the required size for the encoder state. Note that 1.109 + * future versions of this code may change the size, so no assuptions should be made about it. 1.110 + * 1.111 + * The encoder state is always continuous in memory and only a shallow copy is sufficient 1.112 + * to copy it (e.g. memcpy()) 1.113 + * 1.114 + * It is possible to change some of the encoder's settings using the opus_encoder_ctl() 1.115 + * interface. All these settings already default to the recommended value, so they should 1.116 + * only be changed when necessary. The most common settings one may want to change are: 1.117 + * 1.118 + * @code 1.119 + * opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrate)); 1.120 + * opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(complexity)); 1.121 + * opus_encoder_ctl(enc, OPUS_SET_SIGNAL(signal_type)); 1.122 + * @endcode 1.123 + * 1.124 + * where 1.125 + * 1.126 + * @arg bitrate is in bits per second (b/s) 1.127 + * @arg complexity is a value from 1 to 10, where 1 is the lowest complexity and 10 is the highest 1.128 + * @arg signal_type is either OPUS_AUTO (default), OPUS_SIGNAL_VOICE, or OPUS_SIGNAL_MUSIC 1.129 + * 1.130 + * 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. 1.131 + * 1.132 + * 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: 1.133 + * @code 1.134 + * len = opus_encode(enc, audio_frame, frame_size, packet, max_packet); 1.135 + * @endcode 1.136 + * 1.137 + * where 1.138 + * <ul> 1.139 + * <li>audio_frame is the audio data in opus_int16 (or float for opus_encode_float())</li> 1.140 + * <li>frame_size is the duration of the frame in samples (per channel)</li> 1.141 + * <li>packet is the byte array to which the compressed data is written</li> 1.142 + * <li>max_packet is the maximum number of bytes that can be written in the packet (4000 bytes is recommended). 1.143 + * Do not use max_packet to control VBR target bitrate, instead use the #OPUS_SET_BITRATE CTL.</li> 1.144 + * </ul> 1.145 + * 1.146 + * opus_encode() and opus_encode_float() return the number of bytes actually written to the packet. 1.147 + * The return value <b>can be negative</b>, which indicates that an error has occurred. If the return value 1.148 + * is 1 byte, then the packet does not need to be transmitted (DTX). 1.149 + * 1.150 + * Once the encoder state if no longer needed, it can be destroyed with 1.151 + * 1.152 + * @code 1.153 + * opus_encoder_destroy(enc); 1.154 + * @endcode 1.155 + * 1.156 + * If the encoder was created with opus_encoder_init() rather than opus_encoder_create(), 1.157 + * then no action is required aside from potentially freeing the memory that was manually 1.158 + * allocated for it (calling free(enc) for the example above) 1.159 + * 1.160 + */ 1.161 + 1.162 +/** Opus encoder state. 1.163 + * This contains the complete state of an Opus encoder. 1.164 + * It is position independent and can be freely copied. 1.165 + * @see opus_encoder_create,opus_encoder_init 1.166 + */ 1.167 +typedef struct OpusEncoder OpusEncoder; 1.168 + 1.169 +/** Gets the size of an <code>OpusEncoder</code> structure. 1.170 + * @param[in] channels <tt>int</tt>: Number of channels. 1.171 + * This must be 1 or 2. 1.172 + * @returns The size in bytes. 1.173 + */ 1.174 +OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_encoder_get_size(int channels); 1.175 + 1.176 +/** 1.177 + */ 1.178 + 1.179 +/** Allocates and initializes an encoder state. 1.180 + * There are three coding modes: 1.181 + * 1.182 + * @ref OPUS_APPLICATION_VOIP gives best quality at a given bitrate for voice 1.183 + * signals. It enhances the input signal by high-pass filtering and 1.184 + * emphasizing formants and harmonics. Optionally it includes in-band 1.185 + * forward error correction to protect against packet loss. Use this 1.186 + * mode for typical VoIP applications. Because of the enhancement, 1.187 + * even at high bitrates the output may sound different from the input. 1.188 + * 1.189 + * @ref OPUS_APPLICATION_AUDIO gives best quality at a given bitrate for most 1.190 + * non-voice signals like music. Use this mode for music and mixed 1.191 + * (music/voice) content, broadcast, and applications requiring less 1.192 + * than 15 ms of coding delay. 1.193 + * 1.194 + * @ref OPUS_APPLICATION_RESTRICTED_LOWDELAY configures low-delay mode that 1.195 + * disables the speech-optimized mode in exchange for slightly reduced delay. 1.196 + * This mode can only be set on an newly initialized or freshly reset encoder 1.197 + * because it changes the codec delay. 1.198 + * 1.199 + * This is useful when the caller knows that the speech-optimized modes will not be needed (use with caution). 1.200 + * @param [in] Fs <tt>opus_int32</tt>: Sampling rate of input signal (Hz) 1.201 + * This must be one of 8000, 12000, 16000, 1.202 + * 24000, or 48000. 1.203 + * @param [in] channels <tt>int</tt>: Number of channels (1 or 2) in input signal 1.204 + * @param [in] application <tt>int</tt>: Coding mode (@ref OPUS_APPLICATION_VOIP/@ref OPUS_APPLICATION_AUDIO/@ref OPUS_APPLICATION_RESTRICTED_LOWDELAY) 1.205 + * @param [out] error <tt>int*</tt>: @ref opus_errorcodes 1.206 + * @note Regardless of the sampling rate and number channels selected, the Opus encoder 1.207 + * can switch to a lower audio bandwidth or number of channels if the bitrate 1.208 + * selected is too low. This also means that it is safe to always use 48 kHz stereo input 1.209 + * and let the encoder optimize the encoding. 1.210 + */ 1.211 +OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusEncoder *opus_encoder_create( 1.212 + opus_int32 Fs, 1.213 + int channels, 1.214 + int application, 1.215 + int *error 1.216 +); 1.217 + 1.218 +/** Initializes a previously allocated encoder state 1.219 + * The memory pointed to by st must be at least the size returned by opus_encoder_get_size(). 1.220 + * This is intended for applications which use their own allocator instead of malloc. 1.221 + * @see opus_encoder_create(),opus_encoder_get_size() 1.222 + * To reset a previously initialized state, use the #OPUS_RESET_STATE CTL. 1.223 + * @param [in] st <tt>OpusEncoder*</tt>: Encoder state 1.224 + * @param [in] Fs <tt>opus_int32</tt>: Sampling rate of input signal (Hz) 1.225 + * This must be one of 8000, 12000, 16000, 1.226 + * 24000, or 48000. 1.227 + * @param [in] channels <tt>int</tt>: Number of channels (1 or 2) in input signal 1.228 + * @param [in] application <tt>int</tt>: Coding mode (OPUS_APPLICATION_VOIP/OPUS_APPLICATION_AUDIO/OPUS_APPLICATION_RESTRICTED_LOWDELAY) 1.229 + * @retval #OPUS_OK Success or @ref opus_errorcodes 1.230 + */ 1.231 +OPUS_EXPORT int opus_encoder_init( 1.232 + OpusEncoder *st, 1.233 + opus_int32 Fs, 1.234 + int channels, 1.235 + int application 1.236 +) OPUS_ARG_NONNULL(1); 1.237 + 1.238 +/** Encodes an Opus frame. 1.239 + * @param [in] st <tt>OpusEncoder*</tt>: Encoder state 1.240 + * @param [in] pcm <tt>opus_int16*</tt>: Input signal (interleaved if 2 channels). length is frame_size*channels*sizeof(opus_int16) 1.241 + * @param [in] frame_size <tt>int</tt>: Number of samples per channel in the 1.242 + * input signal. 1.243 + * This must be an Opus frame size for 1.244 + * the encoder's sampling rate. 1.245 + * For example, at 48 kHz the permitted 1.246 + * values are 120, 240, 480, 960, 1920, 1.247 + * and 2880. 1.248 + * Passing in a duration of less than 1.249 + * 10 ms (480 samples at 48 kHz) will 1.250 + * prevent the encoder from using the LPC 1.251 + * or hybrid modes. 1.252 + * @param [out] data <tt>unsigned char*</tt>: Output payload. 1.253 + * This must contain storage for at 1.254 + * least \a max_data_bytes. 1.255 + * @param [in] max_data_bytes <tt>opus_int32</tt>: Size of the allocated 1.256 + * memory for the output 1.257 + * payload. This may be 1.258 + * used to impose an upper limit on 1.259 + * the instant bitrate, but should 1.260 + * not be used as the only bitrate 1.261 + * control. Use #OPUS_SET_BITRATE to 1.262 + * control the bitrate. 1.263 + * @returns The length of the encoded packet (in bytes) on success or a 1.264 + * negative error code (see @ref opus_errorcodes) on failure. 1.265 + */ 1.266 +OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_encode( 1.267 + OpusEncoder *st, 1.268 + const opus_int16 *pcm, 1.269 + int frame_size, 1.270 + unsigned char *data, 1.271 + opus_int32 max_data_bytes 1.272 +) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4); 1.273 + 1.274 +/** Encodes an Opus frame from floating point input. 1.275 + * @param [in] st <tt>OpusEncoder*</tt>: Encoder state 1.276 + * @param [in] pcm <tt>float*</tt>: Input in float format (interleaved if 2 channels), with a normal range of +/-1.0. 1.277 + * Samples with a range beyond +/-1.0 are supported but will 1.278 + * be clipped by decoders using the integer API and should 1.279 + * only be used if it is known that the far end supports 1.280 + * extended dynamic range. 1.281 + * length is frame_size*channels*sizeof(float) 1.282 + * @param [in] frame_size <tt>int</tt>: Number of samples per channel in the 1.283 + * input signal. 1.284 + * This must be an Opus frame size for 1.285 + * the encoder's sampling rate. 1.286 + * For example, at 48 kHz the permitted 1.287 + * values are 120, 240, 480, 960, 1920, 1.288 + * and 2880. 1.289 + * Passing in a duration of less than 1.290 + * 10 ms (480 samples at 48 kHz) will 1.291 + * prevent the encoder from using the LPC 1.292 + * or hybrid modes. 1.293 + * @param [out] data <tt>unsigned char*</tt>: Output payload. 1.294 + * This must contain storage for at 1.295 + * least \a max_data_bytes. 1.296 + * @param [in] max_data_bytes <tt>opus_int32</tt>: Size of the allocated 1.297 + * memory for the output 1.298 + * payload. This may be 1.299 + * used to impose an upper limit on 1.300 + * the instant bitrate, but should 1.301 + * not be used as the only bitrate 1.302 + * control. Use #OPUS_SET_BITRATE to 1.303 + * control the bitrate. 1.304 + * @returns The length of the encoded packet (in bytes) on success or a 1.305 + * negative error code (see @ref opus_errorcodes) on failure. 1.306 + */ 1.307 +OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_encode_float( 1.308 + OpusEncoder *st, 1.309 + const float *pcm, 1.310 + int frame_size, 1.311 + unsigned char *data, 1.312 + opus_int32 max_data_bytes 1.313 +) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4); 1.314 + 1.315 +/** Frees an <code>OpusEncoder</code> allocated by opus_encoder_create(). 1.316 + * @param[in] st <tt>OpusEncoder*</tt>: State to be freed. 1.317 + */ 1.318 +OPUS_EXPORT void opus_encoder_destroy(OpusEncoder *st); 1.319 + 1.320 +/** Perform a CTL function on an Opus encoder. 1.321 + * 1.322 + * Generally the request and subsequent arguments are generated 1.323 + * by a convenience macro. 1.324 + * @param st <tt>OpusEncoder*</tt>: Encoder state. 1.325 + * @param request This and all remaining parameters should be replaced by one 1.326 + * of the convenience macros in @ref opus_genericctls or 1.327 + * @ref opus_encoderctls. 1.328 + * @see opus_genericctls 1.329 + * @see opus_encoderctls 1.330 + */ 1.331 +OPUS_EXPORT int opus_encoder_ctl(OpusEncoder *st, int request, ...) OPUS_ARG_NONNULL(1); 1.332 +/**@}*/ 1.333 + 1.334 +/** @defgroup opus_decoder Opus Decoder 1.335 + * @{ 1.336 + * 1.337 + * @brief This page describes the process and functions used to decode Opus. 1.338 + * 1.339 + * The decoding process also starts with creating a decoder 1.340 + * state. This can be done with: 1.341 + * @code 1.342 + * int error; 1.343 + * OpusDecoder *dec; 1.344 + * dec = opus_decoder_create(Fs, channels, &error); 1.345 + * @endcode 1.346 + * where 1.347 + * @li Fs is the sampling rate and must be 8000, 12000, 16000, 24000, or 48000 1.348 + * @li channels is the number of channels (1 or 2) 1.349 + * @li error will hold the error code in case of failure (or #OPUS_OK on success) 1.350 + * @li the return value is a newly created decoder state to be used for decoding 1.351 + * 1.352 + * While opus_decoder_create() allocates memory for the state, it's also possible 1.353 + * to initialize pre-allocated memory: 1.354 + * @code 1.355 + * int size; 1.356 + * int error; 1.357 + * OpusDecoder *dec; 1.358 + * size = opus_decoder_get_size(channels); 1.359 + * dec = malloc(size); 1.360 + * error = opus_decoder_init(dec, Fs, channels); 1.361 + * @endcode 1.362 + * where opus_decoder_get_size() returns the required size for the decoder state. Note that 1.363 + * future versions of this code may change the size, so no assuptions should be made about it. 1.364 + * 1.365 + * The decoder state is always continuous in memory and only a shallow copy is sufficient 1.366 + * to copy it (e.g. memcpy()) 1.367 + * 1.368 + * To decode a frame, opus_decode() or opus_decode_float() must be called with a packet of compressed audio data: 1.369 + * @code 1.370 + * frame_size = opus_decode(dec, packet, len, decoded, max_size, 0); 1.371 + * @endcode 1.372 + * where 1.373 + * 1.374 + * @li packet is the byte array containing the compressed data 1.375 + * @li len is the exact number of bytes contained in the packet 1.376 + * @li decoded is the decoded audio data in opus_int16 (or float for opus_decode_float()) 1.377 + * @li max_size is the max duration of the frame in samples (per channel) that can fit into the decoded_frame array 1.378 + * 1.379 + * opus_decode() and opus_decode_float() return the number of samples (per channel) decoded from the packet. 1.380 + * If that value is negative, then an error has occurred. This can occur if the packet is corrupted or if the audio 1.381 + * buffer is too small to hold the decoded audio. 1.382 + * 1.383 + * Opus is a stateful codec with overlapping blocks and as a result Opus 1.384 + * packets are not coded independently of each other. Packets must be 1.385 + * passed into the decoder serially and in the correct order for a correct 1.386 + * decode. Lost packets can be replaced with loss concealment by calling 1.387 + * the decoder with a null pointer and zero length for the missing packet. 1.388 + * 1.389 + * A single codec state may only be accessed from a single thread at 1.390 + * a time and any required locking must be performed by the caller. Separate 1.391 + * streams must be decoded with separate decoder states and can be decoded 1.392 + * in parallel unless the library was compiled with NONTHREADSAFE_PSEUDOSTACK 1.393 + * defined. 1.394 + * 1.395 + */ 1.396 + 1.397 +/** Opus decoder state. 1.398 + * This contains the complete state of an Opus decoder. 1.399 + * It is position independent and can be freely copied. 1.400 + * @see opus_decoder_create,opus_decoder_init 1.401 + */ 1.402 +typedef struct OpusDecoder OpusDecoder; 1.403 + 1.404 +/** Gets the size of an <code>OpusDecoder</code> structure. 1.405 + * @param [in] channels <tt>int</tt>: Number of channels. 1.406 + * This must be 1 or 2. 1.407 + * @returns The size in bytes. 1.408 + */ 1.409 +OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decoder_get_size(int channels); 1.410 + 1.411 +/** Allocates and initializes a decoder state. 1.412 + * @param [in] Fs <tt>opus_int32</tt>: Sample rate to decode at (Hz). 1.413 + * This must be one of 8000, 12000, 16000, 1.414 + * 24000, or 48000. 1.415 + * @param [in] channels <tt>int</tt>: Number of channels (1 or 2) to decode 1.416 + * @param [out] error <tt>int*</tt>: #OPUS_OK Success or @ref opus_errorcodes 1.417 + * 1.418 + * Internally Opus stores data at 48000 Hz, so that should be the default 1.419 + * value for Fs. However, the decoder can efficiently decode to buffers 1.420 + * at 8, 12, 16, and 24 kHz so if for some reason the caller cannot use 1.421 + * data at the full sample rate, or knows the compressed data doesn't 1.422 + * use the full frequency range, it can request decoding at a reduced 1.423 + * rate. Likewise, the decoder is capable of filling in either mono or 1.424 + * interleaved stereo pcm buffers, at the caller's request. 1.425 + */ 1.426 +OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusDecoder *opus_decoder_create( 1.427 + opus_int32 Fs, 1.428 + int channels, 1.429 + int *error 1.430 +); 1.431 + 1.432 +/** Initializes a previously allocated decoder state. 1.433 + * The state must be at least the size returned by opus_decoder_get_size(). 1.434 + * This is intended for applications which use their own allocator instead of malloc. @see opus_decoder_create,opus_decoder_get_size 1.435 + * To reset a previously initialized state, use the #OPUS_RESET_STATE CTL. 1.436 + * @param [in] st <tt>OpusDecoder*</tt>: Decoder state. 1.437 + * @param [in] Fs <tt>opus_int32</tt>: Sampling rate to decode to (Hz). 1.438 + * This must be one of 8000, 12000, 16000, 1.439 + * 24000, or 48000. 1.440 + * @param [in] channels <tt>int</tt>: Number of channels (1 or 2) to decode 1.441 + * @retval #OPUS_OK Success or @ref opus_errorcodes 1.442 + */ 1.443 +OPUS_EXPORT int opus_decoder_init( 1.444 + OpusDecoder *st, 1.445 + opus_int32 Fs, 1.446 + int channels 1.447 +) OPUS_ARG_NONNULL(1); 1.448 + 1.449 +/** Decode an Opus packet. 1.450 + * @param [in] st <tt>OpusDecoder*</tt>: Decoder state 1.451 + * @param [in] data <tt>char*</tt>: Input payload. Use a NULL pointer to indicate packet loss 1.452 + * @param [in] len <tt>opus_int32</tt>: Number of bytes in payload* 1.453 + * @param [out] pcm <tt>opus_int16*</tt>: Output signal (interleaved if 2 channels). length 1.454 + * is frame_size*channels*sizeof(opus_int16) 1.455 + * @param [in] frame_size Number of samples per channel of available space in \a pcm. 1.456 + * If this is less than the maximum packet duration (120ms; 5760 for 48kHz), this function will 1.457 + * not be capable of decoding some packets. In the case of PLC (data==NULL) or FEC (decode_fec=1), 1.458 + * then frame_size needs to be exactly the duration of audio that is missing, otherwise the 1.459 + * decoder will not be in the optimal state to decode the next incoming packet. For the PLC and 1.460 + * FEC cases, frame_size <b>must</b> be a multiple of 2.5 ms. 1.461 + * @param [in] decode_fec <tt>int</tt>: Flag (0 or 1) to request that any in-band forward error correction data be 1.462 + * decoded. If no such data is available, the frame is decoded as if it were lost. 1.463 + * @returns Number of decoded samples or @ref opus_errorcodes 1.464 + */ 1.465 +OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decode( 1.466 + OpusDecoder *st, 1.467 + const unsigned char *data, 1.468 + opus_int32 len, 1.469 + opus_int16 *pcm, 1.470 + int frame_size, 1.471 + int decode_fec 1.472 +) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4); 1.473 + 1.474 +/** Decode an Opus packet with floating point output. 1.475 + * @param [in] st <tt>OpusDecoder*</tt>: Decoder state 1.476 + * @param [in] data <tt>char*</tt>: Input payload. Use a NULL pointer to indicate packet loss 1.477 + * @param [in] len <tt>opus_int32</tt>: Number of bytes in payload 1.478 + * @param [out] pcm <tt>float*</tt>: Output signal (interleaved if 2 channels). length 1.479 + * is frame_size*channels*sizeof(float) 1.480 + * @param [in] frame_size Number of samples per channel of available space in \a pcm. 1.481 + * If this is less than the maximum packet duration (120ms; 5760 for 48kHz), this function will 1.482 + * not be capable of decoding some packets. In the case of PLC (data==NULL) or FEC (decode_fec=1), 1.483 + * then frame_size needs to be exactly the duration of audio that is missing, otherwise the 1.484 + * decoder will not be in the optimal state to decode the next incoming packet. For the PLC and 1.485 + * FEC cases, frame_size <b>must</b> be a multiple of 2.5 ms. 1.486 + * @param [in] decode_fec <tt>int</tt>: Flag (0 or 1) to request that any in-band forward error correction data be 1.487 + * decoded. If no such data is available the frame is decoded as if it were lost. 1.488 + * @returns Number of decoded samples or @ref opus_errorcodes 1.489 + */ 1.490 +OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decode_float( 1.491 + OpusDecoder *st, 1.492 + const unsigned char *data, 1.493 + opus_int32 len, 1.494 + float *pcm, 1.495 + int frame_size, 1.496 + int decode_fec 1.497 +) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4); 1.498 + 1.499 +/** Perform a CTL function on an Opus decoder. 1.500 + * 1.501 + * Generally the request and subsequent arguments are generated 1.502 + * by a convenience macro. 1.503 + * @param st <tt>OpusDecoder*</tt>: Decoder state. 1.504 + * @param request This and all remaining parameters should be replaced by one 1.505 + * of the convenience macros in @ref opus_genericctls or 1.506 + * @ref opus_decoderctls. 1.507 + * @see opus_genericctls 1.508 + * @see opus_decoderctls 1.509 + */ 1.510 +OPUS_EXPORT int opus_decoder_ctl(OpusDecoder *st, int request, ...) OPUS_ARG_NONNULL(1); 1.511 + 1.512 +/** Frees an <code>OpusDecoder</code> allocated by opus_decoder_create(). 1.513 + * @param[in] st <tt>OpusDecoder*</tt>: State to be freed. 1.514 + */ 1.515 +OPUS_EXPORT void opus_decoder_destroy(OpusDecoder *st); 1.516 + 1.517 +/** Parse an opus packet into one or more frames. 1.518 + * Opus_decode will perform this operation internally so most applications do 1.519 + * not need to use this function. 1.520 + * This function does not copy the frames, the returned pointers are pointers into 1.521 + * the input packet. 1.522 + * @param [in] data <tt>char*</tt>: Opus packet to be parsed 1.523 + * @param [in] len <tt>opus_int32</tt>: size of data 1.524 + * @param [out] out_toc <tt>char*</tt>: TOC pointer 1.525 + * @param [out] frames <tt>char*[48]</tt> encapsulated frames 1.526 + * @param [out] size <tt>opus_int16[48]</tt> sizes of the encapsulated frames 1.527 + * @param [out] payload_offset <tt>int*</tt>: returns the position of the payload within the packet (in bytes) 1.528 + * @returns number of frames 1.529 + */ 1.530 +OPUS_EXPORT int opus_packet_parse( 1.531 + const unsigned char *data, 1.532 + opus_int32 len, 1.533 + unsigned char *out_toc, 1.534 + const unsigned char *frames[48], 1.535 + opus_int16 size[48], 1.536 + int *payload_offset 1.537 +) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4); 1.538 + 1.539 +/** Gets the bandwidth of an Opus packet. 1.540 + * @param [in] data <tt>char*</tt>: Opus packet 1.541 + * @retval OPUS_BANDWIDTH_NARROWBAND Narrowband (4kHz bandpass) 1.542 + * @retval OPUS_BANDWIDTH_MEDIUMBAND Mediumband (6kHz bandpass) 1.543 + * @retval OPUS_BANDWIDTH_WIDEBAND Wideband (8kHz bandpass) 1.544 + * @retval OPUS_BANDWIDTH_SUPERWIDEBAND Superwideband (12kHz bandpass) 1.545 + * @retval OPUS_BANDWIDTH_FULLBAND Fullband (20kHz bandpass) 1.546 + * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type 1.547 + */ 1.548 +OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_bandwidth(const unsigned char *data) OPUS_ARG_NONNULL(1); 1.549 + 1.550 +/** Gets the number of samples per frame from an Opus packet. 1.551 + * @param [in] data <tt>char*</tt>: Opus packet. 1.552 + * This must contain at least one byte of 1.553 + * data. 1.554 + * @param [in] Fs <tt>opus_int32</tt>: Sampling rate in Hz. 1.555 + * This must be a multiple of 400, or 1.556 + * inaccurate results will be returned. 1.557 + * @returns Number of samples per frame. 1.558 + */ 1.559 +OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_samples_per_frame(const unsigned char *data, opus_int32 Fs) OPUS_ARG_NONNULL(1); 1.560 + 1.561 +/** Gets the number of channels from an Opus packet. 1.562 + * @param [in] data <tt>char*</tt>: Opus packet 1.563 + * @returns Number of channels 1.564 + * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type 1.565 + */ 1.566 +OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_channels(const unsigned char *data) OPUS_ARG_NONNULL(1); 1.567 + 1.568 +/** Gets the number of frames in an Opus packet. 1.569 + * @param [in] packet <tt>char*</tt>: Opus packet 1.570 + * @param [in] len <tt>opus_int32</tt>: Length of packet 1.571 + * @returns Number of frames 1.572 + * @retval OPUS_BAD_ARG Insufficient data was passed to the function 1.573 + * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type 1.574 + */ 1.575 +OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_frames(const unsigned char packet[], opus_int32 len) OPUS_ARG_NONNULL(1); 1.576 + 1.577 +/** Gets the number of samples of an Opus packet. 1.578 + * @param [in] packet <tt>char*</tt>: Opus packet 1.579 + * @param [in] len <tt>opus_int32</tt>: Length of packet 1.580 + * @param [in] Fs <tt>opus_int32</tt>: Sampling rate in Hz. 1.581 + * This must be a multiple of 400, or 1.582 + * inaccurate results will be returned. 1.583 + * @returns Number of samples 1.584 + * @retval OPUS_BAD_ARG Insufficient data was passed to the function 1.585 + * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type 1.586 + */ 1.587 +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); 1.588 + 1.589 +/** Gets the number of samples of an Opus packet. 1.590 + * @param [in] dec <tt>OpusDecoder*</tt>: Decoder state 1.591 + * @param [in] packet <tt>char*</tt>: Opus packet 1.592 + * @param [in] len <tt>opus_int32</tt>: Length of packet 1.593 + * @returns Number of samples 1.594 + * @retval OPUS_BAD_ARG Insufficient data was passed to the function 1.595 + * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type 1.596 + */ 1.597 +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); 1.598 + 1.599 +/** Applies soft-clipping to bring a float signal within the [-1,1] range. If 1.600 + * the signal is already in that range, nothing is done. If there are values 1.601 + * outside of [-1,1], then the signal is clipped as smoothly as possible to 1.602 + * both fit in the range and avoid creating excessive distortion in the 1.603 + * process. 1.604 + * @param [in,out] pcm <tt>float*</tt>: Input PCM and modified PCM 1.605 + * @param [in] frame_size <tt>int</tt> Number of samples per channel to process 1.606 + * @param [in] channels <tt>int</tt>: Number of channels 1.607 + * @param [in,out] softclip_mem <tt>float*</tt>: State memory for the soft clipping process (one float per channel, initialized to zero) 1.608 + */ 1.609 +OPUS_EXPORT void opus_pcm_soft_clip(float *pcm, int frame_size, int channels, float *softclip_mem); 1.610 + 1.611 + 1.612 +/**@}*/ 1.613 + 1.614 +/** @defgroup opus_repacketizer Repacketizer 1.615 + * @{ 1.616 + * 1.617 + * The repacketizer can be used to merge multiple Opus packets into a single 1.618 + * packet or alternatively to split Opus packets that have previously been 1.619 + * merged. Splitting valid Opus packets is always guaranteed to succeed, 1.620 + * whereas merging valid packets only succeeds if all frames have the same 1.621 + * mode, bandwidth, and frame size, and when the total duration of the merged 1.622 + * packet is no more than 120 ms. 1.623 + * The repacketizer currently only operates on elementary Opus 1.624 + * streams. It will not manipualte multistream packets successfully, except in 1.625 + * the degenerate case where they consist of data from a single stream. 1.626 + * 1.627 + * The repacketizing process starts with creating a repacketizer state, either 1.628 + * by calling opus_repacketizer_create() or by allocating the memory yourself, 1.629 + * e.g., 1.630 + * @code 1.631 + * OpusRepacketizer *rp; 1.632 + * rp = (OpusRepacketizer*)malloc(opus_repacketizer_get_size()); 1.633 + * if (rp != NULL) 1.634 + * opus_repacketizer_init(rp); 1.635 + * @endcode 1.636 + * 1.637 + * Then the application should submit packets with opus_repacketizer_cat(), 1.638 + * extract new packets with opus_repacketizer_out() or 1.639 + * opus_repacketizer_out_range(), and then reset the state for the next set of 1.640 + * input packets via opus_repacketizer_init(). 1.641 + * 1.642 + * For example, to split a sequence of packets into individual frames: 1.643 + * @code 1.644 + * unsigned char *data; 1.645 + * int len; 1.646 + * while (get_next_packet(&data, &len)) 1.647 + * { 1.648 + * unsigned char out[1276]; 1.649 + * opus_int32 out_len; 1.650 + * int nb_frames; 1.651 + * int err; 1.652 + * int i; 1.653 + * err = opus_repacketizer_cat(rp, data, len); 1.654 + * if (err != OPUS_OK) 1.655 + * { 1.656 + * release_packet(data); 1.657 + * return err; 1.658 + * } 1.659 + * nb_frames = opus_repacketizer_get_nb_frames(rp); 1.660 + * for (i = 0; i < nb_frames; i++) 1.661 + * { 1.662 + * out_len = opus_repacketizer_out_range(rp, i, i+1, out, sizeof(out)); 1.663 + * if (out_len < 0) 1.664 + * { 1.665 + * release_packet(data); 1.666 + * return (int)out_len; 1.667 + * } 1.668 + * output_next_packet(out, out_len); 1.669 + * } 1.670 + * opus_repacketizer_init(rp); 1.671 + * release_packet(data); 1.672 + * } 1.673 + * @endcode 1.674 + * 1.675 + * Alternatively, to combine a sequence of frames into packets that each 1.676 + * contain up to <code>TARGET_DURATION_MS</code> milliseconds of data: 1.677 + * @code 1.678 + * // The maximum number of packets with duration TARGET_DURATION_MS occurs 1.679 + * // when the frame size is 2.5 ms, for a total of (TARGET_DURATION_MS*2/5) 1.680 + * // packets. 1.681 + * unsigned char *data[(TARGET_DURATION_MS*2/5)+1]; 1.682 + * opus_int32 len[(TARGET_DURATION_MS*2/5)+1]; 1.683 + * int nb_packets; 1.684 + * unsigned char out[1277*(TARGET_DURATION_MS*2/2)]; 1.685 + * opus_int32 out_len; 1.686 + * int prev_toc; 1.687 + * nb_packets = 0; 1.688 + * while (get_next_packet(data+nb_packets, len+nb_packets)) 1.689 + * { 1.690 + * int nb_frames; 1.691 + * int err; 1.692 + * nb_frames = opus_packet_get_nb_frames(data[nb_packets], len[nb_packets]); 1.693 + * if (nb_frames < 1) 1.694 + * { 1.695 + * release_packets(data, nb_packets+1); 1.696 + * return nb_frames; 1.697 + * } 1.698 + * nb_frames += opus_repacketizer_get_nb_frames(rp); 1.699 + * // If adding the next packet would exceed our target, or it has an 1.700 + * // incompatible TOC sequence, output the packets we already have before 1.701 + * // submitting it. 1.702 + * // N.B., The nb_packets > 0 check ensures we've submitted at least one 1.703 + * // packet since the last call to opus_repacketizer_init(). Otherwise a 1.704 + * // single packet longer than TARGET_DURATION_MS would cause us to try to 1.705 + * // output an (invalid) empty packet. It also ensures that prev_toc has 1.706 + * // been set to a valid value. Additionally, len[nb_packets] > 0 is 1.707 + * // guaranteed by the call to opus_packet_get_nb_frames() above, so the 1.708 + * // reference to data[nb_packets][0] should be valid. 1.709 + * if (nb_packets > 0 && ( 1.710 + * ((prev_toc & 0xFC) != (data[nb_packets][0] & 0xFC)) || 1.711 + * opus_packet_get_samples_per_frame(data[nb_packets], 48000)*nb_frames > 1.712 + * TARGET_DURATION_MS*48)) 1.713 + * { 1.714 + * out_len = opus_repacketizer_out(rp, out, sizeof(out)); 1.715 + * if (out_len < 0) 1.716 + * { 1.717 + * release_packets(data, nb_packets+1); 1.718 + * return (int)out_len; 1.719 + * } 1.720 + * output_next_packet(out, out_len); 1.721 + * opus_repacketizer_init(rp); 1.722 + * release_packets(data, nb_packets); 1.723 + * data[0] = data[nb_packets]; 1.724 + * len[0] = len[nb_packets]; 1.725 + * nb_packets = 0; 1.726 + * } 1.727 + * err = opus_repacketizer_cat(rp, data[nb_packets], len[nb_packets]); 1.728 + * if (err != OPUS_OK) 1.729 + * { 1.730 + * release_packets(data, nb_packets+1); 1.731 + * return err; 1.732 + * } 1.733 + * prev_toc = data[nb_packets][0]; 1.734 + * nb_packets++; 1.735 + * } 1.736 + * // Output the final, partial packet. 1.737 + * if (nb_packets > 0) 1.738 + * { 1.739 + * out_len = opus_repacketizer_out(rp, out, sizeof(out)); 1.740 + * release_packets(data, nb_packets); 1.741 + * if (out_len < 0) 1.742 + * return (int)out_len; 1.743 + * output_next_packet(out, out_len); 1.744 + * } 1.745 + * @endcode 1.746 + * 1.747 + * An alternate way of merging packets is to simply call opus_repacketizer_cat() 1.748 + * unconditionally until it fails. At that point, the merged packet can be 1.749 + * obtained with opus_repacketizer_out() and the input packet for which 1.750 + * opus_repacketizer_cat() needs to be re-added to a newly reinitialized 1.751 + * repacketizer state. 1.752 + */ 1.753 + 1.754 +typedef struct OpusRepacketizer OpusRepacketizer; 1.755 + 1.756 +/** Gets the size of an <code>OpusRepacketizer</code> structure. 1.757 + * @returns The size in bytes. 1.758 + */ 1.759 +OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_repacketizer_get_size(void); 1.760 + 1.761 +/** (Re)initializes a previously allocated repacketizer state. 1.762 + * The state must be at least the size returned by opus_repacketizer_get_size(). 1.763 + * This can be used for applications which use their own allocator instead of 1.764 + * malloc(). 1.765 + * It must also be called to reset the queue of packets waiting to be 1.766 + * repacketized, which is necessary if the maximum packet duration of 120 ms 1.767 + * is reached or if you wish to submit packets with a different Opus 1.768 + * configuration (coding mode, audio bandwidth, frame size, or channel count). 1.769 + * Failure to do so will prevent a new packet from being added with 1.770 + * opus_repacketizer_cat(). 1.771 + * @see opus_repacketizer_create 1.772 + * @see opus_repacketizer_get_size 1.773 + * @see opus_repacketizer_cat 1.774 + * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state to 1.775 + * (re)initialize. 1.776 + * @returns A pointer to the same repacketizer state that was passed in. 1.777 + */ 1.778 +OPUS_EXPORT OpusRepacketizer *opus_repacketizer_init(OpusRepacketizer *rp) OPUS_ARG_NONNULL(1); 1.779 + 1.780 +/** Allocates memory and initializes the new repacketizer with 1.781 + * opus_repacketizer_init(). 1.782 + */ 1.783 +OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusRepacketizer *opus_repacketizer_create(void); 1.784 + 1.785 +/** Frees an <code>OpusRepacketizer</code> allocated by 1.786 + * opus_repacketizer_create(). 1.787 + * @param[in] rp <tt>OpusRepacketizer*</tt>: State to be freed. 1.788 + */ 1.789 +OPUS_EXPORT void opus_repacketizer_destroy(OpusRepacketizer *rp); 1.790 + 1.791 +/** Add a packet to the current repacketizer state. 1.792 + * This packet must match the configuration of any packets already submitted 1.793 + * for repacketization since the last call to opus_repacketizer_init(). 1.794 + * This means that it must have the same coding mode, audio bandwidth, frame 1.795 + * size, and channel count. 1.796 + * This can be checked in advance by examining the top 6 bits of the first 1.797 + * byte of the packet, and ensuring they match the top 6 bits of the first 1.798 + * byte of any previously submitted packet. 1.799 + * The total duration of audio in the repacketizer state also must not exceed 1.800 + * 120 ms, the maximum duration of a single packet, after adding this packet. 1.801 + * 1.802 + * The contents of the current repacketizer state can be extracted into new 1.803 + * packets using opus_repacketizer_out() or opus_repacketizer_out_range(). 1.804 + * 1.805 + * In order to add a packet with a different configuration or to add more 1.806 + * audio beyond 120 ms, you must clear the repacketizer state by calling 1.807 + * opus_repacketizer_init(). 1.808 + * If a packet is too large to add to the current repacketizer state, no part 1.809 + * of it is added, even if it contains multiple frames, some of which might 1.810 + * fit. 1.811 + * If you wish to be able to add parts of such packets, you should first use 1.812 + * another repacketizer to split the packet into pieces and add them 1.813 + * individually. 1.814 + * @see opus_repacketizer_out_range 1.815 + * @see opus_repacketizer_out 1.816 + * @see opus_repacketizer_init 1.817 + * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state to which to 1.818 + * add the packet. 1.819 + * @param[in] data <tt>const unsigned char*</tt>: The packet data. 1.820 + * The application must ensure 1.821 + * this pointer remains valid 1.822 + * until the next call to 1.823 + * opus_repacketizer_init() or 1.824 + * opus_repacketizer_destroy(). 1.825 + * @param len <tt>opus_int32</tt>: The number of bytes in the packet data. 1.826 + * @returns An error code indicating whether or not the operation succeeded. 1.827 + * @retval #OPUS_OK The packet's contents have been added to the repacketizer 1.828 + * state. 1.829 + * @retval #OPUS_INVALID_PACKET The packet did not have a valid TOC sequence, 1.830 + * the packet's TOC sequence was not compatible 1.831 + * with previously submitted packets (because 1.832 + * the coding mode, audio bandwidth, frame size, 1.833 + * or channel count did not match), or adding 1.834 + * this packet would increase the total amount of 1.835 + * audio stored in the repacketizer state to more 1.836 + * than 120 ms. 1.837 + */ 1.838 +OPUS_EXPORT int opus_repacketizer_cat(OpusRepacketizer *rp, const unsigned char *data, opus_int32 len) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2); 1.839 + 1.840 + 1.841 +/** Construct a new packet from data previously submitted to the repacketizer 1.842 + * state via opus_repacketizer_cat(). 1.843 + * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state from which to 1.844 + * construct the new packet. 1.845 + * @param begin <tt>int</tt>: The index of the first frame in the current 1.846 + * repacketizer state to include in the output. 1.847 + * @param end <tt>int</tt>: One past the index of the last frame in the 1.848 + * current repacketizer state to include in the 1.849 + * output. 1.850 + * @param[out] data <tt>const unsigned char*</tt>: The buffer in which to 1.851 + * store the output packet. 1.852 + * @param maxlen <tt>opus_int32</tt>: The maximum number of bytes to store in 1.853 + * the output buffer. In order to guarantee 1.854 + * success, this should be at least 1.855 + * <code>1276</code> for a single frame, 1.856 + * or for multiple frames, 1.857 + * <code>1277*(end-begin)</code>. 1.858 + * However, <code>1*(end-begin)</code> plus 1.859 + * the size of all packet data submitted to 1.860 + * the repacketizer since the last call to 1.861 + * opus_repacketizer_init() or 1.862 + * opus_repacketizer_create() is also 1.863 + * sufficient, and possibly much smaller. 1.864 + * @returns The total size of the output packet on success, or an error code 1.865 + * on failure. 1.866 + * @retval #OPUS_BAD_ARG <code>[begin,end)</code> was an invalid range of 1.867 + * frames (begin < 0, begin >= end, or end > 1.868 + * opus_repacketizer_get_nb_frames()). 1.869 + * @retval #OPUS_BUFFER_TOO_SMALL \a maxlen was insufficient to contain the 1.870 + * complete output packet. 1.871 + */ 1.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); 1.873 + 1.874 +/** Return the total number of frames contained in packet data submitted to 1.875 + * the repacketizer state so far via opus_repacketizer_cat() since the last 1.876 + * call to opus_repacketizer_init() or opus_repacketizer_create(). 1.877 + * This defines the valid range of packets that can be extracted with 1.878 + * opus_repacketizer_out_range() or opus_repacketizer_out(). 1.879 + * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state containing the 1.880 + * frames. 1.881 + * @returns The total number of frames contained in the packet data submitted 1.882 + * to the repacketizer state. 1.883 + */ 1.884 +OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_repacketizer_get_nb_frames(OpusRepacketizer *rp) OPUS_ARG_NONNULL(1); 1.885 + 1.886 +/** Construct a new packet from data previously submitted to the repacketizer 1.887 + * state via opus_repacketizer_cat(). 1.888 + * This is a convenience routine that returns all the data submitted so far 1.889 + * in a single packet. 1.890 + * It is equivalent to calling 1.891 + * @code 1.892 + * opus_repacketizer_out_range(rp, 0, opus_repacketizer_get_nb_frames(rp), 1.893 + * data, maxlen) 1.894 + * @endcode 1.895 + * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state from which to 1.896 + * construct the new packet. 1.897 + * @param[out] data <tt>const unsigned char*</tt>: The buffer in which to 1.898 + * store the output packet. 1.899 + * @param maxlen <tt>opus_int32</tt>: The maximum number of bytes to store in 1.900 + * the output buffer. In order to guarantee 1.901 + * success, this should be at least 1.902 + * <code>1277*opus_repacketizer_get_nb_frames(rp)</code>. 1.903 + * However, 1.904 + * <code>1*opus_repacketizer_get_nb_frames(rp)</code> 1.905 + * plus the size of all packet data 1.906 + * submitted to the repacketizer since the 1.907 + * last call to opus_repacketizer_init() or 1.908 + * opus_repacketizer_create() is also 1.909 + * sufficient, and possibly much smaller. 1.910 + * @returns The total size of the output packet on success, or an error code 1.911 + * on failure. 1.912 + * @retval #OPUS_BUFFER_TOO_SMALL \a maxlen was insufficient to contain the 1.913 + * complete output packet. 1.914 + */ 1.915 +OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_repacketizer_out(OpusRepacketizer *rp, unsigned char *data, opus_int32 maxlen) OPUS_ARG_NONNULL(1); 1.916 + 1.917 +/** Pads a given Opus packet to a larger size (possibly changing the TOC sequence). 1.918 + * @param[in,out] data <tt>const unsigned char*</tt>: The buffer containing the 1.919 + * packet to pad. 1.920 + * @param len <tt>opus_int32</tt>: The size of the packet. 1.921 + * This must be at least 1. 1.922 + * @param new_len <tt>opus_int32</tt>: The desired size of the packet after padding. 1.923 + * This must be at least as large as len. 1.924 + * @returns an error code 1.925 + * @retval #OPUS_OK \a on success. 1.926 + * @retval #OPUS_BAD_ARG \a len was less than 1 or new_len was less than len. 1.927 + * @retval #OPUS_INVALID_PACKET \a data did not contain a valid Opus packet. 1.928 + */ 1.929 +OPUS_EXPORT int opus_packet_pad(unsigned char *data, opus_int32 len, opus_int32 new_len); 1.930 + 1.931 +/** Remove all padding from a given Opus packet and rewrite the TOC sequence to 1.932 + * minimize space usage. 1.933 + * @param[in,out] data <tt>const unsigned char*</tt>: The buffer containing the 1.934 + * packet to strip. 1.935 + * @param len <tt>opus_int32</tt>: The size of the packet. 1.936 + * This must be at least 1. 1.937 + * @returns The new size of the output packet on success, or an error code 1.938 + * on failure. 1.939 + * @retval #OPUS_BAD_ARG \a len was less than 1. 1.940 + * @retval #OPUS_INVALID_PACKET \a data did not contain a valid Opus packet. 1.941 + */ 1.942 +OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_packet_unpad(unsigned char *data, opus_int32 len); 1.943 + 1.944 +/** Pads a given Opus multi-stream packet to a larger size (possibly changing the TOC sequence). 1.945 + * @param[in,out] data <tt>const unsigned char*</tt>: The buffer containing the 1.946 + * packet to pad. 1.947 + * @param len <tt>opus_int32</tt>: The size of the packet. 1.948 + * This must be at least 1. 1.949 + * @param new_len <tt>opus_int32</tt>: The desired size of the packet after padding. 1.950 + * This must be at least 1. 1.951 + * @param nb_streams <tt>opus_int32</tt>: The number of streams (not channels) in the packet. 1.952 + * This must be at least as large as len. 1.953 + * @returns an error code 1.954 + * @retval #OPUS_OK \a on success. 1.955 + * @retval #OPUS_BAD_ARG \a len was less than 1. 1.956 + * @retval #OPUS_INVALID_PACKET \a data did not contain a valid Opus packet. 1.957 + */ 1.958 +OPUS_EXPORT int opus_multistream_packet_pad(unsigned char *data, opus_int32 len, opus_int32 new_len, int nb_streams); 1.959 + 1.960 +/** Remove all padding from a given Opus multi-stream packet and rewrite the TOC sequence to 1.961 + * minimize space usage. 1.962 + * @param[in,out] data <tt>const unsigned char*</tt>: The buffer containing the 1.963 + * packet to strip. 1.964 + * @param len <tt>opus_int32</tt>: The size of the packet. 1.965 + * This must be at least 1. 1.966 + * @param nb_streams <tt>opus_int32</tt>: The number of streams (not channels) in the packet. 1.967 + * This must be at least 1. 1.968 + * @returns The new size of the output packet on success, or an error code 1.969 + * on failure. 1.970 + * @retval #OPUS_BAD_ARG \a len was less than 1 or new_len was less than len. 1.971 + * @retval #OPUS_INVALID_PACKET \a data did not contain a valid Opus packet. 1.972 + */ 1.973 +OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_multistream_packet_unpad(unsigned char *data, opus_int32 len, int nb_streams); 1.974 + 1.975 +/**@}*/ 1.976 + 1.977 +#ifdef __cplusplus 1.978 +} 1.979 +#endif 1.980 + 1.981 +#endif /* OPUS_H */