gosu 0.15.2 → 1.1.0.pre2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (243) hide show
  1. checksums.yaml +4 -4
  2. data/dependencies/SDL/include/SDL.h +138 -0
  3. data/dependencies/SDL/include/SDL_assert.h +293 -0
  4. data/dependencies/SDL/include/SDL_atomic.h +295 -0
  5. data/dependencies/SDL/include/SDL_audio.h +859 -0
  6. data/dependencies/SDL/include/SDL_bits.h +121 -0
  7. data/dependencies/SDL/include/SDL_blendmode.h +123 -0
  8. data/dependencies/SDL/include/SDL_clipboard.h +71 -0
  9. data/dependencies/SDL/include/SDL_config.h +55 -0
  10. data/dependencies/SDL/include/SDL_config_android.h +182 -0
  11. data/dependencies/SDL/include/SDL_config_iphoneos.h +207 -0
  12. data/dependencies/SDL/include/SDL_config_macosx.h +266 -0
  13. data/dependencies/SDL/include/SDL_config_minimal.h +85 -0
  14. data/dependencies/SDL/include/SDL_config_os2.h +188 -0
  15. data/dependencies/SDL/include/SDL_config_pandora.h +135 -0
  16. data/dependencies/SDL/include/SDL_config_psp.h +165 -0
  17. data/dependencies/SDL/include/SDL_config_windows.h +288 -0
  18. data/dependencies/SDL/include/SDL_config_winrt.h +243 -0
  19. data/dependencies/SDL/include/SDL_config_wiz.h +149 -0
  20. data/dependencies/SDL/include/SDL_copying.h +20 -0
  21. data/dependencies/SDL/include/SDL_cpuinfo.h +299 -0
  22. data/dependencies/SDL/include/SDL_egl.h +1676 -0
  23. data/dependencies/SDL/include/SDL_endian.h +263 -0
  24. data/dependencies/SDL/include/SDL_error.h +112 -0
  25. data/dependencies/SDL/include/SDL_events.h +827 -0
  26. data/dependencies/SDL/include/SDL_filesystem.h +136 -0
  27. data/dependencies/SDL/include/SDL_gamecontroller.h +541 -0
  28. data/dependencies/SDL/include/SDL_gesture.h +87 -0
  29. data/dependencies/SDL/include/SDL_haptic.h +1247 -0
  30. data/dependencies/SDL/include/SDL_hints.h +1578 -0
  31. data/dependencies/SDL/include/SDL_joystick.h +499 -0
  32. data/dependencies/SDL/include/SDL_keyboard.h +217 -0
  33. data/dependencies/SDL/include/SDL_keycode.h +351 -0
  34. data/dependencies/SDL/include/SDL_loadso.h +81 -0
  35. data/dependencies/SDL/include/SDL_locale.h +101 -0
  36. data/dependencies/SDL/include/SDL_log.h +211 -0
  37. data/dependencies/SDL/include/SDL_main.h +180 -0
  38. data/dependencies/SDL/include/SDL_messagebox.h +146 -0
  39. data/dependencies/SDL/include/SDL_metal.h +117 -0
  40. data/dependencies/SDL/include/SDL_misc.h +75 -0
  41. data/dependencies/SDL/include/SDL_mouse.h +302 -0
  42. data/dependencies/SDL/include/SDL_mutex.h +251 -0
  43. data/dependencies/SDL/include/SDL_name.h +33 -0
  44. data/dependencies/SDL/include/SDL_opengl.h +2183 -0
  45. data/dependencies/SDL/include/SDL_opengl_glext.h +11180 -0
  46. data/dependencies/SDL/include/SDL_opengles.h +39 -0
  47. data/dependencies/SDL/include/SDL_opengles2.h +52 -0
  48. data/dependencies/SDL/include/SDL_opengles2_gl2.h +621 -0
  49. data/dependencies/SDL/include/SDL_opengles2_gl2ext.h +2050 -0
  50. data/dependencies/SDL/include/SDL_opengles2_gl2platform.h +30 -0
  51. data/dependencies/SDL/include/SDL_opengles2_khrplatform.h +282 -0
  52. data/dependencies/SDL/include/SDL_pixels.h +479 -0
  53. data/dependencies/SDL/include/SDL_platform.h +198 -0
  54. data/dependencies/SDL/include/SDL_power.h +75 -0
  55. data/dependencies/SDL/include/SDL_quit.h +58 -0
  56. data/dependencies/SDL/include/SDL_rect.h +174 -0
  57. data/dependencies/SDL/include/SDL_render.h +1158 -0
  58. data/dependencies/SDL/include/SDL_revision.h +2 -0
  59. data/dependencies/SDL/include/SDL_rwops.h +283 -0
  60. data/dependencies/SDL/include/SDL_scancode.h +413 -0
  61. data/dependencies/SDL/include/SDL_sensor.h +267 -0
  62. data/dependencies/SDL/include/SDL_shape.h +144 -0
  63. data/dependencies/SDL/include/SDL_stdinc.h +647 -0
  64. data/dependencies/SDL/include/SDL_surface.h +563 -0
  65. data/dependencies/SDL/include/SDL_system.h +325 -0
  66. data/dependencies/SDL/include/SDL_syswm.h +354 -0
  67. data/dependencies/SDL/include/SDL_test.h +69 -0
  68. data/dependencies/SDL/include/SDL_test_assert.h +105 -0
  69. data/dependencies/SDL/include/SDL_test_common.h +218 -0
  70. data/dependencies/SDL/include/SDL_test_compare.h +69 -0
  71. data/dependencies/SDL/include/SDL_test_crc32.h +124 -0
  72. data/dependencies/SDL/include/SDL_test_font.h +81 -0
  73. data/dependencies/SDL/include/SDL_test_fuzzer.h +384 -0
  74. data/dependencies/SDL/include/SDL_test_harness.h +134 -0
  75. data/dependencies/SDL/include/SDL_test_images.h +78 -0
  76. data/dependencies/SDL/include/SDL_test_log.h +67 -0
  77. data/dependencies/SDL/include/SDL_test_md5.h +129 -0
  78. data/dependencies/SDL/include/SDL_test_memory.h +63 -0
  79. data/dependencies/SDL/include/SDL_test_random.h +115 -0
  80. data/dependencies/SDL/include/SDL_thread.h +366 -0
  81. data/dependencies/SDL/include/SDL_timer.h +115 -0
  82. data/dependencies/SDL/include/SDL_touch.h +102 -0
  83. data/dependencies/SDL/include/SDL_types.h +29 -0
  84. data/dependencies/SDL/include/SDL_version.h +162 -0
  85. data/dependencies/SDL/include/SDL_video.h +1282 -0
  86. data/dependencies/SDL/include/SDL_vulkan.h +276 -0
  87. data/dependencies/SDL/include/begin_code.h +166 -0
  88. data/dependencies/SDL/include/close_code.h +40 -0
  89. data/dependencies/SDL/lib/x64/libSDL2.dll.a +0 -0
  90. data/dependencies/SDL/lib/x86/libSDL2.dll.a +0 -0
  91. data/dependencies/SDL_sound/SDL_sound.c +795 -0
  92. data/dependencies/SDL_sound/SDL_sound.h +725 -0
  93. data/dependencies/SDL_sound/SDL_sound_aiff.c +537 -0
  94. data/dependencies/SDL_sound/SDL_sound_au.c +352 -0
  95. data/dependencies/SDL_sound/SDL_sound_coreaudio.c +747 -0
  96. data/dependencies/SDL_sound/SDL_sound_flac.c +182 -0
  97. data/dependencies/SDL_sound/SDL_sound_internal.h +304 -0
  98. data/dependencies/SDL_sound/SDL_sound_modplug.c +228 -0
  99. data/dependencies/SDL_sound/SDL_sound_mp3.c +184 -0
  100. data/dependencies/SDL_sound/SDL_sound_raw.c +164 -0
  101. data/dependencies/SDL_sound/SDL_sound_shn.c +1309 -0
  102. data/dependencies/SDL_sound/SDL_sound_voc.c +550 -0
  103. data/dependencies/SDL_sound/SDL_sound_vorbis.c +223 -0
  104. data/dependencies/SDL_sound/SDL_sound_wav.c +783 -0
  105. data/dependencies/SDL_sound/dr_flac.h +5906 -0
  106. data/dependencies/SDL_sound/dr_mp3.h +2832 -0
  107. data/dependencies/SDL_sound/libmodplug/fastmix.c +1748 -0
  108. data/dependencies/SDL_sound/libmodplug/libmodplug.h +1001 -0
  109. data/dependencies/SDL_sound/libmodplug/load_669.c +188 -0
  110. data/dependencies/SDL_sound/libmodplug/load_abc.c +4725 -0
  111. data/dependencies/SDL_sound/libmodplug/load_amf.c +403 -0
  112. data/dependencies/SDL_sound/libmodplug/load_ams.c +587 -0
  113. data/dependencies/SDL_sound/libmodplug/load_dbm.c +357 -0
  114. data/dependencies/SDL_sound/libmodplug/load_dmf.c +531 -0
  115. data/dependencies/SDL_sound/libmodplug/load_dsm.c +232 -0
  116. data/dependencies/SDL_sound/libmodplug/load_far.c +253 -0
  117. data/dependencies/SDL_sound/libmodplug/load_it.c +796 -0
  118. data/dependencies/SDL_sound/libmodplug/load_mdl.c +488 -0
  119. data/dependencies/SDL_sound/libmodplug/load_med.c +757 -0
  120. data/dependencies/SDL_sound/libmodplug/load_mid.c +1405 -0
  121. data/dependencies/SDL_sound/libmodplug/load_mod.c +269 -0
  122. data/dependencies/SDL_sound/libmodplug/load_mt2.c +546 -0
  123. data/dependencies/SDL_sound/libmodplug/load_mtm.c +142 -0
  124. data/dependencies/SDL_sound/libmodplug/load_okt.c +192 -0
  125. data/dependencies/SDL_sound/libmodplug/load_pat.c +1143 -0
  126. data/dependencies/SDL_sound/libmodplug/load_pat.h +25 -0
  127. data/dependencies/SDL_sound/libmodplug/load_psm.c +350 -0
  128. data/dependencies/SDL_sound/libmodplug/load_ptm.c +204 -0
  129. data/dependencies/SDL_sound/libmodplug/load_s3m.c +325 -0
  130. data/dependencies/SDL_sound/libmodplug/load_stm.c +180 -0
  131. data/dependencies/SDL_sound/libmodplug/load_ult.c +206 -0
  132. data/dependencies/SDL_sound/libmodplug/load_umx.c +51 -0
  133. data/dependencies/SDL_sound/libmodplug/load_xm.c +554 -0
  134. data/dependencies/SDL_sound/libmodplug/mmcmp.c +382 -0
  135. data/dependencies/SDL_sound/libmodplug/modplug.c +170 -0
  136. data/dependencies/SDL_sound/libmodplug/modplug.h +90 -0
  137. data/dependencies/SDL_sound/libmodplug/snd_dsp.c +301 -0
  138. data/dependencies/SDL_sound/libmodplug/snd_flt.c +63 -0
  139. data/dependencies/SDL_sound/libmodplug/snd_fx.c +2350 -0
  140. data/dependencies/SDL_sound/libmodplug/sndfile.c +1169 -0
  141. data/dependencies/SDL_sound/libmodplug/sndmix.c +1034 -0
  142. data/dependencies/SDL_sound/libmodplug/tables.h +371 -0
  143. data/{src/stb_vorbis.c → dependencies/SDL_sound/stb_vorbis.h} +143 -78
  144. data/dependencies/al_soft/AL/al.h +655 -0
  145. data/dependencies/al_soft/AL/alc.h +270 -0
  146. data/dependencies/al_soft/AL/alext.h +585 -0
  147. data/dependencies/al_soft/AL/efx-creative.h +3 -0
  148. data/dependencies/al_soft/AL/efx-presets.h +402 -0
  149. data/dependencies/al_soft/AL/efx.h +762 -0
  150. data/dependencies/al_soft/x64/libOpenAL32.dll.a +0 -0
  151. data/dependencies/al_soft/x86/libOpenAL32.dll.a +0 -0
  152. data/{src → dependencies/stb}/stb_image.h +330 -127
  153. data/{src → dependencies/stb}/stb_image_write.h +156 -85
  154. data/{src → dependencies/stb}/stb_truetype.h +192 -69
  155. data/{src → dependencies/utf8proc}/utf8proc.c +0 -0
  156. data/{src → dependencies/utf8proc}/utf8proc.h +0 -0
  157. data/{src → dependencies/utf8proc}/utf8proc_data.h +0 -0
  158. data/ext/gosu/extconf.rb +53 -39
  159. data/{Gosu → include/Gosu}/Audio.hpp +6 -8
  160. data/include/Gosu/Bitmap.hpp +100 -0
  161. data/{Gosu → include/Gosu}/Buttons.hpp +104 -44
  162. data/{Gosu → include/Gosu}/Color.hpp +0 -0
  163. data/{Gosu → include/Gosu}/Directories.hpp +0 -0
  164. data/{Gosu → include/Gosu}/Font.hpp +1 -1
  165. data/{Gosu → include/Gosu}/Fwd.hpp +0 -5
  166. data/{Gosu → include/Gosu}/Gosu.hpp +0 -0
  167. data/{Gosu → include/Gosu}/Graphics.hpp +0 -0
  168. data/{Gosu → include/Gosu}/GraphicsBase.hpp +0 -0
  169. data/{Gosu → include/Gosu}/IO.hpp +0 -0
  170. data/{Gosu → include/Gosu}/Image.hpp +7 -6
  171. data/{Gosu → include/Gosu}/ImageData.hpp +0 -0
  172. data/{Gosu → include/Gosu}/Input.hpp +39 -51
  173. data/{Gosu → include/Gosu}/Inspection.hpp +0 -0
  174. data/{Gosu → include/Gosu}/Math.hpp +0 -0
  175. data/{Gosu → include/Gosu}/Platform.hpp +0 -0
  176. data/{Gosu → include/Gosu}/Text.hpp +0 -0
  177. data/{Gosu → include/Gosu}/TextInput.hpp +0 -0
  178. data/{Gosu → include/Gosu}/Timing.hpp +0 -0
  179. data/{Gosu → include/Gosu}/Utility.hpp +15 -4
  180. data/{Gosu → include/Gosu}/Version.hpp +3 -3
  181. data/{Gosu → include/Gosu}/Window.hpp +46 -34
  182. data/lib/OpenAL32.dll +0 -0
  183. data/lib/SDL2.dll +0 -0
  184. data/lib/gosu.rb +0 -3
  185. data/lib/gosu/patches.rb +0 -23
  186. data/lib/gosu/preview.rb +1 -3
  187. data/lib/gosu/swig_patches.rb +14 -12
  188. data/lib64/OpenAL32.dll +0 -0
  189. data/lib64/SDL2.dll +0 -0
  190. data/rdoc/gosu.rb +112 -23
  191. data/src/Audio.cpp +50 -224
  192. data/src/AudioFile.hpp +20 -37
  193. data/src/AudioFileAudioToolbox.cpp +237 -0
  194. data/src/AudioFileSDLSound.cpp +147 -0
  195. data/src/AudioImpl.cpp +3 -12
  196. data/src/AudioImpl.hpp +3 -1
  197. data/src/Bitmap.cpp +85 -83
  198. data/src/BitmapIO.cpp +52 -58
  199. data/src/Font.cpp +3 -1
  200. data/src/Graphics.cpp +7 -4
  201. data/src/Image.cpp +13 -16
  202. data/src/Input.cpp +412 -164
  203. data/src/LargeImageData.cpp +1 -1
  204. data/src/MarkupParser.cpp +2 -1
  205. data/src/Resolution.cpp +8 -8
  206. data/src/RubyGosu.cxx +1017 -196
  207. data/src/RubyGosu.h +4 -2
  208. data/src/TexChunk.cpp +1 -1
  209. data/src/TextBuilder.cpp +3 -1
  210. data/src/Texture.cpp +1 -1
  211. data/src/TrueTypeFont.cpp +1 -1
  212. data/src/TrueTypeFontWin.cpp +3 -3
  213. data/src/Utility.cpp +11 -7
  214. data/src/Window.cpp +90 -62
  215. data/src/WindowUIKit.cpp +21 -9
  216. metadata +194 -65
  217. data/Gosu/AutoLink.hpp +0 -14
  218. data/Gosu/Bitmap.hpp +0 -113
  219. data/Gosu/Channel.h +0 -25
  220. data/Gosu/Color.h +0 -38
  221. data/Gosu/Font.h +0 -36
  222. data/Gosu/Gosu.h +0 -79
  223. data/Gosu/Image.h +0 -54
  224. data/Gosu/Sample.h +0 -19
  225. data/Gosu/Song.h +0 -24
  226. data/Gosu/TextInput.h +0 -30
  227. data/Gosu/Window.h +0 -61
  228. data/lib/gosu/zen.rb +0 -89
  229. data/src/AudioToolboxFile.hpp +0 -210
  230. data/src/ChannelWrapper.cpp +0 -50
  231. data/src/ColorWrapper.cpp +0 -126
  232. data/src/Constants.cpp +0 -287
  233. data/src/FontWrapper.cpp +0 -74
  234. data/src/GosuWrapper.cpp +0 -232
  235. data/src/ImageWrapper.cpp +0 -168
  236. data/src/MPEGFile.hpp +0 -90
  237. data/src/OggFile.hpp +0 -92
  238. data/src/SampleWrapper.cpp +0 -30
  239. data/src/SndFile.hpp +0 -174
  240. data/src/SongWrapper.cpp +0 -52
  241. data/src/TextInputWrapper.cpp +0 -101
  242. data/src/WinMain.cpp +0 -64
  243. data/src/WindowWrapper.cpp +0 -289
