opusfile.h 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE libopusfile SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE libopusfile SOURCE CODE IS (C) COPYRIGHT 1994-2012 *
  9. * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: stdio-based convenience library for opening/seeking/decoding
  13. last mod: $Id: vorbisfile.h 17182 2010-04-29 03:48:32Z xiphmont $
  14. ********************************************************************/
  15. #if !defined(_opusfile_h)
  16. # define _opusfile_h (1)
  17. /**\mainpage
  18. \section Introduction
  19. This is the documentation for the <tt>libopusfile</tt> C API.
  20. The <tt>libopusfile</tt> package provides a convenient high-level API for
  21. decoding and basic manipulation of all Ogg Opus audio streams.
  22. <tt>libopusfile</tt> is implemented as a layer on top of Xiph.Org's
  23. reference
  24. <tt><a href="https://www.xiph.org/ogg/doc/libogg/reference.html">libogg</a></tt>
  25. and
  26. <tt><a href="https://mf4.xiph.org/jenkins/view/opus/job/opus/ws/doc/html/index.html">libopus</a></tt>
  27. libraries.
  28. <tt>libopusfile</tt> provides several sets of built-in routines for
  29. file/stream access, and may also use custom stream I/O routines provided by
  30. the embedded environment.
  31. There are built-in I/O routines provided for ANSI-compliant
  32. <code>stdio</code> (<code>FILE *</code>), memory buffers, and URLs
  33. (including <file:> URLs, plus optionally <http:> and <https:> URLs).
  34. \section Organization
  35. The main API is divided into several sections:
  36. - \ref stream_open_close
  37. - \ref stream_info
  38. - \ref stream_decoding
  39. - \ref stream_seeking
  40. Several additional sections are not tied to the main API.
  41. - \ref stream_callbacks
  42. - \ref header_info
  43. - \ref error_codes
  44. \section Overview
  45. The <tt>libopusfile</tt> API always decodes files to 48&nbsp;kHz.
  46. The original sample rate is not preserved by the lossy compression, though
  47. it is stored in the header to allow you to resample to it after decoding
  48. (the <tt>libopusfile</tt> API does not currently provide a resampler,
  49. but the
  50. <a href="http://www.speex.org/docs/manual/speex-manual/node7.html#SECTION00760000000000000000">the
  51. Speex resampler</a> is a good choice if you need one).
  52. In general, if you are playing back the audio, you should leave it at
  53. 48&nbsp;kHz, provided your audio hardware supports it.
  54. When decoding to a file, it may be worth resampling back to the original
  55. sample rate, so as not to surprise users who might not expect the sample
  56. rate to change after encoding to Opus and decoding.
  57. Opus files can contain anywhere from 1 to 255 channels of audio.
  58. The channel mappings for up to 8 channels are the same as the
  59. <a href="http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9">Vorbis
  60. mappings</a>.
  61. A special stereo API can convert everything to 2 channels, making it simple
  62. to support multichannel files in an application which only has stereo
  63. output.
  64. Although the <tt>libopusfile</tt> ABI provides support for the theoretical
  65. maximum number of channels, the current implementation does not support
  66. files with more than 8 channels, as they do not have well-defined channel
  67. mappings.
  68. Like all Ogg files, Opus files may be "chained".
  69. That is, multiple Opus files may be combined into a single, longer file just
  70. by concatenating the original files.
  71. This is commonly done in internet radio streaming, as it allows the title
  72. and artist to be updated each time the song changes, since each link in the
  73. chain includes its own set of metadata.
  74. <tt>libopusfile</tt> fully supports chained files.
  75. It will decode the first Opus stream found in each link of a chained file
  76. (ignoring any other streams that might be concurrently multiplexed with it,
  77. such as a video stream).
  78. The channel count can also change between links.
  79. If your application is not prepared to deal with this, it can use the stereo
  80. API to ensure the audio from all links will always get decoded into a
  81. common format.
  82. Since <tt>libopusfile</tt> always decodes to 48&nbsp;kHz, you do not have to
  83. worry about the sample rate changing between links (as was possible with
  84. Vorbis).
  85. This makes application support for chained files with <tt>libopusfile</tt>
  86. very easy.*/
  87. # if defined(__cplusplus)
  88. extern "C" {
  89. # endif
  90. # include <stdarg.h>
  91. # include <stdio.h>
  92. # include <ogg/ogg.h>
  93. # include <opus_multistream.h>
  94. /**@cond PRIVATE*/
  95. /*Enable special features for gcc and gcc-compatible compilers.*/
  96. # if !defined(OP_GNUC_PREREQ)
  97. # if defined(__GNUC__)&&defined(__GNUC_MINOR__)
  98. # define OP_GNUC_PREREQ(_maj,_min) \
  99. ((__GNUC__<<16)+__GNUC_MINOR__>=((_maj)<<16)+(_min))
  100. # else
  101. # define OP_GNUC_PREREQ(_maj,_min) 0
  102. # endif
  103. # endif
  104. # if OP_GNUC_PREREQ(4,0)
  105. # pragma GCC visibility push(default)
  106. # endif
  107. typedef struct OpusHead OpusHead;
  108. typedef struct OpusTags OpusTags;
  109. typedef struct OpusPictureTag OpusPictureTag;
  110. typedef struct OpusServerInfo OpusServerInfo;
  111. typedef struct OpusFileCallbacks OpusFileCallbacks;
  112. typedef struct OggOpusFile OggOpusFile;
  113. /*Warning attributes for libopusfile functions.*/
  114. # if OP_GNUC_PREREQ(3,4)
  115. # define OP_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
  116. # else
  117. # define OP_WARN_UNUSED_RESULT
  118. # endif
  119. # if OP_GNUC_PREREQ(3,4)
  120. # define OP_ARG_NONNULL(_x) __attribute__((__nonnull__(_x)))
  121. # else
  122. # define OP_ARG_NONNULL(_x)
  123. # endif
  124. /**@endcond*/
  125. /**\defgroup error_codes Error Codes*/
  126. /*@{*/
  127. /**\name List of possible error codes
  128. Many of the functions in this library return a negative error code when a
  129. function fails.
  130. This list provides a brief explanation of the common errors.
  131. See each individual function for more details on what a specific error code
  132. means in that context.*/
  133. /*@{*/
  134. /**A request did not succeed.*/
  135. #define OP_FALSE (-1)
  136. /*Currently not used externally.*/
  137. #define OP_EOF (-2)
  138. /**There was a hole in the page sequence numbers (e.g., a page was corrupt or
  139. missing).*/
  140. #define OP_HOLE (-3)
  141. /**An underlying read, seek, or tell operation failed when it should have
  142. succeeded.*/
  143. #define OP_EREAD (-128)
  144. /**A <code>NULL</code> pointer was passed where one was unexpected, or an
  145. internal memory allocation failed, or an internal library error was
  146. encountered.*/
  147. #define OP_EFAULT (-129)
  148. /**The stream used a feature that is not implemented, such as an unsupported
  149. channel family.*/
  150. #define OP_EIMPL (-130)
  151. /**One or more parameters to a function were invalid.*/
  152. #define OP_EINVAL (-131)
  153. /**A purported Ogg Opus stream did not begin with an Ogg page, a purported
  154. header packet did not start with one of the required strings, "OpusHead" or
  155. "OpusTags", or a link in a chained file was encountered that did not
  156. contain any logical Opus streams.*/
  157. #define OP_ENOTFORMAT (-132)
  158. /**A required header packet was not properly formatted, contained illegal
  159. values, or was missing altogether.*/
  160. #define OP_EBADHEADER (-133)
  161. /**The ID header contained an unrecognized version number.*/
  162. #define OP_EVERSION (-134)
  163. /*Currently not used at all.*/
  164. #define OP_ENOTAUDIO (-135)
  165. /**An audio packet failed to decode properly.
  166. This is usually caused by a multistream Ogg packet where the durations of
  167. the individual Opus packets contained in it are not all the same.*/
  168. #define OP_EBADPACKET (-136)
  169. /**We failed to find data we had seen before, or the bitstream structure was
  170. sufficiently malformed that seeking to the target destination was
  171. impossible.*/
  172. #define OP_EBADLINK (-137)
  173. /**An operation that requires seeking was requested on an unseekable stream.*/
  174. #define OP_ENOSEEK (-138)
  175. /**The first or last granule position of a link failed basic validity checks.*/
  176. #define OP_EBADTIMESTAMP (-139)
  177. /*@}*/
  178. /*@}*/
  179. /**\defgroup header_info Header Information*/
  180. /*@{*/
  181. /**The maximum number of channels in an Ogg Opus stream.*/
  182. #define OPUS_CHANNEL_COUNT_MAX (255)
  183. /**Ogg Opus bitstream information.
  184. This contains the basic playback parameters for a stream, and corresponds to
  185. the initial ID header packet of an Ogg Opus stream.*/
  186. struct OpusHead{
  187. /**The Ogg Opus format version, in the range 0...255.
  188. The top 4 bits represent a "major" version, and the bottom four bits
  189. represent backwards-compatible "minor" revisions.
  190. The current specification describes version 1.
  191. This library will recognize versions up through 15 as backwards compatible
  192. with the current specification.
  193. An earlier draft of the specification described a version 0, but the only
  194. difference between version 1 and version 0 is that version 0 did
  195. not specify the semantics for handling the version field.*/
  196. int version;
  197. /**The number of channels, in the range 1...255.*/
  198. int channel_count;
  199. /**The number of samples that should be discarded from the beginning of the
  200. stream.*/
  201. unsigned pre_skip;
  202. /**The sampling rate of the original input.
  203. All Opus audio is coded at 48 kHz, and should also be decoded at 48 kHz
  204. for playback (unless the target hardware does not support this sampling
  205. rate).
  206. However, this field may be used to resample the audio back to the original
  207. sampling rate, for example, when saving the output to a file.*/
  208. opus_uint32 input_sample_rate;
  209. /**The gain to apply to the decoded output, in dB, as a Q8 value in the range
  210. -32768...32767.
  211. The <tt>libopusfile</tt> API will automatically apply this gain to the
  212. decoded output before returning it, scaling it by
  213. <code>pow(10,output_gain/(20.0*256))</code>.*/
  214. int output_gain;
  215. /**The channel mapping family, in the range 0...255.
  216. Channel mapping family 0 covers mono or stereo in a single stream.
  217. Channel mapping family 1 covers 1 to 8 channels in one or more streams,
  218. using the Vorbis speaker assignments.
  219. Channel mapping family 255 covers 1 to 255 channels in one or more
  220. streams, but without any defined speaker assignment.*/
  221. int mapping_family;
  222. /**The number of Opus streams in each Ogg packet, in the range 1...255.*/
  223. int stream_count;
  224. /**The number of coupled Opus streams in each Ogg packet, in the range
  225. 0...127.
  226. This must satisfy <code>0 <= coupled_count <= stream_count</code> and
  227. <code>coupled_count + stream_count <= 255</code>.
  228. The coupled streams appear first, before all uncoupled streams, in an Ogg
  229. Opus packet.*/
  230. int coupled_count;
  231. /**The mapping from coded stream channels to output channels.
  232. Let <code>index=mapping[k]</code> be the value for channel <code>k</code>.
  233. If <code>index<2*coupled_count</code>, then it refers to the left channel
  234. from stream <code>(index/2)</code> if even, and the right channel from
  235. stream <code>(index/2)</code> if odd.
  236. Otherwise, it refers to the output of the uncoupled stream
  237. <code>(index-coupled_count)</code>.*/
  238. unsigned char mapping[OPUS_CHANNEL_COUNT_MAX];
  239. };
  240. /**The metadata from an Ogg Opus stream.
  241. This structure holds the in-stream metadata corresponding to the 'comment'
  242. header packet of an Ogg Opus stream.
  243. The comment header is meant to be used much like someone jotting a quick
  244. note on the label of a CD.
  245. It should be a short, to the point text note that can be more than a couple
  246. words, but not more than a short paragraph.
  247. The metadata is stored as a series of (tag, value) pairs, in length-encoded
  248. string vectors, using the same format as Vorbis (without the final "framing
  249. bit"), Theora, and Speex, except for the packet header.
  250. The first occurrence of the '=' character delimits the tag and value.
  251. A particular tag may occur more than once, and order is significant.
  252. The character set encoding for the strings is always UTF-8, but the tag
  253. names are limited to ASCII, and treated as case-insensitive.
  254. See <a href="http://www.xiph.org/vorbis/doc/v-comment.html">the Vorbis
  255. comment header specification</a> for details.
  256. In filling in this structure, <tt>libopusfile</tt> will null-terminate the
  257. #user_comments strings for safety.
  258. However, the bitstream format itself treats them as 8-bit clean vectors,
  259. possibly containing NUL characters, so the #comment_lengths array should be
  260. treated as their authoritative length.
  261. This structure is binary and source-compatible with a
  262. <code>vorbis_comment</code>, and pointers to it may be freely cast to
  263. <code>vorbis_comment</code> pointers, and vice versa.
  264. It is provided as a separate type to avoid introducing a compile-time
  265. dependency on the libvorbis headers.*/
  266. struct OpusTags{
  267. /**The array of comment string vectors.*/
  268. char **user_comments;
  269. /**An array of the corresponding length of each vector, in bytes.*/
  270. int *comment_lengths;
  271. /**The total number of comment streams.*/
  272. int comments;
  273. /**The null-terminated vendor string.
  274. This identifies the software used to encode the stream.*/
  275. char *vendor;
  276. };
  277. /**\name Picture tag image formats*/
  278. /*@{*/
  279. /**The MIME type was not recognized, or the image data did not match the
  280. declared MIME type.*/
  281. #define OP_PIC_FORMAT_UNKNOWN (-1)
  282. /**The MIME type indicates the image data is really a URL.*/
  283. #define OP_PIC_FORMAT_URL (0)
  284. /**The image is a JPEG.*/
  285. #define OP_PIC_FORMAT_JPEG (1)
  286. /**The image is a PNG.*/
  287. #define OP_PIC_FORMAT_PNG (2)
  288. /**The image is a GIF.*/
  289. #define OP_PIC_FORMAT_GIF (3)
  290. /*@}*/
  291. /**The contents of a METADATA_BLOCK_PICTURE tag.*/
  292. struct OpusPictureTag{
  293. /**The picture type according to the ID3v2 APIC frame:
  294. <ol start="0">
  295. <li>Other</li>
  296. <li>32x32 pixels 'file icon' (PNG only)</li>
  297. <li>Other file icon</li>
  298. <li>Cover (front)</li>
  299. <li>Cover (back)</li>
  300. <li>Leaflet page</li>
  301. <li>Media (e.g. label side of CD)</li>
  302. <li>Lead artist/lead performer/soloist</li>
  303. <li>Artist/performer</li>
  304. <li>Conductor</li>
  305. <li>Band/Orchestra</li>
  306. <li>Composer</li>
  307. <li>Lyricist/text writer</li>
  308. <li>Recording Location</li>
  309. <li>During recording</li>
  310. <li>During performance</li>
  311. <li>Movie/video screen capture</li>
  312. <li>A bright colored fish</li>
  313. <li>Illustration</li>
  314. <li>Band/artist logotype</li>
  315. <li>Publisher/Studio logotype</li>
  316. </ol>
  317. Others are reserved and should not be used.
  318. There may only be one each of picture type 1 and 2 in a file.*/
  319. opus_int32 type;
  320. /**The MIME type of the picture, in printable ASCII characters 0x20-0x7E.
  321. The MIME type may also be <code>"-->"</code> to signify that the data part
  322. is a URL pointing to the picture instead of the picture data itself.
  323. In this case, a terminating NUL is appended to the URL string in #data,
  324. but #data_length is set to the length of the string excluding that
  325. terminating NUL.*/
  326. char *mime_type;
  327. /**The description of the picture, in UTF-8.*/
  328. char *description;
  329. /**The width of the picture in pixels.*/
  330. opus_uint32 width;
  331. /**The height of the picture in pixels.*/
  332. opus_uint32 height;
  333. /**The color depth of the picture in bits-per-pixel (<em>not</em>
  334. bits-per-channel).*/
  335. opus_uint32 depth;
  336. /**For indexed-color pictures (e.g., GIF), the number of colors used, or 0
  337. for non-indexed pictures.*/
  338. opus_uint32 colors;
  339. /**The length of the picture data in bytes.*/
  340. opus_uint32 data_length;
  341. /**The binary picture data.*/
  342. unsigned char *data;
  343. /**The format of the picture data, if known.
  344. One of
  345. <ul>
  346. <li>#OP_PIC_FORMAT_UNKNOWN,</li>
  347. <li>#OP_PIC_FORMAT_URL,</li>
  348. <li>#OP_PIC_FORMAT_JPEG,</li>
  349. <li>#OP_PIC_FORMAT_PNG, or</li>
  350. <li>#OP_PIC_FORMAT_GIF.</li>
  351. </ul>*/
  352. int format;
  353. };
  354. /**\name Functions for manipulating header data
  355. These functions manipulate the #OpusHead and #OpusTags structures,
  356. which describe the audio parameters and tag-value metadata, respectively.
  357. These can be used to query the headers returned by <tt>libopusfile</tt>, or
  358. to parse Opus headers from sources other than an Ogg Opus stream, provided
  359. they use the same format.*/
  360. /*@{*/
  361. /**Parses the contents of the ID header packet of an Ogg Opus stream.
  362. \param[out] _head Returns the contents of the parsed packet.
  363. The contents of this structure are untouched on error.
  364. This may be <code>NULL</code> to merely test the header
  365. for validity.
  366. \param[in] _data The contents of the ID header packet.
  367. \param _len The number of bytes of data in the ID header packet.
  368. \return 0 on success or a negative value on error.
  369. \retval #OP_ENOTFORMAT If the data does not start with the "OpusHead"
  370. string.
  371. \retval #OP_EVERSION If the version field signaled a version this library
  372. does not know how to parse.
  373. \retval #OP_EIMPL If the channel mapping family was 255, which general
  374. purpose players should not attempt to play.
  375. \retval #OP_EBADHEADER If the contents of the packet otherwise violate the
  376. Ogg Opus specification:
  377. <ul>
  378. <li>Insufficient data,</li>
  379. <li>Too much data for the known minor versions,</li>
  380. <li>An unrecognized channel mapping family,</li>
  381. <li>Zero channels or too many channels,</li>
  382. <li>Zero coded streams,</li>
  383. <li>Too many coupled streams, or</li>
  384. <li>An invalid channel mapping index.</li>
  385. </ul>*/
  386. OP_WARN_UNUSED_RESULT int opus_head_parse(OpusHead *_head,
  387. const unsigned char *_data,size_t _len) OP_ARG_NONNULL(2);
  388. /**Converts a granule position to a sample offset for a given Ogg Opus stream.
  389. The sample offset is simply <code>_gp-_head->pre_skip</code>.
  390. Granule position values smaller than OpusHead#pre_skip correspond to audio
  391. that should never be played, and thus have no associated sample offset.
  392. This function returns -1 for such values.
  393. This function also correctly handles extremely large granule positions,
  394. which may have wrapped around to a negative number when stored in a signed
  395. ogg_int64_t value.
  396. \param _head The #OpusHead information from the ID header of the stream.
  397. \param _gp The granule position to convert.
  398. \return The sample offset associated with the given granule position
  399. (counting at a 48 kHz sampling rate), or the special value -1 on
  400. error (i.e., the granule position was smaller than the pre-skip
  401. amount).*/
  402. ogg_int64_t opus_granule_sample(const OpusHead *_head,ogg_int64_t _gp)
  403. OP_ARG_NONNULL(1);
  404. /**Parses the contents of the 'comment' header packet of an Ogg Opus stream.
  405. \param[out] _tags An uninitialized #OpusTags structure.
  406. This returns the contents of the parsed packet.
  407. The contents of this structure are untouched on error.
  408. This may be <code>NULL</code> to merely test the header
  409. for validity.
  410. \param[in] _data The contents of the 'comment' header packet.
  411. \param _len The number of bytes of data in the 'info' header packet.
  412. \retval 0 Success.
  413. \retval #OP_ENOTFORMAT If the data does not start with the "OpusTags"
  414. string.
  415. \retval #OP_EBADHEADER If the contents of the packet otherwise violate the
  416. Ogg Opus specification.
  417. \retval #OP_EFAULT If there wasn't enough memory to store the tags.*/
  418. OP_WARN_UNUSED_RESULT int opus_tags_parse(OpusTags *_tags,
  419. const unsigned char *_data,size_t _len) OP_ARG_NONNULL(2);
  420. /**Performs a deep copy of an #OpusTags structure.
  421. \param _dst The #OpusTags structure to copy into.
  422. If this function fails, the contents of this structure remain
  423. untouched.
  424. \param _src The #OpusTags structure to copy from.
  425. \retval 0 Success.
  426. \retval #OP_EFAULT If there wasn't enough memory to copy the tags.*/
  427. int opus_tags_copy(OpusTags *_dst,const OpusTags *_src) OP_ARG_NONNULL(1);
  428. /**Initializes an #OpusTags structure.
  429. This should be called on a freshly allocated #OpusTags structure before
  430. attempting to use it.
  431. \param _tags The #OpusTags structure to initialize.*/
  432. void opus_tags_init(OpusTags *_tags) OP_ARG_NONNULL(1);
  433. /**Add a (tag, value) pair to an initialized #OpusTags structure.
  434. \note Neither opus_tags_add() nor opus_tags_add_comment() support values
  435. containing embedded NULs, although the bitstream format does support them.
  436. To add such tags, you will need to manipulate the #OpusTags structure
  437. directly.
  438. \param _tags The #OpusTags structure to add the (tag, value) pair to.
  439. \param _tag A NUL-terminated, case-insensitive, ASCII string containing
  440. the tag to add (without an '=' character).
  441. \param _value A NUL-terminated UTF-8 containing the corresponding value.
  442. \return 0 on success, or a negative value on failure.
  443. \retval #OP_EFAULT An internal memory allocation failed.*/
  444. int opus_tags_add(OpusTags *_tags,const char *_tag,const char *_value)
  445. OP_ARG_NONNULL(1) OP_ARG_NONNULL(2) OP_ARG_NONNULL(3);
  446. /**Add a comment to an initialized #OpusTags structure.
  447. \note Neither opus_tags_add_comment() nor opus_tags_add() support comments
  448. containing embedded NULs, although the bitstream format does support them.
  449. To add such tags, you will need to manipulate the #OpusTags structure
  450. directly.
  451. \param _tags The #OpusTags structure to add the comment to.
  452. \param _comment A NUL-terminated UTF-8 string containing the comment in
  453. "TAG=value" form.
  454. \return 0 on success, or a negative value on failure.
  455. \retval #OP_EFAULT An internal memory allocation failed.*/
  456. int opus_tags_add_comment(OpusTags *_tags,const char *_comment)
  457. OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
  458. /**Replace the binary suffix data at the end of the packet (if any).
  459. \param _tags An initialized #OpusTags structure.
  460. \param _data A buffer of binary data to append after the encoded user
  461. comments.
  462. The least significant bit of the first byte of this data must
  463. be set (to ensure the data is preserved by other editors).
  464. \param _len The number of bytes of binary data to append.
  465. This may be zero to remove any existing binary suffix data.
  466. \return 0 on success, or a negative value on error.
  467. \retval #OP_EINVAL \a _len was negative, or \a _len was positive but
  468. \a _data was <code>NULL</code> or the least significant
  469. bit of the first byte was not set.
  470. \retval #OP_EFAULT An internal memory allocation failed.*/
  471. int opus_tags_set_binary_suffix(OpusTags *_tags,
  472. const unsigned char *_data,int _len) OP_ARG_NONNULL(1);
  473. /**Look up a comment value by its tag.
  474. \param _tags An initialized #OpusTags structure.
  475. \param _tag The tag to look up.
  476. \param _count The instance of the tag.
  477. The same tag can appear multiple times, each with a distinct
  478. value, so an index is required to retrieve them all.
  479. The order in which these values appear is significant and
  480. should be preserved.
  481. Use opus_tags_query_count() to get the legal range for the
  482. \a _count parameter.
  483. \return A pointer to the queried tag's value.
  484. This points directly to data in the #OpusTags structure.
  485. It should not be modified or freed by the application, and
  486. modifications to the structure may invalidate the pointer.
  487. \retval NULL If no matching tag is found.*/
  488. const char *opus_tags_query(const OpusTags *_tags,const char *_tag,int _count)
  489. OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
  490. /**Look up the number of instances of a tag.
  491. Call this first when querying for a specific tag and then iterate over the
  492. number of instances with separate calls to opus_tags_query() to retrieve
  493. all the values for that tag in order.
  494. \param _tags An initialized #OpusTags structure.
  495. \param _tag The tag to look up.
  496. \return The number of instances of this particular tag.*/
  497. int opus_tags_query_count(const OpusTags *_tags,const char *_tag)
  498. OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
  499. /**Retrieve the binary suffix data at the end of the packet (if any).
  500. \param _tags An initialized #OpusTags structure.
  501. \param[out] _len Returns the number of bytes of binary suffix data returned.
  502. \return A pointer to the binary suffix data, or <code>NULL</code> if none
  503. was present.*/
  504. const unsigned char *opus_tags_get_binary_suffix(const OpusTags *_tags,
  505. int *_len) OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
  506. /**Get the album gain from an R128_ALBUM_GAIN tag, if one was specified.
  507. This searches for the first R128_ALBUM_GAIN tag with a valid signed,
  508. 16-bit decimal integer value and returns the value.
  509. This routine is exposed merely for convenience for applications which wish
  510. to do something special with the album gain (i.e., display it).
  511. If you simply wish to apply the album gain instead of the header gain, you
  512. can use op_set_gain_offset() with an #OP_ALBUM_GAIN type and no offset.
  513. \param _tags An initialized #OpusTags structure.
  514. \param[out] _gain_q8 The album gain, in 1/256ths of a dB.
  515. This will lie in the range [-32768,32767], and should
  516. be applied in <em>addition</em> to the header gain.
  517. On error, no value is returned, and the previous
  518. contents remain unchanged.
  519. \return 0 on success, or a negative value on error.
  520. \retval #OP_FALSE There was no album gain available in the given tags.*/
  521. int opus_tags_get_album_gain(const OpusTags *_tags,int *_gain_q8)
  522. OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
  523. /**Get the track gain from an R128_TRACK_GAIN tag, if one was specified.
  524. This searches for the first R128_TRACK_GAIN tag with a valid signed,
  525. 16-bit decimal integer value and returns the value.
  526. This routine is exposed merely for convenience for applications which wish
  527. to do something special with the track gain (i.e., display it).
  528. If you simply wish to apply the track gain instead of the header gain, you
  529. can use op_set_gain_offset() with an #OP_TRACK_GAIN type and no offset.
  530. \param _tags An initialized #OpusTags structure.
  531. \param[out] _gain_q8 The track gain, in 1/256ths of a dB.
  532. This will lie in the range [-32768,32767], and should
  533. be applied in <em>addition</em> to the header gain.
  534. On error, no value is returned, and the previous
  535. contents remain unchanged.
  536. \return 0 on success, or a negative value on error.
  537. \retval #OP_FALSE There was no track gain available in the given tags.*/
  538. int opus_tags_get_track_gain(const OpusTags *_tags,int *_gain_q8)
  539. OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
  540. /**Clears the #OpusTags structure.
  541. This should be called on an #OpusTags structure after it is no longer
  542. needed.
  543. It will free all memory used by the structure members.
  544. \param _tags The #OpusTags structure to clear.*/
  545. void opus_tags_clear(OpusTags *_tags) OP_ARG_NONNULL(1);
  546. /**Check if \a _comment is an instance of a \a _tag_name tag.
  547. \see opus_tagncompare
  548. \param _tag_name A NUL-terminated, case-insensitive, ASCII string containing
  549. the name of the tag to check for (without the terminating
  550. '=' character).
  551. \param _comment The comment string to check.
  552. \return An integer less than, equal to, or greater than zero if \a _comment
  553. is found respectively, to be less than, to match, or be greater
  554. than a "tag=value" string whose tag matches \a _tag_name.*/
  555. int opus_tagcompare(const char *_tag_name,const char *_comment);
  556. /**Check if \a _comment is an instance of a \a _tag_name tag.
  557. This version is slightly more efficient than opus_tagcompare() if the length
  558. of the tag name is already known (e.g., because it is a constant).
  559. \see opus_tagcompare
  560. \param _tag_name A case-insensitive ASCII string containing the name of the
  561. tag to check for (without the terminating '=' character).
  562. \param _tag_len The number of characters in the tag name.
  563. This must be non-negative.
  564. \param _comment The comment string to check.
  565. \return An integer less than, equal to, or greater than zero if \a _comment
  566. is found respectively, to be less than, to match, or be greater
  567. than a "tag=value" string whose tag matches the first \a _tag_len
  568. characters of \a _tag_name.*/
  569. int opus_tagncompare(const char *_tag_name,int _tag_len,const char *_comment);
  570. /**Parse a single METADATA_BLOCK_PICTURE tag.
  571. This decodes the BASE64-encoded content of the tag and returns a structure
  572. with the MIME type, description, image parameters (if known), and the
  573. compressed image data.
  574. If the MIME type indicates the presence of an image format we recognize
  575. (JPEG, PNG, or GIF) and the actual image data contains the magic signature
  576. associated with that format, then the OpusPictureTag::format field will be
  577. set to the corresponding format.
  578. This is provided as a convenience to avoid requiring applications to parse
  579. the MIME type and/or do their own format detection for the commonly used
  580. formats.
  581. In this case, we also attempt to extract the image parameters directly from
  582. the image data (overriding any that were present in the tag, which the
  583. specification says applications are not meant to rely on).
  584. The application must still provide its own support for actually decoding the
  585. image data and, if applicable, retrieving that data from URLs.
  586. \param[out] _pic Returns the parsed picture data.
  587. No sanitation is done on the type, MIME type, or
  588. description fields, so these might return invalid values.
  589. The contents of this structure are left unmodified on
  590. failure.
  591. \param _tag The METADATA_BLOCK_PICTURE tag contents.
  592. The leading "METADATA_BLOCK_PICTURE=" portion is optional,
  593. to allow the function to be used on either directly on the
  594. values in OpusTags::user_comments or on the return value
  595. of opus_tags_query().
  596. \return 0 on success or a negative value on error.
  597. \retval #OP_ENOTFORMAT The METADATA_BLOCK_PICTURE contents were not valid.
  598. \retval #OP_EFAULT There was not enough memory to store the picture tag
  599. contents.*/
  600. OP_WARN_UNUSED_RESULT int opus_picture_tag_parse(OpusPictureTag *_pic,
  601. const char *_tag) OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
  602. /**Initializes an #OpusPictureTag structure.
  603. This should be called on a freshly allocated #OpusPictureTag structure
  604. before attempting to use it.
  605. \param _pic The #OpusPictureTag structure to initialize.*/
  606. void opus_picture_tag_init(OpusPictureTag *_pic) OP_ARG_NONNULL(1);
  607. /**Clears the #OpusPictureTag structure.
  608. This should be called on an #OpusPictureTag structure after it is no longer
  609. needed.
  610. It will free all memory used by the structure members.
  611. \param _pic The #OpusPictureTag structure to clear.*/
  612. void opus_picture_tag_clear(OpusPictureTag *_pic) OP_ARG_NONNULL(1);
  613. /*@}*/
  614. /*@}*/
  615. /**\defgroup url_options URL Reading Options*/
  616. /*@{*/
  617. /**\name URL reading options
  618. Options for op_url_stream_create() and associated functions.
  619. These allow you to provide proxy configuration parameters, skip SSL
  620. certificate checks, etc.
  621. Options are processed in order, and if the same option is passed multiple
  622. times, only the value specified by the last occurrence has an effect
  623. (unless otherwise specified).
  624. They may be expanded in the future.*/
  625. /*@{*/
  626. /**@cond PRIVATE*/
  627. /*These are the raw numbers used to define the request codes.
  628. They should not be used directly.*/
  629. #define OP_SSL_SKIP_CERTIFICATE_CHECK_REQUEST (6464)
  630. #define OP_HTTP_PROXY_HOST_REQUEST (6528)
  631. #define OP_HTTP_PROXY_PORT_REQUEST (6592)
  632. #define OP_HTTP_PROXY_USER_REQUEST (6656)
  633. #define OP_HTTP_PROXY_PASS_REQUEST (6720)
  634. #define OP_GET_SERVER_INFO_REQUEST (6784)
  635. #define OP_URL_OPT(_request) ((_request)+(char *)0)
  636. /*These macros trigger compilation errors or warnings if the wrong types are
  637. provided to one of the URL options.*/
  638. #define OP_CHECK_INT(_x) ((void)((_x)==(opus_int32)0),(opus_int32)(_x))
  639. #define OP_CHECK_CONST_CHAR_PTR(_x) ((_x)+((_x)-(const char *)(_x)))
  640. #define OP_CHECK_SERVER_INFO_PTR(_x) ((_x)+((_x)-(OpusServerInfo *)(_x)))
  641. /**@endcond*/
  642. /**HTTP/Shoutcast/Icecast server information associated with a URL.*/
  643. struct OpusServerInfo{
  644. /**The name of the server (icy-name/ice-name).
  645. This is <code>NULL</code> if there was no <code>icy-name</code> or
  646. <code>ice-name</code> header.*/
  647. char *name;
  648. /**A short description of the server (icy-description/ice-description).
  649. This is <code>NULL</code> if there was no <code>icy-description</code> or
  650. <code>ice-description</code> header.*/
  651. char *description;
  652. /**The genre the server falls under (icy-genre/ice-genre).
  653. This is <code>NULL</code> if there was no <code>icy-genre</code> or
  654. <code>ice-genre</code> header.*/
  655. char *genre;
  656. /**The homepage for the server (icy-url/ice-url).
  657. This is <code>NULL</code> if there was no <code>icy-url</code> or
  658. <code>ice-url</code> header.*/
  659. char *url;
  660. /**The software used by the origin server (Server).
  661. This is <code>NULL</code> if there was no <code>Server</code> header.*/
  662. char *server;
  663. /**The media type of the entity sent to the recepient (Content-Type).
  664. This is <code>NULL</code> if there was no <code>Content-Type</code>
  665. header.*/
  666. char *content_type;
  667. /**The nominal stream bitrate in kbps (icy-br/ice-bitrate).
  668. This is <code>-1</code> if there was no <code>icy-br</code> or
  669. <code>ice-bitrate</code> header.*/
  670. opus_int32 bitrate_kbps;
  671. /**Flag indicating whether the server is public (<code>1</code>) or not
  672. (<code>0</code>) (icy-pub/ice-public).
  673. This is <code>-1</code> if there was no <code>icy-pub</code> or
  674. <code>ice-public</code> header.*/
  675. int is_public;
  676. /**Flag indicating whether the server is using HTTPS instead of HTTP.
  677. This is <code>0</code> unless HTTPS is being used.
  678. This may not match the protocol used in the original URL if there were
  679. redirections.*/
  680. int is_ssl;
  681. };
  682. /**Initializes an #OpusServerInfo structure.
  683. All fields are set as if the corresponding header was not available.
  684. \param _info The #OpusServerInfo structure to initialize.
  685. \note If you use this function, you must link against <tt>libopusurl</tt>.*/
  686. void opus_server_info_init(OpusServerInfo *_info) OP_ARG_NONNULL(1);
  687. /**Clears the #OpusServerInfo structure.
  688. This should be called on an #OpusServerInfo structure after it is no longer
  689. needed.
  690. It will free all memory used by the structure members.
  691. \param _info The #OpusServerInfo structure to clear.
  692. \note If you use this function, you must link against <tt>libopusurl</tt>.*/
  693. void opus_server_info_clear(OpusServerInfo *_info) OP_ARG_NONNULL(1);
  694. /**Skip the certificate check when connecting via TLS/SSL (https).
  695. \param _b <code>opus_int32</code>: Whether or not to skip the certificate
  696. check.
  697. The check will be skipped if \a _b is non-zero, and will not be
  698. skipped if \a _b is zero.
  699. \hideinitializer*/
  700. #define OP_SSL_SKIP_CERTIFICATE_CHECK(_b) \
  701. OP_URL_OPT(OP_SSL_SKIP_CERTIFICATE_CHECK_REQUEST),OP_CHECK_INT(_b)
  702. /**Proxy connections through the given host.
  703. If no port is specified via #OP_HTTP_PROXY_PORT, the port number defaults
  704. to 8080 (http-alt).
  705. All proxy parameters are ignored for non-http and non-https URLs.
  706. \param _host <code>const char *</code>: The proxy server hostname.
  707. This may be <code>NULL</code> to disable the use of a proxy
  708. server.
  709. \hideinitializer*/
  710. #define OP_HTTP_PROXY_HOST(_host) \
  711. OP_URL_OPT(OP_HTTP_PROXY_HOST_REQUEST),OP_CHECK_CONST_CHAR_PTR(_host)
  712. /**Use the given port when proxying connections.
  713. This option only has an effect if #OP_HTTP_PROXY_HOST is specified with a
  714. non-<code>NULL</code> \a _host.
  715. If this option is not provided, the proxy port number defaults to 8080
  716. (http-alt).
  717. All proxy parameters are ignored for non-http and non-https URLs.
  718. \param _port <code>opus_int32</code>: The proxy server port.
  719. This must be in the range 0...65535 (inclusive), or the
  720. URL function this is passed to will fail.
  721. \hideinitializer*/
  722. #define OP_HTTP_PROXY_PORT(_port) \
  723. OP_URL_OPT(OP_HTTP_PROXY_PORT_REQUEST),OP_CHECK_INT(_port)
  724. /**Use the given user name for authentication when proxying connections.
  725. All proxy parameters are ignored for non-http and non-https URLs.
  726. \param _user const char *: The proxy server user name.
  727. This may be <code>NULL</code> to disable proxy
  728. authentication.
  729. A non-<code>NULL</code> value only has an effect
  730. if #OP_HTTP_PROXY_HOST and #OP_HTTP_PROXY_PASS
  731. are also specified with non-<code>NULL</code>
  732. arguments.
  733. \hideinitializer*/
  734. #define OP_HTTP_PROXY_USER(_user) \
  735. OP_URL_OPT(OP_HTTP_PROXY_USER_REQUEST),OP_CHECK_CONST_CHAR_PTR(_user)
  736. /**Use the given password for authentication when proxying connections.
  737. All proxy parameters are ignored for non-http and non-https URLs.
  738. \param _pass const char *: The proxy server password.
  739. This may be <code>NULL</code> to disable proxy
  740. authentication.
  741. A non-<code>NULL</code> value only has an effect
  742. if #OP_HTTP_PROXY_HOST and #OP_HTTP_PROXY_USER
  743. are also specified with non-<code>NULL</code>
  744. arguments.
  745. \hideinitializer*/
  746. #define OP_HTTP_PROXY_PASS(_pass) \
  747. OP_URL_OPT(OP_HTTP_PROXY_PASS_REQUEST),OP_CHECK_CONST_CHAR_PTR(_pass)
  748. /**Parse information about the streaming server (if any) and return it.
  749. Very little validation is done.
  750. In particular, OpusServerInfo::url may not be a valid URL,
  751. OpusServerInfo::bitrate_kbps may not really be in kbps, and
  752. OpusServerInfo::content_type may not be a valid MIME type.
  753. The character set of the string fields is not specified anywhere, and should
  754. not be assumed to be valid UTF-8.
  755. \param _info OpusServerInfo *: Returns information about the server.
  756. If there is any error opening the stream, the
  757. contents of this structure remain
  758. unmodified.
  759. On success, fills in the structure with the
  760. server information that was available, if
  761. any.
  762. After a successful return, the contents of
  763. this structure should be freed by calling
  764. opus_server_info_clear().
  765. \hideinitializer*/
  766. #define OP_GET_SERVER_INFO(_info) \
  767. OP_URL_OPT(OP_GET_SERVER_INFO_REQUEST),OP_CHECK_SERVER_INFO_PTR(_info)
  768. /*@}*/
  769. /*@}*/
  770. /**\defgroup stream_callbacks Abstract Stream Reading Interface*/
  771. /*@{*/
  772. /**\name Functions for reading from streams
  773. These functions define the interface used to read from and seek in a stream
  774. of data.
  775. A stream does not need to implement seeking, but the decoder will not be
  776. able to seek if it does not do so.
  777. These functions also include some convenience routines for working with
  778. standard <code>FILE</code> pointers, complete streams stored in a single
  779. block of memory, or URLs.*/
  780. /*@{*/
  781. /**Reads up to \a _nbytes bytes of data from \a _stream.
  782. \param _stream The stream to read from.
  783. \param[out] _ptr The buffer to store the data in.
  784. \param _nbytes The maximum number of bytes to read.
  785. This function may return fewer, though it will not
  786. return zero unless it reaches end-of-file.
  787. \return The number of bytes successfully read, or a negative value on
  788. error.*/
  789. typedef int (*op_read_func)(void *_stream,unsigned char *_ptr,int _nbytes);
  790. /**Sets the position indicator for \a _stream.
  791. The new position, measured in bytes, is obtained by adding \a _offset
  792. bytes to the position specified by \a _whence.
  793. If \a _whence is set to <code>SEEK_SET</code>, <code>SEEK_CUR</code>, or
  794. <code>SEEK_END</code>, the offset is relative to the start of the stream,
  795. the current position indicator, or end-of-file, respectively.
  796. \retval 0 Success.
  797. \retval -1 Seeking is not supported or an error occurred.
  798. <code>errno</code> need not be set.*/
  799. typedef int (*op_seek_func)(void *_stream,opus_int64 _offset,int _whence);
  800. /**Obtains the current value of the position indicator for \a _stream.
  801. \return The current position indicator.*/
  802. typedef opus_int64 (*op_tell_func)(void *_stream);
  803. /**Closes the underlying stream.
  804. \retval 0 Success.
  805. \retval EOF An error occurred.
  806. <code>errno</code> need not be set.*/
  807. typedef int (*op_close_func)(void *_stream);
  808. /**The callbacks used to access non-<code>FILE</code> stream resources.
  809. The function prototypes are basically the same as for the stdio functions
  810. <code>fread()</code>, <code>fseek()</code>, <code>ftell()</code>, and
  811. <code>fclose()</code>.
  812. The differences are that the <code>FILE *</code> arguments have been
  813. replaced with a <code>void *</code>, which is to be used as a pointer to
  814. whatever internal data these functions might need, that #seek and #tell
  815. take and return 64-bit offsets, and that #seek <em>must</em> return -1 if
  816. the stream is unseekable.*/
  817. struct OpusFileCallbacks{
  818. /**Used to read data from the stream.
  819. This must not be <code>NULL</code>.*/
  820. op_read_func read;
  821. /**Used to seek in the stream.
  822. This may be <code>NULL</code> if seeking is not implemented.*/
  823. op_seek_func seek;
  824. /**Used to return the current read position in the stream.
  825. This may be <code>NULL</code> if seeking is not implemented.*/
  826. op_tell_func tell;
  827. /**Used to close the stream when the decoder is freed.
  828. This may be <code>NULL</code> to leave the stream open.*/
  829. op_close_func close;
  830. };
  831. /**Opens a stream with <code>fopen()</code> and fills in a set of callbacks
  832. that can be used to access it.
  833. This is useful to avoid writing your own portable 64-bit seeking wrappers,
  834. and also avoids cross-module linking issues on Windows, where a
  835. <code>FILE *</code> must be accessed by routines defined in the same module
  836. that opened it.
  837. \param[out] _cb The callbacks to use for this file.
  838. If there is an error opening the file, nothing will be
  839. filled in here.
  840. \param _path The path to the file to open.
  841. On Windows, this string must be UTF-8 (to allow access to
  842. files whose names cannot be represented in the current
  843. MBCS code page).
  844. All other systems use the native character encoding.
  845. \param _mode The mode to open the file in.
  846. \return A stream handle to use with the callbacks, or <code>NULL</code> on
  847. error.*/
  848. OP_WARN_UNUSED_RESULT void *op_fopen(OpusFileCallbacks *_cb,
  849. const char *_path,const char *_mode) OP_ARG_NONNULL(1) OP_ARG_NONNULL(2)
  850. OP_ARG_NONNULL(3);
  851. /**Opens a stream with <code>fdopen()</code> and fills in a set of callbacks
  852. that can be used to access it.
  853. This is useful to avoid writing your own portable 64-bit seeking wrappers,
  854. and also avoids cross-module linking issues on Windows, where a
  855. <code>FILE *</code> must be accessed by routines defined in the same module
  856. that opened it.
  857. \param[out] _cb The callbacks to use for this file.
  858. If there is an error opening the file, nothing will be
  859. filled in here.
  860. \param _fd The file descriptor to open.
  861. \param _mode The mode to open the file in.
  862. \return A stream handle to use with the callbacks, or <code>NULL</code> on
  863. error.*/
  864. OP_WARN_UNUSED_RESULT void *op_fdopen(OpusFileCallbacks *_cb,
  865. int _fd,const char *_mode) OP_ARG_NONNULL(1) OP_ARG_NONNULL(3);
  866. /**Opens a stream with <code>freopen()</code> and fills in a set of callbacks
  867. that can be used to access it.
  868. This is useful to avoid writing your own portable 64-bit seeking wrappers,
  869. and also avoids cross-module linking issues on Windows, where a
  870. <code>FILE *</code> must be accessed by routines defined in the same module
  871. that opened it.
  872. \param[out] _cb The callbacks to use for this file.
  873. If there is an error opening the file, nothing will be
  874. filled in here.
  875. \param _path The path to the file to open.
  876. On Windows, this string must be UTF-8 (to allow access
  877. to files whose names cannot be represented in the
  878. current MBCS code page).
  879. All other systems use the native character encoding.
  880. \param _mode The mode to open the file in.
  881. \param _stream A stream previously returned by op_fopen(), op_fdopen(),
  882. or op_freopen().
  883. \return A stream handle to use with the callbacks, or <code>NULL</code> on
  884. error.*/
  885. OP_WARN_UNUSED_RESULT void *op_freopen(OpusFileCallbacks *_cb,
  886. const char *_path,const char *_mode,void *_stream) OP_ARG_NONNULL(1)
  887. OP_ARG_NONNULL(2) OP_ARG_NONNULL(3) OP_ARG_NONNULL(4);
  888. /**Creates a stream that reads from the given block of memory.
  889. This block of memory must contain the complete stream to decode.
  890. This is useful for caching small streams (e.g., sound effects) in RAM.
  891. \param[out] _cb The callbacks to use for this stream.
  892. If there is an error creating the stream, nothing will be
  893. filled in here.
  894. \param _data The block of memory to read from.
  895. \param _size The size of the block of memory.
  896. \return A stream handle to use with the callbacks, or <code>NULL</code> on
  897. error.*/
  898. OP_WARN_UNUSED_RESULT void *op_mem_stream_create(OpusFileCallbacks *_cb,
  899. const unsigned char *_data,size_t _size) OP_ARG_NONNULL(1);
  900. /**Creates a stream that reads from the given URL.
  901. This function behaves identically to op_url_stream_create(), except that it
  902. takes a va_list instead of a variable number of arguments.
  903. It does not call the <code>va_end</code> macro, and because it invokes the
  904. <code>va_arg</code> macro, the value of \a _ap is undefined after the call.
  905. \note If you use this function, you must link against <tt>libopusurl</tt>.
  906. \param[out] _cb The callbacks to use for this stream.
  907. If there is an error creating the stream, nothing will
  908. be filled in here.
  909. \param _url The URL to read from.
  910. Currently only the <file:>, <http:>, and <https:>
  911. schemes are supported.
  912. Both <http:> and <https:> may be disabled at compile
  913. time, in which case opening such URLs will always fail.
  914. Currently this only supports URIs.
  915. IRIs should be converted to UTF-8 and URL-escaped, with
  916. internationalized domain names encoded in punycode,
  917. before passing them to this function.
  918. \param[in,out] _ap A list of the \ref url_options "optional flags" to use.
  919. This is a variable-length list of options terminated
  920. with <code>NULL</code>.
  921. \return A stream handle to use with the callbacks, or <code>NULL</code> on
  922. error.*/
  923. OP_WARN_UNUSED_RESULT void *op_url_stream_vcreate(OpusFileCallbacks *_cb,
  924. const char *_url,va_list _ap) OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
  925. /**Creates a stream that reads from the given URL.
  926. \note If you use this function, you must link against <tt>libopusurl</tt>.
  927. \param[out] _cb The callbacks to use for this stream.
  928. If there is an error creating the stream, nothing will be
  929. filled in here.
  930. \param _url The URL to read from.
  931. Currently only the <file:>, <http:>, and <https:> schemes
  932. are supported.
  933. Both <http:> and <https:> may be disabled at compile time,
  934. in which case opening such URLs will always fail.
  935. Currently this only supports URIs.
  936. IRIs should be converted to UTF-8 and URL-escaped, with
  937. internationalized domain names encoded in punycode, before
  938. passing them to this function.
  939. \param ... The \ref url_options "optional flags" to use.
  940. This is a variable-length list of options terminated with
  941. <code>NULL</code>.
  942. \return A stream handle to use with the callbacks, or <code>NULL</code> on
  943. error.*/
  944. OP_WARN_UNUSED_RESULT void *op_url_stream_create(OpusFileCallbacks *_cb,
  945. const char *_url,...) OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
  946. /*@}*/
  947. /*@}*/
  948. /**\defgroup stream_open_close Opening and Closing*/
  949. /*@{*/
  950. /**\name Functions for opening and closing streams
  951. These functions allow you to test a stream to see if it is Opus, open it,
  952. and close it.
  953. Several flavors are provided for each of the built-in stream types, plus a
  954. more general version which takes a set of application-provided callbacks.*/
  955. /*@{*/
  956. /**Test to see if this is an Opus stream.
  957. For good results, you will need at least 57 bytes (for a pure Opus-only
  958. stream).
  959. Something like 512 bytes will give more reliable results for multiplexed
  960. streams.
  961. This function is meant to be a quick-rejection filter.
  962. Its purpose is not to guarantee that a stream is a valid Opus stream, but to
  963. ensure that it looks enough like Opus that it isn't going to be recognized
  964. as some other format (except possibly an Opus stream that is also
  965. multiplexed with other codecs, such as video).
  966. \param[out] _head The parsed ID header contents.
  967. You may pass <code>NULL</code> if you do not need
  968. this information.
  969. If the function fails, the contents of this structure
  970. remain untouched.
  971. \param _initial_data An initial buffer of data from the start of the
  972. stream.
  973. \param _initial_bytes The number of bytes in \a _initial_data.
  974. \return 0 if the data appears to be Opus, or a negative value on error.
  975. \retval #OP_FALSE There was not enough data to tell if this was an Opus
  976. stream or not.
  977. \retval #OP_EFAULT An internal memory allocation failed.
  978. \retval #OP_EIMPL The stream used a feature that is not implemented,
  979. such as an unsupported channel family.
  980. \retval #OP_ENOTFORMAT If the data did not contain a recognizable ID
  981. header for an Opus stream.
  982. \retval #OP_EVERSION If the version field signaled a version this library
  983. does not know how to parse.
  984. \retval #OP_EBADHEADER The ID header was not properly formatted or contained
  985. illegal values.*/
  986. int op_test(OpusHead *_head,
  987. const unsigned char *_initial_data,size_t _initial_bytes);
  988. /**Open a stream from the given file path.
  989. \param _path The path to the file to open.
  990. \param[out] _error Returns 0 on success, or a failure code on error.
  991. You may pass in <code>NULL</code> if you don't want the
  992. failure code.
  993. The failure code will be #OP_EFAULT if the file could not
  994. be opened, or one of the other failure codes from
  995. op_open_callbacks() otherwise.
  996. \return A freshly opened \c OggOpusFile, or <code>NULL</code> on error.*/
  997. OP_WARN_UNUSED_RESULT OggOpusFile *op_open_file(const char *_path,int *_error)
  998. OP_ARG_NONNULL(1);
  999. /**Open a stream from a memory buffer.
  1000. \param _data The memory buffer to open.
  1001. \param _size The number of bytes in the buffer.
  1002. \param[out] _error Returns 0 on success, or a failure code on error.
  1003. You may pass in <code>NULL</code> if you don't want the
  1004. failure code.
  1005. See op_open_callbacks() for a full list of failure codes.
  1006. \return A freshly opened \c OggOpusFile, or <code>NULL</code> on error.*/
  1007. OP_WARN_UNUSED_RESULT OggOpusFile *op_open_memory(const unsigned char *_data,
  1008. size_t _size,int *_error);
  1009. /**Open a stream from a URL.
  1010. This function behaves identically to op_open_url(), except that it
  1011. takes a va_list instead of a variable number of arguments.
  1012. It does not call the <code>va_end</code> macro, and because it invokes the
  1013. <code>va_arg</code> macro, the value of \a _ap is undefined after the call.
  1014. \note If you use this function, you must link against <tt>libopusurl</tt>.
  1015. \param _url The URL to open.
  1016. Currently only the <file:>, <http:>, and <https:>
  1017. schemes are supported.
  1018. Both <http:> and <https:> may be disabled at compile
  1019. time, in which case opening such URLs will always
  1020. fail.
  1021. Currently this only supports URIs.
  1022. IRIs should be converted to UTF-8 and URL-escaped,
  1023. with internationalized domain names encoded in
  1024. punycode, before passing them to this function.
  1025. \param[out] _error Returns 0 on success, or a failure code on error.
  1026. You may pass in <code>NULL</code> if you don't want
  1027. the failure code.
  1028. See op_open_callbacks() for a full list of failure
  1029. codes.
  1030. \param[in,out] _ap A list of the \ref url_options "optional flags" to
  1031. use.
  1032. This is a variable-length list of options terminated
  1033. with <code>NULL</code>.
  1034. \return A freshly opened \c OggOpusFile, or <code>NULL</code> on error.*/
  1035. OP_WARN_UNUSED_RESULT OggOpusFile *op_vopen_url(const char *_url,
  1036. int *_error,va_list _ap) OP_ARG_NONNULL(1);
  1037. /**Open a stream from a URL.
  1038. \note If you use this function, you must link against <tt>libopusurl</tt>.
  1039. \param _url The URL to open.
  1040. Currently only the <file:>, <http:>, and <https:> schemes
  1041. are supported.
  1042. Both <http:> and <https:> may be disabled at compile
  1043. time, in which case opening such URLs will always fail.
  1044. Currently this only supports URIs.
  1045. IRIs should be converted to UTF-8 and URL-escaped, with
  1046. internationalized domain names encoded in punycode,
  1047. before passing them to this function.
  1048. \param[out] _error Returns 0 on success, or a failure code on error.
  1049. You may pass in <code>NULL</code> if you don't want the
  1050. failure code.
  1051. See op_open_callbacks() for a full list of failure codes.
  1052. \param ... The \ref url_options "optional flags" to use.
  1053. This is a variable-length list of options terminated with
  1054. <code>NULL</code>.
  1055. \return A freshly opened \c OggOpusFile, or <code>NULL</code> on error.*/
  1056. OP_WARN_UNUSED_RESULT OggOpusFile *op_open_url(const char *_url,
  1057. int *_error,...) OP_ARG_NONNULL(1);
  1058. /**Open a stream using the given set of callbacks to access it.
  1059. \param _source The stream to read from (e.g., a <code>FILE *</code>).
  1060. \param _cb The callbacks with which to access the stream.
  1061. <code><a href="#op_read_func">read()</a></code> must
  1062. be implemented.
  1063. <code><a href="#op_seek_func">seek()</a></code> and
  1064. <code><a href="#op_tell_func">tell()</a></code> may
  1065. be <code>NULL</code>, or may always return -1 to
  1066. indicate a source is unseekable, but if
  1067. <code><a href="#op_seek_func">seek()</a></code> is
  1068. implemented and succeeds on a particular source, then
  1069. <code><a href="#op_tell_func">tell()</a></code> must
  1070. also.
  1071. <code><a href="#op_close_func">close()</a></code> may
  1072. be <code>NULL</code>, but if it is not, it will be
  1073. called when the \c OggOpusFile is destroyed by
  1074. op_free().
  1075. It will not be called if op_open_callbacks() fails
  1076. with an error.
  1077. \param _initial_data An initial buffer of data from the start of the
  1078. stream.
  1079. Applications can read some number of bytes from the
  1080. start of the stream to help identify this as an Opus
  1081. stream, and then provide them here to allow the
  1082. stream to be opened, even if it is unseekable.
  1083. \param _initial_bytes The number of bytes in \a _initial_data.
  1084. If the stream is seekable, its current position (as
  1085. reported by
  1086. <code><a href="#opus_tell_func">tell()</a></code>
  1087. at the start of this function) must be equal to
  1088. \a _initial_bytes.
  1089. Otherwise, seeking to absolute positions will
  1090. generate inconsistent results.
  1091. \param[out] _error Returns 0 on success, or a failure code on error.
  1092. You may pass in <code>NULL</code> if you don't want
  1093. the failure code.
  1094. The failure code will be one of
  1095. <dl>
  1096. <dt>#OP_EREAD</dt>
  1097. <dd>An underlying read, seek, or tell operation
  1098. failed when it should have succeeded, or we failed
  1099. to find data in the stream we had seen before.</dd>
  1100. <dt>#OP_EFAULT</dt>
  1101. <dd>There was a memory allocation failure, or an
  1102. internal library error.</dd>
  1103. <dt>#OP_EIMPL</dt>
  1104. <dd>The stream used a feature that is not
  1105. implemented, such as an unsupported channel
  1106. family.</dd>
  1107. <dt>#OP_EINVAL</dt>
  1108. <dd><code><a href="#op_seek_func">seek()</a></code>
  1109. was implemented and succeeded on this source, but
  1110. <code><a href="#op_tell_func">tell()</a></code>
  1111. did not, or the starting position indicator was
  1112. not equal to \a _initial_bytes.</dd>
  1113. <dt>#OP_ENOTFORMAT</dt>
  1114. <dd>The stream contained a link that did not have
  1115. any logical Opus streams in it.</dd>
  1116. <dt>#OP_EBADHEADER</dt>
  1117. <dd>A required header packet was not properly
  1118. formatted, contained illegal values, or was missing
  1119. altogether.</dd>
  1120. <dt>#OP_EVERSION</dt>
  1121. <dd>An ID header contained an unrecognized version
  1122. number.</dd>
  1123. <dt>#OP_EBADLINK</dt>
  1124. <dd>We failed to find data we had seen before after
  1125. seeking.</dd>
  1126. <dt>#OP_EBADTIMESTAMP</dt>
  1127. <dd>The first or last timestamp in a link failed
  1128. basic validity checks.</dd>
  1129. </dl>
  1130. \return A freshly opened \c OggOpusFile, or <code>NULL</code> on error.
  1131. <tt>libopusfile</tt> does <em>not</em> take ownership of the source
  1132. if the call fails.
  1133. The calling application is responsible for closing the source if
  1134. this call returns an error.*/
  1135. OP_WARN_UNUSED_RESULT OggOpusFile *op_open_callbacks(void *_source,
  1136. const OpusFileCallbacks *_cb,const unsigned char *_initial_data,
  1137. size_t _initial_bytes,int *_error) OP_ARG_NONNULL(2);
  1138. /**Partially open a stream from the given file path.
  1139. \see op_test_callbacks
  1140. \param _path The path to the file to open.
  1141. \param[out] _error Returns 0 on success, or a failure code on error.
  1142. You may pass in <code>NULL</code> if you don't want the
  1143. failure code.
  1144. The failure code will be #OP_EFAULT if the file could not
  1145. be opened, or one of the other failure codes from
  1146. op_open_callbacks() otherwise.
  1147. \return A partially opened \c OggOpusFile, or <code>NULL</code> on error.*/
  1148. OP_WARN_UNUSED_RESULT OggOpusFile *op_test_file(const char *_path,int *_error)
  1149. OP_ARG_NONNULL(1);
  1150. /**Partially open a stream from a memory buffer.
  1151. \see op_test_callbacks
  1152. \param _data The memory buffer to open.
  1153. \param _size The number of bytes in the buffer.
  1154. \param[out] _error Returns 0 on success, or a failure code on error.
  1155. You may pass in <code>NULL</code> if you don't want the
  1156. failure code.
  1157. See op_open_callbacks() for a full list of failure codes.
  1158. \return A partially opened \c OggOpusFile, or <code>NULL</code> on error.*/
  1159. OP_WARN_UNUSED_RESULT OggOpusFile *op_test_memory(const unsigned char *_data,
  1160. size_t _size,int *_error);
  1161. /**Partially open a stream from a URL.
  1162. This function behaves identically to op_test_url(), except that it
  1163. takes a va_list instead of a variable number of arguments.
  1164. It does not call the <code>va_end</code> macro, and because it invokes the
  1165. <code>va_arg</code> macro, the value of \a _ap is undefined after the call.
  1166. \note If you use this function, you must link against <tt>libopusurl</tt>.
  1167. \see op_test_url
  1168. \see op_test_callbacks
  1169. \param _url The URL to open.
  1170. Currently only the <file:>, <http:>, and <https:>
  1171. schemes are supported.
  1172. Both <http:> and <https:> may be disabled at compile
  1173. time, in which case opening such URLs will always
  1174. fail.
  1175. Currently this only supports URIs.
  1176. IRIs should be converted to UTF-8 and URL-escaped,
  1177. with internationalized domain names encoded in
  1178. punycode, before passing them to this function.
  1179. \param[out] _error Returns 0 on success, or a failure code on error.
  1180. You may pass in <code>NULL</code> if you don't want
  1181. the failure code.
  1182. See op_open_callbacks() for a full list of failure
  1183. codes.
  1184. \param[in,out] _ap A list of the \ref url_options "optional flags" to
  1185. use.
  1186. This is a variable-length list of options terminated
  1187. with <code>NULL</code>.
  1188. \return A partially opened \c OggOpusFile, or <code>NULL</code> on error.*/
  1189. OP_WARN_UNUSED_RESULT OggOpusFile *op_vtest_url(const char *_url,
  1190. int *_error,va_list _ap) OP_ARG_NONNULL(1);
  1191. /**Partially open a stream from a URL.
  1192. \note If you use this function, you must link against <tt>libopusurl</tt>.
  1193. \see op_test_callbacks
  1194. \param _url The URL to open.
  1195. Currently only the <file:>, <http:>, and <https:>
  1196. schemes are supported.
  1197. Both <http:> and <https:> may be disabled at compile
  1198. time, in which case opening such URLs will always fail.
  1199. Currently this only supports URIs.
  1200. IRIs should be converted to UTF-8 and URL-escaped, with
  1201. internationalized domain names encoded in punycode,
  1202. before passing them to this function.
  1203. \param[out] _error Returns 0 on success, or a failure code on error.
  1204. You may pass in <code>NULL</code> if you don't want the
  1205. failure code.
  1206. See op_open_callbacks() for a full list of failure
  1207. codes.
  1208. \param ... The \ref url_options "optional flags" to use.
  1209. This is a variable-length list of options terminated
  1210. with <code>NULL</code>.
  1211. \return A partially opened \c OggOpusFile, or <code>NULL</code> on error.*/
  1212. OP_WARN_UNUSED_RESULT OggOpusFile *op_test_url(const char *_url,
  1213. int *_error,...) OP_ARG_NONNULL(1);
  1214. /**Partially open a stream using the given set of callbacks to access it.
  1215. This tests for Opusness and loads the headers for the first link.
  1216. It does not seek (although it tests for seekability).
  1217. You can query a partially open stream for the few pieces of basic
  1218. information returned by op_serialno(), op_channel_count(), op_head(), and
  1219. op_tags() (but only for the first link).
  1220. You may also determine if it is seekable via a call to op_seekable().
  1221. You cannot read audio from the stream, seek, get the size or duration,
  1222. get information from links other than the first one, or even get the total
  1223. number of links until you finish opening the stream with op_test_open().
  1224. If you do not need to do any of these things, you can dispose of it with
  1225. op_free() instead.
  1226. This function is provided mostly to simplify porting existing code that used
  1227. <tt>libvorbisfile</tt>.
  1228. For new code, you are likely better off using op_test() instead, which
  1229. is less resource-intensive, requires less data to succeed, and imposes a
  1230. hard limit on the amount of data it examines (important for unseekable
  1231. sources, where all such data must be buffered until you are sure of the
  1232. stream type).
  1233. \param _source The stream to read from (e.g., a <code>FILE *</code>).
  1234. \param _cb The callbacks with which to access the stream.
  1235. <code><a href="#op_read_func">read()</a></code> must
  1236. be implemented.
  1237. <code><a href="#op_seek_func">seek()</a></code> and
  1238. <code><a href="#op_tell_func">tell()</a></code> may
  1239. be <code>NULL</code>, or may always return -1 to
  1240. indicate a source is unseekable, but if
  1241. <code><a href="#op_seek_func">seek()</a></code> is
  1242. implemented and succeeds on a particular source, then
  1243. <code><a href="#op_tell_func">tell()</a></code> must
  1244. also.
  1245. <code><a href="#op_close_func">close()</a></code> may
  1246. be <code>NULL</code>, but if it is not, it will be
  1247. called when the \c OggOpusFile is destroyed by
  1248. op_free().
  1249. It will not be called if op_open_callbacks() fails
  1250. with an error.
  1251. \param _initial_data An initial buffer of data from the start of the
  1252. stream.
  1253. Applications can read some number of bytes from the
  1254. start of the stream to help identify this as an Opus
  1255. stream, and then provide them here to allow the
  1256. stream to be tested more thoroughly, even if it is
  1257. unseekable.
  1258. \param _initial_bytes The number of bytes in \a _initial_data.
  1259. If the stream is seekable, its current position (as
  1260. reported by
  1261. <code><a href="#opus_tell_func">tell()</a></code>
  1262. at the start of this function) must be equal to
  1263. \a _initial_bytes.
  1264. Otherwise, seeking to absolute positions will
  1265. generate inconsistent results.
  1266. \param[out] _error Returns 0 on success, or a failure code on error.
  1267. You may pass in <code>NULL</code> if you don't want
  1268. the failure code.
  1269. See op_open_callbacks() for a full list of failure
  1270. codes.
  1271. \return A partially opened \c OggOpusFile, or <code>NULL</code> on error.
  1272. <tt>libopusfile</tt> does <em>not</em> take ownership of the source
  1273. if the call fails.
  1274. The calling application is responsible for closing the source if
  1275. this call returns an error.*/
  1276. OP_WARN_UNUSED_RESULT OggOpusFile *op_test_callbacks(void *_source,
  1277. const OpusFileCallbacks *_cb,const unsigned char *_initial_data,
  1278. size_t _initial_bytes,int *_error) OP_ARG_NONNULL(2);
  1279. /**Finish opening a stream partially opened with op_test_callbacks() or one of
  1280. the associated convenience functions.
  1281. If this function fails, you are still responsible for freeing the
  1282. \c OggOpusFile with op_free().
  1283. \param _of The \c OggOpusFile to finish opening.
  1284. \return 0 on success, or a negative value on error.
  1285. \retval #OP_EREAD An underlying read, seek, or tell operation failed
  1286. when it should have succeeded.
  1287. \retval #OP_EFAULT There was a memory allocation failure, or an
  1288. internal library error.
  1289. \retval #OP_EIMPL The stream used a feature that is not implemented,
  1290. such as an unsupported channel family.
  1291. \retval #OP_EINVAL The stream was not partially opened with
  1292. op_test_callbacks() or one of the associated
  1293. convenience functions.
  1294. \retval #OP_ENOTFORMAT The stream contained a link that did not have any
  1295. logical Opus streams in it.
  1296. \retval #OP_EBADHEADER A required header packet was not properly
  1297. formatted, contained illegal values, or was
  1298. missing altogether.
  1299. \retval #OP_EVERSION An ID header contained an unrecognized version
  1300. number.
  1301. \retval #OP_EBADLINK We failed to find data we had seen before after
  1302. seeking.
  1303. \retval #OP_EBADTIMESTAMP The first or last timestamp in a link failed basic
  1304. validity checks.*/
  1305. int op_test_open(OggOpusFile *_of) OP_ARG_NONNULL(1);
  1306. /**Release all memory used by an \c OggOpusFile.
  1307. \param _of The \c OggOpusFile to free.*/
  1308. void op_free(OggOpusFile *_of);
  1309. /*@}*/
  1310. /*@}*/
  1311. /**\defgroup stream_info Stream Information*/
  1312. /*@{*/
  1313. /**\name Functions for obtaining information about streams
  1314. These functions allow you to get basic information about a stream, including
  1315. seekability, the number of links (for chained streams), plus the size,
  1316. duration, bitrate, header parameters, and meta information for each link
  1317. (or, where available, the stream as a whole).
  1318. Some of these (size, duration) are only available for seekable streams.
  1319. You can also query the current stream position, link, and playback time,
  1320. and instantaneous bitrate during playback.
  1321. Some of these functions may be used successfully on the partially open
  1322. streams returned by op_test_callbacks() or one of the associated
  1323. convenience functions.
  1324. Their documention will indicate so explicitly.*/
  1325. /*@{*/
  1326. /**Returns whether or not the data source being read is seekable.
  1327. This is true if
  1328. <ol>
  1329. <li>The <code><a href="#op_seek_func">seek()</a></code> and
  1330. <code><a href="#op_tell_func">tell()</a></code> callbacks are both
  1331. non-<code>NULL</code>,</li>
  1332. <li>The <code><a href="#op_seek_func">seek()</a></code> callback was
  1333. successfully executed at least once, and</li>
  1334. <li>The <code><a href="#op_tell_func">tell()</a></code> callback was
  1335. successfully able to report the position indicator afterwards.</li>
  1336. </ol>
  1337. This function may be called on partially-opened streams.
  1338. \param _of The \c OggOpusFile whose seekable status is to be returned.
  1339. \return A non-zero value if seekable, and 0 if unseekable.*/
  1340. int op_seekable(const OggOpusFile *_of) OP_ARG_NONNULL(1);
  1341. /**Returns the number of links in this chained stream.
  1342. This function may be called on partially-opened streams, but it will always
  1343. return 1.
  1344. The actual number of links is not known until the stream is fully opened.
  1345. \param _of The \c OggOpusFile from which to retrieve the link count.
  1346. \return For fully-open seekable sources, this returns the total number of
  1347. links in the whole stream, which will be at least 1.
  1348. For partially-open or unseekable sources, this always returns 1.*/
  1349. int op_link_count(const OggOpusFile *_of) OP_ARG_NONNULL(1);
  1350. /**Get the serial number of the given link in a (possibly-chained) Ogg Opus
  1351. stream.
  1352. This function may be called on partially-opened streams, but it will always
  1353. return the serial number of the Opus stream in the first link.
  1354. \param _of The \c OggOpusFile from which to retrieve the serial number.
  1355. \param _li The index of the link whose serial number should be retrieved.
  1356. Use a negative number to get the serial number of the current
  1357. link.
  1358. \return The serial number of the given link.
  1359. If \a _li is greater than the total number of links, this returns
  1360. the serial number of the last link.
  1361. If the source is not seekable, this always returns the serial number
  1362. of the current link.*/
  1363. opus_uint32 op_serialno(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
  1364. /**Get the channel count of the given link in a (possibly-chained) Ogg Opus
  1365. stream.
  1366. This is equivalent to <code>op_head(_of,_li)->channel_count</code>, but
  1367. is provided for convenience.
  1368. This function may be called on partially-opened streams, but it will always
  1369. return the channel count of the Opus stream in the first link.
  1370. \param _of The \c OggOpusFile from which to retrieve the channel count.
  1371. \param _li The index of the link whose channel count should be retrieved.
  1372. Use a negative number to get the channel count of the current
  1373. link.
  1374. \return The channel count of the given link.
  1375. If \a _li is greater than the total number of links, this returns
  1376. the channel count of the last link.
  1377. If the source is not seekable, this always returns the channel count
  1378. of the current link.*/
  1379. int op_channel_count(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
  1380. /**Get the total (compressed) size of the stream, or of an individual link in
  1381. a (possibly-chained) Ogg Opus stream, including all headers and Ogg muxing
  1382. overhead.
  1383. \warning If the Opus stream (or link) is concurrently multiplexed with other
  1384. logical streams (e.g., video), this returns the size of the entire stream
  1385. (or link), not just the number of bytes in the first logical Opus stream.
  1386. Returning the latter would require scanning the entire file.
  1387. \param _of The \c OggOpusFile from which to retrieve the compressed size.
  1388. \param _li The index of the link whose compressed size should be computed.
  1389. Use a negative number to get the compressed size of the entire
  1390. stream.
  1391. \return The compressed size of the entire stream if \a _li is negative, the
  1392. compressed size of link \a _li if it is non-negative, or a negative
  1393. value on error.
  1394. The compressed size of the entire stream may be smaller than that
  1395. of the underlying source if trailing garbage was detected in the
  1396. file.
  1397. \retval #OP_EINVAL The source is not seekable (so we can't know the length),
  1398. \a _li wasn't less than the total number of links in
  1399. the stream, or the stream was only partially open.*/
  1400. opus_int64 op_raw_total(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
  1401. /**Get the total PCM length (number of samples at 48 kHz) of the stream, or of
  1402. an individual link in a (possibly-chained) Ogg Opus stream.
  1403. Users looking for <code>op_time_total()</code> should use op_pcm_total()
  1404. instead.
  1405. Because timestamps in Opus are fixed at 48 kHz, there is no need for a
  1406. separate function to convert this to seconds (and leaving it out avoids
  1407. introducing floating point to the API, for those that wish to avoid it).
  1408. \param _of The \c OggOpusFile from which to retrieve the PCM offset.
  1409. \param _li The index of the link whose PCM length should be computed.
  1410. Use a negative number to get the PCM length of the entire stream.
  1411. \return The PCM length of the entire stream if \a _li is negative, the PCM
  1412. length of link \a _li if it is non-negative, or a negative value on
  1413. error.
  1414. \retval #OP_EINVAL The source is not seekable (so we can't know the length),
  1415. \a _li wasn't less than the total number of links in
  1416. the stream, or the stream was only partially open.*/
  1417. ogg_int64_t op_pcm_total(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
  1418. /**Get the ID header information for the given link in a (possibly chained) Ogg
  1419. Opus stream.
  1420. This function may be called on partially-opened streams, but it will always
  1421. return the ID header information of the Opus stream in the first link.
  1422. \param _of The \c OggOpusFile from which to retrieve the ID header
  1423. information.
  1424. \param _li The index of the link whose ID header information should be
  1425. retrieved.
  1426. Use a negative number to get the ID header information of the
  1427. current link.
  1428. For an unseekable stream, \a _li is ignored, and the ID header
  1429. information for the current link is always returned, if
  1430. available.
  1431. \return The contents of the ID header for the given link.*/
  1432. const OpusHead *op_head(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
  1433. /**Get the comment header information for the given link in a (possibly
  1434. chained) Ogg Opus stream.
  1435. This function may be called on partially-opened streams, but it will always
  1436. return the tags from the Opus stream in the first link.
  1437. \param _of The \c OggOpusFile from which to retrieve the comment header
  1438. information.
  1439. \param _li The index of the link whose comment header information should be
  1440. retrieved.
  1441. Use a negative number to get the comment header information of
  1442. the current link.
  1443. For an unseekable stream, \a _li is ignored, and the comment
  1444. header information for the current link is always returned, if
  1445. available.
  1446. \return The contents of the comment header for the given link, or
  1447. <code>NULL</code> if this is an unseekable stream that encountered
  1448. an invalid link.*/
  1449. const OpusTags *op_tags(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
  1450. /**Retrieve the index of the current link.
  1451. This is the link that produced the data most recently read by
  1452. op_read_float() or its associated functions, or, after a seek, the link
  1453. that the seek target landed in.
  1454. Reading more data may advance the link index (even on the first read after a
  1455. seek).
  1456. \param _of The \c OggOpusFile from which to retrieve the current link index.
  1457. \return The index of the current link on success, or a negative value on
  1458. failure.
  1459. For seekable streams, this is a number between 0 and the value
  1460. returned by op_link_count().
  1461. For unseekable streams, this value starts at 0 and increments by one
  1462. each time a new link is encountered (even though op_link_count()
  1463. always returns 1).
  1464. \retval #OP_EINVAL The stream was only partially open.*/
  1465. int op_current_link(const OggOpusFile *_of) OP_ARG_NONNULL(1);
  1466. /**Computes the bitrate of the stream, or of an individual link in a
  1467. (possibly-chained) Ogg Opus stream.
  1468. The stream must be seekable to compute the bitrate.
  1469. For unseekable streams, use op_bitrate_instant() to get periodic estimates.
  1470. \warning If the Opus stream (or link) is concurrently multiplexed with other
  1471. logical streams (e.g., video), this uses the size of the entire stream (or
  1472. link) to compute the bitrate, not just the number of bytes in the first
  1473. logical Opus stream.
  1474. Returning the latter requires scanning the entire file, but this may be done
  1475. by decoding the whole file and calling op_bitrate_instant() once at the
  1476. end.
  1477. Install a trivial decoding callback with op_set_decode_callback() if you
  1478. wish to skip actual decoding during this process.
  1479. \param _of The \c OggOpusFile from which to retrieve the bitrate.
  1480. \param _li The index of the link whose bitrate should be computed.
  1481. Use a negative number to get the bitrate of the whole stream.
  1482. \return The bitrate on success, or a negative value on error.
  1483. \retval #OP_EINVAL The stream was only partially open, the stream was not
  1484. seekable, or \a _li was larger than the number of
  1485. links.*/
  1486. opus_int32 op_bitrate(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
  1487. /**Compute the instantaneous bitrate, measured as the ratio of bits to playable
  1488. samples decoded since a) the last call to op_bitrate_instant(), b) the last
  1489. seek, or c) the start of playback, whichever was most recent.
  1490. This will spike somewhat after a seek or at the start/end of a chain
  1491. boundary, as pre-skip, pre-roll, and end-trimming causes samples to be
  1492. decoded but not played.
  1493. \param _of The \c OggOpusFile from which to retrieve the bitrate.
  1494. \return The bitrate, in bits per second, or a negative value on error.
  1495. \retval #OP_FALSE No data has been decoded since any of the events
  1496. described above.
  1497. \retval #OP_EINVAL The stream was only partially open.*/
  1498. opus_int32 op_bitrate_instant(OggOpusFile *_of) OP_ARG_NONNULL(1);
  1499. /**Obtain the current value of the position indicator for \a _of.
  1500. \param _of The \c OggOpusFile from which to retrieve the position indicator.
  1501. \return The byte position that is currently being read from.
  1502. \retval #OP_EINVAL The stream was only partially open.*/
  1503. opus_int64 op_raw_tell(const OggOpusFile *_of) OP_ARG_NONNULL(1);
  1504. /**Obtain the PCM offset of the next sample to be read.
  1505. If the stream is not properly timestamped, this might not increment by the
  1506. proper amount between reads, or even return monotonically increasing
  1507. values.
  1508. \param _of The \c OggOpusFile from which to retrieve the PCM offset.
  1509. \return The PCM offset of the next sample to be read.
  1510. \retval #OP_EINVAL The stream was only partially open.*/
  1511. ogg_int64_t op_pcm_tell(const OggOpusFile *_of) OP_ARG_NONNULL(1);
  1512. /*@}*/
  1513. /*@}*/
  1514. /**\defgroup stream_seeking Seeking*/
  1515. /*@{*/
  1516. /**\name Functions for seeking in Opus streams
  1517. These functions let you seek in Opus streams, if the underlying source
  1518. support it.
  1519. Seeking is implemented for all built-in stream I/O routines, though some
  1520. individual sources may not be seekable (pipes, live HTTP streams, or HTTP
  1521. streams from a server that does not support <code>Range</code> requests).
  1522. op_raw_seek() is the fastest: it is guaranteed to perform at most one
  1523. physical seek, but, since the target is a byte position, makes no guarantee
  1524. how close to a given time it will come.
  1525. op_pcm_seek() provides sample-accurate seeking.
  1526. The number of physical seeks it requires is still quite small (often 1 or
  1527. 2, even in highly variable bitrate streams).
  1528. Seeking in Opus requires decoding some pre-roll amount before playback to
  1529. allow the internal state to converge (as if recovering from packet loss).
  1530. This is handled internally by <tt>libopusfile</tt>, but means there is
  1531. little extra overhead for decoding up to the exact position requested
  1532. (since it must decode some amount of audio anyway).
  1533. It also means that decoding after seeking may not return exactly the same
  1534. values as would be obtained by decoding the stream straight through.
  1535. However, such differences are expected to be smaller than the loss
  1536. introduced by Opus's lossy compression.*/
  1537. /*@{*/
  1538. /**Seek to a byte offset relative to the <b>compressed</b> data.
  1539. This also scans packets to update the PCM cursor.
  1540. It will cross a logical bitstream boundary, but only if it can't get any
  1541. packets out of the tail of the link to which it seeks.
  1542. \param _of The \c OggOpusFile in which to seek.
  1543. \param _byte_offset The byte position to seek to.
  1544. \return 0 on success, or a negative error code on failure.
  1545. \retval #OP_EREAD The underlying seek operation failed.
  1546. \retval #OP_EINVAL The stream was only partially open, or the target was
  1547. outside the valid range for the stream.
  1548. \retval #OP_ENOSEEK This stream is not seekable.
  1549. \retval #OP_EBADLINK Failed to initialize a decoder for a stream for an
  1550. unknown reason.*/
  1551. int op_raw_seek(OggOpusFile *_of,opus_int64 _byte_offset) OP_ARG_NONNULL(1);
  1552. /**Seek to the specified PCM offset, such that decoding will begin at exactly
  1553. the requested position.
  1554. \param _of The \c OggOpusFile in which to seek.
  1555. \param _pcm_offset The PCM offset to seek to.
  1556. This is in samples at 48 kHz relative to the start of the
  1557. stream.
  1558. \return 0 on success, or a negative value on error.
  1559. \retval #OP_EREAD An underlying read or seek operation failed.
  1560. \retval #OP_EINVAL The stream was only partially open, or the target was
  1561. outside the valid range for the stream.
  1562. \retval #OP_ENOSEEK This stream is not seekable.
  1563. \retval #OP_EBADLINK We failed to find data we had seen before, or the
  1564. bitstream structure was sufficiently malformed that
  1565. seeking to the target destination was impossible.*/
  1566. int op_pcm_seek(OggOpusFile *_of,ogg_int64_t _pcm_offset) OP_ARG_NONNULL(1);
  1567. /*@}*/
  1568. /*@}*/
  1569. /**\defgroup stream_decoding Decoding*/
  1570. /*@{*/
  1571. /**\name Functions for decoding audio data
  1572. These functions retrieve actual decoded audio data from the stream.
  1573. The general functions, op_read() and op_read_float() return 16-bit or
  1574. floating-point output, both using native endian ordering.
  1575. The number of channels returned can change from link to link in a chained
  1576. stream.
  1577. There are special functions, op_read_stereo() and op_read_float_stereo(),
  1578. which always output two channels, to simplify applications which do not
  1579. wish to handle multichannel audio.
  1580. These downmix multichannel files to two channels, so they can always return
  1581. samples in the same format for every link in a chained file.
  1582. If the rest of your audio processing chain can handle floating point, those
  1583. routines should be preferred, as floating point output avoids introducing
  1584. clipping and other issues which might be avoided entirely if, e.g., you
  1585. scale down the volume at some other stage.
  1586. However, if you intend to direct consume 16-bit samples, the conversion in
  1587. <tt>libopusfile</tt> provides noise-shaping dithering and, if compiled
  1588. against <tt>libopus</tt>&nbsp;1.1 or later, soft-clipping prevention.
  1589. <tt>libopusfile</tt> can also be configured at compile time to use the
  1590. fixed-point <tt>libopus</tt> API.
  1591. If so, <tt>libopusfile</tt>'s floating-point API may also be disabled.
  1592. In that configuration, nothing in <tt>libopusfile</tt> will use any
  1593. floating-point operations, to simplify support on devices without an
  1594. adequate FPU.
  1595. \warning HTTPS streams may be be vulnerable to truncation attacks if you do
  1596. not check the error return code from op_read_float() or its associated
  1597. functions.
  1598. If the remote peer does not close the connection gracefully (with a TLS
  1599. "close notify" message), these functions will return #OP_EREAD instead of 0
  1600. when they reach the end of the file.
  1601. If you are reading from an <https:> URL (particularly if seeking is not
  1602. supported), you should make sure to check for this error and warn the user
  1603. appropriately.*/
  1604. /*@{*/
  1605. /**Indicates that the decoding callback should produce signed 16-bit
  1606. native-endian output samples.*/
  1607. #define OP_DEC_FORMAT_SHORT (7008)
  1608. /**Indicates that the decoding callback should produce 32-bit native-endian
  1609. float samples.*/
  1610. #define OP_DEC_FORMAT_FLOAT (7040)
  1611. /**Indicates that the decoding callback did not decode anything, and that
  1612. <tt>libopusfile</tt> should decode normally instead.*/
  1613. #define OP_DEC_USE_DEFAULT (6720)
  1614. /**Called to decode an Opus packet.
  1615. This should invoke the functional equivalent of opus_multistream_decode() or
  1616. opus_multistream_decode_float(), except that it returns 0 on success
  1617. instead of the number of decoded samples (which is known a priori).
  1618. \param _ctx The application-provided callback context.
  1619. \param _decoder The decoder to use to decode the packet.
  1620. \param[out] _pcm The buffer to decode into.
  1621. This will always have enough room for \a _nchannels of
  1622. \a _nsamples samples, which should be placed into this
  1623. buffer interleaved.
  1624. \param _op The packet to decode.
  1625. This will always have its granule position set to a valid
  1626. value.
  1627. \param _nsamples The number of samples expected from the packet.
  1628. \param _nchannels The number of channels expected from the packet.
  1629. \param _format The desired sample output format.
  1630. This is either #OP_DEC_FORMAT_SHORT or
  1631. #OP_DEC_FORMAT_FLOAT.
  1632. \param _li The index of the link from which this packet was decoded.
  1633. \return A non-negative value on success, or a negative value on error.
  1634. The error codes should be the same as those returned by
  1635. opus_multistream_decode() or opus_multistream_decode_float().
  1636. \retval 0 Decoding was successful.
  1637. The application has filled the buffer with
  1638. exactly <code>\a _nsamples*\a
  1639. _nchannels</code> samples in the requested
  1640. format.
  1641. \retval #OP_DEC_USE_DEFAULT No decoding was done.
  1642. <tt>libopusfile</tt> should decode normally
  1643. instead.*/
  1644. typedef int (*op_decode_cb_func)(void *_ctx,OpusMSDecoder *_decoder,void *_pcm,
  1645. const ogg_packet *_op,int _nsamples,int _nchannels,int _format,int _li);
  1646. /**Sets the packet decode callback function.
  1647. This is called once for each packet that needs to be decoded.
  1648. A call to this function is no guarantee that the audio will eventually be
  1649. delivered to the application.
  1650. Some or all of the data from the packet may be discarded (i.e., at the
  1651. beginning or end of a link, or after a seek), however the callback is
  1652. required to provide all of it.
  1653. \param _of The \c OggOpusFile on which to set the decode callback.
  1654. \param _decode_cb The callback function to call.
  1655. This may be <code>NULL</code> to disable calling the
  1656. callback.
  1657. \param _ctx The application-provided context pointer to pass to the
  1658. callback on each call.*/
  1659. void op_set_decode_callback(OggOpusFile *_of,
  1660. op_decode_cb_func _decode_cb,void *_ctx) OP_ARG_NONNULL(1);
  1661. /**Gain offset type that indicates that the provided offset is relative to the
  1662. header gain.
  1663. This is the default.*/
  1664. #define OP_HEADER_GAIN (0)
  1665. /**Gain offset type that indicates that the provided offset is relative to the
  1666. R128_ALBUM_GAIN value (if any), in addition to the header gain.*/
  1667. #define OP_ALBUM_GAIN (3007)
  1668. /**Gain offset type that indicates that the provided offset is relative to the
  1669. R128_TRACK_GAIN value (if any), in addition to the header gain.*/
  1670. #define OP_TRACK_GAIN (3008)
  1671. /**Gain offset type that indicates that the provided offset should be used as
  1672. the gain directly, without applying any the header or track gains.*/
  1673. #define OP_ABSOLUTE_GAIN (3009)
  1674. /**Sets the gain to be used for decoded output.
  1675. By default, the gain in the header is applied with no additional offset.
  1676. The total gain (including header gain and/or track gain, if applicable, and
  1677. this offset), will be clamped to [-32768,32767]/256 dB.
  1678. This is more than enough to saturate or underflow 16-bit PCM.
  1679. \note The new gain will not be applied to any already buffered, decoded
  1680. output.
  1681. This means you cannot change it sample-by-sample, as at best it will be
  1682. updated packet-by-packet.
  1683. It is meant for setting a target volume level, rather than applying smooth
  1684. fades, etc.
  1685. \param _of The \c OggOpusFile on which to set the gain offset.
  1686. \param _gain_type One of #OP_HEADER_GAIN, #OP_ALBUM_GAIN,
  1687. #OP_TRACK_GAIN, or #OP_ABSOLUTE_GAIN.
  1688. \param _gain_offset_q8 The gain offset to apply, in 1/256ths of a dB.
  1689. \return 0 on success or a negative value on error.
  1690. \retval #OP_EINVAL The \a _gain_type was unrecognized.*/
  1691. int op_set_gain_offset(OggOpusFile *_of,
  1692. int _gain_type,opus_int32 _gain_offset_q8) OP_ARG_NONNULL(1);
  1693. /**Sets whether or not dithering is enabled for 16-bit decoding.
  1694. By default, when <tt>libopusfile</tt> is compiled to use floating-point
  1695. internally, calling op_read() or op_read_stereo() will first decode to
  1696. float, and then convert to fixed-point using noise-shaping dithering.
  1697. This flag can be used to disable that dithering.
  1698. When the application uses op_read_float() or op_read_float_stereo(), or when
  1699. the library has been compiled to decode directly to fixed point, this flag
  1700. has no effect.
  1701. \param _of The \c OggOpusFile on which to enable or disable dithering.
  1702. \param _enabled A non-zero value to enable dithering, or 0 to disable it.*/
  1703. void op_set_dither_enabled(OggOpusFile *_of,int _enabled) OP_ARG_NONNULL(1);
  1704. /**Reads more samples from the stream.
  1705. \note Although \a _buf_size must indicate the total number of values that
  1706. can be stored in \a _pcm, the return value is the number of samples
  1707. <em>per channel</em>.
  1708. This is done because
  1709. <ol>
  1710. <li>The channel count cannot be known a priori (reading more samples might
  1711. advance us into the next link, with a different channel count), so
  1712. \a _buf_size cannot also be in units of samples per channel,</li>
  1713. <li>Returning the samples per channel matches the <code>libopus</code> API
  1714. as closely as we're able,</li>
  1715. <li>Returning the total number of values instead of samples per channel
  1716. would mean the caller would need a division to compute the samples per
  1717. channel, and might worry about the possibility of getting back samples
  1718. for some channels and not others, and</li>
  1719. <li>This approach is relatively fool-proof: if an application passes too
  1720. small a value to \a _buf_size, they will simply get fewer samples back,
  1721. and if they assume the return value is the total number of values, then
  1722. they will simply read too few (rather than reading too many and going
  1723. off the end of the buffer).</li>
  1724. </ol>
  1725. \param _of The \c OggOpusFile from which to read.
  1726. \param[out] _pcm A buffer in which to store the output PCM samples, as
  1727. signed native-endian 16-bit values at 48&nbsp;kHz
  1728. with a nominal range of <code>[-32768,32767)</code>.
  1729. Multiple channels are interleaved using the
  1730. <a href="http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9">Vorbis
  1731. channel ordering</a>.
  1732. This must have room for at least \a _buf_size values.
  1733. \param _buf_size The number of values that can be stored in \a _pcm.
  1734. It is recommended that this be large enough for at
  1735. least 120 ms of data at 48 kHz per channel (5760
  1736. values per channel).
  1737. Smaller buffers will simply return less data, possibly
  1738. consuming more memory to buffer the data internally.
  1739. <tt>libopusfile</tt> may return less data than
  1740. requested.
  1741. If so, there is no guarantee that the remaining data
  1742. in \a _pcm will be unmodified.
  1743. \param[out] _li The index of the link this data was decoded from.
  1744. You may pass <code>NULL</code> if you do not need this
  1745. information.
  1746. If this function fails (returning a negative value),
  1747. this parameter is left unset.
  1748. \return The number of samples read per channel on success, or a negative
  1749. value on failure.
  1750. The channel count can be retrieved on success by calling
  1751. <code>op_head(_of,*_li)</code>.
  1752. The number of samples returned may be 0 if the buffer was too small
  1753. to store even a single sample for all channels, or if end-of-file
  1754. was reached.
  1755. The list of possible failure codes follows.
  1756. Most of them can only be returned by unseekable, chained streams
  1757. that encounter a new link.
  1758. \retval #OP_HOLE There was a hole in the data, and some samples
  1759. may have been skipped.
  1760. Call this function again to continue decoding
  1761. past the hole.
  1762. \retval #OP_EREAD An underlying read operation failed.
  1763. This may signal a truncation attack from an
  1764. <https:> source.
  1765. \retval #OP_EFAULT An internal memory allocation failed.
  1766. \retval #OP_EIMPL An unseekable stream encountered a new link that
  1767. used a feature that is not implemented, such as
  1768. an unsupported channel family.
  1769. \retval #OP_EINVAL The stream was only partially open.
  1770. \retval #OP_ENOTFORMAT An unseekable stream encountered a new link that
  1771. did not have any logical Opus streams in it.
  1772. \retval #OP_EBADHEADER An unseekable stream encountered a new link with a
  1773. required header packet that was not properly
  1774. formatted, contained illegal values, or was
  1775. missing altogether.
  1776. \retval #OP_EVERSION An unseekable stream encountered a new link with
  1777. an ID header that contained an unrecognized
  1778. version number.
  1779. \retval #OP_EBADPACKET Failed to properly decode the next packet.
  1780. \retval #OP_EBADLINK We failed to find data we had seen before.
  1781. \retval #OP_EBADTIMESTAMP An unseekable stream encountered a new link with
  1782. a starting timestamp that failed basic validity
  1783. checks.*/
  1784. OP_WARN_UNUSED_RESULT int op_read(OggOpusFile *_of,
  1785. opus_int16 *_pcm,int _buf_size,int *_li) OP_ARG_NONNULL(1);
  1786. /**Reads more samples from the stream.
  1787. \note Although \a _buf_size must indicate the total number of values that
  1788. can be stored in \a _pcm, the return value is the number of samples
  1789. <em>per channel</em>.
  1790. <ol>
  1791. <li>The channel count cannot be known a priori (reading more samples might
  1792. advance us into the next link, with a different channel count), so
  1793. \a _buf_size cannot also be in units of samples per channel,</li>
  1794. <li>Returning the samples per channel matches the <code>libopus</code> API
  1795. as closely as we're able,</li>
  1796. <li>Returning the total number of values instead of samples per channel
  1797. would mean the caller would need a division to compute the samples per
  1798. channel, and might worry about the possibility of getting back samples
  1799. for some channels and not others, and</li>
  1800. <li>This approach is relatively fool-proof: if an application passes too
  1801. small a value to \a _buf_size, they will simply get fewer samples back,
  1802. and if they assume the return value is the total number of values, then
  1803. they will simply read too few (rather than reading too many and going
  1804. off the end of the buffer).</li>
  1805. </ol>
  1806. \param _of The \c OggOpusFile from which to read.
  1807. \param[out] _pcm A buffer in which to store the output PCM samples as
  1808. signed floats at 48&nbsp;kHz with a nominal range of
  1809. <code>[-1.0,1.0]</code>.
  1810. Multiple channels are interleaved using the
  1811. <a href="http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9">Vorbis
  1812. channel ordering</a>.
  1813. This must have room for at least \a _buf_size floats.
  1814. \param _buf_size The number of floats that can be stored in \a _pcm.
  1815. It is recommended that this be large enough for at
  1816. least 120 ms of data at 48 kHz per channel (5760
  1817. samples per channel).
  1818. Smaller buffers will simply return less data, possibly
  1819. consuming more memory to buffer the data internally.
  1820. If less than \a _buf_size values are returned,
  1821. <tt>libopusfile</tt> makes no guarantee that the
  1822. remaining data in \a _pcm will be unmodified.
  1823. \param[out] _li The index of the link this data was decoded from.
  1824. You may pass <code>NULL</code> if you do not need this
  1825. information.
  1826. If this function fails (returning a negative value),
  1827. this parameter is left unset.
  1828. \return The number of samples read per channel on success, or a negative
  1829. value on failure.
  1830. The channel count can be retrieved on success by calling
  1831. <code>op_head(_of,*_li)</code>.
  1832. The number of samples returned may be 0 if the buffer was too small
  1833. to store even a single sample for all channels, or if end-of-file
  1834. was reached.
  1835. The list of possible failure codes follows.
  1836. Most of them can only be returned by unseekable, chained streams
  1837. that encounter a new link.
  1838. \retval #OP_HOLE There was a hole in the data, and some samples
  1839. may have been skipped.
  1840. Call this function again to continue decoding
  1841. past the hole.
  1842. \retval #OP_EREAD An underlying read operation failed.
  1843. This may signal a truncation attack from an
  1844. <https:> source.
  1845. \retval #OP_EFAULT An internal memory allocation failed.
  1846. \retval #OP_EIMPL An unseekable stream encountered a new link that
  1847. used a feature that is not implemented, such as
  1848. an unsupported channel family.
  1849. \retval #OP_EINVAL The stream was only partially open.
  1850. \retval #OP_ENOTFORMAT An unseekable stream encountered a new link that
  1851. did not have any logical Opus streams in it.
  1852. \retval #OP_EBADHEADER An unseekable stream encountered a new link with a
  1853. required header packet that was not properly
  1854. formatted, contained illegal values, or was
  1855. missing altogether.
  1856. \retval #OP_EVERSION An unseekable stream encountered a new link with
  1857. an ID header that contained an unrecognized
  1858. version number.
  1859. \retval #OP_EBADPACKET Failed to properly decode the next packet.
  1860. \retval #OP_EBADLINK We failed to find data we had seen before.
  1861. \retval #OP_EBADTIMESTAMP An unseekable stream encountered a new link with
  1862. a starting timestamp that failed basic validity
  1863. checks.*/
  1864. OP_WARN_UNUSED_RESULT int op_read_float(OggOpusFile *_of,
  1865. float *_pcm,int _buf_size,int *_li) OP_ARG_NONNULL(1);
  1866. /**Reads more samples from the stream and downmixes to stereo, if necessary.
  1867. This function is intended for simple players that want a uniform output
  1868. format, even if the channel count changes between links in a chained
  1869. stream.
  1870. \note \a _buf_size indicates the total number of values that can be stored
  1871. in \a _pcm, while the return value is the number of samples <em>per
  1872. channel</em>, even though the channel count is known, for consistency with
  1873. op_read().
  1874. \param _of The \c OggOpusFile from which to read.
  1875. \param[out] _pcm A buffer in which to store the output PCM samples, as
  1876. signed native-endian 16-bit values at 48&nbsp;kHz
  1877. with a nominal range of <code>[-32768,32767)</code>.
  1878. The left and right channels are interleaved in the
  1879. buffer.
  1880. This must have room for at least \a _buf_size values.
  1881. \param _buf_size The number of values that can be stored in \a _pcm.
  1882. It is recommended that this be large enough for at
  1883. least 120 ms of data at 48 kHz per channel (11520
  1884. values total).
  1885. Smaller buffers will simply return less data, possibly
  1886. consuming more memory to buffer the data internally.
  1887. If less than \a _buf_size values are returned,
  1888. <tt>libopusfile</tt> makes no guarantee that the
  1889. remaining data in \a _pcm will be unmodified.
  1890. \return The number of samples read per channel on success, or a negative
  1891. value on failure.
  1892. The number of samples returned may be 0 if the buffer was too small
  1893. to store even a single sample for both channels, or if end-of-file
  1894. was reached.
  1895. The list of possible failure codes follows.
  1896. Most of them can only be returned by unseekable, chained streams
  1897. that encounter a new link.
  1898. \retval #OP_HOLE There was a hole in the data, and some samples
  1899. may have been skipped.
  1900. Call this function again to continue decoding
  1901. past the hole.
  1902. \retval #OP_EREAD An underlying read operation failed.
  1903. This may signal a truncation attack from an
  1904. <https:> source.
  1905. \retval #OP_EFAULT An internal memory allocation failed.
  1906. \retval #OP_EIMPL An unseekable stream encountered a new link that
  1907. used a feature that is not implemented, such as
  1908. an unsupported channel family.
  1909. \retval #OP_EINVAL The stream was only partially open.
  1910. \retval #OP_ENOTFORMAT An unseekable stream encountered a new link that
  1911. did not have any logical Opus streams in it.
  1912. \retval #OP_EBADHEADER An unseekable stream encountered a new link with a
  1913. required header packet that was not properly
  1914. formatted, contained illegal values, or was
  1915. missing altogether.
  1916. \retval #OP_EVERSION An unseekable stream encountered a new link with
  1917. an ID header that contained an unrecognized
  1918. version number.
  1919. \retval #OP_EBADPACKET Failed to properly decode the next packet.
  1920. \retval #OP_EBADLINK We failed to find data we had seen before.
  1921. \retval #OP_EBADTIMESTAMP An unseekable stream encountered a new link with
  1922. a starting timestamp that failed basic validity
  1923. checks.*/
  1924. OP_WARN_UNUSED_RESULT int op_read_stereo(OggOpusFile *_of,
  1925. opus_int16 *_pcm,int _buf_size) OP_ARG_NONNULL(1);
  1926. /**Reads more samples from the stream and downmixes to stereo, if necessary.
  1927. This function is intended for simple players that want a uniform output
  1928. format, even if the channel count changes between links in a chained
  1929. stream.
  1930. \note \a _buf_size indicates the total number of values that can be stored
  1931. in \a _pcm, while the return value is the number of samples <em>per
  1932. channel</em>, even though the channel count is known, for consistency with
  1933. op_read_float().
  1934. \param _of The \c OggOpusFile from which to read.
  1935. \param[out] _pcm A buffer in which to store the output PCM samples, as
  1936. signed floats at 48&nbsp;kHz with a nominal range of
  1937. <code>[-1.0,1.0]</code>.
  1938. The left and right channels are interleaved in the
  1939. buffer.
  1940. This must have room for at least \a _buf_size values.
  1941. \param _buf_size The number of values that can be stored in \a _pcm.
  1942. It is recommended that this be large enough for at
  1943. least 120 ms of data at 48 kHz per channel (11520
  1944. values total).
  1945. Smaller buffers will simply return less data, possibly
  1946. consuming more memory to buffer the data internally.
  1947. If less than \a _buf_size values are returned,
  1948. <tt>libopusfile</tt> makes no guarantee that the
  1949. remaining data in \a _pcm will be unmodified.
  1950. \return The number of samples read per channel on success, or a negative
  1951. value on failure.
  1952. The number of samples returned may be 0 if the buffer was too small
  1953. to store even a single sample for both channels, or if end-of-file
  1954. was reached.
  1955. The list of possible failure codes follows.
  1956. Most of them can only be returned by unseekable, chained streams
  1957. that encounter a new link.
  1958. \retval #OP_HOLE There was a hole in the data, and some samples
  1959. may have been skipped.
  1960. Call this function again to continue decoding
  1961. past the hole.
  1962. \retval #OP_EREAD An underlying read operation failed.
  1963. This may signal a truncation attack from an
  1964. <https:> source.
  1965. \retval #OP_EFAULT An internal memory allocation failed.
  1966. \retval #OP_EIMPL An unseekable stream encountered a new link that
  1967. used a feature that is not implemented, such as
  1968. an unsupported channel family.
  1969. \retval #OP_EINVAL The stream was only partially open.
  1970. \retval #OP_ENOTFORMAT An unseekable stream encountered a new link that
  1971. that did not have any logical Opus streams in it.
  1972. \retval #OP_EBADHEADER An unseekable stream encountered a new link with a
  1973. required header packet that was not properly
  1974. formatted, contained illegal values, or was
  1975. missing altogether.
  1976. \retval #OP_EVERSION An unseekable stream encountered a new link with
  1977. an ID header that contained an unrecognized
  1978. version number.
  1979. \retval #OP_EBADPACKET Failed to properly decode the next packet.
  1980. \retval #OP_EBADLINK We failed to find data we had seen before.
  1981. \retval #OP_EBADTIMESTAMP An unseekable stream encountered a new link with
  1982. a starting timestamp that failed basic validity
  1983. checks.*/
  1984. OP_WARN_UNUSED_RESULT int op_read_float_stereo(OggOpusFile *_of,
  1985. float *_pcm,int _buf_size) OP_ARG_NONNULL(1);
  1986. /*@}*/
  1987. /*@}*/
  1988. # if OP_GNUC_PREREQ(4,0)
  1989. # pragma GCC visibility pop
  1990. # endif
  1991. # if defined(__cplusplus)
  1992. }
  1993. # endif
  1994. #endif