opusfile  0.4
Stand-alone decoder library for .opus files.
 All Data Structures Functions Variables Typedefs Groups
Decoding

Functions for decoding audio data

These functions retrieve actual decoded audio data from the stream.

The general functions, op_read() and op_read_float() return 16-bit or floating-point output, both using native endian ordering. The number of channels returned can change from link to link in a chained stream. There are special functions, op_read_stereo() and op_read_float_stereo(), which always output two channels, to simplify applications which do not wish to handle multichannel audio. These downmix multichannel files to two channels, so they can always return samples in the same format for every link in a chained file.

If the rest of your audio processing chain can handle floating point, those routines should be preferred, as floating point output avoids introducing clipping and other issues which might be avoided entirely if, e.g., you scale down the volume at some other stage. However, if you intend to direct consume 16-bit samples, the conversion in libopusfile provides noise-shaping dithering and, if compiled against libopus 1.1 or later, soft-clipping prevention.

libopusfile can also be configured at compile time to use the fixed-point libopus API. If so, libopusfile's floating-point API may also be disabled. In that configuration, nothing in libopusfile will use any floating-point operations, to simplify support on devices without an adequate FPU.

Warning:
HTTPS streams may be be vulnerable to truncation attacks if you do not check the error return code from op_read_float() or its associated functions. If the remote peer does not close the connection gracefully (with a TLS "close notify" message), these functions will return OP_EREAD instead of 0 when they reach the end of the file. If you are reading from an <https:> URL (particularly if seeking is not supported), you should make sure to check for this error and warn the user appropriately.
int op_set_gain_offset (OggOpusFile *_of, int _gain_type, opus_int32 _gain_offset_q8)
 Sets the gain to be used for decoded output.
OP_WARN_UNUSED_RESULT int op_read (OggOpusFile *_of, opus_int16 *_pcm, int _buf_size, int *_li) OP_ARG_NONNULL(1)
 Reads more samples from the stream.
OP_WARN_UNUSED_RESULT int op_read_float (OggOpusFile *_of, float *_pcm, int _buf_size, int *_li) OP_ARG_NONNULL(1)
 Reads more samples from the stream.
OP_WARN_UNUSED_RESULT int op_read_stereo (OggOpusFile *_of, opus_int16 *_pcm, int _buf_size) OP_ARG_NONNULL(1)
 Reads more samples from the stream and downmixes to stereo, if necessary.
OP_WARN_UNUSED_RESULT int op_read_float_stereo (OggOpusFile *_of, float *_pcm, int _buf_size) OP_ARG_NONNULL(1)
 Reads more samples from the stream and downmixes to stereo, if necessary.
#define OP_HEADER_GAIN   (0)
 Gain offset type that indicates that the provided offset is relative to the header gain.
#define OP_TRACK_GAIN   (3008)
 Gain offset type that indicates that the provided offset is relative to the R128_TRACK_GAIN value (if any), in addition to the header gain.
#define OP_ABSOLUTE_GAIN   (3009)
 Gain offset type that indicates that the provided offset should be used as the gain directly, without applying any the header or track gains.

Detailed Description


Macro Definition Documentation

#define OP_HEADER_GAIN   (0)

Gain offset type that indicates that the provided offset is relative to the header gain.

This is the default.

#define OP_TRACK_GAIN   (3008)

Gain offset type that indicates that the provided offset is relative to the R128_TRACK_GAIN value (if any), in addition to the header gain.

#define OP_ABSOLUTE_GAIN   (3009)

Gain offset type that indicates that the provided offset should be used as the gain directly, without applying any the header or track gains.


Function Documentation

int op_set_gain_offset ( OggOpusFile *  _of,
int  _gain_type,
opus_int32  _gain_offset_q8 
)

Sets the gain to be used for decoded output.

