gosu 1.4.6 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (320) hide show
  1. checksums.yaml +4 -4
  2. data/COPYING +2 -1
  3. data/dependencies/SDL/include/SDL3/SDL.h +91 -0
  4. data/dependencies/SDL/include/SDL3/SDL_assert.h +710 -0
  5. data/dependencies/SDL/include/SDL3/SDL_asyncio.h +550 -0
  6. data/dependencies/SDL/include/SDL3/SDL_atomic.h +692 -0
  7. data/dependencies/SDL/include/SDL3/SDL_audio.h +2382 -0
  8. data/dependencies/SDL/include/SDL3/SDL_begin_code.h +553 -0
  9. data/dependencies/SDL/include/{SDL_bits.h → SDL3/SDL_bits.h} +47 -27
  10. data/dependencies/SDL/include/{SDL_blendmode.h → SDL3/SDL_blendmode.h} +59 -55
  11. data/dependencies/SDL/include/SDL3/SDL_camera.h +535 -0
  12. data/dependencies/SDL/include/SDL3/SDL_clipboard.h +331 -0
  13. data/dependencies/SDL/include/{close_code.h → SDL3/SDL_close_code.h} +9 -8
  14. data/dependencies/SDL/include/{SDL_types.h → SDL3/SDL_copying.h} +2 -9
  15. data/dependencies/SDL/include/SDL3/SDL_cpuinfo.h +374 -0
  16. data/dependencies/SDL/include/SDL3/SDL_dialog.h +343 -0
  17. data/dependencies/SDL/include/SDL3/SDL_dlopennote.h +234 -0
  18. data/dependencies/SDL/include/{SDL_egl.h → SDL3/SDL_egl.h} +11 -8
  19. data/dependencies/SDL/include/SDL3/SDL_endian.h +645 -0
  20. data/dependencies/SDL/include/SDL3/SDL_error.h +226 -0
  21. data/dependencies/SDL/include/SDL3/SDL_events.h +1651 -0
  22. data/dependencies/SDL/include/SDL3/SDL_filesystem.h +537 -0
  23. data/dependencies/SDL/include/SDL3/SDL_gamepad.h +1657 -0
  24. data/dependencies/SDL/include/SDL3/SDL_gpu.h +4637 -0
  25. data/dependencies/SDL/include/SDL3/SDL_guid.h +106 -0
  26. data/dependencies/SDL/include/SDL3/SDL_haptic.h +1461 -0
  27. data/dependencies/SDL/include/SDL3/SDL_hidapi.h +571 -0
  28. data/dependencies/SDL/include/SDL3/SDL_hints.h +5041 -0
  29. data/dependencies/SDL/include/SDL3/SDL_init.h +508 -0
  30. data/dependencies/SDL/include/SDL3/SDL_intrin.h +410 -0
  31. data/dependencies/SDL/include/SDL3/SDL_iostream.h +1379 -0
  32. data/dependencies/SDL/include/SDL3/SDL_joystick.h +1385 -0
  33. data/dependencies/SDL/include/SDL3/SDL_keyboard.h +615 -0
  34. data/dependencies/SDL/include/SDL3/SDL_keycode.h +348 -0
  35. data/dependencies/SDL/include/SDL3/SDL_loadso.h +145 -0
  36. data/dependencies/SDL/include/{SDL_locale.h → SDL3/SDL_locale.h} +40 -24
  37. data/dependencies/SDL/include/SDL3/SDL_log.h +541 -0
  38. data/dependencies/SDL/include/SDL3/SDL_main.h +704 -0
  39. data/dependencies/SDL/include/SDL3/SDL_main_impl.h +149 -0
  40. data/dependencies/SDL/include/{SDL_messagebox.h → SDL3/SDL_messagebox.h} +85 -48
  41. data/dependencies/SDL/include/{SDL_metal.h → SDL3/SDL_metal.h} +29 -29
  42. data/dependencies/SDL/include/{SDL_misc.h → SDL3/SDL_misc.h} +13 -12
  43. data/dependencies/SDL/include/SDL3/SDL_mouse.h +813 -0
  44. data/dependencies/SDL/include/SDL3/SDL_mutex.h +1117 -0
  45. data/dependencies/SDL/include/SDL3/SDL_oldnames.h +1339 -0
  46. data/dependencies/SDL/include/{SDL_opengl.h → SDL3/SDL_opengl.h} +1247 -278
  47. data/dependencies/SDL/include/{SDL_opengl_glext.h → SDL3/SDL_opengl_glext.h} +5 -1
  48. data/dependencies/SDL/include/{SDL_opengles.h → SDL3/SDL_opengles.h} +6 -7
  49. data/dependencies/SDL/include/{SDL_opengles2.h → SDL3/SDL_opengles2.h} +10 -11
  50. data/dependencies/SDL/include/SDL3/SDL_pen.h +198 -0
  51. data/dependencies/SDL/include/SDL3/SDL_pixels.h +1441 -0
  52. data/dependencies/SDL/include/{SDL_test_log.h → SDL3/SDL_platform.h} +25 -26
  53. data/dependencies/SDL/include/SDL3/SDL_platform_defines.h +497 -0
  54. data/dependencies/SDL/include/{SDL_power.h → SDL3/SDL_power.h} +40 -20
  55. data/dependencies/SDL/include/SDL3/SDL_process.h +441 -0
  56. data/dependencies/SDL/include/SDL3/SDL_properties.h +572 -0
  57. data/dependencies/SDL/include/SDL3/SDL_rect.h +528 -0
  58. data/dependencies/SDL/include/SDL3/SDL_render.h +3034 -0
  59. data/dependencies/SDL/include/SDL3/SDL_revision.h +56 -0
  60. data/dependencies/SDL/include/{SDL_scancode.h → SDL3/SDL_scancode.h} +60 -68
  61. data/dependencies/SDL/include/SDL3/SDL_sensor.h +321 -0
  62. data/dependencies/SDL/include/SDL3/SDL_stdinc.h +6186 -0
  63. data/dependencies/SDL/include/SDL3/SDL_storage.h +686 -0
  64. data/dependencies/SDL/include/SDL3/SDL_surface.h +1769 -0
  65. data/dependencies/SDL/include/SDL3/SDL_system.h +840 -0
  66. data/dependencies/SDL/include/{SDL_test.h → SDL3/SDL_test.h} +16 -22
  67. data/dependencies/SDL/include/{SDL_test_assert.h → SDL3/SDL_test_assert.h} +27 -34
  68. data/dependencies/SDL/include/{SDL_test_common.h → SDL3/SDL_test_common.h} +109 -52
  69. data/dependencies/SDL/include/{SDL_test_compare.h → SDL3/SDL_test_compare.h} +22 -14
  70. data/dependencies/SDL/include/{SDL_test_crc32.h → SDL3/SDL_test_crc32.h} +27 -30
  71. data/dependencies/SDL/include/{SDL_test_font.h → SDL3/SDL_test_font.h} +50 -49
  72. data/dependencies/SDL/include/{SDL_test_fuzzer.h → SDL3/SDL_test_fuzzer.h} +64 -79
  73. data/dependencies/SDL/include/{SDL_test_harness.h → SDL3/SDL_test_harness.h} +47 -30
  74. data/dependencies/SDL/include/SDL3/SDL_test_log.h +83 -0
  75. data/dependencies/SDL/include/{SDL_test_md5.h → SDL3/SDL_test_md5.h} +21 -28
  76. data/dependencies/SDL/include/{SDL_test_memory.h → SDL3/SDL_test_memory.h} +19 -16
  77. data/dependencies/SDL/include/SDL3/SDL_thread.h +602 -0
  78. data/dependencies/SDL/include/SDL3/SDL_time.h +249 -0
  79. data/dependencies/SDL/include/SDL3/SDL_timer.h +454 -0
  80. data/dependencies/SDL/include/SDL3/SDL_touch.h +184 -0
  81. data/dependencies/SDL/include/SDL3/SDL_tray.h +544 -0
  82. data/dependencies/SDL/include/SDL3/SDL_version.h +198 -0
  83. data/dependencies/SDL/include/SDL3/SDL_video.h +3497 -0
  84. data/dependencies/SDL/include/SDL3/SDL_vulkan.h +287 -0
  85. data/dependencies/SDL/lib/x64/libSDL3.dll.a +0 -0
  86. data/dependencies/SDL/lib/x86/libSDL3.dll.a +0 -0
  87. data/dependencies/SDL_sound/SDL3_sound/SDL_sound.h +755 -0
  88. data/dependencies/SDL_sound/SDL_sound.c +136 -215
  89. data/dependencies/SDL_sound/SDL_sound_aiff.c +55 -54
  90. data/dependencies/SDL_sound/SDL_sound_au.c +32 -32
  91. data/dependencies/SDL_sound/SDL_sound_coreaudio.c +24 -41
  92. data/dependencies/SDL_sound/SDL_sound_flac.c +32 -12
  93. data/dependencies/SDL_sound/SDL_sound_internal.h +33 -48
  94. data/dependencies/SDL_sound/SDL_sound_modplug.c +21 -29
  95. data/dependencies/SDL_sound/SDL_sound_mp3.c +35 -13
  96. data/dependencies/SDL_sound/SDL_sound_raw.c +14 -15
  97. data/dependencies/SDL_sound/SDL_sound_shn.c +82 -82
  98. data/dependencies/SDL_sound/SDL_sound_voc.c +30 -30
  99. data/dependencies/SDL_sound/SDL_sound_vorbis.c +34 -25
  100. data/dependencies/SDL_sound/SDL_sound_wav.c +86 -82
  101. data/dependencies/SDL_sound/dr_flac.h +1890 -1301
  102. data/dependencies/SDL_sound/dr_mp3.h +1098 -660
  103. data/dependencies/SDL_sound/libmodplug/fastmix.c +1 -1
  104. data/dependencies/SDL_sound/libmodplug/libmodplug.h +5 -4
  105. data/dependencies/SDL_sound/libmodplug/load_ams.c +6 -3
  106. data/dependencies/SDL_sound/libmodplug/load_dsm.c +2 -1
  107. data/dependencies/SDL_sound/libmodplug/load_mt2.c +2 -2
  108. data/dependencies/SDL_sound/libmodplug/modplug.h +2 -1
  109. data/dependencies/SDL_sound/libmodplug/snd_fx.c +3 -3
  110. data/dependencies/SDL_sound/libmodplug/sndfile.c +3 -0
  111. data/dependencies/SDL_sound/libmodplug/sndmix.c +7 -3
  112. data/dependencies/SDL_sound/stb_vorbis.h +397 -342
  113. data/dependencies/mojoAL/AL/alc.h +2 -2
  114. data/dependencies/mojoAL/mojoal.c +2120 -1885
  115. data/dependencies/stb/stb_image.h +1190 -1379
  116. data/dependencies/stb/stb_image_write.h +329 -129
  117. data/dependencies/stb/stb_truetype.h +304 -60
  118. data/dependencies/utf8proc/utf8proc.c +133 -75
  119. data/dependencies/utf8proc/utf8proc.h +131 -50
  120. data/dependencies/utf8proc/utf8proc_data.h +16213 -15658
  121. data/ext/gosu-ffi/extconf.rb +103 -0
  122. data/ext/gosu-ffi/gosu-ffi.def +467 -0
  123. data/ffi/Gosu.cpp +330 -0
  124. data/ffi/Gosu.h +84 -0
  125. data/ffi/Gosu_Channel.cpp +62 -0
  126. data/ffi/Gosu_Channel.h +17 -0
  127. data/ffi/Gosu_Color.cpp +176 -0
  128. data/ffi/Gosu_Color.h +31 -0
  129. data/ffi/Gosu_Constants.cpp +334 -0
  130. data/ffi/Gosu_FFI.h +34 -0
  131. data/ffi/Gosu_FFI_internal.h +160 -0
  132. data/ffi/Gosu_Font.cpp +97 -0
  133. data/ffi/Gosu_Font.h +32 -0
  134. data/ffi/Gosu_Image.cpp +239 -0
  135. data/ffi/Gosu_Image.h +71 -0
  136. data/ffi/Gosu_Sample.cpp +29 -0
  137. data/ffi/Gosu_Sample.h +14 -0
  138. data/ffi/Gosu_Song.cpp +69 -0
  139. data/ffi/Gosu_Song.h +18 -0
  140. data/ffi/Gosu_TextInput.cpp +94 -0
  141. data/ffi/Gosu_TextInput.h +25 -0
  142. data/ffi/Gosu_Window.cpp +314 -0
  143. data/ffi/Gosu_Window.h +78 -0
  144. data/include/Gosu/Audio.hpp +6 -11
  145. data/include/Gosu/Bitmap.hpp +38 -53
  146. data/include/Gosu/Buffer.hpp +54 -0
  147. data/include/Gosu/Color.hpp +29 -36
  148. data/include/Gosu/Directories.hpp +25 -28
  149. data/include/Gosu/Drawable.hpp +58 -0
  150. data/include/Gosu/Font.hpp +6 -5
  151. data/include/Gosu/Fwd.hpp +4 -6
  152. data/include/Gosu/Gosu.hpp +5 -5
  153. data/include/Gosu/Graphics.hpp +53 -61
  154. data/include/Gosu/GraphicsBase.hpp +1 -11
  155. data/include/Gosu/Image.hpp +12 -15
  156. data/include/Gosu/Math.hpp +50 -72
  157. data/include/Gosu/Transform.hpp +32 -0
  158. data/include/Gosu/Utility.hpp +77 -1
  159. data/include/Gosu/Version.hpp +2 -2
  160. data/include/Gosu/Window.hpp +15 -9
  161. data/lib/SDL3.dll +0 -0
  162. data/lib/gosu/channel.rb +50 -0
  163. data/lib/gosu/color.rb +173 -0
  164. data/lib/gosu/compat.rb +130 -17
  165. data/lib/gosu/constants.rb +386 -0
  166. data/lib/gosu/ffi.rb +269 -0
  167. data/lib/gosu/font.rb +62 -0
  168. data/lib/gosu/gl_tex_info.rb +33 -0
  169. data/lib/gosu/gosu.rb +250 -0
  170. data/lib/gosu/image.rb +179 -0
  171. data/lib/gosu/numeric.rb +17 -0
  172. data/lib/gosu/preview.rb +6 -6
  173. data/lib/gosu/sample.rb +25 -0
  174. data/lib/gosu/song.rb +57 -0
  175. data/lib/gosu/text_input.rb +83 -0
  176. data/lib/gosu/window.rb +249 -0
  177. data/lib/gosu.rb +29 -8
  178. data/lib64/SDL3.dll +0 -0
  179. data/rdoc/gosu.rb +2 -2
  180. data/src/Audio.cpp +30 -24
  181. data/src/AudioFile.hpp +5 -4
  182. data/src/AudioFileAudioToolbox.cpp +8 -8
  183. data/src/AudioFileSDLSound.cpp +12 -15
  184. data/src/BinPacker.cpp +188 -0
  185. data/src/BinPacker.hpp +55 -0
  186. data/src/Bitmap.cpp +172 -143
  187. data/src/BitmapIO.cpp +60 -86
  188. data/src/Buffer.cpp +150 -0
  189. data/src/ClipRectStack.cpp +38 -0
  190. data/src/ClipRectStack.hpp +17 -83
  191. data/src/Color.cpp +75 -80
  192. data/src/Directories.cpp +47 -0
  193. data/src/DirectoriesUIKit.cpp +50 -0
  194. data/src/DrawOp.hpp +20 -18
  195. data/src/DrawOpQueue.hpp +27 -33
  196. data/src/Drawable.cpp +95 -0
  197. data/src/EmptyDrawable.hpp +38 -0
  198. data/src/FPS.cpp +31 -0
  199. data/src/Font.cpp +104 -74
  200. data/src/GosuGLView.cpp +13 -11
  201. data/src/GosuViewController.cpp +2 -10
  202. data/src/Graphics.cpp +86 -145
  203. data/src/GraphicsImpl.hpp +14 -68
  204. data/src/Image.cpp +41 -35
  205. data/src/Input.cpp +73 -78
  206. data/src/Macro.cpp +46 -29
  207. data/src/Macro.hpp +23 -17
  208. data/src/MarkupParser.cpp +5 -5
  209. data/src/Math.cpp +48 -22
  210. data/src/OffScreenTarget.cpp +47 -50
  211. data/src/OffScreenTarget.hpp +13 -11
  212. data/src/OpenGLContext.cpp +116 -0
  213. data/src/OpenGLContext.hpp +41 -0
  214. data/src/RenderState.hpp +49 -71
  215. data/src/Resolution.cpp +65 -53
  216. data/src/TexChunk.cpp +35 -80
  217. data/src/TexChunk.hpp +44 -35
  218. data/src/Text.cpp +1 -1
  219. data/src/TextBuilder.cpp +35 -21
  220. data/src/TextBuilder.hpp +6 -9
  221. data/src/TextInput.cpp +9 -9
  222. data/src/Texture.cpp +62 -80
  223. data/src/Texture.hpp +25 -23
  224. data/src/TiledDrawable.cpp +149 -0
  225. data/src/TiledDrawable.hpp +47 -0
  226. data/src/TimingApple.cpp +1 -1
  227. data/src/Transform.cpp +45 -50
  228. data/src/TransformStack.hpp +16 -16
  229. data/src/TrueTypeFont.cpp +59 -51
  230. data/src/TrueTypeFont.hpp +6 -7
  231. data/src/TrueTypeFontApple.cpp +28 -19
  232. data/src/TrueTypeFontUnix.cpp +39 -32
  233. data/src/TrueTypeFontWin.cpp +30 -30
  234. data/src/Utility.cpp +113 -37
  235. data/src/UtilityWin.cpp +49 -0
  236. data/src/Window.cpp +121 -151
  237. data/src/WindowUIKit.cpp +14 -14
  238. metadata +165 -118
  239. data/dependencies/SDL/include/SDL.h +0 -233
  240. data/dependencies/SDL/include/SDL_assert.h +0 -320
  241. data/dependencies/SDL/include/SDL_atomic.h +0 -415
  242. data/dependencies/SDL/include/SDL_audio.h +0 -1500
  243. data/dependencies/SDL/include/SDL_clipboard.h +0 -141
  244. data/dependencies/SDL/include/SDL_config.h +0 -331
  245. data/dependencies/SDL/include/SDL_cpuinfo.h +0 -594
  246. data/dependencies/SDL/include/SDL_endian.h +0 -348
  247. data/dependencies/SDL/include/SDL_error.h +0 -163
  248. data/dependencies/SDL/include/SDL_events.h +0 -1166
  249. data/dependencies/SDL/include/SDL_filesystem.h +0 -149
  250. data/dependencies/SDL/include/SDL_gamecontroller.h +0 -1074
  251. data/dependencies/SDL/include/SDL_gesture.h +0 -117
  252. data/dependencies/SDL/include/SDL_guid.h +0 -100
  253. data/dependencies/SDL/include/SDL_haptic.h +0 -1341
  254. data/dependencies/SDL/include/SDL_hidapi.h +0 -451
  255. data/dependencies/SDL/include/SDL_hints.h +0 -2569
  256. data/dependencies/SDL/include/SDL_joystick.h +0 -1066
  257. data/dependencies/SDL/include/SDL_keyboard.h +0 -353
  258. data/dependencies/SDL/include/SDL_keycode.h +0 -358
  259. data/dependencies/SDL/include/SDL_loadso.h +0 -115
  260. data/dependencies/SDL/include/SDL_log.h +0 -404
  261. data/dependencies/SDL/include/SDL_main.h +0 -275
  262. data/dependencies/SDL/include/SDL_mouse.h +0 -465
  263. data/dependencies/SDL/include/SDL_mutex.h +0 -471
  264. data/dependencies/SDL/include/SDL_name.h +0 -33
  265. data/dependencies/SDL/include/SDL_pixels.h +0 -644
  266. data/dependencies/SDL/include/SDL_platform.h +0 -261
  267. data/dependencies/SDL/include/SDL_quit.h +0 -58
  268. data/dependencies/SDL/include/SDL_rect.h +0 -376
  269. data/dependencies/SDL/include/SDL_render.h +0 -1919
  270. data/dependencies/SDL/include/SDL_revision.h +0 -7
  271. data/dependencies/SDL/include/SDL_rwops.h +0 -841
  272. data/dependencies/SDL/include/SDL_sensor.h +0 -322
  273. data/dependencies/SDL/include/SDL_shape.h +0 -155
  274. data/dependencies/SDL/include/SDL_stdinc.h +0 -830
  275. data/dependencies/SDL/include/SDL_surface.h +0 -997
  276. data/dependencies/SDL/include/SDL_system.h +0 -623
  277. data/dependencies/SDL/include/SDL_syswm.h +0 -386
  278. data/dependencies/SDL/include/SDL_test_images.h +0 -78
  279. data/dependencies/SDL/include/SDL_test_random.h +0 -115
  280. data/dependencies/SDL/include/SDL_thread.h +0 -464
  281. data/dependencies/SDL/include/SDL_timer.h +0 -222
  282. data/dependencies/SDL/include/SDL_touch.h +0 -150
  283. data/dependencies/SDL/include/SDL_version.h +0 -204
  284. data/dependencies/SDL/include/SDL_video.h +0 -2150
  285. data/dependencies/SDL/include/SDL_vulkan.h +0 -215
  286. data/dependencies/SDL/include/begin_code.h +0 -187
  287. data/dependencies/SDL/lib/x64/libSDL2.dll.a +0 -0
  288. data/dependencies/SDL/lib/x86/libSDL2.dll.a +0 -0
  289. data/dependencies/SDL_sound/SDL_sound.h +0 -725
  290. data/ext/gosu/extconf.rb +0 -119
  291. data/include/Gosu/IO.hpp +0 -254
  292. data/include/Gosu/ImageData.hpp +0 -53
  293. data/include/Gosu/Inspection.hpp +0 -7
  294. data/lib/SDL2.dll +0 -0
  295. data/lib/gosu/patches.rb +0 -66
  296. data/lib/gosu/run.rb +0 -20
  297. data/lib/gosu/swig_patches.rb +0 -110
  298. data/lib64/SDL2.dll +0 -0
  299. data/src/BlockAllocator.cpp +0 -131
  300. data/src/BlockAllocator.hpp +0 -32
  301. data/src/DirectoriesApple.cpp +0 -69
  302. data/src/DirectoriesUnix.cpp +0 -46
  303. data/src/DirectoriesWin.cpp +0 -65
  304. data/src/EmptyImageData.hpp +0 -52
  305. data/src/FileUnix.cpp +0 -99
  306. data/src/FileWin.cpp +0 -88
  307. data/src/IO.cpp +0 -60
  308. data/src/Iconv.hpp +0 -51
  309. data/src/Inspection.cpp +0 -27
  310. data/src/LargeImageData.cpp +0 -215
  311. data/src/LargeImageData.hpp +0 -39
  312. data/src/Log.hpp +0 -19
  313. data/src/RubyGosu.cxx +0 -13100
  314. data/src/RubyGosu.h +0 -49
  315. data/src/WinUtility.cpp +0 -61
  316. data/src/WinUtility.hpp +0 -27
  317. /data/dependencies/SDL/include/{SDL_opengles2_gl2.h → SDL3/SDL_opengles2_gl2.h} +0 -0
  318. /data/dependencies/SDL/include/{SDL_opengles2_gl2ext.h → SDL3/SDL_opengles2_gl2ext.h} +0 -0
  319. /data/dependencies/SDL/include/{SDL_opengles2_gl2platform.h → SDL3/SDL_opengles2_gl2platform.h} +0 -0
  320. /data/dependencies/SDL/include/{SDL_opengles2_khrplatform.h → SDL3/SDL_opengles2_khrplatform.h} +0 -0
