17 #if defined(__clang__)
18 #pragma GCC diagnostic push
19 #pragma GCC diagnostic ignored "-Wlanguage-extension-token"
20 #pragma GCC diagnostic ignored "-Wlong-long"
21 #pragma GCC diagnostic ignored "-Wc++11-long-long"
31 #if defined(__clang__)
32 #pragma GCC diagnostic pop
41 #elif defined(__GNUC__)
65 #define DROPUS_FALSE 0
76 #define DROPUS_SIZE_MAX SIZE_MAX
78 #define DROPUS_SIZE_MAX 0xFFFFFFFF
82 #define DROPUS_INLINE __forceinline
83 #elif defined(__GNUC__)
91 #if defined(__STRICT_ANSI__)
92 #define DROPUS_INLINE __inline__ __attribute__((always_inline))
94 #define DROPUS_INLINE inline __attribute__((always_inline))
100 #if !defined(DROPUS_API)
101 #if defined(DROPUS_DLL)
103 #define DROPUS_DLL_IMPORT __declspec(dllimport)
104 #define DROPUS_DLL_EXPORT __declspec(dllexport)
105 #define DROPUS_DLL_PRIVATE static
107 #if defined(__GNUC__) && __GNUC__ >= 4
108 #define DROPUS_DLL_IMPORT __attribute__((visibility("default")))
109 #define DROPUS_DLL_EXPORT __attribute__((visibility("default")))
110 #define DROPUS_DLL_PRIVATE __attribute__((visibility("hidden")))
112 #define DROPUS_DLL_IMPORT
113 #define DROPUS_DLL_EXPORT
114 #define DROPUS_DLL_PRIVATE static
118 #if defined(DR_OPUS_IMPLEMENTATION) || defined(DROPUS_IMPLEMENTATION)
119 #define DROPUS_API DROPUS_DLL_EXPORT
121 #define DROPUS_API DROPUS_DLL_IMPORT
123 #define DROPUS_PRIVATE DROPUS_DLL_PRIVATE
125 #define DROPUS_API extern
126 #define DROPUS_PRIVATE static
131 #define DROPUS_SUCCESS 0
132 #define DROPUS_ERROR -1
133 #define DROPUS_INVALID_ARGS -2
134 #define DROPUS_INVALID_OPERATION -3
135 #define DROPUS_OUT_OF_MEMORY -4
136 #define DROPUS_OUT_OF_RANGE -5
137 #define DROPUS_ACCESS_DENIED -6
138 #define DROPUS_DOES_NOT_EXIST -7
139 #define DROPUS_ALREADY_EXISTS -8
140 #define DROPUS_TOO_MANY_OPEN_FILES -9
141 #define DROPUS_INVALID_FILE -10
142 #define DROPUS_TOO_BIG -11
143 #define DROPUS_PATH_TOO_LONG -12
144 #define DROPUS_NAME_TOO_LONG -13
145 #define DROPUS_NOT_DIRECTORY -14
146 #define DROPUS_IS_DIRECTORY -15
147 #define DROPUS_DIRECTORY_NOT_EMPTY -16
148 #define DROPUS_END_OF_FILE -17
149 #define DROPUS_NO_SPACE -18
150 #define DROPUS_BUSY -19
151 #define DROPUS_IO_ERROR -20
152 #define DROPUS_INTERRUPT -21
153 #define DROPUS_UNAVAILABLE -22
154 #define DROPUS_ALREADY_IN_USE -23
155 #define DROPUS_BAD_ADDRESS -24
156 #define DROPUS_BAD_SEEK -25
157 #define DROPUS_BAD_PIPE -26
158 #define DROPUS_DEADLOCK -27
159 #define DROPUS_TOO_MANY_LINKS -28
160 #define DROPUS_NOT_IMPLEMENTED -29
161 #define DROPUS_NO_MESSAGE -30
162 #define DROPUS_BAD_MESSAGE -31
163 #define DROPUS_NO_DATA_AVAILABLE -32
164 #define DROPUS_INVALID_DATA -33
165 #define DROPUS_TIMEOUT -34
166 #define DROPUS_NO_NETWORK -35
167 #define DROPUS_NOT_UNIQUE -36
168 #define DROPUS_NOT_SOCKET -37
169 #define DROPUS_NO_ADDRESS -38
170 #define DROPUS_BAD_PROTOCOL -39
171 #define DROPUS_PROTOCOL_UNAVAILABLE -40
172 #define DROPUS_PROTOCOL_NOT_SUPPORTED -41
173 #define DROPUS_PROTOCOL_FAMILY_NOT_SUPPORTED -42
174 #define DROPUS_ADDRESS_FAMILY_NOT_SUPPORTED -43
175 #define DROPUS_SOCKET_NOT_SUPPORTED -44
176 #define DROPUS_CONNECTION_RESET -45
177 #define DROPUS_ALREADY_CONNECTED -46
178 #define DROPUS_NOT_CONNECTED -47
179 #define DROPUS_CONNECTION_REFUSED -48
180 #define DROPUS_NO_HOST -49
181 #define DROPUS_IN_PROGRESS -50
182 #define DROPUS_CANCELLED -51
183 #define DROPUS_MEMORY_ALREADY_MAPPED -52
184 #define DROPUS_AT_END -53
185 #define DROPUS_CRC_MISMATCH -100
186 #define DROPUS_BAD_DATA -101
196 #define DROPUS_MAX_OPUS_FRAMES_PER_PACKET 48
197 #define DROPUS_MAX_PCM_FRAMES_PER_PACKET 6144
250 typedef size_t (*
dropus_read_proc)(
void* pUserData,
void* pBufferOut,
size_t bytesToRead);
256 void* (* onMalloc)(
size_t sz,
void* pUserData);
257 void* (* onRealloc)(
void* p,
size_t sz,
void* pUserData);
258 void (* onFree)(
void* p,
void* pUserData);
281 #ifndef DR_OPUS_NO_STDIO
393 #if defined(DR_OPUS_IMPLEMENTATION) || defined(DROPUS_IMPLEMENTATION)
396 #ifndef DR_OPUS_NO_STDIO
402 #if defined(__x86_64__) || defined(_M_X64)
404 #elif defined(__i386) || defined(_M_IX86)
406 #elif defined(__arm__) || defined(_M_ARM)
411 #if !defined(DR_OPUS_NO_SIMD) && (defined(DROPUS_X86) || defined(DROPUS_X64))
412 #if defined(_MSC_VER) && !defined(__clang__)
415 static DROPUS_INLINE void dropus__cpuid(
int info[4],
int fid)
420 #define DROPUS_NO_CPUID
423 #if defined(__GNUC__) || defined(__clang__)
424 static DROPUS_INLINE void dropus__cpuid(
int info[4],
int fid)
433 #if defined(DROPUS_X86) && defined(__PIC__)
434 __asm__ __volatile__ (
435 "xchg{l} {%%}ebx, %k1;"
437 "xchg{l} {%%}ebx, %k1;"
438 :
"=a"(info[0]),
"=&r"(info[1]),
"=c"(info[2]),
"=d"(info[3]) :
"a"(fid),
"c"(0)
441 __asm__ __volatile__ (
442 "cpuid" :
"=a"(info[0]),
"=b"(info[1]),
"=c"(info[2]),
"=d"(info[3]) :
"a"(fid),
"c"(0)
447 #define DROPUS_NO_CPUID
451 #define DROPUS_NO_CPUID
455 #if defined(_MSC_VER) && _MSC_VER >= 1500 && (defined(DROPUS_X86) || defined(DROPUS_X64))
456 #define DROPUS_HAS_LZCNT_INTRINSIC
457 #elif (defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)))
458 #define DROPUS_HAS_LZCNT_INTRINSIC
459 #elif defined(__clang__)
460 #if __has_builtin(__builtin_clzll) || __has_builtin(__builtin_clzl)
461 #define DROPUS_HAS_LZCNT_INTRINSIC
465 #if defined(_MSC_VER) && _MSC_VER >= 1400
466 #define DROPUS_HAS_BYTESWAP16_INTRINSIC
467 #define DROPUS_HAS_BYTESWAP32_INTRINSIC
468 #define DROPUS_HAS_BYTESWAP64_INTRINSIC
469 #elif defined(__clang__)
470 #if __has_builtin(__builtin_bswap16)
471 #define DROPUS_HAS_BYTESWAP16_INTRINSIC
473 #if __has_builtin(__builtin_bswap32)
474 #define DROPUS_HAS_BYTESWAP32_INTRINSIC
476 #if __has_builtin(__builtin_bswap64)
477 #define DROPUS_HAS_BYTESWAP64_INTRINSIC
479 #elif defined(__GNUC__)
480 #if ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
481 #define DROPUS_HAS_BYTESWAP32_INTRINSIC
482 #define DROPUS_HAS_BYTESWAP64_INTRINSIC
484 #if ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))
485 #define DROPUS_HAS_BYTESWAP16_INTRINSIC
490 #ifndef DROPUS_ASSERT
492 #define DROPUS_ASSERT(expression) assert(expression)
494 #ifndef DROPUS_MALLOC
495 #define DROPUS_MALLOC(sz) malloc((sz))
497 #ifndef DROPUS_REALLOC
498 #define DROPUS_REALLOC(p, sz) realloc((p), (sz))
501 #define DROPUS_FREE(p) free((p))
503 #ifndef DROPUS_COPY_MEMORY
504 #define DROPUS_COPY_MEMORY(dst, src, sz) memcpy((dst), (src), (sz))
506 #ifndef DROPUS_ZERO_MEMORY
507 #define DROPUS_ZERO_MEMORY(p, sz) memset((p), 0, (sz))
509 #ifndef DROPUS_ZERO_OBJECT
510 #define DROPUS_ZERO_OBJECT(p) DROPUS_ZERO_MEMORY((p), sizeof(*(p)))
514 #define DROPUS_MIN(x, y) (((x) < (y)) ? (x) : (y))
517 #define DROPUS_MAX(x, y) (((x) > (y)) ? (x) : (y))
519 #ifndef DROPUS_COUNTOF
520 #define DROPUS_COUNTOF(p) (sizeof(p) / sizeof((p)[0]))
529 #if defined(DROPUS_X86) || defined(DROPUS_X64)
531 #elif defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN
535 return (*(
char*)&n) == 1;
541 #ifdef DROPUS_HAS_BYTESWAP16_INTRINSIC
542 #if defined(_MSC_VER)
543 return _byteswap_ushort(n);
544 #elif defined(__GNUC__) || defined(__clang__)
545 return __builtin_bswap16(n);
547 #error "This compiler does not support the byte swap intrinsic."
550 return ((n & 0xFF00) >> 8) |
557 #ifdef DROPUS_HAS_BYTESWAP32_INTRINSIC
558 #if defined(_MSC_VER)
559 return _byteswap_ulong(n);
560 #elif defined(__GNUC__) || defined(__clang__)
561 return __builtin_bswap32(n);
563 #error "This compiler does not support the byte swap intrinsic."
566 return ((n & 0xFF000000) >> 24) |
567 ((n & 0x00FF0000) >> 8) |
568 ((n & 0x0000FF00) << 8) |
569 ((n & 0x000000FF) << 24);
575 #ifdef DROPUS_HAS_BYTESWAP64_INTRINSIC
576 #if defined(_MSC_VER)
577 return _byteswap_uint64(n);
578 #elif defined(__GNUC__) || defined(__clang__)
579 return __builtin_bswap64(n);
581 #error "This compiler does not support the byte swap intrinsic."
598 if (dropus__is_little_endian()) {
599 return dropus__swap_endian_uint16(n);
607 if (dropus__is_little_endian()) {
608 return dropus__swap_endian_uint32(n);
616 if (dropus__is_little_endian()) {
617 return dropus__swap_endian_uint64(n);
626 if (!dropus__is_little_endian()) {
627 return dropus__swap_endian_uint16(n);
635 if (!dropus__is_little_endian()) {
636 return dropus__swap_endian_uint32(n);
644 if (!dropus__is_little_endian()) {
645 return dropus__swap_endian_uint64(n);
657 #define DROPUS_MAX_FRAME_SIZE_IN_BYTES 1275
658 #define DROPUS_MAX_PACKET_SIZE_IN_BYTES DROPUS_MAX_FRAME_SIZE_IN_BYTES*DROPUS_MAX_OPUS_FRAMES_PER_PACKET
665 return (toc & 0xF8) >> 3;
670 return (toc & 0x04) >> 2;
693 DROPUS_ASSERT(
config < 32);
699 return dropus_toc_config_mode(dropus_toc_config(toc));
706 8000, 8000, 8000, 8000,
707 12000, 12000, 12000, 12000,
708 16000, 16000, 16000, 16000,
711 8000, 8000, 8000, 8000,
712 16000, 16000, 16000, 16000,
713 24000, 24000, 24000, 24000,
714 48000, 48000, 48000, 48000
717 DROPUS_ASSERT(
config < 32);
723 return dropus_toc_config_sample_rate(dropus_toc_config(toc));
728 return dropus_toc_sample_rate(toc) / 1000;
746 DROPUS_ASSERT(
config < 32);
752 return dropus_toc_config_frame_size_in_pcm_frames(dropus_toc_config(toc));
770 DROPUS_ASSERT(
config < 32);
776 return dropus_toc_config_silk_frame_count(dropus_toc_config(toc));
783 -13732, -10050, -8266, -7526,
784 -6500, -5000, -2950, -820,
785 820, 2950, 5000, 6500,
786 7526, 8266, 10050, 13732
789 DROPUS_ASSERT(index < DROPUS_COUNTOF(Q13));
802 } dropus_range_decoder;
804 static DROPUS_INLINE void dropus_range_decoder_normalize(dropus_range_decoder* pRangeDecoder)
809 DROPUS_ASSERT(pRangeDecoder !=
NULL);
811 while (pRangeDecoder->rng <= 0x800000) {
812 pRangeDecoder->rng = (pRangeDecoder->rng << 8);
815 if (pRangeDecoder->dataSize > pRangeDecoder->readPointer) {
816 b1 = pRangeDecoder->pData[pRangeDecoder->readPointer++];
822 sym = ((pRangeDecoder->b0 & 0x01) << 7) | (b1 >> 7);
825 pRangeDecoder->b0 = (b1 & 0x01);
828 pRangeDecoder->val = ((pRangeDecoder->val << 8) + (255 - sym)) & 0x7FFFFFFF;
831 DROPUS_ASSERT(pRangeDecoder->rng > 0x800000);
836 DROPUS_ASSERT(pRangeDecoder !=
NULL);
838 pRangeDecoder->pData = pData;
839 pRangeDecoder->dataSize = dataSize;
840 pRangeDecoder->readPointer = 0;
842 pRangeDecoder->b0 = 0;
844 pRangeDecoder->b0 = pData[pRangeDecoder->readPointer++];
847 pRangeDecoder->rng = 128;
848 pRangeDecoder->val = 127 - (pRangeDecoder->b0 >> 1);
856 dropus_range_decoder_normalize(pRangeDecoder);
863 DROPUS_ASSERT(pRangeDecoder !=
NULL);
866 return (
dropus_uint16)(ft - DROPUS_MIN((pRangeDecoder->val / (pRangeDecoder->rng/ft)) + 1, ft));
875 for (i = 0; i < n; ++i) {
877 if (fl <= fs && fs < fh) {
897 DROPUS_ASSERT(pRangeDecoder !=
NULL);
898 DROPUS_ASSERT(f !=
NULL);
899 DROPUS_ASSERT(n > 0);
900 DROPUS_ASSERT(ft > 0);
902 k = dropus_range_decoder_k(f, n, fs, &fl, &fh);
905 DROPUS_ASSERT(fl < fh);
906 DROPUS_ASSERT(fh <= ft);
910 pRangeDecoder->val = pRangeDecoder->val - (pRangeDecoder->rng/ft) * (ft - fh);
914 pRangeDecoder->rng = (pRangeDecoder->rng/ft) * (fh - fl);
916 pRangeDecoder->rng = pRangeDecoder->rng - (pRangeDecoder->rng/ft) * (ft - fh);
926 dropus_range_decoder_normalize(pRangeDecoder);
935 DROPUS_ASSERT(pRangeDecoder !=
NULL);
936 DROPUS_ASSERT(f !=
NULL);
937 DROPUS_ASSERT(n > 0);
938 DROPUS_ASSERT(ft > 0);
941 fs = dropus_range_decoder_fs(pRangeDecoder, ft);
944 return dropus_range_decoder_update(pRangeDecoder, f, n, ft, fs);
950 if (pOpusStream ==
NULL) {
954 DROPUS_ZERO_OBJECT(pOpusStream);
960 #define DROPUS_MAX_SILK_FRAME_SIZE_IN_PCM_FRAMES 20*16
965 dropus_silk_frame_regularity_regular,
966 dropus_silk_frame_regularity_lbrr,
967 } dropus_silk_frame_regularity;
973 dropus_int16 s16[DROPUS_MAX_SILK_FRAME_SIZE_IN_PCM_FRAMES];
974 float f32[DROPUS_MAX_SILK_FRAME_SIZE_IN_PCM_FRAMES];
985 channels = dropus_toc_s(pOpusStream->
packet.
toc) + 1;
994 if (channels == 2 && iChannel == 0) {
995 dropus_uint16 f_Stage1[] = {7, 2, 1, 1, 1, 10, 24, 8, 1, 1, 3, 23, 92, 23, 3, 1, 1, 8, 24, 10, 1, 1, 1, 2, 7}, ft_Stage1 = 256;
997 dropus_uint16 f_Stage3[] = {51, 51, 52, 51, 51}, ft_Stage3 = 256;
1002 n = dropus_range_decoder_decode(pRD, f_Stage1, DROPUS_COUNTOF(f_Stage1), ft_Stage1);
1003 i0 = dropus_range_decoder_decode(pRD, f_Stage2, DROPUS_COUNTOF(f_Stage2), ft_Stage2);
1004 i1 = dropus_range_decoder_decode(pRD, f_Stage3, DROPUS_COUNTOF(f_Stage3), ft_Stage3);
1005 i2 = dropus_range_decoder_decode(pRD, f_Stage2, DROPUS_COUNTOF(f_Stage2), ft_Stage2);
1006 i3 = dropus_range_decoder_decode(pRD, f_Stage3, DROPUS_COUNTOF(f_Stage3), ft_Stage3);
1008 wi0 = i0 + 3 * (n / 5);
1009 wi1 = i2 + 3 * (n % 5);
1012 w1_Q13 = dropus_Q13(wi1) + (((dropus_Q13((
dropus_uint16)(wi1 + 1)) - dropus_Q13(wi1)) * 6554) >> 16) * ((2 * i3) + 1);
1013 w0_Q13 = dropus_Q13(wi0) + (((dropus_Q13((
dropus_uint16)(wi0 + 1)) - dropus_Q13(wi0)) * 6554) >> 16) * ((2 * i1) + 1) - w1_Q13;
1018 midOnlyFlag = (
dropus_uint8)dropus_range_decoder_decode(pRD, f_MOF, DROPUS_COUNTOF(f_MOF), ft_MOF);
1031 dropus_uint16 f_Stage1[] = {7, 2, 1, 1, 1, 10, 24, 8, 1, 1, 3, 23, 92, 23, 3, 1, 1, 8, 24, 10, 1, 1, 1, 2, 7}, ft_Stage1 = 256;
1033 dropus_uint16 f_Stage3[] = {51, 51, 52, 51, 51}, ft_Stage3 = 256;
1046 for (iChannel = 0; iChannel < 2; iChannel += 1) {
1048 if (regularity == dropus_silk_frame_regularity_lbrr && (pFlagsLBRR[iChannel] & (1 << iSILKFrame)) == 0) {
1053 if (iChannel == 0) {
1054 n = dropus_range_decoder_decode(pRD, f_Stage1, DROPUS_COUNTOF(f_Stage1), ft_Stage1);
1055 i0 = dropus_range_decoder_decode(pRD, f_Stage2, DROPUS_COUNTOF(f_Stage2), ft_Stage2);
1056 i1 = dropus_range_decoder_decode(pRD, f_Stage3, DROPUS_COUNTOF(f_Stage3), ft_Stage3);
1057 i2 = dropus_range_decoder_decode(pRD, f_Stage2, DROPUS_COUNTOF(f_Stage2), ft_Stage2);
1058 i3 = dropus_range_decoder_decode(pRD, f_Stage3, DROPUS_COUNTOF(f_Stage3), ft_Stage3);
1060 wi0 = i0 + 3 * (n / 5);
1061 wi1 = i2 + 3 * (n % 5);
1065 w1_Q13 = dropus_Q13(wi1) + (((dropus_Q13((
dropus_uint16)(wi1 + 1)) - dropus_Q13(wi1)) * 6554) >> 16) * ((2 * i3) + 1);
1066 w0_Q13 = dropus_Q13(wi0) + (((dropus_Q13((
dropus_uint16)(wi0 + 1)) - dropus_Q13(wi0)) * 6554) >> 16) * ((2 * i1) + 1) - w1_Q13;
1071 midOnlyFlag = (
dropus_uint8)dropus_range_decoder_decode(pRD, f_MOF, DROPUS_COUNTOF(f_MOF), ft_MOF);
1084 silkFrameCount = dropus_toc_silk_frame_count(pOpusStream->
packet.
toc);
1085 DROPUS_ASSERT(silkFrameCount > 0);
1087 for (iSILKFrame = 0; iSILKFrame < silkFrameCount; iSILKFrame += 1) {
1088 result = dropus_stream_decode_silk_frame__stereo(pOpusStream, pRD, regularity, iSILKFrame, pFlagsVAD, pFlagsLBRR, pSILKFrames);
1101 dropus_range_decoder rd;
1103 DROPUS_ASSERT(pOpusStream !=
NULL);
1104 DROPUS_ASSERT(pOpusFrame !=
NULL);
1105 DROPUS_ASSERT(pData !=
NULL);
1106 DROPUS_ASSERT(dataSize <= DROPUS_MAX_FRAME_SIZE_IN_BYTES);
1111 dropus_range_decoder_init(pData, pOpusFrame->
sizeInBytes, &rd);
1122 dropus_silk_frame silkFrames_LBRR[2][3];
1123 dropus_silk_frame silkFrames_Regular[2][3];
1129 frameCountSILK = dropus_toc_silk_frame_count(pOpusStream->
packet.
toc);
1130 if (frameCountSILK == 0) {
1134 channels = dropus_toc_s(pOpusStream->
packet.
toc) + 1;
1137 for (iChannel = 0; iChannel < channels; ++iChannel) {
1138 for (iFrameSILK = 0; iFrameSILK < frameCountSILK; ++iFrameSILK) {
1139 k = dropus_range_decoder_decode(&rd, f_Flags, DROPUS_COUNTOF(f_Flags), ft_Flags);
1140 DROPUS_ASSERT(k <= 1);
1141 flagsVAD[iChannel] |= (k << iFrameSILK);
1144 k = dropus_range_decoder_decode(&rd, f_Flags, DROPUS_COUNTOF(f_Flags), ft_Flags);
1145 DROPUS_ASSERT(k <= 1);
1150 if (frameCountSILK > 1) {
1152 for (iChannel = 0; iChannel < channels; iChannel += 1) {
1154 dropus_uint16 f_60[8] = {0, 41, 20, 29, 41, 15, 28, 82}, ft_60 = 256;
1155 if (flagsLBRR[iChannel] != 0) {
1156 if (frameCountSILK == 2) {
1157 k = dropus_range_decoder_decode(&rd, f_40, DROPUS_COUNTOF(f_40), ft_40);
1158 DROPUS_ASSERT(k <= 0b11);
1160 DROPUS_ASSERT(frameCountSILK == 3);
1161 k = dropus_range_decoder_decode(&rd, f_60, DROPUS_COUNTOF(f_60), ft_60);
1162 DROPUS_ASSERT(k <= 0b111);
1171 for (iFrameSILK = 0; iFrameSILK < frameCountSILK; ++iFrameSILK) {
1172 if (channels == 1) {
1175 result = dropus_stream_decode_silk_frames__stereo(pOpusStream, &rd, dropus_silk_frame_regularity_lbrr, flagsVAD, flagsLBRR, silkFrames_LBRR);
1178 for (iChannel = 0; iChannel < channels; ++iChannel) {
1179 if ((flagsLBRR[iChannel] & (1 << iFrameSILK)) != 0) {
1180 result = dropus_stream_decode_frame__silk_frame(pOpusStream, &rd, &silkFrames_LBRR[iFrameSILK]);
1190 if (channels == 1) {
1203 dropus_range_decoder rd;
1205 DROPUS_ASSERT(pOpusStream !=
NULL);
1206 DROPUS_ASSERT(pOpusFrame !=
NULL);
1207 DROPUS_ASSERT(pData !=
NULL);
1208 DROPUS_ASSERT(dataSize <= DROPUS_MAX_FRAME_SIZE_IN_BYTES);
1213 dropus_range_decoder_init(pData, pOpusFrame->
sizeInBytes, &rd);
1223 dropus_range_decoder rd;
1225 DROPUS_ASSERT(pOpusStream !=
NULL);
1226 DROPUS_ASSERT(pOpusFrame !=
NULL);
1227 DROPUS_ASSERT(pData !=
NULL);
1228 DROPUS_ASSERT(dataSize <= DROPUS_MAX_FRAME_SIZE_IN_BYTES);
1233 dropus_range_decoder_init(pData, pOpusFrame->
sizeInBytes, &rd);
1249 if (pOpusStream ==
NULL || pData ==
NULL) {
1253 DROPUS_ASSERT(DROPUS_MAX_PACKET_SIZE_IN_BYTES < 65536);
1254 if (dataSize > DROPUS_MAX_PACKET_SIZE_IN_BYTES) {
1264 toc = pRunningData8[0];
1271 code = dropus_toc_c(toc);
1278 if (opusFrameSize > DROPUS_MAX_FRAME_SIZE_IN_BYTES) {
1283 opusFrameSizes[0] = opusFrameSize;
1291 if ((dataSize & 1) == 0) {
1298 if (opusFrameSize > DROPUS_MAX_FRAME_SIZE_IN_BYTES) {
1303 opusFrameSizes[0] = opusFrameSize;
1304 opusFrameSizes[1] = opusFrameSize;
1321 byte0 = pRunningData8[0]; pRunningData8 += 1;
1333 if (byte0 >= 1 && byte0 <= 251) {
1334 opusFrameSize0 = byte0;
1336 if (byte0 >= 252 ) {
1342 byte1 = pRunningData8[0]; pRunningData8 += 1;
1343 opusFrameSize0 = (byte1*4) + byte0;
1349 if (opusFrameSize0 > DROPUS_MAX_FRAME_SIZE_IN_BYTES) {
1354 if ((dataSize-headerByteCount) > dataSize) {
1358 opusFrameSize1 = (
dropus_uint16)(dataSize-headerByteCount-opusFrameSize0);
1361 if (opusFrameSize1 > DROPUS_MAX_FRAME_SIZE_IN_BYTES) {
1366 if ((
size_t)(headerByteCount + opusFrameSize0 + opusFrameSize1) > dataSize) {
1372 opusFrameSizes[0] = opusFrameSize0;
1373 opusFrameSizes[1] = opusFrameSize1;
1396 frameCountByte = pRunningData8[0]; pRunningData8 += 1;
1397 v = (frameCountByte & 0x80) >> 7;
1398 p = (frameCountByte & 0x40) >> 6;
1399 M = (frameCountByte & 0x3F);
1402 ms = (M * dropus_toc_frame_size_in_pcm_frames(toc)) / dropus_toc_sample_rate_ms(toc);
1403 if (M < 1 || ms > 120) {
1414 paddingByteCount = 0;
1416 size_t iPaddingByte;
1417 for (iPaddingByte = 0; iPaddingByte < dataSize-2; ++iPaddingByte) {
1418 dropus_uint8 paddingByte = pRunningData8[0]; pRunningData8 += 1;
1420 paddingByteCount += 1;
1423 if (paddingByte == 255) {
1425 if (iPaddingByte+1 >= dataSize-2) {
1435 if (P > dataSize-2) {
1447 if (frameSize > DROPUS_MAX_FRAME_SIZE_IN_BYTES) {
1455 if (paddingByteCount+P > dataSize-2) {
1463 for (iFrame = 0; iFrame < opusFrameCount; ++iFrame) {
1464 opusFrameSizes[iFrame] = frameSize;
1472 for (iFrame = 0; iFrame < opusFrameCount-1; ++iFrame) {
1476 if (pRunningData8 >= ((
const dropus_uint8*)pData) + dataSize) {
1480 byte0 = pRunningData8[0]; pRunningData8 += 1;
1482 opusFrameSizes[iFrame] = 0;
1484 if (byte0 >= 1 && byte0 <= 251) {
1485 opusFrameSizes[iFrame] = byte0;
1487 if (byte0 >= 252 ) {
1488 if (pRunningData8 >= ((
const dropus_uint8*)pData) + dataSize) {
1492 byte1 = pRunningData8[0]; pRunningData8 += 1;
1493 opusFrameSizes[iFrame] = (byte1*4) + byte0;
1496 if (opusFrameSizes[iFrame] > DROPUS_MAX_FRAME_SIZE_IN_BYTES) {
1502 totalFrameSizeExceptLast += opusFrameSizes[iFrame];
1514 if ((headerSizeInBytes + totalFrameSizeExceptLast + P) > dataSize) {
1519 opusFrameSizes[opusFrameCount-1] = (
dropus_uint16)(dataSize - headerSizeInBytes - totalFrameSizeExceptLast - P);
1522 if (opusFrameSizes[opusFrameCount-1] > DROPUS_MAX_FRAME_SIZE_IN_BYTES) {
1547 mode = dropus_toc_mode(pOpusStream->
packet.
toc);
1550 for (iFrame = 0; iFrame < opusFrameCount; ++iFrame) {
1552 result = dropus_stream_decode_frame__silk(pOpusStream, &pOpusStream->
packet.
frames[iFrame], pRunningData8, opusFrameSizes[iFrame]);
1554 result = dropus_stream_decode_frame__celt(pOpusStream, &pOpusStream->
packet.
frames[iFrame], pRunningData8, opusFrameSizes[iFrame]);
1557 result = dropus_stream_decode_frame__hybrid(pOpusStream, &pOpusStream->
packet.
frames[iFrame], pRunningData8, opusFrameSizes[iFrame]);
1564 pRunningData8 += opusFrameSizes[iFrame];
1578 static void* dropus__malloc_default(
size_t sz,
void* pUserData)
1581 return DROPUS_MALLOC(sz);
1584 static void* dropus__realloc_default(
void* p,
size_t sz,
void* pUserData)
1587 return DROPUS_REALLOC(p, sz);
1590 static void dropus__free_default(
void* p,
void* pUserData)
1599 if (pAllocationCallbacks ==
NULL) {
1615 static void* dropus__realloc_from_callbacks(
void* p,
size_t szNew,
size_t szOld,
const dropus_allocation_callbacks* pAllocationCallbacks)
1617 if (pAllocationCallbacks ==
NULL) {
1635 DROPUS_COPY_MEMORY(p2, p, szOld);
1647 if (p ==
NULL || pAllocationCallbacks ==
NULL) {
1659 if (pAllocationCallbacks !=
NULL) {
1661 return *pAllocationCallbacks;
1666 allocationCallbacks.
onMalloc = dropus__malloc_default;
1667 allocationCallbacks.
onRealloc = dropus__realloc_default;
1668 allocationCallbacks.
onFree = dropus__free_default;
1669 return allocationCallbacks;
1675 DROPUS_ASSERT(pOpus !=
NULL);
1676 DROPUS_ASSERT(onRead !=
NULL);
1679 if (onRead ==
NULL) {
1686 pOpus->
allocationCallbacks = dropus__copy_allocation_callbacks_or_defaults(pAllocationCallbacks);
1700 if (pOpus ==
NULL) {
1704 DROPUS_ZERO_OBJECT(pOpus);
1706 return dropus_init_internal(pOpus, onRead, onSeek, pUserData, pAllocationCallbacks);
1709 #ifndef DR_OPUS_NO_STDIO
1986 #ifdef EPROTONOSUPPORT
1989 #ifdef ESOCKTNOSUPPORT
2004 #ifdef EADDRNOTAVAIL
2100 #ifdef ENOTRECOVERABLE
2113 static dropus_result dropus_fopen(FILE** ppFile,
const char* pFilePath,
const char* pOpenMode)
2115 #if defined(_MSC_VER) && _MSC_VER >= 1400
2119 if (ppFile !=
NULL) {
2123 if (pFilePath ==
NULL || pOpenMode ==
NULL || ppFile ==
NULL) {
2127 #if defined(_MSC_VER) && _MSC_VER >= 1400
2128 err = fopen_s(ppFile, pFilePath, pOpenMode);
2130 return dropus_result_from_errno(err);
2133 #if defined(_WIN32) || defined(__APPLE__)
2134 *ppFile = fopen(pFilePath, pOpenMode);
2136 #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 && defined(_LARGEFILE64_SOURCE)
2137 *ppFile = fopen64(pFilePath, pOpenMode);
2139 *ppFile = fopen(pFilePath, pOpenMode);
2142 if (*ppFile ==
NULL) {
2167 #if defined(_MSC_VER) || defined(__MINGW64__) || !defined(__STRICT_ANSI__)
2168 #define DROPUS_HAS_WFOPEN
2174 if (ppFile !=
NULL) {
2178 if (pFilePath ==
NULL || pOpenMode ==
NULL || ppFile ==
NULL) {
2182 #if defined(DROPUS_HAS_WFOPEN)
2185 #if defined(_MSC_VER) && _MSC_VER >= 1400
2186 errno_t err = _wfopen_s(ppFile, pFilePath, pOpenMode);
2188 return dropus_result_from_errno(err);
2191 *ppFile = _wfopen(pFilePath, pOpenMode);
2192 if (*ppFile ==
NULL) {
2193 return dropus_result_from_errno(
errno);
2196 (void)pAllocationCallbacks;
2207 const wchar_t* pFilePathTemp = pFilePath;
2208 char* pFilePathMB =
NULL;
2209 char pOpenModeMB[32] = {0};
2212 DROPUS_ZERO_OBJECT(&mbs);
2213 lenMB = wcsrtombs(
NULL, &pFilePathTemp, 0, &mbs);
2214 if (lenMB == (
size_t)-1) {
2215 return dropus_result_from_errno(
errno);
2218 pFilePathMB = (
char*)dropus__malloc_from_callbacks(lenMB + 1, pAllocationCallbacks);
2219 if (pFilePathMB ==
NULL) {
2223 pFilePathTemp = pFilePath;
2224 DROPUS_ZERO_OBJECT(&mbs);
2225 wcsrtombs(pFilePathMB, &pFilePathTemp, lenMB + 1, &mbs);
2231 if (pOpenMode[i] == 0) {
2232 pOpenModeMB[i] =
'\0';
2236 pOpenModeMB[i] = (char)pOpenMode[i];
2241 *ppFile = fopen(pFilePathMB, pOpenModeMB);
2243 dropus__free_from_callbacks(pFilePathMB, pAllocationCallbacks);
2246 if (*ppFile ==
NULL) {
2255 static size_t dropus_on_read_stdio(
void* pUserData,
void* pBufferOut,
size_t bytesToRead)
2257 return fread(pBufferOut, 1, bytesToRead, (FILE*)pUserData);
2270 if (pOpus ==
NULL) {
2274 DROPUS_ZERO_OBJECT(pOpus);
2276 if (pFilePath ==
NULL || pFilePath[0] ==
'\0') {
2280 result = dropus_fopen(&pFile, pFilePath,
"rb");
2285 pOpus->
pFile = (
void*)pFile;
2287 result = dropus_init_internal(pOpus, dropus_on_read_stdio, dropus_on_seek_stdio,
NULL, pAllocationCallbacks);
2301 if (pOpus ==
NULL) {
2305 DROPUS_ZERO_OBJECT(pOpus);
2307 if (pFilePath ==
NULL || pFilePath[0] ==
'\0') {
2311 result = dropus_wfopen(&pFile, pFilePath,
L"rb", pAllocationCallbacks);
2316 pOpus->
pFile = (
void*)pFile;
2318 result = dropus_init_internal(pOpus, dropus_on_read_stdio, dropus_on_seek_stdio,
NULL, pAllocationCallbacks);
2328 static size_t dropus_on_read_memory(
void* pUserData,
void* pBufferOut,
size_t bytesToRead)
2330 size_t bytesRemaining;
2333 DROPUS_ASSERT(pOpus !=
NULL);
2337 if (bytesToRead > bytesRemaining) {
2338 bytesToRead = bytesRemaining;
2341 if (bytesToRead > 0) {
2352 DROPUS_ASSERT(pOpus !=
NULL);
2355 if (byteOffset > 0) {
2380 if (pOpus ==
NULL) {
2384 DROPUS_ZERO_OBJECT(pOpus);
2386 if (pData ==
NULL || dataSize == 0) {
2394 return dropus_init_internal(pOpus, dropus_on_read_memory, dropus_on_seek_memory,
NULL, pAllocationCallbacks);
2400 if (pOpus ==
NULL) {
2404 #ifndef DR_OPUS_NO_STDIO
2407 fclose((FILE*)pOpus->
pFile);
2437 case DROPUS_BUSY:
return "Device or resource busy";
2477 default:
return "Unknown error";
2483 if (pAllocationCallbacks ==
NULL) {
2484 return dropus__malloc_default(sz,
NULL);
2486 return dropus__malloc_from_callbacks(sz, pAllocationCallbacks);
2492 if (pAllocationCallbacks ==
NULL) {
2493 return dropus__realloc_default(p, sz,
NULL);
2503 return dropus__realloc_from_callbacks(p, sz, 0, pAllocationCallbacks);
2509 if (pAllocationCallbacks ==
NULL) {
2510 dropus__free_default(p,
NULL);
2512 dropus__free_from_callbacks(p, pAllocationCallbacks);