Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2. FLAC audio decoder. Choice of public domain or MIT-0. See license statements at the end of this file.
  3. dr_flac - v0.12.13 - 2020-05-16
  4.  
  5. David Reid - mackron@gmail.com
  6.  
  7. GitHub: https://github.com/mackron/dr_libs
  8. */
  9.  
  10. /*
  11. RELEASE NOTES - v0.12.0
  12. =======================
  13. Version 0.12.0 has breaking API changes including changes to the existing API and the removal of deprecated APIs.
  14.  
  15.  
  16. Improved Client-Defined Memory Allocation
  17. -----------------------------------------
  18. The main change with this release is the addition of a more flexible way of implementing custom memory allocation routines. The
  19. existing system of DRFLAC_MALLOC, DRFLAC_REALLOC and DRFLAC_FREE are still in place and will be used by default when no custom
  20. allocation callbacks are specified.
  21.  
  22. To use the new system, you pass in a pointer to a drflac_allocation_callbacks object to drflac_open() and family, like this:
  23.  
  24.     void* my_malloc(size_t sz, void* pUserData)
  25.     {
  26.         return malloc(sz);
  27.     }
  28.     void* my_realloc(void* p, size_t sz, void* pUserData)
  29.     {
  30.         return realloc(p, sz);
  31.     }
  32.     void my_free(void* p, void* pUserData)
  33.     {
  34.         free(p);
  35.     }
  36.  
  37.     ...
  38.  
  39.     drflac_allocation_callbacks allocationCallbacks;
  40.     allocationCallbacks.pUserData = &myData;
  41.     allocationCallbacks.onMalloc  = my_malloc;
  42.     allocationCallbacks.onRealloc = my_realloc;
  43.     allocationCallbacks.onFree    = my_free;
  44.     drflac* pFlac = drflac_open_file("my_file.flac", &allocationCallbacks);
  45.  
  46. The advantage of this new system is that it allows you to specify user data which will be passed in to the allocation routines.
  47.  
  48. Passing in null for the allocation callbacks object will cause dr_flac to use defaults which is the same as DRFLAC_MALLOC,
  49. DRFLAC_REALLOC and DRFLAC_FREE and the equivalent of how it worked in previous versions.
  50.  
  51. Every API that opens a drflac object now takes this extra parameter. These include the following:
  52.  
  53.     drflac_open()
  54.     drflac_open_relaxed()
  55.     drflac_open_with_metadata()
  56.     drflac_open_with_metadata_relaxed()
  57.     drflac_open_file()
  58.     drflac_open_file_with_metadata()
  59.     drflac_open_memory()
  60.     drflac_open_memory_with_metadata()
  61.     drflac_open_and_read_pcm_frames_s32()
  62.     drflac_open_and_read_pcm_frames_s16()
  63.     drflac_open_and_read_pcm_frames_f32()
  64.     drflac_open_file_and_read_pcm_frames_s32()
  65.     drflac_open_file_and_read_pcm_frames_s16()
  66.     drflac_open_file_and_read_pcm_frames_f32()
  67.     drflac_open_memory_and_read_pcm_frames_s32()
  68.     drflac_open_memory_and_read_pcm_frames_s16()
  69.     drflac_open_memory_and_read_pcm_frames_f32()
  70.  
  71.  
  72.  
  73. Optimizations
  74. -------------
  75. Seeking performance has been greatly improved. A new binary search based seeking algorithm has been introduced which significantly
  76. improves performance over the brute force method which was used when no seek table was present. Seek table based seeking also takes
  77. advantage of the new binary search seeking system to further improve performance there as well. Note that this depends on CRC which
  78. means it will be disabled when DR_FLAC_NO_CRC is used.
  79.  
  80. The SSE4.1 pipeline has been cleaned up and optimized. You should see some improvements with decoding speed of 24-bit files in
  81. particular. 16-bit streams should also see some improvement.
  82.  
  83. drflac_read_pcm_frames_s16() has been optimized. Previously this sat on top of drflac_read_pcm_frames_s32() and performed it's s32
  84. to s16 conversion in a second pass. This is now all done in a single pass. This includes SSE2 and ARM NEON optimized paths.
  85.  
  86. A minor optimization has been implemented for drflac_read_pcm_frames_s32(). This will now use an SSE2 optimized pipeline for stereo
  87. channel reconstruction which is the last part of the decoding process.
  88.  
  89. The ARM build has seen a few improvements. The CLZ (count leading zeroes) and REV (byte swap) instructions are now used when
  90. compiling with GCC and Clang which is achieved using inline assembly. The CLZ instruction requires ARM architecture version 5 at
  91. compile time and the REV instruction requires ARM architecture version 6.
  92.  
  93. An ARM NEON optimized pipeline has been implemented. To enable this you'll need to add -mfpu=neon to the command line when compiling.
  94.  
  95.  
  96. Removed APIs
  97. ------------
  98. The following APIs were deprecated in version 0.11.0 and have been completely removed in version 0.12.0:
  99.  
  100.     drflac_read_s32()                   -> drflac_read_pcm_frames_s32()
  101.     drflac_read_s16()                   -> drflac_read_pcm_frames_s16()
  102.     drflac_read_f32()                   -> drflac_read_pcm_frames_f32()
  103.     drflac_seek_to_sample()             -> drflac_seek_to_pcm_frame()
  104.     drflac_open_and_decode_s32()        -> drflac_open_and_read_pcm_frames_s32()
  105.     drflac_open_and_decode_s16()        -> drflac_open_and_read_pcm_frames_s16()
  106.     drflac_open_and_decode_f32()        -> drflac_open_and_read_pcm_frames_f32()
  107.     drflac_open_and_decode_file_s32()   -> drflac_open_file_and_read_pcm_frames_s32()
  108.     drflac_open_and_decode_file_s16()   -> drflac_open_file_and_read_pcm_frames_s16()
  109.     drflac_open_and_decode_file_f32()   -> drflac_open_file_and_read_pcm_frames_f32()
  110.     drflac_open_and_decode_memory_s32() -> drflac_open_memory_and_read_pcm_frames_s32()
  111.     drflac_open_and_decode_memory_s16() -> drflac_open_memory_and_read_pcm_frames_s16()
  112.     drflac_open_and_decode_memory_f32() -> drflac_open_memroy_and_read_pcm_frames_f32()
  113.  
  114. Prior versions of dr_flac operated on a per-sample basis whereas now it operates on PCM frames. The removed APIs all relate
  115. to the old per-sample APIs. You now need to use the "pcm_frame" versions.
  116. */
  117.  
  118.  
  119. /*
  120. Introduction
  121. ============
  122. dr_flac is a single file library. To use it, do something like the following in one .c file.
  123.  
  124.     ```c
  125.     #define DR_FLAC_IMPLEMENTATION
  126.     #include "dr_flac.h"
  127.     ```
  128.  
  129. You can then #include this file in other parts of the program as you would with any other header file. To decode audio data, do something like the following:
  130.  
  131.     ```c
  132.     drflac* pFlac = drflac_open_file("MySong.flac", NULL);
  133.     if (pFlac == NULL) {
  134.         // Failed to open FLAC file
  135.     }
  136.  
  137.     drflac_int32* pSamples = malloc(pFlac->totalPCMFrameCount * pFlac->channels * sizeof(drflac_int32));
  138.     drflac_uint64 numberOfInterleavedSamplesActuallyRead = drflac_read_pcm_frames_s32(pFlac, pFlac->totalPCMFrameCount, pSamples);
  139.     ```
  140.  
  141. The drflac object represents the decoder. It is a transparent type so all the information you need, such as the number of channels and the bits per sample,
  142. should be directly accessible - just make sure you don't change their values. Samples are always output as interleaved signed 32-bit PCM. In the example above
  143. a native FLAC stream was opened, however dr_flac has seamless support for Ogg encapsulated FLAC streams as well.
  144.  
  145. You do not need to decode the entire stream in one go - you just specify how many samples you'd like at any given time and the decoder will give you as many
  146. samples as it can, up to the amount requested. Later on when you need the next batch of samples, just call it again. Example:
  147.  
  148.     ```c
  149.     while (drflac_read_pcm_frames_s32(pFlac, chunkSizeInPCMFrames, pChunkSamples) > 0) {
  150.         do_something();
  151.     }
  152.     ```
  153.  
  154. You can seek to a specific PCM frame with `drflac_seek_to_pcm_frame()`.
  155.  
  156. If you just want to quickly decode an entire FLAC file in one go you can do something like this:
  157.  
  158.     ```c
  159.     unsigned int channels;
  160.     unsigned int sampleRate;
  161.     drflac_uint64 totalPCMFrameCount;
  162.     drflac_int32* pSampleData = drflac_open_file_and_read_pcm_frames_s32("MySong.flac", &channels, &sampleRate, &totalPCMFrameCount, NULL);
  163.     if (pSampleData == NULL) {
  164.         // Failed to open and decode FLAC file.
  165.     }
  166.  
  167.     ...
  168.  
  169.     drflac_free(pSampleData);
  170.     ```
  171.  
  172. You can read samples as signed 16-bit integer and 32-bit floating-point PCM with the *_s16() and *_f32() family of APIs respectively, but note that these
  173. should be considered lossy.
  174.  
  175.  
  176. If you need access to metadata (album art, etc.), use `drflac_open_with_metadata()`, `drflac_open_file_with_metdata()` or `drflac_open_memory_with_metadata()`.
  177. The rationale for keeping these APIs separate is that they're slightly slower than the normal versions and also just a little bit harder to use. dr_flac
  178. reports metadata to the application through the use of a callback, and every metadata block is reported before `drflac_open_with_metdata()` returns.
  179.  
  180. The main opening APIs (`drflac_open()`, etc.) will fail if the header is not present. The presents a problem in certain scenarios such as broadcast style
  181. streams or internet radio where the header may not be present because the user has started playback mid-stream. To handle this, use the relaxed APIs:
  182.    
  183.     `drflac_open_relaxed()`
  184.     `drflac_open_with_metadata_relaxed()`
  185.  
  186. It is not recommended to use these APIs for file based streams because a missing header would usually indicate a corrupt or perverse file. In addition, these
  187. APIs can take a long time to initialize because they may need to spend a lot of time finding the first frame.
  188.  
  189.  
  190.  
  191. Build Options
  192. =============
  193. #define these options before including this file.
  194.  
  195. #define DR_FLAC_NO_STDIO
  196.   Disable `drflac_open_file()` and family.
  197.  
  198. #define DR_FLAC_NO_OGG
  199.   Disables support for Ogg/FLAC streams.
  200.  
  201. #define DR_FLAC_BUFFER_SIZE <number>
  202.   Defines the size of the internal buffer to store data from onRead(). This buffer is used to reduce the number of calls back to the client for more data.
  203.   Larger values means more memory, but better performance. My tests show diminishing returns after about 4KB (which is the default). Consider reducing this if
  204.   you have a very efficient implementation of onRead(), or increase it if it's very inefficient. Must be a multiple of 8.
  205.  
  206. #define DR_FLAC_NO_CRC
  207.   Disables CRC checks. This will offer a performance boost when CRC is unnecessary. This will disable binary search seeking. When seeking, the seek table will
  208.   be used if available. Otherwise the seek will be performed using brute force.
  209.  
  210. #define DR_FLAC_NO_SIMD
  211.   Disables SIMD optimizations (SSE on x86/x64 architectures, NEON on ARM architectures). Use this if you are having compatibility issues with your compiler.
  212.  
  213.  
  214.  
  215. Notes
  216. =====
  217. - dr_flac does not support changing the sample rate nor channel count mid stream.
  218. - dr_flac is not thread-safe, but its APIs can be called from any thread so long as you do your own synchronization.
  219. - When using Ogg encapsulation, a corrupted metadata block will result in `drflac_open_with_metadata()` and `drflac_open()` returning inconsistent samples due
  220.   to differences in corrupted stream recorvery logic between the two APIs.
  221. */
  222.  
  223. #ifndef dr_flac_h
  224. #define dr_flac_h
  225.  
  226. #ifdef __cplusplus
  227. extern "C" {
  228. #endif
  229.  
  230. #define DRFLAC_STRINGIFY(x)      #x
  231. #define DRFLAC_XSTRINGIFY(x)     DRFLAC_STRINGIFY(x)
  232.  
  233. #define DRFLAC_VERSION_MAJOR     0
  234. #define DRFLAC_VERSION_MINOR     12
  235. #define DRFLAC_VERSION_REVISION  13
  236. #define DRFLAC_VERSION_STRING    DRFLAC_XSTRINGIFY(DRFLAC_VERSION_MAJOR) "." DRFLAC_XSTRINGIFY(DRFLAC_VERSION_MINOR) "." DRFLAC_XSTRINGIFY(DRFLAC_VERSION_REVISION)
  237.  
  238. #include <stddef.h> /* For size_t. */
  239.  
  240. /* Sized types. Prefer built-in types. Fall back to stdint. */
  241. #ifdef _MSC_VER
  242.     #if defined(__clang__)
  243.         #pragma GCC diagnostic push
  244.         #pragma GCC diagnostic ignored "-Wlanguage-extension-token"
  245.         #pragma GCC diagnostic ignored "-Wlong-long"        
  246.         #pragma GCC diagnostic ignored "-Wc++11-long-long"
  247.     #endif
  248.     typedef   signed __int8  drflac_int8;
  249.     typedef unsigned __int8  drflac_uint8;
  250.     typedef   signed __int16 drflac_int16;
  251.     typedef unsigned __int16 drflac_uint16;
  252.     typedef   signed __int32 drflac_int32;
  253.     typedef unsigned __int32 drflac_uint32;
  254.     typedef   signed __int64 drflac_int64;
  255.     typedef unsigned __int64 drflac_uint64;
  256.     #if defined(__clang__)
  257.         #pragma GCC diagnostic pop
  258.     #endif
  259. #else
  260.     #include <stdint.h>
  261.     typedef int8_t           drflac_int8;
  262.     typedef uint8_t          drflac_uint8;
  263.     typedef int16_t          drflac_int16;
  264.     typedef uint16_t         drflac_uint16;
  265.     typedef int32_t          drflac_int32;
  266.     typedef uint32_t         drflac_uint32;
  267.     typedef int64_t          drflac_int64;
  268.     typedef uint64_t         drflac_uint64;
  269. #endif
  270. typedef drflac_uint8         drflac_bool8;
  271. typedef drflac_uint32        drflac_bool32;
  272. #define DRFLAC_TRUE          1
  273. #define DRFLAC_FALSE         0
  274.  
  275. #if !defined(DRFLAC_API)
  276.     #if defined(DRFLAC_DLL)
  277.         #if defined(_WIN32)
  278.             #define DRFLAC_DLL_IMPORT  __declspec(dllimport)
  279.             #define DRFLAC_DLL_EXPORT  __declspec(dllexport)
  280.             #define DRFLAC_DLL_PRIVATE static
  281.         #else
  282.             #if defined(__GNUC__) && __GNUC__ >= 4
  283.                 #define DRFLAC_DLL_IMPORT  __attribute__((visibility("default")))
  284.                 #define DRFLAC_DLL_EXPORT  __attribute__((visibility("default")))
  285.                 #define DRFLAC_DLL_PRIVATE __attribute__((visibility("hidden")))
  286.             #else
  287.                 #define DRFLAC_DLL_IMPORT
  288.                 #define DRFLAC_DLL_EXPORT
  289.                 #define DRFLAC_DLL_PRIVATE static
  290.             #endif
  291.         #endif
  292.  
  293.         #if defined(DR_FLAC_IMPLEMENTATION) || defined(DRFLAC_IMPLEMENTATION)
  294.             #define DRFLAC_API  DRFLAC_DLL_EXPORT
  295.         #else
  296.             #define DRFLAC_API  DRFLAC_DLL_IMPORT
  297.         #endif
  298.         #define DRFLAC_PRIVATE DRFLAC_DLL_PRIVATE
  299.     #else
  300.         #define DRFLAC_API extern
  301.         #define DRFLAC_PRIVATE static
  302.     #endif
  303. #endif
  304.  
  305. #if defined(_MSC_VER) && _MSC_VER >= 1700   /* Visual Studio 2012 */
  306.     #define DRFLAC_DEPRECATED       __declspec(deprecated)
  307. #elif (defined(__GNUC__) && __GNUC__ >= 4)  /* GCC 4 */
  308.     #define DRFLAC_DEPRECATED       __attribute__((deprecated))
  309. #elif defined(__has_feature)                /* Clang */
  310.     #if __has_feature(attribute_deprecated)
  311.         #define DRFLAC_DEPRECATED   __attribute__((deprecated))
  312.     #else
  313.         #define DRFLAC_DEPRECATED
  314.     #endif
  315. #else
  316.     #define DRFLAC_DEPRECATED
  317. #endif
  318.  
  319. DRFLAC_API void drflac_version(drflac_uint32* pMajor, drflac_uint32* pMinor, drflac_uint32* pRevision);
  320. DRFLAC_API const char* drflac_version_string();
  321.  
  322. /*
  323. As data is read from the client it is placed into an internal buffer for fast access. This controls the size of that buffer. Larger values means more speed,
  324. but also more memory. In my testing there is diminishing returns after about 4KB, but you can fiddle with this to suit your own needs. Must be a multiple of 8.
  325. */
  326. #ifndef DR_FLAC_BUFFER_SIZE
  327. #define DR_FLAC_BUFFER_SIZE   4096
  328. #endif
  329.  
  330. /* Check if we can enable 64-bit optimizations. */
  331. #if defined(_WIN64) || defined(_LP64) || defined(__LP64__)
  332. #define DRFLAC_64BIT
  333. #endif
  334.  
  335. #ifdef DRFLAC_64BIT
  336. typedef drflac_uint64 drflac_cache_t;
  337. #else
  338. typedef drflac_uint32 drflac_cache_t;
  339. #endif
  340.  
  341. /* The various metadata block types. */
  342. #define DRFLAC_METADATA_BLOCK_TYPE_STREAMINFO       0
  343. #define DRFLAC_METADATA_BLOCK_TYPE_PADDING          1
  344. #define DRFLAC_METADATA_BLOCK_TYPE_APPLICATION      2
  345. #define DRFLAC_METADATA_BLOCK_TYPE_SEEKTABLE        3
  346. #define DRFLAC_METADATA_BLOCK_TYPE_VORBIS_COMMENT   4
  347. #define DRFLAC_METADATA_BLOCK_TYPE_CUESHEET         5
  348. #define DRFLAC_METADATA_BLOCK_TYPE_PICTURE          6
  349. #define DRFLAC_METADATA_BLOCK_TYPE_INVALID          127
  350.  
  351. /* The various picture types specified in the PICTURE block. */
  352. #define DRFLAC_PICTURE_TYPE_OTHER                   0
  353. #define DRFLAC_PICTURE_TYPE_FILE_ICON               1
  354. #define DRFLAC_PICTURE_TYPE_OTHER_FILE_ICON         2
  355. #define DRFLAC_PICTURE_TYPE_COVER_FRONT             3
  356. #define DRFLAC_PICTURE_TYPE_COVER_BACK              4
  357. #define DRFLAC_PICTURE_TYPE_LEAFLET_PAGE            5
  358. #define DRFLAC_PICTURE_TYPE_MEDIA                   6
  359. #define DRFLAC_PICTURE_TYPE_LEAD_ARTIST             7
  360. #define DRFLAC_PICTURE_TYPE_ARTIST                  8
  361. #define DRFLAC_PICTURE_TYPE_CONDUCTOR               9
  362. #define DRFLAC_PICTURE_TYPE_BAND                    10
  363. #define DRFLAC_PICTURE_TYPE_COMPOSER                11
  364. #define DRFLAC_PICTURE_TYPE_LYRICIST                12
  365. #define DRFLAC_PICTURE_TYPE_RECORDING_LOCATION      13
  366. #define DRFLAC_PICTURE_TYPE_DURING_RECORDING        14
  367. #define DRFLAC_PICTURE_TYPE_DURING_PERFORMANCE      15
  368. #define DRFLAC_PICTURE_TYPE_SCREEN_CAPTURE          16
  369. #define DRFLAC_PICTURE_TYPE_BRIGHT_COLORED_FISH     17
  370. #define DRFLAC_PICTURE_TYPE_ILLUSTRATION            18
  371. #define DRFLAC_PICTURE_TYPE_BAND_LOGOTYPE           19
  372. #define DRFLAC_PICTURE_TYPE_PUBLISHER_LOGOTYPE      20
  373.  
  374. typedef enum
  375. {
  376.     drflac_container_native,
  377.     drflac_container_ogg,
  378.     drflac_container_unknown
  379. } drflac_container;
  380.  
  381. typedef enum
  382. {
  383.     drflac_seek_origin_start,
  384.     drflac_seek_origin_current
  385. } drflac_seek_origin;
  386.  
  387. /* Packing is important on this structure because we map this directly to the raw data within the SEEKTABLE metadata block. */
  388. #pragma pack(2)
  389. typedef struct
  390. {
  391.     drflac_uint64 firstPCMFrame;
  392.     drflac_uint64 flacFrameOffset;   /* The offset from the first byte of the header of the first frame. */
  393.     drflac_uint16 pcmFrameCount;
  394. } drflac_seekpoint;
  395. #pragma pack()
  396.  
  397. typedef struct
  398. {
  399.     drflac_uint16 minBlockSizeInPCMFrames;
  400.     drflac_uint16 maxBlockSizeInPCMFrames;
  401.     drflac_uint32 minFrameSizeInPCMFrames;
  402.     drflac_uint32 maxFrameSizeInPCMFrames;
  403.     drflac_uint32 sampleRate;
  404.     drflac_uint8  channels;
  405.     drflac_uint8  bitsPerSample;
  406.     drflac_uint64 totalPCMFrameCount;
  407.     drflac_uint8  md5[16];
  408. } drflac_streaminfo;
  409.  
  410. typedef struct
  411. {
  412.     /* The metadata type. Use this to know how to interpret the data below. */
  413.     drflac_uint32 type;
  414.  
  415.     /*
  416.     A pointer to the raw data. This points to a temporary buffer so don't hold on to it. It's best to
  417.     not modify the contents of this buffer. Use the structures below for more meaningful and structured
  418.     information about the metadata. It's possible for this to be null.
  419.     */
  420.     const void* pRawData;
  421.  
  422.     /* The size in bytes of the block and the buffer pointed to by pRawData if it's non-NULL. */
  423.     drflac_uint32 rawDataSize;
  424.  
  425.     union
  426.     {
  427.         drflac_streaminfo streaminfo;
  428.  
  429.         struct
  430.         {
  431.             int unused;
  432.         } padding;
  433.  
  434.         struct
  435.         {
  436.             drflac_uint32 id;
  437.             const void* pData;
  438.             drflac_uint32 dataSize;
  439.         } application;
  440.  
  441.         struct
  442.         {
  443.             drflac_uint32 seekpointCount;
  444.             const drflac_seekpoint* pSeekpoints;
  445.         } seektable;
  446.  
  447.         struct
  448.         {
  449.             drflac_uint32 vendorLength;
  450.             const char* vendor;
  451.             drflac_uint32 commentCount;
  452.             const void* pComments;
  453.         } vorbis_comment;
  454.  
  455.         struct
  456.         {
  457.             char catalog[128];
  458.             drflac_uint64 leadInSampleCount;
  459.             drflac_bool32 isCD;
  460.             drflac_uint8 trackCount;
  461.             const void* pTrackData;
  462.         } cuesheet;
  463.  
  464.         struct
  465.         {
  466.             drflac_uint32 type;
  467.             drflac_uint32 mimeLength;
  468.             const char* mime;
  469.             drflac_uint32 descriptionLength;
  470.             const char* description;
  471.             drflac_uint32 width;
  472.             drflac_uint32 height;
  473.             drflac_uint32 colorDepth;
  474.             drflac_uint32 indexColorCount;
  475.             drflac_uint32 pictureDataSize;
  476.             const drflac_uint8* pPictureData;
  477.         } picture;
  478.     } data;
  479. } drflac_metadata;
  480.  
  481.  
  482. /*
  483. Callback for when data needs to be read from the client.
  484.  
  485.  
  486. Parameters
  487. ----------
  488. pUserData (in)
  489.     The user data that was passed to drflac_open() and family.
  490.  
  491. pBufferOut (out)
  492.     The output buffer.
  493.  
  494. bytesToRead (in)
  495.     The number of bytes to read.
  496.  
  497.  
  498. Return Value
  499. ------------
  500. The number of bytes actually read.
  501.  
  502.  
  503. Remarks
  504. -------
  505. A return value of less than bytesToRead indicates the end of the stream. Do _not_ return from this callback until either the entire bytesToRead is filled or
  506. you have reached the end of the stream.
  507. */
  508. typedef size_t (* drflac_read_proc)(void* pUserData, void* pBufferOut, size_t bytesToRead);
  509.  
  510. /*
  511. Callback for when data needs to be seeked.
  512.  
  513.  
  514. Parameters
  515. ----------
  516. pUserData (in)
  517.     The user data that was passed to drflac_open() and family.
  518.  
  519. offset (in)
  520.     The number of bytes to move, relative to the origin. Will never be negative.
  521.  
  522. origin (in)
  523.     The origin of the seek - the current position or the start of the stream.
  524.  
  525.  
  526. Return Value
  527. ------------
  528. Whether or not the seek was successful.
  529.  
  530.  
  531. Remarks
  532. -------
  533. The offset will never be negative. Whether or not it is relative to the beginning or current position is determined by the "origin" parameter which will be
  534. either drflac_seek_origin_start or drflac_seek_origin_current.
  535.  
  536. When seeking to a PCM frame using drflac_seek_to_pcm_frame(), dr_flac may call this with an offset beyond the end of the FLAC stream. This needs to be detected
  537. and handled by returning DRFLAC_FALSE.
  538. */
  539. typedef drflac_bool32 (* drflac_seek_proc)(void* pUserData, int offset, drflac_seek_origin origin);
  540.  
  541. /*
  542. Callback for when a metadata block is read.
  543.  
  544.  
  545. Parameters
  546. ----------
  547. pUserData (in)
  548.     The user data that was passed to drflac_open() and family.
  549.  
  550. pMetadata (in)
  551.     A pointer to a structure containing the data of the metadata block.
  552.  
  553.  
  554. Remarks
  555. -------
  556. Use pMetadata->type to determine which metadata block is being handled and how to read the data.
  557. */
  558. typedef void (* drflac_meta_proc)(void* pUserData, drflac_metadata* pMetadata);
  559.  
  560.  
  561. typedef struct
  562. {
  563.     void* pUserData;
  564.     void* (* onMalloc)(size_t sz, void* pUserData);
  565.     void* (* onRealloc)(void* p, size_t sz, void* pUserData);
  566.     void  (* onFree)(void* p, void* pUserData);
  567. } drflac_allocation_callbacks;
  568.  
  569. /* Structure for internal use. Only used for decoders opened with drflac_open_memory. */
  570. typedef struct
  571. {
  572.     const drflac_uint8* data;
  573.     size_t dataSize;
  574.     size_t currentReadPos;
  575. } drflac__memory_stream;
  576.  
  577. /* Structure for internal use. Used for bit streaming. */
  578. typedef struct
  579. {
  580.     /* The function to call when more data needs to be read. */
  581.     drflac_read_proc onRead;
  582.  
  583.     /* The function to call when the current read position needs to be moved. */
  584.     drflac_seek_proc onSeek;
  585.  
  586.     /* The user data to pass around to onRead and onSeek. */
  587.     void* pUserData;
  588.  
  589.  
  590.     /*
  591.     The number of unaligned bytes in the L2 cache. This will always be 0 until the end of the stream is hit. At the end of the
  592.     stream there will be a number of bytes that don't cleanly fit in an L1 cache line, so we use this variable to know whether
  593.     or not the bistreamer needs to run on a slower path to read those last bytes. This will never be more than sizeof(drflac_cache_t).
  594.     */
  595.     size_t unalignedByteCount;
  596.  
  597.     /* The content of the unaligned bytes. */
  598.     drflac_cache_t unalignedCache;
  599.  
  600.     /* The index of the next valid cache line in the "L2" cache. */
  601.     drflac_uint32 nextL2Line;
  602.  
  603.     /* The number of bits that have been consumed by the cache. This is used to determine how many valid bits are remaining. */
  604.     drflac_uint32 consumedBits;
  605.  
  606.     /*
  607.     The cached data which was most recently read from the client. There are two levels of cache. Data flows as such:
  608.     Client -> L2 -> L1. The L2 -> L1 movement is aligned and runs on a fast path in just a few instructions.
  609.     */
  610.     drflac_cache_t cacheL2[DR_FLAC_BUFFER_SIZE/sizeof(drflac_cache_t)];
  611.     drflac_cache_t cache;
  612.  
  613.     /*
  614.     CRC-16. This is updated whenever bits are read from the bit stream. Manually set this to 0 to reset the CRC. For FLAC, this
  615.     is reset to 0 at the beginning of each frame.
  616.     */
  617.     drflac_uint16 crc16;
  618.     drflac_cache_t crc16Cache;              /* A cache for optimizing CRC calculations. This is filled when when the L1 cache is reloaded. */
  619.     drflac_uint32 crc16CacheIgnoredBytes;   /* The number of bytes to ignore when updating the CRC-16 from the CRC-16 cache. */
  620. } drflac_bs;
  621.  
  622. typedef struct
  623. {
  624.     /* The type of the subframe: SUBFRAME_CONSTANT, SUBFRAME_VERBATIM, SUBFRAME_FIXED or SUBFRAME_LPC. */
  625.     drflac_uint8 subframeType;
  626.  
  627.     /* The number of wasted bits per sample as specified by the sub-frame header. */
  628.     drflac_uint8 wastedBitsPerSample;
  629.  
  630.     /* The order to use for the prediction stage for SUBFRAME_FIXED and SUBFRAME_LPC. */
  631.     drflac_uint8 lpcOrder;
  632.  
  633.     /* A pointer to the buffer containing the decoded samples in the subframe. This pointer is an offset from drflac::pExtraData. */
  634.     drflac_int32* pSamplesS32;
  635. } drflac_subframe;
  636.  
  637. typedef struct
  638. {
  639.     /*
  640.     If the stream uses variable block sizes, this will be set to the index of the first PCM frame. If fixed block sizes are used, this will
  641.     always be set to 0. This is 64-bit because the decoded PCM frame number will be 36 bits.
  642.     */
  643.     drflac_uint64 pcmFrameNumber;
  644.  
  645.     /*
  646.     If the stream uses fixed block sizes, this will be set to the frame number. If variable block sizes are used, this will always be 0. This
  647.     is 32-bit because in fixed block sizes, the maximum frame number will be 31 bits.
  648.     */
  649.     drflac_uint32 flacFrameNumber;
  650.  
  651.     /* The sample rate of this frame. */
  652.     drflac_uint32 sampleRate;
  653.  
  654.     /* The number of PCM frames in each sub-frame within this frame. */
  655.     drflac_uint16 blockSizeInPCMFrames;
  656.  
  657.     /*
  658.     The channel assignment of this frame. This is not always set to the channel count. If interchannel decorrelation is being used this
  659.     will be set to DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE, DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE or DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE.
  660.     */
  661.     drflac_uint8 channelAssignment;
  662.  
  663.     /* The number of bits per sample within this frame. */
  664.     drflac_uint8 bitsPerSample;
  665.  
  666.     /* The frame's CRC. */
  667.     drflac_uint8 crc8;
  668. } drflac_frame_header;
  669.  
  670. typedef struct
  671. {
  672.     /* The header. */
  673.     drflac_frame_header header;
  674.  
  675.     /*
  676.     The number of PCM frames left to be read in this FLAC frame. This is initially set to the block size. As PCM frames are read,
  677.     this will be decremented. When it reaches 0, the decoder will see this frame as fully consumed and load the next frame.
  678.     */
  679.     drflac_uint32 pcmFramesRemaining;
  680.  
  681.     /* The list of sub-frames within the frame. There is one sub-frame for each channel, and there's a maximum of 8 channels. */
  682.     drflac_subframe subframes[8];
  683. } drflac_frame;
  684.  
  685. typedef struct
  686. {
  687.     /* The function to call when a metadata block is read. */
  688.     drflac_meta_proc onMeta;
  689.  
  690.     /* The user data posted to the metadata callback function. */
  691.     void* pUserDataMD;
  692.  
  693.     /* Memory allocation callbacks. */
  694.     drflac_allocation_callbacks allocationCallbacks;
  695.  
  696.  
  697.     /* The sample rate. Will be set to something like 44100. */
  698.     drflac_uint32 sampleRate;
  699.  
  700.     /*
  701.     The number of channels. This will be set to 1 for monaural streams, 2 for stereo, etc. Maximum 8. This is set based on the
  702.     value specified in the STREAMINFO block.
  703.     */
  704.     drflac_uint8 channels;
  705.  
  706.     /* The bits per sample. Will be set to something like 16, 24, etc. */
  707.     drflac_uint8 bitsPerSample;
  708.  
  709.     /* The maximum block size, in samples. This number represents the number of samples in each channel (not combined). */
  710.     drflac_uint16 maxBlockSizeInPCMFrames;
  711.  
  712.     /*
  713.     The total number of PCM Frames making up the stream. Can be 0 in which case it's still a valid stream, but just means
  714.     the total PCM frame count is unknown. Likely the case with streams like internet radio.
  715.     */
  716.     drflac_uint64 totalPCMFrameCount;
  717.  
  718.  
  719.     /* The container type. This is set based on whether or not the decoder was opened from a native or Ogg stream. */
  720.     drflac_container container;
  721.  
  722.     /* The number of seekpoints in the seektable. */
  723.     drflac_uint32 seekpointCount;
  724.  
  725.  
  726.     /* Information about the frame the decoder is currently sitting on. */
  727.     drflac_frame currentFLACFrame;
  728.  
  729.  
  730.     /* The index of the PCM frame the decoder is currently sitting on. This is only used for seeking. */
  731.     drflac_uint64 currentPCMFrame;
  732.  
  733.     /* The position of the first FLAC frame in the stream. This is only ever used for seeking. */
  734.     drflac_uint64 firstFLACFramePosInBytes;
  735.  
  736.  
  737.     /* A hack to avoid a malloc() when opening a decoder with drflac_open_memory(). */
  738.     drflac__memory_stream memoryStream;
  739.  
  740.  
  741.     /* A pointer to the decoded sample data. This is an offset of pExtraData. */
  742.     drflac_int32* pDecodedSamples;
  743.  
  744.     /* A pointer to the seek table. This is an offset of pExtraData, or NULL if there is no seek table. */
  745.     drflac_seekpoint* pSeekpoints;
  746.  
  747.     /* Internal use only. Only used with Ogg containers. Points to a drflac_oggbs object. This is an offset of pExtraData. */
  748.     void* _oggbs;
  749.  
  750.     /* Internal use only. Used for profiling and testing different seeking modes. */
  751.     drflac_bool32 _noSeekTableSeek    : 1;
  752.     drflac_bool32 _noBinarySearchSeek : 1;
  753.     drflac_bool32 _noBruteForceSeek   : 1;
  754.  
  755.     /* The bit streamer. The raw FLAC data is fed through this object. */
  756.     drflac_bs bs;
  757.  
  758.     /* Variable length extra data. We attach this to the end of the object so we can avoid unnecessary mallocs. */
  759.     drflac_uint8 pExtraData[1];
  760. } drflac;
  761.  
  762.  
  763. /*
  764. Opens a FLAC decoder.
  765.  
  766.  
  767. Parameters
  768. ----------
  769. onRead (in)
  770.     The function to call when data needs to be read from the client.
  771.  
  772. onSeek (in)
  773.     The function to call when the read position of the client data needs to move.
  774.  
  775. pUserData (in, optional)
  776.     A pointer to application defined data that will be passed to onRead and onSeek.
  777.  
  778. pAllocationCallbacks (in, optional)
  779.     A pointer to application defined callbacks for managing memory allocations.
  780.  
  781.  
  782. Return Value
  783. ------------
  784. Returns a pointer to an object representing the decoder.
  785.  
  786.  
  787. Remarks
  788. -------
  789. Close the decoder with `drflac_close()`.
  790.  
  791. `pAllocationCallbacks` can be NULL in which case it will use `DRFLAC_MALLOC`, `DRFLAC_REALLOC` and `DRFLAC_FREE`.
  792.  
  793. This function will automatically detect whether or not you are attempting to open a native or Ogg encapsulated FLAC, both of which should work seamlessly
  794. without any manual intervention. Ogg encapsulation also works with multiplexed streams which basically means it can play FLAC encoded audio tracks in videos.
  795.  
  796. This is the lowest level function for opening a FLAC stream. You can also use `drflac_open_file()` and `drflac_open_memory()` to open the stream from a file or
  797. from a block of memory respectively.
  798.  
  799. The STREAMINFO block must be present for this to succeed. Use `drflac_open_relaxed()` to open a FLAC stream where the header may not be present.
  800.  
  801.  
  802. Seek Also
  803. ---------
  804. drflac_open_file()
  805. drflac_open_memory()
  806. drflac_open_with_metadata()
  807. drflac_close()
  808. */
  809. DRFLAC_API drflac* drflac_open(drflac_read_proc onRead, drflac_seek_proc onSeek, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks);
  810.  
  811. /*
  812. Opens a FLAC stream with relaxed validation of the header block.
  813.  
  814.  
  815. Parameters
  816. ----------
  817. onRead (in)
  818.     The function to call when data needs to be read from the client.
  819.  
  820. onSeek (in)
  821.     The function to call when the read position of the client data needs to move.
  822.  
  823. container (in)
  824.     Whether or not the FLAC stream is encapsulated using standard FLAC encapsulation or Ogg encapsulation.
  825.  
  826. pUserData (in, optional)
  827.     A pointer to application defined data that will be passed to onRead and onSeek.
  828.  
  829. pAllocationCallbacks (in, optional)
  830.     A pointer to application defined callbacks for managing memory allocations.
  831.  
  832.  
  833. Return Value
  834. ------------
  835. A pointer to an object representing the decoder.
  836.  
  837.  
  838. Remarks
  839. -------
  840. The same as drflac_open(), except attempts to open the stream even when a header block is not present.
  841.  
  842. Because the header is not necessarily available, the caller must explicitly define the container (Native or Ogg). Do not set this to `drflac_container_unknown`
  843. as that is for internal use only.
  844.  
  845. Opening in relaxed mode will continue reading data from onRead until it finds a valid frame. If a frame is never found it will continue forever. To abort,
  846. force your `onRead` callback to return 0, which dr_flac will use as an indicator that the end of the stream was found.
  847. */
  848. DRFLAC_API drflac* drflac_open_relaxed(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_container container, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks);
  849.  
  850. /*
  851. Opens a FLAC decoder and notifies the caller of the metadata chunks (album art, etc.).
  852.  
  853.  
  854. Parameters
  855. ----------
  856. onRead (in)
  857.     The function to call when data needs to be read from the client.
  858.  
  859. onSeek (in)
  860.     The function to call when the read position of the client data needs to move.
  861.  
  862. onMeta (in)
  863.     The function to call for every metadata block.
  864.  
  865. pUserData (in, optional)
  866.     A pointer to application defined data that will be passed to onRead, onSeek and onMeta.
  867.  
  868. pAllocationCallbacks (in, optional)
  869.     A pointer to application defined callbacks for managing memory allocations.
  870.  
  871.  
  872. Return Value
  873. ------------
  874. A pointer to an object representing the decoder.
  875.  
  876.  
  877. Remarks
  878. -------
  879. Close the decoder with `drflac_close()`.
  880.  
  881. `pAllocationCallbacks` can be NULL in which case it will use `DRFLAC_MALLOC`, `DRFLAC_REALLOC` and `DRFLAC_FREE`.
  882.  
  883. This is slower than `drflac_open()`, so avoid this one if you don't need metadata. Internally, this will allocate and free memory on the heap for every
  884. metadata block except for STREAMINFO and PADDING blocks.
  885.  
  886. The caller is notified of the metadata via the `onMeta` callback. All metadata blocks will be handled before the function returns.
  887.  
  888. The STREAMINFO block must be present for this to succeed. Use `drflac_open_with_metadata_relaxed()` to open a FLAC stream where the header may not be present.
  889.  
  890. Note that this will behave inconsistently with `drflac_open()` if the stream is an Ogg encapsulated stream and a metadata block is corrupted. This is due to
  891. the way the Ogg stream recovers from corrupted pages. When `drflac_open_with_metadata()` is being used, the open routine will try to read the contents of the
  892. metadata block, whereas `drflac_open()` will simply seek past it (for the sake of efficiency). This inconsistency can result in different samples being
  893. returned depending on whether or not the stream is being opened with metadata.
  894.  
  895.  
  896. Seek Also
  897. ---------
  898. drflac_open_file_with_metadata()
  899. drflac_open_memory_with_metadata()
  900. drflac_open()
  901. drflac_close()
  902. */
  903. DRFLAC_API drflac* drflac_open_with_metadata(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_meta_proc onMeta, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks);
  904.  
  905. /*
  906. The same as drflac_open_with_metadata(), except attempts to open the stream even when a header block is not present.
  907.  
  908. See Also
  909. --------
  910. drflac_open_with_metadata()
  911. drflac_open_relaxed()
  912. */
  913. DRFLAC_API drflac* drflac_open_with_metadata_relaxed(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_meta_proc onMeta, drflac_container container, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks);
  914.  
  915. /*
  916. Closes the given FLAC decoder.
  917.  
  918.  
  919. Parameters
  920. ----------
  921. pFlac (in)
  922.     The decoder to close.
  923.  
  924.  
  925. Remarks
  926. -------
  927. This will destroy the decoder object.
  928.  
  929.  
  930. See Also
  931. --------
  932. drflac_open()
  933. drflac_open_with_metadata()
  934. drflac_open_file()
  935. drflac_open_file_w()
  936. drflac_open_file_with_metadata()
  937. drflac_open_file_with_metadata_w()
  938. drflac_open_memory()
  939. drflac_open_memory_with_metadata()
  940. */
  941. DRFLAC_API void drflac_close(drflac* pFlac);
  942.  
  943.  
  944. /*
  945. Reads sample data from the given FLAC decoder, output as interleaved signed 32-bit PCM.
  946.  
  947.  
  948. Parameters
  949. ----------
  950. pFlac (in)
  951.     The decoder.
  952.  
  953. framesToRead (in)
  954.     The number of PCM frames to read.
  955.  
  956. pBufferOut (out, optional)
  957.     A pointer to the buffer that will receive the decoded samples.
  958.  
  959.  
  960. Return Value
  961. ------------
  962. Returns the number of PCM frames actually read. If the return value is less than `framesToRead` it has reached the end.
  963.  
  964.  
  965. Remarks
  966. -------
  967. pBufferOut can be null, in which case the call will act as a seek, and the return value will be the number of frames seeked.
  968. */
  969. DRFLAC_API drflac_uint64 drflac_read_pcm_frames_s32(drflac* pFlac, drflac_uint64 framesToRead, drflac_int32* pBufferOut);
  970.  
  971.  
  972. /*
  973. Reads sample data from the given FLAC decoder, output as interleaved signed 16-bit PCM.
  974.  
  975.  
  976. Parameters
  977. ----------
  978. pFlac (in)
  979.     The decoder.
  980.  
  981. framesToRead (in)
  982.     The number of PCM frames to read.
  983.  
  984. pBufferOut (out, optional)
  985.     A pointer to the buffer that will receive the decoded samples.
  986.  
  987.  
  988. Return Value
  989. ------------
  990. Returns the number of PCM frames actually read. If the return value is less than `framesToRead` it has reached the end.
  991.  
  992.  
  993. Remarks
  994. -------
  995. pBufferOut can be null, in which case the call will act as a seek, and the return value will be the number of frames seeked.
  996.  
  997. Note that this is lossy for streams where the bits per sample is larger than 16.
  998. */
  999. DRFLAC_API drflac_uint64 drflac_read_pcm_frames_s16(drflac* pFlac, drflac_uint64 framesToRead, drflac_int16* pBufferOut);
  1000.  
  1001. /*
  1002. Reads sample data from the given FLAC decoder, output as interleaved 32-bit floating point PCM.
  1003.  
  1004.  
  1005. Parameters
  1006. ----------
  1007. pFlac (in)
  1008.     The decoder.
  1009.  
  1010. framesToRead (in)
  1011.     The number of PCM frames to read.
  1012.  
  1013. pBufferOut (out, optional)
  1014.     A pointer to the buffer that will receive the decoded samples.
  1015.  
  1016.  
  1017. Return Value
  1018. ------------
  1019. Returns the number of PCM frames actually read. If the return value is less than `framesToRead` it has reached the end.
  1020.  
  1021.  
  1022. Remarks
  1023. -------
  1024. pBufferOut can be null, in which case the call will act as a seek, and the return value will be the number of frames seeked.
  1025.  
  1026. Note that this should be considered lossy due to the nature of floating point numbers not being able to exactly represent every possible number.
  1027. */
  1028. DRFLAC_API drflac_uint64 drflac_read_pcm_frames_f32(drflac* pFlac, drflac_uint64 framesToRead, float* pBufferOut);
  1029.  
  1030. /*
  1031. Seeks to the PCM frame at the given index.
  1032.  
  1033.  
  1034. Parameters
  1035. ----------
  1036. pFlac (in)
  1037.     The decoder.
  1038.  
  1039. pcmFrameIndex (in)
  1040.     The index of the PCM frame to seek to. See notes below.
  1041.  
  1042.  
  1043. Return Value
  1044. -------------
  1045. `DRFLAC_TRUE` if successful; `DRFLAC_FALSE` otherwise.
  1046. */
  1047. DRFLAC_API drflac_bool32 drflac_seek_to_pcm_frame(drflac* pFlac, drflac_uint64 pcmFrameIndex);
  1048.  
  1049.  
  1050.  
  1051. #ifndef DR_FLAC_NO_STDIO
  1052. /*
  1053. Opens a FLAC decoder from the file at the given path.
  1054.  
  1055.  
  1056. Parameters
  1057. ----------
  1058. pFileName (in)
  1059.     The path of the file to open, either absolute or relative to the current directory.
  1060.  
  1061. pAllocationCallbacks (in, optional)
  1062.     A pointer to application defined callbacks for managing memory allocations.
  1063.  
  1064.  
  1065. Return Value
  1066. ------------
  1067. A pointer to an object representing the decoder.
  1068.  
  1069.  
  1070. Remarks
  1071. -------
  1072. Close the decoder with drflac_close().
  1073.  
  1074.  
  1075. Remarks
  1076. -------
  1077. This will hold a handle to the file until the decoder is closed with drflac_close(). Some platforms will restrict the number of files a process can have open
  1078. at any given time, so keep this mind if you have many decoders open at the same time.
  1079.  
  1080.  
  1081. See Also
  1082. --------
  1083. drflac_open_file_with_metadata()
  1084. drflac_open()
  1085. drflac_close()
  1086. */
  1087. DRFLAC_API drflac* drflac_open_file(const char* pFileName, const drflac_allocation_callbacks* pAllocationCallbacks);
  1088. DRFLAC_API drflac* drflac_open_file_w(const wchar_t* pFileName, const drflac_allocation_callbacks* pAllocationCallbacks);
  1089.  
  1090. /*
  1091. Opens a FLAC decoder from the file at the given path and notifies the caller of the metadata chunks (album art, etc.)
  1092.  
  1093.  
  1094. Parameters
  1095. ----------
  1096. pFileName (in)
  1097.     The path of the file to open, either absolute or relative to the current directory.
  1098.  
  1099. pAllocationCallbacks (in, optional)
  1100.     A pointer to application defined callbacks for managing memory allocations.
  1101.  
  1102. onMeta (in)
  1103.     The callback to fire for each metadata block.
  1104.  
  1105. pUserData (in)
  1106.     A pointer to the user data to pass to the metadata callback.
  1107.  
  1108. pAllocationCallbacks (in)
  1109.     A pointer to application defined callbacks for managing memory allocations.
  1110.  
  1111.  
  1112. Remarks
  1113. -------
  1114. Look at the documentation for drflac_open_with_metadata() for more information on how metadata is handled.
  1115.  
  1116.  
  1117. See Also
  1118. --------
  1119. drflac_open_with_metadata()
  1120. drflac_open()
  1121. drflac_close()
  1122. */
  1123. DRFLAC_API drflac* drflac_open_file_with_metadata(const char* pFileName, drflac_meta_proc onMeta, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks);
  1124. DRFLAC_API drflac* drflac_open_file_with_metadata_w(const wchar_t* pFileName, drflac_meta_proc onMeta, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks);
  1125. #endif
  1126.  
  1127. /*
  1128. Opens a FLAC decoder from a pre-allocated block of memory
  1129.  
  1130.  
  1131. Parameters
  1132. ----------
  1133. pData (in)
  1134.     A pointer to the raw encoded FLAC data.
  1135.  
  1136. dataSize (in)
  1137.     The size in bytes of `data`.
  1138.  
  1139. pAllocationCallbacks (in)
  1140.     A pointer to application defined callbacks for managing memory allocations.
  1141.  
  1142.  
  1143. Return Value
  1144. ------------
  1145. A pointer to an object representing the decoder.
  1146.  
  1147.  
  1148. Remarks
  1149. -------
  1150. This does not create a copy of the data. It is up to the application to ensure the buffer remains valid for the lifetime of the decoder.
  1151.  
  1152.  
  1153. See Also
  1154. --------
  1155. drflac_open()
  1156. drflac_close()
  1157. */
  1158. DRFLAC_API drflac* drflac_open_memory(const void* pData, size_t dataSize, const drflac_allocation_callbacks* pAllocationCallbacks);
  1159.  
  1160. /*
  1161. Opens a FLAC decoder from a pre-allocated block of memory and notifies the caller of the metadata chunks (album art, etc.)
  1162.  
  1163.  
  1164. Parameters
  1165. ----------
  1166. pData (in)
  1167.     A pointer to the raw encoded FLAC data.
  1168.  
  1169. dataSize (in)
  1170.     The size in bytes of `data`.
  1171.  
  1172. onMeta (in)
  1173.     The callback to fire for each metadata block.
  1174.  
  1175. pUserData (in)
  1176.     A pointer to the user data to pass to the metadata callback.
  1177.  
  1178. pAllocationCallbacks (in)
  1179.     A pointer to application defined callbacks for managing memory allocations.
  1180.  
  1181.  
  1182. Remarks
  1183. -------
  1184. Look at the documentation for drflac_open_with_metadata() for more information on how metadata is handled.
  1185.  
  1186.  
  1187. See Also
  1188. -------
  1189. drflac_open_with_metadata()
  1190. drflac_open()
  1191. drflac_close()
  1192. */
  1193. DRFLAC_API drflac* drflac_open_memory_with_metadata(const void* pData, size_t dataSize, drflac_meta_proc onMeta, void* pUserData, const drflac_allocation_callbacks* pAllocationCallbacks);
  1194.  
  1195.  
  1196.  
  1197. /* High Level APIs */
  1198.  
  1199. /*
  1200. Opens a FLAC stream from the given callbacks and fully decodes it in a single operation. The return value is a
  1201. pointer to the sample data as interleaved signed 32-bit PCM. The returned data must be freed with drflac_free().
  1202.  
  1203. You can pass in custom memory allocation callbacks via the pAllocationCallbacks parameter. This can be NULL in which
  1204. case it will use DRFLAC_MALLOC, DRFLAC_REALLOC and DRFLAC_FREE.
  1205.  
  1206. Sometimes a FLAC file won't keep track of the total sample count. In this situation the function will continuously
  1207. read samples into a dynamically sized buffer on the heap until no samples are left.
  1208.  
  1209. Do not call this function on a broadcast type of stream (like internet radio streams and whatnot).
  1210. */
  1211. DRFLAC_API drflac_int32* drflac_open_and_read_pcm_frames_s32(drflac_read_proc onRead, drflac_seek_proc onSeek, void* pUserData, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks);
  1212.  
  1213. /* Same as drflac_open_and_read_pcm_frames_s32(), except returns signed 16-bit integer samples. */
  1214. DRFLAC_API drflac_int16* drflac_open_and_read_pcm_frames_s16(drflac_read_proc onRead, drflac_seek_proc onSeek, void* pUserData, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks);
  1215.  
  1216. /* Same as drflac_open_and_read_pcm_frames_s32(), except returns 32-bit floating-point samples. */
  1217. DRFLAC_API float* drflac_open_and_read_pcm_frames_f32(drflac_read_proc onRead, drflac_seek_proc onSeek, void* pUserData, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks);
  1218.  
  1219. #ifndef DR_FLAC_NO_STDIO
  1220. /* Same as drflac_open_and_read_pcm_frames_s32() except opens the decoder from a file. */
  1221. DRFLAC_API drflac_int32* drflac_open_file_and_read_pcm_frames_s32(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks);
  1222.  
  1223. /* Same as drflac_open_file_and_read_pcm_frames_s32(), except returns signed 16-bit integer samples. */
  1224. DRFLAC_API drflac_int16* drflac_open_file_and_read_pcm_frames_s16(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks);
  1225.  
  1226. /* Same as drflac_open_file_and_read_pcm_frames_s32(), except returns 32-bit floating-point samples. */
  1227. DRFLAC_API float* drflac_open_file_and_read_pcm_frames_f32(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks);
  1228. #endif
  1229.  
  1230. /* Same as drflac_open_and_read_pcm_frames_s32() except opens the decoder from a block of memory. */
  1231. DRFLAC_API drflac_int32* drflac_open_memory_and_read_pcm_frames_s32(const void* data, size_t dataSize, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks);
  1232.  
  1233. /* Same as drflac_open_memory_and_read_pcm_frames_s32(), except returns signed 16-bit integer samples. */
  1234. DRFLAC_API drflac_int16* drflac_open_memory_and_read_pcm_frames_s16(const void* data, size_t dataSize, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks);
  1235.  
  1236. /* Same as drflac_open_memory_and_read_pcm_frames_s32(), except returns 32-bit floating-point samples. */
  1237. DRFLAC_API float* drflac_open_memory_and_read_pcm_frames_f32(const void* data, size_t dataSize, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount, const drflac_allocation_callbacks* pAllocationCallbacks);
  1238.  
  1239. /*
  1240. Frees memory that was allocated internally by dr_flac.
  1241.  
  1242. Set pAllocationCallbacks to the same object that was passed to drflac_open_*_and_read_pcm_frames_*(). If you originally passed in NULL, pass in NULL for this.
  1243. */
  1244. DRFLAC_API void drflac_free(void* p, const drflac_allocation_callbacks* pAllocationCallbacks);
  1245.  
  1246.  
  1247. /* Structure representing an iterator for vorbis comments in a VORBIS_COMMENT metadata block. */
  1248. typedef struct
  1249. {
  1250.     drflac_uint32 countRemaining;
  1251.     const char* pRunningData;
  1252. } drflac_vorbis_comment_iterator;
  1253.  
  1254. /*
  1255. Initializes a vorbis comment iterator. This can be used for iterating over the vorbis comments in a VORBIS_COMMENT
  1256. metadata block.
  1257. */
  1258. DRFLAC_API void drflac_init_vorbis_comment_iterator(drflac_vorbis_comment_iterator* pIter, drflac_uint32 commentCount, const void* pComments);
  1259.  
  1260. /*
  1261. Goes to the next vorbis comment in the given iterator. If null is returned it means there are no more comments. The
  1262. returned string is NOT null terminated.
  1263. */
  1264. DRFLAC_API const char* drflac_next_vorbis_comment(drflac_vorbis_comment_iterator* pIter, drflac_uint32* pCommentLengthOut);
  1265.  
  1266.  
  1267. /* Structure representing an iterator for cuesheet tracks in a CUESHEET metadata block. */
  1268. typedef struct
  1269. {
  1270.     drflac_uint32 countRemaining;
  1271.     const char* pRunningData;
  1272. } drflac_cuesheet_track_iterator;
  1273.  
  1274. /* Packing is important on this structure because we map this directly to the raw data within the CUESHEET metadata block. */
  1275. #pragma pack(4)
  1276. typedef struct
  1277. {
  1278.     drflac_uint64 offset;
  1279.     drflac_uint8 index;
  1280.     drflac_uint8 reserved[3];
  1281. } drflac_cuesheet_track_index;
  1282. #pragma pack()
  1283.  
  1284. typedef struct
  1285. {
  1286.     drflac_uint64 offset;
  1287.     drflac_uint8 trackNumber;
  1288.     char ISRC[12];
  1289.     drflac_bool8 isAudio;
  1290.     drflac_bool8 preEmphasis;
  1291.     drflac_uint8 indexCount;
  1292.     const drflac_cuesheet_track_index* pIndexPoints;
  1293. } drflac_cuesheet_track;
  1294.  
  1295. /*
  1296. Initializes a cuesheet track iterator. This can be used for iterating over the cuesheet tracks in a CUESHEET metadata
  1297. block.
  1298. */
  1299. DRFLAC_API void drflac_init_cuesheet_track_iterator(drflac_cuesheet_track_iterator* pIter, drflac_uint32 trackCount, const void* pTrackData);
  1300.  
  1301. /* Goes to the next cuesheet track in the given iterator. If DRFLAC_FALSE is returned it means there are no more comments. */
  1302. DRFLAC_API drflac_bool32 drflac_next_cuesheet_track(drflac_cuesheet_track_iterator* pIter, drflac_cuesheet_track* pCuesheetTrack);
  1303.  
  1304.  
  1305. #ifdef __cplusplus
  1306. }
  1307. #endif
  1308. #endif  /* dr_flac_h */
  1309.  
  1310.  
  1311. /************************************************************************************************************************************************************
  1312.  ************************************************************************************************************************************************************
  1313.  
  1314.  IMPLEMENTATION
  1315.  
  1316.  ************************************************************************************************************************************************************
  1317.  ************************************************************************************************************************************************************/
  1318. #if defined(DR_FLAC_IMPLEMENTATION) || defined(DRFLAC_IMPLEMENTATION)
  1319.  
  1320. /* Disable some annoying warnings. */
  1321. #if defined(__GNUC__)
  1322.     #pragma GCC diagnostic push
  1323.     #if __GNUC__ >= 7
  1324.     #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
  1325.     #endif
  1326. #endif
  1327.  
  1328. #ifdef __linux__
  1329.     #ifndef _BSD_SOURCE
  1330.         #define _BSD_SOURCE
  1331.     #endif
  1332.     #ifndef __USE_BSD
  1333.         #define __USE_BSD
  1334.     #endif
  1335.     #include <endian.h>
  1336. #endif
  1337.  
  1338. #include <stdlib.h>
  1339. #include <string.h>
  1340.  
  1341. #ifdef _MSC_VER
  1342.     #define DRFLAC_INLINE __forceinline
  1343. #elif defined(__GNUC__)
  1344.     /*
  1345.     I've had a bug report where GCC is emitting warnings about functions possibly not being inlineable. This warning happens when
  1346.     the __attribute__((always_inline)) attribute is defined without an "inline" statement. I think therefore there must be some
  1347.     case where "__inline__" is not always defined, thus the compiler emitting these warnings. When using -std=c89 or -ansi on the
  1348.     command line, we cannot use the "inline" keyword and instead need to use "__inline__". In an attempt to work around this issue
  1349.     I am using "__inline__" only when we're compiling in strict ANSI mode.
  1350.     */
  1351.     #if defined(__STRICT_ANSI__)
  1352.         #define DRFLAC_INLINE __inline__ __attribute__((always_inline))
  1353.     #else
  1354.         #define DRFLAC_INLINE inline __attribute__((always_inline))
  1355.     #endif
  1356. #else
  1357.     #define DRFLAC_INLINE
  1358. #endif
  1359.  
  1360. /* CPU architecture. */
  1361. #if defined(__x86_64__) || defined(_M_X64)
  1362.     #define DRFLAC_X64
  1363. #elif defined(__i386) || defined(_M_IX86)
  1364.     #define DRFLAC_X86
  1365. #elif defined(__arm__) || defined(_M_ARM)
  1366.     #define DRFLAC_ARM
  1367. #endif
  1368.  
  1369. /* Intrinsics Support */
  1370. #if !defined(DR_FLAC_NO_SIMD)
  1371.     #if defined(DRFLAC_X64) || defined(DRFLAC_X86)
  1372.         #if defined(_MSC_VER) && !defined(__clang__)
  1373.             /* MSVC. */
  1374.             #if _MSC_VER >= 1400 && !defined(DRFLAC_NO_SSE2)    /* 2005 */
  1375.                 #define DRFLAC_SUPPORT_SSE2
  1376.             #endif
  1377.             #if _MSC_VER >= 1600 && !defined(DRFLAC_NO_SSE41)   /* 2010 */
  1378.                 #define DRFLAC_SUPPORT_SSE41
  1379.             #endif
  1380.         #else
  1381.             /* Assume GNUC-style. */
  1382.             #if defined(__SSE2__) && !defined(DRFLAC_NO_SSE2)
  1383.                 #define DRFLAC_SUPPORT_SSE2
  1384.             #endif
  1385.             #if defined(__SSE4_1__) && !defined(DRFLAC_NO_SSE41)
  1386.                 #define DRFLAC_SUPPORT_SSE41
  1387.             #endif
  1388.         #endif
  1389.  
  1390.         /* If at this point we still haven't determined compiler support for the intrinsics just fall back to __has_include. */
  1391.         #if !defined(__GNUC__) && !defined(__clang__) && defined(__has_include)
  1392.             #if !defined(DRFLAC_SUPPORT_SSE2) && !defined(DRFLAC_NO_SSE2) && __has_include(<emmintrin.h>)
  1393.                 #define DRFLAC_SUPPORT_SSE2
  1394.             #endif
  1395.             #if !defined(DRFLAC_SUPPORT_SSE41) && !defined(DRFLAC_NO_SSE41) && __has_include(<smmintrin.h>)
  1396.                 #define DRFLAC_SUPPORT_SSE41
  1397.             #endif
  1398.         #endif
  1399.  
  1400.         #if defined(DRFLAC_SUPPORT_SSE41)
  1401.             #include <smmintrin.h>
  1402.         #elif defined(DRFLAC_SUPPORT_SSE2)
  1403.             #include <emmintrin.h>
  1404.         #endif
  1405.     #endif
  1406.  
  1407.     #if defined(DRFLAC_ARM)
  1408.         #if !defined(DRFLAC_NO_NEON) && (defined(__ARM_NEON) || defined(__aarch64__) || defined(_M_ARM64))
  1409.             #define DRFLAC_SUPPORT_NEON
  1410.         #endif
  1411.  
  1412.         /* Fall back to looking for the #include file. */
  1413.         #if !defined(__GNUC__) && !defined(__clang__) && defined(__has_include)
  1414.             #if !defined(DRFLAC_SUPPORT_NEON) && !defined(DRFLAC_NO_NEON) && __has_include(<arm_neon.h>)
  1415.                 #define DRFLAC_SUPPORT_NEON
  1416.             #endif
  1417.         #endif
  1418.  
  1419.         #if defined(DRFLAC_SUPPORT_NEON)
  1420.             #include <arm_neon.h>
  1421.         #endif
  1422.     #endif
  1423. #endif
  1424.  
  1425. /* Compile-time CPU feature support. */
  1426. #if !defined(DR_FLAC_NO_SIMD) && (defined(DRFLAC_X86) || defined(DRFLAC_X64))
  1427.     #if defined(_MSC_VER) && !defined(__clang__)
  1428.         #if _MSC_VER >= 1400
  1429.             #include <intrin.h>
  1430.             static void drflac__cpuid(int info[4], int fid)
  1431.             {
  1432.                 __cpuid(info, fid);
  1433.             }
  1434.         #else
  1435.             #define DRFLAC_NO_CPUID
  1436.         #endif
  1437.     #else
  1438.         #if defined(__GNUC__) || defined(__clang__)
  1439.             static void drflac__cpuid(int info[4], int fid)
  1440.             {
  1441.                 /*
  1442.                 It looks like the -fPIC option uses the ebx register which GCC complains about. We can work around this by just using a different register, the
  1443.                 specific register of which I'm letting the compiler decide on. The "k" prefix is used to specify a 32-bit register. The {...} syntax is for
  1444.                 supporting different assembly dialects.
  1445.  
  1446.                 What's basically happening is that we're saving and restoring the ebx register manually.
  1447.                 */
  1448.                 #if defined(DRFLAC_X86) && defined(__PIC__)
  1449.                     __asm__ __volatile__ (
  1450.                         "xchg{l} {%%}ebx, %k1;"
  1451.                         "cpuid;"
  1452.                         "xchg{l} {%%}ebx, %k1;"
  1453.                         : "=a"(info[0]), "=&r"(info[1]), "=c"(info[2]), "=d"(info[3]) : "a"(fid), "c"(0)
  1454.                     );
  1455.                 #else
  1456.                     __asm__ __volatile__ (
  1457.                         "cpuid" : "=a"(info[0]), "=b"(info[1]), "=c"(info[2]), "=d"(info[3]) : "a"(fid), "c"(0)
  1458.                     );
  1459.                 #endif
  1460.             }
  1461.         #else
  1462.             #define DRFLAC_NO_CPUID
  1463.         #endif
  1464.     #endif
  1465. #else
  1466.     #define DRFLAC_NO_CPUID
  1467. #endif
  1468.  
  1469. static DRFLAC_INLINE drflac_bool32 drflac_has_sse2(void)
  1470. {
  1471. #if defined(DRFLAC_SUPPORT_SSE2)
  1472.     #if (defined(DRFLAC_X64) || defined(DRFLAC_X86)) && !defined(DRFLAC_NO_SSE2)
  1473.         #if defined(DRFLAC_X64)
  1474.             return DRFLAC_TRUE;    /* 64-bit targets always support SSE2. */
  1475.         #elif (defined(_M_IX86_FP) && _M_IX86_FP == 2) || defined(__SSE2__)
  1476.             return DRFLAC_TRUE;    /* If the compiler is allowed to freely generate SSE2 code we can assume support. */
  1477.         #else
  1478.             #if defined(DRFLAC_NO_CPUID)
  1479.                 return DRFLAC_FALSE;
  1480.             #else
  1481.                 int info[4];
  1482.                 drflac__cpuid(info, 1);
  1483.                 return (info[3] & (1 << 26)) != 0;
  1484.             #endif
  1485.         #endif
  1486.     #else
  1487.         return DRFLAC_FALSE;       /* SSE2 is only supported on x86 and x64 architectures. */
  1488.     #endif
  1489. #else
  1490.     return DRFLAC_FALSE;           /* No compiler support. */
  1491. #endif
  1492. }
  1493.  
  1494. static DRFLAC_INLINE drflac_bool32 drflac_has_sse41(void)
  1495. {
  1496. #if defined(DRFLAC_SUPPORT_SSE41)
  1497.     #if (defined(DRFLAC_X64) || defined(DRFLAC_X86)) && !defined(DRFLAC_NO_SSE41)
  1498.         #if defined(DRFLAC_X64)
  1499.             return DRFLAC_TRUE;    /* 64-bit targets always support SSE4.1. */
  1500.         #elif (defined(_M_IX86_FP) && _M_IX86_FP == 2) || defined(__SSE4_1__)
  1501.             return DRFLAC_TRUE;    /* If the compiler is allowed to freely generate SSE41 code we can assume support. */
  1502.         #else
  1503.             #if defined(DRFLAC_NO_CPUID)
  1504.                 return DRFLAC_FALSE;
  1505.             #else
  1506.                 int info[4];
  1507.                 drflac__cpuid(info, 1);
  1508.                 return (info[2] & (1 << 19)) != 0;
  1509.             #endif
  1510.         #endif
  1511.     #else
  1512.         return DRFLAC_FALSE;       /* SSE41 is only supported on x86 and x64 architectures. */
  1513.     #endif
  1514. #else
  1515.     return DRFLAC_FALSE;           /* No compiler support. */
  1516. #endif
  1517. }
  1518.  
  1519.  
  1520. #if defined(_MSC_VER) && _MSC_VER >= 1500 && (defined(DRFLAC_X86) || defined(DRFLAC_X64))
  1521.     #define DRFLAC_HAS_LZCNT_INTRINSIC
  1522. #elif (defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)))
  1523.     #define DRFLAC_HAS_LZCNT_INTRINSIC
  1524. #elif defined(__clang__)
  1525.     #if defined(__has_builtin)
  1526.         #if __has_builtin(__builtin_clzll) || __has_builtin(__builtin_clzl)
  1527.             #define DRFLAC_HAS_LZCNT_INTRINSIC
  1528.         #endif
  1529.     #endif
  1530. #endif
  1531.  
  1532. #if defined(_MSC_VER) && _MSC_VER >= 1400
  1533.     #define DRFLAC_HAS_BYTESWAP16_INTRINSIC
  1534.     #define DRFLAC_HAS_BYTESWAP32_INTRINSIC
  1535.     #define DRFLAC_HAS_BYTESWAP64_INTRINSIC
  1536. #elif defined(__clang__)
  1537.     #if defined(__has_builtin)
  1538.         #if __has_builtin(__builtin_bswap16)
  1539.             #define DRFLAC_HAS_BYTESWAP16_INTRINSIC
  1540.         #endif
  1541.         #if __has_builtin(__builtin_bswap32)
  1542.             #define DRFLAC_HAS_BYTESWAP32_INTRINSIC
  1543.         #endif
  1544.         #if __has_builtin(__builtin_bswap64)
  1545.             #define DRFLAC_HAS_BYTESWAP64_INTRINSIC
  1546.         #endif
  1547.     #endif
  1548. #elif defined(__GNUC__)
  1549.     #if ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  1550.         #define DRFLAC_HAS_BYTESWAP32_INTRINSIC
  1551.         #define DRFLAC_HAS_BYTESWAP64_INTRINSIC
  1552.     #endif
  1553.     #if ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))
  1554.         #define DRFLAC_HAS_BYTESWAP16_INTRINSIC
  1555.     #endif
  1556. #endif
  1557.  
  1558.  
  1559. /* Standard library stuff. */
  1560. #ifndef DRFLAC_ASSERT
  1561. #include <assert.h>
  1562. #define DRFLAC_ASSERT(expression)           assert(expression)
  1563. #endif
  1564. #ifndef DRFLAC_MALLOC
  1565. #define DRFLAC_MALLOC(sz)                   malloc((sz))
  1566. #endif
  1567. #ifndef DRFLAC_REALLOC
  1568. #define DRFLAC_REALLOC(p, sz)               realloc((p), (sz))
  1569. #endif
  1570. #ifndef DRFLAC_FREE
  1571. #define DRFLAC_FREE(p)                      free((p))
  1572. #endif
  1573. #ifndef DRFLAC_COPY_MEMORY
  1574. #define DRFLAC_COPY_MEMORY(dst, src, sz)    memcpy((dst), (src), (sz))
  1575. #endif
  1576. #ifndef DRFLAC_ZERO_MEMORY
  1577. #define DRFLAC_ZERO_MEMORY(p, sz)           memset((p), 0, (sz))
  1578. #endif
  1579. #ifndef DRFLAC_ZERO_OBJECT
  1580. #define DRFLAC_ZERO_OBJECT(p)               DRFLAC_ZERO_MEMORY((p), sizeof(*(p)))
  1581. #endif
  1582.  
  1583. #define DRFLAC_MAX_SIMD_VECTOR_SIZE                     64  /* 64 for AVX-512 in the future. */
  1584.  
  1585. typedef drflac_int32 drflac_result;
  1586. #define DRFLAC_SUCCESS                                   0
  1587. #define DRFLAC_ERROR                                    -1   /* A generic error. */
  1588. #define DRFLAC_INVALID_ARGS                             -2
  1589. #define DRFLAC_INVALID_OPERATION                        -3
  1590. #define DRFLAC_OUT_OF_MEMORY                            -4
  1591. #define DRFLAC_OUT_OF_RANGE                             -5
  1592. #define DRFLAC_ACCESS_DENIED                            -6
  1593. #define DRFLAC_DOES_NOT_EXIST                           -7
  1594. #define DRFLAC_ALREADY_EXISTS                           -8
  1595. #define DRFLAC_TOO_MANY_OPEN_FILES                      -9
  1596. #define DRFLAC_INVALID_FILE                             -10
  1597. #define DRFLAC_TOO_BIG                                  -11
  1598. #define DRFLAC_PATH_TOO_LONG                            -12
  1599. #define DRFLAC_NAME_TOO_LONG                            -13
  1600. #define DRFLAC_NOT_DIRECTORY                            -14
  1601. #define DRFLAC_IS_DIRECTORY                             -15
  1602. #define DRFLAC_DIRECTORY_NOT_EMPTY                      -16
  1603. #define DRFLAC_END_OF_FILE                              -17
  1604. #define DRFLAC_NO_SPACE                                 -18
  1605. #define DRFLAC_BUSY                                     -19
  1606. #define DRFLAC_IO_ERROR                                 -20
  1607. #define DRFLAC_INTERRUPT                                -21
  1608. #define DRFLAC_UNAVAILABLE                              -22
  1609. #define DRFLAC_ALREADY_IN_USE                           -23
  1610. #define DRFLAC_BAD_ADDRESS                              -24
  1611. #define DRFLAC_BAD_SEEK                                 -25
  1612. #define DRFLAC_BAD_PIPE                                 -26
  1613. #define DRFLAC_DEADLOCK                                 -27
  1614. #define DRFLAC_TOO_MANY_LINKS                           -28
  1615. #define DRFLAC_NOT_IMPLEMENTED                          -29
  1616. #define DRFLAC_NO_MESSAGE                               -30
  1617. #define DRFLAC_BAD_MESSAGE                              -31
  1618. #define DRFLAC_NO_DATA_AVAILABLE                        -32
  1619. #define DRFLAC_INVALID_DATA                             -33
  1620. #define DRFLAC_TIMEOUT                                  -34
  1621. #define DRFLAC_NO_NETWORK                               -35
  1622. #define DRFLAC_NOT_UNIQUE                               -36
  1623. #define DRFLAC_NOT_SOCKET                               -37
  1624. #define DRFLAC_NO_ADDRESS                               -38
  1625. #define DRFLAC_BAD_PROTOCOL                             -39
  1626. #define DRFLAC_PROTOCOL_UNAVAILABLE                     -40
  1627. #define DRFLAC_PROTOCOL_NOT_SUPPORTED                   -41
  1628. #define DRFLAC_PROTOCOL_FAMILY_NOT_SUPPORTED            -42
  1629. #define DRFLAC_ADDRESS_FAMILY_NOT_SUPPORTED             -43
  1630. #define DRFLAC_SOCKET_NOT_SUPPORTED                     -44
  1631. #define DRFLAC_CONNECTION_RESET                         -45
  1632. #define DRFLAC_ALREADY_CONNECTED                        -46
  1633. #define DRFLAC_NOT_CONNECTED                            -47
  1634. #define DRFLAC_CONNECTION_REFUSED                       -48
  1635. #define DRFLAC_NO_HOST                                  -49
  1636. #define DRFLAC_IN_PROGRESS                              -50
  1637. #define DRFLAC_CANCELLED                                -51
  1638. #define DRFLAC_MEMORY_ALREADY_MAPPED                    -52
  1639. #define DRFLAC_AT_END                                   -53
  1640. #define DRFLAC_CRC_MISMATCH                             -128
  1641.  
  1642. #define DRFLAC_SUBFRAME_CONSTANT                        0
  1643. #define DRFLAC_SUBFRAME_VERBATIM                        1
  1644. #define DRFLAC_SUBFRAME_FIXED                           8
  1645. #define DRFLAC_SUBFRAME_LPC                             32
  1646. #define DRFLAC_SUBFRAME_RESERVED                        255
  1647.  
  1648. #define DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE  0
  1649. #define DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2 1
  1650.  
  1651. #define DRFLAC_CHANNEL_ASSIGNMENT_INDEPENDENT           0
  1652. #define DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE             8
  1653. #define DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE            9
  1654. #define DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE              10
  1655.  
  1656. #define drflac_align(x, a)                              ((((x) + (a) - 1) / (a)) * (a))
  1657.  
  1658.  
  1659. DRFLAC_API void drflac_version(drflac_uint32* pMajor, drflac_uint32* pMinor, drflac_uint32* pRevision)
  1660. {
  1661.     if (pMajor) {
  1662.         *pMajor = DRFLAC_VERSION_MAJOR;
  1663.     }
  1664.  
  1665.     if (pMinor) {
  1666.         *pMinor = DRFLAC_VERSION_MINOR;
  1667.     }
  1668.  
  1669.     if (pRevision) {
  1670.         *pRevision = DRFLAC_VERSION_REVISION;
  1671.     }
  1672. }
  1673.  
  1674. DRFLAC_API const char* drflac_version_string()
  1675. {
  1676.     return DRFLAC_VERSION_STRING;
  1677. }
  1678.  
  1679.  
  1680. /* CPU caps. */
  1681. #if defined(__has_feature)
  1682.     #if __has_feature(thread_sanitizer)
  1683.         #define DRFLAC_NO_THREAD_SANITIZE __attribute__((no_sanitize("thread")))
  1684.     #else
  1685.         #define DRFLAC_NO_THREAD_SANITIZE
  1686.     #endif
  1687. #else
  1688.     #define DRFLAC_NO_THREAD_SANITIZE
  1689. #endif
  1690.  
  1691. #if defined(DRFLAC_HAS_LZCNT_INTRINSIC)
  1692. static drflac_bool32 drflac__gIsLZCNTSupported = DRFLAC_FALSE;
  1693. #endif
  1694.  
  1695. #ifndef DRFLAC_NO_CPUID
  1696. static drflac_bool32 drflac__gIsSSE2Supported  = DRFLAC_FALSE;
  1697. static drflac_bool32 drflac__gIsSSE41Supported = DRFLAC_FALSE;
  1698.  
  1699. /*
  1700. I've had a bug report that Clang's ThreadSanitizer presents a warning in this function. Having reviewed this, this does
  1701. actually make sense. However, since CPU caps should never differ for a running process, I don't think the trade off of
  1702. complicating internal API's by passing around CPU caps versus just disabling the warnings is worthwhile. I'm therefore
  1703. just going to disable these warnings. This is disabled via the DRFLAC_NO_THREAD_SANITIZE attribute.
  1704. */
  1705. DRFLAC_NO_THREAD_SANITIZE static void drflac__init_cpu_caps(void)
  1706. {
  1707.     static drflac_bool32 isCPUCapsInitialized = DRFLAC_FALSE;
  1708.  
  1709.     if (!isCPUCapsInitialized) {
  1710.         /* LZCNT */
  1711. #if defined(DRFLAC_HAS_LZCNT_INTRINSIC)
  1712.         int info[4] = {0};
  1713.         drflac__cpuid(info, 0x80000001);
  1714.         drflac__gIsLZCNTSupported = (info[2] & (1 << 5)) != 0;
  1715. #endif
  1716.  
  1717.         /* SSE2 */
  1718.         drflac__gIsSSE2Supported = drflac_has_sse2();
  1719.  
  1720.         /* SSE4.1 */
  1721.         drflac__gIsSSE41Supported = drflac_has_sse41();
  1722.  
  1723.         /* Initialized. */
  1724.         isCPUCapsInitialized = DRFLAC_TRUE;
  1725.     }
  1726. }
  1727. #else
  1728. static drflac_bool32 drflac__gIsNEONSupported  = DRFLAC_FALSE;
  1729.  
  1730. static DRFLAC_INLINE drflac_bool32 drflac__has_neon(void)
  1731. {
  1732. #if defined(DRFLAC_SUPPORT_NEON)
  1733.     #if defined(DRFLAC_ARM) && !defined(DRFLAC_NO_NEON)
  1734.         #if (defined(__ARM_NEON) || defined(__aarch64__) || defined(_M_ARM64))
  1735.             return DRFLAC_TRUE;    /* If the compiler is allowed to freely generate NEON code we can assume support. */
  1736.         #else
  1737.             /* TODO: Runtime check. */
  1738.             return DRFLAC_FALSE;
  1739.         #endif
  1740.     #else
  1741.         return DRFLAC_FALSE;       /* NEON is only supported on ARM architectures. */
  1742.     #endif
  1743. #else
  1744.     return DRFLAC_FALSE;           /* No compiler support. */
  1745. #endif
  1746. }
  1747.  
  1748. DRFLAC_NO_THREAD_SANITIZE static void drflac__init_cpu_caps(void)
  1749. {
  1750.     drflac__gIsNEONSupported = drflac__has_neon();
  1751.  
  1752. #if defined(DRFLAC_HAS_LZCNT_INTRINSIC) && defined(DRFLAC_ARM) && (defined(__ARM_ARCH) && __ARM_ARCH >= 5)
  1753.     drflac__gIsLZCNTSupported = DRFLAC_TRUE;
  1754. #endif
  1755. }
  1756. #endif
  1757.  
  1758.  
  1759. /* Endian Management */
  1760. static DRFLAC_INLINE drflac_bool32 drflac__is_little_endian(void)
  1761. {
  1762. #if defined(DRFLAC_X86) || defined(DRFLAC_X64)
  1763.     return DRFLAC_TRUE;
  1764. #elif defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN
  1765.     return DRFLAC_TRUE;
  1766. #else
  1767.     int n = 1;
  1768.     return (*(char*)&n) == 1;
  1769. #endif
  1770. }
  1771.  
  1772. static DRFLAC_INLINE drflac_uint16 drflac__swap_endian_uint16(drflac_uint16 n)
  1773. {
  1774. #ifdef DRFLAC_HAS_BYTESWAP16_INTRINSIC
  1775.     #if defined(_MSC_VER)
  1776.         return _byteswap_ushort(n);
  1777.     #elif defined(__GNUC__) || defined(__clang__)
  1778.         return __builtin_bswap16(n);
  1779.     #else
  1780.         #error "This compiler does not support the byte swap intrinsic."
  1781.     #endif
  1782. #else
  1783.     return ((n & 0xFF00) >> 8) |
  1784.            ((n & 0x00FF) << 8);
  1785. #endif
  1786. }
  1787.  
  1788. static DRFLAC_INLINE drflac_uint32 drflac__swap_endian_uint32(drflac_uint32 n)
  1789. {
  1790. #ifdef DRFLAC_HAS_BYTESWAP32_INTRINSIC
  1791.     #if defined(_MSC_VER)
  1792.         return _byteswap_ulong(n);
  1793.     #elif defined(__GNUC__) || defined(__clang__)
  1794.         #if defined(DRFLAC_ARM) && (defined(__ARM_ARCH) && __ARM_ARCH >= 6) && !defined(DRFLAC_64BIT)   /* <-- 64-bit inline assembly has not been tested, so disabling for now. */
  1795.             /* Inline assembly optimized implementation for ARM. In my testing, GCC does not generate optimized code with __builtin_bswap32(). */
  1796.             drflac_uint32 r;
  1797.             __asm__ __volatile__ (
  1798.             #if defined(DRFLAC_64BIT)
  1799.                 "rev %w[out], %w[in]" : [out]"=r"(r) : [in]"r"(n)   /* <-- This is untested. If someone in the community could test this, that would be appreciated! */
  1800.             #else
  1801.                 "rev %[out], %[in]" : [out]"=r"(r) : [in]"r"(n)
  1802.             #endif
  1803.             );
  1804.             return r;
  1805.         #else
  1806.             return __builtin_bswap32(n);
  1807.         #endif
  1808.     #else
  1809.         #error "This compiler does not support the byte swap intrinsic."
  1810.     #endif
  1811. #else
  1812.     return ((n & 0xFF000000) >> 24) |
  1813.            ((n & 0x00FF0000) >>  8) |
  1814.            ((n & 0x0000FF00) <<  8) |
  1815.            ((n & 0x000000FF) << 24);
  1816. #endif
  1817. }
  1818.  
  1819. static DRFLAC_INLINE drflac_uint64 drflac__swap_endian_uint64(drflac_uint64 n)
  1820. {
  1821. #ifdef DRFLAC_HAS_BYTESWAP64_INTRINSIC
  1822.     #if defined(_MSC_VER)
  1823.         return _byteswap_uint64(n);
  1824.     #elif defined(__GNUC__) || defined(__clang__)
  1825.         return __builtin_bswap64(n);
  1826.     #else
  1827.         #error "This compiler does not support the byte swap intrinsic."
  1828.     #endif
  1829. #else
  1830.     return ((n & (drflac_uint64)0xFF00000000000000) >> 56) |
  1831.            ((n & (drflac_uint64)0x00FF000000000000) >> 40) |
  1832.            ((n & (drflac_uint64)0x0000FF0000000000) >> 24) |
  1833.            ((n & (drflac_uint64)0x000000FF00000000) >>  8) |
  1834.            ((n & (drflac_uint64)0x00000000FF000000) <<  8) |
  1835.            ((n & (drflac_uint64)0x0000000000FF0000) << 24) |
  1836.            ((n & (drflac_uint64)0x000000000000FF00) << 40) |
  1837.            ((n & (drflac_uint64)0x00000000000000FF) << 56);
  1838. #endif
  1839. }
  1840.  
  1841.  
  1842. static DRFLAC_INLINE drflac_uint16 drflac__be2host_16(drflac_uint16 n)
  1843. {
  1844.     if (drflac__is_little_endian()) {
  1845.         return drflac__swap_endian_uint16(n);
  1846.     }
  1847.  
  1848.     return n;
  1849. }
  1850.  
  1851. static DRFLAC_INLINE drflac_uint32 drflac__be2host_32(drflac_uint32 n)
  1852. {
  1853.     if (drflac__is_little_endian()) {
  1854.         return drflac__swap_endian_uint32(n);
  1855.     }
  1856.  
  1857.     return n;
  1858. }
  1859.  
  1860. static DRFLAC_INLINE drflac_uint64 drflac__be2host_64(drflac_uint64 n)
  1861. {
  1862.     if (drflac__is_little_endian()) {
  1863.         return drflac__swap_endian_uint64(n);
  1864.     }
  1865.  
  1866.     return n;
  1867. }
  1868.  
  1869.  
  1870. static DRFLAC_INLINE drflac_uint32 drflac__le2host_32(drflac_uint32 n)
  1871. {
  1872.     if (!drflac__is_little_endian()) {
  1873.         return drflac__swap_endian_uint32(n);
  1874.     }
  1875.  
  1876.     return n;
  1877. }
  1878.  
  1879.  
  1880. static DRFLAC_INLINE drflac_uint32 drflac__unsynchsafe_32(drflac_uint32 n)
  1881. {
  1882.     drflac_uint32 result = 0;
  1883.     result |= (n & 0x7F000000) >> 3;
  1884.     result |= (n & 0x007F0000) >> 2;
  1885.     result |= (n & 0x00007F00) >> 1;
  1886.     result |= (n & 0x0000007F) >> 0;
  1887.  
  1888.     return result;
  1889. }
  1890.  
  1891.  
  1892.  
  1893. /* The CRC code below is based on this document: http://zlib.net/crc_v3.txt */
  1894. static drflac_uint8 drflac__crc8_table[] = {
  1895.     0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D,
  1896.     0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D,
  1897.     0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD,
  1898.     0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD,
  1899.     0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA,
  1900.     0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A,
  1901.     0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A,
  1902.     0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A,
  1903.     0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4,
  1904.     0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4,
  1905.     0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44,
  1906.     0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34,
  1907.     0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63,
  1908.     0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13,
  1909.     0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,
  1910.     0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3
  1911. };
  1912.  
  1913. static drflac_uint16 drflac__crc16_table[] = {
  1914.     0x0000, 0x8005, 0x800F, 0x000A, 0x801B, 0x001E, 0x0014, 0x8011,
  1915.     0x8033, 0x0036, 0x003C, 0x8039, 0x0028, 0x802D, 0x8027, 0x0022,
  1916.     0x8063, 0x0066, 0x006C, 0x8069, 0x0078, 0x807D, 0x8077, 0x0072,
  1917.     0x0050, 0x8055, 0x805F, 0x005A, 0x804B, 0x004E, 0x0044, 0x8041,
  1918.     0x80C3, 0x00C6, 0x00CC, 0x80C9, 0x00D8, 0x80DD, 0x80D7, 0x00D2,
  1919.     0x00F0, 0x80F5, 0x80FF, 0x00FA, 0x80EB, 0x00EE, 0x00E4, 0x80E1,
  1920.     0x00A0, 0x80A5, 0x80AF, 0x00AA, 0x80BB, 0x00BE, 0x00B4, 0x80B1,
  1921.     0x8093, 0x0096, 0x009C, 0x8099, 0x0088, 0x808D, 0x8087, 0x0082,
  1922.     0x8183, 0x0186, 0x018C, 0x8189, 0x0198, 0x819D, 0x8197, 0x0192,
  1923.     0x01B0, 0x81B5, 0x81BF, 0x01BA, 0x81AB, 0x01AE, 0x01A4, 0x81A1,
  1924.     0x01E0, 0x81E5, 0x81EF, 0x01EA, 0x81FB, 0x01FE, 0x01F4, 0x81F1,
  1925.     0x81D3, 0x01D6, 0x01DC, 0x81D9, 0x01C8, 0x81CD, 0x81C7, 0x01C2,
  1926.     0x0140, 0x8145, 0x814F, 0x014A, 0x815B, 0x015E, 0x0154, 0x8151,
  1927.     0x8173, 0x0176, 0x017C, 0x8179, 0x0168, 0x816D, 0x8167, 0x0162,
  1928.     0x8123, 0x0126, 0x012C, 0x8129, 0x0138, 0x813D, 0x8137, 0x0132,
  1929.     0x0110, 0x8115, 0x811F, 0x011A, 0x810B, 0x010E, 0x0104, 0x8101,
  1930.     0x8303, 0x0306, 0x030C, 0x8309, 0x0318, 0x831D, 0x8317, 0x0312,
  1931.     0x0330, 0x8335, 0x833F, 0x033A, 0x832B, 0x032E, 0x0324, 0x8321,
  1932.     0x0360, 0x8365, 0x836F, 0x036A, 0x837B, 0x037E, 0x0374, 0x8371,
  1933.     0x8353, 0x0356, 0x035C, 0x8359, 0x0348, 0x834D, 0x8347, 0x0342,
  1934.     0x03C0, 0x83C5, 0x83CF, 0x03CA, 0x83DB, 0x03DE, 0x03D4, 0x83D1,
  1935.     0x83F3, 0x03F6, 0x03FC, 0x83F9, 0x03E8, 0x83ED, 0x83E7, 0x03E2,
  1936.     0x83A3, 0x03A6, 0x03AC, 0x83A9, 0x03B8, 0x83BD, 0x83B7, 0x03B2,
  1937.     0x0390, 0x8395, 0x839F, 0x039A, 0x838B, 0x038E, 0x0384, 0x8381,
  1938.     0x0280, 0x8285, 0x828F, 0x028A, 0x829B, 0x029E, 0x0294, 0x8291,
  1939.     0x82B3, 0x02B6, 0x02BC, 0x82B9, 0x02A8, 0x82AD, 0x82A7, 0x02A2,
  1940.     0x82E3, 0x02E6, 0x02EC, 0x82E9, 0x02F8, 0x82FD, 0x82F7, 0x02F2,
  1941.     0x02D0, 0x82D5, 0x82DF, 0x02DA, 0x82CB, 0x02CE, 0x02C4, 0x82C1,
  1942.     0x8243, 0x0246, 0x024C, 0x8249, 0x0258, 0x825D, 0x8257, 0x0252,
  1943.     0x0270, 0x8275, 0x827F, 0x027A, 0x826B, 0x026E, 0x0264, 0x8261,
  1944.     0x0220, 0x8225, 0x822F, 0x022A, 0x823B, 0x023E, 0x0234, 0x8231,
  1945.     0x8213, 0x0216, 0x021C, 0x8219, 0x0208, 0x820D, 0x8207, 0x0202
  1946. };
  1947.  
  1948. static DRFLAC_INLINE drflac_uint8 drflac_crc8_byte(drflac_uint8 crc, drflac_uint8 data)
  1949. {
  1950.     return drflac__crc8_table[crc ^ data];
  1951. }
  1952.  
  1953. static DRFLAC_INLINE drflac_uint8 drflac_crc8(drflac_uint8 crc, drflac_uint32 data, drflac_uint32 count)
  1954. {
  1955. #ifdef DR_FLAC_NO_CRC
  1956.     (void)crc;
  1957.     (void)data;
  1958.     (void)count;
  1959.     return 0;
  1960. #else
  1961. #if 0
  1962.     /* REFERENCE (use of this implementation requires an explicit flush by doing "drflac_crc8(crc, 0, 8);") */
  1963.     drflac_uint8 p = 0x07;
  1964.     for (int i = count-1; i >= 0; --i) {
  1965.         drflac_uint8 bit = (data & (1 << i)) >> i;
  1966.         if (crc & 0x80) {
  1967.             crc = ((crc << 1) | bit) ^ p;
  1968.         } else {
  1969.             crc = ((crc << 1) | bit);
  1970.         }
  1971.     }
  1972.     return crc;
  1973. #else
  1974.     drflac_uint32 wholeBytes;
  1975.     drflac_uint32 leftoverBits;
  1976.     drflac_uint64 leftoverDataMask;
  1977.  
  1978.     static drflac_uint64 leftoverDataMaskTable[8] = {
  1979.         0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F
  1980.     };
  1981.  
  1982.     DRFLAC_ASSERT(count <= 32);
  1983.  
  1984.     wholeBytes = count >> 3;
  1985.     leftoverBits = count - (wholeBytes*8);
  1986.     leftoverDataMask = leftoverDataMaskTable[leftoverBits];
  1987.  
  1988.     switch (wholeBytes) {
  1989.         case 4: crc = drflac_crc8_byte(crc, (drflac_uint8)((data & (0xFF000000UL << leftoverBits)) >> (24 + leftoverBits)));
  1990.         case 3: crc = drflac_crc8_byte(crc, (drflac_uint8)((data & (0x00FF0000UL << leftoverBits)) >> (16 + leftoverBits)));
  1991.         case 2: crc = drflac_crc8_byte(crc, (drflac_uint8)((data & (0x0000FF00UL << leftoverBits)) >> ( 8 + leftoverBits)));
  1992.         case 1: crc = drflac_crc8_byte(crc, (drflac_uint8)((data & (0x000000FFUL << leftoverBits)) >> ( 0 + leftoverBits)));
  1993.         case 0: if (leftoverBits > 0) crc = (drflac_uint8)((crc << leftoverBits) ^ drflac__crc8_table[(crc >> (8 - leftoverBits)) ^ (data & leftoverDataMask)]);
  1994.     }
  1995.     return crc;
  1996. #endif
  1997. #endif
  1998. }
  1999.  
  2000. static DRFLAC_INLINE drflac_uint16 drflac_crc16_byte(drflac_uint16 crc, drflac_uint8 data)
  2001. {
  2002.     return (crc << 8) ^ drflac__crc16_table[(drflac_uint8)(crc >> 8) ^ data];
  2003. }
  2004.  
  2005. static DRFLAC_INLINE drflac_uint16 drflac_crc16_cache(drflac_uint16 crc, drflac_cache_t data)
  2006. {
  2007. #ifdef DRFLAC_64BIT
  2008.     crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 56) & 0xFF));
  2009.     crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 48) & 0xFF));
  2010.     crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 40) & 0xFF));
  2011.     crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 32) & 0xFF));
  2012. #endif
  2013.     crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 24) & 0xFF));
  2014.     crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 16) & 0xFF));
  2015.     crc = drflac_crc16_byte(crc, (drflac_uint8)((data >>  8) & 0xFF));
  2016.     crc = drflac_crc16_byte(crc, (drflac_uint8)((data >>  0) & 0xFF));
  2017.  
  2018.     return crc;
  2019. }
  2020.  
  2021. static DRFLAC_INLINE drflac_uint16 drflac_crc16_bytes(drflac_uint16 crc, drflac_cache_t data, drflac_uint32 byteCount)
  2022. {
  2023.     switch (byteCount)
  2024.     {
  2025. #ifdef DRFLAC_64BIT
  2026.     case 8: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 56) & 0xFF));
  2027.     case 7: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 48) & 0xFF));
  2028.     case 6: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 40) & 0xFF));
  2029.     case 5: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 32) & 0xFF));
  2030. #endif
  2031.     case 4: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 24) & 0xFF));
  2032.     case 3: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 16) & 0xFF));
  2033.     case 2: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >>  8) & 0xFF));
  2034.     case 1: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >>  0) & 0xFF));
  2035.     }
  2036.  
  2037.     return crc;
  2038. }
  2039.  
  2040. #if 0
  2041. static DRFLAC_INLINE drflac_uint16 drflac_crc16__32bit(drflac_uint16 crc, drflac_uint32 data, drflac_uint32 count)
  2042. {
  2043. #ifdef DR_FLAC_NO_CRC
  2044.     (void)crc;
  2045.     (void)data;
  2046.     (void)count;
  2047.     return 0;
  2048. #else
  2049. #if 0
  2050.     /* REFERENCE (use of this implementation requires an explicit flush by doing "drflac_crc16(crc, 0, 16);") */
  2051.     drflac_uint16 p = 0x8005;
  2052.     for (int i = count-1; i >= 0; --i) {
  2053.         drflac_uint16 bit = (data & (1ULL << i)) >> i;
  2054.         if (r & 0x8000) {
  2055.             r = ((r << 1) | bit) ^ p;
  2056.         } else {
  2057.             r = ((r << 1) | bit);
  2058.         }
  2059.     }
  2060.  
  2061.     return crc;
  2062. #else
  2063.     drflac_uint32 wholeBytes;
  2064.     drflac_uint32 leftoverBits;
  2065.     drflac_uint64 leftoverDataMask;
  2066.  
  2067.     static drflac_uint64 leftoverDataMaskTable[8] = {
  2068.         0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F
  2069.     };
  2070.  
  2071.     DRFLAC_ASSERT(count <= 64);
  2072.  
  2073.     wholeBytes = count >> 3;
  2074.     leftoverBits = count & 7;
  2075.     leftoverDataMask = leftoverDataMaskTable[leftoverBits];
  2076.  
  2077.     switch (wholeBytes) {
  2078.         default:
  2079.         case 4: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (0xFF000000UL << leftoverBits)) >> (24 + leftoverBits)));
  2080.         case 3: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (0x00FF0000UL << leftoverBits)) >> (16 + leftoverBits)));
  2081.         case 2: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (0x0000FF00UL << leftoverBits)) >> ( 8 + leftoverBits)));
  2082.         case 1: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (0x000000FFUL << leftoverBits)) >> ( 0 + leftoverBits)));
  2083.         case 0: if (leftoverBits > 0) crc = (crc << leftoverBits) ^ drflac__crc16_table[(crc >> (16 - leftoverBits)) ^ (data & leftoverDataMask)];
  2084.     }
  2085.     return crc;
  2086. #endif
  2087. #endif
  2088. }
  2089.  
  2090. static DRFLAC_INLINE drflac_uint16 drflac_crc16__64bit(drflac_uint16 crc, drflac_uint64 data, drflac_uint32 count)
  2091. {
  2092. #ifdef DR_FLAC_NO_CRC
  2093.     (void)crc;
  2094.     (void)data;
  2095.     (void)count;
  2096.     return 0;
  2097. #else
  2098.     drflac_uint32 wholeBytes;
  2099.     drflac_uint32 leftoverBits;
  2100.     drflac_uint64 leftoverDataMask;
  2101.  
  2102.     static drflac_uint64 leftoverDataMaskTable[8] = {
  2103.         0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F
  2104.     };
  2105.  
  2106.     DRFLAC_ASSERT(count <= 64);
  2107.  
  2108.     wholeBytes = count >> 3;
  2109.     leftoverBits = count & 7;
  2110.     leftoverDataMask = leftoverDataMaskTable[leftoverBits];
  2111.  
  2112.     switch (wholeBytes) {
  2113.         default:
  2114.         case 8: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0xFF000000 << 32) << leftoverBits)) >> (56 + leftoverBits)));    /* Weird "<< 32" bitshift is required for C89 because it doesn't support 64-bit constants. Should be optimized out by a good compiler. */
  2115.         case 7: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0x00FF0000 << 32) << leftoverBits)) >> (48 + leftoverBits)));
  2116.         case 6: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0x0000FF00 << 32) << leftoverBits)) >> (40 + leftoverBits)));
  2117.         case 5: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0x000000FF << 32) << leftoverBits)) >> (32 + leftoverBits)));
  2118.         case 4: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0xFF000000      ) << leftoverBits)) >> (24 + leftoverBits)));
  2119.         case 3: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0x00FF0000      ) << leftoverBits)) >> (16 + leftoverBits)));
  2120.         case 2: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0x0000FF00      ) << leftoverBits)) >> ( 8 + leftoverBits)));
  2121.         case 1: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0x000000FF      ) << leftoverBits)) >> ( 0 + leftoverBits)));
  2122.         case 0: if (leftoverBits > 0) crc = (crc << leftoverBits) ^ drflac__crc16_table[(crc >> (16 - leftoverBits)) ^ (data & leftoverDataMask)];
  2123.     }
  2124.     return crc;
  2125. #endif
  2126. }
  2127.  
  2128.  
  2129. static DRFLAC_INLINE drflac_uint16 drflac_crc16(drflac_uint16 crc, drflac_cache_t data, drflac_uint32 count)
  2130. {
  2131. #ifdef DRFLAC_64BIT
  2132.     return drflac_crc16__64bit(crc, data, count);
  2133. #else
  2134.     return drflac_crc16__32bit(crc, data, count);
  2135. #endif
  2136. }
  2137. #endif
  2138.  
  2139.  
  2140. #ifdef DRFLAC_64BIT
  2141. #define drflac__be2host__cache_line drflac__be2host_64
  2142. #else
  2143. #define drflac__be2host__cache_line drflac__be2host_32
  2144. #endif
  2145.  
  2146. /*
  2147. BIT READING ATTEMPT #2
  2148.  
  2149. This uses a 32- or 64-bit bit-shifted cache - as bits are read, the cache is shifted such that the first valid bit is sitting
  2150. on the most significant bit. It uses the notion of an L1 and L2 cache (borrowed from CPU architecture), where the L1 cache
  2151. is a 32- or 64-bit unsigned integer (depending on whether or not a 32- or 64-bit build is being compiled) and the L2 is an
  2152. array of "cache lines", with each cache line being the same size as the L1. The L2 is a buffer of about 4KB and is where data
  2153. from onRead() is read into.
  2154. */
  2155. #define DRFLAC_CACHE_L1_SIZE_BYTES(bs)                      (sizeof((bs)->cache))
  2156. #define DRFLAC_CACHE_L1_SIZE_BITS(bs)                       (sizeof((bs)->cache)*8)
  2157. #define DRFLAC_CACHE_L1_BITS_REMAINING(bs)                  (DRFLAC_CACHE_L1_SIZE_BITS(bs) - (bs)->consumedBits)
  2158. #define DRFLAC_CACHE_L1_SELECTION_MASK(_bitCount)           (~((~(drflac_cache_t)0) >> (_bitCount)))
  2159. #define DRFLAC_CACHE_L1_SELECTION_SHIFT(bs, _bitCount)      (DRFLAC_CACHE_L1_SIZE_BITS(bs) - (_bitCount))
  2160. #define DRFLAC_CACHE_L1_SELECT(bs, _bitCount)               (((bs)->cache) & DRFLAC_CACHE_L1_SELECTION_MASK(_bitCount))
  2161. #define DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, _bitCount)     (DRFLAC_CACHE_L1_SELECT((bs), (_bitCount)) >>  DRFLAC_CACHE_L1_SELECTION_SHIFT((bs), (_bitCount)))
  2162. #define DRFLAC_CACHE_L1_SELECT_AND_SHIFT_SAFE(bs, _bitCount)(DRFLAC_CACHE_L1_SELECT((bs), (_bitCount)) >> (DRFLAC_CACHE_L1_SELECTION_SHIFT((bs), (_bitCount)) & (DRFLAC_CACHE_L1_SIZE_BITS(bs)-1)))
  2163. #define DRFLAC_CACHE_L2_SIZE_BYTES(bs)                      (sizeof((bs)->cacheL2))
  2164. #define DRFLAC_CACHE_L2_LINE_COUNT(bs)                      (DRFLAC_CACHE_L2_SIZE_BYTES(bs) / sizeof((bs)->cacheL2[0]))
  2165. #define DRFLAC_CACHE_L2_LINES_REMAINING(bs)                 (DRFLAC_CACHE_L2_LINE_COUNT(bs) - (bs)->nextL2Line)
  2166.  
  2167.  
  2168. #ifndef DR_FLAC_NO_CRC
  2169. static DRFLAC_INLINE void drflac__reset_crc16(drflac_bs* bs)
  2170. {
  2171.     bs->crc16 = 0;
  2172.     bs->crc16CacheIgnoredBytes = bs->consumedBits >> 3;
  2173. }
  2174.  
  2175. static DRFLAC_INLINE void drflac__update_crc16(drflac_bs* bs)
  2176. {
  2177.     if (bs->crc16CacheIgnoredBytes == 0) {
  2178.         bs->crc16 = drflac_crc16_cache(bs->crc16, bs->crc16Cache);
  2179.     } else {
  2180.         bs->crc16 = drflac_crc16_bytes(bs->crc16, bs->crc16Cache, DRFLAC_CACHE_L1_SIZE_BYTES(bs) - bs->crc16CacheIgnoredBytes);
  2181.         bs->crc16CacheIgnoredBytes = 0;
  2182.     }
  2183. }
  2184.  
  2185. static DRFLAC_INLINE drflac_uint16 drflac__flush_crc16(drflac_bs* bs)
  2186. {
  2187.     /* We should never be flushing in a situation where we are not aligned on a byte boundary. */
  2188.     DRFLAC_ASSERT((DRFLAC_CACHE_L1_BITS_REMAINING(bs) & 7) == 0);
  2189.  
  2190.     /*
  2191.     The bits that were read from the L1 cache need to be accumulated. The number of bytes needing to be accumulated is determined
  2192.     by the number of bits that have been consumed.
  2193.     */
  2194.     if (DRFLAC_CACHE_L1_BITS_REMAINING(bs) == 0) {
  2195.         drflac__update_crc16(bs);
  2196.     } else {
  2197.         /* We only accumulate the consumed bits. */
  2198.         bs->crc16 = drflac_crc16_bytes(bs->crc16, bs->crc16Cache >> DRFLAC_CACHE_L1_BITS_REMAINING(bs), (bs->consumedBits >> 3) - bs->crc16CacheIgnoredBytes);
  2199.  
  2200.         /*
  2201.         The bits that we just accumulated should never be accumulated again. We need to keep track of how many bytes were accumulated
  2202.         so we can handle that later.
  2203.         */
  2204.         bs->crc16CacheIgnoredBytes = bs->consumedBits >> 3;
  2205.     }
  2206.  
  2207.     return bs->crc16;
  2208. }
  2209. #endif
  2210.  
  2211. static DRFLAC_INLINE drflac_bool32 drflac__reload_l1_cache_from_l2(drflac_bs* bs)
  2212. {
  2213.     size_t bytesRead;
  2214.     size_t alignedL1LineCount;
  2215.  
  2216.     /* Fast path. Try loading straight from L2. */
  2217.     if (bs->nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) {
  2218.         bs->cache = bs->cacheL2[bs->nextL2Line++];
  2219.         return DRFLAC_TRUE;
  2220.     }
  2221.  
  2222.     /*
  2223.     If we get here it means we've run out of data in the L2 cache. We'll need to fetch more from the client, if there's
  2224.     any left.
  2225.     */
  2226.     if (bs->unalignedByteCount > 0) {
  2227.         return DRFLAC_FALSE;   /* If we have any unaligned bytes it means there's no more aligned bytes left in the client. */
  2228.     }
  2229.  
  2230.     bytesRead = bs->onRead(bs->pUserData, bs->cacheL2, DRFLAC_CACHE_L2_SIZE_BYTES(bs));
  2231.  
  2232.     bs->nextL2Line = 0;
  2233.     if (bytesRead == DRFLAC_CACHE_L2_SIZE_BYTES(bs)) {
  2234.         bs->cache = bs->cacheL2[bs->nextL2Line++];
  2235.         return DRFLAC_TRUE;
  2236.     }
  2237.  
  2238.  
  2239.     /*
  2240.     If we get here it means we were unable to retrieve enough data to fill the entire L2 cache. It probably
  2241.     means we've just reached the end of the file. We need to move the valid data down to the end of the buffer
  2242.     and adjust the index of the next line accordingly. Also keep in mind that the L2 cache must be aligned to
  2243.     the size of the L1 so we'll need to seek backwards by any misaligned bytes.
  2244.     */
  2245.     alignedL1LineCount = bytesRead / DRFLAC_CACHE_L1_SIZE_BYTES(bs);
  2246.  
  2247.     /* We need to keep track of any unaligned bytes for later use. */
  2248.     bs->unalignedByteCount = bytesRead - (alignedL1LineCount * DRFLAC_CACHE_L1_SIZE_BYTES(bs));
  2249.     if (bs->unalignedByteCount > 0) {
  2250.         bs->unalignedCache = bs->cacheL2[alignedL1LineCount];
  2251.     }
  2252.  
  2253.     if (alignedL1LineCount > 0) {
  2254.         size_t offset = DRFLAC_CACHE_L2_LINE_COUNT(bs) - alignedL1LineCount;
  2255.         size_t i;
  2256.         for (i = alignedL1LineCount; i > 0; --i) {
  2257.             bs->cacheL2[i-1 + offset] = bs->cacheL2[i-1];
  2258.         }
  2259.  
  2260.         bs->nextL2Line = (drflac_uint32)offset;
  2261.         bs->cache = bs->cacheL2[bs->nextL2Line++];
  2262.         return DRFLAC_TRUE;
  2263.     } else {
  2264.         /* If we get into this branch it means we weren't able to load any L1-aligned data. */
  2265.         bs->nextL2Line = DRFLAC_CACHE_L2_LINE_COUNT(bs);
  2266.         return DRFLAC_FALSE;
  2267.     }
  2268. }
  2269.  
  2270. static drflac_bool32 drflac__reload_cache(drflac_bs* bs)
  2271. {
  2272.     size_t bytesRead;
  2273.  
  2274. #ifndef DR_FLAC_NO_CRC
  2275.     drflac__update_crc16(bs);
  2276. #endif
  2277.  
  2278.     /* Fast path. Try just moving the next value in the L2 cache to the L1 cache. */
  2279.     if (drflac__reload_l1_cache_from_l2(bs)) {
  2280.         bs->cache = drflac__be2host__cache_line(bs->cache);
  2281.         bs->consumedBits = 0;
  2282. #ifndef DR_FLAC_NO_CRC
  2283.         bs->crc16Cache = bs->cache;
  2284. #endif
  2285.         return DRFLAC_TRUE;
  2286.     }
  2287.  
  2288.     /* Slow path. */
  2289.  
  2290.     /*
  2291.     If we get here it means we have failed to load the L1 cache from the L2. Likely we've just reached the end of the stream and the last
  2292.     few bytes did not meet the alignment requirements for the L2 cache. In this case we need to fall back to a slower path and read the
  2293.     data from the unaligned cache.
  2294.     */
  2295.     bytesRead = bs->unalignedByteCount;
  2296.     if (bytesRead == 0) {
  2297.         bs->consumedBits = DRFLAC_CACHE_L1_SIZE_BITS(bs);   /* <-- The stream has been exhausted, so marked the bits as consumed. */
  2298.         return DRFLAC_FALSE;
  2299.     }
  2300.  
  2301.     DRFLAC_ASSERT(bytesRead < DRFLAC_CACHE_L1_SIZE_BYTES(bs));
  2302.     bs->consumedBits = (drflac_uint32)(DRFLAC_CACHE_L1_SIZE_BYTES(bs) - bytesRead) * 8;
  2303.  
  2304.     bs->cache = drflac__be2host__cache_line(bs->unalignedCache);
  2305.     bs->cache &= DRFLAC_CACHE_L1_SELECTION_MASK(DRFLAC_CACHE_L1_BITS_REMAINING(bs));    /* <-- Make sure the consumed bits are always set to zero. Other parts of the library depend on this property. */
  2306.     bs->unalignedByteCount = 0;     /* <-- At this point the unaligned bytes have been moved into the cache and we thus have no more unaligned bytes. */
  2307.  
  2308. #ifndef DR_FLAC_NO_CRC
  2309.     bs->crc16Cache = bs->cache >> bs->consumedBits;
  2310.     bs->crc16CacheIgnoredBytes = bs->consumedBits >> 3;
  2311. #endif
  2312.     return DRFLAC_TRUE;
  2313. }
  2314.  
  2315. static void drflac__reset_cache(drflac_bs* bs)
  2316. {
  2317.     bs->nextL2Line   = DRFLAC_CACHE_L2_LINE_COUNT(bs);  /* <-- This clears the L2 cache. */
  2318.     bs->consumedBits = DRFLAC_CACHE_L1_SIZE_BITS(bs);   /* <-- This clears the L1 cache. */
  2319.     bs->cache = 0;
  2320.     bs->unalignedByteCount = 0;                         /* <-- This clears the trailing unaligned bytes. */
  2321.     bs->unalignedCache = 0;
  2322.  
  2323. #ifndef DR_FLAC_NO_CRC
  2324.     bs->crc16Cache = 0;
  2325.     bs->crc16CacheIgnoredBytes = 0;
  2326. #endif
  2327. }
  2328.  
  2329.  
  2330. static DRFLAC_INLINE drflac_bool32 drflac__read_uint32(drflac_bs* bs, unsigned int bitCount, drflac_uint32* pResultOut)
  2331. {
  2332.     DRFLAC_ASSERT(bs != NULL);
  2333.     DRFLAC_ASSERT(pResultOut != NULL);
  2334.     DRFLAC_ASSERT(bitCount > 0);
  2335.     DRFLAC_ASSERT(bitCount <= 32);
  2336.  
  2337.     if (bs->consumedBits == DRFLAC_CACHE_L1_SIZE_BITS(bs)) {
  2338.         if (!drflac__reload_cache(bs)) {
  2339.             return DRFLAC_FALSE;
  2340.         }
  2341.     }
  2342.  
  2343.     if (bitCount <= DRFLAC_CACHE_L1_BITS_REMAINING(bs)) {
  2344.         /*
  2345.         If we want to load all 32-bits from a 32-bit cache we need to do it slightly differently because we can't do
  2346.         a 32-bit shift on a 32-bit integer. This will never be the case on 64-bit caches, so we can have a slightly
  2347.         more optimal solution for this.
  2348.         */
  2349. #ifdef DRFLAC_64BIT
  2350.         *pResultOut = (drflac_uint32)DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, bitCount);
  2351.         bs->consumedBits += bitCount;
  2352.         bs->cache <<= bitCount;
  2353. #else
  2354.         if (bitCount < DRFLAC_CACHE_L1_SIZE_BITS(bs)) {
  2355.             *pResultOut = (drflac_uint32)DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, bitCount);
  2356.             bs->consumedBits += bitCount;
  2357.             bs->cache <<= bitCount;
  2358.         } else {
  2359.             /* Cannot shift by 32-bits, so need to do it differently. */
  2360.             *pResultOut = (drflac_uint32)bs->cache;
  2361.             bs->consumedBits = DRFLAC_CACHE_L1_SIZE_BITS(bs);
  2362.             bs->cache = 0;
  2363.         }
  2364. #endif
  2365.  
  2366.         return DRFLAC_TRUE;
  2367.     } else {
  2368.         /* It straddles the cached data. It will never cover more than the next chunk. We just read the number in two parts and combine them. */
  2369.         drflac_uint32 bitCountHi = DRFLAC_CACHE_L1_BITS_REMAINING(bs);
  2370.         drflac_uint32 bitCountLo = bitCount - bitCountHi;
  2371.         drflac_uint32 resultHi;
  2372.  
  2373.         DRFLAC_ASSERT(bitCountHi > 0);
  2374.         DRFLAC_ASSERT(bitCountHi < 32);
  2375.         resultHi = (drflac_uint32)DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, bitCountHi);
  2376.  
  2377.         if (!drflac__reload_cache(bs)) {
  2378.             return DRFLAC_FALSE;
  2379.         }
  2380.  
  2381.         *pResultOut = (resultHi << bitCountLo) | (drflac_uint32)DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, bitCountLo);
  2382.         bs->consumedBits += bitCountLo;
  2383.         bs->cache <<= bitCountLo;
  2384.         return DRFLAC_TRUE;
  2385.     }
  2386. }
  2387.  
  2388. static drflac_bool32 drflac__read_int32(drflac_bs* bs, unsigned int bitCount, drflac_int32* pResult)
  2389. {
  2390.     drflac_uint32 result;
  2391.     drflac_uint32 signbit;
  2392.  
  2393.     DRFLAC_ASSERT(bs != NULL);
  2394.     DRFLAC_ASSERT(pResult != NULL);
  2395.     DRFLAC_ASSERT(bitCount > 0);
  2396.     DRFLAC_ASSERT(bitCount <= 32);
  2397.  
  2398.     if (!drflac__read_uint32(bs, bitCount, &result)) {
  2399.         return DRFLAC_FALSE;
  2400.     }
  2401.  
  2402.     signbit = ((result >> (bitCount-1)) & 0x01);
  2403.     result |= (~signbit + 1) << bitCount;
  2404.  
  2405.     *pResult = (drflac_int32)result;
  2406.     return DRFLAC_TRUE;
  2407. }
  2408.  
  2409. #ifdef DRFLAC_64BIT
  2410. static drflac_bool32 drflac__read_uint64(drflac_bs* bs, unsigned int bitCount, drflac_uint64* pResultOut)
  2411. {
  2412.     drflac_uint32 resultHi;
  2413.     drflac_uint32 resultLo;
  2414.  
  2415.     DRFLAC_ASSERT(bitCount <= 64);
  2416.     DRFLAC_ASSERT(bitCount >  32);
  2417.  
  2418.     if (!drflac__read_uint32(bs, bitCount - 32, &resultHi)) {
  2419.         return DRFLAC_FALSE;
  2420.     }
  2421.  
  2422.     if (!drflac__read_uint32(bs, 32, &resultLo)) {
  2423.         return DRFLAC_FALSE;
  2424.     }
  2425.  
  2426.     *pResultOut = (((drflac_uint64)resultHi) << 32) | ((drflac_uint64)resultLo);
  2427.     return DRFLAC_TRUE;
  2428. }
  2429. #endif
  2430.  
  2431. /* Function below is unused, but leaving it here in case I need to quickly add it again. */
  2432. #if 0
  2433. static drflac_bool32 drflac__read_int64(drflac_bs* bs, unsigned int bitCount, drflac_int64* pResultOut)
  2434. {
  2435.     drflac_uint64 result;
  2436.     drflac_uint64 signbit;
  2437.  
  2438.     DRFLAC_ASSERT(bitCount <= 64);
  2439.  
  2440.     if (!drflac__read_uint64(bs, bitCount, &result)) {
  2441.         return DRFLAC_FALSE;
  2442.     }
  2443.  
  2444.     signbit = ((result >> (bitCount-1)) & 0x01);
  2445.     result |= (~signbit + 1) << bitCount;
  2446.  
  2447.     *pResultOut = (drflac_int64)result;
  2448.     return DRFLAC_TRUE;
  2449. }
  2450. #endif
  2451.  
  2452. static drflac_bool32 drflac__read_uint16(drflac_bs* bs, unsigned int bitCount, drflac_uint16* pResult)
  2453. {
  2454.     drflac_uint32 result;
  2455.  
  2456.     DRFLAC_ASSERT(bs != NULL);
  2457.     DRFLAC_ASSERT(pResult != NULL);
  2458.     DRFLAC_ASSERT(bitCount > 0);
  2459.     DRFLAC_ASSERT(bitCount <= 16);
  2460.  
  2461.     if (!drflac__read_uint32(bs, bitCount, &result)) {
  2462.         return DRFLAC_FALSE;
  2463.     }
  2464.  
  2465.     *pResult = (drflac_uint16)result;
  2466.     return DRFLAC_TRUE;
  2467. }
  2468.  
  2469. #if 0
  2470. static drflac_bool32 drflac__read_int16(drflac_bs* bs, unsigned int bitCount, drflac_int16* pResult)
  2471. {
  2472.     drflac_int32 result;
  2473.  
  2474.     DRFLAC_ASSERT(bs != NULL);
  2475.     DRFLAC_ASSERT(pResult != NULL);
  2476.     DRFLAC_ASSERT(bitCount > 0);
  2477.     DRFLAC_ASSERT(bitCount <= 16);
  2478.  
  2479.     if (!drflac__read_int32(bs, bitCount, &result)) {
  2480.         return DRFLAC_FALSE;
  2481.     }
  2482.  
  2483.     *pResult = (drflac_int16)result;
  2484.     return DRFLAC_TRUE;
  2485. }
  2486. #endif
  2487.  
  2488. static drflac_bool32 drflac__read_uint8(drflac_bs* bs, unsigned int bitCount, drflac_uint8* pResult)
  2489. {
  2490.     drflac_uint32 result;
  2491.  
  2492.     DRFLAC_ASSERT(bs != NULL);
  2493.     DRFLAC_ASSERT(pResult != NULL);
  2494.     DRFLAC_ASSERT(bitCount > 0);
  2495.     DRFLAC_ASSERT(bitCount <= 8);
  2496.  
  2497.     if (!drflac__read_uint32(bs, bitCount, &result)) {
  2498.         return DRFLAC_FALSE;
  2499.     }
  2500.  
  2501.     *pResult = (drflac_uint8)result;
  2502.     return DRFLAC_TRUE;
  2503. }
  2504.  
  2505. static drflac_bool32 drflac__read_int8(drflac_bs* bs, unsigned int bitCount, drflac_int8* pResult)
  2506. {
  2507.     drflac_int32 result;
  2508.  
  2509.     DRFLAC_ASSERT(bs != NULL);
  2510.     DRFLAC_ASSERT(pResult != NULL);
  2511.     DRFLAC_ASSERT(bitCount > 0);
  2512.     DRFLAC_ASSERT(bitCount <= 8);
  2513.  
  2514.     if (!drflac__read_int32(bs, bitCount, &result)) {
  2515.         return DRFLAC_FALSE;
  2516.     }
  2517.  
  2518.     *pResult = (drflac_int8)result;
  2519.     return DRFLAC_TRUE;
  2520. }
  2521.  
  2522.  
  2523. static drflac_bool32 drflac__seek_bits(drflac_bs* bs, size_t bitsToSeek)
  2524. {
  2525.     if (bitsToSeek <= DRFLAC_CACHE_L1_BITS_REMAINING(bs)) {
  2526.         bs->consumedBits += (drflac_uint32)bitsToSeek;
  2527.         bs->cache <<= bitsToSeek;
  2528.         return DRFLAC_TRUE;
  2529.     } else {
  2530.         /* It straddles the cached data. This function isn't called too frequently so I'm favouring simplicity here. */
  2531.         bitsToSeek       -= DRFLAC_CACHE_L1_BITS_REMAINING(bs);
  2532.         bs->consumedBits += DRFLAC_CACHE_L1_BITS_REMAINING(bs);
  2533.         bs->cache         = 0;
  2534.  
  2535.         /* Simple case. Seek in groups of the same number as bits that fit within a cache line. */
  2536. #ifdef DRFLAC_64BIT
  2537.         while (bitsToSeek >= DRFLAC_CACHE_L1_SIZE_BITS(bs)) {
  2538.             drflac_uint64 bin;
  2539.             if (!drflac__read_uint64(bs, DRFLAC_CACHE_L1_SIZE_BITS(bs), &bin)) {
  2540.                 return DRFLAC_FALSE;
  2541.             }
  2542.             bitsToSeek -= DRFLAC_CACHE_L1_SIZE_BITS(bs);
  2543.         }
  2544. #else
  2545.         while (bitsToSeek >= DRFLAC_CACHE_L1_SIZE_BITS(bs)) {
  2546.             drflac_uint32 bin;
  2547.             if (!drflac__read_uint32(bs, DRFLAC_CACHE_L1_SIZE_BITS(bs), &bin)) {
  2548.                 return DRFLAC_FALSE;
  2549.             }
  2550.             bitsToSeek -= DRFLAC_CACHE_L1_SIZE_BITS(bs);
  2551.         }
  2552. #endif
  2553.  
  2554.         /* Whole leftover bytes. */
  2555.         while (bitsToSeek >= 8) {
  2556.             drflac_uint8 bin;
  2557.             if (!drflac__read_uint8(bs, 8, &bin)) {
  2558.                 return DRFLAC_FALSE;
  2559.             }
  2560.             bitsToSeek -= 8;
  2561.         }
  2562.  
  2563.         /* Leftover bits. */
  2564.         if (bitsToSeek > 0) {
  2565.             drflac_uint8 bin;
  2566.             if (!drflac__read_uint8(bs, (drflac_uint32)bitsToSeek, &bin)) {
  2567.                 return DRFLAC_FALSE;
  2568.             }
  2569.             bitsToSeek = 0; /* <-- Necessary for the assert below. */
  2570.         }
  2571.  
  2572.         DRFLAC_ASSERT(bitsToSeek == 0);
  2573.         return DRFLAC_TRUE;
  2574.     }
  2575. }
  2576.  
  2577.  
  2578. /* This function moves the bit streamer to the first bit after the sync code (bit 15 of the of the frame header). It will also update the CRC-16. */
  2579. static drflac_bool32 drflac__find_and_seek_to_next_sync_code(drflac_bs* bs)
  2580. {
  2581.     DRFLAC_ASSERT(bs != NULL);
  2582.  
  2583.     /*
  2584.     The sync code is always aligned to 8 bits. This is convenient for us because it means we can do byte-aligned movements. The first
  2585.     thing to do is align to the next byte.
  2586.     */
  2587.     if (!drflac__seek_bits(bs, DRFLAC_CACHE_L1_BITS_REMAINING(bs) & 7)) {
  2588.         return DRFLAC_FALSE;
  2589.     }
  2590.  
  2591.     for (;;) {
  2592.         drflac_uint8 hi;
  2593.  
  2594. #ifndef DR_FLAC_NO_CRC
  2595.         drflac__reset_crc16(bs);
  2596. #endif
  2597.  
  2598.         if (!drflac__read_uint8(bs, 8, &hi)) {
  2599.             return DRFLAC_FALSE;
  2600.         }
  2601.  
  2602.         if (hi == 0xFF) {
  2603.             drflac_uint8 lo;
  2604.             if (!drflac__read_uint8(bs, 6, &lo)) {
  2605.                 return DRFLAC_FALSE;
  2606.             }
  2607.  
  2608.             if (lo == 0x3E) {
  2609.                 return DRFLAC_TRUE;
  2610.             } else {
  2611.                 if (!drflac__seek_bits(bs, DRFLAC_CACHE_L1_BITS_REMAINING(bs) & 7)) {
  2612.                     return DRFLAC_FALSE;
  2613.                 }
  2614.             }
  2615.         }
  2616.     }
  2617.  
  2618.     /* Should never get here. */
  2619.     /*return DRFLAC_FALSE;*/
  2620. }
  2621.  
  2622.  
  2623. #if defined(DRFLAC_HAS_LZCNT_INTRINSIC)
  2624. #define DRFLAC_IMPLEMENT_CLZ_LZCNT
  2625. #endif
  2626. #if  defined(_MSC_VER) && _MSC_VER >= 1400 && (defined(DRFLAC_X64) || defined(DRFLAC_X86))
  2627. #define DRFLAC_IMPLEMENT_CLZ_MSVC
  2628. #endif
  2629.  
  2630. static DRFLAC_INLINE drflac_uint32 drflac__clz_software(drflac_cache_t x)
  2631. {
  2632.     drflac_uint32 n;
  2633.     static drflac_uint32 clz_table_4[] = {
  2634.         0,
  2635.         4,
  2636.         3, 3,
  2637.         2, 2, 2, 2,
  2638.         1, 1, 1, 1, 1, 1, 1, 1
  2639.     };
  2640.  
  2641.     if (x == 0) {
  2642.         return sizeof(x)*8;
  2643.     }
  2644.  
  2645.     n = clz_table_4[x >> (sizeof(x)*8 - 4)];
  2646.     if (n == 0) {
  2647. #ifdef DRFLAC_64BIT
  2648.         if ((x & ((drflac_uint64)0xFFFFFFFF << 32)) == 0) { n  = 32; x <<= 32; }
  2649.         if ((x & ((drflac_uint64)0xFFFF0000 << 32)) == 0) { n += 16; x <<= 16; }
  2650.         if ((x & ((drflac_uint64)0xFF000000 << 32)) == 0) { n += 8;  x <<= 8;  }
  2651.         if ((x & ((drflac_uint64)0xF0000000 << 32)) == 0) { n += 4;  x <<= 4;  }
  2652. #else
  2653.         if ((x & 0xFFFF0000) == 0) { n  = 16; x <<= 16; }
  2654.         if ((x & 0xFF000000) == 0) { n += 8;  x <<= 8;  }
  2655.         if ((x & 0xF0000000) == 0) { n += 4;  x <<= 4;  }
  2656. #endif
  2657.         n += clz_table_4[x >> (sizeof(x)*8 - 4)];
  2658.     }
  2659.  
  2660.     return n - 1;
  2661. }
  2662.  
  2663. #ifdef DRFLAC_IMPLEMENT_CLZ_LZCNT
  2664. static DRFLAC_INLINE drflac_bool32 drflac__is_lzcnt_supported(void)
  2665. {
  2666.     /* Fast compile time check for ARM. */
  2667. #if defined(DRFLAC_HAS_LZCNT_INTRINSIC) && defined(DRFLAC_ARM) && (defined(__ARM_ARCH) && __ARM_ARCH >= 5)
  2668.     return DRFLAC_TRUE;
  2669. #else
  2670.     /* If the compiler itself does not support the intrinsic then we'll need to return false. */
  2671.     #ifdef DRFLAC_HAS_LZCNT_INTRINSIC
  2672.         return drflac__gIsLZCNTSupported;
  2673.     #else
  2674.         return DRFLAC_FALSE;
  2675.     #endif
  2676. #endif
  2677. }
  2678.  
  2679. static DRFLAC_INLINE drflac_uint32 drflac__clz_lzcnt(drflac_cache_t x)
  2680. {
  2681. #if defined(_MSC_VER) && !defined(__clang__)
  2682.     #ifdef DRFLAC_64BIT
  2683.         return (drflac_uint32)__lzcnt64(x);
  2684.     #else
  2685.         return (drflac_uint32)__lzcnt(x);
  2686.     #endif
  2687. #else
  2688.     #if defined(__GNUC__) || defined(__clang__)
  2689.         #if defined(DRFLAC_X64)
  2690.             {
  2691.                 drflac_uint64 r;
  2692.                 __asm__ __volatile__ (
  2693.                     "lzcnt{ %1, %0| %0, %1}" : "=r"(r) : "r"(x)
  2694.                 );
  2695.  
  2696.                 return (drflac_uint32)r;
  2697.             }
  2698.         #elif defined(DRFLAC_X86)
  2699.             {
  2700.                 drflac_uint32 r;
  2701.                 __asm__ __volatile__ (
  2702.                     "lzcnt{l %1, %0| %0, %1}" : "=r"(r) : "r"(x)
  2703.                 );
  2704.  
  2705.                 return r;
  2706.             }
  2707.         #elif defined(DRFLAC_ARM) && (defined(__ARM_ARCH) && __ARM_ARCH >= 5) && !defined(DRFLAC_64BIT)   /* <-- I haven't tested 64-bit inline assembly, so only enabling this for the 32-bit build for now. */
  2708.             {
  2709.                 unsigned int r;
  2710.                 __asm__ __volatile__ (
  2711.                 #if defined(DRFLAC_64BIT)
  2712.                     "clz %w[out], %w[in]" : [out]"=r"(r) : [in]"r"(x)   /* <-- This is untested. If someone in the community could test this, that would be appreciated! */
  2713.                 #else
  2714.                     "clz %[out], %[in]" : [out]"=r"(r) : [in]"r"(x)
  2715.                 #endif
  2716.                 );
  2717.  
  2718.                 return r;
  2719.             }
  2720.         #else
  2721.             if (x == 0) {
  2722.                 return sizeof(x)*8;
  2723.             }
  2724.             #ifdef DRFLAC_64BIT
  2725.                 return (drflac_uint32)__builtin_clzll((drflac_uint64)x);
  2726.             #else
  2727.                 return (drflac_uint32)__builtin_clzl((drflac_uint32)x);
  2728.             #endif
  2729.         #endif
  2730.     #else
  2731.         /* Unsupported compiler. */
  2732.         #error "This compiler does not support the lzcnt intrinsic."
  2733.     #endif
  2734. #endif
  2735. }
  2736. #endif
  2737.  
  2738. #ifdef DRFLAC_IMPLEMENT_CLZ_MSVC
  2739. #include <intrin.h> /* For BitScanReverse(). */
  2740.  
  2741. static DRFLAC_INLINE drflac_uint32 drflac__clz_msvc(drflac_cache_t x)
  2742. {
  2743.     drflac_uint32 n;
  2744.  
  2745.     if (x == 0) {
  2746.         return sizeof(x)*8;
  2747.     }
  2748.  
  2749. #ifdef DRFLAC_64BIT
  2750.     _BitScanReverse64((unsigned long*)&n, x);
  2751. #else
  2752.     _BitScanReverse((unsigned long*)&n, x);
  2753. #endif
  2754.     return sizeof(x)*8 - n - 1;
  2755. }
  2756. #endif
  2757.  
  2758. static DRFLAC_INLINE drflac_uint32 drflac__clz(drflac_cache_t x)
  2759. {
  2760. #ifdef DRFLAC_IMPLEMENT_CLZ_LZCNT
  2761.     if (drflac__is_lzcnt_supported()) {
  2762.         return drflac__clz_lzcnt(x);
  2763.     } else
  2764. #endif
  2765.     {
  2766. #ifdef DRFLAC_IMPLEMENT_CLZ_MSVC
  2767.         return drflac__clz_msvc(x);
  2768. #else
  2769.         return drflac__clz_software(x);
  2770. #endif
  2771.     }
  2772. }
  2773.  
  2774.  
  2775. static DRFLAC_INLINE drflac_bool32 drflac__seek_past_next_set_bit(drflac_bs* bs, unsigned int* pOffsetOut)
  2776. {
  2777.     drflac_uint32 zeroCounter = 0;
  2778.     drflac_uint32 setBitOffsetPlus1;
  2779.  
  2780.     while (bs->cache == 0) {
  2781.         zeroCounter += (drflac_uint32)DRFLAC_CACHE_L1_BITS_REMAINING(bs);
  2782.         if (!drflac__reload_cache(bs)) {
  2783.             return DRFLAC_FALSE;
  2784.         }
  2785.     }
  2786.  
  2787.     setBitOffsetPlus1 = drflac__clz(bs->cache);
  2788.     setBitOffsetPlus1 += 1;
  2789.  
  2790.     bs->consumedBits += setBitOffsetPlus1;
  2791.     bs->cache <<= setBitOffsetPlus1;
  2792.  
  2793.     *pOffsetOut = zeroCounter + setBitOffsetPlus1 - 1;
  2794.     return DRFLAC_TRUE;
  2795. }
  2796.  
  2797.  
  2798.  
  2799. static drflac_bool32 drflac__seek_to_byte(drflac_bs* bs, drflac_uint64 offsetFromStart)
  2800. {
  2801.     DRFLAC_ASSERT(bs != NULL);
  2802.     DRFLAC_ASSERT(offsetFromStart > 0);
  2803.  
  2804.     /*
  2805.     Seeking from the start is not quite as trivial as it sounds because the onSeek callback takes a signed 32-bit integer (which
  2806.     is intentional because it simplifies the implementation of the onSeek callbacks), however offsetFromStart is unsigned 64-bit.
  2807.     To resolve we just need to do an initial seek from the start, and then a series of offset seeks to make up the remainder.
  2808.     */
  2809.     if (offsetFromStart > 0x7FFFFFFF) {
  2810.         drflac_uint64 bytesRemaining = offsetFromStart;
  2811.         if (!bs->onSeek(bs->pUserData, 0x7FFFFFFF, drflac_seek_origin_start)) {
  2812.             return DRFLAC_FALSE;
  2813.         }
  2814.         bytesRemaining -= 0x7FFFFFFF;
  2815.  
  2816.         while (bytesRemaining > 0x7FFFFFFF) {
  2817.             if (!bs->onSeek(bs->pUserData, 0x7FFFFFFF, drflac_seek_origin_current)) {
  2818.                 return DRFLAC_FALSE;
  2819.             }
  2820.             bytesRemaining -= 0x7FFFFFFF;
  2821.         }
  2822.  
  2823.         if (bytesRemaining > 0) {
  2824.             if (!bs->onSeek(bs->pUserData, (int)bytesRemaining, drflac_seek_origin_current)) {
  2825.                 return DRFLAC_FALSE;
  2826.             }
  2827.         }
  2828.     } else {
  2829.         if (!bs->onSeek(bs->pUserData, (int)offsetFromStart, drflac_seek_origin_start)) {
  2830.             return DRFLAC_FALSE;
  2831.         }
  2832.     }
  2833.  
  2834.     /* The cache should be reset to force a reload of fresh data from the client. */
  2835.     drflac__reset_cache(bs);
  2836.     return DRFLAC_TRUE;
  2837. }
  2838.  
  2839.  
  2840. static drflac_result drflac__read_utf8_coded_number(drflac_bs* bs, drflac_uint64* pNumberOut, drflac_uint8* pCRCOut)
  2841. {
  2842.     drflac_uint8 crc;
  2843.     drflac_uint64 result;
  2844.     drflac_uint8 utf8[7] = {0};
  2845.     int byteCount;
  2846.     int i;
  2847.  
  2848.     DRFLAC_ASSERT(bs != NULL);
  2849.     DRFLAC_ASSERT(pNumberOut != NULL);
  2850.     DRFLAC_ASSERT(pCRCOut != NULL);
  2851.  
  2852.     crc = *pCRCOut;
  2853.  
  2854.     if (!drflac__read_uint8(bs, 8, utf8)) {
  2855.         *pNumberOut = 0;
  2856.         return DRFLAC_AT_END;
  2857.     }
  2858.     crc = drflac_crc8(crc, utf8[0], 8);
  2859.  
  2860.     if ((utf8[0] & 0x80) == 0) {
  2861.         *pNumberOut = utf8[0];
  2862.         *pCRCOut = crc;
  2863.         return DRFLAC_SUCCESS;
  2864.     }
  2865.  
  2866.     /*byteCount = 1;*/
  2867.     if ((utf8[0] & 0xE0) == 0xC0) {
  2868.         byteCount = 2;
  2869.     } else if ((utf8[0] & 0xF0) == 0xE0) {
  2870.         byteCount = 3;
  2871.     } else if ((utf8[0] & 0xF8) == 0xF0) {
  2872.         byteCount = 4;
  2873.     } else if ((utf8[0] & 0xFC) == 0xF8) {
  2874.         byteCount = 5;
  2875.     } else if ((utf8[0] & 0xFE) == 0xFC) {
  2876.         byteCount = 6;
  2877.     } else if ((utf8[0] & 0xFF) == 0xFE) {
  2878.         byteCount = 7;
  2879.     } else {
  2880.         *pNumberOut = 0;
  2881.         return DRFLAC_CRC_MISMATCH;     /* Bad UTF-8 encoding. */
  2882.     }
  2883.  
  2884.     /* Read extra bytes. */
  2885.     DRFLAC_ASSERT(byteCount > 1);
  2886.  
  2887.     result = (drflac_uint64)(utf8[0] & (0xFF >> (byteCount + 1)));
  2888.     for (i = 1; i < byteCount; ++i) {
  2889.         if (!drflac__read_uint8(bs, 8, utf8 + i)) {
  2890.             *pNumberOut = 0;
  2891.             return DRFLAC_AT_END;
  2892.         }
  2893.         crc = drflac_crc8(crc, utf8[i], 8);
  2894.  
  2895.         result = (result << 6) | (utf8[i] & 0x3F);
  2896.     }
  2897.  
  2898.     *pNumberOut = result;
  2899.     *pCRCOut = crc;
  2900.     return DRFLAC_SUCCESS;
  2901. }
  2902.  
  2903.  
  2904.  
  2905. /*
  2906. The next two functions are responsible for calculating the prediction.
  2907.  
  2908. When the bits per sample is >16 we need to use 64-bit integer arithmetic because otherwise we'll run out of precision. It's
  2909. safe to assume this will be slower on 32-bit platforms so we use a more optimal solution when the bits per sample is <=16.
  2910. */
  2911. static DRFLAC_INLINE drflac_int32 drflac__calculate_prediction_32(drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pDecodedSamples)
  2912. {
  2913.     drflac_int32 prediction = 0;
  2914.  
  2915.     DRFLAC_ASSERT(order <= 32);
  2916.  
  2917.     /* 32-bit version. */
  2918.  
  2919.     /* VC++ optimizes this to a single jmp. I've not yet verified this for other compilers. */
  2920.     switch (order)
  2921.     {
  2922.     case 32: prediction += coefficients[31] * pDecodedSamples[-32];
  2923.     case 31: prediction += coefficients[30] * pDecodedSamples[-31];
  2924.     case 30: prediction += coefficients[29] * pDecodedSamples[-30];
  2925.     case 29: prediction += coefficients[28] * pDecodedSamples[-29];
  2926.     case 28: prediction += coefficients[27] * pDecodedSamples[-28];
  2927.     case 27: prediction += coefficients[26] * pDecodedSamples[-27];
  2928.     case 26: prediction += coefficients[25] * pDecodedSamples[-26];
  2929.     case 25: prediction += coefficients[24] * pDecodedSamples[-25];
  2930.     case 24: prediction += coefficients[23] * pDecodedSamples[-24];
  2931.     case 23: prediction += coefficients[22] * pDecodedSamples[-23];
  2932.     case 22: prediction += coefficients[21] * pDecodedSamples[-22];
  2933.     case 21: prediction += coefficients[20] * pDecodedSamples[-21];
  2934.     case 20: prediction += coefficients[19] * pDecodedSamples[-20];
  2935.     case 19: prediction += coefficients[18] * pDecodedSamples[-19];
  2936.     case 18: prediction += coefficients[17] * pDecodedSamples[-18];
  2937.     case 17: prediction += coefficients[16] * pDecodedSamples[-17];
  2938.     case 16: prediction += coefficients[15] * pDecodedSamples[-16];
  2939.     case 15: prediction += coefficients[14] * pDecodedSamples[-15];
  2940.     case 14: prediction += coefficients[13] * pDecodedSamples[-14];
  2941.     case 13: prediction += coefficients[12] * pDecodedSamples[-13];
  2942.     case 12: prediction += coefficients[11] * pDecodedSamples[-12];
  2943.     case 11: prediction += coefficients[10] * pDecodedSamples[-11];
  2944.     case 10: prediction += coefficients[ 9] * pDecodedSamples[-10];
  2945.     case  9: prediction += coefficients[ 8] * pDecodedSamples[- 9];
  2946.     case  8: prediction += coefficients[ 7] * pDecodedSamples[- 8];
  2947.     case  7: prediction += coefficients[ 6] * pDecodedSamples[- 7];
  2948.     case  6: prediction += coefficients[ 5] * pDecodedSamples[- 6];
  2949.     case  5: prediction += coefficients[ 4] * pDecodedSamples[- 5];
  2950.     case  4: prediction += coefficients[ 3] * pDecodedSamples[- 4];
  2951.     case  3: prediction += coefficients[ 2] * pDecodedSamples[- 3];
  2952.     case  2: prediction += coefficients[ 1] * pDecodedSamples[- 2];
  2953.     case  1: prediction += coefficients[ 0] * pDecodedSamples[- 1];
  2954.     }
  2955.  
  2956.     return (drflac_int32)(prediction >> shift);
  2957. }
  2958.  
  2959. static DRFLAC_INLINE drflac_int32 drflac__calculate_prediction_64(drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pDecodedSamples)
  2960. {
  2961.     drflac_int64 prediction;
  2962.  
  2963.     DRFLAC_ASSERT(order <= 32);
  2964.  
  2965.     /* 64-bit version. */
  2966.  
  2967.     /* This method is faster on the 32-bit build when compiling with VC++. See note below. */
  2968. #ifndef DRFLAC_64BIT
  2969.     if (order == 8)
  2970.     {
  2971.         prediction  = coefficients[0] * (drflac_int64)pDecodedSamples[-1];
  2972.         prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2];
  2973.         prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3];
  2974.         prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4];
  2975.         prediction += coefficients[4] * (drflac_int64)pDecodedSamples[-5];
  2976.         prediction += coefficients[5] * (drflac_int64)pDecodedSamples[-6];
  2977.         prediction += coefficients[6] * (drflac_int64)pDecodedSamples[-7];
  2978.         prediction += coefficients[7] * (drflac_int64)pDecodedSamples[-8];
  2979.     }
  2980.     else if (order == 7)
  2981.     {
  2982.         prediction  = coefficients[0] * (drflac_int64)pDecodedSamples[-1];
  2983.         prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2];
  2984.         prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3];
  2985.         prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4];
  2986.         prediction += coefficients[4] * (drflac_int64)pDecodedSamples[-5];
  2987.         prediction += coefficients[5] * (drflac_int64)pDecodedSamples[-6];
  2988.         prediction += coefficients[6] * (drflac_int64)pDecodedSamples[-7];
  2989.     }
  2990.     else if (order == 3)
  2991.     {
  2992.         prediction  = coefficients[0] * (drflac_int64)pDecodedSamples[-1];
  2993.         prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2];
  2994.         prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3];
  2995.     }
  2996.     else if (order == 6)
  2997.     {
  2998.         prediction  = coefficients[0] * (drflac_int64)pDecodedSamples[-1];
  2999.         prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2];
  3000.         prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3];
  3001.         prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4];
  3002.         prediction += coefficients[4] * (drflac_int64)pDecodedSamples[-5];
  3003.         prediction += coefficients[5] * (drflac_int64)pDecodedSamples[-6];
  3004.     }
  3005.     else if (order == 5)
  3006.     {
  3007.         prediction  = coefficients[0] * (drflac_int64)pDecodedSamples[-1];
  3008.         prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2];
  3009.         prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3];
  3010.         prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4];
  3011.         prediction += coefficients[4] * (drflac_int64)pDecodedSamples[-5];
  3012.     }
  3013.     else if (order == 4)
  3014.     {
  3015.         prediction  = coefficients[0] * (drflac_int64)pDecodedSamples[-1];
  3016.         prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2];
  3017.         prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3];
  3018.         prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4];
  3019.     }
  3020.     else if (order == 12)
  3021.     {
  3022.         prediction  = coefficients[0]  * (drflac_int64)pDecodedSamples[-1];
  3023.         prediction += coefficients[1]  * (drflac_int64)pDecodedSamples[-2];
  3024.         prediction += coefficients[2]  * (drflac_int64)pDecodedSamples[-3];
  3025.         prediction += coefficients[3]  * (drflac_int64)pDecodedSamples[-4];
  3026.         prediction += coefficients[4]  * (drflac_int64)pDecodedSamples[-5];
  3027.         prediction += coefficients[5]  * (drflac_int64)pDecodedSamples[-6];
  3028.         prediction += coefficients[6]  * (drflac_int64)pDecodedSamples[-7];
  3029.         prediction += coefficients[7]  * (drflac_int64)pDecodedSamples[-8];
  3030.         prediction += coefficients[8]  * (drflac_int64)pDecodedSamples[-9];
  3031.         prediction += coefficients[9]  * (drflac_int64)pDecodedSamples[-10];
  3032.         prediction += coefficients[10] * (drflac_int64)pDecodedSamples[-11];
  3033.         prediction += coefficients[11] * (drflac_int64)pDecodedSamples[-12];
  3034.     }
  3035.     else if (order == 2)
  3036.     {
  3037.         prediction  = coefficients[0] * (drflac_int64)pDecodedSamples[-1];
  3038.         prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2];
  3039.     }
  3040.     else if (order == 1)
  3041.     {
  3042.         prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1];
  3043.     }
  3044.     else if (order ==