vkQuake2 doxygen  1.0 dev
dr_flac.h
Go to the documentation of this file.
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.11.8 - 2019-05-21
4 
5 David Reid - mackron@gmail.com
6 */
7 
8 /*
9 USAGE
10 =====
11 dr_flac is a single-file library. To use it, do something like the following in one .c file.
12  #define DR_FLAC_IMPLEMENTATION
13  #include "dr_flac.h"
14 
15 You can then #include this file in other parts of the program as you would with any other header file. To decode audio data,
16 do something like the following:
17 
18  drflac* pFlac = drflac_open_file("MySong.flac");
19  if (pFlac == NULL) {
20  // Failed to open FLAC file
21  }
22 
23  drflac_int32* pSamples = malloc(pFlac->totalPCMFrameCount * pFlac->channels * sizeof(drflac_int32));
24  drflac_uint64 numberOfInterleavedSamplesActuallyRead = drflac_read_pcm_frames_s32(pFlac, pFlac->totalPCMFrameCount, pSamples);
25 
26 The drflac object represents the decoder. It is a transparent type so all the information you need, such as the number of
27 channels and the bits per sample, should be directly accessible - just make sure you don't change their values. Samples are
28 always output as interleaved signed 32-bit PCM. In the example above a native FLAC stream was opened, however dr_flac has
29 seamless support for Ogg encapsulated FLAC streams as well.
30 
31 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
32 the decoder will give you as many samples as it can, up to the amount requested. Later on when you need the next batch of
33 samples, just call it again. Example:
34 
35  while (drflac_read_pcm_frames_s32(pFlac, chunkSizeInPCMFrames, pChunkSamples) > 0) {
36  do_something();
37  }
38 
39 You can seek to a specific sample with drflac_seek_to_sample(). The given sample is based on interleaving. So for example,
40 if you were to seek to the sample at index 0 in a stereo stream, you'll be seeking to the first sample of the left channel.
41 The sample at index 1 will be the first sample of the right channel. The sample at index 2 will be the second sample of the
42 left channel, etc.
43 
44 
45 If you just want to quickly decode an entire FLAC file in one go you can do something like this:
46 
47  unsigned int channels;
48  unsigned int sampleRate;
49  drflac_uint64 totalPCMFrameCount;
50  drflac_int32* pSampleData = drflac_open_file_and_read_pcm_frames_s32("MySong.flac", &channels, &sampleRate, &totalPCMFrameCount);
51  if (pSampleData == NULL) {
52  // Failed to open and decode FLAC file.
53  }
54 
55  ...
56 
57  drflac_free(pSampleData);
58 
59 
60 You can read samples as signed 16-bit integer and 32-bit floating-point PCM with the *_s16() and *_f32() family of APIs
61 respectively, but note that these should be considered lossy.
62 
63 
64 If you need access to metadata (album art, etc.), use drflac_open_with_metadata(), drflac_open_file_with_metdata() or
65 drflac_open_memory_with_metadata(). The rationale for keeping these APIs separate is that they're slightly slower than the
66 normal versions and also just a little bit harder to use.
67 
68 dr_flac reports metadata to the application through the use of a callback, and every metadata block is reported before
69 drflac_open_with_metdata() returns.
70 
71 
72 The main opening APIs (drflac_open(), etc.) will fail if the header is not present. The presents a problem in certain
73 scenarios such as broadcast style streams like internet radio where the header may not be present because the user has
74 started playback mid-stream. To handle this, use the relaxed APIs: drflac_open_relaxed() and drflac_open_with_metadata_relaxed().
75 
76 It is not recommended to use these APIs for file based streams because a missing header would usually indicate a
77 corrupted or perverse file. In addition, these APIs can take a long time to initialize because they may need to spend
78 a lot of time finding the first frame.
79 
80 
81 
82 OPTIONS
83 =======
84 #define these options before including this file.
85 
86 #define DR_FLAC_NO_STDIO
87  Disable drflac_open_file() and family.
88 
89 #define DR_FLAC_NO_OGG
90  Disables support for Ogg/FLAC streams.
91 
92 #define DR_FLAC_BUFFER_SIZE <number>
93  Defines the size of the internal buffer to store data from onRead(). This buffer is used to reduce the number of calls
94  back to the client for more data. Larger values means more memory, but better performance. My tests show diminishing
95  returns after about 4KB (which is the default). Consider reducing this if you have a very efficient implementation of
96  onRead(), or increase it if it's very inefficient. Must be a multiple of 8.
97 
98 #define DR_FLAC_NO_CRC
99  Disables CRC checks. This will offer a performance boost when CRC is unnecessary.
100 
101 #define DR_FLAC_NO_SIMD
102  Disables SIMD optimizations (SSE on x86/x64 architectures). Use this if you are having compatibility issues with your
103  compiler.
104 
105 
106 
107 QUICK NOTES
108 ===========
109 - dr_flac does not currently support changing the sample rate nor channel count mid stream.
110 - Audio data is output as signed 32-bit PCM, regardless of the bits per sample the FLAC stream is encoded as.
111 - This has not been tested on big-endian architectures.
112 - dr_flac is not thread-safe, but its APIs can be called from any thread so long as you do your own synchronization.
113 - When using Ogg encapsulation, a corrupted metadata block will result in drflac_open_with_metadata() and drflac_open()
114  returning inconsistent samples.
115 */
116 
117 #ifndef dr_flac_h
118 #define dr_flac_h
119 
120 #include <stddef.h>
121 
122 #if defined(_MSC_VER) && _MSC_VER < 1600
123 typedef signed char drflac_int8;
124 typedef unsigned char drflac_uint8;
125 typedef signed short drflac_int16;
126 typedef unsigned short drflac_uint16;
127 typedef signed int drflac_int32;
128 typedef unsigned int drflac_uint32;
129 typedef signed __int64 drflac_int64;
130 typedef unsigned __int64 drflac_uint64;
131 #else
132 #include <stdint.h>
133 typedef int8_t drflac_int8;
134 typedef uint8_t drflac_uint8;
135 typedef int16_t drflac_int16;
136 typedef uint16_t drflac_uint16;
137 typedef int32_t drflac_int32;
138 typedef uint32_t drflac_uint32;
139 typedef int64_t drflac_int64;
140 typedef uint64_t drflac_uint64;
141 #endif
144 #define DRFLAC_TRUE 1
145 #define DRFLAC_FALSE 0
146 
147 #if defined(_MSC_VER) && _MSC_VER >= 1700 /* Visual Studio 2012 */
148  #define DRFLAC_DEPRECATED __declspec(deprecated)
149 #elif (defined(__GNUC__) && __GNUC__ >= 4) /* GCC 4 */
150  #define DRFLAC_DEPRECATED __attribute__((deprecated))
151 #elif defined(__has_feature) /* Clang */
152  #if defined(__has_feature(attribute_deprecated))
153  #define DRFLAC_DEPRECATED __attribute__((deprecated))
154  #else
155  #define DRFLAC_DEPRECATED
156  #endif
157 #else
158  #define DRFLAC_DEPRECATED
159 #endif
160 
161 /*
162 As data is read from the client it is placed into an internal buffer for fast access. This controls the
163 size of that buffer. Larger values means more speed, but also more memory. In my testing there is diminishing
164 returns after about 4KB, but you can fiddle with this to suit your own needs. Must be a multiple of 8.
165 */
166 #ifndef DR_FLAC_BUFFER_SIZE
167 #define DR_FLAC_BUFFER_SIZE 4096
168 #endif
169 
170 #ifdef __cplusplus
171 extern "C" {
172 #endif
173 
174 /* Check if we can enable 64-bit optimizations. */
175 #if defined(_WIN64) || defined(_LP64) || defined(__LP64__)
176 #define DRFLAC_64BIT
177 #endif
178 
179 #ifdef DRFLAC_64BIT
181 #else
183 #endif
184 
185 /* The various metadata block types. */
186 #define DRFLAC_METADATA_BLOCK_TYPE_STREAMINFO 0
187 #define DRFLAC_METADATA_BLOCK_TYPE_PADDING 1
188 #define DRFLAC_METADATA_BLOCK_TYPE_APPLICATION 2
189 #define DRFLAC_METADATA_BLOCK_TYPE_SEEKTABLE 3
190 #define DRFLAC_METADATA_BLOCK_TYPE_VORBIS_COMMENT 4
191 #define DRFLAC_METADATA_BLOCK_TYPE_CUESHEET 5
192 #define DRFLAC_METADATA_BLOCK_TYPE_PICTURE 6
193 #define DRFLAC_METADATA_BLOCK_TYPE_INVALID 127
194 
195 /* The various picture types specified in the PICTURE block. */
196 #define DRFLAC_PICTURE_TYPE_OTHER 0
197 #define DRFLAC_PICTURE_TYPE_FILE_ICON 1
198 #define DRFLAC_PICTURE_TYPE_OTHER_FILE_ICON 2
199 #define DRFLAC_PICTURE_TYPE_COVER_FRONT 3
200 #define DRFLAC_PICTURE_TYPE_COVER_BACK 4
201 #define DRFLAC_PICTURE_TYPE_LEAFLET_PAGE 5
202 #define DRFLAC_PICTURE_TYPE_MEDIA 6
203 #define DRFLAC_PICTURE_TYPE_LEAD_ARTIST 7
204 #define DRFLAC_PICTURE_TYPE_ARTIST 8
205 #define DRFLAC_PICTURE_TYPE_CONDUCTOR 9
206 #define DRFLAC_PICTURE_TYPE_BAND 10
207 #define DRFLAC_PICTURE_TYPE_COMPOSER 11
208 #define DRFLAC_PICTURE_TYPE_LYRICIST 12
209 #define DRFLAC_PICTURE_TYPE_RECORDING_LOCATION 13
210 #define DRFLAC_PICTURE_TYPE_DURING_RECORDING 14
211 #define DRFLAC_PICTURE_TYPE_DURING_PERFORMANCE 15
212 #define DRFLAC_PICTURE_TYPE_SCREEN_CAPTURE 16
213 #define DRFLAC_PICTURE_TYPE_BRIGHT_COLORED_FISH 17
214 #define DRFLAC_PICTURE_TYPE_ILLUSTRATION 18
215 #define DRFLAC_PICTURE_TYPE_BAND_LOGOTYPE 19
216 #define DRFLAC_PICTURE_TYPE_PUBLISHER_LOGOTYPE 20
217 
218 typedef enum
219 {
224 
225 typedef enum
226 {
230 
231 /* Packing is important on this structure because we map this directly to the raw data within the SEEKTABLE metadata block. */
232 #pragma pack(2)
233 typedef struct
234 {
236  drflac_uint64 frameOffset; /* The offset from the first byte of the header of the first frame. */
239 #pragma pack()
240 
241 typedef struct
242 {
251  drflac_uint8 md5[16];
253 
254 typedef struct
255 {
256  /* The metadata type. Use this to know how to interpret the data below. */
258 
259  /*
260  A pointer to the raw data. This points to a temporary buffer so don't hold on to it. It's best to
261  not modify the contents of this buffer. Use the structures below for more meaningful and structured
262  information about the metadata. It's possible for this to be null.
263  */
264  const void* pRawData;
265 
266  /* The size in bytes of the block and the buffer pointed to by pRawData if it's non-NULL. */
268 
269  union
270  {
272 
273  struct
274  {
275  int unused;
276  } padding;
277 
278  struct
279  {
281  const void* pData;
283  } application;
284 
285  struct
286  {
289  } seektable;
290 
291  struct
292  {
294  const char* vendor;
296  const void* pComments;
297  } vorbis_comment;
298 
299  struct
300  {
301  char catalog[128];
305  const void* pTrackData;
306  } cuesheet;
307 
308  struct
309  {
312  const char* mime;
314  const char* description;
321  } picture;
322  } data;
324 
325 
326 /*
327 Callback for when data needs to be read from the client.
328 
329 pUserData [in] The user data that was passed to drflac_open() and family.
330 pBufferOut [out] The output buffer.
331 bytesToRead [in] The number of bytes to read.
332 
333 Returns the number of bytes actually read.
334 
335 A return value of less than bytesToRead indicates the end of the stream. Do _not_ return from this callback until
336 either the entire bytesToRead is filled or you have reached the end of the stream.
337 */
338 typedef size_t (* drflac_read_proc)(void* pUserData, void* pBufferOut, size_t bytesToRead);
339 
340 /*
341 Callback for when data needs to be seeked.
342 
343 pUserData [in] The user data that was passed to drflac_open() and family.
344 offset [in] The number of bytes to move, relative to the origin. Will never be negative.
345 origin [in] The origin of the seek - the current position or the start of the stream.
346 
347 Returns whether or not the seek was successful.
348 
349 The offset will never be negative. Whether or not it is relative to the beginning or current position is determined
350 by the "origin" parameter which will be either drflac_seek_origin_start or drflac_seek_origin_current.
351 */
352 typedef drflac_bool32 (* drflac_seek_proc)(void* pUserData, int offset, drflac_seek_origin origin);
353 
354 /*
355 Callback for when a metadata block is read.
356 
357 pUserData [in] The user data that was passed to drflac_open() and family.
358 pMetadata [in] A pointer to a structure containing the data of the metadata block.
359 
360 Use pMetadata->type to determine which metadata block is being handled and how to read the data.
361 */
362 typedef void (* drflac_meta_proc)(void* pUserData, drflac_metadata* pMetadata);
363 
364 
365 /* Structure for internal use. Only used for decoders opened with drflac_open_memory. */
366 typedef struct
367 {
369  size_t dataSize;
372 
373 /* Structure for internal use. Used for bit streaming. */
374 typedef struct
375 {
376  /* The function to call when more data needs to be read. */
378 
379  /* The function to call when the current read position needs to be moved. */
381 
382  /* The user data to pass around to onRead and onSeek. */
383  void* pUserData;
384 
385 
386  /*
387  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
388  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
389  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).
390  */
392 
393  /* The content of the unaligned bytes. */
395 
396  /* The index of the next valid cache line in the "L2" cache. */
398 
399  /* The number of bits that have been consumed by the cache. This is used to determine how many valid bits are remaining. */
401 
402  /*
403  The cached data which was most recently read from the client. There are two levels of cache. Data flows as such:
404  Client -> L2 -> L1. The L2 -> L1 movement is aligned and runs on a fast path in just a few instructions.
405  */
408 
409  /*
410  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
411  is reset to 0 at the beginning of each frame.
412  */
414  drflac_cache_t crc16Cache; /* A cache for optimizing CRC calculations. This is filled when when the L1 cache is reloaded. */
415  drflac_uint32 crc16CacheIgnoredBytes; /* The number of bytes to ignore when updating the CRC-16 from the CRC-16 cache. */
416 } drflac_bs;
417 
418 typedef struct
419 {
420  /* The type of the subframe: SUBFRAME_CONSTANT, SUBFRAME_VERBATIM, SUBFRAME_FIXED or SUBFRAME_LPC. */
422 
423  /* The number of wasted bits per sample as specified by the sub-frame header. */
425 
426  /* The order to use for the prediction stage for SUBFRAME_FIXED and SUBFRAME_LPC. */
428 
429  /*
430  The number of bits per sample for this subframe. This is not always equal to the current frame's bit per sample because
431  an extra bit is required for side channels when interchannel decorrelation is being used.
432  */
434 
435  /*
436  A pointer to the buffer containing the decoded samples in the subframe. This pointer is an offset from drflac::pExtraData. Note that
437  it's a signed 32-bit integer for each value.
438  */
441 
442 typedef struct
443 {
444  /*
445  If the stream uses variable block sizes, this will be set to the index of the first sample. If fixed block sizes are used, this will
446  always be set to 0.
447  */
449 
450  /* 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. */
452 
453  /* The sample rate of this frame. */
455 
456  /* The number of samples in each sub-frame within this frame. */
458 
459  /*
460  The channel assignment of this frame. This is not always set to the channel count. If interchannel decorrelation is being used this
461  will be set to DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE, DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE or DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE.
462  */
464 
465  /* The number of bits per sample within this frame. */
467 
468  /* The frame's CRC. */
471 
472 typedef struct
473 {
474  /* The header. */
476 
477  /*
478  The number of samples left to be read in this frame. This is initially set to the block size multiplied by the channel count. As samples
479  are read, this will be decremented. When it reaches 0, the decoder will see this frame as fully consumed and load the next frame.
480  */
482 
483  /* The list of sub-frames within the frame. There is one sub-frame for each channel, and there's a maximum of 8 channels. */
484  drflac_subframe subframes[8];
485 } drflac_frame;
486 
487 typedef struct
488 {
489  /* The function to call when a metadata block is read. */
491 
492  /* The user data posted to the metadata callback function. */
493  void* pUserDataMD;
494 
495 
496  /* The sample rate. Will be set to something like 44100. */
498 
499  /*
500  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
501  value specified in the STREAMINFO block.
502  */
504 
505  /* The bits per sample. Will be set to something like 16, 24, etc. */
507 
508  /* The maximum block size, in samples. This number represents the number of samples in each channel (not combined). */
510 
511  /*
512  The total number of samples making up the stream. This includes every channel. For example, if the stream has 2 channels,
513  with each channel having a total of 4096, this value will be set to 2*4096 = 8192. Can be 0 in which case it's still a
514  valid stream, but just means the total sample count is unknown. Likely the case with streams like internet radio.
515  */
517  drflac_uint64 totalPCMFrameCount; /* <-- Equal to totalSampleCount / channels. */
518 
519 
520  /* The container type. This is set based on whether or not the decoder was opened from a native or Ogg stream. */
522 
523  /* The number of seekpoints in the seektable. */
525 
526 
527  /* Information about the frame the decoder is currently sitting on. */
529 
530  /* The index of the sample the decoder is currently sitting on. This is only used for seeking. */
532 
533  /* The position of the first frame in the stream. This is only ever used for seeking. */
535 
536 
537  /* A hack to avoid a malloc() when opening a decoder with drflac_open_memory(). */
539 
540 
541  /* A pointer to the decoded sample data. This is an offset of pExtraData. */
543 
544  /* A pointer to the seek table. This is an offset of pExtraData, or NULL if there is no seek table. */
546 
547  /* Internal use only. Only used with Ogg containers. Points to a drflac_oggbs object. This is an offset of pExtraData. */
548  void* _oggbs;
549 
550  /* The bit streamer. The raw FLAC data is fed through this object. */
552 
553  /* Variable length extra data. We attach this to the end of the object so we can avoid unnecessary mallocs. */
554  drflac_uint8 pExtraData[1];
555 } drflac;
556 
557 
558 /*
559 Opens a FLAC decoder.
560 
561 onRead [in] The function to call when data needs to be read from the client.
562 onSeek [in] The function to call when the read position of the client data needs to move.
563 pUserData [in, optional] A pointer to application defined data that will be passed to onRead and onSeek.
564 
565 Returns a pointer to an object representing the decoder.
566 
567 Close the decoder with drflac_close().
568 
569 This function will automatically detect whether or not you are attempting to open a native or Ogg encapsulated
570 FLAC, both of which should work seamlessly without any manual intervention. Ogg encapsulation also works with
571 multiplexed streams which basically means it can play FLAC encoded audio tracks in videos.
572 
573 This is the lowest level function for opening a FLAC stream. You can also use drflac_open_file() and drflac_open_memory()
574 to open the stream from a file or from a block of memory respectively.
575 
576 The STREAMINFO block must be present for this to succeed. Use drflac_open_relaxed() to open a FLAC stream where
577 the header may not be present.
578 
579 See also: drflac_open_file(), drflac_open_memory(), drflac_open_with_metadata(), drflac_close()
580 */
581 drflac* drflac_open(drflac_read_proc onRead, drflac_seek_proc onSeek, void* pUserData);
582 
583 /*
584 The same as drflac_open(), except attempts to open the stream even when a header block is not present.
585 
586 Because the header is not necessarily available, the caller must explicitly define the container (Native or Ogg). Do
587 not set this to drflac_container_unknown - that is for internal use only.
588 
589 Opening in relaxed mode will continue reading data from onRead until it finds a valid frame. If a frame is never
590 found it will continue forever. To abort, force your onRead callback to return 0, which dr_flac will use as an
591 indicator that the end of the stream was found.
592 */
593 drflac* drflac_open_relaxed(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_container container, void* pUserData);
594 
595 /*
596 Opens a FLAC decoder and notifies the caller of the metadata chunks (album art, etc.).
597 
598 onRead [in] The function to call when data needs to be read from the client.
599 onSeek [in] The function to call when the read position of the client data needs to move.
600 onMeta [in] The function to call for every metadata block.
601 pUserData [in, optional] A pointer to application defined data that will be passed to onRead, onSeek and onMeta.
602 
603 Returns a pointer to an object representing the decoder.
604 
605 Close the decoder with drflac_close().
606 
607 This is slower than drflac_open(), so avoid this one if you don't need metadata. Internally, this will do a DRFLAC_MALLOC()
608 and DRFLAC_FREE() for every metadata block except for STREAMINFO and PADDING blocks.
609 
610 The caller is notified of the metadata via the onMeta callback. All metadata blocks will be handled before the function
611 returns.
612 
613 The STREAMINFO block must be present for this to succeed. Use drflac_open_with_metadata_relaxed() to open a FLAC
614 stream where the header may not be present.
615 
616 Note that this will behave inconsistently with drflac_open() if the stream is an Ogg encapsulated stream and a metadata
617 block is corrupted. This is due to the way the Ogg stream recovers from corrupted pages. When drflac_open_with_metadata()
618 is being used, the open routine will try to read the contents of the metadata block, whereas drflac_open() will simply
619 seek past it (for the sake of efficiency). This inconsistency can result in different samples being returned depending on
620 whether or not the stream is being opened with metadata.
621 
622 See also: drflac_open_file_with_metadata(), drflac_open_memory_with_metadata(), drflac_open(), drflac_close()
623 */
624 drflac* drflac_open_with_metadata(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_meta_proc onMeta, void* pUserData);
625 
626 /*
627 The same as drflac_open_with_metadata(), except attempts to open the stream even when a header block is not present.
628 
629 See also: drflac_open_with_metadata(), drflac_open_relaxed()
630 */
632 
633 /*
634 Closes the given FLAC decoder.
635 
636 pFlac [in] The decoder to close.
637 
638 This will destroy the decoder object.
639 */
640 void drflac_close(drflac* pFlac);
641 
642 
643 /*
644 Reads sample data from the given FLAC decoder, output as interleaved signed 32-bit PCM.
645 
646 pFlac [in] The decoder.
647 framesToRead [in] The number of PCM frames to read.
648 pBufferOut [out, optional] A pointer to the buffer that will receive the decoded samples.
649 
650 Returns the number of PCM frames actually read.
651 
652 pBufferOut can be null, in which case the call will act as a seek, and the return value will be the number of frames
653 seeked.
654 */
656 
657 /*
658 Same as drflac_read_pcm_frames_s32(), except outputs samples as 16-bit integer PCM rather than 32-bit.
659 
660 Note that this is lossy for streams where the bits per sample is larger than 16.
661 */
663 
664 /*
665 Same as drflac_read_pcm_frames_s32(), except outputs samples as 32-bit floating-point PCM.
666 
667 Note that this should be considered lossy due to the nature of floating point numbers not being able to exactly
668 represent every possible number.
669 */
670 drflac_uint64 drflac_read_pcm_frames_f32(drflac* pFlac, drflac_uint64 framesToRead, float* pBufferOut);
671 
672 /*
673 Seeks to the PCM frame at the given index.
674 
675 pFlac [in] The decoder.
676 pcmFrameIndex [in] The index of the PCM frame to seek to. See notes below.
677 
678 Returns DRFLAC_TRUE if successful; DRFLAC_FALSE otherwise.
679 */
681 
682 
683 
684 #ifndef DR_FLAC_NO_STDIO
685 /*
686 Opens a FLAC decoder from the file at the given path.
687 
688 filename [in] The path of the file to open, either absolute or relative to the current directory.
689 
690 Returns a pointer to an object representing the decoder.
691 
692 Close the decoder with drflac_close().
693 
694 This will hold a handle to the file until the decoder is closed with drflac_close(). Some platforms will restrict the
695 number of files a process can have open at any given time, so keep this mind if you have many decoders open at the
696 same time.
697 
698 See also: drflac_open(), drflac_open_file_with_metadata(), drflac_close()
699 */
700 drflac* drflac_open_file(const char* filename);
701 
702 /*
703 Opens a FLAC decoder from the file at the given path and notifies the caller of the metadata chunks (album art, etc.)
704 
705 Look at the documentation for drflac_open_with_metadata() for more information on how metadata is handled.
706 */
707 drflac* drflac_open_file_with_metadata(const char* filename, drflac_meta_proc onMeta, void* pUserData);
708 #endif
709 
710 /*
711 Opens a FLAC decoder from a pre-allocated block of memory
712 
713 This does not create a copy of the data. It is up to the application to ensure the buffer remains valid for
714 the lifetime of the decoder.
715 */
716 drflac* drflac_open_memory(const void* data, size_t dataSize);
717 
718 /*
719 Opens a FLAC decoder from a pre-allocated block of memory and notifies the caller of the metadata chunks (album art, etc.)
720 
721 Look at the documentation for drflac_open_with_metadata() for more information on how metadata is handled.
722 */
723 drflac* drflac_open_memory_with_metadata(const void* data, size_t dataSize, drflac_meta_proc onMeta, void* pUserData);
724 
725 
726 
727 /* High Level APIs */
728 
729 /*
730 Opens a FLAC stream from the given callbacks and fully decodes it in a single operation. The return value is a
731 pointer to the sample data as interleaved signed 32-bit PCM. The returned data must be freed with DRFLAC_FREE().
732 
733 Sometimes a FLAC file won't keep track of the total sample count. In this situation the function will continuously
734 read samples into a dynamically sized buffer on the heap until no samples are left.
735 
736 Do not call this function on a broadcast type of stream (like internet radio streams and whatnot).
737 */
738 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);
739 
740 /* Same as drflac_open_and_read_pcm_frames_s32(), except returns signed 16-bit integer samples. */
741 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);
742 
743 /* Same as drflac_open_and_read_pcm_frames_s32(), except returns 32-bit floating-point samples. */
744 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);
745 
746 #ifndef DR_FLAC_NO_STDIO
747 /* Same as drflac_open_and_read_pcm_frames_s32() except opens the decoder from a file. */
748 drflac_int32* drflac_open_file_and_read_pcm_frames_s32(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount);
749 
750 /* Same as drflac_open_file_and_read_pcm_frames_s32(), except returns signed 16-bit integer samples. */
751 drflac_int16* drflac_open_file_and_read_pcm_frames_s16(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount);
752 
753 /* Same as drflac_open_file_and_read_pcm_frames_s32(), except returns 32-bit floating-point samples. */
754 float* drflac_open_file_and_read_pcm_frames_f32(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount);
755 #endif
756 
757 /* Same as drflac_open_and_read_pcm_frames_s32() except opens the decoder from a block of memory. */
758 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);
759 
760 /* Same as drflac_open_memory_and_read_pcm_frames_s32(), except returns signed 16-bit integer samples. */
761 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);
762 
763 /* Same as drflac_open_memory_and_read_pcm_frames_s32(), except returns 32-bit floating-point samples. */
764 float* drflac_open_memory_and_read_pcm_frames_f32(const void* data, size_t dataSize, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount);
765 
766 /* Frees memory that was allocated internally by dr_flac. */
767 void drflac_free(void* p);
768 
769 
770 /* Structure representing an iterator for vorbis comments in a VORBIS_COMMENT metadata block. */
771 typedef struct
772 {
774  const char* pRunningData;
776 
777 /*
778 Initializes a vorbis comment iterator. This can be used for iterating over the vorbis comments in a VORBIS_COMMENT
779 metadata block.
780 */
781 void drflac_init_vorbis_comment_iterator(drflac_vorbis_comment_iterator* pIter, drflac_uint32 commentCount, const void* pComments);
782 
783 /*
784 Goes to the next vorbis comment in the given iterator. If null is returned it means there are no more comments. The
785 returned string is NOT null terminated.
786 */
787 const char* drflac_next_vorbis_comment(drflac_vorbis_comment_iterator* pIter, drflac_uint32* pCommentLengthOut);
788 
789 
790 /* Structure representing an iterator for cuesheet tracks in a CUESHEET metadata block. */
791 typedef struct
792 {
794  const char* pRunningData;
796 
797 /* Packing is important on this structure because we map this directly to the raw data within the CUESHEET metadata block. */
798 #pragma pack(4)
799 typedef struct
800 {
803  drflac_uint8 reserved[3];
805 #pragma pack()
806 
807 typedef struct
808 {
811  char ISRC[12];
817 
818 /*
819 Initializes a cuesheet track iterator. This can be used for iterating over the cuesheet tracks in a CUESHEET metadata
820 block.
821 */
822 void drflac_init_cuesheet_track_iterator(drflac_cuesheet_track_iterator* pIter, drflac_uint32 trackCount, const void* pTrackData);
823 
824 /* Goes to the next cuesheet track in the given iterator. If DRFLAC_FALSE is returned it means there are no more comments. */
826 
827 
828 /* Deprecated APIs */
829 DRFLAC_DEPRECATED drflac_uint64 drflac_read_s32(drflac* pFlac, drflac_uint64 samplesToRead, drflac_int32* pBufferOut); /* Use drflac_read_pcm_frames_s32() instead. */
830 DRFLAC_DEPRECATED drflac_uint64 drflac_read_s16(drflac* pFlac, drflac_uint64 samplesToRead, drflac_int16* pBufferOut); /* Use drflac_read_pcm_frames_s16() instead. */
831 DRFLAC_DEPRECATED drflac_uint64 drflac_read_f32(drflac* pFlac, drflac_uint64 samplesToRead, float* pBufferOut); /* Use drflac_read_pcm_frames_f32() instead. */
832 DRFLAC_DEPRECATED drflac_bool32 drflac_seek_to_sample(drflac* pFlac, drflac_uint64 sampleIndex); /* Use drflac_seek_to_pcm_frame() instead. */
833 DRFLAC_DEPRECATED drflac_int32* drflac_open_and_decode_s32(drflac_read_proc onRead, drflac_seek_proc onSeek, void* pUserData, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalSampleCount); /* Use drflac_open_and_read_pcm_frames_s32(). */
834 DRFLAC_DEPRECATED drflac_int16* drflac_open_and_decode_s16(drflac_read_proc onRead, drflac_seek_proc onSeek, void* pUserData, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalSampleCount); /* Use drflac_open_and_read_pcm_frames_s16(). */
835 DRFLAC_DEPRECATED float* drflac_open_and_decode_f32(drflac_read_proc onRead, drflac_seek_proc onSeek, void* pUserData, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalSampleCount); /* Use drflac_open_and_read_pcm_frames_f32(). */
836 DRFLAC_DEPRECATED drflac_int32* drflac_open_and_decode_file_s32(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalSampleCount); /* Use drflac_open_file_and_read_pcm_frames_s32(). */
837 DRFLAC_DEPRECATED drflac_int16* drflac_open_and_decode_file_s16(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalSampleCount); /* Use drflac_open_file_and_read_pcm_frames_s16(). */
838 DRFLAC_DEPRECATED float* drflac_open_and_decode_file_f32(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalSampleCount); /* Use drflac_open_file_and_read_pcm_frames_f32(). */
839 DRFLAC_DEPRECATED drflac_int32* drflac_open_and_decode_memory_s32(const void* data, size_t dataSize, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalSampleCount); /* Use drflac_open_memory_and_read_pcm_frames_s32(). */
840 DRFLAC_DEPRECATED drflac_int16* drflac_open_and_decode_memory_s16(const void* data, size_t dataSize, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalSampleCount); /* Use drflac_open_memory_and_read_pcm_frames_s16(). */
841 DRFLAC_DEPRECATED float* drflac_open_and_decode_memory_f32(const void* data, size_t dataSize, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalSampleCount); /* Use drflac_open_memory_and_read_pcm_frames_f32(). */
842 
843 #ifdef __cplusplus
844 }
845 #endif
846 #endif /* dr_flac_h */
847 
848 
849 /************************************************************************************************************************************************************
850  ************************************************************************************************************************************************************
851 
852  IMPLEMENTATION
853 
854  ************************************************************************************************************************************************************
855  ************************************************************************************************************************************************************/
856 #ifdef DR_FLAC_IMPLEMENTATION
857 
858 /* Disable some annoying warnings. */
859 #if defined(__GNUC__)
860  #pragma GCC diagnostic push
861  #if __GNUC__ >= 7
862  #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
863  #endif
864 #endif
865 
866 #ifdef __linux__
867  #ifndef _BSD_SOURCE
868  #define _BSD_SOURCE
869  #endif
870  #ifndef __USE_BSD
871  #define __USE_BSD
872  #endif
873  #include <endian.h>
874 #endif
875 
876 #include <stdlib.h>
877 #include <string.h>
878 
879 #ifdef _MSC_VER
880 #define DRFLAC_INLINE __forceinline
881 #else
882 #ifdef __GNUC__
883 #define DRFLAC_INLINE __inline__ __attribute__((always_inline))
884 #else
885 #define DRFLAC_INLINE
886 #endif
887 #endif
888 
889 /* CPU architecture. */
890 #if defined(__x86_64__) || defined(_M_X64)
891  #define DRFLAC_X64
892 #elif defined(__i386) || defined(_M_IX86)
893  #define DRFLAC_X86
894 #elif defined(__arm__) || defined(_M_ARM)
895  #define DRFLAC_ARM
896 #endif
897 
898 /* Intrinsics Support */
899 #if !defined(DR_FLAC_NO_SIMD)
900  #if defined(DRFLAC_X64) || defined(DRFLAC_X86)
901  #if defined(_MSC_VER) && !defined(__clang__)
902  /* MSVC. */
903  #if _MSC_VER >= 1400 && !defined(DRFLAC_NO_SSE2) /* 2005 */
904  #define DRFLAC_SUPPORT_SSE2
905  #endif
906  #if _MSC_VER >= 1600 && !defined(DRFLAC_NO_SSE41) /* 2010 */
907  #define DRFLAC_SUPPORT_SSE41
908  #endif
909  #else
910  /* Assume GNUC-style. */
911  #if defined(__SSE2__) && !defined(DRFLAC_NO_SSE2)
912  #define DRFLAC_SUPPORT_SSE2
913  #endif
914  #if defined(__SSE4_1__) && !defined(DRFLAC_NO_SSE41)
915  #define DRFLAC_SUPPORT_SSE41
916  #endif
917  #endif
918 
919  /* If at this point we still haven't determined compiler support for the intrinsics just fall back to __has_include. */
920  #if !defined(__GNUC__) && !defined(__clang__) && defined(__has_include)
921  #if !defined(DRFLAC_SUPPORT_SSE2) && !defined(DRFLAC_NO_SSE2) && __has_include(<emmintrin.h>)
922  #define DRFLAC_SUPPORT_SSE2
923  #endif
924  #if !defined(DRFLAC_SUPPORT_SSE41) && !defined(DRFLAC_NO_SSE41) && __has_include(<smmintrin.h>)
925  #define DRFLAC_SUPPORT_SSE41
926  #endif
927  #endif
928 
929  #if defined(DRFLAC_SUPPORT_SSE41)
930  #include <smmintrin.h>
931  #elif defined(DRFLAC_SUPPORT_SSE2)
932  #include <emmintrin.h>
933  #endif
934  #endif
935 
936  #if defined(DRFLAC_ARM)
937  #if !defined(DRFLAC_NO_NEON) && (defined(__ARM_NEON) || defined(__aarch64__) || defined(_M_ARM64))
938  #define DRFLAC_SUPPORT_NEON
939  #endif
940 
941  /* Fall back to looking for the #include file. */
942  #if !defined(__GNUC__) && !defined(__clang__) && defined(__has_include)
943  #if !defined(DRFLAC_SUPPORT_NEON) && !defined(DRFLAC_NO_NEON) && __has_include(<arm_neon.h>)
944  #define DRFLAC_SUPPORT_NEON
945  #endif
946  #endif
947 
948  #if defined(DRFLAC_SUPPORT_NEON)
949  #include <arm_neon.h>
950  #endif
951  #endif
952 #endif
953 
954 /* Compile-time CPU feature support. */
955 #if !defined(DR_FLAC_NO_SIMD) && (defined(DRFLAC_X86) || defined(DRFLAC_X64))
956  #if defined(_MSC_VER) && !defined(__clang__)
957  #if _MSC_VER >= 1400
958  #include <intrin.h>
959  static void drflac__cpuid(int info[4], int fid)
960  {
961  __cpuid(info, fid);
962  }
963  #else
964  #define DRFLAC_NO_CPUID
965  #endif
966  #else
967  #if defined(__GNUC__) || defined(__clang__)
968  static void drflac__cpuid(int info[4], int fid)
969  {
970  /*
971  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
972  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
973  supporting different assembly dialects.
974 
975  What's basically happening is that we're saving and restoring the ebx register manually.
976  */
977  #if defined(DRFLAC_X86) && defined(__PIC__)
978  __asm__ __volatile__ (
979  "xchg{l} {%%}ebx, %k1;"
980  "cpuid;"
981  "xchg{l} {%%}ebx, %k1;"
982  : "=a"(info[0]), "=&r"(info[1]), "=c"(info[2]), "=d"(info[3]) : "a"(fid), "c"(0)
983  );
984  #else
985  __asm__ __volatile__ (
986  "cpuid" : "=a"(info[0]), "=b"(info[1]), "=c"(info[2]), "=d"(info[3]) : "a"(fid), "c"(0)
987  );
988  #endif
989  }
990  #else
991  #define DRFLAC_NO_CPUID
992  #endif
993  #endif
994 #else
995  #define DRFLAC_NO_CPUID
996 #endif
997 
998 static DRFLAC_INLINE drflac_bool32 drflac_has_sse2()
999 {
1000 #if defined(DRFLAC_SUPPORT_SSE2)
1001  #if (defined(DRFLAC_X64) || defined(DRFLAC_X86)) && !defined(DRFLAC_NO_SSE2)
1002  #if defined(DRFLAC_X64)
1003  return DRFLAC_TRUE; /* 64-bit targets always support SSE2. */
1004  #elif (defined(_M_IX86_FP) && _M_IX86_FP == 2) || defined(__SSE2__)
1005  return DRFLAC_TRUE; /* If the compiler is allowed to freely generate SSE2 code we can assume support. */
1006  #else
1007  #if defined(DRFLAC_NO_CPUID)
1008  return DRFLAC_FALSE;
1009  #else
1010  int info[4];
1011  drflac_cpuid(info, 1);
1012  return (info[3] & (1 << 26)) != 0;
1013  #endif
1014  #endif
1015  #else
1016  return DRFLAC_FALSE; /* SSE2 is only supported on x86 and x64 architectures. */
1017  #endif
1018 #else
1019  return DRFLAC_FALSE; /* No compiler support. */
1020 #endif
1021 }
1022 
1023 static DRFLAC_INLINE drflac_bool32 drflac_has_sse41()
1024 {
1025 #if defined(DRFLAC_SUPPORT_SSE41)
1026  #if (defined(DRFLAC_X64) || defined(DRFLAC_X86)) && !defined(DRFLAC_NO_SSE41)
1027  #if defined(DRFLAC_X64)
1028  return DRFLAC_TRUE; /* 64-bit targets always support SSE4.1. */
1029  #elif (defined(_M_IX86_FP) && _M_IX86_FP == 2) || defined(__SSE4_1__)
1030  return DRFLAC_TRUE; /* If the compiler is allowed to freely generate SSE41 code we can assume support. */
1031  #else
1032  #if defined(DRFLAC_NO_CPUID)
1033  return DRFLAC_FALSE;
1034  #else
1035  int info[4];
1036  drflac_cpuid(info, 1);
1037  return (info[2] & (1 << 19)) != 0;
1038  #endif
1039  #endif
1040  #else
1041  return DRFLAC_FALSE; /* SSE41 is only supported on x86 and x64 architectures. */
1042  #endif
1043 #else
1044  return DRFLAC_FALSE; /* No compiler support. */
1045 #endif
1046 }
1047 
1048 
1049 #if defined(_MSC_VER) && _MSC_VER >= 1500 && (defined(DRFLAC_X86) || defined(DRFLAC_X64))
1050  #define DRFLAC_HAS_LZCNT_INTRINSIC
1051 #elif (defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)))
1052  #define DRFLAC_HAS_LZCNT_INTRINSIC
1053 #elif defined(__clang__)
1054  #if __has_builtin(__builtin_clzll) || __has_builtin(__builtin_clzl)
1055  #define DRFLAC_HAS_LZCNT_INTRINSIC
1056  #endif
1057 #endif
1058 
1059 #if defined(_MSC_VER) && _MSC_VER >= 1300
1060  #define DRFLAC_HAS_BYTESWAP16_INTRINSIC
1061  #define DRFLAC_HAS_BYTESWAP32_INTRINSIC
1062  #define DRFLAC_HAS_BYTESWAP64_INTRINSIC
1063 #elif defined(__clang__)
1064  #if __has_builtin(__builtin_bswap16)
1065  #define DRFLAC_HAS_BYTESWAP16_INTRINSIC
1066  #endif
1067  #if __has_builtin(__builtin_bswap32)
1068  #define DRFLAC_HAS_BYTESWAP32_INTRINSIC
1069  #endif
1070  #if __has_builtin(__builtin_bswap64)
1071  #define DRFLAC_HAS_BYTESWAP64_INTRINSIC
1072  #endif
1073 #elif defined(__GNUC__)
1074  #if ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
1075  #define DRFLAC_HAS_BYTESWAP32_INTRINSIC
1076  #define DRFLAC_HAS_BYTESWAP64_INTRINSIC
1077  #endif
1078  #if ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))
1079  #define DRFLAC_HAS_BYTESWAP16_INTRINSIC
1080  #endif
1081 #endif
1082 
1083 
1084 /* Standard library stuff. */
1085 #ifndef DRFLAC_ASSERT
1086 #include <assert.h>
1087 #define DRFLAC_ASSERT(expression) assert(expression)
1088 #endif
1089 #ifndef DRFLAC_MALLOC
1090 #define DRFLAC_MALLOC(sz) malloc((sz))
1091 #endif
1092 #ifndef DRFLAC_REALLOC
1093 #define DRFLAC_REALLOC(p, sz) realloc((p), (sz))
1094 #endif
1095 #ifndef DRFLAC_FREE
1096 #define DRFLAC_FREE(p) free((p))
1097 #endif
1098 #ifndef DRFLAC_COPY_MEMORY
1099 #define DRFLAC_COPY_MEMORY(dst, src, sz) memcpy((dst), (src), (sz))
1100 #endif
1101 #ifndef DRFLAC_ZERO_MEMORY
1102 #define DRFLAC_ZERO_MEMORY(p, sz) memset((p), 0, (sz))
1103 #endif
1104 
1105 #define DRFLAC_MAX_SIMD_VECTOR_SIZE 64 /* 64 for AVX-512 in the future. */
1106 
1107 typedef drflac_int32 drflac_result;
1108 #define DRFLAC_SUCCESS 0
1109 #define DRFLAC_ERROR -1 /* A generic error. */
1110 #define DRFLAC_INVALID_ARGS -2
1111 #define DRFLAC_END_OF_STREAM -128
1112 #define DRFLAC_CRC_MISMATCH -129
1113 
1114 #define DRFLAC_SUBFRAME_CONSTANT 0
1115 #define DRFLAC_SUBFRAME_VERBATIM 1
1116 #define DRFLAC_SUBFRAME_FIXED 8
1117 #define DRFLAC_SUBFRAME_LPC 32
1118 #define DRFLAC_SUBFRAME_RESERVED 255
1119 
1120 #define DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE 0
1121 #define DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2 1
1122 
1123 #define DRFLAC_CHANNEL_ASSIGNMENT_INDEPENDENT 0
1124 #define DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE 8
1125 #define DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE 9
1126 #define DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE 10
1127 
1128 /*
1129 Keeps track of the number of leading samples for each sub-frame. This is required because the SSE pipeline will occasionally
1130 reference excess prior samples.
1131 */
1132 #define DRFLAC_LEADING_SAMPLES 32
1133 
1134 
1135 #define drflac_align(x, a) ((((x) + (a) - 1) / (a)) * (a))
1136 #define drflac_assert DRFLAC_ASSERT
1137 #define drflac_copy_memory DRFLAC_COPY_MEMORY
1138 #define drflac_zero_memory DRFLAC_ZERO_MEMORY
1139 
1140 
1141 /* CPU caps. */
1142 static drflac_bool32 drflac__gIsLZCNTSupported = DRFLAC_FALSE;
1143 #ifndef DRFLAC_NO_CPUID
1144 static drflac_bool32 drflac__gIsSSE2Supported = DRFLAC_FALSE;
1145 static drflac_bool32 drflac__gIsSSE41Supported = DRFLAC_FALSE;
1146 static void drflac__init_cpu_caps()
1147 {
1148  static drflac_bool32 isCPUCapsInitialized = DRFLAC_FALSE;
1149 
1150  if (!isCPUCapsInitialized) {
1151  int info[4] = {0};
1152 
1153  /* LZCNT */
1154  drflac__cpuid(info, 0x80000001);
1155  drflac__gIsLZCNTSupported = (info[2] & (1 << 5)) != 0;
1156 
1157  /* SSE2 */
1158  drflac__gIsSSE2Supported = drflac_has_sse2();
1159 
1160  /* SSE4.1 */
1161  drflac__gIsSSE41Supported = drflac_has_sse41();
1162 
1163  /* Initialized. */
1164  isCPUCapsInitialized = DRFLAC_TRUE;
1165  }
1166 }
1167 #endif
1168 
1169 
1170 /* Endian Management */
1171 static DRFLAC_INLINE drflac_bool32 drflac__is_little_endian()
1172 {
1173 #if defined(DRFLAC_X86) || defined(DRFLAC_X64)
1174  return DRFLAC_TRUE;
1175 #elif defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN
1176  return DRFLAC_TRUE;
1177 #else
1178  int n = 1;
1179  return (*(char*)&n) == 1;
1180 #endif
1181 }
1182 
1183 static DRFLAC_INLINE drflac_uint16 drflac__swap_endian_uint16(drflac_uint16 n)
1184 {
1185 #ifdef DRFLAC_HAS_BYTESWAP16_INTRINSIC
1186  #if defined(_MSC_VER)
1187  return _byteswap_ushort(n);
1188  #elif defined(__GNUC__) || defined(__clang__)
1189  return __builtin_bswap16(n);
1190  #else
1191  #error "This compiler does not support the byte swap intrinsic."
1192  #endif
1193 #else
1194  return ((n & 0xFF00) >> 8) |
1195  ((n & 0x00FF) << 8);
1196 #endif
1197 }
1198 
1199 static DRFLAC_INLINE drflac_uint32 drflac__swap_endian_uint32(drflac_uint32 n)
1200 {
1201 #ifdef DRFLAC_HAS_BYTESWAP32_INTRINSIC
1202  #if defined(_MSC_VER)
1203  return _byteswap_ulong(n);
1204  #elif defined(__GNUC__) || defined(__clang__)
1205  return __builtin_bswap32(n);
1206  #else
1207  #error "This compiler does not support the byte swap intrinsic."
1208  #endif
1209 #else
1210  return ((n & 0xFF000000) >> 24) |
1211  ((n & 0x00FF0000) >> 8) |
1212  ((n & 0x0000FF00) << 8) |
1213  ((n & 0x000000FF) << 24);
1214 #endif
1215 }
1216 
1217 static DRFLAC_INLINE drflac_uint64 drflac__swap_endian_uint64(drflac_uint64 n)
1218 {
1219 #ifdef DRFLAC_HAS_BYTESWAP64_INTRINSIC
1220  #if defined(_MSC_VER)
1221  return _byteswap_uint64(n);
1222  #elif defined(__GNUC__) || defined(__clang__)
1223  return __builtin_bswap64(n);
1224  #else
1225  #error "This compiler does not support the byte swap intrinsic."
1226  #endif
1227 #else
1228  return ((n & (drflac_uint64)0xFF00000000000000) >> 56) |
1229  ((n & (drflac_uint64)0x00FF000000000000) >> 40) |
1230  ((n & (drflac_uint64)0x0000FF0000000000) >> 24) |
1231  ((n & (drflac_uint64)0x000000FF00000000) >> 8) |
1232  ((n & (drflac_uint64)0x00000000FF000000) << 8) |
1233  ((n & (drflac_uint64)0x0000000000FF0000) << 24) |
1234  ((n & (drflac_uint64)0x000000000000FF00) << 40) |
1235  ((n & (drflac_uint64)0x00000000000000FF) << 56);
1236 #endif
1237 }
1238 
1239 
1240 static DRFLAC_INLINE drflac_uint16 drflac__be2host_16(drflac_uint16 n)
1241 {
1242  if (drflac__is_little_endian()) {
1243  return drflac__swap_endian_uint16(n);
1244  }
1245 
1246  return n;
1247 }
1248 
1249 static DRFLAC_INLINE drflac_uint32 drflac__be2host_32(drflac_uint32 n)
1250 {
1251  if (drflac__is_little_endian()) {
1252  return drflac__swap_endian_uint32(n);
1253  }
1254 
1255  return n;
1256 }
1257 
1258 static DRFLAC_INLINE drflac_uint64 drflac__be2host_64(drflac_uint64 n)
1259 {
1260  if (drflac__is_little_endian()) {
1261  return drflac__swap_endian_uint64(n);
1262  }
1263 
1264  return n;
1265 }
1266 
1267 
1268 static DRFLAC_INLINE drflac_uint32 drflac__le2host_32(drflac_uint32 n)
1269 {
1270  if (!drflac__is_little_endian()) {
1271  return drflac__swap_endian_uint32(n);
1272  }
1273 
1274  return n;
1275 }
1276 
1277 
1278 static DRFLAC_INLINE drflac_uint32 drflac__unsynchsafe_32(drflac_uint32 n)
1279 {
1280  drflac_uint32 result = 0;
1281  result |= (n & 0x7F000000) >> 3;
1282  result |= (n & 0x007F0000) >> 2;
1283  result |= (n & 0x00007F00) >> 1;
1284  result |= (n & 0x0000007F) >> 0;
1285 
1286  return result;
1287 }
1288 
1289 
1290 
1291 /* The CRC code below is based on this document: http://zlib.net/crc_v3.txt */
1292 static drflac_uint8 drflac__crc8_table[] = {
1293  0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D,
1294  0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D,
1295  0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD,
1296  0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD,
1297  0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA,
1298  0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A,
1299  0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A,
1300  0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A,
1301  0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4,
1302  0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4,
1303  0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44,
1304  0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34,
1305  0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63,
1306  0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13,
1307  0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,
1308  0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3
1309 };
1310 
1311 static drflac_uint16 drflac__crc16_table[] = {
1312  0x0000, 0x8005, 0x800F, 0x000A, 0x801B, 0x001E, 0x0014, 0x8011,
1313  0x8033, 0x0036, 0x003C, 0x8039, 0x0028, 0x802D, 0x8027, 0x0022,
1314  0x8063, 0x0066, 0x006C, 0x8069, 0x0078, 0x807D, 0x8077, 0x0072,
1315  0x0050, 0x8055, 0x805F, 0x005A, 0x804B, 0x004E, 0x0044, 0x8041,
1316  0x80C3, 0x00C6, 0x00CC, 0x80C9, 0x00D8, 0x80DD, 0x80D7, 0x00D2,
1317  0x00F0, 0x80F5, 0x80FF, 0x00FA, 0x80EB, 0x00EE, 0x00E4, 0x80E1,
1318  0x00A0, 0x80A5, 0x80AF, 0x00AA, 0x80BB, 0x00BE, 0x00B4, 0x80B1,
1319  0x8093, 0x0096, 0x009C, 0x8099, 0x0088, 0x808D, 0x8087, 0x0082,
1320  0x8183, 0x0186, 0x018C, 0x8189, 0x0198, 0x819D, 0x8197, 0x0192,
1321  0x01B0, 0x81B5, 0x81BF, 0x01BA, 0x81AB, 0x01AE, 0x01A4, 0x81A1,
1322  0x01E0, 0x81E5, 0x81EF, 0x01EA, 0x81FB, 0x01FE, 0x01F4, 0x81F1,
1323  0x81D3, 0x01D6, 0x01DC, 0x81D9, 0x01C8, 0x81CD, 0x81C7, 0x01C2,
1324  0x0140, 0x8145, 0x814F, 0x014A, 0x815B, 0x015E, 0x0154, 0x8151,
1325  0x8173, 0x0176, 0x017C, 0x8179, 0x0168, 0x816D, 0x8167, 0x0162,
1326  0x8123, 0x0126, 0x012C, 0x8129, 0x0138, 0x813D, 0x8137, 0x0132,
1327  0x0110, 0x8115, 0x811F, 0x011A, 0x810B, 0x010E, 0x0104, 0x8101,
1328  0x8303, 0x0306, 0x030C, 0x8309, 0x0318, 0x831D, 0x8317, 0x0312,
1329  0x0330, 0x8335, 0x833F, 0x033A, 0x832B, 0x032E, 0x0324, 0x8321,
1330  0x0360, 0x8365, 0x836F, 0x036A, 0x837B, 0x037E, 0x0374, 0x8371,
1331  0x8353, 0x0356, 0x035C, 0x8359, 0x0348, 0x834D, 0x8347, 0x0342,
1332  0x03C0, 0x83C5, 0x83CF, 0x03CA, 0x83DB, 0x03DE, 0x03D4, 0x83D1,
1333  0x83F3, 0x03F6, 0x03FC, 0x83F9, 0x03E8, 0x83ED, 0x83E7, 0x03E2,
1334  0x83A3, 0x03A6, 0x03AC, 0x83A9, 0x03B8, 0x83BD, 0x83B7, 0x03B2,
1335  0x0390, 0x8395, 0x839F, 0x039A, 0x838B, 0x038E, 0x0384, 0x8381,
1336  0x0280, 0x8285, 0x828F, 0x028A, 0x829B, 0x029E, 0x0294, 0x8291,
1337  0x82B3, 0x02B6, 0x02BC, 0x82B9, 0x02A8, 0x82AD, 0x82A7, 0x02A2,
1338  0x82E3, 0x02E6, 0x02EC, 0x82E9, 0x02F8, 0x82FD, 0x82F7, 0x02F2,
1339  0x02D0, 0x82D5, 0x82DF, 0x02DA, 0x82CB, 0x02CE, 0x02C4, 0x82C1,
1340  0x8243, 0x0246, 0x024C, 0x8249, 0x0258, 0x825D, 0x8257, 0x0252,
1341  0x0270, 0x8275, 0x827F, 0x027A, 0x826B, 0x026E, 0x0264, 0x8261,
1342  0x0220, 0x8225, 0x822F, 0x022A, 0x823B, 0x023E, 0x0234, 0x8231,
1343  0x8213, 0x0216, 0x021C, 0x8219, 0x0208, 0x820D, 0x8207, 0x0202
1344 };
1345 
1346 static DRFLAC_INLINE drflac_uint8 drflac_crc8_byte(drflac_uint8 crc, drflac_uint8 data)
1347 {
1348  return drflac__crc8_table[crc ^ data];
1349 }
1350 
1351 static DRFLAC_INLINE drflac_uint8 drflac_crc8(drflac_uint8 crc, drflac_uint32 data, drflac_uint32 count)
1352 {
1353 #ifdef DR_FLAC_NO_CRC
1354  (void)crc;
1355  (void)data;
1356  (void)count;
1357  return 0;
1358 #else
1359 #if 0
1360  /* REFERENCE (use of this implementation requires an explicit flush by doing "drflac_crc8(crc, 0, 8);") */
1361  drflac_uint8 p = 0x07;
1362  for (int i = count-1; i >= 0; --i) {
1363  drflac_uint8 bit = (data & (1 << i)) >> i;
1364  if (crc & 0x80) {
1365  crc = ((crc << 1) | bit) ^ p;
1366  } else {
1367  crc = ((crc << 1) | bit);
1368  }
1369  }
1370  return crc;
1371 #else
1372  drflac_uint32 wholeBytes;
1373  drflac_uint32 leftoverBits;
1374  drflac_uint64 leftoverDataMask;
1375 
1376  static drflac_uint64 leftoverDataMaskTable[8] = {
1377  0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F
1378  };
1379 
1380  drflac_assert(count <= 32);
1381 
1382  wholeBytes = count >> 3;
1383  leftoverBits = count - (wholeBytes*8);
1384  leftoverDataMask = leftoverDataMaskTable[leftoverBits];
1385 
1386  switch (wholeBytes) {
1387  case 4: crc = drflac_crc8_byte(crc, (drflac_uint8)((data & (0xFF000000UL << leftoverBits)) >> (24 + leftoverBits)));
1388  case 3: crc = drflac_crc8_byte(crc, (drflac_uint8)((data & (0x00FF0000UL << leftoverBits)) >> (16 + leftoverBits)));
1389  case 2: crc = drflac_crc8_byte(crc, (drflac_uint8)((data & (0x0000FF00UL << leftoverBits)) >> ( 8 + leftoverBits)));
1390  case 1: crc = drflac_crc8_byte(crc, (drflac_uint8)((data & (0x000000FFUL << leftoverBits)) >> ( 0 + leftoverBits)));
1391  case 0: if (leftoverBits > 0) crc = (crc << leftoverBits) ^ drflac__crc8_table[(crc >> (8 - leftoverBits)) ^ (data & leftoverDataMask)];
1392  }
1393  return crc;
1394 #endif
1395 #endif
1396 }
1397 
1398 static DRFLAC_INLINE drflac_uint16 drflac_crc16_byte(drflac_uint16 crc, drflac_uint8 data)
1399 {
1400  return (crc << 8) ^ drflac__crc16_table[(drflac_uint8)(crc >> 8) ^ data];
1401 }
1402 
1403 static DRFLAC_INLINE drflac_uint16 drflac_crc16_bytes(drflac_uint16 crc, drflac_cache_t data, drflac_uint32 byteCount)
1404 {
1405  switch (byteCount)
1406  {
1407 #ifdef DRFLAC_64BIT
1408  case 8: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 56) & 0xFF));
1409  case 7: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 48) & 0xFF));
1410  case 6: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 40) & 0xFF));
1411  case 5: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 32) & 0xFF));
1412 #endif
1413  case 4: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 24) & 0xFF));
1414  case 3: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 16) & 0xFF));
1415  case 2: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 8) & 0xFF));
1416  case 1: crc = drflac_crc16_byte(crc, (drflac_uint8)((data >> 0) & 0xFF));
1417  }
1418 
1419  return crc;
1420 }
1421 
1422 static DRFLAC_INLINE drflac_uint16 drflac_crc16__32bit(drflac_uint16 crc, drflac_uint32 data, drflac_uint32 count)
1423 {
1424 #ifdef DR_FLAC_NO_CRC
1425  (void)crc;
1426  (void)data;
1427  (void)count;
1428  return 0;
1429 #else
1430 #if 0
1431  /* REFERENCE (use of this implementation requires an explicit flush by doing "drflac_crc16(crc, 0, 16);") */
1432  drflac_uint16 p = 0x8005;
1433  for (int i = count-1; i >= 0; --i) {
1434  drflac_uint16 bit = (data & (1ULL << i)) >> i;
1435  if (r & 0x8000) {
1436  r = ((r << 1) | bit) ^ p;
1437  } else {
1438  r = ((r << 1) | bit);
1439  }
1440  }
1441 
1442  return crc;
1443 #else
1444  drflac_uint32 wholeBytes;
1445  drflac_uint32 leftoverBits;
1446  drflac_uint64 leftoverDataMask;
1447 
1448  static drflac_uint64 leftoverDataMaskTable[8] = {
1449  0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F
1450  };
1451 
1452  drflac_assert(count <= 64);
1453 
1454  wholeBytes = count >> 3;
1455  leftoverBits = count - (wholeBytes*8);
1456  leftoverDataMask = leftoverDataMaskTable[leftoverBits];
1457 
1458  switch (wholeBytes) {
1459  default:
1460  case 4: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (0xFF000000UL << leftoverBits)) >> (24 + leftoverBits)));
1461  case 3: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (0x00FF0000UL << leftoverBits)) >> (16 + leftoverBits)));
1462  case 2: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (0x0000FF00UL << leftoverBits)) >> ( 8 + leftoverBits)));
1463  case 1: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (0x000000FFUL << leftoverBits)) >> ( 0 + leftoverBits)));
1464  case 0: if (leftoverBits > 0) crc = (crc << leftoverBits) ^ drflac__crc16_table[(crc >> (16 - leftoverBits)) ^ (data & leftoverDataMask)];
1465  }
1466  return crc;
1467 #endif
1468 #endif
1469 }
1470 
1471 static DRFLAC_INLINE drflac_uint16 drflac_crc16__64bit(drflac_uint16 crc, drflac_uint64 data, drflac_uint32 count)
1472 {
1473 #ifdef DR_FLAC_NO_CRC
1474  (void)crc;
1475  (void)data;
1476  (void)count;
1477  return 0;
1478 #else
1479  drflac_uint32 wholeBytes;
1480  drflac_uint32 leftoverBits;
1481  drflac_uint64 leftoverDataMask;
1482 
1483  static drflac_uint64 leftoverDataMaskTable[8] = {
1484  0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F
1485  };
1486 
1487  drflac_assert(count <= 64);
1488 
1489  wholeBytes = count >> 3;
1490  leftoverBits = count - (wholeBytes*8);
1491  leftoverDataMask = leftoverDataMaskTable[leftoverBits];
1492 
1493  switch (wholeBytes) {
1494  default:
1495  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. */
1496  case 7: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0x00FF0000 << 32) << leftoverBits)) >> (48 + leftoverBits)));
1497  case 6: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0x0000FF00 << 32) << leftoverBits)) >> (40 + leftoverBits)));
1498  case 5: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0x000000FF << 32) << leftoverBits)) >> (32 + leftoverBits)));
1499  case 4: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0xFF000000 ) << leftoverBits)) >> (24 + leftoverBits)));
1500  case 3: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0x00FF0000 ) << leftoverBits)) >> (16 + leftoverBits)));
1501  case 2: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0x0000FF00 ) << leftoverBits)) >> ( 8 + leftoverBits)));
1502  case 1: crc = drflac_crc16_byte(crc, (drflac_uint8)((data & (((drflac_uint64)0x000000FF ) << leftoverBits)) >> ( 0 + leftoverBits)));
1503  case 0: if (leftoverBits > 0) crc = (crc << leftoverBits) ^ drflac__crc16_table[(crc >> (16 - leftoverBits)) ^ (data & leftoverDataMask)];
1504  }
1505  return crc;
1506 #endif
1507 }
1508 
1509 
1510 static DRFLAC_INLINE drflac_uint16 drflac_crc16(drflac_uint16 crc, drflac_cache_t data, drflac_uint32 count)
1511 {
1512 #ifdef DRFLAC_64BIT
1513  return drflac_crc16__64bit(crc, data, count);
1514 #else
1515  return drflac_crc16__32bit(crc, data, count);
1516 #endif
1517 }
1518 
1519 
1520 #ifdef DRFLAC_64BIT
1521 #define drflac__be2host__cache_line drflac__be2host_64
1522 #else
1523 #define drflac__be2host__cache_line drflac__be2host_32
1524 #endif
1525 
1526 /*
1527 BIT READING ATTEMPT #2
1528 
1529 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
1530 on the most significant bit. It uses the notion of an L1 and L2 cache (borrowed from CPU architecture), where the L1 cache
1531 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
1532 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
1533 from onRead() is read into.
1534 */
1535 #define DRFLAC_CACHE_L1_SIZE_BYTES(bs) (sizeof((bs)->cache))
1536 #define DRFLAC_CACHE_L1_SIZE_BITS(bs) (sizeof((bs)->cache)*8)
1537 #define DRFLAC_CACHE_L1_BITS_REMAINING(bs) (DRFLAC_CACHE_L1_SIZE_BITS(bs) - (bs)->consumedBits)
1538 #define DRFLAC_CACHE_L1_SELECTION_MASK(_bitCount) (~((~(drflac_cache_t)0) >> (_bitCount)))
1539 #define DRFLAC_CACHE_L1_SELECTION_SHIFT(bs, _bitCount) (DRFLAC_CACHE_L1_SIZE_BITS(bs) - (_bitCount))
1540 #define DRFLAC_CACHE_L1_SELECT(bs, _bitCount) (((bs)->cache) & DRFLAC_CACHE_L1_SELECTION_MASK(_bitCount))
1541 #define DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, _bitCount) (DRFLAC_CACHE_L1_SELECT((bs), (_bitCount)) >> DRFLAC_CACHE_L1_SELECTION_SHIFT((bs), (_bitCount)))
1542 #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)))
1543 #define DRFLAC_CACHE_L2_SIZE_BYTES(bs) (sizeof((bs)->cacheL2))
1544 #define DRFLAC_CACHE_L2_LINE_COUNT(bs) (DRFLAC_CACHE_L2_SIZE_BYTES(bs) / sizeof((bs)->cacheL2[0]))
1545 #define DRFLAC_CACHE_L2_LINES_REMAINING(bs) (DRFLAC_CACHE_L2_LINE_COUNT(bs) - (bs)->nextL2Line)
1546 
1547 
1548 #ifndef DR_FLAC_NO_CRC
1549 static DRFLAC_INLINE void drflac__reset_crc16(drflac_bs* bs)
1550 {
1551  bs->crc16 = 0;
1552  bs->crc16CacheIgnoredBytes = bs->consumedBits >> 3;
1553 }
1554 
1555 static DRFLAC_INLINE void drflac__update_crc16(drflac_bs* bs)
1556 {
1557  bs->crc16 = drflac_crc16_bytes(bs->crc16, bs->crc16Cache, DRFLAC_CACHE_L1_SIZE_BYTES(bs) - bs->crc16CacheIgnoredBytes);
1558  bs->crc16CacheIgnoredBytes = 0;
1559 }
1560 
1561 static DRFLAC_INLINE drflac_uint16 drflac__flush_crc16(drflac_bs* bs)
1562 {
1563  /* We should never be flushing in a situation where we are not aligned on a byte boundary. */
1564  drflac_assert((DRFLAC_CACHE_L1_BITS_REMAINING(bs) & 7) == 0);
1565 
1566  /*
1567  The bits that were read from the L1 cache need to be accumulated. The number of bytes needing to be accumulated is determined
1568  by the number of bits that have been consumed.
1569  */
1570  if (DRFLAC_CACHE_L1_BITS_REMAINING(bs) == 0) {
1571  drflac__update_crc16(bs);
1572  } else {
1573  /* We only accumulate the consumed bits. */
1574  bs->crc16 = drflac_crc16_bytes(bs->crc16, bs->crc16Cache >> DRFLAC_CACHE_L1_BITS_REMAINING(bs), (bs->consumedBits >> 3) - bs->crc16CacheIgnoredBytes);
1575 
1576  /*
1577  The bits that we just accumulated should never be accumulated again. We need to keep track of how many bytes were accumulated
1578  so we can handle that later.
1579  */
1580  bs->crc16CacheIgnoredBytes = bs->consumedBits >> 3;
1581  }
1582 
1583  return bs->crc16;
1584 }
1585 #endif
1586 
1587 static DRFLAC_INLINE drflac_bool32 drflac__reload_l1_cache_from_l2(drflac_bs* bs)
1588 {
1589  size_t bytesRead;
1590  size_t alignedL1LineCount;
1591 
1592  /* Fast path. Try loading straight from L2. */
1593  if (bs->nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) {
1594  bs->cache = bs->cacheL2[bs->nextL2Line++];
1595  return DRFLAC_TRUE;
1596  }
1597 
1598  /*
1599  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
1600  any left.
1601  */
1602  if (bs->unalignedByteCount > 0) {
1603  return DRFLAC_FALSE; /* If we have any unaligned bytes it means there's no more aligned bytes left in the client. */
1604  }
1605 
1606  bytesRead = bs->onRead(bs->pUserData, bs->cacheL2, DRFLAC_CACHE_L2_SIZE_BYTES(bs));
1607 
1608  bs->nextL2Line = 0;
1609  if (bytesRead == DRFLAC_CACHE_L2_SIZE_BYTES(bs)) {
1610  bs->cache = bs->cacheL2[bs->nextL2Line++];
1611  return DRFLAC_TRUE;
1612  }
1613 
1614 
1615  /*
1616  If we get here it means we were unable to retrieve enough data to fill the entire L2 cache. It probably
1617  means we've just reached the end of the file. We need to move the valid data down to the end of the buffer
1618  and adjust the index of the next line accordingly. Also keep in mind that the L2 cache must be aligned to
1619  the size of the L1 so we'll need to seek backwards by any misaligned bytes.
1620  */
1621  alignedL1LineCount = bytesRead / DRFLAC_CACHE_L1_SIZE_BYTES(bs);
1622 
1623  /* We need to keep track of any unaligned bytes for later use. */
1624  bs->unalignedByteCount = bytesRead - (alignedL1LineCount * DRFLAC_CACHE_L1_SIZE_BYTES(bs));
1625  if (bs->unalignedByteCount > 0) {
1626  bs->unalignedCache = bs->cacheL2[alignedL1LineCount];
1627  }
1628 
1629  if (alignedL1LineCount > 0) {
1630  size_t offset = DRFLAC_CACHE_L2_LINE_COUNT(bs) - alignedL1LineCount;
1631  size_t i;
1632  for (i = alignedL1LineCount; i > 0; --i) {
1633  bs->cacheL2[i-1 + offset] = bs->cacheL2[i-1];
1634  }
1635 
1636  bs->nextL2Line = (drflac_uint32)offset;
1637  bs->cache = bs->cacheL2[bs->nextL2Line++];
1638  return DRFLAC_TRUE;
1639  } else {
1640  /* If we get into this branch it means we weren't able to load any L1-aligned data. */
1641  bs->nextL2Line = DRFLAC_CACHE_L2_LINE_COUNT(bs);
1642  return DRFLAC_FALSE;
1643  }
1644 }
1645 
1646 static drflac_bool32 drflac__reload_cache(drflac_bs* bs)
1647 {
1648  size_t bytesRead;
1649 
1650 #ifndef DR_FLAC_NO_CRC
1651  drflac__update_crc16(bs);
1652 #endif
1653 
1654  /* Fast path. Try just moving the next value in the L2 cache to the L1 cache. */
1655  if (drflac__reload_l1_cache_from_l2(bs)) {
1656  bs->cache = drflac__be2host__cache_line(bs->cache);
1657  bs->consumedBits = 0;
1658 #ifndef DR_FLAC_NO_CRC
1659  bs->crc16Cache = bs->cache;
1660 #endif
1661  return DRFLAC_TRUE;
1662  }
1663 
1664  /* Slow path. */
1665 
1666  /*
1667  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
1668  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
1669  data from the unaligned cache.
1670  */
1671  bytesRead = bs->unalignedByteCount;
1672  if (bytesRead == 0) {
1673  bs->consumedBits = DRFLAC_CACHE_L1_SIZE_BITS(bs); /* <-- The stream has been exhausted, so marked the bits as consumed. */
1674  return DRFLAC_FALSE;
1675  }
1676 
1677  drflac_assert(bytesRead < DRFLAC_CACHE_L1_SIZE_BYTES(bs));
1678  bs->consumedBits = (drflac_uint32)(DRFLAC_CACHE_L1_SIZE_BYTES(bs) - bytesRead) * 8;
1679 
1680  bs->cache = drflac__be2host__cache_line(bs->unalignedCache);
1681  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. */
1682  bs->unalignedByteCount = 0; /* <-- At this point the unaligned bytes have been moved into the cache and we thus have no more unaligned bytes. */
1683 
1684 #ifndef DR_FLAC_NO_CRC
1685  bs->crc16Cache = bs->cache >> bs->consumedBits;
1686  bs->crc16CacheIgnoredBytes = bs->consumedBits >> 3;
1687 #endif
1688  return DRFLAC_TRUE;
1689 }
1690 
1691 static void drflac__reset_cache(drflac_bs* bs)
1692 {
1693  bs->nextL2Line = DRFLAC_CACHE_L2_LINE_COUNT(bs); /* <-- This clears the L2 cache. */
1694  bs->consumedBits = DRFLAC_CACHE_L1_SIZE_BITS(bs); /* <-- This clears the L1 cache. */
1695  bs->cache = 0;
1696  bs->unalignedByteCount = 0; /* <-- This clears the trailing unaligned bytes. */
1697  bs->unalignedCache = 0;
1698 
1699 #ifndef DR_FLAC_NO_CRC
1700  bs->crc16Cache = 0;
1701  bs->crc16CacheIgnoredBytes = 0;
1702 #endif
1703 }
1704 
1705 
1706 static DRFLAC_INLINE drflac_bool32 drflac__read_uint32(drflac_bs* bs, unsigned int bitCount, drflac_uint32* pResultOut)
1707 {
1708  drflac_assert(bs != NULL);
1709  drflac_assert(pResultOut != NULL);
1710  drflac_assert(bitCount > 0);
1711  drflac_assert(bitCount <= 32);
1712 
1713  if (bs->consumedBits == DRFLAC_CACHE_L1_SIZE_BITS(bs)) {
1714  if (!drflac__reload_cache(bs)) {
1715  return DRFLAC_FALSE;
1716  }
1717  }
1718 
1719  if (bitCount <= DRFLAC_CACHE_L1_BITS_REMAINING(bs)) {
1720  /*
1721  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
1722  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
1723  more optimal solution for this.
1724  */
1725 #ifdef DRFLAC_64BIT
1726  *pResultOut = (drflac_uint32)DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, bitCount);
1727  bs->consumedBits += bitCount;
1728  bs->cache <<= bitCount;
1729 #else
1730  if (bitCount < DRFLAC_CACHE_L1_SIZE_BITS(bs)) {
1731  *pResultOut = (drflac_uint32)DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, bitCount);
1732  bs->consumedBits += bitCount;
1733  bs->cache <<= bitCount;
1734  } else {
1735  /* Cannot shift by 32-bits, so need to do it differently. */
1736  *pResultOut = (drflac_uint32)bs->cache;
1737  bs->consumedBits = DRFLAC_CACHE_L1_SIZE_BITS(bs);
1738  bs->cache = 0;
1739  }
1740 #endif
1741 
1742  return DRFLAC_TRUE;
1743  } else {
1744  /* 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. */
1745  drflac_uint32 bitCountHi = DRFLAC_CACHE_L1_BITS_REMAINING(bs);
1746  drflac_uint32 bitCountLo = bitCount - bitCountHi;
1747  drflac_uint32 resultHi = (drflac_uint32)DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, bitCountHi);
1748 
1749  if (!drflac__reload_cache(bs)) {
1750  return DRFLAC_FALSE;
1751  }
1752 
1753  *pResultOut = (resultHi << bitCountLo) | (drflac_uint32)DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, bitCountLo);
1754  bs->consumedBits += bitCountLo;
1755  bs->cache <<= bitCountLo;
1756  return DRFLAC_TRUE;
1757  }
1758 }
1759 
1760 static drflac_bool32 drflac__read_int32(drflac_bs* bs, unsigned int bitCount, drflac_int32* pResult)
1761 {
1762  drflac_uint32 result;
1763  drflac_uint32 signbit;
1764 
1765  drflac_assert(bs != NULL);
1766  drflac_assert(pResult != NULL);
1767  drflac_assert(bitCount > 0);
1768  drflac_assert(bitCount <= 32);
1769 
1770  if (!drflac__read_uint32(bs, bitCount, &result)) {
1771  return DRFLAC_FALSE;
1772  }
1773 
1774  signbit = ((result >> (bitCount-1)) & 0x01);
1775  result |= (~signbit + 1) << bitCount;
1776 
1777  *pResult = (drflac_int32)result;
1778  return DRFLAC_TRUE;
1779 }
1780 
1781 #ifdef DRFLAC_64BIT
1782 static drflac_bool32 drflac__read_uint64(drflac_bs* bs, unsigned int bitCount, drflac_uint64* pResultOut)
1783 {
1784  drflac_uint32 resultHi;
1785  drflac_uint32 resultLo;
1786 
1787  drflac_assert(bitCount <= 64);
1788  drflac_assert(bitCount > 32);
1789 
1790  if (!drflac__read_uint32(bs, bitCount - 32, &resultHi)) {
1791  return DRFLAC_FALSE;
1792  }
1793 
1794  if (!drflac__read_uint32(bs, 32, &resultLo)) {
1795  return DRFLAC_FALSE;
1796  }
1797 
1798  *pResultOut = (((drflac_uint64)resultHi) << 32) | ((drflac_uint64)resultLo);
1799  return DRFLAC_TRUE;
1800 }
1801 #endif
1802 
1803 /* Function below is unused, but leaving it here in case I need to quickly add it again. */
1804 #if 0
1805 static drflac_bool32 drflac__read_int64(drflac_bs* bs, unsigned int bitCount, drflac_int64* pResultOut)
1806 {
1807  drflac_uint64 result;
1808  drflac_uint64 signbit;
1809 
1810  drflac_assert(bitCount <= 64);
1811 
1812  if (!drflac__read_uint64(bs, bitCount, &result)) {
1813  return DRFLAC_FALSE;
1814  }
1815 
1816  signbit = ((result >> (bitCount-1)) & 0x01);
1817  result |= (~signbit + 1) << bitCount;
1818 
1819  *pResultOut = (drflac_int64)result;
1820  return DRFLAC_TRUE;
1821 }
1822 #endif
1823 
1824 static drflac_bool32 drflac__read_uint16(drflac_bs* bs, unsigned int bitCount, drflac_uint16* pResult)
1825 {
1826  drflac_uint32 result;
1827 
1828  drflac_assert(bs != NULL);
1829  drflac_assert(pResult != NULL);
1830  drflac_assert(bitCount > 0);
1831  drflac_assert(bitCount <= 16);
1832 
1833  if (!drflac__read_uint32(bs, bitCount, &result)) {
1834  return DRFLAC_FALSE;
1835  }
1836 
1837  *pResult = (drflac_uint16)result;
1838  return DRFLAC_TRUE;
1839 }
1840 
1841 #if 0
1842 static drflac_bool32 drflac__read_int16(drflac_bs* bs, unsigned int bitCount, drflac_int16* pResult)
1843 {
1844  drflac_int32 result;
1845 
1846  drflac_assert(bs != NULL);
1847  drflac_assert(pResult != NULL);
1848  drflac_assert(bitCount > 0);
1849  drflac_assert(bitCount <= 16);
1850 
1851  if (!drflac__read_int32(bs, bitCount, &result)) {
1852  return DRFLAC_FALSE;
1853  }
1854 
1855  *pResult = (drflac_int16)result;
1856  return DRFLAC_TRUE;
1857 }
1858 #endif
1859 
1860 static drflac_bool32 drflac__read_uint8(drflac_bs* bs, unsigned int bitCount, drflac_uint8* pResult)
1861 {
1862  drflac_uint32 result;
1863 
1864  drflac_assert(bs != NULL);
1865  drflac_assert(pResult != NULL);
1866  drflac_assert(bitCount > 0);
1867  drflac_assert(bitCount <= 8);
1868 
1869  if (!drflac__read_uint32(bs, bitCount, &result)) {
1870  return DRFLAC_FALSE;
1871  }
1872 
1873  *pResult = (drflac_uint8)result;
1874  return DRFLAC_TRUE;
1875 }
1876 
1877 static drflac_bool32 drflac__read_int8(drflac_bs* bs, unsigned int bitCount, drflac_int8* pResult)
1878 {
1879  drflac_int32 result;
1880 
1881  drflac_assert(bs != NULL);
1882  drflac_assert(pResult != NULL);
1883  drflac_assert(bitCount > 0);
1884  drflac_assert(bitCount <= 8);
1885 
1886  if (!drflac__read_int32(bs, bitCount, &result)) {
1887  return DRFLAC_FALSE;
1888  }
1889 
1890  *pResult = (drflac_int8)result;
1891  return DRFLAC_TRUE;
1892 }
1893 
1894 
1895 static drflac_bool32 drflac__seek_bits(drflac_bs* bs, size_t bitsToSeek)
1896 {
1897  if (bitsToSeek <= DRFLAC_CACHE_L1_BITS_REMAINING(bs)) {
1898  bs->consumedBits += (drflac_uint32)bitsToSeek;
1899  bs->cache <<= bitsToSeek;
1900  return DRFLAC_TRUE;
1901  } else {
1902  /* It straddles the cached data. This function isn't called too frequently so I'm favouring simplicity here. */
1903  bitsToSeek -= DRFLAC_CACHE_L1_BITS_REMAINING(bs);
1904  bs->consumedBits += DRFLAC_CACHE_L1_BITS_REMAINING(bs);
1905  bs->cache = 0;
1906 
1907  /* Simple case. Seek in groups of the same number as bits that fit within a cache line. */
1908 #ifdef DRFLAC_64BIT
1909  while (bitsToSeek >= DRFLAC_CACHE_L1_SIZE_BITS(bs)) {
1910  drflac_uint64 bin;
1911  if (!drflac__read_uint64(bs, DRFLAC_CACHE_L1_SIZE_BITS(bs), &bin)) {
1912  return DRFLAC_FALSE;
1913  }
1914  bitsToSeek -= DRFLAC_CACHE_L1_SIZE_BITS(bs);
1915  }
1916 #else
1917  while (bitsToSeek >= DRFLAC_CACHE_L1_SIZE_BITS(bs)) {
1918  drflac_uint32 bin;
1919  if (!drflac__read_uint32(bs, DRFLAC_CACHE_L1_SIZE_BITS(bs), &bin)) {
1920  return DRFLAC_FALSE;
1921  }
1922  bitsToSeek -= DRFLAC_CACHE_L1_SIZE_BITS(bs);
1923  }
1924 #endif
1925 
1926  /* Whole leftover bytes. */
1927  while (bitsToSeek >= 8) {
1928  drflac_uint8 bin;
1929  if (!drflac__read_uint8(bs, 8, &bin)) {
1930  return DRFLAC_FALSE;
1931  }
1932  bitsToSeek -= 8;
1933  }
1934 
1935  /* Leftover bits. */
1936  if (bitsToSeek > 0) {
1937  drflac_uint8 bin;
1938  if (!drflac__read_uint8(bs, (drflac_uint32)bitsToSeek, &bin)) {
1939  return DRFLAC_FALSE;
1940  }
1941  bitsToSeek = 0; /* <-- Necessary for the assert below. */
1942  }
1943 
1944  drflac_assert(bitsToSeek == 0);
1945  return DRFLAC_TRUE;
1946  }
1947 }
1948 
1949 
1950 /* 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. */
1951 static drflac_bool32 drflac__find_and_seek_to_next_sync_code(drflac_bs* bs)
1952 {
1953  drflac_assert(bs != NULL);
1954 
1955  /*
1956  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
1957  thing to do is align to the next byte.
1958  */
1959  if (!drflac__seek_bits(bs, DRFLAC_CACHE_L1_BITS_REMAINING(bs) & 7)) {
1960  return DRFLAC_FALSE;
1961  }
1962 
1963  for (;;) {
1964  drflac_uint8 hi;
1965 
1966 #ifndef DR_FLAC_NO_CRC
1967  drflac__reset_crc16(bs);
1968 #endif
1969 
1970  if (!drflac__read_uint8(bs, 8, &hi)) {
1971  return DRFLAC_FALSE;
1972  }
1973 
1974  if (hi == 0xFF) {
1975  drflac_uint8 lo;
1976  if (!drflac__read_uint8(bs, 6, &lo)) {
1977  return DRFLAC_FALSE;
1978  }
1979 
1980  if (lo == 0x3E) {
1981  return DRFLAC_TRUE;
1982  } else {
1983  if (!drflac__seek_bits(bs, DRFLAC_CACHE_L1_BITS_REMAINING(bs) & 7)) {
1984  return DRFLAC_FALSE;
1985  }
1986  }
1987  }
1988  }
1989 
1990  /* Should never get here. */
1991  /*return DRFLAC_FALSE;*/
1992 }
1993 
1994 
1995 #if !defined(DR_FLAC_NO_SIMD) && defined(DRFLAC_HAS_LZCNT_INTRINSIC)
1996 #define DRFLAC_IMPLEMENT_CLZ_LZCNT
1997 #endif
1998 #if defined(_MSC_VER) && _MSC_VER >= 1400 && (defined(DRFLAC_X64) || defined(DRFLAC_X86))
1999 #define DRFLAC_IMPLEMENT_CLZ_MSVC
2000 #endif
2001 
2002 static DRFLAC_INLINE drflac_uint32 drflac__clz_software(drflac_cache_t x)
2003 {
2004  drflac_uint32 n;
2005  static drflac_uint32 clz_table_4[] = {
2006  0,
2007  4,
2008  3, 3,
2009  2, 2, 2, 2,
2010  1, 1, 1, 1, 1, 1, 1, 1
2011  };
2012 
2013  if (x == 0) {
2014  return sizeof(x)*8;
2015  }
2016 
2017  n = clz_table_4[x >> (sizeof(x)*8 - 4)];
2018  if (n == 0) {
2019 #ifdef DRFLAC_64BIT
2020  if ((x & ((drflac_uint64)0xFFFFFFFF << 32)) == 0) { n = 32; x <<= 32; }
2021  if ((x & ((drflac_uint64)0xFFFF0000 << 32)) == 0) { n += 16; x <<= 16; }
2022  if ((x & ((drflac_uint64)0xFF000000 << 32)) == 0) { n += 8; x <<= 8; }
2023  if ((x & ((drflac_uint64)0xF0000000 << 32)) == 0) { n += 4; x <<= 4; }
2024 #else
2025  if ((x & 0xFFFF0000) == 0) { n = 16; x <<= 16; }
2026  if ((x & 0xFF000000) == 0) { n += 8; x <<= 8; }
2027  if ((x & 0xF0000000) == 0) { n += 4; x <<= 4; }
2028 #endif
2029  n += clz_table_4[x >> (sizeof(x)*8 - 4)];
2030  }
2031 
2032  return n - 1;
2033 }
2034 
2035 #ifdef DRFLAC_IMPLEMENT_CLZ_LZCNT
2036 static DRFLAC_INLINE drflac_bool32 drflac__is_lzcnt_supported()
2037 {
2038  /* If the compiler itself does not support the intrinsic then we'll need to return false. */
2039 #ifdef DRFLAC_HAS_LZCNT_INTRINSIC
2040  return drflac__gIsLZCNTSupported;
2041 #else
2042  return DRFLAC_FALSE;
2043 #endif
2044 }
2045 
2046 static DRFLAC_INLINE drflac_uint32 drflac__clz_lzcnt(drflac_cache_t x)
2047 {
2048 #if defined(_MSC_VER) && !defined(__clang__)
2049  #ifdef DRFLAC_64BIT
2050  return (drflac_uint32)__lzcnt64(x);
2051  #else
2052  return (drflac_uint32)__lzcnt(x);
2053  #endif
2054 #else
2055  #if defined(__GNUC__) || defined(__clang__)
2056  if (x == 0) {
2057  return sizeof(x)*8;
2058  }
2059  #ifdef DRFLAC_64BIT
2060  return (drflac_uint32)__builtin_clzll((drflac_uint64)x);
2061  #else
2062  return (drflac_uint32)__builtin_clzl((drflac_uint32)x);
2063  #endif
2064  #else
2065  /* Unsupported compiler. */
2066  #error "This compiler does not support the lzcnt intrinsic."
2067  #endif
2068 #endif
2069 }
2070 #endif
2071 
2072 #ifdef DRFLAC_IMPLEMENT_CLZ_MSVC
2073 #include <intrin.h> /* For BitScanReverse(). */
2074 
2075 static DRFLAC_INLINE drflac_uint32 drflac__clz_msvc(drflac_cache_t x)
2076 {
2077  drflac_uint32 n;
2078 
2079  if (x == 0) {
2080  return sizeof(x)*8;
2081  }
2082 
2083 #ifdef DRFLAC_64BIT
2084  _BitScanReverse64((unsigned long*)&n, x);
2085 #else
2086  _BitScanReverse((unsigned long*)&n, x);
2087 #endif
2088  return sizeof(x)*8 - n - 1;
2089 }
2090 #endif
2091 
2092 static DRFLAC_INLINE drflac_uint32 drflac__clz(drflac_cache_t x)
2093 {
2094 #ifdef DRFLAC_IMPLEMENT_CLZ_LZCNT
2095  if (drflac__is_lzcnt_supported()) {
2096  return drflac__clz_lzcnt(x);
2097  } else
2098 #endif
2099  {
2100 #ifdef DRFLAC_IMPLEMENT_CLZ_MSVC
2101  return drflac__clz_msvc(x);
2102 #else
2103  return drflac__clz_software(x);
2104 #endif
2105  }
2106 }
2107 
2108 
2109 static DRFLAC_INLINE drflac_bool32 drflac__seek_past_next_set_bit(drflac_bs* bs, unsigned int* pOffsetOut)
2110 {
2111  drflac_uint32 zeroCounter = 0;
2112  drflac_uint32 setBitOffsetPlus1;
2113 
2114  while (bs->cache == 0) {
2115  zeroCounter += (drflac_uint32)DRFLAC_CACHE_L1_BITS_REMAINING(bs);
2116  if (!drflac__reload_cache(bs)) {
2117  return DRFLAC_FALSE;
2118  }
2119  }
2120 
2121  setBitOffsetPlus1 = drflac__clz(bs->cache);
2122  setBitOffsetPlus1 += 1;
2123 
2124  bs->consumedBits += setBitOffsetPlus1;
2125  bs->cache <<= setBitOffsetPlus1;
2126 
2127  *pOffsetOut = zeroCounter + setBitOffsetPlus1 - 1;
2128  return DRFLAC_TRUE;
2129 }
2130 
2131 
2132 
2133 static drflac_bool32 drflac__seek_to_byte(drflac_bs* bs, drflac_uint64 offsetFromStart)
2134 {
2135  drflac_assert(bs != NULL);
2136  drflac_assert(offsetFromStart > 0);
2137 
2138  /*
2139  Seeking from the start is not quite as trivial as it sounds because the onSeek callback takes a signed 32-bit integer (which
2140  is intentional because it simplifies the implementation of the onSeek callbacks), however offsetFromStart is unsigned 64-bit.
2141  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.
2142  */
2143  if (offsetFromStart > 0x7FFFFFFF) {
2144  drflac_uint64 bytesRemaining = offsetFromStart;
2145  if (!bs->onSeek(bs->pUserData, 0x7FFFFFFF, drflac_seek_origin_start)) {
2146  return DRFLAC_FALSE;
2147  }
2148  bytesRemaining -= 0x7FFFFFFF;
2149 
2150  while (bytesRemaining > 0x7FFFFFFF) {
2151  if (!bs->onSeek(bs->pUserData, 0x7FFFFFFF, drflac_seek_origin_current)) {
2152  return DRFLAC_FALSE;
2153  }
2154  bytesRemaining -= 0x7FFFFFFF;
2155  }
2156 
2157  if (bytesRemaining > 0) {
2158  if (!bs->onSeek(bs->pUserData, (int)bytesRemaining, drflac_seek_origin_current)) {
2159  return DRFLAC_FALSE;
2160  }
2161  }
2162  } else {
2163  if (!bs->onSeek(bs->pUserData, (int)offsetFromStart, drflac_seek_origin_start)) {
2164  return DRFLAC_FALSE;
2165  }
2166  }
2167 
2168  /* The cache should be reset to force a reload of fresh data from the client. */
2169  drflac__reset_cache(bs);
2170  return DRFLAC_TRUE;
2171 }
2172 
2173 
2174 static drflac_result drflac__read_utf8_coded_number(drflac_bs* bs, drflac_uint64* pNumberOut, drflac_uint8* pCRCOut)
2175 {
2176  drflac_uint8 crc;
2177  drflac_uint64 result;
2178  unsigned char utf8[7] = {0};
2179  int byteCount;
2180  int i;
2181 
2182  drflac_assert(bs != NULL);
2183  drflac_assert(pNumberOut != NULL);
2184  drflac_assert(pCRCOut != NULL);
2185 
2186  crc = *pCRCOut;
2187 
2188  if (!drflac__read_uint8(bs, 8, utf8)) {
2189  *pNumberOut = 0;
2190  return DRFLAC_END_OF_STREAM;
2191  }
2192  crc = drflac_crc8(crc, utf8[0], 8);
2193 
2194  if ((utf8[0] & 0x80) == 0) {
2195  *pNumberOut = utf8[0];
2196  *pCRCOut = crc;
2197  return DRFLAC_SUCCESS;
2198  }
2199 
2200  byteCount = 1;
2201  if ((utf8[0] & 0xE0) == 0xC0) {
2202  byteCount = 2;
2203  } else if ((utf8[0] & 0xF0) == 0xE0) {
2204  byteCount = 3;
2205  } else if ((utf8[0] & 0xF8) == 0xF0) {
2206  byteCount = 4;
2207  } else if ((utf8[0] & 0xFC) == 0xF8) {
2208  byteCount = 5;
2209  } else if ((utf8[0] & 0xFE) == 0xFC) {
2210  byteCount = 6;
2211  } else if ((utf8[0] & 0xFF) == 0xFE) {
2212  byteCount = 7;
2213  } else {
2214  *pNumberOut = 0;
2215  return DRFLAC_CRC_MISMATCH; /* Bad UTF-8 encoding. */
2216  }
2217 
2218  /* Read extra bytes. */
2219  drflac_assert(byteCount > 1);
2220 
2221  result = (drflac_uint64)(utf8[0] & (0xFF >> (byteCount + 1)));
2222  for (i = 1; i < byteCount; ++i) {
2223  if (!drflac__read_uint8(bs, 8, utf8 + i)) {
2224  *pNumberOut = 0;
2225  return DRFLAC_END_OF_STREAM;
2226  }
2227  crc = drflac_crc8(crc, utf8[i], 8);
2228 
2229  result = (result << 6) | (utf8[i] & 0x3F);
2230  }
2231 
2232  *pNumberOut = result;
2233  *pCRCOut = crc;
2234  return DRFLAC_SUCCESS;
2235 }
2236 
2237 
2238 
2239 /*
2240 The next two functions are responsible for calculating the prediction.
2241 
2242 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
2243 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.
2244 */
2245 static DRFLAC_INLINE drflac_int32 drflac__calculate_prediction_32(drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pDecodedSamples)
2246 {
2247  drflac_int32 prediction = 0;
2248 
2249  drflac_assert(order <= 32);
2250 
2251  /* 32-bit version. */
2252 
2253  /* VC++ optimizes this to a single jmp. I've not yet verified this for other compilers. */
2254  switch (order)
2255  {
2256  case 32: prediction += coefficients[31] * pDecodedSamples[-32];
2257  case 31: prediction += coefficients[30] * pDecodedSamples[-31];
2258  case 30: prediction += coefficients[29] * pDecodedSamples[-30];
2259  case 29: prediction += coefficients[28] * pDecodedSamples[-29];
2260  case 28: prediction += coefficients[27] * pDecodedSamples[-28];
2261  case 27: prediction += coefficients[26] * pDecodedSamples[-27];
2262  case 26: prediction += coefficients[25] * pDecodedSamples[-26];
2263  case 25: prediction += coefficients[24] * pDecodedSamples[-25];
2264  case 24: prediction += coefficients[23] * pDecodedSamples[-24];
2265  case 23: prediction += coefficients[22] * pDecodedSamples[-23];
2266  case 22: prediction += coefficients[21] * pDecodedSamples[-22];
2267  case 21: prediction += coefficients[20] * pDecodedSamples[-21];
2268  case 20: prediction += coefficients[19] * pDecodedSamples[-20];
2269  case 19: prediction += coefficients[18] * pDecodedSamples[-19];
2270  case 18: prediction += coefficients[17] * pDecodedSamples[-18];
2271  case 17: prediction += coefficients[16] * pDecodedSamples[-17];
2272  case 16: prediction += coefficients[15] * pDecodedSamples[-16];
2273  case 15: prediction += coefficients[14] * pDecodedSamples[-15];
2274  case 14: prediction += coefficients[13] * pDecodedSamples[-14];
2275  case 13: prediction += coefficients[12] * pDecodedSamples[-13];
2276  case 12: prediction += coefficients[11] * pDecodedSamples[-12];
2277  case 11: prediction += coefficients[10] * pDecodedSamples[-11];
2278  case 10: prediction += coefficients[ 9] * pDecodedSamples[-10];
2279  case 9: prediction += coefficients[ 8] * pDecodedSamples[- 9];
2280  case 8: prediction += coefficients[ 7] * pDecodedSamples[- 8];
2281  case 7: prediction += coefficients[ 6] * pDecodedSamples[- 7];
2282  case 6: prediction += coefficients[ 5] * pDecodedSamples[- 6];
2283  case 5: prediction += coefficients[ 4] * pDecodedSamples[- 5];
2284  case 4: prediction += coefficients[ 3] * pDecodedSamples[- 4];
2285  case 3: prediction += coefficients[ 2] * pDecodedSamples[- 3];
2286  case 2: prediction += coefficients[ 1] * pDecodedSamples[- 2];
2287  case 1: prediction += coefficients[ 0] * pDecodedSamples[- 1];
2288  }
2289 
2290  return (drflac_int32)(prediction >> shift);
2291 }
2292 
2293 static DRFLAC_INLINE drflac_int32 drflac__calculate_prediction_64(drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pDecodedSamples)
2294 {
2295  drflac_int64 prediction;
2296 
2297  drflac_assert(order <= 32);
2298 
2299  /* 64-bit version. */
2300 
2301  /* This method is faster on the 32-bit build when compiling with VC++. See note below. */
2302 #ifndef DRFLAC_64BIT
2303  if (order == 8)
2304  {
2305  prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1];
2306  prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2];
2307  prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3];
2308  prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4];
2309  prediction += coefficients[4] * (drflac_int64)pDecodedSamples[-5];
2310  prediction += coefficients[5] * (drflac_int64)pDecodedSamples[-6];
2311  prediction += coefficients[6] * (drflac_int64)pDecodedSamples[-7];
2312  prediction += coefficients[7] * (drflac_int64)pDecodedSamples[-8];
2313  }
2314  else if (order == 7)
2315  {
2316  prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1];
2317  prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2];
2318  prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3];
2319  prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4];
2320  prediction += coefficients[4] * (drflac_int64)pDecodedSamples[-5];
2321  prediction += coefficients[5] * (drflac_int64)pDecodedSamples[-6];
2322  prediction += coefficients[6] * (drflac_int64)pDecodedSamples[-7];
2323  }
2324  else if (order == 3)
2325  {
2326  prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1];
2327  prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2];
2328  prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3];
2329  }
2330  else if (order == 6)
2331  {
2332  prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1];
2333  prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2];
2334  prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3];
2335  prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4];
2336  prediction += coefficients[4] * (drflac_int64)pDecodedSamples[-5];
2337  prediction += coefficients[5] * (drflac_int64)pDecodedSamples[-6];
2338  }
2339  else if (order == 5)
2340  {
2341  prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1];
2342  prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2];
2343  prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3];
2344  prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4];
2345  prediction += coefficients[4] * (drflac_int64)pDecodedSamples[-5];
2346  }
2347  else if (order == 4)
2348  {
2349  prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1];
2350  prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2];
2351  prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3];
2352  prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4];
2353  }
2354  else if (order == 12)
2355  {
2356  prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1];
2357  prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2];
2358  prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3];
2359  prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4];
2360  prediction += coefficients[4] * (drflac_int64)pDecodedSamples[-5];
2361  prediction += coefficients[5] * (drflac_int64)pDecodedSamples[-6];
2362  prediction += coefficients[6] * (drflac_int64)pDecodedSamples[-7];
2363  prediction += coefficients[7] * (drflac_int64)pDecodedSamples[-8];
2364  prediction += coefficients[8] * (drflac_int64)pDecodedSamples[-9];
2365  prediction += coefficients[9] * (drflac_int64)pDecodedSamples[-10];
2366  prediction += coefficients[10] * (drflac_int64)pDecodedSamples[-11];
2367  prediction += coefficients[11] * (drflac_int64)pDecodedSamples[-12];
2368  }
2369  else if (order == 2)
2370  {
2371  prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1];
2372  prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2];
2373  }
2374  else if (order == 1)
2375  {
2376  prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1];
2377  }
2378  else if (order == 10)
2379  {
2380  prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1];
2381  prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2];
2382  prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3];
2383  prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4];
2384  prediction += coefficients[4] * (drflac_int64)pDecodedSamples[-5];
2385  prediction += coefficients[5] * (drflac_int64)pDecodedSamples[-6];
2386  prediction += coefficients[6] * (drflac_int64)pDecodedSamples[-7];
2387  prediction += coefficients[7] * (drflac_int64)pDecodedSamples[-8];
2388  prediction += coefficients[8] * (drflac_int64)pDecodedSamples[-9];
2389  prediction += coefficients[9] * (drflac_int64)pDecodedSamples[-10];
2390  }
2391  else if (order == 9)
2392  {
2393  prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1];
2394  prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2];
2395  prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3];
2396  prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4];
2397  prediction += coefficients[4] * (drflac_int64)pDecodedSamples[-5];
2398  prediction += coefficients[5] * (drflac_int64)pDecodedSamples[-6];
2399  prediction += coefficients[6] * (drflac_int64)pDecodedSamples[-7];
2400  prediction += coefficients[7] * (drflac_int64)pDecodedSamples[-8];
2401  prediction += coefficients[8] * (drflac_int64)pDecodedSamples[-9];
2402  }
2403  else if (order == 11)
2404  {
2405  prediction = coefficients[0] * (drflac_int64)pDecodedSamples[-1];
2406  prediction += coefficients[1] * (drflac_int64)pDecodedSamples[-2];
2407  prediction += coefficients[2] * (drflac_int64)pDecodedSamples[-3];
2408  prediction += coefficients[3] * (drflac_int64)pDecodedSamples[-4];
2409  prediction += coefficients[4] * (drflac_int64)pDecodedSamples[-5];
2410  prediction += coefficients[5] * (drflac_int64)pDecodedSamples[-6];
2411  prediction += coefficients[6] * (drflac_int64)pDecodedSamples[-7];
2412  prediction += coefficients[7] * (drflac_int64)pDecodedSamples[-8];
2413  prediction += coefficients[8] * (drflac_int64)pDecodedSamples[-9];
2414  prediction += coefficients[9] * (drflac_int64)pDecodedSamples[-10];
2415  prediction += coefficients[10] * (drflac_int64)pDecodedSamples[-11];
2416  }
2417  else
2418  {
2419  int j;
2420 
2421  prediction = 0;
2422  for (j = 0; j < (int)order; ++j) {
2423  prediction += coefficients[j] * (drflac_int64)pDecodedSamples[-j-1];
2424  }
2425  }
2426 #endif
2427 
2428  /*
2429  VC++ optimizes this to a single jmp instruction, but only the 64-bit build. The 32-bit build generates less efficient code for some
2430  reason. The ugly version above is faster so we'll just switch between the two depending on the target platform.
2431  */
2432 #ifdef DRFLAC_64BIT
2433  prediction = 0;
2434  switch (order)
2435  {
2436  case 32: prediction += coefficients[31] * (drflac_int64)pDecodedSamples[-32];
2437  case 31: prediction += coefficients[30] * (drflac_int64)pDecodedSamples[-31];
2438  case 30: prediction += coefficients[29] * (drflac_int64)pDecodedSamples[-30];
2439  case 29: prediction += coefficients[28] * (drflac_int64)pDecodedSamples[-29];
2440  case 28: prediction += coefficients[27] * (drflac_int64)pDecodedSamples[-28];
2441  case 27: prediction += coefficients[26] * (drflac_int64)pDecodedSamples[-27];
2442  case 26: prediction += coefficients[25] * (drflac_int64)pDecodedSamples[-26];
2443  case 25: prediction += coefficients[24] * (drflac_int64)pDecodedSamples[-25];
2444  case 24: prediction += coefficients[23] * (drflac_int64)pDecodedSamples[-24];
2445  case 23: prediction += coefficients[22] * (drflac_int64)pDecodedSamples[-23];
2446  case 22: prediction += coefficients[21] * (drflac_int64)pDecodedSamples[-22];
2447  case 21: prediction += coefficients[20] * (drflac_int64)pDecodedSamples[-21];
2448  case 20: prediction += coefficients[19] * (drflac_int64)pDecodedSamples[-20];
2449  case 19: prediction += coefficients[18] * (drflac_int64)pDecodedSamples[-19];
2450  case 18: prediction += coefficients[17] * (drflac_int64)pDecodedSamples[-18];
2451  case 17: prediction += coefficients[16] * (drflac_int64)pDecodedSamples[-17];
2452  case 16: prediction += coefficients[15] * (drflac_int64)pDecodedSamples[-16];
2453  case 15: prediction += coefficients[14] * (drflac_int64)pDecodedSamples[-15];
2454  case 14: prediction += coefficients[13] * (drflac_int64)pDecodedSamples[-14];
2455  case 13: prediction += coefficients[12] * (drflac_int64)pDecodedSamples[-13];
2456  case 12: prediction += coefficients[11] * (drflac_int64)pDecodedSamples[-12];
2457  case 11: prediction += coefficients[10] * (drflac_int64)pDecodedSamples[-11];
2458  case 10: prediction += coefficients[ 9] * (drflac_int64)pDecodedSamples[-10];
2459  case 9: prediction += coefficients[ 8] * (drflac_int64)pDecodedSamples[- 9];
2460  case 8: prediction += coefficients[ 7] * (drflac_int64)pDecodedSamples[- 8];
2461  case 7: prediction += coefficients[ 6] * (drflac_int64)pDecodedSamples[- 7];
2462  case 6: prediction += coefficients[ 5] * (drflac_int64)pDecodedSamples[- 6];
2463  case 5: prediction += coefficients[ 4] * (drflac_int64)pDecodedSamples[- 5];
2464  case 4: prediction += coefficients[ 3] * (drflac_int64)pDecodedSamples[- 4];
2465  case 3: prediction += coefficients[ 2] * (drflac_int64)pDecodedSamples[- 3];
2466  case 2: prediction += coefficients[ 1] * (drflac_int64)pDecodedSamples[- 2];
2467  case 1: prediction += coefficients[ 0] * (drflac_int64)pDecodedSamples[- 1];
2468  }
2469 #endif
2470 
2471  return (drflac_int32)(prediction >> shift);
2472 }
2473 
2474 static DRFLAC_INLINE void drflac__calculate_prediction_64_x4(drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, const drflac_uint32 riceParamParts[4], drflac_int32* pDecodedSamples)
2475 {
2476  drflac_int64 prediction0 = 0;
2477  drflac_int64 prediction1 = 0;
2478  drflac_int64 prediction2 = 0;
2479  drflac_int64 prediction3 = 0;
2480 
2481  drflac_assert(order <= 32);
2482 
2483  switch (order)
2484  {
2485  case 32:
2486  prediction0 += coefficients[31] * (drflac_int64)pDecodedSamples[-32];
2487  prediction1 += coefficients[31] * (drflac_int64)pDecodedSamples[-31];
2488  prediction2 += coefficients[31] * (drflac_int64)pDecodedSamples[-30];
2489  prediction3 += coefficients[31] * (drflac_int64)pDecodedSamples[-29];
2490  case 31:
2491  prediction0 += coefficients[30] * (drflac_int64)pDecodedSamples[-31];
2492  prediction1 += coefficients[30] * (drflac_int64)pDecodedSamples[-30];
2493  prediction2 += coefficients[30] * (drflac_int64)pDecodedSamples[-29];
2494  prediction3 += coefficients[30] * (drflac_int64)pDecodedSamples[-28];
2495  case 30:
2496  prediction0 += coefficients[29] * (drflac_int64)pDecodedSamples[-30];
2497  prediction1 += coefficients[29] * (drflac_int64)pDecodedSamples[-29];
2498  prediction2 += coefficients[29] * (drflac_int64)pDecodedSamples[-28];
2499  prediction3 += coefficients[29] * (drflac_int64)pDecodedSamples[-27];
2500  case 29:
2501  prediction0 += coefficients[28] * (drflac_int64)pDecodedSamples[-29];
2502  prediction1 += coefficients[28] * (drflac_int64)pDecodedSamples[-28];
2503  prediction2 += coefficients[28] * (drflac_int64)pDecodedSamples[-27];
2504  prediction3 += coefficients[28] * (drflac_int64)pDecodedSamples[-26];
2505  case 28:
2506  prediction0 += coefficients[27] * (drflac_int64)pDecodedSamples[-28];
2507  prediction1 += coefficients[27] * (drflac_int64)pDecodedSamples[-27];
2508  prediction2 += coefficients[27] * (drflac_int64)pDecodedSamples[-26];
2509  prediction3 += coefficients[27] * (drflac_int64)pDecodedSamples[-25];
2510  case 27:
2511  prediction0 += coefficients[26] * (drflac_int64)pDecodedSamples[-27];
2512  prediction1 += coefficients[26] * (drflac_int64)pDecodedSamples[-26];
2513  prediction2 += coefficients[26] * (drflac_int64)pDecodedSamples[-25];
2514  prediction3 += coefficients[26] * (drflac_int64)pDecodedSamples[-24];
2515  case 26:
2516  prediction0 += coefficients[25] * (drflac_int64)pDecodedSamples[-26];
2517  prediction1 += coefficients[25] * (drflac_int64)pDecodedSamples[-25];
2518  prediction2 += coefficients[25] * (drflac_int64)pDecodedSamples[-24];
2519  prediction3 += coefficients[25] * (drflac_int64)pDecodedSamples[-23];
2520  case 25:
2521  prediction0 += coefficients[24] * (drflac_int64)pDecodedSamples[-25];
2522  prediction1 += coefficients[24] * (drflac_int64)pDecodedSamples[-24];
2523  prediction2 += coefficients[24] * (drflac_int64)pDecodedSamples[-23];
2524  prediction3 += coefficients[24] * (drflac_int64)pDecodedSamples[-22];
2525  case 24:
2526  prediction0 += coefficients[23] * (drflac_int64)pDecodedSamples[-24];
2527  prediction1 += coefficients[23] * (drflac_int64)pDecodedSamples[-23];
2528  prediction2 += coefficients[23] * (drflac_int64)pDecodedSamples[-22];
2529  prediction3 += coefficients[23] * (drflac_int64)pDecodedSamples[-21];
2530  case 23:
2531  prediction0 += coefficients[22] * (drflac_int64)pDecodedSamples[-23];
2532  prediction1 += coefficients[22] * (drflac_int64)pDecodedSamples[-22];
2533  prediction2 += coefficients[22] * (drflac_int64)pDecodedSamples[-21];
2534  prediction3 += coefficients[22] * (drflac_int64)pDecodedSamples[-20];
2535  case 22:
2536  prediction0 += coefficients[21] * (drflac_int64)pDecodedSamples[-22];
2537  prediction1 += coefficients[21] * (drflac_int64)pDecodedSamples[-21];
2538  prediction2 += coefficients[21] * (drflac_int64)pDecodedSamples[-20];
2539  prediction3 += coefficients[21] * (drflac_int64)pDecodedSamples[-19];
2540  case 21:
2541  prediction0 += coefficients[20] * (drflac_int64)pDecodedSamples[-21];
2542  prediction1 += coefficients[20] * (drflac_int64)pDecodedSamples[-20];
2543  prediction2 += coefficients[20] * (drflac_int64)pDecodedSamples[-19];
2544  prediction3 += coefficients[20] * (drflac_int64)pDecodedSamples[-18];
2545  case 20:
2546  prediction0 += coefficients[19] * (drflac_int64)pDecodedSamples[-20];
2547  prediction1 += coefficients[19] * (drflac_int64)pDecodedSamples[-19];
2548  prediction2 += coefficients[19] * (drflac_int64)pDecodedSamples[-18];
2549  prediction3 += coefficients[19] * (drflac_int64)pDecodedSamples[-17];
2550  case 19:
2551  prediction0 += coefficients[18] * (drflac_int64)pDecodedSamples[-19];
2552  prediction1 += coefficients[18] * (drflac_int64)pDecodedSamples[-18];
2553  prediction2 += coefficients[18] * (drflac_int64)pDecodedSamples[-17];
2554  prediction3 += coefficients[18] * (drflac_int64)pDecodedSamples[-16];
2555  case 18:
2556  prediction0 += coefficients[17] * (drflac_int64)pDecodedSamples[-18];
2557  prediction1 += coefficients[17] * (drflac_int64)pDecodedSamples[-17];
2558  prediction2 += coefficients[17] * (drflac_int64)pDecodedSamples[-16];
2559  prediction3 += coefficients[17] * (drflac_int64)pDecodedSamples[-15];
2560  case 17:
2561  prediction0 += coefficients[16] * (drflac_int64)pDecodedSamples[-17];
2562  prediction1 += coefficients[16] * (drflac_int64)pDecodedSamples[-16];
2563  prediction2 += coefficients[16] * (drflac_int64)pDecodedSamples[-15];
2564  prediction3 += coefficients[16] * (drflac_int64)pDecodedSamples[-14];
2565 
2566  case 16:
2567  prediction0 += coefficients[15] * (drflac_int64)pDecodedSamples[-16];
2568  prediction1 += coefficients[15] * (drflac_int64)pDecodedSamples[-15];
2569  prediction2 += coefficients[15] * (drflac_int64)pDecodedSamples[-14];
2570  prediction3 += coefficients[15] * (drflac_int64)pDecodedSamples[-13];
2571  case 15:
2572  prediction0 += coefficients[14] * (drflac_int64)pDecodedSamples[-15];
2573  prediction1 += coefficients[14] * (drflac_int64)pDecodedSamples[-14];
2574  prediction2 += coefficients[14] * (drflac_int64)pDecodedSamples[-13];
2575  prediction3 += coefficients[14] * (drflac_int64)pDecodedSamples[-12];
2576  case 14:
2577  prediction0 += coefficients[13] * (drflac_int64)pDecodedSamples[-14];
2578  prediction1 += coefficients[13] * (drflac_int64)pDecodedSamples[-13];
2579  prediction2 += coefficients[13] * (drflac_int64)pDecodedSamples[-12];
2580  prediction3 += coefficients[13] * (drflac_int64)pDecodedSamples[-11];
2581  case 13:
2582  prediction0 += coefficients[12] * (drflac_int64)pDecodedSamples[-13];
2583  prediction1 += coefficients[12] * (drflac_int64)pDecodedSamples[-12];
2584  prediction2 += coefficients[12] * (drflac_int64)pDecodedSamples[-11];
2585  prediction3 += coefficients[12] * (drflac_int64)pDecodedSamples[-10];
2586  case 12:
2587  prediction0 += coefficients[11] * (drflac_int64)pDecodedSamples[-12];
2588  prediction1 += coefficients[11] * (drflac_int64)pDecodedSamples[-11];
2589  prediction2 += coefficients[11] * (drflac_int64)pDecodedSamples[-10];
2590  prediction3 += coefficients[11] * (drflac_int64)pDecodedSamples[- 9];
2591  case 11:
2592  prediction0 += coefficients[10] * (drflac_int64)pDecodedSamples[-11];
2593  prediction1 += coefficients[10] * (drflac_int64)pDecodedSamples[-10];
2594  prediction2 += coefficients[10] * (drflac_int64)pDecodedSamples[- 9];
2595  prediction3 += coefficients[10] * (drflac_int64)pDecodedSamples[- 8];
2596  case 10:
2597  prediction0 += coefficients[9] * (drflac_int64)pDecodedSamples[-10];
2598  prediction1 += coefficients[9] * (drflac_int64)pDecodedSamples[- 9];
2599  prediction2 += coefficients[9] * (drflac_int64)pDecodedSamples[- 8];
2600  prediction3 += coefficients[9] * (drflac_int64)pDecodedSamples[- 7];
2601  case 9:
2602  prediction0 += coefficients[8] * (drflac_int64)pDecodedSamples[- 9];
2603  prediction1 += coefficients[8] * (drflac_int64)pDecodedSamples[- 8];
2604  prediction2 += coefficients[8] * (drflac_int64)pDecodedSamples[- 7];
2605  prediction3 += coefficients[8] * (drflac_int64)pDecodedSamples[- 6];
2606  case 8:
2607  prediction0 += coefficients[7] * (drflac_int64)pDecodedSamples[- 8];
2608  prediction1 += coefficients[7] * (drflac_int64)pDecodedSamples[- 7];
2609  prediction2 += coefficients[7] * (drflac_int64)pDecodedSamples[- 6];
2610  prediction3 += coefficients[7] * (drflac_int64)pDecodedSamples[- 5];
2611  case 7:
2612  prediction0 += coefficients[6] * (drflac_int64)pDecodedSamples[- 7];
2613  prediction1 += coefficients[6] * (drflac_int64)pDecodedSamples[- 6];
2614  prediction2 += coefficients[6] * (drflac_int64)pDecodedSamples[- 5];
2615  prediction3 += coefficients[6] * (drflac_int64)pDecodedSamples[- 4];
2616  case 6:
2617  prediction0 += coefficients[5] * (drflac_int64)pDecodedSamples[- 6];
2618  prediction1 += coefficients[5] * (drflac_int64)pDecodedSamples[- 5];
2619  prediction2 += coefficients[5] * (drflac_int64)pDecodedSamples[- 4];
2620  prediction3 += coefficients[5] * (drflac_int64)pDecodedSamples[- 3];
2621  case 5:
2622  prediction0 += coefficients[4] * (drflac_int64)pDecodedSamples[- 5];
2623  prediction1 += coefficients[4] * (drflac_int64)pDecodedSamples[- 4];
2624  prediction2 += coefficients[4] * (drflac_int64)pDecodedSamples[- 3];
2625  prediction3 += coefficients[4] * (drflac_int64)pDecodedSamples[- 2];
2626  case 4:
2627  prediction0 += coefficients[3] * (drflac_int64)pDecodedSamples[- 4];
2628  prediction1 += coefficients[3] * (drflac_int64)pDecodedSamples[- 3];
2629  prediction2 += coefficients[3] * (drflac_int64)pDecodedSamples[- 2];
2630  prediction3 += coefficients[3] * (drflac_int64)pDecodedSamples[- 1];
2631  order = 3;
2632  }
2633 
2634  switch (order)
2635  {
2636  case 3: prediction0 += coefficients[ 2] * (drflac_int64)pDecodedSamples[- 3];
2637  case 2: prediction0 += coefficients[ 1] * (drflac_int64)pDecodedSamples[- 2];
2638  case 1: prediction0 += coefficients[ 0] * (drflac_int64)pDecodedSamples[- 1];
2639  }
2640  pDecodedSamples[0] = riceParamParts[0] + (drflac_int32)(prediction0 >> shift);
2641 
2642  switch (order)
2643  {
2644  case 3: prediction1 += coefficients[ 2] * (drflac_int64)pDecodedSamples[- 2];
2645  case 2: prediction1 += coefficients[ 1] * (drflac_int64)pDecodedSamples[- 1];
2646  case 1: prediction1 += coefficients[ 0] * (drflac_int64)pDecodedSamples[ 0];
2647  }
2648  pDecodedSamples[1] = riceParamParts[1] + (drflac_int32)(prediction1 >> shift);
2649 
2650  switch (order)
2651  {
2652  case 3: prediction2 += coefficients[ 2] * (drflac_int64)pDecodedSamples[- 1];
2653  case 2: prediction2 += coefficients[ 1] * (drflac_int64)pDecodedSamples[ 0];
2654  case 1: prediction2 += coefficients[ 0] * (drflac_int64)pDecodedSamples[ 1];
2655  }
2656  pDecodedSamples[2] = riceParamParts[2] + (drflac_int32)(prediction2 >> shift);
2657 
2658  switch (order)
2659  {
2660  case 3: prediction3 += coefficients[ 2] * (drflac_int64)pDecodedSamples[ 0];
2661  case 2: prediction3 += coefficients[ 1] * (drflac_int64)pDecodedSamples[ 1];
2662  case 1: prediction3 += coefficients[ 0] * (drflac_int64)pDecodedSamples[ 2];
2663  }
2664  pDecodedSamples[3] = riceParamParts[3] + (drflac_int32)(prediction3 >> shift);
2665 }
2666 
2667 #if defined(DRFLAC_SUPPORT_SSE41)
2668 static DRFLAC_INLINE drflac_int32 drflac__calculate_prediction_64__sse41(drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pDecodedSamples)
2669 {
2670  __m128i prediction = _mm_setzero_si128();
2671 
2672  drflac_assert(order <= 32);
2673 
2674  switch (order)
2675  {
2676  case 32:
2677  case 31: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[31], 0, coefficients[30]), _mm_set_epi32(0, pDecodedSamples[-32], 0, pDecodedSamples[-31])));
2678  case 30:
2679  case 29: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[29], 0, coefficients[28]), _mm_set_epi32(0, pDecodedSamples[-30], 0, pDecodedSamples[-29])));
2680  case 28:
2681  case 27: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[27], 0, coefficients[26]), _mm_set_epi32(0, pDecodedSamples[-28], 0, pDecodedSamples[-27])));
2682  case 26:
2683  case 25: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[25], 0, coefficients[24]), _mm_set_epi32(0, pDecodedSamples[-26], 0, pDecodedSamples[-25])));
2684  case 24:
2685  case 23: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[23], 0, coefficients[22]), _mm_set_epi32(0, pDecodedSamples[-24], 0, pDecodedSamples[-23])));
2686  case 22:
2687  case 21: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[21], 0, coefficients[20]), _mm_set_epi32(0, pDecodedSamples[-22], 0, pDecodedSamples[-21])));
2688  case 20:
2689  case 19: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[19], 0, coefficients[18]), _mm_set_epi32(0, pDecodedSamples[-20], 0, pDecodedSamples[-19])));
2690  case 18:
2691  case 17: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[17], 0, coefficients[16]), _mm_set_epi32(0, pDecodedSamples[-18], 0, pDecodedSamples[-17])));
2692  case 16:
2693  case 15: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[15], 0, coefficients[14]), _mm_set_epi32(0, pDecodedSamples[-16], 0, pDecodedSamples[-15])));
2694  case 14:
2695  case 13: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[13], 0, coefficients[12]), _mm_set_epi32(0, pDecodedSamples[-14], 0, pDecodedSamples[-13])));
2696  case 12:
2697  case 11: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[11], 0, coefficients[10]), _mm_set_epi32(0, pDecodedSamples[-12], 0, pDecodedSamples[-11])));
2698  case 10:
2699  case 9: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 9], 0, coefficients[ 8]), _mm_set_epi32(0, pDecodedSamples[-10], 0, pDecodedSamples[- 9])));
2700  case 8:
2701  case 7: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 7], 0, coefficients[ 6]), _mm_set_epi32(0, pDecodedSamples[- 8], 0, pDecodedSamples[- 7])));
2702  case 6:
2703  case 5: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 5], 0, coefficients[ 4]), _mm_set_epi32(0, pDecodedSamples[- 6], 0, pDecodedSamples[- 5])));
2704  case 4:
2705  case 3: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 3], 0, coefficients[ 2]), _mm_set_epi32(0, pDecodedSamples[- 4], 0, pDecodedSamples[- 3])));
2706  case 2:
2707  case 1: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 1], 0, coefficients[ 0]), _mm_set_epi32(0, pDecodedSamples[- 2], 0, pDecodedSamples[- 1])));
2708  }
2709 
2710  return (drflac_int32)((
2711  ((drflac_uint64*)&prediction)[0] +
2712  ((drflac_uint64*)&prediction)[1]) >> shift);
2713 }
2714 
2715 static DRFLAC_INLINE void drflac__calculate_prediction_64_x2__sse41(drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, const drflac_uint32 riceParamParts[4], drflac_int32* pDecodedSamples)
2716 {
2717  __m128i prediction = _mm_setzero_si128();
2718  drflac_int64 predictions[2] = {0, 0};
2719 
2720  drflac_assert(order <= 32);
2721 
2722  switch (order)
2723  {
2724  case 32: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[31], 0, coefficients[31]), _mm_set_epi32(0, pDecodedSamples[-31], 0, pDecodedSamples[-32])));
2725  case 31: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[30], 0, coefficients[30]), _mm_set_epi32(0, pDecodedSamples[-30], 0, pDecodedSamples[-31])));
2726  case 30: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[29], 0, coefficients[29]), _mm_set_epi32(0, pDecodedSamples[-29], 0, pDecodedSamples[-30])));
2727  case 29: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[28], 0, coefficients[28]), _mm_set_epi32(0, pDecodedSamples[-28], 0, pDecodedSamples[-29])));
2728  case 28: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[27], 0, coefficients[27]), _mm_set_epi32(0, pDecodedSamples[-27], 0, pDecodedSamples[-28])));
2729  case 27: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[26], 0, coefficients[26]), _mm_set_epi32(0, pDecodedSamples[-26], 0, pDecodedSamples[-27])));
2730  case 26: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[25], 0, coefficients[25]), _mm_set_epi32(0, pDecodedSamples[-25], 0, pDecodedSamples[-26])));
2731  case 25: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[24], 0, coefficients[24]), _mm_set_epi32(0, pDecodedSamples[-24], 0, pDecodedSamples[-25])));
2732  case 24: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[23], 0, coefficients[23]), _mm_set_epi32(0, pDecodedSamples[-23], 0, pDecodedSamples[-24])));
2733  case 23: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[22], 0, coefficients[22]), _mm_set_epi32(0, pDecodedSamples[-22], 0, pDecodedSamples[-23])));
2734  case 22: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[21], 0, coefficients[21]), _mm_set_epi32(0, pDecodedSamples[-21], 0, pDecodedSamples[-22])));
2735  case 21: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[20], 0, coefficients[20]), _mm_set_epi32(0, pDecodedSamples[-20], 0, pDecodedSamples[-21])));
2736  case 20: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[19], 0, coefficients[19]), _mm_set_epi32(0, pDecodedSamples[-19], 0, pDecodedSamples[-20])));
2737  case 19: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[18], 0, coefficients[18]), _mm_set_epi32(0, pDecodedSamples[-18], 0, pDecodedSamples[-19])));
2738  case 18: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[17], 0, coefficients[17]), _mm_set_epi32(0, pDecodedSamples[-17], 0, pDecodedSamples[-18])));
2739  case 17: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[16], 0, coefficients[16]), _mm_set_epi32(0, pDecodedSamples[-16], 0, pDecodedSamples[-17])));
2740  case 16: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[15], 0, coefficients[15]), _mm_set_epi32(0, pDecodedSamples[-15], 0, pDecodedSamples[-16])));
2741  case 15: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[14], 0, coefficients[14]), _mm_set_epi32(0, pDecodedSamples[-14], 0, pDecodedSamples[-15])));
2742  case 14: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[13], 0, coefficients[13]), _mm_set_epi32(0, pDecodedSamples[-13], 0, pDecodedSamples[-14])));
2743  case 13: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[12], 0, coefficients[12]), _mm_set_epi32(0, pDecodedSamples[-12], 0, pDecodedSamples[-13])));
2744  case 12: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[11], 0, coefficients[11]), _mm_set_epi32(0, pDecodedSamples[-11], 0, pDecodedSamples[-12])));
2745  case 11: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[10], 0, coefficients[10]), _mm_set_epi32(0, pDecodedSamples[-10], 0, pDecodedSamples[-11])));
2746  case 10: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 9], 0, coefficients[ 9]), _mm_set_epi32(0, pDecodedSamples[- 9], 0, pDecodedSamples[-10])));
2747  case 9: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 8], 0, coefficients[ 8]), _mm_set_epi32(0, pDecodedSamples[- 8], 0, pDecodedSamples[- 9])));
2748  case 8: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 7], 0, coefficients[ 7]), _mm_set_epi32(0, pDecodedSamples[- 7], 0, pDecodedSamples[- 8])));
2749  case 7: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 6], 0, coefficients[ 6]), _mm_set_epi32(0, pDecodedSamples[- 6], 0, pDecodedSamples[- 7])));
2750  case 6: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 5], 0, coefficients[ 5]), _mm_set_epi32(0, pDecodedSamples[- 5], 0, pDecodedSamples[- 6])));
2751  case 5: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 4], 0, coefficients[ 4]), _mm_set_epi32(0, pDecodedSamples[- 4], 0, pDecodedSamples[- 5])));
2752  case 4: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 3], 0, coefficients[ 3]), _mm_set_epi32(0, pDecodedSamples[- 3], 0, pDecodedSamples[- 4])));
2753  case 3: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 2], 0, coefficients[ 2]), _mm_set_epi32(0, pDecodedSamples[- 2], 0, pDecodedSamples[- 3])));
2754  case 2: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 1], 0, coefficients[ 1]), _mm_set_epi32(0, pDecodedSamples[- 1], 0, pDecodedSamples[- 2])));
2755  order = 1;
2756  }
2757 
2758  _mm_storeu_si128((__m128i*)predictions, prediction);
2759 
2760  switch (order)
2761  {
2762  case 1: predictions[0] += coefficients[ 0] * (drflac_int64)pDecodedSamples[- 1];
2763  }
2764  pDecodedSamples[0] = riceParamParts[0] + (drflac_int32)(predictions[0] >> shift);
2765 
2766  switch (order)
2767  {
2768  case 1: predictions[1] += coefficients[ 0] * (drflac_int64)pDecodedSamples[ 0];
2769  }
2770  pDecodedSamples[1] = riceParamParts[1] + (drflac_int32)(predictions[1] >> shift);
2771 }
2772 
2773 
2774 static DRFLAC_INLINE __m128i drflac__mm_not_si128(__m128i a)
2775 {
2776  return _mm_xor_si128(a, _mm_cmpeq_epi32(_mm_setzero_si128(), _mm_setzero_si128()));
2777 }
2778 
2779 static DRFLAC_INLINE __m128i drflac__mm_slide1_epi32(__m128i a, __m128i b)
2780 {
2781  /* a3a2a1a0/b3b2b1b0 -> a2a1a0b3 */
2782 
2783  /* Result = a2a1a0b3 */
2784  __m128i b3a3b2a2 = _mm_unpackhi_epi32(a, b);
2785  __m128i a2b3a2b3 = _mm_shuffle_epi32(b3a3b2a2, _MM_SHUFFLE(0, 3, 0, 3));
2786  __m128i a1a2a0b3 = _mm_unpacklo_epi32(a2b3a2b3, a);
2787  __m128i a2a1a0b3 = _mm_shuffle_epi32(a1a2a0b3, _MM_SHUFFLE(2, 3, 1, 0));
2788  return a2a1a0b3;
2789 }
2790 
2791 static DRFLAC_INLINE __m128i drflac__mm_slide2_epi32(__m128i a, __m128i b)
2792 {
2793  /* Result = a1a0b3b2 */
2794  __m128i b1b0b3b2 = _mm_shuffle_epi32(b, _MM_SHUFFLE(1, 0, 3, 2));
2795  __m128i a1b3a0b2 = _mm_unpacklo_epi32(b1b0b3b2, a);
2796  __m128i a1a0b3b2 = _mm_shuffle_epi32(a1b3a0b2, _MM_SHUFFLE(3, 1, 2, 0));
2797  return a1a0b3b2;
2798 }
2799 
2800 static DRFLAC_INLINE __m128i drflac__mm_slide3_epi32(__m128i a, __m128i b)
2801 {
2802  /* Result = a0b3b2b1 */
2803  __m128i b1a1b0a0 = _mm_unpacklo_epi32(a, b);
2804  __m128i a0b1a0b1 = _mm_shuffle_epi32(b1a1b0a0, _MM_SHUFFLE(0, 3, 0, 3));
2805  __m128i b3a0b2b1 = _mm_unpackhi_epi32(a0b1a0b1, b);
2806  __m128i a0b3b2b1 = _mm_shuffle_epi32(b3a0b2b1, _MM_SHUFFLE(2, 3, 1, 0));
2807  return a0b3b2b1;
2808 }
2809 
2810 static DRFLAC_INLINE void drflac__calculate_prediction_32_x4__sse41(drflac_uint32 order, drflac_int32 shift, const __m128i* coefficients128, const __m128i riceParamParts128, drflac_int32* pDecodedSamples)
2811 {
2812  drflac_assert(order <= 32);
2813 
2814  /* I don't think this is as efficient as it could be. More work needs to be done on this. */
2815  if (order > 0) {
2816  drflac_int32 predictions[4];
2817  drflac_uint32 riceParamParts[4];
2818 
2819  __m128i s_09_10_11_12 = _mm_loadu_si128((const __m128i*)(pDecodedSamples - 12));
2820  __m128i s_05_06_07_08 = _mm_loadu_si128((const __m128i*)(pDecodedSamples - 8));
2821  __m128i s_01_02_03_04 = _mm_loadu_si128((const __m128i*)(pDecodedSamples - 4));
2822 
2823  __m128i prediction = _mm_setzero_si128();
2824 
2825  /*
2826  The idea with this switch is to do do a single jump based on the value of "order". In my test library, "order" is never larger than 12, so
2827  I have decided to do a less optimal, but simpler solution in the order > 12 case.
2828  */
2829  switch (order)
2830  {
2831  case 32: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[31], _mm_loadu_si128((const __m128i*)(pDecodedSamples - 32))));
2832  case 31: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[30], _mm_loadu_si128((const __m128i*)(pDecodedSamples - 31))));
2833  case 30: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[29], _mm_loadu_si128((const __m128i*)(pDecodedSamples - 30))));
2834  case 29: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[28], _mm_loadu_si128((const __m128i*)(pDecodedSamples - 29))));
2835  case 28: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[27], _mm_loadu_si128((const __m128i*)(pDecodedSamples - 28))));
2836  case 27: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[26], _mm_loadu_si128((const __m128i*)(pDecodedSamples - 27))));
2837  case 26: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[25], _mm_loadu_si128((const __m128i*)(pDecodedSamples - 26))));
2838  case 25: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[24], _mm_loadu_si128((const __m128i*)(pDecodedSamples - 25))));
2839  case 24: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[23], _mm_loadu_si128((const __m128i*)(pDecodedSamples - 24))));
2840  case 23: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[22], _mm_loadu_si128((const __m128i*)(pDecodedSamples - 23))));
2841  case 22: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[21], _mm_loadu_si128((const __m128i*)(pDecodedSamples - 22))));
2842  case 21: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[20], _mm_loadu_si128((const __m128i*)(pDecodedSamples - 21))));
2843  case 20: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[19], _mm_loadu_si128((const __m128i*)(pDecodedSamples - 20))));
2844  case 19: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[18], _mm_loadu_si128((const __m128i*)(pDecodedSamples - 19))));
2845  case 18: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[17], _mm_loadu_si128((const __m128i*)(pDecodedSamples - 18))));
2846  case 17: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[16], _mm_loadu_si128((const __m128i*)(pDecodedSamples - 17))));
2847  case 16: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[15], _mm_loadu_si128((const __m128i*)(pDecodedSamples - 16))));
2848  case 15: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[14], _mm_loadu_si128((const __m128i*)(pDecodedSamples - 15))));
2849  case 14: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[13], _mm_loadu_si128((const __m128i*)(pDecodedSamples - 14))));
2850  case 13: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[12], _mm_loadu_si128((const __m128i*)(pDecodedSamples - 13))));
2851 
2852  case 12: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[11], s_09_10_11_12));
2853  case 11: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[10], drflac__mm_slide3_epi32(s_05_06_07_08, s_09_10_11_12)));
2854  case 10: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[ 9], drflac__mm_slide2_epi32(s_05_06_07_08, s_09_10_11_12)));
2855  case 9: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[ 8], drflac__mm_slide1_epi32(s_05_06_07_08, s_09_10_11_12)));
2856  case 8: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[ 7], s_05_06_07_08));
2857  case 7: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[ 6], drflac__mm_slide3_epi32(s_01_02_03_04, s_05_06_07_08)));
2858  case 6: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[ 5], drflac__mm_slide2_epi32(s_01_02_03_04, s_05_06_07_08)));
2859  case 5: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[ 4], drflac__mm_slide1_epi32(s_01_02_03_04, s_05_06_07_08)));
2860  case 4: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[ 3], s_01_02_03_04)); order = 3; /* <-- Don't forget to set order to 3 here! */
2861  case 3: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[ 2], drflac__mm_slide3_epi32(_mm_setzero_si128(), s_01_02_03_04)));
2862  case 2: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[ 1], drflac__mm_slide2_epi32(_mm_setzero_si128(), s_01_02_03_04)));
2863  case 1: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[ 0], drflac__mm_slide1_epi32(_mm_setzero_si128(), s_01_02_03_04)));
2864  }
2865 
2866  _mm_storeu_si128((__m128i*)predictions, prediction);
2867  _mm_storeu_si128((__m128i*)riceParamParts, riceParamParts128);
2868 
2869  predictions[0] = riceParamParts[0] + (predictions[0] >> shift);
2870 
2871  switch (order)
2872  {
2873  case 3: predictions[3] += ((const drflac_int32*)&coefficients128[ 2])[0] * predictions[ 0];
2874  case 2: predictions[2] += ((const drflac_int32*)&coefficients128[ 1])[0] * predictions[ 0];
2875  case 1: predictions[1] += ((const drflac_int32*)&coefficients128[ 0])[0] * predictions[ 0];
2876  }
2877  predictions[1] = riceParamParts[1] + (predictions[1] >> shift);
2878 
2879  switch (order)
2880  {
2881  case 3:
2882  case 2: predictions[3] += ((const drflac_int32*)&coefficients128[ 1])[0] * predictions[ 1];
2883  case 1: predictions[2] += ((const drflac_int32*)&coefficients128[ 0])[0] * predictions[ 1];
2884  }
2885  predictions[2] = riceParamParts[2] + (predictions[2] >> shift);
2886 
2887  switch (order)
2888  {
2889  case 3:
2890  case 2:
2891  case 1: predictions[3] += ((const drflac_int32*)&coefficients128[ 0])[0] * predictions[ 2];
2892  }
2893  predictions[3] = riceParamParts[3] + (predictions[3] >> shift);
2894 
2895  pDecodedSamples[0] = predictions[0];
2896  pDecodedSamples[1] = predictions[1];
2897  pDecodedSamples[2] = predictions[2];
2898  pDecodedSamples[3] = predictions[3];
2899  } else {
2900  _mm_storeu_si128((__m128i*)pDecodedSamples, riceParamParts128);
2901  }
2902 }
2903 #endif
2904 
2905 #if 0
2906 /*
2907 Reference implementation for reading and decoding samples with residual. This is intentionally left unoptimized for the
2908 sake of readability and should only be used as a reference.
2909 */
2910 static drflac_bool32 drflac__decode_samples_with_residual__rice__reference(drflac_bs* bs, drflac_uint32 bitsPerSample, drflac_uint32 count, drflac_uint8 riceParam, drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pSamplesOut)
2911 {
2912  drflac_uint32 i;
2913 
2914  drflac_assert(bs != NULL);
2915  drflac_assert(count > 0);
2916  drflac_assert(pSamplesOut != NULL);
2917 
2918  for (i = 0; i < count; ++i) {
2919  drflac_uint32 zeroCounter = 0;
2920  for (;;) {
2921  drflac_uint8 bit;
2922  if (!drflac__read_uint8(bs, 1, &bit)) {
2923  return DRFLAC_FALSE;
2924  }
2925 
2926  if (bit == 0) {
2927  zeroCounter += 1;
2928  } else {
2929  break;
2930  }
2931  }
2932 
2933  drflac_uint32 decodedRice;
2934  if (riceParam > 0) {
2935  if (!drflac__read_uint32(bs, riceParam, &decodedRice)) {
2936  return DRFLAC_FALSE;
2937  }
2938  } else {
2939  decodedRice = 0;
2940  }
2941 
2942  decodedRice |= (zeroCounter << riceParam);
2943  if ((decodedRice & 0x01)) {
2944  decodedRice = ~(decodedRice >> 1);
2945  } else {
2946  decodedRice = (decodedRice >> 1);
2947  }
2948 
2949 
2950  if (bitsPerSample > 16) {
2951  pSamplesOut[i] = decodedRice + drflac__calculate_prediction_64(order, shift, coefficients, pSamplesOut + i);
2952  } else {
2953  pSamplesOut[i] = decodedRice + drflac__calculate_prediction_32(order, shift, coefficients, pSamplesOut + i);
2954  }
2955  }
2956 
2957  return DRFLAC_TRUE;
2958 }
2959 #endif
2960 
2961 #if 0
2962 static drflac_bool32 drflac__read_rice_parts__reference(drflac_bs* bs, drflac_uint8 riceParam, drflac_uint32* pZeroCounterOut, drflac_uint32* pRiceParamPartOut)
2963 {
2964  drflac_uint32 zeroCounter = 0;
2965  drflac_uint32 decodedRice;
2966 
2967  for (;;) {
2968  drflac_uint8 bit;
2969  if (!drflac__read_uint8(bs, 1, &bit)) {
2970  return DRFLAC_FALSE;
2971  }
2972 
2973  if (bit == 0) {
2974  zeroCounter += 1;
2975  } else {
2976  break;
2977  }
2978  }
2979 
2980  if (riceParam > 0) {
2981  if (!drflac__read_uint32(bs, riceParam, &decodedRice)) {
2982  return DRFLAC_FALSE;
2983  }
2984  } else {
2985  decodedRice = 0;
2986  }
2987 
2988  *pZeroCounterOut = zeroCounter;
2989  *pRiceParamPartOut = decodedRice;
2990  return DRFLAC_TRUE;
2991 }
2992 #endif
2993 
2994 #if 0
2995 static DRFLAC_INLINE drflac_bool32 drflac__read_rice_parts(drflac_bs* bs, drflac_uint8 riceParam, drflac_uint32* pZeroCounterOut, drflac_uint32* pRiceParamPartOut)
2996 {
2997  drflac_cache_t riceParamMask;
2998  drflac_uint32 zeroCounter;
2999  drflac_uint32 setBitOffsetPlus1;
3000  drflac_uint32 riceParamPart;
3001  drflac_uint32 riceLength;
3002 
3003  drflac_assert(riceParam > 0); /* <-- riceParam should never be 0. drflac__read_rice_parts__param_equals_zero() should be used instead for this case. */
3004 
3005  riceParamMask = DRFLAC_CACHE_L1_SELECTION_MASK(riceParam);
3006 
3007  zeroCounter = 0;
3008  while (bs->cache == 0) {
3009  zeroCounter += (drflac_uint32)DRFLAC_CACHE_L1_BITS_REMAINING(bs);
3010  if (!drflac__reload_cache(bs)) {
3011  return DRFLAC_FALSE;
3012  }
3013  }
3014 
3015  setBitOffsetPlus1 = drflac__clz(bs->cache);
3016  zeroCounter += setBitOffsetPlus1;
3017  setBitOffsetPlus1 += 1;
3018 
3019  riceLength = setBitOffsetPlus1 + riceParam;
3020  if (riceLength < DRFLAC_CACHE_L1_BITS_REMAINING(bs)) {
3021  riceParamPart = (drflac_uint32)((bs->cache & (riceParamMask >> setBitOffsetPlus1)) >> DRFLAC_CACHE_L1_SELECTION_SHIFT(bs, riceLength));
3022 
3023  bs->consumedBits += riceLength;
3024  bs->cache <<= riceLength;
3025  } else {
3026  drflac_uint32 bitCountLo;
3027  drflac_cache_t resultHi;
3028 
3029  bs->consumedBits += riceLength;
3030  bs->cache <<= setBitOffsetPlus1 & (DRFLAC_CACHE_L1_SIZE_BITS(bs)-1); /* <-- Equivalent to "if (setBitOffsetPlus1 < DRFLAC_CACHE_L1_SIZE_BITS(bs)) { bs->cache <<= setBitOffsetPlus1; }" */
3031 
3032  /* 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. */
3033  bitCountLo = bs->consumedBits - DRFLAC_CACHE_L1_SIZE_BITS(bs);
3034  resultHi = DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, riceParam); /* <-- Use DRFLAC_CACHE_L1_SELECT_AND_SHIFT_SAFE() if ever this function allows riceParam=0. */
3035 
3036  if (bs->nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) {
3037 #ifndef DR_FLAC_NO_CRC
3038  drflac__update_crc16(bs);
3039 #endif
3040  bs->cache = drflac__be2host__cache_line(bs->cacheL2[bs->nextL2Line++]);
3041  bs->consumedBits = 0;
3042 #ifndef DR_FLAC_NO_CRC
3043  bs->crc16Cache = bs->cache;
3044 #endif
3045  } else {
3046  /* Slow path. We need to fetch more data from the client. */
3047  if (!drflac__reload_cache(bs)) {
3048  return DRFLAC_FALSE;
3049  }
3050  }
3051 
3052  riceParamPart = (drflac_uint32)(resultHi | DRFLAC_CACHE_L1_SELECT_AND_SHIFT_SAFE(bs, bitCountLo));
3053 
3054  bs->consumedBits += bitCountLo;
3055  bs->cache <<= bitCountLo;
3056  }
3057 
3058  pZeroCounterOut[0] = zeroCounter;
3059  pRiceParamPartOut[0] = riceParamPart;
3060 
3061  return DRFLAC_TRUE;
3062 }
3063 #endif
3064 
3065 static DRFLAC_INLINE drflac_bool32 drflac__read_rice_parts_x1(drflac_bs* bs, drflac_uint8 riceParam, drflac_uint32* pZeroCounterOut, drflac_uint32* pRiceParamPartOut)
3066 {
3067  drflac_uint32 riceParamPlus1 = riceParam + 1;
3068  /*drflac_cache_t riceParamPlus1Mask = DRFLAC_CACHE_L1_SELECTION_MASK(riceParamPlus1);*/
3069  drflac_uint32 riceParamPlus1Shift = DRFLAC_CACHE_L1_SELECTION_SHIFT(bs, riceParamPlus1);
3070  drflac_uint32 riceParamPlus1MaxConsumedBits = DRFLAC_CACHE_L1_SIZE_BITS(bs) - riceParamPlus1;
3071 
3072  /*
3073  The idea here is to use local variables for the cache in an attempt to encourage the compiler to store them in registers. I have
3074  no idea how this will work in practice...
3075  */
3076  drflac_cache_t bs_cache = bs->cache;
3077  drflac_uint32 bs_consumedBits = bs->consumedBits;
3078 
3079  /* The first thing to do is find the first unset bit. Most likely a bit will be set in the current cache line. */
3080  drflac_uint32 lzcount = drflac__clz(bs_cache);
3081  if (lzcount < sizeof(bs_cache)*8) {
3082  pZeroCounterOut[0] = lzcount;
3083 
3084  /*
3085  It is most likely that the riceParam part (which comes after the zero counter) is also on this cache line. When extracting
3086  this, we include the set bit from the unary coded part because it simplifies cache management. This bit will be handled
3087  outside of this function at a higher level.
3088  */
3089  extract_rice_param_part:
3090  bs_cache <<= lzcount;
3091  bs_consumedBits += lzcount;
3092 
3093  if (bs_consumedBits <= riceParamPlus1MaxConsumedBits) {
3094  /* Getting here means the rice parameter part is wholly contained within the current cache line. */
3095  pRiceParamPartOut[0] = (drflac_uint32)(bs_cache >> riceParamPlus1Shift);
3096  bs_cache <<= riceParamPlus1;
3097  bs_consumedBits += riceParamPlus1;
3098  } else {
3099  drflac_uint32 riceParamPartHi;
3100  drflac_uint32 riceParamPartLo;
3101  drflac_uint32 riceParamPartLoBitCount;
3102 
3103  /*
3104  Getting here means the rice parameter part straddles the cache line. We need to read from the tail of the current cache
3105  line, reload the cache, and then combine it with the head of the next cache line.
3106  */
3107 
3108  /* Grab the high part of the rice parameter part. */
3109  riceParamPartHi = (drflac_uint32)(bs_cache >> riceParamPlus1Shift);
3110 
3111  /* Before reloading the cache we need to grab the size in bits of the low part. */
3112  riceParamPartLoBitCount = bs_consumedBits - riceParamPlus1MaxConsumedBits;
3113  drflac_assert(riceParamPartLoBitCount > 0 && riceParamPartLoBitCount < 32);
3114 
3115  /* Now reload the cache. */
3116  if (bs->nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) {
3117  #ifndef DR_FLAC_NO_CRC
3118  drflac__update_crc16(bs);
3119  #endif
3120  bs_cache = drflac__be2host__cache_line(bs->cacheL2[bs->nextL2Line++]);
3121  bs_consumedBits = riceParamPartLoBitCount;
3122  #ifndef DR_FLAC_NO_CRC
3123  bs->crc16Cache = bs_cache;
3124  #endif
3125  } else {
3126  /* Slow path. We need to fetch more data from the client. */
3127  if (!drflac__reload_cache(bs)) {
3128  return DRFLAC_FALSE;
3129  }
3130 
3131  bs_cache = bs->cache;
3132  bs_consumedBits = bs->consumedBits + riceParamPartLoBitCount;
3133  }
3134 
3135  /* We should now have enough information to construct the rice parameter part. */
3136  riceParamPartLo = (drflac_uint32)(bs_cache >> (DRFLAC_CACHE_L1_SELECTION_SHIFT(bs, riceParamPartLoBitCount)));
3137  pRiceParamPartOut[0] = riceParamPartHi | riceParamPartLo;
3138 
3139  bs_cache <<= riceParamPartLoBitCount;
3140  }
3141  } else {
3142  /*
3143  Getting here means there are no bits set on the cache line. This is a less optimal case because we just wasted a call
3144  to drflac__clz() and we need to reload the cache.
3145  */
3146  drflac_uint32 zeroCounter = (drflac_uint32)(DRFLAC_CACHE_L1_SIZE_BITS(bs) - bs_consumedBits);
3147  for (;;) {
3148  if (bs->nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) {
3149  #ifndef DR_FLAC_NO_CRC
3150  drflac__update_crc16(bs);
3151  #endif
3152  bs_cache = drflac__be2host__cache_line(bs->cacheL2[bs->nextL2Line++]);
3153  bs_consumedBits = 0;
3154  #ifndef DR_FLAC_NO_CRC
3155  bs->crc16Cache = bs_cache;
3156  #endif
3157  } else {
3158  /* Slow path. We need to fetch more data from the client. */
3159  if (!drflac__reload_cache(bs)) {
3160  return DRFLAC_FALSE;
3161  }
3162 
3163  bs_cache = bs->cache;
3164  bs_consumedBits = bs->consumedBits;
3165  }
3166 
3167  lzcount = drflac__clz(bs_cache);
3168  zeroCounter += lzcount;
3169 
3170  if (lzcount < sizeof(bs_cache)*8) {
3171  break;
3172  }
3173  }
3174 
3175  pZeroCounterOut[0] = zeroCounter;
3176  goto extract_rice_param_part;
3177  }
3178 
3179  /* Make sure the cache is restored at the end of it all. */
3180  bs->cache = bs_cache;
3181  bs->consumedBits = bs_consumedBits;
3182 
3183  return DRFLAC_TRUE;
3184 }
3185 
3186 static DRFLAC_INLINE drflac_bool32 drflac__read_rice_parts_x4(drflac_bs* bs, drflac_uint8 riceParam, drflac_uint32* pZeroCounterOut, drflac_uint32* pRiceParamPartOut)
3187 {
3188  drflac_uint32 riceParamPlus1 = riceParam + 1;
3189  /*drflac_cache_t riceParamPlus1Mask = DRFLAC_CACHE_L1_SELECTION_MASK(riceParamPlus1);*/
3190  drflac_uint32 riceParamPlus1Shift = DRFLAC_CACHE_L1_SELECTION_SHIFT(bs, riceParamPlus1);
3191  drflac_uint32 riceParamPlus1MaxConsumedBits = DRFLAC_CACHE_L1_SIZE_BITS(bs) - riceParamPlus1;
3192 
3193  /*
3194  The idea here is to use local variables for the cache in an attempt to encourage the compiler to store them in registers. I have
3195  no idea how this will work in practice...
3196  */
3197  drflac_cache_t bs_cache = bs->cache;
3198  drflac_uint32 bs_consumedBits = bs->consumedBits;
3199 
3200  /*
3201  What this is doing is trying to efficiently extract 4 rice parts at a time, the idea being that we can exploit certain properties
3202  to our advantage to make things more efficient.
3203  */
3204  int i;
3205  for (i = 0; i < 4; ++i) {
3206  /* The first thing to do is find the first unset bit. Most likely a bit will be set in the current cache line. */
3207  drflac_uint32 lzcount = drflac__clz(bs_cache);
3208  if (lzcount < sizeof(bs_cache)*8) {
3209  pZeroCounterOut[i] = lzcount;
3210 
3211  /*
3212  It is most likely that the riceParam part (which comes after the zero counter) is also on this cache line. When extracting
3213  this, we include the set bit from the unary coded part because it simplifies cache management. This bit will be handled
3214  outside of this function at a higher level.
3215  */
3216  extract_rice_param_part:
3217  bs_cache <<= lzcount;
3218  bs_consumedBits += lzcount;
3219 
3220  if (bs_consumedBits <= riceParamPlus1MaxConsumedBits) {
3221  /* Getting here means the rice parameter part is wholly contained within the current cache line. */
3222  pRiceParamPartOut[i] = (drflac_uint32)(bs_cache >> riceParamPlus1Shift);
3223  bs_cache <<= riceParamPlus1;
3224  bs_consumedBits += riceParamPlus1;
3225  } else {
3226  drflac_uint32 riceParamPartHi;
3227  drflac_uint32 riceParamPartLo;
3228  drflac_uint32 riceParamPartLoBitCount;
3229 
3230  /*
3231  Getting here means the rice parameter part straddles the cache line. We need to read from the tail of the current cache
3232  line, reload the cache, and then combine it with the head of the next cache line.
3233  */
3234 
3235  /* Grab the high part of the rice parameter part. */
3236  riceParamPartHi = (drflac_uint32)(bs_cache >> riceParamPlus1Shift);
3237 
3238  /* Before reloading the cache we need to grab the size in bits of the low part. */
3239  riceParamPartLoBitCount = bs_consumedBits - riceParamPlus1MaxConsumedBits;
3240 
3241  /* Now reload the cache. */
3242  if (bs->nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) {
3243  #ifndef DR_FLAC_NO_CRC
3244  drflac__update_crc16(bs);
3245  #endif
3246  bs_cache = drflac__be2host__cache_line(bs->cacheL2[bs->nextL2Line++]);
3247  bs_consumedBits = riceParamPartLoBitCount;
3248  #ifndef DR_FLAC_NO_CRC
3249  bs->crc16Cache = bs_cache;
3250  #endif
3251  } else {
3252  /* Slow path. We need to fetch more data from the client. */
3253  if (!drflac__reload_cache(bs)) {
3254  return DRFLAC_FALSE;
3255  }
3256 
3257  bs_cache = bs->cache;
3258  bs_consumedBits = bs->consumedBits + riceParamPartLoBitCount;
3259  }
3260 
3261  /* We should now have enough information to construct the rice parameter part. */
3262  riceParamPartLo = (drflac_uint32)(bs_cache >> (DRFLAC_CACHE_L1_SELECTION_SHIFT(bs, riceParamPartLoBitCount)));
3263  pRiceParamPartOut[i] = riceParamPartHi | riceParamPartLo;
3264 
3265  bs_cache <<= riceParamPartLoBitCount;
3266  }
3267  } else {
3268  /*
3269  Getting here means there are no bits set on the cache line. This is a less optimal case because we just wasted a call
3270  to drflac__clz() and we need to reload the cache.
3271  */
3272  drflac_uint32 zeroCounter = (drflac_uint32)(DRFLAC_CACHE_L1_SIZE_BITS(bs) - bs_consumedBits);
3273  for (;;) {
3274  if (bs->nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) {
3275  #ifndef DR_FLAC_NO_CRC
3276  drflac__update_crc16(bs);
3277  #endif
3278  bs_cache = drflac__be2host__cache_line(bs->cacheL2[bs->nextL2Line++]);
3279  bs_consumedBits = 0;
3280  #ifndef DR_FLAC_NO_CRC
3281  bs->crc16Cache = bs_cache;
3282  #endif
3283  } else {
3284  /* Slow path. We need to fetch more data from the client. */
3285  if (!drflac__reload_cache(bs)) {
3286  return DRFLAC_FALSE;
3287  }
3288 
3289  bs_cache = bs->cache;
3290  bs_consumedBits = bs->consumedBits;
3291  }
3292 
3293  lzcount = drflac__clz(bs_cache);
3294  zeroCounter += lzcount;
3295 
3296  if (lzcount < sizeof(bs_cache)*8) {
3297  break;
3298  }
3299  }
3300 
3301  pZeroCounterOut[i] = zeroCounter;
3302  goto extract_rice_param_part;
3303  }
3304  }
3305 
3306  /* Make sure the cache is restored at the end of it all. */
3307  bs->cache = bs_cache;
3308  bs->consumedBits = bs_consumedBits;
3309 
3310  return DRFLAC_TRUE;
3311 }
3312 
3313 static DRFLAC_INLINE drflac_bool32 drflac__seek_rice_parts(drflac_bs* bs, drflac_uint8 riceParam)
3314 {
3315  drflac_uint32 riceParamPlus1 = riceParam + 1;
3316  drflac_uint32 riceParamPlus1MaxConsumedBits = DRFLAC_CACHE_L1_SIZE_BITS(bs) - riceParamPlus1;
3317 
3318  /*
3319  The idea here is to use local variables for the cache in an attempt to encourage the compiler to store them in registers. I have
3320  no idea how this will work in practice...
3321  */
3322  drflac_cache_t bs_cache = bs->cache;
3323  drflac_uint32 bs_consumedBits = bs->consumedBits;
3324 
3325  /* The first thing to do is find the first unset bit. Most likely a bit will be set in the current cache line. */
3326  drflac_uint32 lzcount = drflac__clz(bs_cache);
3327  if (lzcount < sizeof(bs_cache)*8) {
3328  /*
3329  It is most likely that the riceParam part (which comes after the zero counter) is also on this cache line. When extracting
3330  this, we include the set bit from the unary coded part because it simplifies cache management. This bit will be handled
3331  outside of this function at a higher level.
3332  */
3333  extract_rice_param_part:
3334  bs_cache <<= lzcount;
3335  bs_consumedBits += lzcount;
3336 
3337  if (bs_consumedBits <= riceParamPlus1MaxConsumedBits) {
3338  /* Getting here means the rice parameter part is wholly contained within the current cache line. */
3339  bs_cache <<= riceParamPlus1;
3340  bs_consumedBits += riceParamPlus1;
3341  } else {
3342  /*
3343  Getting here means the rice parameter part straddles the cache line. We need to read from the tail of the current cache
3344  line, reload the cache, and then combine it with the head of the next cache line.
3345  */
3346 
3347  /* Before reloading the cache we need to grab the size in bits of the low part. */
3348  drflac_uint32 riceParamPartLoBitCount = bs_consumedBits - riceParamPlus1MaxConsumedBits;
3349  drflac_assert(riceParamPartLoBitCount > 0 && riceParamPartLoBitCount < 32);
3350 
3351  /* Now reload the cache. */
3352  if (bs->nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) {
3353  #ifndef DR_FLAC_NO_CRC
3354  drflac__update_crc16(bs);
3355  #endif
3356  bs_cache = drflac__be2host__cache_line(bs->cacheL2[bs->nextL2Line++]);
3357  bs_consumedBits = riceParamPartLoBitCount;
3358  #ifndef DR_FLAC_NO_CRC
3359  bs->crc16Cache = bs_cache;
3360  #endif
3361  } else {
3362  /* Slow path. We need to fetch more data from the client. */
3363  if (!drflac__reload_cache(bs)) {
3364  return DRFLAC_FALSE;
3365  }
3366 
3367  bs_cache = bs->cache;
3368  bs_consumedBits = bs->consumedBits + riceParamPartLoBitCount;
3369  }
3370 
3371  bs_cache <<= riceParamPartLoBitCount;
3372  }
3373  } else {
3374  /*
3375  Getting here means there are no bits set on the cache line. This is a less optimal case because we just wasted a call
3376  to drflac__clz() and we need to reload the cache.
3377  */
3378  for (;;) {
3379  if (bs->nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) {
3380  #ifndef DR_FLAC_NO_CRC
3381  drflac__update_crc16(bs);
3382  #endif
3383  bs_cache = drflac__be2host__cache_line(bs->cacheL2[bs->nextL2Line++]);
3384  bs_consumedBits = 0;
3385  #ifndef DR_FLAC_NO_CRC
3386  bs->crc16Cache = bs_cache;
3387  #endif
3388  } else {
3389  /* Slow path. We need to fetch more data from the client. */
3390  if (!drflac__reload_cache(bs)) {
3391  return DRFLAC_FALSE;
3392  }
3393 
3394  bs_cache = bs->cache;
3395  bs_consumedBits = bs->consumedBits;
3396  }
3397 
3398  lzcount = drflac__clz(bs_cache);
3399  if (lzcount < sizeof(bs_cache)*8) {
3400  break;
3401  }
3402  }
3403 
3404  goto extract_rice_param_part;
3405  }
3406 
3407  /* Make sure the cache is restored at the end of it all. */
3408  bs->cache = bs_cache;
3409  bs->consumedBits = bs_consumedBits;
3410 
3411  return DRFLAC_TRUE;
3412 }
3413 
3414 
3415 static drflac_bool32 drflac__decode_samples_with_residual__rice__scalar(drflac_bs* bs, drflac_uint32 bitsPerSample, drflac_uint32 count, drflac_uint8 riceParam, drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pSamplesOut)
3416 {
3417  drflac_uint32 t[2] = {0x00000000, 0xFFFFFFFF};
3418  drflac_uint32 zeroCountPart0;
3419  drflac_uint32 zeroCountPart1;
3420  drflac_uint32 zeroCountPart2;
3421  drflac_uint32 zeroCountPart3;
3422  drflac_uint32 riceParamPart0;
3423  drflac_uint32 riceParamPart1;
3424  drflac_uint32 riceParamPart2;
3425  drflac_uint32 riceParamPart3;
3426  drflac_uint32 riceParamMask;
3427  const drflac_int32* pSamplesOutEnd;
3428  drflac_uint32 i;
3429 
3430  drflac_assert(bs != NULL);
3431  drflac_assert(count > 0);
3432  drflac_assert(pSamplesOut != NULL);
3433 
3434  riceParamMask = ~((~0UL) << riceParam);
3435  pSamplesOutEnd = pSamplesOut + ((count >> 2) << 2);
3436 
3437  if (bitsPerSample >= 24) {
3438  while (pSamplesOut < pSamplesOutEnd) {
3439  /*
3440  Rice extraction. It's faster to do this one at a time against local variables than it is to use the x4 version
3441  against an array. Not sure why, but perhaps it's making more efficient use of registers?
3442  */
3443  if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart0, &riceParamPart0) ||
3444  !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart1, &riceParamPart1) ||
3445  !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart2, &riceParamPart2) ||
3446  !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart3, &riceParamPart3)) {
3447  return DRFLAC_FALSE;
3448  }
3449 
3450  riceParamPart0 &= riceParamMask;
3451  riceParamPart1 &= riceParamMask;
3452  riceParamPart2 &= riceParamMask;
3453  riceParamPart3 &= riceParamMask;
3454 
3455  riceParamPart0 |= (zeroCountPart0 << riceParam);
3456  riceParamPart1 |= (zeroCountPart1 << riceParam);
3457  riceParamPart2 |= (zeroCountPart2 << riceParam);
3458  riceParamPart3 |= (zeroCountPart3 << riceParam);
3459 
3460  riceParamPart0 = (riceParamPart0 >> 1) ^ t[riceParamPart0 & 0x01];
3461  riceParamPart1 = (riceParamPart1 >> 1) ^ t[riceParamPart1 & 0x01];
3462  riceParamPart2 = (riceParamPart2 >> 1) ^ t[riceParamPart2 & 0x01];
3463  riceParamPart3 = (riceParamPart3 >> 1) ^ t[riceParamPart3 & 0x01];
3464 
3465  pSamplesOut[0] = riceParamPart0 + drflac__calculate_prediction_64(order, shift, coefficients, pSamplesOut + 0);
3466  pSamplesOut[1] = riceParamPart1 + drflac__calculate_prediction_64(order, shift, coefficients, pSamplesOut + 1);
3467  pSamplesOut[2] = riceParamPart2 + drflac__calculate_prediction_64(order, shift, coefficients, pSamplesOut + 2);
3468  pSamplesOut[3] = riceParamPart3 + drflac__calculate_prediction_64(order, shift, coefficients, pSamplesOut + 3);
3469 
3470  pSamplesOut += 4;
3471  }
3472  } else {
3473  while (pSamplesOut < pSamplesOutEnd) {
3474  if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart0, &riceParamPart0) ||
3475  !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart1, &riceParamPart1) ||
3476  !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart2, &riceParamPart2) ||
3477  !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart3, &riceParamPart3)) {
3478  return DRFLAC_FALSE;
3479  }
3480 
3481  riceParamPart0 &= riceParamMask;
3482  riceParamPart1 &= riceParamMask;
3483  riceParamPart2 &= riceParamMask;
3484  riceParamPart3 &= riceParamMask;
3485 
3486  riceParamPart0 |= (zeroCountPart0 << riceParam);
3487  riceParamPart1 |= (zeroCountPart1 << riceParam);
3488  riceParamPart2 |= (zeroCountPart2 << riceParam);
3489  riceParamPart3 |= (zeroCountPart3 << riceParam);
3490 
3491  riceParamPart0 = (riceParamPart0 >> 1) ^ t[riceParamPart0 & 0x01];
3492  riceParamPart1 = (riceParamPart1 >> 1) ^ t[riceParamPart1 & 0x01];
3493  riceParamPart2 = (riceParamPart2 >> 1) ^ t[riceParamPart2 & 0x01];
3494  riceParamPart3 = (riceParamPart3 >> 1) ^ t[riceParamPart3 & 0x01];
3495 
3496  pSamplesOut[0] = riceParamPart0 + drflac__calculate_prediction_32(order, shift, coefficients, pSamplesOut + 0);
3497  pSamplesOut[1] = riceParamPart1 + drflac__calculate_prediction_32(order, shift, coefficients, pSamplesOut + 1);
3498  pSamplesOut[2] = riceParamPart2 + drflac__calculate_prediction_32(order, shift, coefficients, pSamplesOut + 2);
3499  pSamplesOut[3] = riceParamPart3 + drflac__calculate_prediction_32(order, shift, coefficients, pSamplesOut + 3);
3500 
3501  pSamplesOut += 4;
3502  }
3503  }
3504 
3505  i = ((count >> 2) << 2);
3506  while (i < count) {
3507  /* Rice extraction. */
3508  if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart0, &riceParamPart0)) {
3509  return DRFLAC_FALSE;
3510  }
3511 
3512  /* Rice reconstruction. */
3513  riceParamPart0 &= riceParamMask;
3514  riceParamPart0 |= (zeroCountPart0 << riceParam);
3515  riceParamPart0 = (riceParamPart0 >> 1) ^ t[riceParamPart0 & 0x01];
3516  /*riceParamPart0 = (riceParamPart0 >> 1) ^ (~(riceParamPart0 & 0x01) + 1);*/
3517 
3518  /* Sample reconstruction. */
3519  if (bitsPerSample >= 24) {
3520  pSamplesOut[0] = riceParamPart0 + drflac__calculate_prediction_64(order, shift, coefficients, pSamplesOut + 0);
3521  } else {
3522  pSamplesOut[0] = riceParamPart0 + drflac__calculate_prediction_32(order, shift, coefficients, pSamplesOut + 0);
3523  }
3524 
3525  i += 1;
3526  pSamplesOut += 1;
3527  }
3528 
3529  return DRFLAC_TRUE;
3530 }
3531 
3532 #if defined(DRFLAC_SUPPORT_SSE41)
3533 static drflac_bool32 drflac__decode_samples_with_residual__rice__sse41(drflac_bs* bs, drflac_uint32 bitsPerSample, drflac_uint32 count, drflac_uint8 riceParam, drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pSamplesOut)
3534 {
3535  static drflac_uint32 t[2] = {0x00000000, 0xFFFFFFFF};
3536 
3537  /*drflac_uint32 zeroCountParts[4];*/
3538  /*drflac_uint32 riceParamParts[4];*/
3539 
3540  drflac_uint32 zeroCountParts0;
3541  drflac_uint32 zeroCountParts1;
3542  drflac_uint32 zeroCountParts2;
3543  drflac_uint32 zeroCountParts3;
3544  drflac_uint32 riceParamParts0;
3545  drflac_uint32 riceParamParts1;
3546  drflac_uint32 riceParamParts2;
3547  drflac_uint32 riceParamParts3;
3548  drflac_uint32 riceParamMask;
3549  const drflac_int32* pSamplesOutEnd;
3550  __m128i riceParamMask128;
3551  __m128i one;
3552  drflac_uint32 i;
3553 
3554  drflac_assert(bs != NULL);
3555  drflac_assert(count > 0);
3556  drflac_assert(pSamplesOut != NULL);
3557 
3558  riceParamMask = ~((~0UL) << riceParam);
3559  riceParamMask128 = _mm_set1_epi32(riceParamMask);
3560  one = _mm_set1_epi32(0x01);
3561 
3562  pSamplesOutEnd = pSamplesOut + ((count >> 2) << 2);
3563 
3564  if (bitsPerSample >= 24) {
3565  while (pSamplesOut < pSamplesOutEnd) {
3566  __m128i zeroCountPart128;
3567  __m128i riceParamPart128;
3568  drflac_uint32 riceParamParts[4];
3569 
3570  /* Rice extraction. */
3571  if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts0, &riceParamParts0) ||
3572  !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts1, &riceParamParts1) ||
3573  !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts2, &riceParamParts2) ||
3574  !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts3, &riceParamParts3)) {
3575  return DRFLAC_FALSE;
3576  }
3577 
3578  zeroCountPart128 = _mm_set_epi32(zeroCountParts3, zeroCountParts2, zeroCountParts1, zeroCountParts0);
3579  riceParamPart128 = _mm_set_epi32(riceParamParts3, riceParamParts2, riceParamParts1, riceParamParts0);
3580 
3581  riceParamPart128 = _mm_and_si128(riceParamPart128, riceParamMask128);
3582  riceParamPart128 = _mm_or_si128(riceParamPart128, _mm_slli_epi32(zeroCountPart128, riceParam));
3583  riceParamPart128 = _mm_xor_si128(_mm_srli_epi32(riceParamPart128, 1), _mm_mullo_epi32(_mm_and_si128(riceParamPart128, one), _mm_set1_epi32(0xFFFFFFFF))); /* <-- Only supported from SSE4.1 */
3584  /*riceParamPart128 = _mm_xor_si128(_mm_srli_epi32(riceParamPart128, 1), _mm_add_epi32(drflac__mm_not_si128(_mm_and_si128(riceParamPart128, one)), one));*/ /* <-- SSE2 compatible */
3585 
3586  _mm_storeu_si128((__m128i*)riceParamParts, riceParamPart128);
3587 
3588  #if defined(DRFLAC_64BIT)
3589  /* The scalar implementation seems to be faster on 64-bit in my testing. */
3590  drflac__calculate_prediction_64_x4(order, shift, coefficients, riceParamParts, pSamplesOut);
3591  #else
3592  pSamplesOut[0] = riceParamParts[0] + drflac__calculate_prediction_64__sse41(order, shift, coefficients, pSamplesOut + 0);
3593  pSamplesOut[1] = riceParamParts[1] + drflac__calculate_prediction_64__sse41(order, shift, coefficients, pSamplesOut + 1);
3594  pSamplesOut[2] = riceParamParts[2] + drflac__calculate_prediction_64__sse41(order, shift, coefficients, pSamplesOut + 2);
3595  pSamplesOut[3] = riceParamParts[3] + drflac__calculate_prediction_64__sse41(order, shift, coefficients, pSamplesOut + 3);
3596  #endif
3597 
3598  pSamplesOut += 4;
3599  }
3600  } else {
3601  drflac_int32 coefficientsUnaligned[32*4 + 4] = {0};
3602  drflac_int32* coefficients128 = (drflac_int32*)(((size_t)coefficientsUnaligned + 15) & ~15);
3603 
3604  for (i = 0; i < order; ++i) {
3605  coefficients128[i*4+0] = coefficients[i];
3606  coefficients128[i*4+1] = coefficients[i];
3607  coefficients128[i*4+2] = coefficients[i];
3608  coefficients128[i*4+3] = coefficients[i];
3609  }
3610 
3611  while (pSamplesOut < pSamplesOutEnd) {
3612  __m128i zeroCountPart128;
3613  __m128i riceParamPart128;
3614  /*drflac_int32 riceParamParts[4];*/
3615 
3616  /* Rice extraction. */
3617 #if 1
3618  if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts0, &riceParamParts0) ||
3619  !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts1, &riceParamParts1) ||
3620  !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts2, &riceParamParts2) ||
3621  !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts3, &riceParamParts3)) {
3622  return DRFLAC_FALSE;
3623  }
3624 
3625  zeroCountPart128 = _mm_set_epi32(zeroCountParts3, zeroCountParts2, zeroCountParts1, zeroCountParts0);
3626  riceParamPart128 = _mm_set_epi32(riceParamParts3, riceParamParts2, riceParamParts1, riceParamParts0);
3627 #else
3628  if (!drflac__read_rice_parts_x4(bs, riceParam, zeroCountParts, riceParamParts)) {
3629  return DRFLAC_FALSE;
3630  }
3631 
3632  zeroCountPart128 = _mm_set_epi32(zeroCountParts[3], zeroCountParts[2], zeroCountParts[1], zeroCountParts[0]);
3633  riceParamPart128 = _mm_set_epi32(riceParamParts[3], riceParamParts[2], riceParamParts[1], riceParamParts[0]);
3634 #endif
3635 
3636  riceParamPart128 = _mm_and_si128(riceParamPart128, riceParamMask128);
3637  riceParamPart128 = _mm_or_si128(riceParamPart128, _mm_slli_epi32(zeroCountPart128, riceParam));
3638  riceParamPart128 = _mm_xor_si128(_mm_srli_epi32(riceParamPart128, 1), _mm_mullo_epi32(_mm_and_si128(riceParamPart128, one), _mm_set1_epi32(0xFFFFFFFF)));
3639 
3640 #if 1
3641  drflac__calculate_prediction_32_x4__sse41(order, shift, (const __m128i*)coefficients128, riceParamPart128, pSamplesOut);
3642 #else
3643  _mm_storeu_si128((__m128i*)riceParamParts, riceParamPart128);
3644 
3645  pSamplesOut[0] = riceParamParts[0] + drflac__calculate_prediction_32(order, shift, coefficients, pSamplesOut + 0);
3646  pSamplesOut[1] = riceParamParts[1] + drflac__calculate_prediction_32(order, shift, coefficients, pSamplesOut + 1);
3647  pSamplesOut[2] = riceParamParts[2] + drflac__calculate_prediction_32(order, shift, coefficients, pSamplesOut + 2);
3648  pSamplesOut[3] = riceParamParts[3] + drflac__calculate_prediction_32(order, shift, coefficients, pSamplesOut + 3);
3649 #endif
3650 
3651  pSamplesOut += 4;
3652  }
3653  }
3654 
3655 
3656  i = ((count >> 2) << 2);
3657  while (i < count) {
3658  /* Rice extraction. */
3659  if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts0, &riceParamParts0)) {
3660  return DRFLAC_FALSE;
3661  }
3662 
3663  /* Rice reconstruction. */
3664  riceParamParts0 &= riceParamMask;
3665  riceParamParts0 |= (zeroCountParts0 << riceParam);
3666  riceParamParts0 = (riceParamParts0 >> 1) ^ t[riceParamParts0 & 0x01];
3667 
3668  /* Sample reconstruction. */
3669  if (bitsPerSample >= 24) {
3670  pSamplesOut[0] = riceParamParts0 + drflac__calculate_prediction_64(order, shift, coefficients, pSamplesOut + 0);
3671  } else {
3672  pSamplesOut[0] = riceParamParts0 + drflac__calculate_prediction_32(order, shift, coefficients, pSamplesOut + 0);
3673  }
3674 
3675  i += 1;
3676  pSamplesOut += 1;
3677  }
3678 
3679  return DRFLAC_TRUE;
3680 }
3681 #endif
3682 
3683 static drflac_bool32 drflac__decode_samples_with_residual__rice(drflac_bs* bs, drflac_uint32 bitsPerSample, drflac_uint32 count, drflac_uint8 riceParam, drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pSamplesOut)
3684 {
3685 #if defined(DRFLAC_SUPPORT_SSE41)
3686  if (drflac__gIsSSE41Supported) {
3687  return drflac__decode_samples_with_residual__rice__sse41(bs, bitsPerSample, count, riceParam, order, shift, coefficients, pSamplesOut);
3688  } else
3689 #endif
3690  {
3691  /* Scalar fallback. */
3692  #if 0
3693  return drflac__decode_samples_with_residual__rice__reference(bs, bitsPerSample, count, riceParam, order, shift, coefficients, pSamplesOut);
3694  #else
3695  return drflac__decode_samples_with_residual__rice__scalar(bs, bitsPerSample, count, riceParam, order, shift, coefficients, pSamplesOut);
3696  #endif
3697  }
3698 }
3699 
3700 /* Reads and seeks past a string of residual values as Rice codes. The decoder should be sitting on the first bit of the Rice codes. */
3701 static drflac_bool32 drflac__read_and_seek_residual__rice(drflac_bs* bs, drflac_uint32 count, drflac_uint8 riceParam)
3702 {
3703  drflac_uint32 i;
3704 
3705  drflac_assert(bs != NULL);
3706  drflac_assert(count > 0);
3707 
3708  for (i = 0; i < count; ++i) {
3709  if (!drflac__seek_rice_parts(bs, riceParam)) {
3710  return DRFLAC_FALSE;
3711  }
3712  }
3713 
3714  return DRFLAC_TRUE;
3715 }
3716 
3717 static drflac_bool32 drflac__decode_samples_with_residual__unencoded(drflac_bs* bs, drflac_uint32 bitsPerSample, drflac_uint32 count, drflac_uint8 unencodedBitsPerSample, drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pSamplesOut)
3718 {
3719  drflac_uint32 i;
3720 
3721  drflac_assert(bs != NULL);
3722  drflac_assert(count > 0);
3723  drflac_assert(unencodedBitsPerSample <= 31); /* <-- unencodedBitsPerSample is a 5 bit number, so cannot exceed 31. */
3724  drflac_assert(pSamplesOut != NULL);
3725 
3726  for (i = 0; i < count; ++i) {
3727  if (unencodedBitsPerSample > 0) {
3728  if (!drflac__read_int32(bs, unencodedBitsPerSample, pSamplesOut + i)) {
3729  return DRFLAC_FALSE;
3730  }
3731  } else {
3732  pSamplesOut[i] = 0;
3733  }
3734 
3735  if (bitsPerSample > 16) {
3736  pSamplesOut[i] += drflac__calculate_prediction_64(order, shift, coefficients, pSamplesOut + i);
3737  } else {
3738  pSamplesOut[i] += drflac__calculate_prediction_32(order, shift, coefficients, pSamplesOut + i);
3739  }
3740  }
3741 
3742  return DRFLAC_TRUE;
3743 }
3744 
3745 
3746 /*
3747 Reads and decodes the residual for the sub-frame the decoder is currently sitting on. This function should be called
3748 when the decoder is sitting at the very start of the RESIDUAL block. The first <order> residuals will be ignored. The
3749 <blockSize> and <order> parameters are used to determine how many residual values need to be decoded.
3750 */
3751 static drflac_bool32 drflac__decode_samples_with_residual(drflac_bs* bs, drflac_uint32 bitsPerSample, drflac_uint32 blockSize, drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pDecodedSamples)
3752 {
3753  drflac_uint8 residualMethod;
3754  drflac_uint8 partitionOrder;
3755  drflac_uint32 samplesInPartition;
3756  drflac_uint32 partitionsRemaining;
3757 
3758  drflac_assert(bs != NULL);
3759  drflac_assert(blockSize != 0);
3760  drflac_assert(pDecodedSamples != NULL); /* <-- Should we allow NULL, in which case we just seek past the residual rather than do a full decode? */
3761 
3762  if (!drflac__read_uint8(bs, 2, &residualMethod)) {
3763  return DRFLAC_FALSE;
3764  }
3765 
3766  if (residualMethod != DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE && residualMethod != DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2) {
3767  return DRFLAC_FALSE; /* Unknown or unsupported residual coding method. */
3768  }
3769 
3770  /* Ignore the first <order> values. */
3771  pDecodedSamples += order;
3772 
3773  if (!drflac__read_uint8(bs, 4, &partitionOrder)) {
3774  return DRFLAC_FALSE;
3775  }
3776 
3777  /*
3778  From the FLAC spec:
3779  The Rice partition order in a Rice-coded residual section must be less than or equal to 8.
3780  */
3781  if (partitionOrder > 8) {
3782  return DRFLAC_FALSE;
3783  }
3784 
3785  /* Validation check. */
3786  if ((blockSize / (1 << partitionOrder)) <= order) {
3787  return DRFLAC_FALSE;
3788  }
3789 
3790  samplesInPartition = (blockSize / (1 << partitionOrder)) - order;
3791  partitionsRemaining = (1 << partitionOrder);
3792  for (;;) {
3793  drflac_uint8 riceParam = 0;
3794  if (residualMethod == DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE) {
3795  if (!drflac__read_uint8(bs, 4, &riceParam)) {
3796  return DRFLAC_FALSE;
3797  }
3798  if (riceParam == 15) {
3799  riceParam = 0xFF;
3800  }
3801  } else if (residualMethod == DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2) {
3802  if (!drflac__read_uint8(bs, 5, &riceParam)) {
3803  return DRFLAC_FALSE;
3804  }
3805  if (riceParam == 31) {
3806  riceParam = 0xFF;
3807  }
3808  }
3809 
3810  if (riceParam != 0xFF) {
3811  if (!drflac__decode_samples_with_residual__rice(bs, bitsPerSample, samplesInPartition, riceParam, order, shift, coefficients, pDecodedSamples)) {
3812  return DRFLAC_FALSE;
3813  }
3814  } else {
3815  unsigned char unencodedBitsPerSample = 0;
3816  if (!drflac__read_uint8(bs, 5, &unencodedBitsPerSample)) {
3817  return DRFLAC_FALSE;
3818  }
3819 
3820  if (!drflac__decode_samples_with_residual__unencoded(bs, bitsPerSample, samplesInPartition, unencodedBitsPerSample, order, shift, coefficients, pDecodedSamples)) {
3821  return DRFLAC_FALSE;
3822  }
3823  }
3824 
3825  pDecodedSamples += samplesInPartition;
3826 
3827  if (partitionsRemaining == 1) {
3828  break;
3829  }
3830 
3831  partitionsRemaining -= 1;
3832 
3833  if (partitionOrder != 0) {
3834  samplesInPartition = blockSize / (1 << partitionOrder);
3835  }
3836  }
3837 
3838  return DRFLAC_TRUE;
3839 }
3840 
3841 /*
3842 Reads and seeks past the residual for the sub-frame the decoder is currently sitting on. This function should be called
3843 when the decoder is sitting at the very start of the RESIDUAL block. The first <order> residuals will be set to 0. The
3844 <blockSize> and <order> parameters are used to determine how many residual values need to be decoded.
3845 */
3846 static drflac_bool32 drflac__read_and_seek_residual(drflac_bs* bs, drflac_uint32 blockSize, drflac_uint32 order)
3847 {
3848  drflac_uint8 residualMethod;
3849  drflac_uint8 partitionOrder;
3850  drflac_uint32 samplesInPartition;
3851  drflac_uint32 partitionsRemaining;
3852 
3853  drflac_assert(bs != NULL);
3854  drflac_assert(blockSize != 0);
3855 
3856  if (!drflac__read_uint8(bs, 2, &residualMethod)) {
3857  return DRFLAC_FALSE;
3858  }
3859 
3860  if (residualMethod != DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE && residualMethod != DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2) {
3861  return DRFLAC_FALSE; /* Unknown or unsupported residual coding method. */
3862  }
3863 
3864  if (!drflac__read_uint8(bs, 4, &partitionOrder)) {
3865  return DRFLAC_FALSE;
3866  }
3867 
3868  /*
3869  From the FLAC spec:
3870  The Rice partition order in a Rice-coded residual section must be less than or equal to 8.
3871  */
3872  if (partitionOrder > 8) {
3873  return DRFLAC_FALSE;
3874  }
3875 
3876  /* Validation check. */
3877  if ((blockSize / (1 << partitionOrder)) <= order) {
3878  return DRFLAC_FALSE;
3879  }
3880 
3881  samplesInPartition = (blockSize / (1 << partitionOrder)) - order;
3882  partitionsRemaining = (1 << partitionOrder);
3883  for (;;)
3884  {
3885  drflac_uint8 riceParam = 0;
3886  if (residualMethod == DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE) {
3887  if (!drflac__read_uint8(bs, 4, &riceParam)) {
3888  return DRFLAC_FALSE;
3889  }
3890  if (riceParam == 15) {
3891  riceParam = 0xFF;
3892  }
3893  } else if (residualMethod == DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2) {
3894  if (!drflac__read_uint8(bs, 5, &riceParam)) {
3895  return DRFLAC_FALSE;
3896  }
3897  if (riceParam == 31) {
3898  riceParam = 0xFF;
3899  }
3900  }
3901 
3902  if (riceParam != 0xFF) {
3903  if (!drflac__read_and_seek_residual__rice(bs, samplesInPartition, riceParam)) {
3904  return DRFLAC_FALSE;
3905  }
3906  } else {
3907  unsigned char unencodedBitsPerSample = 0;
3908  if (!drflac__read_uint8(bs, 5, &unencodedBitsPerSample)) {
3909  return DRFLAC_FALSE;
3910  }
3911 
3912  if (!drflac__seek_bits(bs, unencodedBitsPerSample * samplesInPartition)) {
3913  return DRFLAC_FALSE;
3914  }
3915  }
3916 
3917 
3918  if (partitionsRemaining == 1) {
3919  break;
3920  }
3921 
3922  partitionsRemaining -= 1;
3923  samplesInPartition = blockSize / (1 << partitionOrder);
3924  }
3925 
3926  return DRFLAC_TRUE;
3927 }
3928 
3929 
3930 static drflac_bool32 drflac__decode_samples__constant(drflac_bs* bs, drflac_uint32 blockSize, drflac_uint32 bitsPerSample, drflac_int32* pDecodedSamples)
3931 {
3932  drflac_uint32 i;
3933 
3934  /* Only a single sample needs to be decoded here. */
3936  if (!drflac__read_int32(bs, bitsPerSample, &sample)) {
3937  return DRFLAC_FALSE;
3938  }
3939 
3940  /*
3941  We don't really need to expand this, but it does simplify the process of reading samples. If this becomes a performance issue (unlikely)
3942  we'll want to look at a more efficient way.
3943  */
3944  for (i = 0; i < blockSize; ++i) {
3945  pDecodedSamples[i] = sample;
3946  }
3947 
3948  return DRFLAC_TRUE;
3949 }
3950 
3951 static drflac_bool32 drflac__decode_samples__verbatim(drflac_bs* bs, drflac_uint32 blockSize, drflac_uint32 bitsPerSample, drflac_int32* pDecodedSamples)
3952 {
3953  drflac_uint32 i;
3954 
3955  for (i = 0; i < blockSize; ++i) {
3957  if (!drflac__read_int32(bs, bitsPerSample, &sample)) {
3958  return DRFLAC_FALSE;
3959  }
3960 
3961  pDecodedSamples[i] = sample;
3962  }
3963 
3964  return DRFLAC_TRUE;
3965 }
3966 
3967 static drflac_bool32 drflac__decode_samples__fixed(drflac_bs* bs, drflac_uint32 blockSize, drflac_uint32 bitsPerSample, drflac_uint8 lpcOrder, drflac_int32* pDecodedSamples)
3968 {
3969  drflac_uint32 i;
3970 
3971  static drflac_int32 lpcCoefficientsTable[5][4] = {
3972  {0, 0, 0, 0},
3973  {1, 0, 0, 0},
3974  {2, -1, 0, 0},
3975  {3, -3, 1, 0},
3976  {4, -6, 4, -1}
3977  };
3978 
3979  /* Warm up samples and coefficients. */
3980  for (i = 0; i < lpcOrder; ++i) {
3982  if (!drflac__read_int32(bs, bitsPerSample, &sample)) {
3983  return DRFLAC_FALSE;
3984  }
3985 
3986  pDecodedSamples[i] = sample;
3987  }
3988 
3989  if (!drflac__decode_samples_with_residual(bs, bitsPerSample, blockSize, lpcOrder, 0, lpcCoefficientsTable[lpcOrder], pDecodedSamples)) {
3990  return DRFLAC_FALSE;
3991  }
3992 
3993  return DRFLAC_TRUE;
3994 }
3995 
3996 static drflac_bool32 drflac__decode_samples__lpc(drflac_bs* bs, drflac_uint32 blockSize, drflac_uint32 bitsPerSample, drflac_uint8 lpcOrder, drflac_int32* pDecodedSamples)
3997 {
3998  drflac_uint8 i;
3999  drflac_uint8 lpcPrecision;
4000  drflac_int8 lpcShift;
4001  drflac_int32 coefficients[32];
4002 
4003  /* Warm up samples. */
4004  for (i = 0; i < lpcOrder; ++i) {
4006  if (!drflac__read_int32(bs, bitsPerSample, &sample)) {
4007  return DRFLAC_FALSE;
4008  }
4009 
4010  pDecodedSamples[i] = sample;
4011  }
4012 
4013  if (!drflac__read_uint8(bs, 4, &lpcPrecision)) {
4014  return DRFLAC_FALSE;
4015  }
4016  if (lpcPrecision == 15) {
4017  return DRFLAC_FALSE; /* Invalid. */
4018  }
4019  lpcPrecision += 1;
4020 
4021  if (!drflac__read_int8(bs, 5, &lpcShift)) {
4022  return DRFLAC_FALSE;
4023  }
4024 
4025  drflac_zero_memory(coefficients, sizeof(coefficients));
4026  for (i = 0; i < lpcOrder; ++i) {
4027  if (!drflac__read_int32(bs, lpcPrecision, coefficients + i)) {
4028  return DRFLAC_FALSE;
4029  }
4030  }
4031 
4032  if (!drflac__decode_samples_with_residual(bs, bitsPerSample, blockSize, lpcOrder, lpcShift, coefficients, pDecodedSamples)) {
4033  return DRFLAC_FALSE;
4034  }
4035 
4036  return DRFLAC_TRUE;
4037 }
4038 
4039 
4040 static drflac_bool32 drflac__read_next_flac_frame_header(drflac_bs* bs, drflac_uint8 streaminfoBitsPerSample, drflac_frame_header* header)
4041 {
4042  const drflac_uint32 sampleRateTable[12] = {0, 88200, 176400, 192000, 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000};
4043  const drflac_uint8 bitsPerSampleTable[8] = {0, 8, 12, (drflac_uint8)-1, 16, 20, 24, (drflac_uint8)-1}; /* -1 = reserved. */
4044 
4045  drflac_assert(bs != NULL);
4046  drflac_assert(header != NULL);
4047 
4048  /* Keep looping until we find a valid sync code. */
4049  for (;;) {
4050  drflac_uint8 crc8 = 0xCE; /* 0xCE = drflac_crc8(0, 0x3FFE, 14); */
4051  drflac_uint8 reserved = 0;
4052  drflac_uint8 blockingStrategy = 0;
4053  drflac_uint8 blockSize = 0;
4054  drflac_uint8 sampleRate = 0;
4055  drflac_uint8 channelAssignment = 0;
4056  drflac_uint8 bitsPerSample = 0;
4057  drflac_bool32 isVariableBlockSize;
4058 
4059  if (!drflac__find_and_seek_to_next_sync_code(bs)) {
4060  return DRFLAC_FALSE;
4061  }
4062 
4063  if (!drflac__read_uint8(bs, 1, &reserved)) {
4064  return DRFLAC_FALSE;
4065  }
4066  if (reserved == 1) {
4067  continue;
4068  }
4069  crc8 = drflac_crc8(crc8, reserved, 1);
4070 
4071  if (!drflac__read_uint8(bs, 1, &blockingStrategy)) {
4072  return DRFLAC_FALSE;
4073  }
4074  crc8 = drflac_crc8(crc8, blockingStrategy, 1);
4075 
4076  if (!drflac__read_uint8(bs, 4, &blockSize)) {
4077  return DRFLAC_FALSE;
4078  }
4079  if (blockSize == 0) {
4080  continue;
4081  }
4082  crc8 = drflac_crc8(crc8, blockSize, 4);
4083 
4084  if (!drflac__read_uint8(bs, 4, &sampleRate)) {
4085  return DRFLAC_FALSE;
4086  }
4087  crc8 = drflac_crc8(crc8, sampleRate, 4);
4088 
4089  if (!drflac__read_uint8(bs, 4, &channelAssignment)) {
4090  return DRFLAC_FALSE;
4091  }
4092  if (channelAssignment > 10) {
4093  continue;
4094  }
4095  crc8 = drflac_crc8(crc8, channelAssignment, 4);
4096 
4097  if (!drflac__read_uint8(bs, 3, &bitsPerSample)) {
4098  return DRFLAC_FALSE;
4099  }
4100  if (bitsPerSample == 3 || bitsPerSample == 7) {
4101  continue;
4102  }
4103  crc8 = drflac_crc8(crc8, bitsPerSample, 3);
4104 
4105 
4106  if (!drflac__read_uint8(bs, 1, &reserved)) {
4107  return DRFLAC_FALSE;
4108  }
4109  if (reserved == 1) {
4110  continue;
4111  }
4112  crc8 = drflac_crc8(crc8, reserved, 1);
4113 
4114 
4115  isVariableBlockSize = blockingStrategy == 1;
4116  if (isVariableBlockSize) {
4117  drflac_uint64 sampleNumber;
4118  drflac_result result = drflac__read_utf8_coded_number(bs, &sampleNumber, &crc8);
4119  if (result != DRFLAC_SUCCESS) {
4120  if (result == DRFLAC_END_OF_STREAM) {
4121  return DRFLAC_FALSE;
4122  } else {
4123  continue;
4124  }
4125  }
4126  header->frameNumber = 0;
4127  header->sampleNumber = sampleNumber;
4128  } else {
4129  drflac_uint64 frameNumber = 0;
4130  drflac_result result = drflac__read_utf8_coded_number(bs, &frameNumber, &crc8);
4131  if (result != DRFLAC_SUCCESS) {
4132  if (result == DRFLAC_END_OF_STREAM) {
4133  return DRFLAC_FALSE;
4134  } else {
4135  continue;
4136  }
4137  }
4138  header->frameNumber = (drflac_uint32)frameNumber; /* <-- Safe cast. */
4139  header->sampleNumber = 0;
4140  }
4141 
4142 
4143  if (blockSize == 1) {
4144  header->blockSize = 192;
4145  } else if (blockSize >= 2 && blockSize <= 5) {
4146  header->blockSize = 576 * (1 << (blockSize - 2));
4147  } else if (blockSize == 6) {
4148  if (!drflac__read_uint16(bs, 8, &header->blockSize)) {
4149  return DRFLAC_FALSE;
4150  }
4151  crc8 = drflac_crc8(crc8, header->blockSize, 8);
4152  header->blockSize += 1;
4153  } else if (blockSize == 7) {
4154  if (!drflac__read_uint16(bs, 16, &header->blockSize)) {
4155  return DRFLAC_FALSE;
4156  }
4157  crc8 = drflac_crc8(crc8, header->blockSize, 16);
4158  header->blockSize += 1;
4159  } else {
4160  header->blockSize = 256 * (1 << (blockSize - 8));
4161  }
4162 
4163 
4164  if (sampleRate <= 11) {
4165  header->sampleRate = sampleRateTable[sampleRate];
4166  } else if (sampleRate == 12) {
4167  if (!drflac__read_uint32(bs, 8, &header->sampleRate)) {
4168  return DRFLAC_FALSE;
4169  }
4170  crc8 = drflac_crc8(crc8, header->sampleRate, 8);
4171  header->sampleRate *= 1000;
4172  } else if (sampleRate == 13) {
4173  if (!drflac__read_uint32(bs, 16, &header->sampleRate)) {
4174  return DRFLAC_FALSE;
4175  }
4176  crc8 = drflac_crc8(crc8, header->sampleRate, 16);
4177  } else if (sampleRate == 14) {
4178  if (!drflac__read_uint32(bs, 16, &header->sampleRate)) {
4179  return DRFLAC_FALSE;
4180  }
4181  crc8 = drflac_crc8(crc8, header->sampleRate, 16);
4182  header->sampleRate *= 10;
4183  } else {
4184  continue; /* Invalid. Assume an invalid block. */
4185  }
4186 
4187 
4188  header->channelAssignment = channelAssignment;
4189 
4190  header->bitsPerSample = bitsPerSampleTable[bitsPerSample];
4191  if (header->bitsPerSample == 0) {
4192  header->bitsPerSample = streaminfoBitsPerSample;
4193  }
4194 
4195  if (!drflac__read_uint8(bs, 8, &header->crc8)) {
4196  return DRFLAC_FALSE;
4197  }
4198 
4199 #ifndef DR_FLAC_NO_CRC
4200  if (header->crc8 != crc8) {
4201  continue; /* CRC mismatch. Loop back to the top and find the next sync code. */
4202  }
4203 #endif
4204  return DRFLAC_TRUE;
4205  }
4206 }
4207 
4208 static drflac_bool32 drflac__read_subframe_header(drflac_bs* bs, drflac_subframe* pSubframe)
4209 {
4210  drflac_uint8 header;
4211  int type;
4212 
4213  if (!drflac__read_uint8(bs, 8, &header)) {
4214  return DRFLAC_FALSE;
4215  }
4216 
4217  /* First bit should always be 0. */
4218  if ((header & 0x80) != 0) {
4219  return DRFLAC_FALSE;
4220  }
4221 
4222  type = (header & 0x7E) >> 1;
4223  if (type == 0) {
4224  pSubframe->subframeType = DRFLAC_SUBFRAME_CONSTANT;
4225  } else if (type == 1) {
4226  pSubframe->subframeType = DRFLAC_SUBFRAME_VERBATIM;
4227  } else {
4228  if ((type & 0x20) != 0) {
4229  pSubframe->subframeType = DRFLAC_SUBFRAME_LPC;
4230  pSubframe->lpcOrder = (type & 0x1F) + 1;
4231  } else if ((type & 0x08) != 0) {
4232  pSubframe->subframeType = DRFLAC_SUBFRAME_FIXED;
4233  pSubframe->lpcOrder = (type & 0x07);
4234  if (pSubframe->lpcOrder > 4) {
4235  pSubframe->subframeType = DRFLAC_SUBFRAME_RESERVED;
4236  pSubframe->lpcOrder = 0;
4237  }
4238  } else {
4239  pSubframe->subframeType = DRFLAC_SUBFRAME_RESERVED;
4240  }
4241  }
4242 
4243  if (pSubframe->subframeType == DRFLAC_SUBFRAME_RESERVED) {
4244  return DRFLAC_FALSE;
4245  }
4246 
4247  /* Wasted bits per sample. */
4248  pSubframe->wastedBitsPerSample = 0;
4249  if ((header & 0x01) == 1) {
4250  unsigned int wastedBitsPerSample;
4251  if (!drflac__seek_past_next_set_bit(bs, &wastedBitsPerSample)) {
4252  return DRFLAC_FALSE;
4253  }
4254  pSubframe->wastedBitsPerSample = (unsigned char)wastedBitsPerSample + 1;
4255  }
4256 
4257  return DRFLAC_TRUE;
4258 }
4259 
4260 static drflac_bool32 drflac__decode_subframe(drflac_bs* bs, drflac_frame* frame, int subframeIndex, drflac_int32* pDecodedSamplesOut)
4261 {
4262  drflac_subframe* pSubframe;
4263 
4264  drflac_assert(bs != NULL);
4265  drflac_assert(frame != NULL);
4266 
4267  pSubframe = frame->subframes + subframeIndex;
4268  if (!drflac__read_subframe_header(bs, pSubframe)) {
4269  return DRFLAC_FALSE;
4270  }
4271 
4272  /* Side channels require an extra bit per sample. Took a while to figure that one out... */
4273  pSubframe->bitsPerSample = frame->header.bitsPerSample;
4274  if ((frame->header.channelAssignment == DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE || frame->header.channelAssignment == DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE) && subframeIndex == 1) {
4275  pSubframe->bitsPerSample += 1;
4276  } else if (frame->header.channelAssignment == DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE && subframeIndex == 0) {
4277  pSubframe->bitsPerSample += 1;
4278  }
4279 
4280  /* Need to handle wasted bits per sample. */
4281  if (pSubframe->wastedBitsPerSample >= pSubframe->bitsPerSample) {
4282  return DRFLAC_FALSE;
4283  }
4284  pSubframe->bitsPerSample -= pSubframe->wastedBitsPerSample;
4285  pSubframe->pDecodedSamples = pDecodedSamplesOut;
4286 
4287  switch (pSubframe->subframeType)
4288  {
4289  case DRFLAC_SUBFRAME_CONSTANT:
4290  {
4291  drflac__decode_samples__constant(bs, frame->header.blockSize, pSubframe->bitsPerSample, pSubframe->pDecodedSamples);
4292  } break;
4293 
4294  case DRFLAC_SUBFRAME_VERBATIM:
4295  {
4296  drflac__decode_samples__verbatim(bs, frame->header.blockSize, pSubframe->bitsPerSample, pSubframe->pDecodedSamples);
4297  } break;
4298 
4299  case DRFLAC_SUBFRAME_FIXED:
4300  {
4301  drflac__decode_samples__fixed(bs, frame->header.blockSize, pSubframe->bitsPerSample, pSubframe->lpcOrder, pSubframe->pDecodedSamples);
4302  } break;
4303 
4304  case DRFLAC_SUBFRAME_LPC:
4305  {
4306  drflac__decode_samples__lpc(bs, frame->header.blockSize, pSubframe->bitsPerSample, pSubframe->lpcOrder, pSubframe->pDecodedSamples);
4307  } break;
4308 
4309  default: return DRFLAC_FALSE;
4310  }
4311 
4312  return DRFLAC_TRUE;
4313 }
4314 
4315 static drflac_bool32 drflac__seek_subframe(drflac_bs* bs, drflac_frame* frame, int subframeIndex)
4316 {
4317  drflac_subframe* pSubframe;
4318 
4319  drflac_assert(bs != NULL);
4320  drflac_assert(frame != NULL);
4321 
4322  pSubframe = frame->subframes + subframeIndex;
4323  if (!drflac__read_subframe_header(bs, pSubframe)) {
4324  return DRFLAC_FALSE;
4325  }
4326 
4327  /* Side channels require an extra bit per sample. Took a while to figure that one out... */
4328  pSubframe->bitsPerSample = frame->header.bitsPerSample;
4329  if ((frame->header.channelAssignment == DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE || frame->header.channelAssignment == DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE) && subframeIndex == 1) {
4330  pSubframe->bitsPerSample += 1;
4331  } else if (frame->header.channelAssignment == DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE && subframeIndex == 0) {
4332  pSubframe->bitsPerSample += 1;
4333  }
4334 
4335  /* Need to handle wasted bits per sample. */
4336  if (pSubframe->wastedBitsPerSample >= pSubframe->bitsPerSample) {
4337  return DRFLAC_FALSE;
4338  }
4339  pSubframe->bitsPerSample -= pSubframe->wastedBitsPerSample;
4340  pSubframe->pDecodedSamples = NULL;
4341 
4342  switch (pSubframe->subframeType)
4343  {
4344  case DRFLAC_SUBFRAME_CONSTANT:
4345  {
4346  if (!drflac__seek_bits(bs, pSubframe->bitsPerSample)) {
4347  return DRFLAC_FALSE;
4348  }
4349  } break;
4350 
4351  case DRFLAC_SUBFRAME_VERBATIM:
4352  {
4353  unsigned int bitsToSeek = frame->header.blockSize * pSubframe->bitsPerSample;
4354  if (!drflac__seek_bits(bs, bitsToSeek)) {
4355  return DRFLAC_FALSE;
4356  }
4357  } break;
4358 
4359  case DRFLAC_SUBFRAME_FIXED:
4360  {
4361  unsigned int bitsToSeek = pSubframe->lpcOrder * pSubframe->bitsPerSample;
4362  if (!drflac__seek_bits(bs, bitsToSeek)) {
4363  return DRFLAC_FALSE;
4364  }
4365 
4366  if (!drflac__read_and_seek_residual(bs, frame->header.blockSize, pSubframe->lpcOrder)) {
4367  return DRFLAC_FALSE;
4368  }
4369  } break;
4370 
4371  case DRFLAC_SUBFRAME_LPC:
4372  {
4373  unsigned char lpcPrecision;
4374 
4375  unsigned int bitsToSeek = pSubframe->lpcOrder * pSubframe->bitsPerSample;
4376  if (!drflac__seek_bits(bs, bitsToSeek)) {
4377  return DRFLAC_FALSE;
4378  }
4379 
4380  if (!drflac__read_uint8(bs, 4, &lpcPrecision)) {
4381  return DRFLAC_FALSE;
4382  }
4383  if (lpcPrecision == 15) {
4384  return DRFLAC_FALSE; /* Invalid. */
4385  }
4386  lpcPrecision += 1;
4387 
4388 
4389  bitsToSeek = (pSubframe->lpcOrder * lpcPrecision) + 5; /* +5 for shift. */
4390  if (!drflac__seek_bits(bs, bitsToSeek)) {
4391  return DRFLAC_FALSE;
4392  }
4393 
4394  if (!drflac__read_and_seek_residual(bs, frame->header.blockSize, pSubframe->lpcOrder)) {
4395  return DRFLAC_FALSE;
4396  }
4397  } break;
4398 
4399  default: return DRFLAC_FALSE;
4400  }
4401 
4402  return DRFLAC_TRUE;
4403 }
4404 
4405 
4406 static DRFLAC_INLINE drflac_uint8 drflac__get_channel_count_from_channel_assignment(drflac_int8 channelAssignment)
4407 {
4408  drflac_uint8 lookup[] = {1, 2, 3, 4, 5, 6, 7, 8, 2, 2, 2};
4409 
4410  drflac_assert(channelAssignment <= 10);
4411  return lookup[channelAssignment];
4412 }
4413 
4414 static drflac_result drflac__decode_flac_frame(drflac* pFlac)
4415 {
4416  int channelCount;
4417  int i;
4418  drflac_uint8 paddingSizeInBits;
4419  drflac_uint16 desiredCRC16;
4420 #ifndef DR_FLAC_NO_CRC
4421  drflac_uint16 actualCRC16;
4422 #endif
4423 
4424  /* This function should be called while the stream is sitting on the first byte after the frame header. */
4425  drflac_zero_memory(pFlac->currentFrame.subframes, sizeof(pFlac->currentFrame.subframes));
4426 
4427  /* The frame block size must never be larger than the maximum block size defined by the FLAC stream. */
4428  if (pFlac->currentFrame.header.blockSize > pFlac->maxBlockSize) {
4429  return DRFLAC_ERROR;
4430  }
4431 
4432  /* The number of channels in the frame must match the channel count from the STREAMINFO block. */
4433  channelCount = drflac__get_channel_count_from_channel_assignment(pFlac->currentFrame.header.channelAssignment);
4434  if (channelCount != (int)pFlac->channels) {
4435  return DRFLAC_ERROR;
4436  }
4437 
4438  for (i = 0; i < channelCount; ++i) {
4439  if (!drflac__decode_subframe(&pFlac->bs, &pFlac->currentFrame, i, pFlac->pDecodedSamples + ((pFlac->currentFrame.header.blockSize+DRFLAC_LEADING_SAMPLES) * i) + DRFLAC_LEADING_SAMPLES)) {
4440  return DRFLAC_ERROR;
4441  }
4442  }
4443 
4444  paddingSizeInBits = DRFLAC_CACHE_L1_BITS_REMAINING(&pFlac->bs) & 7;
4445  if (paddingSizeInBits > 0) {
4446  drflac_uint8 padding = 0;
4447  if (!drflac__read_uint8(&pFlac->bs, paddingSizeInBits, &padding)) {
4448  return DRFLAC_END_OF_STREAM;
4449  }
4450  }
4451 
4452 #ifndef DR_FLAC_NO_CRC
4453  actualCRC16 = drflac__flush_crc16(&pFlac->bs);
4454 #endif
4455  if (!drflac__read_uint16(&pFlac->bs, 16, &desiredCRC16)) {
4456  return DRFLAC_END_OF_STREAM;
4457  }
4458 
4459 #ifndef DR_FLAC_NO_CRC
4460  if (actualCRC16 != desiredCRC16) {
4461  return DRFLAC_CRC_MISMATCH; /* CRC mismatch. */
4462  }
4463 #endif
4464 
4465  pFlac->currentFrame.samplesRemaining = pFlac->currentFrame.header.blockSize * channelCount;
4466 
4467  return DRFLAC_SUCCESS;
4468 }
4469 
4470 static drflac_result drflac__seek_flac_frame(drflac* pFlac)
4471 {
4472  int channelCount;
4473  int i;
4474  drflac_uint16 desiredCRC16;
4475 #ifndef DR_FLAC_NO_CRC
4476  drflac_uint16 actualCRC16;
4477 #endif
4478 
4479  channelCount = drflac__get_channel_count_from_channel_assignment(pFlac->currentFrame.header.channelAssignment);
4480  for (i = 0; i < channelCount; ++i) {
4481  if (!drflac__seek_subframe(&pFlac->bs, &pFlac->currentFrame, i)) {
4482  return DRFLAC_ERROR;
4483  }
4484  }
4485 
4486  /* Padding. */
4487  if (!drflac__seek_bits(&pFlac->bs, DRFLAC_CACHE_L1_BITS_REMAINING(&pFlac->bs) & 7)) {
4488  return DRFLAC_ERROR;
4489  }
4490 
4491  /* CRC. */
4492 #ifndef DR_FLAC_NO_CRC
4493  actualCRC16 = drflac__flush_crc16(&pFlac->bs);
4494 #endif
4495  if (!drflac__read_uint16(&pFlac->bs, 16, &desiredCRC16)) {
4496  return DRFLAC_END_OF_STREAM;
4497  }
4498 
4499 #ifndef DR_FLAC_NO_CRC
4500  if (actualCRC16 != desiredCRC16) {
4501  return DRFLAC_CRC_MISMATCH; /* CRC mismatch. */
4502  }
4503 #endif
4504 
4505  return DRFLAC_SUCCESS;
4506 }
4507 
4508 static drflac_bool32 drflac__read_and_decode_next_flac_frame(drflac* pFlac)
4509 {
4510  drflac_assert(pFlac != NULL);
4511 
4512  for (;;) {
4513  drflac_result result;
4514 
4515  if (!drflac__read_next_flac_frame_header(&pFlac->bs, pFlac->bitsPerSample, &pFlac->currentFrame.header)) {
4516  return DRFLAC_FALSE;
4517  }
4518 
4519  result = drflac__decode_flac_frame(pFlac);
4520  if (result != DRFLAC_SUCCESS) {
4521  if (result == DRFLAC_CRC_MISMATCH) {
4522  continue; /* CRC mismatch. Skip to the next frame. */
4523  } else {
4524  return DRFLAC_FALSE;
4525  }
4526  }
4527 
4528  return DRFLAC_TRUE;
4529  }
4530 }
4531 
4532 
4533 static void drflac__get_current_frame_sample_range(drflac* pFlac, drflac_uint64* pFirstSampleInFrameOut, drflac_uint64* pLastSampleInFrameOut)
4534 {
4535  unsigned int channelCount;
4536  drflac_uint64 firstSampleInFrame;
4537  drflac_uint64 lastSampleInFrame;
4538 
4539  drflac_assert(pFlac != NULL);
4540 
4541  channelCount = drflac__get_channel_count_from_channel_assignment(pFlac->currentFrame.header.channelAssignment);
4542 
4543  firstSampleInFrame = pFlac->currentFrame.header.sampleNumber*channelCount;
4544  if (firstSampleInFrame == 0) {
4545  firstSampleInFrame = pFlac->currentFrame.header.frameNumber * pFlac->maxBlockSize*channelCount;
4546  }
4547 
4548  lastSampleInFrame = firstSampleInFrame + (pFlac->currentFrame.header.blockSize*channelCount);
4549  if (lastSampleInFrame > 0) {
4550  lastSampleInFrame -= 1; /* Needs to be zero based. */
4551  }
4552 
4553  if (pFirstSampleInFrameOut) {
4554  *pFirstSampleInFrameOut = firstSampleInFrame;
4555  }
4556  if (pLastSampleInFrameOut) {
4557  *pLastSampleInFrameOut = lastSampleInFrame;
4558  }
4559 }
4560 
4561 /* This function will be replacing drflac__get_current_frame_sample_range(), but it's not currently used so I have commented it out to silence a compiler warning. */
4562 #if 0
4563 static void drflac__get_pcm_frame_range_of_current_flac_frame(drflac* pFlac, drflac_uint64* pFirstPCMFrame, drflac_uint64* pLastPCMFrame)
4564 {
4565  drflac_uint64 firstPCMFrame;
4566  drflac_uint64 lastPCMFrame;
4567 
4568  drflac_assert(pFlac != NULL);
4569 
4570  firstPCMFrame = pFlac->currentFrame.header.sampleNumber;
4571  if (firstPCMFrame == 0) {
4572  firstPCMFrame = pFlac->currentFrame.header.frameNumber * pFlac->maxBlockSize;
4573  }
4574 
4575  lastPCMFrame = firstPCMFrame + (pFlac->currentFrame.header.blockSize);
4576  if (lastPCMFrame > 0) {
4577  lastPCMFrame -= 1; /* Needs to be zero based. */
4578  }
4579 
4580  if (pFirstPCMFrame) {
4581  *pFirstPCMFrame = firstPCMFrame;
4582  }
4583  if (pLastPCMFrame) {
4584  *pLastPCMFrame = lastPCMFrame;
4585  }
4586 }
4587 #endif
4588 
4589 static drflac_bool32 drflac__seek_to_first_frame(drflac* pFlac)
4590 {
4591  drflac_bool32 result;
4592 
4593  drflac_assert(pFlac != NULL);
4594 
4595  result = drflac__seek_to_byte(&pFlac->bs, pFlac->firstFramePos);
4596 
4597  drflac_zero_memory(&pFlac->currentFrame, sizeof(pFlac->currentFrame));
4598  pFlac->currentSample = 0;
4599 
4600  return result;
4601 }
4602 
4603 static DRFLAC_INLINE drflac_result drflac__seek_to_next_flac_frame(drflac* pFlac)
4604 {
4605  /* This function should only ever be called while the decoder is sitting on the first byte past the FRAME_HEADER section. */
4606  drflac_assert(pFlac != NULL);
4607  return drflac__seek_flac_frame(pFlac);
4608 }
4609 
4610 drflac_uint64 drflac__seek_forward_by_samples(drflac* pFlac, drflac_uint64 samplesToRead)
4611 {
4612  drflac_uint64 samplesRead = 0;
4613  while (samplesToRead > 0) {
4614  if (pFlac->currentFrame.samplesRemaining == 0) {
4615  if (!drflac__read_and_decode_next_flac_frame(pFlac)) {
4616  break; /* Couldn't read the next frame, so just break from the loop and return. */
4617  }
4618  } else {
4619  if (pFlac->currentFrame.samplesRemaining > samplesToRead) {
4620  samplesRead += samplesToRead;
4621  pFlac->currentFrame.samplesRemaining -= (drflac_uint32)samplesToRead; /* <-- Safe cast. Will always be < currentFrame.samplesRemaining < 65536. */
4622  samplesToRead = 0;
4623  } else {
4624  samplesRead += pFlac->currentFrame.samplesRemaining;
4625  samplesToRead -= pFlac->currentFrame.samplesRemaining;
4626  pFlac->currentFrame.samplesRemaining = 0;
4627  }
4628  }
4629  }
4630 
4631  pFlac->currentSample += samplesRead;
4632  return samplesRead;
4633 }
4634 
4635 drflac_uint64 drflac__seek_forward_by_pcm_frames(drflac* pFlac, drflac_uint64 pcmFramesToSeek)
4636 {
4637  return drflac__seek_forward_by_samples(pFlac, pcmFramesToSeek*pFlac->channels);
4638 }
4639 
4640 static drflac_bool32 drflac__seek_to_sample__brute_force(drflac* pFlac, drflac_uint64 sampleIndex)
4641 {
4642  drflac_bool32 isMidFrame = DRFLAC_FALSE;
4643  drflac_uint64 runningSampleCount;
4644 
4645  drflac_assert(pFlac != NULL);
4646 
4647  /* If we are seeking forward we start from the current position. Otherwise we need to start all the way from the start of the file. */
4648  if (sampleIndex >= pFlac->currentSample) {
4649  /* Seeking forward. Need to seek from the current position. */
4650  runningSampleCount = pFlac->currentSample;
4651 
4652  /* The frame header for the first frame may not yet have been read. We need to do that if necessary. */
4653  if (pFlac->currentSample == 0 && pFlac->currentFrame.samplesRemaining == 0) {
4654  if (!drflac__read_next_flac_frame_header(&pFlac->bs, pFlac->bitsPerSample, &pFlac->currentFrame.header)) {
4655  return DRFLAC_FALSE;
4656  }
4657  } else {
4658  isMidFrame = DRFLAC_TRUE;
4659  }
4660  } else {
4661  /* Seeking backwards. Need to seek from the start of the file. */
4662  runningSampleCount = 0;
4663 
4664  /* Move back to the start. */
4665  if (!drflac__seek_to_first_frame(pFlac)) {
4666  return DRFLAC_FALSE;
4667  }
4668 
4669  /* Decode the first frame in preparation for sample-exact seeking below. */
4670  if (!drflac__read_next_flac_frame_header(&pFlac->bs, pFlac->bitsPerSample, &pFlac->currentFrame.header)) {
4671  return DRFLAC_FALSE;
4672  }
4673  }
4674 
4675  /*
4676  We need to as quickly as possible find the frame that contains the target sample. To do this, we iterate over each frame and inspect its
4677  header. If based on the header we can determine that the frame contains the sample, we do a full decode of that frame.
4678  */
4679  for (;;) {
4680  drflac_uint64 sampleCountInThisFrame;
4681  drflac_uint64 firstSampleInFrame = 0;
4682  drflac_uint64 lastSampleInFrame = 0;
4683 
4684  drflac__get_current_frame_sample_range(pFlac, &firstSampleInFrame, &lastSampleInFrame);
4685 
4686  sampleCountInThisFrame = (lastSampleInFrame - firstSampleInFrame) + 1;
4687  if (sampleIndex < (runningSampleCount + sampleCountInThisFrame)) {
4688  /*
4689  The sample should be in this frame. We need to fully decode it, however if it's an invalid frame (a CRC mismatch), we need to pretend
4690  it never existed and keep iterating.
4691  */
4692  drflac_uint64 samplesToDecode = sampleIndex - runningSampleCount;
4693 
4694  if (!isMidFrame) {
4695  drflac_result result = drflac__decode_flac_frame(pFlac);
4696  if (result == DRFLAC_SUCCESS) {
4697  /* The frame is valid. We just need to skip over some samples to ensure it's sample-exact. */
4698  return drflac__seek_forward_by_samples(pFlac, samplesToDecode) == samplesToDecode; /* <-- If this fails, something bad has happened (it should never fail). */
4699  } else {
4700  if (result == DRFLAC_CRC_MISMATCH) {
4701  goto next_iteration; /* CRC mismatch. Pretend this frame never existed. */
4702  } else {
4703  return DRFLAC_FALSE;
4704  }
4705  }
4706  } else {
4707  /* We started seeking mid-frame which means we need to skip the frame decoding part. */
4708  return drflac__seek_forward_by_samples(pFlac, samplesToDecode) == samplesToDecode;
4709  }
4710  } else {
4711  /*
4712  It's not in this frame. We need to seek past the frame, but check if there was a CRC mismatch. If so, we pretend this
4713  frame never existed and leave the running sample count untouched.
4714  */
4715  if (!isMidFrame) {
4716  drflac_result result = drflac__seek_to_next_flac_frame(pFlac);
4717  if (result == DRFLAC_SUCCESS) {
4718  runningSampleCount += sampleCountInThisFrame;
4719  } else {
4720  if (result == DRFLAC_CRC_MISMATCH) {
4721  goto next_iteration; /* CRC mismatch. Pretend this frame never existed. */
4722  } else {
4723  return DRFLAC_FALSE;
4724  }
4725  }
4726  } else {
4727  /*
4728  We started seeking mid-frame which means we need to seek by reading to the end of the frame instead of with
4729  drflac__seek_to_next_flac_frame() which only works if the decoder is sitting on the byte just after the frame header.
4730  */
4731  runningSampleCount += pFlac->currentFrame.samplesRemaining;
4732  pFlac->currentFrame.samplesRemaining = 0;
4733  isMidFrame = DRFLAC_FALSE;
4734  }
4735  }
4736 
4737  next_iteration:
4738  /* Grab the next frame in preparation for the next iteration. */
4739  if (!drflac__read_next_flac_frame_header(&pFlac->bs, pFlac->bitsPerSample, &pFlac->currentFrame.header)) {
4740  return DRFLAC_FALSE;
4741  }
4742  }
4743 }
4744 
4745 
4746 static drflac_bool32 drflac__seek_to_sample__seek_table(drflac* pFlac, drflac_uint64 sampleIndex)
4747 {
4748  drflac_uint32 iClosestSeekpoint = 0;
4749  drflac_bool32 isMidFrame = DRFLAC_FALSE;
4750  drflac_uint64 runningSampleCount;
4751  drflac_uint32 iSeekpoint;
4752 
4753  drflac_assert(pFlac != NULL);
4754 
4755  if (pFlac->pSeekpoints == NULL || pFlac->seekpointCount == 0) {
4756  return DRFLAC_FALSE;
4757  }
4758 
4759  for (iSeekpoint = 0; iSeekpoint < pFlac->seekpointCount; ++iSeekpoint) {
4760  if (pFlac->pSeekpoints[iSeekpoint].firstSample*pFlac->channels >= sampleIndex) {
4761  break;
4762  }
4763 
4764  iClosestSeekpoint = iSeekpoint;
4765  }
4766 
4767  /*
4768  At this point we should have found the seekpoint closest to our sample. If we are seeking forward and the closest seekpoint is _before_ the current sample, we
4769  just seek forward from where we are. Otherwise we start seeking from the seekpoint's first sample.
4770  */
4771  if ((sampleIndex >= pFlac->currentSample) && (pFlac->pSeekpoints[iClosestSeekpoint].firstSample*pFlac->channels <= pFlac->currentSample)) {
4772  /* Optimized case. Just seek forward from where we are. */
4773  runningSampleCount = pFlac->currentSample;
4774 
4775  /* The frame header for the first frame may not yet have been read. We need to do that if necessary. */
4776  if (pFlac->currentSample == 0 && pFlac->currentFrame.samplesRemaining == 0) {
4777  if (!drflac__read_next_flac_frame_header(&pFlac->bs, pFlac->bitsPerSample, &pFlac->currentFrame.header)) {
4778  return DRFLAC_FALSE;
4779  }
4780  } else {
4781  isMidFrame = DRFLAC_TRUE;
4782  }
4783  } else {
4784  /* Slower case. Seek to the start of the seekpoint and then seek forward from there. */
4785  runningSampleCount = pFlac->pSeekpoints[iClosestSeekpoint].firstSample*pFlac->channels;
4786 
4787  if (!drflac__seek_to_byte(&pFlac->bs, pFlac->firstFramePos + pFlac->pSeekpoints[iClosestSeekpoint].frameOffset)) {
4788  return DRFLAC_FALSE;
4789  }
4790 
4791  /* Grab the frame the seekpoint is sitting on in preparation for the sample-exact seeking below. */
4792  if (!drflac__read_next_flac_frame_header(&pFlac->bs, pFlac->bitsPerSample, &pFlac->currentFrame.header)) {
4793  return DRFLAC_FALSE;
4794  }
4795  }
4796 
4797  for (;;) {
4798  drflac_uint64 sampleCountInThisFrame;
4799  drflac_uint64 firstSampleInFrame = 0;
4800  drflac_uint64 lastSampleInFrame = 0;
4801  drflac__get_current_frame_sample_range(pFlac, &firstSampleInFrame, &lastSampleInFrame);
4802 
4803  sampleCountInThisFrame = (lastSampleInFrame - firstSampleInFrame) + 1;
4804  if (sampleIndex < (runningSampleCount + sampleCountInThisFrame)) {
4805  /*
4806  The sample should be in this frame. We need to fully decode it, but if it's an invalid frame (a CRC mismatch) we need to pretend
4807  it never existed and keep iterating.
4808  */
4809  drflac_uint64 samplesToDecode = sampleIndex - runningSampleCount;
4810 
4811  if (!isMidFrame) {
4812  drflac_result result = drflac__decode_flac_frame(pFlac);
4813  if (result == DRFLAC_SUCCESS) {
4814  /* The frame is valid. We just need to skip over some samples to ensure it's sample-exact. */
4815  return drflac__seek_forward_by_samples(pFlac, samplesToDecode) == samplesToDecode; /* <-- If this fails, something bad has happened (it should never fail). */
4816  } else {
4817  if (result == DRFLAC_CRC_MISMATCH) {
4818  goto next_iteration; /* CRC mismatch. Pretend this frame never existed. */
4819  } else {
4820  return DRFLAC_FALSE;
4821  }
4822  }
4823  } else {
4824  /* We started seeking mid-frame which means we need to skip the frame decoding part. */
4825  return drflac__seek_forward_by_samples(pFlac, samplesToDecode) == samplesToDecode;
4826  }
4827  } else {
4828  /*
4829  It's not in this frame. We need to seek past the frame, but check if there was a CRC mismatch. If so, we pretend this
4830  frame never existed and leave the running sample count untouched.
4831  */
4832  if (!isMidFrame) {
4833  drflac_result result = drflac__seek_to_next_flac_frame(pFlac);
4834  if (result == DRFLAC_SUCCESS) {
4835  runningSampleCount += sampleCountInThisFrame;
4836  } else {
4837  if (result == DRFLAC_CRC_MISMATCH) {
4838  goto next_iteration; /* CRC mismatch. Pretend this frame never existed. */
4839  } else {
4840  return DRFLAC_FALSE;
4841  }
4842  }
4843  } else {
4844  /*
4845  We started seeking mid-frame which means we need to seek by reading to the end of the frame instead of with
4846  drflac__seek_to_next_flac_frame() which only works if the decoder is sitting on the byte just after the frame header.
4847  */
4848  runningSampleCount += pFlac->currentFrame.samplesRemaining;
4849  pFlac->currentFrame.samplesRemaining = 0;
4850  isMidFrame = DRFLAC_FALSE;
4851  }
4852  }
4853 
4854  next_iteration:
4855  /* Grab the next frame in preparation for the next iteration. */
4856  if (!drflac__read_next_flac_frame_header(&pFlac->bs, pFlac->bitsPerSample, &pFlac->currentFrame.header)) {
4857  return DRFLAC_FALSE;
4858  }
4859  }
4860 }
4861 
4862 
4863 #ifndef DR_FLAC_NO_OGG
4864 typedef struct
4865 {
4866  drflac_uint8 capturePattern[4]; /* Should be "OggS" */
4867  drflac_uint8 structureVersion; /* Always 0. */
4868  drflac_uint8 headerType;
4869  drflac_uint64 granulePosition;
4870  drflac_uint32 serialNumber;
4871  drflac_uint32 sequenceNumber;
4872  drflac_uint32 checksum;
4873  drflac_uint8 segmentCount;
4874  drflac_uint8 segmentTable[255];
4875 } drflac_ogg_page_header;
4876 #endif
4877 
4878 typedef struct
4879 {
4880  drflac_read_proc onRead;
4881  drflac_seek_proc onSeek;
4882  drflac_meta_proc onMeta;
4883  drflac_container container;
4884  void* pUserData;
4885  void* pUserDataMD;
4886  drflac_uint32 sampleRate;
4888  drflac_uint8 bitsPerSample;
4889  drflac_uint64 totalSampleCount;
4890  drflac_uint16 maxBlockSize;
4891  drflac_uint64 runningFilePos;
4892  drflac_bool32 hasStreamInfoBlock;
4893  drflac_bool32 hasMetadataBlocks;
4894  drflac_bs bs; /* <-- A bit streamer is required for loading data during initialization. */
4895  drflac_frame_header firstFrameHeader; /* <-- The header of the first frame that was read during relaxed initalization. Only set if there is no STREAMINFO block. */
4896 
4897 #ifndef DR_FLAC_NO_OGG
4898  drflac_uint32 oggSerial;
4899  drflac_uint64 oggFirstBytePos;
4900  drflac_ogg_page_header oggBosHeader;
4901 #endif
4902 } drflac_init_info;
4903 
4904 static DRFLAC_INLINE void drflac__decode_block_header(drflac_uint32 blockHeader, drflac_uint8* isLastBlock, drflac_uint8* blockType, drflac_uint32* blockSize)
4905 {
4906  blockHeader = drflac__be2host_32(blockHeader);
4907  *isLastBlock = (blockHeader & 0x80000000UL) >> 31;
4908  *blockType = (blockHeader & 0x7F000000UL) >> 24;
4909  *blockSize = (blockHeader & 0x00FFFFFFUL);
4910 }
4911 
4912 static DRFLAC_INLINE drflac_bool32 drflac__read_and_decode_block_header(drflac_read_proc onRead, void* pUserData, drflac_uint8* isLastBlock, drflac_uint8* blockType, drflac_uint32* blockSize)
4913 {
4914  drflac_uint32 blockHeader;
4915  if (onRead(pUserData, &blockHeader, 4) != 4) {
4916  return DRFLAC_FALSE;
4917  }
4918 
4919  drflac__decode_block_header(blockHeader, isLastBlock, blockType, blockSize);
4920  return DRFLAC_TRUE;
4921 }
4922 
4923 drflac_bool32 drflac__read_streaminfo(drflac_read_proc onRead, void* pUserData, drflac_streaminfo* pStreamInfo)
4924 {
4925  drflac_uint32 blockSizes;
4926  drflac_uint64 frameSizes = 0;
4927  drflac_uint64 importantProps;
4928  drflac_uint8 md5[16];
4929 
4930  /* min/max block size. */
4931  if (onRead(pUserData, &blockSizes, 4) != 4) {
4932  return DRFLAC_FALSE;
4933  }
4934 
4935  /* min/max frame size. */
4936  if (onRead(pUserData, &frameSizes, 6) != 6) {
4937  return DRFLAC_FALSE;
4938  }
4939 
4940  /* Sample rate, channels, bits per sample and total sample count. */
4941  if (onRead(pUserData, &importantProps, 8) != 8) {
4942  return DRFLAC_FALSE;
4943  }
4944 
4945  /* MD5 */
4946  if (onRead(pUserData, md5, sizeof(md5)) != sizeof(md5)) {
4947  return DRFLAC_FALSE;
4948  }
4949 
4950  blockSizes = drflac__be2host_32(blockSizes);
4951  frameSizes = drflac__be2host_64(frameSizes);
4952  importantProps = drflac__be2host_64(importantProps);
4953 
4954  pStreamInfo->minBlockSize = (blockSizes & 0xFFFF0000) >> 16;
4955  pStreamInfo->maxBlockSize = (blockSizes & 0x0000FFFF);
4956  pStreamInfo->minFrameSize = (drflac_uint32)((frameSizes & (((drflac_uint64)0x00FFFFFF << 16) << 24)) >> 40);
4957  pStreamInfo->maxFrameSize = (drflac_uint32)((frameSizes & (((drflac_uint64)0x00FFFFFF << 16) << 0)) >> 16);
4958  pStreamInfo->sampleRate = (drflac_uint32)((importantProps & (((drflac_uint64)0x000FFFFF << 16) << 28)) >> 44);
4959  pStreamInfo->channels = (drflac_uint8 )((importantProps & (((drflac_uint64)0x0000000E << 16) << 24)) >> 41) + 1;
4960  pStreamInfo->bitsPerSample = (drflac_uint8 )((importantProps & (((drflac_uint64)0x0000001F << 16) << 20)) >> 36) + 1;
4961  pStreamInfo->totalSampleCount = ((importantProps & ((((drflac_uint64)0x0000000F << 16) << 16) | 0xFFFFFFFF))) * pStreamInfo->channels;
4962  drflac_copy_memory(pStreamInfo->md5, md5, sizeof(md5));
4963 
4964  return DRFLAC_TRUE;
4965 }
4966 
4967 drflac_bool32 drflac__read_and_decode_metadata(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_meta_proc onMeta, void* pUserData, void* pUserDataMD, drflac_uint64* pFirstFramePos, drflac_uint64* pSeektablePos, drflac_uint32* pSeektableSize)
4968 {
4969  /*
4970  We want to keep track of the byte position in the stream of the seektable. At the time of calling this function we know that
4971  we'll be sitting on byte 42.
4972  */
4973  drflac_uint64 runningFilePos = 42;
4974  drflac_uint64 seektablePos = 0;
4975  drflac_uint32 seektableSize = 0;
4976 
4977  for (;;) {
4978  drflac_metadata metadata;
4979  drflac_uint8 isLastBlock = 0;
4980  drflac_uint8 blockType;
4981  drflac_uint32 blockSize;
4982  if (!drflac__read_and_decode_block_header(onRead, pUserData, &isLastBlock, &blockType, &blockSize)) {
4983  return DRFLAC_FALSE;
4984  }
4985  runningFilePos += 4;
4986 
4987  metadata.type = blockType;
4988  metadata.pRawData = NULL;
4989  metadata.rawDataSize = 0;
4990 
4991  switch (blockType)
4992  {
4994  {
4995  if (blockSize < 4) {
4996  return DRFLAC_FALSE;
4997  }
4998 
4999  if (onMeta) {
5000  void* pRawData = DRFLAC_MALLOC(blockSize);
5001  if (pRawData == NULL) {
5002  return DRFLAC_FALSE;
5003  }
5004 
5005  if (onRead(pUserData, pRawData, blockSize) != blockSize) {
5006  DRFLAC_FREE(pRawData);
5007  return DRFLAC_FALSE;
5008  }
5009 
5010  metadata.pRawData = pRawData;
5011  metadata.rawDataSize = blockSize;
5012  metadata.data.application.id = drflac__be2host_32(*(drflac_uint32*)pRawData);
5013  metadata.data.application.pData = (const void*)((drflac_uint8*)pRawData + sizeof(drflac_uint32));
5014  metadata.data.application.dataSize = blockSize - sizeof(drflac_uint32);
5015  onMeta(pUserDataMD, &metadata);
5016 
5017  DRFLAC_FREE(pRawData);
5018  }
5019  } break;
5020 
5022  {
5023  seektablePos = runningFilePos;
5024  seektableSize = blockSize;
5025 
5026  if (onMeta) {
5027  drflac_uint32 iSeekpoint;
5028  void* pRawData;
5029 
5030  pRawData = DRFLAC_MALLOC(blockSize);
5031  if (pRawData == NULL) {
5032  return DRFLAC_FALSE;
5033  }
5034 
5035  if (onRead(pUserData, pRawData, blockSize) != blockSize) {
5036  DRFLAC_FREE(pRawData);
5037  return DRFLAC_FALSE;
5038  }
5039 
5040  metadata.pRawData = pRawData;
5041  metadata.rawDataSize = blockSize;
5042  metadata.data.seektable.seekpointCount = blockSize/sizeof(drflac_seekpoint);
5043  metadata.data.seektable.pSeekpoints = (const drflac_seekpoint*)pRawData;
5044 
5045  /* Endian swap. */
5046  for (iSeekpoint = 0; iSeekpoint < metadata.data.seektable.seekpointCount; ++iSeekpoint) {
5047  drflac_seekpoint* pSeekpoint = (drflac_seekpoint*)pRawData + iSeekpoint;
5048  pSeekpoint->firstSample = drflac__be2host_64(pSeekpoint->firstSample);
5049  pSeekpoint->frameOffset = drflac__be2host_64(pSeekpoint->frameOffset);
5050  pSeekpoint->sampleCount = drflac__be2host_16(pSeekpoint->sampleCount);
5051  }
5052 
5053  onMeta(pUserDataMD, &metadata);
5054 
5055  DRFLAC_FREE(pRawData);
5056  }
5057  } break;
5058 
5060  {
5061  if (blockSize < 8) {
5062  return DRFLAC_FALSE;
5063  }
5064 
5065  if (onMeta) {
5066  void* pRawData;
5067  const char* pRunningData;
5068  const char* pRunningDataEnd;
5069  drflac_uint32 i;
5070 
5071  pRawData = DRFLAC_MALLOC(blockSize);
5072  if (pRawData == NULL) {
5073  return DRFLAC_FALSE;
5074  }
5075 
5076  if (onRead(pUserData, pRawData, blockSize) != blockSize) {
5077  DRFLAC_FREE(pRawData);
5078  return DRFLAC_FALSE;
5079  }
5080 
5081  metadata.pRawData = pRawData;
5082  metadata.rawDataSize = blockSize;
5083 
5084  pRunningData = (const char*)pRawData;
5085  pRunningDataEnd = (const char*)pRawData + blockSize;
5086 
5087  metadata.data.vorbis_comment.vendorLength = drflac__le2host_32(*(const drflac_uint32*)pRunningData); pRunningData += 4;
5088 
5089  /* Need space for the rest of the block */
5090  if ((pRunningDataEnd - pRunningData) - 4 < (drflac_int64)metadata.data.vorbis_comment.vendorLength) { /* <-- Note the order of operations to avoid overflow to a valid value */
5091  DRFLAC_FREE(pRawData);
5092  return DRFLAC_FALSE;
5093  }
5094  metadata.data.vorbis_comment.vendor = pRunningData; pRunningData += metadata.data.vorbis_comment.vendorLength;
5095  metadata.data.vorbis_comment.commentCount = drflac__le2host_32(*(const drflac_uint32*)pRunningData); pRunningData += 4;
5096 
5097  /* Need space for 'commentCount' comments after the block, which at minimum is a drflac_uint32 per comment */
5098  if ((pRunningDataEnd - pRunningData) / sizeof(drflac_uint32) < metadata.data.vorbis_comment.commentCount) { /* <-- Note the order of operations to avoid overflow to a valid value */
5099  DRFLAC_FREE(pRawData);
5100  return DRFLAC_FALSE;
5101  }
5102  metadata.data.vorbis_comment.pComments = pRunningData;
5103 
5104  /* Check that the comments section is valid before passing it to the callback */
5105  for (i = 0; i < metadata.data.vorbis_comment.commentCount; ++i) {
5106  drflac_uint32 commentLength;
5107 
5108  if (pRunningDataEnd - pRunningData < 4) {
5109  DRFLAC_FREE(pRawData);
5110  return DRFLAC_FALSE;
5111  }
5112 
5113  commentLength = drflac__le2host_32(*(const drflac_uint32*)pRunningData); pRunningData += 4;
5114  if (pRunningDataEnd - pRunningData < (drflac_int64)commentLength) { /* <-- Note the order of operations to avoid overflow to a valid value */
5115  DRFLAC_FREE(pRawData);
5116  return DRFLAC_FALSE;
5117  }
5118  pRunningData += commentLength;
5119  }
5120 
5121  onMeta(pUserDataMD, &metadata);
5122 
5123  DRFLAC_FREE(pRawData);
5124  }
5125  } break;
5126 
5128  {
5129  if (blockSize < 396) {
5130  return DRFLAC_FALSE;
5131  }
5132 
5133  if (onMeta) {
5134  void* pRawData;
5135  const char* pRunningData;
5136  const char* pRunningDataEnd;
5137  drflac_uint8 iTrack;
5138  drflac_uint8 iIndex;
5139 
5140  pRawData = DRFLAC_MALLOC(blockSize);
5141  if (pRawData == NULL) {
5142  return DRFLAC_FALSE;
5143  }
5144 
5145  if (onRead(pUserData, pRawData, blockSize) != blockSize) {
5146  DRFLAC_FREE(pRawData);
5147  return DRFLAC_FALSE;
5148  }
5149 
5150  metadata.pRawData = pRawData;
5151  metadata.rawDataSize = blockSize;
5152 
5153  pRunningData = (const char*)pRawData;
5154  pRunningDataEnd = (const char*)pRawData + blockSize;
5155 
5156  drflac_copy_memory(metadata.data.cuesheet.catalog, pRunningData, 128); pRunningData += 128;
5157  metadata.data.cuesheet.leadInSampleCount = drflac__be2host_64(*(const drflac_uint64*)pRunningData); pRunningData += 8;
5158  metadata.data.cuesheet.isCD = (pRunningData[0] & 0x80) != 0; pRunningData += 259;
5159  metadata.data.cuesheet.trackCount = pRunningData[0]; pRunningData += 1;
5160  metadata.data.cuesheet.pTrackData = pRunningData;
5161 
5162  /* Check that the cuesheet tracks are valid before passing it to the callback */
5163  for (iTrack = 0; iTrack < metadata.data.cuesheet.trackCount; ++iTrack) {
5164  drflac_uint8 indexCount;
5165  drflac_uint32 indexPointSize;
5166 
5167  if (pRunningDataEnd - pRunningData < 36) {
5168  DRFLAC_FREE(pRawData);
5169  return DRFLAC_FALSE;
5170  }
5171 
5172  /* Skip to the index point count */
5173  pRunningData += 35;
5174  indexCount = pRunningData[0]; pRunningData += 1;
5175  indexPointSize = indexCount * sizeof(drflac_cuesheet_track_index);
5176  if (pRunningDataEnd - pRunningData < (drflac_int64)indexPointSize) {
5177  DRFLAC_FREE(pRawData);
5178  return DRFLAC_FALSE;
5179  }
5180 
5181  /* Endian swap. */
5182  for (iIndex = 0; iIndex < indexCount; ++iIndex) {
5184  pRunningData += sizeof(drflac_cuesheet_track_index);
5185  pTrack->offset = drflac__be2host_64(pTrack->offset);
5186  }
5187  }
5188 
5189  onMeta(pUserDataMD, &metadata);
5190 
5191  DRFLAC_FREE(pRawData);
5192  }
5193  } break;
5194 
5196  {
5197  if (blockSize < 32) {
5198  return DRFLAC_FALSE;
5199  }
5200 
5201  if (onMeta) {
5202  void* pRawData;
5203  const char* pRunningData;
5204  const char* pRunningDataEnd;
5205 
5206  pRawData = DRFLAC_MALLOC(blockSize);
5207  if (pRawData == NULL) {
5208  return DRFLAC_FALSE;
5209  }
5210 
5211  if (onRead(pUserData, pRawData, blockSize) != blockSize) {
5212  DRFLAC_FREE(pRawData);
5213  return DRFLAC_FALSE;
5214  }
5215 
5216  metadata.pRawData = pRawData;
5217  metadata.rawDataSize = blockSize;
5218 
5219  pRunningData = (const char*)pRawData;
5220  pRunningDataEnd = (const char*)pRawData + blockSize;
5221 
5222  metadata.data.picture.type = drflac__be2host_32(*(const drflac_uint32*)pRunningData); pRunningData += 4;
5223  metadata.data.picture.mimeLength = drflac__be2host_32(*(const drflac_uint32*)pRunningData); pRunningData += 4;
5224 
5225  /* Need space for the rest of the block */
5226  if ((pRunningDataEnd - pRunningData) - 24 < (drflac_int64)metadata.data.picture.mimeLength) { /* <-- Note the order of operations to avoid overflow to a valid value */
5227  DRFLAC_FREE(pRawData);
5228  return DRFLAC_FALSE;
5229  }
5230  metadata.data.picture.mime = pRunningData; pRunningData += metadata.data.picture.mimeLength;
5231  metadata.data.picture.descriptionLength = drflac__be2host_32(*(const drflac_uint32*)pRunningData); pRunningData += 4;
5232 
5233  /* Need space for the rest of the block */
5234  if ((pRunningDataEnd - pRunningData) - 20 < (drflac_int64)metadata.data.picture.descriptionLength) { /* <-- Note the order of operations to avoid overflow to a valid value */
5235  DRFLAC_FREE(pRawData);
5236  return DRFLAC_FALSE;
5237  }
5238  metadata.data.picture.description = pRunningData; pRunningData += metadata.data.picture.descriptionLength;
5239  metadata.data.picture.width = drflac__be2host_32(*(const drflac_uint32*)pRunningData); pRunningData += 4;
5240  metadata.data.picture.height = drflac__be2host_32(*(const drflac_uint32*)pRunningData); pRunningData += 4;
5241  metadata.data.picture.colorDepth = drflac__be2host_32(*(const drflac_uint32*)pRunningData); pRunningData += 4;
5242  metadata.data.picture.indexColorCount = drflac__be2host_32(*(const drflac_uint32*)pRunningData); pRunningData += 4;
5243  metadata.data.picture.pictureDataSize = drflac__be2host_32(*(const drflac_uint32*)pRunningData); pRunningData += 4;
5244  metadata.data.picture.pPictureData = (const drflac_uint8*)pRunningData;
5245 
5246  /* Need space for the picture after the block */
5247  if (pRunningDataEnd - pRunningData < (drflac_int64)metadata.data.picture.pictureDataSize) { /* <-- Note the order of operations to avoid overflow to a valid value */
5248  DRFLAC_FREE(pRawData);
5249  return DRFLAC_FALSE;
5250  }
5251 
5252  onMeta(pUserDataMD, &metadata);
5253 
5254  DRFLAC_FREE(pRawData);
5255  }
5256  } break;
5257 
5259  {
5260  if (onMeta) {
5261  metadata.data.padding.unused = 0;
5262 
5263  /* Padding doesn't have anything meaningful in it, so just skip over it, but make sure the caller is aware of it by firing the callback. */
5264  if (!onSeek(pUserData, blockSize, drflac_seek_origin_current)) {
5265  isLastBlock = DRFLAC_TRUE; /* An error occurred while seeking. Attempt to recover by treating this as the last block which will in turn terminate the loop. */
5266  } else {
5267  onMeta(pUserDataMD, &metadata);
5268  }
5269  }
5270  } break;
5271 
5273  {
5274  /* Invalid chunk. Just skip over this one. */
5275  if (onMeta) {
5276  if (!onSeek(pUserData, blockSize, drflac_seek_origin_current)) {
5277  isLastBlock = DRFLAC_TRUE; /* An error occurred while seeking. Attempt to recover by treating this as the last block which will in turn terminate the loop. */
5278  }
5279  }
5280  } break;
5281 
5282  default:
5283  {
5284  /*
5285  It's an unknown chunk, but not necessarily invalid. There's a chance more metadata blocks might be defined later on, so we
5286  can at the very least report the chunk to the application and let it look at the raw data.
5287  */
5288  if (onMeta) {
5289  void* pRawData = DRFLAC_MALLOC(blockSize);
5290  if (pRawData == NULL) {
5291  return DRFLAC_FALSE;
5292  }
5293 
5294  if (onRead(pUserData, pRawData, blockSize) != blockSize) {
5295  DRFLAC_FREE(pRawData);
5296  return DRFLAC_FALSE;
5297  }
5298 
5299  metadata.pRawData = pRawData;
5300  metadata.rawDataSize = blockSize;
5301  onMeta(pUserDataMD, &metadata);
5302 
5303  DRFLAC_FREE(pRawData);
5304  }
5305  } break;
5306  }
5307 
5308  /* If we're not handling metadata, just skip over the block. If we are, it will have been handled earlier in the switch statement above. */
5309  if (onMeta == NULL && blockSize > 0) {
5310  if (!onSeek(pUserData, blockSize, drflac_seek_origin_current)) {
5311  isLastBlock = DRFLAC_TRUE;
5312  }
5313  }
5314 
5315  runningFilePos += blockSize;
5316  if (isLastBlock) {
5317  break;
5318  }
5319  }
5320 
5321  *pSeektablePos = seektablePos;
5322  *pSeektableSize = seektableSize;
5323  *pFirstFramePos = runningFilePos;
5324 
5325  return DRFLAC_TRUE;
5326 }
5327 
5328 drflac_bool32 drflac__init_private__native(drflac_init_info* pInit, drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_meta_proc onMeta, void* pUserData, void* pUserDataMD, drflac_bool32 relaxed)
5329 {
5330  /* Pre Condition: The bit stream should be sitting just past the 4-byte id header. */
5331 
5332  drflac_uint8 isLastBlock;
5333  drflac_uint8 blockType;
5334  drflac_uint32 blockSize;
5335 
5336  (void)onSeek;
5337 
5338  pInit->container = drflac_container_native;
5339 
5340  /* The first metadata block should be the STREAMINFO block. */
5341  if (!drflac__read_and_decode_block_header(onRead, pUserData, &isLastBlock, &blockType, &blockSize)) {
5342  return DRFLAC_FALSE;
5343  }
5344 
5345  if (blockType != DRFLAC_METADATA_BLOCK_TYPE_STREAMINFO || blockSize != 34) {
5346  if (!relaxed) {
5347  /* We're opening in strict mode and the first block is not the STREAMINFO block. Error. */
5348  return DRFLAC_FALSE;
5349  } else {
5350  /*
5351  Relaxed mode. To open from here we need to just find the first frame and set the sample rate, etc. to whatever is defined
5352  for that frame.
5353  */
5354  pInit->hasStreamInfoBlock = DRFLAC_FALSE;
5355  pInit->hasMetadataBlocks = DRFLAC_FALSE;
5356 
5357  if (!drflac__read_next_flac_frame_header(&pInit->bs, 0, &pInit->firstFrameHeader)) {
5358  return DRFLAC_FALSE; /* Couldn't find a frame. */
5359  }
5360 
5361  if (pInit->firstFrameHeader.bitsPerSample == 0) {
5362  return DRFLAC_FALSE; /* Failed to initialize because the first frame depends on the STREAMINFO block, which does not exist. */
5363  }
5364 
5365  pInit->sampleRate = pInit->firstFrameHeader.sampleRate;
5366  pInit->channels = drflac__get_channel_count_from_channel_assignment(pInit->firstFrameHeader.channelAssignment);
5367  pInit->bitsPerSample = pInit->firstFrameHeader.bitsPerSample;
5368  pInit->maxBlockSize = 65535; /* <-- See notes here: https://xiph.org/flac/format.html#metadata_block_streaminfo */
5369  return DRFLAC_TRUE;
5370  }
5371  } else {
5372  drflac_streaminfo streaminfo;
5373  if (!drflac__read_streaminfo(onRead, pUserData, &streaminfo)) {
5374  return DRFLAC_FALSE;
5375  }
5376 
5377  pInit->hasStreamInfoBlock = DRFLAC_TRUE;
5378  pInit->sampleRate = streaminfo.sampleRate;
5379  pInit->channels = streaminfo.channels;
5380  pInit->bitsPerSample = streaminfo.bitsPerSample;
5381  pInit->totalSampleCount = streaminfo.totalSampleCount;
5382  pInit->maxBlockSize = streaminfo.maxBlockSize; /* Don't care about the min block size - only the max (used for determining the size of the memory allocation). */
5383  pInit->hasMetadataBlocks = !isLastBlock;
5384 
5385  if (onMeta) {
5386  drflac_metadata metadata;
5388  metadata.pRawData = NULL;
5389  metadata.rawDataSize = 0;
5390  metadata.data.streaminfo = streaminfo;
5391  onMeta(pUserDataMD, &metadata);
5392  }
5393 
5394  return DRFLAC_TRUE;
5395  }
5396 }
5397 
5398 #ifndef DR_FLAC_NO_OGG
5399 #define DRFLAC_OGG_MAX_PAGE_SIZE 65307
5400 #define DRFLAC_OGG_CAPTURE_PATTERN_CRC32 1605413199 /* CRC-32 of "OggS". */
5401 
5402 typedef enum
5403 {
5404  drflac_ogg_recover_on_crc_mismatch,
5405  drflac_ogg_fail_on_crc_mismatch
5406 } drflac_ogg_crc_mismatch_recovery;
5407 
5408 #ifndef DR_FLAC_NO_CRC
5409 static drflac_uint32 drflac__crc32_table[] = {
5410  0x00000000L, 0x04C11DB7L, 0x09823B6EL, 0x0D4326D9L,
5411  0x130476DCL, 0x17C56B6BL, 0x1A864DB2L, 0x1E475005L,
5412  0x2608EDB8L, 0x22C9F00FL, 0x2F8AD6D6L, 0x2B4BCB61L,
5413  0x350C9B64L, 0x31CD86D3L, 0x3C8EA00AL, 0x384FBDBDL,
5414  0x4C11DB70L, 0x48D0C6C7L, 0x4593E01EL, 0x4152FDA9L,
5415  0x5F15ADACL, 0x5BD4B01BL, 0x569796C2L, 0x52568B75L,
5416  0x6A1936C8L, 0x6ED82B7FL, 0x639B0DA6L, 0x675A1011L,
5417  0x791D4014L, 0x7DDC5DA3L, 0x709F7B7AL, 0x745E66CDL,
5418  0x9823B6E0L, 0x9CE2AB57L, 0x91A18D8EL, 0x95609039L,
5419  0x8B27C03CL, 0x8FE6DD8BL, 0x82A5FB52L, 0x8664E6E5L,
5420  0xBE2B5B58L, 0xBAEA46EFL, 0xB7A96036L, 0xB3687D81L,
5421  0xAD2F2D84L, 0xA9EE3033L, 0xA4AD16EAL, 0xA06C0B5DL,
5422  0xD4326D90L, 0xD0F37027L, 0xDDB056FEL, 0xD9714B49L,
5423  0xC7361B4CL, 0xC3F706FBL, 0xCEB42022L, 0xCA753D95L,
5424  0xF23A8028L, 0xF6FB9D9FL, 0xFBB8BB46L, 0xFF79A6F1L,
5425  0xE13EF6F4L, 0xE5FFEB43L, 0xE8BCCD9AL, 0xEC7DD02DL,
5426  0x34867077L, 0x30476DC0L, 0x3D044B19L, 0x39C556AEL,
5427  0x278206ABL, 0x23431B1CL, 0x2E003DC5L, 0x2AC12072L,
5428  0x128E9DCFL, 0x164F8078L, 0x1B0CA6A1L, 0x1FCDBB16L,
5429  0x018AEB13L, 0x054BF6A4L, 0x0808D07DL, 0x0CC9CDCAL,
5430  0x7897AB07L, 0x7C56B6B0L, 0x71159069L, 0x75D48DDEL,
5431  0x6B93DDDBL, 0x6F52C06CL, 0x6211E6B5L, 0x66D0FB02L,
5432  0x5E9F46BFL, 0x5A5E5B08L, 0x571D7DD1L, 0x53DC6066L,
5433  0x4D9B3063L, 0x495A2DD4L, 0x44190B0DL, 0x40D816BAL,
5434  0xACA5C697L, 0xA864DB20L, 0xA527FDF9L, 0xA1E6E04EL,
5435  0xBFA1B04BL, 0xBB60ADFCL, 0xB6238B25L, 0xB2E29692L,
5436  0x8AAD2B2FL, 0x8E6C3698L, 0x832F1041L, 0x87EE0DF6L,
5437  0x99A95DF3L, 0x9D684044L, 0x902B669DL, 0x94EA7B2AL,
5438  0xE0B41DE7L, 0xE4750050L, 0xE9362689L, 0xEDF73B3EL,
5439  0xF3B06B3BL, 0xF771768CL, 0xFA325055L, 0xFEF34DE2L,
5440  0xC6BCF05FL, 0xC27DEDE8L, 0xCF3ECB31L, 0xCBFFD686L,
5441  0xD5B88683L, 0xD1799B34L, 0xDC3ABDEDL, 0xD8FBA05AL,
5442  0x690CE0EEL, 0x6DCDFD59L, 0x608EDB80L, 0x644FC637L,
5443  0x7A089632L, 0x7EC98B85L, 0x738AAD5CL, 0x774BB0EBL,
5444  0x4F040D56L, 0x4BC510E1L, 0x46863638L, 0x42472B8FL,
5445  0x5C007B8AL, 0x58C1663DL, 0x558240E4L, 0x51435D53L,
5446  0x251D3B9EL, 0x21DC2629L, 0x2C9F00F0L, 0x285E1D47L,
5447  0x36194D42L, 0x32D850F5L, 0x3F9B762CL, 0x3B5A6B9BL,
5448  0x0315D626L, 0x07D4CB91L, 0x0A97ED48L, 0x0E56F0FFL,
5449  0x1011A0FAL, 0x14D0BD4DL, 0x19939B94L, 0x1D528623L,
5450  0xF12F560EL, 0xF5EE4BB9L, 0xF8AD6D60L, 0xFC6C70D7L,
5451  0xE22B20D2L, 0xE6EA3D65L, 0xEBA91BBCL, 0xEF68060BL,
5452  0xD727BBB6L, 0xD3E6A601L, 0xDEA580D8L, 0xDA649D6FL,
5453  0xC423CD6AL, 0xC0E2D0DDL, 0xCDA1F604L, 0xC960EBB3L,
5454  0xBD3E8D7EL, 0xB9FF90C9L, 0xB4BCB610L, 0xB07DABA7L,
5455  0xAE3AFBA2L, 0xAAFBE615L, 0xA7B8C0CCL, 0xA379DD7BL,
5456  0x9B3660C6L, 0x9FF77D71L, 0x92B45BA8L, 0x9675461FL,
5457  0x8832161AL, 0x8CF30BADL, 0x81B02D74L, 0x857130C3L,
5458  0x5D8A9099L, 0x594B8D2EL, 0x5408ABF7L, 0x50C9B640L,
5459  0x4E8EE645L, 0x4A4FFBF2L, 0x470CDD2BL, 0x43CDC09CL,
5460  0x7B827D21L, 0x7F436096L, 0x7200464FL, 0x76C15BF8L,
5461  0x68860BFDL, 0x6C47164AL, 0x61043093L, 0x65C52D24L,
5462  0x119B4BE9L, 0x155A565EL, 0x18197087L, 0x1CD86D30L,
5463  0x029F3D35L, 0x065E2082L, 0x0B1D065BL, 0x0FDC1BECL,
5464  0x3793A651L, 0x3352BBE6L, 0x3E119D3FL, 0x3AD08088L,
5465  0x2497D08DL, 0x2056CD3AL, 0x2D15EBE3L, 0x29D4F654L,
5466  0xC5A92679L, 0xC1683BCEL, 0xCC2B1D17L, 0xC8EA00A0L,
5467  0xD6AD50A5L, 0xD26C4D12L, 0xDF2F6BCBL, 0xDBEE767CL,
5468  0xE3A1CBC1L, 0xE760D676L, 0xEA23F0AFL, 0xEEE2ED18L,
5469  0xF0A5BD1DL, 0xF464A0AAL, 0xF9278673L, 0xFDE69BC4L,
5470  0x89B8FD09L, 0x8D79E0BEL, 0x803AC667L, 0x84FBDBD0L,
5471  0x9ABC8BD5L, 0x9E7D9662L, 0x933EB0BBL, 0x97FFAD0CL,
5472  0xAFB010B1L, 0xAB710D06L, 0xA6322BDFL, 0xA2F33668L,
5473  0xBCB4666DL, 0xB8757BDAL, 0xB5365D03L, 0xB1F740B4L
5474 };
5475 #endif
5476 
5477 static DRFLAC_INLINE drflac_uint32 drflac_crc32_byte(drflac_uint32 crc32, drflac_uint8 data)
5478 {
5479 #ifndef DR_FLAC_NO_CRC
5480  return (crc32 << 8) ^ drflac__crc32_table[(drflac_uint8)((crc32 >> 24) & 0xFF) ^ data];
5481 #else
5482  (void)data;
5483  return crc32;
5484 #endif
5485 }
5486 
5487 #if 0
5488 static DRFLAC_INLINE drflac_uint32 drflac_crc32_uint32(drflac_uint32 crc32, drflac_uint32 data)
5489 {
5490  crc32 = drflac_crc32_byte(crc32, (drflac_uint8)((data >> 24) & 0xFF));
5491  crc32 = drflac_crc32_byte(crc32, (drflac_uint8)((data >> 16) & 0xFF));
5492  crc32 = drflac_crc32_byte(crc32, (drflac_uint8)((data >> 8) & 0xFF));
5493  crc32 = drflac_crc32_byte(crc32, (drflac_uint8)((data >> 0) & 0xFF));
5494  return crc32;
5495 }
5496 
5497 static DRFLAC_INLINE drflac_uint32 drflac_crc32_uint64(drflac_uint32 crc32, drflac_uint64 data)
5498 {
5499  crc32 = drflac_crc32_uint32(crc32, (drflac_uint32)((data >> 32) & 0xFFFFFFFF));
5500  crc32 = drflac_crc32_uint32(crc32, (drflac_uint32)((data >> 0) & 0xFFFFFFFF));
5501  return crc32;
5502 }
5503 #endif
5504 
5505 static DRFLAC_INLINE drflac_uint32 drflac_crc32_buffer(drflac_uint32 crc32, drflac_uint8* pData, drflac_uint32 dataSize)
5506 {
5507  /* This can be optimized. */
5508  drflac_uint32 i;
5509  for (i = 0; i < dataSize; ++i) {
5510  crc32 = drflac_crc32_byte(crc32, pData[i]);
5511  }
5512  return crc32;
5513 }
5514 
5515 
5516 static DRFLAC_INLINE drflac_bool32 drflac_ogg__is_capture_pattern(drflac_uint8 pattern[4])
5517 {
5518  return pattern[0] == 'O' && pattern[1] == 'g' && pattern[2] == 'g' && pattern[3] == 'S';
5519 }
5520 
5521 static DRFLAC_INLINE drflac_uint32 drflac_ogg__get_page_header_size(drflac_ogg_page_header* pHeader)
5522 {
5523  return 27 + pHeader->segmentCount;
5524 }
5525 
5526 static DRFLAC_INLINE drflac_uint32 drflac_ogg__get_page_body_size(drflac_ogg_page_header* pHeader)
5527 {
5528  drflac_uint32 pageBodySize = 0;
5529  int i;
5530 
5531  for (i = 0; i < pHeader->segmentCount; ++i) {
5532  pageBodySize += pHeader->segmentTable[i];
5533  }
5534 
5535  return pageBodySize;
5536 }
5537 
5538 drflac_result drflac_ogg__read_page_header_after_capture_pattern(drflac_read_proc onRead, void* pUserData, drflac_ogg_page_header* pHeader, drflac_uint32* pBytesRead, drflac_uint32* pCRC32)
5539 {
5540  drflac_uint8 data[23];
5541  drflac_uint32 i;
5542 
5543  drflac_assert(*pCRC32 == DRFLAC_OGG_CAPTURE_PATTERN_CRC32);
5544 
5545  if (onRead(pUserData, data, 23) != 23) {
5546  return DRFLAC_END_OF_STREAM;
5547  }
5548  *pBytesRead += 23;
5549 
5550  pHeader->structureVersion = data[0];
5551  pHeader->headerType = data[1];
5552  drflac_copy_memory(&pHeader->granulePosition, &data[ 2], 8);
5553  drflac_copy_memory(&pHeader->serialNumber, &data[10], 4);
5554  drflac_copy_memory(&pHeader->sequenceNumber, &data[14], 4);
5555  drflac_copy_memory(&pHeader->checksum, &data[18], 4);
5556  pHeader->segmentCount = data[22];
5557 
5558  /* Calculate the CRC. Note that for the calculation the checksum part of the page needs to be set to 0. */
5559  data[18] = 0;
5560  data[19] = 0;
5561  data[20] = 0;
5562  data[21] = 0;
5563 
5564  for (i = 0; i < 23; ++i) {
5565  *pCRC32 = drflac_crc32_byte(*pCRC32, data[i]);
5566  }
5567 
5568 
5569  if (onRead(pUserData, pHeader->segmentTable, pHeader->segmentCount) != pHeader->segmentCount) {
5570  return DRFLAC_END_OF_STREAM;
5571  }
5572  *pBytesRead += pHeader->segmentCount;
5573 
5574  for (i = 0; i < pHeader->segmentCount; ++i) {
5575  *pCRC32 = drflac_crc32_byte(*pCRC32, pHeader->segmentTable[i]);
5576  }
5577 
5578  return DRFLAC_SUCCESS;
5579 }
5580 
5581 drflac_result drflac_ogg__read_page_header(drflac_read_proc onRead, void* pUserData, drflac_ogg_page_header* pHeader, drflac_uint32* pBytesRead, drflac_uint32* pCRC32)
5582 {
5583  drflac_uint8 id[4];
5584 
5585  *pBytesRead = 0;
5586 
5587  if (onRead(pUserData, id, 4) != 4) {
5588  return DRFLAC_END_OF_STREAM;
5589  }
5590  *pBytesRead += 4;
5591 
5592  /* We need to read byte-by-byte until we find the OggS capture pattern. */
5593  for (;;) {
5594  if (drflac_ogg__is_capture_pattern(id)) {
5595  drflac_result result;
5596 
5597  *pCRC32 = DRFLAC_OGG_CAPTURE_PATTERN_CRC32;
5598 
5599  result = drflac_ogg__read_page_header_after_capture_pattern(onRead, pUserData, pHeader, pBytesRead, pCRC32);
5600  if (result == DRFLAC_SUCCESS) {
5601  return DRFLAC_SUCCESS;
5602  } else {
5603  if (result == DRFLAC_CRC_MISMATCH) {
5604  continue;
5605  } else {
5606  return result;
5607  }
5608  }
5609  } else {
5610  /* The first 4 bytes did not equal the capture pattern. Read the next byte and try again. */
5611  id[0] = id[1];
5612  id[1] = id[2];
5613  id[2] = id[3];
5614  if (onRead(pUserData, &id[3], 1) != 1) {
5615  return DRFLAC_END_OF_STREAM;
5616  }
5617  *pBytesRead += 1;
5618  }
5619  }
5620 }
5621 
5622 
5623 /*
5624 The main part of the Ogg encapsulation is the conversion from the physical Ogg bitstream to the native FLAC bitstream. It works
5625 in three general stages: Ogg Physical Bitstream -> Ogg/FLAC Logical Bitstream -> FLAC Native Bitstream. dr_flac is designed
5626 in such a way that the core sections assume everything is delivered in native format. Therefore, for each encapsulation type
5627 dr_flac is supporting there needs to be a layer sitting on top of the onRead and onSeek callbacks that ensures the bits read from
5628 the physical Ogg bitstream are converted and delivered in native FLAC format.
5629 */
5630 typedef struct
5631 {
5632  drflac_read_proc onRead; /* The original onRead callback from drflac_open() and family. */
5633  drflac_seek_proc onSeek; /* The original onSeek callback from drflac_open() and family. */
5634  void* pUserData; /* The user data passed on onRead and onSeek. This is the user data that was passed on drflac_open() and family. */
5635  drflac_uint64 currentBytePos; /* The position of the byte we are sitting on in the physical byte stream. Used for efficient seeking. */
5636  drflac_uint64 firstBytePos; /* The position of the first byte in the physical bitstream. Points to the start of the "OggS" identifier of the FLAC bos page. */
5637  drflac_uint32 serialNumber; /* The serial number of the FLAC audio pages. This is determined by the initial header page that was read during initialization. */
5638  drflac_ogg_page_header bosPageHeader; /* Used for seeking. */
5639  drflac_ogg_page_header currentPageHeader;
5640  drflac_uint32 bytesRemainingInPage;
5641  drflac_uint32 pageDataSize;
5642  drflac_uint8 pageData[DRFLAC_OGG_MAX_PAGE_SIZE];
5643 } drflac_oggbs; /* oggbs = Ogg Bitstream */
5644 
5645 static size_t drflac_oggbs__read_physical(drflac_oggbs* oggbs, void* bufferOut, size_t bytesToRead)
5646 {
5647  size_t bytesActuallyRead = oggbs->onRead(oggbs->pUserData, bufferOut, bytesToRead);
5648  oggbs->currentBytePos += bytesActuallyRead;
5649 
5650  return bytesActuallyRead;
5651 }
5652 
5653 static drflac_bool32 drflac_oggbs__seek_physical(drflac_oggbs* oggbs, drflac_uint64 offset, drflac_seek_origin origin)
5654 {
5655  if (origin == drflac_seek_origin_start) {
5656  if (offset <= 0x7FFFFFFF) {
5657  if (!oggbs->onSeek(oggbs->pUserData, (int)offset, drflac_seek_origin_start)) {
5658  return DRFLAC_FALSE;
5659  }
5660  oggbs->currentBytePos = offset;
5661 
5662  return DRFLAC_TRUE;
5663  } else {
5664  if (!oggbs->onSeek(oggbs->pUserData, 0x7FFFFFFF, drflac_seek_origin_start)) {
5665  return DRFLAC_FALSE;
5666  }
5667  oggbs->currentBytePos = offset;
5668 
5669  return drflac_oggbs__seek_physical(oggbs, offset - 0x7FFFFFFF, drflac_seek_origin_current);
5670  }
5671  } else {
5672  while (offset > 0x7FFFFFFF) {
5673  if (!oggbs->onSeek(oggbs->pUserData, 0x7FFFFFFF, drflac_seek_origin_current)) {
5674  return DRFLAC_FALSE;
5675  }
5676  oggbs->currentBytePos += 0x7FFFFFFF;
5677  offset -= 0x7FFFFFFF;
5678  }
5679 
5680  if (!oggbs->onSeek(oggbs->pUserData, (int)offset, drflac_seek_origin_current)) { /* <-- Safe cast thanks to the loop above. */
5681  return DRFLAC_FALSE;
5682  }
5683  oggbs->currentBytePos += offset;
5684 
5685  return DRFLAC_TRUE;
5686  }
5687 }
5688 
5689 static drflac_bool32 drflac_oggbs__goto_next_page(drflac_oggbs* oggbs, drflac_ogg_crc_mismatch_recovery recoveryMethod)
5690 {
5691  drflac_ogg_page_header header;
5692  for (;;) {
5693  drflac_uint32 crc32 = 0;
5694  drflac_uint32 bytesRead;
5695  drflac_uint32 pageBodySize;
5696 #ifndef DR_FLAC_NO_CRC
5697  drflac_uint32 actualCRC32;
5698 #endif
5699 
5700  if (drflac_ogg__read_page_header(oggbs->onRead, oggbs->pUserData, &header, &bytesRead, &crc32) != DRFLAC_SUCCESS) {
5701  return DRFLAC_FALSE;
5702  }
5703  oggbs->currentBytePos += bytesRead;
5704 
5705  pageBodySize = drflac_ogg__get_page_body_size(&header);
5706  if (pageBodySize > DRFLAC_OGG_MAX_PAGE_SIZE) {
5707  continue; /* Invalid page size. Assume it's corrupted and just move to the next page. */
5708  }
5709 
5710  if (header.serialNumber != oggbs->serialNumber) {
5711  /* It's not a FLAC page. Skip it. */
5712  if (pageBodySize > 0 && !drflac_oggbs__seek_physical(oggbs, pageBodySize, drflac_seek_origin_current)) {
5713  return DRFLAC_FALSE;
5714  }
5715  continue;
5716  }
5717 
5718 
5719  /* We need to read the entire page and then do a CRC check on it. If there's a CRC mismatch we need to skip this page. */
5720  if (drflac_oggbs__read_physical(oggbs, oggbs->pageData, pageBodySize) != pageBodySize) {
5721  return DRFLAC_FALSE;
5722  }
5723  oggbs->pageDataSize = pageBodySize;
5724 
5725 #ifndef DR_FLAC_NO_CRC
5726  actualCRC32 = drflac_crc32_buffer(crc32, oggbs->pageData, oggbs->pageDataSize);
5727  if (actualCRC32 != header.checksum) {
5728  if (recoveryMethod == drflac_ogg_recover_on_crc_mismatch) {
5729  continue; /* CRC mismatch. Skip this page. */
5730  } else {
5731  /*
5732  Even though we are failing on a CRC mismatch, we still want our stream to be in a good state. Therefore we
5733  go to the next valid page to ensure we're in a good state, but return false to let the caller know that the
5734  seek did not fully complete.
5735  */
5736  drflac_oggbs__goto_next_page(oggbs, drflac_ogg_recover_on_crc_mismatch);
5737  return DRFLAC_FALSE;
5738  }
5739  }
5740 #else
5741  (void)recoveryMethod; /* <-- Silence a warning. */
5742 #endif
5743 
5744  oggbs->currentPageHeader = header;
5745  oggbs->bytesRemainingInPage = pageBodySize;
5746  return DRFLAC_TRUE;
5747  }
5748 }
5749 
5750 /* Function below is unused at the moment, but I might be re-adding it later. */
5751 #if 0
5752 static drflac_uint8 drflac_oggbs__get_current_segment_index(drflac_oggbs* oggbs, drflac_uint8* pBytesRemainingInSeg)
5753 {
5754  drflac_uint32 bytesConsumedInPage = drflac_ogg__get_page_body_size(&oggbs->currentPageHeader) - oggbs->bytesRemainingInPage;
5755  drflac_uint8 iSeg = 0;
5756  drflac_uint32 iByte = 0;
5757  while (iByte < bytesConsumedInPage) {
5758  drflac_uint8 segmentSize = oggbs->currentPageHeader.segmentTable[iSeg];
5759  if (iByte + segmentSize > bytesConsumedInPage) {
5760  break;
5761  } else {
5762  iSeg += 1;
5763  iByte += segmentSize;
5764  }
5765  }
5766 
5767  *pBytesRemainingInSeg = oggbs->currentPageHeader.segmentTable[iSeg] - (drflac_uint8)(bytesConsumedInPage - iByte);
5768  return iSeg;
5769 }
5770 
5771 static drflac_bool32 drflac_oggbs__seek_to_next_packet(drflac_oggbs* oggbs)
5772 {
5773  /* The current packet ends when we get to the segment with a lacing value of < 255 which is not at the end of a page. */
5774  for (;;) {
5775  drflac_bool32 atEndOfPage = DRFLAC_FALSE;
5776 
5777  drflac_uint8 bytesRemainingInSeg;
5778  drflac_uint8 iFirstSeg = drflac_oggbs__get_current_segment_index(oggbs, &bytesRemainingInSeg);
5779 
5780  drflac_uint32 bytesToEndOfPacketOrPage = bytesRemainingInSeg;
5781  for (drflac_uint8 iSeg = iFirstSeg; iSeg < oggbs->currentPageHeader.segmentCount; ++iSeg) {
5782  drflac_uint8 segmentSize = oggbs->currentPageHeader.segmentTable[iSeg];
5783  if (segmentSize < 255) {
5784  if (iSeg == oggbs->currentPageHeader.segmentCount-1) {
5785  atEndOfPage = DRFLAC_TRUE;
5786  }
5787 
5788  break;
5789  }
5790 
5791  bytesToEndOfPacketOrPage += segmentSize;
5792  }
5793 
5794  /*
5795  At this point we will have found either the packet or the end of the page. If were at the end of the page we'll
5796  want to load the next page and keep searching for the end of the packet.
5797  */
5798  drflac_oggbs__seek_physical(oggbs, bytesToEndOfPacketOrPage, drflac_seek_origin_current);
5799  oggbs->bytesRemainingInPage -= bytesToEndOfPacketOrPage;
5800 
5801  if (atEndOfPage) {
5802  /*
5803  We're potentially at the next packet, but we need to check the next page first to be sure because the packet may
5804  straddle pages.
5805  */
5806  if (!drflac_oggbs__goto_next_page(oggbs)) {
5807  return DRFLAC_FALSE;
5808  }
5809 
5810  /* If it's a fresh packet it most likely means we're at the next packet. */
5811  if ((oggbs->currentPageHeader.headerType & 0x01) == 0) {
5812  return DRFLAC_TRUE;
5813  }
5814  } else {
5815  /* We're at the next packet. */
5816  return DRFLAC_TRUE;
5817  }
5818  }
5819 }
5820 
5821 static drflac_bool32 drflac_oggbs__seek_to_next_frame(drflac_oggbs* oggbs)
5822 {
5823  /* The bitstream should be sitting on the first byte just after the header of the frame. */
5824 
5825  /* What we're actually doing here is seeking to the start of the next packet. */
5826  return drflac_oggbs__seek_to_next_packet(oggbs);
5827 }
5828 #endif
5829 
5830 static size_t drflac__on_read_ogg(void* pUserData, void* bufferOut, size_t bytesToRead)
5831 {
5832  drflac_oggbs* oggbs = (drflac_oggbs*)pUserData;
5833  drflac_uint8* pRunningBufferOut = (drflac_uint8*)bufferOut;
5834  size_t bytesRead = 0;
5835 
5836  drflac_assert(oggbs != NULL);
5837  drflac_assert(pRunningBufferOut != NULL);
5838 
5839  /* Reading is done page-by-page. If we've run out of bytes in the page we need to move to the next one. */
5840  while (bytesRead < bytesToRead) {
5841  size_t bytesRemainingToRead = bytesToRead - bytesRead;
5842 
5843  if (oggbs->bytesRemainingInPage >= bytesRemainingToRead) {
5844  drflac_copy_memory(pRunningBufferOut, oggbs->pageData + (oggbs->pageDataSize - oggbs->bytesRemainingInPage), bytesRemainingToRead);
5845  bytesRead += bytesRemainingToRead;
5846  oggbs->bytesRemainingInPage -= (drflac_uint32)bytesRemainingToRead;
5847  break;
5848  }
5849 
5850  /* If we get here it means some of the requested data is contained in the next pages. */
5851  if (oggbs->bytesRemainingInPage > 0) {
5852  drflac_copy_memory(pRunningBufferOut, oggbs->pageData + (oggbs->pageDataSize - oggbs->bytesRemainingInPage), oggbs->bytesRemainingInPage);
5853  bytesRead += oggbs->bytesRemainingInPage;
5854  pRunningBufferOut += oggbs->bytesRemainingInPage;
5855  oggbs->bytesRemainingInPage = 0;
5856  }
5857 
5858  drflac_assert(bytesRemainingToRead > 0);
5859  if (!drflac_oggbs__goto_next_page(oggbs, drflac_ogg_recover_on_crc_mismatch)) {
5860  break; /* Failed to go to the next page. Might have simply hit the end of the stream. */
5861  }
5862  }
5863 
5864  return bytesRead;
5865 }
5866 
5867 static drflac_bool32 drflac__on_seek_ogg(void* pUserData, int offset, drflac_seek_origin origin)
5868 {
5869  drflac_oggbs* oggbs = (drflac_oggbs*)pUserData;
5870  int bytesSeeked = 0;
5871 
5872  drflac_assert(oggbs != NULL);
5873  drflac_assert(offset >= 0); /* <-- Never seek backwards. */
5874 
5875  /* Seeking is always forward which makes things a lot simpler. */
5876  if (origin == drflac_seek_origin_start) {
5877  if (!drflac_oggbs__seek_physical(oggbs, (int)oggbs->firstBytePos, drflac_seek_origin_start)) {
5878  return DRFLAC_FALSE;
5879  }
5880 
5881  if (!drflac_oggbs__goto_next_page(oggbs, drflac_ogg_fail_on_crc_mismatch)) {
5882  return DRFLAC_FALSE;
5883  }
5884 
5885  return drflac__on_seek_ogg(pUserData, offset, drflac_seek_origin_current);
5886  }
5887 
5888  drflac_assert(origin == drflac_seek_origin_current);
5889 
5890  while (bytesSeeked < offset) {
5891  int bytesRemainingToSeek = offset - bytesSeeked;
5892  drflac_assert(bytesRemainingToSeek >= 0);
5893 
5894  if (oggbs->bytesRemainingInPage >= (size_t)bytesRemainingToSeek) {
5895  bytesSeeked += bytesRemainingToSeek;
5896  oggbs->bytesRemainingInPage -= bytesRemainingToSeek;
5897  break;
5898  }
5899 
5900  /* If we get here it means some of the requested data is contained in the next pages. */
5901  if (oggbs->bytesRemainingInPage > 0) {
5902  bytesSeeked += (int)oggbs->bytesRemainingInPage;
5903  oggbs->bytesRemainingInPage = 0;
5904  }
5905 
5906  drflac_assert(bytesRemainingToSeek > 0);
5907  if (!drflac_oggbs__goto_next_page(oggbs, drflac_ogg_fail_on_crc_mismatch)) {
5908  /* Failed to go to the next page. We either hit the end of the stream or had a CRC mismatch. */
5909  return DRFLAC_FALSE;
5910  }
5911  }
5912 
5913  return DRFLAC_TRUE;
5914 }
5915 
5916 drflac_bool32 drflac_ogg__seek_to_sample(drflac* pFlac, drflac_uint64 sampleIndex)
5917 {
5918  drflac_oggbs* oggbs = (drflac_oggbs*)pFlac->_oggbs;
5919  drflac_uint64 originalBytePos;
5920  drflac_uint64 runningGranulePosition;
5921  drflac_uint64 runningFrameBytePos;
5922  drflac_uint64 runningSampleCount;
5923 
5924  drflac_assert(oggbs != NULL);
5925 
5926  originalBytePos = oggbs->currentBytePos; /* For recovery. */
5927 
5928  /* First seek to the first frame. */
5929  if (!drflac__seek_to_byte(&pFlac->bs, pFlac->firstFramePos)) {
5930  return DRFLAC_FALSE;
5931  }
5932  oggbs->bytesRemainingInPage = 0;
5933 
5934  runningGranulePosition = 0;
5935  runningFrameBytePos = oggbs->currentBytePos; /* <-- Points to the OggS identifier. */
5936  for (;;) {
5937  if (!drflac_oggbs__goto_next_page(oggbs, drflac_ogg_recover_on_crc_mismatch)) {
5938  drflac_oggbs__seek_physical(oggbs, originalBytePos, drflac_seek_origin_start);
5939  return DRFLAC_FALSE; /* Never did find that sample... */
5940  }
5941 
5942  runningFrameBytePos = oggbs->currentBytePos - drflac_ogg__get_page_header_size(&oggbs->currentPageHeader) - oggbs->pageDataSize;
5943  if (oggbs->currentPageHeader.granulePosition*pFlac->channels >= sampleIndex) {
5944  break; /* The sample is somewhere in the previous page. */
5945  }
5946 
5947  /*
5948  At this point we know the sample is not in the previous page. It could possibly be in this page. For simplicity we
5949  disregard any pages that do not begin a fresh packet.
5950  */
5951  if ((oggbs->currentPageHeader.headerType & 0x01) == 0) { /* <-- Is it a fresh page? */
5952  if (oggbs->currentPageHeader.segmentTable[0] >= 2) {
5953  drflac_uint8 firstBytesInPage[2];
5954  firstBytesInPage[0] = oggbs->pageData[0];
5955  firstBytesInPage[1] = oggbs->pageData[1];
5956 
5957  if ((firstBytesInPage[0] == 0xFF) && (firstBytesInPage[1] & 0xFC) == 0xF8) { /* <-- Does the page begin with a frame's sync code? */
5958  runningGranulePosition = oggbs->currentPageHeader.granulePosition*pFlac->channels;
5959  }
5960 
5961  continue;
5962  }
5963  }
5964  }
5965 
5966  /*
5967  We found the page that that is closest to the sample, so now we need to find it. The first thing to do is seek to the
5968  start of that page. In the loop above we checked that it was a fresh page which means this page is also the start of
5969  a new frame. This property means that after we've seeked to the page we can immediately start looping over frames until
5970  we find the one containing the target sample.
5971  */
5972  if (!drflac_oggbs__seek_physical(oggbs, runningFrameBytePos, drflac_seek_origin_start)) {
5973  return DRFLAC_FALSE;
5974  }
5975  if (!drflac_oggbs__goto_next_page(oggbs, drflac_ogg_recover_on_crc_mismatch)) {
5976  return DRFLAC_FALSE;
5977  }
5978 
5979  /*
5980  At this point we'll be sitting on the first byte of the frame header of the first frame in the page. We just keep
5981  looping over these frames until we find the one containing the sample we're after.
5982  */
5983  runningSampleCount = runningGranulePosition;
5984  for (;;) {
5985  /*
5986  There are two ways to find the sample and seek past irrelevant frames:
5987  1) Use the native FLAC decoder.
5988  2) Use Ogg's framing system.
5989 
5990  Both of these options have their own pros and cons. Using the native FLAC decoder is slower because it needs to
5991  do a full decode of the frame. Using Ogg's framing system is faster, but more complicated and involves some code
5992  duplication for the decoding of frame headers.
5993 
5994  Another thing to consider is that using the Ogg framing system will perform direct seeking of the physical Ogg
5995  bitstream. This is important to consider because it means we cannot read data from the drflac_bs object using the
5996  standard drflac__*() APIs because that will read in extra data for its own internal caching which in turn breaks
5997  the positioning of the read pointer of the physical Ogg bitstream. Therefore, anything that would normally be read
5998  using the native FLAC decoding APIs, such as drflac__read_next_flac_frame_header(), need to be re-implemented so as to
5999  avoid the use of the drflac_bs object.
6000 
6001  Considering these issues, I have decided to use the slower native FLAC decoding method for the following reasons:
6002  1) Seeking is already partially accelerated using Ogg's paging system in the code block above.
6003  2) Seeking in an Ogg encapsulated FLAC stream is probably quite uncommon.
6004  3) Simplicity.
6005  */
6006  drflac_uint64 firstSampleInFrame = 0;
6007  drflac_uint64 lastSampleInFrame = 0;
6008  drflac_uint64 sampleCountInThisFrame;
6009 
6010  if (!drflac__read_next_flac_frame_header(&pFlac->bs, pFlac->bitsPerSample, &pFlac->currentFrame.header)) {
6011  return DRFLAC_FALSE;
6012  }
6013 
6014  drflac__get_current_frame_sample_range(pFlac, &firstSampleInFrame, &lastSampleInFrame);
6015 
6016  sampleCountInThisFrame = (lastSampleInFrame - firstSampleInFrame) + 1;
6017  if (sampleIndex < (runningSampleCount + sampleCountInThisFrame)) {
6018  /*
6019  The sample should be in this frame. We need to fully decode it, however if it's an invalid frame (a CRC mismatch), we need to pretend
6020  it never existed and keep iterating.
6021  */
6022  drflac_result result = drflac__decode_flac_frame(pFlac);
6023  if (result == DRFLAC_SUCCESS) {
6024  /* The frame is valid. We just need to skip over some samples to ensure it's sample-exact. */
6025  drflac_uint64 samplesToDecode = (size_t)(sampleIndex - runningSampleCount); /* <-- Safe cast because the maximum number of samples in a frame is 65535. */
6026  if (samplesToDecode == 0) {
6027  return DRFLAC_TRUE;
6028  }
6029  return drflac__seek_forward_by_samples(pFlac, samplesToDecode) == samplesToDecode; /* <-- If this fails, something bad has happened (it should never fail). */
6030  } else {
6031  if (result == DRFLAC_CRC_MISMATCH) {
6032  continue; /* CRC mismatch. Pretend this frame never existed. */
6033  } else {
6034  return DRFLAC_FALSE;
6035  }
6036  }
6037  } else {
6038  /*
6039  It's not in this frame. We need to seek past the frame, but check if there was a CRC mismatch. If so, we pretend this
6040  frame never existed and leave the running sample count untouched.
6041  */
6042  drflac_result result = drflac__seek_to_next_flac_frame(pFlac);
6043  if (result == DRFLAC_SUCCESS) {
6044  runningSampleCount += sampleCountInThisFrame;
6045  } else {
6046  if (result == DRFLAC_CRC_MISMATCH) {
6047  continue; /* CRC mismatch. Pretend this frame never existed. */
6048  } else {
6049  return DRFLAC_FALSE;
6050  }
6051  }
6052  }
6053  }
6054 }
6055 
6056 
6057 drflac_bool32 drflac__init_private__ogg(drflac_init_info* pInit, drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_meta_proc onMeta, void* pUserData, void* pUserDataMD, drflac_bool32 relaxed)
6058 {
6059  drflac_ogg_page_header header;
6060  drflac_uint32 crc32 = DRFLAC_OGG_CAPTURE_PATTERN_CRC32;
6061  drflac_uint32 bytesRead = 0;
6062 
6063  /* Pre Condition: The bit stream should be sitting just past the 4-byte OggS capture pattern. */
6064  (void)relaxed;
6065 
6066  pInit->container = drflac_container_ogg;
6067  pInit->oggFirstBytePos = 0;
6068 
6069  /*
6070  We'll get here if the first 4 bytes of the stream were the OggS capture pattern, however it doesn't necessarily mean the
6071  stream includes FLAC encoded audio. To check for this we need to scan the beginning-of-stream page markers and check if
6072  any match the FLAC specification. Important to keep in mind that the stream may be multiplexed.
6073  */
6074  if (drflac_ogg__read_page_header_after_capture_pattern(onRead, pUserData, &header, &bytesRead, &crc32) != DRFLAC_SUCCESS) {
6075  return DRFLAC_FALSE;
6076  }
6077  pInit->runningFilePos += bytesRead;
6078 
6079  for (;;) {
6080  int pageBodySize;
6081 
6082  /* Break if we're past the beginning of stream page. */
6083  if ((header.headerType & 0x02) == 0) {
6084  return DRFLAC_FALSE;
6085  }
6086 
6087  /* Check if it's a FLAC header. */
6088  pageBodySize = drflac_ogg__get_page_body_size(&header);
6089  if (pageBodySize == 51) { /* 51 = the lacing value of the FLAC header packet. */
6090  /* It could be a FLAC page... */
6091  drflac_uint32 bytesRemainingInPage = pageBodySize;
6092  drflac_uint8 packetType;
6093 
6094  if (onRead(pUserData, &packetType, 1) != 1) {
6095  return DRFLAC_FALSE;
6096  }
6097 
6098  bytesRemainingInPage -= 1;
6099  if (packetType == 0x7F) {
6100  /* Increasingly more likely to be a FLAC page... */
6101  drflac_uint8 sig[4];
6102  if (onRead(pUserData, sig, 4) != 4) {
6103  return DRFLAC_FALSE;
6104  }
6105 
6106  bytesRemainingInPage -= 4;
6107  if (sig[0] == 'F' && sig[1] == 'L' && sig[2] == 'A' && sig[3] == 'C') {
6108  /* Almost certainly a FLAC page... */
6109  drflac_uint8 mappingVersion[2];
6110  if (onRead(pUserData, mappingVersion, 2) != 2) {
6111  return DRFLAC_FALSE;
6112  }
6113 
6114  if (mappingVersion[0] != 1) {
6115  return DRFLAC_FALSE; /* Only supporting version 1.x of the Ogg mapping. */
6116  }
6117 
6118  /*
6119  The next 2 bytes are the non-audio packets, not including this one. We don't care about this because we're going to
6120  be handling it in a generic way based on the serial number and packet types.
6121  */
6122  if (!onSeek(pUserData, 2, drflac_seek_origin_current)) {
6123  return DRFLAC_FALSE;
6124  }
6125 
6126  /* Expecting the native FLAC signature "fLaC". */
6127  if (onRead(pUserData, sig, 4) != 4) {
6128  return DRFLAC_FALSE;
6129  }
6130 
6131  if (sig[0] == 'f' && sig[1] == 'L' && sig[2] == 'a' && sig[3] == 'C') {
6132  /* The remaining data in the page should be the STREAMINFO block. */
6133  drflac_streaminfo streaminfo;
6134  drflac_uint8 isLastBlock;
6135  drflac_uint8 blockType;
6136  drflac_uint32 blockSize;
6137  if (!drflac__read_and_decode_block_header(onRead, pUserData, &isLastBlock, &blockType, &blockSize)) {
6138  return DRFLAC_FALSE;
6139  }
6140 
6141  if (blockType != DRFLAC_METADATA_BLOCK_TYPE_STREAMINFO || blockSize != 34) {
6142  return DRFLAC_FALSE; /* Invalid block type. First block must be the STREAMINFO block. */
6143  }
6144 
6145  if (drflac__read_streaminfo(onRead, pUserData, &streaminfo)) {
6146  /* Success! */
6147  pInit->hasStreamInfoBlock = DRFLAC_TRUE;
6148  pInit->sampleRate = streaminfo.sampleRate;
6149  pInit->channels = streaminfo.channels;
6150  pInit->bitsPerSample = streaminfo.bitsPerSample;
6151  pInit->totalSampleCount = streaminfo.totalSampleCount;
6152  pInit->maxBlockSize = streaminfo.maxBlockSize;
6153  pInit->hasMetadataBlocks = !isLastBlock;
6154 
6155  if (onMeta) {
6156  drflac_metadata metadata;
6158  metadata.pRawData = NULL;
6159  metadata.rawDataSize = 0;
6160  metadata.data.streaminfo = streaminfo;
6161  onMeta(pUserDataMD, &metadata);
6162  }
6163 
6164  pInit->runningFilePos += pageBodySize;
6165  pInit->oggFirstBytePos = pInit->runningFilePos - 79; /* Subtracting 79 will place us right on top of the "OggS" identifier of the FLAC bos page. */
6166  pInit->oggSerial = header.serialNumber;
6167  pInit->oggBosHeader = header;
6168  break;
6169  } else {
6170  /* Failed to read STREAMINFO block. Aww, so close... */
6171  return DRFLAC_FALSE;
6172  }
6173  } else {
6174  /* Invalid file. */
6175  return DRFLAC_FALSE;
6176  }
6177  } else {
6178  /* Not a FLAC header. Skip it. */
6179  if (!onSeek(pUserData, bytesRemainingInPage, drflac_seek_origin_current)) {
6180  return DRFLAC_FALSE;
6181  }
6182  }
6183  } else {
6184  /* Not a FLAC header. Seek past the entire page and move on to the next. */
6185  if (!onSeek(pUserData, bytesRemainingInPage, drflac_seek_origin_current)) {
6186  return DRFLAC_FALSE;
6187  }
6188  }
6189  } else {
6190  if (!onSeek(pUserData, pageBodySize, drflac_seek_origin_current)) {
6191  return DRFLAC_FALSE;
6192  }
6193  }
6194 
6195  pInit->runningFilePos += pageBodySize;
6196 
6197 
6198  /* Read the header of the next page. */
6199  if (drflac_ogg__read_page_header(onRead, pUserData, &header, &bytesRead, &crc32) != DRFLAC_SUCCESS) {
6200  return DRFLAC_FALSE;
6201  }
6202  pInit->runningFilePos += bytesRead;
6203  }
6204 
6205  /*
6206  If we get here it means we found a FLAC audio stream. We should be sitting on the first byte of the header of the next page. The next
6207  packets in the FLAC logical stream contain the metadata. The only thing left to do in the initialization phase for Ogg is to create the
6208  Ogg bistream object.
6209  */
6210  pInit->hasMetadataBlocks = DRFLAC_TRUE; /* <-- Always have at least VORBIS_COMMENT metadata block. */
6211  return DRFLAC_TRUE;
6212 }
6213 #endif
6214 
6215 drflac_bool32 drflac__init_private(drflac_init_info* pInit, drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_meta_proc onMeta, drflac_container container, void* pUserData, void* pUserDataMD)
6216 {
6217  drflac_bool32 relaxed;
6218  drflac_uint8 id[4];
6219 
6220  if (pInit == NULL || onRead == NULL || onSeek == NULL) {
6221  return DRFLAC_FALSE;
6222  }
6223 
6224  drflac_zero_memory(pInit, sizeof(*pInit));
6225  pInit->onRead = onRead;
6226  pInit->onSeek = onSeek;
6227  pInit->onMeta = onMeta;
6228  pInit->container = container;
6229  pInit->pUserData = pUserData;
6230  pInit->pUserDataMD = pUserDataMD;
6231 
6232  pInit->bs.onRead = onRead;
6233  pInit->bs.onSeek = onSeek;
6234  pInit->bs.pUserData = pUserData;
6235  drflac__reset_cache(&pInit->bs);
6236 
6237 
6238  /* If the container is explicitly defined then we can try opening in relaxed mode. */
6239  relaxed = container != drflac_container_unknown;
6240 
6241  /* Skip over any ID3 tags. */
6242  for (;;) {
6243  if (onRead(pUserData, id, 4) != 4) {
6244  return DRFLAC_FALSE; /* Ran out of data. */
6245  }
6246  pInit->runningFilePos += 4;
6247 
6248  if (id[0] == 'I' && id[1] == 'D' && id[2] == '3') {
6249  drflac_uint8 header[6];
6250  drflac_uint8 flags;
6251  drflac_uint32 headerSize;
6252 
6253  if (onRead(pUserData, header, 6) != 6) {
6254  return DRFLAC_FALSE; /* Ran out of data. */
6255  }
6256  pInit->runningFilePos += 6;
6257 
6258  flags = header[1];
6259 
6260  drflac_copy_memory(&headerSize, header+2, 4);
6261  headerSize = drflac__unsynchsafe_32(drflac__be2host_32(headerSize));
6262  if (flags & 0x10) {
6263  headerSize += 10;
6264  }
6265 
6266  if (!onSeek(pUserData, headerSize, drflac_seek_origin_current)) {
6267  return DRFLAC_FALSE; /* Failed to seek past the tag. */
6268  }
6269  pInit->runningFilePos += headerSize;
6270  } else {
6271  break;
6272  }
6273  }
6274 
6275  if (id[0] == 'f' && id[1] == 'L' && id[2] == 'a' && id[3] == 'C') {
6276  return drflac__init_private__native(pInit, onRead, onSeek, onMeta, pUserData, pUserDataMD, relaxed);
6277  }
6278 #ifndef DR_FLAC_NO_OGG
6279  if (id[0] == 'O' && id[1] == 'g' && id[2] == 'g' && id[3] == 'S') {
6280  return drflac__init_private__ogg(pInit, onRead, onSeek, onMeta, pUserData, pUserDataMD, relaxed);
6281  }
6282 #endif
6283 
6284  /* If we get here it means we likely don't have a header. Try opening in relaxed mode, if applicable. */
6285  if (relaxed) {
6286  if (container == drflac_container_native) {
6287  return drflac__init_private__native(pInit, onRead, onSeek, onMeta, pUserData, pUserDataMD, relaxed);
6288  }
6289 #ifndef DR_FLAC_NO_OGG
6290  if (container == drflac_container_ogg) {
6291  return drflac__init_private__ogg(pInit, onRead, onSeek, onMeta, pUserData, pUserDataMD, relaxed);
6292  }
6293 #endif
6294  }
6295 
6296  /* Unsupported container. */
6297  return DRFLAC_FALSE;
6298 }
6299 
6300 void drflac__init_from_info(drflac* pFlac, drflac_init_info* pInit)
6301 {
6302  drflac_assert(pFlac != NULL);
6303  drflac_assert(pInit != NULL);
6304 
6305  drflac_zero_memory(pFlac, sizeof(*pFlac));
6306  pFlac->bs = pInit->bs;
6307  pFlac->onMeta = pInit->onMeta;
6308  pFlac->pUserDataMD = pInit->pUserDataMD;
6309  pFlac->maxBlockSize = pInit->maxBlockSize;
6310  pFlac->sampleRate = pInit->sampleRate;
6311  pFlac->channels = (drflac_uint8)pInit->channels;
6312  pFlac->bitsPerSample = (drflac_uint8)pInit->bitsPerSample;
6313  pFlac->totalSampleCount = pInit->totalSampleCount;
6314  pFlac->totalPCMFrameCount = pInit->totalSampleCount / pFlac->channels;
6315  pFlac->container = pInit->container;
6316 }
6317 
6318 drflac* drflac_open_with_metadata_private(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_meta_proc onMeta, drflac_container container, void* pUserData, void* pUserDataMD)
6319 {
6320  drflac_init_info init;
6321  drflac_uint32 allocationSize;
6322  drflac_uint32 wholeSIMDVectorCountPerChannel;
6323  drflac_uint32 decodedSamplesAllocationSize;
6324 #ifndef DR_FLAC_NO_OGG
6325  drflac_uint32 oggbsAllocationSize;
6326  drflac_oggbs oggbs;
6327 #endif
6328  drflac_uint64 firstFramePos;
6329  drflac_uint64 seektablePos;
6330  drflac_uint32 seektableSize;
6331  drflac* pFlac;
6332 
6333 #ifndef DRFLAC_NO_CPUID
6334  /* CPU support first. */
6335  drflac__init_cpu_caps();
6336 #endif
6337 
6338  if (!drflac__init_private(&init, onRead, onSeek, onMeta, container, pUserData, pUserDataMD)) {
6339  return NULL;
6340  }
6341 
6342  /*
6343  The size of the allocation for the drflac object needs to be large enough to fit the following:
6344  1) The main members of the drflac structure
6345  2) A block of memory large enough to store the decoded samples of the largest frame in the stream
6346  3) If the container is Ogg, a drflac_oggbs object
6347 
6348  The complicated part of the allocation is making sure there's enough room the decoded samples, taking into consideration
6349  the different SIMD instruction sets.
6350  */
6351  allocationSize = sizeof(drflac);
6352 
6353  /*
6354  The allocation size for decoded frames depends on the number of 32-bit integers that fit inside the largest SIMD vector
6355  we are supporting.
6356  */
6357  if (((init.maxBlockSize+DRFLAC_LEADING_SAMPLES) % (DRFLAC_MAX_SIMD_VECTOR_SIZE / sizeof(drflac_int32))) == 0) {
6358  wholeSIMDVectorCountPerChannel = ((init.maxBlockSize+DRFLAC_LEADING_SAMPLES) / (DRFLAC_MAX_SIMD_VECTOR_SIZE / sizeof(drflac_int32)));
6359  } else {
6360  wholeSIMDVectorCountPerChannel = ((init.maxBlockSize+DRFLAC_LEADING_SAMPLES) / (DRFLAC_MAX_SIMD_VECTOR_SIZE / sizeof(drflac_int32))) + 1;
6361  }
6362 
6363  decodedSamplesAllocationSize = wholeSIMDVectorCountPerChannel * DRFLAC_MAX_SIMD_VECTOR_SIZE * init.channels;
6364 
6365  allocationSize += decodedSamplesAllocationSize;
6366  allocationSize += DRFLAC_MAX_SIMD_VECTOR_SIZE; /* Allocate extra bytes to ensure we have enough for alignment. */
6367 
6368 #ifndef DR_FLAC_NO_OGG
6369  /* There's additional data required for Ogg streams. */
6370  oggbsAllocationSize = 0;
6371  if (init.container == drflac_container_ogg) {
6372  oggbsAllocationSize = sizeof(drflac_oggbs);
6373  allocationSize += oggbsAllocationSize;
6374  }
6375 
6376  drflac_zero_memory(&oggbs, sizeof(oggbs));
6377  if (init.container == drflac_container_ogg) {
6378  oggbs.onRead = onRead;
6379  oggbs.onSeek = onSeek;
6380  oggbs.pUserData = pUserData;
6381  oggbs.currentBytePos = init.oggFirstBytePos;
6382  oggbs.firstBytePos = init.oggFirstBytePos;
6383  oggbs.serialNumber = init.oggSerial;
6384  oggbs.bosPageHeader = init.oggBosHeader;
6385  oggbs.bytesRemainingInPage = 0;
6386  }
6387 #endif
6388 
6389  /*
6390  This part is a bit awkward. We need to load the seektable so that it can be referenced in-memory, but I want the drflac object to
6391  consist of only a single heap allocation. To this, the size of the seek table needs to be known, which we determine when reading
6392  and decoding the metadata.
6393  */
6394  firstFramePos = 42; /* <-- We know we are at byte 42 at this point. */
6395  seektablePos = 0;
6396  seektableSize = 0;
6397  if (init.hasMetadataBlocks) {
6398  drflac_read_proc onReadOverride = onRead;
6399  drflac_seek_proc onSeekOverride = onSeek;
6400  void* pUserDataOverride = pUserData;
6401 
6402 #ifndef DR_FLAC_NO_OGG
6403  if (init.container == drflac_container_ogg) {
6404  onReadOverride = drflac__on_read_ogg;
6405  onSeekOverride = drflac__on_seek_ogg;
6406  pUserDataOverride = (void*)&oggbs;
6407  }
6408 #endif
6409 
6410  if (!drflac__read_and_decode_metadata(onReadOverride, onSeekOverride, onMeta, pUserDataOverride, pUserDataMD, &firstFramePos, &seektablePos, &seektableSize)) {
6411  return NULL;
6412  }
6413 
6414  allocationSize += seektableSize;
6415  }
6416 
6417 
6418  pFlac = (drflac*)DRFLAC_MALLOC(allocationSize);
6419  drflac__init_from_info(pFlac, &init);
6420  pFlac->pDecodedSamples = (drflac_int32*)drflac_align((size_t)pFlac->pExtraData, DRFLAC_MAX_SIMD_VECTOR_SIZE);
6421 
6422 #ifndef DR_FLAC_NO_OGG
6423  if (init.container == drflac_container_ogg) {
6424  drflac_oggbs* pInternalOggbs = (drflac_oggbs*)((drflac_uint8*)pFlac->pDecodedSamples + decodedSamplesAllocationSize + seektableSize);
6425  *pInternalOggbs = oggbs;
6426 
6427  /* The Ogg bistream needs to be layered on top of the original bitstream. */
6428  pFlac->bs.onRead = drflac__on_read_ogg;
6429  pFlac->bs.onSeek = drflac__on_seek_ogg;
6430  pFlac->bs.pUserData = (void*)pInternalOggbs;
6431  pFlac->_oggbs = (void*)pInternalOggbs;
6432  }
6433 #endif
6434 
6435  pFlac->firstFramePos = firstFramePos;
6436 
6437  /* NOTE: Seektables are not currently compatible with Ogg encapsulation (Ogg has its own accelerated seeking system). I may change this later, so I'm leaving this here for now. */
6438 #ifndef DR_FLAC_NO_OGG
6439  if (init.container == drflac_container_ogg)
6440  {
6441  pFlac->pSeekpoints = NULL;
6442  pFlac->seekpointCount = 0;
6443  }
6444  else
6445 #endif
6446  {
6447  /* If we have a seektable we need to load it now, making sure we move back to where we were previously. */
6448  if (seektablePos != 0) {
6449  pFlac->seekpointCount = seektableSize / sizeof(*pFlac->pSeekpoints);
6450  pFlac->pSeekpoints = (drflac_seekpoint*)((drflac_uint8*)pFlac->pDecodedSamples + decodedSamplesAllocationSize);
6451 
6452  /* Seek to the seektable, then just read directly into our seektable buffer. */
6453  if (pFlac->bs.onSeek(pFlac->bs.pUserData, (int)seektablePos, drflac_seek_origin_start)) {
6454  if (pFlac->bs.onRead(pFlac->bs.pUserData, pFlac->pSeekpoints, seektableSize) == seektableSize) {
6455  /* Endian swap. */
6456  drflac_uint32 iSeekpoint;
6457  for (iSeekpoint = 0; iSeekpoint < pFlac->seekpointCount; ++iSeekpoint) {
6458  pFlac->pSeekpoints[iSeekpoint].firstSample = drflac__be2host_64(pFlac->pSeekpoints[iSeekpoint].firstSample);
6459  pFlac->pSeekpoints[iSeekpoint].frameOffset = drflac__be2host_64(pFlac->pSeekpoints[iSeekpoint].frameOffset);
6460  pFlac->pSeekpoints[iSeekpoint].sampleCount = drflac__be2host_16(pFlac->pSeekpoints[iSeekpoint].sampleCount);
6461  }
6462  } else {
6463  /* Failed to read the seektable. Pretend we don't have one. */
6464  pFlac->pSeekpoints = NULL;
6465  pFlac->seekpointCount = 0;
6466  }
6467 
6468  /* We need to seek back to where we were. If this fails it's a critical error. */
6469  if (!pFlac->bs.onSeek(pFlac->bs.pUserData, (int)pFlac->firstFramePos, drflac_seek_origin_start)) {
6470  DRFLAC_FREE(pFlac);
6471  return NULL;
6472  }
6473  } else {
6474  /* Failed to seek to the seektable. Ominous sign, but for now we can just pretend we don't have one. */
6475  pFlac->pSeekpoints = NULL;
6476  pFlac->seekpointCount = 0;
6477  }
6478  }
6479  }
6480 
6481 
6482  /*
6483  If we get here, but don't have a STREAMINFO block, it means we've opened the stream in relaxed mode and need to decode
6484  the first frame.
6485  */
6486  if (!init.hasStreamInfoBlock) {
6487  pFlac->currentFrame.header = init.firstFrameHeader;
6488  do
6489  {
6490  drflac_result result = drflac__decode_flac_frame(pFlac);
6491  if (result == DRFLAC_SUCCESS) {
6492  break;
6493  } else {
6494  if (result == DRFLAC_CRC_MISMATCH) {
6495  if (!drflac__read_next_flac_frame_header(&pFlac->bs, pFlac->bitsPerSample, &pFlac->currentFrame.header)) {
6496  DRFLAC_FREE(pFlac);
6497  return NULL;
6498  }
6499  continue;
6500  } else {
6501  DRFLAC_FREE(pFlac);
6502  return NULL;
6503  }
6504  }
6505  } while (1);
6506  }
6507 
6508  return pFlac;
6509 }
6510 
6511 
6512 
6513 #ifndef DR_FLAC_NO_STDIO
6514 #include <stdio.h>
6515 
6516 static size_t drflac__on_read_stdio(void* pUserData, void* bufferOut, size_t bytesToRead)
6517 {
6518  return fread(bufferOut, 1, bytesToRead, (FILE*)pUserData);
6519 }
6520 
6521 static drflac_bool32 drflac__on_seek_stdio(void* pUserData, int offset, drflac_seek_origin origin)
6522 {
6523  drflac_assert(offset >= 0); /* <-- Never seek backwards. */
6524 
6525  return fseek((FILE*)pUserData, offset, (origin == drflac_seek_origin_current) ? SEEK_CUR : SEEK_SET) == 0;
6526 }
6527 
6528 static FILE* drflac__fopen(const char* filename)
6529 {
6530  FILE* pFile;
6531 #if defined(_MSC_VER) && _MSC_VER >= 1400
6532  if (fopen_s(&pFile, filename, "rb") != 0) {
6533  return NULL;
6534  }
6535 #else
6536  pFile = fopen(filename, "rb");
6537  if (pFile == NULL) {
6538  return NULL;
6539  }
6540 #endif
6541 
6542  return pFile;
6543 }
6544 
6545 
6546 drflac* drflac_open_file(const char* filename)
6547 {
6548  drflac* pFlac;
6549  FILE* pFile;
6550 
6551  pFile = drflac__fopen(filename);
6552  if (pFile == NULL) {
6553  return NULL;
6554  }
6555 
6556  pFlac = drflac_open(drflac__on_read_stdio, drflac__on_seek_stdio, (void*)pFile);
6557  if (pFlac == NULL) {
6558  fclose(pFile);
6559  return NULL;
6560  }
6561 
6562  return pFlac;
6563 }
6564 
6565 drflac* drflac_open_file_with_metadata(const char* filename, drflac_meta_proc onMeta, void* pUserData)
6566 {
6567  drflac* pFlac;
6568  FILE* pFile;
6569 
6570  pFile = drflac__fopen(filename);
6571  if (pFile == NULL) {
6572  return NULL;
6573  }
6574 
6575  pFlac = drflac_open_with_metadata_private(drflac__on_read_stdio, drflac__on_seek_stdio, onMeta, drflac_container_unknown, (void*)pFile, pUserData);
6576  if (pFlac == NULL) {
6577  fclose(pFile);
6578  return pFlac;
6579  }
6580 
6581  return pFlac;
6582 }
6583 #endif /* DR_FLAC_NO_STDIO */
6584 
6585 static size_t drflac__on_read_memory(void* pUserData, void* bufferOut, size_t bytesToRead)
6586 {
6587  drflac__memory_stream* memoryStream = (drflac__memory_stream*)pUserData;
6588  size_t bytesRemaining;
6589 
6590  drflac_assert(memoryStream != NULL);
6591  drflac_assert(memoryStream->dataSize >= memoryStream->currentReadPos);
6592 
6593  bytesRemaining = memoryStream->dataSize - memoryStream->currentReadPos;
6594  if (bytesToRead > bytesRemaining) {
6595  bytesToRead = bytesRemaining;
6596  }
6597 
6598  if (bytesToRead > 0) {
6599  drflac_copy_memory(bufferOut, memoryStream->data + memoryStream->currentReadPos, bytesToRead);
6600  memoryStream->currentReadPos += bytesToRead;
6601  }
6602 
6603  return bytesToRead;
6604 }
6605 
6606 static drflac_bool32 drflac__on_seek_memory(void* pUserData, int offset, drflac_seek_origin origin)
6607 {
6608  drflac__memory_stream* memoryStream = (drflac__memory_stream*)pUserData;
6609 
6610  drflac_assert(memoryStream != NULL);
6611  drflac_assert(offset >= 0); /* <-- Never seek backwards. */
6612 
6613  if (offset > (drflac_int64)memoryStream->dataSize) {
6614  return DRFLAC_FALSE;
6615  }
6616 
6617  if (origin == drflac_seek_origin_current) {
6618  if (memoryStream->currentReadPos + offset <= memoryStream->dataSize) {
6619  memoryStream->currentReadPos += offset;
6620  } else {
6621  return DRFLAC_FALSE; /* Trying to seek too far forward. */
6622  }
6623  } else {
6624  if ((drflac_uint32)offset <= memoryStream->dataSize) {
6625  memoryStream->currentReadPos = offset;
6626  } else {
6627  return DRFLAC_FALSE; /* Trying to seek too far forward. */
6628  }
6629  }
6630 
6631  return DRFLAC_TRUE;
6632 }
6633 
6634 drflac* drflac_open_memory(const void* data, size_t dataSize)
6635 {
6636  drflac__memory_stream memoryStream;
6637  drflac* pFlac;
6638 
6639  memoryStream.data = (const unsigned char*)data;
6640  memoryStream.dataSize = dataSize;
6641  memoryStream.currentReadPos = 0;
6642  pFlac = drflac_open(drflac__on_read_memory, drflac__on_seek_memory, &memoryStream);
6643  if (pFlac == NULL) {
6644  return NULL;
6645  }
6646 
6647  pFlac->memoryStream = memoryStream;
6648 
6649  /* This is an awful hack... */
6650 #ifndef DR_FLAC_NO_OGG
6651  if (pFlac->container == drflac_container_ogg)
6652  {
6653  drflac_oggbs* oggbs = (drflac_oggbs*)pFlac->_oggbs;
6654  oggbs->pUserData = &pFlac->memoryStream;
6655  }
6656  else
6657 #endif
6658  {
6659  pFlac->bs.pUserData = &pFlac->memoryStream;
6660  }
6661 
6662  return pFlac;
6663 }
6664 
6665 drflac* drflac_open_memory_with_metadata(const void* data, size_t dataSize, drflac_meta_proc onMeta, void* pUserData)
6666 {
6667  drflac__memory_stream memoryStream;
6668  drflac* pFlac;
6669 
6670  memoryStream.data = (const unsigned char*)data;
6671  memoryStream.dataSize = dataSize;
6672  memoryStream.currentReadPos = 0;
6673  pFlac = drflac_open_with_metadata_private(drflac__on_read_memory, drflac__on_seek_memory, onMeta, drflac_container_unknown, &memoryStream, pUserData);
6674  if (pFlac == NULL) {
6675  return NULL;
6676  }
6677 
6678  pFlac->memoryStream = memoryStream;
6679 
6680  /* This is an awful hack... */
6681 #ifndef DR_FLAC_NO_OGG
6682  if (pFlac->container == drflac_container_ogg)
6683  {
6684  drflac_oggbs* oggbs = (drflac_oggbs*)pFlac->_oggbs;
6685  oggbs->pUserData = &pFlac->memoryStream;
6686  }
6687  else
6688 #endif
6689  {
6690  pFlac->bs.pUserData = &pFlac->memoryStream;
6691  }
6692 
6693  return pFlac;
6694 }
6695 
6696 
6697 
6698 drflac* drflac_open(drflac_read_proc onRead, drflac_seek_proc onSeek, void* pUserData)
6699 {
6700  return drflac_open_with_metadata_private(onRead, onSeek, NULL, drflac_container_unknown, pUserData, pUserData);
6701 }
6702 drflac* drflac_open_relaxed(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_container container, void* pUserData)
6703 {
6704  return drflac_open_with_metadata_private(onRead, onSeek, NULL, container, pUserData, pUserData);
6705 }
6706 
6707 drflac* drflac_open_with_metadata(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_meta_proc onMeta, void* pUserData)
6708 {
6709  return drflac_open_with_metadata_private(onRead, onSeek, onMeta, drflac_container_unknown, pUserData, pUserData);
6710 }
6712 {
6713  return drflac_open_with_metadata_private(onRead, onSeek, onMeta, container, pUserData, pUserData);
6714 }
6715 
6716 void drflac_close(drflac* pFlac)
6717 {
6718  if (pFlac == NULL) {
6719  return;
6720  }
6721 
6722 #ifndef DR_FLAC_NO_STDIO
6723  /*
6724  If we opened the file with drflac_open_file() we will want to close the file handle. We can know whether or not drflac_open_file()
6725  was used by looking at the callbacks.
6726  */
6727  if (pFlac->bs.onRead == drflac__on_read_stdio) {
6728  fclose((FILE*)pFlac->bs.pUserData);
6729  }
6730 
6731 #ifndef DR_FLAC_NO_OGG
6732  /* Need to clean up Ogg streams a bit differently due to the way the bit streaming is chained. */
6733  if (pFlac->container == drflac_container_ogg) {
6734  drflac_oggbs* oggbs = (drflac_oggbs*)pFlac->_oggbs;
6735  drflac_assert(pFlac->bs.onRead == drflac__on_read_ogg);
6736 
6737  if (oggbs->onRead == drflac__on_read_stdio) {
6738  fclose((FILE*)oggbs->pUserData);
6739  }
6740  }
6741 #endif
6742 #endif
6743 
6744  DRFLAC_FREE(pFlac);
6745 }
6746 
6747 drflac_uint64 drflac__read_s32__misaligned(drflac* pFlac, drflac_uint64 samplesToRead, drflac_int32* bufferOut)
6748 {
6749  unsigned int channelCount = drflac__get_channel_count_from_channel_assignment(pFlac->currentFrame.header.channelAssignment);
6750  drflac_uint64 samplesRead;
6751 
6752  /* We should never be calling this when the number of samples to read is >= the sample count. */
6753  drflac_assert(samplesToRead < channelCount);
6754  drflac_assert(pFlac->currentFrame.samplesRemaining > 0 && samplesToRead <= pFlac->currentFrame.samplesRemaining);
6755 
6756  samplesRead = 0;
6757  while (samplesToRead > 0) {
6758  drflac_uint64 totalSamplesInFrame = pFlac->currentFrame.header.blockSize * channelCount;
6759  drflac_uint64 samplesReadFromFrameSoFar = totalSamplesInFrame - pFlac->currentFrame.samplesRemaining;
6760  drflac_uint64 channelIndex = samplesReadFromFrameSoFar % channelCount;
6761  drflac_uint64 nextSampleInFrame = samplesReadFromFrameSoFar / channelCount;
6762  int decodedSample = 0;
6763 
6764  switch (pFlac->currentFrame.header.channelAssignment)
6765  {
6766  case DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE:
6767  {
6768  if (channelIndex == 0) {
6769  decodedSample = (int)((drflac_uint32)pFlac->currentFrame.subframes[channelIndex + 0].pDecodedSamples[nextSampleInFrame] << pFlac->currentFrame.subframes[channelIndex + 0].wastedBitsPerSample);
6770  } else {
6771  int side = (int)((drflac_uint32)pFlac->currentFrame.subframes[channelIndex + 0].pDecodedSamples[nextSampleInFrame] << pFlac->currentFrame.subframes[channelIndex + 0].wastedBitsPerSample);
6772  int left = (int)((drflac_uint32)pFlac->currentFrame.subframes[channelIndex - 1].pDecodedSamples[nextSampleInFrame] << pFlac->currentFrame.subframes[channelIndex - 1].wastedBitsPerSample);
6773  decodedSample = left - side;
6774  }
6775  } break;
6776 
6777  case DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE:
6778  {
6779  if (channelIndex == 0) {
6780  int side = (int)((drflac_uint32)pFlac->currentFrame.subframes[channelIndex + 0].pDecodedSamples[nextSampleInFrame] << pFlac->currentFrame.subframes[channelIndex + 0].wastedBitsPerSample);
6781  int right = (int)((drflac_uint32)pFlac->currentFrame.subframes[channelIndex + 1].pDecodedSamples[nextSampleInFrame] << pFlac->currentFrame.subframes[channelIndex + 1].wastedBitsPerSample);
6782  decodedSample = side + right;
6783  } else {
6784  decodedSample = (int)((drflac_uint32)pFlac->currentFrame.subframes[channelIndex + 0].pDecodedSamples[nextSampleInFrame] << pFlac->currentFrame.subframes[channelIndex + 0].wastedBitsPerSample);
6785  }
6786  } break;
6787 
6788  case DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE:
6789  {
6790  int mid;
6791  int side;
6792  if (channelIndex == 0) {
6793  mid = (int)((drflac_uint32)pFlac->currentFrame.subframes[channelIndex + 0].pDecodedSamples[nextSampleInFrame] << pFlac->currentFrame.subframes[channelIndex + 0].wastedBitsPerSample);
6794  side = (int)((drflac_uint32)pFlac->currentFrame.subframes[channelIndex + 1].pDecodedSamples[nextSampleInFrame] << pFlac->currentFrame.subframes[channelIndex + 1].wastedBitsPerSample);
6795 
6796  mid = (((unsigned int)mid) << 1) | (side & 0x01);
6797  decodedSample = (mid + side) >> 1;
6798  } else {
6799  mid = (int)((drflac_uint32)pFlac->currentFrame.subframes[channelIndex - 1].pDecodedSamples[nextSampleInFrame] << pFlac->currentFrame.subframes[channelIndex - 1].wastedBitsPerSample);
6800  side = (int)((drflac_uint32)pFlac->currentFrame.subframes[channelIndex + 0].pDecodedSamples[nextSampleInFrame] << pFlac->currentFrame.subframes[channelIndex + 0].wastedBitsPerSample);
6801 
6802  mid = (((unsigned int)mid) << 1) | (side & 0x01);
6803  decodedSample = (mid - side) >> 1;
6804  }
6805  } break;
6806 
6807  case DRFLAC_CHANNEL_ASSIGNMENT_INDEPENDENT:
6808  default:
6809  {
6810  decodedSample = (int)((drflac_uint32)pFlac->currentFrame.subframes[channelIndex + 0].pDecodedSamples[nextSampleInFrame] << pFlac->currentFrame.subframes[channelIndex + 0].wastedBitsPerSample);
6811  } break;
6812  }
6813 
6814  decodedSample = (int)((drflac_uint32)decodedSample << (32 - pFlac->bitsPerSample));
6815 
6816  if (bufferOut) {
6817  *bufferOut++ = decodedSample;
6818  }
6819 
6820  samplesRead += 1;
6821  pFlac->currentFrame.samplesRemaining -= 1;
6822  samplesToRead -= 1;
6823  }
6824 
6825  return samplesRead;
6826 }
6827 
6828 drflac_uint64 drflac_read_s32(drflac* pFlac, drflac_uint64 samplesToRead, drflac_int32* bufferOut)
6829 {
6830  drflac_uint64 samplesRead;
6831 
6832  /* Note that <bufferOut> is allowed to be null, in which case this will act like a seek. */
6833  if (pFlac == NULL || samplesToRead == 0) {
6834  return 0;
6835  }
6836 
6837  if (bufferOut == NULL) {
6838  return drflac__seek_forward_by_samples(pFlac, samplesToRead);
6839  }
6840 
6841  samplesRead = 0;
6842  while (samplesToRead > 0) {
6843  /* If we've run out of samples in this frame, go to the next. */
6844  if (pFlac->currentFrame.samplesRemaining == 0) {
6845  if (!drflac__read_and_decode_next_flac_frame(pFlac)) {
6846  break; /* Couldn't read the next frame, so just break from the loop and return. */
6847  }
6848  } else {
6849  /* Here is where we grab the samples and interleave them. */
6850  unsigned int channelCount = drflac__get_channel_count_from_channel_assignment(pFlac->currentFrame.header.channelAssignment);
6851  drflac_uint64 totalSamplesInFrame = pFlac->currentFrame.header.blockSize * channelCount;
6852  drflac_uint64 samplesReadFromFrameSoFar = totalSamplesInFrame - pFlac->currentFrame.samplesRemaining;
6853  drflac_uint64 misalignedSampleCount = samplesReadFromFrameSoFar % channelCount;
6854  drflac_uint64 alignedSampleCountPerChannel;
6855  drflac_uint64 firstAlignedSampleInFrame;
6856  unsigned int unusedBitsPerSample;
6857  drflac_uint64 alignedSamplesRead;
6858 
6859  if (misalignedSampleCount > 0) {
6860  drflac_uint64 misalignedSamplesRead = drflac__read_s32__misaligned(pFlac, misalignedSampleCount, bufferOut);
6861  samplesRead += misalignedSamplesRead;
6862  samplesReadFromFrameSoFar += misalignedSamplesRead;
6863  bufferOut += misalignedSamplesRead;
6864  samplesToRead -= misalignedSamplesRead;
6865  pFlac->currentSample += misalignedSamplesRead;
6866  }
6867 
6868 
6869  alignedSampleCountPerChannel = samplesToRead / channelCount;
6870  if (alignedSampleCountPerChannel > pFlac->currentFrame.samplesRemaining / channelCount) {
6871  alignedSampleCountPerChannel = pFlac->currentFrame.samplesRemaining / channelCount;
6872  }
6873 
6874  firstAlignedSampleInFrame = samplesReadFromFrameSoFar / channelCount;
6875  unusedBitsPerSample = 32 - pFlac->bitsPerSample;
6876 
6877  switch (pFlac->currentFrame.header.channelAssignment)
6878  {
6879  case DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE:
6880  {
6881  drflac_uint64 i;
6882  const drflac_int32* pDecodedSamples0 = pFlac->currentFrame.subframes[0].pDecodedSamples + firstAlignedSampleInFrame;
6883  const drflac_int32* pDecodedSamples1 = pFlac->currentFrame.subframes[1].pDecodedSamples + firstAlignedSampleInFrame;
6884 
6885  for (i = 0; i < alignedSampleCountPerChannel; ++i) {
6886  int left = (int)((drflac_uint32)pDecodedSamples0[i] << (unusedBitsPerSample + pFlac->currentFrame.subframes[0].wastedBitsPerSample));
6887  int side = (int)((drflac_uint32)pDecodedSamples1[i] << (unusedBitsPerSample + pFlac->currentFrame.subframes[1].wastedBitsPerSample));
6888  int right = left - side;
6889 
6890  bufferOut[i*2+0] = left;
6891  bufferOut[i*2+1] = right;
6892  }
6893  } break;
6894 
6895  case DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE:
6896  {
6897  drflac_uint64 i;
6898  const drflac_int32* pDecodedSamples0 = pFlac->currentFrame.subframes[0].pDecodedSamples + firstAlignedSampleInFrame;
6899  const drflac_int32* pDecodedSamples1 = pFlac->currentFrame.subframes[1].pDecodedSamples + firstAlignedSampleInFrame;
6900 
6901  for (i = 0; i < alignedSampleCountPerChannel; ++i) {
6902  int side = (int)((drflac_uint32)pDecodedSamples0[i] << (unusedBitsPerSample + pFlac->currentFrame.subframes[0].wastedBitsPerSample));
6903  int right = (int)((drflac_uint32)pDecodedSamples1[i] << (unusedBitsPerSample + pFlac->currentFrame.subframes[1].wastedBitsPerSample));
6904  int left = right + side;
6905 
6906  bufferOut[i*2+0] = left;
6907  bufferOut[i*2+1] = right;
6908  }
6909  } break;
6910 
6911  case DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE:
6912  {
6913  drflac_uint64 i;
6914  const drflac_int32* pDecodedSamples0 = pFlac->currentFrame.subframes[0].pDecodedSamples + firstAlignedSampleInFrame;
6915  const drflac_int32* pDecodedSamples1 = pFlac->currentFrame.subframes[1].pDecodedSamples + firstAlignedSampleInFrame;
6916 
6917  for (i = 0; i < alignedSampleCountPerChannel; ++i) {
6918  int mid = (int)((drflac_uint32)pDecodedSamples0[i] << pFlac->currentFrame.subframes[0].wastedBitsPerSample);
6919  int side = (int)((drflac_uint32)pDecodedSamples1[i] << pFlac->currentFrame.subframes[1].wastedBitsPerSample);
6920 
6921  mid = (((drflac_uint32)mid) << 1) | (side & 0x01);
6922 
6923  bufferOut[i*2+0] = (drflac_int32)((drflac_uint32)((mid + side) >> 1) << (unusedBitsPerSample));
6924  bufferOut[i*2+1] = (drflac_int32)((drflac_uint32)((mid - side) >> 1) << (unusedBitsPerSample));
6925  }
6926  } break;
6927 
6928  case DRFLAC_CHANNEL_ASSIGNMENT_INDEPENDENT:
6929  default:
6930  {
6931  if (pFlac->currentFrame.header.channelAssignment == 1) /* 1 = Stereo */
6932  {
6933  /* Stereo optimized inner loop unroll. */
6934  drflac_uint64 i;
6935  const drflac_int32* pDecodedSamples0 = pFlac->currentFrame.subframes[0].pDecodedSamples + firstAlignedSampleInFrame;
6936  const drflac_int32* pDecodedSamples1 = pFlac->currentFrame.subframes[1].pDecodedSamples + firstAlignedSampleInFrame;
6937 
6938  for (i = 0; i < alignedSampleCountPerChannel; ++i) {
6939  bufferOut[i*2+0] = (drflac_int32)((drflac_uint32)pDecodedSamples0[i] << (unusedBitsPerSample + pFlac->currentFrame.subframes[0].wastedBitsPerSample));
6940  bufferOut[i*2+1] = (drflac_int32)((drflac_uint32)pDecodedSamples1[i] << (unusedBitsPerSample + pFlac->currentFrame.subframes[1].wastedBitsPerSample));
6941  }
6942  }
6943  else
6944  {
6945  /* Generic interleaving. */
6946  drflac_uint64 i;
6947  for (i = 0; i < alignedSampleCountPerChannel; ++i) {
6948  unsigned int j;
6949  for (j = 0; j < channelCount; ++j) {
6950  bufferOut[(i*channelCount)+j] = (drflac_int32)((drflac_uint32)(pFlac->currentFrame.subframes[j].pDecodedSamples[firstAlignedSampleInFrame + i]) << (unusedBitsPerSample + pFlac->currentFrame.subframes[j].wastedBitsPerSample));
6951  }
6952  }
6953  }
6954  } break;
6955  }
6956 
6957  alignedSamplesRead = alignedSampleCountPerChannel * channelCount;
6958  samplesRead += alignedSamplesRead;
6959  samplesReadFromFrameSoFar += alignedSamplesRead;
6960  bufferOut += alignedSamplesRead;
6961  samplesToRead -= alignedSamplesRead;
6962  pFlac->currentSample += alignedSamplesRead;
6963  pFlac->currentFrame.samplesRemaining -= (unsigned int)alignedSamplesRead;
6964 
6965 
6966  /* At this point we may still have some excess samples left to read. */
6967  if (samplesToRead > 0 && pFlac->currentFrame.samplesRemaining > 0) {
6968  drflac_uint64 excessSamplesRead = 0;
6969  if (samplesToRead < pFlac->currentFrame.samplesRemaining) {
6970  excessSamplesRead = drflac__read_s32__misaligned(pFlac, samplesToRead, bufferOut);
6971  } else {
6972  excessSamplesRead = drflac__read_s32__misaligned(pFlac, pFlac->currentFrame.samplesRemaining, bufferOut);
6973  }
6974 
6975  samplesRead += excessSamplesRead;
6976  samplesReadFromFrameSoFar += excessSamplesRead;
6977  bufferOut += excessSamplesRead;
6978  samplesToRead -= excessSamplesRead;
6979  pFlac->currentSample += excessSamplesRead;
6980  }
6981  }
6982  }
6983 
6984  return samplesRead;
6985 }
6986 
6988 {
6989 #if defined(_MSC_VER) && !defined(__clang__)
6990  #pragma warning(push)
6991  #pragma warning(disable:4996) /* was declared deprecated */
6992 #elif defined(__GNUC__) || defined(__clang__)
6993  #pragma GCC diagnostic push
6994  #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
6995 #endif
6996  return drflac_read_s32(pFlac, framesToRead*pFlac->channels, pBufferOut) / pFlac->channels;
6997 #if defined(_MSC_VER) && !defined(__clang__)
6998  #pragma warning(pop)
6999 #elif defined(__GNUC__) || defined(__clang__)
7000  #pragma GCC diagnostic pop
7001 #endif
7002 }
7003 
7004 
7005 drflac_uint64 drflac_read_s16(drflac* pFlac, drflac_uint64 samplesToRead, drflac_int16* pBufferOut)
7006 {
7007  /* This reads samples in 2 passes and can probably be optimized. */
7008  drflac_uint64 totalSamplesRead = 0;
7009 
7010 #if defined(_MSC_VER) && !defined(__clang__)
7011  #pragma warning(push)
7012  #pragma warning(disable:4996) /* was declared deprecated */
7013 #elif defined(__GNUC__) || defined(__clang__)
7014  #pragma GCC diagnostic push
7015  #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
7016 #endif
7017 
7018  while (samplesToRead > 0) {
7019  drflac_uint64 i;
7020  drflac_int32 samples32[4096];
7021  drflac_uint64 samplesJustRead = drflac_read_s32(pFlac, (samplesToRead > 4096) ? 4096 : samplesToRead, samples32);
7022  if (samplesJustRead == 0) {
7023  break; /* Reached the end. */
7024  }
7025 
7026  /* s32 -> s16 */
7027  for (i = 0; i < samplesJustRead; ++i) {
7028  pBufferOut[i] = (drflac_int16)(samples32[i] >> 16);
7029  }
7030 
7031  totalSamplesRead += samplesJustRead;
7032  samplesToRead -= samplesJustRead;
7033  pBufferOut += samplesJustRead;
7034  }
7035 
7036 #if defined(_MSC_VER) && !defined(__clang__)
7037  #pragma warning(pop)
7038 #elif defined(__GNUC__) || defined(__clang__)
7039  #pragma GCC diagnostic pop
7040 #endif
7041 
7042  return totalSamplesRead;
7043 }
7044 
7046 {
7047  /* This reads samples in 2 passes and can probably be optimized. */
7048  drflac_uint64 totalPCMFramesRead = 0;
7049 
7050  while (framesToRead > 0) {
7051  drflac_uint64 iFrame;
7052  drflac_int32 samples32[4096];
7053  drflac_uint64 framesJustRead = drflac_read_pcm_frames_s32(pFlac, (framesToRead > 4096/pFlac->channels) ? 4096/pFlac->channels : framesToRead, samples32);
7054  if (framesJustRead == 0) {
7055  break; /* Reached the end. */
7056  }
7057 
7058  /* s32 -> s16 */
7059  for (iFrame = 0; iFrame < framesJustRead; ++iFrame) {
7060  drflac_uint32 iChannel;
7061  for (iChannel = 0; iChannel < pFlac->channels; ++iChannel) {
7062  drflac_uint64 iSample = iFrame*pFlac->channels + iChannel;
7063  pBufferOut[iSample] = (drflac_int16)(samples32[iSample] >> 16);
7064  }
7065  }
7066 
7067  totalPCMFramesRead += framesJustRead;
7068  framesToRead -= framesJustRead;
7069  pBufferOut += framesJustRead * pFlac->channels;
7070  }
7071 
7072  return totalPCMFramesRead;
7073 }
7074 
7075 
7076 drflac_uint64 drflac_read_f32(drflac* pFlac, drflac_uint64 samplesToRead, float* pBufferOut)
7077 {
7078  /* This reads samples in 2 passes and can probably be optimized. */
7079  drflac_uint64 totalSamplesRead = 0;
7080 
7081 #if defined(_MSC_VER) && !defined(__clang__)
7082  #pragma warning(push)
7083  #pragma warning(disable:4996) /* was declared deprecated */
7084 #elif defined(__GNUC__) || defined(__clang__)
7085  #pragma GCC diagnostic push
7086  #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
7087 #endif
7088 
7089  while (samplesToRead > 0) {
7090  drflac_uint64 i;
7091  drflac_int32 samples32[4096];
7092  drflac_uint64 samplesJustRead = drflac_read_s32(pFlac, (samplesToRead > 4096) ? 4096 : samplesToRead, samples32);
7093  if (samplesJustRead == 0) {
7094  break; /* Reached the end. */
7095  }
7096 
7097  /* s32 -> f32 */
7098  for (i = 0; i < samplesJustRead; ++i) {
7099  pBufferOut[i] = (float)(samples32[i] / 2147483648.0);
7100  }
7101 
7102  totalSamplesRead += samplesJustRead;
7103  samplesToRead -= samplesJustRead;
7104  pBufferOut += samplesJustRead;
7105  }
7106 
7107 #if defined(_MSC_VER) && !defined(__clang__)
7108  #pragma warning(pop)
7109 #elif defined(__GNUC__) || defined(__clang__)
7110  #pragma GCC diagnostic pop
7111 #endif
7112 
7113  return totalSamplesRead;
7114 }
7115 
7116 #if 0
7117 static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_left_side__reference(drflac* pFlac, drflac_uint64 frameCount, drflac_int32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples)
7118 {
7119  drflac_uint64 i;
7120  for (i = 0; i < frameCount; ++i) {
7121  int left = pInputSamples0[i] << (unusedBitsPerSample + pFlac->currentFrame.subframes[0].wastedBitsPerSample);
7122  int side = pInputSamples1[i] << (unusedBitsPerSample + pFlac->currentFrame.subframes[1].wastedBitsPerSample);
7123  int right = left - side;
7124 
7125  pOutputSamples[i*2+0] = (float)(left / 2147483648.0);
7126  pOutputSamples[i*2+1] = (float)(right / 2147483648.0);
7127  }
7128 }
7129 #endif
7130 
7131 static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_left_side__scalar(drflac* pFlac, drflac_uint64 frameCount, drflac_int32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples)
7132 {
7133  drflac_uint64 i;
7134  drflac_uint64 frameCount4 = frameCount >> 2;
7135 
7136  float factor = 1 / 2147483648.0;
7137 
7138  drflac_int32 shift0 = unusedBitsPerSample + pFlac->currentFrame.subframes[0].wastedBitsPerSample;
7139  drflac_int32 shift1 = unusedBitsPerSample + pFlac->currentFrame.subframes[1].wastedBitsPerSample;
7140  for (i = 0; i < frameCount4; ++i) {
7141  drflac_int32 left0 = pInputSamples0[i*4+0] << shift0;
7142  drflac_int32 left1 = pInputSamples0[i*4+1] << shift0;
7143  drflac_int32 left2 = pInputSamples0[i*4+2] << shift0;
7144  drflac_int32 left3 = pInputSamples0[i*4+3] << shift0;
7145 
7146  drflac_int32 side0 = pInputSamples1[i*4+0] << shift1;
7147  drflac_int32 side1 = pInputSamples1[i*4+1] << shift1;
7148  drflac_int32 side2 = pInputSamples1[i*4+2] << shift1;
7149  drflac_int32 side3 = pInputSamples1[i*4+3] << shift1;
7150 
7151  drflac_int32 right0 = left0 - side0;
7152  drflac_int32 right1 = left1 - side1;
7153  drflac_int32 right2 = left2 - side2;
7154  drflac_int32 right3 = left3 - side3;
7155 
7156  pOutputSamples[i*8+0] = left0 * factor;
7157  pOutputSamples[i*8+1] = right0 * factor;
7158  pOutputSamples[i*8+2] = left1 * factor;
7159  pOutputSamples[i*8+3] = right1 * factor;
7160  pOutputSamples[i*8+4] = left2 * factor;
7161  pOutputSamples[i*8+5] = right2 * factor;
7162  pOutputSamples[i*8+6] = left3 * factor;
7163  pOutputSamples[i*8+7] = right3 * factor;
7164  }
7165 
7166  for (i = (frameCount4 << 2); i < frameCount; ++i) {
7167  int left = pInputSamples0[i] << shift0;
7168  int side = pInputSamples1[i] << shift1;
7169  int right = left - side;
7170 
7171  pOutputSamples[i*2+0] = (float)(left * factor);
7172  pOutputSamples[i*2+1] = (float)(right * factor);
7173  }
7174 }
7175 
7176 #if defined(DRFLAC_SUPPORT_SSE2)
7177 static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_left_side__sse2(drflac* pFlac, drflac_uint64 frameCount, drflac_int32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples)
7178 {
7179  drflac_uint64 frameCount4;
7180  __m128 factor;
7181  int shift0;
7182  int shift1;
7183  drflac_uint64 i;
7184 
7185  drflac_assert(pFlac->bitsPerSample <= 24);
7186 
7187  frameCount4 = frameCount >> 2;
7188 
7189  factor = _mm_set1_ps(1.0f / 8388608.0f);
7190  shift0 = (unusedBitsPerSample + pFlac->currentFrame.subframes[0].wastedBitsPerSample) - 8;
7191  shift1 = (unusedBitsPerSample + pFlac->currentFrame.subframes[1].wastedBitsPerSample) - 8;
7192 
7193  for (i = 0; i < frameCount4; ++i) {
7194  __m128i inputSample0 = _mm_loadu_si128((const __m128i*)pInputSamples0 + i);
7195  __m128i inputSample1 = _mm_loadu_si128((const __m128i*)pInputSamples1 + i);
7196 
7197  __m128i left = _mm_slli_epi32(inputSample0, shift0);
7198  __m128i side = _mm_slli_epi32(inputSample1, shift1);
7199  __m128i right = _mm_sub_epi32(left, side);
7200  __m128 leftf = _mm_mul_ps(_mm_cvtepi32_ps(left), factor);
7201  __m128 rightf = _mm_mul_ps(_mm_cvtepi32_ps(right), factor);
7202 
7203  pOutputSamples[i*8+0] = ((float*)&leftf)[0];
7204  pOutputSamples[i*8+1] = ((float*)&rightf)[0];
7205  pOutputSamples[i*8+2] = ((float*)&leftf)[1];
7206  pOutputSamples[i*8+3] = ((float*)&rightf)[1];
7207  pOutputSamples[i*8+4] = ((float*)&leftf)[2];
7208  pOutputSamples[i*8+5] = ((float*)&rightf)[2];
7209  pOutputSamples[i*8+6] = ((float*)&leftf)[3];
7210  pOutputSamples[i*8+7] = ((float*)&rightf)[3];
7211  }
7212 
7213  for (i = (frameCount4 << 2); i < frameCount; ++i) {
7214  int left = pInputSamples0[i] << shift0;
7215  int side = pInputSamples1[i] << shift1;
7216  int right = left - side;
7217 
7218  pOutputSamples[i*2+0] = (float)(left / 8388608.0f);
7219  pOutputSamples[i*2+1] = (float)(right / 8388608.0f);
7220  }
7221 }
7222 #endif
7223 
7224 static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_left_side(drflac* pFlac, drflac_uint64 frameCount, drflac_int32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples)
7225 {
7226 #if defined(DRFLAC_SUPPORT_SSE2)
7227  if (drflac__gIsSSE2Supported && pFlac->bitsPerSample <= 24) {
7228  drflac_read_pcm_frames_f32__decode_left_side__sse2(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7229  } else
7230 #endif
7231  {
7232  /* Scalar fallback. */
7233 #if 0
7234  drflac_read_pcm_frames_f32__decode_left_side__reference(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7235 #else
7236  drflac_read_pcm_frames_f32__decode_left_side__scalar(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7237 #endif
7238  }
7239 }
7240 
7241 
7242 #if 0
7243 static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_right_side__reference(drflac* pFlac, drflac_uint64 frameCount, drflac_int32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples)
7244 {
7245  drflac_uint64 i;
7246  for (i = 0; i < frameCount; ++i) {
7247  int side = pInputSamples0[i] << (unusedBitsPerSample + pFlac->currentFrame.subframes[0].wastedBitsPerSample);
7248  int right = pInputSamples1[i] << (unusedBitsPerSample + pFlac->currentFrame.subframes[1].wastedBitsPerSample);
7249  int left = right + side;
7250 
7251  pOutputSamples[i*2+0] = (float)(left / 2147483648.0);
7252  pOutputSamples[i*2+1] = (float)(right / 2147483648.0);
7253  }
7254 }
7255 #endif
7256 
7257 static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_right_side__scalar(drflac* pFlac, drflac_uint64 frameCount, drflac_int32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples)
7258 {
7259  drflac_uint64 i;
7260  drflac_uint64 frameCount4 = frameCount >> 2;
7261 
7262  float factor = 1 / 2147483648.0;
7263 
7264  drflac_int32 shift0 = unusedBitsPerSample + pFlac->currentFrame.subframes[0].wastedBitsPerSample;
7265  drflac_int32 shift1 = unusedBitsPerSample + pFlac->currentFrame.subframes[1].wastedBitsPerSample;
7266  for (i = 0; i < frameCount4; ++i) {
7267  drflac_int32 side0 = pInputSamples0[i*4+0] << shift0;
7268  drflac_int32 side1 = pInputSamples0[i*4+1] << shift0;
7269  drflac_int32 side2 = pInputSamples0[i*4+2] << shift0;
7270  drflac_int32 side3 = pInputSamples0[i*4+3] << shift0;
7271 
7272  drflac_int32 right0 = pInputSamples1[i*4+0] << shift1;
7273  drflac_int32 right1 = pInputSamples1[i*4+1] << shift1;
7274  drflac_int32 right2 = pInputSamples1[i*4+2] << shift1;
7275  drflac_int32 right3 = pInputSamples1[i*4+3] << shift1;
7276 
7277  drflac_int32 left0 = right0 + side0;
7278  drflac_int32 left1 = right1 + side1;
7279  drflac_int32 left2 = right2 + side2;
7280  drflac_int32 left3 = right3 + side3;
7281 
7282  pOutputSamples[i*8+0] = left0 * factor;
7283  pOutputSamples[i*8+1] = right0 * factor;
7284  pOutputSamples[i*8+2] = left1 * factor;
7285  pOutputSamples[i*8+3] = right1 * factor;
7286  pOutputSamples[i*8+4] = left2 * factor;
7287  pOutputSamples[i*8+5] = right2 * factor;
7288  pOutputSamples[i*8+6] = left3 * factor;
7289  pOutputSamples[i*8+7] = right3 * factor;
7290  }
7291 
7292  for (i = (frameCount4 << 2); i < frameCount; ++i) {
7293  int side = pInputSamples0[i] << shift0;
7294  int right = pInputSamples1[i] << shift1;
7295  int left = right + side;
7296 
7297  pOutputSamples[i*2+0] = (float)(left * factor);
7298  pOutputSamples[i*2+1] = (float)(right * factor);
7299  }
7300 }
7301 
7302 #if defined(DRFLAC_SUPPORT_SSE2)
7303 static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_right_side__sse2(drflac* pFlac, drflac_uint64 frameCount, drflac_int32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples)
7304 {
7305  drflac_uint64 frameCount4;
7306  __m128 factor;
7307  int shift0;
7308  int shift1;
7309  drflac_uint64 i;
7310 
7311  drflac_assert(pFlac->bitsPerSample <= 24);
7312 
7313  frameCount4 = frameCount >> 2;
7314 
7315  factor = _mm_set1_ps(1.0f / 8388608.0f);
7316  shift0 = (unusedBitsPerSample + pFlac->currentFrame.subframes[0].wastedBitsPerSample) - 8;
7317  shift1 = (unusedBitsPerSample + pFlac->currentFrame.subframes[1].wastedBitsPerSample) - 8;
7318 
7319  for (i = 0; i < frameCount4; ++i) {
7320  __m128i inputSample0 = _mm_loadu_si128((const __m128i*)pInputSamples0 + i);
7321  __m128i inputSample1 = _mm_loadu_si128((const __m128i*)pInputSamples1 + i);
7322 
7323  __m128i side = _mm_slli_epi32(inputSample0, shift0);
7324  __m128i right = _mm_slli_epi32(inputSample1, shift1);
7325  __m128i left = _mm_add_epi32(right, side);
7326  __m128 leftf = _mm_mul_ps(_mm_cvtepi32_ps(left), factor);
7327  __m128 rightf = _mm_mul_ps(_mm_cvtepi32_ps(right), factor);
7328 
7329  pOutputSamples[i*8+0] = ((float*)&leftf)[0];
7330  pOutputSamples[i*8+1] = ((float*)&rightf)[0];
7331  pOutputSamples[i*8+2] = ((float*)&leftf)[1];
7332  pOutputSamples[i*8+3] = ((float*)&rightf)[1];
7333  pOutputSamples[i*8+4] = ((float*)&leftf)[2];
7334  pOutputSamples[i*8+5] = ((float*)&rightf)[2];
7335  pOutputSamples[i*8+6] = ((float*)&leftf)[3];
7336  pOutputSamples[i*8+7] = ((float*)&rightf)[3];
7337  }
7338 
7339  for (i = (frameCount4 << 2); i < frameCount; ++i) {
7340  int side = pInputSamples0[i] << shift0;
7341  int right = pInputSamples1[i] << shift1;
7342  int left = right + side;
7343 
7344  pOutputSamples[i*2+0] = (float)(left / 8388608.0f);
7345  pOutputSamples[i*2+1] = (float)(right / 8388608.0f);
7346  }
7347 }
7348 #endif
7349 
7350 static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_right_side(drflac* pFlac, drflac_uint64 frameCount, drflac_int32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples)
7351 {
7352 #if defined(DRFLAC_SUPPORT_SSE2)
7353  if (drflac__gIsSSE2Supported && pFlac->bitsPerSample <= 24) {
7354  drflac_read_pcm_frames_f32__decode_right_side__sse2(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7355  } else
7356 #endif
7357  {
7358  /* Scalar fallback. */
7359 #if 0
7360  drflac_read_pcm_frames_f32__decode_right_side__reference(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7361 #else
7362  drflac_read_pcm_frames_f32__decode_right_side__scalar(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7363 #endif
7364  }
7365 }
7366 
7367 
7368 #if 0
7369 static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_mid_side__reference(drflac* pFlac, drflac_uint64 frameCount, drflac_int32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples)
7370 {
7371  for (drflac_uint64 i = 0; i < frameCount; ++i) {
7372  int mid = pInputSamples0[i] << pFlac->currentFrame.subframes[0].wastedBitsPerSample;
7373  int side = pInputSamples1[i] << pFlac->currentFrame.subframes[1].wastedBitsPerSample;
7374 
7375  mid = (((drflac_uint32)mid) << 1) | (side & 0x01);
7376 
7377  pOutputSamples[i*2+0] = (float)((((mid + side) >> 1) << (unusedBitsPerSample)) / 2147483648.0);
7378  pOutputSamples[i*2+1] = (float)((((mid - side) >> 1) << (unusedBitsPerSample)) / 2147483648.0);
7379  }
7380 }
7381 #endif
7382 
7383 static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_mid_side__scalar(drflac* pFlac, drflac_uint64 frameCount, drflac_int32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples)
7384 {
7385  drflac_uint64 i;
7386  drflac_uint64 frameCount4 = frameCount >> 2;
7387 
7388  float factor = 1 / 2147483648.0;
7389 
7390  int shift = unusedBitsPerSample;
7391  if (shift > 0) {
7392  shift -= 1;
7393  for (i = 0; i < frameCount4; ++i) {
7394  int temp0L;
7395  int temp1L;
7396  int temp2L;
7397  int temp3L;
7398  int temp0R;
7399  int temp1R;
7400  int temp2R;
7401  int temp3R;
7402 
7403  int mid0 = pInputSamples0[i*4+0] << pFlac->currentFrame.subframes[0].wastedBitsPerSample;
7404  int mid1 = pInputSamples0[i*4+1] << pFlac->currentFrame.subframes[0].wastedBitsPerSample;
7405  int mid2 = pInputSamples0[i*4+2] << pFlac->currentFrame.subframes[0].wastedBitsPerSample;
7406  int mid3 = pInputSamples0[i*4+3] << pFlac->currentFrame.subframes[0].wastedBitsPerSample;
7407 
7408  int side0 = pInputSamples1[i*4+0] << pFlac->currentFrame.subframes[1].wastedBitsPerSample;
7409  int side1 = pInputSamples1[i*4+1] << pFlac->currentFrame.subframes[1].wastedBitsPerSample;
7410  int side2 = pInputSamples1[i*4+2] << pFlac->currentFrame.subframes[1].wastedBitsPerSample;
7411  int side3 = pInputSamples1[i*4+3] << pFlac->currentFrame.subframes[1].wastedBitsPerSample;
7412 
7413  mid0 = (((drflac_uint32)mid0) << 1) | (side0 & 0x01);
7414  mid1 = (((drflac_uint32)mid1) << 1) | (side1 & 0x01);
7415  mid2 = (((drflac_uint32)mid2) << 1) | (side2 & 0x01);
7416  mid3 = (((drflac_uint32)mid3) << 1) | (side3 & 0x01);
7417 
7418  temp0L = ((mid0 + side0) << shift);
7419  temp1L = ((mid1 + side1) << shift);
7420  temp2L = ((mid2 + side2) << shift);
7421  temp3L = ((mid3 + side3) << shift);
7422 
7423  temp0R = ((mid0 - side0) << shift);
7424  temp1R = ((mid1 - side1) << shift);
7425  temp2R = ((mid2 - side2) << shift);
7426  temp3R = ((mid3 - side3) << shift);
7427 
7428  pOutputSamples[i*8+0] = (float)(temp0L * factor);
7429  pOutputSamples[i*8+1] = (float)(temp0R * factor);
7430  pOutputSamples[i*8+2] = (float)(temp1L * factor);
7431  pOutputSamples[i*8+3] = (float)(temp1R * factor);
7432  pOutputSamples[i*8+4] = (float)(temp2L * factor);
7433  pOutputSamples[i*8+5] = (float)(temp2R * factor);
7434  pOutputSamples[i*8+6] = (float)(temp3L * factor);
7435  pOutputSamples[i*8+7] = (float)(temp3R * factor);
7436  }
7437  } else {
7438  for (i = 0; i < frameCount4; ++i) {
7439  int temp0L;
7440  int temp1L;
7441  int temp2L;
7442  int temp3L;
7443  int temp0R;
7444  int temp1R;
7445  int temp2R;
7446  int temp3R;
7447 
7448  int mid0 = pInputSamples0[i*4+0] << pFlac->currentFrame.subframes[0].wastedBitsPerSample;
7449  int mid1 = pInputSamples0[i*4+1] << pFlac->currentFrame.subframes[0].wastedBitsPerSample;
7450  int mid2 = pInputSamples0[i*4+2] << pFlac->currentFrame.subframes[0].wastedBitsPerSample;
7451  int mid3 = pInputSamples0[i*4+3] << pFlac->currentFrame.subframes[0].wastedBitsPerSample;
7452 
7453  int side0 = pInputSamples1[i*4+0] << pFlac->currentFrame.subframes[1].wastedBitsPerSample;
7454  int side1 = pInputSamples1[i*4+1] << pFlac->currentFrame.subframes[1].wastedBitsPerSample;
7455  int side2 = pInputSamples1[i*4+2] << pFlac->currentFrame.subframes[1].wastedBitsPerSample;
7456  int side3 = pInputSamples1[i*4+3] << pFlac->currentFrame.subframes[1].wastedBitsPerSample;
7457 
7458  mid0 = (((drflac_uint32)mid0) << 1) | (side0 & 0x01);
7459  mid1 = (((drflac_uint32)mid1) << 1) | (side1 & 0x01);
7460  mid2 = (((drflac_uint32)mid2) << 1) | (side2 & 0x01);
7461  mid3 = (((drflac_uint32)mid3) << 1) | (side3 & 0x01);
7462 
7463  temp0L = ((mid0 + side0) >> 1);
7464  temp1L = ((mid1 + side1) >> 1);
7465  temp2L = ((mid2 + side2) >> 1);
7466  temp3L = ((mid3 + side3) >> 1);
7467 
7468  temp0R = ((mid0 - side0) >> 1);
7469  temp1R = ((mid1 - side1) >> 1);
7470  temp2R = ((mid2 - side2) >> 1);
7471  temp3R = ((mid3 - side3) >> 1);
7472 
7473  pOutputSamples[i*8+0] = (float)(temp0L * factor);
7474  pOutputSamples[i*8+1] = (float)(temp0R * factor);
7475  pOutputSamples[i*8+2] = (float)(temp1L * factor);
7476  pOutputSamples[i*8+3] = (float)(temp1R * factor);
7477  pOutputSamples[i*8+4] = (float)(temp2L * factor);
7478  pOutputSamples[i*8+5] = (float)(temp2R * factor);
7479  pOutputSamples[i*8+6] = (float)(temp3L * factor);
7480  pOutputSamples[i*8+7] = (float)(temp3R * factor);
7481  }
7482  }
7483 
7484  for (i = (frameCount4 << 2); i < frameCount; ++i) {
7485  int mid = pInputSamples0[i] << pFlac->currentFrame.subframes[0].wastedBitsPerSample;
7486  int side = pInputSamples1[i] << pFlac->currentFrame.subframes[1].wastedBitsPerSample;
7487 
7488  mid = (((drflac_uint32)mid) << 1) | (side & 0x01);
7489 
7490  pOutputSamples[i*2+0] = (float)((((mid + side) >> 1) << unusedBitsPerSample) * factor);
7491  pOutputSamples[i*2+1] = (float)((((mid - side) >> 1) << unusedBitsPerSample) * factor);
7492  }
7493 }
7494 
7495 #if defined(DRFLAC_SUPPORT_SSE2)
7496 static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_mid_side__sse2(drflac* pFlac, drflac_uint64 frameCount, drflac_int32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples)
7497 {
7498  drflac_uint64 i;
7499  drflac_uint64 frameCount4;
7500  float factor;
7501  int shift;
7502  __m128 factor128;
7503 
7504  drflac_assert(pFlac->bitsPerSample <= 24);
7505 
7506  frameCount4 = frameCount >> 2;
7507 
7508  factor = 1.0f / 8388608.0f;
7509  factor128 = _mm_set1_ps(1.0f / 8388608.0f);
7510 
7511  shift = unusedBitsPerSample - 8;
7512  if (shift == 0) {
7513  for (i = 0; i < frameCount4; ++i) {
7514  __m128i tempL;
7515  __m128i tempR;
7516  __m128 leftf;
7517  __m128 rightf;
7518 
7519  __m128i inputSample0 = _mm_loadu_si128((const __m128i*)pInputSamples0 + i);
7520  __m128i inputSample1 = _mm_loadu_si128((const __m128i*)pInputSamples1 + i);
7521 
7522  __m128i mid = _mm_slli_epi32(inputSample0, pFlac->currentFrame.subframes[0].wastedBitsPerSample);
7523  __m128i side = _mm_slli_epi32(inputSample1, pFlac->currentFrame.subframes[1].wastedBitsPerSample);
7524 
7525  mid = _mm_or_si128(_mm_slli_epi32(mid, 1), _mm_and_si128(side, _mm_set1_epi32(0x01)));
7526 
7527  tempL = _mm_add_epi32(mid, side);
7528  tempR = _mm_sub_epi32(mid, side);
7529 
7530  /* Signed bit shift. */
7531  tempL = _mm_or_si128(_mm_srli_epi32(tempL, 1), _mm_and_si128(tempL, _mm_set1_epi32(0x80000000)));
7532  tempR = _mm_or_si128(_mm_srli_epi32(tempR, 1), _mm_and_si128(tempR, _mm_set1_epi32(0x80000000)));
7533 
7534  leftf = _mm_mul_ps(_mm_cvtepi32_ps(tempL), factor128);
7535  rightf = _mm_mul_ps(_mm_cvtepi32_ps(tempR), factor128);
7536 
7537  pOutputSamples[i*8+0] = ((float*)&leftf)[0];
7538  pOutputSamples[i*8+1] = ((float*)&rightf)[0];
7539  pOutputSamples[i*8+2] = ((float*)&leftf)[1];
7540  pOutputSamples[i*8+3] = ((float*)&rightf)[1];
7541  pOutputSamples[i*8+4] = ((float*)&leftf)[2];
7542  pOutputSamples[i*8+5] = ((float*)&rightf)[2];
7543  pOutputSamples[i*8+6] = ((float*)&leftf)[3];
7544  pOutputSamples[i*8+7] = ((float*)&rightf)[3];
7545  }
7546 
7547  for (i = (frameCount4 << 2); i < frameCount; ++i) {
7548  int mid = pInputSamples0[i] << pFlac->currentFrame.subframes[0].wastedBitsPerSample;
7549  int side = pInputSamples1[i] << pFlac->currentFrame.subframes[1].wastedBitsPerSample;
7550 
7551  mid = (((drflac_uint32)mid) << 1) | (side & 0x01);
7552 
7553  pOutputSamples[i*2+0] = (float)(((mid + side) >> 1) * factor);
7554  pOutputSamples[i*2+1] = (float)(((mid - side) >> 1) * factor);
7555  }
7556  } else {
7557  for (i = 0; i < frameCount4; ++i) {
7558  __m128i inputSample0;
7559  __m128i inputSample1;
7560  __m128i mid;
7561  __m128i side;
7562  __m128i tempL;
7563  __m128i tempR;
7564  __m128 leftf;
7565  __m128 rightf;
7566 
7567  inputSample0 = _mm_loadu_si128((const __m128i*)pInputSamples0 + i);
7568  inputSample1 = _mm_loadu_si128((const __m128i*)pInputSamples1 + i);
7569 
7570  mid = _mm_slli_epi32(inputSample0, pFlac->currentFrame.subframes[0].wastedBitsPerSample);
7571  side = _mm_slli_epi32(inputSample1, pFlac->currentFrame.subframes[1].wastedBitsPerSample);
7572 
7573  mid = _mm_or_si128(_mm_slli_epi32(mid, 1), _mm_and_si128(side, _mm_set1_epi32(0x01)));
7574 
7575  tempL = _mm_slli_epi32(_mm_srli_epi32(_mm_add_epi32(mid, side), 1), shift);
7576  tempR = _mm_slli_epi32(_mm_srli_epi32(_mm_sub_epi32(mid, side), 1), shift);
7577 
7578  leftf = _mm_mul_ps(_mm_cvtepi32_ps(tempL), factor128);
7579  rightf = _mm_mul_ps(_mm_cvtepi32_ps(tempR), factor128);
7580 
7581  pOutputSamples[i*8+0] = ((float*)&leftf)[0];
7582  pOutputSamples[i*8+1] = ((float*)&rightf)[0];
7583  pOutputSamples[i*8+2] = ((float*)&leftf)[1];
7584  pOutputSamples[i*8+3] = ((float*)&rightf)[1];
7585  pOutputSamples[i*8+4] = ((float*)&leftf)[2];
7586  pOutputSamples[i*8+5] = ((float*)&rightf)[2];
7587  pOutputSamples[i*8+6] = ((float*)&leftf)[3];
7588  pOutputSamples[i*8+7] = ((float*)&rightf)[3];
7589  }
7590 
7591  for (i = (frameCount4 << 2); i < frameCount; ++i) {
7592  int mid = pInputSamples0[i] << pFlac->currentFrame.subframes[0].wastedBitsPerSample;
7593  int side = pInputSamples1[i] << pFlac->currentFrame.subframes[1].wastedBitsPerSample;
7594 
7595  mid = (((drflac_uint32)mid) << 1) | (side & 0x01);
7596 
7597  pOutputSamples[i*2+0] = (float)((((mid + side) >> 1) << shift) * factor);
7598  pOutputSamples[i*2+1] = (float)((((mid - side) >> 1) << shift) * factor);
7599  }
7600  }
7601 }
7602 #endif
7603 
7604 
7605 static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_mid_side(drflac* pFlac, drflac_uint64 frameCount, drflac_int32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples)
7606 {
7607 #if defined(DRFLAC_SUPPORT_SSE2)
7608  if (drflac__gIsSSE2Supported && pFlac->bitsPerSample <= 24) {
7609  drflac_read_pcm_frames_f32__decode_mid_side__sse2(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7610  } else
7611 #endif
7612  {
7613  /* Scalar fallback. */
7614 #if 0
7615  drflac_read_pcm_frames_f32__decode_mid_side__reference(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7616 #else
7617  drflac_read_pcm_frames_f32__decode_mid_side__scalar(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7618 #endif
7619  }
7620 }
7621 
7622 #if 0
7623 static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_independent_stereo__reference(drflac* pFlac, drflac_uint64 frameCount, drflac_int32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples)
7624 {
7625  for (drflac_uint64 i = 0; i < frameCount; ++i) {
7626  pOutputSamples[i*2+0] = (float)((pInputSamples0[i] << (unusedBitsPerSample + pFlac->currentFrame.subframes[0].wastedBitsPerSample)) / 2147483648.0);
7627  pOutputSamples[i*2+1] = (float)((pInputSamples1[i] << (unusedBitsPerSample + pFlac->currentFrame.subframes[1].wastedBitsPerSample)) / 2147483648.0);
7628  }
7629 }
7630 #endif
7631 
7632 static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_independent_stereo__scalar(drflac* pFlac, drflac_uint64 frameCount, drflac_int32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples)
7633 {
7634  drflac_uint64 i;
7635  drflac_uint64 frameCount4 = frameCount >> 2;
7636 
7637  float factor = 1 / 2147483648.0;
7638 
7639  int shift0 = (unusedBitsPerSample + pFlac->currentFrame.subframes[0].wastedBitsPerSample);
7640  int shift1 = (unusedBitsPerSample + pFlac->currentFrame.subframes[1].wastedBitsPerSample);
7641 
7642  for (i = 0; i < frameCount4; ++i) {
7643  int tempL0 = pInputSamples0[i*4+0] << shift0;
7644  int tempL1 = pInputSamples0[i*4+1] << shift0;
7645  int tempL2 = pInputSamples0[i*4+2] << shift0;
7646  int tempL3 = pInputSamples0[i*4+3] << shift0;
7647 
7648  int tempR0 = pInputSamples1[i*4+0] << shift1;
7649  int tempR1 = pInputSamples1[i*4+1] << shift1;
7650  int tempR2 = pInputSamples1[i*4+2] << shift1;
7651  int tempR3 = pInputSamples1[i*4+3] << shift1;
7652 
7653  pOutputSamples[i*8+0] = (float)(tempL0 * factor);
7654  pOutputSamples[i*8+1] = (float)(tempR0 * factor);
7655  pOutputSamples[i*8+2] = (float)(tempL1 * factor);
7656  pOutputSamples[i*8+3] = (float)(tempR1 * factor);
7657  pOutputSamples[i*8+4] = (float)(tempL2 * factor);
7658  pOutputSamples[i*8+5] = (float)(tempR2 * factor);
7659  pOutputSamples[i*8+6] = (float)(tempL3 * factor);
7660  pOutputSamples[i*8+7] = (float)(tempR3 * factor);
7661  }
7662 
7663  for (i = (frameCount4 << 2); i < frameCount; ++i) {
7664  pOutputSamples[i*2+0] = (float)((pInputSamples0[i] << shift0) * factor);
7665  pOutputSamples[i*2+1] = (float)((pInputSamples1[i] << shift1) * factor);
7666  }
7667 }
7668 
7669 #if defined(DRFLAC_SUPPORT_SSE2)
7670 static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_independent_stereo__sse2(drflac* pFlac, drflac_uint64 frameCount, drflac_int32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples)
7671 {
7672  drflac_uint64 i;
7673  drflac_uint64 frameCount4 = frameCount >> 2;
7674 
7675  float factor = 1.0f / 8388608.0f;
7676  __m128 factor128 = _mm_set1_ps(1.0f / 8388608.0f);
7677 
7678  int shift0 = (unusedBitsPerSample + pFlac->currentFrame.subframes[0].wastedBitsPerSample) - 8;
7679  int shift1 = (unusedBitsPerSample + pFlac->currentFrame.subframes[1].wastedBitsPerSample) - 8;
7680 
7681  for (i = 0; i < frameCount4; ++i) {
7682  __m128i inputSample0 = _mm_loadu_si128((const __m128i*)pInputSamples0 + i);
7683  __m128i inputSample1 = _mm_loadu_si128((const __m128i*)pInputSamples1 + i);
7684 
7685  __m128i i32L = _mm_slli_epi32(inputSample0, shift0);
7686  __m128i i32R = _mm_slli_epi32(inputSample1, shift1);
7687 
7688  __m128 f32L = _mm_mul_ps(_mm_cvtepi32_ps(i32L), factor128);
7689  __m128 f32R = _mm_mul_ps(_mm_cvtepi32_ps(i32R), factor128);
7690 
7691  pOutputSamples[i*8+0] = ((float*)&f32L)[0];
7692  pOutputSamples[i*8+1] = ((float*)&f32R)[0];
7693  pOutputSamples[i*8+2] = ((float*)&f32L)[1];
7694  pOutputSamples[i*8+3] = ((float*)&f32R)[1];
7695  pOutputSamples[i*8+4] = ((float*)&f32L)[2];
7696  pOutputSamples[i*8+5] = ((float*)&f32R)[2];
7697  pOutputSamples[i*8+6] = ((float*)&f32L)[3];
7698  pOutputSamples[i*8+7] = ((float*)&f32R)[3];
7699  }
7700 
7701  for (i = (frameCount4 << 2); i < frameCount; ++i) {
7702  pOutputSamples[i*2+0] = (float)((pInputSamples0[i] << shift0) * factor);
7703  pOutputSamples[i*2+1] = (float)((pInputSamples1[i] << shift1) * factor);
7704  }
7705 }
7706 #endif
7707 
7708 static DRFLAC_INLINE void drflac_read_pcm_frames_f32__decode_independent_stereo(drflac* pFlac, drflac_uint64 frameCount, drflac_int32 unusedBitsPerSample, const drflac_int32* pInputSamples0, const drflac_int32* pInputSamples1, float* pOutputSamples)
7709 {
7710 #if defined(DRFLAC_SUPPORT_SSE2)
7711  if (drflac__gIsSSE2Supported && pFlac->bitsPerSample <= 24) {
7712  drflac_read_pcm_frames_f32__decode_independent_stereo__sse2(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7713  } else
7714 #endif
7715  {
7716  /* Scalar fallback. */
7717 #if 0
7718  drflac_read_pcm_frames_f32__decode_independent_stereo__reference(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7719 #else
7720  drflac_read_pcm_frames_f32__decode_independent_stereo__scalar(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7721 #endif
7722  }
7723 }
7724 
7725 drflac_uint64 drflac_read_pcm_frames_f32(drflac* pFlac, drflac_uint64 framesToRead, float* pBufferOut)
7726 {
7727  drflac_uint64 framesRead;
7728 
7729  if (pFlac == NULL || framesToRead == 0) {
7730  return 0;
7731  }
7732 
7733  if (pBufferOut == NULL) {
7734  return drflac__seek_forward_by_pcm_frames(pFlac, framesToRead);
7735  }
7736 
7737  framesRead = 0;
7738  while (framesToRead > 0) {
7739  /* If we've run out of samples in this frame, go to the next. */
7740  if (pFlac->currentFrame.samplesRemaining == 0) {
7741  if (!drflac__read_and_decode_next_flac_frame(pFlac)) {
7742  break; /* Couldn't read the next frame, so just break from the loop and return. */
7743  }
7744  } else {
7745  unsigned int channelCount = drflac__get_channel_count_from_channel_assignment(pFlac->currentFrame.header.channelAssignment);
7746  drflac_uint64 totalFramesInPacket = pFlac->currentFrame.header.blockSize;
7747  drflac_uint64 framesReadFromPacketSoFar = totalFramesInPacket - (pFlac->currentFrame.samplesRemaining/channelCount);
7748  drflac_uint64 iFirstPCMFrame = framesReadFromPacketSoFar;
7749  drflac_int32 unusedBitsPerSample = 32 - pFlac->bitsPerSample;
7750  drflac_uint64 frameCountThisIteration = framesToRead;
7751  drflac_uint64 samplesReadThisIteration;
7752 
7753  if (frameCountThisIteration > pFlac->currentFrame.samplesRemaining / channelCount) {
7754  frameCountThisIteration = pFlac->currentFrame.samplesRemaining / channelCount;
7755  }
7756 
7757  if (channelCount == 2) {
7758  const drflac_int32* pDecodedSamples0 = pFlac->currentFrame.subframes[0].pDecodedSamples + iFirstPCMFrame;
7759  const drflac_int32* pDecodedSamples1 = pFlac->currentFrame.subframes[1].pDecodedSamples + iFirstPCMFrame;
7760 
7761  switch (pFlac->currentFrame.header.channelAssignment)
7762  {
7763  case DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE:
7764  {
7765  drflac_read_pcm_frames_f32__decode_left_side(pFlac, frameCountThisIteration, unusedBitsPerSample, pDecodedSamples0, pDecodedSamples1, pBufferOut);
7766  } break;
7767 
7768  case DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE:
7769  {
7770  drflac_read_pcm_frames_f32__decode_right_side(pFlac, frameCountThisIteration, unusedBitsPerSample, pDecodedSamples0, pDecodedSamples1, pBufferOut);
7771  } break;
7772 
7773  case DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE:
7774  {
7775  drflac_read_pcm_frames_f32__decode_mid_side(pFlac, frameCountThisIteration, unusedBitsPerSample, pDecodedSamples0, pDecodedSamples1, pBufferOut);
7776  } break;
7777 
7778  case DRFLAC_CHANNEL_ASSIGNMENT_INDEPENDENT:
7779  default:
7780  {
7781  drflac_read_pcm_frames_f32__decode_independent_stereo(pFlac, frameCountThisIteration, unusedBitsPerSample, pDecodedSamples0, pDecodedSamples1, pBufferOut);
7782  } break;
7783  }
7784  } else {
7785  /* Generic interleaving. */
7786  drflac_uint64 i;
7787  for (i = 0; i < frameCountThisIteration; ++i) {
7788  unsigned int j;
7789  for (j = 0; j < channelCount; ++j) {
7790  pBufferOut[(i*channelCount)+j] = (float)(((pFlac->currentFrame.subframes[j].pDecodedSamples[iFirstPCMFrame + i]) << (unusedBitsPerSample + pFlac->currentFrame.subframes[j].wastedBitsPerSample)) / 2147483648.0);
7791  }
7792  }
7793  }
7794 
7795  samplesReadThisIteration = frameCountThisIteration * channelCount;
7796  framesRead += frameCountThisIteration;
7797  framesReadFromPacketSoFar += frameCountThisIteration;
7798  pBufferOut += samplesReadThisIteration;
7799  framesToRead -= frameCountThisIteration;
7800  pFlac->currentSample += samplesReadThisIteration;
7801  pFlac->currentFrame.samplesRemaining -= (unsigned int)samplesReadThisIteration;
7802  }
7803  }
7804 
7805  return framesRead;
7806 }
7807 
7809 {
7810  if (pFlac == NULL) {
7811  return DRFLAC_FALSE;
7812  }
7813 
7814  /*
7815  If we don't know where the first frame begins then we can't seek. This will happen when the STREAMINFO block was not present
7816  when the decoder was opened.
7817  */
7818  if (pFlac->firstFramePos == 0) {
7819  return DRFLAC_FALSE;
7820  }
7821 
7822  if (sampleIndex == 0) {
7823  pFlac->currentSample = 0;
7824  return drflac__seek_to_first_frame(pFlac);
7825  } else {
7826  drflac_bool32 wasSuccessful = DRFLAC_FALSE;
7827 
7828  /* Clamp the sample to the end. */
7829  if (sampleIndex >= pFlac->totalSampleCount) {
7830  sampleIndex = pFlac->totalSampleCount - 1;
7831  }
7832 
7833  /* If the target sample and the current sample are in the same frame we just move the position forward. */
7834  if (sampleIndex > pFlac->currentSample) {
7835  /* Forward. */
7836  drflac_uint32 offset = (drflac_uint32)(sampleIndex - pFlac->currentSample);
7837  if (pFlac->currentFrame.samplesRemaining > offset) {
7838  pFlac->currentFrame.samplesRemaining -= offset;
7839  pFlac->currentSample = sampleIndex;
7840  return DRFLAC_TRUE;
7841  }
7842  } else {
7843  /* Backward. */
7844  drflac_uint32 offsetAbs = (drflac_uint32)(pFlac->currentSample - sampleIndex);
7845  drflac_uint32 currentFrameSampleCount = pFlac->currentFrame.header.blockSize * drflac__get_channel_count_from_channel_assignment(pFlac->currentFrame.header.channelAssignment);
7846  drflac_uint32 currentFrameSamplesConsumed = (drflac_uint32)(currentFrameSampleCount - pFlac->currentFrame.samplesRemaining);
7847  if (currentFrameSamplesConsumed > offsetAbs) {
7848  pFlac->currentFrame.samplesRemaining += offsetAbs;
7849  pFlac->currentSample = sampleIndex;
7850  return DRFLAC_TRUE;
7851  }
7852  }
7853 
7854  /*
7855  Different techniques depending on encapsulation. Using the native FLAC seektable with Ogg encapsulation is a bit awkward so
7856  we'll instead use Ogg's natural seeking facility.
7857  */
7858 #ifndef DR_FLAC_NO_OGG
7859  if (pFlac->container == drflac_container_ogg)
7860  {
7861  wasSuccessful = drflac_ogg__seek_to_sample(pFlac, sampleIndex);
7862  }
7863  else
7864 #endif
7865  {
7866  /* First try seeking via the seek table. If this fails, fall back to a brute force seek which is much slower. */
7867  wasSuccessful = drflac__seek_to_sample__seek_table(pFlac, sampleIndex);
7868  if (!wasSuccessful) {
7869  wasSuccessful = drflac__seek_to_sample__brute_force(pFlac, sampleIndex);
7870  }
7871  }
7872 
7873  pFlac->currentSample = sampleIndex;
7874  return wasSuccessful;
7875  }
7876 }
7877 
7879 {
7880  if (pFlac == NULL) {
7881  return DRFLAC_FALSE;
7882  }
7883 
7884  /*
7885  If we don't know where the first frame begins then we can't seek. This will happen when the STREAMINFO block was not present
7886  when the decoder was opened.
7887  */
7888  if (pFlac->firstFramePos == 0) {
7889  return DRFLAC_FALSE;
7890  }
7891 
7892  if (pcmFrameIndex == 0) {
7893  pFlac->currentSample = 0;
7894  return drflac__seek_to_first_frame(pFlac);
7895  } else {
7896  drflac_bool32 wasSuccessful = DRFLAC_FALSE;
7897 
7898  /* Clamp the sample to the end. */
7899  if (pcmFrameIndex >= pFlac->totalPCMFrameCount) {
7900  pcmFrameIndex = pFlac->totalPCMFrameCount - 1;
7901  }
7902 
7903  /* If the target sample and the current sample are in the same frame we just move the position forward. */
7904  if (pcmFrameIndex*pFlac->channels > pFlac->currentSample) {
7905  /* Forward. */
7906  drflac_uint32 offset = (drflac_uint32)(pcmFrameIndex*pFlac->channels - pFlac->currentSample);
7907  if (pFlac->currentFrame.samplesRemaining > offset) {
7908  pFlac->currentFrame.samplesRemaining -= offset;
7909  pFlac->currentSample = pcmFrameIndex*pFlac->channels;
7910  return DRFLAC_TRUE;
7911  }
7912  } else {
7913  /* Backward. */
7914  drflac_uint32 offsetAbs = (drflac_uint32)(pFlac->currentSample - pcmFrameIndex*pFlac->channels);
7915  drflac_uint32 currentFrameSampleCount = pFlac->currentFrame.header.blockSize * drflac__get_channel_count_from_channel_assignment(pFlac->currentFrame.header.channelAssignment);
7916  drflac_uint32 currentFrameSamplesConsumed = (drflac_uint32)(currentFrameSampleCount - pFlac->currentFrame.samplesRemaining);
7917  if (currentFrameSamplesConsumed > offsetAbs) {
7918  pFlac->currentFrame.samplesRemaining += offsetAbs;
7919  pFlac->currentSample = pcmFrameIndex*pFlac->channels;
7920  return DRFLAC_TRUE;
7921  }
7922  }
7923 
7924  /*
7925  Different techniques depending on encapsulation. Using the native FLAC seektable with Ogg encapsulation is a bit awkward so
7926  we'll instead use Ogg's natural seeking facility.
7927  */
7928 #ifndef DR_FLAC_NO_OGG
7929  if (pFlac->container == drflac_container_ogg)
7930  {
7931  wasSuccessful = drflac_ogg__seek_to_sample(pFlac, pcmFrameIndex*pFlac->channels);
7932  }
7933  else
7934 #endif
7935  {
7936  /* First try seeking via the seek table. If this fails, fall back to a brute force seek which is much slower. */
7937  wasSuccessful = drflac__seek_to_sample__seek_table(pFlac, pcmFrameIndex*pFlac->channels);
7938  if (!wasSuccessful) {
7939  wasSuccessful = drflac__seek_to_sample__brute_force(pFlac, pcmFrameIndex*pFlac->channels);
7940  }
7941  }
7942 
7943  pFlac->currentSample = pcmFrameIndex*pFlac->channels;
7944  return wasSuccessful;
7945  }
7946 }
7947 
7948 
7949 
7950 /* High Level APIs */
7951 
7952 #if defined(SIZE_MAX)
7953  #define DRFLAC_SIZE_MAX SIZE_MAX
7954 #else
7955  #if defined(DRFLAC_64BIT)
7956  #define DRFLAC_SIZE_MAX ((drflac_uint64)0xFFFFFFFFFFFFFFFF)
7957  #else
7958  #define DRFLAC_SIZE_MAX 0xFFFFFFFF
7959  #endif
7960 #endif
7961 
7962 
7963 /* Using a macro as the definition of the drflac__full_decode_and_close_*() API family. Sue me. */
7964 #define DRFLAC_DEFINE_FULL_READ_AND_CLOSE(extension, type) \
7965 static type* drflac__full_read_and_close_ ## extension (drflac* pFlac, unsigned int* channelsOut, unsigned int* sampleRateOut, drflac_uint64* totalPCMFrameCountOut)\
7966 { \
7967  type* pSampleData = NULL; \
7968  drflac_uint64 totalPCMFrameCount; \
7969  \
7970  drflac_assert(pFlac != NULL); \
7971  \
7972  totalPCMFrameCount = pFlac->totalPCMFrameCount; \
7973  \
7974  if (totalPCMFrameCount == 0) { \
7975  type buffer[4096]; \
7976  drflac_uint64 pcmFramesRead; \
7977  size_t sampleDataBufferSize = sizeof(buffer); \
7978  \
7979  pSampleData = (type*)DRFLAC_MALLOC(sampleDataBufferSize); \
7980  if (pSampleData == NULL) { \
7981  goto on_error; \
7982  } \
7983  \
7984  while ((pcmFramesRead = (drflac_uint64)drflac_read_pcm_frames_##extension(pFlac, sizeof(buffer)/sizeof(buffer[0])/pFlac->channels, buffer)) > 0) { \
7985  if (((totalPCMFrameCount + pcmFramesRead) * pFlac->channels * sizeof(type)) > sampleDataBufferSize) { \
7986  type* pNewSampleData; \
7987  \
7988  sampleDataBufferSize *= 2; \
7989  pNewSampleData = (type*)DRFLAC_REALLOC(pSampleData, sampleDataBufferSize); \
7990  if (pNewSampleData == NULL) { \
7991  DRFLAC_FREE(pSampleData); \
7992  goto on_error; \
7993  } \
7994  \
7995  pSampleData = pNewSampleData; \
7996  } \
7997  \
7998  drflac_copy_memory(pSampleData + (totalPCMFrameCount*pFlac->channels), buffer, (size_t)(pcmFramesRead*pFlac->channels*sizeof(type))); \
7999  totalPCMFrameCount += pcmFramesRead; \
8000  } \
8001  \
8002  /* At this point everything should be decoded, but we just want to fill the unused part buffer with silence - need to \
8003  protect those ears from random noise! */ \
8004  drflac_zero_memory(pSampleData + (totalPCMFrameCount*pFlac->channels), (size_t)(sampleDataBufferSize - totalPCMFrameCount*pFlac->channels*sizeof(type))); \
8005  } else { \
8006  drflac_uint64 dataSize = totalPCMFrameCount*pFlac->channels*sizeof(type); \
8007  if (dataSize > DRFLAC_SIZE_MAX) { \
8008  goto on_error; /* The decoded data is too big. */ \
8009  } \
8010  \
8011  pSampleData = (type*)DRFLAC_MALLOC((size_t)dataSize); /* <-- Safe cast as per the check above. */ \
8012  if (pSampleData == NULL) { \
8013  goto on_error; \
8014  } \
8015  \
8016  totalPCMFrameCount = drflac_read_pcm_frames_##extension(pFlac, pFlac->totalPCMFrameCount, pSampleData); \
8017  } \
8018  \
8019  if (sampleRateOut) *sampleRateOut = pFlac->sampleRate; \
8020  if (channelsOut) *channelsOut = pFlac->channels; \
8021  if (totalPCMFrameCountOut) *totalPCMFrameCountOut = totalPCMFrameCount; \
8022  \
8023  drflac_close(pFlac); \
8024  return pSampleData; \
8025  \
8026 on_error: \
8027  drflac_close(pFlac); \
8028  return NULL; \
8029 }
8030 
8031 DRFLAC_DEFINE_FULL_READ_AND_CLOSE(s32, drflac_int32)
8032 DRFLAC_DEFINE_FULL_READ_AND_CLOSE(s16, drflac_int16)
8033 DRFLAC_DEFINE_FULL_READ_AND_CLOSE(f32, float)
8034 
8035 drflac_int32* drflac_open_and_read_pcm_frames_s32(drflac_read_proc onRead, drflac_seek_proc onSeek, void* pUserData, unsigned int* channelsOut, unsigned int* sampleRateOut, drflac_uint64* totalPCMFrameCountOut)
8036 {
8037  drflac* pFlac;
8038 
8039  if (channelsOut) {
8040  *channelsOut = 0;
8041  }
8042  if (sampleRateOut) {
8043  *sampleRateOut = 0;
8044  }
8045  if (totalPCMFrameCountOut) {
8046  *totalPCMFrameCountOut = 0;
8047  }
8048 
8049  pFlac = drflac_open(onRead, onSeek, pUserData);
8050  if (pFlac == NULL) {
8051  return NULL;
8052  }
8053 
8054  return drflac__full_read_and_close_s32(pFlac, channelsOut, sampleRateOut, totalPCMFrameCountOut);
8055 }
8056 
8057 drflac_int32* drflac_open_and_decode_s32(drflac_read_proc onRead, drflac_seek_proc onSeek, void* pUserData, unsigned int* channelsOut, unsigned int* sampleRateOut, drflac_uint64* totalSampleCountOut)
8058 {
8059  unsigned int channels;
8060  unsigned int sampleRate;
8061  drflac_uint64 totalPCMFrameCount;
8062  drflac_int32* pResult;
8063 
8064  if (channelsOut) {
8065  *channelsOut = 0;
8066  }
8067  if (sampleRateOut) {
8068  *sampleRateOut = 0;
8069  }
8070  if (totalSampleCountOut) {
8071  *totalSampleCountOut = 0;
8072  }
8073 
8074  pResult = drflac_open_and_read_pcm_frames_s32(onRead, onSeek, pUserData, &channels, &sampleRate, &totalPCMFrameCount);
8075  if (pResult == NULL) {
8076  return NULL;
8077  }
8078 
8079  if (channelsOut) {
8080  *channelsOut = channels;
8081  }
8082  if (sampleRateOut) {
8083  *sampleRateOut = sampleRate;
8084  }
8085  if (totalSampleCountOut) {
8086  *totalSampleCountOut = totalPCMFrameCount * channels;
8087  }
8088 
8089  return pResult;
8090 }
8091 
8092 
8093 
8094 drflac_int16* drflac_open_and_read_pcm_frames_s16(drflac_read_proc onRead, drflac_seek_proc onSeek, void* pUserData, unsigned int* channelsOut, unsigned int* sampleRateOut, drflac_uint64* totalPCMFrameCountOut)
8095 {
8096  drflac* pFlac;
8097 
8098  if (channelsOut) {
8099  *channelsOut = 0;
8100  }
8101  if (sampleRateOut) {
8102  *sampleRateOut = 0;
8103  }
8104  if (totalPCMFrameCountOut) {
8105  *totalPCMFrameCountOut = 0;
8106  }
8107 
8108  pFlac = drflac_open(onRead, onSeek, pUserData);
8109  if (pFlac == NULL) {
8110  return NULL;
8111  }
8112 
8113  return drflac__full_read_and_close_s16(pFlac, channelsOut, sampleRateOut, totalPCMFrameCountOut);
8114 }
8115 
8116 drflac_int16* drflac_open_and_decode_s16(drflac_read_proc onRead, drflac_seek_proc onSeek, void* pUserData, unsigned int* channelsOut, unsigned int* sampleRateOut, drflac_uint64* totalSampleCountOut)
8117 {
8118  unsigned int channels;
8119  unsigned int sampleRate;
8120  drflac_uint64 totalPCMFrameCount;
8121  drflac_int16* pResult;
8122 
8123  if (channelsOut) {
8124  *channelsOut = 0;
8125  }
8126  if (sampleRateOut) {
8127  *sampleRateOut = 0;
8128  }
8129  if (totalSampleCountOut) {
8130  *totalSampleCountOut = 0;
8131  }
8132 
8133  pResult = drflac_open_and_read_pcm_frames_s16(onRead, onSeek, pUserData, &channels, &sampleRate, &totalPCMFrameCount);
8134  if (pResult == NULL) {
8135  return NULL;
8136  }
8137 
8138  if (channelsOut) {
8139  *channelsOut = channels;
8140  }
8141  if (sampleRateOut) {
8142  *sampleRateOut = sampleRate;
8143  }
8144  if (totalSampleCountOut) {
8145  *totalSampleCountOut = totalPCMFrameCount * channels;
8146  }
8147 
8148  return pResult;
8149 }
8150 
8151 
8152 float* drflac_open_and_read_pcm_frames_f32(drflac_read_proc onRead, drflac_seek_proc onSeek, void* pUserData, unsigned int* channelsOut, unsigned int* sampleRateOut, drflac_uint64* totalPCMFrameCountOut)
8153 {
8154  drflac* pFlac;
8155 
8156  if (channelsOut) {
8157  *channelsOut = 0;
8158  }
8159  if (sampleRateOut) {
8160  *sampleRateOut = 0;
8161  }
8162  if (totalPCMFrameCountOut) {
8163  *totalPCMFrameCountOut = 0;
8164  }
8165 
8166  pFlac = drflac_open(onRead, onSeek, pUserData);
8167  if (pFlac == NULL) {
8168  return NULL;
8169  }
8170 
8171  return drflac__full_read_and_close_f32(pFlac, channelsOut, sampleRateOut, totalPCMFrameCountOut);
8172 }
8173 
8174 float* drflac_open_and_decode_f32(drflac_read_proc onRead, drflac_seek_proc onSeek, void* pUserData, unsigned int* channelsOut, unsigned int* sampleRateOut, drflac_uint64* totalSampleCountOut)
8175 {
8176  unsigned int channels;
8177  unsigned int sampleRate;
8178  drflac_uint64 totalPCMFrameCount;
8179  float* pResult;
8180 
8181  if (channelsOut) {
8182  *channelsOut = 0;
8183  }
8184  if (sampleRateOut) {
8185  *sampleRateOut = 0;
8186  }
8187  if (totalSampleCountOut) {
8188  *totalSampleCountOut = 0;
8189  }
8190 
8191  pResult = drflac_open_and_read_pcm_frames_f32(onRead, onSeek, pUserData, &channels, &sampleRate, &totalPCMFrameCount);
8192  if (pResult == NULL) {
8193  return NULL;
8194  }
8195 
8196  if (channelsOut) {
8197  *channelsOut = channels;
8198  }
8199  if (sampleRateOut) {
8200  *sampleRateOut = sampleRate;
8201  }
8202  if (totalSampleCountOut) {
8203  *totalSampleCountOut = totalPCMFrameCount * channels;
8204  }
8205 
8206  return pResult;
8207 }
8208 
8209 #ifndef DR_FLAC_NO_STDIO
8210 drflac_int32* drflac_open_file_and_read_pcm_frames_s32(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount)
8211 {
8212  drflac* pFlac;
8213 
8214  if (sampleRate) {
8215  *sampleRate = 0;
8216  }
8217  if (channels) {
8218  *channels = 0;
8219  }
8220  if (totalPCMFrameCount) {
8221  *totalPCMFrameCount = 0;
8222  }
8223 
8224  pFlac = drflac_open_file(filename);
8225  if (pFlac == NULL) {
8226  return NULL;
8227  }
8228 
8229  return drflac__full_read_and_close_s32(pFlac, channels, sampleRate, totalPCMFrameCount);
8230 }
8231 
8232 drflac_int32* drflac_open_and_decode_file_s32(const char* filename, unsigned int* channelsOut, unsigned int* sampleRateOut, drflac_uint64* totalSampleCountOut)
8233 {
8234  unsigned int channels;
8235  unsigned int sampleRate;
8236  drflac_uint64 totalPCMFrameCount;
8237  drflac_int32* pResult;
8238 
8239  if (channelsOut) {
8240  *channelsOut = 0;
8241  }
8242  if (sampleRateOut) {
8243  *sampleRateOut = 0;
8244  }
8245  if (totalSampleCountOut) {
8246  *totalSampleCountOut = 0;
8247  }
8248 
8249  pResult = drflac_open_file_and_read_pcm_frames_s32(filename, &channels, &sampleRate, &totalPCMFrameCount);
8250  if (pResult == NULL) {
8251  return NULL;
8252  }
8253 
8254  if (channelsOut) {
8255  *channelsOut = channels;
8256  }
8257  if (sampleRateOut) {
8258  *sampleRateOut = sampleRate;
8259  }
8260  if (totalSampleCountOut) {
8261  *totalSampleCountOut = totalPCMFrameCount * channels;
8262  }
8263 
8264  return pResult;
8265 }
8266 
8267 
8268 drflac_int16* drflac_open_file_and_read_pcm_frames_s16(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount)
8269 {
8270  drflac* pFlac;
8271 
8272  if (sampleRate) {
8273  *sampleRate = 0;
8274  }
8275  if (channels) {
8276  *channels = 0;
8277  }
8278  if (totalPCMFrameCount) {
8279  *totalPCMFrameCount = 0;
8280  }
8281 
8282  pFlac = drflac_open_file(filename);
8283  if (pFlac == NULL) {
8284  return NULL;
8285  }
8286 
8287  return drflac__full_read_and_close_s16(pFlac, channels, sampleRate, totalPCMFrameCount);
8288 }
8289 
8290 drflac_int16* drflac_open_and_decode_file_s16(const char* filename, unsigned int* channelsOut, unsigned int* sampleRateOut, drflac_uint64* totalSampleCountOut)
8291 {
8292  unsigned int channels;
8293  unsigned int sampleRate;
8294  drflac_uint64 totalPCMFrameCount;
8295  drflac_int16* pResult;
8296 
8297  if (channelsOut) {
8298  *channelsOut = 0;
8299  }
8300  if (sampleRateOut) {
8301  *sampleRateOut = 0;
8302  }
8303  if (totalSampleCountOut) {
8304  *totalSampleCountOut = 0;
8305  }
8306 
8307  pResult = drflac_open_file_and_read_pcm_frames_s16(filename, &channels, &sampleRate, &totalPCMFrameCount);
8308  if (pResult == NULL) {
8309  return NULL;
8310  }
8311 
8312  if (channelsOut) {
8313  *channelsOut = channels;
8314  }
8315  if (sampleRateOut) {
8316  *sampleRateOut = sampleRate;
8317  }
8318  if (totalSampleCountOut) {
8319  *totalSampleCountOut = totalPCMFrameCount * channels;
8320  }
8321 
8322  return pResult;
8323 }
8324 
8325 
8326 float* drflac_open_file_and_read_pcm_frames_f32(const char* filename, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount)
8327 {
8328  drflac* pFlac;
8329 
8330  if (sampleRate) {
8331  *sampleRate = 0;
8332  }
8333  if (channels) {
8334  *channels = 0;
8335  }
8336  if (totalPCMFrameCount) {
8337  *totalPCMFrameCount = 0;
8338  }
8339 
8340  pFlac = drflac_open_file(filename);
8341  if (pFlac == NULL) {
8342  return NULL;
8343  }
8344 
8345  return drflac__full_read_and_close_f32(pFlac, channels, sampleRate, totalPCMFrameCount);
8346 }
8347 
8348 float* drflac_open_and_decode_file_f32(const char* filename, unsigned int* channelsOut, unsigned int* sampleRateOut, drflac_uint64* totalSampleCountOut)
8349 {
8350  unsigned int channels;
8351  unsigned int sampleRate;
8352  drflac_uint64 totalPCMFrameCount;
8353  float* pResult;
8354 
8355  if (channelsOut) {
8356  *channelsOut = 0;
8357  }
8358  if (sampleRateOut) {
8359  *sampleRateOut = 0;
8360  }
8361  if (totalSampleCountOut) {
8362  *totalSampleCountOut = 0;
8363  }
8364 
8365  pResult = drflac_open_file_and_read_pcm_frames_f32(filename, &channels, &sampleRate, &totalPCMFrameCount);
8366  if (pResult == NULL) {
8367  return NULL;
8368  }
8369 
8370  if (channelsOut) {
8371  *channelsOut = channels;
8372  }
8373  if (sampleRateOut) {
8374  *sampleRateOut = sampleRate;
8375  }
8376  if (totalSampleCountOut) {
8377  *totalSampleCountOut = totalPCMFrameCount * channels;
8378  }
8379 
8380  return pResult;
8381 }
8382 #endif
8383 
8384 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)
8385 {
8386  drflac* pFlac;
8387 
8388  if (sampleRate) {
8389  *sampleRate = 0;
8390  }
8391  if (channels) {
8392  *channels = 0;
8393  }
8394  if (totalPCMFrameCount) {
8395  *totalPCMFrameCount = 0;
8396  }
8397 
8398  pFlac = drflac_open_memory(data, dataSize);
8399  if (pFlac == NULL) {
8400  return NULL;
8401  }
8402 
8403  return drflac__full_read_and_close_s32(pFlac, channels, sampleRate, totalPCMFrameCount);
8404 }
8405 
8406 drflac_int32* drflac_open_and_decode_memory_s32(const void* data, size_t dataSize, unsigned int* channelsOut, unsigned int* sampleRateOut, drflac_uint64* totalSampleCountOut)
8407 {
8408  unsigned int channels;
8409  unsigned int sampleRate;
8410  drflac_uint64 totalPCMFrameCount;
8411  drflac_int32* pResult;
8412 
8413  if (channelsOut) {
8414  *channelsOut = 0;
8415  }
8416  if (sampleRateOut) {
8417  *sampleRateOut = 0;
8418  }
8419  if (totalSampleCountOut) {
8420  *totalSampleCountOut = 0;
8421  }
8422 
8423  pResult = drflac_open_memory_and_read_pcm_frames_s32(data, dataSize, &channels, &sampleRate, &totalPCMFrameCount);
8424  if (pResult == NULL) {
8425  return NULL;
8426  }
8427 
8428  if (channelsOut) {
8429  *channelsOut = channels;
8430  }
8431  if (sampleRateOut) {
8432  *sampleRateOut = sampleRate;
8433  }
8434  if (totalSampleCountOut) {
8435  *totalSampleCountOut = totalPCMFrameCount * channels;
8436  }
8437 
8438  return pResult;
8439 }
8440 
8441 
8442 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)
8443 {
8444  drflac* pFlac;
8445 
8446  if (sampleRate) {
8447  *sampleRate = 0;
8448  }
8449  if (channels) {
8450  *channels = 0;
8451  }
8452  if (totalPCMFrameCount) {
8453  *totalPCMFrameCount = 0;
8454  }
8455 
8456  pFlac = drflac_open_memory(data, dataSize);
8457  if (pFlac == NULL) {
8458  return NULL;
8459  }
8460 
8461  return drflac__full_read_and_close_s16(pFlac, channels, sampleRate, totalPCMFrameCount);
8462 }
8463 
8464 drflac_int16* drflac_open_and_decode_memory_s16(const void* data, size_t dataSize, unsigned int* channelsOut, unsigned int* sampleRateOut, drflac_uint64* totalSampleCountOut)
8465 {
8466  unsigned int channels;
8467  unsigned int sampleRate;
8468  drflac_uint64 totalPCMFrameCount;
8469  drflac_int16* pResult;
8470 
8471  if (channelsOut) {
8472  *channelsOut = 0;
8473  }
8474  if (sampleRateOut) {
8475  *sampleRateOut = 0;
8476  }
8477  if (totalSampleCountOut) {
8478  *totalSampleCountOut = 0;
8479  }
8480 
8481  pResult = drflac_open_memory_and_read_pcm_frames_s16(data, dataSize, &channels, &sampleRate, &totalPCMFrameCount);
8482  if (pResult == NULL) {
8483  return NULL;
8484  }
8485 
8486  if (channelsOut) {
8487  *channelsOut = channels;
8488  }
8489  if (sampleRateOut) {
8490  *sampleRateOut = sampleRate;
8491  }
8492  if (totalSampleCountOut) {
8493  *totalSampleCountOut = totalPCMFrameCount * channels;
8494  }
8495 
8496  return pResult;
8497 }
8498 
8499 
8500 float* drflac_open_memory_and_read_pcm_frames_f32(const void* data, size_t dataSize, unsigned int* channels, unsigned int* sampleRate, drflac_uint64* totalPCMFrameCount)
8501 {
8502  drflac* pFlac;
8503 
8504  if (sampleRate) {
8505  *sampleRate = 0;
8506  }
8507  if (channels) {
8508  *channels = 0;
8509  }
8510  if (totalPCMFrameCount) {
8511  *totalPCMFrameCount = 0;
8512  }
8513 
8514  pFlac = drflac_open_memory(data, dataSize);
8515  if (pFlac == NULL) {
8516  return NULL;
8517  }
8518 
8519  return drflac__full_read_and_close_f32(pFlac, channels, sampleRate, totalPCMFrameCount);
8520 }
8521 
8522 float* drflac_open_and_decode_memory_f32(const void* data, size_t dataSize, unsigned int* channelsOut, unsigned int* sampleRateOut, drflac_uint64* totalSampleCountOut)
8523 {
8524  unsigned int channels;
8525  unsigned int sampleRate;
8526  drflac_uint64 totalPCMFrameCount;
8527  float* pResult;
8528 
8529  if (channelsOut) {
8530  *channelsOut = 0;
8531  }
8532  if (sampleRateOut) {
8533  *sampleRateOut = 0;
8534  }
8535  if (totalSampleCountOut) {
8536  *totalSampleCountOut = 0;
8537  }
8538 
8539  pResult = drflac_open_memory_and_read_pcm_frames_f32(data, dataSize, &channels, &sampleRate, &totalPCMFrameCount);
8540  if (pResult == NULL) {
8541  return NULL;
8542  }
8543 
8544  if (channelsOut) {
8545  *channelsOut = channels;
8546  }
8547  if (sampleRateOut) {
8548  *sampleRateOut = sampleRate;
8549  }
8550  if (totalSampleCountOut) {
8551  *totalSampleCountOut = totalPCMFrameCount * channels;
8552  }
8553 
8554  return pResult;
8555 }
8556 
8557 
8558 void drflac_free(void* pSampleDataReturnedByOpenAndDecode)
8559 {
8560  DRFLAC_FREE(pSampleDataReturnedByOpenAndDecode);
8561 }
8562 
8563 
8564 
8565 
8566 void drflac_init_vorbis_comment_iterator(drflac_vorbis_comment_iterator* pIter, drflac_uint32 commentCount, const void* pComments)
8567 {
8568  if (pIter == NULL) {
8569  return;
8570  }
8571 
8572  pIter->countRemaining = commentCount;
8573  pIter->pRunningData = (const char*)pComments;
8574 }
8575 
8576 const char* drflac_next_vorbis_comment(drflac_vorbis_comment_iterator* pIter, drflac_uint32* pCommentLengthOut)
8577 {
8578  drflac_int32 length;
8579  const char* pComment;
8580 
8581  /* Safety. */
8582  if (pCommentLengthOut) {
8583  *pCommentLengthOut = 0;
8584  }
8585 
8586  if (pIter == NULL || pIter->countRemaining == 0 || pIter->pRunningData == NULL) {
8587  return NULL;
8588  }
8589 
8590  length = drflac__le2host_32(*(const drflac_uint32*)pIter->pRunningData);
8591  pIter->pRunningData += 4;
8592 
8593  pComment = pIter->pRunningData;
8594  pIter->pRunningData += length;
8595  pIter->countRemaining -= 1;
8596 
8597  if (pCommentLengthOut) {
8598  *pCommentLengthOut = length;
8599  }
8600 
8601  return pComment;
8602 }
8603 
8604 
8605 
8606 
8607 void drflac_init_cuesheet_track_iterator(drflac_cuesheet_track_iterator* pIter, drflac_uint32 trackCount, const void* pTrackData)
8608 {
8609  if (pIter == NULL) {
8610  return;
8611  }
8612 
8613  pIter->countRemaining = trackCount;
8614  pIter->pRunningData = (const char*)pTrackData;
8615 }
8616 
8618 {
8619  drflac_cuesheet_track cuesheetTrack;
8620  const char* pRunningData;
8621  drflac_uint64 offsetHi;
8622  drflac_uint64 offsetLo;
8623 
8624  if (pIter == NULL || pIter->countRemaining == 0 || pIter->pRunningData == NULL) {
8625  return DRFLAC_FALSE;
8626  }
8627 
8628  pRunningData = pIter->pRunningData;
8629 
8630  offsetHi = drflac__be2host_32(*(const drflac_uint32*)pRunningData); pRunningData += 4;
8631  offsetLo = drflac__be2host_32(*(const drflac_uint32*)pRunningData); pRunningData += 4;
8632  cuesheetTrack.offset = offsetLo | (offsetHi << 32);
8633  cuesheetTrack.trackNumber = pRunningData[0]; pRunningData += 1;
8634  drflac_copy_memory(cuesheetTrack.ISRC, pRunningData, sizeof(cuesheetTrack.ISRC)); pRunningData += 12;
8635  cuesheetTrack.isAudio = (pRunningData[0] & 0x80) != 0;
8636  cuesheetTrack.preEmphasis = (pRunningData[0] & 0x40) != 0; pRunningData += 14;
8637  cuesheetTrack.indexCount = pRunningData[0]; pRunningData += 1;
8638  cuesheetTrack.pIndexPoints = (const drflac_cuesheet_track_index*)pRunningData; pRunningData += cuesheetTrack.indexCount * sizeof(drflac_cuesheet_track_index);
8639 
8640  pIter->pRunningData = pRunningData;
8641  pIter->countRemaining -= 1;
8642 
8643  if (pCuesheetTrack) {
8644  *pCuesheetTrack = cuesheetTrack;
8645  }
8646 
8647  return DRFLAC_TRUE;
8648 }
8649 
8650 #if defined(__GNUC__)
8651  #pragma GCC diagnostic pop
8652 #endif
8653 #endif /* DR_FLAC_IMPLEMENTATION */
8654 
8655 
8656 /*
8657 REVISION HISTORY
8658 ================
8659 v0.11.8 - 2019-05-21
8660  - Fix warnings.
8661 
8662 v0.11.7 - 2019-05-06
8663  - C89 fixes.
8664 
8665 v0.11.6 - 2019-05-05
8666  - Add support for C89.
8667  - Fix a compiler warning when CRC is disabled.
8668  - Change license to choice of public domain or MIT-0.
8669 
8670 v0.11.5 - 2019-04-19
8671  - Fix a compiler error with GCC.
8672 
8673 v0.11.4 - 2019-04-17
8674  - Fix some warnings with GCC when compiling with -std=c99.
8675 
8676 v0.11.3 - 2019-04-07
8677  - Silence warnings with GCC.
8678 
8679 v0.11.2 - 2019-03-10
8680  - Fix a warning.
8681 
8682 v0.11.1 - 2019-02-17
8683  - Fix a potential bug with seeking.
8684 
8685 v0.11.0 - 2018-12-16
8686  - API CHANGE: Deprecated drflac_read_s32(), drflac_read_s16() and drflac_read_f32() and replaced them with
8687  drflac_read_pcm_frames_s32(), drflac_read_pcm_frames_s16() and drflac_read_pcm_frames_f32(). The new APIs take
8688  and return PCM frame counts instead of sample counts. To upgrade you will need to change the input count by
8689  dividing it by the channel count, and then do the same with the return value.
8690  - API_CHANGE: Deprecated drflac_seek_to_sample() and replaced with drflac_seek_to_pcm_frame(). Same rules as
8691  the changes to drflac_read_*() apply.
8692  - API CHANGE: Deprecated drflac_open_and_decode_*() and replaced with drflac_open_*_and_read_*(). Same rules as
8693  the changes to drflac_read_*() apply.
8694  - Optimizations.
8695 
8696 v0.10.0 - 2018-09-11
8697  - Remove the DR_FLAC_NO_WIN32_IO option and the Win32 file IO functionality. If you need to use Win32 file IO you
8698  need to do it yourself via the callback API.
8699  - Fix the clang build.
8700  - Fix undefined behavior.
8701  - Fix errors with CUESHEET metdata blocks.
8702  - Add an API for iterating over each cuesheet track in the CUESHEET metadata block. This works the same way as the
8703  Vorbis comment API.
8704  - Other miscellaneous bug fixes, mostly relating to invalid FLAC streams.
8705  - Minor optimizations.
8706 
8707 v0.9.11 - 2018-08-29
8708  - Fix a bug with sample reconstruction.
8709 
8710 v0.9.10 - 2018-08-07
8711  - Improve 64-bit detection.
8712 
8713 v0.9.9 - 2018-08-05
8714  - Fix C++ build on older versions of GCC.
8715 
8716 v0.9.8 - 2018-07-24
8717  - Fix compilation errors.
8718 
8719 v0.9.7 - 2018-07-05
8720  - Fix a warning.
8721 
8722 v0.9.6 - 2018-06-29
8723  - Fix some typos.
8724 
8725 v0.9.5 - 2018-06-23
8726  - Fix some warnings.
8727 
8728 v0.9.4 - 2018-06-14
8729  - Optimizations to seeking.
8730  - Clean up.
8731 
8732 v0.9.3 - 2018-05-22
8733  - Bug fix.
8734 
8735 v0.9.2 - 2018-05-12
8736  - Fix a compilation error due to a missing break statement.
8737 
8738 v0.9.1 - 2018-04-29
8739  - Fix compilation error with Clang.
8740 
8741 v0.9 - 2018-04-24
8742  - Fix Clang build.
8743  - Start using major.minor.revision versioning.
8744 
8745 v0.8g - 2018-04-19
8746  - Fix build on non-x86/x64 architectures.
8747 
8748 v0.8f - 2018-02-02
8749  - Stop pretending to support changing rate/channels mid stream.
8750 
8751 v0.8e - 2018-02-01
8752  - Fix a crash when the block size of a frame is larger than the maximum block size defined by the FLAC stream.
8753  - Fix a crash the the Rice partition order is invalid.
8754 
8755 v0.8d - 2017-09-22
8756  - Add support for decoding streams with ID3 tags. ID3 tags are just skipped.
8757 
8758 v0.8c - 2017-09-07
8759  - Fix warning on non-x86/x64 architectures.
8760 
8761 v0.8b - 2017-08-19
8762  - Fix build on non-x86/x64 architectures.
8763 
8764 v0.8a - 2017-08-13
8765  - A small optimization for the Clang build.
8766 
8767 v0.8 - 2017-08-12
8768  - API CHANGE: Rename dr_* types to drflac_*.
8769  - Optimizations. This brings dr_flac back to about the same class of efficiency as the reference implementation.
8770  - Add support for custom implementations of malloc(), realloc(), etc.
8771  - Add CRC checking to Ogg encapsulated streams.
8772  - Fix VC++ 6 build. This is only for the C++ compiler. The C compiler is not currently supported.
8773  - Bug fixes.
8774 
8775 v0.7 - 2017-07-23
8776  - Add support for opening a stream without a header block. To do this, use drflac_open_relaxed() / drflac_open_with_metadata_relaxed().
8777 
8778 v0.6 - 2017-07-22
8779  - Add support for recovering from invalid frames. With this change, dr_flac will simply skip over invalid frames as if they
8780  never existed. Frames are checked against their sync code, the CRC-8 of the frame header and the CRC-16 of the whole frame.
8781 
8782 v0.5 - 2017-07-16
8783  - Fix typos.
8784  - Change drflac_bool* types to unsigned.
8785  - Add CRC checking. This makes dr_flac slower, but can be disabled with #define DR_FLAC_NO_CRC.
8786 
8787 v0.4f - 2017-03-10
8788  - Fix a couple of bugs with the bitstreaming code.
8789 
8790 v0.4e - 2017-02-17
8791  - Fix some warnings.
8792 
8793 v0.4d - 2016-12-26
8794  - Add support for 32-bit floating-point PCM decoding.
8795  - Use drflac_int* and drflac_uint* sized types to improve compiler support.
8796  - Minor improvements to documentation.
8797 
8798 v0.4c - 2016-12-26
8799  - Add support for signed 16-bit integer PCM decoding.
8800 
8801 v0.4b - 2016-10-23
8802  - A minor change to drflac_bool8 and drflac_bool32 types.
8803 
8804 v0.4a - 2016-10-11
8805  - Rename drBool32 to drflac_bool32 for styling consistency.
8806 
8807 v0.4 - 2016-09-29
8808  - API/ABI CHANGE: Use fixed size 32-bit booleans instead of the built-in bool type.
8809  - API CHANGE: Rename drflac_open_and_decode*() to drflac_open_and_decode*_s32().
8810  - API CHANGE: Swap the order of "channels" and "sampleRate" parameters in drflac_open_and_decode*(). Rationale for this is to
8811  keep it consistent with drflac_audio.
8812 
8813 v0.3f - 2016-09-21
8814  - Fix a warning with GCC.
8815 
8816 v0.3e - 2016-09-18
8817  - Fixed a bug where GCC 4.3+ was not getting properly identified.
8818  - Fixed a few typos.
8819  - Changed date formats to ISO 8601 (YYYY-MM-DD).
8820 
8821 v0.3d - 2016-06-11
8822  - Minor clean up.
8823 
8824 v0.3c - 2016-05-28
8825  - Fixed compilation error.
8826 
8827 v0.3b - 2016-05-16
8828  - Fixed Linux/GCC build.
8829  - Updated documentation.
8830 
8831 v0.3a - 2016-05-15
8832  - Minor fixes to documentation.
8833 
8834 v0.3 - 2016-05-11
8835  - Optimizations. Now at about parity with the reference implementation on 32-bit builds.
8836  - Lots of clean up.
8837 
8838 v0.2b - 2016-05-10
8839  - Bug fixes.
8840 
8841 v0.2a - 2016-05-10
8842  - Made drflac_open_and_decode() more robust.
8843  - Removed an unused debugging variable
8844 
8845 v0.2 - 2016-05-09
8846  - Added support for Ogg encapsulation.
8847  - API CHANGE. Have the onSeek callback take a third argument which specifies whether or not the seek
8848  should be relative to the start or the current position. Also changes the seeking rules such that
8849  seeking offsets will never be negative.
8850  - Have drflac_open_and_decode() fail gracefully if the stream has an unknown total sample count.
8851 
8852 v0.1b - 2016-05-07
8853  - Properly close the file handle in drflac_open_file() and family when the decoder fails to initialize.
8854  - Removed a stale comment.
8855 
8856 v0.1a - 2016-05-05
8857  - Minor formatting changes.
8858  - Fixed a warning on the GCC build.
8859 
8860 v0.1 - 2016-05-03
8861  - Initial versioned release.
8862 */
8863 
8864 /*
8865 This software is available as a choice of the following licenses. Choose
8866 whichever you prefer.
8867 
8868 ===============================================================================
8869 ALTERNATIVE 1 - Public Domain (www.unlicense.org)
8870 ===============================================================================
8871 This is free and unencumbered software released into the public domain.
8872 
8873 Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
8874 software, either in source code form or as a compiled binary, for any purpose,
8875 commercial or non-commercial, and by any means.
8876 
8877 In jurisdictions that recognize copyright laws, the author or authors of this
8878 software dedicate any and all copyright interest in the software to the public
8879 domain. We make this dedication for the benefit of the public at large and to
8880 the detriment of our heirs and successors. We intend this dedication to be an
8881 overt act of relinquishment in perpetuity of all present and future rights to
8882 this software under copyright law.
8883 
8884 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8885 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8886 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8887 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
8888 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
8889 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8890 
8891 For more information, please refer to <http://unlicense.org/>
8892 
8893 ===============================================================================
8894 ALTERNATIVE 2 - MIT No Attribution
8895 ===============================================================================
8896 Copyright 2018 David Reid
8897 
8898 Permission is hereby granted, free of charge, to any person obtaining a copy of
8899 this software and associated documentation files (the "Software"), to deal in
8900 the Software without restriction, including without limitation the rights to
8901 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8902 of the Software, and to permit persons to whom the Software is furnished to do
8903 so.
8904 
8905 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8906 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8907 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8908 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8909 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
8910 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
8911 SOFTWARE.
8912 */
drflac_bool8
drflac_uint8 drflac_bool8
Definition: dr_flac.h:142
drflac_frame::samplesRemaining
drflac_uint32 samplesRemaining
Definition: dr_flac.h:481
drflac_metadata::leadInSampleCount
drflac_uint64 leadInSampleCount
Definition: dr_flac.h:302
drflac_metadata::trackCount
drflac_uint8 trackCount
Definition: dr_flac.h:304
drflac_cuesheet_track_index::index
drflac_uint8 index
Definition: dr_flac.h:802
drflac_open_and_decode_memory_s32
DRFLAC_DEPRECATED drflac_int32 * drflac_open_and_decode_memory_s32(const void *data, size_t dataSize, unsigned int *channels, unsigned int *sampleRate, drflac_uint64 *totalSampleCount)
drflac_streaminfo::minBlockSize
drflac_uint16 minBlockSize
Definition: dr_flac.h:243
drflac__memory_stream
Definition: dr_flac.h:366
drflac_open_and_decode_f32
DRFLAC_DEPRECATED float * drflac_open_and_decode_f32(drflac_read_proc onRead, drflac_seek_proc onSeek, void *pUserData, unsigned int *channels, unsigned int *sampleRate, drflac_uint64 *totalSampleCount)
drflac_frame_header::sampleNumber
drflac_uint64 sampleNumber
Definition: dr_flac.h:448
L
#define L
Definition: stb_vorbis.c:5002
drflac_open_and_read_pcm_frames_s16
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)
drflac_seek_origin
drflac_seek_origin
Definition: dr_flac.h:225
drflac_container_native
@ drflac_container_native
Definition: dr_flac.h:220
drflac_bs::cacheL2
drflac_cache_t cacheL2[DR_FLAC_BUFFER_SIZE/sizeof(drflac_cache_t)]
Definition: dr_flac.h:406
drflac::pDecodedSamples
drflac_int32 * pDecodedSamples
Definition: dr_flac.h:542
drflac_open_and_decode_memory_s16
DRFLAC_DEPRECATED drflac_int16 * drflac_open_and_decode_memory_s16(const void *data, size_t dataSize, unsigned int *channels, unsigned int *sampleRate, drflac_uint64 *totalSampleCount)
drflac_open
drflac * drflac_open(drflac_read_proc onRead, drflac_seek_proc onSeek, void *pUserData)
drflac_metadata::colorDepth
drflac_uint32 colorDepth
Definition: dr_flac.h:317
drflac_bs::onRead
drflac_read_proc onRead
Definition: dr_flac.h:377
drflac_subframe::subframeType
drflac_uint8 subframeType
Definition: dr_flac.h:421
drflac_metadata::rawDataSize
drflac_uint32 rawDataSize
Definition: dr_flac.h:267
drflac_bs::unalignedByteCount
size_t unalignedByteCount
Definition: dr_flac.h:391
drflac_cuesheet_track::trackNumber
drflac_uint8 trackNumber
Definition: dr_flac.h:810
drflac_uint32
uint32_t drflac_uint32
Definition: dr_flac.h:138
int
CONST PIXELFORMATDESCRIPTOR int
Definition: qgl_win.c:35
drflac_open_file
drflac * drflac_open_file(const char *filename)
drflac_open_file_and_read_pcm_frames_s16
drflac_int16 * drflac_open_file_and_read_pcm_frames_s16(const char *filename, unsigned int *channels, unsigned int *sampleRate, drflac_uint64 *totalPCMFrameCount)
drflac_cuesheet_track
Definition: dr_flac.h:807
drflac_metadata::vendor
const char * vendor
Definition: dr_flac.h:294
drflac_seekpoint::sampleCount
drflac_uint16 sampleCount
Definition: dr_flac.h:237
drflac_streaminfo::maxFrameSize
drflac_uint32 maxFrameSize
Definition: dr_flac.h:246
drflac
Definition: dr_flac.h:487
drflac_metadata::application
struct drflac_metadata::@0::@2 application
drflac_metadata::padding
struct drflac_metadata::@0::@1 padding
drflac_seek_origin_current
@ drflac_seek_origin_current
Definition: dr_flac.h:228
drflac::channels
drflac_uint8 channels
Definition: dr_flac.h:503
drflac::totalSampleCount
drflac_uint64 totalSampleCount
Definition: dr_flac.h:516
drflac_next_vorbis_comment
const char * drflac_next_vorbis_comment(drflac_vorbis_comment_iterator *pIter, drflac_uint32 *pCommentLengthOut)
drflac_read_s16
DRFLAC_DEPRECATED drflac_uint64 drflac_read_s16(drflac *pFlac, drflac_uint64 samplesToRead, drflac_int16 *pBufferOut)
drflac_seek_to_pcm_frame
drflac_bool32 drflac_seek_to_pcm_frame(drflac *pFlac, drflac_uint64 pcmFrameIndex)
drflac_cuesheet_track_iterator::countRemaining
drflac_uint32 countRemaining
Definition: dr_flac.h:793
drflac_seek_to_sample
DRFLAC_DEPRECATED drflac_bool32 drflac_seek_to_sample(drflac *pFlac, drflac_uint64 sampleIndex)
drflac_vorbis_comment_iterator
Definition: dr_flac.h:771
drflac_streaminfo::channels
drflac_uint8 channels
Definition: dr_flac.h:248
drflac_streaminfo::minFrameSize
drflac_uint32 minFrameSize
Definition: dr_flac.h:245
drflac_free
void drflac_free(void *p)
DRFLAC_METADATA_BLOCK_TYPE_APPLICATION
#define DRFLAC_METADATA_BLOCK_TYPE_APPLICATION
Definition: dr_flac.h:188
drflac_subframe::pDecodedSamples
drflac_int32 * pDecodedSamples
Definition: dr_flac.h:439
drflac_vorbis_comment_iterator::countRemaining
drflac_uint32 countRemaining
Definition: dr_flac.h:773
drflac_metadata::cuesheet
struct drflac_metadata::@0::@5 cuesheet
DRFLAC_METADATA_BLOCK_TYPE_SEEKTABLE
#define DRFLAC_METADATA_BLOCK_TYPE_SEEKTABLE
Definition: dr_flac.h:189
drflac_frame
Definition: dr_flac.h:472
x
GLint GLenum GLint x
Definition: qgl_win.c:116
drflac_frame_header::channelAssignment
drflac_uint8 channelAssignment
Definition: dr_flac.h:463
i
int i
Definition: q_shared.c:305
drflac_seek_origin_start
@ drflac_seek_origin_start
Definition: dr_flac.h:227
drflac_int16
int16_t drflac_int16
Definition: dr_flac.h:135
drflac_int64
int64_t drflac_int64
Definition: dr_flac.h:139
DRFLAC_METADATA_BLOCK_TYPE_STREAMINFO
#define DRFLAC_METADATA_BLOCK_TYPE_STREAMINFO
Definition: dr_flac.h:186
drflac_close
void drflac_close(drflac *pFlac)
drflac_uint8
uint8_t drflac_uint8
Definition: dr_flac.h:134
drflac__memory_stream::dataSize
size_t dataSize
Definition: dr_flac.h:369
drflac_metadata::data
union drflac_metadata::@0 data
drflac_open_file_and_read_pcm_frames_f32
float * drflac_open_file_and_read_pcm_frames_f32(const char *filename, unsigned int *channels, unsigned int *sampleRate, drflac_uint64 *totalPCMFrameCount)
drflac_subframe::lpcOrder
drflac_uint8 lpcOrder
Definition: dr_flac.h:427
drflac_metadata::dataSize
drflac_uint32 dataSize
Definition: dr_flac.h:282
order
GLdouble GLdouble GLint GLint order
Definition: qgl_win.c:225
drflac::memoryStream
drflac__memory_stream memoryStream
Definition: dr_flac.h:538
type
GLenum type
Definition: qgl_win.c:72
drflac_cache_t
drflac_uint32 drflac_cache_t
Definition: dr_flac.h:182
drflac_metadata::pComments
const void * pComments
Definition: dr_flac.h:296
drflac_metadata::streaminfo
drflac_streaminfo streaminfo
Definition: dr_flac.h:271
DRFLAC_METADATA_BLOCK_TYPE_CUESHEET
#define DRFLAC_METADATA_BLOCK_TYPE_CUESHEET
Definition: dr_flac.h:191
drflac_bs::onSeek
drflac_seek_proc onSeek
Definition: dr_flac.h:380
DRFLAC_METADATA_BLOCK_TYPE_PADDING
#define DRFLAC_METADATA_BLOCK_TYPE_PADDING
Definition: dr_flac.h:187
drflac_subframe::wastedBitsPerSample
drflac_uint8 wastedBitsPerSample
Definition: dr_flac.h:424
drflac_metadata::seektable
struct drflac_metadata::@0::@3 seektable
drflac_metadata::description
const char * description
Definition: dr_flac.h:314
drflac_seek_proc
drflac_bool32(* drflac_seek_proc)(void *pUserData, int offset, drflac_seek_origin origin)
Definition: dr_flac.h:352
j
GLint j
Definition: qgl_win.c:150
drflac_cuesheet_track::isAudio
drflac_bool8 isAudio
Definition: dr_flac.h:812
drflac_bs::crc16Cache
drflac_cache_t crc16Cache
Definition: dr_flac.h:414
drflac::seekpointCount
drflac_uint32 seekpointCount
Definition: dr_flac.h:524
drflac::container
drflac_container container
Definition: dr_flac.h:521
drflac_frame_header::crc8
drflac_uint8 crc8
Definition: dr_flac.h:469
drflac_streaminfo::maxBlockSize
drflac_uint16 maxBlockSize
Definition: dr_flac.h:244
drflac_init_cuesheet_track_iterator
void drflac_init_cuesheet_track_iterator(drflac_cuesheet_track_iterator *pIter, drflac_uint32 trackCount, const void *pTrackData)
drflac_metadata::id
drflac_uint32 id
Definition: dr_flac.h:280
drflac_subframe::bitsPerSample
drflac_uint32 bitsPerSample
Definition: dr_flac.h:433
drflac_read_proc
size_t(* drflac_read_proc)(void *pUserData, void *pBufferOut, size_t bytesToRead)
Definition: dr_flac.h:338
drflac_subframe
Definition: dr_flac.h:418
drflac_cuesheet_track_index::offset
drflac_uint64 offset
Definition: dr_flac.h:801
drflac_frame_header
Definition: dr_flac.h:442
r
GLdouble GLdouble r
Definition: qgl_win.c:336
drflac_open_memory_and_read_pcm_frames_s16
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)
drflac_metadata::pictureDataSize
drflac_uint32 pictureDataSize
Definition: dr_flac.h:319
drflac::maxBlockSize
drflac_uint16 maxBlockSize
Definition: dr_flac.h:509
drflac_open_and_read_pcm_frames_s32
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)
drflac::pUserDataMD
void * pUserDataMD
Definition: dr_flac.h:493
drflac_frame_header::blockSize
drflac_uint16 blockSize
Definition: dr_flac.h:457
drflac_cuesheet_track_iterator
Definition: dr_flac.h:791
drflac_metadata::vendorLength
drflac_uint32 vendorLength
Definition: dr_flac.h:293
drflac_frame_header::bitsPerSample
drflac_uint8 bitsPerSample
Definition: dr_flac.h:466
drflac_bs
Definition: dr_flac.h:374
drflac_read_pcm_frames_f32
drflac_uint64 drflac_read_pcm_frames_f32(drflac *pFlac, drflac_uint64 framesToRead, float *pBufferOut)
drflac_container_unknown
@ drflac_container_unknown
Definition: dr_flac.h:222
DRFLAC_TRUE
#define DRFLAC_TRUE
Definition: dr_flac.h:144
t
GLdouble t
Definition: qgl_win.c:328
drflac_cuesheet_track::pIndexPoints
const drflac_cuesheet_track_index * pIndexPoints
Definition: dr_flac.h:815
drflac_metadata::pSeekpoints
const drflac_seekpoint * pSeekpoints
Definition: dr_flac.h:288
drflac_open_memory
drflac * drflac_open_memory(const void *data, size_t dataSize)
drflac_bool32
drflac_uint32 drflac_bool32
Definition: dr_flac.h:143
drflac__memory_stream::currentReadPos
size_t currentReadPos
Definition: dr_flac.h:370
drflac_seekpoint
Definition: dr_flac.h:233
drflac_cuesheet_track::indexCount
drflac_uint8 indexCount
Definition: dr_flac.h:814
drflac_uint64
uint64_t drflac_uint64
Definition: dr_flac.h:140
drflac_frame::header
drflac_frame_header header
Definition: dr_flac.h:475
drflac_read_s32
DRFLAC_DEPRECATED drflac_uint64 drflac_read_s32(drflac *pFlac, drflac_uint64 samplesToRead, drflac_int32 *pBufferOut)
drflac_metadata::mimeLength
drflac_uint32 mimeLength
Definition: dr_flac.h:311
drflac_metadata::pData
const void * pData
Definition: dr_flac.h:281
drflac_metadata::mime
const char * mime
Definition: dr_flac.h:312
drflac_metadata::width
drflac_uint32 width
Definition: dr_flac.h:315
drflac_vorbis_comment_iterator::pRunningData
const char * pRunningData
Definition: dr_flac.h:774
drflac_metadata::vorbis_comment
struct drflac_metadata::@0::@4 vorbis_comment
drflac_open_with_metadata_relaxed
drflac * drflac_open_with_metadata_relaxed(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_meta_proc onMeta, drflac_container container, void *pUserData)
NULL
#define NULL
Definition: q_shared.h:67
channels
channel_t channels[MAX_CHANNELS]
Definition: snd_dma.c:42
drflac_seekpoint::firstSample
drflac_uint64 firstSample
Definition: dr_flac.h:235
drflac_uint16
uint16_t drflac_uint16
Definition: dr_flac.h:136
drflac_cuesheet_track_iterator::pRunningData
const char * pRunningData
Definition: dr_flac.h:794
DRFLAC_METADATA_BLOCK_TYPE_VORBIS_COMMENT
#define DRFLAC_METADATA_BLOCK_TYPE_VORBIS_COMMENT
Definition: dr_flac.h:190
drflac_open_and_decode_memory_f32
DRFLAC_DEPRECATED float * drflac_open_and_decode_memory_f32(const void *data, size_t dataSize, unsigned int *channels, unsigned int *sampleRate, drflac_uint64 *totalSampleCount)
drflac_metadata::height
drflac_uint32 height
Definition: dr_flac.h:316
drflac_open_memory_and_read_pcm_frames_f32
float * drflac_open_memory_and_read_pcm_frames_f32(const void *data, size_t dataSize, unsigned int *channels, unsigned int *sampleRate, drflac_uint64 *totalPCMFrameCount)
drflac_bs::crc16
drflac_uint16 crc16
Definition: dr_flac.h:413
DR_FLAC_BUFFER_SIZE
#define DR_FLAC_BUFFER_SIZE
Definition: dr_flac.h:167
drflac::currentFrame
drflac_frame currentFrame
Definition: dr_flac.h:528
drflac::currentSample
drflac_uint64 currentSample
Definition: dr_flac.h:531
drflac_metadata::picture
struct drflac_metadata::@0::@6 picture
drflac_metadata::seekpointCount
drflac_uint32 seekpointCount
Definition: dr_flac.h:287
drflac_streaminfo::md5
drflac_uint8 md5[16]
Definition: dr_flac.h:251
drflac_open_with_metadata
drflac * drflac_open_with_metadata(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_meta_proc onMeta, void *pUserData)
drflac_read_pcm_frames_s32
drflac_uint64 drflac_read_pcm_frames_s32(drflac *pFlac, drflac_uint64 framesToRead, drflac_int32 *pBufferOut)
drflac::pExtraData
drflac_uint8 pExtraData[1]
Definition: dr_flac.h:554
drflac_metadata::indexColorCount
drflac_uint32 indexColorCount
Definition: dr_flac.h:318
drflac_metadata::pRawData
const void * pRawData
Definition: dr_flac.h:264
drflac::bs
drflac_bs bs
Definition: dr_flac.h:551
drflac_streaminfo::totalSampleCount
drflac_uint64 totalSampleCount
Definition: dr_flac.h:250
DRFLAC_METADATA_BLOCK_TYPE_INVALID
#define DRFLAC_METADATA_BLOCK_TYPE_INVALID
Definition: dr_flac.h:193
drflac_open_and_decode_file_s16
DRFLAC_DEPRECATED drflac_int16 * drflac_open_and_decode_file_s16(const char *filename, unsigned int *channels, unsigned int *sampleRate, drflac_uint64 *totalSampleCount)
drflac_open_and_decode_s16
DRFLAC_DEPRECATED drflac_int16 * drflac_open_and_decode_s16(drflac_read_proc onRead, drflac_seek_proc onSeek, void *pUserData, unsigned int *channels, unsigned int *sampleRate, drflac_uint64 *totalSampleCount)
drflac::bitsPerSample
drflac_uint8 bitsPerSample
Definition: dr_flac.h:506
drflac_cuesheet_track_index
Definition: dr_flac.h:799
drflac_int8
int8_t drflac_int8
Definition: dr_flac.h:133
drflac_bs::cache
drflac_cache_t cache
Definition: dr_flac.h:407
DRFLAC_DEPRECATED
#define DRFLAC_DEPRECATED
Definition: dr_flac.h:158
drflac_open_memory_and_read_pcm_frames_s32
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)
drflac_open_file_and_read_pcm_frames_s32
drflac_int32 * drflac_open_file_and_read_pcm_frames_s32(const char *filename, unsigned int *channels, unsigned int *sampleRate, drflac_uint64 *totalPCMFrameCount)
drflac__memory_stream::data
const drflac_uint8 * data
Definition: dr_flac.h:368
drflac_next_cuesheet_track
drflac_bool32 drflac_next_cuesheet_track(drflac_cuesheet_track_iterator *pIter, drflac_cuesheet_track *pCuesheetTrack)
drflac_cuesheet_track::preEmphasis
drflac_bool8 preEmphasis
Definition: dr_flac.h:813
drflac_metadata::type
drflac_uint32 type
Definition: dr_flac.h:257
drflac_open_and_decode_file_f32
DRFLAC_DEPRECATED float * drflac_open_and_decode_file_f32(const char *filename, unsigned int *channels, unsigned int *sampleRate, drflac_uint64 *totalSampleCount)
drflac_container_ogg
@ drflac_container_ogg
Definition: dr_flac.h:221
drflac_metadata::descriptionLength
drflac_uint32 descriptionLength
Definition: dr_flac.h:313
drflac_int32
int32_t drflac_int32
Definition: dr_flac.h:137
drflac_frame_header::sampleRate
drflac_uint32 sampleRate
Definition: dr_flac.h:454
drflac_bs::unalignedCache
drflac_cache_t unalignedCache
Definition: dr_flac.h:394
drflac_seekpoint::frameOffset
drflac_uint64 frameOffset
Definition: dr_flac.h:236
drflac_open_relaxed
drflac * drflac_open_relaxed(drflac_read_proc onRead, drflac_seek_proc onSeek, drflac_container container, void *pUserData)
drflac_streaminfo::sampleRate
drflac_uint32 sampleRate
Definition: dr_flac.h:247
drflac::sampleRate
drflac_uint32 sampleRate
Definition: dr_flac.h:497
drflac_streaminfo
Definition: dr_flac.h:241
DRFLAC_FALSE
#define DRFLAC_FALSE
Definition: dr_flac.h:145
drflac_open_and_read_pcm_frames_f32
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)
drflac_metadata::isCD
drflac_bool32 isCD
Definition: dr_flac.h:303
drflac_streaminfo::bitsPerSample
drflac_uint8 bitsPerSample
Definition: dr_flac.h:249
sample
Definition: jar_mod.h:111
right
GLdouble right
Definition: qgl_win.c:159
drflac_metadata::pTrackData
const void * pTrackData
Definition: dr_flac.h:305
drflac_metadata::pPictureData
const drflac_uint8 * pPictureData
Definition: dr_flac.h:320
drflac_init_vorbis_comment_iterator
void drflac_init_vorbis_comment_iterator(drflac_vorbis_comment_iterator *pIter, drflac_uint32 commentCount, const void *pComments)
drflac_open_file_with_metadata
drflac * drflac_open_file_with_metadata(const char *filename, drflac_meta_proc onMeta, void *pUserData)
drflac_frame::subframes
drflac_subframe subframes[8]
Definition: dr_flac.h:484
drflac_cuesheet_track::offset
drflac_uint64 offset
Definition: dr_flac.h:809
drflac_read_f32
DRFLAC_DEPRECATED drflac_uint64 drflac_read_f32(drflac *pFlac, drflac_uint64 samplesToRead, float *pBufferOut)
drflac::totalPCMFrameCount
drflac_uint64 totalPCMFrameCount
Definition: dr_flac.h:517
pattern
GLushort pattern
Definition: qgl_win.c:217
drflac_bs::crc16CacheIgnoredBytes
drflac_uint32 crc16CacheIgnoredBytes
Definition: dr_flac.h:415
drflac::onMeta
drflac_meta_proc onMeta
Definition: dr_flac.h:490
DRFLAC_METADATA_BLOCK_TYPE_PICTURE
#define DRFLAC_METADATA_BLOCK_TYPE_PICTURE
Definition: dr_flac.h:192
drflac_read_pcm_frames_s16
drflac_uint64 drflac_read_pcm_frames_s16(drflac *pFlac, drflac_uint64 framesToRead, drflac_int16 *pBufferOut)
drflac_frame_header::frameNumber
drflac_uint32 frameNumber
Definition: dr_flac.h:451
drflac_open_and_decode_s32
DRFLAC_DEPRECATED drflac_int32 * drflac_open_and_decode_s32(drflac_read_proc onRead, drflac_seek_proc onSeek, void *pUserData, unsigned int *channels, unsigned int *sampleRate, drflac_uint64 *totalSampleCount)
drflac_bs::nextL2Line
drflac_uint32 nextL2Line
Definition: dr_flac.h:397
drflac_cuesheet_track::ISRC
char ISRC[12]
Definition: dr_flac.h:811
drflac_container
drflac_container
Definition: dr_flac.h:218
drflac_open_memory_with_metadata
drflac * drflac_open_memory_with_metadata(const void *data, size_t dataSize, drflac_meta_proc onMeta, void *pUserData)
drflac_bs::pUserData
void * pUserData
Definition: dr_flac.h:383
drflac::_oggbs
void * _oggbs
Definition: dr_flac.h:548
drflac::firstFramePos
drflac_uint64 firstFramePos
Definition: dr_flac.h:534
drflac::pSeekpoints
drflac_seekpoint * pSeekpoints
Definition: dr_flac.h:545
drflac_meta_proc
void(* drflac_meta_proc)(void *pUserData, drflac_metadata *pMetadata)
Definition: dr_flac.h:362
void
void(APIENTRY *qglAccum)(GLenum op
count
GLint GLsizei count
Definition: qgl_win.c:128
drflac_metadata
Definition: dr_flac.h:254
drflac_open_and_decode_file_s32
DRFLAC_DEPRECATED drflac_int32 * drflac_open_and_decode_file_s32(const char *filename, unsigned int *channels, unsigned int *sampleRate, drflac_uint64 *totalSampleCount)
drflac_bs::consumedBits
drflac_uint32 consumedBits
Definition: dr_flac.h:400
drflac_metadata::unused
int unused
Definition: dr_flac.h:275
drflac_metadata::commentCount
drflac_uint32 commentCount
Definition: dr_flac.h:295