external/the-forge/Common_3/ThirdParty/OpenSource/soloud20181119/src/audiosource/wav/dr_flac.h
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)
{
    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_uint8 residualMethod;
    if (!drflac__read_uint8(bs, 2, &residualMethod)) {
        return DRFLAC_FALSE;
    }

    if (residualMethod != DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE && residualMethod != DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2) {
        return DRFLAC_FALSE;    // Unknown or unsupported residual coding method.
    }

    // Ignore the first <order> values.
    pDecodedSamples += order;


    drflac_uint8 partitionOrder;
    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.
    if (partitionOrder > 8) {
        return DRFLAC_FALSE;
    }

    // Validation check.
    if ((blockSize / (1 << partitionOrder)) <= order) {
        return DRFLAC_FALSE;
    }

    drflac_uint32 samplesInPartition = (blockSize / (1 << partitionOrder)) - order;
    drflac_uint32 partitionsRemaining = (1 << partitionOrder);
    for (;;) {
        drflac_uint8 riceParam = 0;
        if (residualMethod == DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE) {
            if (!drflac__read_uint8(bs, 4, &riceParam)) {
                return DRFLAC_FALSE;
            }
            if (riceParam == 15) {
                riceParam = 0xFF;
            }
        } else if (residualMethod == DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2) {
            if (!drflac__read_uint8(bs, 5, &riceParam)) {
                return DRFLAC_FALSE;
            }
            if (riceParam == 31) {
                riceParam = 0xFF;
            }
        }

        if (riceParam != 0xFF) {
            if (!drflac__decode_samples_with_residual__rice(bs, bitsPerSample, samplesInPartition, riceParam, order, shift, coefficients, pDecodedSamples)) {
                return DRFLAC_FALSE;
            }
        } else {
            unsigned char unencodedBitsPerSample = 0;
            if (!drflac__read_uint8(bs, 5, &unencodedBitsPerSample)) {
                return DRFLAC_FALSE;
            }

            if (!drflac__decode_samples_with_residual__unencoded(bs, bitsPerSample, samplesInPartition, unencodedBitsPerSample, order, shift, coefficients, pDecodedSamples)) {
                return DRFLAC_FALSE;
            }
        }

        pDecodedSamples += samplesInPartition;


        if (partitionsRemaining == 1) {
            break;
        }

        partitionsRemaining -= 1;

        if (partitionOrder != 0) {
            samplesInPartition = blockSize / (1 << partitionOrder);
        }
    }

    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.
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  residuals will be ignored. The
 and  parameters are used to determine how many residual values need to be decoded.
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 residuals will be ignored . The and 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 order, drflac_int32 shift, const drflac_int32* coefficients, drflac_int32* pDecodedSamples)
{
    drflac_assert(bs != NULL);
    drflac_assert(blockSize != 0);
    drflac_assert(pDecodedSamples != NULL);       
    drflac_uint8 residualMethod;
    if (!drflac__read_uint8(bs, 2, &residualMethod)) {
        return DRFLAC_FALSE;
    }
    if (residualMethod != DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE && residualMethod != DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2) {
        return DRFLAC_FALSE;    
    }
    pDecodedSamples += order;
    drflac_uint8 partitionOrder;
    if (!drflac__read_uint8(bs, 4, &partitionOrder)) {
        return DRFLAC_FALSE;
    }
    if (partitionOrder > 8) {
        return DRFLAC_FALSE;
    }
    if ((blockSize / (1 << partitionOrder)) <= order) {
        return DRFLAC_FALSE;
    }
    drflac_uint32 samplesInPartition = (blockSize / (1 << partitionOrder)) - order;
    drflac_uint32 partitionsRemaining = (1 << partitionOrder);
    for (;;) {
        drflac_uint8 riceParam = 0;
        if (residualMethod == DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE) {
            if (!drflac__read_uint8(bs, 4, &riceParam)) {
                return DRFLAC_FALSE;
            }
            if (riceParam == 15) {
                riceParam = 0xFF;
            }
        } else if (residualMethod == DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2) {
            if (!drflac__read_uint8(bs, 5, &riceParam)) {
                return DRFLAC_FALSE;
            }
            if (riceParam == 31) {
                riceParam = 0xFF;
            }
        }
        if (riceParam != 0xFF) {
            if (!drflac__decode_samples_with_residual__rice(bs, bitsPerSample, samplesInPartition, riceParam, order, shift, coefficients, pDecodedSamples)) {
                return DRFLAC_FALSE;
            }
        } else {
            unsigned char unencodedBitsPerSample = 0;
            if (!drflac__read_uint8(bs, 5, &unencodedBitsPerSample)) {
                return DRFLAC_FALSE;
            }
            if (!drflac__decode_samples_with_residual__unencoded(bs, bitsPerSample, samplesInPartition, unencodedBitsPerSample, order, shift, coefficients, pDecodedSamples)) {
                return DRFLAC_FALSE;
            }
        }
        pDecodedSamples += samplesInPartition;
        if (partitionsRemaining == 1) {
            break;
        }
        partitionsRemaining -= 1;
        if (partitionOrder != 0) {
            samplesInPartition = blockSize / (1 << partitionOrder);
        }
    }
    return DRFLAC_TRUE;
}
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 ) { drflac_assert ( bs != NULL ) ; drflac_assert ( blockSize != 0 ) ; drflac_assert ( pDecodedSamples != NULL ) ; drflac_uint8 residualMethod ; if ( ! drflac__read_uint8 ( bs , 2 , & residualMethod ) ) { return DRFLAC_FALSE ; } if ( residualMethod != DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE && residualMethod != DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2 ) { return DRFLAC_FALSE ; } pDecodedSamples += order ; drflac_uint8 partitionOrder ; if ( ! drflac__read_uint8 ( bs , 4 , & partitionOrder ) ) { return DRFLAC_FALSE ; } if ( partitionOrder > 8 ) { return DRFLAC_FALSE ; } if ( ( blockSize / ( 1 << partitionOrder ) ) <= order ) { return DRFLAC_FALSE ; } drflac_uint32 samplesInPartition = ( blockSize / ( 1 << partitionOrder ) ) - order ; drflac_uint32 partitionsRemaining = ( 1 << partitionOrder ) ; for ( ; ; ) { drflac_uint8 riceParam = 0 ; if ( residualMethod == DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE ) { if ( ! drflac__read_uint8 ( bs , 4 , & riceParam ) ) { return DRFLAC_FALSE ; } if ( riceParam == 15 ) { riceParam = 0xFF ; } } else if ( residualMethod == DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2 ) { if ( ! drflac__read_uint8 ( bs , 5 , & riceParam ) ) { return DRFLAC_FALSE ; } if ( riceParam == 31 ) { riceParam = 0xFF ; } } if ( riceParam != 0xFF ) { if ( ! drflac__decode_samples_with_residual__rice ( bs , bitsPerSample , samplesInPartition , riceParam , order , shift , coefficients , pDecodedSamples ) ) { return DRFLAC_FALSE ; } } else { unsigned char unencodedBitsPerSample = 0 ; if ( ! drflac__read_uint8 ( bs , 5 , & unencodedBitsPerSample ) ) { return DRFLAC_FALSE ; } if ( ! drflac__decode_samples_with_residual__unencoded ( bs , bitsPerSample , samplesInPartition , unencodedBitsPerSample , order , shift , coefficients , pDecodedSamples ) ) { return DRFLAC_FALSE ; } } pDecodedSamples += samplesInPartition ; if ( partitionsRemaining == 1 ) { break ; } partitionsRemaining -= 1 ; if ( partitionOrder != 0 ) { samplesInPartition = blockSize / ( 1 << partitionOrder ) ; } } return DRFLAC_TRUE ; }
Reads and decodes the residual for the sub-frame the decoder is currently sitting on.
Reads and decodes the residual for the sub - frame the decoder is currently sitting on .
// <-- Should we allow NULL, in which case we just seek past the residual rather than do a full decode? // Unknown or unsupported residual coding method. // Ignore the first <order> values. // From the FLAC spec: //   The Rice partition order in a Rice-coded residual section must be less than or equal to 8. // Validation check.