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