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