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