vkQuake2 doxygen  1.0 dev
dr_mp3.h
Go to the documentation of this file.
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.4.4 - 2019-05-06
4 
5 David Reid - mackron@gmail.com
6 
7 Based off minimp3 (https://github.com/lieff/minimp3) which is where the real work was done. See the bottom of this file for
8 differences between minimp3 and dr_mp3.
9 */
10 
11 /*
12 USAGE
13 =====
14 dr_mp3 is a single-file library. To use it, do something like the following in one .c file.
15  #define DR_MP3_IMPLEMENTATION
16  #include "dr_mp3.h"
17 
18 You can then #include this file in other parts of the program as you would with any other header file. To decode audio data,
19 do something like the following:
20 
21  drmp3 mp3;
22  if (!drmp3_init_file(&mp3, "MySong.mp3", NULL)) {
23  // Failed to open file
24  }
25 
26  ...
27 
28  drmp3_uint64 framesRead = drmp3_read_pcm_frames_f32(pMP3, framesToRead, pFrames);
29 
30 The drmp3 object is transparent so you can get access to the channel count and sample rate like so:
31 
32  drmp3_uint32 channels = mp3.channels;
33  drmp3_uint32 sampleRate = mp3.sampleRate;
34 
35 The third parameter of drmp3_init_file() in the example above allows you to control the output channel count and sample rate. It
36 is a pointer to a drmp3_config object. Setting any of the variables of this object to 0 will cause dr_mp3 to use defaults.
37 
38 The example above initializes a decoder from a file, but you can also initialize it from a block of memory and read and seek
39 callbacks with drmp3_init_memory() and drmp3_init() respectively.
40 
41 You do not need to do any annoying memory management when reading PCM frames - this is all managed internally. You can request
42 any number of PCM frames in each call to drmp3_read_pcm_frames_f32() and it will return as many PCM frames as it can, up to the
43 requested amount.
44 
45 You can also decode an entire file in one go with drmp3_open_and_read_f32(), drmp3_open_memory_and_read_f32() and
46 drmp3_open_file_and_read_f32().
47 
48 
49 OPTIONS
50 =======
51 #define these options before including this file.
52 
53 #define DR_MP3_NO_STDIO
54  Disable drmp3_init_file(), etc.
55 
56 #define DR_MP3_NO_SIMD
57  Disable SIMD optimizations.
58 */
59 
60 #ifndef dr_mp3_h
61 #define dr_mp3_h
62 
63 #ifdef __cplusplus
64 extern "C" {
65 #endif
66 
67 #include <stddef.h>
68 
69 #if defined(_MSC_VER) && _MSC_VER < 1600
70 typedef signed char drmp3_int8;
71 typedef unsigned char drmp3_uint8;
72 typedef signed short drmp3_int16;
73 typedef unsigned short drmp3_uint16;
74 typedef signed int drmp3_int32;
75 typedef unsigned int drmp3_uint32;
76 typedef signed __int64 drmp3_int64;
77 typedef unsigned __int64 drmp3_uint64;
78 #else
79 #include <stdint.h>
80 typedef int8_t drmp3_int8;
81 typedef uint8_t drmp3_uint8;
82 typedef int16_t drmp3_int16;
83 typedef uint16_t drmp3_uint16;
84 typedef int32_t drmp3_int32;
85 typedef uint32_t drmp3_uint32;
86 typedef int64_t drmp3_int64;
87 typedef uint64_t drmp3_uint64;
88 #endif
91 #define DRMP3_TRUE 1
92 #define DRMP3_FALSE 0
93 
94 #define DRMP3_MAX_PCM_FRAMES_PER_MP3_FRAME 1152
95 #define DRMP3_MAX_SAMPLES_PER_FRAME (DRMP3_MAX_PCM_FRAMES_PER_MP3_FRAME*2)
96 
97 #ifdef _MSC_VER
98 #define DRMP3_INLINE __forceinline
99 #else
100 #ifdef __GNUC__
101 #define DRMP3_INLINE __inline__ __attribute__((always_inline))
102 #else
103 #define DRMP3_INLINE
104 #endif
105 #endif
106 
107 /*
108 Low Level Push API
109 ==================
110 */
111 typedef struct
112 {
113  int frame_bytes, channels, hz, layer, bitrate_kbps;
115 
116 typedef struct
117 {
118  float mdct_overlap[2][9*32], qmf_state[15*2*32];
119  int reserv, free_format_bytes;
120  unsigned char header[4], reserv_buf[511];
121 } drmp3dec;
122 
123 /* Initializes a low level decoder. */
124 void drmp3dec_init(drmp3dec *dec);
125 
126 /* Reads a frame from a low level decoder. */
127 int drmp3dec_decode_frame(drmp3dec *dec, const unsigned char *mp3, int mp3_bytes, void *pcm, drmp3dec_frame_info *info);
128 
129 /* Helper for converting between f32 and s16. */
130 void drmp3dec_f32_to_s16(const float *in, drmp3_int16 *out, int num_samples);
131 
132 
133 
134 /*
135 Main API (Pull API)
136 ===================
137 */
138 #ifndef DR_MP3_DEFAULT_CHANNELS
139 #define DR_MP3_DEFAULT_CHANNELS 2
140 #endif
141 #ifndef DR_MP3_DEFAULT_SAMPLE_RATE
142 #define DR_MP3_DEFAULT_SAMPLE_RATE 44100
143 #endif
144 
145 typedef struct drmp3_src drmp3_src;
146 typedef drmp3_uint64 (* drmp3_src_read_proc)(drmp3_src* pSRC, drmp3_uint64 frameCount, void* pFramesOut, void* pUserData); /* Returns the number of frames that were read. */
147 
148 typedef enum
149 {
153 
154 #define DRMP3_SRC_CACHE_SIZE_IN_FRAMES 512
155 typedef struct
156 {
158  float pCachedFrames[2 * DRMP3_SRC_CACHE_SIZE_IN_FRAMES];
162 
163 typedef struct
164 {
169  drmp3_uint32 cacheSizeInFrames; /* The number of frames to read from the client at a time. */
171 
172 struct drmp3_src
173 {
176  void* pUserData;
177  float bin[256];
178  drmp3_src_cache cache; /* <-- For simplifying and optimizing client -> memory reading. */
179  union
180  {
181  struct
182  {
183  double alpha;
186  } linear;
187  } algo;
188 };
189 
190 typedef enum
191 {
195 
196 typedef struct
197 {
198  drmp3_uint64 seekPosInBytes; /* Points to the first byte of an MP3 frame. */
199  drmp3_uint64 pcmFrameIndex; /* The index of the PCM frame this seek point targets. */
200  drmp3_uint16 mp3FramesToDiscard; /* The number of whole MP3 frames to be discarded before pcmFramesToDiscard. */
201  drmp3_uint16 pcmFramesToDiscard; /* The number of leading samples to read and discard. These are discarded after mp3FramesToDiscard. */
203 
204 /*
205 Callback for when data is read. Return value is the number of bytes actually read.
206 
207 pUserData [in] The user data that was passed to drmp3_init(), drmp3_open() and family.
208 pBufferOut [out] The output buffer.
209 bytesToRead [in] The number of bytes to read.
210 
211 Returns the number of bytes actually read.
212 
213 A return value of less than bytesToRead indicates the end of the stream. Do _not_ return from this callback until
214 either the entire bytesToRead is filled or you have reached the end of the stream.
215 */
216 typedef size_t (* drmp3_read_proc)(void* pUserData, void* pBufferOut, size_t bytesToRead);
217 
218 /*
219 Callback for when data needs to be seeked.
220 
221 pUserData [in] The user data that was passed to drmp3_init(), drmp3_open() and family.
222 offset [in] The number of bytes to move, relative to the origin. Will never be negative.
223 origin [in] The origin of the seek - the current position or the start of the stream.
224 
225 Returns whether or not the seek was successful.
226 
227 Whether or not it is relative to the beginning or current position is determined by the "origin" parameter which
228 will be either drmp3_seek_origin_start or drmp3_seek_origin_current.
229 */
230 typedef drmp3_bool32 (* drmp3_seek_proc)(void* pUserData, int offset, drmp3_seek_origin origin);
231 
232 typedef struct
233 {
236 } drmp3_config;
237 
238 typedef struct
239 {
246  void* pUserData;
247  drmp3_uint32 mp3FrameChannels; /* The number of channels in the currently loaded MP3 frame. Internal use only. */
248  drmp3_uint32 mp3FrameSampleRate; /* The sample rate of the currently loaded MP3 frame. Internal use only. */
251  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. */
252  drmp3_uint64 currentPCMFrame; /* The current PCM frame, globally, based on the output sample rate. Mainly used for seeking. */
253  drmp3_uint64 streamCursor; /* The current byte the decoder is sitting on in the raw stream. */
255  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. */
256  drmp3_uint32 seekPointCount; /* The number of items in pSeekPoints. When set to 0 assumes to no seek table. Defaults to zero. */
257  size_t dataSize;
258  size_t dataCapacity;
261  struct
262  {
264  size_t dataSize;
266  } memory; /* Only used for decoders that were opened against a block of memory. */
267 } drmp3;
268 
269 /*
270 Initializes an MP3 decoder.
271 
272 onRead [in] The function to call when data needs to be read from the client.
273 onSeek [in] The function to call when the read position of the client data needs to move.
274 pUserData [in, optional] A pointer to application defined data that will be passed to onRead and onSeek.
275 
276 Returns true if successful; false otherwise.
277 
278 Close the loader with drmp3_uninit().
279 
280 See also: drmp3_init_file(), drmp3_init_memory(), drmp3_uninit()
281 */
282 drmp3_bool32 drmp3_init(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, const drmp3_config* pConfig);
283 
284 /*
285 Initializes an MP3 decoder from a block of memory.
286 
287 This does not create a copy of the data. It is up to the application to ensure the buffer remains valid for
288 the lifetime of the drmp3 object.
289 
290 The buffer should contain the contents of the entire MP3 file.
291 */
292 drmp3_bool32 drmp3_init_memory(drmp3* pMP3, const void* pData, size_t dataSize, const drmp3_config* pConfig);
293 
294 #ifndef DR_MP3_NO_STDIO
295 /*
296 Initializes an MP3 decoder from a file.
297 
298 This holds the internal FILE object until drmp3_uninit() is called. Keep this in mind if you're caching drmp3
299 objects because the operating system may restrict the number of file handles an application can have open at
300 any given time.
301 */
302 drmp3_bool32 drmp3_init_file(drmp3* pMP3, const char* filePath, const drmp3_config* pConfig);
303 #endif
304 
305 /*
306 Uninitializes an MP3 decoder.
307 */
308 void drmp3_uninit(drmp3* pMP3);
309 
310 /*
311 Reads PCM frames as interleaved 32-bit IEEE floating point PCM.
312 
313 Note that framesToRead specifies the number of PCM frames to read, _not_ the number of MP3 frames.
314 */
315 drmp3_uint64 drmp3_read_pcm_frames_f32(drmp3* pMP3, drmp3_uint64 framesToRead, float* pBufferOut);
316 
317 /*
318 Reads PCM frames as interleaved signed 16-bit integer PCM.
319 
320 Note that framesToRead specifies the number of PCM frames to read, _not_ the number of MP3 frames.
321 */
322 drmp3_uint64 drmp3_read_pcm_frames_s16(drmp3* pMP3, drmp3_uint64 framesToRead, drmp3_int16* pBufferOut);
323 
324 /*
325 Seeks to a specific frame.
326 
327 Note that this is _not_ an MP3 frame, but rather a PCM frame.
328 */
330 
331 /*
332 Calculates the total number of PCM frames in the MP3 stream. Cannot be used for infinite streams such as internet
333 radio. Runs in linear time. Returns 0 on error.
334 */
336 
337 /*
338 Calculates the total number of MP3 frames in the MP3 stream. Cannot be used for infinite streams such as internet
339 radio. Runs in linear time. Returns 0 on error.
340 */
342 
343 /*
344 Calculates the total number of MP3 and PCM frames in the MP3 stream. Cannot be used for infinite streams such as internet
345 radio. Runs in linear time. Returns 0 on error.
346 
347 This is equivalent to calling drmp3_get_mp3_frame_count() and drmp3_get_pcm_frame_count() except that it's more efficient.
348 */
349 drmp3_bool32 drmp3_get_mp3_and_pcm_frame_count(drmp3* pMP3, drmp3_uint64* pMP3FrameCount, drmp3_uint64* pPCMFrameCount);
350 
351 /*
352 Calculates the seekpoints based on PCM frames. This is slow.
353 
354 pSeekpoint count is a pointer to a uint32 containing the seekpoint count. On input it contains the desired count.
355 On output it contains the actual count. The reason for this design is that the client may request too many
356 seekpoints, in which case dr_mp3 will return a corrected count.
357 
358 Note that seektable seeking is not quite sample exact when the MP3 stream contains inconsistent sample rates.
359 */
360 drmp3_bool32 drmp3_calculate_seek_points(drmp3* pMP3, drmp3_uint32* pSeekPointCount, drmp3_seek_point* pSeekPoints);
361 
362 /*
363 Binds a seek table to the decoder.
364 
365 This does _not_ make a copy of pSeekPoints - it only references it. It is up to the application to ensure this
366 remains valid while it is bound to the decoder.
367 
368 Use drmp3_calculate_seek_points() to calculate the seek points.
369 */
370 drmp3_bool32 drmp3_bind_seek_table(drmp3* pMP3, drmp3_uint32 seekPointCount, drmp3_seek_point* pSeekPoints);
371 
372 
373 /*
374 Opens an decodes an entire MP3 stream as a single operation.
375 
376 pConfig is both an input and output. On input it contains what you want. On output it contains what you got.
377 
378 Free the returned pointer with drmp3_free().
379 */
380 float* drmp3_open_and_read_f32(drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount);
381 drmp3_int16* drmp3_open_and_read_s16(drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount);
382 
383 float* drmp3_open_memory_and_read_f32(const void* pData, size_t dataSize, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount);
384 drmp3_int16* drmp3_open_memory_and_read_s16(const void* pData, size_t dataSize, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount);
385 
386 #ifndef DR_MP3_NO_STDIO
387 float* drmp3_open_file_and_read_f32(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount);
388 drmp3_int16* drmp3_open_file_and_read_s16(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount);
389 #endif
390 
391 /*
392 Frees any memory that was allocated by a public drmp3 API.
393 */
394 void drmp3_free(void* p);
395 
396 #ifdef __cplusplus
397 }
398 #endif
399 #endif /* dr_mp3_h */
400 
401 
402 /************************************************************************************************************************************************************
403  ************************************************************************************************************************************************************
404 
405  IMPLEMENTATION
406 
407  ************************************************************************************************************************************************************
408  ************************************************************************************************************************************************************/
409 #ifdef DR_MP3_IMPLEMENTATION
410 #include <stdlib.h>
411 #include <string.h>
412 #include <limits.h> /* For INT_MAX */
413 
414 /* Disable SIMD when compiling with TCC for now. */
415 #if defined(__TINYC__)
416 #define DR_MP3_NO_SIMD
417 #endif
418 
419 #define DRMP3_OFFSET_PTR(p, offset) ((void*)((drmp3_uint8*)(p) + (offset)))
420 
421 #define DRMP3_MAX_FREE_FORMAT_FRAME_SIZE 2304 /* more than ISO spec's */
422 #ifndef DRMP3_MAX_FRAME_SYNC_MATCHES
423 #define DRMP3_MAX_FRAME_SYNC_MATCHES 10
424 #endif
425 
426 #define DRMP3_MAX_L3_FRAME_PAYLOAD_BYTES DRMP3_MAX_FREE_FORMAT_FRAME_SIZE /* MUST be >= 320000/8/32000*1152 = 1440 */
427 
428 #define DRMP3_MAX_BITRESERVOIR_BYTES 511
429 #define DRMP3_SHORT_BLOCK_TYPE 2
430 #define DRMP3_STOP_BLOCK_TYPE 3
431 #define DRMP3_MODE_MONO 3
432 #define DRMP3_MODE_JOINT_STEREO 1
433 #define DRMP3_HDR_SIZE 4
434 #define DRMP3_HDR_IS_MONO(h) (((h[3]) & 0xC0) == 0xC0)
435 #define DRMP3_HDR_IS_MS_STEREO(h) (((h[3]) & 0xE0) == 0x60)
436 #define DRMP3_HDR_IS_FREE_FORMAT(h) (((h[2]) & 0xF0) == 0)
437 #define DRMP3_HDR_IS_CRC(h) (!((h[1]) & 1))
438 #define DRMP3_HDR_TEST_PADDING(h) ((h[2]) & 0x2)
439 #define DRMP3_HDR_TEST_MPEG1(h) ((h[1]) & 0x8)
440 #define DRMP3_HDR_TEST_NOT_MPEG25(h) ((h[1]) & 0x10)
441 #define DRMP3_HDR_TEST_I_STEREO(h) ((h[3]) & 0x10)
442 #define DRMP3_HDR_TEST_MS_STEREO(h) ((h[3]) & 0x20)
443 #define DRMP3_HDR_GET_STEREO_MODE(h) (((h[3]) >> 6) & 3)
444 #define DRMP3_HDR_GET_STEREO_MODE_EXT(h) (((h[3]) >> 4) & 3)
445 #define DRMP3_HDR_GET_LAYER(h) (((h[1]) >> 1) & 3)
446 #define DRMP3_HDR_GET_BITRATE(h) ((h[2]) >> 4)
447 #define DRMP3_HDR_GET_SAMPLE_RATE(h) (((h[2]) >> 2) & 3)
448 #define DRMP3_HDR_GET_MY_SAMPLE_RATE(h) (DRMP3_HDR_GET_SAMPLE_RATE(h) + (((h[1] >> 3) & 1) + ((h[1] >> 4) & 1))*3)
449 #define DRMP3_HDR_IS_FRAME_576(h) ((h[1] & 14) == 2)
450 #define DRMP3_HDR_IS_LAYER_1(h) ((h[1] & 6) == 6)
451 
452 #define DRMP3_BITS_DEQUANTIZER_OUT -1
453 #define DRMP3_MAX_SCF (255 + DRMP3_BITS_DEQUANTIZER_OUT*4 - 210)
454 #define DRMP3_MAX_SCFI ((DRMP3_MAX_SCF + 3) & ~3)
455 
456 #define DRMP3_MIN(a, b) ((a) > (b) ? (b) : (a))
457 #define DRMP3_MAX(a, b) ((a) < (b) ? (b) : (a))
458 
459 #if !defined(DR_MP3_NO_SIMD)
460 
461 #if !defined(DR_MP3_ONLY_SIMD) && (defined(_M_X64) || defined(_M_ARM64) || defined(__x86_64__) || defined(__aarch64__))
462 /* x64 always have SSE2, arm64 always have neon, no need for generic code */
463 #define DR_MP3_ONLY_SIMD
464 #endif
465 
466 #if ((defined(_MSC_VER) && _MSC_VER >= 1400) && (defined(_M_IX86) || defined(_M_X64))) || ((defined(__i386__) || defined(__x86_64__)) && defined(__SSE2__))
467 #if defined(_MSC_VER)
468 #include <intrin.h>
469 #endif
470 #include <emmintrin.h>
471 #define DRMP3_HAVE_SSE 1
472 #define DRMP3_HAVE_SIMD 1
473 #define DRMP3_VSTORE _mm_storeu_ps
474 #define DRMP3_VLD _mm_loadu_ps
475 #define DRMP3_VSET _mm_set1_ps
476 #define DRMP3_VADD _mm_add_ps
477 #define DRMP3_VSUB _mm_sub_ps
478 #define DRMP3_VMUL _mm_mul_ps
479 #define DRMP3_VMAC(a, x, y) _mm_add_ps(a, _mm_mul_ps(x, y))
480 #define DRMP3_VMSB(a, x, y) _mm_sub_ps(a, _mm_mul_ps(x, y))
481 #define DRMP3_VMUL_S(x, s) _mm_mul_ps(x, _mm_set1_ps(s))
482 #define DRMP3_VREV(x) _mm_shuffle_ps(x, x, _MM_SHUFFLE(0, 1, 2, 3))
483 typedef __m128 drmp3_f4;
484 #if defined(_MSC_VER) || defined(DR_MP3_ONLY_SIMD)
485 #define drmp3_cpuid __cpuid
486 #else
487 static __inline__ __attribute__((always_inline)) void drmp3_cpuid(int CPUInfo[], const int InfoType)
488 {
489 #if defined(__PIC__)
490  __asm__ __volatile__(
491 #if defined(__x86_64__)
492  "push %%rbx\n"
493  "cpuid\n"
494  "xchgl %%ebx, %1\n"
495  "pop %%rbx\n"
496 #else
497  "xchgl %%ebx, %1\n"
498  "cpuid\n"
499  "xchgl %%ebx, %1\n"
500 #endif
501  : "=a" (CPUInfo[0]), "=r" (CPUInfo[1]), "=c" (CPUInfo[2]), "=d" (CPUInfo[3])
502  : "a" (InfoType));
503 #else
504  __asm__ __volatile__(
505  "cpuid"
506  : "=a" (CPUInfo[0]), "=b" (CPUInfo[1]), "=c" (CPUInfo[2]), "=d" (CPUInfo[3])
507  : "a" (InfoType));
508 #endif
509 }
510 #endif
511 static int drmp3_have_simd()
512 {
513 #ifdef DR_MP3_ONLY_SIMD
514  return 1;
515 #else
516  static int g_have_simd;
517  int CPUInfo[4];
518 #ifdef MINIMP3_TEST
519  static int g_counter;
520  if (g_counter++ > 100)
521  return 0;
522 #endif
523  if (g_have_simd)
524  goto end;
525  drmp3_cpuid(CPUInfo, 0);
526  if (CPUInfo[0] > 0)
527  {
528  drmp3_cpuid(CPUInfo, 1);
529  g_have_simd = (CPUInfo[3] & (1 << 26)) + 1; /* SSE2 */
530  return g_have_simd - 1;
531  }
532 
533 end:
534  return g_have_simd - 1;
535 #endif
536 }
537 #elif defined(__ARM_NEON) || defined(__aarch64__)
538 #include <arm_neon.h>
539 #define DRMP3_HAVE_SIMD 1
540 #define DRMP3_VSTORE vst1q_f32
541 #define DRMP3_VLD vld1q_f32
542 #define DRMP3_VSET vmovq_n_f32
543 #define DRMP3_VADD vaddq_f32
544 #define DRMP3_VSUB vsubq_f32
545 #define DRMP3_VMUL vmulq_f32
546 #define DRMP3_VMAC(a, x, y) vmlaq_f32(a, x, y)
547 #define DRMP3_VMSB(a, x, y) vmlsq_f32(a, x, y)
548 #define DRMP3_VMUL_S(x, s) vmulq_f32(x, vmovq_n_f32(s))
549 #define DRMP3_VREV(x) vcombine_f32(vget_high_f32(vrev64q_f32(x)), vget_low_f32(vrev64q_f32(x)))
550 typedef float32x4_t drmp3_f4;
551 static int drmp3_have_simd()
552 { /* TODO: detect neon for !DR_MP3_ONLY_SIMD */
553  return 1;
554 }
555 #else
556 #define DRMP3_HAVE_SIMD 0
557 #ifdef DR_MP3_ONLY_SIMD
558 #error DR_MP3_ONLY_SIMD used, but SSE/NEON not enabled
559 #endif
560 #endif
561 
562 #else
563 
564 #define DRMP3_HAVE_SIMD 0
565 
566 #endif
567 
568 typedef struct
569 {
570  const drmp3_uint8 *buf;
571  int pos, limit;
572 } drmp3_bs;
573 
574 typedef struct
575 {
576  float scf[3*64];
577  drmp3_uint8 total_bands, stereo_bands, bitalloc[64], scfcod[64];
578 } drmp3_L12_scale_info;
579 
580 typedef struct
581 {
582  drmp3_uint8 tab_offset, code_tab_width, band_count;
583 } drmp3_L12_subband_alloc;
584 
585 typedef struct
586 {
587  const drmp3_uint8 *sfbtab;
588  drmp3_uint16 part_23_length, big_values, scalefac_compress;
589  drmp3_uint8 global_gain, block_type, mixed_block_flag, n_long_sfb, n_short_sfb;
590  drmp3_uint8 table_select[3], region_count[3], subblock_gain[3];
591  drmp3_uint8 preflag, scalefac_scale, count1_table, scfsi;
592 } drmp3_L3_gr_info;
593 
594 typedef struct
595 {
596  drmp3_bs bs;
597  drmp3_uint8 maindata[DRMP3_MAX_BITRESERVOIR_BYTES + DRMP3_MAX_L3_FRAME_PAYLOAD_BYTES];
598  drmp3_L3_gr_info gr_info[4];
599  float grbuf[2][576], scf[40], syn[18 + 15][2*32];
600  drmp3_uint8 ist_pos[2][39];
601 } drmp3dec_scratch;
602 
603 static void drmp3_bs_init(drmp3_bs *bs, const drmp3_uint8 *data, int bytes)
604 {
605  bs->buf = data;
606  bs->pos = 0;
607  bs->limit = bytes*8;
608 }
609 
610 static drmp3_uint32 drmp3_bs_get_bits(drmp3_bs *bs, int n)
611 {
612  drmp3_uint32 next, cache = 0, s = bs->pos & 7;
613  int shl = n + s;
614  const drmp3_uint8 *p = bs->buf + (bs->pos >> 3);
615  if ((bs->pos += n) > bs->limit)
616  return 0;
617  next = *p++ & (255 >> s);
618  while ((shl -= 8) > 0)
619  {
620  cache |= next << shl;
621  next = *p++;
622  }
623  return cache | (next >> -shl);
624 }
625 
626 static int drmp3_hdr_valid(const drmp3_uint8 *h)
627 {
628  return h[0] == 0xff &&
629  ((h[1] & 0xF0) == 0xf0 || (h[1] & 0xFE) == 0xe2) &&
630  (DRMP3_HDR_GET_LAYER(h) != 0) &&
631  (DRMP3_HDR_GET_BITRATE(h) != 15) &&
632  (DRMP3_HDR_GET_SAMPLE_RATE(h) != 3);
633 }
634 
635 static int drmp3_hdr_compare(const drmp3_uint8 *h1, const drmp3_uint8 *h2)
636 {
637  return drmp3_hdr_valid(h2) &&
638  ((h1[1] ^ h2[1]) & 0xFE) == 0 &&
639  ((h1[2] ^ h2[2]) & 0x0C) == 0 &&
640  !(DRMP3_HDR_IS_FREE_FORMAT(h1) ^ DRMP3_HDR_IS_FREE_FORMAT(h2));
641 }
642 
643 static unsigned drmp3_hdr_bitrate_kbps(const drmp3_uint8 *h)
644 {
645  static const drmp3_uint8 halfrate[2][3][15] = {
646  { { 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 } },
647  { { 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 } },
648  };
649  return 2*halfrate[!!DRMP3_HDR_TEST_MPEG1(h)][DRMP3_HDR_GET_LAYER(h) - 1][DRMP3_HDR_GET_BITRATE(h)];
650 }
651 
652 static unsigned drmp3_hdr_sample_rate_hz(const drmp3_uint8 *h)
653 {
654  static const unsigned g_hz[3] = { 44100, 48000, 32000 };
655  return g_hz[DRMP3_HDR_GET_SAMPLE_RATE(h)] >> (int)!DRMP3_HDR_TEST_MPEG1(h) >> (int)!DRMP3_HDR_TEST_NOT_MPEG25(h);
656 }
657 
658 static unsigned drmp3_hdr_frame_samples(const drmp3_uint8 *h)
659 {
660  return DRMP3_HDR_IS_LAYER_1(h) ? 384 : (1152 >> (int)DRMP3_HDR_IS_FRAME_576(h));
661 }
662 
663 static int drmp3_hdr_frame_bytes(const drmp3_uint8 *h, int free_format_size)
664 {
665  int frame_bytes = drmp3_hdr_frame_samples(h)*drmp3_hdr_bitrate_kbps(h)*125/drmp3_hdr_sample_rate_hz(h);
666  if (DRMP3_HDR_IS_LAYER_1(h))
667  {
668  frame_bytes &= ~3; /* slot align */
669  }
670  return frame_bytes ? frame_bytes : free_format_size;
671 }
672 
673 static int drmp3_hdr_padding(const drmp3_uint8 *h)
674 {
675  return DRMP3_HDR_TEST_PADDING(h) ? (DRMP3_HDR_IS_LAYER_1(h) ? 4 : 1) : 0;
676 }
677 
678 #ifndef DR_MP3_ONLY_MP3
679 static const drmp3_L12_subband_alloc *drmp3_L12_subband_alloc_table(const drmp3_uint8 *hdr, drmp3_L12_scale_info *sci)
680 {
681  const drmp3_L12_subband_alloc *alloc;
682  int mode = DRMP3_HDR_GET_STEREO_MODE(hdr);
683  int nbands, stereo_bands = (mode == DRMP3_MODE_MONO) ? 0 : (mode == DRMP3_MODE_JOINT_STEREO) ? (DRMP3_HDR_GET_STEREO_MODE_EXT(hdr) << 2) + 4 : 32;
684 
685  if (DRMP3_HDR_IS_LAYER_1(hdr))
686  {
687  static const drmp3_L12_subband_alloc g_alloc_L1[] = { { 76, 4, 32 } };
688  alloc = g_alloc_L1;
689  nbands = 32;
690  } else if (!DRMP3_HDR_TEST_MPEG1(hdr))
691  {
692  static const drmp3_L12_subband_alloc g_alloc_L2M2[] = { { 60, 4, 4 }, { 44, 3, 7 }, { 44, 2, 19 } };
693  alloc = g_alloc_L2M2;
694  nbands = 30;
695  } else
696  {
697  static const drmp3_L12_subband_alloc g_alloc_L2M1[] = { { 0, 4, 3 }, { 16, 4, 8 }, { 32, 3, 12 }, { 40, 2, 7 } };
698  int sample_rate_idx = DRMP3_HDR_GET_SAMPLE_RATE(hdr);
699  unsigned kbps = drmp3_hdr_bitrate_kbps(hdr) >> (int)(mode != DRMP3_MODE_MONO);
700  if (!kbps) /* free-format */
701  {
702  kbps = 192;
703  }
704 
705  alloc = g_alloc_L2M1;
706  nbands = 27;
707  if (kbps < 56)
708  {
709  static const drmp3_L12_subband_alloc g_alloc_L2M1_lowrate[] = { { 44, 4, 2 }, { 44, 3, 10 } };
710  alloc = g_alloc_L2M1_lowrate;
711  nbands = sample_rate_idx == 2 ? 12 : 8;
712  } else if (kbps >= 96 && sample_rate_idx != 1)
713  {
714  nbands = 30;
715  }
716  }
717 
718  sci->total_bands = (drmp3_uint8)nbands;
719  sci->stereo_bands = (drmp3_uint8)DRMP3_MIN(stereo_bands, nbands);
720 
721  return alloc;
722 }
723 
724 static void drmp3_L12_read_scalefactors(drmp3_bs *bs, drmp3_uint8 *pba, drmp3_uint8 *scfcod, int bands, float *scf)
725 {
726  static const float g_deq_L12[18*3] = {
727 #define DRMP3_DQ(x) 9.53674316e-07f/x, 7.56931807e-07f/x, 6.00777173e-07f/x
728  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)
729  };
730  int i, m;
731  for (i = 0; i < bands; i++)
732  {
733  float s = 0;
734  int ba = *pba++;
735  int mask = ba ? 4 + ((19 >> scfcod[i]) & 3) : 0;
736  for (m = 4; m; m >>= 1)
737  {
738  if (mask & m)
739  {
740  int b = drmp3_bs_get_bits(bs, 6);
741  s = g_deq_L12[ba*3 - 6 + b % 3]*(1 << 21 >> b/3);
742  }
743  *scf++ = s;
744  }
745  }
746 }
747 
748 static void drmp3_L12_read_scale_info(const drmp3_uint8 *hdr, drmp3_bs *bs, drmp3_L12_scale_info *sci)
749 {
750  static const drmp3_uint8 g_bitalloc_code_tab[] = {
751  0,17, 3, 4, 5,6,7, 8,9,10,11,12,13,14,15,16,
752  0,17,18, 3,19,4,5, 6,7, 8, 9,10,11,12,13,16,
753  0,17,18, 3,19,4,5,16,
754  0,17,18,16,
755  0,17,18,19, 4,5,6, 7,8, 9,10,11,12,13,14,15,
756  0,17,18, 3,19,4,5, 6,7, 8, 9,10,11,12,13,14,
757  0, 2, 3, 4, 5,6,7, 8,9,10,11,12,13,14,15,16
758  };
759  const drmp3_L12_subband_alloc *subband_alloc = drmp3_L12_subband_alloc_table(hdr, sci);
760 
761  int i, k = 0, ba_bits = 0;
762  const drmp3_uint8 *ba_code_tab = g_bitalloc_code_tab;
763 
764  for (i = 0; i < sci->total_bands; i++)
765  {
766  drmp3_uint8 ba;
767  if (i == k)
768  {
769  k += subband_alloc->band_count;
770  ba_bits = subband_alloc->code_tab_width;
771  ba_code_tab = g_bitalloc_code_tab + subband_alloc->tab_offset;
772  subband_alloc++;
773  }
774  ba = ba_code_tab[drmp3_bs_get_bits(bs, ba_bits)];
775  sci->bitalloc[2*i] = ba;
776  if (i < sci->stereo_bands)
777  {
778  ba = ba_code_tab[drmp3_bs_get_bits(bs, ba_bits)];
779  }
780  sci->bitalloc[2*i + 1] = sci->stereo_bands ? ba : 0;
781  }
782 
783  for (i = 0; i < 2*sci->total_bands; i++)
784  {
785  sci->scfcod[i] = (drmp3_uint8)(sci->bitalloc[i] ? DRMP3_HDR_IS_LAYER_1(hdr) ? 2 : drmp3_bs_get_bits(bs, 2) : 6);
786  }
787 
788  drmp3_L12_read_scalefactors(bs, sci->bitalloc, sci->scfcod, sci->total_bands*2, sci->scf);
789 
790  for (i = sci->stereo_bands; i < sci->total_bands; i++)
791  {
792  sci->bitalloc[2*i + 1] = 0;
793  }
794 }
795 
796 static int drmp3_L12_dequantize_granule(float *grbuf, drmp3_bs *bs, drmp3_L12_scale_info *sci, int group_size)
797 {
798  int i, j, k, choff = 576;
799  for (j = 0; j < 4; j++)
800  {
801  float *dst = grbuf + group_size*j;
802  for (i = 0; i < 2*sci->total_bands; i++)
803  {
804  int ba = sci->bitalloc[i];
805  if (ba != 0)
806  {
807  if (ba < 17)
808  {
809  int half = (1 << (ba - 1)) - 1;
810  for (k = 0; k < group_size; k++)
811  {
812  dst[k] = (float)((int)drmp3_bs_get_bits(bs, ba) - half);
813  }
814  } else
815  {
816  unsigned mod = (2 << (ba - 17)) + 1; /* 3, 5, 9 */
817  unsigned code = drmp3_bs_get_bits(bs, mod + 2 - (mod >> 3)); /* 5, 7, 10 */
818  for (k = 0; k < group_size; k++, code /= mod)
819  {
820  dst[k] = (float)((int)(code % mod - mod/2));
821  }
822  }
823  }
824  dst += choff;
825  choff = 18 - choff;
826  }
827  }
828  return group_size*4;
829 }
830 
831 static void drmp3_L12_apply_scf_384(drmp3_L12_scale_info *sci, const float *scf, float *dst)
832 {
833  int i, k;
834  memcpy(dst + 576 + sci->stereo_bands*18, dst + sci->stereo_bands*18, (sci->total_bands - sci->stereo_bands)*18*sizeof(float));
835  for (i = 0; i < sci->total_bands; i++, dst += 18, scf += 6)
836  {
837  for (k = 0; k < 12; k++)
838  {
839  dst[k + 0] *= scf[0];
840  dst[k + 576] *= scf[3];
841  }
842  }
843 }
844 #endif
845 
846 static int drmp3_L3_read_side_info(drmp3_bs *bs, drmp3_L3_gr_info *gr, const drmp3_uint8 *hdr)
847 {
848  static const drmp3_uint8 g_scf_long[8][23] = {
849  { 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 },
850  { 12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2,0 },
851  { 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 },
852  { 6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,54,62,70,76,36,0 },
853  { 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 },
854  { 4,4,4,4,4,4,6,6,8,8,10,12,16,20,24,28,34,42,50,54,76,158,0 },
855  { 4,4,4,4,4,4,6,6,6,8,10,12,16,18,22,28,34,40,46,54,54,192,0 },
856  { 4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102,26,0 }
857  };
858  static const drmp3_uint8 g_scf_short[8][40] = {
859  { 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 },
860  { 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 },
861  { 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 },
862  { 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 },
863  { 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 },
864  { 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 },
865  { 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 },
866  { 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 }
867  };
868  static const drmp3_uint8 g_scf_mixed[8][40] = {
869  { 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 },
870  { 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 },
871  { 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 },
872  { 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 },
873  { 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 },
874  { 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 },
875  { 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 },
876  { 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 }
877  };
878 
879  unsigned tables, scfsi = 0;
880  int main_data_begin, part_23_sum = 0;
881  int gr_count = DRMP3_HDR_IS_MONO(hdr) ? 1 : 2;
882  int sr_idx = DRMP3_HDR_GET_MY_SAMPLE_RATE(hdr); sr_idx -= (sr_idx != 0);
883 
884  if (DRMP3_HDR_TEST_MPEG1(hdr))
885  {
886  gr_count *= 2;
887  main_data_begin = drmp3_bs_get_bits(bs, 9);
888  scfsi = drmp3_bs_get_bits(bs, 7 + gr_count);
889  } else
890  {
891  main_data_begin = drmp3_bs_get_bits(bs, 8 + gr_count) >> gr_count;
892  }
893 
894  do
895  {
896  if (DRMP3_HDR_IS_MONO(hdr))
897  {
898  scfsi <<= 4;
899  }
900  gr->part_23_length = (drmp3_uint16)drmp3_bs_get_bits(bs, 12);
901  part_23_sum += gr->part_23_length;
902  gr->big_values = (drmp3_uint16)drmp3_bs_get_bits(bs, 9);
903  if (gr->big_values > 288)
904  {
905  return -1;
906  }
907  gr->global_gain = (drmp3_uint8)drmp3_bs_get_bits(bs, 8);
908  gr->scalefac_compress = (drmp3_uint16)drmp3_bs_get_bits(bs, DRMP3_HDR_TEST_MPEG1(hdr) ? 4 : 9);
909  gr->sfbtab = g_scf_long[sr_idx];
910  gr->n_long_sfb = 22;
911  gr->n_short_sfb = 0;
912  if (drmp3_bs_get_bits(bs, 1))
913  {
914  gr->block_type = (drmp3_uint8)drmp3_bs_get_bits(bs, 2);
915  if (!gr->block_type)
916  {
917  return -1;
918  }
919  gr->mixed_block_flag = (drmp3_uint8)drmp3_bs_get_bits(bs, 1);
920  gr->region_count[0] = 7;
921  gr->region_count[1] = 255;
922  if (gr->block_type == DRMP3_SHORT_BLOCK_TYPE)
923  {
924  scfsi &= 0x0F0F;
925  if (!gr->mixed_block_flag)
926  {
927  gr->region_count[0] = 8;
928  gr->sfbtab = g_scf_short[sr_idx];
929  gr->n_long_sfb = 0;
930  gr->n_short_sfb = 39;
931  } else
932  {
933  gr->sfbtab = g_scf_mixed[sr_idx];
934  gr->n_long_sfb = DRMP3_HDR_TEST_MPEG1(hdr) ? 8 : 6;
935  gr->n_short_sfb = 30;
936  }
937  }
938  tables = drmp3_bs_get_bits(bs, 10);
939  tables <<= 5;
940  gr->subblock_gain[0] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3);
941  gr->subblock_gain[1] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3);
942  gr->subblock_gain[2] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3);
943  } else
944  {
945  gr->block_type = 0;
946  gr->mixed_block_flag = 0;
947  tables = drmp3_bs_get_bits(bs, 15);
948  gr->region_count[0] = (drmp3_uint8)drmp3_bs_get_bits(bs, 4);
949  gr->region_count[1] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3);
950  gr->region_count[2] = 255;
951  }
952  gr->table_select[0] = (drmp3_uint8)(tables >> 10);
953  gr->table_select[1] = (drmp3_uint8)((tables >> 5) & 31);
954  gr->table_select[2] = (drmp3_uint8)((tables) & 31);
955  gr->preflag = (drmp3_uint8)(DRMP3_HDR_TEST_MPEG1(hdr) ? drmp3_bs_get_bits(bs, 1) : (gr->scalefac_compress >= 500));
956  gr->scalefac_scale = (drmp3_uint8)drmp3_bs_get_bits(bs, 1);
957  gr->count1_table = (drmp3_uint8)drmp3_bs_get_bits(bs, 1);
958  gr->scfsi = (drmp3_uint8)((scfsi >> 12) & 15);
959  scfsi <<= 4;
960  gr++;
961  } while(--gr_count);
962 
963  if (part_23_sum + bs->pos > bs->limit + main_data_begin*8)
964  {
965  return -1;
966  }
967 
968  return main_data_begin;
969 }
970 
971 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)
972 {
973  int i, k;
974  for (i = 0; i < 4 && scf_count[i]; i++, scfsi *= 2)
975  {
976  int cnt = scf_count[i];
977  if (scfsi & 8)
978  {
979  memcpy(scf, ist_pos, cnt);
980  } else
981  {
982  int bits = scf_size[i];
983  if (!bits)
984  {
985  memset(scf, 0, cnt);
986  memset(ist_pos, 0, cnt);
987  } else
988  {
989  int max_scf = (scfsi < 0) ? (1 << bits) - 1 : -1;
990  for (k = 0; k < cnt; k++)
991  {
992  int s = drmp3_bs_get_bits(bitbuf, bits);
993  ist_pos[k] = (drmp3_uint8)(s == max_scf ? -1 : s);
994  scf[k] = (drmp3_uint8)s;
995  }
996  }
997  }
998  ist_pos += cnt;
999  scf += cnt;
1000  }
1001  scf[0] = scf[1] = scf[2] = 0;
1002 }
1003 
1004 static float drmp3_L3_ldexp_q2(float y, int exp_q2)
1005 {
1006  static const float g_expfrac[4] = { 9.31322575e-10f,7.83145814e-10f,6.58544508e-10f,5.53767716e-10f };
1007  int e;
1008  do
1009  {
1010  e = DRMP3_MIN(30*4, exp_q2);
1011  y *= g_expfrac[e & 3]*(1 << 30 >> (e >> 2));
1012  } while ((exp_q2 -= e) > 0);
1013  return y;
1014 }
1015 
1016 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)
1017 {
1018  static const drmp3_uint8 g_scf_partitions[3][28] = {
1019  { 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 },
1020  { 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 },
1021  { 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 }
1022  };
1023  const drmp3_uint8 *scf_partition = g_scf_partitions[!!gr->n_short_sfb + !gr->n_long_sfb];
1024  drmp3_uint8 scf_size[4], iscf[40];
1025  int i, scf_shift = gr->scalefac_scale + 1, gain_exp, scfsi = gr->scfsi;
1026  float gain;
1027 
1028  if (DRMP3_HDR_TEST_MPEG1(hdr))
1029  {
1030  static const drmp3_uint8 g_scfc_decode[16] = { 0,1,2,3, 12,5,6,7, 9,10,11,13, 14,15,18,19 };
1031  int part = g_scfc_decode[gr->scalefac_compress];
1032  scf_size[1] = scf_size[0] = (drmp3_uint8)(part >> 2);
1033  scf_size[3] = scf_size[2] = (drmp3_uint8)(part & 3);
1034  } else
1035  {
1036  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 };
1037  int k, modprod, sfc, ist = DRMP3_HDR_TEST_I_STEREO(hdr) && ch;
1038  sfc = gr->scalefac_compress >> ist;
1039  for (k = ist*3*4; sfc >= 0; sfc -= modprod, k += 4)
1040  {
1041  for (modprod = 1, i = 3; i >= 0; i--)
1042  {
1043  scf_size[i] = (drmp3_uint8)(sfc / modprod % g_mod[k + i]);
1044  modprod *= g_mod[k + i];
1045  }
1046  }
1047  scf_partition += k;
1048  scfsi = -16;
1049  }
1050  drmp3_L3_read_scalefactors(iscf, ist_pos, scf_size, scf_partition, bs, scfsi);
1051 
1052  if (gr->n_short_sfb)
1053  {
1054  int sh = 3 - scf_shift;
1055  for (i = 0; i < gr->n_short_sfb; i += 3)
1056  {
1057  iscf[gr->n_long_sfb + i + 0] += gr->subblock_gain[0] << sh;
1058  iscf[gr->n_long_sfb + i + 1] += gr->subblock_gain[1] << sh;
1059  iscf[gr->n_long_sfb + i + 2] += gr->subblock_gain[2] << sh;
1060  }
1061  } else if (gr->preflag)
1062  {
1063  static const drmp3_uint8 g_preamp[10] = { 1,1,1,1,2,2,3,3,3,2 };
1064  for (i = 0; i < 10; i++)
1065  {
1066  iscf[11 + i] += g_preamp[i];
1067  }
1068  }
1069 
1070  gain_exp = gr->global_gain + DRMP3_BITS_DEQUANTIZER_OUT*4 - 210 - (DRMP3_HDR_IS_MS_STEREO(hdr) ? 2 : 0);
1071  gain = drmp3_L3_ldexp_q2(1 << (DRMP3_MAX_SCFI/4), DRMP3_MAX_SCFI - gain_exp);
1072  for (i = 0; i < (int)(gr->n_long_sfb + gr->n_short_sfb); i++)
1073  {
1074  scf[i] = drmp3_L3_ldexp_q2(gain, iscf[i] << scf_shift);
1075  }
1076 }
1077 
1078 static const float g_drmp3_pow43[129 + 16] = {
1079  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,
1080  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
1081 };
1082 
1083 static float drmp3_L3_pow_43(int x)
1084 {
1085  float frac;
1086  int sign, mult = 256;
1087 
1088  if (x < 129)
1089  {
1090  return g_drmp3_pow43[16 + x];
1091  }
1092 
1093  if (x < 1024)
1094  {
1095  mult = 16;
1096  x <<= 3;
1097  }
1098 
1099  sign = 2*x & 64;
1100  frac = (float)((x & 63) - sign) / ((x & ~63) + sign);
1101  return g_drmp3_pow43[16 + ((x + sign) >> 6)]*(1.f + frac*((4.f/3) + frac*(2.f/9)))*mult;
1102 }
1103 
1104 static void drmp3_L3_huffman(float *dst, drmp3_bs *bs, const drmp3_L3_gr_info *gr_info, const float *scf, int layer3gr_limit)
1105 {
1106  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,
1107  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,
1108  -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,
1109  -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,
1110  -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,
1111  -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,
1112  -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,
1113  -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,
1114  -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,
1115  -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,
1116  -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,
1117  -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,
1118  -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,
1119  -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,
1120  -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,
1121  -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 };
1122  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};
1123  static const drmp3_uint8 tab33[] = { 252,236,220,204,188,172,156,140,124,108,92,76,60,44,28,12 };
1124  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 };
1125  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 };
1126 
1127 #define DRMP3_PEEK_BITS(n) (bs_cache >> (32 - n))
1128 #define DRMP3_FLUSH_BITS(n) { bs_cache <<= (n); bs_sh += (n); }
1129 #define DRMP3_CHECK_BITS while (bs_sh >= 0) { bs_cache |= (drmp3_uint32)*bs_next_ptr++ << bs_sh; bs_sh -= 8; }
1130 #define DRMP3_BSPOS ((bs_next_ptr - bs->buf)*8 - 24 + bs_sh)
1131 
1132  float one = 0.0f;
1133  int ireg = 0, big_val_cnt = gr_info->big_values;
1134  const drmp3_uint8 *sfb = gr_info->sfbtab;
1135  const drmp3_uint8 *bs_next_ptr = bs->buf + bs->pos/8;
1136  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);
1137  int pairs_to_decode, np, bs_sh = (bs->pos & 7) - 8;
1138  bs_next_ptr += 4;
1139 
1140  while (big_val_cnt > 0)
1141  {
1142  int tab_num = gr_info->table_select[ireg];
1143  int sfb_cnt = gr_info->region_count[ireg++];
1144  const drmp3_int16 *codebook = tabs + tabindex[tab_num];
1145  int linbits = g_linbits[tab_num];
1146  do
1147  {
1148  np = *sfb++ / 2;
1149  pairs_to_decode = DRMP3_MIN(big_val_cnt, np);
1150  one = *scf++;
1151  do
1152  {
1153  int j, w = 5;
1154  int leaf = codebook[DRMP3_PEEK_BITS(w)];
1155  while (leaf < 0)
1156  {
1157  DRMP3_FLUSH_BITS(w);
1158  w = leaf & 7;
1159  leaf = codebook[DRMP3_PEEK_BITS(w) - (leaf >> 3)];
1160  }
1161  DRMP3_FLUSH_BITS(leaf >> 8);
1162 
1163  for (j = 0; j < 2; j++, dst++, leaf >>= 4)
1164  {
1165  int lsb = leaf & 0x0F;
1166  if (lsb == 15 && linbits)
1167  {
1168  lsb += DRMP3_PEEK_BITS(linbits);
1169  DRMP3_FLUSH_BITS(linbits);
1170  DRMP3_CHECK_BITS;
1171  *dst = one*drmp3_L3_pow_43(lsb)*((drmp3_int32)bs_cache < 0 ? -1: 1);
1172  } else
1173  {
1174  *dst = g_drmp3_pow43[16 + lsb - 16*(bs_cache >> 31)]*one;
1175  }
1176  DRMP3_FLUSH_BITS(lsb ? 1 : 0);
1177  }
1178  DRMP3_CHECK_BITS;
1179  } while (--pairs_to_decode);
1180  } while ((big_val_cnt -= np) > 0 && --sfb_cnt >= 0);
1181  }
1182 
1183  for (np = 1 - big_val_cnt;; dst += 4)
1184  {
1185  const drmp3_uint8 *codebook_count1 = (gr_info->count1_table) ? tab33 : tab32;
1186  int leaf = codebook_count1[DRMP3_PEEK_BITS(4)];
1187  if (!(leaf & 8))
1188  {
1189  leaf = codebook_count1[(leaf >> 3) + (bs_cache << 4 >> (32 - (leaf & 3)))];
1190  }
1191  DRMP3_FLUSH_BITS(leaf & 7);
1192  if (DRMP3_BSPOS > layer3gr_limit)
1193  {
1194  break;
1195  }
1196 #define DRMP3_RELOAD_SCALEFACTOR if (!--np) { np = *sfb++/2; if (!np) break; one = *scf++; }
1197 #define DRMP3_DEQ_COUNT1(s) if (leaf & (128 >> s)) { dst[s] = ((drmp3_int32)bs_cache < 0) ? -one : one; DRMP3_FLUSH_BITS(1) }
1198  DRMP3_RELOAD_SCALEFACTOR;
1199  DRMP3_DEQ_COUNT1(0);
1200  DRMP3_DEQ_COUNT1(1);
1201  DRMP3_RELOAD_SCALEFACTOR;
1202  DRMP3_DEQ_COUNT1(2);
1203  DRMP3_DEQ_COUNT1(3);
1204  DRMP3_CHECK_BITS;
1205  }
1206 
1207  bs->pos = layer3gr_limit;
1208 }
1209 
1210 static void drmp3_L3_midside_stereo(float *left, int n)
1211 {
1212  int i = 0;
1213  float *right = left + 576;
1214 #if DRMP3_HAVE_SIMD
1215  if (drmp3_have_simd()) for (; i < n - 3; i += 4)
1216  {
1217  drmp3_f4 vl = DRMP3_VLD(left + i);
1218  drmp3_f4 vr = DRMP3_VLD(right + i);
1219  DRMP3_VSTORE(left + i, DRMP3_VADD(vl, vr));
1220  DRMP3_VSTORE(right + i, DRMP3_VSUB(vl, vr));
1221  }
1222 #endif
1223  for (; i < n; i++)
1224  {
1225  float a = left[i];
1226  float b = right[i];
1227  left[i] = a + b;
1228  right[i] = a - b;
1229  }
1230 }
1231 
1232 static void drmp3_L3_intensity_stereo_band(float *left, int n, float kl, float kr)
1233 {
1234  int i;
1235  for (i = 0; i < n; i++)
1236  {
1237  left[i + 576] = left[i]*kr;
1238  left[i] = left[i]*kl;
1239  }
1240 }
1241 
1242 static void drmp3_L3_stereo_top_band(const float *right, const drmp3_uint8 *sfb, int nbands, int max_band[3])
1243 {
1244  int i, k;
1245 
1246  max_band[0] = max_band[1] = max_band[2] = -1;
1247 
1248  for (i = 0; i < nbands; i++)
1249  {
1250  for (k = 0; k < sfb[i]; k += 2)
1251  {
1252  if (right[k] != 0 || right[k + 1] != 0)
1253  {
1254  max_band[i % 3] = i;
1255  break;
1256  }
1257  }
1258  right += sfb[i];
1259  }
1260 }
1261 
1262 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)
1263 {
1264  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 };
1265  unsigned i, max_pos = DRMP3_HDR_TEST_MPEG1(hdr) ? 7 : 64;
1266 
1267  for (i = 0; sfb[i]; i++)
1268  {
1269  unsigned ipos = ist_pos[i];
1270  if ((int)i > max_band[i % 3] && ipos < max_pos)
1271  {
1272  float kl, kr, s = DRMP3_HDR_TEST_MS_STEREO(hdr) ? 1.41421356f : 1;
1273  if (DRMP3_HDR_TEST_MPEG1(hdr))
1274  {
1275  kl = g_pan[2*ipos];
1276  kr = g_pan[2*ipos + 1];
1277  } else
1278  {
1279  kl = 1;
1280  kr = drmp3_L3_ldexp_q2(1, (ipos + 1) >> 1 << mpeg2_sh);
1281  if (ipos & 1)
1282  {
1283  kl = kr;
1284  kr = 1;
1285  }
1286  }
1287  drmp3_L3_intensity_stereo_band(left, sfb[i], kl*s, kr*s);
1288  } else if (DRMP3_HDR_TEST_MS_STEREO(hdr))
1289  {
1290  drmp3_L3_midside_stereo(left, sfb[i]);
1291  }
1292  left += sfb[i];
1293  }
1294 }
1295 
1296 static void drmp3_L3_intensity_stereo(float *left, drmp3_uint8 *ist_pos, const drmp3_L3_gr_info *gr, const drmp3_uint8 *hdr)
1297 {
1298  int max_band[3], n_sfb = gr->n_long_sfb + gr->n_short_sfb;
1299  int i, max_blocks = gr->n_short_sfb ? 3 : 1;
1300 
1301  drmp3_L3_stereo_top_band(left + 576, gr->sfbtab, n_sfb, max_band);
1302  if (gr->n_long_sfb)
1303  {
1304  max_band[0] = max_band[1] = max_band[2] = DRMP3_MAX(DRMP3_MAX(max_band[0], max_band[1]), max_band[2]);
1305  }
1306  for (i = 0; i < max_blocks; i++)
1307  {
1308  int default_pos = DRMP3_HDR_TEST_MPEG1(hdr) ? 3 : 0;
1309  int itop = n_sfb - max_blocks + i;
1310  int prev = itop - max_blocks;
1311  ist_pos[itop] = (drmp3_uint8)(max_band[i] >= prev ? default_pos : ist_pos[prev]);
1312  }
1313  drmp3_L3_stereo_process(left, ist_pos, gr->sfbtab, hdr, max_band, gr[1].scalefac_compress & 1);
1314 }
1315 
1316 static void drmp3_L3_reorder(float *grbuf, float *scratch, const drmp3_uint8 *sfb)
1317 {
1318  int i, len;
1319  float *src = grbuf, *dst = scratch;
1320 
1321  for (;0 != (len = *sfb); sfb += 3, src += 2*len)
1322  {
1323  for (i = 0; i < len; i++, src++)
1324  {
1325  *dst++ = src[0*len];
1326  *dst++ = src[1*len];
1327  *dst++ = src[2*len];
1328  }
1329  }
1330  memcpy(grbuf, scratch, (dst - scratch)*sizeof(float));
1331 }
1332 
1333 static void drmp3_L3_antialias(float *grbuf, int nbands)
1334 {
1335  static const float g_aa[2][8] = {
1336  {0.85749293f,0.88174200f,0.94962865f,0.98331459f,0.99551782f,0.99916056f,0.99989920f,0.99999316f},
1337  {0.51449576f,0.47173197f,0.31337745f,0.18191320f,0.09457419f,0.04096558f,0.01419856f,0.00369997f}
1338  };
1339 
1340  for (; nbands > 0; nbands--, grbuf += 18)
1341  {
1342  int i = 0;
1343 #if DRMP3_HAVE_SIMD
1344  if (drmp3_have_simd()) for (; i < 8; i += 4)
1345  {
1346  drmp3_f4 vu = DRMP3_VLD(grbuf + 18 + i);
1347  drmp3_f4 vd = DRMP3_VLD(grbuf + 14 - i);
1348  drmp3_f4 vc0 = DRMP3_VLD(g_aa[0] + i);
1349  drmp3_f4 vc1 = DRMP3_VLD(g_aa[1] + i);
1350  vd = DRMP3_VREV(vd);
1351  DRMP3_VSTORE(grbuf + 18 + i, DRMP3_VSUB(DRMP3_VMUL(vu, vc0), DRMP3_VMUL(vd, vc1)));
1352  vd = DRMP3_VADD(DRMP3_VMUL(vu, vc1), DRMP3_VMUL(vd, vc0));
1353  DRMP3_VSTORE(grbuf + 14 - i, DRMP3_VREV(vd));
1354  }
1355 #endif
1356 #ifndef DR_MP3_ONLY_SIMD
1357  for(; i < 8; i++)
1358  {
1359  float u = grbuf[18 + i];
1360  float d = grbuf[17 - i];
1361  grbuf[18 + i] = u*g_aa[0][i] - d*g_aa[1][i];
1362  grbuf[17 - i] = u*g_aa[1][i] + d*g_aa[0][i];
1363  }
1364 #endif
1365  }
1366 }
1367 
1368 static void drmp3_L3_dct3_9(float *y)
1369 {
1370  float s0, s1, s2, s3, s4, s5, s6, s7, s8, t0, t2, t4;
1371 
1372  s0 = y[0]; s2 = y[2]; s4 = y[4]; s6 = y[6]; s8 = y[8];
1373  t0 = s0 + s6*0.5f;
1374  s0 -= s6;
1375  t4 = (s4 + s2)*0.93969262f;
1376  t2 = (s8 + s2)*0.76604444f;
1377  s6 = (s4 - s8)*0.17364818f;
1378  s4 += s8 - s2;
1379 
1380  s2 = s0 - s4*0.5f;
1381  y[4] = s4 + s0;
1382  s8 = t0 - t2 + s6;
1383  s0 = t0 - t4 + t2;
1384  s4 = t0 + t4 - s6;
1385 
1386  s1 = y[1]; s3 = y[3]; s5 = y[5]; s7 = y[7];
1387 
1388  s3 *= 0.86602540f;
1389  t0 = (s5 + s1)*0.98480775f;
1390  t4 = (s5 - s7)*0.34202014f;
1391  t2 = (s1 + s7)*0.64278761f;
1392  s1 = (s1 - s5 - s7)*0.86602540f;
1393 
1394  s5 = t0 - s3 - t2;
1395  s7 = t4 - s3 - t0;
1396  s3 = t4 + s3 - t2;
1397 
1398  y[0] = s4 - s7;
1399  y[1] = s2 + s1;
1400  y[2] = s0 - s3;
1401  y[3] = s8 + s5;
1402  y[5] = s8 - s5;
1403  y[6] = s0 + s3;
1404  y[7] = s2 - s1;
1405  y[8] = s4 + s7;
1406 }
1407 
1408 static void drmp3_L3_imdct36(float *grbuf, float *overlap, const float *window, int nbands)
1409 {
1410  int i, j;
1411  static const float g_twid9[18] = {
1412  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
1413  };
1414 
1415  for (j = 0; j < nbands; j++, grbuf += 18, overlap += 9)
1416  {
1417  float co[9], si[9];
1418  co[0] = -grbuf[0];
1419  si[0] = grbuf[17];
1420  for (i = 0; i < 4; i++)
1421  {
1422  si[8 - 2*i] = grbuf[4*i + 1] - grbuf[4*i + 2];
1423  co[1 + 2*i] = grbuf[4*i + 1] + grbuf[4*i + 2];
1424  si[7 - 2*i] = grbuf[4*i + 4] - grbuf[4*i + 3];
1425  co[2 + 2*i] = -(grbuf[4*i + 3] + grbuf[4*i + 4]);
1426  }
1427  drmp3_L3_dct3_9(co);
1428  drmp3_L3_dct3_9(si);
1429 
1430  si[1] = -si[1];
1431  si[3] = -si[3];
1432  si[5] = -si[5];
1433  si[7] = -si[7];
1434 
1435  i = 0;
1436 
1437 #if DRMP3_HAVE_SIMD
1438  if (drmp3_have_simd()) for (; i < 8; i += 4)
1439  {
1440  drmp3_f4 vovl = DRMP3_VLD(overlap + i);
1441  drmp3_f4 vc = DRMP3_VLD(co + i);
1442  drmp3_f4 vs = DRMP3_VLD(si + i);
1443  drmp3_f4 vr0 = DRMP3_VLD(g_twid9 + i);
1444  drmp3_f4 vr1 = DRMP3_VLD(g_twid9 + 9 + i);
1445  drmp3_f4 vw0 = DRMP3_VLD(window + i);
1446  drmp3_f4 vw1 = DRMP3_VLD(window + 9 + i);
1447  drmp3_f4 vsum = DRMP3_VADD(DRMP3_VMUL(vc, vr1), DRMP3_VMUL(vs, vr0));
1448  DRMP3_VSTORE(overlap + i, DRMP3_VSUB(DRMP3_VMUL(vc, vr0), DRMP3_VMUL(vs, vr1)));
1449  DRMP3_VSTORE(grbuf + i, DRMP3_VSUB(DRMP3_VMUL(vovl, vw0), DRMP3_VMUL(vsum, vw1)));
1450  vsum = DRMP3_VADD(DRMP3_VMUL(vovl, vw1), DRMP3_VMUL(vsum, vw0));
1451  DRMP3_VSTORE(grbuf + 14 - i, DRMP3_VREV(vsum));
1452  }
1453 #endif
1454  for (; i < 9; i++)
1455  {
1456  float ovl = overlap[i];
1457  float sum = co[i]*g_twid9[9 + i] + si[i]*g_twid9[0 + i];
1458  overlap[i] = co[i]*g_twid9[0 + i] - si[i]*g_twid9[9 + i];
1459  grbuf[i] = ovl*window[0 + i] - sum*window[9 + i];
1460  grbuf[17 - i] = ovl*window[9 + i] + sum*window[0 + i];
1461  }
1462  }
1463 }
1464 
1465 static void drmp3_L3_idct3(float x0, float x1, float x2, float *dst)
1466 {
1467  float m1 = x1*0.86602540f;
1468  float a1 = x0 - x2*0.5f;
1469  dst[1] = x0 + x2;
1470  dst[0] = a1 + m1;
1471  dst[2] = a1 - m1;
1472 }
1473 
1474 static void drmp3_L3_imdct12(float *x, float *dst, float *overlap)
1475 {
1476  static const float g_twid3[6] = { 0.79335334f,0.92387953f,0.99144486f, 0.60876143f,0.38268343f,0.13052619f };
1477  float co[3], si[3];
1478  int i;
1479 
1480  drmp3_L3_idct3(-x[0], x[6] + x[3], x[12] + x[9], co);
1481  drmp3_L3_idct3(x[15], x[12] - x[9], x[6] - x[3], si);
1482  si[1] = -si[1];
1483 
1484  for (i = 0; i < 3; i++)
1485  {
1486  float ovl = overlap[i];
1487  float sum = co[i]*g_twid3[3 + i] + si[i]*g_twid3[0 + i];
1488  overlap[i] = co[i]*g_twid3[0 + i] - si[i]*g_twid3[3 + i];
1489  dst[i] = ovl*g_twid3[2 - i] - sum*g_twid3[5 - i];
1490  dst[5 - i] = ovl*g_twid3[5 - i] + sum*g_twid3[2 - i];
1491  }
1492 }
1493 
1494 static void drmp3_L3_imdct_short(float *grbuf, float *overlap, int nbands)
1495 {
1496  for (;nbands > 0; nbands--, overlap += 9, grbuf += 18)
1497  {
1498  float tmp[18];
1499  memcpy(tmp, grbuf, sizeof(tmp));
1500  memcpy(grbuf, overlap, 6*sizeof(float));
1501  drmp3_L3_imdct12(tmp, grbuf + 6, overlap + 6);
1502  drmp3_L3_imdct12(tmp + 1, grbuf + 12, overlap + 6);
1503  drmp3_L3_imdct12(tmp + 2, overlap, overlap + 6);
1504  }
1505 }
1506 
1507 static void drmp3_L3_change_sign(float *grbuf)
1508 {
1509  int b, i;
1510  for (b = 0, grbuf += 18; b < 32; b += 2, grbuf += 36)
1511  for (i = 1; i < 18; i += 2)
1512  grbuf[i] = -grbuf[i];
1513 }
1514 
1515 static void drmp3_L3_imdct_gr(float *grbuf, float *overlap, unsigned block_type, unsigned n_long_bands)
1516 {
1517  static const float g_mdct_window[2][18] = {
1518  { 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 },
1519  { 1,1,1,1,1,1,0.99144486f,0.92387953f,0.79335334f,0,0,0,0,0,0,0.13052619f,0.38268343f,0.60876143f }
1520  };
1521  if (n_long_bands)
1522  {
1523  drmp3_L3_imdct36(grbuf, overlap, g_mdct_window[0], n_long_bands);
1524  grbuf += 18*n_long_bands;
1525  overlap += 9*n_long_bands;
1526  }
1527  if (block_type == DRMP3_SHORT_BLOCK_TYPE)
1528  drmp3_L3_imdct_short(grbuf, overlap, 32 - n_long_bands);
1529  else
1530  drmp3_L3_imdct36(grbuf, overlap, g_mdct_window[block_type == DRMP3_STOP_BLOCK_TYPE], 32 - n_long_bands);
1531 }
1532 
1533 static void drmp3_L3_save_reservoir(drmp3dec *h, drmp3dec_scratch *s)
1534 {
1535  int pos = (s->bs.pos + 7)/8u;
1536  int remains = s->bs.limit/8u - pos;
1537  if (remains > DRMP3_MAX_BITRESERVOIR_BYTES)
1538  {
1539  pos += remains - DRMP3_MAX_BITRESERVOIR_BYTES;
1540  remains = DRMP3_MAX_BITRESERVOIR_BYTES;
1541  }
1542  if (remains > 0)
1543  {
1544  memmove(h->reserv_buf, s->maindata + pos, remains);
1545  }
1546  h->reserv = remains;
1547 }
1548 
1549 static int drmp3_L3_restore_reservoir(drmp3dec *h, drmp3_bs *bs, drmp3dec_scratch *s, int main_data_begin)
1550 {
1551  int frame_bytes = (bs->limit - bs->pos)/8;
1552  int bytes_have = DRMP3_MIN(h->reserv, main_data_begin);
1553  memcpy(s->maindata, h->reserv_buf + DRMP3_MAX(0, h->reserv - main_data_begin), DRMP3_MIN(h->reserv, main_data_begin));
1554  memcpy(s->maindata + bytes_have, bs->buf + bs->pos/8, frame_bytes);
1555  drmp3_bs_init(&s->bs, s->maindata, bytes_have + frame_bytes);
1556  return h->reserv >= main_data_begin;
1557 }
1558 
1559 static void drmp3_L3_decode(drmp3dec *h, drmp3dec_scratch *s, drmp3_L3_gr_info *gr_info, int nch)
1560 {
1561  int ch;
1562 
1563  for (ch = 0; ch < nch; ch++)
1564  {
1565  int layer3gr_limit = s->bs.pos + gr_info[ch].part_23_length;
1566  drmp3_L3_decode_scalefactors(h->header, s->ist_pos[ch], &s->bs, gr_info + ch, s->scf, ch);
1567  drmp3_L3_huffman(s->grbuf[ch], &s->bs, gr_info + ch, s->scf, layer3gr_limit);
1568  }
1569 
1570  if (DRMP3_HDR_TEST_I_STEREO(h->header))
1571  {
1572  drmp3_L3_intensity_stereo(s->grbuf[0], s->ist_pos[1], gr_info, h->header);
1573  } else if (DRMP3_HDR_IS_MS_STEREO(h->header))
1574  {
1575  drmp3_L3_midside_stereo(s->grbuf[0], 576);
1576  }
1577 
1578  for (ch = 0; ch < nch; ch++, gr_info++)
1579  {
1580  int aa_bands = 31;
1581  int n_long_bands = (gr_info->mixed_block_flag ? 2 : 0) << (int)(DRMP3_HDR_GET_MY_SAMPLE_RATE(h->header) == 2);
1582 
1583  if (gr_info->n_short_sfb)
1584  {
1585  aa_bands = n_long_bands - 1;
1586  drmp3_L3_reorder(s->grbuf[ch] + n_long_bands*18, s->syn[0], gr_info->sfbtab + gr_info->n_long_sfb);
1587  }
1588 
1589  drmp3_L3_antialias(s->grbuf[ch], aa_bands);
1590  drmp3_L3_imdct_gr(s->grbuf[ch], h->mdct_overlap[ch], gr_info->block_type, n_long_bands);
1591  drmp3_L3_change_sign(s->grbuf[ch]);
1592  }
1593 }
1594 
1595 static void drmp3d_DCT_II(float *grbuf, int n)
1596 {
1597  static const float g_sec[24] = {
1598  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
1599  };
1600  int i, k = 0;
1601 #if DRMP3_HAVE_SIMD
1602  if (drmp3_have_simd()) for (; k < n; k += 4)
1603  {
1604  drmp3_f4 t[4][8], *x;
1605  float *y = grbuf + k;
1606 
1607  for (x = t[0], i = 0; i < 8; i++, x++)
1608  {
1609  drmp3_f4 x0 = DRMP3_VLD(&y[i*18]);
1610  drmp3_f4 x1 = DRMP3_VLD(&y[(15 - i)*18]);
1611  drmp3_f4 x2 = DRMP3_VLD(&y[(16 + i)*18]);
1612  drmp3_f4 x3 = DRMP3_VLD(&y[(31 - i)*18]);
1613  drmp3_f4 t0 = DRMP3_VADD(x0, x3);
1614  drmp3_f4 t1 = DRMP3_VADD(x1, x2);
1615  drmp3_f4 t2 = DRMP3_VMUL_S(DRMP3_VSUB(x1, x2), g_sec[3*i + 0]);
1616  drmp3_f4 t3 = DRMP3_VMUL_S(DRMP3_VSUB(x0, x3), g_sec[3*i + 1]);
1617  x[0] = DRMP3_VADD(t0, t1);
1618  x[8] = DRMP3_VMUL_S(DRMP3_VSUB(t0, t1), g_sec[3*i + 2]);
1619  x[16] = DRMP3_VADD(t3, t2);
1620  x[24] = DRMP3_VMUL_S(DRMP3_VSUB(t3, t2), g_sec[3*i + 2]);
1621  }
1622  for (x = t[0], i = 0; i < 4; i++, x += 8)
1623  {
1624  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;
1625  xt = DRMP3_VSUB(x0, x7); x0 = DRMP3_VADD(x0, x7);
1626  x7 = DRMP3_VSUB(x1, x6); x1 = DRMP3_VADD(x1, x6);
1627  x6 = DRMP3_VSUB(x2, x5); x2 = DRMP3_VADD(x2, x5);
1628  x5 = DRMP3_VSUB(x3, x4); x3 = DRMP3_VADD(x3, x4);
1629  x4 = DRMP3_VSUB(x0, x3); x0 = DRMP3_VADD(x0, x3);
1630  x3 = DRMP3_VSUB(x1, x2); x1 = DRMP3_VADD(x1, x2);
1631  x[0] = DRMP3_VADD(x0, x1);
1632  x[4] = DRMP3_VMUL_S(DRMP3_VSUB(x0, x1), 0.70710677f);
1633  x5 = DRMP3_VADD(x5, x6);
1634  x6 = DRMP3_VMUL_S(DRMP3_VADD(x6, x7), 0.70710677f);
1635  x7 = DRMP3_VADD(x7, xt);
1636  x3 = DRMP3_VMUL_S(DRMP3_VADD(x3, x4), 0.70710677f);
1637  x5 = DRMP3_VSUB(x5, DRMP3_VMUL_S(x7, 0.198912367f)); /* rotate by PI/8 */
1638  x7 = DRMP3_VADD(x7, DRMP3_VMUL_S(x5, 0.382683432f));
1639  x5 = DRMP3_VSUB(x5, DRMP3_VMUL_S(x7, 0.198912367f));
1640  x0 = DRMP3_VSUB(xt, x6); xt = DRMP3_VADD(xt, x6);
1641  x[1] = DRMP3_VMUL_S(DRMP3_VADD(xt, x7), 0.50979561f);
1642  x[2] = DRMP3_VMUL_S(DRMP3_VADD(x4, x3), 0.54119611f);
1643  x[3] = DRMP3_VMUL_S(DRMP3_VSUB(x0, x5), 0.60134488f);
1644  x[5] = DRMP3_VMUL_S(DRMP3_VADD(x0, x5), 0.89997619f);
1645  x[6] = DRMP3_VMUL_S(DRMP3_VSUB(x4, x3), 1.30656302f);
1646  x[7] = DRMP3_VMUL_S(DRMP3_VSUB(xt, x7), 2.56291556f);
1647  }
1648 
1649  if (k > n - 3)
1650  {
1651 #if DRMP3_HAVE_SSE
1652 #define DRMP3_VSAVE2(i, v) _mm_storel_pi((__m64 *)(void*)&y[i*18], v)
1653 #else
1654 #define DRMP3_VSAVE2(i, v) vst1_f32((float32_t *)&y[i*18], vget_low_f32(v))
1655 #endif
1656  for (i = 0; i < 7; i++, y += 4*18)
1657  {
1658  drmp3_f4 s = DRMP3_VADD(t[3][i], t[3][i + 1]);
1659  DRMP3_VSAVE2(0, t[0][i]);
1660  DRMP3_VSAVE2(1, DRMP3_VADD(t[2][i], s));
1661  DRMP3_VSAVE2(2, DRMP3_VADD(t[1][i], t[1][i + 1]));
1662  DRMP3_VSAVE2(3, DRMP3_VADD(t[2][1 + i], s));
1663  }
1664  DRMP3_VSAVE2(0, t[0][7]);
1665  DRMP3_VSAVE2(1, DRMP3_VADD(t[2][7], t[3][7]));
1666  DRMP3_VSAVE2(2, t[1][7]);
1667  DRMP3_VSAVE2(3, t[3][7]);
1668  } else
1669  {
1670 #define DRMP3_VSAVE4(i, v) DRMP3_VSTORE(&y[i*18], v)
1671  for (i = 0; i < 7; i++, y += 4*18)
1672  {
1673  drmp3_f4 s = DRMP3_VADD(t[3][i], t[3][i + 1]);
1674  DRMP3_VSAVE4(0, t[0][i]);
1675  DRMP3_VSAVE4(1, DRMP3_VADD(t[2][i], s));
1676  DRMP3_VSAVE4(2, DRMP3_VADD(t[1][i], t[1][i + 1]));
1677  DRMP3_VSAVE4(3, DRMP3_VADD(t[2][1 + i], s));
1678  }
1679  DRMP3_VSAVE4(0, t[0][7]);
1680  DRMP3_VSAVE4(1, DRMP3_VADD(t[2][7], t[3][7]));
1681  DRMP3_VSAVE4(2, t[1][7]);
1682  DRMP3_VSAVE4(3, t[3][7]);
1683  }
1684  } else
1685 #endif
1686 #ifdef DR_MP3_ONLY_SIMD
1687  {}
1688 #else
1689  for (; k < n; k++)
1690  {
1691  float t[4][8], *x, *y = grbuf + k;
1692 
1693  for (x = t[0], i = 0; i < 8; i++, x++)
1694  {
1695  float x0 = y[i*18];
1696  float x1 = y[(15 - i)*18];
1697  float x2 = y[(16 + i)*18];
1698  float x3 = y[(31 - i)*18];
1699  float t0 = x0 + x3;
1700  float t1 = x1 + x2;
1701  float t2 = (x1 - x2)*g_sec[3*i + 0];
1702  float t3 = (x0 - x3)*g_sec[3*i + 1];
1703  x[0] = t0 + t1;
1704  x[8] = (t0 - t1)*g_sec[3*i + 2];
1705  x[16] = t3 + t2;
1706  x[24] = (t3 - t2)*g_sec[3*i + 2];
1707  }
1708  for (x = t[0], i = 0; i < 4; i++, x += 8)
1709  {
1710  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;
1711  xt = x0 - x7; x0 += x7;
1712  x7 = x1 - x6; x1 += x6;
1713  x6 = x2 - x5; x2 += x5;
1714  x5 = x3 - x4; x3 += x4;
1715  x4 = x0 - x3; x0 += x3;
1716  x3 = x1 - x2; x1 += x2;
1717  x[0] = x0 + x1;
1718  x[4] = (x0 - x1)*0.70710677f;
1719  x5 = x5 + x6;
1720  x6 = (x6 + x7)*0.70710677f;
1721  x7 = x7 + xt;
1722  x3 = (x3 + x4)*0.70710677f;
1723  x5 -= x7*0.198912367f; /* rotate by PI/8 */
1724  x7 += x5*0.382683432f;
1725  x5 -= x7*0.198912367f;
1726  x0 = xt - x6; xt += x6;
1727  x[1] = (xt + x7)*0.50979561f;
1728  x[2] = (x4 + x3)*0.54119611f;
1729  x[3] = (x0 - x5)*0.60134488f;
1730  x[5] = (x0 + x5)*0.89997619f;
1731  x[6] = (x4 - x3)*1.30656302f;
1732  x[7] = (xt - x7)*2.56291556f;
1733 
1734  }
1735  for (i = 0; i < 7; i++, y += 4*18)
1736  {
1737  y[0*18] = t[0][i];
1738  y[1*18] = t[2][i] + t[3][i] + t[3][i + 1];
1739  y[2*18] = t[1][i] + t[1][i + 1];
1740  y[3*18] = t[2][i + 1] + t[3][i] + t[3][i + 1];
1741  }
1742  y[0*18] = t[0][7];
1743  y[1*18] = t[2][7] + t[3][7];
1744  y[2*18] = t[1][7];
1745  y[3*18] = t[3][7];
1746  }
1747 #endif
1748 }
1749 
1750 #ifndef DR_MP3_FLOAT_OUTPUT
1751 typedef drmp3_int16 drmp3d_sample_t;
1752 
1753 static drmp3_int16 drmp3d_scale_pcm(float sample)
1754 {
1755  drmp3_int16 s;
1756  if (sample >= 32766.5) return (drmp3_int16) 32767;
1757  if (sample <= -32767.5) return (drmp3_int16)-32768;
1758  s = (drmp3_int16)(sample + .5f);
1759  s -= (s < 0); /* away from zero, to be compliant */
1760  return (drmp3_int16)s;
1761 }
1762 #else
1763 typedef float drmp3d_sample_t;
1764 
1765 static float drmp3d_scale_pcm(float sample)
1766 {
1767  return sample*(1.f/32768.f);
1768 }
1769 #endif
1770 
1771 static void drmp3d_synth_pair(drmp3d_sample_t *pcm, int nch, const float *z)
1772 {
1773  float a;
1774  a = (z[14*64] - z[ 0]) * 29;
1775  a += (z[ 1*64] + z[13*64]) * 213;
1776  a += (z[12*64] - z[ 2*64]) * 459;
1777  a += (z[ 3*64] + z[11*64]) * 2037;
1778  a += (z[10*64] - z[ 4*64]) * 5153;
1779  a += (z[ 5*64] + z[ 9*64]) * 6574;
1780  a += (z[ 8*64] - z[ 6*64]) * 37489;
1781  a += z[ 7*64] * 75038;
1782  pcm[0] = drmp3d_scale_pcm(a);
1783 
1784  z += 2;
1785  a = z[14*64] * 104;
1786  a += z[12*64] * 1567;
1787  a += z[10*64] * 9727;
1788  a += z[ 8*64] * 64019;
1789  a += z[ 6*64] * -9975;
1790  a += z[ 4*64] * -45;
1791  a += z[ 2*64] * 146;
1792  a += z[ 0*64] * -5;
1793  pcm[16*nch] = drmp3d_scale_pcm(a);
1794 }
1795 
1796 static void drmp3d_synth(float *xl, drmp3d_sample_t *dstl, int nch, float *lins)
1797 {
1798  int i;
1799  float *xr = xl + 576*(nch - 1);
1800  drmp3d_sample_t *dstr = dstl + (nch - 1);
1801 
1802  static const float g_win[] = {
1803  -1,26,-31,208,218,401,-519,2063,2000,4788,-5517,7134,5959,35640,-39336,74992,
1804  -1,24,-35,202,222,347,-581,2080,1952,4425,-5879,7640,5288,33791,-41176,74856,
1805  -1,21,-38,196,225,294,-645,2087,1893,4063,-6237,8092,4561,31947,-43006,74630,
1806  -1,19,-41,190,227,244,-711,2085,1822,3705,-6589,8492,3776,30112,-44821,74313,
1807  -1,17,-45,183,228,197,-779,2075,1739,3351,-6935,8840,2935,28289,-46617,73908,
1808  -1,16,-49,176,228,153,-848,2057,1644,3004,-7271,9139,2037,26482,-48390,73415,
1809  -2,14,-53,169,227,111,-919,2032,1535,2663,-7597,9389,1082,24694,-50137,72835,
1810  -2,13,-58,161,224,72,-991,2001,1414,2330,-7910,9592,70,22929,-51853,72169,
1811  -2,11,-63,154,221,36,-1064,1962,1280,2006,-8209,9750,-998,21189,-53534,71420,
1812  -2,10,-68,147,215,2,-1137,1919,1131,1692,-8491,9863,-2122,19478,-55178,70590,
1813  -3,9,-73,139,208,-29,-1210,1870,970,1388,-8755,9935,-3300,17799,-56778,69679,
1814  -3,8,-79,132,200,-57,-1283,1817,794,1095,-8998,9966,-4533,16155,-58333,68692,
1815  -4,7,-85,125,189,-83,-1356,1759,605,814,-9219,9959,-5818,14548,-59838,67629,
1816  -4,7,-91,117,177,-106,-1428,1698,402,545,-9416,9916,-7154,12980,-61289,66494,
1817  -5,6,-97,111,163,-127,-1498,1634,185,288,-9585,9838,-8540,11455,-62684,65290
1818  };
1819  float *zlin = lins + 15*64;
1820  const float *w = g_win;
1821 
1822  zlin[4*15] = xl[18*16];
1823  zlin[4*15 + 1] = xr[18*16];
1824  zlin[4*15 + 2] = xl[0];
1825  zlin[4*15 + 3] = xr[0];
1826 
1827  zlin[4*31] = xl[1 + 18*16];
1828  zlin[4*31 + 1] = xr[1 + 18*16];
1829  zlin[4*31 + 2] = xl[1];
1830  zlin[4*31 + 3] = xr[1];
1831 
1832  drmp3d_synth_pair(dstr, nch, lins + 4*15 + 1);
1833  drmp3d_synth_pair(dstr + 32*nch, nch, lins + 4*15 + 64 + 1);
1834  drmp3d_synth_pair(dstl, nch, lins + 4*15);
1835  drmp3d_synth_pair(dstl + 32*nch, nch, lins + 4*15 + 64);
1836 
1837 #if DRMP3_HAVE_SIMD
1838  if (drmp3_have_simd()) for (i = 14; i >= 0; i--)
1839  {
1840 #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)]);
1841 #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)); }
1842 #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))); }
1843 #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))); }
1844  drmp3_f4 a, b;
1845  zlin[4*i] = xl[18*(31 - i)];
1846  zlin[4*i + 1] = xr[18*(31 - i)];
1847  zlin[4*i + 2] = xl[1 + 18*(31 - i)];
1848  zlin[4*i + 3] = xr[1 + 18*(31 - i)];
1849  zlin[4*i + 64] = xl[1 + 18*(1 + i)];
1850  zlin[4*i + 64 + 1] = xr[1 + 18*(1 + i)];
1851  zlin[4*i - 64 + 2] = xl[18*(1 + i)];
1852  zlin[4*i - 64 + 3] = xr[18*(1 + i)];
1853 
1854  DRMP3_V0(0) DRMP3_V2(1) DRMP3_V1(2) DRMP3_V2(3) DRMP3_V1(4) DRMP3_V2(5) DRMP3_V1(6) DRMP3_V2(7)
1855 
1856  {
1857 #ifndef DR_MP3_FLOAT_OUTPUT
1858 #if DRMP3_HAVE_SSE
1859  static const drmp3_f4 g_max = { 32767.0f, 32767.0f, 32767.0f, 32767.0f };
1860  static const drmp3_f4 g_min = { -32768.0f, -32768.0f, -32768.0f, -32768.0f };
1861  __m128i pcm8 = _mm_packs_epi32(_mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(a, g_max), g_min)),
1862  _mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(b, g_max), g_min)));
1863  dstr[(15 - i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 1);
1864  dstr[(17 + i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 5);
1865  dstl[(15 - i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 0);
1866  dstl[(17 + i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 4);
1867  dstr[(47 - i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 3);
1868  dstr[(49 + i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 7);
1869  dstl[(47 - i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 2);
1870  dstl[(49 + i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 6);
1871 #else
1872  int16x4_t pcma, pcmb;
1873  a = DRMP3_VADD(a, DRMP3_VSET(0.5f));
1874  b = DRMP3_VADD(b, DRMP3_VSET(0.5f));
1875  pcma = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(a), vreinterpretq_s32_u32(vcltq_f32(a, DRMP3_VSET(0)))));
1876  pcmb = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(b), vreinterpretq_s32_u32(vcltq_f32(b, DRMP3_VSET(0)))));
1877  vst1_lane_s16(dstr + (15 - i)*nch, pcma, 1);
1878  vst1_lane_s16(dstr + (17 + i)*nch, pcmb, 1);
1879  vst1_lane_s16(dstl + (15 - i)*nch, pcma, 0);
1880  vst1_lane_s16(dstl + (17 + i)*nch, pcmb, 0);
1881  vst1_lane_s16(dstr + (47 - i)*nch, pcma, 3);
1882  vst1_lane_s16(dstr + (49 + i)*nch, pcmb, 3);
1883  vst1_lane_s16(dstl + (47 - i)*nch, pcma, 2);
1884  vst1_lane_s16(dstl + (49 + i)*nch, pcmb, 2);
1885 #endif
1886 #else
1887  static const drmp3_f4 g_scale = { 1.0f/32768.0f, 1.0f/32768.0f, 1.0f/32768.0f, 1.0f/32768.0f };
1888  a = DRMP3_VMUL(a, g_scale);
1889  b = DRMP3_VMUL(b, g_scale);
1890 #if DRMP3_HAVE_SSE
1891  _mm_store_ss(dstr + (15 - i)*nch, _mm_shuffle_ps(a, a, _MM_SHUFFLE(1, 1, 1, 1)));
1892  _mm_store_ss(dstr + (17 + i)*nch, _mm_shuffle_ps(b, b, _MM_SHUFFLE(1, 1, 1, 1)));
1893  _mm_store_ss(dstl + (15 - i)*nch, _mm_shuffle_ps(a, a, _MM_SHUFFLE(0, 0, 0, 0)));
1894  _mm_store_ss(dstl + (17 + i)*nch, _mm_shuffle_ps(b, b, _MM_SHUFFLE(0, 0, 0, 0)));
1895  _mm_store_ss(dstr + (47 - i)*nch, _mm_shuffle_ps(a, a, _MM_SHUFFLE(3, 3, 3, 3)));
1896  _mm_store_ss(dstr + (49 + i)*nch, _mm_shuffle_ps(b, b, _MM_SHUFFLE(3, 3, 3, 3)));
1897  _mm_store_ss(dstl + (47 - i)*nch, _mm_shuffle_ps(a, a, _MM_SHUFFLE(2, 2, 2, 2)));
1898  _mm_store_ss(dstl + (49 + i)*nch, _mm_shuffle_ps(b, b, _MM_SHUFFLE(2, 2, 2, 2)));
1899 #else
1900  vst1q_lane_f32(dstr + (15 - i)*nch, a, 1);
1901  vst1q_lane_f32(dstr + (17 + i)*nch, b, 1);
1902  vst1q_lane_f32(dstl + (15 - i)*nch, a, 0);
1903  vst1q_lane_f32(dstl + (17 + i)*nch, b, 0);
1904  vst1q_lane_f32(dstr + (47 - i)*nch, a, 3);
1905  vst1q_lane_f32(dstr + (49 + i)*nch, b, 3);
1906  vst1q_lane_f32(dstl + (47 - i)*nch, a, 2);
1907  vst1q_lane_f32(dstl + (49 + i)*nch, b, 2);
1908 #endif
1909 #endif /* DR_MP3_FLOAT_OUTPUT */
1910  }
1911  } else
1912 #endif
1913 #ifdef DR_MP3_ONLY_SIMD
1914  {}
1915 #else
1916  for (i = 14; i >= 0; i--)
1917  {
1918 #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];
1919 #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; }
1920 #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; }
1921 #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; }
1922  float a[4], b[4];
1923 
1924  zlin[4*i] = xl[18*(31 - i)];
1925  zlin[4*i + 1] = xr[18*(31 - i)];
1926  zlin[4*i + 2] = xl[1 + 18*(31 - i)];
1927  zlin[4*i + 3] = xr[1 + 18*(31 - i)];
1928  zlin[4*(i + 16)] = xl[1 + 18*(1 + i)];
1929  zlin[4*(i + 16) + 1] = xr[1 + 18*(1 + i)];
1930  zlin[4*(i - 16) + 2] = xl[18*(1 + i)];
1931  zlin[4*(i - 16) + 3] = xr[18*(1 + i)];
1932 
1933  DRMP3_S0(0) DRMP3_S2(1) DRMP3_S1(2) DRMP3_S2(3) DRMP3_S1(4) DRMP3_S2(5) DRMP3_S1(6) DRMP3_S2(7)
1934 
1935  dstr[(15 - i)*nch] = drmp3d_scale_pcm(a[1]);
1936  dstr[(17 + i)*nch] = drmp3d_scale_pcm(b[1]);
1937  dstl[(15 - i)*nch] = drmp3d_scale_pcm(a[0]);
1938  dstl[(17 + i)*nch] = drmp3d_scale_pcm(b[0]);
1939  dstr[(47 - i)*nch] = drmp3d_scale_pcm(a[3]);
1940  dstr[(49 + i)*nch] = drmp3d_scale_pcm(b[3]);
1941  dstl[(47 - i)*nch] = drmp3d_scale_pcm(a[2]);
1942  dstl[(49 + i)*nch] = drmp3d_scale_pcm(b[2]);
1943  }
1944 #endif
1945 }
1946 
1947 static void drmp3d_synth_granule(float *qmf_state, float *grbuf, int nbands, int nch, drmp3d_sample_t *pcm, float *lins)
1948 {
1949  int i;
1950  for (i = 0; i < nch; i++)
1951  {
1952  drmp3d_DCT_II(grbuf + 576*i, nbands);
1953  }
1954 
1955  memcpy(lins, qmf_state, sizeof(float)*15*64);
1956 
1957  for (i = 0; i < nbands; i += 2)
1958  {
1959  drmp3d_synth(grbuf + i, pcm + 32*nch*i, nch, lins + i*64);
1960  }
1961 #ifndef DR_MP3_NONSTANDARD_BUT_LOGICAL
1962  if (nch == 1)
1963  {
1964  for (i = 0; i < 15*64; i += 2)
1965  {
1966  qmf_state[i] = lins[nbands*64 + i];
1967  }
1968  } else
1969 #endif
1970  {
1971  memcpy(qmf_state, lins + nbands*64, sizeof(float)*15*64);
1972  }
1973 }
1974 
1975 static int drmp3d_match_frame(const drmp3_uint8 *hdr, int mp3_bytes, int frame_bytes)
1976 {
1977  int i, nmatch;
1978  for (i = 0, nmatch = 0; nmatch < DRMP3_MAX_FRAME_SYNC_MATCHES; nmatch++)
1979  {
1980  i += drmp3_hdr_frame_bytes(hdr + i, frame_bytes) + drmp3_hdr_padding(hdr + i);
1981  if (i + DRMP3_HDR_SIZE > mp3_bytes)
1982  return nmatch > 0;
1983  if (!drmp3_hdr_compare(hdr, hdr + i))
1984  return 0;
1985  }
1986  return 1;
1987 }
1988 
1989 static int drmp3d_find_frame(const drmp3_uint8 *mp3, int mp3_bytes, int *free_format_bytes, int *ptr_frame_bytes)
1990 {
1991  int i, k;
1992  for (i = 0; i < mp3_bytes - DRMP3_HDR_SIZE; i++, mp3++)
1993  {
1994  if (drmp3_hdr_valid(mp3))
1995  {
1996  int frame_bytes = drmp3_hdr_frame_bytes(mp3, *free_format_bytes);
1997  int frame_and_padding = frame_bytes + drmp3_hdr_padding(mp3);
1998 
1999  for (k = DRMP3_HDR_SIZE; !frame_bytes && k < DRMP3_MAX_FREE_FORMAT_FRAME_SIZE && i + 2*k < mp3_bytes - DRMP3_HDR_SIZE; k++)
2000  {
2001  if (drmp3_hdr_compare(mp3, mp3 + k))
2002  {
2003  int fb = k - drmp3_hdr_padding(mp3);
2004  int nextfb = fb + drmp3_hdr_padding(mp3 + k);
2005  if (i + k + nextfb + DRMP3_HDR_SIZE > mp3_bytes || !drmp3_hdr_compare(mp3, mp3 + k + nextfb))
2006  continue;
2007  frame_and_padding = k;
2008  frame_bytes = fb;
2009  *free_format_bytes = fb;
2010  }
2011  }
2012 
2013  if ((frame_bytes && i + frame_and_padding <= mp3_bytes &&
2014  drmp3d_match_frame(mp3, mp3_bytes - i, frame_bytes)) ||
2015  (!i && frame_and_padding == mp3_bytes))
2016  {
2017  *ptr_frame_bytes = frame_and_padding;
2018  return i;
2019  }
2020  *free_format_bytes = 0;
2021  }
2022  }
2023  *ptr_frame_bytes = 0;
2024  return i;
2025 }
2026 
2027 void drmp3dec_init(drmp3dec *dec)
2028 {
2029  dec->header[0] = 0;
2030 }
2031 
2032 int drmp3dec_decode_frame(drmp3dec *dec, const unsigned char *mp3, int mp3_bytes, void *pcm, drmp3dec_frame_info *info)
2033 {
2034  int i = 0, igr, frame_size = 0, success = 1;
2035  const drmp3_uint8 *hdr;
2036  drmp3_bs bs_frame[1];
2037  drmp3dec_scratch scratch;
2038 
2039  if (mp3_bytes > 4 && dec->header[0] == 0xff && drmp3_hdr_compare(dec->header, mp3))
2040  {
2041  frame_size = drmp3_hdr_frame_bytes(mp3, dec->free_format_bytes) + drmp3_hdr_padding(mp3);
2042  if (frame_size != mp3_bytes && (frame_size + DRMP3_HDR_SIZE > mp3_bytes || !drmp3_hdr_compare(mp3, mp3 + frame_size)))
2043  {
2044  frame_size = 0;
2045  }
2046  }
2047  if (!frame_size)
2048  {
2049  memset(dec, 0, sizeof(drmp3dec));
2050  i = drmp3d_find_frame(mp3, mp3_bytes, &dec->free_format_bytes, &frame_size);
2051  if (!frame_size || i + frame_size > mp3_bytes)
2052  {
2053  info->frame_bytes = i;
2054  return 0;
2055  }
2056  }
2057 
2058  hdr = mp3 + i;
2059  memcpy(dec->header, hdr, DRMP3_HDR_SIZE);
2060  info->frame_bytes = i + frame_size;
2061  info->channels = DRMP3_HDR_IS_MONO(hdr) ? 1 : 2;
2062  info->hz = drmp3_hdr_sample_rate_hz(hdr);
2063  info->layer = 4 - DRMP3_HDR_GET_LAYER(hdr);
2064  info->bitrate_kbps = drmp3_hdr_bitrate_kbps(hdr);
2065 
2066  drmp3_bs_init(bs_frame, hdr + DRMP3_HDR_SIZE, frame_size - DRMP3_HDR_SIZE);
2067  if (DRMP3_HDR_IS_CRC(hdr))
2068  {
2069  drmp3_bs_get_bits(bs_frame, 16);
2070  }
2071 
2072  if (info->layer == 3)
2073  {
2074  int main_data_begin = drmp3_L3_read_side_info(bs_frame, scratch.gr_info, hdr);
2075  if (main_data_begin < 0 || bs_frame->pos > bs_frame->limit)
2076  {
2077  drmp3dec_init(dec);
2078  return 0;
2079  }
2080  success = drmp3_L3_restore_reservoir(dec, bs_frame, &scratch, main_data_begin);
2081  if (success && pcm != NULL)
2082  {
2083  for (igr = 0; igr < (DRMP3_HDR_TEST_MPEG1(hdr) ? 2 : 1); igr++, pcm = DRMP3_OFFSET_PTR(pcm, sizeof(drmp3d_sample_t)*576*info->channels))
2084  {
2085  memset(scratch.grbuf[0], 0, 576*2*sizeof(float));
2086  drmp3_L3_decode(dec, &scratch, scratch.gr_info + igr*info->channels, info->channels);
2087  drmp3d_synth_granule(dec->qmf_state, scratch.grbuf[0], 18, info->channels, (drmp3d_sample_t*)pcm, scratch.syn[0]);
2088  }
2089  }
2090  drmp3_L3_save_reservoir(dec, &scratch);
2091  } else
2092  {
2093 #ifdef DR_MP3_ONLY_MP3
2094  return 0;
2095 #else
2096  drmp3_L12_scale_info sci[1];
2097 
2098  if (pcm == NULL) {
2099  return drmp3_hdr_frame_samples(hdr);
2100  }
2101 
2102  drmp3_L12_read_scale_info(hdr, bs_frame, sci);
2103 
2104  memset(scratch.grbuf[0], 0, 576*2*sizeof(float));
2105  for (i = 0, igr = 0; igr < 3; igr++)
2106  {
2107  if (12 == (i += drmp3_L12_dequantize_granule(scratch.grbuf[0] + i, bs_frame, sci, info->layer | 1)))
2108  {
2109  i = 0;
2110  drmp3_L12_apply_scf_384(sci, sci->scf + igr, scratch.grbuf[0]);
2111  drmp3d_synth_granule(dec->qmf_state, scratch.grbuf[0], 12, info->channels, (drmp3d_sample_t*)pcm, scratch.syn[0]);
2112  memset(scratch.grbuf[0], 0, 576*2*sizeof(float));
2113  pcm = DRMP3_OFFSET_PTR(pcm, sizeof(drmp3d_sample_t)*384*info->channels);
2114  }
2115  if (bs_frame->pos > bs_frame->limit)
2116  {
2117  drmp3dec_init(dec);
2118  return 0;
2119  }
2120  }
2121 #endif
2122  }
2123 
2124  return success*drmp3_hdr_frame_samples(dec->header);
2125 }
2126 
2127 void drmp3dec_f32_to_s16(const float *in, drmp3_int16 *out, int num_samples)
2128 {
2129  if(num_samples > 0)
2130  {
2131  int i = 0;
2132 #if DRMP3_HAVE_SIMD
2133  int aligned_count = num_samples & ~7;
2134  for(; i < aligned_count; i+=8)
2135  {
2136  static const drmp3_f4 g_scale = { 32768.0f, 32768.0f, 32768.0f, 32768.0f };
2137  drmp3_f4 a = DRMP3_VMUL(DRMP3_VLD(&in[i ]), g_scale);
2138  drmp3_f4 b = DRMP3_VMUL(DRMP3_VLD(&in[i+4]), g_scale);
2139 #if DRMP3_HAVE_SSE
2140  static const drmp3_f4 g_max = { 32767.0f, 32767.0f, 32767.0f, 32767.0f };
2141  static const drmp3_f4 g_min = { -32768.0f, -32768.0f, -32768.0f, -32768.0f };
2142  __m128i pcm8 = _mm_packs_epi32(_mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(a, g_max), g_min)),
2143  _mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(b, g_max), g_min)));
2144  out[i ] = (drmp3_int16)_mm_extract_epi16(pcm8, 0);
2145  out[i+1] = (drmp3_int16)_mm_extract_epi16(pcm8, 1);
2146  out[i+2] = (drmp3_int16)_mm_extract_epi16(pcm8, 2);
2147  out[i+3] = (drmp3_int16)_mm_extract_epi16(pcm8, 3);
2148  out[i+4] = (drmp3_int16)_mm_extract_epi16(pcm8, 4);
2149  out[i+5] = (drmp3_int16)_mm_extract_epi16(pcm8, 5);
2150  out[i+6] = (drmp3_int16)_mm_extract_epi16(pcm8, 6);
2151  out[i+7] = (drmp3_int16)_mm_extract_epi16(pcm8, 7);
2152 #else
2153  int16x4_t pcma, pcmb;
2154  a = DRMP3_VADD(a, DRMP3_VSET(0.5f));
2155  b = DRMP3_VADD(b, DRMP3_VSET(0.5f));
2156  pcma = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(a), vreinterpretq_s32_u32(vcltq_f32(a, DRMP3_VSET(0)))));
2157  pcmb = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(b), vreinterpretq_s32_u32(vcltq_f32(b, DRMP3_VSET(0)))));
2158  vst1_lane_s16(out+i , pcma, 0);
2159  vst1_lane_s16(out+i+1, pcma, 1);
2160  vst1_lane_s16(out+i+2, pcma, 2);
2161  vst1_lane_s16(out+i+3, pcma, 3);
2162  vst1_lane_s16(out+i+4, pcmb, 0);
2163  vst1_lane_s16(out+i+5, pcmb, 1);
2164  vst1_lane_s16(out+i+6, pcmb, 2);
2165  vst1_lane_s16(out+i+7, pcmb, 3);
2166 #endif
2167  }
2168 #endif
2169  for(; i < num_samples; i++)
2170  {
2171  float sample = in[i] * 32768.0f;
2172  if (sample >= 32766.5)
2173  out[i] = (drmp3_int16) 32767;
2174  else if (sample <= -32767.5)
2175  out[i] = (drmp3_int16)-32768;
2176  else
2177  {
2178  short s = (drmp3_int16)(sample + .5f);
2179  s -= (s < 0); /* away from zero, to be compliant */
2180  out[i] = s;
2181  }
2182  }
2183  }
2184 }
2185 
2186 
2187 
2188 /************************************************************************************************************************************************************
2189 
2190  Main Public API
2191 
2192  ************************************************************************************************************************************************************/
2193 
2194 #if defined(SIZE_MAX)
2195  #define DRMP3_SIZE_MAX SIZE_MAX
2196 #else
2197  #if defined(_WIN64) || defined(_LP64) || defined(__LP64__)
2198  #define DRMP3_SIZE_MAX ((drmp3_uint64)0xFFFFFFFFFFFFFFFF)
2199  #else
2200  #define DRMP3_SIZE_MAX 0xFFFFFFFF
2201  #endif
2202 #endif
2203 
2204 /* Options. */
2205 #ifndef DRMP3_SEEK_LEADING_MP3_FRAMES
2206 #define DRMP3_SEEK_LEADING_MP3_FRAMES 2
2207 #endif
2208 
2209 
2210 /* Standard library stuff. */
2211 #ifndef DRMP3_ASSERT
2212 #include <assert.h>
2213 #define DRMP3_ASSERT(expression) assert(expression)
2214 #endif
2215 #ifndef DRMP3_COPY_MEMORY
2216 #define DRMP3_COPY_MEMORY(dst, src, sz) memcpy((dst), (src), (sz))
2217 #endif
2218 #ifndef DRMP3_ZERO_MEMORY
2219 #define DRMP3_ZERO_MEMORY(p, sz) memset((p), 0, (sz))
2220 #endif
2221 #define DRMP3_ZERO_OBJECT(p) DRMP3_ZERO_MEMORY((p), sizeof(*(p)))
2222 #ifndef DRMP3_MALLOC
2223 #define DRMP3_MALLOC(sz) malloc((sz))
2224 #endif
2225 #ifndef DRMP3_REALLOC
2226 #define DRMP3_REALLOC(p, sz) realloc((p), (sz))
2227 #endif
2228 #ifndef DRMP3_FREE
2229 #define DRMP3_FREE(p) free((p))
2230 #endif
2231 
2232 #define drmp3_assert DRMP3_ASSERT
2233 #define drmp3_copy_memory DRMP3_COPY_MEMORY
2234 #define drmp3_zero_memory DRMP3_ZERO_MEMORY
2235 #define drmp3_zero_object DRMP3_ZERO_OBJECT
2236 #define drmp3_malloc DRMP3_MALLOC
2237 #define drmp3_realloc DRMP3_REALLOC
2238 
2239 #define drmp3_countof(x) (sizeof(x) / sizeof(x[0]))
2240 #define drmp3_max(x, y) (((x) > (y)) ? (x) : (y))
2241 #define drmp3_min(x, y) (((x) < (y)) ? (x) : (y))
2242 
2243 #define DRMP3_DATA_CHUNK_SIZE 16384 /* The size in bytes of each chunk of data to read from the MP3 stream. minimp3 recommends 16K. */
2244 
2245 static DRMP3_INLINE float drmp3_mix_f32(float x, float y, float a)
2246 {
2247  return x*(1-a) + y*a;
2248 }
2249 
2250 static void drmp3_blend_f32(float* pOut, float* pInA, float* pInB, float factor, drmp3_uint32 channels)
2251 {
2252  drmp3_uint32 i;
2253  for (i = 0; i < channels; ++i) {
2254  pOut[i] = drmp3_mix_f32(pInA[i], pInB[i], factor);
2255  }
2256 }
2257 
2258 void drmp3_src_cache_init(drmp3_src* pSRC, drmp3_src_cache* pCache)
2259 {
2260  drmp3_assert(pSRC != NULL);
2261  drmp3_assert(pCache != NULL);
2262 
2263  pCache->pSRC = pSRC;
2264  pCache->cachedFrameCount = 0;
2265  pCache->iNextFrame = 0;
2266 }
2267 
2268 drmp3_uint64 drmp3_src_cache_read_frames(drmp3_src_cache* pCache, drmp3_uint64 frameCount, float* pFramesOut)
2269 {
2271  drmp3_uint64 totalFramesRead = 0;
2272 
2273  drmp3_assert(pCache != NULL);
2274  drmp3_assert(pCache->pSRC != NULL);
2275  drmp3_assert(pCache->pSRC->onRead != NULL);
2276  drmp3_assert(frameCount > 0);
2277  drmp3_assert(pFramesOut != NULL);
2278 
2279  channels = pCache->pSRC->config.channels;
2280 
2281  while (frameCount > 0) {
2282  /* If there's anything in memory go ahead and copy that over first. */
2283  drmp3_uint32 framesToReadFromClient;
2284  drmp3_uint64 framesRemainingInMemory = pCache->cachedFrameCount - pCache->iNextFrame;
2285  drmp3_uint64 framesToReadFromMemory = frameCount;
2286  if (framesToReadFromMemory > framesRemainingInMemory) {
2287  framesToReadFromMemory = framesRemainingInMemory;
2288  }
2289 
2290  drmp3_copy_memory(pFramesOut, pCache->pCachedFrames + pCache->iNextFrame*channels, (drmp3_uint32)(framesToReadFromMemory * channels * sizeof(float)));
2291  pCache->iNextFrame += (drmp3_uint32)framesToReadFromMemory;
2292 
2293  totalFramesRead += framesToReadFromMemory;
2294  frameCount -= framesToReadFromMemory;
2295  if (frameCount == 0) {
2296  break;
2297  }
2298 
2299 
2300  /* At this point there are still more frames to read from the client, so we'll need to reload the cache with fresh data. */
2301  drmp3_assert(frameCount > 0);
2302  pFramesOut += framesToReadFromMemory * channels;
2303 
2304  pCache->iNextFrame = 0;
2305  pCache->cachedFrameCount = 0;
2306 
2307  framesToReadFromClient = drmp3_countof(pCache->pCachedFrames) / pCache->pSRC->config.channels;
2308  if (framesToReadFromClient > pCache->pSRC->config.cacheSizeInFrames) {
2309  framesToReadFromClient = pCache->pSRC->config.cacheSizeInFrames;
2310  }
2311 
2312  pCache->cachedFrameCount = (drmp3_uint32)pCache->pSRC->onRead(pCache->pSRC, framesToReadFromClient, pCache->pCachedFrames, pCache->pSRC->pUserData);
2313 
2314 
2315  /* Get out of this loop if nothing was able to be retrieved. */
2316  if (pCache->cachedFrameCount == 0) {
2317  break;
2318  }
2319  }
2320 
2321  return totalFramesRead;
2322 }
2323 
2324 
2325 drmp3_uint64 drmp3_src_read_frames_passthrough(drmp3_src* pSRC, drmp3_uint64 frameCount, void* pFramesOut, drmp3_bool32 flush);
2326 drmp3_uint64 drmp3_src_read_frames_linear(drmp3_src* pSRC, drmp3_uint64 frameCount, void* pFramesOut, drmp3_bool32 flush);
2327 
2328 drmp3_bool32 drmp3_src_init(const drmp3_src_config* pConfig, drmp3_src_read_proc onRead, void* pUserData, drmp3_src* pSRC)
2329 {
2330  if (pSRC == NULL) {
2331  return DRMP3_FALSE;
2332  }
2333 
2334  drmp3_zero_object(pSRC);
2335 
2336  if (pConfig == NULL || onRead == NULL) {
2337  return DRMP3_FALSE;
2338  }
2339 
2340  if (pConfig->channels == 0 || pConfig->channels > 2) {
2341  return DRMP3_FALSE;
2342  }
2343 
2344  pSRC->config = *pConfig;
2345  pSRC->onRead = onRead;
2346  pSRC->pUserData = pUserData;
2347 
2350  }
2351 
2352  drmp3_src_cache_init(pSRC, &pSRC->cache);
2353  return DRMP3_TRUE;
2354 }
2355 
2356 drmp3_bool32 drmp3_src_set_input_sample_rate(drmp3_src* pSRC, drmp3_uint32 sampleRateIn)
2357 {
2358  if (pSRC == NULL) {
2359  return DRMP3_FALSE;
2360  }
2361 
2362  /* Must have a sample rate of > 0. */
2363  if (sampleRateIn == 0) {
2364  return DRMP3_FALSE;
2365  }
2366 
2367  pSRC->config.sampleRateIn = sampleRateIn;
2368  return DRMP3_TRUE;
2369 }
2370 
2371 drmp3_bool32 drmp3_src_set_output_sample_rate(drmp3_src* pSRC, drmp3_uint32 sampleRateOut)
2372 {
2373  if (pSRC == NULL) {
2374  return DRMP3_FALSE;
2375  }
2376 
2377  /* Must have a sample rate of > 0. */
2378  if (sampleRateOut == 0) {
2379  return DRMP3_FALSE;
2380  }
2381 
2382  pSRC->config.sampleRateOut = sampleRateOut;
2383  return DRMP3_TRUE;
2384 }
2385 
2386 drmp3_uint64 drmp3_src_read_frames_ex(drmp3_src* pSRC, drmp3_uint64 frameCount, void* pFramesOut, drmp3_bool32 flush)
2387 {
2388  drmp3_src_algorithm algorithm;
2389 
2390  if (pSRC == NULL || frameCount == 0 || pFramesOut == NULL) {
2391  return 0;
2392  }
2393 
2394  algorithm = pSRC->config.algorithm;
2395 
2396  /* Always use passthrough if the sample rates are the same. */
2397  if (pSRC->config.sampleRateIn == pSRC->config.sampleRateOut) {
2398  algorithm = drmp3_src_algorithm_none;
2399  }
2400 
2401  /* Could just use a function pointer instead of a switch for this... */
2402  switch (algorithm)
2403  {
2404  case drmp3_src_algorithm_none: return drmp3_src_read_frames_passthrough(pSRC, frameCount, pFramesOut, flush);
2405  case drmp3_src_algorithm_linear: return drmp3_src_read_frames_linear(pSRC, frameCount, pFramesOut, flush);
2406  default: return 0;
2407  }
2408 }
2409 
2410 drmp3_uint64 drmp3_src_read_frames(drmp3_src* pSRC, drmp3_uint64 frameCount, void* pFramesOut)
2411 {
2412  return drmp3_src_read_frames_ex(pSRC, frameCount, pFramesOut, DRMP3_FALSE);
2413 }
2414 
2415 drmp3_uint64 drmp3_src_read_frames_passthrough(drmp3_src* pSRC, drmp3_uint64 frameCount, void* pFramesOut, drmp3_bool32 flush)
2416 {
2417  drmp3_assert(pSRC != NULL);
2418  drmp3_assert(frameCount > 0);
2419  drmp3_assert(pFramesOut != NULL);
2420 
2421  (void)flush; /* Passthrough need not care about flushing. */
2422  return pSRC->onRead(pSRC, frameCount, pFramesOut, pSRC->pUserData);
2423 }
2424 
2425 drmp3_uint64 drmp3_src_read_frames_linear(drmp3_src* pSRC, drmp3_uint64 frameCount, void* pFramesOut, drmp3_bool32 flush)
2426 {
2427  double factor;
2428  drmp3_uint64 totalFramesRead;
2429 
2430  drmp3_assert(pSRC != NULL);
2431  drmp3_assert(frameCount > 0);
2432  drmp3_assert(pFramesOut != NULL);
2433 
2434  /* For linear SRC, the bin is only 2 frames: 1 prior, 1 future. */
2435 
2436  /* Load the bin if necessary. */
2437  if (!pSRC->algo.linear.isPrevFramesLoaded) {
2438  drmp3_uint64 framesRead = drmp3_src_cache_read_frames(&pSRC->cache, 1, pSRC->bin);
2439  if (framesRead == 0) {
2440  return 0;
2441  }
2442  pSRC->algo.linear.isPrevFramesLoaded = DRMP3_TRUE;
2443  }
2444  if (!pSRC->algo.linear.isNextFramesLoaded) {
2445  drmp3_uint64 framesRead = drmp3_src_cache_read_frames(&pSRC->cache, 1, pSRC->bin + pSRC->config.channels);
2446  if (framesRead == 0) {
2447  return 0;
2448  }
2449  pSRC->algo.linear.isNextFramesLoaded = DRMP3_TRUE;
2450  }
2451 
2452  factor = (double)pSRC->config.sampleRateIn / pSRC->config.sampleRateOut;
2453 
2454  totalFramesRead = 0;
2455  while (frameCount > 0) {
2456  drmp3_uint32 i;
2457  drmp3_uint32 framesToReadFromClient;
2458 
2459  /* The bin is where the previous and next frames are located. */
2460  float* pPrevFrame = pSRC->bin;
2461  float* pNextFrame = pSRC->bin + pSRC->config.channels;
2462 
2463  drmp3_blend_f32((float*)pFramesOut, pPrevFrame, pNextFrame, (float)pSRC->algo.linear.alpha, pSRC->config.channels);
2464 
2465  pSRC->algo.linear.alpha += factor;
2466 
2467  /* The new alpha value is how we determine whether or not we need to read fresh frames. */
2468  framesToReadFromClient = (drmp3_uint32)pSRC->algo.linear.alpha;
2469  pSRC->algo.linear.alpha = pSRC->algo.linear.alpha - framesToReadFromClient;
2470 
2471  for (i = 0; i < framesToReadFromClient; ++i) {
2472  drmp3_uint64 framesRead;
2473  drmp3_uint32 j;
2474 
2475  for (j = 0; j < pSRC->config.channels; ++j) {
2476  pPrevFrame[j] = pNextFrame[j];
2477  }
2478 
2479  framesRead = drmp3_src_cache_read_frames(&pSRC->cache, 1, pNextFrame);
2480  if (framesRead == 0) {
2481  drmp3_uint32 k;
2482  for (k = 0; k < pSRC->config.channels; ++k) {
2483  pNextFrame[k] = 0;
2484  }
2485 
2486  if (pSRC->algo.linear.isNextFramesLoaded) {
2487  pSRC->algo.linear.isNextFramesLoaded = DRMP3_FALSE;
2488  } else {
2489  if (flush) {
2490  pSRC->algo.linear.isPrevFramesLoaded = DRMP3_FALSE;
2491  }
2492  }
2493 
2494  break;
2495  }
2496  }
2497 
2498  pFramesOut = (drmp3_uint8*)pFramesOut + (1 * pSRC->config.channels * sizeof(float));
2499  frameCount -= 1;
2500  totalFramesRead += 1;
2501 
2502  /* If there's no frames available we need to get out of this loop. */
2503  if (!pSRC->algo.linear.isNextFramesLoaded && (!flush || !pSRC->algo.linear.isPrevFramesLoaded)) {
2504  break;
2505  }
2506  }
2507 
2508  return totalFramesRead;
2509 }
2510 
2511 
2512 static size_t drmp3__on_read(drmp3* pMP3, void* pBufferOut, size_t bytesToRead)
2513 {
2514  size_t bytesRead = pMP3->onRead(pMP3->pUserData, pBufferOut, bytesToRead);
2515  pMP3->streamCursor += bytesRead;
2516  return bytesRead;
2517 }
2518 
2519 static drmp3_bool32 drmp3__on_seek(drmp3* pMP3, int offset, drmp3_seek_origin origin)
2520 {
2521  drmp3_assert(offset >= 0);
2522 
2523  if (!pMP3->onSeek(pMP3->pUserData, offset, origin)) {
2524  return DRMP3_FALSE;
2525  }
2526 
2527  if (origin == drmp3_seek_origin_start) {
2528  pMP3->streamCursor = (drmp3_uint64)offset;
2529  } else {
2530  pMP3->streamCursor += offset;
2531  }
2532 
2533  return DRMP3_TRUE;
2534 }
2535 
2536 static drmp3_bool32 drmp3__on_seek_64(drmp3* pMP3, drmp3_uint64 offset, drmp3_seek_origin origin)
2537 {
2538  if (offset <= 0x7FFFFFFF) {
2539  return drmp3__on_seek(pMP3, (int)offset, origin);
2540  }
2541 
2542 
2543  /* Getting here "offset" is too large for a 32-bit integer. We just keep seeking forward until we hit the offset. */
2544  if (!drmp3__on_seek(pMP3, 0x7FFFFFFF, drmp3_seek_origin_start)) {
2545  return DRMP3_FALSE;
2546  }
2547 
2548  offset -= 0x7FFFFFFF;
2549  while (offset > 0) {
2550  if (offset <= 0x7FFFFFFF) {
2551  if (!drmp3__on_seek(pMP3, (int)offset, drmp3_seek_origin_current)) {
2552  return DRMP3_FALSE;
2553  }
2554  offset = 0;
2555  } else {
2556  if (!drmp3__on_seek(pMP3, 0x7FFFFFFF, drmp3_seek_origin_current)) {
2557  return DRMP3_FALSE;
2558  }
2559  offset -= 0x7FFFFFFF;
2560  }
2561  }
2562 
2563  return DRMP3_TRUE;
2564 }
2565 
2566 static drmp3_uint32 drmp3_decode_next_frame_ex(drmp3* pMP3, drmp3d_sample_t* pPCMFrames, drmp3_bool32 discard);
2567 static drmp3_uint32 drmp3_decode_next_frame(drmp3* pMP3);
2568 
2569 static drmp3_uint64 drmp3_read_src(drmp3_src* pSRC, drmp3_uint64 frameCount, void* pFramesOut, void* pUserData)
2570 {
2571  drmp3* pMP3 = (drmp3*)pUserData;
2572  float* pFramesOutF = (float*)pFramesOut;
2573  drmp3_uint64 totalFramesRead = 0;
2574 
2575  drmp3_assert(pMP3 != NULL);
2576  drmp3_assert(pMP3->onRead != NULL);
2577 
2578  while (frameCount > 0) {
2579  /* Read from the in-memory buffer first. */
2580  while (pMP3->pcmFramesRemainingInMP3Frame > 0 && frameCount > 0) {
2581  drmp3d_sample_t* frames = (drmp3d_sample_t*)pMP3->pcmFrames;
2582 #ifndef DR_MP3_FLOAT_OUTPUT
2583  if (pMP3->mp3FrameChannels == 1) {
2584  if (pMP3->channels == 1) {
2585  /* Mono -> Mono. */
2586  pFramesOutF[0] = frames[pMP3->pcmFramesConsumedInMP3Frame] / 32768.0f;
2587  } else {
2588  /* Mono -> Stereo. */
2589  pFramesOutF[0] = frames[pMP3->pcmFramesConsumedInMP3Frame] / 32768.0f;
2590  pFramesOutF[1] = frames[pMP3->pcmFramesConsumedInMP3Frame] / 32768.0f;
2591  }
2592  } else {
2593  if (pMP3->channels == 1) {
2594  /* Stereo -> Mono */
2595  float sample = 0;
2596  sample += frames[(pMP3->pcmFramesConsumedInMP3Frame*pMP3->mp3FrameChannels)+0] / 32768.0f;
2597  sample += frames[(pMP3->pcmFramesConsumedInMP3Frame*pMP3->mp3FrameChannels)+1] / 32768.0f;
2598  pFramesOutF[0] = sample * 0.5f;
2599  } else {
2600  /* Stereo -> Stereo */
2601  pFramesOutF[0] = frames[(pMP3->pcmFramesConsumedInMP3Frame*pMP3->mp3FrameChannels)+0] / 32768.0f;
2602  pFramesOutF[1] = frames[(pMP3->pcmFramesConsumedInMP3Frame*pMP3->mp3FrameChannels)+1] / 32768.0f;
2603  }
2604  }
2605 #else
2606  if (pMP3->mp3FrameChannels == 1) {
2607  if (pMP3->channels == 1) {
2608  /* Mono -> Mono. */
2609  pFramesOutF[0] = frames[pMP3->pcmFramesConsumedInMP3Frame];
2610  } else {
2611  /* Mono -> Stereo. */
2612  pFramesOutF[0] = frames[pMP3->pcmFramesConsumedInMP3Frame];
2613  pFramesOutF[1] = frames[pMP3->pcmFramesConsumedInMP3Frame];
2614  }
2615  } else {
2616  if (pMP3->channels == 1) {
2617  /* Stereo -> Mono */
2618  float sample = 0;
2619  sample += frames[(pMP3->pcmFramesConsumedInMP3Frame*pMP3->mp3FrameChannels)+0];
2620  sample += frames[(pMP3->pcmFramesConsumedInMP3Frame*pMP3->mp3FrameChannels)+1];
2621  pFramesOutF[0] = sample * 0.5f;
2622  } else {
2623  /* Stereo -> Stereo */
2624  pFramesOutF[0] = frames[(pMP3->pcmFramesConsumedInMP3Frame*pMP3->mp3FrameChannels)+0];
2625  pFramesOutF[1] = frames[(pMP3->pcmFramesConsumedInMP3Frame*pMP3->mp3FrameChannels)+1];
2626  }
2627  }
2628 #endif
2629 
2630  pMP3->pcmFramesConsumedInMP3Frame += 1;
2631  pMP3->pcmFramesRemainingInMP3Frame -= 1;
2632  totalFramesRead += 1;
2633  frameCount -= 1;
2634  pFramesOutF += pSRC->config.channels;
2635  }
2636 
2637  if (frameCount == 0) {
2638  break;
2639  }
2640 
2641  drmp3_assert(pMP3->pcmFramesRemainingInMP3Frame == 0);
2642 
2643  /*
2644  At this point we have exhausted our in-memory buffer so we need to re-fill. Note that the sample rate may have changed
2645  at this point which means we'll also need to update our sample rate conversion pipeline.
2646  */
2647  if (drmp3_decode_next_frame(pMP3) == 0) {
2648  break;
2649  }
2650  }
2651 
2652  return totalFramesRead;
2653 }
2654 
2655 static drmp3_bool32 drmp3_init_src(drmp3* pMP3)
2656 {
2657  drmp3_src_config srcConfig;
2658  drmp3_zero_object(&srcConfig);
2660  srcConfig.sampleRateOut = pMP3->sampleRate;
2661  srcConfig.channels = pMP3->channels;
2663  if (!drmp3_src_init(&srcConfig, drmp3_read_src, pMP3, &pMP3->src)) {
2664  drmp3_uninit(pMP3);
2665  return DRMP3_FALSE;
2666  }
2667 
2668  return DRMP3_TRUE;
2669 }
2670 
2671 static drmp3_uint32 drmp3_decode_next_frame_ex(drmp3* pMP3, drmp3d_sample_t* pPCMFrames, drmp3_bool32 discard)
2672 {
2673  drmp3_uint32 pcmFramesRead = 0;
2674 
2675  drmp3_assert(pMP3 != NULL);
2676  drmp3_assert(pMP3->onRead != NULL);
2677 
2678  if (pMP3->atEnd) {
2679  return 0;
2680  }
2681 
2682  do {
2683  drmp3dec_frame_info info;
2684  size_t leftoverDataSize;
2685 
2686  /* minimp3 recommends doing data submission in 16K chunks. If we don't have at least 16K bytes available, get more. */
2687  if (pMP3->dataSize < DRMP3_DATA_CHUNK_SIZE) {
2688  size_t bytesRead;
2689 
2690  if (pMP3->dataCapacity < DRMP3_DATA_CHUNK_SIZE) {
2691  drmp3_uint8* pNewData;
2692 
2693  pMP3->dataCapacity = DRMP3_DATA_CHUNK_SIZE;
2694  pNewData = (drmp3_uint8*)drmp3_realloc(pMP3->pData, pMP3->dataCapacity);
2695  if (pNewData == NULL) {
2696  return 0; /* Out of memory. */
2697  }
2698 
2699  pMP3->pData = pNewData;
2700  }
2701 
2702  bytesRead = drmp3__on_read(pMP3, pMP3->pData + pMP3->dataSize, (pMP3->dataCapacity - pMP3->dataSize));
2703  if (bytesRead == 0) {
2704  if (pMP3->dataSize == 0) {
2705  pMP3->atEnd = DRMP3_TRUE;
2706  return 0; /* No data. */
2707  }
2708  }
2709 
2710  pMP3->dataSize += bytesRead;
2711  }
2712 
2713  if (pMP3->dataSize > INT_MAX) {
2714  pMP3->atEnd = DRMP3_TRUE;
2715  return 0; /* File too big. */
2716  }
2717 
2718  pcmFramesRead = drmp3dec_decode_frame(&pMP3->decoder, pMP3->pData, (int)pMP3->dataSize, pPCMFrames, &info); /* <-- Safe size_t -> int conversion thanks to the check above. */
2719 
2720  /* Consume the data. */
2721  leftoverDataSize = (pMP3->dataSize - (size_t)info.frame_bytes);
2722  if (info.frame_bytes > 0) {
2723  memmove(pMP3->pData, pMP3->pData + info.frame_bytes, leftoverDataSize);
2724  pMP3->dataSize = leftoverDataSize;
2725  }
2726 
2727  /*
2728  pcmFramesRead will be equal to 0 if decoding failed. If it is zero and info.frame_bytes > 0 then we have successfully
2729  decoded the frame. A special case is if we are wanting to discard the frame, in which case we return successfully.
2730  */
2731  if (pcmFramesRead > 0 || (info.frame_bytes > 0 && discard)) {
2732  pcmFramesRead = drmp3_hdr_frame_samples(pMP3->decoder.header);
2733  pMP3->pcmFramesConsumedInMP3Frame = 0;
2734  pMP3->pcmFramesRemainingInMP3Frame = pcmFramesRead;
2735  pMP3->mp3FrameChannels = info.channels;
2736  pMP3->mp3FrameSampleRate = info.hz;
2737 
2738  /* We need to initialize the resampler if we don't yet have the channel count or sample rate. */
2739  if (pMP3->channels == 0 || pMP3->sampleRate == 0) {
2740  if (pMP3->channels == 0) {
2741  pMP3->channels = info.channels;
2742  }
2743  if (pMP3->sampleRate == 0) {
2744  pMP3->sampleRate = info.hz;
2745  }
2746  drmp3_init_src(pMP3);
2747  }
2748 
2749  drmp3_src_set_input_sample_rate(&pMP3->src, pMP3->mp3FrameSampleRate);
2750  break;
2751  } else if (info.frame_bytes == 0) {
2752  size_t bytesRead;
2753 
2754  /* Need more data. minimp3 recommends doing data submission in 16K chunks. */
2755  if (pMP3->dataCapacity == pMP3->dataSize) {
2756  drmp3_uint8* pNewData;
2757 
2758  /* No room. Expand. */
2759  pMP3->dataCapacity += DRMP3_DATA_CHUNK_SIZE;
2760  pNewData = (drmp3_uint8*)drmp3_realloc(pMP3->pData, pMP3->dataCapacity);
2761  if (pNewData == NULL) {
2762  return 0; /* Out of memory. */
2763  }
2764 
2765  pMP3->pData = pNewData;
2766  }
2767 
2768  /* Fill in a chunk. */
2769  bytesRead = drmp3__on_read(pMP3, pMP3->pData + pMP3->dataSize, (pMP3->dataCapacity - pMP3->dataSize));
2770  if (bytesRead == 0) {
2771  pMP3->atEnd = DRMP3_TRUE;
2772  return 0; /* Error reading more data. */
2773  }
2774 
2775  pMP3->dataSize += bytesRead;
2776  }
2777  } while (DRMP3_TRUE);
2778 
2779  return pcmFramesRead;
2780 }
2781 
2782 static drmp3_uint32 drmp3_decode_next_frame(drmp3* pMP3)
2783 {
2784  drmp3_assert(pMP3 != NULL);
2785  return drmp3_decode_next_frame_ex(pMP3, (drmp3d_sample_t*)pMP3->pcmFrames, DRMP3_FALSE);
2786 }
2787 
2788 #if 0
2789 static drmp3_uint32 drmp3_seek_next_frame(drmp3* pMP3)
2790 {
2791  drmp3_uint32 pcmFrameCount;
2792 
2793  drmp3_assert(pMP3 != NULL);
2794 
2795  pcmFrameCount = drmp3_decode_next_frame_ex(pMP3, NULL);
2796  if (pcmFrameCount == 0) {
2797  return 0;
2798  }
2799 
2800  /* We have essentially just skipped past the frame, so just set the remaining samples to 0. */
2801  pMP3->currentPCMFrame += pcmFrameCount;
2802  pMP3->pcmFramesConsumedInMP3Frame = pcmFrameCount;
2803  pMP3->pcmFramesRemainingInMP3Frame = 0;
2804 
2805  return pcmFrameCount;
2806 }
2807 #endif
2808 
2809 drmp3_bool32 drmp3_init_internal(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, const drmp3_config* pConfig)
2810 {
2811  drmp3_config config;
2812 
2813  drmp3_assert(pMP3 != NULL);
2814  drmp3_assert(onRead != NULL);
2815 
2816  /* This function assumes the output object has already been reset to 0. Do not do that here, otherwise things will break. */
2817  drmp3dec_init(&pMP3->decoder);
2818 
2819  /* The config can be null in which case we use defaults. */
2820  if (pConfig != NULL) {
2821  config = *pConfig;
2822  } else {
2823  drmp3_zero_object(&config);
2824  }
2825 
2826  pMP3->channels = config.outputChannels;
2827 
2828  /* Cannot have more than 2 channels. */
2829  if (pMP3->channels > 2) {
2830  pMP3->channels = 2;
2831  }
2832 
2833  pMP3->sampleRate = config.outputSampleRate;
2834 
2835  pMP3->onRead = onRead;
2836  pMP3->onSeek = onSeek;
2837  pMP3->pUserData = pUserData;
2838 
2839  /*
2840  We need a sample rate converter for converting the sample rate from the MP3 frames to the requested output sample rate. Note that if
2841  we don't yet know the channel count or sample rate we defer this until the first frame is read.
2842  */
2843  if (pMP3->channels != 0 && pMP3->sampleRate != 0) {
2844  drmp3_init_src(pMP3);
2845  }
2846 
2847  /* Decode the first frame to confirm that it is indeed a valid MP3 stream. */
2848  if (!drmp3_decode_next_frame(pMP3)) {
2849  drmp3_uninit(pMP3);
2850  return DRMP3_FALSE; /* Not a valid MP3 stream. */
2851  }
2852 
2853  return DRMP3_TRUE;
2854 }
2855 
2856 drmp3_bool32 drmp3_init(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, const drmp3_config* pConfig)
2857 {
2858  if (pMP3 == NULL || onRead == NULL) {
2859  return DRMP3_FALSE;
2860  }
2861 
2862  drmp3_zero_object(pMP3);
2863  return drmp3_init_internal(pMP3, onRead, onSeek, pUserData, pConfig);
2864 }
2865 
2866 
2867 static size_t drmp3__on_read_memory(void* pUserData, void* pBufferOut, size_t bytesToRead)
2868 {
2869  drmp3* pMP3 = (drmp3*)pUserData;
2870  size_t bytesRemaining;
2871 
2872  drmp3_assert(pMP3 != NULL);
2873  drmp3_assert(pMP3->memory.dataSize >= pMP3->memory.currentReadPos);
2874 
2875  bytesRemaining = pMP3->memory.dataSize - pMP3->memory.currentReadPos;
2876  if (bytesToRead > bytesRemaining) {
2877  bytesToRead = bytesRemaining;
2878  }
2879 
2880  if (bytesToRead > 0) {
2881  drmp3_copy_memory(pBufferOut, pMP3->memory.pData + pMP3->memory.currentReadPos, bytesToRead);
2882  pMP3->memory.currentReadPos += bytesToRead;
2883  }
2884 
2885  return bytesToRead;
2886 }
2887 
2888 static drmp3_bool32 drmp3__on_seek_memory(void* pUserData, int byteOffset, drmp3_seek_origin origin)
2889 {
2890  drmp3* pMP3 = (drmp3*)pUserData;
2891 
2892  drmp3_assert(pMP3 != NULL);
2893 
2894  if (origin == drmp3_seek_origin_current) {
2895  if (byteOffset > 0) {
2896  if (pMP3->memory.currentReadPos + byteOffset > pMP3->memory.dataSize) {
2897  byteOffset = (int)(pMP3->memory.dataSize - pMP3->memory.currentReadPos); /* Trying to seek too far forward. */
2898  }
2899  } else {
2900  if (pMP3->memory.currentReadPos < (size_t)-byteOffset) {
2901  byteOffset = -(int)pMP3->memory.currentReadPos; /* Trying to seek too far backwards. */
2902  }
2903  }
2904 
2905  /* This will never underflow thanks to the clamps above. */
2906  pMP3->memory.currentReadPos += byteOffset;
2907  } else {
2908  if ((drmp3_uint32)byteOffset <= pMP3->memory.dataSize) {
2909  pMP3->memory.currentReadPos = byteOffset;
2910  } else {
2911  pMP3->memory.currentReadPos = pMP3->memory.dataSize; /* Trying to seek too far forward. */
2912  }
2913  }
2914 
2915  return DRMP3_TRUE;
2916 }
2917 
2918 drmp3_bool32 drmp3_init_memory(drmp3* pMP3, const void* pData, size_t dataSize, const drmp3_config* pConfig)
2919 {
2920  if (pMP3 == NULL) {
2921  return DRMP3_FALSE;
2922  }
2923 
2924  drmp3_zero_object(pMP3);
2925 
2926  if (pData == NULL || dataSize == 0) {
2927  return DRMP3_FALSE;
2928  }
2929 
2930  pMP3->memory.pData = (const drmp3_uint8*)pData;
2931  pMP3->memory.dataSize = dataSize;
2932  pMP3->memory.currentReadPos = 0;
2933 
2934  return drmp3_init_internal(pMP3, drmp3__on_read_memory, drmp3__on_seek_memory, pMP3, pConfig);
2935 }
2936 
2937 
2938 #ifndef DR_MP3_NO_STDIO
2939 #include <stdio.h>
2940 
2941 static size_t drmp3__on_read_stdio(void* pUserData, void* pBufferOut, size_t bytesToRead)
2942 {
2943  return fread(pBufferOut, 1, bytesToRead, (FILE*)pUserData);
2944 }
2945 
2946 static drmp3_bool32 drmp3__on_seek_stdio(void* pUserData, int offset, drmp3_seek_origin origin)
2947 {
2948  return fseek((FILE*)pUserData, offset, (origin == drmp3_seek_origin_current) ? SEEK_CUR : SEEK_SET) == 0;
2949 }
2950 
2951 drmp3_bool32 drmp3_init_file(drmp3* pMP3, const char* filePath, const drmp3_config* pConfig)
2952 {
2953  FILE* pFile;
2954 #if defined(_MSC_VER) && _MSC_VER >= 1400
2955  if (fopen_s(&pFile, filePath, "rb") != 0) {
2956  return DRMP3_FALSE;
2957  }
2958 #else
2959  pFile = fopen(filePath, "rb");
2960  if (pFile == NULL) {
2961  return DRMP3_FALSE;
2962  }
2963 #endif
2964 
2965  return drmp3_init(pMP3, drmp3__on_read_stdio, drmp3__on_seek_stdio, (void*)pFile, pConfig);
2966 }
2967 #endif
2968 
2969 void drmp3_uninit(drmp3* pMP3)
2970 {
2971  if (pMP3 == NULL) {
2972  return;
2973  }
2974 
2975 #ifndef DR_MP3_NO_STDIO
2976  if (pMP3->onRead == drmp3__on_read_stdio) {
2977  fclose((FILE*)pMP3->pUserData);
2978  }
2979 #endif
2980 
2981  drmp3_free(pMP3->pData);
2982 }
2983 
2984 drmp3_uint64 drmp3_read_pcm_frames_f32(drmp3* pMP3, drmp3_uint64 framesToRead, float* pBufferOut)
2985 {
2986  drmp3_uint64 totalFramesRead = 0;
2987 
2988  if (pMP3 == NULL || pMP3->onRead == NULL) {
2989  return 0;
2990  }
2991 
2992  if (pBufferOut == NULL) {
2993  float temp[4096];
2994  while (framesToRead > 0) {
2995  drmp3_uint64 framesJustRead;
2996  drmp3_uint64 framesToReadRightNow = sizeof(temp)/sizeof(temp[0]) / pMP3->channels;
2997  if (framesToReadRightNow > framesToRead) {
2998  framesToReadRightNow = framesToRead;
2999  }
3000 
3001  framesJustRead = drmp3_read_pcm_frames_f32(pMP3, framesToReadRightNow, temp);
3002  if (framesJustRead == 0) {
3003  break;
3004  }
3005 
3006  framesToRead -= framesJustRead;
3007  totalFramesRead += framesJustRead;
3008  }
3009  } else {
3010  totalFramesRead = drmp3_src_read_frames_ex(&pMP3->src, framesToRead, pBufferOut, DRMP3_TRUE);
3011  pMP3->currentPCMFrame += totalFramesRead;
3012  }
3013 
3014  return totalFramesRead;
3015 }
3016 
3017 drmp3_uint64 drmp3_read_pcm_frames_s16(drmp3* pMP3, drmp3_uint64 framesToRead, drmp3_int16* pBufferOut)
3018 {
3019  float tempF32[4096];
3020  drmp3_uint64 pcmFramesJustRead;
3021  drmp3_uint64 totalPCMFramesRead = 0;
3022 
3023  if (pMP3 == NULL || pMP3->onRead == NULL) {
3024  return 0;
3025  }
3026 
3027  /* Naive implementation: read into a temp f32 buffer, then convert. */
3028  for (;;) {
3029  drmp3_uint64 pcmFramesToReadThisIteration = (framesToRead - totalPCMFramesRead);
3030  if (pcmFramesToReadThisIteration > drmp3_countof(tempF32)/pMP3->channels) {
3031  pcmFramesToReadThisIteration = drmp3_countof(tempF32)/pMP3->channels;
3032  }
3033 
3034  pcmFramesJustRead = drmp3_read_pcm_frames_f32(pMP3, pcmFramesToReadThisIteration, tempF32);
3035  if (pcmFramesJustRead == 0) {
3036  break;
3037  }
3038 
3039  drmp3dec_f32_to_s16(tempF32, pBufferOut, (int)(pcmFramesJustRead * pMP3->channels)); /* <-- Safe cast since pcmFramesJustRead will be clamped based on the size of tempF32 which is always small. */
3040  pBufferOut += pcmFramesJustRead * pMP3->channels;
3041 
3042  totalPCMFramesRead += pcmFramesJustRead;
3043 
3044  if (pcmFramesJustRead < pcmFramesToReadThisIteration) {
3045  break;
3046  }
3047  }
3048 
3049  return totalPCMFramesRead;
3050 }
3051 
3052 void drmp3_reset(drmp3* pMP3)
3053 {
3054  drmp3_assert(pMP3 != NULL);
3055 
3056  pMP3->pcmFramesConsumedInMP3Frame = 0;
3057  pMP3->pcmFramesRemainingInMP3Frame = 0;
3058  pMP3->currentPCMFrame = 0;
3059  pMP3->dataSize = 0;
3060  pMP3->atEnd = DRMP3_FALSE;
3061  pMP3->src.bin[0] = 0;
3062  pMP3->src.bin[1] = 0;
3063  pMP3->src.bin[2] = 0;
3064  pMP3->src.bin[3] = 0;
3065  pMP3->src.cache.cachedFrameCount = 0;
3066  pMP3->src.cache.iNextFrame = 0;
3067  pMP3->src.algo.linear.alpha = 0;
3068  pMP3->src.algo.linear.isNextFramesLoaded = 0;
3069  pMP3->src.algo.linear.isPrevFramesLoaded = 0;
3070  drmp3dec_init(&pMP3->decoder);
3071 }
3072 
3073 drmp3_bool32 drmp3_seek_to_start_of_stream(drmp3* pMP3)
3074 {
3075  drmp3_assert(pMP3 != NULL);
3076  drmp3_assert(pMP3->onSeek != NULL);
3077 
3078  /* Seek to the start of the stream to begin with. */
3079  if (!drmp3__on_seek(pMP3, 0, drmp3_seek_origin_start)) {
3080  return DRMP3_FALSE;
3081  }
3082 
3083  /* Clear any cached data. */
3084  drmp3_reset(pMP3);
3085  return DRMP3_TRUE;
3086 }
3087 
3088 float drmp3_get_cached_pcm_frame_count_from_src(drmp3* pMP3)
3089 {
3090  return (pMP3->src.cache.cachedFrameCount - pMP3->src.cache.iNextFrame) + (float)pMP3->src.algo.linear.alpha;
3091 }
3092 
3093 float drmp3_get_pcm_frames_remaining_in_mp3_frame(drmp3* pMP3)
3094 {
3095  float factor = (float)pMP3->src.config.sampleRateOut / (float)pMP3->src.config.sampleRateIn;
3096  float frameCountPreSRC = drmp3_get_cached_pcm_frame_count_from_src(pMP3) + pMP3->pcmFramesRemainingInMP3Frame;
3097  return frameCountPreSRC * factor;
3098 }
3099 
3100 /*
3101 NOTE ON SEEKING
3102 ===============
3103 The seeking code below is a complete mess and is broken for cases when the sample rate changes. The problem
3104 is with the resampling and the crappy resampler used by dr_mp3. What needs to happen is the following:
3105 
3106 1) The resampler needs to be replaced.
3107 2) The resampler has state which needs to be updated whenever an MP3 frame is decoded outside of
3108  drmp3_read_pcm_frames_f32(). The resampler needs an API to "flush" some imaginary input so that it's
3109  state is updated accordingly.
3110 */
3111 drmp3_bool32 drmp3_seek_forward_by_pcm_frames__brute_force(drmp3* pMP3, drmp3_uint64 frameOffset)
3112 {
3113  drmp3_uint64 framesRead;
3114 
3115 #if 0
3116  /*
3117  MP3 is a bit annoying when it comes to seeking because of the bit reservoir. It basically means that an MP3 frame can possibly
3118  depend on some of the data of prior frames. This means it's not as simple as seeking to the first byte of the MP3 frame that
3119  contains the sample because that MP3 frame will need the data from the previous MP3 frame (which we just seeked past!). To
3120  resolve this we seek past a number of MP3 frames up to a point, and then read-and-discard the remainder.
3121  */
3122  drmp3_uint64 maxFramesToReadAndDiscard = (drmp3_uint64)(DRMP3_MAX_PCM_FRAMES_PER_MP3_FRAME * 3 * ((float)pMP3->src.config.sampleRateOut / (float)pMP3->src.config.sampleRateIn));
3123 
3124  /* Now get rid of leading whole frames. */
3125  while (frameOffset > maxFramesToReadAndDiscard) {
3126  float pcmFramesRemainingInCurrentMP3FrameF = drmp3_get_pcm_frames_remaining_in_mp3_frame(pMP3);
3127  drmp3_uint32 pcmFramesRemainingInCurrentMP3Frame = (drmp3_uint32)pcmFramesRemainingInCurrentMP3FrameF;
3128  if (frameOffset > pcmFramesRemainingInCurrentMP3Frame) {
3129  frameOffset -= pcmFramesRemainingInCurrentMP3Frame;
3130  pMP3->currentPCMFrame += pcmFramesRemainingInCurrentMP3Frame;
3132  pMP3->pcmFramesRemainingInMP3Frame = 0;
3133  } else {
3134  break;
3135  }
3136 
3137  drmp3_uint32 pcmFrameCount = drmp3_decode_next_frame_ex(pMP3, pMP3->pcmFrames, DRMP3_FALSE);
3138  if (pcmFrameCount == 0) {
3139  break;
3140  }
3141  }
3142 
3143  /* The last step is to read-and-discard any remaining PCM frames to make it sample-exact. */
3144  framesRead = drmp3_read_pcm_frames_f32(pMP3, frameOffset, NULL);
3145  if (framesRead != frameOffset) {
3146  return DRMP3_FALSE;
3147  }
3148 #else
3149  /* Just using a dumb read-and-discard for now pending updates to the resampler. */
3150  framesRead = drmp3_read_pcm_frames_f32(pMP3, frameOffset, NULL);
3151  if (framesRead != frameOffset) {
3152  return DRMP3_FALSE;
3153  }
3154 #endif
3155 
3156  return DRMP3_TRUE;
3157 }
3158 
3159 drmp3_bool32 drmp3_seek_to_pcm_frame__brute_force(drmp3* pMP3, drmp3_uint64 frameIndex)
3160 {
3161  drmp3_assert(pMP3 != NULL);
3162 
3163  if (frameIndex == pMP3->currentPCMFrame) {
3164  return DRMP3_TRUE;
3165  }
3166 
3167  /*
3168  If we're moving foward we just read from where we're at. Otherwise we need to move back to the start of
3169  the stream and read from the beginning.
3170  */
3171  if (frameIndex < pMP3->currentPCMFrame) {
3172  /* Moving backward. Move to the start of the stream and then move forward. */
3173  if (!drmp3_seek_to_start_of_stream(pMP3)) {
3174  return DRMP3_FALSE;
3175  }
3176  }
3177 
3178  drmp3_assert(frameIndex >= pMP3->currentPCMFrame);
3179  return drmp3_seek_forward_by_pcm_frames__brute_force(pMP3, (frameIndex - pMP3->currentPCMFrame));
3180 }
3181 
3182 drmp3_bool32 drmp3_find_closest_seek_point(drmp3* pMP3, drmp3_uint64 frameIndex, drmp3_uint32* pSeekPointIndex)
3183 {
3184  drmp3_uint32 iSeekPoint;
3185 
3186  drmp3_assert(pSeekPointIndex != NULL);
3187 
3188  *pSeekPointIndex = 0;
3189 
3190  if (frameIndex < pMP3->pSeekPoints[0].pcmFrameIndex) {
3191  return DRMP3_FALSE;
3192  }
3193 
3194  /* 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. */
3195  for (iSeekPoint = 0; iSeekPoint < pMP3->seekPointCount; ++iSeekPoint) {
3196  if (pMP3->pSeekPoints[iSeekPoint].pcmFrameIndex > frameIndex) {
3197  break; /* Found it. */
3198  }
3199 
3200  *pSeekPointIndex = iSeekPoint;
3201  }
3202 
3203  return DRMP3_TRUE;
3204 }
3205 
3206 drmp3_bool32 drmp3_seek_to_pcm_frame__seek_table(drmp3* pMP3, drmp3_uint64 frameIndex)
3207 {
3208  drmp3_seek_point seekPoint;
3209  drmp3_uint32 priorSeekPointIndex;
3210  drmp3_uint16 iMP3Frame;
3211  drmp3_uint64 leftoverFrames;
3212 
3213  drmp3_assert(pMP3 != NULL);
3214  drmp3_assert(pMP3->pSeekPoints != NULL);
3215  drmp3_assert(pMP3->seekPointCount > 0);
3216 
3217  /* 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. */
3218  if (drmp3_find_closest_seek_point(pMP3, frameIndex, &priorSeekPointIndex)) {
3219  seekPoint = pMP3->pSeekPoints[priorSeekPointIndex];
3220  } else {
3221  seekPoint.seekPosInBytes = 0;
3222  seekPoint.pcmFrameIndex = 0;
3223  seekPoint.mp3FramesToDiscard = 0;
3224  seekPoint.pcmFramesToDiscard = 0;
3225  }
3226 
3227  /* First thing to do is seek to the first byte of the relevant MP3 frame. */
3228  if (!drmp3__on_seek_64(pMP3, seekPoint.seekPosInBytes, drmp3_seek_origin_start)) {
3229  return DRMP3_FALSE; /* Failed to seek. */
3230  }
3231 
3232  /* Clear any cached data. */
3233  drmp3_reset(pMP3);
3234 
3235  /* Whole MP3 frames need to be discarded first. */
3236  for (iMP3Frame = 0; iMP3Frame < seekPoint.mp3FramesToDiscard; ++iMP3Frame) {
3237  drmp3_uint32 pcmFramesReadPreSRC;
3238  drmp3d_sample_t* pPCMFrames;
3239 
3240  /* Pass in non-null for the last frame because we want to ensure the sample rate converter is preloaded correctly. */
3241  pPCMFrames = NULL;
3242  if (iMP3Frame == seekPoint.mp3FramesToDiscard-1) {
3243  pPCMFrames = (drmp3d_sample_t*)pMP3->pcmFrames;
3244  }
3245 
3246  /* We first need to decode the next frame, and then we need to flush the resampler. */
3247  pcmFramesReadPreSRC = drmp3_decode_next_frame_ex(pMP3, pPCMFrames, DRMP3_TRUE);
3248  if (pcmFramesReadPreSRC == 0) {
3249  return DRMP3_FALSE;
3250  }
3251  }
3252 
3253  /* We seeked to an MP3 frame in the raw stream so we need to make sure the current PCM frame is set correctly. */
3254  pMP3->currentPCMFrame = seekPoint.pcmFrameIndex - seekPoint.pcmFramesToDiscard;
3255 
3256  /*
3257  Update resampler. This is wrong. Need to instead update it on a per MP3 frame basis. Also broken for cases when
3258  the sample rate is being reduced in my testing. Should work fine when the input and output sample rate is the same
3259  or a clean multiple.
3260  */
3261  pMP3->src.algo.linear.alpha = (drmp3_int64)pMP3->currentPCMFrame * ((double)pMP3->src.config.sampleRateIn / pMP3->src.config.sampleRateOut); /* <-- Cast to int64 is required for VC6. */
3262  pMP3->src.algo.linear.alpha = pMP3->src.algo.linear.alpha - (drmp3_uint32)(pMP3->src.algo.linear.alpha);
3263  if (pMP3->src.algo.linear.alpha > 0) {
3264  pMP3->src.algo.linear.isPrevFramesLoaded = 1;
3265  }
3266 
3267  /*
3268  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
3269  read-and-discard at least 2 whole MP3 frames.
3270  */
3271  leftoverFrames = frameIndex - pMP3->currentPCMFrame;
3272  return drmp3_seek_forward_by_pcm_frames__brute_force(pMP3, leftoverFrames);
3273 }
3274 
3276 {
3277  if (pMP3 == NULL || pMP3->onSeek == NULL) {
3278  return DRMP3_FALSE;
3279  }
3280 
3281  if (frameIndex == 0) {
3282  return drmp3_seek_to_start_of_stream(pMP3);
3283  }
3284 
3285  /* Use the seek table if we have one. */
3286  if (pMP3->pSeekPoints != NULL && pMP3->seekPointCount > 0) {
3287  return drmp3_seek_to_pcm_frame__seek_table(pMP3, frameIndex);
3288  } else {
3289  return drmp3_seek_to_pcm_frame__brute_force(pMP3, frameIndex);
3290  }
3291 }
3292 
3293 drmp3_bool32 drmp3_get_mp3_and_pcm_frame_count(drmp3* pMP3, drmp3_uint64* pMP3FrameCount, drmp3_uint64* pPCMFrameCount)
3294 {
3295  drmp3_uint64 currentPCMFrame;
3296  drmp3_uint64 totalPCMFrameCount;
3297  drmp3_uint64 totalMP3FrameCount;
3298  float totalPCMFrameCountFractionalPart;
3299 
3300  if (pMP3 == NULL) {
3301  return DRMP3_FALSE;
3302  }
3303 
3304  /*
3305  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
3306  on our output sample rate, the seek back to the PCM frame we were sitting on before calling this function.
3307  */
3308 
3309  /* The stream must support seeking for this to work. */
3310  if (pMP3->onSeek == NULL) {
3311  return DRMP3_FALSE;
3312  }
3313 
3314  /* 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. */
3315  currentPCMFrame = pMP3->currentPCMFrame;
3316 
3317  if (!drmp3_seek_to_start_of_stream(pMP3)) {
3318  return DRMP3_FALSE;
3319  }
3320 
3321  totalPCMFrameCount = 0;
3322  totalMP3FrameCount = 0;
3323 
3324  totalPCMFrameCountFractionalPart = 0; /* <-- With resampling there will be a fractional part to each MP3 frame that we need to accumulate. */
3325  for (;;) {
3326  drmp3_uint32 pcmFramesInCurrentMP3FrameIn;
3327  float srcRatio;
3328  float pcmFramesInCurrentMP3FrameOutF;
3329  drmp3_uint32 pcmFramesInCurrentMP3FrameOut;
3330 
3331  pcmFramesInCurrentMP3FrameIn = drmp3_decode_next_frame_ex(pMP3, NULL, DRMP3_FALSE);
3332  if (pcmFramesInCurrentMP3FrameIn == 0) {
3333  break;
3334  }
3335 
3336  srcRatio = (float)pMP3->mp3FrameSampleRate / (float)pMP3->sampleRate;
3337  drmp3_assert(srcRatio > 0);
3338 
3339  pcmFramesInCurrentMP3FrameOutF = totalPCMFrameCountFractionalPart + (pcmFramesInCurrentMP3FrameIn / srcRatio);
3340  pcmFramesInCurrentMP3FrameOut = (drmp3_uint32)pcmFramesInCurrentMP3FrameOutF;
3341  totalPCMFrameCountFractionalPart = pcmFramesInCurrentMP3FrameOutF - pcmFramesInCurrentMP3FrameOut;
3342  totalPCMFrameCount += pcmFramesInCurrentMP3FrameOut;
3343  totalMP3FrameCount += 1;
3344  }
3345 
3346  /* Finally, we need to seek back to where we were. */
3347  if (!drmp3_seek_to_start_of_stream(pMP3)) {
3348  return DRMP3_FALSE;
3349  }
3350 
3351  if (!drmp3_seek_to_pcm_frame(pMP3, currentPCMFrame)) {
3352  return DRMP3_FALSE;
3353  }
3354 
3355  if (pMP3FrameCount != NULL) {
3356  *pMP3FrameCount = totalMP3FrameCount;
3357  }
3358  if (pPCMFrameCount != NULL) {
3359  *pPCMFrameCount = totalPCMFrameCount;
3360  }
3361 
3362  return DRMP3_TRUE;
3363 }
3364 
3366 {
3367  drmp3_uint64 totalPCMFrameCount;
3368  if (!drmp3_get_mp3_and_pcm_frame_count(pMP3, NULL, &totalPCMFrameCount)) {
3369  return 0;
3370  }
3371 
3372  return totalPCMFrameCount;
3373 }
3374 
3376 {
3377  drmp3_uint64 totalMP3FrameCount;
3378  if (!drmp3_get_mp3_and_pcm_frame_count(pMP3, &totalMP3FrameCount, NULL)) {
3379  return 0;
3380  }
3381 
3382  return totalMP3FrameCount;
3383 }
3384 
3385 void drmp3__accumulate_running_pcm_frame_count(drmp3* pMP3, drmp3_uint32 pcmFrameCountIn, drmp3_uint64* pRunningPCMFrameCount, float* pRunningPCMFrameCountFractionalPart)
3386 {
3387  float srcRatio;
3388  float pcmFrameCountOutF;
3389  drmp3_uint32 pcmFrameCountOut;
3390 
3391  srcRatio = (float)pMP3->mp3FrameSampleRate / (float)pMP3->sampleRate;
3392  drmp3_assert(srcRatio > 0);
3393 
3394  pcmFrameCountOutF = *pRunningPCMFrameCountFractionalPart + (pcmFrameCountIn / srcRatio);
3395  pcmFrameCountOut = (drmp3_uint32)pcmFrameCountOutF;
3396  *pRunningPCMFrameCountFractionalPart = pcmFrameCountOutF - pcmFrameCountOut;
3397  *pRunningPCMFrameCount += pcmFrameCountOut;
3398 }
3399 
3400 typedef struct
3401 {
3402  drmp3_uint64 bytePos;
3403  drmp3_uint64 pcmFrameIndex; /* <-- After sample rate conversion. */
3404 } drmp3__seeking_mp3_frame_info;
3405 
3406 drmp3_bool32 drmp3_calculate_seek_points(drmp3* pMP3, drmp3_uint32* pSeekPointCount, drmp3_seek_point* pSeekPoints)
3407 {
3408  drmp3_uint32 seekPointCount;
3409  drmp3_uint64 currentPCMFrame;
3410  drmp3_uint64 totalMP3FrameCount;
3411  drmp3_uint64 totalPCMFrameCount;
3412 
3413  if (pMP3 == NULL || pSeekPointCount == NULL || pSeekPoints == NULL) {
3414  return DRMP3_FALSE; /* Invalid args. */
3415  }
3416 
3417  seekPointCount = *pSeekPointCount;
3418  if (seekPointCount == 0) {
3419  return DRMP3_FALSE; /* The client has requested no seek points. Consider this to be invalid arguments since the client has probably not intended this. */
3420  }
3421 
3422  /* 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. */
3423  currentPCMFrame = pMP3->currentPCMFrame;
3424 
3425  /* We never do more than the total number of MP3 frames and we limit it to 32-bits. */
3426  if (!drmp3_get_mp3_and_pcm_frame_count(pMP3, &totalMP3FrameCount, &totalPCMFrameCount)) {
3427  return DRMP3_FALSE;
3428  }
3429 
3430  /* 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. */
3431  if (totalMP3FrameCount < DRMP3_SEEK_LEADING_MP3_FRAMES+1) {
3432  seekPointCount = 1;
3433  pSeekPoints[0].seekPosInBytes = 0;
3434  pSeekPoints[0].pcmFrameIndex = 0;
3435  pSeekPoints[0].mp3FramesToDiscard = 0;
3436  pSeekPoints[0].pcmFramesToDiscard = 0;
3437  } else {
3438  drmp3_uint64 pcmFramesBetweenSeekPoints;
3439  drmp3__seeking_mp3_frame_info mp3FrameInfo[DRMP3_SEEK_LEADING_MP3_FRAMES+1];
3440  drmp3_uint64 runningPCMFrameCount = 0;
3441  float runningPCMFrameCountFractionalPart = 0;
3442  drmp3_uint64 nextTargetPCMFrame;
3443  drmp3_uint32 iMP3Frame;
3444  drmp3_uint32 iSeekPoint;
3445 
3446  if (seekPointCount > totalMP3FrameCount-1) {
3447  seekPointCount = (drmp3_uint32)totalMP3FrameCount-1;
3448  }
3449 
3450  pcmFramesBetweenSeekPoints = totalPCMFrameCount / (seekPointCount+1);
3451 
3452  /*
3453  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
3454  MP3 frame.
3455  */
3456  if (!drmp3_seek_to_start_of_stream(pMP3)) {
3457  return DRMP3_FALSE;
3458  }
3459 
3460  /*
3461  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
3462  array. The value in the first item in this array is the byte position that will be reported in the next seek point.
3463  */
3464 
3465  /* We need to initialize the array of MP3 byte positions for the leading MP3 frames. */
3466  for (iMP3Frame = 0; iMP3Frame < DRMP3_SEEK_LEADING_MP3_FRAMES+1; ++iMP3Frame) {
3467  drmp3_uint32 pcmFramesInCurrentMP3FrameIn;
3468 
3469  /* The byte position of the next frame will be the stream's cursor position, minus whatever is sitting in the buffer. */
3470  drmp3_assert(pMP3->streamCursor >= pMP3->dataSize);
3471  mp3FrameInfo[iMP3Frame].bytePos = pMP3->streamCursor - pMP3->dataSize;
3472  mp3FrameInfo[iMP3Frame].pcmFrameIndex = runningPCMFrameCount;
3473 
3474  /* We need to get information about this frame so we can know how many samples it contained. */
3475  pcmFramesInCurrentMP3FrameIn = drmp3_decode_next_frame_ex(pMP3, NULL, DRMP3_FALSE);
3476  if (pcmFramesInCurrentMP3FrameIn == 0) {
3477  return DRMP3_FALSE; /* This should never happen. */
3478  }
3479 
3480  drmp3__accumulate_running_pcm_frame_count(pMP3, pcmFramesInCurrentMP3FrameIn, &runningPCMFrameCount, &runningPCMFrameCountFractionalPart);
3481  }
3482 
3483  /*
3484  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
3485  calculate them.
3486  */
3487  nextTargetPCMFrame = 0;
3488  for (iSeekPoint = 0; iSeekPoint < seekPointCount; ++iSeekPoint) {
3489  nextTargetPCMFrame += pcmFramesBetweenSeekPoints;
3490 
3491  for (;;) {
3492  if (nextTargetPCMFrame < runningPCMFrameCount) {
3493  /* The next seek point is in the current MP3 frame. */
3494  pSeekPoints[iSeekPoint].seekPosInBytes = mp3FrameInfo[0].bytePos;
3495  pSeekPoints[iSeekPoint].pcmFrameIndex = nextTargetPCMFrame;
3496  pSeekPoints[iSeekPoint].mp3FramesToDiscard = DRMP3_SEEK_LEADING_MP3_FRAMES;
3497  pSeekPoints[iSeekPoint].pcmFramesToDiscard = (drmp3_uint16)(nextTargetPCMFrame - mp3FrameInfo[DRMP3_SEEK_LEADING_MP3_FRAMES-1].pcmFrameIndex);
3498  break;
3499  } else {
3500  size_t i;
3501  drmp3_uint32 pcmFramesInCurrentMP3FrameIn;
3502 
3503  /*
3504  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
3505  MP3 frame info.
3506  */
3507  for (i = 0; i < drmp3_countof(mp3FrameInfo)-1; ++i) {
3508  mp3FrameInfo[i] = mp3FrameInfo[i+1];
3509  }
3510 
3511  /* Cache previous MP3 frame info. */
3512  mp3FrameInfo[drmp3_countof(mp3FrameInfo)-1].bytePos = pMP3->streamCursor - pMP3->dataSize;
3513  mp3FrameInfo[drmp3_countof(mp3FrameInfo)-1].pcmFrameIndex = runningPCMFrameCount;
3514 
3515  /*
3516  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
3517  should only ever do it for the last seek point.
3518  */
3519  pcmFramesInCurrentMP3FrameIn = drmp3_decode_next_frame_ex(pMP3, NULL, DRMP3_TRUE);
3520  if (pcmFramesInCurrentMP3FrameIn == 0) {
3521  pSeekPoints[iSeekPoint].seekPosInBytes = mp3FrameInfo[0].bytePos;
3522  pSeekPoints[iSeekPoint].pcmFrameIndex = nextTargetPCMFrame;
3523  pSeekPoints[iSeekPoint].mp3FramesToDiscard = DRMP3_SEEK_LEADING_MP3_FRAMES;
3524  pSeekPoints[iSeekPoint].pcmFramesToDiscard = (drmp3_uint16)(nextTargetPCMFrame - mp3FrameInfo[DRMP3_SEEK_LEADING_MP3_FRAMES-1].pcmFrameIndex);
3525  break;
3526  }
3527 
3528  drmp3__accumulate_running_pcm_frame_count(pMP3, pcmFramesInCurrentMP3FrameIn, &runningPCMFrameCount, &runningPCMFrameCountFractionalPart);
3529  }
3530  }
3531  }
3532 
3533  /* Finally, we need to seek back to where we were. */
3534  if (!drmp3_seek_to_start_of_stream(pMP3)) {
3535  return DRMP3_FALSE;
3536  }
3537  if (!drmp3_seek_to_pcm_frame(pMP3, currentPCMFrame)) {
3538  return DRMP3_FALSE;
3539  }
3540  }
3541 
3542  *pSeekPointCount = seekPointCount;
3543  return DRMP3_TRUE;
3544 }
3545 
3546 drmp3_bool32 drmp3_bind_seek_table(drmp3* pMP3, drmp3_uint32 seekPointCount, drmp3_seek_point* pSeekPoints)
3547 {
3548  if (pMP3 == NULL) {
3549  return DRMP3_FALSE;
3550  }
3551 
3552  if (seekPointCount == 0 || pSeekPoints == NULL) {
3553  /* Unbinding. */
3554  pMP3->seekPointCount = 0;
3555  pMP3->pSeekPoints = NULL;
3556  } else {
3557  /* Binding. */
3558  pMP3->seekPointCount = seekPointCount;
3559  pMP3->pSeekPoints = pSeekPoints;
3560  }
3561 
3562  return DRMP3_TRUE;
3563 }
3564 
3565 
3566 float* drmp3__full_read_and_close_f32(drmp3* pMP3, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount)
3567 {
3568  drmp3_uint64 totalFramesRead = 0;
3569  drmp3_uint64 framesCapacity = 0;
3570  float* pFrames = NULL;
3571  float temp[4096];
3572 
3573  drmp3_assert(pMP3 != NULL);
3574 
3575  for (;;) {
3576  drmp3_uint64 framesToReadRightNow = drmp3_countof(temp) / pMP3->channels;
3577  drmp3_uint64 framesJustRead = drmp3_read_pcm_frames_f32(pMP3, framesToReadRightNow, temp);
3578  if (framesJustRead == 0) {
3579  break;
3580  }
3581 
3582  /* Reallocate the output buffer if there's not enough room. */
3583  if (framesCapacity < totalFramesRead + framesJustRead) {
3584  drmp3_uint64 newFramesBufferSize;
3585  float* pNewFrames;
3586 
3587  framesCapacity *= 2;
3588  if (framesCapacity < totalFramesRead + framesJustRead) {
3589  framesCapacity = totalFramesRead + framesJustRead;
3590  }
3591 
3592  newFramesBufferSize = framesCapacity*pMP3->channels*sizeof(float);
3593  if (newFramesBufferSize > DRMP3_SIZE_MAX) {
3594  break;
3595  }
3596 
3597  pNewFrames = (float*)drmp3_realloc(pFrames, (size_t)newFramesBufferSize);
3598  if (pNewFrames == NULL) {
3599  drmp3_free(pFrames);
3600  break;
3601  }
3602 
3603  pFrames = pNewFrames;
3604  }
3605 
3606  drmp3_copy_memory(pFrames + totalFramesRead*pMP3->channels, temp, (size_t)(framesJustRead*pMP3->channels*sizeof(float)));
3607  totalFramesRead += framesJustRead;
3608 
3609  /* If the number of frames we asked for is less that what we actually read it means we've reached the end. */
3610  if (framesJustRead != framesToReadRightNow) {
3611  break;
3612  }
3613  }
3614 
3615  if (pConfig != NULL) {
3616  pConfig->outputChannels = pMP3->channels;
3617  pConfig->outputSampleRate = pMP3->sampleRate;
3618  }
3619 
3620  drmp3_uninit(pMP3);
3621 
3622  if (pTotalFrameCount) {
3623  *pTotalFrameCount = totalFramesRead;
3624  }
3625 
3626  return pFrames;
3627 }
3628 
3629 drmp3_int16* drmp3__full_read_and_close_s16(drmp3* pMP3, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount)
3630 {
3631  drmp3_uint64 totalFramesRead = 0;
3632  drmp3_uint64 framesCapacity = 0;
3633  drmp3_int16* pFrames = NULL;
3634  drmp3_int16 temp[4096];
3635 
3636  drmp3_assert(pMP3 != NULL);
3637 
3638  for (;;) {
3639  drmp3_uint64 framesToReadRightNow = drmp3_countof(temp) / pMP3->channels;
3640  drmp3_uint64 framesJustRead = drmp3_read_pcm_frames_s16(pMP3, framesToReadRightNow, temp);
3641  if (framesJustRead == 0) {
3642  break;
3643  }
3644 
3645  /* Reallocate the output buffer if there's not enough room. */
3646  if (framesCapacity < totalFramesRead + framesJustRead) {
3647  drmp3_uint64 newFramesBufferSize;
3648  drmp3_int16* pNewFrames;
3649 
3650  framesCapacity *= 2;
3651  if (framesCapacity < totalFramesRead + framesJustRead) {
3652  framesCapacity = totalFramesRead + framesJustRead;
3653  }
3654 
3655  newFramesBufferSize = framesCapacity*pMP3->channels*sizeof(drmp3_int16);
3656  if (newFramesBufferSize > DRMP3_SIZE_MAX) {
3657  break;
3658  }
3659 
3660  pNewFrames = (drmp3_int16*)drmp3_realloc(pFrames, (size_t)newFramesBufferSize);
3661  if (pNewFrames == NULL) {
3662  drmp3_free(pFrames);
3663  break;
3664  }
3665 
3666  pFrames = pNewFrames;
3667  }
3668 
3669  drmp3_copy_memory(pFrames + totalFramesRead*pMP3->channels, temp, (size_t)(framesJustRead*pMP3->channels*sizeof(drmp3_int16)));
3670  totalFramesRead += framesJustRead;
3671 
3672  /* If the number of frames we asked for is less that what we actually read it means we've reached the end. */
3673  if (framesJustRead != framesToReadRightNow) {
3674  break;
3675  }
3676  }
3677 
3678  if (pConfig != NULL) {
3679  pConfig->outputChannels = pMP3->channels;
3680  pConfig->outputSampleRate = pMP3->sampleRate;
3681  }
3682 
3683  drmp3_uninit(pMP3);
3684 
3685  if (pTotalFrameCount) {
3686  *pTotalFrameCount = totalFramesRead;
3687  }
3688 
3689  return pFrames;
3690 }
3691 
3692 
3693 float* drmp3_open_and_read_f32(drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount)
3694 {
3695  drmp3 mp3;
3696  if (!drmp3_init(&mp3, onRead, onSeek, pUserData, pConfig)) {
3697  return NULL;
3698  }
3699 
3700  return drmp3__full_read_and_close_f32(&mp3, pConfig, pTotalFrameCount);
3701 }
3702 
3703 drmp3_int16* drmp3_open_and_read_s16(drmp3_read_proc onRead, drmp3_seek_proc onSeek, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount)
3704 {
3705  drmp3 mp3;
3706  if (!drmp3_init(&mp3, onRead, onSeek, pUserData, pConfig)) {
3707  return NULL;
3708  }
3709 
3710  return drmp3__full_read_and_close_s16(&mp3, pConfig, pTotalFrameCount);
3711 }
3712 
3713 
3714 float* drmp3_open_memory_and_read_f32(const void* pData, size_t dataSize, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount)
3715 {
3716  drmp3 mp3;
3717  if (!drmp3_init_memory(&mp3, pData, dataSize, pConfig)) {
3718  return NULL;
3719  }
3720 
3721  return drmp3__full_read_and_close_f32(&mp3, pConfig, pTotalFrameCount);
3722 }
3723 
3724 drmp3_int16* drmp3_open_memory_and_read_s16(const void* pData, size_t dataSize, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount)
3725 {
3726  drmp3 mp3;
3727  if (!drmp3_init_memory(&mp3, pData, dataSize, pConfig)) {
3728  return NULL;
3729  }
3730 
3731  return drmp3__full_read_and_close_s16(&mp3, pConfig, pTotalFrameCount);
3732 }
3733 
3734 
3735 #ifndef DR_MP3_NO_STDIO
3736 float* drmp3_open_file_and_read_f32(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount)
3737 {
3738  drmp3 mp3;
3739  if (!drmp3_init_file(&mp3, filePath, pConfig)) {
3740  return NULL;
3741  }
3742 
3743  return drmp3__full_read_and_close_f32(&mp3, pConfig, pTotalFrameCount);
3744 }
3745 
3746 drmp3_int16* drmp3_open_file_and_read_s16(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount)
3747 {
3748  drmp3 mp3;
3749  if (!drmp3_init_file(&mp3, filePath, pConfig)) {
3750  return NULL;
3751  }
3752 
3753  return drmp3__full_read_and_close_s16(&mp3, pConfig, pTotalFrameCount);
3754 }
3755 #endif
3756 
3757 void drmp3_free(void* p)
3758 {
3759  DRMP3_FREE(p);
3760 }
3761 
3762 #endif /*DR_MP3_IMPLEMENTATION*/
3763 
3764 /*
3765 DIFFERENCES BETWEEN minimp3 AND dr_mp3
3766 ======================================
3767 - First, keep in mind that minimp3 (https://github.com/lieff/minimp3) is where all the real work was done. All of the
3768  code relating to the actual decoding remains mostly unmodified, apart from some namespacing changes.
3769 - dr_mp3 adds a pulling style API which allows you to deliver raw data via callbacks. So, rather than pushing data
3770  to the decoder, the decoder _pulls_ data from your callbacks.
3771 - In addition to callbacks, a decoder can be initialized from a block of memory and a file.
3772 - The dr_mp3 pull API reads PCM frames rather than whole MP3 frames.
3773 - dr_mp3 adds convenience APIs for opening and decoding entire files in one go.
3774 - dr_mp3 is fully namespaced, including the implementation section, which is more suitable when compiling projects
3775  as a single translation unit (aka unity builds). At the time of writing this, a unity build is not possible when
3776  using minimp3 in conjunction with stb_vorbis. dr_mp3 addresses this.
3777 */
3778 
3779 /*
3780 REVISION HISTORY
3781 ================
3782 v0.4.4 - 2019-05-06
3783  - Fixes to the VC6 build.
3784 
3785 v0.4.3 - 2019-05-05
3786  - Use the channel count and/or sample rate of the first MP3 frame instead of DR_MP3_DEFAULT_CHANNELS and
3787  DR_MP3_DEFAULT_SAMPLE_RATE when they are set to 0. To use the old behaviour, just set the relevant property to
3788  DR_MP3_DEFAULT_CHANNELS or DR_MP3_DEFAULT_SAMPLE_RATE.
3789  - Add s16 reading APIs
3790  - drmp3_read_pcm_frames_s16
3791  - drmp3_open_memory_and_read_s16
3792  - drmp3_open_and_read_s16
3793  - drmp3_open_file_and_read_s16
3794  - Add drmp3_get_mp3_and_pcm_frame_count() to the public header section.
3795  - Add support for C89.
3796  - Change license to choice of public domain or MIT-0.
3797 
3798 v0.4.2 - 2019-02-21
3799  - Fix a warning.
3800 
3801 v0.4.1 - 2018-12-30
3802  - Fix a warning.
3803 
3804 v0.4.0 - 2018-12-16
3805  - API CHANGE: Rename some APIs:
3806  - drmp3_read_f32 -> to drmp3_read_pcm_frames_f32
3807  - drmp3_seek_to_frame -> drmp3_seek_to_pcm_frame
3808  - drmp3_open_and_decode_f32 -> drmp3_open_and_read_f32
3809  - drmp3_open_and_decode_memory_f32 -> drmp3_open_memory_and_read_f32
3810  - drmp3_open_and_decode_file_f32 -> drmp3_open_file_and_read_f32
3811  - Add drmp3_get_pcm_frame_count().
3812  - Add drmp3_get_mp3_frame_count().
3813  - Improve seeking performance.
3814 
3815 v0.3.2 - 2018-09-11
3816  - Fix a couple of memory leaks.
3817  - Bring up to date with minimp3.
3818 
3819 v0.3.1 - 2018-08-25
3820  - Fix C++ build.
3821 
3822 v0.3.0 - 2018-08-25
3823  - Bring up to date with minimp3. This has a minor API change: the "pcm" parameter of drmp3dec_decode_frame() has
3824  been changed from short* to void* because it can now output both s16 and f32 samples, depending on whether or
3825  not the DR_MP3_FLOAT_OUTPUT option is set.
3826 
3827 v0.2.11 - 2018-08-08
3828  - Fix a bug where the last part of a file is not read.
3829 
3830 v0.2.10 - 2018-08-07
3831  - Improve 64-bit detection.
3832 
3833 v0.2.9 - 2018-08-05
3834  - Fix C++ build on older versions of GCC.
3835  - Bring up to date with minimp3.
3836 
3837 v0.2.8 - 2018-08-02
3838  - Fix compilation errors with older versions of GCC.
3839 
3840 v0.2.7 - 2018-07-13
3841  - Bring up to date with minimp3.
3842 
3843 v0.2.6 - 2018-07-12
3844  - Bring up to date with minimp3.
3845 
3846 v0.2.5 - 2018-06-22
3847  - Bring up to date with minimp3.
3848 
3849 v0.2.4 - 2018-05-12
3850  - Bring up to date with minimp3.
3851 
3852 v0.2.3 - 2018-04-29
3853  - Fix TCC build.
3854 
3855 v0.2.2 - 2018-04-28
3856  - Fix bug when opening a decoder from memory.
3857 
3858 v0.2.1 - 2018-04-27
3859  - Efficiency improvements when the decoder reaches the end of the stream.
3860 
3861 v0.2 - 2018-04-21
3862  - Bring up to date with minimp3.
3863  - Start using major.minor.revision versioning.
3864 
3865 v0.1d - 2018-03-30
3866  - Bring up to date with minimp3.
3867 
3868 v0.1c - 2018-03-11
3869  - Fix C++ build error.
3870 
3871 v0.1b - 2018-03-07
3872  - Bring up to date with minimp3.
3873 
3874 v0.1a - 2018-02-28
3875  - Fix compilation error on GCC/Clang.
3876  - Fix some warnings.
3877 
3878 v0.1 - 2018-02-xx
3879  - Initial versioned release.
3880 */
3881 
3882 /*
3883 This software is available as a choice of the following licenses. Choose
3884 whichever you prefer.
3885 
3886 ===============================================================================
3887 ALTERNATIVE 1 - Public Domain (www.unlicense.org)
3888 ===============================================================================
3889 This is free and unencumbered software released into the public domain.
3890 
3891 Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
3892 software, either in source code form or as a compiled binary, for any purpose,
3893 commercial or non-commercial, and by any means.
3894 
3895 In jurisdictions that recognize copyright laws, the author or authors of this
3896 software dedicate any and all copyright interest in the software to the public
3897 domain. We make this dedication for the benefit of the public at large and to
3898 the detriment of our heirs and successors. We intend this dedication to be an
3899 overt act of relinquishment in perpetuity of all present and future rights to
3900 this software under copyright law.
3901 
3902 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3903 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3904 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3905 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
3906 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
3907 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3908 
3909 For more information, please refer to <http://unlicense.org/>
3910 
3911 ===============================================================================
3912 ALTERNATIVE 2 - MIT No Attribution
3913 ===============================================================================
3914 Copyright 2018 David Reid
3915 
3916 Permission is hereby granted, free of charge, to any person obtaining a copy of
3917 this software and associated documentation files (the "Software"), to deal in
3918 the Software without restriction, including without limitation the rights to
3919 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
3920 of the Software, and to permit persons to whom the Software is furnished to do
3921 so.
3922 
3923 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3924 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3925 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3926 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3927 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3928 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3929 SOFTWARE.
3930 */
3931 
3932 /*
3933  https://github.com/lieff/minimp3
3934  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.
3935  This software is distributed without any warranty.
3936  See <http://creativecommons.org/publicdomain/zero/1.0/>.
3937 */
drmp3dec_f32_to_s16
void drmp3dec_f32_to_s16(const float *in, drmp3_int16 *out, int num_samples)
drmp3::channels
drmp3_uint32 channels
Definition: dr_mp3.h:242
drmp3dec::reserv
int reserv
Definition: dr_mp3.h:119
drmp3_src_read_proc
drmp3_uint64(* drmp3_src_read_proc)(drmp3_src *pSRC, drmp3_uint64 frameCount, void *pFramesOut, void *pUserData)
Definition: dr_mp3.h:146
drmp3dec::mdct_overlap
float mdct_overlap[2][9 *32]
Definition: dr_mp3.h:118
drmp3_init_file
drmp3_bool32 drmp3_init_file(drmp3 *pMP3, const char *filePath, const drmp3_config *pConfig)
drmp3_uint8
uint8_t drmp3_uint8
Definition: dr_mp3.h:81
drmp3::pcmFramesConsumedInMP3Frame
drmp3_uint32 pcmFramesConsumedInMP3Frame
Definition: dr_mp3.h:249
drmp3_src::bin
float bin[256]
Definition: dr_mp3.h:177
int
CONST PIXELFORMATDESCRIPTOR int
Definition: qgl_win.c:35
drmp3dec_frame_info::layer
int layer
Definition: dr_mp3.h:113
drmp3_init
drmp3_bool32 drmp3_init(drmp3 *pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, void *pUserData, const drmp3_config *pConfig)
drmp3::memory
struct drmp3::@9 memory
drmp3::currentReadPos
size_t currentReadPos
Definition: dr_mp3.h:265
drmp3_get_mp3_and_pcm_frame_count
drmp3_bool32 drmp3_get_mp3_and_pcm_frame_count(drmp3 *pMP3, drmp3_uint64 *pMP3FrameCount, drmp3_uint64 *pPCMFrameCount)
drmp3::currentPCMFrame
drmp3_uint64 currentPCMFrame
Definition: dr_mp3.h:252
drmp3_config
Definition: dr_mp3.h:232
drmp3_open_memory_and_read_s16
drmp3_int16 * drmp3_open_memory_and_read_s16(const void *pData, size_t dataSize, drmp3_config *pConfig, drmp3_uint64 *pTotalFrameCount)
drmp3_free
void drmp3_free(void *p)
drmp3_src_config::algorithm
drmp3_src_algorithm algorithm
Definition: dr_mp3.h:168
drmp3dec_decode_frame
int drmp3dec_decode_frame(drmp3dec *dec, const unsigned char *mp3, int mp3_bytes, void *pcm, drmp3dec_frame_info *info)
drmp3_src
Definition: dr_mp3.h:172
x2
GLdouble GLdouble x2
Definition: qgl_win.c:301
drmp3_src::pUserData
void * pUserData
Definition: dr_mp3.h:176
drmp3dec_frame_info::bitrate_kbps
int bitrate_kbps
Definition: dr_mp3.h:113
x
GLint GLenum GLint x
Definition: qgl_win.c:116
drmp3dec_frame_info::hz
int hz
Definition: dr_mp3.h:113
z
GLdouble GLdouble z
Definition: qgl_win.c:283
drmp3_config::outputChannels
drmp3_uint32 outputChannels
Definition: dr_mp3.h:234
i
int i
Definition: q_shared.c:305
drmp3::pSeekPoints
drmp3_seek_point * pSeekPoints
Definition: dr_mp3.h:255
drmp3dec::free_format_bytes
int free_format_bytes
Definition: dr_mp3.h:119
drmp3_seek_point::pcmFrameIndex
drmp3_uint64 pcmFrameIndex
Definition: dr_mp3.h:199
drmp3_src::isNextFramesLoaded
drmp3_bool32 isNextFramesLoaded
Definition: dr_mp3.h:185
drmp3_src_algorithm
drmp3_src_algorithm
Definition: dr_mp3.h:148
drmp3::pData
drmp3_uint8 * pData
Definition: dr_mp3.h:259
drmp3::frameInfo
drmp3dec_frame_info frameInfo
Definition: dr_mp3.h:241
DR_MP3_DEFAULT_SAMPLE_RATE
#define DR_MP3_DEFAULT_SAMPLE_RATE
Definition: dr_mp3.h:142
drmp3_open_and_read_s16
drmp3_int16 * drmp3_open_and_read_s16(drmp3_read_proc onRead, drmp3_seek_proc onSeek, void *pUserData, drmp3_config *pConfig, drmp3_uint64 *pTotalFrameCount)
drmp3dec::reserv_buf
unsigned char reserv_buf[511]
Definition: dr_mp3.h:120
drmp3
Definition: dr_mp3.h:238
drmp3dec_frame_info
Definition: dr_mp3.h:111
drmp3_seek_point::pcmFramesToDiscard
drmp3_uint16 pcmFramesToDiscard
Definition: dr_mp3.h:201
drmp3dec::header
unsigned char header[4]
Definition: dr_mp3.h:120
drmp3::dataCapacity
size_t dataCapacity
Definition: dr_mp3.h:258
DRMP3_MAX_SAMPLES_PER_FRAME
#define DRMP3_MAX_SAMPLES_PER_FRAME
Definition: dr_mp3.h:95
drmp3_seek_point
Definition: dr_mp3.h:196
drmp3_uninit
void drmp3_uninit(drmp3 *pMP3)
j
GLint j
Definition: qgl_win.c:150
drmp3_bind_seek_table
drmp3_bool32 drmp3_bind_seek_table(drmp3 *pMP3, drmp3_uint32 seekPointCount, drmp3_seek_point *pSeekPoints)
DRMP3_MAX_PCM_FRAMES_PER_MP3_FRAME
#define DRMP3_MAX_PCM_FRAMES_PER_MP3_FRAME
Definition: dr_mp3.h:94
drmp3dec
Definition: dr_mp3.h:116
u
static int u
Definition: r_part.c:472
drmp3::mp3FrameChannels
drmp3_uint32 mp3FrameChannels
Definition: dr_mp3.h:247
drmp3::sampleRate
drmp3_uint32 sampleRate
Definition: dr_mp3.h:243
drmp3_int64
int64_t drmp3_int64
Definition: dr_mp3.h:86
t
GLdouble t
Definition: qgl_win.c:328
drmp3_src_config::sampleRateOut
drmp3_uint32 sampleRateOut
Definition: dr_mp3.h:166
drmp3_src::cache
drmp3_src_cache cache
Definition: dr_mp3.h:178
drmp3::pData
const drmp3_uint8 * pData
Definition: dr_mp3.h:263
drmp3_int32
int32_t drmp3_int32
Definition: dr_mp3.h:84
drmp3_seek_origin_current
@ drmp3_seek_origin_current
Definition: dr_mp3.h:193
drmp3_open_memory_and_read_f32
float * drmp3_open_memory_and_read_f32(const void *pData, size_t dataSize, drmp3_config *pConfig, drmp3_uint64 *pTotalFrameCount)
drmp3_seek_to_pcm_frame
drmp3_bool32 drmp3_seek_to_pcm_frame(drmp3 *pMP3, drmp3_uint64 frameIndex)
drmp3_src_config::cacheSizeInFrames
drmp3_uint32 cacheSizeInFrames
Definition: dr_mp3.h:169
drmp3dec::qmf_state
float qmf_state[15 *2 *32]
Definition: dr_mp3.h:118
drmp3_src::algo
union drmp3_src::@7 algo
drmp3_src_cache::iNextFrame
drmp3_uint32 iNextFrame
Definition: dr_mp3.h:160
drmp3::pcmFramesRemainingInMP3Frame
drmp3_uint32 pcmFramesRemainingInMP3Frame
Definition: dr_mp3.h:250
drmp3_open_file_and_read_s16
drmp3_int16 * drmp3_open_file_and_read_s16(const char *filePath, drmp3_config *pConfig, drmp3_uint64 *pTotalFrameCount)
NULL
#define NULL
Definition: q_shared.h:67
drmp3::pcmFrames
drmp3_uint8 pcmFrames[sizeof(float) *DRMP3_MAX_SAMPLES_PER_FRAME]
Definition: dr_mp3.h:251
drmp3::onSeek
drmp3_seek_proc onSeek
Definition: dr_mp3.h:245
drmp3::mp3FrameSampleRate
drmp3_uint32 mp3FrameSampleRate
Definition: dr_mp3.h:248
drmp3_src_cache::cachedFrameCount
drmp3_uint32 cachedFrameCount
Definition: dr_mp3.h:159
channels
channel_t channels[MAX_CHANNELS]
Definition: snd_dma.c:42
drmp3dec_init
void drmp3dec_init(drmp3dec *dec)
drmp3_uint16
uint16_t drmp3_uint16
Definition: dr_mp3.h:83
drmp3_src::onRead
drmp3_src_read_proc onRead
Definition: dr_mp3.h:175
drmp3_src::linear
struct drmp3_src::@7::@8 linear
drmp3_src_config::channels
drmp3_uint32 channels
Definition: dr_mp3.h:167
drmp3_get_mp3_frame_count
drmp3_uint64 drmp3_get_mp3_frame_count(drmp3 *pMP3)
drmp3_bool8
drmp3_uint8 drmp3_bool8
Definition: dr_mp3.h:89
drmp3_src::config
drmp3_src_config config
Definition: dr_mp3.h:174
drmp3_src_cache::pSRC
drmp3_src * pSRC
Definition: dr_mp3.h:157
drmp3_seek_point::seekPosInBytes
drmp3_uint64 seekPosInBytes
Definition: dr_mp3.h:198
drmp3_calculate_seek_points
drmp3_bool32 drmp3_calculate_seek_points(drmp3 *pMP3, drmp3_uint32 *pSeekPointCount, drmp3_seek_point *pSeekPoints)
s
static fixed16_t s
Definition: r_scan.c:30
drmp3_src::alpha
double alpha
Definition: dr_mp3.h:183
y
GLint y
Definition: qgl_win.c:115
drmp3_config::outputSampleRate
drmp3_uint32 outputSampleRate
Definition: dr_mp3.h:235
drmp3_src_cache
Definition: dr_mp3.h:155
DRMP3_TRUE
#define DRMP3_TRUE
Definition: dr_mp3.h:91
drmp3_read_pcm_frames_s16
drmp3_uint64 drmp3_read_pcm_frames_s16(drmp3 *pMP3, drmp3_uint64 framesToRead, drmp3_int16 *pBufferOut)
drmp3_src_cache::pCachedFrames
float pCachedFrames[2 *DRMP3_SRC_CACHE_SIZE_IN_FRAMES]
Definition: dr_mp3.h:158
drmp3_src_config::sampleRateIn
drmp3_uint32 sampleRateIn
Definition: dr_mp3.h:165
drmp3_seek_proc
drmp3_bool32(* drmp3_seek_proc)(void *pUserData, int offset, drmp3_seek_origin origin)
Definition: dr_mp3.h:230
DRMP3_SRC_CACHE_SIZE_IN_FRAMES
#define DRMP3_SRC_CACHE_SIZE_IN_FRAMES
Definition: dr_mp3.h:154
drmp3_read_pcm_frames_f32
drmp3_uint64 drmp3_read_pcm_frames_f32(drmp3 *pMP3, drmp3_uint64 framesToRead, float *pBufferOut)
drmp3_bool32
drmp3_uint32 drmp3_bool32
Definition: dr_mp3.h:90
DRMP3_FALSE
#define DRMP3_FALSE
Definition: dr_mp3.h:92
drmp3_uint32
uint32_t drmp3_uint32
Definition: dr_mp3.h:85
drmp3_seek_origin_start
@ drmp3_seek_origin_start
Definition: dr_mp3.h:192
drmp3::decoder
drmp3dec decoder
Definition: dr_mp3.h:240
drmp3dec_frame_info::frame_bytes
int frame_bytes
Definition: dr_mp3.h:113
drmp3dec_frame_info::channels
int channels
Definition: dr_mp3.h:113
drmp3::atEnd
drmp3_bool32 atEnd
Definition: dr_mp3.h:260
drmp3_uint64
uint64_t drmp3_uint64
Definition: dr_mp3.h:87
drmp3_read_proc
size_t(* drmp3_read_proc)(void *pUserData, void *pBufferOut, size_t bytesToRead)
Definition: dr_mp3.h:216
drmp3_int16
int16_t drmp3_int16
Definition: dr_mp3.h:82
sample
Definition: jar_mod.h:111
w
GLdouble GLdouble GLdouble w
Definition: qgl_win.c:291
right
GLdouble right
Definition: qgl_win.c:159
drmp3_src_algorithm_none
@ drmp3_src_algorithm_none
Definition: dr_mp3.h:150
drmp3::pUserData
void * pUserData
Definition: dr_mp3.h:246
drmp3_open_and_read_f32
float * drmp3_open_and_read_f32(drmp3_read_proc onRead, drmp3_seek_proc onSeek, void *pUserData, drmp3_config *pConfig, drmp3_uint64 *pTotalFrameCount)
DRMP3_INLINE
#define DRMP3_INLINE
Definition: dr_mp3.h:103
drmp3_seek_origin
drmp3_seek_origin
Definition: dr_mp3.h:190
mode
GLenum mode
Definition: qgl_win.c:113
mask
GLint GLuint mask
Definition: qgl_win.c:317
drmp3::seekPointCount
drmp3_uint32 seekPointCount
Definition: dr_mp3.h:256
drmp3_open_file_and_read_f32
float * drmp3_open_file_and_read_f32(const char *filePath, drmp3_config *pConfig, drmp3_uint64 *pTotalFrameCount)
drmp3_seek_point::mp3FramesToDiscard
drmp3_uint16 mp3FramesToDiscard
Definition: dr_mp3.h:200
drmp3::onRead
drmp3_read_proc onRead
Definition: dr_mp3.h:244
drmp3_src::isPrevFramesLoaded
drmp3_bool32 isPrevFramesLoaded
Definition: dr_mp3.h:184
drmp3_init_memory
drmp3_bool32 drmp3_init_memory(drmp3 *pMP3, const void *pData, size_t dataSize, const drmp3_config *pConfig)
drmp3::dataSize
size_t dataSize
Definition: dr_mp3.h:257
drmp3_int8
int8_t drmp3_int8
Definition: dr_mp3.h:80
drmp3_src_config
Definition: dr_mp3.h:163
drmp3_src_algorithm_linear
@ drmp3_src_algorithm_linear
Definition: dr_mp3.h:151
drmp3::streamCursor
drmp3_uint64 streamCursor
Definition: dr_mp3.h:253
void
void(APIENTRY *qglAccum)(GLenum op
drmp3::src
drmp3_src src
Definition: dr_mp3.h:254
drmp3_get_pcm_frame_count
drmp3_uint64 drmp3_get_pcm_frame_count(drmp3 *pMP3)