tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

opus_custom.h (16965B)


      1 /* Copyright (c) 2007-2008 CSIRO
      2   Copyright (c) 2007-2009 Xiph.Org Foundation
      3   Copyright (c) 2008-2012 Gregory Maxwell
      4   Written by Jean-Marc Valin and Gregory Maxwell */
      5 /*
      6   Redistribution and use in source and binary forms, with or without
      7   modification, are permitted provided that the following conditions
      8   are met:
      9 
     10   - Redistributions of source code must retain the above copyright
     11   notice, this list of conditions and the following disclaimer.
     12 
     13   - Redistributions in binary form must reproduce the above copyright
     14   notice, this list of conditions and the following disclaimer in the
     15   documentation and/or other materials provided with the distribution.
     16 
     17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     19   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     20   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
     21   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     22   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     23   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     24   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     25   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     26   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     27   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 */
     29 
     30 /**
     31  @file opus_custom.h
     32  @brief Opus-Custom reference implementation API
     33 */
     34 
     35 #ifndef OPUS_CUSTOM_H
     36 #define OPUS_CUSTOM_H
     37 
     38 #include "opus_defines.h"
     39 
     40 #ifdef __cplusplus
     41 extern "C" {
     42 #endif
     43 
     44 #if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
     45 # define OPUS_CUSTOM_EXPORT OPUS_EXPORT
     46 # define OPUS_CUSTOM_EXPORT_STATIC OPUS_EXPORT
     47 #else
     48 # define OPUS_CUSTOM_EXPORT
     49 # ifdef OPUS_BUILD
     50 #  define OPUS_CUSTOM_EXPORT_STATIC static OPUS_INLINE
     51 # else
     52 #  define OPUS_CUSTOM_EXPORT_STATIC
     53 # endif
     54 #endif
     55 
     56 /** @defgroup opus_custom Opus Custom
     57  * @{
     58  *  Opus Custom is an optional part of the Opus specification and
     59  * reference implementation which uses a distinct API from the regular
     60  * API and supports frame sizes that are not normally supported.\ Use
     61  * of Opus Custom is discouraged for all but very special applications
     62  * for which a frame size different from 2.5, 5, 10, or 20 ms is needed
     63  * (for either complexity or latency reasons) and where interoperability
     64  * is less important.
     65  *
     66  * In addition to the interoperability limitations the use of Opus custom
     67  * disables a substantial chunk of the codec and generally lowers the
     68  * quality available at a given bitrate. Normally when an application needs
     69  * a different frame size from the codec it should buffer to match the
     70  * sizes but this adds a small amount of delay which may be important
     71  * in some very low latency applications. Some transports (especially
     72  * constant rate RF transports) may also work best with frames of
     73  * particular durations.
     74  *
     75  * Libopus only supports custom modes if they are enabled at compile time.
     76  *
     77  * The Opus Custom API is similar to the regular API but the
     78  * @ref opus_encoder_create and @ref opus_decoder_create calls take
     79  * an additional mode parameter which is a structure produced by
     80  * a call to @ref opus_custom_mode_create. Both the encoder and decoder
     81  * must create a mode using the same sample rate (fs) and frame size
     82  * (frame size) so these parameters must either be signaled out of band
     83  * or fixed in a particular implementation.
     84  *
     85  * Similar to regular Opus the custom modes support on the fly frame size
     86  * switching, but the sizes available depend on the particular frame size in
     87  * use. For some initial frame sizes on a single on the fly size is available.
     88  */
     89 
     90 /** Contains the state of an encoder. One encoder state is needed
     91    for each stream. It is initialized once at the beginning of the
     92    stream. Do *not* re-initialize the state for every frame.
     93   @brief Encoder state
     94 */
     95 typedef struct OpusCustomEncoder OpusCustomEncoder;
     96 
     97 /** State of the decoder. One decoder state is needed for each stream.
     98    It is initialized once at the beginning of the stream. Do *not*
     99    re-initialize the state for every frame.
    100   @brief Decoder state
    101 */
    102 typedef struct OpusCustomDecoder OpusCustomDecoder;
    103 
    104 /** The mode contains all the information necessary to create an
    105    encoder. Both the encoder and decoder need to be initialized
    106    with exactly the same mode, otherwise the output will be
    107    corrupted. The mode MUST NOT BE DESTROYED until the encoders and
    108    decoders that use it are destroyed as well.
    109   @brief Mode configuration
    110 */
    111 typedef struct OpusCustomMode OpusCustomMode;
    112 
    113 /** Creates a new mode struct. This will be passed to an encoder or
    114  * decoder. The mode MUST NOT BE DESTROYED until the encoders and
    115  * decoders that use it are destroyed as well.
    116  * @param [in] Fs <tt>int</tt>: Sampling rate (8000 to 96000 Hz)
    117  * @param [in] frame_size <tt>int</tt>: Number of samples (per channel) to encode in each
    118  *        packet (64 - 1024, prime factorization must contain zero or more 2s, 3s, or 5s and no other primes)
    119  * @param [out] error <tt>int*</tt>: Returned error code (if NULL, no error will be returned)
    120  * @return A newly created mode
    121  */
    122 OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT OpusCustomMode *opus_custom_mode_create(opus_int32 Fs, int frame_size, int *error);
    123 
    124 /** Destroys a mode struct. Only call this after all encoders and
    125  * decoders using this mode are destroyed as well.
    126  * @param [in] mode <tt>OpusCustomMode*</tt>: Mode to be freed.
    127  */
    128 OPUS_CUSTOM_EXPORT void opus_custom_mode_destroy(OpusCustomMode *mode);
    129 
    130 
    131 #if !defined(OPUS_BUILD) || defined(CELT_ENCODER_C)
    132 
    133 /* Encoder */
    134 /** Gets the size of an OpusCustomEncoder structure.
    135  * @param [in] mode <tt>OpusCustomMode *</tt>: Mode configuration
    136  * @param [in] channels <tt>int</tt>: Number of channels
    137  * @returns size
    138  */
    139 OPUS_CUSTOM_EXPORT_STATIC OPUS_WARN_UNUSED_RESULT int opus_custom_encoder_get_size(
    140    const OpusCustomMode *mode,
    141    int channels
    142 ) OPUS_ARG_NONNULL(1);
    143 
    144 #if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
    145 /** Initializes a previously allocated encoder state
    146  * The memory pointed to by st must be the size returned by opus_custom_encoder_get_size.
    147  * This is intended for applications which use their own allocator instead of malloc.
    148  * @see opus_custom_encoder_create(),opus_custom_encoder_get_size()
    149  * To reset a previously initialized state use the OPUS_RESET_STATE CTL.
    150  * @param [in] st <tt>OpusCustomEncoder*</tt>: Encoder state
    151  * @param [in] mode <tt>OpusCustomMode *</tt>: Contains all the information about the characteristics of
    152  *  the stream (must be the same characteristics as used for the
    153  *  decoder)
    154  * @param [in] channels <tt>int</tt>: Number of channels
    155  * @return OPUS_OK Success or @ref opus_errorcodes
    156  */
    157 OPUS_CUSTOM_EXPORT int opus_custom_encoder_init(
    158    OpusCustomEncoder *st,
    159    const OpusCustomMode *mode,
    160    int channels
    161 ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2);
    162 # endif
    163 #endif
    164 
    165 
    166 /** Creates a new encoder state. Each stream needs its own encoder
    167  * state (can't be shared across simultaneous streams).
    168  * @param [in] mode <tt>OpusCustomMode*</tt>: Contains all the information about the characteristics of
    169  *  the stream (must be the same characteristics as used for the
    170  *  decoder)
    171  * @param [in] channels <tt>int</tt>: Number of channels
    172  * @param [out] error <tt>int*</tt>: Returns an error code
    173  * @return Newly created encoder state.
    174 */
    175 OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT OpusCustomEncoder *opus_custom_encoder_create(
    176    const OpusCustomMode *mode,
    177    int channels,
    178    int *error
    179 ) OPUS_ARG_NONNULL(1);
    180 
    181 
    182 /** Destroys an encoder state.
    183  * @param[in] st <tt>OpusCustomEncoder*</tt>: State to be freed.
    184  */
    185 OPUS_CUSTOM_EXPORT void opus_custom_encoder_destroy(OpusCustomEncoder *st);
    186 
    187 /** Encodes a frame of audio.
    188  * @param [in] st <tt>OpusCustomEncoder*</tt>: Encoder state
    189  * @param [in] pcm <tt>float*</tt>: PCM audio in float format, with a normal range of +/-1.0.
    190  *          Samples with a range beyond +/-1.0 are supported but will
    191  *          be clipped by decoders using the integer API and should
    192  *          only be used if it is known that the far end supports
    193  *          extended dynamic range. There must be exactly
    194  *          frame_size samples per channel.
    195  * @param [in] frame_size <tt>int</tt>: Number of samples per frame of input signal
    196  * @param [out] compressed <tt>char *</tt>: The compressed data is written here. This may not alias pcm and must be at least maxCompressedBytes long.
    197  * @param [in] maxCompressedBytes <tt>int</tt>: Maximum number of bytes to use for compressing the frame
    198  *          (can change from one frame to another)
    199  * @return Number of bytes written to "compressed".
    200  *       If negative, an error has occurred (see error codes). It is IMPORTANT that
    201  *       the length returned be somehow transmitted to the decoder. Otherwise, no
    202  *       decoding is possible.
    203  */
    204 OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT int opus_custom_encode_float(
    205    OpusCustomEncoder *st,
    206    const float *pcm,
    207    int frame_size,
    208    unsigned char *compressed,
    209    int maxCompressedBytes
    210 ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4);
    211 
    212 /** Encodes a frame of audio.
    213  * @param [in] st <tt>OpusCustomEncoder*</tt>: Encoder state
    214  * @param [in] pcm <tt>opus_int16*</tt>: PCM audio in signed 16-bit format (native endian).
    215  *          There must be exactly frame_size samples per channel.
    216  * @param [in] frame_size <tt>int</tt>: Number of samples per frame of input signal
    217  * @param [out] compressed <tt>char *</tt>: The compressed data is written here. This may not alias pcm and must be at least maxCompressedBytes long.
    218  * @param [in] maxCompressedBytes <tt>int</tt>: Maximum number of bytes to use for compressing the frame
    219  *          (can change from one frame to another)
    220  * @return Number of bytes written to "compressed".
    221  *       If negative, an error has occurred (see error codes). It is IMPORTANT that
    222  *       the length returned be somehow transmitted to the decoder. Otherwise, no
    223  *       decoding is possible.
    224 */
    225 OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT int opus_custom_encode(
    226    OpusCustomEncoder *st,
    227    const opus_int16 *pcm,
    228    int frame_size,
    229    unsigned char *compressed,
    230    int maxCompressedBytes
    231 ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4);
    232 
    233 /** Encodes a frame of audio.
    234  * @param [in] st <tt>OpusCustomEncoder*</tt>: Encoder state
    235  * @param [in] pcm <tt>opus_int32*</tt>: PCM audio in signed 32-bit format (native endian) representing (or slightly exceeding) 24-bit values.
    236  *          There must be exactly frame_size samples per channel.
    237  * @param [in] frame_size <tt>int</tt>: Number of samples per frame of input signal
    238  * @param [out] compressed <tt>char *</tt>: The compressed data is written here. This may not alias pcm and must be at least maxCompressedBytes long.
    239  * @param [in] maxCompressedBytes <tt>int</tt>: Maximum number of bytes to use for compressing the frame
    240  *          (can change from one frame to another)
    241  * @return Number of bytes written to "compressed".
    242  *       If negative, an error has occurred (see error codes). It is IMPORTANT that
    243  *       the length returned be somehow transmitted to the decoder. Otherwise, no
    244  *       decoding is possible.
    245 */
    246 OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT int opus_custom_encode24(
    247    OpusCustomEncoder *st,
    248    const opus_int32 *pcm,
    249    int frame_size,
    250    unsigned char *compressed,
    251    int maxCompressedBytes
    252 ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4);
    253 
    254 /** Perform a CTL function on an Opus custom encoder.
    255  *
    256  * Generally the request and subsequent arguments are generated
    257  * by a convenience macro.
    258  * @see opus_encoderctls
    259  */
    260 OPUS_CUSTOM_EXPORT int opus_custom_encoder_ctl(OpusCustomEncoder * OPUS_RESTRICT st, int request, ...) OPUS_ARG_NONNULL(1);
    261 
    262 
    263 #if !defined(OPUS_BUILD) || defined(CELT_DECODER_C)
    264 /* Decoder */
    265 
    266 /** Gets the size of an OpusCustomDecoder structure.
    267  * @param [in] mode <tt>OpusCustomMode *</tt>: Mode configuration
    268  * @param [in] channels <tt>int</tt>: Number of channels
    269  * @returns size
    270  */
    271 OPUS_CUSTOM_EXPORT_STATIC OPUS_WARN_UNUSED_RESULT int opus_custom_decoder_get_size(
    272    const OpusCustomMode *mode,
    273    int channels
    274 ) OPUS_ARG_NONNULL(1);
    275 
    276 /** Initializes a previously allocated decoder state
    277  * The memory pointed to by st must be the size returned by opus_custom_decoder_get_size.
    278  * This is intended for applications which use their own allocator instead of malloc.
    279  * @see opus_custom_decoder_create(),opus_custom_decoder_get_size()
    280  * To reset a previously initialized state use the OPUS_RESET_STATE CTL.
    281  * @param [in] st <tt>OpusCustomDecoder*</tt>: Decoder state
    282  * @param [in] mode <tt>OpusCustomMode *</tt>: Contains all the information about the characteristics of
    283  *  the stream (must be the same characteristics as used for the
    284  *  encoder)
    285  * @param [in] channels <tt>int</tt>: Number of channels
    286  * @return OPUS_OK Success or @ref opus_errorcodes
    287  */
    288 OPUS_CUSTOM_EXPORT_STATIC int opus_custom_decoder_init(
    289    OpusCustomDecoder *st,
    290    const OpusCustomMode *mode,
    291    int channels
    292 ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2);
    293 
    294 #endif
    295 
    296 
    297 /** Creates a new decoder state. Each stream needs its own decoder state (can't
    298  * be shared across simultaneous streams).
    299  * @param [in] mode <tt>OpusCustomMode</tt>: Contains all the information about the characteristics of the
    300  *          stream (must be the same characteristics as used for the encoder)
    301  * @param [in] channels <tt>int</tt>: Number of channels
    302  * @param [out] error <tt>int*</tt>: Returns an error code
    303  * @return Newly created decoder state.
    304  */
    305 OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT OpusCustomDecoder *opus_custom_decoder_create(
    306    const OpusCustomMode *mode,
    307    int channels,
    308    int *error
    309 ) OPUS_ARG_NONNULL(1);
    310 
    311 /** Destroys a decoder state.
    312  * @param[in] st <tt>OpusCustomDecoder*</tt>: State to be freed.
    313  */
    314 OPUS_CUSTOM_EXPORT void opus_custom_decoder_destroy(OpusCustomDecoder *st);
    315 
    316 /** Decode an opus custom frame with floating point output
    317  * @param [in] st <tt>OpusCustomDecoder*</tt>: Decoder state
    318  * @param [in] data <tt>char*</tt>: Input payload. Use a NULL pointer to indicate packet loss
    319  * @param [in] len <tt>int</tt>: Number of bytes in payload
    320  * @param [out] pcm <tt>float*</tt>: Output signal (interleaved if 2 channels). length
    321  *  is frame_size*channels*sizeof(float)
    322  * @param [in] frame_size Number of samples per channel of available space in *pcm.
    323  * @returns Number of decoded samples or @ref opus_errorcodes
    324  */
    325 OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT int opus_custom_decode_float(
    326    OpusCustomDecoder *st,
    327    const unsigned char *data,
    328    int len,
    329    float *pcm,
    330    int frame_size
    331 ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);
    332 
    333 /** Decode an opus custom frame
    334  * @param [in] st <tt>OpusCustomDecoder*</tt>: Decoder state
    335  * @param [in] data <tt>char*</tt>: Input payload. Use a NULL pointer to indicate packet loss
    336  * @param [in] len <tt>int</tt>: Number of bytes in payload
    337  * @param [out] pcm <tt>opus_int16*</tt>: Output signal (interleaved if 2 channels). length
    338  *  is frame_size*channels*sizeof(opus_int16)
    339  * @param [in] frame_size Number of samples per channel of available space in *pcm.
    340  * @returns Number of decoded samples or @ref opus_errorcodes
    341  */
    342 OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT int opus_custom_decode(
    343    OpusCustomDecoder *st,
    344    const unsigned char *data,
    345    int len,
    346    opus_int16 *pcm,
    347    int frame_size
    348 ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);
    349 
    350 /** Decode an opus custom frame
    351  * @param [in] st <tt>OpusCustomDecoder*</tt>: Decoder state
    352  * @param [in] data <tt>char*</tt>: Input payload. Use a NULL pointer to indicate packet loss
    353  * @param [in] len <tt>int</tt>: Number of bytes in payload
    354  * @param [out] pcm <tt>opus_int32*</tt>: Output signal (interleaved if 2 channels) representing (or slightly exceeding) 24-bit values. length
    355  *  is frame_size*channels*sizeof(opus_int32)
    356  * @param [in] frame_size Number of samples per channel of available space in *pcm.
    357  * @returns Number of decoded samples or @ref opus_errorcodes
    358  */
    359 OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT int opus_custom_decode24(
    360    OpusCustomDecoder *st,
    361    const unsigned char *data,
    362    int len,
    363    opus_int32 *pcm,
    364    int frame_size
    365 ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);
    366 
    367 /** Perform a CTL function on an Opus custom decoder.
    368  *
    369  * Generally the request and subsequent arguments are generated
    370  * by a convenience macro.
    371  * @see opus_genericctls
    372  */
    373 OPUS_CUSTOM_EXPORT int opus_custom_decoder_ctl(OpusCustomDecoder * OPUS_RESTRICT st, int request, ...) OPUS_ARG_NONNULL(1);
    374 
    375 /**@}*/
    376 
    377 #ifdef __cplusplus
    378 }
    379 #endif
    380 
    381 #endif /* OPUS_CUSTOM_H */