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