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