cannam@155: /* Copyright (c) 2011 Xiph.Org Foundation
cannam@155:    Written by Jean-Marc Valin */
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_multistream.h
cannam@155:  * @brief Opus reference implementation multistream API
cannam@155:  */
cannam@155: 
cannam@155: #ifndef OPUS_MULTISTREAM_H
cannam@155: #define OPUS_MULTISTREAM_H
cannam@155: 
cannam@155: #include "opus.h"
cannam@155: 
cannam@155: #ifdef __cplusplus
cannam@155: extern "C" {
cannam@155: #endif
cannam@155: 
cannam@155: /** @cond OPUS_INTERNAL_DOC */
cannam@155: 
cannam@155: /** Macros to trigger compilation errors when the wrong types are provided to a
cannam@155:   * CTL. */
cannam@155: /**@{*/
cannam@155: #define __opus_check_encstate_ptr(ptr) ((ptr) + ((ptr) - (OpusEncoder**)(ptr)))
cannam@155: #define __opus_check_decstate_ptr(ptr) ((ptr) + ((ptr) - (OpusDecoder**)(ptr)))
cannam@155: /**@}*/
cannam@155: 
cannam@155: /** These are the actual encoder and decoder CTL ID numbers.
cannam@155:   * They should not be used directly by applications.
cannam@155:   * In general, SETs should be even and GETs should be odd.*/
cannam@155: /**@{*/
cannam@155: #define OPUS_MULTISTREAM_GET_ENCODER_STATE_REQUEST 5120
cannam@155: #define OPUS_MULTISTREAM_GET_DECODER_STATE_REQUEST 5122
cannam@155: /**@}*/
cannam@155: 
cannam@155: /** @endcond */
cannam@155: 
cannam@155: /** @defgroup opus_multistream_ctls Multistream specific encoder and decoder CTLs
cannam@155:   *
cannam@155:   * These are convenience macros that are specific to the
cannam@155:   * opus_multistream_encoder_ctl() and opus_multistream_decoder_ctl()
cannam@155:   * interface.
cannam@155:   * The CTLs from @ref opus_genericctls, @ref opus_encoderctls, and
cannam@155:   * @ref opus_decoderctls may be applied to a multistream encoder or decoder as
cannam@155:   * well.
cannam@155:   * In addition, you may retrieve the encoder or decoder state for an specific
cannam@155:   * stream via #OPUS_MULTISTREAM_GET_ENCODER_STATE or
cannam@155:   * #OPUS_MULTISTREAM_GET_DECODER_STATE and apply CTLs to it individually.
cannam@155:   */
cannam@155: /**@{*/
cannam@155: 
cannam@155: /** Gets the encoder state for an individual stream of a multistream encoder.
cannam@155:   * @param[in] x <tt>opus_int32</tt>: The index of the stream whose encoder you
cannam@155:   *                                   wish to retrieve.
cannam@155:   *                                   This must be non-negative and less than
cannam@155:   *                                   the <code>streams</code> parameter used
cannam@155:   *                                   to initialize the encoder.
cannam@155:   * @param[out] y <tt>OpusEncoder**</tt>: Returns a pointer to the given
cannam@155:   *                                       encoder state.
cannam@155:   * @retval OPUS_BAD_ARG The index of the requested stream was out of range.
cannam@155:   * @hideinitializer
cannam@155:   */
cannam@155: #define OPUS_MULTISTREAM_GET_ENCODER_STATE(x,y) OPUS_MULTISTREAM_GET_ENCODER_STATE_REQUEST, __opus_check_int(x), __opus_check_encstate_ptr(y)
cannam@155: 
cannam@155: /** Gets the decoder state for an individual stream of a multistream decoder.
cannam@155:   * @param[in] x <tt>opus_int32</tt>: The index of the stream whose decoder you
cannam@155:   *                                   wish to retrieve.
cannam@155:   *                                   This must be non-negative and less than
cannam@155:   *                                   the <code>streams</code> parameter used
cannam@155:   *                                   to initialize the decoder.
cannam@155:   * @param[out] y <tt>OpusDecoder**</tt>: Returns a pointer to the given
cannam@155:   *                                       decoder state.
cannam@155:   * @retval OPUS_BAD_ARG The index of the requested stream was out of range.
cannam@155:   * @hideinitializer
cannam@155:   */
cannam@155: #define OPUS_MULTISTREAM_GET_DECODER_STATE(x,y) OPUS_MULTISTREAM_GET_DECODER_STATE_REQUEST, __opus_check_int(x), __opus_check_decstate_ptr(y)
cannam@155: 
cannam@155: /**@}*/
cannam@155: 
cannam@155: /** @defgroup opus_multistream Opus Multistream API
cannam@155:   * @{
cannam@155:   *
cannam@155:   * The multistream API allows individual Opus streams to be combined into a
cannam@155:   * single packet, enabling support for up to 255 channels. Unlike an
cannam@155:   * elementary Opus stream, the encoder and decoder must negotiate the channel
cannam@155:   * configuration before the decoder can successfully interpret the data in the
cannam@155:   * packets produced by the encoder. Some basic information, such as packet
cannam@155:   * duration, can be computed without any special negotiation.
cannam@155:   *
cannam@155:   * The format for multistream Opus packets is defined in
cannam@155:   * <a href="https://tools.ietf.org/html/rfc7845">RFC 7845</a>
cannam@155:   * and is based on the self-delimited Opus framing described in Appendix B of
cannam@155:   * <a href="https://tools.ietf.org/html/rfc6716">RFC 6716</a>.
cannam@155:   * Normal Opus packets are just a degenerate case of multistream Opus packets,
cannam@155:   * and can be encoded or decoded with the multistream API by setting
cannam@155:   * <code>streams</code> to <code>1</code> when initializing the encoder or
cannam@155:   * decoder.
cannam@155:   *
cannam@155:   * Multistream Opus streams can contain up to 255 elementary Opus streams.
cannam@155:   * These may be either "uncoupled" or "coupled", indicating that the decoder
cannam@155:   * is configured to decode them to either 1 or 2 channels, respectively.
cannam@155:   * The streams are ordered so that all coupled streams appear at the
cannam@155:   * beginning.
cannam@155:   *
cannam@155:   * A <code>mapping</code> table defines which decoded channel <code>i</code>
cannam@155:   * should be used for each input/output (I/O) channel <code>j</code>. This table is
cannam@155:   * typically provided as an unsigned char array.
cannam@155:   * Let <code>i = mapping[j]</code> be the index for I/O channel <code>j</code>.
cannam@155:   * If <code>i < 2*coupled_streams</code>, then I/O channel <code>j</code> is
cannam@155:   * encoded as the left channel of stream <code>(i/2)</code> if <code>i</code>
cannam@155:   * is even, or  as the right channel of stream <code>(i/2)</code> if
cannam@155:   * <code>i</code> is odd. Otherwise, I/O channel <code>j</code> is encoded as
cannam@155:   * mono in stream <code>(i - coupled_streams)</code>, unless it has the special
cannam@155:   * value 255, in which case it is omitted from the encoding entirely (the
cannam@155:   * decoder will reproduce it as silence). Each value <code>i</code> must either
cannam@155:   * be the special value 255 or be less than <code>streams + coupled_streams</code>.
cannam@155:   *
cannam@155:   * The output channels specified by the encoder
cannam@155:   * should use the
cannam@155:   * <a href="https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-810004.3.9">Vorbis
cannam@155:   * channel ordering</a>. A decoder may wish to apply an additional permutation
cannam@155:   * to the mapping the encoder used to achieve a different output channel
cannam@155:   * order (e.g. for outputing in WAV order).
cannam@155:   *
cannam@155:   * Each multistream packet contains an Opus packet for each stream, and all of
cannam@155:   * the Opus packets in a single multistream packet must have the same
cannam@155:   * duration. Therefore the duration of a multistream packet can be extracted
cannam@155:   * from the TOC sequence of the first stream, which is located at the
cannam@155:   * beginning of the packet, just like an elementary Opus stream:
cannam@155:   *
cannam@155:   * @code
cannam@155:   * int nb_samples;
cannam@155:   * int nb_frames;
cannam@155:   * nb_frames = opus_packet_get_nb_frames(data, len);
cannam@155:   * if (nb_frames < 1)
cannam@155:   *   return nb_frames;
cannam@155:   * nb_samples = opus_packet_get_samples_per_frame(data, 48000) * nb_frames;
cannam@155:   * @endcode
cannam@155:   *
cannam@155:   * The general encoding and decoding process proceeds exactly the same as in
cannam@155:   * the normal @ref opus_encoder and @ref opus_decoder APIs.
cannam@155:   * See their documentation for an overview of how to use the corresponding
cannam@155:   * multistream functions.
cannam@155:   */
cannam@155: 
cannam@155: /** Opus multistream encoder state.
cannam@155:   * This contains the complete state of a multistream Opus encoder.
cannam@155:   * It is position independent and can be freely copied.
cannam@155:   * @see opus_multistream_encoder_create
cannam@155:   * @see opus_multistream_encoder_init
cannam@155:   */
cannam@155: typedef struct OpusMSEncoder OpusMSEncoder;
cannam@155: 
cannam@155: /** Opus multistream decoder state.
cannam@155:   * This contains the complete state of a multistream Opus decoder.
cannam@155:   * It is position independent and can be freely copied.
cannam@155:   * @see opus_multistream_decoder_create
cannam@155:   * @see opus_multistream_decoder_init
cannam@155:   */
cannam@155: typedef struct OpusMSDecoder OpusMSDecoder;
cannam@155: 
cannam@155: /**\name Multistream encoder functions */
cannam@155: /**@{*/
cannam@155: 
cannam@155: /** Gets the size of an OpusMSEncoder structure.
cannam@155:   * @param streams <tt>int</tt>: The total number of streams to encode from the
cannam@155:   *                              input.
cannam@155:   *                              This must be no more than 255.
cannam@155:   * @param coupled_streams <tt>int</tt>: Number of coupled (2 channel) streams
cannam@155:   *                                      to encode.
cannam@155:   *                                      This must be no larger than the total
cannam@155:   *                                      number of streams.
cannam@155:   *                                      Additionally, The total number of
cannam@155:   *                                      encoded channels (<code>streams +
cannam@155:   *                                      coupled_streams</code>) must be no
cannam@155:   *                                      more than 255.
cannam@155:   * @returns The size in bytes on success, or a negative error code
cannam@155:   *          (see @ref opus_errorcodes) on error.
cannam@155:   */
cannam@155: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_multistream_encoder_get_size(
cannam@155:       int streams,
cannam@155:       int coupled_streams
cannam@155: );
cannam@155: 
cannam@155: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_multistream_surround_encoder_get_size(
cannam@155:       int channels,
cannam@155:       int mapping_family
cannam@155: );
cannam@155: 
cannam@155: 
cannam@155: /** Allocates and initializes a multistream encoder state.
cannam@155:   * Call opus_multistream_encoder_destroy() to release
cannam@155:   * this object when finished.
cannam@155:   * @param Fs <tt>opus_int32</tt>: Sampling rate of the input signal (in Hz).
cannam@155:   *                                This must be one of 8000, 12000, 16000,
cannam@155:   *                                24000, or 48000.
cannam@155:   * @param channels <tt>int</tt>: Number of channels in the input signal.
cannam@155:   *                               This must be at most 255.
cannam@155:   *                               It may be greater than the number of
cannam@155:   *                               coded channels (<code>streams +
cannam@155:   *                               coupled_streams</code>).
cannam@155:   * @param streams <tt>int</tt>: The total number of streams to encode from the
cannam@155:   *                              input.
cannam@155:   *                              This must be no more than the number of channels.
cannam@155:   * @param coupled_streams <tt>int</tt>: Number of coupled (2 channel) streams
cannam@155:   *                                      to encode.
cannam@155:   *                                      This must be no larger than the total
cannam@155:   *                                      number of streams.
cannam@155:   *                                      Additionally, The total number of
cannam@155:   *                                      encoded channels (<code>streams +
cannam@155:   *                                      coupled_streams</code>) must be no
cannam@155:   *                                      more than the number of input channels.
cannam@155:   * @param[in] mapping <code>const unsigned char[channels]</code>: Mapping from
cannam@155:   *                    encoded channels to input channels, as described in
cannam@155:   *                    @ref opus_multistream. As an extra constraint, the
cannam@155:   *                    multistream encoder does not allow encoding coupled
cannam@155:   *                    streams for which one channel is unused since this
cannam@155:   *                    is never a good idea.
cannam@155:   * @param application <tt>int</tt>: The target encoder application.
cannam@155:   *                                  This must be one of the following:
cannam@155:   * <dl>
cannam@155:   * <dt>#OPUS_APPLICATION_VOIP</dt>
cannam@155:   * <dd>Process signal for improved speech intelligibility.</dd>
cannam@155:   * <dt>#OPUS_APPLICATION_AUDIO</dt>
cannam@155:   * <dd>Favor faithfulness to the original input.</dd>
cannam@155:   * <dt>#OPUS_APPLICATION_RESTRICTED_LOWDELAY</dt>
cannam@155:   * <dd>Configure the minimum possible coding delay by disabling certain modes
cannam@155:   * of operation.</dd>
cannam@155:   * </dl>
cannam@155:   * @param[out] error <tt>int *</tt>: Returns #OPUS_OK on success, or an error
cannam@155:   *                                   code (see @ref opus_errorcodes) on
cannam@155:   *                                   failure.
cannam@155:   */
cannam@155: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusMSEncoder *opus_multistream_encoder_create(
cannam@155:       opus_int32 Fs,
cannam@155:       int channels,
cannam@155:       int streams,
cannam@155:       int coupled_streams,
cannam@155:       const unsigned char *mapping,
cannam@155:       int application,
cannam@155:       int *error
cannam@155: ) OPUS_ARG_NONNULL(5);
cannam@155: 
cannam@155: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusMSEncoder *opus_multistream_surround_encoder_create(
cannam@155:       opus_int32 Fs,
cannam@155:       int channels,
cannam@155:       int mapping_family,
cannam@155:       int *streams,
cannam@155:       int *coupled_streams,
cannam@155:       unsigned char *mapping,
cannam@155:       int application,
cannam@155:       int *error
cannam@155: ) OPUS_ARG_NONNULL(4) OPUS_ARG_NONNULL(5) OPUS_ARG_NONNULL(6);
cannam@155: 
cannam@155: /** Initialize a previously allocated multistream encoder state.
cannam@155:   * The memory pointed to by \a st must be at least the size returned by
cannam@155:   * opus_multistream_encoder_get_size().
cannam@155:   * This is intended for applications which use their own allocator instead of
cannam@155:   * malloc.
cannam@155:   * To reset a previously initialized state, use the #OPUS_RESET_STATE CTL.
cannam@155:   * @see opus_multistream_encoder_create
cannam@155:   * @see opus_multistream_encoder_get_size
cannam@155:   * @param st <tt>OpusMSEncoder*</tt>: Multistream encoder state to initialize.
cannam@155:   * @param Fs <tt>opus_int32</tt>: Sampling rate of the input signal (in Hz).
cannam@155:   *                                This must be one of 8000, 12000, 16000,
cannam@155:   *                                24000, or 48000.
cannam@155:   * @param channels <tt>int</tt>: Number of channels in the input signal.
cannam@155:   *                               This must be at most 255.
cannam@155:   *                               It may be greater than the number of
cannam@155:   *                               coded channels (<code>streams +
cannam@155:   *                               coupled_streams</code>).
cannam@155:   * @param streams <tt>int</tt>: The total number of streams to encode from the
cannam@155:   *                              input.
cannam@155:   *                              This must be no more than the number of channels.
cannam@155:   * @param coupled_streams <tt>int</tt>: Number of coupled (2 channel) streams
cannam@155:   *                                      to encode.
cannam@155:   *                                      This must be no larger than the total
cannam@155:   *                                      number of streams.
cannam@155:   *                                      Additionally, The total number of
cannam@155:   *                                      encoded channels (<code>streams +
cannam@155:   *                                      coupled_streams</code>) must be no
cannam@155:   *                                      more than the number of input channels.
cannam@155:   * @param[in] mapping <code>const unsigned char[channels]</code>: Mapping from
cannam@155:   *                    encoded channels to input channels, as described in
cannam@155:   *                    @ref opus_multistream. As an extra constraint, the
cannam@155:   *                    multistream encoder does not allow encoding coupled
cannam@155:   *                    streams for which one channel is unused since this
cannam@155:   *                    is never a good idea.
cannam@155:   * @param application <tt>int</tt>: The target encoder application.
cannam@155:   *                                  This must be one of the following:
cannam@155:   * <dl>
cannam@155:   * <dt>#OPUS_APPLICATION_VOIP</dt>
cannam@155:   * <dd>Process signal for improved speech intelligibility.</dd>
cannam@155:   * <dt>#OPUS_APPLICATION_AUDIO</dt>
cannam@155:   * <dd>Favor faithfulness to the original input.</dd>
cannam@155:   * <dt>#OPUS_APPLICATION_RESTRICTED_LOWDELAY</dt>
cannam@155:   * <dd>Configure the minimum possible coding delay by disabling certain modes
cannam@155:   * of operation.</dd>
cannam@155:   * </dl>
cannam@155:   * @returns #OPUS_OK on success, or an error code (see @ref opus_errorcodes)
cannam@155:   *          on failure.
cannam@155:   */
cannam@155: OPUS_EXPORT int opus_multistream_encoder_init(
cannam@155:       OpusMSEncoder *st,
cannam@155:       opus_int32 Fs,
cannam@155:       int channels,
cannam@155:       int streams,
cannam@155:       int coupled_streams,
cannam@155:       const unsigned char *mapping,
cannam@155:       int application
cannam@155: ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(6);
cannam@155: 
cannam@155: OPUS_EXPORT int opus_multistream_surround_encoder_init(
cannam@155:       OpusMSEncoder *st,
cannam@155:       opus_int32 Fs,
cannam@155:       int channels,
cannam@155:       int mapping_family,
cannam@155:       int *streams,
cannam@155:       int *coupled_streams,
cannam@155:       unsigned char *mapping,
cannam@155:       int application
cannam@155: ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(5) OPUS_ARG_NONNULL(6) OPUS_ARG_NONNULL(7);
cannam@155: 
cannam@155: /** Encodes a multistream Opus frame.
cannam@155:   * @param st <tt>OpusMSEncoder*</tt>: Multistream encoder state.
cannam@155:   * @param[in] pcm <tt>const opus_int16*</tt>: The input signal as interleaved
cannam@155:   *                                            samples.
cannam@155:   *                                            This must contain
cannam@155:   *                                            <code>frame_size*channels</code>
cannam@155:   *                                            samples.
cannam@155:   * @param frame_size <tt>int</tt>: Number of samples per channel in the input
cannam@155:   *                                 signal.
cannam@155:   *                                 This must be an Opus frame size for the
cannam@155:   *                                 encoder's sampling rate.
cannam@155:   *                                 For example, at 48 kHz the permitted values
cannam@155:   *                                 are 120, 240, 480, 960, 1920, and 2880.
cannam@155:   *                                 Passing in a duration of less than 10 ms
cannam@155:   *                                 (480 samples at 48 kHz) will prevent the
cannam@155:   *                                 encoder from using the LPC or hybrid modes.
cannam@155:   * @param[out] data <tt>unsigned char*</tt>: Output payload.
cannam@155:   *                                           This must contain storage for at
cannam@155:   *                                           least \a max_data_bytes.
cannam@155:   * @param [in] max_data_bytes <tt>opus_int32</tt>: Size of the allocated
cannam@155:   *                                                 memory for the output
cannam@155:   *                                                 payload. This may be
cannam@155:   *                                                 used to impose an upper limit on
cannam@155:   *                                                 the instant bitrate, but should
cannam@155:   *                                                 not be used as the only bitrate
cannam@155:   *                                                 control. Use #OPUS_SET_BITRATE to
cannam@155:   *                                                 control the bitrate.
cannam@155:   * @returns The length of the encoded packet (in bytes) on success or a
cannam@155:   *          negative error code (see @ref opus_errorcodes) on failure.
cannam@155:   */
cannam@155: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_multistream_encode(
cannam@155:     OpusMSEncoder *st,
cannam@155:     const opus_int16 *pcm,
cannam@155:     int frame_size,
cannam@155:     unsigned char *data,
cannam@155:     opus_int32 max_data_bytes
cannam@155: ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4);
cannam@155: 
cannam@155: /** Encodes a multistream Opus frame from floating point input.
cannam@155:   * @param st <tt>OpusMSEncoder*</tt>: Multistream encoder state.
cannam@155:   * @param[in] pcm <tt>const float*</tt>: The input signal as interleaved
cannam@155:   *                                       samples with a normal range of
cannam@155:   *                                       +/-1.0.
cannam@155:   *                                       Samples with a range beyond +/-1.0
cannam@155:   *                                       are supported but will be clipped by
cannam@155:   *                                       decoders using the integer API and
cannam@155:   *                                       should only be used if it is known
cannam@155:   *                                       that the far end supports extended
cannam@155:   *                                       dynamic range.
cannam@155:   *                                       This must contain
cannam@155:   *                                       <code>frame_size*channels</code>
cannam@155:   *                                       samples.
cannam@155:   * @param frame_size <tt>int</tt>: Number of samples per channel in the input
cannam@155:   *                                 signal.
cannam@155:   *                                 This must be an Opus frame size for the
cannam@155:   *                                 encoder's sampling rate.
cannam@155:   *                                 For example, at 48 kHz the permitted values
cannam@155:   *                                 are 120, 240, 480, 960, 1920, and 2880.
cannam@155:   *                                 Passing in a duration of less than 10 ms
cannam@155:   *                                 (480 samples at 48 kHz) will prevent the
cannam@155:   *                                 encoder from using the LPC or hybrid modes.
cannam@155:   * @param[out] data <tt>unsigned char*</tt>: Output payload.
cannam@155:   *                                           This must contain storage for at
cannam@155:   *                                           least \a max_data_bytes.
cannam@155:   * @param [in] max_data_bytes <tt>opus_int32</tt>: Size of the allocated
cannam@155:   *                                                 memory for the output
cannam@155:   *                                                 payload. This may be
cannam@155:   *                                                 used to impose an upper limit on
cannam@155:   *                                                 the instant bitrate, but should
cannam@155:   *                                                 not be used as the only bitrate
cannam@155:   *                                                 control. Use #OPUS_SET_BITRATE to
cannam@155:   *                                                 control the bitrate.
cannam@155:   * @returns The length of the encoded packet (in bytes) on success or a
cannam@155:   *          negative error code (see @ref opus_errorcodes) on failure.
cannam@155:   */
cannam@155: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_multistream_encode_float(
cannam@155:       OpusMSEncoder *st,
cannam@155:       const float *pcm,
cannam@155:       int frame_size,
cannam@155:       unsigned char *data,
cannam@155:       opus_int32 max_data_bytes
cannam@155: ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4);
cannam@155: 
cannam@155: /** Frees an <code>OpusMSEncoder</code> allocated by
cannam@155:   * opus_multistream_encoder_create().
cannam@155:   * @param st <tt>OpusMSEncoder*</tt>: Multistream encoder state to be freed.
cannam@155:   */
cannam@155: OPUS_EXPORT void opus_multistream_encoder_destroy(OpusMSEncoder *st);
cannam@155: 
cannam@155: /** Perform a CTL function on a multistream Opus encoder.
cannam@155:   *
cannam@155:   * Generally the request and subsequent arguments are generated by a
cannam@155:   * convenience macro.
cannam@155:   * @param st <tt>OpusMSEncoder*</tt>: Multistream encoder state.
cannam@155:   * @param request This and all remaining parameters should be replaced by one
cannam@155:   *                of the convenience macros in @ref opus_genericctls,
cannam@155:   *                @ref opus_encoderctls, or @ref opus_multistream_ctls.
cannam@155:   * @see opus_genericctls
cannam@155:   * @see opus_encoderctls
cannam@155:   * @see opus_multistream_ctls
cannam@155:   */
cannam@155: OPUS_EXPORT int opus_multistream_encoder_ctl(OpusMSEncoder *st, int request, ...) OPUS_ARG_NONNULL(1);
cannam@155: 
cannam@155: /**@}*/
cannam@155: 
cannam@155: /**\name Multistream decoder functions */
cannam@155: /**@{*/
cannam@155: 
cannam@155: /** Gets the size of an <code>OpusMSDecoder</code> structure.
cannam@155:   * @param streams <tt>int</tt>: The total number of streams coded in the
cannam@155:   *                              input.
cannam@155:   *                              This must be no more than 255.
cannam@155:   * @param coupled_streams <tt>int</tt>: Number streams to decode as coupled
cannam@155:   *                                      (2 channel) streams.
cannam@155:   *                                      This must be no larger than the total
cannam@155:   *                                      number of streams.
cannam@155:   *                                      Additionally, The total number of
cannam@155:   *                                      coded channels (<code>streams +
cannam@155:   *                                      coupled_streams</code>) must be no
cannam@155:   *                                      more than 255.
cannam@155:   * @returns The size in bytes on success, or a negative error code
cannam@155:   *          (see @ref opus_errorcodes) on error.
cannam@155:   */
cannam@155: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_multistream_decoder_get_size(
cannam@155:       int streams,
cannam@155:       int coupled_streams
cannam@155: );
cannam@155: 
cannam@155: /** Allocates and initializes a multistream decoder state.
cannam@155:   * Call opus_multistream_decoder_destroy() to release
cannam@155:   * this object when finished.
cannam@155:   * @param Fs <tt>opus_int32</tt>: Sampling rate to decode at (in Hz).
cannam@155:   *                                This must be one of 8000, 12000, 16000,
cannam@155:   *                                24000, or 48000.
cannam@155:   * @param channels <tt>int</tt>: Number of channels to output.
cannam@155:   *                               This must be at most 255.
cannam@155:   *                               It may be different from the number of coded
cannam@155:   *                               channels (<code>streams +
cannam@155:   *                               coupled_streams</code>).
cannam@155:   * @param streams <tt>int</tt>: The total number of streams coded in the
cannam@155:   *                              input.
cannam@155:   *                              This must be no more than 255.
cannam@155:   * @param coupled_streams <tt>int</tt>: Number of streams to decode as coupled
cannam@155:   *                                      (2 channel) streams.
cannam@155:   *                                      This must be no larger than the total
cannam@155:   *                                      number of streams.
cannam@155:   *                                      Additionally, The total number of
cannam@155:   *                                      coded channels (<code>streams +
cannam@155:   *                                      coupled_streams</code>) must be no
cannam@155:   *                                      more than 255.
cannam@155:   * @param[in] mapping <code>const unsigned char[channels]</code>: Mapping from
cannam@155:   *                    coded channels to output channels, as described in
cannam@155:   *                    @ref opus_multistream.
cannam@155:   * @param[out] error <tt>int *</tt>: Returns #OPUS_OK on success, or an error
cannam@155:   *                                   code (see @ref opus_errorcodes) on
cannam@155:   *                                   failure.
cannam@155:   */
cannam@155: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusMSDecoder *opus_multistream_decoder_create(
cannam@155:       opus_int32 Fs,
cannam@155:       int channels,
cannam@155:       int streams,
cannam@155:       int coupled_streams,
cannam@155:       const unsigned char *mapping,
cannam@155:       int *error
cannam@155: ) OPUS_ARG_NONNULL(5);
cannam@155: 
cannam@155: /** Intialize a previously allocated decoder state object.
cannam@155:   * The memory pointed to by \a st must be at least the size returned by
cannam@155:   * opus_multistream_encoder_get_size().
cannam@155:   * This is intended for applications which use their own allocator instead of
cannam@155:   * malloc.
cannam@155:   * To reset a previously initialized state, use the #OPUS_RESET_STATE CTL.
cannam@155:   * @see opus_multistream_decoder_create
cannam@155:   * @see opus_multistream_deocder_get_size
cannam@155:   * @param st <tt>OpusMSEncoder*</tt>: Multistream encoder state to initialize.
cannam@155:   * @param Fs <tt>opus_int32</tt>: Sampling rate to decode at (in Hz).
cannam@155:   *                                This must be one of 8000, 12000, 16000,
cannam@155:   *                                24000, or 48000.
cannam@155:   * @param channels <tt>int</tt>: Number of channels to output.
cannam@155:   *                               This must be at most 255.
cannam@155:   *                               It may be different from the number of coded
cannam@155:   *                               channels (<code>streams +
cannam@155:   *                               coupled_streams</code>).
cannam@155:   * @param streams <tt>int</tt>: The total number of streams coded in the
cannam@155:   *                              input.
cannam@155:   *                              This must be no more than 255.
cannam@155:   * @param coupled_streams <tt>int</tt>: Number of streams to decode as coupled
cannam@155:   *                                      (2 channel) streams.
cannam@155:   *                                      This must be no larger than the total
cannam@155:   *                                      number of streams.
cannam@155:   *                                      Additionally, The total number of
cannam@155:   *                                      coded channels (<code>streams +
cannam@155:   *                                      coupled_streams</code>) must be no
cannam@155:   *                                      more than 255.
cannam@155:   * @param[in] mapping <code>const unsigned char[channels]</code>: Mapping from
cannam@155:   *                    coded channels to output channels, as described in
cannam@155:   *                    @ref opus_multistream.
cannam@155:   * @returns #OPUS_OK on success, or an error code (see @ref opus_errorcodes)
cannam@155:   *          on failure.
cannam@155:   */
cannam@155: OPUS_EXPORT int opus_multistream_decoder_init(
cannam@155:       OpusMSDecoder *st,
cannam@155:       opus_int32 Fs,
cannam@155:       int channels,
cannam@155:       int streams,
cannam@155:       int coupled_streams,
cannam@155:       const unsigned char *mapping
cannam@155: ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(6);
cannam@155: 
cannam@155: /** Decode a multistream Opus packet.
cannam@155:   * @param st <tt>OpusMSDecoder*</tt>: Multistream decoder state.
cannam@155:   * @param[in] data <tt>const unsigned char*</tt>: Input payload.
cannam@155:   *                                                Use a <code>NULL</code>
cannam@155:   *                                                pointer to indicate packet
cannam@155:   *                                                loss.
cannam@155:   * @param len <tt>opus_int32</tt>: Number of bytes in payload.
cannam@155:   * @param[out] pcm <tt>opus_int16*</tt>: Output signal, with interleaved
cannam@155:   *                                       samples.
cannam@155:   *                                       This must contain room for
cannam@155:   *                                       <code>frame_size*channels</code>
cannam@155:   *                                       samples.
cannam@155:   * @param frame_size <tt>int</tt>: The number of samples per channel of
cannam@155:   *                                 available space in \a pcm.
cannam@155:   *                                 If this is less than the maximum packet duration
cannam@155:   *                                 (120 ms; 5760 for 48kHz), this function will not be capable
cannam@155:   *                                 of decoding some packets. In the case of PLC (data==NULL)
cannam@155:   *                                 or FEC (decode_fec=1), then frame_size needs to be exactly
cannam@155:   *                                 the duration of audio that is missing, otherwise the
cannam@155:   *                                 decoder will not be in the optimal state to decode the
cannam@155:   *                                 next incoming packet. For the PLC and FEC cases, frame_size
cannam@155:   *                                 <b>must</b> be a multiple of 2.5 ms.
cannam@155:   * @param decode_fec <tt>int</tt>: Flag (0 or 1) to request that any in-band
cannam@155:   *                                 forward error correction data be decoded.
cannam@155:   *                                 If no such data is available, the frame is
cannam@155:   *                                 decoded as if it were lost.
cannam@155:   * @returns Number of samples decoded on success or a negative error code
cannam@155:   *          (see @ref opus_errorcodes) on failure.
cannam@155:   */
cannam@155: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_multistream_decode(
cannam@155:     OpusMSDecoder *st,
cannam@155:     const unsigned char *data,
cannam@155:     opus_int32 len,
cannam@155:     opus_int16 *pcm,
cannam@155:     int frame_size,
cannam@155:     int decode_fec
cannam@155: ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);
cannam@155: 
cannam@155: /** Decode a multistream Opus packet with floating point output.
cannam@155:   * @param st <tt>OpusMSDecoder*</tt>: Multistream decoder state.
cannam@155:   * @param[in] data <tt>const unsigned char*</tt>: Input payload.
cannam@155:   *                                                Use a <code>NULL</code>
cannam@155:   *                                                pointer to indicate packet
cannam@155:   *                                                loss.
cannam@155:   * @param len <tt>opus_int32</tt>: Number of bytes in payload.
cannam@155:   * @param[out] pcm <tt>opus_int16*</tt>: Output signal, with interleaved
cannam@155:   *                                       samples.
cannam@155:   *                                       This must contain room for
cannam@155:   *                                       <code>frame_size*channels</code>
cannam@155:   *                                       samples.
cannam@155:   * @param frame_size <tt>int</tt>: The number of samples per channel of
cannam@155:   *                                 available space in \a pcm.
cannam@155:   *                                 If this is less than the maximum packet duration
cannam@155:   *                                 (120 ms; 5760 for 48kHz), this function will not be capable
cannam@155:   *                                 of decoding some packets. In the case of PLC (data==NULL)
cannam@155:   *                                 or FEC (decode_fec=1), then frame_size needs to be exactly
cannam@155:   *                                 the duration of audio that is missing, otherwise the
cannam@155:   *                                 decoder will not be in the optimal state to decode the
cannam@155:   *                                 next incoming packet. For the PLC and FEC cases, frame_size
cannam@155:   *                                 <b>must</b> be a multiple of 2.5 ms.
cannam@155:   * @param decode_fec <tt>int</tt>: Flag (0 or 1) to request that any in-band
cannam@155:   *                                 forward error correction data be decoded.
cannam@155:   *                                 If no such data is available, the frame is
cannam@155:   *                                 decoded as if it were lost.
cannam@155:   * @returns Number of samples decoded on success or a negative error code
cannam@155:   *          (see @ref opus_errorcodes) on failure.
cannam@155:   */
cannam@155: OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_multistream_decode_float(
cannam@155:     OpusMSDecoder *st,
cannam@155:     const unsigned char *data,
cannam@155:     opus_int32 len,
cannam@155:     float *pcm,
cannam@155:     int frame_size,
cannam@155:     int decode_fec
cannam@155: ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);
cannam@155: 
cannam@155: /** Perform a CTL function on a multistream Opus decoder.
cannam@155:   *
cannam@155:   * Generally the request and subsequent arguments are generated by a
cannam@155:   * convenience macro.
cannam@155:   * @param st <tt>OpusMSDecoder*</tt>: Multistream decoder state.
cannam@155:   * @param request This and all remaining parameters should be replaced by one
cannam@155:   *                of the convenience macros in @ref opus_genericctls,
cannam@155:   *                @ref opus_decoderctls, or @ref opus_multistream_ctls.
cannam@155:   * @see opus_genericctls
cannam@155:   * @see opus_decoderctls
cannam@155:   * @see opus_multistream_ctls
cannam@155:   */
cannam@155: OPUS_EXPORT int opus_multistream_decoder_ctl(OpusMSDecoder *st, int request, ...) OPUS_ARG_NONNULL(1);
cannam@155: 
cannam@155: /** Frees an <code>OpusMSDecoder</code> allocated by
cannam@155:   * opus_multistream_decoder_create().
cannam@155:   * @param st <tt>OpusMSDecoder</tt>: Multistream decoder state to be freed.
cannam@155:   */
cannam@155: OPUS_EXPORT void opus_multistream_decoder_destroy(OpusMSDecoder *st);
cannam@155: 
cannam@155: /**@}*/
cannam@155: 
cannam@155: /**@}*/
cannam@155: 
cannam@155: #ifdef __cplusplus
cannam@155: }
cannam@155: #endif
cannam@155: 
cannam@155: #endif /* OPUS_MULTISTREAM_H */