cannam@154: /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited cannam@154: Written by Jean-Marc Valin and Koen Vos */ cannam@154: /* cannam@154: Redistribution and use in source and binary forms, with or without cannam@154: modification, are permitted provided that the following conditions cannam@154: are met: cannam@154: cannam@154: - Redistributions of source code must retain the above copyright cannam@154: notice, this list of conditions and the following disclaimer. cannam@154: cannam@154: - Redistributions in binary form must reproduce the above copyright cannam@154: notice, this list of conditions and the following disclaimer in the cannam@154: documentation and/or other materials provided with the distribution. cannam@154: cannam@154: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS cannam@154: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT cannam@154: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR cannam@154: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER cannam@154: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, cannam@154: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, cannam@154: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR cannam@154: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF cannam@154: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING cannam@154: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS cannam@154: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cannam@154: */ cannam@154: cannam@154: /** cannam@154: * @file opus.h cannam@154: * @brief Opus reference implementation API cannam@154: */ cannam@154: cannam@154: #ifndef OPUS_H cannam@154: #define OPUS_H cannam@154: cannam@154: #include "opus_types.h" cannam@154: #include "opus_defines.h" cannam@154: cannam@154: #ifdef __cplusplus cannam@154: extern "C" { cannam@154: #endif cannam@154: cannam@154: /** cannam@154: * @mainpage Opus cannam@154: * cannam@154: * The Opus codec is designed for interactive speech and audio transmission over the Internet. cannam@154: * It is designed by the IETF Codec Working Group and incorporates technology from cannam@154: * Skype's SILK codec and Xiph.Org's CELT codec. cannam@154: * cannam@154: * The Opus codec is designed to handle a wide range of interactive audio applications, cannam@154: * including Voice over IP, videoconferencing, in-game chat, and even remote live music cannam@154: * performances. It can scale from low bit-rate narrowband speech to very high quality cannam@154: * stereo music. Its main features are: cannam@154: cannam@154: * @li Sampling rates from 8 to 48 kHz cannam@154: * @li Bit-rates from 6 kb/s to 510 kb/s cannam@154: * @li Support for both constant bit-rate (CBR) and variable bit-rate (VBR) cannam@154: * @li Audio bandwidth from narrowband to full-band cannam@154: * @li Support for speech and music cannam@154: * @li Support for mono and stereo cannam@154: * @li Support for multichannel (up to 255 channels) cannam@154: * @li Frame sizes from 2.5 ms to 60 ms cannam@154: * @li Good loss robustness and packet loss concealment (PLC) cannam@154: * @li Floating point and fixed-point implementation cannam@154: * cannam@154: * Documentation sections: cannam@154: * @li @ref opus_encoder cannam@154: * @li @ref opus_decoder cannam@154: * @li @ref opus_repacketizer cannam@154: * @li @ref opus_multistream cannam@154: * @li @ref opus_libinfo cannam@154: * @li @ref opus_custom cannam@154: */ cannam@154: cannam@154: /** @defgroup opus_encoder Opus Encoder cannam@154: * @{ cannam@154: * cannam@154: * @brief This page describes the process and functions used to encode Opus. cannam@154: * cannam@154: * Since Opus is a stateful codec, the encoding process starts with creating an encoder cannam@154: * state. This can be done with: cannam@154: * cannam@154: * @code cannam@154: * int error; cannam@154: * OpusEncoder *enc; cannam@154: * enc = opus_encoder_create(Fs, channels, application, &error); cannam@154: * @endcode cannam@154: * cannam@154: * From this point, @c enc can be used for encoding an audio stream. An encoder state cannam@154: * @b must @b not be used for more than one stream at the same time. Similarly, the encoder cannam@154: * state @b must @b not be re-initialized for each frame. cannam@154: * cannam@154: * While opus_encoder_create() allocates memory for the state, it's also possible cannam@154: * to initialize pre-allocated memory: cannam@154: * cannam@154: * @code cannam@154: * int size; cannam@154: * int error; cannam@154: * OpusEncoder *enc; cannam@154: * size = opus_encoder_get_size(channels); cannam@154: * enc = malloc(size); cannam@154: * error = opus_encoder_init(enc, Fs, channels, application); cannam@154: * @endcode cannam@154: * cannam@154: * where opus_encoder_get_size() returns the required size for the encoder state. Note that cannam@154: * future versions of this code may change the size, so no assuptions should be made about it. cannam@154: * cannam@154: * The encoder state is always continuous in memory and only a shallow copy is sufficient cannam@154: * to copy it (e.g. memcpy()) cannam@154: * cannam@154: * It is possible to change some of the encoder's settings using the opus_encoder_ctl() cannam@154: * interface. All these settings already default to the recommended value, so they should cannam@154: * only be changed when necessary. The most common settings one may want to change are: cannam@154: * cannam@154: * @code cannam@154: * opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrate)); cannam@154: * opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(complexity)); cannam@154: * opus_encoder_ctl(enc, OPUS_SET_SIGNAL(signal_type)); cannam@154: * @endcode cannam@154: * cannam@154: * where cannam@154: * cannam@154: * @arg bitrate is in bits per second (b/s) cannam@154: * @arg complexity is a value from 1 to 10, where 1 is the lowest complexity and 10 is the highest cannam@154: * @arg signal_type is either OPUS_AUTO (default), OPUS_SIGNAL_VOICE, or OPUS_SIGNAL_MUSIC cannam@154: * cannam@154: * 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@154: * cannam@154: * 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@154: * @code cannam@154: * len = opus_encode(enc, audio_frame, frame_size, packet, max_packet); cannam@154: * @endcode cannam@154: * cannam@154: * where cannam@154: * cannam@154: * cannam@154: * opus_encode() and opus_encode_float() return the number of bytes actually written to the packet. cannam@154: * The return value can be negative, which indicates that an error has occurred. If the return value cannam@154: * is 2 bytes or less, then the packet does not need to be transmitted (DTX). cannam@154: * cannam@154: * Once the encoder state if no longer needed, it can be destroyed with cannam@154: * cannam@154: * @code cannam@154: * opus_encoder_destroy(enc); cannam@154: * @endcode cannam@154: * cannam@154: * If the encoder was created with opus_encoder_init() rather than opus_encoder_create(), cannam@154: * then no action is required aside from potentially freeing the memory that was manually cannam@154: * allocated for it (calling free(enc) for the example above) cannam@154: * cannam@154: */ cannam@154: cannam@154: /** Opus encoder state. cannam@154: * This contains the complete state of an Opus encoder. cannam@154: * It is position independent and can be freely copied. cannam@154: * @see opus_encoder_create,opus_encoder_init cannam@154: */ cannam@154: typedef struct OpusEncoder OpusEncoder; cannam@154: cannam@154: /** Gets the size of an OpusEncoder structure. cannam@154: * @param[in] channels int: Number of channels. cannam@154: * This must be 1 or 2. cannam@154: * @returns The size in bytes. cannam@154: */ cannam@154: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_encoder_get_size(int channels); cannam@154: cannam@154: /** cannam@154: */ cannam@154: cannam@154: /** Allocates and initializes an encoder state. cannam@154: * There are three coding modes: cannam@154: * cannam@154: * @ref OPUS_APPLICATION_VOIP gives best quality at a given bitrate for voice cannam@154: * signals. It enhances the input signal by high-pass filtering and cannam@154: * emphasizing formants and harmonics. Optionally it includes in-band cannam@154: * forward error correction to protect against packet loss. Use this cannam@154: * mode for typical VoIP applications. Because of the enhancement, cannam@154: * even at high bitrates the output may sound different from the input. cannam@154: * cannam@154: * @ref OPUS_APPLICATION_AUDIO gives best quality at a given bitrate for most cannam@154: * non-voice signals like music. Use this mode for music and mixed cannam@154: * (music/voice) content, broadcast, and applications requiring less cannam@154: * than 15 ms of coding delay. cannam@154: * cannam@154: * @ref OPUS_APPLICATION_RESTRICTED_LOWDELAY configures low-delay mode that cannam@154: * disables the speech-optimized mode in exchange for slightly reduced delay. cannam@154: * This mode can only be set on an newly initialized or freshly reset encoder cannam@154: * because it changes the codec delay. cannam@154: * cannam@154: * This is useful when the caller knows that the speech-optimized modes will not be needed (use with caution). cannam@154: * @param [in] Fs opus_int32: Sampling rate of input signal (Hz) cannam@154: * This must be one of 8000, 12000, 16000, cannam@154: * 24000, or 48000. cannam@154: * @param [in] channels int: Number of channels (1 or 2) in input signal cannam@154: * @param [in] application int: Coding mode (@ref OPUS_APPLICATION_VOIP/@ref OPUS_APPLICATION_AUDIO/@ref OPUS_APPLICATION_RESTRICTED_LOWDELAY) cannam@154: * @param [out] error int*: @ref opus_errorcodes cannam@154: * @note Regardless of the sampling rate and number channels selected, the Opus encoder cannam@154: * can switch to a lower audio bandwidth or number of channels if the bitrate cannam@154: * selected is too low. This also means that it is safe to always use 48 kHz stereo input cannam@154: * and let the encoder optimize the encoding. cannam@154: */ cannam@154: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusEncoder *opus_encoder_create( cannam@154: opus_int32 Fs, cannam@154: int channels, cannam@154: int application, cannam@154: int *error cannam@154: ); cannam@154: cannam@154: /** Initializes a previously allocated encoder state cannam@154: * The memory pointed to by st must be at least the size returned by opus_encoder_get_size(). cannam@154: * This is intended for applications which use their own allocator instead of malloc. cannam@154: * @see opus_encoder_create(),opus_encoder_get_size() cannam@154: * To reset a previously initialized state, use the #OPUS_RESET_STATE CTL. cannam@154: * @param [in] st OpusEncoder*: Encoder state cannam@154: * @param [in] Fs opus_int32: Sampling rate of input signal (Hz) cannam@154: * This must be one of 8000, 12000, 16000, cannam@154: * 24000, or 48000. cannam@154: * @param [in] channels int: Number of channels (1 or 2) in input signal cannam@154: * @param [in] application int: Coding mode (OPUS_APPLICATION_VOIP/OPUS_APPLICATION_AUDIO/OPUS_APPLICATION_RESTRICTED_LOWDELAY) cannam@154: * @retval #OPUS_OK Success or @ref opus_errorcodes cannam@154: */ cannam@154: OPUS_EXPORT int opus_encoder_init( cannam@154: OpusEncoder *st, cannam@154: opus_int32 Fs, cannam@154: int channels, cannam@154: int application cannam@154: ) OPUS_ARG_NONNULL(1); cannam@154: cannam@154: /** Encodes an Opus frame. cannam@154: * @param [in] st OpusEncoder*: Encoder state cannam@154: * @param [in] pcm opus_int16*: Input signal (interleaved if 2 channels). length is frame_size*channels*sizeof(opus_int16) cannam@154: * @param [in] frame_size int: Number of samples per channel in the cannam@154: * input signal. cannam@154: * This must be an Opus frame size for cannam@154: * the encoder's sampling rate. cannam@154: * For example, at 48 kHz the permitted cannam@154: * values are 120, 240, 480, 960, 1920, cannam@154: * and 2880. cannam@154: * Passing in a duration of less than cannam@154: * 10 ms (480 samples at 48 kHz) will cannam@154: * prevent the encoder from using the LPC cannam@154: * or hybrid modes. cannam@154: * @param [out] data unsigned char*: Output payload. cannam@154: * This must contain storage for at cannam@154: * least \a max_data_bytes. cannam@154: * @param [in] max_data_bytes opus_int32: Size of the allocated cannam@154: * memory for the output cannam@154: * payload. This may be cannam@154: * used to impose an upper limit on cannam@154: * the instant bitrate, but should cannam@154: * not be used as the only bitrate cannam@154: * control. Use #OPUS_SET_BITRATE to cannam@154: * control the bitrate. cannam@154: * @returns The length of the encoded packet (in bytes) on success or a cannam@154: * negative error code (see @ref opus_errorcodes) on failure. cannam@154: */ cannam@154: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_encode( cannam@154: OpusEncoder *st, cannam@154: const opus_int16 *pcm, cannam@154: int frame_size, cannam@154: unsigned char *data, cannam@154: opus_int32 max_data_bytes cannam@154: ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4); cannam@154: cannam@154: /** Encodes an Opus frame from floating point input. cannam@154: * @param [in] st OpusEncoder*: Encoder state cannam@154: * @param [in] pcm float*: Input in float format (interleaved if 2 channels), with a normal range of +/-1.0. cannam@154: * Samples with a range beyond +/-1.0 are supported but will cannam@154: * be clipped by decoders using the integer API and should cannam@154: * only be used if it is known that the far end supports cannam@154: * extended dynamic range. cannam@154: * length is frame_size*channels*sizeof(float) cannam@154: * @param [in] frame_size int: Number of samples per channel in the cannam@154: * input signal. cannam@154: * This must be an Opus frame size for cannam@154: * the encoder's sampling rate. cannam@154: * For example, at 48 kHz the permitted cannam@154: * values are 120, 240, 480, 960, 1920, cannam@154: * and 2880. cannam@154: * Passing in a duration of less than cannam@154: * 10 ms (480 samples at 48 kHz) will cannam@154: * prevent the encoder from using the LPC cannam@154: * or hybrid modes. cannam@154: * @param [out] data unsigned char*: Output payload. cannam@154: * This must contain storage for at cannam@154: * least \a max_data_bytes. cannam@154: * @param [in] max_data_bytes opus_int32: Size of the allocated cannam@154: * memory for the output cannam@154: * payload. This may be cannam@154: * used to impose an upper limit on cannam@154: * the instant bitrate, but should cannam@154: * not be used as the only bitrate cannam@154: * control. Use #OPUS_SET_BITRATE to cannam@154: * control the bitrate. cannam@154: * @returns The length of the encoded packet (in bytes) on success or a cannam@154: * negative error code (see @ref opus_errorcodes) on failure. cannam@154: */ cannam@154: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_encode_float( cannam@154: OpusEncoder *st, cannam@154: const float *pcm, cannam@154: int frame_size, cannam@154: unsigned char *data, cannam@154: opus_int32 max_data_bytes cannam@154: ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4); cannam@154: cannam@154: /** Frees an OpusEncoder allocated by opus_encoder_create(). cannam@154: * @param[in] st OpusEncoder*: State to be freed. cannam@154: */ cannam@154: OPUS_EXPORT void opus_encoder_destroy(OpusEncoder *st); cannam@154: cannam@154: /** Perform a CTL function on an Opus encoder. cannam@154: * cannam@154: * Generally the request and subsequent arguments are generated cannam@154: * by a convenience macro. cannam@154: * @param st OpusEncoder*: Encoder state. cannam@154: * @param request This and all remaining parameters should be replaced by one cannam@154: * of the convenience macros in @ref opus_genericctls or cannam@154: * @ref opus_encoderctls. cannam@154: * @see opus_genericctls cannam@154: * @see opus_encoderctls cannam@154: */ cannam@154: OPUS_EXPORT int opus_encoder_ctl(OpusEncoder *st, int request, ...) OPUS_ARG_NONNULL(1); cannam@154: /**@}*/ cannam@154: cannam@154: /** @defgroup opus_decoder Opus Decoder cannam@154: * @{ cannam@154: * cannam@154: * @brief This page describes the process and functions used to decode Opus. cannam@154: * cannam@154: * The decoding process also starts with creating a decoder cannam@154: * state. This can be done with: cannam@154: * @code cannam@154: * int error; cannam@154: * OpusDecoder *dec; cannam@154: * dec = opus_decoder_create(Fs, channels, &error); cannam@154: * @endcode cannam@154: * where cannam@154: * @li Fs is the sampling rate and must be 8000, 12000, 16000, 24000, or 48000 cannam@154: * @li channels is the number of channels (1 or 2) cannam@154: * @li error will hold the error code in case of failure (or #OPUS_OK on success) cannam@154: * @li the return value is a newly created decoder state to be used for decoding cannam@154: * cannam@154: * While opus_decoder_create() allocates memory for the state, it's also possible cannam@154: * to initialize pre-allocated memory: cannam@154: * @code cannam@154: * int size; cannam@154: * int error; cannam@154: * OpusDecoder *dec; cannam@154: * size = opus_decoder_get_size(channels); cannam@154: * dec = malloc(size); cannam@154: * error = opus_decoder_init(dec, Fs, channels); cannam@154: * @endcode cannam@154: * where opus_decoder_get_size() returns the required size for the decoder state. Note that cannam@154: * future versions of this code may change the size, so no assuptions should be made about it. cannam@154: * cannam@154: * The decoder state is always continuous in memory and only a shallow copy is sufficient cannam@154: * to copy it (e.g. memcpy()) cannam@154: * cannam@154: * To decode a frame, opus_decode() or opus_decode_float() must be called with a packet of compressed audio data: cannam@154: * @code cannam@154: * frame_size = opus_decode(dec, packet, len, decoded, max_size, 0); cannam@154: * @endcode cannam@154: * where cannam@154: * cannam@154: * @li packet is the byte array containing the compressed data cannam@154: * @li len is the exact number of bytes contained in the packet cannam@154: * @li decoded is the decoded audio data in opus_int16 (or float for opus_decode_float()) cannam@154: * @li max_size is the max duration of the frame in samples (per channel) that can fit into the decoded_frame array cannam@154: * cannam@154: * opus_decode() and opus_decode_float() return the number of samples (per channel) decoded from the packet. cannam@154: * If that value is negative, then an error has occurred. This can occur if the packet is corrupted or if the audio cannam@154: * buffer is too small to hold the decoded audio. cannam@154: * cannam@154: * Opus is a stateful codec with overlapping blocks and as a result Opus cannam@154: * packets are not coded independently of each other. Packets must be cannam@154: * passed into the decoder serially and in the correct order for a correct cannam@154: * decode. Lost packets can be replaced with loss concealment by calling cannam@154: * the decoder with a null pointer and zero length for the missing packet. cannam@154: * cannam@154: * A single codec state may only be accessed from a single thread at cannam@154: * a time and any required locking must be performed by the caller. Separate cannam@154: * streams must be decoded with separate decoder states and can be decoded cannam@154: * in parallel unless the library was compiled with NONTHREADSAFE_PSEUDOSTACK cannam@154: * defined. cannam@154: * cannam@154: */ cannam@154: cannam@154: /** Opus decoder state. cannam@154: * This contains the complete state of an Opus decoder. cannam@154: * It is position independent and can be freely copied. cannam@154: * @see opus_decoder_create,opus_decoder_init cannam@154: */ cannam@154: typedef struct OpusDecoder OpusDecoder; cannam@154: cannam@154: /** Gets the size of an OpusDecoder structure. cannam@154: * @param [in] channels int: Number of channels. cannam@154: * This must be 1 or 2. cannam@154: * @returns The size in bytes. cannam@154: */ cannam@154: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decoder_get_size(int channels); cannam@154: cannam@154: /** Allocates and initializes a decoder state. cannam@154: * @param [in] Fs opus_int32: Sample rate to decode at (Hz). cannam@154: * This must be one of 8000, 12000, 16000, cannam@154: * 24000, or 48000. cannam@154: * @param [in] channels int: Number of channels (1 or 2) to decode cannam@154: * @param [out] error int*: #OPUS_OK Success or @ref opus_errorcodes cannam@154: * cannam@154: * Internally Opus stores data at 48000 Hz, so that should be the default cannam@154: * value for Fs. However, the decoder can efficiently decode to buffers cannam@154: * at 8, 12, 16, and 24 kHz so if for some reason the caller cannot use cannam@154: * data at the full sample rate, or knows the compressed data doesn't cannam@154: * use the full frequency range, it can request decoding at a reduced cannam@154: * rate. Likewise, the decoder is capable of filling in either mono or cannam@154: * interleaved stereo pcm buffers, at the caller's request. cannam@154: */ cannam@154: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusDecoder *opus_decoder_create( cannam@154: opus_int32 Fs, cannam@154: int channels, cannam@154: int *error cannam@154: ); cannam@154: cannam@154: /** Initializes a previously allocated decoder state. cannam@154: * The state must be at least the size returned by opus_decoder_get_size(). cannam@154: * This is intended for applications which use their own allocator instead of malloc. @see opus_decoder_create,opus_decoder_get_size cannam@154: * To reset a previously initialized state, use the #OPUS_RESET_STATE CTL. cannam@154: * @param [in] st OpusDecoder*: Decoder state. cannam@154: * @param [in] Fs opus_int32: Sampling rate to decode to (Hz). cannam@154: * This must be one of 8000, 12000, 16000, cannam@154: * 24000, or 48000. cannam@154: * @param [in] channels int: Number of channels (1 or 2) to decode cannam@154: * @retval #OPUS_OK Success or @ref opus_errorcodes cannam@154: */ cannam@154: OPUS_EXPORT int opus_decoder_init( cannam@154: OpusDecoder *st, cannam@154: opus_int32 Fs, cannam@154: int channels cannam@154: ) OPUS_ARG_NONNULL(1); cannam@154: cannam@154: /** Decode an Opus packet. cannam@154: * @param [in] st OpusDecoder*: Decoder state cannam@154: * @param [in] data char*: Input payload. Use a NULL pointer to indicate packet loss cannam@154: * @param [in] len opus_int32: Number of bytes in payload* cannam@154: * @param [out] pcm opus_int16*: Output signal (interleaved if 2 channels). length cannam@154: * is frame_size*channels*sizeof(opus_int16) cannam@154: * @param [in] frame_size Number of samples per channel of available space in \a pcm. cannam@154: * If this is less than the maximum packet duration (120ms; 5760 for 48kHz), this function will cannam@154: * not be capable of decoding some packets. In the case of PLC (data==NULL) or FEC (decode_fec=1), cannam@154: * then frame_size needs to be exactly the duration of audio that is missing, otherwise the cannam@154: * decoder will not be in the optimal state to decode the next incoming packet. For the PLC and cannam@154: * FEC cases, frame_size must be a multiple of 2.5 ms. cannam@154: * @param [in] decode_fec int: Flag (0 or 1) to request that any in-band forward error correction data be cannam@154: * decoded. If no such data is available, the frame is decoded as if it were lost. cannam@154: * @returns Number of decoded samples or @ref opus_errorcodes cannam@154: */ cannam@154: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decode( cannam@154: OpusDecoder *st, cannam@154: const unsigned char *data, cannam@154: opus_int32 len, cannam@154: opus_int16 *pcm, cannam@154: int frame_size, cannam@154: int decode_fec cannam@154: ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4); cannam@154: cannam@154: /** Decode an Opus packet with floating point output. cannam@154: * @param [in] st OpusDecoder*: Decoder state cannam@154: * @param [in] data char*: Input payload. Use a NULL pointer to indicate packet loss cannam@154: * @param [in] len opus_int32: Number of bytes in payload cannam@154: * @param [out] pcm float*: Output signal (interleaved if 2 channels). length cannam@154: * is frame_size*channels*sizeof(float) cannam@154: * @param [in] frame_size Number of samples per channel of available space in \a pcm. cannam@154: * If this is less than the maximum packet duration (120ms; 5760 for 48kHz), this function will cannam@154: * not be capable of decoding some packets. In the case of PLC (data==NULL) or FEC (decode_fec=1), cannam@154: * then frame_size needs to be exactly the duration of audio that is missing, otherwise the cannam@154: * decoder will not be in the optimal state to decode the next incoming packet. For the PLC and cannam@154: * FEC cases, frame_size must be a multiple of 2.5 ms. cannam@154: * @param [in] decode_fec int: Flag (0 or 1) to request that any in-band forward error correction data be cannam@154: * decoded. If no such data is available the frame is decoded as if it were lost. cannam@154: * @returns Number of decoded samples or @ref opus_errorcodes cannam@154: */ cannam@154: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decode_float( cannam@154: OpusDecoder *st, cannam@154: const unsigned char *data, cannam@154: opus_int32 len, cannam@154: float *pcm, cannam@154: int frame_size, cannam@154: int decode_fec cannam@154: ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4); cannam@154: cannam@154: /** Perform a CTL function on an Opus decoder. cannam@154: * cannam@154: * Generally the request and subsequent arguments are generated cannam@154: * by a convenience macro. cannam@154: * @param st OpusDecoder*: Decoder state. cannam@154: * @param request This and all remaining parameters should be replaced by one cannam@154: * of the convenience macros in @ref opus_genericctls or cannam@154: * @ref opus_decoderctls. cannam@154: * @see opus_genericctls cannam@154: * @see opus_decoderctls cannam@154: */ cannam@154: OPUS_EXPORT int opus_decoder_ctl(OpusDecoder *st, int request, ...) OPUS_ARG_NONNULL(1); cannam@154: cannam@154: /** Frees an OpusDecoder allocated by opus_decoder_create(). cannam@154: * @param[in] st OpusDecoder*: State to be freed. cannam@154: */ cannam@154: OPUS_EXPORT void opus_decoder_destroy(OpusDecoder *st); cannam@154: cannam@154: /** Parse an opus packet into one or more frames. cannam@154: * Opus_decode will perform this operation internally so most applications do cannam@154: * not need to use this function. cannam@154: * This function does not copy the frames, the returned pointers are pointers into cannam@154: * the input packet. cannam@154: * @param [in] data char*: Opus packet to be parsed cannam@154: * @param [in] len opus_int32: size of data cannam@154: * @param [out] out_toc char*: TOC pointer cannam@154: * @param [out] frames char*[48] encapsulated frames cannam@154: * @param [out] size opus_int16[48] sizes of the encapsulated frames cannam@154: * @param [out] payload_offset int*: returns the position of the payload within the packet (in bytes) cannam@154: * @returns number of frames cannam@154: */ cannam@154: OPUS_EXPORT int opus_packet_parse( cannam@154: const unsigned char *data, cannam@154: opus_int32 len, cannam@154: unsigned char *out_toc, cannam@154: const unsigned char *frames[48], cannam@154: opus_int16 size[48], cannam@154: int *payload_offset cannam@154: ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(5); cannam@154: cannam@154: /** Gets the bandwidth of an Opus packet. cannam@154: * @param [in] data char*: Opus packet cannam@154: * @retval OPUS_BANDWIDTH_NARROWBAND Narrowband (4kHz bandpass) cannam@154: * @retval OPUS_BANDWIDTH_MEDIUMBAND Mediumband (6kHz bandpass) cannam@154: * @retval OPUS_BANDWIDTH_WIDEBAND Wideband (8kHz bandpass) cannam@154: * @retval OPUS_BANDWIDTH_SUPERWIDEBAND Superwideband (12kHz bandpass) cannam@154: * @retval OPUS_BANDWIDTH_FULLBAND Fullband (20kHz bandpass) cannam@154: * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type cannam@154: */ cannam@154: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_bandwidth(const unsigned char *data) OPUS_ARG_NONNULL(1); cannam@154: cannam@154: /** Gets the number of samples per frame from an Opus packet. cannam@154: * @param [in] data char*: Opus packet. cannam@154: * This must contain at least one byte of cannam@154: * data. cannam@154: * @param [in] Fs opus_int32: Sampling rate in Hz. cannam@154: * This must be a multiple of 400, or cannam@154: * inaccurate results will be returned. cannam@154: * @returns Number of samples per frame. cannam@154: */ cannam@154: 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@154: cannam@154: /** Gets the number of channels from an Opus packet. cannam@154: * @param [in] data char*: Opus packet cannam@154: * @returns Number of channels cannam@154: * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type cannam@154: */ cannam@154: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_channels(const unsigned char *data) OPUS_ARG_NONNULL(1); cannam@154: cannam@154: /** Gets the number of frames in an Opus packet. cannam@154: * @param [in] packet char*: Opus packet cannam@154: * @param [in] len opus_int32: Length of packet cannam@154: * @returns Number of frames cannam@154: * @retval OPUS_BAD_ARG Insufficient data was passed to the function cannam@154: * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type cannam@154: */ cannam@154: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_frames(const unsigned char packet[], opus_int32 len) OPUS_ARG_NONNULL(1); cannam@154: cannam@154: /** Gets the number of samples of an Opus packet. cannam@154: * @param [in] packet char*: Opus packet cannam@154: * @param [in] len opus_int32: Length of packet cannam@154: * @param [in] Fs opus_int32: Sampling rate in Hz. cannam@154: * This must be a multiple of 400, or cannam@154: * inaccurate results will be returned. cannam@154: * @returns Number of samples cannam@154: * @retval OPUS_BAD_ARG Insufficient data was passed to the function cannam@154: * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type cannam@154: */ cannam@154: 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@154: cannam@154: /** Gets the number of samples of an Opus packet. cannam@154: * @param [in] dec OpusDecoder*: Decoder state cannam@154: * @param [in] packet char*: Opus packet cannam@154: * @param [in] len opus_int32: Length of packet cannam@154: * @returns Number of samples cannam@154: * @retval OPUS_BAD_ARG Insufficient data was passed to the function cannam@154: * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type cannam@154: */ cannam@154: 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@154: cannam@154: /** Applies soft-clipping to bring a float signal within the [-1,1] range. If cannam@154: * the signal is already in that range, nothing is done. If there are values cannam@154: * outside of [-1,1], then the signal is clipped as smoothly as possible to cannam@154: * both fit in the range and avoid creating excessive distortion in the cannam@154: * process. cannam@154: * @param [in,out] pcm float*: Input PCM and modified PCM cannam@154: * @param [in] frame_size int Number of samples per channel to process cannam@154: * @param [in] channels int: Number of channels cannam@154: * @param [in,out] softclip_mem float*: State memory for the soft clipping process (one float per channel, initialized to zero) cannam@154: */ cannam@154: OPUS_EXPORT void opus_pcm_soft_clip(float *pcm, int frame_size, int channels, float *softclip_mem); cannam@154: cannam@154: cannam@154: /**@}*/ cannam@154: cannam@154: /** @defgroup opus_repacketizer Repacketizer cannam@154: * @{ cannam@154: * cannam@154: * The repacketizer can be used to merge multiple Opus packets into a single cannam@154: * packet or alternatively to split Opus packets that have previously been cannam@154: * merged. Splitting valid Opus packets is always guaranteed to succeed, cannam@154: * whereas merging valid packets only succeeds if all frames have the same cannam@154: * mode, bandwidth, and frame size, and when the total duration of the merged cannam@154: * packet is no more than 120 ms. The 120 ms limit comes from the cannam@154: * specification and limits decoder memory requirements at a point where cannam@154: * framing overhead becomes negligible. cannam@154: * cannam@154: * The repacketizer currently only operates on elementary Opus cannam@154: * streams. It will not manipualte multistream packets successfully, except in cannam@154: * the degenerate case where they consist of data from a single stream. cannam@154: * cannam@154: * The repacketizing process starts with creating a repacketizer state, either cannam@154: * by calling opus_repacketizer_create() or by allocating the memory yourself, cannam@154: * e.g., cannam@154: * @code cannam@154: * OpusRepacketizer *rp; cannam@154: * rp = (OpusRepacketizer*)malloc(opus_repacketizer_get_size()); cannam@154: * if (rp != NULL) cannam@154: * opus_repacketizer_init(rp); cannam@154: * @endcode cannam@154: * cannam@154: * Then the application should submit packets with opus_repacketizer_cat(), cannam@154: * extract new packets with opus_repacketizer_out() or cannam@154: * opus_repacketizer_out_range(), and then reset the state for the next set of cannam@154: * input packets via opus_repacketizer_init(). cannam@154: * cannam@154: * For example, to split a sequence of packets into individual frames: cannam@154: * @code cannam@154: * unsigned char *data; cannam@154: * int len; cannam@154: * while (get_next_packet(&data, &len)) cannam@154: * { cannam@154: * unsigned char out[1276]; cannam@154: * opus_int32 out_len; cannam@154: * int nb_frames; cannam@154: * int err; cannam@154: * int i; cannam@154: * err = opus_repacketizer_cat(rp, data, len); cannam@154: * if (err != OPUS_OK) cannam@154: * { cannam@154: * release_packet(data); cannam@154: * return err; cannam@154: * } cannam@154: * nb_frames = opus_repacketizer_get_nb_frames(rp); cannam@154: * for (i = 0; i < nb_frames; i++) cannam@154: * { cannam@154: * out_len = opus_repacketizer_out_range(rp, i, i+1, out, sizeof(out)); cannam@154: * if (out_len < 0) cannam@154: * { cannam@154: * release_packet(data); cannam@154: * return (int)out_len; cannam@154: * } cannam@154: * output_next_packet(out, out_len); cannam@154: * } cannam@154: * opus_repacketizer_init(rp); cannam@154: * release_packet(data); cannam@154: * } cannam@154: * @endcode cannam@154: * cannam@154: * Alternatively, to combine a sequence of frames into packets that each cannam@154: * contain up to TARGET_DURATION_MS milliseconds of data: cannam@154: * @code cannam@154: * // The maximum number of packets with duration TARGET_DURATION_MS occurs cannam@154: * // when the frame size is 2.5 ms, for a total of (TARGET_DURATION_MS*2/5) cannam@154: * // packets. cannam@154: * unsigned char *data[(TARGET_DURATION_MS*2/5)+1]; cannam@154: * opus_int32 len[(TARGET_DURATION_MS*2/5)+1]; cannam@154: * int nb_packets; cannam@154: * unsigned char out[1277*(TARGET_DURATION_MS*2/2)]; cannam@154: * opus_int32 out_len; cannam@154: * int prev_toc; cannam@154: * nb_packets = 0; cannam@154: * while (get_next_packet(data+nb_packets, len+nb_packets)) cannam@154: * { cannam@154: * int nb_frames; cannam@154: * int err; cannam@154: * nb_frames = opus_packet_get_nb_frames(data[nb_packets], len[nb_packets]); cannam@154: * if (nb_frames < 1) cannam@154: * { cannam@154: * release_packets(data, nb_packets+1); cannam@154: * return nb_frames; cannam@154: * } cannam@154: * nb_frames += opus_repacketizer_get_nb_frames(rp); cannam@154: * // If adding the next packet would exceed our target, or it has an cannam@154: * // incompatible TOC sequence, output the packets we already have before cannam@154: * // submitting it. cannam@154: * // N.B., The nb_packets > 0 check ensures we've submitted at least one cannam@154: * // packet since the last call to opus_repacketizer_init(). Otherwise a cannam@154: * // single packet longer than TARGET_DURATION_MS would cause us to try to cannam@154: * // output an (invalid) empty packet. It also ensures that prev_toc has cannam@154: * // been set to a valid value. Additionally, len[nb_packets] > 0 is cannam@154: * // guaranteed by the call to opus_packet_get_nb_frames() above, so the cannam@154: * // reference to data[nb_packets][0] should be valid. cannam@154: * if (nb_packets > 0 && ( cannam@154: * ((prev_toc & 0xFC) != (data[nb_packets][0] & 0xFC)) || cannam@154: * opus_packet_get_samples_per_frame(data[nb_packets], 48000)*nb_frames > cannam@154: * TARGET_DURATION_MS*48)) cannam@154: * { cannam@154: * out_len = opus_repacketizer_out(rp, out, sizeof(out)); cannam@154: * if (out_len < 0) cannam@154: * { cannam@154: * release_packets(data, nb_packets+1); cannam@154: * return (int)out_len; cannam@154: * } cannam@154: * output_next_packet(out, out_len); cannam@154: * opus_repacketizer_init(rp); cannam@154: * release_packets(data, nb_packets); cannam@154: * data[0] = data[nb_packets]; cannam@154: * len[0] = len[nb_packets]; cannam@154: * nb_packets = 0; cannam@154: * } cannam@154: * err = opus_repacketizer_cat(rp, data[nb_packets], len[nb_packets]); cannam@154: * if (err != OPUS_OK) cannam@154: * { cannam@154: * release_packets(data, nb_packets+1); cannam@154: * return err; cannam@154: * } cannam@154: * prev_toc = data[nb_packets][0]; cannam@154: * nb_packets++; cannam@154: * } cannam@154: * // Output the final, partial packet. cannam@154: * if (nb_packets > 0) cannam@154: * { cannam@154: * out_len = opus_repacketizer_out(rp, out, sizeof(out)); cannam@154: * release_packets(data, nb_packets); cannam@154: * if (out_len < 0) cannam@154: * return (int)out_len; cannam@154: * output_next_packet(out, out_len); cannam@154: * } cannam@154: * @endcode cannam@154: * cannam@154: * An alternate way of merging packets is to simply call opus_repacketizer_cat() cannam@154: * unconditionally until it fails. At that point, the merged packet can be cannam@154: * obtained with opus_repacketizer_out() and the input packet for which cannam@154: * opus_repacketizer_cat() needs to be re-added to a newly reinitialized cannam@154: * repacketizer state. cannam@154: */ cannam@154: cannam@154: typedef struct OpusRepacketizer OpusRepacketizer; cannam@154: cannam@154: /** Gets the size of an OpusRepacketizer structure. cannam@154: * @returns The size in bytes. cannam@154: */ cannam@154: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_repacketizer_get_size(void); cannam@154: cannam@154: /** (Re)initializes a previously allocated repacketizer state. cannam@154: * The state must be at least the size returned by opus_repacketizer_get_size(). cannam@154: * This can be used for applications which use their own allocator instead of cannam@154: * malloc(). cannam@154: * It must also be called to reset the queue of packets waiting to be cannam@154: * repacketized, which is necessary if the maximum packet duration of 120 ms cannam@154: * is reached or if you wish to submit packets with a different Opus cannam@154: * configuration (coding mode, audio bandwidth, frame size, or channel count). cannam@154: * Failure to do so will prevent a new packet from being added with cannam@154: * opus_repacketizer_cat(). cannam@154: * @see opus_repacketizer_create cannam@154: * @see opus_repacketizer_get_size cannam@154: * @see opus_repacketizer_cat cannam@154: * @param rp OpusRepacketizer*: The repacketizer state to cannam@154: * (re)initialize. cannam@154: * @returns A pointer to the same repacketizer state that was passed in. cannam@154: */ cannam@154: OPUS_EXPORT OpusRepacketizer *opus_repacketizer_init(OpusRepacketizer *rp) OPUS_ARG_NONNULL(1); cannam@154: cannam@154: /** Allocates memory and initializes the new repacketizer with cannam@154: * opus_repacketizer_init(). cannam@154: */ cannam@154: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusRepacketizer *opus_repacketizer_create(void); cannam@154: cannam@154: /** Frees an OpusRepacketizer allocated by cannam@154: * opus_repacketizer_create(). cannam@154: * @param[in] rp OpusRepacketizer*: State to be freed. cannam@154: */ cannam@154: OPUS_EXPORT void opus_repacketizer_destroy(OpusRepacketizer *rp); cannam@154: cannam@154: /** Add a packet to the current repacketizer state. cannam@154: * This packet must match the configuration of any packets already submitted cannam@154: * for repacketization since the last call to opus_repacketizer_init(). cannam@154: * This means that it must have the same coding mode, audio bandwidth, frame cannam@154: * size, and channel count. cannam@154: * This can be checked in advance by examining the top 6 bits of the first cannam@154: * byte of the packet, and ensuring they match the top 6 bits of the first cannam@154: * byte of any previously submitted packet. cannam@154: * The total duration of audio in the repacketizer state also must not exceed cannam@154: * 120 ms, the maximum duration of a single packet, after adding this packet. cannam@154: * cannam@154: * The contents of the current repacketizer state can be extracted into new cannam@154: * packets using opus_repacketizer_out() or opus_repacketizer_out_range(). cannam@154: * cannam@154: * In order to add a packet with a different configuration or to add more cannam@154: * audio beyond 120 ms, you must clear the repacketizer state by calling cannam@154: * opus_repacketizer_init(). cannam@154: * If a packet is too large to add to the current repacketizer state, no part cannam@154: * of it is added, even if it contains multiple frames, some of which might cannam@154: * fit. cannam@154: * If you wish to be able to add parts of such packets, you should first use cannam@154: * another repacketizer to split the packet into pieces and add them cannam@154: * individually. cannam@154: * @see opus_repacketizer_out_range cannam@154: * @see opus_repacketizer_out cannam@154: * @see opus_repacketizer_init cannam@154: * @param rp OpusRepacketizer*: The repacketizer state to which to cannam@154: * add the packet. cannam@154: * @param[in] data const unsigned char*: The packet data. cannam@154: * The application must ensure cannam@154: * this pointer remains valid cannam@154: * until the next call to cannam@154: * opus_repacketizer_init() or cannam@154: * opus_repacketizer_destroy(). cannam@154: * @param len opus_int32: The number of bytes in the packet data. cannam@154: * @returns An error code indicating whether or not the operation succeeded. cannam@154: * @retval #OPUS_OK The packet's contents have been added to the repacketizer cannam@154: * state. cannam@154: * @retval #OPUS_INVALID_PACKET The packet did not have a valid TOC sequence, cannam@154: * the packet's TOC sequence was not compatible cannam@154: * with previously submitted packets (because cannam@154: * the coding mode, audio bandwidth, frame size, cannam@154: * or channel count did not match), or adding cannam@154: * this packet would increase the total amount of cannam@154: * audio stored in the repacketizer state to more cannam@154: * than 120 ms. cannam@154: */ cannam@154: OPUS_EXPORT int opus_repacketizer_cat(OpusRepacketizer *rp, const unsigned char *data, opus_int32 len) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2); cannam@154: cannam@154: cannam@154: /** Construct a new packet from data previously submitted to the repacketizer cannam@154: * state via opus_repacketizer_cat(). cannam@154: * @param rp OpusRepacketizer*: The repacketizer state from which to cannam@154: * construct the new packet. cannam@154: * @param begin int: The index of the first frame in the current cannam@154: * repacketizer state to include in the output. cannam@154: * @param end int: One past the index of the last frame in the cannam@154: * current repacketizer state to include in the cannam@154: * output. cannam@154: * @param[out] data const unsigned char*: The buffer in which to cannam@154: * store the output packet. cannam@154: * @param maxlen opus_int32: The maximum number of bytes to store in cannam@154: * the output buffer. In order to guarantee cannam@154: * success, this should be at least cannam@154: * 1276 for a single frame, cannam@154: * or for multiple frames, cannam@154: * 1277*(end-begin). cannam@154: * However, 1*(end-begin) plus cannam@154: * the size of all packet data submitted to cannam@154: * the repacketizer since the last call to cannam@154: * opus_repacketizer_init() or cannam@154: * opus_repacketizer_create() is also cannam@154: * sufficient, and possibly much smaller. cannam@154: * @returns The total size of the output packet on success, or an error code cannam@154: * on failure. cannam@154: * @retval #OPUS_BAD_ARG [begin,end) was an invalid range of cannam@154: * frames (begin < 0, begin >= end, or end > cannam@154: * opus_repacketizer_get_nb_frames()). cannam@154: * @retval #OPUS_BUFFER_TOO_SMALL \a maxlen was insufficient to contain the cannam@154: * complete output packet. cannam@154: */ cannam@154: 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@154: cannam@154: /** Return the total number of frames contained in packet data submitted to cannam@154: * the repacketizer state so far via opus_repacketizer_cat() since the last cannam@154: * call to opus_repacketizer_init() or opus_repacketizer_create(). cannam@154: * This defines the valid range of packets that can be extracted with cannam@154: * opus_repacketizer_out_range() or opus_repacketizer_out(). cannam@154: * @param rp OpusRepacketizer*: The repacketizer state containing the cannam@154: * frames. cannam@154: * @returns The total number of frames contained in the packet data submitted cannam@154: * to the repacketizer state. cannam@154: */ cannam@154: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_repacketizer_get_nb_frames(OpusRepacketizer *rp) OPUS_ARG_NONNULL(1); cannam@154: cannam@154: /** Construct a new packet from data previously submitted to the repacketizer cannam@154: * state via opus_repacketizer_cat(). cannam@154: * This is a convenience routine that returns all the data submitted so far cannam@154: * in a single packet. cannam@154: * It is equivalent to calling cannam@154: * @code cannam@154: * opus_repacketizer_out_range(rp, 0, opus_repacketizer_get_nb_frames(rp), cannam@154: * data, maxlen) cannam@154: * @endcode cannam@154: * @param rp OpusRepacketizer*: The repacketizer state from which to cannam@154: * construct the new packet. cannam@154: * @param[out] data const unsigned char*: The buffer in which to cannam@154: * store the output packet. cannam@154: * @param maxlen opus_int32: The maximum number of bytes to store in cannam@154: * the output buffer. In order to guarantee cannam@154: * success, this should be at least cannam@154: * 1277*opus_repacketizer_get_nb_frames(rp). cannam@154: * However, cannam@154: * 1*opus_repacketizer_get_nb_frames(rp) cannam@154: * plus the size of all packet data cannam@154: * submitted to the repacketizer since the cannam@154: * last call to opus_repacketizer_init() or cannam@154: * opus_repacketizer_create() is also cannam@154: * sufficient, and possibly much smaller. cannam@154: * @returns The total size of the output packet on success, or an error code cannam@154: * on failure. cannam@154: * @retval #OPUS_BUFFER_TOO_SMALL \a maxlen was insufficient to contain the cannam@154: * complete output packet. cannam@154: */ cannam@154: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_repacketizer_out(OpusRepacketizer *rp, unsigned char *data, opus_int32 maxlen) OPUS_ARG_NONNULL(1); cannam@154: cannam@154: /** Pads a given Opus packet to a larger size (possibly changing the TOC sequence). cannam@154: * @param[in,out] data const unsigned char*: The buffer containing the cannam@154: * packet to pad. cannam@154: * @param len opus_int32: The size of the packet. cannam@154: * This must be at least 1. cannam@154: * @param new_len opus_int32: The desired size of the packet after padding. cannam@154: * This must be at least as large as len. cannam@154: * @returns an error code cannam@154: * @retval #OPUS_OK \a on success. cannam@154: * @retval #OPUS_BAD_ARG \a len was less than 1 or new_len was less than len. cannam@154: * @retval #OPUS_INVALID_PACKET \a data did not contain a valid Opus packet. cannam@154: */ cannam@154: OPUS_EXPORT int opus_packet_pad(unsigned char *data, opus_int32 len, opus_int32 new_len); cannam@154: cannam@154: /** Remove all padding from a given Opus packet and rewrite the TOC sequence to cannam@154: * minimize space usage. cannam@154: * @param[in,out] data const unsigned char*: The buffer containing the cannam@154: * packet to strip. cannam@154: * @param len opus_int32: The size of the packet. cannam@154: * This must be at least 1. cannam@154: * @returns The new size of the output packet on success, or an error code cannam@154: * on failure. cannam@154: * @retval #OPUS_BAD_ARG \a len was less than 1. cannam@154: * @retval #OPUS_INVALID_PACKET \a data did not contain a valid Opus packet. cannam@154: */ cannam@154: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_packet_unpad(unsigned char *data, opus_int32 len); cannam@154: cannam@154: /** Pads a given Opus multi-stream packet to a larger size (possibly changing the TOC sequence). cannam@154: * @param[in,out] data const unsigned char*: The buffer containing the cannam@154: * packet to pad. cannam@154: * @param len opus_int32: The size of the packet. cannam@154: * This must be at least 1. cannam@154: * @param new_len opus_int32: The desired size of the packet after padding. cannam@154: * This must be at least 1. cannam@154: * @param nb_streams opus_int32: The number of streams (not channels) in the packet. cannam@154: * This must be at least as large as len. cannam@154: * @returns an error code cannam@154: * @retval #OPUS_OK \a on success. cannam@154: * @retval #OPUS_BAD_ARG \a len was less than 1. cannam@154: * @retval #OPUS_INVALID_PACKET \a data did not contain a valid Opus packet. cannam@154: */ cannam@154: OPUS_EXPORT int opus_multistream_packet_pad(unsigned char *data, opus_int32 len, opus_int32 new_len, int nb_streams); cannam@154: cannam@154: /** Remove all padding from a given Opus multi-stream packet and rewrite the TOC sequence to cannam@154: * minimize space usage. cannam@154: * @param[in,out] data const unsigned char*: The buffer containing the cannam@154: * packet to strip. cannam@154: * @param len opus_int32: The size of the packet. cannam@154: * This must be at least 1. cannam@154: * @param nb_streams opus_int32: The number of streams (not channels) in the packet. cannam@154: * This must be at least 1. cannam@154: * @returns The new size of the output packet on success, or an error code cannam@154: * on failure. cannam@154: * @retval #OPUS_BAD_ARG \a len was less than 1 or new_len was less than len. cannam@154: * @retval #OPUS_INVALID_PACKET \a data did not contain a valid Opus packet. cannam@154: */ cannam@154: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_multistream_packet_unpad(unsigned char *data, opus_int32 len, int nb_streams); cannam@154: cannam@154: /**@}*/ cannam@154: cannam@154: #ifdef __cplusplus cannam@154: } cannam@154: #endif cannam@154: cannam@154: #endif /* OPUS_H */