@@ -1,6 +1,6 @@
1
1
  /*
2
2
  MP3 audio decoder. Choice of public domain or MIT-0. See license statements at the end of this file.
3
- dr_mp3 - v0.6.34 - 2022-09-17
3
+ dr_mp3 - v0.7.5 - UNRELEASED
4
4
 
5
5
  David Reid - mackron@gmail.com
6
6
 
@@ -10,30 +10,7 @@ Based on minimp3 (https://github.com/lieff/minimp3) which is where the real work
10
10
  */
11
11
 
12
12
  /*
13
- RELEASE NOTES - VERSION 0.6
14
- ===========================
15
- Version 0.6 includes breaking changes with the configuration of decoders. The ability to customize the number of output channels and the sample rate has been
16
- removed. You must now use the channel count and sample rate reported by the MP3 stream itself, and all channel and sample rate conversion must be done
17
- yourself.
18
-
19
-
20
- Changes to Initialization
21
- -------------------------
22
- Previously, `drmp3_init()`, etc. took a pointer to a `drmp3_config` object that allowed you to customize the output channels and sample rate. This has been
23
- removed. If you need the old behaviour you will need to convert the data yourself or just not upgrade. The following APIs have changed.
24
-
25
- `drmp3_init()`
26
- `drmp3_init_memory()`
27
- `drmp3_init_file()`
28
-
29
-
30
- Miscellaneous Changes
31
- ---------------------
32
- Support for loading a file from a `wchar_t` string has been added via the `drmp3_init_file_w()` API.
33
- */
34
-
35
- /*
36
- Introducation
13
+ Introduction
37
14
  =============
38
15
  dr_mp3 is a single file library. To use it, do something like the following in one .c file.
39
16
 
@@ -94,13 +71,13 @@ extern "C" {
94
71
  #define DRMP3_XSTRINGIFY(x) DRMP3_STRINGIFY(x)
95
72
 
96
73
  #define DRMP3_VERSION_MAJOR 0
97
- #define DRMP3_VERSION_MINOR 6
98
- #define DRMP3_VERSION_REVISION 34
74
+ #define DRMP3_VERSION_MINOR 7
75
+ #define DRMP3_VERSION_REVISION 5
99
76
  #define DRMP3_VERSION_STRING DRMP3_XSTRINGIFY(DRMP3_VERSION_MAJOR) "." DRMP3_XSTRINGIFY(DRMP3_VERSION_MINOR) "." DRMP3_XSTRINGIFY(DRMP3_VERSION_REVISION)
100
77
 
101
78
  #include <stddef.h> /* For size_t. */
102
79
 
103
- /* Sized types. */
80
+ /* Sized Types */
104
81
  typedef signed char drmp3_int8;
105
82
  typedef unsigned char drmp3_uint8;
106
83
  typedef signed short drmp3_int16;
@@ -124,7 +101,7 @@ typedef unsigned int drmp3_uint32;
124
101
  #pragma GCC diagnostic pop
125
102
  #endif
126
103
  #endif
127
- #if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__)) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(_M_ARM64) || defined(__powerpc64__)
104
+ #if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__)) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC) || defined(__powerpc64__)
128
105
  typedef drmp3_uint64 drmp3_uintptr;
129
106
  #else
130
107
  typedef drmp3_uint32 drmp3_uintptr;
@@ -134,6 +111,11 @@ typedef drmp3_uint32 drmp3_bool32;
134
111
  #define DRMP3_TRUE 1
135
112
  #define DRMP3_FALSE 0
136
113
 
114
+ /* Weird shifting syntax is for VC6 compatibility. */
115
+ #define DRMP3_UINT64_MAX (((drmp3_uint64)0xFFFFFFFF << 32) | (drmp3_uint64)0xFFFFFFFF)
116
+ /* End Sized Types */
117
+
118
+ /* Decorations */
137
119
  #if !defined(DRMP3_API)
138
120
  #if defined(DRMP3_DLL)
139
121
  #if defined(_WIN32)
@@ -152,7 +134,7 @@ typedef drmp3_uint32 drmp3_bool32;
152
134
  #endif
153
135
  #endif
154
136
 
155
- #if defined(DR_MP3_IMPLEMENTATION) || defined(DRMP3_IMPLEMENTATION)
137
+ #if defined(DR_MP3_IMPLEMENTATION)
156
138
  #define DRMP3_API DRMP3_DLL_EXPORT
157
139
  #else
158
140
  #define DRMP3_API DRMP3_DLL_IMPORT
@@ -163,7 +145,9 @@ typedef drmp3_uint32 drmp3_bool32;
163
145
  #define DRMP3_PRIVATE static
164
146
  #endif
165
147
  #endif
148
+ /* End Decorations */
166
149
 
150
+ /* Result Codes */
167
151
  typedef drmp3_int32 drmp3_result;
168
152
  #define DRMP3_SUCCESS 0
169
153
  #define DRMP3_ERROR -1 /* A generic error. */
@@ -219,11 +203,12 @@ typedef drmp3_int32 drmp3_result;
219
203
  #define DRMP3_CANCELLED -51
220
204
  #define DRMP3_MEMORY_ALREADY_MAPPED -52
221
205
  #define DRMP3_AT_END -53
222
-
206
+ /* End Result Codes */
223
207
 
224
208
  #define DRMP3_MAX_PCM_FRAMES_PER_MP3_FRAME 1152
225
209
  #define DRMP3_MAX_SAMPLES_PER_FRAME (DRMP3_MAX_PCM_FRAMES_PER_MP3_FRAME*2)
226
210
 
211
+ /* Inline */
227
212
  #ifdef _MSC_VER
228
213
  #define DRMP3_INLINE __forceinline
229
214
  #elif defined(__GNUC__)
@@ -250,26 +235,71 @@ typedef drmp3_int32 drmp3_result;
250
235
  #else
251
236
  #define DRMP3_INLINE
252
237
  #endif
238
+ /* End Inline */
253
239
 
254
240
 
255
241
  DRMP3_API void drmp3_version(drmp3_uint32* pMajor, drmp3_uint32* pMinor, drmp3_uint32* pRevision);
256
242
  DRMP3_API const char* drmp3_version_string(void);
257
243
 
258
244
 
245
+ /* Allocation Callbacks */
246
+ typedef struct
247
+ {
248
+ void* pUserData;
249
+ void* (* onMalloc)(size_t sz, void* pUserData);
250
+ void* (* onRealloc)(void* p, size_t sz, void* pUserData);
251
+ void (* onFree)(void* p, void* pUserData);
252
+ } drmp3_allocation_callbacks;
253
+ /* End Allocation Callbacks */
254
+
255
+
259
256
  /*
260
257
  Low Level Push API
261
258
  ==================
262
259
  */
260
+ #define DRMP3_MAX_BITRESERVOIR_BYTES 511
261
+ #define DRMP3_MAX_FREE_FORMAT_FRAME_SIZE 2304 /* more than ISO spec's */
262
+ #define DRMP3_MAX_L3_FRAME_PAYLOAD_BYTES DRMP3_MAX_FREE_FORMAT_FRAME_SIZE /* MUST be >= 320000/8/32000*1152 = 1440 */
263
+
263
264
  typedef struct
264
265
  {
265
- int frame_bytes, channels, hz, layer, bitrate_kbps;
266
+ int frame_bytes;
267
+ int channels;
268
+ int sample_rate;
269
+ int layer; /* Set to 0 when no complete MP3 frame is available. Otherwise set to 1, 2 or 3. */
270
+ int bitrate_kbps;
266
271
  } drmp3dec_frame_info;
267
272
 
273
+ typedef struct
274
+ {
275
+ const drmp3_uint8 *buf;
276
+ int pos, limit;
277
+ } drmp3_bs;
278
+
279
+ typedef struct
280
+ {
281
+ const drmp3_uint8 *sfbtab;
282
+ drmp3_uint16 part_23_length, big_values, scalefac_compress;
283
+ drmp3_uint8 global_gain, block_type, mixed_block_flag, n_long_sfb, n_short_sfb;
284
+ drmp3_uint8 table_select[3], region_count[3], subblock_gain[3];
285
+ drmp3_uint8 preflag, scalefac_scale, count1_table, scfsi;
286
+ } drmp3_L3_gr_info;
287
+
288
+ typedef struct
289
+ {
290
+ drmp3_bs bs;
291
+ drmp3_uint8 maindata[DRMP3_MAX_BITRESERVOIR_BYTES + DRMP3_MAX_L3_FRAME_PAYLOAD_BYTES];
292
+ drmp3_L3_gr_info gr_info[4];
293
+ float grbuf[2][576], scf[40], syn[18 + 15][2*32];
294
+ drmp3_uint8 ist_pos[2][39];
295
+ } drmp3dec_scratch;
296
+
268
297
  typedef struct
269
298
  {
270
299
  float mdct_overlap[2][9*32], qmf_state[15*2*32];
271
300
  int reserv, free_format_bytes;
272
301
  drmp3_uint8 header[4], reserv_buf[511];
302
+ drmp3dec_scratch scratch;
273
303
  } drmp3dec;
274
304
 
275
305
  /* Initializes a low level decoder. */
@@ -289,8 +319,9 @@ Main API (Pull API)
289
319
  */
290
320
  typedef enum
291
321
  {
292
- drmp3_seek_origin_start,
293
- drmp3_seek_origin_current
322
+ DRMP3_SEEK_SET,
323
+ DRMP3_SEEK_CUR,
324
+ DRMP3_SEEK_END
294
325
  } drmp3_seek_origin;
295
326
 
296
327
  typedef struct
@@ -301,10 +332,28 @@ typedef struct
301
332
  drmp3_uint16 pcmFramesToDiscard; /* The number of leading samples to read and discard. These are discarded after mp3FramesToDiscard. */
302
333
  } drmp3_seek_point;
303
334
 
335
+ typedef enum
336
+ {
337
+ DRMP3_METADATA_TYPE_ID3V1,
338
+ DRMP3_METADATA_TYPE_ID3V2,
339
+ DRMP3_METADATA_TYPE_APE,
340
+ DRMP3_METADATA_TYPE_XING,
341
+ DRMP3_METADATA_TYPE_VBRI,
342
+ DRMP3_METADATA_TYPE_LYRICS3V2
343
+ } drmp3_metadata_type;
344
+
345
+ typedef struct
346
+ {
347
+ drmp3_metadata_type type;
348
+ const void* pRawData; /* A pointer to the raw data. */
349
+ size_t rawDataSize;
350
+ } drmp3_metadata;
351
+
352
+
304
353
  /*
305
354
  Callback for when data is read. Return value is the number of bytes actually read.
306
355
 
307
- pUserData [in] The user data that was passed to drmp3_init(), drmp3_open() and family.
356
+ pUserData [in] The user data that was passed to drmp3_init(), and family.
308
357
  pBufferOut [out] The output buffer.
309
358
  bytesToRead [in] The number of bytes to read.
310
359
 
@@ -318,24 +367,33 @@ typedef size_t (* drmp3_read_proc)(void* pUserData, void* pBufferOut, size_t byt
318
367
  /*
319
368
  Callback for when data needs to be seeked.
320
369
 
321
- pUserData [in] The user data that was passed to drmp3_init(), drmp3_open() and family.
322
- offset [in] The number of bytes to move, relative to the origin. Will never be negative.
323
- origin [in] The origin of the seek - the current position or the start of the stream.
370
+ pUserData [in] The user data that was passed to drmp3_init(), and family.
371
+ offset [in] The number of bytes to move, relative to the origin. Can be negative.
372
+ origin [in] The origin of the seek.
324
373
 
325
374
  Returns whether or not the seek was successful.
326
-
327
- Whether or not it is relative to the beginning or current position is determined by the "origin" parameter which
328
- will be either drmp3_seek_origin_start or drmp3_seek_origin_current.
329
375
  */
330
376
  typedef drmp3_bool32 (* drmp3_seek_proc)(void* pUserData, int offset, drmp3_seek_origin origin);
331
377
 
332
- typedef struct
333
- {
334
- void* pUserData;
335
- void* (* onMalloc)(size_t sz, void* pUserData);
336
- void* (* onRealloc)(void* p, size_t sz, void* pUserData);
337
- void (* onFree)(void* p, void* pUserData);
338
- } drmp3_allocation_callbacks;
378
+ /*
379
+ Callback for retrieving the current cursor position.
380
+
381
+ pUserData [in] The user data that was passed to drmp3_init(), and family.
382
+ pCursor [out] The cursor position in bytes from the start of the stream.
383
+
384
+ Returns whether or not the cursor position was successfully retrieved.
385
+ */
386
+ typedef drmp3_bool32 (* drmp3_tell_proc)(void* pUserData, drmp3_int64* pCursor);
387
+
388
+
389
+ /*
390
+ Callback for when metadata is read.
391
+
392
+ Only the raw data is provided. The client is responsible for parsing the contents of the data themsevles.
393
+ Lyrics3v2 data includes the LYRICSBEGIN header, fields, size descriptor and LYRICS200 footer.
394
+ */
395
+ typedef void (* drmp3_meta_proc)(void* pUserData, const drmp3_metadata* pMetadata);
396
+
339
397
 
340
398
  typedef struct
