GCC Code Coverage Report


Directory: ./
File: submodules/raylib/src/external/dr_mp3.h
Date: 2023-09-29 04:53:15
Exec Total Coverage
Lines: 0 1079 0.0%
Branches: 0 798 0.0%

Line Branch Exec Source
1 /*
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
4
5 David Reid - mackron@gmail.com
6
7 GitHub: https://github.com/mackron/dr_libs
8
9 Based on minimp3 (https://github.com/lieff/minimp3) which is where the real work was done. See the bottom of this file for differences between minimp3 and dr_mp3.
10 */
11
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
37 =============
38 dr_mp3 is a single file library. To use it, do something like the following in one .c file.
39
40 ```c
41 #define DR_MP3_IMPLEMENTATION
42 #include "dr_mp3.h"
43 ```
44
45 You can then #include this file in other parts of the program as you would with any other header file. To decode audio data, do something like the following:
46
47 ```c
48 drmp3 mp3;
49 if (!drmp3_init_file(&mp3, "MySong.mp3", NULL)) {
50 // Failed to open file
51 }
52
53 ...
54
55 drmp3_uint64 framesRead = drmp3_read_pcm_frames_f32(pMP3, framesToRead, pFrames);
56 ```
57
58 The drmp3 object is transparent so you can get access to the channel count and sample rate like so:
59
60 ```
61 drmp3_uint32 channels = mp3.channels;
62 drmp3_uint32 sampleRate = mp3.sampleRate;
63 ```
64
65 The example above initializes a decoder from a file, but you can also initialize it from a block of memory and read and seek callbacks with
66 `drmp3_init_memory()` and `drmp3_init()` respectively.
67
68 You do not need to do any annoying memory management when reading PCM frames - this is all managed internally. You can request any number of PCM frames in each
69 call to `drmp3_read_pcm_frames_f32()` and it will return as many PCM frames as it can, up to the requested amount.
70
71 You can also decode an entire file in one go with `drmp3_open_and_read_pcm_frames_f32()`, `drmp3_open_memory_and_read_pcm_frames_f32()` and
72 `drmp3_open_file_and_read_pcm_frames_f32()`.
73
74
75 Build Options
76 =============
77 #define these options before including this file.
78
79 #define DR_MP3_NO_STDIO
80 Disable drmp3_init_file(), etc.
81
82 #define DR_MP3_NO_SIMD
83 Disable SIMD optimizations.
84 */
85
86 #ifndef dr_mp3_h
87 #define dr_mp3_h
88
89 #ifdef __cplusplus
90 extern "C" {
91 #endif
92
93 #define DRMP3_STRINGIFY(x) #x
94 #define DRMP3_XSTRINGIFY(x) DRMP3_STRINGIFY(x)
95
96 #define DRMP3_VERSION_MAJOR 0
97 #define DRMP3_VERSION_MINOR 6
98 #define DRMP3_VERSION_REVISION 34
99 #define DRMP3_VERSION_STRING DRMP3_XSTRINGIFY(DRMP3_VERSION_MAJOR) "." DRMP3_XSTRINGIFY(DRMP3_VERSION_MINOR) "." DRMP3_XSTRINGIFY(DRMP3_VERSION_REVISION)
100
101 #include <stddef.h> /* For size_t. */
102
103 /* Sized types. */
104 typedef signed char drmp3_int8;
105 typedef unsigned char drmp3_uint8;
106 typedef signed short drmp3_int16;
107 typedef unsigned short drmp3_uint16;
108 typedef signed int drmp3_int32;
109 typedef unsigned int drmp3_uint32;
110 #if defined(_MSC_VER) && !defined(__clang__)
111 typedef signed __int64 drmp3_int64;
112 typedef unsigned __int64 drmp3_uint64;
113 #else
114 #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
115 #pragma GCC diagnostic push
116 #pragma GCC diagnostic ignored "-Wlong-long"
117 #if defined(__clang__)
118 #pragma GCC diagnostic ignored "-Wc++11-long-long"
119 #endif
120 #endif
121 typedef signed long long drmp3_int64;
122 typedef unsigned long long drmp3_uint64;
123 #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
124 #pragma GCC diagnostic pop
125 #endif
126 #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__)
128 typedef drmp3_uint64 drmp3_uintptr;
129 #else
130 typedef drmp3_uint32 drmp3_uintptr;
131 #endif
132 typedef drmp3_uint8 drmp3_bool8;
133 typedef drmp3_uint32 drmp3_bool32;
134 #define DRMP3_TRUE 1
135 #define DRMP3_FALSE 0
136
137 #if !defined(DRMP3_API)
138 #if defined(DRMP3_DLL)
139 #if defined(_WIN32)
140 #define DRMP3_DLL_IMPORT __declspec(dllimport)
141 #define DRMP3_DLL_EXPORT __declspec(dllexport)
142 #define DRMP3_DLL_PRIVATE static
143 #else
144 #if defined(__GNUC__) && __GNUC__ >= 4
145 #define DRMP3_DLL_IMPORT __attribute__((visibility("default")))
146 #define DRMP3_DLL_EXPORT __attribute__((visibility("default")))
147 #define DRMP3_DLL_PRIVATE __attribute__((visibility("hidden")))
148 #else
149 #define DRMP3_DLL_IMPORT
150 #define DRMP3_DLL_EXPORT
151 #define DRMP3_DLL_PRIVATE static
152 #endif
153 #endif
154
155 #if defined(DR_MP3_IMPLEMENTATION) || defined(DRMP3_IMPLEMENTATION)
156 #define DRMP3_API DRMP3_DLL_EXPORT
157 #else
158 #define DRMP3_API DRMP3_DLL_IMPORT
159 #endif
160 #define DRMP3_PRIVATE DRMP3_DLL_PRIVATE
161 #else
162 #define DRMP3_API extern
163 #define DRMP3_PRIVATE static
164 #endif
165 #endif
166
167 typedef drmp3_int32 drmp3_result;
168 #define DRMP3_SUCCESS 0
169 #define DRMP3_ERROR -1 /* A generic error. */
170 #define DRMP3_INVALID_ARGS -2
171 #define DRMP3_INVALID_OPERATION -3
172 #define DRMP3_OUT_OF_MEMORY -4
173 #define DRMP3_OUT_OF_RANGE -5
174 #define DRMP3_ACCESS_DENIED -6
175 #define DRMP3_DOES_NOT_EXIST -7
176 #define DRMP3_ALREADY_EXISTS -8
177 #define DRMP3_TOO_MANY_OPEN_FILES -9
178 #define DRMP3_INVALID_FILE -10
179 #define DRMP3_TOO_BIG -11
180 #define DRMP3_PATH_TOO_LONG -12
181 #define DRMP3_NAME_TOO_LONG -13
182 #define DRMP3_NOT_DIRECTORY -14
183 #define DRMP3_IS_DIRECTORY -15
184 #define DRMP3_DIRECTORY_NOT_EMPTY -16
185 #define DRMP3_END_OF_FILE -17
186 #define DRMP3_NO_SPACE -18
187 #define DRMP3_BUSY -19
188 #define DRMP3_IO_ERROR -20
189 #define DRMP3_INTERRUPT -21
190 #define DRMP3_UNAVAILABLE -22
191 #define DRMP3_ALREADY_IN_USE -23
192 #define DRMP3_BAD_ADDRESS -24
193 #define DRMP3_BAD_SEEK -25
194 #define DRMP3_BAD_PIPE -26
195 #define DRMP3_DEADLOCK -27
196 #define DRMP3_TOO_MANY_LINKS -28
197 #define DRMP3_NOT_IMPLEMENTED -29
198 #define DRMP3_NO_MESSAGE -30
199 #define DRMP3_BAD_MESSAGE -31
200 #define DRMP3_NO_DATA_AVAILABLE -32
201 #define DRMP3_INVALID_DATA -33
202 #define DRMP3_TIMEOUT -34
203 #define DRMP3_NO_NETWORK -35
204 #define DRMP3_NOT_UNIQUE -36
205 #define DRMP3_NOT_SOCKET -37
206 #define DRMP3_NO_ADDRESS -38
207 #define DRMP3_BAD_PROTOCOL -39
208 #define DRMP3_PROTOCOL_UNAVAILABLE -40
209 #define DRMP3_PROTOCOL_NOT_SUPPORTED -41
210 #define DRMP3_PROTOCOL_FAMILY_NOT_SUPPORTED -42
211 #define DRMP3_ADDRESS_FAMILY_NOT_SUPPORTED -43
212 #define DRMP3_SOCKET_NOT_SUPPORTED -44
213 #define DRMP3_CONNECTION_RESET -45
214 #define DRMP3_ALREADY_CONNECTED -46
215 #define DRMP3_NOT_CONNECTED -47
216 #define DRMP3_CONNECTION_REFUSED -48
217 #define DRMP3_NO_HOST -49
218 #define DRMP3_IN_PROGRESS -50
219 #define DRMP3_CANCELLED -51
220 #define DRMP3_MEMORY_ALREADY_MAPPED -52
221 #define DRMP3_AT_END -53
222
223
224 #define DRMP3_MAX_PCM_FRAMES_PER_MP3_FRAME 1152
225 #define DRMP3_MAX_SAMPLES_PER_FRAME (DRMP3_MAX_PCM_FRAMES_PER_MP3_FRAME*2)
226
227 #ifdef _MSC_VER
228 #define DRMP3_INLINE __forceinline
229 #elif defined(__GNUC__)
230 /*
231 I've had a bug report where GCC is emitting warnings about functions possibly not being inlineable. This warning happens when
232 the __attribute__((always_inline)) attribute is defined without an "inline" statement. I think therefore there must be some
233 case where "__inline__" is not always defined, thus the compiler emitting these warnings. When using -std=c89 or -ansi on the
234 command line, we cannot use the "inline" keyword and instead need to use "__inline__". In an attempt to work around this issue
235 I am using "__inline__" only when we're compiling in strict ANSI mode.
236 */
237 #if defined(__STRICT_ANSI__)
238 #define DRMP3_GNUC_INLINE_HINT __inline__
239 #else
240 #define DRMP3_GNUC_INLINE_HINT inline
241 #endif
242
243 #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2)) || defined(__clang__)
244 #define DRMP3_INLINE DRMP3_GNUC_INLINE_HINT __attribute__((always_inline))
245 #else
246 #define DRMP3_INLINE DRMP3_GNUC_INLINE_HINT
247 #endif
248 #elif defined(__WATCOMC__)
249 #define DRMP3_INLINE __inline
250 #else
251 #define DRMP3_INLINE
252 #endif
253
254
255 DRMP3_API void drmp3_version(drmp3_uint32* pMajor, drmp3_uint32* pMinor, drmp3_uint32* pRevision);
256 DRMP3_API const char* drmp3_version_string(void);
257
258
259 /*
260 Low Level Push API
261 ==================
262 */
263 typedef struct
264 {
265 int frame_bytes, channels, hz, layer, bitrate_kbps;
266 } drmp3dec_frame_info;
267
268 typedef struct
269 {
270 float mdct_overlap[2][9*32], qmf_state[15*2*32];
271 int reserv, free_format_bytes;
272 drmp3_uint8 header[4], reserv_buf[511];
273 } drmp3dec;
274
275 /* Initializes a low level decoder. */
276 DRMP3_API void drmp3dec_init(drmp3dec *dec);
277
278 /* Reads a frame from a low level decoder. */
279 DRMP3_API int drmp3dec_decode_frame(drmp3dec *dec, const drmp3_uint8 *mp3, int mp3_bytes, void *pcm, drmp3dec_frame_info *info);
280
281 /* Helper for converting between f32 and s16. */
282 DRMP3_API void drmp3dec_f32_to_s16(const float *in, drmp3_int16 *out, size_t num_samples);
283
284
285
286 /*
287 Main API (Pull API)
288 ===================
289 */
290 typedef enum
291 {
292 drmp3_seek_origin_start,
293 drmp3_seek_origin_current
294 } drmp3_seek_origin;
295
296 typedef struct
297 {
298 drmp3_uint64 seekPosInBytes; /* Points to the first byte of an MP3 frame. */
299 drmp3_uint64 pcmFrameIndex; /* The index of the PCM frame this seek point targets. */
300 drmp3_uint16 mp3FramesToDiscard; /* The number of whole MP3 frames to be discarded before pcmFramesToDiscard. */
301 drmp3_uint16 pcmFramesToDiscard; /* The number of leading samples to read and discard. These are discarded after mp3FramesToDiscard. */
302 } drmp3_seek_point;
303
304 /*
305 Callback for when data is read. Return value is the number of bytes actually read.
306
307 pUserData [in] The user data that was passed to drmp3_init(), drmp3_open() and family.
308 pBufferOut [out] The output buffer.
309 bytesToRead [in] The number of bytes to read.
310
311 Returns the number of bytes actually read.
312
313 A return value of less than bytesToRead indicates the end of the stream. Do _not_ return from this callback until
314 either the entire bytesToRead is filled or you have reached the end of the stream.
315 */
316 typedef size_t (* drmp3_read_proc)(void* pUserData, void* pBufferOut, size_t bytesToRead);
317
318 /*
319 Callback for when data needs to be seeked.
320
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.
324
325 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 */
330 typedef drmp3_bool32 (* drmp3_seek_proc)(void* pUserData, int offset, drmp3_seek_origin origin);
331
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;
339
340 typedef struct
341 {
342 drmp3_uint32 channels;
343 drmp3_uint32 sampleRate;
344 } drmp3_config;
345
346 typedef struct
347 {
348 drmp3dec decoder;
349 drmp3_uint32 channels;
350 drmp3_uint32 sampleRate;
351 drmp3_read_proc onRead;
352 drmp3_seek_proc onSeek;
353 void* pUserData;
354 drmp3_allocation_callbacks allocationCallbacks;
355 drmp3_uint32 mp3FrameChannels; /* The number of channels in the currently loaded MP3 frame. Internal use only. */
356 drmp3_uint32 mp3FrameSampleRate; /* The sample rate of the currently loaded MP3 frame. Internal use only. */
357 drmp3_uint32 pcmFramesConsumedInMP3Frame;
358 drmp3_uint32 pcmFramesRemainingInMP3Frame;
359 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. */
361 drmp3_uint64 streamCursor; /* The current byte the decoder is sitting on in the raw stream. */
362 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 drmp3_uint32 seekPointCount; /* The number of items in pSeekPoints. When set to 0 assumes to no seek table. Defaults to zero. */
364 size_t dataSize;
365 size_t dataCapacity;
366 size_t dataConsumed;
367 drmp3_uint8* pData;
368 drmp3_bool32 atEnd : 1;
369 struct
370 {
371 const drmp3_uint8* pData;
372 size_t dataSize;
373 size_t currentReadPos;
374 } memory; /* Only used for decoders that were opened against a block of memory. */
375 } drmp3;
376
377 /*
378 Initializes an MP3 decoder.
379
380 onRead [in] The function to call when data needs to be read from the client.
381 onSeek [in] The function to call when the read position of the client data needs to move.
382 pUserData [in, optional] A pointer to application defined data that will be passed to onRead and onSeek.
383
384 Returns true if successful; false otherwise.
385
386 Close the loader with drmp3_uninit().
387
388 See also: drmp3_init_file(), drmp3_init_memory(), drmp3_uninit()
389 */
390 DRMP3_API drmp3_bool32 drmp3_init(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, const drmp3_allocation_callbacks* pAllocationCallbacks);
391
392 /*
393 Initializes an MP3 decoder from a block of memory.
394
395 This does not create a copy of the data. It is up to the application to ensure the buffer remains valid for
396 the lifetime of the drmp3 object.
397
398 The buffer should contain the contents of the entire MP3 file.
399 */
400 DRMP3_API drmp3_bool32 drmp3_init_memory(drmp3* pMP3, const void* pData, size_t dataSize, const drmp3_allocation_callbacks* pAllocationCallbacks);
401
402 #ifndef DR_MP3_NO_STDIO
403 /*
404 Initializes an MP3 decoder from a file.
405
406 This holds the internal FILE object until drmp3_uninit() is called. Keep this in mind if you're caching drmp3
407 objects because the operating system may restrict the number of file handles an application can have open at
408 any given time.
409 */
410 DRMP3_API drmp3_bool32 drmp3_init_file(drmp3* pMP3, const char* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks);
411 DRMP3_API drmp3_bool32 drmp3_init_file_w(drmp3* pMP3, const wchar_t* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks);
412 #endif
413
414 /*
415 Uninitializes an MP3 decoder.
416 */
417 DRMP3_API void drmp3_uninit(drmp3* pMP3);
418
419 /*
420 Reads PCM frames as interleaved 32-bit IEEE floating point PCM.
421
422 Note that framesToRead specifies the number of PCM frames to read, _not_ the number of MP3 frames.
423 */
424 DRMP3_API drmp3_uint64 drmp3_read_pcm_frames_f32(drmp3* pMP3, drmp3_uint64 framesToRead, float* pBufferOut);
425
426 /*
427 Reads PCM frames as interleaved signed 16-bit integer PCM.
428
429 Note that framesToRead specifies the number of PCM frames to read, _not_ the number of MP3 frames.
430 */
431 DRMP3_API drmp3_uint64 drmp3_read_pcm_frames_s16(drmp3* pMP3, drmp3_uint64 framesToRead, drmp3_int16* pBufferOut);
432
433 /*
434 Seeks to a specific frame.
435
436 Note that this is _not_ an MP3 frame, but rather a PCM frame.
437 */
438 DRMP3_API drmp3_bool32 drmp3_seek_to_pcm_frame(drmp3* pMP3, drmp3_uint64 frameIndex);
439
440 /*
441 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.
443 */
444 DRMP3_API drmp3_uint64 drmp3_get_pcm_frame_count(drmp3* pMP3);
445
446 /*
447 Calculates the total number of MP3 frames in the MP3 stream. Cannot be used for infinite streams such as internet
448 radio. Runs in linear time. Returns 0 on error.
449 */
450 DRMP3_API drmp3_uint64 drmp3_get_mp3_frame_count(drmp3* pMP3);
451
452 /*
453 Calculates the total number of MP3 and PCM frames in the MP3 stream. Cannot be used for infinite streams such as internet
454 radio. Runs in linear time. Returns 0 on error.
455
456 This is equivalent to calling drmp3_get_mp3_frame_count() and drmp3_get_pcm_frame_count() except that it's more efficient.
457 */
458 DRMP3_API drmp3_bool32 drmp3_get_mp3_and_pcm_frame_count(drmp3* pMP3, drmp3_uint64* pMP3FrameCount, drmp3_uint64* pPCMFrameCount);
459
460 /*
461 Calculates the seekpoints based on PCM frames. This is slow.
462
463 pSeekpoint count is a pointer to a uint32 containing the seekpoint count. On input it contains the desired count.
464 On output it contains the actual count. The reason for this design is that the client may request too many
465 seekpoints, in which case dr_mp3 will return a corrected count.
466
467 Note that seektable seeking is not quite sample exact when the MP3 stream contains inconsistent sample rates.
468 */
469 DRMP3_API drmp3_bool32 drmp3_calculate_seek_points(drmp3* pMP3, drmp3_uint32* pSeekPointCount, drmp3_seek_point* pSeekPoints);
470
471 /*
472 Binds a seek table to the decoder.
473
474 This does _not_ make a copy of pSeekPoints - it only references it. It is up to the application to ensure this
475 remains valid while it is bound to the decoder.
476
477 Use drmp3_calculate_seek_points() to calculate the seek points.
478 */
479 DRMP3_API drmp3_bool32 drmp3_bind_seek_table(drmp3* pMP3, drmp3_uint32 seekPointCount, drmp3_seek_point* pSeekPoints);
480
481
482 /*
483 Opens an decodes an entire MP3 stream as a single operation.
484
485 On output pConfig will receive the channel count and sample rate of the stream.
486
487 Free the returned pointer with drmp3_free().
488 */
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);
491
492 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 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);
494
495 #ifndef DR_MP3_NO_STDIO
496 DRMP3_API float* drmp3_open_file_and_read_pcm_frames_f32(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks);
497 DRMP3_API drmp3_int16* drmp3_open_file_and_read_pcm_frames_s16(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks);
498 #endif
499
500 /*
501 Allocates a block of memory on the heap.
502 */
503 DRMP3_API void* drmp3_malloc(size_t sz, const drmp3_allocation_callbacks* pAllocationCallbacks);
504
505 /*
506 Frees any memory that was allocated by a public drmp3 API.
507 */
508 DRMP3_API void drmp3_free(void* p, const drmp3_allocation_callbacks* pAllocationCallbacks);
509
510 #ifdef __cplusplus
511 }
512 #endif
513 #endif /* dr_mp3_h */
514
515
516 /************************************************************************************************************************************************************
517 ************************************************************************************************************************************************************
518
519 IMPLEMENTATION
520
521 ************************************************************************************************************************************************************
522 ************************************************************************************************************************************************************/
523 #if defined(DR_MP3_IMPLEMENTATION) || defined(DRMP3_IMPLEMENTATION)
524 #ifndef dr_mp3_c
525 #define dr_mp3_c
526
527 #include <stdlib.h>
528 #include <string.h>
529 #include <limits.h> /* For INT_MAX */
530
531 DRMP3_API void drmp3_version(drmp3_uint32* pMajor, drmp3_uint32* pMinor, drmp3_uint32* pRevision)
532 {
533 if (pMajor) {
534 *pMajor = DRMP3_VERSION_MAJOR;
535 }
536
537 if (pMinor) {
538 *pMinor = DRMP3_VERSION_MINOR;
539 }
540
541 if (pRevision) {
542 *pRevision = DRMP3_VERSION_REVISION;
543 }
544 }
545
546 DRMP3_API const char* drmp3_version_string(void)
547 {
548 return DRMP3_VERSION_STRING;
549 }
550
551 /* Disable SIMD when compiling with TCC for now. */
552 #if defined(__TINYC__)
553 #define DR_MP3_NO_SIMD
554 #endif
555
556 #define DRMP3_OFFSET_PTR(p, offset) ((void*)((drmp3_uint8*)(p) + (offset)))
557
558 #define DRMP3_MAX_FREE_FORMAT_FRAME_SIZE 2304 /* more than ISO spec's */
559 #ifndef DRMP3_MAX_FRAME_SYNC_MATCHES
560 #define DRMP3_MAX_FRAME_SYNC_MATCHES 10
561 #endif
562
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 #define DRMP3_SHORT_BLOCK_TYPE 2
567 #define DRMP3_STOP_BLOCK_TYPE 3
568 #define DRMP3_MODE_MONO 3
569 #define DRMP3_MODE_JOINT_STEREO 1
570 #define DRMP3_HDR_SIZE 4
571 #define DRMP3_HDR_IS_MONO(h) (((h[3]) & 0xC0) == 0xC0)
572 #define DRMP3_HDR_IS_MS_STEREO(h) (((h[3]) & 0xE0) == 0x60)
573 #define DRMP3_HDR_IS_FREE_FORMAT(h) (((h[2]) & 0xF0) == 0)
574 #define DRMP3_HDR_IS_CRC(h) (!((h[1]) & 1))
575 #define DRMP3_HDR_TEST_PADDING(h) ((h[2]) & 0x2)
576 #define DRMP3_HDR_TEST_MPEG1(h) ((h[1]) & 0x8)
577 #define DRMP3_HDR_TEST_NOT_MPEG25(h) ((h[1]) & 0x10)
578 #define DRMP3_HDR_TEST_I_STEREO(h) ((h[3]) & 0x10)
579 #define DRMP3_HDR_TEST_MS_STEREO(h) ((h[3]) & 0x20)
580 #define DRMP3_HDR_GET_STEREO_MODE(h) (((h[3]) >> 6) & 3)
581 #define DRMP3_HDR_GET_STEREO_MODE_EXT(h) (((h[3]) >> 4) & 3)
582 #define DRMP3_HDR_GET_LAYER(h) (((h[1]) >> 1) & 3)
583 #define DRMP3_HDR_GET_BITRATE(h) ((h[2]) >> 4)
584 #define DRMP3_HDR_GET_SAMPLE_RATE(h) (((h[2]) >> 2) & 3)
585 #define DRMP3_HDR_GET_MY_SAMPLE_RATE(h) (DRMP3_HDR_GET_SAMPLE_RATE(h) + (((h[1] >> 3) & 1) + ((h[1] >> 4) & 1))*3)
586 #define DRMP3_HDR_IS_FRAME_576(h) ((h[1] & 14) == 2)
587 #define DRMP3_HDR_IS_LAYER_1(h) ((h[1] & 6) == 6)
588
589 #define DRMP3_BITS_DEQUANTIZER_OUT -1
590 #define DRMP3_MAX_SCF (255 + DRMP3_BITS_DEQUANTIZER_OUT*4 - 210)
591 #define DRMP3_MAX_SCFI ((DRMP3_MAX_SCF + 3) & ~3)
592
593 #define DRMP3_MIN(a, b) ((a) > (b) ? (b) : (a))
594 #define DRMP3_MAX(a, b) ((a) < (b) ? (b) : (a))
595
596 #if !defined(DR_MP3_NO_SIMD)
597
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 */
600 #define DR_MP3_ONLY_SIMD
601 #endif
602
603 #if ((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__)))
604 #if defined(_MSC_VER)
605 #include <intrin.h>
606 #endif
607 #include <emmintrin.h>
608 #define DRMP3_HAVE_SSE 1
609 #define DRMP3_HAVE_SIMD 1
610 #define DRMP3_VSTORE _mm_storeu_ps
611 #define DRMP3_VLD _mm_loadu_ps
612 #define DRMP3_VSET _mm_set1_ps
613 #define DRMP3_VADD _mm_add_ps
614 #define DRMP3_VSUB _mm_sub_ps
615 #define DRMP3_VMUL _mm_mul_ps
616 #define DRMP3_VMAC(a, x, y) _mm_add_ps(a, _mm_mul_ps(x, y))
617 #define DRMP3_VMSB(a, x, y) _mm_sub_ps(a, _mm_mul_ps(x, y))
618 #define DRMP3_VMUL_S(x, s) _mm_mul_ps(x, _mm_set1_ps(s))
619 #define DRMP3_VREV(x) _mm_shuffle_ps(x, x, _MM_SHUFFLE(0, 1, 2, 3))
620 typedef __m128 drmp3_f4;
621 #if defined(_MSC_VER) || defined(DR_MP3_ONLY_SIMD)
622 #define drmp3_cpuid __cpuid
623 #else
624 static __inline__ __attribute__((always_inline)) void drmp3_cpuid(int CPUInfo[], const int InfoType)
625 {
626 #if defined(__PIC__)
627 __asm__ __volatile__(
628 #if defined(__x86_64__)
629 "push %%rbx\n"
630 "cpuid\n"
631 "xchgl %%ebx, %1\n"
632 "pop %%rbx\n"
633 #else
634 "xchgl %%ebx, %1\n"
635 "cpuid\n"
636 "xchgl %%ebx, %1\n"
637 #endif
638 : "=a" (CPUInfo[0]), "=r" (CPUInfo[1]), "=c" (CPUInfo[2]), "=d" (CPUInfo[3])
639 : "a" (InfoType));
640 #else
641 __asm__ __volatile__(
642 "cpuid"
643 : "=a" (CPUInfo[0]), "=b" (CPUInfo[1]), "=c" (CPUInfo[2]), "=d" (CPUInfo[3])
644 : "a" (InfoType));
645 #endif
646 }
647 #endif
648 static int drmp3_have_simd(void)
649 {
650 #ifdef DR_MP3_ONLY_SIMD
651 return 1;
652 #else
653 static int g_have_simd;
654 int CPUInfo[4];
655 #ifdef MINIMP3_TEST
656 static int g_counter;
657 if (g_counter++ > 100)
658 return 0;
659 #endif
660 if (g_have_simd)
661 goto end;
662 drmp3_cpuid(CPUInfo, 0);
663 if (CPUInfo[0] > 0)
664 {
665 drmp3_cpuid(CPUInfo, 1);
666 g_have_simd = (CPUInfo[3] & (1 << 26)) + 1; /* SSE2 */
667 return g_have_simd - 1;
668 }
669
670 end:
671 return g_have_simd - 1;
672 #endif
673 }
674 #elif defined(__ARM_NEON) || defined(__aarch64__) || defined(_M_ARM64)
675 #include <arm_neon.h>
676 #define DRMP3_HAVE_SSE 0
677 #define DRMP3_HAVE_SIMD 1
678 #define DRMP3_VSTORE vst1q_f32
679 #define DRMP3_VLD vld1q_f32
680 #define DRMP3_VSET vmovq_n_f32
681 #define DRMP3_VADD vaddq_f32
682 #define DRMP3_VSUB vsubq_f32
683 #define DRMP3_VMUL vmulq_f32
684 #define DRMP3_VMAC(a, x, y) vmlaq_f32(a, x, y)
685 #define DRMP3_VMSB(a, x, y) vmlsq_f32(a, x, y)
686 #define DRMP3_VMUL_S(x, s) vmulq_f32(x, vmovq_n_f32(s))
687 #define DRMP3_VREV(x) vcombine_f32(vget_high_f32(vrev64q_f32(x)), vget_low_f32(vrev64q_f32(x)))
688 typedef float32x4_t drmp3_f4;
689 static int drmp3_have_simd(void)
690 { /* TODO: detect neon for !DR_MP3_ONLY_SIMD */
691 return 1;
692 }
693 #else
694 #define DRMP3_HAVE_SSE 0
695 #define DRMP3_HAVE_SIMD 0
696 #ifdef DR_MP3_ONLY_SIMD
697 #error DR_MP3_ONLY_SIMD used, but SSE/NEON not enabled
698 #endif
699 #endif
700
701 #else
702
703 #define DRMP3_HAVE_SIMD 0
704
705 #endif
706
707 #if defined(__ARM_ARCH) && (__ARM_ARCH >= 6) && !defined(__aarch64__) && !defined(_M_ARM64)
708 #define DRMP3_HAVE_ARMV6 1
709 static __inline__ __attribute__((always_inline)) drmp3_int32 drmp3_clip_int16_arm(drmp3_int32 a)
710 {
711 drmp3_int32 x = 0;
712 __asm__ ("ssat %0, #16, %1" : "=r"(x) : "r"(a));
713 return x;
714 }
715 #else
716 #define DRMP3_HAVE_ARMV6 0
717 #endif
718
719
720 /* Standard library stuff. */
721 #ifndef DRMP3_ASSERT
722 #include <assert.h>
723 #define DRMP3_ASSERT(expression) assert(expression)
724 #endif
725 #ifndef DRMP3_COPY_MEMORY
726 #define DRMP3_COPY_MEMORY(dst, src, sz) memcpy((dst), (src), (sz))
727 #endif
728 #ifndef DRMP3_MOVE_MEMORY
729 #define DRMP3_MOVE_MEMORY(dst, src, sz) memmove((dst), (src), (sz))
730 #endif
731 #ifndef DRMP3_ZERO_MEMORY
732 #define DRMP3_ZERO_MEMORY(p, sz) memset((p), 0, (sz))
733 #endif
734 #define DRMP3_ZERO_OBJECT(p) DRMP3_ZERO_MEMORY((p), sizeof(*(p)))
735 #ifndef DRMP3_MALLOC
736 #define DRMP3_MALLOC(sz) malloc((sz))
737 #endif
738 #ifndef DRMP3_REALLOC
739 #define DRMP3_REALLOC(p, sz) realloc((p), (sz))
740 #endif
741 #ifndef DRMP3_FREE
742 #define DRMP3_FREE(p) free((p))
743 #endif
744
745 typedef struct
746 {
747 const drmp3_uint8 *buf;
748 int pos, limit;
749 } drmp3_bs;
750
751 typedef struct
752 {
753 float scf[3*64];
754 drmp3_uint8 total_bands, stereo_bands, bitalloc[64], scfcod[64];
755 } drmp3_L12_scale_info;
756
757 typedef struct
758 {
759 drmp3_uint8 tab_offset, code_tab_width, band_count;
760 } drmp3_L12_subband_alloc;
761
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 static void drmp3_bs_init(drmp3_bs *bs, const drmp3_uint8 *data, int bytes)
781 {
782 bs->buf = data;
783 bs->pos = 0;
784 bs->limit = bytes*8;
785 }
786
787 static drmp3_uint32 drmp3_bs_get_bits(drmp3_bs *bs, int n)
788 {
789 drmp3_uint32 next, cache = 0, s = bs->pos & 7;
790 int shl = n + s;
791 const drmp3_uint8 *p = bs->buf + (bs->pos >> 3);
792 if ((bs->pos += n) > bs->limit)
793 return 0;
794 next = *p++ & (255 >> s);
795 while ((shl -= 8) > 0)
796 {
797 cache |= next << shl;
798 next = *p++;
799 }
800 return cache | (next >> -shl);
801 }
802
803 static int drmp3_hdr_valid(const drmp3_uint8 *h)
804 {
805 return h[0] == 0xff &&
806 ((h[1] & 0xF0) == 0xf0 || (h[1] & 0xFE) == 0xe2) &&
807 (DRMP3_HDR_GET_LAYER(h) != 0) &&
808 (DRMP3_HDR_GET_BITRATE(h) != 15) &&
809 (DRMP3_HDR_GET_SAMPLE_RATE(h) != 3);
810 }
811
812 static int drmp3_hdr_compare(const drmp3_uint8 *h1, const drmp3_uint8 *h2)
813 {
814 return drmp3_hdr_valid(h2) &&
815 ((h1[1] ^ h2[1]) & 0xFE) == 0 &&
816 ((h1[2] ^ h2[2]) & 0x0C) == 0 &&
817 !(DRMP3_HDR_IS_FREE_FORMAT(h1) ^ DRMP3_HDR_IS_FREE_FORMAT(h2));
818 }
819
820 static unsigned drmp3_hdr_bitrate_kbps(const drmp3_uint8 *h)
821 {
822 static const drmp3_uint8 halfrate[2][3][15] = {
823 { { 0,4,8,12,16,20,24,28,32,40,48,56,64,72,80 }, { 0,4,8,12,16,20,24,28,32,40,48,56,64,72,80 }, { 0,16,24,28,32,40,48,56,64,72,80,88,96,112,128 } },
824 { { 0,16,20,24,28,32,40,48,56,64,80,96,112,128,160 }, { 0,16,24,28,32,40,48,56,64,80,96,112,128,160,192 }, { 0,16,32,48,64,80,96,112,128,144,160,176,192,208,224 } },
825 };
826 return 2*halfrate[!!DRMP3_HDR_TEST_MPEG1(h)][DRMP3_HDR_GET_LAYER(h) - 1][DRMP3_HDR_GET_BITRATE(h)];
827 }
828
829 static unsigned drmp3_hdr_sample_rate_hz(const drmp3_uint8 *h)
830 {
831 static const unsigned g_hz[3] = { 44100, 48000, 32000 };
832 return g_hz[DRMP3_HDR_GET_SAMPLE_RATE(h)] >> (int)!DRMP3_HDR_TEST_MPEG1(h) >> (int)!DRMP3_HDR_TEST_NOT_MPEG25(h);
833 }
834
835 static unsigned drmp3_hdr_frame_samples(const drmp3_uint8 *h)
836 {
837 return DRMP3_HDR_IS_LAYER_1(h) ? 384 : (1152 >> (int)DRMP3_HDR_IS_FRAME_576(h));
838 }
839
840 static int drmp3_hdr_frame_bytes(const drmp3_uint8 *h, int free_format_size)
841 {
842 int frame_bytes = drmp3_hdr_frame_samples(h)*drmp3_hdr_bitrate_kbps(h)*125/drmp3_hdr_sample_rate_hz(h);
843 if (DRMP3_HDR_IS_LAYER_1(h))
844 {
845 frame_bytes &= ~3; /* slot align */
846 }
847 return frame_bytes ? frame_bytes : free_format_size;
848 }
849
850 static int drmp3_hdr_padding(const drmp3_uint8 *h)
851 {
852 return DRMP3_HDR_TEST_PADDING(h) ? (DRMP3_HDR_IS_LAYER_1(h) ? 4 : 1) : 0;
853 }
854
855 #ifndef DR_MP3_ONLY_MP3
856 static const drmp3_L12_subband_alloc *drmp3_L12_subband_alloc_table(const drmp3_uint8 *hdr, drmp3_L12_scale_info *sci)
857 {
858 const drmp3_L12_subband_alloc *alloc;
859 int mode = DRMP3_HDR_GET_STEREO_MODE(hdr);
860 int nbands, stereo_bands = (mode == DRMP3_MODE_MONO) ? 0 : (mode == DRMP3_MODE_JOINT_STEREO) ? (DRMP3_HDR_GET_STEREO_MODE_EXT(hdr) << 2) + 4 : 32;
861
862 if (DRMP3_HDR_IS_LAYER_1(hdr))
863 {
864 static const drmp3_L12_subband_alloc g_alloc_L1[] = { { 76, 4, 32 } };
865 alloc = g_alloc_L1;
866 nbands = 32;
867 } else if (!DRMP3_HDR_TEST_MPEG1(hdr))
868 {
869 static const drmp3_L12_subband_alloc g_alloc_L2M2[] = { { 60, 4, 4 }, { 44, 3, 7 }, { 44, 2, 19 } };
870 alloc = g_alloc_L2M2;
871 nbands = 30;
872 } else
873 {
874 static const drmp3_L12_subband_alloc g_alloc_L2M1[] = { { 0, 4, 3 }, { 16, 4, 8 }, { 32, 3, 12 }, { 40, 2, 7 } };
875 int sample_rate_idx = DRMP3_HDR_GET_SAMPLE_RATE(hdr);
876 unsigned kbps = drmp3_hdr_bitrate_kbps(hdr) >> (int)(mode != DRMP3_MODE_MONO);
877 if (!kbps) /* free-format */
878 {
879 kbps = 192;
880 }
881
882 alloc = g_alloc_L2M1;
883 nbands = 27;
884 if (kbps < 56)
885 {
886 static const drmp3_L12_subband_alloc g_alloc_L2M1_lowrate[] = { { 44, 4, 2 }, { 44, 3, 10 } };
887 alloc = g_alloc_L2M1_lowrate;
888 nbands = sample_rate_idx == 2 ? 12 : 8;
889 } else if (kbps >= 96 && sample_rate_idx != 1)
890 {
891 nbands = 30;
892 }
893 }
894
895 sci->total_bands = (drmp3_uint8)nbands;
896 sci->stereo_bands = (drmp3_uint8)DRMP3_MIN(stereo_bands, nbands);
897
898 return alloc;
899 }
900
901 static void drmp3_L12_read_scalefactors(drmp3_bs *bs, drmp3_uint8 *pba, drmp3_uint8 *scfcod, int bands, float *scf)
902 {
903 static const float g_deq_L12[18*3] = {
904 #define DRMP3_DQ(x) 9.53674316e-07f/x, 7.56931807e-07f/x, 6.00777173e-07f/x
905 DRMP3_DQ(3),DRMP3_DQ(7),DRMP3_DQ(15),DRMP3_DQ(31),DRMP3_DQ(63),DRMP3_DQ(127),DRMP3_DQ(255),DRMP3_DQ(511),DRMP3_DQ(1023),DRMP3_DQ(2047),DRMP3_DQ(4095),DRMP3_DQ(8191),DRMP3_DQ(16383),DRMP3_DQ(32767),DRMP3_DQ(65535),DRMP3_DQ(3),DRMP3_DQ(5),DRMP3_DQ(9)
906 };
907 int i, m;
908 for (i = 0; i < bands; i++)
909 {
910 float s = 0;
911 int ba = *pba++;
912 int mask = ba ? 4 + ((19 >> scfcod[i]) & 3) : 0;
913 for (m = 4; m; m >>= 1)
914 {
915 if (mask & m)
916 {
917 int b = drmp3_bs_get_bits(bs, 6);
918 s = g_deq_L12[ba*3 - 6 + b % 3]*(int)(1 << 21 >> b/3);
919 }
920 *scf++ = s;
921 }
922 }
923 }
924
925 static void drmp3_L12_read_scale_info(const drmp3_uint8 *hdr, drmp3_bs *bs, drmp3_L12_scale_info *sci)
926 {
927 static const drmp3_uint8 g_bitalloc_code_tab[] = {
928 0,17, 3, 4, 5,6,7, 8,9,10,11,12,13,14,15,16,
929 0,17,18, 3,19,4,5, 6,7, 8, 9,10,11,12,13,16,
930 0,17,18, 3,19,4,5,16,
931 0,17,18,16,
932 0,17,18,19, 4,5,6, 7,8, 9,10,11,12,13,14,15,
933 0,17,18, 3,19,4,5, 6,7, 8, 9,10,11,12,13,14,
934 0, 2, 3, 4, 5,6,7, 8,9,10,11,12,13,14,15,16
935 };
936 const drmp3_L12_subband_alloc *subband_alloc = drmp3_L12_subband_alloc_table(hdr, sci);
937
938 int i, k = 0, ba_bits = 0;
939 const drmp3_uint8 *ba_code_tab = g_bitalloc_code_tab;
940
941 for (i = 0; i < sci->total_bands; i++)
942 {
943 drmp3_uint8 ba;
944 if (i == k)
945 {
946 k += subband_alloc->band_count;
947 ba_bits = subband_alloc->code_tab_width;
948 ba_code_tab = g_bitalloc_code_tab + subband_alloc->tab_offset;
949 subband_alloc++;
950 }
951 ba = ba_code_tab[drmp3_bs_get_bits(bs, ba_bits)];
952 sci->bitalloc[2*i] = ba;
953 if (i < sci->stereo_bands)
954 {
955 ba = ba_code_tab[drmp3_bs_get_bits(bs, ba_bits)];
956 }
957 sci->bitalloc[2*i + 1] = sci->stereo_bands ? ba : 0;
958 }
959
960 for (i = 0; i < 2*sci->total_bands; i++)
961 {
962 sci->scfcod[i] = (drmp3_uint8)(sci->bitalloc[i] ? DRMP3_HDR_IS_LAYER_1(hdr) ? 2 : drmp3_bs_get_bits(bs, 2) : 6);
963 }
964
965 drmp3_L12_read_scalefactors(bs, sci->bitalloc, sci->scfcod, sci->total_bands*2, sci->scf);
966
967 for (i = sci->stereo_bands; i < sci->total_bands; i++)
968 {
969 sci->bitalloc[2*i + 1] = 0;
970 }
971 }
972
973 static int drmp3_L12_dequantize_granule(float *grbuf, drmp3_bs *bs, drmp3_L12_scale_info *sci, int group_size)
974 {
975 int i, j, k, choff = 576;
976 for (j = 0; j < 4; j++)
977 {
978 float *dst = grbuf + group_size*j;
979 for (i = 0; i < 2*sci->total_bands; i++)
980 {
981 int ba = sci->bitalloc[i];
982 if (ba != 0)
983 {
984 if (ba < 17)
985 {
986 int half = (1 << (ba - 1)) - 1;
987 for (k = 0; k < group_size; k++)
988 {
989 dst[k] = (float)((int)drmp3_bs_get_bits(bs, ba) - half);
990 }
991 } else
992 {
993 unsigned mod = (2 << (ba - 17)) + 1; /* 3, 5, 9 */
994 unsigned code = drmp3_bs_get_bits(bs, mod + 2 - (mod >> 3)); /* 5, 7, 10 */
995 for (k = 0; k < group_size; k++, code /= mod)
996 {
997 dst[k] = (float)((int)(code % mod - mod/2));
998 }
999 }
1000 }
1001 dst += choff;
1002 choff = 18 - choff;
1003 }
1004 }
1005 return group_size*4;
1006 }
1007
1008 static void drmp3_L12_apply_scf_384(drmp3_L12_scale_info *sci, const float *scf, float *dst)
1009 {
1010 int i, k;
1011 DRMP3_COPY_MEMORY(dst + 576 + sci->stereo_bands*18, dst + sci->stereo_bands*18, (sci->total_bands - sci->stereo_bands)*18*sizeof(float));
1012 for (i = 0; i < sci->total_bands; i++, dst += 18, scf += 6)
1013 {
1014 for (k = 0; k < 12; k++)
1015 {
1016 dst[k + 0] *= scf[0];
1017 dst[k + 576] *= scf[3];
1018 }
1019 }
1020 }
1021 #endif
1022
1023 static int drmp3_L3_read_side_info(drmp3_bs *bs, drmp3_L3_gr_info *gr, const drmp3_uint8 *hdr)
1024 {
1025 static const drmp3_uint8 g_scf_long[8][23] = {
1026 { 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 },
1027 { 12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2,0 },
1028 { 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 },
1029 { 6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,54,62,70,76,36,0 },
1030 { 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 },
1031 { 4,4,4,4,4,4,6,6,8,8,10,12,16,20,24,28,34,42,50,54,76,158,0 },
1032 { 4,4,4,4,4,4,6,6,6,8,10,12,16,18,22,28,34,40,46,54,54,192,0 },
1033 { 4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102,26,0 }
1034 };
1035 static const drmp3_uint8 g_scf_short[8][40] = {
1036 { 4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,30,30,30,40,40,40,18,18,18,0 },
1037 { 8,8,8,8,8,8,8,8,8,12,12,12,16,16,16,20,20,20,24,24,24,28,28,28,36,36,36,2,2,2,2,2,2,2,2,2,26,26,26,0 },
1038 { 4,4,4,4,4,4,4,4,4,6,6,6,6,6,6,8,8,8,10,10,10,14,14,14,18,18,18,26,26,26,32,32,32,42,42,42,18,18,18,0 },
1039 { 4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,32,32,32,44,44,44,12,12,12,0 },
1040 { 4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,30,30,30,40,40,40,18,18,18,0 },
1041 { 4,4,4,4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,22,22,22,30,30,30,56,56,56,0 },
1042 { 4,4,4,4,4,4,4,4,4,4,4,4,6,6,6,6,6,6,10,10,10,12,12,12,14,14,14,16,16,16,20,20,20,26,26,26,66,66,66,0 },
1043 { 4,4,4,4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,12,12,12,16,16,16,20,20,20,26,26,26,34,34,34,42,42,42,12,12,12,0 }
1044 };
1045 static const drmp3_uint8 g_scf_mixed[8][40] = {
1046 { 6,6,6,6,6,6,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,30,30,30,40,40,40,18,18,18,0 },
1047 { 12,12,12,4,4,4,8,8,8,12,12,12,16,16,16,20,20,20,24,24,24,28,28,28,36,36,36,2,2,2,2,2,2,2,2,2,26,26,26,0 },
1048 { 6,6,6,6,6,6,6,6,6,6,6,6,8,8,8,10,10,10,14,14,14,18,18,18,26,26,26,32,32,32,42,42,42,18,18,18,0 },
1049 { 6,6,6,6,6,6,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,32,32,32,44,44,44,12,12,12,0 },
1050 { 6,6,6,6,6,6,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,30,30,30,40,40,40,18,18,18,0 },
1051 { 4,4,4,4,4,4,6,6,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,22,22,22,30,30,30,56,56,56,0 },
1052 { 4,4,4,4,4,4,6,6,4,4,4,6,6,6,6,6,6,10,10,10,12,12,12,14,14,14,16,16,16,20,20,20,26,26,26,66,66,66,0 },
1053 { 4,4,4,4,4,4,6,6,4,4,4,6,6,6,8,8,8,12,12,12,16,16,16,20,20,20,26,26,26,34,34,34,42,42,42,12,12,12,0 }
1054 };
1055
1056 unsigned tables, scfsi = 0;
1057 int main_data_begin, part_23_sum = 0;
1058 int gr_count = DRMP3_HDR_IS_MONO(hdr) ? 1 : 2;
1059 int sr_idx = DRMP3_HDR_GET_MY_SAMPLE_RATE(hdr); sr_idx -= (sr_idx != 0);
1060
1061 if (DRMP3_HDR_TEST_MPEG1(hdr))
1062 {
1063 gr_count *= 2;
1064 main_data_begin = drmp3_bs_get_bits(bs, 9);
1065 scfsi = drmp3_bs_get_bits(bs, 7 + gr_count);
1066 } else
1067 {
1068 main_data_begin = drmp3_bs_get_bits(bs, 8 + gr_count) >> gr_count;
1069 }
1070
1071 do
1072 {
1073 if (DRMP3_HDR_IS_MONO(hdr))
1074 {
1075 scfsi <<= 4;
1076 }
1077 gr->part_23_length = (drmp3_uint16)drmp3_bs_get_bits(bs, 12);
1078 part_23_sum += gr->part_23_length;
1079 gr->big_values = (drmp3_uint16)drmp3_bs_get_bits(bs, 9);
1080 if (gr->big_values > 288)
1081 {
1082 return -1;
1083 }
1084 gr->global_gain = (drmp3_uint8)drmp3_bs_get_bits(bs, 8);
1085 gr->scalefac_compress = (drmp3_uint16)drmp3_bs_get_bits(bs, DRMP3_HDR_TEST_MPEG1(hdr) ? 4 : 9);
1086 gr->sfbtab = g_scf_long[sr_idx];
1087 gr->n_long_sfb = 22;
1088 gr->n_short_sfb = 0;
1089 if (drmp3_bs_get_bits(bs, 1))
1090 {
1091 gr->block_type = (drmp3_uint8)drmp3_bs_get_bits(bs, 2);
1092 if (!gr->block_type)
1093 {
1094 return -1;
1095 }
1096 gr->mixed_block_flag = (drmp3_uint8)drmp3_bs_get_bits(bs, 1);
1097 gr->region_count[0] = 7;
1098 gr->region_count[1] = 255;
1099 if (gr->block_type == DRMP3_SHORT_BLOCK_TYPE)
1100 {
1101 scfsi &= 0x0F0F;
1102 if (!gr->mixed_block_flag)
1103 {
1104 gr->region_count[0] = 8;
1105 gr->sfbtab = g_scf_short[sr_idx];
1106 gr->n_long_sfb = 0;
1107 gr->n_short_sfb = 39;
1108 } else
1109 {
1110 gr->sfbtab = g_scf_mixed[sr_idx];
1111 gr->n_long_sfb = DRMP3_HDR_TEST_MPEG1(hdr) ? 8 : 6;
1112 gr->n_short_sfb = 30;
1113 }
1114 }
1115 tables = drmp3_bs_get_bits(bs, 10);
1116 tables <<= 5;
1117 gr->subblock_gain[0] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3);
1118 gr->subblock_gain[1] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3);
1119 gr->subblock_gain[2] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3);
1120 } else
1121 {
1122 gr->block_type = 0;
1123 gr->mixed_block_flag = 0;
1124 tables = drmp3_bs_get_bits(bs, 15);
1125 gr->region_count[0] = (drmp3_uint8)drmp3_bs_get_bits(bs, 4);
1126 gr->region_count[1] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3);
1127 gr->region_count[2] = 255;
1128 }
1129 gr->table_select[0] = (drmp3_uint8)(tables >> 10);
1130 gr->table_select[1] = (drmp3_uint8)((tables >> 5) & 31);
1131 gr->table_select[2] = (drmp3_uint8)((tables) & 31);
1132 gr->preflag = (drmp3_uint8)(DRMP3_HDR_TEST_MPEG1(hdr) ? drmp3_bs_get_bits(bs, 1) : (gr->scalefac_compress >= 500));
1133 gr->scalefac_scale = (drmp3_uint8)drmp3_bs_get_bits(bs, 1);
1134 gr->count1_table = (drmp3_uint8)drmp3_bs_get_bits(bs, 1);
1135 gr->scfsi = (drmp3_uint8)((scfsi >> 12) & 15);
1136 scfsi <<= 4;
1137 gr++;
1138 } while(--gr_count);
1139
1140 if (part_23_sum + bs->pos > bs->limit + main_data_begin*8)
1141 {
1142 return -1;
1143 }
1144
1145 return main_data_begin;
1146 }
1147
1148 static void drmp3_L3_read_scalefactors(drmp3_uint8 *scf, drmp3_uint8 *ist_pos, const drmp3_uint8 *scf_size, const drmp3_uint8 *scf_count, drmp3_bs *bitbuf, int scfsi)
1149 {
1150 int i, k;
1151 for (i = 0; i < 4 && scf_count[i]; i++, scfsi *= 2)
1152 {
1153 int cnt = scf_count[i];
1154 if (scfsi & 8)
1155 {
1156 DRMP3_COPY_MEMORY(scf, ist_pos, cnt);
1157 } else
1158 {
1159 int bits = scf_size[i];
1160 if (!bits)
1161 {
1162 DRMP3_ZERO_MEMORY(scf, cnt);
1163 DRMP3_ZERO_MEMORY(ist_pos, cnt);
1164 } else
1165 {
1166 int max_scf = (scfsi < 0) ? (1 << bits) - 1 : -1;
1167 for (k = 0; k < cnt; k++)
1168 {
1169 int s = drmp3_bs_get_bits(bitbuf, bits);
1170 ist_pos[k] = (drmp3_uint8)(s == max_scf ? -1 : s);
1171 scf[k] = (drmp3_uint8)s;
1172 }
1173 }
1174 }
1175 ist_pos += cnt;
1176 scf += cnt;
1177 }
1178 scf[0] = scf[1] = scf[2] = 0;
1179 }
1180
1181 static float drmp3_L3_ldexp_q2(float y, int exp_q2)
1182 {
1183 static const float g_expfrac[4] = { 9.31322575e-10f,7.83145814e-10f,6.58544508e-10f,5.53767716e-10f };
1184 int e;
1185 do
1186 {
1187 e = DRMP3_MIN(30*4, exp_q2);
1188 y *= g_expfrac[e & 3]*(1 << 30 >> (e >> 2));
1189 } while ((exp_q2 -= e) > 0);
1190 return y;
1191 }
1192
1193 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 {
1195 static const drmp3_uint8 g_scf_partitions[3][28] = {
1196 { 6,5,5, 5,6,5,5,5,6,5, 7,3,11,10,0,0, 7, 7, 7,0, 6, 6,6,3, 8, 8,5,0 },
1197 { 8,9,6,12,6,9,9,9,6,9,12,6,15,18,0,0, 6,15,12,0, 6,12,9,6, 6,18,9,0 },
1198 { 9,9,6,12,9,9,9,9,9,9,12,6,18,18,0,0,12,12,12,0,12, 9,9,6,15,12,9,0 }
1199 };
1200 const drmp3_uint8 *scf_partition = g_scf_partitions[!!gr->n_short_sfb + !gr->n_long_sfb];
1201 drmp3_uint8 scf_size[4], iscf[40];
1202 int i, scf_shift = gr->scalefac_scale + 1, gain_exp, scfsi = gr->scfsi;
1203 float gain;
1204
1205 if (DRMP3_HDR_TEST_MPEG1(hdr))
1206 {
1207 static const drmp3_uint8 g_scfc_decode[16] = { 0,1,2,3, 12,5,6,7, 9,10,11,13, 14,15,18,19 };
1208 int part = g_scfc_decode[gr->scalefac_compress];
1209 scf_size[1] = scf_size[0] = (drmp3_uint8)(part >> 2);
1210 scf_size[3] = scf_size[2] = (drmp3_uint8)(part & 3);
1211 } else
1212 {
1213 static const drmp3_uint8 g_mod[6*4] = { 5,5,4,4,5,5,4,1,4,3,1,1,5,6,6,1,4,4,4,1,4,3,1,1 };
1214 int k, modprod, sfc, ist = DRMP3_HDR_TEST_I_STEREO(hdr) && ch;
1215 sfc = gr->scalefac_compress >> ist;
1216 for (k = ist*3*4; sfc >= 0; sfc -= modprod, k += 4)
1217 {
1218 for (modprod = 1, i = 3; i >= 0; i--)
1219 {
1220 scf_size[i] = (drmp3_uint8)(sfc / modprod % g_mod[k + i]);
1221 modprod *= g_mod[k + i];
1222 }
1223 }
1224 scf_partition += k;
1225 scfsi = -16;
1226 }
1227 drmp3_L3_read_scalefactors(iscf, ist_pos, scf_size, scf_partition, bs, scfsi);
1228
1229 if (gr->n_short_sfb)
1230 {
1231 int sh = 3 - scf_shift;
1232 for (i = 0; i < gr->n_short_sfb; i += 3)
1233 {
1234 iscf[gr->n_long_sfb + i + 0] = (drmp3_uint8)(iscf[gr->n_long_sfb + i + 0] + (gr->subblock_gain[0] << sh));
1235 iscf[gr->n_long_sfb + i + 1] = (drmp3_uint8)(iscf[gr->n_long_sfb + i + 1] + (gr->subblock_gain[1] << sh));
1236 iscf[gr->n_long_sfb + i + 2] = (drmp3_uint8)(iscf[gr->n_long_sfb + i + 2] + (gr->subblock_gain[2] << sh));
1237 }
1238 } else if (gr->preflag)
1239 {
1240 static const drmp3_uint8 g_preamp[10] = { 1,1,1,1,2,2,3,3,3,2 };
1241 for (i = 0; i < 10; i++)
1242 {
1243 iscf[11 + i] = (drmp3_uint8)(iscf[11 + i] + g_preamp[i]);
1244 }
1245 }
1246
1247 gain_exp = gr->global_gain + DRMP3_BITS_DEQUANTIZER_OUT*4 - 210 - (DRMP3_HDR_IS_MS_STEREO(hdr) ? 2 : 0);
1248 gain = drmp3_L3_ldexp_q2(1 << (DRMP3_MAX_SCFI/4), DRMP3_MAX_SCFI - gain_exp);
1249 for (i = 0; i < (int)(gr->n_long_sfb + gr->n_short_sfb); i++)
1250 {
1251 scf[i] = drmp3_L3_ldexp_q2(gain, iscf[i] << scf_shift);
1252 }
1253 }
1254
1255 static const float g_drmp3_pow43[129 + 16] = {
1256 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,
1257 0,1,2.519842f,4.326749f,6.349604f,8.549880f,10.902724f,13.390518f,16.000000f,18.720754f,21.544347f,24.463781f,27.473142f,30.567351f,33.741992f,36.993181f,40.317474f,43.711787f,47.173345f,50.699631f,54.288352f,57.937408f,61.644865f,65.408941f,69.227979f,73.100443f,77.024898f,81.000000f,85.024491f,89.097188f,93.216975f,97.382800f,101.593667f,105.848633f,110.146801f,114.487321f,118.869381f,123.292209f,127.755065f,132.257246f,136.798076f,141.376907f,145.993119f,150.646117f,155.335327f,160.060199f,164.820202f,169.614826f,174.443577f,179.305980f,184.201575f,189.129918f,194.090580f,199.083145f,204.107210f,209.162385f,214.248292f,219.364564f,224.510845f,229.686789f,234.892058f,240.126328f,245.389280f,250.680604f,256.000000f,261.347174f,266.721841f,272.123723f,277.552547f,283.008049f,288.489971f,293.998060f,299.532071f,305.091761f,310.676898f,316.287249f,321.922592f,327.582707f,333.267377f,338.976394f,344.709550f,350.466646f,356.247482f,362.051866f,367.879608f,373.730522f,379.604427f,385.501143f,391.420496f,397.362314f,403.326427f,409.312672f,415.320884f,421.350905f,427.402579f,433.475750f,439.570269f,445.685987f,451.822757f,457.980436f,464.158883f,470.357960f,476.577530f,482.817459f,489.077615f,495.357868f,501.658090f,507.978156f,514.317941f,520.677324f,527.056184f,533.454404f,539.871867f,546.308458f,552.764065f,559.238575f,565.731879f,572.243870f,578.774440f,585.323483f,591.890898f,598.476581f,605.080431f,611.702349f,618.342238f,625.000000f,631.675540f,638.368763f,645.079578f
1258 };
1259
1260 static float drmp3_L3_pow_43(int x)
1261 {
1262 float frac;
1263 int sign, mult = 256;
1264
1265 if (x < 129)
1266 {
1267 return g_drmp3_pow43[16 + x];
1268 }
1269
1270 if (x < 1024)
1271 {
1272 mult = 16;
1273 x <<= 3;
1274 }
1275
1276 sign = 2*x & 64;
1277 frac = (float)((x & 63) - sign) / ((x & ~63) + sign);
1278 return g_drmp3_pow43[16 + ((x + sign) >> 6)]*(1.f + frac*((4.f/3) + frac*(2.f/9)))*mult;
1279 }
1280
1281 static void drmp3_L3_huffman(float *dst, drmp3_bs *bs, const drmp3_L3_gr_info *gr_info, const float *scf, int layer3gr_limit)
1282 {
1283 static const drmp3_int16 tabs[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1284 785,785,785,785,784,784,784,784,513,513,513,513,513,513,513,513,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,
1285 -255,1313,1298,1282,785,785,785,785,784,784,784,784,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,290,288,
1286 -255,1313,1298,1282,769,769,769,769,529,529,529,529,529,529,529,529,528,528,528,528,528,528,528,528,512,512,512,512,512,512,512,512,290,288,
1287 -253,-318,-351,-367,785,785,785,785,784,784,784,784,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,819,818,547,547,275,275,275,275,561,560,515,546,289,274,288,258,
1288 -254,-287,1329,1299,1314,1312,1057,1057,1042,1042,1026,1026,784,784,784,784,529,529,529,529,529,529,529,529,769,769,769,769,768,768,768,768,563,560,306,306,291,259,
1289 -252,-413,-477,-542,1298,-575,1041,1041,784,784,784,784,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,-383,-399,1107,1092,1106,1061,849,849,789,789,1104,1091,773,773,1076,1075,341,340,325,309,834,804,577,577,532,532,516,516,832,818,803,816,561,561,531,531,515,546,289,289,288,258,
1290 -252,-429,-493,-559,1057,1057,1042,1042,529,529,529,529,529,529,529,529,784,784,784,784,769,769,769,769,512,512,512,512,512,512,512,512,-382,1077,-415,1106,1061,1104,849,849,789,789,1091,1076,1029,1075,834,834,597,581,340,340,339,324,804,833,532,532,832,772,818,803,817,787,816,771,290,290,290,290,288,258,
1291 -253,-349,-414,-447,-463,1329,1299,-479,1314,1312,1057,1057,1042,1042,1026,1026,785,785,785,785,784,784,784,784,769,769,769,769,768,768,768,768,-319,851,821,-335,836,850,805,849,341,340,325,336,533,533,579,579,564,564,773,832,578,548,563,516,321,276,306,291,304,259,
1292 -251,-572,-733,-830,-863,-879,1041,1041,784,784,784,784,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,-511,-527,-543,1396,1351,1381,1366,1395,1335,1380,-559,1334,1138,1138,1063,1063,1350,1392,1031,1031,1062,1062,1364,1363,1120,1120,1333,1348,881,881,881,881,375,374,359,373,343,358,341,325,791,791,1123,1122,-703,1105,1045,-719,865,865,790,790,774,774,1104,1029,338,293,323,308,-799,-815,833,788,772,818,803,816,322,292,307,320,561,531,515,546,289,274,288,258,
1293 -251,-525,-605,-685,-765,-831,-846,1298,1057,1057,1312,1282,785,785,785,785,784,784,784,784,769,769,769,769,512,512,512,512,512,512,512,512,1399,1398,1383,1367,1382,1396,1351,-511,1381,1366,1139,1139,1079,1079,1124,1124,1364,1349,1363,1333,882,882,882,882,807,807,807,807,1094,1094,1136,1136,373,341,535,535,881,775,867,822,774,-591,324,338,-671,849,550,550,866,864,609,609,293,336,534,534,789,835,773,-751,834,804,308,307,833,788,832,772,562,562,547,547,305,275,560,515,290,290,
1294 -252,-397,-477,-557,-622,-653,-719,-735,-750,1329,1299,1314,1057,1057,1042,1042,1312,1282,1024,1024,785,785,785,785,784,784,784,784,769,769,769,769,-383,1127,1141,1111,1126,1140,1095,1110,869,869,883,883,1079,1109,882,882,375,374,807,868,838,881,791,-463,867,822,368,263,852,837,836,-543,610,610,550,550,352,336,534,534,865,774,851,821,850,805,593,533,579,564,773,832,578,578,548,548,577,577,307,276,306,291,516,560,259,259,
1295 -250,-2107,-2507,-2764,-2909,-2974,-3007,-3023,1041,1041,1040,1040,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,-767,-1052,-1213,-1277,-1358,-1405,-1469,-1535,-1550,-1582,-1614,-1647,-1662,-1694,-1726,-1759,-1774,-1807,-1822,-1854,-1886,1565,-1919,-1935,-1951,-1967,1731,1730,1580,1717,-1983,1729,1564,-1999,1548,-2015,-2031,1715,1595,-2047,1714,-2063,1610,-2079,1609,-2095,1323,1323,1457,1457,1307,1307,1712,1547,1641,1700,1699,1594,1685,1625,1442,1442,1322,1322,-780,-973,-910,1279,1278,1277,1262,1276,1261,1275,1215,1260,1229,-959,974,974,989,989,-943,735,478,478,495,463,506,414,-1039,1003,958,1017,927,942,987,957,431,476,1272,1167,1228,-1183,1256,-1199,895,895,941,941,1242,1227,1212,1135,1014,1014,490,489,503,487,910,1013,985,925,863,894,970,955,1012,847,-1343,831,755,755,984,909,428,366,754,559,-1391,752,486,457,924,997,698,698,983,893,740,740,908,877,739,739,667,667,953,938,497,287,271,271,683,606,590,712,726,574,302,302,738,736,481,286,526,725,605,711,636,724,696,651,589,681,666,710,364,467,573,695,466,466,301,465,379,379,709,604,665,679,316,316,634,633,436,436,464,269,424,394,452,332,438,363,347,408,393,448,331,422,362,407,392,421,346,406,391,376,375,359,1441,1306,-2367,1290,-2383,1337,-2399,-2415,1426,1321,-2431,1411,1336,-2447,-2463,-2479,1169,1169,1049,1049,1424,1289,1412,1352,1319,-2495,1154,1154,1064,1064,1153,1153,416,390,360,404,403,389,344,374,373,343,358,372,327,357,342,311,356,326,1395,1394,1137,1137,1047,1047,1365,1392,1287,1379,1334,1364,1349,1378,1318,1363,792,792,792,792,1152,1152,1032,1032,1121,1121,1046,1046,1120,1120,1030,1030,-2895,1106,1061,1104,849,849,789,789,1091,1076,1029,1090,1060,1075,833,833,309,324,532,532,832,772,818,803,561,561,531,560,515,546,289,274,288,258,
1296 -250,-1179,-1579,-1836,-1996,-2124,-2253,-2333,-2413,-2477,-2542,-2574,-2607,-2622,-2655,1314,1313,1298,1312,1282,785,785,785,785,1040,1040,1025,1025,768,768,768,768,-766,-798,-830,-862,-895,-911,-927,-943,-959,-975,-991,-1007,-1023,-1039,-1055,-1070,1724,1647,-1103,-1119,1631,1767,1662,1738,1708,1723,-1135,1780,1615,1779,1599,1677,1646,1778,1583,-1151,1777,1567,1737,1692,1765,1722,1707,1630,1751,1661,1764,1614,1736,1676,1763,1750,1645,1598,1721,1691,1762,1706,1582,1761,1566,-1167,1749,1629,767,766,751,765,494,494,735,764,719,749,734,763,447,447,748,718,477,506,431,491,446,476,461,505,415,430,475,445,504,399,460,489,414,503,383,474,429,459,502,502,746,752,488,398,501,473,413,472,486,271,480,270,-1439,-1455,1357,-1471,-1487,-1503,1341,1325,-1519,1489,1463,1403,1309,-1535,1372,1448,1418,1476,1356,1462,1387,-1551,1475,1340,1447,1402,1386,-1567,1068,1068,1474,1461,455,380,468,440,395,425,410,454,364,467,466,464,453,269,409,448,268,432,1371,1473,1432,1417,1308,1460,1355,1446,1459,1431,1083,1083,1401,1416,1458,1445,1067,1067,1370,1457,1051,1051,1291,1430,1385,1444,1354,1415,1400,1443,1082,1082,1173,1113,1186,1066,1185,1050,-1967,1158,1128,1172,1097,1171,1081,-1983,1157,1112,416,266,375,400,1170,1142,1127,1065,793,793,1169,1033,1156,1096,1141,1111,1155,1080,1126,1140,898,898,808,808,897,897,792,792,1095,1152,1032,1125,1110,1139,1079,1124,882,807,838,881,853,791,-2319,867,368,263,822,852,837,866,806,865,-2399,851,352,262,534,534,821,836,594,594,549,549,593,593,533,533,848,773,579,579,564,578,548,563,276,276,577,576,306,291,516,560,305,305,275,259,
1297 -251,-892,-2058,-2620,-2828,-2957,-3023,-3039,1041,1041,1040,1040,769,769,769,769,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,-511,-527,-543,-559,1530,-575,-591,1528,1527,1407,1526,1391,1023,1023,1023,1023,1525,1375,1268,1268,1103,1103,1087,1087,1039,1039,1523,-604,815,815,815,815,510,495,509,479,508,463,507,447,431,505,415,399,-734,-782,1262,-815,1259,1244,-831,1258,1228,-847,-863,1196,-879,1253,987,987,748,-767,493,493,462,477,414,414,686,669,478,446,461,445,474,429,487,458,412,471,1266,1264,1009,1009,799,799,-1019,-1276,-1452,-1581,-1677,-1757,-1821,-1886,-1933,-1997,1257,1257,1483,1468,1512,1422,1497,1406,1467,1496,1421,1510,1134,1134,1225,1225,1466,1451,1374,1405,1252,1252,1358,1480,1164,1164,1251,1251,1238,1238,1389,1465,-1407,1054,1101,-1423,1207,-1439,830,830,1248,1038,1237,1117,1223,1148,1236,1208,411,426,395,410,379,269,1193,1222,1132,1235,1221,1116,976,976,1192,1162,1177,1220,1131,1191,963,963,-1647,961,780,-1663,558,558,994,993,437,408,393,407,829,978,813,797,947,-1743,721,721,377,392,844,950,828,890,706,706,812,859,796,960,948,843,934,874,571,571,-1919,690,555,689,421,346,539,539,944,779,918,873,932,842,903,888,570,570,931,917,674,674,-2575,1562,-2591,1609,-2607,1654,1322,1322,1441,1441,1696,1546,1683,1593,1669,1624,1426,1426,1321,1321,1639,1680,1425,1425,1305,1305,1545,1668,1608,1623,1667,1592,1638,1666,1320,1320,1652,1607,1409,1409,1304,1304,1288,1288,1664,1637,1395,1395,1335,1335,1622,1636,1394,1394,1319,1319,1606,1621,1392,1392,1137,1137,1137,1137,345,390,360,375,404,373,1047,-2751,-2767,-2783,1062,1121,1046,-2799,1077,-2815,1106,1061,789,789,1105,1104,263,355,310,340,325,354,352,262,339,324,1091,1076,1029,1090,1060,1075,833,833,788,788,1088,1028,818,818,803,803,561,561,531,531,816,771,546,546,289,274,288,258,
1298 -253,-317,-381,-446,-478,-509,1279,1279,-811,-1179,-1451,-1756,-1900,-2028,-2189,-2253,-2333,-2414,-2445,-2511,-2526,1313,1298,-2559,1041,1041,1040,1040,1025,1025,1024,1024,1022,1007,1021,991,1020,975,1019,959,687,687,1018,1017,671,671,655,655,1016,1015,639,639,758,758,623,623,757,607,756,591,755,575,754,559,543,543,1009,783,-575,-621,-685,-749,496,-590,750,749,734,748,974,989,1003,958,988,973,1002,942,987,957,972,1001,926,986,941,971,956,1000,910,985,925,999,894,970,-1071,-1087,-1102,1390,-1135,1436,1509,1451,1374,-1151,1405,1358,1480,1420,-1167,1507,1494,1389,1342,1465,1435,1450,1326,1505,1310,1493,1373,1479,1404,1492,1464,1419,428,443,472,397,736,526,464,464,486,457,442,471,484,482,1357,1449,1434,1478,1388,1491,1341,1490,1325,1489,1463,1403,1309,1477,1372,1448,1418,1433,1476,1356,1462,1387,-1439,1475,1340,1447,1402,1474,1324,1461,1371,1473,269,448,1432,1417,1308,1460,-1711,1459,-1727,1441,1099,1099,1446,1386,1431,1401,-1743,1289,1083,1083,1160,1160,1458,1445,1067,1067,1370,1457,1307,1430,1129,1129,1098,1098,268,432,267,416,266,400,-1887,1144,1187,1082,1173,1113,1186,1066,1050,1158,1128,1143,1172,1097,1171,1081,420,391,1157,1112,1170,1142,1127,1065,1169,1049,1156,1096,1141,1111,1155,1080,1126,1154,1064,1153,1140,1095,1048,-2159,1125,1110,1137,-2175,823,823,1139,1138,807,807,384,264,368,263,868,838,853,791,867,822,852,837,866,806,865,790,-2319,851,821,836,352,262,850,805,849,-2399,533,533,835,820,336,261,578,548,563,577,532,532,832,772,562,562,547,547,305,275,560,515,290,290,288,258 };
1299 static const drmp3_uint8 tab32[] = { 130,162,193,209,44,28,76,140,9,9,9,9,9,9,9,9,190,254,222,238,126,94,157,157,109,61,173,205};
1300 static const drmp3_uint8 tab33[] = { 252,236,220,204,188,172,156,140,124,108,92,76,60,44,28,12 };
1301 static const drmp3_int16 tabindex[2*16] = { 0,32,64,98,0,132,180,218,292,364,426,538,648,746,0,1126,1460,1460,1460,1460,1460,1460,1460,1460,1842,1842,1842,1842,1842,1842,1842,1842 };
1302 static const drmp3_uint8 g_linbits[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4,6,8,10,13,4,5,6,7,8,9,11,13 };
1303
1304 #define DRMP3_PEEK_BITS(n) (bs_cache >> (32 - (n)))
1305 #define DRMP3_FLUSH_BITS(n) { bs_cache <<= (n); bs_sh += (n); }
1306 #define DRMP3_CHECK_BITS while (bs_sh >= 0) { bs_cache |= (drmp3_uint32)*bs_next_ptr++ << bs_sh; bs_sh -= 8; }
1307 #define DRMP3_BSPOS ((bs_next_ptr - bs->buf)*8 - 24 + bs_sh)
1308
1309 float one = 0.0f;
1310 int ireg = 0, big_val_cnt = gr_info->big_values;
1311 const drmp3_uint8 *sfb = gr_info->sfbtab;
1312 const drmp3_uint8 *bs_next_ptr = bs->buf + bs->pos/8;
1313 drmp3_uint32 bs_cache = (((bs_next_ptr[0]*256u + bs_next_ptr[1])*256u + bs_next_ptr[2])*256u + bs_next_ptr[3]) << (bs->pos & 7);
1314 int pairs_to_decode, np, bs_sh = (bs->pos & 7) - 8;
1315 bs_next_ptr += 4;
1316
1317 while (big_val_cnt > 0)
1318 {
1319 int tab_num = gr_info->table_select[ireg];
1320 int sfb_cnt = gr_info->region_count[ireg++];
1321 const drmp3_int16 *codebook = tabs + tabindex[tab_num];
1322 int linbits = g_linbits[tab_num];
1323 if (linbits)
1324 {
1325 do
1326 {
1327 np = *sfb++ / 2;
1328 pairs_to_decode = DRMP3_MIN(big_val_cnt, np);
1329 one = *scf++;
1330 do
1331 {
1332 int j, w = 5;
1333 int leaf = codebook[DRMP3_PEEK_BITS(w)];
1334 while (leaf < 0)
1335 {
1336 DRMP3_FLUSH_BITS(w);
1337 w = leaf & 7;
1338 leaf = codebook[DRMP3_PEEK_BITS(w) - (leaf >> 3)];
1339 }
1340 DRMP3_FLUSH_BITS(leaf >> 8);
1341
1342 for (j = 0; j < 2; j++, dst++, leaf >>= 4)
1343 {
1344 int lsb = leaf & 0x0F;
1345 if (lsb == 15)
1346 {
1347 lsb += DRMP3_PEEK_BITS(linbits);
1348 DRMP3_FLUSH_BITS(linbits);
1349 DRMP3_CHECK_BITS;
1350 *dst = one*drmp3_L3_pow_43(lsb)*((drmp3_int32)bs_cache < 0 ? -1: 1);
1351 } else
1352 {
1353 *dst = g_drmp3_pow43[16 + lsb - 16*(bs_cache >> 31)]*one;
1354 }
1355 DRMP3_FLUSH_BITS(lsb ? 1 : 0);
1356 }
1357 DRMP3_CHECK_BITS;
1358 } while (--pairs_to_decode);
1359 } while ((big_val_cnt -= np) > 0 && --sfb_cnt >= 0);
1360 } else
1361 {
1362 do
1363 {
1364 np = *sfb++ / 2;
1365 pairs_to_decode = DRMP3_MIN(big_val_cnt, np);
1366 one = *scf++;
1367 do
1368 {
1369 int j, w = 5;
1370 int leaf = codebook[DRMP3_PEEK_BITS(w)];
1371 while (leaf < 0)
1372 {
1373 DRMP3_FLUSH_BITS(w);
1374 w = leaf & 7;
1375 leaf = codebook[DRMP3_PEEK_BITS(w) - (leaf >> 3)];
1376 }
1377 DRMP3_FLUSH_BITS(leaf >> 8);
1378
1379 for (j = 0; j < 2; j++, dst++, leaf >>= 4)
1380 {
1381 int lsb = leaf & 0x0F;
1382 *dst = g_drmp3_pow43[16 + lsb - 16*(bs_cache >> 31)]*one;
1383 DRMP3_FLUSH_BITS(lsb ? 1 : 0);
1384 }
1385 DRMP3_CHECK_BITS;
1386 } while (--pairs_to_decode);
1387 } while ((big_val_cnt -= np) > 0 && --sfb_cnt >= 0);
1388 }
1389 }
1390
1391 for (np = 1 - big_val_cnt;; dst += 4)
1392 {
1393 const drmp3_uint8 *codebook_count1 = (gr_info->count1_table) ? tab33 : tab32;
1394 int leaf = codebook_count1[DRMP3_PEEK_BITS(4)];
1395 if (!(leaf & 8))
1396 {
1397 leaf = codebook_count1[(leaf >> 3) + (bs_cache << 4 >> (32 - (leaf & 3)))];
1398 }
1399 DRMP3_FLUSH_BITS(leaf & 7);
1400 if (DRMP3_BSPOS > layer3gr_limit)
1401 {
1402 break;
1403 }
1404 #define DRMP3_RELOAD_SCALEFACTOR if (!--np) { np = *sfb++/2; if (!np) break; one = *scf++; }
1405 #define DRMP3_DEQ_COUNT1(s) if (leaf & (128 >> s)) { dst[s] = ((drmp3_int32)bs_cache < 0) ? -one : one; DRMP3_FLUSH_BITS(1) }
1406 DRMP3_RELOAD_SCALEFACTOR;
1407 DRMP3_DEQ_COUNT1(0);
1408 DRMP3_DEQ_COUNT1(1);
1409 DRMP3_RELOAD_SCALEFACTOR;
1410 DRMP3_DEQ_COUNT1(2);
1411 DRMP3_DEQ_COUNT1(3);
1412 DRMP3_CHECK_BITS;
1413 }
1414
1415 bs->pos = layer3gr_limit;
1416 }
1417
1418 static void drmp3_L3_midside_stereo(float *left, int n)
1419 {
1420 int i = 0;
1421 float *right = left + 576;
1422 #if DRMP3_HAVE_SIMD
1423 if (drmp3_have_simd())
1424 {
1425 for (; i < n - 3; i += 4)
1426 {
1427 drmp3_f4 vl = DRMP3_VLD(left + i);
1428 drmp3_f4 vr = DRMP3_VLD(right + i);
1429 DRMP3_VSTORE(left + i, DRMP3_VADD(vl, vr));
1430 DRMP3_VSTORE(right + i, DRMP3_VSUB(vl, vr));
1431 }
1432 #ifdef __GNUC__
1433 /* Workaround for spurious -Waggressive-loop-optimizations warning from gcc.
1434 * For more info see: https://github.com/lieff/minimp3/issues/88
1435 */
1436 if (__builtin_constant_p(n % 4 == 0) && n % 4 == 0)
1437 return;
1438 #endif
1439 }
1440 #endif
1441 for (; i < n; i++)
1442 {
1443 float a = left[i];
1444 float b = right[i];
1445 left[i] = a + b;
1446 right[i] = a - b;
1447 }
1448 }
1449
1450 static void drmp3_L3_intensity_stereo_band(float *left, int n, float kl, float kr)
1451 {
1452 int i;
1453 for (i = 0; i < n; i++)
1454 {
1455 left[i + 576] = left[i]*kr;
1456 left[i] = left[i]*kl;
1457 }
1458 }
1459
1460 static void drmp3_L3_stereo_top_band(const float *right, const drmp3_uint8 *sfb, int nbands, int max_band[3])
1461 {
1462 int i, k;
1463
1464 max_band[0] = max_band[1] = max_band[2] = -1;
1465
1466 for (i = 0; i < nbands; i++)
1467 {
1468 for (k = 0; k < sfb[i]; k += 2)
1469 {
1470 if (right[k] != 0 || right[k + 1] != 0)
1471 {
1472 max_band[i % 3] = i;
1473 break;
1474 }
1475 }
1476 right += sfb[i];
1477 }
1478 }
1479
1480 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 {
1482 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;
1484
1485 for (i = 0; sfb[i]; i++)
1486 {
1487 unsigned ipos = ist_pos[i];
1488 if ((int)i > max_band[i % 3] && ipos < max_pos)
1489 {
1490 float kl, kr, s = DRMP3_HDR_TEST_MS_STEREO(hdr) ? 1.41421356f : 1;
1491 if (DRMP3_HDR_TEST_MPEG1(hdr))
1492 {
1493 kl = g_pan[2*ipos];
1494 kr = g_pan[2*ipos + 1];
1495 } else
1496 {
1497 kl = 1;
1498 kr = drmp3_L3_ldexp_q2(1, (ipos + 1) >> 1 << mpeg2_sh);
1499 if (ipos & 1)
1500 {
1501 kl = kr;
1502 kr = 1;
1503 }
1504 }
1505 drmp3_L3_intensity_stereo_band(left, sfb[i], kl*s, kr*s);
1506 } else if (DRMP3_HDR_TEST_MS_STEREO(hdr))
1507 {
1508 drmp3_L3_midside_stereo(left, sfb[i]);
1509 }
1510 left += sfb[i];
1511 }
1512 }
1513
1514 static void drmp3_L3_intensity_stereo(float *left, drmp3_uint8 *ist_pos, const drmp3_L3_gr_info *gr, const drmp3_uint8 *hdr)
1515 {
1516 int max_band[3], n_sfb = gr->n_long_sfb + gr->n_short_sfb;
1517 int i, max_blocks = gr->n_short_sfb ? 3 : 1;
1518
1519 drmp3_L3_stereo_top_band(left + 576, gr->sfbtab, n_sfb, max_band);
1520 if (gr->n_long_sfb)
1521 {
1522 max_band[0] = max_band[1] = max_band[2] = DRMP3_MAX(DRMP3_MAX(max_band[0], max_band[1]), max_band[2]);
1523 }
1524 for (i = 0; i < max_blocks; i++)
1525 {
1526 int default_pos = DRMP3_HDR_TEST_MPEG1(hdr) ? 3 : 0;
1527 int itop = n_sfb - max_blocks + i;
1528 int prev = itop - max_blocks;
1529 ist_pos[itop] = (drmp3_uint8)(max_band[i] >= prev ? default_pos : ist_pos[prev]);
1530 }
1531 drmp3_L3_stereo_process(left, ist_pos, gr->sfbtab, hdr, max_band, gr[1].scalefac_compress & 1);
1532 }
1533
1534 static void drmp3_L3_reorder(float *grbuf, float *scratch, const drmp3_uint8 *sfb)
1535 {
1536 int i, len;
1537 float *src = grbuf, *dst = scratch;
1538
1539 for (;0 != (len = *sfb); sfb += 3, src += 2*len)
1540 {
1541 for (i = 0; i < len; i++, src++)
1542 {
1543 *dst++ = src[0*len];
1544 *dst++ = src[1*len];
1545 *dst++ = src[2*len];
1546 }
1547 }
1548 DRMP3_COPY_MEMORY(grbuf, scratch, (dst - scratch)*sizeof(float));
1549 }
1550
1551 static void drmp3_L3_antialias(float *grbuf, int nbands)
1552 {
1553 static const float g_aa[2][8] = {
1554 {0.85749293f,0.88174200f,0.94962865f,0.98331459f,0.99551782f,0.99916056f,0.99989920f,0.99999316f},
1555 {0.51449576f,0.47173197f,0.31337745f,0.18191320f,0.09457419f,0.04096558f,0.01419856f,0.00369997f}
1556 };
1557
1558 for (; nbands > 0; nbands--, grbuf += 18)
1559 {
1560 int i = 0;
1561 #if DRMP3_HAVE_SIMD
1562 if (drmp3_have_simd()) for (; i < 8; i += 4)
1563 {
1564 drmp3_f4 vu = DRMP3_VLD(grbuf + 18 + i);
1565 drmp3_f4 vd = DRMP3_VLD(grbuf + 14 - i);
1566 drmp3_f4 vc0 = DRMP3_VLD(g_aa[0] + i);
1567 drmp3_f4 vc1 = DRMP3_VLD(g_aa[1] + i);
1568 vd = DRMP3_VREV(vd);
1569 DRMP3_VSTORE(grbuf + 18 + i, DRMP3_VSUB(DRMP3_VMUL(vu, vc0), DRMP3_VMUL(vd, vc1)));
1570 vd = DRMP3_VADD(DRMP3_VMUL(vu, vc1), DRMP3_VMUL(vd, vc0));
1571 DRMP3_VSTORE(grbuf + 14 - i, DRMP3_VREV(vd));
1572 }
1573 #endif
1574 #ifndef DR_MP3_ONLY_SIMD
1575 for(; i < 8; i++)
1576 {
1577 float u = grbuf[18 + i];
1578 float d = grbuf[17 - i];
1579 grbuf[18 + i] = u*g_aa[0][i] - d*g_aa[1][i];
1580 grbuf[17 - i] = u*g_aa[1][i] + d*g_aa[0][i];
1581 }
1582 #endif
1583 }
1584 }
1585
1586 static void drmp3_L3_dct3_9(float *y)
1587 {
1588 float s0, s1, s2, s3, s4, s5, s6, s7, s8, t0, t2, t4;
1589
1590 s0 = y[0]; s2 = y[2]; s4 = y[4]; s6 = y[6]; s8 = y[8];
1591 t0 = s0 + s6*0.5f;
1592 s0 -= s6;
1593 t4 = (s4 + s2)*0.93969262f;
1594 t2 = (s8 + s2)*0.76604444f;
1595 s6 = (s4 - s8)*0.17364818f;
1596 s4 += s8 - s2;
1597
1598 s2 = s0 - s4*0.5f;
1599 y[4] = s4 + s0;
1600 s8 = t0 - t2 + s6;
1601 s0 = t0 - t4 + t2;
1602 s4 = t0 + t4 - s6;
1603
1604 s1 = y[1]; s3 = y[3]; s5 = y[5]; s7 = y[7];
1605
1606 s3 *= 0.86602540f;
1607 t0 = (s5 + s1)*0.98480775f;
1608 t4 = (s5 - s7)*0.34202014f;
1609 t2 = (s1 + s7)*0.64278761f;
1610 s1 = (s1 - s5 - s7)*0.86602540f;
1611
1612 s5 = t0 - s3 - t2;
1613 s7 = t4 - s3 - t0;
1614 s3 = t4 + s3 - t2;
1615
1616 y[0] = s4 - s7;
1617 y[1] = s2 + s1;
1618 y[2] = s0 - s3;
1619 y[3] = s8 + s5;
1620 y[5] = s8 - s5;
1621 y[6] = s0 + s3;
1622 y[7] = s2 - s1;
1623 y[8] = s4 + s7;
1624 }
1625
1626 static void drmp3_L3_imdct36(float *grbuf, float *overlap, const float *window, int nbands)
1627 {
1628 int i, j;
1629 static const float g_twid9[18] = {
1630 0.73727734f,0.79335334f,0.84339145f,0.88701083f,0.92387953f,0.95371695f,0.97629601f,0.99144486f,0.99904822f,0.67559021f,0.60876143f,0.53729961f,0.46174861f,0.38268343f,0.30070580f,0.21643961f,0.13052619f,0.04361938f
1631 };
1632
1633 for (j = 0; j < nbands; j++, grbuf += 18, overlap += 9)
1634 {
1635 float co[9], si[9];
1636 co[0] = -grbuf[0];
1637 si[0] = grbuf[17];
1638 for (i = 0; i < 4; i++)
1639 {
1640 si[8 - 2*i] = grbuf[4*i + 1] - grbuf[4*i + 2];
1641 co[1 + 2*i] = grbuf[4*i + 1] + grbuf[4*i + 2];
1642 si[7 - 2*i] = grbuf[4*i + 4] - grbuf[4*i + 3];
1643 co[2 + 2*i] = -(grbuf[4*i + 3] + grbuf[4*i + 4]);
1644 }
1645 drmp3_L3_dct3_9(co);
1646 drmp3_L3_dct3_9(si);
1647
1648 si[1] = -si[1];
1649 si[3] = -si[3];
1650 si[5] = -si[5];
1651 si[7] = -si[7];
1652
1653 i = 0;
1654
1655 #if DRMP3_HAVE_SIMD
1656 if (drmp3_have_simd()) for (; i < 8; i += 4)
1657 {
1658 drmp3_f4 vovl = DRMP3_VLD(overlap + i);
1659 drmp3_f4 vc = DRMP3_VLD(co + i);
1660 drmp3_f4 vs = DRMP3_VLD(si + i);
1661 drmp3_f4 vr0 = DRMP3_VLD(g_twid9 + i);
1662 drmp3_f4 vr1 = DRMP3_VLD(g_twid9 + 9 + i);
1663 drmp3_f4 vw0 = DRMP3_VLD(window + i);
1664 drmp3_f4 vw1 = DRMP3_VLD(window + 9 + i);
1665 drmp3_f4 vsum = DRMP3_VADD(DRMP3_VMUL(vc, vr1), DRMP3_VMUL(vs, vr0));
1666 DRMP3_VSTORE(overlap + i, DRMP3_VSUB(DRMP3_VMUL(vc, vr0), DRMP3_VMUL(vs, vr1)));
1667 DRMP3_VSTORE(grbuf + i, DRMP3_VSUB(DRMP3_VMUL(vovl, vw0), DRMP3_VMUL(vsum, vw1)));
1668 vsum = DRMP3_VADD(DRMP3_VMUL(vovl, vw1), DRMP3_VMUL(vsum, vw0));
1669 DRMP3_VSTORE(grbuf + 14 - i, DRMP3_VREV(vsum));
1670 }
1671 #endif
1672 for (; i < 9; i++)
1673 {
1674 float ovl = overlap[i];
1675 float sum = co[i]*g_twid9[9 + i] + si[i]*g_twid9[0 + i];
1676 overlap[i] = co[i]*g_twid9[0 + i] - si[i]*g_twid9[9 + i];
1677 grbuf[i] = ovl*window[0 + i] - sum*window[9 + i];
1678 grbuf[17 - i] = ovl*window[9 + i] + sum*window[0 + i];
1679 }
1680 }
1681 }
1682
1683 static void drmp3_L3_idct3(float x0, float x1, float x2, float *dst)
1684 {
1685 float m1 = x1*0.86602540f;
1686 float a1 = x0 - x2*0.5f;
1687 dst[1] = x0 + x2;
1688 dst[0] = a1 + m1;
1689 dst[2] = a1 - m1;
1690 }
1691
1692 static void drmp3_L3_imdct12(float *x, float *dst, float *overlap)
1693 {
1694 static const float g_twid3[6] = { 0.79335334f,0.92387953f,0.99144486f, 0.60876143f,0.38268343f,0.13052619f };
1695 float co[3], si[3];
1696 int i;
1697
1698 drmp3_L3_idct3(-x[0], x[6] + x[3], x[12] + x[9], co);
1699 drmp3_L3_idct3(x[15], x[12] - x[9], x[6] - x[3], si);
1700 si[1] = -si[1];
1701
1702 for (i = 0; i < 3; i++)
1703 {
1704 float ovl = overlap[i];
1705 float sum = co[i]*g_twid3[3 + i] + si[i]*g_twid3[0 + i];
1706 overlap[i] = co[i]*g_twid3[0 + i] - si[i]*g_twid3[3 + i];
1707 dst[i] = ovl*g_twid3[2 - i] - sum*g_twid3[5 - i];
1708 dst[5 - i] = ovl*g_twid3[5 - i] + sum*g_twid3[2 - i];
1709 }
1710 }
1711
1712 static void drmp3_L3_imdct_short(float *grbuf, float *overlap, int nbands)
1713 {
1714 for (;nbands > 0; nbands--, overlap += 9, grbuf += 18)
1715 {
1716 float tmp[18];
1717 DRMP3_COPY_MEMORY(tmp, grbuf, sizeof(tmp));
1718 DRMP3_COPY_MEMORY(grbuf, overlap, 6*sizeof(float));
1719 drmp3_L3_imdct12(tmp, grbuf + 6, overlap + 6);
1720 drmp3_L3_imdct12(tmp + 1, grbuf + 12, overlap + 6);
1721 drmp3_L3_imdct12(tmp + 2, overlap, overlap + 6);
1722 }
1723 }
1724
1725 static void drmp3_L3_change_sign(float *grbuf)
1726 {
1727 int b, i;
1728 for (b = 0, grbuf += 18; b < 32; b += 2, grbuf += 36)
1729 for (i = 1; i < 18; i += 2)
1730 grbuf[i] = -grbuf[i];
1731 }
1732
1733 static void drmp3_L3_imdct_gr(float *grbuf, float *overlap, unsigned block_type, unsigned n_long_bands)
1734 {
1735 static const float g_mdct_window[2][18] = {
1736 { 0.99904822f,0.99144486f,0.97629601f,0.95371695f,0.92387953f,0.88701083f,0.84339145f,0.79335334f,0.73727734f,0.04361938f,0.13052619f,0.21643961f,0.30070580f,0.38268343f,0.46174861f,0.53729961f,0.60876143f,0.67559021f },
1737 { 1,1,1,1,1,1,0.99144486f,0.92387953f,0.79335334f,0,0,0,0,0,0,0.13052619f,0.38268343f,0.60876143f }
1738 };
1739 if (n_long_bands)
1740 {
1741 drmp3_L3_imdct36(grbuf, overlap, g_mdct_window[0], n_long_bands);
1742 grbuf += 18*n_long_bands;
1743 overlap += 9*n_long_bands;
1744 }
1745 if (block_type == DRMP3_SHORT_BLOCK_TYPE)
1746 drmp3_L3_imdct_short(grbuf, overlap, 32 - n_long_bands);
1747 else
1748 drmp3_L3_imdct36(grbuf, overlap, g_mdct_window[block_type == DRMP3_STOP_BLOCK_TYPE], 32 - n_long_bands);
1749 }
1750
1751 static void drmp3_L3_save_reservoir(drmp3dec *h, drmp3dec_scratch *s)
1752 {
1753 int pos = (s->bs.pos + 7)/8u;
1754 int remains = s->bs.limit/8u - pos;
1755 if (remains > DRMP3_MAX_BITRESERVOIR_BYTES)
1756 {
1757 pos += remains - DRMP3_MAX_BITRESERVOIR_BYTES;
1758 remains = DRMP3_MAX_BITRESERVOIR_BYTES;
1759 }
1760 if (remains > 0)
1761 {
1762 DRMP3_MOVE_MEMORY(h->reserv_buf, s->maindata + pos, remains);
1763 }
1764 h->reserv = remains;
1765 }
1766
1767 static int drmp3_L3_restore_reservoir(drmp3dec *h, drmp3_bs *bs, drmp3dec_scratch *s, int main_data_begin)
1768 {
1769 int frame_bytes = (bs->limit - bs->pos)/8;
1770 int bytes_have = DRMP3_MIN(h->reserv, main_data_begin);
1771 DRMP3_COPY_MEMORY(s->maindata, h->reserv_buf + DRMP3_MAX(0, h->reserv - main_data_begin), DRMP3_MIN(h->reserv, main_data_begin));
1772 DRMP3_COPY_MEMORY(s->maindata + bytes_have, bs->buf + bs->pos/8, frame_bytes);
1773 drmp3_bs_init(&s->bs, s->maindata, bytes_have + frame_bytes);
1774 return h->reserv >= main_data_begin;
1775 }
1776
1777 static void drmp3_L3_decode(drmp3dec *h, drmp3dec_scratch *s, drmp3_L3_gr_info *gr_info, int nch)
1778 {
1779 int ch;
1780
1781 for (ch = 0; ch < nch; ch++)
1782 {
1783 int layer3gr_limit = s->bs.pos + gr_info[ch].part_23_length;
1784 drmp3_L3_decode_scalefactors(h->header, s->ist_pos[ch], &s->bs, gr_info + ch, s->scf, ch);
1785 drmp3_L3_huffman(s->grbuf[ch], &s->bs, gr_info + ch, s->scf, layer3gr_limit);
1786 }
1787
1788 if (DRMP3_HDR_TEST_I_STEREO(h->header))
1789 {
1790 drmp3_L3_intensity_stereo(s->grbuf[0], s->ist_pos[1], gr_info, h->header);
1791 } else if (DRMP3_HDR_IS_MS_STEREO(h->header))
1792 {
1793 drmp3_L3_midside_stereo(s->grbuf[0], 576);
1794 }
1795
1796 for (ch = 0; ch < nch; ch++, gr_info++)
1797 {
1798 int aa_bands = 31;
1799 int n_long_bands = (gr_info->mixed_block_flag ? 2 : 0) << (int)(DRMP3_HDR_GET_MY_SAMPLE_RATE(h->header) == 2);
1800
1801 if (gr_info->n_short_sfb)
1802 {
1803 aa_bands = n_long_bands - 1;
1804 drmp3_L3_reorder(s->grbuf[ch] + n_long_bands*18, s->syn[0], gr_info->sfbtab + gr_info->n_long_sfb);
1805 }
1806
1807 drmp3_L3_antialias(s->grbuf[ch], aa_bands);
1808 drmp3_L3_imdct_gr(s->grbuf[ch], h->mdct_overlap[ch], gr_info->block_type, n_long_bands);
1809 drmp3_L3_change_sign(s->grbuf[ch]);
1810 }
1811 }
1812
1813 static void drmp3d_DCT_II(float *grbuf, int n)
1814 {
1815 static const float g_sec[24] = {
1816 10.19000816f,0.50060302f,0.50241929f,3.40760851f,0.50547093f,0.52249861f,2.05778098f,0.51544732f,0.56694406f,1.48416460f,0.53104258f,0.64682180f,1.16943991f,0.55310392f,0.78815460f,0.97256821f,0.58293498f,1.06067765f,0.83934963f,0.62250412f,1.72244716f,0.74453628f,0.67480832f,5.10114861f
1817 };
1818 int i, k = 0;
1819 #if DRMP3_HAVE_SIMD
1820 if (drmp3_have_simd()) for (; k < n; k += 4)
1821 {
1822 drmp3_f4 t[4][8], *x;
1823 float *y = grbuf + k;
1824
1825 for (x = t[0], i = 0; i < 8; i++, x++)
1826 {
1827 drmp3_f4 x0 = DRMP3_VLD(&y[i*18]);
1828 drmp3_f4 x1 = DRMP3_VLD(&y[(15 - i)*18]);
1829 drmp3_f4 x2 = DRMP3_VLD(&y[(16 + i)*18]);
1830 drmp3_f4 x3 = DRMP3_VLD(&y[(31 - i)*18]);
1831 drmp3_f4 t0 = DRMP3_VADD(x0, x3);
1832 drmp3_f4 t1 = DRMP3_VADD(x1, x2);
1833 drmp3_f4 t2 = DRMP3_VMUL_S(DRMP3_VSUB(x1, x2), g_sec[3*i + 0]);
1834 drmp3_f4 t3 = DRMP3_VMUL_S(DRMP3_VSUB(x0, x3), g_sec[3*i + 1]);
1835 x[0] = DRMP3_VADD(t0, t1);
1836 x[8] = DRMP3_VMUL_S(DRMP3_VSUB(t0, t1), g_sec[3*i + 2]);
1837 x[16] = DRMP3_VADD(t3, t2);
1838 x[24] = DRMP3_VMUL_S(DRMP3_VSUB(t3, t2), g_sec[3*i + 2]);
1839 }
1840 for (x = t[0], i = 0; i < 4; i++, x += 8)
1841 {
1842 drmp3_f4 x0 = x[0], x1 = x[1], x2 = x[2], x3 = x[3], x4 = x[4], x5 = x[5], x6 = x[6], x7 = x[7], xt;
1843 xt = DRMP3_VSUB(x0, x7); x0 = DRMP3_VADD(x0, x7);
1844 x7 = DRMP3_VSUB(x1, x6); x1 = DRMP3_VADD(x1, x6);
1845 x6 = DRMP3_VSUB(x2, x5); x2 = DRMP3_VADD(x2, x5);
1846 x5 = DRMP3_VSUB(x3, x4); x3 = DRMP3_VADD(x3, x4);
1847 x4 = DRMP3_VSUB(x0, x3); x0 = DRMP3_VADD(x0, x3);
1848 x3 = DRMP3_VSUB(x1, x2); x1 = DRMP3_VADD(x1, x2);
1849 x[0] = DRMP3_VADD(x0, x1);
1850 x[4] = DRMP3_VMUL_S(DRMP3_VSUB(x0, x1), 0.70710677f);
1851 x5 = DRMP3_VADD(x5, x6);
1852 x6 = DRMP3_VMUL_S(DRMP3_VADD(x6, x7), 0.70710677f);
1853 x7 = DRMP3_VADD(x7, xt);
1854 x3 = DRMP3_VMUL_S(DRMP3_VADD(x3, x4), 0.70710677f);
1855 x5 = DRMP3_VSUB(x5, DRMP3_VMUL_S(x7, 0.198912367f)); /* rotate by PI/8 */
1856 x7 = DRMP3_VADD(x7, DRMP3_VMUL_S(x5, 0.382683432f));
1857 x5 = DRMP3_VSUB(x5, DRMP3_VMUL_S(x7, 0.198912367f));
1858 x0 = DRMP3_VSUB(xt, x6); xt = DRMP3_VADD(xt, x6);
1859 x[1] = DRMP3_VMUL_S(DRMP3_VADD(xt, x7), 0.50979561f);
1860 x[2] = DRMP3_VMUL_S(DRMP3_VADD(x4, x3), 0.54119611f);
1861 x[3] = DRMP3_VMUL_S(DRMP3_VSUB(x0, x5), 0.60134488f);
1862 x[5] = DRMP3_VMUL_S(DRMP3_VADD(x0, x5), 0.89997619f);
1863 x[6] = DRMP3_VMUL_S(DRMP3_VSUB(x4, x3), 1.30656302f);
1864 x[7] = DRMP3_VMUL_S(DRMP3_VSUB(xt, x7), 2.56291556f);
1865 }
1866
1867 if (k > n - 3)
1868 {
1869 #if DRMP3_HAVE_SSE
1870 #define DRMP3_VSAVE2(i, v) _mm_storel_pi((__m64 *)(void*)&y[i*18], v)
1871 #else
1872 #define DRMP3_VSAVE2(i, v) vst1_f32((float32_t *)&y[(i)*18], vget_low_f32(v))
1873 #endif
1874 for (i = 0; i < 7; i++, y += 4*18)
1875 {
1876 drmp3_f4 s = DRMP3_VADD(t[3][i], t[3][i + 1]);
1877 DRMP3_VSAVE2(0, t[0][i]);
1878 DRMP3_VSAVE2(1, DRMP3_VADD(t[2][i], s));
1879 DRMP3_VSAVE2(2, DRMP3_VADD(t[1][i], t[1][i + 1]));
1880 DRMP3_VSAVE2(3, DRMP3_VADD(t[2][1 + i], s));
1881 }
1882 DRMP3_VSAVE2(0, t[0][7]);
1883 DRMP3_VSAVE2(1, DRMP3_VADD(t[2][7], t[3][7]));
1884 DRMP3_VSAVE2(2, t[1][7]);
1885 DRMP3_VSAVE2(3, t[3][7]);
1886 } else
1887 {
1888 #define DRMP3_VSAVE4(i, v) DRMP3_VSTORE(&y[(i)*18], v)
1889 for (i = 0; i < 7; i++, y += 4*18)
1890 {
1891 drmp3_f4 s = DRMP3_VADD(t[3][i], t[3][i + 1]);
1892 DRMP3_VSAVE4(0, t[0][i]);
1893 DRMP3_VSAVE4(1, DRMP3_VADD(t[2][i], s));
1894 DRMP3_VSAVE4(2, DRMP3_VADD(t[1][i], t[1][i + 1]));
1895 DRMP3_VSAVE4(3, DRMP3_VADD(t[2][1 + i], s));
1896 }
1897 DRMP3_VSAVE4(0, t[0][7]);
1898 DRMP3_VSAVE4(1, DRMP3_VADD(t[2][7], t[3][7]));
1899 DRMP3_VSAVE4(2, t[1][7]);
1900 DRMP3_VSAVE4(3, t[3][7]);
1901 }
1902 } else
1903 #endif
1904 #ifdef DR_MP3_ONLY_SIMD
1905 {} /* for HAVE_SIMD=1, MINIMP3_ONLY_SIMD=1 case we do not need non-intrinsic "else" branch */
1906 #else
1907 for (; k < n; k++)
1908 {
1909 float t[4][8], *x, *y = grbuf + k;
1910
1911 for (x = t[0], i = 0; i < 8; i++, x++)
1912 {
1913 float x0 = y[i*18];
1914 float x1 = y[(15 - i)*18];
1915 float x2 = y[(16 + i)*18];
1916 float x3 = y[(31 - i)*18];
1917 float t0 = x0 + x3;
1918 float t1 = x1 + x2;
1919 float t2 = (x1 - x2)*g_sec[3*i + 0];
1920 float t3 = (x0 - x3)*g_sec[3*i + 1];
1921 x[0] = t0 + t1;
1922 x[8] = (t0 - t1)*g_sec[3*i + 2];
1923 x[16] = t3 + t2;
1924 x[24] = (t3 - t2)*g_sec[3*i + 2];
1925 }
1926 for (x = t[0], i = 0; i < 4; i++, x += 8)
1927 {
1928 float x0 = x[0], x1 = x[1], x2 = x[2], x3 = x[3], x4 = x[4], x5 = x[5], x6 = x[6], x7 = x[7], xt;
1929 xt = x0 - x7; x0 += x7;
1930 x7 = x1 - x6; x1 += x6;
1931 x6 = x2 - x5; x2 += x5;
1932 x5 = x3 - x4; x3 += x4;
1933 x4 = x0 - x3; x0 += x3;
1934 x3 = x1 - x2; x1 += x2;
1935 x[0] = x0 + x1;
1936 x[4] = (x0 - x1)*0.70710677f;
1937 x5 = x5 + x6;
1938 x6 = (x6 + x7)*0.70710677f;
1939 x7 = x7 + xt;
1940 x3 = (x3 + x4)*0.70710677f;
1941 x5 -= x7*0.198912367f; /* rotate by PI/8 */
1942 x7 += x5*0.382683432f;
1943 x5 -= x7*0.198912367f;
1944 x0 = xt - x6; xt += x6;
1945 x[1] = (xt + x7)*0.50979561f;
1946 x[2] = (x4 + x3)*0.54119611f;
1947 x[3] = (x0 - x5)*0.60134488f;
1948 x[5] = (x0 + x5)*0.89997619f;
1949 x[6] = (x4 - x3)*1.30656302f;
1950 x[7] = (xt - x7)*2.56291556f;
1951
1952 }
1953 for (i = 0; i < 7; i++, y += 4*18)
1954 {
1955 y[0*18] = t[0][i];
1956 y[1*18] = t[2][i] + t[3][i] + t[3][i + 1];
1957 y[2*18] = t[1][i] + t[1][i + 1];
1958 y[3*18] = t[2][i + 1] + t[3][i] + t[3][i + 1];
1959 }
1960 y[0*18] = t[0][7];
1961 y[1*18] = t[2][7] + t[3][7];
1962 y[2*18] = t[1][7];
1963 y[3*18] = t[3][7];
1964 }
1965 #endif
1966 }
1967
1968 #ifndef DR_MP3_FLOAT_OUTPUT
1969 typedef drmp3_int16 drmp3d_sample_t;
1970
1971 static drmp3_int16 drmp3d_scale_pcm(float sample)
1972 {
1973 drmp3_int16 s;
1974 #if DRMP3_HAVE_ARMV6
1975 drmp3_int32 s32 = (drmp3_int32)(sample + .5f);
1976 s32 -= (s32 < 0);
1977 s = (drmp3_int16)drmp3_clip_int16_arm(s32);
1978 #else
1979 if (sample >= 32766.5) return (drmp3_int16) 32767;
1980 if (sample <= -32767.5) return (drmp3_int16)-32768;
1981 s = (drmp3_int16)(sample + .5f);
1982 s -= (s < 0); /* away from zero, to be compliant */
1983 #endif
1984 return s;
1985 }
1986 #else
1987 typedef float drmp3d_sample_t;
1988
1989 static float drmp3d_scale_pcm(float sample)
1990 {
1991 return sample*(1.f/32768.f);
1992 }
1993 #endif
1994
1995 static void drmp3d_synth_pair(drmp3d_sample_t *pcm, int nch, const float *z)
1996 {
1997 float a;
1998 a = (z[14*64] - z[ 0]) * 29;
1999 a += (z[ 1*64] + z[13*64]) * 213;
2000 a += (z[12*64] - z[ 2*64]) * 459;
2001 a += (z[ 3*64] + z[11*64]) * 2037;
2002 a += (z[10*64] - z[ 4*64]) * 5153;
2003 a += (z[ 5*64] + z[ 9*64]) * 6574;
2004 a += (z[ 8*64] - z[ 6*64]) * 37489;
2005 a += z[ 7*64] * 75038;
2006 pcm[0] = drmp3d_scale_pcm(a);
2007
2008 z += 2;
2009 a = z[14*64] * 104;
2010 a += z[12*64] * 1567;
2011 a += z[10*64] * 9727;
2012 a += z[ 8*64] * 64019;
2013 a += z[ 6*64] * -9975;
2014 a += z[ 4*64] * -45;
2015 a += z[ 2*64] * 146;
2016 a += z[ 0*64] * -5;
2017 pcm[16*nch] = drmp3d_scale_pcm(a);
2018 }
2019
2020 static void drmp3d_synth(float *xl, drmp3d_sample_t *dstl, int nch, float *lins)
2021 {
2022 int i;
2023 float *xr = xl + 576*(nch - 1);
2024 drmp3d_sample_t *dstr = dstl + (nch - 1);
2025
2026 static const float g_win[] = {
2027 -1,26,-31,208,218,401,-519,2063,2000,4788,-5517,7134,5959,35640,-39336,74992,
2028 -1,24,-35,202,222,347,-581,2080,1952,4425,-5879,7640,5288,33791,-41176,74856,
2029 -1,21,-38,196,225,294,-645,2087,1893,4063,-6237,8092,4561,31947,-43006,74630,
2030 -1,19,-41,190,227,244,-711,2085,1822,3705,-6589,8492,3776,30112,-44821,74313,
2031 -1,17,-45,183,228,197,-779,2075,1739,3351,-6935,8840,2935,28289,-46617,73908,
2032 -1,16,-49,176,228,153,-848,2057,1644,3004,-7271,9139,2037,26482,-48390,73415,
2033 -2,14,-53,169,227,111,-919,2032,1535,2663,-7597,9389,1082,24694,-50137,72835,
2034 -2,13,-58,161,224,72,-991,2001,1414,2330,-7910,9592,70,22929,-51853,72169,
2035 -2,11,-63,154,221,36,-1064,1962,1280,2006,-8209,9750,-998,21189,-53534,71420,
2036 -2,10,-68,147,215,2,-1137,1919,1131,1692,-8491,9863,-2122,19478,-55178,70590,
2037 -3,9,-73,139,208,-29,-1210,1870,970,1388,-8755,9935,-3300,17799,-56778,69679,
2038 -3,8,-79,132,200,-57,-1283,1817,794,1095,-8998,9966,-4533,16155,-58333,68692,
2039 -4,7,-85,125,189,-83,-1356,1759,605,814,-9219,9959,-5818,14548,-59838,67629,
2040 -4,7,-91,117,177,-106,-1428,1698,402,545,-9416,9916,-7154,12980,-61289,66494,
2041 -5,6,-97,111,163,-127,-1498,1634,185,288,-9585,9838,-8540,11455,-62684,65290
2042 };
2043 float *zlin = lins + 15*64;
2044 const float *w = g_win;
2045
2046 zlin[4*15] = xl[18*16];
2047 zlin[4*15 + 1] = xr[18*16];
2048 zlin[4*15 + 2] = xl[0];
2049 zlin[4*15 + 3] = xr[0];
2050
2051 zlin[4*31] = xl[1 + 18*16];
2052 zlin[4*31 + 1] = xr[1 + 18*16];
2053 zlin[4*31 + 2] = xl[1];
2054 zlin[4*31 + 3] = xr[1];
2055
2056 drmp3d_synth_pair(dstr, nch, lins + 4*15 + 1);
2057 drmp3d_synth_pair(dstr + 32*nch, nch, lins + 4*15 + 64 + 1);
2058 drmp3d_synth_pair(dstl, nch, lins + 4*15);
2059 drmp3d_synth_pair(dstl + 32*nch, nch, lins + 4*15 + 64);
2060
2061 #if DRMP3_HAVE_SIMD
2062 if (drmp3_have_simd()) for (i = 14; i >= 0; i--)
2063 {
2064 #define DRMP3_VLOAD(k) drmp3_f4 w0 = DRMP3_VSET(*w++); drmp3_f4 w1 = DRMP3_VSET(*w++); drmp3_f4 vz = DRMP3_VLD(&zlin[4*i - 64*k]); drmp3_f4 vy = DRMP3_VLD(&zlin[4*i - 64*(15 - k)]);
2065 #define DRMP3_V0(k) { DRMP3_VLOAD(k) b = DRMP3_VADD(DRMP3_VMUL(vz, w1), DRMP3_VMUL(vy, w0)) ; a = DRMP3_VSUB(DRMP3_VMUL(vz, w0), DRMP3_VMUL(vy, w1)); }
2066 #define DRMP3_V1(k) { DRMP3_VLOAD(k) b = DRMP3_VADD(b, DRMP3_VADD(DRMP3_VMUL(vz, w1), DRMP3_VMUL(vy, w0))); a = DRMP3_VADD(a, DRMP3_VSUB(DRMP3_VMUL(vz, w0), DRMP3_VMUL(vy, w1))); }
2067 #define DRMP3_V2(k) { DRMP3_VLOAD(k) b = DRMP3_VADD(b, DRMP3_VADD(DRMP3_VMUL(vz, w1), DRMP3_VMUL(vy, w0))); a = DRMP3_VADD(a, DRMP3_VSUB(DRMP3_VMUL(vy, w1), DRMP3_VMUL(vz, w0))); }
2068 drmp3_f4 a, b;
2069 zlin[4*i] = xl[18*(31 - i)];
2070 zlin[4*i + 1] = xr[18*(31 - i)];
2071 zlin[4*i + 2] = xl[1 + 18*(31 - i)];
2072 zlin[4*i + 3] = xr[1 + 18*(31 - i)];
2073 zlin[4*i + 64] = xl[1 + 18*(1 + i)];
2074 zlin[4*i + 64 + 1] = xr[1 + 18*(1 + i)];
2075 zlin[4*i - 64 + 2] = xl[18*(1 + i)];
2076 zlin[4*i - 64 + 3] = xr[18*(1 + i)];
2077
2078 DRMP3_V0(0) DRMP3_V2(1) DRMP3_V1(2) DRMP3_V2(3) DRMP3_V1(4) DRMP3_V2(5) DRMP3_V1(6) DRMP3_V2(7)
2079
2080 {
2081 #ifndef DR_MP3_FLOAT_OUTPUT
2082 #if DRMP3_HAVE_SSE
2083 static const drmp3_f4 g_max = { 32767.0f, 32767.0f, 32767.0f, 32767.0f };
2084 static const drmp3_f4 g_min = { -32768.0f, -32768.0f, -32768.0f, -32768.0f };
2085 __m128i pcm8 = _mm_packs_epi32(_mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(a, g_max), g_min)),
2086 _mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(b, g_max), g_min)));
2087 dstr[(15 - i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 1);
2088 dstr[(17 + i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 5);
2089 dstl[(15 - i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 0);
2090 dstl[(17 + i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 4);
2091 dstr[(47 - i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 3);
2092 dstr[(49 + i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 7);
2093 dstl[(47 - i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 2);
2094 dstl[(49 + i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 6);
2095 #else
2096 int16x4_t pcma, pcmb;
2097 a = DRMP3_VADD(a, DRMP3_VSET(0.5f));
2098 b = DRMP3_VADD(b, DRMP3_VSET(0.5f));
2099 pcma = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(a), vreinterpretq_s32_u32(vcltq_f32(a, DRMP3_VSET(0)))));
2100 pcmb = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(b), vreinterpretq_s32_u32(vcltq_f32(b, DRMP3_VSET(0)))));
2101 vst1_lane_s16(dstr + (15 - i)*nch, pcma, 1);
2102 vst1_lane_s16(dstr + (17 + i)*nch, pcmb, 1);
2103 vst1_lane_s16(dstl + (15 - i)*nch, pcma, 0);
2104 vst1_lane_s16(dstl + (17 + i)*nch, pcmb, 0);
2105 vst1_lane_s16(dstr + (47 - i)*nch, pcma, 3);
2106 vst1_lane_s16(dstr + (49 + i)*nch, pcmb, 3);
2107 vst1_lane_s16(dstl + (47 - i)*nch, pcma, 2);
2108 vst1_lane_s16(dstl + (49 + i)*nch, pcmb, 2);
2109 #endif
2110 #else
2111 #if DRMP3_HAVE_SSE
2112 static const drmp3_f4 g_scale = { 1.0f/32768.0f, 1.0f/32768.0f, 1.0f/32768.0f, 1.0f/32768.0f };
2113 #else
2114 const drmp3_f4 g_scale = vdupq_n_f32(1.0f/32768.0f);
2115 #endif
2116 a = DRMP3_VMUL(a, g_scale);
2117 b = DRMP3_VMUL(b, g_scale);
2118 #if DRMP3_HAVE_SSE
2119 _mm_store_ss(dstr + (15 - i)*nch, _mm_shuffle_ps(a, a, _MM_SHUFFLE(1, 1, 1, 1)));
2120 _mm_store_ss(dstr + (17 + i)*nch, _mm_shuffle_ps(b, b, _MM_SHUFFLE(1, 1, 1, 1)));
2121 _mm_store_ss(dstl + (15 - i)*nch, _mm_shuffle_ps(a, a, _MM_SHUFFLE(0, 0, 0, 0)));
2122 _mm_store_ss(dstl + (17 + i)*nch, _mm_shuffle_ps(b, b, _MM_SHUFFLE(0, 0, 0, 0)));
2123 _mm_store_ss(dstr + (47 - i)*nch, _mm_shuffle_ps(a, a, _MM_SHUFFLE(3, 3, 3, 3)));
2124 _mm_store_ss(dstr + (49 + i)*nch, _mm_shuffle_ps(b, b, _MM_SHUFFLE(3, 3, 3, 3)));
2125 _mm_store_ss(dstl + (47 - i)*nch, _mm_shuffle_ps(a, a, _MM_SHUFFLE(2, 2, 2, 2)));
2126 _mm_store_ss(dstl + (49 + i)*nch, _mm_shuffle_ps(b, b, _MM_SHUFFLE(2, 2, 2, 2)));
2127 #else
2128 vst1q_lane_f32(dstr + (15 - i)*nch, a, 1);
2129 vst1q_lane_f32(dstr + (17 + i)*nch, b, 1);
2130 vst1q_lane_f32(dstl + (15 - i)*nch, a, 0);
2131 vst1q_lane_f32(dstl + (17 + i)*nch, b, 0);
2132 vst1q_lane_f32(dstr + (47 - i)*nch, a, 3);
2133 vst1q_lane_f32(dstr + (49 + i)*nch, b, 3);
2134 vst1q_lane_f32(dstl + (47 - i)*nch, a, 2);
2135 vst1q_lane_f32(dstl + (49 + i)*nch, b, 2);
2136 #endif
2137 #endif /* DR_MP3_FLOAT_OUTPUT */
2138 }
2139 } else
2140 #endif
2141 #ifdef DR_MP3_ONLY_SIMD
2142 {} /* for HAVE_SIMD=1, MINIMP3_ONLY_SIMD=1 case we do not need non-intrinsic "else" branch */
2143 #else
2144 for (i = 14; i >= 0; i--)
2145 {
2146 #define DRMP3_LOAD(k) float w0 = *w++; float w1 = *w++; float *vz = &zlin[4*i - k*64]; float *vy = &zlin[4*i - (15 - k)*64];
2147 #define DRMP3_S0(k) { int j; DRMP3_LOAD(k); for (j = 0; j < 4; j++) b[j] = vz[j]*w1 + vy[j]*w0, a[j] = vz[j]*w0 - vy[j]*w1; }
2148 #define DRMP3_S1(k) { int j; DRMP3_LOAD(k); for (j = 0; j < 4; j++) b[j] += vz[j]*w1 + vy[j]*w0, a[j] += vz[j]*w0 - vy[j]*w1; }
2149 #define DRMP3_S2(k) { int j; DRMP3_LOAD(k); for (j = 0; j < 4; j++) b[j] += vz[j]*w1 + vy[j]*w0, a[j] += vy[j]*w1 - vz[j]*w0; }
2150 float a[4], b[4];
2151
2152 zlin[4*i] = xl[18*(31 - i)];
2153 zlin[4*i + 1] = xr[18*(31 - i)];
2154 zlin[4*i + 2] = xl[1 + 18*(31 - i)];
2155 zlin[4*i + 3] = xr[1 + 18*(31 - i)];
2156 zlin[4*(i + 16)] = xl[1 + 18*(1 + i)];
2157 zlin[4*(i + 16) + 1] = xr[1 + 18*(1 + i)];
2158 zlin[4*(i - 16) + 2] = xl[18*(1 + i)];
2159 zlin[4*(i - 16) + 3] = xr[18*(1 + i)];
2160
2161 DRMP3_S0(0) DRMP3_S2(1) DRMP3_S1(2) DRMP3_S2(3) DRMP3_S1(4) DRMP3_S2(5) DRMP3_S1(6) DRMP3_S2(7)
2162
2163 dstr[(15 - i)*nch] = drmp3d_scale_pcm(a[1]);
2164 dstr[(17 + i)*nch] = drmp3d_scale_pcm(b[1]);
2165 dstl[(15 - i)*nch] = drmp3d_scale_pcm(a[0]);
2166 dstl[(17 + i)*nch] = drmp3d_scale_pcm(b[0]);
2167 dstr[(47 - i)*nch] = drmp3d_scale_pcm(a[3]);
2168 dstr[(49 + i)*nch] = drmp3d_scale_pcm(b[3]);
2169 dstl[(47 - i)*nch] = drmp3d_scale_pcm(a[2]);
2170 dstl[(49 + i)*nch] = drmp3d_scale_pcm(b[2]);
2171 }
2172 #endif
2173 }
2174
2175 static void drmp3d_synth_granule(float *qmf_state, float *grbuf, int nbands, int nch, drmp3d_sample_t *pcm, float *lins)
2176 {
2177 int i;
2178 for (i = 0; i < nch; i++)
2179 {
2180 drmp3d_DCT_II(grbuf + 576*i, nbands);
2181 }
2182
2183 DRMP3_COPY_MEMORY(lins, qmf_state, sizeof(float)*15*64);
2184
2185 for (i = 0; i < nbands; i += 2)
2186 {
2187 drmp3d_synth(grbuf + i, pcm + 32*nch*i, nch, lins + i*64);
2188 }
2189 #ifndef DR_MP3_NONSTANDARD_BUT_LOGICAL
2190 if (nch == 1)
2191 {
2192 for (i = 0; i < 15*64; i += 2)
2193 {
2194 qmf_state[i] = lins[nbands*64 + i];
2195 }
2196 } else
2197 #endif
2198 {
2199 DRMP3_COPY_MEMORY(qmf_state, lins + nbands*64, sizeof(float)*15*64);
2200 }
2201 }
2202
2203 static int drmp3d_match_frame(const drmp3_uint8 *hdr, int mp3_bytes, int frame_bytes)
2204 {
2205 int i, nmatch;
2206 for (i = 0, nmatch = 0; nmatch < DRMP3_MAX_FRAME_SYNC_MATCHES; nmatch++)
2207 {
2208 i += drmp3_hdr_frame_bytes(hdr + i, frame_bytes) + drmp3_hdr_padding(hdr + i);
2209 if (i + DRMP3_HDR_SIZE > mp3_bytes)
2210 return nmatch > 0;
2211 if (!drmp3_hdr_compare(hdr, hdr + i))
2212 return 0;
2213 }
2214 return 1;
2215 }
2216
2217 static int drmp3d_find_frame(const drmp3_uint8 *mp3, int mp3_bytes, int *free_format_bytes, int *ptr_frame_bytes)
2218 {
2219 int i, k;
2220 for (i = 0; i < mp3_bytes - DRMP3_HDR_SIZE; i++, mp3++)
2221 {
2222 if (drmp3_hdr_valid(mp3))
2223 {
2224 int frame_bytes = drmp3_hdr_frame_bytes(mp3, *free_format_bytes);
2225 int frame_and_padding = frame_bytes + drmp3_hdr_padding(mp3);
2226
2227 for (k = DRMP3_HDR_SIZE; !frame_bytes && k < DRMP3_MAX_FREE_FORMAT_FRAME_SIZE && i + 2*k < mp3_bytes - DRMP3_HDR_SIZE; k++)
2228 {
2229 if (drmp3_hdr_compare(mp3, mp3 + k))
2230 {
2231 int fb = k - drmp3_hdr_padding(mp3);
2232 int nextfb = fb + drmp3_hdr_padding(mp3 + k);
2233 if (i + k + nextfb + DRMP3_HDR_SIZE > mp3_bytes || !drmp3_hdr_compare(mp3, mp3 + k + nextfb))
2234 continue;
2235 frame_and_padding = k;
2236 frame_bytes = fb;
2237 *free_format_bytes = fb;
2238 }
2239 }
2240
2241 if ((frame_bytes && i + frame_and_padding <= mp3_bytes &&
2242 drmp3d_match_frame(mp3, mp3_bytes - i, frame_bytes)) ||
2243 (!i && frame_and_padding == mp3_bytes))
2244 {
2245 *ptr_frame_bytes = frame_and_padding;
2246 return i;
2247 }
2248 *free_format_bytes = 0;
2249 }
2250 }
2251 *ptr_frame_bytes = 0;
2252 return mp3_bytes;
2253 }
2254
2255 DRMP3_API void drmp3dec_init(drmp3dec *dec)
2256 {
2257 dec->header[0] = 0;
2258 }
2259
2260 DRMP3_API int drmp3dec_decode_frame(drmp3dec *dec, const drmp3_uint8 *mp3, int mp3_bytes, void *pcm, drmp3dec_frame_info *info)
2261 {
2262 int i = 0, igr, frame_size = 0, success = 1;
2263 const drmp3_uint8 *hdr;
2264 drmp3_bs bs_frame[1];
2265 drmp3dec_scratch scratch;
2266
2267 if (mp3_bytes > 4 && dec->header[0] == 0xff && drmp3_hdr_compare(dec->header, mp3))
2268 {
2269 frame_size = drmp3_hdr_frame_bytes(mp3, dec->free_format_bytes) + drmp3_hdr_padding(mp3);
2270 if (frame_size != mp3_bytes && (frame_size + DRMP3_HDR_SIZE > mp3_bytes || !drmp3_hdr_compare(mp3, mp3 + frame_size)))
2271 {
2272 frame_size = 0;
2273 }
2274 }
2275 if (!frame_size)
2276 {
2277 DRMP3_ZERO_MEMORY(dec, sizeof(drmp3dec));
2278 i = drmp3d_find_frame(mp3, mp3_bytes, &dec->free_format_bytes, &frame_size);
2279 if (!frame_size || i + frame_size > mp3_bytes)
2280 {
2281 info->frame_bytes = i;
2282 return 0;
2283 }
2284 }
2285
2286 hdr = mp3 + i;
2287 DRMP3_COPY_MEMORY(dec->header, hdr, DRMP3_HDR_SIZE);
2288 info->frame_bytes = i + frame_size;
2289 info->channels = DRMP3_HDR_IS_MONO(hdr) ? 1 : 2;
2290 info->hz = drmp3_hdr_sample_rate_hz(hdr);
2291 info->layer = 4 - DRMP3_HDR_GET_LAYER(hdr);
2292 info->bitrate_kbps = drmp3_hdr_bitrate_kbps(hdr);
2293
2294 drmp3_bs_init(bs_frame, hdr + DRMP3_HDR_SIZE, frame_size - DRMP3_HDR_SIZE);
2295 if (DRMP3_HDR_IS_CRC(hdr))
2296 {
2297 drmp3_bs_get_bits(bs_frame, 16);
2298 }
2299
2300 if (info->layer == 3)
2301 {
2302 int main_data_begin = drmp3_L3_read_side_info(bs_frame, scratch.gr_info, hdr);
2303 if (main_data_begin < 0 || bs_frame->pos > bs_frame->limit)
2304 {
2305 drmp3dec_init(dec);
2306 return 0;
2307 }
2308 success = drmp3_L3_restore_reservoir(dec, bs_frame, &scratch, main_data_begin);
2309 if (success && pcm != NULL)
2310 {
2311 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 {
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]);
2316 }
2317 }
2318 drmp3_L3_save_reservoir(dec, &scratch);
2319 } else
2320 {
2321 #ifdef DR_MP3_ONLY_MP3
2322 return 0;
2323 #else
2324 drmp3_L12_scale_info sci[1];
2325
2326 if (pcm == NULL) {
2327 return drmp3_hdr_frame_samples(hdr);
2328 }
2329
2330 drmp3_L12_read_scale_info(hdr, bs_frame, sci);
2331
2332 DRMP3_ZERO_MEMORY(scratch.grbuf[0], 576*2*sizeof(float));
2333 for (i = 0, igr = 0; igr < 3; igr++)
2334 {
2335 if (12 == (i += drmp3_L12_dequantize_granule(scratch.grbuf[0] + i, bs_frame, sci, info->layer | 1)))
2336 {
2337 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));
2341 pcm = DRMP3_OFFSET_PTR(pcm, sizeof(drmp3d_sample_t)*384*info->channels);
2342 }
2343 if (bs_frame->pos > bs_frame->limit)
2344 {
2345 drmp3dec_init(dec);
2346 return 0;
2347 }
2348 }
2349 #endif
2350 }
2351
2352 return success*drmp3_hdr_frame_samples(dec->header);
2353 }
2354
2355 DRMP3_API void drmp3dec_f32_to_s16(const float *in, drmp3_int16 *out, size_t num_samples)
2356 {
2357 size_t i = 0;
2358 #if DRMP3_HAVE_SIMD
2359 size_t aligned_count = num_samples & ~7;
2360 for(; i < aligned_count; i+=8)
2361 {
2362 drmp3_f4 scale = DRMP3_VSET(32768.0f);
2363 drmp3_f4 a = DRMP3_VMUL(DRMP3_VLD(&in[i ]), scale);
2364 drmp3_f4 b = DRMP3_VMUL(DRMP3_VLD(&in[i+4]), scale);
2365 #if DRMP3_HAVE_SSE
2366 drmp3_f4 s16max = DRMP3_VSET( 32767.0f);
2367 drmp3_f4 s16min = DRMP3_VSET(-32768.0f);
2368 __m128i pcm8 = _mm_packs_epi32(_mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(a, s16max), s16min)),
2369 _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);
2378 #else
2379 int16x4_t pcma, pcmb;
2380 a = DRMP3_VADD(a, DRMP3_VSET(0.5f));
2381 b = DRMP3_VADD(b, DRMP3_VSET(0.5f));
2382 pcma = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(a), vreinterpretq_s32_u32(vcltq_f32(a, DRMP3_VSET(0)))));
2383 pcmb = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(b), vreinterpretq_s32_u32(vcltq_f32(b, DRMP3_VSET(0)))));
2384 vst1_lane_s16(out+i , pcma, 0);
2385 vst1_lane_s16(out+i+1, pcma, 1);
2386 vst1_lane_s16(out+i+2, pcma, 2);
2387 vst1_lane_s16(out+i+3, pcma, 3);
2388 vst1_lane_s16(out+i+4, pcmb, 0);
2389 vst1_lane_s16(out+i+5, pcmb, 1);
2390 vst1_lane_s16(out+i+6, pcmb, 2);
2391 vst1_lane_s16(out+i+7, pcmb, 3);
2392 #endif
2393 }
2394 #endif
2395 for(; i < num_samples; i++)
2396 {
2397 float sample = in[i] * 32768.0f;
2398 if (sample >= 32766.5)
2399 out[i] = (drmp3_int16) 32767;
2400 else if (sample <= -32767.5)
2401 out[i] = (drmp3_int16)-32768;
2402 else
2403 {
2404 short s = (drmp3_int16)(sample + .5f);
2405 s -= (s < 0); /* away from zero, to be compliant */
2406 out[i] = s;
2407 }
2408 }
2409 }
2410
2411
2412
2413 /************************************************************************************************************************************************************
2414
2415 Main Public API
2416
2417 ************************************************************************************************************************************************************/
2418 #if defined(SIZE_MAX)
2419 #define DRMP3_SIZE_MAX SIZE_MAX
2420 #else
2421 #if defined(_WIN64) || defined(_LP64) || defined(__LP64__)
2422 #define DRMP3_SIZE_MAX ((drmp3_uint64)0xFFFFFFFFFFFFFFFF)
2423 #else
2424 #define DRMP3_SIZE_MAX 0xFFFFFFFF
2425 #endif
2426 #endif
2427
2428 /* Options. */
2429 #ifndef DRMP3_SEEK_LEADING_MP3_FRAMES
2430 #define DRMP3_SEEK_LEADING_MP3_FRAMES 2
2431 #endif
2432
2433 #define DRMP3_MIN_DATA_CHUNK_SIZE 16384
2434
2435 /* The size in bytes of each chunk of data to read from the MP3 stream. minimp3 recommends at least 16K, but in an attempt to reduce data movement I'm making this slightly larger. */
2436 #ifndef DRMP3_DATA_CHUNK_SIZE
2437 #define DRMP3_DATA_CHUNK_SIZE (DRMP3_MIN_DATA_CHUNK_SIZE*4)
2438 #endif
2439
2440
2441 #define DRMP3_COUNTOF(x) (sizeof(x) / sizeof(x[0]))
2442 #define DRMP3_CLAMP(x, lo, hi) (DRMP3_MAX(lo, DRMP3_MIN(x, hi)))
2443
2444 #ifndef DRMP3_PI_D
2445 #define DRMP3_PI_D 3.14159265358979323846264
2446 #endif
2447
2448 #define DRMP3_DEFAULT_RESAMPLER_LPF_ORDER 2
2449
2450 static DRMP3_INLINE float drmp3_mix_f32(float x, float y, float a)
2451 {
2452 return x*(1-a) + y*a;
2453 }
2454 static DRMP3_INLINE float drmp3_mix_f32_fast(float x, float y, float a)
2455 {
2456 float r0 = (y - x);
2457 float r1 = r0*a;
2458 return x + r1;
2459 /*return x + (y - x)*a;*/
2460 }
2461
2462
2463 /*
2464 Greatest common factor using Euclid's algorithm iteratively.
2465 */
2466 static DRMP3_INLINE drmp3_uint32 drmp3_gcf_u32(drmp3_uint32 a, drmp3_uint32 b)
2467 {
2468 for (;;) {
2469 if (b == 0) {
2470 break;
2471 } else {
2472 drmp3_uint32 t = a;
2473 a = b;
2474 b = t % a;
2475 }
2476 }
2477
2478 return a;
2479 }
2480
2481
2482 static void* drmp3__malloc_default(size_t sz, void* pUserData)
2483 {
2484 (void)pUserData;
2485 return DRMP3_MALLOC(sz);
2486 }
2487
2488 static void* drmp3__realloc_default(void* p, size_t sz, void* pUserData)
2489 {
2490 (void)pUserData;
2491 return DRMP3_REALLOC(p, sz);
2492 }
2493
2494 static void drmp3__free_default(void* p, void* pUserData)
2495 {
2496 (void)pUserData;
2497 DRMP3_FREE(p);
2498 }
2499
2500
2501 static void* drmp3__malloc_from_callbacks(size_t sz, const drmp3_allocation_callbacks* pAllocationCallbacks)
2502 {
2503 if (pAllocationCallbacks == NULL) {
2504 return NULL;
2505 }
2506
2507 if (pAllocationCallbacks->onMalloc != NULL) {
2508 return pAllocationCallbacks->onMalloc(sz, pAllocationCallbacks->pUserData);
2509 }
2510
2511 /* Try using realloc(). */
2512 if (pAllocationCallbacks->onRealloc != NULL) {
2513 return pAllocationCallbacks->onRealloc(NULL, sz, pAllocationCallbacks->pUserData);
2514 }
2515
2516 return NULL;
2517 }
2518
2519 static void* drmp3__realloc_from_callbacks(void* p, size_t szNew, size_t szOld, const drmp3_allocation_callbacks* pAllocationCallbacks)
2520 {
2521 if (pAllocationCallbacks == NULL) {
2522 return NULL;
2523 }
2524
2525 if (pAllocationCallbacks->onRealloc != NULL) {
2526 return pAllocationCallbacks->onRealloc(p, szNew, pAllocationCallbacks->pUserData);
2527 }
2528
2529 /* Try emulating realloc() in terms of malloc()/free(). */
2530 if (pAllocationCallbacks->onMalloc != NULL && pAllocationCallbacks->onFree != NULL) {
2531 void* p2;
2532
2533 p2 = pAllocationCallbacks->onMalloc(szNew, pAllocationCallbacks->pUserData);
2534 if (p2 == NULL) {
2535 return NULL;
2536 }
2537
2538 if (p != NULL) {
2539 DRMP3_COPY_MEMORY(p2, p, szOld);
2540 pAllocationCallbacks->onFree(p, pAllocationCallbacks->pUserData);
2541 }
2542
2543 return p2;
2544 }
2545
2546 return NULL;
2547 }
2548
2549 static void drmp3__free_from_callbacks(void* p, const drmp3_allocation_callbacks* pAllocationCallbacks)
2550 {
2551 if (p == NULL || pAllocationCallbacks == NULL) {
2552 return;
2553 }
2554
2555 if (pAllocationCallbacks->onFree != NULL) {
2556 pAllocationCallbacks->onFree(p, pAllocationCallbacks->pUserData);
2557 }
2558 }
2559
2560
2561 static drmp3_allocation_callbacks drmp3_copy_allocation_callbacks_or_defaults(const drmp3_allocation_callbacks* pAllocationCallbacks)
2562 {
2563 if (pAllocationCallbacks != NULL) {
2564 /* Copy. */
2565 return *pAllocationCallbacks;
2566 } else {
2567 /* Defaults. */
2568 drmp3_allocation_callbacks allocationCallbacks;
2569 allocationCallbacks.pUserData = NULL;
2570 allocationCallbacks.onMalloc = drmp3__malloc_default;
2571 allocationCallbacks.onRealloc = drmp3__realloc_default;
2572 allocationCallbacks.onFree = drmp3__free_default;
2573 return allocationCallbacks;
2574 }
2575 }
2576
2577
2578
2579 static size_t drmp3__on_read(drmp3* pMP3, void* pBufferOut, size_t bytesToRead)
2580 {
2581 size_t bytesRead = pMP3->onRead(pMP3->pUserData, pBufferOut, bytesToRead);
2582 pMP3->streamCursor += bytesRead;
2583 return bytesRead;
2584 }
2585
2586 static drmp3_bool32 drmp3__on_seek(drmp3* pMP3, int offset, drmp3_seek_origin origin)
2587 {
2588 DRMP3_ASSERT(offset >= 0);
2589
2590 if (!pMP3->onSeek(pMP3->pUserData, offset, origin)) {
2591 return DRMP3_FALSE;
2592 }
2593
2594 if (origin == drmp3_seek_origin_start) {
2595 pMP3->streamCursor = (drmp3_uint64)offset;
2596 } else {
2597 pMP3->streamCursor += offset;
2598 }
2599
2600 return DRMP3_TRUE;
2601 }
2602
2603 static drmp3_bool32 drmp3__on_seek_64(drmp3* pMP3, drmp3_uint64 offset, drmp3_seek_origin origin)
2604 {
2605 if (offset <= 0x7FFFFFFF) {
2606 return drmp3__on_seek(pMP3, (int)offset, origin);
2607 }
2608
2609
2610 /* 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)) {
2612 return DRMP3_FALSE;
2613 }
2614
2615 offset -= 0x7FFFFFFF;
2616 while (offset > 0) {
2617 if (offset <= 0x7FFFFFFF) {
2618 if (!drmp3__on_seek(pMP3, (int)offset, drmp3_seek_origin_current)) {
2619 return DRMP3_FALSE;
2620 }
2621 offset = 0;
2622 } else {
2623 if (!drmp3__on_seek(pMP3, 0x7FFFFFFF, drmp3_seek_origin_current)) {
2624 return DRMP3_FALSE;
2625 }
2626 offset -= 0x7FFFFFFF;
2627 }
2628 }
2629
2630 return DRMP3_TRUE;
2631 }
2632
2633
2634 static drmp3_uint32 drmp3_decode_next_frame_ex__callbacks(drmp3* pMP3, drmp3d_sample_t* pPCMFrames)
2635 {
2636 drmp3_uint32 pcmFramesRead = 0;
2637
2638 DRMP3_ASSERT(pMP3 != NULL);
2639 DRMP3_ASSERT(pMP3->onRead != NULL);
2640
2641 if (pMP3->atEnd) {
2642 return 0;
2643 }
2644
2645 for (;;) {
2646 drmp3dec_frame_info info;
2647
2648 /* minimp3 recommends doing data submission in chunks of at least 16K. If we don't have at least 16K bytes available, get more. */
2649 if (pMP3->dataSize < DRMP3_MIN_DATA_CHUNK_SIZE) {
2650 size_t bytesRead;
2651
2652 /* First we need to move the data down. */
2653 if (pMP3->pData != NULL) {
2654 DRMP3_MOVE_MEMORY(pMP3->pData, pMP3->pData + pMP3->dataConsumed, pMP3->dataSize);
2655 }
2656
2657 pMP3->dataConsumed = 0;
2658
2659 if (pMP3->dataCapacity < DRMP3_DATA_CHUNK_SIZE) {
2660 drmp3_uint8* pNewData;
2661 size_t newDataCap;
2662
2663 newDataCap = DRMP3_DATA_CHUNK_SIZE;
2664
2665 pNewData = (drmp3_uint8*)drmp3__realloc_from_callbacks(pMP3->pData, newDataCap, pMP3->dataCapacity, &pMP3->allocationCallbacks);
2666 if (pNewData == NULL) {
2667 return 0; /* Out of memory. */
2668 }
2669
2670 pMP3->pData = pNewData;
2671 pMP3->dataCapacity = newDataCap;
2672 }
2673
2674 bytesRead = drmp3__on_read(pMP3, pMP3->pData + pMP3->dataSize, (pMP3->dataCapacity - pMP3->dataSize));
2675 if (bytesRead == 0) {
2676 if (pMP3->dataSize == 0) {
2677 pMP3->atEnd = DRMP3_TRUE;
2678 return 0; /* No data. */
2679 }
2680 }
2681
2682 pMP3->dataSize += bytesRead;
2683 }
2684
2685 if (pMP3->dataSize > INT_MAX) {
2686 pMP3->atEnd = DRMP3_TRUE;
2687 return 0; /* File too big. */
2688 }
2689
2690 DRMP3_ASSERT(pMP3->pData != NULL);
2691 DRMP3_ASSERT(pMP3->dataCapacity > 0);
2692
2693 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
2695 /* 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 }
2700
2701 /* 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 if (pcmFramesRead > 0) {
2703 pcmFramesRead = drmp3_hdr_frame_samples(pMP3->decoder.header);
2704 pMP3->pcmFramesConsumedInMP3Frame = 0;
2705 pMP3->pcmFramesRemainingInMP3Frame = pcmFramesRead;
2706 pMP3->mp3FrameChannels = info.channels;
2707 pMP3->mp3FrameSampleRate = info.hz;
2708 break;
2709 } else if (info.frame_bytes == 0) {
2710 /* Need more data. minimp3 recommends doing data submission in 16K chunks. */
2711 size_t bytesRead;
2712
2713 /* First we need to move the data down. */
2714 DRMP3_MOVE_MEMORY(pMP3->pData, pMP3->pData + pMP3->dataConsumed, pMP3->dataSize);
2715 pMP3->dataConsumed = 0;
2716
2717 if (pMP3->dataCapacity == pMP3->dataSize) {
2718 /* No room. Expand. */
2719 drmp3_uint8* pNewData;
2720 size_t newDataCap;
2721
2722 newDataCap = pMP3->dataCapacity + DRMP3_DATA_CHUNK_SIZE;
2723
2724 pNewData = (drmp3_uint8*)drmp3__realloc_from_callbacks(pMP3->pData, newDataCap, pMP3->dataCapacity, &pMP3->allocationCallbacks);
2725 if (pNewData == NULL) {
2726 return 0; /* Out of memory. */
2727 }
2728
2729 pMP3->pData = pNewData;
2730 pMP3->dataCapacity = newDataCap;
2731 }
2732
2733 /* Fill in a chunk. */
2734 bytesRead = drmp3__on_read(pMP3, pMP3->pData + pMP3->dataSize, (pMP3->dataCapacity - pMP3->dataSize));
2735 if (bytesRead == 0) {
2736 pMP3->atEnd = DRMP3_TRUE;
2737 return 0; /* Error reading more data. */
2738 }
2739
2740 pMP3->dataSize += bytesRead;
2741 }
2742 };
2743
2744 return pcmFramesRead;
2745 }
2746
2747 static drmp3_uint32 drmp3_decode_next_frame_ex__memory(drmp3* pMP3, drmp3d_sample_t* pPCMFrames)
2748 {
2749 drmp3_uint32 pcmFramesRead = 0;
2750 drmp3dec_frame_info info;
2751
2752 DRMP3_ASSERT(pMP3 != NULL);
2753 DRMP3_ASSERT(pMP3->memory.pData != NULL);
2754
2755 if (pMP3->atEnd) {
2756 return 0;
2757 }
2758
2759 for (;;) {
2760 pcmFramesRead = drmp3dec_decode_frame(&pMP3->decoder, pMP3->memory.pData + pMP3->memory.currentReadPos, (int)(pMP3->memory.dataSize - pMP3->memory.currentReadPos), pPCMFrames, &info);
2761 if (pcmFramesRead > 0) {
2762 pcmFramesRead = drmp3_hdr_frame_samples(pMP3->decoder.header);
2763 pMP3->pcmFramesConsumedInMP3Frame = 0;
2764 pMP3->pcmFramesRemainingInMP3Frame = pcmFramesRead;
2765 pMP3->mp3FrameChannels = info.channels;
2766 pMP3->mp3FrameSampleRate = info.hz;
2767 break;
2768 } else if (info.frame_bytes > 0) {
2769 /* No frames were read, but it looks like we skipped past one. Read the next MP3 frame. */
2770 pMP3->memory.currentReadPos += (size_t)info.frame_bytes;
2771 } else {
2772 /* Nothing at all was read. Abort. */
2773 break;
2774 }
2775 }
2776
2777 /* Consume the data. */
2778 pMP3->memory.currentReadPos += (size_t)info.frame_bytes;
2779
2780 return pcmFramesRead;
2781 }
2782
2783 static drmp3_uint32 drmp3_decode_next_frame_ex(drmp3* pMP3, drmp3d_sample_t* pPCMFrames)
2784 {
2785 if (pMP3->memory.pData != NULL && pMP3->memory.dataSize > 0) {
2786 return drmp3_decode_next_frame_ex__memory(pMP3, pPCMFrames);
2787 } else {
2788 return drmp3_decode_next_frame_ex__callbacks(pMP3, pPCMFrames);
2789 }
2790 }
2791
2792 static drmp3_uint32 drmp3_decode_next_frame(drmp3* pMP3)
2793 {
2794 DRMP3_ASSERT(pMP3 != NULL);
2795 return drmp3_decode_next_frame_ex(pMP3, (drmp3d_sample_t*)pMP3->pcmFrames);
2796 }
2797
2798 #if 0
2799 static drmp3_uint32 drmp3_seek_next_frame(drmp3* pMP3)
2800 {
2801 drmp3_uint32 pcmFrameCount;
2802
2803 DRMP3_ASSERT(pMP3 != NULL);
2804
2805 pcmFrameCount = drmp3_decode_next_frame_ex(pMP3, NULL);
2806 if (pcmFrameCount == 0) {
2807 return 0;
2808 }
2809
2810 /* We have essentially just skipped past the frame, so just set the remaining samples to 0. */
2811 pMP3->currentPCMFrame += pcmFrameCount;
2812 pMP3->pcmFramesConsumedInMP3Frame = pcmFrameCount;
2813 pMP3->pcmFramesRemainingInMP3Frame = 0;
2814
2815 return pcmFrameCount;
2816 }
2817 #endif
2818
2819 static drmp3_bool32 drmp3_init_internal(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, const drmp3_allocation_callbacks* pAllocationCallbacks)
2820 {
2821 DRMP3_ASSERT(pMP3 != NULL);
2822 DRMP3_ASSERT(onRead != NULL);
2823
2824 /* This function assumes the output object has already been reset to 0. Do not do that here, otherwise things will break. */
2825 drmp3dec_init(&pMP3->decoder);
2826
2827 pMP3->onRead = onRead;
2828 pMP3->onSeek = onSeek;
2829 pMP3->pUserData = pUserData;
2830 pMP3->allocationCallbacks = drmp3_copy_allocation_callbacks_or_defaults(pAllocationCallbacks);
2831
2832 if (pMP3->allocationCallbacks.onFree == NULL || (pMP3->allocationCallbacks.onMalloc == NULL && pMP3->allocationCallbacks.onRealloc == NULL)) {
2833 return DRMP3_FALSE; /* Invalid allocation callbacks. */
2834 }
2835
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 }
2841
2842 pMP3->channels = pMP3->mp3FrameChannels;
2843 pMP3->sampleRate = pMP3->mp3FrameSampleRate;
2844
2845 return DRMP3_TRUE;
2846 }
2847
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;
2852 }
2853
2854 DRMP3_ZERO_OBJECT(pMP3);
2855 return drmp3_init_internal(pMP3, onRead, onSeek, pUserData, pAllocationCallbacks);
2856 }
2857
2858
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;
2863
2864 DRMP3_ASSERT(pMP3 != NULL);
2865 DRMP3_ASSERT(pMP3->memory.dataSize >= pMP3->memory.currentReadPos);
2866
2867 bytesRemaining = pMP3->memory.dataSize - pMP3->memory.currentReadPos;
2868 if (bytesToRead > bytesRemaining) {
2869 bytesToRead = bytesRemaining;
2870 }
2871
2872 if (bytesToRead > 0) {
2873 DRMP3_COPY_MEMORY(pBufferOut, pMP3->memory.pData + pMP3->memory.currentReadPos, bytesToRead);
2874 pMP3->memory.currentReadPos += bytesToRead;
2875 }
2876
2877 return bytesToRead;
2878 }
2879
2880 static drmp3_bool32 drmp3__on_seek_memory(void* pUserData, int byteOffset, drmp3_seek_origin origin)
2881 {
2882 drmp3* pMP3 = (drmp3*)pUserData;
2883
2884 DRMP3_ASSERT(pMP3 != NULL);
2885
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. */
2894 }
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 } else {
2903 pMP3->memory.currentReadPos = pMP3->memory.dataSize; /* Trying to seek too far forward. */
2904 }
2905 }
2906
2907 return DRMP3_TRUE;
2908 }
2909
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 }
2915
2916 DRMP3_ZERO_OBJECT(pMP3);
2917
2918 if (pData == NULL || dataSize == 0) {
2919 return DRMP3_FALSE;
2920 }
2921
2922 pMP3->memory.pData = (const drmp3_uint8*)pData;
2923 pMP3->memory.dataSize = dataSize;
2924 pMP3->memory.currentReadPos = 0;
2925
2926 return drmp3_init_internal(pMP3, drmp3__on_read_memory, drmp3__on_seek_memory, pMP3, pAllocationCallbacks);
2927 }
2928
2929
2930 #ifndef DR_MP3_NO_STDIO
2931 #include <stdio.h>
2932 #include <wchar.h> /* For wcslen(), wcsrtombs() */
2933
2934 /* drmp3_result_from_errno() is only used inside DR_MP3_NO_STDIO for now. Move this out if it's ever used elsewhere. */
2935 #include <errno.h>
2936 static drmp3_result drmp3_result_from_errno(int e)
2937 {
2938 switch (e)
2939 {
2940 case 0: return DRMP3_SUCCESS;
2941 #ifdef EPERM
2942 case EPERM: return DRMP3_INVALID_OPERATION;
2943 #endif
2944 #ifdef ENOENT
2945 case ENOENT: return DRMP3_DOES_NOT_EXIST;
2946 #endif
2947 #ifdef ESRCH
2948 case ESRCH: return DRMP3_DOES_NOT_EXIST;
2949 #endif
2950 #ifdef EINTR
2951 case EINTR: return DRMP3_INTERRUPT;
2952 #endif
2953 #ifdef EIO
2954 case EIO: return DRMP3_IO_ERROR;
2955 #endif
2956 #ifdef ENXIO
2957 case ENXIO: return DRMP3_DOES_NOT_EXIST;
2958 #endif
2959 #ifdef E2BIG
2960 case E2BIG: return DRMP3_INVALID_ARGS;
2961 #endif
2962 #ifdef ENOEXEC
2963 case ENOEXEC: return DRMP3_INVALID_FILE;
2964 #endif
2965 #ifdef EBADF
2966 case EBADF: return DRMP3_INVALID_FILE;
2967 #endif
2968 #ifdef ECHILD
2969 case ECHILD: return DRMP3_ERROR;
2970 #endif
2971 #ifdef EAGAIN
2972 case EAGAIN: return DRMP3_UNAVAILABLE;
2973 #endif
2974 #ifdef ENOMEM
2975 case ENOMEM: return DRMP3_OUT_OF_MEMORY;
2976 #endif
2977 #ifdef EACCES
2978 case EACCES: return DRMP3_ACCESS_DENIED;
2979 #endif
2980 #ifdef EFAULT
2981 case EFAULT: return DRMP3_BAD_ADDRESS;
2982 #endif
2983 #ifdef ENOTBLK
2984 case ENOTBLK: return DRMP3_ERROR;
2985 #endif
2986 #ifdef EBUSY
2987 case EBUSY: return DRMP3_BUSY;
2988 #endif
2989 #ifdef EEXIST
2990 case EEXIST: return DRMP3_ALREADY_EXISTS;
2991 #endif
2992 #ifdef EXDEV
2993 case EXDEV: return DRMP3_ERROR;
2994 #endif
2995 #ifdef ENODEV
2996 case ENODEV: return DRMP3_DOES_NOT_EXIST;
2997 #endif
2998 #ifdef ENOTDIR
2999 case ENOTDIR: return DRMP3_NOT_DIRECTORY;
3000 #endif
3001 #ifdef EISDIR
3002 case EISDIR: return DRMP3_IS_DIRECTORY;
3003 #endif
3004 #ifdef EINVAL
3005 case EINVAL: return DRMP3_INVALID_ARGS;
3006 #endif
3007 #ifdef ENFILE
3008 case ENFILE: return DRMP3_TOO_MANY_OPEN_FILES;
3009 #endif
3010 #ifdef EMFILE
3011 case EMFILE: return DRMP3_TOO_MANY_OPEN_FILES;
3012 #endif
3013 #ifdef ENOTTY
3014 case ENOTTY: return DRMP3_INVALID_OPERATION;
3015 #endif
3016 #ifdef ETXTBSY
3017 case ETXTBSY: return DRMP3_BUSY;
3018 #endif
3019 #ifdef EFBIG
3020 case EFBIG: return DRMP3_TOO_BIG;
3021 #endif
3022 #ifdef ENOSPC
3023 case ENOSPC: return DRMP3_NO_SPACE;
3024 #endif
3025 #ifdef ESPIPE
3026 case ESPIPE: return DRMP3_BAD_SEEK;
3027 #endif
3028 #ifdef EROFS
3029 case EROFS: return DRMP3_ACCESS_DENIED;
3030 #endif
3031 #ifdef EMLINK
3032 case EMLINK: return DRMP3_TOO_MANY_LINKS;
3033 #endif
3034 #ifdef EPIPE
3035 case EPIPE: return DRMP3_BAD_PIPE;
3036 #endif
3037 #ifdef EDOM
3038 case EDOM: return DRMP3_OUT_OF_RANGE;
3039 #endif
3040 #ifdef ERANGE
3041 case ERANGE: return DRMP3_OUT_OF_RANGE;
3042 #endif
3043 #ifdef EDEADLK
3044 case EDEADLK: return DRMP3_DEADLOCK;
3045 #endif
3046 #ifdef ENAMETOOLONG
3047 case ENAMETOOLONG: return DRMP3_PATH_TOO_LONG;
3048 #endif
3049 #ifdef ENOLCK
3050 case ENOLCK: return DRMP3_ERROR;
3051 #endif
3052 #ifdef ENOSYS
3053 case ENOSYS: return DRMP3_NOT_IMPLEMENTED;
3054 #endif
3055 #ifdef ENOTEMPTY
3056 case ENOTEMPTY: return DRMP3_DIRECTORY_NOT_EMPTY;
3057 #endif
3058 #ifdef ELOOP
3059 case ELOOP: return DRMP3_TOO_MANY_LINKS;
3060 #endif
3061 #ifdef ENOMSG
3062 case ENOMSG: return DRMP3_NO_MESSAGE;
3063 #endif
3064 #ifdef EIDRM
3065 case EIDRM: return DRMP3_ERROR;
3066 #endif
3067 #ifdef ECHRNG
3068 case ECHRNG: return DRMP3_ERROR;
3069 #endif
3070 #ifdef EL2NSYNC
3071 case EL2NSYNC: return DRMP3_ERROR;
3072 #endif
3073 #ifdef EL3HLT
3074 case EL3HLT: return DRMP3_ERROR;
3075 #endif
3076 #ifdef EL3RST
3077 case EL3RST: return DRMP3_ERROR;
3078 #endif
3079 #ifdef ELNRNG
3080 case ELNRNG: return DRMP3_OUT_OF_RANGE;
3081 #endif
3082 #ifdef EUNATCH
3083 case EUNATCH: return DRMP3_ERROR;
3084 #endif
3085 #ifdef ENOCSI
3086 case ENOCSI: return DRMP3_ERROR;
3087 #endif
3088 #ifdef EL2HLT
3089 case EL2HLT: return DRMP3_ERROR;
3090 #endif
3091 #ifdef EBADE
3092 case EBADE: return DRMP3_ERROR;
3093 #endif
3094 #ifdef EBADR
3095 case EBADR: return DRMP3_ERROR;
3096 #endif
3097 #ifdef EXFULL
3098 case EXFULL: return DRMP3_ERROR;
3099 #endif
3100 #ifdef ENOANO
3101 case ENOANO: return DRMP3_ERROR;
3102 #endif
3103 #ifdef EBADRQC
3104 case EBADRQC: return DRMP3_ERROR;
3105 #endif
3106 #ifdef EBADSLT
3107 case EBADSLT: return DRMP3_ERROR;
3108 #endif
3109 #ifdef EBFONT
3110 case EBFONT: return DRMP3_INVALID_FILE;
3111 #endif
3112 #ifdef ENOSTR
3113 case ENOSTR: return DRMP3_ERROR;
3114 #endif
3115 #ifdef ENODATA
3116 case ENODATA: return DRMP3_NO_DATA_AVAILABLE;
3117 #endif
3118 #ifdef ETIME
3119 case ETIME: return DRMP3_TIMEOUT;
3120 #endif
3121 #ifdef ENOSR
3122 case ENOSR: return DRMP3_NO_DATA_AVAILABLE;
3123 #endif
3124 #ifdef ENONET
3125 case ENONET: return DRMP3_NO_NETWORK;
3126 #endif
3127 #ifdef ENOPKG
3128 case ENOPKG: return DRMP3_ERROR;
3129 #endif
3130 #ifdef EREMOTE
3131 case EREMOTE: return DRMP3_ERROR;
3132 #endif
3133 #ifdef ENOLINK
3134 case ENOLINK: return DRMP3_ERROR;
3135 #endif
3136 #ifdef EADV
3137 case EADV: return DRMP3_ERROR;
3138 #endif
3139 #ifdef ESRMNT
3140 case ESRMNT: return DRMP3_ERROR;
3141 #endif
3142 #ifdef ECOMM
3143 case ECOMM: return DRMP3_ERROR;
3144 #endif
3145 #ifdef EPROTO
3146 case EPROTO: return DRMP3_ERROR;
3147 #endif
3148 #ifdef EMULTIHOP
3149 case EMULTIHOP: return DRMP3_ERROR;
3150 #endif
3151 #ifdef EDOTDOT
3152 case EDOTDOT: return DRMP3_ERROR;
3153 #endif
3154 #ifdef EBADMSG
3155 case EBADMSG: return DRMP3_BAD_MESSAGE;
3156 #endif
3157 #ifdef EOVERFLOW
3158 case EOVERFLOW: return DRMP3_TOO_BIG;
3159 #endif
3160 #ifdef ENOTUNIQ
3161 case ENOTUNIQ: return DRMP3_NOT_UNIQUE;
3162 #endif
3163 #ifdef EBADFD
3164 case EBADFD: return DRMP3_ERROR;
3165 #endif
3166 #ifdef EREMCHG
3167 case EREMCHG: return DRMP3_ERROR;
3168 #endif
3169 #ifdef ELIBACC
3170 case ELIBACC: return DRMP3_ACCESS_DENIED;
3171 #endif
3172 #ifdef ELIBBAD
3173 case ELIBBAD: return DRMP3_INVALID_FILE;
3174 #endif
3175 #ifdef ELIBSCN
3176 case ELIBSCN: return DRMP3_INVALID_FILE;
3177 #endif
3178 #ifdef ELIBMAX
3179 case ELIBMAX: return DRMP3_ERROR;
3180 #endif
3181 #ifdef ELIBEXEC
3182 case ELIBEXEC: return DRMP3_ERROR;
3183 #endif
3184 #ifdef EILSEQ
3185 case EILSEQ: return DRMP3_INVALID_DATA;
3186 #endif
3187 #ifdef ERESTART
3188 case ERESTART: return DRMP3_ERROR;
3189 #endif
3190 #ifdef ESTRPIPE
3191 case ESTRPIPE: return DRMP3_ERROR;
3192 #endif
3193 #ifdef EUSERS
3194 case EUSERS: return DRMP3_ERROR;
3195 #endif
3196 #ifdef ENOTSOCK
3197 case ENOTSOCK: return DRMP3_NOT_SOCKET;
3198 #endif
3199 #ifdef EDESTADDRREQ
3200 case EDESTADDRREQ: return DRMP3_NO_ADDRESS;
3201 #endif
3202 #ifdef EMSGSIZE
3203 case EMSGSIZE: return DRMP3_TOO_BIG;
3204 #endif
3205 #ifdef EPROTOTYPE
3206 case EPROTOTYPE: return DRMP3_BAD_PROTOCOL;
3207 #endif
3208 #ifdef ENOPROTOOPT
3209 case ENOPROTOOPT: return DRMP3_PROTOCOL_UNAVAILABLE;
3210 #endif
3211 #ifdef EPROTONOSUPPORT
3212 case EPROTONOSUPPORT: return DRMP3_PROTOCOL_NOT_SUPPORTED;
3213 #endif
3214 #ifdef ESOCKTNOSUPPORT
3215 case ESOCKTNOSUPPORT: return DRMP3_SOCKET_NOT_SUPPORTED;
3216 #endif
3217 #ifdef EOPNOTSUPP
3218 case EOPNOTSUPP: return DRMP3_INVALID_OPERATION;
3219 #endif
3220 #ifdef EPFNOSUPPORT
3221 case EPFNOSUPPORT: return DRMP3_PROTOCOL_FAMILY_NOT_SUPPORTED;
3222 #endif
3223 #ifdef EAFNOSUPPORT
3224 case EAFNOSUPPORT: return DRMP3_ADDRESS_FAMILY_NOT_SUPPORTED;
3225 #endif
3226 #ifdef EADDRINUSE
3227 case EADDRINUSE: return DRMP3_ALREADY_IN_USE;
3228 #endif
3229 #ifdef EADDRNOTAVAIL
3230 case EADDRNOTAVAIL: return DRMP3_ERROR;
3231 #endif
3232 #ifdef ENETDOWN
3233 case ENETDOWN: return DRMP3_NO_NETWORK;
3234 #endif
3235 #ifdef ENETUNREACH
3236 case ENETUNREACH: return DRMP3_NO_NETWORK;
3237 #endif
3238 #ifdef ENETRESET
3239 case ENETRESET: return DRMP3_NO_NETWORK;
3240 #endif
3241 #ifdef ECONNABORTED
3242 case ECONNABORTED: return DRMP3_NO_NETWORK;
3243 #endif
3244 #ifdef ECONNRESET
3245 case ECONNRESET: return DRMP3_CONNECTION_RESET;
3246 #endif
3247 #ifdef ENOBUFS
3248 case ENOBUFS: return DRMP3_NO_SPACE;
3249 #endif
3250 #ifdef EISCONN
3251 case EISCONN: return DRMP3_ALREADY_CONNECTED;
3252 #endif
3253 #ifdef ENOTCONN
3254 case ENOTCONN: return DRMP3_NOT_CONNECTED;
3255 #endif
3256 #ifdef ESHUTDOWN
3257 case ESHUTDOWN: return DRMP3_ERROR;
3258 #endif
3259 #ifdef ETOOMANYREFS
3260 case ETOOMANYREFS: return DRMP3_ERROR;
3261 #endif
3262 #ifdef ETIMEDOUT
3263 case ETIMEDOUT: return DRMP3_TIMEOUT;
3264 #endif
3265 #ifdef ECONNREFUSED
3266 case ECONNREFUSED: return DRMP3_CONNECTION_REFUSED;
3267 #endif
3268 #ifdef EHOSTDOWN
3269 case EHOSTDOWN: return DRMP3_NO_HOST;
3270 #endif
3271 #ifdef EHOSTUNREACH
3272 case EHOSTUNREACH: return DRMP3_NO_HOST;
3273 #endif
3274 #ifdef EALREADY
3275 case EALREADY: return DRMP3_IN_PROGRESS;
3276 #endif
3277 #ifdef EINPROGRESS
3278 case EINPROGRESS: return DRMP3_IN_PROGRESS;
3279 #endif
3280 #ifdef ESTALE
3281 case ESTALE: return DRMP3_INVALID_FILE;
3282 #endif
3283 #ifdef EUCLEAN
3284 case EUCLEAN: return DRMP3_ERROR;
3285 #endif
3286 #ifdef ENOTNAM
3287 case ENOTNAM: return DRMP3_ERROR;
3288 #endif
3289 #ifdef ENAVAIL
3290 case ENAVAIL: return DRMP3_ERROR;
3291 #endif
3292 #ifdef EISNAM
3293 case EISNAM: return DRMP3_ERROR;
3294 #endif
3295 #ifdef EREMOTEIO
3296 case EREMOTEIO: return DRMP3_IO_ERROR;
3297 #endif
3298 #ifdef EDQUOT
3299 case EDQUOT: return DRMP3_NO_SPACE;
3300 #endif
3301 #ifdef ENOMEDIUM
3302 case ENOMEDIUM: return DRMP3_DOES_NOT_EXIST;
3303 #endif
3304 #ifdef EMEDIUMTYPE
3305 case EMEDIUMTYPE: return DRMP3_ERROR;
3306 #endif
3307 #ifdef ECANCELED
3308 case ECANCELED: return DRMP3_CANCELLED;
3309 #endif
3310 #ifdef ENOKEY
3311 case ENOKEY: return DRMP3_ERROR;
3312 #endif
3313 #ifdef EKEYEXPIRED
3314 case EKEYEXPIRED: return DRMP3_ERROR;
3315 #endif
3316 #ifdef EKEYREVOKED
3317 case EKEYREVOKED: return DRMP3_ERROR;
3318 #endif
3319 #ifdef EKEYREJECTED
3320 case EKEYREJECTED: return DRMP3_ERROR;
3321 #endif
3322 #ifdef EOWNERDEAD
3323 case EOWNERDEAD: return DRMP3_ERROR;
3324 #endif
3325 #ifdef ENOTRECOVERABLE
3326 case ENOTRECOVERABLE: return DRMP3_ERROR;
3327 #endif
3328 #ifdef ERFKILL
3329 case ERFKILL: return DRMP3_ERROR;
3330 #endif
3331 #ifdef EHWPOISON
3332 case EHWPOISON: return DRMP3_ERROR;
3333 #endif
3334 default: return DRMP3_ERROR;
3335 }
3336 }
3337
3338 static drmp3_result drmp3_fopen(FILE** ppFile, const char* pFilePath, const char* pOpenMode)
3339 {
3340 #if defined(_MSC_VER) && _MSC_VER >= 1400
3341 errno_t err;
3342 #endif
3343
3344 if (ppFile != NULL) {
3345 *ppFile = NULL; /* Safety. */
3346 }
3347
3348 if (pFilePath == NULL || pOpenMode == NULL || ppFile == NULL) {
3349 return DRMP3_INVALID_ARGS;
3350 }
3351
3352 #if defined(_MSC_VER) && _MSC_VER >= 1400
3353 err = fopen_s(ppFile, pFilePath, pOpenMode);
3354 if (err != 0) {
3355 return drmp3_result_from_errno(err);
3356 }
3357 #else
3358 #if defined(_WIN32) || defined(__APPLE__)
3359 *ppFile = fopen(pFilePath, pOpenMode);
3360 #else
3361 #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 && defined(_LARGEFILE64_SOURCE)
3362 *ppFile = fopen64(pFilePath, pOpenMode);
3363 #else
3364 *ppFile = fopen(pFilePath, pOpenMode);
3365 #endif
3366 #endif
3367 if (*ppFile == NULL) {
3368 drmp3_result result = drmp3_result_from_errno(errno);
3369 if (result == DRMP3_SUCCESS) {
3370 result = DRMP3_ERROR; /* Just a safety check to make sure we never ever return success when pFile == NULL. */
3371 }
3372
3373 return result;
3374 }
3375 #endif
3376
3377 return DRMP3_SUCCESS;
3378 }
3379
3380 /*
3381 _wfopen() isn't always available in all compilation environments.
3382
3383 * Windows only.
3384 * MSVC seems to support it universally as far back as VC6 from what I can tell (haven't checked further back).
3385 * MinGW-64 (both 32- and 64-bit) seems to support it.
3386 * MinGW wraps it in !defined(__STRICT_ANSI__).
3387 * OpenWatcom wraps it in !defined(_NO_EXT_KEYS).
3388
3389 This can be reviewed as compatibility issues arise. The preference is to use _wfopen_s() and _wfopen() as opposed to the wcsrtombs()
3390 fallback, so if you notice your compiler not detecting this properly I'm happy to look at adding support.
3391 */
3392 #if defined(_WIN32)
3393 #if defined(_MSC_VER) || defined(__MINGW64__) || (!defined(__STRICT_ANSI__) && !defined(_NO_EXT_KEYS))
3394 #define DRMP3_HAS_WFOPEN
3395 #endif
3396 #endif
3397
3398 static drmp3_result drmp3_wfopen(FILE** ppFile, const wchar_t* pFilePath, const wchar_t* pOpenMode, const drmp3_allocation_callbacks* pAllocationCallbacks)
3399 {
3400 if (ppFile != NULL) {
3401 *ppFile = NULL; /* Safety. */
3402 }
3403
3404 if (pFilePath == NULL || pOpenMode == NULL || ppFile == NULL) {
3405 return DRMP3_INVALID_ARGS;
3406 }
3407
3408 #if defined(DRMP3_HAS_WFOPEN)
3409 {
3410 /* Use _wfopen() on Windows. */
3411 #if defined(_MSC_VER) && _MSC_VER >= 1400
3412 errno_t err = _wfopen_s(ppFile, pFilePath, pOpenMode);
3413 if (err != 0) {
3414 return drmp3_result_from_errno(err);
3415 }
3416 #else
3417 *ppFile = _wfopen(pFilePath, pOpenMode);
3418 if (*ppFile == NULL) {
3419 return drmp3_result_from_errno(errno);
3420 }
3421 #endif
3422 (void)pAllocationCallbacks;
3423 }
3424 #else
3425 /*
3426 Use fopen() on anything other than Windows. Requires a conversion. This is annoying because
3427 fopen() is locale specific. The only real way I can think of to do this is with wcsrtombs(). Note
3428 that wcstombs() is apparently not thread-safe because it uses a static global mbstate_t object for
3429 maintaining state. I've checked this with -std=c89 and it works, but if somebody get's a compiler
3430 error I'll look into improving compatibility.
3431 */
3432
3433 /*
3434 Some compilers don't support wchar_t or wcsrtombs() which we're using below. In this case we just
3435 need to abort with an error. If you encounter a compiler lacking such support, add it to this list
3436 and submit a bug report and it'll be added to the library upstream.
3437 */
3438 #if defined(__DJGPP__)
3439 {
3440 /* Nothing to do here. This will fall through to the error check below. */
3441 }
3442 #else
3443 {
3444 mbstate_t mbs;
3445 size_t lenMB;
3446 const wchar_t* pFilePathTemp = pFilePath;
3447 char* pFilePathMB = NULL;
3448 char pOpenModeMB[32] = {0};
3449
3450 /* Get the length first. */
3451 DRMP3_ZERO_OBJECT(&mbs);
3452 lenMB = wcsrtombs(NULL, &pFilePathTemp, 0, &mbs);
3453 if (lenMB == (size_t)-1) {
3454 return drmp3_result_from_errno(errno);
3455 }
3456
3457 pFilePathMB = (char*)drmp3__malloc_from_callbacks(lenMB + 1, pAllocationCallbacks);
3458 if (pFilePathMB == NULL) {
3459 return DRMP3_OUT_OF_MEMORY;
3460 }
3461
3462 pFilePathTemp = pFilePath;
3463 DRMP3_ZERO_OBJECT(&mbs);
3464 wcsrtombs(pFilePathMB, &pFilePathTemp, lenMB + 1, &mbs);
3465
3466 /* The open mode should always consist of ASCII characters so we should be able to do a trivial conversion. */
3467 {
3468 size_t i = 0;
3469 for (;;) {
3470 if (pOpenMode[i] == 0) {
3471 pOpenModeMB[i] = '\0';
3472 break;
3473 }
3474
3475 pOpenModeMB[i] = (char)pOpenMode[i];
3476 i += 1;
3477 }
3478 }
3479
3480 *ppFile = fopen(pFilePathMB, pOpenModeMB);
3481
3482 drmp3__free_from_callbacks(pFilePathMB, pAllocationCallbacks);
3483 }
3484 #endif
3485
3486 if (*ppFile == NULL) {
3487 return DRMP3_ERROR;
3488 }
3489 #endif
3490
3491 return DRMP3_SUCCESS;
3492 }
3493
3494
3495
3496 static size_t drmp3__on_read_stdio(void* pUserData, void* pBufferOut, size_t bytesToRead)
3497 {
3498 return fread(pBufferOut, 1, bytesToRead, (FILE*)pUserData);
3499 }
3500
3501 static drmp3_bool32 drmp3__on_seek_stdio(void* pUserData, int offset, drmp3_seek_origin origin)
3502 {
3503 return fseek((FILE*)pUserData, offset, (origin == drmp3_seek_origin_current) ? SEEK_CUR : SEEK_SET) == 0;
3504 }
3505
3506 DRMP3_API drmp3_bool32 drmp3_init_file(drmp3* pMP3, const char* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks)
3507 {
3508 drmp3_bool32 result;
3509 FILE* pFile;
3510
3511 if (drmp3_fopen(&pFile, pFilePath, "rb") != DRMP3_SUCCESS) {
3512 return DRMP3_FALSE;
3513 }
3514
3515 result = drmp3_init(pMP3, drmp3__on_read_stdio, drmp3__on_seek_stdio, (void*)pFile, pAllocationCallbacks);
3516 if (result != DRMP3_TRUE) {
3517 fclose(pFile);
3518 return result;
3519 }
3520
3521 return DRMP3_TRUE;
3522 }
3523
3524 DRMP3_API drmp3_bool32 drmp3_init_file_w(drmp3* pMP3, const wchar_t* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks)
3525 {
3526 drmp3_bool32 result;
3527 FILE* pFile;
3528
3529 if (drmp3_wfopen(&pFile, pFilePath, L"rb", pAllocationCallbacks) != DRMP3_SUCCESS) {
3530 return DRMP3_FALSE;
3531 }
3532
3533 result = drmp3_init(pMP3, drmp3__on_read_stdio, drmp3__on_seek_stdio, (void*)pFile, pAllocationCallbacks);
3534 if (result != DRMP3_TRUE) {
3535 fclose(pFile);
3536 return result;
3537 }
3538
3539 return DRMP3_TRUE;
3540 }
3541 #endif
3542
3543 DRMP3_API void drmp3_uninit(drmp3* pMP3)
3544 {
3545 if (pMP3 == NULL) {
3546 return;
3547 }
3548
3549 #ifndef DR_MP3_NO_STDIO
3550 if (pMP3->onRead == drmp3__on_read_stdio) {
3551 FILE* pFile = (FILE*)pMP3->pUserData;
3552 if (pFile != NULL) {
3553 fclose(pFile);
3554 pMP3->pUserData = NULL; /* Make sure the file handle is cleared to NULL to we don't attempt to close it a second time. */
3555 }
3556 }
3557 #endif
3558
3559 drmp3__free_from_callbacks(pMP3->pData, &pMP3->allocationCallbacks);
3560 }
3561
3562 #if defined(DR_MP3_FLOAT_OUTPUT)
3563 static void drmp3_f32_to_s16(drmp3_int16* dst, const float* src, drmp3_uint64 sampleCount)
3564 {
3565 drmp3_uint64 i;
3566 drmp3_uint64 i4;
3567 drmp3_uint64 sampleCount4;
3568
3569 /* Unrolled. */
3570 i = 0;
3571 sampleCount4 = sampleCount >> 2;
3572 for (i4 = 0; i4 < sampleCount4; i4 += 1) {
3573 float x0 = src[i+0];
3574 float x1 = src[i+1];
3575 float x2 = src[i+2];
3576 float x3 = src[i+3];
3577
3578 x0 = ((x0 < -1) ? -1 : ((x0 > 1) ? 1 : x0));
3579 x1 = ((x1 < -1) ? -1 : ((x1 > 1) ? 1 : x1));
3580 x2 = ((x2 < -1) ? -1 : ((x2 > 1) ? 1 : x2));
3581 x3 = ((x3 < -1) ? -1 : ((x3 > 1) ? 1 : x3));
3582
3583 x0 = x0 * 32767.0f;
3584 x1 = x1 * 32767.0f;
3585 x2 = x2 * 32767.0f;
3586 x3 = x3 * 32767.0f;
3587
3588 dst[i+0] = (drmp3_int16)x0;
3589 dst[i+1] = (drmp3_int16)x1;
3590 dst[i+2] = (drmp3_int16)x2;
3591 dst[i+3] = (drmp3_int16)x3;
3592
3593 i += 4;
3594 }
3595
3596 /* Leftover. */
3597 for (; i < sampleCount; i += 1) {
3598 float x = src[i];
3599 x = ((x < -1) ? -1 : ((x > 1) ? 1 : x)); /* clip */
3600 x = x * 32767.0f; /* -1..1 to -32767..32767 */
3601
3602 dst[i] = (drmp3_int16)x;
3603 }
3604 }
3605 #endif
3606
3607 #if !defined(DR_MP3_FLOAT_OUTPUT)
3608 static void drmp3_s16_to_f32(float* dst, const drmp3_int16* src, drmp3_uint64 sampleCount)
3609 {
3610 drmp3_uint64 i;
3611 for (i = 0; i < sampleCount; i += 1) {
3612 float x = (float)src[i];
3613 x = x * 0.000030517578125f; /* -32768..32767 to -1..0.999969482421875 */
3614 dst[i] = x;
3615 }
3616 }
3617 #endif
3618
3619
3620 static drmp3_uint64 drmp3_read_pcm_frames_raw(drmp3* pMP3, drmp3_uint64 framesToRead, void* pBufferOut)
3621 {
3622 drmp3_uint64 totalFramesRead = 0;
3623
3624 DRMP3_ASSERT(pMP3 != NULL);
3625 DRMP3_ASSERT(pMP3->onRead != NULL);
3626
3627 while (framesToRead > 0) {
3628 drmp3_uint32 framesToConsume = (drmp3_uint32)DRMP3_MIN(pMP3->pcmFramesRemainingInMP3Frame, framesToRead);
3629 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
3641 }
3642
3643 pMP3->currentPCMFrame += framesToConsume;
3644 pMP3->pcmFramesConsumedInMP3Frame += framesToConsume;
3645 pMP3->pcmFramesRemainingInMP3Frame -= framesToConsume;
3646 totalFramesRead += framesToConsume;
3647 framesToRead -= framesToConsume;
3648
3649 if (framesToRead == 0) {
3650 break;
3651 }
3652
3653 DRMP3_ASSERT(pMP3->pcmFramesRemainingInMP3Frame == 0);
3654
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 */
3659 if (drmp3_decode_next_frame(pMP3) == 0) {
3660 break;
3661 }
3662 }
3663
3664 return totalFramesRead;
3665 }
3666
3667
3668 DRMP3_API drmp3_uint64 drmp3_read_pcm_frames_f32(drmp3* pMP3, drmp3_uint64 framesToRead, float* pBufferOut)
3669 {
3670 if (pMP3 == NULL || pMP3->onRead == NULL) {
3671 return 0;
3672 }
3673
3674 #if defined(DR_MP3_FLOAT_OUTPUT)
3675 /* Fast path. No conversion required. */
3676 return drmp3_read_pcm_frames_raw(pMP3, framesToRead, pBufferOut);
3677 #else
3678 /* Slow path. Convert from s16 to f32. */
3679 {
3680 drmp3_int16 pTempS16[8192];
3681 drmp3_uint64 totalPCMFramesRead = 0;
3682
3683 while (totalPCMFramesRead < framesToRead) {
3684 drmp3_uint64 framesJustRead;
3685 drmp3_uint64 framesRemaining = framesToRead - totalPCMFramesRead;
3686 drmp3_uint64 framesToReadNow = DRMP3_COUNTOF(pTempS16) / pMP3->channels;
3687 if (framesToReadNow > framesRemaining) {
3688 framesToReadNow = framesRemaining;
3689 }
3690
3691 framesJustRead = drmp3_read_pcm_frames_raw(pMP3, framesToReadNow, pTempS16);
3692 if (framesJustRead == 0) {
3693 break;
3694 }
3695
3696 drmp3_s16_to_f32((float*)DRMP3_OFFSET_PTR(pBufferOut, sizeof(float) * totalPCMFramesRead * pMP3->channels), pTempS16, framesJustRead * pMP3->channels);
3697 totalPCMFramesRead += framesJustRead;
3698 }
3699
3700 return totalPCMFramesRead;
3701 }
3702 #endif
3703 }
3704
3705 DRMP3_API drmp3_uint64 drmp3_read_pcm_frames_s16(drmp3* pMP3, drmp3_uint64 framesToRead, drmp3_int16* pBufferOut)
3706 {
3707 if (pMP3 == NULL || pMP3->onRead == NULL) {
3708 return 0;
3709 }
3710
3711 #if !defined(DR_MP3_FLOAT_OUTPUT)
3712 /* Fast path. No conversion required. */
3713 return drmp3_read_pcm_frames_raw(pMP3, framesToRead, pBufferOut);
3714 #else
3715 /* Slow path. Convert from f32 to s16. */
3716 {
3717 float pTempF32[4096];
3718 drmp3_uint64 totalPCMFramesRead = 0;
3719
3720 while (totalPCMFramesRead < framesToRead) {
3721 drmp3_uint64 framesJustRead;
3722 drmp3_uint64 framesRemaining = framesToRead - totalPCMFramesRead;
3723 drmp3_uint64 framesToReadNow = DRMP3_COUNTOF(pTempF32) / pMP3->channels;
3724 if (framesToReadNow > framesRemaining) {
3725 framesToReadNow = framesRemaining;
3726 }
3727
3728 framesJustRead = drmp3_read_pcm_frames_raw(pMP3, framesToReadNow, pTempF32);
3729 if (framesJustRead == 0) {
3730 break;
3731 }
3732
3733 drmp3_f32_to_s16((drmp3_int16*)DRMP3_OFFSET_PTR(pBufferOut, sizeof(drmp3_int16) * totalPCMFramesRead * pMP3->channels), pTempF32, framesJustRead * pMP3->channels);
3734 totalPCMFramesRead += framesJustRead;
3735 }
3736
3737 return totalPCMFramesRead;
3738 }
3739 #endif
3740 }
3741
3742 static void drmp3_reset(drmp3* pMP3)
3743 {
3744 DRMP3_ASSERT(pMP3 != NULL);
3745
3746 pMP3->pcmFramesConsumedInMP3Frame = 0;
3747 pMP3->pcmFramesRemainingInMP3Frame = 0;
3748 pMP3->currentPCMFrame = 0;
3749 pMP3->dataSize = 0;
3750 pMP3->atEnd = DRMP3_FALSE;
3751 drmp3dec_init(&pMP3->decoder);
3752 }
3753
3754 static drmp3_bool32 drmp3_seek_to_start_of_stream(drmp3* pMP3)
3755 {
3756 DRMP3_ASSERT(pMP3 != NULL);
3757 DRMP3_ASSERT(pMP3->onSeek != NULL);
3758
3759 /* Seek to the start of the stream to begin with. */
3760 if (!drmp3__on_seek(pMP3, 0, drmp3_seek_origin_start)) {
3761 return DRMP3_FALSE;
3762 }
3763
3764 /* Clear any cached data. */
3765 drmp3_reset(pMP3);
3766 return DRMP3_TRUE;
3767 }
3768
3769
3770 static drmp3_bool32 drmp3_seek_forward_by_pcm_frames__brute_force(drmp3* pMP3, drmp3_uint64 frameOffset)
3771 {
3772 drmp3_uint64 framesRead;
3773
3774 /*
3775 Just using a dumb read-and-discard for now. What would be nice is to parse only the header of the MP3 frame, and then skip over leading
3776 frames without spending the time doing a full decode. I cannot see an easy way to do this in minimp3, however, so it may involve some
3777 kind of manual processing.
3778 */
3779 #if defined(DR_MP3_FLOAT_OUTPUT)
3780 framesRead = drmp3_read_pcm_frames_f32(pMP3, frameOffset, NULL);
3781 #else
3782 framesRead = drmp3_read_pcm_frames_s16(pMP3, frameOffset, NULL);
3783 #endif
3784 if (framesRead != frameOffset) {
3785 return DRMP3_FALSE;
3786 }
3787
3788 return DRMP3_TRUE;
3789 }
3790
3791 static drmp3_bool32 drmp3_seek_to_pcm_frame__brute_force(drmp3* pMP3, drmp3_uint64 frameIndex)
3792 {
3793 DRMP3_ASSERT(pMP3 != NULL);
3794
3795 if (frameIndex == pMP3->currentPCMFrame) {
3796 return DRMP3_TRUE;
3797 }
3798
3799 /*
3800 If we're moving foward we just read from where we're at. Otherwise we need to move back to the start of
3801 the stream and read from the beginning.
3802 */
3803 if (frameIndex < pMP3->currentPCMFrame) {
3804 /* Moving backward. Move to the start of the stream and then move forward. */
3805 if (!drmp3_seek_to_start_of_stream(pMP3)) {
3806 return DRMP3_FALSE;
3807 }
3808 }
3809
3810 DRMP3_ASSERT(frameIndex >= pMP3->currentPCMFrame);
3811 return drmp3_seek_forward_by_pcm_frames__brute_force(pMP3, (frameIndex - pMP3->currentPCMFrame));
3812 }
3813
3814 static drmp3_bool32 drmp3_find_closest_seek_point(drmp3* pMP3, drmp3_uint64 frameIndex, drmp3_uint32* pSeekPointIndex)
3815 {
3816 drmp3_uint32 iSeekPoint;
3817
3818 DRMP3_ASSERT(pSeekPointIndex != NULL);
3819
3820 *pSeekPointIndex = 0;
3821
3822 if (frameIndex < pMP3->pSeekPoints[0].pcmFrameIndex) {
3823 return DRMP3_FALSE;
3824 }
3825
3826 /* Linear search for simplicity to begin with while I'm getting this thing working. Once it's all working change this to a binary search. */
3827 for (iSeekPoint = 0; iSeekPoint < pMP3->seekPointCount; ++iSeekPoint) {
3828 if (pMP3->pSeekPoints[iSeekPoint].pcmFrameIndex > frameIndex) {
3829 break; /* Found it. */
3830 }
3831
3832 *pSeekPointIndex = iSeekPoint;
3833 }
3834
3835 return DRMP3_TRUE;
3836 }
3837
3838 static drmp3_bool32 drmp3_seek_to_pcm_frame__seek_table(drmp3* pMP3, drmp3_uint64 frameIndex)
3839 {
3840 drmp3_seek_point seekPoint;
3841 drmp3_uint32 priorSeekPointIndex;
3842 drmp3_uint16 iMP3Frame;
3843 drmp3_uint64 leftoverFrames;
3844
3845 DRMP3_ASSERT(pMP3 != NULL);
3846 DRMP3_ASSERT(pMP3->pSeekPoints != NULL);
3847 DRMP3_ASSERT(pMP3->seekPointCount > 0);
3848
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)) {
3851 seekPoint = pMP3->pSeekPoints[priorSeekPointIndex];
3852 } else {
3853 seekPoint.seekPosInBytes = 0;
3854 seekPoint.pcmFrameIndex = 0;
3855 seekPoint.mp3FramesToDiscard = 0;
3856 seekPoint.pcmFramesToDiscard = 0;
3857 }
3858
3859 /* 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)) {
3861 return DRMP3_FALSE; /* Failed to seek. */
3862 }
3863
3864 /* Clear any cached data. */
3865 drmp3_reset(pMP3);
3866
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;
3871
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;
3876 }
3877
3878 /* We first need to decode the next frame. */
3879 pcmFramesRead = drmp3_decode_next_frame_ex(pMP3, pPCMFrames);
3880 if (pcmFramesRead == 0) {
3881 return DRMP3_FALSE;
3882 }
3883 }
3884
3885 /* We seeked to an MP3 frame in the raw stream so we need to make sure the current PCM frame is set correctly. */
3886 pMP3->currentPCMFrame = seekPoint.pcmFrameIndex - seekPoint.pcmFramesToDiscard;
3887
3888 /*
3889 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 read-and-discard at least 2 whole MP3 frames.
3891 */
3892 leftoverFrames = frameIndex - pMP3->currentPCMFrame;
3893 return drmp3_seek_forward_by_pcm_frames__brute_force(pMP3, leftoverFrames);
3894 }
3895
3896 DRMP3_API drmp3_bool32 drmp3_seek_to_pcm_frame(drmp3* pMP3, drmp3_uint64 frameIndex)
3897 {
3898 if (pMP3 == NULL || pMP3->onSeek == NULL) {
3899 return DRMP3_FALSE;
3900 }
3901
3902 if (frameIndex == 0) {
3903 return drmp3_seek_to_start_of_stream(pMP3);
3904 }
3905
3906 /* Use the seek table if we have one. */
3907 if (pMP3->pSeekPoints != NULL && pMP3->seekPointCount > 0) {
3908 return drmp3_seek_to_pcm_frame__seek_table(pMP3, frameIndex);
3909 } else {
3910 return drmp3_seek_to_pcm_frame__brute_force(pMP3, frameIndex);
3911 }
3912 }
3913
3914 DRMP3_API drmp3_bool32 drmp3_get_mp3_and_pcm_frame_count(drmp3* pMP3, drmp3_uint64* pMP3FrameCount, drmp3_uint64* pPCMFrameCount)
3915 {
3916 drmp3_uint64 currentPCMFrame;
3917 drmp3_uint64 totalPCMFrameCount;
3918 drmp3_uint64 totalMP3FrameCount;
3919
3920 if (pMP3 == NULL) {
3921 return DRMP3_FALSE;
3922 }
3923
3924 /*
3925 The way this works is we move back to the start of the stream, iterate over each MP3 frame and calculate the frame count based
3926 on our output sample rate, the seek back to the PCM frame we were sitting on before calling this function.
3927 */
3928
3929 /* The stream must support seeking for this to work. */
3930 if (pMP3->onSeek == NULL) {
3931 return DRMP3_FALSE;
3932 }
3933
3934 /* 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
3937 if (!drmp3_seek_to_start_of_stream(pMP3)) {
3938 return DRMP3_FALSE;
3939 }
3940
3941 totalPCMFrameCount = 0;
3942 totalMP3FrameCount = 0;
3943
3944 for (;;) {
3945 drmp3_uint32 pcmFramesInCurrentMP3Frame;
3946
3947 pcmFramesInCurrentMP3Frame = drmp3_decode_next_frame_ex(pMP3, NULL);
3948 if (pcmFramesInCurrentMP3Frame == 0) {
3949 break;
3950 }
3951
3952 totalPCMFrameCount += pcmFramesInCurrentMP3Frame;
3953 totalMP3FrameCount += 1;
3954 }
3955
3956 /* Finally, we need to seek back to where we were. */
3957 if (!drmp3_seek_to_start_of_stream(pMP3)) {
3958 return DRMP3_FALSE;
3959 }
3960
3961 if (!drmp3_seek_to_pcm_frame(pMP3, currentPCMFrame)) {
3962 return DRMP3_FALSE;
3963 }
3964
3965 if (pMP3FrameCount != NULL) {
3966 *pMP3FrameCount = totalMP3FrameCount;
3967 }
3968 if (pPCMFrameCount != NULL) {
3969 *pPCMFrameCount = totalPCMFrameCount;
3970 }
3971
3972 return DRMP3_TRUE;
3973 }
3974
3975 DRMP3_API drmp3_uint64 drmp3_get_pcm_frame_count(drmp3* pMP3)
3976 {
3977 drmp3_uint64 totalPCMFrameCount;
3978 if (!drmp3_get_mp3_and_pcm_frame_count(pMP3, NULL, &totalPCMFrameCount)) {
3979 return 0;
3980 }
3981
3982 return totalPCMFrameCount;
3983 }
3984
3985 DRMP3_API drmp3_uint64 drmp3_get_mp3_frame_count(drmp3* pMP3)
3986 {
3987 drmp3_uint64 totalMP3FrameCount;
3988 if (!drmp3_get_mp3_and_pcm_frame_count(pMP3, &totalMP3FrameCount, NULL)) {
3989 return 0;
3990 }
3991
3992 return totalMP3FrameCount;
3993 }
3994
3995 static void drmp3__accumulate_running_pcm_frame_count(drmp3* pMP3, drmp3_uint32 pcmFrameCountIn, drmp3_uint64* pRunningPCMFrameCount, float* pRunningPCMFrameCountFractionalPart)
3996 {
3997 float srcRatio;
3998 float pcmFrameCountOutF;
3999 drmp3_uint32 pcmFrameCountOut;
4000
4001 srcRatio = (float)pMP3->mp3FrameSampleRate / (float)pMP3->sampleRate;
4002 DRMP3_ASSERT(srcRatio > 0);
4003
4004 pcmFrameCountOutF = *pRunningPCMFrameCountFractionalPart + (pcmFrameCountIn / srcRatio);
4005 pcmFrameCountOut = (drmp3_uint32)pcmFrameCountOutF;
4006 *pRunningPCMFrameCountFractionalPart = pcmFrameCountOutF - pcmFrameCountOut;
4007 *pRunningPCMFrameCount += pcmFrameCountOut;
4008 }
4009
4010 typedef struct
4011 {
4012 drmp3_uint64 bytePos;
4013 drmp3_uint64 pcmFrameIndex; /* <-- After sample rate conversion. */
4014 } drmp3__seeking_mp3_frame_info;
4015
4016 DRMP3_API drmp3_bool32 drmp3_calculate_seek_points(drmp3* pMP3, drmp3_uint32* pSeekPointCount, drmp3_seek_point* pSeekPoints)
4017 {
4018 drmp3_uint32 seekPointCount;
4019 drmp3_uint64 currentPCMFrame;
4020 drmp3_uint64 totalMP3FrameCount;
4021 drmp3_uint64 totalPCMFrameCount;
4022
4023 if (pMP3 == NULL || pSeekPointCount == NULL || pSeekPoints == NULL) {
4024 return DRMP3_FALSE; /* Invalid args. */
4025 }
4026
4027 seekPointCount = *pSeekPointCount;
4028 if (seekPointCount == 0) {
4029 return DRMP3_FALSE; /* The client has requested no seek points. Consider this to be invalid arguments since the client has probably not intended this. */
4030 }
4031
4032 /* 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
4035 /* We never do more than the total number of MP3 frames and we limit it to 32-bits. */
4036 if (!drmp3_get_mp3_and_pcm_frame_count(pMP3, &totalMP3FrameCount, &totalPCMFrameCount)) {
4037 return DRMP3_FALSE;
4038 }
4039
4040 /* 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 if (totalMP3FrameCount < DRMP3_SEEK_LEADING_MP3_FRAMES+1) {
4042 seekPointCount = 1;
4043 pSeekPoints[0].seekPosInBytes = 0;
4044 pSeekPoints[0].pcmFrameIndex = 0;
4045 pSeekPoints[0].mp3FramesToDiscard = 0;
4046 pSeekPoints[0].pcmFramesToDiscard = 0;
4047 } else {
4048 drmp3_uint64 pcmFramesBetweenSeekPoints;
4049 drmp3__seeking_mp3_frame_info mp3FrameInfo[DRMP3_SEEK_LEADING_MP3_FRAMES+1];
4050 drmp3_uint64 runningPCMFrameCount = 0;
4051 float runningPCMFrameCountFractionalPart = 0;
4052 drmp3_uint64 nextTargetPCMFrame;
4053 drmp3_uint32 iMP3Frame;
4054 drmp3_uint32 iSeekPoint;
4055
4056 if (seekPointCount > totalMP3FrameCount-1) {
4057 seekPointCount = (drmp3_uint32)totalMP3FrameCount-1;
4058 }
4059
4060 pcmFramesBetweenSeekPoints = totalPCMFrameCount / (seekPointCount+1);
4061
4062 /*
4063 Here is where we actually calculate the seek points. We need to start by moving the start of the stream. We then enumerate over each
4064 MP3 frame.
4065 */
4066 if (!drmp3_seek_to_start_of_stream(pMP3)) {
4067 return DRMP3_FALSE;
4068 }
4069
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 nextTargetPCMFrame = 0;
4098 for (iSeekPoint = 0; iSeekPoint < seekPointCount; ++iSeekPoint) {
4099 nextTargetPCMFrame += pcmFramesBetweenSeekPoints;
4100
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 {
4110 size_t i;
4111 drmp3_uint32 pcmFramesInCurrentMP3FrameIn;
4112
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 for (i = 0; i < DRMP3_COUNTOF(mp3FrameInfo)-1; ++i) {
4118 mp3FrameInfo[i] = mp3FrameInfo[i+1];
4119 }
4120
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 }
4137
4138 drmp3__accumulate_running_pcm_frame_count(pMP3, pcmFramesInCurrentMP3FrameIn, &runningPCMFrameCount, &runningPCMFrameCountFractionalPart);
4139 }
4140 }
4141 }
4142
4143 /* Finally, we need to seek back to where we were. */
4144 if (!drmp3_seek_to_start_of_stream(pMP3)) {
4145 return DRMP3_FALSE;
4146 }
4147 if (!drmp3_seek_to_pcm_frame(pMP3, currentPCMFrame)) {
4148 return DRMP3_FALSE;
4149 }
4150 }
4151
4152 *pSeekPointCount = seekPointCount;
4153 return DRMP3_TRUE;
4154 }
4155
4156 DRMP3_API drmp3_bool32 drmp3_bind_seek_table(drmp3* pMP3, drmp3_uint32 seekPointCount, drmp3_seek_point* pSeekPoints)
4157 {
4158 if (pMP3 == NULL) {
4159 return DRMP3_FALSE;
4160 }
4161
4162 if (seekPointCount == 0 || pSeekPoints == NULL) {
4163 /* Unbinding. */
4164 pMP3->seekPointCount = 0;
4165 pMP3->pSeekPoints = NULL;
4166 } else {
4167 /* Binding. */
4168 pMP3->seekPointCount = seekPointCount;
4169 pMP3->pSeekPoints = pSeekPoints;
4170 }
4171
4172 return DRMP3_TRUE;
4173 }
4174
4175
4176 static float* drmp3__full_read_and_close_f32(drmp3* pMP3, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount)
4177 {
4178 drmp3_uint64 totalFramesRead = 0;
4179 drmp3_uint64 framesCapacity = 0;
4180 float* pFrames = NULL;
4181 float temp[4096];
4182
4183 DRMP3_ASSERT(pMP3 != NULL);
4184
4185 for (;;) {
4186 drmp3_uint64 framesToReadRightNow = DRMP3_COUNTOF(temp) / pMP3->channels;
4187 drmp3_uint64 framesJustRead = drmp3_read_pcm_frames_f32(pMP3, framesToReadRightNow, temp);
4188 if (framesJustRead == 0) {
4189 break;
4190 }
4191
4192 /* Reallocate the output buffer if there's not enough room. */
4193 if (framesCapacity < totalFramesRead + framesJustRead) {
4194 drmp3_uint64 oldFramesBufferSize;
4195 drmp3_uint64 newFramesBufferSize;
4196 drmp3_uint64 newFramesCap;
4197 float* pNewFrames;
4198
4199 newFramesCap = framesCapacity * 2;
4200 if (newFramesCap < totalFramesRead + framesJustRead) {
4201 newFramesCap = totalFramesRead + framesJustRead;
4202 }
4203
4204 oldFramesBufferSize = framesCapacity * pMP3->channels * sizeof(float);
4205 newFramesBufferSize = newFramesCap * pMP3->channels * sizeof(float);
4206 if (newFramesBufferSize > (drmp3_uint64)DRMP3_SIZE_MAX) {
4207 break;
4208 }
4209
4210 pNewFrames = (float*)drmp3__realloc_from_callbacks(pFrames, (size_t)newFramesBufferSize, (size_t)oldFramesBufferSize, &pMP3->allocationCallbacks);
4211 if (pNewFrames == NULL) {
4212 drmp3__free_from_callbacks(pFrames, &pMP3->allocationCallbacks);
4213 break;
4214 }
4215
4216 pFrames = pNewFrames;
4217 framesCapacity = newFramesCap;
4218 }
4219
4220 DRMP3_COPY_MEMORY(pFrames + totalFramesRead*pMP3->channels, temp, (size_t)(framesJustRead*pMP3->channels*sizeof(float)));
4221 totalFramesRead += framesJustRead;
4222
4223 /* If the number of frames we asked for is less that what we actually read it means we've reached the end. */
4224 if (framesJustRead != framesToReadRightNow) {
4225 break;
4226 }
4227 }
4228
4229 if (pConfig != NULL) {
4230 pConfig->channels = pMP3->channels;
4231 pConfig->sampleRate = pMP3->sampleRate;
4232 }
4233
4234 drmp3_uninit(pMP3);
4235
4236 if (pTotalFrameCount) {
4237 *pTotalFrameCount = totalFramesRead;
4238 }
4239
4240 return pFrames;
4241 }
4242
4243 static drmp3_int16* drmp3__full_read_and_close_s16(drmp3* pMP3, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount)
4244 {
4245 drmp3_uint64 totalFramesRead = 0;
4246 drmp3_uint64 framesCapacity = 0;
4247 drmp3_int16* pFrames = NULL;
4248 drmp3_int16 temp[4096];
4249
4250 DRMP3_ASSERT(pMP3 != NULL);
4251
4252 for (;;) {
4253 drmp3_uint64 framesToReadRightNow = DRMP3_COUNTOF(temp) / pMP3->channels;
4254 drmp3_uint64 framesJustRead = drmp3_read_pcm_frames_s16(pMP3, framesToReadRightNow, temp);
4255 if (framesJustRead == 0) {
4256 break;
4257 }
4258
4259 /* Reallocate the output buffer if there's not enough room. */
4260 if (framesCapacity < totalFramesRead + framesJustRead) {
4261 drmp3_uint64 newFramesBufferSize;
4262 drmp3_uint64 oldFramesBufferSize;
4263 drmp3_uint64 newFramesCap;
4264 drmp3_int16* pNewFrames;
4265
4266 newFramesCap = framesCapacity * 2;
4267 if (newFramesCap < totalFramesRead + framesJustRead) {
4268 newFramesCap = totalFramesRead + framesJustRead;
4269 }
4270
4271 oldFramesBufferSize = framesCapacity * pMP3->channels * sizeof(drmp3_int16);
4272 newFramesBufferSize = newFramesCap * pMP3->channels * sizeof(drmp3_int16);
4273 if (newFramesBufferSize > (drmp3_uint64)DRMP3_SIZE_MAX) {
4274 break;
4275 }
4276
4277 pNewFrames = (drmp3_int16*)drmp3__realloc_from_callbacks(pFrames, (size_t)newFramesBufferSize, (size_t)oldFramesBufferSize, &pMP3->allocationCallbacks);
4278 if (pNewFrames == NULL) {
4279 drmp3__free_from_callbacks(pFrames, &pMP3->allocationCallbacks);
4280 break;
4281 }
4282
4283 pFrames = pNewFrames;
4284 framesCapacity = newFramesCap;
4285 }
4286
4287 DRMP3_COPY_MEMORY(pFrames + totalFramesRead*pMP3->channels, temp, (size_t)(framesJustRead*pMP3->channels*sizeof(drmp3_int16)));
4288 totalFramesRead += framesJustRead;
4289
4290 /* If the number of frames we asked for is less that what we actually read it means we've reached the end. */
4291 if (framesJustRead != framesToReadRightNow) {
4292 break;
4293 }
4294 }
4295
4296 if (pConfig != NULL) {
4297 pConfig->channels = pMP3->channels;
4298 pConfig->sampleRate = pMP3->sampleRate;
4299 }
4300
4301 drmp3_uninit(pMP3);
4302
4303 if (pTotalFrameCount) {
4304 *pTotalFrameCount = totalFramesRead;
4305 }
4306
4307 return pFrames;
4308 }
4309
4310
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)
4312 {
4313 drmp3 mp3;
4314 if (!drmp3_init(&mp3, onRead, onSeek, pUserData, pAllocationCallbacks)) {
4315 return NULL;
4316 }
4317
4318 return drmp3__full_read_and_close_f32(&mp3, pConfig, pTotalFrameCount);
4319 }
4320
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)
4322 {
4323 drmp3 mp3;
4324 if (!drmp3_init(&mp3, onRead, onSeek, pUserData, pAllocationCallbacks)) {
4325 return NULL;
4326 }
4327
4328 return drmp3__full_read_and_close_s16(&mp3, pConfig, pTotalFrameCount);
4329 }
4330
4331
4332 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)
4333 {
4334 drmp3 mp3;
4335 if (!drmp3_init_memory(&mp3, pData, dataSize, pAllocationCallbacks)) {
4336 return NULL;
4337 }
4338
4339 return drmp3__full_read_and_close_f32(&mp3, pConfig, pTotalFrameCount);
4340 }
4341
4342 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)
4343 {
4344 drmp3 mp3;
4345 if (!drmp3_init_memory(&mp3, pData, dataSize, pAllocationCallbacks)) {
4346 return NULL;
4347 }
4348
4349 return drmp3__full_read_and_close_s16(&mp3, pConfig, pTotalFrameCount);
4350 }
4351
4352
4353 #ifndef DR_MP3_NO_STDIO
4354 DRMP3_API float* drmp3_open_file_and_read_pcm_frames_f32(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks)
4355 {
4356 drmp3 mp3;
4357 if (!drmp3_init_file(&mp3, filePath, pAllocationCallbacks)) {
4358 return NULL;
4359 }
4360
4361 return drmp3__full_read_and_close_f32(&mp3, pConfig, pTotalFrameCount);
4362 }
4363
4364 DRMP3_API drmp3_int16* drmp3_open_file_and_read_pcm_frames_s16(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks)
4365 {
4366 drmp3 mp3;
4367 if (!drmp3_init_file(&mp3, filePath, pAllocationCallbacks)) {
4368 return NULL;
4369 }
4370
4371 return drmp3__full_read_and_close_s16(&mp3, pConfig, pTotalFrameCount);
4372 }
4373 #endif
4374
4375 DRMP3_API void* drmp3_malloc(size_t sz, const drmp3_allocation_callbacks* pAllocationCallbacks)
4376 {
4377 if (pAllocationCallbacks != NULL) {
4378 return drmp3__malloc_from_callbacks(sz, pAllocationCallbacks);
4379 } else {
4380 return drmp3__malloc_default(sz, NULL);
4381 }
4382 }
4383
4384 DRMP3_API void drmp3_free(void* p, const drmp3_allocation_callbacks* pAllocationCallbacks)
4385 {
4386 if (pAllocationCallbacks != NULL) {
4387 drmp3__free_from_callbacks(p, pAllocationCallbacks);
4388 } else {
4389 drmp3__free_default(p, NULL);
4390 }
4391 }
4392
4393 #endif /* dr_mp3_c */
4394 #endif /*DR_MP3_IMPLEMENTATION*/
4395
4396 /*
4397 DIFFERENCES BETWEEN minimp3 AND dr_mp3
4398 ======================================
4399 - First, keep in mind that minimp3 (https://github.com/lieff/minimp3) is where all the real work was done. All of the
4400 code relating to the actual decoding remains mostly unmodified, apart from some namespacing changes.
4401 - dr_mp3 adds a pulling style API which allows you to deliver raw data via callbacks. So, rather than pushing data
4402 to the decoder, the decoder _pulls_ data from your callbacks.
4403 - In addition to callbacks, a decoder can be initialized from a block of memory and a file.
4404 - The dr_mp3 pull API reads PCM frames rather than whole MP3 frames.
4405 - dr_mp3 adds convenience APIs for opening and decoding entire files in one go.
4406 - dr_mp3 is fully namespaced, including the implementation section, which is more suitable when compiling projects
4407 as a single translation unit (aka unity builds). At the time of writing this, a unity build is not possible when
4408 using minimp3 in conjunction with stb_vorbis. dr_mp3 addresses this.
4409 */
4410
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 /*
4749 This software is available as a choice of the following licenses. Choose
4750 whichever you prefer.
4751
4752 ===============================================================================
4753 ALTERNATIVE 1 - Public Domain (www.unlicense.org)
4754 ===============================================================================
4755 This is free and unencumbered software released into the public domain.
4756
4757 Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
4758 software, either in source code form or as a compiled binary, for any purpose,
4759 commercial or non-commercial, and by any means.
4760
4761 In jurisdictions that recognize copyright laws, the author or authors of this
4762 software dedicate any and all copyright interest in the software to the public
4763 domain. We make this dedication for the benefit of the public at large and to
4764 the detriment of our heirs and successors. We intend this dedication to be an
4765 overt act of relinquishment in perpetuity of all present and future rights to
4766 this software under copyright law.
4767
4768 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
4769 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
4770 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
4771 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
4772 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
4773 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4774
4775 For more information, please refer to <http://unlicense.org/>
4776
4777 ===============================================================================
4778 ALTERNATIVE 2 - MIT No Attribution
4779 ===============================================================================
4780 Copyright 2020 David Reid
4781
4782 Permission is hereby granted, free of charge, to any person obtaining a copy of
4783 this software and associated documentation files (the "Software"), to deal in
4784 the Software without restriction, including without limitation the rights to
4785 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
4786 of the Software, and to permit persons to whom the Software is furnished to do
4787 so.
4788
4789 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
4790 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
4791 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
4792 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
4793 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
4794 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
4795 SOFTWARE.
4796 */
4797
4798 /*
4799 https://github.com/lieff/minimp3
4800 To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide.
4801 This software is distributed without any warranty.
4802 See <http://creativecommons.org/publicdomain/zero/1.0/>.
4803 */
4804