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