341
399
  {
@@ -350,22 +408,32 @@ typedef struct
350
408
  drmp3_uint32 sampleRate;
351
409
  drmp3_read_proc onRead;
352
410
  drmp3_seek_proc onSeek;
411
+ drmp3_meta_proc onMeta;
353
412
  void* pUserData;
413
+ void* pUserDataMeta;
354
414
  drmp3_allocation_callbacks allocationCallbacks;
355
415
  drmp3_uint32 mp3FrameChannels; /* The number of channels in the currently loaded MP3 frame. Internal use only. */
356
416
  drmp3_uint32 mp3FrameSampleRate; /* The sample rate of the currently loaded MP3 frame. Internal use only. */
357
417
  drmp3_uint32 pcmFramesConsumedInMP3Frame;
358
418
  drmp3_uint32 pcmFramesRemainingInMP3Frame;
359
419
  drmp3_uint8 pcmFrames[sizeof(float)*DRMP3_MAX_SAMPLES_PER_FRAME]; /* <-- Multipled by sizeof(float) to ensure there's enough room for DR_MP3_FLOAT_OUTPUT. */
360
- drmp3_uint64 currentPCMFrame; /* The current PCM frame, globally, based on the output sample rate. Mainly used for seeking. */
420
+ drmp3_uint64 currentPCMFrame; /* The current PCM frame, globally. */
361
421
  drmp3_uint64 streamCursor; /* The current byte the decoder is sitting on in the raw stream. */
422
+ drmp3_uint64 streamLength; /* The length of the stream in bytes, excluding recognized trailing tags. dr_mp3 will not read beyond this. */
423
+ drmp3_uint64 streamStartOffset; /* The offset of the start of the MP3 data. This is used for skipping ID3v2 and VBR tags. */
362
424
  drmp3_seek_point* pSeekPoints; /* NULL by default. Set with drmp3_bind_seek_table(). Memory is owned by the client. dr_mp3 will never attempt to free this pointer. */
363
425
  drmp3_uint32 seekPointCount; /* The number of items in pSeekPoints. When set to 0 assumes to no seek table. Defaults to zero. */
426
+ drmp3_uint32 delayInPCMFrames;
427
+ drmp3_uint32 paddingInPCMFrames;
428
+ drmp3_uint32 mp3FramesSkipped; /* MP3 frames without output skipped by the last decode. Internal use only. */
429
+ drmp3_uint64 totalPCMFrameCount; /* Set to DRMP3_UINT64_MAX if the length is unknown. Includes delay and padding. */
430
+ drmp3_bool32 isVBR;
431
+ drmp3_bool32 isCBR;
364
432
  size_t dataSize;
365
433
  size_t dataCapacity;
366
434
  size_t dataConsumed;
367
435
  drmp3_uint8* pData;
368
- drmp3_bool32 atEnd : 1;
436
+ drmp3_bool32 atEnd;
369
437
  struct
370
438
  {
371
439
  const drmp3_uint8* pData;
@@ -379,6 +447,7 @@ Initializes an MP3 decoder.
379
447
 
380
448
  onRead [in] The function to call when data needs to be read from the client.
381
449
  onSeek [in] The function to call when the read position of the client data needs to move.
450
+ onTell [in] The function to call when the read position of the client data needs to be retrieved.
382
451
  pUserData [in, optional] A pointer to application defined data that will be passed to onRead and onSeek.
383
452
 
384
453
  Returns true if successful; false otherwise.
@@ -387,7 +456,7 @@ Close the loader with drmp3_uninit().
387
456
 
388
457
  See also: drmp3_init_file(), drmp3_init_memory(), drmp3_uninit()
389
458
  */
390
- DRMP3_API drmp3_bool32 drmp3_init(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, const drmp3_allocation_callbacks* pAllocationCallbacks);
459
+ DRMP3_API drmp3_bool32 drmp3_init(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, drmp3_meta_proc onMeta, void* pUserData, const drmp3_allocation_callbacks* pAllocationCallbacks);
391
460
 
392
461
  /*
393
462
  Initializes an MP3 decoder from a block of memory.
@@ -397,6 +466,7 @@ the lifetime of the drmp3 object.
397
466
 
398
467
  The buffer should contain the contents of the entire MP3 file.
399
468
  */
469
+ DRMP3_API drmp3_bool32 drmp3_init_memory_with_metadata(drmp3* pMP3, const void* pData, size_t dataSize, drmp3_meta_proc onMeta, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks);
400
470
  DRMP3_API drmp3_bool32 drmp3_init_memory(drmp3* pMP3, const void* pData, size_t dataSize, const drmp3_allocation_callbacks* pAllocationCallbacks);
401
471
 
402
472
  #ifndef DR_MP3_NO_STDIO
@@ -407,6 +477,9 @@ This holds the internal FILE object until drmp3_uninit() is called. Keep this in
407
477
  objects because the operating system may restrict the number of file handles an application can have open at
408
478
  any given time.
409
479
  */
480
+ DRMP3_API drmp3_bool32 drmp3_init_file_with_metadata(drmp3* pMP3, const char* pFilePath, drmp3_meta_proc onMeta, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks);
481
+ DRMP3_API drmp3_bool32 drmp3_init_file_with_metadata_w(drmp3* pMP3, const wchar_t* pFilePath, drmp3_meta_proc onMeta, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks);
482
+
410
483
  DRMP3_API drmp3_bool32 drmp3_init_file(drmp3* pMP3, const char* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks);
411
484
  DRMP3_API drmp3_bool32 drmp3_init_file_w(drmp3* pMP3, const wchar_t* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks);
412
485
  #endif
@@ -439,7 +512,7 @@ DRMP3_API drmp3_bool32 drmp3_seek_to_pcm_frame(drmp3* pMP3, drmp3_uint64 frameIn
439
512
 
440
513
  /*
441
514
  Calculates the total number of PCM frames in the MP3 stream. Cannot be used for infinite streams such as internet
442
- radio. Runs in linear time. Returns 0 on error.
515
+ radio. Should be considered a hint. Unreliable for malformed streams. Runs in linear time. Returns 0 on error.
443
516
  */
444
517
  DRMP3_API drmp3_uint64 drmp3_get_pcm_frame_count(drmp3* pMP3);
445
518
 
@@ -486,8 +559,8 @@ On output pConfig will receive the channel count and sample rate of the stream.
486
559
 
487
560
  Free the returned pointer with drmp3_free().
488
561
  */
489
- DRMP3_API float* drmp3_open_and_read_pcm_frames_f32(drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks);
490
- DRMP3_API drmp3_int16* drmp3_open_and_read_pcm_frames_s16(drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks);
562
+ DRMP3_API float* drmp3_open_and_read_pcm_frames_f32(drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks);
563
+ DRMP3_API drmp3_int16* drmp3_open_and_read_pcm_frames_s16(drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks);
491
564
 
492
565
  DRMP3_API float* drmp3_open_memory_and_read_pcm_frames_f32(const void* pData, size_t dataSize, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks);
493
566
  DRMP3_API drmp3_int16* drmp3_open_memory_and_read_pcm_frames_s16(const void* pData, size_t dataSize, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks);
@@ -520,7 +593,7 @@ DRMP3_API void drmp3_free(void* p, const drmp3_allocation_callbacks* pAllocation
520
593
 
521
594
  ************************************************************************************************************************************************************
522
595
  ************************************************************************************************************************************************************/
523
- #if defined(DR_MP3_IMPLEMENTATION) || defined(DRMP3_IMPLEMENTATION)
596
+ #if defined(DR_MP3_IMPLEMENTATION)
524
597
  #ifndef dr_mp3_c
525
598
  #define dr_mp3_c
526
599
 
@@ -555,14 +628,10 @@ DRMP3_API const char* drmp3_version_string(void)
555
628
 
556
629
  #define DRMP3_OFFSET_PTR(p, offset) ((void*)((drmp3_uint8*)(p) + (offset)))
557
630
 
558
- #define DRMP3_MAX_FREE_FORMAT_FRAME_SIZE 2304 /* more than ISO spec's */
559
631
  #ifndef DRMP3_MAX_FRAME_SYNC_MATCHES
560
632
  #define DRMP3_MAX_FRAME_SYNC_MATCHES 10
561
633
  #endif
562
634
 
563
- #define DRMP3_MAX_L3_FRAME_PAYLOAD_BYTES DRMP3_MAX_FREE_FORMAT_FRAME_SIZE /* MUST be >= 320000/8/32000*1152 = 1440 */
564
-
565
- #define DRMP3_MAX_BITRESERVOIR_BYTES 511
566
635
  #define DRMP3_SHORT_BLOCK_TYPE 2
567
636
  #define DRMP3_STOP_BLOCK_TYPE 3
568
637
  #define DRMP3_MODE_MONO 3
@@ -595,8 +664,10 @@ DRMP3_API const char* drmp3_version_string(void)
595
664
 
596
665
  #if !defined(DR_MP3_NO_SIMD)
597
666
 
598
- #if !defined(DR_MP3_ONLY_SIMD) && (defined(_M_X64) || defined(__x86_64__) || defined(__aarch64__) || defined(_M_ARM64))
599
- /* x64 always have SSE2, arm64 always have neon, no need for generic code */
667
+ #if !defined(DR_MP3_ONLY_SIMD) && ((defined(_MSC_VER) && _MSC_VER >= 1400) && defined(_M_X64)) || ((defined(__i386) || defined(_M_IX86) || defined(__i386__) || defined(__x86_64__)) && ((defined(_M_IX86_FP) && _M_IX86_FP == 2) || defined(__SSE2__)))
668
+ #define DR_MP3_ONLY_SIMD
669
+ #endif
670
+ #if !defined(DR_MP3_ONLY_SIMD) && (defined(__ARM_NEON) || defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC))
600
671
  #define DR_MP3_ONLY_SIMD
601
672
  #endif
602
673
 
@@ -618,7 +689,7 @@ DRMP3_API const char* drmp3_version_string(void)
618
689
  #define DRMP3_VMUL_S(x, s) _mm_mul_ps(x, _mm_set1_ps(s))
619
690
  #define DRMP3_VREV(x) _mm_shuffle_ps(x, x, _MM_SHUFFLE(0, 1, 2, 3))
620
691
  typedef __m128 drmp3_f4;
621
- #if defined(_MSC_VER) || defined(DR_MP3_ONLY_SIMD)
692
+ #if (defined(_MSC_VER) || defined(DR_MP3_ONLY_SIMD)) && !defined(__clang__)
622
693
  #define drmp3_cpuid __cpuid
623
694
  #else
624
695
  static __inline__ __attribute__((always_inline)) void drmp3_cpuid(int CPUInfo[], const int InfoType)
@@ -671,7 +742,7 @@ end:
671
742
  return g_have_simd - 1;
672
743
  #endif
673
744
  }
674
- #elif defined(__ARM_NEON) || defined(__aarch64__) || defined(_M_ARM64)
745
+ #elif defined(__ARM_NEON) || defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
675
746
  #include <arm_neon.h>
676
747
  #define DRMP3_HAVE_SSE 0
677
748
  #define DRMP3_HAVE_SIMD 1
@@ -704,7 +775,7 @@ static int drmp3_have_simd(void)
704
775
 
705
776
  #endif
706
777
 
707
- #if defined(__ARM_ARCH) && (__ARM_ARCH >= 6) && !defined(__aarch64__) && !defined(_M_ARM64)
778
+ #if defined(__ARM_ARCH) && (__ARM_ARCH >= 6) && !defined(__aarch64__) && !defined(_M_ARM64) && !defined(_M_ARM64EC) && !defined(__ARM_ARCH_6M__)
708
779
  #define DRMP3_HAVE_ARMV6 1
709
780
  static __inline__ __attribute__((always_inline)) drmp3_int32 drmp3_clip_int16_arm(drmp3_int32 a)
710
781
  {
@@ -728,6 +799,9 @@ static __inline__ __attribute__((always_inline)) drmp3_int32 drmp3_clip_int16_ar
728
799
  #ifndef DRMP3_MOVE_MEMORY
729
800
  #define DRMP3_MOVE_MEMORY(dst, src, sz) memmove((dst), (src), (sz))
730
801
  #endif
802
+ #ifndef DRMP3_CMP_MEMORY
803
+ #define DRMP3_CMP_MEMORY(src1, src2, sz) memcmp((src1), (src2), (sz))
804
+ #endif
731
805
  #ifndef DRMP3_ZERO_MEMORY
732
806
  #define DRMP3_ZERO_MEMORY(p, sz) memset((p), 0, (sz))
733
807
  #endif
@@ -742,11 +816,7 @@ static __inline__ __attribute__((always_inline)) drmp3_int32 drmp3_clip_int16_ar
742
816
  #define DRMP3_FREE(p) free((p))
743
817
  #endif
744
818
 
745
- typedef struct
746
- {
747
- const drmp3_uint8 *buf;
748
- int pos, limit;
749
- } drmp3_bs;
819
+
750
820
 
751
821
  typedef struct
752
822
  {
@@ -759,24 +829,6 @@ typedef struct
759
829
  drmp3_uint8 tab_offset, code_tab_width, band_count;
760
830
  } drmp3_L12_subband_alloc;
761
831
 
762
- typedef struct
763
- {
764
- const drmp3_uint8 *sfbtab;
765
- drmp3_uint16 part_23_length, big_values, scalefac_compress;
766
- drmp3_uint8 global_gain, block_type, mixed_block_flag, n_long_sfb, n_short_sfb;
767
- drmp3_uint8 table_select[3], region_count[3], subblock_gain[3];
768
- drmp3_uint8 preflag, scalefac_scale, count1_table, scfsi;
769
- } drmp3_L3_gr_info;
770
-
771
- typedef struct
772
- {
773
- drmp3_bs bs;
774
- drmp3_uint8 maindata[DRMP3_MAX_BITRESERVOIR_BYTES + DRMP3_MAX_L3_FRAME_PAYLOAD_BYTES];
775
- drmp3_L3_gr_info gr_info[4];
776
- float grbuf[2][576], scf[40], syn[18 + 15][2*32];
777
- drmp3_uint8 ist_pos[2][39];
778
- } drmp3dec_scratch;
779
-
780
832
  static void drmp3_bs_init(drmp3_bs *bs, const drmp3_uint8 *data, int bytes)
781
833
  {
782
834
  bs->buf = data;
@@ -1190,6 +1242,14 @@ static float drmp3_L3_ldexp_q2(float y, int exp_q2)
1190
1242
  return y;
1191
1243
  }
1192
1244
 
1245
+ /*
1246
+ I've had reports of GCC 14 throwing an incorrect -Wstringop-overflow warning here. This is an attempt
1247
+ to silence this warning.
1248
+ */
1249
+ #if (defined(__GNUC__) && (__GNUC__ >= 13)) && !defined(__clang__)
1250
+ #pragma GCC diagnostic push
1251
+ #pragma GCC diagnostic ignored "-Wstringop-overflow"
1252
+ #endif
1193
1253
  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)
1194
1254
  {
1195
1255
  static const drmp3_uint8 g_scf_partitions[3][28] = {
@@ -1251,6 +1311,9 @@ static void drmp3_L3_decode_scalefactors(const drmp3_uint8 *hdr, drmp3_uint8 *is
1251
1311
  scf[i] = drmp3_L3_ldexp_q2(gain, iscf[i] << scf_shift);
1252
1312
  }
1253
1313
  }
1314
+ #if (defined(__GNUC__) && (__GNUC__ >= 13)) && !defined(__clang__)
1315
+ #pragma GCC diagnostic pop
1316
+ #endif
1254
1317
 
1255
1318
  static const float g_drmp3_pow43[129 + 16] = {
1256
1319
  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,
@@ -1480,7 +1543,8 @@ static void drmp3_L3_stereo_top_band(const float *right, const drmp3_uint8 *sfb,
1480
1543
  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)
1481
1544
  {
1482
1545
  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 };
1483
- unsigned i, max_pos = DRMP3_HDR_TEST_MPEG1(hdr) ? 7 : 64;
1546
+ const drmp3_uint8 mpeg1 = DRMP3_HDR_TEST_MPEG1(hdr);
1547
+ unsigned i, max_pos = mpeg1 ? 7 : 64;
1484
1548
 
1485
1549
  for (i = 0; sfb[i]; i++)
1486
1550
  {
@@ -1488,7 +1552,7 @@ static void drmp3_L3_stereo_process(float *left, const drmp3_uint8 *ist_pos, con
1488
1552
  if ((int)i > max_band[i % 3] && ipos < max_pos)
1489
1553
  {
1490
1554
  float kl, kr, s = DRMP3_HDR_TEST_MS_STEREO(hdr) ? 1.41421356f : 1;
1491
- if (DRMP3_HDR_TEST_MPEG1(hdr))
1555
+ if (mpeg1)
1492
1556
  {
1493
1557
  kl = g_pan[2*ipos];
1494
1558
  kr = g_pan[2*ipos + 1];
@@ -1976,8 +2040,8 @@ static drmp3_int16 drmp3d_scale_pcm(float sample)
1976
2040
  s32 -= (s32 < 0);
1977
2041
  s = (drmp3_int16)drmp3_clip_int16_arm(s32);
1978
2042
  #else
1979
- if (sample >= 32766.5) return (drmp3_int16) 32767;
1980
- if (sample <= -32767.5) return (drmp3_int16)-32768;
2043
+ if (sample >= 32766.5f) return (drmp3_int16) 32767;
2044
+ if (sample <= -32767.5f) return (drmp3_int16)-32768;
1981
2045
  s = (drmp3_int16)(sample + .5f);
1982
2046
  s -= (s < 0); /* away from zero, to be compliant */
1983
2047
  #endif
@@ -2200,6 +2264,22 @@ static void drmp3d_synth_granule(float *qmf_state, float *grbuf, int nbands, int
2200
2264
  }
2201
2265
  }
2202
2266
 
2267
+ static int drmp3_hdr_is_null(const drmp3_uint8 *hdr)
2268
+ {
2269
+ return !hdr[0] && !hdr[1] && !hdr[2] && !hdr[3];
2270
+ }
2271
+
2272
+ static int drmp3_hdr_is_id3v1(const drmp3_uint8 *hdr, int bytes_left)
2273
+ {
2274
+ /* Require a whole trailing ID3v1 tag so a random "TAG" in audio data is not mistaken for end-of-stream. */
2275
+ return 128 == bytes_left && !DRMP3_CMP_MEMORY(hdr, "TAG", 3);
2276
+ }
2277
+
2278
+ static int drmp3_hdr_is_null_or_tag(const drmp3_uint8 *hdr, int bytes_left)
2279
+ {
2280
+ return drmp3_hdr_is_null(hdr) || drmp3_hdr_is_id3v1(hdr, bytes_left);
2281
+ }
2282
+
2203
2283
  static int drmp3d_match_frame(const drmp3_uint8 *hdr, int mp3_bytes, int frame_bytes)
2204
2284
  {
2205
2285
  int i, nmatch;
@@ -2208,6 +2288,8 @@ static int drmp3d_match_frame(const drmp3_uint8 *hdr, int mp3_bytes, int frame_b
2208
2288
  i += drmp3_hdr_frame_bytes(hdr + i, frame_bytes) + drmp3_hdr_padding(hdr + i);
2209
2289
  if (i + DRMP3_HDR_SIZE > mp3_bytes)
2210
2290
  return nmatch > 0;
2291
+ if (drmp3_hdr_is_null_or_tag(hdr + i, mp3_bytes - i))
2292
+ return nmatch > 0;
2211
2293
  if (!drmp3_hdr_compare(hdr, hdr + i))
2212
2294
  return 0;
2213
2295
  }
@@ -2262,7 +2344,10 @@ DRMP3_API int drmp3dec_decode_frame(drmp3dec *dec, const drmp3_uint8 *mp3, int m
2262
2344
  int i = 0, igr, frame_size = 0, success = 1;
2263
2345
  const drmp3_uint8 *hdr;
2264
2346
  drmp3_bs bs_frame[1];
2265
- drmp3dec_scratch scratch;
2347
+
2348
+ DRMP3_ZERO_MEMORY(dec->scratch.ist_pos, sizeof(dec->scratch.ist_pos));
2349
+
2350
+ info->layer = 0;
2266
2351
 
2267
2352
  if (mp3_bytes > 4 && dec->header[0] == 0xff && drmp3_hdr_compare(dec->header, mp3))
2268
2353
  {
@@ -2287,7 +2372,7 @@ DRMP3_API int drmp3dec_decode_frame(drmp3dec *dec, const drmp3_uint8 *mp3, int m
2287
2372
  DRMP3_COPY_MEMORY(dec->header, hdr, DRMP3_HDR_SIZE);
2288
2373
  info->frame_bytes = i + frame_size;
2289
2374
  info->channels = DRMP3_HDR_IS_MONO(hdr) ? 1 : 2;
2290
- info->hz = drmp3_hdr_sample_rate_hz(hdr);
2375
+ info->sample_rate = drmp3_hdr_sample_rate_hz(hdr);
2291
2376
  info->layer = 4 - DRMP3_HDR_GET_LAYER(hdr);
2292
2377
  info->bitrate_kbps = drmp3_hdr_bitrate_kbps(hdr);
2293
2378
 
@@ -2299,23 +2384,23 @@ DRMP3_API int drmp3dec_decode_frame(drmp3dec *dec, const drmp3_uint8 *mp3, int m
2299
2384
 
2300
2385
  if (info->layer == 3)
2301
2386
  {
2302
- int main_data_begin = drmp3_L3_read_side_info(bs_frame, scratch.gr_info, hdr);
2387
+ int main_data_begin = drmp3_L3_read_side_info(bs_frame, dec->scratch.gr_info, hdr);
2303
2388
  if (main_data_begin < 0 || bs_frame->pos > bs_frame->limit)
2304
2389
  {
2305
2390
  drmp3dec_init(dec);
2306
2391
  return 0;
2307
2392
  }
2308
- success = drmp3_L3_restore_reservoir(dec, bs_frame, &scratch, main_data_begin);
2393
+ success = drmp3_L3_restore_reservoir(dec, bs_frame, &dec->scratch, main_data_begin);
2309
2394
  if (success && pcm != NULL)
2310
2395
  {
2311
2396
  for (igr = 0; igr < (DRMP3_HDR_TEST_MPEG1(hdr) ? 2 : 1); igr++, pcm = DRMP3_OFFSET_PTR(pcm, sizeof(drmp3d_sample_t)*576*info->channels))
2312
2397
  {
2313
- DRMP3_ZERO_MEMORY(scratch.grbuf[0], 576*2*sizeof(float));
2314
- drmp3_L3_decode(dec, &scratch, scratch.gr_info + igr*info->channels, info->channels);
2315
- drmp3d_synth_granule(dec->qmf_state, scratch.grbuf[0], 18, info->channels, (drmp3d_sample_t*)pcm, scratch.syn[0]);
2398
+ DRMP3_ZERO_MEMORY(dec->scratch.grbuf, sizeof(dec->scratch.grbuf));
2399
+ drmp3_L3_decode(dec, &dec->scratch, dec->scratch.gr_info + igr*info->channels, info->channels);
2400
+ drmp3d_synth_granule(dec->qmf_state, dec->scratch.grbuf[0], 18, info->channels, (drmp3d_sample_t*)pcm, dec->scratch.syn[0]);
2316
2401
  }
2317
2402
  }
2318
- drmp3_L3_save_reservoir(dec, &scratch);
2403
+ drmp3_L3_save_reservoir(dec, &dec->scratch);
2319
2404
  } else
2320
2405
  {
2321
2406
  #ifdef DR_MP3_ONLY_MP3
@@ -2329,15 +2414,15 @@ DRMP3_API int drmp3dec_decode_frame(drmp3dec *dec, const drmp3_uint8 *mp3, int m
2329
2414
 
2330
2415
  drmp3_L12_read_scale_info(hdr, bs_frame, sci);
2331
2416
 
2332
- DRMP3_ZERO_MEMORY(scratch.grbuf[0], 576*2*sizeof(float));
2417
+ DRMP3_ZERO_MEMORY(dec->scratch.grbuf, sizeof(dec->scratch.grbuf));
2333
2418
  for (i = 0, igr = 0; igr < 3; igr++)
2334
2419
  {
2335
- if (12 == (i += drmp3_L12_dequantize_granule(scratch.grbuf[0] + i, bs_frame, sci, info->layer | 1)))
2420
+ if (12 == (i += drmp3_L12_dequantize_granule(dec->scratch.grbuf[0] + i, bs_frame, sci, info->layer | 1)))
2336
2421
  {
2337
2422
  i = 0;
2338
- drmp3_L12_apply_scf_384(sci, sci->scf + igr, scratch.grbuf[0]);
2339
- drmp3d_synth_granule(dec->qmf_state, scratch.grbuf[0], 12, info->channels, (drmp3d_sample_t*)pcm, scratch.syn[0]);
2340
- DRMP3_ZERO_MEMORY(scratch.grbuf[0], 576*2*sizeof(float));
2423
+ drmp3_L12_apply_scf_384(sci, sci->scf + igr, dec->scratch.grbuf[0]);
2424
+ drmp3d_synth_granule(dec->qmf_state, dec->scratch.grbuf[0], 12, info->channels, (drmp3d_sample_t*)pcm, dec->scratch.syn[0]);
2425
+ DRMP3_ZERO_MEMORY(dec->scratch.grbuf, sizeof(dec->scratch.grbuf));
2341
2426
  pcm = DRMP3_OFFSET_PTR(pcm, sizeof(drmp3d_sample_t)*384*info->channels);
2342
2427
  }
2343
2428
  if (bs_frame->pos > bs_frame->limit)
@@ -2367,14 +2452,7 @@ DRMP3_API void drmp3dec_f32_to_s16(const float *in, drmp3_int16 *out, size_t num
2367
2452
  drmp3_f4 s16min = DRMP3_VSET(-32768.0f);
2368
2453
  __m128i pcm8 = _mm_packs_epi32(_mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(a, s16max), s16min)),
2369
2454
  _mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(b, s16max), s16min)));
2370
- out[i ] = (drmp3_int16)_mm_extract_epi16(pcm8, 0);
2371
- out[i+1] = (drmp3_int16)_mm_extract_epi16(pcm8, 1);
2372
- out[i+2] = (drmp3_int16)_mm_extract_epi16(pcm8, 2);
2373
- out[i+3] = (drmp3_int16)_mm_extract_epi16(pcm8, 3);
2374
- out[i+4] = (drmp3_int16)_mm_extract_epi16(pcm8, 4);
2375
- out[i+5] = (drmp3_int16)_mm_extract_epi16(pcm8, 5);
2376
- out[i+6] = (drmp3_int16)_mm_extract_epi16(pcm8, 6);
2377
- out[i+7] = (drmp3_int16)_mm_extract_epi16(pcm8, 7);
2455
+ _mm_storeu_si128( (__m128i *)(&out[i]), pcm8 );
2378
2456
  #else
2379
2457
  int16x4_t pcma, pcmb;
2380
2458
  a = DRMP3_VADD(a, DRMP3_VSET(0.5f));
@@ -2395,9 +2473,9 @@ DRMP3_API void drmp3dec_f32_to_s16(const float *in, drmp3_int16 *out, size_t num
2395
2473
  for(; i < num_samples; i++)
2396
2474
  {
2397
2475
  float sample = in[i] * 32768.0f;
2398
- if (sample >= 32766.5)
2476
+ if (sample >= 32766.5f)
2399
2477
  out[i] = (drmp3_int16) 32767;
2400
- else if (sample <= -32767.5)
2478
+ else if (sample <= -32767.5f)
2401
2479
  out[i] = (drmp3_int16)-32768;
2402
2480
  else
2403
2481
  {
@@ -2415,6 +2493,7 @@ DRMP3_API void drmp3dec_f32_to_s16(const float *in, drmp3_int16 *out, size_t num
2415
2493
  Main Public API
2416
2494
 
2417
2495
  ************************************************************************************************************************************************************/
2496
+ /* SIZE_MAX */
2418
2497
  #if defined(SIZE_MAX)
2419
2498
  #define DRMP3_SIZE_MAX SIZE_MAX
2420
2499
  #else
@@ -2424,10 +2503,12 @@ DRMP3_API void drmp3dec_f32_to_s16(const float *in, drmp3_int16 *out, size_t num
2424
2503
  #define DRMP3_SIZE_MAX 0xFFFFFFFF
2425
2504
  #endif
2426
2505
  #endif
2506
+ /* End SIZE_MAX */
2427
2507
 
2428
2508
  /* Options. */
2509
+ /* Enough to refill a full bit reservoir (511 bytes) at 32 kbps. */
2429
2510
  #ifndef DRMP3_SEEK_LEADING_MP3_FRAMES
2430
- #define DRMP3_SEEK_LEADING_MP3_FRAMES 2
2511
+ #define DRMP3_SEEK_LEADING_MP3_FRAMES 16
2431
2512
  #endif
2432
2513
 
2433
2514
  #define DRMP3_MIN_DATA_CHUNK_SIZE 16384
@@ -2578,22 +2659,56 @@ static drmp3_allocation_callbacks drmp3_copy_allocation_callbacks_or_defaults(co
2578
2659
 
2579
2660
  static size_t drmp3__on_read(drmp3* pMP3, void* pBufferOut, size_t bytesToRead)
2580
2661
  {
2581
- size_t bytesRead = pMP3->onRead(pMP3->pUserData, pBufferOut, bytesToRead);
2662
+ size_t bytesRead;
2663
+
2664
+ DRMP3_ASSERT(pMP3 != NULL);
2665
+ DRMP3_ASSERT(pMP3->onRead != NULL);
2666
+
2667
+ /*
2668
+ Don't try reading 0 bytes from the callback. This can happen when the stream is clamped against
2669
+ ID3v1 or APE tags at the end of the stream.
2670
+ */
2671
+ if (bytesToRead == 0) {
2672
+ return 0;
2673
+ }
2674
+
2675
+ bytesRead = pMP3->onRead(pMP3->pUserData, pBufferOut, bytesToRead);
2582
2676
  pMP3->streamCursor += bytesRead;
2677
+
2583
2678
  return bytesRead;
2584
2679
  }
2585
2680
 
2681
+ static size_t drmp3__on_read_clamped(drmp3* pMP3, void* pBufferOut, size_t bytesToRead)
2682
+ {
2683
+ DRMP3_ASSERT(pMP3 != NULL);
2684
+ DRMP3_ASSERT(pMP3->onRead != NULL);
2685
+
2686
+ if (pMP3->streamLength == DRMP3_UINT64_MAX) {
2687
+ return drmp3__on_read(pMP3, pBufferOut, bytesToRead);
2688
+ } else {
2689
+ drmp3_uint64 bytesRemaining;
2690
+
2691
+ bytesRemaining = (pMP3->streamLength - pMP3->streamCursor);
2692
+ if (bytesToRead > bytesRemaining) {
2693
+ bytesToRead = (size_t)bytesRemaining;
2694
+ }
2695
+
2696
+ return drmp3__on_read(pMP3, pBufferOut, bytesToRead);
2697
+ }
2698
+ }
2699
+
2586
2700
  static drmp3_bool32 drmp3__on_seek(drmp3* pMP3, int offset, drmp3_seek_origin origin)
2587
2701
  {
2588
2702
  DRMP3_ASSERT(offset >= 0);
2703
+ DRMP3_ASSERT(origin == DRMP3_SEEK_SET || origin == DRMP3_SEEK_CUR);
2589
2704
 
2590
2705
  if (!pMP3->onSeek(pMP3->pUserData, offset, origin)) {
2591
2706
  return DRMP3_FALSE;
2592
2707
  }
2593
2708
 
2594
- if (origin == drmp3_seek_origin_start) {
2709
+ if (origin == DRMP3_SEEK_SET) {
2595
2710
  pMP3->streamCursor = (drmp3_uint64)offset;
2596
- } else {
2711
+ } else{
2597
2712
  pMP3->streamCursor += offset;
2598
2713
  }
2599
2714
 
@@ -2606,21 +2721,20 @@ static drmp3_bool32 drmp3__on_seek_64(drmp3* pMP3, drmp3_uint64 offset, drmp3_se
2606
2721
  return drmp3__on_seek(pMP3, (int)offset, origin);
2607
2722
  }
2608
2723
 
2609
-
2610
2724
  /* Getting here "offset" is too large for a 32-bit integer. We just keep seeking forward until we hit the offset. */
2611
- if (!drmp3__on_seek(pMP3, 0x7FFFFFFF, drmp3_seek_origin_start)) {
2725
+ if (!drmp3__on_seek(pMP3, 0x7FFFFFFF, DRMP3_SEEK_SET)) {
2612
2726
  return DRMP3_FALSE;
2613
2727
  }
2614
2728
 
2615
2729
  offset -= 0x7FFFFFFF;
2616
2730
  while (offset > 0) {
2617
2731
  if (offset <= 0x7FFFFFFF) {
2618
- if (!drmp3__on_seek(pMP3, (int)offset, drmp3_seek_origin_current)) {
2732
+ if (!drmp3__on_seek(pMP3, (int)offset, DRMP3_SEEK_CUR)) {
2619
2733
  return DRMP3_FALSE;
2620
2734
  }
2621
2735
  offset = 0;
2622
2736
  } else {
2623
- if (!drmp3__on_seek(pMP3, 0x7FFFFFFF, drmp3_seek_origin_current)) {
2737
+ if (!drmp3__on_seek(pMP3, 0x7FFFFFFF, DRMP3_SEEK_CUR)) {
2624
2738
  return DRMP3_FALSE;
2625
2739
  }
2626
2740
  offset -= 0x7FFFFFFF;
@@ -2630,8 +2744,22 @@ static drmp3_bool32 drmp3__on_seek_64(drmp3* pMP3, drmp3_uint64 offset, drmp3_se
2630
2744
  return DRMP3_TRUE;
2631
2745
  }
2632
2746
 
2747
+ static void drmp3__on_meta(drmp3* pMP3, drmp3_metadata_type type, const void* pRawData, size_t rawDataSize)
2748
+ {
2749
+ if (pMP3->onMeta) {
2750
+ drmp3_metadata metadata;
2751
+
2752
+ DRMP3_ZERO_OBJECT(&metadata);
2753
+ metadata.type = type;
2754
+ metadata.pRawData = pRawData;
2755
+ metadata.rawDataSize = rawDataSize;
2756
+
2757
+ pMP3->onMeta(pMP3->pUserDataMeta, &metadata);
2758
+ }
2759
+ }
2760
+
2633
2761
 
2634
- static drmp3_uint32 drmp3_decode_next_frame_ex__callbacks(drmp3* pMP3, drmp3d_sample_t* pPCMFrames)
2762
+ static drmp3_uint32 drmp3_decode_next_frame_ex__callbacks(drmp3* pMP3, drmp3d_sample_t* pPCMFrames, drmp3dec_frame_info* pMP3FrameInfo, const drmp3_uint8** ppMP3FrameData)
2635
2763
  {
2636
2764
  drmp3_uint32 pcmFramesRead = 0;
2637
2765
 
@@ -2671,7 +2799,7 @@ static drmp3_uint32 drmp3_decode_next_frame_ex__callbacks(drmp3* pMP3, drmp3d_sa
2671
2799
  pMP3->dataCapacity = newDataCap;
2672
2800
  }
2673
2801
 
2674
- bytesRead = drmp3__on_read(pMP3, pMP3->pData + pMP3->dataSize, (pMP3->dataCapacity - pMP3->dataSize));
2802
+ bytesRead = drmp3__on_read_clamped(pMP3, pMP3->pData + pMP3->dataSize, (pMP3->dataCapacity - pMP3->dataSize));
2675
2803
  if (bytesRead == 0) {
2676
2804
  if (pMP3->dataSize == 0) {
2677
2805
  pMP3->atEnd = DRMP3_TRUE;
@@ -2690,13 +2818,16 @@ static drmp3_uint32 drmp3_decode_next_frame_ex__callbacks(drmp3* pMP3, drmp3d_sa
2690
2818
  DRMP3_ASSERT(pMP3->pData != NULL);
2691
2819
  DRMP3_ASSERT(pMP3->dataCapacity > 0);
2692
2820
 
2821
+ /* Do a runtime check here to try silencing a false-positive from clang-analyzer. */
2822
+ if (pMP3->pData == NULL) {
2823
+ return 0;
2824
+ }
2825
+
2693
2826
  pcmFramesRead = drmp3dec_decode_frame(&pMP3->decoder, pMP3->pData + pMP3->dataConsumed, (int)pMP3->dataSize, pPCMFrames, &info); /* <-- Safe size_t -> int conversion thanks to the check above. */
2694
2827
 
2695
2828
  /* Consume the data. */
2696
- if (info.frame_bytes > 0) {
2697
- pMP3->dataConsumed += (size_t)info.frame_bytes;
2698
- pMP3->dataSize -= (size_t)info.frame_bytes;
2699
- }
2829
+ pMP3->dataConsumed += (size_t)info.frame_bytes;
2830
+ pMP3->dataSize -= (size_t)info.frame_bytes;
2700
2831
 
2701
2832
  /* pcmFramesRead will be equal to 0 if decoding failed. If it is zero and info.frame_bytes > 0 then we have successfully decoded the frame. */
2702
2833
  if (pcmFramesRead > 0) {
@@ -2704,7 +2835,16 @@ static drmp3_uint32 drmp3_decode_next_frame_ex__callbacks(drmp3* pMP3, drmp3d_sa
2704
2835
  pMP3->pcmFramesConsumedInMP3Frame = 0;
2705
2836
  pMP3->pcmFramesRemainingInMP3Frame = pcmFramesRead;
2706
2837
  pMP3->mp3FrameChannels = info.channels;
2707
- pMP3->mp3FrameSampleRate = info.hz;
2838
+ pMP3->mp3FrameSampleRate = info.sample_rate;
2839
+
2840
+ if (pMP3FrameInfo != NULL) {
2841
+ *pMP3FrameInfo = info;
2842
+ }
2843
+
2844
+ if (ppMP3FrameData != NULL) {
2845
+ *ppMP3FrameData = pMP3->pData + pMP3->dataConsumed - (size_t)info.frame_bytes;
2846
+ }
2847
+
2708
2848
  break;
2709
2849
  } else if (info.frame_bytes == 0) {
2710
2850
  /* Need more data. minimp3 recommends doing data submission in 16K chunks. */
@@ -2731,20 +2871,22 @@ static drmp3_uint32 drmp3_decode_next_frame_ex__callbacks(drmp3* pMP3, drmp3d_sa
2731
2871
  }
2732
2872
 
2733
2873
  /* Fill in a chunk. */
2734
- bytesRead = drmp3__on_read(pMP3, pMP3->pData + pMP3->dataSize, (pMP3->dataCapacity - pMP3->dataSize));
2874
+ bytesRead = drmp3__on_read_clamped(pMP3, pMP3->pData + pMP3->dataSize, (pMP3->dataCapacity - pMP3->dataSize));
2735
2875
  if (bytesRead == 0) {
2736
2876
  pMP3->atEnd = DRMP3_TRUE;
2737
2877
  return 0; /* Error reading more data. */
2738
2878
  }
2739
2879
 
2740
2880
  pMP3->dataSize += bytesRead;
2881
+ } else if (info.layer != 0) {
2882
+ pMP3->mp3FramesSkipped += 1;
2741
2883
  }
2742
2884
  };
2743
2885
 
2744
2886
  return pcmFramesRead;
2745
2887
  }
2746
2888
 
2747
- static drmp3_uint32 drmp3_decode_next_frame_ex__memory(drmp3* pMP3, drmp3d_sample_t* pPCMFrames)
2889
+ static drmp3_uint32 drmp3_decode_next_frame_ex__memory(drmp3* pMP3, drmp3d_sample_t* pPCMFrames, drmp3dec_frame_info* pMP3FrameInfo, const drmp3_uint8** ppMP3FrameData)
2748
2890
  {
2749
2891
  drmp3_uint32 pcmFramesRead = 0;
2750
2892
  drmp3dec_frame_info info;
@@ -2763,11 +2905,25 @@ static drmp3_uint32 drmp3_decode_next_frame_ex__memory(drmp3* pMP3, drmp3d_sampl
2763
2905
  pMP3->pcmFramesConsumedInMP3Frame = 0;
2764
2906
  pMP3->pcmFramesRemainingInMP3Frame = pcmFramesRead;
2765
2907
  pMP3->mp3FrameChannels = info.channels;
2766
- pMP3->mp3FrameSampleRate = info.hz;
2908
+ pMP3->mp3FrameSampleRate = info.sample_rate;
2909
+
2910
+ if (pMP3FrameInfo != NULL) {
2911
+ *pMP3FrameInfo = info;
2912
+ }
2913
+
2914
+ if (ppMP3FrameData != NULL) {
2915
+ *ppMP3FrameData = pMP3->memory.pData + pMP3->memory.currentReadPos;
2916
+ }
2917
+
2767
2918
  break;
2768
2919
  } else if (info.frame_bytes > 0) {
2769
2920
  /* No frames were read, but it looks like we skipped past one. Read the next MP3 frame. */
2921
+ if (info.layer != 0) {
2922
+ pMP3->mp3FramesSkipped += 1;
2923
+ }
2924
+
2770
2925
  pMP3->memory.currentReadPos += (size_t)info.frame_bytes;
2926
+ pMP3->streamCursor += (size_t)info.frame_bytes;
2771
2927
  } else {
2772
2928
  /* Nothing at all was read. Abort. */
2773
2929
  break;
@@ -2776,23 +2932,26 @@ static drmp3_uint32 drmp3_decode_next_frame_ex__memory(drmp3* pMP3, drmp3d_sampl
2776
2932
 
2777
2933
  /* Consume the data. */
2778
2934
  pMP3->memory.currentReadPos += (size_t)info.frame_bytes;
2935
+ pMP3->streamCursor += (size_t)info.frame_bytes;
2779
2936
 
2780
2937
  return pcmFramesRead;
2781
2938
  }
2782
2939
 
2783
- static drmp3_uint32 drmp3_decode_next_frame_ex(drmp3* pMP3, drmp3d_sample_t* pPCMFrames)
2940
+ static drmp3_uint32 drmp3_decode_next_frame_ex(drmp3* pMP3, drmp3d_sample_t* pPCMFrames, drmp3dec_frame_info* pMP3FrameInfo, const drmp3_uint8** ppMP3FrameData)
2784
2941
  {
2942
+ pMP3->mp3FramesSkipped = 0;
2943
+
2785
2944
  if (pMP3->memory.pData != NULL && pMP3->memory.dataSize > 0) {
2786
- return drmp3_decode_next_frame_ex__memory(pMP3, pPCMFrames);
2945
+ return drmp3_decode_next_frame_ex__memory(pMP3, pPCMFrames, pMP3FrameInfo, ppMP3FrameData);
2787
2946
  } else {
2788
- return drmp3_decode_next_frame_ex__callbacks(pMP3, pPCMFrames);
2947
+ return drmp3_decode_next_frame_ex__callbacks(pMP3, pPCMFrames, pMP3FrameInfo, ppMP3FrameData);
2789
2948
  }
2790
2949
  }
2791
2950
 
2792
2951
  static drmp3_uint32 drmp3_decode_next_frame(drmp3* pMP3)
2793
2952
  {
2794
2953
  DRMP3_ASSERT(pMP3 != NULL);
2795
- return drmp3_decode_next_frame_ex(pMP3, (drmp3d_sample_t*)pMP3->pcmFrames);
2954
+ return drmp3_decode_next_frame_ex(pMP3, (drmp3d_sample_t*)pMP3->pcmFrames, NULL, NULL);
2796
2955
  }
2797
2956
 
2798
2957
  #if 0
@@ -2802,7 +2961,7 @@ static drmp3_uint32 drmp3_seek_next_frame(drmp3* pMP3)
2802
2961
 
2803
2962
  DRMP3_ASSERT(pMP3 != NULL);
2804
2963
 
2805
- pcmFrameCount = drmp3_decode_next_frame_ex(pMP3, NULL);
2964
+ pcmFrameCount = drmp3_decode_next_frame_ex(pMP3, NULL, NULL, NULL);
2806
2965
  if (pcmFrameCount == 0) {
2807
2966
  return 0;
2808
2967
  }
@@ -2816,8 +2975,13 @@ static drmp3_uint32 drmp3_seek_next_frame(drmp3* pMP3)
2816
2975
  }
2817
2976
  #endif
2818
2977
 
2819
- static drmp3_bool32 drmp3_init_internal(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, const drmp3_allocation_callbacks* pAllocationCallbacks)
2978
+ static drmp3_bool32 drmp3_init_internal(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, drmp3_meta_proc onMeta, void* pUserData, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks)
2820
2979
  {
2980
+ drmp3dec_frame_info firstFrameInfo;
2981
+ const drmp3_uint8* pFirstFrameData;
2982
+ drmp3_uint32 firstFramePCMFrameCount;
2983
+ drmp3_uint32 detectedMP3FrameCount = 0xFFFFFFFF;
2984
+
2821
2985
  DRMP3_ASSERT(pMP3 != NULL);
2822
2986
  DRMP3_ASSERT(onRead != NULL);
2823
2987
 
@@ -2826,104 +2990,589 @@ static drmp3_bool32 drmp3_init_internal(drmp3* pMP3, drmp3_read_proc onRead, drm
2826
2990
 
2827
2991
  pMP3->onRead = onRead;
2828
2992
  pMP3->onSeek = onSeek;
2993
+ pMP3->onMeta = onMeta;
2829
2994
  pMP3->pUserData = pUserData;
2995
+ pMP3->pUserDataMeta = pUserDataMeta;
2830
2996
  pMP3->allocationCallbacks = drmp3_copy_allocation_callbacks_or_defaults(pAllocationCallbacks);
2831
2997
 
2832
2998
  if (pMP3->allocationCallbacks.onFree == NULL || (pMP3->allocationCallbacks.onMalloc == NULL && pMP3->allocationCallbacks.onRealloc == NULL)) {
2833
2999
  return DRMP3_FALSE; /* Invalid allocation callbacks. */
2834
3000
  }
2835
3001
 
2836
- /* Decode the first frame to confirm that it is indeed a valid MP3 stream. */
2837
- if (drmp3_decode_next_frame(pMP3) == 0) {
2838
- drmp3__free_from_callbacks(pMP3->pData, &pMP3->allocationCallbacks); /* The call above may have allocated memory. Need to make sure it's freed before aborting. */
2839
- return DRMP3_FALSE; /* Not a valid MP3 stream. */
2840
- }
3002
+ pMP3->streamCursor = 0;
3003
+ pMP3->streamLength = DRMP3_UINT64_MAX;
3004
+ pMP3->streamStartOffset = 0;
3005
+ pMP3->delayInPCMFrames = 0;
3006
+ pMP3->paddingInPCMFrames = 0;
3007
+ pMP3->totalPCMFrameCount = DRMP3_UINT64_MAX;
3008
+
3009
+ /* We'll first check for trailing tags. */
3010
+ #if 1
3011
+ if (onSeek != NULL && onTell != NULL) {
3012
+ if (onSeek(pUserData, 0, DRMP3_SEEK_END)) {
3013
+ drmp3_int64 streamLen;
3014
+ int streamEndOffset = 0;
3015
+
3016
+ /* First get the length of the stream. We need this so we can ensure the stream is big enough to store the tags. */
3017
+ if (onTell(pUserData, &streamLen)) {
3018
+ /* Run this in a loop because removing a tag can expose another tag at the new end of the stream. */
3019
+ for (;;) {
3020
+ drmp3_int64 streamLenBeforeTags = streamLen;
3021
+
3022
+ /* ID3v1 */
3023
+ if (streamLen > 128 && streamEndOffset >= INT_MIN + 128) {
3024
+ char id3[3];
3025
+ if (onSeek(pUserData, streamEndOffset - 128, DRMP3_SEEK_END)) {
3026
+ if (onRead(pUserData, id3, 3) == 3 && id3[0] == 'T' && id3[1] == 'A' && id3[2] == 'G') {
3027
+ /* We have an ID3v1 tag. */
3028
+ streamEndOffset -= 128;
3029
+ streamLen -= 128;
3030
+
3031
+ /* Fire a metadata callback for the TAG data. */
3032
+ if (onMeta != NULL) {
3033
+ drmp3_uint8 tag[128];
3034
+ tag[0] = 'T'; tag[1] = 'A'; tag[2] = 'G';
3035
+
3036
+ if (onRead(pUserData, tag + 3, 125) == 125) {
3037
+ drmp3__on_meta(pMP3, DRMP3_METADATA_TYPE_ID3V1, tag, 128);
3038
+ }
3039
+ }
3040
+ } else {
3041
+ /* No ID3v1 tag. */
3042
+ }
3043
+ } else {
3044
+ /* Failed to seek to the ID3v1 tag. */
3045
+ }
3046
+ } else {
3047
+ /* Stream too short or seek offset out of range. No ID3v1 tag. */
3048
+ }
2841
3049
 
2842
- pMP3->channels = pMP3->mp3FrameChannels;
2843
- pMP3->sampleRate = pMP3->mp3FrameSampleRate;
3050
+ /* APE */
3051
+ if (streamLen > 32 && streamEndOffset >= INT_MIN + 32) {
3052
+ drmp3_uint8 ape[32]; /* The footer. */
3053
+
3054
+ if (onSeek(pUserData, streamEndOffset - 32, DRMP3_SEEK_END)) {
3055
+ if (onRead(pUserData, ape, 32) == 32 && ape[0] == 'A' && ape[1] == 'P' && ape[2] == 'E' && ape[3] == 'T' && ape[4] == 'A' && ape[5] == 'G' && ape[6] == 'E' && ape[7] == 'X') {
3056
+ drmp3_uint32 apeVersion;
3057
+ drmp3_uint32 tagSize;
3058
+ drmp3_int64 totalTagSize;
3059
+
3060
+ apeVersion =
3061
+ ((drmp3_uint32)ape[8] << 0) |
3062
+ ((drmp3_uint32)ape[9] << 8) |
3063
+ ((drmp3_uint32)ape[10] << 16) |
3064
+ ((drmp3_uint32)ape[11] << 24);
3065
+
3066
+ tagSize =
3067
+ ((drmp3_uint32)ape[12] << 0) |
3068
+ ((drmp3_uint32)ape[13] << 8) |
3069
+ ((drmp3_uint32)ape[14] << 16) |
3070
+ ((drmp3_uint32)ape[15] << 24);
3071
+
3072
+ /* The tag size includes the footer, but not the optional APEv2 header. */
3073
+ totalTagSize = tagSize;
3074
+ if (apeVersion >= 2000 && (ape[23] & 0x80) != 0) {
3075
+ totalTagSize += 32;
3076
+ }
3077
+
3078
+ /* The tag must contain a footer, and the end-relative seek offset must fit in an int. */
3079
+ if (tagSize >= 32 && totalTagSize < streamLen && totalTagSize <= (drmp3_int64)streamEndOffset - INT_MIN) {
3080
+ streamEndOffset = (int)(streamEndOffset - totalTagSize);
3081
+ streamLen -= totalTagSize;
3082
+
3083
+ /* Fire a metadata callback for the APE data. Must include both the main content and footer. */
3084
+ if (onMeta != NULL) {
3085
+ /* We first need to seek to the start of the APE tag. */
3086
+ if (onSeek(pUserData, streamEndOffset, DRMP3_SEEK_END)) {
3087
+ size_t apeTagSize = (size_t)totalTagSize;
3088
+ drmp3_uint8* pTagData;
3089
+
3090
+ pTagData = (drmp3_uint8*)drmp3_malloc(apeTagSize, pAllocationCallbacks);
3091
+ if (pTagData != NULL) {
3092
+ if (onRead(pUserData, pTagData, apeTagSize) == apeTagSize) {
3093
+ drmp3__on_meta(pMP3, DRMP3_METADATA_TYPE_APE, pTagData, apeTagSize);
3094
+ }
3095
+
3096
+ drmp3_free(pTagData, pAllocationCallbacks);
3097
+ }
3098
+ }
3099
+ }
3100
+ } else {
3101
+ /* Invalid or unsupported APE tag size. */
3102
+ }
3103
+ }
3104
+ }
3105
+ } else {
3106
+ /* Stream too short or seek offset out of range. No APE tag. */
3107
+ }
2844
3108
 
2845
- return DRMP3_TRUE;
2846
- }
3109
+ /* Lyrics3v2 */
3110
+ if (streamLen > 15 && streamEndOffset >= INT_MIN + 15) {
3111
+ drmp3_uint8 footer[15];
3112
+
3113
+ if (onSeek(pUserData, streamEndOffset - (int)sizeof(footer), DRMP3_SEEK_END)) {
3114
+ if (onRead(pUserData, footer, sizeof(footer)) == sizeof(footer) && DRMP3_CMP_MEMORY(footer + 6, "LYRICS200", 9) == 0) {
3115
+ drmp3_uint32 tagSize = 0;
3116
+ drmp3_uint32 totalTagSize;
3117
+ int i;
3118
+
3119
+ /* The six-digit size includes the header and fields, but not the 15-byte footer. */
3120
+ for (i = 0; i < 6; i += 1) {
3121
+ if (footer[i] < '0' || footer[i] > '9') {
3122
+ break;
3123
+ }
3124
+
3125
+ tagSize = tagSize * 10 + (footer[i] - '0');
3126
+ }
3127
+
3128
+ totalTagSize = tagSize + sizeof(footer);
3129
+ if (i == 6 && tagSize >= 11 && totalTagSize < streamLen && totalTagSize <= (drmp3_int64)streamEndOffset - INT_MIN) {
3130
+ drmp3_uint8 header[11];
3131
+ int tagOffset;
3132
+
3133
+ tagOffset = streamEndOffset - (int)totalTagSize;
3134
+ if (onSeek(pUserData, tagOffset, DRMP3_SEEK_END)) {
3135
+ if (onRead(pUserData, header, sizeof(header)) == sizeof(header) && DRMP3_CMP_MEMORY(header, "LYRICSBEGIN", sizeof(header)) == 0) {
3136
+ streamEndOffset = tagOffset;
3137
+ streamLen -= totalTagSize;
3138
+
3139
+ /* Include the header, fields, size descriptor and footer in the callback. */
3140
+ if (onMeta != NULL) {
3141
+ drmp3_uint8* pTagData;
3142
+
3143
+ pTagData = (drmp3_uint8*)drmp3_malloc(totalTagSize, pAllocationCallbacks);
3144
+ if (pTagData != NULL) {
3145
+ DRMP3_COPY_MEMORY(pTagData, header, sizeof(header));
3146
+
3147
+ if (onRead(pUserData, pTagData + sizeof(header), totalTagSize - sizeof(header)) == totalTagSize - sizeof(header)) {
3148
+ drmp3__on_meta(pMP3, DRMP3_METADATA_TYPE_LYRICS3V2, pTagData, totalTagSize);
3149
+ }
3150
+
3151
+ drmp3_free(pTagData, pAllocationCallbacks);
3152
+ }
3153
+ }
3154
+ }
3155
+ }
3156
+ }
3157
+ }
3158
+ }
3159
+ } else {
3160
+ /* Stream too short or seek offset out of range. No Lyrics3v2 tag. */
3161
+ }
2847
3162
 
2848
- DRMP3_API drmp3_bool32 drmp3_init(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, const drmp3_allocation_callbacks* pAllocationCallbacks)
2849
- {
2850
- if (pMP3 == NULL || onRead == NULL) {
2851
- return DRMP3_FALSE;
3163
+ if (streamLen == streamLenBeforeTags) {
3164
+ break;
3165
+ }
3166
+ }
3167
+
3168
+ /* Seek back to the start. */
3169
+ if (!onSeek(pUserData, 0, DRMP3_SEEK_SET)) {
3170
+ return DRMP3_FALSE; /* Failed to seek back to the start. */
3171
+ }
3172
+
3173
+ pMP3->streamLength = (drmp3_uint64)streamLen;
3174
+
3175
+ if (pMP3->memory.pData != NULL) {
3176
+ pMP3->memory.dataSize = (size_t)pMP3->streamLength;
3177
+ }
3178
+ } else {
3179
+ /* Failed to get the length of the stream. Trailing tags cannot be skipped. */
3180
+ if (!onSeek(pUserData, 0, DRMP3_SEEK_SET)) {
3181
+ return DRMP3_FALSE; /* Failed to seek back to the start. */
3182
+ }
3183
+ }
3184
+ } else {
3185
+ /* Failed to seek to the end. Cannot skip trailing tags. */
3186
+ }
3187
+ } else {
3188
+ /* No onSeek or onTell callback. Cannot skip trailing tags. */
2852
3189
  }
3190
+ #endif
2853
3191
 
2854
- DRMP3_ZERO_OBJECT(pMP3);
2855
- return drmp3_init_internal(pMP3, onRead, onSeek, pUserData, pAllocationCallbacks);
2856
- }
2857
3192
 
3193
+ /* ID3v2 tags */
3194
+ #if 1
3195
+ {
3196
+ char header[10];
3197
+ if (onRead(pUserData, header, 10) == 10) {
3198
+ if (header[0] == 'I' && header[1] == 'D' && header[2] == '3') {
3199
+ drmp3_uint32 tagSize =
3200
+ (((drmp3_uint32)header[6] & 0x7F) << 21) |
3201
+ (((drmp3_uint32)header[7] & 0x7F) << 14) |
3202
+ (((drmp3_uint32)header[8] & 0x7F) << 7) |
3203
+ (((drmp3_uint32)header[9] & 0x7F) << 0);
3204
+
3205
+ /* Account for the footer. */
3206
+ if (header[5] & 0x10) {
3207
+ tagSize += 10;
3208
+ }
2858
3209
 
2859
- static size_t drmp3__on_read_memory(void* pUserData, void* pBufferOut, size_t bytesToRead)
2860
- {
2861
- drmp3* pMP3 = (drmp3*)pUserData;
2862
- size_t bytesRemaining;
3210
+ /* Read the tag content and fire a metadata callback. */
3211
+ if (onMeta != NULL) {
3212
+ size_t tagSizeWithHeader = 10 + tagSize;
3213
+ drmp3_uint8* pTagData = (drmp3_uint8*)drmp3_malloc(tagSizeWithHeader, pAllocationCallbacks);
3214
+ if (pTagData != NULL) {
3215
+ DRMP3_COPY_MEMORY(pTagData, header, 10);
2863
3216
 
2864
- DRMP3_ASSERT(pMP3 != NULL);
2865
- DRMP3_ASSERT(pMP3->memory.dataSize >= pMP3->memory.currentReadPos);
3217
+ if (onRead(pUserData, pTagData + 10, tagSize) == tagSize) {
3218
+ drmp3__on_meta(pMP3, DRMP3_METADATA_TYPE_ID3V2, pTagData, tagSizeWithHeader);
3219
+ }
2866
3220
 
2867
- bytesRemaining = pMP3->memory.dataSize - pMP3->memory.currentReadPos;
2868
- if (bytesToRead > bytesRemaining) {
2869
- bytesToRead = bytesRemaining;
2870
- }
3221
+ drmp3_free(pTagData, pAllocationCallbacks);
3222
+ }
3223
+ } else {
3224
+ /* Don't have a metadata callback, so just skip the tag. */
3225
+ if (onSeek != NULL) {
3226
+ if (!onSeek(pUserData, tagSize, DRMP3_SEEK_CUR)) {
3227
+ return DRMP3_FALSE; /* Failed to seek past the ID3v2 tag. */
3228
+ }
3229
+ } else {
3230
+ /* Don't have a seek callback. Read and discard. */
3231
+ char discard[1024];
2871
3232
 
2872
- if (bytesToRead > 0) {
2873
- DRMP3_COPY_MEMORY(pBufferOut, pMP3->memory.pData + pMP3->memory.currentReadPos, bytesToRead);
2874
- pMP3->memory.currentReadPos += bytesToRead;
2875
- }
3233
+ while (tagSize > 0) {
3234
+ size_t bytesToRead = tagSize;
3235
+ if (bytesToRead > sizeof(discard)) {
3236
+ bytesToRead = sizeof(discard);
3237
+ }
2876
3238
 
2877
- return bytesToRead;
2878
- }
3239
+ if (onRead(pUserData, discard, bytesToRead) != bytesToRead) {
3240
+ return DRMP3_FALSE; /* Failed to read data. */
3241
+ }
2879
3242
 
2880
- static drmp3_bool32 drmp3__on_seek_memory(void* pUserData, int byteOffset, drmp3_seek_origin origin)
2881
- {
2882
- drmp3* pMP3 = (drmp3*)pUserData;
3243
+ tagSize -= (drmp3_uint32)bytesToRead;
3244
+ }
3245
+ }
3246
+ }
2883
3247
 
2884
- DRMP3_ASSERT(pMP3 != NULL);
3248
+ pMP3->streamStartOffset += 10 + tagSize; /* +10 for the header. */
3249
+ pMP3->streamCursor = pMP3->streamStartOffset;
3250
+ } else {
3251
+ /* Not an ID3v2 tag. Seek back to the start. */
3252
+ if (onSeek != NULL) {
3253
+ if (!onSeek(pUserData, 0, DRMP3_SEEK_SET)) {
3254
+ return DRMP3_FALSE; /* Failed to seek back to the start. */
3255
+ }
3256
+ } else {
3257
+ /*
3258
+ Don't have a seek callback to move backwards. Put the header into the cache so decoding starts with it; otherwise the
3259
+ first frame is lost (and with it a Xing/Info tag). There is no need to handle the memory case because that will always
3260
+ have a seek implementation and will never hit this code path.
3261
+ */
3262
+ pMP3->pData = (drmp3_uint8*)drmp3__malloc_from_callbacks(DRMP3_DATA_CHUNK_SIZE, &pMP3->allocationCallbacks);
3263
+ if (pMP3->pData == NULL) {
3264
+ return DRMP3_FALSE; /* Out of memory. */
3265
+ }
2885
3266
 
2886
- if (origin == drmp3_seek_origin_current) {
2887
- if (byteOffset > 0) {
2888
- if (pMP3->memory.currentReadPos + byteOffset > pMP3->memory.dataSize) {
2889
- byteOffset = (int)(pMP3->memory.dataSize - pMP3->memory.currentReadPos); /* Trying to seek too far forward. */
2890
- }
2891
- } else {
2892
- if (pMP3->memory.currentReadPos < (size_t)-byteOffset) {
2893
- byteOffset = -(int)pMP3->memory.currentReadPos; /* Trying to seek too far backwards. */
3267
+ pMP3->dataCapacity = DRMP3_DATA_CHUNK_SIZE;
3268
+ DRMP3_COPY_MEMORY(pMP3->pData, header, sizeof(header));
3269
+ pMP3->dataSize = sizeof(header);
3270
+ pMP3->streamCursor = sizeof(header);
3271
+ }
2894
3272
  }
2895
- }
2896
-
2897
- /* This will never underflow thanks to the clamps above. */
2898
- pMP3->memory.currentReadPos += byteOffset;
2899
- } else {
2900
- if ((drmp3_uint32)byteOffset <= pMP3->memory.dataSize) {
2901
- pMP3->memory.currentReadPos = byteOffset;
2902
3273
  } else {
2903
- pMP3->memory.currentReadPos = pMP3->memory.dataSize; /* Trying to seek too far forward. */
3274
+ /* Failed to read the header. We can return false here. If we couldn't read 10 bytes there's no way we'll have a valid MP3 stream. */
3275
+ return DRMP3_FALSE;
2904
3276
  }
2905
3277
  }
3278
+ #endif
2906
3279
 
2907
- return DRMP3_TRUE;
2908
- }
3280
+ /*
3281
+ Decode the first frame to confirm that it is indeed a valid MP3 stream. Note that it's possible the first frame
3282
+ is actually a Xing/LAME/VBRI header. If this is the case we need to skip over it.
3283
+ */
3284
+ firstFramePCMFrameCount = drmp3_decode_next_frame_ex(pMP3, (drmp3d_sample_t*)pMP3->pcmFrames, &firstFrameInfo, &pFirstFrameData);
3285
+ if (firstFramePCMFrameCount > 0) {
3286
+ DRMP3_ASSERT(pFirstFrameData != NULL);
2909
3287
 
2910
- DRMP3_API drmp3_bool32 drmp3_init_memory(drmp3* pMP3, const void* pData, size_t dataSize, const drmp3_allocation_callbacks* pAllocationCallbacks)
2911
- {
2912
- if (pMP3 == NULL) {
2913
- return DRMP3_FALSE;
2914
- }
3288
+ /*
3289
+ It might be a header. If so, we need to clear out the cached PCM frames in order to trigger a reload of fresh
3290
+ data when decoding starts. We can assume all validation has already been performed to check if this is a valid
3291
+ MP3 frame and that there is more than 0 bytes making up the frame.
2915
3292
 
2916
- DRMP3_ZERO_OBJECT(pMP3);
3293
+ We're going to be basing this parsing code off the minimp3_ex implementation.
3294
+ */
3295
+ #if 1
3296
+ DRMP3_ASSERT(firstFrameInfo.frame_bytes > 0);
3297
+ {
3298
+ drmp3_bs bs;
3299
+ drmp3_L3_gr_info grInfo[4];
2917
3300
 
2918
- if (pData == NULL || dataSize == 0) {
2919
- return DRMP3_FALSE;
3301
+ drmp3_bs_init(&bs, pFirstFrameData + DRMP3_HDR_SIZE, firstFrameInfo.frame_bytes - DRMP3_HDR_SIZE);
3302
+
3303
+ if (DRMP3_HDR_IS_CRC(pFirstFrameData)) {
3304
+ drmp3_bs_get_bits(&bs, 16); /* CRC. */
3305
+ }
3306
+
3307
+ if (drmp3_L3_read_side_info(&bs, grInfo, pFirstFrameData) >= 0) {
3308
+ drmp3_bool32 isXing = DRMP3_FALSE;
3309
+ drmp3_bool32 isInfo = DRMP3_FALSE;
3310
+ const drmp3_uint8* pTagData;
3311
+ const drmp3_uint8* pTagDataBeg;
3312
+ const void* pDataBufferEnd = NULL;
3313
+ size_t frameBytes;
3314
+
3315
+ pTagDataBeg = pFirstFrameData + DRMP3_HDR_SIZE + (bs.pos/8);
3316
+ pTagData = pTagDataBeg;
3317
+
3318
+ /*
3319
+ We need to determine how many bytes are actually available in pTagData. Unfortunately this is different depending on
3320
+ whether or not it's being decoded from memory or callbacks.
3321
+ */
3322
+ if (pMP3->memory.pData != NULL && pMP3->memory.dataSize > 0) {
3323
+ pDataBufferEnd = pMP3->memory.pData + pMP3->memory.dataSize;
3324
+ } else {
3325
+ pDataBufferEnd = pMP3->pData + pMP3->dataCapacity;
3326
+ }
3327
+
3328
+ frameBytes = DRMP3_MIN((size_t)firstFrameInfo.frame_bytes, (size_t)((drmp3_uint8*)pDataBufferEnd - pFirstFrameData));
3329
+
3330
+ if (frameBytes < (size_t)(pTagData - pFirstFrameData) + 8) {
3331
+ goto done_xing_info; /* Frame too small for a Xing/Info tag. */
3332
+ }
3333
+
3334
+ /* Check for both "Xing" and "Info" identifiers. */
3335
+ isXing = (pTagData[0] == 'X' && pTagData[1] == 'i' && pTagData[2] == 'n' && pTagData[3] == 'g');
3336
+ isInfo = (pTagData[0] == 'I' && pTagData[1] == 'n' && pTagData[2] == 'f' && pTagData[3] == 'o');
3337
+
3338
+ if (isXing || isInfo) {
3339
+ drmp3_uint32 bytes = 0;
3340
+ drmp3_uint32 flags = pTagData[7];
3341
+
3342
+ pTagData += 8; /* Skip past the ID and flags. */
3343
+
3344
+ if (flags & 0x01) { /* FRAMES flag. */
3345
+ if (frameBytes < (size_t)(pTagData - pFirstFrameData) + 4) {
3346
+ goto done_xing_info; /* Invalid Xing/Info tag. */
3347
+ }
3348
+
3349
+ detectedMP3FrameCount = (drmp3_uint32)pTagData[0] << 24 | (drmp3_uint32)pTagData[1] << 16 | (drmp3_uint32)pTagData[2] << 8 | (drmp3_uint32)pTagData[3];
3350
+ pTagData += 4;
3351
+ }
3352
+
3353
+ if (flags & 0x02) { /* BYTES flag. */
3354
+ if (frameBytes < (size_t)(pTagData - pFirstFrameData) + 4) {
3355
+ goto done_xing_info; /* Invalid Xing/Info tag. */
3356
+ }
3357
+
3358
+ bytes = (drmp3_uint32)pTagData[0] << 24 | (drmp3_uint32)pTagData[1] << 16 | (drmp3_uint32)pTagData[2] << 8 | (drmp3_uint32)pTagData[3];
3359
+ (void)bytes; /* <-- Just to silence a warning about `bytes` being assigned but unused. Want to leave this here in case I want to make use of it later. */
3360
+ pTagData += 4;
3361
+ }
3362
+
3363
+ if (flags & 0x04) { /* TOC flag. */
3364
+ if (frameBytes < (size_t)(pTagData - pFirstFrameData) + 100) {
3365
+ goto done_xing_info; /* Invalid Xing/Info tag. */
3366
+ }
3367
+
3368
+ /* TODO: Extract and bind seek points. */
3369
+ pTagData += 100;
3370
+ }
3371
+
3372
+ if (flags & 0x08) { /* SCALE flag. */
3373
+ if (frameBytes < (size_t)(pTagData - pFirstFrameData) + 4) {
3374
+ goto done_xing_info; /* Invalid Xing/Info tag. */
3375
+ }
3376
+
3377
+ pTagData += 4;
3378
+ }
3379
+
3380
+ /* At this point we're done with the Xing/Info header. Now we can look at the LAME data. */
3381
+ if (frameBytes < (size_t)(pTagData - pFirstFrameData) + 1) {
3382
+ goto done_xing_info; /* Not enough data left to check for a LAME header. */
3383
+ }
3384
+
3385
+ if (pTagData[0]) {
3386
+ int delayInPCMFrames;
3387
+ int paddingInPCMFrames;
3388
+
3389
+ if (frameBytes < (size_t)(pTagData - pFirstFrameData) + 36) {
3390
+ goto done_xing_info; /* Invalid Xing/Info tag. */
3391
+ }
3392
+
3393
+ pTagData += 21;
3394
+
3395
+ delayInPCMFrames = (( (drmp3_uint32)pTagData[0] << 4) | ((drmp3_uint32)pTagData[1] >> 4)) + (528 + 1);
3396
+ paddingInPCMFrames = ((((drmp3_uint32)pTagData[1] & 0xF) << 8) | ((drmp3_uint32)pTagData[2] )) - (528 + 1);
3397
+ if (paddingInPCMFrames < 0) {
3398
+ paddingInPCMFrames = 0; /* Padding cannot be negative. Probably a malformed file. Ignore. */
3399
+ }
3400
+
3401
+ pMP3->delayInPCMFrames = (drmp3_uint32)delayInPCMFrames;
3402
+ pMP3->paddingInPCMFrames = (drmp3_uint32)paddingInPCMFrames;
3403
+ }
3404
+
3405
+ /*
3406
+ My understanding is that if the "Xing" header is present we can consider this to be a VBR stream and if the "Info" header is
3407
+ present it's a CBR stream. If this is not the case let me know! I'm just tracking this for the time being in case I want to
3408
+ look at doing some CBR optimizations later on, such as faster seeking.
3409
+ */
3410
+ if (isXing) {
3411
+ pMP3->isVBR = DRMP3_TRUE;
3412
+ } else if (isInfo) {
3413
+ pMP3->isCBR = DRMP3_TRUE;
3414
+ }
3415
+
3416
+ /* Post the raw data of the tag to the metadata callback. */
3417
+ if (onMeta != NULL) {
3418
+ drmp3_metadata_type metadataType = isXing ? DRMP3_METADATA_TYPE_XING : DRMP3_METADATA_TYPE_VBRI;
3419
+ size_t tagDataSize;
3420
+
3421
+ tagDataSize = frameBytes;
3422
+ tagDataSize -= (size_t)(pTagDataBeg - pFirstFrameData);
3423
+
3424
+ drmp3__on_meta(pMP3, metadataType, pTagDataBeg, tagDataSize);
3425
+ }
3426
+
3427
+ /* Since this was identified as a tag, we don't want to treat it as audio. We need to clear out the PCM cache. */
3428
+ pMP3->pcmFramesRemainingInMP3Frame = 0;
3429
+
3430
+ /* Start after this frame, including any bytes skipped before it, so seeking does not decode the tag as audio. */
3431
+ pMP3->streamStartOffset = pMP3->streamCursor - pMP3->dataSize;
3432
+
3433
+ /*
3434
+ The internal decoder needs to be reset to clear out any state. If we don't reset this state, it's possible for
3435
+ there to be inconsistencies in the number of samples read when reading to the end of the stream depending on
3436
+ whether or not the caller seeks to the start of the stream.
3437
+ */
3438
+ drmp3dec_init(&pMP3->decoder);
3439
+ }
3440
+
3441
+ done_xing_info:;
3442
+ } else {
3443
+ /* Failed to read the side info. */
3444
+ }
3445
+ }
3446
+ #endif
3447
+ } else {
3448
+ /* Not a valid MP3 stream. */
3449
+ drmp3__free_from_callbacks(pMP3->pData, &pMP3->allocationCallbacks); /* The call above may have allocated memory. Need to make sure it's freed before aborting. */
3450
+ return DRMP3_FALSE;
3451
+ }
3452
+
3453
+ if (detectedMP3FrameCount != 0xFFFFFFFF) {
3454
+ pMP3->totalPCMFrameCount = (drmp3_uint64)detectedMP3FrameCount * firstFramePCMFrameCount;
3455
+ }
3456
+
3457
+ pMP3->channels = pMP3->mp3FrameChannels;
3458
+ pMP3->sampleRate = pMP3->mp3FrameSampleRate;
3459
+
3460
+ return DRMP3_TRUE;
3461
+ }
3462
+
3463
+ DRMP3_API drmp3_bool32 drmp3_init(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, drmp3_meta_proc onMeta, void* pUserData, const drmp3_allocation_callbacks* pAllocationCallbacks)
3464
+ {
3465
+ if (pMP3 == NULL || onRead == NULL) {
3466
+ return DRMP3_FALSE;
3467
+ }
3468
+
3469
+ DRMP3_ZERO_OBJECT(pMP3);
3470
+ return drmp3_init_internal(pMP3, onRead, onSeek, onTell, onMeta, pUserData, pUserData, pAllocationCallbacks);
3471
+ }
3472
+
3473
+
3474
+ static size_t drmp3__on_read_memory(void* pUserData, void* pBufferOut, size_t bytesToRead)
3475
+ {
3476
+ drmp3* pMP3 = (drmp3*)pUserData;
3477
+ size_t bytesRemaining;
3478
+
3479
+ DRMP3_ASSERT(pMP3 != NULL);
3480
+ DRMP3_ASSERT(pMP3->memory.dataSize >= pMP3->memory.currentReadPos);
3481
+
3482
+ bytesRemaining = pMP3->memory.dataSize - pMP3->memory.currentReadPos;
3483
+ if (bytesToRead > bytesRemaining) {
3484
+ bytesToRead = bytesRemaining;
3485
+ }
3486
+
3487
+ if (bytesToRead > 0) {
3488
+ DRMP3_COPY_MEMORY(pBufferOut, pMP3->memory.pData + pMP3->memory.currentReadPos, bytesToRead);
3489
+ pMP3->memory.currentReadPos += bytesToRead;
3490
+ }
3491
+
3492
+ return bytesToRead;
3493
+ }
3494
+
3495
+ static drmp3_bool32 drmp3__on_seek_memory(void* pUserData, int byteOffset, drmp3_seek_origin origin)
3496
+ {
3497
+ drmp3* pMP3 = (drmp3*)pUserData;
3498
+ drmp3_int64 newCursor;
3499
+
3500
+ DRMP3_ASSERT(pMP3 != NULL);
3501
+
3502
+ if (origin == DRMP3_SEEK_SET) {
3503
+ newCursor = 0;
3504
+ } else if (origin == DRMP3_SEEK_CUR) {
3505
+ newCursor = (drmp3_int64)pMP3->memory.currentReadPos;
3506
+ } else if (origin == DRMP3_SEEK_END) {
3507
+ newCursor = (drmp3_int64)pMP3->memory.dataSize;
3508
+ } else {
3509
+ DRMP3_ASSERT(!"Invalid seek origin");
3510
+ return DRMP3_FALSE;
3511
+ }
3512
+
3513
+ newCursor += byteOffset;
3514
+
3515
+ if (newCursor < 0) {
3516
+ return DRMP3_FALSE; /* Trying to seek prior to the start of the buffer. */
3517
+ }
3518
+ if ((size_t)newCursor > pMP3->memory.dataSize) {
3519
+ return DRMP3_FALSE; /* Trying to seek beyond the end of the buffer. */
3520
+ }
3521
+
3522
+ pMP3->memory.currentReadPos = (size_t)newCursor;
3523
+
3524
+ return DRMP3_TRUE;
3525
+ }
3526
+
3527
+ static drmp3_bool32 drmp3__on_tell_memory(void* pUserData, drmp3_int64* pCursor)
3528
+ {
3529
+ drmp3* pMP3 = (drmp3*)pUserData;
3530
+
3531
+ DRMP3_ASSERT(pMP3 != NULL);
3532
+ DRMP3_ASSERT(pCursor != NULL);
3533
+
3534
+ *pCursor = (drmp3_int64)pMP3->memory.currentReadPos;
3535
+ return DRMP3_TRUE;
3536
+ }
3537
+
3538
+ DRMP3_API drmp3_bool32 drmp3_init_memory_with_metadata(drmp3* pMP3, const void* pData, size_t dataSize, drmp3_meta_proc onMeta, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks)
3539
+ {
3540
+ drmp3_bool32 result;
3541
+
3542
+ if (pMP3 == NULL) {
3543
+ return DRMP3_FALSE;
3544
+ }
3545
+
3546
+ DRMP3_ZERO_OBJECT(pMP3);
3547
+
3548
+ if (pData == NULL || dataSize == 0) {
3549
+ return DRMP3_FALSE;
2920
3550
  }
2921
3551
 
2922
3552
  pMP3->memory.pData = (const drmp3_uint8*)pData;
2923
3553
  pMP3->memory.dataSize = dataSize;
2924
3554
  pMP3->memory.currentReadPos = 0;
2925
3555
 
2926
- return drmp3_init_internal(pMP3, drmp3__on_read_memory, drmp3__on_seek_memory, pMP3, pAllocationCallbacks);
3556
+ result = drmp3_init_internal(pMP3, drmp3__on_read_memory, drmp3__on_seek_memory, drmp3__on_tell_memory, onMeta, pMP3, pUserDataMeta, pAllocationCallbacks);
3557
+ if (result == DRMP3_FALSE) {
3558
+ return DRMP3_FALSE;
3559
+ }
3560
+
3561
+ /* Adjust the length of the memory stream to account for trailing tags. */
3562
+ if (pMP3->streamLength <= (drmp3_uint64)DRMP3_SIZE_MAX) {
3563
+ pMP3->memory.dataSize = (size_t)pMP3->streamLength; /* Safe cast. */
3564
+ }
3565
+
3566
+ if (pMP3->streamStartOffset > (drmp3_uint64)DRMP3_SIZE_MAX) {
3567
+ return DRMP3_FALSE; /* Tags too big. */
3568
+ }
3569
+
3570
+ return DRMP3_TRUE;
3571
+ }
3572
+
3573
+ DRMP3_API drmp3_bool32 drmp3_init_memory(drmp3* pMP3, const void* pData, size_t dataSize, const drmp3_allocation_callbacks* pAllocationCallbacks)
3574
+ {
3575
+ return drmp3_init_memory_with_metadata(pMP3, pData, dataSize, NULL, NULL, pAllocationCallbacks);
2927
3576
  }
2928
3577
 
2929
3578
 
@@ -2931,6 +3580,7 @@ DRMP3_API drmp3_bool32 drmp3_init_memory(drmp3* pMP3, const void* pData, size_t
2931
3580
  #include <stdio.h>
2932
3581
  #include <wchar.h> /* For wcslen(), wcsrtombs() */
2933
3582
 
3583
+ /* Errno */
2934
3584
  /* drmp3_result_from_errno() is only used inside DR_MP3_NO_STDIO for now. Move this out if it's ever used elsewhere. */
2935
3585
  #include <errno.h>
2936
3586
  static drmp3_result drmp3_result_from_errno(int e)
@@ -3052,7 +3702,7 @@ static drmp3_result drmp3_result_from_errno(int e)
3052
3702
  #ifdef ENOSYS
3053
3703
  case ENOSYS: return DRMP3_NOT_IMPLEMENTED;
3054
3704
  #endif
3055
- #ifdef ENOTEMPTY
3705
+ #if defined(ENOTEMPTY) && ENOTEMPTY != EEXIST /* In AIX, ENOTEMPTY and EEXIST use the same value. */
3056
3706
  case ENOTEMPTY: return DRMP3_DIRECTORY_NOT_EMPTY;
3057
3707
  #endif
3058
3708
  #ifdef ELOOP
@@ -3334,7 +3984,9 @@ static drmp3_result drmp3_result_from_errno(int e)
3334
3984
  default: return DRMP3_ERROR;
3335
3985
  }
3336
3986
  }
3987
+ /* End Errno */
3337
3988
 
3989
+ /* fopen */
3338
3990
  static drmp3_result drmp3_fopen(FILE** ppFile, const char* pFilePath, const char* pOpenMode)
3339
3991
  {
3340
3992
  #if defined(_MSC_VER) && _MSC_VER >= 1400
@@ -3490,7 +4142,7 @@ static drmp3_result drmp3_wfopen(FILE** ppFile, const wchar_t* pFilePath, const
3490
4142
 
3491
4143
  return DRMP3_SUCCESS;
3492
4144
  }
3493
-
4145
+ /* End fopen */
3494
4146
 
3495
4147
 
3496
4148
  static size_t drmp3__on_read_stdio(void* pUserData, void* pBufferOut, size_t bytesToRead)
@@ -3500,19 +4152,56 @@ static size_t drmp3__on_read_stdio(void* pUserData, void* pBufferOut, size_t byt
3500
4152
 
3501
4153
  static drmp3_bool32 drmp3__on_seek_stdio(void* pUserData, int offset, drmp3_seek_origin origin)
3502
4154
  {
3503
- return fseek((FILE*)pUserData, offset, (origin == drmp3_seek_origin_current) ? SEEK_CUR : SEEK_SET) == 0;
4155
+ int whence = SEEK_SET;
4156
+ if (origin == DRMP3_SEEK_CUR) {
4157
+ whence = SEEK_CUR;
4158
+ } else if (origin == DRMP3_SEEK_END) {
4159
+ whence = SEEK_END;
4160
+ }
4161
+
4162
+ return fseek((FILE*)pUserData, offset, whence) == 0;
3504
4163
  }
3505
4164
 
3506
- DRMP3_API drmp3_bool32 drmp3_init_file(drmp3* pMP3, const char* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks)
4165
+ static drmp3_bool32 drmp3__on_tell_stdio(void* pUserData, drmp3_int64* pCursor)
4166
+ {
4167
+ FILE* pFileStdio = (FILE*)pUserData;
4168
+ drmp3_int64 result;
4169
+
4170
+ /* These were all validated at a higher level. */
4171
+ DRMP3_ASSERT(pFileStdio != NULL);
4172
+ DRMP3_ASSERT(pCursor != NULL);
4173
+
4174
+ #if defined(_WIN32) && !defined(NXDK)
4175
+ #if defined(_MSC_VER) && _MSC_VER > 1200
4176
+ result = _ftelli64(pFileStdio);
4177
+ #else
4178
+ result = ftell(pFileStdio);
4179
+ #endif
4180
+ #else
4181
+ result = ftell(pFileStdio);
4182
+ #endif
4183
+
4184
+ *pCursor = result;
4185
+
4186
+ return DRMP3_TRUE;
4187
+ }
4188
+
4189
+ DRMP3_API drmp3_bool32 drmp3_init_file_with_metadata(drmp3* pMP3, const char* pFilePath, drmp3_meta_proc onMeta, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks)
3507
4190
  {
3508
4191
  drmp3_bool32 result;
3509
4192
  FILE* pFile;
3510
4193
 
4194
+ if (pMP3 == NULL) {
4195
+ return DRMP3_FALSE;
4196
+ }
4197
+
4198
+ DRMP3_ZERO_OBJECT(pMP3);
4199
+
3511
4200
  if (drmp3_fopen(&pFile, pFilePath, "rb") != DRMP3_SUCCESS) {
3512
4201
  return DRMP3_FALSE;
3513
4202
  }
3514
4203
 
3515
- result = drmp3_init(pMP3, drmp3__on_read_stdio, drmp3__on_seek_stdio, (void*)pFile, pAllocationCallbacks);
4204
+ result = drmp3_init_internal(pMP3, drmp3__on_read_stdio, drmp3__on_seek_stdio, drmp3__on_tell_stdio, onMeta, (void*)pFile, pUserDataMeta, pAllocationCallbacks);
3516
4205
  if (result != DRMP3_TRUE) {
3517
4206
  fclose(pFile);
3518
4207
  return result;
@@ -3521,16 +4210,22 @@ DRMP3_API drmp3_bool32 drmp3_init_file(drmp3* pMP3, const char* pFilePath, const
3521
4210
  return DRMP3_TRUE;
3522
4211
  }
3523
4212
 
3524
- DRMP3_API drmp3_bool32 drmp3_init_file_w(drmp3* pMP3, const wchar_t* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks)
4213
+ DRMP3_API drmp3_bool32 drmp3_init_file_with_metadata_w(drmp3* pMP3, const wchar_t* pFilePath, drmp3_meta_proc onMeta, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks)
3525
4214
  {
3526
4215
  drmp3_bool32 result;
3527
4216
  FILE* pFile;
3528
4217
 
4218
+ if (pMP3 == NULL) {
4219
+ return DRMP3_FALSE;
4220
+ }
4221
+
4222
+ DRMP3_ZERO_OBJECT(pMP3);
4223
+
3529
4224
  if (drmp3_wfopen(&pFile, pFilePath, L"rb", pAllocationCallbacks) != DRMP3_SUCCESS) {
3530
4225
  return DRMP3_FALSE;
3531
4226
  }
3532
4227
 
3533
- result = drmp3_init(pMP3, drmp3__on_read_stdio, drmp3__on_seek_stdio, (void*)pFile, pAllocationCallbacks);
4228
+ result = drmp3_init_internal(pMP3, drmp3__on_read_stdio, drmp3__on_seek_stdio, drmp3__on_tell_stdio, onMeta, (void*)pFile, pUserDataMeta, pAllocationCallbacks);
3534
4229
  if (result != DRMP3_TRUE) {
3535
4230
  fclose(pFile);
3536
4231
  return result;
@@ -3538,6 +4233,16 @@ DRMP3_API drmp3_bool32 drmp3_init_file_w(drmp3* pMP3, const wchar_t* pFilePath,
3538
4233
 
3539
4234
  return DRMP3_TRUE;
3540
4235
  }
4236
+
4237
+ DRMP3_API drmp3_bool32 drmp3_init_file(drmp3* pMP3, const char* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks)
4238
+ {
4239
+ return drmp3_init_file_with_metadata(pMP3, pFilePath, NULL, NULL, pAllocationCallbacks);
4240
+ }
4241
+
4242
+ DRMP3_API drmp3_bool32 drmp3_init_file_w(drmp3* pMP3, const wchar_t* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks)
4243
+ {
4244
+ return drmp3_init_file_with_metadata_w(pMP3, pFilePath, NULL, NULL, pAllocationCallbacks);
4245
+ }
3541
4246
  #endif
3542
4247
 
3543
4248
  DRMP3_API void drmp3_uninit(drmp3* pMP3)
@@ -3545,7 +4250,7 @@ DRMP3_API void drmp3_uninit(drmp3* pMP3)
3545
4250
  if (pMP3 == NULL) {
3546
4251
  return;
3547
4252
  }
3548
-
4253
+
3549
4254
  #ifndef DR_MP3_NO_STDIO
3550
4255
  if (pMP3->onRead == drmp3__on_read_stdio) {
3551
4256
  FILE* pFile = (FILE*)pMP3->pUserData;
@@ -3625,19 +4330,48 @@ static drmp3_uint64 drmp3_read_pcm_frames_raw(drmp3* pMP3, drmp3_uint64 framesTo
3625
4330
  DRMP3_ASSERT(pMP3->onRead != NULL);
3626
4331
 
3627
4332
  while (framesToRead > 0) {
3628
- drmp3_uint32 framesToConsume = (drmp3_uint32)DRMP3_MIN(pMP3->pcmFramesRemainingInMP3Frame, framesToRead);
4333
+ drmp3_uint32 framesToConsume;
4334
+
4335
+ /* Skip frames if necessary. */
4336
+ if (pMP3->currentPCMFrame < pMP3->delayInPCMFrames) {
4337
+ drmp3_uint32 framesToSkip = (drmp3_uint32)DRMP3_MIN(pMP3->pcmFramesRemainingInMP3Frame, pMP3->delayInPCMFrames - pMP3->currentPCMFrame);
4338
+
4339
+ pMP3->currentPCMFrame += framesToSkip;
4340
+ pMP3->pcmFramesConsumedInMP3Frame += framesToSkip;
4341
+ pMP3->pcmFramesRemainingInMP3Frame -= framesToSkip;
4342
+ }
4343
+
4344
+ framesToConsume = (drmp3_uint32)DRMP3_MIN(pMP3->pcmFramesRemainingInMP3Frame, framesToRead);
4345
+
4346
+ /* Clamp the number of frames to read to the padding. */
4347
+ if (pMP3->totalPCMFrameCount != DRMP3_UINT64_MAX && pMP3->totalPCMFrameCount > pMP3->paddingInPCMFrames) {
4348
+ if (pMP3->currentPCMFrame < (pMP3->totalPCMFrameCount - pMP3->paddingInPCMFrames)) {
4349
+ drmp3_uint64 framesRemainigToPadding = (pMP3->totalPCMFrameCount - pMP3->paddingInPCMFrames) - pMP3->currentPCMFrame;
4350
+ if (framesToConsume > framesRemainigToPadding) {
4351
+ framesToConsume = (drmp3_uint32)framesRemainigToPadding;
4352
+ }
4353
+ } else {
4354
+ /* We're into the padding. Abort. */
4355
+ break;
4356
+ }
4357
+ }
4358
+
3629
4359
  if (pBufferOut != NULL) {
3630
- #if defined(DR_MP3_FLOAT_OUTPUT)
3631
- /* f32 */
3632
- float* pFramesOutF32 = (float*)DRMP3_OFFSET_PTR(pBufferOut, sizeof(float) * totalFramesRead * pMP3->channels);
3633
- float* pFramesInF32 = (float*)DRMP3_OFFSET_PTR(&pMP3->pcmFrames[0], sizeof(float) * pMP3->pcmFramesConsumedInMP3Frame * pMP3->mp3FrameChannels);
3634
- DRMP3_COPY_MEMORY(pFramesOutF32, pFramesInF32, sizeof(float) * framesToConsume * pMP3->channels);
3635
- #else
3636
- /* s16 */
3637
- drmp3_int16* pFramesOutS16 = (drmp3_int16*)DRMP3_OFFSET_PTR(pBufferOut, sizeof(drmp3_int16) * totalFramesRead * pMP3->channels);
3638
- drmp3_int16* pFramesInS16 = (drmp3_int16*)DRMP3_OFFSET_PTR(&pMP3->pcmFrames[0], sizeof(drmp3_int16) * pMP3->pcmFramesConsumedInMP3Frame * pMP3->mp3FrameChannels);
3639
- DRMP3_COPY_MEMORY(pFramesOutS16, pFramesInS16, sizeof(drmp3_int16) * framesToConsume * pMP3->channels);
3640
- #endif
4360
+ #if defined(DR_MP3_FLOAT_OUTPUT)
4361
+ {
4362
+ /* f32 */
4363
+ float* pFramesOutF32 = (float*)DRMP3_OFFSET_PTR(pBufferOut, sizeof(float) * totalFramesRead * pMP3->channels);
4364
+ float* pFramesInF32 = (float*)DRMP3_OFFSET_PTR(&pMP3->pcmFrames[0], sizeof(float) * pMP3->pcmFramesConsumedInMP3Frame * pMP3->mp3FrameChannels);
4365
+ DRMP3_COPY_MEMORY(pFramesOutF32, pFramesInF32, sizeof(float) * framesToConsume * pMP3->channels);
4366
+ }
4367
+ #else
4368
+ {
4369
+ /* s16 */
4370
+ drmp3_int16* pFramesOutS16 = (drmp3_int16*)DRMP3_OFFSET_PTR(pBufferOut, sizeof(drmp3_int16) * totalFramesRead * pMP3->channels);
4371
+ drmp3_int16* pFramesInS16 = (drmp3_int16*)DRMP3_OFFSET_PTR(&pMP3->pcmFrames[0], sizeof(drmp3_int16) * pMP3->pcmFramesConsumedInMP3Frame * pMP3->mp3FrameChannels);
4372
+ DRMP3_COPY_MEMORY(pFramesOutS16, pFramesInS16, sizeof(drmp3_int16) * framesToConsume * pMP3->channels);
4373
+ }
4374
+ #endif
3641
4375
  }
3642
4376
 
3643
4377
  pMP3->currentPCMFrame += framesToConsume;
@@ -3650,12 +4384,14 @@ static drmp3_uint64 drmp3_read_pcm_frames_raw(drmp3* pMP3, drmp3_uint64 framesTo
3650
4384
  break;
3651
4385
  }
3652
4386
 
4387
+ /* If the cursor is already at the padding we need to abort. */
4388
+ if (pMP3->totalPCMFrameCount != DRMP3_UINT64_MAX && pMP3->totalPCMFrameCount > pMP3->paddingInPCMFrames && pMP3->currentPCMFrame >= (pMP3->totalPCMFrameCount - pMP3->paddingInPCMFrames)) {
4389
+ break;
4390
+ }
4391
+
3653
4392
  DRMP3_ASSERT(pMP3->pcmFramesRemainingInMP3Frame == 0);
3654
4393
 
3655
- /*
3656
- At this point we have exhausted our in-memory buffer so we need to re-fill. Note that the sample rate may have changed
3657
- at this point which means we'll also need to update our sample rate conversion pipeline.
3658
- */
4394
+ /* At this point we have exhausted our in-memory buffer so we need to re-fill. */
3659
4395
  if (drmp3_decode_next_frame(pMP3) == 0) {
3660
4396
  break;
3661
4397
  }
@@ -3677,7 +4413,7 @@ DRMP3_API drmp3_uint64 drmp3_read_pcm_frames_f32(drmp3* pMP3, drmp3_uint64 frame
3677
4413
  #else
3678
4414
  /* Slow path. Convert from s16 to f32. */
3679
4415
  {
3680
- drmp3_int16 pTempS16[8192];
4416
+ drmp3_int16 pTempS16[1152*2]; /* MP3 frames have a maximum per-channel sample count of 1152. Times 2 to account for stereo. */
3681
4417
  drmp3_uint64 totalPCMFramesRead = 0;
3682
4418
 
3683
4419
  while (totalPCMFramesRead < framesToRead) {
@@ -3714,7 +4450,7 @@ DRMP3_API drmp3_uint64 drmp3_read_pcm_frames_s16(drmp3* pMP3, drmp3_uint64 frame
3714
4450
  #else
3715
4451
  /* Slow path. Convert from f32 to s16. */
3716
4452
  {
3717
- float pTempF32[4096];
4453
+ float pTempF32[1152*2]; /* MP3 frames have a maximum per-channel sample count of 1152. Times 2 to account for stereo. */
3718
4454
  drmp3_uint64 totalPCMFramesRead = 0;
3719
4455
 
3720
4456
  while (totalPCMFramesRead < framesToRead) {
@@ -3757,7 +4493,7 @@ static drmp3_bool32 drmp3_seek_to_start_of_stream(drmp3* pMP3)
3757
4493
  DRMP3_ASSERT(pMP3->onSeek != NULL);
3758
4494
 
3759
4495
  /* Seek to the start of the stream to begin with. */
3760
- if (!drmp3__on_seek(pMP3, 0, drmp3_seek_origin_start)) {
4496
+ if (!drmp3__on_seek_64(pMP3, pMP3->streamStartOffset, DRMP3_SEEK_SET)) {
3761
4497
  return DRMP3_FALSE;
3762
4498
  }
3763
4499
 
@@ -3767,6 +4503,19 @@ static drmp3_bool32 drmp3_seek_to_start_of_stream(drmp3* pMP3)
3767
4503
  }
3768
4504
 
3769
4505
 
4506
+ /*
4507
+ The PCM frame the next read returns, as the caller counts them. currentPCMFrame also counts the delay frames that reading skips at the
4508
+ start of the stream, so it runs ahead by up to delayInPCMFrames.
4509
+ */
4510
+ static drmp3_uint64 drmp3_get_cursor_in_pcm_frames(const drmp3* pMP3)
4511
+ {
4512
+ if (pMP3->currentPCMFrame > pMP3->delayInPCMFrames) {
4513
+ return pMP3->currentPCMFrame - pMP3->delayInPCMFrames;
4514
+ }
4515
+
4516
+ return 0;
4517
+ }
4518
+
3770
4519
  static drmp3_bool32 drmp3_seek_forward_by_pcm_frames__brute_force(drmp3* pMP3, drmp3_uint64 frameOffset)
3771
4520
  {
3772
4521
  drmp3_uint64 framesRead;
@@ -3790,9 +4539,12 @@ static drmp3_bool32 drmp3_seek_forward_by_pcm_frames__brute_force(drmp3* pMP3, d
3790
4539
 
3791
4540
  static drmp3_bool32 drmp3_seek_to_pcm_frame__brute_force(drmp3* pMP3, drmp3_uint64 frameIndex)
3792
4541
  {
4542
+ drmp3_uint64 cursor;
4543
+
3793
4544
  DRMP3_ASSERT(pMP3 != NULL);
3794
4545
 
3795
- if (frameIndex == pMP3->currentPCMFrame) {
4546
+ cursor = drmp3_get_cursor_in_pcm_frames(pMP3);
4547
+ if (frameIndex == cursor) {
3796
4548
  return DRMP3_TRUE;
3797
4549
  }
3798
4550
 
@@ -3800,15 +4552,17 @@ static drmp3_bool32 drmp3_seek_to_pcm_frame__brute_force(drmp3* pMP3, drmp3_uint
3800
4552
  If we're moving foward we just read from where we're at. Otherwise we need to move back to the start of
3801
4553
  the stream and read from the beginning.
3802
4554
  */
3803
- if (frameIndex < pMP3->currentPCMFrame) {
4555
+ if (frameIndex < cursor) {
3804
4556
  /* Moving backward. Move to the start of the stream and then move forward. */
3805
4557
  if (!drmp3_seek_to_start_of_stream(pMP3)) {
3806
4558
  return DRMP3_FALSE;
3807
4559
  }
4560
+
4561
+ cursor = 0;
3808
4562
  }
3809
4563
 
3810
- DRMP3_ASSERT(frameIndex >= pMP3->currentPCMFrame);
3811
- return drmp3_seek_forward_by_pcm_frames__brute_force(pMP3, (frameIndex - pMP3->currentPCMFrame));
4564
+ DRMP3_ASSERT(frameIndex >= cursor);
4565
+ return drmp3_seek_forward_by_pcm_frames__brute_force(pMP3, (frameIndex - cursor));
3812
4566
  }
3813
4567
 
3814
4568
  static drmp3_bool32 drmp3_find_closest_seek_point(drmp3* pMP3, drmp3_uint64 frameIndex, drmp3_uint32* pSeekPointIndex)
@@ -3839,47 +4593,69 @@ static drmp3_bool32 drmp3_seek_to_pcm_frame__seek_table(drmp3* pMP3, drmp3_uint6
3839
4593
  {
3840
4594
  drmp3_seek_point seekPoint;
3841
4595
  drmp3_uint32 priorSeekPointIndex;
3842
- drmp3_uint16 iMP3Frame;
4596
+ drmp3_uint32 iMP3Frame;
4597
+ drmp3_uint32 mp3FramesDecoded;
4598
+ drmp3_uint64 internalFrameIndex;
3843
4599
  drmp3_uint64 leftoverFrames;
3844
4600
 
3845
4601
  DRMP3_ASSERT(pMP3 != NULL);
3846
4602
  DRMP3_ASSERT(pMP3->pSeekPoints != NULL);
3847
4603
  DRMP3_ASSERT(pMP3->seekPointCount > 0);
3848
4604
 
3849
- /* If there is no prior seekpoint it means the target PCM frame comes before the first seek point. Just assume a seekpoint at the start of the file in this case. */
3850
- if (drmp3_find_closest_seek_point(pMP3, frameIndex, &priorSeekPointIndex)) {
4605
+ /*
4606
+ If there is no prior seekpoint it means the target PCM frame comes before the first seek point. Just assume a seekpoint at the start of the MP3 stream in this case.
4607
+ Seek points count the delay frames too.
4608
+ */
4609
+ if (frameIndex > DRMP3_UINT64_MAX - pMP3->delayInPCMFrames) {
4610
+ internalFrameIndex = DRMP3_UINT64_MAX;
4611
+ } else {
4612
+ internalFrameIndex = frameIndex + pMP3->delayInPCMFrames;
4613
+ }
4614
+
4615
+ if (drmp3_find_closest_seek_point(pMP3, internalFrameIndex, &priorSeekPointIndex)) {
3851
4616
  seekPoint = pMP3->pSeekPoints[priorSeekPointIndex];
3852
4617
  } else {
3853
- seekPoint.seekPosInBytes = 0;
4618
+ seekPoint.seekPosInBytes = pMP3->streamStartOffset;
3854
4619
  seekPoint.pcmFrameIndex = 0;
3855
4620
  seekPoint.mp3FramesToDiscard = 0;
3856
4621
  seekPoint.pcmFramesToDiscard = 0;
3857
4622
  }
3858
4623
 
3859
4624
  /* First thing to do is seek to the first byte of the relevant MP3 frame. */
3860
- if (!drmp3__on_seek_64(pMP3, seekPoint.seekPosInBytes, drmp3_seek_origin_start)) {
4625
+ if (!drmp3__on_seek_64(pMP3, seekPoint.seekPosInBytes, DRMP3_SEEK_SET)) {
3861
4626
  return DRMP3_FALSE; /* Failed to seek. */
3862
4627
  }
3863
4628
 
3864
4629
  /* Clear any cached data. */
3865
4630
  drmp3_reset(pMP3);
3866
4631
 
3867
- /* Whole MP3 frames need to be discarded first. */
3868
- for (iMP3Frame = 0; iMP3Frame < seekPoint.mp3FramesToDiscard; ++iMP3Frame) {
3869
- drmp3_uint32 pcmFramesRead;
3870
- drmp3d_sample_t* pPCMFrames;
4632
+ /* Whole MP3 frames need to be discarded first. They are decoded with output so the decoder state matches a linear read. */
4633
+ iMP3Frame = 0;
4634
+ mp3FramesDecoded = 0;
4635
+ while (iMP3Frame < seekPoint.mp3FramesToDiscard) {
4636
+ if (drmp3_decode_next_frame_ex(pMP3, (drmp3d_sample_t*)pMP3->pcmFrames, NULL, NULL) == 0) {
4637
+ break;
4638
+ }
3871
4639
 
3872
- /* Pass in non-null for the last frame because we want to ensure the sample rate converter is preloaded correctly. */
3873
- pPCMFrames = NULL;
3874
- if (iMP3Frame == seekPoint.mp3FramesToDiscard-1) {
3875
- pPCMFrames = (drmp3d_sample_t*)pMP3->pcmFrames;
4640
+ iMP3Frame += 1 + pMP3->mp3FramesSkipped;
4641
+
4642
+ if (pMP3->mp3FramesSkipped == 0) {
4643
+ mp3FramesDecoded += 1;
4644
+ } else {
4645
+ mp3FramesDecoded = 1;
3876
4646
  }
4647
+ }
4648
+
4649
+ pMP3->pcmFramesConsumedInMP3Frame = 0;
4650
+ pMP3->pcmFramesRemainingInMP3Frame = 0;
3877
4651
 
3878
- /* We first need to decode the next frame. */
3879
- pcmFramesRead = drmp3_decode_next_frame_ex(pMP3, pPCMFrames);
3880
- if (pcmFramesRead == 0) {
4652
+ /* The two frames before the target need to have decoded, otherwise fall back to decoding from the start. */
4653
+ if (iMP3Frame != seekPoint.mp3FramesToDiscard || (mp3FramesDecoded < 2 && seekPoint.seekPosInBytes != pMP3->streamStartOffset)) {
4654
+ if (!drmp3_seek_to_start_of_stream(pMP3)) {
3881
4655
  return DRMP3_FALSE;
3882
4656
  }
4657
+
4658
+ return drmp3_seek_forward_by_pcm_frames__brute_force(pMP3, frameIndex);
3883
4659
  }
3884
4660
 
3885
4661
  /* We seeked to an MP3 frame in the raw stream so we need to make sure the current PCM frame is set correctly. */
@@ -3889,7 +4665,7 @@ static drmp3_bool32 drmp3_seek_to_pcm_frame__seek_table(drmp3* pMP3, drmp3_uint6
3889
4665
  Now at this point we can follow the same process as the brute force technique where we just skip over unnecessary MP3 frames and then
3890
4666
  read-and-discard at least 2 whole MP3 frames.
3891
4667
  */
3892
- leftoverFrames = frameIndex - pMP3->currentPCMFrame;
4668
+ leftoverFrames = frameIndex - drmp3_get_cursor_in_pcm_frames(pMP3);
3893
4669
  return drmp3_seek_forward_by_pcm_frames__brute_force(pMP3, leftoverFrames);
3894
4670
  }
3895
4671
 
@@ -3932,8 +4708,8 @@ DRMP3_API drmp3_bool32 drmp3_get_mp3_and_pcm_frame_count(drmp3* pMP3, drmp3_uint
3932
4708
  }
3933
4709
 
3934
4710
  /* We'll need to seek back to where we were, so grab the PCM frame we're currently sitting on so we can restore later. */
3935
- currentPCMFrame = pMP3->currentPCMFrame;
3936
-
4711
+ currentPCMFrame = drmp3_get_cursor_in_pcm_frames(pMP3);
4712
+
3937
4713
  if (!drmp3_seek_to_start_of_stream(pMP3)) {
3938
4714
  return DRMP3_FALSE;
3939
4715
  }
@@ -3944,7 +4720,7 @@ DRMP3_API drmp3_bool32 drmp3_get_mp3_and_pcm_frame_count(drmp3* pMP3, drmp3_uint
3944
4720
  for (;;) {
3945
4721
  drmp3_uint32 pcmFramesInCurrentMP3Frame;
3946
4722
 
3947
- pcmFramesInCurrentMP3Frame = drmp3_decode_next_frame_ex(pMP3, NULL);
4723
+ pcmFramesInCurrentMP3Frame = drmp3_decode_next_frame_ex(pMP3, NULL, NULL, NULL);
3948
4724
  if (pcmFramesInCurrentMP3Frame == 0) {
3949
4725
  break;
3950
4726
  }
@@ -3975,11 +4751,35 @@ DRMP3_API drmp3_bool32 drmp3_get_mp3_and_pcm_frame_count(drmp3* pMP3, drmp3_uint
3975
4751
  DRMP3_API drmp3_uint64 drmp3_get_pcm_frame_count(drmp3* pMP3)
3976
4752
  {
3977
4753
  drmp3_uint64 totalPCMFrameCount;
3978
- if (!drmp3_get_mp3_and_pcm_frame_count(pMP3, NULL, &totalPCMFrameCount)) {
4754
+
4755
+ if (pMP3 == NULL) {
3979
4756
  return 0;
3980
4757
  }
3981
4758
 
3982
- return totalPCMFrameCount;
4759
+ if (pMP3->totalPCMFrameCount != DRMP3_UINT64_MAX) {
4760
+ totalPCMFrameCount = pMP3->totalPCMFrameCount;
4761
+
4762
+ if (totalPCMFrameCount >= pMP3->delayInPCMFrames) {
4763
+ totalPCMFrameCount -= pMP3->delayInPCMFrames;
4764
+ } else {
4765
+ /* The delay is greater than the frame count reported by the Xing/Info tag. Assume it's invalid and ignore. */
4766
+ }
4767
+
4768
+ if (totalPCMFrameCount >= pMP3->paddingInPCMFrames) {
4769
+ totalPCMFrameCount -= pMP3->paddingInPCMFrames;
4770
+ } else {
4771
+ /* The padding is greater than the frame count reported by the Xing/Info tag. Assume it's invalid and ignore. */
4772
+ }
4773
+
4774
+ return totalPCMFrameCount;
4775
+ } else {
4776
+ /* Unknown frame count. Need to calculate it. */
4777
+ if (!drmp3_get_mp3_and_pcm_frame_count(pMP3, NULL, &totalPCMFrameCount)) {
4778
+ return 0;
4779
+ }
4780
+
4781
+ return totalPCMFrameCount;
4782
+ }
3983
4783
  }
3984
4784
 
3985
4785
  DRMP3_API drmp3_uint64 drmp3_get_mp3_frame_count(drmp3* pMP3)
@@ -4011,6 +4811,7 @@ typedef struct
4011
4811
  {
4012
4812
  drmp3_uint64 bytePos;
4013
4813
  drmp3_uint64 pcmFrameIndex; /* <-- After sample rate conversion. */
4814
+ drmp3_uint64 mp3FrameIndex;
4014
4815
  } drmp3__seeking_mp3_frame_info;
4015
4816
 
4016
4817
  DRMP3_API drmp3_bool32 drmp3_calculate_seek_points(drmp3* pMP3, drmp3_uint32* pSeekPointCount, drmp3_seek_point* pSeekPoints)
@@ -4030,8 +4831,8 @@ DRMP3_API drmp3_bool32 drmp3_calculate_seek_points(drmp3* pMP3, drmp3_uint32* pS
4030
4831
  }
4031
4832
 
4032
4833
  /* We'll need to seek back to the current sample after calculating the seekpoints so we need to go ahead and grab the current location at the top. */
4033
- currentPCMFrame = pMP3->currentPCMFrame;
4034
-
4834
+ currentPCMFrame = drmp3_get_cursor_in_pcm_frames(pMP3);
4835
+
4035
4836
  /* We never do more than the total number of MP3 frames and we limit it to 32-bits. */
4036
4837
  if (!drmp3_get_mp3_and_pcm_frame_count(pMP3, &totalMP3FrameCount, &totalPCMFrameCount)) {
4037
4838
  return DRMP3_FALSE;
@@ -4040,17 +4841,18 @@ DRMP3_API drmp3_bool32 drmp3_calculate_seek_points(drmp3* pMP3, drmp3_uint32* pS
4040
4841
  /* If there's less than DRMP3_SEEK_LEADING_MP3_FRAMES+1 frames we just report 1 seek point which will be the very start of the stream. */
4041
4842
  if (totalMP3FrameCount < DRMP3_SEEK_LEADING_MP3_FRAMES+1) {
4042
4843
  seekPointCount = 1;
4043
- pSeekPoints[0].seekPosInBytes = 0;
4844
+ pSeekPoints[0].seekPosInBytes = pMP3->streamStartOffset;
4044
4845
  pSeekPoints[0].pcmFrameIndex = 0;
4045
4846
  pSeekPoints[0].mp3FramesToDiscard = 0;
4046
4847
  pSeekPoints[0].pcmFramesToDiscard = 0;
4047
4848
  } else {
4048
4849
  drmp3_uint64 pcmFramesBetweenSeekPoints;
4049
4850
  drmp3__seeking_mp3_frame_info mp3FrameInfo[DRMP3_SEEK_LEADING_MP3_FRAMES+1];
4851
+ drmp3_uint32 mp3FrameInfoCount = 0;
4050
4852
  drmp3_uint64 runningPCMFrameCount = 0;
4051
4853
  float runningPCMFrameCountFractionalPart = 0;
4854
+ drmp3_uint64 runningMP3FrameCount = 0;
4052
4855
  drmp3_uint64 nextTargetPCMFrame;
4053
- drmp3_uint32 iMP3Frame;
4054
4856
  drmp3_uint32 iSeekPoint;
4055
4857
 
4056
4858
  if (seekPointCount > totalMP3FrameCount-1) {
@@ -4067,77 +4869,46 @@ DRMP3_API drmp3_bool32 drmp3_calculate_seek_points(drmp3* pMP3, drmp3_uint32* pS
4067
4869
  return DRMP3_FALSE;
4068
4870
  }
4069
4871
 
4070
- /*
4071
- We need to cache the byte positions of the previous MP3 frames. As a new MP3 frame is iterated, we cycle the byte positions in this
4072
- array. The value in the first item in this array is the byte position that will be reported in the next seek point.
4073
- */
4074
-
4075
- /* We need to initialize the array of MP3 byte positions for the leading MP3 frames. */
4076
- for (iMP3Frame = 0; iMP3Frame < DRMP3_SEEK_LEADING_MP3_FRAMES+1; ++iMP3Frame) {
4077
- drmp3_uint32 pcmFramesInCurrentMP3FrameIn;
4078
-
4079
- /* The byte position of the next frame will be the stream's cursor position, minus whatever is sitting in the buffer. */
4080
- DRMP3_ASSERT(pMP3->streamCursor >= pMP3->dataSize);
4081
- mp3FrameInfo[iMP3Frame].bytePos = pMP3->streamCursor - pMP3->dataSize;
4082
- mp3FrameInfo[iMP3Frame].pcmFrameIndex = runningPCMFrameCount;
4083
-
4084
- /* We need to get information about this frame so we can know how many samples it contained. */
4085
- pcmFramesInCurrentMP3FrameIn = drmp3_decode_next_frame_ex(pMP3, NULL);
4086
- if (pcmFramesInCurrentMP3FrameIn == 0) {
4087
- return DRMP3_FALSE; /* This should never happen. */
4088
- }
4089
-
4090
- drmp3__accumulate_running_pcm_frame_count(pMP3, pcmFramesInCurrentMP3FrameIn, &runningPCMFrameCount, &runningPCMFrameCountFractionalPart);
4091
- }
4092
-
4093
- /*
4094
- At this point we will have extracted the byte positions of the leading MP3 frames. We can now start iterating over each seek point and
4095
- calculate them.
4096
- */
4097
4872
  nextTargetPCMFrame = 0;
4098
4873
  for (iSeekPoint = 0; iSeekPoint < seekPointCount; ++iSeekPoint) {
4099
4874
  nextTargetPCMFrame += pcmFramesBetweenSeekPoints;
4100
4875
 
4101
- for (;;) {
4102
- if (nextTargetPCMFrame < runningPCMFrameCount) {
4103
- /* The next seek point is in the current MP3 frame. */
4104
- pSeekPoints[iSeekPoint].seekPosInBytes = mp3FrameInfo[0].bytePos;
4105
- pSeekPoints[iSeekPoint].pcmFrameIndex = nextTargetPCMFrame;
4106
- pSeekPoints[iSeekPoint].mp3FramesToDiscard = DRMP3_SEEK_LEADING_MP3_FRAMES;
4107
- pSeekPoints[iSeekPoint].pcmFramesToDiscard = (drmp3_uint16)(nextTargetPCMFrame - mp3FrameInfo[DRMP3_SEEK_LEADING_MP3_FRAMES-1].pcmFrameIndex);
4108
- break;
4109
- } else {
4876
+ /* Go to the MP3 frame holding the target. mp3FrameInfo holds it and up to DRMP3_SEEK_LEADING_MP3_FRAMES frames before it. */
4877
+ while (mp3FrameInfoCount == 0 || nextTargetPCMFrame >= runningPCMFrameCount) {
4878
+ drmp3_uint32 pcmFramesInCurrentMP3FrameIn;
4879
+
4880
+ if (mp3FrameInfoCount == DRMP3_COUNTOF(mp3FrameInfo)) {
4110
4881
  size_t i;
4111
- drmp3_uint32 pcmFramesInCurrentMP3FrameIn;
4112
4882
 
4113
- /*
4114
- The next seek point is not in the current MP3 frame, so continue on to the next one. The first thing to do is cycle the cached
4115
- MP3 frame info.
4116
- */
4117
4883
  for (i = 0; i < DRMP3_COUNTOF(mp3FrameInfo)-1; ++i) {
4118
4884
  mp3FrameInfo[i] = mp3FrameInfo[i+1];
4119
4885
  }
4886
+
4887
+ mp3FrameInfoCount -= 1;
4888
+ }
4120
4889
 
4121
- /* Cache previous MP3 frame info. */
4122
- mp3FrameInfo[DRMP3_COUNTOF(mp3FrameInfo)-1].bytePos = pMP3->streamCursor - pMP3->dataSize;
4123
- mp3FrameInfo[DRMP3_COUNTOF(mp3FrameInfo)-1].pcmFrameIndex = runningPCMFrameCount;
4124
-
4125
- /*
4126
- Go to the next MP3 frame. This shouldn't ever fail, but just in case it does we just set the seek point and break. If it happens, it
4127
- should only ever do it for the last seek point.
4128
- */
4129
- pcmFramesInCurrentMP3FrameIn = drmp3_decode_next_frame_ex(pMP3, NULL);
4130
- if (pcmFramesInCurrentMP3FrameIn == 0) {
4131
- pSeekPoints[iSeekPoint].seekPosInBytes = mp3FrameInfo[0].bytePos;
4132
- pSeekPoints[iSeekPoint].pcmFrameIndex = nextTargetPCMFrame;
4133
- pSeekPoints[iSeekPoint].mp3FramesToDiscard = DRMP3_SEEK_LEADING_MP3_FRAMES;
4134
- pSeekPoints[iSeekPoint].pcmFramesToDiscard = (drmp3_uint16)(nextTargetPCMFrame - mp3FrameInfo[DRMP3_SEEK_LEADING_MP3_FRAMES-1].pcmFrameIndex);
4135
- break;
4136
- }
4890
+ /* The byte position of the next frame will be the stream's cursor position, minus whatever is sitting in the buffer. */
4891
+ DRMP3_ASSERT(pMP3->streamCursor >= pMP3->dataSize);
4892
+ mp3FrameInfo[mp3FrameInfoCount].bytePos = pMP3->streamCursor - pMP3->dataSize;
4893
+ mp3FrameInfo[mp3FrameInfoCount].pcmFrameIndex = runningPCMFrameCount;
4894
+ mp3FrameInfo[mp3FrameInfoCount].mp3FrameIndex = runningMP3FrameCount;
4137
4895
 
4138
- drmp3__accumulate_running_pcm_frame_count(pMP3, pcmFramesInCurrentMP3FrameIn, &runningPCMFrameCount, &runningPCMFrameCountFractionalPart);
4896
+ /* This shouldn't ever fail, but just in case it does we set the seek point on the last frame. */
4897
+ pcmFramesInCurrentMP3FrameIn = drmp3_decode_next_frame_ex(pMP3, NULL, NULL, NULL);
4898
+ if (pcmFramesInCurrentMP3FrameIn == 0) {
4899
+ break;
4139
4900
  }
4901
+
4902
+ mp3FrameInfoCount += 1;
4903
+ runningMP3FrameCount += 1 + pMP3->mp3FramesSkipped;
4904
+ drmp3__accumulate_running_pcm_frame_count(pMP3, pcmFramesInCurrentMP3FrameIn, &runningPCMFrameCount, &runningPCMFrameCountFractionalPart);
4140
4905
  }
4906
+
4907
+ DRMP3_ASSERT(mp3FrameInfoCount > 0);
4908
+ pSeekPoints[iSeekPoint].seekPosInBytes = mp3FrameInfo[0].bytePos;
4909
+ pSeekPoints[iSeekPoint].pcmFrameIndex = nextTargetPCMFrame;
4910
+ pSeekPoints[iSeekPoint].mp3FramesToDiscard = (drmp3_uint16)(mp3FrameInfo[mp3FrameInfoCount-1].mp3FrameIndex - mp3FrameInfo[0].mp3FrameIndex);
4911
+ pSeekPoints[iSeekPoint].pcmFramesToDiscard = (drmp3_uint16)(nextTargetPCMFrame - mp3FrameInfo[mp3FrameInfoCount-1].pcmFrameIndex);
4141
4912
  }
4142
4913
 
4143
4914
  /* Finally, we need to seek back to where we were. */
@@ -4178,7 +4949,7 @@ static float* drmp3__full_read_and_close_f32(drmp3* pMP3, drmp3_config* pConfig,
4178
4949
  drmp3_uint64 totalFramesRead = 0;
4179
4950
  drmp3_uint64 framesCapacity = 0;
4180
4951
  float* pFrames = NULL;
4181
- float temp[4096];
4952
+ float temp[1152*2]; /* MP3 frames have a maximum per-channel sample count of 1152. Times 2 to account for stereo. */
4182
4953
 
4183
4954
  DRMP3_ASSERT(pMP3 != NULL);
4184
4955
 
@@ -4210,6 +4981,8 @@ static float* drmp3__full_read_and_close_f32(drmp3* pMP3, drmp3_config* pConfig,
4210
4981
  pNewFrames = (float*)drmp3__realloc_from_callbacks(pFrames, (size_t)newFramesBufferSize, (size_t)oldFramesBufferSize, &pMP3->allocationCallbacks);
4211
4982
  if (pNewFrames == NULL) {
4212
4983
  drmp3__free_from_callbacks(pFrames, &pMP3->allocationCallbacks);
4984
+ pFrames = NULL;
4985
+ totalFramesRead = 0;
4213
4986
  break;
4214
4987
  }
4215
4988
 
@@ -4245,7 +5018,7 @@ static drmp3_int16* drmp3__full_read_and_close_s16(drmp3* pMP3, drmp3_config* pC
4245
5018
  drmp3_uint64 totalFramesRead = 0;
4246
5019
  drmp3_uint64 framesCapacity = 0;
4247
5020
  drmp3_int16* pFrames = NULL;
4248
- drmp3_int16 temp[4096];
5021
+ drmp3_int16 temp[1152*2]; /* MP3 frames have a maximum per-channel sample count of 1152. Times 2 to account for stereo. */
4249
5022
 
4250
5023
  DRMP3_ASSERT(pMP3 != NULL);
4251
5024
 
@@ -4277,6 +5050,8 @@ static drmp3_int16* drmp3__full_read_and_close_s16(drmp3* pMP3, drmp3_config* pC
4277
5050
  pNewFrames = (drmp3_int16*)drmp3__realloc_from_callbacks(pFrames, (size_t)newFramesBufferSize, (size_t)oldFramesBufferSize, &pMP3->allocationCallbacks);
4278
5051
  if (pNewFrames == NULL) {
4279
5052
  drmp3__free_from_callbacks(pFrames, &pMP3->allocationCallbacks);
5053
+ pFrames = NULL;
5054
+ totalFramesRead = 0;
4280
5055
  break;
4281
5056
  }
4282
5057
 
@@ -4308,20 +5083,20 @@ static drmp3_int16* drmp3__full_read_and_close_s16(drmp3* pMP3, drmp3_config* pC
4308
5083
  }
4309
5084
 
4310
5085
 
4311
- DRMP3_API float* drmp3_open_and_read_pcm_frames_f32(drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks)
5086
+ DRMP3_API float* drmp3_open_and_read_pcm_frames_f32(drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks)
4312
5087
  {
4313
5088
  drmp3 mp3;
4314
- if (!drmp3_init(&mp3, onRead, onSeek, pUserData, pAllocationCallbacks)) {
5089
+ if (!drmp3_init(&mp3, onRead, onSeek, onTell, NULL, pUserData, pAllocationCallbacks)) {
4315
5090
  return NULL;
4316
5091
  }
4317
5092
 
4318
5093
  return drmp3__full_read_and_close_f32(&mp3, pConfig, pTotalFrameCount);
4319
5094
  }
4320
5095
 
4321
- DRMP3_API drmp3_int16* drmp3_open_and_read_pcm_frames_s16(drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks)
5096
+ DRMP3_API drmp3_int16* drmp3_open_and_read_pcm_frames_s16(drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks)
4322
5097
  {
4323
5098
  drmp3 mp3;
4324
- if (!drmp3_init(&mp3, onRead, onSeek, pUserData, pAllocationCallbacks)) {
5099
+ if (!drmp3_init(&mp3, onRead, onSeek, onTell, NULL, pUserData, pAllocationCallbacks)) {
4325
5100
  return NULL;
4326
5101
  }
4327
5102
 
@@ -4408,343 +5183,6 @@ DIFFERENCES BETWEEN minimp3 AND dr_mp3
4408
5183
  using minimp3 in conjunction with stb_vorbis. dr_mp3 addresses this.
4409
5184
  */
4410
5185
 
4411
- /*
4412
- RELEASE NOTES - v0.5.0
4413
- =======================
4414
- Version 0.5.0 has breaking API changes.
4415
-
4416
- Improved Client-Defined Memory Allocation
4417
- -----------------------------------------
4418
- The main change with this release is the addition of a more flexible way of implementing custom memory allocation routines. The
4419
- existing system of DRMP3_MALLOC, DRMP3_REALLOC and DRMP3_FREE are still in place and will be used by default when no custom
4420
- allocation callbacks are specified.
4421
-
4422
- To use the new system, you pass in a pointer to a drmp3_allocation_callbacks object to drmp3_init() and family, like this:
4423
-
4424
- void* my_malloc(size_t sz, void* pUserData)
4425
- {
4426
- return malloc(sz);
4427
- }
4428
- void* my_realloc(void* p, size_t sz, void* pUserData)
4429
- {
4430
- return realloc(p, sz);
4431
- }
4432
- void my_free(void* p, void* pUserData)
4433
- {
4434
- free(p);
4435
- }
4436
-
4437
- ...
4438
-
4439
- drmp3_allocation_callbacks allocationCallbacks;
4440
- allocationCallbacks.pUserData = &myData;
4441
- allocationCallbacks.onMalloc = my_malloc;
4442
- allocationCallbacks.onRealloc = my_realloc;
4443
- allocationCallbacks.onFree = my_free;
4444
- drmp3_init_file(&mp3, "my_file.mp3", NULL, &allocationCallbacks);
4445
-
4446
- The advantage of this new system is that it allows you to specify user data which will be passed in to the allocation routines.
4447
-
4448
- Passing in null for the allocation callbacks object will cause dr_mp3 to use defaults which is the same as DRMP3_MALLOC,
4449
- DRMP3_REALLOC and DRMP3_FREE and the equivalent of how it worked in previous versions.
4450
-
4451
- Every API that opens a drmp3 object now takes this extra parameter. These include the following:
4452
-
4453
- drmp3_init()
4454
- drmp3_init_file()
4455
- drmp3_init_memory()
4456
- drmp3_open_and_read_pcm_frames_f32()
4457
- drmp3_open_and_read_pcm_frames_s16()
4458
- drmp3_open_memory_and_read_pcm_frames_f32()
4459
- drmp3_open_memory_and_read_pcm_frames_s16()
4460
- drmp3_open_file_and_read_pcm_frames_f32()
4461
- drmp3_open_file_and_read_pcm_frames_s16()
4462
-
4463
- Renamed APIs
4464
- ------------
4465
- The following APIs have been renamed for consistency with other dr_* libraries and to make it clear that they return PCM frame
4466
- counts rather than sample counts.
4467
-
4468
- drmp3_open_and_read_f32() -> drmp3_open_and_read_pcm_frames_f32()
4469
- drmp3_open_and_read_s16() -> drmp3_open_and_read_pcm_frames_s16()
4470
- drmp3_open_memory_and_read_f32() -> drmp3_open_memory_and_read_pcm_frames_f32()
4471
- drmp3_open_memory_and_read_s16() -> drmp3_open_memory_and_read_pcm_frames_s16()
4472
- drmp3_open_file_and_read_f32() -> drmp3_open_file_and_read_pcm_frames_f32()
4473
- drmp3_open_file_and_read_s16() -> drmp3_open_file_and_read_pcm_frames_s16()
4474
- */
4475
-
4476
- /*
4477
- REVISION HISTORY
4478
- ================
4479
- v0.6.34 - 2022-09-17
4480
- - Fix compilation with DJGPP.
4481
- - Fix compilation when compiling with x86 with no SSE2.
4482
- - Remove an unnecessary variable from the drmp3 structure.
4483
-
4484
- v0.6.33 - 2022-04-10
4485
- - Fix compilation error with the MSVC ARM64 build.
4486
- - Fix compilation error on older versions of GCC.
4487
- - Remove some unused functions.
4488
-
4489
- v0.6.32 - 2021-12-11
4490
- - Fix a warning with Clang.
4491
-
4492
- v0.6.31 - 2021-08-22
4493
- - Fix a bug when loading from memory.
4494
-
4495
- v0.6.30 - 2021-08-16
4496
- - Silence some warnings.
4497
- - Replace memory operations with DRMP3_* macros.
4498
-
4499
- v0.6.29 - 2021-08-08
4500
- - Bring up to date with minimp3.
4501
-
4502
- v0.6.28 - 2021-07-31
4503
- - Fix platform detection for ARM64.
4504
- - Fix a compilation error with C89.
4505
-
4506
- v0.6.27 - 2021-02-21
4507
- - Fix a warning due to referencing _MSC_VER when it is undefined.
4508
-
4509
- v0.6.26 - 2021-01-31
4510
- - Bring up to date with minimp3.
4511
-
4512
- v0.6.25 - 2020-12-26
4513
- - Remove DRMP3_DEFAULT_CHANNELS and DRMP3_DEFAULT_SAMPLE_RATE which are leftovers from some removed APIs.
4514
-
4515
- v0.6.24 - 2020-12-07
4516
- - Fix a typo in version date for 0.6.23.
4517
-
4518
- v0.6.23 - 2020-12-03
4519
- - Fix an error where a file can be closed twice when initialization of the decoder fails.
4520
-
4521
- v0.6.22 - 2020-12-02
4522
- - Fix an error where it's possible for a file handle to be left open when initialization of the decoder fails.
4523
-
4524
- v0.6.21 - 2020-11-28
4525
- - Bring up to date with minimp3.
4526
-
4527
- v0.6.20 - 2020-11-21
4528
- - Fix compilation with OpenWatcom.
4529
-
4530
- v0.6.19 - 2020-11-13
4531
- - Minor code clean up.
4532
-
4533
- v0.6.18 - 2020-11-01
4534
- - Improve compiler support for older versions of GCC.
4535
-
4536
- v0.6.17 - 2020-09-28
4537
- - Bring up to date with minimp3.
4538
-
4539
- v0.6.16 - 2020-08-02
4540
- - Simplify sized types.
4541
-
4542
- v0.6.15 - 2020-07-25
4543
- - Fix a compilation warning.
4544
-
4545
- v0.6.14 - 2020-07-23
4546
- - Fix undefined behaviour with memmove().
4547
-
4548
- v0.6.13 - 2020-07-06
4549
- - Fix a bug when converting from s16 to f32 in drmp3_read_pcm_frames_f32().
4550
-
4551
- v0.6.12 - 2020-06-23
4552
- - Add include guard for the implementation section.
4553
-
4554
- v0.6.11 - 2020-05-26
4555
- - Fix use of uninitialized variable error.
4556
-
4557
- v0.6.10 - 2020-05-16
4558
- - Add compile-time and run-time version querying.
4559
- - DRMP3_VERSION_MINOR
4560
- - DRMP3_VERSION_MAJOR
4561
- - DRMP3_VERSION_REVISION
4562
- - DRMP3_VERSION_STRING
4563
- - drmp3_version()
4564
- - drmp3_version_string()
4565
-
4566
- v0.6.9 - 2020-04-30
4567
- - Change the `pcm` parameter of drmp3dec_decode_frame() to a `const drmp3_uint8*` for consistency with internal APIs.
4568
-
4569
- v0.6.8 - 2020-04-26
4570
- - Optimizations to decoding when initializing from memory.
4571
-
4572
- v0.6.7 - 2020-04-25
4573
- - Fix a compilation error with DR_MP3_NO_STDIO
4574
- - Optimization to decoding by reducing some data movement.
4575
-
4576
- v0.6.6 - 2020-04-23
4577
- - Fix a minor bug with the running PCM frame counter.
4578
-
4579
- v0.6.5 - 2020-04-19
4580
- - Fix compilation error on ARM builds.
4581
-
4582
- v0.6.4 - 2020-04-19
4583
- - Bring up to date with changes to minimp3.
4584
-
4585
- v0.6.3 - 2020-04-13
4586
- - Fix some pedantic warnings.
4587
-
4588
- v0.6.2 - 2020-04-10
4589
- - Fix a crash in drmp3_open_*_and_read_pcm_frames_*() if the output config object is NULL.
4590
-
4591
- v0.6.1 - 2020-04-05
4592
- - Fix warnings.
4593
-
4594
- v0.6.0 - 2020-04-04
4595
- - API CHANGE: Remove the pConfig parameter from the following APIs:
4596
- - drmp3_init()
4597
- - drmp3_init_memory()
4598
- - drmp3_init_file()
4599
- - Add drmp3_init_file_w() for opening a file from a wchar_t encoded path.
4600
-
4601
- v0.5.6 - 2020-02-12
4602
- - Bring up to date with minimp3.
4603
-
4604
- v0.5.5 - 2020-01-29
4605
- - Fix a memory allocation bug in high level s16 decoding APIs.
4606
-
4607
- v0.5.4 - 2019-12-02
4608
- - Fix a possible null pointer dereference when using custom memory allocators for realloc().
4609
-
4610
- v0.5.3 - 2019-11-14
4611
- - Fix typos in documentation.
4612
-
4613
- v0.5.2 - 2019-11-02
4614
- - Bring up to date with minimp3.
4615
-
4616
- v0.5.1 - 2019-10-08
4617
- - Fix a warning with GCC.
4618
-
4619
- v0.5.0 - 2019-10-07
4620
- - API CHANGE: Add support for user defined memory allocation routines. This system allows the program to specify their own memory allocation
4621
- routines with a user data pointer for client-specific contextual data. This adds an extra parameter to the end of the following APIs:
4622
- - drmp3_init()
4623
- - drmp3_init_file()
4624
- - drmp3_init_memory()
4625
- - drmp3_open_and_read_pcm_frames_f32()
4626
- - drmp3_open_and_read_pcm_frames_s16()
4627
- - drmp3_open_memory_and_read_pcm_frames_f32()
4628
- - drmp3_open_memory_and_read_pcm_frames_s16()
4629
- - drmp3_open_file_and_read_pcm_frames_f32()
4630
- - drmp3_open_file_and_read_pcm_frames_s16()
4631
- - API CHANGE: Renamed the following APIs:
4632
- - drmp3_open_and_read_f32() -> drmp3_open_and_read_pcm_frames_f32()
4633
- - drmp3_open_and_read_s16() -> drmp3_open_and_read_pcm_frames_s16()
4634
- - drmp3_open_memory_and_read_f32() -> drmp3_open_memory_and_read_pcm_frames_f32()
4635
- - drmp3_open_memory_and_read_s16() -> drmp3_open_memory_and_read_pcm_frames_s16()
4636
- - drmp3_open_file_and_read_f32() -> drmp3_open_file_and_read_pcm_frames_f32()
4637
- - drmp3_open_file_and_read_s16() -> drmp3_open_file_and_read_pcm_frames_s16()
4638
-
4639
- v0.4.7 - 2019-07-28
4640
- - Fix a compiler error.
4641
-
4642
- v0.4.6 - 2019-06-14
4643
- - Fix a compiler error.
4644
-
4645
- v0.4.5 - 2019-06-06
4646
- - Bring up to date with minimp3.
4647
-
4648
- v0.4.4 - 2019-05-06
4649
- - Fixes to the VC6 build.
4650
-
4651
- v0.4.3 - 2019-05-05
4652
- - Use the channel count and/or sample rate of the first MP3 frame instead of DRMP3_DEFAULT_CHANNELS and
4653
- DRMP3_DEFAULT_SAMPLE_RATE when they are set to 0. To use the old behaviour, just set the relevant property to
4654
- DRMP3_DEFAULT_CHANNELS or DRMP3_DEFAULT_SAMPLE_RATE.
4655
- - Add s16 reading APIs
4656
- - drmp3_read_pcm_frames_s16
4657
- - drmp3_open_memory_and_read_pcm_frames_s16
4658
- - drmp3_open_and_read_pcm_frames_s16
4659
- - drmp3_open_file_and_read_pcm_frames_s16
4660
- - Add drmp3_get_mp3_and_pcm_frame_count() to the public header section.
4661
- - Add support for C89.
4662
- - Change license to choice of public domain or MIT-0.
4663
-
4664
- v0.4.2 - 2019-02-21
4665
- - Fix a warning.
4666
-
4667
- v0.4.1 - 2018-12-30
4668
- - Fix a warning.
4669
-
4670
- v0.4.0 - 2018-12-16
4671
- - API CHANGE: Rename some APIs:
4672
- - drmp3_read_f32 -> to drmp3_read_pcm_frames_f32
4673
- - drmp3_seek_to_frame -> drmp3_seek_to_pcm_frame
4674
- - drmp3_open_and_decode_f32 -> drmp3_open_and_read_pcm_frames_f32
4675
- - drmp3_open_and_decode_memory_f32 -> drmp3_open_memory_and_read_pcm_frames_f32
4676
- - drmp3_open_and_decode_file_f32 -> drmp3_open_file_and_read_pcm_frames_f32
4677
- - Add drmp3_get_pcm_frame_count().
4678
- - Add drmp3_get_mp3_frame_count().
4679
- - Improve seeking performance.
4680
-
4681
- v0.3.2 - 2018-09-11
4682
- - Fix a couple of memory leaks.
4683
- - Bring up to date with minimp3.
4684
-
4685
- v0.3.1 - 2018-08-25
4686
- - Fix C++ build.
4687
-
4688
- v0.3.0 - 2018-08-25
4689
- - Bring up to date with minimp3. This has a minor API change: the "pcm" parameter of drmp3dec_decode_frame() has
4690
- been changed from short* to void* because it can now output both s16 and f32 samples, depending on whether or
4691
- not the DR_MP3_FLOAT_OUTPUT option is set.
4692
-
4693
- v0.2.11 - 2018-08-08
4694
- - Fix a bug where the last part of a file is not read.
4695
-
4696
- v0.2.10 - 2018-08-07
4697
- - Improve 64-bit detection.
4698
-
4699
- v0.2.9 - 2018-08-05
4700
- - Fix C++ build on older versions of GCC.
4701
- - Bring up to date with minimp3.
4702
-
4703
- v0.2.8 - 2018-08-02
4704
- - Fix compilation errors with older versions of GCC.
4705
-
4706
- v0.2.7 - 2018-07-13
4707
- - Bring up to date with minimp3.
4708
-
4709
- v0.2.6 - 2018-07-12
4710
- - Bring up to date with minimp3.
4711
-
4712
- v0.2.5 - 2018-06-22
4713
- - Bring up to date with minimp3.
4714
-
4715
- v0.2.4 - 2018-05-12
4716
- - Bring up to date with minimp3.
4717
-
4718
- v0.2.3 - 2018-04-29
4719
- - Fix TCC build.
4720
-
4721
- v0.2.2 - 2018-04-28
4722
- - Fix bug when opening a decoder from memory.
4723
-
4724
- v0.2.1 - 2018-04-27
4725
- - Efficiency improvements when the decoder reaches the end of the stream.
4726
-
4727
- v0.2 - 2018-04-21
4728
- - Bring up to date with minimp3.
4729
- - Start using major.minor.revision versioning.
4730
-
4731
- v0.1d - 2018-03-30
4732
- - Bring up to date with minimp3.
4733
-
4734
- v0.1c - 2018-03-11
4735
- - Fix C++ build error.
4736
-
4737
- v0.1b - 2018-03-07
4738
- - Bring up to date with minimp3.
4739
-
4740
- v0.1a - 2018-02-28
4741
- - Fix compilation error on GCC/Clang.
4742
- - Fix some warnings.
4743
-
4744
- v0.1 - 2018-02-xx
4745
- - Initial versioned release.
4746
- */
4747
-
4748
5186
  /*
4749
5187
  This software is available as a choice of the following licenses. Choose
4750
5188
  whichever you prefer.
@@ -4777,7 +5215,7 @@ For more information, please refer to <http://unlicense.org/>
4777
5215
  ===============================================================================
4778
5216
  ALTERNATIVE 2 - MIT No Attribution
4779
5217
  ===============================================================================
4780
- Copyright 2020 David Reid
5218
+ Copyright 2023 David Reid
4781
5219
 
4782
5220
  Permission is hereby granted, free of charge, to any person obtaining a copy of
4783
5221
  this software and associated documentation files (the "Software"), to deal in