From 794a899dfed395822eba75b4b0b1b8387fc807e6 Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Sat, 31 Oct 2026 06:29:32 +0300
Subject: [PATCH] update dr_flac and dr_mp3 from mainstream (Oct.31, 2026)
(cherry picked from commit 8d30c639eb30014cd2916f505b439f91971a4ec3)
---
src/dr_libs/dr_flac.h | 1711 ++++++++++++++++++++++++++++-------------
src/dr_libs/dr_mp3.h | 2 +-
2 files changed, 1175 insertions(+), 538 deletions(-)
diff --git a/src/dr_libs/dr_flac.h b/src/dr_libs/dr_flac.h
index f811bcfa..6d66ea2d 100644
--- a/src/dr_libs/dr_flac.h
+++ b/src/dr_libs/dr_flac.h
@@ -1,6 +1,6 @@
/*
FLAC audio decoder. Choice of public domain or MIT-0. See license statements at the end of this file.
-dr_flac - v0.13.4 - TBD
+dr_flac - v0.13.4 - 2026-10-31
David Reid - mackron@gmail.com
@@ -556,6 +556,12 @@ typedef struct
drflac_uint32 crc16CacheIgnoredBytes; /* The number of bytes to ignore when updating the CRC-16 from the CRC-16 cache. */
} drflac_bs;
+typedef union
+{
+ drflac_int32* pS32;
+ drflac_int64* pS64;
+} drflac_samples;
+
typedef struct
{
/* The type of the subframe: SUBFRAME_CONSTANT, SUBFRAME_VERBATIM, SUBFRAME_FIXED or SUBFRAME_LPC. */
@@ -567,8 +573,8 @@ typedef struct
/* The order to use for the prediction stage for SUBFRAME_FIXED and SUBFRAME_LPC. */
drflac_uint8 lpcOrder;
- /* A pointer to the buffer containing the decoded samples in the subframe. This pointer is an offset from drflac::pExtraData. */
- drflac_int32* pSamplesS32;
+ /* An offset from drflac::pExtraData. 32-bit streams use pS64; lower bit depths use pS32. */
+ drflac_samples samples;
} drflac_subframe;
typedef struct
@@ -592,8 +598,8 @@ typedef struct
drflac_uint16 blockSizeInPCMFrames;
/*
- The channel assignment of this frame. This is not always set to the channel count. If interchannel decorrelation is being used this
- will be set to DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE, DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE or DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE.
+ The raw channel assignment from the frame header. Values 0 through 7 specify one through eight independent channels (channel count minus one).
+ Stereo decorrelation uses DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE, DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE or DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE.
*/
drflac_uint8 channelAssignment;
@@ -640,7 +646,7 @@ typedef struct
*/
drflac_uint8 channels;
- /* The bits per sample. Will be set to something like 16, 24, etc. */
+ /* The bits per sample declared by STREAMINFO, or inferred when STREAMINFO is missing. Individual FLAC frames may specify a different depth. */
drflac_uint8 bitsPerSample;
/* The maximum block size, in samples. This number represents the number of samples in each channel (not combined). */
@@ -667,7 +673,7 @@ typedef struct
/* The index of the PCM frame the decoder is currently sitting on. This is only used for seeking. */
drflac_uint64 currentPCMFrame;
- /* The position of the first FLAC frame in the stream. This is only ever used for seeking. */
+ /* The position from which to find the first FLAC frame. Headerless streams may have leading bytes to skip. This is only used for seeking. */
drflac_uint64 firstFLACFramePosInBytes;
@@ -675,8 +681,12 @@ typedef struct
drflac__memory_stream memoryStream;
- /* A pointer to the decoded sample data. This is an offset of pExtraData. */
- drflac_int32* pDecodedSamples;
+ /* Base of the active decoded sample storage. 32-bit streams use pS64; lower bit depths use pS32. */
+ drflac_samples decodedSamples;
+
+ /* Separate allocation for frames larger than STREAMINFO permits. NULL when using pExtraData. */
+ void* pDecodedSamplesAllocation;
+ drflac_uint16 decodedSamplesCapacityInPCMFrames;
/* A pointer to the seek table. This is an offset of pExtraData, or NULL if there is no seek table. */
drflac_seekpoint* pSeekpoints;
@@ -1630,7 +1640,7 @@ typedef drflac_int32 drflac_result;
#define DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE 0
#define DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2 1
-#define DRFLAC_CHANNEL_ASSIGNMENT_INDEPENDENT 0
+#define DRFLAC_CHANNEL_ASSIGNMENT_INDEPENDENT_STEREO 1
#define DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE 8
#define DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE 9
#define DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE 10
@@ -2419,48 +2429,55 @@ static drflac_bool32 drflac__read_int32(drflac_bs* bs, unsigned int bitCount, dr
return DRFLAC_TRUE;
}
-#ifdef DRFLAC_64BIT
static drflac_bool32 drflac__read_uint64(drflac_bs* bs, unsigned int bitCount, drflac_uint64* pResultOut)
{
- drflac_uint32 resultHi;
+ drflac_uint32 resultHi = 0;
drflac_uint32 resultLo;
+ DRFLAC_ASSERT(bs != NULL);
+ DRFLAC_ASSERT(pResultOut != NULL);
+ DRFLAC_ASSERT(bitCount > 0);
DRFLAC_ASSERT(bitCount <= 64);
- DRFLAC_ASSERT(bitCount > 32);
- if (!drflac__read_uint32(bs, bitCount - 32, &resultHi)) {
- return DRFLAC_FALSE;
+ if (bitCount > 32) {
+ if (!drflac__read_uint32(bs, bitCount - 32, &resultHi)) {
+ return DRFLAC_FALSE;
+ }
+
+ bitCount = 32;
}
- if (!drflac__read_uint32(bs, 32, &resultLo)) {
+ if (!drflac__read_uint32(bs, bitCount, &resultLo)) {
return DRFLAC_FALSE;
}
*pResultOut = (((drflac_uint64)resultHi) << 32) | ((drflac_uint64)resultLo);
return DRFLAC_TRUE;
}
-#endif
-/* Function below is unused, but leaving it here in case I need to quickly add it again. */
-#if 0
-static drflac_bool32 drflac__read_int64(drflac_bs* bs, unsigned int bitCount, drflac_int64* pResultOut)
+static DRFLAC_INLINE drflac_bool32 drflac__read_int64(drflac_bs* bs, unsigned int bitCount, drflac_int64* pResultOut)
{
drflac_uint64 result;
- drflac_uint64 signbit;
+ DRFLAC_ASSERT(bs != NULL);
+ DRFLAC_ASSERT(pResultOut != NULL);
+ DRFLAC_ASSERT(bitCount > 0);
DRFLAC_ASSERT(bitCount <= 64);
if (!drflac__read_uint64(bs, bitCount, &result)) {
return DRFLAC_FALSE;
}
- signbit = ((result >> (bitCount-1)) & 0x01);
- result |= (~signbit + 1) << bitCount;
+ /* Do not attempt to shift by 64 as it's undefined. */
+ if (bitCount < 64) {
+ drflac_uint64 signbit;
+ signbit = ((result >> (bitCount-1)) & 0x01);
+ result |= (~signbit + 1) << bitCount;
+ }
*pResultOut = (drflac_int64)result;
return DRFLAC_TRUE;
}
-#endif
static drflac_bool32 drflac__read_uint16(drflac_bs* bs, unsigned int bitCount, drflac_uint16* pResult)
{
@@ -2612,18 +2629,30 @@ static drflac_bool32 drflac__find_and_seek_to_next_sync_code(drflac_bs* bs)
return DRFLAC_FALSE;
}
- if (hi == 0xFF) {
+ while (hi == 0xFF) {
drflac_uint8 lo;
+ drflac_uint8 trailingBits;
if (!drflac__read_uint8(bs, 6, &lo)) {
return DRFLAC_FALSE;
}
if (lo == 0x3E) {
return DRFLAC_TRUE;
- } else {
- if (!drflac__seek_bits(bs, DRFLAC_CACHE_L1_BITS_REMAINING(bs) & 7)) {
- return DRFLAC_FALSE;
+ }
+
+ if (!drflac__read_uint8(bs, 2, &trailingBits)) {
+ return DRFLAC_FALSE;
+ }
+
+ hi = (drflac_uint8)((lo << 2) | trailingBits);
+ if (hi == 0xFF) {
+ /* This byte can be the start of a sync code. Do not skip it when searching again. */
+ #ifndef DR_FLAC_NO_CRC
+ {
+ drflac__reset_crc16(bs);
+ bs->crc16 = drflac_crc16_byte(0, hi);
}
+ #endif
}
}
}
@@ -2889,7 +2918,6 @@ static DRFLAC_INLINE drflac_bool32 drflac__seek_past_next_set_bit(drflac_bs* bs,
static drflac_bool32 drflac__seek_to_byte(drflac_bs* bs, drflac_uint64 offsetFromStart)
{
DRFLAC_ASSERT(bs != NULL);
- DRFLAC_ASSERT(offsetFromStart > 0);
/*
Seeking from the start is not quite as trivial as it sounds because the onSeek callback takes a signed 32-bit integer (which
@@ -3020,7 +3048,7 @@ safe to assume this will be slower on 32-bit platforms so we use a more optimal
#if defined(__clang__)
__attribute__((no_sanitize("signed-integer-overflow")))
#endif
-static DRFLAC_INLINE drflac_int32 drflac__calculate_prediction_32(drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pDecodedSamples)
+static DRFLAC_INLINE drflac_int32 drflac__calculate_prediction_32_s32(drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pDecodedSamples)
{
drflac_int32 prediction = 0;
@@ -3068,7 +3096,7 @@ static DRFLAC_INLINE drflac_int32 drflac__calculate_prediction_32(drflac_uint32
return (drflac_int32)(prediction >> shift);
}
-static DRFLAC_INLINE drflac_int32 drflac__calculate_prediction_64(drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pDecodedSamples)
+static DRFLAC_INLINE drflac_int32 drflac__calculate_prediction_64_s32(drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pDecodedSamples)
{
drflac_int64 prediction;
@@ -3295,9 +3323,9 @@ static drflac_bool32 drflac__decode_samples_with_residual__rice__reference(drfla
if (drflac__use_64_bit_prediction(bitsPerSample, lpcOrder, lpcPrecision)) {
- pSamplesOut[i] = decodedRice + drflac__calculate_prediction_64(lpcOrder, lpcShift, coefficients, pSamplesOut + i);
+ pSamplesOut[i] = decodedRice + drflac__calculate_prediction_64_s32(lpcOrder, lpcShift, coefficients, pSamplesOut + i);
} else {
- pSamplesOut[i] = decodedRice + drflac__calculate_prediction_32(lpcOrder, lpcShift, coefficients, pSamplesOut + i);
+ pSamplesOut[i] = decodedRice + drflac__calculate_prediction_32_s32(lpcOrder, lpcShift, coefficients, pSamplesOut + i);
}
}
@@ -3736,10 +3764,10 @@ static drflac_bool32 drflac__decode_samples_with_residual__rice__scalar(drflac_b
riceParamPart2 = (riceParamPart2 >> 1) ^ t[riceParamPart2 & 0x01];
riceParamPart3 = (riceParamPart3 >> 1) ^ t[riceParamPart3 & 0x01];
- pSamplesOut[0] = riceParamPart0 + drflac__calculate_prediction_64(lpcOrder, lpcShift, coefficients, pSamplesOut + 0);
- pSamplesOut[1] = riceParamPart1 + drflac__calculate_prediction_64(lpcOrder, lpcShift, coefficients, pSamplesOut + 1);
- pSamplesOut[2] = riceParamPart2 + drflac__calculate_prediction_64(lpcOrder, lpcShift, coefficients, pSamplesOut + 2);
- pSamplesOut[3] = riceParamPart3 + drflac__calculate_prediction_64(lpcOrder, lpcShift, coefficients, pSamplesOut + 3);
+ pSamplesOut[0] = riceParamPart0 + drflac__calculate_prediction_64_s32(lpcOrder, lpcShift, coefficients, pSamplesOut + 0);
+ pSamplesOut[1] = riceParamPart1 + drflac__calculate_prediction_64_s32(lpcOrder, lpcShift, coefficients, pSamplesOut + 1);
+ pSamplesOut[2] = riceParamPart2 + drflac__calculate_prediction_64_s32(lpcOrder, lpcShift, coefficients, pSamplesOut + 2);
+ pSamplesOut[3] = riceParamPart3 + drflac__calculate_prediction_64_s32(lpcOrder, lpcShift, coefficients, pSamplesOut + 3);
pSamplesOut += 4;
}
@@ -3767,10 +3795,10 @@ static drflac_bool32 drflac__decode_samples_with_residual__rice__scalar(drflac_b
riceParamPart2 = (riceParamPart2 >> 1) ^ t[riceParamPart2 & 0x01];
riceParamPart3 = (riceParamPart3 >> 1) ^ t[riceParamPart3 & 0x01];
- pSamplesOut[0] = riceParamPart0 + drflac__calculate_prediction_32(lpcOrder, lpcShift, coefficients, pSamplesOut + 0);
- pSamplesOut[1] = riceParamPart1 + drflac__calculate_prediction_32(lpcOrder, lpcShift, coefficients, pSamplesOut + 1);
- pSamplesOut[2] = riceParamPart2 + drflac__calculate_prediction_32(lpcOrder, lpcShift, coefficients, pSamplesOut + 2);
- pSamplesOut[3] = riceParamPart3 + drflac__calculate_prediction_32(lpcOrder, lpcShift, coefficients, pSamplesOut + 3);
+ pSamplesOut[0] = riceParamPart0 + drflac__calculate_prediction_32_s32(lpcOrder, lpcShift, coefficients, pSamplesOut + 0);
+ pSamplesOut[1] = riceParamPart1 + drflac__calculate_prediction_32_s32(lpcOrder, lpcShift, coefficients, pSamplesOut + 1);
+ pSamplesOut[2] = riceParamPart2 + drflac__calculate_prediction_32_s32(lpcOrder, lpcShift, coefficients, pSamplesOut + 2);
+ pSamplesOut[3] = riceParamPart3 + drflac__calculate_prediction_32_s32(lpcOrder, lpcShift, coefficients, pSamplesOut + 3);
pSamplesOut += 4;
}
@@ -3791,9 +3819,9 @@ static drflac_bool32 drflac__decode_samples_with_residual__rice__scalar(drflac_b
/* Sample reconstruction. */
if (drflac__use_64_bit_prediction(bitsPerSample, lpcOrder, lpcPrecision)) {
- pSamplesOut[0] = riceParamPart0 + drflac__calculate_prediction_64(lpcOrder, lpcShift, coefficients, pSamplesOut + 0);
+ pSamplesOut[0] = riceParamPart0 + drflac__calculate_prediction_64_s32(lpcOrder, lpcShift, coefficients, pSamplesOut + 0);
} else {
- pSamplesOut[0] = riceParamPart0 + drflac__calculate_prediction_32(lpcOrder, lpcShift, coefficients, pSamplesOut + 0);
+ pSamplesOut[0] = riceParamPart0 + drflac__calculate_prediction_32_s32(lpcOrder, lpcShift, coefficients, pSamplesOut + 0);
}
i += 1;
@@ -4050,7 +4078,7 @@ static drflac_bool32 drflac__decode_samples_with_residual__rice__sse41_32(drflac
riceParamParts0 = (riceParamParts0 >> 1) ^ t[riceParamParts0 & 0x01];
/* Sample reconstruction. */
- pDecodedSamples[0] = riceParamParts0 + drflac__calculate_prediction_32(order, shift, coefficients, pDecodedSamples);
+ pDecodedSamples[0] = riceParamParts0 + drflac__calculate_prediction_32_s32(order, shift, coefficients, pDecodedSamples);
i += 1;
pDecodedSamples += 1;
@@ -4240,7 +4268,7 @@ static drflac_bool32 drflac__decode_samples_with_residual__rice__sse41_64(drflac
riceParamParts0 = (riceParamParts0 >> 1) ^ t[riceParamParts0 & 0x01];
/* Sample reconstruction. */
- pDecodedSamples[0] = riceParamParts0 + drflac__calculate_prediction_64(order, shift, coefficients, pDecodedSamples);
+ pDecodedSamples[0] = riceParamParts0 + drflac__calculate_prediction_64_s32(order, shift, coefficients, pDecodedSamples);
i += 1;
pDecodedSamples += 1;
@@ -4560,7 +4588,7 @@ static drflac_bool32 drflac__decode_samples_with_residual__rice__neon_32(drflac_
riceParamParts[0] = (riceParamParts[0] >> 1) ^ t[riceParamParts[0] & 0x01];
/* Sample reconstruction. */
- pDecodedSamples[0] = riceParamParts[0] + drflac__calculate_prediction_32(order, shift, coefficients, pDecodedSamples);
+ pDecodedSamples[0] = riceParamParts[0] + drflac__calculate_prediction_32_s32(order, shift, coefficients, pDecodedSamples);
i += 1;
pDecodedSamples += 1;
@@ -4737,7 +4765,7 @@ static drflac_bool32 drflac__decode_samples_with_residual__rice__neon_64(drflac_
riceParamParts[0] = (riceParamParts[0] >> 1) ^ t[riceParamParts[0] & 0x01];
/* Sample reconstruction. */
- pDecodedSamples[0] = riceParamParts[0] + drflac__calculate_prediction_64(order, shift, coefficients, pDecodedSamples);
+ pDecodedSamples[0] = riceParamParts[0] + drflac__calculate_prediction_64_s32(order, shift, coefficients, pDecodedSamples);
i += 1;
pDecodedSamples += 1;
@@ -4822,22 +4850,117 @@ static drflac_bool32 drflac__decode_samples_with_residual__unencoded(drflac_bs*
}
if (drflac__use_64_bit_prediction(bitsPerSample, lpcOrder, lpcPrecision)) {
- pSamplesOut[i] += drflac__calculate_prediction_64(lpcOrder, lpcShift, coefficients, pSamplesOut + i);
+ pSamplesOut[i] += drflac__calculate_prediction_64_s32(lpcOrder, lpcShift, coefficients, pSamplesOut + i);
} else {
- pSamplesOut[i] += drflac__calculate_prediction_32(lpcOrder, lpcShift, coefficients, pSamplesOut + i);
+ pSamplesOut[i] += drflac__calculate_prediction_32_s32(lpcOrder, lpcShift, coefficients, pSamplesOut + i);
+ }
+ }
+
+ return DRFLAC_TRUE;
+}
+
+
+static DRFLAC_INLINE drflac_int64 drflac__calculate_prediction_64_s64(drflac_uint32 order, drflac_int32 shift, const drflac_int32* coefficients, const drflac_int64* pDecodedSamples)
+{
+ drflac_int64 prediction = 0;
+ drflac_uint32 i;
+
+ /* At most 32 coefficients of 15 bits each, multiplied by samples of at most 33 bits. */
+ for (i = 0; i < order; ++i) {
+ prediction += coefficients[i] * pDecodedSamples[-(drflac_int32)i - 1];
+ }
+
+ return prediction >> shift;
+}
+
+static drflac_bool32 drflac__decode_samples_with_residual__rice__s64(drflac_bs* bs, drflac_uint32 bitsPerSample, drflac_uint32 count, drflac_uint8 riceParam, drflac_uint32 lpcOrder, drflac_int32 lpcShift, const drflac_int32* coefficients, drflac_int64* pSamplesOut)
+{
+ drflac_uint32 riceParamMask;
+ drflac_int64 minSample;
+ drflac_int64 maxSample;
+ drflac_uint32 i;
+
+ DRFLAC_ASSERT(bitsPerSample > 0 && bitsPerSample <= 33);
+
+ riceParamMask = (1U << riceParam) - 1;
+ minSample = -((drflac_int64)1 << (bitsPerSample - 1));
+ maxSample = -(minSample + 1);
+
+ for (i = 0; i < count; ++i) {
+ drflac_uint32 zeroCountPart;
+ drflac_uint32 riceParamPart;
+ drflac_int32 residual;
+ drflac_int64 sample;
+
+ if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart, &riceParamPart)) {
+ return DRFLAC_FALSE;
+ }
+
+ /* Residuals must fit in a signed 32-bit integer, excluding its most negative value. */
+ if (zeroCountPart > (0xFFFFFFFFU >> riceParam)) {
+ return DRFLAC_FALSE;
+ }
+
+ riceParamPart = (zeroCountPart << riceParam) | (riceParamPart & riceParamMask);
+ if (riceParamPart == 0xFFFFFFFFU) {
+ return DRFLAC_FALSE;
+ }
+
+ residual = (drflac_int32)(riceParamPart >> 1);
+ if ((riceParamPart & 1) != 0) {
+ residual = -residual - 1;
}
+
+ sample = drflac__calculate_prediction_64_s64(lpcOrder, lpcShift, coefficients, pSamplesOut + i) + residual;
+ if (sample < minSample || sample > maxSample) {
+ return DRFLAC_FALSE;
+ }
+
+ pSamplesOut[i] = sample;
}
return DRFLAC_TRUE;
}
+static drflac_bool32 drflac__decode_samples_with_residual__unencoded_s64(drflac_bs* bs, drflac_uint32 bitsPerSample, drflac_uint32 count, drflac_uint8 unencodedBitsPerSample, drflac_uint32 lpcOrder, drflac_int32 lpcShift, const drflac_int32* coefficients, drflac_int64* pSamplesOut)
+{
+ drflac_int64 minSample;
+ drflac_int64 maxSample;
+ drflac_uint32 i;
+
+ DRFLAC_ASSERT(bitsPerSample > 0 && bitsPerSample <= 33);
+ DRFLAC_ASSERT(unencodedBitsPerSample <= 31);
+
+ minSample = -((drflac_int64)1 << (bitsPerSample - 1));
+ maxSample = -(minSample + 1);
+
+ for (i = 0; i < count; ++i) {
+ drflac_int32 residual = 0;
+ drflac_int64 sample;
+
+ if (unencodedBitsPerSample > 0) {
+ if (!drflac__read_int32(bs, unencodedBitsPerSample, &residual)) {
+ return DRFLAC_FALSE;
+ }
+ }
+
+ sample = drflac__calculate_prediction_64_s64(lpcOrder, lpcShift, coefficients, pSamplesOut + i) + residual;
+ if (sample < minSample || sample > maxSample) {
+ return DRFLAC_FALSE;
+ }
+
+ pSamplesOut[i] = sample;
+ }
+
+ return DRFLAC_TRUE;
+}
/*
Reads and decodes the residual for the sub-frame the decoder is currently sitting on. This function should be called
when the decoder is sitting at the very start of the RESIDUAL block. The first <order> residuals will be ignored. The
<blockSize> and <order> parameters are used to determine how many residual values need to be decoded.
*/
-static drflac_bool32 drflac__decode_samples_with_residual(drflac_bs* bs, drflac_uint32 bitsPerSample, drflac_uint32 blockSize, drflac_uint32 lpcOrder, drflac_int32 lpcShift, drflac_uint32 lpcPrecision, const drflac_int32* coefficients, drflac_int32* pDecodedSamples)
+static drflac_bool32 drflac__decode_samples_with_residual(drflac_bs* bs, drflac_uint32 bitsPerSample, drflac_uint32 blockSize, drflac_uint32 lpcOrder, drflac_int32 lpcShift, drflac_uint32 lpcPrecision, const drflac_int32* coefficients, drflac_int32* pDecodedSamplesS32, drflac_int64* pDecodedSamplesS64)
{
drflac_uint8 residualMethod;
drflac_uint8 partitionOrder;
@@ -4846,7 +4969,8 @@ static drflac_bool32 drflac__decode_samples_with_residual(drflac_bs* bs, drflac_
DRFLAC_ASSERT(bs != NULL);
DRFLAC_ASSERT(blockSize != 0);
- DRFLAC_ASSERT(pDecodedSamples != NULL); /* <-- Should we allow NULL, in which case we just seek past the residual rather than do a full decode? */
+ DRFLAC_ASSERT(pDecodedSamplesS32 != NULL || pDecodedSamplesS64 != NULL);
+ DRFLAC_ASSERT(pDecodedSamplesS32 == NULL || pDecodedSamplesS64 == NULL);
if (!drflac__read_uint8(bs, 2, &residualMethod)) {
return DRFLAC_FALSE;
@@ -4856,18 +4980,19 @@ static drflac_bool32 drflac__decode_samples_with_residual(drflac_bs* bs, drflac_
return DRFLAC_FALSE; /* Unknown or unsupported residual coding method. */
}
- /* Ignore the first <order> values. */
- pDecodedSamples += lpcOrder;
-
if (!drflac__read_uint8(bs, 4, &partitionOrder)) {
return DRFLAC_FALSE;
}
/*
From the FLAC spec:
+
The Rice partition order in a Rice-coded residual section must be less than or equal to 8.
+
+ Technically the Rice partition order can be up to 15 because it's stored in 4 bits. dr_flac is
+ supporting the full range up to 15.
*/
- if (partitionOrder > 8) {
+ if ((blockSize & ((1U << partitionOrder) - 1)) != 0) {
return DRFLAC_FALSE;
}
@@ -4876,6 +5001,13 @@ static drflac_bool32 drflac__decode_samples_with_residual(drflac_bs* bs, drflac_
return DRFLAC_FALSE;
}
+ /* Skip the warmup samples. Exactly one sample buffer must be specified. */
+ if (pDecodedSamplesS32 != NULL) {
+ pDecodedSamplesS32 += lpcOrder;
+ } else {
+ pDecodedSamplesS64 += lpcOrder;
+ }
+
samplesInPartition = (blockSize / (1 << partitionOrder)) - lpcOrder;
partitionsRemaining = (1 << partitionOrder);
for (;;) {
@@ -4897,8 +5029,14 @@ static drflac_bool32 drflac__decode_samples_with_residual(drflac_bs* bs, drflac_
}
if (riceParam != 0xFF) {
- if (!drflac__decode_samples_with_residual__rice(bs, bitsPerSample, samplesInPartition, riceParam, lpcOrder, lpcShift, lpcPrecision, coefficients, pDecodedSamples)) {
- return DRFLAC_FALSE;
+ if (pDecodedSamplesS32 != NULL) {
+ if (!drflac__decode_samples_with_residual__rice(bs, bitsPerSample, samplesInPartition, riceParam, lpcOrder, lpcShift, lpcPrecision, coefficients, pDecodedSamplesS32)) {
+ return DRFLAC_FALSE;
+ }
+ } else {
+ if (!drflac__decode_samples_with_residual__rice__s64(bs, bitsPerSample, samplesInPartition, riceParam, lpcOrder, lpcShift, coefficients, pDecodedSamplesS64)) {
+ return DRFLAC_FALSE;
+ }
}
} else {
drflac_uint8 unencodedBitsPerSample = 0;
@@ -4906,12 +5044,22 @@ static drflac_bool32 drflac__decode_samples_with_residual(drflac_bs* bs, drflac_
return DRFLAC_FALSE;
}
- if (!drflac__decode_samples_with_residual__unencoded(bs, bitsPerSample, samplesInPartition, unencodedBitsPerSample, lpcOrder, lpcShift, lpcPrecision, coefficients, pDecodedSamples)) {
- return DRFLAC_FALSE;
+ if (pDecodedSamplesS32 != NULL) {
+ if (!drflac__decode_samples_with_residual__unencoded(bs, bitsPerSample, samplesInPartition, unencodedBitsPerSample, lpcOrder, lpcShift, lpcPrecision, coefficients, pDecodedSamplesS32)) {
+ return DRFLAC_FALSE;
+ }
+ } else {
+ if (!drflac__decode_samples_with_residual__unencoded_s64(bs, bitsPerSample, samplesInPartition, unencodedBitsPerSample, lpcOrder, lpcShift, coefficients, pDecodedSamplesS64)) {
+ return DRFLAC_FALSE;
+ }
}
}
- pDecodedSamples += samplesInPartition;
+ if (pDecodedSamplesS32 != NULL) {
+ pDecodedSamplesS32 += samplesInPartition;
+ } else {
+ pDecodedSamplesS64 += samplesInPartition;
+ }
if (partitionsRemaining == 1) {
break;
@@ -4956,14 +5104,18 @@ static drflac_bool32 drflac__read_and_seek_residual(drflac_bs* bs, drflac_uint32
/*
From the FLAC spec:
+
The Rice partition order in a Rice-coded residual section must be less than or equal to 8.
+
+ Technically the Rice partition order can be up to 15 because it's stored in 4 bits. dr_flac is
+ supporting the full range up to 15.
*/
- if (partitionOrder > 8) {
+ if ((blockSize & ((1U << partitionOrder) - 1)) != 0) {
return DRFLAC_FALSE;
}
/* Validation check. */
- if ((blockSize / (1 << partitionOrder)) <= order) {
+ if ((blockSize / (1 << partitionOrder)) < order) {
return DRFLAC_FALSE;
}
@@ -5016,44 +5168,65 @@ static drflac_bool32 drflac__read_and_seek_residual(drflac_bs* bs, drflac_uint32
}
-static drflac_bool32 drflac__decode_samples__constant(drflac_bs* bs, drflac_uint32 blockSize, drflac_uint32 subframeBitsPerSample, drflac_int32* pDecodedSamples)
+static drflac_bool32 drflac__decode_samples__constant(drflac_bs* bs, drflac_uint32 blockSize, drflac_uint32 subframeBitsPerSample, drflac_int32* pDecodedSamplesS32, drflac_int64* pDecodedSamplesS64)
{
drflac_uint32 i;
- /* Only a single sample needs to be decoded here. */
- drflac_int32 sample;
- if (!drflac__read_int32(bs, subframeBitsPerSample, &sample)) {
- return DRFLAC_FALSE;
- }
+ DRFLAC_ASSERT(pDecodedSamplesS32 != NULL || pDecodedSamplesS64 != NULL);
+ DRFLAC_ASSERT(pDecodedSamplesS32 == NULL || pDecodedSamplesS64 == NULL);
/*
- We don't really need to expand this, but it does simplify the process of reading samples. If this becomes a performance issue (unlikely)
- we'll want to look at a more efficient way.
+ Only a single sample needs to be decoded here. We don't really need to expand this, but it does simplify the process of reading samples.
+ If this becomes a performance issue (unlikely) we'll want to look at a more efficient way.
*/
- for (i = 0; i < blockSize; ++i) {
- pDecodedSamples[i] = sample;
+ if (pDecodedSamplesS32 != NULL) {
+ drflac_int32 sample;
+ if (!drflac__read_int32(bs, subframeBitsPerSample, &sample)) {
+ return DRFLAC_FALSE;
+ }
+
+ for (i = 0; i < blockSize; ++i) {
+ pDecodedSamplesS32[i] = sample;
+ }
+ } else {
+ drflac_int64 sample;
+ if (!drflac__read_int64(bs, subframeBitsPerSample, &sample)) {
+ return DRFLAC_FALSE;
+ }
+
+ for (i = 0; i < blockSize; ++i) {
+ pDecodedSamplesS64[i] = sample;
+ }
}
return DRFLAC_TRUE;
}
-static drflac_bool32 drflac__decode_samples__verbatim(drflac_bs* bs, drflac_uint32 blockSize, drflac_uint32 subframeBitsPerSample, drflac_int32* pDecodedSamples)
+static drflac_bool32 drflac__decode_samples__verbatim(drflac_bs* bs, drflac_uint32 blockSize, drflac_uint32 subframeBitsPerSample, drflac_int32* pDecodedSamplesS32, drflac_int64* pDecodedSamplesS64)
{
drflac_uint32 i;
- for (i = 0; i < blockSize; ++i) {
- drflac_int32 sample;
- if (!drflac__read_int32(bs, subframeBitsPerSample, &sample)) {
- return DRFLAC_FALSE;
- }
+ DRFLAC_ASSERT(pDecodedSamplesS32 != NULL || pDecodedSamplesS64 != NULL);
+ DRFLAC_ASSERT(pDecodedSamplesS32 == NULL || pDecodedSamplesS64 == NULL);
- pDecodedSamples[i] = sample;
+ if (pDecodedSamplesS32 != NULL) {
+ for (i = 0; i < blockSize; ++i) {
+ if (!drflac__read_int32(bs, subframeBitsPerSample, pDecodedSamplesS32 + i)) {
+ return DRFLAC_FALSE;
+ }
+ }
+ } else {
+ for (i = 0; i < blockSize; ++i) {
+ if (!drflac__read_int64(bs, subframeBitsPerSample, pDecodedSamplesS64 + i)) {
+ return DRFLAC_FALSE;
+ }
+ }
}
return DRFLAC_TRUE;
}
-static drflac_bool32 drflac__decode_samples__fixed(drflac_bs* bs, drflac_uint32 blockSize, drflac_uint32 subframeBitsPerSample, drflac_uint8 lpcOrder, drflac_int32* pDecodedSamples)
+static drflac_bool32 drflac__decode_samples__fixed(drflac_bs* bs, drflac_uint32 blockSize, drflac_uint32 subframeBitsPerSample, drflac_uint8 lpcOrder, drflac_int32* pDecodedSamplesS32, drflac_int64* pDecodedSamplesS64)
{
drflac_uint32 i;
@@ -5065,38 +5238,54 @@ static drflac_bool32 drflac__decode_samples__fixed(drflac_bs* bs, drflac_uint32
{4, -6, 4, -1}
};
- /* Warm up samples and coefficients. */
- for (i = 0; i < lpcOrder; ++i) {
- drflac_int32 sample;
- if (!drflac__read_int32(bs, subframeBitsPerSample, &sample)) {
- return DRFLAC_FALSE;
- }
+ DRFLAC_ASSERT(pDecodedSamplesS32 != NULL || pDecodedSamplesS64 != NULL);
+ DRFLAC_ASSERT(pDecodedSamplesS32 == NULL || pDecodedSamplesS64 == NULL);
- pDecodedSamples[i] = sample;
+ /* Warm up samples. */
+ if (pDecodedSamplesS32 != NULL) {
+ for (i = 0; i < lpcOrder; ++i) {
+ if (!drflac__read_int32(bs, subframeBitsPerSample, pDecodedSamplesS32 + i)) {
+ return DRFLAC_FALSE;
+ }
+ }
+ } else {
+ for (i = 0; i < lpcOrder; ++i) {
+ if (!drflac__read_int64(bs, subframeBitsPerSample, pDecodedSamplesS64 + i)) {
+ return DRFLAC_FALSE;
+ }
+ }
}
- if (!drflac__decode_samples_with_residual(bs, subframeBitsPerSample, blockSize, lpcOrder, 0, 4, lpcCoefficientsTable[lpcOrder], pDecodedSamples)) {
+ if (!drflac__decode_samples_with_residual(bs, subframeBitsPerSample, blockSize, lpcOrder, 0, 4, lpcCoefficientsTable[lpcOrder], pDecodedSamplesS32, pDecodedSamplesS64)) {
return DRFLAC_FALSE;
}
return DRFLAC_TRUE;
}
-static drflac_bool32 drflac__decode_samples__lpc(drflac_bs* bs, drflac_uint32 blockSize, drflac_uint32 bitsPerSample, drflac_uint8 lpcOrder, drflac_int32* pDecodedSamples)
+static drflac_bool32 drflac__decode_samples__lpc(drflac_bs* bs, drflac_uint32 blockSize, drflac_uint32 bitsPerSample, drflac_uint8 lpcOrder, drflac_int32* pDecodedSamplesS32, drflac_int64* pDecodedSamplesS64)
{
drflac_uint8 i;
drflac_uint8 lpcPrecision;
drflac_int8 lpcShift;
drflac_int32 coefficients[32];
+ DRFLAC_ASSERT(pDecodedSamplesS32 != NULL || pDecodedSamplesS64 != NULL);
+ DRFLAC_ASSERT(pDecodedSamplesS32 == NULL || pDecodedSamplesS64 == NULL);
+
/* Warm up samples. */
- for (i = 0; i < lpcOrder; ++i) {
- drflac_int32 sample;
- if (!drflac__read_int32(bs, bitsP
(Patch may be truncated, please check the link at the top of this post.)