@@ -0,0 +1,2832 @@
1
+ // MP3 audio decoder. Public domain. See "unlicense" statement at the end of this file.
2
+ // dr_mp3 - v0.2.7 - 2018-07-13
3
+ //
4
+ // David Reid - mackron@gmail.com
5
+ //
6
+ // Based off minimp3 (https://github.com/lieff/minimp3) which is where the real work was done. See the bottom of this file for
7
+ // differences between minimp3 and dr_mp3.
8
+
9
+ // USAGE
10
+ // =====
11
+ // dr_mp3 is a single-file library. To use it, do something like the following in one .c file.
12
+ // #define DR_MP3_IMPLEMENTATION
13
+ // #include "dr_mp3.h"
14
+ //
15
+ // You can then #include this file in other parts of the program as you would with any other header file. To decode audio data,
16
+ // do something like the following:
17
+ //
18
+ // drmp3 mp3;
19
+ // if (!drmp3_init_file(&mp3, "MySong.mp3", NULL)) {
20
+ // // Failed to open file
21
+ // }
22
+ //
23
+ // ...
24
+ //
25
+ // drmp3_uint64 framesRead = drmp3_read_f32(pMP3, framesToRead, pFrames);
26
+ //
27
+ // The drmp3 object is transparent so you can get access to the channel count and sample rate like so:
28
+ //
29
+ // drmp3_uint32 channels = mp3.channels;
30
+ // drmp3_uint32 sampleRate = mp3.sampleRate;
31
+ //
32
+ // The third parameter of drmp3_init_file() in the example above allows you to control the output channel count and sample rate. It
33
+ // is a pointer to a drmp3_config object. Setting any of the variables of this object to 0 will cause dr_mp3 to use defaults.
34
+ //
35
+ // The example above initializes a decoder from a file, but you can also initialize it from a block of memory and read and seek
36
+ // callbacks with drmp3_init_memory() and drmp3_init() respectively.
37
+ //
38
+ // You do need to do any annoying memory management when reading PCM frames - this is all managed internally. You can request
39
+ // any number of PCM frames in each call to drmp3_read_f32() and it will return as many PCM frames as it can, up to the requested
40
+ // amount.
41
+ //
42
+ // You can also decode an entire file in one go with drmp3_open_and_decode_f32(), drmp3_open_and_decode_memory_f32() and
43
+ // drmp3_open_and_decode_file_f32().
44
+ //
45
+ //
46
+ // OPTIONS
47
+ // =======
48
+ // #define these options before including this file.
49
+ //
50
+ // #define DR_MP3_NO_STDIO
51
+ // Disable drmp3_init_file(), etc.
52
+ //
53
+ // #define DR_MP3_NO_SIMD
54
+ // Disable SIMD optimizations.
55
+ //
56
+ //
57
+ // LIMITATIONS
58
+ // ===========
59
+ // - Seeking is extremely inefficient.
60
+
61
+ #ifndef dr_mp3_h
62
+ #define dr_mp3_h
63
+
64
+ #ifdef __cplusplus
65
+ extern "C" {
66
+ #endif
67
+
68
+ #include <stddef.h>
69
+
70
+ #ifdef __SDL_SOUND_INTERNAL__
71
+ typedef Sint8 drmp3_int8;
72
+ typedef Uint8 drmp3_uint8;
73
+ typedef Sint16 drmp3_int16;
74
+ typedef Uint16 drmp3_uint16;
75
+ typedef Sint32 drmp3_int32;
76
+ typedef Uint32 drmp3_uint32;
77
+ typedef Sint64 drmp3_int64;
78
+ typedef Uint64 drmp3_uint64;
79
+ #elif defined(_MSC_VER) && _MSC_VER < 1600
80
+ typedef signed char drmp3_int8;
81
+ typedef unsigned char drmp3_uint8;
82
+ typedef signed short drmp3_int16;
83
+ typedef unsigned short drmp3_uint16;
84
+ typedef signed int drmp3_int32;
85
+ typedef unsigned int drmp3_uint32;
86
+ typedef signed __int64 drmp3_int64;
87
+ typedef unsigned __int64 drmp3_uint64;
88
+ #else
89
+ #include <stdint.h>
90
+ typedef int8_t drmp3_int8;
91
+ typedef uint8_t drmp3_uint8;
92
+ typedef int16_t drmp3_int16;
93
+ typedef uint16_t drmp3_uint16;
94
+ typedef int32_t drmp3_int32;
95
+ typedef uint32_t drmp3_uint32;
96
+ typedef int64_t drmp3_int64;
97
+ typedef uint64_t drmp3_uint64;
98
+ #endif
99
+ typedef drmp3_uint8 drmp3_bool8;
100
+ typedef drmp3_uint32 drmp3_bool32;
101
+ #define DRMP3_TRUE 1
102
+ #define DRMP3_FALSE 0
103
+
104
+ #define DRMP3_MAX_SAMPLES_PER_FRAME (1152*2)
105
+
106
+
107
+ // Low Level Push API
108
+ // ==================
109
+ typedef struct
110
+ {
111
+ int frame_bytes, channels, hz, layer, bitrate_kbps;
112
+ } drmp3dec_frame_info;
113
+
114
+ typedef struct
115
+ {
116
+ float mdct_overlap[2][9*32], qmf_state[15*2*32];
117
+ int reserv, free_format_bytes;
118
+ unsigned char header[4], reserv_buf[511];
119
+ } drmp3dec;
120
+
121
+ // Initializes a low level decoder.
122
+ void drmp3dec_init(drmp3dec *dec);
123
+
124
+ // Reads a frame from a low level decoder.
125
+ int drmp3dec_decode_frame(drmp3dec *dec, const unsigned char *mp3, int mp3_bytes, short *pcm, drmp3dec_frame_info *info);
126
+
127
+
128
+ // Main API (Pull API)
129
+ // ===================
130
+
131
+ typedef struct drmp3_src drmp3_src;
132
+ typedef drmp3_uint64 (* drmp3_src_read_proc)(drmp3_src* pSRC, drmp3_uint64 frameCount, void* pFramesOut, void* pUserData); // Returns the number of frames that were read.
133
+
134
+ typedef enum
135
+ {
136
+ drmp3_src_algorithm_none,
137
+ drmp3_src_algorithm_linear
138
+ } drmp3_src_algorithm;
139
+
140
+ #define DRMP3_SRC_CACHE_SIZE_IN_FRAMES 512
141
+ typedef struct
142
+ {
143
+ drmp3_src* pSRC;
144
+ float pCachedFrames[2 * DRMP3_SRC_CACHE_SIZE_IN_FRAMES];
145
+ drmp3_uint32 cachedFrameCount;
146
+ drmp3_uint32 iNextFrame;
147
+ } drmp3_src_cache;
148
+
149
+ typedef struct
150
+ {
151
+ drmp3_uint32 sampleRateIn;
152
+ drmp3_uint32 sampleRateOut;
153
+ drmp3_uint32 channels;
154
+ drmp3_src_algorithm algorithm;
155
+ drmp3_uint32 cacheSizeInFrames; // The number of frames to read from the client at a time.
156
+ } drmp3_src_config;
157
+
158
+ struct drmp3_src
159
+ {
160
+ drmp3_src_config config;
161
+ drmp3_src_read_proc onRead;
162
+ void* pUserData;
163
+ float bin[256];
164
+ drmp3_src_cache cache; // <-- For simplifying and optimizing client -> memory reading.
165
+ union
166
+ {
167
+ struct
168
+ {
169
+ float alpha;
170
+ drmp3_bool32 isPrevFramesLoaded : 1;
171
+ drmp3_bool32 isNextFramesLoaded : 1;
172
+ } linear;
173
+ } algo;
174
+ };
175
+
176
+ typedef enum
177
+ {
178
+ drmp3_seek_origin_start,
179
+ drmp3_seek_origin_current
180
+ } drmp3_seek_origin;
181
+
182
+ // Callback for when data is read. Return value is the number of bytes actually read.
183
+ //
184
+ // pUserData [in] The user data that was passed to drmp3_init(), drmp3_open() and family.
185
+ // pBufferOut [out] The output buffer.
186
+ // bytesToRead [in] The number of bytes to read.
187
+ //
188
+ // Returns the number of bytes actually read.
189
+ //
190
+ // A return value of less than bytesToRead indicates the end of the stream. Do _not_ return from this callback until
191
+ // either the entire bytesToRead is filled or you have reached the end of the stream.
192
+ typedef size_t (* drmp3_read_proc)(void* pUserData, void* pBufferOut, size_t bytesToRead);
193
+
194
+ // Callback for when data needs to be seeked.
195
+ //
196
+ // pUserData [in] The user data that was passed to drmp3_init(), drmp3_open() and family.
197
+ // offset [in] The number of bytes to move, relative to the origin. Will never be negative.
198
+ // origin [in] The origin of the seek - the current position or the start of the stream.
199
+ //
200
+ // Returns whether or not the seek was successful.
201
+ //
202
+ // Whether or not it is relative to the beginning or current position is determined by the "origin" parameter which
203
+ // will be either drmp3_seek_origin_start or drmp3_seek_origin_current.
204
+ typedef drmp3_bool32 (* drmp3_seek_proc)(void* pUserData, int offset, drmp3_seek_origin origin);
205
+
206
+ typedef struct
207
+ {
208
+ drmp3_uint32 outputChannels;
209
+ drmp3_uint32 outputSampleRate;
210
+ } drmp3_config;
211
+
212
+ typedef struct
213
+ {
214
+ drmp3dec decoder;
215
+ drmp3dec_frame_info frameInfo;
216
+ drmp3_uint32 channels;
217
+ drmp3_uint32 sampleRate;
218
+ drmp3_read_proc onRead;
219
+ drmp3_seek_proc onSeek;
220
+ void* pUserData;
221
+ drmp3_uint32 frameChannels; // The number of channels in the currently loaded MP3 frame. Internal use only.
222
+ drmp3_uint32 frameSampleRate; // The sample rate of the currently loaded MP3 frame. Internal use only.
223
+ drmp3_uint32 framesConsumed;
224
+ drmp3_uint32 framesRemaining;
225
+ drmp3_int16 frames[DRMP3_MAX_SAMPLES_PER_FRAME];
226
+ drmp3_src src;
227
+ size_t dataSize;
228
+ size_t dataCapacity;
229
+ drmp3_uint8* pData;
230
+ drmp3_bool32 atEnd : 1;
231
+ struct
232
+ {
233
+ const drmp3_uint8* pData;
234
+ size_t dataSize;
235
+ size_t currentReadPos;
236
+ } memory; // Only used for decoders that were opened against a block of memory.
237
+ } drmp3;
238
+
239
+ // Initializes an MP3 decoder.
240
+ //
241
+ // onRead [in] The function to call when data needs to be read from the client.
242
+ // onSeek [in] The function to call when the read position of the client data needs to move.
243
+ // pUserData [in, optional] A pointer to application defined data that will be passed to onRead and onSeek.
244
+ //
245
+ // Returns true if successful; false otherwise.
246
+ //
247
+ // Close the loader with drmp3_uninit().
248
+ //
249
+ // See also: drmp3_init_file(), drmp3_init_memory(), drmp3_uninit()
250
+ drmp3_bool32 drmp3_init(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, const drmp3_config* pConfig);
251
+
252
+ // Initializes an MP3 decoder from a block of memory.
253
+ //
254
+ // This does not create a copy of the data. It is up to the application to ensure the buffer remains valid for
255
+ // the lifetime of the drmp3 object.
256
+ //
257
+ // The buffer should contain the contents of the entire MP3 file.
258
+ drmp3_bool32 drmp3_init_memory(drmp3* pMP3, const void* pData, size_t dataSize, const drmp3_config* pConfig);
259
+
260
+ #ifndef DR_MP3_NO_STDIO
261
+ // Initializes an MP3 decoder from a file.
262
+ //
263
+ // This holds the internal FILE object until drmp3_uninit() is called. Keep this in mind if you're caching drmp3
264
+ // objects because the operating system may restrict the number of file handles an application can have open at
265
+ // any given time.
266
+ drmp3_bool32 drmp3_init_file(drmp3* pMP3, const char* filePath, const drmp3_config* pConfig);
267
+ #endif
268
+
269
+ // Uninitializes an MP3 decoder.
270
+ void drmp3_uninit(drmp3* pMP3);
271
+
272
+ // Reads PCM frames as interleaved 32-bit IEEE floating point PCM.
273
+ //
274
+ // Note that framesToRead specifies the number of PCM frames to read, _not_ the number of MP3 frames.
275
+ drmp3_uint64 drmp3_read_f32(drmp3* pMP3, drmp3_uint64 framesToRead, float* pBufferOut);
276
+
277
+ // Seeks to a specific frame.
278
+ //
279
+ // Note that this is _not_ an MP3 frame, but rather a PCM frame.
280
+ drmp3_bool32 drmp3_seek_to_frame(drmp3* pMP3, drmp3_uint64 frameIndex);
281
+
282
+
283
+ // Opens an decodes an entire MP3 stream as a single operation.
284
+ //
285
+ // pConfig is both an input and output. On input it contains what you want. On output it contains what you got.
286
+ //
287
+ // Free the returned pointer with drmp3_free().
288
+ float* drmp3_open_and_decode_f32(drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount);
289
+ float* drmp3_open_and_decode_memory_f32(const void* pData, size_t dataSize, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount);
290
+ #ifndef DR_MP3_NO_STDIO
291
+ float* drmp3_open_and_decode_file_f32(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount);
292
+ #endif
293
+
294
+ // Frees any memory that was allocated by a public drmp3 API.
295
+ void drmp3_free(void* p);
296
+
297
+ #ifdef __cplusplus
298
+ }
299
+ #endif
300
+ #endif // dr_mp3_h
301
+
302
+
303
+ /////////////////////////////////////////////////////
304
+ //
305
+ // IMPLEMENTATION
306
+ //
307
+ /////////////////////////////////////////////////////
308
+ #ifdef DR_MP3_IMPLEMENTATION
309
+ #include <stdlib.h>
310
+ #include <string.h>
311
+ #include <stdint.h>
312
+ #include <limits.h> // For INT_MAX
313
+
314
+ // Disable SIMD when compiling with TCC for now.
315
+ #if defined(__TINYC__)
316
+ #define DR_MP3_NO_SIMD
317
+ #endif
318
+
319
+ #define DRMP3_MAX_FREE_FORMAT_FRAME_SIZE 2304 /* more than ISO spec's */
320
+ #define DRMP3_MAX_FRAME_SYNC_MATCHES 10
321
+
322
+ #define DRMP3_MAX_L3_FRAME_PAYLOAD_BYTES DRMP3_MAX_FREE_FORMAT_FRAME_SIZE /* MUST be >= 320000/8/32000*1152 = 1440 */
323
+
324
+ #define DRMP3_MAX_BITRESERVOIR_BYTES 511
325
+ #define DRMP3_SHORT_BLOCK_TYPE 2
326
+ #define DRMP3_STOP_BLOCK_TYPE 3
327
+ #define DRMP3_MODE_MONO 3
328
+ #define DRMP3_MODE_JOINT_STEREO 1
329
+ #define DRMP3_HDR_SIZE 4
330
+ #define DRMP3_HDR_IS_MONO(h) (((h[3]) & 0xC0) == 0xC0)
331
+ #define DRMP3_HDR_IS_MS_STEREO(h) (((h[3]) & 0xE0) == 0x60)
332
+ #define DRMP3_HDR_IS_FREE_FORMAT(h) (((h[2]) & 0xF0) == 0)
333
+ #define DRMP3_HDR_IS_CRC(h) (!((h[1]) & 1))
334
+ #define DRMP3_HDR_TEST_PADDING(h) ((h[2]) & 0x2)
335
+ #define DRMP3_HDR_TEST_MPEG1(h) ((h[1]) & 0x8)
336
+ #define DRMP3_HDR_TEST_NOT_MPEG25(h) ((h[1]) & 0x10)
337
+ #define DRMP3_HDR_TEST_I_STEREO(h) ((h[3]) & 0x10)
338
+ #define DRMP3_HDR_TEST_MS_STEREO(h) ((h[3]) & 0x20)
339
+ #define DRMP3_HDR_GET_STEREO_MODE(h) (((h[3]) >> 6) & 3)
340
+ #define DRMP3_HDR_GET_STEREO_MODE_EXT(h) (((h[3]) >> 4) & 3)
341
+ #define DRMP3_HDR_GET_LAYER(h) (((h[1]) >> 1) & 3)
342
+ #define DRMP3_HDR_GET_BITRATE(h) ((h[2]) >> 4)
343
+ #define DRMP3_HDR_GET_SAMPLE_RATE(h) (((h[2]) >> 2) & 3)
344
+ #define DRMP3_HDR_GET_MY_SAMPLE_RATE(h) (DRMP3_HDR_GET_SAMPLE_RATE(h) + (((h[1] >> 3) & 1) + ((h[1] >> 4) & 1))*3)
345
+ #define DRMP3_HDR_IS_FRAME_576(h) ((h[1] & 14) == 2)
346
+ #define DRMP3_HDR_IS_LAYER_1(h) ((h[1] & 6) == 6)
347
+
348
+ #define DRMP3_BITS_DEQUANTIZER_OUT -1
349
+ #define DRMP3_MAX_SCF (255 + DRMP3_BITS_DEQUANTIZER_OUT*4 - 210)
350
+ #define DRMP3_MAX_SCFI ((DRMP3_MAX_SCF + 3) & ~3)
351
+
352
+ #define DRMP3_MIN(a, b) ((a) > (b) ? (b) : (a))
353
+ #define DRMP3_MAX(a, b) ((a) < (b) ? (b) : (a))
354
+
355
+ #if !defined(DR_MP3_NO_SIMD)
356
+
357
+ #if !defined(DR_MP3_ONLY_SIMD) && (defined(_M_X64) || defined(_M_ARM64) || defined(__x86_64__) || defined(__aarch64__))
358
+ /* x64 always have SSE2, arm64 always have neon, no need for generic code */
359
+ #define DR_MP3_ONLY_SIMD
360
+ #endif
361
+
362
+ #if (defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))) || ((defined(__i386__) || defined(__x86_64__)) && defined(__SSE2__))
363
+ #if defined(_MSC_VER)
364
+ #include <intrin.h>
365
+ #endif
366
+ #include <immintrin.h>
367
+ #define DRMP3_HAVE_SSE 1
368
+ #define DRMP3_HAVE_SIMD 1
369
+ #define DRMP3_VSTORE _mm_storeu_ps
370
+ #define DRMP3_VLD _mm_loadu_ps
371
+ #define DRMP3_VSET _mm_set1_ps
372
+ #define DRMP3_VADD _mm_add_ps
373
+ #define DRMP3_VSUB _mm_sub_ps
374
+ #define DRMP3_VMUL _mm_mul_ps
375
+ #define DRMP3_VMAC(a, x, y) _mm_add_ps(a, _mm_mul_ps(x, y))
376
+ #define DRMP3_VMSB(a, x, y) _mm_sub_ps(a, _mm_mul_ps(x, y))
377
+ #define DRMP3_VMUL_S(x, s) _mm_mul_ps(x, _mm_set1_ps(s))
378
+ #define DRMP3_VREV(x) _mm_shuffle_ps(x, x, _MM_SHUFFLE(0, 1, 2, 3))
379
+ typedef __m128 drmp3_f4;
380
+ #if defined(_MSC_VER) || defined(DR_MP3_ONLY_SIMD)
381
+ #define drmp3_cpuid __cpuid
382
+ #else
383
+ static __inline__ __attribute__((always_inline)) void drmp3_cpuid(int CPUInfo[], const int InfoType)
384
+ {
385
+ #if defined(__PIC__)
386
+ __asm__ __volatile__(
387
+ #if defined(__x86_64__)
388
+ "push %%rbx\n"
389
+ "cpuid\n"
390
+ "xchgl %%ebx, %1\n"
391
+ "pop %%rbx\n"
392
+ #else
393
+ "xchgl %%ebx, %1\n"
394
+ "cpuid\n"
395
+ "xchgl %%ebx, %1\n"
396
+ #endif
397
+ : "=a" (CPUInfo[0]), "=r" (CPUInfo[1]), "=c" (CPUInfo[2]), "=d" (CPUInfo[3])
398
+ : "a" (InfoType));
399
+ #else
400
+ __asm__ __volatile__(
401
+ "cpuid"
402
+ : "=a" (CPUInfo[0]), "=b" (CPUInfo[1]), "=c" (CPUInfo[2]), "=d" (CPUInfo[3])
403
+ : "a" (InfoType));
404
+ #endif
405
+ }
406
+ #endif
407
+ static int drmp3_have_simd()
408
+ {
409
+ #ifdef DR_MP3_ONLY_SIMD
410
+ return 1;
411
+ #else
412
+ static int g_have_simd;
413
+ int CPUInfo[4];
414
+ #ifdef MINIMP3_TEST
415
+ static int g_counter;
416
+ if (g_counter++ > 100)
417
+ return 0;
418
+ #endif
419
+ if (g_have_simd)
420
+ goto end;
421
+ drmp3_cpuid(CPUInfo, 0);
422
+ if (CPUInfo[0] > 0)
423
+ {
424
+ drmp3_cpuid(CPUInfo, 1);
425
+ g_have_simd = (CPUInfo[3] & (1 << 26)) + 1; /* SSE2 */
426
+ return g_have_simd - 1;
427
+ }
428
+
429
+ end:
430
+ return g_have_simd - 1;
431
+ #endif
432
+ }
433
+ #elif defined(__ARM_NEON) || defined(__aarch64__)
434
+ #include <arm_neon.h>
435
+ #define DRMP3_HAVE_SIMD 1
436
+ #define DRMP3_VSTORE vst1q_f32
437
+ #define DRMP3_VLD vld1q_f32
438
+ #define DRMP3_VSET vmovq_n_f32
439
+ #define DRMP3_VADD vaddq_f32
440
+ #define DRMP3_VSUB vsubq_f32
441
+ #define DRMP3_VMUL vmulq_f32
442
+ #define DRMP3_VMAC(a, x, y) vmlaq_f32(a, x, y)
443
+ #define DRMP3_VMSB(a, x, y) vmlsq_f32(a, x, y)
444
+ #define DRMP3_VMUL_S(x, s) vmulq_f32(x, vmovq_n_f32(s))
445
+ #define DRMP3_VREV(x) vcombine_f32(vget_high_f32(vrev64q_f32(x)), vget_low_f32(vrev64q_f32(x)))
446
+ typedef float32x4_t drmp3_f4;
447
+ static int drmp3_have_simd()
448
+ { /* TODO: detect neon for !DR_MP3_ONLY_SIMD */
449
+ return 1;
450
+ }
451
+ #else
452
+ #define DRMP3_HAVE_SIMD 0
453
+ #ifdef DR_MP3_ONLY_SIMD
454
+ #error DR_MP3_ONLY_SIMD used, but SSE/NEON not enabled
455
+ #endif
456
+ #endif
457
+
458
+ #else
459
+
460
+ #define DRMP3_HAVE_SIMD 0
461
+
462
+ #endif
463
+
464
+ typedef struct
465
+ {
466
+ const drmp3_uint8 *buf;
467
+ int pos, limit;
468
+ } drmp3_bs;
469
+
470
+ typedef struct
471
+ {
472
+ float scf[3*64];
473
+ drmp3_uint8 total_bands, stereo_bands, bitalloc[64], scfcod[64];
474
+ } drmp3_L12_scale_info;
475
+
476
+ typedef struct
477
+ {
478
+ drmp3_uint8 tab_offset, code_tab_width, band_count;
479
+ } drmp3_L12_subband_alloc;
480
+
481
+ typedef struct
482
+ {
483
+ const drmp3_uint8 *sfbtab;
484
+ drmp3_uint16 part_23_length, big_values, scalefac_compress;
485
+ drmp3_uint8 global_gain, block_type, mixed_block_flag, n_long_sfb, n_short_sfb;
486
+ drmp3_uint8 table_select[3], region_count[3], subblock_gain[3];
487
+ drmp3_uint8 preflag, scalefac_scale, count1_table, scfsi;
488
+ } drmp3_L3_gr_info;
489
+
490
+ typedef struct
491
+ {
492
+ drmp3_bs bs;
493
+ drmp3_uint8 maindata[DRMP3_MAX_BITRESERVOIR_BYTES + DRMP3_MAX_L3_FRAME_PAYLOAD_BYTES];
494
+ drmp3_L3_gr_info gr_info[4];
495
+ float grbuf[2][576], scf[40], syn[18 + 15][2*32];
496
+ drmp3_uint8 ist_pos[2][39];
497
+ } drmp3dec_scratch;
498
+
499
+ static void drmp3_bs_init(drmp3_bs *bs, const drmp3_uint8 *data, int bytes)
500
+ {
501
+ bs->buf = data;
502
+ bs->pos = 0;
503
+ bs->limit = bytes*8;
504
+ }
505
+
506
+ static drmp3_uint32 drmp3_bs_get_bits(drmp3_bs *bs, int n)
507
+ {
508
+ drmp3_uint32 next, cache = 0, s = bs->pos & 7;
509
+ int shl = n + s;
510
+ const drmp3_uint8 *p = bs->buf + (bs->pos >> 3);
511
+ if ((bs->pos += n) > bs->limit)
512
+ return 0;
513
+ next = *p++ & (255 >> s);
514
+ while ((shl -= 8) > 0)
515
+ {
516
+ cache |= next << shl;
517
+ next = *p++;
518
+ }
519
+ return cache | (next >> -shl);
520
+ }
521
+
522
+ static int drmp3_hdr_valid(const drmp3_uint8 *h)
523
+ {
524
+ return h[0] == 0xff &&
525
+ ((h[1] & 0xF0) == 0xf0 || (h[1] & 0xFE) == 0xe2) &&
526
+ (DRMP3_HDR_GET_LAYER(h) != 0) &&
527
+ (DRMP3_HDR_GET_BITRATE(h) != 15) &&
528
+ (DRMP3_HDR_GET_SAMPLE_RATE(h) != 3);
529
+ }
530
+
531
+ static int drmp3_hdr_compare(const drmp3_uint8 *h1, const drmp3_uint8 *h2)
532
+ {
533
+ return drmp3_hdr_valid(h2) &&
534
+ ((h1[1] ^ h2[1]) & 0xFE) == 0 &&
535
+ ((h1[2] ^ h2[2]) & 0x0C) == 0 &&
536
+ !(DRMP3_HDR_IS_FREE_FORMAT(h1) ^ DRMP3_HDR_IS_FREE_FORMAT(h2));
537
+ }
538
+
539
+ static unsigned drmp3_hdr_bitrate_kbps(const drmp3_uint8 *h)
540
+ {
541
+ static const drmp3_uint8 halfrate[2][3][15] = {
542
+ { { 0,4,8,12,16,20,24,28,32,40,48,56,64,72,80 }, { 0,4,8,12,16,20,24,28,32,40,48,56,64,72,80 }, { 0,16,24,28,32,40,48,56,64,72,80,88,96,112,128 } },
543
+ { { 0,16,20,24,28,32,40,48,56,64,80,96,112,128,160 }, { 0,16,24,28,32,40,48,56,64,80,96,112,128,160,192 }, { 0,16,32,48,64,80,96,112,128,144,160,176,192,208,224 } },
544
+ };
545
+ return 2*halfrate[!!DRMP3_HDR_TEST_MPEG1(h)][DRMP3_HDR_GET_LAYER(h) - 1][DRMP3_HDR_GET_BITRATE(h)];
546
+ }
547
+
548
+ static unsigned drmp3_hdr_sample_rate_hz(const drmp3_uint8 *h)
549
+ {
550
+ static const unsigned g_hz[3] = { 44100, 48000, 32000 };
551
+ return g_hz[DRMP3_HDR_GET_SAMPLE_RATE(h)] >> (int)!DRMP3_HDR_TEST_MPEG1(h) >> (int)!DRMP3_HDR_TEST_NOT_MPEG25(h);
552
+ }
553
+
554
+ static unsigned drmp3_hdr_frame_samples(const drmp3_uint8 *h)
555
+ {
556
+ return DRMP3_HDR_IS_LAYER_1(h) ? 384 : (1152 >> (int)DRMP3_HDR_IS_FRAME_576(h));
557
+ }
558
+
559
+ static int drmp3_hdr_frame_bytes(const drmp3_uint8 *h, int free_format_size)
560
+ {
561
+ int frame_bytes = drmp3_hdr_frame_samples(h)*drmp3_hdr_bitrate_kbps(h)*125/drmp3_hdr_sample_rate_hz(h);
562
+ if (DRMP3_HDR_IS_LAYER_1(h))
563
+ {
564
+ frame_bytes &= ~3; /* slot align */
565
+ }
566
+ return frame_bytes ? frame_bytes : free_format_size;
567
+ }
568
+
569
+ static int drmp3_hdr_padding(const drmp3_uint8 *h)
570
+ {
571
+ return DRMP3_HDR_TEST_PADDING(h) ? (DRMP3_HDR_IS_LAYER_1(h) ? 4 : 1) : 0;
572
+ }
573
+
574
+ #ifndef DR_MP3_ONLY_MP3
575
+ static const drmp3_L12_subband_alloc *drmp3_L12_subband_alloc_table(const drmp3_uint8 *hdr, drmp3_L12_scale_info *sci)
576
+ {
577
+ const drmp3_L12_subband_alloc *alloc;
578
+ int mode = DRMP3_HDR_GET_STEREO_MODE(hdr);
579
+ int nbands, stereo_bands = (mode == DRMP3_MODE_MONO) ? 0 : (mode == DRMP3_MODE_JOINT_STEREO) ? (DRMP3_HDR_GET_STEREO_MODE_EXT(hdr) << 2) + 4 : 32;
580
+
581
+ if (DRMP3_HDR_IS_LAYER_1(hdr))
582
+ {
583
+ static const drmp3_L12_subband_alloc g_alloc_L1[] = { { 76, 4, 32 } };
584
+ alloc = g_alloc_L1;
585
+ nbands = 32;
586
+ } else if (!DRMP3_HDR_TEST_MPEG1(hdr))
587
+ {
588
+ static const drmp3_L12_subband_alloc g_alloc_L2M2[] = { { 60, 4, 4 }, { 44, 3, 7 }, { 44, 2, 19 } };
589
+ alloc = g_alloc_L2M2;
590
+ nbands = 30;
591
+ } else
592
+ {
593
+ static const drmp3_L12_subband_alloc g_alloc_L2M1[] = { { 0, 4, 3 }, { 16, 4, 8 }, { 32, 3, 12 }, { 40, 2, 7 } };
594
+ int sample_rate_idx = DRMP3_HDR_GET_SAMPLE_RATE(hdr);
595
+ unsigned kbps = drmp3_hdr_bitrate_kbps(hdr) >> (int)(mode != DRMP3_MODE_MONO);
596
+ if (!kbps) /* free-format */
597
+ {
598
+ kbps = 192;
599
+ }
600
+
601
+ alloc = g_alloc_L2M1;
602
+ nbands = 27;
603
+ if (kbps < 56)
604
+ {
605
+ static const drmp3_L12_subband_alloc g_alloc_L2M1_lowrate[] = { { 44, 4, 2 }, { 44, 3, 10 } };
606
+ alloc = g_alloc_L2M1_lowrate;
607
+ nbands = sample_rate_idx == 2 ? 12 : 8;
608
+ } else if (kbps >= 96 && sample_rate_idx != 1)
609
+ {
610
+ nbands = 30;
611
+ }
612
+ }
613
+
614
+ sci->total_bands = (drmp3_uint8)nbands;
615
+ sci->stereo_bands = (drmp3_uint8)DRMP3_MIN(stereo_bands, nbands);
616
+
617
+ return alloc;
618
+ }
619
+
620
+ static void drmp3_L12_read_scalefactors(drmp3_bs *bs, drmp3_uint8 *pba, drmp3_uint8 *scfcod, int bands, float *scf)
621
+ {
622
+ static const float g_deq_L12[18*3] = {
623
+ #define DRMP3_DQ(x) 9.53674316e-07f/x, 7.56931807e-07f/x, 6.00777173e-07f/x
624
+ DRMP3_DQ(3),DRMP3_DQ(7),DRMP3_DQ(15),DRMP3_DQ(31),DRMP3_DQ(63),DRMP3_DQ(127),DRMP3_DQ(255),DRMP3_DQ(511),DRMP3_DQ(1023),DRMP3_DQ(2047),DRMP3_DQ(4095),DRMP3_DQ(8191),DRMP3_DQ(16383),DRMP3_DQ(32767),DRMP3_DQ(65535),DRMP3_DQ(3),DRMP3_DQ(5),DRMP3_DQ(9)
625
+ };
626
+ int i, m;
627
+ for (i = 0; i < bands; i++)
628
+ {
629
+ float s = 0;
630
+ int ba = *pba++;
631
+ int mask = ba ? 4 + ((19 >> scfcod[i]) & 3) : 0;
632
+ for (m = 4; m; m >>= 1)
633
+ {
634
+ if (mask & m)
635
+ {
636
+ int b = drmp3_bs_get_bits(bs, 6);
637
+ s = g_deq_L12[ba*3 - 6 + b % 3]*(1 << 21 >> b/3);
638
+ }
639
+ *scf++ = s;
640
+ }
641
+ }
642
+ }
643
+
644
+ static void drmp3_L12_read_scale_info(const drmp3_uint8 *hdr, drmp3_bs *bs, drmp3_L12_scale_info *sci)
645
+ {
646
+ static const drmp3_uint8 g_bitalloc_code_tab[] = {
647
+ 0,17, 3, 4, 5,6,7, 8,9,10,11,12,13,14,15,16,
648
+ 0,17,18, 3,19,4,5, 6,7, 8, 9,10,11,12,13,16,
649
+ 0,17,18, 3,19,4,5,16,
650
+ 0,17,18,16,
651
+ 0,17,18,19, 4,5,6, 7,8, 9,10,11,12,13,14,15,
652
+ 0,17,18, 3,19,4,5, 6,7, 8, 9,10,11,12,13,14,
653
+ 0, 2, 3, 4, 5,6,7, 8,9,10,11,12,13,14,15,16
654
+ };
655
+ const drmp3_L12_subband_alloc *subband_alloc = drmp3_L12_subband_alloc_table(hdr, sci);
656
+
657
+ int i, k = 0, ba_bits = 0;
658
+ const drmp3_uint8 *ba_code_tab = g_bitalloc_code_tab;
659
+
660
+ for (i = 0; i < sci->total_bands; i++)
661
+ {
662
+ drmp3_uint8 ba;
663
+ if (i == k)
664
+ {
665
+ k += subband_alloc->band_count;
666
+ ba_bits = subband_alloc->code_tab_width;
667
+ ba_code_tab = g_bitalloc_code_tab + subband_alloc->tab_offset;
668
+ subband_alloc++;
669
+ }
670
+ ba = ba_code_tab[drmp3_bs_get_bits(bs, ba_bits)];
671
+ sci->bitalloc[2*i] = ba;
672
+ if (i < sci->stereo_bands)
673
+ {
674
+ ba = ba_code_tab[drmp3_bs_get_bits(bs, ba_bits)];
675
+ }
676
+ sci->bitalloc[2*i + 1] = sci->stereo_bands ? ba : 0;
677
+ }
678
+
679
+ for (i = 0; i < 2*sci->total_bands; i++)
680
+ {
681
+ sci->scfcod[i] = (drmp3_uint8)(sci->bitalloc[i] ? DRMP3_HDR_IS_LAYER_1(hdr) ? 2 : drmp3_bs_get_bits(bs, 2) : 6);
682
+ }
683
+
684
+ drmp3_L12_read_scalefactors(bs, sci->bitalloc, sci->scfcod, sci->total_bands*2, sci->scf);
685
+
686
+ for (i = sci->stereo_bands; i < sci->total_bands; i++)
687
+ {
688
+ sci->bitalloc[2*i + 1] = 0;
689
+ }
690
+ }
691
+
692
+ static int drmp3_L12_dequantize_granule(float *grbuf, drmp3_bs *bs, drmp3_L12_scale_info *sci, int group_size)
693
+ {
694
+ int i, j, k, choff = 576;
695
+ for (j = 0; j < 4; j++)
696
+ {
697
+ float *dst = grbuf + group_size*j;
698
+ for (i = 0; i < 2*sci->total_bands; i++)
699
+ {
700
+ int ba = sci->bitalloc[i];
701
+ if (ba != 0)
702
+ {
703
+ if (ba < 17)
704
+ {
705
+ int half = (1 << (ba - 1)) - 1;
706
+ for (k = 0; k < group_size; k++)
707
+ {
708
+ dst[k] = (float)((int)drmp3_bs_get_bits(bs, ba) - half);
709
+ }
710
+ } else
711
+ {
712
+ unsigned mod = (2 << (ba - 17)) + 1; /* 3, 5, 9 */
713
+ unsigned code = drmp3_bs_get_bits(bs, mod + 2 - (mod >> 3)); /* 5, 7, 10 */
714
+ for (k = 0; k < group_size; k++, code /= mod)
715
+ {
716
+ dst[k] = (float)((int)(code % mod - mod/2));
717
+ }
718
+ }
719
+ }
720
+ dst += choff;
721
+ choff = 18 - choff;
722
+ }
723
+ }
724
+ return group_size*4;
725
+ }
726
+
727
+ static void drmp3_L12_apply_scf_384(drmp3_L12_scale_info *sci, const float *scf, float *dst)
728
+ {
729
+ int i, k;
730
+ memcpy(dst + 576 + sci->stereo_bands*18, dst + sci->stereo_bands*18, (sci->total_bands - sci->stereo_bands)*18*sizeof(float));
731
+ for (i = 0; i < sci->total_bands; i++, dst += 18, scf += 6)
732
+ {
733
+ for (k = 0; k < 12; k++)
734
+ {
735
+ dst[k + 0] *= scf[0];
736
+ dst[k + 576] *= scf[3];
737
+ }
738
+ }
739
+ }
740
+ #endif
741
+
742
+ static int drmp3_L3_read_side_info(drmp3_bs *bs, drmp3_L3_gr_info *gr, const drmp3_uint8 *hdr)
743
+ {
744
+ static const drmp3_uint8 g_scf_long[8][23] = {
745
+ { 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 },
746
+ { 12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2,0 },
747
+ { 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 },
748
+ { 6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,54,62,70,76,36,0 },
749
+ { 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 },
750
+ { 4,4,4,4,4,4,6,6,8,8,10,12,16,20,24,28,34,42,50,54,76,158,0 },
751
+ { 4,4,4,4,4,4,6,6,6,8,10,12,16,18,22,28,34,40,46,54,54,192,0 },
752
+ { 4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102,26,0 }
753
+ };
754
+ static const drmp3_uint8 g_scf_short[8][40] = {
755
+ { 4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,30,30,30,40,40,40,18,18,18,0 },
756
+ { 8,8,8,8,8,8,8,8,8,12,12,12,16,16,16,20,20,20,24,24,24,28,28,28,36,36,36,2,2,2,2,2,2,2,2,2,26,26,26,0 },
757
+ { 4,4,4,4,4,4,4,4,4,6,6,6,6,6,6,8,8,8,10,10,10,14,14,14,18,18,18,26,26,26,32,32,32,42,42,42,18,18,18,0 },
758
+ { 4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,32,32,32,44,44,44,12,12,12,0 },
759
+ { 4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,30,30,30,40,40,40,18,18,18,0 },
760
+ { 4,4,4,4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,22,22,22,30,30,30,56,56,56,0 },
761
+ { 4,4,4,4,4,4,4,4,4,4,4,4,6,6,6,6,6,6,10,10,10,12,12,12,14,14,14,16,16,16,20,20,20,26,26,26,66,66,66,0 },
762
+ { 4,4,4,4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,12,12,12,16,16,16,20,20,20,26,26,26,34,34,34,42,42,42,12,12,12,0 }
763
+ };
764
+ static const drmp3_uint8 g_scf_mixed[8][40] = {
765
+ { 6,6,6,6,6,6,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,30,30,30,40,40,40,18,18,18,0 },
766
+ { 12,12,12,4,4,4,8,8,8,12,12,12,16,16,16,20,20,20,24,24,24,28,28,28,36,36,36,2,2,2,2,2,2,2,2,2,26,26,26,0 },
767
+ { 6,6,6,6,6,6,6,6,6,6,6,6,8,8,8,10,10,10,14,14,14,18,18,18,26,26,26,32,32,32,42,42,42,18,18,18,0 },
768
+ { 6,6,6,6,6,6,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,32,32,32,44,44,44,12,12,12,0 },
769
+ { 6,6,6,6,6,6,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,30,30,30,40,40,40,18,18,18,0 },
770
+ { 4,4,4,4,4,4,6,6,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,22,22,22,30,30,30,56,56,56,0 },
771
+ { 4,4,4,4,4,4,6,6,4,4,4,6,6,6,6,6,6,10,10,10,12,12,12,14,14,14,16,16,16,20,20,20,26,26,26,66,66,66,0 },
772
+ { 4,4,4,4,4,4,6,6,4,4,4,6,6,6,8,8,8,12,12,12,16,16,16,20,20,20,26,26,26,34,34,34,42,42,42,12,12,12,0 }
773
+ };
774
+
775
+ unsigned tables, scfsi = 0;
776
+ int main_data_begin, part_23_sum = 0;
777
+ int sr_idx = DRMP3_HDR_GET_MY_SAMPLE_RATE(hdr); sr_idx -= (sr_idx != 0);
778
+ int gr_count = DRMP3_HDR_IS_MONO(hdr) ? 1 : 2;
779
+
780
+ if (DRMP3_HDR_TEST_MPEG1(hdr))
781
+ {
782
+ gr_count *= 2;
783
+ main_data_begin = drmp3_bs_get_bits(bs, 9);
784
+ scfsi = drmp3_bs_get_bits(bs, 7 + gr_count);
785
+ } else
786
+ {
787
+ main_data_begin = drmp3_bs_get_bits(bs, 8 + gr_count) >> gr_count;
788
+ }
789
+
790
+ do
791
+ {
792
+ if (DRMP3_HDR_IS_MONO(hdr))
793
+ {
794
+ scfsi <<= 4;
795
+ }
796
+ gr->part_23_length = (drmp3_uint16)drmp3_bs_get_bits(bs, 12);
797
+ part_23_sum += gr->part_23_length;
798
+ gr->big_values = (drmp3_uint16)drmp3_bs_get_bits(bs, 9);
799
+ if (gr->big_values > 288)
800
+ {
801
+ return -1;
802
+ }
803
+ gr->global_gain = (drmp3_uint8)drmp3_bs_get_bits(bs, 8);
804
+ gr->scalefac_compress = (drmp3_uint16)drmp3_bs_get_bits(bs, DRMP3_HDR_TEST_MPEG1(hdr) ? 4 : 9);
805
+ gr->sfbtab = g_scf_long[sr_idx];
806
+ gr->n_long_sfb = 22;
807
+ gr->n_short_sfb = 0;
808
+ if (drmp3_bs_get_bits(bs, 1))
809
+ {
810
+ gr->block_type = (drmp3_uint8)drmp3_bs_get_bits(bs, 2);
811
+ if (!gr->block_type)
812
+ {
813
+ return -1;
814
+ }
815
+ gr->mixed_block_flag = (drmp3_uint8)drmp3_bs_get_bits(bs, 1);
816
+ gr->region_count[0] = 7;
817
+ gr->region_count[1] = 255;
818
+ if (gr->block_type == DRMP3_SHORT_BLOCK_TYPE)
819
+ {
820
+ scfsi &= 0x0F0F;
821
+ if (!gr->mixed_block_flag)
822
+ {
823
+ gr->region_count[0] = 8;
824
+ gr->sfbtab = g_scf_short[sr_idx];
825
+ gr->n_long_sfb = 0;
826
+ gr->n_short_sfb = 39;
827
+ } else
828
+ {
829
+ gr->sfbtab = g_scf_mixed[sr_idx];
830
+ gr->n_long_sfb = DRMP3_HDR_TEST_MPEG1(hdr) ? 8 : 6;
831
+ gr->n_short_sfb = 30;
832
+ }
833
+ }
834
+ tables = drmp3_bs_get_bits(bs, 10);
835
+ tables <<= 5;
836
+ gr->subblock_gain[0] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3);
837
+ gr->subblock_gain[1] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3);
838
+ gr->subblock_gain[2] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3);
839
+ } else
840
+ {
841
+ gr->block_type = 0;
842
+ gr->mixed_block_flag = 0;
843
+ tables = drmp3_bs_get_bits(bs, 15);
844
+ gr->region_count[0] = (drmp3_uint8)drmp3_bs_get_bits(bs, 4);
845
+ gr->region_count[1] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3);
846
+ gr->region_count[2] = 255;
847
+ }
848
+ gr->table_select[0] = (drmp3_uint8)(tables >> 10);
849
+ gr->table_select[1] = (drmp3_uint8)((tables >> 5) & 31);
850
+ gr->table_select[2] = (drmp3_uint8)((tables) & 31);
851
+ gr->preflag = (drmp3_uint8)(DRMP3_HDR_TEST_MPEG1(hdr) ? drmp3_bs_get_bits(bs, 1) : (gr->scalefac_compress >= 500));
852
+ gr->scalefac_scale = (drmp3_uint8)drmp3_bs_get_bits(bs, 1);
853
+ gr->count1_table = (drmp3_uint8)drmp3_bs_get_bits(bs, 1);
854
+ gr->scfsi = (drmp3_uint8)((scfsi >> 12) & 15);
855
+ scfsi <<= 4;
856
+ gr++;
857
+ } while(--gr_count);
858
+
859
+ if (part_23_sum + bs->pos > bs->limit + main_data_begin*8)
860
+ {
861
+ return -1;
862
+ }
863
+
864
+ return main_data_begin;
865
+ }
866
+
867
+ static void drmp3_L3_read_scalefactors(drmp3_uint8 *scf, drmp3_uint8 *ist_pos, const drmp3_uint8 *scf_size, const drmp3_uint8 *scf_count, drmp3_bs *bitbuf, int scfsi)
868
+ {
869
+ int i, k;
870
+ for (i = 0; i < 4 && scf_count[i]; i++, scfsi *= 2)
871
+ {
872
+ int cnt = scf_count[i];
873
+ if (scfsi & 8)
874
+ {
875
+ memcpy(scf, ist_pos, cnt);
876
+ } else
877
+ {
878
+ int bits = scf_size[i];
879
+ if (!bits)
880
+ {
881
+ memset(scf, 0, cnt);
882
+ memset(ist_pos, 0, cnt);
883
+ } else
884
+ {
885
+ int max_scf = (scfsi < 0) ? (1 << bits) - 1 : -1;
886
+ for (k = 0; k < cnt; k++)
887
+ {
888
+ int s = drmp3_bs_get_bits(bitbuf, bits);
889
+ ist_pos[k] = (drmp3_uint8)(s == max_scf ? -1 : s);
890
+ scf[k] = (drmp3_uint8)s;
891
+ }
892
+ }
893
+ }
894
+ ist_pos += cnt;
895
+ scf += cnt;
896
+ }
897
+ scf[0] = scf[1] = scf[2] = 0;
898
+ }
899
+
900
+ static float drmp3_L3_ldexp_q2(float y, int exp_q2)
901
+ {
902
+ static const float g_expfrac[4] = { 9.31322575e-10f,7.83145814e-10f,6.58544508e-10f,5.53767716e-10f };
903
+ int e;
904
+ do
905
+ {
906
+ e = DRMP3_MIN(30*4, exp_q2);
907
+ y *= g_expfrac[e & 3]*(1 << 30 >> (e >> 2));
908
+ } while ((exp_q2 -= e) > 0);
909
+ return y;
910
+ }
911
+
912
+ static void drmp3_L3_decode_scalefactors(const drmp3_uint8 *hdr, drmp3_uint8 *ist_pos, drmp3_bs *bs, const drmp3_L3_gr_info *gr, float *scf, int ch)
913
+ {
914
+ static const drmp3_uint8 g_scf_partitions[3][28] = {
915
+ { 6,5,5, 5,6,5,5,5,6,5, 7,3,11,10,0,0, 7, 7, 7,0, 6, 6,6,3, 8, 8,5,0 },
916
+ { 8,9,6,12,6,9,9,9,6,9,12,6,15,18,0,0, 6,15,12,0, 6,12,9,6, 6,18,9,0 },
917
+ { 9,9,6,12,9,9,9,9,9,9,12,6,18,18,0,0,12,12,12,0,12, 9,9,6,15,12,9,0 }
918
+ };
919
+ const drmp3_uint8 *scf_partition = g_scf_partitions[!!gr->n_short_sfb + !gr->n_long_sfb];
920
+ drmp3_uint8 scf_size[4], iscf[40];
921
+ int i, scf_shift = gr->scalefac_scale + 1, gain_exp, scfsi = gr->scfsi;
922
+ float gain;
923
+
924
+ if (DRMP3_HDR_TEST_MPEG1(hdr))
925
+ {
926
+ static const drmp3_uint8 g_scfc_decode[16] = { 0,1,2,3, 12,5,6,7, 9,10,11,13, 14,15,18,19 };
927
+ int part = g_scfc_decode[gr->scalefac_compress];
928
+ scf_size[1] = scf_size[0] = (drmp3_uint8)(part >> 2);
929
+ scf_size[3] = scf_size[2] = (drmp3_uint8)(part & 3);
930
+ } else
931
+ {
932
+ static const drmp3_uint8 g_mod[6*4] = { 5,5,4,4,5,5,4,1,4,3,1,1,5,6,6,1,4,4,4,1,4,3,1,1 };
933
+ int k, modprod, sfc, ist = DRMP3_HDR_TEST_I_STEREO(hdr) && ch;
934
+ sfc = gr->scalefac_compress >> ist;
935
+ for (k = ist*3*4; sfc >= 0; sfc -= modprod, k += 4)
936
+ {
937
+ for (modprod = 1, i = 3; i >= 0; i--)
938
+ {
939
+ scf_size[i] = (drmp3_uint8)(sfc / modprod % g_mod[k + i]);
940
+ modprod *= g_mod[k + i];
941
+ }
942
+ }
943
+ scf_partition += k;
944
+ scfsi = -16;
945
+ }
946
+ drmp3_L3_read_scalefactors(iscf, ist_pos, scf_size, scf_partition, bs, scfsi);
947
+
948
+ if (gr->n_short_sfb)
949
+ {
950
+ int sh = 3 - scf_shift;
951
+ for (i = 0; i < gr->n_short_sfb; i += 3)
952
+ {
953
+ iscf[gr->n_long_sfb + i + 0] += gr->subblock_gain[0] << sh;
954
+ iscf[gr->n_long_sfb + i + 1] += gr->subblock_gain[1] << sh;
955
+ iscf[gr->n_long_sfb + i + 2] += gr->subblock_gain[2] << sh;
956
+ }
957
+ } else if (gr->preflag)
958
+ {
959
+ static const drmp3_uint8 g_preamp[10] = { 1,1,1,1,2,2,3,3,3,2 };
960
+ for (i = 0; i < 10; i++)
961
+ {
962
+ iscf[11 + i] += g_preamp[i];
963
+ }
964
+ }
965
+
966
+ gain_exp = gr->global_gain + DRMP3_BITS_DEQUANTIZER_OUT*4 - 210 - (DRMP3_HDR_IS_MS_STEREO(hdr) ? 2 : 0);
967
+ gain = drmp3_L3_ldexp_q2(1 << (DRMP3_MAX_SCFI/4), DRMP3_MAX_SCFI - gain_exp);
968
+ for (i = 0; i < (int)(gr->n_long_sfb + gr->n_short_sfb); i++)
969
+ {
970
+ scf[i] = drmp3_L3_ldexp_q2(gain, iscf[i] << scf_shift);
971
+ }
972
+ }
973
+
974
+ static const float g_drmp3_pow43[129 + 16] = {
975
+ 0,-1,-2.519842f,-4.326749f,-6.349604f,-8.549880f,-10.902724f,-13.390518f,-16.000000f,-18.720754f,-21.544347f,-24.463781f,-27.473142f,-30.567351f,-33.741992f,-36.993181f,
976
+ 0,1,2.519842f,4.326749f,6.349604f,8.549880f,10.902724f,13.390518f,16.000000f,18.720754f,21.544347f,24.463781f,27.473142f,30.567351f,33.741992f,36.993181f,40.317474f,43.711787f,47.173345f,50.699631f,54.288352f,57.937408f,61.644865f,65.408941f,69.227979f,73.100443f,77.024898f,81.000000f,85.024491f,89.097188f,93.216975f,97.382800f,101.593667f,105.848633f,110.146801f,114.487321f,118.869381f,123.292209f,127.755065f,132.257246f,136.798076f,141.376907f,145.993119f,150.646117f,155.335327f,160.060199f,164.820202f,169.614826f,174.443577f,179.305980f,184.201575f,189.129918f,194.090580f,199.083145f,204.107210f,209.162385f,214.248292f,219.364564f,224.510845f,229.686789f,234.892058f,240.126328f,245.389280f,250.680604f,256.000000f,261.347174f,266.721841f,272.123723f,277.552547f,283.008049f,288.489971f,293.998060f,299.532071f,305.091761f,310.676898f,316.287249f,321.922592f,327.582707f,333.267377f,338.976394f,344.709550f,350.466646f,356.247482f,362.051866f,367.879608f,373.730522f,379.604427f,385.501143f,391.420496f,397.362314f,403.326427f,409.312672f,415.320884f,421.350905f,427.402579f,433.475750f,439.570269f,445.685987f,451.822757f,457.980436f,464.158883f,470.357960f,476.577530f,482.817459f,489.077615f,495.357868f,501.658090f,507.978156f,514.317941f,520.677324f,527.056184f,533.454404f,539.871867f,546.308458f,552.764065f,559.238575f,565.731879f,572.243870f,578.774440f,585.323483f,591.890898f,598.476581f,605.080431f,611.702349f,618.342238f,625.000000f,631.675540f,638.368763f,645.079578f
977
+ };
978
+
979
+ static float drmp3_L3_pow_43(int x)
980
+ {
981
+ float frac;
982
+ int sign, mult = 256;
983
+
984
+ if (x < 129)
985
+ {
986
+ return g_drmp3_pow43[16 + x];
987
+ }
988
+
989
+ if (x < 1024)
990
+ {
991
+ mult = 16;
992
+ x <<= 3;
993
+ }
994
+
995
+ sign = 2*x & 64;
996
+ frac = (float)((x & 63) - sign) / ((x & ~63) + sign);
997
+ return g_drmp3_pow43[16 + ((x + sign) >> 6)]*(1.f + frac*((4.f/3) + frac*(2.f/9)))*mult;
998
+ }
999
+
1000
+ static void drmp3_L3_huffman(float *dst, drmp3_bs *bs, const drmp3_L3_gr_info *gr_info, const float *scf, int layer3gr_limit)
1001
+ {
1002
+ static const drmp3_int16 tabs[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1003
+ 785,785,785,785,784,784,784,784,513,513,513,513,513,513,513,513,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,
1004
+ -255,1313,1298,1282,785,785,785,785,784,784,784,784,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,290,288,
1005
+ -255,1313,1298,1282,769,769,769,769,529,529,529,529,529,529,529,529,528,528,528,528,528,528,528,528,512,512,512,512,512,512,512,512,290,288,
1006
+ -253,-318,-351,-367,785,785,785,785,784,784,784,784,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,819,818,547,547,275,275,275,275,561,560,515,546,289,274,288,258,
1007
+ -254,-287,1329,1299,1314,1312,1057,1057,1042,1042,1026,1026,784,784,784,784,529,529,529,529,529,529,529,529,769,769,769,769,768,768,768,768,563,560,306,306,291,259,
1008
+ -252,-413,-477,-542,1298,-575,1041,1041,784,784,784,784,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,-383,-399,1107,1092,1106,1061,849,849,789,789,1104,1091,773,773,1076,1075,341,340,325,309,834,804,577,577,532,532,516,516,832,818,803,816,561,561,531,531,515,546,289,289,288,258,
1009
+ -252,-429,-493,-559,1057,1057,1042,1042,529,529,529,529,529,529,529,529,784,784,784,784,769,769,769,769,512,512,512,512,512,512,512,512,-382,1077,-415,1106,1061,1104,849,849,789,789,1091,1076,1029,1075,834,834,597,581,340,340,339,324,804,833,532,532,832,772,818,803,817,787,816,771,290,290,290,290,288,258,
1010
+ -253,-349,-414,-447,-463,1329,1299,-479,1314,1312,1057,1057,1042,1042,1026,1026,785,785,785,785,784,784,784,784,769,769,769,769,768,768,768,768,-319,851,821,-335,836,850,805,849,341,340,325,336,533,533,579,579,564,564,773,832,578,548,563,516,321,276,306,291,304,259,
1011
+ -251,-572,-733,-830,-863,-879,1041,1041,784,784,784,784,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,-511,-527,-543,1396,1351,1381,1366,1395,1335,1380,-559,1334,1138,1138,1063,1063,1350,1392,1031,1031,1062,1062,1364,1363,1120,1120,1333,1348,881,881,881,881,375,374,359,373,343,358,341,325,791,791,1123,1122,-703,1105,1045,-719,865,865,790,790,774,774,1104,1029,338,293,323,308,-799,-815,833,788,772,818,803,816,322,292,307,320,561,531,515,546,289,274,288,258,
1012
+ -251,-525,-605,-685,-765,-831,-846,1298,1057,1057,1312,1282,785,785,785,785,784,784,784,784,769,769,769,769,512,512,512,512,512,512,512,512,1399,1398,1383,1367,1382,1396,1351,-511,1381,1366,1139,1139,1079,1079,1124,1124,1364,1349,1363,1333,882,882,882,882,807,807,807,807,1094,1094,1136,1136,373,341,535,535,881,775,867,822,774,-591,324,338,-671,849,550,550,866,864,609,609,293,336,534,534,789,835,773,-751,834,804,308,307,833,788,832,772,562,562,547,547,305,275,560,515,290,290,
1013
+ -252,-397,-477,-557,-622,-653,-719,-735,-750,1329,1299,1314,1057,1057,1042,1042,1312,1282,1024,1024,785,785,785,785,784,784,784,784,769,769,769,769,-383,1127,1141,1111,1126,1140,1095,1110,869,869,883,883,1079,1109,882,882,375,374,807,868,838,881,791,-463,867,822,368,263,852,837,836,-543,610,610,550,550,352,336,534,534,865,774,851,821,850,805,593,533,579,564,773,832,578,578,548,548,577,577,307,276,306,291,516,560,259,259,
1014
+ -250,-2107,-2507,-2764,-2909,-2974,-3007,-3023,1041,1041,1040,1040,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,-767,-1052,-1213,-1277,-1358,-1405,-1469,-1535,-1550,-1582,-1614,-1647,-1662,-1694,-1726,-1759,-1774,-1807,-1822,-1854,-1886,1565,-1919,-1935,-1951,-1967,1731,1730,1580,1717,-1983,1729,1564,-1999,1548,-2015,-2031,1715,1595,-2047,1714,-2063,1610,-2079,1609,-2095,1323,1323,1457,1457,1307,1307,1712,1547,1641,1700,1699,1594,1685,1625,1442,1442,1322,1322,-780,-973,-910,1279,1278,1277,1262,1276,1261,1275,1215,1260,1229,-959,974,974,989,989,-943,735,478,478,495,463,506,414,-1039,1003,958,1017,927,942,987,957,431,476,1272,1167,1228,-1183,1256,-1199,895,895,941,941,1242,1227,1212,1135,1014,1014,490,489,503,487,910,1013,985,925,863,894,970,955,1012,847,-1343,831,755,755,984,909,428,366,754,559,-1391,752,486,457,924,997,698,698,983,893,740,740,908,877,739,739,667,667,953,938,497,287,271,271,683,606,590,712,726,574,302,302,738,736,481,286,526,725,605,711,636,724,696,651,589,681,666,710,364,467,573,695,466,466,301,465,379,379,709,604,665,679,316,316,634,633,436,436,464,269,424,394,452,332,438,363,347,408,393,448,331,422,362,407,392,421,346,406,391,376,375,359,1441,1306,-2367,1290,-2383,1337,-2399,-2415,1426,1321,-2431,1411,1336,-2447,-2463,-2479,1169,1169,1049,1049,1424,1289,1412,1352,1319,-2495,1154,1154,1064,1064,1153,1153,416,390,360,404,403,389,344,374,373,343,358,372,327,357,342,311,356,326,1395,1394,1137,1137,1047,1047,1365,1392,1287,1379,1334,1364,1349,1378,1318,1363,792,792,792,792,1152,1152,1032,1032,1121,1121,1046,1046,1120,1120,1030,1030,-2895,1106,1061,1104,849,849,789,789,1091,1076,1029,1090,1060,1075,833,833,309,324,532,532,832,772,818,803,561,561,531,560,515,546,289,274,288,258,
1015
+ -250,-1179,-1579,-1836,-1996,-2124,-2253,-2333,-2413,-2477,-2542,-2574,-2607,-2622,-2655,1314,1313,1298,1312,1282,785,785,785,785,1040,1040,1025,1025,768,768,768,768,-766,-798,-830,-862,-895,-911,-927,-943,-959,-975,-991,-1007,-1023,-1039,-1055,-1070,1724,1647,-1103,-1119,1631,1767,1662,1738,1708,1723,-1135,1780,1615,1779,1599,1677,1646,1778,1583,-1151,1777,1567,1737,1692,1765,1722,1707,1630,1751,1661,1764,1614,1736,1676,1763,1750,1645,1598,1721,1691,1762,1706,1582,1761,1566,-1167,1749,1629,767,766,751,765,494,494,735,764,719,749,734,763,447,447,748,718,477,506,431,491,446,476,461,505,415,430,475,445,504,399,460,489,414,503,383,474,429,459,502,502,746,752,488,398,501,473,413,472,486,271,480,270,-1439,-1455,1357,-1471,-1487,-1503,1341,1325,-1519,1489,1463,1403,1309,-1535,1372,1448,1418,1476,1356,1462,1387,-1551,1475,1340,1447,1402,1386,-1567,1068,1068,1474,1461,455,380,468,440,395,425,410,454,364,467,466,464,453,269,409,448,268,432,1371,1473,1432,1417,1308,1460,1355,1446,1459,1431,1083,1083,1401,1416,1458,1445,1067,1067,1370,1457,1051,1051,1291,1430,1385,1444,1354,1415,1400,1443,1082,1082,1173,1113,1186,1066,1185,1050,-1967,1158,1128,1172,1097,1171,1081,-1983,1157,1112,416,266,375,400,1170,1142,1127,1065,793,793,1169,1033,1156,1096,1141,1111,1155,1080,1126,1140,898,898,808,808,897,897,792,792,1095,1152,1032,1125,1110,1139,1079,1124,882,807,838,881,853,791,-2319,867,368,263,822,852,837,866,806,865,-2399,851,352,262,534,534,821,836,594,594,549,549,593,593,533,533,848,773,579,579,564,578,548,563,276,276,577,576,306,291,516,560,305,305,275,259,
1016
+ -251,-892,-2058,-2620,-2828,-2957,-3023,-3039,1041,1041,1040,1040,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,-511,-527,-543,-559,1530,-575,-591,1528,1527,1407,1526,1391,1023,1023,1023,1023,1525,1375,1268,1268,1103,1103,1087,1087,1039,1039,1523,-604,815,815,815,815,510,495,509,479,508,463,507,447,431,505,415,399,-734,-782,1262,-815,1259,1244,-831,1258,1228,-847,-863,1196,-879,1253,987,987,748,-767,493,493,462,477,414,414,686,669,478,446,461,445,474,429,487,458,412,471,1266,1264,1009,1009,799,799,-1019,-1276,-1452,-1581,-1677,-1757,-1821,-1886,-1933,-1997,1257,1257,1483,1468,1512,1422,1497,1406,1467,1496,1421,1510,1134,1134,1225,1225,1466,1451,1374,1405,1252,1252,1358,1480,1164,1164,1251,1251,1238,1238,1389,1465,-1407,1054,1101,-1423,1207,-1439,830,830,1248,1038,1237,1117,1223,1148,1236,1208,411,426,395,410,379,269,1193,1222,1132,1235,1221,1116,976,976,1192,1162,1177,1220,1131,1191,963,963,-1647,961,780,-1663,558,558,994,993,437,408,393,407,829,978,813,797,947,-1743,721,721,377,392,844,950,828,890,706,706,812,859,796,960,948,843,934,874,571,571,-1919,690,555,689,421,346,539,539,944,779,918,873,932,842,903,888,570,570,931,917,674,674,-2575,1562,-2591,1609,-2607,1654,1322,1322,1441,1441,1696,1546,1683,1593,1669,1624,1426,1426,1321,1321,1639,1680,1425,1425,1305,1305,1545,1668,1608,1623,1667,1592,1638,1666,1320,1320,1652,1607,1409,1409,1304,1304,1288,1288,1664,1637,1395,1395,1335,1335,1622,1636,1394,1394,1319,1319,1606,1621,1392,1392,1137,1137,1137,1137,345,390,360,375,404,373,1047,-2751,-2767,-2783,1062,1121,1046,-2799,1077,-2815,1106,1061,789,789,1105,1104,263,355,310,340,325,354,352,262,339,324,1091,1076,1029,1090,1060,1075,833,833,788,788,1088,1028,818,818,803,803,561,561,531,531,816,771,546,546,289,274,288,258,
1017
+ -253,-317,-381,-446,-478,-509,1279,1279,-811,-1179,-1451,-1756,-1900,-2028,-2189,-2253,-2333,-2414,-2445,-2511,-2526,1313,1298,-2559,1041,1041,1040,1040,1025,1025,1024,1024,1022,1007,1021,991,1020,975,1019,959,687,687,1018,1017,671,671,655,655,1016,1015,639,639,758,758,623,623,757,607,756,591,755,575,754,559,543,543,1009,783,-575,-621,-685,-749,496,-590,750,749,734,748,974,989,1003,958,988,973,1002,942,987,957,972,1001,926,986,941,971,956,1000,910,985,925,999,894,970,-1071,-1087,-1102,1390,-1135,1436,1509,1451,1374,-1151,1405,1358,1480,1420,-1167,1507,1494,1389,1342,1465,1435,1450,1326,1505,1310,1493,1373,1479,1404,1492,1464,1419,428,443,472,397,736,526,464,464,486,457,442,471,484,482,1357,1449,1434,1478,1388,1491,1341,1490,1325,1489,1463,1403,1309,1477,1372,1448,1418,1433,1476,1356,1462,1387,-1439,1475,1340,1447,1402,1474,1324,1461,1371,1473,269,448,1432,1417,1308,1460,-1711,1459,-1727,1441,1099,1099,1446,1386,1431,1401,-1743,1289,1083,1083,1160,1160,1458,1445,1067,1067,1370,1457,1307,1430,1129,1129,1098,1098,268,432,267,416,266,400,-1887,1144,1187,1082,1173,1113,1186,1066,1050,1158,1128,1143,1172,1097,1171,1081,420,391,1157,1112,1170,1142,1127,1065,1169,1049,1156,1096,1141,1111,1155,1080,1126,1154,1064,1153,1140,1095,1048,-2159,1125,1110,1137,-2175,823,823,1139,1138,807,807,384,264,368,263,868,838,853,791,867,822,852,837,866,806,865,790,-2319,851,821,836,352,262,850,805,849,-2399,533,533,835,820,336,261,578,548,563,577,532,532,832,772,562,562,547,547,305,275,560,515,290,290,288,258 };
1018
+ static const drmp3_uint8 tab32[] = { 130,162,193,209,44,28,76,140,9,9,9,9,9,9,9,9,190,254,222,238,126,94,157,157,109,61,173,205};
1019
+ static const drmp3_uint8 tab33[] = { 252,236,220,204,188,172,156,140,124,108,92,76,60,44,28,12 };
1020
+ static const drmp3_int16 tabindex[2*16] = { 0,32,64,98,0,132,180,218,292,364,426,538,648,746,0,1126,1460,1460,1460,1460,1460,1460,1460,1460,1842,1842,1842,1842,1842,1842,1842,1842 };
1021
+ static const drmp3_uint8 g_linbits[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4,6,8,10,13,4,5,6,7,8,9,11,13 };
1022
+
1023
+ #define DRMP3_PEEK_BITS(n) (bs_cache >> (32 - n))
1024
+ #define DRMP3_FLUSH_BITS(n) { bs_cache <<= (n); bs_sh += (n); }
1025
+ #define DRMP3_CHECK_BITS while (bs_sh >= 0) { bs_cache |= (drmp3_uint32)*bs_next_ptr++ << bs_sh; bs_sh -= 8; }
1026
+ #define DRMP3_BSPOS ((bs_next_ptr - bs->buf)*8 - 24 + bs_sh)
1027
+
1028
+ float one = 0.0f;
1029
+ int ireg = 0, big_val_cnt = gr_info->big_values;
1030
+ const drmp3_uint8 *sfb = gr_info->sfbtab;
1031
+ const drmp3_uint8 *bs_next_ptr = bs->buf + bs->pos/8;
1032
+ drmp3_uint32 bs_cache = (((bs_next_ptr[0]*256u + bs_next_ptr[1])*256u + bs_next_ptr[2])*256u + bs_next_ptr[3]) << (bs->pos & 7);
1033
+ int pairs_to_decode, np, bs_sh = (bs->pos & 7) - 8;
1034
+ bs_next_ptr += 4;
1035
+
1036
+ while (big_val_cnt > 0)
1037
+ {
1038
+ int tab_num = gr_info->table_select[ireg];
1039
+ int sfb_cnt = gr_info->region_count[ireg++];
1040
+ const short *codebook = tabs + tabindex[tab_num];
1041
+ int linbits = g_linbits[tab_num];
1042
+ do
1043
+ {
1044
+ np = *sfb++ / 2;
1045
+ pairs_to_decode = DRMP3_MIN(big_val_cnt, np);
1046
+ one = *scf++;
1047
+ do
1048
+ {
1049
+ int j, w = 5;
1050
+ int leaf = codebook[DRMP3_PEEK_BITS(w)];
1051
+ while (leaf < 0)
1052
+ {
1053
+ DRMP3_FLUSH_BITS(w);
1054
+ w = leaf & 7;
1055
+ leaf = codebook[DRMP3_PEEK_BITS(w) - (leaf >> 3)];
1056
+ }
1057
+ DRMP3_FLUSH_BITS(leaf >> 8);
1058
+
1059
+ for (j = 0; j < 2; j++, dst++, leaf >>= 4)
1060
+ {
1061
+ int lsb = leaf & 0x0F;
1062
+ if (lsb == 15 && linbits)
1063
+ {
1064
+ lsb += DRMP3_PEEK_BITS(linbits);
1065
+ DRMP3_FLUSH_BITS(linbits);
1066
+ DRMP3_CHECK_BITS;
1067
+ *dst = one*drmp3_L3_pow_43(lsb)*((int32_t)bs_cache < 0 ? -1: 1);
1068
+ } else
1069
+ {
1070
+ *dst = g_drmp3_pow43[16 + lsb - 16*(bs_cache >> 31)]*one;
1071
+ }
1072
+ DRMP3_FLUSH_BITS(lsb ? 1 : 0);
1073
+ }
1074
+ DRMP3_CHECK_BITS;
1075
+ } while (--pairs_to_decode);
1076
+ } while ((big_val_cnt -= np) > 0 && --sfb_cnt >= 0);
1077
+ }
1078
+
1079
+ for (np = 1 - big_val_cnt;; dst += 4)
1080
+ {
1081
+ const drmp3_uint8 *codebook_count1 = (gr_info->count1_table) ? tab33 : tab32;
1082
+ int leaf = codebook_count1[DRMP3_PEEK_BITS(4)];
1083
+ if (!(leaf & 8))
1084
+ {
1085
+ leaf = codebook_count1[(leaf >> 3) + (bs_cache << 4 >> (32 - (leaf & 3)))];
1086
+ }
1087
+ DRMP3_FLUSH_BITS(leaf & 7);
1088
+ if (DRMP3_BSPOS > layer3gr_limit)
1089
+ {
1090
+ break;
1091
+ }
1092
+ #define DRMP3_RELOAD_SCALEFACTOR if (!--np) { np = *sfb++/2; if (!np) break; one = *scf++; }
1093
+ #define DRMP3_DEQ_COUNT1(s) if (leaf & (128 >> s)) { dst[s] = ((drmp3_int32)bs_cache < 0) ? -one : one; DRMP3_FLUSH_BITS(1) }
1094
+ DRMP3_RELOAD_SCALEFACTOR;
1095
+ DRMP3_DEQ_COUNT1(0);
1096
+ DRMP3_DEQ_COUNT1(1);
1097
+ DRMP3_RELOAD_SCALEFACTOR;
1098
+ DRMP3_DEQ_COUNT1(2);
1099
+ DRMP3_DEQ_COUNT1(3);
1100
+ DRMP3_CHECK_BITS;
1101
+ }
1102
+
1103
+ bs->pos = layer3gr_limit;
1104
+ }
1105
+
1106
+ static void drmp3_L3_midside_stereo(float *left, int n)
1107
+ {
1108
+ int i = 0;
1109
+ float *right = left + 576;
1110
+ #if DRMP3_HAVE_SIMD
1111
+ if (drmp3_have_simd()) for (; i < n - 3; i += 4)
1112
+ {
1113
+ drmp3_f4 vl = DRMP3_VLD(left + i);
1114
+ drmp3_f4 vr = DRMP3_VLD(right + i);
1115
+ DRMP3_VSTORE(left + i, DRMP3_VADD(vl, vr));
1116
+ DRMP3_VSTORE(right + i, DRMP3_VSUB(vl, vr));
1117
+ }
1118
+ #endif
1119
+ for (; i < n; i++)
1120
+ {
1121
+ float a = left[i];
1122
+ float b = right[i];
1123
+ left[i] = a + b;
1124
+ right[i] = a - b;
1125
+ }
1126
+ }
1127
+
1128
+ static void drmp3_L3_intensity_stereo_band(float *left, int n, float kl, float kr)
1129
+ {
1130
+ int i;
1131
+ for (i = 0; i < n; i++)
1132
+ {
1133
+ left[i + 576] = left[i]*kr;
1134
+ left[i] = left[i]*kl;
1135
+ }
1136
+ }
1137
+
1138
+ static void drmp3_L3_stereo_top_band(const float *right, const drmp3_uint8 *sfb, int nbands, int max_band[3])
1139
+ {
1140
+ int i, k;
1141
+
1142
+ max_band[0] = max_band[1] = max_band[2] = -1;
1143
+
1144
+ for (i = 0; i < nbands; i++)
1145
+ {
1146
+ for (k = 0; k < sfb[i]; k += 2)
1147
+ {
1148
+ if (right[k] != 0 || right[k + 1] != 0)
1149
+ {
1150
+ max_band[i % 3] = i;
1151
+ break;
1152
+ }
1153
+ }
1154
+ right += sfb[i];
1155
+ }
1156
+ }
1157
+
1158
+ static void drmp3_L3_stereo_process(float *left, const drmp3_uint8 *ist_pos, const drmp3_uint8 *sfb, const drmp3_uint8 *hdr, int max_band[3], int mpeg2_sh)
1159
+ {
1160
+ static const float g_pan[7*2] = { 0,1,0.21132487f,0.78867513f,0.36602540f,0.63397460f,0.5f,0.5f,0.63397460f,0.36602540f,0.78867513f,0.21132487f,1,0 };
1161
+ unsigned i, max_pos = DRMP3_HDR_TEST_MPEG1(hdr) ? 7 : 64;
1162
+
1163
+ for (i = 0; sfb[i]; i++)
1164
+ {
1165
+ unsigned ipos = ist_pos[i];
1166
+ if ((int)i > max_band[i % 3] && ipos < max_pos)
1167
+ {
1168
+ float kl, kr, s = DRMP3_HDR_TEST_MS_STEREO(hdr) ? 1.41421356f : 1;
1169
+ if (DRMP3_HDR_TEST_MPEG1(hdr))
1170
+ {
1171
+ kl = g_pan[2*ipos];
1172
+ kr = g_pan[2*ipos + 1];
1173
+ } else
1174
+ {
1175
+ kl = 1;
1176
+ kr = drmp3_L3_ldexp_q2(1, (ipos + 1) >> 1 << mpeg2_sh);
1177
+ if (ipos & 1)
1178
+ {
1179
+ kl = kr;
1180
+ kr = 1;
1181
+ }
1182
+ }
1183
+ drmp3_L3_intensity_stereo_band(left, sfb[i], kl*s, kr*s);
1184
+ } else if (DRMP3_HDR_TEST_MS_STEREO(hdr))
1185
+ {
1186
+ drmp3_L3_midside_stereo(left, sfb[i]);
1187
+ }
1188
+ left += sfb[i];
1189
+ }
1190
+ }
1191
+
1192
+ static void drmp3_L3_intensity_stereo(float *left, drmp3_uint8 *ist_pos, const drmp3_L3_gr_info *gr, const drmp3_uint8 *hdr)
1193
+ {
1194
+ int max_band[3], n_sfb = gr->n_long_sfb + gr->n_short_sfb;
1195
+ int i, max_blocks = gr->n_short_sfb ? 3 : 1;
1196
+
1197
+ drmp3_L3_stereo_top_band(left + 576, gr->sfbtab, n_sfb, max_band);
1198
+ if (gr->n_long_sfb)
1199
+ {
1200
+ max_band[0] = max_band[1] = max_band[2] = DRMP3_MAX(DRMP3_MAX(max_band[0], max_band[1]), max_band[2]);
1201
+ }
1202
+ for (i = 0; i < max_blocks; i++)
1203
+ {
1204
+ int default_pos = DRMP3_HDR_TEST_MPEG1(hdr) ? 3 : 0;
1205
+ int itop = n_sfb - max_blocks + i;
1206
+ int prev = itop - max_blocks;
1207
+ ist_pos[itop] = (drmp3_uint8)(max_band[i] >= prev ? default_pos : ist_pos[prev]);
1208
+ }
1209
+ drmp3_L3_stereo_process(left, ist_pos, gr->sfbtab, hdr, max_band, gr[1].scalefac_compress & 1);
1210
+ }
1211
+
1212
+ static void drmp3_L3_reorder(float *grbuf, float *scratch, const drmp3_uint8 *sfb)
1213
+ {
1214
+ int i, len;
1215
+ float *src = grbuf, *dst = scratch;
1216
+
1217
+ for (;0 != (len = *sfb); sfb += 3, src += 2*len)
1218
+ {
1219
+ for (i = 0; i < len; i++, src++)
1220
+ {
1221
+ *dst++ = src[0*len];
1222
+ *dst++ = src[1*len];
1223
+ *dst++ = src[2*len];
1224
+ }
1225
+ }
1226
+ memcpy(grbuf, scratch, (dst - scratch)*sizeof(float));
1227
+ }
1228
+
1229
+ static void drmp3_L3_antialias(float *grbuf, int nbands)
1230
+ {
1231
+ static const float g_aa[2][8] = {
1232
+ {0.85749293f,0.88174200f,0.94962865f,0.98331459f,0.99551782f,0.99916056f,0.99989920f,0.99999316f},
1233
+ {0.51449576f,0.47173197f,0.31337745f,0.18191320f,0.09457419f,0.04096558f,0.01419856f,0.00369997f}
1234
+ };
1235
+
1236
+ for (; nbands > 0; nbands--, grbuf += 18)
1237
+ {
1238
+ int i = 0;
1239
+ #if DRMP3_HAVE_SIMD
1240
+ if (drmp3_have_simd()) for (; i < 8; i += 4)
1241
+ {
1242
+ drmp3_f4 vu = DRMP3_VLD(grbuf + 18 + i);
1243
+ drmp3_f4 vd = DRMP3_VLD(grbuf + 14 - i);
1244
+ drmp3_f4 vc0 = DRMP3_VLD(g_aa[0] + i);
1245
+ drmp3_f4 vc1 = DRMP3_VLD(g_aa[1] + i);
1246
+ vd = DRMP3_VREV(vd);
1247
+ DRMP3_VSTORE(grbuf + 18 + i, DRMP3_VSUB(DRMP3_VMUL(vu, vc0), DRMP3_VMUL(vd, vc1)));
1248
+ vd = DRMP3_VADD(DRMP3_VMUL(vu, vc1), DRMP3_VMUL(vd, vc0));
1249
+ DRMP3_VSTORE(grbuf + 14 - i, DRMP3_VREV(vd));
1250
+ }
1251
+ #endif
1252
+ #ifndef DR_MP3_ONLY_SIMD
1253
+ for(; i < 8; i++)
1254
+ {
1255
+ float u = grbuf[18 + i];
1256
+ float d = grbuf[17 - i];
1257
+ grbuf[18 + i] = u*g_aa[0][i] - d*g_aa[1][i];
1258
+ grbuf[17 - i] = u*g_aa[1][i] + d*g_aa[0][i];
1259
+ }
1260
+ #endif
1261
+ }
1262
+ }
1263
+
1264
+ static void drmp3_L3_dct3_9(float *y)
1265
+ {
1266
+ float s0, s1, s2, s3, s4, s5, s6, s7, s8, t0, t2, t4;
1267
+
1268
+ s0 = y[0]; s2 = y[2]; s4 = y[4]; s6 = y[6]; s8 = y[8];
1269
+ t0 = s0 + s6*0.5f;
1270
+ s0 -= s6;
1271
+ t4 = (s4 + s2)*0.93969262f;
1272
+ t2 = (s8 + s2)*0.76604444f;
1273
+ s6 = (s4 - s8)*0.17364818f;
1274
+ s4 += s8 - s2;
1275
+
1276
+ s2 = s0 - s4*0.5f;
1277
+ y[4] = s4 + s0;
1278
+ s8 = t0 - t2 + s6;
1279
+ s0 = t0 - t4 + t2;
1280
+ s4 = t0 + t4 - s6;
1281
+
1282
+ s1 = y[1]; s3 = y[3]; s5 = y[5]; s7 = y[7];
1283
+
1284
+ s3 *= 0.86602540f;
1285
+ t0 = (s5 + s1)*0.98480775f;
1286
+ t4 = (s5 - s7)*0.34202014f;
1287
+ t2 = (s1 + s7)*0.64278761f;
1288
+ s1 = (s1 - s5 - s7)*0.86602540f;
1289
+
1290
+ s5 = t0 - s3 - t2;
1291
+ s7 = t4 - s3 - t0;
1292
+ s3 = t4 + s3 - t2;
1293
+
1294
+ y[0] = s4 - s7;
1295
+ y[1] = s2 + s1;
1296
+ y[2] = s0 - s3;
1297
+ y[3] = s8 + s5;
1298
+ y[5] = s8 - s5;
1299
+ y[6] = s0 + s3;
1300
+ y[7] = s2 - s1;
1301
+ y[8] = s4 + s7;
1302
+ }
1303
+
1304
+ static void drmp3_L3_imdct36(float *grbuf, float *overlap, const float *window, int nbands)
1305
+ {
1306
+ int i, j;
1307
+ static const float g_twid9[18] = {
1308
+ 0.73727734f,0.79335334f,0.84339145f,0.88701083f,0.92387953f,0.95371695f,0.97629601f,0.99144486f,0.99904822f,0.67559021f,0.60876143f,0.53729961f,0.46174861f,0.38268343f,0.30070580f,0.21643961f,0.13052619f,0.04361938f
1309
+ };
1310
+
1311
+ for (j = 0; j < nbands; j++, grbuf += 18, overlap += 9)
1312
+ {
1313
+ float co[9], si[9];
1314
+ co[0] = -grbuf[0];
1315
+ si[0] = grbuf[17];
1316
+ for (i = 0; i < 4; i++)
1317
+ {
1318
+ si[8 - 2*i] = grbuf[4*i + 1] - grbuf[4*i + 2];
1319
+ co[1 + 2*i] = grbuf[4*i + 1] + grbuf[4*i + 2];
1320
+ si[7 - 2*i] = grbuf[4*i + 4] - grbuf[4*i + 3];
1321
+ co[2 + 2*i] = -(grbuf[4*i + 3] + grbuf[4*i + 4]);
1322
+ }
1323
+ drmp3_L3_dct3_9(co);
1324
+ drmp3_L3_dct3_9(si);
1325
+
1326
+ si[1] = -si[1];
1327
+ si[3] = -si[3];
1328
+ si[5] = -si[5];
1329
+ si[7] = -si[7];
1330
+
1331
+ i = 0;
1332
+
1333
+ #if DRMP3_HAVE_SIMD
1334
+ if (drmp3_have_simd()) for (; i < 8; i += 4)
1335
+ {
1336
+ drmp3_f4 vovl = DRMP3_VLD(overlap + i);
1337
+ drmp3_f4 vc = DRMP3_VLD(co + i);
1338
+ drmp3_f4 vs = DRMP3_VLD(si + i);
1339
+ drmp3_f4 vr0 = DRMP3_VLD(g_twid9 + i);
1340
+ drmp3_f4 vr1 = DRMP3_VLD(g_twid9 + 9 + i);
1341
+ drmp3_f4 vw0 = DRMP3_VLD(window + i);
1342
+ drmp3_f4 vw1 = DRMP3_VLD(window + 9 + i);
1343
+ drmp3_f4 vsum = DRMP3_VADD(DRMP3_VMUL(vc, vr1), DRMP3_VMUL(vs, vr0));
1344
+ DRMP3_VSTORE(overlap + i, DRMP3_VSUB(DRMP3_VMUL(vc, vr0), DRMP3_VMUL(vs, vr1)));
1345
+ DRMP3_VSTORE(grbuf + i, DRMP3_VSUB(DRMP3_VMUL(vovl, vw0), DRMP3_VMUL(vsum, vw1)));
1346
+ vsum = DRMP3_VADD(DRMP3_VMUL(vovl, vw1), DRMP3_VMUL(vsum, vw0));
1347
+ DRMP3_VSTORE(grbuf + 14 - i, DRMP3_VREV(vsum));
1348
+ }
1349
+ #endif
1350
+ for (; i < 9; i++)
1351
+ {
1352
+ float ovl = overlap[i];
1353
+ float sum = co[i]*g_twid9[9 + i] + si[i]*g_twid9[0 + i];
1354
+ overlap[i] = co[i]*g_twid9[0 + i] - si[i]*g_twid9[9 + i];
1355
+ grbuf[i] = ovl*window[0 + i] - sum*window[9 + i];
1356
+ grbuf[17 - i] = ovl*window[9 + i] + sum*window[0 + i];
1357
+ }
1358
+ }
1359
+ }
1360
+
1361
+ static void drmp3_L3_idct3(float x0, float x1, float x2, float *dst)
1362
+ {
1363
+ float m1 = x1*0.86602540f;
1364
+ float a1 = x0 - x2*0.5f;
1365
+ dst[1] = x0 + x2;
1366
+ dst[0] = a1 + m1;
1367
+ dst[2] = a1 - m1;
1368
+ }
1369
+
1370
+ static void drmp3_L3_imdct12(float *x, float *dst, float *overlap)
1371
+ {
1372
+ static const float g_twid3[6] = { 0.79335334f,0.92387953f,0.99144486f, 0.60876143f,0.38268343f,0.13052619f };
1373
+ float co[3], si[3];
1374
+ int i;
1375
+
1376
+ drmp3_L3_idct3(-x[0], x[6] + x[3], x[12] + x[9], co);
1377
+ drmp3_L3_idct3(x[15], x[12] - x[9], x[6] - x[3], si);
1378
+ si[1] = -si[1];
1379
+
1380
+ for (i = 0; i < 3; i++)
1381
+ {
1382
+ float ovl = overlap[i];
1383
+ float sum = co[i]*g_twid3[3 + i] + si[i]*g_twid3[0 + i];
1384
+ overlap[i] = co[i]*g_twid3[0 + i] - si[i]*g_twid3[3 + i];
1385
+ dst[i] = ovl*g_twid3[2 - i] - sum*g_twid3[5 - i];
1386
+ dst[5 - i] = ovl*g_twid3[5 - i] + sum*g_twid3[2 - i];
1387
+ }
1388
+ }
1389
+
1390
+ static void drmp3_L3_imdct_short(float *grbuf, float *overlap, int nbands)
1391
+ {
1392
+ for (;nbands > 0; nbands--, overlap += 9, grbuf += 18)
1393
+ {
1394
+ float tmp[18];
1395
+ memcpy(tmp, grbuf, sizeof(tmp));
1396
+ memcpy(grbuf, overlap, 6*sizeof(float));
1397
+ drmp3_L3_imdct12(tmp, grbuf + 6, overlap + 6);
1398
+ drmp3_L3_imdct12(tmp + 1, grbuf + 12, overlap + 6);
1399
+ drmp3_L3_imdct12(tmp + 2, overlap, overlap + 6);
1400
+ }
1401
+ }
1402
+
1403
+ static void drmp3_L3_change_sign(float *grbuf)
1404
+ {
1405
+ int b, i;
1406
+ for (b = 0, grbuf += 18; b < 32; b += 2, grbuf += 36)
1407
+ for (i = 1; i < 18; i += 2)
1408
+ grbuf[i] = -grbuf[i];
1409
+ }
1410
+
1411
+ static void drmp3_L3_imdct_gr(float *grbuf, float *overlap, unsigned block_type, unsigned n_long_bands)
1412
+ {
1413
+ static const float g_mdct_window[2][18] = {
1414
+ { 0.99904822f,0.99144486f,0.97629601f,0.95371695f,0.92387953f,0.88701083f,0.84339145f,0.79335334f,0.73727734f,0.04361938f,0.13052619f,0.21643961f,0.30070580f,0.38268343f,0.46174861f,0.53729961f,0.60876143f,0.67559021f },
1415
+ { 1,1,1,1,1,1,0.99144486f,0.92387953f,0.79335334f,0,0,0,0,0,0,0.13052619f,0.38268343f,0.60876143f }
1416
+ };
1417
+ if (n_long_bands)
1418
+ {
1419
+ drmp3_L3_imdct36(grbuf, overlap, g_mdct_window[0], n_long_bands);
1420
+ grbuf += 18*n_long_bands;
1421
+ overlap += 9*n_long_bands;
1422
+ }
1423
+ if (block_type == DRMP3_SHORT_BLOCK_TYPE)
1424
+ drmp3_L3_imdct_short(grbuf, overlap, 32 - n_long_bands);
1425
+ else
1426
+ drmp3_L3_imdct36(grbuf, overlap, g_mdct_window[block_type == DRMP3_STOP_BLOCK_TYPE], 32 - n_long_bands);
1427
+ }
1428
+
1429
+ static void drmp3_L3_save_reservoir(drmp3dec *h, drmp3dec_scratch *s)
1430
+ {
1431
+ int pos = (s->bs.pos + 7)/8u;
1432
+ int remains = s->bs.limit/8u - pos;
1433
+ if (remains > DRMP3_MAX_BITRESERVOIR_BYTES)
1434
+ {
1435
+ pos += remains - DRMP3_MAX_BITRESERVOIR_BYTES;
1436
+ remains = DRMP3_MAX_BITRESERVOIR_BYTES;
1437
+ }
1438
+ if (remains > 0)
1439
+ {
1440
+ memmove(h->reserv_buf, s->maindata + pos, remains);
1441
+ }
1442
+ h->reserv = remains;
1443
+ }
1444
+
1445
+ static int drmp3_L3_restore_reservoir(drmp3dec *h, drmp3_bs *bs, drmp3dec_scratch *s, int main_data_begin)
1446
+ {
1447
+ int frame_bytes = (bs->limit - bs->pos)/8;
1448
+ int bytes_have = DRMP3_MIN(h->reserv, main_data_begin);
1449
+ memcpy(s->maindata, h->reserv_buf + DRMP3_MAX(0, h->reserv - main_data_begin), DRMP3_MIN(h->reserv, main_data_begin));
1450
+ memcpy(s->maindata + bytes_have, bs->buf + bs->pos/8, frame_bytes);
1451
+ drmp3_bs_init(&s->bs, s->maindata, bytes_have + frame_bytes);
1452
+ return h->reserv >= main_data_begin;
1453
+ }
1454
+
1455
+ static void drmp3_L3_decode(drmp3dec *h, drmp3dec_scratch *s, drmp3_L3_gr_info *gr_info, int nch)
1456
+ {
1457
+ int ch;
1458
+
1459
+ for (ch = 0; ch < nch; ch++)
1460
+ {
1461
+ int layer3gr_limit = s->bs.pos + gr_info[ch].part_23_length;
1462
+ drmp3_L3_decode_scalefactors(h->header, s->ist_pos[ch], &s->bs, gr_info + ch, s->scf, ch);
1463
+ drmp3_L3_huffman(s->grbuf[ch], &s->bs, gr_info + ch, s->scf, layer3gr_limit);
1464
+ }
1465
+
1466
+ if (DRMP3_HDR_TEST_I_STEREO(h->header))
1467
+ {
1468
+ drmp3_L3_intensity_stereo(s->grbuf[0], s->ist_pos[1], gr_info, h->header);
1469
+ } else if (DRMP3_HDR_IS_MS_STEREO(h->header))
1470
+ {
1471
+ drmp3_L3_midside_stereo(s->grbuf[0], 576);
1472
+ }
1473
+
1474
+ for (ch = 0; ch < nch; ch++, gr_info++)
1475
+ {
1476
+ int aa_bands = 31;
1477
+ int n_long_bands = (gr_info->mixed_block_flag ? 2 : 0) << (int)(DRMP3_HDR_GET_MY_SAMPLE_RATE(h->header) == 2);
1478
+
1479
+ if (gr_info->n_short_sfb)
1480
+ {
1481
+ aa_bands = n_long_bands - 1;
1482
+ drmp3_L3_reorder(s->grbuf[ch] + n_long_bands*18, s->syn[0], gr_info->sfbtab + gr_info->n_long_sfb);
1483
+ }
1484
+
1485
+ drmp3_L3_antialias(s->grbuf[ch], aa_bands);
1486
+ drmp3_L3_imdct_gr(s->grbuf[ch], h->mdct_overlap[ch], gr_info->block_type, n_long_bands);
1487
+ drmp3_L3_change_sign(s->grbuf[ch]);
1488
+ }
1489
+ }
1490
+
1491
+ static void drmp3d_DCT_II(float *grbuf, int n)
1492
+ {
1493
+ static const float g_sec[24] = {
1494
+ 10.19000816f,0.50060302f,0.50241929f,3.40760851f,0.50547093f,0.52249861f,2.05778098f,0.51544732f,0.56694406f,1.48416460f,0.53104258f,0.64682180f,1.16943991f,0.55310392f,0.78815460f,0.97256821f,0.58293498f,1.06067765f,0.83934963f,0.62250412f,1.72244716f,0.74453628f,0.67480832f,5.10114861f
1495
+ };
1496
+ int i, k = 0;
1497
+ #if DRMP3_HAVE_SIMD
1498
+ if (drmp3_have_simd()) for (; k < n; k += 4)
1499
+ {
1500
+ drmp3_f4 t[4][8], *x;
1501
+ float *y = grbuf + k;
1502
+
1503
+ for (x = t[0], i = 0; i < 8; i++, x++)
1504
+ {
1505
+ drmp3_f4 x0 = DRMP3_VLD(&y[i*18]);
1506
+ drmp3_f4 x1 = DRMP3_VLD(&y[(15 - i)*18]);
1507
+ drmp3_f4 x2 = DRMP3_VLD(&y[(16 + i)*18]);
1508
+ drmp3_f4 x3 = DRMP3_VLD(&y[(31 - i)*18]);
1509
+ drmp3_f4 t0 = DRMP3_VADD(x0, x3);
1510
+ drmp3_f4 t1 = DRMP3_VADD(x1, x2);
1511
+ drmp3_f4 t2 = DRMP3_VMUL_S(DRMP3_VSUB(x1, x2), g_sec[3*i + 0]);
1512
+ drmp3_f4 t3 = DRMP3_VMUL_S(DRMP3_VSUB(x0, x3), g_sec[3*i + 1]);
1513
+ x[0] = DRMP3_VADD(t0, t1);
1514
+ x[8] = DRMP3_VMUL_S(DRMP3_VSUB(t0, t1), g_sec[3*i + 2]);
1515
+ x[16] = DRMP3_VADD(t3, t2);
1516
+ x[24] = DRMP3_VMUL_S(DRMP3_VSUB(t3, t2), g_sec[3*i + 2]);
1517
+ }
1518
+ for (x = t[0], i = 0; i < 4; i++, x += 8)
1519
+ {
1520
+ drmp3_f4 x0 = x[0], x1 = x[1], x2 = x[2], x3 = x[3], x4 = x[4], x5 = x[5], x6 = x[6], x7 = x[7], xt;
1521
+ xt = DRMP3_VSUB(x0, x7); x0 = DRMP3_VADD(x0, x7);
1522
+ x7 = DRMP3_VSUB(x1, x6); x1 = DRMP3_VADD(x1, x6);
1523
+ x6 = DRMP3_VSUB(x2, x5); x2 = DRMP3_VADD(x2, x5);
1524
+ x5 = DRMP3_VSUB(x3, x4); x3 = DRMP3_VADD(x3, x4);
1525
+ x4 = DRMP3_VSUB(x0, x3); x0 = DRMP3_VADD(x0, x3);
1526
+ x3 = DRMP3_VSUB(x1, x2); x1 = DRMP3_VADD(x1, x2);
1527
+ x[0] = DRMP3_VADD(x0, x1);
1528
+ x[4] = DRMP3_VMUL_S(DRMP3_VSUB(x0, x1), 0.70710677f);
1529
+ x5 = DRMP3_VADD(x5, x6);
1530
+ x6 = DRMP3_VMUL_S(DRMP3_VADD(x6, x7), 0.70710677f);
1531
+ x7 = DRMP3_VADD(x7, xt);
1532
+ x3 = DRMP3_VMUL_S(DRMP3_VADD(x3, x4), 0.70710677f);
1533
+ x5 = DRMP3_VSUB(x5, DRMP3_VMUL_S(x7, 0.198912367f)); /* rotate by PI/8 */
1534
+ x7 = DRMP3_VADD(x7, DRMP3_VMUL_S(x5, 0.382683432f));
1535
+ x5 = DRMP3_VSUB(x5, DRMP3_VMUL_S(x7, 0.198912367f));
1536
+ x0 = DRMP3_VSUB(xt, x6); xt = DRMP3_VADD(xt, x6);
1537
+ x[1] = DRMP3_VMUL_S(DRMP3_VADD(xt, x7), 0.50979561f);
1538
+ x[2] = DRMP3_VMUL_S(DRMP3_VADD(x4, x3), 0.54119611f);
1539
+ x[3] = DRMP3_VMUL_S(DRMP3_VSUB(x0, x5), 0.60134488f);
1540
+ x[5] = DRMP3_VMUL_S(DRMP3_VADD(x0, x5), 0.89997619f);
1541
+ x[6] = DRMP3_VMUL_S(DRMP3_VSUB(x4, x3), 1.30656302f);
1542
+ x[7] = DRMP3_VMUL_S(DRMP3_VSUB(xt, x7), 2.56291556f);
1543
+ }
1544
+
1545
+ if (k > n - 3)
1546
+ {
1547
+ #if DRMP3_HAVE_SSE
1548
+ #define DRMP3_VSAVE2(i, v) _mm_storel_pi((__m64 *)(void*)&y[i*18], v)
1549
+ #else
1550
+ #define DRMP3_VSAVE2(i, v) vst1_f32((float32_t *)&y[i*18], vget_low_f32(v))
1551
+ #endif
1552
+ for (i = 0; i < 7; i++, y += 4*18)
1553
+ {
1554
+ drmp3_f4 s = DRMP3_VADD(t[3][i], t[3][i + 1]);
1555
+ DRMP3_VSAVE2(0, t[0][i]);
1556
+ DRMP3_VSAVE2(1, DRMP3_VADD(t[2][i], s));
1557
+ DRMP3_VSAVE2(2, DRMP3_VADD(t[1][i], t[1][i + 1]));
1558
+ DRMP3_VSAVE2(3, DRMP3_VADD(t[2][1 + i], s));
1559
+ }
1560
+ DRMP3_VSAVE2(0, t[0][7]);
1561
+ DRMP3_VSAVE2(1, DRMP3_VADD(t[2][7], t[3][7]));
1562
+ DRMP3_VSAVE2(2, t[1][7]);
1563
+ DRMP3_VSAVE2(3, t[3][7]);
1564
+ } else
1565
+ {
1566
+ #define DRMP3_VSAVE4(i, v) DRMP3_VSTORE(&y[i*18], v)
1567
+ for (i = 0; i < 7; i++, y += 4*18)
1568
+ {
1569
+ drmp3_f4 s = DRMP3_VADD(t[3][i], t[3][i + 1]);
1570
+ DRMP3_VSAVE4(0, t[0][i]);
1571
+ DRMP3_VSAVE4(1, DRMP3_VADD(t[2][i], s));
1572
+ DRMP3_VSAVE4(2, DRMP3_VADD(t[1][i], t[1][i + 1]));
1573
+ DRMP3_VSAVE4(3, DRMP3_VADD(t[2][1 + i], s));
1574
+ }
1575
+ DRMP3_VSAVE4(0, t[0][7]);
1576
+ DRMP3_VSAVE4(1, DRMP3_VADD(t[2][7], t[3][7]));
1577
+ DRMP3_VSAVE4(2, t[1][7]);
1578
+ DRMP3_VSAVE4(3, t[3][7]);
1579
+ }
1580
+ } else
1581
+ #endif
1582
+ #ifdef DR_MP3_ONLY_SIMD
1583
+ {}
1584
+ #else
1585
+ for (; k < n; k++)
1586
+ {
1587
+ float t[4][8], *x, *y = grbuf + k;
1588
+
1589
+ for (x = t[0], i = 0; i < 8; i++, x++)
1590
+ {
1591
+ float x0 = y[i*18];
1592
+ float x1 = y[(15 - i)*18];
1593
+ float x2 = y[(16 + i)*18];
1594
+ float x3 = y[(31 - i)*18];
1595
+ float t0 = x0 + x3;
1596
+ float t1 = x1 + x2;
1597
+ float t2 = (x1 - x2)*g_sec[3*i + 0];
1598
+ float t3 = (x0 - x3)*g_sec[3*i + 1];
1599
+ x[0] = t0 + t1;
1600
+ x[8] = (t0 - t1)*g_sec[3*i + 2];
1601
+ x[16] = t3 + t2;
1602
+ x[24] = (t3 - t2)*g_sec[3*i + 2];
1603
+ }
1604
+ for (x = t[0], i = 0; i < 4; i++, x += 8)
1605
+ {
1606
+ float x0 = x[0], x1 = x[1], x2 = x[2], x3 = x[3], x4 = x[4], x5 = x[5], x6 = x[6], x7 = x[7], xt;
1607
+ xt = x0 - x7; x0 += x7;
1608
+ x7 = x1 - x6; x1 += x6;
1609
+ x6 = x2 - x5; x2 += x5;
1610
+ x5 = x3 - x4; x3 += x4;
1611
+ x4 = x0 - x3; x0 += x3;
1612
+ x3 = x1 - x2; x1 += x2;
1613
+ x[0] = x0 + x1;
1614
+ x[4] = (x0 - x1)*0.70710677f;
1615
+ x5 = x5 + x6;
1616
+ x6 = (x6 + x7)*0.70710677f;
1617
+ x7 = x7 + xt;
1618
+ x3 = (x3 + x4)*0.70710677f;
1619
+ x5 -= x7*0.198912367f; /* rotate by PI/8 */
1620
+ x7 += x5*0.382683432f;
1621
+ x5 -= x7*0.198912367f;
1622
+ x0 = xt - x6; xt += x6;
1623
+ x[1] = (xt + x7)*0.50979561f;
1624
+ x[2] = (x4 + x3)*0.54119611f;
1625
+ x[3] = (x0 - x5)*0.60134488f;
1626
+ x[5] = (x0 + x5)*0.89997619f;
1627
+ x[6] = (x4 - x3)*1.30656302f;
1628
+ x[7] = (xt - x7)*2.56291556f;
1629
+
1630
+ }
1631
+ for (i = 0; i < 7; i++, y += 4*18)
1632
+ {
1633
+ y[0*18] = t[0][i];
1634
+ y[1*18] = t[2][i] + t[3][i] + t[3][i + 1];
1635
+ y[2*18] = t[1][i] + t[1][i + 1];
1636
+ y[3*18] = t[2][i + 1] + t[3][i] + t[3][i + 1];
1637
+ }
1638
+ y[0*18] = t[0][7];
1639
+ y[1*18] = t[2][7] + t[3][7];
1640
+ y[2*18] = t[1][7];
1641
+ y[3*18] = t[3][7];
1642
+ }
1643
+ #endif
1644
+ }
1645
+
1646
+ static short drmp3d_scale_pcm(float sample)
1647
+ {
1648
+ if (sample > 32767.0) return (short) 32767;
1649
+ if (sample < -32768.0) return (short)-32768;
1650
+ int s = (int)(sample + .5f);
1651
+ s -= (s < 0); /* away from zero, to be compliant */
1652
+ if (s > 32767) return (short) 32767;
1653
+ if (s < -32768) return (short)-32768;
1654
+ return (short)s;
1655
+ }
1656
+
1657
+ static void drmp3d_synth_pair(short *pcm, int nch, const float *z)
1658
+ {
1659
+ float a;
1660
+ a = (z[14*64] - z[ 0]) * 29;
1661
+ a += (z[ 1*64] + z[13*64]) * 213;
1662
+ a += (z[12*64] - z[ 2*64]) * 459;
1663
+ a += (z[ 3*64] + z[11*64]) * 2037;
1664
+ a += (z[10*64] - z[ 4*64]) * 5153;
1665
+ a += (z[ 5*64] + z[ 9*64]) * 6574;
1666
+ a += (z[ 8*64] - z[ 6*64]) * 37489;
1667
+ a += z[ 7*64] * 75038;
1668
+ pcm[0] = drmp3d_scale_pcm(a);
1669
+
1670
+ z += 2;
1671
+ a = z[14*64] * 104;
1672
+ a += z[12*64] * 1567;
1673
+ a += z[10*64] * 9727;
1674
+ a += z[ 8*64] * 64019;
1675
+ a += z[ 6*64] * -9975;
1676
+ a += z[ 4*64] * -45;
1677
+ a += z[ 2*64] * 146;
1678
+ a += z[ 0*64] * -5;
1679
+ pcm[16*nch] = drmp3d_scale_pcm(a);
1680
+ }
1681
+
1682
+ static void drmp3d_synth(float *xl, short *dstl, int nch, float *lins)
1683
+ {
1684
+ int i;
1685
+ float *xr = xl + 576*(nch - 1);
1686
+ short *dstr = dstl + (nch - 1);
1687
+
1688
+ static const float g_win[] = {
1689
+ -1,26,-31,208,218,401,-519,2063,2000,4788,-5517,7134,5959,35640,-39336,74992,
1690
+ -1,24,-35,202,222,347,-581,2080,1952,4425,-5879,7640,5288,33791,-41176,74856,
1691
+ -1,21,-38,196,225,294,-645,2087,1893,4063,-6237,8092,4561,31947,-43006,74630,
1692
+ -1,19,-41,190,227,244,-711,2085,1822,3705,-6589,8492,3776,30112,-44821,74313,
1693
+ -1,17,-45,183,228,197,-779,2075,1739,3351,-6935,8840,2935,28289,-46617,73908,
1694
+ -1,16,-49,176,228,153,-848,2057,1644,3004,-7271,9139,2037,26482,-48390,73415,
1695
+ -2,14,-53,169,227,111,-919,2032,1535,2663,-7597,9389,1082,24694,-50137,72835,
1696
+ -2,13,-58,161,224,72,-991,2001,1414,2330,-7910,9592,70,22929,-51853,72169,
1697
+ -2,11,-63,154,221,36,-1064,1962,1280,2006,-8209,9750,-998,21189,-53534,71420,
1698
+ -2,10,-68,147,215,2,-1137,1919,1131,1692,-8491,9863,-2122,19478,-55178,70590,
1699
+ -3,9,-73,139,208,-29,-1210,1870,970,1388,-8755,9935,-3300,17799,-56778,69679,
1700
+ -3,8,-79,132,200,-57,-1283,1817,794,1095,-8998,9966,-4533,16155,-58333,68692,
1701
+ -4,7,-85,125,189,-83,-1356,1759,605,814,-9219,9959,-5818,14548,-59838,67629,
1702
+ -4,7,-91,117,177,-106,-1428,1698,402,545,-9416,9916,-7154,12980,-61289,66494,
1703
+ -5,6,-97,111,163,-127,-1498,1634,185,288,-9585,9838,-8540,11455,-62684,65290
1704
+ };
1705
+ float *zlin = lins + 15*64;
1706
+ const float *w = g_win;
1707
+
1708
+ zlin[4*15] = xl[18*16];
1709
+ zlin[4*15 + 1] = xr[18*16];
1710
+ zlin[4*15 + 2] = xl[0];
1711
+ zlin[4*15 + 3] = xr[0];
1712
+
1713
+ zlin[4*31] = xl[1 + 18*16];
1714
+ zlin[4*31 + 1] = xr[1 + 18*16];
1715
+ zlin[4*31 + 2] = xl[1];
1716
+ zlin[4*31 + 3] = xr[1];
1717
+
1718
+ drmp3d_synth_pair(dstr, nch, lins + 4*15 + 1);
1719
+ drmp3d_synth_pair(dstr + 32*nch, nch, lins + 4*15 + 64 + 1);
1720
+ drmp3d_synth_pair(dstl, nch, lins + 4*15);
1721
+ drmp3d_synth_pair(dstl + 32*nch, nch, lins + 4*15 + 64);
1722
+
1723
+ #if DRMP3_HAVE_SIMD
1724
+ if (drmp3_have_simd()) for (i = 14; i >= 0; i--)
1725
+ {
1726
+ #define DRMP3_VLOAD(k) drmp3_f4 w0 = DRMP3_VSET(*w++); drmp3_f4 w1 = DRMP3_VSET(*w++); drmp3_f4 vz = DRMP3_VLD(&zlin[4*i - 64*k]); drmp3_f4 vy = DRMP3_VLD(&zlin[4*i - 64*(15 - k)]);
1727
+ #define DRMP3_V0(k) { DRMP3_VLOAD(k) b = DRMP3_VADD(DRMP3_VMUL(vz, w1), DRMP3_VMUL(vy, w0)) ; a = DRMP3_VSUB(DRMP3_VMUL(vz, w0), DRMP3_VMUL(vy, w1)); }
1728
+ #define DRMP3_V1(k) { DRMP3_VLOAD(k) b = DRMP3_VADD(b, DRMP3_VADD(DRMP3_VMUL(vz, w1), DRMP3_VMUL(vy, w0))); a = DRMP3_VADD(a, DRMP3_VSUB(DRMP3_VMUL(vz, w0), DRMP3_VMUL(vy, w1))); }
1729
+ #define DRMP3_V2(k) { DRMP3_VLOAD(k) b = DRMP3_VADD(b, DRMP3_VADD(DRMP3_VMUL(vz, w1), DRMP3_VMUL(vy, w0))); a = DRMP3_VADD(a, DRMP3_VSUB(DRMP3_VMUL(vy, w1), DRMP3_VMUL(vz, w0))); }
1730
+ drmp3_f4 a, b;
1731
+ zlin[4*i] = xl[18*(31 - i)];
1732
+ zlin[4*i + 1] = xr[18*(31 - i)];
1733
+ zlin[4*i + 2] = xl[1 + 18*(31 - i)];
1734
+ zlin[4*i + 3] = xr[1 + 18*(31 - i)];
1735
+ zlin[4*i + 64] = xl[1 + 18*(1 + i)];
1736
+ zlin[4*i + 64 + 1] = xr[1 + 18*(1 + i)];
1737
+ zlin[4*i - 64 + 2] = xl[18*(1 + i)];
1738
+ zlin[4*i - 64 + 3] = xr[18*(1 + i)];
1739
+
1740
+ DRMP3_V0(0) DRMP3_V2(1) DRMP3_V1(2) DRMP3_V2(3) DRMP3_V1(4) DRMP3_V2(5) DRMP3_V1(6) DRMP3_V2(7)
1741
+
1742
+ {
1743
+ #if DRMP3_HAVE_SSE
1744
+ static const drmp3_f4 g_max = { 32767.0f, 32767.0f, 32767.0f, 32767.0f };
1745
+ static const drmp3_f4 g_min = { -32768.0f, -32768.0f, -32768.0f, -32768.0f };
1746
+ __m128i pcm8 = _mm_packs_epi32(_mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(a, g_max), g_min)),
1747
+ _mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(b, g_max), g_min)));
1748
+ dstr[(15 - i)*nch] = (short)_mm_extract_epi16(pcm8, 1);
1749
+ dstr[(17 + i)*nch] = (short)_mm_extract_epi16(pcm8, 5);
1750
+ dstl[(15 - i)*nch] = (short)_mm_extract_epi16(pcm8, 0);
1751
+ dstl[(17 + i)*nch] = (short)_mm_extract_epi16(pcm8, 4);
1752
+ dstr[(47 - i)*nch] = (short)_mm_extract_epi16(pcm8, 3);
1753
+ dstr[(49 + i)*nch] = (short)_mm_extract_epi16(pcm8, 7);
1754
+ dstl[(47 - i)*nch] = (short)_mm_extract_epi16(pcm8, 2);
1755
+ dstl[(49 + i)*nch] = (short)_mm_extract_epi16(pcm8, 6);
1756
+ #else
1757
+ int16x4_t pcma, pcmb;
1758
+ a = DRMP3_VADD(a, DRMP3_VSET(0.5f));
1759
+ b = DRMP3_VADD(b, DRMP3_VSET(0.5f));
1760
+ pcma = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(a), vreinterpretq_s32_u32(vcltq_f32(a, DRMP3_VSET(0)))));
1761
+ pcmb = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(b), vreinterpretq_s32_u32(vcltq_f32(b, DRMP3_VSET(0)))));
1762
+ vst1_lane_s16(dstr + (15 - i)*nch, pcma, 1);
1763
+ vst1_lane_s16(dstr + (17 + i)*nch, pcmb, 1);
1764
+ vst1_lane_s16(dstl + (15 - i)*nch, pcma, 0);
1765
+ vst1_lane_s16(dstl + (17 + i)*nch, pcmb, 0);
1766
+ vst1_lane_s16(dstr + (47 - i)*nch, pcma, 3);
1767
+ vst1_lane_s16(dstr + (49 + i)*nch, pcmb, 3);
1768
+ vst1_lane_s16(dstl + (47 - i)*nch, pcma, 2);
1769
+ vst1_lane_s16(dstl + (49 + i)*nch, pcmb, 2);
1770
+ #endif
1771
+ }
1772
+ } else
1773
+ #endif
1774
+ #ifdef DR_MP3_ONLY_SIMD
1775
+ {}
1776
+ #else
1777
+ for (i = 14; i >= 0; i--)
1778
+ {
1779
+ #define DRMP3_LOAD(k) float w0 = *w++; float w1 = *w++; float *vz = &zlin[4*i - k*64]; float *vy = &zlin[4*i - (15 - k)*64];
1780
+ #define DRMP3_S0(k) { int j; DRMP3_LOAD(k); for (j = 0; j < 4; j++) b[j] = vz[j]*w1 + vy[j]*w0, a[j] = vz[j]*w0 - vy[j]*w1; }
1781
+ #define DRMP3_S1(k) { int j; DRMP3_LOAD(k); for (j = 0; j < 4; j++) b[j] += vz[j]*w1 + vy[j]*w0, a[j] += vz[j]*w0 - vy[j]*w1; }
1782
+ #define DRMP3_S2(k) { int j; DRMP3_LOAD(k); for (j = 0; j < 4; j++) b[j] += vz[j]*w1 + vy[j]*w0, a[j] += vy[j]*w1 - vz[j]*w0; }
1783
+ float a[4], b[4];
1784
+
1785
+ zlin[4*i] = xl[18*(31 - i)];
1786
+ zlin[4*i + 1] = xr[18*(31 - i)];
1787
+ zlin[4*i + 2] = xl[1 + 18*(31 - i)];
1788
+ zlin[4*i + 3] = xr[1 + 18*(31 - i)];
1789
+ zlin[4*(i + 16)] = xl[1 + 18*(1 + i)];
1790
+ zlin[4*(i + 16) + 1] = xr[1 + 18*(1 + i)];
1791
+ zlin[4*(i - 16) + 2] = xl[18*(1 + i)];
1792
+ zlin[4*(i - 16) + 3] = xr[18*(1 + i)];
1793
+
1794
+ DRMP3_S0(0) DRMP3_S2(1) DRMP3_S1(2) DRMP3_S2(3) DRMP3_S1(4) DRMP3_S2(5) DRMP3_S1(6) DRMP3_S2(7)
1795
+
1796
+ dstr[(15 - i)*nch] = drmp3d_scale_pcm(a[1]);
1797
+ dstr[(17 + i)*nch] = drmp3d_scale_pcm(b[1]);
1798
+ dstl[(15 - i)*nch] = drmp3d_scale_pcm(a[0]);
1799
+ dstl[(17 + i)*nch] = drmp3d_scale_pcm(b[0]);
1800
+ dstr[(47 - i)*nch] = drmp3d_scale_pcm(a[3]);
1801
+ dstr[(49 + i)*nch] = drmp3d_scale_pcm(b[3]);
1802
+ dstl[(47 - i)*nch] = drmp3d_scale_pcm(a[2]);
1803
+ dstl[(49 + i)*nch] = drmp3d_scale_pcm(b[2]);
1804
+ }
1805
+ #endif
1806
+ }
1807
+
1808
+ static void drmp3d_synth_granule(float *qmf_state, float *grbuf, int nbands, int nch, short *pcm, float *lins)
1809
+ {
1810
+ int i;
1811
+ for (i = 0; i < nch; i++)
1812
+ {
1813
+ drmp3d_DCT_II(grbuf + 576*i, nbands);
1814
+ }
1815
+
1816
+ memcpy(lins, qmf_state, sizeof(float)*15*64);
1817
+
1818
+ for (i = 0; i < nbands; i += 2)
1819
+ {
1820
+ drmp3d_synth(grbuf + i, pcm + 32*nch*i, nch, lins + i*64);
1821
+ }
1822
+ #ifndef DR_MP3_NONSTANDARD_BUT_LOGICAL
1823
+ if (nch == 1)
1824
+ {
1825
+ for (i = 0; i < 15*64; i += 2)
1826
+ {
1827
+ qmf_state[i] = lins[nbands*64 + i];
1828
+ }
1829
+ } else
1830
+ #endif
1831
+ {
1832
+ memcpy(qmf_state, lins + nbands*64, sizeof(float)*15*64);
1833
+ }
1834
+ }
1835
+
1836
+ static int drmp3d_match_frame(const drmp3_uint8 *hdr, int mp3_bytes, int frame_bytes)
1837
+ {
1838
+ int i, nmatch;
1839
+ for (i = 0, nmatch = 0; nmatch < DRMP3_MAX_FRAME_SYNC_MATCHES; nmatch++)
1840
+ {
1841
+ i += drmp3_hdr_frame_bytes(hdr + i, frame_bytes) + drmp3_hdr_padding(hdr + i);
1842
+ if (i + DRMP3_HDR_SIZE > mp3_bytes)
1843
+ return nmatch > 0;
1844
+ if (!drmp3_hdr_compare(hdr, hdr + i))
1845
+ return 0;
1846
+ }
1847
+ return 1;
1848
+ }
1849
+
1850
+ static int drmp3d_find_frame(const drmp3_uint8 *mp3, int mp3_bytes, int *free_format_bytes, int *ptr_frame_bytes)
1851
+ {
1852
+ int i, k;
1853
+ for (i = 0; i < mp3_bytes - DRMP3_HDR_SIZE; i++, mp3++)
1854
+ {
1855
+ if (drmp3_hdr_valid(mp3))
1856
+ {
1857
+ int frame_bytes = drmp3_hdr_frame_bytes(mp3, *free_format_bytes);
1858
+ int frame_and_padding = frame_bytes + drmp3_hdr_padding(mp3);
1859
+
1860
+ for (k = DRMP3_HDR_SIZE; !frame_bytes && k < DRMP3_MAX_FREE_FORMAT_FRAME_SIZE && i + 2*k < mp3_bytes - DRMP3_HDR_SIZE; k++)
1861
+ {
1862
+ if (drmp3_hdr_compare(mp3, mp3 + k))
1863
+ {
1864
+ int fb = k - drmp3_hdr_padding(mp3);
1865
+ int nextfb = fb + drmp3_hdr_padding(mp3 + k);
1866
+ if (i + k + nextfb + DRMP3_HDR_SIZE > mp3_bytes || !drmp3_hdr_compare(mp3, mp3 + k + nextfb))
1867
+ continue;
1868
+ frame_and_padding = k;
1869
+ frame_bytes = fb;
1870
+ *free_format_bytes = fb;
1871
+ }
1872
+ }
1873
+
1874
+ if ((frame_bytes && i + frame_and_padding <= mp3_bytes &&
1875
+ drmp3d_match_frame(mp3, mp3_bytes - i, frame_bytes)) ||
1876
+ (!i && frame_and_padding == mp3_bytes))
1877
+ {
1878
+ *ptr_frame_bytes = frame_and_padding;
1879
+ return i;
1880
+ }
1881
+ *free_format_bytes = 0;
1882
+ }
1883
+ }
1884
+ *ptr_frame_bytes = 0;
1885
+ return i;
1886
+ }
1887
+
1888
+ void drmp3dec_init(drmp3dec *dec)
1889
+ {
1890
+ dec->header[0] = 0;
1891
+ }
1892
+
1893
+ int drmp3dec_decode_frame(drmp3dec *dec, const unsigned char *mp3, int mp3_bytes, short *pcm, drmp3dec_frame_info *info)
1894
+ {
1895
+ int i = 0, igr, frame_size = 0, success = 1;
1896
+ const drmp3_uint8 *hdr;
1897
+ drmp3_bs bs_frame[1];
1898
+ drmp3dec_scratch scratch;
1899
+
1900
+ if (mp3_bytes > 4 && dec->header[0] == 0xff && drmp3_hdr_compare(dec->header, mp3))
1901
+ {
1902
+ frame_size = drmp3_hdr_frame_bytes(mp3, dec->free_format_bytes) + drmp3_hdr_padding(mp3);
1903
+ if (frame_size != mp3_bytes && (frame_size + DRMP3_HDR_SIZE > mp3_bytes || !drmp3_hdr_compare(mp3, mp3 + frame_size)))
1904
+ {
1905
+ frame_size = 0;
1906
+ }
1907
+ }
1908
+ if (!frame_size)
1909
+ {
1910
+ memset(dec, 0, sizeof(drmp3dec));
1911
+ i = drmp3d_find_frame(mp3, mp3_bytes, &dec->free_format_bytes, &frame_size);
1912
+ if (!frame_size || i + frame_size > mp3_bytes)
1913
+ {
1914
+ info->frame_bytes = i;
1915
+ return 0;
1916
+ }
1917
+ }
1918
+
1919
+ hdr = mp3 + i;
1920
+ memcpy(dec->header, hdr, DRMP3_HDR_SIZE);
1921
+ info->frame_bytes = i + frame_size;
1922
+ info->channels = DRMP3_HDR_IS_MONO(hdr) ? 1 : 2;
1923
+ info->hz = drmp3_hdr_sample_rate_hz(hdr);
1924
+ info->layer = 4 - DRMP3_HDR_GET_LAYER(hdr);
1925
+ info->bitrate_kbps = drmp3_hdr_bitrate_kbps(hdr);
1926
+
1927
+ drmp3_bs_init(bs_frame, hdr + DRMP3_HDR_SIZE, frame_size - DRMP3_HDR_SIZE);
1928
+ if (DRMP3_HDR_IS_CRC(hdr))
1929
+ {
1930
+ drmp3_bs_get_bits(bs_frame, 16);
1931
+ }
1932
+
1933
+ if (info->layer == 3)
1934
+ {
1935
+ int main_data_begin = drmp3_L3_read_side_info(bs_frame, scratch.gr_info, hdr);
1936
+ if (main_data_begin < 0 || bs_frame->pos > bs_frame->limit)
1937
+ {
1938
+ drmp3dec_init(dec);
1939
+ return 0;
1940
+ }
1941
+ success = drmp3_L3_restore_reservoir(dec, bs_frame, &scratch, main_data_begin);
1942
+ if (success)
1943
+ {
1944
+ for (igr = 0; igr < (DRMP3_HDR_TEST_MPEG1(hdr) ? 2 : 1); igr++, pcm += 576*info->channels)
1945
+ {
1946
+ memset(scratch.grbuf[0], 0, 576*2*sizeof(float));
1947
+ drmp3_L3_decode(dec, &scratch, scratch.gr_info + igr*info->channels, info->channels);
1948
+ drmp3d_synth_granule(dec->qmf_state, scratch.grbuf[0], 18, info->channels, pcm, scratch.syn[0]);
1949
+ }
1950
+ }
1951
+ drmp3_L3_save_reservoir(dec, &scratch);
1952
+ } else
1953
+ {
1954
+ #ifdef DR_MP3_ONLY_MP3
1955
+ return 0;
1956
+ #else
1957
+ drmp3_L12_scale_info sci[1];
1958
+ drmp3_L12_read_scale_info(hdr, bs_frame, sci);
1959
+
1960
+ memset(scratch.grbuf[0], 0, 576*2*sizeof(float));
1961
+ for (i = 0, igr = 0; igr < 3; igr++)
1962
+ {
1963
+ if (12 == (i += drmp3_L12_dequantize_granule(scratch.grbuf[0] + i, bs_frame, sci, info->layer | 1)))
1964
+ {
1965
+ i = 0;
1966
+ drmp3_L12_apply_scf_384(sci, sci->scf + igr, scratch.grbuf[0]);
1967
+ drmp3d_synth_granule(dec->qmf_state, scratch.grbuf[0], 12, info->channels, pcm, scratch.syn[0]);
1968
+ memset(scratch.grbuf[0], 0, 576*2*sizeof(float));
1969
+ pcm += 384*info->channels;
1970
+ }
1971
+ if (bs_frame->pos > bs_frame->limit)
1972
+ {
1973
+ drmp3dec_init(dec);
1974
+ return 0;
1975
+ }
1976
+ }
1977
+ #endif
1978
+ }
1979
+ return success*drmp3_hdr_frame_samples(dec->header);
1980
+ }
1981
+
1982
+
1983
+
1984
+
1985
+ ///////////////////////////////////////////////////////////////////////////////
1986
+ //
1987
+ // Main Public API
1988
+ //
1989
+ ///////////////////////////////////////////////////////////////////////////////
1990
+
1991
+ // Options.
1992
+ #ifndef DR_MP3_DEFAULT_CHANNELS
1993
+ #define DR_MP3_DEFAULT_CHANNELS 2
1994
+ #endif
1995
+ #ifndef DR_MP3_DEFAULT_SAMPLE_RATE
1996
+ #define DR_MP3_DEFAULT_SAMPLE_RATE 44100
1997
+ #endif
1998
+
1999
+
2000
+ // Standard library stuff.
2001
+ #ifndef DRMP3_ASSERT
2002
+ #include <assert.h>
2003
+ #define DRMP3_ASSERT(expression) assert(expression)
2004
+ #endif
2005
+ #ifndef DRMP3_COPY_MEMORY
2006
+ #define DRMP3_COPY_MEMORY(dst, src, sz) memcpy((dst), (src), (sz))
2007
+ #endif
2008
+ #ifndef DRMP3_ZERO_MEMORY
2009
+ #define DRMP3_ZERO_MEMORY(p, sz) memset((p), 0, (sz))
2010
+ #endif
2011
+ #define DRMP3_ZERO_OBJECT(p) DRMP3_ZERO_MEMORY((p), sizeof(*(p)))
2012
+ #ifndef DRMP3_MALLOC
2013
+ #define DRMP3_MALLOC(sz) malloc((sz))
2014
+ #endif
2015
+ #ifndef DRMP3_REALLOC
2016
+ #define DRMP3_REALLOC(p, sz) realloc((p), (sz))
2017
+ #endif
2018
+ #ifndef DRMP3_FREE
2019
+ #define DRMP3_FREE(p) free((p))
2020
+ #endif
2021
+
2022
+ #define drmp3_assert DRMP3_ASSERT
2023
+ #define drmp3_copy_memory DRMP3_COPY_MEMORY
2024
+ #define drmp3_zero_memory DRMP3_ZERO_MEMORY
2025
+ #define drmp3_zero_object DRMP3_ZERO_OBJECT
2026
+ #define drmp3_malloc DRMP3_MALLOC
2027
+ #define drmp3_realloc DRMP3_REALLOC
2028
+
2029
+ #define drmp3_countof(x) (sizeof(x) / sizeof(x[0]))
2030
+ #define drmp3_max(x, y) (((x) > (y)) ? (x) : (y))
2031
+ #define drmp3_min(x, y) (((x) < (y)) ? (x) : (y))
2032
+
2033
+ #define DRMP3_DATA_CHUNK_SIZE 16384 // The size in bytes of each chunk of data to read from the MP3 stream. minimp3 recommends 16K.
2034
+
2035
+ static inline float drmp3_mix_f32(float x, float y, float a)
2036
+ {
2037
+ return x*(1-a) + y*a;
2038
+ }
2039
+
2040
+ static void drmp3_blend_f32(float* pOut, float* pInA, float* pInB, float factor, drmp3_uint32 channels)
2041
+ {
2042
+ for (drmp3_uint32 i = 0; i < channels; ++i) {
2043
+ pOut[i] = drmp3_mix_f32(pInA[i], pInB[i], factor);
2044
+ }
2045
+ }
2046
+
2047
+ void drmp3_src_cache_init(drmp3_src* pSRC, drmp3_src_cache* pCache)
2048
+ {
2049
+ drmp3_assert(pSRC != NULL);
2050
+ drmp3_assert(pCache != NULL);
2051
+
2052
+ pCache->pSRC = pSRC;
2053
+ pCache->cachedFrameCount = 0;
2054
+ pCache->iNextFrame = 0;
2055
+ }
2056
+
2057
+ drmp3_uint64 drmp3_src_cache_read_frames(drmp3_src_cache* pCache, drmp3_uint64 frameCount, float* pFramesOut)
2058
+ {
2059
+ drmp3_assert(pCache != NULL);
2060
+ drmp3_assert(pCache->pSRC != NULL);
2061
+ drmp3_assert(pCache->pSRC->onRead != NULL);
2062
+ drmp3_assert(frameCount > 0);
2063
+ drmp3_assert(pFramesOut != NULL);
2064
+
2065
+ drmp3_uint32 channels = pCache->pSRC->config.channels;
2066
+
2067
+ drmp3_uint64 totalFramesRead = 0;
2068
+ while (frameCount > 0) {
2069
+ // If there's anything in memory go ahead and copy that over first.
2070
+ drmp3_uint64 framesRemainingInMemory = pCache->cachedFrameCount - pCache->iNextFrame;
2071
+ drmp3_uint64 framesToReadFromMemory = frameCount;
2072
+ if (framesToReadFromMemory > framesRemainingInMemory) {
2073
+ framesToReadFromMemory = framesRemainingInMemory;
2074
+ }
2075
+
2076
+ drmp3_copy_memory(pFramesOut, pCache->pCachedFrames + pCache->iNextFrame*channels, (drmp3_uint32)(framesToReadFromMemory * channels * sizeof(float)));
2077
+ pCache->iNextFrame += (drmp3_uint32)framesToReadFromMemory;
2078
+
2079
+ totalFramesRead += framesToReadFromMemory;
2080
+ frameCount -= framesToReadFromMemory;
2081
+ if (frameCount == 0) {
2082
+ break;
2083
+ }
2084
+
2085
+
2086
+ // At this point there are still more frames to read from the client, so we'll need to reload the cache with fresh data.
2087
+ drmp3_assert(frameCount > 0);
2088
+ pFramesOut += framesToReadFromMemory * channels;
2089
+
2090
+ pCache->iNextFrame = 0;
2091
+ pCache->cachedFrameCount = 0;
2092
+
2093
+ drmp3_uint32 framesToReadFromClient = drmp3_countof(pCache->pCachedFrames) / pCache->pSRC->config.channels;
2094
+ if (framesToReadFromClient > pCache->pSRC->config.cacheSizeInFrames) {
2095
+ framesToReadFromClient = pCache->pSRC->config.cacheSizeInFrames;
2096
+ }
2097
+
2098
+ pCache->cachedFrameCount = (drmp3_uint32)pCache->pSRC->onRead(pCache->pSRC, framesToReadFromClient, pCache->pCachedFrames, pCache->pSRC->pUserData);
2099
+
2100
+
2101
+ // Get out of this loop if nothing was able to be retrieved.
2102
+ if (pCache->cachedFrameCount == 0) {
2103
+ break;
2104
+ }
2105
+ }
2106
+
2107
+ return totalFramesRead;
2108
+ }
2109
+
2110
+
2111
+ drmp3_uint64 drmp3_src_read_frames_passthrough(drmp3_src* pSRC, drmp3_uint64 frameCount, void* pFramesOut, drmp3_bool32 flush);
2112
+ drmp3_uint64 drmp3_src_read_frames_linear(drmp3_src* pSRC, drmp3_uint64 frameCount, void* pFramesOut, drmp3_bool32 flush);
2113
+
2114
+ drmp3_bool32 drmp3_src_init(const drmp3_src_config* pConfig, drmp3_src_read_proc onRead, void* pUserData, drmp3_src* pSRC)
2115
+ {
2116
+ if (pSRC == NULL) return DRMP3_FALSE;
2117
+ drmp3_zero_object(pSRC);
2118
+
2119
+ if (pConfig == NULL || onRead == NULL) return DRMP3_FALSE;
2120
+ if (pConfig->channels == 0 || pConfig->channels > 2) return DRMP3_FALSE;
2121
+
2122
+ drmp3_copy_memory(&pSRC->config, pConfig, sizeof (drmp3_src_config));
2123
+ pSRC->onRead = onRead;
2124
+ pSRC->pUserData = pUserData;
2125
+
2126
+ if (pSRC->config.cacheSizeInFrames > DRMP3_SRC_CACHE_SIZE_IN_FRAMES || pSRC->config.cacheSizeInFrames == 0) {
2127
+ pSRC->config.cacheSizeInFrames = DRMP3_SRC_CACHE_SIZE_IN_FRAMES;
2128
+ }
2129
+
2130
+ drmp3_src_cache_init(pSRC, &pSRC->cache);
2131
+ return DRMP3_TRUE;
2132
+ }
2133
+
2134
+ drmp3_bool32 drmp3_src_set_input_sample_rate(drmp3_src* pSRC, drmp3_uint32 sampleRateIn)
2135
+ {
2136
+ if (pSRC == NULL) return DRMP3_FALSE;
2137
+
2138
+ // Must have a sample rate of > 0.
2139
+ if (sampleRateIn == 0) {
2140
+ return DRMP3_FALSE;
2141
+ }
2142
+
2143
+ pSRC->config.sampleRateIn = sampleRateIn;
2144
+ return DRMP3_TRUE;
2145
+ }
2146
+
2147
+ drmp3_bool32 drmp3_src_set_output_sample_rate(drmp3_src* pSRC, drmp3_uint32 sampleRateOut)
2148
+ {
2149
+ if (pSRC == NULL) return DRMP3_FALSE;
2150
+
2151
+ // Must have a sample rate of > 0.
2152
+ if (sampleRateOut == 0) {
2153
+ return DRMP3_FALSE;
2154
+ }
2155
+
2156
+ pSRC->config.sampleRateOut = sampleRateOut;
2157
+ return DRMP3_TRUE;
2158
+ }
2159
+
2160
+ drmp3_uint64 drmp3_src_read_frames_ex(drmp3_src* pSRC, drmp3_uint64 frameCount, void* pFramesOut, drmp3_bool32 flush)
2161
+ {
2162
+ if (pSRC == NULL || frameCount == 0 || pFramesOut == NULL) return 0;
2163
+
2164
+ drmp3_src_algorithm algorithm = pSRC->config.algorithm;
2165
+
2166
+ // Always use passthrough if the sample rates are the same.
2167
+ if (pSRC->config.sampleRateIn == pSRC->config.sampleRateOut) {
2168
+ algorithm = drmp3_src_algorithm_none;
2169
+ }
2170
+
2171
+ // Could just use a function pointer instead of a switch for this...
2172
+ switch (algorithm)
2173
+ {
2174
+ case drmp3_src_algorithm_none: return drmp3_src_read_frames_passthrough(pSRC, frameCount, pFramesOut, flush);
2175
+ case drmp3_src_algorithm_linear: return drmp3_src_read_frames_linear(pSRC, frameCount, pFramesOut, flush);
2176
+ default: return 0;
2177
+ }
2178
+ }
2179
+
2180
+ drmp3_uint64 drmp3_src_read_frames(drmp3_src* pSRC, drmp3_uint64 frameCount, void* pFramesOut)
2181
+ {
2182
+ return drmp3_src_read_frames_ex(pSRC, frameCount, pFramesOut, DRMP3_FALSE);
2183
+ }
2184
+
2185
+ drmp3_uint64 drmp3_src_read_frames_passthrough(drmp3_src* pSRC, drmp3_uint64 frameCount, void* pFramesOut, drmp3_bool32 flush)
2186
+ {
2187
+ drmp3_assert(pSRC != NULL);
2188
+ drmp3_assert(frameCount > 0);
2189
+ drmp3_assert(pFramesOut != NULL);
2190
+
2191
+ (void)flush; // Passthrough need not care about flushing.
2192
+ return pSRC->onRead(pSRC, frameCount, pFramesOut, pSRC->pUserData);
2193
+ }
2194
+
2195
+ drmp3_uint64 drmp3_src_read_frames_linear(drmp3_src* pSRC, drmp3_uint64 frameCount, void* pFramesOut, drmp3_bool32 flush)
2196
+ {
2197
+ drmp3_assert(pSRC != NULL);
2198
+ drmp3_assert(frameCount > 0);
2199
+ drmp3_assert(pFramesOut != NULL);
2200
+
2201
+ // For linear SRC, the bin is only 2 frames: 1 prior, 1 future.
2202
+
2203
+ // Load the bin if necessary.
2204
+ if (!pSRC->algo.linear.isPrevFramesLoaded) {
2205
+ drmp3_uint64 framesRead = drmp3_src_cache_read_frames(&pSRC->cache, 1, pSRC->bin);
2206
+ if (framesRead == 0) {
2207
+ return 0;
2208
+ }
2209
+ pSRC->algo.linear.isPrevFramesLoaded = DRMP3_TRUE;
2210
+ }
2211
+ if (!pSRC->algo.linear.isNextFramesLoaded) {
2212
+ drmp3_uint64 framesRead = drmp3_src_cache_read_frames(&pSRC->cache, 1, pSRC->bin + pSRC->config.channels);
2213
+ if (framesRead == 0) {
2214
+ return 0;
2215
+ }
2216
+ pSRC->algo.linear.isNextFramesLoaded = DRMP3_TRUE;
2217
+ }
2218
+
2219
+ float factor = (float)pSRC->config.sampleRateIn / pSRC->config.sampleRateOut;
2220
+
2221
+ drmp3_uint64 totalFramesRead = 0;
2222
+ while (frameCount > 0) {
2223
+ // The bin is where the previous and next frames are located.
2224
+ float* pPrevFrame = pSRC->bin;
2225
+ float* pNextFrame = pSRC->bin + pSRC->config.channels;
2226
+
2227
+ drmp3_blend_f32((float*)pFramesOut, pPrevFrame, pNextFrame, pSRC->algo.linear.alpha, pSRC->config.channels);
2228
+
2229
+ pSRC->algo.linear.alpha += factor;
2230
+
2231
+ // The new alpha value is how we determine whether or not we need to read fresh frames.
2232
+ drmp3_uint32 framesToReadFromClient = (drmp3_uint32)pSRC->algo.linear.alpha;
2233
+ pSRC->algo.linear.alpha = pSRC->algo.linear.alpha - framesToReadFromClient;
2234
+
2235
+ for (drmp3_uint32 i = 0; i < framesToReadFromClient; ++i) {
2236
+ for (drmp3_uint32 j = 0; j < pSRC->config.channels; ++j) {
2237
+ pPrevFrame[j] = pNextFrame[j];
2238
+ }
2239
+
2240
+ drmp3_uint64 framesRead = drmp3_src_cache_read_frames(&pSRC->cache, 1, pNextFrame);
2241
+ if (framesRead == 0) {
2242
+ for (drmp3_uint32 j = 0; j < pSRC->config.channels; ++j) {
2243
+ pNextFrame[j] = 0;
2244
+ }
2245
+
2246
+ if (pSRC->algo.linear.isNextFramesLoaded) {
2247
+ pSRC->algo.linear.isNextFramesLoaded = DRMP3_FALSE;
2248
+ } else {
2249
+ if (flush) {
2250
+ pSRC->algo.linear.isPrevFramesLoaded = DRMP3_FALSE;
2251
+ }
2252
+ }
2253
+
2254
+ break;
2255
+ }
2256
+ }
2257
+
2258
+ pFramesOut = (drmp3_uint8*)pFramesOut + (1 * pSRC->config.channels * sizeof(float));
2259
+ frameCount -= 1;
2260
+ totalFramesRead += 1;
2261
+
2262
+ // If there's no frames available we need to get out of this loop.
2263
+ if (!pSRC->algo.linear.isNextFramesLoaded && (!flush || !pSRC->algo.linear.isPrevFramesLoaded)) {
2264
+ break;
2265
+ }
2266
+ }
2267
+
2268
+ return totalFramesRead;
2269
+ }
2270
+
2271
+
2272
+
2273
+ static drmp3_bool32 drmp3_decode_next_frame(drmp3* pMP3)
2274
+ {
2275
+ drmp3_assert(pMP3 != NULL);
2276
+ drmp3_assert(pMP3->onRead != NULL);
2277
+
2278
+ if (pMP3->atEnd) {
2279
+ return DRMP3_FALSE;
2280
+ }
2281
+
2282
+ do
2283
+ {
2284
+ // minimp3 recommends doing data submission in 16K chunks. If we don't have at least 16K bytes available, get more.
2285
+ if (pMP3->dataSize < DRMP3_DATA_CHUNK_SIZE) {
2286
+ if (pMP3->dataCapacity < DRMP3_DATA_CHUNK_SIZE) {
2287
+ pMP3->dataCapacity = DRMP3_DATA_CHUNK_SIZE;
2288
+ drmp3_uint8* pNewData = (drmp3_uint8*)drmp3_realloc(pMP3->pData, pMP3->dataCapacity);
2289
+ if (pNewData == NULL) {
2290
+ return DRMP3_FALSE; // Out of memory.
2291
+ }
2292
+
2293
+ pMP3->pData = pNewData;
2294
+ }
2295
+
2296
+ size_t bytesRead = pMP3->onRead(pMP3->pUserData, pMP3->pData + pMP3->dataSize, (pMP3->dataCapacity - pMP3->dataSize));
2297
+ if (bytesRead == 0) {
2298
+ pMP3->atEnd = DRMP3_TRUE;
2299
+ return DRMP3_FALSE; // No data.
2300
+ }
2301
+
2302
+ pMP3->dataSize += bytesRead;
2303
+ }
2304
+
2305
+ if (pMP3->dataSize > INT_MAX) {
2306
+ pMP3->atEnd = DRMP3_TRUE;
2307
+ return DRMP3_FALSE; // File too big.
2308
+ }
2309
+
2310
+ drmp3dec_frame_info info;
2311
+ drmp3_uint32 samplesRead = drmp3dec_decode_frame(&pMP3->decoder, pMP3->pData, (int)pMP3->dataSize, pMP3->frames, &info); // <-- Safe size_t -> int conversion thanks to the check above.
2312
+ if (samplesRead != 0) {
2313
+ size_t leftoverDataSize = (pMP3->dataSize - (size_t)info.frame_bytes);
2314
+ for (size_t i = 0; i < leftoverDataSize; ++i) {
2315
+ pMP3->pData[i] = pMP3->pData[i + (size_t)info.frame_bytes];
2316
+ }
2317
+
2318
+ pMP3->dataSize = leftoverDataSize;
2319
+ pMP3->framesConsumed = 0;
2320
+ pMP3->framesRemaining = samplesRead;
2321
+ pMP3->frameChannels = info.channels;
2322
+ pMP3->frameSampleRate = info.hz;
2323
+ drmp3_src_set_input_sample_rate(&pMP3->src, pMP3->frameSampleRate);
2324
+ break;
2325
+ } else {
2326
+ // Need more data. minimp3 recommends doing data submission in 16K chunks.
2327
+ if (pMP3->dataCapacity == pMP3->dataSize) {
2328
+ // No room. Expand.
2329
+ pMP3->dataCapacity += DRMP3_DATA_CHUNK_SIZE;
2330
+ drmp3_uint8* pNewData = (drmp3_uint8*)drmp3_realloc(pMP3->pData, pMP3->dataCapacity);
2331
+ if (pNewData == NULL) {
2332
+ return DRMP3_FALSE; // Out of memory.
2333
+ }
2334
+
2335
+ pMP3->pData = pNewData;
2336
+ }
2337
+
2338
+ // Fill in a chunk.
2339
+ size_t bytesRead = pMP3->onRead(pMP3->pUserData, pMP3->pData + pMP3->dataSize, (pMP3->dataCapacity - pMP3->dataSize));
2340
+ if (bytesRead == 0) {
2341
+ pMP3->atEnd = DRMP3_TRUE;
2342
+ return DRMP3_FALSE; // Error reading more data.
2343
+ }
2344
+
2345
+ pMP3->dataSize += bytesRead;
2346
+ }
2347
+ } while (DRMP3_TRUE);
2348
+
2349
+ return DRMP3_TRUE;
2350
+ }
2351
+
2352
+ static drmp3_uint64 drmp3_read_src(drmp3_src* pSRC, drmp3_uint64 frameCount, void* pFramesOut, void* pUserData)
2353
+ {
2354
+ drmp3* pMP3 = (drmp3*)pUserData;
2355
+ drmp3_assert(pMP3 != NULL);
2356
+ drmp3_assert(pMP3->onRead != NULL);
2357
+
2358
+ float* pFramesOutF = (float*)pFramesOut;
2359
+ drmp3_uint32 totalFramesRead = 0;
2360
+
2361
+ while (frameCount > 0) {
2362
+ // Read from the in-memory buffer first.
2363
+ while (pMP3->framesRemaining > 0 && frameCount > 0) {
2364
+ if (pMP3->frameChannels == 1) {
2365
+ if (pMP3->channels == 1) {
2366
+ // Mono -> Mono.
2367
+ pFramesOutF[0] = pMP3->frames[pMP3->framesConsumed] / 32768.0f;
2368
+ } else {
2369
+ // Mono -> Stereo.
2370
+ pFramesOutF[0] = pMP3->frames[pMP3->framesConsumed] / 32768.0f;
2371
+ pFramesOutF[1] = pMP3->frames[pMP3->framesConsumed] / 32768.0f;
2372
+ }
2373
+ } else {
2374
+ if (pMP3->channels == 1) {
2375
+ // Stereo -> Mono
2376
+ float sample = 0;
2377
+ sample += pMP3->frames[(pMP3->framesConsumed*pMP3->frameChannels)+0] / 32768.0f;
2378
+ sample += pMP3->frames[(pMP3->framesConsumed*pMP3->frameChannels)+1] / 32768.0f;
2379
+ pFramesOutF[0] = sample * 0.5f;
2380
+ } else {
2381
+ // Stereo -> Stereo
2382
+ pFramesOutF[0] = pMP3->frames[(pMP3->framesConsumed*pMP3->frameChannels)+0] / 32768.0f;
2383
+ pFramesOutF[1] = pMP3->frames[(pMP3->framesConsumed*pMP3->frameChannels)+1] / 32768.0f;
2384
+ }
2385
+ }
2386
+
2387
+ pMP3->framesConsumed += 1;
2388
+ pMP3->framesRemaining -= 1;
2389
+ frameCount -= 1;
2390
+ totalFramesRead += 1;
2391
+ pFramesOutF += pSRC->config.channels;
2392
+ }
2393
+
2394
+ if (frameCount == 0) {
2395
+ break;
2396
+ }
2397
+
2398
+ drmp3_assert(pMP3->framesRemaining == 0);
2399
+
2400
+ // At this point we have exhausted our in-memory buffer so we need to re-fill. Note that the sample rate may have changed
2401
+ // at this point which means we'll also need to update our sample rate conversion pipeline.
2402
+ if (!drmp3_decode_next_frame(pMP3)) {
2403
+ break;
2404
+ }
2405
+ }
2406
+
2407
+ return totalFramesRead;
2408
+ }
2409
+
2410
+ drmp3_bool32 drmp3_init_internal(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, const drmp3_config* pConfig)
2411
+ {
2412
+ drmp3_assert(pMP3 != NULL);
2413
+ drmp3_assert(onRead != NULL);
2414
+
2415
+ // This function assumes the output object has already been reset to 0. Do not do that here, otherwise things will break.
2416
+ drmp3dec_init(&pMP3->decoder);
2417
+
2418
+ // The config can be null in which case we use defaults.
2419
+ drmp3_config config;
2420
+ if (pConfig != NULL) {
2421
+ drmp3_copy_memory(&config, pConfig, sizeof (drmp3_config));
2422
+ } else {
2423
+ drmp3_zero_object(&config);
2424
+ }
2425
+
2426
+ pMP3->channels = config.outputChannels;
2427
+ if (pMP3->channels == 0) {
2428
+ pMP3->channels = DR_MP3_DEFAULT_CHANNELS;
2429
+ }
2430
+
2431
+ // Cannot have more than 2 channels.
2432
+ if (pMP3->channels > 2) {
2433
+ pMP3->channels = 2;
2434
+ }
2435
+
2436
+ pMP3->sampleRate = config.outputSampleRate;
2437
+ if (pMP3->sampleRate == 0) {
2438
+ pMP3->sampleRate = DR_MP3_DEFAULT_SAMPLE_RATE;
2439
+ }
2440
+
2441
+ pMP3->onRead = onRead;
2442
+ pMP3->onSeek = onSeek;
2443
+ pMP3->pUserData = pUserData;
2444
+
2445
+ // We need a sample rate converter for converting the sample rate from the MP3 frames to the requested output sample rate.
2446
+ drmp3_src_config srcConfig;
2447
+ drmp3_zero_object(&srcConfig);
2448
+ srcConfig.sampleRateIn = DR_MP3_DEFAULT_SAMPLE_RATE;
2449
+ srcConfig.sampleRateOut = pMP3->sampleRate;
2450
+ srcConfig.channels = pMP3->channels;
2451
+ srcConfig.algorithm = drmp3_src_algorithm_linear;
2452
+ if (!drmp3_src_init(&srcConfig, drmp3_read_src, pMP3, &pMP3->src)) {
2453
+ return DRMP3_FALSE;
2454
+ }
2455
+
2456
+ // Decode the first frame to confirm that it is indeed a valid MP3 stream.
2457
+ if (!drmp3_decode_next_frame(pMP3)) {
2458
+ return DRMP3_FALSE; // Not a valid MP3 stream.
2459
+ }
2460
+
2461
+ return DRMP3_TRUE;
2462
+ }
2463
+
2464
+ drmp3_bool32 drmp3_init(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, const drmp3_config* pConfig)
2465
+ {
2466
+ if (pMP3 == NULL || onRead == NULL) {
2467
+ return DRMP3_FALSE;
2468
+ }
2469
+
2470
+ drmp3_zero_object(pMP3);
2471
+ return drmp3_init_internal(pMP3, onRead, onSeek, pUserData, pConfig);
2472
+ }
2473
+
2474
+
2475
+ static size_t drmp3__on_read_memory(void* pUserData, void* pBufferOut, size_t bytesToRead)
2476
+ {
2477
+ drmp3* pMP3 = (drmp3*)pUserData;
2478
+ drmp3_assert(pMP3 != NULL);
2479
+ drmp3_assert(pMP3->memory.dataSize >= pMP3->memory.currentReadPos);
2480
+
2481
+ size_t bytesRemaining = pMP3->memory.dataSize - pMP3->memory.currentReadPos;
2482
+ if (bytesToRead > bytesRemaining) {
2483
+ bytesToRead = bytesRemaining;
2484
+ }
2485
+
2486
+ if (bytesToRead > 0) {
2487
+ drmp3_copy_memory(pBufferOut, pMP3->memory.pData + pMP3->memory.currentReadPos, bytesToRead);
2488
+ pMP3->memory.currentReadPos += bytesToRead;
2489
+ }
2490
+
2491
+ return bytesToRead;
2492
+ }
2493
+
2494
+ static drmp3_bool32 drmp3__on_seek_memory(void* pUserData, int byteOffset, drmp3_seek_origin origin)
2495
+ {
2496
+ drmp3* pMP3 = (drmp3*)pUserData;
2497
+ drmp3_assert(pMP3 != NULL);
2498
+
2499
+ if (origin == drmp3_seek_origin_current) {
2500
+ if (byteOffset > 0) {
2501
+ if (pMP3->memory.currentReadPos + byteOffset > pMP3->memory.dataSize) {
2502
+ byteOffset = (int)(pMP3->memory.dataSize - pMP3->memory.currentReadPos); // Trying to seek too far forward.
2503
+ }
2504
+ } else {
2505
+ if (pMP3->memory.currentReadPos < (size_t)-byteOffset) {
2506
+ byteOffset = -(int)pMP3->memory.currentReadPos; // Trying to seek too far backwards.
2507
+ }
2508
+ }
2509
+
2510
+ // This will never underflow thanks to the clamps above.
2511
+ pMP3->memory.currentReadPos += byteOffset;
2512
+ } else {
2513
+ if ((drmp3_uint32)byteOffset <= pMP3->memory.dataSize) {
2514
+ pMP3->memory.currentReadPos = byteOffset;
2515
+ } else {
2516
+ pMP3->memory.currentReadPos = pMP3->memory.dataSize; // Trying to seek too far forward.
2517
+ }
2518
+ }
2519
+
2520
+ return DRMP3_TRUE;
2521
+ }
2522
+
2523
+ drmp3_bool32 drmp3_init_memory(drmp3* pMP3, const void* pData, size_t dataSize, const drmp3_config* pConfig)
2524
+ {
2525
+ if (pMP3 == NULL) {
2526
+ return DRMP3_FALSE;
2527
+ }
2528
+
2529
+ drmp3_zero_object(pMP3);
2530
+
2531
+ if (pData == NULL || dataSize == 0) {
2532
+ return DRMP3_FALSE;
2533
+ }
2534
+
2535
+ pMP3->memory.pData = (const drmp3_uint8*)pData;
2536
+ pMP3->memory.dataSize = dataSize;
2537
+ pMP3->memory.currentReadPos = 0;
2538
+
2539
+ return drmp3_init_internal(pMP3, drmp3__on_read_memory, drmp3__on_seek_memory, pMP3, pConfig);
2540
+ }
2541
+
2542
+
2543
+ #ifndef DR_MP3_NO_STDIO
2544
+ #include <stdio.h>
2545
+
2546
+ static size_t drmp3__on_read_stdio(void* pUserData, void* pBufferOut, size_t bytesToRead)
2547
+ {
2548
+ return fread(pBufferOut, 1, bytesToRead, (FILE*)pUserData);
2549
+ }
2550
+
2551
+ static drmp3_bool32 drmp3__on_seek_stdio(void* pUserData, int offset, drmp3_seek_origin origin)
2552
+ {
2553
+ return fseek((FILE*)pUserData, offset, (origin == drmp3_seek_origin_current) ? SEEK_CUR : SEEK_SET) == 0;
2554
+ }
2555
+
2556
+ drmp3_bool32 drmp3_init_file(drmp3* pMP3, const char* filePath, const drmp3_config* pConfig)
2557
+ {
2558
+ FILE* pFile;
2559
+ #if defined(_MSC_VER) && _MSC_VER >= 1400
2560
+ if (fopen_s(&pFile, filePath, "rb") != 0) {
2561
+ return DRMP3_FALSE;
2562
+ }
2563
+ #else
2564
+ pFile = fopen(filePath, "rb");
2565
+ if (pFile == NULL) {
2566
+ return DRMP3_FALSE;
2567
+ }
2568
+ #endif
2569
+
2570
+ return drmp3_init(pMP3, drmp3__on_read_stdio, drmp3__on_seek_stdio, (void*)pFile, pConfig);
2571
+ }
2572
+ #endif
2573
+
2574
+ void drmp3_uninit(drmp3* pMP3)
2575
+ {
2576
+ if (pMP3 == NULL) return;
2577
+
2578
+ #ifndef DR_MP3_NO_STDIO
2579
+ if (pMP3->onRead == drmp3__on_read_stdio) {
2580
+ fclose((FILE*)pMP3->pUserData);
2581
+ }
2582
+ #endif
2583
+
2584
+ drmp3_free(pMP3->pData);
2585
+ }
2586
+
2587
+ drmp3_uint64 drmp3_read_f32(drmp3* pMP3, drmp3_uint64 framesToRead, float* pBufferOut)
2588
+ {
2589
+ if (pMP3 == NULL || pMP3->onRead == NULL) return 0;
2590
+
2591
+ drmp3_uint64 totalFramesRead = 0;
2592
+
2593
+ if (pBufferOut == NULL) {
2594
+ float temp[4096];
2595
+ while (framesToRead > 0) {
2596
+ drmp3_uint64 framesToReadRightNow = sizeof(temp)/sizeof(temp[0]) / pMP3->channels;
2597
+ if (framesToReadRightNow > framesToRead) {
2598
+ framesToReadRightNow = framesToRead;
2599
+ }
2600
+
2601
+ drmp3_uint64 framesJustRead = drmp3_read_f32(pMP3, framesToReadRightNow, temp);
2602
+ if (framesJustRead == 0) {
2603
+ break;
2604
+ }
2605
+
2606
+ framesToRead -= framesJustRead;
2607
+ totalFramesRead += framesJustRead;
2608
+ }
2609
+ } else {
2610
+ totalFramesRead = drmp3_src_read_frames_ex(&pMP3->src, framesToRead, pBufferOut, DRMP3_TRUE);
2611
+ }
2612
+
2613
+ return totalFramesRead;
2614
+ }
2615
+
2616
+ drmp3_bool32 drmp3_seek_to_frame(drmp3* pMP3, drmp3_uint64 frameIndex)
2617
+ {
2618
+ if (pMP3 == NULL || pMP3->onSeek == NULL) return DRMP3_FALSE;
2619
+
2620
+ // Seek to the start of the stream to begin with.
2621
+ if (!pMP3->onSeek(pMP3->pUserData, 0, drmp3_seek_origin_start)) {
2622
+ return DRMP3_FALSE;
2623
+ }
2624
+
2625
+ // Clear any cached data.
2626
+ pMP3->framesConsumed = 0;
2627
+ pMP3->framesRemaining = 0;
2628
+ pMP3->dataSize = 0;
2629
+ pMP3->atEnd = DRMP3_FALSE;
2630
+
2631
+ // TODO: Optimize.
2632
+ //
2633
+ // This is inefficient. We simply read frames from the start of the stream.
2634
+ drmp3_uint64 framesRead = drmp3_read_f32(pMP3, frameIndex, NULL);
2635
+ if (framesRead != frameIndex) {
2636
+ return DRMP3_FALSE;
2637
+ }
2638
+
2639
+ return DRMP3_TRUE;
2640
+ }
2641
+
2642
+
2643
+
2644
+ float* drmp3__full_decode_and_close_f32(drmp3* pMP3, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount)
2645
+ {
2646
+ drmp3_assert(pMP3 != NULL);
2647
+
2648
+ drmp3_uint64 totalFramesRead = 0;
2649
+ drmp3_uint64 framesCapacity = 0;
2650
+ float* pFrames = NULL;
2651
+
2652
+ float temp[4096];
2653
+ for (;;) {
2654
+ drmp3_uint64 framesToReadRightNow = drmp3_countof(temp) / pMP3->channels;
2655
+ drmp3_uint64 framesJustRead = drmp3_read_f32(pMP3, framesToReadRightNow, temp);
2656
+ if (framesJustRead == 0) {
2657
+ break;
2658
+ }
2659
+
2660
+ // Reallocate the output buffer if there's not enough room.
2661
+ if (framesCapacity < totalFramesRead + framesJustRead) {
2662
+ framesCapacity *= 2;
2663
+ if (framesCapacity < totalFramesRead + framesJustRead) {
2664
+ framesCapacity = totalFramesRead + framesJustRead;
2665
+ }
2666
+
2667
+ drmp3_uint64 newFramesBufferSize = framesCapacity*pMP3->channels*sizeof(float);
2668
+ if (newFramesBufferSize > SIZE_MAX) {
2669
+ break;
2670
+ }
2671
+
2672
+ float* pNewFrames = (float*)drmp3_realloc(pFrames, (size_t)newFramesBufferSize);
2673
+ if (pNewFrames == NULL) {
2674
+ drmp3_free(pFrames);
2675
+ break;
2676
+ }
2677
+
2678
+ pFrames = pNewFrames;
2679
+ }
2680
+
2681
+ drmp3_copy_memory(pFrames + totalFramesRead*pMP3->channels, temp, (size_t)(framesJustRead*pMP3->channels*sizeof(float)));
2682
+ totalFramesRead += framesJustRead;
2683
+
2684
+ // If the number of frames we asked for is less that what we actually read it means we've reached the end.
2685
+ if (framesJustRead != framesToReadRightNow) {
2686
+ break;
2687
+ }
2688
+ }
2689
+
2690
+ if (pConfig != NULL) {
2691
+ pConfig->outputChannels = pMP3->channels;
2692
+ pConfig->outputSampleRate = pMP3->sampleRate;
2693
+ }
2694
+
2695
+ drmp3_uninit(pMP3);
2696
+
2697
+ if (pTotalFrameCount) *pTotalFrameCount = totalFramesRead;
2698
+ return pFrames;
2699
+ }
2700
+
2701
+ float* drmp3_open_and_decode_f32(drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount)
2702
+ {
2703
+ drmp3 mp3;
2704
+ if (!drmp3_init(&mp3, onRead, onSeek, pUserData, pConfig)) {
2705
+ return NULL;
2706
+ }
2707
+
2708
+ return drmp3__full_decode_and_close_f32(&mp3, pConfig, pTotalFrameCount);
2709
+ }
2710
+
2711
+ float* drmp3_open_and_decode_memory_f32(const void* pData, size_t dataSize, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount)
2712
+ {
2713
+ drmp3 mp3;
2714
+ if (!drmp3_init_memory(&mp3, pData, dataSize, pConfig)) {
2715
+ return NULL;
2716
+ }
2717
+
2718
+ return drmp3__full_decode_and_close_f32(&mp3, pConfig, pTotalFrameCount);
2719
+ }
2720
+
2721
+ #ifndef DR_MP3_NO_STDIO
2722
+ float* drmp3_open_and_decode_file_f32(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount)
2723
+ {
2724
+ drmp3 mp3;
2725
+ if (!drmp3_init_file(&mp3, filePath, pConfig)) {
2726
+ return NULL;
2727
+ }
2728
+
2729
+ return drmp3__full_decode_and_close_f32(&mp3, pConfig, pTotalFrameCount);
2730
+ }
2731
+ #endif
2732
+
2733
+ void drmp3_free(void* p)
2734
+ {
2735
+ DRMP3_FREE(p);
2736
+ }
2737
+
2738
+ #endif /*DR_MP3_IMPLEMENTATION*/
2739
+
2740
+
2741
+ // DIFFERENCES BETWEEN minimp3 AND dr_mp3
2742
+ // ======================================
2743
+ // - First, keep in mind that minimp3 (https://github.com/lieff/minimp3) is where all the real work was done. All of the
2744
+ // code relating to the actual decoding remains mostly unmodified, apart from some namespacing changes.
2745
+ // - dr_mp3 adds a pulling style API which allows you to deliver raw data via callbacks. So, rather than pushing data
2746
+ // to the decoder, the decoder _pulls_ data from your callbacks.
2747
+ // - In addition to callbacks, a decoder can be initialized from a block of memory and a file.
2748
+ // - The dr_mp3 pull API reads PCM frames rather than whole MP3 frames.
2749
+ // - dr_mp3 adds convenience APIs for opening and decoding entire files in one go.
2750
+ // - dr_mp3 is fully namespaced, including the implementation section, which is more suitable when compiling projects
2751
+ // as a single translation unit (aka unity builds). At the time of writing this, a unity build is not possible when
2752
+ // using minimp3 in conjunction with stb_vorbis. dr_mp3 addresses this.
2753
+
2754
+
2755
+ // REVISION HISTORY
2756
+ // ===============
2757
+ //
2758
+ // v0.2.7 - 2018-07-13
2759
+ // - Bring up to date with minimp3.
2760
+ //
2761
+ // v0.2.6 - 2018-07-12
2762
+ // - Bring up to date with minimp3.
2763
+ //
2764
+ // v0.2.5 - 2018-06-22
2765
+ // - Bring up to date with minimp3.
2766
+ //
2767
+ // v0.2.4 - 2018-05-12
2768
+ // - Bring up to date with minimp3.
2769
+ //
2770
+ // v0.2.3 - 2018-04-29
2771
+ // - Fix TCC build.
2772
+ //
2773
+ // v0.2.2 - 2018-04-28
2774
+ // - Fix bug when opening a decoder from memory.
2775
+ //
2776
+ // v0.2.1 - 2018-04-27
2777
+ // - Efficiency improvements when the decoder reaches the end of the stream.
2778
+ //
2779
+ // v0.2 - 2018-04-21
2780
+ // - Bring up to date with minimp3.
2781
+ // - Start using major.minor.revision versioning.
2782
+ //
2783
+ // v0.1d - 2018-03-30
2784
+ // - Bring up to date with minimp3.
2785
+ //
2786
+ // v0.1c - 2018-03-11
2787
+ // - Fix C++ build error.
2788
+ //
2789
+ // v0.1b - 2018-03-07
2790
+ // - Bring up to date with minimp3.
2791
+ //
2792
+ // v0.1a - 2018-02-28
2793
+ // - Fix compilation error on GCC/Clang.
2794
+ // - Fix some warnings.
2795
+ //
2796
+ // v0.1 - 2018-02-xx
2797
+ // - Initial versioned release.
2798
+
2799
+
2800
+ /*
2801
+ This is free and unencumbered software released into the public domain.
2802
+
2803
+ Anyone is free to copy, modify, publish, use, compile, sell, or
2804
+ distribute this software, either in source code form or as a compiled
2805
+ binary, for any purpose, commercial or non-commercial, and by any
2806
+ means.
2807
+
2808
+ In jurisdictions that recognize copyright laws, the author or authors
2809
+ of this software dedicate any and all copyright interest in the
2810
+ software to the public domain. We make this dedication for the benefit
2811
+ of the public at large and to the detriment of our heirs and
2812
+ successors. We intend this dedication to be an overt act of
2813
+ relinquishment in perpetuity of all present and future rights to this
2814
+ software under copyright law.
2815
+
2816
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2817
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2818
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
2819
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
2820
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
2821
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2822
+ OTHER DEALINGS IN THE SOFTWARE.
2823
+
2824
+ For more information, please refer to <http://unlicense.org/>
2825
+ */
2826
+
2827
+ /*
2828
+ https://github.com/lieff/minimp3
2829
+ To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide.
2830
+ This software is distributed without any warranty.
2831
+ See <http://creativecommons.org/publicdomain/zero/1.0/>.
2832
+ */