opusfile.h 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090
  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. /**Look up a comment value by its tag.
  459. \param _tags An initialized #OpusTags structure.
  460. \param _tag The tag to look up.
  461. \param _count The instance of the tag.
  462. The same tag can appear multiple times, each with a distinct
  463. value, so an index is required to retrieve them all.
  464. The order in which these values appear is significant and
  465. should be preserved.
  466. Use opus_tags_query_count() to get the legal range for the
  467. \a _count parameter.
  468. \return A pointer to the queried tag's value.
  469. This points directly to data in the #OpusTags structure.
  470. It should not be modified or freed by the application, and
  471. modifications to the structure may invalidate the pointer.
  472. \retval NULL If no matching tag is found.*/
  473. const char *opus_tags_query(const OpusTags *_tags,const char *_tag,int _count)
  474. OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
  475. /**Look up the number of instances of a tag.
  476. Call this first when querying for a specific tag and then iterate over the
  477. number of instances with separate calls to opus_tags_query() to retrieve
  478. all the values for that tag in order.
  479. \param _tags An initialized #OpusTags structure.
  480. \param _tag The tag to look up.
  481. \return The number of instances of this particular tag.*/
  482. int opus_tags_query_count(const OpusTags *_tags,const char *_tag)
  483. OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
  484. /**Get the track gain from an R128_TRACK_GAIN tag, if one was specified.
  485. This searches for the first R128_TRACK_GAIN tag with a valid signed,
  486. 16-bit decimal integer value and returns the value.
  487. This routine is exposed merely for convenience for applications which wish
  488. to do something special with the track gain (i.e., display it).
  489. If you simply wish to apply the track gain instead of the header gain, you
  490. can use op_set_gain_offset() with an #OP_TRACK_GAIN type and no offset.
  491. \param _tags An initialized #OpusTags structure.
  492. \param[out] _gain_q8 The track gain, in 1/256ths of a dB.
  493. This will lie in the range [-32768,32767], and should
  494. be applied in <em>addition</em> to the header gain.
  495. On error, no value is returned, and the previous
  496. contents remain unchanged.
  497. \return 0 on success, or a negative value on error.
  498. \retval #OP_FALSE There was no track gain available in the given tags.*/
  499. int opus_tags_get_track_gain(const OpusTags *_tags,int *_gain_q8)
  500. OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
  501. /**Clears the #OpusTags structure.
  502. This should be called on an #OpusTags structure after it is no longer
  503. needed.
  504. It will free all memory used by the structure members.
  505. \param _tags The #OpusTags structure to clear.*/
  506. void opus_tags_clear(OpusTags *_tags) OP_ARG_NONNULL(1);
  507. /**Check if \a _comment is an instance of a \a _tag_name tag.
  508. \see opus_tagncompare
  509. \param _tag_name A NUL-terminated, case-insensitive, ASCII string containing
  510. the name of the tag to check for (without the terminating
  511. '=' character).
  512. \param _comment The comment string to check.
  513. \return An integer less than, equal to, or greater than zero if \a _comment
  514. is found respectively, to be less than, to match, or be greater
  515. than a "tag=value" string whose tag matches \a _tag_name.*/
  516. int opus_tagcompare(const char *_tag_name,const char *_comment);
  517. /**Check if \a _comment is an instance of a \a _tag_name tag.
  518. This version is slightly more efficient than opus_tagcompare() if the length
  519. of the tag name is already known (e.g., because it is a constant).
  520. \see opus_tagcompare
  521. \param _tag_name A case-insensitive ASCII string containing the name of the
  522. tag to check for (without the terminating '=' character).
  523. \param _tag_len The number of characters in the tag name.
  524. This must be non-negative.
  525. \param _comment The comment string to check.
  526. \return An integer less than, equal to, or greater than zero if \a _comment
  527. is found respectively, to be less than, to match, or be greater
  528. than a "tag=value" string whose tag matches the first \a _tag_len
  529. characters of \a _tag_name.*/
  530. int opus_tagncompare(const char *_tag_name,int _tag_len,const char *_comment);
  531. /**Parse a single METADATA_BLOCK_PICTURE tag.
  532. This decodes the BASE64-encoded content of the tag and returns a structure
  533. with the MIME type, description, image parameters (if known), and the
  534. compressed image data.
  535. If the MIME type indicates the presence of an image format we recognize
  536. (JPEG, PNG, or GIF) and the actual image data contains the magic signature
  537. associated with that format, then the OpusPictureTag::format field will be
  538. set to the corresponding format.
  539. This is provided as a convenience to avoid requiring applications to parse
  540. the MIME type and/or do their own format detection for the commonly used
  541. formats.
  542. In this case, we also attempt to extract the image parameters directly from
  543. the image data (overriding any that were present in the tag, which the
  544. specification says applications are not meant to rely on).
  545. The application must still provide its own support for actually decoding the
  546. image data and, if applicable, retrieving that data from URLs.
  547. \param[out] _pic Returns the parsed picture data.
  548. No sanitation is done on the type, MIME type, or
  549. description fields, so these might return invalid values.
  550. The contents of this structure are left unmodified on
  551. failure.
  552. \param _tag The METADATA_BLOCK_PICTURE tag contents.
  553. The leading "METADATA_BLOCK_PICTURE=" portion is optional,
  554. to allow the function to be used on either directly on the
  555. values in OpusTags::user_comments or on the return value
  556. of opus_tags_query().
  557. \return 0 on success or a negative value on error.
  558. \retval #OP_ENOTFORMAT The METADATA_BLOCK_PICTURE contents were not valid.
  559. \retval #OP_EFAULT There was not enough memory to store the picture tag
  560. contents.*/
  561. OP_WARN_UNUSED_RESULT int opus_picture_tag_parse(OpusPictureTag *_pic,
  562. const char *_tag) OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
  563. /**Initializes an #OpusPictureTag structure.
  564. This should be called on a freshly allocated #OpusPictureTag structure
  565. before attempting to use it.
  566. \param _pic The #OpusPictureTag structure to initialize.*/
  567. void opus_picture_tag_init(OpusPictureTag *_pic) OP_ARG_NONNULL(1);
  568. /**Clears the #OpusPictureTag structure.
  569. This should be called on an #OpusPictureTag structure after it is no longer
  570. needed.
  571. It will free all memory used by the structure members.
  572. \param _pic The #OpusPictureTag structure to clear.*/
  573. void opus_picture_tag_clear(OpusPictureTag *_pic) OP_ARG_NONNULL(1);
  574. /*@}*/
  575. /*@}*/
  576. /**\defgroup url_options URL Reading Options*/
  577. /*@{*/
  578. /**\name URL reading options
  579. Options for op_url_stream_create() and associated functions.
  580. These allow you to provide proxy configuration parameters, skip SSL
  581. certificate checks, etc.
  582. Options are processed in order, and if the same option is passed multiple
  583. times, only the value specified by the last occurrence has an effect
  584. (unless otherwise specified).
  585. They may be expanded in the future.*/
  586. /*@{*/
  587. /**@cond PRIVATE*/
  588. /*These are the raw numbers used to define the request codes.
  589. They should not be used directly.*/
  590. #define OP_SSL_SKIP_CERTIFICATE_CHECK_REQUEST (6464)
  591. #define OP_HTTP_PROXY_HOST_REQUEST (6528)
  592. #define OP_HTTP_PROXY_PORT_REQUEST (6592)
  593. #define OP_HTTP_PROXY_USER_REQUEST (6656)
  594. #define OP_HTTP_PROXY_PASS_REQUEST (6720)
  595. #define OP_GET_SERVER_INFO_REQUEST (6784)
  596. #define OP_URL_OPT(_request) ((_request)+(char *)0)
  597. /*These macros trigger compilation errors or warnings if the wrong types are
  598. provided to one of the URL options.*/
  599. #define OP_CHECK_INT(_x) ((void)((_x)==(opus_int32)0),(opus_int32)(_x))
  600. #define OP_CHECK_CONST_CHAR_PTR(_x) ((_x)+((_x)-(const char *)(_x)))
  601. #define OP_CHECK_SERVER_INFO_PTR(_x) ((_x)+((_x)-(OpusServerInfo *)(_x)))
  602. /**@endcond*/
  603. /**HTTP/Shoutcast/Icecast server information associated with a URL.*/
  604. struct OpusServerInfo{
  605. /**The name of the server (icy-name/ice-name).
  606. This is <code>NULL</code> if there was no <code>icy-name</code> or
  607. <code>ice-name</code> header.*/
  608. char *name;
  609. /**A short description of the server (icy-description/ice-description).
  610. This is <code>NULL</code> if there was no <code>icy-description</code> or
  611. <code>ice-description</code> header.*/
  612. char *description;
  613. /**The genre the server falls under (icy-genre/ice-genre).
  614. This is <code>NULL</code> if there was no <code>icy-genre</code> or
  615. <code>ice-genre</code> header.*/
  616. char *genre;
  617. /**The homepage for the server (icy-url/ice-url).
  618. This is <code>NULL</code> if there was no <code>icy-url</code> or
  619. <code>ice-url</code> header.*/
  620. char *url;
  621. /**The software used by the origin server (Server).
  622. This is <code>NULL</code> if there was no <code>Server</code> header.*/
  623. char *server;
  624. /**The media type of the entity sent to the recepient (Content-Type).
  625. This is <code>NULL</code> if there was no <code>Content-Type</code>
  626. header.*/
  627. char *content_type;
  628. /**The nominal stream bitrate in kbps (icy-br/ice-bitrate).
  629. This is <code>-1</code> if there was no <code>icy-br</code> or
  630. <code>ice-bitrate</code> header.*/
  631. opus_int32 bitrate_kbps;
  632. /**Flag indicating whether the server is public (<code>1</code>) or not
  633. (<code>0</code>) (icy-pub/ice-public).
  634. This is <code>-1</code> if there was no <code>icy-pub</code> or
  635. <code>ice-public</code> header.*/
  636. int is_public;
  637. /**Flag indicating whether the server is using HTTPS instead of HTTP.
  638. This is <code>0</code> unless HTTPS is being used.
  639. This may not match the protocol used in the original URL if there were
  640. redirections.*/
  641. int is_ssl;
  642. };
  643. /**Initializes an #OpusServerInfo structure.
  644. All fields are set as if the corresponding header was not available.
  645. \param _info The #OpusServerInfo structure to initialize.
  646. \note If you use this function, you must link against <tt>libopusurl</tt>.*/
  647. void opus_server_info_init(OpusServerInfo *_info) OP_ARG_NONNULL(1);
  648. /**Clears the #OpusServerInfo structure.
  649. This should be called on an #OpusServerInfo structure after it is no longer
  650. needed.
  651. It will free all memory used by the structure members.
  652. \param _info The #OpusServerInfo structure to clear.
  653. \note If you use this function, you must link against <tt>libopusurl</tt>.*/
  654. void opus_server_info_clear(OpusServerInfo *_info) OP_ARG_NONNULL(1);
  655. /**Skip the certificate check when connecting via TLS/SSL (https).
  656. \param _b <code>opus_int32</code>: Whether or not to skip the certificate
  657. check.
  658. The check will be skipped if \a _b is non-zero, and will not be
  659. skipped if \a _b is zero.
  660. \hideinitializer*/
  661. #define OP_SSL_SKIP_CERTIFICATE_CHECK(_b) \
  662. OP_URL_OPT(OP_SSL_SKIP_CERTIFICATE_CHECK_REQUEST),OP_CHECK_INT(_b)
  663. /**Proxy connections through the given host.
  664. If no port is specified via #OP_HTTP_PROXY_PORT, the port number defaults
  665. to 8080 (http-alt).
  666. All proxy parameters are ignored for non-http and non-https URLs.
  667. \param _host <code>const char *</code>: The proxy server hostname.
  668. This may be <code>NULL</code> to disable the use of a proxy
  669. server.
  670. \hideinitializer*/
  671. #define OP_HTTP_PROXY_HOST(_host) \
  672. OP_URL_OPT(OP_HTTP_PROXY_HOST_REQUEST),OP_CHECK_CONST_CHAR_PTR(_host)
  673. /**Use the given port when proxying connections.
  674. This option only has an effect if #OP_HTTP_PROXY_HOST is specified with a
  675. non-<code>NULL</code> \a _host.
  676. If this option is not provided, the proxy port number defaults to 8080
  677. (http-alt).
  678. All proxy parameters are ignored for non-http and non-https URLs.
  679. \param _port <code>opus_int32</code>: The proxy server port.
  680. This must be in the range 0...65535 (inclusive), or the
  681. URL function this is passed to will fail.
  682. \hideinitializer*/
  683. #define OP_HTTP_PROXY_PORT(_port) \
  684. OP_URL_OPT(OP_HTTP_PROXY_PORT_REQUEST),OP_CHECK_INT(_port)
  685. /**Use the given user name for authentication when proxying connections.
  686. All proxy parameters are ignored for non-http and non-https URLs.
  687. \param _user const char *: The proxy server user name.
  688. This may be <code>NULL</code> to disable proxy
  689. authentication.
  690. A non-<code>NULL</code> value only has an effect
  691. if #OP_HTTP_PROXY_HOST and #OP_HTTP_PROXY_PASS
  692. are also specified with non-<code>NULL</code>
  693. arguments.
  694. \hideinitializer*/
  695. #define OP_HTTP_PROXY_USER(_user) \
  696. OP_URL_OPT(OP_HTTP_PROXY_USER_REQUEST),OP_CHECK_CONST_CHAR_PTR(_user)
  697. /**Use the given password for authentication when proxying connections.
  698. All proxy parameters are ignored for non-http and non-https URLs.
  699. \param _pass const char *: The proxy server password.
  700. This may be <code>NULL</code> to disable proxy
  701. authentication.
  702. A non-<code>NULL</code> value only has an effect
  703. if #OP_HTTP_PROXY_HOST and #OP_HTTP_PROXY_USER
  704. are also specified with non-<code>NULL</code>
  705. arguments.
  706. \hideinitializer*/
  707. #define OP_HTTP_PROXY_PASS(_pass) \
  708. OP_URL_OPT(OP_HTTP_PROXY_PASS_REQUEST),OP_CHECK_CONST_CHAR_PTR(_pass)
  709. /**Parse information about the streaming server (if any) and return it.
  710. Very little validation is done.
  711. In particular, OpusServerInfo::url may not be a valid URL,
  712. OpusServerInfo::bitrate_kbps may not really be in kbps, and
  713. OpusServerInfo::content_type may not be a valid MIME type.
  714. The character set of the string fields is not specified anywhere, and should
  715. not be assumed to be valid UTF-8.
  716. \param _info OpusServerInfo *: Returns information about the server.
  717. If there is any error opening the stream, the
  718. contents of this structure remain
  719. unmodified.
  720. On success, fills in the structure with the
  721. server information that was available, if
  722. any.
  723. After a successful return, the contents of
  724. this structure should be freed by calling
  725. opus_server_info_clear().
  726. \hideinitializer*/
  727. #define OP_GET_SERVER_INFO(_info) \
  728. OP_URL_OPT(OP_GET_SERVER_INFO_REQUEST),OP_CHECK_SERVER_INFO_PTR(_info)
  729. /*@}*/
  730. /*@}*/
  731. /**\defgroup stream_callbacks Abstract Stream Reading Interface*/
  732. /*@{*/
  733. /**\name Functions for reading from streams
  734. These functions define the interface used to read from and seek in a stream
  735. of data.
  736. A stream does not need to implement seeking, but the decoder will not be
  737. able to seek if it does not do so.
  738. These functions also include some convenience routines for working with
  739. standard <code>FILE</code> pointers, complete streams stored in a single
  740. block of memory, or URLs.*/
  741. /*@{*/
  742. /**Reads up to \a _nbytes bytes of data from \a _stream.
  743. \param _stream The stream to read from.
  744. \param[out] _ptr The buffer to store the data in.
  745. \param _nbytes The maximum number of bytes to read.
  746. This function may return fewer, though it will not
  747. return zero unless it reaches end-of-file.
  748. \return The number of bytes successfully read, or a negative value on
  749. error.*/
  750. typedef int (*op_read_func)(void *_stream,unsigned char *_ptr,int _nbytes);
  751. /**Sets the position indicator for \a _stream.
  752. The new position, measured in bytes, is obtained by adding \a _offset
  753. bytes to the position specified by \a _whence.
  754. If \a _whence is set to <code>SEEK_SET</code>, <code>SEEK_CUR</code>, or
  755. <code>SEEK_END</code>, the offset is relative to the start of the stream,
  756. the current position indicator, or end-of-file, respectively.
  757. \retval 0 Success.
  758. \retval -1 Seeking is not supported or an error occurred.
  759. <code>errno</code> need not be set.*/
  760. typedef int (*op_seek_func)(void *_stream,opus_int64 _offset,int _whence);
  761. /**Obtains the current value of the position indicator for \a _stream.
  762. \return The current position indicator.*/
  763. typedef opus_int64 (*op_tell_func)(void *_stream);
  764. /**Closes the underlying stream.
  765. \retval 0 Success.
  766. \retval EOF An error occurred.
  767. <code>errno</code> need not be set.*/
  768. typedef int (*op_close_func)(void *_stream);
  769. /**The callbacks used to access non-<code>FILE</code> stream resources.
  770. The function prototypes are basically the same as for the stdio functions
  771. <code>fread()</code>, <code>fseek()</code>, <code>ftell()</code>, and
  772. <code>fclose()</code>.
  773. The differences are that the <code>FILE *</code> arguments have been
  774. replaced with a <code>void *</code>, which is to be used as a pointer to
  775. whatever internal data these functions might need, that #seek and #tell
  776. take and return 64-bit offsets, and that #seek <em>must</em> return -1 if
  777. the stream is unseekable.*/
  778. struct OpusFileCallbacks{
  779. /**Used to read data from the stream.
  780. This must not be <code>NULL</code>.*/
  781. op_read_func read;
  782. /**Used to seek in the stream.
  783. This may be <code>NULL</code> if seeking is not implemented.*/
  784. op_seek_func seek;
  785. /**Used to return the current read position in the stream.
  786. This may be <code>NULL</code> if seeking is not implemented.*/
  787. op_tell_func tell;
  788. /**Used to close the stream when the decoder is freed.
  789. This may be <code>NULL</code> to leave the stream open.*/
  790. op_close_func close;
  791. };
  792. /**Opens a stream with <code>fopen()</code> and fills in a set of callbacks
  793. that can be used to access it.
  794. This is useful to avoid writing your own portable 64-bit seeking wrappers,
  795. and also avoids cross-module linking issues on Windows, where a
  796. <code>FILE *</code> must be accessed by routines defined in the same module
  797. that opened it.
  798. \param[out] _cb The callbacks to use for this file.
  799. If there is an error opening the file, nothing will be
  800. filled in here.
  801. \param _path The path to the file to open.
  802. On Windows, this string must be UTF-8 (to allow access to
  803. files whose names cannot be represented in the current
  804. MBCS code page).
  805. All other systems use the native character encoding.
  806. \param _mode The mode to open the file in.
  807. \return A stream handle to use with the callbacks, or <code>NULL</code> on
  808. error.*/
  809. OP_WARN_UNUSED_RESULT void *op_fopen(OpusFileCallbacks *_cb,
  810. const char *_path,const char *_mode) OP_ARG_NONNULL(1) OP_ARG_NONNULL(2)
  811. OP_ARG_NONNULL(3);
  812. /**Opens a stream with <code>fdopen()</code> and fills in a set of callbacks
  813. that can be used to access it.
  814. This is useful to avoid writing your own portable 64-bit seeking wrappers,
  815. and also avoids cross-module linking issues on Windows, where a
  816. <code>FILE *</code> must be accessed by routines defined in the same module
  817. that opened it.
  818. \param[out] _cb The callbacks to use for this file.
  819. If there is an error opening the file, nothing will be
  820. filled in here.
  821. \param _fd The file descriptor to open.
  822. \param _mode The mode to open the file in.
  823. \return A stream handle to use with the callbacks, or <code>NULL</code> on
  824. error.*/
  825. OP_WARN_UNUSED_RESULT void *op_fdopen(OpusFileCallbacks *_cb,
  826. int _fd,const char *_mode) OP_ARG_NONNULL(1) OP_ARG_NONNULL(3);
  827. /**Opens a stream with <code>freopen()</code> and fills in a set of callbacks
  828. that can be used to access it.
  829. This is useful to avoid writing your own portable 64-bit seeking wrappers,
  830. and also avoids cross-module linking issues on Windows, where a
  831. <code>FILE *</code> must be accessed by routines defined in the same module
  832. that opened it.
  833. \param[out] _cb The callbacks to use for this file.
  834. If there is an error opening the file, nothing will be
  835. filled in here.
  836. \param _path The path to the file to open.
  837. On Windows, this string must be UTF-8 (to allow access
  838. to files whose names cannot be represented in the
  839. current MBCS code page).
  840. All other systems use the native character encoding.
  841. \param _mode The mode to open the file in.
  842. \param _stream A stream previously returned by op_fopen(), op_fdopen(),
  843. or op_freopen().
  844. \return A stream handle to use with the callbacks, or <code>NULL</code> on
  845. error.*/
  846. OP_WARN_UNUSED_RESULT void *op_freopen(OpusFileCallbacks *_cb,
  847. const char *_path,const char *_mode,void *_stream) OP_ARG_NONNULL(1)
  848. OP_ARG_NONNULL(2) OP_ARG_NONNULL(3) OP_ARG_NONNULL(4);
  849. /**Creates a stream that reads from the given block of memory.
  850. This block of memory must contain the complete stream to decode.
  851. This is useful for caching small streams (e.g., sound effects) in RAM.
  852. \param[out] _cb The callbacks to use for this stream.
  853. If there is an error creating the stream, nothing will be
  854. filled in here.
  855. \param _data The block of memory to read from.
  856. \param _size The size of the block of memory.
  857. \return A stream handle to use with the callbacks, or <code>NULL</code> on
  858. error.*/
  859. OP_WARN_UNUSED_RESULT void *op_mem_stream_create(OpusFileCallbacks *_cb,
  860. const unsigned char *_data,size_t _size) OP_ARG_NONNULL(1);
  861. /**Creates a stream that reads from the given URL.
  862. This function behaves identically to op_url_stream_create(), except that it
  863. takes a va_list instead of a variable number of arguments.
  864. It does not call the <code>va_end</code> macro, and because it invokes the
  865. <code>va_arg</code> macro, the value of \a _ap is undefined after the call.
  866. \note If you use this function, you must link against <tt>libopusurl</tt>.
  867. \param[out] _cb The callbacks to use for this stream.
  868. If there is an error creating the stream, nothing will
  869. be filled in here.
  870. \param _url The URL to read from.
  871. Currently only the <file:>, <http:>, and <https:>
  872. schemes are supported.
  873. Both <http:> and <https:> may be disabled at compile
  874. time, in which case opening such URLs will always fail.
  875. Currently this only supports URIs.
  876. IRIs should be converted to UTF-8 and URL-escaped, with
  877. internationalized domain names encoded in punycode,
  878. before passing them to this function.
  879. \param[in,out] _ap A list of the \ref url_options "optional flags" to use.
  880. This is a variable-length list of options terminated
  881. with <code>NULL</code>.
  882. \return A stream handle to use with the callbacks, or <code>NULL</code> on
  883. error.*/
  884. OP_WARN_UNUSED_RESULT void *op_url_stream_vcreate(OpusFileCallbacks *_cb,
  885. const char *_url,va_list _ap) OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
  886. /**Creates a stream that reads from the given URL.
  887. \note If you use this function, you must link against <tt>libopusurl</tt>.
  888. \param[out] _cb The callbacks to use for this stream.
  889. If there is an error creating the stream, nothing will be
  890. filled in here.
  891. \param _url The URL to read from.
  892. Currently only the <file:>, <http:>, and <https:> schemes
  893. are supported.
  894. Both <http:> and <https:> may be disabled at compile time,
  895. in which case opening such URLs will always fail.
  896. Currently this only supports URIs.
  897. IRIs should be converted to UTF-8 and URL-escaped, with
  898. internationalized domain names encoded in punycode, before
  899. passing them to this function.
  900. \param ... The \ref url_options "optional flags" to use.
  901. This is a variable-length list of options terminated with
  902. <code>NULL</code>.
  903. \return A stream handle to use with the callbacks, or <code>NULL</code> on
  904. error.*/
  905. OP_WARN_UNUSED_RESULT void *op_url_stream_create(OpusFileCallbacks *_cb,
  906. const char *_url,...) OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
  907. /*@}*/
  908. /*@}*/
  909. /**\defgroup stream_open_close Opening and Closing*/
  910. /*@{*/
  911. /**\name Functions for opening and closing streams
  912. These functions allow you to test a stream to see if it is Opus, open it,
  913. and close it.
  914. Several flavors are provided for each of the built-in stream types, plus a
  915. more general version which takes a set of application-provided callbacks.*/
  916. /*@{*/
  917. /**Test to see if this is an Opus stream.
  918. For good results, you will need at least 57 bytes (for a pure Opus-only
  919. stream).
  920. Something like 512 bytes will give more reliable results for multiplexed
  921. streams.
  922. This function is meant to be a quick-rejection filter.
  923. Its purpose is not to guarantee that a stream is a valid Opus stream, but to
  924. ensure that it looks enough like Opus that it isn't going to be recognized
  925. as some other format (except possibly an Opus stream that is also
  926. multiplexed with other codecs, such as video).
  927. \param[out] _head The parsed ID header contents.
  928. You may pass <code>NULL</code> if you do not need
  929. this information.
  930. If the function fails, the contents of this structure
  931. remain untouched.
  932. \param _initial_data An initial buffer of data from the start of the
  933. stream.
  934. \param _initial_bytes The number of bytes in \a _initial_data.
  935. \return 0 if the data appears to be Opus, or a negative value on error.
  936. \retval #OP_FALSE There was not enough data to tell if this was an Opus
  937. stream or not.
  938. \retval #OP_EFAULT An internal memory allocation failed.
  939. \retval #OP_EIMPL The stream used a feature that is not implemented,
  940. such as an unsupported channel family.
  941. \retval #OP_ENOTFORMAT If the data did not contain a recognizable ID
  942. header for an Opus stream.
  943. \retval #OP_EVERSION If the version field signaled a version this library
  944. does not know how to parse.
  945. \retval #OP_EBADHEADER The ID header was not properly formatted or contained
  946. illegal values.*/
  947. int op_test(OpusHead *_head,
  948. const unsigned char *_initial_data,size_t _initial_bytes);
  949. /**Open a stream from the given file path.
  950. \param _path The path to the file to open.
  951. \param[out] _error Returns 0 on success, or a failure code on error.
  952. You may pass in <code>NULL</code> if you don't want the
  953. failure code.
  954. The failure code will be #OP_EFAULT if the file could not
  955. be opened, or one of the other failure codes from
  956. op_open_callbacks() otherwise.
  957. \return A freshly opened \c OggOpusFile, or <code>NULL</code> on error.*/
  958. OP_WARN_UNUSED_RESULT OggOpusFile *op_open_file(const char *_path,int *_error)
  959. OP_ARG_NONNULL(1);
  960. /**Open a stream from a memory buffer.
  961. \param _data The memory buffer to open.
  962. \param _size The number of bytes in the buffer.
  963. \param[out] _error Returns 0 on success, or a failure code on error.
  964. You may pass in <code>NULL</code> if you don't want the
  965. failure code.
  966. See op_open_callbacks() for a full list of failure codes.
  967. \return A freshly opened \c OggOpusFile, or <code>NULL</code> on error.*/
  968. OP_WARN_UNUSED_RESULT OggOpusFile *op_open_memory(const unsigned char *_data,
  969. size_t _size,int *_error);
  970. /**Open a stream from a URL.
  971. This function behaves identically to op_open_url(), except that it
  972. takes a va_list instead of a variable number of arguments.
  973. It does not call the <code>va_end</code> macro, and because it invokes the
  974. <code>va_arg</code> macro, the value of \a _ap is undefined after the call.
  975. \note If you use this function, you must link against <tt>libopusurl</tt>.
  976. \param _url The URL to open.
  977. Currently only the <file:>, <http:>, and <https:>
  978. schemes are supported.
  979. Both <http:> and <https:> may be disabled at compile
  980. time, in which case opening such URLs will always
  981. fail.
  982. Currently this only supports URIs.
  983. IRIs should be converted to UTF-8 and URL-escaped,
  984. with internationalized domain names encoded in
  985. punycode, before passing them to this function.
  986. \param[out] _error Returns 0 on success, or a failure code on error.
  987. You may pass in <code>NULL</code> if you don't want
  988. the failure code.
  989. See op_open_callbacks() for a full list of failure
  990. codes.
  991. \param[in,out] _ap A list of the \ref url_options "optional flags" to
  992. use.
  993. This is a variable-length list of options terminated
  994. with <code>NULL</code>.
  995. \return A freshly opened \c OggOpusFile, or <code>NULL</code> on error.*/
  996. OP_WARN_UNUSED_RESULT OggOpusFile *op_vopen_url(const char *_url,
  997. int *_error,va_list _ap) OP_ARG_NONNULL(1);
  998. /**Open a stream from a URL.
  999. \note If you use this function, you must link against <tt>libopusurl</tt>.
  1000. \param _url The URL to open.
  1001. Currently only the <file:>, <http:>, and <https:> schemes
  1002. are supported.
  1003. Both <http:> and <https:> may be disabled at compile
  1004. time, in which case opening such URLs will always fail.
  1005. Currently this only supports URIs.
  1006. IRIs should be converted to UTF-8 and URL-escaped, with
  1007. internationalized domain names encoded in punycode,
  1008. before passing them to this function.
  1009. \param[out] _error Returns 0 on success, or a failure code on error.
  1010. You may pass in <code>NULL</code> if you don't want the
  1011. failure code.
  1012. See op_open_callbacks() for a full list of failure codes.
  1013. \param ... The \ref url_options "optional flags" to use.
  1014. This is a variable-length list of options terminated with
  1015. <code>NULL</code>.
  1016. \return A freshly opened \c OggOpusFile, or <code>NULL</code> on error.*/
  1017. OP_WARN_UNUSED_RESULT OggOpusFile *op_open_url(const char *_url,
  1018. int *_error,...) OP_ARG_NONNULL(1);
  1019. /**Open a stream using the given set of callbacks to access it.
  1020. \param _source The stream to read from (e.g., a <code>FILE *</code>).
  1021. \param _cb The callbacks with which to access the stream.
  1022. <code><a href="#op_read_func">read()</a></code> must
  1023. be implemented.
  1024. <code><a href="#op_seek_func">seek()</a></code> and
  1025. <code><a href="#op_tell_func">tell()</a></code> may
  1026. be <code>NULL</code>, or may always return -1 to
  1027. indicate a source is unseekable, but if
  1028. <code><a href="#op_seek_func">seek()</a></code> is
  1029. implemented and succeeds on a particular source, then
  1030. <code><a href="#op_tell_func">tell()</a></code> must
  1031. also.
  1032. <code><a href="#op_close_func">close()</a></code> may
  1033. be <code>NULL</code>, but if it is not, it will be
  1034. called when the \c OggOpusFile is destroyed by
  1035. op_free().
  1036. It will not be called if op_open_callbacks() fails
  1037. with an error.
  1038. \param _initial_data An initial buffer of data from the start of the
  1039. stream.
  1040. Applications can read some number of bytes from the
  1041. start of the stream to help identify this as an Opus
  1042. stream, and then provide them here to allow the
  1043. stream to be opened, even if it is unseekable.
  1044. \param _initial_bytes The number of bytes in \a _initial_data.
  1045. If the stream is seekable, its current position (as
  1046. reported by
  1047. <code><a href="#opus_tell_func">tell()</a></code>
  1048. at the start of this function) must be equal to
  1049. \a _initial_bytes.
  1050. Otherwise, seeking to absolute positions will
  1051. generate inconsistent results.
  1052. \param[out] _error Returns 0 on success, or a failure code on error.
  1053. You may pass in <code>NULL</code> if you don't want
  1054. the failure code.
  1055. The failure code will be one of
  1056. <dl>
  1057. <dt>#OP_EREAD</dt>
  1058. <dd>An underlying read, seek, or tell operation
  1059. failed when it should have succeeded, or we failed
  1060. to find data in the stream we had seen before.</dd>
  1061. <dt>#OP_EFAULT</dt>
  1062. <dd>There was a memory allocation failure, or an
  1063. internal library error.</dd>
  1064. <dt>#OP_EIMPL</dt>
  1065. <dd>The stream used a feature that is not
  1066. implemented, such as an unsupported channel
  1067. family.</dd>
  1068. <dt>#OP_EINVAL</dt>
  1069. <dd><code><a href="#op_seek_func">seek()</a></code>
  1070. was implemented and succeeded on this source, but
  1071. <code><a href="#op_tell_func">tell()</a></code>
  1072. did not, or the starting position indicator was
  1073. not equal to \a _initial_bytes.</dd>
  1074. <dt>#OP_ENOTFORMAT</dt>
  1075. <dd>The stream contained a link that did not have
  1076. any logical Opus streams in it.</dd>
  1077. <dt>#OP_EBADHEADER</dt>
  1078. <dd>A required header packet was not properly
  1079. formatted, contained illegal values, or was missing
  1080. altogether.</dd>
  1081. <dt>#OP_EVERSION</dt>
  1082. <dd>An ID header contained an unrecognized version
  1083. number.</dd>
  1084. <dt>#OP_EBADLINK</dt>
  1085. <dd>We failed to find data we had seen before after
  1086. seeking.</dd>
  1087. <dt>#OP_EBADTIMESTAMP</dt>
  1088. <dd>The first or last timestamp in a link failed
  1089. basic validity checks.</dd>
  1090. </dl>
  1091. \return A freshly opened \c OggOpusFile, or <code>NULL</code> on error.
  1092. <tt>libopusfile</tt> does <em>not</em> take ownership of the source
  1093. if the call fails.
  1094. The calling application is responsible for closing the source if
  1095. this call returns an error.*/
  1096. OP_WARN_UNUSED_RESULT OggOpusFile *op_open_callbacks(void *_source,
  1097. const OpusFileCallbacks *_cb,const unsigned char *_initial_data,
  1098. size_t _initial_bytes,int *_error) OP_ARG_NONNULL(2);
  1099. /**Partially open a stream from the given file path.
  1100. \see op_test_callbacks
  1101. \param _path The path to the file to open.
  1102. \param[out] _error Returns 0 on success, or a failure code on error.
  1103. You may pass in <code>NULL</code> if you don't want the
  1104. failure code.
  1105. The failure code will be #OP_EFAULT if the file could not
  1106. be opened, or one of the other failure codes from
  1107. op_open_callbacks() otherwise.
  1108. \return A partially opened \c OggOpusFile, or <code>NULL</code> on error.*/
  1109. OP_WARN_UNUSED_RESULT OggOpusFile *op_test_file(const char *_path,int *_error)
  1110. OP_ARG_NONNULL(1);
  1111. /**Partially open a stream from a memory buffer.
  1112. \see op_test_callbacks
  1113. \param _data The memory buffer to open.
  1114. \param _size The number of bytes in the buffer.
  1115. \param[out] _error Returns 0 on success, or a failure code on error.
  1116. You may pass in <code>NULL</code> if you don't want the
  1117. failure code.
  1118. See op_open_callbacks() for a full list of failure codes.
  1119. \return A partially opened \c OggOpusFile, or <code>NULL</code> on error.*/
  1120. OP_WARN_UNUSED_RESULT OggOpusFile *op_test_memory(const unsigned char *_data,
  1121. size_t _size,int *_error);
  1122. /**Partially open a stream from a URL.
  1123. This function behaves identically to op_test_url(), except that it
  1124. takes a va_list instead of a variable number of arguments.
  1125. It does not call the <code>va_end</code> macro, and because it invokes the
  1126. <code>va_arg</code> macro, the value of \a _ap is undefined after the call.
  1127. \note If you use this function, you must link against <tt>libopusurl</tt>.
  1128. \see op_test_url
  1129. \see op_test_callbacks
  1130. \param _url The URL to open.
  1131. Currently only the <file:>, <http:>, and <https:>
  1132. schemes are supported.
  1133. Both <http:> and <https:> may be disabled at compile
  1134. time, in which case opening such URLs will always
  1135. fail.
  1136. Currently this only supports URIs.
  1137. IRIs should be converted to UTF-8 and URL-escaped,
  1138. with internationalized domain names encoded in
  1139. punycode, before passing them to this function.
  1140. \param[out] _error Returns 0 on success, or a failure code on error.
  1141. You may pass in <code>NULL</code> if you don't want
  1142. the failure code.
  1143. See op_open_callbacks() for a full list of failure
  1144. codes.
  1145. \param[in,out] _ap A list of the \ref url_options "optional flags" to
  1146. use.
  1147. This is a variable-length list of options terminated
  1148. with <code>NULL</code>.
  1149. \return A partially opened \c OggOpusFile, or <code>NULL</code> on error.*/
  1150. OP_WARN_UNUSED_RESULT OggOpusFile *op_vtest_url(const char *_url,
  1151. int *_error,va_list _ap) OP_ARG_NONNULL(1);
  1152. /**Partially open a stream from a URL.
  1153. \note If you use this function, you must link against <tt>libopusurl</tt>.
  1154. \see op_test_callbacks
  1155. \param _url The URL to open.
  1156. Currently only the <file:>, <http:>, and <https:>
  1157. schemes are supported.
  1158. Both <http:> and <https:> may be disabled at compile
  1159. time, in which case opening such URLs will always fail.
  1160. Currently this only supports URIs.
  1161. IRIs should be converted to UTF-8 and URL-escaped, with
  1162. internationalized domain names encoded in punycode,
  1163. before passing them to this function.
  1164. \param[out] _error Returns 0 on success, or a failure code on error.
  1165. You may pass in <code>NULL</code> if you don't want the
  1166. failure code.
  1167. See op_open_callbacks() for a full list of failure
  1168. codes.
  1169. \param ... The \ref url_options "optional flags" to use.
  1170. This is a variable-length list of options terminated
  1171. with <code>NULL</code>.
  1172. \return A partially opened \c OggOpusFile, or <code>NULL</code> on error.*/
  1173. OP_WARN_UNUSED_RESULT OggOpusFile *op_test_url(const char *_url,
  1174. int *_error,...) OP_ARG_NONNULL(1);
  1175. /**Partially open a stream using the given set of callbacks to access it.
  1176. This tests for Opusness and loads the headers for the first link.
  1177. It does not seek (although it tests for seekability).
  1178. You can query a partially open stream for the few pieces of basic
  1179. information returned by op_serialno(), op_channel_count(), op_head(), and
  1180. op_tags() (but only for the first link).
  1181. You may also determine if it is seekable via a call to op_seekable().
  1182. You cannot read audio from the stream, seek, get the size or duration,
  1183. get information from links other than the first one, or even get the total
  1184. number of links until you finish opening the stream with op_test_open().
  1185. If you do not need to do any of these things, you can dispose of it with
  1186. op_free() instead.
  1187. This function is provided mostly to simplify porting existing code that used
  1188. <tt>libvorbisfile</tt>.
  1189. For new code, you are likely better off using op_test() instead, which
  1190. is less resource-intensive, requires less data to succeed, and imposes a
  1191. hard limit on the amount of data it examines (important for unseekable
  1192. sources, where all such data must be buffered until you are sure of the
  1193. stream type).
  1194. \param _source The stream to read from (e.g., a <code>FILE *</code>).
  1195. \param _cb The callbacks with which to access the stream.
  1196. <code><a href="#op_read_func">read()</a></code> must
  1197. be implemented.
  1198. <code><a href="#op_seek_func">seek()</a></code> and
  1199. <code><a href="#op_tell_func">tell()</a></code> may
  1200. be <code>NULL</code>, or may always return -1 to
  1201. indicate a source is unseekable, but if
  1202. <code><a href="#op_seek_func">seek()</a></code> is
  1203. implemented and succeeds on a particular source, then
  1204. <code><a href="#op_tell_func">tell()</a></code> must
  1205. also.
  1206. <code><a href="#op_close_func">close()</a></code> may
  1207. be <code>NULL</code>, but if it is not, it will be
  1208. called when the \c OggOpusFile is destroyed by
  1209. op_free().
  1210. It will not be called if op_open_callbacks() fails
  1211. with an error.
  1212. \param _initial_data An initial buffer of data from the start of the
  1213. stream.
  1214. Applications can read some number of bytes from the
  1215. start of the stream to help identify this as an Opus
  1216. stream, and then provide them here to allow the
  1217. stream to be tested more thoroughly, even if it is
  1218. unseekable.
  1219. \param _initial_bytes The number of bytes in \a _initial_data.
  1220. If the stream is seekable, its current position (as
  1221. reported by
  1222. <code><a href="#opus_tell_func">tell()</a></code>
  1223. at the start of this function) must be equal to
  1224. \a _initial_bytes.
  1225. Otherwise, seeking to absolute positions will
  1226. generate inconsistent results.
  1227. \param[out] _error Returns 0 on success, or a failure code on error.
  1228. You may pass in <code>NULL</code> if you don't want
  1229. the failure code.
  1230. See op_open_callbacks() for a full list of failure
  1231. codes.
  1232. \return A partially opened \c OggOpusFile, or <code>NULL</code> on error.
  1233. <tt>libopusfile</tt> does <em>not</em> take ownership of the source
  1234. if the call fails.
  1235. The calling application is responsible for closing the source if
  1236. this call returns an error.*/
  1237. OP_WARN_UNUSED_RESULT OggOpusFile *op_test_callbacks(void *_source,
  1238. const OpusFileCallbacks *_cb,const unsigned char *_initial_data,
  1239. size_t _initial_bytes,int *_error) OP_ARG_NONNULL(2);
  1240. /**Finish opening a stream partially opened with op_test_callbacks() or one of
  1241. the associated convenience functions.
  1242. If this function fails, you are still responsible for freeing the
  1243. \c OggOpusFile with op_free().
  1244. \param _of The \c OggOpusFile to finish opening.
  1245. \return 0 on success, or a negative value on error.
  1246. \retval #OP_EREAD An underlying read, seek, or tell operation failed
  1247. when it should have succeeded.
  1248. \retval #OP_EFAULT There was a memory allocation failure, or an
  1249. internal library error.
  1250. \retval #OP_EIMPL The stream used a feature that is not implemented,
  1251. such as an unsupported channel family.
  1252. \retval #OP_EINVAL The stream was not partially opened with
  1253. op_test_callbacks() or one of the associated
  1254. convenience functions.
  1255. \retval #OP_ENOTFORMAT The stream contained a link that did not have any
  1256. logical Opus streams in it.
  1257. \retval #OP_EBADHEADER A required header packet was not properly
  1258. formatted, contained illegal values, or was
  1259. missing altogether.
  1260. \retval #OP_EVERSION An ID header contained an unrecognized version
  1261. number.
  1262. \retval #OP_EBADLINK We failed to find data we had seen before after
  1263. seeking.
  1264. \retval #OP_EBADTIMESTAMP The first or last timestamp in a link failed basic
  1265. validity checks.*/
  1266. int op_test_open(OggOpusFile *_of) OP_ARG_NONNULL(1);
  1267. /**Release all memory used by an \c OggOpusFile.
  1268. \param _of The \c OggOpusFile to free.*/
  1269. void op_free(OggOpusFile *_of);
  1270. /*@}*/
  1271. /*@}*/
  1272. /**\defgroup stream_info Stream Information*/
  1273. /*@{*/
  1274. /**\name Functions for obtaining information about streams
  1275. These functions allow you to get basic information about a stream, including
  1276. seekability, the number of links (for chained streams), plus the size,
  1277. duration, bitrate, header parameters, and meta information for each link
  1278. (or, where available, the stream as a whole).
  1279. Some of these (size, duration) are only available for seekable streams.
  1280. You can also query the current stream position, link, and playback time,
  1281. and instantaneous bitrate during playback.
  1282. Some of these functions may be used successfully on the partially open
  1283. streams returned by op_test_callbacks() or one of the associated
  1284. convenience functions.
  1285. Their documention will indicate so explicitly.*/
  1286. /*@{*/
  1287. /**Returns whether or not the data source being read is seekable.
  1288. This is true if
  1289. <ol>
  1290. <li>The <code><a href="#op_seek_func">seek()</a></code> and
  1291. <code><a href="#op_tell_func">tell()</a></code> callbacks are both
  1292. non-<code>NULL</code>,</li>
  1293. <li>The <code><a href="#op_seek_func">seek()</a></code> callback was
  1294. successfully executed at least once, and</li>
  1295. <li>The <code><a href="#op_tell_func">tell()</a></code> callback was
  1296. successfully able to report the position indicator afterwards.</li>
  1297. </ol>
  1298. This function may be called on partially-opened streams.
  1299. \param _of The \c OggOpusFile whose seekable status is to be returned.
  1300. \return A non-zero value if seekable, and 0 if unseekable.*/
  1301. int op_seekable(const OggOpusFile *_of) OP_ARG_NONNULL(1);
  1302. /**Returns the number of links in this chained stream.
  1303. This function may be called on partially-opened streams, but it will always
  1304. return 1.
  1305. The actual number of links is not known until the stream is fully opened.
  1306. \param _of The \c OggOpusFile from which to retrieve the link count.
  1307. \return For fully-open seekable sources, this returns the total number of
  1308. links in the whole stream, which will be at least 1.
  1309. For partially-open or unseekable sources, this always returns 1.*/
  1310. int op_link_count(const OggOpusFile *_of) OP_ARG_NONNULL(1);
  1311. /**Get the serial number of the given link in a (possibly-chained) Ogg Opus
  1312. stream.
  1313. This function may be called on partially-opened streams, but it will always
  1314. return the serial number of the Opus stream in the first link.
  1315. \param _of The \c OggOpusFile from which to retrieve the serial number.
  1316. \param _li The index of the link whose serial number should be retrieved.
  1317. Use a negative number to get the serial number of the current
  1318. link.
  1319. \return The serial number of the given link.
  1320. If \a _li is greater than the total number of links, this returns
  1321. the serial number of the last link.
  1322. If the source is not seekable, this always returns the serial number
  1323. of the current link.*/
  1324. opus_uint32 op_serialno(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
  1325. /**Get the channel count of the given link in a (possibly-chained) Ogg Opus
  1326. stream.
  1327. This is equivalent to <code>op_head(_of,_li)->channel_count</code>, but
  1328. is provided for convenience.
  1329. This function may be called on partially-opened streams, but it will always
  1330. return the channel count of the Opus stream in the first link.
  1331. \param _of The \c OggOpusFile from which to retrieve the channel count.
  1332. \param _li The index of the link whose channel count should be retrieved.
  1333. Use a negative number to get the channel count of the current
  1334. link.
  1335. \return The channel count of the given link.
  1336. If \a _li is greater than the total number of links, this returns
  1337. the channel count of the last link.
  1338. If the source is not seekable, this always returns the channel count
  1339. of the current link.*/
  1340. int op_channel_count(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
  1341. /**Get the total (compressed) size of the stream, or of an individual link in
  1342. a (possibly-chained) Ogg Opus stream, including all headers and Ogg muxing
  1343. overhead.
  1344. \param _of The \c OggOpusFile from which to retrieve the compressed size.
  1345. \param _li The index of the link whose compressed size should be computed.
  1346. Use a negative number to get the compressed size of the entire
  1347. stream.
  1348. \return The compressed size of the entire stream if \a _li is negative, the
  1349. compressed size of link \a _li if it is non-negative, or a negative
  1350. value on error.
  1351. The compressed size of the entire stream may be smaller than that
  1352. of the underlying source if trailing garbage was detected in the
  1353. file.
  1354. \retval #OP_EINVAL The source is not seekable (so we can't know the length),
  1355. \a _li wasn't less than the total number of links in
  1356. the stream, or the stream was only partially open.*/
  1357. opus_int64 op_raw_total(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
  1358. /**Get the total PCM length (number of samples at 48 kHz) of the stream, or of
  1359. an individual link in a (possibly-chained) Ogg Opus stream.
  1360. Users looking for <code>op_time_total()</code> should use op_pcm_total()
  1361. instead.
  1362. Because timestamps in Opus are fixed at 48 kHz, there is no need for a
  1363. separate function to convert this to seconds (and leaving it out avoids
  1364. introducing floating point to the API, for those that wish to avoid it).
  1365. \param _of The \c OggOpusFile from which to retrieve the PCM offset.
  1366. \param _li The index of the link whose PCM length should be computed.
  1367. Use a negative number to get the PCM length of the entire stream.
  1368. \return The PCM length of the entire stream if \a _li is negative, the PCM
  1369. length of link \a _li if it is non-negative, or a negative value on
  1370. error.
  1371. \retval #OP_EINVAL The source is not seekable (so we can't know the length),
  1372. \a _li wasn't less than the total number of links in
  1373. the stream, or the stream was only partially open.*/
  1374. ogg_int64_t op_pcm_total(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
  1375. /**Get the ID header information for the given link in a (possibly chained) Ogg
  1376. Opus stream.
  1377. This function may be called on partially-opened streams, but it will always
  1378. return the ID header information of the Opus stream in the first link.
  1379. \param _of The \c OggOpusFile from which to retrieve the ID header
  1380. information.
  1381. \param _li The index of the link whose ID header information should be
  1382. retrieved.
  1383. Use a negative number to get the ID header information of the
  1384. current link.
  1385. For an unseekable stream, \a _li is ignored, and the ID header
  1386. information for the current link is always returned, if
  1387. available.
  1388. \return The contents of the ID header for the given link.*/
  1389. const OpusHead *op_head(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
  1390. /**Get the comment header information for the given link in a (possibly
  1391. chained) Ogg Opus stream.
  1392. This function may be called on partially-opened streams, but it will always
  1393. return the tags from the Opus stream in the first link.
  1394. \param _of The \c OggOpusFile from which to retrieve the comment header
  1395. information.
  1396. \param _li The index of the link whose comment header information should be
  1397. retrieved.
  1398. Use a negative number to get the comment header information of
  1399. the current link.
  1400. For an unseekable stream, \a _li is ignored, and the comment
  1401. header information for the current link is always returned, if
  1402. available.
  1403. \return The contents of the comment header for the given link, or
  1404. <code>NULL</code> if this is an unseekable stream that encountered
  1405. an invalid link.*/
  1406. const OpusTags *op_tags(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
  1407. /**Retrieve the index of the current link.
  1408. This is the link that produced the data most recently read by
  1409. op_read_float() or its associated functions, or, after a seek, the link
  1410. that the seek target landed in.
  1411. Reading more data may advance the link index (even on the first read after a
  1412. seek).
  1413. \param _of The \c OggOpusFile from which to retrieve the current link index.
  1414. \return The index of the current link on success, or a negative value on
  1415. failure.
  1416. For seekable streams, this is a number between 0 and the value
  1417. returned by op_link_count().
  1418. For unseekable streams, this value starts at 0 and increments by one
  1419. each time a new link is encountered (even though op_link_count()
  1420. always returns 1).
  1421. \retval #OP_EINVAL The stream was only partially open.*/
  1422. int op_current_link(const OggOpusFile *_of) OP_ARG_NONNULL(1);
  1423. /**Computes the bitrate for a given link in a (possibly chained) Ogg Opus
  1424. stream.
  1425. The stream must be seekable to compute the bitrate.
  1426. For unseekable streams, use op_bitrate_instant() to get periodic estimates.
  1427. \param _of The \c OggOpusFile from which to retrieve the bitrate.
  1428. \param _li The index of the link whose bitrate should be computed.
  1429. USe a negative number to get the bitrate of the whole stream.
  1430. \return The bitrate on success, or a negative value on error.
  1431. \retval #OP_EINVAL The stream was only partially open, the stream was not
  1432. seekable, or \a _li was larger than the number of
  1433. links.*/
  1434. opus_int32 op_bitrate(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1);
  1435. /**Compute the instantaneous bitrate, measured as the ratio of bits to playable
  1436. samples decoded since a) the last call to op_bitrate_instant(), b) the last
  1437. seek, or c) the start of playback, whichever was most recent.
  1438. This will spike somewhat after a seek or at the start/end of a chain
  1439. boundary, as pre-skip, pre-roll, and end-trimming causes samples to be
  1440. decoded but not played.
  1441. \param _of The \c OggOpusFile from which to retrieve the bitrate.
  1442. \return The bitrate, in bits per second, or a negative value on error.
  1443. \retval #OP_FALSE No data has been decoded since any of the events
  1444. described above.
  1445. \retval #OP_EINVAL The stream was only partially open.*/
  1446. opus_int32 op_bitrate_instant(OggOpusFile *_of) OP_ARG_NONNULL(1);
  1447. /**Obtain the current value of the position indicator for \a _of.
  1448. \param _of The \c OggOpusFile from which to retrieve the position indicator.
  1449. \return The byte position that is currently being read from.
  1450. \retval #OP_EINVAL The stream was only partially open.*/
  1451. opus_int64 op_raw_tell(const OggOpusFile *_of) OP_ARG_NONNULL(1);
  1452. /**Obtain the PCM offset of the next sample to be read.
  1453. If the stream is not properly timestamped, this might not increment by the
  1454. proper amount between reads, or even return monotonically increasing
  1455. values.
  1456. \param _of The \c OggOpusFile from which to retrieve the PCM offset.
  1457. \return The PCM offset of the next sample to be read.
  1458. \retval #OP_EINVAL The stream was only partially open.*/
  1459. ogg_int64_t op_pcm_tell(const OggOpusFile *_of) OP_ARG_NONNULL(1);
  1460. /*@}*/
  1461. /*@}*/
  1462. /**\defgroup stream_seeking Seeking*/
  1463. /*@{*/
  1464. /**\name Functions for seeking in Opus streams
  1465. These functions let you seek in Opus streams, if the underlying source
  1466. support it.
  1467. Seeking is implemented for all built-in stream I/O routines, though some
  1468. individual sources may not be seekable (pipes, live HTTP streams, or HTTP
  1469. streams from a server that does not support <code>Range</code> requests).
  1470. op_raw_seek() is the fastest: it is guaranteed to perform at most one
  1471. physical seek, but, since the target is a byte position, makes no guarantee
  1472. how close to a given time it will come.
  1473. op_pcm_seek() provides sample-accurate seeking.
  1474. The number of physical seeks it requires is still quite small (often 1 or
  1475. 2, even in highly variable bitrate streams).
  1476. Seeking in Opus requires decoding some pre-roll amount before playback to
  1477. allow the internal state to converge (as if recovering from packet loss).
  1478. This is handled internally by <tt>libopusfile</tt>, but means there is
  1479. little extra overhead for decoding up to the exact position requested
  1480. (since it must decode some amount of audio anyway).
  1481. It also means that decoding after seeking may not return exactly the same
  1482. values as would be obtained by decoding the stream straight through.
  1483. However, such differences are expected to be smaller than the loss
  1484. introduced by Opus's lossy compression.*/
  1485. /*@{*/
  1486. /**Seek to a byte offset relative to the <b>compressed</b> data.
  1487. This also scans packets to update the PCM cursor.
  1488. It will cross a logical bitstream boundary, but only if it can't get any
  1489. packets out of the tail of the link to which it seeks.
  1490. \param _of The \c OggOpusFile in which to seek.
  1491. \param _byte_offset The byte position to seek to.
  1492. \return 0 on success, or a negative error code on failure.
  1493. \retval #OP_EREAD The underlying seek operation failed.
  1494. \retval #OP_EINVAL The stream was only partially open, or the target was
  1495. outside the valid range for the stream.
  1496. \retval #OP_ENOSEEK This stream is not seekable.
  1497. \retval #OP_EBADLINK Failed to initialize a decoder for a stream for an
  1498. unknown reason.*/
  1499. int op_raw_seek(OggOpusFile *_of,opus_int64 _byte_offset) OP_ARG_NONNULL(1);
  1500. /**Seek to the specified PCM offset, such that decoding will begin at exactly
  1501. the requested position.
  1502. \param _of The \c OggOpusFile in which to seek.
  1503. \param _pcm_offset The PCM offset to seek to.
  1504. This is in samples at 48 kHz relative to the start of the
  1505. stream.
  1506. \return 0 on success, or a negative value on error.
  1507. \retval #OP_EREAD An underlying read or seek operation failed.
  1508. \retval #OP_EINVAL The stream was only partially open, or the target was
  1509. outside the valid range for the stream.
  1510. \retval #OP_ENOSEEK This stream is not seekable.
  1511. \retval #OP_EBADLINK We failed to find data we had seen before, or the
  1512. bitstream structure was sufficiently malformed that
  1513. seeking to the target destination was impossible.*/
  1514. int op_pcm_seek(OggOpusFile *_of,ogg_int64_t _pcm_offset) OP_ARG_NONNULL(1);
  1515. /*@}*/
  1516. /*@}*/
  1517. /**\defgroup stream_decoding Decoding*/
  1518. /*@{*/
  1519. /**\name Functions for decoding audio data
  1520. These functions retrieve actual decoded audio data from the stream.
  1521. The general functions, op_read() and op_read_float() return 16-bit or
  1522. floating-point output, both using native endian ordering.
  1523. The number of channels returned can change from link to link in a chained
  1524. stream.
  1525. There are special functions, op_read_stereo() and op_read_float_stereo(),
  1526. which always output two channels, to simplify applications which do not
  1527. wish to handle multichannel audio.
  1528. These downmix multichannel files to two channels, so they can always return
  1529. samples in the same format for every link in a chained file.
  1530. If the rest of your audio processing chain can handle floating point, those
  1531. routines should be preferred, as floating point output avoids introducing
  1532. clipping and other issues which might be avoided entirely if, e.g., you
  1533. scale down the volume at some other stage.
  1534. However, if you intend to direct consume 16-bit samples, the conversion in
  1535. <tt>libopusfile</tt> provides noise-shaping dithering and, if compiled
  1536. against <tt>libopus</tt>&nbsp;1.1 or later, soft-clipping prevention.
  1537. <tt>libopusfile</tt> can also be configured at compile time to use the
  1538. fixed-point <tt>libopus</tt> API.
  1539. If so, <tt>libopusfile</tt>'s floating-point API may also be disabled.
  1540. In that configuration, nothing in <tt>libopusfile</tt> will use any
  1541. floating-point operations, to simplify support on devices without an
  1542. adequate FPU.
  1543. \warning HTTPS streams may be be vulnerable to truncation attacks if you do
  1544. not check the error return code from op_read_float() or its associated
  1545. functions.
  1546. If the remote peer does not close the connection gracefully (with a TLS
  1547. "close notify" message), these functions will return #OP_EREAD instead of 0
  1548. when they reach the end of the file.
  1549. If you are reading from an <https:> URL (particularly if seeking is not
  1550. supported), you should make sure to check for this error and warn the user
  1551. appropriately.*/
  1552. /*@{*/
  1553. /**Indicates that the decoding callback should produce signed 16-bit
  1554. native-endian output samples.*/
  1555. #define OP_DEC_FORMAT_SHORT (7008)
  1556. /**Indicates that the decoding callback should produce 32-bit native-endian
  1557. float samples.*/
  1558. #define OP_DEC_FORMAT_FLOAT (7040)
  1559. /**Indicates that the decoding callback did not decode anything, and that
  1560. <tt>libopusfile</tt> should decode normally instead.*/
  1561. #define OP_DEC_USE_DEFAULT (6720)
  1562. /**Called to decode an Opus packet.
  1563. This should invoke the functional equivalent of opus_multistream_decode() or
  1564. opus_multistream_decode_float(), except that it returns 0 on success
  1565. instead of the number of decoded samples (which is known a priori).
  1566. \param _ctx The application-provided callback context.
  1567. \param _decoder The decoder to use to decode the packet.
  1568. \param[out] _pcm The buffer to decode into.
  1569. This will always have enough room for \a _nchannels of
  1570. \a _nsamples samples, which should be placed into this
  1571. buffer interleaved.
  1572. \param _op The packet to decode.
  1573. This will always have its granule position set to a valid
  1574. value.
  1575. \param _nsamples The number of samples expected from the packet.
  1576. \param _nchannels The number of channels expected from the packet.
  1577. \param _format The desired sample output format.
  1578. This is either #OP_DEC_FORMAT_SHORT or
  1579. #OP_DEC_FORMAT_FLOAT.
  1580. \param _li The index of the link from which this packet was decoded.
  1581. \return A non-negative value on success, or a negative value on error.
  1582. The error codes should be the same as those returned by
  1583. opus_multistream_decode() or opus_multistream_decode_float().
  1584. \retval 0 Decoding was successful.
  1585. The application has filled the buffer with
  1586. exactly <code>\a _nsamples*\a
  1587. _nchannels</code> samples in the requested
  1588. format.
  1589. \retval #OP_DEC_USE_DEFAULT No decoding was done.
  1590. <tt>libopusfile</tt> should decode normally
  1591. instead.*/
  1592. typedef int (*op_decode_cb_func)(void *_ctx,OpusMSDecoder *_decoder,void *_pcm,
  1593. const ogg_packet *_op,int _nsamples,int _nchannels,int _format,int _li);
  1594. /**Sets the packet decode callback function.
  1595. This is called once for each packet that needs to be decoded.
  1596. A call to this function is no guarantee that the audio will eventually be
  1597. delivered to the application.
  1598. Some or all of the data from the packet may be discarded (i.e., at the
  1599. beginning or end of a link, or after a seek), however the callback is
  1600. required to provide all of it.
  1601. \param _of The \c OggOpusFile on which to set the decode callback.
  1602. \param _decode_cb The callback function to call.
  1603. This may be <code>NULL</code> to disable calling the
  1604. callback.
  1605. \param _ctx The application-provided context pointer to pass to the
  1606. callback on each call.*/
  1607. void op_set_decode_callback(OggOpusFile *_of,
  1608. op_decode_cb_func _decode_cb,void *_ctx) OP_ARG_NONNULL(1);
  1609. /**Gain offset type that indicates that the provided offset is relative to the
  1610. header gain.
  1611. This is the default.*/
  1612. #define OP_HEADER_GAIN (0)
  1613. /**Gain offset type that indicates that the provided offset is relative to the
  1614. R128_TRACK_GAIN value (if any), in addition to the header gain.*/
  1615. #define OP_TRACK_GAIN (3008)
  1616. /**Gain offset type that indicates that the provided offset should be used as
  1617. the gain directly, without applying any the header or track gains.*/
  1618. #define OP_ABSOLUTE_GAIN (3009)
  1619. /**Sets the gain to be used for decoded output.
  1620. By default, the gain in the header is applied with no additional offset.
  1621. The total gain (including header gain and/or track gain, if applicable, and
  1622. this offset), will be clamped to [-32768,32767]/256 dB.
  1623. This is more than enough to saturate or underflow 16-bit PCM.
  1624. \note The new gain will not be applied to any already buffered, decoded
  1625. output.
  1626. This means you cannot change it sample-by-sample, as at best it will be
  1627. updated packet-by-packet.
  1628. It is meant for setting a target volume level, rather than applying smooth
  1629. fades, etc.
  1630. \param _of The \c OggOpusFile on which to set the gain offset.
  1631. \param _gain_type One of #OP_HEADER_GAIN, #OP_TRACK_GAIN, or
  1632. #OP_ABSOLUTE_GAIN.
  1633. \param _gain_offset_q8 The gain offset to apply, in 1/256ths of a dB.
  1634. \return 0 on success or a negative value on error.
  1635. \retval #OP_EINVAL The \a _gain_type was unrecognized.*/
  1636. int op_set_gain_offset(OggOpusFile *_of,
  1637. int _gain_type,opus_int32 _gain_offset_q8) OP_ARG_NONNULL(1);
  1638. /**Sets whether or not dithering is enabled for 16-bit decoding.
  1639. By default, when <tt>libopusfile</tt> is compiled to use floating-point
  1640. internally, calling op_read() or op_read_stereo() will first decode to
  1641. float, and then convert to fixed-point using noise-shaping dithering.
  1642. This flag can be used to disable that dithering.
  1643. When the application uses op_read_float() or op_read_float_stereo(), or when
  1644. the library has been compiled to decode directly to fixed point, this flag
  1645. has no effect.
  1646. \param _of The \c OggOpusFile on which to enable or disable dithering.
  1647. \param _enabled A non-zero value to enable dithering, or 0 to disable it.*/
  1648. void op_set_dither_enabled(OggOpusFile *_of,int _enabled) OP_ARG_NONNULL(1);
  1649. /**Reads more samples from the stream.
  1650. \note Although \a _buf_size must indicate the total number of values that
  1651. can be stored in \a _pcm, the return value is the number of samples
  1652. <em>per channel</em>.
  1653. This is done because
  1654. <ol>
  1655. <li>The channel count cannot be known a priori (reading more samples might
  1656. advance us into the next link, with a different channel count), so
  1657. \a _buf_size cannot also be in units of samples per channel,</li>
  1658. <li>Returning the samples per channel matches the <code>libopus</code> API
  1659. as closely as we're able,</li>
  1660. <li>Returning the total number of values instead of samples per channel
  1661. would mean the caller would need a division to compute the samples per
  1662. channel, and might worry about the possibility of getting back samples
  1663. for some channels and not others, and</li>
  1664. <li>This approach is relatively fool-proof: if an application passes too
  1665. small a value to \a _buf_size, they will simply get fewer samples back,
  1666. and if they assume the return value is the total number of values, then
  1667. they will simply read too few (rather than reading too many and going
  1668. off the end of the buffer).</li>
  1669. </ol>
  1670. \param _of The \c OggOpusFile from which to read.
  1671. \param[out] _pcm A buffer in which to store the output PCM samples, as
  1672. signed native-endian 16-bit values at 48&nbsp;kHz
  1673. with a nominal range of <code>[-32768,32767)</code>.
  1674. Multiple channels are interleaved using the
  1675. <a href="http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9">Vorbis
  1676. channel ordering</a>.
  1677. This must have room for at least \a _buf_size values.
  1678. \param _buf_size The number of values that can be stored in \a _pcm.
  1679. It is recommended that this be large enough for at
  1680. least 120 ms of data at 48 kHz per channel (5760
  1681. values per channel).
  1682. Smaller buffers will simply return less data, possibly
  1683. consuming more memory to buffer the data internally.
  1684. <tt>libopusfile</tt> may return less data than
  1685. requested.
  1686. If so, there is no guarantee that the remaining data
  1687. in \a _pcm will be unmodified.
  1688. \param[out] _li The index of the link this data was decoded from.
  1689. You may pass <code>NULL</code> if you do not need this
  1690. information.
  1691. If this function fails (returning a negative value),
  1692. this parameter is left unset.
  1693. \return The number of samples read per channel on success, or a negative
  1694. value on failure.
  1695. The channel count can be retrieved on success by calling
  1696. <code>op_head(_of,*_li)</code>.
  1697. The number of samples returned may be 0 if the buffer was too small
  1698. to store even a single sample for all channels, or if end-of-file
  1699. was reached.
  1700. The list of possible failure codes follows.
  1701. Most of them can only be returned by unseekable, chained streams
  1702. that encounter a new link.
  1703. \retval #OP_HOLE There was a hole in the data, and some samples
  1704. may have been skipped.
  1705. Call this function again to continue decoding
  1706. past the hole.
  1707. \retval #OP_EREAD An underlying read operation failed.
  1708. This may signal a truncation attack from an
  1709. <https:> source.
  1710. \retval #OP_EFAULT An internal memory allocation failed.
  1711. \retval #OP_EIMPL An unseekable stream encountered a new link that
  1712. used a feature that is not implemented, such as
  1713. an unsupported channel family.
  1714. \retval #OP_EINVAL The stream was only partially open.
  1715. \retval #OP_ENOTFORMAT An unseekable stream encountered a new link that
  1716. did not have any logical Opus streams in it.
  1717. \retval #OP_EBADHEADER An unseekable stream encountered a new link with a
  1718. required header packet that was not properly
  1719. formatted, contained illegal values, or was
  1720. missing altogether.
  1721. \retval #OP_EVERSION An unseekable stream encountered a new link with
  1722. an ID header that contained an unrecognized
  1723. version number.
  1724. \retval #OP_EBADPACKET Failed to properly decode the next packet.
  1725. \retval #OP_EBADLINK We failed to find data we had seen before.
  1726. \retval #OP_EBADTIMESTAMP An unseekable stream encountered a new link with
  1727. a starting timestamp that failed basic validity
  1728. checks.*/
  1729. OP_WARN_UNUSED_RESULT int op_read(OggOpusFile *_of,
  1730. opus_int16 *_pcm,int _buf_size,int *_li) OP_ARG_NONNULL(1);
  1731. /**Reads more samples from the stream.
  1732. \note Although \a _buf_size must indicate the total number of values that
  1733. can be stored in \a _pcm, the return value is the number of samples
  1734. <em>per channel</em>.
  1735. <ol>
  1736. <li>The channel count cannot be known a priori (reading more samples might
  1737. advance us into the next link, with a different channel count), so
  1738. \a _buf_size cannot also be in units of samples per channel,</li>
  1739. <li>Returning the samples per channel matches the <code>libopus</code> API
  1740. as closely as we're able,</li>
  1741. <li>Returning the total number of values instead of samples per channel
  1742. would mean the caller would need a division to compute the samples per
  1743. channel, and might worry about the possibility of getting back samples
  1744. for some channels and not others, and</li>
  1745. <li>This approach is relatively fool-proof: if an application passes too
  1746. small a value to \a _buf_size, they will simply get fewer samples back,
  1747. and if they assume the return value is the total number of values, then
  1748. they will simply read too few (rather than reading too many and going
  1749. off the end of the buffer).</li>
  1750. </ol>
  1751. \param _of The \c OggOpusFile from which to read.
  1752. \param[out] _pcm A buffer in which to store the output PCM samples as
  1753. signed floats at 48&nbsp;kHz with a nominal range of
  1754. <code>[-1.0,1.0]</code>.
  1755. Multiple channels are interleaved using the
  1756. <a href="http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9">Vorbis
  1757. channel ordering</a>.
  1758. This must have room for at least \a _buf_size floats.
  1759. \param _buf_size The number of floats that can be stored in \a _pcm.
  1760. It is recommended that this be large enough for at
  1761. least 120 ms of data at 48 kHz per channel (5760
  1762. samples per channel).
  1763. Smaller buffers will simply return less data, possibly
  1764. consuming more memory to buffer the data internally.
  1765. If less than \a _buf_size values are returned,
  1766. <tt>libopusfile</tt> makes no guarantee that the
  1767. remaining data in \a _pcm will be unmodified.
  1768. \param[out] _li The index of the link this data was decoded from.
  1769. You may pass <code>NULL</code> if you do not need this
  1770. information.
  1771. If this function fails (returning a negative value),
  1772. this parameter is left unset.
  1773. \return The number of samples read per channel on success, or a negative
  1774. value on failure.
  1775. The channel count can be retrieved on success by calling
  1776. <code>op_head(_of,*_li)</code>.
  1777. The number of samples returned may be 0 if the buffer was too small
  1778. to store even a single sample for all channels, or if end-of-file
  1779. was reached.
  1780. The list of possible failure codes follows.
  1781. Most of them can only be returned by unseekable, chained streams
  1782. that encounter a new link.
  1783. \retval #OP_HOLE There was a hole in the data, and some samples
  1784. may have been skipped.
  1785. Call this function again to continue decoding
  1786. past the hole.
  1787. \retval #OP_EREAD An underlying read operation failed.
  1788. This may signal a truncation attack from an
  1789. <https:> source.
  1790. \retval #OP_EFAULT An internal memory allocation failed.
  1791. \retval #OP_EIMPL An unseekable stream encountered a new link that
  1792. used a feature that is not implemented, such as
  1793. an unsupported channel family.
  1794. \retval #OP_EINVAL The stream was only partially open.
  1795. \retval #OP_ENOTFORMAT An unseekable stream encountered a new link that
  1796. did not have any logical Opus streams in it.
  1797. \retval #OP_EBADHEADER An unseekable stream encountered a new link with a
  1798. required header packet that was not properly
  1799. formatted, contained illegal values, or was
  1800. missing altogether.
  1801. \retval #OP_EVERSION An unseekable stream encountered a new link with
  1802. an ID header that contained an unrecognized
  1803. version number.
  1804. \retval #OP_EBADPACKET Failed to properly decode the next packet.
  1805. \retval #OP_EBADLINK We failed to find data we had seen before.
  1806. \retval #OP_EBADTIMESTAMP An unseekable stream encountered a new link with
  1807. a starting timestamp that failed basic validity
  1808. checks.*/
  1809. OP_WARN_UNUSED_RESULT int op_read_float(OggOpusFile *_of,
  1810. float *_pcm,int _buf_size,int *_li) OP_ARG_NONNULL(1);
  1811. /**Reads more samples from the stream and downmixes to stereo, if necessary.
  1812. This function is intended for simple players that want a uniform output
  1813. format, even if the channel count changes between links in a chained
  1814. stream.
  1815. \note \a _buf_size indicates the total number of values that can be stored
  1816. in \a _pcm, while the return value is the number of samples <em>per
  1817. channel</em>, even though the channel count is known, for consistency with
  1818. op_read().
  1819. \param _of The \c OggOpusFile from which to read.
  1820. \param[out] _pcm A buffer in which to store the output PCM samples, as
  1821. signed native-endian 16-bit values at 48&nbsp;kHz
  1822. with a nominal range of <code>[-32768,32767)</code>.
  1823. The left and right channels are interleaved in the
  1824. buffer.
  1825. This must have room for at least \a _buf_size values.
  1826. \param _buf_size The number of values that can be stored in \a _pcm.
  1827. It is recommended that this be large enough for at
  1828. least 120 ms of data at 48 kHz per channel (11520
  1829. values total).
  1830. Smaller buffers will simply return less data, possibly
  1831. consuming more memory to buffer the data internally.
  1832. If less than \a _buf_size values are returned,
  1833. <tt>libopusfile</tt> makes no guarantee that the
  1834. remaining data in \a _pcm will be unmodified.
  1835. \return The number of samples read per channel on success, or a negative
  1836. value on failure.
  1837. The number of samples returned may be 0 if the buffer was too small
  1838. to store even a single sample for both channels, or if end-of-file
  1839. was reached.
  1840. The list of possible failure codes follows.
  1841. Most of them can only be returned by unseekable, chained streams
  1842. that encounter a new link.
  1843. \retval #OP_HOLE There was a hole in the data, and some samples
  1844. may have been skipped.
  1845. Call this function again to continue decoding
  1846. past the hole.
  1847. \retval #OP_EREAD An underlying read operation failed.
  1848. This may signal a truncation attack from an
  1849. <https:> source.
  1850. \retval #OP_EFAULT An internal memory allocation failed.
  1851. \retval #OP_EIMPL An unseekable stream encountered a new link that
  1852. used a feature that is not implemented, such as
  1853. an unsupported channel family.
  1854. \retval #OP_EINVAL The stream was only partially open.
  1855. \retval #OP_ENOTFORMAT An unseekable stream encountered a new link that
  1856. did not have any logical Opus streams in it.
  1857. \retval #OP_EBADHEADER An unseekable stream encountered a new link with a
  1858. required header packet that was not properly
  1859. formatted, contained illegal values, or was
  1860. missing altogether.
  1861. \retval #OP_EVERSION An unseekable stream encountered a new link with
  1862. an ID header that contained an unrecognized
  1863. version number.
  1864. \retval #OP_EBADPACKET Failed to properly decode the next packet.
  1865. \retval #OP_EBADLINK We failed to find data we had seen before.
  1866. \retval #OP_EBADTIMESTAMP An unseekable stream encountered a new link with
  1867. a starting timestamp that failed basic validity
  1868. checks.*/
  1869. OP_WARN_UNUSED_RESULT int op_read_stereo(OggOpusFile *_of,
  1870. opus_int16 *_pcm,int _buf_size) OP_ARG_NONNULL(1);
  1871. /**Reads more samples from the stream and downmixes to stereo, if necessary.
  1872. This function is intended for simple players that want a uniform output
  1873. format, even if the channel count changes between links in a chained
  1874. stream.
  1875. \note \a _buf_size indicates the total number of values that can be stored
  1876. in \a _pcm, while the return value is the number of samples <em>per
  1877. channel</em>, even though the channel count is known, for consistency with
  1878. op_read_float().
  1879. \param _of The \c OggOpusFile from which to read.
  1880. \param[out] _pcm A buffer in which to store the output PCM samples, as
  1881. signed floats at 48&nbsp;kHz with a nominal range of
  1882. <code>[-1.0,1.0]</code>.
  1883. The left and right channels are interleaved in the
  1884. buffer.
  1885. This must have room for at least \a _buf_size values.
  1886. \param _buf_size The number of values that can be stored in \a _pcm.
  1887. It is recommended that this be large enough for at
  1888. least 120 ms of data at 48 kHz per channel (11520
  1889. values total).
  1890. Smaller buffers will simply return less data, possibly
  1891. consuming more memory to buffer the data internally.
  1892. If less than \a _buf_size values are returned,
  1893. <tt>libopusfile</tt> makes no guarantee that the
  1894. remaining data in \a _pcm will be unmodified.
  1895. \return The number of samples read per channel on success, or a negative
  1896. value on failure.
  1897. The number of samples returned may be 0 if the buffer was too small
  1898. to store even a single sample for both channels, or if end-of-file
  1899. was reached.
  1900. The list of possible failure codes follows.
  1901. Most of them can only be returned by unseekable, chained streams
  1902. that encounter a new link.
  1903. \retval #OP_HOLE There was a hole in the data, and some samples
  1904. may have been skipped.
  1905. Call this function again to continue decoding
  1906. past the hole.
  1907. \retval #OP_EREAD An underlying read operation failed.
  1908. This may signal a truncation attack from an
  1909. <https:> source.
  1910. \retval #OP_EFAULT An internal memory allocation failed.
  1911. \retval #OP_EIMPL An unseekable stream encountered a new link that
  1912. used a feature that is not implemented, such as
  1913. an unsupported channel family.
  1914. \retval #OP_EINVAL The stream was only partially open.
  1915. \retval #OP_ENOTFORMAT An unseekable stream encountered a new link that
  1916. that did not have any logical Opus streams in it.
  1917. \retval #OP_EBADHEADER An unseekable stream encountered a new link with a
  1918. required header packet that was not properly
  1919. formatted, contained illegal values, or was
  1920. missing altogether.
  1921. \retval #OP_EVERSION An unseekable stream encountered a new link with
  1922. an ID header that contained an unrecognized
  1923. version number.
  1924. \retval #OP_EBADPACKET Failed to properly decode the next packet.
  1925. \retval #OP_EBADLINK We failed to find data we had seen before.
  1926. \retval #OP_EBADTIMESTAMP An unseekable stream encountered a new link with
  1927. a starting timestamp that failed basic validity
  1928. checks.*/
  1929. OP_WARN_UNUSED_RESULT int op_read_float_stereo(OggOpusFile *_of,
  1930. float *_pcm,int _buf_size) OP_ARG_NONNULL(1);
  1931. /*@}*/
  1932. /*@}*/
  1933. # if OP_GNUC_PREREQ(4,0)
  1934. # pragma GCC visibility pop
  1935. # endif
  1936. # if defined(__cplusplus)
  1937. }
  1938. # endif
  1939. #endif