michael@0: /* Copyright (c) 2007-2008 CSIRO michael@0: Copyright (c) 2007-2009 Xiph.Org Foundation michael@0: Copyright (c) 2008-2012 Gregory Maxwell michael@0: Written by Jean-Marc Valin and Gregory Maxwell */ michael@0: /* michael@0: Redistribution and use in source and binary forms, with or without michael@0: modification, are permitted provided that the following conditions michael@0: are met: michael@0: michael@0: - Redistributions of source code must retain the above copyright michael@0: notice, this list of conditions and the following disclaimer. michael@0: michael@0: - Redistributions in binary form must reproduce the above copyright michael@0: notice, this list of conditions and the following disclaimer in the michael@0: documentation and/or other materials provided with the distribution. michael@0: michael@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER michael@0: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF michael@0: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING michael@0: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: /** michael@0: @file opus_custom.h michael@0: @brief Opus-Custom reference implementation API michael@0: */ michael@0: michael@0: #ifndef OPUS_CUSTOM_H michael@0: #define OPUS_CUSTOM_H michael@0: michael@0: #include "opus_defines.h" michael@0: michael@0: #ifdef __cplusplus michael@0: extern "C" { michael@0: #endif michael@0: michael@0: #ifdef CUSTOM_MODES michael@0: # define OPUS_CUSTOM_EXPORT OPUS_EXPORT michael@0: # define OPUS_CUSTOM_EXPORT_STATIC OPUS_EXPORT michael@0: #else michael@0: # define OPUS_CUSTOM_EXPORT michael@0: # ifdef OPUS_BUILD michael@0: # define OPUS_CUSTOM_EXPORT_STATIC static OPUS_INLINE michael@0: # else michael@0: # define OPUS_CUSTOM_EXPORT_STATIC michael@0: # endif michael@0: #endif michael@0: michael@0: /** @defgroup opus_custom Opus Custom michael@0: * @{ michael@0: * Opus Custom is an optional part of the Opus specification and michael@0: * reference implementation which uses a distinct API from the regular michael@0: * API and supports frame sizes that are not normally supported.\ Use michael@0: * of Opus Custom is discouraged for all but very special applications michael@0: * for which a frame size different from 2.5, 5, 10, or 20 ms is needed michael@0: * (for either complexity or latency reasons) and where interoperability michael@0: * is less important. michael@0: * michael@0: * In addition to the interoperability limitations the use of Opus custom michael@0: * disables a substantial chunk of the codec and generally lowers the michael@0: * quality available at a given bitrate. Normally when an application needs michael@0: * a different frame size from the codec it should buffer to match the michael@0: * sizes but this adds a small amount of delay which may be important michael@0: * in some very low latency applications. Some transports (especially michael@0: * constant rate RF transports) may also work best with frames of michael@0: * particular durations. michael@0: * michael@0: * Libopus only supports custom modes if they are enabled at compile time. michael@0: * michael@0: * The Opus Custom API is similar to the regular API but the michael@0: * @ref opus_encoder_create and @ref opus_decoder_create calls take michael@0: * an additional mode parameter which is a structure produced by michael@0: * a call to @ref opus_custom_mode_create. Both the encoder and decoder michael@0: * must create a mode using the same sample rate (fs) and frame size michael@0: * (frame size) so these parameters must either be signaled out of band michael@0: * or fixed in a particular implementation. michael@0: * michael@0: * Similar to regular Opus the custom modes support on the fly frame size michael@0: * switching, but the sizes available depend on the particular frame size in michael@0: * use. For some initial frame sizes on a single on the fly size is available. michael@0: */ michael@0: michael@0: /** Contains the state of an encoder. One encoder state is needed michael@0: for each stream. It is initialized once at the beginning of the michael@0: stream. Do *not* re-initialize the state for every frame. michael@0: @brief Encoder state michael@0: */ michael@0: typedef struct OpusCustomEncoder OpusCustomEncoder; michael@0: michael@0: /** State of the decoder. One decoder state is needed for each stream. michael@0: It is initialized once at the beginning of the stream. Do *not* michael@0: re-initialize the state for every frame. michael@0: @brief Decoder state michael@0: */ michael@0: typedef struct OpusCustomDecoder OpusCustomDecoder; michael@0: michael@0: /** The mode contains all the information necessary to create an michael@0: encoder. Both the encoder and decoder need to be initialized michael@0: with exactly the same mode, otherwise the output will be michael@0: corrupted. michael@0: @brief Mode configuration michael@0: */ michael@0: typedef struct OpusCustomMode OpusCustomMode; michael@0: michael@0: /** Creates a new mode struct. This will be passed to an encoder or michael@0: * decoder. The mode MUST NOT BE DESTROYED until the encoders and michael@0: * decoders that use it are destroyed as well. michael@0: * @param [in] Fs int: Sampling rate (8000 to 96000 Hz) michael@0: * @param [in] frame_size int: Number of samples (per channel) to encode in each michael@0: * packet (64 - 1024, prime factorization must contain zero or more 2s, 3s, or 5s and no other primes) michael@0: * @param [out] error int*: Returned error code (if NULL, no error will be returned) michael@0: * @return A newly created mode michael@0: */ michael@0: OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT OpusCustomMode *opus_custom_mode_create(opus_int32 Fs, int frame_size, int *error); michael@0: michael@0: /** Destroys a mode struct. Only call this after all encoders and michael@0: * decoders using this mode are destroyed as well. michael@0: * @param [in] mode OpusCustomMode*: Mode to be freed. michael@0: */ michael@0: OPUS_CUSTOM_EXPORT void opus_custom_mode_destroy(OpusCustomMode *mode); michael@0: michael@0: michael@0: #if !defined(OPUS_BUILD) || defined(CELT_ENCODER_C) michael@0: michael@0: /* Encoder */ michael@0: /** Gets the size of an OpusCustomEncoder structure. michael@0: * @param [in] mode OpusCustomMode *: Mode configuration michael@0: * @param [in] channels int: Number of channels michael@0: * @returns size michael@0: */ michael@0: OPUS_CUSTOM_EXPORT_STATIC OPUS_WARN_UNUSED_RESULT int opus_custom_encoder_get_size( michael@0: const OpusCustomMode *mode, michael@0: int channels michael@0: ) OPUS_ARG_NONNULL(1); michael@0: michael@0: # ifdef CUSTOM_MODES michael@0: /** Initializes a previously allocated encoder state michael@0: * The memory pointed to by st must be the size returned by opus_custom_encoder_get_size. michael@0: * This is intended for applications which use their own allocator instead of malloc. michael@0: * @see opus_custom_encoder_create(),opus_custom_encoder_get_size() michael@0: * To reset a previously initialized state use the OPUS_RESET_STATE CTL. michael@0: * @param [in] st OpusCustomEncoder*: Encoder state michael@0: * @param [in] mode OpusCustomMode *: Contains all the information about the characteristics of michael@0: * the stream (must be the same characteristics as used for the michael@0: * decoder) michael@0: * @param [in] channels int: Number of channels michael@0: * @return OPUS_OK Success or @ref opus_errorcodes michael@0: */ michael@0: OPUS_CUSTOM_EXPORT int opus_custom_encoder_init( michael@0: OpusCustomEncoder *st, michael@0: const OpusCustomMode *mode, michael@0: int channels michael@0: ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2); michael@0: # endif michael@0: #endif michael@0: michael@0: michael@0: /** Creates a new encoder state. Each stream needs its own encoder michael@0: * state (can't be shared across simultaneous streams). michael@0: * @param [in] mode OpusCustomMode*: Contains all the information about the characteristics of michael@0: * the stream (must be the same characteristics as used for the michael@0: * decoder) michael@0: * @param [in] channels int: Number of channels michael@0: * @param [out] error int*: Returns an error code michael@0: * @return Newly created encoder state. michael@0: */ michael@0: OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT OpusCustomEncoder *opus_custom_encoder_create( michael@0: const OpusCustomMode *mode, michael@0: int channels, michael@0: int *error michael@0: ) OPUS_ARG_NONNULL(1); michael@0: michael@0: michael@0: /** Destroys a an encoder state. michael@0: * @param[in] st OpusCustomEncoder*: State to be freed. michael@0: */ michael@0: OPUS_CUSTOM_EXPORT void opus_custom_encoder_destroy(OpusCustomEncoder *st); michael@0: michael@0: /** Encodes a frame of audio. michael@0: * @param [in] st OpusCustomEncoder*: Encoder state michael@0: * @param [in] pcm float*: PCM audio in float format, with a normal range of +/-1.0. michael@0: * Samples with a range beyond +/-1.0 are supported but will michael@0: * be clipped by decoders using the integer API and should michael@0: * only be used if it is known that the far end supports michael@0: * extended dynamic range. There must be exactly michael@0: * frame_size samples per channel. michael@0: * @param [in] frame_size int: Number of samples per frame of input signal michael@0: * @param [out] compressed char *: The compressed data is written here. This may not alias pcm and must be at least maxCompressedBytes long. michael@0: * @param [in] maxCompressedBytes int: Maximum number of bytes to use for compressing the frame michael@0: * (can change from one frame to another) michael@0: * @return Number of bytes written to "compressed". michael@0: * If negative, an error has occurred (see error codes). It is IMPORTANT that michael@0: * the length returned be somehow transmitted to the decoder. Otherwise, no michael@0: * decoding is possible. michael@0: */ michael@0: OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT int opus_custom_encode_float( michael@0: OpusCustomEncoder *st, michael@0: const float *pcm, michael@0: int frame_size, michael@0: unsigned char *compressed, michael@0: int maxCompressedBytes michael@0: ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4); michael@0: michael@0: /** Encodes a frame of audio. michael@0: * @param [in] st OpusCustomEncoder*: Encoder state michael@0: * @param [in] pcm opus_int16*: PCM audio in signed 16-bit format (native endian). michael@0: * There must be exactly frame_size samples per channel. michael@0: * @param [in] frame_size int: Number of samples per frame of input signal michael@0: * @param [out] compressed char *: The compressed data is written here. This may not alias pcm and must be at least maxCompressedBytes long. michael@0: * @param [in] maxCompressedBytes int: Maximum number of bytes to use for compressing the frame michael@0: * (can change from one frame to another) michael@0: * @return Number of bytes written to "compressed". michael@0: * If negative, an error has occurred (see error codes). It is IMPORTANT that michael@0: * the length returned be somehow transmitted to the decoder. Otherwise, no michael@0: * decoding is possible. michael@0: */ michael@0: OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT int opus_custom_encode( michael@0: OpusCustomEncoder *st, michael@0: const opus_int16 *pcm, michael@0: int frame_size, michael@0: unsigned char *compressed, michael@0: int maxCompressedBytes michael@0: ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4); michael@0: michael@0: /** Perform a CTL function on an Opus custom encoder. michael@0: * michael@0: * Generally the request and subsequent arguments are generated michael@0: * by a convenience macro. michael@0: * @see opus_encoderctls michael@0: */ michael@0: OPUS_CUSTOM_EXPORT int opus_custom_encoder_ctl(OpusCustomEncoder * OPUS_RESTRICT st, int request, ...) OPUS_ARG_NONNULL(1); michael@0: michael@0: michael@0: #if !defined(OPUS_BUILD) || defined(CELT_DECODER_C) michael@0: /* Decoder */ michael@0: michael@0: /** Gets the size of an OpusCustomDecoder structure. michael@0: * @param [in] mode OpusCustomMode *: Mode configuration michael@0: * @param [in] channels int: Number of channels michael@0: * @returns size michael@0: */ michael@0: OPUS_CUSTOM_EXPORT_STATIC OPUS_WARN_UNUSED_RESULT int opus_custom_decoder_get_size( michael@0: const OpusCustomMode *mode, michael@0: int channels michael@0: ) OPUS_ARG_NONNULL(1); michael@0: michael@0: /** Initializes a previously allocated decoder state michael@0: * The memory pointed to by st must be the size returned by opus_custom_decoder_get_size. michael@0: * This is intended for applications which use their own allocator instead of malloc. michael@0: * @see opus_custom_decoder_create(),opus_custom_decoder_get_size() michael@0: * To reset a previously initialized state use the OPUS_RESET_STATE CTL. michael@0: * @param [in] st OpusCustomDecoder*: Decoder state michael@0: * @param [in] mode OpusCustomMode *: Contains all the information about the characteristics of michael@0: * the stream (must be the same characteristics as used for the michael@0: * encoder) michael@0: * @param [in] channels int: Number of channels michael@0: * @return OPUS_OK Success or @ref opus_errorcodes michael@0: */ michael@0: OPUS_CUSTOM_EXPORT_STATIC int opus_custom_decoder_init( michael@0: OpusCustomDecoder *st, michael@0: const OpusCustomMode *mode, michael@0: int channels michael@0: ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2); michael@0: michael@0: #endif michael@0: michael@0: michael@0: /** Creates a new decoder state. Each stream needs its own decoder state (can't michael@0: * be shared across simultaneous streams). michael@0: * @param [in] mode OpusCustomMode: Contains all the information about the characteristics of the michael@0: * stream (must be the same characteristics as used for the encoder) michael@0: * @param [in] channels int: Number of channels michael@0: * @param [out] error int*: Returns an error code michael@0: * @return Newly created decoder state. michael@0: */ michael@0: OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT OpusCustomDecoder *opus_custom_decoder_create( michael@0: const OpusCustomMode *mode, michael@0: int channels, michael@0: int *error michael@0: ) OPUS_ARG_NONNULL(1); michael@0: michael@0: /** Destroys a an decoder state. michael@0: * @param[in] st OpusCustomDecoder*: State to be freed. michael@0: */ michael@0: OPUS_CUSTOM_EXPORT void opus_custom_decoder_destroy(OpusCustomDecoder *st); michael@0: michael@0: /** Decode an opus custom frame with floating point output michael@0: * @param [in] st OpusCustomDecoder*: Decoder state michael@0: * @param [in] data char*: Input payload. Use a NULL pointer to indicate packet loss michael@0: * @param [in] len int: Number of bytes in payload michael@0: * @param [out] pcm float*: Output signal (interleaved if 2 channels). length michael@0: * is frame_size*channels*sizeof(float) michael@0: * @param [in] frame_size Number of samples per channel of available space in *pcm. michael@0: * @returns Number of decoded samples or @ref opus_errorcodes michael@0: */ michael@0: OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT int opus_custom_decode_float( michael@0: OpusCustomDecoder *st, michael@0: const unsigned char *data, michael@0: int len, michael@0: float *pcm, michael@0: int frame_size michael@0: ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4); michael@0: michael@0: /** Decode an opus custom frame michael@0: * @param [in] st OpusCustomDecoder*: Decoder state michael@0: * @param [in] data char*: Input payload. Use a NULL pointer to indicate packet loss michael@0: * @param [in] len int: Number of bytes in payload michael@0: * @param [out] pcm opus_int16*: Output signal (interleaved if 2 channels). length michael@0: * is frame_size*channels*sizeof(opus_int16) michael@0: * @param [in] frame_size Number of samples per channel of available space in *pcm. michael@0: * @returns Number of decoded samples or @ref opus_errorcodes michael@0: */ michael@0: OPUS_CUSTOM_EXPORT OPUS_WARN_UNUSED_RESULT int opus_custom_decode( michael@0: OpusCustomDecoder *st, michael@0: const unsigned char *data, michael@0: int len, michael@0: opus_int16 *pcm, michael@0: int frame_size michael@0: ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4); michael@0: michael@0: /** Perform a CTL function on an Opus custom decoder. michael@0: * michael@0: * Generally the request and subsequent arguments are generated michael@0: * by a convenience macro. michael@0: * @see opus_genericctls michael@0: */ michael@0: OPUS_CUSTOM_EXPORT int opus_custom_decoder_ctl(OpusCustomDecoder * OPUS_RESTRICT st, int request, ...) OPUS_ARG_NONNULL(1); michael@0: michael@0: /**@}*/ michael@0: michael@0: #ifdef __cplusplus michael@0: } michael@0: #endif michael@0: michael@0: #endif /* OPUS_CUSTOM_H */