Chris@70: /********************************************************************
Chris@70: * *
Chris@70: * THIS FILE IS PART OF THE libopusfile SOFTWARE CODEC SOURCE CODE. *
Chris@70: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
Chris@70: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
Chris@70: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
Chris@70: * *
Chris@70: * THE libopusfile SOURCE CODE IS (C) COPYRIGHT 1994-2012 *
Chris@70: * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
Chris@70: * *
Chris@70: ********************************************************************
Chris@70:
Chris@70: function: stdio-based convenience library for opening/seeking/decoding
Chris@70: last mod: $Id: vorbisfile.h 17182 2010-04-29 03:48:32Z xiphmont $
Chris@70:
Chris@70: ********************************************************************/
Chris@70: #if !defined(_opusfile_h)
Chris@70: # define _opusfile_h (1)
Chris@70:
Chris@70: /**\mainpage
Chris@70: \section Introduction
Chris@70:
Chris@70: This is the documentation for the libopusfile C API.
Chris@70:
Chris@70: The libopusfile package provides a convenient high-level API for
Chris@70: decoding and basic manipulation of all Ogg Opus audio streams.
Chris@70: libopusfile is implemented as a layer on top of Xiph.Org's
Chris@70: reference
Chris@70: libogg
Chris@70: and
Chris@70: libopus
Chris@70: libraries.
Chris@70:
Chris@70: libopusfile provides several sets of built-in routines for
Chris@70: file/stream access, and may also use custom stream I/O routines provided by
Chris@70: the embedded environment.
Chris@70: There are built-in I/O routines provided for ANSI-compliant
Chris@70: stdio
(FILE *
), memory buffers, and URLs
Chris@70: (including URLs, plus optionally and URLs).
Chris@70:
Chris@70: \section Organization
Chris@70:
Chris@70: The main API is divided into several sections:
Chris@70: - \ref stream_open_close
Chris@70: - \ref stream_info
Chris@70: - \ref stream_decoding
Chris@70: - \ref stream_seeking
Chris@70:
Chris@70: Several additional sections are not tied to the main API.
Chris@70: - \ref stream_callbacks
Chris@70: - \ref header_info
Chris@70: - \ref error_codes
Chris@70:
Chris@70: \section Overview
Chris@70:
Chris@70: The libopusfile API always decodes files to 48 kHz.
Chris@70: The original sample rate is not preserved by the lossy compression, though
Chris@70: it is stored in the header to allow you to resample to it after decoding
Chris@70: (the libopusfile API does not currently provide a resampler,
Chris@70: but the
Chris@70: the
Chris@70: Speex resampler is a good choice if you need one).
Chris@70: In general, if you are playing back the audio, you should leave it at
Chris@70: 48 kHz, provided your audio hardware supports it.
Chris@70: When decoding to a file, it may be worth resampling back to the original
Chris@70: sample rate, so as not to surprise users who might not expect the sample
Chris@70: rate to change after encoding to Opus and decoding.
Chris@70:
Chris@70: Opus files can contain anywhere from 1 to 255 channels of audio.
Chris@70: The channel mappings for up to 8 channels are the same as the
Chris@70: Vorbis
Chris@70: mappings.
Chris@70: A special stereo API can convert everything to 2 channels, making it simple
Chris@70: to support multichannel files in an application which only has stereo
Chris@70: output.
Chris@70: Although the libopusfile ABI provides support for the theoretical
Chris@70: maximum number of channels, the current implementation does not support
Chris@70: files with more than 8 channels, as they do not have well-defined channel
Chris@70: mappings.
Chris@70:
Chris@70: Like all Ogg files, Opus files may be "chained".
Chris@70: That is, multiple Opus files may be combined into a single, longer file just
Chris@70: by concatenating the original files.
Chris@70: This is commonly done in internet radio streaming, as it allows the title
Chris@70: and artist to be updated each time the song changes, since each link in the
Chris@70: chain includes its own set of metadata.
Chris@70:
Chris@70: libopusfile fully supports chained files.
Chris@70: It will decode the first Opus stream found in each link of a chained file
Chris@70: (ignoring any other streams that might be concurrently multiplexed with it,
Chris@70: such as a video stream).
Chris@70:
Chris@70: The channel count can also change between links.
Chris@70: If your application is not prepared to deal with this, it can use the stereo
Chris@70: API to ensure the audio from all links will always get decoded into a
Chris@70: common format.
Chris@70: Since libopusfile always decodes to 48 kHz, you do not have to
Chris@70: worry about the sample rate changing between links (as was possible with
Chris@70: Vorbis).
Chris@70: This makes application support for chained files with libopusfile
Chris@70: very easy.*/
Chris@70:
Chris@70: # if defined(__cplusplus)
Chris@70: extern "C" {
Chris@70: # endif
Chris@70:
Chris@70: # include
Chris@70: # include
Chris@70: # include
Chris@70: # include
Chris@70:
Chris@70: /**@cond PRIVATE*/
Chris@70:
Chris@70: /*Enable special features for gcc and gcc-compatible compilers.*/
Chris@70: # if !defined(OP_GNUC_PREREQ)
Chris@70: # if defined(__GNUC__)&&defined(__GNUC_MINOR__)
Chris@70: # define OP_GNUC_PREREQ(_maj,_min) \
Chris@70: ((__GNUC__<<16)+__GNUC_MINOR__>=((_maj)<<16)+(_min))
Chris@70: # else
Chris@70: # define OP_GNUC_PREREQ(_maj,_min) 0
Chris@70: # endif
Chris@70: # endif
Chris@70:
Chris@70: # if OP_GNUC_PREREQ(4,0)
Chris@70: # pragma GCC visibility push(default)
Chris@70: # endif
Chris@70:
Chris@70: typedef struct OpusHead OpusHead;
Chris@70: typedef struct OpusTags OpusTags;
Chris@70: typedef struct OpusPictureTag OpusPictureTag;
Chris@70: typedef struct OpusServerInfo OpusServerInfo;
Chris@70: typedef struct OpusFileCallbacks OpusFileCallbacks;
Chris@70: typedef struct OggOpusFile OggOpusFile;
Chris@70:
Chris@70: /*Warning attributes for libopusfile functions.*/
Chris@70: # if OP_GNUC_PREREQ(3,4)
Chris@70: # define OP_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
Chris@70: # else
Chris@70: # define OP_WARN_UNUSED_RESULT
Chris@70: # endif
Chris@70: # if OP_GNUC_PREREQ(3,4)
Chris@70: # define OP_ARG_NONNULL(_x) __attribute__((__nonnull__(_x)))
Chris@70: # else
Chris@70: # define OP_ARG_NONNULL(_x)
Chris@70: # endif
Chris@70:
Chris@70: /**@endcond*/
Chris@70:
Chris@70: /**\defgroup error_codes Error Codes*/
Chris@70: /*@{*/
Chris@70: /**\name List of possible error codes
Chris@70: Many of the functions in this library return a negative error code when a
Chris@70: function fails.
Chris@70: This list provides a brief explanation of the common errors.
Chris@70: See each individual function for more details on what a specific error code
Chris@70: means in that context.*/
Chris@70: /*@{*/
Chris@70:
Chris@70: /**A request did not succeed.*/
Chris@70: #define OP_FALSE (-1)
Chris@70: /*Currently not used externally.*/
Chris@70: #define OP_EOF (-2)
Chris@70: /**There was a hole in the page sequence numbers (e.g., a page was corrupt or
Chris@70: missing).*/
Chris@70: #define OP_HOLE (-3)
Chris@70: /**An underlying read, seek, or tell operation failed when it should have
Chris@70: succeeded.*/
Chris@70: #define OP_EREAD (-128)
Chris@70: /**A NULL
pointer was passed where one was unexpected, or an
Chris@70: internal memory allocation failed, or an internal library error was
Chris@70: encountered.*/
Chris@70: #define OP_EFAULT (-129)
Chris@70: /**The stream used a feature that is not implemented, such as an unsupported
Chris@70: channel family.*/
Chris@70: #define OP_EIMPL (-130)
Chris@70: /**One or more parameters to a function were invalid.*/
Chris@70: #define OP_EINVAL (-131)
Chris@70: /**A purported Ogg Opus stream did not begin with an Ogg page, a purported
Chris@70: header packet did not start with one of the required strings, "OpusHead" or
Chris@70: "OpusTags", or a link in a chained file was encountered that did not
Chris@70: contain any logical Opus streams.*/
Chris@70: #define OP_ENOTFORMAT (-132)
Chris@70: /**A required header packet was not properly formatted, contained illegal
Chris@70: values, or was missing altogether.*/
Chris@70: #define OP_EBADHEADER (-133)
Chris@70: /**The ID header contained an unrecognized version number.*/
Chris@70: #define OP_EVERSION (-134)
Chris@70: /*Currently not used at all.*/
Chris@70: #define OP_ENOTAUDIO (-135)
Chris@70: /**An audio packet failed to decode properly.
Chris@70: This is usually caused by a multistream Ogg packet where the durations of
Chris@70: the individual Opus packets contained in it are not all the same.*/
Chris@70: #define OP_EBADPACKET (-136)
Chris@70: /**We failed to find data we had seen before, or the bitstream structure was
Chris@70: sufficiently malformed that seeking to the target destination was
Chris@70: impossible.*/
Chris@70: #define OP_EBADLINK (-137)
Chris@70: /**An operation that requires seeking was requested on an unseekable stream.*/
Chris@70: #define OP_ENOSEEK (-138)
Chris@70: /**The first or last granule position of a link failed basic validity checks.*/
Chris@70: #define OP_EBADTIMESTAMP (-139)
Chris@70:
Chris@70: /*@}*/
Chris@70: /*@}*/
Chris@70:
Chris@70: /**\defgroup header_info Header Information*/
Chris@70: /*@{*/
Chris@70:
Chris@70: /**The maximum number of channels in an Ogg Opus stream.*/
Chris@70: #define OPUS_CHANNEL_COUNT_MAX (255)
Chris@70:
Chris@70: /**Ogg Opus bitstream information.
Chris@70: This contains the basic playback parameters for a stream, and corresponds to
Chris@70: the initial ID header packet of an Ogg Opus stream.*/
Chris@70: struct OpusHead{
Chris@70: /**The Ogg Opus format version, in the range 0...255.
Chris@70: The top 4 bits represent a "major" version, and the bottom four bits
Chris@70: represent backwards-compatible "minor" revisions.
Chris@70: The current specification describes version 1.
Chris@70: This library will recognize versions up through 15 as backwards compatible
Chris@70: with the current specification.
Chris@70: An earlier draft of the specification described a version 0, but the only
Chris@70: difference between version 1 and version 0 is that version 0 did
Chris@70: not specify the semantics for handling the version field.*/
Chris@70: int version;
Chris@70: /**The number of channels, in the range 1...255.*/
Chris@70: int channel_count;
Chris@70: /**The number of samples that should be discarded from the beginning of the
Chris@70: stream.*/
Chris@70: unsigned pre_skip;
Chris@70: /**The sampling rate of the original input.
Chris@70: All Opus audio is coded at 48 kHz, and should also be decoded at 48 kHz
Chris@70: for playback (unless the target hardware does not support this sampling
Chris@70: rate).
Chris@70: However, this field may be used to resample the audio back to the original
Chris@70: sampling rate, for example, when saving the output to a file.*/
Chris@70: opus_uint32 input_sample_rate;
Chris@70: /**The gain to apply to the decoded output, in dB, as a Q8 value in the range
Chris@70: -32768...32767.
Chris@70: The libopusfile API will automatically apply this gain to the
Chris@70: decoded output before returning it, scaling it by
Chris@70: pow(10,output_gain/(20.0*256))
.
Chris@70: You can adjust this behavior with op_set_gain_offset().*/
Chris@70: int output_gain;
Chris@70: /**The channel mapping family, in the range 0...255.
Chris@70: Channel mapping family 0 covers mono or stereo in a single stream.
Chris@70: Channel mapping family 1 covers 1 to 8 channels in one or more streams,
Chris@70: using the Vorbis speaker assignments.
Chris@70: Channel mapping family 255 covers 1 to 255 channels in one or more
Chris@70: streams, but without any defined speaker assignment.*/
Chris@70: int mapping_family;
Chris@70: /**The number of Opus streams in each Ogg packet, in the range 1...255.*/
Chris@70: int stream_count;
Chris@70: /**The number of coupled Opus streams in each Ogg packet, in the range
Chris@70: 0...127.
Chris@70: This must satisfy 0 <= coupled_count <= stream_count
and
Chris@70: coupled_count + stream_count <= 255
.
Chris@70: The coupled streams appear first, before all uncoupled streams, in an Ogg
Chris@70: Opus packet.*/
Chris@70: int coupled_count;
Chris@70: /**The mapping from coded stream channels to output channels.
Chris@70: Let index=mapping[k]
be the value for channel k
.
Chris@70: If index<2*coupled_count
, then it refers to the left channel
Chris@70: from stream (index/2)
if even, and the right channel from
Chris@70: stream (index/2)
if odd.
Chris@70: Otherwise, it refers to the output of the uncoupled stream
Chris@70: (index-coupled_count)
.*/
Chris@70: unsigned char mapping[OPUS_CHANNEL_COUNT_MAX];
Chris@70: };
Chris@70:
Chris@70: /**The metadata from an Ogg Opus stream.
Chris@70:
Chris@70: This structure holds the in-stream metadata corresponding to the 'comment'
Chris@70: header packet of an Ogg Opus stream.
Chris@70: The comment header is meant to be used much like someone jotting a quick
Chris@70: note on the label of a CD.
Chris@70: It should be a short, to the point text note that can be more than a couple
Chris@70: words, but not more than a short paragraph.
Chris@70:
Chris@70: The metadata is stored as a series of (tag, value) pairs, in length-encoded
Chris@70: string vectors, using the same format as Vorbis (without the final "framing
Chris@70: bit"), Theora, and Speex, except for the packet header.
Chris@70: The first occurrence of the '=' character delimits the tag and value.
Chris@70: A particular tag may occur more than once, and order is significant.
Chris@70: The character set encoding for the strings is always UTF-8, but the tag
Chris@70: names are limited to ASCII, and treated as case-insensitive.
Chris@70: See the Vorbis
Chris@70: comment header specification for details.
Chris@70:
Chris@70: In filling in this structure, libopusfile will null-terminate the
Chris@70: #user_comments strings for safety.
Chris@70: However, the bitstream format itself treats them as 8-bit clean vectors,
Chris@70: possibly containing NUL characters, so the #comment_lengths array should be
Chris@70: treated as their authoritative length.
Chris@70:
Chris@70: This structure is binary and source-compatible with a
Chris@70: vorbis_comment
, and pointers to it may be freely cast to
Chris@70: vorbis_comment
pointers, and vice versa.
Chris@70: It is provided as a separate type to avoid introducing a compile-time
Chris@70: dependency on the libvorbis headers.*/
Chris@70: struct OpusTags{
Chris@70: /**The array of comment string vectors.*/
Chris@70: char **user_comments;
Chris@70: /**An array of the corresponding length of each vector, in bytes.*/
Chris@70: int *comment_lengths;
Chris@70: /**The total number of comment streams.*/
Chris@70: int comments;
Chris@70: /**The null-terminated vendor string.
Chris@70: This identifies the software used to encode the stream.*/
Chris@70: char *vendor;
Chris@70: };
Chris@70:
Chris@70: /**\name Picture tag image formats*/
Chris@70: /*@{*/
Chris@70:
Chris@70: /**The MIME type was not recognized, or the image data did not match the
Chris@70: declared MIME type.*/
Chris@70: #define OP_PIC_FORMAT_UNKNOWN (-1)
Chris@70: /**The MIME type indicates the image data is really a URL.*/
Chris@70: #define OP_PIC_FORMAT_URL (0)
Chris@70: /**The image is a JPEG.*/
Chris@70: #define OP_PIC_FORMAT_JPEG (1)
Chris@70: /**The image is a PNG.*/
Chris@70: #define OP_PIC_FORMAT_PNG (2)
Chris@70: /**The image is a GIF.*/
Chris@70: #define OP_PIC_FORMAT_GIF (3)
Chris@70:
Chris@70: /*@}*/
Chris@70:
Chris@70: /**The contents of a METADATA_BLOCK_PICTURE tag.*/
Chris@70: struct OpusPictureTag{
Chris@70: /**The picture type according to the ID3v2 APIC frame:
Chris@70:
Chris@70: - Other
Chris@70: - 32x32 pixels 'file icon' (PNG only)
Chris@70: - Other file icon
Chris@70: - Cover (front)
Chris@70: - Cover (back)
Chris@70: - Leaflet page
Chris@70: - Media (e.g. label side of CD)
Chris@70: - Lead artist/lead performer/soloist
Chris@70: - Artist/performer
Chris@70: - Conductor
Chris@70: - Band/Orchestra
Chris@70: - Composer
Chris@70: - Lyricist/text writer
Chris@70: - Recording Location
Chris@70: - During recording
Chris@70: - During performance
Chris@70: - Movie/video screen capture
Chris@70: - A bright colored fish
Chris@70: - Illustration
Chris@70: - Band/artist logotype
Chris@70: - Publisher/Studio logotype
Chris@70:
Chris@70: Others are reserved and should not be used.
Chris@70: There may only be one each of picture type 1 and 2 in a file.*/
Chris@70: opus_int32 type;
Chris@70: /**The MIME type of the picture, in printable ASCII characters 0x20-0x7E.
Chris@70: The MIME type may also be "-->"
to signify that the data part
Chris@70: is a URL pointing to the picture instead of the picture data itself.
Chris@70: In this case, a terminating NUL is appended to the URL string in #data,
Chris@70: but #data_length is set to the length of the string excluding that
Chris@70: terminating NUL.*/
Chris@70: char *mime_type;
Chris@70: /**The description of the picture, in UTF-8.*/
Chris@70: char *description;
Chris@70: /**The width of the picture in pixels.*/
Chris@70: opus_uint32 width;
Chris@70: /**The height of the picture in pixels.*/
Chris@70: opus_uint32 height;
Chris@70: /**The color depth of the picture in bits-per-pixel (not
Chris@70: bits-per-channel).*/
Chris@70: opus_uint32 depth;
Chris@70: /**For indexed-color pictures (e.g., GIF), the number of colors used, or 0
Chris@70: for non-indexed pictures.*/
Chris@70: opus_uint32 colors;
Chris@70: /**The length of the picture data in bytes.*/
Chris@70: opus_uint32 data_length;
Chris@70: /**The binary picture data.*/
Chris@70: unsigned char *data;
Chris@70: /**The format of the picture data, if known.
Chris@70: One of
Chris@70:
Chris@70: - #OP_PIC_FORMAT_UNKNOWN,
Chris@70: - #OP_PIC_FORMAT_URL,
Chris@70: - #OP_PIC_FORMAT_JPEG,
Chris@70: - #OP_PIC_FORMAT_PNG, or
Chris@70: - #OP_PIC_FORMAT_GIF.
Chris@70:
*/
Chris@70: int format;
Chris@70: };
Chris@70:
Chris@70: /**\name Functions for manipulating header data
Chris@70:
Chris@70: These functions manipulate the #OpusHead and #OpusTags structures,
Chris@70: which describe the audio parameters and tag-value metadata, respectively.
Chris@70: These can be used to query the headers returned by libopusfile, or
Chris@70: to parse Opus headers from sources other than an Ogg Opus stream, provided
Chris@70: they use the same format.*/
Chris@70: /*@{*/
Chris@70:
Chris@70: /**Parses the contents of the ID header packet of an Ogg Opus stream.
Chris@70: \param[out] _head Returns the contents of the parsed packet.
Chris@70: The contents of this structure are untouched on error.
Chris@70: This may be NULL
to merely test the header
Chris@70: for validity.
Chris@70: \param[in] _data The contents of the ID header packet.
Chris@70: \param _len The number of bytes of data in the ID header packet.
Chris@70: \return 0 on success or a negative value on error.
Chris@70: \retval #OP_ENOTFORMAT If the data does not start with the "OpusHead"
Chris@70: string.
Chris@70: \retval #OP_EVERSION If the version field signaled a version this library
Chris@70: does not know how to parse.
Chris@70: \retval #OP_EIMPL If the channel mapping family was 255, which general
Chris@70: purpose players should not attempt to play.
Chris@70: \retval #OP_EBADHEADER If the contents of the packet otherwise violate the
Chris@70: Ogg Opus specification:
Chris@70:
Chris@70: - Insufficient data,
Chris@70: - Too much data for the known minor versions,
Chris@70: - An unrecognized channel mapping family,
Chris@70: - Zero channels or too many channels,
Chris@70: - Zero coded streams,
Chris@70: - Too many coupled streams, or
Chris@70: - An invalid channel mapping index.
Chris@70:
*/
Chris@70: OP_WARN_UNUSED_RESULT int opus_head_parse(OpusHead *_head,
Chris@70: const unsigned char *_data,size_t _len) OP_ARG_NONNULL(2);
Chris@70:
Chris@70: /**Converts a granule position to a sample offset for a given Ogg Opus stream.
Chris@70: The sample offset is simply _gp-_head->pre_skip
.
Chris@70: Granule position values smaller than OpusHead#pre_skip correspond to audio
Chris@70: that should never be played, and thus have no associated sample offset.
Chris@70: This function returns -1 for such values.
Chris@70: This function also correctly handles extremely large granule positions,
Chris@70: which may have wrapped around to a negative number when stored in a signed
Chris@70: ogg_int64_t value.
Chris@70: \param _head The #OpusHead information from the ID header of the stream.
Chris@70: \param _gp The granule position to convert.
Chris@70: \return The sample offset associated with the given granule position
Chris@70: (counting at a 48 kHz sampling rate), or the special value -1 on
Chris@70: error (i.e., the granule position was smaller than the pre-skip
Chris@70: amount).*/
Chris@70: ogg_int64_t opus_granule_sample(const OpusHead *_head,ogg_int64_t _gp)
Chris@70: OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Parses the contents of the 'comment' header packet of an Ogg Opus stream.
Chris@70: \param[out] _tags An uninitialized #OpusTags structure.
Chris@70: This returns the contents of the parsed packet.
Chris@70: The contents of this structure are untouched on error.
Chris@70: This may be NULL
to merely test the header
Chris@70: for validity.
Chris@70: \param[in] _data The contents of the 'comment' header packet.
Chris@70: \param _len The number of bytes of data in the 'info' header packet.
Chris@70: \retval 0 Success.
Chris@70: \retval #OP_ENOTFORMAT If the data does not start with the "OpusTags"
Chris@70: string.
Chris@70: \retval #OP_EBADHEADER If the contents of the packet otherwise violate the
Chris@70: Ogg Opus specification.
Chris@70: \retval #OP_EFAULT If there wasn't enough memory to store the tags.*/
Chris@70: OP_WARN_UNUSED_RESULT int opus_tags_parse(OpusTags *_tags,
Chris@70: const unsigned char *_data,size_t _len) OP_ARG_NONNULL(2);
Chris@70:
Chris@70: /**Performs a deep copy of an #OpusTags structure.
Chris@70: \param _dst The #OpusTags structure to copy into.
Chris@70: If this function fails, the contents of this structure remain
Chris@70: untouched.
Chris@70: \param _src The #OpusTags structure to copy from.
Chris@70: \retval 0 Success.
Chris@70: \retval #OP_EFAULT If there wasn't enough memory to copy the tags.*/
Chris@70: int opus_tags_copy(OpusTags *_dst,const OpusTags *_src) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Initializes an #OpusTags structure.
Chris@70: This should be called on a freshly allocated #OpusTags structure before
Chris@70: attempting to use it.
Chris@70: \param _tags The #OpusTags structure to initialize.*/
Chris@70: void opus_tags_init(OpusTags *_tags) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Add a (tag, value) pair to an initialized #OpusTags structure.
Chris@70: \note Neither opus_tags_add() nor opus_tags_add_comment() support values
Chris@70: containing embedded NULs, although the bitstream format does support them.
Chris@70: To add such tags, you will need to manipulate the #OpusTags structure
Chris@70: directly.
Chris@70: \param _tags The #OpusTags structure to add the (tag, value) pair to.
Chris@70: \param _tag A NUL-terminated, case-insensitive, ASCII string containing
Chris@70: the tag to add (without an '=' character).
Chris@70: \param _value A NUL-terminated UTF-8 containing the corresponding value.
Chris@70: \return 0 on success, or a negative value on failure.
Chris@70: \retval #OP_EFAULT An internal memory allocation failed.*/
Chris@70: int opus_tags_add(OpusTags *_tags,const char *_tag,const char *_value)
Chris@70: OP_ARG_NONNULL(1) OP_ARG_NONNULL(2) OP_ARG_NONNULL(3);
Chris@70:
Chris@70: /**Add a comment to an initialized #OpusTags structure.
Chris@70: \note Neither opus_tags_add_comment() nor opus_tags_add() support comments
Chris@70: containing embedded NULs, although the bitstream format does support them.
Chris@70: To add such tags, you will need to manipulate the #OpusTags structure
Chris@70: directly.
Chris@70: \param _tags The #OpusTags structure to add the comment to.
Chris@70: \param _comment A NUL-terminated UTF-8 string containing the comment in
Chris@70: "TAG=value" form.
Chris@70: \return 0 on success, or a negative value on failure.
Chris@70: \retval #OP_EFAULT An internal memory allocation failed.*/
Chris@70: int opus_tags_add_comment(OpusTags *_tags,const char *_comment)
Chris@70: OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
Chris@70:
Chris@70: /**Replace the binary suffix data at the end of the packet (if any).
Chris@70: \param _tags An initialized #OpusTags structure.
Chris@70: \param _data A buffer of binary data to append after the encoded user
Chris@70: comments.
Chris@70: The least significant bit of the first byte of this data must
Chris@70: be set (to ensure the data is preserved by other editors).
Chris@70: \param _len The number of bytes of binary data to append.
Chris@70: This may be zero to remove any existing binary suffix data.
Chris@70: \return 0 on success, or a negative value on error.
Chris@70: \retval #OP_EINVAL \a _len was negative, or \a _len was positive but
Chris@70: \a _data was NULL
or the least significant
Chris@70: bit of the first byte was not set.
Chris@70: \retval #OP_EFAULT An internal memory allocation failed.*/
Chris@70: int opus_tags_set_binary_suffix(OpusTags *_tags,
Chris@70: const unsigned char *_data,int _len) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Look up a comment value by its tag.
Chris@70: \param _tags An initialized #OpusTags structure.
Chris@70: \param _tag The tag to look up.
Chris@70: \param _count The instance of the tag.
Chris@70: The same tag can appear multiple times, each with a distinct
Chris@70: value, so an index is required to retrieve them all.
Chris@70: The order in which these values appear is significant and
Chris@70: should be preserved.
Chris@70: Use opus_tags_query_count() to get the legal range for the
Chris@70: \a _count parameter.
Chris@70: \return A pointer to the queried tag's value.
Chris@70: This points directly to data in the #OpusTags structure.
Chris@70: It should not be modified or freed by the application, and
Chris@70: modifications to the structure may invalidate the pointer.
Chris@70: \retval NULL If no matching tag is found.*/
Chris@70: const char *opus_tags_query(const OpusTags *_tags,const char *_tag,int _count)
Chris@70: OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
Chris@70:
Chris@70: /**Look up the number of instances of a tag.
Chris@70: Call this first when querying for a specific tag and then iterate over the
Chris@70: number of instances with separate calls to opus_tags_query() to retrieve
Chris@70: all the values for that tag in order.
Chris@70: \param _tags An initialized #OpusTags structure.
Chris@70: \param _tag The tag to look up.
Chris@70: \return The number of instances of this particular tag.*/
Chris@70: int opus_tags_query_count(const OpusTags *_tags,const char *_tag)
Chris@70: OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
Chris@70:
Chris@70: /**Retrieve the binary suffix data at the end of the packet (if any).
Chris@70: \param _tags An initialized #OpusTags structure.
Chris@70: \param[out] _len Returns the number of bytes of binary suffix data returned.
Chris@70: \return A pointer to the binary suffix data, or NULL
if none
Chris@70: was present.*/
Chris@70: const unsigned char *opus_tags_get_binary_suffix(const OpusTags *_tags,
Chris@70: int *_len) OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
Chris@70:
Chris@70: /**Get the album gain from an R128_ALBUM_GAIN tag, if one was specified.
Chris@70: This searches for the first R128_ALBUM_GAIN tag with a valid signed,
Chris@70: 16-bit decimal integer value and returns the value.
Chris@70: This routine is exposed merely for convenience for applications which wish
Chris@70: to do something special with the album gain (i.e., display it).
Chris@70: If you simply wish to apply the album gain instead of the header gain, you
Chris@70: can use op_set_gain_offset() with an #OP_ALBUM_GAIN type and no offset.
Chris@70: \param _tags An initialized #OpusTags structure.
Chris@70: \param[out] _gain_q8 The album gain, in 1/256ths of a dB.
Chris@70: This will lie in the range [-32768,32767], and should
Chris@70: be applied in addition to the header gain.
Chris@70: On error, no value is returned, and the previous
Chris@70: contents remain unchanged.
Chris@70: \return 0 on success, or a negative value on error.
Chris@70: \retval #OP_FALSE There was no album gain available in the given tags.*/
Chris@70: int opus_tags_get_album_gain(const OpusTags *_tags,int *_gain_q8)
Chris@70: OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
Chris@70:
Chris@70: /**Get the track gain from an R128_TRACK_GAIN tag, if one was specified.
Chris@70: This searches for the first R128_TRACK_GAIN tag with a valid signed,
Chris@70: 16-bit decimal integer value and returns the value.
Chris@70: This routine is exposed merely for convenience for applications which wish
Chris@70: to do something special with the track gain (i.e., display it).
Chris@70: If you simply wish to apply the track gain instead of the header gain, you
Chris@70: can use op_set_gain_offset() with an #OP_TRACK_GAIN type and no offset.
Chris@70: \param _tags An initialized #OpusTags structure.
Chris@70: \param[out] _gain_q8 The track gain, in 1/256ths of a dB.
Chris@70: This will lie in the range [-32768,32767], and should
Chris@70: be applied in addition to the header gain.
Chris@70: On error, no value is returned, and the previous
Chris@70: contents remain unchanged.
Chris@70: \return 0 on success, or a negative value on error.
Chris@70: \retval #OP_FALSE There was no track gain available in the given tags.*/
Chris@70: int opus_tags_get_track_gain(const OpusTags *_tags,int *_gain_q8)
Chris@70: OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
Chris@70:
Chris@70: /**Clears the #OpusTags structure.
Chris@70: This should be called on an #OpusTags structure after it is no longer
Chris@70: needed.
Chris@70: It will free all memory used by the structure members.
Chris@70: \param _tags The #OpusTags structure to clear.*/
Chris@70: void opus_tags_clear(OpusTags *_tags) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Check if \a _comment is an instance of a \a _tag_name tag.
Chris@70: \see opus_tagncompare
Chris@70: \param _tag_name A NUL-terminated, case-insensitive, ASCII string containing
Chris@70: the name of the tag to check for (without the terminating
Chris@70: '=' character).
Chris@70: \param _comment The comment string to check.
Chris@70: \return An integer less than, equal to, or greater than zero if \a _comment
Chris@70: is found respectively, to be less than, to match, or be greater
Chris@70: than a "tag=value" string whose tag matches \a _tag_name.*/
Chris@70: int opus_tagcompare(const char *_tag_name,const char *_comment);
Chris@70:
Chris@70: /**Check if \a _comment is an instance of a \a _tag_name tag.
Chris@70: This version is slightly more efficient than opus_tagcompare() if the length
Chris@70: of the tag name is already known (e.g., because it is a constant).
Chris@70: \see opus_tagcompare
Chris@70: \param _tag_name A case-insensitive ASCII string containing the name of the
Chris@70: tag to check for (without the terminating '=' character).
Chris@70: \param _tag_len The number of characters in the tag name.
Chris@70: This must be non-negative.
Chris@70: \param _comment The comment string to check.
Chris@70: \return An integer less than, equal to, or greater than zero if \a _comment
Chris@70: is found respectively, to be less than, to match, or be greater
Chris@70: than a "tag=value" string whose tag matches the first \a _tag_len
Chris@70: characters of \a _tag_name.*/
Chris@70: int opus_tagncompare(const char *_tag_name,int _tag_len,const char *_comment);
Chris@70:
Chris@70: /**Parse a single METADATA_BLOCK_PICTURE tag.
Chris@70: This decodes the BASE64-encoded content of the tag and returns a structure
Chris@70: with the MIME type, description, image parameters (if known), and the
Chris@70: compressed image data.
Chris@70: If the MIME type indicates the presence of an image format we recognize
Chris@70: (JPEG, PNG, or GIF) and the actual image data contains the magic signature
Chris@70: associated with that format, then the OpusPictureTag::format field will be
Chris@70: set to the corresponding format.
Chris@70: This is provided as a convenience to avoid requiring applications to parse
Chris@70: the MIME type and/or do their own format detection for the commonly used
Chris@70: formats.
Chris@70: In this case, we also attempt to extract the image parameters directly from
Chris@70: the image data (overriding any that were present in the tag, which the
Chris@70: specification says applications are not meant to rely on).
Chris@70: The application must still provide its own support for actually decoding the
Chris@70: image data and, if applicable, retrieving that data from URLs.
Chris@70: \param[out] _pic Returns the parsed picture data.
Chris@70: No sanitation is done on the type, MIME type, or
Chris@70: description fields, so these might return invalid values.
Chris@70: The contents of this structure are left unmodified on
Chris@70: failure.
Chris@70: \param _tag The METADATA_BLOCK_PICTURE tag contents.
Chris@70: The leading "METADATA_BLOCK_PICTURE=" portion is optional,
Chris@70: to allow the function to be used on either directly on the
Chris@70: values in OpusTags::user_comments or on the return value
Chris@70: of opus_tags_query().
Chris@70: \return 0 on success or a negative value on error.
Chris@70: \retval #OP_ENOTFORMAT The METADATA_BLOCK_PICTURE contents were not valid.
Chris@70: \retval #OP_EFAULT There was not enough memory to store the picture tag
Chris@70: contents.*/
Chris@70: OP_WARN_UNUSED_RESULT int opus_picture_tag_parse(OpusPictureTag *_pic,
Chris@70: const char *_tag) OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
Chris@70:
Chris@70: /**Initializes an #OpusPictureTag structure.
Chris@70: This should be called on a freshly allocated #OpusPictureTag structure
Chris@70: before attempting to use it.
Chris@70: \param _pic The #OpusPictureTag structure to initialize.*/
Chris@70: void opus_picture_tag_init(OpusPictureTag *_pic) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Clears the #OpusPictureTag structure.
Chris@70: This should be called on an #OpusPictureTag structure after it is no longer
Chris@70: needed.
Chris@70: It will free all memory used by the structure members.
Chris@70: \param _pic The #OpusPictureTag structure to clear.*/
Chris@70: void opus_picture_tag_clear(OpusPictureTag *_pic) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /*@}*/
Chris@70:
Chris@70: /*@}*/
Chris@70:
Chris@70: /**\defgroup url_options URL Reading Options*/
Chris@70: /*@{*/
Chris@70: /**\name URL reading options
Chris@70: Options for op_url_stream_create() and associated functions.
Chris@70: These allow you to provide proxy configuration parameters, skip SSL
Chris@70: certificate checks, etc.
Chris@70: Options are processed in order, and if the same option is passed multiple
Chris@70: times, only the value specified by the last occurrence has an effect
Chris@70: (unless otherwise specified).
Chris@70: They may be expanded in the future.*/
Chris@70: /*@{*/
Chris@70:
Chris@70: /**@cond PRIVATE*/
Chris@70:
Chris@70: /*These are the raw numbers used to define the request codes.
Chris@70: They should not be used directly.*/
Chris@70: #define OP_SSL_SKIP_CERTIFICATE_CHECK_REQUEST (6464)
Chris@70: #define OP_HTTP_PROXY_HOST_REQUEST (6528)
Chris@70: #define OP_HTTP_PROXY_PORT_REQUEST (6592)
Chris@70: #define OP_HTTP_PROXY_USER_REQUEST (6656)
Chris@70: #define OP_HTTP_PROXY_PASS_REQUEST (6720)
Chris@70: #define OP_GET_SERVER_INFO_REQUEST (6784)
Chris@70:
Chris@70: #define OP_URL_OPT(_request) ((_request)+(char *)0)
Chris@70:
Chris@70: /*These macros trigger compilation errors or warnings if the wrong types are
Chris@70: provided to one of the URL options.*/
Chris@70: #define OP_CHECK_INT(_x) ((void)((_x)==(opus_int32)0),(opus_int32)(_x))
Chris@70: #define OP_CHECK_CONST_CHAR_PTR(_x) ((_x)+((_x)-(const char *)(_x)))
Chris@70: #define OP_CHECK_SERVER_INFO_PTR(_x) ((_x)+((_x)-(OpusServerInfo *)(_x)))
Chris@70:
Chris@70: /**@endcond*/
Chris@70:
Chris@70: /**HTTP/Shoutcast/Icecast server information associated with a URL.*/
Chris@70: struct OpusServerInfo{
Chris@70: /**The name of the server (icy-name/ice-name).
Chris@70: This is NULL
if there was no icy-name
or
Chris@70: ice-name
header.*/
Chris@70: char *name;
Chris@70: /**A short description of the server (icy-description/ice-description).
Chris@70: This is NULL
if there was no icy-description
or
Chris@70: ice-description
header.*/
Chris@70: char *description;
Chris@70: /**The genre the server falls under (icy-genre/ice-genre).
Chris@70: This is NULL
if there was no icy-genre
or
Chris@70: ice-genre
header.*/
Chris@70: char *genre;
Chris@70: /**The homepage for the server (icy-url/ice-url).
Chris@70: This is NULL
if there was no icy-url
or
Chris@70: ice-url
header.*/
Chris@70: char *url;
Chris@70: /**The software used by the origin server (Server).
Chris@70: This is NULL
if there was no Server
header.*/
Chris@70: char *server;
Chris@70: /**The media type of the entity sent to the recepient (Content-Type).
Chris@70: This is NULL
if there was no Content-Type
Chris@70: header.*/
Chris@70: char *content_type;
Chris@70: /**The nominal stream bitrate in kbps (icy-br/ice-bitrate).
Chris@70: This is -1
if there was no icy-br
or
Chris@70: ice-bitrate
header.*/
Chris@70: opus_int32 bitrate_kbps;
Chris@70: /**Flag indicating whether the server is public (1
) or not
Chris@70: (0
) (icy-pub/ice-public).
Chris@70: This is -1
if there was no icy-pub
or
Chris@70: ice-public
header.*/
Chris@70: int is_public;
Chris@70: /**Flag indicating whether the server is using HTTPS instead of HTTP.
Chris@70: This is 0
unless HTTPS is being used.
Chris@70: This may not match the protocol used in the original URL if there were
Chris@70: redirections.*/
Chris@70: int is_ssl;
Chris@70: };
Chris@70:
Chris@70: /**Initializes an #OpusServerInfo structure.
Chris@70: All fields are set as if the corresponding header was not available.
Chris@70: \param _info The #OpusServerInfo structure to initialize.
Chris@70: \note If you use this function, you must link against libopusurl.*/
Chris@70: void opus_server_info_init(OpusServerInfo *_info) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Clears the #OpusServerInfo structure.
Chris@70: This should be called on an #OpusServerInfo structure after it is no longer
Chris@70: needed.
Chris@70: It will free all memory used by the structure members.
Chris@70: \param _info The #OpusServerInfo structure to clear.
Chris@70: \note If you use this function, you must link against libopusurl.*/
Chris@70: void opus_server_info_clear(OpusServerInfo *_info) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Skip the certificate check when connecting via TLS/SSL (https).
Chris@70: \param _b opus_int32
: Whether or not to skip the certificate
Chris@70: check.
Chris@70: The check will be skipped if \a _b is non-zero, and will not be
Chris@70: skipped if \a _b is zero.
Chris@70: \hideinitializer*/
Chris@70: #define OP_SSL_SKIP_CERTIFICATE_CHECK(_b) \
Chris@70: OP_URL_OPT(OP_SSL_SKIP_CERTIFICATE_CHECK_REQUEST),OP_CHECK_INT(_b)
Chris@70:
Chris@70: /**Proxy connections through the given host.
Chris@70: If no port is specified via #OP_HTTP_PROXY_PORT, the port number defaults
Chris@70: to 8080 (http-alt).
Chris@70: All proxy parameters are ignored for non-http and non-https URLs.
Chris@70: \param _host const char *
: The proxy server hostname.
Chris@70: This may be NULL
to disable the use of a proxy
Chris@70: server.
Chris@70: \hideinitializer*/
Chris@70: #define OP_HTTP_PROXY_HOST(_host) \
Chris@70: OP_URL_OPT(OP_HTTP_PROXY_HOST_REQUEST),OP_CHECK_CONST_CHAR_PTR(_host)
Chris@70:
Chris@70: /**Use the given port when proxying connections.
Chris@70: This option only has an effect if #OP_HTTP_PROXY_HOST is specified with a
Chris@70: non-NULL
\a _host.
Chris@70: If this option is not provided, the proxy port number defaults to 8080
Chris@70: (http-alt).
Chris@70: All proxy parameters are ignored for non-http and non-https URLs.
Chris@70: \param _port opus_int32
: The proxy server port.
Chris@70: This must be in the range 0...65535 (inclusive), or the
Chris@70: URL function this is passed to will fail.
Chris@70: \hideinitializer*/
Chris@70: #define OP_HTTP_PROXY_PORT(_port) \
Chris@70: OP_URL_OPT(OP_HTTP_PROXY_PORT_REQUEST),OP_CHECK_INT(_port)
Chris@70:
Chris@70: /**Use the given user name for authentication when proxying connections.
Chris@70: All proxy parameters are ignored for non-http and non-https URLs.
Chris@70: \param _user const char *: The proxy server user name.
Chris@70: This may be NULL
to disable proxy
Chris@70: authentication.
Chris@70: A non-NULL
value only has an effect
Chris@70: if #OP_HTTP_PROXY_HOST and #OP_HTTP_PROXY_PASS
Chris@70: are also specified with non-NULL
Chris@70: arguments.
Chris@70: \hideinitializer*/
Chris@70: #define OP_HTTP_PROXY_USER(_user) \
Chris@70: OP_URL_OPT(OP_HTTP_PROXY_USER_REQUEST),OP_CHECK_CONST_CHAR_PTR(_user)
Chris@70:
Chris@70: /**Use the given password for authentication when proxying connections.
Chris@70: All proxy parameters are ignored for non-http and non-https URLs.
Chris@70: \param _pass const char *: The proxy server password.
Chris@70: This may be NULL
to disable proxy
Chris@70: authentication.
Chris@70: A non-NULL
value only has an effect
Chris@70: if #OP_HTTP_PROXY_HOST and #OP_HTTP_PROXY_USER
Chris@70: are also specified with non-NULL
Chris@70: arguments.
Chris@70: \hideinitializer*/
Chris@70: #define OP_HTTP_PROXY_PASS(_pass) \
Chris@70: OP_URL_OPT(OP_HTTP_PROXY_PASS_REQUEST),OP_CHECK_CONST_CHAR_PTR(_pass)
Chris@70:
Chris@70: /**Parse information about the streaming server (if any) and return it.
Chris@70: Very little validation is done.
Chris@70: In particular, OpusServerInfo::url may not be a valid URL,
Chris@70: OpusServerInfo::bitrate_kbps may not really be in kbps, and
Chris@70: OpusServerInfo::content_type may not be a valid MIME type.
Chris@70: The character set of the string fields is not specified anywhere, and should
Chris@70: not be assumed to be valid UTF-8.
Chris@70: \param _info OpusServerInfo *: Returns information about the server.
Chris@70: If there is any error opening the stream, the
Chris@70: contents of this structure remain
Chris@70: unmodified.
Chris@70: On success, fills in the structure with the
Chris@70: server information that was available, if
Chris@70: any.
Chris@70: After a successful return, the contents of
Chris@70: this structure should be freed by calling
Chris@70: opus_server_info_clear().
Chris@70: \hideinitializer*/
Chris@70: #define OP_GET_SERVER_INFO(_info) \
Chris@70: OP_URL_OPT(OP_GET_SERVER_INFO_REQUEST),OP_CHECK_SERVER_INFO_PTR(_info)
Chris@70:
Chris@70: /*@}*/
Chris@70: /*@}*/
Chris@70:
Chris@70: /**\defgroup stream_callbacks Abstract Stream Reading Interface*/
Chris@70: /*@{*/
Chris@70: /**\name Functions for reading from streams
Chris@70: These functions define the interface used to read from and seek in a stream
Chris@70: of data.
Chris@70: A stream does not need to implement seeking, but the decoder will not be
Chris@70: able to seek if it does not do so.
Chris@70: These functions also include some convenience routines for working with
Chris@70: standard FILE
pointers, complete streams stored in a single
Chris@70: block of memory, or URLs.*/
Chris@70: /*@{*/
Chris@70:
Chris@70: /**Reads up to \a _nbytes bytes of data from \a _stream.
Chris@70: \param _stream The stream to read from.
Chris@70: \param[out] _ptr The buffer to store the data in.
Chris@70: \param _nbytes The maximum number of bytes to read.
Chris@70: This function may return fewer, though it will not
Chris@70: return zero unless it reaches end-of-file.
Chris@70: \return The number of bytes successfully read, or a negative value on
Chris@70: error.*/
Chris@70: typedef int (*op_read_func)(void *_stream,unsigned char *_ptr,int _nbytes);
Chris@70:
Chris@70: /**Sets the position indicator for \a _stream.
Chris@70: The new position, measured in bytes, is obtained by adding \a _offset
Chris@70: bytes to the position specified by \a _whence.
Chris@70: If \a _whence is set to SEEK_SET
, SEEK_CUR
, or
Chris@70: SEEK_END
, the offset is relative to the start of the stream,
Chris@70: the current position indicator, or end-of-file, respectively.
Chris@70: \retval 0 Success.
Chris@70: \retval -1 Seeking is not supported or an error occurred.
Chris@70: errno
need not be set.*/
Chris@70: typedef int (*op_seek_func)(void *_stream,opus_int64 _offset,int _whence);
Chris@70:
Chris@70: /**Obtains the current value of the position indicator for \a _stream.
Chris@70: \return The current position indicator.*/
Chris@70: typedef opus_int64 (*op_tell_func)(void *_stream);
Chris@70:
Chris@70: /**Closes the underlying stream.
Chris@70: \retval 0 Success.
Chris@70: \retval EOF An error occurred.
Chris@70: errno
need not be set.*/
Chris@70: typedef int (*op_close_func)(void *_stream);
Chris@70:
Chris@70: /**The callbacks used to access non-FILE
stream resources.
Chris@70: The function prototypes are basically the same as for the stdio functions
Chris@70: fread()
, fseek()
, ftell()
, and
Chris@70: fclose()
.
Chris@70: The differences are that the FILE *
arguments have been
Chris@70: replaced with a void *
, which is to be used as a pointer to
Chris@70: whatever internal data these functions might need, that #seek and #tell
Chris@70: take and return 64-bit offsets, and that #seek must return -1 if
Chris@70: the stream is unseekable.*/
Chris@70: struct OpusFileCallbacks{
Chris@70: /**Used to read data from the stream.
Chris@70: This must not be NULL
.*/
Chris@70: op_read_func read;
Chris@70: /**Used to seek in the stream.
Chris@70: This may be NULL
if seeking is not implemented.*/
Chris@70: op_seek_func seek;
Chris@70: /**Used to return the current read position in the stream.
Chris@70: This may be NULL
if seeking is not implemented.*/
Chris@70: op_tell_func tell;
Chris@70: /**Used to close the stream when the decoder is freed.
Chris@70: This may be NULL
to leave the stream open.*/
Chris@70: op_close_func close;
Chris@70: };
Chris@70:
Chris@70: /**Opens a stream with fopen()
and fills in a set of callbacks
Chris@70: that can be used to access it.
Chris@70: This is useful to avoid writing your own portable 64-bit seeking wrappers,
Chris@70: and also avoids cross-module linking issues on Windows, where a
Chris@70: FILE *
must be accessed by routines defined in the same module
Chris@70: that opened it.
Chris@70: \param[out] _cb The callbacks to use for this file.
Chris@70: If there is an error opening the file, nothing will be
Chris@70: filled in here.
Chris@70: \param _path The path to the file to open.
Chris@70: On Windows, this string must be UTF-8 (to allow access to
Chris@70: files whose names cannot be represented in the current
Chris@70: MBCS code page).
Chris@70: All other systems use the native character encoding.
Chris@70: \param _mode The mode to open the file in.
Chris@70: \return A stream handle to use with the callbacks, or NULL
on
Chris@70: error.*/
Chris@70: OP_WARN_UNUSED_RESULT void *op_fopen(OpusFileCallbacks *_cb,
Chris@70: const char *_path,const char *_mode) OP_ARG_NONNULL(1) OP_ARG_NONNULL(2)
Chris@70: OP_ARG_NONNULL(3);
Chris@70:
Chris@70: /**Opens a stream with fdopen()
and fills in a set of callbacks
Chris@70: that can be used to access it.
Chris@70: This is useful to avoid writing your own portable 64-bit seeking wrappers,
Chris@70: and also avoids cross-module linking issues on Windows, where a
Chris@70: FILE *
must be accessed by routines defined in the same module
Chris@70: that opened it.
Chris@70: \param[out] _cb The callbacks to use for this file.
Chris@70: If there is an error opening the file, nothing will be
Chris@70: filled in here.
Chris@70: \param _fd The file descriptor to open.
Chris@70: \param _mode The mode to open the file in.
Chris@70: \return A stream handle to use with the callbacks, or NULL
on
Chris@70: error.*/
Chris@70: OP_WARN_UNUSED_RESULT void *op_fdopen(OpusFileCallbacks *_cb,
Chris@70: int _fd,const char *_mode) OP_ARG_NONNULL(1) OP_ARG_NONNULL(3);
Chris@70:
Chris@70: /**Opens a stream with freopen()
and fills in a set of callbacks
Chris@70: that can be used to access it.
Chris@70: This is useful to avoid writing your own portable 64-bit seeking wrappers,
Chris@70: and also avoids cross-module linking issues on Windows, where a
Chris@70: FILE *
must be accessed by routines defined in the same module
Chris@70: that opened it.
Chris@70: \param[out] _cb The callbacks to use for this file.
Chris@70: If there is an error opening the file, nothing will be
Chris@70: filled in here.
Chris@70: \param _path The path to the file to open.
Chris@70: On Windows, this string must be UTF-8 (to allow access
Chris@70: to files whose names cannot be represented in the
Chris@70: current MBCS code page).
Chris@70: All other systems use the native character encoding.
Chris@70: \param _mode The mode to open the file in.
Chris@70: \param _stream A stream previously returned by op_fopen(), op_fdopen(),
Chris@70: or op_freopen().
Chris@70: \return A stream handle to use with the callbacks, or NULL
on
Chris@70: error.*/
Chris@70: OP_WARN_UNUSED_RESULT void *op_freopen(OpusFileCallbacks *_cb,
Chris@70: const char *_path,const char *_mode,void *_stream) OP_ARG_NONNULL(1)
Chris@70: OP_ARG_NONNULL(2) OP_ARG_NONNULL(3) OP_ARG_NONNULL(4);
Chris@70:
Chris@70: /**Creates a stream that reads from the given block of memory.
Chris@70: This block of memory must contain the complete stream to decode.
Chris@70: This is useful for caching small streams (e.g., sound effects) in RAM.
Chris@70: \param[out] _cb The callbacks to use for this stream.
Chris@70: If there is an error creating the stream, nothing will be
Chris@70: filled in here.
Chris@70: \param _data The block of memory to read from.
Chris@70: \param _size The size of the block of memory.
Chris@70: \return A stream handle to use with the callbacks, or NULL
on
Chris@70: error.*/
Chris@70: OP_WARN_UNUSED_RESULT void *op_mem_stream_create(OpusFileCallbacks *_cb,
Chris@70: const unsigned char *_data,size_t _size) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Creates a stream that reads from the given URL.
Chris@70: This function behaves identically to op_url_stream_create(), except that it
Chris@70: takes a va_list instead of a variable number of arguments.
Chris@70: It does not call the va_end
macro, and because it invokes the
Chris@70: va_arg
macro, the value of \a _ap is undefined after the call.
Chris@70: \note If you use this function, you must link against libopusurl.
Chris@70: \param[out] _cb The callbacks to use for this stream.
Chris@70: If there is an error creating the stream, nothing will
Chris@70: be filled in here.
Chris@70: \param _url The URL to read from.
Chris@70: Currently only the , , and
Chris@70: schemes are supported.
Chris@70: Both and may be disabled at compile
Chris@70: time, in which case opening such URLs will always fail.
Chris@70: Currently this only supports URIs.
Chris@70: IRIs should be converted to UTF-8 and URL-escaped, with
Chris@70: internationalized domain names encoded in punycode,
Chris@70: before passing them to this function.
Chris@70: \param[in,out] _ap A list of the \ref url_options "optional flags" to use.
Chris@70: This is a variable-length list of options terminated
Chris@70: with NULL
.
Chris@70: \return A stream handle to use with the callbacks, or NULL
on
Chris@70: error.*/
Chris@70: OP_WARN_UNUSED_RESULT void *op_url_stream_vcreate(OpusFileCallbacks *_cb,
Chris@70: const char *_url,va_list _ap) OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
Chris@70:
Chris@70: /**Creates a stream that reads from the given URL.
Chris@70: \note If you use this function, you must link against libopusurl.
Chris@70: \param[out] _cb The callbacks to use for this stream.
Chris@70: If there is an error creating the stream, nothing will be
Chris@70: filled in here.
Chris@70: \param _url The URL to read from.
Chris@70: Currently only the , , and schemes
Chris@70: are supported.
Chris@70: Both and may be disabled at compile time,
Chris@70: in which case opening such URLs will always fail.
Chris@70: Currently this only supports URIs.
Chris@70: IRIs should be converted to UTF-8 and URL-escaped, with
Chris@70: internationalized domain names encoded in punycode, before
Chris@70: passing them to this function.
Chris@70: \param ... The \ref url_options "optional flags" to use.
Chris@70: This is a variable-length list of options terminated with
Chris@70: NULL
.
Chris@70: \return A stream handle to use with the callbacks, or NULL
on
Chris@70: error.*/
Chris@70: OP_WARN_UNUSED_RESULT void *op_url_stream_create(OpusFileCallbacks *_cb,
Chris@70: const char *_url,...) OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
Chris@70:
Chris@70: /*@}*/
Chris@70: /*@}*/
Chris@70:
Chris@70: /**\defgroup stream_open_close Opening and Closing*/
Chris@70: /*@{*/
Chris@70: /**\name Functions for opening and closing streams
Chris@70:
Chris@70: These functions allow you to test a stream to see if it is Opus, open it,
Chris@70: and close it.
Chris@70: Several flavors are provided for each of the built-in stream types, plus a
Chris@70: more general version which takes a set of application-provided callbacks.*/
Chris@70: /*@{*/
Chris@70:
Chris@70: /**Test to see if this is an Opus stream.
Chris@70: For good results, you will need at least 57 bytes (for a pure Opus-only
Chris@70: stream).
Chris@70: Something like 512 bytes will give more reliable results for multiplexed
Chris@70: streams.
Chris@70: This function is meant to be a quick-rejection filter.
Chris@70: Its purpose is not to guarantee that a stream is a valid Opus stream, but to
Chris@70: ensure that it looks enough like Opus that it isn't going to be recognized
Chris@70: as some other format (except possibly an Opus stream that is also
Chris@70: multiplexed with other codecs, such as video).
Chris@70: \param[out] _head The parsed ID header contents.
Chris@70: You may pass NULL
if you do not need
Chris@70: this information.
Chris@70: If the function fails, the contents of this structure
Chris@70: remain untouched.
Chris@70: \param _initial_data An initial buffer of data from the start of the
Chris@70: stream.
Chris@70: \param _initial_bytes The number of bytes in \a _initial_data.
Chris@70: \return 0 if the data appears to be Opus, or a negative value on error.
Chris@70: \retval #OP_FALSE There was not enough data to tell if this was an Opus
Chris@70: stream or not.
Chris@70: \retval #OP_EFAULT An internal memory allocation failed.
Chris@70: \retval #OP_EIMPL The stream used a feature that is not implemented,
Chris@70: such as an unsupported channel family.
Chris@70: \retval #OP_ENOTFORMAT If the data did not contain a recognizable ID
Chris@70: header for an Opus stream.
Chris@70: \retval #OP_EVERSION If the version field signaled a version this library
Chris@70: does not know how to parse.
Chris@70: \retval #OP_EBADHEADER The ID header was not properly formatted or contained
Chris@70: illegal values.*/
Chris@70: int op_test(OpusHead *_head,
Chris@70: const unsigned char *_initial_data,size_t _initial_bytes);
Chris@70:
Chris@70: /**Open a stream from the given file path.
Chris@70: \param _path The path to the file to open.
Chris@70: \param[out] _error Returns 0 on success, or a failure code on error.
Chris@70: You may pass in NULL
if you don't want the
Chris@70: failure code.
Chris@70: The failure code will be #OP_EFAULT if the file could not
Chris@70: be opened, or one of the other failure codes from
Chris@70: op_open_callbacks() otherwise.
Chris@70: \return A freshly opened \c OggOpusFile, or NULL
on error.*/
Chris@70: OP_WARN_UNUSED_RESULT OggOpusFile *op_open_file(const char *_path,int *_error)
Chris@70: OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Open a stream from a memory buffer.
Chris@70: \param _data The memory buffer to open.
Chris@70: \param _size The number of bytes in the buffer.
Chris@70: \param[out] _error Returns 0 on success, or a failure code on error.
Chris@70: You may pass in NULL
if you don't want the
Chris@70: failure code.
Chris@70: See op_open_callbacks() for a full list of failure codes.
Chris@70: \return A freshly opened \c OggOpusFile, or NULL
on error.*/
Chris@70: OP_WARN_UNUSED_RESULT OggOpusFile *op_open_memory(const unsigned char *_data,
Chris@70: size_t _size,int *_error);
Chris@70:
Chris@70: /**Open a stream from a URL.
Chris@70: This function behaves identically to op_open_url(), except that it
Chris@70: takes a va_list instead of a variable number of arguments.
Chris@70: It does not call the va_end
macro, and because it invokes the
Chris@70: va_arg
macro, the value of \a _ap is undefined after the call.
Chris@70: \note If you use this function, you must link against libopusurl.
Chris@70: \param _url The URL to open.
Chris@70: Currently only the , , and
Chris@70: schemes are supported.
Chris@70: Both and may be disabled at compile
Chris@70: time, in which case opening such URLs will always
Chris@70: fail.
Chris@70: Currently this only supports URIs.
Chris@70: IRIs should be converted to UTF-8 and URL-escaped,
Chris@70: with internationalized domain names encoded in
Chris@70: punycode, before passing them to this function.
Chris@70: \param[out] _error Returns 0 on success, or a failure code on error.
Chris@70: You may pass in NULL
if you don't want
Chris@70: the failure code.
Chris@70: See op_open_callbacks() for a full list of failure
Chris@70: codes.
Chris@70: \param[in,out] _ap A list of the \ref url_options "optional flags" to
Chris@70: use.
Chris@70: This is a variable-length list of options terminated
Chris@70: with NULL
.
Chris@70: \return A freshly opened \c OggOpusFile, or NULL
on error.*/
Chris@70: OP_WARN_UNUSED_RESULT OggOpusFile *op_vopen_url(const char *_url,
Chris@70: int *_error,va_list _ap) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Open a stream from a URL.
Chris@70: \note If you use this function, you must link against libopusurl.
Chris@70: \param _url The URL to open.
Chris@70: Currently only the , , and schemes
Chris@70: are supported.
Chris@70: Both and may be disabled at compile
Chris@70: time, in which case opening such URLs will always fail.
Chris@70: Currently this only supports URIs.
Chris@70: IRIs should be converted to UTF-8 and URL-escaped, with
Chris@70: internationalized domain names encoded in punycode,
Chris@70: before passing them to this function.
Chris@70: \param[out] _error Returns 0 on success, or a failure code on error.
Chris@70: You may pass in NULL
if you don't want the
Chris@70: failure code.
Chris@70: See op_open_callbacks() for a full list of failure codes.
Chris@70: \param ... The \ref url_options "optional flags" to use.
Chris@70: This is a variable-length list of options terminated with
Chris@70: NULL
.
Chris@70: \return A freshly opened \c OggOpusFile, or NULL
on error.*/
Chris@70: OP_WARN_UNUSED_RESULT OggOpusFile *op_open_url(const char *_url,
Chris@70: int *_error,...) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Open a stream using the given set of callbacks to access it.
Chris@70: \param _stream The stream to read from (e.g., a FILE *
).
Chris@70: This value will be passed verbatim as the first
Chris@70: argument to all of the callbacks.
Chris@70: \param _cb The callbacks with which to access the stream.
Chris@70: read()
must
Chris@70: be implemented.
Chris@70: seek()
and
Chris@70: tell()
may
Chris@70: be NULL
, or may always return -1 to
Chris@70: indicate a stream is unseekable, but if
Chris@70: seek()
is
Chris@70: implemented and succeeds on a particular stream, then
Chris@70: tell()
must
Chris@70: also.
Chris@70: close()
may
Chris@70: be NULL
, but if it is not, it will be
Chris@70: called when the \c OggOpusFile is destroyed by
Chris@70: op_free().
Chris@70: It will not be called if op_open_callbacks() fails
Chris@70: with an error.
Chris@70: \param _initial_data An initial buffer of data from the start of the
Chris@70: stream.
Chris@70: Applications can read some number of bytes from the
Chris@70: start of the stream to help identify this as an Opus
Chris@70: stream, and then provide them here to allow the
Chris@70: stream to be opened, even if it is unseekable.
Chris@70: \param _initial_bytes The number of bytes in \a _initial_data.
Chris@70: If the stream is seekable, its current position (as
Chris@70: reported by
Chris@70: tell()
Chris@70: at the start of this function) must be equal to
Chris@70: \a _initial_bytes.
Chris@70: Otherwise, seeking to absolute positions will
Chris@70: generate inconsistent results.
Chris@70: \param[out] _error Returns 0 on success, or a failure code on error.
Chris@70: You may pass in NULL
if you don't want
Chris@70: the failure code.
Chris@70: The failure code will be one of
Chris@70:
Chris@70: - #OP_EREAD
Chris@70: - An underlying read, seek, or tell operation
Chris@70: failed when it should have succeeded, or we failed
Chris@70: to find data in the stream we had seen before.
Chris@70: - #OP_EFAULT
Chris@70: - There was a memory allocation failure, or an
Chris@70: internal library error.
Chris@70: - #OP_EIMPL
Chris@70: - The stream used a feature that is not
Chris@70: implemented, such as an unsupported channel
Chris@70: family.
Chris@70: - #OP_EINVAL
Chris@70: seek()
Chris@70: was implemented and succeeded on this source, but
Chris@70: tell()
Chris@70: did not, or the starting position indicator was
Chris@70: not equal to \a _initial_bytes.
Chris@70: - #OP_ENOTFORMAT
Chris@70: - The stream contained a link that did not have
Chris@70: any logical Opus streams in it.
Chris@70: - #OP_EBADHEADER
Chris@70: - A required header packet was not properly
Chris@70: formatted, contained illegal values, or was missing
Chris@70: altogether.
Chris@70: - #OP_EVERSION
Chris@70: - An ID header contained an unrecognized version
Chris@70: number.
Chris@70: - #OP_EBADLINK
Chris@70: - We failed to find data we had seen before after
Chris@70: seeking.
Chris@70: - #OP_EBADTIMESTAMP
Chris@70: - The first or last timestamp in a link failed
Chris@70: basic validity checks.
Chris@70:
Chris@70: \return A freshly opened \c OggOpusFile, or NULL
on error.
Chris@70: libopusfile does not take ownership of the stream
Chris@70: if the call fails.
Chris@70: The calling application is responsible for closing the stream if
Chris@70: this call returns an error.*/
Chris@70: OP_WARN_UNUSED_RESULT OggOpusFile *op_open_callbacks(void *_stream,
Chris@70: const OpusFileCallbacks *_cb,const unsigned char *_initial_data,
Chris@70: size_t _initial_bytes,int *_error) OP_ARG_NONNULL(2);
Chris@70:
Chris@70: /**Partially open a stream from the given file path.
Chris@70: \see op_test_callbacks
Chris@70: \param _path The path to the file to open.
Chris@70: \param[out] _error Returns 0 on success, or a failure code on error.
Chris@70: You may pass in NULL
if you don't want the
Chris@70: failure code.
Chris@70: The failure code will be #OP_EFAULT if the file could not
Chris@70: be opened, or one of the other failure codes from
Chris@70: op_open_callbacks() otherwise.
Chris@70: \return A partially opened \c OggOpusFile, or NULL
on error.*/
Chris@70: OP_WARN_UNUSED_RESULT OggOpusFile *op_test_file(const char *_path,int *_error)
Chris@70: OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Partially open a stream from a memory buffer.
Chris@70: \see op_test_callbacks
Chris@70: \param _data The memory buffer to open.
Chris@70: \param _size The number of bytes in the buffer.
Chris@70: \param[out] _error Returns 0 on success, or a failure code on error.
Chris@70: You may pass in NULL
if you don't want the
Chris@70: failure code.
Chris@70: See op_open_callbacks() for a full list of failure codes.
Chris@70: \return A partially opened \c OggOpusFile, or NULL
on error.*/
Chris@70: OP_WARN_UNUSED_RESULT OggOpusFile *op_test_memory(const unsigned char *_data,
Chris@70: size_t _size,int *_error);
Chris@70:
Chris@70: /**Partially open a stream from a URL.
Chris@70: This function behaves identically to op_test_url(), except that it
Chris@70: takes a va_list instead of a variable number of arguments.
Chris@70: It does not call the va_end
macro, and because it invokes the
Chris@70: va_arg
macro, the value of \a _ap is undefined after the call.
Chris@70: \note If you use this function, you must link against libopusurl.
Chris@70: \see op_test_url
Chris@70: \see op_test_callbacks
Chris@70: \param _url The URL to open.
Chris@70: Currently only the , , and
Chris@70: schemes are supported.
Chris@70: Both and may be disabled at compile
Chris@70: time, in which case opening such URLs will always
Chris@70: fail.
Chris@70: Currently this only supports URIs.
Chris@70: IRIs should be converted to UTF-8 and URL-escaped,
Chris@70: with internationalized domain names encoded in
Chris@70: punycode, before passing them to this function.
Chris@70: \param[out] _error Returns 0 on success, or a failure code on error.
Chris@70: You may pass in NULL
if you don't want
Chris@70: the failure code.
Chris@70: See op_open_callbacks() for a full list of failure
Chris@70: codes.
Chris@70: \param[in,out] _ap A list of the \ref url_options "optional flags" to
Chris@70: use.
Chris@70: This is a variable-length list of options terminated
Chris@70: with NULL
.
Chris@70: \return A partially opened \c OggOpusFile, or NULL
on error.*/
Chris@70: OP_WARN_UNUSED_RESULT OggOpusFile *op_vtest_url(const char *_url,
Chris@70: int *_error,va_list _ap) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Partially open a stream from a URL.
Chris@70: \note If you use this function, you must link against libopusurl.
Chris@70: \see op_test_callbacks
Chris@70: \param _url The URL to open.
Chris@70: Currently only the , , and
Chris@70: schemes are supported.
Chris@70: Both and may be disabled at compile
Chris@70: time, in which case opening such URLs will always fail.
Chris@70: Currently this only supports URIs.
Chris@70: IRIs should be converted to UTF-8 and URL-escaped, with
Chris@70: internationalized domain names encoded in punycode,
Chris@70: before passing them to this function.
Chris@70: \param[out] _error Returns 0 on success, or a failure code on error.
Chris@70: You may pass in NULL
if you don't want the
Chris@70: failure code.
Chris@70: See op_open_callbacks() for a full list of failure
Chris@70: codes.
Chris@70: \param ... The \ref url_options "optional flags" to use.
Chris@70: This is a variable-length list of options terminated
Chris@70: with NULL
.
Chris@70: \return A partially opened \c OggOpusFile, or NULL
on error.*/
Chris@70: OP_WARN_UNUSED_RESULT OggOpusFile *op_test_url(const char *_url,
Chris@70: int *_error,...) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Partially open a stream using the given set of callbacks to access it.
Chris@70: This tests for Opusness and loads the headers for the first link.
Chris@70: It does not seek (although it tests for seekability).
Chris@70: You can query a partially open stream for the few pieces of basic
Chris@70: information returned by op_serialno(), op_channel_count(), op_head(), and
Chris@70: op_tags() (but only for the first link).
Chris@70: You may also determine if it is seekable via a call to op_seekable().
Chris@70: You cannot read audio from the stream, seek, get the size or duration,
Chris@70: get information from links other than the first one, or even get the total
Chris@70: number of links until you finish opening the stream with op_test_open().
Chris@70: If you do not need to do any of these things, you can dispose of it with
Chris@70: op_free() instead.
Chris@70:
Chris@70: This function is provided mostly to simplify porting existing code that used
Chris@70: libvorbisfile.
Chris@70: For new code, you are likely better off using op_test() instead, which
Chris@70: is less resource-intensive, requires less data to succeed, and imposes a
Chris@70: hard limit on the amount of data it examines (important for unseekable
Chris@70: streams, where all such data must be buffered until you are sure of the
Chris@70: stream type).
Chris@70: \param _stream The stream to read from (e.g., a FILE *
).
Chris@70: This value will be passed verbatim as the first
Chris@70: argument to all of the callbacks.
Chris@70: \param _cb The callbacks with which to access the stream.
Chris@70: read()
must
Chris@70: be implemented.
Chris@70: seek()
and
Chris@70: tell()
may
Chris@70: be NULL
, or may always return -1 to
Chris@70: indicate a stream is unseekable, but if
Chris@70: seek()
is
Chris@70: implemented and succeeds on a particular stream, then
Chris@70: tell()
must
Chris@70: also.
Chris@70: close()
may
Chris@70: be NULL
, but if it is not, it will be
Chris@70: called when the \c OggOpusFile is destroyed by
Chris@70: op_free().
Chris@70: It will not be called if op_open_callbacks() fails
Chris@70: with an error.
Chris@70: \param _initial_data An initial buffer of data from the start of the
Chris@70: stream.
Chris@70: Applications can read some number of bytes from the
Chris@70: start of the stream to help identify this as an Opus
Chris@70: stream, and then provide them here to allow the
Chris@70: stream to be tested more thoroughly, even if it is
Chris@70: unseekable.
Chris@70: \param _initial_bytes The number of bytes in \a _initial_data.
Chris@70: If the stream is seekable, its current position (as
Chris@70: reported by
Chris@70: tell()
Chris@70: at the start of this function) must be equal to
Chris@70: \a _initial_bytes.
Chris@70: Otherwise, seeking to absolute positions will
Chris@70: generate inconsistent results.
Chris@70: \param[out] _error Returns 0 on success, or a failure code on error.
Chris@70: You may pass in NULL
if you don't want
Chris@70: the failure code.
Chris@70: See op_open_callbacks() for a full list of failure
Chris@70: codes.
Chris@70: \return A partially opened \c OggOpusFile, or NULL
on error.
Chris@70: libopusfile does not take ownership of the stream
Chris@70: if the call fails.
Chris@70: The calling application is responsible for closing the stream if
Chris@70: this call returns an error.*/
Chris@70: OP_WARN_UNUSED_RESULT OggOpusFile *op_test_callbacks(void *_stream,
Chris@70: const OpusFileCallbacks *_cb,const unsigned char *_initial_data,
Chris@70: size_t _initial_bytes,int *_error) OP_ARG_NONNULL(2);
Chris@70:
Chris@70: /**Finish opening a stream partially opened with op_test_callbacks() or one of
Chris@70: the associated convenience functions.
Chris@70: If this function fails, you are still responsible for freeing the
Chris@70: \c OggOpusFile with op_free().
Chris@70: \param _of The \c OggOpusFile to finish opening.
Chris@70: \return 0 on success, or a negative value on error.
Chris@70: \retval #OP_EREAD An underlying read, seek, or tell operation failed
Chris@70: when it should have succeeded.
Chris@70: \retval #OP_EFAULT There was a memory allocation failure, or an
Chris@70: internal library error.
Chris@70: \retval #OP_EIMPL The stream used a feature that is not implemented,
Chris@70: such as an unsupported channel family.
Chris@70: \retval #OP_EINVAL The stream was not partially opened with
Chris@70: op_test_callbacks() or one of the associated
Chris@70: convenience functions.
Chris@70: \retval #OP_ENOTFORMAT The stream contained a link that did not have any
Chris@70: logical Opus streams in it.
Chris@70: \retval #OP_EBADHEADER A required header packet was not properly
Chris@70: formatted, contained illegal values, or was
Chris@70: missing altogether.
Chris@70: \retval #OP_EVERSION An ID header contained an unrecognized version
Chris@70: number.
Chris@70: \retval #OP_EBADLINK We failed to find data we had seen before after
Chris@70: seeking.
Chris@70: \retval #OP_EBADTIMESTAMP The first or last timestamp in a link failed basic
Chris@70: validity checks.*/
Chris@70: int op_test_open(OggOpusFile *_of) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Release all memory used by an \c OggOpusFile.
Chris@70: \param _of The \c OggOpusFile to free.*/
Chris@70: void op_free(OggOpusFile *_of);
Chris@70:
Chris@70: /*@}*/
Chris@70: /*@}*/
Chris@70:
Chris@70: /**\defgroup stream_info Stream Information*/
Chris@70: /*@{*/
Chris@70: /**\name Functions for obtaining information about streams
Chris@70:
Chris@70: These functions allow you to get basic information about a stream, including
Chris@70: seekability, the number of links (for chained streams), plus the size,
Chris@70: duration, bitrate, header parameters, and meta information for each link
Chris@70: (or, where available, the stream as a whole).
Chris@70: Some of these (size, duration) are only available for seekable streams.
Chris@70: You can also query the current stream position, link, and playback time,
Chris@70: and instantaneous bitrate during playback.
Chris@70:
Chris@70: Some of these functions may be used successfully on the partially open
Chris@70: streams returned by op_test_callbacks() or one of the associated
Chris@70: convenience functions.
Chris@70: Their documention will indicate so explicitly.*/
Chris@70: /*@{*/
Chris@70:
Chris@70: /**Returns whether or not the stream being read is seekable.
Chris@70: This is true if
Chris@70:
Chris@70: - The
seek()
and
Chris@70: tell()
callbacks are both
Chris@70: non-NULL
,
Chris@70: - The
seek()
callback was
Chris@70: successfully executed at least once, and
Chris@70: - The
tell()
callback was
Chris@70: successfully able to report the position indicator afterwards.
Chris@70:
Chris@70: This function may be called on partially-opened streams.
Chris@70: \param _of The \c OggOpusFile whose seekable status is to be returned.
Chris@70: \return A non-zero value if seekable, and 0 if unseekable.*/
Chris@70: int op_seekable(const OggOpusFile *_of) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Returns the number of links in this chained stream.
Chris@70: This function may be called on partially-opened streams, but it will always
Chris@70: return 1.
Chris@70: The actual number of links is not known until the stream is fully opened.
Chris@70: \param _of The \c OggOpusFile from which to retrieve the link count.
Chris@70: \return For fully-open seekable streams, this returns the total number of
Chris@70: links in the whole stream, which will be at least 1.
Chris@70: For partially-open or unseekable streams, this always returns 1.*/
Chris@70: int op_link_count(const OggOpusFile *_of) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Get the serial number of the given link in a (possibly-chained) Ogg Opus
Chris@70: stream.
Chris@70: This function may be called on partially-opened streams, but it will always
Chris@70: return the serial number of the Opus stream in the first link.
Chris@70: \param _of The \c OggOpusFile from which to retrieve the serial number.
Chris@70: \param _li The index of the link whose serial number should be retrieved.
Chris@70: Use a negative number to get the serial number of the current
Chris@70: link.
Chris@70: \return The serial number of the given link.
Chris@70: If \a _li is greater than the total number of links, this returns
Chris@70: the serial number of the last link.
Chris@70: If the stream is not seekable, this always returns the serial number
Chris@70: of the current link.*/
Chris@70: opus_uint32 op_serialno(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Get the channel count of the given link in a (possibly-chained) Ogg Opus
Chris@70: stream.
Chris@70: This is equivalent to op_head(_of,_li)->channel_count
, but
Chris@70: is provided for convenience.
Chris@70: This function may be called on partially-opened streams, but it will always
Chris@70: return the channel count of the Opus stream in the first link.
Chris@70: \param _of The \c OggOpusFile from which to retrieve the channel count.
Chris@70: \param _li The index of the link whose channel count should be retrieved.
Chris@70: Use a negative number to get the channel count of the current
Chris@70: link.
Chris@70: \return The channel count of the given link.
Chris@70: If \a _li is greater than the total number of links, this returns
Chris@70: the channel count of the last link.
Chris@70: If the stream is not seekable, this always returns the channel count
Chris@70: of the current link.*/
Chris@70: int op_channel_count(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Get the total (compressed) size of the stream, or of an individual link in
Chris@70: a (possibly-chained) Ogg Opus stream, including all headers and Ogg muxing
Chris@70: overhead.
Chris@70: \warning If the Opus stream (or link) is concurrently multiplexed with other
Chris@70: logical streams (e.g., video), this returns the size of the entire stream
Chris@70: (or link), not just the number of bytes in the first logical Opus stream.
Chris@70: Returning the latter would require scanning the entire file.
Chris@70: \param _of The \c OggOpusFile from which to retrieve the compressed size.
Chris@70: \param _li The index of the link whose compressed size should be computed.
Chris@70: Use a negative number to get the compressed size of the entire
Chris@70: stream.
Chris@70: \return The compressed size of the entire stream if \a _li is negative, the
Chris@70: compressed size of link \a _li if it is non-negative, or a negative
Chris@70: value on error.
Chris@70: The compressed size of the entire stream may be smaller than that
Chris@70: of the underlying stream if trailing garbage was detected in the
Chris@70: file.
Chris@70: \retval #OP_EINVAL The stream is not seekable (so we can't know the length),
Chris@70: \a _li wasn't less than the total number of links in
Chris@70: the stream, or the stream was only partially open.*/
Chris@70: opus_int64 op_raw_total(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Get the total PCM length (number of samples at 48 kHz) of the stream, or of
Chris@70: an individual link in a (possibly-chained) Ogg Opus stream.
Chris@70: Users looking for op_time_total()
should use op_pcm_total()
Chris@70: instead.
Chris@70: Because timestamps in Opus are fixed at 48 kHz, there is no need for a
Chris@70: separate function to convert this to seconds (and leaving it out avoids
Chris@70: introducing floating point to the API, for those that wish to avoid it).
Chris@70: \param _of The \c OggOpusFile from which to retrieve the PCM offset.
Chris@70: \param _li The index of the link whose PCM length should be computed.
Chris@70: Use a negative number to get the PCM length of the entire stream.
Chris@70: \return The PCM length of the entire stream if \a _li is negative, the PCM
Chris@70: length of link \a _li if it is non-negative, or a negative value on
Chris@70: error.
Chris@70: \retval #OP_EINVAL The stream is not seekable (so we can't know the length),
Chris@70: \a _li wasn't less than the total number of links in
Chris@70: the stream, or the stream was only partially open.*/
Chris@70: ogg_int64_t op_pcm_total(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Get the ID header information for the given link in a (possibly chained) Ogg
Chris@70: Opus stream.
Chris@70: This function may be called on partially-opened streams, but it will always
Chris@70: return the ID header information of the Opus stream in the first link.
Chris@70: \param _of The \c OggOpusFile from which to retrieve the ID header
Chris@70: information.
Chris@70: \param _li The index of the link whose ID header information should be
Chris@70: retrieved.
Chris@70: Use a negative number to get the ID header information of the
Chris@70: current link.
Chris@70: For an unseekable stream, \a _li is ignored, and the ID header
Chris@70: information for the current link is always returned, if
Chris@70: available.
Chris@70: \return The contents of the ID header for the given link.*/
Chris@70: const OpusHead *op_head(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Get the comment header information for the given link in a (possibly
Chris@70: chained) Ogg Opus stream.
Chris@70: This function may be called on partially-opened streams, but it will always
Chris@70: return the tags from the Opus stream in the first link.
Chris@70: \param _of The \c OggOpusFile from which to retrieve the comment header
Chris@70: information.
Chris@70: \param _li The index of the link whose comment header information should be
Chris@70: retrieved.
Chris@70: Use a negative number to get the comment header information of
Chris@70: the current link.
Chris@70: For an unseekable stream, \a _li is ignored, and the comment
Chris@70: header information for the current link is always returned, if
Chris@70: available.
Chris@70: \return The contents of the comment header for the given link, or
Chris@70: NULL
if this is an unseekable stream that encountered
Chris@70: an invalid link.*/
Chris@70: const OpusTags *op_tags(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Retrieve the index of the current link.
Chris@70: This is the link that produced the data most recently read by
Chris@70: op_read_float() or its associated functions, or, after a seek, the link
Chris@70: that the seek target landed in.
Chris@70: Reading more data may advance the link index (even on the first read after a
Chris@70: seek).
Chris@70: \param _of The \c OggOpusFile from which to retrieve the current link index.
Chris@70: \return The index of the current link on success, or a negative value on
Chris@70: failure.
Chris@70: For seekable streams, this is a number between 0 (inclusive) and the
Chris@70: value returned by op_link_count() (exclusive).
Chris@70: For unseekable streams, this value starts at 0 and increments by one
Chris@70: each time a new link is encountered (even though op_link_count()
Chris@70: always returns 1).
Chris@70: \retval #OP_EINVAL The stream was only partially open.*/
Chris@70: int op_current_link(const OggOpusFile *_of) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Computes the bitrate of the stream, or of an individual link in a
Chris@70: (possibly-chained) Ogg Opus stream.
Chris@70: The stream must be seekable to compute the bitrate.
Chris@70: For unseekable streams, use op_bitrate_instant() to get periodic estimates.
Chris@70: \warning If the Opus stream (or link) is concurrently multiplexed with other
Chris@70: logical streams (e.g., video), this uses the size of the entire stream (or
Chris@70: link) to compute the bitrate, not just the number of bytes in the first
Chris@70: logical Opus stream.
Chris@70: Returning the latter requires scanning the entire file, but this may be done
Chris@70: by decoding the whole file and calling op_bitrate_instant() once at the
Chris@70: end.
Chris@70: Install a trivial decoding callback with op_set_decode_callback() if you
Chris@70: wish to skip actual decoding during this process.
Chris@70: \param _of The \c OggOpusFile from which to retrieve the bitrate.
Chris@70: \param _li The index of the link whose bitrate should be computed.
Chris@70: Use a negative number to get the bitrate of the whole stream.
Chris@70: \return The bitrate on success, or a negative value on error.
Chris@70: \retval #OP_EINVAL The stream was only partially open, the stream was not
Chris@70: seekable, or \a _li was larger than the number of
Chris@70: links.*/
Chris@70: opus_int32 op_bitrate(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Compute the instantaneous bitrate, measured as the ratio of bits to playable
Chris@70: samples decoded since a) the last call to op_bitrate_instant(), b) the last
Chris@70: seek, or c) the start of playback, whichever was most recent.
Chris@70: This will spike somewhat after a seek or at the start/end of a chain
Chris@70: boundary, as pre-skip, pre-roll, and end-trimming causes samples to be
Chris@70: decoded but not played.
Chris@70: \param _of The \c OggOpusFile from which to retrieve the bitrate.
Chris@70: \return The bitrate, in bits per second, or a negative value on error.
Chris@70: \retval #OP_FALSE No data has been decoded since any of the events
Chris@70: described above.
Chris@70: \retval #OP_EINVAL The stream was only partially open.*/
Chris@70: opus_int32 op_bitrate_instant(OggOpusFile *_of) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Obtain the current value of the position indicator for \a _of.
Chris@70: \param _of The \c OggOpusFile from which to retrieve the position indicator.
Chris@70: \return The byte position that is currently being read from.
Chris@70: \retval #OP_EINVAL The stream was only partially open.*/
Chris@70: opus_int64 op_raw_tell(const OggOpusFile *_of) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Obtain the PCM offset of the next sample to be read.
Chris@70: If the stream is not properly timestamped, this might not increment by the
Chris@70: proper amount between reads, or even return monotonically increasing
Chris@70: values.
Chris@70: \param _of The \c OggOpusFile from which to retrieve the PCM offset.
Chris@70: \return The PCM offset of the next sample to be read.
Chris@70: \retval #OP_EINVAL The stream was only partially open.*/
Chris@70: ogg_int64_t op_pcm_tell(const OggOpusFile *_of) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /*@}*/
Chris@70: /*@}*/
Chris@70:
Chris@70: /**\defgroup stream_seeking Seeking*/
Chris@70: /*@{*/
Chris@70: /**\name Functions for seeking in Opus streams
Chris@70:
Chris@70: These functions let you seek in Opus streams, if the underlying stream
Chris@70: support it.
Chris@70: Seeking is implemented for all built-in stream I/O routines, though some
Chris@70: individual streams may not be seekable (pipes, live HTTP streams, or HTTP
Chris@70: streams from a server that does not support Range
requests).
Chris@70:
Chris@70: op_raw_seek() is the fastest: it is guaranteed to perform at most one
Chris@70: physical seek, but, since the target is a byte position, makes no guarantee
Chris@70: how close to a given time it will come.
Chris@70: op_pcm_seek() provides sample-accurate seeking.
Chris@70: The number of physical seeks it requires is still quite small (often 1 or
Chris@70: 2, even in highly variable bitrate streams).
Chris@70:
Chris@70: Seeking in Opus requires decoding some pre-roll amount before playback to
Chris@70: allow the internal state to converge (as if recovering from packet loss).
Chris@70: This is handled internally by libopusfile, but means there is
Chris@70: little extra overhead for decoding up to the exact position requested
Chris@70: (since it must decode some amount of audio anyway).
Chris@70: It also means that decoding after seeking may not return exactly the same
Chris@70: values as would be obtained by decoding the stream straight through.
Chris@70: However, such differences are expected to be smaller than the loss
Chris@70: introduced by Opus's lossy compression.*/
Chris@70: /*@{*/
Chris@70:
Chris@70: /**Seek to a byte offset relative to the compressed data.
Chris@70: This also scans packets to update the PCM cursor.
Chris@70: It will cross a logical bitstream boundary, but only if it can't get any
Chris@70: packets out of the tail of the link to which it seeks.
Chris@70: \param _of The \c OggOpusFile in which to seek.
Chris@70: \param _byte_offset The byte position to seek to.
Chris@70: This must be between 0 and #op_raw_total(\a _of,\c -1)
Chris@70: (inclusive).
Chris@70: \return 0 on success, or a negative error code on failure.
Chris@70: \retval #OP_EREAD The underlying seek operation failed.
Chris@70: \retval #OP_EINVAL The stream was only partially open, or the target was
Chris@70: outside the valid range for the stream.
Chris@70: \retval #OP_ENOSEEK This stream is not seekable.
Chris@70: \retval #OP_EBADLINK Failed to initialize a decoder for a stream for an
Chris@70: unknown reason.*/
Chris@70: int op_raw_seek(OggOpusFile *_of,opus_int64 _byte_offset) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Seek to the specified PCM offset, such that decoding will begin at exactly
Chris@70: the requested position.
Chris@70: \param _of The \c OggOpusFile in which to seek.
Chris@70: \param _pcm_offset The PCM offset to seek to.
Chris@70: This is in samples at 48 kHz relative to the start of the
Chris@70: stream.
Chris@70: \return 0 on success, or a negative value on error.
Chris@70: \retval #OP_EREAD An underlying read or seek operation failed.
Chris@70: \retval #OP_EINVAL The stream was only partially open, or the target was
Chris@70: outside the valid range for the stream.
Chris@70: \retval #OP_ENOSEEK This stream is not seekable.
Chris@70: \retval #OP_EBADLINK We failed to find data we had seen before, or the
Chris@70: bitstream structure was sufficiently malformed that
Chris@70: seeking to the target destination was impossible.*/
Chris@70: int op_pcm_seek(OggOpusFile *_of,ogg_int64_t _pcm_offset) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /*@}*/
Chris@70: /*@}*/
Chris@70:
Chris@70: /**\defgroup stream_decoding Decoding*/
Chris@70: /*@{*/
Chris@70: /**\name Functions for decoding audio data
Chris@70:
Chris@70: These functions retrieve actual decoded audio data from the stream.
Chris@70: The general functions, op_read() and op_read_float() return 16-bit or
Chris@70: floating-point output, both using native endian ordering.
Chris@70: The number of channels returned can change from link to link in a chained
Chris@70: stream.
Chris@70: There are special functions, op_read_stereo() and op_read_float_stereo(),
Chris@70: which always output two channels, to simplify applications which do not
Chris@70: wish to handle multichannel audio.
Chris@70: These downmix multichannel files to two channels, so they can always return
Chris@70: samples in the same format for every link in a chained file.
Chris@70:
Chris@70: If the rest of your audio processing chain can handle floating point, the
Chris@70: floating-point routines should be preferred, as they prevent clipping and
Chris@70: other issues which might be avoided entirely if, e.g., you scale down the
Chris@70: volume at some other stage.
Chris@70: However, if you intend to consume 16-bit samples directly, the conversion in
Chris@70: libopusfile provides noise-shaping dithering and, if compiled
Chris@70: against libopus 1.1 or later, soft-clipping prevention.
Chris@70:
Chris@70: libopusfile can also be configured at compile time to use the
Chris@70: fixed-point libopus API.
Chris@70: If so, libopusfile's floating-point API may also be disabled.
Chris@70: In that configuration, nothing in libopusfile will use any
Chris@70: floating-point operations, to simplify support on devices without an
Chris@70: adequate FPU.
Chris@70:
Chris@70: \warning HTTPS streams may be be vulnerable to truncation attacks if you do
Chris@70: not check the error return code from op_read_float() or its associated
Chris@70: functions.
Chris@70: If the remote peer does not close the connection gracefully (with a TLS
Chris@70: "close notify" message), these functions will return #OP_EREAD instead of 0
Chris@70: when they reach the end of the file.
Chris@70: If you are reading from an URL (particularly if seeking is not
Chris@70: supported), you should make sure to check for this error and warn the user
Chris@70: appropriately.*/
Chris@70: /*@{*/
Chris@70:
Chris@70: /**Indicates that the decoding callback should produce signed 16-bit
Chris@70: native-endian output samples.*/
Chris@70: #define OP_DEC_FORMAT_SHORT (7008)
Chris@70: /**Indicates that the decoding callback should produce 32-bit native-endian
Chris@70: float samples.*/
Chris@70: #define OP_DEC_FORMAT_FLOAT (7040)
Chris@70:
Chris@70: /**Indicates that the decoding callback did not decode anything, and that
Chris@70: libopusfile should decode normally instead.*/
Chris@70: #define OP_DEC_USE_DEFAULT (6720)
Chris@70:
Chris@70: /**Called to decode an Opus packet.
Chris@70: This should invoke the functional equivalent of opus_multistream_decode() or
Chris@70: opus_multistream_decode_float(), except that it returns 0 on success
Chris@70: instead of the number of decoded samples (which is known a priori).
Chris@70: \param _ctx The application-provided callback context.
Chris@70: \param _decoder The decoder to use to decode the packet.
Chris@70: \param[out] _pcm The buffer to decode into.
Chris@70: This will always have enough room for \a _nchannels of
Chris@70: \a _nsamples samples, which should be placed into this
Chris@70: buffer interleaved.
Chris@70: \param _op The packet to decode.
Chris@70: This will always have its granule position set to a valid
Chris@70: value.
Chris@70: \param _nsamples The number of samples expected from the packet.
Chris@70: \param _nchannels The number of channels expected from the packet.
Chris@70: \param _format The desired sample output format.
Chris@70: This is either #OP_DEC_FORMAT_SHORT or
Chris@70: #OP_DEC_FORMAT_FLOAT.
Chris@70: \param _li The index of the link from which this packet was decoded.
Chris@70: \return A non-negative value on success, or a negative value on error.
Chris@70: Any error codes should be the same as those returned by
Chris@70: opus_multistream_decode() or opus_multistream_decode_float().
Chris@70: Success codes are as follows:
Chris@70: \retval 0 Decoding was successful.
Chris@70: The application has filled the buffer with
Chris@70: exactly \a _nsamples*\a
Chris@70: _nchannels
samples in the requested
Chris@70: format.
Chris@70: \retval #OP_DEC_USE_DEFAULT No decoding was done.
Chris@70: libopusfile should do the decoding
Chris@70: by itself instead.*/
Chris@70: typedef int (*op_decode_cb_func)(void *_ctx,OpusMSDecoder *_decoder,void *_pcm,
Chris@70: const ogg_packet *_op,int _nsamples,int _nchannels,int _format,int _li);
Chris@70:
Chris@70: /**Sets the packet decode callback function.
Chris@70: If set, this is called once for each packet that needs to be decoded.
Chris@70: This can be used by advanced applications to do additional processing on the
Chris@70: compressed or uncompressed data.
Chris@70: For example, an application might save the final entropy coder state for
Chris@70: debugging and testing purposes, or it might apply additional filters
Chris@70: before the downmixing, dithering, or soft-clipping performed by
Chris@70: libopusfile, so long as these filters do not introduce any
Chris@70: latency.
Chris@70:
Chris@70: A call to this function is no guarantee that the audio will eventually be
Chris@70: delivered to the application.
Chris@70: libopusfile may discard some or all of the decoded audio data
Chris@70: (i.e., at the beginning or end of a link, or after a seek), however the
Chris@70: callback is still required to provide all of it.
Chris@70: \param _of The \c OggOpusFile on which to set the decode callback.
Chris@70: \param _decode_cb The callback function to call.
Chris@70: This may be NULL
to disable calling the
Chris@70: callback.
Chris@70: \param _ctx The application-provided context pointer to pass to the
Chris@70: callback on each call.*/
Chris@70: void op_set_decode_callback(OggOpusFile *_of,
Chris@70: op_decode_cb_func _decode_cb,void *_ctx) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Gain offset type that indicates that the provided offset is relative to the
Chris@70: header gain.
Chris@70: This is the default.*/
Chris@70: #define OP_HEADER_GAIN (0)
Chris@70:
Chris@70: /**Gain offset type that indicates that the provided offset is relative to the
Chris@70: R128_ALBUM_GAIN value (if any), in addition to the header gain.*/
Chris@70: #define OP_ALBUM_GAIN (3007)
Chris@70:
Chris@70: /**Gain offset type that indicates that the provided offset is relative to the
Chris@70: R128_TRACK_GAIN value (if any), in addition to the header gain.*/
Chris@70: #define OP_TRACK_GAIN (3008)
Chris@70:
Chris@70: /**Gain offset type that indicates that the provided offset should be used as
Chris@70: the gain directly, without applying any the header or track gains.*/
Chris@70: #define OP_ABSOLUTE_GAIN (3009)
Chris@70:
Chris@70: /**Sets the gain to be used for decoded output.
Chris@70: By default, the gain in the header is applied with no additional offset.
Chris@70: The total gain (including header gain and/or track gain, if applicable, and
Chris@70: this offset), will be clamped to [-32768,32767]/256 dB.
Chris@70: This is more than enough to saturate or underflow 16-bit PCM.
Chris@70: \note The new gain will not be applied to any already buffered, decoded
Chris@70: output.
Chris@70: This means you cannot change it sample-by-sample, as at best it will be
Chris@70: updated packet-by-packet.
Chris@70: It is meant for setting a target volume level, rather than applying smooth
Chris@70: fades, etc.
Chris@70: \param _of The \c OggOpusFile on which to set the gain offset.
Chris@70: \param _gain_type One of #OP_HEADER_GAIN, #OP_ALBUM_GAIN,
Chris@70: #OP_TRACK_GAIN, or #OP_ABSOLUTE_GAIN.
Chris@70: \param _gain_offset_q8 The gain offset to apply, in 1/256ths of a dB.
Chris@70: \return 0 on success or a negative value on error.
Chris@70: \retval #OP_EINVAL The \a _gain_type was unrecognized.*/
Chris@70: int op_set_gain_offset(OggOpusFile *_of,
Chris@70: int _gain_type,opus_int32 _gain_offset_q8) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Sets whether or not dithering is enabled for 16-bit decoding.
Chris@70: By default, when libopusfile is compiled to use floating-point
Chris@70: internally, calling op_read() or op_read_stereo() will first decode to
Chris@70: float, and then convert to fixed-point using noise-shaping dithering.
Chris@70: This flag can be used to disable that dithering.
Chris@70: When the application uses op_read_float() or op_read_float_stereo(), or when
Chris@70: the library has been compiled to decode directly to fixed point, this flag
Chris@70: has no effect.
Chris@70: \param _of The \c OggOpusFile on which to enable or disable dithering.
Chris@70: \param _enabled A non-zero value to enable dithering, or 0 to disable it.*/
Chris@70: void op_set_dither_enabled(OggOpusFile *_of,int _enabled) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Reads more samples from the stream.
Chris@70: \note Although \a _buf_size must indicate the total number of values that
Chris@70: can be stored in \a _pcm, the return value is the number of samples
Chris@70: per channel.
Chris@70: This is done because
Chris@70:
Chris@70: - The channel count cannot be known a priori (reading more samples might
Chris@70: advance us into the next link, with a different channel count), so
Chris@70: \a _buf_size cannot also be in units of samples per channel,
Chris@70: - Returning the samples per channel matches the
libopus
API
Chris@70: as closely as we're able,
Chris@70: - Returning the total number of values instead of samples per channel
Chris@70: would mean the caller would need a division to compute the samples per
Chris@70: channel, and might worry about the possibility of getting back samples
Chris@70: for some channels and not others, and
Chris@70: - This approach is relatively fool-proof: if an application passes too
Chris@70: small a value to \a _buf_size, they will simply get fewer samples back,
Chris@70: and if they assume the return value is the total number of values, then
Chris@70: they will simply read too few (rather than reading too many and going
Chris@70: off the end of the buffer).
Chris@70:
Chris@70: \param _of The \c OggOpusFile from which to read.
Chris@70: \param[out] _pcm A buffer in which to store the output PCM samples, as
Chris@70: signed native-endian 16-bit values at 48 kHz
Chris@70: with a nominal range of [-32768,32767)
.
Chris@70: Multiple channels are interleaved using the
Chris@70: Vorbis
Chris@70: channel ordering.
Chris@70: This must have room for at least \a _buf_size values.
Chris@70: \param _buf_size The number of values that can be stored in \a _pcm.
Chris@70: It is recommended that this be large enough for at
Chris@70: least 120 ms of data at 48 kHz per channel (5760
Chris@70: values per channel).
Chris@70: Smaller buffers will simply return less data, possibly
Chris@70: consuming more memory to buffer the data internally.
Chris@70: libopusfile may return less data than
Chris@70: requested.
Chris@70: If so, there is no guarantee that the remaining data
Chris@70: in \a _pcm will be unmodified.
Chris@70: \param[out] _li The index of the link this data was decoded from.
Chris@70: You may pass NULL
if you do not need this
Chris@70: information.
Chris@70: If this function fails (returning a negative value),
Chris@70: this parameter is left unset.
Chris@70: \return The number of samples read per channel on success, or a negative
Chris@70: value on failure.
Chris@70: The channel count can be retrieved on success by calling
Chris@70: op_head(_of,*_li)
.
Chris@70: The number of samples returned may be 0 if the buffer was too small
Chris@70: to store even a single sample for all channels, or if end-of-file
Chris@70: was reached.
Chris@70: The list of possible failure codes follows.
Chris@70: Most of them can only be returned by unseekable, chained streams
Chris@70: that encounter a new link.
Chris@70: \retval #OP_HOLE There was a hole in the data, and some samples
Chris@70: may have been skipped.
Chris@70: Call this function again to continue decoding
Chris@70: past the hole.
Chris@70: \retval #OP_EREAD An underlying read operation failed.
Chris@70: This may signal a truncation attack from an
Chris@70: source.
Chris@70: \retval #OP_EFAULT An internal memory allocation failed.
Chris@70: \retval #OP_EIMPL An unseekable stream encountered a new link that
Chris@70: used a feature that is not implemented, such as
Chris@70: an unsupported channel family.
Chris@70: \retval #OP_EINVAL The stream was only partially open.
Chris@70: \retval #OP_ENOTFORMAT An unseekable stream encountered a new link that
Chris@70: did not have any logical Opus streams in it.
Chris@70: \retval #OP_EBADHEADER An unseekable stream encountered a new link with a
Chris@70: required header packet that was not properly
Chris@70: formatted, contained illegal values, or was
Chris@70: missing altogether.
Chris@70: \retval #OP_EVERSION An unseekable stream encountered a new link with
Chris@70: an ID header that contained an unrecognized
Chris@70: version number.
Chris@70: \retval #OP_EBADPACKET Failed to properly decode the next packet.
Chris@70: \retval #OP_EBADLINK We failed to find data we had seen before.
Chris@70: \retval #OP_EBADTIMESTAMP An unseekable stream encountered a new link with
Chris@70: a starting timestamp that failed basic validity
Chris@70: checks.*/
Chris@70: OP_WARN_UNUSED_RESULT int op_read(OggOpusFile *_of,
Chris@70: opus_int16 *_pcm,int _buf_size,int *_li) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Reads more samples from the stream.
Chris@70: \note Although \a _buf_size must indicate the total number of values that
Chris@70: can be stored in \a _pcm, the return value is the number of samples
Chris@70: per channel.
Chris@70:
Chris@70: - The channel count cannot be known a priori (reading more samples might
Chris@70: advance us into the next link, with a different channel count), so
Chris@70: \a _buf_size cannot also be in units of samples per channel,
Chris@70: - Returning the samples per channel matches the
libopus
API
Chris@70: as closely as we're able,
Chris@70: - Returning the total number of values instead of samples per channel
Chris@70: would mean the caller would need a division to compute the samples per
Chris@70: channel, and might worry about the possibility of getting back samples
Chris@70: for some channels and not others, and
Chris@70: - This approach is relatively fool-proof: if an application passes too
Chris@70: small a value to \a _buf_size, they will simply get fewer samples back,
Chris@70: and if they assume the return value is the total number of values, then
Chris@70: they will simply read too few (rather than reading too many and going
Chris@70: off the end of the buffer).
Chris@70:
Chris@70: \param _of The \c OggOpusFile from which to read.
Chris@70: \param[out] _pcm A buffer in which to store the output PCM samples as
Chris@70: signed floats at 48 kHz with a nominal range of
Chris@70: [-1.0,1.0]
.
Chris@70: Multiple channels are interleaved using the
Chris@70: Vorbis
Chris@70: channel ordering.
Chris@70: This must have room for at least \a _buf_size floats.
Chris@70: \param _buf_size The number of floats that can be stored in \a _pcm.
Chris@70: It is recommended that this be large enough for at
Chris@70: least 120 ms of data at 48 kHz per channel (5760
Chris@70: samples per channel).
Chris@70: Smaller buffers will simply return less data, possibly
Chris@70: consuming more memory to buffer the data internally.
Chris@70: If less than \a _buf_size values are returned,
Chris@70: libopusfile makes no guarantee that the
Chris@70: remaining data in \a _pcm will be unmodified.
Chris@70: \param[out] _li The index of the link this data was decoded from.
Chris@70: You may pass NULL
if you do not need this
Chris@70: information.
Chris@70: If this function fails (returning a negative value),
Chris@70: this parameter is left unset.
Chris@70: \return The number of samples read per channel on success, or a negative
Chris@70: value on failure.
Chris@70: The channel count can be retrieved on success by calling
Chris@70: op_head(_of,*_li)
.
Chris@70: The number of samples returned may be 0 if the buffer was too small
Chris@70: to store even a single sample for all channels, or if end-of-file
Chris@70: was reached.
Chris@70: The list of possible failure codes follows.
Chris@70: Most of them can only be returned by unseekable, chained streams
Chris@70: that encounter a new link.
Chris@70: \retval #OP_HOLE There was a hole in the data, and some samples
Chris@70: may have been skipped.
Chris@70: Call this function again to continue decoding
Chris@70: past the hole.
Chris@70: \retval #OP_EREAD An underlying read operation failed.
Chris@70: This may signal a truncation attack from an
Chris@70: source.
Chris@70: \retval #OP_EFAULT An internal memory allocation failed.
Chris@70: \retval #OP_EIMPL An unseekable stream encountered a new link that
Chris@70: used a feature that is not implemented, such as
Chris@70: an unsupported channel family.
Chris@70: \retval #OP_EINVAL The stream was only partially open.
Chris@70: \retval #OP_ENOTFORMAT An unseekable stream encountered a new link that
Chris@70: did not have any logical Opus streams in it.
Chris@70: \retval #OP_EBADHEADER An unseekable stream encountered a new link with a
Chris@70: required header packet that was not properly
Chris@70: formatted, contained illegal values, or was
Chris@70: missing altogether.
Chris@70: \retval #OP_EVERSION An unseekable stream encountered a new link with
Chris@70: an ID header that contained an unrecognized
Chris@70: version number.
Chris@70: \retval #OP_EBADPACKET Failed to properly decode the next packet.
Chris@70: \retval #OP_EBADLINK We failed to find data we had seen before.
Chris@70: \retval #OP_EBADTIMESTAMP An unseekable stream encountered a new link with
Chris@70: a starting timestamp that failed basic validity
Chris@70: checks.*/
Chris@70: OP_WARN_UNUSED_RESULT int op_read_float(OggOpusFile *_of,
Chris@70: float *_pcm,int _buf_size,int *_li) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Reads more samples from the stream and downmixes to stereo, if necessary.
Chris@70: This function is intended for simple players that want a uniform output
Chris@70: format, even if the channel count changes between links in a chained
Chris@70: stream.
Chris@70: \note \a _buf_size indicates the total number of values that can be stored
Chris@70: in \a _pcm, while the return value is the number of samples per
Chris@70: channel, even though the channel count is known, for consistency with
Chris@70: op_read().
Chris@70: \param _of The \c OggOpusFile from which to read.
Chris@70: \param[out] _pcm A buffer in which to store the output PCM samples, as
Chris@70: signed native-endian 16-bit values at 48 kHz
Chris@70: with a nominal range of [-32768,32767)
.
Chris@70: The left and right channels are interleaved in the
Chris@70: buffer.
Chris@70: This must have room for at least \a _buf_size values.
Chris@70: \param _buf_size The number of values that can be stored in \a _pcm.
Chris@70: It is recommended that this be large enough for at
Chris@70: least 120 ms of data at 48 kHz per channel (11520
Chris@70: values total).
Chris@70: Smaller buffers will simply return less data, possibly
Chris@70: consuming more memory to buffer the data internally.
Chris@70: If less than \a _buf_size values are returned,
Chris@70: libopusfile makes no guarantee that the
Chris@70: remaining data in \a _pcm will be unmodified.
Chris@70: \return The number of samples read per channel on success, or a negative
Chris@70: value on failure.
Chris@70: The number of samples returned may be 0 if the buffer was too small
Chris@70: to store even a single sample for both channels, or if end-of-file
Chris@70: was reached.
Chris@70: The list of possible failure codes follows.
Chris@70: Most of them can only be returned by unseekable, chained streams
Chris@70: that encounter a new link.
Chris@70: \retval #OP_HOLE There was a hole in the data, and some samples
Chris@70: may have been skipped.
Chris@70: Call this function again to continue decoding
Chris@70: past the hole.
Chris@70: \retval #OP_EREAD An underlying read operation failed.
Chris@70: This may signal a truncation attack from an
Chris@70: source.
Chris@70: \retval #OP_EFAULT An internal memory allocation failed.
Chris@70: \retval #OP_EIMPL An unseekable stream encountered a new link that
Chris@70: used a feature that is not implemented, such as
Chris@70: an unsupported channel family.
Chris@70: \retval #OP_EINVAL The stream was only partially open.
Chris@70: \retval #OP_ENOTFORMAT An unseekable stream encountered a new link that
Chris@70: did not have any logical Opus streams in it.
Chris@70: \retval #OP_EBADHEADER An unseekable stream encountered a new link with a
Chris@70: required header packet that was not properly
Chris@70: formatted, contained illegal values, or was
Chris@70: missing altogether.
Chris@70: \retval #OP_EVERSION An unseekable stream encountered a new link with
Chris@70: an ID header that contained an unrecognized
Chris@70: version number.
Chris@70: \retval #OP_EBADPACKET Failed to properly decode the next packet.
Chris@70: \retval #OP_EBADLINK We failed to find data we had seen before.
Chris@70: \retval #OP_EBADTIMESTAMP An unseekable stream encountered a new link with
Chris@70: a starting timestamp that failed basic validity
Chris@70: checks.*/
Chris@70: OP_WARN_UNUSED_RESULT int op_read_stereo(OggOpusFile *_of,
Chris@70: opus_int16 *_pcm,int _buf_size) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /**Reads more samples from the stream and downmixes to stereo, if necessary.
Chris@70: This function is intended for simple players that want a uniform output
Chris@70: format, even if the channel count changes between links in a chained
Chris@70: stream.
Chris@70: \note \a _buf_size indicates the total number of values that can be stored
Chris@70: in \a _pcm, while the return value is the number of samples per
Chris@70: channel, even though the channel count is known, for consistency with
Chris@70: op_read_float().
Chris@70: \param _of The \c OggOpusFile from which to read.
Chris@70: \param[out] _pcm A buffer in which to store the output PCM samples, as
Chris@70: signed floats at 48 kHz with a nominal range of
Chris@70: [-1.0,1.0]
.
Chris@70: The left and right channels are interleaved in the
Chris@70: buffer.
Chris@70: This must have room for at least \a _buf_size values.
Chris@70: \param _buf_size The number of values that can be stored in \a _pcm.
Chris@70: It is recommended that this be large enough for at
Chris@70: least 120 ms of data at 48 kHz per channel (11520
Chris@70: values total).
Chris@70: Smaller buffers will simply return less data, possibly
Chris@70: consuming more memory to buffer the data internally.
Chris@70: If less than \a _buf_size values are returned,
Chris@70: libopusfile makes no guarantee that the
Chris@70: remaining data in \a _pcm will be unmodified.
Chris@70: \return The number of samples read per channel on success, or a negative
Chris@70: value on failure.
Chris@70: The number of samples returned may be 0 if the buffer was too small
Chris@70: to store even a single sample for both channels, or if end-of-file
Chris@70: was reached.
Chris@70: The list of possible failure codes follows.
Chris@70: Most of them can only be returned by unseekable, chained streams
Chris@70: that encounter a new link.
Chris@70: \retval #OP_HOLE There was a hole in the data, and some samples
Chris@70: may have been skipped.
Chris@70: Call this function again to continue decoding
Chris@70: past the hole.
Chris@70: \retval #OP_EREAD An underlying read operation failed.
Chris@70: This may signal a truncation attack from an
Chris@70: source.
Chris@70: \retval #OP_EFAULT An internal memory allocation failed.
Chris@70: \retval #OP_EIMPL An unseekable stream encountered a new link that
Chris@70: used a feature that is not implemented, such as
Chris@70: an unsupported channel family.
Chris@70: \retval #OP_EINVAL The stream was only partially open.
Chris@70: \retval #OP_ENOTFORMAT An unseekable stream encountered a new link that
Chris@70: that did not have any logical Opus streams in it.
Chris@70: \retval #OP_EBADHEADER An unseekable stream encountered a new link with a
Chris@70: required header packet that was not properly
Chris@70: formatted, contained illegal values, or was
Chris@70: missing altogether.
Chris@70: \retval #OP_EVERSION An unseekable stream encountered a new link with
Chris@70: an ID header that contained an unrecognized
Chris@70: version number.
Chris@70: \retval #OP_EBADPACKET Failed to properly decode the next packet.
Chris@70: \retval #OP_EBADLINK We failed to find data we had seen before.
Chris@70: \retval #OP_EBADTIMESTAMP An unseekable stream encountered a new link with
Chris@70: a starting timestamp that failed basic validity
Chris@70: checks.*/
Chris@70: OP_WARN_UNUSED_RESULT int op_read_float_stereo(OggOpusFile *_of,
Chris@70: float *_pcm,int _buf_size) OP_ARG_NONNULL(1);
Chris@70:
Chris@70: /*@}*/
Chris@70: /*@}*/
Chris@70:
Chris@70: # if OP_GNUC_PREREQ(4,0)
Chris@70: # pragma GCC visibility pop
Chris@70: # endif
Chris@70:
Chris@70: # if defined(__cplusplus)
Chris@70: }
Chris@70: # endif
Chris@70:
Chris@70: #endif