FastLED 3.9.15
Loading...
Searching...
No Matches
stb_vorbis.h
Go to the documentation of this file.
1// stb_vorbis.h - Header for stb_vorbis audio decoder
2//
3// This file contains the API declarations extracted from stb_vorbis.cpp.hpp.
4// It provides the public interface without including the implementation.
5//
6// For the implementation, include stb_vorbis.cpp.hpp (which is included
7// via the unity build system in _build.hpp).
8//
9// Original: https://github.com/nothings/stb/blob/master/stb_vorbis.c
10// Author: Sean Barrett - http://nothings.org/stb_vorbis/
11// License: MIT or Public Domain
12
13// Use same guard as original stb_vorbis to prevent type redefinition
14#ifndef FL_STB_VORBIS_INCLUDE_FL_STB_VORBIS_H
15#define FL_STB_VORBIS_INCLUDE_FL_STB_VORBIS_H
16
17// Ogg Vorbis audio decoder - v1.22 - public domain
18// http://nothings.org/stb_vorbis/
19//
20// Original version written by Sean Barrett in 2007.
21//
22// Originally sponsored by RAD Game Tools. Seeking implementation
23// sponsored by Phillip Bennefall, Marc Andersen, Aaron Baker,
24// Elias Software, Aras Pranckevicius, and Sean Barrett.
25//
26// LICENSE
27//
28// See end of file for license information.
29//
30// Limitations:
31//
32// - floor 0 not supported (used in old ogg vorbis files pre-2004)
33// - lossless sample-truncation at beginning ignored
34// - cannot concatenate multiple vorbis streams
35// - sample positions are 32-bit, limiting seekable 192Khz
36// files to around 6 hours (Ogg supports 64-bit)
37//
38// Feature contributors:
39// Dougall Johnson (sample-exact seeking)
40//
41// Bugfix/warning contributors:
42// Terje Mathisen Niklas Frykholm Andy Hill
43// Casey Muratori John Bolton Gargaj
44// Laurent Gomila Marc LeBlanc Ronny Chevalier
45// Bernhard Wodo Evan Balster github:alxprd
46// Tom Beaumont Ingo Leitgeb Nicolas Guillemot
47// Phillip Bennefall Rohit Thiago Goulart
48// github:manxorist Saga Musix github:infatum
49// Timur Gagiev Maxwell Koo Peter Waller
50// github:audinowho Dougall Johnson David Reid
51// github:Clownacy Pedro J. Estebanez Remi Verschelde
52// AnthoFoxo github:morlat Gabriel Ravier
53//
54// Partial history:
55// 1.22 - 2021-07-11 - various small fixes
56// 1.21 - 2021-07-02 - fix bug for files with no comments
57// 1.20 - 2020-07-11 - several small fixes
58// 1.19 - 2020-02-05 - warnings
59// 1.18 - 2020-02-02 - fix seek bugs; parse header comments; misc warnings etc.
60// 1.17 - 2019-07-08 - fix CVE-2019-13217..CVE-2019-13223 (by ForAllSecure)
61// 1.16 - 2019-03-04 - fix warnings
62// 1.15 - 2019-02-07 - explicit failure if Ogg Skeleton data is found
63// 1.14 - 2018-02-11 - delete bogus dealloca usage
64// 1.13 - 2018-01-29 - fix truncation of last frame (hopefully)
65// 1.12 - 2017-11-21 - limit residue begin/end to blocksize/2 to avoid large temp allocs in bad/corrupt files
66// 1.11 - 2017-07-23 - fix MinGW compilation
67// 1.10 - 2017-03-03 - more robust seeking; fix negative ilog(); clear error in open_memory
68// 1.09 - 2016-04-04 - back out 'truncation of last frame' fix from previous version
69// 1.08 - 2016-04-02 - warnings; setup memory leaks; truncation of last frame
70// 1.07 - 2015-01-16 - fixes for crashes on invalid files; warning fixes; const
71// 1.06 - 2015-08-31 - full, correct support for seeking API (Dougall Johnson)
72// some crash fixes when out of memory or with corrupt files
73// fix some inappropriately signed shifts
74// 1.05 - 2015-04-19 - don't define __forceinline if it's redundant
75// 1.04 - 2014-08-27 - fix missing const-correct case in API
76// 1.03 - 2014-08-07 - warning fixes
77// 1.02 - 2014-07-09 - declare qsort comparison as explicitly _cdecl in Windows
78// 1.01 - 2014-06-18 - fix stb_vorbis_get_samples_float (interleaved was correct)
79// 1.0 - 2014-05-26 - fix memory leaks; fix warnings; fix bugs in >2-channel;
80// (API change) report sample rate for decode-full-file funcs
81//
82// See end of file for full version history.
83
84
86//
87// HEADER BEGINS HERE
88//
89
90// FastLED integration: use fl::numeric_limits instead of UINT_MAX
91#include "fl/stl/limits.h"
92#include "fl/stl/stdint.h"
93#include "fl/stl/noexcept.h"
94
95// FastLED integration: use fl::FILE* for file I/O abstraction
96#ifndef FL_STB_VORBIS_NO_STDIO
98#endif
99
100// FastLED integration: Disable stdio-based functions by default
101// Embedded platforms don't have standard file I/O, and including <stdio.h>
102// in headers is banned by the project linter. Users needing file-based
103// decoding can define FL_STB_VORBIS_ENABLE_STDIO before including this header.
104#ifndef FL_STB_VORBIS_ENABLE_STDIO
105#define FL_STB_VORBIS_NO_STDIO 1
106#endif
107
108#if defined(FL_STB_VORBIS_NO_CRT) && !defined(FL_STB_VORBIS_NO_STDIO)
109#define FL_STB_VORBIS_NO_STDIO 1
110#endif
111
112namespace fl {
113namespace third_party {
114namespace vorbis {
115
117
118// Individual stb_vorbis* handles are not thread-safe; you cannot decode from
119// them from multiple threads at the same time. However, you can have multiple
120// stb_vorbis* handles and decode from them independently in multiple thrads.
121
122
124
125// normally stb_vorbis uses malloc() to allocate memory at startup,
126// and alloca() to allocate temporary memory during a frame on the
127// stack. (Memory consumption will depend on the amount of setup
128// data in the file and how you set the compile flags for speed
129// vs. size. In my test files the maximal-size usage is ~150KB.)
130//
131// You can modify the wrapper functions in the source (setup_malloc,
132// setup_temp_malloc, temp_malloc) to change this behavior, or you
133// can use a simpler allocation model: you pass in a buffer from
134// which stb_vorbis will allocate _all_ its memory (including the
135// temp memory). "open" may fail with a VORBIS_outofmem if you
136// do not pass in enough data; there is no way to determine how
137// much you do need except to succeed (at which point you can
138// query get_info to find the exact amount required. yes I know
139// this is lame).
140//
141// If you pass in a non-NULL buffer of the type below, allocation
142// will occur from it as described above. Otherwise just pass NULL
143// to use malloc()/alloca()
144
150
151
153
154typedef struct stb_vorbis stb_vorbis;
155
167
175
176// get general information about the file
178
179// get ogg comments
181
182// get the last error detected (clears it, too)
184
185// close an ogg vorbis file and free all memory in use
187
188// this function returns the offset (in samples) from the beginning of the
189// file that will be returned by the next decode, if it is known, or -1
190// otherwise. after a flush_pushdata() call, this may take a while before
191// it becomes valid again.
192// NOT WORKING YET after a seek with PULLDATA API
194
195// returns the current seek point within the file, or offset from the beginning
196// of the memory buffer. In pushdata mode it returns 0.
198
200
201#ifndef FL_STB_VORBIS_NO_PUSHDATA_API
202
203// this API allows you to get blocks of data from any source and hand
204// them to stb_vorbis. you have to buffer them; stb_vorbis will tell
205// you how much it used, and you have to give it the rest next time;
206// and stb_vorbis may not have enough data to work with and you will
207// need to give it the same data again PLUS more. Note that the Vorbis
208// specification does not bound the size of an individual frame.
209
211 const unsigned char * datablock, int32_t datablock_length_in_bytes,
212 int32_t *datablock_memory_consumed_in_bytes,
213 int32_t *error,
214 const stb_vorbis_alloc *alloc_buffer) FL_NOEXCEPT;
215// create a vorbis decoder by passing in the initial data block containing
216// the ogg&vorbis headers (you don't need to do parse them, just provide
217// the first N bytes of the file--you're told if it's not enough, see below)
218// on success, returns an stb_vorbis *, does not set error, returns the amount of
219// data parsed/consumed on this call in *datablock_memory_consumed_in_bytes;
220// on failure, returns NULL on error and sets *error, does not change *datablock_memory_consumed
221// if returns NULL and *error is VORBIS_need_more_data, then the input block was
222// incomplete and you need to pass in a larger block from the start of the file
223
225 stb_vorbis *f,
226 const unsigned char *datablock, int32_t datablock_length_in_bytes,
227 int32_t *channels, // place to write number of float * buffers
228 float ***output, // place to write float ** array of float * buffers
229 int32_t *samples // place to write number of output samples
230 ) FL_NOEXCEPT;
231// decode a frame of audio sample data if possible from the passed-in data block
232//
233// return value: number of bytes we used from datablock
234//
235// possible cases:
236// 0 bytes used, 0 samples output (need more data)
237// N bytes used, 0 samples output (resynching the stream, keep going)
238// N bytes used, M samples output (one frame of data)
239// note that after opening a file, you will ALWAYS get one N-bytes,0-sample
240// frame, because Vorbis always "discards" the first frame.
241//
242// Note that on resynch, stb_vorbis will rarely consume all of the buffer,
243// instead only datablock_length_in_bytes-3 or less. This is because it wants
244// to avoid missing parts of a page header if they cross a datablock boundary,
245// without writing state-machiney code to record a partial detection.
246//
247// The number of channels returned are stored in *channels (which can be
248// NULL--it is always the same as the number of channels reported by
249// get_info). *output will contain an array of float* buffers, one per
250// channel. In other words, (*output)[0][0] contains the first sample from
251// the first channel, and (*output)[1][0] contains the first sample from
252// the second channel.
253//
254// *output points into stb_vorbis's internal output buffer storage; these
255// buffers are owned by stb_vorbis and application code should not free
256// them or modify their contents. They are transient and will be overwritten
257// once you ask for more data to get decoded, so be sure to grab any data
258// you need before then.
259
261// inform stb_vorbis that your next datablock will not be contiguous with
262// previous ones (e.g. you've seeked in the data); future attempts to decode
263// frames will cause stb_vorbis to resynchronize (as noted above), and
264// once it sees a valid Ogg page (typically 4-8KB, as large as 64KB), it
265// will begin decoding the _next_ frame.
266//
267// if you want to seek using pushdata, you need to seek in your file, then
268// call stb_vorbis_flush_pushdata(), then start calling decoding, then once
269// decoding is returning you data, call stb_vorbis_get_sample_offset, and
270// if you don't like the result, seek your file again and repeat.
271#endif
272
273
275
276#ifndef FL_STB_VORBIS_NO_PULLDATA_API
277// This API assumes stb_vorbis is allowed to pull data from a source--
278// either a block of memory containing the _entire_ vorbis stream, or a
279// fl::FILE * that you or it create, or possibly some other reading mechanism
280// if you go modify the source to replace the fl::FILE * case with some kind
281// of callback to your code. (But if you don't support seeking, you may
282// just want to go ahead and use pushdata.)
283
284#if !defined(FL_STB_VORBIS_NO_STDIO) && !defined(FL_STB_VORBIS_NO_INTEGER_CONVERSION)
285int32_t stb_vorbis_decode_filename(const char *filename, int32_t *channels, int32_t *sample_rate, int16_t **output);
286#endif
287#if !defined(FL_STB_VORBIS_NO_INTEGER_CONVERSION)
288int32_t stb_vorbis_decode_memory(const unsigned char *mem, int32_t len, int32_t *channels, int32_t *sample_rate, int16_t **output) FL_NOEXCEPT;
289#endif
290// decode an entire file and output the data interleaved into a malloc()ed
291// buffer stored in *output. The return value is the number of samples
292// decoded, or -1 if the file could not be opened or was not an ogg vorbis file.
293// When you're done with it, just free() the pointer returned in *output.
294
295stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int32_t len,
296 int32_t *error, const stb_vorbis_alloc *alloc_buffer) FL_NOEXCEPT;
297// create an ogg vorbis decoder from an ogg vorbis stream in memory (note
298// this must be the entire stream!). on failure, returns NULL and sets *error
299
300#ifndef FL_STB_VORBIS_NO_STDIO
301stb_vorbis * stb_vorbis_open_filename(const char *filename,
302 int32_t *error, const stb_vorbis_alloc *alloc_buffer);
303// create an ogg vorbis decoder from a filename via fopen(). on failure,
304// returns NULL and sets *error (possibly to VORBIS_file_open_failure).
305
306stb_vorbis * stb_vorbis_open_file(fl::FILE *f, int32_t close_handle_on_close,
307 int32_t *error, const stb_vorbis_alloc *alloc_buffer);
308// create an ogg vorbis decoder from an open fl::FILE *, looking for a stream at
309// the _current_ seek point (ftell). on failure, returns NULL and sets *error.
310// note that stb_vorbis must "own" this stream; if you seek it in between
311// calls to stb_vorbis, it will become confused. Moreover, if you attempt to
312// perform stb_vorbis_seek_*() operations on this file, it will assume it
313// owns the _entire_ rest of the file after the start point. Use the next
314// function, stb_vorbis_open_file_section(), to limit it.
315
316stb_vorbis * stb_vorbis_open_file_section(fl::FILE *f, int32_t close_handle_on_close,
317 int32_t *error, const stb_vorbis_alloc *alloc_buffer, uint32_t len);
318// create an ogg vorbis decoder from an open fl::FILE *, looking for a stream at
319// the _current_ seek point (ftell); the stream will be of length 'len' bytes.
320// on failure, returns NULL and sets *error. note that stb_vorbis must "own"
321// this stream; if you seek it in between calls to stb_vorbis, it will become
322// confused.
323#endif
324
327// these functions seek in the Vorbis file to (approximately) 'sample_number'.
328// after calling seek_frame(), the next call to get_frame_*() will include
329// the specified sample. after calling stb_vorbis_seek(), the next call to
330// stb_vorbis_get_samples_* will start with the specified sample. If you
331// do not need to seek to EXACTLY the target sample when using get_samples_*,
332// you can also use seek_frame().
333
335// this function is equivalent to stb_vorbis_seek(f,0)
336
339// these functions return the total length of the vorbis stream
340
342// decode the next frame and return the number of samples. the number of
343// channels returned are stored in *channels (which can be NULL--it is always
344// the same as the number of channels reported by get_info). *output will
345// contain an array of float* buffers, one per channel. These outputs will
346// be overwritten on the next call to stb_vorbis_get_frame_*.
347//
348// You generally should not intermix calls to stb_vorbis_get_frame_*()
349// and stb_vorbis_get_samples_*(), since the latter calls the former.
350
351#ifndef FL_STB_VORBIS_NO_INTEGER_CONVERSION
354#endif
355// decode the next frame and return the number of *samples* per channel.
356// Note that for interleaved data, you pass in the number of shorts (the
357// size of your array), but the return value is the number of samples per
358// channel, not the total number of samples.
359//
360// The data is coerced to the number of channels you request according to the
361// channel coercion rules (see below). You must pass in the size of your
362// buffer(s) so that stb_vorbis will not overwrite the end of the buffer.
363// The maximum buffer size needed can be gotten from get_info(); however,
364// the Vorbis I specification implies an absolute maximum of 4096 samples
365// per channel.
366
367// Channel coercion rules:
368// Let M be the number of channels requested, and N the number of channels present,
369// and Cn be the nth channel; let stereo L be the sum of all L and center channels,
370// and stereo R be the sum of all R and center channels (channel assignment from the
371// vorbis spec).
372// M N output
373// 1 k sum(Ck) for all k
374// 2 * stereo L, stereo R
375// k l k > l, the first l channels, then 0s
376// k l k <= l, the first k channels
377// Note that this is not _good_ surround etc. mixing at all! It's just so
378// you get something useful.
379
382// gets num_samples samples, not necessarily on a frame boundary--this requires
383// buffering so you have to supply the buffers. DOES NOT APPLY THE COERCION RULES.
384// Returns the number of samples stored per channel; it may be less than requested
385// at the end of the file. If there are no more samples in the file, returns 0.
386
387#ifndef FL_STB_VORBIS_NO_INTEGER_CONVERSION
390#endif
391// gets num_samples samples, not necessarily on a frame boundary--this requires
392// buffering so you have to supply the buffers. Applies the coercion rules above
393// to produce 'channels' channels. Returns the number of samples stored per channel;
394// it may be less than requested at the end of the file. If there are no more
395// samples in the file, returns 0.
396
397#endif
398
400
402{
404
405 VORBIS_need_more_data=1, // not a real error
406
407 VORBIS_invalid_api_mixing, // can't mix API modes
408 VORBIS_outofmem, // not enough memory
410 VORBIS_too_many_channels, // FL_STB_VORBIS_MAX_CHANNELS is too small
411 VORBIS_file_open_failure, // fopen() failed
412 VORBIS_seek_without_length, // can't seek in unknown-length file
413
414 VORBIS_unexpected_eof=10, // file is truncated?
415 VORBIS_seek_invalid, // seek past EOF
416
417 // decoding errors (corrupt/invalid stream) -- you probably
418 // don't care about the exact details of these
419
420 // vorbis errors:
423
424 // ogg errors:
434};
435
436
437} // namespace vorbis
438} // namespace third_party
439} // namespace fl
440
441#endif // FL_STB_VORBIS_INCLUDE_FL_STB_VORBIS_H
float stb_vorbis_stream_length_in_seconds(stb_vorbis *f) FL_NOEXCEPT
static int32_t error(vorb *f, enum STBVorbisError e) FL_NOEXCEPT
int32_t stb_vorbis_get_frame_short(stb_vorbis *f, int32_t num_c, short **buffer, int32_t num_samples) FL_NOEXCEPT
stb_vorbis * stb_vorbis_open_filename(const char *filename, int32_t *error, const stb_vorbis_alloc *alloc)
int32_t stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int32_t channels, short *buffer, int32_t num_shorts) FL_NOEXCEPT
int32_t stb_vorbis_seek_start(stb_vorbis *f) FL_NOEXCEPT
int32_t stb_vorbis_seek(stb_vorbis *f, uint32_t sample_number) FL_NOEXCEPT
int32_t stb_vorbis_get_frame_float(stb_vorbis *f, int32_t *channels, float ***output) FL_NOEXCEPT
stb_vorbis * stb_vorbis_open_pushdata(const unsigned char *data, int32_t data_len, int32_t *data_used, int32_t *error, const stb_vorbis_alloc *alloc) FL_NOEXCEPT
stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f) FL_NOEXCEPT
uint32_t stb_vorbis_stream_length_in_samples(stb_vorbis *f) FL_NOEXCEPT
int32_t stb_vorbis_get_error(stb_vorbis *f) FL_NOEXCEPT
stb_vorbis * stb_vorbis_open_file_section(fl::FILE *file, int32_t close_on_free, int32_t *error, const stb_vorbis_alloc *alloc, uint32_t length)
stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int32_t len, int32_t *error, const stb_vorbis_alloc *alloc) FL_NOEXCEPT
int32_t stb_vorbis_get_samples_float(stb_vorbis *f, int32_t channels, float **buffer, int32_t num_samples) FL_NOEXCEPT
uint32_t stb_vorbis_get_file_offset(stb_vorbis *f) FL_NOEXCEPT
int32_t stb_vorbis_decode_filename(const char *filename, int32_t *channels, int32_t *sample_rate, short **output)
void stb_vorbis_close(stb_vorbis *p) FL_NOEXCEPT
void stb_vorbis_flush_pushdata(stb_vorbis *f) FL_NOEXCEPT
int32_t stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int32_t channels, float *buffer, int32_t num_floats) FL_NOEXCEPT
int32_t stb_vorbis_decode_frame_pushdata(stb_vorbis *f, const uint8 *data, int32_t data_len, int32_t *channels, float ***output, int32_t *samples) FL_NOEXCEPT
stb_vorbis_comment stb_vorbis_get_comment(stb_vorbis *f) FL_NOEXCEPT
int32_t stb_vorbis_seek_frame(stb_vorbis *f, uint32_t sample_number) FL_NOEXCEPT
int32_t stb_vorbis_get_samples_short(stb_vorbis *f, int32_t channels, short **buffer, int32_t len) FL_NOEXCEPT
int32_t stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int32_t num_c, short *buffer, int32_t num_shorts) FL_NOEXCEPT
int32_t stb_vorbis_get_sample_offset(stb_vorbis *f) FL_NOEXCEPT
stb_vorbis * stb_vorbis_open_file(fl::FILE *file, int32_t close_on_free, int32_t *error, const stb_vorbis_alloc *alloc)
int32_t stb_vorbis_decode_memory(const uint8 *mem, int32_t len, int32_t *channels, int32_t *sample_rate, short **output) FL_NOEXCEPT
fl::u32 uint32_t
Definition coder.h:219
fl::i16 int16_t
Definition coder.h:215
fl::i32 int32_t
Definition coder.h:220
FILE_impl FILE
Definition file_io.h:32
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT