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