By default, the gain in the header is applied with no additional offset. The total gain (including header gain and/or track gain, if applicable, and this offset), will be clamped to [-32768,32767]/256 dB. This is more than enough to saturate or underflow 16-bit PCM.

Note:
The new gain will not be applied to any already buffered, decoded output. This means you cannot change it sample-by-sample, as at best it will be updated packet-by-packet. It is meant for setting a target volume level, rather than applying smooth fades, etc.
Parameters:
_ofThe OggOpusFile on which to set the gain offset.
_gain_typeOne of OP_HEADER_GAIN, OP_TRACK_GAIN, or OP_ABSOLUTE_GAIN.
_gain_offset_q8The gain offset to apply, in 1/256ths of a dB.
Returns:
0 on success or a negative value on error.
Return values:
OP_EINVALThe _gain_type was unrecognized.
OP_WARN_UNUSED_RESULT int op_read ( OggOpusFile *  _of,
opus_int16 *  _pcm,
int  _buf_size,
int *  _li 
)

Reads more samples from the stream.

Note:
Although _buf_size must indicate the total number of values that can be stored in _pcm, the return value is the number of samples per channel. This is done because
  1. The channel count cannot be known a priori (reading more samples might advance us into the next link, with a different channel count), so _buf_size cannot also be in units of samples per channel,
  2. Returning the samples per channel matches the libopus API as closely as we're able,
  3. Returning the total number of values instead of samples per channel would mean the caller would need a division to compute the samples per channel, and might worry about the possibility of getting back samples for some channels and not others, and
  4. This approach is relatively fool-proof: if an application passes too small a value to _buf_size, they will simply get fewer samples back, and if they assume the return value is the total number of values, then they will simply read too few (rather than reading too many and going off the end of the buffer).
Parameters:
_ofThe OggOpusFile from which to read.
[out]_pcmA buffer in which to store the output PCM samples, as signed native-endian 16-bit values at 48 kHz with a nominal range of [-32768,32767). Multiple channels are interleaved using the Vorbis channel ordering. This must have room for at least _buf_size values.
_buf_sizeThe number of values that can be stored in _pcm. It is recommended that this be large enough for at least 120 ms of data at 48 kHz per channel (5760 values per channel). Smaller buffers will simply return less data, possibly consuming more memory to buffer the data internally. libopusfile may return less data than requested. If so, there is no guarantee that the remaining data in _pcm will be unmodified.
[out]_liThe index of the link this data was decoded from. You may pass NULL if you do not need this information. If this function fails (returning a negative value), this parameter is left unset.
Returns:
The number of samples read per channel on success, or a negative value on failure. The channel count can be retrieved on success by calling op_head(_of,*_li). The number of samples returned may be 0 if the buffer was too small to store even a single sample for all channels, or if end-of-file was reached. The list of possible failure codes follows. Most of them can only be returned by unseekable, chained streams that encounter a new link.
Return values:
OP_HOLEThere was a hole in the data, and some samples may have been skipped. Call this function again to continue decoding past the hole.
OP_EREADAn underlying read operation failed. This may signal a truncation attack from an <https:> source.
OP_EFAULTAn internal memory allocation failed.
OP_EIMPLAn unseekable stream encountered a new link that used a feature that is not implemented, such as an unsupported channel family.
OP_EINVALThe stream was only partially open.
OP_ENOTFORMATAn unseekable stream encountered a new link that did not have any logical Opus streams in it.
OP_EBADHEADERAn unseekable stream encountered a new link with a required header packet that was not properly formatted, contained illegal values, or was missing altogether.
OP_EVERSIONAn unseekable stream encountered a new link with an ID header that contained an unrecognized version number.
OP_EBADPACKETFailed to properly decode the next packet.
OP_EBADLINKWe failed to find data we had seen before.
OP_EBADTIMESTAMPAn unseekable stream encountered a new link with a starting timestamp that failed basic validity checks.
OP_WARN_UNUSED_RESULT int op_read_float ( OggOpusFile *  _of,
float *  _pcm,
int  _buf_size,
int *  _li 
)

