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