egra: checkbox fixes
[iv.d.git] / xyph / opus.d
blob364d8ceca2dfaeaec1a3e381896313a6e5ef9f0a
1 /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited
2 Written by Jean-Marc Valin and Koen Vos */
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:
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
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.
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.
27 module iv.xyph.opus /*is aliced*/;
28 pragma(lib, "opus");
29 import iv.alice;
31 extern(C) nothrow @nogc:
33 /**
34 @file opus_types.h
35 @brief Opus reference implementation types
38 alias opus_int16 = short;
39 alias opus_uint16 = ushort;
40 alias opus_int32 = int;
41 alias opus_uint32 = uint;
42 alias opus_int64 = long;
43 alias opus_uint64 = ulong;
46 /**
47 * @file opus_defines.h
48 * @brief Opus reference implementation constants
51 /** @defgroup opus_errorcodes Error codes
52 * @{
54 /** No error @hideinitializer*/
55 enum OPUS_OK = 0;
56 /** One or more invalid/out of range arguments @hideinitializer*/
57 enum OPUS_BAD_ARG = -1;
58 /** Not enough bytes allocated in the buffer @hideinitializer*/
59 enum OPUS_BUFFER_TOO_SMALL = -2;
60 /** An internal error was detected @hideinitializer*/
61 enum OPUS_INTERNAL_ERROR = -3;
62 /** The compressed data passed is corrupted @hideinitializer*/
63 enum OPUS_INVALID_PACKET = -4;
64 /** Invalid/unsupported request number @hideinitializer*/
65 enum OPUS_UNIMPLEMENTED = -5;
66 /** An encoder or decoder structure is invalid or already freed @hideinitializer*/
67 enum OPUS_INVALID_STATE = -6;
68 /** Memory allocation has failed @hideinitializer*/
69 enum OPUS_ALLOC_FAIL = -7;
70 /**@}*/
72 /** These are the actual Encoder CTL ID numbers.
73 * They should not be used directly by applications.
74 * In general, SETs should be even and GETs should be odd.*/
75 enum OPUS_SET_APPLICATION_REQUEST = 4000;
76 enum OPUS_GET_APPLICATION_REQUEST = 4001;
77 enum OPUS_SET_BITRATE_REQUEST = 4002;
78 enum OPUS_GET_BITRATE_REQUEST = 4003;
79 enum OPUS_SET_MAX_BANDWIDTH_REQUEST = 4004;
80 enum OPUS_GET_MAX_BANDWIDTH_REQUEST = 4005;
81 enum OPUS_SET_VBR_REQUEST = 4006;
82 enum OPUS_GET_VBR_REQUEST = 4007;
83 enum OPUS_SET_BANDWIDTH_REQUEST = 4008;
84 enum OPUS_GET_BANDWIDTH_REQUEST = 4009;
85 enum OPUS_SET_COMPLEXITY_REQUEST = 4010;
86 enum OPUS_GET_COMPLEXITY_REQUEST = 4011;
87 enum OPUS_SET_INBAND_FEC_REQUEST = 4012;
88 enum OPUS_GET_INBAND_FEC_REQUEST = 4013;
89 enum OPUS_SET_PACKET_LOSS_PERC_REQUEST = 4014;
90 enum OPUS_GET_PACKET_LOSS_PERC_REQUEST = 4015;
91 enum OPUS_SET_DTX_REQUEST = 4016;
92 enum OPUS_GET_DTX_REQUEST = 4017;
93 enum OPUS_SET_VBR_CONSTRAINT_REQUEST = 4020;
94 enum OPUS_GET_VBR_CONSTRAINT_REQUEST = 4021;
95 enum OPUS_SET_FORCE_CHANNELS_REQUEST = 4022;
96 enum OPUS_GET_FORCE_CHANNELS_REQUEST = 4023;
97 enum OPUS_SET_SIGNAL_REQUEST = 4024;
98 enum OPUS_GET_SIGNAL_REQUEST = 4025;
99 enum OPUS_GET_LOOKAHEAD_REQUEST = 4027;
100 /* enum OPUS_RESET_STATE = 4028; */
101 enum OPUS_GET_SAMPLE_RATE_REQUEST = 4029;
102 enum OPUS_GET_FINAL_RANGE_REQUEST = 4031;
103 enum OPUS_GET_PITCH_REQUEST = 4033;
104 enum OPUS_SET_GAIN_REQUEST = 4034;
105 enum OPUS_GET_GAIN_REQUEST = 4045; /* Should have been 4035 */
106 enum OPUS_SET_LSB_DEPTH_REQUEST = 4036;
107 enum OPUS_GET_LSB_DEPTH_REQUEST = 4037;
108 enum OPUS_GET_LAST_PACKET_DURATION_REQUEST = 4039;
109 enum OPUS_SET_EXPERT_FRAME_DURATION_REQUEST = 4040;
110 enum OPUS_GET_EXPERT_FRAME_DURATION_REQUEST = 4041;
111 enum OPUS_SET_PREDICTION_DISABLED_REQUEST = 4042;
112 enum OPUS_GET_PREDICTION_DISABLED_REQUEST = 4043;
113 /* Don't use 4045, it's already taken by OPUS_GET_GAIN_REQUEST */
114 enum OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST = 4046;
115 enum OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST = 4047;
117 /** @defgroup opus_ctlvalues Pre-defined values for CTL interface
118 * @see opus_genericctls, opus_encoderctls
119 * @{
121 /* Values for the various encoder CTLs */
122 enum OPUS_AUTO = -1000; /**<Auto/default setting @hideinitializer*/
123 enum OPUS_BITRATE_MAX = -1; /**<Maximum bitrate @hideinitializer*/
125 /** Best for most VoIP/videoconference applications where listening quality and intelligibility matter most
126 * @hideinitializer */
127 enum OPUS_APPLICATION_VOIP = 2048;
128 /** Best for broadcast/high-fidelity application where the decoded audio should be as close as possible to the input
129 * @hideinitializer */
130 enum OPUS_APPLICATION_AUDIO = 2049;
131 /** Only use when lowest-achievable latency is what matters most. Voice-optimized modes cannot be used.
132 * @hideinitializer */
133 enum OPUS_APPLICATION_RESTRICTED_LOWDELAY = 2051;
135 enum OPUS_SIGNAL_VOICE = 3001; /**< Signal being encoded is voice */
136 enum OPUS_SIGNAL_MUSIC = 3002; /**< Signal being encoded is music */
137 enum OPUS_BANDWIDTH_NARROWBAND = 1101; /**< 4 kHz bandpass @hideinitializer*/
138 enum OPUS_BANDWIDTH_MEDIUMBAND = 1102; /**< 6 kHz bandpass @hideinitializer*/
139 enum OPUS_BANDWIDTH_WIDEBAND = 1103; /**< 8 kHz bandpass @hideinitializer*/
140 enum OPUS_BANDWIDTH_SUPERWIDEBAND = 1104; /**<12 kHz bandpass @hideinitializer*/
141 enum OPUS_BANDWIDTH_FULLBAND = 1105; /**<20 kHz bandpass @hideinitializer*/
143 enum OPUS_FRAMESIZE_ARG = 5000; /**< Select frame size from the argument (default) */
144 enum OPUS_FRAMESIZE_2_5_MS = 5001; /**< Use 2.5 ms frames */
145 enum OPUS_FRAMESIZE_5_MS = 5002; /**< Use 5 ms frames */
146 enum OPUS_FRAMESIZE_10_MS = 5003; /**< Use 10 ms frames */
147 enum OPUS_FRAMESIZE_20_MS = 5004; /**< Use 20 ms frames */
148 enum OPUS_FRAMESIZE_40_MS = 5005; /**< Use 40 ms frames */
149 enum OPUS_FRAMESIZE_60_MS = 5006; /**< Use 60 ms frames */
150 enum OPUS_FRAMESIZE_80_MS = 5007; /**< Use 80 ms frames */
151 enum OPUS_FRAMESIZE_100_MS = 5008; /**< Use 100 ms frames */
152 enum OPUS_FRAMESIZE_120_MS = 5009; /**< Use 120 ms frames */
154 /**@}*/
157 /** @defgroup opus_encoderctls Encoder related CTLs
159 * These are convenience macros for use with the \c opus_encode_ctl
160 * interface. They are used to generate the appropriate series of
161 * arguments for that call, passing the correct type, size and so
162 * on as expected for each particular request.
164 * Some usage examples:
166 * @code
167 * int ret;
168 * ret = opus_encoder_ctl(enc_ctx, OPUS_SET_BANDWIDTH(OPUS_AUTO));
169 * if (ret != OPUS_OK) return ret;
171 * opus_int32 rate;
172 * opus_encoder_ctl(enc_ctx, OPUS_GET_BANDWIDTH(&rate));
174 * opus_encoder_ctl(enc_ctx, OPUS_RESET_STATE);
175 * @endcode
177 * @see opus_genericctls, opus_encoder
178 * @{
181 /+TODO
182 /** Configures the encoder's computational complexity.
183 * The supported range is 0-10 inclusive with 10 representing the highest complexity.
184 * @see OPUS_GET_COMPLEXITY
185 * @param[in] x <tt>opus_int32</tt>: Allowed values: 0-10, inclusive.
187 * @hideinitializer */
188 #define OPUS_SET_COMPLEXITY(x) OPUS_SET_COMPLEXITY_REQUEST, __opus_check_int(x)
189 /** Gets the encoder's complexity configuration.
190 * @see OPUS_SET_COMPLEXITY
191 * @param[out] x <tt>opus_int32 *</tt>: Returns a value in the range 0-10,
192 * inclusive.
193 * @hideinitializer */
194 #define OPUS_GET_COMPLEXITY(x) OPUS_GET_COMPLEXITY_REQUEST, __opus_check_int_ptr(x)
196 /** Configures the bitrate in the encoder.
197 * Rates from 500 to 512000 bits per second are meaningful, as well as the
198 * special values #OPUS_AUTO and #OPUS_BITRATE_MAX.
199 * The value #OPUS_BITRATE_MAX can be used to cause the codec to use as much
200 * rate as it can, which is useful for controlling the rate by adjusting the
201 * output buffer size.
202 * @see OPUS_GET_BITRATE
203 * @param[in] x <tt>opus_int32</tt>: Bitrate in bits per second. The default
204 * is determined based on the number of
205 * channels and the input sampling rate.
206 * @hideinitializer */
207 #define OPUS_SET_BITRATE(x) OPUS_SET_BITRATE_REQUEST, __opus_check_int(x)
208 /** Gets the encoder's bitrate configuration.
209 * @see OPUS_SET_BITRATE
210 * @param[out] x <tt>opus_int32 *</tt>: Returns the bitrate in bits per second.
211 * The default is determined based on the
212 * number of channels and the input
213 * sampling rate.
214 * @hideinitializer */
215 #define OPUS_GET_BITRATE(x) OPUS_GET_BITRATE_REQUEST, __opus_check_int_ptr(x)
217 /** Enables or disables variable bitrate (VBR) in the encoder.
218 * The configured bitrate may not be met exactly because frames must
219 * be an integer number of bytes in length.
220 * @see OPUS_GET_VBR
221 * @see OPUS_SET_VBR_CONSTRAINT
222 * @param[in] x <tt>opus_int32</tt>: Allowed values:
223 * <dl>
224 * <dt>0</dt><dd>Hard CBR. For LPC/hybrid modes at very low bit-rate, this can
225 * cause noticeable quality degradation.</dd>
226 * <dt>1</dt><dd>VBR (default). The exact type of VBR is controlled by
227 * #OPUS_SET_VBR_CONSTRAINT.</dd>
228 * </dl>
229 * @hideinitializer */
230 #define OPUS_SET_VBR(x) OPUS_SET_VBR_REQUEST, __opus_check_int(x)
231 /** Determine if variable bitrate (VBR) is enabled in the encoder.
232 * @see OPUS_SET_VBR
233 * @see OPUS_GET_VBR_CONSTRAINT
234 * @param[out] x <tt>opus_int32 *</tt>: Returns one of the following values:
235 * <dl>
236 * <dt>0</dt><dd>Hard CBR.</dd>
237 * <dt>1</dt><dd>VBR (default). The exact type of VBR may be retrieved via
238 * #OPUS_GET_VBR_CONSTRAINT.</dd>
239 * </dl>
240 * @hideinitializer */
241 #define OPUS_GET_VBR(x) OPUS_GET_VBR_REQUEST, __opus_check_int_ptr(x)
243 /** Enables or disables constrained VBR in the encoder.
244 * This setting is ignored when the encoder is in CBR mode.
245 * @warning Only the MDCT mode of Opus currently heeds the constraint.
246 * Speech mode ignores it completely, hybrid mode may fail to obey it
247 * if the LPC layer uses more bitrate than the constraint would have
248 * permitted.
249 * @see OPUS_GET_VBR_CONSTRAINT
250 * @see OPUS_SET_VBR
251 * @param[in] x <tt>opus_int32</tt>: Allowed values:
252 * <dl>
253 * <dt>0</dt><dd>Unconstrained VBR.</dd>
254 * <dt>1</dt><dd>Constrained VBR (default). This creates a maximum of one
255 * frame of buffering delay assuming a transport with a
256 * serialization speed of the nominal bitrate.</dd>
257 * </dl>
258 * @hideinitializer */
259 #define OPUS_SET_VBR_CONSTRAINT(x) OPUS_SET_VBR_CONSTRAINT_REQUEST, __opus_check_int(x)
260 /** Determine if constrained VBR is enabled in the encoder.
261 * @see OPUS_SET_VBR_CONSTRAINT
262 * @see OPUS_GET_VBR
263 * @param[out] x <tt>opus_int32 *</tt>: Returns one of the following values:
264 * <dl>
265 * <dt>0</dt><dd>Unconstrained VBR.</dd>
266 * <dt>1</dt><dd>Constrained VBR (default).</dd>
267 * </dl>
268 * @hideinitializer */
269 #define OPUS_GET_VBR_CONSTRAINT(x) OPUS_GET_VBR_CONSTRAINT_REQUEST, __opus_check_int_ptr(x)
271 /** Configures mono/stereo forcing in the encoder.
272 * This can force the encoder to produce packets encoded as either mono or
273 * stereo, regardless of the format of the input audio. This is useful when
274 * the caller knows that the input signal is currently a mono source embedded
275 * in a stereo stream.
276 * @see OPUS_GET_FORCE_CHANNELS
277 * @param[in] x <tt>opus_int32</tt>: Allowed values:
278 * <dl>
279 * <dt>#OPUS_AUTO</dt><dd>Not forced (default)</dd>
280 * <dt>1</dt> <dd>Forced mono</dd>
281 * <dt>2</dt> <dd>Forced stereo</dd>
282 * </dl>
283 * @hideinitializer */
284 #define OPUS_SET_FORCE_CHANNELS(x) OPUS_SET_FORCE_CHANNELS_REQUEST, __opus_check_int(x)
285 /** Gets the encoder's forced channel configuration.
286 * @see OPUS_SET_FORCE_CHANNELS
287 * @param[out] x <tt>opus_int32 *</tt>:
288 * <dl>
289 * <dt>#OPUS_AUTO</dt><dd>Not forced (default)</dd>
290 * <dt>1</dt> <dd>Forced mono</dd>
291 * <dt>2</dt> <dd>Forced stereo</dd>
292 * </dl>
293 * @hideinitializer */
294 #define OPUS_GET_FORCE_CHANNELS(x) OPUS_GET_FORCE_CHANNELS_REQUEST, __opus_check_int_ptr(x)
296 /** Configures the maximum bandpass that the encoder will select automatically.
297 * Applications should normally use this instead of #OPUS_SET_BANDWIDTH
298 * (leaving that set to the default, #OPUS_AUTO). This allows the
299 * application to set an upper bound based on the type of input it is
300 * providing, but still gives the encoder the freedom to reduce the bandpass
301 * when the bitrate becomes too low, for better overall quality.
302 * @see OPUS_GET_MAX_BANDWIDTH
303 * @param[in] x <tt>opus_int32</tt>: Allowed values:
304 * <dl>
305 * <dt>OPUS_BANDWIDTH_NARROWBAND</dt> <dd>4 kHz passband</dd>
306 * <dt>OPUS_BANDWIDTH_MEDIUMBAND</dt> <dd>6 kHz passband</dd>
307 * <dt>OPUS_BANDWIDTH_WIDEBAND</dt> <dd>8 kHz passband</dd>
308 * <dt>OPUS_BANDWIDTH_SUPERWIDEBAND</dt><dd>12 kHz passband</dd>
309 * <dt>OPUS_BANDWIDTH_FULLBAND</dt> <dd>20 kHz passband (default)</dd>
310 * </dl>
311 * @hideinitializer */
312 #define OPUS_SET_MAX_BANDWIDTH(x) OPUS_SET_MAX_BANDWIDTH_REQUEST, __opus_check_int(x)
314 /** Gets the encoder's configured maximum allowed bandpass.
315 * @see OPUS_SET_MAX_BANDWIDTH
316 * @param[out] x <tt>opus_int32 *</tt>: Allowed values:
317 * <dl>
318 * <dt>#OPUS_BANDWIDTH_NARROWBAND</dt> <dd>4 kHz passband</dd>
319 * <dt>#OPUS_BANDWIDTH_MEDIUMBAND</dt> <dd>6 kHz passband</dd>
320 * <dt>#OPUS_BANDWIDTH_WIDEBAND</dt> <dd>8 kHz passband</dd>
321 * <dt>#OPUS_BANDWIDTH_SUPERWIDEBAND</dt><dd>12 kHz passband</dd>
322 * <dt>#OPUS_BANDWIDTH_FULLBAND</dt> <dd>20 kHz passband (default)</dd>
323 * </dl>
324 * @hideinitializer */
325 #define OPUS_GET_MAX_BANDWIDTH(x) OPUS_GET_MAX_BANDWIDTH_REQUEST, __opus_check_int_ptr(x)
327 /** Sets the encoder's bandpass to a specific value.
328 * This prevents the encoder from automatically selecting the bandpass based
329 * on the available bitrate. If an application knows the bandpass of the input
330 * audio it is providing, it should normally use #OPUS_SET_MAX_BANDWIDTH
331 * instead, which still gives the encoder the freedom to reduce the bandpass
332 * when the bitrate becomes too low, for better overall quality.
333 * @see OPUS_GET_BANDWIDTH
334 * @param[in] x <tt>opus_int32</tt>: Allowed values:
335 * <dl>
336 * <dt>#OPUS_AUTO</dt> <dd>(default)</dd>
337 * <dt>#OPUS_BANDWIDTH_NARROWBAND</dt> <dd>4 kHz passband</dd>
338 * <dt>#OPUS_BANDWIDTH_MEDIUMBAND</dt> <dd>6 kHz passband</dd>
339 * <dt>#OPUS_BANDWIDTH_WIDEBAND</dt> <dd>8 kHz passband</dd>
340 * <dt>#OPUS_BANDWIDTH_SUPERWIDEBAND</dt><dd>12 kHz passband</dd>
341 * <dt>#OPUS_BANDWIDTH_FULLBAND</dt> <dd>20 kHz passband</dd>
342 * </dl>
343 * @hideinitializer */
344 #define OPUS_SET_BANDWIDTH(x) OPUS_SET_BANDWIDTH_REQUEST, __opus_check_int(x)
346 /** Configures the type of signal being encoded.
347 * This is a hint which helps the encoder's mode selection.
348 * @see OPUS_GET_SIGNAL
349 * @param[in] x <tt>opus_int32</tt>: Allowed values:
350 * <dl>
351 * <dt>#OPUS_AUTO</dt> <dd>(default)</dd>
352 * <dt>#OPUS_SIGNAL_VOICE</dt><dd>Bias thresholds towards choosing LPC or Hybrid modes.</dd>
353 * <dt>#OPUS_SIGNAL_MUSIC</dt><dd>Bias thresholds towards choosing MDCT modes.</dd>
354 * </dl>
355 * @hideinitializer */
356 #define OPUS_SET_SIGNAL(x) OPUS_SET_SIGNAL_REQUEST, __opus_check_int(x)
357 /** Gets the encoder's configured signal type.
358 * @see OPUS_SET_SIGNAL
359 * @param[out] x <tt>opus_int32 *</tt>: Returns one of the following values:
360 * <dl>
361 * <dt>#OPUS_AUTO</dt> <dd>(default)</dd>
362 * <dt>#OPUS_SIGNAL_VOICE</dt><dd>Bias thresholds towards choosing LPC or Hybrid modes.</dd>
363 * <dt>#OPUS_SIGNAL_MUSIC</dt><dd>Bias thresholds towards choosing MDCT modes.</dd>
364 * </dl>
365 * @hideinitializer */
366 #define OPUS_GET_SIGNAL(x) OPUS_GET_SIGNAL_REQUEST, __opus_check_int_ptr(x)
369 /** Configures the encoder's intended application.
370 * The initial value is a mandatory argument to the encoder_create function.
371 * @see OPUS_GET_APPLICATION
372 * @param[in] x <tt>opus_int32</tt>: Returns one of the following values:
373 * <dl>
374 * <dt>#OPUS_APPLICATION_VOIP</dt>
375 * <dd>Process signal for improved speech intelligibility.</dd>
376 * <dt>#OPUS_APPLICATION_AUDIO</dt>
377 * <dd>Favor faithfulness to the original input.</dd>
378 * <dt>#OPUS_APPLICATION_RESTRICTED_LOWDELAY</dt>
379 * <dd>Configure the minimum possible coding delay by disabling certain modes
380 * of operation.</dd>
381 * </dl>
382 * @hideinitializer */
383 #define OPUS_SET_APPLICATION(x) OPUS_SET_APPLICATION_REQUEST, __opus_check_int(x)
384 /** Gets the encoder's configured application.
385 * @see OPUS_SET_APPLICATION
386 * @param[out] x <tt>opus_int32 *</tt>: Returns one of the following values:
387 * <dl>
388 * <dt>#OPUS_APPLICATION_VOIP</dt>
389 * <dd>Process signal for improved speech intelligibility.</dd>
390 * <dt>#OPUS_APPLICATION_AUDIO</dt>
391 * <dd>Favor faithfulness to the original input.</dd>
392 * <dt>#OPUS_APPLICATION_RESTRICTED_LOWDELAY</dt>
393 * <dd>Configure the minimum possible coding delay by disabling certain modes
394 * of operation.</dd>
395 * </dl>
396 * @hideinitializer */
397 #define OPUS_GET_APPLICATION(x) OPUS_GET_APPLICATION_REQUEST, __opus_check_int_ptr(x)
399 /** Gets the total samples of delay added by the entire codec.
400 * This can be queried by the encoder and then the provided number of samples can be
401 * skipped on from the start of the decoder's output to provide time aligned input
402 * and output. From the perspective of a decoding application the real data begins this many
403 * samples late.
405 * The decoder contribution to this delay is identical for all decoders, but the
406 * encoder portion of the delay may vary from implementation to implementation,
407 * version to version, or even depend on the encoder's initial configuration.
408 * Applications needing delay compensation should call this CTL rather than
409 * hard-coding a value.
410 * @param[out] x <tt>opus_int32 *</tt>: Number of lookahead samples
411 * @hideinitializer */
412 #define OPUS_GET_LOOKAHEAD(x) OPUS_GET_LOOKAHEAD_REQUEST, __opus_check_int_ptr(x)
414 /** Configures the encoder's use of inband forward error correction (FEC).
415 * @note This is only applicable to the LPC layer
416 * @see OPUS_GET_INBAND_FEC
417 * @param[in] x <tt>opus_int32</tt>: Allowed values:
418 * <dl>
419 * <dt>0</dt><dd>Disable inband FEC (default).</dd>
420 * <dt>1</dt><dd>Enable inband FEC.</dd>
421 * </dl>
422 * @hideinitializer */
423 #define OPUS_SET_INBAND_FEC(x) OPUS_SET_INBAND_FEC_REQUEST, __opus_check_int(x)
424 /** Gets encoder's configured use of inband forward error correction.
425 * @see OPUS_SET_INBAND_FEC
426 * @param[out] x <tt>opus_int32 *</tt>: Returns one of the following values:
427 * <dl>
428 * <dt>0</dt><dd>Inband FEC disabled (default).</dd>
429 * <dt>1</dt><dd>Inband FEC enabled.</dd>
430 * </dl>
431 * @hideinitializer */
432 #define OPUS_GET_INBAND_FEC(x) OPUS_GET_INBAND_FEC_REQUEST, __opus_check_int_ptr(x)
434 /** Configures the encoder's expected packet loss percentage.
435 * Higher values trigger progressively more loss resistant behavior in the encoder
436 * at the expense of quality at a given bitrate in the absence of packet loss, but
437 * greater quality under loss.
438 * @see OPUS_GET_PACKET_LOSS_PERC
439 * @param[in] x <tt>opus_int32</tt>: Loss percentage in the range 0-100, inclusive (default: 0).
440 * @hideinitializer */
441 #define OPUS_SET_PACKET_LOSS_PERC(x) OPUS_SET_PACKET_LOSS_PERC_REQUEST, __opus_check_int(x)
442 /** Gets the encoder's configured packet loss percentage.
443 * @see OPUS_SET_PACKET_LOSS_PERC
444 * @param[out] x <tt>opus_int32 *</tt>: Returns the configured loss percentage
445 * in the range 0-100, inclusive (default: 0).
446 * @hideinitializer */
447 #define OPUS_GET_PACKET_LOSS_PERC(x) OPUS_GET_PACKET_LOSS_PERC_REQUEST, __opus_check_int_ptr(x)
449 /** Configures the encoder's use of discontinuous transmission (DTX).
450 * @note This is only applicable to the LPC layer
451 * @see OPUS_GET_DTX
452 * @param[in] x <tt>opus_int32</tt>: Allowed values:
453 * <dl>
454 * <dt>0</dt><dd>Disable DTX (default).</dd>
455 * <dt>1</dt><dd>Enabled DTX.</dd>
456 * </dl>
457 * @hideinitializer */
458 #define OPUS_SET_DTX(x) OPUS_SET_DTX_REQUEST, __opus_check_int(x)
459 /** Gets encoder's configured use of discontinuous transmission.
460 * @see OPUS_SET_DTX
461 * @param[out] x <tt>opus_int32 *</tt>: Returns one of the following values:
462 * <dl>
463 * <dt>0</dt><dd>DTX disabled (default).</dd>
464 * <dt>1</dt><dd>DTX enabled.</dd>
465 * </dl>
466 * @hideinitializer */
467 #define OPUS_GET_DTX(x) OPUS_GET_DTX_REQUEST, __opus_check_int_ptr(x)
468 /** Configures the depth of signal being encoded.
470 * This is a hint which helps the encoder identify silence and near-silence.
471 * It represents the number of significant bits of linear intensity below
472 * which the signal contains ignorable quantization or other noise.
474 * For example, OPUS_SET_LSB_DEPTH(14) would be an appropriate setting
475 * for G.711 u-law input. OPUS_SET_LSB_DEPTH(16) would be appropriate
476 * for 16-bit linear pcm input with opus_encode_float().
478 * When using opus_encode() instead of opus_encode_float(), or when libopus
479 * is compiled for fixed-point, the encoder uses the minimum of the value
480 * set here and the value 16.
482 * @see OPUS_GET_LSB_DEPTH
483 * @param[in] x <tt>opus_int32</tt>: Input precision in bits, between 8 and 24
484 * (default: 24).
485 * @hideinitializer */
486 #define OPUS_SET_LSB_DEPTH(x) OPUS_SET_LSB_DEPTH_REQUEST, __opus_check_int(x)
487 /** Gets the encoder's configured signal depth.
488 * @see OPUS_SET_LSB_DEPTH
489 * @param[out] x <tt>opus_int32 *</tt>: Input precision in bits, between 8 and
490 * 24 (default: 24).
491 * @hideinitializer */
492 #define OPUS_GET_LSB_DEPTH(x) OPUS_GET_LSB_DEPTH_REQUEST, __opus_check_int_ptr(x)
494 /** Configures the encoder's use of variable duration frames.
495 * When variable duration is enabled, the encoder is free to use a shorter frame
496 * size than the one requested in the opus_encode*() call.
497 * It is then the user's responsibility
498 * to verify how much audio was encoded by checking the ToC byte of the encoded
499 * packet. The part of the audio that was not encoded needs to be resent to the
500 * encoder for the next call. Do not use this option unless you <b>really</b>
501 * know what you are doing.
502 * @see OPUS_GET_EXPERT_FRAME_DURATION
503 * @param[in] x <tt>opus_int32</tt>: Allowed values:
504 * <dl>
505 * <dt>OPUS_FRAMESIZE_ARG</dt><dd>Select frame size from the argument (default).</dd>
506 * <dt>OPUS_FRAMESIZE_2_5_MS</dt><dd>Use 2.5 ms frames.</dd>
507 * <dt>OPUS_FRAMESIZE_5_MS</dt><dd>Use 5 ms frames.</dd>
508 * <dt>OPUS_FRAMESIZE_10_MS</dt><dd>Use 10 ms frames.</dd>
509 * <dt>OPUS_FRAMESIZE_20_MS</dt><dd>Use 20 ms frames.</dd>
510 * <dt>OPUS_FRAMESIZE_40_MS</dt><dd>Use 40 ms frames.</dd>
511 * <dt>OPUS_FRAMESIZE_60_MS</dt><dd>Use 60 ms frames.</dd>
512 * <dt>OPUS_FRAMESIZE_80_MS</dt><dd>Use 80 ms frames.</dd>
513 * <dt>OPUS_FRAMESIZE_100_MS</dt><dd>Use 100 ms frames.</dd>
514 * <dt>OPUS_FRAMESIZE_120_MS</dt><dd>Use 120 ms frames.</dd>
515 * </dl>
516 * @hideinitializer */
517 #define OPUS_SET_EXPERT_FRAME_DURATION(x) OPUS_SET_EXPERT_FRAME_DURATION_REQUEST, __opus_check_int(x)
518 /** Gets the encoder's configured use of variable duration frames.
519 * @see OPUS_SET_EXPERT_FRAME_DURATION
520 * @param[out] x <tt>opus_int32 *</tt>: Returns one of the following values:
521 * <dl>
522 * <dt>OPUS_FRAMESIZE_ARG</dt><dd>Select frame size from the argument (default).</dd>
523 * <dt>OPUS_FRAMESIZE_2_5_MS</dt><dd>Use 2.5 ms frames.</dd>
524 * <dt>OPUS_FRAMESIZE_5_MS</dt><dd>Use 5 ms frames.</dd>
525 * <dt>OPUS_FRAMESIZE_10_MS</dt><dd>Use 10 ms frames.</dd>
526 * <dt>OPUS_FRAMESIZE_20_MS</dt><dd>Use 20 ms frames.</dd>
527 * <dt>OPUS_FRAMESIZE_40_MS</dt><dd>Use 40 ms frames.</dd>
528 * <dt>OPUS_FRAMESIZE_60_MS</dt><dd>Use 60 ms frames.</dd>
529 * <dt>OPUS_FRAMESIZE_80_MS</dt><dd>Use 80 ms frames.</dd>
530 * <dt>OPUS_FRAMESIZE_100_MS</dt><dd>Use 100 ms frames.</dd>
531 * <dt>OPUS_FRAMESIZE_120_MS</dt><dd>Use 120 ms frames.</dd>
532 * </dl>
533 * @hideinitializer */
534 #define OPUS_GET_EXPERT_FRAME_DURATION(x) OPUS_GET_EXPERT_FRAME_DURATION_REQUEST, __opus_check_int_ptr(x)
536 /** If set to 1, disables almost all use of prediction, making frames almost
537 * completely independent. This reduces quality.
538 * @see OPUS_GET_PREDICTION_DISABLED
539 * @param[in] x <tt>opus_int32</tt>: Allowed values:
540 * <dl>
541 * <dt>0</dt><dd>Enable prediction (default).</dd>
542 * <dt>1</dt><dd>Disable prediction.</dd>
543 * </dl>
544 * @hideinitializer */
545 #define OPUS_SET_PREDICTION_DISABLED(x) OPUS_SET_PREDICTION_DISABLED_REQUEST, __opus_check_int(x)
546 /** Gets the encoder's configured prediction status.
547 * @see OPUS_SET_PREDICTION_DISABLED
548 * @param[out] x <tt>opus_int32 *</tt>: Returns one of the following values:
549 * <dl>
550 * <dt>0</dt><dd>Prediction enabled (default).</dd>
551 * <dt>1</dt><dd>Prediction disabled.</dd>
552 * </dl>
553 * @hideinitializer */
554 #define OPUS_GET_PREDICTION_DISABLED(x) OPUS_GET_PREDICTION_DISABLED_REQUEST, __opus_check_int_ptr(x)
556 /**@}*/
558 /** @defgroup opus_genericctls Generic CTLs
560 * These macros are used with the \c opus_decoder_ctl and
561 * \c opus_encoder_ctl calls to generate a particular
562 * request.
564 * When called on an \c OpusDecoder they apply to that
565 * particular decoder instance. When called on an
566 * \c OpusEncoder they apply to the corresponding setting
567 * on that encoder instance, if present.
569 * Some usage examples:
571 * @code
572 * int ret;
573 * opus_int32 pitch;
574 * ret = opus_decoder_ctl(dec_ctx, OPUS_GET_PITCH(&pitch));
575 * if (ret == OPUS_OK) return ret;
577 * opus_encoder_ctl(enc_ctx, OPUS_RESET_STATE);
578 * opus_decoder_ctl(dec_ctx, OPUS_RESET_STATE);
580 * opus_int32 enc_bw, dec_bw;
581 * opus_encoder_ctl(enc_ctx, OPUS_GET_BANDWIDTH(&enc_bw));
582 * opus_decoder_ctl(dec_ctx, OPUS_GET_BANDWIDTH(&dec_bw));
583 * if (enc_bw != dec_bw) {
584 * printf("packet bandwidth mismatch!\n");
586 * @endcode
588 * @see opus_encoder, opus_decoder_ctl, opus_encoder_ctl, opus_decoderctls, opus_encoderctls
589 * @{
592 /** Resets the codec state to be equivalent to a freshly initialized state.
593 * This should be called when switching streams in order to prevent
594 * the back to back decoding from giving different results from
595 * one at a time decoding.
596 * @hideinitializer */
597 enum OPUS_RESET_STATE = 4028;
599 /+TODO
600 /** Gets the final state of the codec's entropy coder.
601 * This is used for testing purposes,
602 * The encoder and decoder state should be identical after coding a payload
603 * (assuming no data corruption or software bugs)
605 * @param[out] x <tt>opus_uint32 *</tt>: Entropy coder state
607 * @hideinitializer */
608 #define OPUS_GET_FINAL_RANGE(x) OPUS_GET_FINAL_RANGE_REQUEST, __opus_check_uint_ptr(x)
610 /** Gets the encoder's configured bandpass or the decoder's last bandpass.
611 * @see OPUS_SET_BANDWIDTH
612 * @param[out] x <tt>opus_int32 *</tt>: Returns one of the following values:
613 * <dl>
614 * <dt>#OPUS_AUTO</dt> <dd>(default)</dd>
615 * <dt>#OPUS_BANDWIDTH_NARROWBAND</dt> <dd>4 kHz passband</dd>
616 * <dt>#OPUS_BANDWIDTH_MEDIUMBAND</dt> <dd>6 kHz passband</dd>
617 * <dt>#OPUS_BANDWIDTH_WIDEBAND</dt> <dd>8 kHz passband</dd>
618 * <dt>#OPUS_BANDWIDTH_SUPERWIDEBAND</dt><dd>12 kHz passband</dd>
619 * <dt>#OPUS_BANDWIDTH_FULLBAND</dt> <dd>20 kHz passband</dd>
620 * </dl>
621 * @hideinitializer */
622 #define OPUS_GET_BANDWIDTH(x) OPUS_GET_BANDWIDTH_REQUEST, __opus_check_int_ptr(x)
624 /** Gets the sampling rate the encoder or decoder was initialized with.
625 * This simply returns the <code>Fs</code> value passed to opus_encoder_init()
626 * or opus_decoder_init().
627 * @param[out] x <tt>opus_int32 *</tt>: Sampling rate of encoder or decoder.
628 * @hideinitializer
630 #define OPUS_GET_SAMPLE_RATE(x) OPUS_GET_SAMPLE_RATE_REQUEST, __opus_check_int_ptr(x)
632 /** If set to 1, disables the use of phase inversion for intensity stereo,
633 * improving the quality of mono downmixes, but slightly reducing normal
634 * stereo quality. Disabling phase inversion in the decoder does not comply
635 * with RFC 6716, although it does not cause any interoperability issue and
636 * is expected to become part of the Opus standard once RFC 6716 is updated
637 * by draft-ietf-codec-opus-update.
638 * @see OPUS_GET_PHASE_INVERSION_DISABLED
639 * @param[in] x <tt>opus_int32</tt>: Allowed values:
640 * <dl>
641 * <dt>0</dt><dd>Enable phase inversion (default).</dd>
642 * <dt>1</dt><dd>Disable phase inversion.</dd>
643 * </dl>
644 * @hideinitializer */
645 #define OPUS_SET_PHASE_INVERSION_DISABLED(x) OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST, __opus_check_int(x)
646 /** Gets the encoder's configured phase inversion status.
647 * @see OPUS_SET_PHASE_INVERSION_DISABLED
648 * @param[out] x <tt>opus_int32 *</tt>: Returns one of the following values:
649 * <dl>
650 * <dt>0</dt><dd>Stereo phase inversion enabled (default).</dd>
651 * <dt>1</dt><dd>Stereo phase inversion disabled.</dd>
652 * </dl>
653 * @hideinitializer */
654 #define OPUS_GET_PHASE_INVERSION_DISABLED(x) OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST, __opus_check_int_ptr(x)
656 /**@}*/
658 /** @defgroup opus_decoderctls Decoder related CTLs
659 * @see opus_genericctls, opus_encoderctls, opus_decoder
660 * @{
663 /+TODO
664 /** Configures decoder gain adjustment.
665 * Scales the decoded output by a factor specified in Q8 dB units.
666 * This has a maximum range of -32768 to 32767 inclusive, and returns
667 * OPUS_BAD_ARG otherwise. The default is zero indicating no adjustment.
668 * This setting survives decoder reset.
670 * gain = pow(10, x/(20.0*256))
672 * @param[in] x <tt>opus_int32</tt>: Amount to scale PCM signal by in Q8 dB units.
673 * @hideinitializer */
674 #define OPUS_SET_GAIN(x) OPUS_SET_GAIN_REQUEST, __opus_check_int(x)
675 /** Gets the decoder's configured gain adjustment. @see OPUS_SET_GAIN
677 * @param[out] x <tt>opus_int32 *</tt>: Amount to scale PCM signal by in Q8 dB units.
678 * @hideinitializer */
679 #define OPUS_GET_GAIN(x) OPUS_GET_GAIN_REQUEST, __opus_check_int_ptr(x)
681 /** Gets the duration (in samples) of the last packet successfully decoded or concealed.
682 * @param[out] x <tt>opus_int32 *</tt>: Number of samples (at current sampling rate).
683 * @hideinitializer */
684 #define OPUS_GET_LAST_PACKET_DURATION(x) OPUS_GET_LAST_PACKET_DURATION_REQUEST, __opus_check_int_ptr(x)
686 /** Gets the pitch of the last decoded frame, if available.
687 * This can be used for any post-processing algorithm requiring the use of pitch,
688 * e.g. time stretching/shortening. If the last frame was not voiced, or if the
689 * pitch was not coded in the frame, then zero is returned.
691 * This CTL is only implemented for decoder instances.
693 * @param[out] x <tt>opus_int32 *</tt>: pitch period at 48 kHz (or 0 if not available)
695 * @hideinitializer */
696 #define OPUS_GET_PITCH(x) OPUS_GET_PITCH_REQUEST, __opus_check_int_ptr(x)
698 /**@}*/
700 /** @defgroup opus_libinfo Opus library information functions
701 * @{
704 /** Converts an opus error code into a human readable string.
706 * @param[in] error <tt>int</tt>: Error number
707 * @returns Error string
709 const(char)* opus_strerror (int error);
710 const(char)[] opus_strerr (int error) {
711 auto s = opus_strerror(error);
712 if (s !is null) {
713 uint len = 0;
714 while (s[len]) ++len;
715 return s[0..len];
717 return null;
720 /** Gets the libopus version string.
722 * Applications may look for the substring "-fixed" in the version string to
723 * determine whether they have a fixed-point or floating-point build at
724 * runtime.
726 * @returns Version string
728 /**@}*/
729 const(char)* opus_get_version_string ();
730 const(char)[] opus_get_version_str () {
731 auto s = opus_get_version_string();
732 if (s !is null) {
733 uint len = 0;
734 while (s[len]) ++len;
735 return s[0..len];
737 return null;
742 * @file opus.h
743 * @brief Opus reference implementation API
747 * @mainpage Opus
749 * The Opus codec is designed for interactive speech and audio transmission over the Internet.
750 * It is designed by the IETF Codec Working Group and incorporates technology from
751 * Skype's SILK codec and Xiph.Org's CELT codec.
753 * The Opus codec is designed to handle a wide range of interactive audio applications,
754 * including Voice over IP, videoconferencing, in-game chat, and even remote live music
755 * performances. It can scale from low bit-rate narrowband speech to very high quality
756 * stereo music. Its main features are:
758 * @li Sampling rates from 8 to 48 kHz
759 * @li Bit-rates from 6 kb/s to 510 kb/s
760 * @li Support for both constant bit-rate (CBR) and variable bit-rate (VBR)
761 * @li Audio bandwidth from narrowband to full-band
762 * @li Support for speech and music
763 * @li Support for mono and stereo
764 * @li Support for multichannel (up to 255 channels)
765 * @li Frame sizes from 2.5 ms to 60 ms
766 * @li Good loss robustness and packet loss concealment (PLC)
767 * @li Floating point and fixed-point implementation
769 * Documentation sections:
770 * @li @ref opus_encoder
771 * @li @ref opus_decoder
772 * @li @ref opus_repacketizer
773 * @li @ref opus_multistream
774 * @li @ref opus_libinfo
775 * @li @ref opus_custom
778 /** @defgroup opus_encoder Opus Encoder
779 * @{
781 * @brief This page describes the process and functions used to encode Opus.
783 * Since Opus is a stateful codec, the encoding process starts with creating an encoder
784 * state. This can be done with:
786 * @code
787 * int error;
788 * OpusEncoder *enc;
789 * enc = opus_encoder_create(Fs, channels, application, &error);
790 * @endcode
792 * From this point, @c enc can be used for encoding an audio stream. An encoder state
793 * @b must @b not be used for more than one stream at the same time. Similarly, the encoder
794 * state @b must @b not be re-initialized for each frame.
796 * While opus_encoder_create() allocates memory for the state, it's also possible
797 * to initialize pre-allocated memory:
799 * @code
800 * int size;
801 * int error;
802 * OpusEncoder *enc;
803 * size = opus_encoder_get_size(channels);
804 * enc = malloc(size);
805 * error = opus_encoder_init(enc, Fs, channels, application);
806 * @endcode
808 * where opus_encoder_get_size() returns the required size for the encoder state. Note that
809 * future versions of this code may change the size, so no assuptions should be made about it.
811 * The encoder state is always continuous in memory and only a shallow copy is sufficient
812 * to copy it (e.g. memcpy())
814 * It is possible to change some of the encoder's settings using the opus_encoder_ctl()
815 * interface. All these settings already default to the recommended value, so they should
816 * only be changed when necessary. The most common settings one may want to change are:
818 * @code
819 * opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrate));
820 * opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(complexity));
821 * opus_encoder_ctl(enc, OPUS_SET_SIGNAL(signal_type));
822 * @endcode
824 * where
826 * @arg bitrate is in bits per second (b/s)
827 * @arg complexity is a value from 1 to 10, where 1 is the lowest complexity and 10 is the highest
828 * @arg signal_type is either OPUS_AUTO (default), OPUS_SIGNAL_VOICE, or OPUS_SIGNAL_MUSIC
830 * See @ref opus_encoderctls and @ref opus_genericctls for a complete list of parameters that can be set or queried. Most parameters can be set or changed at any time during a stream.
832 * To encode a frame, opus_encode() or opus_encode_float() must be called with exactly one frame (2.5, 5, 10, 20, 40 or 60 ms) of audio data:
833 * @code
834 * len = opus_encode(enc, audio_frame, frame_size, packet, max_packet);
835 * @endcode
837 * where
838 * <ul>
839 * <li>audio_frame is the audio data in opus_int16 (or float for opus_encode_float())</li>
840 * <li>frame_size is the duration of the frame in samples (per channel)</li>
841 * <li>packet is the byte array to which the compressed data is written</li>
842 * <li>max_packet is the maximum number of bytes that can be written in the packet (4000 bytes is recommended).
843 * Do not use max_packet to control VBR target bitrate, instead use the #OPUS_SET_BITRATE CTL.</li>
844 * </ul>
846 * opus_encode() and opus_encode_float() return the number of bytes actually written to the packet.
847 * The return value <b>can be negative</b>, which indicates that an error has occurred. If the return value
848 * is 2 bytes or less, then the packet does not need to be transmitted (DTX).
850 * Once the encoder state if no longer needed, it can be destroyed with
852 * @code
853 * opus_encoder_destroy(enc);
854 * @endcode
856 * If the encoder was created with opus_encoder_init() rather than opus_encoder_create(),
857 * then no action is required aside from potentially freeing the memory that was manually
858 * allocated for it (calling free(enc) for the example above)
862 /** Opus encoder state.
863 * This contains the complete state of an Opus encoder.
864 * It is position independent and can be freely copied.
865 * @see opus_encoder_create,opus_encoder_init
867 struct OpusEncoder;
869 /** Gets the size of an <code>OpusEncoder</code> structure.
870 * @param[in] channels <tt>int</tt>: Number of channels.
871 * This must be 1 or 2.
872 * @returns The size in bytes.
874 int opus_encoder_get_size (int channels);
879 /** Allocates and initializes an encoder state.
880 * There are three coding modes:
882 * @ref OPUS_APPLICATION_VOIP gives best quality at a given bitrate for voice
883 * signals. It enhances the input signal by high-pass filtering and
884 * emphasizing formants and harmonics. Optionally it includes in-band
885 * forward error correction to protect against packet loss. Use this
886 * mode for typical VoIP applications. Because of the enhancement,
887 * even at high bitrates the output may sound different from the input.
889 * @ref OPUS_APPLICATION_AUDIO gives best quality at a given bitrate for most
890 * non-voice signals like music. Use this mode for music and mixed
891 * (music/voice) content, broadcast, and applications requiring less
892 * than 15 ms of coding delay.
894 * @ref OPUS_APPLICATION_RESTRICTED_LOWDELAY configures low-delay mode that
895 * disables the speech-optimized mode in exchange for slightly reduced delay.
896 * This mode can only be set on an newly initialized or freshly reset encoder
897 * because it changes the codec delay.
899 * This is useful when the caller knows that the speech-optimized modes will not be needed (use with caution).
900 * @param [in] Fs <tt>opus_int32</tt>: Sampling rate of input signal (Hz)
901 * This must be one of 8000, 12000, 16000,
902 * 24000, or 48000.
903 * @param [in] channels <tt>int</tt>: Number of channels (1 or 2) in input signal
904 * @param [in] application <tt>int</tt>: Coding mode (@ref OPUS_APPLICATION_VOIP/@ref OPUS_APPLICATION_AUDIO/@ref OPUS_APPLICATION_RESTRICTED_LOWDELAY)
905 * @param [out] error <tt>int*</tt>: @ref opus_errorcodes
906 * @note Regardless of the sampling rate and number channels selected, the Opus encoder
907 * can switch to a lower audio bandwidth or number of channels if the bitrate
908 * selected is too low. This also means that it is safe to always use 48 kHz stereo input
909 * and let the encoder optimize the encoding.
911 OpusEncoder* opus_encoder_create(
912 opus_int32 Fs,
913 int channels,
914 int application,
915 int* error
918 /** Initializes a previously allocated encoder state
919 * The memory pointed to by st must be at least the size returned by opus_encoder_get_size().
920 * This is intended for applications which use their own allocator instead of malloc.
921 * @see opus_encoder_create(),opus_encoder_get_size()
922 * To reset a previously initialized state, use the #OPUS_RESET_STATE CTL.
923 * @param [in] st <tt>OpusEncoder*</tt>: Encoder state
924 * @param [in] Fs <tt>opus_int32</tt>: Sampling rate of input signal (Hz)
925 * This must be one of 8000, 12000, 16000,
926 * 24000, or 48000.
927 * @param [in] channels <tt>int</tt>: Number of channels (1 or 2) in input signal
928 * @param [in] application <tt>int</tt>: Coding mode (OPUS_APPLICATION_VOIP/OPUS_APPLICATION_AUDIO/OPUS_APPLICATION_RESTRICTED_LOWDELAY)
929 * @retval #OPUS_OK Success or @ref opus_errorcodes
931 int opus_encoder_init(
932 OpusEncoder* st,
933 opus_int32 Fs,
934 int channels,
935 int application
936 ) /*OPUS_ARG_NONNULL(1)*/;
938 /** Encodes an Opus frame.
939 * @param [in] st <tt>OpusEncoder*</tt>: Encoder state
940 * @param [in] pcm <tt>opus_int16*</tt>: Input signal (interleaved if 2 channels). length is frame_size*channels*sizeof(opus_int16)
941 * @param [in] frame_size <tt>int</tt>: Number of samples per channel in the
942 * input signal.
943 * This must be an Opus frame size for
944 * the encoder's sampling rate.
945 * For example, at 48 kHz the permitted
946 * values are 120, 240, 480, 960, 1920,
947 * and 2880.
948 * Passing in a duration of less than
949 * 10 ms (480 samples at 48 kHz) will
950 * prevent the encoder from using the LPC
951 * or hybrid modes.
952 * @param [out] data <tt>ubyte*</tt>: Output payload.
953 * This must contain storage for at
954 * least \a max_data_bytes.
955 * @param [in] max_data_bytes <tt>opus_int32</tt>: Size of the allocated
956 * memory for the output
957 * payload. This may be
958 * used to impose an upper limit on
959 * the instant bitrate, but should
960 * not be used as the only bitrate
961 * control. Use #OPUS_SET_BITRATE to
962 * control the bitrate.
963 * @returns The length of the encoded packet (in bytes) on success or a
964 * negative error code (see @ref opus_errorcodes) on failure.
966 opus_int32 opus_encode(
967 OpusEncoder* st,
968 const(opus_int16)* pcm,
969 int frame_size,
970 ubyte* data,
971 opus_int32 max_data_bytes
972 ) /*OPUS_ARG_NONNULL(1)*/ /*OPUS_ARG_NONNULL(2)*/ /*OPUS_ARG_NONNULL(4)*/;
974 /** Encodes an Opus frame from floating point input.
975 * @param [in] st <tt>OpusEncoder*</tt>: Encoder state
976 * @param [in] pcm <tt>float*</tt>: Input in float format (interleaved if 2 channels), with a normal range of +/-1.0.
977 * Samples with a range beyond +/-1.0 are supported but will
978 * be clipped by decoders using the integer API and should
979 * only be used if it is known that the far end supports
980 * extended dynamic range.
981 * length is frame_size*channels*sizeof(float)
982 * @param [in] frame_size <tt>int</tt>: Number of samples per channel in the
983 * input signal.
984 * This must be an Opus frame size for
985 * the encoder's sampling rate.
986 * For example, at 48 kHz the permitted
987 * values are 120, 240, 480, 960, 1920,
988 * and 2880.
989 * Passing in a duration of less than
990 * 10 ms (480 samples at 48 kHz) will
991 * prevent the encoder from using the LPC
992 * or hybrid modes.
993 * @param [out] data <tt>ubyte*</tt>: Output payload.
994 * This must contain storage for at
995 * least \a max_data_bytes.
996 * @param [in] max_data_bytes <tt>opus_int32</tt>: Size of the allocated
997 * memory for the output
998 * payload. This may be
999 * used to impose an upper limit on
1000 * the instant bitrate, but should
1001 * not be used as the only bitrate
1002 * control. Use #OPUS_SET_BITRATE to
1003 * control the bitrate.
1004 * @returns The length of the encoded packet (in bytes) on success or a
1005 * negative error code (see @ref opus_errorcodes) on failure.
1007 opus_int32 opus_encode_float(
1008 OpusEncoder*st,
1009 const(float)* pcm,
1010 int frame_size,
1011 ubyte* data,
1012 opus_int32 max_data_bytes
1013 ) /*OPUS_ARG_NONNULL(1)*/ /*OPUS_ARG_NONNULL(2)*/ /*OPUS_ARG_NONNULL(4)*/;
1015 /** Frees an <code>OpusEncoder</code> allocated by opus_encoder_create().
1016 * @param[in] st <tt>OpusEncoder*</tt>: State to be freed.
1018 void opus_encoder_destroy (OpusEncoder* st);
1020 /** Perform a CTL function on an Opus encoder.
1022 * Generally the request and subsequent arguments are generated
1023 * by a convenience macro.
1024 * @param st <tt>OpusEncoder*</tt>: Encoder state.
1025 * @param request This and all remaining parameters should be replaced by one
1026 * of the convenience macros in @ref opus_genericctls or
1027 * @ref opus_encoderctls.
1028 * @see opus_genericctls
1029 * @see opus_encoderctls
1031 int opus_encoder_ctl (OpusEncoder* st, int request, ...) /*OPUS_ARG_NONNULL(1)*/;
1032 /**@}*/
1034 /** @defgroup opus_decoder Opus Decoder
1035 * @{
1037 * @brief This page describes the process and functions used to decode Opus.
1039 * The decoding process also starts with creating a decoder
1040 * state. This can be done with:
1041 * @code
1042 * int error;
1043 * OpusDecoder *dec;
1044 * dec = opus_decoder_create(Fs, channels, &error);
1045 * @endcode
1046 * where
1047 * @li Fs is the sampling rate and must be 8000, 12000, 16000, 24000, or 48000
1048 * @li channels is the number of channels (1 or 2)
1049 * @li error will hold the error code in case of failure (or #OPUS_OK on success)
1050 * @li the return value is a newly created decoder state to be used for decoding
1052 * While opus_decoder_create() allocates memory for the state, it's also possible
1053 * to initialize pre-allocated memory:
1054 * @code
1055 * int size;
1056 * int error;
1057 * OpusDecoder *dec;
1058 * size = opus_decoder_get_size(channels);
1059 * dec = malloc(size);
1060 * error = opus_decoder_init(dec, Fs, channels);
1061 * @endcode
1062 * where opus_decoder_get_size() returns the required size for the decoder state. Note that
1063 * future versions of this code may change the size, so no assuptions should be made about it.
1065 * The decoder state is always continuous in memory and only a shallow copy is sufficient
1066 * to copy it (e.g. memcpy())
1068 * To decode a frame, opus_decode() or opus_decode_float() must be called with a packet of compressed audio data:
1069 * @code
1070 * frame_size = opus_decode(dec, packet, len, decoded, max_size, 0);
1071 * @endcode
1072 * where
1074 * @li packet is the byte array containing the compressed data
1075 * @li len is the exact number of bytes contained in the packet
1076 * @li decoded is the decoded audio data in opus_int16 (or float for opus_decode_float())
1077 * @li max_size is the max duration of the frame in samples (per channel) that can fit into the decoded_frame array
1079 * opus_decode() and opus_decode_float() return the number of samples (per channel) decoded from the packet.
1080 * If that value is negative, then an error has occurred. This can occur if the packet is corrupted or if the audio
1081 * buffer is too small to hold the decoded audio.
1083 * Opus is a stateful codec with overlapping blocks and as a result Opus
1084 * packets are not coded independently of each other. Packets must be
1085 * passed into the decoder serially and in the correct order for a correct
1086 * decode. Lost packets can be replaced with loss concealment by calling
1087 * the decoder with a null pointer and zero length for the missing packet.
1089 * A single codec state may only be accessed from a single thread at
1090 * a time and any required locking must be performed by the caller. Separate
1091 * streams must be decoded with separate decoder states and can be decoded
1092 * in parallel unless the library was compiled with NONTHREADSAFE_PSEUDOSTACK
1093 * defined.
1097 /** Opus decoder state.
1098 * This contains the complete state of an Opus decoder.
1099 * It is position independent and can be freely copied.
1100 * @see opus_decoder_create,opus_decoder_init
1102 struct OpusDecoder;
1104 /** Gets the size of an <code>OpusDecoder</code> structure.
1105 * @param [in] channels <tt>int</tt>: Number of channels.
1106 * This must be 1 or 2.
1107 * @returns The size in bytes.
1109 int opus_decoder_get_size (int channels);
1111 /** Allocates and initializes a decoder state.
1112 * @param [in] Fs <tt>opus_int32</tt>: Sample rate to decode at (Hz).
1113 * This must be one of 8000, 12000, 16000,
1114 * 24000, or 48000.
1115 * @param [in] channels <tt>int</tt>: Number of channels (1 or 2) to decode
1116 * @param [out] error <tt>int*</tt>: #OPUS_OK Success or @ref opus_errorcodes
1118 * Internally Opus stores data at 48000 Hz, so that should be the default
1119 * value for Fs. However, the decoder can efficiently decode to buffers
1120 * at 8, 12, 16, and 24 kHz so if for some reason the caller cannot use
1121 * data at the full sample rate, or knows the compressed data doesn't
1122 * use the full frequency range, it can request decoding at a reduced
1123 * rate. Likewise, the decoder is capable of filling in either mono or
1124 * interleaved stereo pcm buffers, at the caller's request.
1126 OpusDecoder* opus_decoder_create(
1127 opus_int32 Fs,
1128 int channels,
1129 int* error
1132 /** Initializes a previously allocated decoder state.
1133 * The state must be at least the size returned by opus_decoder_get_size().
1134 * This is intended for applications which use their own allocator instead of malloc. @see opus_decoder_create,opus_decoder_get_size
1135 * To reset a previously initialized state, use the #OPUS_RESET_STATE CTL.
1136 * @param [in] st <tt>OpusDecoder*</tt>: Decoder state.
1137 * @param [in] Fs <tt>opus_int32</tt>: Sampling rate to decode to (Hz).
1138 * This must be one of 8000, 12000, 16000,
1139 * 24000, or 48000.
1140 * @param [in] channels <tt>int</tt>: Number of channels (1 or 2) to decode
1141 * @retval #OPUS_OK Success or @ref opus_errorcodes
1143 int opus_decoder_init(
1144 OpusDecoder* st,
1145 opus_int32 Fs,
1146 int channels
1147 ) /*OPUS_ARG_NONNULL(1)*/;
1149 /** Decode an Opus packet.
1150 * @param [in] st <tt>OpusDecoder*</tt>: Decoder state
1151 * @param [in] data <tt>char*</tt>: Input payload. Use a NULL pointer to indicate packet loss
1152 * @param [in] len <tt>opus_int32</tt>: Number of bytes in payload*
1153 * @param [out] pcm <tt>opus_int16*</tt>: Output signal (interleaved if 2 channels). length
1154 * is frame_size*channels*sizeof(opus_int16)
1155 * @param [in] frame_size Number of samples per channel of available space in \a pcm.
1156 * If this is less than the maximum packet duration (120ms; 5760 for 48kHz), this function will
1157 * not be capable of decoding some packets. In the case of PLC (data==NULL) or FEC (decode_fec=1),
1158 * then frame_size needs to be exactly the duration of audio that is missing, otherwise the
1159 * decoder will not be in the optimal state to decode the next incoming packet. For the PLC and
1160 * FEC cases, frame_size <b>must</b> be a multiple of 2.5 ms.
1161 * @param [in] decode_fec <tt>int</tt>: Flag (0 or 1) to request that any in-band forward error correction data be
1162 * decoded. If no such data is available, the frame is decoded as if it were lost.
1163 * @returns Number of decoded samples or @ref opus_errorcodes
1165 int opus_decode(
1166 OpusDecoder*st,
1167 const(ubyte)* data,
1168 opus_int32 len,
1169 opus_int16* pcm,
1170 int frame_size,
1171 int decode_fec
1172 ) /*OPUS_ARG_NONNULL(1)*/ /*OPUS_ARG_NONNULL(4)*/;
1174 /** Decode an Opus packet with floating point output.
1175 * @param [in] st <tt>OpusDecoder*</tt>: Decoder state
1176 * @param [in] data <tt>char*</tt>: Input payload. Use a NULL pointer to indicate packet loss
1177 * @param [in] len <tt>opus_int32</tt>: Number of bytes in payload
1178 * @param [out] pcm <tt>float*</tt>: Output signal (interleaved if 2 channels). length
1179 * is frame_size*channels*sizeof(float)
1180 * @param [in] frame_size Number of samples per channel of available space in \a pcm.
1181 * If this is less than the maximum packet duration (120ms; 5760 for 48kHz), this function will
1182 * not be capable of decoding some packets. In the case of PLC (data==NULL) or FEC (decode_fec=1),
1183 * then frame_size needs to be exactly the duration of audio that is missing, otherwise the
1184 * decoder will not be in the optimal state to decode the next incoming packet. For the PLC and
1185 * FEC cases, frame_size <b>must</b> be a multiple of 2.5 ms.
1186 * @param [in] decode_fec <tt>int</tt>: Flag (0 or 1) to request that any in-band forward error correction data be
1187 * decoded. If no such data is available the frame is decoded as if it were lost.
1188 * @returns Number of decoded samples or @ref opus_errorcodes
1190 int opus_decode_float(
1191 OpusDecoder*st,
1192 const(ubyte)* data,
1193 opus_int32 len,
1194 float* pcm,
1195 int frame_size,
1196 int decode_fec
1197 ) /*OPUS_ARG_NONNULL(1)*/ /*OPUS_ARG_NONNULL(4)*/;
1199 /** Perform a CTL function on an Opus decoder.
1201 * Generally the request and subsequent arguments are generated
1202 * by a convenience macro.
1203 * @param st <tt>OpusDecoder*</tt>: Decoder state.
1204 * @param request This and all remaining parameters should be replaced by one
1205 * of the convenience macros in @ref opus_genericctls or
1206 * @ref opus_decoderctls.
1207 * @see opus_genericctls
1208 * @see opus_decoderctls
1210 int opus_decoder_ctl (OpusDecoder* st, int request, ...) /*OPUS_ARG_NONNULL(1)*/;
1212 /** Frees an <code>OpusDecoder</code> allocated by opus_decoder_create().
1213 * @param[in] st <tt>OpusDecoder*</tt>: State to be freed.
1215 void opus_decoder_destroy (OpusDecoder* st);
1217 /** Parse an opus packet into one or more frames.
1218 * Opus_decode will perform this operation internally so most applications do
1219 * not need to use this function.
1220 * This function does not copy the frames, the returned pointers are pointers into
1221 * the input packet.
1222 * @param [in] data <tt>char*</tt>: Opus packet to be parsed
1223 * @param [in] len <tt>opus_int32</tt>: size of data
1224 * @param [out] out_toc <tt>char*</tt>: TOC pointer
1225 * @param [out] frames <tt>char*[48]</tt> encapsulated frames
1226 * @param [out] size <tt>opus_int16[48]</tt> sizes of the encapsulated frames
1227 * @param [out] payload_offset <tt>int*</tt>: returns the position of the payload within the packet (in bytes)
1228 * @returns number of frames
1230 int opus_packet_parse(
1231 const(ubyte)* data,
1232 opus_int32 len,
1233 ubyte* out_toc,
1234 const(ubyte)** frames/*[48]*/,
1235 opus_int16* size/*[48]*/,
1236 int* payload_offset
1237 ) /*OPUS_ARG_NONNULL(1)*/ /*OPUS_ARG_NONNULL(4)*/;
1239 /** Gets the bandwidth of an Opus packet.
1240 * @param [in] data <tt>char*</tt>: Opus packet
1241 * @retval OPUS_BANDWIDTH_NARROWBAND Narrowband (4kHz bandpass)
1242 * @retval OPUS_BANDWIDTH_MEDIUMBAND Mediumband (6kHz bandpass)
1243 * @retval OPUS_BANDWIDTH_WIDEBAND Wideband (8kHz bandpass)
1244 * @retval OPUS_BANDWIDTH_SUPERWIDEBAND Superwideband (12kHz bandpass)
1245 * @retval OPUS_BANDWIDTH_FULLBAND Fullband (20kHz bandpass)
1246 * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type
1248 int opus_packet_get_bandwidth (const(ubyte)* data) /*OPUS_ARG_NONNULL(1)*/;
1250 /** Gets the number of samples per frame from an Opus packet.
1251 * @param [in] data <tt>char*</tt>: Opus packet.
1252 * This must contain at least one byte of
1253 * data.
1254 * @param [in] Fs <tt>opus_int32</tt>: Sampling rate in Hz.
1255 * This must be a multiple of 400, or
1256 * inaccurate results will be returned.
1257 * @returns Number of samples per frame.
1259 int opus_packet_get_samples_per_frame (const(ubyte)* data, opus_int32 Fs) /*OPUS_ARG_NONNULL(1)*/;
1261 /** Gets the number of channels from an Opus packet.
1262 * @param [in] data <tt>char*</tt>: Opus packet
1263 * @returns Number of channels
1264 * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type
1266 int opus_packet_get_nb_channels (const(ubyte)* data) /*OPUS_ARG_NONNULL(1)*/;
1268 /** Gets the number of frames in an Opus packet.
1269 * @param [in] packet <tt>char*</tt>: Opus packet
1270 * @param [in] len <tt>opus_int32</tt>: Length of packet
1271 * @returns Number of frames
1272 * @retval OPUS_BAD_ARG Insufficient data was passed to the function
1273 * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type
1275 int opus_packet_get_nb_frames (const(ubyte)* packet, opus_int32 len) /*OPUS_ARG_NONNULL(1)*/;
1277 /** Gets the number of samples of an Opus packet.
1278 * @param [in] packet <tt>char*</tt>: Opus packet
1279 * @param [in] len <tt>opus_int32</tt>: Length of packet
1280 * @param [in] Fs <tt>opus_int32</tt>: Sampling rate in Hz.
1281 * This must be a multiple of 400, or
1282 * inaccurate results will be returned.
1283 * @returns Number of samples
1284 * @retval OPUS_BAD_ARG Insufficient data was passed to the function
1285 * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type
1287 int opus_packet_get_nb_samples (const(ubyte)* packet, opus_int32 len, opus_int32 Fs) /*OPUS_ARG_NONNULL(1)*/;
1289 /** Gets the number of samples of an Opus packet.
1290 * @param [in] dec <tt>OpusDecoder*</tt>: Decoder state
1291 * @param [in] packet <tt>char*</tt>: Opus packet
1292 * @param [in] len <tt>opus_int32</tt>: Length of packet
1293 * @returns Number of samples
1294 * @retval OPUS_BAD_ARG Insufficient data was passed to the function
1295 * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type
1297 int opus_decoder_get_nb_samples (const(OpusDecoder)* dec, const(ubyte)* packet, opus_int32 len) /*OPUS_ARG_NONNULL(1)*/ /*OPUS_ARG_NONNULL(2)*/;
1299 /** Applies soft-clipping to bring a float signal within the [-1,1] range. If
1300 * the signal is already in that range, nothing is done. If there are values
1301 * outside of [-1,1], then the signal is clipped as smoothly as possible to
1302 * both fit in the range and avoid creating excessive distortion in the
1303 * process.
1304 * @param [in,out] pcm <tt>float*</tt>: Input PCM and modified PCM
1305 * @param [in] frame_size <tt>int</tt> Number of samples per channel to process
1306 * @param [in] channels <tt>int</tt>: Number of channels
1307 * @param [in,out] softclip_mem <tt>float*</tt>: State memory for the soft clipping process (one float per channel, initialized to zero)
1309 void opus_pcm_soft_clip (float* pcm, int frame_size, int channels, float* softclip_mem);
1312 /**@}*/
1314 /** @defgroup opus_repacketizer Repacketizer
1315 * @{
1317 * The repacketizer can be used to merge multiple Opus packets into a single
1318 * packet or alternatively to split Opus packets that have previously been
1319 * merged. Splitting valid Opus packets is always guaranteed to succeed,
1320 * whereas merging valid packets only succeeds if all frames have the same
1321 * mode, bandwidth, and frame size, and when the total duration of the merged
1322 * packet is no more than 120 ms. The 120 ms limit comes from the
1323 * specification and limits decoder memory requirements at a point where
1324 * framing overhead becomes negligible.
1326 * The repacketizer currently only operates on elementary Opus
1327 * streams. It will not manipualte multistream packets successfully, except in
1328 * the degenerate case where they consist of data from a single stream.
1330 * The repacketizing process starts with creating a repacketizer state, either
1331 * by calling opus_repacketizer_create() or by allocating the memory yourself,
1332 * e.g.,
1333 * @code
1334 * OpusRepacketizer *rp;
1335 * rp = (OpusRepacketizer*)malloc(opus_repacketizer_get_size());
1336 * if (rp != NULL)
1337 * opus_repacketizer_init(rp);
1338 * @endcode
1340 * Then the application should submit packets with opus_repacketizer_cat(),
1341 * extract new packets with opus_repacketizer_out() or
1342 * opus_repacketizer_out_range(), and then reset the state for the next set of
1343 * input packets via opus_repacketizer_init().
1345 * For example, to split a sequence of packets into individual frames:
1346 * @code
1347 * ubyte *data;
1348 * int len;
1349 * while (get_next_packet(&data, &len))
1351 * ubyte out[1276];
1352 * opus_int32 out_len;
1353 * int nb_frames;
1354 * int err;
1355 * int i;
1356 * err = opus_repacketizer_cat(rp, data, len);
1357 * if (err != OPUS_OK)
1359 * release_packet(data);
1360 * return err;
1362 * nb_frames = opus_repacketizer_get_nb_frames(rp);
1363 * for (i = 0; i < nb_frames; i++)
1365 * out_len = opus_repacketizer_out_range(rp, i, i+1, out, sizeof(out));
1366 * if (out_len < 0)
1368 * release_packet(data);
1369 * return (int)out_len;
1371 * output_next_packet(out, out_len);
1373 * opus_repacketizer_init(rp);
1374 * release_packet(data);
1376 * @endcode
1378 * Alternatively, to combine a sequence of frames into packets that each
1379 * contain up to <code>TARGET_DURATION_MS</code> milliseconds of data:
1380 * @code
1381 * // The maximum number of packets with duration TARGET_DURATION_MS occurs
1382 * // when the frame size is 2.5 ms, for a total of (TARGET_DURATION_MS*2/5)
1383 * // packets.
1384 * ubyte *data[(TARGET_DURATION_MS*2/5)+1];
1385 * opus_int32 len[(TARGET_DURATION_MS*2/5)+1];
1386 * int nb_packets;
1387 * ubyte out[1277*(TARGET_DURATION_MS*2/2)];
1388 * opus_int32 out_len;
1389 * int prev_toc;
1390 * nb_packets = 0;
1391 * while (get_next_packet(data+nb_packets, len+nb_packets))
1393 * int nb_frames;
1394 * int err;
1395 * nb_frames = opus_packet_get_nb_frames(data[nb_packets], len[nb_packets]);
1396 * if (nb_frames < 1)
1398 * release_packets(data, nb_packets+1);
1399 * return nb_frames;
1401 * nb_frames += opus_repacketizer_get_nb_frames(rp);
1402 * // If adding the next packet would exceed our target, or it has an
1403 * // incompatible TOC sequence, output the packets we already have before
1404 * // submitting it.
1405 * // N.B., The nb_packets > 0 check ensures we've submitted at least one
1406 * // packet since the last call to opus_repacketizer_init(). Otherwise a
1407 * // single packet longer than TARGET_DURATION_MS would cause us to try to
1408 * // output an (invalid) empty packet. It also ensures that prev_toc has
1409 * // been set to a valid value. Additionally, len[nb_packets] > 0 is
1410 * // guaranteed by the call to opus_packet_get_nb_frames() above, so the
1411 * // reference to data[nb_packets][0] should be valid.
1412 * if (nb_packets > 0 && (
1413 * ((prev_toc & 0xFC) != (data[nb_packets][0] & 0xFC)) ||
1414 * opus_packet_get_samples_per_frame(data[nb_packets], 48000)*nb_frames >
1415 * TARGET_DURATION_MS*48))
1417 * out_len = opus_repacketizer_out(rp, out, sizeof(out));
1418 * if (out_len < 0)
1420 * release_packets(data, nb_packets+1);
1421 * return (int)out_len;
1423 * output_next_packet(out, out_len);
1424 * opus_repacketizer_init(rp);
1425 * release_packets(data, nb_packets);
1426 * data[0] = data[nb_packets];
1427 * len[0] = len[nb_packets];
1428 * nb_packets = 0;
1430 * err = opus_repacketizer_cat(rp, data[nb_packets], len[nb_packets]);
1431 * if (err != OPUS_OK)
1433 * release_packets(data, nb_packets+1);
1434 * return err;
1436 * prev_toc = data[nb_packets][0];
1437 * nb_packets++;
1439 * // Output the final, partial packet.
1440 * if (nb_packets > 0)
1442 * out_len = opus_repacketizer_out(rp, out, sizeof(out));
1443 * release_packets(data, nb_packets);
1444 * if (out_len < 0)
1445 * return (int)out_len;
1446 * output_next_packet(out, out_len);
1448 * @endcode
1450 * An alternate way of merging packets is to simply call opus_repacketizer_cat()
1451 * unconditionally until it fails. At that point, the merged packet can be
1452 * obtained with opus_repacketizer_out() and the input packet for which
1453 * opus_repacketizer_cat() needs to be re-added to a newly reinitialized
1454 * repacketizer state.
1457 struct OpusRepacketizer;
1459 /** Gets the size of an <code>OpusRepacketizer</code> structure.
1460 * @returns The size in bytes.
1462 int opus_repacketizer_get_size ();
1464 /** (Re)initializes a previously allocated repacketizer state.
1465 * The state must be at least the size returned by opus_repacketizer_get_size().
1466 * This can be used for applications which use their own allocator instead of
1467 * malloc().
1468 * It must also be called to reset the queue of packets waiting to be
1469 * repacketized, which is necessary if the maximum packet duration of 120 ms
1470 * is reached or if you wish to submit packets with a different Opus
1471 * configuration (coding mode, audio bandwidth, frame size, or channel count).
1472 * Failure to do so will prevent a new packet from being added with
1473 * opus_repacketizer_cat().
1474 * @see opus_repacketizer_create
1475 * @see opus_repacketizer_get_size
1476 * @see opus_repacketizer_cat
1477 * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state to
1478 * (re)initialize.
1479 * @returns A pointer to the same repacketizer state that was passed in.
1481 OpusRepacketizer* opus_repacketizer_init (OpusRepacketizer* rp) /*OPUS_ARG_NONNULL(1)*/;
1483 /** Allocates memory and initializes the new repacketizer with
1484 * opus_repacketizer_init().
1486 OpusRepacketizer* opus_repacketizer_create ();
1488 /** Frees an <code>OpusRepacketizer</code> allocated by
1489 * opus_repacketizer_create().
1490 * @param[in] rp <tt>OpusRepacketizer*</tt>: State to be freed.
1492 void opus_repacketizer_destroy (OpusRepacketizer* rp);
1494 /** Add a packet to the current repacketizer state.
1495 * This packet must match the configuration of any packets already submitted
1496 * for repacketization since the last call to opus_repacketizer_init().
1497 * This means that it must have the same coding mode, audio bandwidth, frame
1498 * size, and channel count.
1499 * This can be checked in advance by examining the top 6 bits of the first
1500 * byte of the packet, and ensuring they match the top 6 bits of the first
1501 * byte of any previously submitted packet.
1502 * The total duration of audio in the repacketizer state also must not exceed
1503 * 120 ms, the maximum duration of a single packet, after adding this packet.
1505 * The contents of the current repacketizer state can be extracted into new
1506 * packets using opus_repacketizer_out() or opus_repacketizer_out_range().
1508 * In order to add a packet with a different configuration or to add more
1509 * audio beyond 120 ms, you must clear the repacketizer state by calling
1510 * opus_repacketizer_init().
1511 * If a packet is too large to add to the current repacketizer state, no part
1512 * of it is added, even if it contains multiple frames, some of which might
1513 * fit.
1514 * If you wish to be able to add parts of such packets, you should first use
1515 * another repacketizer to split the packet into pieces and add them
1516 * individually.
1517 * @see opus_repacketizer_out_range
1518 * @see opus_repacketizer_out
1519 * @see opus_repacketizer_init
1520 * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state to which to
1521 * add the packet.
1522 * @param[in] data <tt>const ubyte*</tt>: The packet data.
1523 * The application must ensure
1524 * this pointer remains valid
1525 * until the next call to
1526 * opus_repacketizer_init() or
1527 * opus_repacketizer_destroy().
1528 * @param len <tt>opus_int32</tt>: The number of bytes in the packet data.
1529 * @returns An error code indicating whether or not the operation succeeded.
1530 * @retval #OPUS_OK The packet's contents have been added to the repacketizer
1531 * state.
1532 * @retval #OPUS_INVALID_PACKET The packet did not have a valid TOC sequence,
1533 * the packet's TOC sequence was not compatible
1534 * with previously submitted packets (because
1535 * the coding mode, audio bandwidth, frame size,
1536 * or channel count did not match), or adding
1537 * this packet would increase the total amount of
1538 * audio stored in the repacketizer state to more
1539 * than 120 ms.
1541 int opus_repacketizer_cat (OpusRepacketizer* rp, const(ubyte)* data, opus_int32 len) /*OPUS_ARG_NONNULL(1)*/ /*OPUS_ARG_NONNULL(2)*/;
1544 /** Construct a new packet from data previously submitted to the repacketizer
1545 * state via opus_repacketizer_cat().
1546 * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state from which to
1547 * construct the new packet.
1548 * @param begin <tt>int</tt>: The index of the first frame in the current
1549 * repacketizer state to include in the output.
1550 * @param end <tt>int</tt>: One past the index of the last frame in the
1551 * current repacketizer state to include in the
1552 * output.
1553 * @param[out] data <tt>const ubyte*</tt>: The buffer in which to
1554 * store the output packet.
1555 * @param maxlen <tt>opus_int32</tt>: The maximum number of bytes to store in
1556 * the output buffer. In order to guarantee
1557 * success, this should be at least
1558 * <code>1276</code> for a single frame,
1559 * or for multiple frames,
1560 * <code>1277*(end-begin)</code>.
1561 * However, <code>1*(end-begin)</code> plus
1562 * the size of all packet data submitted to
1563 * the repacketizer since the last call to
1564 * opus_repacketizer_init() or
1565 * opus_repacketizer_create() is also
1566 * sufficient, and possibly much smaller.
1567 * @returns The total size of the output packet on success, or an error code
1568 * on failure.
1569 * @retval #OPUS_BAD_ARG <code>[begin,end)</code> was an invalid range of
1570 * frames (begin < 0, begin >= end, or end >
1571 * opus_repacketizer_get_nb_frames()).
1572 * @retval #OPUS_BUFFER_TOO_SMALL \a maxlen was insufficient to contain the
1573 * complete output packet.
1575 opus_int32 opus_repacketizer_out_range (OpusRepacketizer* rp, int begin, int end, ubyte* data, opus_int32 maxlen) /*OPUS_ARG_NONNULL(1)*/ /*OPUS_ARG_NONNULL(4)*/;
1577 /** Return the total number of frames contained in packet data submitted to
1578 * the repacketizer state so far via opus_repacketizer_cat() since the last
1579 * call to opus_repacketizer_init() or opus_repacketizer_create().
1580 * This defines the valid range of packets that can be extracted with
1581 * opus_repacketizer_out_range() or opus_repacketizer_out().
1582 * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state containing the
1583 * frames.
1584 * @returns The total number of frames contained in the packet data submitted
1585 * to the repacketizer state.
1587 int opus_repacketizer_get_nb_frames (OpusRepacketizer* rp) /*OPUS_ARG_NONNULL(1)*/;
1589 /** Construct a new packet from data previously submitted to the repacketizer
1590 * state via opus_repacketizer_cat().
1591 * This is a convenience routine that returns all the data submitted so far
1592 * in a single packet.
1593 * It is equivalent to calling
1594 * @code
1595 * opus_repacketizer_out_range(rp, 0, opus_repacketizer_get_nb_frames(rp),
1596 * data, maxlen)
1597 * @endcode
1598 * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state from which to
1599 * construct the new packet.
1600 * @param[out] data <tt>const ubyte*</tt>: The buffer in which to
1601 * store the output packet.
1602 * @param maxlen <tt>opus_int32</tt>: The maximum number of bytes to store in
1603 * the output buffer. In order to guarantee
1604 * success, this should be at least
1605 * <code>1277*opus_repacketizer_get_nb_frames(rp)</code>.
1606 * However,
1607 * <code>1*opus_repacketizer_get_nb_frames(rp)</code>
1608 * plus the size of all packet data
1609 * submitted to the repacketizer since the
1610 * last call to opus_repacketizer_init() or
1611 * opus_repacketizer_create() is also
1612 * sufficient, and possibly much smaller.
1613 * @returns The total size of the output packet on success, or an error code
1614 * on failure.
1615 * @retval #OPUS_BUFFER_TOO_SMALL \a maxlen was insufficient to contain the
1616 * complete output packet.
1618 opus_int32 opus_repacketizer_out (OpusRepacketizer* rp, ubyte* data, opus_int32 maxlen) /*OPUS_ARG_NONNULL(1)*/;
1620 /** Pads a given Opus packet to a larger size (possibly changing the TOC sequence).
1621 * @param[in,out] data <tt>const ubyte*</tt>: The buffer containing the
1622 * packet to pad.
1623 * @param len <tt>opus_int32</tt>: The size of the packet.
1624 * This must be at least 1.
1625 * @param new_len <tt>opus_int32</tt>: The desired size of the packet after padding.
1626 * This must be at least as large as len.
1627 * @returns an error code
1628 * @retval #OPUS_OK \a on success.
1629 * @retval #OPUS_BAD_ARG \a len was less than 1 or new_len was less than len.
1630 * @retval #OPUS_INVALID_PACKET \a data did not contain a valid Opus packet.
1632 int opus_packet_pad (ubyte* data, opus_int32 len, opus_int32 new_len);
1634 /** Remove all padding from a given Opus packet and rewrite the TOC sequence to
1635 * minimize space usage.
1636 * @param[in,out] data <tt>const ubyte*</tt>: The buffer containing the
1637 * packet to strip.
1638 * @param len <tt>opus_int32</tt>: The size of the packet.
1639 * This must be at least 1.
1640 * @returns The new size of the output packet on success, or an error code
1641 * on failure.
1642 * @retval #OPUS_BAD_ARG \a len was less than 1.
1643 * @retval #OPUS_INVALID_PACKET \a data did not contain a valid Opus packet.
1645 opus_int32 opus_packet_unpad (ubyte* data, opus_int32 len);
1647 /** Pads a given Opus multi-stream packet to a larger size (possibly changing the TOC sequence).
1648 * @param[in,out] data <tt>const ubyte*</tt>: The buffer containing the
1649 * packet to pad.
1650 * @param len <tt>opus_int32</tt>: The size of the packet.
1651 * This must be at least 1.
1652 * @param new_len <tt>opus_int32</tt>: The desired size of the packet after padding.
1653 * This must be at least 1.
1654 * @param nb_streams <tt>opus_int32</tt>: The number of streams (not channels) in the packet.
1655 * This must be at least as large as len.
1656 * @returns an error code
1657 * @retval #OPUS_OK \a on success.
1658 * @retval #OPUS_BAD_ARG \a len was less than 1.
1659 * @retval #OPUS_INVALID_PACKET \a data did not contain a valid Opus packet.
1661 int opus_multistream_packet_pad (ubyte* data, opus_int32 len, opus_int32 new_len, int nb_streams);
1663 /** Remove all padding from a given Opus multi-stream packet and rewrite the TOC sequence to
1664 * minimize space usage.
1665 * @param[in,out] data <tt>const ubyte*</tt>: The buffer containing the
1666 * packet to strip.
1667 * @param len <tt>opus_int32</tt>: The size of the packet.
1668 * This must be at least 1.
1669 * @param nb_streams <tt>opus_int32</tt>: The number of streams (not channels) in the packet.
1670 * This must be at least 1.
1671 * @returns The new size of the output packet on success, or an error code
1672 * on failure.
1673 * @retval #OPUS_BAD_ARG \a len was less than 1 or new_len was less than len.
1674 * @retval #OPUS_INVALID_PACKET \a data did not contain a valid Opus packet.
1676 opus_int32 opus_multistream_packet_unpad (ubyte* data, opus_int32 len, int nb_streams);
1678 /**@}*/
1682 * @file opus_multistream.h
1683 * @brief Opus reference implementation multistream API
1687 /** These are the actual encoder and decoder CTL ID numbers.
1688 * They should not be used directly by applications.
1689 * In general, SETs should be even and GETs should be odd.*/
1690 /**@{*/
1691 enum OPUS_MULTISTREAM_GET_ENCODER_STATE_REQUEST = 5120;
1692 enum OPUS_MULTISTREAM_GET_DECODER_STATE_REQUEST = 5122;
1693 /**@}*/
1695 /** @endcond */
1697 /** @defgroup opus_multistream_ctls Multistream specific encoder and decoder CTLs
1699 * These are convenience macros that are specific to the
1700 * opus_multistream_encoder_ctl() and opus_multistream_decoder_ctl()
1701 * interface.
1702 * The CTLs from @ref opus_genericctls, @ref opus_encoderctls, and
1703 * @ref opus_decoderctls may be applied to a multistream encoder or decoder as
1704 * well.
1705 * In addition, you may retrieve the encoder or decoder state for an specific
1706 * stream via #OPUS_MULTISTREAM_GET_ENCODER_STATE or
1707 * #OPUS_MULTISTREAM_GET_DECODER_STATE and apply CTLs to it individually.
1709 /**@{*/
1711 /+TODO
1712 /** Gets the encoder state for an individual stream of a multistream encoder.
1713 * @param[in] x <tt>opus_int32</tt>: The index of the stream whose encoder you
1714 * wish to retrieve.
1715 * This must be non-negative and less than
1716 * the <code>streams</code> parameter used
1717 * to initialize the encoder.
1718 * @param[out] y <tt>OpusEncoder**</tt>: Returns a pointer to the given
1719 * encoder state.
1720 * @retval OPUS_BAD_ARG The index of the requested stream was out of range.
1721 * @hideinitializer
1723 #define OPUS_MULTISTREAM_GET_ENCODER_STATE(x,y) OPUS_MULTISTREAM_GET_ENCODER_STATE_REQUEST, __opus_check_int(x), __opus_check_encstate_ptr(y)
1725 /** Gets the decoder state for an individual stream of a multistream decoder.
1726 * @param[in] x <tt>opus_int32</tt>: The index of the stream whose decoder you
1727 * wish to retrieve.
1728 * This must be non-negative and less than
1729 * the <code>streams</code> parameter used
1730 * to initialize the decoder.
1731 * @param[out] y <tt>OpusDecoder**</tt>: Returns a pointer to the given
1732 * decoder state.
1733 * @retval OPUS_BAD_ARG The index of the requested stream was out of range.
1734 * @hideinitializer
1736 #define OPUS_MULTISTREAM_GET_DECODER_STATE(x,y) OPUS_MULTISTREAM_GET_DECODER_STATE_REQUEST, __opus_check_int(x), __opus_check_decstate_ptr(y)
1738 /**@}*/
1740 /** @defgroup opus_multistream Opus Multistream API
1741 * @{
1743 * The multistream API allows individual Opus streams to be combined into a
1744 * single packet, enabling support for up to 255 channels. Unlike an
1745 * elementary Opus stream, the encoder and decoder must negotiate the channel
1746 * configuration before the decoder can successfully interpret the data in the
1747 * packets produced by the encoder. Some basic information, such as packet
1748 * duration, can be computed without any special negotiation.
1750 * The format for multistream Opus packets is defined in
1751 * <a href="https://tools.ietf.org/html/rfc7845">RFC 7845</a>
1752 * and is based on the self-delimited Opus framing described in Appendix B of
1753 * <a href="https://tools.ietf.org/html/rfc6716">RFC 6716</a>.
1754 * Normal Opus packets are just a degenerate case of multistream Opus packets,
1755 * and can be encoded or decoded with the multistream API by setting
1756 * <code>streams</code> to <code>1</code> when initializing the encoder or
1757 * decoder.
1759 * Multistream Opus streams can contain up to 255 elementary Opus streams.
1760 * These may be either "uncoupled" or "coupled", indicating that the decoder
1761 * is configured to decode them to either 1 or 2 channels, respectively.
1762 * The streams are ordered so that all coupled streams appear at the
1763 * beginning.
1765 * A <code>mapping</code> table defines which decoded channel <code>i</code>
1766 * should be used for each input/output (I/O) channel <code>j</code>. This table is
1767 * typically provided as an ubyte array.
1768 * Let <code>i = mapping[j]</code> be the index for I/O channel <code>j</code>.
1769 * If <code>i < 2*coupled_streams</code>, then I/O channel <code>j</code> is
1770 * encoded as the left channel of stream <code>(i/2)</code> if <code>i</code>
1771 * is even, or as the right channel of stream <code>(i/2)</code> if
1772 * <code>i</code> is odd. Otherwise, I/O channel <code>j</code> is encoded as
1773 * mono in stream <code>(i - coupled_streams)</code>, unless it has the special
1774 * value 255, in which case it is omitted from the encoding entirely (the
1775 * decoder will reproduce it as silence). Each value <code>i</code> must either
1776 * be the special value 255 or be less than <code>streams + coupled_streams</code>.
1778 * The output channels specified by the encoder
1779 * should use the
1780 * <a href="https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-810004.3.9">Vorbis
1781 * channel ordering</a>. A decoder may wish to apply an additional permutation
1782 * to the mapping the encoder used to achieve a different output channel
1783 * order (e.g. for outputing in WAV order).
1785 * Each multistream packet contains an Opus packet for each stream, and all of
1786 * the Opus packets in a single multistream packet must have the same
1787 * duration. Therefore the duration of a multistream packet can be extracted
1788 * from the TOC sequence of the first stream, which is located at the
1789 * beginning of the packet, just like an elementary Opus stream:
1791 * @code
1792 * int nb_samples;
1793 * int nb_frames;
1794 * nb_frames = opus_packet_get_nb_frames(data, len);
1795 * if (nb_frames < 1)
1796 * return nb_frames;
1797 * nb_samples = opus_packet_get_samples_per_frame(data, 48000) * nb_frames;
1798 * @endcode
1800 * The general encoding and decoding process proceeds exactly the same as in
1801 * the normal @ref opus_encoder and @ref opus_decoder APIs.
1802 * See their documentation for an overview of how to use the corresponding
1803 * multistream functions.
1806 /** Opus multistream encoder state.
1807 * This contains the complete state of a multistream Opus encoder.
1808 * It is position independent and can be freely copied.
1809 * @see opus_multistream_encoder_create
1810 * @see opus_multistream_encoder_init
1812 struct OpusMSEncoder;
1814 /** Opus multistream decoder state.
1815 * This contains the complete state of a multistream Opus decoder.
1816 * It is position independent and can be freely copied.
1817 * @see opus_multistream_decoder_create
1818 * @see opus_multistream_decoder_init
1820 struct OpusMSDecoder;
1822 /**\name Multistream encoder functions */
1823 /**@{*/
1825 /** Gets the size of an OpusMSEncoder structure.
1826 * @param streams <tt>int</tt>: The total number of streams to encode from the
1827 * input.
1828 * This must be no more than 255.
1829 * @param coupled_streams <tt>int</tt>: Number of coupled (2 channel) streams
1830 * to encode.
1831 * This must be no larger than the total
1832 * number of streams.
1833 * Additionally, The total number of
1834 * encoded channels (<code>streams +
1835 * coupled_streams</code>) must be no
1836 * more than 255.
1837 * @returns The size in bytes on success, or a negative error code
1838 * (see @ref opus_errorcodes) on error.
1840 opus_int32 opus_multistream_encoder_get_size(
1841 int streams,
1842 int coupled_streams
1845 opus_int32 opus_multistream_surround_encoder_get_size(
1846 int channels,
1847 int mapping_family
1851 /** Allocates and initializes a multistream encoder state.
1852 * Call opus_multistream_encoder_destroy() to release
1853 * this object when finished.
1854 * @param Fs <tt>opus_int32</tt>: Sampling rate of the input signal (in Hz).
1855 * This must be one of 8000, 12000, 16000,
1856 * 24000, or 48000.
1857 * @param channels <tt>int</tt>: Number of channels in the input signal.
1858 * This must be at most 255.
1859 * It may be greater than the number of
1860 * coded channels (<code>streams +
1861 * coupled_streams</code>).
1862 * @param streams <tt>int</tt>: The total number of streams to encode from the
1863 * input.
1864 * This must be no more than the number of channels.
1865 * @param coupled_streams <tt>int</tt>: Number of coupled (2 channel) streams
1866 * to encode.
1867 * This must be no larger than the total
1868 * number of streams.
1869 * Additionally, The total number of
1870 * encoded channels (<code>streams +
1871 * coupled_streams</code>) must be no
1872 * more than the number of input channels.
1873 * @param[in] mapping <code>const ubyte[channels]</code>: Mapping from
1874 * encoded channels to input channels, as described in
1875 * @ref opus_multistream. As an extra constraint, the
1876 * multistream encoder does not allow encoding coupled
1877 * streams for which one channel is unused since this
1878 * is never a good idea.
1879 * @param application <tt>int</tt>: The target encoder application.
1880 * This must be one of the following:
1881 * <dl>
1882 * <dt>#OPUS_APPLICATION_VOIP</dt>
1883 * <dd>Process signal for improved speech intelligibility.</dd>
1884 * <dt>#OPUS_APPLICATION_AUDIO</dt>
1885 * <dd>Favor faithfulness to the original input.</dd>
1886 * <dt>#OPUS_APPLICATION_RESTRICTED_LOWDELAY</dt>
1887 * <dd>Configure the minimum possible coding delay by disabling certain modes
1888 * of operation.</dd>
1889 * </dl>
1890 * @param[out] error <tt>int *</tt>: Returns #OPUS_OK on success, or an error
1891 * code (see @ref opus_errorcodes) on
1892 * failure.
1894 OpusMSEncoder* opus_multistream_encoder_create(
1895 opus_int32 Fs,
1896 int channels,
1897 int streams,
1898 int coupled_streams,
1899 const(ubyte)* mapping,
1900 int application,
1901 int* error
1902 ) /*OPUS_ARG_NONNULL(5)*/;
1904 OpusMSEncoder* opus_multistream_surround_encoder_create(
1905 opus_int32 Fs,
1906 int channels,
1907 int mapping_family,
1908 int* streams,
1909 int* coupled_streams,
1910 ubyte* mapping,
1911 int application,
1912 int* error
1913 ) /*OPUS_ARG_NONNULL(5)*/;
1915 /** Initialize a previously allocated multistream encoder state.
1916 * The memory pointed to by \a st must be at least the size returned by
1917 * opus_multistream_encoder_get_size().
1918 * This is intended for applications which use their own allocator instead of
1919 * malloc.
1920 * To reset a previously initialized state, use the #OPUS_RESET_STATE CTL.
1921 * @see opus_multistream_encoder_create
1922 * @see opus_multistream_encoder_get_size
1923 * @param st <tt>OpusMSEncoder*</tt>: Multistream encoder state to initialize.
1924 * @param Fs <tt>opus_int32</tt>: Sampling rate of the input signal (in Hz).
1925 * This must be one of 8000, 12000, 16000,
1926 * 24000, or 48000.
1927 * @param channels <tt>int</tt>: Number of channels in the input signal.
1928 * This must be at most 255.
1929 * It may be greater than the number of
1930 * coded channels (<code>streams +
1931 * coupled_streams</code>).
1932 * @param streams <tt>int</tt>: The total number of streams to encode from the
1933 * input.
1934 * This must be no more than the number of channels.
1935 * @param coupled_streams <tt>int</tt>: Number of coupled (2 channel) streams
1936 * to encode.
1937 * This must be no larger than the total
1938 * number of streams.
1939 * Additionally, The total number of
1940 * encoded channels (<code>streams +
1941 * coupled_streams</code>) must be no
1942 * more than the number of input channels.
1943 * @param[in] mapping <code>const ubyte[channels]</code>: Mapping from
1944 * encoded channels to input channels, as described in
1945 * @ref opus_multistream. As an extra constraint, the
1946 * multistream encoder does not allow encoding coupled
1947 * streams for which one channel is unused since this
1948 * is never a good idea.
1949 * @param application <tt>int</tt>: The target encoder application.
1950 * This must be one of the following:
1951 * <dl>
1952 * <dt>#OPUS_APPLICATION_VOIP</dt>
1953 * <dd>Process signal for improved speech intelligibility.</dd>
1954 * <dt>#OPUS_APPLICATION_AUDIO</dt>
1955 * <dd>Favor faithfulness to the original input.</dd>
1956 * <dt>#OPUS_APPLICATION_RESTRICTED_LOWDELAY</dt>
1957 * <dd>Configure the minimum possible coding delay by disabling certain modes
1958 * of operation.</dd>
1959 * </dl>
1960 * @returns #OPUS_OK on success, or an error code (see @ref opus_errorcodes)
1961 * on failure.
1963 int opus_multistream_encoder_init(
1964 OpusMSEncoder* st,
1965 opus_int32 Fs,
1966 int channels,
1967 int streams,
1968 int coupled_streams,
1969 const(ubyte)* mapping,
1970 int application
1971 ) /*OPUS_ARG_NONNULL(1)*/ /*OPUS_ARG_NONNULL(6)*/;
1973 int opus_multistream_surround_encoder_init(
1974 OpusMSEncoder* st,
1975 opus_int32 Fs,
1976 int channels,
1977 int mapping_family,
1978 int* streams,
1979 int* coupled_streams,
1980 ubyte* mapping,
1981 int application
1982 ) /*OPUS_ARG_NONNULL(1)*/ /*OPUS_ARG_NONNULL(6)*/;
1984 /** Encodes a multistream Opus frame.
1985 * @param st <tt>OpusMSEncoder*</tt>: Multistream encoder state.
1986 * @param[in] pcm <tt>const opus_int16*</tt>: The input signal as interleaved
1987 * samples.
1988 * This must contain
1989 * <code>frame_size*channels</code>
1990 * samples.
1991 * @param frame_size <tt>int</tt>: Number of samples per channel in the input
1992 * signal.
1993 * This must be an Opus frame size for the
1994 * encoder's sampling rate.
1995 * For example, at 48 kHz the permitted values
1996 * are 120, 240, 480, 960, 1920, and 2880.
1997 * Passing in a duration of less than 10 ms
1998 * (480 samples at 48 kHz) will prevent the
1999 * encoder from using the LPC or hybrid modes.
2000 * @param[out] data <tt>ubyte*</tt>: Output payload.
2001 * This must contain storage for at
2002 * least \a max_data_bytes.
2003 * @param [in] max_data_bytes <tt>opus_int32</tt>: Size of the allocated
2004 * memory for the output
2005 * payload. This may be
2006 * used to impose an upper limit on
2007 * the instant bitrate, but should
2008 * not be used as the only bitrate
2009 * control. Use #OPUS_SET_BITRATE to
2010 * control the bitrate.
2011 * @returns The length of the encoded packet (in bytes) on success or a
2012 * negative error code (see @ref opus_errorcodes) on failure.
2014 int opus_multistream_encode(
2015 OpusMSEncoder* st,
2016 const(opus_int16)* pcm,
2017 int frame_size,
2018 ubyte* data,
2019 opus_int32 max_data_bytes
2020 ) /*OPUS_ARG_NONNULL(1)*/ /*OPUS_ARG_NONNULL(2)*/ /*OPUS_ARG_NONNULL(4)*/;
2022 /** Encodes a multistream Opus frame from floating point input.
2023 * @param st <tt>OpusMSEncoder*</tt>: Multistream encoder state.
2024 * @param[in] pcm <tt>const float*</tt>: The input signal as interleaved
2025 * samples with a normal range of
2026 * +/-1.0.
2027 * Samples with a range beyond +/-1.0
2028 * are supported but will be clipped by
2029 * decoders using the integer API and
2030 * should only be used if it is known
2031 * that the far end supports extended
2032 * dynamic range.
2033 * This must contain
2034 * <code>frame_size*channels</code>
2035 * samples.
2036 * @param frame_size <tt>int</tt>: Number of samples per channel in the input
2037 * signal.
2038 * This must be an Opus frame size for the
2039 * encoder's sampling rate.
2040 * For example, at 48 kHz the permitted values
2041 * are 120, 240, 480, 960, 1920, and 2880.
2042 * Passing in a duration of less than 10 ms
2043 * (480 samples at 48 kHz) will prevent the
2044 * encoder from using the LPC or hybrid modes.
2045 * @param[out] data <tt>ubyte*</tt>: Output payload.
2046 * This must contain storage for at
2047 * least \a max_data_bytes.
2048 * @param [in] max_data_bytes <tt>opus_int32</tt>: Size of the allocated
2049 * memory for the output
2050 * payload. This may be
2051 * used to impose an upper limit on
2052 * the instant bitrate, but should
2053 * not be used as the only bitrate
2054 * control. Use #OPUS_SET_BITRATE to
2055 * control the bitrate.
2056 * @returns The length of the encoded packet (in bytes) on success or a
2057 * negative error code (see @ref opus_errorcodes) on failure.
2059 int opus_multistream_encode_float(
2060 OpusMSEncoder* st,
2061 const(float)* pcm,
2062 int frame_size,
2063 ubyte* data,
2064 opus_int32 max_data_bytes
2065 ) /*OPUS_ARG_NONNULL(1)*/ /*OPUS_ARG_NONNULL(2)*/ /*OPUS_ARG_NONNULL(4)*/;
2067 /** Frees an <code>OpusMSEncoder</code> allocated by
2068 * opus_multistream_encoder_create().
2069 * @param st <tt>OpusMSEncoder*</tt>: Multistream encoder state to be freed.
2071 void opus_multistream_encoder_destroy (OpusMSEncoder* st);
2073 /** Perform a CTL function on a multistream Opus encoder.
2075 * Generally the request and subsequent arguments are generated by a
2076 * convenience macro.
2077 * @param st <tt>OpusMSEncoder*</tt>: Multistream encoder state.
2078 * @param request This and all remaining parameters should be replaced by one
2079 * of the convenience macros in @ref opus_genericctls,
2080 * @ref opus_encoderctls, or @ref opus_multistream_ctls.
2081 * @see opus_genericctls
2082 * @see opus_encoderctls
2083 * @see opus_multistream_ctls
2085 int opus_multistream_encoder_ctl (OpusMSEncoder* st, int request, ...) /*OPUS_ARG_NONNULL(1)*/;
2087 /**@}*/
2089 /**\name Multistream decoder functions */
2090 /**@{*/
2092 /** Gets the size of an <code>OpusMSDecoder</code> structure.
2093 * @param streams <tt>int</tt>: The total number of streams coded in the
2094 * input.
2095 * This must be no more than 255.
2096 * @param coupled_streams <tt>int</tt>: Number streams to decode as coupled
2097 * (2 channel) streams.
2098 * This must be no larger than the total
2099 * number of streams.
2100 * Additionally, The total number of
2101 * coded channels (<code>streams +
2102 * coupled_streams</code>) must be no
2103 * more than 255.
2104 * @returns The size in bytes on success, or a negative error code
2105 * (see @ref opus_errorcodes) on error.
2107 opus_int32 opus_multistream_decoder_get_size(
2108 int streams,
2109 int coupled_streams
2112 /** Allocates and initializes a multistream decoder state.
2113 * Call opus_multistream_decoder_destroy() to release
2114 * this object when finished.
2115 * @param Fs <tt>opus_int32</tt>: Sampling rate to decode at (in Hz).
2116 * This must be one of 8000, 12000, 16000,
2117 * 24000, or 48000.
2118 * @param channels <tt>int</tt>: Number of channels to output.
2119 * This must be at most 255.
2120 * It may be different from the number of coded
2121 * channels (<code>streams +
2122 * coupled_streams</code>).
2123 * @param streams <tt>int</tt>: The total number of streams coded in the
2124 * input.
2125 * This must be no more than 255.
2126 * @param coupled_streams <tt>int</tt>: Number of streams to decode as coupled
2127 * (2 channel) streams.
2128 * This must be no larger than the total
2129 * number of streams.
2130 * Additionally, The total number of
2131 * coded channels (<code>streams +
2132 * coupled_streams</code>) must be no
2133 * more than 255.
2134 * @param[in] mapping <code>const ubyte[channels]</code>: Mapping from
2135 * coded channels to output channels, as described in
2136 * @ref opus_multistream.
2137 * @param[out] error <tt>int *</tt>: Returns #OPUS_OK on success, or an error
2138 * code (see @ref opus_errorcodes) on
2139 * failure.
2141 OpusMSDecoder* opus_multistream_decoder_create(
2142 opus_int32 Fs,
2143 int channels,
2144 int streams,
2145 int coupled_streams,
2146 const(ubyte)* mapping,
2147 int* error
2148 ) /*OPUS_ARG_NONNULL(5)*/;
2150 /** Intialize a previously allocated decoder state object.
2151 * The memory pointed to by \a st must be at least the size returned by
2152 * opus_multistream_encoder_get_size().
2153 * This is intended for applications which use their own allocator instead of
2154 * malloc.
2155 * To reset a previously initialized state, use the #OPUS_RESET_STATE CTL.
2156 * @see opus_multistream_decoder_create
2157 * @see opus_multistream_deocder_get_size
2158 * @param st <tt>OpusMSEncoder*</tt>: Multistream encoder state to initialize.
2159 * @param Fs <tt>opus_int32</tt>: Sampling rate to decode at (in Hz).
2160 * This must be one of 8000, 12000, 16000,
2161 * 24000, or 48000.
2162 * @param channels <tt>int</tt>: Number of channels to output.
2163 * This must be at most 255.
2164 * It may be different from the number of coded
2165 * channels (<code>streams +
2166 * coupled_streams</code>).
2167 * @param streams <tt>int</tt>: The total number of streams coded in the
2168 * input.
2169 * This must be no more than 255.
2170 * @param coupled_streams <tt>int</tt>: Number of streams to decode as coupled
2171 * (2 channel) streams.
2172 * This must be no larger than the total
2173 * number of streams.
2174 * Additionally, The total number of
2175 * coded channels (<code>streams +
2176 * coupled_streams</code>) must be no
2177 * more than 255.
2178 * @param[in] mapping <code>const ubyte[channels]</code>: Mapping from
2179 * coded channels to output channels, as described in
2180 * @ref opus_multistream.
2181 * @returns #OPUS_OK on success, or an error code (see @ref opus_errorcodes)
2182 * on failure.
2184 int opus_multistream_decoder_init(
2185 OpusMSDecoder* st,
2186 opus_int32 Fs,
2187 int channels,
2188 int streams,
2189 int coupled_streams,
2190 const(ubyte)* mapping
2191 ) /*OPUS_ARG_NONNULL(1)*/ /*OPUS_ARG_NONNULL(6)*/;
2193 /** Decode a multistream Opus packet.
2194 * @param st <tt>OpusMSDecoder*</tt>: Multistream decoder state.
2195 * @param[in] data <tt>const ubyte*</tt>: Input payload.
2196 * Use a <code>NULL</code>
2197 * pointer to indicate packet
2198 * loss.
2199 * @param len <tt>opus_int32</tt>: Number of bytes in payload.
2200 * @param[out] pcm <tt>opus_int16*</tt>: Output signal, with interleaved
2201 * samples.
2202 * This must contain room for
2203 * <code>frame_size*channels</code>
2204 * samples.
2205 * @param frame_size <tt>int</tt>: The number of samples per channel of
2206 * available space in \a pcm.
2207 * If this is less than the maximum packet duration
2208 * (120 ms; 5760 for 48kHz), this function will not be capable
2209 * of decoding some packets. In the case of PLC (data==NULL)
2210 * or FEC (decode_fec=1), then frame_size needs to be exactly
2211 * the duration of audio that is missing, otherwise the
2212 * decoder will not be in the optimal state to decode the
2213 * next incoming packet. For the PLC and FEC cases, frame_size
2214 * <b>must</b> be a multiple of 2.5 ms.
2215 * @param decode_fec <tt>int</tt>: Flag (0 or 1) to request that any in-band
2216 * forward error correction data be decoded.
2217 * If no such data is available, the frame is
2218 * decoded as if it were lost.
2219 * @returns Number of samples decoded on success or a negative error code
2220 * (see @ref opus_errorcodes) on failure.
2222 int opus_multistream_decode(
2223 OpusMSDecoder* st,
2224 const(ubyte)* data,
2225 opus_int32 len,
2226 opus_int16* pcm,
2227 int frame_size,
2228 int decode_fec
2229 ) /*OPUS_ARG_NONNULL(1)*/ /*OPUS_ARG_NONNULL(4)*/;
2231 /** Decode a multistream Opus packet with floating point output.
2232 * @param st <tt>OpusMSDecoder*</tt>: Multistream decoder state.
2233 * @param[in] data <tt>const ubyte*</tt>: Input payload.
2234 * Use a <code>NULL</code>
2235 * pointer to indicate packet
2236 * loss.
2237 * @param len <tt>opus_int32</tt>: Number of bytes in payload.
2238 * @param[out] pcm <tt>opus_int16*</tt>: Output signal, with interleaved
2239 * samples.
2240 * This must contain room for
2241 * <code>frame_size*channels</code>
2242 * samples.
2243 * @param frame_size <tt>int</tt>: The number of samples per channel of
2244 * available space in \a pcm.
2245 * If this is less than the maximum packet duration
2246 * (120 ms; 5760 for 48kHz), this function will not be capable
2247 * of decoding some packets. In the case of PLC (data==NULL)
2248 * or FEC (decode_fec=1), then frame_size needs to be exactly
2249 * the duration of audio that is missing, otherwise the
2250 * decoder will not be in the optimal state to decode the
2251 * next incoming packet. For the PLC and FEC cases, frame_size
2252 * <b>must</b> be a multiple of 2.5 ms.
2253 * @param decode_fec <tt>int</tt>: Flag (0 or 1) to request that any in-band
2254 * forward error correction data be decoded.
2255 * If no such data is available, the frame is
2256 * decoded as if it were lost.
2257 * @returns Number of samples decoded on success or a negative error code
2258 * (see @ref opus_errorcodes) on failure.
2260 int opus_multistream_decode_float(
2261 OpusMSDecoder* st,
2262 const(ubyte)* data,
2263 opus_int32 len,
2264 float* pcm,
2265 int frame_size,
2266 int decode_fec
2267 ) /*OPUS_ARG_NONNULL(1)*/ /*OPUS_ARG_NONNULL(4)*/;
2269 /** Perform a CTL function on a multistream Opus decoder.
2271 * Generally the request and subsequent arguments are generated by a
2272 * convenience macro.
2273 * @param st <tt>OpusMSDecoder*</tt>: Multistream decoder state.
2274 * @param request This and all remaining parameters should be replaced by one
2275 * of the convenience macros in @ref opus_genericctls,
2276 * @ref opus_decoderctls, or @ref opus_multistream_ctls.
2277 * @see opus_genericctls
2278 * @see opus_decoderctls
2279 * @see opus_multistream_ctls
2281 int opus_multistream_decoder_ctl (OpusMSDecoder* st, int request, ...) /*OPUS_ARG_NONNULL(1)*/;
2283 /** Frees an <code>OpusMSDecoder</code> allocated by
2284 * opus_multistream_decoder_create().
2285 * @param st <tt>OpusMSDecoder</tt>: Multistream decoder state to be freed.
2287 void opus_multistream_decoder_destroy (OpusMSDecoder* st);
2289 /**@}*/
2291 /**@}*/