Reads more samples from the stream.

Note:
Although _buf_size must indicate the total number of values that can be stored in _pcm, the return value is the number of samples per channel.
  1. The channel count cannot be known a priori (reading more samples might advance us into the next link, with a different channel count), so _buf_size cannot also be in units of samples per channel,
  2. Returning the samples per channel matches the libopus API as closely as we're able,
  3. Returning the total number of values instead of samples per channel would mean the caller would need a division to compute the samples per channel, and might worry about the possibility of getting back samples for some channels and not others, and
  4. This approach is relatively fool-proof: if an application passes too small a value to _buf_size, they will simply get fewer samples back, and if they assume the return value is the total number of values, then they will simply read too few (rather than reading too many and going off the end of the buffer).
Parameters:
_ofThe OggOpusFile from which to read.
[out]_pcmA buffer in which to store the output PCM samples as signed floats at 48 kHz with a nominal range of [-1.0,1.0]. Multiple channels are interleaved using the Vorbis channel ordering. This must have room for at least _buf_size floats.
_buf_sizeThe number of floats that can be stored in _pcm. It is recommended that this be large enough for at least 120 ms of data at 48 kHz per channel (5760 samples per channel). Smaller buffers will simply return less data, possibly consuming more memory to buffer the data internally. If less than _buf_size values are returned, libopusfile makes no guarantee that the remaining data in _pcm will be unmodified.
[out]_liThe index of the link this data was decoded from. You may pass NULL if you do not need this information. If this function fails (returning a negative value), this parameter is left unset.
Returns:
The number of samples read per channel on success, or a negative value on failure. The channel count can be retrieved on success by calling op_head(_of,*_li). The number of samples returned may be 0 if the buffer was too small to store even a single sample for all channels, or if end-of-file was reached. The list of possible failure codes follows. Most of them can only be returned by unseekable, chained streams that encounter a new link.
Return values:
OP_HOLEThere was a hole in the data, and some samples may have been skipped. Call this function again to continue decoding past the hole.
OP_EREADAn underlying read operation failed. This may signal a truncation attack from an <https:> source.
OP_EFAULTAn internal memory allocation failed.
OP_EIMPLAn unseekable stream encountered a new link that used a feature that is not implemented, such as an unsupported channel family.
OP_EINVALThe stream was only partially open.
OP_ENOTFORMATAn unseekable stream encountered a new link that did not have any logical Opus streams in it.
OP_EBADHEADERAn unseekable stream encountered a new link with a required header packet that was not properly formatted, contained illegal values, or was missing altogether.
OP_EVERSIONAn unseekable stream encountered a new link with an ID header that contained an unrecognized version number.
OP_EBADPACKETFailed to properly decode the next packet.
OP_EBADLINKWe failed to find data we had seen before.
OP_EBADTIMESTAMPAn unseekable stream encountered a new link with a starting timestamp that failed basic validity checks.
OP_WARN_UNUSED_RESULT int op_read_stereo ( OggOpusFile *  _of,
opus_int16 *  _pcm,
int  _buf_size 
)

Reads more samples from the stream and downmixes to stereo, if necessary.

This function is intended for simple players that want a uniform output format, even if the channel count changes between links in a chained stream.

