2 FLAC audio decoder. Choice of public domain or MIT-0. See license statements at the end of this file.
3 dr_flac - v0.12.31 - 2021-08-16
5 David Reid - mackron@gmail.com
7 GitHub: https://github.com/mackron/dr_libs
11 RELEASE NOTES - v0.12.0
12 =======================
13 Version 0.12.0 has breaking API changes including changes to the existing API and the removal of deprecated APIs.
16 Improved Client-Defined Memory Allocation
17 -----------------------------------------
18 The main change with this release is the addition of a more flexible way of implementing custom memory allocation routines. The
19 existing system of DRFLAC_MALLOC, DRFLAC_REALLOC and DRFLAC_FREE are still in place and will be used by default when no custom
20 allocation callbacks are specified.
22 To use the new system, you pass in a pointer to a drflac_allocation_callbacks object to drflac_open() and family, like this:
24 void* my_malloc(size_t sz, void* pUserData)
28 void* my_realloc(void* p, size_t sz, void* pUserData)
30 return realloc(p, sz);
32 void my_free(void* p, void* pUserData)
39 drflac_allocation_callbacks allocationCallbacks;
40 allocationCallbacks.pUserData = &myData;
41 allocationCallbacks.onMalloc = my_malloc;
42 allocationCallbacks.onRealloc = my_realloc;
43 allocationCallbacks.onFree = my_free;
44 drflac* pFlac = drflac_open_file("my_file.flac", &allocationCallbacks);
46 The advantage of this new system is that it allows you to specify user data which will be passed in to the allocation routines.
48 Passing in null for the allocation callbacks object will cause dr_flac to use defaults which is the same as DRFLAC_MALLOC,
49 DRFLAC_REALLOC and DRFLAC_FREE and the equivalent of how it worked in previous versions.
51 Every API that opens a drflac object now takes this extra parameter. These include the following:
55 drflac_open_with_metadata()
56 drflac_open_with_metadata_relaxed()
58 drflac_open_file_with_metadata()
60 drflac_open_memory_with_metadata()
61 drflac_open_and_read_pcm_frames_s32()
62 drflac_open_and_read_pcm_frames_s16()
63 drflac_open_and_read_pcm_frames_f32()
64 drflac_open_file_and_read_pcm_frames_s32()
65 drflac_open_file_and_read_pcm_frames_s16()
66 drflac_open_file_and_read_pcm_frames_f32()
67 drflac_open_memory_and_read_pcm_frames_s32()
68 drflac_open_memory_and_read_pcm_frames_s16()
69 drflac_open_memory_and_read_pcm_frames_f32()
75 Seeking performance has been greatly improved. A new binary search based seeking algorithm has been introduced which significantly
76 improves performance over the brute force method which was used when no seek table was present. Seek table based seeking also takes
77 advantage of the new binary search seeking system to further improve performance there as well. Note that this depends on CRC which
78 means it will be disabled when DR_FLAC_NO_CRC is used.
80 The SSE4.1 pipeline has been cleaned up and optimized. You should see some improvements with decoding speed of 24-bit files in
81 particular. 16-bit streams should also see some improvement.
83 drflac_read_pcm_frames_s16() has been optimized. Previously this sat on top of drflac_read_pcm_frames_s32() and performed it's s32
84 to s16 conversion in a second pass. This is now all done in a single pass. This includes SSE2 and ARM NEON optimized paths.
86 A minor optimization has been implemented for drflac_read_pcm_frames_s32(). This will now use an SSE2 optimized pipeline for stereo
87 channel reconstruction which is the last part of the decoding process.
89 The ARM build has seen a few improvements. The CLZ (count leading zeroes) and REV (byte swap) instructions are now used when
90 compiling with GCC and Clang which is achieved using inline assembly. The CLZ instruction requires ARM architecture version 5 at
91 compile time and the REV instruction requires ARM architecture version 6.
93 An ARM NEON optimized pipeline has been implemented. To enable this you'll need to add -mfpu=neon to the command line when compiling.
98 The following APIs were deprecated in version 0.11.0 and have been completely removed in version 0.12.0:
100 drflac_read_s32() -> drflac_read_pcm_frames_s32()
101 drflac_read_s16() -> drflac_read_pcm_frames_s16()
102 drflac_read_f32() -> drflac_read_pcm_frames_f32()
103 drflac_seek_to_sample() -> drflac_seek_to_pcm_frame()
104 drflac_open_and_decode_s32() -> drflac_open_and_read_pcm_frames_s32()
105 drflac_open_and_decode_s16() -> drflac_open_and_read_pcm_frames_s16()
106 drflac_open_and_decode_f32() -> drflac_open_and_read_pcm_frames_f32()
107 drflac_open_and_decode_file_s32() -> drflac_open_file_and_read_pcm_frames_s32()
108 drflac_open_and_decode_file_s16() -> drflac_open_file_and_read_pcm_frames_s16()
109 drflac_open_and_decode_file_f32() -> drflac_open_file_and_read_pcm_frames_f32()
110 drflac_open_and_decode_memory_s32() -> drflac_open_memory_and_read_pcm_frames_s32()
111 drflac_open_and_decode_memory_s16() -> drflac_open_memory_and_read_pcm_frames_s16()
112 drflac_open_and_decode_memory_f32() -> drflac_open_memroy_and_read_pcm_frames_f32()
114 Prior versions of dr_flac operated on a per-sample basis whereas now it operates on PCM frames. The removed APIs all relate
115 to the old per-sample APIs. You now need to use the "pcm_frame" versions.
122 dr_flac is a single file library. To use it, do something like the following in one .c file.
125 #define DR_FLAC_IMPLEMENTATION
129 You can then #include this file in other parts of the program as you would with any other header file. To decode audio data, do something like the following:
132 drflac* pFlac = drflac_open_file("MySong.flac", NULL);
134 // Failed to open FLAC file
137 drflac_int32* pSamples = malloc(pFlac->totalPCMFrameCount * pFlac->channels * sizeof(drflac_int32));
138 drflac_uint64 numberOfInterleavedSamplesActuallyRead = drflac_read_pcm_frames_s32(pFlac, pFlac->totalPCMFrameCount, pSamples);
141 The drflac object represents the decoder. It is a transparent type so all the information you need, such as the number of channels and the bits per sample,
142 should be directly accessible - just make sure you don't change their values. Samples are always output as interleaved signed 32-bit PCM. In the example above
143 a native FLAC stream was opened, however dr_flac has seamless support for Ogg encapsulated FLAC streams as well.
145 You do not need to decode the entire stream in one go - you just specify how many samples you'd like at any given time and the decoder will give you as many
146 samples as it can, up to the amount requested. Later on when you need the next batch of samples, just call it again. Example:
149 while (drflac_read_pcm_frames_s32(pFlac, chunkSizeInPCMFrames, pChunkSamples) > 0) {
154 You can seek to a specific PCM frame with `drflac_seek_to_pcm_frame()`.
156 If you just want to quickly decode an entire FLAC file in one go you can do something like this:
159 unsigned int channels;
160 unsigned int sampleRate;
161 drflac_uint64 totalPCMFrameCount;
162 drflac_int32* pSampleData = drflac_open_file_and_read_pcm_frames_s32("MySong.flac", &channels, &sampleRate, &totalPCMFrameCount, NULL);
163 if (pSampleData == NULL) {
164 // Failed to open and decode FLAC file.
169 drflac_free(pSampleData, NULL);
172 You can read samples as signed 16-bit integer and 32-bit floating-point PCM with the *_s16() and *_f32() family of APIs respectively, but note that these
173 should be considered lossy.
176 If you need access to metadata (album art, etc.), use `drflac_open_with_metadata()`, `drflac_open_file_with_metdata()` or `drflac_open_memory_with_metadata()`.
177 The rationale for keeping these APIs separate is that they're slightly slower than the normal versions and also just a little bit harder to use. dr_flac
178 reports metadata to the application through the use of a callback, and every metadata block is reported before `drflac_open_with_metdata()` returns.
180 The main opening APIs (`drflac_open()`, etc.) will fail if the header is not present. The presents a problem in certain scenarios such as broadcast style
181 streams or internet radio where the header may not be present because the user has started playback mid-stream. To handle this, use the relaxed APIs:
183 `drflac_open_relaxed()`
184 `drflac_open_with_metadata_relaxed()`
186 It is not recommended to use these APIs for file based streams because a missing header would usually indicate a corrupt or perverse file. In addition, these
187 APIs can take a long time to initialize because they may need to spend a lot of time finding the first frame.
193 #define these options before including this file.
195 #define DR_FLAC_NO_STDIO
196 Disable `drflac_open_file()` and family.
198 #define DR_FLAC_NO_OGG
199 Disables support for Ogg/FLAC streams.
201 #define DR_FLAC_BUFFER_SIZE <number>
202 Defines the size of the internal buffer to store data from onRead(). This buffer is used to reduce the number of calls back to the client for more data.
203 Larger values means more memory, but better performance. My tests show diminishing returns after about 4KB (which is the default). Consider reducing this if
204 you have a very efficient implementation of onRead(), or increase it if it's very inefficient. Must be a multiple of 8.
206 #define DR_FLAC_NO_CRC
207 Disables CRC checks. This will offer a performance boost when CRC is unnecessary. This will disable binary search seeking. When seeking, the seek table will
208 be used if available. Otherwise the seek will be performed using brute force.
210 #define DR_FLAC_NO_SIMD
211 Disables SIMD optimizations (SSE on x86/x64 architectures, NEON on ARM architectures). Use this if you are having compatibility issues with your compiler.
217 - dr_flac does not support changing the sample rate nor channel count mid stream.
218 - dr_flac is not thread-safe, but its APIs can be called from any thread so long as you do your own synchronization.
219 - When using Ogg encapsulation, a corrupted metadata block will result in `drflac_open_with_metadata()` and `drflac_open()` returning inconsistent samples due
220 to differences in corrupted stream recorvery logic between the two APIs.
230 #define DRFLAC_STRINGIFY(x) #x
231 #define DRFLAC_XSTRINGIFY(x) DRFLAC_STRINGIFY(x)
233 #define DRFLAC_VERSION_MAJOR 0
234 #define DRFLAC_VERSION_MINOR 12
235 #define DRFLAC_VERSION_REVISION 31
236 #define DRFLAC_VERSION_STRING DRFLAC_XSTRINGIFY(DRFLAC_VERSION_MAJOR) "." DRFLAC_XSTRINGIFY(DRFLAC_VERSION_MINOR) "." DRFLAC_XSTRINGIFY(DRFLAC_VERSION_REVISION)
238 #include <stddef.h> /* For size_t. */
241 typedef signed char drflac_int8
;
242 typedef unsigned char drflac_uint8
;
243 typedef signed short drflac_int16
;
244 typedef unsigned short drflac_uint16
;
245 typedef signed int drflac_int32
;
246 typedef unsigned int drflac_uint32
;
247 #if defined(_MSC_VER)
248 typedef signed __int64 drflac_int64
;
249 typedef unsigned __int64 drflac_uint64
;
251 #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
252 #pragma GCC diagnostic push
253 #pragma GCC diagnostic ignored "-Wlong-long"
254 #if defined(__clang__)
255 #pragma GCC diagnostic ignored "-Wc++11-long-long"
258 typedef signed long long drflac_int64
;
259 typedef unsigned long long drflac_uint64
;
260 #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
261 #pragma GCC diagnostic pop
264 #if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__)) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(_M_ARM64) || defined(__powerpc64__)
265 typedef drflac_uint64 drflac_uintptr
;
267 typedef drflac_uint32 drflac_uintptr
;
269 typedef drflac_uint8 drflac_bool8
;
270 typedef drflac_uint32 drflac_bool32
;
271 #define DRFLAC_TRUE 1
272 #define DRFLAC_FALSE 0
274 #if !defined(DRFLAC_API)
275 #if defined(DRFLAC_DLL)
277 #define DRFLAC_DLL_IMPORT __declspec(dllimport)
278 #define DRFLAC_DLL_EXPORT __declspec(dllexport)
279 #define DRFLAC_DLL_PRIVATE static
281 #if defined(__GNUC__) && __GNUC__ >= 4
282 #define DRFLAC_DLL_IMPORT __attribute__((visibility("default")))
283 #define DRFLAC_DLL_EXPORT __attribute__((visibility("default")))
284 #define DRFLAC_DLL_PRIVATE __attribute__((visibility("hidden")))
286 #define DRFLAC_DLL_IMPORT
287 #define DRFLAC_DLL_EXPORT
288 #define DRFLAC_DLL_PRIVATE static
292 #if defined(DR_FLAC_IMPLEMENTATION) || defined(DRFLAC_IMPLEMENTATION)
293 #define DRFLAC_API DRFLAC_DLL_EXPORT
295 #define DRFLAC_API DRFLAC_DLL_IMPORT
297 #define DRFLAC_PRIVATE DRFLAC_DLL_PRIVATE
299 #define DRFLAC_API extern
300 #define DRFLAC_PRIVATE static
304 #if defined(_MSC_VER) && _MSC_VER >= 1700 /* Visual Studio 2012 */
305 #define DRFLAC_DEPRECATED __declspec(deprecated)
306 #elif (defined(__GNUC__) && __GNUC__ >= 4) /* GCC 4 */
307 #define DRFLAC_DEPRECATED __attribute__((deprecated))
308 #elif defined(__has_feature) /* Clang */
309 #if __has_feature(attribute_deprecated)
310 #define DRFLAC_DEPRECATED __attribute__((deprecated))
312 #define DRFLAC_DEPRECATED
315 #define DRFLAC_DEPRECATED
318 DRFLAC_API
void drflac_version(drflac_uint32
* pMajor
, drflac_uint32
* pMinor
, drflac_uint32
* pRevision
);
319 DRFLAC_API
const char* drflac_version_string(void);
322 As data is read from the client it is placed into an internal buffer for fast access. This controls the size of that buffer. Larger values means more speed,
323 but also more memory. In my testing there is diminishing returns after about 4KB, but you can fiddle with this to suit your own needs. Must be a multiple of 8.
325 #ifndef DR_FLAC_BUFFER_SIZE
326 #define DR_FLAC_BUFFER_SIZE 4096
329 /* Check if we can enable 64-bit optimizations. */
330 #if defined(_WIN64) || defined(_LP64) || defined(__LP64__)
335 typedef drflac_uint64 drflac_cache_t
;
337 typedef drflac_uint32 drflac_cache_t
;
340 /* The various metadata block types. */
341 #define DRFLAC_METADATA_BLOCK_TYPE_STREAMINFO 0
342 #define DRFLAC_METADATA_BLOCK_TYPE_PADDING 1
343 #define DRFLAC_METADATA_BLOCK_TYPE_APPLICATION 2
344 #define DRFLAC_METADATA_BLOCK_TYPE_SEEKTABLE 3
345 #define DRFLAC_METADATA_BLOCK_TYPE_VORBIS_COMMENT 4
346 #define DRFLAC_METADATA_BLOCK_TYPE_CUESHEET 5
347 #define DRFLAC_METADATA_BLOCK_TYPE_PICTURE 6
348 #define DRFLAC_METADATA_BLOCK_TYPE_INVALID 127
350 /* The various picture types specified in the PICTURE block. */
351 #define DRFLAC_PICTURE_TYPE_OTHER 0
352 #define DRFLAC_PICTURE_TYPE_FILE_ICON 1
353 #define DRFLAC_PICTURE_TYPE_OTHER_FILE_ICON 2
354 #define DRFLAC_PICTURE_TYPE_COVER_FRONT 3
355 #define DRFLAC_PICTURE_TYPE_COVER_BACK 4
356 #define DRFLAC_PICTURE_TYPE_LEAFLET_PAGE 5
357 #define DRFLAC_PICTURE_TYPE_MEDIA 6
358 #define DRFLAC_PICTURE_TYPE_LEAD_ARTIST 7
359 #define DRFLAC_PICTURE_TYPE_ARTIST 8
360 #define DRFLAC_PICTURE_TYPE_CONDUCTOR 9
361 #define DRFLAC_PICTURE_TYPE_BAND 10
362 #define DRFLAC_PICTURE_TYPE_COMPOSER 11
363 #define DRFLAC_PICTURE_TYPE_LYRICIST 12
364 #define DRFLAC_PICTURE_TYPE_RECORDING_LOCATION 13
365 #define DRFLAC_PICTURE_TYPE_DURING_RECORDING 14
366 #define DRFLAC_PICTURE_TYPE_DURING_PERFORMANCE 15
367 #define DRFLAC_PICTURE_TYPE_SCREEN_CAPTURE 16
368 #define DRFLAC_PICTURE_TYPE_BRIGHT_COLORED_FISH 17
369 #define DRFLAC_PICTURE_TYPE_ILLUSTRATION 18
370 #define DRFLAC_PICTURE_TYPE_BAND_LOGOTYPE 19
371 #define DRFLAC_PICTURE_TYPE_PUBLISHER_LOGOTYPE 20
375 drflac_container_native
,
376 drflac_container_ogg
,
377 drflac_container_unknown
382 drflac_seek_origin_start
,
383 drflac_seek_origin_current
384 } drflac_seek_origin
;
386 /* Packing is important on this structure because we map this directly to the raw data within the SEEKTABLE metadata block. */
390 drflac_uint64 firstPCMFrame
;
391 drflac_uint64 flacFrameOffset
; /* The offset from the first byte of the header of the first frame. */
392 drflac_uint16 pcmFrameCount
;
398 drflac_uint16 minBlockSizeInPCMFrames
;
399 drflac_uint16 maxBlockSizeInPCMFrames
;
400 drflac_uint32 minFrameSizeInPCMFrames
;
401 drflac_uint32 maxFrameSizeInPCMFrames
;
402 drflac_uint32 sampleRate
;
403 drflac_uint8 channels
;
404 drflac_uint8 bitsPerSample
;
405 drflac_uint64 totalPCMFrameCount
;
406 drflac_uint8 md5
[16];
412 The metadata type. Use this to know how to interpret the data below. Will be set to one of the
413 DRFLAC_METADATA_BLOCK_TYPE_* tokens.
418 A pointer to the raw data. This points to a temporary buffer so don't hold on to it. It's best to
419 not modify the contents of this buffer. Use the structures below for more meaningful and structured
420 information about the metadata. It's possible for this to be null.
422 const void* pRawData
;
424 /* The size in bytes of the block and the buffer pointed to by pRawData if it's non-NULL. */
425 drflac_uint32 rawDataSize
;
429 drflac_streaminfo streaminfo
;
440 drflac_uint32 dataSize
;
445 drflac_uint32 seekpointCount
;
446 const drflac_seekpoint
* pSeekpoints
;
451 drflac_uint32 vendorLength
;
453 drflac_uint32 commentCount
;
454 const void* pComments
;
460 drflac_uint64 leadInSampleCount
;
462 drflac_uint8 trackCount
;
463 const void* pTrackData
;
469 drflac_uint32 mimeLength
;
471 drflac_uint32 descriptionLength
;
472 const char* description
;
474 drflac_uint32 height
;
475 drflac_uint32 colorDepth
;
476 drflac_uint32 indexColorCount
;
477 drflac_uint32 pictureDataSize
;
478 const drflac_uint8
* pPictureData
;
485 Callback for when data needs to be read from the client.
491 The user data that was passed to drflac_open() and family.
497 The number of bytes to read.
502 The number of bytes actually read.
507 A return value of less than bytesToRead indicates the end of the stream. Do _not_ return from this callback until either the entire bytesToRead is filled or
508 you have reached the end of the stream.
510 typedef size_t (* drflac_read_proc
)(void* pUserData
, void* pBufferOut
, size_t bytesToRead
);
513 Callback for when data needs to be seeked.
519 The user data that was passed to drflac_open() and family.
522 The number of bytes to move, relative to the origin. Will never be negative.
525 The origin of the seek - the current position or the start of the stream.
530 Whether or not the seek was successful.
535 The offset will never be negative. Whether or not it is relative to the beginning or current position is determined by the "origin" parameter which will be
536 either drflac_seek_origin_start or drflac_seek_origin_current.
538 When seeking to a PCM frame using drflac_seek_to_pcm_frame(), dr_flac may call this with an offset beyond the end of the FLAC stream. This needs to be detected
539 and handled by returning DRFLAC_FALSE.
541 typedef drflac_bool32 (* drflac_seek_proc
)(void* pUserData
, int offset
, drflac_seek_origin origin
);
544 Callback for when a metadata block is read.
550 The user data that was passed to drflac_open() and family.
553 A pointer to a structure containing the data of the metadata block.
558 Use pMetadata->type to determine which metadata block is being handled and how to read the data. This
559 will be set to one of the DRFLAC_METADATA_BLOCK_TYPE_* tokens.
561 typedef void (* drflac_meta_proc
)(void* pUserData
, drflac_metadata
* pMetadata
);
567 void* (* onMalloc
)(size_t sz
, void* pUserData
);
568 void* (* onRealloc
)(void* p
, size_t sz
, void* pUserData
);
569 void (* onFree
)(void* p
, void* pUserData
);
570 } drflac_allocation_callbacks
;
572 /* Structure for internal use. Only used for decoders opened with drflac_open_memory. */
575 const drflac_uint8
* data
;
577 size_t currentReadPos
;
578 } drflac__memory_stream
;
580 /* Structure for internal use. Used for bit streaming. */
583 /* The function to call when more data needs to be read. */
584 drflac_read_proc onRead
;
586 /* The function to call when the current read position needs to be moved. */
587 drflac_seek_proc onSeek
;
589 /* The user data to pass around to onRead and onSeek. */
594 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
595 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
596 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).
598 size_t unalignedByteCount
;
600 /* The content of the unaligned bytes. */
601 drflac_cache_t unalignedCache
;
603 /* The index of the next valid cache line in the "L2" cache. */
604 drflac_uint32 nextL2Line
;
606 /* The number of bits that have been consumed by the cache. This is used to determine how many valid bits are remaining. */
607 drflac_uint32 consumedBits
;
610 The cached data which was most recently read from the client. There are two levels of cache. Data flows as such:
611 Client -> L2 -> L1. The L2 -> L1 movement is aligned and runs on a fast path in just a few instructions.
613 drflac_cache_t cacheL2
[DR_FLAC_BUFFER_SIZE
/sizeof(drflac_cache_t
)];
614 drflac_cache_t cache
;
617 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
618 is reset to 0 at the beginning of each frame.
621 drflac_cache_t crc16Cache
; /* A cache for optimizing CRC calculations. This is filled when when the L1 cache is reloaded. */
622 drflac_uint32 crc16CacheIgnoredBytes
; /* The number of bytes to ignore when updating the CRC-16 from the CRC-16 cache. */
627 /* The type of the subframe: SUBFRAME_CONSTANT, SUBFRAME_VERBATIM, SUBFRAME_FIXED or SUBFRAME_LPC. */
628 drflac_uint8 subframeType
;
630 /* The number of wasted bits per sample as specified by the sub-frame header. */
631 drflac_uint8 wastedBitsPerSample
;
633 /* The order to use for the prediction stage for SUBFRAME_FIXED and SUBFRAME_LPC. */
634 drflac_uint8 lpcOrder
;
636 /* A pointer to the buffer containing the decoded samples in the subframe. This pointer is an offset from drflac::pExtraData. */
637 drflac_int32
* pSamplesS32
;
643 If the stream uses variable block sizes, this will be set to the index of the first PCM frame. If fixed block sizes are used, this will
644 always be set to 0. This is 64-bit because the decoded PCM frame number will be 36 bits.
646 drflac_uint64 pcmFrameNumber
;
649 If the stream uses fixed block sizes, this will be set to the frame number. If variable block sizes are used, this will always be 0. This
650 is 32-bit because in fixed block sizes, the maximum frame number will be 31 bits.
652 drflac_uint32 flacFrameNumber
;
654 /* The sample rate of this frame. */
655 drflac_uint32 sampleRate
;
657 /* The number of PCM frames in each sub-frame within this frame. */
658 drflac_uint16 blockSizeInPCMFrames
;
661 The channel assignment of this frame. This is not always set to the channel count. If interchannel decorrelation is being used this
662 will be set to DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE, DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE or DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE.
664 drflac_uint8 channelAssignment
;
666 /* The number of bits per sample within this frame. */
667 drflac_uint8 bitsPerSample
;
669 /* The frame's CRC. */
671 } drflac_frame_header
;
676 drflac_frame_header header
;
679 The number of PCM frames left to be read in this FLAC frame. This is initially set to the block size. As PCM frames are read,
680 this will be decremented. When it reaches 0, the decoder will see this frame as fully consumed and load the next frame.
682 drflac_uint32 pcmFramesRemaining
;
684 /* The list of sub-frames within the frame. There is one sub-frame for each channel, and there's a maximum of 8 channels. */
685 drflac_subframe subframes
[8];
690 /* The function to call when a metadata block is read. */
691 drflac_meta_proc onMeta
;
693 /* The user data posted to the metadata callback function. */
696 /* Memory allocation callbacks. */
697 drflac_allocation_callbacks allocationCallbacks
;
700 /* The sample rate. Will be set to something like 44100. */
701 drflac_uint32 sampleRate
;
704 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
705 value specified in the STREAMINFO block.
707 drflac_uint8 channels
;
709 /* The bits per sample. Will be set to something like 16, 24, etc. */
710 drflac_uint8 bitsPerSample
;
712 /* The maximum block size, in samples. This number represents the number of samples in each channel (not combined). */
713 drflac_uint16 maxBlockSizeInPCMFrames
;
716 The total number of PCM Frames making up the stream. Can be 0 in which case it's still a valid stream, but just means
717 the total PCM frame count is unknown. Likely the case with streams like internet radio.
719 drflac_uint64 totalPCMFrameCount
;
722 /* The container type. This is set based on whether or not the decoder was opened from a native or Ogg stream. */
723 drflac_container container
;
725 /* The number of seekpoints in the seektable. */
726 drflac_uint32 seekpointCount
;
729 /* Information about the frame the decoder is currently sitting on. */
730 drflac_frame currentFLACFrame
;
733 /* The index of the PCM frame the decoder is currently sitting on. This is only used for seeking. */
734 drflac_uint64 currentPCMFrame
;
736 /* The position of the first FLAC frame in the stream. This is only ever used for seeking. */
737 drflac_uint64 firstFLACFramePosInBytes
;
740 /* A hack to avoid a malloc() when opening a decoder with drflac_open_memory(). */
741 drflac__memory_stream memoryStream
;
744 /* A pointer to the decoded sample data. This is an offset of pExtraData. */
745 drflac_int32
* pDecodedSamples
;
747 /* A pointer to the seek table. This is an offset of pExtraData, or NULL if there is no seek table. */
748 drflac_seekpoint
* pSeekpoints
;
750 /* Internal use only. Only used with Ogg containers. Points to a drflac_oggbs object. This is an offset of pExtraData. */
753 /* Internal use only. Used for profiling and testing different seeking modes. */
754 drflac_bool32 _noSeekTableSeek
: 1;
755 drflac_bool32 _noBinarySearchSeek
: 1;
756 drflac_bool32 _noBruteForceSeek
: 1;
758 /* The bit streamer. The raw FLAC data is fed through this object. */
761 /* Variable length extra data. We attach this to the end of the object so we can avoid unnecessary mallocs. */
762 drflac_uint8 pExtraData
[1];
767 Opens a FLAC decoder.
773 The function to call when data needs to be read from the client.
776 The function to call when the read position of the client data needs to move.
778 pUserData (in, optional)
779 A pointer to application defined data that will be passed to onRead and onSeek.
781 pAllocationCallbacks (in, optional)
782 A pointer to application defined callbacks for managing memory allocations.
787 Returns a pointer to an object representing the decoder.
792 Close the decoder with `drflac_close()`.
794 `pAllocationCallbacks` can be NULL in which case it will use `DRFLAC_MALLOC`, `DRFLAC_REALLOC` and `DRFLAC_FREE`.
796 This function will automatically detect whether or not you are attempting to open a native or Ogg encapsulated FLAC, both of which should work seamlessly
797 without any manual intervention. Ogg encapsulation also works with multiplexed streams which basically means it can play FLAC encoded audio tracks in videos.
799 This is the lowest level function for opening a FLAC stream. You can also use `drflac_open_file()` and `drflac_open_memory()` to open the stream from a file or
800 from a block of memory respectively.
802 The STREAMINFO block must be present for this to succeed. Use `drflac_open_relaxed()` to open a FLAC stream where the header may not be present.
804 Use `drflac_open_with_metadata()` if you need access to metadata.
811 drflac_open_with_metadata()
814 DRFLAC_API drflac
* drflac_open(drflac_read_proc onRead
, drflac_seek_proc onSeek
, void* pUserData
, const drflac_allocation_callbacks
* pAllocationCallbacks
);
817 Opens a FLAC stream with relaxed validation of the header block.
823 The function to call when data needs to be read from the client.
826 The function to call when the read position of the client data needs to move.
829 Whether or not the FLAC stream is encapsulated using standard FLAC encapsulation or Ogg encapsulation.
831 pUserData (in, optional)
832 A pointer to application defined data that will be passed to onRead and onSeek.
834 pAllocationCallbacks (in, optional)
835 A pointer to application defined callbacks for managing memory allocations.
840 A pointer to an object representing the decoder.
845 The same as drflac_open(), except attempts to open the stream even when a header block is not present.
847 Because the header is not necessarily available, the caller must explicitly define the container (Native or Ogg). Do not set this to `drflac_container_unknown`
848 as that is for internal use only.
850 Opening in relaxed mode will continue reading data from onRead until it finds a valid frame. If a frame is never found it will continue forever. To abort,
851 force your `onRead` callback to return 0, which dr_flac will use as an indicator that the end of the stream was found.
853 Use `drflac_open_with_metadata_relaxed()` if you need access to metadata.
855 DRFLAC_API drflac
* drflac_open_relaxed(drflac_read_proc onRead
, drflac_seek_proc onSeek
, drflac_container container
, void* pUserData
, const drflac_allocation_callbacks
* pAllocationCallbacks
);
858 Opens a FLAC decoder and notifies the caller of the metadata chunks (album art, etc.).
864 The function to call when data needs to be read from the client.
867 The function to call when the read position of the client data needs to move.
870 The function to call for every metadata block.
872 pUserData (in, optional)
873 A pointer to application defined data that will be passed to onRead, onSeek and onMeta.
875 pAllocationCallbacks (in, optional)
876 A pointer to application defined callbacks for managing memory allocations.
881 A pointer to an object representing the decoder.
886 Close the decoder with `drflac_close()`.
888 `pAllocationCallbacks` can be NULL in which case it will use `DRFLAC_MALLOC`, `DRFLAC_REALLOC` and `DRFLAC_FREE`.
890 This is slower than `drflac_open()`, so avoid this one if you don't need metadata. Internally, this will allocate and free memory on the heap for every
891 metadata block except for STREAMINFO and PADDING blocks.
893 The caller is notified of the metadata via the `onMeta` callback. All metadata blocks will be handled before the function returns. This callback takes a
894 pointer to a `drflac_metadata` object which is a union containing the data of all relevant metadata blocks. Use the `type` member to discriminate against
895 the different metadata types.
897 The STREAMINFO block must be present for this to succeed. Use `drflac_open_with_metadata_relaxed()` to open a FLAC stream where the header may not be present.
899 Note that this will behave inconsistently with `drflac_open()` if the stream is an Ogg encapsulated stream and a metadata block is corrupted. This is due to
900 the way the Ogg stream recovers from corrupted pages. When `drflac_open_with_metadata()` is being used, the open routine will try to read the contents of the
901 metadata block, whereas `drflac_open()` will simply seek past it (for the sake of efficiency). This inconsistency can result in different samples being
902 returned depending on whether or not the stream is being opened with metadata.
907 drflac_open_file_with_metadata()
908 drflac_open_memory_with_metadata()
912 DRFLAC_API drflac
* drflac_open_with_metadata(drflac_read_proc onRead
, drflac_seek_proc onSeek
, drflac_meta_proc onMeta
, void* pUserData
, const drflac_allocation_callbacks
* pAllocationCallbacks
);
915 The same as drflac_open_with_metadata(), except attempts to open the stream even when a header block is not present.
919 drflac_open_with_metadata()
920 drflac_open_relaxed()
922 DRFLAC_API drflac
* drflac_open_with_metadata_relaxed(drflac_read_proc onRead
, drflac_seek_proc onSeek
, drflac_meta_proc onMeta
, drflac_container container
, void* pUserData
, const drflac_allocation_callbacks
* pAllocationCallbacks
);
925 Closes the given FLAC decoder.
931 The decoder to close.
936 This will destroy the decoder object.
942 drflac_open_with_metadata()
945 drflac_open_file_with_metadata()
946 drflac_open_file_with_metadata_w()
948 drflac_open_memory_with_metadata()
950 DRFLAC_API
void drflac_close(drflac
* pFlac
);
954 Reads sample data from the given FLAC decoder, output as interleaved signed 32-bit PCM.
963 The number of PCM frames to read.
965 pBufferOut (out, optional)
966 A pointer to the buffer that will receive the decoded samples.
971 Returns the number of PCM frames actually read. If the return value is less than `framesToRead` it has reached the end.
976 pBufferOut can be null, in which case the call will act as a seek, and the return value will be the number of frames seeked.
978 DRFLAC_API drflac_uint64
drflac_read_pcm_frames_s32(drflac
* pFlac
, drflac_uint64 framesToRead
, drflac_int32
* pBufferOut
);
982 Reads sample data from the given FLAC decoder, output as interleaved signed 16-bit PCM.
991 The number of PCM frames to read.
993 pBufferOut (out, optional)
994 A pointer to the buffer that will receive the decoded samples.
999 Returns the number of PCM frames actually read. If the return value is less than `framesToRead` it has reached the end.
1004 pBufferOut can be null, in which case the call will act as a seek, and the return value will be the number of frames seeked.
1006 Note that this is lossy for streams where the bits per sample is larger than 16.
1008 DRFLAC_API drflac_uint64
drflac_read_pcm_frames_s16(drflac
* pFlac
, drflac_uint64 framesToRead
, drflac_int16
* pBufferOut
);
1011 Reads sample data from the given FLAC decoder, output as interleaved 32-bit floating point PCM.
1020 The number of PCM frames to read.
1022 pBufferOut (out, optional)
1023 A pointer to the buffer that will receive the decoded samples.
1028 Returns the number of PCM frames actually read. If the return value is less than `framesToRead` it has reached the end.
1033 pBufferOut can be null, in which case the call will act as a seek, and the return value will be the number of frames seeked.
1035 Note that this should be considered lossy due to the nature of floating point numbers not being able to exactly represent every possible number.
1037 DRFLAC_API drflac_uint64
drflac_read_pcm_frames_f32(drflac
* pFlac
, drflac_uint64 framesToRead
, float* pBufferOut
);
1040 Seeks to the PCM frame at the given index.
1049 The index of the PCM frame to seek to. See notes below.
1054 `DRFLAC_TRUE` if successful; `DRFLAC_FALSE` otherwise.
1056 DRFLAC_API drflac_bool32
drflac_seek_to_pcm_frame(drflac
* pFlac
, drflac_uint64 pcmFrameIndex
);
1060 #ifndef DR_FLAC_NO_STDIO
1062 Opens a FLAC decoder from the file at the given path.
1068 The path of the file to open, either absolute or relative to the current directory.
1070 pAllocationCallbacks (in, optional)
1071 A pointer to application defined callbacks for managing memory allocations.
1076 A pointer to an object representing the decoder.
1081 Close the decoder with drflac_close().
1086 This will hold a handle to the file until the decoder is closed with drflac_close(). Some platforms will restrict the number of files a process can have open
1087 at any given time, so keep this mind if you have many decoders open at the same time.
1092 drflac_open_file_with_metadata()
1096 DRFLAC_API drflac
* drflac_open_file(const char* pFileName
, const drflac_allocation_callbacks
* pAllocationCallbacks
);
1097 DRFLAC_API drflac
* drflac_open_file_w(const wchar_t* pFileName
, const drflac_allocation_callbacks
* pAllocationCallbacks
);
1100 Opens a FLAC decoder from the file at the given path and notifies the caller of the metadata chunks (album art, etc.)
1106 The path of the file to open, either absolute or relative to the current directory.
1108 pAllocationCallbacks (in, optional)
1109 A pointer to application defined callbacks for managing memory allocations.
1112 The callback to fire for each metadata block.
1115 A pointer to the user data to pass to the metadata callback.
1117 pAllocationCallbacks (in)
1118 A pointer to application defined callbacks for managing memory allocations.
1123 Look at the documentation for drflac_open_with_metadata() for more information on how metadata is handled.
1128 drflac_open_with_metadata()
1132 DRFLAC_API drflac
* drflac_open_file_with_metadata(const char* pFileName
, drflac_meta_proc onMeta
, void* pUserData
, const drflac_allocation_callbacks
* pAllocationCallbacks
);
1133 DRFLAC_API drflac
* drflac_open_file_with_metadata_w(const wchar_t* pFileName
, drflac_meta_proc onMeta
, void* pUserData
, const drflac_allocation_callbacks
* pAllocationCallbacks
);
1137 Opens a FLAC decoder from a pre-allocated block of memory
1143 A pointer to the raw encoded FLAC data.
1146 The size in bytes of `data`.
1148 pAllocationCallbacks (in)
1149 A pointer to application defined callbacks for managing memory allocations.
1154 A pointer to an object representing the decoder.
1159 This does not create a copy of the data. It is up to the application to ensure the buffer remains valid for the lifetime of the decoder.
1167 DRFLAC_API drflac
* drflac_open_memory(const void* pData
, size_t dataSize
, const drflac_allocation_callbacks
* pAllocationCallbacks
);
1170 Opens a FLAC decoder from a pre-allocated block of memory and notifies the caller of the metadata chunks (album art, etc.)
1176 A pointer to the raw encoded FLAC data.
1179 The size in bytes of `data`.
1182 The callback to fire for each metadata block.
1185 A pointer to the user data to pass to the metadata callback.
1187 pAllocationCallbacks (in)
1188 A pointer to application defined callbacks for managing memory allocations.
1193 Look at the documentation for drflac_open_with_metadata() for more information on how metadata is handled.
1198 drflac_open_with_metadata()
1202 DRFLAC_API drflac
* drflac_open_memory_with_metadata(const void* pData
, size_t dataSize
, drflac_meta_proc onMeta
, void* pUserData
, const drflac_allocation_callbacks
* pAllocationCallbacks
);
1206 /* High Level APIs */
1209 Opens a FLAC stream from the given callbacks and fully decodes it in a single operation. The return value is a
1210 pointer to the sample data as interleaved signed 32-bit PCM. The returned data must be freed with drflac_free().
1212 You can pass in custom memory allocation callbacks via the pAllocationCallbacks parameter. This can be NULL in which
1213 case it will use DRFLAC_MALLOC, DRFLAC_REALLOC and DRFLAC_FREE.
1215 Sometimes a FLAC file won't keep track of the total sample count. In this situation the function will continuously
1216 read samples into a dynamically sized buffer on the heap until no samples are left.
1218 Do not call this function on a broadcast type of stream (like internet radio streams and whatnot).
1220 DRFLAC_API drflac_int32
* drflac_open_and_read_pcm_frames_s32(drflac_read_proc onRead
, drflac_seek_proc onSeek
, void* pUserData
, unsigned int* channels
, unsigned int* sampleRate
, drflac_uint64
* totalPCMFrameCount
, const drflac_allocation_callbacks
* pAllocationCallbacks
);
1222 /* Same as drflac_open_and_read_pcm_frames_s32(), except returns signed 16-bit integer samples. */
1223 DRFLAC_API drflac_int16
* drflac_open_and_read_pcm_frames_s16(drflac_read_proc onRead
, drflac_seek_proc onSeek
, void* pUserData
, unsigned int* channels
, unsigned int* sampleRate
, drflac_uint64
* totalPCMFrameCount
, const drflac_allocation_callbacks
* pAllocationCallbacks
);
1225 /* Same as drflac_open_and_read_pcm_frames_s32(), except returns 32-bit floating-point samples. */
1226 DRFLAC_API
float* drflac_open_and_read_pcm_frames_f32(drflac_read_proc onRead
, drflac_seek_proc onSeek
, void* pUserData
, unsigned int* channels
, unsigned int* sampleRate
, drflac_uint64
* totalPCMFrameCount
, const drflac_allocation_callbacks
* pAllocationCallbacks
);
1228 #ifndef DR_FLAC_NO_STDIO
1229 /* Same as drflac_open_and_read_pcm_frames_s32() except opens the decoder from a file. */
1230 DRFLAC_API drflac_int32
* drflac_open_file_and_read_pcm_frames_s32(const char* filename
, unsigned int* channels
, unsigned int* sampleRate
, drflac_uint64
* totalPCMFrameCount
, const drflac_allocation_callbacks
* pAllocationCallbacks
);
1232 /* Same as drflac_open_file_and_read_pcm_frames_s32(), except returns signed 16-bit integer samples. */
1233 DRFLAC_API drflac_int16
* drflac_open_file_and_read_pcm_frames_s16(const char* filename
, unsigned int* channels
, unsigned int* sampleRate
, drflac_uint64
* totalPCMFrameCount
, const drflac_allocation_callbacks
* pAllocationCallbacks
);
1235 /* Same as drflac_open_file_and_read_pcm_frames_s32(), except returns 32-bit floating-point samples. */
1236 DRFLAC_API
float* drflac_open_file_and_read_pcm_frames_f32(const char* filename
, unsigned int* channels
, unsigned int* sampleRate
, drflac_uint64
* totalPCMFrameCount
, const drflac_allocation_callbacks
* pAllocationCallbacks
);
1239 /* Same as drflac_open_and_read_pcm_frames_s32() except opens the decoder from a block of memory. */
1240 DRFLAC_API drflac_int32
* drflac_open_memory_and_read_pcm_frames_s32(const void* data
, size_t dataSize
, unsigned int* channels
, unsigned int* sampleRate
, drflac_uint64
* totalPCMFrameCount
, const drflac_allocation_callbacks
* pAllocationCallbacks
);
1242 /* Same as drflac_open_memory_and_read_pcm_frames_s32(), except returns signed 16-bit integer samples. */
1243 DRFLAC_API drflac_int16
* drflac_open_memory_and_read_pcm_frames_s16(const void* data
, size_t dataSize
, unsigned int* channels
, unsigned int* sampleRate
, drflac_uint64
* totalPCMFrameCount
, const drflac_allocation_callbacks
* pAllocationCallbacks
);
1245 /* Same as drflac_open_memory_and_read_pcm_frames_s32(), except returns 32-bit floating-point samples. */
1246 DRFLAC_API
float* drflac_open_memory_and_read_pcm_frames_f32(const void* data
, size_t dataSize
, unsigned int* channels
, unsigned int* sampleRate
, drflac_uint64
* totalPCMFrameCount
, const drflac_allocation_callbacks
* pAllocationCallbacks
);
1249 Frees memory that was allocated internally by dr_flac.
1251 Set pAllocationCallbacks to the same object that was passed to drflac_open_*_and_read_pcm_frames_*(). If you originally passed in NULL, pass in NULL for this.
1253 DRFLAC_API
void drflac_free(void* p
, const drflac_allocation_callbacks
* pAllocationCallbacks
);
1256 /* Structure representing an iterator for vorbis comments in a VORBIS_COMMENT metadata block. */
1259 drflac_uint32 countRemaining
;
1260 const char* pRunningData
;
1261 } drflac_vorbis_comment_iterator
;
1264 Initializes a vorbis comment iterator. This can be used for iterating over the vorbis comments in a VORBIS_COMMENT
1267 DRFLAC_API
void drflac_init_vorbis_comment_iterator(drflac_vorbis_comment_iterator
* pIter
, drflac_uint32 commentCount
, const void* pComments
);
1270 Goes to the next vorbis comment in the given iterator. If null is returned it means there are no more comments. The
1271 returned string is NOT null terminated.
1273 DRFLAC_API
const char* drflac_next_vorbis_comment(drflac_vorbis_comment_iterator
* pIter
, drflac_uint32
* pCommentLengthOut
);
1276 /* Structure representing an iterator for cuesheet tracks in a CUESHEET metadata block. */
1279 drflac_uint32 countRemaining
;
1280 const char* pRunningData
;
1281 } drflac_cuesheet_track_iterator
;
1283 /* Packing is important on this structure because we map this directly to the raw data within the CUESHEET metadata block. */
1287 drflac_uint64 offset
;
1289 drflac_uint8 reserved
[3];
1290 } drflac_cuesheet_track_index
;
1295 drflac_uint64 offset
;
1296 drflac_uint8 trackNumber
;
1298 drflac_bool8 isAudio
;
1299 drflac_bool8 preEmphasis
;
1300 drflac_uint8 indexCount
;
1301 const drflac_cuesheet_track_index
* pIndexPoints
;
1302 } drflac_cuesheet_track
;
1305 Initializes a cuesheet track iterator. This can be used for iterating over the cuesheet tracks in a CUESHEET metadata
1308 DRFLAC_API
void drflac_init_cuesheet_track_iterator(drflac_cuesheet_track_iterator
* pIter
, drflac_uint32 trackCount
, const void* pTrackData
);
1310 /* Goes to the next cuesheet track in the given iterator. If DRFLAC_FALSE is returned it means there are no more comments. */
1311 DRFLAC_API drflac_bool32
drflac_next_cuesheet_track(drflac_cuesheet_track_iterator
* pIter
, drflac_cuesheet_track
* pCuesheetTrack
);
1317 #endif /* dr_flac_h */
1320 /************************************************************************************************************************************************************
1321 ************************************************************************************************************************************************************
1325 ************************************************************************************************************************************************************
1326 ************************************************************************************************************************************************************/
1327 #if defined(DR_FLAC_IMPLEMENTATION) || defined(DRFLAC_IMPLEMENTATION)
1331 /* Disable some annoying warnings. */
1332 #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
1333 #pragma GCC diagnostic push
1335 #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
1343 #ifndef _DEFAULT_SOURCE
1344 #define _DEFAULT_SOURCE
1356 #define DRFLAC_INLINE __forceinline
1357 #elif defined(__GNUC__)
1359 I've had a bug report where GCC is emitting warnings about functions possibly not being inlineable. This warning happens when
1360 the __attribute__((always_inline)) attribute is defined without an "inline" statement. I think therefore there must be some
1361 case where "__inline__" is not always defined, thus the compiler emitting these warnings. When using -std=c89 or -ansi on the
1362 command line, we cannot use the "inline" keyword and instead need to use "__inline__". In an attempt to work around this issue
1363 I am using "__inline__" only when we're compiling in strict ANSI mode.
1365 #if defined(__STRICT_ANSI__)
1366 #define DRFLAC_INLINE __inline__ __attribute__((always_inline))
1368 #define DRFLAC_INLINE inline __attribute__((always_inline))
1370 #elif defined(__WATCOMC__)
1371 #define DRFLAC_INLINE __inline
1373 #define DRFLAC_INLINE
1376 /* CPU architecture. */
1377 #if defined(__x86_64__) || defined(_M_X64)
1379 #elif defined(__i386) || defined(_M_IX86)
1381 #elif defined(__arm__) || defined(_M_ARM) || defined(_M_ARM64)
1388 There's a bug in GCC 4.2.x which results in an incorrect compilation error when using _mm_slli_epi32() where it complains with
1390 "error: shift must be an immediate"
1392 Unfortuantely dr_flac depends on this for a few things so we're just going to disable SSE on GCC 4.2 and below.
1394 #if !defined(DR_FLAC_NO_SIMD)
1395 #if defined(DRFLAC_X64) || defined(DRFLAC_X86)
1396 #if defined(_MSC_VER) && !defined(__clang__)
1398 #if _MSC_VER >= 1400 && !defined(DRFLAC_NO_SSE2) /* 2005 */
1399 #define DRFLAC_SUPPORT_SSE2
1401 #if _MSC_VER >= 1600 && !defined(DRFLAC_NO_SSE41) /* 2010 */
1402 #define DRFLAC_SUPPORT_SSE41
1404 #elif defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
1405 /* Assume GNUC-style. */
1406 #if defined(__SSE2__) && !defined(DRFLAC_NO_SSE2)
1407 #define DRFLAC_SUPPORT_SSE2
1409 #if defined(__SSE4_1__) && !defined(DRFLAC_NO_SSE41)
1410 #define DRFLAC_SUPPORT_SSE41
1414 /* If at this point we still haven't determined compiler support for the intrinsics just fall back to __has_include. */
1415 #if !defined(__GNUC__) && !defined(__clang__) && defined(__has_include)
1416 #if !defined(DRFLAC_SUPPORT_SSE2) && !defined(DRFLAC_NO_SSE2) && __has_include(<emmintrin.h>)
1417 #define DRFLAC_SUPPORT_SSE2
1419 #if !defined(DRFLAC_SUPPORT_SSE41) && !defined(DRFLAC_NO_SSE41) && __has_include(<smmintrin.h>)
1420 #define DRFLAC_SUPPORT_SSE41
1424 #if defined(DRFLAC_SUPPORT_SSE41)
1425 #include <smmintrin.h>
1426 #elif defined(DRFLAC_SUPPORT_SSE2)
1427 #include <emmintrin.h>
1431 #if defined(DRFLAC_ARM)
1432 #if !defined(DRFLAC_NO_NEON) && (defined(__ARM_NEON) || defined(__aarch64__) || defined(_M_ARM64))
1433 #define DRFLAC_SUPPORT_NEON
1436 /* Fall back to looking for the #include file. */
1437 #if !defined(__GNUC__) && !defined(__clang__) && defined(__has_include)
1438 #if !defined(DRFLAC_SUPPORT_NEON) && !defined(DRFLAC_NO_NEON) && __has_include(<arm_neon.h>)
1439 #define DRFLAC_SUPPORT_NEON
1443 #if defined(DRFLAC_SUPPORT_NEON)
1444 #include <arm_neon.h>
1449 /* Compile-time CPU feature support. */
1450 #if !defined(DR_FLAC_NO_SIMD) && (defined(DRFLAC_X86) || defined(DRFLAC_X64))
1451 #if defined(_MSC_VER) && !defined(__clang__)
1452 #if _MSC_VER >= 1400
1454 static void drflac__cpuid(int info
[4], int fid
)
1459 #define DRFLAC_NO_CPUID
1462 #if defined(__GNUC__) || defined(__clang__)
1463 static void drflac__cpuid(int info
[4], int fid
)
1466 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
1467 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
1468 supporting different assembly dialects.
1470 What's basically happening is that we're saving and restoring the ebx register manually.
1472 #if defined(DRFLAC_X86) && defined(__PIC__)
1473 __asm__
__volatile__ (
1474 "xchg{l} {%%}ebx, %k1;"
1476 "xchg{l} {%%}ebx, %k1;"
1477 : "=a"(info
[0]), "=&r"(info
[1]), "=c"(info
[2]), "=d"(info
[3]) : "a"(fid
), "c"(0)
1480 __asm__
__volatile__ (
1481 "cpuid" : "=a"(info
[0]), "=b"(info
[1]), "=c"(info
[2]), "=d"(info
[3]) : "a"(fid
), "c"(0)
1486 #define DRFLAC_NO_CPUID
1490 #define DRFLAC_NO_CPUID
1493 static DRFLAC_INLINE drflac_bool32
drflac_has_sse2(void)
1495 #if defined(DRFLAC_SUPPORT_SSE2)
1496 #if (defined(DRFLAC_X64) || defined(DRFLAC_X86)) && !defined(DRFLAC_NO_SSE2)
1497 #if defined(DRFLAC_X64)
1498 return DRFLAC_TRUE
; /* 64-bit targets always support SSE2. */
1499 #elif (defined(_M_IX86_FP) && _M_IX86_FP == 2) || defined(__SSE2__)
1500 return DRFLAC_TRUE
; /* If the compiler is allowed to freely generate SSE2 code we can assume support. */
1502 #if defined(DRFLAC_NO_CPUID)
1503 return DRFLAC_FALSE
;
1506 drflac__cpuid(info
, 1);
1507 return (info
[3] & (1 << 26)) != 0;
1511 return DRFLAC_FALSE
; /* SSE2 is only supported on x86 and x64 architectures. */
1514 return DRFLAC_FALSE
; /* No compiler support. */
1518 static DRFLAC_INLINE drflac_bool32
drflac_has_sse41(void)
1520 #if defined(DRFLAC_SUPPORT_SSE41)
1521 #if (defined(DRFLAC_X64) || defined(DRFLAC_X86)) && !defined(DRFLAC_NO_SSE41)
1522 #if defined(DRFLAC_X64)
1523 return DRFLAC_TRUE
; /* 64-bit targets always support SSE4.1. */
1524 #elif (defined(_M_IX86_FP) && _M_IX86_FP == 2) || defined(__SSE4_1__)
1525 return DRFLAC_TRUE
; /* If the compiler is allowed to freely generate SSE41 code we can assume support. */
1527 #if defined(DRFLAC_NO_CPUID)
1528 return DRFLAC_FALSE
;
1531 drflac__cpuid(info
, 1);
1532 return (info
[2] & (1 << 19)) != 0;
1536 return DRFLAC_FALSE
; /* SSE41 is only supported on x86 and x64 architectures. */
1539 return DRFLAC_FALSE
; /* No compiler support. */
1544 #if defined(_MSC_VER) && _MSC_VER >= 1500 && (defined(DRFLAC_X86) || defined(DRFLAC_X64)) && !defined(__clang__)
1545 #define DRFLAC_HAS_LZCNT_INTRINSIC
1546 #elif (defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)))
1547 #define DRFLAC_HAS_LZCNT_INTRINSIC
1548 #elif defined(__clang__)
1549 #if defined(__has_builtin)
1550 #if __has_builtin(__builtin_clzll) || __has_builtin(__builtin_clzl)
1551 #define DRFLAC_HAS_LZCNT_INTRINSIC
1556 #if defined(_MSC_VER) && _MSC_VER >= 1400 && !defined(__clang__)
1557 #define DRFLAC_HAS_BYTESWAP16_INTRINSIC
1558 #define DRFLAC_HAS_BYTESWAP32_INTRINSIC
1559 #define DRFLAC_HAS_BYTESWAP64_INTRINSIC
1560 #elif defined(__clang__)
1561 #if defined(__has_builtin)
1562 #if __has_builtin(__builtin_bswap16)
1563 #define DRFLAC_HAS_BYTESWAP16_INTRINSIC
1565 #if __has_builtin(__builtin_bswap32)
1566 #define DRFLAC_HAS_BYTESWAP32_INTRINSIC
1568 #if __has_builtin(__builtin_bswap64)
1569 #define DRFLAC_HAS_BYTESWAP64_INTRINSIC
1572 #elif defined(__GNUC__)
1573 #if ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
1574 #define DRFLAC_HAS_BYTESWAP32_INTRINSIC
1575 #define DRFLAC_HAS_BYTESWAP64_INTRINSIC
1577 #if ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))
1578 #define DRFLAC_HAS_BYTESWAP16_INTRINSIC
1580 #elif defined(__WATCOMC__) && defined(__386__)
1581 #define DRFLAC_HAS_BYTESWAP16_INTRINSIC
1582 #define DRFLAC_HAS_BYTESWAP32_INTRINSIC
1583 #define DRFLAC_HAS_BYTESWAP64_INTRINSIC
1584 extern __inline drflac_uint16
_watcom_bswap16(drflac_uint16
);
1585 extern __inline drflac_uint32
_watcom_bswap32(drflac_uint32
);
1586 extern __inline drflac_uint64
_watcom_bswap64(drflac_uint64
);
1587 #pragma aux _watcom_bswap16 = \
1591 #pragma aux _watcom_bswap32 = \
1595 #pragma aux _watcom_bswap64 = \
1604 /* Standard library stuff. */
1605 #ifndef DRFLAC_ASSERT
1607 #define DRFLAC_ASSERT(expression) assert(expression)
1609 #ifndef DRFLAC_MALLOC
1610 #define DRFLAC_MALLOC(sz) malloc((sz))
1612 #ifndef DRFLAC_REALLOC
1613 #define DRFLAC_REALLOC(p, sz) realloc((p), (sz))
1616 #define DRFLAC_FREE(p) free((p))
1618 #ifndef DRFLAC_COPY_MEMORY
1619 #define DRFLAC_COPY_MEMORY(dst, src, sz) memcpy((dst), (src), (sz))
1621 #ifndef DRFLAC_ZERO_MEMORY
1622 #define DRFLAC_ZERO_MEMORY(p, sz) memset((p), 0, (sz))
1624 #ifndef DRFLAC_ZERO_OBJECT
1625 #define DRFLAC_ZERO_OBJECT(p) DRFLAC_ZERO_MEMORY((p), sizeof(*(p)))
1628 #define DRFLAC_MAX_SIMD_VECTOR_SIZE 64 /* 64 for AVX-512 in the future. */
1630 typedef drflac_int32 drflac_result
;
1631 #define DRFLAC_SUCCESS 0
1632 #define DRFLAC_ERROR -1 /* A generic error. */
1633 #define DRFLAC_INVALID_ARGS -2
1634 #define DRFLAC_INVALID_OPERATION -3
1635 #define DRFLAC_OUT_OF_MEMORY -4
1636 #define DRFLAC_OUT_OF_RANGE -5
1637 #define DRFLAC_ACCESS_DENIED -6
1638 #define DRFLAC_DOES_NOT_EXIST -7
1639 #define DRFLAC_ALREADY_EXISTS -8
1640 #define DRFLAC_TOO_MANY_OPEN_FILES -9
1641 #define DRFLAC_INVALID_FILE -10
1642 #define DRFLAC_TOO_BIG -11
1643 #define DRFLAC_PATH_TOO_LONG -12
1644 #define DRFLAC_NAME_TOO_LONG -13
1645 #define DRFLAC_NOT_DIRECTORY -14
1646 #define DRFLAC_IS_DIRECTORY -15
1647 #define DRFLAC_DIRECTORY_NOT_EMPTY -16
1648 #define DRFLAC_END_OF_FILE -17
1649 #define DRFLAC_NO_SPACE -18
1650 #define DRFLAC_BUSY -19
1651 #define DRFLAC_IO_ERROR -20
1652 #define DRFLAC_INTERRUPT -21
1653 #define DRFLAC_UNAVAILABLE -22
1654 #define DRFLAC_ALREADY_IN_USE -23
1655 #define DRFLAC_BAD_ADDRESS -24
1656 #define DRFLAC_BAD_SEEK -25
1657 #define DRFLAC_BAD_PIPE -26
1658 #define DRFLAC_DEADLOCK -27
1659 #define DRFLAC_TOO_MANY_LINKS -28
1660 #define DRFLAC_NOT_IMPLEMENTED -29
1661 #define DRFLAC_NO_MESSAGE -30
1662 #define DRFLAC_BAD_MESSAGE -31
1663 #define DRFLAC_NO_DATA_AVAILABLE -32
1664 #define DRFLAC_INVALID_DATA -33
1665 #define DRFLAC_TIMEOUT -34
1666 #define DRFLAC_NO_NETWORK -35
1667 #define DRFLAC_NOT_UNIQUE -36
1668 #define DRFLAC_NOT_SOCKET -37
1669 #define DRFLAC_NO_ADDRESS -38
1670 #define DRFLAC_BAD_PROTOCOL -39
1671 #define DRFLAC_PROTOCOL_UNAVAILABLE -40
1672 #define DRFLAC_PROTOCOL_NOT_SUPPORTED -41
1673 #define DRFLAC_PROTOCOL_FAMILY_NOT_SUPPORTED -42
1674 #define DRFLAC_ADDRESS_FAMILY_NOT_SUPPORTED -43
1675 #define DRFLAC_SOCKET_NOT_SUPPORTED -44
1676 #define DRFLAC_CONNECTION_RESET -45
1677 #define DRFLAC_ALREADY_CONNECTED -46
1678 #define DRFLAC_NOT_CONNECTED -47
1679 #define DRFLAC_CONNECTION_REFUSED -48
1680 #define DRFLAC_NO_HOST -49
1681 #define DRFLAC_IN_PROGRESS -50
1682 #define DRFLAC_CANCELLED -51
1683 #define DRFLAC_MEMORY_ALREADY_MAPPED -52
1684 #define DRFLAC_AT_END -53
1685 #define DRFLAC_CRC_MISMATCH -128
1687 #define DRFLAC_SUBFRAME_CONSTANT 0
1688 #define DRFLAC_SUBFRAME_VERBATIM 1
1689 #define DRFLAC_SUBFRAME_FIXED 8
1690 #define DRFLAC_SUBFRAME_LPC 32
1691 #define DRFLAC_SUBFRAME_RESERVED 255
1693 #define DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE 0
1694 #define DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2 1
1696 #define DRFLAC_CHANNEL_ASSIGNMENT_INDEPENDENT 0
1697 #define DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE 8
1698 #define DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE 9
1699 #define DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE 10
1701 #define drflac_align(x, a) ((((x) + (a) - 1) / (a)) * (a))
1704 DRFLAC_API
void drflac_version(drflac_uint32
* pMajor
, drflac_uint32
* pMinor
, drflac_uint32
* pRevision
)
1707 *pMajor
= DRFLAC_VERSION_MAJOR
;
1711 *pMinor
= DRFLAC_VERSION_MINOR
;
1715 *pRevision
= DRFLAC_VERSION_REVISION
;
1719 DRFLAC_API
const char* drflac_version_string(void)
1721 return DRFLAC_VERSION_STRING
;
1726 #if defined(__has_feature)
1727 #if __has_feature(thread_sanitizer)
1728 #define DRFLAC_NO_THREAD_SANITIZE __attribute__((no_sanitize("thread")))
1730 #define DRFLAC_NO_THREAD_SANITIZE
1733 #define DRFLAC_NO_THREAD_SANITIZE
1736 #if defined(DRFLAC_HAS_LZCNT_INTRINSIC)
1737 static drflac_bool32 drflac__gIsLZCNTSupported
= DRFLAC_FALSE
;
1740 #ifndef DRFLAC_NO_CPUID
1741 static drflac_bool32 drflac__gIsSSE2Supported
= DRFLAC_FALSE
;
1742 static drflac_bool32 drflac__gIsSSE41Supported
= DRFLAC_FALSE
;
1745 I've had a bug report that Clang's ThreadSanitizer presents a warning in this function. Having reviewed this, this does
1746 actually make sense. However, since CPU caps should never differ for a running process, I don't think the trade off of
1747 complicating internal API's by passing around CPU caps versus just disabling the warnings is worthwhile. I'm therefore
1748 just going to disable these warnings. This is disabled via the DRFLAC_NO_THREAD_SANITIZE attribute.
1750 DRFLAC_NO_THREAD_SANITIZE
static void drflac__init_cpu_caps(void)
1752 static drflac_bool32 isCPUCapsInitialized
= DRFLAC_FALSE
;
1754 if (!isCPUCapsInitialized
) {
1756 #if defined(DRFLAC_HAS_LZCNT_INTRINSIC)
1758 drflac__cpuid(info
, 0x80000001);
1759 drflac__gIsLZCNTSupported
= (info
[2] & (1 << 5)) != 0;
1763 drflac__gIsSSE2Supported
= drflac_has_sse2();
1766 drflac__gIsSSE41Supported
= drflac_has_sse41();
1769 isCPUCapsInitialized
= DRFLAC_TRUE
;
1773 static drflac_bool32 drflac__gIsNEONSupported
= DRFLAC_FALSE
;
1775 static DRFLAC_INLINE drflac_bool32
drflac__has_neon(void)
1777 #if defined(DRFLAC_SUPPORT_NEON)
1778 #if defined(DRFLAC_ARM) && !defined(DRFLAC_NO_NEON)
1779 #if (defined(__ARM_NEON) || defined(__aarch64__) || defined(_M_ARM64))
1780 return DRFLAC_TRUE
; /* If the compiler is allowed to freely generate NEON code we can assume support. */
1782 /* TODO: Runtime check. */
1783 return DRFLAC_FALSE
;
1786 return DRFLAC_FALSE
; /* NEON is only supported on ARM architectures. */
1789 return DRFLAC_FALSE
; /* No compiler support. */
1793 DRFLAC_NO_THREAD_SANITIZE
static void drflac__init_cpu_caps(void)
1795 drflac__gIsNEONSupported
= drflac__has_neon();
1797 #if defined(DRFLAC_HAS_LZCNT_INTRINSIC) && defined(DRFLAC_ARM) && (defined(__ARM_ARCH) && __ARM_ARCH >= 5)
1798 drflac__gIsLZCNTSupported
= DRFLAC_TRUE
;
1804 /* Endian Management */
1805 static DRFLAC_INLINE drflac_bool32
drflac__is_little_endian(void)
1807 #if defined(DRFLAC_X86) || defined(DRFLAC_X64)
1809 #elif defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN
1813 return (*(char*)&n
) == 1;
1817 static DRFLAC_INLINE drflac_uint16
drflac__swap_endian_uint16(drflac_uint16 n
)
1819 #ifdef DRFLAC_HAS_BYTESWAP16_INTRINSIC
1820 #if defined(_MSC_VER) && !defined(__clang__)
1821 return _byteswap_ushort(n
);
1822 #elif defined(__GNUC__) || defined(__clang__)
1823 return __builtin_bswap16(n
);
1824 #elif defined(__WATCOMC__) && defined(__386__)
1825 return _watcom_bswap16(n
);
1827 #error "This compiler does not support the byte swap intrinsic."
1830 return ((n
& 0xFF00) >> 8) |
1831 ((n
& 0x00FF) << 8);
1835 static DRFLAC_INLINE drflac_uint32
drflac__swap_endian_uint32(drflac_uint32 n
)
1837 #ifdef DRFLAC_HAS_BYTESWAP32_INTRINSIC
1838 #if defined(_MSC_VER) && !defined(__clang__)
1839 return _byteswap_ulong(n
);
1840 #elif defined(__GNUC__) || defined(__clang__)
1841 #if defined(DRFLAC_ARM) && (defined(__ARM_ARCH) && __ARM_ARCH >= 6) && !defined(DRFLAC_64BIT) /* <-- 64-bit inline assembly has not been tested, so disabling for now. */
1842 /* Inline assembly optimized implementation for ARM. In my testing, GCC does not generate optimized code with __builtin_bswap32(). */
1844 __asm__
__volatile__ (
1845 #if defined(DRFLAC_64BIT)
1846 "rev %w[out], %w[in]" : [out
]"=r"(r
) : [in
]"r"(n
) /* <-- This is untested. If someone in the community could test this, that would be appreciated! */
1848 "rev %[out], %[in]" : [out
]"=r"(r
) : [in
]"r"(n
)
1853 return __builtin_bswap32(n
);
1855 #elif defined(__WATCOMC__) && defined(__386__)
1856 return _watcom_bswap32(n
);
1858 #error "This compiler does not support the byte swap intrinsic."
1861 return ((n
& 0xFF000000) >> 24) |
1862 ((n
& 0x00FF0000) >> 8) |
1863 ((n
& 0x0000FF00) << 8) |
1864 ((n
& 0x000000FF) << 24);
1868 static DRFLAC_INLINE drflac_uint64
drflac__swap_endian_uint64(drflac_uint64 n
)
1870 #ifdef DRFLAC_HAS_BYTESWAP64_INTRINSIC
1871 #if defined(_MSC_VER) && !defined(__clang__)
1872 return _byteswap_uint64(n
);
1873 #elif defined(__GNUC__) || defined(__clang__)
1874 return __builtin_bswap64(n
);
1875 #elif defined(__WATCOMC__) && defined(__386__)
1876 return _watcom_bswap64(n
);
1878 #error "This compiler does not support the byte swap intrinsic."
1881 /* Weird "<< 32" bitshift is required for C89 because it doesn't support 64-bit constants. Should be optimized out by a good compiler. */
1882 return ((n
& ((drflac_uint64
)0xFF000000 << 32)) >> 56) |
1883 ((n
& ((drflac_uint64
)0x00FF0000 << 32)) >> 40) |
1884 ((n
& ((drflac_uint64
)0x0000FF00 << 32)) >> 24) |
1885 ((n
& ((drflac_uint64
)0x000000FF << 32)) >> 8) |
1886 ((n
& ((drflac_uint64
)0xFF000000 )) << 8) |
1887 ((n
& ((drflac_uint64
)0x00FF0000 )) << 24) |
1888 ((n
& ((drflac_uint64
)0x0000FF00 )) << 40) |
1889 ((n
& ((drflac_uint64
)0x000000FF )) << 56);
1894 static DRFLAC_INLINE drflac_uint16
drflac__be2host_16(drflac_uint16 n
)
1896 if (drflac__is_little_endian()) {
1897 return drflac__swap_endian_uint16(n
);
1903 static DRFLAC_INLINE drflac_uint32
drflac__be2host_32(drflac_uint32 n
)
1905 if (drflac__is_little_endian()) {
1906 return drflac__swap_endian_uint32(n
);
1912 static DRFLAC_INLINE drflac_uint64
drflac__be2host_64(drflac_uint64 n
)
1914 if (drflac__is_little_endian()) {
1915 return drflac__swap_endian_uint64(n
);
1922 static DRFLAC_INLINE drflac_uint32
drflac__le2host_32(drflac_uint32 n
)
1924 if (!drflac__is_little_endian()) {
1925 return drflac__swap_endian_uint32(n
);
1932 static DRFLAC_INLINE drflac_uint32
drflac__unsynchsafe_32(drflac_uint32 n
)
1934 drflac_uint32 result
= 0;
1935 result
|= (n
& 0x7F000000) >> 3;
1936 result
|= (n
& 0x007F0000) >> 2;
1937 result
|= (n
& 0x00007F00) >> 1;
1938 result
|= (n
& 0x0000007F) >> 0;
1945 /* The CRC code below is based on this document: http://zlib.net/crc_v3.txt */
1946 static drflac_uint8 drflac__crc8_table
[] = {
1947 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D,
1948 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D,
1949 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD,
1950 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD,
1951 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA,
1952 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A,
1953 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A,
1954 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A,
1955 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4,
1956 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4,
1957 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44,
1958 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34,
1959 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63,
1960 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13,
1961 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,
1962 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3
1965 static drflac_uint16 drflac__crc16_table
[] = {
1966 0x0000, 0x8005, 0x800F, 0x000A, 0x801B, 0x001E, 0x0014, 0x8011,
1967 0x8033, 0x0036, 0x003C, 0x8039, 0x0028, 0x802D, 0x8027, 0x0022,
1968 0x8063, 0x0066, 0x006C, 0x8069, 0x0078, 0x807D, 0x8077, 0x0072,
1969 0x0050, 0x8055, 0x805F, 0x005A, 0x804B, 0x004E, 0x0044, 0x8041,
1970 0x80C3, 0x00C6, 0x00CC, 0x80C9, 0x00D8, 0x80DD, 0x80D7, 0x00D2,
1971 0x00F0, 0x80F5, 0x80FF, 0x00FA, 0x80EB, 0x00EE, 0x00E4, 0x80E1,
1972 0x00A0, 0x80A5, 0x80AF, 0x00AA, 0x80BB, 0x00BE, 0x00B4, 0x80B1,
1973 0x8093, 0x0096, 0x009C, 0x8099, 0x0088, 0x808D, 0x8087, 0x0082,
1974 0x8183, 0x0186, 0x018C, 0x8189, 0x0198, 0x819D, 0x8197, 0x0192,
1975 0x01B0, 0x81B5, 0x81BF, 0x01BA, 0x81AB, 0x01AE, 0x01A4, 0x81A1,
1976 0x01E0, 0x81E5, 0x81EF, 0x01EA, 0x81FB, 0x01FE, 0x01F4, 0x81F1,
1977 0x81D3, 0x01D6, 0x01DC, 0x81D9, 0x01C8, 0x81CD, 0x81C7, 0x01C2,
1978 0x0140, 0x8145, 0x814F, 0x014A, 0x815B, 0x015E, 0x0154, 0x8151,
1979 0x8173, 0x0176, 0x017C, 0x8179, 0x0168, 0x816D, 0x8167, 0x0162,
1980 0x8123, 0x0126, 0x012C, 0x8129, 0x0138, 0x813D, 0x8137, 0x0132,
1981 0x0110, 0x8115, 0x811F, 0x011A, 0x810B, 0x010E, 0x0104, 0x8101,
1982 0x8303, 0x0306, 0x030C, 0x8309, 0x0318, 0x831D, 0x8317, 0x0312,
1983 0x0330, 0x8335, 0x833F, 0x033A, 0x832B, 0x032E, 0x0324, 0x8321,
1984 0x0360, 0x8365, 0x836F, 0x036A, 0x837B, 0x037E, 0x0374, 0x8371,
1985 0x8353, 0x0356, 0x035C, 0x8359, 0x0348, 0x834D, 0x8347, 0x0342,
1986 0x03C0, 0x83C5, 0x83CF, 0x03CA, 0x83DB, 0x03DE, 0x03D4, 0x83D1,
1987 0x83F3, 0x03F6, 0x03FC, 0x83F9, 0x03E8, 0x83ED, 0x83E7, 0x03E2,
1988 0x83A3, 0x03A6, 0x03AC, 0x83A9, 0x03B8, 0x83BD, 0x83B7, 0x03B2,
1989 0x0390, 0x8395, 0x839F, 0x039A, 0x838B, 0x038E, 0x0384, 0x8381,
1990 0x0280, 0x8285, 0x828F, 0x028A, 0x829B, 0x029E, 0x0294, 0x8291,
1991 0x82B3, 0x02B6, 0x02BC, 0x82B9, 0x02A8, 0x82AD, 0x82A7, 0x02A2,
1992 0x82E3, 0x02E6, 0x02EC, 0x82E9, 0x02F8, 0x82FD, 0x82F7, 0x02F2,
1993 0x02D0, 0x82D5, 0x82DF, 0x02DA, 0x82CB, 0x02CE, 0x02C4, 0x82C1,
1994 0x8243, 0x0246, 0x024C, 0x8249, 0x0258, 0x825D, 0x8257, 0x0252,
1995 0x0270, 0x8275, 0x827F, 0x027A, 0x826B, 0x026E, 0x0264, 0x8261,
1996 0x0220, 0x8225, 0x822F, 0x022A, 0x823B, 0x023E, 0x0234, 0x8231,
1997 0x8213, 0x0216, 0x021C, 0x8219, 0x0208, 0x820D, 0x8207, 0x0202
2000 static DRFLAC_INLINE drflac_uint8
drflac_crc8_byte(drflac_uint8 crc
, drflac_uint8 data
)
2002 return drflac__crc8_table
[crc
^ data
];
2005 static DRFLAC_INLINE drflac_uint8
drflac_crc8(drflac_uint8 crc
, drflac_uint32 data
, drflac_uint32 count
)
2007 #ifdef DR_FLAC_NO_CRC
2014 /* REFERENCE (use of this implementation requires an explicit flush by doing "drflac_crc8(crc, 0, 8);") */
2015 drflac_uint8 p
= 0x07;
2016 for (int i
= count
-1; i
>= 0; --i
) {
2017 drflac_uint8 bit
= (data
& (1 << i
)) >> i
;
2019 crc
= ((crc
<< 1) | bit
) ^ p
;
2021 crc
= ((crc
<< 1) | bit
);
2026 drflac_uint32 wholeBytes
;
2027 drflac_uint32 leftoverBits
;
2028 drflac_uint64 leftoverDataMask
;
2030 static drflac_uint64 leftoverDataMaskTable
[8] = {
2031 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F
2034 DRFLAC_ASSERT(count
<= 32);
2036 wholeBytes
= count
>> 3;
2037 leftoverBits
= count
- (wholeBytes
*8);
2038 leftoverDataMask
= leftoverDataMaskTable
[leftoverBits
];
2040 switch (wholeBytes
) {
2041 case 4: crc
= drflac_crc8_byte(crc
, (drflac_uint8
)((data
& (0xFF000000UL
<< leftoverBits
)) >> (24 + leftoverBits
)));
2042 case 3: crc
= drflac_crc8_byte(crc
, (drflac_uint8
)((data
& (0x00FF0000UL
<< leftoverBits
)) >> (16 + leftoverBits
)));
2043 case 2: crc
= drflac_crc8_byte(crc
, (drflac_uint8
)((data
& (0x0000FF00UL
<< leftoverBits
)) >> ( 8 + leftoverBits
)));
2044 case 1: crc
= drflac_crc8_byte(crc
, (drflac_uint8
)((data
& (0x000000FFUL
<< leftoverBits
)) >> ( 0 + leftoverBits
)));
2045 case 0: if (leftoverBits
> 0) crc
= (drflac_uint8
)((crc
<< leftoverBits
) ^ drflac__crc8_table
[(crc
>> (8 - leftoverBits
)) ^ (data
& leftoverDataMask
)]);
2052 static DRFLAC_INLINE drflac_uint16
drflac_crc16_byte(drflac_uint16 crc
, drflac_uint8 data
)
2054 return (crc
<< 8) ^ drflac__crc16_table
[(drflac_uint8
)(crc
>> 8) ^ data
];
2057 static DRFLAC_INLINE drflac_uint16
drflac_crc16_cache(drflac_uint16 crc
, drflac_cache_t data
)
2060 crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
>> 56) & 0xFF));
2061 crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
>> 48) & 0xFF));
2062 crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
>> 40) & 0xFF));
2063 crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
>> 32) & 0xFF));
2065 crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
>> 24) & 0xFF));
2066 crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
>> 16) & 0xFF));
2067 crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
>> 8) & 0xFF));
2068 crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
>> 0) & 0xFF));
2073 static DRFLAC_INLINE drflac_uint16
drflac_crc16_bytes(drflac_uint16 crc
, drflac_cache_t data
, drflac_uint32 byteCount
)
2078 case 8: crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
>> 56) & 0xFF));
2079 case 7: crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
>> 48) & 0xFF));
2080 case 6: crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
>> 40) & 0xFF));
2081 case 5: crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
>> 32) & 0xFF));
2083 case 4: crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
>> 24) & 0xFF));
2084 case 3: crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
>> 16) & 0xFF));
2085 case 2: crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
>> 8) & 0xFF));
2086 case 1: crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
>> 0) & 0xFF));
2093 static DRFLAC_INLINE drflac_uint16
drflac_crc16__32bit(drflac_uint16 crc
, drflac_uint32 data
, drflac_uint32 count
)
2095 #ifdef DR_FLAC_NO_CRC
2102 /* REFERENCE (use of this implementation requires an explicit flush by doing "drflac_crc16(crc, 0, 16);") */
2103 drflac_uint16 p
= 0x8005;
2104 for (int i
= count
-1; i
>= 0; --i
) {
2105 drflac_uint16 bit
= (data
& (1ULL << i
)) >> i
;
2107 r
= ((r
<< 1) | bit
) ^ p
;
2109 r
= ((r
<< 1) | bit
);
2115 drflac_uint32 wholeBytes
;
2116 drflac_uint32 leftoverBits
;
2117 drflac_uint64 leftoverDataMask
;
2119 static drflac_uint64 leftoverDataMaskTable
[8] = {
2120 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F
2123 DRFLAC_ASSERT(count
<= 64);
2125 wholeBytes
= count
>> 3;
2126 leftoverBits
= count
& 7;
2127 leftoverDataMask
= leftoverDataMaskTable
[leftoverBits
];
2129 switch (wholeBytes
) {
2131 case 4: crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
& (0xFF000000UL
<< leftoverBits
)) >> (24 + leftoverBits
)));
2132 case 3: crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
& (0x00FF0000UL
<< leftoverBits
)) >> (16 + leftoverBits
)));
2133 case 2: crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
& (0x0000FF00UL
<< leftoverBits
)) >> ( 8 + leftoverBits
)));
2134 case 1: crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
& (0x000000FFUL
<< leftoverBits
)) >> ( 0 + leftoverBits
)));
2135 case 0: if (leftoverBits
> 0) crc
= (crc
<< leftoverBits
) ^ drflac__crc16_table
[(crc
>> (16 - leftoverBits
)) ^ (data
& leftoverDataMask
)];
2142 static DRFLAC_INLINE drflac_uint16
drflac_crc16__64bit(drflac_uint16 crc
, drflac_uint64 data
, drflac_uint32 count
)
2144 #ifdef DR_FLAC_NO_CRC
2150 drflac_uint32 wholeBytes
;
2151 drflac_uint32 leftoverBits
;
2152 drflac_uint64 leftoverDataMask
;
2154 static drflac_uint64 leftoverDataMaskTable
[8] = {
2155 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F
2158 DRFLAC_ASSERT(count
<= 64);
2160 wholeBytes
= count
>> 3;
2161 leftoverBits
= count
& 7;
2162 leftoverDataMask
= leftoverDataMaskTable
[leftoverBits
];
2164 switch (wholeBytes
) {
2166 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. */
2167 case 7: crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
& (((drflac_uint64
)0x00FF0000 << 32) << leftoverBits
)) >> (48 + leftoverBits
)));
2168 case 6: crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
& (((drflac_uint64
)0x0000FF00 << 32) << leftoverBits
)) >> (40 + leftoverBits
)));
2169 case 5: crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
& (((drflac_uint64
)0x000000FF << 32) << leftoverBits
)) >> (32 + leftoverBits
)));
2170 case 4: crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
& (((drflac_uint64
)0xFF000000 ) << leftoverBits
)) >> (24 + leftoverBits
)));
2171 case 3: crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
& (((drflac_uint64
)0x00FF0000 ) << leftoverBits
)) >> (16 + leftoverBits
)));
2172 case 2: crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
& (((drflac_uint64
)0x0000FF00 ) << leftoverBits
)) >> ( 8 + leftoverBits
)));
2173 case 1: crc
= drflac_crc16_byte(crc
, (drflac_uint8
)((data
& (((drflac_uint64
)0x000000FF ) << leftoverBits
)) >> ( 0 + leftoverBits
)));
2174 case 0: if (leftoverBits
> 0) crc
= (crc
<< leftoverBits
) ^ drflac__crc16_table
[(crc
>> (16 - leftoverBits
)) ^ (data
& leftoverDataMask
)];
2181 static DRFLAC_INLINE drflac_uint16
drflac_crc16(drflac_uint16 crc
, drflac_cache_t data
, drflac_uint32 count
)
2184 return drflac_crc16__64bit(crc
, data
, count
);
2186 return drflac_crc16__32bit(crc
, data
, count
);
2193 #define drflac__be2host__cache_line drflac__be2host_64
2195 #define drflac__be2host__cache_line drflac__be2host_32
2199 BIT READING ATTEMPT #2
2201 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
2202 on the most significant bit. It uses the notion of an L1 and L2 cache (borrowed from CPU architecture), where the L1 cache
2203 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
2204 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
2205 from onRead() is read into.
2207 #define DRFLAC_CACHE_L1_SIZE_BYTES(bs) (sizeof((bs)->cache))
2208 #define DRFLAC_CACHE_L1_SIZE_BITS(bs) (sizeof((bs)->cache)*8)
2209 #define DRFLAC_CACHE_L1_BITS_REMAINING(bs) (DRFLAC_CACHE_L1_SIZE_BITS(bs) - (bs)->consumedBits)
2210 #define DRFLAC_CACHE_L1_SELECTION_MASK(_bitCount) (~((~(drflac_cache_t)0) >> (_bitCount)))
2211 #define DRFLAC_CACHE_L1_SELECTION_SHIFT(bs, _bitCount) (DRFLAC_CACHE_L1_SIZE_BITS(bs) - (_bitCount))
2212 #define DRFLAC_CACHE_L1_SELECT(bs, _bitCount) (((bs)->cache) & DRFLAC_CACHE_L1_SELECTION_MASK(_bitCount))
2213 #define DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, _bitCount) (DRFLAC_CACHE_L1_SELECT((bs), (_bitCount)) >> DRFLAC_CACHE_L1_SELECTION_SHIFT((bs), (_bitCount)))
2214 #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)))
2215 #define DRFLAC_CACHE_L2_SIZE_BYTES(bs) (sizeof((bs)->cacheL2))
2216 #define DRFLAC_CACHE_L2_LINE_COUNT(bs) (DRFLAC_CACHE_L2_SIZE_BYTES(bs) / sizeof((bs)->cacheL2[0]))
2217 #define DRFLAC_CACHE_L2_LINES_REMAINING(bs) (DRFLAC_CACHE_L2_LINE_COUNT(bs) - (bs)->nextL2Line)
2220 #ifndef DR_FLAC_NO_CRC
2221 static DRFLAC_INLINE
void drflac__reset_crc16(drflac_bs
* bs
)
2224 bs
->crc16CacheIgnoredBytes
= bs
->consumedBits
>> 3;
2227 static DRFLAC_INLINE
void drflac__update_crc16(drflac_bs
* bs
)
2229 if (bs
->crc16CacheIgnoredBytes
== 0) {
2230 bs
->crc16
= drflac_crc16_cache(bs
->crc16
, bs
->crc16Cache
);
2232 bs
->crc16
= drflac_crc16_bytes(bs
->crc16
, bs
->crc16Cache
, DRFLAC_CACHE_L1_SIZE_BYTES(bs
) - bs
->crc16CacheIgnoredBytes
);
2233 bs
->crc16CacheIgnoredBytes
= 0;
2237 static DRFLAC_INLINE drflac_uint16
drflac__flush_crc16(drflac_bs
* bs
)
2239 /* We should never be flushing in a situation where we are not aligned on a byte boundary. */
2240 DRFLAC_ASSERT((DRFLAC_CACHE_L1_BITS_REMAINING(bs
) & 7) == 0);
2243 The bits that were read from the L1 cache need to be accumulated. The number of bytes needing to be accumulated is determined
2244 by the number of bits that have been consumed.
2246 if (DRFLAC_CACHE_L1_BITS_REMAINING(bs
) == 0) {
2247 drflac__update_crc16(bs
);
2249 /* We only accumulate the consumed bits. */
2250 bs
->crc16
= drflac_crc16_bytes(bs
->crc16
, bs
->crc16Cache
>> DRFLAC_CACHE_L1_BITS_REMAINING(bs
), (bs
->consumedBits
>> 3) - bs
->crc16CacheIgnoredBytes
);
2253 The bits that we just accumulated should never be accumulated again. We need to keep track of how many bytes were accumulated
2254 so we can handle that later.
2256 bs
->crc16CacheIgnoredBytes
= bs
->consumedBits
>> 3;
2263 static DRFLAC_INLINE drflac_bool32
drflac__reload_l1_cache_from_l2(drflac_bs
* bs
)
2266 size_t alignedL1LineCount
;
2268 /* Fast path. Try loading straight from L2. */
2269 if (bs
->nextL2Line
< DRFLAC_CACHE_L2_LINE_COUNT(bs
)) {
2270 bs
->cache
= bs
->cacheL2
[bs
->nextL2Line
++];
2275 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
2278 if (bs
->unalignedByteCount
> 0) {
2279 return DRFLAC_FALSE
; /* If we have any unaligned bytes it means there's no more aligned bytes left in the client. */
2282 bytesRead
= bs
->onRead(bs
->pUserData
, bs
->cacheL2
, DRFLAC_CACHE_L2_SIZE_BYTES(bs
));
2285 if (bytesRead
== DRFLAC_CACHE_L2_SIZE_BYTES(bs
)) {
2286 bs
->cache
= bs
->cacheL2
[bs
->nextL2Line
++];
2292 If we get here it means we were unable to retrieve enough data to fill the entire L2 cache. It probably
2293 means we've just reached the end of the file. We need to move the valid data down to the end of the buffer
2294 and adjust the index of the next line accordingly. Also keep in mind that the L2 cache must be aligned to
2295 the size of the L1 so we'll need to seek backwards by any misaligned bytes.
2297 alignedL1LineCount
= bytesRead
/ DRFLAC_CACHE_L1_SIZE_BYTES(bs
);
2299 /* We need to keep track of any unaligned bytes for later use. */
2300 bs
->unalignedByteCount
= bytesRead
- (alignedL1LineCount
* DRFLAC_CACHE_L1_SIZE_BYTES(bs
));
2301 if (bs
->unalignedByteCount
> 0) {
2302 bs
->unalignedCache
= bs
->cacheL2
[alignedL1LineCount
];
2305 if (alignedL1LineCount
> 0) {
2306 size_t offset
= DRFLAC_CACHE_L2_LINE_COUNT(bs
) - alignedL1LineCount
;
2308 for (i
= alignedL1LineCount
; i
> 0; --i
) {
2309 bs
->cacheL2
[i
-1 + offset
] = bs
->cacheL2
[i
-1];
2312 bs
->nextL2Line
= (drflac_uint32
)offset
;
2313 bs
->cache
= bs
->cacheL2
[bs
->nextL2Line
++];
2316 /* If we get into this branch it means we weren't able to load any L1-aligned data. */
2317 bs
->nextL2Line
= DRFLAC_CACHE_L2_LINE_COUNT(bs
);
2318 return DRFLAC_FALSE
;
2322 static drflac_bool32
drflac__reload_cache(drflac_bs
* bs
)
2326 #ifndef DR_FLAC_NO_CRC
2327 drflac__update_crc16(bs
);
2330 /* Fast path. Try just moving the next value in the L2 cache to the L1 cache. */
2331 if (drflac__reload_l1_cache_from_l2(bs
)) {
2332 bs
->cache
= drflac__be2host__cache_line(bs
->cache
);
2333 bs
->consumedBits
= 0;
2334 #ifndef DR_FLAC_NO_CRC
2335 bs
->crc16Cache
= bs
->cache
;
2343 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
2344 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
2345 data from the unaligned cache.
2347 bytesRead
= bs
->unalignedByteCount
;
2348 if (bytesRead
== 0) {
2349 bs
->consumedBits
= DRFLAC_CACHE_L1_SIZE_BITS(bs
); /* <-- The stream has been exhausted, so marked the bits as consumed. */
2350 return DRFLAC_FALSE
;
2353 DRFLAC_ASSERT(bytesRead
< DRFLAC_CACHE_L1_SIZE_BYTES(bs
));
2354 bs
->consumedBits
= (drflac_uint32
)(DRFLAC_CACHE_L1_SIZE_BYTES(bs
) - bytesRead
) * 8;
2356 bs
->cache
= drflac__be2host__cache_line(bs
->unalignedCache
);
2357 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. */
2358 bs
->unalignedByteCount
= 0; /* <-- At this point the unaligned bytes have been moved into the cache and we thus have no more unaligned bytes. */
2360 #ifndef DR_FLAC_NO_CRC
2361 bs
->crc16Cache
= bs
->cache
>> bs
->consumedBits
;
2362 bs
->crc16CacheIgnoredBytes
= bs
->consumedBits
>> 3;
2367 static void drflac__reset_cache(drflac_bs
* bs
)
2369 bs
->nextL2Line
= DRFLAC_CACHE_L2_LINE_COUNT(bs
); /* <-- This clears the L2 cache. */
2370 bs
->consumedBits
= DRFLAC_CACHE_L1_SIZE_BITS(bs
); /* <-- This clears the L1 cache. */
2372 bs
->unalignedByteCount
= 0; /* <-- This clears the trailing unaligned bytes. */
2373 bs
->unalignedCache
= 0;
2375 #ifndef DR_FLAC_NO_CRC
2377 bs
->crc16CacheIgnoredBytes
= 0;
2382 static DRFLAC_INLINE drflac_bool32
drflac__read_uint32(drflac_bs
* bs
, unsigned int bitCount
, drflac_uint32
* pResultOut
)
2384 DRFLAC_ASSERT(bs
!= NULL
);
2385 DRFLAC_ASSERT(pResultOut
!= NULL
);
2386 DRFLAC_ASSERT(bitCount
> 0);
2387 DRFLAC_ASSERT(bitCount
<= 32);
2389 if (bs
->consumedBits
== DRFLAC_CACHE_L1_SIZE_BITS(bs
)) {
2390 if (!drflac__reload_cache(bs
)) {
2391 return DRFLAC_FALSE
;
2395 if (bitCount
<= DRFLAC_CACHE_L1_BITS_REMAINING(bs
)) {
2397 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
2398 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
2399 more optimal solution for this.
2402 *pResultOut
= (drflac_uint32
)DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs
, bitCount
);
2403 bs
->consumedBits
+= bitCount
;
2404 bs
->cache
<<= bitCount
;
2406 if (bitCount
< DRFLAC_CACHE_L1_SIZE_BITS(bs
)) {
2407 *pResultOut
= (drflac_uint32
)DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs
, bitCount
);
2408 bs
->consumedBits
+= bitCount
;
2409 bs
->cache
<<= bitCount
;
2411 /* Cannot shift by 32-bits, so need to do it differently. */
2412 *pResultOut
= (drflac_uint32
)bs
->cache
;
2413 bs
->consumedBits
= DRFLAC_CACHE_L1_SIZE_BITS(bs
);
2420 /* 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. */
2421 drflac_uint32 bitCountHi
= DRFLAC_CACHE_L1_BITS_REMAINING(bs
);
2422 drflac_uint32 bitCountLo
= bitCount
- bitCountHi
;
2423 drflac_uint32 resultHi
;
2425 DRFLAC_ASSERT(bitCountHi
> 0);
2426 DRFLAC_ASSERT(bitCountHi
< 32);
2427 resultHi
= (drflac_uint32
)DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs
, bitCountHi
);
2429 if (!drflac__reload_cache(bs
)) {
2430 return DRFLAC_FALSE
;
2433 *pResultOut
= (resultHi
<< bitCountLo
) | (drflac_uint32
)DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs
, bitCountLo
);
2434 bs
->consumedBits
+= bitCountLo
;
2435 bs
->cache
<<= bitCountLo
;
2440 static drflac_bool32
drflac__read_int32(drflac_bs
* bs
, unsigned int bitCount
, drflac_int32
* pResult
)
2442 drflac_uint32 result
;
2444 DRFLAC_ASSERT(bs
!= NULL
);
2445 DRFLAC_ASSERT(pResult
!= NULL
);
2446 DRFLAC_ASSERT(bitCount
> 0);
2447 DRFLAC_ASSERT(bitCount
<= 32);
2449 if (!drflac__read_uint32(bs
, bitCount
, &result
)) {
2450 return DRFLAC_FALSE
;
2453 /* Do not attempt to shift by 32 as it's undefined. */
2454 if (bitCount
< 32) {
2455 drflac_uint32 signbit
;
2456 signbit
= ((result
>> (bitCount
-1)) & 0x01);
2457 result
|= (~signbit
+ 1) << bitCount
;
2460 *pResult
= (drflac_int32
)result
;
2465 static drflac_bool32
drflac__read_uint64(drflac_bs
* bs
, unsigned int bitCount
, drflac_uint64
* pResultOut
)
2467 drflac_uint32 resultHi
;
2468 drflac_uint32 resultLo
;
2470 DRFLAC_ASSERT(bitCount
<= 64);
2471 DRFLAC_ASSERT(bitCount
> 32);
2473 if (!drflac__read_uint32(bs
, bitCount
- 32, &resultHi
)) {
2474 return DRFLAC_FALSE
;
2477 if (!drflac__read_uint32(bs
, 32, &resultLo
)) {
2478 return DRFLAC_FALSE
;
2481 *pResultOut
= (((drflac_uint64
)resultHi
) << 32) | ((drflac_uint64
)resultLo
);
2486 /* Function below is unused, but leaving it here in case I need to quickly add it again. */
2488 static drflac_bool32
drflac__read_int64(drflac_bs
* bs
, unsigned int bitCount
, drflac_int64
* pResultOut
)
2490 drflac_uint64 result
;
2491 drflac_uint64 signbit
;
2493 DRFLAC_ASSERT(bitCount
<= 64);
2495 if (!drflac__read_uint64(bs
, bitCount
, &result
)) {
2496 return DRFLAC_FALSE
;
2499 signbit
= ((result
>> (bitCount
-1)) & 0x01);
2500 result
|= (~signbit
+ 1) << bitCount
;
2502 *pResultOut
= (drflac_int64
)result
;
2507 static drflac_bool32
drflac__read_uint16(drflac_bs
* bs
, unsigned int bitCount
, drflac_uint16
* pResult
)
2509 drflac_uint32 result
;
2511 DRFLAC_ASSERT(bs
!= NULL
);
2512 DRFLAC_ASSERT(pResult
!= NULL
);
2513 DRFLAC_ASSERT(bitCount
> 0);
2514 DRFLAC_ASSERT(bitCount
<= 16);
2516 if (!drflac__read_uint32(bs
, bitCount
, &result
)) {
2517 return DRFLAC_FALSE
;
2520 *pResult
= (drflac_uint16
)result
;
2525 static drflac_bool32
drflac__read_int16(drflac_bs
* bs
, unsigned int bitCount
, drflac_int16
* pResult
)
2527 drflac_int32 result
;
2529 DRFLAC_ASSERT(bs
!= NULL
);
2530 DRFLAC_ASSERT(pResult
!= NULL
);
2531 DRFLAC_ASSERT(bitCount
> 0);
2532 DRFLAC_ASSERT(bitCount
<= 16);
2534 if (!drflac__read_int32(bs
, bitCount
, &result
)) {
2535 return DRFLAC_FALSE
;
2538 *pResult
= (drflac_int16
)result
;
2543 static drflac_bool32
drflac__read_uint8(drflac_bs
* bs
, unsigned int bitCount
, drflac_uint8
* pResult
)
2545 drflac_uint32 result
;
2547 DRFLAC_ASSERT(bs
!= NULL
);
2548 DRFLAC_ASSERT(pResult
!= NULL
);
2549 DRFLAC_ASSERT(bitCount
> 0);
2550 DRFLAC_ASSERT(bitCount
<= 8);
2552 if (!drflac__read_uint32(bs
, bitCount
, &result
)) {
2553 return DRFLAC_FALSE
;
2556 *pResult
= (drflac_uint8
)result
;
2560 static drflac_bool32
drflac__read_int8(drflac_bs
* bs
, unsigned int bitCount
, drflac_int8
* pResult
)
2562 drflac_int32 result
;
2564 DRFLAC_ASSERT(bs
!= NULL
);
2565 DRFLAC_ASSERT(pResult
!= NULL
);
2566 DRFLAC_ASSERT(bitCount
> 0);
2567 DRFLAC_ASSERT(bitCount
<= 8);
2569 if (!drflac__read_int32(bs
, bitCount
, &result
)) {
2570 return DRFLAC_FALSE
;
2573 *pResult
= (drflac_int8
)result
;
2578 static drflac_bool32
drflac__seek_bits(drflac_bs
* bs
, size_t bitsToSeek
)
2580 if (bitsToSeek
<= DRFLAC_CACHE_L1_BITS_REMAINING(bs
)) {
2581 bs
->consumedBits
+= (drflac_uint32
)bitsToSeek
;
2582 bs
->cache
<<= bitsToSeek
;
2585 /* It straddles the cached data. This function isn't called too frequently so I'm favouring simplicity here. */
2586 bitsToSeek
-= DRFLAC_CACHE_L1_BITS_REMAINING(bs
);
2587 bs
->consumedBits
+= DRFLAC_CACHE_L1_BITS_REMAINING(bs
);
2590 /* Simple case. Seek in groups of the same number as bits that fit within a cache line. */
2592 while (bitsToSeek
>= DRFLAC_CACHE_L1_SIZE_BITS(bs
)) {
2594 if (!drflac__read_uint64(bs
, DRFLAC_CACHE_L1_SIZE_BITS(bs
), &bin
)) {
2595 return DRFLAC_FALSE
;
2597 bitsToSeek
-= DRFLAC_CACHE_L1_SIZE_BITS(bs
);
2600 while (bitsToSeek
>= DRFLAC_CACHE_L1_SIZE_BITS(bs
)) {
2602 if (!drflac__read_uint32(bs
, DRFLAC_CACHE_L1_SIZE_BITS(bs
), &bin
)) {
2603 return DRFLAC_FALSE
;
2605 bitsToSeek
-= DRFLAC_CACHE_L1_SIZE_BITS(bs
);
2609 /* Whole leftover bytes. */
2610 while (bitsToSeek
>= 8) {
2612 if (!drflac__read_uint8(bs
, 8, &bin
)) {
2613 return DRFLAC_FALSE
;
2618 /* Leftover bits. */
2619 if (bitsToSeek
> 0) {
2621 if (!drflac__read_uint8(bs
, (drflac_uint32
)bitsToSeek
, &bin
)) {
2622 return DRFLAC_FALSE
;
2624 bitsToSeek
= 0; /* <-- Necessary for the assert below. */
2627 DRFLAC_ASSERT(bitsToSeek
== 0);
2633 /* 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. */
2634 static drflac_bool32
drflac__find_and_seek_to_next_sync_code(drflac_bs
* bs
)
2636 DRFLAC_ASSERT(bs
!= NULL
);
2639 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
2640 thing to do is align to the next byte.
2642 if (!drflac__seek_bits(bs
, DRFLAC_CACHE_L1_BITS_REMAINING(bs
) & 7)) {
2643 return DRFLAC_FALSE
;
2649 #ifndef DR_FLAC_NO_CRC
2650 drflac__reset_crc16(bs
);
2653 if (!drflac__read_uint8(bs
, 8, &hi
)) {
2654 return DRFLAC_FALSE
;
2659 if (!drflac__read_uint8(bs
, 6, &lo
)) {
2660 return DRFLAC_FALSE
;
2666 if (!drflac__seek_bits(bs
, DRFLAC_CACHE_L1_BITS_REMAINING(bs
) & 7)) {
2667 return DRFLAC_FALSE
;
2673 /* Should never get here. */
2674 /*return DRFLAC_FALSE;*/
2678 #if defined(DRFLAC_HAS_LZCNT_INTRINSIC)
2679 #define DRFLAC_IMPLEMENT_CLZ_LZCNT
2681 #if defined(_MSC_VER) && _MSC_VER >= 1400 && (defined(DRFLAC_X64) || defined(DRFLAC_X86)) && !defined(__clang__)
2682 #define DRFLAC_IMPLEMENT_CLZ_MSVC
2684 #if defined(__WATCOMC__) && defined(__386__)
2685 #define DRFLAC_IMPLEMENT_CLZ_WATCOM
2688 static DRFLAC_INLINE drflac_uint32
drflac__clz_software(drflac_cache_t x
)
2691 static drflac_uint32 clz_table_4
[] = {
2696 1, 1, 1, 1, 1, 1, 1, 1
2703 n
= clz_table_4
[x
>> (sizeof(x
)*8 - 4)];
2706 if ((x
& ((drflac_uint64
)0xFFFFFFFF << 32)) == 0) { n
= 32; x
<<= 32; }
2707 if ((x
& ((drflac_uint64
)0xFFFF0000 << 32)) == 0) { n
+= 16; x
<<= 16; }
2708 if ((x
& ((drflac_uint64
)0xFF000000 << 32)) == 0) { n
+= 8; x
<<= 8; }
2709 if ((x
& ((drflac_uint64
)0xF0000000 << 32)) == 0) { n
+= 4; x
<<= 4; }
2711 if ((x
& 0xFFFF0000) == 0) { n
= 16; x
<<= 16; }
2712 if ((x
& 0xFF000000) == 0) { n
+= 8; x
<<= 8; }
2713 if ((x
& 0xF0000000) == 0) { n
+= 4; x
<<= 4; }
2715 n
+= clz_table_4
[x
>> (sizeof(x
)*8 - 4)];
2721 #ifdef DRFLAC_IMPLEMENT_CLZ_LZCNT
2722 static DRFLAC_INLINE drflac_bool32
drflac__is_lzcnt_supported(void)
2724 /* Fast compile time check for ARM. */
2725 #if defined(DRFLAC_HAS_LZCNT_INTRINSIC) && defined(DRFLAC_ARM) && (defined(__ARM_ARCH) && __ARM_ARCH >= 5)
2728 /* If the compiler itself does not support the intrinsic then we'll need to return false. */
2729 #ifdef DRFLAC_HAS_LZCNT_INTRINSIC
2730 return drflac__gIsLZCNTSupported
;
2732 return DRFLAC_FALSE
;
2737 static DRFLAC_INLINE drflac_uint32
drflac__clz_lzcnt(drflac_cache_t x
)
2740 It's critical for competitive decoding performance that this function be highly optimal. With MSVC we can use the __lzcnt64() and __lzcnt() intrinsics
2741 to achieve good performance, however on GCC and Clang it's a little bit more annoying. The __builtin_clzl() and __builtin_clzll() intrinsics leave
2742 it undefined as to the return value when `x` is 0. We need this to be well defined as returning 32 or 64, depending on whether or not it's a 32- or
2743 64-bit build. To work around this we would need to add a conditional to check for the x = 0 case, but this creates unnecessary inefficiency. To work
2744 around this problem I have written some inline assembly to emit the LZCNT (x86) or CLZ (ARM) instruction directly which removes the need to include
2745 the conditional. This has worked well in the past, but for some reason Clang's MSVC compatible driver, clang-cl, does not seem to be handling this
2746 in the same way as the normal Clang driver. It seems that `clang-cl` is just outputting the wrong results sometimes, maybe due to some register
2749 I'm not sure if this is a bug with dr_flac's inlined assembly (most likely), a bug in `clang-cl` or just a misunderstanding on my part with inline
2750 assembly rules for `clang-cl`. If somebody can identify an error in dr_flac's inlined assembly I'm happy to get that fixed.
2752 Fortunately there is an easy workaround for this. Clang implements MSVC-specific intrinsics for compatibility. It also defines _MSC_VER for extra
2753 compatibility. We can therefore just check for _MSC_VER and use the MSVC intrinsic which, fortunately for us, Clang supports. It would still be nice
2754 to know how to fix the inlined assembly for correctness sake, however.
2757 #if defined(_MSC_VER) /*&& !defined(__clang__)*/ /* <-- Intentionally wanting Clang to use the MSVC __lzcnt64/__lzcnt intrinsics due to above ^. */
2759 return (drflac_uint32
)__lzcnt64(x
);
2761 return (drflac_uint32
)__lzcnt(x
);
2764 #if defined(__GNUC__) || defined(__clang__)
2765 #if defined(DRFLAC_X64)
2768 __asm__
__volatile__ (
2769 "lzcnt{ %1, %0| %0, %1}" : "=r"(r
) : "r"(x
) : "cc"
2772 return (drflac_uint32
)r
;
2774 #elif defined(DRFLAC_X86)
2777 __asm__
__volatile__ (
2778 "lzcnt{l %1, %0| %0, %1}" : "=r"(r
) : "r"(x
) : "cc"
2783 #elif defined(DRFLAC_ARM) && (defined(__ARM_ARCH) && __ARM_ARCH >= 5) && !defined(DRFLAC_64BIT) /* <-- I haven't tested 64-bit inline assembly, so only enabling this for the 32-bit build for now. */
2786 __asm__
__volatile__ (
2787 #if defined(DRFLAC_64BIT)
2788 "clz %w[out], %w[in]" : [out
]"=r"(r
) : [in
]"r"(x
) /* <-- This is untested. If someone in the community could test this, that would be appreciated! */
2790 "clz %[out], %[in]" : [out
]"=r"(r
) : [in
]"r"(x
)
2801 return (drflac_uint32
)__builtin_clzll((drflac_uint64
)x
);
2803 return (drflac_uint32
)__builtin_clzl((drflac_uint32
)x
);
2807 /* Unsupported compiler. */
2808 #error "This compiler does not support the lzcnt intrinsic."
2814 #ifdef DRFLAC_IMPLEMENT_CLZ_MSVC
2815 #include <intrin.h> /* For BitScanReverse(). */
2817 static DRFLAC_INLINE drflac_uint32
drflac__clz_msvc(drflac_cache_t x
)
2826 _BitScanReverse64((unsigned long*)&n
, x
);
2828 _BitScanReverse((unsigned long*)&n
, x
);
2830 return sizeof(x
)*8 - n
- 1;
2834 #ifdef DRFLAC_IMPLEMENT_CLZ_WATCOM
2835 static __inline drflac_uint32
drflac__clz_watcom (drflac_uint32
);
2836 #pragma aux drflac__clz_watcom = \
2839 parm [eax] nomemory \
2841 modify exact [eax] nomemory;
2844 static DRFLAC_INLINE drflac_uint32
drflac__clz(drflac_cache_t x
)
2846 #ifdef DRFLAC_IMPLEMENT_CLZ_LZCNT
2847 if (drflac__is_lzcnt_supported()) {
2848 return drflac__clz_lzcnt(x
);
2852 #ifdef DRFLAC_IMPLEMENT_CLZ_MSVC
2853 return drflac__clz_msvc(x
);
2854 #elif defined(DRFLAC_IMPLEMENT_CLZ_WATCOM)
2855 return (x
== 0) ? sizeof(x
)*8 : drflac__clz_watcom(x
);
2857 return drflac__clz_software(x
);
2863 static DRFLAC_INLINE drflac_bool32
drflac__seek_past_next_set_bit(drflac_bs
* bs
, unsigned int* pOffsetOut
)
2865 drflac_uint32 zeroCounter
= 0;
2866 drflac_uint32 setBitOffsetPlus1
;
2868 while (bs
->cache
== 0) {
2869 zeroCounter
+= (drflac_uint32
)DRFLAC_CACHE_L1_BITS_REMAINING(bs
);
2870 if (!drflac__reload_cache(bs
)) {
2871 return DRFLAC_FALSE
;
2875 setBitOffsetPlus1
= drflac__clz(bs
->cache
);
2876 setBitOffsetPlus1
+= 1;
2878 bs
->consumedBits
+= setBitOffsetPlus1
;
2879 bs
->cache
<<= setBitOffsetPlus1
;
2881 *pOffsetOut
= zeroCounter
+ setBitOffsetPlus1
- 1;
2887 static drflac_bool32
drflac__seek_to_byte(drflac_bs
* bs
, drflac_uint64 offsetFromStart
)
2889 DRFLAC_ASSERT(bs
!= NULL
);
2890 DRFLAC_ASSERT(offsetFromStart
> 0);
2893 Seeking from the start is not quite as trivial as it sounds because the onSeek callback takes a signed 32-bit integer (which
2894 is intentional because it simplifies the implementation of the onSeek callbacks), however offsetFromStart is unsigned 64-bit.
2895 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.
2897 if (offsetFromStart
> 0x7FFFFFFF) {
2898 drflac_uint64 bytesRemaining
= offsetFromStart
;
2899 if (!bs
->onSeek(bs
->pUserData
, 0x7FFFFFFF, drflac_seek_origin_start
)) {
2900 return DRFLAC_FALSE
;
2902 bytesRemaining
-= 0x7FFFFFFF;
2904 while (bytesRemaining
> 0x7FFFFFFF) {
2905 if (!bs
->onSeek(bs
->pUserData
, 0x7FFFFFFF, drflac_seek_origin_current
)) {
2906 return DRFLAC_FALSE
;
2908 bytesRemaining
-= 0x7FFFFFFF;
2911 if (bytesRemaining
> 0) {
2912 if (!bs
->onSeek(bs
->pUserData
, (int)bytesRemaining
, drflac_seek_origin_current
)) {
2913 return DRFLAC_FALSE
;
2917 if (!bs
->onSeek(bs
->pUserData
, (int)offsetFromStart
, drflac_seek_origin_start
)) {
2918 return DRFLAC_FALSE
;
2922 /* The cache should be reset to force a reload of fresh data from the client. */
2923 drflac__reset_cache(bs
);
2928 static drflac_result
drflac__read_utf8_coded_number(drflac_bs
* bs
, drflac_uint64
* pNumberOut
, drflac_uint8
* pCRCOut
)
2931 drflac_uint64 result
;
2932 drflac_uint8 utf8
[7] = {0};
2936 DRFLAC_ASSERT(bs
!= NULL
);
2937 DRFLAC_ASSERT(pNumberOut
!= NULL
);
2938 DRFLAC_ASSERT(pCRCOut
!= NULL
);
2942 if (!drflac__read_uint8(bs
, 8, utf8
)) {
2944 return DRFLAC_AT_END
;
2946 crc
= drflac_crc8(crc
, utf8
[0], 8);
2948 if ((utf8
[0] & 0x80) == 0) {
2949 *pNumberOut
= utf8
[0];
2951 return DRFLAC_SUCCESS
;
2955 if ((utf8
[0] & 0xE0) == 0xC0) {
2957 } else if ((utf8
[0] & 0xF0) == 0xE0) {
2959 } else if ((utf8
[0] & 0xF8) == 0xF0) {
2961 } else if ((utf8
[0] & 0xFC) == 0xF8) {
2963 } else if ((utf8
[0] & 0xFE) == 0xFC) {
2965 } else if ((utf8
[0] & 0xFF) == 0xFE) {
2969 return DRFLAC_CRC_MISMATCH
; /* Bad UTF-8 encoding. */
2972 /* Read extra bytes. */
2973 DRFLAC_ASSERT(byteCount
> 1);
2975 result
= (drflac_uint64
)(utf8
[0] & (0xFF >> (byteCount
+ 1)));
2976 for (i
= 1; i
< byteCount
; ++i
) {
2977 if (!drflac__read_uint8(bs
, 8, utf8
+ i
)) {
2979 return DRFLAC_AT_END
;
2981 crc
= drflac_crc8(crc
, utf8
[i
], 8);
2983 result
= (result
<< 6) | (utf8
[i
] & 0x3F);
2986 *pNumberOut
= result
;
2988 return DRFLAC_SUCCESS
;
2994 The next two functions are responsible for calculating the prediction.
2996 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
2997 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.
2999 static DRFLAC_INLINE drflac_int32
drflac__calculate_prediction_32(drflac_uint32 order
, drflac_int32 shift
, const drflac_int32
* coefficients
, drflac_int32
* pDecodedSamples
)
3001 drflac_int32 prediction
= 0;
3003 DRFLAC_ASSERT(order
<= 32);
3005 /* 32-bit version. */
3007 /* VC++ optimizes this to a single jmp. I've not yet verified this for other compilers. */
3010 case 32: prediction
+= coefficients
[31] * pDecodedSamples
[-32];
3011 case 31: prediction
+= coefficients
[30] * pDecodedSamples
[-31];
3012 case 30: prediction
+= coefficients
[29] * pDecodedSamples
[-30];
3013 case 29: prediction
+= coefficients
[28] * pDecodedSamples
[-29];
3014 case 28: prediction
+= coefficients
[27] * pDecodedSamples
[-28];
3015 case 27: prediction
+= coefficients
[26] * pDecodedSamples
[-27];
3016 case 26: prediction
+= coefficients
[25] * pDecodedSamples
[-26];
3017 case 25: prediction
+= coefficients
[24] * pDecodedSamples
[-25];
3018 case 24: prediction
+= coefficients
[23] * pDecodedSamples
[-24];
3019 case 23: prediction
+= coefficients
[22] * pDecodedSamples
[-23];
3020 case 22: prediction
+= coefficients
[21] * pDecodedSamples
[-22];
3021 case 21: prediction
+= coefficients
[20] * pDecodedSamples
[-21];
3022 case 20: prediction
+= coefficients
[19] * pDecodedSamples
[-20];
3023 case 19: prediction
+= coefficients
[18] * pDecodedSamples
[-19];
3024 case 18: prediction
+= coefficients
[17] * pDecodedSamples
[-18];
3025 case 17: prediction
+= coefficients
[16] * pDecodedSamples
[-17];
3026 case 16: prediction
+= coefficients
[15] * pDecodedSamples
[-16];
3027 case 15: prediction
+= coefficients
[14] * pDecodedSamples
[-15];
3028 case 14: prediction
+= coefficients
[13] * pDecodedSamples
[-14];
3029 case 13: prediction
+= coefficients
[12] * pDecodedSamples
[-13];
3030 case 12: prediction
+= coefficients
[11] * pDecodedSamples
[-12];
3031 case 11: prediction
+= coefficients
[10] * pDecodedSamples
[-11];
3032 case 10: prediction
+= coefficients
[ 9] * pDecodedSamples
[-10];
3033 case 9: prediction
+= coefficients
[ 8] * pDecodedSamples
[- 9];
3034 case 8: prediction
+= coefficients
[ 7] * pDecodedSamples
[- 8];
3035 case 7: prediction
+= coefficients
[ 6] * pDecodedSamples
[- 7];
3036 case 6: prediction
+= coefficients
[ 5] * pDecodedSamples
[- 6];
3037 case 5: prediction
+= coefficients
[ 4] * pDecodedSamples
[- 5];
3038 case 4: prediction
+= coefficients
[ 3] * pDecodedSamples
[- 4];
3039 case 3: prediction
+= coefficients
[ 2] * pDecodedSamples
[- 3];
3040 case 2: prediction
+= coefficients
[ 1] * pDecodedSamples
[- 2];
3041 case 1: prediction
+= coefficients
[ 0] * pDecodedSamples
[- 1];
3044 return (drflac_int32
)(prediction
>> shift
);
3047 static DRFLAC_INLINE drflac_int32
drflac__calculate_prediction_64(drflac_uint32 order
, drflac_int32 shift
, const drflac_int32
* coefficients
, drflac_int32
* pDecodedSamples
)
3049 drflac_int64 prediction
;
3051 DRFLAC_ASSERT(order
<= 32);
3053 /* 64-bit version. */
3055 /* This method is faster on the 32-bit build when compiling with VC++. See note below. */
3056 #ifndef DRFLAC_64BIT
3059 prediction
= coefficients
[0] * (drflac_int64
)pDecodedSamples
[-1];
3060 prediction
+= coefficients
[1] * (drflac_int64
)pDecodedSamples
[-2];
3061 prediction
+= coefficients
[2] * (drflac_int64
)pDecodedSamples
[-3];
3062 prediction
+= coefficients
[3] * (drflac_int64
)pDecodedSamples
[-4];
3063 prediction
+= coefficients
[4] * (drflac_int64
)pDecodedSamples
[-5];
3064 prediction
+= coefficients
[5] * (drflac_int64
)pDecodedSamples
[-6];
3065 prediction
+= coefficients
[6] * (drflac_int64
)pDecodedSamples
[-7];
3066 prediction
+= coefficients
[7] * (drflac_int64
)pDecodedSamples
[-8];
3068 else if (order
== 7)
3070 prediction
= coefficients
[0] * (drflac_int64
)pDecodedSamples
[-1];
3071 prediction
+= coefficients
[1] * (drflac_int64
)pDecodedSamples
[-2];
3072 prediction
+= coefficients
[2] * (drflac_int64
)pDecodedSamples
[-3];
3073 prediction
+= coefficients
[3] * (drflac_int64
)pDecodedSamples
[-4];
3074 prediction
+= coefficients
[4] * (drflac_int64
)pDecodedSamples
[-5];
3075 prediction
+= coefficients
[5] * (drflac_int64
)pDecodedSamples
[-6];
3076 prediction
+= coefficients
[6] * (drflac_int64
)pDecodedSamples
[-7];
3078 else if (order
== 3)
3080 prediction
= coefficients
[0] * (drflac_int64
)pDecodedSamples
[-1];
3081 prediction
+= coefficients
[1] * (drflac_int64
)pDecodedSamples
[-2];
3082 prediction
+= coefficients
[2] * (drflac_int64
)pDecodedSamples
[-3];
3084 else if (order
== 6)
3086 prediction
= coefficients
[0] * (drflac_int64
)pDecodedSamples
[-1];
3087 prediction
+= coefficients
[1] * (drflac_int64
)pDecodedSamples
[-2];
3088 prediction
+= coefficients
[2] * (drflac_int64
)pDecodedSamples
[-3];
3089 prediction
+= coefficients
[3] * (drflac_int64
)pDecodedSamples
[-4];
3090 prediction
+= coefficients
[4] * (drflac_int64
)pDecodedSamples
[-5];
3091 prediction
+= coefficients
[5] * (drflac_int64
)pDecodedSamples
[-6];
3093 else if (order
== 5)
3095 prediction
= coefficients
[0] * (drflac_int64
)pDecodedSamples
[-1];
3096 prediction
+= coefficients
[1] * (drflac_int64
)pDecodedSamples
[-2];
3097 prediction
+= coefficients
[2] * (drflac_int64
)pDecodedSamples
[-3];
3098 prediction
+= coefficients
[3] * (drflac_int64
)pDecodedSamples
[-4];
3099 prediction
+= coefficients
[4] * (drflac_int64
)pDecodedSamples
[-5];
3101 else if (order
== 4)
3103 prediction
= coefficients
[0] * (drflac_int64
)pDecodedSamples
[-1];
3104 prediction
+= coefficients
[1] * (drflac_int64
)pDecodedSamples
[-2];
3105 prediction
+= coefficients
[2] * (drflac_int64
)pDecodedSamples
[-3];
3106 prediction
+= coefficients
[3] * (drflac_int64
)pDecodedSamples
[-4];
3108 else if (order
== 12)
3110 prediction
= coefficients
[0] * (drflac_int64
)pDecodedSamples
[-1];
3111 prediction
+= coefficients
[1] * (drflac_int64
)pDecodedSamples
[-2];
3112 prediction
+= coefficients
[2] * (drflac_int64
)pDecodedSamples
[-3];
3113 prediction
+= coefficients
[3] * (drflac_int64
)pDecodedSamples
[-4];
3114 prediction
+= coefficients
[4] * (drflac_int64
)pDecodedSamples
[-5];
3115 prediction
+= coefficients
[5] * (drflac_int64
)pDecodedSamples
[-6];
3116 prediction
+= coefficients
[6] * (drflac_int64
)pDecodedSamples
[-7];
3117 prediction
+= coefficients
[7] * (drflac_int64
)pDecodedSamples
[-8];
3118 prediction
+= coefficients
[8] * (drflac_int64
)pDecodedSamples
[-9];
3119 prediction
+= coefficients
[9] * (drflac_int64
)pDecodedSamples
[-10];
3120 prediction
+= coefficients
[10] * (drflac_int64
)pDecodedSamples
[-11];
3121 prediction
+= coefficients
[11] * (drflac_int64
)pDecodedSamples
[-12];
3123 else if (order
== 2)
3125 prediction
= coefficients
[0] * (drflac_int64
)pDecodedSamples
[-1];
3126 prediction
+= coefficients
[1] * (drflac_int64
)pDecodedSamples
[-2];
3128 else if (order
== 1)
3130 prediction
= coefficients
[0] * (drflac_int64
)pDecodedSamples
[-1];
3132 else if (order
== 10)
3134 prediction
= coefficients
[0] * (drflac_int64
)pDecodedSamples
[-1];
3135 prediction
+= coefficients
[1] * (drflac_int64
)pDecodedSamples
[-2];
3136 prediction
+= coefficients
[2] * (drflac_int64
)pDecodedSamples
[-3];
3137 prediction
+= coefficients
[3] * (drflac_int64
)pDecodedSamples
[-4];
3138 prediction
+= coefficients
[4] * (drflac_int64
)pDecodedSamples
[-5];
3139 prediction
+= coefficients
[5] * (drflac_int64
)pDecodedSamples
[-6];
3140 prediction
+= coefficients
[6] * (drflac_int64
)pDecodedSamples
[-7];
3141 prediction
+= coefficients
[7] * (drflac_int64
)pDecodedSamples
[-8];
3142 prediction
+= coefficients
[8] * (drflac_int64
)pDecodedSamples
[-9];
3143 prediction
+= coefficients
[9] * (drflac_int64
)pDecodedSamples
[-10];
3145 else if (order
== 9)
3147 prediction
= coefficients
[0] * (drflac_int64
)pDecodedSamples
[-1];
3148 prediction
+= coefficients
[1] * (drflac_int64
)pDecodedSamples
[-2];
3149 prediction
+= coefficients
[2] * (drflac_int64
)pDecodedSamples
[-3];
3150 prediction
+= coefficients
[3] * (drflac_int64
)pDecodedSamples
[-4];
3151 prediction
+= coefficients
[4] * (drflac_int64
)pDecodedSamples
[-5];
3152 prediction
+= coefficients
[5] * (drflac_int64
)pDecodedSamples
[-6];
3153 prediction
+= coefficients
[6] * (drflac_int64
)pDecodedSamples
[-7];
3154 prediction
+= coefficients
[7] * (drflac_int64
)pDecodedSamples
[-8];
3155 prediction
+= coefficients
[8] * (drflac_int64
)pDecodedSamples
[-9];
3157 else if (order
== 11)
3159 prediction
= coefficients
[0] * (drflac_int64
)pDecodedSamples
[-1];
3160 prediction
+= coefficients
[1] * (drflac_int64
)pDecodedSamples
[-2];
3161 prediction
+= coefficients
[2] * (drflac_int64
)pDecodedSamples
[-3];
3162 prediction
+= coefficients
[3] * (drflac_int64
)pDecodedSamples
[-4];
3163 prediction
+= coefficients
[4] * (drflac_int64
)pDecodedSamples
[-5];
3164 prediction
+= coefficients
[5] * (drflac_int64
)pDecodedSamples
[-6];
3165 prediction
+= coefficients
[6] * (drflac_int64
)pDecodedSamples
[-7];
3166 prediction
+= coefficients
[7] * (drflac_int64
)pDecodedSamples
[-8];
3167 prediction
+= coefficients
[8] * (drflac_int64
)pDecodedSamples
[-9];
3168 prediction
+= coefficients
[9] * (drflac_int64
)pDecodedSamples
[-10];
3169 prediction
+= coefficients
[10] * (drflac_int64
)pDecodedSamples
[-11];
3176 for (j
= 0; j
< (int)order
; ++j
) {
3177 prediction
+= coefficients
[j
] * (drflac_int64
)pDecodedSamples
[-j
-1];
3183 VC++ optimizes this to a single jmp instruction, but only the 64-bit build. The 32-bit build generates less efficient code for some
3184 reason. The ugly version above is faster so we'll just switch between the two depending on the target platform.
3190 case 32: prediction
+= coefficients
[31] * (drflac_int64
)pDecodedSamples
[-32];
3191 case 31: prediction
+= coefficients
[30] * (drflac_int64
)pDecodedSamples
[-31];
3192 case 30: prediction
+= coefficients
[29] * (drflac_int64
)pDecodedSamples
[-30];
3193 case 29: prediction
+= coefficients
[28] * (drflac_int64
)pDecodedSamples
[-29];
3194 case 28: prediction
+= coefficients
[27] * (drflac_int64
)pDecodedSamples
[-28];
3195 case 27: prediction
+= coefficients
[26] * (drflac_int64
)pDecodedSamples
[-27];
3196 case 26: prediction
+= coefficients
[25] * (drflac_int64
)pDecodedSamples
[-26];
3197 case 25: prediction
+= coefficients
[24] * (drflac_int64
)pDecodedSamples
[-25];
3198 case 24: prediction
+= coefficients
[23] * (drflac_int64
)pDecodedSamples
[-24];
3199 case 23: prediction
+= coefficients
[22] * (drflac_int64
)pDecodedSamples
[-23];
3200 case 22: prediction
+= coefficients
[21] * (drflac_int64
)pDecodedSamples
[-22];
3201 case 21: prediction
+= coefficients
[20] * (drflac_int64
)pDecodedSamples
[-21];
3202 case 20: prediction
+= coefficients
[19] * (drflac_int64
)pDecodedSamples
[-20];
3203 case 19: prediction
+= coefficients
[18] * (drflac_int64
)pDecodedSamples
[-19];
3204 case 18: prediction
+= coefficients
[17] * (drflac_int64
)pDecodedSamples
[-18];
3205 case 17: prediction
+= coefficients
[16] * (drflac_int64
)pDecodedSamples
[-17];
3206 case 16: prediction
+= coefficients
[15] * (drflac_int64
)pDecodedSamples
[-16];
3207 case 15: prediction
+= coefficients
[14] * (drflac_int64
)pDecodedSamples
[-15];
3208 case 14: prediction
+= coefficients
[13] * (drflac_int64
)pDecodedSamples
[-14];
3209 case 13: prediction
+= coefficients
[12] * (drflac_int64
)pDecodedSamples
[-13];
3210 case 12: prediction
+= coefficients
[11] * (drflac_int64
)pDecodedSamples
[-12];
3211 case 11: prediction
+= coefficients
[10] * (drflac_int64
)pDecodedSamples
[-11];
3212 case 10: prediction
+= coefficients
[ 9] * (drflac_int64
)pDecodedSamples
[-10];
3213 case 9: prediction
+= coefficients
[ 8] * (drflac_int64
)pDecodedSamples
[- 9];
3214 case 8: prediction
+= coefficients
[ 7] * (drflac_int64
)pDecodedSamples
[- 8];
3215 case 7: prediction
+= coefficients
[ 6] * (drflac_int64
)pDecodedSamples
[- 7];
3216 case 6: prediction
+= coefficients
[ 5] * (drflac_int64
)pDecodedSamples
[- 6];
3217 case 5: prediction
+= coefficients
[ 4] * (drflac_int64
)pDecodedSamples
[- 5];
3218 case 4: prediction
+= coefficients
[ 3] * (drflac_int64
)pDecodedSamples
[- 4];
3219 case 3: prediction
+= coefficients
[ 2] * (drflac_int64
)pDecodedSamples
[- 3];
3220 case 2: prediction
+= coefficients
[ 1] * (drflac_int64
)pDecodedSamples
[- 2];
3221 case 1: prediction
+= coefficients
[ 0] * (drflac_int64
)pDecodedSamples
[- 1];
3225 return (drflac_int32
)(prediction
>> shift
);
3231 Reference implementation for reading and decoding samples with residual. This is intentionally left unoptimized for the
3232 sake of readability and should only be used as a reference.
3234 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
)
3238 DRFLAC_ASSERT(bs
!= NULL
);
3239 DRFLAC_ASSERT(pSamplesOut
!= NULL
);
3241 for (i
= 0; i
< count
; ++i
) {
3242 drflac_uint32 zeroCounter
= 0;
3245 if (!drflac__read_uint8(bs
, 1, &bit
)) {
3246 return DRFLAC_FALSE
;
3256 drflac_uint32 decodedRice
;
3257 if (riceParam
> 0) {
3258 if (!drflac__read_uint32(bs
, riceParam
, &decodedRice
)) {
3259 return DRFLAC_FALSE
;
3265 decodedRice
|= (zeroCounter
<< riceParam
);
3266 if ((decodedRice
& 0x01)) {
3267 decodedRice
= ~(decodedRice
>> 1);
3269 decodedRice
= (decodedRice
>> 1);
3273 if (bitsPerSample
+shift
>= 32) {
3274 pSamplesOut
[i
] = decodedRice
+ drflac__calculate_prediction_64(order
, shift
, coefficients
, pSamplesOut
+ i
);
3276 pSamplesOut
[i
] = decodedRice
+ drflac__calculate_prediction_32(order
, shift
, coefficients
, pSamplesOut
+ i
);
3285 static drflac_bool32
drflac__read_rice_parts__reference(drflac_bs
* bs
, drflac_uint8 riceParam
, drflac_uint32
* pZeroCounterOut
, drflac_uint32
* pRiceParamPartOut
)
3287 drflac_uint32 zeroCounter
= 0;
3288 drflac_uint32 decodedRice
;
3292 if (!drflac__read_uint8(bs
, 1, &bit
)) {
3293 return DRFLAC_FALSE
;
3303 if (riceParam
> 0) {
3304 if (!drflac__read_uint32(bs
, riceParam
, &decodedRice
)) {
3305 return DRFLAC_FALSE
;
3311 *pZeroCounterOut
= zeroCounter
;
3312 *pRiceParamPartOut
= decodedRice
;
3318 static DRFLAC_INLINE drflac_bool32
drflac__read_rice_parts(drflac_bs
* bs
, drflac_uint8 riceParam
, drflac_uint32
* pZeroCounterOut
, drflac_uint32
* pRiceParamPartOut
)
3320 drflac_cache_t riceParamMask
;
3321 drflac_uint32 zeroCounter
;
3322 drflac_uint32 setBitOffsetPlus1
;
3323 drflac_uint32 riceParamPart
;
3324 drflac_uint32 riceLength
;
3326 DRFLAC_ASSERT(riceParam
> 0); /* <-- riceParam should never be 0. drflac__read_rice_parts__param_equals_zero() should be used instead for this case. */
3328 riceParamMask
= DRFLAC_CACHE_L1_SELECTION_MASK(riceParam
);
3331 while (bs
->cache
== 0) {
3332 zeroCounter
+= (drflac_uint32
)DRFLAC_CACHE_L1_BITS_REMAINING(bs
);
3333 if (!drflac__reload_cache(bs
)) {
3334 return DRFLAC_FALSE
;
3338 setBitOffsetPlus1
= drflac__clz(bs
->cache
);
3339 zeroCounter
+= setBitOffsetPlus1
;
3340 setBitOffsetPlus1
+= 1;
3342 riceLength
= setBitOffsetPlus1
+ riceParam
;
3343 if (riceLength
< DRFLAC_CACHE_L1_BITS_REMAINING(bs
)) {
3344 riceParamPart
= (drflac_uint32
)((bs
->cache
& (riceParamMask
>> setBitOffsetPlus1
)) >> DRFLAC_CACHE_L1_SELECTION_SHIFT(bs
, riceLength
));
3346 bs
->consumedBits
+= riceLength
;
3347 bs
->cache
<<= riceLength
;
3349 drflac_uint32 bitCountLo
;
3350 drflac_cache_t resultHi
;
3352 bs
->consumedBits
+= riceLength
;
3353 bs
->cache
<<= setBitOffsetPlus1
& (DRFLAC_CACHE_L1_SIZE_BITS(bs
)-1); /* <-- Equivalent to "if (setBitOffsetPlus1 < DRFLAC_CACHE_L1_SIZE_BITS(bs)) { bs->cache <<= setBitOffsetPlus1; }" */
3355 /* 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. */
3356 bitCountLo
= bs
->consumedBits
- DRFLAC_CACHE_L1_SIZE_BITS(bs
);
3357 resultHi
= DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs
, riceParam
); /* <-- Use DRFLAC_CACHE_L1_SELECT_AND_SHIFT_SAFE() if ever this function allows riceParam=0. */
3359 if (bs
->nextL2Line
< DRFLAC_CACHE_L2_LINE_COUNT(bs
)) {
3360 #ifndef DR_FLAC_NO_CRC
3361 drflac__update_crc16(bs
);
3363 bs
->cache
= drflac__be2host__cache_line(bs
->cacheL2
[bs
->nextL2Line
++]);
3364 bs
->consumedBits
= 0;
3365 #ifndef DR_FLAC_NO_CRC
3366 bs
->crc16Cache
= bs
->cache
;
3369 /* Slow path. We need to fetch more data from the client. */
3370 if (!drflac__reload_cache(bs
)) {
3371 return DRFLAC_FALSE
;
3375 riceParamPart
= (drflac_uint32
)(resultHi
| DRFLAC_CACHE_L1_SELECT_AND_SHIFT_SAFE(bs
, bitCountLo
));
3377 bs
->consumedBits
+= bitCountLo
;
3378 bs
->cache
<<= bitCountLo
;
3381 pZeroCounterOut
[0] = zeroCounter
;
3382 pRiceParamPartOut
[0] = riceParamPart
;
3388 static DRFLAC_INLINE drflac_bool32
drflac__read_rice_parts_x1(drflac_bs
* bs
, drflac_uint8 riceParam
, drflac_uint32
* pZeroCounterOut
, drflac_uint32
* pRiceParamPartOut
)
3390 drflac_uint32 riceParamPlus1
= riceParam
+ 1;
3391 /*drflac_cache_t riceParamPlus1Mask = DRFLAC_CACHE_L1_SELECTION_MASK(riceParamPlus1);*/
3392 drflac_uint32 riceParamPlus1Shift
= DRFLAC_CACHE_L1_SELECTION_SHIFT(bs
, riceParamPlus1
);
3393 drflac_uint32 riceParamPlus1MaxConsumedBits
= DRFLAC_CACHE_L1_SIZE_BITS(bs
) - riceParamPlus1
;
3396 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
3397 no idea how this will work in practice...
3399 drflac_cache_t bs_cache
= bs
->cache
;
3400 drflac_uint32 bs_consumedBits
= bs
->consumedBits
;
3402 /* The first thing to do is find the first unset bit. Most likely a bit will be set in the current cache line. */
3403 drflac_uint32 lzcount
= drflac__clz(bs_cache
);
3404 if (lzcount
< sizeof(bs_cache
)*8) {
3405 pZeroCounterOut
[0] = lzcount
;
3408 It is most likely that the riceParam part (which comes after the zero counter) is also on this cache line. When extracting
3409 this, we include the set bit from the unary coded part because it simplifies cache management. This bit will be handled
3410 outside of this function at a higher level.
3412 extract_rice_param_part
:
3413 bs_cache
<<= lzcount
;
3414 bs_consumedBits
+= lzcount
;
3416 if (bs_consumedBits
<= riceParamPlus1MaxConsumedBits
) {
3417 /* Getting here means the rice parameter part is wholly contained within the current cache line. */
3418 pRiceParamPartOut
[0] = (drflac_uint32
)(bs_cache
>> riceParamPlus1Shift
);
3419 bs_cache
<<= riceParamPlus1
;
3420 bs_consumedBits
+= riceParamPlus1
;
3422 drflac_uint32 riceParamPartHi
;
3423 drflac_uint32 riceParamPartLo
;
3424 drflac_uint32 riceParamPartLoBitCount
;
3427 Getting here means the rice parameter part straddles the cache line. We need to read from the tail of the current cache
3428 line, reload the cache, and then combine it with the head of the next cache line.
3431 /* Grab the high part of the rice parameter part. */
3432 riceParamPartHi
= (drflac_uint32
)(bs_cache
>> riceParamPlus1Shift
);
3434 /* Before reloading the cache we need to grab the size in bits of the low part. */
3435 riceParamPartLoBitCount
= bs_consumedBits
- riceParamPlus1MaxConsumedBits
;
3436 DRFLAC_ASSERT(riceParamPartLoBitCount
> 0 && riceParamPartLoBitCount
< 32);
3438 /* Now reload the cache. */
3439 if (bs
->nextL2Line
< DRFLAC_CACHE_L2_LINE_COUNT(bs
)) {
3440 #ifndef DR_FLAC_NO_CRC
3441 drflac__update_crc16(bs
);
3443 bs_cache
= drflac__be2host__cache_line(bs
->cacheL2
[bs
->nextL2Line
++]);
3444 bs_consumedBits
= riceParamPartLoBitCount
;
3445 #ifndef DR_FLAC_NO_CRC
3446 bs
->crc16Cache
= bs_cache
;
3449 /* Slow path. We need to fetch more data from the client. */
3450 if (!drflac__reload_cache(bs
)) {
3451 return DRFLAC_FALSE
;
3454 bs_cache
= bs
->cache
;
3455 bs_consumedBits
= bs
->consumedBits
+ riceParamPartLoBitCount
;
3458 /* We should now have enough information to construct the rice parameter part. */
3459 riceParamPartLo
= (drflac_uint32
)(bs_cache
>> (DRFLAC_CACHE_L1_SELECTION_SHIFT(bs
, riceParamPartLoBitCount
)));
3460 pRiceParamPartOut
[0] = riceParamPartHi
| riceParamPartLo
;
3462 bs_cache
<<= riceParamPartLoBitCount
;
3466 Getting here means there are no bits set on the cache line. This is a less optimal case because we just wasted a call
3467 to drflac__clz() and we need to reload the cache.
3469 drflac_uint32 zeroCounter
= (drflac_uint32
)(DRFLAC_CACHE_L1_SIZE_BITS(bs
) - bs_consumedBits
);
3471 if (bs
->nextL2Line
< DRFLAC_CACHE_L2_LINE_COUNT(bs
)) {
3472 #ifndef DR_FLAC_NO_CRC
3473 drflac__update_crc16(bs
);
3475 bs_cache
= drflac__be2host__cache_line(bs
->cacheL2
[bs
->nextL2Line
++]);
3476 bs_consumedBits
= 0;
3477 #ifndef DR_FLAC_NO_CRC
3478 bs
->crc16Cache
= bs_cache
;
3481 /* Slow path. We need to fetch more data from the client. */
3482 if (!drflac__reload_cache(bs
)) {
3483 return DRFLAC_FALSE
;
3486 bs_cache
= bs
->cache
;
3487 bs_consumedBits
= bs
->consumedBits
;
3490 lzcount
= drflac__clz(bs_cache
);
3491 zeroCounter
+= lzcount
;
3493 if (lzcount
< sizeof(bs_cache
)*8) {
3498 pZeroCounterOut
[0] = zeroCounter
;
3499 goto extract_rice_param_part
;
3502 /* Make sure the cache is restored at the end of it all. */
3503 bs
->cache
= bs_cache
;
3504 bs
->consumedBits
= bs_consumedBits
;
3509 static DRFLAC_INLINE drflac_bool32
drflac__seek_rice_parts(drflac_bs
* bs
, drflac_uint8 riceParam
)
3511 drflac_uint32 riceParamPlus1
= riceParam
+ 1;
3512 drflac_uint32 riceParamPlus1MaxConsumedBits
= DRFLAC_CACHE_L1_SIZE_BITS(bs
) - riceParamPlus1
;
3515 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
3516 no idea how this will work in practice...
3518 drflac_cache_t bs_cache
= bs
->cache
;
3519 drflac_uint32 bs_consumedBits
= bs
->consumedBits
;
3521 /* The first thing to do is find the first unset bit. Most likely a bit will be set in the current cache line. */
3522 drflac_uint32 lzcount
= drflac__clz(bs_cache
);
3523 if (lzcount
< sizeof(bs_cache
)*8) {
3525 It is most likely that the riceParam part (which comes after the zero counter) is also on this cache line. When extracting
3526 this, we include the set bit from the unary coded part because it simplifies cache management. This bit will be handled
3527 outside of this function at a higher level.
3529 extract_rice_param_part
:
3530 bs_cache
<<= lzcount
;
3531 bs_consumedBits
+= lzcount
;
3533 if (bs_consumedBits
<= riceParamPlus1MaxConsumedBits
) {
3534 /* Getting here means the rice parameter part is wholly contained within the current cache line. */
3535 bs_cache
<<= riceParamPlus1
;
3536 bs_consumedBits
+= riceParamPlus1
;
3539 Getting here means the rice parameter part straddles the cache line. We need to read from the tail of the current cache
3540 line, reload the cache, and then combine it with the head of the next cache line.
3543 /* Before reloading the cache we need to grab the size in bits of the low part. */
3544 drflac_uint32 riceParamPartLoBitCount
= bs_consumedBits
- riceParamPlus1MaxConsumedBits
;
3545 DRFLAC_ASSERT(riceParamPartLoBitCount
> 0 && riceParamPartLoBitCount
< 32);
3547 /* Now reload the cache. */
3548 if (bs
->nextL2Line
< DRFLAC_CACHE_L2_LINE_COUNT(bs
)) {
3549 #ifndef DR_FLAC_NO_CRC
3550 drflac__update_crc16(bs
);
3552 bs_cache
= drflac__be2host__cache_line(bs
->cacheL2
[bs
->nextL2Line
++]);
3553 bs_consumedBits
= riceParamPartLoBitCount
;
3554 #ifndef DR_FLAC_NO_CRC
3555 bs
->crc16Cache
= bs_cache
;
3558 /* Slow path. We need to fetch more data from the client. */
3559 if (!drflac__reload_cache(bs
)) {
3560 return DRFLAC_FALSE
;
3563 bs_cache
= bs
->cache
;
3564 bs_consumedBits
= bs
->consumedBits
+ riceParamPartLoBitCount
;
3567 bs_cache
<<= riceParamPartLoBitCount
;
3571 Getting here means there are no bits set on the cache line. This is a less optimal case because we just wasted a call
3572 to drflac__clz() and we need to reload the cache.
3575 if (bs
->nextL2Line
< DRFLAC_CACHE_L2_LINE_COUNT(bs
)) {
3576 #ifndef DR_FLAC_NO_CRC
3577 drflac__update_crc16(bs
);
3579 bs_cache
= drflac__be2host__cache_line(bs
->cacheL2
[bs
->nextL2Line
++]);
3580 bs_consumedBits
= 0;
3581 #ifndef DR_FLAC_NO_CRC
3582 bs
->crc16Cache
= bs_cache
;
3585 /* Slow path. We need to fetch more data from the client. */
3586 if (!drflac__reload_cache(bs
)) {
3587 return DRFLAC_FALSE
;
3590 bs_cache
= bs
->cache
;
3591 bs_consumedBits
= bs
->consumedBits
;
3594 lzcount
= drflac__clz(bs_cache
);
3595 if (lzcount
< sizeof(bs_cache
)*8) {
3600 goto extract_rice_param_part
;
3603 /* Make sure the cache is restored at the end of it all. */
3604 bs
->cache
= bs_cache
;
3605 bs
->consumedBits
= bs_consumedBits
;
3611 static drflac_bool32
drflac__decode_samples_with_residual__rice__scalar_zeroorder(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
)
3613 drflac_uint32 t
[2] = {0x00000000, 0xFFFFFFFF};
3614 drflac_uint32 zeroCountPart0
;
3615 drflac_uint32 riceParamPart0
;
3616 drflac_uint32 riceParamMask
;
3619 DRFLAC_ASSERT(bs
!= NULL
);
3620 DRFLAC_ASSERT(pSamplesOut
!= NULL
);
3622 (void)bitsPerSample
;
3627 riceParamMask
= (drflac_uint32
)~((~0UL) << riceParam
);
3631 /* Rice extraction. */
3632 if (!drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountPart0
, &riceParamPart0
)) {
3633 return DRFLAC_FALSE
;
3636 /* Rice reconstruction. */
3637 riceParamPart0
&= riceParamMask
;
3638 riceParamPart0
|= (zeroCountPart0
<< riceParam
);
3639 riceParamPart0
= (riceParamPart0
>> 1) ^ t
[riceParamPart0
& 0x01];
3641 pSamplesOut
[i
] = riceParamPart0
;
3649 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
)
3651 drflac_uint32 t
[2] = {0x00000000, 0xFFFFFFFF};
3652 drflac_uint32 zeroCountPart0
= 0;
3653 drflac_uint32 zeroCountPart1
= 0;
3654 drflac_uint32 zeroCountPart2
= 0;
3655 drflac_uint32 zeroCountPart3
= 0;
3656 drflac_uint32 riceParamPart0
= 0;
3657 drflac_uint32 riceParamPart1
= 0;
3658 drflac_uint32 riceParamPart2
= 0;
3659 drflac_uint32 riceParamPart3
= 0;
3660 drflac_uint32 riceParamMask
;
3661 const drflac_int32
* pSamplesOutEnd
;
3664 DRFLAC_ASSERT(bs
!= NULL
);
3665 DRFLAC_ASSERT(pSamplesOut
!= NULL
);
3668 return drflac__decode_samples_with_residual__rice__scalar_zeroorder(bs
, bitsPerSample
, count
, riceParam
, order
, shift
, coefficients
, pSamplesOut
);
3671 riceParamMask
= (drflac_uint32
)~((~0UL) << riceParam
);
3672 pSamplesOutEnd
= pSamplesOut
+ (count
& ~3);
3674 if (bitsPerSample
+shift
> 32) {
3675 while (pSamplesOut
< pSamplesOutEnd
) {
3677 Rice extraction. It's faster to do this one at a time against local variables than it is to use the x4 version
3678 against an array. Not sure why, but perhaps it's making more efficient use of registers?
3680 if (!drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountPart0
, &riceParamPart0
) ||
3681 !drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountPart1
, &riceParamPart1
) ||
3682 !drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountPart2
, &riceParamPart2
) ||
3683 !drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountPart3
, &riceParamPart3
)) {
3684 return DRFLAC_FALSE
;
3687 riceParamPart0
&= riceParamMask
;
3688 riceParamPart1
&= riceParamMask
;
3689 riceParamPart2
&= riceParamMask
;
3690 riceParamPart3
&= riceParamMask
;
3692 riceParamPart0
|= (zeroCountPart0
<< riceParam
);
3693 riceParamPart1
|= (zeroCountPart1
<< riceParam
);
3694 riceParamPart2
|= (zeroCountPart2
<< riceParam
);
3695 riceParamPart3
|= (zeroCountPart3
<< riceParam
);
3697 riceParamPart0
= (riceParamPart0
>> 1) ^ t
[riceParamPart0
& 0x01];
3698 riceParamPart1
= (riceParamPart1
>> 1) ^ t
[riceParamPart1
& 0x01];
3699 riceParamPart2
= (riceParamPart2
>> 1) ^ t
[riceParamPart2
& 0x01];
3700 riceParamPart3
= (riceParamPart3
>> 1) ^ t
[riceParamPart3
& 0x01];
3702 pSamplesOut
[0] = riceParamPart0
+ drflac__calculate_prediction_64(order
, shift
, coefficients
, pSamplesOut
+ 0);
3703 pSamplesOut
[1] = riceParamPart1
+ drflac__calculate_prediction_64(order
, shift
, coefficients
, pSamplesOut
+ 1);
3704 pSamplesOut
[2] = riceParamPart2
+ drflac__calculate_prediction_64(order
, shift
, coefficients
, pSamplesOut
+ 2);
3705 pSamplesOut
[3] = riceParamPart3
+ drflac__calculate_prediction_64(order
, shift
, coefficients
, pSamplesOut
+ 3);
3710 while (pSamplesOut
< pSamplesOutEnd
) {
3711 if (!drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountPart0
, &riceParamPart0
) ||
3712 !drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountPart1
, &riceParamPart1
) ||
3713 !drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountPart2
, &riceParamPart2
) ||
3714 !drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountPart3
, &riceParamPart3
)) {
3715 return DRFLAC_FALSE
;
3718 riceParamPart0
&= riceParamMask
;
3719 riceParamPart1
&= riceParamMask
;
3720 riceParamPart2
&= riceParamMask
;
3721 riceParamPart3
&= riceParamMask
;
3723 riceParamPart0
|= (zeroCountPart0
<< riceParam
);
3724 riceParamPart1
|= (zeroCountPart1
<< riceParam
);
3725 riceParamPart2
|= (zeroCountPart2
<< riceParam
);
3726 riceParamPart3
|= (zeroCountPart3
<< riceParam
);
3728 riceParamPart0
= (riceParamPart0
>> 1) ^ t
[riceParamPart0
& 0x01];
3729 riceParamPart1
= (riceParamPart1
>> 1) ^ t
[riceParamPart1
& 0x01];
3730 riceParamPart2
= (riceParamPart2
>> 1) ^ t
[riceParamPart2
& 0x01];
3731 riceParamPart3
= (riceParamPart3
>> 1) ^ t
[riceParamPart3
& 0x01];
3733 pSamplesOut
[0] = riceParamPart0
+ drflac__calculate_prediction_32(order
, shift
, coefficients
, pSamplesOut
+ 0);
3734 pSamplesOut
[1] = riceParamPart1
+ drflac__calculate_prediction_32(order
, shift
, coefficients
, pSamplesOut
+ 1);
3735 pSamplesOut
[2] = riceParamPart2
+ drflac__calculate_prediction_32(order
, shift
, coefficients
, pSamplesOut
+ 2);
3736 pSamplesOut
[3] = riceParamPart3
+ drflac__calculate_prediction_32(order
, shift
, coefficients
, pSamplesOut
+ 3);
3744 /* Rice extraction. */
3745 if (!drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountPart0
, &riceParamPart0
)) {
3746 return DRFLAC_FALSE
;
3749 /* Rice reconstruction. */
3750 riceParamPart0
&= riceParamMask
;
3751 riceParamPart0
|= (zeroCountPart0
<< riceParam
);
3752 riceParamPart0
= (riceParamPart0
>> 1) ^ t
[riceParamPart0
& 0x01];
3753 /*riceParamPart0 = (riceParamPart0 >> 1) ^ (~(riceParamPart0 & 0x01) + 1);*/
3755 /* Sample reconstruction. */
3756 if (bitsPerSample
+shift
> 32) {
3757 pSamplesOut
[0] = riceParamPart0
+ drflac__calculate_prediction_64(order
, shift
, coefficients
, pSamplesOut
+ 0);
3759 pSamplesOut
[0] = riceParamPart0
+ drflac__calculate_prediction_32(order
, shift
, coefficients
, pSamplesOut
+ 0);
3769 #if defined(DRFLAC_SUPPORT_SSE2)
3770 static DRFLAC_INLINE __m128i
drflac__mm_packs_interleaved_epi32(__m128i a
, __m128i b
)
3775 r
= _mm_packs_epi32(a
, b
);
3777 /* a3a2 a1a0 b3b2 b1b0 -> a3a2 b3b2 a1a0 b1b0 */
3778 r
= _mm_shuffle_epi32(r
, _MM_SHUFFLE(3, 1, 2, 0));
3780 /* a3a2 b3b2 a1a0 b1b0 -> a3b3 a2b2 a1b1 a0b0 */
3781 r
= _mm_shufflehi_epi16(r
, _MM_SHUFFLE(3, 1, 2, 0));
3782 r
= _mm_shufflelo_epi16(r
, _MM_SHUFFLE(3, 1, 2, 0));
3788 #if defined(DRFLAC_SUPPORT_SSE41)
3789 static DRFLAC_INLINE __m128i
drflac__mm_not_si128(__m128i a
)
3791 return _mm_xor_si128(a
, _mm_cmpeq_epi32(_mm_setzero_si128(), _mm_setzero_si128()));
3794 static DRFLAC_INLINE __m128i
drflac__mm_hadd_epi32(__m128i x
)
3796 __m128i x64
= _mm_add_epi32(x
, _mm_shuffle_epi32(x
, _MM_SHUFFLE(1, 0, 3, 2)));
3797 __m128i x32
= _mm_shufflelo_epi16(x64
, _MM_SHUFFLE(1, 0, 3, 2));
3798 return _mm_add_epi32(x64
, x32
);
3801 static DRFLAC_INLINE __m128i
drflac__mm_hadd_epi64(__m128i x
)
3803 return _mm_add_epi64(x
, _mm_shuffle_epi32(x
, _MM_SHUFFLE(1, 0, 3, 2)));
3806 static DRFLAC_INLINE __m128i
drflac__mm_srai_epi64(__m128i x
, int count
)
3809 To simplify this we are assuming count < 32. This restriction allows us to work on a low side and a high side. The low side
3810 is shifted with zero bits, whereas the right side is shifted with sign bits.
3812 __m128i lo
= _mm_srli_epi64(x
, count
);
3813 __m128i hi
= _mm_srai_epi32(x
, count
);
3815 hi
= _mm_and_si128(hi
, _mm_set_epi32(0xFFFFFFFF, 0, 0xFFFFFFFF, 0)); /* The high part needs to have the low part cleared. */
3817 return _mm_or_si128(lo
, hi
);
3820 static drflac_bool32
drflac__decode_samples_with_residual__rice__sse41_32(drflac_bs
* bs
, drflac_uint32 count
, drflac_uint8 riceParam
, drflac_uint32 order
, drflac_int32 shift
, const drflac_int32
* coefficients
, drflac_int32
* pSamplesOut
)
3823 drflac_uint32 riceParamMask
;
3824 drflac_int32
* pDecodedSamples
= pSamplesOut
;
3825 drflac_int32
* pDecodedSamplesEnd
= pSamplesOut
+ (count
& ~3);
3826 drflac_uint32 zeroCountParts0
= 0;
3827 drflac_uint32 zeroCountParts1
= 0;
3828 drflac_uint32 zeroCountParts2
= 0;
3829 drflac_uint32 zeroCountParts3
= 0;
3830 drflac_uint32 riceParamParts0
= 0;
3831 drflac_uint32 riceParamParts1
= 0;
3832 drflac_uint32 riceParamParts2
= 0;
3833 drflac_uint32 riceParamParts3
= 0;
3834 __m128i coefficients128_0
;
3835 __m128i coefficients128_4
;
3836 __m128i coefficients128_8
;
3837 __m128i samples128_0
;
3838 __m128i samples128_4
;
3839 __m128i samples128_8
;
3840 __m128i riceParamMask128
;
3842 const drflac_uint32 t
[2] = {0x00000000, 0xFFFFFFFF};
3844 riceParamMask
= (drflac_uint32
)~((~0UL) << riceParam
);
3845 riceParamMask128
= _mm_set1_epi32(riceParamMask
);
3848 coefficients128_0
= _mm_setzero_si128();
3849 coefficients128_4
= _mm_setzero_si128();
3850 coefficients128_8
= _mm_setzero_si128();
3852 samples128_0
= _mm_setzero_si128();
3853 samples128_4
= _mm_setzero_si128();
3854 samples128_8
= _mm_setzero_si128();
3857 Pre-loading the coefficients and prior samples is annoying because we need to ensure we don't try reading more than
3858 what's available in the input buffers. It would be convenient to use a fall-through switch to do this, but this results
3859 in strict aliasing warnings with GCC. To work around this I'm just doing something hacky. This feels a bit convoluted
3860 so I think there's opportunity for this to be simplified.
3864 int runningOrder
= order
;
3867 if (runningOrder
>= 4) {
3868 coefficients128_0
= _mm_loadu_si128((const __m128i
*)(coefficients
+ 0));
3869 samples128_0
= _mm_loadu_si128((const __m128i
*)(pSamplesOut
- 4));
3872 switch (runningOrder
) {
3873 case 3: coefficients128_0
= _mm_set_epi32(0, coefficients
[2], coefficients
[1], coefficients
[0]); samples128_0
= _mm_set_epi32(pSamplesOut
[-1], pSamplesOut
[-2], pSamplesOut
[-3], 0); break;
3874 case 2: coefficients128_0
= _mm_set_epi32(0, 0, coefficients
[1], coefficients
[0]); samples128_0
= _mm_set_epi32(pSamplesOut
[-1], pSamplesOut
[-2], 0, 0); break;
3875 case 1: coefficients128_0
= _mm_set_epi32(0, 0, 0, coefficients
[0]); samples128_0
= _mm_set_epi32(pSamplesOut
[-1], 0, 0, 0); break;
3881 if (runningOrder
>= 4) {
3882 coefficients128_4
= _mm_loadu_si128((const __m128i
*)(coefficients
+ 4));
3883 samples128_4
= _mm_loadu_si128((const __m128i
*)(pSamplesOut
- 8));
3886 switch (runningOrder
) {
3887 case 3: coefficients128_4
= _mm_set_epi32(0, coefficients
[6], coefficients
[5], coefficients
[4]); samples128_4
= _mm_set_epi32(pSamplesOut
[-5], pSamplesOut
[-6], pSamplesOut
[-7], 0); break;
3888 case 2: coefficients128_4
= _mm_set_epi32(0, 0, coefficients
[5], coefficients
[4]); samples128_4
= _mm_set_epi32(pSamplesOut
[-5], pSamplesOut
[-6], 0, 0); break;
3889 case 1: coefficients128_4
= _mm_set_epi32(0, 0, 0, coefficients
[4]); samples128_4
= _mm_set_epi32(pSamplesOut
[-5], 0, 0, 0); break;
3895 if (runningOrder
== 4) {
3896 coefficients128_8
= _mm_loadu_si128((const __m128i
*)(coefficients
+ 8));
3897 samples128_8
= _mm_loadu_si128((const __m128i
*)(pSamplesOut
- 12));
3900 switch (runningOrder
) {
3901 case 3: coefficients128_8
= _mm_set_epi32(0, coefficients
[10], coefficients
[9], coefficients
[8]); samples128_8
= _mm_set_epi32(pSamplesOut
[-9], pSamplesOut
[-10], pSamplesOut
[-11], 0); break;
3902 case 2: coefficients128_8
= _mm_set_epi32(0, 0, coefficients
[9], coefficients
[8]); samples128_8
= _mm_set_epi32(pSamplesOut
[-9], pSamplesOut
[-10], 0, 0); break;
3903 case 1: coefficients128_8
= _mm_set_epi32(0, 0, 0, coefficients
[8]); samples128_8
= _mm_set_epi32(pSamplesOut
[-9], 0, 0, 0); break;
3908 /* Coefficients need to be shuffled for our streaming algorithm below to work. Samples are already in the correct order from the loading routine above. */
3909 coefficients128_0
= _mm_shuffle_epi32(coefficients128_0
, _MM_SHUFFLE(0, 1, 2, 3));
3910 coefficients128_4
= _mm_shuffle_epi32(coefficients128_4
, _MM_SHUFFLE(0, 1, 2, 3));
3911 coefficients128_8
= _mm_shuffle_epi32(coefficients128_8
, _MM_SHUFFLE(0, 1, 2, 3));
3914 /* This causes strict-aliasing warnings with GCC. */
3917 case 12: ((drflac_int32
*)&coefficients128_8
)[0] = coefficients
[11]; ((drflac_int32
*)&samples128_8
)[0] = pDecodedSamples
[-12];
3918 case 11: ((drflac_int32
*)&coefficients128_8
)[1] = coefficients
[10]; ((drflac_int32
*)&samples128_8
)[1] = pDecodedSamples
[-11];
3919 case 10: ((drflac_int32
*)&coefficients128_8
)[2] = coefficients
[ 9]; ((drflac_int32
*)&samples128_8
)[2] = pDecodedSamples
[-10];
3920 case 9: ((drflac_int32
*)&coefficients128_8
)[3] = coefficients
[ 8]; ((drflac_int32
*)&samples128_8
)[3] = pDecodedSamples
[- 9];
3921 case 8: ((drflac_int32
*)&coefficients128_4
)[0] = coefficients
[ 7]; ((drflac_int32
*)&samples128_4
)[0] = pDecodedSamples
[- 8];
3922 case 7: ((drflac_int32
*)&coefficients128_4
)[1] = coefficients
[ 6]; ((drflac_int32
*)&samples128_4
)[1] = pDecodedSamples
[- 7];
3923 case 6: ((drflac_int32
*)&coefficients128_4
)[2] = coefficients
[ 5]; ((drflac_int32
*)&samples128_4
)[2] = pDecodedSamples
[- 6];
3924 case 5: ((drflac_int32
*)&coefficients128_4
)[3] = coefficients
[ 4]; ((drflac_int32
*)&samples128_4
)[3] = pDecodedSamples
[- 5];
3925 case 4: ((drflac_int32
*)&coefficients128_0
)[0] = coefficients
[ 3]; ((drflac_int32
*)&samples128_0
)[0] = pDecodedSamples
[- 4];
3926 case 3: ((drflac_int32
*)&coefficients128_0
)[1] = coefficients
[ 2]; ((drflac_int32
*)&samples128_0
)[1] = pDecodedSamples
[- 3];
3927 case 2: ((drflac_int32
*)&coefficients128_0
)[2] = coefficients
[ 1]; ((drflac_int32
*)&samples128_0
)[2] = pDecodedSamples
[- 2];
3928 case 1: ((drflac_int32
*)&coefficients128_0
)[3] = coefficients
[ 0]; ((drflac_int32
*)&samples128_0
)[3] = pDecodedSamples
[- 1];
3932 /* For this version we are doing one sample at a time. */
3933 while (pDecodedSamples
< pDecodedSamplesEnd
) {
3934 __m128i prediction128
;
3935 __m128i zeroCountPart128
;
3936 __m128i riceParamPart128
;
3938 if (!drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountParts0
, &riceParamParts0
) ||
3939 !drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountParts1
, &riceParamParts1
) ||
3940 !drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountParts2
, &riceParamParts2
) ||
3941 !drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountParts3
, &riceParamParts3
)) {
3942 return DRFLAC_FALSE
;
3945 zeroCountPart128
= _mm_set_epi32(zeroCountParts3
, zeroCountParts2
, zeroCountParts1
, zeroCountParts0
);
3946 riceParamPart128
= _mm_set_epi32(riceParamParts3
, riceParamParts2
, riceParamParts1
, riceParamParts0
);
3948 riceParamPart128
= _mm_and_si128(riceParamPart128
, riceParamMask128
);
3949 riceParamPart128
= _mm_or_si128(riceParamPart128
, _mm_slli_epi32(zeroCountPart128
, riceParam
));
3950 riceParamPart128
= _mm_xor_si128(_mm_srli_epi32(riceParamPart128
, 1), _mm_add_epi32(drflac__mm_not_si128(_mm_and_si128(riceParamPart128
, _mm_set1_epi32(0x01))), _mm_set1_epi32(0x01))); /* <-- SSE2 compatible */
3951 /*riceParamPart128 = _mm_xor_si128(_mm_srli_epi32(riceParamPart128, 1), _mm_mullo_epi32(_mm_and_si128(riceParamPart128, _mm_set1_epi32(0x01)), _mm_set1_epi32(0xFFFFFFFF)));*/ /* <-- Only supported from SSE4.1 and is slower in my testing... */
3954 for (i
= 0; i
< 4; i
+= 1) {
3955 prediction128
= _mm_mullo_epi32(coefficients128_0
, samples128_0
);
3957 /* Horizontal add and shift. */
3958 prediction128
= drflac__mm_hadd_epi32(prediction128
);
3959 prediction128
= _mm_srai_epi32(prediction128
, shift
);
3960 prediction128
= _mm_add_epi32(riceParamPart128
, prediction128
);
3962 samples128_0
= _mm_alignr_epi8(prediction128
, samples128_0
, 4);
3963 riceParamPart128
= _mm_alignr_epi8(_mm_setzero_si128(), riceParamPart128
, 4);
3965 } else if (order
<= 8) {
3966 for (i
= 0; i
< 4; i
+= 1) {
3967 prediction128
= _mm_mullo_epi32(coefficients128_4
, samples128_4
);
3968 prediction128
= _mm_add_epi32(prediction128
, _mm_mullo_epi32(coefficients128_0
, samples128_0
));
3970 /* Horizontal add and shift. */
3971 prediction128
= drflac__mm_hadd_epi32(prediction128
);
3972 prediction128
= _mm_srai_epi32(prediction128
, shift
);
3973 prediction128
= _mm_add_epi32(riceParamPart128
, prediction128
);
3975 samples128_4
= _mm_alignr_epi8(samples128_0
, samples128_4
, 4);
3976 samples128_0
= _mm_alignr_epi8(prediction128
, samples128_0
, 4);
3977 riceParamPart128
= _mm_alignr_epi8(_mm_setzero_si128(), riceParamPart128
, 4);
3980 for (i
= 0; i
< 4; i
+= 1) {
3981 prediction128
= _mm_mullo_epi32(coefficients128_8
, samples128_8
);
3982 prediction128
= _mm_add_epi32(prediction128
, _mm_mullo_epi32(coefficients128_4
, samples128_4
));
3983 prediction128
= _mm_add_epi32(prediction128
, _mm_mullo_epi32(coefficients128_0
, samples128_0
));
3985 /* Horizontal add and shift. */
3986 prediction128
= drflac__mm_hadd_epi32(prediction128
);
3987 prediction128
= _mm_srai_epi32(prediction128
, shift
);
3988 prediction128
= _mm_add_epi32(riceParamPart128
, prediction128
);
3990 samples128_8
= _mm_alignr_epi8(samples128_4
, samples128_8
, 4);
3991 samples128_4
= _mm_alignr_epi8(samples128_0
, samples128_4
, 4);
3992 samples128_0
= _mm_alignr_epi8(prediction128
, samples128_0
, 4);
3993 riceParamPart128
= _mm_alignr_epi8(_mm_setzero_si128(), riceParamPart128
, 4);
3997 /* We store samples in groups of 4. */
3998 _mm_storeu_si128((__m128i
*)pDecodedSamples
, samples128_0
);
3999 pDecodedSamples
+= 4;
4002 /* Make sure we process the last few samples. */
4004 while (i
< (int)count
) {
4005 /* Rice extraction. */
4006 if (!drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountParts0
, &riceParamParts0
)) {
4007 return DRFLAC_FALSE
;
4010 /* Rice reconstruction. */
4011 riceParamParts0
&= riceParamMask
;
4012 riceParamParts0
|= (zeroCountParts0
<< riceParam
);
4013 riceParamParts0
= (riceParamParts0
>> 1) ^ t
[riceParamParts0
& 0x01];
4015 /* Sample reconstruction. */
4016 pDecodedSamples
[0] = riceParamParts0
+ drflac__calculate_prediction_32(order
, shift
, coefficients
, pDecodedSamples
);
4019 pDecodedSamples
+= 1;
4025 static drflac_bool32
drflac__decode_samples_with_residual__rice__sse41_64(drflac_bs
* bs
, drflac_uint32 count
, drflac_uint8 riceParam
, drflac_uint32 order
, drflac_int32 shift
, const drflac_int32
* coefficients
, drflac_int32
* pSamplesOut
)
4028 drflac_uint32 riceParamMask
;
4029 drflac_int32
* pDecodedSamples
= pSamplesOut
;
4030 drflac_int32
* pDecodedSamplesEnd
= pSamplesOut
+ (count
& ~3);
4031 drflac_uint32 zeroCountParts0
= 0;
4032 drflac_uint32 zeroCountParts1
= 0;
4033 drflac_uint32 zeroCountParts2
= 0;
4034 drflac_uint32 zeroCountParts3
= 0;
4035 drflac_uint32 riceParamParts0
= 0;
4036 drflac_uint32 riceParamParts1
= 0;
4037 drflac_uint32 riceParamParts2
= 0;
4038 drflac_uint32 riceParamParts3
= 0;
4039 __m128i coefficients128_0
;
4040 __m128i coefficients128_4
;
4041 __m128i coefficients128_8
;
4042 __m128i samples128_0
;
4043 __m128i samples128_4
;
4044 __m128i samples128_8
;
4045 __m128i prediction128
;
4046 __m128i riceParamMask128
;
4048 const drflac_uint32 t
[2] = {0x00000000, 0xFFFFFFFF};
4050 DRFLAC_ASSERT(order
<= 12);
4052 riceParamMask
= (drflac_uint32
)~((~0UL) << riceParam
);
4053 riceParamMask128
= _mm_set1_epi32(riceParamMask
);
4055 prediction128
= _mm_setzero_si128();
4058 coefficients128_0
= _mm_setzero_si128();
4059 coefficients128_4
= _mm_setzero_si128();
4060 coefficients128_8
= _mm_setzero_si128();
4062 samples128_0
= _mm_setzero_si128();
4063 samples128_4
= _mm_setzero_si128();
4064 samples128_8
= _mm_setzero_si128();
4068 int runningOrder
= order
;
4071 if (runningOrder
>= 4) {
4072 coefficients128_0
= _mm_loadu_si128((const __m128i
*)(coefficients
+ 0));
4073 samples128_0
= _mm_loadu_si128((const __m128i
*)(pSamplesOut
- 4));
4076 switch (runningOrder
) {
4077 case 3: coefficients128_0
= _mm_set_epi32(0, coefficients
[2], coefficients
[1], coefficients
[0]); samples128_0
= _mm_set_epi32(pSamplesOut
[-1], pSamplesOut
[-2], pSamplesOut
[-3], 0); break;
4078 case 2: coefficients128_0
= _mm_set_epi32(0, 0, coefficients
[1], coefficients
[0]); samples128_0
= _mm_set_epi32(pSamplesOut
[-1], pSamplesOut
[-2], 0, 0); break;
4079 case 1: coefficients128_0
= _mm_set_epi32(0, 0, 0, coefficients
[0]); samples128_0
= _mm_set_epi32(pSamplesOut
[-1], 0, 0, 0); break;
4085 if (runningOrder
>= 4) {
4086 coefficients128_4
= _mm_loadu_si128((const __m128i
*)(coefficients
+ 4));
4087 samples128_4
= _mm_loadu_si128((const __m128i
*)(pSamplesOut
- 8));
4090 switch (runningOrder
) {
4091 case 3: coefficients128_4
= _mm_set_epi32(0, coefficients
[6], coefficients
[5], coefficients
[4]); samples128_4
= _mm_set_epi32(pSamplesOut
[-5], pSamplesOut
[-6], pSamplesOut
[-7], 0); break;
4092 case 2: coefficients128_4
= _mm_set_epi32(0, 0, coefficients
[5], coefficients
[4]); samples128_4
= _mm_set_epi32(pSamplesOut
[-5], pSamplesOut
[-6], 0, 0); break;
4093 case 1: coefficients128_4
= _mm_set_epi32(0, 0, 0, coefficients
[4]); samples128_4
= _mm_set_epi32(pSamplesOut
[-5], 0, 0, 0); break;
4099 if (runningOrder
== 4) {
4100 coefficients128_8
= _mm_loadu_si128((const __m128i
*)(coefficients
+ 8));
4101 samples128_8
= _mm_loadu_si128((const __m128i
*)(pSamplesOut
- 12));
4104 switch (runningOrder
) {
4105 case 3: coefficients128_8
= _mm_set_epi32(0, coefficients
[10], coefficients
[9], coefficients
[8]); samples128_8
= _mm_set_epi32(pSamplesOut
[-9], pSamplesOut
[-10], pSamplesOut
[-11], 0); break;
4106 case 2: coefficients128_8
= _mm_set_epi32(0, 0, coefficients
[9], coefficients
[8]); samples128_8
= _mm_set_epi32(pSamplesOut
[-9], pSamplesOut
[-10], 0, 0); break;
4107 case 1: coefficients128_8
= _mm_set_epi32(0, 0, 0, coefficients
[8]); samples128_8
= _mm_set_epi32(pSamplesOut
[-9], 0, 0, 0); break;
4112 /* Coefficients need to be shuffled for our streaming algorithm below to work. Samples are already in the correct order from the loading routine above. */
4113 coefficients128_0
= _mm_shuffle_epi32(coefficients128_0
, _MM_SHUFFLE(0, 1, 2, 3));
4114 coefficients128_4
= _mm_shuffle_epi32(coefficients128_4
, _MM_SHUFFLE(0, 1, 2, 3));
4115 coefficients128_8
= _mm_shuffle_epi32(coefficients128_8
, _MM_SHUFFLE(0, 1, 2, 3));
4120 case 12: ((drflac_int32
*)&coefficients128_8
)[0] = coefficients
[11]; ((drflac_int32
*)&samples128_8
)[0] = pDecodedSamples
[-12];
4121 case 11: ((drflac_int32
*)&coefficients128_8
)[1] = coefficients
[10]; ((drflac_int32
*)&samples128_8
)[1] = pDecodedSamples
[-11];
4122 case 10: ((drflac_int32
*)&coefficients128_8
)[2] = coefficients
[ 9]; ((drflac_int32
*)&samples128_8
)[2] = pDecodedSamples
[-10];
4123 case 9: ((drflac_int32
*)&coefficients128_8
)[3] = coefficients
[ 8]; ((drflac_int32
*)&samples128_8
)[3] = pDecodedSamples
[- 9];
4124 case 8: ((drflac_int32
*)&coefficients128_4
)[0] = coefficients
[ 7]; ((drflac_int32
*)&samples128_4
)[0] = pDecodedSamples
[- 8];
4125 case 7: ((drflac_int32
*)&coefficients128_4
)[1] = coefficients
[ 6]; ((drflac_int32
*)&samples128_4
)[1] = pDecodedSamples
[- 7];
4126 case 6: ((drflac_int32
*)&coefficients128_4
)[2] = coefficients
[ 5]; ((drflac_int32
*)&samples128_4
)[2] = pDecodedSamples
[- 6];
4127 case 5: ((drflac_int32
*)&coefficients128_4
)[3] = coefficients
[ 4]; ((drflac_int32
*)&samples128_4
)[3] = pDecodedSamples
[- 5];
4128 case 4: ((drflac_int32
*)&coefficients128_0
)[0] = coefficients
[ 3]; ((drflac_int32
*)&samples128_0
)[0] = pDecodedSamples
[- 4];
4129 case 3: ((drflac_int32
*)&coefficients128_0
)[1] = coefficients
[ 2]; ((drflac_int32
*)&samples128_0
)[1] = pDecodedSamples
[- 3];
4130 case 2: ((drflac_int32
*)&coefficients128_0
)[2] = coefficients
[ 1]; ((drflac_int32
*)&samples128_0
)[2] = pDecodedSamples
[- 2];
4131 case 1: ((drflac_int32
*)&coefficients128_0
)[3] = coefficients
[ 0]; ((drflac_int32
*)&samples128_0
)[3] = pDecodedSamples
[- 1];
4135 /* For this version we are doing one sample at a time. */
4136 while (pDecodedSamples
< pDecodedSamplesEnd
) {
4137 __m128i zeroCountPart128
;
4138 __m128i riceParamPart128
;
4140 if (!drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountParts0
, &riceParamParts0
) ||
4141 !drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountParts1
, &riceParamParts1
) ||
4142 !drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountParts2
, &riceParamParts2
) ||
4143 !drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountParts3
, &riceParamParts3
)) {
4144 return DRFLAC_FALSE
;
4147 zeroCountPart128
= _mm_set_epi32(zeroCountParts3
, zeroCountParts2
, zeroCountParts1
, zeroCountParts0
);
4148 riceParamPart128
= _mm_set_epi32(riceParamParts3
, riceParamParts2
, riceParamParts1
, riceParamParts0
);
4150 riceParamPart128
= _mm_and_si128(riceParamPart128
, riceParamMask128
);
4151 riceParamPart128
= _mm_or_si128(riceParamPart128
, _mm_slli_epi32(zeroCountPart128
, riceParam
));
4152 riceParamPart128
= _mm_xor_si128(_mm_srli_epi32(riceParamPart128
, 1), _mm_add_epi32(drflac__mm_not_si128(_mm_and_si128(riceParamPart128
, _mm_set1_epi32(1))), _mm_set1_epi32(1)));
4154 for (i
= 0; i
< 4; i
+= 1) {
4155 prediction128
= _mm_xor_si128(prediction128
, prediction128
); /* Reset to 0. */
4160 case 11: prediction128
= _mm_add_epi64(prediction128
, _mm_mul_epi32(_mm_shuffle_epi32(coefficients128_8
, _MM_SHUFFLE(1, 1, 0, 0)), _mm_shuffle_epi32(samples128_8
, _MM_SHUFFLE(1, 1, 0, 0))));
4162 case 9: prediction128
= _mm_add_epi64(prediction128
, _mm_mul_epi32(_mm_shuffle_epi32(coefficients128_8
, _MM_SHUFFLE(3, 3, 2, 2)), _mm_shuffle_epi32(samples128_8
, _MM_SHUFFLE(3, 3, 2, 2))));
4164 case 7: prediction128
= _mm_add_epi64(prediction128
, _mm_mul_epi32(_mm_shuffle_epi32(coefficients128_4
, _MM_SHUFFLE(1, 1, 0, 0)), _mm_shuffle_epi32(samples128_4
, _MM_SHUFFLE(1, 1, 0, 0))));
4166 case 5: prediction128
= _mm_add_epi64(prediction128
, _mm_mul_epi32(_mm_shuffle_epi32(coefficients128_4
, _MM_SHUFFLE(3, 3, 2, 2)), _mm_shuffle_epi32(samples128_4
, _MM_SHUFFLE(3, 3, 2, 2))));
4168 case 3: prediction128
= _mm_add_epi64(prediction128
, _mm_mul_epi32(_mm_shuffle_epi32(coefficients128_0
, _MM_SHUFFLE(1, 1, 0, 0)), _mm_shuffle_epi32(samples128_0
, _MM_SHUFFLE(1, 1, 0, 0))));
4170 case 1: prediction128
= _mm_add_epi64(prediction128
, _mm_mul_epi32(_mm_shuffle_epi32(coefficients128_0
, _MM_SHUFFLE(3, 3, 2, 2)), _mm_shuffle_epi32(samples128_0
, _MM_SHUFFLE(3, 3, 2, 2))));
4173 /* Horizontal add and shift. */
4174 prediction128
= drflac__mm_hadd_epi64(prediction128
);
4175 prediction128
= drflac__mm_srai_epi64(prediction128
, shift
);
4176 prediction128
= _mm_add_epi32(riceParamPart128
, prediction128
);
4178 /* Our value should be sitting in prediction128[0]. We need to combine this with our SSE samples. */
4179 samples128_8
= _mm_alignr_epi8(samples128_4
, samples128_8
, 4);
4180 samples128_4
= _mm_alignr_epi8(samples128_0
, samples128_4
, 4);
4181 samples128_0
= _mm_alignr_epi8(prediction128
, samples128_0
, 4);
4183 /* Slide our rice parameter down so that the value in position 0 contains the next one to process. */
4184 riceParamPart128
= _mm_alignr_epi8(_mm_setzero_si128(), riceParamPart128
, 4);
4187 /* We store samples in groups of 4. */
4188 _mm_storeu_si128((__m128i
*)pDecodedSamples
, samples128_0
);
4189 pDecodedSamples
+= 4;
4192 /* Make sure we process the last few samples. */
4194 while (i
< (int)count
) {
4195 /* Rice extraction. */
4196 if (!drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountParts0
, &riceParamParts0
)) {
4197 return DRFLAC_FALSE
;
4200 /* Rice reconstruction. */
4201 riceParamParts0
&= riceParamMask
;
4202 riceParamParts0
|= (zeroCountParts0
<< riceParam
);
4203 riceParamParts0
= (riceParamParts0
>> 1) ^ t
[riceParamParts0
& 0x01];
4205 /* Sample reconstruction. */
4206 pDecodedSamples
[0] = riceParamParts0
+ drflac__calculate_prediction_64(order
, shift
, coefficients
, pDecodedSamples
);
4209 pDecodedSamples
+= 1;
4215 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
)
4217 DRFLAC_ASSERT(bs
!= NULL
);
4218 DRFLAC_ASSERT(pSamplesOut
!= NULL
);
4220 /* In my testing the order is rarely > 12, so in this case I'm going to simplify the SSE implementation by only handling order <= 12. */
4221 if (order
> 0 && order
<= 12) {
4222 if (bitsPerSample
+shift
> 32) {
4223 return drflac__decode_samples_with_residual__rice__sse41_64(bs
, count
, riceParam
, order
, shift
, coefficients
, pSamplesOut
);
4225 return drflac__decode_samples_with_residual__rice__sse41_32(bs
, count
, riceParam
, order
, shift
, coefficients
, pSamplesOut
);
4228 return drflac__decode_samples_with_residual__rice__scalar(bs
, bitsPerSample
, count
, riceParam
, order
, shift
, coefficients
, pSamplesOut
);
4233 #if defined(DRFLAC_SUPPORT_NEON)
4234 static DRFLAC_INLINE
void drflac__vst2q_s32(drflac_int32
* p
, int32x4x2_t x
)
4236 vst1q_s32(p
+0, x
.val
[0]);
4237 vst1q_s32(p
+4, x
.val
[1]);
4240 static DRFLAC_INLINE
void drflac__vst2q_u32(drflac_uint32
* p
, uint32x4x2_t x
)
4242 vst1q_u32(p
+0, x
.val
[0]);
4243 vst1q_u32(p
+4, x
.val
[1]);
4246 static DRFLAC_INLINE
void drflac__vst2q_f32(float* p
, float32x4x2_t x
)
4248 vst1q_f32(p
+0, x
.val
[0]);
4249 vst1q_f32(p
+4, x
.val
[1]);
4252 static DRFLAC_INLINE
void drflac__vst2q_s16(drflac_int16
* p
, int16x4x2_t x
)
4254 vst1q_s16(p
, vcombine_s16(x
.val
[0], x
.val
[1]));
4257 static DRFLAC_INLINE
void drflac__vst2q_u16(drflac_uint16
* p
, uint16x4x2_t x
)
4259 vst1q_u16(p
, vcombine_u16(x
.val
[0], x
.val
[1]));
4262 static DRFLAC_INLINE int32x4_t
drflac__vdupq_n_s32x4(drflac_int32 x3
, drflac_int32 x2
, drflac_int32 x1
, drflac_int32 x0
)
4269 return vld1q_s32(x
);
4272 static DRFLAC_INLINE int32x4_t
drflac__valignrq_s32_1(int32x4_t a
, int32x4_t b
)
4274 /* Equivalent to SSE's _mm_alignr_epi8(a, b, 4) */
4277 /*return drflac__vdupq_n_s32x4(
4278 vgetq_lane_s32(a, 0),
4279 vgetq_lane_s32(b, 3),
4280 vgetq_lane_s32(b, 2),
4281 vgetq_lane_s32(b, 1)
4284 return vextq_s32(b
, a
, 1);
4287 static DRFLAC_INLINE uint32x4_t
drflac__valignrq_u32_1(uint32x4_t a
, uint32x4_t b
)
4289 /* Equivalent to SSE's _mm_alignr_epi8(a, b, 4) */
4292 /*return drflac__vdupq_n_s32x4(
4293 vgetq_lane_s32(a, 0),
4294 vgetq_lane_s32(b, 3),
4295 vgetq_lane_s32(b, 2),
4296 vgetq_lane_s32(b, 1)
4299 return vextq_u32(b
, a
, 1);
4302 static DRFLAC_INLINE int32x2_t
drflac__vhaddq_s32(int32x4_t x
)
4304 /* The sum must end up in position 0. */
4307 /*return vdupq_n_s32(
4308 vgetq_lane_s32(x, 3) +
4309 vgetq_lane_s32(x, 2) +
4310 vgetq_lane_s32(x, 1) +
4311 vgetq_lane_s32(x, 0)
4314 int32x2_t r
= vadd_s32(vget_high_s32(x
), vget_low_s32(x
));
4315 return vpadd_s32(r
, r
);
4318 static DRFLAC_INLINE int64x1_t
drflac__vhaddq_s64(int64x2_t x
)
4320 return vadd_s64(vget_high_s64(x
), vget_low_s64(x
));
4323 static DRFLAC_INLINE int32x4_t
drflac__vrevq_s32(int32x4_t x
)
4326 /*return drflac__vdupq_n_s32x4(
4327 vgetq_lane_s32(x, 0),
4328 vgetq_lane_s32(x, 1),
4329 vgetq_lane_s32(x, 2),
4330 vgetq_lane_s32(x, 3)
4333 return vrev64q_s32(vcombine_s32(vget_high_s32(x
), vget_low_s32(x
)));
4336 static DRFLAC_INLINE int32x4_t
drflac__vnotq_s32(int32x4_t x
)
4338 return veorq_s32(x
, vdupq_n_s32(0xFFFFFFFF));
4341 static DRFLAC_INLINE uint32x4_t
drflac__vnotq_u32(uint32x4_t x
)
4343 return veorq_u32(x
, vdupq_n_u32(0xFFFFFFFF));
4346 static drflac_bool32
drflac__decode_samples_with_residual__rice__neon_32(drflac_bs
* bs
, drflac_uint32 count
, drflac_uint8 riceParam
, drflac_uint32 order
, drflac_int32 shift
, const drflac_int32
* coefficients
, drflac_int32
* pSamplesOut
)
4349 drflac_uint32 riceParamMask
;
4350 drflac_int32
* pDecodedSamples
= pSamplesOut
;
4351 drflac_int32
* pDecodedSamplesEnd
= pSamplesOut
+ (count
& ~3);
4352 drflac_uint32 zeroCountParts
[4];
4353 drflac_uint32 riceParamParts
[4];
4354 int32x4_t coefficients128_0
;
4355 int32x4_t coefficients128_4
;
4356 int32x4_t coefficients128_8
;
4357 int32x4_t samples128_0
;
4358 int32x4_t samples128_4
;
4359 int32x4_t samples128_8
;
4360 uint32x4_t riceParamMask128
;
4361 int32x4_t riceParam128
;
4365 const drflac_uint32 t
[2] = {0x00000000, 0xFFFFFFFF};
4367 riceParamMask
= ~((~0UL) << riceParam
);
4368 riceParamMask128
= vdupq_n_u32(riceParamMask
);
4370 riceParam128
= vdupq_n_s32(riceParam
);
4371 shift64
= vdup_n_s32(-shift
); /* Negate the shift because we'll be doing a variable shift using vshlq_s32(). */
4372 one128
= vdupq_n_u32(1);
4375 Pre-loading the coefficients and prior samples is annoying because we need to ensure we don't try reading more than
4376 what's available in the input buffers. It would be conenient to use a fall-through switch to do this, but this results
4377 in strict aliasing warnings with GCC. To work around this I'm just doing something hacky. This feels a bit convoluted
4378 so I think there's opportunity for this to be simplified.
4381 int runningOrder
= order
;
4382 drflac_int32 tempC
[4] = {0, 0, 0, 0};
4383 drflac_int32 tempS
[4] = {0, 0, 0, 0};
4386 if (runningOrder
>= 4) {
4387 coefficients128_0
= vld1q_s32(coefficients
+ 0);
4388 samples128_0
= vld1q_s32(pSamplesOut
- 4);
4391 switch (runningOrder
) {
4392 case 3: tempC
[2] = coefficients
[2]; tempS
[1] = pSamplesOut
[-3]; /* fallthrough */
4393 case 2: tempC
[1] = coefficients
[1]; tempS
[2] = pSamplesOut
[-2]; /* fallthrough */
4394 case 1: tempC
[0] = coefficients
[0]; tempS
[3] = pSamplesOut
[-1]; /* fallthrough */
4397 coefficients128_0
= vld1q_s32(tempC
);
4398 samples128_0
= vld1q_s32(tempS
);
4403 if (runningOrder
>= 4) {
4404 coefficients128_4
= vld1q_s32(coefficients
+ 4);
4405 samples128_4
= vld1q_s32(pSamplesOut
- 8);
4408 switch (runningOrder
) {
4409 case 3: tempC
[2] = coefficients
[6]; tempS
[1] = pSamplesOut
[-7]; /* fallthrough */
4410 case 2: tempC
[1] = coefficients
[5]; tempS
[2] = pSamplesOut
[-6]; /* fallthrough */
4411 case 1: tempC
[0] = coefficients
[4]; tempS
[3] = pSamplesOut
[-5]; /* fallthrough */
4414 coefficients128_4
= vld1q_s32(tempC
);
4415 samples128_4
= vld1q_s32(tempS
);
4420 if (runningOrder
== 4) {
4421 coefficients128_8
= vld1q_s32(coefficients
+ 8);
4422 samples128_8
= vld1q_s32(pSamplesOut
- 12);
4425 switch (runningOrder
) {
4426 case 3: tempC
[2] = coefficients
[10]; tempS
[1] = pSamplesOut
[-11]; /* fallthrough */
4427 case 2: tempC
[1] = coefficients
[ 9]; tempS
[2] = pSamplesOut
[-10]; /* fallthrough */
4428 case 1: tempC
[0] = coefficients
[ 8]; tempS
[3] = pSamplesOut
[- 9]; /* fallthrough */
4431 coefficients128_8
= vld1q_s32(tempC
);
4432 samples128_8
= vld1q_s32(tempS
);
4436 /* Coefficients need to be shuffled for our streaming algorithm below to work. Samples are already in the correct order from the loading routine above. */
4437 coefficients128_0
= drflac__vrevq_s32(coefficients128_0
);
4438 coefficients128_4
= drflac__vrevq_s32(coefficients128_4
);
4439 coefficients128_8
= drflac__vrevq_s32(coefficients128_8
);
4442 /* For this version we are doing one sample at a time. */
4443 while (pDecodedSamples
< pDecodedSamplesEnd
) {
4444 int32x4_t prediction128
;
4445 int32x2_t prediction64
;
4446 uint32x4_t zeroCountPart128
;
4447 uint32x4_t riceParamPart128
;
4449 if (!drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountParts
[0], &riceParamParts
[0]) ||
4450 !drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountParts
[1], &riceParamParts
[1]) ||
4451 !drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountParts
[2], &riceParamParts
[2]) ||
4452 !drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountParts
[3], &riceParamParts
[3])) {
4453 return DRFLAC_FALSE
;
4456 zeroCountPart128
= vld1q_u32(zeroCountParts
);
4457 riceParamPart128
= vld1q_u32(riceParamParts
);
4459 riceParamPart128
= vandq_u32(riceParamPart128
, riceParamMask128
);
4460 riceParamPart128
= vorrq_u32(riceParamPart128
, vshlq_u32(zeroCountPart128
, riceParam128
));
4461 riceParamPart128
= veorq_u32(vshrq_n_u32(riceParamPart128
, 1), vaddq_u32(drflac__vnotq_u32(vandq_u32(riceParamPart128
, one128
)), one128
));
4464 for (i
= 0; i
< 4; i
+= 1) {
4465 prediction128
= vmulq_s32(coefficients128_0
, samples128_0
);
4467 /* Horizontal add and shift. */
4468 prediction64
= drflac__vhaddq_s32(prediction128
);
4469 prediction64
= vshl_s32(prediction64
, shift64
);
4470 prediction64
= vadd_s32(prediction64
, vget_low_s32(vreinterpretq_s32_u32(riceParamPart128
)));
4472 samples128_0
= drflac__valignrq_s32_1(vcombine_s32(prediction64
, vdup_n_s32(0)), samples128_0
);
4473 riceParamPart128
= drflac__valignrq_u32_1(vdupq_n_u32(0), riceParamPart128
);
4475 } else if (order
<= 8) {
4476 for (i
= 0; i
< 4; i
+= 1) {
4477 prediction128
= vmulq_s32(coefficients128_4
, samples128_4
);
4478 prediction128
= vmlaq_s32(prediction128
, coefficients128_0
, samples128_0
);
4480 /* Horizontal add and shift. */
4481 prediction64
= drflac__vhaddq_s32(prediction128
);
4482 prediction64
= vshl_s32(prediction64
, shift64
);
4483 prediction64
= vadd_s32(prediction64
, vget_low_s32(vreinterpretq_s32_u32(riceParamPart128
)));
4485 samples128_4
= drflac__valignrq_s32_1(samples128_0
, samples128_4
);
4486 samples128_0
= drflac__valignrq_s32_1(vcombine_s32(prediction64
, vdup_n_s32(0)), samples128_0
);
4487 riceParamPart128
= drflac__valignrq_u32_1(vdupq_n_u32(0), riceParamPart128
);
4490 for (i
= 0; i
< 4; i
+= 1) {
4491 prediction128
= vmulq_s32(coefficients128_8
, samples128_8
);
4492 prediction128
= vmlaq_s32(prediction128
, coefficients128_4
, samples128_4
);
4493 prediction128
= vmlaq_s32(prediction128
, coefficients128_0
, samples128_0
);
4495 /* Horizontal add and shift. */
4496 prediction64
= drflac__vhaddq_s32(prediction128
);
4497 prediction64
= vshl_s32(prediction64
, shift64
);
4498 prediction64
= vadd_s32(prediction64
, vget_low_s32(vreinterpretq_s32_u32(riceParamPart128
)));
4500 samples128_8
= drflac__valignrq_s32_1(samples128_4
, samples128_8
);
4501 samples128_4
= drflac__valignrq_s32_1(samples128_0
, samples128_4
);
4502 samples128_0
= drflac__valignrq_s32_1(vcombine_s32(prediction64
, vdup_n_s32(0)), samples128_0
);
4503 riceParamPart128
= drflac__valignrq_u32_1(vdupq_n_u32(0), riceParamPart128
);
4507 /* We store samples in groups of 4. */
4508 vst1q_s32(pDecodedSamples
, samples128_0
);
4509 pDecodedSamples
+= 4;
4512 /* Make sure we process the last few samples. */
4514 while (i
< (int)count
) {
4515 /* Rice extraction. */
4516 if (!drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountParts
[0], &riceParamParts
[0])) {
4517 return DRFLAC_FALSE
;
4520 /* Rice reconstruction. */
4521 riceParamParts
[0] &= riceParamMask
;
4522 riceParamParts
[0] |= (zeroCountParts
[0] << riceParam
);
4523 riceParamParts
[0] = (riceParamParts
[0] >> 1) ^ t
[riceParamParts
[0] & 0x01];
4525 /* Sample reconstruction. */
4526 pDecodedSamples
[0] = riceParamParts
[0] + drflac__calculate_prediction_32(order
, shift
, coefficients
, pDecodedSamples
);
4529 pDecodedSamples
+= 1;
4535 static drflac_bool32
drflac__decode_samples_with_residual__rice__neon_64(drflac_bs
* bs
, drflac_uint32 count
, drflac_uint8 riceParam
, drflac_uint32 order
, drflac_int32 shift
, const drflac_int32
* coefficients
, drflac_int32
* pSamplesOut
)
4538 drflac_uint32 riceParamMask
;
4539 drflac_int32
* pDecodedSamples
= pSamplesOut
;
4540 drflac_int32
* pDecodedSamplesEnd
= pSamplesOut
+ (count
& ~3);
4541 drflac_uint32 zeroCountParts
[4];
4542 drflac_uint32 riceParamParts
[4];
4543 int32x4_t coefficients128_0
;
4544 int32x4_t coefficients128_4
;
4545 int32x4_t coefficients128_8
;
4546 int32x4_t samples128_0
;
4547 int32x4_t samples128_4
;
4548 int32x4_t samples128_8
;
4549 uint32x4_t riceParamMask128
;
4550 int32x4_t riceParam128
;
4554 const drflac_uint32 t
[2] = {0x00000000, 0xFFFFFFFF};
4556 riceParamMask
= ~((~0UL) << riceParam
);
4557 riceParamMask128
= vdupq_n_u32(riceParamMask
);
4559 riceParam128
= vdupq_n_s32(riceParam
);
4560 shift64
= vdup_n_s64(-shift
); /* Negate the shift because we'll be doing a variable shift using vshlq_s32(). */
4561 one128
= vdupq_n_u32(1);
4564 Pre-loading the coefficients and prior samples is annoying because we need to ensure we don't try reading more than
4565 what's available in the input buffers. It would be conenient to use a fall-through switch to do this, but this results
4566 in strict aliasing warnings with GCC. To work around this I'm just doing something hacky. This feels a bit convoluted
4567 so I think there's opportunity for this to be simplified.
4570 int runningOrder
= order
;
4571 drflac_int32 tempC
[4] = {0, 0, 0, 0};
4572 drflac_int32 tempS
[4] = {0, 0, 0, 0};
4575 if (runningOrder
>= 4) {
4576 coefficients128_0
= vld1q_s32(coefficients
+ 0);
4577 samples128_0
= vld1q_s32(pSamplesOut
- 4);
4580 switch (runningOrder
) {
4581 case 3: tempC
[2] = coefficients
[2]; tempS
[1] = pSamplesOut
[-3]; /* fallthrough */
4582 case 2: tempC
[1] = coefficients
[1]; tempS
[2] = pSamplesOut
[-2]; /* fallthrough */
4583 case 1: tempC
[0] = coefficients
[0]; tempS
[3] = pSamplesOut
[-1]; /* fallthrough */
4586 coefficients128_0
= vld1q_s32(tempC
);
4587 samples128_0
= vld1q_s32(tempS
);
4592 if (runningOrder
>= 4) {
4593 coefficients128_4
= vld1q_s32(coefficients
+ 4);
4594 samples128_4
= vld1q_s32(pSamplesOut
- 8);
4597 switch (runningOrder
) {
4598 case 3: tempC
[2] = coefficients
[6]; tempS
[1] = pSamplesOut
[-7]; /* fallthrough */
4599 case 2: tempC
[1] = coefficients
[5]; tempS
[2] = pSamplesOut
[-6]; /* fallthrough */
4600 case 1: tempC
[0] = coefficients
[4]; tempS
[3] = pSamplesOut
[-5]; /* fallthrough */
4603 coefficients128_4
= vld1q_s32(tempC
);
4604 samples128_4
= vld1q_s32(tempS
);
4609 if (runningOrder
== 4) {
4610 coefficients128_8
= vld1q_s32(coefficients
+ 8);
4611 samples128_8
= vld1q_s32(pSamplesOut
- 12);
4614 switch (runningOrder
) {
4615 case 3: tempC
[2] = coefficients
[10]; tempS
[1] = pSamplesOut
[-11]; /* fallthrough */
4616 case 2: tempC
[1] = coefficients
[ 9]; tempS
[2] = pSamplesOut
[-10]; /* fallthrough */
4617 case 1: tempC
[0] = coefficients
[ 8]; tempS
[3] = pSamplesOut
[- 9]; /* fallthrough */
4620 coefficients128_8
= vld1q_s32(tempC
);
4621 samples128_8
= vld1q_s32(tempS
);
4625 /* Coefficients need to be shuffled for our streaming algorithm below to work. Samples are already in the correct order from the loading routine above. */
4626 coefficients128_0
= drflac__vrevq_s32(coefficients128_0
);
4627 coefficients128_4
= drflac__vrevq_s32(coefficients128_4
);
4628 coefficients128_8
= drflac__vrevq_s32(coefficients128_8
);
4631 /* For this version we are doing one sample at a time. */
4632 while (pDecodedSamples
< pDecodedSamplesEnd
) {
4633 int64x2_t prediction128
;
4634 uint32x4_t zeroCountPart128
;
4635 uint32x4_t riceParamPart128
;
4637 if (!drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountParts
[0], &riceParamParts
[0]) ||
4638 !drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountParts
[1], &riceParamParts
[1]) ||
4639 !drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountParts
[2], &riceParamParts
[2]) ||
4640 !drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountParts
[3], &riceParamParts
[3])) {
4641 return DRFLAC_FALSE
;
4644 zeroCountPart128
= vld1q_u32(zeroCountParts
);
4645 riceParamPart128
= vld1q_u32(riceParamParts
);
4647 riceParamPart128
= vandq_u32(riceParamPart128
, riceParamMask128
);
4648 riceParamPart128
= vorrq_u32(riceParamPart128
, vshlq_u32(zeroCountPart128
, riceParam128
));
4649 riceParamPart128
= veorq_u32(vshrq_n_u32(riceParamPart128
, 1), vaddq_u32(drflac__vnotq_u32(vandq_u32(riceParamPart128
, one128
)), one128
));
4651 for (i
= 0; i
< 4; i
+= 1) {
4652 int64x1_t prediction64
;
4654 prediction128
= veorq_s64(prediction128
, prediction128
); /* Reset to 0. */
4658 case 11: prediction128
= vaddq_s64(prediction128
, vmull_s32(vget_low_s32(coefficients128_8
), vget_low_s32(samples128_8
)));
4660 case 9: prediction128
= vaddq_s64(prediction128
, vmull_s32(vget_high_s32(coefficients128_8
), vget_high_s32(samples128_8
)));
4662 case 7: prediction128
= vaddq_s64(prediction128
, vmull_s32(vget_low_s32(coefficients128_4
), vget_low_s32(samples128_4
)));
4664 case 5: prediction128
= vaddq_s64(prediction128
, vmull_s32(vget_high_s32(coefficients128_4
), vget_high_s32(samples128_4
)));
4666 case 3: prediction128
= vaddq_s64(prediction128
, vmull_s32(vget_low_s32(coefficients128_0
), vget_low_s32(samples128_0
)));
4668 case 1: prediction128
= vaddq_s64(prediction128
, vmull_s32(vget_high_s32(coefficients128_0
), vget_high_s32(samples128_0
)));
4671 /* Horizontal add and shift. */
4672 prediction64
= drflac__vhaddq_s64(prediction128
);
4673 prediction64
= vshl_s64(prediction64
, shift64
);
4674 prediction64
= vadd_s64(prediction64
, vdup_n_s64(vgetq_lane_u32(riceParamPart128
, 0)));
4676 /* Our value should be sitting in prediction64[0]. We need to combine this with our SSE samples. */
4677 samples128_8
= drflac__valignrq_s32_1(samples128_4
, samples128_8
);
4678 samples128_4
= drflac__valignrq_s32_1(samples128_0
, samples128_4
);
4679 samples128_0
= drflac__valignrq_s32_1(vcombine_s32(vreinterpret_s32_s64(prediction64
), vdup_n_s32(0)), samples128_0
);
4681 /* Slide our rice parameter down so that the value in position 0 contains the next one to process. */
4682 riceParamPart128
= drflac__valignrq_u32_1(vdupq_n_u32(0), riceParamPart128
);
4685 /* We store samples in groups of 4. */
4686 vst1q_s32(pDecodedSamples
, samples128_0
);
4687 pDecodedSamples
+= 4;
4690 /* Make sure we process the last few samples. */
4692 while (i
< (int)count
) {
4693 /* Rice extraction. */
4694 if (!drflac__read_rice_parts_x1(bs
, riceParam
, &zeroCountParts
[0], &riceParamParts
[0])) {
4695 return DRFLAC_FALSE
;
4698 /* Rice reconstruction. */
4699 riceParamParts
[0] &= riceParamMask
;
4700 riceParamParts
[0] |= (zeroCountParts
[0] << riceParam
);
4701 riceParamParts
[0] = (riceParamParts
[0] >> 1) ^ t
[riceParamParts
[0] & 0x01];
4703 /* Sample reconstruction. */
4704 pDecodedSamples
[0] = riceParamParts
[0] + drflac__calculate_prediction_64(order
, shift
, coefficients
, pDecodedSamples
);
4707 pDecodedSamples
+= 1;
4713 static drflac_bool32
drflac__decode_samples_with_residual__rice__neon(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
)
4715 DRFLAC_ASSERT(bs
!= NULL
);
4716 DRFLAC_ASSERT(pSamplesOut
!= NULL
);
4718 /* In my testing the order is rarely > 12, so in this case I'm going to simplify the NEON implementation by only handling order <= 12. */
4719 if (order
> 0 && order
<= 12) {
4720 if (bitsPerSample
+shift
> 32) {
4721 return drflac__decode_samples_with_residual__rice__neon_64(bs
, count
, riceParam
, order
, shift
, coefficients
, pSamplesOut
);
4723 return drflac__decode_samples_with_residual__rice__neon_32(bs
, count
, riceParam
, order
, shift
, coefficients
, pSamplesOut
);
4726 return drflac__decode_samples_with_residual__rice__scalar(bs
, bitsPerSample
, count
, riceParam
, order
, shift
, coefficients
, pSamplesOut
);
4731 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
)
4733 #if defined(DRFLAC_SUPPORT_SSE41)
4734 if (drflac__gIsSSE41Supported
) {
4735 return drflac__decode_samples_with_residual__rice__sse41(bs
, bitsPerSample
, count
, riceParam
, order
, shift
, coefficients
, pSamplesOut
);
4737 #elif defined(DRFLAC_SUPPORT_NEON)
4738 if (drflac__gIsNEONSupported
) {
4739 return drflac__decode_samples_with_residual__rice__neon(bs
, bitsPerSample
, count
, riceParam
, order
, shift
, coefficients
, pSamplesOut
);
4743 /* Scalar fallback. */
4745 return drflac__decode_samples_with_residual__rice__reference(bs
, bitsPerSample
, count
, riceParam
, order
, shift
, coefficients
, pSamplesOut
);
4747 return drflac__decode_samples_with_residual__rice__scalar(bs
, bitsPerSample
, count
, riceParam
, order
, shift
, coefficients
, pSamplesOut
);
4752 /* 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. */
4753 static drflac_bool32
drflac__read_and_seek_residual__rice(drflac_bs
* bs
, drflac_uint32 count
, drflac_uint8 riceParam
)
4757 DRFLAC_ASSERT(bs
!= NULL
);
4759 for (i
= 0; i
< count
; ++i
) {
4760 if (!drflac__seek_rice_parts(bs
, riceParam
)) {
4761 return DRFLAC_FALSE
;
4768 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
)
4772 DRFLAC_ASSERT(bs
!= NULL
);
4773 DRFLAC_ASSERT(unencodedBitsPerSample
<= 31); /* <-- unencodedBitsPerSample is a 5 bit number, so cannot exceed 31. */
4774 DRFLAC_ASSERT(pSamplesOut
!= NULL
);
4776 for (i
= 0; i
< count
; ++i
) {
4777 if (unencodedBitsPerSample
> 0) {
4778 if (!drflac__read_int32(bs
, unencodedBitsPerSample
, pSamplesOut
+ i
)) {
4779 return DRFLAC_FALSE
;
4785 if (bitsPerSample
>= 24) {
4786 pSamplesOut
[i
] += drflac__calculate_prediction_64(order
, shift
, coefficients
, pSamplesOut
+ i
);
4788 pSamplesOut
[i
] += drflac__calculate_prediction_32(order
, shift
, coefficients
, pSamplesOut
+ i
);
4797 Reads and decodes the residual for the sub-frame the decoder is currently sitting on. This function should be called
4798 when the decoder is sitting at the very start of the RESIDUAL block. The first <order> residuals will be ignored. The
4799 <blockSize> and <order> parameters are used to determine how many residual values need to be decoded.
4801 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
)
4803 drflac_uint8 residualMethod
;
4804 drflac_uint8 partitionOrder
;
4805 drflac_uint32 samplesInPartition
;
4806 drflac_uint32 partitionsRemaining
;
4808 DRFLAC_ASSERT(bs
!= NULL
);
4809 DRFLAC_ASSERT(blockSize
!= 0);
4810 DRFLAC_ASSERT(pDecodedSamples
!= NULL
); /* <-- Should we allow NULL, in which case we just seek past the residual rather than do a full decode? */
4812 if (!drflac__read_uint8(bs
, 2, &residualMethod
)) {
4813 return DRFLAC_FALSE
;
4816 if (residualMethod
!= DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE
&& residualMethod
!= DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2
) {
4817 return DRFLAC_FALSE
; /* Unknown or unsupported residual coding method. */
4820 /* Ignore the first <order> values. */
4821 pDecodedSamples
+= order
;
4823 if (!drflac__read_uint8(bs
, 4, &partitionOrder
)) {
4824 return DRFLAC_FALSE
;
4829 The Rice partition order in a Rice-coded residual section must be less than or equal to 8.
4831 if (partitionOrder
> 8) {
4832 return DRFLAC_FALSE
;
4835 /* Validation check. */
4836 if ((blockSize
/ (1 << partitionOrder
)) < order
) {
4837 return DRFLAC_FALSE
;
4840 samplesInPartition
= (blockSize
/ (1 << partitionOrder
)) - order
;
4841 partitionsRemaining
= (1 << partitionOrder
);
4843 drflac_uint8 riceParam
= 0;
4844 if (residualMethod
== DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE
) {
4845 if (!drflac__read_uint8(bs
, 4, &riceParam
)) {
4846 return DRFLAC_FALSE
;
4848 if (riceParam
== 15) {
4851 } else if (residualMethod
== DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2
) {
4852 if (!drflac__read_uint8(bs
, 5, &riceParam
)) {
4853 return DRFLAC_FALSE
;
4855 if (riceParam
== 31) {
4860 if (riceParam
!= 0xFF) {
4861 if (!drflac__decode_samples_with_residual__rice(bs
, bitsPerSample
, samplesInPartition
, riceParam
, order
, shift
, coefficients
, pDecodedSamples
)) {
4862 return DRFLAC_FALSE
;
4865 drflac_uint8 unencodedBitsPerSample
= 0;
4866 if (!drflac__read_uint8(bs
, 5, &unencodedBitsPerSample
)) {
4867 return DRFLAC_FALSE
;
4870 if (!drflac__decode_samples_with_residual__unencoded(bs
, bitsPerSample
, samplesInPartition
, unencodedBitsPerSample
, order
, shift
, coefficients
, pDecodedSamples
)) {
4871 return DRFLAC_FALSE
;
4875 pDecodedSamples
+= samplesInPartition
;
4877 if (partitionsRemaining
== 1) {
4881 partitionsRemaining
-= 1;
4883 if (partitionOrder
!= 0) {
4884 samplesInPartition
= blockSize
/ (1 << partitionOrder
);
4892 Reads and seeks past the residual for the sub-frame the decoder is currently sitting on. This function should be called
4893 when the decoder is sitting at the very start of the RESIDUAL block. The first <order> residuals will be set to 0. The
4894 <blockSize> and <order> parameters are used to determine how many residual values need to be decoded.
4896 static drflac_bool32
drflac__read_and_seek_residual(drflac_bs
* bs
, drflac_uint32 blockSize
, drflac_uint32 order
)
4898 drflac_uint8 residualMethod
;
4899 drflac_uint8 partitionOrder
;
4900 drflac_uint32 samplesInPartition
;
4901 drflac_uint32 partitionsRemaining
;
4903 DRFLAC_ASSERT(bs
!= NULL
);
4904 DRFLAC_ASSERT(blockSize
!= 0);
4906 if (!drflac__read_uint8(bs
, 2, &residualMethod
)) {
4907 return DRFLAC_FALSE
;
4910 if (residualMethod
!= DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE
&& residualMethod
!= DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2
) {
4911 return DRFLAC_FALSE
; /* Unknown or unsupported residual coding method. */
4914 if (!drflac__read_uint8(bs
, 4, &partitionOrder
)) {
4915 return DRFLAC_FALSE
;
4920 The Rice partition order in a Rice-coded residual section must be less than or equal to 8.
4922 if (partitionOrder
> 8) {
4923 return DRFLAC_FALSE
;
4926 /* Validation check. */
4927 if ((blockSize
/ (1 << partitionOrder
)) <= order
) {
4928 return DRFLAC_FALSE
;
4931 samplesInPartition
= (blockSize
/ (1 << partitionOrder
)) - order
;
4932 partitionsRemaining
= (1 << partitionOrder
);
4935 drflac_uint8 riceParam
= 0;
4936 if (residualMethod
== DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE
) {
4937 if (!drflac__read_uint8(bs
, 4, &riceParam
)) {
4938 return DRFLAC_FALSE
;
4940 if (riceParam
== 15) {
4943 } else if (residualMethod
== DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2
) {
4944 if (!drflac__read_uint8(bs
, 5, &riceParam
)) {
4945 return DRFLAC_FALSE
;
4947 if (riceParam
== 31) {
4952 if (riceParam
!= 0xFF) {
4953 if (!drflac__read_and_seek_residual__rice(bs
, samplesInPartition
, riceParam
)) {
4954 return DRFLAC_FALSE
;
4957 drflac_uint8 unencodedBitsPerSample
= 0;
4958 if (!drflac__read_uint8(bs
, 5, &unencodedBitsPerSample
)) {
4959 return DRFLAC_FALSE
;
4962 if (!drflac__seek_bits(bs
, unencodedBitsPerSample
* samplesInPartition
)) {
4963 return DRFLAC_FALSE
;
4968 if (partitionsRemaining
== 1) {
4972 partitionsRemaining
-= 1;
4973 samplesInPartition
= blockSize
/ (1 << partitionOrder
);
4980 static drflac_bool32
drflac__decode_samples__constant(drflac_bs
* bs
, drflac_uint32 blockSize
, drflac_uint32 subframeBitsPerSample
, drflac_int32
* pDecodedSamples
)
4984 /* Only a single sample needs to be decoded here. */
4985 drflac_int32 sample
;
4986 if (!drflac__read_int32(bs
, subframeBitsPerSample
, &sample
)) {
4987 return DRFLAC_FALSE
;
4991 We don't really need to expand this, but it does simplify the process of reading samples. If this becomes a performance issue (unlikely)
4992 we'll want to look at a more efficient way.
4994 for (i
= 0; i
< blockSize
; ++i
) {
4995 pDecodedSamples
[i
] = sample
;
5001 static drflac_bool32
drflac__decode_samples__verbatim(drflac_bs
* bs
, drflac_uint32 blockSize
, drflac_uint32 subframeBitsPerSample
, drflac_int32
* pDecodedSamples
)
5005 for (i
= 0; i
< blockSize
; ++i
) {
5006 drflac_int32 sample
;
5007 if (!drflac__read_int32(bs
, subframeBitsPerSample
, &sample
)) {
5008 return DRFLAC_FALSE
;
5011 pDecodedSamples
[i
] = sample
;
5017 static drflac_bool32
drflac__decode_samples__fixed(drflac_bs
* bs
, drflac_uint32 blockSize
, drflac_uint32 subframeBitsPerSample
, drflac_uint8 lpcOrder
, drflac_int32
* pDecodedSamples
)
5021 static drflac_int32 lpcCoefficientsTable
[5][4] = {
5029 /* Warm up samples and coefficients. */
5030 for (i
= 0; i
< lpcOrder
; ++i
) {
5031 drflac_int32 sample
;
5032 if (!drflac__read_int32(bs
, subframeBitsPerSample
, &sample
)) {
5033 return DRFLAC_FALSE
;
5036 pDecodedSamples
[i
] = sample
;
5039 if (!drflac__decode_samples_with_residual(bs
, subframeBitsPerSample
, blockSize
, lpcOrder
, 0, lpcCoefficientsTable
[lpcOrder
], pDecodedSamples
)) {
5040 return DRFLAC_FALSE
;
5046 static drflac_bool32
drflac__decode_samples__lpc(drflac_bs
* bs
, drflac_uint32 blockSize
, drflac_uint32 bitsPerSample
, drflac_uint8 lpcOrder
, drflac_int32
* pDecodedSamples
)
5049 drflac_uint8 lpcPrecision
;
5050 drflac_int8 lpcShift
;
5051 drflac_int32 coefficients
[32];
5053 /* Warm up samples. */
5054 for (i
= 0; i
< lpcOrder
; ++i
) {
5055 drflac_int32 sample
;
5056 if (!drflac__read_int32(bs
, bitsPerSample
, &sample
)) {
5057 return DRFLAC_FALSE
;
5060 pDecodedSamples
[i
] = sample
;
5063 if (!drflac__read_uint8(bs
, 4, &lpcPrecision
)) {
5064 return DRFLAC_FALSE
;
5066 if (lpcPrecision
== 15) {
5067 return DRFLAC_FALSE
; /* Invalid. */
5071 if (!drflac__read_int8(bs
, 5, &lpcShift
)) {
5072 return DRFLAC_FALSE
;
5076 From the FLAC specification:
5078 Quantized linear predictor coefficient shift needed in bits (NOTE: this number is signed two's-complement)
5080 Emphasis on the "signed two's-complement". In practice there does not seem to be any encoders nor decoders supporting negative shifts. For now dr_flac is
5081 not going to support negative shifts as I don't have any reference files. However, when a reference file comes through I will consider adding support.
5084 return DRFLAC_FALSE
;
5087 DRFLAC_ZERO_MEMORY(coefficients
, sizeof(coefficients
));
5088 for (i
= 0; i
< lpcOrder
; ++i
) {
5089 if (!drflac__read_int32(bs
, lpcPrecision
, coefficients
+ i
)) {
5090 return DRFLAC_FALSE
;
5094 if (!drflac__decode_samples_with_residual(bs
, bitsPerSample
, blockSize
, lpcOrder
, lpcShift
, coefficients
, pDecodedSamples
)) {
5095 return DRFLAC_FALSE
;
5102 static drflac_bool32
drflac__read_next_flac_frame_header(drflac_bs
* bs
, drflac_uint8 streaminfoBitsPerSample
, drflac_frame_header
* header
)
5104 const drflac_uint32 sampleRateTable
[12] = {0, 88200, 176400, 192000, 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000};
5105 const drflac_uint8 bitsPerSampleTable
[8] = {0, 8, 12, (drflac_uint8
)-1, 16, 20, 24, (drflac_uint8
)-1}; /* -1 = reserved. */
5107 DRFLAC_ASSERT(bs
!= NULL
);
5108 DRFLAC_ASSERT(header
!= NULL
);
5110 /* Keep looping until we find a valid sync code. */
5112 drflac_uint8 crc8
= 0xCE; /* 0xCE = drflac_crc8(0, 0x3FFE, 14); */
5113 drflac_uint8 reserved
= 0;
5114 drflac_uint8 blockingStrategy
= 0;
5115 drflac_uint8 blockSize
= 0;
5116 drflac_uint8 sampleRate
= 0;
5117 drflac_uint8 channelAssignment
= 0;
5118 drflac_uint8 bitsPerSample
= 0;
5119 drflac_bool32 isVariableBlockSize
;
5121 if (!drflac__find_and_seek_to_next_sync_code(bs
)) {
5122 return DRFLAC_FALSE
;
5125 if (!drflac__read_uint8(bs
, 1, &reserved
)) {
5126 return DRFLAC_FALSE
;
5128 if (reserved
== 1) {
5131 crc8
= drflac_crc8(crc8
, reserved
, 1);
5133 if (!drflac__read_uint8(bs
, 1, &blockingStrategy
)) {
5134 return DRFLAC_FALSE
;
5136 crc8
= drflac_crc8(crc8
, blockingStrategy
, 1);
5138 if (!drflac__read_uint8(bs
, 4, &blockSize
)) {
5139 return DRFLAC_FALSE
;
5141 if (blockSize
== 0) {
5144 crc8
= drflac_crc8(crc8
, blockSize
, 4);
5146 if (!drflac__read_uint8(bs
, 4, &sampleRate
)) {
5147 return DRFLAC_FALSE
;
5149 crc8
= drflac_crc8(crc8
, sampleRate
, 4);
5151 if (!drflac__read_uint8(bs
, 4, &channelAssignment
)) {
5152 return DRFLAC_FALSE
;
5154 if (channelAssignment
> 10) {
5157 crc8
= drflac_crc8(crc8
, channelAssignment
, 4);
5159 if (!drflac__read_uint8(bs
, 3, &bitsPerSample
)) {
5160 return DRFLAC_FALSE
;
5162 if (bitsPerSample
== 3 || bitsPerSample
== 7) {
5165 crc8
= drflac_crc8(crc8
, bitsPerSample
, 3);
5168 if (!drflac__read_uint8(bs
, 1, &reserved
)) {
5169 return DRFLAC_FALSE
;
5171 if (reserved
== 1) {
5174 crc8
= drflac_crc8(crc8
, reserved
, 1);
5177 isVariableBlockSize
= blockingStrategy
== 1;
5178 if (isVariableBlockSize
) {
5179 drflac_uint64 pcmFrameNumber
;
5180 drflac_result result
= drflac__read_utf8_coded_number(bs
, &pcmFrameNumber
, &crc8
);
5181 if (result
!= DRFLAC_SUCCESS
) {
5182 if (result
== DRFLAC_AT_END
) {
5183 return DRFLAC_FALSE
;
5188 header
->flacFrameNumber
= 0;
5189 header
->pcmFrameNumber
= pcmFrameNumber
;
5191 drflac_uint64 flacFrameNumber
= 0;
5192 drflac_result result
= drflac__read_utf8_coded_number(bs
, &flacFrameNumber
, &crc8
);
5193 if (result
!= DRFLAC_SUCCESS
) {
5194 if (result
== DRFLAC_AT_END
) {
5195 return DRFLAC_FALSE
;
5200 header
->flacFrameNumber
= (drflac_uint32
)flacFrameNumber
; /* <-- Safe cast. */
5201 header
->pcmFrameNumber
= 0;
5205 DRFLAC_ASSERT(blockSize
> 0);
5206 if (blockSize
== 1) {
5207 header
->blockSizeInPCMFrames
= 192;
5208 } else if (blockSize
<= 5) {
5209 DRFLAC_ASSERT(blockSize
>= 2);
5210 header
->blockSizeInPCMFrames
= 576 * (1 << (blockSize
- 2));
5211 } else if (blockSize
== 6) {
5212 if (!drflac__read_uint16(bs
, 8, &header
->blockSizeInPCMFrames
)) {
5213 return DRFLAC_FALSE
;
5215 crc8
= drflac_crc8(crc8
, header
->blockSizeInPCMFrames
, 8);
5216 header
->blockSizeInPCMFrames
+= 1;
5217 } else if (blockSize
== 7) {
5218 if (!drflac__read_uint16(bs
, 16, &header
->blockSizeInPCMFrames
)) {
5219 return DRFLAC_FALSE
;
5221 crc8
= drflac_crc8(crc8
, header
->blockSizeInPCMFrames
, 16);
5222 header
->blockSizeInPCMFrames
+= 1;
5224 DRFLAC_ASSERT(blockSize
>= 8);
5225 header
->blockSizeInPCMFrames
= 256 * (1 << (blockSize
- 8));
5229 if (sampleRate
<= 11) {
5230 header
->sampleRate
= sampleRateTable
[sampleRate
];
5231 } else if (sampleRate
== 12) {
5232 if (!drflac__read_uint32(bs
, 8, &header
->sampleRate
)) {
5233 return DRFLAC_FALSE
;
5235 crc8
= drflac_crc8(crc8
, header
->sampleRate
, 8);
5236 header
->sampleRate
*= 1000;
5237 } else if (sampleRate
== 13) {
5238 if (!drflac__read_uint32(bs
, 16, &header
->sampleRate
)) {
5239 return DRFLAC_FALSE
;
5241 crc8
= drflac_crc8(crc8
, header
->sampleRate
, 16);
5242 } else if (sampleRate
== 14) {
5243 if (!drflac__read_uint32(bs
, 16, &header
->sampleRate
)) {
5244 return DRFLAC_FALSE
;
5246 crc8
= drflac_crc8(crc8
, header
->sampleRate
, 16);
5247 header
->sampleRate
*= 10;
5249 continue; /* Invalid. Assume an invalid block. */
5253 header
->channelAssignment
= channelAssignment
;
5255 header
->bitsPerSample
= bitsPerSampleTable
[bitsPerSample
];
5256 if (header
->bitsPerSample
== 0) {
5257 header
->bitsPerSample
= streaminfoBitsPerSample
;
5260 if (!drflac__read_uint8(bs
, 8, &header
->crc8
)) {
5261 return DRFLAC_FALSE
;
5264 #ifndef DR_FLAC_NO_CRC
5265 if (header
->crc8
!= crc8
) {
5266 continue; /* CRC mismatch. Loop back to the top and find the next sync code. */
5273 static drflac_bool32
drflac__read_subframe_header(drflac_bs
* bs
, drflac_subframe
* pSubframe
)
5275 drflac_uint8 header
;
5278 if (!drflac__read_uint8(bs
, 8, &header
)) {
5279 return DRFLAC_FALSE
;
5282 /* First bit should always be 0. */
5283 if ((header
& 0x80) != 0) {
5284 return DRFLAC_FALSE
;
5287 type
= (header
& 0x7E) >> 1;
5289 pSubframe
->subframeType
= DRFLAC_SUBFRAME_CONSTANT
;
5290 } else if (type
== 1) {
5291 pSubframe
->subframeType
= DRFLAC_SUBFRAME_VERBATIM
;
5293 if ((type
& 0x20) != 0) {
5294 pSubframe
->subframeType
= DRFLAC_SUBFRAME_LPC
;
5295 pSubframe
->lpcOrder
= (drflac_uint8
)(type
& 0x1F) + 1;
5296 } else if ((type
& 0x08) != 0) {
5297 pSubframe
->subframeType
= DRFLAC_SUBFRAME_FIXED
;
5298 pSubframe
->lpcOrder
= (drflac_uint8
)(type
& 0x07);
5299 if (pSubframe
->lpcOrder
> 4) {
5300 pSubframe
->subframeType
= DRFLAC_SUBFRAME_RESERVED
;
5301 pSubframe
->lpcOrder
= 0;
5304 pSubframe
->subframeType
= DRFLAC_SUBFRAME_RESERVED
;
5308 if (pSubframe
->subframeType
== DRFLAC_SUBFRAME_RESERVED
) {
5309 return DRFLAC_FALSE
;
5312 /* Wasted bits per sample. */
5313 pSubframe
->wastedBitsPerSample
= 0;
5314 if ((header
& 0x01) == 1) {
5315 unsigned int wastedBitsPerSample
;
5316 if (!drflac__seek_past_next_set_bit(bs
, &wastedBitsPerSample
)) {
5317 return DRFLAC_FALSE
;
5319 pSubframe
->wastedBitsPerSample
= (drflac_uint8
)wastedBitsPerSample
+ 1;
5325 static drflac_bool32
drflac__decode_subframe(drflac_bs
* bs
, drflac_frame
* frame
, int subframeIndex
, drflac_int32
* pDecodedSamplesOut
)
5327 drflac_subframe
* pSubframe
;
5328 drflac_uint32 subframeBitsPerSample
;
5330 DRFLAC_ASSERT(bs
!= NULL
);
5331 DRFLAC_ASSERT(frame
!= NULL
);
5333 pSubframe
= frame
->subframes
+ subframeIndex
;
5334 if (!drflac__read_subframe_header(bs
, pSubframe
)) {
5335 return DRFLAC_FALSE
;
5338 /* Side channels require an extra bit per sample. Took a while to figure that one out... */
5339 subframeBitsPerSample
= frame
->header
.bitsPerSample
;
5340 if ((frame
->header
.channelAssignment
== DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE
|| frame
->header
.channelAssignment
== DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE
) && subframeIndex
== 1) {
5341 subframeBitsPerSample
+= 1;
5342 } else if (frame
->header
.channelAssignment
== DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE
&& subframeIndex
== 0) {
5343 subframeBitsPerSample
+= 1;
5346 /* Need to handle wasted bits per sample. */
5347 if (pSubframe
->wastedBitsPerSample
>= subframeBitsPerSample
) {
5348 return DRFLAC_FALSE
;
5350 subframeBitsPerSample
-= pSubframe
->wastedBitsPerSample
;
5352 pSubframe
->pSamplesS32
= pDecodedSamplesOut
;
5354 switch (pSubframe
->subframeType
)
5356 case DRFLAC_SUBFRAME_CONSTANT
:
5358 drflac__decode_samples__constant(bs
, frame
->header
.blockSizeInPCMFrames
, subframeBitsPerSample
, pSubframe
->pSamplesS32
);
5361 case DRFLAC_SUBFRAME_VERBATIM
:
5363 drflac__decode_samples__verbatim(bs
, frame
->header
.blockSizeInPCMFrames
, subframeBitsPerSample
, pSubframe
->pSamplesS32
);
5366 case DRFLAC_SUBFRAME_FIXED
:
5368 drflac__decode_samples__fixed(bs
, frame
->header
.blockSizeInPCMFrames
, subframeBitsPerSample
, pSubframe
->lpcOrder
, pSubframe
->pSamplesS32
);
5371 case DRFLAC_SUBFRAME_LPC
:
5373 drflac__decode_samples__lpc(bs
, frame
->header
.blockSizeInPCMFrames
, subframeBitsPerSample
, pSubframe
->lpcOrder
, pSubframe
->pSamplesS32
);
5376 default: return DRFLAC_FALSE
;
5382 static drflac_bool32
drflac__seek_subframe(drflac_bs
* bs
, drflac_frame
* frame
, int subframeIndex
)
5384 drflac_subframe
* pSubframe
;
5385 drflac_uint32 subframeBitsPerSample
;
5387 DRFLAC_ASSERT(bs
!= NULL
);
5388 DRFLAC_ASSERT(frame
!= NULL
);
5390 pSubframe
= frame
->subframes
+ subframeIndex
;
5391 if (!drflac__read_subframe_header(bs
, pSubframe
)) {
5392 return DRFLAC_FALSE
;
5395 /* Side channels require an extra bit per sample. Took a while to figure that one out... */
5396 subframeBitsPerSample
= frame
->header
.bitsPerSample
;
5397 if ((frame
->header
.channelAssignment
== DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE
|| frame
->header
.channelAssignment
== DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE
) && subframeIndex
== 1) {
5398 subframeBitsPerSample
+= 1;
5399 } else if (frame
->header
.channelAssignment
== DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE
&& subframeIndex
== 0) {
5400 subframeBitsPerSample
+= 1;
5403 /* Need to handle wasted bits per sample. */
5404 if (pSubframe
->wastedBitsPerSample
>= subframeBitsPerSample
) {
5405 return DRFLAC_FALSE
;
5407 subframeBitsPerSample
-= pSubframe
->wastedBitsPerSample
;
5409 pSubframe
->pSamplesS32
= NULL
;
5411 switch (pSubframe
->subframeType
)
5413 case DRFLAC_SUBFRAME_CONSTANT
:
5415 if (!drflac__seek_bits(bs
, subframeBitsPerSample
)) {
5416 return DRFLAC_FALSE
;
5420 case DRFLAC_SUBFRAME_VERBATIM
:
5422 unsigned int bitsToSeek
= frame
->header
.blockSizeInPCMFrames
* subframeBitsPerSample
;
5423 if (!drflac__seek_bits(bs
, bitsToSeek
)) {
5424 return DRFLAC_FALSE
;
5428 case DRFLAC_SUBFRAME_FIXED
:
5430 unsigned int bitsToSeek
= pSubframe
->lpcOrder
* subframeBitsPerSample
;
5431 if (!drflac__seek_bits(bs
, bitsToSeek
)) {
5432 return DRFLAC_FALSE
;
5435 if (!drflac__read_and_seek_residual(bs
, frame
->header
.blockSizeInPCMFrames
, pSubframe
->lpcOrder
)) {
5436 return DRFLAC_FALSE
;
5440 case DRFLAC_SUBFRAME_LPC
:
5442 drflac_uint8 lpcPrecision
;
5444 unsigned int bitsToSeek
= pSubframe
->lpcOrder
* subframeBitsPerSample
;
5445 if (!drflac__seek_bits(bs
, bitsToSeek
)) {
5446 return DRFLAC_FALSE
;
5449 if (!drflac__read_uint8(bs
, 4, &lpcPrecision
)) {
5450 return DRFLAC_FALSE
;
5452 if (lpcPrecision
== 15) {
5453 return DRFLAC_FALSE
; /* Invalid. */
5458 bitsToSeek
= (pSubframe
->lpcOrder
* lpcPrecision
) + 5; /* +5 for shift. */
5459 if (!drflac__seek_bits(bs
, bitsToSeek
)) {
5460 return DRFLAC_FALSE
;
5463 if (!drflac__read_and_seek_residual(bs
, frame
->header
.blockSizeInPCMFrames
, pSubframe
->lpcOrder
)) {
5464 return DRFLAC_FALSE
;
5468 default: return DRFLAC_FALSE
;
5475 static DRFLAC_INLINE drflac_uint8
drflac__get_channel_count_from_channel_assignment(drflac_int8 channelAssignment
)
5477 drflac_uint8 lookup
[] = {1, 2, 3, 4, 5, 6, 7, 8, 2, 2, 2};
5479 DRFLAC_ASSERT(channelAssignment
<= 10);
5480 return lookup
[channelAssignment
];
5483 static drflac_result
drflac__decode_flac_frame(drflac
* pFlac
)
5487 drflac_uint8 paddingSizeInBits
;
5488 drflac_uint16 desiredCRC16
;
5489 #ifndef DR_FLAC_NO_CRC
5490 drflac_uint16 actualCRC16
;
5493 /* This function should be called while the stream is sitting on the first byte after the frame header. */
5494 DRFLAC_ZERO_MEMORY(pFlac
->currentFLACFrame
.subframes
, sizeof(pFlac
->currentFLACFrame
.subframes
));
5496 /* The frame block size must never be larger than the maximum block size defined by the FLAC stream. */
5497 if (pFlac
->currentFLACFrame
.header
.blockSizeInPCMFrames
> pFlac
->maxBlockSizeInPCMFrames
) {
5498 return DRFLAC_ERROR
;
5501 /* The number of channels in the frame must match the channel count from the STREAMINFO block. */
5502 channelCount
= drflac__get_channel_count_from_channel_assignment(pFlac
->currentFLACFrame
.header
.channelAssignment
);
5503 if (channelCount
!= (int)pFlac
->channels
) {
5504 return DRFLAC_ERROR
;
5507 for (i
= 0; i
< channelCount
; ++i
) {
5508 if (!drflac__decode_subframe(&pFlac
->bs
, &pFlac
->currentFLACFrame
, i
, pFlac
->pDecodedSamples
+ (pFlac
->currentFLACFrame
.header
.blockSizeInPCMFrames
* i
))) {
5509 return DRFLAC_ERROR
;
5513 paddingSizeInBits
= (drflac_uint8
)(DRFLAC_CACHE_L1_BITS_REMAINING(&pFlac
->bs
) & 7);
5514 if (paddingSizeInBits
> 0) {
5515 drflac_uint8 padding
= 0;
5516 if (!drflac__read_uint8(&pFlac
->bs
, paddingSizeInBits
, &padding
)) {
5517 return DRFLAC_AT_END
;
5521 #ifndef DR_FLAC_NO_CRC
5522 actualCRC16
= drflac__flush_crc16(&pFlac
->bs
);
5524 if (!drflac__read_uint16(&pFlac
->bs
, 16, &desiredCRC16
)) {
5525 return DRFLAC_AT_END
;
5528 #ifndef DR_FLAC_NO_CRC
5529 if (actualCRC16
!= desiredCRC16
) {
5530 return DRFLAC_CRC_MISMATCH
; /* CRC mismatch. */
5534 pFlac
->currentFLACFrame
.pcmFramesRemaining
= pFlac
->currentFLACFrame
.header
.blockSizeInPCMFrames
;
5536 return DRFLAC_SUCCESS
;
5539 static drflac_result
drflac__seek_flac_frame(drflac
* pFlac
)
5543 drflac_uint16 desiredCRC16
;
5544 #ifndef DR_FLAC_NO_CRC
5545 drflac_uint16 actualCRC16
;
5548 channelCount
= drflac__get_channel_count_from_channel_assignment(pFlac
->currentFLACFrame
.header
.channelAssignment
);
5549 for (i
= 0; i
< channelCount
; ++i
) {
5550 if (!drflac__seek_subframe(&pFlac
->bs
, &pFlac
->currentFLACFrame
, i
)) {
5551 return DRFLAC_ERROR
;
5556 if (!drflac__seek_bits(&pFlac
->bs
, DRFLAC_CACHE_L1_BITS_REMAINING(&pFlac
->bs
) & 7)) {
5557 return DRFLAC_ERROR
;
5561 #ifndef DR_FLAC_NO_CRC
5562 actualCRC16
= drflac__flush_crc16(&pFlac
->bs
);
5564 if (!drflac__read_uint16(&pFlac
->bs
, 16, &desiredCRC16
)) {
5565 return DRFLAC_AT_END
;
5568 #ifndef DR_FLAC_NO_CRC
5569 if (actualCRC16
!= desiredCRC16
) {
5570 return DRFLAC_CRC_MISMATCH
; /* CRC mismatch. */
5574 return DRFLAC_SUCCESS
;
5577 static drflac_bool32
drflac__read_and_decode_next_flac_frame(drflac
* pFlac
)
5579 DRFLAC_ASSERT(pFlac
!= NULL
);
5582 drflac_result result
;
5584 if (!drflac__read_next_flac_frame_header(&pFlac
->bs
, pFlac
->bitsPerSample
, &pFlac
->currentFLACFrame
.header
)) {
5585 return DRFLAC_FALSE
;
5588 result
= drflac__decode_flac_frame(pFlac
);
5589 if (result
!= DRFLAC_SUCCESS
) {
5590 if (result
== DRFLAC_CRC_MISMATCH
) {
5591 continue; /* CRC mismatch. Skip to the next frame. */
5593 return DRFLAC_FALSE
;
5601 static void drflac__get_pcm_frame_range_of_current_flac_frame(drflac
* pFlac
, drflac_uint64
* pFirstPCMFrame
, drflac_uint64
* pLastPCMFrame
)
5603 drflac_uint64 firstPCMFrame
;
5604 drflac_uint64 lastPCMFrame
;
5606 DRFLAC_ASSERT(pFlac
!= NULL
);
5608 firstPCMFrame
= pFlac
->currentFLACFrame
.header
.pcmFrameNumber
;
5609 if (firstPCMFrame
== 0) {
5610 firstPCMFrame
= ((drflac_uint64
)pFlac
->currentFLACFrame
.header
.flacFrameNumber
) * pFlac
->maxBlockSizeInPCMFrames
;
5613 lastPCMFrame
= firstPCMFrame
+ pFlac
->currentFLACFrame
.header
.blockSizeInPCMFrames
;
5614 if (lastPCMFrame
> 0) {
5615 lastPCMFrame
-= 1; /* Needs to be zero based. */
5618 if (pFirstPCMFrame
) {
5619 *pFirstPCMFrame
= firstPCMFrame
;
5621 if (pLastPCMFrame
) {
5622 *pLastPCMFrame
= lastPCMFrame
;
5626 static drflac_bool32
drflac__seek_to_first_frame(drflac
* pFlac
)
5628 drflac_bool32 result
;
5630 DRFLAC_ASSERT(pFlac
!= NULL
);
5632 result
= drflac__seek_to_byte(&pFlac
->bs
, pFlac
->firstFLACFramePosInBytes
);
5634 DRFLAC_ZERO_MEMORY(&pFlac
->currentFLACFrame
, sizeof(pFlac
->currentFLACFrame
));
5635 pFlac
->currentPCMFrame
= 0;
5640 static DRFLAC_INLINE drflac_result
drflac__seek_to_next_flac_frame(drflac
* pFlac
)
5642 /* This function should only ever be called while the decoder is sitting on the first byte past the FRAME_HEADER section. */
5643 DRFLAC_ASSERT(pFlac
!= NULL
);
5644 return drflac__seek_flac_frame(pFlac
);
5648 static drflac_uint64
drflac__seek_forward_by_pcm_frames(drflac
* pFlac
, drflac_uint64 pcmFramesToSeek
)
5650 drflac_uint64 pcmFramesRead
= 0;
5651 while (pcmFramesToSeek
> 0) {
5652 if (pFlac
->currentFLACFrame
.pcmFramesRemaining
== 0) {
5653 if (!drflac__read_and_decode_next_flac_frame(pFlac
)) {
5654 break; /* Couldn't read the next frame, so just break from the loop and return. */
5657 if (pFlac
->currentFLACFrame
.pcmFramesRemaining
> pcmFramesToSeek
) {
5658 pcmFramesRead
+= pcmFramesToSeek
;
5659 pFlac
->currentFLACFrame
.pcmFramesRemaining
-= (drflac_uint32
)pcmFramesToSeek
; /* <-- Safe cast. Will always be < currentFrame.pcmFramesRemaining < 65536. */
5660 pcmFramesToSeek
= 0;
5662 pcmFramesRead
+= pFlac
->currentFLACFrame
.pcmFramesRemaining
;
5663 pcmFramesToSeek
-= pFlac
->currentFLACFrame
.pcmFramesRemaining
;
5664 pFlac
->currentFLACFrame
.pcmFramesRemaining
= 0;
5669 pFlac
->currentPCMFrame
+= pcmFramesRead
;
5670 return pcmFramesRead
;
5674 static drflac_bool32
drflac__seek_to_pcm_frame__brute_force(drflac
* pFlac
, drflac_uint64 pcmFrameIndex
)
5676 drflac_bool32 isMidFrame
= DRFLAC_FALSE
;
5677 drflac_uint64 runningPCMFrameCount
;
5679 DRFLAC_ASSERT(pFlac
!= NULL
);
5681 /* 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. */
5682 if (pcmFrameIndex
>= pFlac
->currentPCMFrame
) {
5683 /* Seeking forward. Need to seek from the current position. */
5684 runningPCMFrameCount
= pFlac
->currentPCMFrame
;
5686 /* The frame header for the first frame may not yet have been read. We need to do that if necessary. */
5687 if (pFlac
->currentPCMFrame
== 0 && pFlac
->currentFLACFrame
.pcmFramesRemaining
== 0) {
5688 if (!drflac__read_next_flac_frame_header(&pFlac
->bs
, pFlac
->bitsPerSample
, &pFlac
->currentFLACFrame
.header
)) {
5689 return DRFLAC_FALSE
;
5692 isMidFrame
= DRFLAC_TRUE
;
5695 /* Seeking backwards. Need to seek from the start of the file. */
5696 runningPCMFrameCount
= 0;
5698 /* Move back to the start. */
5699 if (!drflac__seek_to_first_frame(pFlac
)) {
5700 return DRFLAC_FALSE
;
5703 /* Decode the first frame in preparation for sample-exact seeking below. */
5704 if (!drflac__read_next_flac_frame_header(&pFlac
->bs
, pFlac
->bitsPerSample
, &pFlac
->currentFLACFrame
.header
)) {
5705 return DRFLAC_FALSE
;
5710 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
5711 header. If based on the header we can determine that the frame contains the sample, we do a full decode of that frame.
5714 drflac_uint64 pcmFrameCountInThisFLACFrame
;
5715 drflac_uint64 firstPCMFrameInFLACFrame
= 0;
5716 drflac_uint64 lastPCMFrameInFLACFrame
= 0;
5718 drflac__get_pcm_frame_range_of_current_flac_frame(pFlac
, &firstPCMFrameInFLACFrame
, &lastPCMFrameInFLACFrame
);
5720 pcmFrameCountInThisFLACFrame
= (lastPCMFrameInFLACFrame
- firstPCMFrameInFLACFrame
) + 1;
5721 if (pcmFrameIndex
< (runningPCMFrameCount
+ pcmFrameCountInThisFLACFrame
)) {
5723 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
5724 it never existed and keep iterating.
5726 drflac_uint64 pcmFramesToDecode
= pcmFrameIndex
- runningPCMFrameCount
;
5729 drflac_result result
= drflac__decode_flac_frame(pFlac
);
5730 if (result
== DRFLAC_SUCCESS
) {
5731 /* The frame is valid. We just need to skip over some samples to ensure it's sample-exact. */
5732 return drflac__seek_forward_by_pcm_frames(pFlac
, pcmFramesToDecode
) == pcmFramesToDecode
; /* <-- If this fails, something bad has happened (it should never fail). */
5734 if (result
== DRFLAC_CRC_MISMATCH
) {
5735 goto next_iteration
; /* CRC mismatch. Pretend this frame never existed. */
5737 return DRFLAC_FALSE
;
5741 /* We started seeking mid-frame which means we need to skip the frame decoding part. */
5742 return drflac__seek_forward_by_pcm_frames(pFlac
, pcmFramesToDecode
) == pcmFramesToDecode
;
5746 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
5747 frame never existed and leave the running sample count untouched.
5750 drflac_result result
= drflac__seek_to_next_flac_frame(pFlac
);
5751 if (result
== DRFLAC_SUCCESS
) {
5752 runningPCMFrameCount
+= pcmFrameCountInThisFLACFrame
;
5754 if (result
== DRFLAC_CRC_MISMATCH
) {
5755 goto next_iteration
; /* CRC mismatch. Pretend this frame never existed. */
5757 return DRFLAC_FALSE
;
5762 We started seeking mid-frame which means we need to seek by reading to the end of the frame instead of with
5763 drflac__seek_to_next_flac_frame() which only works if the decoder is sitting on the byte just after the frame header.
5765 runningPCMFrameCount
+= pFlac
->currentFLACFrame
.pcmFramesRemaining
;
5766 pFlac
->currentFLACFrame
.pcmFramesRemaining
= 0;
5767 isMidFrame
= DRFLAC_FALSE
;
5770 /* If we are seeking to the end of the file and we've just hit it, we're done. */
5771 if (pcmFrameIndex
== pFlac
->totalPCMFrameCount
&& runningPCMFrameCount
== pFlac
->totalPCMFrameCount
) {
5777 /* Grab the next frame in preparation for the next iteration. */
5778 if (!drflac__read_next_flac_frame_header(&pFlac
->bs
, pFlac
->bitsPerSample
, &pFlac
->currentFLACFrame
.header
)) {
5779 return DRFLAC_FALSE
;
5785 #if !defined(DR_FLAC_NO_CRC)
5787 We use an average compression ratio to determine our approximate start location. FLAC files are generally about 50%-70% the size of their
5788 uncompressed counterparts so we'll use this as a basis. I'm going to split the middle and use a factor of 0.6 to determine the starting
5791 #define DRFLAC_BINARY_SEARCH_APPROX_COMPRESSION_RATIO 0.6f
5793 static drflac_bool32
drflac__seek_to_approximate_flac_frame_to_byte(drflac
* pFlac
, drflac_uint64 targetByte
, drflac_uint64 rangeLo
, drflac_uint64 rangeHi
, drflac_uint64
* pLastSuccessfulSeekOffset
)
5795 DRFLAC_ASSERT(pFlac
!= NULL
);
5796 DRFLAC_ASSERT(pLastSuccessfulSeekOffset
!= NULL
);
5797 DRFLAC_ASSERT(targetByte
>= rangeLo
);
5798 DRFLAC_ASSERT(targetByte
<= rangeHi
);
5800 *pLastSuccessfulSeekOffset
= pFlac
->firstFLACFramePosInBytes
;
5803 /* After rangeLo == rangeHi == targetByte fails, we need to break out. */
5804 drflac_uint64 lastTargetByte
= targetByte
;
5806 /* When seeking to a byte, failure probably means we've attempted to seek beyond the end of the stream. To counter this we just halve it each attempt. */
5807 if (!drflac__seek_to_byte(&pFlac
->bs
, targetByte
)) {
5808 /* If we couldn't even seek to the first byte in the stream we have a problem. Just abandon the whole thing. */
5809 if (targetByte
== 0) {
5810 drflac__seek_to_first_frame(pFlac
); /* Try to recover. */
5811 return DRFLAC_FALSE
;
5814 /* Halve the byte location and continue. */
5815 targetByte
= rangeLo
+ ((rangeHi
- rangeLo
)/2);
5816 rangeHi
= targetByte
;
5818 /* Getting here should mean that we have seeked to an appropriate byte. */
5820 /* Clear the details of the FLAC frame so we don't misreport data. */
5821 DRFLAC_ZERO_MEMORY(&pFlac
->currentFLACFrame
, sizeof(pFlac
->currentFLACFrame
));
5824 Now seek to the next FLAC frame. We need to decode the entire frame (not just the header) because it's possible for the header to incorrectly pass the
5825 CRC check and return bad data. We need to decode the entire frame to be more certain. Although this seems unlikely, this has happened to me in testing
5826 so it needs to stay this way for now.
5829 if (!drflac__read_and_decode_next_flac_frame(pFlac
)) {
5830 /* Halve the byte location and continue. */
5831 targetByte
= rangeLo
+ ((rangeHi
- rangeLo
)/2);
5832 rangeHi
= targetByte
;
5837 if (!drflac__read_next_flac_frame_header(&pFlac
->bs
, pFlac
->bitsPerSample
, &pFlac
->currentFLACFrame
.header
)) {
5838 /* Halve the byte location and continue. */
5839 targetByte
= rangeLo
+ ((rangeHi
- rangeLo
)/2);
5840 rangeHi
= targetByte
;
5847 /* We already tried this byte and there are no more to try, break out. */
5848 if(targetByte
== lastTargetByte
) {
5849 return DRFLAC_FALSE
;
5853 /* The current PCM frame needs to be updated based on the frame we just seeked to. */
5854 drflac__get_pcm_frame_range_of_current_flac_frame(pFlac
, &pFlac
->currentPCMFrame
, NULL
);
5856 DRFLAC_ASSERT(targetByte
<= rangeHi
);
5858 *pLastSuccessfulSeekOffset
= targetByte
;
5862 static drflac_bool32
drflac__decode_flac_frame_and_seek_forward_by_pcm_frames(drflac
* pFlac
, drflac_uint64 offset
)
5864 /* This section of code would be used if we were only decoding the FLAC frame header when calling drflac__seek_to_approximate_flac_frame_to_byte(). */
5866 if (drflac__decode_flac_frame(pFlac
) != DRFLAC_SUCCESS
) {
5867 /* We failed to decode this frame which may be due to it being corrupt. We'll just use the next valid FLAC frame. */
5868 if (drflac__read_and_decode_next_flac_frame(pFlac
) == DRFLAC_FALSE
) {
5869 return DRFLAC_FALSE
;
5874 return drflac__seek_forward_by_pcm_frames(pFlac
, offset
) == offset
;
5878 static drflac_bool32
drflac__seek_to_pcm_frame__binary_search_internal(drflac
* pFlac
, drflac_uint64 pcmFrameIndex
, drflac_uint64 byteRangeLo
, drflac_uint64 byteRangeHi
)
5880 /* This assumes pFlac->currentPCMFrame is sitting on byteRangeLo upon entry. */
5882 drflac_uint64 targetByte
;
5883 drflac_uint64 pcmRangeLo
= pFlac
->totalPCMFrameCount
;
5884 drflac_uint64 pcmRangeHi
= 0;
5885 drflac_uint64 lastSuccessfulSeekOffset
= (drflac_uint64
)-1;
5886 drflac_uint64 closestSeekOffsetBeforeTargetPCMFrame
= byteRangeLo
;
5887 drflac_uint32 seekForwardThreshold
= (pFlac
->maxBlockSizeInPCMFrames
!= 0) ? pFlac
->maxBlockSizeInPCMFrames
*2 : 4096;
5889 targetByte
= byteRangeLo
+ (drflac_uint64
)(((drflac_int64
)((pcmFrameIndex
- pFlac
->currentPCMFrame
) * pFlac
->channels
* pFlac
->bitsPerSample
)/8.0f
) * DRFLAC_BINARY_SEARCH_APPROX_COMPRESSION_RATIO
);
5890 if (targetByte
> byteRangeHi
) {
5891 targetByte
= byteRangeHi
;
5895 if (drflac__seek_to_approximate_flac_frame_to_byte(pFlac
, targetByte
, byteRangeLo
, byteRangeHi
, &lastSuccessfulSeekOffset
)) {
5896 /* We found a FLAC frame. We need to check if it contains the sample we're looking for. */
5897 drflac_uint64 newPCMRangeLo
;
5898 drflac_uint64 newPCMRangeHi
;
5899 drflac__get_pcm_frame_range_of_current_flac_frame(pFlac
, &newPCMRangeLo
, &newPCMRangeHi
);
5901 /* If we selected the same frame, it means we should be pretty close. Just decode the rest. */
5902 if (pcmRangeLo
== newPCMRangeLo
) {
5903 if (!drflac__seek_to_approximate_flac_frame_to_byte(pFlac
, closestSeekOffsetBeforeTargetPCMFrame
, closestSeekOffsetBeforeTargetPCMFrame
, byteRangeHi
, &lastSuccessfulSeekOffset
)) {
5904 break; /* Failed to seek to closest frame. */
5907 if (drflac__decode_flac_frame_and_seek_forward_by_pcm_frames(pFlac
, pcmFrameIndex
- pFlac
->currentPCMFrame
)) {
5910 break; /* Failed to seek forward. */
5914 pcmRangeLo
= newPCMRangeLo
;
5915 pcmRangeHi
= newPCMRangeHi
;
5917 if (pcmRangeLo
<= pcmFrameIndex
&& pcmRangeHi
>= pcmFrameIndex
) {
5918 /* The target PCM frame is in this FLAC frame. */
5919 if (drflac__decode_flac_frame_and_seek_forward_by_pcm_frames(pFlac
, pcmFrameIndex
- pFlac
->currentPCMFrame
) ) {
5922 break; /* Failed to seek to FLAC frame. */
5925 const float approxCompressionRatio
= (drflac_int64
)(lastSuccessfulSeekOffset
- pFlac
->firstFLACFramePosInBytes
) / ((drflac_int64
)(pcmRangeLo
* pFlac
->channels
* pFlac
->bitsPerSample
)/8.0f
);
5927 if (pcmRangeLo
> pcmFrameIndex
) {
5928 /* We seeked too far forward. We need to move our target byte backward and try again. */
5929 byteRangeHi
= lastSuccessfulSeekOffset
;
5930 if (byteRangeLo
> byteRangeHi
) {
5931 byteRangeLo
= byteRangeHi
;
5934 targetByte
= byteRangeLo
+ ((byteRangeHi
- byteRangeLo
) / 2);
5935 if (targetByte
< byteRangeLo
) {
5936 targetByte
= byteRangeLo
;
5938 } else /*if (pcmRangeHi < pcmFrameIndex)*/ {
5939 /* We didn't seek far enough. We need to move our target byte forward and try again. */
5941 /* If we're close enough we can just seek forward. */
5942 if ((pcmFrameIndex
- pcmRangeLo
) < seekForwardThreshold
) {
5943 if (drflac__decode_flac_frame_and_seek_forward_by_pcm_frames(pFlac
, pcmFrameIndex
- pFlac
->currentPCMFrame
)) {
5946 break; /* Failed to seek to FLAC frame. */
5949 byteRangeLo
= lastSuccessfulSeekOffset
;
5950 if (byteRangeHi
< byteRangeLo
) {
5951 byteRangeHi
= byteRangeLo
;
5954 targetByte
= lastSuccessfulSeekOffset
+ (drflac_uint64
)(((drflac_int64
)((pcmFrameIndex
-pcmRangeLo
) * pFlac
->channels
* pFlac
->bitsPerSample
)/8.0f
) * approxCompressionRatio
);
5955 if (targetByte
> byteRangeHi
) {
5956 targetByte
= byteRangeHi
;
5959 if (closestSeekOffsetBeforeTargetPCMFrame
< lastSuccessfulSeekOffset
) {
5960 closestSeekOffsetBeforeTargetPCMFrame
= lastSuccessfulSeekOffset
;
5966 /* Getting here is really bad. We just recover as best we can, but moving to the first frame in the stream, and then abort. */
5971 drflac__seek_to_first_frame(pFlac
); /* <-- Try to recover. */
5972 return DRFLAC_FALSE
;
5975 static drflac_bool32
drflac__seek_to_pcm_frame__binary_search(drflac
* pFlac
, drflac_uint64 pcmFrameIndex
)
5977 drflac_uint64 byteRangeLo
;
5978 drflac_uint64 byteRangeHi
;
5979 drflac_uint32 seekForwardThreshold
= (pFlac
->maxBlockSizeInPCMFrames
!= 0) ? pFlac
->maxBlockSizeInPCMFrames
*2 : 4096;
5981 /* Our algorithm currently assumes the FLAC stream is currently sitting at the start. */
5982 if (drflac__seek_to_first_frame(pFlac
) == DRFLAC_FALSE
) {
5983 return DRFLAC_FALSE
;
5986 /* If we're close enough to the start, just move to the start and seek forward. */
5987 if (pcmFrameIndex
< seekForwardThreshold
) {
5988 return drflac__seek_forward_by_pcm_frames(pFlac
, pcmFrameIndex
) == pcmFrameIndex
;
5992 Our starting byte range is the byte position of the first FLAC frame and the approximate end of the file as if it were completely uncompressed. This ensures
5993 the entire file is included, even though most of the time it'll exceed the end of the actual stream. This is OK as the frame searching logic will handle it.
5995 byteRangeLo
= pFlac
->firstFLACFramePosInBytes
;
5996 byteRangeHi
= pFlac
->firstFLACFramePosInBytes
+ (drflac_uint64
)((drflac_int64
)(pFlac
->totalPCMFrameCount
* pFlac
->channels
* pFlac
->bitsPerSample
)/8.0f
);
5998 return drflac__seek_to_pcm_frame__binary_search_internal(pFlac
, pcmFrameIndex
, byteRangeLo
, byteRangeHi
);
6000 #endif /* !DR_FLAC_NO_CRC */
6002 static drflac_bool32
drflac__seek_to_pcm_frame__seek_table(drflac
* pFlac
, drflac_uint64 pcmFrameIndex
)
6004 drflac_uint32 iClosestSeekpoint
= 0;
6005 drflac_bool32 isMidFrame
= DRFLAC_FALSE
;
6006 drflac_uint64 runningPCMFrameCount
;
6007 drflac_uint32 iSeekpoint
;
6010 DRFLAC_ASSERT(pFlac
!= NULL
);
6012 if (pFlac
->pSeekpoints
== NULL
|| pFlac
->seekpointCount
== 0) {
6013 return DRFLAC_FALSE
;
6016 for (iSeekpoint
= 0; iSeekpoint
< pFlac
->seekpointCount
; ++iSeekpoint
) {
6017 if (pFlac
->pSeekpoints
[iSeekpoint
].firstPCMFrame
>= pcmFrameIndex
) {
6021 iClosestSeekpoint
= iSeekpoint
;
6024 /* There's been cases where the seek table contains only zeros. We need to do some basic validation on the closest seekpoint. */
6025 if (pFlac
->pSeekpoints
[iClosestSeekpoint
].pcmFrameCount
== 0 || pFlac
->pSeekpoints
[iClosestSeekpoint
].pcmFrameCount
> pFlac
->maxBlockSizeInPCMFrames
) {
6026 return DRFLAC_FALSE
;
6028 if (pFlac
->pSeekpoints
[iClosestSeekpoint
].firstPCMFrame
> pFlac
->totalPCMFrameCount
&& pFlac
->totalPCMFrameCount
> 0) {
6029 return DRFLAC_FALSE
;
6032 #if !defined(DR_FLAC_NO_CRC)
6033 /* At this point we should know the closest seek point. We can use a binary search for this. We need to know the total sample count for this. */
6034 if (pFlac
->totalPCMFrameCount
> 0) {
6035 drflac_uint64 byteRangeLo
;
6036 drflac_uint64 byteRangeHi
;
6038 byteRangeHi
= pFlac
->firstFLACFramePosInBytes
+ (drflac_uint64
)((drflac_int64
)(pFlac
->totalPCMFrameCount
* pFlac
->channels
* pFlac
->bitsPerSample
)/8.0f
);
6039 byteRangeLo
= pFlac
->firstFLACFramePosInBytes
+ pFlac
->pSeekpoints
[iClosestSeekpoint
].flacFrameOffset
;
6042 If our closest seek point is not the last one, we only need to search between it and the next one. The section below calculates an appropriate starting
6043 value for byteRangeHi which will clamp it appropriately.
6045 Note that the next seekpoint must have an offset greater than the closest seekpoint because otherwise our binary search algorithm will break down. There
6046 have been cases where a seektable consists of seek points where every byte offset is set to 0 which causes problems. If this happens we need to abort.
6048 if (iClosestSeekpoint
< pFlac
->seekpointCount
-1) {
6049 drflac_uint32 iNextSeekpoint
= iClosestSeekpoint
+ 1;
6051 /* Basic validation on the seekpoints to ensure they're usable. */
6052 if (pFlac
->pSeekpoints
[iClosestSeekpoint
].flacFrameOffset
>= pFlac
->pSeekpoints
[iNextSeekpoint
].flacFrameOffset
|| pFlac
->pSeekpoints
[iNextSeekpoint
].pcmFrameCount
== 0) {
6053 return DRFLAC_FALSE
; /* The next seekpoint doesn't look right. The seek table cannot be trusted from here. Abort. */
6056 if (pFlac
->pSeekpoints
[iNextSeekpoint
].firstPCMFrame
!= (((drflac_uint64
)0xFFFFFFFF << 32) | 0xFFFFFFFF)) { /* Make sure it's not a placeholder seekpoint. */
6057 byteRangeHi
= pFlac
->firstFLACFramePosInBytes
+ pFlac
->pSeekpoints
[iNextSeekpoint
].flacFrameOffset
- 1; /* byteRangeHi must be zero based. */
6061 if (drflac__seek_to_byte(&pFlac
->bs
, pFlac
->firstFLACFramePosInBytes
+ pFlac
->pSeekpoints
[iClosestSeekpoint
].flacFrameOffset
)) {
6062 if (drflac__read_next_flac_frame_header(&pFlac
->bs
, pFlac
->bitsPerSample
, &pFlac
->currentFLACFrame
.header
)) {
6063 drflac__get_pcm_frame_range_of_current_flac_frame(pFlac
, &pFlac
->currentPCMFrame
, NULL
);
6065 if (drflac__seek_to_pcm_frame__binary_search_internal(pFlac
, pcmFrameIndex
, byteRangeLo
, byteRangeHi
)) {
6071 #endif /* !DR_FLAC_NO_CRC */
6073 /* Getting here means we need to use a slower algorithm because the binary search method failed or cannot be used. */
6076 If we are seeking forward and the closest seekpoint is _before_ the current sample, we just seek forward from where we are. Otherwise we start seeking
6077 from the seekpoint's first sample.
6079 if (pcmFrameIndex
>= pFlac
->currentPCMFrame
&& pFlac
->pSeekpoints
[iClosestSeekpoint
].firstPCMFrame
<= pFlac
->currentPCMFrame
) {
6080 /* Optimized case. Just seek forward from where we are. */
6081 runningPCMFrameCount
= pFlac
->currentPCMFrame
;
6083 /* The frame header for the first frame may not yet have been read. We need to do that if necessary. */
6084 if (pFlac
->currentPCMFrame
== 0 && pFlac
->currentFLACFrame
.pcmFramesRemaining
== 0) {
6085 if (!drflac__read_next_flac_frame_header(&pFlac
->bs
, pFlac
->bitsPerSample
, &pFlac
->currentFLACFrame
.header
)) {
6086 return DRFLAC_FALSE
;
6089 isMidFrame
= DRFLAC_TRUE
;
6092 /* Slower case. Seek to the start of the seekpoint and then seek forward from there. */
6093 runningPCMFrameCount
= pFlac
->pSeekpoints
[iClosestSeekpoint
].firstPCMFrame
;
6095 if (!drflac__seek_to_byte(&pFlac
->bs
, pFlac
->firstFLACFramePosInBytes
+ pFlac
->pSeekpoints
[iClosestSeekpoint
].flacFrameOffset
)) {
6096 return DRFLAC_FALSE
;
6099 /* Grab the frame the seekpoint is sitting on in preparation for the sample-exact seeking below. */
6100 if (!drflac__read_next_flac_frame_header(&pFlac
->bs
, pFlac
->bitsPerSample
, &pFlac
->currentFLACFrame
.header
)) {
6101 return DRFLAC_FALSE
;
6106 drflac_uint64 pcmFrameCountInThisFLACFrame
;
6107 drflac_uint64 firstPCMFrameInFLACFrame
= 0;
6108 drflac_uint64 lastPCMFrameInFLACFrame
= 0;
6110 drflac__get_pcm_frame_range_of_current_flac_frame(pFlac
, &firstPCMFrameInFLACFrame
, &lastPCMFrameInFLACFrame
);
6112 pcmFrameCountInThisFLACFrame
= (lastPCMFrameInFLACFrame
- firstPCMFrameInFLACFrame
) + 1;
6113 if (pcmFrameIndex
< (runningPCMFrameCount
+ pcmFrameCountInThisFLACFrame
)) {
6115 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
6116 it never existed and keep iterating.
6118 drflac_uint64 pcmFramesToDecode
= pcmFrameIndex
- runningPCMFrameCount
;
6121 drflac_result result
= drflac__decode_flac_frame(pFlac
);
6122 if (result
== DRFLAC_SUCCESS
) {
6123 /* The frame is valid. We just need to skip over some samples to ensure it's sample-exact. */
6124 return drflac__seek_forward_by_pcm_frames(pFlac
, pcmFramesToDecode
) == pcmFramesToDecode
; /* <-- If this fails, something bad has happened (it should never fail). */
6126 if (result
== DRFLAC_CRC_MISMATCH
) {
6127 goto next_iteration
; /* CRC mismatch. Pretend this frame never existed. */
6129 return DRFLAC_FALSE
;
6133 /* We started seeking mid-frame which means we need to skip the frame decoding part. */
6134 return drflac__seek_forward_by_pcm_frames(pFlac
, pcmFramesToDecode
) == pcmFramesToDecode
;
6138 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
6139 frame never existed and leave the running sample count untouched.
6142 drflac_result result
= drflac__seek_to_next_flac_frame(pFlac
);
6143 if (result
== DRFLAC_SUCCESS
) {
6144 runningPCMFrameCount
+= pcmFrameCountInThisFLACFrame
;
6146 if (result
== DRFLAC_CRC_MISMATCH
) {
6147 goto next_iteration
; /* CRC mismatch. Pretend this frame never existed. */
6149 return DRFLAC_FALSE
;
6154 We started seeking mid-frame which means we need to seek by reading to the end of the frame instead of with
6155 drflac__seek_to_next_flac_frame() which only works if the decoder is sitting on the byte just after the frame header.
6157 runningPCMFrameCount
+= pFlac
->currentFLACFrame
.pcmFramesRemaining
;
6158 pFlac
->currentFLACFrame
.pcmFramesRemaining
= 0;
6159 isMidFrame
= DRFLAC_FALSE
;
6162 /* If we are seeking to the end of the file and we've just hit it, we're done. */
6163 if (pcmFrameIndex
== pFlac
->totalPCMFrameCount
&& runningPCMFrameCount
== pFlac
->totalPCMFrameCount
) {
6169 /* Grab the next frame in preparation for the next iteration. */
6170 if (!drflac__read_next_flac_frame_header(&pFlac
->bs
, pFlac
->bitsPerSample
, &pFlac
->currentFLACFrame
.header
)) {
6171 return DRFLAC_FALSE
;
6177 #ifndef DR_FLAC_NO_OGG
6180 drflac_uint8 capturePattern
[4]; /* Should be "OggS" */
6181 drflac_uint8 structureVersion
; /* Always 0. */
6182 drflac_uint8 headerType
;
6183 drflac_uint64 granulePosition
;
6184 drflac_uint32 serialNumber
;
6185 drflac_uint32 sequenceNumber
;
6186 drflac_uint32 checksum
;
6187 drflac_uint8 segmentCount
;
6188 drflac_uint8 segmentTable
[255];
6189 } drflac_ogg_page_header
;
6194 drflac_read_proc onRead
;
6195 drflac_seek_proc onSeek
;
6196 drflac_meta_proc onMeta
;
6197 drflac_container container
;
6200 drflac_uint32 sampleRate
;
6201 drflac_uint8 channels
;
6202 drflac_uint8 bitsPerSample
;
6203 drflac_uint64 totalPCMFrameCount
;
6204 drflac_uint16 maxBlockSizeInPCMFrames
;
6205 drflac_uint64 runningFilePos
;
6206 drflac_bool32 hasStreamInfoBlock
;
6207 drflac_bool32 hasMetadataBlocks
;
6208 drflac_bs bs
; /* <-- A bit streamer is required for loading data during initialization. */
6209 drflac_frame_header firstFrameHeader
; /* <-- The header of the first frame that was read during relaxed initalization. Only set if there is no STREAMINFO block. */
6211 #ifndef DR_FLAC_NO_OGG
6212 drflac_uint32 oggSerial
;
6213 drflac_uint64 oggFirstBytePos
;
6214 drflac_ogg_page_header oggBosHeader
;
6218 static DRFLAC_INLINE
void drflac__decode_block_header(drflac_uint32 blockHeader
, drflac_uint8
* isLastBlock
, drflac_uint8
* blockType
, drflac_uint32
* blockSize
)
6220 blockHeader
= drflac__be2host_32(blockHeader
);
6221 *isLastBlock
= (drflac_uint8
)((blockHeader
& 0x80000000UL
) >> 31);
6222 *blockType
= (drflac_uint8
)((blockHeader
& 0x7F000000UL
) >> 24);
6223 *blockSize
= (blockHeader
& 0x00FFFFFFUL
);
6226 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
)
6228 drflac_uint32 blockHeader
;
6231 if (onRead(pUserData
, &blockHeader
, 4) != 4) {
6232 return DRFLAC_FALSE
;
6235 drflac__decode_block_header(blockHeader
, isLastBlock
, blockType
, blockSize
);
6239 static drflac_bool32
drflac__read_streaminfo(drflac_read_proc onRead
, void* pUserData
, drflac_streaminfo
* pStreamInfo
)
6241 drflac_uint32 blockSizes
;
6242 drflac_uint64 frameSizes
= 0;
6243 drflac_uint64 importantProps
;
6244 drflac_uint8 md5
[16];
6246 /* min/max block size. */
6247 if (onRead(pUserData
, &blockSizes
, 4) != 4) {
6248 return DRFLAC_FALSE
;
6251 /* min/max frame size. */
6252 if (onRead(pUserData
, &frameSizes
, 6) != 6) {
6253 return DRFLAC_FALSE
;
6256 /* Sample rate, channels, bits per sample and total sample count. */
6257 if (onRead(pUserData
, &importantProps
, 8) != 8) {
6258 return DRFLAC_FALSE
;
6262 if (onRead(pUserData
, md5
, sizeof(md5
)) != sizeof(md5
)) {
6263 return DRFLAC_FALSE
;
6266 blockSizes
= drflac__be2host_32(blockSizes
);
6267 frameSizes
= drflac__be2host_64(frameSizes
);
6268 importantProps
= drflac__be2host_64(importantProps
);
6270 pStreamInfo
->minBlockSizeInPCMFrames
= (drflac_uint16
)((blockSizes
& 0xFFFF0000) >> 16);
6271 pStreamInfo
->maxBlockSizeInPCMFrames
= (drflac_uint16
) (blockSizes
& 0x0000FFFF);
6272 pStreamInfo
->minFrameSizeInPCMFrames
= (drflac_uint32
)((frameSizes
& (((drflac_uint64
)0x00FFFFFF << 16) << 24)) >> 40);
6273 pStreamInfo
->maxFrameSizeInPCMFrames
= (drflac_uint32
)((frameSizes
& (((drflac_uint64
)0x00FFFFFF << 16) << 0)) >> 16);
6274 pStreamInfo
->sampleRate
= (drflac_uint32
)((importantProps
& (((drflac_uint64
)0x000FFFFF << 16) << 28)) >> 44);
6275 pStreamInfo
->channels
= (drflac_uint8
)((importantProps
& (((drflac_uint64
)0x0000000E << 16) << 24)) >> 41) + 1;
6276 pStreamInfo
->bitsPerSample
= (drflac_uint8
)((importantProps
& (((drflac_uint64
)0x0000001F << 16) << 20)) >> 36) + 1;
6277 pStreamInfo
->totalPCMFrameCount
= ((importantProps
& ((((drflac_uint64
)0x0000000F << 16) << 16) | 0xFFFFFFFF)));
6278 DRFLAC_COPY_MEMORY(pStreamInfo
->md5
, md5
, sizeof(md5
));
6284 static void* drflac__malloc_default(size_t sz
, void* pUserData
)
6287 return DRFLAC_MALLOC(sz
);
6290 static void* drflac__realloc_default(void* p
, size_t sz
, void* pUserData
)
6293 return DRFLAC_REALLOC(p
, sz
);
6296 static void drflac__free_default(void* p
, void* pUserData
)
6303 static void* drflac__malloc_from_callbacks(size_t sz
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
6305 if (pAllocationCallbacks
== NULL
) {
6309 if (pAllocationCallbacks
->onMalloc
!= NULL
) {
6310 return pAllocationCallbacks
->onMalloc(sz
, pAllocationCallbacks
->pUserData
);
6313 /* Try using realloc(). */
6314 if (pAllocationCallbacks
->onRealloc
!= NULL
) {
6315 return pAllocationCallbacks
->onRealloc(NULL
, sz
, pAllocationCallbacks
->pUserData
);
6321 static void* drflac__realloc_from_callbacks(void* p
, size_t szNew
, size_t szOld
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
6323 if (pAllocationCallbacks
== NULL
) {
6327 if (pAllocationCallbacks
->onRealloc
!= NULL
) {
6328 return pAllocationCallbacks
->onRealloc(p
, szNew
, pAllocationCallbacks
->pUserData
);
6331 /* Try emulating realloc() in terms of malloc()/free(). */
6332 if (pAllocationCallbacks
->onMalloc
!= NULL
&& pAllocationCallbacks
->onFree
!= NULL
) {
6335 p2
= pAllocationCallbacks
->onMalloc(szNew
, pAllocationCallbacks
->pUserData
);
6341 DRFLAC_COPY_MEMORY(p2
, p
, szOld
);
6342 pAllocationCallbacks
->onFree(p
, pAllocationCallbacks
->pUserData
);
6351 static void drflac__free_from_callbacks(void* p
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
6353 if (p
== NULL
|| pAllocationCallbacks
== NULL
) {
6357 if (pAllocationCallbacks
->onFree
!= NULL
) {
6358 pAllocationCallbacks
->onFree(p
, pAllocationCallbacks
->pUserData
);
6363 static 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
, drflac_allocation_callbacks
* pAllocationCallbacks
)
6366 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
6367 we'll be sitting on byte 42.
6369 drflac_uint64 runningFilePos
= 42;
6370 drflac_uint64 seektablePos
= 0;
6371 drflac_uint32 seektableSize
= 0;
6374 drflac_metadata metadata
;
6375 drflac_uint8 isLastBlock
= 0;
6376 drflac_uint8 blockType
;
6377 drflac_uint32 blockSize
;
6378 if (drflac__read_and_decode_block_header(onRead
, pUserData
, &isLastBlock
, &blockType
, &blockSize
) == DRFLAC_FALSE
) {
6379 return DRFLAC_FALSE
;
6381 runningFilePos
+= 4;
6383 metadata
.type
= blockType
;
6384 metadata
.pRawData
= NULL
;
6385 metadata
.rawDataSize
= 0;
6389 case DRFLAC_METADATA_BLOCK_TYPE_APPLICATION
:
6391 if (blockSize
< 4) {
6392 return DRFLAC_FALSE
;
6396 void* pRawData
= drflac__malloc_from_callbacks(blockSize
, pAllocationCallbacks
);
6397 if (pRawData
== NULL
) {
6398 return DRFLAC_FALSE
;
6401 if (onRead(pUserData
, pRawData
, blockSize
) != blockSize
) {
6402 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6403 return DRFLAC_FALSE
;
6406 metadata
.pRawData
= pRawData
;
6407 metadata
.rawDataSize
= blockSize
;
6408 metadata
.data
.application
.id
= drflac__be2host_32(*(drflac_uint32
*)pRawData
);
6409 metadata
.data
.application
.pData
= (const void*)((drflac_uint8
*)pRawData
+ sizeof(drflac_uint32
));
6410 metadata
.data
.application
.dataSize
= blockSize
- sizeof(drflac_uint32
);
6411 onMeta(pUserDataMD
, &metadata
);
6413 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6417 case DRFLAC_METADATA_BLOCK_TYPE_SEEKTABLE
:
6419 seektablePos
= runningFilePos
;
6420 seektableSize
= blockSize
;
6423 drflac_uint32 iSeekpoint
;
6426 pRawData
= drflac__malloc_from_callbacks(blockSize
, pAllocationCallbacks
);
6427 if (pRawData
== NULL
) {
6428 return DRFLAC_FALSE
;
6431 if (onRead(pUserData
, pRawData
, blockSize
) != blockSize
) {
6432 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6433 return DRFLAC_FALSE
;
6436 metadata
.pRawData
= pRawData
;
6437 metadata
.rawDataSize
= blockSize
;
6438 metadata
.data
.seektable
.seekpointCount
= blockSize
/sizeof(drflac_seekpoint
);
6439 metadata
.data
.seektable
.pSeekpoints
= (const drflac_seekpoint
*)pRawData
;
6442 for (iSeekpoint
= 0; iSeekpoint
< metadata
.data
.seektable
.seekpointCount
; ++iSeekpoint
) {
6443 drflac_seekpoint
* pSeekpoint
= (drflac_seekpoint
*)pRawData
+ iSeekpoint
;
6444 pSeekpoint
->firstPCMFrame
= drflac__be2host_64(pSeekpoint
->firstPCMFrame
);
6445 pSeekpoint
->flacFrameOffset
= drflac__be2host_64(pSeekpoint
->flacFrameOffset
);
6446 pSeekpoint
->pcmFrameCount
= drflac__be2host_16(pSeekpoint
->pcmFrameCount
);
6449 onMeta(pUserDataMD
, &metadata
);
6451 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6455 case DRFLAC_METADATA_BLOCK_TYPE_VORBIS_COMMENT
:
6457 if (blockSize
< 8) {
6458 return DRFLAC_FALSE
;
6463 const char* pRunningData
;
6464 const char* pRunningDataEnd
;
6467 pRawData
= drflac__malloc_from_callbacks(blockSize
, pAllocationCallbacks
);
6468 if (pRawData
== NULL
) {
6469 return DRFLAC_FALSE
;
6472 if (onRead(pUserData
, pRawData
, blockSize
) != blockSize
) {
6473 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6474 return DRFLAC_FALSE
;
6477 metadata
.pRawData
= pRawData
;
6478 metadata
.rawDataSize
= blockSize
;
6480 pRunningData
= (const char*)pRawData
;
6481 pRunningDataEnd
= (const char*)pRawData
+ blockSize
;
6483 metadata
.data
.vorbis_comment
.vendorLength
= drflac__le2host_32(*(const drflac_uint32
*)pRunningData
); pRunningData
+= 4;
6485 /* Need space for the rest of the block */
6486 if ((pRunningDataEnd
- pRunningData
) - 4 < (drflac_int64
)metadata
.data
.vorbis_comment
.vendorLength
) { /* <-- Note the order of operations to avoid overflow to a valid value */
6487 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6488 return DRFLAC_FALSE
;
6490 metadata
.data
.vorbis_comment
.vendor
= pRunningData
; pRunningData
+= metadata
.data
.vorbis_comment
.vendorLength
;
6491 metadata
.data
.vorbis_comment
.commentCount
= drflac__le2host_32(*(const drflac_uint32
*)pRunningData
); pRunningData
+= 4;
6493 /* Need space for 'commentCount' comments after the block, which at minimum is a drflac_uint32 per comment */
6494 if ((pRunningDataEnd
- pRunningData
) / sizeof(drflac_uint32
) < metadata
.data
.vorbis_comment
.commentCount
) { /* <-- Note the order of operations to avoid overflow to a valid value */
6495 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6496 return DRFLAC_FALSE
;
6498 metadata
.data
.vorbis_comment
.pComments
= pRunningData
;
6500 /* Check that the comments section is valid before passing it to the callback */
6501 for (i
= 0; i
< metadata
.data
.vorbis_comment
.commentCount
; ++i
) {
6502 drflac_uint32 commentLength
;
6504 if (pRunningDataEnd
- pRunningData
< 4) {
6505 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6506 return DRFLAC_FALSE
;
6509 commentLength
= drflac__le2host_32(*(const drflac_uint32
*)pRunningData
); pRunningData
+= 4;
6510 if (pRunningDataEnd
- pRunningData
< (drflac_int64
)commentLength
) { /* <-- Note the order of operations to avoid overflow to a valid value */
6511 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6512 return DRFLAC_FALSE
;
6514 pRunningData
+= commentLength
;
6517 onMeta(pUserDataMD
, &metadata
);
6519 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6523 case DRFLAC_METADATA_BLOCK_TYPE_CUESHEET
:
6525 if (blockSize
< 396) {
6526 return DRFLAC_FALSE
;
6531 const char* pRunningData
;
6532 const char* pRunningDataEnd
;
6533 drflac_uint8 iTrack
;
6534 drflac_uint8 iIndex
;
6536 pRawData
= drflac__malloc_from_callbacks(blockSize
, pAllocationCallbacks
);
6537 if (pRawData
== NULL
) {
6538 return DRFLAC_FALSE
;
6541 if (onRead(pUserData
, pRawData
, blockSize
) != blockSize
) {
6542 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6543 return DRFLAC_FALSE
;
6546 metadata
.pRawData
= pRawData
;
6547 metadata
.rawDataSize
= blockSize
;
6549 pRunningData
= (const char*)pRawData
;
6550 pRunningDataEnd
= (const char*)pRawData
+ blockSize
;
6552 DRFLAC_COPY_MEMORY(metadata
.data
.cuesheet
.catalog
, pRunningData
, 128); pRunningData
+= 128;
6553 metadata
.data
.cuesheet
.leadInSampleCount
= drflac__be2host_64(*(const drflac_uint64
*)pRunningData
); pRunningData
+= 8;
6554 metadata
.data
.cuesheet
.isCD
= (pRunningData
[0] & 0x80) != 0; pRunningData
+= 259;
6555 metadata
.data
.cuesheet
.trackCount
= pRunningData
[0]; pRunningData
+= 1;
6556 metadata
.data
.cuesheet
.pTrackData
= pRunningData
;
6558 /* Check that the cuesheet tracks are valid before passing it to the callback */
6559 for (iTrack
= 0; iTrack
< metadata
.data
.cuesheet
.trackCount
; ++iTrack
) {
6560 drflac_uint8 indexCount
;
6561 drflac_uint32 indexPointSize
;
6563 if (pRunningDataEnd
- pRunningData
< 36) {
6564 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6565 return DRFLAC_FALSE
;
6568 /* Skip to the index point count */
6570 indexCount
= pRunningData
[0]; pRunningData
+= 1;
6571 indexPointSize
= indexCount
* sizeof(drflac_cuesheet_track_index
);
6572 if (pRunningDataEnd
- pRunningData
< (drflac_int64
)indexPointSize
) {
6573 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6574 return DRFLAC_FALSE
;
6578 for (iIndex
= 0; iIndex
< indexCount
; ++iIndex
) {
6579 drflac_cuesheet_track_index
* pTrack
= (drflac_cuesheet_track_index
*)pRunningData
;
6580 pRunningData
+= sizeof(drflac_cuesheet_track_index
);
6581 pTrack
->offset
= drflac__be2host_64(pTrack
->offset
);
6585 onMeta(pUserDataMD
, &metadata
);
6587 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6591 case DRFLAC_METADATA_BLOCK_TYPE_PICTURE
:
6593 if (blockSize
< 32) {
6594 return DRFLAC_FALSE
;
6599 const char* pRunningData
;
6600 const char* pRunningDataEnd
;
6602 pRawData
= drflac__malloc_from_callbacks(blockSize
, pAllocationCallbacks
);
6603 if (pRawData
== NULL
) {
6604 return DRFLAC_FALSE
;
6607 if (onRead(pUserData
, pRawData
, blockSize
) != blockSize
) {
6608 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6609 return DRFLAC_FALSE
;
6612 metadata
.pRawData
= pRawData
;
6613 metadata
.rawDataSize
= blockSize
;
6615 pRunningData
= (const char*)pRawData
;
6616 pRunningDataEnd
= (const char*)pRawData
+ blockSize
;
6618 metadata
.data
.picture
.type
= drflac__be2host_32(*(const drflac_uint32
*)pRunningData
); pRunningData
+= 4;
6619 metadata
.data
.picture
.mimeLength
= drflac__be2host_32(*(const drflac_uint32
*)pRunningData
); pRunningData
+= 4;
6621 /* Need space for the rest of the block */
6622 if ((pRunningDataEnd
- pRunningData
) - 24 < (drflac_int64
)metadata
.data
.picture
.mimeLength
) { /* <-- Note the order of operations to avoid overflow to a valid value */
6623 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6624 return DRFLAC_FALSE
;
6626 metadata
.data
.picture
.mime
= pRunningData
; pRunningData
+= metadata
.data
.picture
.mimeLength
;
6627 metadata
.data
.picture
.descriptionLength
= drflac__be2host_32(*(const drflac_uint32
*)pRunningData
); pRunningData
+= 4;
6629 /* Need space for the rest of the block */
6630 if ((pRunningDataEnd
- pRunningData
) - 20 < (drflac_int64
)metadata
.data
.picture
.descriptionLength
) { /* <-- Note the order of operations to avoid overflow to a valid value */
6631 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6632 return DRFLAC_FALSE
;
6634 metadata
.data
.picture
.description
= pRunningData
; pRunningData
+= metadata
.data
.picture
.descriptionLength
;
6635 metadata
.data
.picture
.width
= drflac__be2host_32(*(const drflac_uint32
*)pRunningData
); pRunningData
+= 4;
6636 metadata
.data
.picture
.height
= drflac__be2host_32(*(const drflac_uint32
*)pRunningData
); pRunningData
+= 4;
6637 metadata
.data
.picture
.colorDepth
= drflac__be2host_32(*(const drflac_uint32
*)pRunningData
); pRunningData
+= 4;
6638 metadata
.data
.picture
.indexColorCount
= drflac__be2host_32(*(const drflac_uint32
*)pRunningData
); pRunningData
+= 4;
6639 metadata
.data
.picture
.pictureDataSize
= drflac__be2host_32(*(const drflac_uint32
*)pRunningData
); pRunningData
+= 4;
6640 metadata
.data
.picture
.pPictureData
= (const drflac_uint8
*)pRunningData
;
6642 /* Need space for the picture after the block */
6643 if (pRunningDataEnd
- pRunningData
< (drflac_int64
)metadata
.data
.picture
.pictureDataSize
) { /* <-- Note the order of operations to avoid overflow to a valid value */
6644 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6645 return DRFLAC_FALSE
;
6648 onMeta(pUserDataMD
, &metadata
);
6650 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6654 case DRFLAC_METADATA_BLOCK_TYPE_PADDING
:
6657 metadata
.data
.padding
.unused
= 0;
6659 /* 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. */
6660 if (!onSeek(pUserData
, blockSize
, drflac_seek_origin_current
)) {
6661 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. */
6663 onMeta(pUserDataMD
, &metadata
);
6668 case DRFLAC_METADATA_BLOCK_TYPE_INVALID
:
6670 /* Invalid chunk. Just skip over this one. */
6672 if (!onSeek(pUserData
, blockSize
, drflac_seek_origin_current
)) {
6673 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. */
6681 It's an unknown chunk, but not necessarily invalid. There's a chance more metadata blocks might be defined later on, so we
6682 can at the very least report the chunk to the application and let it look at the raw data.
6685 void* pRawData
= drflac__malloc_from_callbacks(blockSize
, pAllocationCallbacks
);
6686 if (pRawData
== NULL
) {
6687 return DRFLAC_FALSE
;
6690 if (onRead(pUserData
, pRawData
, blockSize
) != blockSize
) {
6691 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6692 return DRFLAC_FALSE
;
6695 metadata
.pRawData
= pRawData
;
6696 metadata
.rawDataSize
= blockSize
;
6697 onMeta(pUserDataMD
, &metadata
);
6699 drflac__free_from_callbacks(pRawData
, pAllocationCallbacks
);
6704 /* 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. */
6705 if (onMeta
== NULL
&& blockSize
> 0) {
6706 if (!onSeek(pUserData
, blockSize
, drflac_seek_origin_current
)) {
6707 isLastBlock
= DRFLAC_TRUE
;
6711 runningFilePos
+= blockSize
;
6717 *pSeektablePos
= seektablePos
;
6718 *pSeektableSize
= seektableSize
;
6719 *pFirstFramePos
= runningFilePos
;
6724 static 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
)
6726 /* Pre Condition: The bit stream should be sitting just past the 4-byte id header. */
6728 drflac_uint8 isLastBlock
;
6729 drflac_uint8 blockType
;
6730 drflac_uint32 blockSize
;
6734 pInit
->container
= drflac_container_native
;
6736 /* The first metadata block should be the STREAMINFO block. */
6737 if (!drflac__read_and_decode_block_header(onRead
, pUserData
, &isLastBlock
, &blockType
, &blockSize
)) {
6738 return DRFLAC_FALSE
;
6741 if (blockType
!= DRFLAC_METADATA_BLOCK_TYPE_STREAMINFO
|| blockSize
!= 34) {
6743 /* We're opening in strict mode and the first block is not the STREAMINFO block. Error. */
6744 return DRFLAC_FALSE
;
6747 Relaxed mode. To open from here we need to just find the first frame and set the sample rate, etc. to whatever is defined
6750 pInit
->hasStreamInfoBlock
= DRFLAC_FALSE
;
6751 pInit
->hasMetadataBlocks
= DRFLAC_FALSE
;
6753 if (!drflac__read_next_flac_frame_header(&pInit
->bs
, 0, &pInit
->firstFrameHeader
)) {
6754 return DRFLAC_FALSE
; /* Couldn't find a frame. */
6757 if (pInit
->firstFrameHeader
.bitsPerSample
== 0) {
6758 return DRFLAC_FALSE
; /* Failed to initialize because the first frame depends on the STREAMINFO block, which does not exist. */
6761 pInit
->sampleRate
= pInit
->firstFrameHeader
.sampleRate
;
6762 pInit
->channels
= drflac__get_channel_count_from_channel_assignment(pInit
->firstFrameHeader
.channelAssignment
);
6763 pInit
->bitsPerSample
= pInit
->firstFrameHeader
.bitsPerSample
;
6764 pInit
->maxBlockSizeInPCMFrames
= 65535; /* <-- See notes here: https://xiph.org/flac/format.html#metadata_block_streaminfo */
6768 drflac_streaminfo streaminfo
;
6769 if (!drflac__read_streaminfo(onRead
, pUserData
, &streaminfo
)) {
6770 return DRFLAC_FALSE
;
6773 pInit
->hasStreamInfoBlock
= DRFLAC_TRUE
;
6774 pInit
->sampleRate
= streaminfo
.sampleRate
;
6775 pInit
->channels
= streaminfo
.channels
;
6776 pInit
->bitsPerSample
= streaminfo
.bitsPerSample
;
6777 pInit
->totalPCMFrameCount
= streaminfo
.totalPCMFrameCount
;
6778 pInit
->maxBlockSizeInPCMFrames
= streaminfo
.maxBlockSizeInPCMFrames
; /* Don't care about the min block size - only the max (used for determining the size of the memory allocation). */
6779 pInit
->hasMetadataBlocks
= !isLastBlock
;
6782 drflac_metadata metadata
;
6783 metadata
.type
= DRFLAC_METADATA_BLOCK_TYPE_STREAMINFO
;
6784 metadata
.pRawData
= NULL
;
6785 metadata
.rawDataSize
= 0;
6786 metadata
.data
.streaminfo
= streaminfo
;
6787 onMeta(pUserDataMD
, &metadata
);
6794 #ifndef DR_FLAC_NO_OGG
6795 #define DRFLAC_OGG_MAX_PAGE_SIZE 65307
6796 #define DRFLAC_OGG_CAPTURE_PATTERN_CRC32 1605413199 /* CRC-32 of "OggS". */
6800 drflac_ogg_recover_on_crc_mismatch
,
6801 drflac_ogg_fail_on_crc_mismatch
6802 } drflac_ogg_crc_mismatch_recovery
;
6804 #ifndef DR_FLAC_NO_CRC
6805 static drflac_uint32 drflac__crc32_table
[] = {
6806 0x00000000L
, 0x04C11DB7L
, 0x09823B6EL
, 0x0D4326D9L
,
6807 0x130476DCL
, 0x17C56B6BL
, 0x1A864DB2L
, 0x1E475005L
,
6808 0x2608EDB8L
, 0x22C9F00FL
, 0x2F8AD6D6L
, 0x2B4BCB61L
,
6809 0x350C9B64L
, 0x31CD86D3L
, 0x3C8EA00AL
, 0x384FBDBDL
,
6810 0x4C11DB70L
, 0x48D0C6C7L
, 0x4593E01EL
, 0x4152FDA9L
,
6811 0x5F15ADACL
, 0x5BD4B01BL
, 0x569796C2L
, 0x52568B75L
,
6812 0x6A1936C8L
, 0x6ED82B7FL
, 0x639B0DA6L
, 0x675A1011L
,
6813 0x791D4014L
, 0x7DDC5DA3L
, 0x709F7B7AL
, 0x745E66CDL
,
6814 0x9823B6E0L
, 0x9CE2AB57L
, 0x91A18D8EL
, 0x95609039L
,
6815 0x8B27C03CL
, 0x8FE6DD8BL
, 0x82A5FB52L
, 0x8664E6E5L
,
6816 0xBE2B5B58L
, 0xBAEA46EFL
, 0xB7A96036L
, 0xB3687D81L
,
6817 0xAD2F2D84L
, 0xA9EE3033L
, 0xA4AD16EAL
, 0xA06C0B5DL
,
6818 0xD4326D90L
, 0xD0F37027L
, 0xDDB056FEL
, 0xD9714B49L
,
6819 0xC7361B4CL
, 0xC3F706FBL
, 0xCEB42022L
, 0xCA753D95L
,
6820 0xF23A8028L
, 0xF6FB9D9FL
, 0xFBB8BB46L
, 0xFF79A6F1L
,
6821 0xE13EF6F4L
, 0xE5FFEB43L
, 0xE8BCCD9AL
, 0xEC7DD02DL
,
6822 0x34867077L
, 0x30476DC0L
, 0x3D044B19L
, 0x39C556AEL
,
6823 0x278206ABL
, 0x23431B1CL
, 0x2E003DC5L
, 0x2AC12072L
,
6824 0x128E9DCFL
, 0x164F8078L
, 0x1B0CA6A1L
, 0x1FCDBB16L
,
6825 0x018AEB13L
, 0x054BF6A4L
, 0x0808D07DL
, 0x0CC9CDCAL
,
6826 0x7897AB07L
, 0x7C56B6B0L
, 0x71159069L
, 0x75D48DDEL
,
6827 0x6B93DDDBL
, 0x6F52C06CL
, 0x6211E6B5L
, 0x66D0FB02L
,
6828 0x5E9F46BFL
, 0x5A5E5B08L
, 0x571D7DD1L
, 0x53DC6066L
,
6829 0x4D9B3063L
, 0x495A2DD4L
, 0x44190B0DL
, 0x40D816BAL
,
6830 0xACA5C697L
, 0xA864DB20L
, 0xA527FDF9L
, 0xA1E6E04EL
,
6831 0xBFA1B04BL
, 0xBB60ADFCL
, 0xB6238B25L
, 0xB2E29692L
,
6832 0x8AAD2B2FL
, 0x8E6C3698L
, 0x832F1041L
, 0x87EE0DF6L
,
6833 0x99A95DF3L
, 0x9D684044L
, 0x902B669DL
, 0x94EA7B2AL
,
6834 0xE0B41DE7L
, 0xE4750050L
, 0xE9362689L
, 0xEDF73B3EL
,
6835 0xF3B06B3BL
, 0xF771768CL
, 0xFA325055L
, 0xFEF34DE2L
,
6836 0xC6BCF05FL
, 0xC27DEDE8L
, 0xCF3ECB31L
, 0xCBFFD686L
,
6837 0xD5B88683L
, 0xD1799B34L
, 0xDC3ABDEDL
, 0xD8FBA05AL
,
6838 0x690CE0EEL
, 0x6DCDFD59L
, 0x608EDB80L
, 0x644FC637L
,
6839 0x7A089632L
, 0x7EC98B85L
, 0x738AAD5CL
, 0x774BB0EBL
,
6840 0x4F040D56L
, 0x4BC510E1L
, 0x46863638L
, 0x42472B8FL
,
6841 0x5C007B8AL
, 0x58C1663DL
, 0x558240E4L
, 0x51435D53L
,
6842 0x251D3B9EL
, 0x21DC2629L
, 0x2C9F00F0L
, 0x285E1D47L
,
6843 0x36194D42L
, 0x32D850F5L
, 0x3F9B762CL
, 0x3B5A6B9BL
,
6844 0x0315D626L
, 0x07D4CB91L
, 0x0A97ED48L
, 0x0E56F0FFL
,
6845 0x1011A0FAL
, 0x14D0BD4DL
, 0x19939B94L
, 0x1D528623L
,
6846 0xF12F560EL
, 0xF5EE4BB9L
, 0xF8AD6D60L
, 0xFC6C70D7L
,
6847 0xE22B20D2L
, 0xE6EA3D65L
, 0xEBA91BBCL
, 0xEF68060BL
,
6848 0xD727BBB6L
, 0xD3E6A601L
, 0xDEA580D8L
, 0xDA649D6FL
,
6849 0xC423CD6AL
, 0xC0E2D0DDL
, 0xCDA1F604L
, 0xC960EBB3L
,
6850 0xBD3E8D7EL
, 0xB9FF90C9L
, 0xB4BCB610L
, 0xB07DABA7L
,
6851 0xAE3AFBA2L
, 0xAAFBE615L
, 0xA7B8C0CCL
, 0xA379DD7BL
,
6852 0x9B3660C6L
, 0x9FF77D71L
, 0x92B45BA8L
, 0x9675461FL
,
6853 0x8832161AL
, 0x8CF30BADL
, 0x81B02D74L
, 0x857130C3L
,
6854 0x5D8A9099L
, 0x594B8D2EL
, 0x5408ABF7L
, 0x50C9B640L
,
6855 0x4E8EE645L
, 0x4A4FFBF2L
, 0x470CDD2BL
, 0x43CDC09CL
,
6856 0x7B827D21L
, 0x7F436096L
, 0x7200464FL
, 0x76C15BF8L
,
6857 0x68860BFDL
, 0x6C47164AL
, 0x61043093L
, 0x65C52D24L
,
6858 0x119B4BE9L
, 0x155A565EL
, 0x18197087L
, 0x1CD86D30L
,
6859 0x029F3D35L
, 0x065E2082L
, 0x0B1D065BL
, 0x0FDC1BECL
,
6860 0x3793A651L
, 0x3352BBE6L
, 0x3E119D3FL
, 0x3AD08088L
,
6861 0x2497D08DL
, 0x2056CD3AL
, 0x2D15EBE3L
, 0x29D4F654L
,
6862 0xC5A92679L
, 0xC1683BCEL
, 0xCC2B1D17L
, 0xC8EA00A0L
,
6863 0xD6AD50A5L
, 0xD26C4D12L
, 0xDF2F6BCBL
, 0xDBEE767CL
,
6864 0xE3A1CBC1L
, 0xE760D676L
, 0xEA23F0AFL
, 0xEEE2ED18L
,
6865 0xF0A5BD1DL
, 0xF464A0AAL
, 0xF9278673L
, 0xFDE69BC4L
,
6866 0x89B8FD09L
, 0x8D79E0BEL
, 0x803AC667L
, 0x84FBDBD0L
,
6867 0x9ABC8BD5L
, 0x9E7D9662L
, 0x933EB0BBL
, 0x97FFAD0CL
,
6868 0xAFB010B1L
, 0xAB710D06L
, 0xA6322BDFL
, 0xA2F33668L
,
6869 0xBCB4666DL
, 0xB8757BDAL
, 0xB5365D03L
, 0xB1F740B4L
6873 static DRFLAC_INLINE drflac_uint32
drflac_crc32_byte(drflac_uint32 crc32
, drflac_uint8 data
)
6875 #ifndef DR_FLAC_NO_CRC
6876 return (crc32
<< 8) ^ drflac__crc32_table
[(drflac_uint8
)((crc32
>> 24) & 0xFF) ^ data
];
6884 static DRFLAC_INLINE drflac_uint32
drflac_crc32_uint32(drflac_uint32 crc32
, drflac_uint32 data
)
6886 crc32
= drflac_crc32_byte(crc32
, (drflac_uint8
)((data
>> 24) & 0xFF));
6887 crc32
= drflac_crc32_byte(crc32
, (drflac_uint8
)((data
>> 16) & 0xFF));
6888 crc32
= drflac_crc32_byte(crc32
, (drflac_uint8
)((data
>> 8) & 0xFF));
6889 crc32
= drflac_crc32_byte(crc32
, (drflac_uint8
)((data
>> 0) & 0xFF));
6893 static DRFLAC_INLINE drflac_uint32
drflac_crc32_uint64(drflac_uint32 crc32
, drflac_uint64 data
)
6895 crc32
= drflac_crc32_uint32(crc32
, (drflac_uint32
)((data
>> 32) & 0xFFFFFFFF));
6896 crc32
= drflac_crc32_uint32(crc32
, (drflac_uint32
)((data
>> 0) & 0xFFFFFFFF));
6901 static DRFLAC_INLINE drflac_uint32
drflac_crc32_buffer(drflac_uint32 crc32
, drflac_uint8
* pData
, drflac_uint32 dataSize
)
6903 /* This can be optimized. */
6905 for (i
= 0; i
< dataSize
; ++i
) {
6906 crc32
= drflac_crc32_byte(crc32
, pData
[i
]);
6912 static DRFLAC_INLINE drflac_bool32
drflac_ogg__is_capture_pattern(drflac_uint8 pattern
[4])
6914 return pattern
[0] == 'O' && pattern
[1] == 'g' && pattern
[2] == 'g' && pattern
[3] == 'S';
6917 static DRFLAC_INLINE drflac_uint32
drflac_ogg__get_page_header_size(drflac_ogg_page_header
* pHeader
)
6919 return 27 + pHeader
->segmentCount
;
6922 static DRFLAC_INLINE drflac_uint32
drflac_ogg__get_page_body_size(drflac_ogg_page_header
* pHeader
)
6924 drflac_uint32 pageBodySize
= 0;
6927 for (i
= 0; i
< pHeader
->segmentCount
; ++i
) {
6928 pageBodySize
+= pHeader
->segmentTable
[i
];
6931 return pageBodySize
;
6934 static 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
)
6936 drflac_uint8 data
[23];
6939 DRFLAC_ASSERT(*pCRC32
== DRFLAC_OGG_CAPTURE_PATTERN_CRC32
);
6941 if (onRead(pUserData
, data
, 23) != 23) {
6942 return DRFLAC_AT_END
;
6947 It's not actually used, but set the capture pattern to 'OggS' for completeness. Not doing this will cause static analysers to complain about
6948 us trying to access uninitialized data. We could alternatively just comment out this member of the drflac_ogg_page_header structure, but I
6949 like to have it map to the structure of the underlying data.
6951 pHeader
->capturePattern
[0] = 'O';
6952 pHeader
->capturePattern
[1] = 'g';
6953 pHeader
->capturePattern
[2] = 'g';
6954 pHeader
->capturePattern
[3] = 'S';
6956 pHeader
->structureVersion
= data
[0];
6957 pHeader
->headerType
= data
[1];
6958 DRFLAC_COPY_MEMORY(&pHeader
->granulePosition
, &data
[ 2], 8);
6959 DRFLAC_COPY_MEMORY(&pHeader
->serialNumber
, &data
[10], 4);
6960 DRFLAC_COPY_MEMORY(&pHeader
->sequenceNumber
, &data
[14], 4);
6961 DRFLAC_COPY_MEMORY(&pHeader
->checksum
, &data
[18], 4);
6962 pHeader
->segmentCount
= data
[22];
6964 /* Calculate the CRC. Note that for the calculation the checksum part of the page needs to be set to 0. */
6970 for (i
= 0; i
< 23; ++i
) {
6971 *pCRC32
= drflac_crc32_byte(*pCRC32
, data
[i
]);
6975 if (onRead(pUserData
, pHeader
->segmentTable
, pHeader
->segmentCount
) != pHeader
->segmentCount
) {
6976 return DRFLAC_AT_END
;
6978 *pBytesRead
+= pHeader
->segmentCount
;
6980 for (i
= 0; i
< pHeader
->segmentCount
; ++i
) {
6981 *pCRC32
= drflac_crc32_byte(*pCRC32
, pHeader
->segmentTable
[i
]);
6984 return DRFLAC_SUCCESS
;
6987 static drflac_result
drflac_ogg__read_page_header(drflac_read_proc onRead
, void* pUserData
, drflac_ogg_page_header
* pHeader
, drflac_uint32
* pBytesRead
, drflac_uint32
* pCRC32
)
6993 if (onRead(pUserData
, id
, 4) != 4) {
6994 return DRFLAC_AT_END
;
6998 /* We need to read byte-by-byte until we find the OggS capture pattern. */
7000 if (drflac_ogg__is_capture_pattern(id
)) {
7001 drflac_result result
;
7003 *pCRC32
= DRFLAC_OGG_CAPTURE_PATTERN_CRC32
;
7005 result
= drflac_ogg__read_page_header_after_capture_pattern(onRead
, pUserData
, pHeader
, pBytesRead
, pCRC32
);
7006 if (result
== DRFLAC_SUCCESS
) {
7007 return DRFLAC_SUCCESS
;
7009 if (result
== DRFLAC_CRC_MISMATCH
) {
7016 /* The first 4 bytes did not equal the capture pattern. Read the next byte and try again. */
7020 if (onRead(pUserData
, &id
[3], 1) != 1) {
7021 return DRFLAC_AT_END
;
7030 The main part of the Ogg encapsulation is the conversion from the physical Ogg bitstream to the native FLAC bitstream. It works
7031 in three general stages: Ogg Physical Bitstream -> Ogg/FLAC Logical Bitstream -> FLAC Native Bitstream. dr_flac is designed
7032 in such a way that the core sections assume everything is delivered in native format. Therefore, for each encapsulation type
7033 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
7034 the physical Ogg bitstream are converted and delivered in native FLAC format.
7038 drflac_read_proc onRead
; /* The original onRead callback from drflac_open() and family. */
7039 drflac_seek_proc onSeek
; /* The original onSeek callback from drflac_open() and family. */
7040 void* pUserData
; /* The user data passed on onRead and onSeek. This is the user data that was passed on drflac_open() and family. */
7041 drflac_uint64 currentBytePos
; /* The position of the byte we are sitting on in the physical byte stream. Used for efficient seeking. */
7042 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. */
7043 drflac_uint32 serialNumber
; /* The serial number of the FLAC audio pages. This is determined by the initial header page that was read during initialization. */
7044 drflac_ogg_page_header bosPageHeader
; /* Used for seeking. */
7045 drflac_ogg_page_header currentPageHeader
;
7046 drflac_uint32 bytesRemainingInPage
;
7047 drflac_uint32 pageDataSize
;
7048 drflac_uint8 pageData
[DRFLAC_OGG_MAX_PAGE_SIZE
];
7049 } drflac_oggbs
; /* oggbs = Ogg Bitstream */
7051 static size_t drflac_oggbs__read_physical(drflac_oggbs
* oggbs
, void* bufferOut
, size_t bytesToRead
)
7053 size_t bytesActuallyRead
= oggbs
->onRead(oggbs
->pUserData
, bufferOut
, bytesToRead
);
7054 oggbs
->currentBytePos
+= bytesActuallyRead
;
7056 return bytesActuallyRead
;
7059 static drflac_bool32
drflac_oggbs__seek_physical(drflac_oggbs
* oggbs
, drflac_uint64 offset
, drflac_seek_origin origin
)
7061 if (origin
== drflac_seek_origin_start
) {
7062 if (offset
<= 0x7FFFFFFF) {
7063 if (!oggbs
->onSeek(oggbs
->pUserData
, (int)offset
, drflac_seek_origin_start
)) {
7064 return DRFLAC_FALSE
;
7066 oggbs
->currentBytePos
= offset
;
7070 if (!oggbs
->onSeek(oggbs
->pUserData
, 0x7FFFFFFF, drflac_seek_origin_start
)) {
7071 return DRFLAC_FALSE
;
7073 oggbs
->currentBytePos
= offset
;
7075 return drflac_oggbs__seek_physical(oggbs
, offset
- 0x7FFFFFFF, drflac_seek_origin_current
);
7078 while (offset
> 0x7FFFFFFF) {
7079 if (!oggbs
->onSeek(oggbs
->pUserData
, 0x7FFFFFFF, drflac_seek_origin_current
)) {
7080 return DRFLAC_FALSE
;
7082 oggbs
->currentBytePos
+= 0x7FFFFFFF;
7083 offset
-= 0x7FFFFFFF;
7086 if (!oggbs
->onSeek(oggbs
->pUserData
, (int)offset
, drflac_seek_origin_current
)) { /* <-- Safe cast thanks to the loop above. */
7087 return DRFLAC_FALSE
;
7089 oggbs
->currentBytePos
+= offset
;
7095 static drflac_bool32
drflac_oggbs__goto_next_page(drflac_oggbs
* oggbs
, drflac_ogg_crc_mismatch_recovery recoveryMethod
)
7097 drflac_ogg_page_header header
;
7099 drflac_uint32 crc32
= 0;
7100 drflac_uint32 bytesRead
;
7101 drflac_uint32 pageBodySize
;
7102 #ifndef DR_FLAC_NO_CRC
7103 drflac_uint32 actualCRC32
;
7106 if (drflac_ogg__read_page_header(oggbs
->onRead
, oggbs
->pUserData
, &header
, &bytesRead
, &crc32
) != DRFLAC_SUCCESS
) {
7107 return DRFLAC_FALSE
;
7109 oggbs
->currentBytePos
+= bytesRead
;
7111 pageBodySize
= drflac_ogg__get_page_body_size(&header
);
7112 if (pageBodySize
> DRFLAC_OGG_MAX_PAGE_SIZE
) {
7113 continue; /* Invalid page size. Assume it's corrupted and just move to the next page. */
7116 if (header
.serialNumber
!= oggbs
->serialNumber
) {
7117 /* It's not a FLAC page. Skip it. */
7118 if (pageBodySize
> 0 && !drflac_oggbs__seek_physical(oggbs
, pageBodySize
, drflac_seek_origin_current
)) {
7119 return DRFLAC_FALSE
;
7125 /* 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. */
7126 if (drflac_oggbs__read_physical(oggbs
, oggbs
->pageData
, pageBodySize
) != pageBodySize
) {
7127 return DRFLAC_FALSE
;
7129 oggbs
->pageDataSize
= pageBodySize
;
7131 #ifndef DR_FLAC_NO_CRC
7132 actualCRC32
= drflac_crc32_buffer(crc32
, oggbs
->pageData
, oggbs
->pageDataSize
);
7133 if (actualCRC32
!= header
.checksum
) {
7134 if (recoveryMethod
== drflac_ogg_recover_on_crc_mismatch
) {
7135 continue; /* CRC mismatch. Skip this page. */
7138 Even though we are failing on a CRC mismatch, we still want our stream to be in a good state. Therefore we
7139 go to the next valid page to ensure we're in a good state, but return false to let the caller know that the
7140 seek did not fully complete.
7142 drflac_oggbs__goto_next_page(oggbs
, drflac_ogg_recover_on_crc_mismatch
);
7143 return DRFLAC_FALSE
;
7147 (void)recoveryMethod
; /* <-- Silence a warning. */
7150 oggbs
->currentPageHeader
= header
;
7151 oggbs
->bytesRemainingInPage
= pageBodySize
;
7156 /* Function below is unused at the moment, but I might be re-adding it later. */
7158 static drflac_uint8
drflac_oggbs__get_current_segment_index(drflac_oggbs
* oggbs
, drflac_uint8
* pBytesRemainingInSeg
)
7160 drflac_uint32 bytesConsumedInPage
= drflac_ogg__get_page_body_size(&oggbs
->currentPageHeader
) - oggbs
->bytesRemainingInPage
;
7161 drflac_uint8 iSeg
= 0;
7162 drflac_uint32 iByte
= 0;
7163 while (iByte
< bytesConsumedInPage
) {
7164 drflac_uint8 segmentSize
= oggbs
->currentPageHeader
.segmentTable
[iSeg
];
7165 if (iByte
+ segmentSize
> bytesConsumedInPage
) {
7169 iByte
+= segmentSize
;
7173 *pBytesRemainingInSeg
= oggbs
->currentPageHeader
.segmentTable
[iSeg
] - (drflac_uint8
)(bytesConsumedInPage
- iByte
);
7177 static drflac_bool32
drflac_oggbs__seek_to_next_packet(drflac_oggbs
* oggbs
)
7179 /* 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. */
7181 drflac_bool32 atEndOfPage
= DRFLAC_FALSE
;
7183 drflac_uint8 bytesRemainingInSeg
;
7184 drflac_uint8 iFirstSeg
= drflac_oggbs__get_current_segment_index(oggbs
, &bytesRemainingInSeg
);
7186 drflac_uint32 bytesToEndOfPacketOrPage
= bytesRemainingInSeg
;
7187 for (drflac_uint8 iSeg
= iFirstSeg
; iSeg
< oggbs
->currentPageHeader
.segmentCount
; ++iSeg
) {
7188 drflac_uint8 segmentSize
= oggbs
->currentPageHeader
.segmentTable
[iSeg
];
7189 if (segmentSize
< 255) {
7190 if (iSeg
== oggbs
->currentPageHeader
.segmentCount
-1) {
7191 atEndOfPage
= DRFLAC_TRUE
;
7197 bytesToEndOfPacketOrPage
+= segmentSize
;
7201 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
7202 want to load the next page and keep searching for the end of the packet.
7204 drflac_oggbs__seek_physical(oggbs
, bytesToEndOfPacketOrPage
, drflac_seek_origin_current
);
7205 oggbs
->bytesRemainingInPage
-= bytesToEndOfPacketOrPage
;
7209 We're potentially at the next packet, but we need to check the next page first to be sure because the packet may
7212 if (!drflac_oggbs__goto_next_page(oggbs
)) {
7213 return DRFLAC_FALSE
;
7216 /* If it's a fresh packet it most likely means we're at the next packet. */
7217 if ((oggbs
->currentPageHeader
.headerType
& 0x01) == 0) {
7221 /* We're at the next packet. */
7227 static drflac_bool32
drflac_oggbs__seek_to_next_frame(drflac_oggbs
* oggbs
)
7229 /* The bitstream should be sitting on the first byte just after the header of the frame. */
7231 /* What we're actually doing here is seeking to the start of the next packet. */
7232 return drflac_oggbs__seek_to_next_packet(oggbs
);
7236 static size_t drflac__on_read_ogg(void* pUserData
, void* bufferOut
, size_t bytesToRead
)
7238 drflac_oggbs
* oggbs
= (drflac_oggbs
*)pUserData
;
7239 drflac_uint8
* pRunningBufferOut
= (drflac_uint8
*)bufferOut
;
7240 size_t bytesRead
= 0;
7242 DRFLAC_ASSERT(oggbs
!= NULL
);
7243 DRFLAC_ASSERT(pRunningBufferOut
!= NULL
);
7245 /* Reading is done page-by-page. If we've run out of bytes in the page we need to move to the next one. */
7246 while (bytesRead
< bytesToRead
) {
7247 size_t bytesRemainingToRead
= bytesToRead
- bytesRead
;
7249 if (oggbs
->bytesRemainingInPage
>= bytesRemainingToRead
) {
7250 DRFLAC_COPY_MEMORY(pRunningBufferOut
, oggbs
->pageData
+ (oggbs
->pageDataSize
- oggbs
->bytesRemainingInPage
), bytesRemainingToRead
);
7251 bytesRead
+= bytesRemainingToRead
;
7252 oggbs
->bytesRemainingInPage
-= (drflac_uint32
)bytesRemainingToRead
;
7256 /* If we get here it means some of the requested data is contained in the next pages. */
7257 if (oggbs
->bytesRemainingInPage
> 0) {
7258 DRFLAC_COPY_MEMORY(pRunningBufferOut
, oggbs
->pageData
+ (oggbs
->pageDataSize
- oggbs
->bytesRemainingInPage
), oggbs
->bytesRemainingInPage
);
7259 bytesRead
+= oggbs
->bytesRemainingInPage
;
7260 pRunningBufferOut
+= oggbs
->bytesRemainingInPage
;
7261 oggbs
->bytesRemainingInPage
= 0;
7264 DRFLAC_ASSERT(bytesRemainingToRead
> 0);
7265 if (!drflac_oggbs__goto_next_page(oggbs
, drflac_ogg_recover_on_crc_mismatch
)) {
7266 break; /* Failed to go to the next page. Might have simply hit the end of the stream. */
7273 static drflac_bool32
drflac__on_seek_ogg(void* pUserData
, int offset
, drflac_seek_origin origin
)
7275 drflac_oggbs
* oggbs
= (drflac_oggbs
*)pUserData
;
7276 int bytesSeeked
= 0;
7278 DRFLAC_ASSERT(oggbs
!= NULL
);
7279 DRFLAC_ASSERT(offset
>= 0); /* <-- Never seek backwards. */
7281 /* Seeking is always forward which makes things a lot simpler. */
7282 if (origin
== drflac_seek_origin_start
) {
7283 if (!drflac_oggbs__seek_physical(oggbs
, (int)oggbs
->firstBytePos
, drflac_seek_origin_start
)) {
7284 return DRFLAC_FALSE
;
7287 if (!drflac_oggbs__goto_next_page(oggbs
, drflac_ogg_fail_on_crc_mismatch
)) {
7288 return DRFLAC_FALSE
;
7291 return drflac__on_seek_ogg(pUserData
, offset
, drflac_seek_origin_current
);
7294 DRFLAC_ASSERT(origin
== drflac_seek_origin_current
);
7296 while (bytesSeeked
< offset
) {
7297 int bytesRemainingToSeek
= offset
- bytesSeeked
;
7298 DRFLAC_ASSERT(bytesRemainingToSeek
>= 0);
7300 if (oggbs
->bytesRemainingInPage
>= (size_t)bytesRemainingToSeek
) {
7301 bytesSeeked
+= bytesRemainingToSeek
;
7302 (void)bytesSeeked
; /* <-- Silence a dead store warning emitted by Clang Static Analyzer. */
7303 oggbs
->bytesRemainingInPage
-= bytesRemainingToSeek
;
7307 /* If we get here it means some of the requested data is contained in the next pages. */
7308 if (oggbs
->bytesRemainingInPage
> 0) {
7309 bytesSeeked
+= (int)oggbs
->bytesRemainingInPage
;
7310 oggbs
->bytesRemainingInPage
= 0;
7313 DRFLAC_ASSERT(bytesRemainingToSeek
> 0);
7314 if (!drflac_oggbs__goto_next_page(oggbs
, drflac_ogg_fail_on_crc_mismatch
)) {
7315 /* Failed to go to the next page. We either hit the end of the stream or had a CRC mismatch. */
7316 return DRFLAC_FALSE
;
7324 static drflac_bool32
drflac_ogg__seek_to_pcm_frame(drflac
* pFlac
, drflac_uint64 pcmFrameIndex
)
7326 drflac_oggbs
* oggbs
= (drflac_oggbs
*)pFlac
->_oggbs
;
7327 drflac_uint64 originalBytePos
;
7328 drflac_uint64 runningGranulePosition
;
7329 drflac_uint64 runningFrameBytePos
;
7330 drflac_uint64 runningPCMFrameCount
;
7332 DRFLAC_ASSERT(oggbs
!= NULL
);
7334 originalBytePos
= oggbs
->currentBytePos
; /* For recovery. Points to the OggS identifier. */
7336 /* First seek to the first frame. */
7337 if (!drflac__seek_to_byte(&pFlac
->bs
, pFlac
->firstFLACFramePosInBytes
)) {
7338 return DRFLAC_FALSE
;
7340 oggbs
->bytesRemainingInPage
= 0;
7342 runningGranulePosition
= 0;
7344 if (!drflac_oggbs__goto_next_page(oggbs
, drflac_ogg_recover_on_crc_mismatch
)) {
7345 drflac_oggbs__seek_physical(oggbs
, originalBytePos
, drflac_seek_origin_start
);
7346 return DRFLAC_FALSE
; /* Never did find that sample... */
7349 runningFrameBytePos
= oggbs
->currentBytePos
- drflac_ogg__get_page_header_size(&oggbs
->currentPageHeader
) - oggbs
->pageDataSize
;
7350 if (oggbs
->currentPageHeader
.granulePosition
>= pcmFrameIndex
) {
7351 break; /* The sample is somewhere in the previous page. */
7355 At this point we know the sample is not in the previous page. It could possibly be in this page. For simplicity we
7356 disregard any pages that do not begin a fresh packet.
7358 if ((oggbs
->currentPageHeader
.headerType
& 0x01) == 0) { /* <-- Is it a fresh page? */
7359 if (oggbs
->currentPageHeader
.segmentTable
[0] >= 2) {
7360 drflac_uint8 firstBytesInPage
[2];
7361 firstBytesInPage
[0] = oggbs
->pageData
[0];
7362 firstBytesInPage
[1] = oggbs
->pageData
[1];
7364 if ((firstBytesInPage
[0] == 0xFF) && (firstBytesInPage
[1] & 0xFC) == 0xF8) { /* <-- Does the page begin with a frame's sync code? */
7365 runningGranulePosition
= oggbs
->currentPageHeader
.granulePosition
;
7374 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
7375 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
7376 a new frame. This property means that after we've seeked to the page we can immediately start looping over frames until
7377 we find the one containing the target sample.
7379 if (!drflac_oggbs__seek_physical(oggbs
, runningFrameBytePos
, drflac_seek_origin_start
)) {
7380 return DRFLAC_FALSE
;
7382 if (!drflac_oggbs__goto_next_page(oggbs
, drflac_ogg_recover_on_crc_mismatch
)) {
7383 return DRFLAC_FALSE
;
7387 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
7388 looping over these frames until we find the one containing the sample we're after.
7390 runningPCMFrameCount
= runningGranulePosition
;
7393 There are two ways to find the sample and seek past irrelevant frames:
7394 1) Use the native FLAC decoder.
7395 2) Use Ogg's framing system.
7397 Both of these options have their own pros and cons. Using the native FLAC decoder is slower because it needs to
7398 do a full decode of the frame. Using Ogg's framing system is faster, but more complicated and involves some code
7399 duplication for the decoding of frame headers.
7401 Another thing to consider is that using the Ogg framing system will perform direct seeking of the physical Ogg
7402 bitstream. This is important to consider because it means we cannot read data from the drflac_bs object using the
7403 standard drflac__*() APIs because that will read in extra data for its own internal caching which in turn breaks
7404 the positioning of the read pointer of the physical Ogg bitstream. Therefore, anything that would normally be read
7405 using the native FLAC decoding APIs, such as drflac__read_next_flac_frame_header(), need to be re-implemented so as to
7406 avoid the use of the drflac_bs object.
7408 Considering these issues, I have decided to use the slower native FLAC decoding method for the following reasons:
7409 1) Seeking is already partially accelerated using Ogg's paging system in the code block above.
7410 2) Seeking in an Ogg encapsulated FLAC stream is probably quite uncommon.
7413 drflac_uint64 firstPCMFrameInFLACFrame
= 0;
7414 drflac_uint64 lastPCMFrameInFLACFrame
= 0;
7415 drflac_uint64 pcmFrameCountInThisFrame
;
7417 if (!drflac__read_next_flac_frame_header(&pFlac
->bs
, pFlac
->bitsPerSample
, &pFlac
->currentFLACFrame
.header
)) {
7418 return DRFLAC_FALSE
;
7421 drflac__get_pcm_frame_range_of_current_flac_frame(pFlac
, &firstPCMFrameInFLACFrame
, &lastPCMFrameInFLACFrame
);
7423 pcmFrameCountInThisFrame
= (lastPCMFrameInFLACFrame
- firstPCMFrameInFLACFrame
) + 1;
7425 /* If we are seeking to the end of the file and we've just hit it, we're done. */
7426 if (pcmFrameIndex
== pFlac
->totalPCMFrameCount
&& (runningPCMFrameCount
+ pcmFrameCountInThisFrame
) == pFlac
->totalPCMFrameCount
) {
7427 drflac_result result
= drflac__decode_flac_frame(pFlac
);
7428 if (result
== DRFLAC_SUCCESS
) {
7429 pFlac
->currentPCMFrame
= pcmFrameIndex
;
7430 pFlac
->currentFLACFrame
.pcmFramesRemaining
= 0;
7433 return DRFLAC_FALSE
;
7437 if (pcmFrameIndex
< (runningPCMFrameCount
+ pcmFrameCountInThisFrame
)) {
7439 The sample should be in this FLAC frame. We need to fully decode it, however if it's an invalid frame (a CRC mismatch), we need to pretend
7440 it never existed and keep iterating.
7442 drflac_result result
= drflac__decode_flac_frame(pFlac
);
7443 if (result
== DRFLAC_SUCCESS
) {
7444 /* The frame is valid. We just need to skip over some samples to ensure it's sample-exact. */
7445 drflac_uint64 pcmFramesToDecode
= (size_t)(pcmFrameIndex
- runningPCMFrameCount
); /* <-- Safe cast because the maximum number of samples in a frame is 65535. */
7446 if (pcmFramesToDecode
== 0) {
7450 pFlac
->currentPCMFrame
= runningPCMFrameCount
;
7452 return drflac__seek_forward_by_pcm_frames(pFlac
, pcmFramesToDecode
) == pcmFramesToDecode
; /* <-- If this fails, something bad has happened (it should never fail). */
7454 if (result
== DRFLAC_CRC_MISMATCH
) {
7455 continue; /* CRC mismatch. Pretend this frame never existed. */
7457 return DRFLAC_FALSE
;
7462 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
7463 frame never existed and leave the running sample count untouched.
7465 drflac_result result
= drflac__seek_to_next_flac_frame(pFlac
);
7466 if (result
== DRFLAC_SUCCESS
) {
7467 runningPCMFrameCount
+= pcmFrameCountInThisFrame
;
7469 if (result
== DRFLAC_CRC_MISMATCH
) {
7470 continue; /* CRC mismatch. Pretend this frame never existed. */
7472 return DRFLAC_FALSE
;
7481 static 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
)
7483 drflac_ogg_page_header header
;
7484 drflac_uint32 crc32
= DRFLAC_OGG_CAPTURE_PATTERN_CRC32
;
7485 drflac_uint32 bytesRead
= 0;
7487 /* Pre Condition: The bit stream should be sitting just past the 4-byte OggS capture pattern. */
7490 pInit
->container
= drflac_container_ogg
;
7491 pInit
->oggFirstBytePos
= 0;
7494 We'll get here if the first 4 bytes of the stream were the OggS capture pattern, however it doesn't necessarily mean the
7495 stream includes FLAC encoded audio. To check for this we need to scan the beginning-of-stream page markers and check if
7496 any match the FLAC specification. Important to keep in mind that the stream may be multiplexed.
7498 if (drflac_ogg__read_page_header_after_capture_pattern(onRead
, pUserData
, &header
, &bytesRead
, &crc32
) != DRFLAC_SUCCESS
) {
7499 return DRFLAC_FALSE
;
7501 pInit
->runningFilePos
+= bytesRead
;
7506 /* Break if we're past the beginning of stream page. */
7507 if ((header
.headerType
& 0x02) == 0) {
7508 return DRFLAC_FALSE
;
7511 /* Check if it's a FLAC header. */
7512 pageBodySize
= drflac_ogg__get_page_body_size(&header
);
7513 if (pageBodySize
== 51) { /* 51 = the lacing value of the FLAC header packet. */
7514 /* It could be a FLAC page... */
7515 drflac_uint32 bytesRemainingInPage
= pageBodySize
;
7516 drflac_uint8 packetType
;
7518 if (onRead(pUserData
, &packetType
, 1) != 1) {
7519 return DRFLAC_FALSE
;
7522 bytesRemainingInPage
-= 1;
7523 if (packetType
== 0x7F) {
7524 /* Increasingly more likely to be a FLAC page... */
7525 drflac_uint8 sig
[4];
7526 if (onRead(pUserData
, sig
, 4) != 4) {
7527 return DRFLAC_FALSE
;
7530 bytesRemainingInPage
-= 4;
7531 if (sig
[0] == 'F' && sig
[1] == 'L' && sig
[2] == 'A' && sig
[3] == 'C') {
7532 /* Almost certainly a FLAC page... */
7533 drflac_uint8 mappingVersion
[2];
7534 if (onRead(pUserData
, mappingVersion
, 2) != 2) {
7535 return DRFLAC_FALSE
;
7538 if (mappingVersion
[0] != 1) {
7539 return DRFLAC_FALSE
; /* Only supporting version 1.x of the Ogg mapping. */
7543 The next 2 bytes are the non-audio packets, not including this one. We don't care about this because we're going to
7544 be handling it in a generic way based on the serial number and packet types.
7546 if (!onSeek(pUserData
, 2, drflac_seek_origin_current
)) {
7547 return DRFLAC_FALSE
;
7550 /* Expecting the native FLAC signature "fLaC". */
7551 if (onRead(pUserData
, sig
, 4) != 4) {
7552 return DRFLAC_FALSE
;
7555 if (sig
[0] == 'f' && sig
[1] == 'L' && sig
[2] == 'a' && sig
[3] == 'C') {
7556 /* The remaining data in the page should be the STREAMINFO block. */
7557 drflac_streaminfo streaminfo
;
7558 drflac_uint8 isLastBlock
;
7559 drflac_uint8 blockType
;
7560 drflac_uint32 blockSize
;
7561 if (!drflac__read_and_decode_block_header(onRead
, pUserData
, &isLastBlock
, &blockType
, &blockSize
)) {
7562 return DRFLAC_FALSE
;
7565 if (blockType
!= DRFLAC_METADATA_BLOCK_TYPE_STREAMINFO
|| blockSize
!= 34) {
7566 return DRFLAC_FALSE
; /* Invalid block type. First block must be the STREAMINFO block. */
7569 if (drflac__read_streaminfo(onRead
, pUserData
, &streaminfo
)) {
7571 pInit
->hasStreamInfoBlock
= DRFLAC_TRUE
;
7572 pInit
->sampleRate
= streaminfo
.sampleRate
;
7573 pInit
->channels
= streaminfo
.channels
;
7574 pInit
->bitsPerSample
= streaminfo
.bitsPerSample
;
7575 pInit
->totalPCMFrameCount
= streaminfo
.totalPCMFrameCount
;
7576 pInit
->maxBlockSizeInPCMFrames
= streaminfo
.maxBlockSizeInPCMFrames
;
7577 pInit
->hasMetadataBlocks
= !isLastBlock
;
7580 drflac_metadata metadata
;
7581 metadata
.type
= DRFLAC_METADATA_BLOCK_TYPE_STREAMINFO
;
7582 metadata
.pRawData
= NULL
;
7583 metadata
.rawDataSize
= 0;
7584 metadata
.data
.streaminfo
= streaminfo
;
7585 onMeta(pUserDataMD
, &metadata
);
7588 pInit
->runningFilePos
+= pageBodySize
;
7589 pInit
->oggFirstBytePos
= pInit
->runningFilePos
- 79; /* Subtracting 79 will place us right on top of the "OggS" identifier of the FLAC bos page. */
7590 pInit
->oggSerial
= header
.serialNumber
;
7591 pInit
->oggBosHeader
= header
;
7594 /* Failed to read STREAMINFO block. Aww, so close... */
7595 return DRFLAC_FALSE
;
7599 return DRFLAC_FALSE
;
7602 /* Not a FLAC header. Skip it. */
7603 if (!onSeek(pUserData
, bytesRemainingInPage
, drflac_seek_origin_current
)) {
7604 return DRFLAC_FALSE
;
7608 /* Not a FLAC header. Seek past the entire page and move on to the next. */
7609 if (!onSeek(pUserData
, bytesRemainingInPage
, drflac_seek_origin_current
)) {
7610 return DRFLAC_FALSE
;
7614 if (!onSeek(pUserData
, pageBodySize
, drflac_seek_origin_current
)) {
7615 return DRFLAC_FALSE
;
7619 pInit
->runningFilePos
+= pageBodySize
;
7622 /* Read the header of the next page. */
7623 if (drflac_ogg__read_page_header(onRead
, pUserData
, &header
, &bytesRead
, &crc32
) != DRFLAC_SUCCESS
) {
7624 return DRFLAC_FALSE
;
7626 pInit
->runningFilePos
+= bytesRead
;
7630 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
7631 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
7632 Ogg bistream object.
7634 pInit
->hasMetadataBlocks
= DRFLAC_TRUE
; /* <-- Always have at least VORBIS_COMMENT metadata block. */
7639 static 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
)
7641 drflac_bool32 relaxed
;
7644 if (pInit
== NULL
|| onRead
== NULL
|| onSeek
== NULL
) {
7645 return DRFLAC_FALSE
;
7648 DRFLAC_ZERO_MEMORY(pInit
, sizeof(*pInit
));
7649 pInit
->onRead
= onRead
;
7650 pInit
->onSeek
= onSeek
;
7651 pInit
->onMeta
= onMeta
;
7652 pInit
->container
= container
;
7653 pInit
->pUserData
= pUserData
;
7654 pInit
->pUserDataMD
= pUserDataMD
;
7656 pInit
->bs
.onRead
= onRead
;
7657 pInit
->bs
.onSeek
= onSeek
;
7658 pInit
->bs
.pUserData
= pUserData
;
7659 drflac__reset_cache(&pInit
->bs
);
7662 /* If the container is explicitly defined then we can try opening in relaxed mode. */
7663 relaxed
= container
!= drflac_container_unknown
;
7665 /* Skip over any ID3 tags. */
7667 if (onRead(pUserData
, id
, 4) != 4) {
7668 return DRFLAC_FALSE
; /* Ran out of data. */
7670 pInit
->runningFilePos
+= 4;
7672 if (id
[0] == 'I' && id
[1] == 'D' && id
[2] == '3') {
7673 drflac_uint8 header
[6];
7675 drflac_uint32 headerSize
;
7677 if (onRead(pUserData
, header
, 6) != 6) {
7678 return DRFLAC_FALSE
; /* Ran out of data. */
7680 pInit
->runningFilePos
+= 6;
7684 DRFLAC_COPY_MEMORY(&headerSize
, header
+2, 4);
7685 headerSize
= drflac__unsynchsafe_32(drflac__be2host_32(headerSize
));
7690 if (!onSeek(pUserData
, headerSize
, drflac_seek_origin_current
)) {
7691 return DRFLAC_FALSE
; /* Failed to seek past the tag. */
7693 pInit
->runningFilePos
+= headerSize
;
7699 if (id
[0] == 'f' && id
[1] == 'L' && id
[2] == 'a' && id
[3] == 'C') {
7700 return drflac__init_private__native(pInit
, onRead
, onSeek
, onMeta
, pUserData
, pUserDataMD
, relaxed
);
7702 #ifndef DR_FLAC_NO_OGG
7703 if (id
[0] == 'O' && id
[1] == 'g' && id
[2] == 'g' && id
[3] == 'S') {
7704 return drflac__init_private__ogg(pInit
, onRead
, onSeek
, onMeta
, pUserData
, pUserDataMD
, relaxed
);
7708 /* If we get here it means we likely don't have a header. Try opening in relaxed mode, if applicable. */
7710 if (container
== drflac_container_native
) {
7711 return drflac__init_private__native(pInit
, onRead
, onSeek
, onMeta
, pUserData
, pUserDataMD
, relaxed
);
7713 #ifndef DR_FLAC_NO_OGG
7714 if (container
== drflac_container_ogg
) {
7715 return drflac__init_private__ogg(pInit
, onRead
, onSeek
, onMeta
, pUserData
, pUserDataMD
, relaxed
);
7720 /* Unsupported container. */
7721 return DRFLAC_FALSE
;
7724 static void drflac__init_from_info(drflac
* pFlac
, const drflac_init_info
* pInit
)
7726 DRFLAC_ASSERT(pFlac
!= NULL
);
7727 DRFLAC_ASSERT(pInit
!= NULL
);
7729 DRFLAC_ZERO_MEMORY(pFlac
, sizeof(*pFlac
));
7730 pFlac
->bs
= pInit
->bs
;
7731 pFlac
->onMeta
= pInit
->onMeta
;
7732 pFlac
->pUserDataMD
= pInit
->pUserDataMD
;
7733 pFlac
->maxBlockSizeInPCMFrames
= pInit
->maxBlockSizeInPCMFrames
;
7734 pFlac
->sampleRate
= pInit
->sampleRate
;
7735 pFlac
->channels
= (drflac_uint8
)pInit
->channels
;
7736 pFlac
->bitsPerSample
= (drflac_uint8
)pInit
->bitsPerSample
;
7737 pFlac
->totalPCMFrameCount
= pInit
->totalPCMFrameCount
;
7738 pFlac
->container
= pInit
->container
;
7742 static 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
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
7744 drflac_init_info init
;
7745 drflac_uint32 allocationSize
;
7746 drflac_uint32 wholeSIMDVectorCountPerChannel
;
7747 drflac_uint32 decodedSamplesAllocationSize
;
7748 #ifndef DR_FLAC_NO_OGG
7751 drflac_uint64 firstFramePos
;
7752 drflac_uint64 seektablePos
;
7753 drflac_uint32 seektableSize
;
7754 drflac_allocation_callbacks allocationCallbacks
;
7757 /* CPU support first. */
7758 drflac__init_cpu_caps();
7760 if (!drflac__init_private(&init
, onRead
, onSeek
, onMeta
, container
, pUserData
, pUserDataMD
)) {
7764 if (pAllocationCallbacks
!= NULL
) {
7765 allocationCallbacks
= *pAllocationCallbacks
;
7766 if (allocationCallbacks
.onFree
== NULL
|| (allocationCallbacks
.onMalloc
== NULL
&& allocationCallbacks
.onRealloc
== NULL
)) {
7767 return NULL
; /* Invalid allocation callbacks. */
7770 allocationCallbacks
.pUserData
= NULL
;
7771 allocationCallbacks
.onMalloc
= drflac__malloc_default
;
7772 allocationCallbacks
.onRealloc
= drflac__realloc_default
;
7773 allocationCallbacks
.onFree
= drflac__free_default
;
7778 The size of the allocation for the drflac object needs to be large enough to fit the following:
7779 1) The main members of the drflac structure
7780 2) A block of memory large enough to store the decoded samples of the largest frame in the stream
7781 3) If the container is Ogg, a drflac_oggbs object
7783 The complicated part of the allocation is making sure there's enough room the decoded samples, taking into consideration
7784 the different SIMD instruction sets.
7786 allocationSize
= sizeof(drflac
);
7789 The allocation size for decoded frames depends on the number of 32-bit integers that fit inside the largest SIMD vector
7792 if ((init
.maxBlockSizeInPCMFrames
% (DRFLAC_MAX_SIMD_VECTOR_SIZE
/ sizeof(drflac_int32
))) == 0) {
7793 wholeSIMDVectorCountPerChannel
= (init
.maxBlockSizeInPCMFrames
/ (DRFLAC_MAX_SIMD_VECTOR_SIZE
/ sizeof(drflac_int32
)));
7795 wholeSIMDVectorCountPerChannel
= (init
.maxBlockSizeInPCMFrames
/ (DRFLAC_MAX_SIMD_VECTOR_SIZE
/ sizeof(drflac_int32
))) + 1;
7798 decodedSamplesAllocationSize
= wholeSIMDVectorCountPerChannel
* DRFLAC_MAX_SIMD_VECTOR_SIZE
* init
.channels
;
7800 allocationSize
+= decodedSamplesAllocationSize
;
7801 allocationSize
+= DRFLAC_MAX_SIMD_VECTOR_SIZE
; /* Allocate extra bytes to ensure we have enough for alignment. */
7803 #ifndef DR_FLAC_NO_OGG
7804 /* There's additional data required for Ogg streams. */
7805 if (init
.container
== drflac_container_ogg
) {
7806 allocationSize
+= sizeof(drflac_oggbs
);
7809 DRFLAC_ZERO_MEMORY(&oggbs
, sizeof(oggbs
));
7810 if (init
.container
== drflac_container_ogg
) {
7811 oggbs
.onRead
= onRead
;
7812 oggbs
.onSeek
= onSeek
;
7813 oggbs
.pUserData
= pUserData
;
7814 oggbs
.currentBytePos
= init
.oggFirstBytePos
;
7815 oggbs
.firstBytePos
= init
.oggFirstBytePos
;
7816 oggbs
.serialNumber
= init
.oggSerial
;
7817 oggbs
.bosPageHeader
= init
.oggBosHeader
;
7818 oggbs
.bytesRemainingInPage
= 0;
7823 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
7824 consist of only a single heap allocation. To this, the size of the seek table needs to be known, which we determine when reading
7825 and decoding the metadata.
7827 firstFramePos
= 42; /* <-- We know we are at byte 42 at this point. */
7830 if (init
.hasMetadataBlocks
) {
7831 drflac_read_proc onReadOverride
= onRead
;
7832 drflac_seek_proc onSeekOverride
= onSeek
;
7833 void* pUserDataOverride
= pUserData
;
7835 #ifndef DR_FLAC_NO_OGG
7836 if (init
.container
== drflac_container_ogg
) {
7837 onReadOverride
= drflac__on_read_ogg
;
7838 onSeekOverride
= drflac__on_seek_ogg
;
7839 pUserDataOverride
= (void*)&oggbs
;
7843 if (!drflac__read_and_decode_metadata(onReadOverride
, onSeekOverride
, onMeta
, pUserDataOverride
, pUserDataMD
, &firstFramePos
, &seektablePos
, &seektableSize
, &allocationCallbacks
)) {
7847 allocationSize
+= seektableSize
;
7851 pFlac
= (drflac
*)drflac__malloc_from_callbacks(allocationSize
, &allocationCallbacks
);
7852 if (pFlac
== NULL
) {
7856 drflac__init_from_info(pFlac
, &init
);
7857 pFlac
->allocationCallbacks
= allocationCallbacks
;
7858 pFlac
->pDecodedSamples
= (drflac_int32
*)drflac_align((size_t)pFlac
->pExtraData
, DRFLAC_MAX_SIMD_VECTOR_SIZE
);
7860 #ifndef DR_FLAC_NO_OGG
7861 if (init
.container
== drflac_container_ogg
) {
7862 drflac_oggbs
* pInternalOggbs
= (drflac_oggbs
*)((drflac_uint8
*)pFlac
->pDecodedSamples
+ decodedSamplesAllocationSize
+ seektableSize
);
7863 *pInternalOggbs
= oggbs
;
7865 /* The Ogg bistream needs to be layered on top of the original bitstream. */
7866 pFlac
->bs
.onRead
= drflac__on_read_ogg
;
7867 pFlac
->bs
.onSeek
= drflac__on_seek_ogg
;
7868 pFlac
->bs
.pUserData
= (void*)pInternalOggbs
;
7869 pFlac
->_oggbs
= (void*)pInternalOggbs
;
7873 pFlac
->firstFLACFramePosInBytes
= firstFramePos
;
7875 /* 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. */
7876 #ifndef DR_FLAC_NO_OGG
7877 if (init
.container
== drflac_container_ogg
)
7879 pFlac
->pSeekpoints
= NULL
;
7880 pFlac
->seekpointCount
= 0;
7885 /* If we have a seektable we need to load it now, making sure we move back to where we were previously. */
7886 if (seektablePos
!= 0) {
7887 pFlac
->seekpointCount
= seektableSize
/ sizeof(*pFlac
->pSeekpoints
);
7888 pFlac
->pSeekpoints
= (drflac_seekpoint
*)((drflac_uint8
*)pFlac
->pDecodedSamples
+ decodedSamplesAllocationSize
);
7890 DRFLAC_ASSERT(pFlac
->bs
.onSeek
!= NULL
);
7891 DRFLAC_ASSERT(pFlac
->bs
.onRead
!= NULL
);
7893 /* Seek to the seektable, then just read directly into our seektable buffer. */
7894 if (pFlac
->bs
.onSeek(pFlac
->bs
.pUserData
, (int)seektablePos
, drflac_seek_origin_start
)) {
7895 if (pFlac
->bs
.onRead(pFlac
->bs
.pUserData
, pFlac
->pSeekpoints
, seektableSize
) == seektableSize
) {
7897 drflac_uint32 iSeekpoint
;
7898 for (iSeekpoint
= 0; iSeekpoint
< pFlac
->seekpointCount
; ++iSeekpoint
) {
7899 pFlac
->pSeekpoints
[iSeekpoint
].firstPCMFrame
= drflac__be2host_64(pFlac
->pSeekpoints
[iSeekpoint
].firstPCMFrame
);
7900 pFlac
->pSeekpoints
[iSeekpoint
].flacFrameOffset
= drflac__be2host_64(pFlac
->pSeekpoints
[iSeekpoint
].flacFrameOffset
);
7901 pFlac
->pSeekpoints
[iSeekpoint
].pcmFrameCount
= drflac__be2host_16(pFlac
->pSeekpoints
[iSeekpoint
].pcmFrameCount
);
7904 /* Failed to read the seektable. Pretend we don't have one. */
7905 pFlac
->pSeekpoints
= NULL
;
7906 pFlac
->seekpointCount
= 0;
7909 /* We need to seek back to where we were. If this fails it's a critical error. */
7910 if (!pFlac
->bs
.onSeek(pFlac
->bs
.pUserData
, (int)pFlac
->firstFLACFramePosInBytes
, drflac_seek_origin_start
)) {
7911 drflac__free_from_callbacks(pFlac
, &allocationCallbacks
);
7915 /* Failed to seek to the seektable. Ominous sign, but for now we can just pretend we don't have one. */
7916 pFlac
->pSeekpoints
= NULL
;
7917 pFlac
->seekpointCount
= 0;
7924 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
7927 if (!init
.hasStreamInfoBlock
) {
7928 pFlac
->currentFLACFrame
.header
= init
.firstFrameHeader
;
7930 drflac_result result
= drflac__decode_flac_frame(pFlac
);
7931 if (result
== DRFLAC_SUCCESS
) {
7934 if (result
== DRFLAC_CRC_MISMATCH
) {
7935 if (!drflac__read_next_flac_frame_header(&pFlac
->bs
, pFlac
->bitsPerSample
, &pFlac
->currentFLACFrame
.header
)) {
7936 drflac__free_from_callbacks(pFlac
, &allocationCallbacks
);
7941 drflac__free_from_callbacks(pFlac
, &allocationCallbacks
);
7953 #ifndef DR_FLAC_NO_STDIO
7955 #include <wchar.h> /* For wcslen(), wcsrtombs() */
7957 /* drflac_result_from_errno() is only used for fopen() and wfopen() so putting it inside DR_WAV_NO_STDIO for now. If something else needs this later we can move it out. */
7959 static drflac_result
drflac_result_from_errno(int e
)
7963 case 0: return DRFLAC_SUCCESS
;
7965 case EPERM
: return DRFLAC_INVALID_OPERATION
;
7968 case ENOENT
: return DRFLAC_DOES_NOT_EXIST
;
7971 case ESRCH
: return DRFLAC_DOES_NOT_EXIST
;
7974 case EINTR
: return DRFLAC_INTERRUPT
;
7977 case EIO
: return DRFLAC_IO_ERROR
;
7980 case ENXIO
: return DRFLAC_DOES_NOT_EXIST
;
7983 case E2BIG
: return DRFLAC_INVALID_ARGS
;
7986 case ENOEXEC
: return DRFLAC_INVALID_FILE
;
7989 case EBADF
: return DRFLAC_INVALID_FILE
;
7992 case ECHILD
: return DRFLAC_ERROR
;
7995 case EAGAIN
: return DRFLAC_UNAVAILABLE
;
7998 case ENOMEM
: return DRFLAC_OUT_OF_MEMORY
;
8001 case EACCES
: return DRFLAC_ACCESS_DENIED
;
8004 case EFAULT
: return DRFLAC_BAD_ADDRESS
;
8007 case ENOTBLK
: return DRFLAC_ERROR
;
8010 case EBUSY
: return DRFLAC_BUSY
;
8013 case EEXIST
: return DRFLAC_ALREADY_EXISTS
;
8016 case EXDEV
: return DRFLAC_ERROR
;
8019 case ENODEV
: return DRFLAC_DOES_NOT_EXIST
;
8022 case ENOTDIR
: return DRFLAC_NOT_DIRECTORY
;
8025 case EISDIR
: return DRFLAC_IS_DIRECTORY
;
8028 case EINVAL
: return DRFLAC_INVALID_ARGS
;
8031 case ENFILE
: return DRFLAC_TOO_MANY_OPEN_FILES
;
8034 case EMFILE
: return DRFLAC_TOO_MANY_OPEN_FILES
;
8037 case ENOTTY
: return DRFLAC_INVALID_OPERATION
;
8040 case ETXTBSY
: return DRFLAC_BUSY
;
8043 case EFBIG
: return DRFLAC_TOO_BIG
;
8046 case ENOSPC
: return DRFLAC_NO_SPACE
;
8049 case ESPIPE
: return DRFLAC_BAD_SEEK
;
8052 case EROFS
: return DRFLAC_ACCESS_DENIED
;
8055 case EMLINK
: return DRFLAC_TOO_MANY_LINKS
;
8058 case EPIPE
: return DRFLAC_BAD_PIPE
;
8061 case EDOM
: return DRFLAC_OUT_OF_RANGE
;
8064 case ERANGE
: return DRFLAC_OUT_OF_RANGE
;
8067 case EDEADLK
: return DRFLAC_DEADLOCK
;
8070 case ENAMETOOLONG
: return DRFLAC_PATH_TOO_LONG
;
8073 case ENOLCK
: return DRFLAC_ERROR
;
8076 case ENOSYS
: return DRFLAC_NOT_IMPLEMENTED
;
8079 case ENOTEMPTY
: return DRFLAC_DIRECTORY_NOT_EMPTY
;
8082 case ELOOP
: return DRFLAC_TOO_MANY_LINKS
;
8085 case ENOMSG
: return DRFLAC_NO_MESSAGE
;
8088 case EIDRM
: return DRFLAC_ERROR
;
8091 case ECHRNG
: return DRFLAC_ERROR
;
8094 case EL2NSYNC
: return DRFLAC_ERROR
;
8097 case EL3HLT
: return DRFLAC_ERROR
;
8100 case EL3RST
: return DRFLAC_ERROR
;
8103 case ELNRNG
: return DRFLAC_OUT_OF_RANGE
;
8106 case EUNATCH
: return DRFLAC_ERROR
;
8109 case ENOCSI
: return DRFLAC_ERROR
;
8112 case EL2HLT
: return DRFLAC_ERROR
;
8115 case EBADE
: return DRFLAC_ERROR
;
8118 case EBADR
: return DRFLAC_ERROR
;
8121 case EXFULL
: return DRFLAC_ERROR
;
8124 case ENOANO
: return DRFLAC_ERROR
;
8127 case EBADRQC
: return DRFLAC_ERROR
;
8130 case EBADSLT
: return DRFLAC_ERROR
;
8133 case EBFONT
: return DRFLAC_INVALID_FILE
;
8136 case ENOSTR
: return DRFLAC_ERROR
;
8139 case ENODATA
: return DRFLAC_NO_DATA_AVAILABLE
;
8142 case ETIME
: return DRFLAC_TIMEOUT
;
8145 case ENOSR
: return DRFLAC_NO_DATA_AVAILABLE
;
8148 case ENONET
: return DRFLAC_NO_NETWORK
;
8151 case ENOPKG
: return DRFLAC_ERROR
;
8154 case EREMOTE
: return DRFLAC_ERROR
;
8157 case ENOLINK
: return DRFLAC_ERROR
;
8160 case EADV
: return DRFLAC_ERROR
;
8163 case ESRMNT
: return DRFLAC_ERROR
;
8166 case ECOMM
: return DRFLAC_ERROR
;
8169 case EPROTO
: return DRFLAC_ERROR
;
8172 case EMULTIHOP
: return DRFLAC_ERROR
;
8175 case EDOTDOT
: return DRFLAC_ERROR
;
8178 case EBADMSG
: return DRFLAC_BAD_MESSAGE
;
8181 case EOVERFLOW
: return DRFLAC_TOO_BIG
;
8184 case ENOTUNIQ
: return DRFLAC_NOT_UNIQUE
;
8187 case EBADFD
: return DRFLAC_ERROR
;
8190 case EREMCHG
: return DRFLAC_ERROR
;
8193 case ELIBACC
: return DRFLAC_ACCESS_DENIED
;
8196 case ELIBBAD
: return DRFLAC_INVALID_FILE
;
8199 case ELIBSCN
: return DRFLAC_INVALID_FILE
;
8202 case ELIBMAX
: return DRFLAC_ERROR
;
8205 case ELIBEXEC
: return DRFLAC_ERROR
;
8208 case EILSEQ
: return DRFLAC_INVALID_DATA
;
8211 case ERESTART
: return DRFLAC_ERROR
;
8214 case ESTRPIPE
: return DRFLAC_ERROR
;
8217 case EUSERS
: return DRFLAC_ERROR
;
8220 case ENOTSOCK
: return DRFLAC_NOT_SOCKET
;
8223 case EDESTADDRREQ
: return DRFLAC_NO_ADDRESS
;
8226 case EMSGSIZE
: return DRFLAC_TOO_BIG
;
8229 case EPROTOTYPE
: return DRFLAC_BAD_PROTOCOL
;
8232 case ENOPROTOOPT
: return DRFLAC_PROTOCOL_UNAVAILABLE
;
8234 #ifdef EPROTONOSUPPORT
8235 case EPROTONOSUPPORT
: return DRFLAC_PROTOCOL_NOT_SUPPORTED
;
8237 #ifdef ESOCKTNOSUPPORT
8238 case ESOCKTNOSUPPORT
: return DRFLAC_SOCKET_NOT_SUPPORTED
;
8241 case EOPNOTSUPP
: return DRFLAC_INVALID_OPERATION
;
8244 case EPFNOSUPPORT
: return DRFLAC_PROTOCOL_FAMILY_NOT_SUPPORTED
;
8247 case EAFNOSUPPORT
: return DRFLAC_ADDRESS_FAMILY_NOT_SUPPORTED
;
8250 case EADDRINUSE
: return DRFLAC_ALREADY_IN_USE
;
8252 #ifdef EADDRNOTAVAIL
8253 case EADDRNOTAVAIL
: return DRFLAC_ERROR
;
8256 case ENETDOWN
: return DRFLAC_NO_NETWORK
;
8259 case ENETUNREACH
: return DRFLAC_NO_NETWORK
;
8262 case ENETRESET
: return DRFLAC_NO_NETWORK
;
8265 case ECONNABORTED
: return DRFLAC_NO_NETWORK
;
8268 case ECONNRESET
: return DRFLAC_CONNECTION_RESET
;
8271 case ENOBUFS
: return DRFLAC_NO_SPACE
;
8274 case EISCONN
: return DRFLAC_ALREADY_CONNECTED
;
8277 case ENOTCONN
: return DRFLAC_NOT_CONNECTED
;
8280 case ESHUTDOWN
: return DRFLAC_ERROR
;
8283 case ETOOMANYREFS
: return DRFLAC_ERROR
;
8286 case ETIMEDOUT
: return DRFLAC_TIMEOUT
;
8289 case ECONNREFUSED
: return DRFLAC_CONNECTION_REFUSED
;
8292 case EHOSTDOWN
: return DRFLAC_NO_HOST
;
8295 case EHOSTUNREACH
: return DRFLAC_NO_HOST
;
8298 case EALREADY
: return DRFLAC_IN_PROGRESS
;
8301 case EINPROGRESS
: return DRFLAC_IN_PROGRESS
;
8304 case ESTALE
: return DRFLAC_INVALID_FILE
;
8307 case EUCLEAN
: return DRFLAC_ERROR
;
8310 case ENOTNAM
: return DRFLAC_ERROR
;
8313 case ENAVAIL
: return DRFLAC_ERROR
;
8316 case EISNAM
: return DRFLAC_ERROR
;
8319 case EREMOTEIO
: return DRFLAC_IO_ERROR
;
8322 case EDQUOT
: return DRFLAC_NO_SPACE
;
8325 case ENOMEDIUM
: return DRFLAC_DOES_NOT_EXIST
;
8328 case EMEDIUMTYPE
: return DRFLAC_ERROR
;
8331 case ECANCELED
: return DRFLAC_CANCELLED
;
8334 case ENOKEY
: return DRFLAC_ERROR
;
8337 case EKEYEXPIRED
: return DRFLAC_ERROR
;
8340 case EKEYREVOKED
: return DRFLAC_ERROR
;
8343 case EKEYREJECTED
: return DRFLAC_ERROR
;
8346 case EOWNERDEAD
: return DRFLAC_ERROR
;
8348 #ifdef ENOTRECOVERABLE
8349 case ENOTRECOVERABLE
: return DRFLAC_ERROR
;
8352 case ERFKILL
: return DRFLAC_ERROR
;
8355 case EHWPOISON
: return DRFLAC_ERROR
;
8357 default: return DRFLAC_ERROR
;
8361 static drflac_result
drflac_fopen(FILE** ppFile
, const char* pFilePath
, const char* pOpenMode
)
8363 #if defined(_MSC_VER) && _MSC_VER >= 1400
8367 if (ppFile
!= NULL
) {
8368 *ppFile
= NULL
; /* Safety. */
8371 if (pFilePath
== NULL
|| pOpenMode
== NULL
|| ppFile
== NULL
) {
8372 return DRFLAC_INVALID_ARGS
;
8375 #if defined(_MSC_VER) && _MSC_VER >= 1400
8376 err
= fopen_s(ppFile
, pFilePath
, pOpenMode
);
8378 return drflac_result_from_errno(err
);
8381 #if defined(_WIN32) || defined(__APPLE__)
8382 *ppFile
= fopen(pFilePath
, pOpenMode
);
8384 #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 && defined(_LARGEFILE64_SOURCE)
8385 *ppFile
= fopen64(pFilePath
, pOpenMode
);
8387 *ppFile
= fopen(pFilePath
, pOpenMode
);
8390 if (*ppFile
== NULL
) {
8391 drflac_result result
= drflac_result_from_errno(errno
);
8392 if (result
== DRFLAC_SUCCESS
) {
8393 result
= DRFLAC_ERROR
; /* Just a safety check to make sure we never ever return success when pFile == NULL. */
8400 return DRFLAC_SUCCESS
;
8404 _wfopen() isn't always available in all compilation environments.
8407 * MSVC seems to support it universally as far back as VC6 from what I can tell (haven't checked further back).
8408 * MinGW-64 (both 32- and 64-bit) seems to support it.
8409 * MinGW wraps it in !defined(__STRICT_ANSI__).
8410 * OpenWatcom wraps it in !defined(_NO_EXT_KEYS).
8412 This can be reviewed as compatibility issues arise. The preference is to use _wfopen_s() and _wfopen() as opposed to the wcsrtombs()
8413 fallback, so if you notice your compiler not detecting this properly I'm happy to look at adding support.
8416 #if defined(_MSC_VER) || defined(__MINGW64__) || (!defined(__STRICT_ANSI__) && !defined(_NO_EXT_KEYS))
8417 #define DRFLAC_HAS_WFOPEN
8421 static drflac_result
drflac_wfopen(FILE** ppFile
, const wchar_t* pFilePath
, const wchar_t* pOpenMode
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
8423 if (ppFile
!= NULL
) {
8424 *ppFile
= NULL
; /* Safety. */
8427 if (pFilePath
== NULL
|| pOpenMode
== NULL
|| ppFile
== NULL
) {
8428 return DRFLAC_INVALID_ARGS
;
8431 #if defined(DRFLAC_HAS_WFOPEN)
8433 /* Use _wfopen() on Windows. */
8434 #if defined(_MSC_VER) && _MSC_VER >= 1400
8435 errno_t err
= _wfopen_s(ppFile
, pFilePath
, pOpenMode
);
8437 return drflac_result_from_errno(err
);
8440 *ppFile
= _wfopen(pFilePath
, pOpenMode
);
8441 if (*ppFile
== NULL
) {
8442 return drflac_result_from_errno(errno
);
8445 (void)pAllocationCallbacks
;
8449 Use fopen() on anything other than Windows. Requires a conversion. This is annoying because fopen() is locale specific. The only real way I can
8450 think of to do this is with wcsrtombs(). Note that wcstombs() is apparently not thread-safe because it uses a static global mbstate_t object for
8451 maintaining state. I've checked this with -std=c89 and it works, but if somebody get's a compiler error I'll look into improving compatibility.
8456 const wchar_t* pFilePathTemp
= pFilePath
;
8457 char* pFilePathMB
= NULL
;
8458 char pOpenModeMB
[32] = {0};
8460 /* Get the length first. */
8461 DRFLAC_ZERO_OBJECT(&mbs
);
8462 lenMB
= wcsrtombs(NULL
, &pFilePathTemp
, 0, &mbs
);
8463 if (lenMB
== (size_t)-1) {
8464 return drflac_result_from_errno(errno
);
8467 pFilePathMB
= (char*)drflac__malloc_from_callbacks(lenMB
+ 1, pAllocationCallbacks
);
8468 if (pFilePathMB
== NULL
) {
8469 return DRFLAC_OUT_OF_MEMORY
;
8472 pFilePathTemp
= pFilePath
;
8473 DRFLAC_ZERO_OBJECT(&mbs
);
8474 wcsrtombs(pFilePathMB
, &pFilePathTemp
, lenMB
+ 1, &mbs
);
8476 /* The open mode should always consist of ASCII characters so we should be able to do a trivial conversion. */
8480 if (pOpenMode
[i
] == 0) {
8481 pOpenModeMB
[i
] = '\0';
8485 pOpenModeMB
[i
] = (char)pOpenMode
[i
];
8490 *ppFile
= fopen(pFilePathMB
, pOpenModeMB
);
8492 drflac__free_from_callbacks(pFilePathMB
, pAllocationCallbacks
);
8495 if (*ppFile
== NULL
) {
8496 return DRFLAC_ERROR
;
8500 return DRFLAC_SUCCESS
;
8503 static size_t drflac__on_read_stdio(void* pUserData
, void* bufferOut
, size_t bytesToRead
)
8505 return fread(bufferOut
, 1, bytesToRead
, (FILE*)pUserData
);
8508 static drflac_bool32
drflac__on_seek_stdio(void* pUserData
, int offset
, drflac_seek_origin origin
)
8510 DRFLAC_ASSERT(offset
>= 0); /* <-- Never seek backwards. */
8512 return fseek((FILE*)pUserData
, offset
, (origin
== drflac_seek_origin_current
) ? SEEK_CUR
: SEEK_SET
) == 0;
8516 DRFLAC_API drflac
* drflac_open_file(const char* pFileName
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
8521 if (drflac_fopen(&pFile
, pFileName
, "rb") != DRFLAC_SUCCESS
) {
8525 pFlac
= drflac_open(drflac__on_read_stdio
, drflac__on_seek_stdio
, (void*)pFile
, pAllocationCallbacks
);
8526 if (pFlac
== NULL
) {
8534 DRFLAC_API drflac
* drflac_open_file_w(const wchar_t* pFileName
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
8539 if (drflac_wfopen(&pFile
, pFileName
, L
"rb", pAllocationCallbacks
) != DRFLAC_SUCCESS
) {
8543 pFlac
= drflac_open(drflac__on_read_stdio
, drflac__on_seek_stdio
, (void*)pFile
, pAllocationCallbacks
);
8544 if (pFlac
== NULL
) {
8552 DRFLAC_API drflac
* drflac_open_file_with_metadata(const char* pFileName
, drflac_meta_proc onMeta
, void* pUserData
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
8557 if (drflac_fopen(&pFile
, pFileName
, "rb") != DRFLAC_SUCCESS
) {
8561 pFlac
= drflac_open_with_metadata_private(drflac__on_read_stdio
, drflac__on_seek_stdio
, onMeta
, drflac_container_unknown
, (void*)pFile
, pUserData
, pAllocationCallbacks
);
8562 if (pFlac
== NULL
) {
8570 DRFLAC_API drflac
* drflac_open_file_with_metadata_w(const wchar_t* pFileName
, drflac_meta_proc onMeta
, void* pUserData
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
8575 if (drflac_wfopen(&pFile
, pFileName
, L
"rb", pAllocationCallbacks
) != DRFLAC_SUCCESS
) {
8579 pFlac
= drflac_open_with_metadata_private(drflac__on_read_stdio
, drflac__on_seek_stdio
, onMeta
, drflac_container_unknown
, (void*)pFile
, pUserData
, pAllocationCallbacks
);
8580 if (pFlac
== NULL
) {
8587 #endif /* DR_FLAC_NO_STDIO */
8589 static size_t drflac__on_read_memory(void* pUserData
, void* bufferOut
, size_t bytesToRead
)
8591 drflac__memory_stream
* memoryStream
= (drflac__memory_stream
*)pUserData
;
8592 size_t bytesRemaining
;
8594 DRFLAC_ASSERT(memoryStream
!= NULL
);
8595 DRFLAC_ASSERT(memoryStream
->dataSize
>= memoryStream
->currentReadPos
);
8597 bytesRemaining
= memoryStream
->dataSize
- memoryStream
->currentReadPos
;
8598 if (bytesToRead
> bytesRemaining
) {
8599 bytesToRead
= bytesRemaining
;
8602 if (bytesToRead
> 0) {
8603 DRFLAC_COPY_MEMORY(bufferOut
, memoryStream
->data
+ memoryStream
->currentReadPos
, bytesToRead
);
8604 memoryStream
->currentReadPos
+= bytesToRead
;
8610 static drflac_bool32
drflac__on_seek_memory(void* pUserData
, int offset
, drflac_seek_origin origin
)
8612 drflac__memory_stream
* memoryStream
= (drflac__memory_stream
*)pUserData
;
8614 DRFLAC_ASSERT(memoryStream
!= NULL
);
8615 DRFLAC_ASSERT(offset
>= 0); /* <-- Never seek backwards. */
8617 if (offset
> (drflac_int64
)memoryStream
->dataSize
) {
8618 return DRFLAC_FALSE
;
8621 if (origin
== drflac_seek_origin_current
) {
8622 if (memoryStream
->currentReadPos
+ offset
<= memoryStream
->dataSize
) {
8623 memoryStream
->currentReadPos
+= offset
;
8625 return DRFLAC_FALSE
; /* Trying to seek too far forward. */
8628 if ((drflac_uint32
)offset
<= memoryStream
->dataSize
) {
8629 memoryStream
->currentReadPos
= offset
;
8631 return DRFLAC_FALSE
; /* Trying to seek too far forward. */
8638 DRFLAC_API drflac
* drflac_open_memory(const void* pData
, size_t dataSize
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
8640 drflac__memory_stream memoryStream
;
8643 memoryStream
.data
= (const drflac_uint8
*)pData
;
8644 memoryStream
.dataSize
= dataSize
;
8645 memoryStream
.currentReadPos
= 0;
8646 pFlac
= drflac_open(drflac__on_read_memory
, drflac__on_seek_memory
, &memoryStream
, pAllocationCallbacks
);
8647 if (pFlac
== NULL
) {
8651 pFlac
->memoryStream
= memoryStream
;
8653 /* This is an awful hack... */
8654 #ifndef DR_FLAC_NO_OGG
8655 if (pFlac
->container
== drflac_container_ogg
)
8657 drflac_oggbs
* oggbs
= (drflac_oggbs
*)pFlac
->_oggbs
;
8658 oggbs
->pUserData
= &pFlac
->memoryStream
;
8663 pFlac
->bs
.pUserData
= &pFlac
->memoryStream
;
8669 DRFLAC_API drflac
* drflac_open_memory_with_metadata(const void* pData
, size_t dataSize
, drflac_meta_proc onMeta
, void* pUserData
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
8671 drflac__memory_stream memoryStream
;
8674 memoryStream
.data
= (const drflac_uint8
*)pData
;
8675 memoryStream
.dataSize
= dataSize
;
8676 memoryStream
.currentReadPos
= 0;
8677 pFlac
= drflac_open_with_metadata_private(drflac__on_read_memory
, drflac__on_seek_memory
, onMeta
, drflac_container_unknown
, &memoryStream
, pUserData
, pAllocationCallbacks
);
8678 if (pFlac
== NULL
) {
8682 pFlac
->memoryStream
= memoryStream
;
8684 /* This is an awful hack... */
8685 #ifndef DR_FLAC_NO_OGG
8686 if (pFlac
->container
== drflac_container_ogg
)
8688 drflac_oggbs
* oggbs
= (drflac_oggbs
*)pFlac
->_oggbs
;
8689 oggbs
->pUserData
= &pFlac
->memoryStream
;
8694 pFlac
->bs
.pUserData
= &pFlac
->memoryStream
;
8702 DRFLAC_API drflac
* drflac_open(drflac_read_proc onRead
, drflac_seek_proc onSeek
, void* pUserData
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
8704 return drflac_open_with_metadata_private(onRead
, onSeek
, NULL
, drflac_container_unknown
, pUserData
, pUserData
, pAllocationCallbacks
);
8706 DRFLAC_API drflac
* drflac_open_relaxed(drflac_read_proc onRead
, drflac_seek_proc onSeek
, drflac_container container
, void* pUserData
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
8708 return drflac_open_with_metadata_private(onRead
, onSeek
, NULL
, container
, pUserData
, pUserData
, pAllocationCallbacks
);
8711 DRFLAC_API drflac
* drflac_open_with_metadata(drflac_read_proc onRead
, drflac_seek_proc onSeek
, drflac_meta_proc onMeta
, void* pUserData
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
8713 return drflac_open_with_metadata_private(onRead
, onSeek
, onMeta
, drflac_container_unknown
, pUserData
, pUserData
, pAllocationCallbacks
);
8715 DRFLAC_API drflac
* drflac_open_with_metadata_relaxed(drflac_read_proc onRead
, drflac_seek_proc onSeek
, drflac_meta_proc onMeta
, drflac_container container
, void* pUserData
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
8717 return drflac_open_with_metadata_private(onRead
, onSeek
, onMeta
, container
, pUserData
, pUserData
, pAllocationCallbacks
);
8720 DRFLAC_API
void drflac_close(drflac
* pFlac
)
8722 if (pFlac
== NULL
) {
8726 #ifndef DR_FLAC_NO_STDIO
8728 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()
8729 was used by looking at the callbacks.
8731 if (pFlac
->bs
.onRead
== drflac__on_read_stdio
) {
8732 fclose((FILE*)pFlac
->bs
.pUserData
);
8735 #ifndef DR_FLAC_NO_OGG
8736 /* Need to clean up Ogg streams a bit differently due to the way the bit streaming is chained. */
8737 if (pFlac
->container
== drflac_container_ogg
) {
8738 drflac_oggbs
* oggbs
= (drflac_oggbs
*)pFlac
->_oggbs
;
8739 DRFLAC_ASSERT(pFlac
->bs
.onRead
== drflac__on_read_ogg
);
8741 if (oggbs
->onRead
== drflac__on_read_stdio
) {
8742 fclose((FILE*)oggbs
->pUserData
);
8748 drflac__free_from_callbacks(pFlac
, &pFlac
->allocationCallbacks
);
8753 static DRFLAC_INLINE
void drflac_read_pcm_frames_s32__decode_left_side__reference(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int32
* pOutputSamples
)
8756 for (i
= 0; i
< frameCount
; ++i
) {
8757 drflac_uint32 left
= (drflac_uint32
)pInputSamples0
[i
] << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
);
8758 drflac_uint32 side
= (drflac_uint32
)pInputSamples1
[i
] << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
);
8759 drflac_uint32 right
= left
- side
;
8761 pOutputSamples
[i
*2+0] = (drflac_int32
)left
;
8762 pOutputSamples
[i
*2+1] = (drflac_int32
)right
;
8767 static DRFLAC_INLINE
void drflac_read_pcm_frames_s32__decode_left_side__scalar(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int32
* pOutputSamples
)
8770 drflac_uint64 frameCount4
= frameCount
>> 2;
8771 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
8772 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
8773 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
8774 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
8776 for (i
= 0; i
< frameCount4
; ++i
) {
8777 drflac_uint32 left0
= pInputSamples0U32
[i
*4+0] << shift0
;
8778 drflac_uint32 left1
= pInputSamples0U32
[i
*4+1] << shift0
;
8779 drflac_uint32 left2
= pInputSamples0U32
[i
*4+2] << shift0
;
8780 drflac_uint32 left3
= pInputSamples0U32
[i
*4+3] << shift0
;
8782 drflac_uint32 side0
= pInputSamples1U32
[i
*4+0] << shift1
;
8783 drflac_uint32 side1
= pInputSamples1U32
[i
*4+1] << shift1
;
8784 drflac_uint32 side2
= pInputSamples1U32
[i
*4+2] << shift1
;
8785 drflac_uint32 side3
= pInputSamples1U32
[i
*4+3] << shift1
;
8787 drflac_uint32 right0
= left0
- side0
;
8788 drflac_uint32 right1
= left1
- side1
;
8789 drflac_uint32 right2
= left2
- side2
;
8790 drflac_uint32 right3
= left3
- side3
;
8792 pOutputSamples
[i
*8+0] = (drflac_int32
)left0
;
8793 pOutputSamples
[i
*8+1] = (drflac_int32
)right0
;
8794 pOutputSamples
[i
*8+2] = (drflac_int32
)left1
;
8795 pOutputSamples
[i
*8+3] = (drflac_int32
)right1
;
8796 pOutputSamples
[i
*8+4] = (drflac_int32
)left2
;
8797 pOutputSamples
[i
*8+5] = (drflac_int32
)right2
;
8798 pOutputSamples
[i
*8+6] = (drflac_int32
)left3
;
8799 pOutputSamples
[i
*8+7] = (drflac_int32
)right3
;
8802 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
8803 drflac_uint32 left
= pInputSamples0U32
[i
] << shift0
;
8804 drflac_uint32 side
= pInputSamples1U32
[i
] << shift1
;
8805 drflac_uint32 right
= left
- side
;
8807 pOutputSamples
[i
*2+0] = (drflac_int32
)left
;
8808 pOutputSamples
[i
*2+1] = (drflac_int32
)right
;
8812 #if defined(DRFLAC_SUPPORT_SSE2)
8813 static DRFLAC_INLINE
void drflac_read_pcm_frames_s32__decode_left_side__sse2(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int32
* pOutputSamples
)
8816 drflac_uint64 frameCount4
= frameCount
>> 2;
8817 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
8818 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
8819 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
8820 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
8822 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 24);
8824 for (i
= 0; i
< frameCount4
; ++i
) {
8825 __m128i left
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples0
+ i
), shift0
);
8826 __m128i side
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples1
+ i
), shift1
);
8827 __m128i right
= _mm_sub_epi32(left
, side
);
8829 _mm_storeu_si128((__m128i
*)(pOutputSamples
+ i
*8 + 0), _mm_unpacklo_epi32(left
, right
));
8830 _mm_storeu_si128((__m128i
*)(pOutputSamples
+ i
*8 + 4), _mm_unpackhi_epi32(left
, right
));
8833 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
8834 drflac_uint32 left
= pInputSamples0U32
[i
] << shift0
;
8835 drflac_uint32 side
= pInputSamples1U32
[i
] << shift1
;
8836 drflac_uint32 right
= left
- side
;
8838 pOutputSamples
[i
*2+0] = (drflac_int32
)left
;
8839 pOutputSamples
[i
*2+1] = (drflac_int32
)right
;
8844 #if defined(DRFLAC_SUPPORT_NEON)
8845 static DRFLAC_INLINE
void drflac_read_pcm_frames_s32__decode_left_side__neon(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int32
* pOutputSamples
)
8848 drflac_uint64 frameCount4
= frameCount
>> 2;
8849 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
8850 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
8851 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
8852 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
8856 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 24);
8858 shift0_4
= vdupq_n_s32(shift0
);
8859 shift1_4
= vdupq_n_s32(shift1
);
8861 for (i
= 0; i
< frameCount4
; ++i
) {
8866 left
= vshlq_u32(vld1q_u32(pInputSamples0U32
+ i
*4), shift0_4
);
8867 side
= vshlq_u32(vld1q_u32(pInputSamples1U32
+ i
*4), shift1_4
);
8868 right
= vsubq_u32(left
, side
);
8870 drflac__vst2q_u32((drflac_uint32
*)pOutputSamples
+ i
*8, vzipq_u32(left
, right
));
8873 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
8874 drflac_uint32 left
= pInputSamples0U32
[i
] << shift0
;
8875 drflac_uint32 side
= pInputSamples1U32
[i
] << shift1
;
8876 drflac_uint32 right
= left
- side
;
8878 pOutputSamples
[i
*2+0] = (drflac_int32
)left
;
8879 pOutputSamples
[i
*2+1] = (drflac_int32
)right
;
8884 static DRFLAC_INLINE
void drflac_read_pcm_frames_s32__decode_left_side(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int32
* pOutputSamples
)
8886 #if defined(DRFLAC_SUPPORT_SSE2)
8887 if (drflac__gIsSSE2Supported
&& pFlac
->bitsPerSample
<= 24) {
8888 drflac_read_pcm_frames_s32__decode_left_side__sse2(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
8890 #elif defined(DRFLAC_SUPPORT_NEON)
8891 if (drflac__gIsNEONSupported
&& pFlac
->bitsPerSample
<= 24) {
8892 drflac_read_pcm_frames_s32__decode_left_side__neon(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
8896 /* Scalar fallback. */
8898 drflac_read_pcm_frames_s32__decode_left_side__reference(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
8900 drflac_read_pcm_frames_s32__decode_left_side__scalar(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
8907 static DRFLAC_INLINE
void drflac_read_pcm_frames_s32__decode_right_side__reference(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int32
* pOutputSamples
)
8910 for (i
= 0; i
< frameCount
; ++i
) {
8911 drflac_uint32 side
= (drflac_uint32
)pInputSamples0
[i
] << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
);
8912 drflac_uint32 right
= (drflac_uint32
)pInputSamples1
[i
] << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
);
8913 drflac_uint32 left
= right
+ side
;
8915 pOutputSamples
[i
*2+0] = (drflac_int32
)left
;
8916 pOutputSamples
[i
*2+1] = (drflac_int32
)right
;
8921 static DRFLAC_INLINE
void drflac_read_pcm_frames_s32__decode_right_side__scalar(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int32
* pOutputSamples
)
8924 drflac_uint64 frameCount4
= frameCount
>> 2;
8925 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
8926 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
8927 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
8928 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
8930 for (i
= 0; i
< frameCount4
; ++i
) {
8931 drflac_uint32 side0
= pInputSamples0U32
[i
*4+0] << shift0
;
8932 drflac_uint32 side1
= pInputSamples0U32
[i
*4+1] << shift0
;
8933 drflac_uint32 side2
= pInputSamples0U32
[i
*4+2] << shift0
;
8934 drflac_uint32 side3
= pInputSamples0U32
[i
*4+3] << shift0
;
8936 drflac_uint32 right0
= pInputSamples1U32
[i
*4+0] << shift1
;
8937 drflac_uint32 right1
= pInputSamples1U32
[i
*4+1] << shift1
;
8938 drflac_uint32 right2
= pInputSamples1U32
[i
*4+2] << shift1
;
8939 drflac_uint32 right3
= pInputSamples1U32
[i
*4+3] << shift1
;
8941 drflac_uint32 left0
= right0
+ side0
;
8942 drflac_uint32 left1
= right1
+ side1
;
8943 drflac_uint32 left2
= right2
+ side2
;
8944 drflac_uint32 left3
= right3
+ side3
;
8946 pOutputSamples
[i
*8+0] = (drflac_int32
)left0
;
8947 pOutputSamples
[i
*8+1] = (drflac_int32
)right0
;
8948 pOutputSamples
[i
*8+2] = (drflac_int32
)left1
;
8949 pOutputSamples
[i
*8+3] = (drflac_int32
)right1
;
8950 pOutputSamples
[i
*8+4] = (drflac_int32
)left2
;
8951 pOutputSamples
[i
*8+5] = (drflac_int32
)right2
;
8952 pOutputSamples
[i
*8+6] = (drflac_int32
)left3
;
8953 pOutputSamples
[i
*8+7] = (drflac_int32
)right3
;
8956 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
8957 drflac_uint32 side
= pInputSamples0U32
[i
] << shift0
;
8958 drflac_uint32 right
= pInputSamples1U32
[i
] << shift1
;
8959 drflac_uint32 left
= right
+ side
;
8961 pOutputSamples
[i
*2+0] = (drflac_int32
)left
;
8962 pOutputSamples
[i
*2+1] = (drflac_int32
)right
;
8966 #if defined(DRFLAC_SUPPORT_SSE2)
8967 static DRFLAC_INLINE
void drflac_read_pcm_frames_s32__decode_right_side__sse2(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int32
* pOutputSamples
)
8970 drflac_uint64 frameCount4
= frameCount
>> 2;
8971 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
8972 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
8973 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
8974 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
8976 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 24);
8978 for (i
= 0; i
< frameCount4
; ++i
) {
8979 __m128i side
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples0
+ i
), shift0
);
8980 __m128i right
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples1
+ i
), shift1
);
8981 __m128i left
= _mm_add_epi32(right
, side
);
8983 _mm_storeu_si128((__m128i
*)(pOutputSamples
+ i
*8 + 0), _mm_unpacklo_epi32(left
, right
));
8984 _mm_storeu_si128((__m128i
*)(pOutputSamples
+ i
*8 + 4), _mm_unpackhi_epi32(left
, right
));
8987 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
8988 drflac_uint32 side
= pInputSamples0U32
[i
] << shift0
;
8989 drflac_uint32 right
= pInputSamples1U32
[i
] << shift1
;
8990 drflac_uint32 left
= right
+ side
;
8992 pOutputSamples
[i
*2+0] = (drflac_int32
)left
;
8993 pOutputSamples
[i
*2+1] = (drflac_int32
)right
;
8998 #if defined(DRFLAC_SUPPORT_NEON)
8999 static DRFLAC_INLINE
void drflac_read_pcm_frames_s32__decode_right_side__neon(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int32
* pOutputSamples
)
9002 drflac_uint64 frameCount4
= frameCount
>> 2;
9003 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
9004 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
9005 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9006 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9010 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 24);
9012 shift0_4
= vdupq_n_s32(shift0
);
9013 shift1_4
= vdupq_n_s32(shift1
);
9015 for (i
= 0; i
< frameCount4
; ++i
) {
9020 side
= vshlq_u32(vld1q_u32(pInputSamples0U32
+ i
*4), shift0_4
);
9021 right
= vshlq_u32(vld1q_u32(pInputSamples1U32
+ i
*4), shift1_4
);
9022 left
= vaddq_u32(right
, side
);
9024 drflac__vst2q_u32((drflac_uint32
*)pOutputSamples
+ i
*8, vzipq_u32(left
, right
));
9027 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
9028 drflac_uint32 side
= pInputSamples0U32
[i
] << shift0
;
9029 drflac_uint32 right
= pInputSamples1U32
[i
] << shift1
;
9030 drflac_uint32 left
= right
+ side
;
9032 pOutputSamples
[i
*2+0] = (drflac_int32
)left
;
9033 pOutputSamples
[i
*2+1] = (drflac_int32
)right
;
9038 static DRFLAC_INLINE
void drflac_read_pcm_frames_s32__decode_right_side(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int32
* pOutputSamples
)
9040 #if defined(DRFLAC_SUPPORT_SSE2)
9041 if (drflac__gIsSSE2Supported
&& pFlac
->bitsPerSample
<= 24) {
9042 drflac_read_pcm_frames_s32__decode_right_side__sse2(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
9044 #elif defined(DRFLAC_SUPPORT_NEON)
9045 if (drflac__gIsNEONSupported
&& pFlac
->bitsPerSample
<= 24) {
9046 drflac_read_pcm_frames_s32__decode_right_side__neon(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
9050 /* Scalar fallback. */
9052 drflac_read_pcm_frames_s32__decode_right_side__reference(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
9054 drflac_read_pcm_frames_s32__decode_right_side__scalar(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
9061 static DRFLAC_INLINE
void drflac_read_pcm_frames_s32__decode_mid_side__reference(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int32
* pOutputSamples
)
9063 for (drflac_uint64 i
= 0; i
< frameCount
; ++i
) {
9064 drflac_uint32 mid
= pInputSamples0U32
[i
] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9065 drflac_uint32 side
= pInputSamples1U32
[i
] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9067 mid
= (mid
<< 1) | (side
& 0x01);
9069 pOutputSamples
[i
*2+0] = (drflac_int32
)((drflac_uint32
)((drflac_int32
)(mid
+ side
) >> 1) << unusedBitsPerSample
);
9070 pOutputSamples
[i
*2+1] = (drflac_int32
)((drflac_uint32
)((drflac_int32
)(mid
- side
) >> 1) << unusedBitsPerSample
);
9075 static DRFLAC_INLINE
void drflac_read_pcm_frames_s32__decode_mid_side__scalar(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int32
* pOutputSamples
)
9078 drflac_uint64 frameCount4
= frameCount
>> 2;
9079 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
9080 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
9081 drflac_int32 shift
= unusedBitsPerSample
;
9085 for (i
= 0; i
< frameCount4
; ++i
) {
9086 drflac_uint32 temp0L
;
9087 drflac_uint32 temp1L
;
9088 drflac_uint32 temp2L
;
9089 drflac_uint32 temp3L
;
9090 drflac_uint32 temp0R
;
9091 drflac_uint32 temp1R
;
9092 drflac_uint32 temp2R
;
9093 drflac_uint32 temp3R
;
9095 drflac_uint32 mid0
= pInputSamples0U32
[i
*4+0] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9096 drflac_uint32 mid1
= pInputSamples0U32
[i
*4+1] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9097 drflac_uint32 mid2
= pInputSamples0U32
[i
*4+2] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9098 drflac_uint32 mid3
= pInputSamples0U32
[i
*4+3] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9100 drflac_uint32 side0
= pInputSamples1U32
[i
*4+0] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9101 drflac_uint32 side1
= pInputSamples1U32
[i
*4+1] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9102 drflac_uint32 side2
= pInputSamples1U32
[i
*4+2] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9103 drflac_uint32 side3
= pInputSamples1U32
[i
*4+3] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9105 mid0
= (mid0
<< 1) | (side0
& 0x01);
9106 mid1
= (mid1
<< 1) | (side1
& 0x01);
9107 mid2
= (mid2
<< 1) | (side2
& 0x01);
9108 mid3
= (mid3
<< 1) | (side3
& 0x01);
9110 temp0L
= (mid0
+ side0
) << shift
;
9111 temp1L
= (mid1
+ side1
) << shift
;
9112 temp2L
= (mid2
+ side2
) << shift
;
9113 temp3L
= (mid3
+ side3
) << shift
;
9115 temp0R
= (mid0
- side0
) << shift
;
9116 temp1R
= (mid1
- side1
) << shift
;
9117 temp2R
= (mid2
- side2
) << shift
;
9118 temp3R
= (mid3
- side3
) << shift
;
9120 pOutputSamples
[i
*8+0] = (drflac_int32
)temp0L
;
9121 pOutputSamples
[i
*8+1] = (drflac_int32
)temp0R
;
9122 pOutputSamples
[i
*8+2] = (drflac_int32
)temp1L
;
9123 pOutputSamples
[i
*8+3] = (drflac_int32
)temp1R
;
9124 pOutputSamples
[i
*8+4] = (drflac_int32
)temp2L
;
9125 pOutputSamples
[i
*8+5] = (drflac_int32
)temp2R
;
9126 pOutputSamples
[i
*8+6] = (drflac_int32
)temp3L
;
9127 pOutputSamples
[i
*8+7] = (drflac_int32
)temp3R
;
9130 for (i
= 0; i
< frameCount4
; ++i
) {
9131 drflac_uint32 temp0L
;
9132 drflac_uint32 temp1L
;
9133 drflac_uint32 temp2L
;
9134 drflac_uint32 temp3L
;
9135 drflac_uint32 temp0R
;
9136 drflac_uint32 temp1R
;
9137 drflac_uint32 temp2R
;
9138 drflac_uint32 temp3R
;
9140 drflac_uint32 mid0
= pInputSamples0U32
[i
*4+0] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9141 drflac_uint32 mid1
= pInputSamples0U32
[i
*4+1] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9142 drflac_uint32 mid2
= pInputSamples0U32
[i
*4+2] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9143 drflac_uint32 mid3
= pInputSamples0U32
[i
*4+3] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9145 drflac_uint32 side0
= pInputSamples1U32
[i
*4+0] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9146 drflac_uint32 side1
= pInputSamples1U32
[i
*4+1] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9147 drflac_uint32 side2
= pInputSamples1U32
[i
*4+2] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9148 drflac_uint32 side3
= pInputSamples1U32
[i
*4+3] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9150 mid0
= (mid0
<< 1) | (side0
& 0x01);
9151 mid1
= (mid1
<< 1) | (side1
& 0x01);
9152 mid2
= (mid2
<< 1) | (side2
& 0x01);
9153 mid3
= (mid3
<< 1) | (side3
& 0x01);
9155 temp0L
= (drflac_uint32
)((drflac_int32
)(mid0
+ side0
) >> 1);
9156 temp1L
= (drflac_uint32
)((drflac_int32
)(mid1
+ side1
) >> 1);
9157 temp2L
= (drflac_uint32
)((drflac_int32
)(mid2
+ side2
) >> 1);
9158 temp3L
= (drflac_uint32
)((drflac_int32
)(mid3
+ side3
) >> 1);
9160 temp0R
= (drflac_uint32
)((drflac_int32
)(mid0
- side0
) >> 1);
9161 temp1R
= (drflac_uint32
)((drflac_int32
)(mid1
- side1
) >> 1);
9162 temp2R
= (drflac_uint32
)((drflac_int32
)(mid2
- side2
) >> 1);
9163 temp3R
= (drflac_uint32
)((drflac_int32
)(mid3
- side3
) >> 1);
9165 pOutputSamples
[i
*8+0] = (drflac_int32
)temp0L
;
9166 pOutputSamples
[i
*8+1] = (drflac_int32
)temp0R
;
9167 pOutputSamples
[i
*8+2] = (drflac_int32
)temp1L
;
9168 pOutputSamples
[i
*8+3] = (drflac_int32
)temp1R
;
9169 pOutputSamples
[i
*8+4] = (drflac_int32
)temp2L
;
9170 pOutputSamples
[i
*8+5] = (drflac_int32
)temp2R
;
9171 pOutputSamples
[i
*8+6] = (drflac_int32
)temp3L
;
9172 pOutputSamples
[i
*8+7] = (drflac_int32
)temp3R
;
9176 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
9177 drflac_uint32 mid
= pInputSamples0U32
[i
] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9178 drflac_uint32 side
= pInputSamples1U32
[i
] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9180 mid
= (mid
<< 1) | (side
& 0x01);
9182 pOutputSamples
[i
*2+0] = (drflac_int32
)((drflac_uint32
)((drflac_int32
)(mid
+ side
) >> 1) << unusedBitsPerSample
);
9183 pOutputSamples
[i
*2+1] = (drflac_int32
)((drflac_uint32
)((drflac_int32
)(mid
- side
) >> 1) << unusedBitsPerSample
);
9187 #if defined(DRFLAC_SUPPORT_SSE2)
9188 static DRFLAC_INLINE
void drflac_read_pcm_frames_s32__decode_mid_side__sse2(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int32
* pOutputSamples
)
9191 drflac_uint64 frameCount4
= frameCount
>> 2;
9192 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
9193 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
9194 drflac_int32 shift
= unusedBitsPerSample
;
9196 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 24);
9199 for (i
= 0; i
< frameCount4
; ++i
) {
9205 mid
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples0
+ i
), pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
);
9206 side
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples1
+ i
), pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
);
9208 mid
= _mm_or_si128(_mm_slli_epi32(mid
, 1), _mm_and_si128(side
, _mm_set1_epi32(0x01)));
9210 left
= _mm_srai_epi32(_mm_add_epi32(mid
, side
), 1);
9211 right
= _mm_srai_epi32(_mm_sub_epi32(mid
, side
), 1);
9213 _mm_storeu_si128((__m128i
*)(pOutputSamples
+ i
*8 + 0), _mm_unpacklo_epi32(left
, right
));
9214 _mm_storeu_si128((__m128i
*)(pOutputSamples
+ i
*8 + 4), _mm_unpackhi_epi32(left
, right
));
9217 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
9218 drflac_uint32 mid
= pInputSamples0U32
[i
] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9219 drflac_uint32 side
= pInputSamples1U32
[i
] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9221 mid
= (mid
<< 1) | (side
& 0x01);
9223 pOutputSamples
[i
*2+0] = (drflac_int32
)(mid
+ side
) >> 1;
9224 pOutputSamples
[i
*2+1] = (drflac_int32
)(mid
- side
) >> 1;
9228 for (i
= 0; i
< frameCount4
; ++i
) {
9234 mid
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples0
+ i
), pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
);
9235 side
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples1
+ i
), pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
);
9237 mid
= _mm_or_si128(_mm_slli_epi32(mid
, 1), _mm_and_si128(side
, _mm_set1_epi32(0x01)));
9239 left
= _mm_slli_epi32(_mm_add_epi32(mid
, side
), shift
);
9240 right
= _mm_slli_epi32(_mm_sub_epi32(mid
, side
), shift
);
9242 _mm_storeu_si128((__m128i
*)(pOutputSamples
+ i
*8 + 0), _mm_unpacklo_epi32(left
, right
));
9243 _mm_storeu_si128((__m128i
*)(pOutputSamples
+ i
*8 + 4), _mm_unpackhi_epi32(left
, right
));
9246 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
9247 drflac_uint32 mid
= pInputSamples0U32
[i
] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9248 drflac_uint32 side
= pInputSamples1U32
[i
] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9250 mid
= (mid
<< 1) | (side
& 0x01);
9252 pOutputSamples
[i
*2+0] = (drflac_int32
)((mid
+ side
) << shift
);
9253 pOutputSamples
[i
*2+1] = (drflac_int32
)((mid
- side
) << shift
);
9259 #if defined(DRFLAC_SUPPORT_NEON)
9260 static DRFLAC_INLINE
void drflac_read_pcm_frames_s32__decode_mid_side__neon(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int32
* pOutputSamples
)
9263 drflac_uint64 frameCount4
= frameCount
>> 2;
9264 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
9265 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
9266 drflac_int32 shift
= unusedBitsPerSample
;
9267 int32x4_t wbpsShift0_4
; /* wbps = Wasted Bits Per Sample */
9268 int32x4_t wbpsShift1_4
; /* wbps = Wasted Bits Per Sample */
9271 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 24);
9273 wbpsShift0_4
= vdupq_n_s32(pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
);
9274 wbpsShift1_4
= vdupq_n_s32(pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
);
9275 one4
= vdupq_n_u32(1);
9278 for (i
= 0; i
< frameCount4
; ++i
) {
9284 mid
= vshlq_u32(vld1q_u32(pInputSamples0U32
+ i
*4), wbpsShift0_4
);
9285 side
= vshlq_u32(vld1q_u32(pInputSamples1U32
+ i
*4), wbpsShift1_4
);
9287 mid
= vorrq_u32(vshlq_n_u32(mid
, 1), vandq_u32(side
, one4
));
9289 left
= vshrq_n_s32(vreinterpretq_s32_u32(vaddq_u32(mid
, side
)), 1);
9290 right
= vshrq_n_s32(vreinterpretq_s32_u32(vsubq_u32(mid
, side
)), 1);
9292 drflac__vst2q_s32(pOutputSamples
+ i
*8, vzipq_s32(left
, right
));
9295 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
9296 drflac_uint32 mid
= pInputSamples0U32
[i
] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9297 drflac_uint32 side
= pInputSamples1U32
[i
] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9299 mid
= (mid
<< 1) | (side
& 0x01);
9301 pOutputSamples
[i
*2+0] = (drflac_int32
)(mid
+ side
) >> 1;
9302 pOutputSamples
[i
*2+1] = (drflac_int32
)(mid
- side
) >> 1;
9308 shift4
= vdupq_n_s32(shift
);
9310 for (i
= 0; i
< frameCount4
; ++i
) {
9316 mid
= vshlq_u32(vld1q_u32(pInputSamples0U32
+ i
*4), wbpsShift0_4
);
9317 side
= vshlq_u32(vld1q_u32(pInputSamples1U32
+ i
*4), wbpsShift1_4
);
9319 mid
= vorrq_u32(vshlq_n_u32(mid
, 1), vandq_u32(side
, one4
));
9321 left
= vreinterpretq_s32_u32(vshlq_u32(vaddq_u32(mid
, side
), shift4
));
9322 right
= vreinterpretq_s32_u32(vshlq_u32(vsubq_u32(mid
, side
), shift4
));
9324 drflac__vst2q_s32(pOutputSamples
+ i
*8, vzipq_s32(left
, right
));
9327 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
9328 drflac_uint32 mid
= pInputSamples0U32
[i
] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9329 drflac_uint32 side
= pInputSamples1U32
[i
] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9331 mid
= (mid
<< 1) | (side
& 0x01);
9333 pOutputSamples
[i
*2+0] = (drflac_int32
)((mid
+ side
) << shift
);
9334 pOutputSamples
[i
*2+1] = (drflac_int32
)((mid
- side
) << shift
);
9340 static DRFLAC_INLINE
void drflac_read_pcm_frames_s32__decode_mid_side(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int32
* pOutputSamples
)
9342 #if defined(DRFLAC_SUPPORT_SSE2)
9343 if (drflac__gIsSSE2Supported
&& pFlac
->bitsPerSample
<= 24) {
9344 drflac_read_pcm_frames_s32__decode_mid_side__sse2(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
9346 #elif defined(DRFLAC_SUPPORT_NEON)
9347 if (drflac__gIsNEONSupported
&& pFlac
->bitsPerSample
<= 24) {
9348 drflac_read_pcm_frames_s32__decode_mid_side__neon(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
9352 /* Scalar fallback. */
9354 drflac_read_pcm_frames_s32__decode_mid_side__reference(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
9356 drflac_read_pcm_frames_s32__decode_mid_side__scalar(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
9363 static DRFLAC_INLINE
void drflac_read_pcm_frames_s32__decode_independent_stereo__reference(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int32
* pOutputSamples
)
9365 for (drflac_uint64 i
= 0; i
< frameCount
; ++i
) {
9366 pOutputSamples
[i
*2+0] = (drflac_int32
)((drflac_uint32
)pInputSamples0
[i
] << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
));
9367 pOutputSamples
[i
*2+1] = (drflac_int32
)((drflac_uint32
)pInputSamples1
[i
] << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
));
9372 static DRFLAC_INLINE
void drflac_read_pcm_frames_s32__decode_independent_stereo__scalar(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int32
* pOutputSamples
)
9375 drflac_uint64 frameCount4
= frameCount
>> 2;
9376 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
9377 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
9378 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9379 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9381 for (i
= 0; i
< frameCount4
; ++i
) {
9382 drflac_uint32 tempL0
= pInputSamples0U32
[i
*4+0] << shift0
;
9383 drflac_uint32 tempL1
= pInputSamples0U32
[i
*4+1] << shift0
;
9384 drflac_uint32 tempL2
= pInputSamples0U32
[i
*4+2] << shift0
;
9385 drflac_uint32 tempL3
= pInputSamples0U32
[i
*4+3] << shift0
;
9387 drflac_uint32 tempR0
= pInputSamples1U32
[i
*4+0] << shift1
;
9388 drflac_uint32 tempR1
= pInputSamples1U32
[i
*4+1] << shift1
;
9389 drflac_uint32 tempR2
= pInputSamples1U32
[i
*4+2] << shift1
;
9390 drflac_uint32 tempR3
= pInputSamples1U32
[i
*4+3] << shift1
;
9392 pOutputSamples
[i
*8+0] = (drflac_int32
)tempL0
;
9393 pOutputSamples
[i
*8+1] = (drflac_int32
)tempR0
;
9394 pOutputSamples
[i
*8+2] = (drflac_int32
)tempL1
;
9395 pOutputSamples
[i
*8+3] = (drflac_int32
)tempR1
;
9396 pOutputSamples
[i
*8+4] = (drflac_int32
)tempL2
;
9397 pOutputSamples
[i
*8+5] = (drflac_int32
)tempR2
;
9398 pOutputSamples
[i
*8+6] = (drflac_int32
)tempL3
;
9399 pOutputSamples
[i
*8+7] = (drflac_int32
)tempR3
;
9402 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
9403 pOutputSamples
[i
*2+0] = (drflac_int32
)(pInputSamples0U32
[i
] << shift0
);
9404 pOutputSamples
[i
*2+1] = (drflac_int32
)(pInputSamples1U32
[i
] << shift1
);
9408 #if defined(DRFLAC_SUPPORT_SSE2)
9409 static DRFLAC_INLINE
void drflac_read_pcm_frames_s32__decode_independent_stereo__sse2(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int32
* pOutputSamples
)
9412 drflac_uint64 frameCount4
= frameCount
>> 2;
9413 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
9414 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
9415 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9416 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9418 for (i
= 0; i
< frameCount4
; ++i
) {
9419 __m128i left
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples0
+ i
), shift0
);
9420 __m128i right
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples1
+ i
), shift1
);
9422 _mm_storeu_si128((__m128i
*)(pOutputSamples
+ i
*8 + 0), _mm_unpacklo_epi32(left
, right
));
9423 _mm_storeu_si128((__m128i
*)(pOutputSamples
+ i
*8 + 4), _mm_unpackhi_epi32(left
, right
));
9426 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
9427 pOutputSamples
[i
*2+0] = (drflac_int32
)(pInputSamples0U32
[i
] << shift0
);
9428 pOutputSamples
[i
*2+1] = (drflac_int32
)(pInputSamples1U32
[i
] << shift1
);
9433 #if defined(DRFLAC_SUPPORT_NEON)
9434 static DRFLAC_INLINE
void drflac_read_pcm_frames_s32__decode_independent_stereo__neon(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int32
* pOutputSamples
)
9437 drflac_uint64 frameCount4
= frameCount
>> 2;
9438 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
9439 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
9440 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9441 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9443 int32x4_t shift4_0
= vdupq_n_s32(shift0
);
9444 int32x4_t shift4_1
= vdupq_n_s32(shift1
);
9446 for (i
= 0; i
< frameCount4
; ++i
) {
9450 left
= vreinterpretq_s32_u32(vshlq_u32(vld1q_u32(pInputSamples0U32
+ i
*4), shift4_0
));
9451 right
= vreinterpretq_s32_u32(vshlq_u32(vld1q_u32(pInputSamples1U32
+ i
*4), shift4_1
));
9453 drflac__vst2q_s32(pOutputSamples
+ i
*8, vzipq_s32(left
, right
));
9456 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
9457 pOutputSamples
[i
*2+0] = (drflac_int32
)(pInputSamples0U32
[i
] << shift0
);
9458 pOutputSamples
[i
*2+1] = (drflac_int32
)(pInputSamples1U32
[i
] << shift1
);
9463 static DRFLAC_INLINE
void drflac_read_pcm_frames_s32__decode_independent_stereo(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int32
* pOutputSamples
)
9465 #if defined(DRFLAC_SUPPORT_SSE2)
9466 if (drflac__gIsSSE2Supported
&& pFlac
->bitsPerSample
<= 24) {
9467 drflac_read_pcm_frames_s32__decode_independent_stereo__sse2(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
9469 #elif defined(DRFLAC_SUPPORT_NEON)
9470 if (drflac__gIsNEONSupported
&& pFlac
->bitsPerSample
<= 24) {
9471 drflac_read_pcm_frames_s32__decode_independent_stereo__neon(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
9475 /* Scalar fallback. */
9477 drflac_read_pcm_frames_s32__decode_independent_stereo__reference(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
9479 drflac_read_pcm_frames_s32__decode_independent_stereo__scalar(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
9485 DRFLAC_API drflac_uint64
drflac_read_pcm_frames_s32(drflac
* pFlac
, drflac_uint64 framesToRead
, drflac_int32
* pBufferOut
)
9487 drflac_uint64 framesRead
;
9488 drflac_uint32 unusedBitsPerSample
;
9490 if (pFlac
== NULL
|| framesToRead
== 0) {
9494 if (pBufferOut
== NULL
) {
9495 return drflac__seek_forward_by_pcm_frames(pFlac
, framesToRead
);
9498 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 32);
9499 unusedBitsPerSample
= 32 - pFlac
->bitsPerSample
;
9502 while (framesToRead
> 0) {
9503 /* If we've run out of samples in this frame, go to the next. */
9504 if (pFlac
->currentFLACFrame
.pcmFramesRemaining
== 0) {
9505 if (!drflac__read_and_decode_next_flac_frame(pFlac
)) {
9506 break; /* Couldn't read the next frame, so just break from the loop and return. */
9509 unsigned int channelCount
= drflac__get_channel_count_from_channel_assignment(pFlac
->currentFLACFrame
.header
.channelAssignment
);
9510 drflac_uint64 iFirstPCMFrame
= pFlac
->currentFLACFrame
.header
.blockSizeInPCMFrames
- pFlac
->currentFLACFrame
.pcmFramesRemaining
;
9511 drflac_uint64 frameCountThisIteration
= framesToRead
;
9513 if (frameCountThisIteration
> pFlac
->currentFLACFrame
.pcmFramesRemaining
) {
9514 frameCountThisIteration
= pFlac
->currentFLACFrame
.pcmFramesRemaining
;
9517 if (channelCount
== 2) {
9518 const drflac_int32
* pDecodedSamples0
= pFlac
->currentFLACFrame
.subframes
[0].pSamplesS32
+ iFirstPCMFrame
;
9519 const drflac_int32
* pDecodedSamples1
= pFlac
->currentFLACFrame
.subframes
[1].pSamplesS32
+ iFirstPCMFrame
;
9521 switch (pFlac
->currentFLACFrame
.header
.channelAssignment
)
9523 case DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE
:
9525 drflac_read_pcm_frames_s32__decode_left_side(pFlac
, frameCountThisIteration
, unusedBitsPerSample
, pDecodedSamples0
, pDecodedSamples1
, pBufferOut
);
9528 case DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE
:
9530 drflac_read_pcm_frames_s32__decode_right_side(pFlac
, frameCountThisIteration
, unusedBitsPerSample
, pDecodedSamples0
, pDecodedSamples1
, pBufferOut
);
9533 case DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE
:
9535 drflac_read_pcm_frames_s32__decode_mid_side(pFlac
, frameCountThisIteration
, unusedBitsPerSample
, pDecodedSamples0
, pDecodedSamples1
, pBufferOut
);
9538 case DRFLAC_CHANNEL_ASSIGNMENT_INDEPENDENT
:
9541 drflac_read_pcm_frames_s32__decode_independent_stereo(pFlac
, frameCountThisIteration
, unusedBitsPerSample
, pDecodedSamples0
, pDecodedSamples1
, pBufferOut
);
9545 /* Generic interleaving. */
9547 for (i
= 0; i
< frameCountThisIteration
; ++i
) {
9549 for (j
= 0; j
< channelCount
; ++j
) {
9550 pBufferOut
[(i
*channelCount
)+j
] = (drflac_int32
)((drflac_uint32
)(pFlac
->currentFLACFrame
.subframes
[j
].pSamplesS32
[iFirstPCMFrame
+ i
]) << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[j
].wastedBitsPerSample
));
9555 framesRead
+= frameCountThisIteration
;
9556 pBufferOut
+= frameCountThisIteration
* channelCount
;
9557 framesToRead
-= frameCountThisIteration
;
9558 pFlac
->currentPCMFrame
+= frameCountThisIteration
;
9559 pFlac
->currentFLACFrame
.pcmFramesRemaining
-= (drflac_uint32
)frameCountThisIteration
;
9568 static DRFLAC_INLINE
void drflac_read_pcm_frames_s16__decode_left_side__reference(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int16
* pOutputSamples
)
9571 for (i
= 0; i
< frameCount
; ++i
) {
9572 drflac_uint32 left
= (drflac_uint32
)pInputSamples0
[i
] << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
);
9573 drflac_uint32 side
= (drflac_uint32
)pInputSamples1
[i
] << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
);
9574 drflac_uint32 right
= left
- side
;
9579 pOutputSamples
[i
*2+0] = (drflac_int16
)left
;
9580 pOutputSamples
[i
*2+1] = (drflac_int16
)right
;
9585 static DRFLAC_INLINE
void drflac_read_pcm_frames_s16__decode_left_side__scalar(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int16
* pOutputSamples
)
9588 drflac_uint64 frameCount4
= frameCount
>> 2;
9589 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
9590 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
9591 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9592 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9594 for (i
= 0; i
< frameCount4
; ++i
) {
9595 drflac_uint32 left0
= pInputSamples0U32
[i
*4+0] << shift0
;
9596 drflac_uint32 left1
= pInputSamples0U32
[i
*4+1] << shift0
;
9597 drflac_uint32 left2
= pInputSamples0U32
[i
*4+2] << shift0
;
9598 drflac_uint32 left3
= pInputSamples0U32
[i
*4+3] << shift0
;
9600 drflac_uint32 side0
= pInputSamples1U32
[i
*4+0] << shift1
;
9601 drflac_uint32 side1
= pInputSamples1U32
[i
*4+1] << shift1
;
9602 drflac_uint32 side2
= pInputSamples1U32
[i
*4+2] << shift1
;
9603 drflac_uint32 side3
= pInputSamples1U32
[i
*4+3] << shift1
;
9605 drflac_uint32 right0
= left0
- side0
;
9606 drflac_uint32 right1
= left1
- side1
;
9607 drflac_uint32 right2
= left2
- side2
;
9608 drflac_uint32 right3
= left3
- side3
;
9620 pOutputSamples
[i
*8+0] = (drflac_int16
)left0
;
9621 pOutputSamples
[i
*8+1] = (drflac_int16
)right0
;
9622 pOutputSamples
[i
*8+2] = (drflac_int16
)left1
;
9623 pOutputSamples
[i
*8+3] = (drflac_int16
)right1
;
9624 pOutputSamples
[i
*8+4] = (drflac_int16
)left2
;
9625 pOutputSamples
[i
*8+5] = (drflac_int16
)right2
;
9626 pOutputSamples
[i
*8+6] = (drflac_int16
)left3
;
9627 pOutputSamples
[i
*8+7] = (drflac_int16
)right3
;
9630 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
9631 drflac_uint32 left
= pInputSamples0U32
[i
] << shift0
;
9632 drflac_uint32 side
= pInputSamples1U32
[i
] << shift1
;
9633 drflac_uint32 right
= left
- side
;
9638 pOutputSamples
[i
*2+0] = (drflac_int16
)left
;
9639 pOutputSamples
[i
*2+1] = (drflac_int16
)right
;
9643 #if defined(DRFLAC_SUPPORT_SSE2)
9644 static DRFLAC_INLINE
void drflac_read_pcm_frames_s16__decode_left_side__sse2(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int16
* pOutputSamples
)
9647 drflac_uint64 frameCount4
= frameCount
>> 2;
9648 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
9649 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
9650 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9651 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9653 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 24);
9655 for (i
= 0; i
< frameCount4
; ++i
) {
9656 __m128i left
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples0
+ i
), shift0
);
9657 __m128i side
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples1
+ i
), shift1
);
9658 __m128i right
= _mm_sub_epi32(left
, side
);
9660 left
= _mm_srai_epi32(left
, 16);
9661 right
= _mm_srai_epi32(right
, 16);
9663 _mm_storeu_si128((__m128i
*)(pOutputSamples
+ i
*8), drflac__mm_packs_interleaved_epi32(left
, right
));
9666 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
9667 drflac_uint32 left
= pInputSamples0U32
[i
] << shift0
;
9668 drflac_uint32 side
= pInputSamples1U32
[i
] << shift1
;
9669 drflac_uint32 right
= left
- side
;
9674 pOutputSamples
[i
*2+0] = (drflac_int16
)left
;
9675 pOutputSamples
[i
*2+1] = (drflac_int16
)right
;
9680 #if defined(DRFLAC_SUPPORT_NEON)
9681 static DRFLAC_INLINE
void drflac_read_pcm_frames_s16__decode_left_side__neon(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int16
* pOutputSamples
)
9684 drflac_uint64 frameCount4
= frameCount
>> 2;
9685 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
9686 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
9687 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9688 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9692 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 24);
9694 shift0_4
= vdupq_n_s32(shift0
);
9695 shift1_4
= vdupq_n_s32(shift1
);
9697 for (i
= 0; i
< frameCount4
; ++i
) {
9702 left
= vshlq_u32(vld1q_u32(pInputSamples0U32
+ i
*4), shift0_4
);
9703 side
= vshlq_u32(vld1q_u32(pInputSamples1U32
+ i
*4), shift1_4
);
9704 right
= vsubq_u32(left
, side
);
9706 left
= vshrq_n_u32(left
, 16);
9707 right
= vshrq_n_u32(right
, 16);
9709 drflac__vst2q_u16((drflac_uint16
*)pOutputSamples
+ i
*8, vzip_u16(vmovn_u32(left
), vmovn_u32(right
)));
9712 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
9713 drflac_uint32 left
= pInputSamples0U32
[i
] << shift0
;
9714 drflac_uint32 side
= pInputSamples1U32
[i
] << shift1
;
9715 drflac_uint32 right
= left
- side
;
9720 pOutputSamples
[i
*2+0] = (drflac_int16
)left
;
9721 pOutputSamples
[i
*2+1] = (drflac_int16
)right
;
9726 static DRFLAC_INLINE
void drflac_read_pcm_frames_s16__decode_left_side(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int16
* pOutputSamples
)
9728 #if defined(DRFLAC_SUPPORT_SSE2)
9729 if (drflac__gIsSSE2Supported
&& pFlac
->bitsPerSample
<= 24) {
9730 drflac_read_pcm_frames_s16__decode_left_side__sse2(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
9732 #elif defined(DRFLAC_SUPPORT_NEON)
9733 if (drflac__gIsNEONSupported
&& pFlac
->bitsPerSample
<= 24) {
9734 drflac_read_pcm_frames_s16__decode_left_side__neon(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
9738 /* Scalar fallback. */
9740 drflac_read_pcm_frames_s16__decode_left_side__reference(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
9742 drflac_read_pcm_frames_s16__decode_left_side__scalar(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
9749 static DRFLAC_INLINE
void drflac_read_pcm_frames_s16__decode_right_side__reference(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int16
* pOutputSamples
)
9752 for (i
= 0; i
< frameCount
; ++i
) {
9753 drflac_uint32 side
= (drflac_uint32
)pInputSamples0
[i
] << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
);
9754 drflac_uint32 right
= (drflac_uint32
)pInputSamples1
[i
] << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
);
9755 drflac_uint32 left
= right
+ side
;
9760 pOutputSamples
[i
*2+0] = (drflac_int16
)left
;
9761 pOutputSamples
[i
*2+1] = (drflac_int16
)right
;
9766 static DRFLAC_INLINE
void drflac_read_pcm_frames_s16__decode_right_side__scalar(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int16
* pOutputSamples
)
9769 drflac_uint64 frameCount4
= frameCount
>> 2;
9770 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
9771 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
9772 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9773 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9775 for (i
= 0; i
< frameCount4
; ++i
) {
9776 drflac_uint32 side0
= pInputSamples0U32
[i
*4+0] << shift0
;
9777 drflac_uint32 side1
= pInputSamples0U32
[i
*4+1] << shift0
;
9778 drflac_uint32 side2
= pInputSamples0U32
[i
*4+2] << shift0
;
9779 drflac_uint32 side3
= pInputSamples0U32
[i
*4+3] << shift0
;
9781 drflac_uint32 right0
= pInputSamples1U32
[i
*4+0] << shift1
;
9782 drflac_uint32 right1
= pInputSamples1U32
[i
*4+1] << shift1
;
9783 drflac_uint32 right2
= pInputSamples1U32
[i
*4+2] << shift1
;
9784 drflac_uint32 right3
= pInputSamples1U32
[i
*4+3] << shift1
;
9786 drflac_uint32 left0
= right0
+ side0
;
9787 drflac_uint32 left1
= right1
+ side1
;
9788 drflac_uint32 left2
= right2
+ side2
;
9789 drflac_uint32 left3
= right3
+ side3
;
9801 pOutputSamples
[i
*8+0] = (drflac_int16
)left0
;
9802 pOutputSamples
[i
*8+1] = (drflac_int16
)right0
;
9803 pOutputSamples
[i
*8+2] = (drflac_int16
)left1
;
9804 pOutputSamples
[i
*8+3] = (drflac_int16
)right1
;
9805 pOutputSamples
[i
*8+4] = (drflac_int16
)left2
;
9806 pOutputSamples
[i
*8+5] = (drflac_int16
)right2
;
9807 pOutputSamples
[i
*8+6] = (drflac_int16
)left3
;
9808 pOutputSamples
[i
*8+7] = (drflac_int16
)right3
;
9811 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
9812 drflac_uint32 side
= pInputSamples0U32
[i
] << shift0
;
9813 drflac_uint32 right
= pInputSamples1U32
[i
] << shift1
;
9814 drflac_uint32 left
= right
+ side
;
9819 pOutputSamples
[i
*2+0] = (drflac_int16
)left
;
9820 pOutputSamples
[i
*2+1] = (drflac_int16
)right
;
9824 #if defined(DRFLAC_SUPPORT_SSE2)
9825 static DRFLAC_INLINE
void drflac_read_pcm_frames_s16__decode_right_side__sse2(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int16
* pOutputSamples
)
9828 drflac_uint64 frameCount4
= frameCount
>> 2;
9829 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
9830 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
9831 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9832 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9834 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 24);
9836 for (i
= 0; i
< frameCount4
; ++i
) {
9837 __m128i side
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples0
+ i
), shift0
);
9838 __m128i right
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples1
+ i
), shift1
);
9839 __m128i left
= _mm_add_epi32(right
, side
);
9841 left
= _mm_srai_epi32(left
, 16);
9842 right
= _mm_srai_epi32(right
, 16);
9844 _mm_storeu_si128((__m128i
*)(pOutputSamples
+ i
*8), drflac__mm_packs_interleaved_epi32(left
, right
));
9847 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
9848 drflac_uint32 side
= pInputSamples0U32
[i
] << shift0
;
9849 drflac_uint32 right
= pInputSamples1U32
[i
] << shift1
;
9850 drflac_uint32 left
= right
+ side
;
9855 pOutputSamples
[i
*2+0] = (drflac_int16
)left
;
9856 pOutputSamples
[i
*2+1] = (drflac_int16
)right
;
9861 #if defined(DRFLAC_SUPPORT_NEON)
9862 static DRFLAC_INLINE
void drflac_read_pcm_frames_s16__decode_right_side__neon(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int16
* pOutputSamples
)
9865 drflac_uint64 frameCount4
= frameCount
>> 2;
9866 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
9867 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
9868 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9869 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9873 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 24);
9875 shift0_4
= vdupq_n_s32(shift0
);
9876 shift1_4
= vdupq_n_s32(shift1
);
9878 for (i
= 0; i
< frameCount4
; ++i
) {
9883 side
= vshlq_u32(vld1q_u32(pInputSamples0U32
+ i
*4), shift0_4
);
9884 right
= vshlq_u32(vld1q_u32(pInputSamples1U32
+ i
*4), shift1_4
);
9885 left
= vaddq_u32(right
, side
);
9887 left
= vshrq_n_u32(left
, 16);
9888 right
= vshrq_n_u32(right
, 16);
9890 drflac__vst2q_u16((drflac_uint16
*)pOutputSamples
+ i
*8, vzip_u16(vmovn_u32(left
), vmovn_u32(right
)));
9893 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
9894 drflac_uint32 side
= pInputSamples0U32
[i
] << shift0
;
9895 drflac_uint32 right
= pInputSamples1U32
[i
] << shift1
;
9896 drflac_uint32 left
= right
+ side
;
9901 pOutputSamples
[i
*2+0] = (drflac_int16
)left
;
9902 pOutputSamples
[i
*2+1] = (drflac_int16
)right
;
9907 static DRFLAC_INLINE
void drflac_read_pcm_frames_s16__decode_right_side(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int16
* pOutputSamples
)
9909 #if defined(DRFLAC_SUPPORT_SSE2)
9910 if (drflac__gIsSSE2Supported
&& pFlac
->bitsPerSample
<= 24) {
9911 drflac_read_pcm_frames_s16__decode_right_side__sse2(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
9913 #elif defined(DRFLAC_SUPPORT_NEON)
9914 if (drflac__gIsNEONSupported
&& pFlac
->bitsPerSample
<= 24) {
9915 drflac_read_pcm_frames_s16__decode_right_side__neon(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
9919 /* Scalar fallback. */
9921 drflac_read_pcm_frames_s16__decode_right_side__reference(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
9923 drflac_read_pcm_frames_s16__decode_right_side__scalar(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
9930 static DRFLAC_INLINE
void drflac_read_pcm_frames_s16__decode_mid_side__reference(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int16
* pOutputSamples
)
9932 for (drflac_uint64 i
= 0; i
< frameCount
; ++i
) {
9933 drflac_uint32 mid
= (drflac_uint32
)pInputSamples0
[i
] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9934 drflac_uint32 side
= (drflac_uint32
)pInputSamples1
[i
] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9936 mid
= (mid
<< 1) | (side
& 0x01);
9938 pOutputSamples
[i
*2+0] = (drflac_int16
)(((drflac_uint32
)((drflac_int32
)(mid
+ side
) >> 1) << unusedBitsPerSample
) >> 16);
9939 pOutputSamples
[i
*2+1] = (drflac_int16
)(((drflac_uint32
)((drflac_int32
)(mid
- side
) >> 1) << unusedBitsPerSample
) >> 16);
9944 static DRFLAC_INLINE
void drflac_read_pcm_frames_s16__decode_mid_side__scalar(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int16
* pOutputSamples
)
9947 drflac_uint64 frameCount4
= frameCount
>> 2;
9948 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
9949 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
9950 drflac_uint32 shift
= unusedBitsPerSample
;
9954 for (i
= 0; i
< frameCount4
; ++i
) {
9955 drflac_uint32 temp0L
;
9956 drflac_uint32 temp1L
;
9957 drflac_uint32 temp2L
;
9958 drflac_uint32 temp3L
;
9959 drflac_uint32 temp0R
;
9960 drflac_uint32 temp1R
;
9961 drflac_uint32 temp2R
;
9962 drflac_uint32 temp3R
;
9964 drflac_uint32 mid0
= pInputSamples0U32
[i
*4+0] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9965 drflac_uint32 mid1
= pInputSamples0U32
[i
*4+1] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9966 drflac_uint32 mid2
= pInputSamples0U32
[i
*4+2] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9967 drflac_uint32 mid3
= pInputSamples0U32
[i
*4+3] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
9969 drflac_uint32 side0
= pInputSamples1U32
[i
*4+0] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9970 drflac_uint32 side1
= pInputSamples1U32
[i
*4+1] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9971 drflac_uint32 side2
= pInputSamples1U32
[i
*4+2] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9972 drflac_uint32 side3
= pInputSamples1U32
[i
*4+3] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
9974 mid0
= (mid0
<< 1) | (side0
& 0x01);
9975 mid1
= (mid1
<< 1) | (side1
& 0x01);
9976 mid2
= (mid2
<< 1) | (side2
& 0x01);
9977 mid3
= (mid3
<< 1) | (side3
& 0x01);
9979 temp0L
= (mid0
+ side0
) << shift
;
9980 temp1L
= (mid1
+ side1
) << shift
;
9981 temp2L
= (mid2
+ side2
) << shift
;
9982 temp3L
= (mid3
+ side3
) << shift
;
9984 temp0R
= (mid0
- side0
) << shift
;
9985 temp1R
= (mid1
- side1
) << shift
;
9986 temp2R
= (mid2
- side2
) << shift
;
9987 temp3R
= (mid3
- side3
) << shift
;
9999 pOutputSamples
[i
*8+0] = (drflac_int16
)temp0L
;
10000 pOutputSamples
[i
*8+1] = (drflac_int16
)temp0R
;
10001 pOutputSamples
[i
*8+2] = (drflac_int16
)temp1L
;
10002 pOutputSamples
[i
*8+3] = (drflac_int16
)temp1R
;
10003 pOutputSamples
[i
*8+4] = (drflac_int16
)temp2L
;
10004 pOutputSamples
[i
*8+5] = (drflac_int16
)temp2R
;
10005 pOutputSamples
[i
*8+6] = (drflac_int16
)temp3L
;
10006 pOutputSamples
[i
*8+7] = (drflac_int16
)temp3R
;
10009 for (i
= 0; i
< frameCount4
; ++i
) {
10010 drflac_uint32 temp0L
;
10011 drflac_uint32 temp1L
;
10012 drflac_uint32 temp2L
;
10013 drflac_uint32 temp3L
;
10014 drflac_uint32 temp0R
;
10015 drflac_uint32 temp1R
;
10016 drflac_uint32 temp2R
;
10017 drflac_uint32 temp3R
;
10019 drflac_uint32 mid0
= pInputSamples0U32
[i
*4+0] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10020 drflac_uint32 mid1
= pInputSamples0U32
[i
*4+1] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10021 drflac_uint32 mid2
= pInputSamples0U32
[i
*4+2] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10022 drflac_uint32 mid3
= pInputSamples0U32
[i
*4+3] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10024 drflac_uint32 side0
= pInputSamples1U32
[i
*4+0] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10025 drflac_uint32 side1
= pInputSamples1U32
[i
*4+1] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10026 drflac_uint32 side2
= pInputSamples1U32
[i
*4+2] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10027 drflac_uint32 side3
= pInputSamples1U32
[i
*4+3] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10029 mid0
= (mid0
<< 1) | (side0
& 0x01);
10030 mid1
= (mid1
<< 1) | (side1
& 0x01);
10031 mid2
= (mid2
<< 1) | (side2
& 0x01);
10032 mid3
= (mid3
<< 1) | (side3
& 0x01);
10034 temp0L
= ((drflac_int32
)(mid0
+ side0
) >> 1);
10035 temp1L
= ((drflac_int32
)(mid1
+ side1
) >> 1);
10036 temp2L
= ((drflac_int32
)(mid2
+ side2
) >> 1);
10037 temp3L
= ((drflac_int32
)(mid3
+ side3
) >> 1);
10039 temp0R
= ((drflac_int32
)(mid0
- side0
) >> 1);
10040 temp1R
= ((drflac_int32
)(mid1
- side1
) >> 1);
10041 temp2R
= ((drflac_int32
)(mid2
- side2
) >> 1);
10042 temp3R
= ((drflac_int32
)(mid3
- side3
) >> 1);
10054 pOutputSamples
[i
*8+0] = (drflac_int16
)temp0L
;
10055 pOutputSamples
[i
*8+1] = (drflac_int16
)temp0R
;
10056 pOutputSamples
[i
*8+2] = (drflac_int16
)temp1L
;
10057 pOutputSamples
[i
*8+3] = (drflac_int16
)temp1R
;
10058 pOutputSamples
[i
*8+4] = (drflac_int16
)temp2L
;
10059 pOutputSamples
[i
*8+5] = (drflac_int16
)temp2R
;
10060 pOutputSamples
[i
*8+6] = (drflac_int16
)temp3L
;
10061 pOutputSamples
[i
*8+7] = (drflac_int16
)temp3R
;
10065 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
10066 drflac_uint32 mid
= pInputSamples0U32
[i
] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10067 drflac_uint32 side
= pInputSamples1U32
[i
] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10069 mid
= (mid
<< 1) | (side
& 0x01);
10071 pOutputSamples
[i
*2+0] = (drflac_int16
)(((drflac_uint32
)((drflac_int32
)(mid
+ side
) >> 1) << unusedBitsPerSample
) >> 16);
10072 pOutputSamples
[i
*2+1] = (drflac_int16
)(((drflac_uint32
)((drflac_int32
)(mid
- side
) >> 1) << unusedBitsPerSample
) >> 16);
10076 #if defined(DRFLAC_SUPPORT_SSE2)
10077 static DRFLAC_INLINE
void drflac_read_pcm_frames_s16__decode_mid_side__sse2(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int16
* pOutputSamples
)
10080 drflac_uint64 frameCount4
= frameCount
>> 2;
10081 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
10082 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
10083 drflac_uint32 shift
= unusedBitsPerSample
;
10085 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 24);
10088 for (i
= 0; i
< frameCount4
; ++i
) {
10094 mid
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples0
+ i
), pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
);
10095 side
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples1
+ i
), pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
);
10097 mid
= _mm_or_si128(_mm_slli_epi32(mid
, 1), _mm_and_si128(side
, _mm_set1_epi32(0x01)));
10099 left
= _mm_srai_epi32(_mm_add_epi32(mid
, side
), 1);
10100 right
= _mm_srai_epi32(_mm_sub_epi32(mid
, side
), 1);
10102 left
= _mm_srai_epi32(left
, 16);
10103 right
= _mm_srai_epi32(right
, 16);
10105 _mm_storeu_si128((__m128i
*)(pOutputSamples
+ i
*8), drflac__mm_packs_interleaved_epi32(left
, right
));
10108 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
10109 drflac_uint32 mid
= pInputSamples0U32
[i
] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10110 drflac_uint32 side
= pInputSamples1U32
[i
] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10112 mid
= (mid
<< 1) | (side
& 0x01);
10114 pOutputSamples
[i
*2+0] = (drflac_int16
)(((drflac_int32
)(mid
+ side
) >> 1) >> 16);
10115 pOutputSamples
[i
*2+1] = (drflac_int16
)(((drflac_int32
)(mid
- side
) >> 1) >> 16);
10119 for (i
= 0; i
< frameCount4
; ++i
) {
10125 mid
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples0
+ i
), pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
);
10126 side
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples1
+ i
), pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
);
10128 mid
= _mm_or_si128(_mm_slli_epi32(mid
, 1), _mm_and_si128(side
, _mm_set1_epi32(0x01)));
10130 left
= _mm_slli_epi32(_mm_add_epi32(mid
, side
), shift
);
10131 right
= _mm_slli_epi32(_mm_sub_epi32(mid
, side
), shift
);
10133 left
= _mm_srai_epi32(left
, 16);
10134 right
= _mm_srai_epi32(right
, 16);
10136 _mm_storeu_si128((__m128i
*)(pOutputSamples
+ i
*8), drflac__mm_packs_interleaved_epi32(left
, right
));
10139 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
10140 drflac_uint32 mid
= pInputSamples0U32
[i
] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10141 drflac_uint32 side
= pInputSamples1U32
[i
] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10143 mid
= (mid
<< 1) | (side
& 0x01);
10145 pOutputSamples
[i
*2+0] = (drflac_int16
)(((mid
+ side
) << shift
) >> 16);
10146 pOutputSamples
[i
*2+1] = (drflac_int16
)(((mid
- side
) << shift
) >> 16);
10152 #if defined(DRFLAC_SUPPORT_NEON)
10153 static DRFLAC_INLINE
void drflac_read_pcm_frames_s16__decode_mid_side__neon(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int16
* pOutputSamples
)
10156 drflac_uint64 frameCount4
= frameCount
>> 2;
10157 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
10158 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
10159 drflac_uint32 shift
= unusedBitsPerSample
;
10160 int32x4_t wbpsShift0_4
; /* wbps = Wasted Bits Per Sample */
10161 int32x4_t wbpsShift1_4
; /* wbps = Wasted Bits Per Sample */
10163 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 24);
10165 wbpsShift0_4
= vdupq_n_s32(pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
);
10166 wbpsShift1_4
= vdupq_n_s32(pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
);
10169 for (i
= 0; i
< frameCount4
; ++i
) {
10175 mid
= vshlq_u32(vld1q_u32(pInputSamples0U32
+ i
*4), wbpsShift0_4
);
10176 side
= vshlq_u32(vld1q_u32(pInputSamples1U32
+ i
*4), wbpsShift1_4
);
10178 mid
= vorrq_u32(vshlq_n_u32(mid
, 1), vandq_u32(side
, vdupq_n_u32(1)));
10180 left
= vshrq_n_s32(vreinterpretq_s32_u32(vaddq_u32(mid
, side
)), 1);
10181 right
= vshrq_n_s32(vreinterpretq_s32_u32(vsubq_u32(mid
, side
)), 1);
10183 left
= vshrq_n_s32(left
, 16);
10184 right
= vshrq_n_s32(right
, 16);
10186 drflac__vst2q_s16(pOutputSamples
+ i
*8, vzip_s16(vmovn_s32(left
), vmovn_s32(right
)));
10189 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
10190 drflac_uint32 mid
= pInputSamples0U32
[i
] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10191 drflac_uint32 side
= pInputSamples1U32
[i
] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10193 mid
= (mid
<< 1) | (side
& 0x01);
10195 pOutputSamples
[i
*2+0] = (drflac_int16
)(((drflac_int32
)(mid
+ side
) >> 1) >> 16);
10196 pOutputSamples
[i
*2+1] = (drflac_int16
)(((drflac_int32
)(mid
- side
) >> 1) >> 16);
10202 shift4
= vdupq_n_s32(shift
);
10204 for (i
= 0; i
< frameCount4
; ++i
) {
10210 mid
= vshlq_u32(vld1q_u32(pInputSamples0U32
+ i
*4), wbpsShift0_4
);
10211 side
= vshlq_u32(vld1q_u32(pInputSamples1U32
+ i
*4), wbpsShift1_4
);
10213 mid
= vorrq_u32(vshlq_n_u32(mid
, 1), vandq_u32(side
, vdupq_n_u32(1)));
10215 left
= vreinterpretq_s32_u32(vshlq_u32(vaddq_u32(mid
, side
), shift4
));
10216 right
= vreinterpretq_s32_u32(vshlq_u32(vsubq_u32(mid
, side
), shift4
));
10218 left
= vshrq_n_s32(left
, 16);
10219 right
= vshrq_n_s32(right
, 16);
10221 drflac__vst2q_s16(pOutputSamples
+ i
*8, vzip_s16(vmovn_s32(left
), vmovn_s32(right
)));
10224 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
10225 drflac_uint32 mid
= pInputSamples0U32
[i
] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10226 drflac_uint32 side
= pInputSamples1U32
[i
] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10228 mid
= (mid
<< 1) | (side
& 0x01);
10230 pOutputSamples
[i
*2+0] = (drflac_int16
)(((mid
+ side
) << shift
) >> 16);
10231 pOutputSamples
[i
*2+1] = (drflac_int16
)(((mid
- side
) << shift
) >> 16);
10237 static DRFLAC_INLINE
void drflac_read_pcm_frames_s16__decode_mid_side(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int16
* pOutputSamples
)
10239 #if defined(DRFLAC_SUPPORT_SSE2)
10240 if (drflac__gIsSSE2Supported
&& pFlac
->bitsPerSample
<= 24) {
10241 drflac_read_pcm_frames_s16__decode_mid_side__sse2(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
10243 #elif defined(DRFLAC_SUPPORT_NEON)
10244 if (drflac__gIsNEONSupported
&& pFlac
->bitsPerSample
<= 24) {
10245 drflac_read_pcm_frames_s16__decode_mid_side__neon(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
10249 /* Scalar fallback. */
10251 drflac_read_pcm_frames_s16__decode_mid_side__reference(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
10253 drflac_read_pcm_frames_s16__decode_mid_side__scalar(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
10260 static DRFLAC_INLINE
void drflac_read_pcm_frames_s16__decode_independent_stereo__reference(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int16
* pOutputSamples
)
10262 for (drflac_uint64 i
= 0; i
< frameCount
; ++i
) {
10263 pOutputSamples
[i
*2+0] = (drflac_int16
)((drflac_int32
)((drflac_uint32
)pInputSamples0
[i
] << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
)) >> 16);
10264 pOutputSamples
[i
*2+1] = (drflac_int16
)((drflac_int32
)((drflac_uint32
)pInputSamples1
[i
] << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
)) >> 16);
10269 static DRFLAC_INLINE
void drflac_read_pcm_frames_s16__decode_independent_stereo__scalar(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int16
* pOutputSamples
)
10272 drflac_uint64 frameCount4
= frameCount
>> 2;
10273 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
10274 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
10275 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10276 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10278 for (i
= 0; i
< frameCount4
; ++i
) {
10279 drflac_uint32 tempL0
= pInputSamples0U32
[i
*4+0] << shift0
;
10280 drflac_uint32 tempL1
= pInputSamples0U32
[i
*4+1] << shift0
;
10281 drflac_uint32 tempL2
= pInputSamples0U32
[i
*4+2] << shift0
;
10282 drflac_uint32 tempL3
= pInputSamples0U32
[i
*4+3] << shift0
;
10284 drflac_uint32 tempR0
= pInputSamples1U32
[i
*4+0] << shift1
;
10285 drflac_uint32 tempR1
= pInputSamples1U32
[i
*4+1] << shift1
;
10286 drflac_uint32 tempR2
= pInputSamples1U32
[i
*4+2] << shift1
;
10287 drflac_uint32 tempR3
= pInputSamples1U32
[i
*4+3] << shift1
;
10299 pOutputSamples
[i
*8+0] = (drflac_int16
)tempL0
;
10300 pOutputSamples
[i
*8+1] = (drflac_int16
)tempR0
;
10301 pOutputSamples
[i
*8+2] = (drflac_int16
)tempL1
;
10302 pOutputSamples
[i
*8+3] = (drflac_int16
)tempR1
;
10303 pOutputSamples
[i
*8+4] = (drflac_int16
)tempL2
;
10304 pOutputSamples
[i
*8+5] = (drflac_int16
)tempR2
;
10305 pOutputSamples
[i
*8+6] = (drflac_int16
)tempL3
;
10306 pOutputSamples
[i
*8+7] = (drflac_int16
)tempR3
;
10309 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
10310 pOutputSamples
[i
*2+0] = (drflac_int16
)((pInputSamples0U32
[i
] << shift0
) >> 16);
10311 pOutputSamples
[i
*2+1] = (drflac_int16
)((pInputSamples1U32
[i
] << shift1
) >> 16);
10315 #if defined(DRFLAC_SUPPORT_SSE2)
10316 static DRFLAC_INLINE
void drflac_read_pcm_frames_s16__decode_independent_stereo__sse2(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int16
* pOutputSamples
)
10319 drflac_uint64 frameCount4
= frameCount
>> 2;
10320 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
10321 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
10322 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10323 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10325 for (i
= 0; i
< frameCount4
; ++i
) {
10326 __m128i left
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples0
+ i
), shift0
);
10327 __m128i right
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples1
+ i
), shift1
);
10329 left
= _mm_srai_epi32(left
, 16);
10330 right
= _mm_srai_epi32(right
, 16);
10332 /* At this point we have results. We can now pack and interleave these into a single __m128i object and then store the in the output buffer. */
10333 _mm_storeu_si128((__m128i
*)(pOutputSamples
+ i
*8), drflac__mm_packs_interleaved_epi32(left
, right
));
10336 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
10337 pOutputSamples
[i
*2+0] = (drflac_int16
)((pInputSamples0U32
[i
] << shift0
) >> 16);
10338 pOutputSamples
[i
*2+1] = (drflac_int16
)((pInputSamples1U32
[i
] << shift1
) >> 16);
10343 #if defined(DRFLAC_SUPPORT_NEON)
10344 static DRFLAC_INLINE
void drflac_read_pcm_frames_s16__decode_independent_stereo__neon(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int16
* pOutputSamples
)
10347 drflac_uint64 frameCount4
= frameCount
>> 2;
10348 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
10349 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
10350 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10351 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10353 int32x4_t shift0_4
= vdupq_n_s32(shift0
);
10354 int32x4_t shift1_4
= vdupq_n_s32(shift1
);
10356 for (i
= 0; i
< frameCount4
; ++i
) {
10360 left
= vreinterpretq_s32_u32(vshlq_u32(vld1q_u32(pInputSamples0U32
+ i
*4), shift0_4
));
10361 right
= vreinterpretq_s32_u32(vshlq_u32(vld1q_u32(pInputSamples1U32
+ i
*4), shift1_4
));
10363 left
= vshrq_n_s32(left
, 16);
10364 right
= vshrq_n_s32(right
, 16);
10366 drflac__vst2q_s16(pOutputSamples
+ i
*8, vzip_s16(vmovn_s32(left
), vmovn_s32(right
)));
10369 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
10370 pOutputSamples
[i
*2+0] = (drflac_int16
)((pInputSamples0U32
[i
] << shift0
) >> 16);
10371 pOutputSamples
[i
*2+1] = (drflac_int16
)((pInputSamples1U32
[i
] << shift1
) >> 16);
10376 static DRFLAC_INLINE
void drflac_read_pcm_frames_s16__decode_independent_stereo(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, drflac_int16
* pOutputSamples
)
10378 #if defined(DRFLAC_SUPPORT_SSE2)
10379 if (drflac__gIsSSE2Supported
&& pFlac
->bitsPerSample
<= 24) {
10380 drflac_read_pcm_frames_s16__decode_independent_stereo__sse2(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
10382 #elif defined(DRFLAC_SUPPORT_NEON)
10383 if (drflac__gIsNEONSupported
&& pFlac
->bitsPerSample
<= 24) {
10384 drflac_read_pcm_frames_s16__decode_independent_stereo__neon(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
10388 /* Scalar fallback. */
10390 drflac_read_pcm_frames_s16__decode_independent_stereo__reference(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
10392 drflac_read_pcm_frames_s16__decode_independent_stereo__scalar(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
10397 DRFLAC_API drflac_uint64
drflac_read_pcm_frames_s16(drflac
* pFlac
, drflac_uint64 framesToRead
, drflac_int16
* pBufferOut
)
10399 drflac_uint64 framesRead
;
10400 drflac_uint32 unusedBitsPerSample
;
10402 if (pFlac
== NULL
|| framesToRead
== 0) {
10406 if (pBufferOut
== NULL
) {
10407 return drflac__seek_forward_by_pcm_frames(pFlac
, framesToRead
);
10410 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 32);
10411 unusedBitsPerSample
= 32 - pFlac
->bitsPerSample
;
10414 while (framesToRead
> 0) {
10415 /* If we've run out of samples in this frame, go to the next. */
10416 if (pFlac
->currentFLACFrame
.pcmFramesRemaining
== 0) {
10417 if (!drflac__read_and_decode_next_flac_frame(pFlac
)) {
10418 break; /* Couldn't read the next frame, so just break from the loop and return. */
10421 unsigned int channelCount
= drflac__get_channel_count_from_channel_assignment(pFlac
->currentFLACFrame
.header
.channelAssignment
);
10422 drflac_uint64 iFirstPCMFrame
= pFlac
->currentFLACFrame
.header
.blockSizeInPCMFrames
- pFlac
->currentFLACFrame
.pcmFramesRemaining
;
10423 drflac_uint64 frameCountThisIteration
= framesToRead
;
10425 if (frameCountThisIteration
> pFlac
->currentFLACFrame
.pcmFramesRemaining
) {
10426 frameCountThisIteration
= pFlac
->currentFLACFrame
.pcmFramesRemaining
;
10429 if (channelCount
== 2) {
10430 const drflac_int32
* pDecodedSamples0
= pFlac
->currentFLACFrame
.subframes
[0].pSamplesS32
+ iFirstPCMFrame
;
10431 const drflac_int32
* pDecodedSamples1
= pFlac
->currentFLACFrame
.subframes
[1].pSamplesS32
+ iFirstPCMFrame
;
10433 switch (pFlac
->currentFLACFrame
.header
.channelAssignment
)
10435 case DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE
:
10437 drflac_read_pcm_frames_s16__decode_left_side(pFlac
, frameCountThisIteration
, unusedBitsPerSample
, pDecodedSamples0
, pDecodedSamples1
, pBufferOut
);
10440 case DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE
:
10442 drflac_read_pcm_frames_s16__decode_right_side(pFlac
, frameCountThisIteration
, unusedBitsPerSample
, pDecodedSamples0
, pDecodedSamples1
, pBufferOut
);
10445 case DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE
:
10447 drflac_read_pcm_frames_s16__decode_mid_side(pFlac
, frameCountThisIteration
, unusedBitsPerSample
, pDecodedSamples0
, pDecodedSamples1
, pBufferOut
);
10450 case DRFLAC_CHANNEL_ASSIGNMENT_INDEPENDENT
:
10453 drflac_read_pcm_frames_s16__decode_independent_stereo(pFlac
, frameCountThisIteration
, unusedBitsPerSample
, pDecodedSamples0
, pDecodedSamples1
, pBufferOut
);
10457 /* Generic interleaving. */
10459 for (i
= 0; i
< frameCountThisIteration
; ++i
) {
10461 for (j
= 0; j
< channelCount
; ++j
) {
10462 drflac_int32 sampleS32
= (drflac_int32
)((drflac_uint32
)(pFlac
->currentFLACFrame
.subframes
[j
].pSamplesS32
[iFirstPCMFrame
+ i
]) << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[j
].wastedBitsPerSample
));
10463 pBufferOut
[(i
*channelCount
)+j
] = (drflac_int16
)(sampleS32
>> 16);
10468 framesRead
+= frameCountThisIteration
;
10469 pBufferOut
+= frameCountThisIteration
* channelCount
;
10470 framesToRead
-= frameCountThisIteration
;
10471 pFlac
->currentPCMFrame
+= frameCountThisIteration
;
10472 pFlac
->currentFLACFrame
.pcmFramesRemaining
-= (drflac_uint32
)frameCountThisIteration
;
10481 static DRFLAC_INLINE
void drflac_read_pcm_frames_f32__decode_left_side__reference(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, float* pOutputSamples
)
10484 for (i
= 0; i
< frameCount
; ++i
) {
10485 drflac_uint32 left
= (drflac_uint32
)pInputSamples0
[i
] << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
);
10486 drflac_uint32 side
= (drflac_uint32
)pInputSamples1
[i
] << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
);
10487 drflac_uint32 right
= left
- side
;
10489 pOutputSamples
[i
*2+0] = (float)((drflac_int32
)left
/ 2147483648.0);
10490 pOutputSamples
[i
*2+1] = (float)((drflac_int32
)right
/ 2147483648.0);
10495 static DRFLAC_INLINE
void drflac_read_pcm_frames_f32__decode_left_side__scalar(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, float* pOutputSamples
)
10498 drflac_uint64 frameCount4
= frameCount
>> 2;
10499 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
10500 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
10501 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10502 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10504 float factor
= 1 / 2147483648.0;
10506 for (i
= 0; i
< frameCount4
; ++i
) {
10507 drflac_uint32 left0
= pInputSamples0U32
[i
*4+0] << shift0
;
10508 drflac_uint32 left1
= pInputSamples0U32
[i
*4+1] << shift0
;
10509 drflac_uint32 left2
= pInputSamples0U32
[i
*4+2] << shift0
;
10510 drflac_uint32 left3
= pInputSamples0U32
[i
*4+3] << shift0
;
10512 drflac_uint32 side0
= pInputSamples1U32
[i
*4+0] << shift1
;
10513 drflac_uint32 side1
= pInputSamples1U32
[i
*4+1] << shift1
;
10514 drflac_uint32 side2
= pInputSamples1U32
[i
*4+2] << shift1
;
10515 drflac_uint32 side3
= pInputSamples1U32
[i
*4+3] << shift1
;
10517 drflac_uint32 right0
= left0
- side0
;
10518 drflac_uint32 right1
= left1
- side1
;
10519 drflac_uint32 right2
= left2
- side2
;
10520 drflac_uint32 right3
= left3
- side3
;
10522 pOutputSamples
[i
*8+0] = (drflac_int32
)left0
* factor
;
10523 pOutputSamples
[i
*8+1] = (drflac_int32
)right0
* factor
;
10524 pOutputSamples
[i
*8+2] = (drflac_int32
)left1
* factor
;
10525 pOutputSamples
[i
*8+3] = (drflac_int32
)right1
* factor
;
10526 pOutputSamples
[i
*8+4] = (drflac_int32
)left2
* factor
;
10527 pOutputSamples
[i
*8+5] = (drflac_int32
)right2
* factor
;
10528 pOutputSamples
[i
*8+6] = (drflac_int32
)left3
* factor
;
10529 pOutputSamples
[i
*8+7] = (drflac_int32
)right3
* factor
;
10532 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
10533 drflac_uint32 left
= pInputSamples0U32
[i
] << shift0
;
10534 drflac_uint32 side
= pInputSamples1U32
[i
] << shift1
;
10535 drflac_uint32 right
= left
- side
;
10537 pOutputSamples
[i
*2+0] = (drflac_int32
)left
* factor
;
10538 pOutputSamples
[i
*2+1] = (drflac_int32
)right
* factor
;
10542 #if defined(DRFLAC_SUPPORT_SSE2)
10543 static DRFLAC_INLINE
void drflac_read_pcm_frames_f32__decode_left_side__sse2(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, float* pOutputSamples
)
10546 drflac_uint64 frameCount4
= frameCount
>> 2;
10547 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
10548 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
10549 drflac_uint32 shift0
= (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
) - 8;
10550 drflac_uint32 shift1
= (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
) - 8;
10553 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 24);
10555 factor
= _mm_set1_ps(1.0f
/ 8388608.0f
);
10557 for (i
= 0; i
< frameCount4
; ++i
) {
10558 __m128i left
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples0
+ i
), shift0
);
10559 __m128i side
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples1
+ i
), shift1
);
10560 __m128i right
= _mm_sub_epi32(left
, side
);
10561 __m128 leftf
= _mm_mul_ps(_mm_cvtepi32_ps(left
), factor
);
10562 __m128 rightf
= _mm_mul_ps(_mm_cvtepi32_ps(right
), factor
);
10564 _mm_storeu_ps(pOutputSamples
+ i
*8 + 0, _mm_unpacklo_ps(leftf
, rightf
));
10565 _mm_storeu_ps(pOutputSamples
+ i
*8 + 4, _mm_unpackhi_ps(leftf
, rightf
));
10568 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
10569 drflac_uint32 left
= pInputSamples0U32
[i
] << shift0
;
10570 drflac_uint32 side
= pInputSamples1U32
[i
] << shift1
;
10571 drflac_uint32 right
= left
- side
;
10573 pOutputSamples
[i
*2+0] = (drflac_int32
)left
/ 8388608.0f
;
10574 pOutputSamples
[i
*2+1] = (drflac_int32
)right
/ 8388608.0f
;
10579 #if defined(DRFLAC_SUPPORT_NEON)
10580 static DRFLAC_INLINE
void drflac_read_pcm_frames_f32__decode_left_side__neon(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, float* pOutputSamples
)
10583 drflac_uint64 frameCount4
= frameCount
>> 2;
10584 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
10585 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
10586 drflac_uint32 shift0
= (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
) - 8;
10587 drflac_uint32 shift1
= (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
) - 8;
10588 float32x4_t factor4
;
10589 int32x4_t shift0_4
;
10590 int32x4_t shift1_4
;
10592 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 24);
10594 factor4
= vdupq_n_f32(1.0f
/ 8388608.0f
);
10595 shift0_4
= vdupq_n_s32(shift0
);
10596 shift1_4
= vdupq_n_s32(shift1
);
10598 for (i
= 0; i
< frameCount4
; ++i
) {
10603 float32x4_t rightf
;
10605 left
= vshlq_u32(vld1q_u32(pInputSamples0U32
+ i
*4), shift0_4
);
10606 side
= vshlq_u32(vld1q_u32(pInputSamples1U32
+ i
*4), shift1_4
);
10607 right
= vsubq_u32(left
, side
);
10608 leftf
= vmulq_f32(vcvtq_f32_s32(vreinterpretq_s32_u32(left
)), factor4
);
10609 rightf
= vmulq_f32(vcvtq_f32_s32(vreinterpretq_s32_u32(right
)), factor4
);
10611 drflac__vst2q_f32(pOutputSamples
+ i
*8, vzipq_f32(leftf
, rightf
));
10614 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
10615 drflac_uint32 left
= pInputSamples0U32
[i
] << shift0
;
10616 drflac_uint32 side
= pInputSamples1U32
[i
] << shift1
;
10617 drflac_uint32 right
= left
- side
;
10619 pOutputSamples
[i
*2+0] = (drflac_int32
)left
/ 8388608.0f
;
10620 pOutputSamples
[i
*2+1] = (drflac_int32
)right
/ 8388608.0f
;
10625 static DRFLAC_INLINE
void drflac_read_pcm_frames_f32__decode_left_side(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, float* pOutputSamples
)
10627 #if defined(DRFLAC_SUPPORT_SSE2)
10628 if (drflac__gIsSSE2Supported
&& pFlac
->bitsPerSample
<= 24) {
10629 drflac_read_pcm_frames_f32__decode_left_side__sse2(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
10631 #elif defined(DRFLAC_SUPPORT_NEON)
10632 if (drflac__gIsNEONSupported
&& pFlac
->bitsPerSample
<= 24) {
10633 drflac_read_pcm_frames_f32__decode_left_side__neon(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
10637 /* Scalar fallback. */
10639 drflac_read_pcm_frames_f32__decode_left_side__reference(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
10641 drflac_read_pcm_frames_f32__decode_left_side__scalar(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
10648 static DRFLAC_INLINE
void drflac_read_pcm_frames_f32__decode_right_side__reference(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, float* pOutputSamples
)
10651 for (i
= 0; i
< frameCount
; ++i
) {
10652 drflac_uint32 side
= (drflac_uint32
)pInputSamples0
[i
] << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
);
10653 drflac_uint32 right
= (drflac_uint32
)pInputSamples1
[i
] << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
);
10654 drflac_uint32 left
= right
+ side
;
10656 pOutputSamples
[i
*2+0] = (float)((drflac_int32
)left
/ 2147483648.0);
10657 pOutputSamples
[i
*2+1] = (float)((drflac_int32
)right
/ 2147483648.0);
10662 static DRFLAC_INLINE
void drflac_read_pcm_frames_f32__decode_right_side__scalar(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, float* pOutputSamples
)
10665 drflac_uint64 frameCount4
= frameCount
>> 2;
10666 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
10667 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
10668 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10669 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10670 float factor
= 1 / 2147483648.0;
10672 for (i
= 0; i
< frameCount4
; ++i
) {
10673 drflac_uint32 side0
= pInputSamples0U32
[i
*4+0] << shift0
;
10674 drflac_uint32 side1
= pInputSamples0U32
[i
*4+1] << shift0
;
10675 drflac_uint32 side2
= pInputSamples0U32
[i
*4+2] << shift0
;
10676 drflac_uint32 side3
= pInputSamples0U32
[i
*4+3] << shift0
;
10678 drflac_uint32 right0
= pInputSamples1U32
[i
*4+0] << shift1
;
10679 drflac_uint32 right1
= pInputSamples1U32
[i
*4+1] << shift1
;
10680 drflac_uint32 right2
= pInputSamples1U32
[i
*4+2] << shift1
;
10681 drflac_uint32 right3
= pInputSamples1U32
[i
*4+3] << shift1
;
10683 drflac_uint32 left0
= right0
+ side0
;
10684 drflac_uint32 left1
= right1
+ side1
;
10685 drflac_uint32 left2
= right2
+ side2
;
10686 drflac_uint32 left3
= right3
+ side3
;
10688 pOutputSamples
[i
*8+0] = (drflac_int32
)left0
* factor
;
10689 pOutputSamples
[i
*8+1] = (drflac_int32
)right0
* factor
;
10690 pOutputSamples
[i
*8+2] = (drflac_int32
)left1
* factor
;
10691 pOutputSamples
[i
*8+3] = (drflac_int32
)right1
* factor
;
10692 pOutputSamples
[i
*8+4] = (drflac_int32
)left2
* factor
;
10693 pOutputSamples
[i
*8+5] = (drflac_int32
)right2
* factor
;
10694 pOutputSamples
[i
*8+6] = (drflac_int32
)left3
* factor
;
10695 pOutputSamples
[i
*8+7] = (drflac_int32
)right3
* factor
;
10698 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
10699 drflac_uint32 side
= pInputSamples0U32
[i
] << shift0
;
10700 drflac_uint32 right
= pInputSamples1U32
[i
] << shift1
;
10701 drflac_uint32 left
= right
+ side
;
10703 pOutputSamples
[i
*2+0] = (drflac_int32
)left
* factor
;
10704 pOutputSamples
[i
*2+1] = (drflac_int32
)right
* factor
;
10708 #if defined(DRFLAC_SUPPORT_SSE2)
10709 static DRFLAC_INLINE
void drflac_read_pcm_frames_f32__decode_right_side__sse2(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, float* pOutputSamples
)
10712 drflac_uint64 frameCount4
= frameCount
>> 2;
10713 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
10714 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
10715 drflac_uint32 shift0
= (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
) - 8;
10716 drflac_uint32 shift1
= (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
) - 8;
10719 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 24);
10721 factor
= _mm_set1_ps(1.0f
/ 8388608.0f
);
10723 for (i
= 0; i
< frameCount4
; ++i
) {
10724 __m128i side
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples0
+ i
), shift0
);
10725 __m128i right
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples1
+ i
), shift1
);
10726 __m128i left
= _mm_add_epi32(right
, side
);
10727 __m128 leftf
= _mm_mul_ps(_mm_cvtepi32_ps(left
), factor
);
10728 __m128 rightf
= _mm_mul_ps(_mm_cvtepi32_ps(right
), factor
);
10730 _mm_storeu_ps(pOutputSamples
+ i
*8 + 0, _mm_unpacklo_ps(leftf
, rightf
));
10731 _mm_storeu_ps(pOutputSamples
+ i
*8 + 4, _mm_unpackhi_ps(leftf
, rightf
));
10734 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
10735 drflac_uint32 side
= pInputSamples0U32
[i
] << shift0
;
10736 drflac_uint32 right
= pInputSamples1U32
[i
] << shift1
;
10737 drflac_uint32 left
= right
+ side
;
10739 pOutputSamples
[i
*2+0] = (drflac_int32
)left
/ 8388608.0f
;
10740 pOutputSamples
[i
*2+1] = (drflac_int32
)right
/ 8388608.0f
;
10745 #if defined(DRFLAC_SUPPORT_NEON)
10746 static DRFLAC_INLINE
void drflac_read_pcm_frames_f32__decode_right_side__neon(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, float* pOutputSamples
)
10749 drflac_uint64 frameCount4
= frameCount
>> 2;
10750 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
10751 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
10752 drflac_uint32 shift0
= (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
) - 8;
10753 drflac_uint32 shift1
= (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
) - 8;
10754 float32x4_t factor4
;
10755 int32x4_t shift0_4
;
10756 int32x4_t shift1_4
;
10758 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 24);
10760 factor4
= vdupq_n_f32(1.0f
/ 8388608.0f
);
10761 shift0_4
= vdupq_n_s32(shift0
);
10762 shift1_4
= vdupq_n_s32(shift1
);
10764 for (i
= 0; i
< frameCount4
; ++i
) {
10769 float32x4_t rightf
;
10771 side
= vshlq_u32(vld1q_u32(pInputSamples0U32
+ i
*4), shift0_4
);
10772 right
= vshlq_u32(vld1q_u32(pInputSamples1U32
+ i
*4), shift1_4
);
10773 left
= vaddq_u32(right
, side
);
10774 leftf
= vmulq_f32(vcvtq_f32_s32(vreinterpretq_s32_u32(left
)), factor4
);
10775 rightf
= vmulq_f32(vcvtq_f32_s32(vreinterpretq_s32_u32(right
)), factor4
);
10777 drflac__vst2q_f32(pOutputSamples
+ i
*8, vzipq_f32(leftf
, rightf
));
10780 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
10781 drflac_uint32 side
= pInputSamples0U32
[i
] << shift0
;
10782 drflac_uint32 right
= pInputSamples1U32
[i
] << shift1
;
10783 drflac_uint32 left
= right
+ side
;
10785 pOutputSamples
[i
*2+0] = (drflac_int32
)left
/ 8388608.0f
;
10786 pOutputSamples
[i
*2+1] = (drflac_int32
)right
/ 8388608.0f
;
10791 static DRFLAC_INLINE
void drflac_read_pcm_frames_f32__decode_right_side(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, float* pOutputSamples
)
10793 #if defined(DRFLAC_SUPPORT_SSE2)
10794 if (drflac__gIsSSE2Supported
&& pFlac
->bitsPerSample
<= 24) {
10795 drflac_read_pcm_frames_f32__decode_right_side__sse2(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
10797 #elif defined(DRFLAC_SUPPORT_NEON)
10798 if (drflac__gIsNEONSupported
&& pFlac
->bitsPerSample
<= 24) {
10799 drflac_read_pcm_frames_f32__decode_right_side__neon(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
10803 /* Scalar fallback. */
10805 drflac_read_pcm_frames_f32__decode_right_side__reference(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
10807 drflac_read_pcm_frames_f32__decode_right_side__scalar(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
10814 static DRFLAC_INLINE
void drflac_read_pcm_frames_f32__decode_mid_side__reference(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, float* pOutputSamples
)
10816 for (drflac_uint64 i
= 0; i
< frameCount
; ++i
) {
10817 drflac_uint32 mid
= (drflac_uint32
)pInputSamples0
[i
] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10818 drflac_uint32 side
= (drflac_uint32
)pInputSamples1
[i
] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10820 mid
= (mid
<< 1) | (side
& 0x01);
10822 pOutputSamples
[i
*2+0] = (float)((((drflac_int32
)(mid
+ side
) >> 1) << (unusedBitsPerSample
)) / 2147483648.0);
10823 pOutputSamples
[i
*2+1] = (float)((((drflac_int32
)(mid
- side
) >> 1) << (unusedBitsPerSample
)) / 2147483648.0);
10828 static DRFLAC_INLINE
void drflac_read_pcm_frames_f32__decode_mid_side__scalar(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, float* pOutputSamples
)
10831 drflac_uint64 frameCount4
= frameCount
>> 2;
10832 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
10833 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
10834 drflac_uint32 shift
= unusedBitsPerSample
;
10835 float factor
= 1 / 2147483648.0;
10839 for (i
= 0; i
< frameCount4
; ++i
) {
10840 drflac_uint32 temp0L
;
10841 drflac_uint32 temp1L
;
10842 drflac_uint32 temp2L
;
10843 drflac_uint32 temp3L
;
10844 drflac_uint32 temp0R
;
10845 drflac_uint32 temp1R
;
10846 drflac_uint32 temp2R
;
10847 drflac_uint32 temp3R
;
10849 drflac_uint32 mid0
= pInputSamples0U32
[i
*4+0] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10850 drflac_uint32 mid1
= pInputSamples0U32
[i
*4+1] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10851 drflac_uint32 mid2
= pInputSamples0U32
[i
*4+2] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10852 drflac_uint32 mid3
= pInputSamples0U32
[i
*4+3] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10854 drflac_uint32 side0
= pInputSamples1U32
[i
*4+0] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10855 drflac_uint32 side1
= pInputSamples1U32
[i
*4+1] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10856 drflac_uint32 side2
= pInputSamples1U32
[i
*4+2] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10857 drflac_uint32 side3
= pInputSamples1U32
[i
*4+3] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10859 mid0
= (mid0
<< 1) | (side0
& 0x01);
10860 mid1
= (mid1
<< 1) | (side1
& 0x01);
10861 mid2
= (mid2
<< 1) | (side2
& 0x01);
10862 mid3
= (mid3
<< 1) | (side3
& 0x01);
10864 temp0L
= (mid0
+ side0
) << shift
;
10865 temp1L
= (mid1
+ side1
) << shift
;
10866 temp2L
= (mid2
+ side2
) << shift
;
10867 temp3L
= (mid3
+ side3
) << shift
;
10869 temp0R
= (mid0
- side0
) << shift
;
10870 temp1R
= (mid1
- side1
) << shift
;
10871 temp2R
= (mid2
- side2
) << shift
;
10872 temp3R
= (mid3
- side3
) << shift
;
10874 pOutputSamples
[i
*8+0] = (drflac_int32
)temp0L
* factor
;
10875 pOutputSamples
[i
*8+1] = (drflac_int32
)temp0R
* factor
;
10876 pOutputSamples
[i
*8+2] = (drflac_int32
)temp1L
* factor
;
10877 pOutputSamples
[i
*8+3] = (drflac_int32
)temp1R
* factor
;
10878 pOutputSamples
[i
*8+4] = (drflac_int32
)temp2L
* factor
;
10879 pOutputSamples
[i
*8+5] = (drflac_int32
)temp2R
* factor
;
10880 pOutputSamples
[i
*8+6] = (drflac_int32
)temp3L
* factor
;
10881 pOutputSamples
[i
*8+7] = (drflac_int32
)temp3R
* factor
;
10884 for (i
= 0; i
< frameCount4
; ++i
) {
10885 drflac_uint32 temp0L
;
10886 drflac_uint32 temp1L
;
10887 drflac_uint32 temp2L
;
10888 drflac_uint32 temp3L
;
10889 drflac_uint32 temp0R
;
10890 drflac_uint32 temp1R
;
10891 drflac_uint32 temp2R
;
10892 drflac_uint32 temp3R
;
10894 drflac_uint32 mid0
= pInputSamples0U32
[i
*4+0] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10895 drflac_uint32 mid1
= pInputSamples0U32
[i
*4+1] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10896 drflac_uint32 mid2
= pInputSamples0U32
[i
*4+2] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10897 drflac_uint32 mid3
= pInputSamples0U32
[i
*4+3] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10899 drflac_uint32 side0
= pInputSamples1U32
[i
*4+0] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10900 drflac_uint32 side1
= pInputSamples1U32
[i
*4+1] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10901 drflac_uint32 side2
= pInputSamples1U32
[i
*4+2] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10902 drflac_uint32 side3
= pInputSamples1U32
[i
*4+3] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10904 mid0
= (mid0
<< 1) | (side0
& 0x01);
10905 mid1
= (mid1
<< 1) | (side1
& 0x01);
10906 mid2
= (mid2
<< 1) | (side2
& 0x01);
10907 mid3
= (mid3
<< 1) | (side3
& 0x01);
10909 temp0L
= (drflac_uint32
)((drflac_int32
)(mid0
+ side0
) >> 1);
10910 temp1L
= (drflac_uint32
)((drflac_int32
)(mid1
+ side1
) >> 1);
10911 temp2L
= (drflac_uint32
)((drflac_int32
)(mid2
+ side2
) >> 1);
10912 temp3L
= (drflac_uint32
)((drflac_int32
)(mid3
+ side3
) >> 1);
10914 temp0R
= (drflac_uint32
)((drflac_int32
)(mid0
- side0
) >> 1);
10915 temp1R
= (drflac_uint32
)((drflac_int32
)(mid1
- side1
) >> 1);
10916 temp2R
= (drflac_uint32
)((drflac_int32
)(mid2
- side2
) >> 1);
10917 temp3R
= (drflac_uint32
)((drflac_int32
)(mid3
- side3
) >> 1);
10919 pOutputSamples
[i
*8+0] = (drflac_int32
)temp0L
* factor
;
10920 pOutputSamples
[i
*8+1] = (drflac_int32
)temp0R
* factor
;
10921 pOutputSamples
[i
*8+2] = (drflac_int32
)temp1L
* factor
;
10922 pOutputSamples
[i
*8+3] = (drflac_int32
)temp1R
* factor
;
10923 pOutputSamples
[i
*8+4] = (drflac_int32
)temp2L
* factor
;
10924 pOutputSamples
[i
*8+5] = (drflac_int32
)temp2R
* factor
;
10925 pOutputSamples
[i
*8+6] = (drflac_int32
)temp3L
* factor
;
10926 pOutputSamples
[i
*8+7] = (drflac_int32
)temp3R
* factor
;
10930 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
10931 drflac_uint32 mid
= pInputSamples0U32
[i
] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10932 drflac_uint32 side
= pInputSamples1U32
[i
] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10934 mid
= (mid
<< 1) | (side
& 0x01);
10936 pOutputSamples
[i
*2+0] = (drflac_int32
)((drflac_uint32
)((drflac_int32
)(mid
+ side
) >> 1) << unusedBitsPerSample
) * factor
;
10937 pOutputSamples
[i
*2+1] = (drflac_int32
)((drflac_uint32
)((drflac_int32
)(mid
- side
) >> 1) << unusedBitsPerSample
) * factor
;
10941 #if defined(DRFLAC_SUPPORT_SSE2)
10942 static DRFLAC_INLINE
void drflac_read_pcm_frames_f32__decode_mid_side__sse2(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, float* pOutputSamples
)
10945 drflac_uint64 frameCount4
= frameCount
>> 2;
10946 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
10947 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
10948 drflac_uint32 shift
= unusedBitsPerSample
- 8;
10952 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 24);
10954 factor
= 1.0f
/ 8388608.0f
;
10955 factor128
= _mm_set1_ps(factor
);
10958 for (i
= 0; i
< frameCount4
; ++i
) {
10966 mid
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples0
+ i
), pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
);
10967 side
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples1
+ i
), pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
);
10969 mid
= _mm_or_si128(_mm_slli_epi32(mid
, 1), _mm_and_si128(side
, _mm_set1_epi32(0x01)));
10971 tempL
= _mm_srai_epi32(_mm_add_epi32(mid
, side
), 1);
10972 tempR
= _mm_srai_epi32(_mm_sub_epi32(mid
, side
), 1);
10974 leftf
= _mm_mul_ps(_mm_cvtepi32_ps(tempL
), factor128
);
10975 rightf
= _mm_mul_ps(_mm_cvtepi32_ps(tempR
), factor128
);
10977 _mm_storeu_ps(pOutputSamples
+ i
*8 + 0, _mm_unpacklo_ps(leftf
, rightf
));
10978 _mm_storeu_ps(pOutputSamples
+ i
*8 + 4, _mm_unpackhi_ps(leftf
, rightf
));
10981 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
10982 drflac_uint32 mid
= pInputSamples0U32
[i
] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
10983 drflac_uint32 side
= pInputSamples1U32
[i
] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
10985 mid
= (mid
<< 1) | (side
& 0x01);
10987 pOutputSamples
[i
*2+0] = ((drflac_int32
)(mid
+ side
) >> 1) * factor
;
10988 pOutputSamples
[i
*2+1] = ((drflac_int32
)(mid
- side
) >> 1) * factor
;
10992 for (i
= 0; i
< frameCount4
; ++i
) {
11000 mid
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples0
+ i
), pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
);
11001 side
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples1
+ i
), pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
);
11003 mid
= _mm_or_si128(_mm_slli_epi32(mid
, 1), _mm_and_si128(side
, _mm_set1_epi32(0x01)));
11005 tempL
= _mm_slli_epi32(_mm_add_epi32(mid
, side
), shift
);
11006 tempR
= _mm_slli_epi32(_mm_sub_epi32(mid
, side
), shift
);
11008 leftf
= _mm_mul_ps(_mm_cvtepi32_ps(tempL
), factor128
);
11009 rightf
= _mm_mul_ps(_mm_cvtepi32_ps(tempR
), factor128
);
11011 _mm_storeu_ps(pOutputSamples
+ i
*8 + 0, _mm_unpacklo_ps(leftf
, rightf
));
11012 _mm_storeu_ps(pOutputSamples
+ i
*8 + 4, _mm_unpackhi_ps(leftf
, rightf
));
11015 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
11016 drflac_uint32 mid
= pInputSamples0U32
[i
] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
11017 drflac_uint32 side
= pInputSamples1U32
[i
] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
11019 mid
= (mid
<< 1) | (side
& 0x01);
11021 pOutputSamples
[i
*2+0] = (drflac_int32
)((mid
+ side
) << shift
) * factor
;
11022 pOutputSamples
[i
*2+1] = (drflac_int32
)((mid
- side
) << shift
) * factor
;
11028 #if defined(DRFLAC_SUPPORT_NEON)
11029 static DRFLAC_INLINE
void drflac_read_pcm_frames_f32__decode_mid_side__neon(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, float* pOutputSamples
)
11032 drflac_uint64 frameCount4
= frameCount
>> 2;
11033 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
11034 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
11035 drflac_uint32 shift
= unusedBitsPerSample
- 8;
11037 float32x4_t factor4
;
11039 int32x4_t wbps0_4
; /* Wasted Bits Per Sample */
11040 int32x4_t wbps1_4
; /* Wasted Bits Per Sample */
11042 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 24);
11044 factor
= 1.0f
/ 8388608.0f
;
11045 factor4
= vdupq_n_f32(factor
);
11046 wbps0_4
= vdupq_n_s32(pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
);
11047 wbps1_4
= vdupq_n_s32(pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
);
11050 for (i
= 0; i
< frameCount4
; ++i
) {
11054 float32x4_t rightf
;
11056 uint32x4_t mid
= vshlq_u32(vld1q_u32(pInputSamples0U32
+ i
*4), wbps0_4
);
11057 uint32x4_t side
= vshlq_u32(vld1q_u32(pInputSamples1U32
+ i
*4), wbps1_4
);
11059 mid
= vorrq_u32(vshlq_n_u32(mid
, 1), vandq_u32(side
, vdupq_n_u32(1)));
11061 lefti
= vshrq_n_s32(vreinterpretq_s32_u32(vaddq_u32(mid
, side
)), 1);
11062 righti
= vshrq_n_s32(vreinterpretq_s32_u32(vsubq_u32(mid
, side
)), 1);
11064 leftf
= vmulq_f32(vcvtq_f32_s32(lefti
), factor4
);
11065 rightf
= vmulq_f32(vcvtq_f32_s32(righti
), factor4
);
11067 drflac__vst2q_f32(pOutputSamples
+ i
*8, vzipq_f32(leftf
, rightf
));
11070 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
11071 drflac_uint32 mid
= pInputSamples0U32
[i
] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
11072 drflac_uint32 side
= pInputSamples1U32
[i
] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
11074 mid
= (mid
<< 1) | (side
& 0x01);
11076 pOutputSamples
[i
*2+0] = ((drflac_int32
)(mid
+ side
) >> 1) * factor
;
11077 pOutputSamples
[i
*2+1] = ((drflac_int32
)(mid
- side
) >> 1) * factor
;
11081 shift4
= vdupq_n_s32(shift
);
11082 for (i
= 0; i
< frameCount4
; ++i
) {
11088 float32x4_t rightf
;
11090 mid
= vshlq_u32(vld1q_u32(pInputSamples0U32
+ i
*4), wbps0_4
);
11091 side
= vshlq_u32(vld1q_u32(pInputSamples1U32
+ i
*4), wbps1_4
);
11093 mid
= vorrq_u32(vshlq_n_u32(mid
, 1), vandq_u32(side
, vdupq_n_u32(1)));
11095 lefti
= vreinterpretq_s32_u32(vshlq_u32(vaddq_u32(mid
, side
), shift4
));
11096 righti
= vreinterpretq_s32_u32(vshlq_u32(vsubq_u32(mid
, side
), shift4
));
11098 leftf
= vmulq_f32(vcvtq_f32_s32(lefti
), factor4
);
11099 rightf
= vmulq_f32(vcvtq_f32_s32(righti
), factor4
);
11101 drflac__vst2q_f32(pOutputSamples
+ i
*8, vzipq_f32(leftf
, rightf
));
11104 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
11105 drflac_uint32 mid
= pInputSamples0U32
[i
] << pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
11106 drflac_uint32 side
= pInputSamples1U32
[i
] << pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
11108 mid
= (mid
<< 1) | (side
& 0x01);
11110 pOutputSamples
[i
*2+0] = (drflac_int32
)((mid
+ side
) << shift
) * factor
;
11111 pOutputSamples
[i
*2+1] = (drflac_int32
)((mid
- side
) << shift
) * factor
;
11117 static DRFLAC_INLINE
void drflac_read_pcm_frames_f32__decode_mid_side(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, float* pOutputSamples
)
11119 #if defined(DRFLAC_SUPPORT_SSE2)
11120 if (drflac__gIsSSE2Supported
&& pFlac
->bitsPerSample
<= 24) {
11121 drflac_read_pcm_frames_f32__decode_mid_side__sse2(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
11123 #elif defined(DRFLAC_SUPPORT_NEON)
11124 if (drflac__gIsNEONSupported
&& pFlac
->bitsPerSample
<= 24) {
11125 drflac_read_pcm_frames_f32__decode_mid_side__neon(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
11129 /* Scalar fallback. */
11131 drflac_read_pcm_frames_f32__decode_mid_side__reference(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
11133 drflac_read_pcm_frames_f32__decode_mid_side__scalar(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
11139 static DRFLAC_INLINE
void drflac_read_pcm_frames_f32__decode_independent_stereo__reference(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, float* pOutputSamples
)
11141 for (drflac_uint64 i
= 0; i
< frameCount
; ++i
) {
11142 pOutputSamples
[i
*2+0] = (float)((drflac_int32
)((drflac_uint32
)pInputSamples0
[i
] << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
)) / 2147483648.0);
11143 pOutputSamples
[i
*2+1] = (float)((drflac_int32
)((drflac_uint32
)pInputSamples1
[i
] << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
)) / 2147483648.0);
11148 static DRFLAC_INLINE
void drflac_read_pcm_frames_f32__decode_independent_stereo__scalar(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, float* pOutputSamples
)
11151 drflac_uint64 frameCount4
= frameCount
>> 2;
11152 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
11153 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
11154 drflac_uint32 shift0
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
;
11155 drflac_uint32 shift1
= unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
;
11156 float factor
= 1 / 2147483648.0;
11158 for (i
= 0; i
< frameCount4
; ++i
) {
11159 drflac_uint32 tempL0
= pInputSamples0U32
[i
*4+0] << shift0
;
11160 drflac_uint32 tempL1
= pInputSamples0U32
[i
*4+1] << shift0
;
11161 drflac_uint32 tempL2
= pInputSamples0U32
[i
*4+2] << shift0
;
11162 drflac_uint32 tempL3
= pInputSamples0U32
[i
*4+3] << shift0
;
11164 drflac_uint32 tempR0
= pInputSamples1U32
[i
*4+0] << shift1
;
11165 drflac_uint32 tempR1
= pInputSamples1U32
[i
*4+1] << shift1
;
11166 drflac_uint32 tempR2
= pInputSamples1U32
[i
*4+2] << shift1
;
11167 drflac_uint32 tempR3
= pInputSamples1U32
[i
*4+3] << shift1
;
11169 pOutputSamples
[i
*8+0] = (drflac_int32
)tempL0
* factor
;
11170 pOutputSamples
[i
*8+1] = (drflac_int32
)tempR0
* factor
;
11171 pOutputSamples
[i
*8+2] = (drflac_int32
)tempL1
* factor
;
11172 pOutputSamples
[i
*8+3] = (drflac_int32
)tempR1
* factor
;
11173 pOutputSamples
[i
*8+4] = (drflac_int32
)tempL2
* factor
;
11174 pOutputSamples
[i
*8+5] = (drflac_int32
)tempR2
* factor
;
11175 pOutputSamples
[i
*8+6] = (drflac_int32
)tempL3
* factor
;
11176 pOutputSamples
[i
*8+7] = (drflac_int32
)tempR3
* factor
;
11179 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
11180 pOutputSamples
[i
*2+0] = (drflac_int32
)(pInputSamples0U32
[i
] << shift0
) * factor
;
11181 pOutputSamples
[i
*2+1] = (drflac_int32
)(pInputSamples1U32
[i
] << shift1
) * factor
;
11185 #if defined(DRFLAC_SUPPORT_SSE2)
11186 static DRFLAC_INLINE
void drflac_read_pcm_frames_f32__decode_independent_stereo__sse2(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, float* pOutputSamples
)
11189 drflac_uint64 frameCount4
= frameCount
>> 2;
11190 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
11191 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
11192 drflac_uint32 shift0
= (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
) - 8;
11193 drflac_uint32 shift1
= (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
) - 8;
11195 float factor
= 1.0f
/ 8388608.0f
;
11196 __m128 factor128
= _mm_set1_ps(factor
);
11198 for (i
= 0; i
< frameCount4
; ++i
) {
11204 lefti
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples0
+ i
), shift0
);
11205 righti
= _mm_slli_epi32(_mm_loadu_si128((const __m128i
*)pInputSamples1
+ i
), shift1
);
11207 leftf
= _mm_mul_ps(_mm_cvtepi32_ps(lefti
), factor128
);
11208 rightf
= _mm_mul_ps(_mm_cvtepi32_ps(righti
), factor128
);
11210 _mm_storeu_ps(pOutputSamples
+ i
*8 + 0, _mm_unpacklo_ps(leftf
, rightf
));
11211 _mm_storeu_ps(pOutputSamples
+ i
*8 + 4, _mm_unpackhi_ps(leftf
, rightf
));
11214 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
11215 pOutputSamples
[i
*2+0] = (drflac_int32
)(pInputSamples0U32
[i
] << shift0
) * factor
;
11216 pOutputSamples
[i
*2+1] = (drflac_int32
)(pInputSamples1U32
[i
] << shift1
) * factor
;
11221 #if defined(DRFLAC_SUPPORT_NEON)
11222 static DRFLAC_INLINE
void drflac_read_pcm_frames_f32__decode_independent_stereo__neon(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, float* pOutputSamples
)
11225 drflac_uint64 frameCount4
= frameCount
>> 2;
11226 const drflac_uint32
* pInputSamples0U32
= (const drflac_uint32
*)pInputSamples0
;
11227 const drflac_uint32
* pInputSamples1U32
= (const drflac_uint32
*)pInputSamples1
;
11228 drflac_uint32 shift0
= (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[0].wastedBitsPerSample
) - 8;
11229 drflac_uint32 shift1
= (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[1].wastedBitsPerSample
) - 8;
11231 float factor
= 1.0f
/ 8388608.0f
;
11232 float32x4_t factor4
= vdupq_n_f32(factor
);
11233 int32x4_t shift0_4
= vdupq_n_s32(shift0
);
11234 int32x4_t shift1_4
= vdupq_n_s32(shift1
);
11236 for (i
= 0; i
< frameCount4
; ++i
) {
11240 float32x4_t rightf
;
11242 lefti
= vreinterpretq_s32_u32(vshlq_u32(vld1q_u32(pInputSamples0U32
+ i
*4), shift0_4
));
11243 righti
= vreinterpretq_s32_u32(vshlq_u32(vld1q_u32(pInputSamples1U32
+ i
*4), shift1_4
));
11245 leftf
= vmulq_f32(vcvtq_f32_s32(lefti
), factor4
);
11246 rightf
= vmulq_f32(vcvtq_f32_s32(righti
), factor4
);
11248 drflac__vst2q_f32(pOutputSamples
+ i
*8, vzipq_f32(leftf
, rightf
));
11251 for (i
= (frameCount4
<< 2); i
< frameCount
; ++i
) {
11252 pOutputSamples
[i
*2+0] = (drflac_int32
)(pInputSamples0U32
[i
] << shift0
) * factor
;
11253 pOutputSamples
[i
*2+1] = (drflac_int32
)(pInputSamples1U32
[i
] << shift1
) * factor
;
11258 static DRFLAC_INLINE
void drflac_read_pcm_frames_f32__decode_independent_stereo(drflac
* pFlac
, drflac_uint64 frameCount
, drflac_uint32 unusedBitsPerSample
, const drflac_int32
* pInputSamples0
, const drflac_int32
* pInputSamples1
, float* pOutputSamples
)
11260 #if defined(DRFLAC_SUPPORT_SSE2)
11261 if (drflac__gIsSSE2Supported
&& pFlac
->bitsPerSample
<= 24) {
11262 drflac_read_pcm_frames_f32__decode_independent_stereo__sse2(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
11264 #elif defined(DRFLAC_SUPPORT_NEON)
11265 if (drflac__gIsNEONSupported
&& pFlac
->bitsPerSample
<= 24) {
11266 drflac_read_pcm_frames_f32__decode_independent_stereo__neon(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
11270 /* Scalar fallback. */
11272 drflac_read_pcm_frames_f32__decode_independent_stereo__reference(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
11274 drflac_read_pcm_frames_f32__decode_independent_stereo__scalar(pFlac
, frameCount
, unusedBitsPerSample
, pInputSamples0
, pInputSamples1
, pOutputSamples
);
11279 DRFLAC_API drflac_uint64
drflac_read_pcm_frames_f32(drflac
* pFlac
, drflac_uint64 framesToRead
, float* pBufferOut
)
11281 drflac_uint64 framesRead
;
11282 drflac_uint32 unusedBitsPerSample
;
11284 if (pFlac
== NULL
|| framesToRead
== 0) {
11288 if (pBufferOut
== NULL
) {
11289 return drflac__seek_forward_by_pcm_frames(pFlac
, framesToRead
);
11292 DRFLAC_ASSERT(pFlac
->bitsPerSample
<= 32);
11293 unusedBitsPerSample
= 32 - pFlac
->bitsPerSample
;
11296 while (framesToRead
> 0) {
11297 /* If we've run out of samples in this frame, go to the next. */
11298 if (pFlac
->currentFLACFrame
.pcmFramesRemaining
== 0) {
11299 if (!drflac__read_and_decode_next_flac_frame(pFlac
)) {
11300 break; /* Couldn't read the next frame, so just break from the loop and return. */
11303 unsigned int channelCount
= drflac__get_channel_count_from_channel_assignment(pFlac
->currentFLACFrame
.header
.channelAssignment
);
11304 drflac_uint64 iFirstPCMFrame
= pFlac
->currentFLACFrame
.header
.blockSizeInPCMFrames
- pFlac
->currentFLACFrame
.pcmFramesRemaining
;
11305 drflac_uint64 frameCountThisIteration
= framesToRead
;
11307 if (frameCountThisIteration
> pFlac
->currentFLACFrame
.pcmFramesRemaining
) {
11308 frameCountThisIteration
= pFlac
->currentFLACFrame
.pcmFramesRemaining
;
11311 if (channelCount
== 2) {
11312 const drflac_int32
* pDecodedSamples0
= pFlac
->currentFLACFrame
.subframes
[0].pSamplesS32
+ iFirstPCMFrame
;
11313 const drflac_int32
* pDecodedSamples1
= pFlac
->currentFLACFrame
.subframes
[1].pSamplesS32
+ iFirstPCMFrame
;
11315 switch (pFlac
->currentFLACFrame
.header
.channelAssignment
)
11317 case DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE
:
11319 drflac_read_pcm_frames_f32__decode_left_side(pFlac
, frameCountThisIteration
, unusedBitsPerSample
, pDecodedSamples0
, pDecodedSamples1
, pBufferOut
);
11322 case DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE
:
11324 drflac_read_pcm_frames_f32__decode_right_side(pFlac
, frameCountThisIteration
, unusedBitsPerSample
, pDecodedSamples0
, pDecodedSamples1
, pBufferOut
);
11327 case DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE
:
11329 drflac_read_pcm_frames_f32__decode_mid_side(pFlac
, frameCountThisIteration
, unusedBitsPerSample
, pDecodedSamples0
, pDecodedSamples1
, pBufferOut
);
11332 case DRFLAC_CHANNEL_ASSIGNMENT_INDEPENDENT
:
11335 drflac_read_pcm_frames_f32__decode_independent_stereo(pFlac
, frameCountThisIteration
, unusedBitsPerSample
, pDecodedSamples0
, pDecodedSamples1
, pBufferOut
);
11339 /* Generic interleaving. */
11341 for (i
= 0; i
< frameCountThisIteration
; ++i
) {
11343 for (j
= 0; j
< channelCount
; ++j
) {
11344 drflac_int32 sampleS32
= (drflac_int32
)((drflac_uint32
)(pFlac
->currentFLACFrame
.subframes
[j
].pSamplesS32
[iFirstPCMFrame
+ i
]) << (unusedBitsPerSample
+ pFlac
->currentFLACFrame
.subframes
[j
].wastedBitsPerSample
));
11345 pBufferOut
[(i
*channelCount
)+j
] = (float)(sampleS32
/ 2147483648.0);
11350 framesRead
+= frameCountThisIteration
;
11351 pBufferOut
+= frameCountThisIteration
* channelCount
;
11352 framesToRead
-= frameCountThisIteration
;
11353 pFlac
->currentPCMFrame
+= frameCountThisIteration
;
11354 pFlac
->currentFLACFrame
.pcmFramesRemaining
-= (unsigned int)frameCountThisIteration
;
11362 DRFLAC_API drflac_bool32
drflac_seek_to_pcm_frame(drflac
* pFlac
, drflac_uint64 pcmFrameIndex
)
11364 if (pFlac
== NULL
) {
11365 return DRFLAC_FALSE
;
11368 /* Don't do anything if we're already on the seek point. */
11369 if (pFlac
->currentPCMFrame
== pcmFrameIndex
) {
11370 return DRFLAC_TRUE
;
11374 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
11375 when the decoder was opened.
11377 if (pFlac
->firstFLACFramePosInBytes
== 0) {
11378 return DRFLAC_FALSE
;
11381 if (pcmFrameIndex
== 0) {
11382 pFlac
->currentPCMFrame
= 0;
11383 return drflac__seek_to_first_frame(pFlac
);
11385 drflac_bool32 wasSuccessful
= DRFLAC_FALSE
;
11386 drflac_uint64 originalPCMFrame
= pFlac
->currentPCMFrame
;
11388 /* Clamp the sample to the end. */
11389 if (pcmFrameIndex
> pFlac
->totalPCMFrameCount
) {
11390 pcmFrameIndex
= pFlac
->totalPCMFrameCount
;
11393 /* If the target sample and the current sample are in the same frame we just move the position forward. */
11394 if (pcmFrameIndex
> pFlac
->currentPCMFrame
) {
11396 drflac_uint32 offset
= (drflac_uint32
)(pcmFrameIndex
- pFlac
->currentPCMFrame
);
11397 if (pFlac
->currentFLACFrame
.pcmFramesRemaining
> offset
) {
11398 pFlac
->currentFLACFrame
.pcmFramesRemaining
-= offset
;
11399 pFlac
->currentPCMFrame
= pcmFrameIndex
;
11400 return DRFLAC_TRUE
;
11404 drflac_uint32 offsetAbs
= (drflac_uint32
)(pFlac
->currentPCMFrame
- pcmFrameIndex
);
11405 drflac_uint32 currentFLACFramePCMFrameCount
= pFlac
->currentFLACFrame
.header
.blockSizeInPCMFrames
;
11406 drflac_uint32 currentFLACFramePCMFramesConsumed
= currentFLACFramePCMFrameCount
- pFlac
->currentFLACFrame
.pcmFramesRemaining
;
11407 if (currentFLACFramePCMFramesConsumed
> offsetAbs
) {
11408 pFlac
->currentFLACFrame
.pcmFramesRemaining
+= offsetAbs
;
11409 pFlac
->currentPCMFrame
= pcmFrameIndex
;
11410 return DRFLAC_TRUE
;
11415 Different techniques depending on encapsulation. Using the native FLAC seektable with Ogg encapsulation is a bit awkward so
11416 we'll instead use Ogg's natural seeking facility.
11418 #ifndef DR_FLAC_NO_OGG
11419 if (pFlac
->container
== drflac_container_ogg
)
11421 wasSuccessful
= drflac_ogg__seek_to_pcm_frame(pFlac
, pcmFrameIndex
);
11426 /* First try seeking via the seek table. If this fails, fall back to a brute force seek which is much slower. */
11427 if (/*!wasSuccessful && */!pFlac
->_noSeekTableSeek
) {
11428 wasSuccessful
= drflac__seek_to_pcm_frame__seek_table(pFlac
, pcmFrameIndex
);
11431 #if !defined(DR_FLAC_NO_CRC)
11432 /* Fall back to binary search if seek table seeking fails. This requires the length of the stream to be known. */
11433 if (!wasSuccessful
&& !pFlac
->_noBinarySearchSeek
&& pFlac
->totalPCMFrameCount
> 0) {
11434 wasSuccessful
= drflac__seek_to_pcm_frame__binary_search(pFlac
, pcmFrameIndex
);
11438 /* Fall back to brute force if all else fails. */
11439 if (!wasSuccessful
&& !pFlac
->_noBruteForceSeek
) {
11440 wasSuccessful
= drflac__seek_to_pcm_frame__brute_force(pFlac
, pcmFrameIndex
);
11444 if (wasSuccessful
) {
11445 pFlac
->currentPCMFrame
= pcmFrameIndex
;
11447 /* Seek failed. Try putting the decoder back to it's original state. */
11448 if (drflac_seek_to_pcm_frame(pFlac
, originalPCMFrame
) == DRFLAC_FALSE
) {
11449 /* Failed to seek back to the original PCM frame. Fall back to 0. */
11450 drflac_seek_to_pcm_frame(pFlac
, 0);
11454 return wasSuccessful
;
11460 /* High Level APIs */
11462 #if defined(SIZE_MAX)
11463 #define DRFLAC_SIZE_MAX SIZE_MAX
11465 #if defined(DRFLAC_64BIT)
11466 #define DRFLAC_SIZE_MAX ((drflac_uint64)0xFFFFFFFFFFFFFFFF)
11468 #define DRFLAC_SIZE_MAX 0xFFFFFFFF
11473 /* Using a macro as the definition of the drflac__full_decode_and_close_*() API family. Sue me. */
11474 #define DRFLAC_DEFINE_FULL_READ_AND_CLOSE(extension, type) \
11475 static type* drflac__full_read_and_close_ ## extension (drflac* pFlac, unsigned int* channelsOut, unsigned int* sampleRateOut, drflac_uint64* totalPCMFrameCountOut)\
11477 type* pSampleData = NULL; \
11478 drflac_uint64 totalPCMFrameCount; \
11480 DRFLAC_ASSERT(pFlac != NULL); \
11482 totalPCMFrameCount = pFlac->totalPCMFrameCount; \
11484 if (totalPCMFrameCount == 0) { \
11485 type buffer[4096]; \
11486 drflac_uint64 pcmFramesRead; \
11487 size_t sampleDataBufferSize = sizeof(buffer); \
11489 pSampleData = (type*)drflac__malloc_from_callbacks(sampleDataBufferSize, &pFlac->allocationCallbacks); \
11490 if (pSampleData == NULL) { \
11494 while ((pcmFramesRead = (drflac_uint64)drflac_read_pcm_frames_##extension(pFlac, sizeof(buffer)/sizeof(buffer[0])/pFlac->channels, buffer)) > 0) { \
11495 if (((totalPCMFrameCount + pcmFramesRead) * pFlac->channels * sizeof(type)) > sampleDataBufferSize) { \
11496 type* pNewSampleData; \
11497 size_t newSampleDataBufferSize; \
11499 newSampleDataBufferSize = sampleDataBufferSize * 2; \
11500 pNewSampleData = (type*)drflac__realloc_from_callbacks(pSampleData, newSampleDataBufferSize, sampleDataBufferSize, &pFlac->allocationCallbacks); \
11501 if (pNewSampleData == NULL) { \
11502 drflac__free_from_callbacks(pSampleData, &pFlac->allocationCallbacks); \
11506 sampleDataBufferSize = newSampleDataBufferSize; \
11507 pSampleData = pNewSampleData; \
11510 DRFLAC_COPY_MEMORY(pSampleData + (totalPCMFrameCount*pFlac->channels), buffer, (size_t)(pcmFramesRead*pFlac->channels*sizeof(type))); \
11511 totalPCMFrameCount += pcmFramesRead; \
11514 /* At this point everything should be decoded, but we just want to fill the unused part buffer with silence - need to \
11515 protect those ears from random noise! */ \
11516 DRFLAC_ZERO_MEMORY(pSampleData + (totalPCMFrameCount*pFlac->channels), (size_t)(sampleDataBufferSize - totalPCMFrameCount*pFlac->channels*sizeof(type))); \
11518 drflac_uint64 dataSize = totalPCMFrameCount*pFlac->channels*sizeof(type); \
11519 if (dataSize > (drflac_uint64)DRFLAC_SIZE_MAX) { \
11520 goto on_error; /* The decoded data is too big. */ \
11523 pSampleData = (type*)drflac__malloc_from_callbacks((size_t)dataSize, &pFlac->allocationCallbacks); /* <-- Safe cast as per the check above. */ \
11524 if (pSampleData == NULL) { \
11528 totalPCMFrameCount = drflac_read_pcm_frames_##extension(pFlac, pFlac->totalPCMFrameCount, pSampleData); \
11531 if (sampleRateOut) *sampleRateOut = pFlac->sampleRate; \
11532 if (channelsOut) *channelsOut = pFlac->channels; \
11533 if (totalPCMFrameCountOut) *totalPCMFrameCountOut = totalPCMFrameCount; \
11535 drflac_close(pFlac); \
11536 return pSampleData; \
11539 drflac_close(pFlac); \
11543 DRFLAC_DEFINE_FULL_READ_AND_CLOSE(s32
, drflac_int32
)
11544 DRFLAC_DEFINE_FULL_READ_AND_CLOSE(s16
, drflac_int16
)
11545 DRFLAC_DEFINE_FULL_READ_AND_CLOSE(f32
, float)
11547 DRFLAC_API 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
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
11554 if (sampleRateOut
) {
11555 *sampleRateOut
= 0;
11557 if (totalPCMFrameCountOut
) {
11558 *totalPCMFrameCountOut
= 0;
11561 pFlac
= drflac_open(onRead
, onSeek
, pUserData
, pAllocationCallbacks
);
11562 if (pFlac
== NULL
) {
11566 return drflac__full_read_and_close_s32(pFlac
, channelsOut
, sampleRateOut
, totalPCMFrameCountOut
);
11569 DRFLAC_API 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
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
11576 if (sampleRateOut
) {
11577 *sampleRateOut
= 0;
11579 if (totalPCMFrameCountOut
) {
11580 *totalPCMFrameCountOut
= 0;
11583 pFlac
= drflac_open(onRead
, onSeek
, pUserData
, pAllocationCallbacks
);
11584 if (pFlac
== NULL
) {
11588 return drflac__full_read_and_close_s16(pFlac
, channelsOut
, sampleRateOut
, totalPCMFrameCountOut
);
11591 DRFLAC_API
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
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
11598 if (sampleRateOut
) {
11599 *sampleRateOut
= 0;
11601 if (totalPCMFrameCountOut
) {
11602 *totalPCMFrameCountOut
= 0;
11605 pFlac
= drflac_open(onRead
, onSeek
, pUserData
, pAllocationCallbacks
);
11606 if (pFlac
== NULL
) {
11610 return drflac__full_read_and_close_f32(pFlac
, channelsOut
, sampleRateOut
, totalPCMFrameCountOut
);
11613 #ifndef DR_FLAC_NO_STDIO
11614 DRFLAC_API drflac_int32
* drflac_open_file_and_read_pcm_frames_s32(const char* filename
, unsigned int* channels
, unsigned int* sampleRate
, drflac_uint64
* totalPCMFrameCount
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
11624 if (totalPCMFrameCount
) {
11625 *totalPCMFrameCount
= 0;
11628 pFlac
= drflac_open_file(filename
, pAllocationCallbacks
);
11629 if (pFlac
== NULL
) {
11633 return drflac__full_read_and_close_s32(pFlac
, channels
, sampleRate
, totalPCMFrameCount
);
11636 DRFLAC_API drflac_int16
* drflac_open_file_and_read_pcm_frames_s16(const char* filename
, unsigned int* channels
, unsigned int* sampleRate
, drflac_uint64
* totalPCMFrameCount
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
11646 if (totalPCMFrameCount
) {
11647 *totalPCMFrameCount
= 0;
11650 pFlac
= drflac_open_file(filename
, pAllocationCallbacks
);
11651 if (pFlac
== NULL
) {
11655 return drflac__full_read_and_close_s16(pFlac
, channels
, sampleRate
, totalPCMFrameCount
);
11658 DRFLAC_API
float* drflac_open_file_and_read_pcm_frames_f32(const char* filename
, unsigned int* channels
, unsigned int* sampleRate
, drflac_uint64
* totalPCMFrameCount
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
11668 if (totalPCMFrameCount
) {
11669 *totalPCMFrameCount
= 0;
11672 pFlac
= drflac_open_file(filename
, pAllocationCallbacks
);
11673 if (pFlac
== NULL
) {
11677 return drflac__full_read_and_close_f32(pFlac
, channels
, sampleRate
, totalPCMFrameCount
);
11681 DRFLAC_API drflac_int32
* drflac_open_memory_and_read_pcm_frames_s32(const void* data
, size_t dataSize
, unsigned int* channels
, unsigned int* sampleRate
, drflac_uint64
* totalPCMFrameCount
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
11691 if (totalPCMFrameCount
) {
11692 *totalPCMFrameCount
= 0;
11695 pFlac
= drflac_open_memory(data
, dataSize
, pAllocationCallbacks
);
11696 if (pFlac
== NULL
) {
11700 return drflac__full_read_and_close_s32(pFlac
, channels
, sampleRate
, totalPCMFrameCount
);
11703 DRFLAC_API drflac_int16
* drflac_open_memory_and_read_pcm_frames_s16(const void* data
, size_t dataSize
, unsigned int* channels
, unsigned int* sampleRate
, drflac_uint64
* totalPCMFrameCount
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
11713 if (totalPCMFrameCount
) {
11714 *totalPCMFrameCount
= 0;
11717 pFlac
= drflac_open_memory(data
, dataSize
, pAllocationCallbacks
);
11718 if (pFlac
== NULL
) {
11722 return drflac__full_read_and_close_s16(pFlac
, channels
, sampleRate
, totalPCMFrameCount
);
11725 DRFLAC_API
float* drflac_open_memory_and_read_pcm_frames_f32(const void* data
, size_t dataSize
, unsigned int* channels
, unsigned int* sampleRate
, drflac_uint64
* totalPCMFrameCount
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
11735 if (totalPCMFrameCount
) {
11736 *totalPCMFrameCount
= 0;
11739 pFlac
= drflac_open_memory(data
, dataSize
, pAllocationCallbacks
);
11740 if (pFlac
== NULL
) {
11744 return drflac__full_read_and_close_f32(pFlac
, channels
, sampleRate
, totalPCMFrameCount
);
11748 DRFLAC_API
void drflac_free(void* p
, const drflac_allocation_callbacks
* pAllocationCallbacks
)
11750 if (pAllocationCallbacks
!= NULL
) {
11751 drflac__free_from_callbacks(p
, pAllocationCallbacks
);
11753 drflac__free_default(p
, NULL
);
11760 DRFLAC_API
void drflac_init_vorbis_comment_iterator(drflac_vorbis_comment_iterator
* pIter
, drflac_uint32 commentCount
, const void* pComments
)
11762 if (pIter
== NULL
) {
11766 pIter
->countRemaining
= commentCount
;
11767 pIter
->pRunningData
= (const char*)pComments
;
11770 DRFLAC_API
const char* drflac_next_vorbis_comment(drflac_vorbis_comment_iterator
* pIter
, drflac_uint32
* pCommentLengthOut
)
11772 drflac_int32 length
;
11773 const char* pComment
;
11776 if (pCommentLengthOut
) {
11777 *pCommentLengthOut
= 0;
11780 if (pIter
== NULL
|| pIter
->countRemaining
== 0 || pIter
->pRunningData
== NULL
) {
11784 length
= drflac__le2host_32(*(const drflac_uint32
*)pIter
->pRunningData
);
11785 pIter
->pRunningData
+= 4;
11787 pComment
= pIter
->pRunningData
;
11788 pIter
->pRunningData
+= length
;
11789 pIter
->countRemaining
-= 1;
11791 if (pCommentLengthOut
) {
11792 *pCommentLengthOut
= length
;
11801 DRFLAC_API
void drflac_init_cuesheet_track_iterator(drflac_cuesheet_track_iterator
* pIter
, drflac_uint32 trackCount
, const void* pTrackData
)
11803 if (pIter
== NULL
) {
11807 pIter
->countRemaining
= trackCount
;
11808 pIter
->pRunningData
= (const char*)pTrackData
;
11811 DRFLAC_API drflac_bool32
drflac_next_cuesheet_track(drflac_cuesheet_track_iterator
* pIter
, drflac_cuesheet_track
* pCuesheetTrack
)
11813 drflac_cuesheet_track cuesheetTrack
;
11814 const char* pRunningData
;
11815 drflac_uint64 offsetHi
;
11816 drflac_uint64 offsetLo
;
11818 if (pIter
== NULL
|| pIter
->countRemaining
== 0 || pIter
->pRunningData
== NULL
) {
11819 return DRFLAC_FALSE
;
11822 pRunningData
= pIter
->pRunningData
;
11824 offsetHi
= drflac__be2host_32(*(const drflac_uint32
*)pRunningData
); pRunningData
+= 4;
11825 offsetLo
= drflac__be2host_32(*(const drflac_uint32
*)pRunningData
); pRunningData
+= 4;
11826 cuesheetTrack
.offset
= offsetLo
| (offsetHi
<< 32);
11827 cuesheetTrack
.trackNumber
= pRunningData
[0]; pRunningData
+= 1;
11828 DRFLAC_COPY_MEMORY(cuesheetTrack
.ISRC
, pRunningData
, sizeof(cuesheetTrack
.ISRC
)); pRunningData
+= 12;
11829 cuesheetTrack
.isAudio
= (pRunningData
[0] & 0x80) != 0;
11830 cuesheetTrack
.preEmphasis
= (pRunningData
[0] & 0x40) != 0; pRunningData
+= 14;
11831 cuesheetTrack
.indexCount
= pRunningData
[0]; pRunningData
+= 1;
11832 cuesheetTrack
.pIndexPoints
= (const drflac_cuesheet_track_index
*)pRunningData
; pRunningData
+= cuesheetTrack
.indexCount
* sizeof(drflac_cuesheet_track_index
);
11834 pIter
->pRunningData
= pRunningData
;
11835 pIter
->countRemaining
-= 1;
11837 if (pCuesheetTrack
) {
11838 *pCuesheetTrack
= cuesheetTrack
;
11841 return DRFLAC_TRUE
;
11844 #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
11845 #pragma GCC diagnostic pop
11847 #endif /* dr_flac_c */
11848 #endif /* DR_FLAC_IMPLEMENTATION */
11854 v0.12.31 - 2021-08-16
11855 - Silence some warnings.
11857 v0.12.30 - 2021-07-31
11858 - Fix platform detection for ARM64.
11860 v0.12.29 - 2021-04-02
11861 - Fix a bug where the running PCM frame index is set to an invalid value when over-seeking.
11862 - Fix a decoding error due to an incorrect validation check.
11864 v0.12.28 - 2021-02-21
11865 - Fix a warning due to referencing _MSC_VER when it is undefined.
11867 v0.12.27 - 2021-01-31
11868 - Fix a static analysis warning.
11870 v0.12.26 - 2021-01-17
11871 - Fix a compilation warning due to _BSD_SOURCE being deprecated.
11873 v0.12.25 - 2020-12-26
11874 - Update documentation.
11876 v0.12.24 - 2020-11-29
11877 - Fix ARM64/NEON detection when compiling with MSVC.
11879 v0.12.23 - 2020-11-21
11880 - Fix compilation with OpenWatcom.
11882 v0.12.22 - 2020-11-01
11883 - Fix an error with the previous release.
11885 v0.12.21 - 2020-11-01
11886 - Fix a possible deadlock when seeking.
11887 - Improve compiler support for older versions of GCC.
11889 v0.12.20 - 2020-09-08
11890 - Fix a compilation error on older compilers.
11892 v0.12.19 - 2020-08-30
11893 - Fix a bug due to an undefined 32-bit shift.
11895 v0.12.18 - 2020-08-14
11896 - Fix a crash when compiling with clang-cl.
11898 v0.12.17 - 2020-08-02
11899 - Simplify sized types.
11901 v0.12.16 - 2020-07-25
11902 - Fix a compilation warning.
11904 v0.12.15 - 2020-07-06
11905 - Check for negative LPC shifts and return an error.
11907 v0.12.14 - 2020-06-23
11908 - Add include guard for the implementation section.
11910 v0.12.13 - 2020-05-16
11911 - Add compile-time and run-time version querying.
11912 - DRFLAC_VERSION_MINOR
11913 - DRFLAC_VERSION_MAJOR
11914 - DRFLAC_VERSION_REVISION
11915 - DRFLAC_VERSION_STRING
11917 - drflac_version_string()
11919 v0.12.12 - 2020-04-30
11920 - Fix compilation errors with VC6.
11922 v0.12.11 - 2020-04-19
11923 - Fix some pedantic warnings.
11924 - Fix some undefined behaviour warnings.
11926 v0.12.10 - 2020-04-10
11927 - Fix some bugs when trying to seek with an invalid seek table.
11929 v0.12.9 - 2020-04-05
11932 v0.12.8 - 2020-04-04
11933 - Add drflac_open_file_w() and drflac_open_file_with_metadata_w().
11934 - Fix some static analysis warnings.
11935 - Minor documentation updates.
11937 v0.12.7 - 2020-03-14
11938 - Fix compilation errors with VC6.
11940 v0.12.6 - 2020-03-07
11941 - Fix compilation error with Visual Studio .NET 2003.
11943 v0.12.5 - 2020-01-30
11944 - Silence some static analysis warnings.
11946 v0.12.4 - 2020-01-29
11947 - Silence some static analysis warnings.
11949 v0.12.3 - 2019-12-02
11950 - Fix some warnings when compiling with GCC and the -Og flag.
11951 - Fix a crash in out-of-memory situations.
11952 - Fix potential integer overflow bug.
11953 - Fix some static analysis warnings.
11954 - Fix a possible crash when using custom memory allocators without a custom realloc() implementation.
11955 - Fix a bug with binary search seeking where the bits per sample is not a multiple of 8.
11957 v0.12.2 - 2019-10-07
11958 - Internal code clean up.
11960 v0.12.1 - 2019-09-29
11961 - Fix some Clang Static Analyzer warnings.
11962 - Fix an unused variable warning.
11964 v0.12.0 - 2019-09-23
11965 - API CHANGE: Add support for user defined memory allocation routines. This system allows the program to specify their own memory allocation
11966 routines with a user data pointer for client-specific contextual data. This adds an extra parameter to the end of the following APIs:
11968 - drflac_open_relaxed()
11969 - drflac_open_with_metadata()
11970 - drflac_open_with_metadata_relaxed()
11971 - drflac_open_file()
11972 - drflac_open_file_with_metadata()
11973 - drflac_open_memory()
11974 - drflac_open_memory_with_metadata()
11975 - drflac_open_and_read_pcm_frames_s32()
11976 - drflac_open_and_read_pcm_frames_s16()
11977 - drflac_open_and_read_pcm_frames_f32()
11978 - drflac_open_file_and_read_pcm_frames_s32()
11979 - drflac_open_file_and_read_pcm_frames_s16()
11980 - drflac_open_file_and_read_pcm_frames_f32()
11981 - drflac_open_memory_and_read_pcm_frames_s32()
11982 - drflac_open_memory_and_read_pcm_frames_s16()
11983 - drflac_open_memory_and_read_pcm_frames_f32()
11984 Set this extra parameter to NULL to use defaults which is the same as the previous behaviour. Setting this NULL will use
11985 DRFLAC_MALLOC, DRFLAC_REALLOC and DRFLAC_FREE.
11986 - Remove deprecated APIs:
11987 - drflac_read_s32()
11988 - drflac_read_s16()
11989 - drflac_read_f32()
11990 - drflac_seek_to_sample()
11991 - drflac_open_and_decode_s32()
11992 - drflac_open_and_decode_s16()
11993 - drflac_open_and_decode_f32()
11994 - drflac_open_and_decode_file_s32()
11995 - drflac_open_and_decode_file_s16()
11996 - drflac_open_and_decode_file_f32()
11997 - drflac_open_and_decode_memory_s32()
11998 - drflac_open_and_decode_memory_s16()
11999 - drflac_open_and_decode_memory_f32()
12000 - Remove drflac.totalSampleCount which is now replaced with drflac.totalPCMFrameCount. You can emulate drflac.totalSampleCount
12001 by doing pFlac->totalPCMFrameCount*pFlac->channels.
12002 - Rename drflac.currentFrame to drflac.currentFLACFrame to remove ambiguity with PCM frames.
12003 - Fix errors when seeking to the end of a stream.
12004 - Optimizations to seeking.
12005 - SSE improvements and optimizations.
12006 - ARM NEON optimizations.
12007 - Optimizations to drflac_read_pcm_frames_s16().
12008 - Optimizations to drflac_read_pcm_frames_s32().
12010 v0.11.10 - 2019-06-26
12011 - Fix a compiler error.
12013 v0.11.9 - 2019-06-16
12014 - Silence some ThreadSanitizer warnings.
12016 v0.11.8 - 2019-05-21
12019 v0.11.7 - 2019-05-06
12022 v0.11.6 - 2019-05-05
12023 - Add support for C89.
12024 - Fix a compiler warning when CRC is disabled.
12025 - Change license to choice of public domain or MIT-0.
12027 v0.11.5 - 2019-04-19
12028 - Fix a compiler error with GCC.
12030 v0.11.4 - 2019-04-17
12031 - Fix some warnings with GCC when compiling with -std=c99.
12033 v0.11.3 - 2019-04-07
12034 - Silence warnings with GCC.
12036 v0.11.2 - 2019-03-10
12039 v0.11.1 - 2019-02-17
12040 - Fix a potential bug with seeking.
12042 v0.11.0 - 2018-12-16
12043 - API CHANGE: Deprecated drflac_read_s32(), drflac_read_s16() and drflac_read_f32() and replaced them with
12044 drflac_read_pcm_frames_s32(), drflac_read_pcm_frames_s16() and drflac_read_pcm_frames_f32(). The new APIs take
12045 and return PCM frame counts instead of sample counts. To upgrade you will need to change the input count by
12046 dividing it by the channel count, and then do the same with the return value.
12047 - API_CHANGE: Deprecated drflac_seek_to_sample() and replaced with drflac_seek_to_pcm_frame(). Same rules as
12048 the changes to drflac_read_*() apply.
12049 - API CHANGE: Deprecated drflac_open_and_decode_*() and replaced with drflac_open_*_and_read_*(). Same rules as
12050 the changes to drflac_read_*() apply.
12053 v0.10.0 - 2018-09-11
12054 - Remove the DR_FLAC_NO_WIN32_IO option and the Win32 file IO functionality. If you need to use Win32 file IO you
12055 need to do it yourself via the callback API.
12056 - Fix the clang build.
12057 - Fix undefined behavior.
12058 - Fix errors with CUESHEET metdata blocks.
12059 - Add an API for iterating over each cuesheet track in the CUESHEET metadata block. This works the same way as the
12060 Vorbis comment API.
12061 - Other miscellaneous bug fixes, mostly relating to invalid FLAC streams.
12062 - Minor optimizations.
12064 v0.9.11 - 2018-08-29
12065 - Fix a bug with sample reconstruction.
12067 v0.9.10 - 2018-08-07
12068 - Improve 64-bit detection.
12070 v0.9.9 - 2018-08-05
12071 - Fix C++ build on older versions of GCC.
12073 v0.9.8 - 2018-07-24
12074 - Fix compilation errors.
12076 v0.9.7 - 2018-07-05
12079 v0.9.6 - 2018-06-29
12082 v0.9.5 - 2018-06-23
12083 - Fix some warnings.
12085 v0.9.4 - 2018-06-14
12086 - Optimizations to seeking.
12089 v0.9.3 - 2018-05-22
12092 v0.9.2 - 2018-05-12
12093 - Fix a compilation error due to a missing break statement.
12095 v0.9.1 - 2018-04-29
12096 - Fix compilation error with Clang.
12100 - Start using major.minor.revision versioning.
12103 - Fix build on non-x86/x64 architectures.
12106 - Stop pretending to support changing rate/channels mid stream.
12109 - Fix a crash when the block size of a frame is larger than the maximum block size defined by the FLAC stream.
12110 - Fix a crash the the Rice partition order is invalid.
12113 - Add support for decoding streams with ID3 tags. ID3 tags are just skipped.
12116 - Fix warning on non-x86/x64 architectures.
12119 - Fix build on non-x86/x64 architectures.
12122 - A small optimization for the Clang build.
12125 - API CHANGE: Rename dr_* types to drflac_*.
12126 - Optimizations. This brings dr_flac back to about the same class of efficiency as the reference implementation.
12127 - Add support for custom implementations of malloc(), realloc(), etc.
12128 - Add CRC checking to Ogg encapsulated streams.
12129 - Fix VC++ 6 build. This is only for the C++ compiler. The C compiler is not currently supported.
12133 - Add support for opening a stream without a header block. To do this, use drflac_open_relaxed() / drflac_open_with_metadata_relaxed().
12136 - Add support for recovering from invalid frames. With this change, dr_flac will simply skip over invalid frames as if they
12137 never existed. Frames are checked against their sync code, the CRC-8 of the frame header and the CRC-16 of the whole frame.
12141 - Change drflac_bool* types to unsigned.
12142 - Add CRC checking. This makes dr_flac slower, but can be disabled with #define DR_FLAC_NO_CRC.
12145 - Fix a couple of bugs with the bitstreaming code.
12148 - Fix some warnings.
12151 - Add support for 32-bit floating-point PCM decoding.
12152 - Use drflac_int* and drflac_uint* sized types to improve compiler support.
12153 - Minor improvements to documentation.
12156 - Add support for signed 16-bit integer PCM decoding.
12159 - A minor change to drflac_bool8 and drflac_bool32 types.
12162 - Rename drBool32 to drflac_bool32 for styling consistency.
12165 - API/ABI CHANGE: Use fixed size 32-bit booleans instead of the built-in bool type.
12166 - API CHANGE: Rename drflac_open_and_decode*() to drflac_open_and_decode*_s32().
12167 - API CHANGE: Swap the order of "channels" and "sampleRate" parameters in drflac_open_and_decode*(). Rationale for this is to
12168 keep it consistent with drflac_audio.
12171 - Fix a warning with GCC.
12174 - Fixed a bug where GCC 4.3+ was not getting properly identified.
12175 - Fixed a few typos.
12176 - Changed date formats to ISO 8601 (YYYY-MM-DD).
12182 - Fixed compilation error.
12185 - Fixed Linux/GCC build.
12186 - Updated documentation.
12189 - Minor fixes to documentation.
12192 - Optimizations. Now at about parity with the reference implementation on 32-bit builds.
12193 - Lots of clean up.
12199 - Made drflac_open_and_decode() more robust.
12200 - Removed an unused debugging variable
12203 - Added support for Ogg encapsulation.
12204 - API CHANGE. Have the onSeek callback take a third argument which specifies whether or not the seek
12205 should be relative to the start or the current position. Also changes the seeking rules such that
12206 seeking offsets will never be negative.
12207 - Have drflac_open_and_decode() fail gracefully if the stream has an unknown total sample count.
12210 - Properly close the file handle in drflac_open_file() and family when the decoder fails to initialize.
12211 - Removed a stale comment.
12214 - Minor formatting changes.
12215 - Fixed a warning on the GCC build.
12218 - Initial versioned release.
12222 This software is available as a choice of the following licenses. Choose
12223 whichever you prefer.
12225 ===============================================================================
12226 ALTERNATIVE 1 - Public Domain (www.unlicense.org)
12227 ===============================================================================
12228 This is free and unencumbered software released into the public domain.
12230 Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
12231 software, either in source code form or as a compiled binary, for any purpose,
12232 commercial or non-commercial, and by any means.
12234 In jurisdictions that recognize copyright laws, the author or authors of this
12235 software dedicate any and all copyright interest in the software to the public
12236 domain. We make this dedication for the benefit of the public at large and to
12237 the detriment of our heirs and successors. We intend this dedication to be an
12238 overt act of relinquishment in perpetuity of all present and future rights to
12239 this software under copyright law.
12241 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12242 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12243 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
12244 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
12245 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
12246 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12248 For more information, please refer to <http://unlicense.org/>
12250 ===============================================================================
12251 ALTERNATIVE 2 - MIT No Attribution
12252 ===============================================================================
12253 Copyright 2020 David Reid
12255 Permission is hereby granted, free of charge, to any person obtaining a copy of
12256 this software and associated documentation files (the "Software"), to deal in
12257 the Software without restriction, including without limitation the rights to
12258 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12259 of the Software, and to permit persons to whom the Software is furnished to do
12262 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12263 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12264 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
12265 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
12266 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
12267 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE