122 #if defined(_MSC_VER) && _MSC_VER < 1600
144 #define DRFLAC_TRUE 1
145 #define DRFLAC_FALSE 0
147 #if defined(_MSC_VER) && _MSC_VER >= 1700
148 #define DRFLAC_DEPRECATED __declspec(deprecated)
149 #elif (defined(__GNUC__) && __GNUC__ >= 4)
150 #define DRFLAC_DEPRECATED __attribute__((deprecated))
151 #elif defined(__has_feature)
152 #if defined(__has_feature(attribute_deprecated))
153 #define DRFLAC_DEPRECATED __attribute__((deprecated))
155 #define DRFLAC_DEPRECATED
158 #define DRFLAC_DEPRECATED
166 #ifndef DR_FLAC_BUFFER_SIZE
167 #define DR_FLAC_BUFFER_SIZE 4096
175 #if defined(_WIN64) || defined(_LP64) || defined(__LP64__)
186 #define DRFLAC_METADATA_BLOCK_TYPE_STREAMINFO 0
187 #define DRFLAC_METADATA_BLOCK_TYPE_PADDING 1
188 #define DRFLAC_METADATA_BLOCK_TYPE_APPLICATION 2
189 #define DRFLAC_METADATA_BLOCK_TYPE_SEEKTABLE 3
190 #define DRFLAC_METADATA_BLOCK_TYPE_VORBIS_COMMENT 4
191 #define DRFLAC_METADATA_BLOCK_TYPE_CUESHEET 5
192 #define DRFLAC_METADATA_BLOCK_TYPE_PICTURE 6
193 #define DRFLAC_METADATA_BLOCK_TYPE_INVALID 127
196 #define DRFLAC_PICTURE_TYPE_OTHER 0
197 #define DRFLAC_PICTURE_TYPE_FILE_ICON 1
198 #define DRFLAC_PICTURE_TYPE_OTHER_FILE_ICON 2
199 #define DRFLAC_PICTURE_TYPE_COVER_FRONT 3
200 #define DRFLAC_PICTURE_TYPE_COVER_BACK 4
201 #define DRFLAC_PICTURE_TYPE_LEAFLET_PAGE 5
202 #define DRFLAC_PICTURE_TYPE_MEDIA 6
203 #define DRFLAC_PICTURE_TYPE_LEAD_ARTIST 7
204 #define DRFLAC_PICTURE_TYPE_ARTIST 8
205 #define DRFLAC_PICTURE_TYPE_CONDUCTOR 9
206 #define DRFLAC_PICTURE_TYPE_BAND 10
207 #define DRFLAC_PICTURE_TYPE_COMPOSER 11
208 #define DRFLAC_PICTURE_TYPE_LYRICIST 12
209 #define DRFLAC_PICTURE_TYPE_RECORDING_LOCATION 13
210 #define DRFLAC_PICTURE_TYPE_DURING_RECORDING 14
211 #define DRFLAC_PICTURE_TYPE_DURING_PERFORMANCE 15
212 #define DRFLAC_PICTURE_TYPE_SCREEN_CAPTURE 16
213 #define DRFLAC_PICTURE_TYPE_BRIGHT_COLORED_FISH 17
214 #define DRFLAC_PICTURE_TYPE_ILLUSTRATION 18
215 #define DRFLAC_PICTURE_TYPE_BAND_LOGOTYPE 19
216 #define DRFLAC_PICTURE_TYPE_PUBLISHER_LOGOTYPE 20
338 typedef size_t (*
drflac_read_proc)(
void* pUserData,
void* pBufferOut,
size_t bytesToRead);
684 #ifndef DR_FLAC_NO_STDIO
746 #ifndef DR_FLAC_NO_STDIO
856 #ifdef DR_FLAC_IMPLEMENTATION
859 #if defined(__GNUC__)
860 #pragma GCC diagnostic push
862 #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
880 #define DRFLAC_INLINE __forceinline
883 #define DRFLAC_INLINE __inline__ __attribute__((always_inline))
885 #define DRFLAC_INLINE
890 #if defined(__x86_64__) || defined(_M_X64)
892 #elif defined(__i386) || defined(_M_IX86)
894 #elif defined(__arm__) || defined(_M_ARM)
899 #if !defined(DR_FLAC_NO_SIMD)
900 #if defined(DRFLAC_X64) || defined(DRFLAC_X86)
901 #if defined(_MSC_VER) && !defined(__clang__)
903 #if _MSC_VER >= 1400 && !defined(DRFLAC_NO_SSE2)
904 #define DRFLAC_SUPPORT_SSE2
906 #if _MSC_VER >= 1600 && !defined(DRFLAC_NO_SSE41)
907 #define DRFLAC_SUPPORT_SSE41
911 #if defined(__SSE2__) && !defined(DRFLAC_NO_SSE2)
912 #define DRFLAC_SUPPORT_SSE2
914 #if defined(__SSE4_1__) && !defined(DRFLAC_NO_SSE41)
915 #define DRFLAC_SUPPORT_SSE41
920 #if !defined(__GNUC__) && !defined(__clang__) && defined(__has_include)
921 #if !defined(DRFLAC_SUPPORT_SSE2) && !defined(DRFLAC_NO_SSE2) && __has_include(<emmintrin.h>)
922 #define DRFLAC_SUPPORT_SSE2
924 #if !defined(DRFLAC_SUPPORT_SSE41) && !defined(DRFLAC_NO_SSE41) && __has_include(<smmintrin.h>)
925 #define DRFLAC_SUPPORT_SSE41
929 #if defined(DRFLAC_SUPPORT_SSE41)
930 #include <smmintrin.h>
931 #elif defined(DRFLAC_SUPPORT_SSE2)
932 #include <emmintrin.h>
936 #if defined(DRFLAC_ARM)
937 #if !defined(DRFLAC_NO_NEON) && (defined(__ARM_NEON) || defined(__aarch64__) || defined(_M_ARM64))
938 #define DRFLAC_SUPPORT_NEON
942 #if !defined(__GNUC__) && !defined(__clang__) && defined(__has_include)
943 #if !defined(DRFLAC_SUPPORT_NEON) && !defined(DRFLAC_NO_NEON) && __has_include(<arm_neon.h>)
944 #define DRFLAC_SUPPORT_NEON
948 #if defined(DRFLAC_SUPPORT_NEON)
949 #include <arm_neon.h>
955 #if !defined(DR_FLAC_NO_SIMD) && (defined(DRFLAC_X86) || defined(DRFLAC_X64))
956 #if defined(_MSC_VER) && !defined(__clang__)
959 static void drflac__cpuid(
int info[4],
int fid)
964 #define DRFLAC_NO_CPUID
967 #if defined(__GNUC__) || defined(__clang__)
968 static void drflac__cpuid(
int info[4],
int fid)
977 #if defined(DRFLAC_X86) && defined(__PIC__)
978 __asm__ __volatile__ (
979 "xchg{l} {%%}ebx, %k1;"
981 "xchg{l} {%%}ebx, %k1;"
982 :
"=a"(info[0]),
"=&r"(info[1]),
"=c"(info[2]),
"=d"(info[3]) :
"a"(fid),
"c"(0)
985 __asm__ __volatile__ (
986 "cpuid" :
"=a"(info[0]),
"=b"(info[1]),
"=c"(info[2]),
"=d"(info[3]) :
"a"(fid),
"c"(0)
991 #define DRFLAC_NO_CPUID
995 #define DRFLAC_NO_CPUID
1000 #if defined(DRFLAC_SUPPORT_SSE2)
1001 #if (defined(DRFLAC_X64) || defined(DRFLAC_X86)) && !defined(DRFLAC_NO_SSE2)
1002 #if defined(DRFLAC_X64)
1004 #elif (defined(_M_IX86_FP) && _M_IX86_FP == 2) || defined(__SSE2__)
1007 #if defined(DRFLAC_NO_CPUID)
1011 drflac_cpuid(info, 1);
1012 return (info[3] & (1 << 26)) != 0;
1025 #if defined(DRFLAC_SUPPORT_SSE41)
1026 #if (defined(DRFLAC_X64) || defined(DRFLAC_X86)) && !defined(DRFLAC_NO_SSE41)
1027 #if defined(DRFLAC_X64)
1029 #elif (defined(_M_IX86_FP) && _M_IX86_FP == 2) || defined(__SSE4_1__)
1032 #if defined(DRFLAC_NO_CPUID)
1036 drflac_cpuid(info, 1);
1037 return (info[2] & (1 << 19)) != 0;
1049 #if defined(_MSC_VER) && _MSC_VER >= 1500 && (defined(DRFLAC_X86) || defined(DRFLAC_X64))
1050 #define DRFLAC_HAS_LZCNT_INTRINSIC
1051 #elif (defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)))
1052 #define DRFLAC_HAS_LZCNT_INTRINSIC
1053 #elif defined(__clang__)
1054 #if __has_builtin(__builtin_clzll) || __has_builtin(__builtin_clzl)
1055 #define DRFLAC_HAS_LZCNT_INTRINSIC
1059 #if defined(_MSC_VER) && _MSC_VER >= 1300
1060 #define DRFLAC_HAS_BYTESWAP16_INTRINSIC
1061 #define DRFLAC_HAS_BYTESWAP32_INTRINSIC
1062 #define DRFLAC_HAS_BYTESWAP64_INTRINSIC
1063 #elif defined(__clang__)
1064 #if __has_builtin(__builtin_bswap16)
1065 #define DRFLAC_HAS_BYTESWAP16_INTRINSIC
1067 #if __has_builtin(__builtin_bswap32)
1068 #define DRFLAC_HAS_BYTESWAP32_INTRINSIC
1070 #if __has_builtin(__builtin_bswap64)
1071 #define DRFLAC_HAS_BYTESWAP64_INTRINSIC
1073 #elif defined(__GNUC__)
1074 #if ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
1075 #define DRFLAC_HAS_BYTESWAP32_INTRINSIC
1076 #define DRFLAC_HAS_BYTESWAP64_INTRINSIC
1078 #if ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))
1079 #define DRFLAC_HAS_BYTESWAP16_INTRINSIC
1085 #ifndef DRFLAC_ASSERT
1087 #define DRFLAC_ASSERT(expression) assert(expression)
1089 #ifndef DRFLAC_MALLOC
1090 #define DRFLAC_MALLOC(sz) malloc((sz))
1092 #ifndef DRFLAC_REALLOC
1093 #define DRFLAC_REALLOC(p, sz) realloc((p), (sz))
1096 #define DRFLAC_FREE(p) free((p))
1098 #ifndef DRFLAC_COPY_MEMORY
1099 #define DRFLAC_COPY_MEMORY(dst, src, sz) memcpy((dst), (src), (sz))
1101 #ifndef DRFLAC_ZERO_MEMORY
1102 #define DRFLAC_ZERO_MEMORY(p, sz) memset((p), 0, (sz))
1105 #define DRFLAC_MAX_SIMD_VECTOR_SIZE 64
1108 #define DRFLAC_SUCCESS 0
1109 #define DRFLAC_ERROR -1
1110 #define DRFLAC_INVALID_ARGS -2
1111 #define DRFLAC_END_OF_STREAM -128
1112 #define DRFLAC_CRC_MISMATCH -129
1114 #define DRFLAC_SUBFRAME_CONSTANT 0
1115 #define DRFLAC_SUBFRAME_VERBATIM 1
1116 #define DRFLAC_SUBFRAME_FIXED 8
1117 #define DRFLAC_SUBFRAME_LPC 32
1118 #define DRFLAC_SUBFRAME_RESERVED 255
1120 #define DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE 0
1121 #define DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2 1
1123 #define DRFLAC_CHANNEL_ASSIGNMENT_INDEPENDENT 0
1124 #define DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE 8
1125 #define DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE 9
1126 #define DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE 10
1132 #define DRFLAC_LEADING_SAMPLES 32
1135 #define drflac_align(x, a) ((((x) + (a) - 1) / (a)) * (a))
1136 #define drflac_assert DRFLAC_ASSERT
1137 #define drflac_copy_memory DRFLAC_COPY_MEMORY
1138 #define drflac_zero_memory DRFLAC_ZERO_MEMORY
1143 #ifndef DRFLAC_NO_CPUID
1146 static void drflac__init_cpu_caps()
1150 if (!isCPUCapsInitialized) {
1154 drflac__cpuid(info, 0x80000001);
1155 drflac__gIsLZCNTSupported = (info[2] & (1 << 5)) != 0;
1158 drflac__gIsSSE2Supported = drflac_has_sse2();
1161 drflac__gIsSSE41Supported = drflac_has_sse41();
1171 static DRFLAC_INLINE
drflac_bool32 drflac__is_little_endian()
1173 #if defined(DRFLAC_X86) || defined(DRFLAC_X64)
1175 #elif defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN
1179 return (*(
char*)&n) == 1;
1185 #ifdef DRFLAC_HAS_BYTESWAP16_INTRINSIC
1186 #if defined(_MSC_VER)
1187 return _byteswap_ushort(n);
1188 #elif defined(__GNUC__) || defined(__clang__)
1189 return __builtin_bswap16(n);
1191 #error "This compiler does not support the byte swap intrinsic."
1194 return ((n & 0xFF00) >> 8) |
1195 ((n & 0x00FF) << 8);
1201 #ifdef DRFLAC_HAS_BYTESWAP32_INTRINSIC
1202 #if defined(_MSC_VER)
1203 return _byteswap_ulong(n);
1204 #elif defined(__GNUC__) || defined(__clang__)
1205 return __builtin_bswap32(n);
1207 #error "This compiler does not support the byte swap intrinsic."
1210 return ((n & 0xFF000000) >> 24) |
1211 ((n & 0x00FF0000) >> 8) |
1212 ((n & 0x0000FF00) << 8) |
1213 ((n & 0x000000FF) << 24);
1219 #ifdef DRFLAC_HAS_BYTESWAP64_INTRINSIC
1220 #if defined(_MSC_VER)
1221 return _byteswap_uint64(n);
1222 #elif defined(__GNUC__) || defined(__clang__)
1223 return __builtin_bswap64(n);
1225 #error "This compiler does not support the byte swap intrinsic."
1242 if (drflac__is_little_endian()) {
1243 return drflac__swap_endian_uint16(n);
1251 if (drflac__is_little_endian()) {
1252 return drflac__swap_endian_uint32(n);
1260 if (drflac__is_little_endian()) {
1261 return drflac__swap_endian_uint64(n);
1270 if (!drflac__is_little_endian()) {
1271 return drflac__swap_endian_uint32(n);
1281 result |= (n & 0x7F000000) >> 3;
1282 result |= (n & 0x007F0000) >> 2;
1283 result |= (n & 0x00007F00) >> 1;
1284 result |= (n & 0x0000007F) >> 0;
1293 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D,
1294 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D,
1295 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD,
1296 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD,
1297 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA,
1298 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A,
1299 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A,
1300 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A,
1301 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4,
1302 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4,
1303 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44,
1304 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34,
1305 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63,
1306 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13,
1307 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,
1308 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3
1312 0x0000, 0x8005, 0x800F, 0x000A, 0x801B, 0x001E, 0x0014, 0x8011,
1313 0x8033, 0x0036, 0x003C, 0x8039, 0x0028, 0x802D, 0x8027, 0x0022,
1314 0x8063, 0x0066, 0x006C, 0x8069, 0x0078, 0x807D, 0x8077, 0x0072,
1315 0x0050, 0x8055, 0x805F, 0x005A, 0x804B, 0x004E, 0x0044, 0x8041,
1316 0x80C3, 0x00C6, 0x00CC, 0x80C9, 0x00D8, 0x80DD, 0x80D7, 0x00D2,
1317 0x00F0, 0x80F5, 0x80FF, 0x00FA, 0x80EB, 0x00EE, 0x00E4, 0x80E1,
1318 0x00A0, 0x80A5, 0x80AF, 0x00AA, 0x80BB, 0x00BE, 0x00B4, 0x80B1,
1319 0x8093, 0x0096, 0x009C, 0x8099, 0x0088, 0x808D, 0x8087, 0x0082,
1320 0x8183, 0x0186, 0x018C, 0x8189, 0x0198, 0x819D, 0x8197, 0x0192,
1321 0x01B0, 0x81B5, 0x81BF, 0x01BA, 0x81AB, 0x01AE, 0x01A4, 0x81A1,
1322 0x01E0, 0x81E5, 0x81EF, 0x01EA, 0x81FB, 0x01FE, 0x01F4, 0x81F1,
1323 0x81D3, 0x01D6, 0x01DC, 0x81D9, 0x01C8, 0x81CD, 0x81C7, 0x01C2,
1324 0x0140, 0x8145, 0x814F, 0x014A, 0x815B, 0x015E, 0x0154, 0x8151,
1325 0x8173, 0x0176, 0x017C, 0x8179, 0x0168, 0x816D, 0x8167, 0x0162,
1326 0x8123, 0x0126, 0x012C, 0x8129, 0x0138, 0x813D, 0x8137, 0x0132,
1327 0x0110, 0x8115, 0x811F, 0x011A, 0x810B, 0x010E, 0x0104, 0x8101,
1328 0x8303, 0x0306, 0x030C, 0x8309, 0x0318, 0x831D, 0x8317, 0x0312,
1329 0x0330, 0x8335, 0x833F, 0x033A, 0x832B, 0x032E, 0x0324, 0x8321,
1330 0x0360, 0x8365, 0x836F, 0x036A, 0x837B, 0x037E, 0x0374, 0x8371,
1331 0x8353, 0x0356, 0x035C, 0x8359, 0x0348, 0x834D, 0x8347, 0x0342,
1332 0x03C0, 0x83C5, 0x83CF, 0x03CA, 0x83DB, 0x03DE, 0x03D4, 0x83D1,
1333 0x83F3, 0x03F6, 0x03FC, 0x83F9, 0x03E8, 0x83ED, 0x83E7, 0x03E2,
1334 0x83A3, 0x03A6, 0x03AC, 0x83A9, 0x03B8, 0x83BD, 0x83B7, 0x03B2,
1335 0x0390, 0x8395, 0x839F, 0x039A, 0x838B, 0x038E, 0x0384, 0x8381,
1336 0x0280, 0x8285, 0x828F, 0x028A, 0x829B, 0x029E, 0x0294, 0x8291,
1337 0x82B3, 0x02B6, 0x02BC, 0x82B9, 0x02A8, 0x82AD, 0x82A7, 0x02A2,
1338 0x82E3, 0x02E6, 0x02EC, 0x82E9, 0x02F8, 0x82FD, 0x82F7, 0x02F2,
1339 0x02D0, 0x82D5, 0x82DF, 0x02DA, 0x82CB, 0x02CE, 0x02C4, 0x82C1,
1340 0x8243, 0x0246, 0x024C, 0x8249, 0x0258, 0x825D, 0x8257, 0x0252,
1341 0x0270, 0x8275, 0x827F, 0x027A, 0x826B, 0x026E, 0x0264, 0x8261,
1342 0x0220, 0x8225, 0x822F, 0x022A, 0x823B, 0x023E, 0x0234, 0x8231,
1343 0x8213, 0x0216, 0x021C, 0x8219, 0x0208, 0x820D, 0x8207, 0x0202
1348 return drflac__crc8_table[crc ^ data];
1353 #ifdef DR_FLAC_NO_CRC
1362 for (
int i =
count-1;
i >= 0; --
i) {
1365 crc = ((crc << 1) | bit) ^ p;
1367 crc = ((crc << 1) | bit);
1377 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F
1380 drflac_assert(
count <= 32);
1382 wholeBytes =
count >> 3;
1383 leftoverBits =
count - (wholeBytes*8);
1384 leftoverDataMask = leftoverDataMaskTable[leftoverBits];
1386 switch (wholeBytes) {
1387 case 4: crc = drflac_crc8_byte(crc, (
drflac_uint8)((data & (0xFF000000UL << leftoverBits)) >> (24 + leftoverBits)));
1388 case 3: crc = drflac_crc8_byte(crc, (
drflac_uint8)((data & (0x00FF0000UL << leftoverBits)) >> (16 + leftoverBits)));
1389 case 2: crc = drflac_crc8_byte(crc, (
drflac_uint8)((data & (0x0000FF00UL << leftoverBits)) >> ( 8 + leftoverBits)));
1390 case 1: crc = drflac_crc8_byte(crc, (
drflac_uint8)((data & (0x000000FFUL << leftoverBits)) >> ( 0 + leftoverBits)));
1391 case 0:
if (leftoverBits > 0) crc = (crc << leftoverBits) ^ drflac__crc8_table[(crc >> (8 - leftoverBits)) ^ (data & leftoverDataMask)];
1400 return (crc << 8) ^ drflac__crc16_table[(
drflac_uint8)(crc >> 8) ^ data];
1408 case 8: crc = drflac_crc16_byte(crc, (
drflac_uint8)((data >> 56) & 0xFF));
1409 case 7: crc = drflac_crc16_byte(crc, (
drflac_uint8)((data >> 48) & 0xFF));
1410 case 6: crc = drflac_crc16_byte(crc, (
drflac_uint8)((data >> 40) & 0xFF));
1411 case 5: crc = drflac_crc16_byte(crc, (
drflac_uint8)((data >> 32) & 0xFF));
1413 case 4: crc = drflac_crc16_byte(crc, (
drflac_uint8)((data >> 24) & 0xFF));
1414 case 3: crc = drflac_crc16_byte(crc, (
drflac_uint8)((data >> 16) & 0xFF));
1415 case 2: crc = drflac_crc16_byte(crc, (
drflac_uint8)((data >> 8) & 0xFF));
1416 case 1: crc = drflac_crc16_byte(crc, (
drflac_uint8)((data >> 0) & 0xFF));
1424 #ifdef DR_FLAC_NO_CRC
1433 for (
int i =
count-1;
i >= 0; --
i) {
1436 r = ((
r << 1) | bit) ^ p;
1438 r = ((
r << 1) | bit);
1449 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F
1452 drflac_assert(
count <= 64);
1454 wholeBytes =
count >> 3;
1455 leftoverBits =
count - (wholeBytes*8);
1456 leftoverDataMask = leftoverDataMaskTable[leftoverBits];
1458 switch (wholeBytes) {
1460 case 4: crc = drflac_crc16_byte(crc, (
drflac_uint8)((data & (0xFF000000UL << leftoverBits)) >> (24 + leftoverBits)));
1461 case 3: crc = drflac_crc16_byte(crc, (
drflac_uint8)((data & (0x00FF0000UL << leftoverBits)) >> (16 + leftoverBits)));
1462 case 2: crc = drflac_crc16_byte(crc, (
drflac_uint8)((data & (0x0000FF00UL << leftoverBits)) >> ( 8 + leftoverBits)));
1463 case 1: crc = drflac_crc16_byte(crc, (
drflac_uint8)((data & (0x000000FFUL << leftoverBits)) >> ( 0 + leftoverBits)));
1464 case 0:
if (leftoverBits > 0) crc = (crc << leftoverBits) ^ drflac__crc16_table[(crc >> (16 - leftoverBits)) ^ (data & leftoverDataMask)];
1473 #ifdef DR_FLAC_NO_CRC
1484 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F
1487 drflac_assert(
count <= 64);
1489 wholeBytes =
count >> 3;
1490 leftoverBits =
count - (wholeBytes*8);
1491 leftoverDataMask = leftoverDataMaskTable[leftoverBits];
1493 switch (wholeBytes) {
1495 case 8: crc = drflac_crc16_byte(crc, (
drflac_uint8)((data & (((
drflac_uint64)0xFF000000 << 32) << leftoverBits)) >> (56 + leftoverBits)));
1496 case 7: crc = drflac_crc16_byte(crc, (
drflac_uint8)((data & (((
drflac_uint64)0x00FF0000 << 32) << leftoverBits)) >> (48 + leftoverBits)));
1497 case 6: crc = drflac_crc16_byte(crc, (
drflac_uint8)((data & (((
drflac_uint64)0x0000FF00 << 32) << leftoverBits)) >> (40 + leftoverBits)));
1498 case 5: crc = drflac_crc16_byte(crc, (
drflac_uint8)((data & (((
drflac_uint64)0x000000FF << 32) << leftoverBits)) >> (32 + leftoverBits)));
1499 case 4: crc = drflac_crc16_byte(crc, (
drflac_uint8)((data & (((
drflac_uint64)0xFF000000 ) << leftoverBits)) >> (24 + leftoverBits)));
1500 case 3: crc = drflac_crc16_byte(crc, (
drflac_uint8)((data & (((
drflac_uint64)0x00FF0000 ) << leftoverBits)) >> (16 + leftoverBits)));
1501 case 2: crc = drflac_crc16_byte(crc, (
drflac_uint8)((data & (((
drflac_uint64)0x0000FF00 ) << leftoverBits)) >> ( 8 + leftoverBits)));
1502 case 1: crc = drflac_crc16_byte(crc, (
drflac_uint8)((data & (((
drflac_uint64)0x000000FF ) << leftoverBits)) >> ( 0 + leftoverBits)));
1503 case 0:
if (leftoverBits > 0) crc = (crc << leftoverBits) ^ drflac__crc16_table[(crc >> (16 - leftoverBits)) ^ (data & leftoverDataMask)];
1513 return drflac_crc16__64bit(crc, data,
count);
1515 return drflac_crc16__32bit(crc, data,
count);
1521 #define drflac__be2host__cache_line drflac__be2host_64
1523 #define drflac__be2host__cache_line drflac__be2host_32
1535 #define DRFLAC_CACHE_L1_SIZE_BYTES(bs) (sizeof((bs)->cache))
1536 #define DRFLAC_CACHE_L1_SIZE_BITS(bs) (sizeof((bs)->cache)*8)
1537 #define DRFLAC_CACHE_L1_BITS_REMAINING(bs) (DRFLAC_CACHE_L1_SIZE_BITS(bs) - (bs)->consumedBits)
1538 #define DRFLAC_CACHE_L1_SELECTION_MASK(_bitCount) (~((~(drflac_cache_t)0) >> (_bitCount)))
1539 #define DRFLAC_CACHE_L1_SELECTION_SHIFT(bs, _bitCount) (DRFLAC_CACHE_L1_SIZE_BITS(bs) - (_bitCount))
1540 #define DRFLAC_CACHE_L1_SELECT(bs, _bitCount) (((bs)->cache) & DRFLAC_CACHE_L1_SELECTION_MASK(_bitCount))
1541 #define DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, _bitCount) (DRFLAC_CACHE_L1_SELECT((bs), (_bitCount)) >> DRFLAC_CACHE_L1_SELECTION_SHIFT((bs), (_bitCount)))
1542 #define DRFLAC_CACHE_L1_SELECT_AND_SHIFT_SAFE(bs, _bitCount)(DRFLAC_CACHE_L1_SELECT((bs), (_bitCount)) >> (DRFLAC_CACHE_L1_SELECTION_SHIFT((bs), (_bitCount)) & (DRFLAC_CACHE_L1_SIZE_BITS(bs)-1)))
1543 #define DRFLAC_CACHE_L2_SIZE_BYTES(bs) (sizeof((bs)->cacheL2))
1544 #define DRFLAC_CACHE_L2_LINE_COUNT(bs) (DRFLAC_CACHE_L2_SIZE_BYTES(bs) / sizeof((bs)->cacheL2[0]))
1545 #define DRFLAC_CACHE_L2_LINES_REMAINING(bs) (DRFLAC_CACHE_L2_LINE_COUNT(bs) - (bs)->nextL2Line)
1548 #ifndef DR_FLAC_NO_CRC
1549 static DRFLAC_INLINE
void drflac__reset_crc16(
drflac_bs* bs)
1555 static DRFLAC_INLINE
void drflac__update_crc16(
drflac_bs* bs)
1564 drflac_assert((DRFLAC_CACHE_L1_BITS_REMAINING(bs) & 7) == 0);
1570 if (DRFLAC_CACHE_L1_BITS_REMAINING(bs) == 0) {
1571 drflac__update_crc16(bs);
1590 size_t alignedL1LineCount;
1593 if (bs->
nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) {
1609 if (bytesRead == DRFLAC_CACHE_L2_SIZE_BYTES(bs)) {
1621 alignedL1LineCount = bytesRead / DRFLAC_CACHE_L1_SIZE_BYTES(bs);
1624 bs->
unalignedByteCount = bytesRead - (alignedL1LineCount * DRFLAC_CACHE_L1_SIZE_BYTES(bs));
1629 if (alignedL1LineCount > 0) {
1630 size_t offset = DRFLAC_CACHE_L2_LINE_COUNT(bs) - alignedL1LineCount;
1632 for (
i = alignedL1LineCount;
i > 0; --
i) {
1641 bs->
nextL2Line = DRFLAC_CACHE_L2_LINE_COUNT(bs);
1650 #ifndef DR_FLAC_NO_CRC
1651 drflac__update_crc16(bs);
1655 if (drflac__reload_l1_cache_from_l2(bs)) {
1656 bs->
cache = drflac__be2host__cache_line(bs->
cache);
1658 #ifndef DR_FLAC_NO_CRC
1672 if (bytesRead == 0) {
1677 drflac_assert(bytesRead < DRFLAC_CACHE_L1_SIZE_BYTES(bs));
1681 bs->
cache &= DRFLAC_CACHE_L1_SELECTION_MASK(DRFLAC_CACHE_L1_BITS_REMAINING(bs));
1684 #ifndef DR_FLAC_NO_CRC
1691 static void drflac__reset_cache(
drflac_bs* bs)
1693 bs->
nextL2Line = DRFLAC_CACHE_L2_LINE_COUNT(bs);
1699 #ifndef DR_FLAC_NO_CRC
1708 drflac_assert(bs !=
NULL);
1709 drflac_assert(pResultOut !=
NULL);
1710 drflac_assert(bitCount > 0);
1711 drflac_assert(bitCount <= 32);
1713 if (bs->
consumedBits == DRFLAC_CACHE_L1_SIZE_BITS(bs)) {
1714 if (!drflac__reload_cache(bs)) {
1719 if (bitCount <= DRFLAC_CACHE_L1_BITS_REMAINING(bs)) {
1726 *pResultOut = (
drflac_uint32)DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, bitCount);
1728 bs->
cache <<= bitCount;
1730 if (bitCount < DRFLAC_CACHE_L1_SIZE_BITS(bs)) {
1731 *pResultOut = (
drflac_uint32)DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, bitCount);
1733 bs->
cache <<= bitCount;
1745 drflac_uint32 bitCountHi = DRFLAC_CACHE_L1_BITS_REMAINING(bs);
1749 if (!drflac__reload_cache(bs)) {
1753 *pResultOut = (resultHi << bitCountLo) | (
drflac_uint32)DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, bitCountLo);
1755 bs->
cache <<= bitCountLo;
1765 drflac_assert(bs !=
NULL);
1766 drflac_assert(pResult !=
NULL);
1767 drflac_assert(bitCount > 0);
1768 drflac_assert(bitCount <= 32);
1770 if (!drflac__read_uint32(bs, bitCount, &result)) {
1774 signbit = ((result >> (bitCount-1)) & 0x01);
1775 result |= (~signbit + 1) << bitCount;
1787 drflac_assert(bitCount <= 64);
1788 drflac_assert(bitCount > 32);
1790 if (!drflac__read_uint32(bs, bitCount - 32, &resultHi)) {
1794 if (!drflac__read_uint32(bs, 32, &resultLo)) {
1810 drflac_assert(bitCount <= 64);
1812 if (!drflac__read_uint64(bs, bitCount, &result)) {
1816 signbit = ((result >> (bitCount-1)) & 0x01);
1817 result |= (~signbit + 1) << bitCount;
1828 drflac_assert(bs !=
NULL);
1829 drflac_assert(pResult !=
NULL);
1830 drflac_assert(bitCount > 0);
1831 drflac_assert(bitCount <= 16);
1833 if (!drflac__read_uint32(bs, bitCount, &result)) {
1846 drflac_assert(bs !=
NULL);
1847 drflac_assert(pResult !=
NULL);
1848 drflac_assert(bitCount > 0);
1849 drflac_assert(bitCount <= 16);
1851 if (!drflac__read_int32(bs, bitCount, &result)) {
1864 drflac_assert(bs !=
NULL);
1865 drflac_assert(pResult !=
NULL);
1866 drflac_assert(bitCount > 0);
1867 drflac_assert(bitCount <= 8);
1869 if (!drflac__read_uint32(bs, bitCount, &result)) {
1881 drflac_assert(bs !=
NULL);
1882 drflac_assert(pResult !=
NULL);
1883 drflac_assert(bitCount > 0);
1884 drflac_assert(bitCount <= 8);
1886 if (!drflac__read_int32(bs, bitCount, &result)) {
1897 if (bitsToSeek <= DRFLAC_CACHE_L1_BITS_REMAINING(bs)) {
1899 bs->
cache <<= bitsToSeek;
1903 bitsToSeek -= DRFLAC_CACHE_L1_BITS_REMAINING(bs);
1904 bs->
consumedBits += DRFLAC_CACHE_L1_BITS_REMAINING(bs);
1909 while (bitsToSeek >= DRFLAC_CACHE_L1_SIZE_BITS(bs)) {
1911 if (!drflac__read_uint64(bs, DRFLAC_CACHE_L1_SIZE_BITS(bs), &bin)) {
1914 bitsToSeek -= DRFLAC_CACHE_L1_SIZE_BITS(bs);
1917 while (bitsToSeek >= DRFLAC_CACHE_L1_SIZE_BITS(bs)) {
1919 if (!drflac__read_uint32(bs, DRFLAC_CACHE_L1_SIZE_BITS(bs), &bin)) {
1922 bitsToSeek -= DRFLAC_CACHE_L1_SIZE_BITS(bs);
1927 while (bitsToSeek >= 8) {
1929 if (!drflac__read_uint8(bs, 8, &bin)) {
1936 if (bitsToSeek > 0) {
1938 if (!drflac__read_uint8(bs, (
drflac_uint32)bitsToSeek, &bin)) {
1944 drflac_assert(bitsToSeek == 0);
1953 drflac_assert(bs !=
NULL);
1959 if (!drflac__seek_bits(bs, DRFLAC_CACHE_L1_BITS_REMAINING(bs) & 7)) {
1966 #ifndef DR_FLAC_NO_CRC
1967 drflac__reset_crc16(bs);
1970 if (!drflac__read_uint8(bs, 8, &hi)) {
1976 if (!drflac__read_uint8(bs, 6, &lo)) {
1983 if (!drflac__seek_bits(bs, DRFLAC_CACHE_L1_BITS_REMAINING(bs) & 7)) {
1995 #if !defined(DR_FLAC_NO_SIMD) && defined(DRFLAC_HAS_LZCNT_INTRINSIC)
1996 #define DRFLAC_IMPLEMENT_CLZ_LZCNT
1998 #if defined(_MSC_VER) && _MSC_VER >= 1400 && (defined(DRFLAC_X64) || defined(DRFLAC_X86))
1999 #define DRFLAC_IMPLEMENT_CLZ_MSVC
2010 1, 1, 1, 1, 1, 1, 1, 1
2017 n = clz_table_4[
x >> (
sizeof(
x)*8 - 4)];
2020 if ((
x & ((
drflac_uint64)0xFFFFFFFF << 32)) == 0) { n = 32;
x <<= 32; }
2021 if ((
x & ((
drflac_uint64)0xFFFF0000 << 32)) == 0) { n += 16;
x <<= 16; }
2022 if ((
x & ((
drflac_uint64)0xFF000000 << 32)) == 0) { n += 8;
x <<= 8; }
2023 if ((
x & ((
drflac_uint64)0xF0000000 << 32)) == 0) { n += 4;
x <<= 4; }
2025 if ((
x & 0xFFFF0000) == 0) { n = 16;
x <<= 16; }
2026 if ((
x & 0xFF000000) == 0) { n += 8;
x <<= 8; }
2027 if ((
x & 0xF0000000) == 0) { n += 4;
x <<= 4; }
2029 n += clz_table_4[
x >> (
sizeof(
x)*8 - 4)];
2035 #ifdef DRFLAC_IMPLEMENT_CLZ_LZCNT
2036 static DRFLAC_INLINE
drflac_bool32 drflac__is_lzcnt_supported()
2039 #ifdef DRFLAC_HAS_LZCNT_INTRINSIC
2040 return drflac__gIsLZCNTSupported;
2048 #if defined(_MSC_VER) && !defined(__clang__)
2055 #if defined(__GNUC__) || defined(__clang__)
2066 #error "This compiler does not support the lzcnt intrinsic."
2072 #ifdef DRFLAC_IMPLEMENT_CLZ_MSVC
2084 _BitScanReverse64((
unsigned long*)&n,
x);
2086 _BitScanReverse((
unsigned long*)&n,
x);
2088 return sizeof(
x)*8 - n - 1;
2094 #ifdef DRFLAC_IMPLEMENT_CLZ_LZCNT
2095 if (drflac__is_lzcnt_supported()) {
2096 return drflac__clz_lzcnt(
x);
2100 #ifdef DRFLAC_IMPLEMENT_CLZ_MSVC
2101 return drflac__clz_msvc(
x);
2103 return drflac__clz_software(
x);
2109 static DRFLAC_INLINE
drflac_bool32 drflac__seek_past_next_set_bit(
drflac_bs* bs,
unsigned int* pOffsetOut)
2114 while (bs->
cache == 0) {
2115 zeroCounter += (
drflac_uint32)DRFLAC_CACHE_L1_BITS_REMAINING(bs);
2116 if (!drflac__reload_cache(bs)) {
2121 setBitOffsetPlus1 = drflac__clz(bs->
cache);
2122 setBitOffsetPlus1 += 1;
2125 bs->
cache <<= setBitOffsetPlus1;
2127 *pOffsetOut = zeroCounter + setBitOffsetPlus1 - 1;
2135 drflac_assert(bs !=
NULL);
2136 drflac_assert(offsetFromStart > 0);
2143 if (offsetFromStart > 0x7FFFFFFF) {
2148 bytesRemaining -= 0x7FFFFFFF;
2150 while (bytesRemaining > 0x7FFFFFFF) {
2154 bytesRemaining -= 0x7FFFFFFF;
2157 if (bytesRemaining > 0) {
2169 drflac__reset_cache(bs);
2178 unsigned char utf8[7] = {0};
2182 drflac_assert(bs !=
NULL);
2183 drflac_assert(pNumberOut !=
NULL);
2184 drflac_assert(pCRCOut !=
NULL);
2188 if (!drflac__read_uint8(bs, 8, utf8)) {
2190 return DRFLAC_END_OF_STREAM;
2192 crc = drflac_crc8(crc, utf8[0], 8);
2194 if ((utf8[0] & 0x80) == 0) {
2195 *pNumberOut = utf8[0];
2197 return DRFLAC_SUCCESS;
2201 if ((utf8[0] & 0xE0) == 0xC0) {
2203 }
else if ((utf8[0] & 0xF0) == 0xE0) {
2205 }
else if ((utf8[0] & 0xF8) == 0xF0) {
2207 }
else if ((utf8[0] & 0xFC) == 0xF8) {
2209 }
else if ((utf8[0] & 0xFE) == 0xFC) {
2211 }
else if ((utf8[0] & 0xFF) == 0xFE) {
2215 return DRFLAC_CRC_MISMATCH;
2219 drflac_assert(byteCount > 1);
2221 result = (
drflac_uint64)(utf8[0] & (0xFF >> (byteCount + 1)));
2222 for (
i = 1;
i < byteCount; ++
i) {
2223 if (!drflac__read_uint8(bs, 8, utf8 +
i)) {
2225 return DRFLAC_END_OF_STREAM;
2227 crc = drflac_crc8(crc, utf8[
i], 8);
2229 result = (result << 6) | (utf8[
i] & 0x3F);
2232 *pNumberOut = result;
2234 return DRFLAC_SUCCESS;
2249 drflac_assert(
order <= 32);
2256 case 32: prediction += coefficients[31] * pDecodedSamples[-32];
2257 case 31: prediction += coefficients[30] * pDecodedSamples[-31];
2258 case 30: prediction += coefficients[29] * pDecodedSamples[-30];
2259 case 29: prediction += coefficients[28] * pDecodedSamples[-29];
2260 case 28: prediction += coefficients[27] * pDecodedSamples[-28];
2261 case 27: prediction += coefficients[26] * pDecodedSamples[-27];
2262 case 26: prediction += coefficients[25] * pDecodedSamples[-26];
2263 case 25: prediction += coefficients[24] * pDecodedSamples[-25];
2264 case 24: prediction += coefficients[23] * pDecodedSamples[-24];
2265 case 23: prediction += coefficients[22] * pDecodedSamples[-23];
2266 case 22: prediction += coefficients[21] * pDecodedSamples[-22];
2267 case 21: prediction += coefficients[20] * pDecodedSamples[-21];
2268 case 20: prediction += coefficients[19] * pDecodedSamples[-20];
2269 case 19: prediction += coefficients[18] * pDecodedSamples[-19];
2270 case 18: prediction += coefficients[17] * pDecodedSamples[-18];
2271 case 17: prediction += coefficients[16] * pDecodedSamples[-17];
2272 case 16: prediction += coefficients[15] * pDecodedSamples[-16];
2273 case 15: prediction += coefficients[14] * pDecodedSamples[-15];
2274 case 14: prediction += coefficients[13] * pDecodedSamples[-14];
2275 case 13: prediction += coefficients[12] * pDecodedSamples[-13];
2276 case 12: prediction += coefficients[11] * pDecodedSamples[-12];
2277 case 11: prediction += coefficients[10] * pDecodedSamples[-11];
2278 case 10: prediction += coefficients[ 9] * pDecodedSamples[-10];
2279 case 9: prediction += coefficients[ 8] * pDecodedSamples[- 9];
2280 case 8: prediction += coefficients[ 7] * pDecodedSamples[- 8];
2281 case 7: prediction += coefficients[ 6] * pDecodedSamples[- 7];
2282 case 6: prediction += coefficients[ 5] * pDecodedSamples[- 6];
2283 case 5: prediction += coefficients[ 4] * pDecodedSamples[- 5];
2284 case 4: prediction += coefficients[ 3] * pDecodedSamples[- 4];
2285 case 3: prediction += coefficients[ 2] * pDecodedSamples[- 3];
2286 case 2: prediction += coefficients[ 1] * pDecodedSamples[- 2];
2287 case 1: prediction += coefficients[ 0] * pDecodedSamples[- 1];
2297 drflac_assert(
order <= 32);
2302 #ifndef DRFLAC_64BIT
2305 prediction = coefficients[0] * (
drflac_int64)pDecodedSamples[-1];
2306 prediction += coefficients[1] * (
drflac_int64)pDecodedSamples[-2];
2307 prediction += coefficients[2] * (
drflac_int64)pDecodedSamples[-3];
2308 prediction += coefficients[3] * (
drflac_int64)pDecodedSamples[-4];
2309 prediction += coefficients[4] * (
drflac_int64)pDecodedSamples[-5];
2310 prediction += coefficients[5] * (
drflac_int64)pDecodedSamples[-6];
2311 prediction += coefficients[6] * (
drflac_int64)pDecodedSamples[-7];
2312 prediction += coefficients[7] * (
drflac_int64)pDecodedSamples[-8];
2314 else if (
order == 7)
2316 prediction = coefficients[0] * (
drflac_int64)pDecodedSamples[-1];
2317 prediction += coefficients[1] * (
drflac_int64)pDecodedSamples[-2];
2318 prediction += coefficients[2] * (
drflac_int64)pDecodedSamples[-3];
2319 prediction += coefficients[3] * (
drflac_int64)pDecodedSamples[-4];
2320 prediction += coefficients[4] * (
drflac_int64)pDecodedSamples[-5];
2321 prediction += coefficients[5] * (
drflac_int64)pDecodedSamples[-6];
2322 prediction += coefficients[6] * (
drflac_int64)pDecodedSamples[-7];
2324 else if (
order == 3)
2326 prediction = coefficients[0] * (
drflac_int64)pDecodedSamples[-1];
2327 prediction += coefficients[1] * (
drflac_int64)pDecodedSamples[-2];
2328 prediction += coefficients[2] * (
drflac_int64)pDecodedSamples[-3];
2330 else if (
order == 6)
2332 prediction = coefficients[0] * (
drflac_int64)pDecodedSamples[-1];
2333 prediction += coefficients[1] * (
drflac_int64)pDecodedSamples[-2];
2334 prediction += coefficients[2] * (
drflac_int64)pDecodedSamples[-3];
2335 prediction += coefficients[3] * (
drflac_int64)pDecodedSamples[-4];
2336 prediction += coefficients[4] * (
drflac_int64)pDecodedSamples[-5];
2337 prediction += coefficients[5] * (
drflac_int64)pDecodedSamples[-6];
2339 else if (
order == 5)
2341 prediction = coefficients[0] * (
drflac_int64)pDecodedSamples[-1];
2342 prediction += coefficients[1] * (
drflac_int64)pDecodedSamples[-2];
2343 prediction += coefficients[2] * (
drflac_int64)pDecodedSamples[-3];
2344 prediction += coefficients[3] * (
drflac_int64)pDecodedSamples[-4];
2345 prediction += coefficients[4] * (
drflac_int64)pDecodedSamples[-5];
2347 else if (
order == 4)
2349 prediction = coefficients[0] * (
drflac_int64)pDecodedSamples[-1];
2350 prediction += coefficients[1] * (
drflac_int64)pDecodedSamples[-2];
2351 prediction += coefficients[2] * (
drflac_int64)pDecodedSamples[-3];
2352 prediction += coefficients[3] * (
drflac_int64)pDecodedSamples[-4];
2354 else if (
order == 12)
2356 prediction = coefficients[0] * (
drflac_int64)pDecodedSamples[-1];
2357 prediction += coefficients[1] * (
drflac_int64)pDecodedSamples[-2];
2358 prediction += coefficients[2] * (
drflac_int64)pDecodedSamples[-3];
2359 prediction += coefficients[3] * (
drflac_int64)pDecodedSamples[-4];
2360 prediction += coefficients[4] * (
drflac_int64)pDecodedSamples[-5];
2361 prediction += coefficients[5] * (
drflac_int64)pDecodedSamples[-6];
2362 prediction += coefficients[6] * (
drflac_int64)pDecodedSamples[-7];
2363 prediction += coefficients[7] * (
drflac_int64)pDecodedSamples[-8];
2364 prediction += coefficients[8] * (
drflac_int64)pDecodedSamples[-9];
2365 prediction += coefficients[9] * (
drflac_int64)pDecodedSamples[-10];
2366 prediction += coefficients[10] * (
drflac_int64)pDecodedSamples[-11];
2367 prediction += coefficients[11] * (
drflac_int64)pDecodedSamples[-12];
2369 else if (
order == 2)
2371 prediction = coefficients[0] * (
drflac_int64)pDecodedSamples[-1];
2372 prediction += coefficients[1] * (
drflac_int64)pDecodedSamples[-2];
2374 else if (
order == 1)
2376 prediction = coefficients[0] * (
drflac_int64)pDecodedSamples[-1];
2378 else if (
order == 10)
2380 prediction = coefficients[0] * (
drflac_int64)pDecodedSamples[-1];
2381 prediction += coefficients[1] * (
drflac_int64)pDecodedSamples[-2];
2382 prediction += coefficients[2] * (
drflac_int64)pDecodedSamples[-3];
2383 prediction += coefficients[3] * (
drflac_int64)pDecodedSamples[-4];
2384 prediction += coefficients[4] * (
drflac_int64)pDecodedSamples[-5];
2385 prediction += coefficients[5] * (
drflac_int64)pDecodedSamples[-6];
2386 prediction += coefficients[6] * (
drflac_int64)pDecodedSamples[-7];
2387 prediction += coefficients[7] * (
drflac_int64)pDecodedSamples[-8];
2388 prediction += coefficients[8] * (
drflac_int64)pDecodedSamples[-9];
2389 prediction += coefficients[9] * (
drflac_int64)pDecodedSamples[-10];
2391 else if (
order == 9)
2393 prediction = coefficients[0] * (
drflac_int64)pDecodedSamples[-1];
2394 prediction += coefficients[1] * (
drflac_int64)pDecodedSamples[-2];
2395 prediction += coefficients[2] * (
drflac_int64)pDecodedSamples[-3];
2396 prediction += coefficients[3] * (
drflac_int64)pDecodedSamples[-4];
2397 prediction += coefficients[4] * (
drflac_int64)pDecodedSamples[-5];
2398 prediction += coefficients[5] * (
drflac_int64)pDecodedSamples[-6];
2399 prediction += coefficients[6] * (
drflac_int64)pDecodedSamples[-7];
2400 prediction += coefficients[7] * (
drflac_int64)pDecodedSamples[-8];
2401 prediction += coefficients[8] * (
drflac_int64)pDecodedSamples[-9];
2403 else if (
order == 11)
2405 prediction = coefficients[0] * (
drflac_int64)pDecodedSamples[-1];
2406 prediction += coefficients[1] * (
drflac_int64)pDecodedSamples[-2];
2407 prediction += coefficients[2] * (
drflac_int64)pDecodedSamples[-3];
2408 prediction += coefficients[3] * (
drflac_int64)pDecodedSamples[-4];
2409 prediction += coefficients[4] * (
drflac_int64)pDecodedSamples[-5];
2410 prediction += coefficients[5] * (
drflac_int64)pDecodedSamples[-6];
2411 prediction += coefficients[6] * (
drflac_int64)pDecodedSamples[-7];
2412 prediction += coefficients[7] * (
drflac_int64)pDecodedSamples[-8];
2413 prediction += coefficients[8] * (
drflac_int64)pDecodedSamples[-9];
2414 prediction += coefficients[9] * (
drflac_int64)pDecodedSamples[-10];
2415 prediction += coefficients[10] * (
drflac_int64)pDecodedSamples[-11];
2423 prediction += coefficients[
j] * (
drflac_int64)pDecodedSamples[-
j-1];
2436 case 32: prediction += coefficients[31] * (
drflac_int64)pDecodedSamples[-32];
2437 case 31: prediction += coefficients[30] * (
drflac_int64)pDecodedSamples[-31];
2438 case 30: prediction += coefficients[29] * (
drflac_int64)pDecodedSamples[-30];
2439 case 29: prediction += coefficients[28] * (
drflac_int64)pDecodedSamples[-29];
2440 case 28: prediction += coefficients[27] * (
drflac_int64)pDecodedSamples[-28];
2441 case 27: prediction += coefficients[26] * (
drflac_int64)pDecodedSamples[-27];
2442 case 26: prediction += coefficients[25] * (
drflac_int64)pDecodedSamples[-26];
2443 case 25: prediction += coefficients[24] * (
drflac_int64)pDecodedSamples[-25];
2444 case 24: prediction += coefficients[23] * (
drflac_int64)pDecodedSamples[-24];
2445 case 23: prediction += coefficients[22] * (
drflac_int64)pDecodedSamples[-23];
2446 case 22: prediction += coefficients[21] * (
drflac_int64)pDecodedSamples[-22];
2447 case 21: prediction += coefficients[20] * (
drflac_int64)pDecodedSamples[-21];
2448 case 20: prediction += coefficients[19] * (
drflac_int64)pDecodedSamples[-20];
2449 case 19: prediction += coefficients[18] * (
drflac_int64)pDecodedSamples[-19];
2450 case 18: prediction += coefficients[17] * (
drflac_int64)pDecodedSamples[-18];
2451 case 17: prediction += coefficients[16] * (
drflac_int64)pDecodedSamples[-17];
2452 case 16: prediction += coefficients[15] * (
drflac_int64)pDecodedSamples[-16];
2453 case 15: prediction += coefficients[14] * (
drflac_int64)pDecodedSamples[-15];
2454 case 14: prediction += coefficients[13] * (
drflac_int64)pDecodedSamples[-14];
2455 case 13: prediction += coefficients[12] * (
drflac_int64)pDecodedSamples[-13];
2456 case 12: prediction += coefficients[11] * (
drflac_int64)pDecodedSamples[-12];
2457 case 11: prediction += coefficients[10] * (
drflac_int64)pDecodedSamples[-11];
2458 case 10: prediction += coefficients[ 9] * (
drflac_int64)pDecodedSamples[-10];
2459 case 9: prediction += coefficients[ 8] * (
drflac_int64)pDecodedSamples[- 9];
2460 case 8: prediction += coefficients[ 7] * (
drflac_int64)pDecodedSamples[- 8];
2461 case 7: prediction += coefficients[ 6] * (
drflac_int64)pDecodedSamples[- 7];
2462 case 6: prediction += coefficients[ 5] * (
drflac_int64)pDecodedSamples[- 6];
2463 case 5: prediction += coefficients[ 4] * (
drflac_int64)pDecodedSamples[- 5];
2464 case 4: prediction += coefficients[ 3] * (
drflac_int64)pDecodedSamples[- 4];
2465 case 3: prediction += coefficients[ 2] * (
drflac_int64)pDecodedSamples[- 3];
2466 case 2: prediction += coefficients[ 1] * (
drflac_int64)pDecodedSamples[- 2];
2467 case 1: prediction += coefficients[ 0] * (
drflac_int64)pDecodedSamples[- 1];
2481 drflac_assert(
order <= 32);
2486 prediction0 += coefficients[31] * (
drflac_int64)pDecodedSamples[-32];
2487 prediction1 += coefficients[31] * (
drflac_int64)pDecodedSamples[-31];
2488 prediction2 += coefficients[31] * (
drflac_int64)pDecodedSamples[-30];
2489 prediction3 += coefficients[31] * (
drflac_int64)pDecodedSamples[-29];
2491 prediction0 += coefficients[30] * (
drflac_int64)pDecodedSamples[-31];
2492 prediction1 += coefficients[30] * (
drflac_int64)pDecodedSamples[-30];
2493 prediction2 += coefficients[30] * (
drflac_int64)pDecodedSamples[-29];
2494 prediction3 += coefficients[30] * (
drflac_int64)pDecodedSamples[-28];
2496 prediction0 += coefficients[29] * (
drflac_int64)pDecodedSamples[-30];
2497 prediction1 += coefficients[29] * (
drflac_int64)pDecodedSamples[-29];
2498 prediction2 += coefficients[29] * (
drflac_int64)pDecodedSamples[-28];
2499 prediction3 += coefficients[29] * (
drflac_int64)pDecodedSamples[-27];
2501 prediction0 += coefficients[28] * (
drflac_int64)pDecodedSamples[-29];
2502 prediction1 += coefficients[28] * (
drflac_int64)pDecodedSamples[-28];
2503 prediction2 += coefficients[28] * (
drflac_int64)pDecodedSamples[-27];
2504 prediction3 += coefficients[28] * (
drflac_int64)pDecodedSamples[-26];
2506 prediction0 += coefficients[27] * (
drflac_int64)pDecodedSamples[-28];
2507 prediction1 += coefficients[27] * (
drflac_int64)pDecodedSamples[-27];
2508 prediction2 += coefficients[27] * (
drflac_int64)pDecodedSamples[-26];
2509 prediction3 += coefficients[27] * (
drflac_int64)pDecodedSamples[-25];
2511 prediction0 += coefficients[26] * (
drflac_int64)pDecodedSamples[-27];
2512 prediction1 += coefficients[26] * (
drflac_int64)pDecodedSamples[-26];
2513 prediction2 += coefficients[26] * (
drflac_int64)pDecodedSamples[-25];
2514 prediction3 += coefficients[26] * (
drflac_int64)pDecodedSamples[-24];
2516 prediction0 += coefficients[25] * (
drflac_int64)pDecodedSamples[-26];
2517 prediction1 += coefficients[25] * (
drflac_int64)pDecodedSamples[-25];
2518 prediction2 += coefficients[25] * (
drflac_int64)pDecodedSamples[-24];
2519 prediction3 += coefficients[25] * (
drflac_int64)pDecodedSamples[-23];
2521 prediction0 += coefficients[24] * (
drflac_int64)pDecodedSamples[-25];
2522 prediction1 += coefficients[24] * (
drflac_int64)pDecodedSamples[-24];
2523 prediction2 += coefficients[24] * (
drflac_int64)pDecodedSamples[-23];
2524 prediction3 += coefficients[24] * (
drflac_int64)pDecodedSamples[-22];
2526 prediction0 += coefficients[23] * (
drflac_int64)pDecodedSamples[-24];
2527 prediction1 += coefficients[23] * (
drflac_int64)pDecodedSamples[-23];
2528 prediction2 += coefficients[23] * (
drflac_int64)pDecodedSamples[-22];
2529 prediction3 += coefficients[23] * (
drflac_int64)pDecodedSamples[-21];
2531 prediction0 += coefficients[22] * (
drflac_int64)pDecodedSamples[-23];
2532 prediction1 += coefficients[22] * (
drflac_int64)pDecodedSamples[-22];
2533 prediction2 += coefficients[22] * (
drflac_int64)pDecodedSamples[-21];
2534 prediction3 += coefficients[22] * (
drflac_int64)pDecodedSamples[-20];
2536 prediction0 += coefficients[21] * (
drflac_int64)pDecodedSamples[-22];
2537 prediction1 += coefficients[21] * (
drflac_int64)pDecodedSamples[-21];
2538 prediction2 += coefficients[21] * (
drflac_int64)pDecodedSamples[-20];
2539 prediction3 += coefficients[21] * (
drflac_int64)pDecodedSamples[-19];
2541 prediction0 += coefficients[20] * (
drflac_int64)pDecodedSamples[-21];
2542 prediction1 += coefficients[20] * (
drflac_int64)pDecodedSamples[-20];
2543 prediction2 += coefficients[20] * (
drflac_int64)pDecodedSamples[-19];
2544 prediction3 += coefficients[20] * (
drflac_int64)pDecodedSamples[-18];
2546 prediction0 += coefficients[19] * (
drflac_int64)pDecodedSamples[-20];
2547 prediction1 += coefficients[19] * (
drflac_int64)pDecodedSamples[-19];
2548 prediction2 += coefficients[19] * (
drflac_int64)pDecodedSamples[-18];
2549 prediction3 += coefficients[19] * (
drflac_int64)pDecodedSamples[-17];
2551 prediction0 += coefficients[18] * (
drflac_int64)pDecodedSamples[-19];
2552 prediction1 += coefficients[18] * (
drflac_int64)pDecodedSamples[-18];
2553 prediction2 += coefficients[18] * (
drflac_int64)pDecodedSamples[-17];
2554 prediction3 += coefficients[18] * (
drflac_int64)pDecodedSamples[-16];
2556 prediction0 += coefficients[17] * (
drflac_int64)pDecodedSamples[-18];
2557 prediction1 += coefficients[17] * (
drflac_int64)pDecodedSamples[-17];
2558 prediction2 += coefficients[17] * (
drflac_int64)pDecodedSamples[-16];
2559 prediction3 += coefficients[17] * (
drflac_int64)pDecodedSamples[-15];
2561 prediction0 += coefficients[16] * (
drflac_int64)pDecodedSamples[-17];
2562 prediction1 += coefficients[16] * (
drflac_int64)pDecodedSamples[-16];
2563 prediction2 += coefficients[16] * (
drflac_int64)pDecodedSamples[-15];
2564 prediction3 += coefficients[16] * (
drflac_int64)pDecodedSamples[-14];
2567 prediction0 += coefficients[15] * (
drflac_int64)pDecodedSamples[-16];
2568 prediction1 += coefficients[15] * (
drflac_int64)pDecodedSamples[-15];
2569 prediction2 += coefficients[15] * (
drflac_int64)pDecodedSamples[-14];
2570 prediction3 += coefficients[15] * (
drflac_int64)pDecodedSamples[-13];
2572 prediction0 += coefficients[14] * (
drflac_int64)pDecodedSamples[-15];
2573 prediction1 += coefficients[14] * (
drflac_int64)pDecodedSamples[-14];
2574 prediction2 += coefficients[14] * (
drflac_int64)pDecodedSamples[-13];
2575 prediction3 += coefficients[14] * (
drflac_int64)pDecodedSamples[-12];
2577 prediction0 += coefficients[13] * (
drflac_int64)pDecodedSamples[-14];
2578 prediction1 += coefficients[13] * (
drflac_int64)pDecodedSamples[-13];
2579 prediction2 += coefficients[13] * (
drflac_int64)pDecodedSamples[-12];
2580 prediction3 += coefficients[13] * (
drflac_int64)pDecodedSamples[-11];
2582 prediction0 += coefficients[12] * (
drflac_int64)pDecodedSamples[-13];
2583 prediction1 += coefficients[12] * (
drflac_int64)pDecodedSamples[-12];
2584 prediction2 += coefficients[12] * (
drflac_int64)pDecodedSamples[-11];
2585 prediction3 += coefficients[12] * (
drflac_int64)pDecodedSamples[-10];
2587 prediction0 += coefficients[11] * (
drflac_int64)pDecodedSamples[-12];
2588 prediction1 += coefficients[11] * (
drflac_int64)pDecodedSamples[-11];
2589 prediction2 += coefficients[11] * (
drflac_int64)pDecodedSamples[-10];
2590 prediction3 += coefficients[11] * (
drflac_int64)pDecodedSamples[- 9];
2592 prediction0 += coefficients[10] * (
drflac_int64)pDecodedSamples[-11];
2593 prediction1 += coefficients[10] * (
drflac_int64)pDecodedSamples[-10];
2594 prediction2 += coefficients[10] * (
drflac_int64)pDecodedSamples[- 9];
2595 prediction3 += coefficients[10] * (
drflac_int64)pDecodedSamples[- 8];
2597 prediction0 += coefficients[9] * (
drflac_int64)pDecodedSamples[-10];
2598 prediction1 += coefficients[9] * (
drflac_int64)pDecodedSamples[- 9];
2599 prediction2 += coefficients[9] * (
drflac_int64)pDecodedSamples[- 8];
2600 prediction3 += coefficients[9] * (
drflac_int64)pDecodedSamples[- 7];
2602 prediction0 += coefficients[8] * (
drflac_int64)pDecodedSamples[- 9];
2603 prediction1 += coefficients[8] * (
drflac_int64)pDecodedSamples[- 8];
2604 prediction2 += coefficients[8] * (
drflac_int64)pDecodedSamples[- 7];
2605 prediction3 += coefficients[8] * (
drflac_int64)pDecodedSamples[- 6];
2607 prediction0 += coefficients[7] * (
drflac_int64)pDecodedSamples[- 8];
2608 prediction1 += coefficients[7] * (
drflac_int64)pDecodedSamples[- 7];
2609 prediction2 += coefficients[7] * (
drflac_int64)pDecodedSamples[- 6];
2610 prediction3 += coefficients[7] * (
drflac_int64)pDecodedSamples[- 5];
2612 prediction0 += coefficients[6] * (
drflac_int64)pDecodedSamples[- 7];
2613 prediction1 += coefficients[6] * (
drflac_int64)pDecodedSamples[- 6];
2614 prediction2 += coefficients[6] * (
drflac_int64)pDecodedSamples[- 5];
2615 prediction3 += coefficients[6] * (
drflac_int64)pDecodedSamples[- 4];
2617 prediction0 += coefficients[5] * (
drflac_int64)pDecodedSamples[- 6];
2618 prediction1 += coefficients[5] * (
drflac_int64)pDecodedSamples[- 5];
2619 prediction2 += coefficients[5] * (
drflac_int64)pDecodedSamples[- 4];
2620 prediction3 += coefficients[5] * (
drflac_int64)pDecodedSamples[- 3];
2622 prediction0 += coefficients[4] * (
drflac_int64)pDecodedSamples[- 5];
2623 prediction1 += coefficients[4] * (
drflac_int64)pDecodedSamples[- 4];
2624 prediction2 += coefficients[4] * (
drflac_int64)pDecodedSamples[- 3];
2625 prediction3 += coefficients[4] * (
drflac_int64)pDecodedSamples[- 2];
2627 prediction0 += coefficients[3] * (
drflac_int64)pDecodedSamples[- 4];
2628 prediction1 += coefficients[3] * (
drflac_int64)pDecodedSamples[- 3];
2629 prediction2 += coefficients[3] * (
drflac_int64)pDecodedSamples[- 2];
2630 prediction3 += coefficients[3] * (
drflac_int64)pDecodedSamples[- 1];
2636 case 3: prediction0 += coefficients[ 2] * (
drflac_int64)pDecodedSamples[- 3];
2637 case 2: prediction0 += coefficients[ 1] * (
drflac_int64)pDecodedSamples[- 2];
2638 case 1: prediction0 += coefficients[ 0] * (
drflac_int64)pDecodedSamples[- 1];
2640 pDecodedSamples[0] = riceParamParts[0] + (
drflac_int32)(prediction0 >> shift);
2644 case 3: prediction1 += coefficients[ 2] * (
drflac_int64)pDecodedSamples[- 2];
2645 case 2: prediction1 += coefficients[ 1] * (
drflac_int64)pDecodedSamples[- 1];
2646 case 1: prediction1 += coefficients[ 0] * (
drflac_int64)pDecodedSamples[ 0];
2648 pDecodedSamples[1] = riceParamParts[1] + (
drflac_int32)(prediction1 >> shift);
2652 case 3: prediction2 += coefficients[ 2] * (
drflac_int64)pDecodedSamples[- 1];
2653 case 2: prediction2 += coefficients[ 1] * (
drflac_int64)pDecodedSamples[ 0];
2654 case 1: prediction2 += coefficients[ 0] * (
drflac_int64)pDecodedSamples[ 1];
2656 pDecodedSamples[2] = riceParamParts[2] + (
drflac_int32)(prediction2 >> shift);
2660 case 3: prediction3 += coefficients[ 2] * (
drflac_int64)pDecodedSamples[ 0];
2661 case 2: prediction3 += coefficients[ 1] * (
drflac_int64)pDecodedSamples[ 1];
2662 case 1: prediction3 += coefficients[ 0] * (
drflac_int64)pDecodedSamples[ 2];
2664 pDecodedSamples[3] = riceParamParts[3] + (
drflac_int32)(prediction3 >> shift);
2667 #if defined(DRFLAC_SUPPORT_SSE41)
2670 __m128i prediction = _mm_setzero_si128();
2672 drflac_assert(
order <= 32);
2677 case 31: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[31], 0, coefficients[30]), _mm_set_epi32(0, pDecodedSamples[-32], 0, pDecodedSamples[-31])));
2679 case 29: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[29], 0, coefficients[28]), _mm_set_epi32(0, pDecodedSamples[-30], 0, pDecodedSamples[-29])));
2681 case 27: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[27], 0, coefficients[26]), _mm_set_epi32(0, pDecodedSamples[-28], 0, pDecodedSamples[-27])));
2683 case 25: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[25], 0, coefficients[24]), _mm_set_epi32(0, pDecodedSamples[-26], 0, pDecodedSamples[-25])));
2685 case 23: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[23], 0, coefficients[22]), _mm_set_epi32(0, pDecodedSamples[-24], 0, pDecodedSamples[-23])));
2687 case 21: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[21], 0, coefficients[20]), _mm_set_epi32(0, pDecodedSamples[-22], 0, pDecodedSamples[-21])));
2689 case 19: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[19], 0, coefficients[18]), _mm_set_epi32(0, pDecodedSamples[-20], 0, pDecodedSamples[-19])));
2691 case 17: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[17], 0, coefficients[16]), _mm_set_epi32(0, pDecodedSamples[-18], 0, pDecodedSamples[-17])));
2693 case 15: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[15], 0, coefficients[14]), _mm_set_epi32(0, pDecodedSamples[-16], 0, pDecodedSamples[-15])));
2695 case 13: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[13], 0, coefficients[12]), _mm_set_epi32(0, pDecodedSamples[-14], 0, pDecodedSamples[-13])));
2697 case 11: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[11], 0, coefficients[10]), _mm_set_epi32(0, pDecodedSamples[-12], 0, pDecodedSamples[-11])));
2699 case 9: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 9], 0, coefficients[ 8]), _mm_set_epi32(0, pDecodedSamples[-10], 0, pDecodedSamples[- 9])));
2701 case 7: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 7], 0, coefficients[ 6]), _mm_set_epi32(0, pDecodedSamples[- 8], 0, pDecodedSamples[- 7])));
2703 case 5: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 5], 0, coefficients[ 4]), _mm_set_epi32(0, pDecodedSamples[- 6], 0, pDecodedSamples[- 5])));
2705 case 3: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 3], 0, coefficients[ 2]), _mm_set_epi32(0, pDecodedSamples[- 4], 0, pDecodedSamples[- 3])));
2707 case 1: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 1], 0, coefficients[ 0]), _mm_set_epi32(0, pDecodedSamples[- 2], 0, pDecodedSamples[- 1])));
2717 __m128i prediction = _mm_setzero_si128();
2720 drflac_assert(
order <= 32);
2724 case 32: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[31], 0, coefficients[31]), _mm_set_epi32(0, pDecodedSamples[-31], 0, pDecodedSamples[-32])));
2725 case 31: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[30], 0, coefficients[30]), _mm_set_epi32(0, pDecodedSamples[-30], 0, pDecodedSamples[-31])));
2726 case 30: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[29], 0, coefficients[29]), _mm_set_epi32(0, pDecodedSamples[-29], 0, pDecodedSamples[-30])));
2727 case 29: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[28], 0, coefficients[28]), _mm_set_epi32(0, pDecodedSamples[-28], 0, pDecodedSamples[-29])));
2728 case 28: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[27], 0, coefficients[27]), _mm_set_epi32(0, pDecodedSamples[-27], 0, pDecodedSamples[-28])));
2729 case 27: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[26], 0, coefficients[26]), _mm_set_epi32(0, pDecodedSamples[-26], 0, pDecodedSamples[-27])));
2730 case 26: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[25], 0, coefficients[25]), _mm_set_epi32(0, pDecodedSamples[-25], 0, pDecodedSamples[-26])));
2731 case 25: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[24], 0, coefficients[24]), _mm_set_epi32(0, pDecodedSamples[-24], 0, pDecodedSamples[-25])));
2732 case 24: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[23], 0, coefficients[23]), _mm_set_epi32(0, pDecodedSamples[-23], 0, pDecodedSamples[-24])));
2733 case 23: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[22], 0, coefficients[22]), _mm_set_epi32(0, pDecodedSamples[-22], 0, pDecodedSamples[-23])));
2734 case 22: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[21], 0, coefficients[21]), _mm_set_epi32(0, pDecodedSamples[-21], 0, pDecodedSamples[-22])));
2735 case 21: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[20], 0, coefficients[20]), _mm_set_epi32(0, pDecodedSamples[-20], 0, pDecodedSamples[-21])));
2736 case 20: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[19], 0, coefficients[19]), _mm_set_epi32(0, pDecodedSamples[-19], 0, pDecodedSamples[-20])));
2737 case 19: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[18], 0, coefficients[18]), _mm_set_epi32(0, pDecodedSamples[-18], 0, pDecodedSamples[-19])));
2738 case 18: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[17], 0, coefficients[17]), _mm_set_epi32(0, pDecodedSamples[-17], 0, pDecodedSamples[-18])));
2739 case 17: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[16], 0, coefficients[16]), _mm_set_epi32(0, pDecodedSamples[-16], 0, pDecodedSamples[-17])));
2740 case 16: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[15], 0, coefficients[15]), _mm_set_epi32(0, pDecodedSamples[-15], 0, pDecodedSamples[-16])));
2741 case 15: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[14], 0, coefficients[14]), _mm_set_epi32(0, pDecodedSamples[-14], 0, pDecodedSamples[-15])));
2742 case 14: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[13], 0, coefficients[13]), _mm_set_epi32(0, pDecodedSamples[-13], 0, pDecodedSamples[-14])));
2743 case 13: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[12], 0, coefficients[12]), _mm_set_epi32(0, pDecodedSamples[-12], 0, pDecodedSamples[-13])));
2744 case 12: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[11], 0, coefficients[11]), _mm_set_epi32(0, pDecodedSamples[-11], 0, pDecodedSamples[-12])));
2745 case 11: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[10], 0, coefficients[10]), _mm_set_epi32(0, pDecodedSamples[-10], 0, pDecodedSamples[-11])));
2746 case 10: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 9], 0, coefficients[ 9]), _mm_set_epi32(0, pDecodedSamples[- 9], 0, pDecodedSamples[-10])));
2747 case 9: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 8], 0, coefficients[ 8]), _mm_set_epi32(0, pDecodedSamples[- 8], 0, pDecodedSamples[- 9])));
2748 case 8: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 7], 0, coefficients[ 7]), _mm_set_epi32(0, pDecodedSamples[- 7], 0, pDecodedSamples[- 8])));
2749 case 7: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 6], 0, coefficients[ 6]), _mm_set_epi32(0, pDecodedSamples[- 6], 0, pDecodedSamples[- 7])));
2750 case 6: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 5], 0, coefficients[ 5]), _mm_set_epi32(0, pDecodedSamples[- 5], 0, pDecodedSamples[- 6])));
2751 case 5: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 4], 0, coefficients[ 4]), _mm_set_epi32(0, pDecodedSamples[- 4], 0, pDecodedSamples[- 5])));
2752 case 4: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 3], 0, coefficients[ 3]), _mm_set_epi32(0, pDecodedSamples[- 3], 0, pDecodedSamples[- 4])));
2753 case 3: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 2], 0, coefficients[ 2]), _mm_set_epi32(0, pDecodedSamples[- 2], 0, pDecodedSamples[- 3])));
2754 case 2: prediction = _mm_add_epi64(prediction, _mm_mul_epi32(_mm_set_epi32(0, coefficients[ 1], 0, coefficients[ 1]), _mm_set_epi32(0, pDecodedSamples[- 1], 0, pDecodedSamples[- 2])));
2758 _mm_storeu_si128((__m128i*)predictions, prediction);
2762 case 1: predictions[0] += coefficients[ 0] * (
drflac_int64)pDecodedSamples[- 1];
2764 pDecodedSamples[0] = riceParamParts[0] + (
drflac_int32)(predictions[0] >> shift);
2768 case 1: predictions[1] += coefficients[ 0] * (
drflac_int64)pDecodedSamples[ 0];
2770 pDecodedSamples[1] = riceParamParts[1] + (
drflac_int32)(predictions[1] >> shift);
2774 static DRFLAC_INLINE __m128i drflac__mm_not_si128(__m128i a)
2776 return _mm_xor_si128(a, _mm_cmpeq_epi32(_mm_setzero_si128(), _mm_setzero_si128()));
2779 static DRFLAC_INLINE __m128i drflac__mm_slide1_epi32(__m128i a, __m128i b)
2784 __m128i b3a3b2a2 = _mm_unpackhi_epi32(a, b);
2785 __m128i a2b3a2b3 = _mm_shuffle_epi32(b3a3b2a2, _MM_SHUFFLE(0, 3, 0, 3));
2786 __m128i a1a2a0b3 = _mm_unpacklo_epi32(a2b3a2b3, a);
2787 __m128i a2a1a0b3 = _mm_shuffle_epi32(a1a2a0b3, _MM_SHUFFLE(2, 3, 1, 0));
2791 static DRFLAC_INLINE __m128i drflac__mm_slide2_epi32(__m128i a, __m128i b)
2794 __m128i b1b0b3b2 = _mm_shuffle_epi32(b, _MM_SHUFFLE(1, 0, 3, 2));
2795 __m128i a1b3a0b2 = _mm_unpacklo_epi32(b1b0b3b2, a);
2796 __m128i a1a0b3b2 = _mm_shuffle_epi32(a1b3a0b2, _MM_SHUFFLE(3, 1, 2, 0));
2800 static DRFLAC_INLINE __m128i drflac__mm_slide3_epi32(__m128i a, __m128i b)
2803 __m128i b1a1b0a0 = _mm_unpacklo_epi32(a, b);
2804 __m128i a0b1a0b1 = _mm_shuffle_epi32(b1a1b0a0, _MM_SHUFFLE(0, 3, 0, 3));
2805 __m128i b3a0b2b1 = _mm_unpackhi_epi32(a0b1a0b1, b);
2806 __m128i a0b3b2b1 = _mm_shuffle_epi32(b3a0b2b1, _MM_SHUFFLE(2, 3, 1, 0));
2812 drflac_assert(
order <= 32);
2819 __m128i s_09_10_11_12 = _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 12));
2820 __m128i s_05_06_07_08 = _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 8));
2821 __m128i s_01_02_03_04 = _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 4));
2823 __m128i prediction = _mm_setzero_si128();
2831 case 32: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[31], _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 32))));
2832 case 31: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[30], _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 31))));
2833 case 30: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[29], _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 30))));
2834 case 29: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[28], _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 29))));
2835 case 28: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[27], _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 28))));
2836 case 27: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[26], _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 27))));
2837 case 26: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[25], _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 26))));
2838 case 25: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[24], _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 25))));
2839 case 24: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[23], _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 24))));
2840 case 23: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[22], _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 23))));
2841 case 22: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[21], _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 22))));
2842 case 21: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[20], _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 21))));
2843 case 20: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[19], _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 20))));
2844 case 19: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[18], _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 19))));
2845 case 18: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[17], _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 18))));
2846 case 17: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[16], _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 17))));
2847 case 16: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[15], _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 16))));
2848 case 15: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[14], _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 15))));
2849 case 14: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[13], _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 14))));
2850 case 13: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[12], _mm_loadu_si128((
const __m128i*)(pDecodedSamples - 13))));
2852 case 12: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[11], s_09_10_11_12));
2853 case 11: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[10], drflac__mm_slide3_epi32(s_05_06_07_08, s_09_10_11_12)));
2854 case 10: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[ 9], drflac__mm_slide2_epi32(s_05_06_07_08, s_09_10_11_12)));
2855 case 9: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[ 8], drflac__mm_slide1_epi32(s_05_06_07_08, s_09_10_11_12)));
2856 case 8: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[ 7], s_05_06_07_08));
2857 case 7: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[ 6], drflac__mm_slide3_epi32(s_01_02_03_04, s_05_06_07_08)));
2858 case 6: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[ 5], drflac__mm_slide2_epi32(s_01_02_03_04, s_05_06_07_08)));
2859 case 5: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[ 4], drflac__mm_slide1_epi32(s_01_02_03_04, s_05_06_07_08)));
2860 case 4: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[ 3], s_01_02_03_04));
order = 3;
2861 case 3: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[ 2], drflac__mm_slide3_epi32(_mm_setzero_si128(), s_01_02_03_04)));
2862 case 2: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[ 1], drflac__mm_slide2_epi32(_mm_setzero_si128(), s_01_02_03_04)));
2863 case 1: prediction = _mm_add_epi32(prediction, _mm_mullo_epi32(coefficients128[ 0], drflac__mm_slide1_epi32(_mm_setzero_si128(), s_01_02_03_04)));
2866 _mm_storeu_si128((__m128i*)predictions, prediction);
2867 _mm_storeu_si128((__m128i*)riceParamParts, riceParamParts128);
2869 predictions[0] = riceParamParts[0] + (predictions[0] >> shift);
2873 case 3: predictions[3] += ((
const drflac_int32*)&coefficients128[ 2])[0] * predictions[ 0];
2874 case 2: predictions[2] += ((
const drflac_int32*)&coefficients128[ 1])[0] * predictions[ 0];
2875 case 1: predictions[1] += ((
const drflac_int32*)&coefficients128[ 0])[0] * predictions[ 0];
2877 predictions[1] = riceParamParts[1] + (predictions[1] >> shift);
2882 case 2: predictions[3] += ((
const drflac_int32*)&coefficients128[ 1])[0] * predictions[ 1];
2883 case 1: predictions[2] += ((
const drflac_int32*)&coefficients128[ 0])[0] * predictions[ 1];
2885 predictions[2] = riceParamParts[2] + (predictions[2] >> shift);
2891 case 1: predictions[3] += ((
const drflac_int32*)&coefficients128[ 0])[0] * predictions[ 2];
2893 predictions[3] = riceParamParts[3] + (predictions[3] >> shift);
2895 pDecodedSamples[0] = predictions[0];
2896 pDecodedSamples[1] = predictions[1];
2897 pDecodedSamples[2] = predictions[2];
2898 pDecodedSamples[3] = predictions[3];
2900 _mm_storeu_si128((__m128i*)pDecodedSamples, riceParamParts128);
2914 drflac_assert(bs !=
NULL);
2915 drflac_assert(
count > 0);
2916 drflac_assert(pSamplesOut !=
NULL);
2922 if (!drflac__read_uint8(bs, 1, &bit)) {
2934 if (riceParam > 0) {
2935 if (!drflac__read_uint32(bs, riceParam, &decodedRice)) {
2942 decodedRice |= (zeroCounter << riceParam);
2943 if ((decodedRice & 0x01)) {
2944 decodedRice = ~(decodedRice >> 1);
2946 decodedRice = (decodedRice >> 1);
2950 if (bitsPerSample > 16) {
2951 pSamplesOut[
i] = decodedRice + drflac__calculate_prediction_64(
order, shift, coefficients, pSamplesOut +
i);
2953 pSamplesOut[
i] = decodedRice + drflac__calculate_prediction_32(
order, shift, coefficients, pSamplesOut +
i);
2969 if (!drflac__read_uint8(bs, 1, &bit)) {
2980 if (riceParam > 0) {
2981 if (!drflac__read_uint32(bs, riceParam, &decodedRice)) {
2988 *pZeroCounterOut = zeroCounter;
2989 *pRiceParamPartOut = decodedRice;
3003 drflac_assert(riceParam > 0);
3005 riceParamMask = DRFLAC_CACHE_L1_SELECTION_MASK(riceParam);
3008 while (bs->
cache == 0) {
3009 zeroCounter += (
drflac_uint32)DRFLAC_CACHE_L1_BITS_REMAINING(bs);
3010 if (!drflac__reload_cache(bs)) {
3015 setBitOffsetPlus1 = drflac__clz(bs->
cache);
3016 zeroCounter += setBitOffsetPlus1;
3017 setBitOffsetPlus1 += 1;
3019 riceLength = setBitOffsetPlus1 + riceParam;
3020 if (riceLength < DRFLAC_CACHE_L1_BITS_REMAINING(bs)) {
3021 riceParamPart = (
drflac_uint32)((bs->
cache & (riceParamMask >> setBitOffsetPlus1)) >> DRFLAC_CACHE_L1_SELECTION_SHIFT(bs, riceLength));
3024 bs->
cache <<= riceLength;
3030 bs->
cache <<= setBitOffsetPlus1 & (DRFLAC_CACHE_L1_SIZE_BITS(bs)-1);
3033 bitCountLo = bs->
consumedBits - DRFLAC_CACHE_L1_SIZE_BITS(bs);
3034 resultHi = DRFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, riceParam);
3036 if (bs->
nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) {
3037 #ifndef DR_FLAC_NO_CRC
3038 drflac__update_crc16(bs);
3042 #ifndef DR_FLAC_NO_CRC
3047 if (!drflac__reload_cache(bs)) {
3052 riceParamPart = (
drflac_uint32)(resultHi | DRFLAC_CACHE_L1_SELECT_AND_SHIFT_SAFE(bs, bitCountLo));
3055 bs->
cache <<= bitCountLo;
3058 pZeroCounterOut[0] = zeroCounter;
3059 pRiceParamPartOut[0] = riceParamPart;
3069 drflac_uint32 riceParamPlus1Shift = DRFLAC_CACHE_L1_SELECTION_SHIFT(bs, riceParamPlus1);
3070 drflac_uint32 riceParamPlus1MaxConsumedBits = DRFLAC_CACHE_L1_SIZE_BITS(bs) - riceParamPlus1;
3081 if (lzcount <
sizeof(bs_cache)*8) {
3082 pZeroCounterOut[0] = lzcount;
3089 extract_rice_param_part:
3090 bs_cache <<= lzcount;
3091 bs_consumedBits += lzcount;
3093 if (bs_consumedBits <= riceParamPlus1MaxConsumedBits) {
3095 pRiceParamPartOut[0] = (
drflac_uint32)(bs_cache >> riceParamPlus1Shift);
3096 bs_cache <<= riceParamPlus1;
3097 bs_consumedBits += riceParamPlus1;
3109 riceParamPartHi = (
drflac_uint32)(bs_cache >> riceParamPlus1Shift);
3112 riceParamPartLoBitCount = bs_consumedBits - riceParamPlus1MaxConsumedBits;
3113 drflac_assert(riceParamPartLoBitCount > 0 && riceParamPartLoBitCount < 32);
3116 if (bs->
nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) {
3117 #ifndef DR_FLAC_NO_CRC
3118 drflac__update_crc16(bs);
3121 bs_consumedBits = riceParamPartLoBitCount;
3122 #ifndef DR_FLAC_NO_CRC
3127 if (!drflac__reload_cache(bs)) {
3131 bs_cache = bs->
cache;
3132 bs_consumedBits = bs->
consumedBits + riceParamPartLoBitCount;
3136 riceParamPartLo = (
drflac_uint32)(bs_cache >> (DRFLAC_CACHE_L1_SELECTION_SHIFT(bs, riceParamPartLoBitCount)));
3137 pRiceParamPartOut[0] = riceParamPartHi | riceParamPartLo;
3139 bs_cache <<= riceParamPartLoBitCount;
3148 if (bs->
nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) {
3149 #ifndef DR_FLAC_NO_CRC
3150 drflac__update_crc16(bs);
3153 bs_consumedBits = 0;
3154 #ifndef DR_FLAC_NO_CRC
3159 if (!drflac__reload_cache(bs)) {
3163 bs_cache = bs->
cache;
3167 lzcount = drflac__clz(bs_cache);
3168 zeroCounter += lzcount;
3170 if (lzcount <
sizeof(bs_cache)*8) {
3175 pZeroCounterOut[0] = zeroCounter;
3176 goto extract_rice_param_part;
3180 bs->
cache = bs_cache;
3190 drflac_uint32 riceParamPlus1Shift = DRFLAC_CACHE_L1_SELECTION_SHIFT(bs, riceParamPlus1);
3191 drflac_uint32 riceParamPlus1MaxConsumedBits = DRFLAC_CACHE_L1_SIZE_BITS(bs) - riceParamPlus1;
3205 for (
i = 0;
i < 4; ++
i) {
3208 if (lzcount <
sizeof(bs_cache)*8) {
3209 pZeroCounterOut[
i] = lzcount;
3216 extract_rice_param_part:
3217 bs_cache <<= lzcount;
3218 bs_consumedBits += lzcount;
3220 if (bs_consumedBits <= riceParamPlus1MaxConsumedBits) {
3222 pRiceParamPartOut[
i] = (
drflac_uint32)(bs_cache >> riceParamPlus1Shift);
3223 bs_cache <<= riceParamPlus1;
3224 bs_consumedBits += riceParamPlus1;
3236 riceParamPartHi = (
drflac_uint32)(bs_cache >> riceParamPlus1Shift);
3239 riceParamPartLoBitCount = bs_consumedBits - riceParamPlus1MaxConsumedBits;
3242 if (bs->
nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) {
3243 #ifndef DR_FLAC_NO_CRC
3244 drflac__update_crc16(bs);
3247 bs_consumedBits = riceParamPartLoBitCount;
3248 #ifndef DR_FLAC_NO_CRC
3253 if (!drflac__reload_cache(bs)) {
3257 bs_cache = bs->
cache;
3258 bs_consumedBits = bs->
consumedBits + riceParamPartLoBitCount;
3262 riceParamPartLo = (
drflac_uint32)(bs_cache >> (DRFLAC_CACHE_L1_SELECTION_SHIFT(bs, riceParamPartLoBitCount)));
3263 pRiceParamPartOut[
i] = riceParamPartHi | riceParamPartLo;
3265 bs_cache <<= riceParamPartLoBitCount;
3274 if (bs->
nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) {
3275 #ifndef DR_FLAC_NO_CRC
3276 drflac__update_crc16(bs);
3279 bs_consumedBits = 0;
3280 #ifndef DR_FLAC_NO_CRC
3285 if (!drflac__reload_cache(bs)) {
3289 bs_cache = bs->
cache;
3293 lzcount = drflac__clz(bs_cache);
3294 zeroCounter += lzcount;
3296 if (lzcount <
sizeof(bs_cache)*8) {
3301 pZeroCounterOut[
i] = zeroCounter;
3302 goto extract_rice_param_part;
3307 bs->
cache = bs_cache;
3316 drflac_uint32 riceParamPlus1MaxConsumedBits = DRFLAC_CACHE_L1_SIZE_BITS(bs) - riceParamPlus1;
3327 if (lzcount <
sizeof(bs_cache)*8) {
3333 extract_rice_param_part:
3334 bs_cache <<= lzcount;
3335 bs_consumedBits += lzcount;
3337 if (bs_consumedBits <= riceParamPlus1MaxConsumedBits) {
3339 bs_cache <<= riceParamPlus1;
3340 bs_consumedBits += riceParamPlus1;
3348 drflac_uint32 riceParamPartLoBitCount = bs_consumedBits - riceParamPlus1MaxConsumedBits;
3349 drflac_assert(riceParamPartLoBitCount > 0 && riceParamPartLoBitCount < 32);
3352 if (bs->
nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) {
3353 #ifndef DR_FLAC_NO_CRC
3354 drflac__update_crc16(bs);
3357 bs_consumedBits = riceParamPartLoBitCount;
3358 #ifndef DR_FLAC_NO_CRC
3363 if (!drflac__reload_cache(bs)) {
3367 bs_cache = bs->
cache;
3368 bs_consumedBits = bs->
consumedBits + riceParamPartLoBitCount;
3371 bs_cache <<= riceParamPartLoBitCount;
3379 if (bs->
nextL2Line < DRFLAC_CACHE_L2_LINE_COUNT(bs)) {
3380 #ifndef DR_FLAC_NO_CRC
3381 drflac__update_crc16(bs);
3384 bs_consumedBits = 0;
3385 #ifndef DR_FLAC_NO_CRC
3390 if (!drflac__reload_cache(bs)) {
3394 bs_cache = bs->
cache;
3398 lzcount = drflac__clz(bs_cache);
3399 if (lzcount <
sizeof(bs_cache)*8) {
3404 goto extract_rice_param_part;
3408 bs->
cache = bs_cache;
3430 drflac_assert(bs !=
NULL);
3431 drflac_assert(
count > 0);
3432 drflac_assert(pSamplesOut !=
NULL);
3434 riceParamMask = ~((~0UL) << riceParam);
3435 pSamplesOutEnd = pSamplesOut + ((
count >> 2) << 2);
3437 if (bitsPerSample >= 24) {
3438 while (pSamplesOut < pSamplesOutEnd) {
3443 if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart0, &riceParamPart0) ||
3444 !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart1, &riceParamPart1) ||
3445 !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart2, &riceParamPart2) ||
3446 !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart3, &riceParamPart3)) {
3450 riceParamPart0 &= riceParamMask;
3451 riceParamPart1 &= riceParamMask;
3452 riceParamPart2 &= riceParamMask;
3453 riceParamPart3 &= riceParamMask;
3455 riceParamPart0 |= (zeroCountPart0 << riceParam);
3456 riceParamPart1 |= (zeroCountPart1 << riceParam);
3457 riceParamPart2 |= (zeroCountPart2 << riceParam);
3458 riceParamPart3 |= (zeroCountPart3 << riceParam);
3460 riceParamPart0 = (riceParamPart0 >> 1) ^
t[riceParamPart0 & 0x01];
3461 riceParamPart1 = (riceParamPart1 >> 1) ^
t[riceParamPart1 & 0x01];
3462 riceParamPart2 = (riceParamPart2 >> 1) ^
t[riceParamPart2 & 0x01];
3463 riceParamPart3 = (riceParamPart3 >> 1) ^
t[riceParamPart3 & 0x01];
3465 pSamplesOut[0] = riceParamPart0 + drflac__calculate_prediction_64(
order, shift, coefficients, pSamplesOut + 0);
3466 pSamplesOut[1] = riceParamPart1 + drflac__calculate_prediction_64(
order, shift, coefficients, pSamplesOut + 1);
3467 pSamplesOut[2] = riceParamPart2 + drflac__calculate_prediction_64(
order, shift, coefficients, pSamplesOut + 2);
3468 pSamplesOut[3] = riceParamPart3 + drflac__calculate_prediction_64(
order, shift, coefficients, pSamplesOut + 3);
3473 while (pSamplesOut < pSamplesOutEnd) {
3474 if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart0, &riceParamPart0) ||
3475 !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart1, &riceParamPart1) ||
3476 !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart2, &riceParamPart2) ||
3477 !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart3, &riceParamPart3)) {
3481 riceParamPart0 &= riceParamMask;
3482 riceParamPart1 &= riceParamMask;
3483 riceParamPart2 &= riceParamMask;
3484 riceParamPart3 &= riceParamMask;
3486 riceParamPart0 |= (zeroCountPart0 << riceParam);
3487 riceParamPart1 |= (zeroCountPart1 << riceParam);
3488 riceParamPart2 |= (zeroCountPart2 << riceParam);
3489 riceParamPart3 |= (zeroCountPart3 << riceParam);
3491 riceParamPart0 = (riceParamPart0 >> 1) ^
t[riceParamPart0 & 0x01];
3492 riceParamPart1 = (riceParamPart1 >> 1) ^
t[riceParamPart1 & 0x01];
3493 riceParamPart2 = (riceParamPart2 >> 1) ^
t[riceParamPart2 & 0x01];
3494 riceParamPart3 = (riceParamPart3 >> 1) ^
t[riceParamPart3 & 0x01];
3496 pSamplesOut[0] = riceParamPart0 + drflac__calculate_prediction_32(
order, shift, coefficients, pSamplesOut + 0);
3497 pSamplesOut[1] = riceParamPart1 + drflac__calculate_prediction_32(
order, shift, coefficients, pSamplesOut + 1);
3498 pSamplesOut[2] = riceParamPart2 + drflac__calculate_prediction_32(
order, shift, coefficients, pSamplesOut + 2);
3499 pSamplesOut[3] = riceParamPart3 + drflac__calculate_prediction_32(
order, shift, coefficients, pSamplesOut + 3);
3508 if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountPart0, &riceParamPart0)) {
3513 riceParamPart0 &= riceParamMask;
3514 riceParamPart0 |= (zeroCountPart0 << riceParam);
3515 riceParamPart0 = (riceParamPart0 >> 1) ^
t[riceParamPart0 & 0x01];
3519 if (bitsPerSample >= 24) {
3520 pSamplesOut[0] = riceParamPart0 + drflac__calculate_prediction_64(
order, shift, coefficients, pSamplesOut + 0);
3522 pSamplesOut[0] = riceParamPart0 + drflac__calculate_prediction_32(
order, shift, coefficients, pSamplesOut + 0);
3532 #if defined(DRFLAC_SUPPORT_SSE41)
3550 __m128i riceParamMask128;
3554 drflac_assert(bs !=
NULL);
3555 drflac_assert(
count > 0);
3556 drflac_assert(pSamplesOut !=
NULL);
3558 riceParamMask = ~((~0UL) << riceParam);
3559 riceParamMask128 = _mm_set1_epi32(riceParamMask);
3560 one = _mm_set1_epi32(0x01);
3562 pSamplesOutEnd = pSamplesOut + ((
count >> 2) << 2);
3564 if (bitsPerSample >= 24) {
3565 while (pSamplesOut < pSamplesOutEnd) {
3566 __m128i zeroCountPart128;
3567 __m128i riceParamPart128;
3571 if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts0, &riceParamParts0) ||
3572 !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts1, &riceParamParts1) ||
3573 !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts2, &riceParamParts2) ||
3574 !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts3, &riceParamParts3)) {
3578 zeroCountPart128 = _mm_set_epi32(zeroCountParts3, zeroCountParts2, zeroCountParts1, zeroCountParts0);
3579 riceParamPart128 = _mm_set_epi32(riceParamParts3, riceParamParts2, riceParamParts1, riceParamParts0);
3581 riceParamPart128 = _mm_and_si128(riceParamPart128, riceParamMask128);
3582 riceParamPart128 = _mm_or_si128(riceParamPart128, _mm_slli_epi32(zeroCountPart128, riceParam));
3583 riceParamPart128 = _mm_xor_si128(_mm_srli_epi32(riceParamPart128, 1), _mm_mullo_epi32(_mm_and_si128(riceParamPart128, one), _mm_set1_epi32(0xFFFFFFFF)));
3586 _mm_storeu_si128((__m128i*)riceParamParts, riceParamPart128);
3588 #if defined(DRFLAC_64BIT)
3590 drflac__calculate_prediction_64_x4(
order, shift, coefficients, riceParamParts, pSamplesOut);
3592 pSamplesOut[0] = riceParamParts[0] + drflac__calculate_prediction_64__sse41(
order, shift, coefficients, pSamplesOut + 0);
3593 pSamplesOut[1] = riceParamParts[1] + drflac__calculate_prediction_64__sse41(
order, shift, coefficients, pSamplesOut + 1);
3594 pSamplesOut[2] = riceParamParts[2] + drflac__calculate_prediction_64__sse41(
order, shift, coefficients, pSamplesOut + 2);
3595 pSamplesOut[3] = riceParamParts[3] + drflac__calculate_prediction_64__sse41(
order, shift, coefficients, pSamplesOut + 3);
3605 coefficients128[
i*4+0] = coefficients[
i];
3606 coefficients128[
i*4+1] = coefficients[
i];
3607 coefficients128[
i*4+2] = coefficients[
i];
3608 coefficients128[
i*4+3] = coefficients[
i];
3611 while (pSamplesOut < pSamplesOutEnd) {
3612 __m128i zeroCountPart128;
3613 __m128i riceParamPart128;
3618 if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts0, &riceParamParts0) ||
3619 !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts1, &riceParamParts1) ||
3620 !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts2, &riceParamParts2) ||
3621 !drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts3, &riceParamParts3)) {
3625 zeroCountPart128 = _mm_set_epi32(zeroCountParts3, zeroCountParts2, zeroCountParts1, zeroCountParts0);
3626 riceParamPart128 = _mm_set_epi32(riceParamParts3, riceParamParts2, riceParamParts1, riceParamParts0);
3628 if (!drflac__read_rice_parts_x4(bs, riceParam, zeroCountParts, riceParamParts)) {
3632 zeroCountPart128 = _mm_set_epi32(zeroCountParts[3], zeroCountParts[2], zeroCountParts[1], zeroCountParts[0]);
3633 riceParamPart128 = _mm_set_epi32(riceParamParts[3], riceParamParts[2], riceParamParts[1], riceParamParts[0]);
3636 riceParamPart128 = _mm_and_si128(riceParamPart128, riceParamMask128);
3637 riceParamPart128 = _mm_or_si128(riceParamPart128, _mm_slli_epi32(zeroCountPart128, riceParam));
3638 riceParamPart128 = _mm_xor_si128(_mm_srli_epi32(riceParamPart128, 1), _mm_mullo_epi32(_mm_and_si128(riceParamPart128, one), _mm_set1_epi32(0xFFFFFFFF)));
3641 drflac__calculate_prediction_32_x4__sse41(
order, shift, (
const __m128i*)coefficients128, riceParamPart128, pSamplesOut);
3643 _mm_storeu_si128((__m128i*)riceParamParts, riceParamPart128);
3645 pSamplesOut[0] = riceParamParts[0] + drflac__calculate_prediction_32(
order, shift, coefficients, pSamplesOut + 0);
3646 pSamplesOut[1] = riceParamParts[1] + drflac__calculate_prediction_32(
order, shift, coefficients, pSamplesOut + 1);
3647 pSamplesOut[2] = riceParamParts[2] + drflac__calculate_prediction_32(
order, shift, coefficients, pSamplesOut + 2);
3648 pSamplesOut[3] = riceParamParts[3] + drflac__calculate_prediction_32(
order, shift, coefficients, pSamplesOut + 3);
3659 if (!drflac__read_rice_parts_x1(bs, riceParam, &zeroCountParts0, &riceParamParts0)) {
3664 riceParamParts0 &= riceParamMask;
3665 riceParamParts0 |= (zeroCountParts0 << riceParam);
3666 riceParamParts0 = (riceParamParts0 >> 1) ^
t[riceParamParts0 & 0x01];
3669 if (bitsPerSample >= 24) {
3670 pSamplesOut[0] = riceParamParts0 + drflac__calculate_prediction_64(
order, shift, coefficients, pSamplesOut + 0);
3672 pSamplesOut[0] = riceParamParts0 + drflac__calculate_prediction_32(
order, shift, coefficients, pSamplesOut + 0);
3685 #if defined(DRFLAC_SUPPORT_SSE41)
3686 if (drflac__gIsSSE41Supported) {
3687 return drflac__decode_samples_with_residual__rice__sse41(bs, bitsPerSample,
count, riceParam,
order, shift, coefficients, pSamplesOut);
3693 return drflac__decode_samples_with_residual__rice__reference(bs, bitsPerSample,
count, riceParam,
order, shift, coefficients, pSamplesOut);
3695 return drflac__decode_samples_with_residual__rice__scalar(bs, bitsPerSample,
count, riceParam,
order, shift, coefficients, pSamplesOut);
3705 drflac_assert(bs !=
NULL);
3706 drflac_assert(
count > 0);
3709 if (!drflac__seek_rice_parts(bs, riceParam)) {
3721 drflac_assert(bs !=
NULL);
3722 drflac_assert(
count > 0);
3723 drflac_assert(unencodedBitsPerSample <= 31);
3724 drflac_assert(pSamplesOut !=
NULL);
3727 if (unencodedBitsPerSample > 0) {
3728 if (!drflac__read_int32(bs, unencodedBitsPerSample, pSamplesOut +
i)) {
3735 if (bitsPerSample > 16) {
3736 pSamplesOut[
i] += drflac__calculate_prediction_64(
order, shift, coefficients, pSamplesOut +
i);
3738 pSamplesOut[
i] += drflac__calculate_prediction_32(
order, shift, coefficients, pSamplesOut +
i);
3758 drflac_assert(bs !=
NULL);
3759 drflac_assert(blockSize != 0);
3760 drflac_assert(pDecodedSamples !=
NULL);
3762 if (!drflac__read_uint8(bs, 2, &residualMethod)) {
3766 if (residualMethod != DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE && residualMethod != DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2) {
3771 pDecodedSamples +=
order;
3773 if (!drflac__read_uint8(bs, 4, &partitionOrder)) {
3781 if (partitionOrder > 8) {
3786 if ((blockSize / (1 << partitionOrder)) <=
order) {
3790 samplesInPartition = (blockSize / (1 << partitionOrder)) -
order;
3791 partitionsRemaining = (1 << partitionOrder);
3794 if (residualMethod == DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE) {
3795 if (!drflac__read_uint8(bs, 4, &riceParam)) {
3798 if (riceParam == 15) {
3801 }
else if (residualMethod == DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2) {
3802 if (!drflac__read_uint8(bs, 5, &riceParam)) {
3805 if (riceParam == 31) {
3810 if (riceParam != 0xFF) {
3811 if (!drflac__decode_samples_with_residual__rice(bs, bitsPerSample, samplesInPartition, riceParam,
order, shift, coefficients, pDecodedSamples)) {
3815 unsigned char unencodedBitsPerSample = 0;
3816 if (!drflac__read_uint8(bs, 5, &unencodedBitsPerSample)) {
3820 if (!drflac__decode_samples_with_residual__unencoded(bs, bitsPerSample, samplesInPartition, unencodedBitsPerSample,
order, shift, coefficients, pDecodedSamples)) {
3825 pDecodedSamples += samplesInPartition;
3827 if (partitionsRemaining == 1) {
3831 partitionsRemaining -= 1;
3833 if (partitionOrder != 0) {
3834 samplesInPartition = blockSize / (1 << partitionOrder);
3853 drflac_assert(bs !=
NULL);
3854 drflac_assert(blockSize != 0);
3856 if (!drflac__read_uint8(bs, 2, &residualMethod)) {
3860 if (residualMethod != DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE && residualMethod != DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2) {
3864 if (!drflac__read_uint8(bs, 4, &partitionOrder)) {
3872 if (partitionOrder > 8) {
3877 if ((blockSize / (1 << partitionOrder)) <=
order) {
3881 samplesInPartition = (blockSize / (1 << partitionOrder)) -
order;
3882 partitionsRemaining = (1 << partitionOrder);
3886 if (residualMethod == DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE) {
3887 if (!drflac__read_uint8(bs, 4, &riceParam)) {
3890 if (riceParam == 15) {
3893 }
else if (residualMethod == DRFLAC_RESIDUAL_CODING_METHOD_PARTITIONED_RICE2) {
3894 if (!drflac__read_uint8(bs, 5, &riceParam)) {
3897 if (riceParam == 31) {
3902 if (riceParam != 0xFF) {
3903 if (!drflac__read_and_seek_residual__rice(bs, samplesInPartition, riceParam)) {
3907 unsigned char unencodedBitsPerSample = 0;
3908 if (!drflac__read_uint8(bs, 5, &unencodedBitsPerSample)) {
3912 if (!drflac__seek_bits(bs, unencodedBitsPerSample * samplesInPartition)) {
3918 if (partitionsRemaining == 1) {
3922 partitionsRemaining -= 1;
3923 samplesInPartition = blockSize / (1 << partitionOrder);
3936 if (!drflac__read_int32(bs, bitsPerSample, &
sample)) {
3944 for (
i = 0;
i < blockSize; ++
i) {
3955 for (
i = 0;
i < blockSize; ++
i) {
3957 if (!drflac__read_int32(bs, bitsPerSample, &
sample)) {
3980 for (
i = 0;
i < lpcOrder; ++
i) {
3982 if (!drflac__read_int32(bs, bitsPerSample, &
sample)) {
3989 if (!drflac__decode_samples_with_residual(bs, bitsPerSample, blockSize, lpcOrder, 0, lpcCoefficientsTable[lpcOrder], pDecodedSamples)) {
4004 for (
i = 0;
i < lpcOrder; ++
i) {
4006 if (!drflac__read_int32(bs, bitsPerSample, &
sample)) {
4013 if (!drflac__read_uint8(bs, 4, &lpcPrecision)) {
4016 if (lpcPrecision == 15) {
4021 if (!drflac__read_int8(bs, 5, &lpcShift)) {
4025 drflac_zero_memory(coefficients,
sizeof(coefficients));
4026 for (
i = 0;
i < lpcOrder; ++
i) {
4027 if (!drflac__read_int32(bs, lpcPrecision, coefficients +
i)) {
4032 if (!drflac__decode_samples_with_residual(bs, bitsPerSample, blockSize, lpcOrder, lpcShift, coefficients, pDecodedSamples)) {
4042 const drflac_uint32 sampleRateTable[12] = {0, 88200, 176400, 192000, 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000};
4045 drflac_assert(bs !=
NULL);
4046 drflac_assert(header !=
NULL);
4059 if (!drflac__find_and_seek_to_next_sync_code(bs)) {
4063 if (!drflac__read_uint8(bs, 1, &reserved)) {
4066 if (reserved == 1) {
4069 crc8 = drflac_crc8(crc8, reserved, 1);
4071 if (!drflac__read_uint8(bs, 1, &blockingStrategy)) {
4074 crc8 = drflac_crc8(crc8, blockingStrategy, 1);
4076 if (!drflac__read_uint8(bs, 4, &blockSize)) {
4079 if (blockSize == 0) {
4082 crc8 = drflac_crc8(crc8, blockSize, 4);
4084 if (!drflac__read_uint8(bs, 4, &sampleRate)) {
4087 crc8 = drflac_crc8(crc8, sampleRate, 4);
4089 if (!drflac__read_uint8(bs, 4, &channelAssignment)) {
4092 if (channelAssignment > 10) {
4095 crc8 = drflac_crc8(crc8, channelAssignment, 4);
4097 if (!drflac__read_uint8(bs, 3, &bitsPerSample)) {
4100 if (bitsPerSample == 3 || bitsPerSample == 7) {
4103 crc8 = drflac_crc8(crc8, bitsPerSample, 3);
4106 if (!drflac__read_uint8(bs, 1, &reserved)) {
4109 if (reserved == 1) {
4112 crc8 = drflac_crc8(crc8, reserved, 1);
4115 isVariableBlockSize = blockingStrategy == 1;
4116 if (isVariableBlockSize) {
4118 drflac_result result = drflac__read_utf8_coded_number(bs, &sampleNumber, &crc8);
4119 if (result != DRFLAC_SUCCESS) {
4120 if (result == DRFLAC_END_OF_STREAM) {
4130 drflac_result result = drflac__read_utf8_coded_number(bs, &frameNumber, &crc8);
4131 if (result != DRFLAC_SUCCESS) {
4132 if (result == DRFLAC_END_OF_STREAM) {
4143 if (blockSize == 1) {
4145 }
else if (blockSize >= 2 && blockSize <= 5) {
4146 header->
blockSize = 576 * (1 << (blockSize - 2));
4147 }
else if (blockSize == 6) {
4148 if (!drflac__read_uint16(bs, 8, &header->
blockSize)) {
4151 crc8 = drflac_crc8(crc8, header->
blockSize, 8);
4153 }
else if (blockSize == 7) {
4154 if (!drflac__read_uint16(bs, 16, &header->
blockSize)) {
4157 crc8 = drflac_crc8(crc8, header->
blockSize, 16);
4160 header->
blockSize = 256 * (1 << (blockSize - 8));
4164 if (sampleRate <= 11) {
4165 header->
sampleRate = sampleRateTable[sampleRate];
4166 }
else if (sampleRate == 12) {
4167 if (!drflac__read_uint32(bs, 8, &header->
sampleRate)) {
4170 crc8 = drflac_crc8(crc8, header->
sampleRate, 8);
4172 }
else if (sampleRate == 13) {
4173 if (!drflac__read_uint32(bs, 16, &header->
sampleRate)) {
4176 crc8 = drflac_crc8(crc8, header->
sampleRate, 16);
4177 }
else if (sampleRate == 14) {
4178 if (!drflac__read_uint32(bs, 16, &header->
sampleRate)) {
4181 crc8 = drflac_crc8(crc8, header->
sampleRate, 16);
4195 if (!drflac__read_uint8(bs, 8, &header->
crc8)) {
4199 #ifndef DR_FLAC_NO_CRC
4200 if (header->
crc8 != crc8) {
4213 if (!drflac__read_uint8(bs, 8, &header)) {
4218 if ((header & 0x80) != 0) {
4222 type = (header & 0x7E) >> 1;
4225 }
else if (
type == 1) {
4228 if ((
type & 0x20) != 0) {
4231 }
else if ((
type & 0x08) != 0) {
4243 if (pSubframe->
subframeType == DRFLAC_SUBFRAME_RESERVED) {
4249 if ((header & 0x01) == 1) {
4250 unsigned int wastedBitsPerSample;
4251 if (!drflac__seek_past_next_set_bit(bs, &wastedBitsPerSample)) {
4264 drflac_assert(bs !=
NULL);
4265 drflac_assert(frame !=
NULL);
4267 pSubframe = frame->
subframes + subframeIndex;
4268 if (!drflac__read_subframe_header(bs, pSubframe)) {
4289 case DRFLAC_SUBFRAME_CONSTANT:
4294 case DRFLAC_SUBFRAME_VERBATIM:
4299 case DRFLAC_SUBFRAME_FIXED:
4304 case DRFLAC_SUBFRAME_LPC:
4319 drflac_assert(bs !=
NULL);
4320 drflac_assert(frame !=
NULL);
4322 pSubframe = frame->
subframes + subframeIndex;
4323 if (!drflac__read_subframe_header(bs, pSubframe)) {
4344 case DRFLAC_SUBFRAME_CONSTANT:
4351 case DRFLAC_SUBFRAME_VERBATIM:
4354 if (!drflac__seek_bits(bs, bitsToSeek)) {
4359 case DRFLAC_SUBFRAME_FIXED:
4362 if (!drflac__seek_bits(bs, bitsToSeek)) {
4371 case DRFLAC_SUBFRAME_LPC:
4373 unsigned char lpcPrecision;
4376 if (!drflac__seek_bits(bs, bitsToSeek)) {
4380 if (!drflac__read_uint8(bs, 4, &lpcPrecision)) {
4383 if (lpcPrecision == 15) {
4389 bitsToSeek = (pSubframe->
lpcOrder * lpcPrecision) + 5;
4390 if (!drflac__seek_bits(bs, bitsToSeek)) {
4406 static DRFLAC_INLINE
drflac_uint8 drflac__get_channel_count_from_channel_assignment(
drflac_int8 channelAssignment)
4408 drflac_uint8 lookup[] = {1, 2, 3, 4, 5, 6, 7, 8, 2, 2, 2};
4410 drflac_assert(channelAssignment <= 10);
4411 return lookup[channelAssignment];
4414 static drflac_result drflac__decode_flac_frame(
drflac* pFlac)
4420 #ifndef DR_FLAC_NO_CRC
4429 return DRFLAC_ERROR;
4434 if (channelCount != (
int)pFlac->
channels) {
4435 return DRFLAC_ERROR;
4438 for (
i = 0;
i < channelCount; ++
i) {
4440 return DRFLAC_ERROR;
4444 paddingSizeInBits = DRFLAC_CACHE_L1_BITS_REMAINING(&pFlac->
bs) & 7;
4445 if (paddingSizeInBits > 0) {
4447 if (!drflac__read_uint8(&pFlac->
bs, paddingSizeInBits, &padding)) {
4448 return DRFLAC_END_OF_STREAM;
4452 #ifndef DR_FLAC_NO_CRC
4453 actualCRC16 = drflac__flush_crc16(&pFlac->
bs);
4455 if (!drflac__read_uint16(&pFlac->
bs, 16, &desiredCRC16)) {
4456 return DRFLAC_END_OF_STREAM;
4459 #ifndef DR_FLAC_NO_CRC
4460 if (actualCRC16 != desiredCRC16) {
4461 return DRFLAC_CRC_MISMATCH;
4467 return DRFLAC_SUCCESS;
4470 static drflac_result drflac__seek_flac_frame(
drflac* pFlac)
4475 #ifndef DR_FLAC_NO_CRC
4480 for (
i = 0;
i < channelCount; ++
i) {
4482 return DRFLAC_ERROR;
4487 if (!drflac__seek_bits(&pFlac->
bs, DRFLAC_CACHE_L1_BITS_REMAINING(&pFlac->
bs) & 7)) {
4488 return DRFLAC_ERROR;
4492 #ifndef DR_FLAC_NO_CRC
4493 actualCRC16 = drflac__flush_crc16(&pFlac->
bs);
4495 if (!drflac__read_uint16(&pFlac->
bs, 16, &desiredCRC16)) {
4496 return DRFLAC_END_OF_STREAM;
4499 #ifndef DR_FLAC_NO_CRC
4500 if (actualCRC16 != desiredCRC16) {
4501 return DRFLAC_CRC_MISMATCH;
4505 return DRFLAC_SUCCESS;
4510 drflac_assert(pFlac !=
NULL);
4513 drflac_result result;
4519 result = drflac__decode_flac_frame(pFlac);
4520 if (result != DRFLAC_SUCCESS) {
4521 if (result == DRFLAC_CRC_MISMATCH) {
4535 unsigned int channelCount;
4539 drflac_assert(pFlac !=
NULL);
4544 if (firstSampleInFrame == 0) {
4549 if (lastSampleInFrame > 0) {
4550 lastSampleInFrame -= 1;
4553 if (pFirstSampleInFrameOut) {
4554 *pFirstSampleInFrameOut = firstSampleInFrame;
4556 if (pLastSampleInFrameOut) {
4557 *pLastSampleInFrameOut = lastSampleInFrame;
4568 drflac_assert(pFlac !=
NULL);
4571 if (firstPCMFrame == 0) {
4576 if (lastPCMFrame > 0) {
4580 if (pFirstPCMFrame) {
4581 *pFirstPCMFrame = firstPCMFrame;
4583 if (pLastPCMFrame) {
4584 *pLastPCMFrame = lastPCMFrame;
4593 drflac_assert(pFlac !=
NULL);
4603 static DRFLAC_INLINE drflac_result drflac__seek_to_next_flac_frame(
drflac* pFlac)
4606 drflac_assert(pFlac !=
NULL);
4607 return drflac__seek_flac_frame(pFlac);
4613 while (samplesToRead > 0) {
4615 if (!drflac__read_and_decode_next_flac_frame(pFlac)) {
4620 samplesRead += samplesToRead;
4637 return drflac__seek_forward_by_samples(pFlac, pcmFramesToSeek*pFlac->
channels);
4645 drflac_assert(pFlac !=
NULL);
4662 runningSampleCount = 0;
4665 if (!drflac__seek_to_first_frame(pFlac)) {
4684 drflac__get_current_frame_sample_range(pFlac, &firstSampleInFrame, &lastSampleInFrame);
4686 sampleCountInThisFrame = (lastSampleInFrame - firstSampleInFrame) + 1;
4687 if (sampleIndex < (runningSampleCount + sampleCountInThisFrame)) {
4692 drflac_uint64 samplesToDecode = sampleIndex - runningSampleCount;
4695 drflac_result result = drflac__decode_flac_frame(pFlac);
4696 if (result == DRFLAC_SUCCESS) {
4698 return drflac__seek_forward_by_samples(pFlac, samplesToDecode) == samplesToDecode;
4700 if (result == DRFLAC_CRC_MISMATCH) {
4701 goto next_iteration;
4708 return drflac__seek_forward_by_samples(pFlac, samplesToDecode) == samplesToDecode;
4716 drflac_result result = drflac__seek_to_next_flac_frame(pFlac);
4717 if (result == DRFLAC_SUCCESS) {
4718 runningSampleCount += sampleCountInThisFrame;
4720 if (result == DRFLAC_CRC_MISMATCH) {
4721 goto next_iteration;
4753 drflac_assert(pFlac !=
NULL);
4759 for (iSeekpoint = 0; iSeekpoint < pFlac->
seekpointCount; ++iSeekpoint) {
4764 iClosestSeekpoint = iSeekpoint;
4801 drflac__get_current_frame_sample_range(pFlac, &firstSampleInFrame, &lastSampleInFrame);
4803 sampleCountInThisFrame = (lastSampleInFrame - firstSampleInFrame) + 1;
4804 if (sampleIndex < (runningSampleCount + sampleCountInThisFrame)) {
4809 drflac_uint64 samplesToDecode = sampleIndex - runningSampleCount;
4812 drflac_result result = drflac__decode_flac_frame(pFlac);
4813 if (result == DRFLAC_SUCCESS) {
4815 return drflac__seek_forward_by_samples(pFlac, samplesToDecode) == samplesToDecode;
4817 if (result == DRFLAC_CRC_MISMATCH) {
4818 goto next_iteration;
4825 return drflac__seek_forward_by_samples(pFlac, samplesToDecode) == samplesToDecode;
4833 drflac_result result = drflac__seek_to_next_flac_frame(pFlac);
4834 if (result == DRFLAC_SUCCESS) {
4835 runningSampleCount += sampleCountInThisFrame;
4837 if (result == DRFLAC_CRC_MISMATCH) {
4838 goto next_iteration;
4863 #ifndef DR_FLAC_NO_OGG
4875 } drflac_ogg_page_header;
4897 #ifndef DR_FLAC_NO_OGG
4900 drflac_ogg_page_header oggBosHeader;
4906 blockHeader = drflac__be2host_32(blockHeader);
4907 *isLastBlock = (blockHeader & 0x80000000UL) >> 31;
4908 *blockType = (blockHeader & 0x7F000000UL) >> 24;
4909 *blockSize = (blockHeader & 0x00FFFFFFUL);
4915 if (onRead(pUserData, &blockHeader, 4) != 4) {
4919 drflac__decode_block_header(blockHeader, isLastBlock, blockType, blockSize);
4931 if (onRead(pUserData, &blockSizes, 4) != 4) {
4936 if (onRead(pUserData, &frameSizes, 6) != 6) {
4941 if (onRead(pUserData, &importantProps, 8) != 8) {
4946 if (onRead(pUserData, md5,
sizeof(md5)) !=
sizeof(md5)) {
4950 blockSizes = drflac__be2host_32(blockSizes);
4951 frameSizes = drflac__be2host_64(frameSizes);
4952 importantProps = drflac__be2host_64(importantProps);
4954 pStreamInfo->
minBlockSize = (blockSizes & 0xFFFF0000) >> 16;
4962 drflac_copy_memory(pStreamInfo->
md5, md5,
sizeof(md5));
4982 if (!drflac__read_and_decode_block_header(onRead, pUserData, &isLastBlock, &blockType, &blockSize)) {
4985 runningFilePos += 4;
4987 metadata.
type = blockType;
4995 if (blockSize < 4) {
5000 void* pRawData = DRFLAC_MALLOC(blockSize);
5001 if (pRawData ==
NULL) {
5005 if (onRead(pUserData, pRawData, blockSize) != blockSize) {
5006 DRFLAC_FREE(pRawData);
5015 onMeta(pUserDataMD, &metadata);
5017 DRFLAC_FREE(pRawData);
5023 seektablePos = runningFilePos;
5024 seektableSize = blockSize;
5030 pRawData = DRFLAC_MALLOC(blockSize);
5031 if (pRawData ==
NULL) {
5035 if (onRead(pUserData, pRawData, blockSize) != blockSize) {
5036 DRFLAC_FREE(pRawData);
5046 for (iSeekpoint = 0; iSeekpoint < metadata.
data.
seektable.seekpointCount; ++iSeekpoint) {
5053 onMeta(pUserDataMD, &metadata);
5055 DRFLAC_FREE(pRawData);
5061 if (blockSize < 8) {
5067 const char* pRunningData;
5068 const char* pRunningDataEnd;
5071 pRawData = DRFLAC_MALLOC(blockSize);
5072 if (pRawData ==
NULL) {
5076 if (onRead(pUserData, pRawData, blockSize) != blockSize) {
5077 DRFLAC_FREE(pRawData);
5084 pRunningData = (
const char*)pRawData;
5085 pRunningDataEnd = (
const char*)pRawData + blockSize;
5091 DRFLAC_FREE(pRawData);
5099 DRFLAC_FREE(pRawData);
5108 if (pRunningDataEnd - pRunningData < 4) {
5109 DRFLAC_FREE(pRawData);
5113 commentLength = drflac__le2host_32(*(
const drflac_uint32*)pRunningData); pRunningData += 4;
5114 if (pRunningDataEnd - pRunningData < (
drflac_int64)commentLength) {
5115 DRFLAC_FREE(pRawData);
5118 pRunningData += commentLength;
5121 onMeta(pUserDataMD, &metadata);
5123 DRFLAC_FREE(pRawData);
5129 if (blockSize < 396) {
5135 const char* pRunningData;
5136 const char* pRunningDataEnd;
5140 pRawData = DRFLAC_MALLOC(blockSize);
5141 if (pRawData ==
NULL) {
5145 if (onRead(pUserData, pRawData, blockSize) != blockSize) {
5146 DRFLAC_FREE(pRawData);
5153 pRunningData = (
const char*)pRawData;
5154 pRunningDataEnd = (
const char*)pRawData + blockSize;
5156 drflac_copy_memory(metadata.
data.
cuesheet.catalog, pRunningData, 128); pRunningData += 128;
5158 metadata.
data.
cuesheet.isCD = (pRunningData[0] & 0x80) != 0; pRunningData += 259;
5159 metadata.
data.
cuesheet.trackCount = pRunningData[0]; pRunningData += 1;
5163 for (iTrack = 0; iTrack < metadata.
data.
cuesheet.trackCount; ++iTrack) {
5167 if (pRunningDataEnd - pRunningData < 36) {
5168 DRFLAC_FREE(pRawData);
5174 indexCount = pRunningData[0]; pRunningData += 1;
5176 if (pRunningDataEnd - pRunningData < (
drflac_int64)indexPointSize) {
5177 DRFLAC_FREE(pRawData);
5182 for (iIndex = 0; iIndex < indexCount; ++iIndex) {
5189 onMeta(pUserDataMD, &metadata);
5191 DRFLAC_FREE(pRawData);
5197 if (blockSize < 32) {
5203 const char* pRunningData;
5204 const char* pRunningDataEnd;
5206 pRawData = DRFLAC_MALLOC(blockSize);
5207 if (pRawData ==
NULL) {
5211 if (onRead(pUserData, pRawData, blockSize) != blockSize) {
5212 DRFLAC_FREE(pRawData);
5219 pRunningData = (
const char*)pRawData;
5220 pRunningDataEnd = (
const char*)pRawData + blockSize;
5227 DRFLAC_FREE(pRawData);
5231 metadata.
data.
picture.descriptionLength = drflac__be2host_32(*(
const drflac_uint32*)pRunningData); pRunningData += 4;
5235 DRFLAC_FREE(pRawData);
5238 metadata.
data.
picture.description = pRunningData; pRunningData += metadata.
data.
picture.descriptionLength;
5242 metadata.
data.
picture.indexColorCount = drflac__be2host_32(*(
const drflac_uint32*)pRunningData); pRunningData += 4;
5243 metadata.
data.
picture.pictureDataSize = drflac__be2host_32(*(
const drflac_uint32*)pRunningData); pRunningData += 4;
5248 DRFLAC_FREE(pRawData);
5252 onMeta(pUserDataMD, &metadata);
5254 DRFLAC_FREE(pRawData);
5267 onMeta(pUserDataMD, &metadata);
5289 void* pRawData = DRFLAC_MALLOC(blockSize);
5290 if (pRawData ==
NULL) {
5294 if (onRead(pUserData, pRawData, blockSize) != blockSize) {
5295 DRFLAC_FREE(pRawData);
5301 onMeta(pUserDataMD, &metadata);
5303 DRFLAC_FREE(pRawData);
5309 if (onMeta ==
NULL && blockSize > 0) {
5315 runningFilePos += blockSize;
5321 *pSeektablePos = seektablePos;
5322 *pSeektableSize = seektableSize;
5323 *pFirstFramePos = runningFilePos;
5341 if (!drflac__read_and_decode_block_header(onRead, pUserData, &isLastBlock, &blockType, &blockSize)) {
5357 if (!drflac__read_next_flac_frame_header(&pInit->bs, 0, &pInit->firstFrameHeader)) {
5361 if (pInit->firstFrameHeader.bitsPerSample == 0) {
5365 pInit->sampleRate = pInit->firstFrameHeader.sampleRate;
5366 pInit->channels = drflac__get_channel_count_from_channel_assignment(pInit->firstFrameHeader.channelAssignment);
5367 pInit->bitsPerSample = pInit->firstFrameHeader.bitsPerSample;
5368 pInit->maxBlockSize = 65535;
5373 if (!drflac__read_streaminfo(onRead, pUserData, &streaminfo)) {
5379 pInit->channels = streaminfo.
channels;
5383 pInit->hasMetadataBlocks = !isLastBlock;
5391 onMeta(pUserDataMD, &metadata);
5398 #ifndef DR_FLAC_NO_OGG
5399 #define DRFLAC_OGG_MAX_PAGE_SIZE 65307
5400 #define DRFLAC_OGG_CAPTURE_PATTERN_CRC32 1605413199
5404 drflac_ogg_recover_on_crc_mismatch,
5405 drflac_ogg_fail_on_crc_mismatch
5406 } drflac_ogg_crc_mismatch_recovery;
5408 #ifndef DR_FLAC_NO_CRC
5410 0x00000000
L, 0x04C11DB7
L, 0x09823B6E
L, 0x0D4326D9
L,
5411 0x130476DC
L, 0x17C56B6B
L, 0x1A864DB2
L, 0x1E475005
L,
5412 0x2608EDB8
L, 0x22C9F00F
L, 0x2F8AD6D6
L, 0x2B4BCB61
L,
5413 0x350C9B64
L, 0x31CD86D3
L, 0x3C8EA00A
L, 0x384FBDBD
L,
5414 0x4C11DB70
L, 0x48D0C6C7
L, 0x4593E01E
L, 0x4152FDA9
L,
5415 0x5F15ADAC
L, 0x5BD4B01B
L, 0x569796C2
L, 0x52568B75
L,
5416 0x6A1936C8
L, 0x6ED82B7F
L, 0x639B0DA6
L, 0x675A1011
L,
5417 0x791D4014
L, 0x7DDC5DA3
L, 0x709F7B7A
L, 0x745E66CD
L,
5418 0x9823B6E0
L, 0x9CE2AB57
L, 0x91A18D8E
L, 0x95609039
L,
5419 0x8B27C03C
L, 0x8FE6DD8B
L, 0x82A5FB52
L, 0x8664E6E5
L,
5420 0xBE2B5B58
L, 0xBAEA46EF
L, 0xB7A96036
L, 0xB3687D81
L,
5421 0xAD2F2D84
L, 0xA9EE3033
L, 0xA4AD16EA
L, 0xA06C0B5D
L,
5422 0xD4326D90
L, 0xD0F37027
L, 0xDDB056FE
L, 0xD9714B49
L,
5423 0xC7361B4C
L, 0xC3F706FB
L, 0xCEB42022
L, 0xCA753D95
L,
5424 0xF23A8028
L, 0xF6FB9D9F
L, 0xFBB8BB46
L, 0xFF79A6F1
L,
5425 0xE13EF6F4
L, 0xE5FFEB43
L, 0xE8BCCD9A
L, 0xEC7DD02D
L,
5426 0x34867077
L, 0x30476DC0
L, 0x3D044B19
L, 0x39C556AE
L,
5427 0x278206AB
L, 0x23431B1C
L, 0x2E003DC5
L, 0x2AC12072
L,
5428 0x128E9DCF
L, 0x164F8078
L, 0x1B0CA6A1
L, 0x1FCDBB16
L,
5429 0x018AEB13
L, 0x054BF6A4
L, 0x0808D07D
L, 0x0CC9CDCA
L,
5430 0x7897AB07
L, 0x7C56B6B0
L, 0x71159069
L, 0x75D48DDE
L,
5431 0x6B93DDDB
L, 0x6F52C06C
L, 0x6211E6B5
L, 0x66D0FB02
L,
5432 0x5E9F46BF
L, 0x5A5E5B08
L, 0x571D7DD1
L, 0x53DC6066
L,
5433 0x4D9B3063
L, 0x495A2DD4
L, 0x44190B0D
L, 0x40D816BA
L,
5434 0xACA5C697
L, 0xA864DB20
L, 0xA527FDF9
L, 0xA1E6E04E
L,
5435 0xBFA1B04B
L, 0xBB60ADFC
L, 0xB6238B25
L, 0xB2E29692
L,
5436 0x8AAD2B2F
L, 0x8E6C3698
L, 0x832F1041
L, 0x87EE0DF6
L,
5437 0x99A95DF3
L, 0x9D684044
L, 0x902B669D
L, 0x94EA7B2A
L,
5438 0xE0B41DE7
L, 0xE4750050
L, 0xE9362689
L, 0xEDF73B3E
L,
5439 0xF3B06B3B
L, 0xF771768C
L, 0xFA325055
L, 0xFEF34DE2
L,
5440 0xC6BCF05F
L, 0xC27DEDE8
L, 0xCF3ECB31
L, 0xCBFFD686
L,
5441 0xD5B88683
L, 0xD1799B34
L, 0xDC3ABDED
L, 0xD8FBA05A
L,
5442 0x690CE0EE
L, 0x6DCDFD59
L, 0x608EDB80
L, 0x644FC637
L,
5443 0x7A089632
L, 0x7EC98B85
L, 0x738AAD5C
L, 0x774BB0EB
L,
5444 0x4F040D56
L, 0x4BC510E1
L, 0x46863638
L, 0x42472B8F
L,
5445 0x5C007B8A
L, 0x58C1663D
L, 0x558240E4
L, 0x51435D53
L,
5446 0x251D3B9E
L, 0x21DC2629
L, 0x2C9F00F0
L, 0x285E1D47
L,
5447 0x36194D42
L, 0x32D850F5
L, 0x3F9B762C
L, 0x3B5A6B9B
L,
5448 0x0315D626
L, 0x07D4CB91
L, 0x0A97ED48
L, 0x0E56F0FF
L,
5449 0x1011A0FA
L, 0x14D0BD4D
L, 0x19939B94
L, 0x1D528623
L,
5450 0xF12F560E
L, 0xF5EE4BB9
L, 0xF8AD6D60
L, 0xFC6C70D7
L,
5451 0xE22B20D2
L, 0xE6EA3D65
L, 0xEBA91BBC
L, 0xEF68060B
L,
5452 0xD727BBB6
L, 0xD3E6A601
L, 0xDEA580D8
L, 0xDA649D6F
L,
5453 0xC423CD6A
L, 0xC0E2D0DD
L, 0xCDA1F604
L, 0xC960EBB3
L,
5454 0xBD3E8D7E
L, 0xB9FF90C9
L, 0xB4BCB610
L, 0xB07DABA7
L,
5455 0xAE3AFBA2
L, 0xAAFBE615
L, 0xA7B8C0CC
L, 0xA379DD7B
L,
5456 0x9B3660C6
L, 0x9FF77D71
L, 0x92B45BA8
L, 0x9675461F
L,
5457 0x8832161A
L, 0x8CF30BAD
L, 0x81B02D74
L, 0x857130C3
L,
5458 0x5D8A9099
L, 0x594B8D2E
L, 0x5408ABF7
L, 0x50C9B640
L,
5459 0x4E8EE645
L, 0x4A4FFBF2
L, 0x470CDD2B
L, 0x43CDC09C
L,
5460 0x7B827D21
L, 0x7F436096
L, 0x7200464F
L, 0x76C15BF8
L,
5461 0x68860BFD
L, 0x6C47164A
L, 0x61043093
L, 0x65C52D24
L,
5462 0x119B4BE9
L, 0x155A565E
L, 0x18197087
L, 0x1CD86D30
L,
5463 0x029F3D35
L, 0x065E2082
L, 0x0B1D065B
L, 0x0FDC1BEC
L,
5464 0x3793A651
L, 0x3352BBE6
L, 0x3E119D3F
L, 0x3AD08088
L,
5465 0x2497D08D
L, 0x2056CD3A
L, 0x2D15EBE3
L, 0x29D4F654
L,
5466 0xC5A92679
L, 0xC1683BCE
L, 0xCC2B1D17
L, 0xC8EA00A0
L,
5467 0xD6AD50A5
L, 0xD26C4D12
L, 0xDF2F6BCB
L, 0xDBEE767C
L,
5468 0xE3A1CBC1
L, 0xE760D676
L, 0xEA23F0AF
L, 0xEEE2ED18
L,
5469 0xF0A5BD1D
L, 0xF464A0AA
L, 0xF9278673
L, 0xFDE69BC4
L,
5470 0x89B8FD09
L, 0x8D79E0BE
L, 0x803AC667
L, 0x84FBDBD0
L,
5471 0x9ABC8BD5
L, 0x9E7D9662
L, 0x933EB0BB
L, 0x97FFAD0C
L,
5472 0xAFB010B1
L, 0xAB710D06
L, 0xA6322BDF
L, 0xA2F33668
L,
5473 0xBCB4666D
L, 0xB8757BDA
L, 0xB5365D03
L, 0xB1F740B4
L
5479 #ifndef DR_FLAC_NO_CRC
5480 return (crc32 << 8) ^ drflac__crc32_table[(
drflac_uint8)((crc32 >> 24) & 0xFF) ^ data];
5490 crc32 = drflac_crc32_byte(crc32, (
drflac_uint8)((data >> 24) & 0xFF));
5491 crc32 = drflac_crc32_byte(crc32, (
drflac_uint8)((data >> 16) & 0xFF));
5492 crc32 = drflac_crc32_byte(crc32, (
drflac_uint8)((data >> 8) & 0xFF));
5493 crc32 = drflac_crc32_byte(crc32, (
drflac_uint8)((data >> 0) & 0xFF));
5499 crc32 = drflac_crc32_uint32(crc32, (
drflac_uint32)((data >> 32) & 0xFFFFFFFF));
5500 crc32 = drflac_crc32_uint32(crc32, (
drflac_uint32)((data >> 0) & 0xFFFFFFFF));
5509 for (
i = 0;
i < dataSize; ++
i) {
5510 crc32 = drflac_crc32_byte(crc32, pData[
i]);
5521 static DRFLAC_INLINE
drflac_uint32 drflac_ogg__get_page_header_size(drflac_ogg_page_header* pHeader)
5523 return 27 + pHeader->segmentCount;
5526 static DRFLAC_INLINE
drflac_uint32 drflac_ogg__get_page_body_size(drflac_ogg_page_header* pHeader)
5531 for (
i = 0;
i < pHeader->segmentCount; ++
i) {
5532 pageBodySize += pHeader->segmentTable[
i];
5535 return pageBodySize;
5543 drflac_assert(*pCRC32 == DRFLAC_OGG_CAPTURE_PATTERN_CRC32);
5545 if (onRead(pUserData, data, 23) != 23) {
5546 return DRFLAC_END_OF_STREAM;
5550 pHeader->structureVersion = data[0];
5551 pHeader->headerType = data[1];
5552 drflac_copy_memory(&pHeader->granulePosition, &data[ 2], 8);
5553 drflac_copy_memory(&pHeader->serialNumber, &data[10], 4);
5554 drflac_copy_memory(&pHeader->sequenceNumber, &data[14], 4);
5555 drflac_copy_memory(&pHeader->checksum, &data[18], 4);
5556 pHeader->segmentCount = data[22];
5564 for (
i = 0;
i < 23; ++
i) {
5565 *pCRC32 = drflac_crc32_byte(*pCRC32, data[
i]);
5569 if (onRead(pUserData, pHeader->segmentTable, pHeader->segmentCount) != pHeader->segmentCount) {
5570 return DRFLAC_END_OF_STREAM;
5572 *pBytesRead += pHeader->segmentCount;
5574 for (
i = 0;
i < pHeader->segmentCount; ++
i) {
5575 *pCRC32 = drflac_crc32_byte(*pCRC32, pHeader->segmentTable[
i]);
5578 return DRFLAC_SUCCESS;
5587 if (onRead(pUserData,
id, 4) != 4) {
5588 return DRFLAC_END_OF_STREAM;
5594 if (drflac_ogg__is_capture_pattern(
id)) {
5595 drflac_result result;
5597 *pCRC32 = DRFLAC_OGG_CAPTURE_PATTERN_CRC32;
5599 result = drflac_ogg__read_page_header_after_capture_pattern(onRead, pUserData, pHeader, pBytesRead, pCRC32);
5600 if (result == DRFLAC_SUCCESS) {
5601 return DRFLAC_SUCCESS;
5603 if (result == DRFLAC_CRC_MISMATCH) {
5614 if (onRead(pUserData, &
id[3], 1) != 1) {
5615 return DRFLAC_END_OF_STREAM;
5638 drflac_ogg_page_header bosPageHeader;
5639 drflac_ogg_page_header currentPageHeader;
5645 static size_t drflac_oggbs__read_physical(drflac_oggbs* oggbs,
void* bufferOut,
size_t bytesToRead)
5647 size_t bytesActuallyRead = oggbs->onRead(oggbs->pUserData, bufferOut, bytesToRead);
5648 oggbs->currentBytePos += bytesActuallyRead;
5650 return bytesActuallyRead;
5656 if (offset <= 0x7FFFFFFF) {
5660 oggbs->currentBytePos = offset;
5667 oggbs->currentBytePos = offset;
5672 while (offset > 0x7FFFFFFF) {
5676 oggbs->currentBytePos += 0x7FFFFFFF;
5677 offset -= 0x7FFFFFFF;
5683 oggbs->currentBytePos += offset;
5689 static drflac_bool32 drflac_oggbs__goto_next_page(drflac_oggbs* oggbs, drflac_ogg_crc_mismatch_recovery recoveryMethod)
5691 drflac_ogg_page_header header;
5696 #ifndef DR_FLAC_NO_CRC
5700 if (drflac_ogg__read_page_header(oggbs->onRead, oggbs->pUserData, &header, &bytesRead, &crc32) != DRFLAC_SUCCESS) {
5703 oggbs->currentBytePos += bytesRead;
5705 pageBodySize = drflac_ogg__get_page_body_size(&header);
5706 if (pageBodySize > DRFLAC_OGG_MAX_PAGE_SIZE) {
5710 if (header.serialNumber != oggbs->serialNumber) {
5720 if (drflac_oggbs__read_physical(oggbs, oggbs->pageData, pageBodySize) != pageBodySize) {
5723 oggbs->pageDataSize = pageBodySize;
5725 #ifndef DR_FLAC_NO_CRC
5726 actualCRC32 = drflac_crc32_buffer(crc32, oggbs->pageData, oggbs->pageDataSize);
5727 if (actualCRC32 != header.checksum) {
5728 if (recoveryMethod == drflac_ogg_recover_on_crc_mismatch) {
5736 drflac_oggbs__goto_next_page(oggbs, drflac_ogg_recover_on_crc_mismatch);
5741 (
void)recoveryMethod;
5744 oggbs->currentPageHeader = header;
5745 oggbs->bytesRemainingInPage = pageBodySize;
5752 static drflac_uint8 drflac_oggbs__get_current_segment_index(drflac_oggbs* oggbs,
drflac_uint8* pBytesRemainingInSeg)
5754 drflac_uint32 bytesConsumedInPage = drflac_ogg__get_page_body_size(&oggbs->currentPageHeader) - oggbs->bytesRemainingInPage;
5757 while (iByte < bytesConsumedInPage) {
5758 drflac_uint8 segmentSize = oggbs->currentPageHeader.segmentTable[iSeg];
5759 if (iByte + segmentSize > bytesConsumedInPage) {
5763 iByte += segmentSize;
5767 *pBytesRemainingInSeg = oggbs->currentPageHeader.segmentTable[iSeg] - (
drflac_uint8)(bytesConsumedInPage - iByte);
5771 static drflac_bool32 drflac_oggbs__seek_to_next_packet(drflac_oggbs* oggbs)
5778 drflac_uint8 iFirstSeg = drflac_oggbs__get_current_segment_index(oggbs, &bytesRemainingInSeg);
5780 drflac_uint32 bytesToEndOfPacketOrPage = bytesRemainingInSeg;
5781 for (
drflac_uint8 iSeg = iFirstSeg; iSeg < oggbs->currentPageHeader.segmentCount; ++iSeg) {
5782 drflac_uint8 segmentSize = oggbs->currentPageHeader.segmentTable[iSeg];
5783 if (segmentSize < 255) {
5784 if (iSeg == oggbs->currentPageHeader.segmentCount-1) {
5791 bytesToEndOfPacketOrPage += segmentSize;
5799 oggbs->bytesRemainingInPage -= bytesToEndOfPacketOrPage;
5806 if (!drflac_oggbs__goto_next_page(oggbs)) {
5811 if ((oggbs->currentPageHeader.headerType & 0x01) == 0) {
5821 static drflac_bool32 drflac_oggbs__seek_to_next_frame(drflac_oggbs* oggbs)
5826 return drflac_oggbs__seek_to_next_packet(oggbs);
5830 static size_t drflac__on_read_ogg(
void* pUserData,
void* bufferOut,
size_t bytesToRead)
5832 drflac_oggbs* oggbs = (drflac_oggbs*)pUserData;
5834 size_t bytesRead = 0;
5836 drflac_assert(oggbs !=
NULL);
5837 drflac_assert(pRunningBufferOut !=
NULL);
5840 while (bytesRead < bytesToRead) {
5841 size_t bytesRemainingToRead = bytesToRead - bytesRead;
5843 if (oggbs->bytesRemainingInPage >= bytesRemainingToRead) {
5844 drflac_copy_memory(pRunningBufferOut, oggbs->pageData + (oggbs->pageDataSize - oggbs->bytesRemainingInPage), bytesRemainingToRead);
5845 bytesRead += bytesRemainingToRead;
5846 oggbs->bytesRemainingInPage -= (
drflac_uint32)bytesRemainingToRead;
5851 if (oggbs->bytesRemainingInPage > 0) {
5852 drflac_copy_memory(pRunningBufferOut, oggbs->pageData + (oggbs->pageDataSize - oggbs->bytesRemainingInPage), oggbs->bytesRemainingInPage);
5853 bytesRead += oggbs->bytesRemainingInPage;
5854 pRunningBufferOut += oggbs->bytesRemainingInPage;
5855 oggbs->bytesRemainingInPage = 0;
5858 drflac_assert(bytesRemainingToRead > 0);
5859 if (!drflac_oggbs__goto_next_page(oggbs, drflac_ogg_recover_on_crc_mismatch)) {
5869 drflac_oggbs* oggbs = (drflac_oggbs*)pUserData;
5870 int bytesSeeked = 0;
5872 drflac_assert(oggbs !=
NULL);
5873 drflac_assert(offset >= 0);
5881 if (!drflac_oggbs__goto_next_page(oggbs, drflac_ogg_fail_on_crc_mismatch)) {
5890 while (bytesSeeked < offset) {
5891 int bytesRemainingToSeek = offset - bytesSeeked;
5892 drflac_assert(bytesRemainingToSeek >= 0);
5894 if (oggbs->bytesRemainingInPage >= (
size_t)bytesRemainingToSeek) {
5895 bytesSeeked += bytesRemainingToSeek;
5896 oggbs->bytesRemainingInPage -= bytesRemainingToSeek;
5901 if (oggbs->bytesRemainingInPage > 0) {
5902 bytesSeeked += (
int)oggbs->bytesRemainingInPage;
5903 oggbs->bytesRemainingInPage = 0;
5906 drflac_assert(bytesRemainingToSeek > 0);
5907 if (!drflac_oggbs__goto_next_page(oggbs, drflac_ogg_fail_on_crc_mismatch)) {
5918 drflac_oggbs* oggbs = (drflac_oggbs*)pFlac->
_oggbs;
5924 drflac_assert(oggbs !=
NULL);
5926 originalBytePos = oggbs->currentBytePos;
5932 oggbs->bytesRemainingInPage = 0;
5934 runningGranulePosition = 0;
5935 runningFrameBytePos = oggbs->currentBytePos;
5937 if (!drflac_oggbs__goto_next_page(oggbs, drflac_ogg_recover_on_crc_mismatch)) {
5942 runningFrameBytePos = oggbs->currentBytePos - drflac_ogg__get_page_header_size(&oggbs->currentPageHeader) - oggbs->pageDataSize;
5943 if (oggbs->currentPageHeader.granulePosition*pFlac->
channels >= sampleIndex) {
5951 if ((oggbs->currentPageHeader.headerType & 0x01) == 0) {
5952 if (oggbs->currentPageHeader.segmentTable[0] >= 2) {
5954 firstBytesInPage[0] = oggbs->pageData[0];
5955 firstBytesInPage[1] = oggbs->pageData[1];
5957 if ((firstBytesInPage[0] == 0xFF) && (firstBytesInPage[1] & 0xFC) == 0xF8) {
5958 runningGranulePosition = oggbs->currentPageHeader.granulePosition*pFlac->
channels;
5975 if (!drflac_oggbs__goto_next_page(oggbs, drflac_ogg_recover_on_crc_mismatch)) {
5983 runningSampleCount = runningGranulePosition;
6014 drflac__get_current_frame_sample_range(pFlac, &firstSampleInFrame, &lastSampleInFrame);
6016 sampleCountInThisFrame = (lastSampleInFrame - firstSampleInFrame) + 1;
6017 if (sampleIndex < (runningSampleCount + sampleCountInThisFrame)) {
6022 drflac_result result = drflac__decode_flac_frame(pFlac);
6023 if (result == DRFLAC_SUCCESS) {
6025 drflac_uint64 samplesToDecode = (size_t)(sampleIndex - runningSampleCount);
6026 if (samplesToDecode == 0) {
6029 return drflac__seek_forward_by_samples(pFlac, samplesToDecode) == samplesToDecode;
6031 if (result == DRFLAC_CRC_MISMATCH) {
6042 drflac_result result = drflac__seek_to_next_flac_frame(pFlac);
6043 if (result == DRFLAC_SUCCESS) {
6044 runningSampleCount += sampleCountInThisFrame;
6046 if (result == DRFLAC_CRC_MISMATCH) {
6059 drflac_ogg_page_header header;
6067 pInit->oggFirstBytePos = 0;
6074 if (drflac_ogg__read_page_header_after_capture_pattern(onRead, pUserData, &header, &bytesRead, &crc32) != DRFLAC_SUCCESS) {
6077 pInit->runningFilePos += bytesRead;
6083 if ((header.headerType & 0x02) == 0) {
6088 pageBodySize = drflac_ogg__get_page_body_size(&header);
6089 if (pageBodySize == 51) {
6094 if (onRead(pUserData, &packetType, 1) != 1) {
6098 bytesRemainingInPage -= 1;
6099 if (packetType == 0x7F) {
6102 if (onRead(pUserData, sig, 4) != 4) {
6106 bytesRemainingInPage -= 4;
6107 if (sig[0] ==
'F' && sig[1] ==
'L' && sig[2] ==
'A' && sig[3] ==
'C') {
6110 if (onRead(pUserData, mappingVersion, 2) != 2) {
6114 if (mappingVersion[0] != 1) {
6127 if (onRead(pUserData, sig, 4) != 4) {
6131 if (sig[0] ==
'f' && sig[1] ==
'L' && sig[2] ==
'a' && sig[3] ==
'C') {
6137 if (!drflac__read_and_decode_block_header(onRead, pUserData, &isLastBlock, &blockType, &blockSize)) {
6145 if (drflac__read_streaminfo(onRead, pUserData, &streaminfo)) {
6149 pInit->channels = streaminfo.
channels;
6153 pInit->hasMetadataBlocks = !isLastBlock;
6161 onMeta(pUserDataMD, &metadata);
6164 pInit->runningFilePos += pageBodySize;
6165 pInit->oggFirstBytePos = pInit->runningFilePos - 79;
6166 pInit->oggSerial = header.serialNumber;
6167 pInit->oggBosHeader = header;
6195 pInit->runningFilePos += pageBodySize;
6199 if (drflac_ogg__read_page_header(onRead, pUserData, &header, &bytesRead, &crc32) != DRFLAC_SUCCESS) {
6202 pInit->runningFilePos += bytesRead;
6220 if (pInit ==
NULL || onRead ==
NULL || onSeek ==
NULL) {
6224 drflac_zero_memory(pInit,
sizeof(*pInit));
6225 pInit->onRead = onRead;
6226 pInit->onSeek = onSeek;
6227 pInit->onMeta = onMeta;
6228 pInit->container = container;
6229 pInit->pUserData = pUserData;
6230 pInit->pUserDataMD = pUserDataMD;
6232 pInit->bs.onRead = onRead;
6233 pInit->bs.onSeek = onSeek;
6234 pInit->bs.pUserData = pUserData;
6235 drflac__reset_cache(&pInit->bs);
6243 if (onRead(pUserData,
id, 4) != 4) {
6246 pInit->runningFilePos += 4;
6248 if (
id[0] ==
'I' &&
id[1] ==
'D' &&
id[2] ==
'3') {
6253 if (onRead(pUserData, header, 6) != 6) {
6256 pInit->runningFilePos += 6;
6260 drflac_copy_memory(&headerSize, header+2, 4);
6261 headerSize = drflac__unsynchsafe_32(drflac__be2host_32(headerSize));
6269 pInit->runningFilePos += headerSize;
6275 if (
id[0] ==
'f' &&
id[1] ==
'L' &&
id[2] ==
'a' &&
id[3] ==
'C') {
6276 return drflac__init_private__native(pInit, onRead, onSeek, onMeta, pUserData, pUserDataMD, relaxed);
6278 #ifndef DR_FLAC_NO_OGG
6279 if (
id[0] ==
'O' &&
id[1] ==
'g' &&
id[2] ==
'g' &&
id[3] ==
'S') {
6280 return drflac__init_private__ogg(pInit, onRead, onSeek, onMeta, pUserData, pUserDataMD, relaxed);
6287 return drflac__init_private__native(pInit, onRead, onSeek, onMeta, pUserData, pUserDataMD, relaxed);
6289 #ifndef DR_FLAC_NO_OGG
6291 return drflac__init_private__ogg(pInit, onRead, onSeek, onMeta, pUserData, pUserDataMD, relaxed);
6300 void drflac__init_from_info(
drflac* pFlac, drflac_init_info* pInit)
6302 drflac_assert(pFlac !=
NULL);
6303 drflac_assert(pInit !=
NULL);
6305 drflac_zero_memory(pFlac,
sizeof(*pFlac));
6306 pFlac->
bs = pInit->bs;
6307 pFlac->
onMeta = pInit->onMeta;
6320 drflac_init_info init;
6324 #ifndef DR_FLAC_NO_OGG
6333 #ifndef DRFLAC_NO_CPUID
6335 drflac__init_cpu_caps();
6338 if (!drflac__init_private(&init, onRead, onSeek, onMeta, container, pUserData, pUserDataMD)) {
6351 allocationSize =
sizeof(
drflac);
6357 if (((init.maxBlockSize+DRFLAC_LEADING_SAMPLES) % (DRFLAC_MAX_SIMD_VECTOR_SIZE /
sizeof(
drflac_int32))) == 0) {
6358 wholeSIMDVectorCountPerChannel = ((init.maxBlockSize+DRFLAC_LEADING_SAMPLES) / (DRFLAC_MAX_SIMD_VECTOR_SIZE /
sizeof(
drflac_int32)));
6360 wholeSIMDVectorCountPerChannel = ((init.maxBlockSize+DRFLAC_LEADING_SAMPLES) / (DRFLAC_MAX_SIMD_VECTOR_SIZE /
sizeof(
drflac_int32))) + 1;
6363 decodedSamplesAllocationSize = wholeSIMDVectorCountPerChannel * DRFLAC_MAX_SIMD_VECTOR_SIZE * init.channels;
6365 allocationSize += decodedSamplesAllocationSize;
6366 allocationSize += DRFLAC_MAX_SIMD_VECTOR_SIZE;
6368 #ifndef DR_FLAC_NO_OGG
6370 oggbsAllocationSize = 0;
6372 oggbsAllocationSize =
sizeof(drflac_oggbs);
6373 allocationSize += oggbsAllocationSize;
6376 drflac_zero_memory(&oggbs,
sizeof(oggbs));
6378 oggbs.onRead = onRead;
6379 oggbs.onSeek = onSeek;
6380 oggbs.pUserData = pUserData;
6381 oggbs.currentBytePos = init.oggFirstBytePos;
6382 oggbs.firstBytePos = init.oggFirstBytePos;
6383 oggbs.serialNumber = init.oggSerial;
6384 oggbs.bosPageHeader = init.oggBosHeader;
6385 oggbs.bytesRemainingInPage = 0;
6397 if (init.hasMetadataBlocks) {
6400 void* pUserDataOverride = pUserData;
6402 #ifndef DR_FLAC_NO_OGG
6404 onReadOverride = drflac__on_read_ogg;
6405 onSeekOverride = drflac__on_seek_ogg;
6406 pUserDataOverride = (
void*)&oggbs;
6410 if (!drflac__read_and_decode_metadata(onReadOverride, onSeekOverride, onMeta, pUserDataOverride, pUserDataMD, &firstFramePos, &seektablePos, &seektableSize)) {
6414 allocationSize += seektableSize;
6418 pFlac = (
drflac*)DRFLAC_MALLOC(allocationSize);
6419 drflac__init_from_info(pFlac, &init);
6422 #ifndef DR_FLAC_NO_OGG
6424 drflac_oggbs* pInternalOggbs = (drflac_oggbs*)((
drflac_uint8*)pFlac->
pDecodedSamples + decodedSamplesAllocationSize + seektableSize);
6425 *pInternalOggbs = oggbs;
6428 pFlac->
bs.
onRead = drflac__on_read_ogg;
6429 pFlac->
bs.
onSeek = drflac__on_seek_ogg;
6431 pFlac->
_oggbs = (
void*)pInternalOggbs;
6438 #ifndef DR_FLAC_NO_OGG
6448 if (seektablePos != 0) {
6457 for (iSeekpoint = 0; iSeekpoint < pFlac->
seekpointCount; ++iSeekpoint) {
6486 if (!init.hasStreamInfoBlock) {
6490 drflac_result result = drflac__decode_flac_frame(pFlac);
6491 if (result == DRFLAC_SUCCESS) {
6494 if (result == DRFLAC_CRC_MISMATCH) {
6513 #ifndef DR_FLAC_NO_STDIO
6516 static size_t drflac__on_read_stdio(
void* pUserData,
void* bufferOut,
size_t bytesToRead)
6518 return fread(bufferOut, 1, bytesToRead, (FILE*)pUserData);
6523 drflac_assert(offset >= 0);
6528 static FILE* drflac__fopen(
const char* filename)
6531 #if defined(_MSC_VER) && _MSC_VER >= 1400
6532 if (fopen_s(&pFile, filename,
"rb") != 0) {
6536 pFile = fopen(filename,
"rb");
6537 if (pFile ==
NULL) {
6551 pFile = drflac__fopen(filename);
6552 if (pFile ==
NULL) {
6556 pFlac =
drflac_open(drflac__on_read_stdio, drflac__on_seek_stdio, (
void*)pFile);
6557 if (pFlac ==
NULL) {
6570 pFile = drflac__fopen(filename);
6571 if (pFile ==
NULL) {
6575 pFlac = drflac_open_with_metadata_private(drflac__on_read_stdio, drflac__on_seek_stdio, onMeta,
drflac_container_unknown, (
void*)pFile, pUserData);
6576 if (pFlac ==
NULL) {
6585 static size_t drflac__on_read_memory(
void* pUserData,
void* bufferOut,
size_t bytesToRead)
6588 size_t bytesRemaining;
6590 drflac_assert(memoryStream !=
NULL);
6594 if (bytesToRead > bytesRemaining) {
6595 bytesToRead = bytesRemaining;
6598 if (bytesToRead > 0) {
6599 drflac_copy_memory(bufferOut, memoryStream->
data + memoryStream->
currentReadPos, bytesToRead);
6610 drflac_assert(memoryStream !=
NULL);
6611 drflac_assert(offset >= 0);
6618 if (memoryStream->
currentReadPos + offset <= memoryStream->dataSize) {
6639 memoryStream.
data = (
const unsigned char*)data;
6642 pFlac =
drflac_open(drflac__on_read_memory, drflac__on_seek_memory, &memoryStream);
6643 if (pFlac ==
NULL) {
6650 #ifndef DR_FLAC_NO_OGG
6653 drflac_oggbs* oggbs = (drflac_oggbs*)pFlac->
_oggbs;
6670 memoryStream.
data = (
const unsigned char*)data;
6673 pFlac = drflac_open_with_metadata_private(drflac__on_read_memory, drflac__on_seek_memory, onMeta,
drflac_container_unknown, &memoryStream, pUserData);
6674 if (pFlac ==
NULL) {
6681 #ifndef DR_FLAC_NO_OGG
6684 drflac_oggbs* oggbs = (drflac_oggbs*)pFlac->
_oggbs;
6704 return drflac_open_with_metadata_private(onRead, onSeek,
NULL, container, pUserData, pUserData);
6713 return drflac_open_with_metadata_private(onRead, onSeek, onMeta, container, pUserData, pUserData);
6718 if (pFlac ==
NULL) {
6722 #ifndef DR_FLAC_NO_STDIO
6727 if (pFlac->
bs.
onRead == drflac__on_read_stdio) {
6731 #ifndef DR_FLAC_NO_OGG
6734 drflac_oggbs* oggbs = (drflac_oggbs*)pFlac->
_oggbs;
6735 drflac_assert(pFlac->
bs.
onRead == drflac__on_read_ogg);
6737 if (oggbs->onRead == drflac__on_read_stdio) {
6738 fclose((FILE*)oggbs->pUserData);
6753 drflac_assert(samplesToRead < channelCount);
6757 while (samplesToRead > 0) {
6760 drflac_uint64 channelIndex = samplesReadFromFrameSoFar % channelCount;
6761 drflac_uint64 nextSampleInFrame = samplesReadFromFrameSoFar / channelCount;
6762 int decodedSample = 0;
6766 case DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE:
6768 if (channelIndex == 0) {
6773 decodedSample = left - side;
6777 case DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE:
6779 if (channelIndex == 0) {
6782 decodedSample = side +
right;
6788 case DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE:
6792 if (channelIndex == 0) {
6796 mid = (((
unsigned int)mid) << 1) | (side & 0x01);
6797 decodedSample = (mid + side) >> 1;
6802 mid = (((
unsigned int)mid) << 1) | (side & 0x01);
6803 decodedSample = (mid - side) >> 1;
6807 case DRFLAC_CHANNEL_ASSIGNMENT_INDEPENDENT:
6817 *bufferOut++ = decodedSample;
6833 if (pFlac ==
NULL || samplesToRead == 0) {
6837 if (bufferOut ==
NULL) {
6838 return drflac__seek_forward_by_samples(pFlac, samplesToRead);
6842 while (samplesToRead > 0) {
6845 if (!drflac__read_and_decode_next_flac_frame(pFlac)) {
6853 drflac_uint64 misalignedSampleCount = samplesReadFromFrameSoFar % channelCount;
6856 unsigned int unusedBitsPerSample;
6859 if (misalignedSampleCount > 0) {
6860 drflac_uint64 misalignedSamplesRead = drflac__read_s32__misaligned(pFlac, misalignedSampleCount, bufferOut);
6861 samplesRead += misalignedSamplesRead;
6862 samplesReadFromFrameSoFar += misalignedSamplesRead;
6863 bufferOut += misalignedSamplesRead;
6864 samplesToRead -= misalignedSamplesRead;
6869 alignedSampleCountPerChannel = samplesToRead / channelCount;
6874 firstAlignedSampleInFrame = samplesReadFromFrameSoFar / channelCount;
6879 case DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE:
6885 for (
i = 0;
i < alignedSampleCountPerChannel; ++
i) {
6888 int right = left - side;
6890 bufferOut[
i*2+0] = left;
6891 bufferOut[
i*2+1] =
right;
6895 case DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE:
6901 for (
i = 0;
i < alignedSampleCountPerChannel; ++
i) {
6904 int left =
right + side;
6906 bufferOut[
i*2+0] = left;
6907 bufferOut[
i*2+1] =
right;
6911 case DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE:
6917 for (
i = 0;
i < alignedSampleCountPerChannel; ++
i) {
6928 case DRFLAC_CHANNEL_ASSIGNMENT_INDEPENDENT:
6938 for (
i = 0;
i < alignedSampleCountPerChannel; ++
i) {
6947 for (
i = 0;
i < alignedSampleCountPerChannel; ++
i) {
6949 for (
j = 0;
j < channelCount; ++
j) {
6957 alignedSamplesRead = alignedSampleCountPerChannel * channelCount;
6958 samplesRead += alignedSamplesRead;
6959 samplesReadFromFrameSoFar += alignedSamplesRead;
6960 bufferOut += alignedSamplesRead;
6961 samplesToRead -= alignedSamplesRead;
6969 if (samplesToRead < pFlac->currentFrame.samplesRemaining) {
6970 excessSamplesRead = drflac__read_s32__misaligned(pFlac, samplesToRead, bufferOut);
6975 samplesRead += excessSamplesRead;
6976 samplesReadFromFrameSoFar += excessSamplesRead;
6977 bufferOut += excessSamplesRead;
6978 samplesToRead -= excessSamplesRead;
6989 #if defined(_MSC_VER) && !defined(__clang__)
6990 #pragma warning(push)
6991 #pragma warning(disable:4996)
6992 #elif defined(__GNUC__) || defined(__clang__)
6993 #pragma GCC diagnostic push
6994 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
6997 #if defined(_MSC_VER) && !defined(__clang__)
6998 #pragma warning(pop)
6999 #elif defined(__GNUC__) || defined(__clang__)
7000 #pragma GCC diagnostic pop
7010 #if defined(_MSC_VER) && !defined(__clang__)
7011 #pragma warning(push)
7012 #pragma warning(disable:4996)
7013 #elif defined(__GNUC__) || defined(__clang__)
7014 #pragma GCC diagnostic push
7015 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
7018 while (samplesToRead > 0) {
7022 if (samplesJustRead == 0) {
7027 for (
i = 0;
i < samplesJustRead; ++
i) {
7031 totalSamplesRead += samplesJustRead;
7032 samplesToRead -= samplesJustRead;
7033 pBufferOut += samplesJustRead;
7036 #if defined(_MSC_VER) && !defined(__clang__)
7037 #pragma warning(pop)
7038 #elif defined(__GNUC__) || defined(__clang__)
7039 #pragma GCC diagnostic pop
7042 return totalSamplesRead;
7050 while (framesToRead > 0) {
7054 if (framesJustRead == 0) {
7059 for (iFrame = 0; iFrame < framesJustRead; ++iFrame) {
7061 for (iChannel = 0; iChannel < pFlac->
channels; ++iChannel) {
7063 pBufferOut[iSample] = (
drflac_int16)(samples32[iSample] >> 16);
7067 totalPCMFramesRead += framesJustRead;
7068 framesToRead -= framesJustRead;
7069 pBufferOut += framesJustRead * pFlac->
channels;
7072 return totalPCMFramesRead;
7081 #if defined(_MSC_VER) && !defined(__clang__)
7082 #pragma warning(push)
7083 #pragma warning(disable:4996)
7084 #elif defined(__GNUC__) || defined(__clang__)
7085 #pragma GCC diagnostic push
7086 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
7089 while (samplesToRead > 0) {
7093 if (samplesJustRead == 0) {
7098 for (
i = 0;
i < samplesJustRead; ++
i) {
7099 pBufferOut[
i] = (float)(samples32[
i] / 2147483648.0);
7102 totalSamplesRead += samplesJustRead;
7103 samplesToRead -= samplesJustRead;
7104 pBufferOut += samplesJustRead;
7107 #if defined(_MSC_VER) && !defined(__clang__)
7108 #pragma warning(pop)
7109 #elif defined(__GNUC__) || defined(__clang__)
7110 #pragma GCC diagnostic pop
7113 return totalSamplesRead;
7120 for (
i = 0;
i < frameCount; ++
i) {
7123 int right = left - side;
7125 pOutputSamples[
i*2+0] = (float)(left / 2147483648.0);
7126 pOutputSamples[
i*2+1] = (float)(
right / 2147483648.0);
7136 float factor = 1 / 2147483648.0;
7140 for (
i = 0;
i < frameCount4; ++
i) {
7156 pOutputSamples[
i*8+0] = left0 * factor;
7157 pOutputSamples[
i*8+1] = right0 * factor;
7158 pOutputSamples[
i*8+2] = left1 * factor;
7159 pOutputSamples[
i*8+3] = right1 * factor;
7160 pOutputSamples[
i*8+4] = left2 * factor;
7161 pOutputSamples[
i*8+5] = right2 * factor;
7162 pOutputSamples[
i*8+6] = left3 * factor;
7163 pOutputSamples[
i*8+7] = right3 * factor;
7166 for (
i = (frameCount4 << 2);
i < frameCount; ++
i) {
7167 int left = pInputSamples0[
i] << shift0;
7168 int side = pInputSamples1[
i] << shift1;
7169 int right = left - side;
7171 pOutputSamples[
i*2+0] = (float)(left * factor);
7172 pOutputSamples[
i*2+1] = (float)(
right * factor);
7176 #if defined(DRFLAC_SUPPORT_SSE2)
7187 frameCount4 = frameCount >> 2;
7189 factor = _mm_set1_ps(1.0f / 8388608.0f);
7193 for (
i = 0;
i < frameCount4; ++
i) {
7194 __m128i inputSample0 = _mm_loadu_si128((
const __m128i*)pInputSamples0 +
i);
7195 __m128i inputSample1 = _mm_loadu_si128((
const __m128i*)pInputSamples1 +
i);
7197 __m128i left = _mm_slli_epi32(inputSample0, shift0);
7198 __m128i side = _mm_slli_epi32(inputSample1, shift1);
7199 __m128i
right = _mm_sub_epi32(left, side);
7200 __m128 leftf = _mm_mul_ps(_mm_cvtepi32_ps(left), factor);
7201 __m128 rightf = _mm_mul_ps(_mm_cvtepi32_ps(
right), factor);
7203 pOutputSamples[
i*8+0] = ((
float*)&leftf)[0];
7204 pOutputSamples[
i*8+1] = ((
float*)&rightf)[0];
7205 pOutputSamples[
i*8+2] = ((
float*)&leftf)[1];
7206 pOutputSamples[
i*8+3] = ((
float*)&rightf)[1];
7207 pOutputSamples[
i*8+4] = ((
float*)&leftf)[2];
7208 pOutputSamples[
i*8+5] = ((
float*)&rightf)[2];
7209 pOutputSamples[
i*8+6] = ((
float*)&leftf)[3];
7210 pOutputSamples[
i*8+7] = ((
float*)&rightf)[3];
7213 for (
i = (frameCount4 << 2);
i < frameCount; ++
i) {
7214 int left = pInputSamples0[
i] << shift0;
7215 int side = pInputSamples1[
i] << shift1;
7216 int right = left - side;
7218 pOutputSamples[
i*2+0] = (float)(left / 8388608.0f);
7219 pOutputSamples[
i*2+1] = (float)(
right / 8388608.0f);
7226 #if defined(DRFLAC_SUPPORT_SSE2)
7227 if (drflac__gIsSSE2Supported && pFlac->
bitsPerSample <= 24) {
7228 drflac_read_pcm_frames_f32__decode_left_side__sse2(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7234 drflac_read_pcm_frames_f32__decode_left_side__reference(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7236 drflac_read_pcm_frames_f32__decode_left_side__scalar(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7246 for (
i = 0;
i < frameCount; ++
i) {
7249 int left =
right + side;
7251 pOutputSamples[
i*2+0] = (float)(left / 2147483648.0);
7252 pOutputSamples[
i*2+1] = (float)(
right / 2147483648.0);
7262 float factor = 1 / 2147483648.0;
7266 for (
i = 0;
i < frameCount4; ++
i) {
7282 pOutputSamples[
i*8+0] = left0 * factor;
7283 pOutputSamples[
i*8+1] = right0 * factor;
7284 pOutputSamples[
i*8+2] = left1 * factor;
7285 pOutputSamples[
i*8+3] = right1 * factor;
7286 pOutputSamples[
i*8+4] = left2 * factor;
7287 pOutputSamples[
i*8+5] = right2 * factor;
7288 pOutputSamples[
i*8+6] = left3 * factor;
7289 pOutputSamples[
i*8+7] = right3 * factor;
7292 for (
i = (frameCount4 << 2);
i < frameCount; ++
i) {
7293 int side = pInputSamples0[
i] << shift0;
7294 int right = pInputSamples1[
i] << shift1;
7295 int left =
right + side;
7297 pOutputSamples[
i*2+0] = (float)(left * factor);
7298 pOutputSamples[
i*2+1] = (float)(
right * factor);
7302 #if defined(DRFLAC_SUPPORT_SSE2)
7313 frameCount4 = frameCount >> 2;
7315 factor = _mm_set1_ps(1.0f / 8388608.0f);
7319 for (
i = 0;
i < frameCount4; ++
i) {
7320 __m128i inputSample0 = _mm_loadu_si128((
const __m128i*)pInputSamples0 +
i);
7321 __m128i inputSample1 = _mm_loadu_si128((
const __m128i*)pInputSamples1 +
i);
7323 __m128i side = _mm_slli_epi32(inputSample0, shift0);
7324 __m128i
right = _mm_slli_epi32(inputSample1, shift1);
7325 __m128i left = _mm_add_epi32(
right, side);
7326 __m128 leftf = _mm_mul_ps(_mm_cvtepi32_ps(left), factor);
7327 __m128 rightf = _mm_mul_ps(_mm_cvtepi32_ps(
right), factor);
7329 pOutputSamples[
i*8+0] = ((
float*)&leftf)[0];
7330 pOutputSamples[
i*8+1] = ((
float*)&rightf)[0];
7331 pOutputSamples[
i*8+2] = ((
float*)&leftf)[1];
7332 pOutputSamples[
i*8+3] = ((
float*)&rightf)[1];
7333 pOutputSamples[
i*8+4] = ((
float*)&leftf)[2];
7334 pOutputSamples[
i*8+5] = ((
float*)&rightf)[2];
7335 pOutputSamples[
i*8+6] = ((
float*)&leftf)[3];
7336 pOutputSamples[
i*8+7] = ((
float*)&rightf)[3];
7339 for (
i = (frameCount4 << 2);
i < frameCount; ++
i) {
7340 int side = pInputSamples0[
i] << shift0;
7341 int right = pInputSamples1[
i] << shift1;
7342 int left =
right + side;
7344 pOutputSamples[
i*2+0] = (float)(left / 8388608.0f);
7345 pOutputSamples[
i*2+1] = (float)(
right / 8388608.0f);
7352 #if defined(DRFLAC_SUPPORT_SSE2)
7353 if (drflac__gIsSSE2Supported && pFlac->
bitsPerSample <= 24) {
7354 drflac_read_pcm_frames_f32__decode_right_side__sse2(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7360 drflac_read_pcm_frames_f32__decode_right_side__reference(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7362 drflac_read_pcm_frames_f32__decode_right_side__scalar(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7377 pOutputSamples[
i*2+0] = (float)((((mid + side) >> 1) << (unusedBitsPerSample)) / 2147483648.0);
7378 pOutputSamples[
i*2+1] = (float)((((mid - side) >> 1) << (unusedBitsPerSample)) / 2147483648.0);
7388 float factor = 1 / 2147483648.0;
7390 int shift = unusedBitsPerSample;
7393 for (
i = 0;
i < frameCount4; ++
i) {
7418 temp0L = ((mid0 + side0) << shift);
7419 temp1L = ((mid1 + side1) << shift);
7420 temp2L = ((mid2 + side2) << shift);
7421 temp3L = ((mid3 + side3) << shift);
7423 temp0R = ((mid0 - side0) << shift);
7424 temp1R = ((mid1 - side1) << shift);
7425 temp2R = ((mid2 - side2) << shift);
7426 temp3R = ((mid3 - side3) << shift);
7428 pOutputSamples[
i*8+0] = (float)(temp0L * factor);
7429 pOutputSamples[
i*8+1] = (float)(temp0R * factor);
7430 pOutputSamples[
i*8+2] = (float)(temp1L * factor);
7431 pOutputSamples[
i*8+3] = (float)(temp1R * factor);
7432 pOutputSamples[
i*8+4] = (float)(temp2L * factor);
7433 pOutputSamples[
i*8+5] = (float)(temp2R * factor);
7434 pOutputSamples[
i*8+6] = (float)(temp3L * factor);
7435 pOutputSamples[
i*8+7] = (float)(temp3R * factor);
7438 for (
i = 0;
i < frameCount4; ++
i) {
7463 temp0L = ((mid0 + side0) >> 1);
7464 temp1L = ((mid1 + side1) >> 1);
7465 temp2L = ((mid2 + side2) >> 1);
7466 temp3L = ((mid3 + side3) >> 1);
7468 temp0R = ((mid0 - side0) >> 1);
7469 temp1R = ((mid1 - side1) >> 1);
7470 temp2R = ((mid2 - side2) >> 1);
7471 temp3R = ((mid3 - side3) >> 1);
7473 pOutputSamples[
i*8+0] = (float)(temp0L * factor);
7474 pOutputSamples[
i*8+1] = (float)(temp0R * factor);
7475 pOutputSamples[
i*8+2] = (float)(temp1L * factor);
7476 pOutputSamples[
i*8+3] = (float)(temp1R * factor);
7477 pOutputSamples[
i*8+4] = (float)(temp2L * factor);
7478 pOutputSamples[
i*8+5] = (float)(temp2R * factor);
7479 pOutputSamples[
i*8+6] = (float)(temp3L * factor);
7480 pOutputSamples[
i*8+7] = (float)(temp3R * factor);
7484 for (
i = (frameCount4 << 2);
i < frameCount; ++
i) {
7490 pOutputSamples[
i*2+0] = (float)((((mid + side) >> 1) << unusedBitsPerSample) * factor);
7491 pOutputSamples[
i*2+1] = (float)((((mid - side) >> 1) << unusedBitsPerSample) * factor);
7495 #if defined(DRFLAC_SUPPORT_SSE2)
7506 frameCount4 = frameCount >> 2;
7508 factor = 1.0f / 8388608.0f;
7509 factor128 = _mm_set1_ps(1.0f / 8388608.0f);
7511 shift = unusedBitsPerSample - 8;
7513 for (
i = 0;
i < frameCount4; ++
i) {
7519 __m128i inputSample0 = _mm_loadu_si128((
const __m128i*)pInputSamples0 +
i);
7520 __m128i inputSample1 = _mm_loadu_si128((
const __m128i*)pInputSamples1 +
i);
7525 mid = _mm_or_si128(_mm_slli_epi32(mid, 1), _mm_and_si128(side, _mm_set1_epi32(0x01)));
7527 tempL = _mm_add_epi32(mid, side);
7528 tempR = _mm_sub_epi32(mid, side);
7531 tempL = _mm_or_si128(_mm_srli_epi32(tempL, 1), _mm_and_si128(tempL, _mm_set1_epi32(0x80000000)));
7532 tempR = _mm_or_si128(_mm_srli_epi32(tempR, 1), _mm_and_si128(tempR, _mm_set1_epi32(0x80000000)));
7534 leftf = _mm_mul_ps(_mm_cvtepi32_ps(tempL), factor128);
7535 rightf = _mm_mul_ps(_mm_cvtepi32_ps(tempR), factor128);
7537 pOutputSamples[
i*8+0] = ((
float*)&leftf)[0];
7538 pOutputSamples[
i*8+1] = ((
float*)&rightf)[0];
7539 pOutputSamples[
i*8+2] = ((
float*)&leftf)[1];
7540 pOutputSamples[
i*8+3] = ((
float*)&rightf)[1];
7541 pOutputSamples[
i*8+4] = ((
float*)&leftf)[2];
7542 pOutputSamples[
i*8+5] = ((
float*)&rightf)[2];
7543 pOutputSamples[
i*8+6] = ((
float*)&leftf)[3];
7544 pOutputSamples[
i*8+7] = ((
float*)&rightf)[3];
7547 for (
i = (frameCount4 << 2);
i < frameCount; ++
i) {
7553 pOutputSamples[
i*2+0] = (float)(((mid + side) >> 1) * factor);
7554 pOutputSamples[
i*2+1] = (float)(((mid - side) >> 1) * factor);
7557 for (
i = 0;
i < frameCount4; ++
i) {
7558 __m128i inputSample0;
7559 __m128i inputSample1;
7567 inputSample0 = _mm_loadu_si128((
const __m128i*)pInputSamples0 +
i);
7568 inputSample1 = _mm_loadu_si128((
const __m128i*)pInputSamples1 +
i);
7573 mid = _mm_or_si128(_mm_slli_epi32(mid, 1), _mm_and_si128(side, _mm_set1_epi32(0x01)));
7575 tempL = _mm_slli_epi32(_mm_srli_epi32(_mm_add_epi32(mid, side), 1), shift);
7576 tempR = _mm_slli_epi32(_mm_srli_epi32(_mm_sub_epi32(mid, side), 1), shift);
7578 leftf = _mm_mul_ps(_mm_cvtepi32_ps(tempL), factor128);
7579 rightf = _mm_mul_ps(_mm_cvtepi32_ps(tempR), factor128);
7581 pOutputSamples[
i*8+0] = ((
float*)&leftf)[0];
7582 pOutputSamples[
i*8+1] = ((
float*)&rightf)[0];
7583 pOutputSamples[
i*8+2] = ((
float*)&leftf)[1];
7584 pOutputSamples[
i*8+3] = ((
float*)&rightf)[1];
7585 pOutputSamples[
i*8+4] = ((
float*)&leftf)[2];
7586 pOutputSamples[
i*8+5] = ((
float*)&rightf)[2];
7587 pOutputSamples[
i*8+6] = ((
float*)&leftf)[3];
7588 pOutputSamples[
i*8+7] = ((
float*)&rightf)[3];
7591 for (
i = (frameCount4 << 2);
i < frameCount; ++
i) {
7597 pOutputSamples[
i*2+0] = (float)((((mid + side) >> 1) << shift) * factor);
7598 pOutputSamples[
i*2+1] = (float)((((mid - side) >> 1) << shift) * factor);
7607 #if defined(DRFLAC_SUPPORT_SSE2)
7608 if (drflac__gIsSSE2Supported && pFlac->
bitsPerSample <= 24) {
7609 drflac_read_pcm_frames_f32__decode_mid_side__sse2(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7615 drflac_read_pcm_frames_f32__decode_mid_side__reference(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7617 drflac_read_pcm_frames_f32__decode_mid_side__scalar(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7637 float factor = 1 / 2147483648.0;
7642 for (
i = 0;
i < frameCount4; ++
i) {
7643 int tempL0 = pInputSamples0[
i*4+0] << shift0;
7644 int tempL1 = pInputSamples0[
i*4+1] << shift0;
7645 int tempL2 = pInputSamples0[
i*4+2] << shift0;
7646 int tempL3 = pInputSamples0[
i*4+3] << shift0;
7648 int tempR0 = pInputSamples1[
i*4+0] << shift1;
7649 int tempR1 = pInputSamples1[
i*4+1] << shift1;
7650 int tempR2 = pInputSamples1[
i*4+2] << shift1;
7651 int tempR3 = pInputSamples1[
i*4+3] << shift1;
7653 pOutputSamples[
i*8+0] = (float)(tempL0 * factor);
7654 pOutputSamples[
i*8+1] = (float)(tempR0 * factor);
7655 pOutputSamples[
i*8+2] = (float)(tempL1 * factor);
7656 pOutputSamples[
i*8+3] = (float)(tempR1 * factor);
7657 pOutputSamples[
i*8+4] = (float)(tempL2 * factor);
7658 pOutputSamples[
i*8+5] = (float)(tempR2 * factor);
7659 pOutputSamples[
i*8+6] = (float)(tempL3 * factor);
7660 pOutputSamples[
i*8+7] = (float)(tempR3 * factor);
7663 for (
i = (frameCount4 << 2);
i < frameCount; ++
i) {
7664 pOutputSamples[
i*2+0] = (float)((pInputSamples0[
i] << shift0) * factor);
7665 pOutputSamples[
i*2+1] = (float)((pInputSamples1[
i] << shift1) * factor);
7669 #if defined(DRFLAC_SUPPORT_SSE2)
7675 float factor = 1.0f / 8388608.0f;
7676 __m128 factor128 = _mm_set1_ps(1.0f / 8388608.0f);
7681 for (
i = 0;
i < frameCount4; ++
i) {
7682 __m128i inputSample0 = _mm_loadu_si128((
const __m128i*)pInputSamples0 +
i);
7683 __m128i inputSample1 = _mm_loadu_si128((
const __m128i*)pInputSamples1 +
i);
7685 __m128i i32L = _mm_slli_epi32(inputSample0, shift0);
7686 __m128i i32R = _mm_slli_epi32(inputSample1, shift1);
7688 __m128 f32L = _mm_mul_ps(_mm_cvtepi32_ps(i32L), factor128);
7689 __m128 f32R = _mm_mul_ps(_mm_cvtepi32_ps(i32R), factor128);
7691 pOutputSamples[
i*8+0] = ((
float*)&f32L)[0];
7692 pOutputSamples[
i*8+1] = ((
float*)&f32R)[0];
7693 pOutputSamples[
i*8+2] = ((
float*)&f32L)[1];
7694 pOutputSamples[
i*8+3] = ((
float*)&f32R)[1];
7695 pOutputSamples[
i*8+4] = ((
float*)&f32L)[2];
7696 pOutputSamples[
i*8+5] = ((
float*)&f32R)[2];
7697 pOutputSamples[
i*8+6] = ((
float*)&f32L)[3];
7698 pOutputSamples[
i*8+7] = ((
float*)&f32R)[3];
7701 for (
i = (frameCount4 << 2);
i < frameCount; ++
i) {
7702 pOutputSamples[
i*2+0] = (float)((pInputSamples0[
i] << shift0) * factor);
7703 pOutputSamples[
i*2+1] = (float)((pInputSamples1[
i] << shift1) * factor);
7710 #if defined(DRFLAC_SUPPORT_SSE2)
7711 if (drflac__gIsSSE2Supported && pFlac->
bitsPerSample <= 24) {
7712 drflac_read_pcm_frames_f32__decode_independent_stereo__sse2(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7718 drflac_read_pcm_frames_f32__decode_independent_stereo__reference(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7720 drflac_read_pcm_frames_f32__decode_independent_stereo__scalar(pFlac, frameCount, unusedBitsPerSample, pInputSamples0, pInputSamples1, pOutputSamples);
7729 if (pFlac ==
NULL || framesToRead == 0) {
7733 if (pBufferOut ==
NULL) {
7734 return drflac__seek_forward_by_pcm_frames(pFlac, framesToRead);
7738 while (framesToRead > 0) {
7741 if (!drflac__read_and_decode_next_flac_frame(pFlac)) {
7757 if (channelCount == 2) {
7763 case DRFLAC_CHANNEL_ASSIGNMENT_LEFT_SIDE:
7765 drflac_read_pcm_frames_f32__decode_left_side(pFlac, frameCountThisIteration, unusedBitsPerSample, pDecodedSamples0, pDecodedSamples1, pBufferOut);
7768 case DRFLAC_CHANNEL_ASSIGNMENT_RIGHT_SIDE:
7770 drflac_read_pcm_frames_f32__decode_right_side(pFlac, frameCountThisIteration, unusedBitsPerSample, pDecodedSamples0, pDecodedSamples1, pBufferOut);
7773 case DRFLAC_CHANNEL_ASSIGNMENT_MID_SIDE:
7775 drflac_read_pcm_frames_f32__decode_mid_side(pFlac, frameCountThisIteration, unusedBitsPerSample, pDecodedSamples0, pDecodedSamples1, pBufferOut);
7778 case DRFLAC_CHANNEL_ASSIGNMENT_INDEPENDENT:
7781 drflac_read_pcm_frames_f32__decode_independent_stereo(pFlac, frameCountThisIteration, unusedBitsPerSample, pDecodedSamples0, pDecodedSamples1, pBufferOut);
7787 for (
i = 0;
i < frameCountThisIteration; ++
i) {
7789 for (
j = 0;
j < channelCount; ++
j) {
7795 samplesReadThisIteration = frameCountThisIteration * channelCount;
7796 framesRead += frameCountThisIteration;
7797 framesReadFromPacketSoFar += frameCountThisIteration;
7798 pBufferOut += samplesReadThisIteration;
7799 framesToRead -= frameCountThisIteration;
7810 if (pFlac ==
NULL) {
7822 if (sampleIndex == 0) {
7824 return drflac__seek_to_first_frame(pFlac);
7847 if (currentFrameSamplesConsumed > offsetAbs) {
7858 #ifndef DR_FLAC_NO_OGG
7861 wasSuccessful = drflac_ogg__seek_to_sample(pFlac, sampleIndex);
7867 wasSuccessful = drflac__seek_to_sample__seek_table(pFlac, sampleIndex);
7868 if (!wasSuccessful) {
7869 wasSuccessful = drflac__seek_to_sample__brute_force(pFlac, sampleIndex);
7874 return wasSuccessful;
7880 if (pFlac ==
NULL) {
7892 if (pcmFrameIndex == 0) {
7894 return drflac__seek_to_first_frame(pFlac);
7917 if (currentFrameSamplesConsumed > offsetAbs) {
7928 #ifndef DR_FLAC_NO_OGG
7931 wasSuccessful = drflac_ogg__seek_to_sample(pFlac, pcmFrameIndex*pFlac->
channels);
7937 wasSuccessful = drflac__seek_to_sample__seek_table(pFlac, pcmFrameIndex*pFlac->
channels);
7938 if (!wasSuccessful) {
7939 wasSuccessful = drflac__seek_to_sample__brute_force(pFlac, pcmFrameIndex*pFlac->
channels);
7944 return wasSuccessful;
7952 #if defined(SIZE_MAX)
7953 #define DRFLAC_SIZE_MAX SIZE_MAX
7955 #if defined(DRFLAC_64BIT)
7956 #define DRFLAC_SIZE_MAX ((drflac_uint64)0xFFFFFFFFFFFFFFFF)
7958 #define DRFLAC_SIZE_MAX 0xFFFFFFFF
7964 #define DRFLAC_DEFINE_FULL_READ_AND_CLOSE(extension, type) \
7965 static type* drflac__full_read_and_close_ ## extension (drflac* pFlac, unsigned int* channelsOut, unsigned int* sampleRateOut, drflac_uint64* totalPCMFrameCountOut)\
7967 type* pSampleData = NULL; \
7968 drflac_uint64 totalPCMFrameCount; \
7970 drflac_assert(pFlac != NULL); \
7972 totalPCMFrameCount = pFlac->totalPCMFrameCount; \
7974 if (totalPCMFrameCount == 0) { \
7975 type buffer[4096]; \
7976 drflac_uint64 pcmFramesRead; \
7977 size_t sampleDataBufferSize = sizeof(buffer); \
7979 pSampleData = (type*)DRFLAC_MALLOC(sampleDataBufferSize); \
7980 if (pSampleData == NULL) { \
7984 while ((pcmFramesRead = (drflac_uint64)drflac_read_pcm_frames_##extension(pFlac, sizeof(buffer)/sizeof(buffer[0])/pFlac->channels, buffer)) > 0) { \
7985 if (((totalPCMFrameCount + pcmFramesRead) * pFlac->channels * sizeof(type)) > sampleDataBufferSize) { \
7986 type* pNewSampleData; \
7988 sampleDataBufferSize *= 2; \
7989 pNewSampleData = (type*)DRFLAC_REALLOC(pSampleData, sampleDataBufferSize); \
7990 if (pNewSampleData == NULL) { \
7991 DRFLAC_FREE(pSampleData); \
7995 pSampleData = pNewSampleData; \
7998 drflac_copy_memory(pSampleData + (totalPCMFrameCount*pFlac->channels), buffer, (size_t)(pcmFramesRead*pFlac->channels*sizeof(type))); \
7999 totalPCMFrameCount += pcmFramesRead; \
8004 drflac_zero_memory(pSampleData + (totalPCMFrameCount*pFlac->channels), (size_t)(sampleDataBufferSize - totalPCMFrameCount*pFlac->channels*sizeof(type))); \
8006 drflac_uint64 dataSize = totalPCMFrameCount*pFlac->channels*sizeof(type); \
8007 if (dataSize > DRFLAC_SIZE_MAX) { \
8011 pSampleData = (type*)DRFLAC_MALLOC((size_t)dataSize); \
8012 if (pSampleData == NULL) { \
8016 totalPCMFrameCount = drflac_read_pcm_frames_##extension(pFlac, pFlac->totalPCMFrameCount, pSampleData); \
8019 if (sampleRateOut) *sampleRateOut = pFlac->sampleRate; \
8020 if (channelsOut) *channelsOut = pFlac->channels; \
8021 if (totalPCMFrameCountOut) *totalPCMFrameCountOut = totalPCMFrameCount; \
8023 drflac_close(pFlac); \
8024 return pSampleData; \
8027 drflac_close(pFlac); \
8033 DRFLAC_DEFINE_FULL_READ_AND_CLOSE(f32,
float)
8042 if (sampleRateOut) {
8045 if (totalPCMFrameCountOut) {
8046 *totalPCMFrameCountOut = 0;
8050 if (pFlac ==
NULL) {
8054 return drflac__full_read_and_close_s32(pFlac, channelsOut, sampleRateOut, totalPCMFrameCountOut);
8060 unsigned int sampleRate;
8067 if (sampleRateOut) {
8070 if (totalSampleCountOut) {
8071 *totalSampleCountOut = 0;
8075 if (pResult ==
NULL) {
8082 if (sampleRateOut) {
8083 *sampleRateOut = sampleRate;
8085 if (totalSampleCountOut) {
8086 *totalSampleCountOut = totalPCMFrameCount *
channels;
8101 if (sampleRateOut) {
8104 if (totalPCMFrameCountOut) {
8105 *totalPCMFrameCountOut = 0;
8109 if (pFlac ==
NULL) {
8113 return drflac__full_read_and_close_s16(pFlac, channelsOut, sampleRateOut, totalPCMFrameCountOut);
8119 unsigned int sampleRate;
8126 if (sampleRateOut) {
8129 if (totalSampleCountOut) {
8130 *totalSampleCountOut = 0;
8134 if (pResult ==
NULL) {
8141 if (sampleRateOut) {
8142 *sampleRateOut = sampleRate;
8144 if (totalSampleCountOut) {
8145 *totalSampleCountOut = totalPCMFrameCount *
channels;
8159 if (sampleRateOut) {
8162 if (totalPCMFrameCountOut) {
8163 *totalPCMFrameCountOut = 0;
8167 if (pFlac ==
NULL) {
8171 return drflac__full_read_and_close_f32(pFlac, channelsOut, sampleRateOut, totalPCMFrameCountOut);
8177 unsigned int sampleRate;
8184 if (sampleRateOut) {
8187 if (totalSampleCountOut) {
8188 *totalSampleCountOut = 0;
8192 if (pResult ==
NULL) {
8199 if (sampleRateOut) {
8200 *sampleRateOut = sampleRate;
8202 if (totalSampleCountOut) {
8203 *totalSampleCountOut = totalPCMFrameCount *
channels;
8209 #ifndef DR_FLAC_NO_STDIO
8220 if (totalPCMFrameCount) {
8221 *totalPCMFrameCount = 0;
8225 if (pFlac ==
NULL) {
8229 return drflac__full_read_and_close_s32(pFlac,
channels, sampleRate, totalPCMFrameCount);
8235 unsigned int sampleRate;
8242 if (sampleRateOut) {
8245 if (totalSampleCountOut) {
8246 *totalSampleCountOut = 0;
8250 if (pResult ==
NULL) {
8257 if (sampleRateOut) {
8258 *sampleRateOut = sampleRate;
8260 if (totalSampleCountOut) {
8261 *totalSampleCountOut = totalPCMFrameCount *
channels;
8278 if (totalPCMFrameCount) {
8279 *totalPCMFrameCount = 0;
8283 if (pFlac ==
NULL) {
8287 return drflac__full_read_and_close_s16(pFlac,
channels, sampleRate, totalPCMFrameCount);
8293 unsigned int sampleRate;
8300 if (sampleRateOut) {
8303 if (totalSampleCountOut) {
8304 *totalSampleCountOut = 0;
8308 if (pResult ==
NULL) {
8315 if (sampleRateOut) {
8316 *sampleRateOut = sampleRate;
8318 if (totalSampleCountOut) {
8319 *totalSampleCountOut = totalPCMFrameCount *
channels;
8336 if (totalPCMFrameCount) {
8337 *totalPCMFrameCount = 0;
8341 if (pFlac ==
NULL) {
8345 return drflac__full_read_and_close_f32(pFlac,
channels, sampleRate, totalPCMFrameCount);
8351 unsigned int sampleRate;
8358 if (sampleRateOut) {
8361 if (totalSampleCountOut) {
8362 *totalSampleCountOut = 0;
8366 if (pResult ==
NULL) {
8373 if (sampleRateOut) {
8374 *sampleRateOut = sampleRate;
8376 if (totalSampleCountOut) {
8377 *totalSampleCountOut = totalPCMFrameCount *
channels;
8394 if (totalPCMFrameCount) {
8395 *totalPCMFrameCount = 0;
8399 if (pFlac ==
NULL) {
8403 return drflac__full_read_and_close_s32(pFlac,
channels, sampleRate, totalPCMFrameCount);
8409 unsigned int sampleRate;
8416 if (sampleRateOut) {
8419 if (totalSampleCountOut) {
8420 *totalSampleCountOut = 0;
8424 if (pResult ==
NULL) {
8431 if (sampleRateOut) {
8432 *sampleRateOut = sampleRate;
8434 if (totalSampleCountOut) {
8435 *totalSampleCountOut = totalPCMFrameCount *
channels;
8452 if (totalPCMFrameCount) {
8453 *totalPCMFrameCount = 0;
8457 if (pFlac ==
NULL) {
8461 return drflac__full_read_and_close_s16(pFlac,
channels, sampleRate, totalPCMFrameCount);
8467 unsigned int sampleRate;
8474 if (sampleRateOut) {
8477 if (totalSampleCountOut) {
8478 *totalSampleCountOut = 0;
8482 if (pResult ==
NULL) {
8489 if (sampleRateOut) {
8490 *sampleRateOut = sampleRate;
8492 if (totalSampleCountOut) {
8493 *totalSampleCountOut = totalPCMFrameCount *
channels;
8510 if (totalPCMFrameCount) {
8511 *totalPCMFrameCount = 0;
8515 if (pFlac ==
NULL) {
8519 return drflac__full_read_and_close_f32(pFlac,
channels, sampleRate, totalPCMFrameCount);
8525 unsigned int sampleRate;
8532 if (sampleRateOut) {
8535 if (totalSampleCountOut) {
8536 *totalSampleCountOut = 0;
8540 if (pResult ==
NULL) {
8547 if (sampleRateOut) {
8548 *sampleRateOut = sampleRate;
8550 if (totalSampleCountOut) {
8551 *totalSampleCountOut = totalPCMFrameCount *
channels;
8558 void drflac_free(
void* pSampleDataReturnedByOpenAndDecode)
8560 DRFLAC_FREE(pSampleDataReturnedByOpenAndDecode);
8568 if (pIter ==
NULL) {
8579 const char* pComment;
8582 if (pCommentLengthOut) {
8583 *pCommentLengthOut = 0;
8597 if (pCommentLengthOut) {
8598 *pCommentLengthOut = length;
8609 if (pIter ==
NULL) {
8620 const char* pRunningData;
8630 offsetHi = drflac__be2host_32(*(
const drflac_uint32*)pRunningData); pRunningData += 4;
8631 offsetLo = drflac__be2host_32(*(
const drflac_uint32*)pRunningData); pRunningData += 4;
8632 cuesheetTrack.
offset = offsetLo | (offsetHi << 32);
8633 cuesheetTrack.
trackNumber = pRunningData[0]; pRunningData += 1;
8634 drflac_copy_memory(cuesheetTrack.
ISRC, pRunningData,
sizeof(cuesheetTrack.
ISRC)); pRunningData += 12;
8635 cuesheetTrack.
isAudio = (pRunningData[0] & 0x80) != 0;
8636 cuesheetTrack.
preEmphasis = (pRunningData[0] & 0x40) != 0; pRunningData += 14;
8637 cuesheetTrack.
indexCount = pRunningData[0]; pRunningData += 1;
8643 if (pCuesheetTrack) {
8644 *pCuesheetTrack = cuesheetTrack;
8650 #if defined(__GNUC__)
8651 #pragma GCC diagnostic pop