stb_vorbis.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. // Ogg Vorbis audio decoder - v1.05 - public domain
  2. // http://nothings.org/stb_vorbis/
  3. //
  4. // Written by Sean Barrett in 2007, last updated in 2014
  5. // Sponsored by RAD Game Tools.
  6. //
  7. // Placed in the public domain April 2007 by the author: no copyright
  8. // is claimed, and you may use it for any purpose you like.
  9. //
  10. // No warranty for any purpose is expressed or implied by the author (nor
  11. // by RAD Game Tools). Report bugs and send enhancements to the author.
  12. //
  13. // Limitations:
  14. //
  15. // - seeking not supported except manually via PUSHDATA api
  16. // - floor 0 not supported (used in old ogg vorbis files pre-2004)
  17. // - lossless sample-truncation at beginning ignored
  18. // - cannot concatenate multiple vorbis streams
  19. // - sample positions are 32-bit, limiting seekable 192Khz
  20. // files to around 6 hours (Ogg supports 64-bit)
  21. //
  22. // Bugfix/warning contributors:
  23. // Terje Mathisen Niklas Frykholm Andy Hill
  24. // Casey Muratori John Bolton Gargaj
  25. // Laurent Gomila Marc LeBlanc Ronny Chevalier
  26. // Bernhard Wodo Evan Balster "alxprd"@github
  27. // Tom Beaumont Ingo Leitgeb Nicolas Guillemot
  28. // (If you reported a bug but do not appear in this list, it is because
  29. // someone else reported the bug before you. There were too many of you to
  30. // list them all because I was lax about updating for a long time, sorry.)
  31. //
  32. // Partial history:
  33. // 1.05 - 2015/04/19 - don't define __forceinline if it's redundant
  34. // 1.04 - 2014/08/27 - fix missing const-correct case in API
  35. // 1.03 - 2014/08/07 - warning fixes
  36. // 1.02 - 2014/07/09 - declare qsort comparison as explicitly _cdecl in Windows
  37. // 1.01 - 2014/06/18 - fix stb_vorbis_get_samples_float (interleaved was correct)
  38. // 1.0 - 2014/05/26 - fix memory leaks; fix warnings; fix bugs in >2-channel;
  39. // (API change) report sample rate for decode-full-file funcs
  40. // 0.99996 - - bracket #include <malloc.h> for macintosh compilation
  41. // 0.99995 - - avoid alias-optimization issue in float-to-int conversion
  42. //
  43. // See end of file for full version history.
  44. //////////////////////////////////////////////////////////////////////////////
  45. //
  46. // HEADER BEGINS HERE
  47. //
  48. #ifndef STB_VORBIS_INCLUDE_STB_VORBIS_H
  49. #define STB_VORBIS_INCLUDE_STB_VORBIS_H
  50. #if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)
  51. #define STB_VORBIS_NO_STDIO 1
  52. #endif
  53. #ifndef STB_VORBIS_NO_STDIO
  54. #include <stdio.h>
  55. #endif
  56. #ifdef __cplusplus
  57. extern "C" {
  58. #endif
  59. /////////// THREAD SAFETY
  60. // Individual stb_vorbis* handles are not thread-safe; you cannot decode from
  61. // them from multiple threads at the same time. However, you can have multiple
  62. // stb_vorbis* handles and decode from them independently in multiple thrads.
  63. /////////// MEMORY ALLOCATION
  64. // normally stb_vorbis uses malloc() to allocate memory at startup,
  65. // and alloca() to allocate temporary memory during a frame on the
  66. // stack. (Memory consumption will depend on the amount of setup
  67. // data in the file and how you set the compile flags for speed
  68. // vs. size. In my test files the maximal-size usage is ~150KB.)
  69. //
  70. // You can modify the wrapper functions in the source (setup_malloc,
  71. // setup_temp_malloc, temp_malloc) to change this behavior, or you
  72. // can use a simpler allocation model: you pass in a buffer from
  73. // which stb_vorbis will allocate _all_ its memory (including the
  74. // temp memory). "open" may fail with a VORBIS_outofmem if you
  75. // do not pass in enough data; there is no way to determine how
  76. // much you do need except to succeed (at which point you can
  77. // query get_info to find the exact amount required. yes I know
  78. // this is lame).
  79. //
  80. // If you pass in a non-NULL buffer of the type below, allocation
  81. // will occur from it as described above. Otherwise just pass NULL
  82. // to use malloc()/alloca()
  83. typedef struct
  84. {
  85. char *alloc_buffer;
  86. int alloc_buffer_length_in_bytes;
  87. } stb_vorbis_alloc;
  88. /////////// FUNCTIONS USEABLE WITH ALL INPUT MODES
  89. typedef struct stb_vorbis stb_vorbis;
  90. typedef struct
  91. {
  92. unsigned int sample_rate;
  93. int channels;
  94. unsigned int setup_memory_required;
  95. unsigned int setup_temp_memory_required;
  96. unsigned int temp_memory_required;
  97. int max_frame_size;
  98. } stb_vorbis_info;
  99. // get general information about the file
  100. extern stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f);
  101. // get the last error detected (clears it, too)
  102. extern int stb_vorbis_get_error(stb_vorbis *f);
  103. // close an ogg vorbis file and free all memory in use
  104. extern void stb_vorbis_close(stb_vorbis *f);
  105. // this function returns the offset (in samples) from the beginning of the
  106. // file that will be returned by the next decode, if it is known, or -1
  107. // otherwise. after a flush_pushdata() call, this may take a while before
  108. // it becomes valid again.
  109. // NOT WORKING YET after a seek with PULLDATA API
  110. extern int stb_vorbis_get_sample_offset(stb_vorbis *f);
  111. // returns the current seek point within the file, or offset from the beginning
  112. // of the memory buffer. In pushdata mode it returns 0.
  113. extern unsigned int stb_vorbis_get_file_offset(stb_vorbis *f);
  114. /////////// PUSHDATA API
  115. #ifndef STB_VORBIS_NO_PUSHDATA_API
  116. // this API allows you to get blocks of data from any source and hand
  117. // them to stb_vorbis. you have to buffer them; stb_vorbis will tell
  118. // you how much it used, and you have to give it the rest next time;
  119. // and stb_vorbis may not have enough data to work with and you will
  120. // need to give it the same data again PLUS more. Note that the Vorbis
  121. // specification does not bound the size of an individual frame.
  122. extern stb_vorbis *stb_vorbis_open_pushdata(
  123. unsigned char *datablock, int datablock_length_in_bytes,
  124. int *datablock_memory_consumed_in_bytes,
  125. int *error,
  126. stb_vorbis_alloc *alloc_buffer);
  127. // create a vorbis decoder by passing in the initial data block containing
  128. // the ogg&vorbis headers (you don't need to do parse them, just provide
  129. // the first N bytes of the file--you're told if it's not enough, see below)
  130. // on success, returns an stb_vorbis *, does not set error, returns the amount of
  131. // data parsed/consumed on this call in *datablock_memory_consumed_in_bytes;
  132. // on failure, returns NULL on error and sets *error, does not change *datablock_memory_consumed
  133. // if returns NULL and *error is VORBIS_need_more_data, then the input block was
  134. // incomplete and you need to pass in a larger block from the start of the file
  135. extern int stb_vorbis_decode_frame_pushdata(
  136. stb_vorbis *f, unsigned char *datablock, int datablock_length_in_bytes,
  137. int *channels, // place to write number of float * buffers
  138. float ***output, // place to write float ** array of float * buffers
  139. int *samples // place to write number of output samples
  140. );
  141. // decode a frame of audio sample data if possible from the passed-in data block
  142. //
  143. // return value: number of bytes we used from datablock
  144. //
  145. // possible cases:
  146. // 0 bytes used, 0 samples output (need more data)
  147. // N bytes used, 0 samples output (resynching the stream, keep going)
  148. // N bytes used, M samples output (one frame of data)
  149. // note that after opening a file, you will ALWAYS get one N-bytes,0-sample
  150. // frame, because Vorbis always "discards" the first frame.
  151. //
  152. // Note that on resynch, stb_vorbis will rarely consume all of the buffer,
  153. // instead only datablock_length_in_bytes-3 or less. This is because it wants
  154. // to avoid missing parts of a page header if they cross a datablock boundary,
  155. // without writing state-machiney code to record a partial detection.
  156. //
  157. // The number of channels returned are stored in *channels (which can be
  158. // NULL--it is always the same as the number of channels reported by
  159. // get_info). *output will contain an array of float* buffers, one per
  160. // channel. In other words, (*output)[0][0] contains the first sample from
  161. // the first channel, and (*output)[1][0] contains the first sample from
  162. // the second channel.
  163. extern void stb_vorbis_flush_pushdata(stb_vorbis *f);
  164. // inform stb_vorbis that your next datablock will not be contiguous with
  165. // previous ones (e.g. you've seeked in the data); future attempts to decode
  166. // frames will cause stb_vorbis to resynchronize (as noted above), and
  167. // once it sees a valid Ogg page (typically 4-8KB, as large as 64KB), it
  168. // will begin decoding the _next_ frame.
  169. //
  170. // if you want to seek using pushdata, you need to seek in your file, then
  171. // call stb_vorbis_flush_pushdata(), then start calling decoding, then once
  172. // decoding is returning you data, call stb_vorbis_get_sample_offset, and
  173. // if you don't like the result, seek your file again and repeat.
  174. #endif
  175. ////////// PULLING INPUT API
  176. #ifndef STB_VORBIS_NO_PULLDATA_API
  177. // This API assumes stb_vorbis is allowed to pull data from a source--
  178. // either a block of memory containing the _entire_ vorbis stream, or a
  179. // FILE * that you or it create, or possibly some other reading mechanism
  180. // if you go modify the source to replace the FILE * case with some kind
  181. // of callback to your code. (But if you don't support seeking, you may
  182. // just want to go ahead and use pushdata.)
  183. #if !defined(STB_VORBIS_NO_STDIO) && !defined(STB_VORBIS_NO_INTEGER_CONVERSION)
  184. extern int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output);
  185. #endif
  186. #if !defined(STB_VORBIS_NO_INTEGER_CONVERSION)
  187. extern int stb_vorbis_decode_memory(const unsigned char *mem, int len, int *channels, int *sample_rate, short **output);
  188. #endif
  189. // decode an entire file and output the data interleaved into a malloc()ed
  190. // buffer stored in *output. The return value is the number of samples
  191. // decoded, or -1 if the file could not be opened or was not an ogg vorbis file.
  192. // When you're done with it, just free() the pointer returned in *output.
  193. extern stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len,
  194. int *error, stb_vorbis_alloc *alloc_buffer);
  195. // create an ogg vorbis decoder from an ogg vorbis stream in memory (note
  196. // this must be the entire stream!). on failure, returns NULL and sets *error
  197. #ifndef STB_VORBIS_NO_STDIO
  198. extern stb_vorbis * stb_vorbis_open_filename(const char *filename,
  199. int *error, stb_vorbis_alloc *alloc_buffer);
  200. // create an ogg vorbis decoder from a filename via fopen(). on failure,
  201. // returns NULL and sets *error (possibly to VORBIS_file_open_failure).
  202. extern stb_vorbis * stb_vorbis_open_file(FILE *f, int close_handle_on_close,
  203. int *error, stb_vorbis_alloc *alloc_buffer);
  204. // create an ogg vorbis decoder from an open FILE *, looking for a stream at
  205. // the _current_ seek point (ftell). on failure, returns NULL and sets *error.
  206. // note that stb_vorbis must "own" this stream; if you seek it in between
  207. // calls to stb_vorbis, it will become confused. Morever, if you attempt to
  208. // perform stb_vorbis_seek_*() operations on this file, it will assume it
  209. // owns the _entire_ rest of the file after the start point. Use the next
  210. // function, stb_vorbis_open_file_section(), to limit it.
  211. extern stb_vorbis * stb_vorbis_open_file_section(FILE *f, int close_handle_on_close,
  212. int *error, stb_vorbis_alloc *alloc_buffer, unsigned int len);
  213. // create an ogg vorbis decoder from an open FILE *, looking for a stream at
  214. // the _current_ seek point (ftell); the stream will be of length 'len' bytes.
  215. // on failure, returns NULL and sets *error. note that stb_vorbis must "own"
  216. // this stream; if you seek it in between calls to stb_vorbis, it will become
  217. // confused.
  218. #endif
  219. extern int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number);
  220. extern int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number);
  221. // NOT WORKING YET
  222. // these functions seek in the Vorbis file to (approximately) 'sample_number'.
  223. // after calling seek_frame(), the next call to get_frame_*() will include
  224. // the specified sample. after calling stb_vorbis_seek(), the next call to
  225. // stb_vorbis_get_samples_* will start with the specified sample. If you
  226. // do not need to seek to EXACTLY the target sample when using get_samples_*,
  227. // you can also use seek_frame().
  228. extern void stb_vorbis_seek_start(stb_vorbis *f);
  229. // this function is equivalent to stb_vorbis_seek(f,0), but it
  230. // actually works
  231. extern unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f);
  232. extern float stb_vorbis_stream_length_in_seconds(stb_vorbis *f);
  233. // these functions return the total length of the vorbis stream
  234. extern int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output);
  235. // decode the next frame and return the number of samples. the number of
  236. // channels returned are stored in *channels (which can be NULL--it is always
  237. // the same as the number of channels reported by get_info). *output will
  238. // contain an array of float* buffers, one per channel. These outputs will
  239. // be overwritten on the next call to stb_vorbis_get_frame_*.
  240. //
  241. // You generally should not intermix calls to stb_vorbis_get_frame_*()
  242. // and stb_vorbis_get_samples_*(), since the latter calls the former.
  243. #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
  244. extern int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts);
  245. extern int stb_vorbis_get_frame_short (stb_vorbis *f, int num_c, short **buffer, int num_samples);
  246. #endif
  247. // decode the next frame and return the number of samples per channel. the
  248. // data is coerced to the number of channels you request according to the
  249. // channel coercion rules (see below). You must pass in the size of your
  250. // buffer(s) so that stb_vorbis will not overwrite the end of the buffer.
  251. // The maximum buffer size needed can be gotten from get_info(); however,
  252. // the Vorbis I specification implies an absolute maximum of 4096 samples
  253. // per channel. Note that for interleaved data, you pass in the number of
  254. // shorts (the size of your array), but the return value is the number of
  255. // samples per channel, not the total number of samples.
  256. // Channel coercion rules:
  257. // Let M be the number of channels requested, and N the number of channels present,
  258. // and Cn be the nth channel; let stereo L be the sum of all L and center channels,
  259. // and stereo R be the sum of all R and center channels (channel assignment from the
  260. // vorbis spec).
  261. // M N output
  262. // 1 k sum(Ck) for all k
  263. // 2 * stereo L, stereo R
  264. // k l k > l, the first l channels, then 0s
  265. // k l k <= l, the first k channels
  266. // Note that this is not _good_ surround etc. mixing at all! It's just so
  267. // you get something useful.
  268. extern int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats);
  269. extern int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples);
  270. // gets num_samples samples, not necessarily on a frame boundary--this requires
  271. // buffering so you have to supply the buffers. DOES NOT APPLY THE COERCION RULES.
  272. // Returns the number of samples stored per channel; it may be less than requested
  273. // at the end of the file. If there are no more samples in the file, returns 0.
  274. #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
  275. extern int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts);
  276. extern int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int num_samples);
  277. #endif
  278. // gets num_samples samples, not necessarily on a frame boundary--this requires
  279. // buffering so you have to supply the buffers. Applies the coercion rules above
  280. // to produce 'channels' channels. Returns the number of samples stored per channel;
  281. // it may be less than requested at the end of the file. If there are no more
  282. // samples in the file, returns 0.
  283. #endif
  284. //////// ERROR CODES
  285. enum STBVorbisError
  286. {
  287. VORBIS__no_error,
  288. VORBIS_need_more_data=1, // not a real error
  289. VORBIS_invalid_api_mixing, // can't mix API modes
  290. VORBIS_outofmem, // not enough memory
  291. VORBIS_feature_not_supported, // uses floor 0
  292. VORBIS_too_many_channels, // STB_VORBIS_MAX_CHANNELS is too small
  293. VORBIS_file_open_failure, // fopen() failed
  294. VORBIS_seek_without_length, // can't seek in unknown-length file
  295. VORBIS_unexpected_eof=10, // file is truncated?
  296. VORBIS_seek_invalid, // seek past EOF
  297. // decoding errors (corrupt/invalid stream) -- you probably
  298. // don't care about the exact details of these
  299. // vorbis errors:
  300. VORBIS_invalid_setup=20,
  301. VORBIS_invalid_stream,
  302. // ogg errors:
  303. VORBIS_missing_capture_pattern=30,
  304. VORBIS_invalid_stream_structure_version,
  305. VORBIS_continued_packet_flag_invalid,
  306. VORBIS_incorrect_stream_serial_number,
  307. VORBIS_invalid_first_page,
  308. VORBIS_bad_packet_type,
  309. VORBIS_cant_find_last_page,
  310. VORBIS_seek_failed,
  311. };
  312. #ifdef __cplusplus
  313. }
  314. #endif
  315. #endif // STB_VORBIS_INCLUDE_STB_VORBIS_H
  316. //
  317. // HEADER ENDS HERE
  318. //
  319. //////////////////////////////////////////////////////////////////////////////