1/* Copyright (c) 2011 Xiph.Org Foundation
2 Written by Jean-Marc Valin */
3/*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28/**
29 * @file opus_multistream.h
30 * @brief Opus reference implementation multistream API
31 */
32
33#ifndef OPUS_MULTISTREAM_H
34#define OPUS_MULTISTREAM_H
35
36#include "opus.h"
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42/** @cond OPUS_INTERNAL_DOC */
43
44/** Macros to trigger compilation errors when the wrong types are provided to a
45 * CTL. */
46/**@{*/
47#define opus_check_encstate_ptr(ptr) ((ptr) + ((ptr) - (OpusEncoder**)(ptr)))
48#define opus_check_decstate_ptr(ptr) ((ptr) + ((ptr) - (OpusDecoder**)(ptr)))
49/**@}*/
50
51/** These are the actual encoder and decoder CTL ID numbers.
52 * They should not be used directly by applications.
53 * In general, SETs should be even and GETs should be odd.*/
54/**@{*/
55#define OPUS_MULTISTREAM_GET_ENCODER_STATE_REQUEST 5120
56#define OPUS_MULTISTREAM_GET_DECODER_STATE_REQUEST 5122
57/**@}*/
58
59/** @endcond */
60
61/** @defgroup opus_multistream_ctls Multistream specific encoder and decoder CTLs
62 *
63 * These are convenience macros that are specific to the
64 * opus_multistream_encoder_ctl() and opus_multistream_decoder_ctl()
65 * interface.
66 * The CTLs from @ref opus_genericctls, @ref opus_encoderctls, and
67 * @ref opus_decoderctls may be applied to a multistream encoder or decoder as
68 * well.
69 * In addition, you may retrieve the encoder or decoder state for an specific
70 * stream via #OPUS_MULTISTREAM_GET_ENCODER_STATE or
71 * #OPUS_MULTISTREAM_GET_DECODER_STATE and apply CTLs to it individually.
72 */
73/**@{*/
74
75/** Gets the encoder state for an individual stream of a multistream encoder.
76 * @param[in] x <tt>opus_int32</tt>: The index of the stream whose encoder you
77 * wish to retrieve.
78 * This must be non-negative and less than
79 * the <code>streams</code> parameter used
80 * to initialize the encoder.
81 * @param[out] y <tt>OpusEncoder**</tt>: Returns a pointer to the given
82 * encoder state.
83 * @retval OPUS_BAD_ARG The index of the requested stream was out of range.
84 * @hideinitializer
85 */
86#define OPUS_MULTISTREAM_GET_ENCODER_STATE(x,y) OPUS_MULTISTREAM_GET_ENCODER_STATE_REQUEST, opus_check_int(x), opus_check_encstate_ptr(y)
87
88/** Gets the decoder state for an individual stream of a multistream decoder.
89 * @param[in] x <tt>opus_int32</tt>: The index of the stream whose decoder you
90 * wish to retrieve.
91 * This must be non-negative and less than
92 * the <code>streams</code> parameter used
93 * to initialize the decoder.
94 * @param[out] y <tt>OpusDecoder**</tt>: Returns a pointer to the given
95 * decoder state.
96 * @retval OPUS_BAD_ARG The index of the requested stream was out of range.
97 * @hideinitializer
98 */
99#define OPUS_MULTISTREAM_GET_DECODER_STATE(x,y) OPUS_MULTISTREAM_GET_DECODER_STATE_REQUEST, opus_check_int(x), opus_check_decstate_ptr(y)
100
101/**@}*/
102
103/** @defgroup opus_multistream Opus Multistream API
104 * @{
105 *
106 * The multistream API allows individual Opus streams to be combined into a
107 * single packet, enabling support for up to 255 channels. Unlike an
108 * elementary Opus stream, the encoder and decoder must negotiate the channel
109 * configuration before the decoder can successfully interpret the data in the
110 * packets produced by the encoder. Some basic information, such as packet
111 * duration, can be computed without any special negotiation.
112 *
113 * The format for multistream Opus packets is defined in
114 * <a href="https://tools.ietf.org/html/rfc7845">RFC 7845</a>
115 * and is based on the self-delimited Opus framing described in Appendix B of
116 * <a href="https://tools.ietf.org/html/rfc6716">RFC 6716</a>.
117 * Normal Opus packets are just a degenerate case of multistream Opus packets,
118 * and can be encoded or decoded with the multistream API by setting
119 * <code>streams</code> to <code>1</code> when initializing the encoder or
120 * decoder.
121 *
122 * Multistream Opus streams can contain up to 255 elementary Opus streams.
123 * These may be either "uncoupled" or "coupled", indicating that the decoder
124 * is configured to decode them to either 1 or 2 channels, respectively.
125 * The streams are ordered so that all coupled streams appear at the
126 * beginning.
127 *
128 * A <code>mapping</code> table defines which decoded channel <code>i</code>
129 * should be used for each input/output (I/O) channel <code>j</code>. This table is
130 * typically provided as an unsigned char array.
131 * Let <code>i = mapping[j]</code> be the index for I/O channel <code>j</code>.
132 * If <code>i < 2*coupled_streams</code>, then I/O channel <code>j</code> is
133 * encoded as the left channel of stream <code>(i/2)</code> if <code>i</code>
134 * is even, or as the right channel of stream <code>(i/2)</code> if
135 * <code>i</code> is odd. Otherwise, I/O channel <code>j</code> is encoded as
136 * mono in stream <code>(i - coupled_streams)</code>, unless it has the special
137 * value 255, in which case it is omitted from the encoding entirely (the
138 * decoder will reproduce it as silence). Each value <code>i</code> must either
139 * be the special value 255 or be less than <code>streams + coupled_streams</code>.
140 *
141 * The output channels specified by the encoder
142 * should use the
143 * <a href="https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-810004.3.9">Vorbis
144 * channel ordering</a>. A decoder may wish to apply an additional permutation
145 * to the mapping the encoder used to achieve a different output channel
146 * order (e.g. for outputting in WAV order).
147 *
148 * Each multistream packet contains an Opus packet for each stream, and all of
149 * the Opus packets in a single multistream packet must have the same
150 * duration. Therefore the duration of a multistream packet can be extracted
151 * from the TOC sequence of the first stream, which is located at the
152 * beginning of the packet, just like an elementary Opus stream:
153 *
154 * @code
155 * int nb_samples;
156 * int nb_frames;
157 * nb_frames = opus_packet_get_nb_frames(data, len);
158 * if (nb_frames < 1)
159 * return nb_frames;
160 * nb_samples = opus_packet_get_samples_per_frame(data, 48000) * nb_frames;
161 * @endcode
162 *
163 * The general encoding and decoding process proceeds exactly the same as in
164 * the normal @ref opus_encoder and @ref opus_decoder APIs.
165 * See their documentation for an overview of how to use the corresponding
166 * multistream functions.
167 */
168
169/** Opus multistream encoder state.
170 * This contains the complete state of a multistream Opus encoder.
171 * It is position independent and can be freely copied.
172 * @see opus_multistream_encoder_create
173 * @see opus_multistream_encoder_init
174 */
175typedef struct OpusMSEncoder OpusMSEncoder;
176
177/** Opus multistream decoder state.
178 * This contains the complete state of a multistream Opus decoder.
179 * It is position independent and can be freely copied.
180 * @see opus_multistream_decoder_create
181 * @see opus_multistream_decoder_init
182 */
183typedef struct OpusMSDecoder OpusMSDecoder;
184
185/**\name Multistream encoder functions */
186/**@{*/
187
188/** Gets the size of an OpusMSEncoder structure.
189 * @param streams <tt>int</tt>: The total number of streams to encode from the
190 * input.
191 * This must be no more than 255.
192 * @param coupled_streams <tt>int</tt>: Number of coupled (2 channel) streams
193 * to encode.
194 * This must be no larger than the total
195 * number of streams.
196 * Additionally, The total number of
197 * encoded channels (<code>streams +
198 * coupled_streams</code>) must be no
199 * more than 255.
200 * @returns The size in bytes on success, or a negative error code
201 * (see @ref opus_errorcodes) on error.
202 */
203OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_multistream_encoder_get_size(
204 int streams,
205 int coupled_streams
206);
207
208OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_multistream_surround_encoder_get_size(
209 int channels,
210 int mapping_family
211);
212
213
214/** Allocates and initializes a multistream encoder state.
215 * Call opus_multistream_encoder_destroy() to release
216 * this object when finished.
217 * @param Fs <tt>opus_int32</tt>: Sampling rate of the input signal (in Hz).
218 * This must be one of 8000, 12000, 16000,
219 * 24000, or 48000.
220 * @param channels <tt>int</tt>: Number of channels in the input signal.
221 * This must be at most 255.
222 * It may be greater than the number of
223 * coded channels (<code>streams +
224 * coupled_streams</code>).
225 * @param streams <tt>int</tt>: The total number of streams to encode from the
226 * input.
227 * This must be no more than the number of channels.
228 * @param coupled_streams <tt>int</tt>: Number of coupled (2 channel) streams
229 * to encode.
230 * This must be no larger than the total
231 * number of streams.
232 * Additionally, The total number of
233 * encoded channels (<code>streams +
234 * coupled_streams</code>) must be no
235 * more than the number of input channels.
236 * @param[in] mapping <code>const unsigned char[channels]</code>: Mapping from
237 * encoded channels to input channels, as described in
238 * @ref opus_multistream. As an extra constraint, the
239 * multistream encoder does not allow encoding coupled
240 * streams for which one channel is unused since this
241 * is never a good idea.
242 * @param application <tt>int</tt>: The target encoder application.
243 * This must be one of the following:
244 * <dl>
245 * <dt>#OPUS_APPLICATION_VOIP</dt>
246 * <dd>Process signal for improved speech intelligibility.</dd>
247 * <dt>#OPUS_APPLICATION_AUDIO</dt>
248 * <dd>Favor faithfulness to the original input.</dd>
249 * <dt>#OPUS_APPLICATION_RESTRICTED_LOWDELAY</dt>
250 * <dd>Configure the minimum possible coding delay by disabling certain modes
251 * of operation.</dd>
252 * </dl>
253 * @param[out] error <tt>int *</tt>: Returns #OPUS_OK on success, or an error
254 * code (see @ref opus_errorcodes) on
255 * failure.
256 */
257OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusMSEncoder *opus_multistream_encoder_create(
258 opus_int32 Fs,
259 int channels,
260 int streams,
261 int coupled_streams,
262 const unsigned char *mapping,
263 int application,
264 int *error
265) OPUS_ARG_NONNULL(5);
266
267OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusMSEncoder *opus_multistream_surround_encoder_create(
268 opus_int32 Fs,
269 int channels,
270 int mapping_family,
271 int *streams,
272 int *coupled_streams,
273 unsigned char *mapping,
274 int application,
275 int *error
276) OPUS_ARG_NONNULL(4) OPUS_ARG_NONNULL(5) OPUS_ARG_NONNULL(6);
277
278/** Initialize a previously allocated multistream encoder state.
279 * The memory pointed to by \a st must be at least the size returned by
280 * opus_multistream_encoder_get_size().
281 * This is intended for applications which use their own allocator instead of
282 * malloc.
283 * To reset a previously initialized state, use the #OPUS_RESET_STATE CTL.
284 * @see opus_multistream_encoder_create
285 * @see opus_multistream_encoder_get_size
286 * @param st <tt>OpusMSEncoder*</tt>: Multistream encoder state to initialize.
287 * @param Fs <tt>opus_int32</tt>: Sampling rate of the input signal (in Hz).
288 * This must be one of 8000, 12000, 16000,
289 * 24000, or 48000.
290 * @param channels <tt>int</tt>: Number of channels in the input signal.
291 * This must be at most 255.
292 * It may be greater than the number of
293 * coded channels (<code>streams +
294 * coupled_streams</code>).
295 * @param streams <tt>int</tt>: The total number of streams to encode from the
296 * input.
297 * This must be no more than the number of channels.
298 * @param coupled_streams <tt>int</tt>: Number of coupled (2 channel) streams
299 * to encode.
300 * This must be no larger than the total
301 * number of streams.
302 * Additionally, The total number of
303 * encoded channels (<code>streams +
304 * coupled_streams</code>) must be no
305 * more than the number of input channels.
306 * @param[in] mapping <code>const unsigned char[channels]</code>: Mapping from
307 * encoded channels to input channels, as described in
308 * @ref opus_multistream. As an extra constraint, the
309 * multistream encoder does not allow encoding coupled
310 * streams for which one channel is unused since this
311 * is never a good idea.
312 * @param application <tt>int</tt>: The target encoder application.
313 * This must be one of the following:
314 * <dl>
315 * <dt>#OPUS_APPLICATION_VOIP</dt>
316 * <dd>Process signal for improved speech intelligibility.</dd>
317 * <dt>#OPUS_APPLICATION_AUDIO</dt>
318 * <dd>Favor faithfulness to the original input.</dd>
319 * <dt>#OPUS_APPLICATION_RESTRICTED_LOWDELAY</dt>
320 * <dd>Configure the minimum possible coding delay by disabling certain modes
321 * of operation.</dd>
322 * </dl>
323 * @returns #OPUS_OK on success, or an error code (see @ref opus_errorcodes)
324 * on failure.
325 */
326OPUS_EXPORT int opus_multistream_encoder_init(
327 OpusMSEncoder *st,
328 opus_int32 Fs,
329 int channels,
330 int streams,
331 int coupled_streams,
332 const unsigned char *mapping,
333 int application
334) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(6);
335
336OPUS_EXPORT int opus_multistream_surround_encoder_init(
337 OpusMSEncoder *st,
338 opus_int32 Fs,
339 int channels,
340 int mapping_family,
341 int *streams,
342 int *coupled_streams,
343 unsigned char *mapping,
344 int application
345) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(5) OPUS_ARG_NONNULL(6) OPUS_ARG_NONNULL(7);
346
347/** Encodes a multistream Opus frame.
348 * @param st <tt>OpusMSEncoder*</tt>: Multistream encoder state.
349 * @param[in] pcm <tt>const opus_int16*</tt>: The input signal as interleaved
350 * samples.
351 * This must contain
352 * <code>frame_size*channels</code>
353 * samples.
354 * @param frame_size <tt>int</tt>: Number of samples per channel in the input
355 * signal.
356 * This must be an Opus frame size for the
357 * encoder's sampling rate.
358 * For example, at 48 kHz the permitted values
359 * are 120, 240, 480, 960, 1920, and 2880.
360 * Passing in a duration of less than 10 ms
361 * (480 samples at 48 kHz) will prevent the
362 * encoder from using the LPC or hybrid modes.
363 * @param[out] data <tt>unsigned char*</tt>: Output payload.
364 * This must contain storage for at
365 * least \a max_data_bytes.
366 * @param [in] max_data_bytes <tt>opus_int32</tt>: Size of the allocated
367 * memory for the output
368 * payload. This may be
369 * used to impose an upper limit on
370 * the instant bitrate, but should
371 * not be used as the only bitrate
372 * control. Use #OPUS_SET_BITRATE to
373 * control the bitrate.
374 * @returns The length of the encoded packet (in bytes) on success or a
375 * negative error code (see @ref opus_errorcodes) on failure.
376 */
377OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_multistream_encode(
378 OpusMSEncoder *st,
379 const opus_int16 *pcm,
380 int frame_size,
381 unsigned char *data,
382 opus_int32 max_data_bytes
383) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4);
384
385/** Encodes a multistream Opus frame.
386 * @param st <tt>OpusMSEncoder*</tt>: Multistream encoder state.
387 * @param[in] pcm <tt>const opus_int32*</tt>: The input signal as interleaved
388 * samples representing (or slightly exceeding) 24-bit values.
389 * This must contain
390 * <code>frame_size*channels</code>
391 * samples.
392 * @param frame_size <tt>int</tt>: Number of samples per channel in the input
393 * signal.
394 * This must be an Opus frame size for the
395 * encoder's sampling rate.
396 * For example, at 48 kHz the permitted values
397 * are 120, 240, 480, 960, 1920, and 2880.
398 * Passing in a duration of less than 10 ms
399 * (480 samples at 48 kHz) will prevent the
400 * encoder from using the LPC or hybrid modes.
401 * @param[out] data <tt>unsigned char*</tt>: Output payload.
402 * This must contain storage for at
403 * least \a max_data_bytes.
404 * @param [in] max_data_bytes <tt>opus_int32</tt>: Size of the allocated
405 * memory for the output
406 * payload. This may be
407 * used to impose an upper limit on
408 * the instant bitrate, but should
409 * not be used as the only bitrate
410 * control. Use #OPUS_SET_BITRATE to
411 * control the bitrate.
412 * @returns The length of the encoded packet (in bytes) on success or a
413 * negative error code (see @ref opus_errorcodes) on failure.
414 */
415OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_multistream_encode24(
416 OpusMSEncoder *st,
417 const opus_int32 *pcm,
418 int frame_size,
419 unsigned char *data,
420 opus_int32 max_data_bytes
421) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4);
422
423/** Encodes a multistream Opus frame from floating point input.
424 * @param st <tt>OpusMSEncoder*</tt>: Multistream encoder state.
425 * @param[in] pcm <tt>const float*</tt>: The input signal as interleaved
426 * samples with a normal range of
427 * +/-1.0.
428 * Samples with a range beyond +/-1.0
429 * are supported but will be clipped by
430 * decoders using the integer API and
431 * should only be used if it is known
432 * that the far end supports extended
433 * dynamic range.
434 * This must contain
435 * <code>frame_size*channels</code>
436 * samples.
437 * @param frame_size <tt>int</tt>: Number of samples per channel in the input
438 * signal.
439 * This must be an Opus frame size for the
440 * encoder's sampling rate.
441 * For example, at 48 kHz the permitted values
442 * are 120, 240, 480, 960, 1920, and 2880.
443 * Passing in a duration of less than 10 ms
444 * (480 samples at 48 kHz) will prevent the
445 * encoder from using the LPC or hybrid modes.
446 * @param[out] data <tt>unsigned char*</tt>: Output payload.
447 * This must contain storage for at
448 * least \a max_data_bytes.
449 * @param [in] max_data_bytes <tt>opus_int32</tt>: Size of the allocated
450 * memory for the output
451 * payload. This may be
452 * used to impose an upper limit on
453 * the instant bitrate, but should
454 * not be used as the only bitrate
455 * control. Use #OPUS_SET_BITRATE to
456 * control the bitrate.
457 * @returns The length of the encoded packet (in bytes) on success or a
458 * negative error code (see @ref opus_errorcodes) on failure.
459 */
460OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_multistream_encode_float(
461 OpusMSEncoder *st,
462 const float *pcm,
463 int frame_size,
464 unsigned char *data,
465 opus_int32 max_data_bytes
466) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4);
467
468/** Frees an <code>OpusMSEncoder</code> allocated by
469 * opus_multistream_encoder_create().
470 * @param st <tt>OpusMSEncoder*</tt>: Multistream encoder state to be freed.
471 */
472OPUS_EXPORT void opus_multistream_encoder_destroy(OpusMSEncoder *st);
473
474/** Perform a CTL function on a multistream Opus encoder.
475 *
476 * Generally the request and subsequent arguments are generated by a
477 * convenience macro.
478 * @param st <tt>OpusMSEncoder*</tt>: Multistream encoder state.
479 * @param request This and all remaining parameters should be replaced by one
480 * of the convenience macros in @ref opus_genericctls,
481 * @ref opus_encoderctls, or @ref opus_multistream_ctls.
482 * @see opus_genericctls
483 * @see opus_encoderctls
484 * @see opus_multistream_ctls
485 */
486OPUS_EXPORT int opus_multistream_encoder_ctl(OpusMSEncoder *st, int request, ...) OPUS_ARG_NONNULL(1);
487
488/**@}*/
489
490/**\name Multistream decoder functions */
491/**@{*/
492
493/** Gets the size of an <code>OpusMSDecoder</code> structure.
494 * @param streams <tt>int</tt>: The total number of streams coded in the
495 * input.
496 * This must be no more than 255.
497 * @param coupled_streams <tt>int</tt>: Number streams to decode as coupled
498 * (2 channel) streams.
499 * This must be no larger than the total
500 * number of streams.
501 * Additionally, The total number of
502 * coded channels (<code>streams +
503 * coupled_streams</code>) must be no
504 * more than 255.
505 * @returns The size in bytes on success, or a negative error code
506 * (see @ref opus_errorcodes) on error.
507 */
508OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_multistream_decoder_get_size(
509 int streams,
510 int coupled_streams
511);
512
513/** Allocates and initializes a multistream decoder state.
514 * Call opus_multistream_decoder_destroy() to release
515 * this object when finished.
516 * @param Fs <tt>opus_int32</tt>: Sampling rate to decode at (in Hz).
517 * This must be one of 8000, 12000, 16000,
518 * 24000, or 48000.
519 * @param channels <tt>int</tt>: Number of channels to output.
520 * This must be at most 255.
521 * It may be different from the number of coded
522 * channels (<code>streams +
523 * coupled_streams</code>).
524 * @param streams <tt>int</tt>: The total number of streams coded in the
525 * input.
526 * This must be no more than 255.
527 * @param coupled_streams <tt>int</tt>: Number of streams to decode as coupled
528 * (2 channel) streams.
529 * This must be no larger than the total
530 * number of streams.
531 * Additionally, The total number of
532 * coded channels (<code>streams +
533 * coupled_streams</code>) must be no
534 * more than 255.
535 * @param[in] mapping <code>const unsigned char[channels]</code>: Mapping from
536 * coded channels to output channels, as described in
537 * @ref opus_multistream.
538 * @param[out] error <tt>int *</tt>: Returns #OPUS_OK on success, or an error
539 * code (see @ref opus_errorcodes) on
540 * failure.
541 */
542OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusMSDecoder *opus_multistream_decoder_create(
543 opus_int32 Fs,
544 int channels,
545 int streams,
546 int coupled_streams,
547 const unsigned char *mapping,
548 int *error
549) OPUS_ARG_NONNULL(5);
550
551/** Initialize a previously allocated decoder state object.
552 * The memory pointed to by \a st must be at least the size returned by
553 * opus_multistream_encoder_get_size().
554 * This is intended for applications which use their own allocator instead of
555 * malloc.
556 * To reset a previously initialized state, use the #OPUS_RESET_STATE CTL.
557 * @see opus_multistream_decoder_create
558 * @see opus_multistream_deocder_get_size
559 * @param st <tt>OpusMSEncoder*</tt>: Multistream encoder state to initialize.
560 * @param Fs <tt>opus_int32</tt>: Sampling rate to decode at (in Hz).
561 * This must be one of 8000, 12000, 16000,
562 * 24000, or 48000.
563 * @param channels <tt>int</tt>: Number of channels to output.
564 * This must be at most 255.
565 * It may be different from the number of coded
566 * channels (<code>streams +
567 * coupled_streams</code>).
568 * @param streams <tt>int</tt>: The total number of streams coded in the
569 * input.
570 * This must be no more than 255.
571 * @param coupled_streams <tt>int</tt>: Number of streams to decode as coupled
572 * (2 channel) streams.
573 * This must be no larger than the total
574 * number of streams.
575 * Additionally, The total number of
576 * coded channels (<code>streams +
577 * coupled_streams</code>) must be no
578 * more than 255.
579 * @param[in] mapping <code>const unsigned char[channels]</code>: Mapping from
580 * coded channels to output channels, as described in
581 * @ref opus_multistream.
582 * @returns #OPUS_OK on success, or an error code (see @ref opus_errorcodes)
583 * on failure.
584 */
585OPUS_EXPORT int opus_multistream_decoder_init(
586 OpusMSDecoder *st,
587 opus_int32 Fs,
588 int channels,
589 int streams,
590 int coupled_streams,
591 const unsigned char *mapping
592) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(6);
593
594/** Decode a multistream Opus packet.
595 * @param st <tt>OpusMSDecoder*</tt>: Multistream decoder state.
596 * @param[in] data <tt>const unsigned char*</tt>: Input payload.
597 * Use a <code>NULL</code>
598 * pointer to indicate packet
599 * loss.
600 * @param len <tt>opus_int32</tt>: Number of bytes in payload.
601 * @param[out] pcm <tt>opus_int16*</tt>: Output signal, with interleaved
602 * samples.
603 * This must contain room for
604 * <code>frame_size*channels</code>
605 * samples.
606 * @param frame_size <tt>int</tt>: The number of samples per channel of
607 * available space in \a pcm.
608 * If this is less than the maximum packet duration
609 * (120 ms; 5760 for 48kHz), this function will not be capable
610 * of decoding some packets. In the case of PLC (data==NULL)
611 * or FEC (decode_fec=1), then frame_size needs to be exactly
612 * the duration of audio that is missing, otherwise the
613 * decoder will not be in the optimal state to decode the
614 * next incoming packet. For the PLC and FEC cases, frame_size
615 * <b>must</b> be a multiple of 2.5 ms.
616 * @param decode_fec <tt>int</tt>: Flag (0 or 1) to request that any in-band
617 * forward error correction data be decoded.
618 * If no such data is available, the frame is
619 * decoded as if it were lost.
620 * @returns Number of samples decoded on success or a negative error code
621 * (see @ref opus_errorcodes) on failure.
622 */
623OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_multistream_decode(
624 OpusMSDecoder *st,
625 const unsigned char *data,
626 opus_int32 len,
627 opus_int16 *pcm,
628 int frame_size,
629 int decode_fec
630) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);
631
632/** Decode a multistream Opus packet.
633 * @param st <tt>OpusMSDecoder*</tt>: Multistream decoder state.
634 * @param[in] data <tt>const unsigned char*</tt>: Input payload.
635 * Use a <code>NULL</code>
636 * pointer to indicate packet
637 * loss.
638 * @param len <tt>opus_int32</tt>: Number of bytes in payload.
639 * @param[out] pcm <tt>opus_int32*</tt>: Output signal, with interleaved
640 * samples representing (or slightly exceeding) 24-bit values.
641 * This must contain room for
642 * <code>frame_size*channels</code>
643 * samples.
644 * @param frame_size <tt>int</tt>: The number of samples per channel of
645 * available space in \a pcm.
646 * If this is less than the maximum packet duration
647 * (120 ms; 5760 for 48kHz), this function will not be capable
648 * of decoding some packets. In the case of PLC (data==NULL)
649 * or FEC (decode_fec=1), then frame_size needs to be exactly
650 * the duration of audio that is missing, otherwise the
651 * decoder will not be in the optimal state to decode the
652 * next incoming packet. For the PLC and FEC cases, frame_size
653 * <b>must</b> be a multiple of 2.5 ms.
654 * @param decode_fec <tt>int</tt>: Flag (0 or 1) to request that any in-band
655 * forward error correction data be decoded.
656 * If no such data is available, the frame is
657 * decoded as if it were lost.
658 * @returns Number of samples decoded on success or a negative error code
659 * (see @ref opus_errorcodes) on failure.
660 */
661OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_multistream_decode24(
662 OpusMSDecoder *st,
663 const unsigned char *data,
664 opus_int32 len,
665 opus_int32 *pcm,
666 int frame_size,
667 int decode_fec
668) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);
669
670/** Decode a multistream Opus packet with floating point output.
671 * @param st <tt>OpusMSDecoder*</tt>: Multistream decoder state.
672 * @param[in] data <tt>const unsigned char*</tt>: Input payload.
673 * Use a <code>NULL</code>
674 * pointer to indicate packet
675 * loss.
676 * @param len <tt>opus_int32</tt>: Number of bytes in payload.
677 * @param[out] pcm <tt>opus_int16*</tt>: Output signal, with interleaved
678 * samples.
679 * This must contain room for
680 * <code>frame_size*channels</code>
681 * samples.
682 * @param frame_size <tt>int</tt>: The number of samples per channel of
683 * available space in \a pcm.
684 * If this is less than the maximum packet duration
685 * (120 ms; 5760 for 48kHz), this function will not be capable
686 * of decoding some packets. In the case of PLC (data==NULL)
687 * or FEC (decode_fec=1), then frame_size needs to be exactly
688 * the duration of audio that is missing, otherwise the
689 * decoder will not be in the optimal state to decode the
690 * next incoming packet. For the PLC and FEC cases, frame_size
691 * <b>must</b> be a multiple of 2.5 ms.
692 * @param decode_fec <tt>int</tt>: Flag (0 or 1) to request that any in-band
693 * forward error correction data be decoded.
694 * If no such data is available, the frame is
695 * decoded as if it were lost.
696 * @returns Number of samples decoded on success or a negative error code
697 * (see @ref opus_errorcodes) on failure.
698 */
699OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_multistream_decode_float(
700 OpusMSDecoder *st,
701 const unsigned char *data,
702 opus_int32 len,
703 float *pcm,
704 int frame_size,
705 int decode_fec
706) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);
707
708/** Perform a CTL function on a multistream Opus decoder.
709 *
710 * Generally the request and subsequent arguments are generated by a
711 * convenience macro.
712 * @param st <tt>OpusMSDecoder*</tt>: Multistream decoder state.
713 * @param request This and all remaining parameters should be replaced by one
714 * of the convenience macros in @ref opus_genericctls,
715 * @ref opus_decoderctls, or @ref opus_multistream_ctls.
716 * @see opus_genericctls
717 * @see opus_decoderctls
718 * @see opus_multistream_ctls
719 */
720OPUS_EXPORT int opus_multistream_decoder_ctl(OpusMSDecoder *st, int request, ...) OPUS_ARG_NONNULL(1);
721
722/** Frees an <code>OpusMSDecoder</code> allocated by
723 * opus_multistream_decoder_create().
724 * @param st <tt>OpusMSDecoder</tt>: Multistream decoder state to be freed.
725 */
726OPUS_EXPORT void opus_multistream_decoder_destroy(OpusMSDecoder *st);
727
728/**@}*/
729
730/**@}*/
731
732#ifdef __cplusplus
733}
734#endif
735
736#endif /* OPUS_MULTISTREAM_H */
737