Note:
_buf_size indicates the total number of values that can be stored in _pcm, while the return value is the number of samples per channel, even though the channel count is known, for consistency with op_read().
Parameters:
_ofThe OggOpusFile from which to read.
[out]_pcmA buffer in which to store the output PCM samples, as signed native-endian 16-bit values at 48 kHz with a nominal range of [-32768,32767). The left and right channels are interleaved in the buffer. This must have room for at least _buf_size values.
_buf_sizeThe number of values that can be stored in _pcm. It is recommended that this be large enough for at least 120 ms of data at 48 kHz per channel (11520 values total). Smaller buffers will simply return less data, possibly consuming more memory to buffer the data internally. If less than _buf_size values are returned, libopusfile makes no guarantee that the remaining data in _pcm will be unmodified.
Returns:
The number of samples read per channel on success, or a negative value on failure. The number of samples returned may be 0 if the buffer was too small to store even a single sample for both channels, or if end-of-file was reached. The list of possible failure codes follows. Most of them can only be returned by unseekable, chained streams that encounter a new link.
Return values:
OP_HOLEThere was a hole in the data, and some samples may have been skipped. Call this function again to continue decoding past the hole.
OP_EREADAn underlying read operation failed. This may signal a truncation attack from an <https:> source.
OP_EFAULTAn internal memory allocation failed.
OP_EIMPLAn unseekable stream encountered a new link that used a feature that is not implemented, such as an unsupported channel family.
OP_EINVALThe stream was only partially open.
OP_ENOTFORMATAn unseekable stream encountered a new link that did not have any logical Opus streams in it.
OP_EBADHEADERAn unseekable stream encountered a new link with a required header packet that was not properly formatted, contained illegal values, or was missing altogether.
OP_EVERSIONAn unseekable stream encountered a new link with an ID header that contained an unrecognized version number.
OP_EBADPACKETFailed to properly decode the next packet.
OP_EBADLINKWe failed to find data we had seen before.
OP_EBADTIMESTAMPAn unseekable stream encountered a new link with a starting timestamp that failed basic validity checks.
OP_WARN_UNUSED_RESULT int op_read_float_stereo ( OggOpusFile *  _of,
float *  _pcm,
int  _buf_size 
)

Reads more samples from the stream and downmixes to stereo, if necessary.

This function is intended for simple players that want a uniform output format, even if the channel count changes between links in a chained stream.

Note:
_buf_size indicates the total number of values that can be stored in _pcm, while the return value is the number of samples per channel, even though the channel count is known, for consistency with op_read_float().
Parameters:
_ofThe OggOpusFile from which to read.
[out]_pcmA buffer in which to store the output PCM samples, as signed floats at 48 kHz with a nominal range of [-1.0,1.0]. The left and right channels are interleaved in the buffer. This must have room for at least _buf_size values.
_buf_sizeThe number of values that can be stored in _pcm. It is recommended that this be large enough for at least 120 ms of data at 48 kHz per channel (11520 values total). Smaller buffers will simply return less data, possibly consuming more memory to buffer the data internally. If less than _buf_size values are returned, libopusfile makes no guarantee that the remaining data in _pcm will be unmodified.
Returns:
The number of samples read per channel on success, or a negative value on failure. The number of samples returned may be 0 if the buffer was too small to store even a single sample for both channels, or if end-of-file was reached. The list of possible failure codes follows. Most of them can only be returned by unseekable, chained streams that encounter a new link.
Return values:
OP_HOLEThere was a hole in the data, and some samples may have been skipped. Call this function again to continue decoding past the hole.
OP_EREADAn underlying read operation failed. This may signal a truncation attack from an <https:> source.
OP_EFAULTAn internal memory allocation failed.
OP_EIMPLAn unseekable stream encountered a new link that used a feature that is not implemented, such as an unsupported channel family.
OP_EINVALThe stream was only partially open.
OP_ENOTFORMATAn unseekable stream encountered a new link that that did not have any logical Opus streams in it.
OP_EBADHEADERAn unseekable stream encountered a new link with a required header packet that was not properly formatted, contained illegal values, or was missing altogether.
OP_EVERSIONAn unseekable stream encountered a new link with an ID header that contained an unrecognized version number.
OP_EBADPACKETFailed to properly decode the next packet.
OP_EBADLINKWe failed to find data we had seen before.
OP_EBADTIMESTAMPAn unseekable stream encountered a new link with a starting timestamp that failed basic validity checks.