1 /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited
2 Written by Jean-Marc Valin and Koen Vos */
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
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.
29 * @file opus_defines.h
30 * @brief Opus reference implementation constants
33 #ifndef OPUS_DEFINES_H
34 #define OPUS_DEFINES_H
36 #include "opus_types.h"
42 /** @defgroup errorcodes Error codes
45 /** No error @hideinitializer*/
47 /** One or more invalid/out of range arguments @hideinitializer*/
48 #define OPUS_BAD_ARG -1
49 /** The mode struct passed is invalid @hideinitializer*/
50 #define OPUS_BUFFER_TOO_SMALL -2
51 /** An internal error was detected @hideinitializer*/
52 #define OPUS_INTERNAL_ERROR -3
53 /** The compressed data passed is corrupted @hideinitializer*/
54 #define OPUS_INVALID_PACKET -4
55 /** Invalid/unsupported request number @hideinitializer*/
56 #define OPUS_UNIMPLEMENTED -5
57 /** An encoder or decoder structure is invalid or already freed @hideinitializer*/
58 #define OPUS_INVALID_STATE -6
59 /** Memory allocation has failed @hideinitializer*/
60 #define OPUS_ALLOC_FAIL -7
63 /** @cond OPUS_INTERNAL_DOC */
64 /**Export control for opus functions */
66 #if defined(__GNUC__) && defined(OPUS_BUILD)
67 # define OPUS_EXPORT __attribute__ ((visibility ("default")))
70 # define OPUS_EXPORT __declspec(dllexport)
72 # define OPUS_EXPORT __declspec(dllimport)
78 /** These are the actual Encoder CTL ID numbers.
79 * They should not be used directly by applications. */
80 #define OPUS_SET_APPLICATION_REQUEST 4000
81 #define OPUS_GET_APPLICATION_REQUEST 4001
82 #define OPUS_SET_BITRATE_REQUEST 4002
83 #define OPUS_GET_BITRATE_REQUEST 4003
84 #define OPUS_SET_MAX_BANDWIDTH_REQUEST 4004
85 #define OPUS_GET_MAX_BANDWIDTH_REQUEST 4005
86 #define OPUS_SET_VBR_REQUEST 4006
87 #define OPUS_GET_VBR_REQUEST 4007
88 #define OPUS_SET_BANDWIDTH_REQUEST 4008
89 #define OPUS_GET_BANDWIDTH_REQUEST 4009
90 #define OPUS_SET_COMPLEXITY_REQUEST 4010
91 #define OPUS_GET_COMPLEXITY_REQUEST 4011
92 #define OPUS_SET_INBAND_FEC_REQUEST 4012
93 #define OPUS_GET_INBAND_FEC_REQUEST 4013
94 #define OPUS_SET_PACKET_LOSS_PERC_REQUEST 4014
95 #define OPUS_GET_PACKET_LOSS_PERC_REQUEST 4015
96 #define OPUS_SET_DTX_REQUEST 4016
97 #define OPUS_GET_DTX_REQUEST 4017
98 #define OPUS_SET_VBR_CONSTRAINT_REQUEST 4020
99 #define OPUS_GET_VBR_CONSTRAINT_REQUEST 4021
100 #define OPUS_SET_FORCE_CHANNELS_REQUEST 4022
101 #define OPUS_GET_FORCE_CHANNELS_REQUEST 4023
102 #define OPUS_SET_SIGNAL_REQUEST 4024
103 #define OPUS_GET_SIGNAL_REQUEST 4025
104 #define OPUS_GET_LOOKAHEAD_REQUEST 4027
105 /* #define OPUS_RESET_STATE 4028 */
106 #define OPUS_GET_FINAL_RANGE_REQUEST 4031
107 #define OPUS_GET_PITCH_REQUEST 4033
109 /* Macros to trigger compilation errors when the wrong types are provided to a CTL */
110 #define __opus_check_int(x) (((void)((x) == (opus_int32)0)), (opus_int32)(x))
111 #define __opus_check_int_ptr(ptr) ((ptr) + ((ptr) - (opus_int32*)(ptr)))
112 #define __opus_check_uint_ptr(ptr) ((ptr) + ((ptr) - (opus_uint32*)(ptr)))
115 /** @defgroup ctlvalues Pre-defined values for CTL interface
116 * @see genericctls, encoderctls
119 /* Values for the various encoder CTLs */
120 #define OPUS_AUTO -1000 /**<Auto/default setting @hideinitializer*/
121 #define OPUS_BITRATE_MAX -1 /**<Maximum bitrate @hideinitializer*/
123 /** Best for most VoIP/videoconference applications where listening quality and intelligibility matter most
124 * @hideinitializer */
125 #define OPUS_APPLICATION_VOIP 2048
126 /** Best for broadcast/high-fidelity application where the decoded audio should be as close as possible to the input
127 * @hideinitializer */
128 #define OPUS_APPLICATION_AUDIO 2049
129 /** Only use when lowest-achievable latency is what matters most. Voice-optimized modes cannot be used.
130 * @hideinitializer */
131 #define OPUS_APPLICATION_RESTRICTED_LOWDELAY 2051
133 #define OPUS_SIGNAL_VOICE 3001 /**< Signal being encoded is voice */
134 #define OPUS_SIGNAL_MUSIC 3002 /**< Signal being encoded is music */
135 #define OPUS_BANDWIDTH_NARROWBAND 1101 /**< 4kHz bandpass @hideinitializer*/
136 #define OPUS_BANDWIDTH_MEDIUMBAND 1102 /**< 6kHz bandpass @hideinitializer*/
137 #define OPUS_BANDWIDTH_WIDEBAND 1103 /**< 8kHz bandpass @hideinitializer*/
138 #define OPUS_BANDWIDTH_SUPERWIDEBAND 1104 /**<12kHz bandpass @hideinitializer*/
139 #define OPUS_BANDWIDTH_FULLBAND 1105 /**<20kHz bandpass @hideinitializer*/
144 /** @defgroup encoderctls Encoder related CTLs
146 * These are convenience macros for use with the \c opus_encode_ctl
147 * interface. They are used to generate the appropriate series of
148 * arguments for that call, passing the correct type, size and so
149 * on as expected for each particular request.
151 * Some usage examples:
155 * ret = opus_encoder_ctl(enc_ctx, OPUS_SET_BANDWIDTH(OPUS_AUTO));
156 * if (ret != OPUS_OK) return ret;
159 * opus_encoder_ctl(enc_ctx, OPUS_GET_BANDWIDTH(&rate));
161 * opus_encoder_ctl(enc_ctx, OPUS_RESET_STATE);
164 * @see genericctls, opusencoder
168 /** Configures the encoder's computational complexity.
169 * The supported range is 0-10 inclusive with 10 representing the highest complexity.
170 * The default value is 10.
171 * @param[in] x <tt>int</tt>: 0-10, inclusive
172 * @hideinitializer */
173 #define OPUS_SET_COMPLEXITY(x) OPUS_SET_COMPLEXITY_REQUEST, __opus_check_int(x)
174 /** Gets the encoder's complexity configuration. @see OPUS_SET_COMPLEXITY
175 * @param[out] x <tt>int*</tt>: 0-10, inclusive
176 * @hideinitializer */
177 #define OPUS_GET_COMPLEXITY(x) OPUS_GET_COMPLEXITY_REQUEST, __opus_check_int_ptr(x)
179 /** Configures the bitrate in the encoder.
180 * Rates from 500 to 512000 bits per second are meaningful as well as the
181 * special values OPUS_BITRATE_AUTO and OPUS_BITRATE_MAX.
182 * The value OPUS_BITRATE_MAX can be used to cause the codec to use as much rate
183 * as it can, which is useful for controlling the rate by adjusting the output
185 * @param[in] x <tt>opus_int32</tt>: bitrate in bits per second.
186 * @hideinitializer */
187 #define OPUS_SET_BITRATE(x) OPUS_SET_BITRATE_REQUEST, __opus_check_int(x)
188 /** Gets the encoder's bitrate configuration. @see OPUS_SET_BITRATE
189 * @param[out] x <tt>opus_int32*</tt>: bitrate in bits per second.
190 * @hideinitializer */
191 #define OPUS_GET_BITRATE(x) OPUS_GET_BITRATE_REQUEST, __opus_check_int_ptr(x)
193 /** Configures VBR in the encoder.
194 * The following values are currently supported:
197 * The configured bitrate may not be met exactly because frames must
198 * be an integer number of bytes in length.
199 * @warning Only the MDCT mode of Opus can provide hard CBR behavior.
200 * @param[in] x <tt>int</tt>: 0; 1 (default)
201 * @hideinitializer */
202 #define OPUS_SET_VBR(x) OPUS_SET_VBR_REQUEST, __opus_check_int(x)
203 /** Gets the encoder's VBR configuration. @see OPUS_SET_VBR
204 * @param[out] x <tt>int*</tt>: 0; 1
205 * @hideinitializer */
206 #define OPUS_GET_VBR(x) OPUS_GET_VBR_REQUEST, __opus_check_int_ptr(x)
208 /** Configures constrained VBR in the encoder.
209 * The following values are currently supported:
210 * - 0 Unconstrained VBR (default)
211 * - 1 Maximum one frame buffering delay assuming transport with a serialization speed of the nominal bitrate
212 * This setting is irrelevant when the encoder is in CBR mode.
213 * @warning Only the MDCT mode of Opus currently heeds the constraint.
214 * Speech mode ignores it completely, hybrid mode may fail to obey it
215 * if the LPC layer uses more bitrate than the constraint would have
217 * @param[in] x <tt>int</tt>: 0 (default); 1
218 * @hideinitializer */
219 #define OPUS_SET_VBR_CONSTRAINT(x) OPUS_SET_VBR_CONSTRAINT_REQUEST, __opus_check_int(x)
220 /** Gets the encoder's constrained VBR configuration @see OPUS_SET_VBR_CONSTRAINT
221 * @param[out] x <tt>int*</tt>: 0; 1
222 * @hideinitializer */
223 #define OPUS_GET_VBR_CONSTRAINT(x) OPUS_GET_VBR_CONSTRAINT_REQUEST, __opus_check_int_ptr(x)
225 /** Configures mono/stereo forcing in the encoder.
226 * This is useful when the caller knows that the input signal is currently a mono
227 * source embedded in a stereo stream.
228 * @param[in] x <tt>int</tt>: OPUS_AUTO (default); 1 (forced mono); 2 (forced stereo)
229 * @hideinitializer */
230 #define OPUS_SET_FORCE_CHANNELS(x) OPUS_SET_FORCE_CHANNELS_REQUEST, __opus_check_int(x)
231 /** Gets the encoder's forced channel configuration. @see OPUS_SET_FORCE_CHANNELS
232 * @param[out] x <tt>int*</tt>: OPUS_AUTO; 0; 1
233 * @hideinitializer */
234 #define OPUS_GET_FORCE_CHANNELS(x) OPUS_GET_FORCE_CHANNELS_REQUEST, __opus_check_int_ptr(x)
236 /** Configures the encoder's maximum bandpass allowed. @see OPUS_GET_MAX_BANDWIDTH
237 * The supported values are:
238 * - OPUS_BANDWIDTH_NARROWBAND 4kHz passband
239 * - OPUS_BANDWIDTH_MEDIUMBAND 6kHz passband
240 * - OPUS_BANDWIDTH_WIDEBAND 8kHz passband
241 * - OPUS_BANDWIDTH_SUPERWIDEBAND 12kHz passband
242 * - OPUS_BANDWIDTH_FULLBAND 20kHz passband (default)
243 * @param[in] x <tt>int</tt>: Bandwidth value
244 * @hideinitializer */
245 #define OPUS_SET_MAX_BANDWIDTH(x) OPUS_SET_MAX_BANDWIDTH_REQUEST, __opus_check_int(x)
247 /** Gets the encoder's configured maximum bandpass allowed. @see OPUS_SET_MAX_BANDWIDTH
248 * @param[out] x <tt>int*</tt>: Bandwidth value
249 * @hideinitializer */
250 #define OPUS_GET_MAX_BANDWIDTH(x) OPUS_GET_MAX_BANDWIDTH_REQUEST, __opus_check_int_ptr(x)
252 /** Configures the encoder's bandpass. @see OPUS_GET_BANDWIDTH
253 * The supported values are:
254 * - OPUS_AUTO (default)
255 * - OPUS_BANDWIDTH_NARROWBAND 4kHz passband
256 * - OPUS_BANDWIDTH_MEDIUMBAND 6kHz passband
257 * - OPUS_BANDWIDTH_WIDEBAND 8kHz passband
258 * - OPUS_BANDWIDTH_SUPERWIDEBAND 12kHz passband
259 * - OPUS_BANDWIDTH_FULLBAND 20kHz passband
260 * @param[in] x <tt>int</tt>: Bandwidth value
261 * @hideinitializer */
262 #define OPUS_SET_BANDWIDTH(x) OPUS_SET_BANDWIDTH_REQUEST, __opus_check_int(x)
264 /** Configures the type of signal being encoded.
265 * This is a hint which helps the encoder's mode selection.
266 * The supported values are:
267 * - OPUS_SIGNAL_AUTO (default)
268 * - OPUS_SIGNAL_VOICE
269 * - OPUS_SIGNAL_MUSIC
270 * @param[in] x <tt>int</tt>: Signal type
271 * @hideinitializer */
272 #define OPUS_SET_SIGNAL(x) OPUS_SET_SIGNAL_REQUEST, __opus_check_int(x)
273 /** Gets the encoder's configured signal type. @see OPUS_SET_SIGNAL
275 * @param[out] x <tt>int*</tt>: Signal type
276 * @hideinitializer */
277 #define OPUS_GET_SIGNAL(x) OPUS_GET_SIGNAL_REQUEST, __opus_check_int_ptr(x)
280 /** Configures the encoder's intended application.
281 * The initial value is a mandatory argument to the encoder_create function.
282 * The supported values are:
283 * - OPUS_APPLICATION_VOIP Process signal for improved speech intelligibility
284 * - OPUS_APPLICATION_AUDIO Favor faithfulness to the original input
285 * - OPUS_APPLICATION_RESTRICTED_LOWDELAY Configure the minimum possible coding delay
287 * @param[in] x <tt>int</tt>: Application value
288 * @hideinitializer */
289 #define OPUS_SET_APPLICATION(x) OPUS_SET_APPLICATION_REQUEST, __opus_check_int(x)
290 /** Gets the encoder's configured application. @see OPUS_SET_APPLICATION
292 * @param[out] x <tt>int*</tt>: Application value
293 * @hideinitializer */
294 #define OPUS_GET_APPLICATION(x) OPUS_GET_APPLICATION_REQUEST, __opus_check_int_ptr(x)
296 /** Gets the total samples of delay added by the entire codec.
297 * This can be queried by the encoder and then the provided number of samples can be
298 * skipped on from the start of the decoder's output to provide time aligned input
299 * and output. From the perspective of a decoding application the real data begins this many
302 * The decoder contribution to this delay is identical for all decoders, but the
303 * encoder portion of the delay may vary from implementation to implementation,
304 * version to version, or even depend on the encoder's initial configuration.
305 * Applications needing delay compensation should call this CTL rather than
306 * hard-coding a value.
307 * @param[out] x <tt>int*</tt>: Number of lookahead samples
308 * @hideinitializer */
309 #define OPUS_GET_LOOKAHEAD(x) OPUS_GET_LOOKAHEAD_REQUEST, __opus_check_int_ptr(x)
311 /** Configures the encoder's use of inband forward error correction.
312 * @note This is only applicable to the LPC layer
314 * @param[in] x <tt>int</tt>: FEC flag, 0 (disabled) is default
315 * @hideinitializer */
316 #define OPUS_SET_INBAND_FEC(x) OPUS_SET_INBAND_FEC_REQUEST, __opus_check_int(x)
317 /** Gets encoder's configured use of inband forward error correction. @see OPUS_SET_INBAND_FEC
319 * @param[out] x <tt>int*</tt>: FEC flag
320 * @hideinitializer */
321 #define OPUS_GET_INBAND_FEC(x) OPUS_GET_INBAND_FEC_REQUEST, __opus_check_int_ptr(x)
323 /** Configures the encoder's expected packet loss percentage.
324 * Higher values with trigger progressively more loss resistant behavior in the encoder
325 * at the expense of quality at a given bitrate in the lossless case, but greater quality
328 * @param[in] x <tt>int</tt>: Loss percentage in the range 0-100, inclusive.
329 * @hideinitializer */
330 #define OPUS_SET_PACKET_LOSS_PERC(x) OPUS_SET_PACKET_LOSS_PERC_REQUEST, __opus_check_int(x)
331 /** Gets the encoder's configured packet loss percentage. @see OPUS_SET_PACKET_LOSS_PERC
333 * @param[out] x <tt>int*</tt>: Loss percentage in the range 0-100, inclusive.
334 * @hideinitializer */
335 #define OPUS_GET_PACKET_LOSS_PERC(x) OPUS_GET_PACKET_LOSS_PERC_REQUEST, __opus_check_int_ptr(x)
337 /** Configures the encoder's use of discontinuous transmission.
338 * @note This is only applicable to the LPC layer
340 * @param[in] x <tt>int</tt>: DTX flag, 0 (disabled) is default
341 * @hideinitializer */
342 #define OPUS_SET_DTX(x) OPUS_SET_DTX_REQUEST, __opus_check_int(x)
343 /** Gets encoder's configured use of discontinuous transmission. @see OPUS_SET_DTX
345 * @param[out] x <tt>int*</tt>: DTX flag
346 * @hideinitializer */
347 #define OPUS_GET_DTX(x) OPUS_GET_DTX_REQUEST, __opus_check_int_ptr(x)
350 /** @defgroup genericctls Generic CTLs
352 * These macros are used with the \c opus_decoder_ctl and
353 * \c opus_encoder_ctl calls to generate a particular
356 * When called on an \c OpusDecoder they apply to that
357 * particular decoder instance. When called on an
358 * \c OpusEncoder they apply to the corresponding setting
359 * on that encoder instance, if present.
361 * Some usage examples:
366 * ret = opus_decoder_ctl(dec_ctx, OPUS_GET_PITCH(&pitch));
367 * if (ret == OPUS_OK) return ret;
369 * opus_encoder_ctl(enc_ctx, OPUS_RESET_STATE);
370 * opus_decoder_ctl(dec_ctx, OPUS_RESET_STATE);
372 * opus_int32 enc_bw, dec_bw;
373 * opus_encoder_ctl(enc_ctx, OPUS_GET_BANDWIDTH(&enc_bw));
374 * opus_decoder_ctl(dec_ctx, OPUS_GET_BANDWIDTH(&dec_bw));
375 * if (enc_bw != dec_bw) {
376 * printf("packet bandwidth mismatch!\n");
380 * @see opusencoder, opus_decoder_ctl, opus_encoder_ctl
384 /** Resets the codec state to be equivalent to a freshly initialized state.
385 * This should be called when switching streams in order to prevent
386 * the back to back decoding from giving different results from
387 * one at a time decoding.
388 * @hideinitializer */
389 #define OPUS_RESET_STATE 4028
391 /** Gets the final state of the codec's entropy coder.
392 * This is used for testing purposes,
393 * The encoder and decoder state should be identical after coding a payload
394 * (assuming no data corruption or software bugs)
396 * @param[out] x <tt>opus_uint32*</tt>: Entropy coder state
398 * @hideinitializer */
399 #define OPUS_GET_FINAL_RANGE(x) OPUS_GET_FINAL_RANGE_REQUEST, __opus_check_uint_ptr(x)
401 /** Gets the pitch of the last decoded frame, if available.
402 * This can be used for any post-processing algorithm requiring the use of pitch,
403 * e.g. time stretching/shortening. If the last frame was not voiced, or if the
404 * pitch was not coded in the frame, then zero is returned.
406 * This CTL is only implemented for decoder instances.
408 * @param[out] x <tt>opus_int32*</tt>: pitch period at 48 kHz (or 0 if not available)
410 * @hideinitializer */
411 #define OPUS_GET_PITCH(x) OPUS_GET_PITCH_REQUEST, __opus_check_int_ptr(x)
413 /** Gets the encoder's configured bandpass or the decoder's last bandpass. @see OPUS_SET_BANDWIDTH
414 * @param[out] x <tt>int*</tt>: Bandwidth value
415 * @hideinitializer */
416 #define OPUS_GET_BANDWIDTH(x) OPUS_GET_BANDWIDTH_REQUEST, __opus_check_int_ptr(x)
420 /** @defgroup libinfo Opus library information functions
424 /** Converts an opus error code into a human readable string.
426 * @param[in] error <tt>int</tt>: Error number
427 * @returns Error string
429 OPUS_EXPORT const char *opus_strerror(int error);
431 /** Gets the libopus version string.
433 * @returns Version string
435 OPUS_EXPORT const char *opus_get_version_string(void);
442 #endif /* OPUS_DEFINES_H */