The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

455 lines
20KB

  1. /* libFLAC - Free Lossless Audio Codec library
  2. * Copyright (C) 2000-2009 Josh Coalson
  3. * Copyright (C) 2011-2023 Xiph.Org Foundation
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * - Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * - Neither the name of the Xiph.org Foundation nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #ifndef FLAC__ALL_H
  33. #define FLAC__ALL_H
  34. #ifndef FLAC__HAS_OGG
  35. #define FLAC__HAS_OGG 0
  36. #endif
  37. #include "export.h"
  38. #include "assert.h"
  39. #include "callback.h"
  40. #include "format.h"
  41. #include "metadata.h"
  42. #include "ordinals.h"
  43. #include "stream_decoder.h"
  44. #include "stream_encoder.h"
  45. /** \mainpage
  46. *
  47. * \section intro Introduction
  48. *
  49. * This is the documentation for the FLAC C and C++ APIs. It is
  50. * highly interconnected; this introduction should give you a top
  51. * level idea of the structure and how to find the information you
  52. * need. As a prerequisite you should have at least a basic
  53. * knowledge of the FLAC format, documented
  54. * <A HREF="https://xiph.org/flac/format.html">here</A>.
  55. *
  56. * \section c_api FLAC C API
  57. *
  58. * The FLAC C API is the interface to libFLAC, a set of structures
  59. * describing the components of FLAC streams, and functions for
  60. * encoding and decoding streams, as well as manipulating FLAC
  61. * metadata in files. The public include files will be installed
  62. * in your include area (for example /usr/include/FLAC/...).
  63. *
  64. * By writing a little code and linking against libFLAC, it is
  65. * relatively easy to add FLAC support to another program. The
  66. * library is licensed under <A HREF="https://xiph.org/flac/license.html">Xiph's BSD license</A>.
  67. * Complete source code of libFLAC as well as the command-line
  68. * encoder and plugins is available and is a useful source of
  69. * examples.
  70. *
  71. * Aside from encoders and decoders, libFLAC provides a powerful
  72. * metadata interface for manipulating metadata in FLAC files. It
  73. * allows the user to add, delete, and modify FLAC metadata blocks
  74. * and it can automatically take advantage of PADDING blocks to avoid
  75. * rewriting the entire FLAC file when changing the size of the
  76. * metadata.
  77. *
  78. * libFLAC usually only requires the standard C library and C math
  79. * library. In particular, threading is not used so there is no
  80. * dependency on a thread library. However, libFLAC does not use
  81. * global variables and should be thread-safe.
  82. *
  83. * libFLAC also supports encoding to and decoding from Ogg FLAC.
  84. * However the metadata editing interfaces currently have limited
  85. * read-only support for Ogg FLAC files.
  86. *
  87. * \section cpp_api FLAC C++ API
  88. *
  89. * The FLAC C++ API is a set of classes that encapsulate the
  90. * structures and functions in libFLAC. They provide slightly more
  91. * functionality with respect to metadata but are otherwise
  92. * equivalent. For the most part, they share the same usage as
  93. * their counterparts in libFLAC, and the FLAC C API documentation
  94. * can be used as a supplement. The public include files
  95. * for the C++ API will be installed in your include area (for
  96. * example /usr/include/FLAC++/...).
  97. *
  98. * libFLAC++ is also licensed under
  99. * <A HREF="https://xiph.org/flac/license.html">Xiph's BSD license</A>.
  100. *
  101. * \section getting_started Getting Started
  102. *
  103. * A good starting point for learning the API is to browse through
  104. * the <A HREF="modules.html">modules</A>. Modules are logical
  105. * groupings of related functions or classes, which correspond roughly
  106. * to header files or sections of header files. Each module includes a
  107. * detailed description of the general usage of its functions or
  108. * classes.
  109. *
  110. * From there you can go on to look at the documentation of
  111. * individual functions. You can see different views of the individual
  112. * functions through the links in top bar across this page.
  113. *
  114. * If you prefer a more hands-on approach, you can jump right to some
  115. * <A HREF="https://xiph.org/flac/documentation_example_code.html">example code</A>.
  116. *
  117. * \section porting_guide Porting Guide
  118. *
  119. * Starting with FLAC 1.1.3 a \link porting Porting Guide \endlink
  120. * has been introduced which gives detailed instructions on how to
  121. * port your code to newer versions of FLAC.
  122. *
  123. * \section embedded_developers Embedded Developers
  124. *
  125. * libFLAC has grown larger over time as more functionality has been
  126. * included, but much of it may be unnecessary for a particular embedded
  127. * implementation. Unused parts may be pruned by some simple editing of
  128. * src/libFLAC/Makefile.am. In general, the decoders, encoders, and
  129. * metadata interface are all independent from each other.
  130. *
  131. * It is easiest to just describe the dependencies:
  132. *
  133. * - All modules depend on the \link flac_format Format \endlink module.
  134. * - The decoders and encoders depend on the bitbuffer.
  135. * - The decoder is independent of the encoder. The encoder uses the
  136. * decoder because of the verify feature, but this can be removed if
  137. * not needed.
  138. * - Parts of the metadata interface require the stream decoder (but not
  139. * the encoder).
  140. * - Ogg support is selectable through the compile time macro
  141. * \c FLAC__HAS_OGG.
  142. *
  143. * For example, if your application only requires the stream decoder, no
  144. * encoder, and no metadata interface, you can remove the stream encoder
  145. * and the metadata interface, which will greatly reduce the size of the
  146. * library.
  147. *
  148. * Also, there are several places in the libFLAC code with comments marked
  149. * with "OPT:" where a \#define can be changed to enable code that might be
  150. * faster on a specific platform. Experimenting with these can yield faster
  151. * binaries.
  152. */
  153. /** \defgroup porting Porting Guide for New Versions
  154. *
  155. * This module describes differences in the library interfaces from
  156. * version to version. It assists in the porting of code that uses
  157. * the libraries to newer versions of FLAC.
  158. *
  159. * One simple facility for making porting easier that has been added
  160. * in FLAC 1.1.3 is a set of \#defines in \c export.h of each
  161. * library's includes (e.g. \c include/FLAC/export.h). The
  162. * \#defines mirror the libraries'
  163. * <A HREF="http://www.gnu.org/software/libtool/manual/libtool.html#Libtool-versioning">libtool version numbers</A>,
  164. * e.g. in libFLAC there are \c FLAC_API_VERSION_CURRENT,
  165. * \c FLAC_API_VERSION_REVISION, and \c FLAC_API_VERSION_AGE.
  166. * These can be used to support multiple versions of an API during the
  167. * transition phase, e.g.
  168. *
  169. * \code
  170. * #if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7
  171. * legacy code
  172. * #else
  173. * new code
  174. * #endif
  175. * \endcode
  176. *
  177. * The source will work for multiple versions and the legacy code can
  178. * easily be removed when the transition is complete.
  179. *
  180. * Another available symbol is FLAC_API_SUPPORTS_OGG_FLAC (defined in
  181. * include/FLAC/export.h), which can be used to determine whether or not
  182. * the library has been compiled with support for Ogg FLAC. This is
  183. * simpler than trying to call an Ogg init function and catching the
  184. * error.
  185. */
  186. /** \defgroup porting_1_1_2_to_1_1_3 Porting from FLAC 1.1.2 to 1.1.3
  187. * \ingroup porting
  188. *
  189. * \brief
  190. * This module describes porting from FLAC 1.1.2 to FLAC 1.1.3.
  191. *
  192. * The main change between the APIs in 1.1.2 and 1.1.3 is that they have
  193. * been simplified. First, libOggFLAC has been merged into libFLAC and
  194. * libOggFLAC++ has been merged into libFLAC++. Second, both the three
  195. * decoding layers and three encoding layers have been merged into a
  196. * single stream decoder and stream encoder. That is, the functionality
  197. * of FLAC__SeekableStreamDecoder and FLAC__FileDecoder has been merged
  198. * into FLAC__StreamDecoder, and FLAC__SeekableStreamEncoder and
  199. * FLAC__FileEncoder into FLAC__StreamEncoder. Only the
  200. * FLAC__StreamDecoder and FLAC__StreamEncoder remain. What this means
  201. * is there is now a single API that can be used to encode or decode
  202. * streams to/from native FLAC or Ogg FLAC and the single API can work
  203. * on both seekable and non-seekable streams.
  204. *
  205. * Instead of creating an encoder or decoder of a certain layer, now the
  206. * client will always create a FLAC__StreamEncoder or
  207. * FLAC__StreamDecoder. The old layers are now differentiated by the
  208. * initialization function. For example, for the decoder,
  209. * FLAC__stream_decoder_init() has been replaced by
  210. * FLAC__stream_decoder_init_stream(). This init function takes
  211. * callbacks for the I/O, and the seeking callbacks are optional. This
  212. * allows the client to use the same object for seekable and
  213. * non-seekable streams. For decoding a FLAC file directly, the client
  214. * can use FLAC__stream_decoder_init_file() and pass just a filename
  215. * and fewer callbacks; most of the other callbacks are supplied
  216. * internally. For situations where fopen()ing by filename is not
  217. * possible (e.g. Unicode filenames on Windows) the client can instead
  218. * open the file itself and supply the FILE* to
  219. * FLAC__stream_decoder_init_FILE(). The init functions now returns a
  220. * FLAC__StreamDecoderInitStatus instead of FLAC__StreamDecoderState.
  221. * Since the callbacks and client data are now passed to the init
  222. * function, the FLAC__stream_decoder_set_*_callback() functions and
  223. * FLAC__stream_decoder_set_client_data() are no longer needed. The
  224. * rest of the calls to the decoder are the same as before.
  225. *
  226. * There are counterpart init functions for Ogg FLAC, e.g.
  227. * FLAC__stream_decoder_init_ogg_stream(). All the rest of the calls
  228. * and callbacks are the same as for native FLAC.
  229. *
  230. * As an example, in FLAC 1.1.2 a seekable stream decoder would have
  231. * been set up like so:
  232. *
  233. * \code
  234. * FLAC__SeekableStreamDecoder *decoder = FLAC__seekable_stream_decoder_new();
  235. * if(decoder == NULL) do_something;
  236. * FLAC__seekable_stream_decoder_set_md5_checking(decoder, true);
  237. * [... other settings ...]
  238. * FLAC__seekable_stream_decoder_set_read_callback(decoder, my_read_callback);
  239. * FLAC__seekable_stream_decoder_set_seek_callback(decoder, my_seek_callback);
  240. * FLAC__seekable_stream_decoder_set_tell_callback(decoder, my_tell_callback);
  241. * FLAC__seekable_stream_decoder_set_length_callback(decoder, my_length_callback);
  242. * FLAC__seekable_stream_decoder_set_eof_callback(decoder, my_eof_callback);
  243. * FLAC__seekable_stream_decoder_set_write_callback(decoder, my_write_callback);
  244. * FLAC__seekable_stream_decoder_set_metadata_callback(decoder, my_metadata_callback);
  245. * FLAC__seekable_stream_decoder_set_error_callback(decoder, my_error_callback);
  246. * FLAC__seekable_stream_decoder_set_client_data(decoder, my_client_data);
  247. * if(FLAC__seekable_stream_decoder_init(decoder) != FLAC__SEEKABLE_STREAM_DECODER_OK) do_something;
  248. * \endcode
  249. *
  250. * In FLAC 1.1.3 it is like this:
  251. *
  252. * \code
  253. * FLAC__StreamDecoder *decoder = FLAC__stream_decoder_new();
  254. * if(decoder == NULL) do_something;
  255. * FLAC__stream_decoder_set_md5_checking(decoder, true);
  256. * [... other settings ...]
  257. * if(FLAC__stream_decoder_init_stream(
  258. * decoder,
  259. * my_read_callback,
  260. * my_seek_callback, // or NULL
  261. * my_tell_callback, // or NULL
  262. * my_length_callback, // or NULL
  263. * my_eof_callback, // or NULL
  264. * my_write_callback,
  265. * my_metadata_callback, // or NULL
  266. * my_error_callback,
  267. * my_client_data
  268. * ) != FLAC__STREAM_DECODER_INIT_STATUS_OK) do_something;
  269. * \endcode
  270. *
  271. * or you could do;
  272. *
  273. * \code
  274. * [...]
  275. * FILE *file = fopen("somefile.flac","rb");
  276. * if(file == NULL) do_somthing;
  277. * if(FLAC__stream_decoder_init_FILE(
  278. * decoder,
  279. * file,
  280. * my_write_callback,
  281. * my_metadata_callback, // or NULL
  282. * my_error_callback,
  283. * my_client_data
  284. * ) != FLAC__STREAM_DECODER_INIT_STATUS_OK) do_something;
  285. * \endcode
  286. *
  287. * or just:
  288. *
  289. * \code
  290. * [...]
  291. * if(FLAC__stream_decoder_init_file(
  292. * decoder,
  293. * "somefile.flac",
  294. * my_write_callback,
  295. * my_metadata_callback, // or NULL
  296. * my_error_callback,
  297. * my_client_data
  298. * ) != FLAC__STREAM_DECODER_INIT_STATUS_OK) do_something;
  299. * \endcode
  300. *
  301. * Another small change to the decoder is in how it handles unparseable
  302. * streams. Before, when the decoder found an unparseable stream
  303. * (reserved for when the decoder encounters a stream from a future
  304. * encoder that it can't parse), it changed the state to
  305. * \c FLAC__STREAM_DECODER_UNPARSEABLE_STREAM. Now the decoder instead
  306. * drops sync and calls the error callback with a new error code
  307. * \c FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM. This is
  308. * more robust. If your error callback does not discriminate on the the
  309. * error state, your code does not need to be changed.
  310. *
  311. * The encoder now has a new setting:
  312. * FLAC__stream_encoder_set_apodization(). This is for setting the
  313. * method used to window the data before LPC analysis. You only need to
  314. * add a call to this function if the default is not suitable. There
  315. * are also two new convenience functions that may be useful:
  316. * FLAC__metadata_object_cuesheet_calculate_cddb_id() and
  317. * FLAC__metadata_get_cuesheet().
  318. *
  319. * The \a bytes parameter to FLAC__StreamDecoderReadCallback,
  320. * FLAC__StreamEncoderReadCallback, and FLAC__StreamEncoderWriteCallback
  321. * is now \c size_t instead of \c uint32_t.
  322. */
  323. /** \defgroup porting_1_1_3_to_1_1_4 Porting from FLAC 1.1.3 to 1.1.4
  324. * \ingroup porting
  325. *
  326. * \brief
  327. * This module describes porting from FLAC 1.1.3 to FLAC 1.1.4.
  328. *
  329. * There were no changes to any of the interfaces from 1.1.3 to 1.1.4.
  330. * There was a slight change in the implementation of
  331. * FLAC__stream_encoder_set_metadata(); the function now makes a copy
  332. * of the \a metadata array of pointers so the client no longer needs
  333. * to maintain it after the call. The objects themselves that are
  334. * pointed to by the array are still not copied though and must be
  335. * maintained until the call to FLAC__stream_encoder_finish().
  336. */
  337. /** \defgroup porting_1_1_4_to_1_2_0 Porting from FLAC 1.1.4 to 1.2.0
  338. * \ingroup porting
  339. *
  340. * \brief
  341. * This module describes porting from FLAC 1.1.4 to FLAC 1.2.0.
  342. *
  343. * There were only very minor changes to the interfaces from 1.1.4 to 1.2.0.
  344. * In libFLAC, \c FLAC__format_sample_rate_is_subset() was added.
  345. * In libFLAC++, \c FLAC::Decoder::Stream::get_decode_position() was added.
  346. *
  347. * Finally, value of the constant \c FLAC__FRAME_HEADER_RESERVED_LEN
  348. * has changed to reflect the conversion of one of the reserved bits
  349. * into active use. It used to be \c 2 and now is \c 1. However the
  350. * FLAC frame header length has not changed, so to skip the proper
  351. * number of bits, use \c FLAC__FRAME_HEADER_RESERVED_LEN +
  352. * \c FLAC__FRAME_HEADER_BLOCKING_STRATEGY_LEN
  353. */
  354. /** \defgroup porting_1_3_4_to_1_4_0 Porting from FLAC 1.3.4 to 1.4.0
  355. * \ingroup porting
  356. *
  357. * \brief
  358. * This module describes porting from FLAC 1.3.4 to FLAC 1.4.0.
  359. *
  360. * \section porting_1_3_4_to_1_4_0_summary Summary
  361. *
  362. * Between FLAC 1.3.4 and FLAC 1.4.0, there have four breaking changes
  363. * - the function get_client_data_from_decoder has been renamed to
  364. * FLAC__get_decoder_client_data
  365. * - some data types in the FLAC__Frame struct have changed
  366. * - all functions resizing metadata blocks now return the object
  367. * untouched if memory allocation fails, whereas previously the
  368. * handling varied and was more or less undefined
  369. * - all functions accepting a filename now take UTF-8 encoded filenames
  370. * on Windows instead of filenames in the current codepage
  371. *
  372. * Furthermore, there have been the following additions
  373. * - the functions FLAC__stream_encoder_set_limit_min_bitrate,
  374. * FLAC__stream_encoder_get_limit_min_bitrate,
  375. * FLAC::encoder::file::set_limit_min_bitrate() and
  376. * FLAC::encoder::file::get_limit_min_bitrate() have been added
  377. * - Added FLAC__STREAM_DECODER_ERROR_STATUS_BAD_METADATA to the
  378. * FLAC__StreamDecoderErrorStatus enum
  379. *
  380. * \section porting_1_3_4_to_1_4_0_breaking Breaking changes
  381. *
  382. * The function \b get_client_data_from_decoder was added in FLAC 1.3.3
  383. * but did not follow the API naming convention and was not properly
  384. * exported. The function is now renamed and properly integrated as
  385. * FLAC__stream_decoder_get_client_data
  386. *
  387. * To accomodate encoding and decoding 32-bit int PCM, some data types
  388. * in the \b FLAC__frame struct were changed. Specifically, warmup
  389. * in both the FLAC__Subframe_Fixed struc and the FLAC__Subframe_LPC
  390. * struct is changed from FLAC__int32 to FLAC__int64. Also, value
  391. * in the FLAC__Subframe_Constant is changed from FLAC__int32 to
  392. * FLAC__int64. Finally, in FLAC__Subframe_Verbatim struct data is
  393. * changes from a FLAC__int32 array to a union containing a FLAC__int32
  394. * array and a FLAC__int64 array. Also, a new member is added,
  395. * data_type, which clarifies whether the FLAC__int32 or FLAC__int64
  396. * array is in use.
  397. *
  398. * Furthermore, the following functions now return the object untouched
  399. * if memory allocation fails, whereas previously the handling varied
  400. * and was more or less undefined
  401. *
  402. * - FLAC__metadata_object_seektable_resize_points
  403. * - FLAC__metadata_object_vorbiscomment_resize_comments
  404. * - FLAC__metadata_object_cuesheet_track_resize_indices
  405. * - FLAC__metadata_object_cuesheet_resize_tracks
  406. *
  407. * The last breaking change is that all API functions taking a filename
  408. * as an argument now, on Windows, must be supplied with that filename
  409. * in the UTF-8 character encoding instead of using the current code
  410. * page. libFLAC internally translates these UTF-8 encoded filenames to
  411. * an appropriate representation to use with _wfopen. On all other
  412. * systems, filename is passed to fopen without any translation, as it
  413. * in libFLAC 1.3.4 and earlier.
  414. *
  415. * \section porting_1_3_4_to_1_4_0_additions Additions
  416. *
  417. * To aid in creating properly streamable FLAC files, a set of functions
  418. * was added to make it possible to enfore a minimum bitrate to files
  419. * created through libFLAC's stream_encoder.h interface. With this
  420. * function enabled the resulting FLAC files have a minimum bitrate of
  421. * 1bit/sample independent of the number of channels, i.e. 48kbit/s for
  422. * 48kHz. This can be beneficial for streaming, as very low bitrates for
  423. * silent sections compressed with 'constant' subframes can result in a
  424. * bitrate of 1kbit/s, creating problems with clients that aren't aware
  425. * of this possibility and buffer too much data.
  426. *
  427. * Finally, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_METADATA was added to
  428. * the FLAC__StreamDecoderErrorStatus enum to signal that the decoder
  429. * encountered unreadable metadata.
  430. *
  431. */
  432. /** \defgroup flac FLAC C API
  433. *
  434. * The FLAC C API is the interface to libFLAC, a set of structures
  435. * describing the components of FLAC streams, and functions for
  436. * encoding and decoding streams, as well as manipulating FLAC
  437. * metadata in files.
  438. *
  439. * You should start with the format components as all other modules
  440. * are dependent on it.
  441. */
  442. #endif