| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
#ifndef dr_mp3_h |
| 64 |
#define dr_mp3_h |
| 65 |
|
| 66 |
#ifdef __cplusplus |
| 67 |
extern "C" { |
| 68 |
#endif |
| 69 |
|
| 70 |
#define DRMP3_STRINGIFY(x) #x |
| 71 |
#define DRMP3_XSTRINGIFY(x) DRMP3_STRINGIFY(x) |
| 72 |
|
| 73 |
#define DRMP3_VERSION_MAJOR 0 |
| 74 |
#define DRMP3_VERSION_MINOR 7 |
| 75 |
#define DRMP3_VERSION_REVISION 4 |
| 76 |
#define DRMP3_VERSION_STRING DRMP3_XSTRINGIFY(DRMP3_VERSION_MAJOR) "." DRMP3_XSTRINGIFY(DRMP3_VERSION_MINOR) "." DRMP3_XSTRINGIFY(DRMP3_VERSION_REVISION) |
| 77 |
|
| 78 |
#include <stddef.h> |
| 79 |
|
| 80 |
|
| 81 |
typedef signed char drmp3_int8; |
| 82 |
typedef unsigned char drmp3_uint8; |
| 83 |
typedef signed short drmp3_int16; |
| 84 |
typedef unsigned short drmp3_uint16; |
| 85 |
typedef signed int drmp3_int32; |
| 86 |
typedef unsigned int drmp3_uint32; |
| 87 |
#if defined(_MSC_VER) && !defined(__clang__) |
| 88 |
typedef signed __int64 drmp3_int64; |
| 89 |
typedef unsigned __int64 drmp3_uint64; |
| 90 |
#else |
| 91 |
#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))) |
| 92 |
#pragma GCC diagnostic push |
| 93 |
#pragma GCC diagnostic ignored "-Wlong-long" |
| 94 |
#if defined(__clang__) |
| 95 |
#pragma GCC diagnostic ignored "-Wc++11-long-long" |
| 96 |
#endif |
| 97 |
#endif |
| 98 |
typedef signed long long drmp3_int64; |
| 99 |
typedef unsigned long long drmp3_uint64; |
| 100 |
#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))) |
| 101 |
#pragma GCC diagnostic pop |
| 102 |
#endif |
| 103 |
#endif |
| 104 |
#if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__)) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC) || defined(__powerpc64__) |
| 105 |
typedef drmp3_uint64 drmp3_uintptr; |
| 106 |
#else |
| 107 |
typedef drmp3_uint32 drmp3_uintptr; |
| 108 |
#endif |
| 109 |
typedef drmp3_uint8 drmp3_bool8; |
| 110 |
typedef drmp3_uint32 drmp3_bool32; |
| 111 |
#define DRMP3_TRUE 1 |
| 112 |
#define DRMP3_FALSE 0 |
| 113 |
|
| 114 |
|
| 115 |
#define DRMP3_UINT64_MAX (((drmp3_uint64)0xFFFFFFFF << 32) | (drmp3_uint64)0xFFFFFFFF) |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
#if !defined(DRMP3_API) |
| 120 |
#if defined(DRMP3_DLL) |
| 121 |
#if defined(_WIN32) |
| 122 |
#define DRMP3_DLL_IMPORT __declspec(dllimport) |
| 123 |
#define DRMP3_DLL_EXPORT __declspec(dllexport) |
| 124 |
#define DRMP3_DLL_PRIVATE static |
| 125 |
#else |
| 126 |
#if defined(__GNUC__) && __GNUC__ >= 4 |
| 127 |
#define DRMP3_DLL_IMPORT __attribute__((visibility("default"))) |
| 128 |
#define DRMP3_DLL_EXPORT __attribute__((visibility("default"))) |
| 129 |
#define DRMP3_DLL_PRIVATE __attribute__((visibility("hidden"))) |
| 130 |
#else |
| 131 |
#define DRMP3_DLL_IMPORT |
| 132 |
#define DRMP3_DLL_EXPORT |
| 133 |
#define DRMP3_DLL_PRIVATE static |
| 134 |
#endif |
| 135 |
#endif |
| 136 |
|
| 137 |
#if defined(DR_MP3_IMPLEMENTATION) |
| 138 |
#define DRMP3_API DRMP3_DLL_EXPORT |
| 139 |
#else |
| 140 |
#define DRMP3_API DRMP3_DLL_IMPORT |
| 141 |
#endif |
| 142 |
#define DRMP3_PRIVATE DRMP3_DLL_PRIVATE |
| 143 |
#else |
| 144 |
#define DRMP3_API extern |
| 145 |
#define DRMP3_PRIVATE static |
| 146 |
#endif |
| 147 |
#endif |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
typedef drmp3_int32 drmp3_result; |
| 152 |
#define DRMP3_SUCCESS 0 |
| 153 |
#define DRMP3_ERROR -1 |
| 154 |
#define DRMP3_INVALID_ARGS -2 |
| 155 |
#define DRMP3_INVALID_OPERATION -3 |
| 156 |
#define DRMP3_OUT_OF_MEMORY -4 |
| 157 |
#define DRMP3_OUT_OF_RANGE -5 |
| 158 |
#define DRMP3_ACCESS_DENIED -6 |
| 159 |
#define DRMP3_DOES_NOT_EXIST -7 |
| 160 |
#define DRMP3_ALREADY_EXISTS -8 |
| 161 |
#define DRMP3_TOO_MANY_OPEN_FILES -9 |
| 162 |
#define DRMP3_INVALID_FILE -10 |
| 163 |
#define DRMP3_TOO_BIG -11 |
| 164 |
#define DRMP3_PATH_TOO_LONG -12 |
| 165 |
#define DRMP3_NAME_TOO_LONG -13 |
| 166 |
#define DRMP3_NOT_DIRECTORY -14 |
| 167 |
#define DRMP3_IS_DIRECTORY -15 |
| 168 |
#define DRMP3_DIRECTORY_NOT_EMPTY -16 |
| 169 |
#define DRMP3_END_OF_FILE -17 |
| 170 |
#define DRMP3_NO_SPACE -18 |
| 171 |
#define DRMP3_BUSY -19 |
| 172 |
#define DRMP3_IO_ERROR -20 |
| 173 |
#define DRMP3_INTERRUPT -21 |
| 174 |
#define DRMP3_UNAVAILABLE -22 |
| 175 |
#define DRMP3_ALREADY_IN_USE -23 |
| 176 |
#define DRMP3_BAD_ADDRESS -24 |
| 177 |
#define DRMP3_BAD_SEEK -25 |
| 178 |
#define DRMP3_BAD_PIPE -26 |
| 179 |
#define DRMP3_DEADLOCK -27 |
| 180 |
#define DRMP3_TOO_MANY_LINKS -28 |
| 181 |
#define DRMP3_NOT_IMPLEMENTED -29 |
| 182 |
#define DRMP3_NO_MESSAGE -30 |
| 183 |
#define DRMP3_BAD_MESSAGE -31 |
| 184 |
#define DRMP3_NO_DATA_AVAILABLE -32 |
| 185 |
#define DRMP3_INVALID_DATA -33 |
| 186 |
#define DRMP3_TIMEOUT -34 |
| 187 |
#define DRMP3_NO_NETWORK -35 |
| 188 |
#define DRMP3_NOT_UNIQUE -36 |
| 189 |
#define DRMP3_NOT_SOCKET -37 |
| 190 |
#define DRMP3_NO_ADDRESS -38 |
| 191 |
#define DRMP3_BAD_PROTOCOL -39 |
| 192 |
#define DRMP3_PROTOCOL_UNAVAILABLE -40 |
| 193 |
#define DRMP3_PROTOCOL_NOT_SUPPORTED -41 |
| 194 |
#define DRMP3_PROTOCOL_FAMILY_NOT_SUPPORTED -42 |
| 195 |
#define DRMP3_ADDRESS_FAMILY_NOT_SUPPORTED -43 |
| 196 |
#define DRMP3_SOCKET_NOT_SUPPORTED -44 |
| 197 |
#define DRMP3_CONNECTION_RESET -45 |
| 198 |
#define DRMP3_ALREADY_CONNECTED -46 |
| 199 |
#define DRMP3_NOT_CONNECTED -47 |
| 200 |
#define DRMP3_CONNECTION_REFUSED -48 |
| 201 |
#define DRMP3_NO_HOST -49 |
| 202 |
#define DRMP3_IN_PROGRESS -50 |
| 203 |
#define DRMP3_CANCELLED -51 |
| 204 |
#define DRMP3_MEMORY_ALREADY_MAPPED -52 |
| 205 |
#define DRMP3_AT_END -53 |
| 206 |
|
| 207 |
|
| 208 |
#define DRMP3_MAX_PCM_FRAMES_PER_MP3_FRAME 1152 |
| 209 |
#define DRMP3_MAX_SAMPLES_PER_FRAME (DRMP3_MAX_PCM_FRAMES_PER_MP3_FRAME*2) |
| 210 |
|
| 211 |
|
| 212 |
#ifdef _MSC_VER |
| 213 |
#define DRMP3_INLINE __forceinline |
| 214 |
#elif defined(__GNUC__) |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
#if defined(__STRICT_ANSI__) |
| 223 |
#define DRMP3_GNUC_INLINE_HINT __inline__ |
| 224 |
#else |
| 225 |
#define DRMP3_GNUC_INLINE_HINT inline |
| 226 |
#endif |
| 227 |
|
| 228 |
#if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2)) || defined(__clang__) |
| 229 |
#define DRMP3_INLINE DRMP3_GNUC_INLINE_HINT __attribute__((always_inline)) |
| 230 |
#else |
| 231 |
#define DRMP3_INLINE DRMP3_GNUC_INLINE_HINT |
| 232 |
#endif |
| 233 |
#elif defined(__WATCOMC__) |
| 234 |
#define DRMP3_INLINE __inline |
| 235 |
#else |
| 236 |
#define DRMP3_INLINE |
| 237 |
#endif |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
DRMP3_API void drmp3_version(drmp3_uint32* pMajor, drmp3_uint32* pMinor, drmp3_uint32* pRevision); |
| 242 |
DRMP3_API const char* drmp3_version_string(void); |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
typedef struct |
| 247 |
{ |
| 248 |
void* pUserData; |
| 249 |
void* (* onMalloc)(size_t sz, void* pUserData); |
| 250 |
void* (* onRealloc)(void* p, size_t sz, void* pUserData); |
| 251 |
void (* onFree)(void* p, void* pUserData); |
| 252 |
} drmp3_allocation_callbacks; |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
#define DRMP3_MAX_BITRESERVOIR_BYTES 511 |
| 261 |
#define DRMP3_MAX_FREE_FORMAT_FRAME_SIZE 2304 |
| 262 |
#define DRMP3_MAX_L3_FRAME_PAYLOAD_BYTES DRMP3_MAX_FREE_FORMAT_FRAME_SIZE |
| 263 |
|
| 264 |
typedef struct |
| 265 |
{ |
| 266 |
int frame_bytes, channels, sample_rate, layer, bitrate_kbps; |
| 267 |
} drmp3dec_frame_info; |
| 268 |
|
| 269 |
typedef struct |
| 270 |
{ |
| 271 |
const drmp3_uint8 *buf; |
| 272 |
int pos, limit; |
| 273 |
} drmp3_bs; |
| 274 |
|
| 275 |
typedef struct |
| 276 |
{ |
| 277 |
const drmp3_uint8 *sfbtab; |
| 278 |
drmp3_uint16 part_23_length, big_values, scalefac_compress; |
| 279 |
drmp3_uint8 global_gain, block_type, mixed_block_flag, n_long_sfb, n_short_sfb; |
| 280 |
drmp3_uint8 table_select[3], region_count[3], subblock_gain[3]; |
| 281 |
drmp3_uint8 preflag, scalefac_scale, count1_table, scfsi; |
| 282 |
} drmp3_L3_gr_info; |
| 283 |
|
| 284 |
typedef struct |
| 285 |
{ |
| 286 |
drmp3_bs bs; |
| 287 |
drmp3_uint8 maindata[DRMP3_MAX_BITRESERVOIR_BYTES + DRMP3_MAX_L3_FRAME_PAYLOAD_BYTES]; |
| 288 |
drmp3_L3_gr_info gr_info[4]; |
| 289 |
float grbuf[2][576], scf[40], syn[18 + 15][2*32]; |
| 290 |
drmp3_uint8 ist_pos[2][39]; |
| 291 |
} drmp3dec_scratch; |
| 292 |
|
| 293 |
typedef struct |
| 294 |
{ |
| 295 |
float mdct_overlap[2][9*32], qmf_state[15*2*32]; |
| 296 |
int reserv, free_format_bytes; |
| 297 |
drmp3_uint8 header[4], reserv_buf[511]; |
| 298 |
drmp3dec_scratch scratch; |
| 299 |
} drmp3dec; |
| 300 |
|
| 301 |
|
| 302 |
DRMP3_API void drmp3dec_init(drmp3dec *dec); |
| 303 |
|
| 304 |
|
| 305 |
DRMP3_API int drmp3dec_decode_frame(drmp3dec *dec, const drmp3_uint8 *mp3, int mp3_bytes, void *pcm, drmp3dec_frame_info *info); |
| 306 |
|
| 307 |
|
| 308 |
DRMP3_API void drmp3dec_f32_to_s16(const float *in, drmp3_int16 *out, size_t num_samples); |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
typedef enum |
| 317 |
{ |
| 318 |
DRMP3_SEEK_SET, |
| 319 |
DRMP3_SEEK_CUR, |
| 320 |
DRMP3_SEEK_END |
| 321 |
} drmp3_seek_origin; |
| 322 |
|
| 323 |
typedef struct |
| 324 |
{ |
| 325 |
drmp3_uint64 seekPosInBytes; |
| 326 |
drmp3_uint64 pcmFrameIndex; |
| 327 |
drmp3_uint16 mp3FramesToDiscard; |
| 328 |
drmp3_uint16 pcmFramesToDiscard; |
| 329 |
} drmp3_seek_point; |
| 330 |
|
| 331 |
typedef enum |
| 332 |
{ |
| 333 |
DRMP3_METADATA_TYPE_ID3V1, |
| 334 |
DRMP3_METADATA_TYPE_ID3V2, |
| 335 |
DRMP3_METADATA_TYPE_APE, |
| 336 |
DRMP3_METADATA_TYPE_XING, |
| 337 |
DRMP3_METADATA_TYPE_VBRI |
| 338 |
} drmp3_metadata_type; |
| 339 |
|
| 340 |
typedef struct |
| 341 |
{ |
| 342 |
drmp3_metadata_type type; |
| 343 |
const void* pRawData; |
| 344 |
size_t rawDataSize; |
| 345 |
} drmp3_metadata; |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
typedef size_t (* drmp3_read_proc)(void* pUserData, void* pBufferOut, size_t bytesToRead); |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
typedef drmp3_bool32 (* drmp3_seek_proc)(void* pUserData, int offset, drmp3_seek_origin origin); |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
typedef drmp3_bool32 (* drmp3_tell_proc)(void* pUserData, drmp3_int64* pCursor); |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
typedef void (* drmp3_meta_proc)(void* pUserData, const drmp3_metadata* pMetadata); |
| 390 |
|
| 391 |
|
| 392 |
typedef struct |
| 393 |
{ |
| 394 |
drmp3_uint32 channels; |
| 395 |
drmp3_uint32 sampleRate; |
| 396 |
} drmp3_config; |
| 397 |
|
| 398 |
typedef struct |
| 399 |
{ |
| 400 |
drmp3dec decoder; |
| 401 |
drmp3_uint32 channels; |
| 402 |
drmp3_uint32 sampleRate; |
| 403 |
drmp3_read_proc onRead; |
| 404 |
drmp3_seek_proc onSeek; |
| 405 |
drmp3_meta_proc onMeta; |
| 406 |
void* pUserData; |
| 407 |
void* pUserDataMeta; |
| 408 |
drmp3_allocation_callbacks allocationCallbacks; |
| 409 |
drmp3_uint32 mp3FrameChannels; |
| 410 |
drmp3_uint32 mp3FrameSampleRate; |
| 411 |
drmp3_uint32 pcmFramesConsumedInMP3Frame; |
| 412 |
drmp3_uint32 pcmFramesRemainingInMP3Frame; |
| 413 |
drmp3_uint8 pcmFrames[sizeof(float)*DRMP3_MAX_SAMPLES_PER_FRAME]; |
| 414 |
drmp3_uint64 currentPCMFrame; |
| 415 |
drmp3_uint64 streamCursor; |
| 416 |
drmp3_uint64 streamLength; |
| 417 |
drmp3_uint64 streamStartOffset; |
| 418 |
drmp3_seek_point* pSeekPoints; |
| 419 |
drmp3_uint32 seekPointCount; |
| 420 |
drmp3_uint32 delayInPCMFrames; |
| 421 |
drmp3_uint32 paddingInPCMFrames; |
| 422 |
drmp3_uint64 totalPCMFrameCount; |
| 423 |
drmp3_bool32 isVBR; |
| 424 |
drmp3_bool32 isCBR; |
| 425 |
size_t dataSize; |
| 426 |
size_t dataCapacity; |
| 427 |
size_t dataConsumed; |
| 428 |
drmp3_uint8* pData; |
| 429 |
drmp3_bool32 atEnd; |
| 430 |
struct |
| 431 |
{ |
| 432 |
const drmp3_uint8* pData; |
| 433 |
size_t dataSize; |
| 434 |
size_t currentReadPos; |
| 435 |
} memory; |
| 436 |
} drmp3; |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
DRMP3_API drmp3_bool32 drmp3_init(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, drmp3_meta_proc onMeta, void* pUserData, const drmp3_allocation_callbacks* pAllocationCallbacks); |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
DRMP3_API drmp3_bool32 drmp3_init_memory_with_metadata(drmp3* pMP3, const void* pData, size_t dataSize, drmp3_meta_proc onMeta, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks); |
| 463 |
DRMP3_API drmp3_bool32 drmp3_init_memory(drmp3* pMP3, const void* pData, size_t dataSize, const drmp3_allocation_callbacks* pAllocationCallbacks); |
| 464 |
|
| 465 |
#ifndef DR_MP3_NO_STDIO |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
DRMP3_API drmp3_bool32 drmp3_init_file_with_metadata(drmp3* pMP3, const char* pFilePath, drmp3_meta_proc onMeta, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks); |
| 474 |
DRMP3_API drmp3_bool32 drmp3_init_file_with_metadata_w(drmp3* pMP3, const wchar_t* pFilePath, drmp3_meta_proc onMeta, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks); |
| 475 |
|
| 476 |
DRMP3_API drmp3_bool32 drmp3_init_file(drmp3* pMP3, const char* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks); |
| 477 |
DRMP3_API drmp3_bool32 drmp3_init_file_w(drmp3* pMP3, const wchar_t* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks); |
| 478 |
#endif |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
DRMP3_API void drmp3_uninit(drmp3* pMP3); |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
DRMP3_API drmp3_uint64 drmp3_read_pcm_frames_f32(drmp3* pMP3, drmp3_uint64 framesToRead, float* pBufferOut); |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
DRMP3_API drmp3_uint64 drmp3_read_pcm_frames_s16(drmp3* pMP3, drmp3_uint64 framesToRead, drmp3_int16* pBufferOut); |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
DRMP3_API drmp3_bool32 drmp3_seek_to_pcm_frame(drmp3* pMP3, drmp3_uint64 frameIndex); |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
DRMP3_API drmp3_uint64 drmp3_get_pcm_frame_count(drmp3* pMP3); |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
DRMP3_API drmp3_uint64 drmp3_get_mp3_frame_count(drmp3* pMP3); |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
DRMP3_API drmp3_bool32 drmp3_get_mp3_and_pcm_frame_count(drmp3* pMP3, drmp3_uint64* pMP3FrameCount, drmp3_uint64* pPCMFrameCount); |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
DRMP3_API drmp3_bool32 drmp3_calculate_seek_points(drmp3* pMP3, drmp3_uint32* pSeekPointCount, drmp3_seek_point* pSeekPoints); |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
DRMP3_API drmp3_bool32 drmp3_bind_seek_table(drmp3* pMP3, drmp3_uint32 seekPointCount, drmp3_seek_point* pSeekPoints); |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
DRMP3_API float* drmp3_open_and_read_pcm_frames_f32(drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks); |
| 556 |
DRMP3_API drmp3_int16* drmp3_open_and_read_pcm_frames_s16(drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks); |
| 557 |
|
| 558 |
DRMP3_API float* drmp3_open_memory_and_read_pcm_frames_f32(const void* pData, size_t dataSize, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks); |
| 559 |
DRMP3_API drmp3_int16* drmp3_open_memory_and_read_pcm_frames_s16(const void* pData, size_t dataSize, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks); |
| 560 |
|
| 561 |
#ifndef DR_MP3_NO_STDIO |
| 562 |
DRMP3_API float* drmp3_open_file_and_read_pcm_frames_f32(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks); |
| 563 |
DRMP3_API drmp3_int16* drmp3_open_file_and_read_pcm_frames_s16(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks); |
| 564 |
#endif |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
DRMP3_API void* drmp3_malloc(size_t sz, const drmp3_allocation_callbacks* pAllocationCallbacks); |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
DRMP3_API void drmp3_free(void* p, const drmp3_allocation_callbacks* pAllocationCallbacks); |
| 575 |
|
| 576 |
#ifdef __cplusplus |
| 577 |
} |
| 578 |
#endif |
| 579 |
#endif |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
#if defined(DR_MP3_IMPLEMENTATION) |
| 590 |
#ifndef dr_mp3_c |
| 591 |
#define dr_mp3_c |
| 592 |
|
| 593 |
#include <stdlib.h> |
| 594 |
#include <string.h> |
| 595 |
#include <limits.h> |
| 596 |
|
| 597 |
DRMP3_API void drmp3_version(drmp3_uint32* pMajor, drmp3_uint32* pMinor, drmp3_uint32* pRevision) |
| 598 |
{ |
| 599 |
if (pMajor) { |
| 600 |
*pMajor = DRMP3_VERSION_MAJOR; |
| 601 |
} |
| 602 |
|
| 603 |
if (pMinor) { |
| 604 |
*pMinor = DRMP3_VERSION_MINOR; |
| 605 |
} |
| 606 |
|
| 607 |
if (pRevision) { |
| 608 |
*pRevision = DRMP3_VERSION_REVISION; |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
DRMP3_API const char* drmp3_version_string(void) |
| 613 |
{ |
| 614 |
return DRMP3_VERSION_STRING; |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
#if defined(__TINYC__) |
| 619 |
#define DR_MP3_NO_SIMD |
| 620 |
#endif |
| 621 |
|
| 622 |
#define DRMP3_OFFSET_PTR(p, offset) ((void*)((drmp3_uint8*)(p) + (offset))) |
| 623 |
|
| 624 |
#ifndef DRMP3_MAX_FRAME_SYNC_MATCHES |
| 625 |
#define DRMP3_MAX_FRAME_SYNC_MATCHES 10 |
| 626 |
#endif |
| 627 |
|
| 628 |
#define DRMP3_SHORT_BLOCK_TYPE 2 |
| 629 |
#define DRMP3_STOP_BLOCK_TYPE 3 |
| 630 |
#define DRMP3_MODE_MONO 3 |
| 631 |
#define DRMP3_MODE_JOINT_STEREO 1 |
| 632 |
#define DRMP3_HDR_SIZE 4 |
| 633 |
#define DRMP3_HDR_IS_MONO(h) (((h[3]) & 0xC0) == 0xC0) |
| 634 |
#define DRMP3_HDR_IS_MS_STEREO(h) (((h[3]) & 0xE0) == 0x60) |
| 635 |
#define DRMP3_HDR_IS_FREE_FORMAT(h) (((h[2]) & 0xF0) == 0) |
| 636 |
#define DRMP3_HDR_IS_CRC(h) (!((h[1]) & 1)) |
| 637 |
#define DRMP3_HDR_TEST_PADDING(h) ((h[2]) & 0x2) |
| 638 |
#define DRMP3_HDR_TEST_MPEG1(h) ((h[1]) & 0x8) |
| 639 |
#define DRMP3_HDR_TEST_NOT_MPEG25(h) ((h[1]) & 0x10) |
| 640 |
#define DRMP3_HDR_TEST_I_STEREO(h) ((h[3]) & 0x10) |
| 641 |
#define DRMP3_HDR_TEST_MS_STEREO(h) ((h[3]) & 0x20) |
| 642 |
#define DRMP3_HDR_GET_STEREO_MODE(h) (((h[3]) >> 6) & 3) |
| 643 |
#define DRMP3_HDR_GET_STEREO_MODE_EXT(h) (((h[3]) >> 4) & 3) |
| 644 |
#define DRMP3_HDR_GET_LAYER(h) (((h[1]) >> 1) & 3) |
| 645 |
#define DRMP3_HDR_GET_BITRATE(h) ((h[2]) >> 4) |
| 646 |
#define DRMP3_HDR_GET_SAMPLE_RATE(h) (((h[2]) >> 2) & 3) |
| 647 |
#define DRMP3_HDR_GET_MY_SAMPLE_RATE(h) (DRMP3_HDR_GET_SAMPLE_RATE(h) + (((h[1] >> 3) & 1) + ((h[1] >> 4) & 1))*3) |
| 648 |
#define DRMP3_HDR_IS_FRAME_576(h) ((h[1] & 14) == 2) |
| 649 |
#define DRMP3_HDR_IS_LAYER_1(h) ((h[1] & 6) == 6) |
| 650 |
|
| 651 |
#define DRMP3_BITS_DEQUANTIZER_OUT -1 |
| 652 |
#define DRMP3_MAX_SCF (255 + DRMP3_BITS_DEQUANTIZER_OUT*4 - 210) |
| 653 |
#define DRMP3_MAX_SCFI ((DRMP3_MAX_SCF + 3) & ~3) |
| 654 |
|
| 655 |
#define DRMP3_MIN(a, b) ((a) > (b) ? (b) : (a)) |
| 656 |
#define DRMP3_MAX(a, b) ((a) < (b) ? (b) : (a)) |
| 657 |
|
| 658 |
#if !defined(DR_MP3_NO_SIMD) |
| 659 |
|
| 660 |
#if !defined(DR_MP3_ONLY_SIMD) && ((defined(_MSC_VER) && _MSC_VER >= 1400) && defined(_M_X64)) || ((defined(__i386) || defined(_M_IX86) || defined(__i386__) || defined(__x86_64__)) && ((defined(_M_IX86_FP) && _M_IX86_FP == 2) || defined(__SSE2__))) |
| 661 |
#define DR_MP3_ONLY_SIMD |
| 662 |
#endif |
| 663 |
#if !defined(DR_MP3_ONLY_SIMD) && (defined(__ARM_NEON) || defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)) |
| 664 |
#define DR_MP3_ONLY_SIMD |
| 665 |
#endif |
| 666 |
|
| 667 |
#if ((defined(_MSC_VER) && _MSC_VER >= 1400) && defined(_M_X64)) || ((defined(__i386) || defined(_M_IX86) || defined(__i386__) || defined(__x86_64__)) && ((defined(_M_IX86_FP) && _M_IX86_FP == 2) || defined(__SSE2__))) |
| 668 |
#if defined(_MSC_VER) |
| 669 |
#include <intrin.h> |
| 670 |
#endif |
| 671 |
#include <emmintrin.h> |
| 672 |
#define DRMP3_HAVE_SSE 1 |
| 673 |
#define DRMP3_HAVE_SIMD 1 |
| 674 |
#define DRMP3_VSTORE _mm_storeu_ps |
| 675 |
#define DRMP3_VLD _mm_loadu_ps |
| 676 |
#define DRMP3_VSET _mm_set1_ps |
| 677 |
#define DRMP3_VADD _mm_add_ps |
| 678 |
#define DRMP3_VSUB _mm_sub_ps |
| 679 |
#define DRMP3_VMUL _mm_mul_ps |
| 680 |
#define DRMP3_VMAC(a, x, y) _mm_add_ps(a, _mm_mul_ps(x, y)) |
| 681 |
#define DRMP3_VMSB(a, x, y) _mm_sub_ps(a, _mm_mul_ps(x, y)) |
| 682 |
#define DRMP3_VMUL_S(x, s) _mm_mul_ps(x, _mm_set1_ps(s)) |
| 683 |
#define DRMP3_VREV(x) _mm_shuffle_ps(x, x, _MM_SHUFFLE(0, 1, 2, 3)) |
| 684 |
typedef __m128 drmp3_f4; |
| 685 |
#if (defined(_MSC_VER) || defined(DR_MP3_ONLY_SIMD)) && !defined(__clang__) |
| 686 |
#define drmp3_cpuid __cpuid |
| 687 |
#else |
| 688 |
static __inline__ __attribute__((always_inline)) void drmp3_cpuid(int CPUInfo[], const int InfoType) |
| 689 |
{ |
| 690 |
#if defined(__PIC__) |
| 691 |
__asm__ __volatile__( |
| 692 |
#if defined(__x86_64__) |
| 693 |
"push %%rbx\n" |
| 694 |
"cpuid\n" |
| 695 |
"xchgl %%ebx, %1\n" |
| 696 |
"pop %%rbx\n" |
| 697 |
#else |
| 698 |
"xchgl %%ebx, %1\n" |
| 699 |
"cpuid\n" |
| 700 |
"xchgl %%ebx, %1\n" |
| 701 |
#endif |
| 702 |
: "=a" (CPUInfo[0]), "=r" (CPUInfo[1]), "=c" (CPUInfo[2]), "=d" (CPUInfo[3]) |
| 703 |
: "a" (InfoType)); |
| 704 |
#else |
| 705 |
__asm__ __volatile__( |
| 706 |
"cpuid" |
| 707 |
: "=a" (CPUInfo[0]), "=b" (CPUInfo[1]), "=c" (CPUInfo[2]), "=d" (CPUInfo[3]) |
| 708 |
: "a" (InfoType)); |
| 709 |
#endif |
| 710 |
} |
| 711 |
#endif |
| 712 |
static int drmp3_have_simd(void) |
| 713 |
{ |
| 714 |
#ifdef DR_MP3_ONLY_SIMD |
| 715 |
return 1; |
| 716 |
#else |
| 717 |
static int g_have_simd; |
| 718 |
int CPUInfo[4]; |
| 719 |
#ifdef MINIMP3_TEST |
| 720 |
static int g_counter; |
| 721 |
if (g_counter++ > 100) |
| 722 |
return 0; |
| 723 |
#endif |
| 724 |
if (g_have_simd) |
| 725 |
goto end; |
| 726 |
drmp3_cpuid(CPUInfo, 0); |
| 727 |
if (CPUInfo[0] > 0) |
| 728 |
{ |
| 729 |
drmp3_cpuid(CPUInfo, 1); |
| 730 |
g_have_simd = (CPUInfo[3] & (1 << 26)) + 1; |
| 731 |
return g_have_simd - 1; |
| 732 |
} |
| 733 |
|
| 734 |
end: |
| 735 |
return g_have_simd - 1; |
| 736 |
#endif |
| 737 |
} |
| 738 |
#elif defined(__ARM_NEON) || defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC) |
| 739 |
#include <arm_neon.h> |
| 740 |
#define DRMP3_HAVE_SSE 0 |
| 741 |
#define DRMP3_HAVE_SIMD 1 |
| 742 |
#define DRMP3_VSTORE vst1q_f32 |
| 743 |
#define DRMP3_VLD vld1q_f32 |
| 744 |
#define DRMP3_VSET vmovq_n_f32 |
| 745 |
#define DRMP3_VADD vaddq_f32 |
| 746 |
#define DRMP3_VSUB vsubq_f32 |
| 747 |
#define DRMP3_VMUL vmulq_f32 |
| 748 |
#define DRMP3_VMAC(a, x, y) vmlaq_f32(a, x, y) |
| 749 |
#define DRMP3_VMSB(a, x, y) vmlsq_f32(a, x, y) |
| 750 |
#define DRMP3_VMUL_S(x, s) vmulq_f32(x, vmovq_n_f32(s)) |
| 751 |
#define DRMP3_VREV(x) vcombine_f32(vget_high_f32(vrev64q_f32(x)), vget_low_f32(vrev64q_f32(x))) |
| 752 |
typedef float32x4_t drmp3_f4; |
| 753 |
static int drmp3_have_simd(void) |
| 754 |
{ |
| 755 |
return 1; |
| 756 |
} |
| 757 |
#else |
| 758 |
#define DRMP3_HAVE_SSE 0 |
| 759 |
#define DRMP3_HAVE_SIMD 0 |
| 760 |
#ifdef DR_MP3_ONLY_SIMD |
| 761 |
#error DR_MP3_ONLY_SIMD used, but SSE/NEON not enabled |
| 762 |
#endif |
| 763 |
#endif |
| 764 |
|
| 765 |
#else |
| 766 |
|
| 767 |
#define DRMP3_HAVE_SIMD 0 |
| 768 |
|
| 769 |
#endif |
| 770 |
|
| 771 |
#if defined(__ARM_ARCH) && (__ARM_ARCH >= 6) && !defined(__aarch64__) && !defined(_M_ARM64) && !defined(_M_ARM64EC) && !defined(__ARM_ARCH_6M__) |
| 772 |
#define DRMP3_HAVE_ARMV6 1 |
| 773 |
static __inline__ __attribute__((always_inline)) drmp3_int32 drmp3_clip_int16_arm(drmp3_int32 a) |
| 774 |
{ |
| 775 |
drmp3_int32 x = 0; |
| 776 |
__asm__ ("ssat %0, #16, %1" : "=r"(x) : "r"(a)); |
| 777 |
return x; |
| 778 |
} |
| 779 |
#else |
| 780 |
#define DRMP3_HAVE_ARMV6 0 |
| 781 |
#endif |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
#ifndef DRMP3_ASSERT |
| 786 |
#include <assert.h> |
| 787 |
#define DRMP3_ASSERT(expression) assert(expression) |
| 788 |
#endif |
| 789 |
#ifndef DRMP3_COPY_MEMORY |
| 790 |
#define DRMP3_COPY_MEMORY(dst, src, sz) memcpy((dst), (src), (sz)) |
| 791 |
#endif |
| 792 |
#ifndef DRMP3_MOVE_MEMORY |
| 793 |
#define DRMP3_MOVE_MEMORY(dst, src, sz) memmove((dst), (src), (sz)) |
| 794 |
#endif |
| 795 |
#ifndef DRMP3_ZERO_MEMORY |
| 796 |
#define DRMP3_ZERO_MEMORY(p, sz) memset((p), 0, (sz)) |
| 797 |
#endif |
| 798 |
#define DRMP3_ZERO_OBJECT(p) DRMP3_ZERO_MEMORY((p), sizeof(*(p))) |
| 799 |
#ifndef DRMP3_MALLOC |
| 800 |
#define DRMP3_MALLOC(sz) malloc((sz)) |
| 801 |
#endif |
| 802 |
#ifndef DRMP3_REALLOC |
| 803 |
#define DRMP3_REALLOC(p, sz) realloc((p), (sz)) |
| 804 |
#endif |
| 805 |
#ifndef DRMP3_FREE |
| 806 |
#define DRMP3_FREE(p) free((p)) |
| 807 |
#endif |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
typedef struct |
| 812 |
{ |
| 813 |
float scf[3*64]; |
| 814 |
drmp3_uint8 total_bands, stereo_bands, bitalloc[64], scfcod[64]; |
| 815 |
} drmp3_L12_scale_info; |
| 816 |
|
| 817 |
typedef struct |
| 818 |
{ |
| 819 |
drmp3_uint8 tab_offset, code_tab_width, band_count; |
| 820 |
} drmp3_L12_subband_alloc; |
| 821 |
|
| 822 |
static void drmp3_bs_init(drmp3_bs *bs, const drmp3_uint8 *data, int bytes) |
| 823 |
{ |
| 824 |
bs->buf = data; |
| 825 |
bs->pos = 0; |
| 826 |
bs->limit = bytes*8; |
| 827 |
} |
| 828 |
|
| 829 |
static drmp3_uint32 drmp3_bs_get_bits(drmp3_bs *bs, int n) |
| 830 |
{ |
| 831 |
drmp3_uint32 next, cache = 0, s = bs->pos & 7; |
| 832 |
int shl = n + s; |
| 833 |
const drmp3_uint8 *p = bs->buf + (bs->pos >> 3); |
| 834 |
if ((bs->pos += n) > bs->limit) |
| 835 |
return 0; |
| 836 |
next = *p++ & (255 >> s); |
| 837 |
while ((shl -= 8) > 0) |
| 838 |
{ |
| 839 |
cache |= next << shl; |
| 840 |
next = *p++; |
| 841 |
} |
| 842 |
return cache | (next >> -shl); |
| 843 |
} |
| 844 |
|
| 845 |
static int drmp3_hdr_valid(const drmp3_uint8 *h) |
| 846 |
{ |
| 847 |
return h[0] == 0xff && |
| 848 |
((h[1] & 0xF0) == 0xf0 || (h[1] & 0xFE) == 0xe2) && |
| 849 |
(DRMP3_HDR_GET_LAYER(h) != 0) && |
| 850 |
(DRMP3_HDR_GET_BITRATE(h) != 15) && |
| 851 |
(DRMP3_HDR_GET_SAMPLE_RATE(h) != 3); |
| 852 |
} |
| 853 |
|
| 854 |
static int drmp3_hdr_compare(const drmp3_uint8 *h1, const drmp3_uint8 *h2) |
| 855 |
{ |
| 856 |
return drmp3_hdr_valid(h2) && |
| 857 |
((h1[1] ^ h2[1]) & 0xFE) == 0 && |
| 858 |
((h1[2] ^ h2[2]) & 0x0C) == 0 && |
| 859 |
!(DRMP3_HDR_IS_FREE_FORMAT(h1) ^ DRMP3_HDR_IS_FREE_FORMAT(h2)); |
| 860 |
} |
| 861 |
|
| 862 |
static unsigned drmp3_hdr_bitrate_kbps(const drmp3_uint8 *h) |
| 863 |
{ |
| 864 |
static const drmp3_uint8 halfrate[2][3][15] = { |
| 865 |
{ { 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 } }, |
| 866 |
{ { 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 } }, |
| 867 |
}; |
| 868 |
return 2*halfrate[!!DRMP3_HDR_TEST_MPEG1(h)][DRMP3_HDR_GET_LAYER(h) - 1][DRMP3_HDR_GET_BITRATE(h)]; |
| 869 |
} |
| 870 |
|
| 871 |
static unsigned drmp3_hdr_sample_rate_hz(const drmp3_uint8 *h) |
| 872 |
{ |
| 873 |
static const unsigned g_hz[3] = { 44100, 48000, 32000 }; |
| 874 |
return g_hz[DRMP3_HDR_GET_SAMPLE_RATE(h)] >> (int)!DRMP3_HDR_TEST_MPEG1(h) >> (int)!DRMP3_HDR_TEST_NOT_MPEG25(h); |
| 875 |
} |
| 876 |
|
| 877 |
static unsigned drmp3_hdr_frame_samples(const drmp3_uint8 *h) |
| 878 |
{ |
| 879 |
return DRMP3_HDR_IS_LAYER_1(h) ? 384 : (1152 >> (int)DRMP3_HDR_IS_FRAME_576(h)); |
| 880 |
} |
| 881 |
|
| 882 |
static int drmp3_hdr_frame_bytes(const drmp3_uint8 *h, int free_format_size) |
| 883 |
{ |
| 884 |
int frame_bytes = drmp3_hdr_frame_samples(h)*drmp3_hdr_bitrate_kbps(h)*125/drmp3_hdr_sample_rate_hz(h); |
| 885 |
if (DRMP3_HDR_IS_LAYER_1(h)) |
| 886 |
{ |
| 887 |
frame_bytes &= ~3; |
| 888 |
} |
| 889 |
return frame_bytes ? frame_bytes : free_format_size; |
| 890 |
} |
| 891 |
|
| 892 |
static int drmp3_hdr_padding(const drmp3_uint8 *h) |
| 893 |
{ |
| 894 |
return DRMP3_HDR_TEST_PADDING(h) ? (DRMP3_HDR_IS_LAYER_1(h) ? 4 : 1) : 0; |
| 895 |
} |
| 896 |
|
| 897 |
#ifndef DR_MP3_ONLY_MP3 |
| 898 |
static const drmp3_L12_subband_alloc *drmp3_L12_subband_alloc_table(const drmp3_uint8 *hdr, drmp3_L12_scale_info *sci) |
| 899 |
{ |
| 900 |
const drmp3_L12_subband_alloc *alloc; |
| 901 |
int mode = DRMP3_HDR_GET_STEREO_MODE(hdr); |
| 902 |
int nbands, stereo_bands = (mode == DRMP3_MODE_MONO) ? 0 : (mode == DRMP3_MODE_JOINT_STEREO) ? (DRMP3_HDR_GET_STEREO_MODE_EXT(hdr) << 2) + 4 : 32; |
| 903 |
|
| 904 |
if (DRMP3_HDR_IS_LAYER_1(hdr)) |
| 905 |
{ |
| 906 |
static const drmp3_L12_subband_alloc g_alloc_L1[] = { { 76, 4, 32 } }; |
| 907 |
alloc = g_alloc_L1; |
| 908 |
nbands = 32; |
| 909 |
} else if (!DRMP3_HDR_TEST_MPEG1(hdr)) |
| 910 |
{ |
| 911 |
static const drmp3_L12_subband_alloc g_alloc_L2M2[] = { { 60, 4, 4 }, { 44, 3, 7 }, { 44, 2, 19 } }; |
| 912 |
alloc = g_alloc_L2M2; |
| 913 |
nbands = 30; |
| 914 |
} else |
| 915 |
{ |
| 916 |
static const drmp3_L12_subband_alloc g_alloc_L2M1[] = { { 0, 4, 3 }, { 16, 4, 8 }, { 32, 3, 12 }, { 40, 2, 7 } }; |
| 917 |
int sample_rate_idx = DRMP3_HDR_GET_SAMPLE_RATE(hdr); |
| 918 |
unsigned kbps = drmp3_hdr_bitrate_kbps(hdr) >> (int)(mode != DRMP3_MODE_MONO); |
| 919 |
if (!kbps) |
| 920 |
{ |
| 921 |
kbps = 192; |
| 922 |
} |
| 923 |
|
| 924 |
alloc = g_alloc_L2M1; |
| 925 |
nbands = 27; |
| 926 |
if (kbps < 56) |
| 927 |
{ |
| 928 |
static const drmp3_L12_subband_alloc g_alloc_L2M1_lowrate[] = { { 44, 4, 2 }, { 44, 3, 10 } }; |
| 929 |
alloc = g_alloc_L2M1_lowrate; |
| 930 |
nbands = sample_rate_idx == 2 ? 12 : 8; |
| 931 |
} else if (kbps >= 96 && sample_rate_idx != 1) |
| 932 |
{ |
| 933 |
nbands = 30; |
| 934 |
} |
| 935 |
} |
| 936 |
|
| 937 |
sci->total_bands = (drmp3_uint8)nbands; |
| 938 |
sci->stereo_bands = (drmp3_uint8)DRMP3_MIN(stereo_bands, nbands); |
| 939 |
|
| 940 |
return alloc; |
| 941 |
} |
| 942 |
|
| 943 |
static void drmp3_L12_read_scalefactors(drmp3_bs *bs, drmp3_uint8 *pba, drmp3_uint8 *scfcod, int bands, float *scf) |
| 944 |
{ |
| 945 |
static const float g_deq_L12[18*3] = { |
| 946 |
#define DRMP3_DQ(x) 9.53674316e-07f/x, 7.56931807e-07f/x, 6.00777173e-07f/x |
| 947 |
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) |
| 948 |
}; |
| 949 |
int i, m; |
| 950 |
for (i = 0; i < bands; i++) |
| 951 |
{ |
| 952 |
float s = 0; |
| 953 |
int ba = *pba++; |
| 954 |
int mask = ba ? 4 + ((19 >> scfcod[i]) & 3) : 0; |
| 955 |
for (m = 4; m; m >>= 1) |
| 956 |
{ |
| 957 |
if (mask & m) |
| 958 |
{ |
| 959 |
int b = drmp3_bs_get_bits(bs, 6); |
| 960 |
s = g_deq_L12[ba*3 - 6 + b % 3]*(int)(1 << 21 >> b/3); |
| 961 |
} |
| 962 |
*scf++ = s; |
| 963 |
} |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
static void drmp3_L12_read_scale_info(const drmp3_uint8 *hdr, drmp3_bs *bs, drmp3_L12_scale_info *sci) |
| 968 |
{ |
| 969 |
static const drmp3_uint8 g_bitalloc_code_tab[] = { |
| 970 |
0,17, 3, 4, 5,6,7, 8,9,10,11,12,13,14,15,16, |
| 971 |
0,17,18, 3,19,4,5, 6,7, 8, 9,10,11,12,13,16, |
| 972 |
0,17,18, 3,19,4,5,16, |
| 973 |
0,17,18,16, |
| 974 |
0,17,18,19, 4,5,6, 7,8, 9,10,11,12,13,14,15, |
| 975 |
0,17,18, 3,19,4,5, 6,7, 8, 9,10,11,12,13,14, |
| 976 |
0, 2, 3, 4, 5,6,7, 8,9,10,11,12,13,14,15,16 |
| 977 |
}; |
| 978 |
const drmp3_L12_subband_alloc *subband_alloc = drmp3_L12_subband_alloc_table(hdr, sci); |
| 979 |
|
| 980 |
int i, k = 0, ba_bits = 0; |
| 981 |
const drmp3_uint8 *ba_code_tab = g_bitalloc_code_tab; |
| 982 |
|
| 983 |
for (i = 0; i < sci->total_bands; i++) |
| 984 |
{ |
| 985 |
drmp3_uint8 ba; |
| 986 |
if (i == k) |
| 987 |
{ |
| 988 |
k += subband_alloc->band_count; |
| 989 |
ba_bits = subband_alloc->code_tab_width; |
| 990 |
ba_code_tab = g_bitalloc_code_tab + subband_alloc->tab_offset; |
| 991 |
subband_alloc++; |
| 992 |
} |
| 993 |
ba = ba_code_tab[drmp3_bs_get_bits(bs, ba_bits)]; |
| 994 |
sci->bitalloc[2*i] = ba; |
| 995 |
if (i < sci->stereo_bands) |
| 996 |
{ |
| 997 |
ba = ba_code_tab[drmp3_bs_get_bits(bs, ba_bits)]; |
| 998 |
} |
| 999 |
sci->bitalloc[2*i + 1] = sci->stereo_bands ? ba : 0; |
| 1000 |
} |
| 1001 |
|
| 1002 |
for (i = 0; i < 2*sci->total_bands; i++) |
| 1003 |
{ |
| 1004 |
sci->scfcod[i] = (drmp3_uint8)(sci->bitalloc[i] ? DRMP3_HDR_IS_LAYER_1(hdr) ? 2 : drmp3_bs_get_bits(bs, 2) : 6); |
| 1005 |
} |
| 1006 |
|
| 1007 |
drmp3_L12_read_scalefactors(bs, sci->bitalloc, sci->scfcod, sci->total_bands*2, sci->scf); |
| 1008 |
|
| 1009 |
for (i = sci->stereo_bands; i < sci->total_bands; i++) |
| 1010 |
{ |
| 1011 |
sci->bitalloc[2*i + 1] = 0; |
| 1012 |
} |
| 1013 |
} |
| 1014 |
|
| 1015 |
static int drmp3_L12_dequantize_granule(float *grbuf, drmp3_bs *bs, drmp3_L12_scale_info *sci, int group_size) |
| 1016 |
{ |
| 1017 |
int i, j, k, choff = 576; |
| 1018 |
for (j = 0; j < 4; j++) |
| 1019 |
{ |
| 1020 |
float *dst = grbuf + group_size*j; |
| 1021 |
for (i = 0; i < 2*sci->total_bands; i++) |
| 1022 |
{ |
| 1023 |
int ba = sci->bitalloc[i]; |
| 1024 |
if (ba != 0) |
| 1025 |
{ |
| 1026 |
if (ba < 17) |
| 1027 |
{ |
| 1028 |
int half = (1 << (ba - 1)) - 1; |
| 1029 |
for (k = 0; k < group_size; k++) |
| 1030 |
{ |
| 1031 |
dst[k] = (float)((int)drmp3_bs_get_bits(bs, ba) - half); |
| 1032 |
} |
| 1033 |
} else |
| 1034 |
{ |
| 1035 |
unsigned mod = (2 << (ba - 17)) + 1; |
| 1036 |
unsigned code = drmp3_bs_get_bits(bs, mod + 2 - (mod >> 3)); |
| 1037 |
for (k = 0; k < group_size; k++, code /= mod) |
| 1038 |
{ |
| 1039 |
dst[k] = (float)((int)(code % mod - mod/2)); |
| 1040 |
} |
| 1041 |
} |
| 1042 |
} |
| 1043 |
dst += choff; |
| 1044 |
choff = 18 - choff; |
| 1045 |
} |
| 1046 |
} |
| 1047 |
return group_size*4; |
| 1048 |
} |
| 1049 |
|
| 1050 |
static void drmp3_L12_apply_scf_384(drmp3_L12_scale_info *sci, const float *scf, float *dst) |
| 1051 |
{ |
| 1052 |
int i, k; |
| 1053 |
DRMP3_COPY_MEMORY(dst + 576 + sci->stereo_bands*18, dst + sci->stereo_bands*18, (sci->total_bands - sci->stereo_bands)*18*sizeof(float)); |
| 1054 |
for (i = 0; i < sci->total_bands; i++, dst += 18, scf += 6) |
| 1055 |
{ |
| 1056 |
for (k = 0; k < 12; k++) |
| 1057 |
{ |
| 1058 |
dst[k + 0] *= scf[0]; |
| 1059 |
dst[k + 576] *= scf[3]; |
| 1060 |
} |
| 1061 |
} |
| 1062 |
} |
| 1063 |
#endif |
| 1064 |
|
| 1065 |
static int drmp3_L3_read_side_info(drmp3_bs *bs, drmp3_L3_gr_info *gr, const drmp3_uint8 *hdr) |
| 1066 |
{ |
| 1067 |
static const drmp3_uint8 g_scf_long[8][23] = { |
| 1068 |
{ 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 }, |
| 1069 |
{ 12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2,0 }, |
| 1070 |
{ 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 }, |
| 1071 |
{ 6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,54,62,70,76,36,0 }, |
| 1072 |
{ 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 }, |
| 1073 |
{ 4,4,4,4,4,4,6,6,8,8,10,12,16,20,24,28,34,42,50,54,76,158,0 }, |
| 1074 |
{ 4,4,4,4,4,4,6,6,6,8,10,12,16,18,22,28,34,40,46,54,54,192,0 }, |
| 1075 |
{ 4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102,26,0 } |
| 1076 |
}; |
| 1077 |
static const drmp3_uint8 g_scf_short[8][40] = { |
| 1078 |
{ 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 }, |
| 1079 |
{ 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 }, |
| 1080 |
{ 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 }, |
| 1081 |
{ 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 }, |
| 1082 |
{ 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 }, |
| 1083 |
{ 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 }, |
| 1084 |
{ 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 }, |
| 1085 |
{ 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 } |
| 1086 |
}; |
| 1087 |
static const drmp3_uint8 g_scf_mixed[8][40] = { |
| 1088 |
{ 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 }, |
| 1089 |
{ 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 }, |
| 1090 |
{ 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 }, |
| 1091 |
{ 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 }, |
| 1092 |
{ 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 }, |
| 1093 |
{ 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 }, |
| 1094 |
{ 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 }, |
| 1095 |
{ 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 } |
| 1096 |
}; |
| 1097 |
|
| 1098 |
unsigned tables, scfsi = 0; |
| 1099 |
int main_data_begin, part_23_sum = 0; |
| 1100 |
int gr_count = DRMP3_HDR_IS_MONO(hdr) ? 1 : 2; |
| 1101 |
int sr_idx = DRMP3_HDR_GET_MY_SAMPLE_RATE(hdr); sr_idx -= (sr_idx != 0); |
| 1102 |
|
| 1103 |
if (DRMP3_HDR_TEST_MPEG1(hdr)) |
| 1104 |
{ |
| 1105 |
gr_count *= 2; |
| 1106 |
main_data_begin = drmp3_bs_get_bits(bs, 9); |
| 1107 |
scfsi = drmp3_bs_get_bits(bs, 7 + gr_count); |
| 1108 |
} else |
| 1109 |
{ |
| 1110 |
main_data_begin = drmp3_bs_get_bits(bs, 8 + gr_count) >> gr_count; |
| 1111 |
} |
| 1112 |
|
| 1113 |
do |
| 1114 |
{ |
| 1115 |
if (DRMP3_HDR_IS_MONO(hdr)) |
| 1116 |
{ |
| 1117 |
scfsi <<= 4; |
| 1118 |
} |
| 1119 |
gr->part_23_length = (drmp3_uint16)drmp3_bs_get_bits(bs, 12); |
| 1120 |
part_23_sum += gr->part_23_length; |
| 1121 |
gr->big_values = (drmp3_uint16)drmp3_bs_get_bits(bs, 9); |
| 1122 |
if (gr->big_values > 288) |
| 1123 |
{ |
| 1124 |
return -1; |
| 1125 |
} |
| 1126 |
gr->global_gain = (drmp3_uint8)drmp3_bs_get_bits(bs, 8); |
| 1127 |
gr->scalefac_compress = (drmp3_uint16)drmp3_bs_get_bits(bs, DRMP3_HDR_TEST_MPEG1(hdr) ? 4 : 9); |
| 1128 |
gr->sfbtab = g_scf_long[sr_idx]; |
| 1129 |
gr->n_long_sfb = 22; |
| 1130 |
gr->n_short_sfb = 0; |
| 1131 |
if (drmp3_bs_get_bits(bs, 1)) |
| 1132 |
{ |
| 1133 |
gr->block_type = (drmp3_uint8)drmp3_bs_get_bits(bs, 2); |
| 1134 |
if (!gr->block_type) |
| 1135 |
{ |
| 1136 |
return -1; |
| 1137 |
} |
| 1138 |
gr->mixed_block_flag = (drmp3_uint8)drmp3_bs_get_bits(bs, 1); |
| 1139 |
gr->region_count[0] = 7; |
| 1140 |
gr->region_count[1] = 255; |
| 1141 |
if (gr->block_type == DRMP3_SHORT_BLOCK_TYPE) |
| 1142 |
{ |
| 1143 |
scfsi &= 0x0F0F; |
| 1144 |
if (!gr->mixed_block_flag) |
| 1145 |
{ |
| 1146 |
gr->region_count[0] = 8; |
| 1147 |
gr->sfbtab = g_scf_short[sr_idx]; |
| 1148 |
gr->n_long_sfb = 0; |
| 1149 |
gr->n_short_sfb = 39; |
| 1150 |
} else |
| 1151 |
{ |
| 1152 |
gr->sfbtab = g_scf_mixed[sr_idx]; |
| 1153 |
gr->n_long_sfb = DRMP3_HDR_TEST_MPEG1(hdr) ? 8 : 6; |
| 1154 |
gr->n_short_sfb = 30; |
| 1155 |
} |
| 1156 |
} |
| 1157 |
tables = drmp3_bs_get_bits(bs, 10); |
| 1158 |
tables <<= 5; |
| 1159 |
gr->subblock_gain[0] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3); |
| 1160 |
gr->subblock_gain[1] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3); |
| 1161 |
gr->subblock_gain[2] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3); |
| 1162 |
} else |
| 1163 |
{ |
| 1164 |
gr->block_type = 0; |
| 1165 |
gr->mixed_block_flag = 0; |
| 1166 |
tables = drmp3_bs_get_bits(bs, 15); |
| 1167 |
gr->region_count[0] = (drmp3_uint8)drmp3_bs_get_bits(bs, 4); |
| 1168 |
gr->region_count[1] = (drmp3_uint8)drmp3_bs_get_bits(bs, 3); |
| 1169 |
gr->region_count[2] = 255; |
| 1170 |
} |
| 1171 |
gr->table_select[0] = (drmp3_uint8)(tables >> 10); |
| 1172 |
gr->table_select[1] = (drmp3_uint8)((tables >> 5) & 31); |
| 1173 |
gr->table_select[2] = (drmp3_uint8)((tables) & 31); |
| 1174 |
gr->preflag = (drmp3_uint8)(DRMP3_HDR_TEST_MPEG1(hdr) ? drmp3_bs_get_bits(bs, 1) : (gr->scalefac_compress >= 500)); |
| 1175 |
gr->scalefac_scale = (drmp3_uint8)drmp3_bs_get_bits(bs, 1); |
| 1176 |
gr->count1_table = (drmp3_uint8)drmp3_bs_get_bits(bs, 1); |
| 1177 |
gr->scfsi = (drmp3_uint8)((scfsi >> 12) & 15); |
| 1178 |
scfsi <<= 4; |
| 1179 |
gr++; |
| 1180 |
} while(--gr_count); |
| 1181 |
|
| 1182 |
if (part_23_sum + bs->pos > bs->limit + main_data_begin*8) |
| 1183 |
{ |
| 1184 |
return -1; |
| 1185 |
} |
| 1186 |
|
| 1187 |
return main_data_begin; |
| 1188 |
} |
| 1189 |
|
| 1190 |
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) |
| 1191 |
{ |
| 1192 |
int i, k; |
| 1193 |
for (i = 0; i < 4 && scf_count[i]; i++, scfsi *= 2) |
| 1194 |
{ |
| 1195 |
int cnt = scf_count[i]; |
| 1196 |
if (scfsi & 8) |
| 1197 |
{ |
| 1198 |
DRMP3_COPY_MEMORY(scf, ist_pos, cnt); |
| 1199 |
} else |
| 1200 |
{ |
| 1201 |
int bits = scf_size[i]; |
| 1202 |
if (!bits) |
| 1203 |
{ |
| 1204 |
DRMP3_ZERO_MEMORY(scf, cnt); |
| 1205 |
DRMP3_ZERO_MEMORY(ist_pos, cnt); |
| 1206 |
} else |
| 1207 |
{ |
| 1208 |
int max_scf = (scfsi < 0) ? (1 << bits) - 1 : -1; |
| 1209 |
for (k = 0; k < cnt; k++) |
| 1210 |
{ |
| 1211 |
int s = drmp3_bs_get_bits(bitbuf, bits); |
| 1212 |
ist_pos[k] = (drmp3_uint8)(s == max_scf ? -1 : s); |
| 1213 |
scf[k] = (drmp3_uint8)s; |
| 1214 |
} |
| 1215 |
} |
| 1216 |
} |
| 1217 |
ist_pos += cnt; |
| 1218 |
scf += cnt; |
| 1219 |
} |
| 1220 |
scf[0] = scf[1] = scf[2] = 0; |
| 1221 |
} |
| 1222 |
|
| 1223 |
static float drmp3_L3_ldexp_q2(float y, int exp_q2) |
| 1224 |
{ |
| 1225 |
static const float g_expfrac[4] = { 9.31322575e-10f,7.83145814e-10f,6.58544508e-10f,5.53767716e-10f }; |
| 1226 |
int e; |
| 1227 |
do |
| 1228 |
{ |
| 1229 |
e = DRMP3_MIN(30*4, exp_q2); |
| 1230 |
y *= g_expfrac[e & 3]*(1 << 30 >> (e >> 2)); |
| 1231 |
} while ((exp_q2 -= e) > 0); |
| 1232 |
return y; |
| 1233 |
} |
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
#if (defined(__GNUC__) && (__GNUC__ >= 13)) && !defined(__clang__) |
| 1240 |
#pragma GCC diagnostic push |
| 1241 |
#pragma GCC diagnostic ignored "-Wstringop-overflow" |
| 1242 |
#endif |
| 1243 |
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) |
| 1244 |
{ |
| 1245 |
static const drmp3_uint8 g_scf_partitions[3][28] = { |
| 1246 |
{ 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 }, |
| 1247 |
{ 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 }, |
| 1248 |
{ 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 } |
| 1249 |
}; |
| 1250 |
const drmp3_uint8 *scf_partition = g_scf_partitions[!!gr->n_short_sfb + !gr->n_long_sfb]; |
| 1251 |
drmp3_uint8 scf_size[4], iscf[40]; |
| 1252 |
int i, scf_shift = gr->scalefac_scale + 1, gain_exp, scfsi = gr->scfsi; |
| 1253 |
float gain; |
| 1254 |
|
| 1255 |
if (DRMP3_HDR_TEST_MPEG1(hdr)) |
| 1256 |
{ |
| 1257 |
static const drmp3_uint8 g_scfc_decode[16] = { 0,1,2,3, 12,5,6,7, 9,10,11,13, 14,15,18,19 }; |
| 1258 |
int part = g_scfc_decode[gr->scalefac_compress]; |
| 1259 |
scf_size[1] = scf_size[0] = (drmp3_uint8)(part >> 2); |
| 1260 |
scf_size[3] = scf_size[2] = (drmp3_uint8)(part & 3); |
| 1261 |
} else |
| 1262 |
{ |
| 1263 |
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 }; |
| 1264 |
int k, modprod, sfc, ist = DRMP3_HDR_TEST_I_STEREO(hdr) && ch; |
| 1265 |
sfc = gr->scalefac_compress >> ist; |
| 1266 |
for (k = ist*3*4; sfc >= 0; sfc -= modprod, k += 4) |
| 1267 |
{ |
| 1268 |
for (modprod = 1, i = 3; i >= 0; i--) |
| 1269 |
{ |
| 1270 |
scf_size[i] = (drmp3_uint8)(sfc / modprod % g_mod[k + i]); |
| 1271 |
modprod *= g_mod[k + i]; |
| 1272 |
} |
| 1273 |
} |
| 1274 |
scf_partition += k; |
| 1275 |
scfsi = -16; |
| 1276 |
} |
| 1277 |
drmp3_L3_read_scalefactors(iscf, ist_pos, scf_size, scf_partition, bs, scfsi); |
| 1278 |
|
| 1279 |
if (gr->n_short_sfb) |
| 1280 |
{ |
| 1281 |
int sh = 3 - scf_shift; |
| 1282 |
for (i = 0; i < gr->n_short_sfb; i += 3) |
| 1283 |
{ |
| 1284 |
iscf[gr->n_long_sfb + i + 0] = (drmp3_uint8)(iscf[gr->n_long_sfb + i + 0] + (gr->subblock_gain[0] << sh)); |
| 1285 |
iscf[gr->n_long_sfb + i + 1] = (drmp3_uint8)(iscf[gr->n_long_sfb + i + 1] + (gr->subblock_gain[1] << sh)); |
| 1286 |
iscf[gr->n_long_sfb + i + 2] = (drmp3_uint8)(iscf[gr->n_long_sfb + i + 2] + (gr->subblock_gain[2] << sh)); |
| 1287 |
} |
| 1288 |
} else if (gr->preflag) |
| 1289 |
{ |
| 1290 |
static const drmp3_uint8 g_preamp[10] = { 1,1,1,1,2,2,3,3,3,2 }; |
| 1291 |
for (i = 0; i < 10; i++) |
| 1292 |
{ |
| 1293 |
iscf[11 + i] = (drmp3_uint8)(iscf[11 + i] + g_preamp[i]); |
| 1294 |
} |
| 1295 |
} |
| 1296 |
|
| 1297 |
gain_exp = gr->global_gain + DRMP3_BITS_DEQUANTIZER_OUT*4 - 210 - (DRMP3_HDR_IS_MS_STEREO(hdr) ? 2 : 0); |
| 1298 |
gain = drmp3_L3_ldexp_q2(1 << (DRMP3_MAX_SCFI/4), DRMP3_MAX_SCFI - gain_exp); |
| 1299 |
for (i = 0; i < (int)(gr->n_long_sfb + gr->n_short_sfb); i++) |
| 1300 |
{ |
| 1301 |
scf[i] = drmp3_L3_ldexp_q2(gain, iscf[i] << scf_shift); |
| 1302 |
} |
| 1303 |
} |
| 1304 |
#if (defined(__GNUC__) && (__GNUC__ >= 13)) && !defined(__clang__) |
| 1305 |
#pragma GCC diagnostic pop |
| 1306 |
#endif |
| 1307 |
|
| 1308 |
static const float g_drmp3_pow43[129 + 16] = { |
| 1309 |
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, |
| 1310 |
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 |
| 1311 |
}; |
| 1312 |
|
| 1313 |
static float drmp3_L3_pow_43(int x) |
| 1314 |
{ |
| 1315 |
float frac; |
| 1316 |
int sign, mult = 256; |
| 1317 |
|
| 1318 |
if (x < 129) |
| 1319 |
{ |
| 1320 |
return g_drmp3_pow43[16 + x]; |
| 1321 |
} |
| 1322 |
|
| 1323 |
if (x < 1024) |
| 1324 |
{ |
| 1325 |
mult = 16; |
| 1326 |
x <<= 3; |
| 1327 |
} |
| 1328 |
|
| 1329 |
sign = 2*x & 64; |
| 1330 |
frac = (float)((x & 63) - sign) / ((x & ~63) + sign); |
| 1331 |
return g_drmp3_pow43[16 + ((x + sign) >> 6)]*(1.f + frac*((4.f/3) + frac*(2.f/9)))*mult; |
| 1332 |
} |
| 1333 |
|
| 1334 |
static void drmp3_L3_huffman(float *dst, drmp3_bs *bs, const drmp3_L3_gr_info *gr_info, const float *scf, int layer3gr_limit) |
| 1335 |
{ |
| 1336 |
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, |
| 1337 |
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, |
| 1338 |
-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, |
| 1339 |
-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, |
| 1340 |
-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, |
| 1341 |
-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, |
| 1342 |
-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, |
| 1343 |
-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, |
| 1344 |
-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, |
| 1345 |
-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, |
| 1346 |
-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, |
| 1347 |
-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, |
| 1348 |
-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, |
| 1349 |
-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, |
| 1350 |
-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, |
| 1351 |
-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 }; |
| 1352 |
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}; |
| 1353 |
static const drmp3_uint8 tab33[] = { 252,236,220,204,188,172,156,140,124,108,92,76,60,44,28,12 }; |
| 1354 |
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 }; |
| 1355 |
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 }; |
| 1356 |
|
| 1357 |
#define DRMP3_PEEK_BITS(n) (bs_cache >> (32 - (n))) |
| 1358 |
#define DRMP3_FLUSH_BITS(n) { bs_cache <<= (n); bs_sh += (n); } |
| 1359 |
#define DRMP3_CHECK_BITS while (bs_sh >= 0) { bs_cache |= (drmp3_uint32)*bs_next_ptr++ << bs_sh; bs_sh -= 8; } |
| 1360 |
#define DRMP3_BSPOS ((bs_next_ptr - bs->buf)*8 - 24 + bs_sh) |
| 1361 |
|
| 1362 |
float one = 0.0f; |
| 1363 |
int ireg = 0, big_val_cnt = gr_info->big_values; |
| 1364 |
const drmp3_uint8 *sfb = gr_info->sfbtab; |
| 1365 |
const drmp3_uint8 *bs_next_ptr = bs->buf + bs->pos/8; |
| 1366 |
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); |
| 1367 |
int pairs_to_decode, np, bs_sh = (bs->pos & 7) - 8; |
| 1368 |
bs_next_ptr += 4; |
| 1369 |
|
| 1370 |
while (big_val_cnt > 0) |
| 1371 |
{ |
| 1372 |
int tab_num = gr_info->table_select[ireg]; |
| 1373 |
int sfb_cnt = gr_info->region_count[ireg++]; |
| 1374 |
const drmp3_int16 *codebook = tabs + tabindex[tab_num]; |
| 1375 |
int linbits = g_linbits[tab_num]; |
| 1376 |
if (linbits) |
| 1377 |
{ |
| 1378 |
do |
| 1379 |
{ |
| 1380 |
np = *sfb++ / 2; |
| 1381 |
pairs_to_decode = DRMP3_MIN(big_val_cnt, np); |
| 1382 |
one = *scf++; |
| 1383 |
do |
| 1384 |
{ |
| 1385 |
int j, w = 5; |
| 1386 |
int leaf = codebook[DRMP3_PEEK_BITS(w)]; |
| 1387 |
while (leaf < 0) |
| 1388 |
{ |
| 1389 |
DRMP3_FLUSH_BITS(w); |
| 1390 |
w = leaf & 7; |
| 1391 |
leaf = codebook[DRMP3_PEEK_BITS(w) - (leaf >> 3)]; |
| 1392 |
} |
| 1393 |
DRMP3_FLUSH_BITS(leaf >> 8); |
| 1394 |
|
| 1395 |
for (j = 0; j < 2; j++, dst++, leaf >>= 4) |
| 1396 |
{ |
| 1397 |
int lsb = leaf & 0x0F; |
| 1398 |
if (lsb == 15) |
| 1399 |
{ |
| 1400 |
lsb += DRMP3_PEEK_BITS(linbits); |
| 1401 |
DRMP3_FLUSH_BITS(linbits); |
| 1402 |
DRMP3_CHECK_BITS; |
| 1403 |
*dst = one*drmp3_L3_pow_43(lsb)*((drmp3_int32)bs_cache < 0 ? -1: 1); |
| 1404 |
} else |
| 1405 |
{ |
| 1406 |
*dst = g_drmp3_pow43[16 + lsb - 16*(bs_cache >> 31)]*one; |
| 1407 |
} |
| 1408 |
DRMP3_FLUSH_BITS(lsb ? 1 : 0); |
| 1409 |
} |
| 1410 |
DRMP3_CHECK_BITS; |
| 1411 |
} while (--pairs_to_decode); |
| 1412 |
} while ((big_val_cnt -= np) > 0 && --sfb_cnt >= 0); |
| 1413 |
} else |
| 1414 |
{ |
| 1415 |
do |
| 1416 |
{ |
| 1417 |
np = *sfb++ / 2; |
| 1418 |
pairs_to_decode = DRMP3_MIN(big_val_cnt, np); |
| 1419 |
one = *scf++; |
| 1420 |
do |
| 1421 |
{ |
| 1422 |
int j, w = 5; |
| 1423 |
int leaf = codebook[DRMP3_PEEK_BITS(w)]; |
| 1424 |
while (leaf < 0) |
| 1425 |
{ |
| 1426 |
DRMP3_FLUSH_BITS(w); |
| 1427 |
w = leaf & 7; |
| 1428 |
leaf = codebook[DRMP3_PEEK_BITS(w) - (leaf >> 3)]; |
| 1429 |
} |
| 1430 |
DRMP3_FLUSH_BITS(leaf >> 8); |
| 1431 |
|
| 1432 |
for (j = 0; j < 2; j++, dst++, leaf >>= 4) |
| 1433 |
{ |
| 1434 |
int lsb = leaf & 0x0F; |
| 1435 |
*dst = g_drmp3_pow43[16 + lsb - 16*(bs_cache >> 31)]*one; |
| 1436 |
DRMP3_FLUSH_BITS(lsb ? 1 : 0); |
| 1437 |
} |
| 1438 |
DRMP3_CHECK_BITS; |
| 1439 |
} while (--pairs_to_decode); |
| 1440 |
} while ((big_val_cnt -= np) > 0 && --sfb_cnt >= 0); |
| 1441 |
} |
| 1442 |
} |
| 1443 |
|
| 1444 |
for (np = 1 - big_val_cnt;; dst += 4) |
| 1445 |
{ |
| 1446 |
const drmp3_uint8 *codebook_count1 = (gr_info->count1_table) ? tab33 : tab32; |
| 1447 |
int leaf = codebook_count1[DRMP3_PEEK_BITS(4)]; |
| 1448 |
if (!(leaf & 8)) |
| 1449 |
{ |
| 1450 |
leaf = codebook_count1[(leaf >> 3) + (bs_cache << 4 >> (32 - (leaf & 3)))]; |
| 1451 |
} |
| 1452 |
DRMP3_FLUSH_BITS(leaf & 7); |
| 1453 |
if (DRMP3_BSPOS > layer3gr_limit) |
| 1454 |
{ |
| 1455 |
break; |
| 1456 |
} |
| 1457 |
#define DRMP3_RELOAD_SCALEFACTOR if (!--np) { np = *sfb++/2; if (!np) break; one = *scf++; } |
| 1458 |
#define DRMP3_DEQ_COUNT1(s) if (leaf & (128 >> s)) { dst[s] = ((drmp3_int32)bs_cache < 0) ? -one : one; DRMP3_FLUSH_BITS(1) } |
| 1459 |
DRMP3_RELOAD_SCALEFACTOR; |
| 1460 |
DRMP3_DEQ_COUNT1(0); |
| 1461 |
DRMP3_DEQ_COUNT1(1); |
| 1462 |
DRMP3_RELOAD_SCALEFACTOR; |
| 1463 |
DRMP3_DEQ_COUNT1(2); |
| 1464 |
DRMP3_DEQ_COUNT1(3); |
| 1465 |
DRMP3_CHECK_BITS; |
| 1466 |
} |
| 1467 |
|
| 1468 |
bs->pos = layer3gr_limit; |
| 1469 |
} |
| 1470 |
|
| 1471 |
static void drmp3_L3_midside_stereo(float *left, int n) |
| 1472 |
{ |
| 1473 |
int i = 0; |
| 1474 |
float *right = left + 576; |
| 1475 |
#if DRMP3_HAVE_SIMD |
| 1476 |
if (drmp3_have_simd()) |
| 1477 |
{ |
| 1478 |
for (; i < n - 3; i += 4) |
| 1479 |
{ |
| 1480 |
drmp3_f4 vl = DRMP3_VLD(left + i); |
| 1481 |
drmp3_f4 vr = DRMP3_VLD(right + i); |
| 1482 |
DRMP3_VSTORE(left + i, DRMP3_VADD(vl, vr)); |
| 1483 |
DRMP3_VSTORE(right + i, DRMP3_VSUB(vl, vr)); |
| 1484 |
} |
| 1485 |
#ifdef __GNUC__ |
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
if (__builtin_constant_p(n % 4 == 0) && n % 4 == 0) |
| 1490 |
return; |
| 1491 |
#endif |
| 1492 |
} |
| 1493 |
#endif |
| 1494 |
for (; i < n; i++) |
| 1495 |
{ |
| 1496 |
float a = left[i]; |
| 1497 |
float b = right[i]; |
| 1498 |
left[i] = a + b; |
| 1499 |
right[i] = a - b; |
| 1500 |
} |
| 1501 |
} |
| 1502 |
|
| 1503 |
static void drmp3_L3_intensity_stereo_band(float *left, int n, float kl, float kr) |
| 1504 |
{ |
| 1505 |
int i; |
| 1506 |
for (i = 0; i < n; i++) |
| 1507 |
{ |
| 1508 |
left[i + 576] = left[i]*kr; |
| 1509 |
left[i] = left[i]*kl; |
| 1510 |
} |
| 1511 |
} |
| 1512 |
|
| 1513 |
static void drmp3_L3_stereo_top_band(const float *right, const drmp3_uint8 *sfb, int nbands, int max_band[3]) |
| 1514 |
{ |
| 1515 |
int i, k; |
| 1516 |
|
| 1517 |
max_band[0] = max_band[1] = max_band[2] = -1; |
| 1518 |
|
| 1519 |
for (i = 0; i < nbands; i++) |
| 1520 |
{ |
| 1521 |
for (k = 0; k < sfb[i]; k += 2) |
| 1522 |
{ |
| 1523 |
if (right[k] != 0 || right[k + 1] != 0) |
| 1524 |
{ |
| 1525 |
max_band[i % 3] = i; |
| 1526 |
break; |
| 1527 |
} |
| 1528 |
} |
| 1529 |
right += sfb[i]; |
| 1530 |
} |
| 1531 |
} |
| 1532 |
|
| 1533 |
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) |
| 1534 |
{ |
| 1535 |
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 }; |
| 1536 |
unsigned i, max_pos = DRMP3_HDR_TEST_MPEG1(hdr) ? 7 : 64; |
| 1537 |
|
| 1538 |
for (i = 0; sfb[i]; i++) |
| 1539 |
{ |
| 1540 |
unsigned ipos = ist_pos[i]; |
| 1541 |
if ((int)i > max_band[i % 3] && ipos < max_pos) |
| 1542 |
{ |
| 1543 |
float kl, kr, s = DRMP3_HDR_TEST_MS_STEREO(hdr) ? 1.41421356f : 1; |
| 1544 |
if (DRMP3_HDR_TEST_MPEG1(hdr)) |
| 1545 |
{ |
| 1546 |
kl = g_pan[2*ipos]; |
| 1547 |
kr = g_pan[2*ipos + 1]; |
| 1548 |
} else |
| 1549 |
{ |
| 1550 |
kl = 1; |
| 1551 |
kr = drmp3_L3_ldexp_q2(1, (ipos + 1) >> 1 << mpeg2_sh); |
| 1552 |
if (ipos & 1) |
| 1553 |
{ |
| 1554 |
kl = kr; |
| 1555 |
kr = 1; |
| 1556 |
} |
| 1557 |
} |
| 1558 |
drmp3_L3_intensity_stereo_band(left, sfb[i], kl*s, kr*s); |
| 1559 |
} else if (DRMP3_HDR_TEST_MS_STEREO(hdr)) |
| 1560 |
{ |
| 1561 |
drmp3_L3_midside_stereo(left, sfb[i]); |
| 1562 |
} |
| 1563 |
left += sfb[i]; |
| 1564 |
} |
| 1565 |
} |
| 1566 |
|
| 1567 |
static void drmp3_L3_intensity_stereo(float *left, drmp3_uint8 *ist_pos, const drmp3_L3_gr_info *gr, const drmp3_uint8 *hdr) |
| 1568 |
{ |
| 1569 |
int max_band[3], n_sfb = gr->n_long_sfb + gr->n_short_sfb; |
| 1570 |
int i, max_blocks = gr->n_short_sfb ? 3 : 1; |
| 1571 |
|
| 1572 |
drmp3_L3_stereo_top_band(left + 576, gr->sfbtab, n_sfb, max_band); |
| 1573 |
if (gr->n_long_sfb) |
| 1574 |
{ |
| 1575 |
max_band[0] = max_band[1] = max_band[2] = DRMP3_MAX(DRMP3_MAX(max_band[0], max_band[1]), max_band[2]); |
| 1576 |
} |
| 1577 |
for (i = 0; i < max_blocks; i++) |
| 1578 |
{ |
| 1579 |
int default_pos = DRMP3_HDR_TEST_MPEG1(hdr) ? 3 : 0; |
| 1580 |
int itop = n_sfb - max_blocks + i; |
| 1581 |
int prev = itop - max_blocks; |
| 1582 |
ist_pos[itop] = (drmp3_uint8)(max_band[i] >= prev ? default_pos : ist_pos[prev]); |
| 1583 |
} |
| 1584 |
drmp3_L3_stereo_process(left, ist_pos, gr->sfbtab, hdr, max_band, gr[1].scalefac_compress & 1); |
| 1585 |
} |
| 1586 |
|
| 1587 |
static void drmp3_L3_reorder(float *grbuf, float *scratch, const drmp3_uint8 *sfb) |
| 1588 |
{ |
| 1589 |
int i, len; |
| 1590 |
float *src = grbuf, *dst = scratch; |
| 1591 |
|
| 1592 |
for (;0 != (len = *sfb); sfb += 3, src += 2*len) |
| 1593 |
{ |
| 1594 |
for (i = 0; i < len; i++, src++) |
| 1595 |
{ |
| 1596 |
*dst++ = src[0*len]; |
| 1597 |
*dst++ = src[1*len]; |
| 1598 |
*dst++ = src[2*len]; |
| 1599 |
} |
| 1600 |
} |
| 1601 |
DRMP3_COPY_MEMORY(grbuf, scratch, (dst - scratch)*sizeof(float)); |
| 1602 |
} |
| 1603 |
|
| 1604 |
static void drmp3_L3_antialias(float *grbuf, int nbands) |
| 1605 |
{ |
| 1606 |
static const float g_aa[2][8] = { |
| 1607 |
{0.85749293f,0.88174200f,0.94962865f,0.98331459f,0.99551782f,0.99916056f,0.99989920f,0.99999316f}, |
| 1608 |
{0.51449576f,0.47173197f,0.31337745f,0.18191320f,0.09457419f,0.04096558f,0.01419856f,0.00369997f} |
| 1609 |
}; |
| 1610 |
|
| 1611 |
for (; nbands > 0; nbands--, grbuf += 18) |
| 1612 |
{ |
| 1613 |
int i = 0; |
| 1614 |
#if DRMP3_HAVE_SIMD |
| 1615 |
if (drmp3_have_simd()) for (; i < 8; i += 4) |
| 1616 |
{ |
| 1617 |
drmp3_f4 vu = DRMP3_VLD(grbuf + 18 + i); |
| 1618 |
drmp3_f4 vd = DRMP3_VLD(grbuf + 14 - i); |
| 1619 |
drmp3_f4 vc0 = DRMP3_VLD(g_aa[0] + i); |
| 1620 |
drmp3_f4 vc1 = DRMP3_VLD(g_aa[1] + i); |
| 1621 |
vd = DRMP3_VREV(vd); |
| 1622 |
DRMP3_VSTORE(grbuf + 18 + i, DRMP3_VSUB(DRMP3_VMUL(vu, vc0), DRMP3_VMUL(vd, vc1))); |
| 1623 |
vd = DRMP3_VADD(DRMP3_VMUL(vu, vc1), DRMP3_VMUL(vd, vc0)); |
| 1624 |
DRMP3_VSTORE(grbuf + 14 - i, DRMP3_VREV(vd)); |
| 1625 |
} |
| 1626 |
#endif |
| 1627 |
#ifndef DR_MP3_ONLY_SIMD |
| 1628 |
for(; i < 8; i++) |
| 1629 |
{ |
| 1630 |
float u = grbuf[18 + i]; |
| 1631 |
float d = grbuf[17 - i]; |
| 1632 |
grbuf[18 + i] = u*g_aa[0][i] - d*g_aa[1][i]; |
| 1633 |
grbuf[17 - i] = u*g_aa[1][i] + d*g_aa[0][i]; |
| 1634 |
} |
| 1635 |
#endif |
| 1636 |
} |
| 1637 |
} |
| 1638 |
|
| 1639 |
static void drmp3_L3_dct3_9(float *y) |
| 1640 |
{ |
| 1641 |
float s0, s1, s2, s3, s4, s5, s6, s7, s8, t0, t2, t4; |
| 1642 |
|
| 1643 |
s0 = y[0]; s2 = y[2]; s4 = y[4]; s6 = y[6]; s8 = y[8]; |
| 1644 |
t0 = s0 + s6*0.5f; |
| 1645 |
s0 -= s6; |
| 1646 |
t4 = (s4 + s2)*0.93969262f; |
| 1647 |
t2 = (s8 + s2)*0.76604444f; |
| 1648 |
s6 = (s4 - s8)*0.17364818f; |
| 1649 |
s4 += s8 - s2; |
| 1650 |
|
| 1651 |
s2 = s0 - s4*0.5f; |
| 1652 |
y[4] = s4 + s0; |
| 1653 |
s8 = t0 - t2 + s6; |
| 1654 |
s0 = t0 - t4 + t2; |
| 1655 |
s4 = t0 + t4 - s6; |
| 1656 |
|
| 1657 |
s1 = y[1]; s3 = y[3]; s5 = y[5]; s7 = y[7]; |
| 1658 |
|
| 1659 |
s3 *= 0.86602540f; |
| 1660 |
t0 = (s5 + s1)*0.98480775f; |
| 1661 |
t4 = (s5 - s7)*0.34202014f; |
| 1662 |
t2 = (s1 + s7)*0.64278761f; |
| 1663 |
s1 = (s1 - s5 - s7)*0.86602540f; |
| 1664 |
|
| 1665 |
s5 = t0 - s3 - t2; |
| 1666 |
s7 = t4 - s3 - t0; |
| 1667 |
s3 = t4 + s3 - t2; |
| 1668 |
|
| 1669 |
y[0] = s4 - s7; |
| 1670 |
y[1] = s2 + s1; |
| 1671 |
y[2] = s0 - s3; |
| 1672 |
y[3] = s8 + s5; |
| 1673 |
y[5] = s8 - s5; |
| 1674 |
y[6] = s0 + s3; |
| 1675 |
y[7] = s2 - s1; |
| 1676 |
y[8] = s4 + s7; |
| 1677 |
} |
| 1678 |
|
| 1679 |
static void drmp3_L3_imdct36(float *grbuf, float *overlap, const float *window, int nbands) |
| 1680 |
{ |
| 1681 |
int i, j; |
| 1682 |
static const float g_twid9[18] = { |
| 1683 |
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 |
| 1684 |
}; |
| 1685 |
|
| 1686 |
for (j = 0; j < nbands; j++, grbuf += 18, overlap += 9) |
| 1687 |
{ |
| 1688 |
float co[9], si[9]; |
| 1689 |
co[0] = -grbuf[0]; |
| 1690 |
si[0] = grbuf[17]; |
| 1691 |
for (i = 0; i < 4; i++) |
| 1692 |
{ |
| 1693 |
si[8 - 2*i] = grbuf[4*i + 1] - grbuf[4*i + 2]; |
| 1694 |
co[1 + 2*i] = grbuf[4*i + 1] + grbuf[4*i + 2]; |
| 1695 |
si[7 - 2*i] = grbuf[4*i + 4] - grbuf[4*i + 3]; |
| 1696 |
co[2 + 2*i] = -(grbuf[4*i + 3] + grbuf[4*i + 4]); |
| 1697 |
} |
| 1698 |
drmp3_L3_dct3_9(co); |
| 1699 |
drmp3_L3_dct3_9(si); |
| 1700 |
|
| 1701 |
si[1] = -si[1]; |
| 1702 |
si[3] = -si[3]; |
| 1703 |
si[5] = -si[5]; |
| 1704 |
si[7] = -si[7]; |
| 1705 |
|
| 1706 |
i = 0; |
| 1707 |
|
| 1708 |
#if DRMP3_HAVE_SIMD |
| 1709 |
if (drmp3_have_simd()) for (; i < 8; i += 4) |
| 1710 |
{ |
| 1711 |
drmp3_f4 vovl = DRMP3_VLD(overlap + i); |
| 1712 |
drmp3_f4 vc = DRMP3_VLD(co + i); |
| 1713 |
drmp3_f4 vs = DRMP3_VLD(si + i); |
| 1714 |
drmp3_f4 vr0 = DRMP3_VLD(g_twid9 + i); |
| 1715 |
drmp3_f4 vr1 = DRMP3_VLD(g_twid9 + 9 + i); |
| 1716 |
drmp3_f4 vw0 = DRMP3_VLD(window + i); |
| 1717 |
drmp3_f4 vw1 = DRMP3_VLD(window + 9 + i); |
| 1718 |
drmp3_f4 vsum = DRMP3_VADD(DRMP3_VMUL(vc, vr1), DRMP3_VMUL(vs, vr0)); |
| 1719 |
DRMP3_VSTORE(overlap + i, DRMP3_VSUB(DRMP3_VMUL(vc, vr0), DRMP3_VMUL(vs, vr1))); |
| 1720 |
DRMP3_VSTORE(grbuf + i, DRMP3_VSUB(DRMP3_VMUL(vovl, vw0), DRMP3_VMUL(vsum, vw1))); |
| 1721 |
vsum = DRMP3_VADD(DRMP3_VMUL(vovl, vw1), DRMP3_VMUL(vsum, vw0)); |
| 1722 |
DRMP3_VSTORE(grbuf + 14 - i, DRMP3_VREV(vsum)); |
| 1723 |
} |
| 1724 |
#endif |
| 1725 |
for (; i < 9; i++) |
| 1726 |
{ |
| 1727 |
float ovl = overlap[i]; |
| 1728 |
float sum = co[i]*g_twid9[9 + i] + si[i]*g_twid9[0 + i]; |
| 1729 |
overlap[i] = co[i]*g_twid9[0 + i] - si[i]*g_twid9[9 + i]; |
| 1730 |
grbuf[i] = ovl*window[0 + i] - sum*window[9 + i]; |
| 1731 |
grbuf[17 - i] = ovl*window[9 + i] + sum*window[0 + i]; |
| 1732 |
} |
| 1733 |
} |
| 1734 |
} |
| 1735 |
|
| 1736 |
static void drmp3_L3_idct3(float x0, float x1, float x2, float *dst) |
| 1737 |
{ |
| 1738 |
float m1 = x1*0.86602540f; |
| 1739 |
float a1 = x0 - x2*0.5f; |
| 1740 |
dst[1] = x0 + x2; |
| 1741 |
dst[0] = a1 + m1; |
| 1742 |
dst[2] = a1 - m1; |
| 1743 |
} |
| 1744 |
|
| 1745 |
static void drmp3_L3_imdct12(float *x, float *dst, float *overlap) |
| 1746 |
{ |
| 1747 |
static const float g_twid3[6] = { 0.79335334f,0.92387953f,0.99144486f, 0.60876143f,0.38268343f,0.13052619f }; |
| 1748 |
float co[3], si[3]; |
| 1749 |
int i; |
| 1750 |
|
| 1751 |
drmp3_L3_idct3(-x[0], x[6] + x[3], x[12] + x[9], co); |
| 1752 |
drmp3_L3_idct3(x[15], x[12] - x[9], x[6] - x[3], si); |
| 1753 |
si[1] = -si[1]; |
| 1754 |
|
| 1755 |
for (i = 0; i < 3; i++) |
| 1756 |
{ |
| 1757 |
float ovl = overlap[i]; |
| 1758 |
float sum = co[i]*g_twid3[3 + i] + si[i]*g_twid3[0 + i]; |
| 1759 |
overlap[i] = co[i]*g_twid3[0 + i] - si[i]*g_twid3[3 + i]; |
| 1760 |
dst[i] = ovl*g_twid3[2 - i] - sum*g_twid3[5 - i]; |
| 1761 |
dst[5 - i] = ovl*g_twid3[5 - i] + sum*g_twid3[2 - i]; |
| 1762 |
} |
| 1763 |
} |
| 1764 |
|
| 1765 |
static void drmp3_L3_imdct_short(float *grbuf, float *overlap, int nbands) |
| 1766 |
{ |
| 1767 |
for (;nbands > 0; nbands--, overlap += 9, grbuf += 18) |
| 1768 |
{ |
| 1769 |
float tmp[18]; |
| 1770 |
DRMP3_COPY_MEMORY(tmp, grbuf, sizeof(tmp)); |
| 1771 |
DRMP3_COPY_MEMORY(grbuf, overlap, 6*sizeof(float)); |
| 1772 |
drmp3_L3_imdct12(tmp, grbuf + 6, overlap + 6); |
| 1773 |
drmp3_L3_imdct12(tmp + 1, grbuf + 12, overlap + 6); |
| 1774 |
drmp3_L3_imdct12(tmp + 2, overlap, overlap + 6); |
| 1775 |
} |
| 1776 |
} |
| 1777 |
|
| 1778 |
static void drmp3_L3_change_sign(float *grbuf) |
| 1779 |
{ |
| 1780 |
int b, i; |
| 1781 |
for (b = 0, grbuf += 18; b < 32; b += 2, grbuf += 36) |
| 1782 |
for (i = 1; i < 18; i += 2) |
| 1783 |
grbuf[i] = -grbuf[i]; |
| 1784 |
} |
| 1785 |
|
| 1786 |
static void drmp3_L3_imdct_gr(float *grbuf, float *overlap, unsigned block_type, unsigned n_long_bands) |
| 1787 |
{ |
| 1788 |
static const float g_mdct_window[2][18] = { |
| 1789 |
{ 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 }, |
| 1790 |
{ 1,1,1,1,1,1,0.99144486f,0.92387953f,0.79335334f,0,0,0,0,0,0,0.13052619f,0.38268343f,0.60876143f } |
| 1791 |
}; |
| 1792 |
if (n_long_bands) |
| 1793 |
{ |
| 1794 |
drmp3_L3_imdct36(grbuf, overlap, g_mdct_window[0], n_long_bands); |
| 1795 |
grbuf += 18*n_long_bands; |
| 1796 |
overlap += 9*n_long_bands; |
| 1797 |
} |
| 1798 |
if (block_type == DRMP3_SHORT_BLOCK_TYPE) |
| 1799 |
drmp3_L3_imdct_short(grbuf, overlap, 32 - n_long_bands); |
| 1800 |
else |
| 1801 |
drmp3_L3_imdct36(grbuf, overlap, g_mdct_window[block_type == DRMP3_STOP_BLOCK_TYPE], 32 - n_long_bands); |
| 1802 |
} |
| 1803 |
|
| 1804 |
static void drmp3_L3_save_reservoir(drmp3dec *h, drmp3dec_scratch *s) |
| 1805 |
{ |
| 1806 |
int pos = (s->bs.pos + 7)/8u; |
| 1807 |
int remains = s->bs.limit/8u - pos; |
| 1808 |
if (remains > DRMP3_MAX_BITRESERVOIR_BYTES) |
| 1809 |
{ |
| 1810 |
pos += remains - DRMP3_MAX_BITRESERVOIR_BYTES; |
| 1811 |
remains = DRMP3_MAX_BITRESERVOIR_BYTES; |
| 1812 |
} |
| 1813 |
if (remains > 0) |
| 1814 |
{ |
| 1815 |
DRMP3_MOVE_MEMORY(h->reserv_buf, s->maindata + pos, remains); |
| 1816 |
} |
| 1817 |
h->reserv = remains; |
| 1818 |
} |
| 1819 |
|
| 1820 |
static int drmp3_L3_restore_reservoir(drmp3dec *h, drmp3_bs *bs, drmp3dec_scratch *s, int main_data_begin) |
| 1821 |
{ |
| 1822 |
int frame_bytes = (bs->limit - bs->pos)/8; |
| 1823 |
int bytes_have = DRMP3_MIN(h->reserv, main_data_begin); |
| 1824 |
DRMP3_COPY_MEMORY(s->maindata, h->reserv_buf + DRMP3_MAX(0, h->reserv - main_data_begin), DRMP3_MIN(h->reserv, main_data_begin)); |
| 1825 |
DRMP3_COPY_MEMORY(s->maindata + bytes_have, bs->buf + bs->pos/8, frame_bytes); |
| 1826 |
drmp3_bs_init(&s->bs, s->maindata, bytes_have + frame_bytes); |
| 1827 |
return h->reserv >= main_data_begin; |
| 1828 |
} |
| 1829 |
|
| 1830 |
static void drmp3_L3_decode(drmp3dec *h, drmp3dec_scratch *s, drmp3_L3_gr_info *gr_info, int nch) |
| 1831 |
{ |
| 1832 |
int ch; |
| 1833 |
|
| 1834 |
for (ch = 0; ch < nch; ch++) |
| 1835 |
{ |
| 1836 |
int layer3gr_limit = s->bs.pos + gr_info[ch].part_23_length; |
| 1837 |
drmp3_L3_decode_scalefactors(h->header, s->ist_pos[ch], &s->bs, gr_info + ch, s->scf, ch); |
| 1838 |
drmp3_L3_huffman(s->grbuf[ch], &s->bs, gr_info + ch, s->scf, layer3gr_limit); |
| 1839 |
} |
| 1840 |
|
| 1841 |
if (DRMP3_HDR_TEST_I_STEREO(h->header)) |
| 1842 |
{ |
| 1843 |
drmp3_L3_intensity_stereo(s->grbuf[0], s->ist_pos[1], gr_info, h->header); |
| 1844 |
} else if (DRMP3_HDR_IS_MS_STEREO(h->header)) |
| 1845 |
{ |
| 1846 |
drmp3_L3_midside_stereo(s->grbuf[0], 576); |
| 1847 |
} |
| 1848 |
|
| 1849 |
for (ch = 0; ch < nch; ch++, gr_info++) |
| 1850 |
{ |
| 1851 |
int aa_bands = 31; |
| 1852 |
int n_long_bands = (gr_info->mixed_block_flag ? 2 : 0) << (int)(DRMP3_HDR_GET_MY_SAMPLE_RATE(h->header) == 2); |
| 1853 |
|
| 1854 |
if (gr_info->n_short_sfb) |
| 1855 |
{ |
| 1856 |
aa_bands = n_long_bands - 1; |
| 1857 |
drmp3_L3_reorder(s->grbuf[ch] + n_long_bands*18, s->syn[0], gr_info->sfbtab + gr_info->n_long_sfb); |
| 1858 |
} |
| 1859 |
|
| 1860 |
drmp3_L3_antialias(s->grbuf[ch], aa_bands); |
| 1861 |
drmp3_L3_imdct_gr(s->grbuf[ch], h->mdct_overlap[ch], gr_info->block_type, n_long_bands); |
| 1862 |
drmp3_L3_change_sign(s->grbuf[ch]); |
| 1863 |
} |
| 1864 |
} |
| 1865 |
|
| 1866 |
static void drmp3d_DCT_II(float *grbuf, int n) |
| 1867 |
{ |
| 1868 |
static const float g_sec[24] = { |
| 1869 |
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 |
| 1870 |
}; |
| 1871 |
int i, k = 0; |
| 1872 |
#if DRMP3_HAVE_SIMD |
| 1873 |
if (drmp3_have_simd()) for (; k < n; k += 4) |
| 1874 |
{ |
| 1875 |
drmp3_f4 t[4][8], *x; |
| 1876 |
float *y = grbuf + k; |
| 1877 |
|
| 1878 |
for (x = t[0], i = 0; i < 8; i++, x++) |
| 1879 |
{ |
| 1880 |
drmp3_f4 x0 = DRMP3_VLD(&y[i*18]); |
| 1881 |
drmp3_f4 x1 = DRMP3_VLD(&y[(15 - i)*18]); |
| 1882 |
drmp3_f4 x2 = DRMP3_VLD(&y[(16 + i)*18]); |
| 1883 |
drmp3_f4 x3 = DRMP3_VLD(&y[(31 - i)*18]); |
| 1884 |
drmp3_f4 t0 = DRMP3_VADD(x0, x3); |
| 1885 |
drmp3_f4 t1 = DRMP3_VADD(x1, x2); |
| 1886 |
drmp3_f4 t2 = DRMP3_VMUL_S(DRMP3_VSUB(x1, x2), g_sec[3*i + 0]); |
| 1887 |
drmp3_f4 t3 = DRMP3_VMUL_S(DRMP3_VSUB(x0, x3), g_sec[3*i + 1]); |
| 1888 |
x[0] = DRMP3_VADD(t0, t1); |
| 1889 |
x[8] = DRMP3_VMUL_S(DRMP3_VSUB(t0, t1), g_sec[3*i + 2]); |
| 1890 |
x[16] = DRMP3_VADD(t3, t2); |
| 1891 |
x[24] = DRMP3_VMUL_S(DRMP3_VSUB(t3, t2), g_sec[3*i + 2]); |
| 1892 |
} |
| 1893 |
for (x = t[0], i = 0; i < 4; i++, x += 8) |
| 1894 |
{ |
| 1895 |
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; |
| 1896 |
xt = DRMP3_VSUB(x0, x7); x0 = DRMP3_VADD(x0, x7); |
| 1897 |
x7 = DRMP3_VSUB(x1, x6); x1 = DRMP3_VADD(x1, x6); |
| 1898 |
x6 = DRMP3_VSUB(x2, x5); x2 = DRMP3_VADD(x2, x5); |
| 1899 |
x5 = DRMP3_VSUB(x3, x4); x3 = DRMP3_VADD(x3, x4); |
| 1900 |
x4 = DRMP3_VSUB(x0, x3); x0 = DRMP3_VADD(x0, x3); |
| 1901 |
x3 = DRMP3_VSUB(x1, x2); x1 = DRMP3_VADD(x1, x2); |
| 1902 |
x[0] = DRMP3_VADD(x0, x1); |
| 1903 |
x[4] = DRMP3_VMUL_S(DRMP3_VSUB(x0, x1), 0.70710677f); |
| 1904 |
x5 = DRMP3_VADD(x5, x6); |
| 1905 |
x6 = DRMP3_VMUL_S(DRMP3_VADD(x6, x7), 0.70710677f); |
| 1906 |
x7 = DRMP3_VADD(x7, xt); |
| 1907 |
x3 = DRMP3_VMUL_S(DRMP3_VADD(x3, x4), 0.70710677f); |
| 1908 |
x5 = DRMP3_VSUB(x5, DRMP3_VMUL_S(x7, 0.198912367f)); |
| 1909 |
x7 = DRMP3_VADD(x7, DRMP3_VMUL_S(x5, 0.382683432f)); |
| 1910 |
x5 = DRMP3_VSUB(x5, DRMP3_VMUL_S(x7, 0.198912367f)); |
| 1911 |
x0 = DRMP3_VSUB(xt, x6); xt = DRMP3_VADD(xt, x6); |
| 1912 |
x[1] = DRMP3_VMUL_S(DRMP3_VADD(xt, x7), 0.50979561f); |
| 1913 |
x[2] = DRMP3_VMUL_S(DRMP3_VADD(x4, x3), 0.54119611f); |
| 1914 |
x[3] = DRMP3_VMUL_S(DRMP3_VSUB(x0, x5), 0.60134488f); |
| 1915 |
x[5] = DRMP3_VMUL_S(DRMP3_VADD(x0, x5), 0.89997619f); |
| 1916 |
x[6] = DRMP3_VMUL_S(DRMP3_VSUB(x4, x3), 1.30656302f); |
| 1917 |
x[7] = DRMP3_VMUL_S(DRMP3_VSUB(xt, x7), 2.56291556f); |
| 1918 |
} |
| 1919 |
|
| 1920 |
if (k > n - 3) |
| 1921 |
{ |
| 1922 |
#if DRMP3_HAVE_SSE |
| 1923 |
#define DRMP3_VSAVE2(i, v) _mm_storel_pi((__m64 *)(void*)&y[i*18], v) |
| 1924 |
#else |
| 1925 |
#define DRMP3_VSAVE2(i, v) vst1_f32((float32_t *)&y[(i)*18], vget_low_f32(v)) |
| 1926 |
#endif |
| 1927 |
for (i = 0; i < 7; i++, y += 4*18) |
| 1928 |
{ |
| 1929 |
drmp3_f4 s = DRMP3_VADD(t[3][i], t[3][i + 1]); |
| 1930 |
DRMP3_VSAVE2(0, t[0][i]); |
| 1931 |
DRMP3_VSAVE2(1, DRMP3_VADD(t[2][i], s)); |
| 1932 |
DRMP3_VSAVE2(2, DRMP3_VADD(t[1][i], t[1][i + 1])); |
| 1933 |
DRMP3_VSAVE2(3, DRMP3_VADD(t[2][1 + i], s)); |
| 1934 |
} |
| 1935 |
DRMP3_VSAVE2(0, t[0][7]); |
| 1936 |
DRMP3_VSAVE2(1, DRMP3_VADD(t[2][7], t[3][7])); |
| 1937 |
DRMP3_VSAVE2(2, t[1][7]); |
| 1938 |
DRMP3_VSAVE2(3, t[3][7]); |
| 1939 |
} else |
| 1940 |
{ |
| 1941 |
#define DRMP3_VSAVE4(i, v) DRMP3_VSTORE(&y[(i)*18], v) |
| 1942 |
for (i = 0; i < 7; i++, y += 4*18) |
| 1943 |
{ |
| 1944 |
drmp3_f4 s = DRMP3_VADD(t[3][i], t[3][i + 1]); |
| 1945 |
DRMP3_VSAVE4(0, t[0][i]); |
| 1946 |
DRMP3_VSAVE4(1, DRMP3_VADD(t[2][i], s)); |
| 1947 |
DRMP3_VSAVE4(2, DRMP3_VADD(t[1][i], t[1][i + 1])); |
| 1948 |
DRMP3_VSAVE4(3, DRMP3_VADD(t[2][1 + i], s)); |
| 1949 |
} |
| 1950 |
DRMP3_VSAVE4(0, t[0][7]); |
| 1951 |
DRMP3_VSAVE4(1, DRMP3_VADD(t[2][7], t[3][7])); |
| 1952 |
DRMP3_VSAVE4(2, t[1][7]); |
| 1953 |
DRMP3_VSAVE4(3, t[3][7]); |
| 1954 |
} |
| 1955 |
} else |
| 1956 |
#endif |
| 1957 |
#ifdef DR_MP3_ONLY_SIMD |
| 1958 |
{} |
| 1959 |
#else |
| 1960 |
for (; k < n; k++) |
| 1961 |
{ |
| 1962 |
float t[4][8], *x, *y = grbuf + k; |
| 1963 |
|
| 1964 |
for (x = t[0], i = 0; i < 8; i++, x++) |
| 1965 |
{ |
| 1966 |
float x0 = y[i*18]; |
| 1967 |
float x1 = y[(15 - i)*18]; |
| 1968 |
float x2 = y[(16 + i)*18]; |
| 1969 |
float x3 = y[(31 - i)*18]; |
| 1970 |
float t0 = x0 + x3; |
| 1971 |
float t1 = x1 + x2; |
| 1972 |
float t2 = (x1 - x2)*g_sec[3*i + 0]; |
| 1973 |
float t3 = (x0 - x3)*g_sec[3*i + 1]; |
| 1974 |
x[0] = t0 + t1; |
| 1975 |
x[8] = (t0 - t1)*g_sec[3*i + 2]; |
| 1976 |
x[16] = t3 + t2; |
| 1977 |
x[24] = (t3 - t2)*g_sec[3*i + 2]; |
| 1978 |
} |
| 1979 |
for (x = t[0], i = 0; i < 4; i++, x += 8) |
| 1980 |
{ |
| 1981 |
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; |
| 1982 |
xt = x0 - x7; x0 += x7; |
| 1983 |
x7 = x1 - x6; x1 += x6; |
| 1984 |
x6 = x2 - x5; x2 += x5; |
| 1985 |
x5 = x3 - x4; x3 += x4; |
| 1986 |
x4 = x0 - x3; x0 += x3; |
| 1987 |
x3 = x1 - x2; x1 += x2; |
| 1988 |
x[0] = x0 + x1; |
| 1989 |
x[4] = (x0 - x1)*0.70710677f; |
| 1990 |
x5 = x5 + x6; |
| 1991 |
x6 = (x6 + x7)*0.70710677f; |
| 1992 |
x7 = x7 + xt; |
| 1993 |
x3 = (x3 + x4)*0.70710677f; |
| 1994 |
x5 -= x7*0.198912367f; |
| 1995 |
x7 += x5*0.382683432f; |
| 1996 |
x5 -= x7*0.198912367f; |
| 1997 |
x0 = xt - x6; xt += x6; |
| 1998 |
x[1] = (xt + x7)*0.50979561f; |
| 1999 |
x[2] = (x4 + x3)*0.54119611f; |
| 2000 |
x[3] = (x0 - x5)*0.60134488f; |
| 2001 |
x[5] = (x0 + x5)*0.89997619f; |
| 2002 |
x[6] = (x4 - x3)*1.30656302f; |
| 2003 |
x[7] = (xt - x7)*2.56291556f; |
| 2004 |
|
| 2005 |
} |
| 2006 |
for (i = 0; i < 7; i++, y += 4*18) |
| 2007 |
{ |
| 2008 |
y[0*18] = t[0][i]; |
| 2009 |
y[1*18] = t[2][i] + t[3][i] + t[3][i + 1]; |
| 2010 |
y[2*18] = t[1][i] + t[1][i + 1]; |
| 2011 |
y[3*18] = t[2][i + 1] + t[3][i] + t[3][i + 1]; |
| 2012 |
} |
| 2013 |
y[0*18] = t[0][7]; |
| 2014 |
y[1*18] = t[2][7] + t[3][7]; |
| 2015 |
y[2*18] = t[1][7]; |
| 2016 |
y[3*18] = t[3][7]; |
| 2017 |
} |
| 2018 |
#endif |
| 2019 |
} |
| 2020 |
|
| 2021 |
#ifndef DR_MP3_FLOAT_OUTPUT |
| 2022 |
typedef drmp3_int16 drmp3d_sample_t; |
| 2023 |
|
| 2024 |
static drmp3_int16 drmp3d_scale_pcm(float sample) |
| 2025 |
{ |
| 2026 |
drmp3_int16 s; |
| 2027 |
#if DRMP3_HAVE_ARMV6 |
| 2028 |
drmp3_int32 s32 = (drmp3_int32)(sample + .5f); |
| 2029 |
s32 -= (s32 < 0); |
| 2030 |
s = (drmp3_int16)drmp3_clip_int16_arm(s32); |
| 2031 |
#else |
| 2032 |
if (sample >= 32766.5f) return (drmp3_int16) 32767; |
| 2033 |
if (sample <= -32767.5f) return (drmp3_int16)-32768; |
| 2034 |
s = (drmp3_int16)(sample + .5f); |
| 2035 |
s -= (s < 0); |
| 2036 |
#endif |
| 2037 |
return s; |
| 2038 |
} |
| 2039 |
#else |
| 2040 |
typedef float drmp3d_sample_t; |
| 2041 |
|
| 2042 |
static float drmp3d_scale_pcm(float sample) |
| 2043 |
{ |
| 2044 |
return sample*(1.f/32768.f); |
| 2045 |
} |
| 2046 |
#endif |
| 2047 |
|
| 2048 |
static void drmp3d_synth_pair(drmp3d_sample_t *pcm, int nch, const float *z) |
| 2049 |
{ |
| 2050 |
float a; |
| 2051 |
a = (z[14*64] - z[ 0]) * 29; |
| 2052 |
a += (z[ 1*64] + z[13*64]) * 213; |
| 2053 |
a += (z[12*64] - z[ 2*64]) * 459; |
| 2054 |
a += (z[ 3*64] + z[11*64]) * 2037; |
| 2055 |
a += (z[10*64] - z[ 4*64]) * 5153; |
| 2056 |
a += (z[ 5*64] + z[ 9*64]) * 6574; |
| 2057 |
a += (z[ 8*64] - z[ 6*64]) * 37489; |
| 2058 |
a += z[ 7*64] * 75038; |
| 2059 |
pcm[0] = drmp3d_scale_pcm(a); |
| 2060 |
|
| 2061 |
z += 2; |
| 2062 |
a = z[14*64] * 104; |
| 2063 |
a += z[12*64] * 1567; |
| 2064 |
a += z[10*64] * 9727; |
| 2065 |
a += z[ 8*64] * 64019; |
| 2066 |
a += z[ 6*64] * -9975; |
| 2067 |
a += z[ 4*64] * -45; |
| 2068 |
a += z[ 2*64] * 146; |
| 2069 |
a += z[ 0*64] * -5; |
| 2070 |
pcm[16*nch] = drmp3d_scale_pcm(a); |
| 2071 |
} |
| 2072 |
|
| 2073 |
static void drmp3d_synth(float *xl, drmp3d_sample_t *dstl, int nch, float *lins) |
| 2074 |
{ |
| 2075 |
int i; |
| 2076 |
float *xr = xl + 576*(nch - 1); |
| 2077 |
drmp3d_sample_t *dstr = dstl + (nch - 1); |
| 2078 |
|
| 2079 |
static const float g_win[] = { |
| 2080 |
-1,26,-31,208,218,401,-519,2063,2000,4788,-5517,7134,5959,35640,-39336,74992, |
| 2081 |
-1,24,-35,202,222,347,-581,2080,1952,4425,-5879,7640,5288,33791,-41176,74856, |
| 2082 |
-1,21,-38,196,225,294,-645,2087,1893,4063,-6237,8092,4561,31947,-43006,74630, |
| 2083 |
-1,19,-41,190,227,244,-711,2085,1822,3705,-6589,8492,3776,30112,-44821,74313, |
| 2084 |
-1,17,-45,183,228,197,-779,2075,1739,3351,-6935,8840,2935,28289,-46617,73908, |
| 2085 |
-1,16,-49,176,228,153,-848,2057,1644,3004,-7271,9139,2037,26482,-48390,73415, |
| 2086 |
-2,14,-53,169,227,111,-919,2032,1535,2663,-7597,9389,1082,24694,-50137,72835, |
| 2087 |
-2,13,-58,161,224,72,-991,2001,1414,2330,-7910,9592,70,22929,-51853,72169, |
| 2088 |
-2,11,-63,154,221,36,-1064,1962,1280,2006,-8209,9750,-998,21189,-53534,71420, |
| 2089 |
-2,10,-68,147,215,2,-1137,1919,1131,1692,-8491,9863,-2122,19478,-55178,70590, |
| 2090 |
-3,9,-73,139,208,-29,-1210,1870,970,1388,-8755,9935,-3300,17799,-56778,69679, |
| 2091 |
-3,8,-79,132,200,-57,-1283,1817,794,1095,-8998,9966,-4533,16155,-58333,68692, |
| 2092 |
-4,7,-85,125,189,-83,-1356,1759,605,814,-9219,9959,-5818,14548,-59838,67629, |
| 2093 |
-4,7,-91,117,177,-106,-1428,1698,402,545,-9416,9916,-7154,12980,-61289,66494, |
| 2094 |
-5,6,-97,111,163,-127,-1498,1634,185,288,-9585,9838,-8540,11455,-62684,65290 |
| 2095 |
}; |
| 2096 |
float *zlin = lins + 15*64; |
| 2097 |
const float *w = g_win; |
| 2098 |
|
| 2099 |
zlin[4*15] = xl[18*16]; |
| 2100 |
zlin[4*15 + 1] = xr[18*16]; |
| 2101 |
zlin[4*15 + 2] = xl[0]; |
| 2102 |
zlin[4*15 + 3] = xr[0]; |
| 2103 |
|
| 2104 |
zlin[4*31] = xl[1 + 18*16]; |
| 2105 |
zlin[4*31 + 1] = xr[1 + 18*16]; |
| 2106 |
zlin[4*31 + 2] = xl[1]; |
| 2107 |
zlin[4*31 + 3] = xr[1]; |
| 2108 |
|
| 2109 |
drmp3d_synth_pair(dstr, nch, lins + 4*15 + 1); |
| 2110 |
drmp3d_synth_pair(dstr + 32*nch, nch, lins + 4*15 + 64 + 1); |
| 2111 |
drmp3d_synth_pair(dstl, nch, lins + 4*15); |
| 2112 |
drmp3d_synth_pair(dstl + 32*nch, nch, lins + 4*15 + 64); |
| 2113 |
|
| 2114 |
#if DRMP3_HAVE_SIMD |
| 2115 |
if (drmp3_have_simd()) for (i = 14; i >= 0; i--) |
| 2116 |
{ |
| 2117 |
#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)]); |
| 2118 |
#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)); } |
| 2119 |
#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))); } |
| 2120 |
#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))); } |
| 2121 |
drmp3_f4 a, b; |
| 2122 |
zlin[4*i] = xl[18*(31 - i)]; |
| 2123 |
zlin[4*i + 1] = xr[18*(31 - i)]; |
| 2124 |
zlin[4*i + 2] = xl[1 + 18*(31 - i)]; |
| 2125 |
zlin[4*i + 3] = xr[1 + 18*(31 - i)]; |
| 2126 |
zlin[4*i + 64] = xl[1 + 18*(1 + i)]; |
| 2127 |
zlin[4*i + 64 + 1] = xr[1 + 18*(1 + i)]; |
| 2128 |
zlin[4*i - 64 + 2] = xl[18*(1 + i)]; |
| 2129 |
zlin[4*i - 64 + 3] = xr[18*(1 + i)]; |
| 2130 |
|
| 2131 |
DRMP3_V0(0) DRMP3_V2(1) DRMP3_V1(2) DRMP3_V2(3) DRMP3_V1(4) DRMP3_V2(5) DRMP3_V1(6) DRMP3_V2(7) |
| 2132 |
|
| 2133 |
{ |
| 2134 |
#ifndef DR_MP3_FLOAT_OUTPUT |
| 2135 |
#if DRMP3_HAVE_SSE |
| 2136 |
static const drmp3_f4 g_max = { 32767.0f, 32767.0f, 32767.0f, 32767.0f }; |
| 2137 |
static const drmp3_f4 g_min = { -32768.0f, -32768.0f, -32768.0f, -32768.0f }; |
| 2138 |
__m128i pcm8 = _mm_packs_epi32(_mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(a, g_max), g_min)), |
| 2139 |
_mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(b, g_max), g_min))); |
| 2140 |
dstr[(15 - i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 1); |
| 2141 |
dstr[(17 + i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 5); |
| 2142 |
dstl[(15 - i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 0); |
| 2143 |
dstl[(17 + i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 4); |
| 2144 |
dstr[(47 - i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 3); |
| 2145 |
dstr[(49 + i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 7); |
| 2146 |
dstl[(47 - i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 2); |
| 2147 |
dstl[(49 + i)*nch] = (drmp3_int16)_mm_extract_epi16(pcm8, 6); |
| 2148 |
#else |
| 2149 |
int16x4_t pcma, pcmb; |
| 2150 |
a = DRMP3_VADD(a, DRMP3_VSET(0.5f)); |
| 2151 |
b = DRMP3_VADD(b, DRMP3_VSET(0.5f)); |
| 2152 |
pcma = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(a), vreinterpretq_s32_u32(vcltq_f32(a, DRMP3_VSET(0))))); |
| 2153 |
pcmb = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(b), vreinterpretq_s32_u32(vcltq_f32(b, DRMP3_VSET(0))))); |
| 2154 |
vst1_lane_s16(dstr + (15 - i)*nch, pcma, 1); |
| 2155 |
vst1_lane_s16(dstr + (17 + i)*nch, pcmb, 1); |
| 2156 |
vst1_lane_s16(dstl + (15 - i)*nch, pcma, 0); |
| 2157 |
vst1_lane_s16(dstl + (17 + i)*nch, pcmb, 0); |
| 2158 |
vst1_lane_s16(dstr + (47 - i)*nch, pcma, 3); |
| 2159 |
vst1_lane_s16(dstr + (49 + i)*nch, pcmb, 3); |
| 2160 |
vst1_lane_s16(dstl + (47 - i)*nch, pcma, 2); |
| 2161 |
vst1_lane_s16(dstl + (49 + i)*nch, pcmb, 2); |
| 2162 |
#endif |
| 2163 |
#else |
| 2164 |
#if DRMP3_HAVE_SSE |
| 2165 |
static const drmp3_f4 g_scale = { 1.0f/32768.0f, 1.0f/32768.0f, 1.0f/32768.0f, 1.0f/32768.0f }; |
| 2166 |
#else |
| 2167 |
const drmp3_f4 g_scale = vdupq_n_f32(1.0f/32768.0f); |
| 2168 |
#endif |
| 2169 |
a = DRMP3_VMUL(a, g_scale); |
| 2170 |
b = DRMP3_VMUL(b, g_scale); |
| 2171 |
#if DRMP3_HAVE_SSE |
| 2172 |
_mm_store_ss(dstr + (15 - i)*nch, _mm_shuffle_ps(a, a, _MM_SHUFFLE(1, 1, 1, 1))); |
| 2173 |
_mm_store_ss(dstr + (17 + i)*nch, _mm_shuffle_ps(b, b, _MM_SHUFFLE(1, 1, 1, 1))); |
| 2174 |
_mm_store_ss(dstl + (15 - i)*nch, _mm_shuffle_ps(a, a, _MM_SHUFFLE(0, 0, 0, 0))); |
| 2175 |
_mm_store_ss(dstl + (17 + i)*nch, _mm_shuffle_ps(b, b, _MM_SHUFFLE(0, 0, 0, 0))); |
| 2176 |
_mm_store_ss(dstr + (47 - i)*nch, _mm_shuffle_ps(a, a, _MM_SHUFFLE(3, 3, 3, 3))); |
| 2177 |
_mm_store_ss(dstr + (49 + i)*nch, _mm_shuffle_ps(b, b, _MM_SHUFFLE(3, 3, 3, 3))); |
| 2178 |
_mm_store_ss(dstl + (47 - i)*nch, _mm_shuffle_ps(a, a, _MM_SHUFFLE(2, 2, 2, 2))); |
| 2179 |
_mm_store_ss(dstl + (49 + i)*nch, _mm_shuffle_ps(b, b, _MM_SHUFFLE(2, 2, 2, 2))); |
| 2180 |
#else |
| 2181 |
vst1q_lane_f32(dstr + (15 - i)*nch, a, 1); |
| 2182 |
vst1q_lane_f32(dstr + (17 + i)*nch, b, 1); |
| 2183 |
vst1q_lane_f32(dstl + (15 - i)*nch, a, 0); |
| 2184 |
vst1q_lane_f32(dstl + (17 + i)*nch, b, 0); |
| 2185 |
vst1q_lane_f32(dstr + (47 - i)*nch, a, 3); |
| 2186 |
vst1q_lane_f32(dstr + (49 + i)*nch, b, 3); |
| 2187 |
vst1q_lane_f32(dstl + (47 - i)*nch, a, 2); |
| 2188 |
vst1q_lane_f32(dstl + (49 + i)*nch, b, 2); |
| 2189 |
#endif |
| 2190 |
#endif |
| 2191 |
} |
| 2192 |
} else |
| 2193 |
#endif |
| 2194 |
#ifdef DR_MP3_ONLY_SIMD |
| 2195 |
{} |
| 2196 |
#else |
| 2197 |
for (i = 14; i >= 0; i--) |
| 2198 |
{ |
| 2199 |
#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]; |
| 2200 |
#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; } |
| 2201 |
#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; } |
| 2202 |
#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; } |
| 2203 |
float a[4], b[4]; |
| 2204 |
|
| 2205 |
zlin[4*i] = xl[18*(31 - i)]; |
| 2206 |
zlin[4*i + 1] = xr[18*(31 - i)]; |
| 2207 |
zlin[4*i + 2] = xl[1 + 18*(31 - i)]; |
| 2208 |
zlin[4*i + 3] = xr[1 + 18*(31 - i)]; |
| 2209 |
zlin[4*(i + 16)] = xl[1 + 18*(1 + i)]; |
| 2210 |
zlin[4*(i + 16) + 1] = xr[1 + 18*(1 + i)]; |
| 2211 |
zlin[4*(i - 16) + 2] = xl[18*(1 + i)]; |
| 2212 |
zlin[4*(i - 16) + 3] = xr[18*(1 + i)]; |
| 2213 |
|
| 2214 |
DRMP3_S0(0) DRMP3_S2(1) DRMP3_S1(2) DRMP3_S2(3) DRMP3_S1(4) DRMP3_S2(5) DRMP3_S1(6) DRMP3_S2(7) |
| 2215 |
|
| 2216 |
dstr[(15 - i)*nch] = drmp3d_scale_pcm(a[1]); |
| 2217 |
dstr[(17 + i)*nch] = drmp3d_scale_pcm(b[1]); |
| 2218 |
dstl[(15 - i)*nch] = drmp3d_scale_pcm(a[0]); |
| 2219 |
dstl[(17 + i)*nch] = drmp3d_scale_pcm(b[0]); |
| 2220 |
dstr[(47 - i)*nch] = drmp3d_scale_pcm(a[3]); |
| 2221 |
dstr[(49 + i)*nch] = drmp3d_scale_pcm(b[3]); |
| 2222 |
dstl[(47 - i)*nch] = drmp3d_scale_pcm(a[2]); |
| 2223 |
dstl[(49 + i)*nch] = drmp3d_scale_pcm(b[2]); |
| 2224 |
} |
| 2225 |
#endif |
| 2226 |
} |
| 2227 |
|
| 2228 |
static void drmp3d_synth_granule(float *qmf_state, float *grbuf, int nbands, int nch, drmp3d_sample_t *pcm, float *lins) |
| 2229 |
{ |
| 2230 |
int i; |
| 2231 |
for (i = 0; i < nch; i++) |
| 2232 |
{ |
| 2233 |
drmp3d_DCT_II(grbuf + 576*i, nbands); |
| 2234 |
} |
| 2235 |
|
| 2236 |
DRMP3_COPY_MEMORY(lins, qmf_state, sizeof(float)*15*64); |
| 2237 |
|
| 2238 |
for (i = 0; i < nbands; i += 2) |
| 2239 |
{ |
| 2240 |
drmp3d_synth(grbuf + i, pcm + 32*nch*i, nch, lins + i*64); |
| 2241 |
} |
| 2242 |
#ifndef DR_MP3_NONSTANDARD_BUT_LOGICAL |
| 2243 |
if (nch == 1) |
| 2244 |
{ |
| 2245 |
for (i = 0; i < 15*64; i += 2) |
| 2246 |
{ |
| 2247 |
qmf_state[i] = lins[nbands*64 + i]; |
| 2248 |
} |
| 2249 |
} else |
| 2250 |
#endif |
| 2251 |
{ |
| 2252 |
DRMP3_COPY_MEMORY(qmf_state, lins + nbands*64, sizeof(float)*15*64); |
| 2253 |
} |
| 2254 |
} |
| 2255 |
|
| 2256 |
static int drmp3d_match_frame(const drmp3_uint8 *hdr, int mp3_bytes, int frame_bytes) |
| 2257 |
{ |
| 2258 |
int i, nmatch; |
| 2259 |
for (i = 0, nmatch = 0; nmatch < DRMP3_MAX_FRAME_SYNC_MATCHES; nmatch++) |
| 2260 |
{ |
| 2261 |
i += drmp3_hdr_frame_bytes(hdr + i, frame_bytes) + drmp3_hdr_padding(hdr + i); |
| 2262 |
if (i + DRMP3_HDR_SIZE > mp3_bytes) |
| 2263 |
return nmatch > 0; |
| 2264 |
if (!drmp3_hdr_compare(hdr, hdr + i)) |
| 2265 |
return 0; |
| 2266 |
} |
| 2267 |
return 1; |
| 2268 |
} |
| 2269 |
|
| 2270 |
static int drmp3d_find_frame(const drmp3_uint8 *mp3, int mp3_bytes, int *free_format_bytes, int *ptr_frame_bytes) |
| 2271 |
{ |
| 2272 |
int i, k; |
| 2273 |
for (i = 0; i < mp3_bytes - DRMP3_HDR_SIZE; i++, mp3++) |
| 2274 |
{ |
| 2275 |
if (drmp3_hdr_valid(mp3)) |
| 2276 |
{ |
| 2277 |
int frame_bytes = drmp3_hdr_frame_bytes(mp3, *free_format_bytes); |
| 2278 |
int frame_and_padding = frame_bytes + drmp3_hdr_padding(mp3); |
| 2279 |
|
| 2280 |
for (k = DRMP3_HDR_SIZE; !frame_bytes && k < DRMP3_MAX_FREE_FORMAT_FRAME_SIZE && i + 2*k < mp3_bytes - DRMP3_HDR_SIZE; k++) |
| 2281 |
{ |
| 2282 |
if (drmp3_hdr_compare(mp3, mp3 + k)) |
| 2283 |
{ |
| 2284 |
int fb = k - drmp3_hdr_padding(mp3); |
| 2285 |
int nextfb = fb + drmp3_hdr_padding(mp3 + k); |
| 2286 |
if (i + k + nextfb + DRMP3_HDR_SIZE > mp3_bytes || !drmp3_hdr_compare(mp3, mp3 + k + nextfb)) |
| 2287 |
continue; |
| 2288 |
frame_and_padding = k; |
| 2289 |
frame_bytes = fb; |
| 2290 |
*free_format_bytes = fb; |
| 2291 |
} |
| 2292 |
} |
| 2293 |
|
| 2294 |
if ((frame_bytes && i + frame_and_padding <= mp3_bytes && |
| 2295 |
drmp3d_match_frame(mp3, mp3_bytes - i, frame_bytes)) || |
| 2296 |
(!i && frame_and_padding == mp3_bytes)) |
| 2297 |
{ |
| 2298 |
*ptr_frame_bytes = frame_and_padding; |
| 2299 |
return i; |
| 2300 |
} |
| 2301 |
*free_format_bytes = 0; |
| 2302 |
} |
| 2303 |
} |
| 2304 |
*ptr_frame_bytes = 0; |
| 2305 |
return mp3_bytes; |
| 2306 |
} |
| 2307 |
|
| 2308 |
DRMP3_API void drmp3dec_init(drmp3dec *dec) |
| 2309 |
{ |
| 2310 |
dec->header[0] = 0; |
| 2311 |
} |
| 2312 |
|
| 2313 |
DRMP3_API int drmp3dec_decode_frame(drmp3dec *dec, const drmp3_uint8 *mp3, int mp3_bytes, void *pcm, drmp3dec_frame_info *info) |
| 2314 |
{ |
| 2315 |
int i = 0, igr, frame_size = 0, success = 1; |
| 2316 |
const drmp3_uint8 *hdr; |
| 2317 |
drmp3_bs bs_frame[1]; |
| 2318 |
|
| 2319 |
if (mp3_bytes > 4 && dec->header[0] == 0xff && drmp3_hdr_compare(dec->header, mp3)) |
| 2320 |
{ |
| 2321 |
frame_size = drmp3_hdr_frame_bytes(mp3, dec->free_format_bytes) + drmp3_hdr_padding(mp3); |
| 2322 |
if (frame_size != mp3_bytes && (frame_size + DRMP3_HDR_SIZE > mp3_bytes || !drmp3_hdr_compare(mp3, mp3 + frame_size))) |
| 2323 |
{ |
| 2324 |
frame_size = 0; |
| 2325 |
} |
| 2326 |
} |
| 2327 |
if (!frame_size) |
| 2328 |
{ |
| 2329 |
DRMP3_ZERO_MEMORY(dec, sizeof(drmp3dec)); |
| 2330 |
i = drmp3d_find_frame(mp3, mp3_bytes, &dec->free_format_bytes, &frame_size); |
| 2331 |
if (!frame_size || i + frame_size > mp3_bytes) |
| 2332 |
{ |
| 2333 |
info->frame_bytes = i; |
| 2334 |
return 0; |
| 2335 |
} |
| 2336 |
} |
| 2337 |
|
| 2338 |
hdr = mp3 + i; |
| 2339 |
DRMP3_COPY_MEMORY(dec->header, hdr, DRMP3_HDR_SIZE); |
| 2340 |
info->frame_bytes = i + frame_size; |
| 2341 |
info->channels = DRMP3_HDR_IS_MONO(hdr) ? 1 : 2; |
| 2342 |
info->sample_rate = drmp3_hdr_sample_rate_hz(hdr); |
| 2343 |
info->layer = 4 - DRMP3_HDR_GET_LAYER(hdr); |
| 2344 |
info->bitrate_kbps = drmp3_hdr_bitrate_kbps(hdr); |
| 2345 |
|
| 2346 |
drmp3_bs_init(bs_frame, hdr + DRMP3_HDR_SIZE, frame_size - DRMP3_HDR_SIZE); |
| 2347 |
if (DRMP3_HDR_IS_CRC(hdr)) |
| 2348 |
{ |
| 2349 |
drmp3_bs_get_bits(bs_frame, 16); |
| 2350 |
} |
| 2351 |
|
| 2352 |
if (info->layer == 3) |
| 2353 |
{ |
| 2354 |
int main_data_begin = drmp3_L3_read_side_info(bs_frame, dec->scratch.gr_info, hdr); |
| 2355 |
if (main_data_begin < 0 || bs_frame->pos > bs_frame->limit) |
| 2356 |
{ |
| 2357 |
drmp3dec_init(dec); |
| 2358 |
return 0; |
| 2359 |
} |
| 2360 |
success = drmp3_L3_restore_reservoir(dec, bs_frame, &dec->scratch, main_data_begin); |
| 2361 |
if (success && pcm != NULL) |
| 2362 |
{ |
| 2363 |
for (igr = 0; igr < (DRMP3_HDR_TEST_MPEG1(hdr) ? 2 : 1); igr++, pcm = DRMP3_OFFSET_PTR(pcm, sizeof(drmp3d_sample_t)*576*info->channels)) |
| 2364 |
{ |
| 2365 |
DRMP3_ZERO_MEMORY(dec->scratch.grbuf[0], 576*2*sizeof(float)); |
| 2366 |
drmp3_L3_decode(dec, &dec->scratch, dec->scratch.gr_info + igr*info->channels, info->channels); |
| 2367 |
drmp3d_synth_granule(dec->qmf_state, dec->scratch.grbuf[0], 18, info->channels, (drmp3d_sample_t*)pcm, dec->scratch.syn[0]); |
| 2368 |
} |
| 2369 |
} |
| 2370 |
drmp3_L3_save_reservoir(dec, &dec->scratch); |
| 2371 |
} else |
| 2372 |
{ |
| 2373 |
#ifdef DR_MP3_ONLY_MP3 |
| 2374 |
return 0; |
| 2375 |
#else |
| 2376 |
drmp3_L12_scale_info sci[1]; |
| 2377 |
|
| 2378 |
if (pcm == NULL) { |
| 2379 |
return drmp3_hdr_frame_samples(hdr); |
| 2380 |
} |
| 2381 |
|
| 2382 |
drmp3_L12_read_scale_info(hdr, bs_frame, sci); |
| 2383 |
|
| 2384 |
DRMP3_ZERO_MEMORY(dec->scratch.grbuf[0], 576*2*sizeof(float)); |
| 2385 |
for (i = 0, igr = 0; igr < 3; igr++) |
| 2386 |
{ |
| 2387 |
if (12 == (i += drmp3_L12_dequantize_granule(dec->scratch.grbuf[0] + i, bs_frame, sci, info->layer | 1))) |
| 2388 |
{ |
| 2389 |
i = 0; |
| 2390 |
drmp3_L12_apply_scf_384(sci, sci->scf + igr, dec->scratch.grbuf[0]); |
| 2391 |
drmp3d_synth_granule(dec->qmf_state, dec->scratch.grbuf[0], 12, info->channels, (drmp3d_sample_t*)pcm, dec->scratch.syn[0]); |
| 2392 |
DRMP3_ZERO_MEMORY(dec->scratch.grbuf[0], 576*2*sizeof(float)); |
| 2393 |
pcm = DRMP3_OFFSET_PTR(pcm, sizeof(drmp3d_sample_t)*384*info->channels); |
| 2394 |
} |
| 2395 |
if (bs_frame->pos > bs_frame->limit) |
| 2396 |
{ |
| 2397 |
drmp3dec_init(dec); |
| 2398 |
return 0; |
| 2399 |
} |
| 2400 |
} |
| 2401 |
#endif |
| 2402 |
} |
| 2403 |
|
| 2404 |
return success*drmp3_hdr_frame_samples(dec->header); |
| 2405 |
} |
| 2406 |
|
| 2407 |
DRMP3_API void drmp3dec_f32_to_s16(const float *in, drmp3_int16 *out, size_t num_samples) |
| 2408 |
{ |
| 2409 |
size_t i = 0; |
| 2410 |
#if DRMP3_HAVE_SIMD |
| 2411 |
size_t aligned_count = num_samples & ~7; |
| 2412 |
for(; i < aligned_count; i+=8) |
| 2413 |
{ |
| 2414 |
drmp3_f4 scale = DRMP3_VSET(32768.0f); |
| 2415 |
drmp3_f4 a = DRMP3_VMUL(DRMP3_VLD(&in[i ]), scale); |
| 2416 |
drmp3_f4 b = DRMP3_VMUL(DRMP3_VLD(&in[i+4]), scale); |
| 2417 |
#if DRMP3_HAVE_SSE |
| 2418 |
drmp3_f4 s16max = DRMP3_VSET( 32767.0f); |
| 2419 |
drmp3_f4 s16min = DRMP3_VSET(-32768.0f); |
| 2420 |
__m128i pcm8 = _mm_packs_epi32(_mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(a, s16max), s16min)), |
| 2421 |
_mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(b, s16max), s16min))); |
| 2422 |
out[i ] = (drmp3_int16)_mm_extract_epi16(pcm8, 0); |
| 2423 |
out[i+1] = (drmp3_int16)_mm_extract_epi16(pcm8, 1); |
| 2424 |
out[i+2] = (drmp3_int16)_mm_extract_epi16(pcm8, 2); |
| 2425 |
out[i+3] = (drmp3_int16)_mm_extract_epi16(pcm8, 3); |
| 2426 |
out[i+4] = (drmp3_int16)_mm_extract_epi16(pcm8, 4); |
| 2427 |
out[i+5] = (drmp3_int16)_mm_extract_epi16(pcm8, 5); |
| 2428 |
out[i+6] = (drmp3_int16)_mm_extract_epi16(pcm8, 6); |
| 2429 |
out[i+7] = (drmp3_int16)_mm_extract_epi16(pcm8, 7); |
| 2430 |
#else |
| 2431 |
int16x4_t pcma, pcmb; |
| 2432 |
a = DRMP3_VADD(a, DRMP3_VSET(0.5f)); |
| 2433 |
b = DRMP3_VADD(b, DRMP3_VSET(0.5f)); |
| 2434 |
pcma = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(a), vreinterpretq_s32_u32(vcltq_f32(a, DRMP3_VSET(0))))); |
| 2435 |
pcmb = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(b), vreinterpretq_s32_u32(vcltq_f32(b, DRMP3_VSET(0))))); |
| 2436 |
vst1_lane_s16(out+i , pcma, 0); |
| 2437 |
vst1_lane_s16(out+i+1, pcma, 1); |
| 2438 |
vst1_lane_s16(out+i+2, pcma, 2); |
| 2439 |
vst1_lane_s16(out+i+3, pcma, 3); |
| 2440 |
vst1_lane_s16(out+i+4, pcmb, 0); |
| 2441 |
vst1_lane_s16(out+i+5, pcmb, 1); |
| 2442 |
vst1_lane_s16(out+i+6, pcmb, 2); |
| 2443 |
vst1_lane_s16(out+i+7, pcmb, 3); |
| 2444 |
#endif |
| 2445 |
} |
| 2446 |
#endif |
| 2447 |
for(; i < num_samples; i++) |
| 2448 |
{ |
| 2449 |
float sample = in[i] * 32768.0f; |
| 2450 |
if (sample >= 32766.5f) |
| 2451 |
out[i] = (drmp3_int16) 32767; |
| 2452 |
else if (sample <= -32767.5f) |
| 2453 |
out[i] = (drmp3_int16)-32768; |
| 2454 |
else |
| 2455 |
{ |
| 2456 |
short s = (drmp3_int16)(sample + .5f); |
| 2457 |
s -= (s < 0); |
| 2458 |
out[i] = s; |
| 2459 |
} |
| 2460 |
} |
| 2461 |
} |
| 2462 |
|
| 2463 |
|
| 2464 |
|
| 2465 |
|
| 2466 |
|
| 2467 |
|
| 2468 |
|
| 2469 |
|
| 2470 |
|
| 2471 |
#if defined(SIZE_MAX) |
| 2472 |
#define DRMP3_SIZE_MAX SIZE_MAX |
| 2473 |
#else |
| 2474 |
#if defined(_WIN64) || defined(_LP64) || defined(__LP64__) |
| 2475 |
#define DRMP3_SIZE_MAX ((drmp3_uint64)0xFFFFFFFFFFFFFFFF) |
| 2476 |
#else |
| 2477 |
#define DRMP3_SIZE_MAX 0xFFFFFFFF |
| 2478 |
#endif |
| 2479 |
#endif |
| 2480 |
|
| 2481 |
|
| 2482 |
|
| 2483 |
#ifndef DRMP3_SEEK_LEADING_MP3_FRAMES |
| 2484 |
#define DRMP3_SEEK_LEADING_MP3_FRAMES 2 |
| 2485 |
#endif |
| 2486 |
|
| 2487 |
#define DRMP3_MIN_DATA_CHUNK_SIZE 16384 |
| 2488 |
|
| 2489 |
|
| 2490 |
#ifndef DRMP3_DATA_CHUNK_SIZE |
| 2491 |
#define DRMP3_DATA_CHUNK_SIZE (DRMP3_MIN_DATA_CHUNK_SIZE*4) |
| 2492 |
#endif |
| 2493 |
|
| 2494 |
|
| 2495 |
#define DRMP3_COUNTOF(x) (sizeof(x) / sizeof(x[0])) |
| 2496 |
#define DRMP3_CLAMP(x, lo, hi) (DRMP3_MAX(lo, DRMP3_MIN(x, hi))) |
| 2497 |
|
| 2498 |
#ifndef DRMP3_PI_D |
| 2499 |
#define DRMP3_PI_D 3.14159265358979323846264 |
| 2500 |
#endif |
| 2501 |
|
| 2502 |
#define DRMP3_DEFAULT_RESAMPLER_LPF_ORDER 2 |
| 2503 |
|
| 2504 |
static DRMP3_INLINE float drmp3_mix_f32(float x, float y, float a) |
| 2505 |
{ |
| 2506 |
return x*(1-a) + y*a; |
| 2507 |
} |
| 2508 |
static DRMP3_INLINE float drmp3_mix_f32_fast(float x, float y, float a) |
| 2509 |
{ |
| 2510 |
float r0 = (y - x); |
| 2511 |
float r1 = r0*a; |
| 2512 |
return x + r1; |
| 2513 |
|
| 2514 |
} |
| 2515 |
|
| 2516 |
|
| 2517 |
|
| 2518 |
|
| 2519 |
|
| 2520 |
static DRMP3_INLINE drmp3_uint32 drmp3_gcf_u32(drmp3_uint32 a, drmp3_uint32 b) |
| 2521 |
{ |
| 2522 |
for (;;) { |
| 2523 |
if (b == 0) { |
| 2524 |
break; |
| 2525 |
} else { |
| 2526 |
drmp3_uint32 t = a; |
| 2527 |
a = b; |
| 2528 |
b = t % a; |
| 2529 |
} |
| 2530 |
} |
| 2531 |
|
| 2532 |
return a; |
| 2533 |
} |
| 2534 |
|
| 2535 |
|
| 2536 |
static void* drmp3__malloc_default(size_t sz, void* pUserData) |
| 2537 |
{ |
| 2538 |
(void)pUserData; |
| 2539 |
return DRMP3_MALLOC(sz); |
| 2540 |
} |
| 2541 |
|
| 2542 |
static void* drmp3__realloc_default(void* p, size_t sz, void* pUserData) |
| 2543 |
{ |
| 2544 |
(void)pUserData; |
| 2545 |
return DRMP3_REALLOC(p, sz); |
| 2546 |
} |
| 2547 |
|
| 2548 |
static void drmp3__free_default(void* p, void* pUserData) |
| 2549 |
{ |
| 2550 |
(void)pUserData; |
| 2551 |
DRMP3_FREE(p); |
| 2552 |
} |
| 2553 |
|
| 2554 |
|
| 2555 |
static void* drmp3__malloc_from_callbacks(size_t sz, const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 2556 |
{ |
| 2557 |
if (pAllocationCallbacks == NULL) { |
| 2558 |
return NULL; |
| 2559 |
} |
| 2560 |
|
| 2561 |
if (pAllocationCallbacks->onMalloc != NULL) { |
| 2562 |
return pAllocationCallbacks->onMalloc(sz, pAllocationCallbacks->pUserData); |
| 2563 |
} |
| 2564 |
|
| 2565 |
|
| 2566 |
if (pAllocationCallbacks->onRealloc != NULL) { |
| 2567 |
return pAllocationCallbacks->onRealloc(NULL, sz, pAllocationCallbacks->pUserData); |
| 2568 |
} |
| 2569 |
|
| 2570 |
return NULL; |
| 2571 |
} |
| 2572 |
|
| 2573 |
static void* drmp3__realloc_from_callbacks(void* p, size_t szNew, size_t szOld, const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 2574 |
{ |
| 2575 |
if (pAllocationCallbacks == NULL) { |
| 2576 |
return NULL; |
| 2577 |
} |
| 2578 |
|
| 2579 |
if (pAllocationCallbacks->onRealloc != NULL) { |
| 2580 |
return pAllocationCallbacks->onRealloc(p, szNew, pAllocationCallbacks->pUserData); |
| 2581 |
} |
| 2582 |
|
| 2583 |
|
| 2584 |
if (pAllocationCallbacks->onMalloc != NULL && pAllocationCallbacks->onFree != NULL) { |
| 2585 |
void* p2; |
| 2586 |
|
| 2587 |
p2 = pAllocationCallbacks->onMalloc(szNew, pAllocationCallbacks->pUserData); |
| 2588 |
if (p2 == NULL) { |
| 2589 |
return NULL; |
| 2590 |
} |
| 2591 |
|
| 2592 |
if (p != NULL) { |
| 2593 |
DRMP3_COPY_MEMORY(p2, p, szOld); |
| 2594 |
pAllocationCallbacks->onFree(p, pAllocationCallbacks->pUserData); |
| 2595 |
} |
| 2596 |
|
| 2597 |
return p2; |
| 2598 |
} |
| 2599 |
|
| 2600 |
return NULL; |
| 2601 |
} |
| 2602 |
|
| 2603 |
static void drmp3__free_from_callbacks(void* p, const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 2604 |
{ |
| 2605 |
if (p == NULL || pAllocationCallbacks == NULL) { |
| 2606 |
return; |
| 2607 |
} |
| 2608 |
|
| 2609 |
if (pAllocationCallbacks->onFree != NULL) { |
| 2610 |
pAllocationCallbacks->onFree(p, pAllocationCallbacks->pUserData); |
| 2611 |
} |
| 2612 |
} |
| 2613 |
|
| 2614 |
|
| 2615 |
static drmp3_allocation_callbacks drmp3_copy_allocation_callbacks_or_defaults(const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 2616 |
{ |
| 2617 |
if (pAllocationCallbacks != NULL) { |
| 2618 |
|
| 2619 |
return *pAllocationCallbacks; |
| 2620 |
} else { |
| 2621 |
|
| 2622 |
drmp3_allocation_callbacks allocationCallbacks; |
| 2623 |
allocationCallbacks.pUserData = NULL; |
| 2624 |
allocationCallbacks.onMalloc = drmp3__malloc_default; |
| 2625 |
allocationCallbacks.onRealloc = drmp3__realloc_default; |
| 2626 |
allocationCallbacks.onFree = drmp3__free_default; |
| 2627 |
return allocationCallbacks; |
| 2628 |
} |
| 2629 |
} |
| 2630 |
|
| 2631 |
|
| 2632 |
|
| 2633 |
static size_t drmp3__on_read(drmp3* pMP3, void* pBufferOut, size_t bytesToRead) |
| 2634 |
{ |
| 2635 |
size_t bytesRead; |
| 2636 |
|
| 2637 |
DRMP3_ASSERT(pMP3 != NULL); |
| 2638 |
DRMP3_ASSERT(pMP3->onRead != NULL); |
| 2639 |
|
| 2640 |
|
| 2641 |
|
| 2642 |
|
| 2643 |
|
| 2644 |
if (bytesToRead == 0) { |
| 2645 |
return 0; |
| 2646 |
} |
| 2647 |
|
| 2648 |
bytesRead = pMP3->onRead(pMP3->pUserData, pBufferOut, bytesToRead); |
| 2649 |
pMP3->streamCursor += bytesRead; |
| 2650 |
|
| 2651 |
return bytesRead; |
| 2652 |
} |
| 2653 |
|
| 2654 |
static size_t drmp3__on_read_clamped(drmp3* pMP3, void* pBufferOut, size_t bytesToRead) |
| 2655 |
{ |
| 2656 |
DRMP3_ASSERT(pMP3 != NULL); |
| 2657 |
DRMP3_ASSERT(pMP3->onRead != NULL); |
| 2658 |
|
| 2659 |
if (pMP3->streamLength == DRMP3_UINT64_MAX) { |
| 2660 |
return drmp3__on_read(pMP3, pBufferOut, bytesToRead); |
| 2661 |
} else { |
| 2662 |
drmp3_uint64 bytesRemaining; |
| 2663 |
|
| 2664 |
bytesRemaining = (pMP3->streamLength - pMP3->streamCursor); |
| 2665 |
if (bytesToRead > bytesRemaining) { |
| 2666 |
bytesToRead = (size_t)bytesRemaining; |
| 2667 |
} |
| 2668 |
|
| 2669 |
return drmp3__on_read(pMP3, pBufferOut, bytesToRead); |
| 2670 |
} |
| 2671 |
} |
| 2672 |
|
| 2673 |
static drmp3_bool32 drmp3__on_seek(drmp3* pMP3, int offset, drmp3_seek_origin origin) |
| 2674 |
{ |
| 2675 |
DRMP3_ASSERT(offset >= 0); |
| 2676 |
DRMP3_ASSERT(origin == DRMP3_SEEK_SET || origin == DRMP3_SEEK_CUR); |
| 2677 |
|
| 2678 |
if (!pMP3->onSeek(pMP3->pUserData, offset, origin)) { |
| 2679 |
return DRMP3_FALSE; |
| 2680 |
} |
| 2681 |
|
| 2682 |
if (origin == DRMP3_SEEK_SET) { |
| 2683 |
pMP3->streamCursor = (drmp3_uint64)offset; |
| 2684 |
} else{ |
| 2685 |
pMP3->streamCursor += offset; |
| 2686 |
} |
| 2687 |
|
| 2688 |
return DRMP3_TRUE; |
| 2689 |
} |
| 2690 |
|
| 2691 |
static drmp3_bool32 drmp3__on_seek_64(drmp3* pMP3, drmp3_uint64 offset, drmp3_seek_origin origin) |
| 2692 |
{ |
| 2693 |
if (offset <= 0x7FFFFFFF) { |
| 2694 |
return drmp3__on_seek(pMP3, (int)offset, origin); |
| 2695 |
} |
| 2696 |
|
| 2697 |
|
| 2698 |
if (!drmp3__on_seek(pMP3, 0x7FFFFFFF, DRMP3_SEEK_SET)) { |
| 2699 |
return DRMP3_FALSE; |
| 2700 |
} |
| 2701 |
|
| 2702 |
offset -= 0x7FFFFFFF; |
| 2703 |
while (offset > 0) { |
| 2704 |
if (offset <= 0x7FFFFFFF) { |
| 2705 |
if (!drmp3__on_seek(pMP3, (int)offset, DRMP3_SEEK_CUR)) { |
| 2706 |
return DRMP3_FALSE; |
| 2707 |
} |
| 2708 |
offset = 0; |
| 2709 |
} else { |
| 2710 |
if (!drmp3__on_seek(pMP3, 0x7FFFFFFF, DRMP3_SEEK_CUR)) { |
| 2711 |
return DRMP3_FALSE; |
| 2712 |
} |
| 2713 |
offset -= 0x7FFFFFFF; |
| 2714 |
} |
| 2715 |
} |
| 2716 |
|
| 2717 |
return DRMP3_TRUE; |
| 2718 |
} |
| 2719 |
|
| 2720 |
static void drmp3__on_meta(drmp3* pMP3, drmp3_metadata_type type, const void* pRawData, size_t rawDataSize) |
| 2721 |
{ |
| 2722 |
if (pMP3->onMeta) { |
| 2723 |
drmp3_metadata metadata; |
| 2724 |
|
| 2725 |
DRMP3_ZERO_OBJECT(&metadata); |
| 2726 |
metadata.type = type; |
| 2727 |
metadata.pRawData = pRawData; |
| 2728 |
metadata.rawDataSize = rawDataSize; |
| 2729 |
|
| 2730 |
pMP3->onMeta(pMP3->pUserDataMeta, &metadata); |
| 2731 |
} |
| 2732 |
} |
| 2733 |
|
| 2734 |
|
| 2735 |
static drmp3_uint32 drmp3_decode_next_frame_ex__callbacks(drmp3* pMP3, drmp3d_sample_t* pPCMFrames, drmp3dec_frame_info* pMP3FrameInfo, const drmp3_uint8** ppMP3FrameData) |
| 2736 |
{ |
| 2737 |
drmp3_uint32 pcmFramesRead = 0; |
| 2738 |
|
| 2739 |
DRMP3_ASSERT(pMP3 != NULL); |
| 2740 |
DRMP3_ASSERT(pMP3->onRead != NULL); |
| 2741 |
|
| 2742 |
if (pMP3->atEnd) { |
| 2743 |
return 0; |
| 2744 |
} |
| 2745 |
|
| 2746 |
for (;;) { |
| 2747 |
drmp3dec_frame_info info; |
| 2748 |
|
| 2749 |
|
| 2750 |
if (pMP3->dataSize < DRMP3_MIN_DATA_CHUNK_SIZE) { |
| 2751 |
size_t bytesRead; |
| 2752 |
|
| 2753 |
|
| 2754 |
if (pMP3->pData != NULL) { |
| 2755 |
DRMP3_MOVE_MEMORY(pMP3->pData, pMP3->pData + pMP3->dataConsumed, pMP3->dataSize); |
| 2756 |
} |
| 2757 |
|
| 2758 |
pMP3->dataConsumed = 0; |
| 2759 |
|
| 2760 |
if (pMP3->dataCapacity < DRMP3_DATA_CHUNK_SIZE) { |
| 2761 |
drmp3_uint8* pNewData; |
| 2762 |
size_t newDataCap; |
| 2763 |
|
| 2764 |
newDataCap = DRMP3_DATA_CHUNK_SIZE; |
| 2765 |
|
| 2766 |
pNewData = (drmp3_uint8*)drmp3__realloc_from_callbacks(pMP3->pData, newDataCap, pMP3->dataCapacity, &pMP3->allocationCallbacks); |
| 2767 |
if (pNewData == NULL) { |
| 2768 |
return 0; |
| 2769 |
} |
| 2770 |
|
| 2771 |
pMP3->pData = pNewData; |
| 2772 |
pMP3->dataCapacity = newDataCap; |
| 2773 |
} |
| 2774 |
|
| 2775 |
bytesRead = drmp3__on_read_clamped(pMP3, pMP3->pData + pMP3->dataSize, (pMP3->dataCapacity - pMP3->dataSize)); |
| 2776 |
if (bytesRead == 0) { |
| 2777 |
if (pMP3->dataSize == 0) { |
| 2778 |
pMP3->atEnd = DRMP3_TRUE; |
| 2779 |
return 0; |
| 2780 |
} |
| 2781 |
} |
| 2782 |
|
| 2783 |
pMP3->dataSize += bytesRead; |
| 2784 |
} |
| 2785 |
|
| 2786 |
if (pMP3->dataSize > INT_MAX) { |
| 2787 |
pMP3->atEnd = DRMP3_TRUE; |
| 2788 |
return 0; |
| 2789 |
} |
| 2790 |
|
| 2791 |
DRMP3_ASSERT(pMP3->pData != NULL); |
| 2792 |
DRMP3_ASSERT(pMP3->dataCapacity > 0); |
| 2793 |
|
| 2794 |
|
| 2795 |
if (pMP3->pData == NULL) { |
| 2796 |
return 0; |
| 2797 |
} |
| 2798 |
|
| 2799 |
pcmFramesRead = drmp3dec_decode_frame(&pMP3->decoder, pMP3->pData + pMP3->dataConsumed, (int)pMP3->dataSize, pPCMFrames, &info); |
| 2800 |
|
| 2801 |
|
| 2802 |
pMP3->dataConsumed += (size_t)info.frame_bytes; |
| 2803 |
pMP3->dataSize -= (size_t)info.frame_bytes; |
| 2804 |
|
| 2805 |
|
| 2806 |
if (pcmFramesRead > 0) { |
| 2807 |
pcmFramesRead = drmp3_hdr_frame_samples(pMP3->decoder.header); |
| 2808 |
pMP3->pcmFramesConsumedInMP3Frame = 0; |
| 2809 |
pMP3->pcmFramesRemainingInMP3Frame = pcmFramesRead; |
| 2810 |
pMP3->mp3FrameChannels = info.channels; |
| 2811 |
pMP3->mp3FrameSampleRate = info.sample_rate; |
| 2812 |
|
| 2813 |
if (pMP3FrameInfo != NULL) { |
| 2814 |
*pMP3FrameInfo = info; |
| 2815 |
} |
| 2816 |
|
| 2817 |
if (ppMP3FrameData != NULL) { |
| 2818 |
*ppMP3FrameData = pMP3->pData + pMP3->dataConsumed - (size_t)info.frame_bytes; |
| 2819 |
} |
| 2820 |
|
| 2821 |
break; |
| 2822 |
} else if (info.frame_bytes == 0) { |
| 2823 |
|
| 2824 |
size_t bytesRead; |
| 2825 |
|
| 2826 |
|
| 2827 |
DRMP3_MOVE_MEMORY(pMP3->pData, pMP3->pData + pMP3->dataConsumed, pMP3->dataSize); |
| 2828 |
pMP3->dataConsumed = 0; |
| 2829 |
|
| 2830 |
if (pMP3->dataCapacity == pMP3->dataSize) { |
| 2831 |
|
| 2832 |
drmp3_uint8* pNewData; |
| 2833 |
size_t newDataCap; |
| 2834 |
|
| 2835 |
newDataCap = pMP3->dataCapacity + DRMP3_DATA_CHUNK_SIZE; |
| 2836 |
|
| 2837 |
pNewData = (drmp3_uint8*)drmp3__realloc_from_callbacks(pMP3->pData, newDataCap, pMP3->dataCapacity, &pMP3->allocationCallbacks); |
| 2838 |
if (pNewData == NULL) { |
| 2839 |
return 0; |
| 2840 |
} |
| 2841 |
|
| 2842 |
pMP3->pData = pNewData; |
| 2843 |
pMP3->dataCapacity = newDataCap; |
| 2844 |
} |
| 2845 |
|
| 2846 |
|
| 2847 |
bytesRead = drmp3__on_read_clamped(pMP3, pMP3->pData + pMP3->dataSize, (pMP3->dataCapacity - pMP3->dataSize)); |
| 2848 |
if (bytesRead == 0) { |
| 2849 |
pMP3->atEnd = DRMP3_TRUE; |
| 2850 |
return 0; |
| 2851 |
} |
| 2852 |
|
| 2853 |
pMP3->dataSize += bytesRead; |
| 2854 |
} |
| 2855 |
}; |
| 2856 |
|
| 2857 |
return pcmFramesRead; |
| 2858 |
} |
| 2859 |
|
| 2860 |
static drmp3_uint32 drmp3_decode_next_frame_ex__memory(drmp3* pMP3, drmp3d_sample_t* pPCMFrames, drmp3dec_frame_info* pMP3FrameInfo, const drmp3_uint8** ppMP3FrameData) |
| 2861 |
{ |
| 2862 |
drmp3_uint32 pcmFramesRead = 0; |
| 2863 |
drmp3dec_frame_info info; |
| 2864 |
|
| 2865 |
DRMP3_ASSERT(pMP3 != NULL); |
| 2866 |
DRMP3_ASSERT(pMP3->memory.pData != NULL); |
| 2867 |
|
| 2868 |
if (pMP3->atEnd) { |
| 2869 |
return 0; |
| 2870 |
} |
| 2871 |
|
| 2872 |
for (;;) { |
| 2873 |
pcmFramesRead = drmp3dec_decode_frame(&pMP3->decoder, pMP3->memory.pData + pMP3->memory.currentReadPos, (int)(pMP3->memory.dataSize - pMP3->memory.currentReadPos), pPCMFrames, &info); |
| 2874 |
if (pcmFramesRead > 0) { |
| 2875 |
pcmFramesRead = drmp3_hdr_frame_samples(pMP3->decoder.header); |
| 2876 |
pMP3->pcmFramesConsumedInMP3Frame = 0; |
| 2877 |
pMP3->pcmFramesRemainingInMP3Frame = pcmFramesRead; |
| 2878 |
pMP3->mp3FrameChannels = info.channels; |
| 2879 |
pMP3->mp3FrameSampleRate = info.sample_rate; |
| 2880 |
|
| 2881 |
if (pMP3FrameInfo != NULL) { |
| 2882 |
*pMP3FrameInfo = info; |
| 2883 |
} |
| 2884 |
|
| 2885 |
if (ppMP3FrameData != NULL) { |
| 2886 |
*ppMP3FrameData = pMP3->memory.pData + pMP3->memory.currentReadPos; |
| 2887 |
} |
| 2888 |
|
| 2889 |
break; |
| 2890 |
} else if (info.frame_bytes > 0) { |
| 2891 |
|
| 2892 |
pMP3->memory.currentReadPos += (size_t)info.frame_bytes; |
| 2893 |
pMP3->streamCursor += (size_t)info.frame_bytes; |
| 2894 |
} else { |
| 2895 |
|
| 2896 |
break; |
| 2897 |
} |
| 2898 |
} |
| 2899 |
|
| 2900 |
|
| 2901 |
pMP3->memory.currentReadPos += (size_t)info.frame_bytes; |
| 2902 |
pMP3->streamCursor += (size_t)info.frame_bytes; |
| 2903 |
|
| 2904 |
return pcmFramesRead; |
| 2905 |
} |
| 2906 |
|
| 2907 |
static drmp3_uint32 drmp3_decode_next_frame_ex(drmp3* pMP3, drmp3d_sample_t* pPCMFrames, drmp3dec_frame_info* pMP3FrameInfo, const drmp3_uint8** ppMP3FrameData) |
| 2908 |
{ |
| 2909 |
if (pMP3->memory.pData != NULL && pMP3->memory.dataSize > 0) { |
| 2910 |
return drmp3_decode_next_frame_ex__memory(pMP3, pPCMFrames, pMP3FrameInfo, ppMP3FrameData); |
| 2911 |
} else { |
| 2912 |
return drmp3_decode_next_frame_ex__callbacks(pMP3, pPCMFrames, pMP3FrameInfo, ppMP3FrameData); |
| 2913 |
} |
| 2914 |
} |
| 2915 |
|
| 2916 |
static drmp3_uint32 drmp3_decode_next_frame(drmp3* pMP3) |
| 2917 |
{ |
| 2918 |
DRMP3_ASSERT(pMP3 != NULL); |
| 2919 |
return drmp3_decode_next_frame_ex(pMP3, (drmp3d_sample_t*)pMP3->pcmFrames, NULL, NULL); |
| 2920 |
} |
| 2921 |
|
| 2922 |
#if 0 |
| 2923 |
static drmp3_uint32 drmp3_seek_next_frame(drmp3* pMP3) |
| 2924 |
{ |
| 2925 |
drmp3_uint32 pcmFrameCount; |
| 2926 |
|
| 2927 |
DRMP3_ASSERT(pMP3 != NULL); |
| 2928 |
|
| 2929 |
pcmFrameCount = drmp3_decode_next_frame_ex(pMP3, NULL, NULL, NULL); |
| 2930 |
if (pcmFrameCount == 0) { |
| 2931 |
return 0; |
| 2932 |
} |
| 2933 |
|
| 2934 |
|
| 2935 |
pMP3->currentPCMFrame += pcmFrameCount; |
| 2936 |
pMP3->pcmFramesConsumedInMP3Frame = pcmFrameCount; |
| 2937 |
pMP3->pcmFramesRemainingInMP3Frame = 0; |
| 2938 |
|
| 2939 |
return pcmFrameCount; |
| 2940 |
} |
| 2941 |
#endif |
| 2942 |
|
| 2943 |
static drmp3_bool32 drmp3_init_internal(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, drmp3_meta_proc onMeta, void* pUserData, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 2944 |
{ |
| 2945 |
drmp3dec_frame_info firstFrameInfo; |
| 2946 |
const drmp3_uint8* pFirstFrameData; |
| 2947 |
drmp3_uint32 firstFramePCMFrameCount; |
| 2948 |
drmp3_uint32 detectedMP3FrameCount = 0xFFFFFFFF; |
| 2949 |
|
| 2950 |
DRMP3_ASSERT(pMP3 != NULL); |
| 2951 |
DRMP3_ASSERT(onRead != NULL); |
| 2952 |
|
| 2953 |
|
| 2954 |
drmp3dec_init(&pMP3->decoder); |
| 2955 |
|
| 2956 |
pMP3->onRead = onRead; |
| 2957 |
pMP3->onSeek = onSeek; |
| 2958 |
pMP3->onMeta = onMeta; |
| 2959 |
pMP3->pUserData = pUserData; |
| 2960 |
pMP3->pUserDataMeta = pUserDataMeta; |
| 2961 |
pMP3->allocationCallbacks = drmp3_copy_allocation_callbacks_or_defaults(pAllocationCallbacks); |
| 2962 |
|
| 2963 |
if (pMP3->allocationCallbacks.onFree == NULL || (pMP3->allocationCallbacks.onMalloc == NULL && pMP3->allocationCallbacks.onRealloc == NULL)) { |
| 2964 |
return DRMP3_FALSE; |
| 2965 |
} |
| 2966 |
|
| 2967 |
pMP3->streamCursor = 0; |
| 2968 |
pMP3->streamLength = DRMP3_UINT64_MAX; |
| 2969 |
pMP3->streamStartOffset = 0; |
| 2970 |
pMP3->delayInPCMFrames = 0; |
| 2971 |
pMP3->paddingInPCMFrames = 0; |
| 2972 |
pMP3->totalPCMFrameCount = DRMP3_UINT64_MAX; |
| 2973 |
|
| 2974 |
|
| 2975 |
#if 1 |
| 2976 |
if (onSeek != NULL && onTell != NULL) { |
| 2977 |
if (onSeek(pUserData, 0, DRMP3_SEEK_END)) { |
| 2978 |
drmp3_int64 streamLen; |
| 2979 |
int streamEndOffset = 0; |
| 2980 |
|
| 2981 |
|
| 2982 |
if (onTell(pUserData, &streamLen)) { |
| 2983 |
|
| 2984 |
if (streamLen > 128) { |
| 2985 |
char id3[3]; |
| 2986 |
if (onSeek(pUserData, streamEndOffset - 128, DRMP3_SEEK_END)) { |
| 2987 |
if (onRead(pUserData, id3, 3) == 3 && id3[0] == 'T' && id3[1] == 'A' && id3[2] == 'G') { |
| 2988 |
|
| 2989 |
streamEndOffset -= 128; |
| 2990 |
streamLen -= 128; |
| 2991 |
|
| 2992 |
|
| 2993 |
if (onMeta != NULL) { |
| 2994 |
drmp3_uint8 tag[128]; |
| 2995 |
tag[0] = 'T'; tag[1] = 'A'; tag[2] = 'G'; |
| 2996 |
|
| 2997 |
if (onRead(pUserData, tag + 3, 125) == 125) { |
| 2998 |
drmp3__on_meta(pMP3, DRMP3_METADATA_TYPE_ID3V1, tag, 128); |
| 2999 |
} |
| 3000 |
} |
| 3001 |
} else { |
| 3002 |
|
| 3003 |
} |
| 3004 |
} else { |
| 3005 |
|
| 3006 |
} |
| 3007 |
} else { |
| 3008 |
|
| 3009 |
} |
| 3010 |
|
| 3011 |
|
| 3012 |
if (streamLen > 32) { |
| 3013 |
char ape[32]; |
| 3014 |
if (onSeek(pUserData, streamEndOffset - 32, DRMP3_SEEK_END)) { |
| 3015 |
if (onRead(pUserData, ape, 32) == 32 && ape[0] == 'A' && ape[1] == 'P' && ape[2] == 'E' && ape[3] == 'T' && ape[4] == 'A' && ape[5] == 'G' && ape[6] == 'E' && ape[7] == 'X') { |
| 3016 |
|
| 3017 |
drmp3_uint32 tagSize = |
| 3018 |
((drmp3_uint32)ape[24] << 0) | |
| 3019 |
((drmp3_uint32)ape[25] << 8) | |
| 3020 |
((drmp3_uint32)ape[26] << 16) | |
| 3021 |
((drmp3_uint32)ape[27] << 24); |
| 3022 |
|
| 3023 |
if (32 + tagSize < streamLen) { |
| 3024 |
streamEndOffset -= 32 + tagSize; |
| 3025 |
streamLen -= 32 + tagSize; |
| 3026 |
|
| 3027 |
|
| 3028 |
if (onMeta != NULL) { |
| 3029 |
|
| 3030 |
if (onSeek(pUserData, streamEndOffset, DRMP3_SEEK_END)) { |
| 3031 |
size_t apeTagSize = (size_t)tagSize + 32; |
| 3032 |
drmp3_uint8* pTagData = (drmp3_uint8*)drmp3_malloc(apeTagSize, pAllocationCallbacks); |
| 3033 |
if (pTagData != NULL) { |
| 3034 |
if (onRead(pUserData, pTagData, apeTagSize) == apeTagSize) { |
| 3035 |
drmp3__on_meta(pMP3, DRMP3_METADATA_TYPE_APE, pTagData, apeTagSize); |
| 3036 |
} |
| 3037 |
|
| 3038 |
drmp3_free(pTagData, pAllocationCallbacks); |
| 3039 |
} |
| 3040 |
} |
| 3041 |
} |
| 3042 |
} else { |
| 3043 |
|
| 3044 |
} |
| 3045 |
} |
| 3046 |
} |
| 3047 |
} else { |
| 3048 |
|
| 3049 |
} |
| 3050 |
|
| 3051 |
|
| 3052 |
if (!onSeek(pUserData, 0, DRMP3_SEEK_SET)) { |
| 3053 |
return DRMP3_FALSE; |
| 3054 |
} |
| 3055 |
|
| 3056 |
pMP3->streamLength = (drmp3_uint64)streamLen; |
| 3057 |
|
| 3058 |
if (pMP3->memory.pData != NULL) { |
| 3059 |
pMP3->memory.dataSize = (size_t)pMP3->streamLength; |
| 3060 |
} |
| 3061 |
} else { |
| 3062 |
|
| 3063 |
if (!onSeek(pUserData, 0, DRMP3_SEEK_SET)) { |
| 3064 |
return DRMP3_FALSE; |
| 3065 |
} |
| 3066 |
} |
| 3067 |
} else { |
| 3068 |
|
| 3069 |
} |
| 3070 |
} else { |
| 3071 |
|
| 3072 |
} |
| 3073 |
#endif |
| 3074 |
|
| 3075 |
|
| 3076 |
|
| 3077 |
#if 1 |
| 3078 |
{ |
| 3079 |
char header[10]; |
| 3080 |
if (onRead(pUserData, header, 10) == 10) { |
| 3081 |
if (header[0] == 'I' && header[1] == 'D' && header[2] == '3') { |
| 3082 |
drmp3_uint32 tagSize = |
| 3083 |
(((drmp3_uint32)header[6] & 0x7F) << 21) | |
| 3084 |
(((drmp3_uint32)header[7] & 0x7F) << 14) | |
| 3085 |
(((drmp3_uint32)header[8] & 0x7F) << 7) | |
| 3086 |
(((drmp3_uint32)header[9] & 0x7F) << 0); |
| 3087 |
|
| 3088 |
|
| 3089 |
if (header[5] & 0x10) { |
| 3090 |
tagSize += 10; |
| 3091 |
} |
| 3092 |
|
| 3093 |
|
| 3094 |
if (onMeta != NULL) { |
| 3095 |
size_t tagSizeWithHeader = 10 + tagSize; |
| 3096 |
drmp3_uint8* pTagData = (drmp3_uint8*)drmp3_malloc(tagSizeWithHeader, pAllocationCallbacks); |
| 3097 |
if (pTagData != NULL) { |
| 3098 |
DRMP3_COPY_MEMORY(pTagData, header, 10); |
| 3099 |
|
| 3100 |
if (onRead(pUserData, pTagData + 10, tagSize) == tagSize) { |
| 3101 |
drmp3__on_meta(pMP3, DRMP3_METADATA_TYPE_ID3V2, pTagData, tagSizeWithHeader); |
| 3102 |
} |
| 3103 |
|
| 3104 |
drmp3_free(pTagData, pAllocationCallbacks); |
| 3105 |
} |
| 3106 |
} else { |
| 3107 |
|
| 3108 |
if (onSeek != NULL) { |
| 3109 |
if (!onSeek(pUserData, tagSize, DRMP3_SEEK_CUR)) { |
| 3110 |
return DRMP3_FALSE; |
| 3111 |
} |
| 3112 |
} else { |
| 3113 |
|
| 3114 |
char discard[1024]; |
| 3115 |
|
| 3116 |
while (tagSize > 0) { |
| 3117 |
size_t bytesToRead = tagSize; |
| 3118 |
if (bytesToRead > sizeof(discard)) { |
| 3119 |
bytesToRead = sizeof(discard); |
| 3120 |
} |
| 3121 |
|
| 3122 |
if (onRead(pUserData, discard, bytesToRead) != bytesToRead) { |
| 3123 |
return DRMP3_FALSE; |
| 3124 |
} |
| 3125 |
|
| 3126 |
tagSize -= (drmp3_uint32)bytesToRead; |
| 3127 |
} |
| 3128 |
} |
| 3129 |
} |
| 3130 |
|
| 3131 |
pMP3->streamStartOffset += 10 + tagSize; |
| 3132 |
pMP3->streamCursor = pMP3->streamStartOffset; |
| 3133 |
} else { |
| 3134 |
|
| 3135 |
if (onSeek != NULL) { |
| 3136 |
if (!onSeek(pUserData, 0, DRMP3_SEEK_SET)) { |
| 3137 |
return DRMP3_FALSE; |
| 3138 |
} |
| 3139 |
} else { |
| 3140 |
|
| 3141 |
|
| 3142 |
|
| 3143 |
|
| 3144 |
|
| 3145 |
|
| 3146 |
} |
| 3147 |
} |
| 3148 |
} else { |
| 3149 |
|
| 3150 |
return DRMP3_FALSE; |
| 3151 |
} |
| 3152 |
} |
| 3153 |
#endif |
| 3154 |
|
| 3155 |
|
| 3156 |
|
| 3157 |
|
| 3158 |
|
| 3159 |
firstFramePCMFrameCount = drmp3_decode_next_frame_ex(pMP3, (drmp3d_sample_t*)pMP3->pcmFrames, &firstFrameInfo, &pFirstFrameData); |
| 3160 |
if (firstFramePCMFrameCount > 0) { |
| 3161 |
DRMP3_ASSERT(pFirstFrameData != NULL); |
| 3162 |
|
| 3163 |
|
| 3164 |
|
| 3165 |
|
| 3166 |
|
| 3167 |
|
| 3168 |
|
| 3169 |
|
| 3170 |
#if 1 |
| 3171 |
DRMP3_ASSERT(firstFrameInfo.frame_bytes > 0); |
| 3172 |
{ |
| 3173 |
drmp3_bs bs; |
| 3174 |
drmp3_L3_gr_info grInfo[4]; |
| 3175 |
|
| 3176 |
drmp3_bs_init(&bs, pFirstFrameData + DRMP3_HDR_SIZE, firstFrameInfo.frame_bytes - DRMP3_HDR_SIZE); |
| 3177 |
|
| 3178 |
if (DRMP3_HDR_IS_CRC(pFirstFrameData)) { |
| 3179 |
drmp3_bs_get_bits(&bs, 16); |
| 3180 |
} |
| 3181 |
|
| 3182 |
if (drmp3_L3_read_side_info(&bs, grInfo, pFirstFrameData) >= 0) { |
| 3183 |
drmp3_bool32 isXing = DRMP3_FALSE; |
| 3184 |
drmp3_bool32 isInfo = DRMP3_FALSE; |
| 3185 |
const drmp3_uint8* pTagData; |
| 3186 |
const drmp3_uint8* pTagDataBeg; |
| 3187 |
|
| 3188 |
pTagDataBeg = pFirstFrameData + DRMP3_HDR_SIZE + (bs.pos/8); |
| 3189 |
pTagData = pTagDataBeg; |
| 3190 |
|
| 3191 |
|
| 3192 |
isXing = (pTagData[0] == 'X' && pTagData[1] == 'i' && pTagData[2] == 'n' && pTagData[3] == 'g'); |
| 3193 |
isInfo = (pTagData[0] == 'I' && pTagData[1] == 'n' && pTagData[2] == 'f' && pTagData[3] == 'o'); |
| 3194 |
|
| 3195 |
if (isXing || isInfo) { |
| 3196 |
drmp3_uint32 bytes = 0; |
| 3197 |
drmp3_uint32 flags = pTagData[7]; |
| 3198 |
|
| 3199 |
pTagData += 8; |
| 3200 |
|
| 3201 |
if (flags & 0x01) { |
| 3202 |
detectedMP3FrameCount = (drmp3_uint32)pTagData[0] << 24 | (drmp3_uint32)pTagData[1] << 16 | (drmp3_uint32)pTagData[2] << 8 | (drmp3_uint32)pTagData[3]; |
| 3203 |
pTagData += 4; |
| 3204 |
} |
| 3205 |
|
| 3206 |
if (flags & 0x02) { |
| 3207 |
bytes = (drmp3_uint32)pTagData[0] << 24 | (drmp3_uint32)pTagData[1] << 16 | (drmp3_uint32)pTagData[2] << 8 | (drmp3_uint32)pTagData[3]; |
| 3208 |
(void)bytes; |
| 3209 |
pTagData += 4; |
| 3210 |
} |
| 3211 |
|
| 3212 |
if (flags & 0x04) { |
| 3213 |
|
| 3214 |
pTagData += 100; |
| 3215 |
} |
| 3216 |
|
| 3217 |
if (flags & 0x08) { |
| 3218 |
pTagData += 4; |
| 3219 |
} |
| 3220 |
|
| 3221 |
|
| 3222 |
if (pTagData[0]) { |
| 3223 |
pTagData += 21; |
| 3224 |
|
| 3225 |
if (pTagData - pFirstFrameData + 14 < firstFrameInfo.frame_bytes) { |
| 3226 |
int delayInPCMFrames; |
| 3227 |
int paddingInPCMFrames; |
| 3228 |
|
| 3229 |
delayInPCMFrames = (( (drmp3_uint32)pTagData[0] << 4) | ((drmp3_uint32)pTagData[1] >> 4)) + (528 + 1); |
| 3230 |
paddingInPCMFrames = ((((drmp3_uint32)pTagData[1] & 0xF) << 8) | ((drmp3_uint32)pTagData[2] )) - (528 + 1); |
| 3231 |
if (paddingInPCMFrames < 0) { |
| 3232 |
paddingInPCMFrames = 0; |
| 3233 |
} |
| 3234 |
|
| 3235 |
pMP3->delayInPCMFrames = (drmp3_uint32)delayInPCMFrames; |
| 3236 |
pMP3->paddingInPCMFrames = (drmp3_uint32)paddingInPCMFrames; |
| 3237 |
} |
| 3238 |
} |
| 3239 |
|
| 3240 |
|
| 3241 |
|
| 3242 |
|
| 3243 |
|
| 3244 |
|
| 3245 |
if (isXing) { |
| 3246 |
pMP3->isVBR = DRMP3_TRUE; |
| 3247 |
} else if (isInfo) { |
| 3248 |
pMP3->isCBR = DRMP3_TRUE; |
| 3249 |
} |
| 3250 |
|
| 3251 |
|
| 3252 |
if (onMeta != NULL) { |
| 3253 |
drmp3_metadata_type metadataType = isXing ? DRMP3_METADATA_TYPE_XING : DRMP3_METADATA_TYPE_VBRI; |
| 3254 |
size_t tagDataSize; |
| 3255 |
|
| 3256 |
tagDataSize = (size_t)firstFrameInfo.frame_bytes; |
| 3257 |
tagDataSize -= (size_t)(pTagDataBeg - pFirstFrameData); |
| 3258 |
|
| 3259 |
drmp3__on_meta(pMP3, metadataType, pTagDataBeg, tagDataSize); |
| 3260 |
} |
| 3261 |
|
| 3262 |
|
| 3263 |
pMP3->pcmFramesRemainingInMP3Frame = 0; |
| 3264 |
|
| 3265 |
|
| 3266 |
pMP3->streamStartOffset += (drmp3_uint32)(firstFrameInfo.frame_bytes); |
| 3267 |
pMP3->streamCursor = pMP3->streamStartOffset; |
| 3268 |
|
| 3269 |
|
| 3270 |
|
| 3271 |
|
| 3272 |
|
| 3273 |
|
| 3274 |
drmp3dec_init(&pMP3->decoder); |
| 3275 |
} |
| 3276 |
} else { |
| 3277 |
|
| 3278 |
} |
| 3279 |
} |
| 3280 |
#endif |
| 3281 |
} else { |
| 3282 |
|
| 3283 |
drmp3__free_from_callbacks(pMP3->pData, &pMP3->allocationCallbacks); |
| 3284 |
return DRMP3_FALSE; |
| 3285 |
} |
| 3286 |
|
| 3287 |
if (detectedMP3FrameCount != 0xFFFFFFFF) { |
| 3288 |
pMP3->totalPCMFrameCount = detectedMP3FrameCount * firstFramePCMFrameCount; |
| 3289 |
} |
| 3290 |
|
| 3291 |
pMP3->channels = pMP3->mp3FrameChannels; |
| 3292 |
pMP3->sampleRate = pMP3->mp3FrameSampleRate; |
| 3293 |
|
| 3294 |
return DRMP3_TRUE; |
| 3295 |
} |
| 3296 |
|
| 3297 |
DRMP3_API drmp3_bool32 drmp3_init(drmp3* pMP3, drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, drmp3_meta_proc onMeta, void* pUserData, const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 3298 |
{ |
| 3299 |
if (pMP3 == NULL || onRead == NULL) { |
| 3300 |
return DRMP3_FALSE; |
| 3301 |
} |
| 3302 |
|
| 3303 |
DRMP3_ZERO_OBJECT(pMP3); |
| 3304 |
return drmp3_init_internal(pMP3, onRead, onSeek, onTell, onMeta, pUserData, pUserData, pAllocationCallbacks); |
| 3305 |
} |
| 3306 |
|
| 3307 |
|
| 3308 |
static size_t drmp3__on_read_memory(void* pUserData, void* pBufferOut, size_t bytesToRead) |
| 3309 |
{ |
| 3310 |
drmp3* pMP3 = (drmp3*)pUserData; |
| 3311 |
size_t bytesRemaining; |
| 3312 |
|
| 3313 |
DRMP3_ASSERT(pMP3 != NULL); |
| 3314 |
DRMP3_ASSERT(pMP3->memory.dataSize >= pMP3->memory.currentReadPos); |
| 3315 |
|
| 3316 |
bytesRemaining = pMP3->memory.dataSize - pMP3->memory.currentReadPos; |
| 3317 |
if (bytesToRead > bytesRemaining) { |
| 3318 |
bytesToRead = bytesRemaining; |
| 3319 |
} |
| 3320 |
|
| 3321 |
if (bytesToRead > 0) { |
| 3322 |
DRMP3_COPY_MEMORY(pBufferOut, pMP3->memory.pData + pMP3->memory.currentReadPos, bytesToRead); |
| 3323 |
pMP3->memory.currentReadPos += bytesToRead; |
| 3324 |
} |
| 3325 |
|
| 3326 |
return bytesToRead; |
| 3327 |
} |
| 3328 |
|
| 3329 |
static drmp3_bool32 drmp3__on_seek_memory(void* pUserData, int byteOffset, drmp3_seek_origin origin) |
| 3330 |
{ |
| 3331 |
drmp3* pMP3 = (drmp3*)pUserData; |
| 3332 |
drmp3_int64 newCursor; |
| 3333 |
|
| 3334 |
DRMP3_ASSERT(pMP3 != NULL); |
| 3335 |
|
| 3336 |
if (origin == DRMP3_SEEK_SET) { |
| 3337 |
newCursor = 0; |
| 3338 |
} else if (origin == DRMP3_SEEK_CUR) { |
| 3339 |
newCursor = (drmp3_int64)pMP3->memory.currentReadPos; |
| 3340 |
} else if (origin == DRMP3_SEEK_END) { |
| 3341 |
newCursor = (drmp3_int64)pMP3->memory.dataSize; |
| 3342 |
} else { |
| 3343 |
DRMP3_ASSERT(!"Invalid seek origin"); |
| 3344 |
return DRMP3_FALSE; |
| 3345 |
} |
| 3346 |
|
| 3347 |
newCursor += byteOffset; |
| 3348 |
|
| 3349 |
if (newCursor < 0) { |
| 3350 |
return DRMP3_FALSE; |
| 3351 |
} |
| 3352 |
if ((size_t)newCursor > pMP3->memory.dataSize) { |
| 3353 |
return DRMP3_FALSE; |
| 3354 |
} |
| 3355 |
|
| 3356 |
pMP3->memory.currentReadPos = (size_t)newCursor; |
| 3357 |
|
| 3358 |
return DRMP3_TRUE; |
| 3359 |
} |
| 3360 |
|
| 3361 |
static drmp3_bool32 drmp3__on_tell_memory(void* pUserData, drmp3_int64* pCursor) |
| 3362 |
{ |
| 3363 |
drmp3* pMP3 = (drmp3*)pUserData; |
| 3364 |
|
| 3365 |
DRMP3_ASSERT(pMP3 != NULL); |
| 3366 |
DRMP3_ASSERT(pCursor != NULL); |
| 3367 |
|
| 3368 |
*pCursor = (drmp3_int64)pMP3->memory.currentReadPos; |
| 3369 |
return DRMP3_TRUE; |
| 3370 |
} |
| 3371 |
|
| 3372 |
DRMP3_API drmp3_bool32 drmp3_init_memory_with_metadata(drmp3* pMP3, const void* pData, size_t dataSize, drmp3_meta_proc onMeta, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 3373 |
{ |
| 3374 |
drmp3_bool32 result; |
| 3375 |
|
| 3376 |
if (pMP3 == NULL) { |
| 3377 |
return DRMP3_FALSE; |
| 3378 |
} |
| 3379 |
|
| 3380 |
DRMP3_ZERO_OBJECT(pMP3); |
| 3381 |
|
| 3382 |
if (pData == NULL || dataSize == 0) { |
| 3383 |
return DRMP3_FALSE; |
| 3384 |
} |
| 3385 |
|
| 3386 |
pMP3->memory.pData = (const drmp3_uint8*)pData; |
| 3387 |
pMP3->memory.dataSize = dataSize; |
| 3388 |
pMP3->memory.currentReadPos = 0; |
| 3389 |
|
| 3390 |
result = drmp3_init_internal(pMP3, drmp3__on_read_memory, drmp3__on_seek_memory, drmp3__on_tell_memory, onMeta, pMP3, pUserDataMeta, pAllocationCallbacks); |
| 3391 |
if (result == DRMP3_FALSE) { |
| 3392 |
return DRMP3_FALSE; |
| 3393 |
} |
| 3394 |
|
| 3395 |
|
| 3396 |
if (pMP3->streamLength <= (drmp3_uint64)DRMP3_SIZE_MAX) { |
| 3397 |
pMP3->memory.dataSize = (size_t)pMP3->streamLength; |
| 3398 |
} |
| 3399 |
|
| 3400 |
if (pMP3->streamStartOffset > (drmp3_uint64)DRMP3_SIZE_MAX) { |
| 3401 |
return DRMP3_FALSE; |
| 3402 |
} |
| 3403 |
|
| 3404 |
return DRMP3_TRUE; |
| 3405 |
} |
| 3406 |
|
| 3407 |
DRMP3_API drmp3_bool32 drmp3_init_memory(drmp3* pMP3, const void* pData, size_t dataSize, const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 3408 |
{ |
| 3409 |
return drmp3_init_memory_with_metadata(pMP3, pData, dataSize, NULL, NULL, pAllocationCallbacks); |
| 3410 |
} |
| 3411 |
|
| 3412 |
|
| 3413 |
#ifndef DR_MP3_NO_STDIO |
| 3414 |
#include <stdio.h> |
| 3415 |
#include <wchar.h> |
| 3416 |
|
| 3417 |
|
| 3418 |
|
| 3419 |
#include <errno.h> |
| 3420 |
static drmp3_result drmp3_result_from_errno(int e) |
| 3421 |
{ |
| 3422 |
switch (e) |
| 3423 |
{ |
| 3424 |
case 0: return DRMP3_SUCCESS; |
| 3425 |
#ifdef EPERM |
| 3426 |
case EPERM: return DRMP3_INVALID_OPERATION; |
| 3427 |
#endif |
| 3428 |
#ifdef ENOENT |
| 3429 |
case ENOENT: return DRMP3_DOES_NOT_EXIST; |
| 3430 |
#endif |
| 3431 |
#ifdef ESRCH |
| 3432 |
case ESRCH: return DRMP3_DOES_NOT_EXIST; |
| 3433 |
#endif |
| 3434 |
#ifdef EINTR |
| 3435 |
case EINTR: return DRMP3_INTERRUPT; |
| 3436 |
#endif |
| 3437 |
#ifdef EIO |
| 3438 |
case EIO: return DRMP3_IO_ERROR; |
| 3439 |
#endif |
| 3440 |
#ifdef ENXIO |
| 3441 |
case ENXIO: return DRMP3_DOES_NOT_EXIST; |
| 3442 |
#endif |
| 3443 |
#ifdef E2BIG |
| 3444 |
case E2BIG: return DRMP3_INVALID_ARGS; |
| 3445 |
#endif |
| 3446 |
#ifdef ENOEXEC |
| 3447 |
case ENOEXEC: return DRMP3_INVALID_FILE; |
| 3448 |
#endif |
| 3449 |
#ifdef EBADF |
| 3450 |
case EBADF: return DRMP3_INVALID_FILE; |
| 3451 |
#endif |
| 3452 |
#ifdef ECHILD |
| 3453 |
case ECHILD: return DRMP3_ERROR; |
| 3454 |
#endif |
| 3455 |
#ifdef EAGAIN |
| 3456 |
case EAGAIN: return DRMP3_UNAVAILABLE; |
| 3457 |
#endif |
| 3458 |
#ifdef ENOMEM |
| 3459 |
case ENOMEM: return DRMP3_OUT_OF_MEMORY; |
| 3460 |
#endif |
| 3461 |
#ifdef EACCES |
| 3462 |
case EACCES: return DRMP3_ACCESS_DENIED; |
| 3463 |
#endif |
| 3464 |
#ifdef EFAULT |
| 3465 |
case EFAULT: return DRMP3_BAD_ADDRESS; |
| 3466 |
#endif |
| 3467 |
#ifdef ENOTBLK |
| 3468 |
case ENOTBLK: return DRMP3_ERROR; |
| 3469 |
#endif |
| 3470 |
#ifdef EBUSY |
| 3471 |
case EBUSY: return DRMP3_BUSY; |
| 3472 |
#endif |
| 3473 |
#ifdef EEXIST |
| 3474 |
case EEXIST: return DRMP3_ALREADY_EXISTS; |
| 3475 |
#endif |
| 3476 |
#ifdef EXDEV |
| 3477 |
case EXDEV: return DRMP3_ERROR; |
| 3478 |
#endif |
| 3479 |
#ifdef ENODEV |
| 3480 |
case ENODEV: return DRMP3_DOES_NOT_EXIST; |
| 3481 |
#endif |
| 3482 |
#ifdef ENOTDIR |
| 3483 |
case ENOTDIR: return DRMP3_NOT_DIRECTORY; |
| 3484 |
#endif |
| 3485 |
#ifdef EISDIR |
| 3486 |
case EISDIR: return DRMP3_IS_DIRECTORY; |
| 3487 |
#endif |
| 3488 |
#ifdef EINVAL |
| 3489 |
case EINVAL: return DRMP3_INVALID_ARGS; |
| 3490 |
#endif |
| 3491 |
#ifdef ENFILE |
| 3492 |
case ENFILE: return DRMP3_TOO_MANY_OPEN_FILES; |
| 3493 |
#endif |
| 3494 |
#ifdef EMFILE |
| 3495 |
case EMFILE: return DRMP3_TOO_MANY_OPEN_FILES; |
| 3496 |
#endif |
| 3497 |
#ifdef ENOTTY |
| 3498 |
case ENOTTY: return DRMP3_INVALID_OPERATION; |
| 3499 |
#endif |
| 3500 |
#ifdef ETXTBSY |
| 3501 |
case ETXTBSY: return DRMP3_BUSY; |
| 3502 |
#endif |
| 3503 |
#ifdef EFBIG |
| 3504 |
case EFBIG: return DRMP3_TOO_BIG; |
| 3505 |
#endif |
| 3506 |
#ifdef ENOSPC |
| 3507 |
case ENOSPC: return DRMP3_NO_SPACE; |
| 3508 |
#endif |
| 3509 |
#ifdef ESPIPE |
| 3510 |
case ESPIPE: return DRMP3_BAD_SEEK; |
| 3511 |
#endif |
| 3512 |
#ifdef EROFS |
| 3513 |
case EROFS: return DRMP3_ACCESS_DENIED; |
| 3514 |
#endif |
| 3515 |
#ifdef EMLINK |
| 3516 |
case EMLINK: return DRMP3_TOO_MANY_LINKS; |
| 3517 |
#endif |
| 3518 |
#ifdef EPIPE |
| 3519 |
case EPIPE: return DRMP3_BAD_PIPE; |
| 3520 |
#endif |
| 3521 |
#ifdef EDOM |
| 3522 |
case EDOM: return DRMP3_OUT_OF_RANGE; |
| 3523 |
#endif |
| 3524 |
#ifdef ERANGE |
| 3525 |
case ERANGE: return DRMP3_OUT_OF_RANGE; |
| 3526 |
#endif |
| 3527 |
#ifdef EDEADLK |
| 3528 |
case EDEADLK: return DRMP3_DEADLOCK; |
| 3529 |
#endif |
| 3530 |
#ifdef ENAMETOOLONG |
| 3531 |
case ENAMETOOLONG: return DRMP3_PATH_TOO_LONG; |
| 3532 |
#endif |
| 3533 |
#ifdef ENOLCK |
| 3534 |
case ENOLCK: return DRMP3_ERROR; |
| 3535 |
#endif |
| 3536 |
#ifdef ENOSYS |
| 3537 |
case ENOSYS: return DRMP3_NOT_IMPLEMENTED; |
| 3538 |
#endif |
| 3539 |
#if defined(ENOTEMPTY) && ENOTEMPTY != EEXIST |
| 3540 |
case ENOTEMPTY: return DRMP3_DIRECTORY_NOT_EMPTY; |
| 3541 |
#endif |
| 3542 |
#ifdef ELOOP |
| 3543 |
case ELOOP: return DRMP3_TOO_MANY_LINKS; |
| 3544 |
#endif |
| 3545 |
#ifdef ENOMSG |
| 3546 |
case ENOMSG: return DRMP3_NO_MESSAGE; |
| 3547 |
#endif |
| 3548 |
#ifdef EIDRM |
| 3549 |
case EIDRM: return DRMP3_ERROR; |
| 3550 |
#endif |
| 3551 |
#ifdef ECHRNG |
| 3552 |
case ECHRNG: return DRMP3_ERROR; |
| 3553 |
#endif |
| 3554 |
#ifdef EL2NSYNC |
| 3555 |
case EL2NSYNC: return DRMP3_ERROR; |
| 3556 |
#endif |
| 3557 |
#ifdef EL3HLT |
| 3558 |
case EL3HLT: return DRMP3_ERROR; |
| 3559 |
#endif |
| 3560 |
#ifdef EL3RST |
| 3561 |
case EL3RST: return DRMP3_ERROR; |
| 3562 |
#endif |
| 3563 |
#ifdef ELNRNG |
| 3564 |
case ELNRNG: return DRMP3_OUT_OF_RANGE; |
| 3565 |
#endif |
| 3566 |
#ifdef EUNATCH |
| 3567 |
case EUNATCH: return DRMP3_ERROR; |
| 3568 |
#endif |
| 3569 |
#ifdef ENOCSI |
| 3570 |
case ENOCSI: return DRMP3_ERROR; |
| 3571 |
#endif |
| 3572 |
#ifdef EL2HLT |
| 3573 |
case EL2HLT: return DRMP3_ERROR; |
| 3574 |
#endif |
| 3575 |
#ifdef EBADE |
| 3576 |
case EBADE: return DRMP3_ERROR; |
| 3577 |
#endif |
| 3578 |
#ifdef EBADR |
| 3579 |
case EBADR: return DRMP3_ERROR; |
| 3580 |
#endif |
| 3581 |
#ifdef EXFULL |
| 3582 |
case EXFULL: return DRMP3_ERROR; |
| 3583 |
#endif |
| 3584 |
#ifdef ENOANO |
| 3585 |
case ENOANO: return DRMP3_ERROR; |
| 3586 |
#endif |
| 3587 |
#ifdef EBADRQC |
| 3588 |
case EBADRQC: return DRMP3_ERROR; |
| 3589 |
#endif |
| 3590 |
#ifdef EBADSLT |
| 3591 |
case EBADSLT: return DRMP3_ERROR; |
| 3592 |
#endif |
| 3593 |
#ifdef EBFONT |
| 3594 |
case EBFONT: return DRMP3_INVALID_FILE; |
| 3595 |
#endif |
| 3596 |
#ifdef ENOSTR |
| 3597 |
case ENOSTR: return DRMP3_ERROR; |
| 3598 |
#endif |
| 3599 |
#ifdef ENODATA |
| 3600 |
case ENODATA: return DRMP3_NO_DATA_AVAILABLE; |
| 3601 |
#endif |
| 3602 |
#ifdef ETIME |
| 3603 |
case ETIME: return DRMP3_TIMEOUT; |
| 3604 |
#endif |
| 3605 |
#ifdef ENOSR |
| 3606 |
case ENOSR: return DRMP3_NO_DATA_AVAILABLE; |
| 3607 |
#endif |
| 3608 |
#ifdef ENONET |
| 3609 |
case ENONET: return DRMP3_NO_NETWORK; |
| 3610 |
#endif |
| 3611 |
#ifdef ENOPKG |
| 3612 |
case ENOPKG: return DRMP3_ERROR; |
| 3613 |
#endif |
| 3614 |
#ifdef EREMOTE |
| 3615 |
case EREMOTE: return DRMP3_ERROR; |
| 3616 |
#endif |
| 3617 |
#ifdef ENOLINK |
| 3618 |
case ENOLINK: return DRMP3_ERROR; |
| 3619 |
#endif |
| 3620 |
#ifdef EADV |
| 3621 |
case EADV: return DRMP3_ERROR; |
| 3622 |
#endif |
| 3623 |
#ifdef ESRMNT |
| 3624 |
case ESRMNT: return DRMP3_ERROR; |
| 3625 |
#endif |
| 3626 |
#ifdef ECOMM |
| 3627 |
case ECOMM: return DRMP3_ERROR; |
| 3628 |
#endif |
| 3629 |
#ifdef EPROTO |
| 3630 |
case EPROTO: return DRMP3_ERROR; |
| 3631 |
#endif |
| 3632 |
#ifdef EMULTIHOP |
| 3633 |
case EMULTIHOP: return DRMP3_ERROR; |
| 3634 |
#endif |
| 3635 |
#ifdef EDOTDOT |
| 3636 |
case EDOTDOT: return DRMP3_ERROR; |
| 3637 |
#endif |
| 3638 |
#ifdef EBADMSG |
| 3639 |
case EBADMSG: return DRMP3_BAD_MESSAGE; |
| 3640 |
#endif |
| 3641 |
#ifdef EOVERFLOW |
| 3642 |
case EOVERFLOW: return DRMP3_TOO_BIG; |
| 3643 |
#endif |
| 3644 |
#ifdef ENOTUNIQ |
| 3645 |
case ENOTUNIQ: return DRMP3_NOT_UNIQUE; |
| 3646 |
#endif |
| 3647 |
#ifdef EBADFD |
| 3648 |
case EBADFD: return DRMP3_ERROR; |
| 3649 |
#endif |
| 3650 |
#ifdef EREMCHG |
| 3651 |
case EREMCHG: return DRMP3_ERROR; |
| 3652 |
#endif |
| 3653 |
#ifdef ELIBACC |
| 3654 |
case ELIBACC: return DRMP3_ACCESS_DENIED; |
| 3655 |
#endif |
| 3656 |
#ifdef ELIBBAD |
| 3657 |
case ELIBBAD: return DRMP3_INVALID_FILE; |
| 3658 |
#endif |
| 3659 |
#ifdef ELIBSCN |
| 3660 |
case ELIBSCN: return DRMP3_INVALID_FILE; |
| 3661 |
#endif |
| 3662 |
#ifdef ELIBMAX |
| 3663 |
case ELIBMAX: return DRMP3_ERROR; |
| 3664 |
#endif |
| 3665 |
#ifdef ELIBEXEC |
| 3666 |
case ELIBEXEC: return DRMP3_ERROR; |
| 3667 |
#endif |
| 3668 |
#ifdef EILSEQ |
| 3669 |
case EILSEQ: return DRMP3_INVALID_DATA; |
| 3670 |
#endif |
| 3671 |
#ifdef ERESTART |
| 3672 |
case ERESTART: return DRMP3_ERROR; |
| 3673 |
#endif |
| 3674 |
#ifdef ESTRPIPE |
| 3675 |
case ESTRPIPE: return DRMP3_ERROR; |
| 3676 |
#endif |
| 3677 |
#ifdef EUSERS |
| 3678 |
case EUSERS: return DRMP3_ERROR; |
| 3679 |
#endif |
| 3680 |
#ifdef ENOTSOCK |
| 3681 |
case ENOTSOCK: return DRMP3_NOT_SOCKET; |
| 3682 |
#endif |
| 3683 |
#ifdef EDESTADDRREQ |
| 3684 |
case EDESTADDRREQ: return DRMP3_NO_ADDRESS; |
| 3685 |
#endif |
| 3686 |
#ifdef EMSGSIZE |
| 3687 |
case EMSGSIZE: return DRMP3_TOO_BIG; |
| 3688 |
#endif |
| 3689 |
#ifdef EPROTOTYPE |
| 3690 |
case EPROTOTYPE: return DRMP3_BAD_PROTOCOL; |
| 3691 |
#endif |
| 3692 |
#ifdef ENOPROTOOPT |
| 3693 |
case ENOPROTOOPT: return DRMP3_PROTOCOL_UNAVAILABLE; |
| 3694 |
#endif |
| 3695 |
#ifdef EPROTONOSUPPORT |
| 3696 |
case EPROTONOSUPPORT: return DRMP3_PROTOCOL_NOT_SUPPORTED; |
| 3697 |
#endif |
| 3698 |
#ifdef ESOCKTNOSUPPORT |
| 3699 |
case ESOCKTNOSUPPORT: return DRMP3_SOCKET_NOT_SUPPORTED; |
| 3700 |
#endif |
| 3701 |
#ifdef EOPNOTSUPP |
| 3702 |
case EOPNOTSUPP: return DRMP3_INVALID_OPERATION; |
| 3703 |
#endif |
| 3704 |
#ifdef EPFNOSUPPORT |
| 3705 |
case EPFNOSUPPORT: return DRMP3_PROTOCOL_FAMILY_NOT_SUPPORTED; |
| 3706 |
#endif |
| 3707 |
#ifdef EAFNOSUPPORT |
| 3708 |
case EAFNOSUPPORT: return DRMP3_ADDRESS_FAMILY_NOT_SUPPORTED; |
| 3709 |
#endif |
| 3710 |
#ifdef EADDRINUSE |
| 3711 |
case EADDRINUSE: return DRMP3_ALREADY_IN_USE; |
| 3712 |
#endif |
| 3713 |
#ifdef EADDRNOTAVAIL |
| 3714 |
case EADDRNOTAVAIL: return DRMP3_ERROR; |
| 3715 |
#endif |
| 3716 |
#ifdef ENETDOWN |
| 3717 |
case ENETDOWN: return DRMP3_NO_NETWORK; |
| 3718 |
#endif |
| 3719 |
#ifdef ENETUNREACH |
| 3720 |
case ENETUNREACH: return DRMP3_NO_NETWORK; |
| 3721 |
#endif |
| 3722 |
#ifdef ENETRESET |
| 3723 |
case ENETRESET: return DRMP3_NO_NETWORK; |
| 3724 |
#endif |
| 3725 |
#ifdef ECONNABORTED |
| 3726 |
case ECONNABORTED: return DRMP3_NO_NETWORK; |
| 3727 |
#endif |
| 3728 |
#ifdef ECONNRESET |
| 3729 |
case ECONNRESET: return DRMP3_CONNECTION_RESET; |
| 3730 |
#endif |
| 3731 |
#ifdef ENOBUFS |
| 3732 |
case ENOBUFS: return DRMP3_NO_SPACE; |
| 3733 |
#endif |
| 3734 |
#ifdef EISCONN |
| 3735 |
case EISCONN: return DRMP3_ALREADY_CONNECTED; |
| 3736 |
#endif |
| 3737 |
#ifdef ENOTCONN |
| 3738 |
case ENOTCONN: return DRMP3_NOT_CONNECTED; |
| 3739 |
#endif |
| 3740 |
#ifdef ESHUTDOWN |
| 3741 |
case ESHUTDOWN: return DRMP3_ERROR; |
| 3742 |
#endif |
| 3743 |
#ifdef ETOOMANYREFS |
| 3744 |
case ETOOMANYREFS: return DRMP3_ERROR; |
| 3745 |
#endif |
| 3746 |
#ifdef ETIMEDOUT |
| 3747 |
case ETIMEDOUT: return DRMP3_TIMEOUT; |
| 3748 |
#endif |
| 3749 |
#ifdef ECONNREFUSED |
| 3750 |
case ECONNREFUSED: return DRMP3_CONNECTION_REFUSED; |
| 3751 |
#endif |
| 3752 |
#ifdef EHOSTDOWN |
| 3753 |
case EHOSTDOWN: return DRMP3_NO_HOST; |
| 3754 |
#endif |
| 3755 |
#ifdef EHOSTUNREACH |
| 3756 |
case EHOSTUNREACH: return DRMP3_NO_HOST; |
| 3757 |
#endif |
| 3758 |
#ifdef EALREADY |
| 3759 |
case EALREADY: return DRMP3_IN_PROGRESS; |
| 3760 |
#endif |
| 3761 |
#ifdef EINPROGRESS |
| 3762 |
case EINPROGRESS: return DRMP3_IN_PROGRESS; |
| 3763 |
#endif |
| 3764 |
#ifdef ESTALE |
| 3765 |
case ESTALE: return DRMP3_INVALID_FILE; |
| 3766 |
#endif |
| 3767 |
#ifdef EUCLEAN |
| 3768 |
case EUCLEAN: return DRMP3_ERROR; |
| 3769 |
#endif |
| 3770 |
#ifdef ENOTNAM |
| 3771 |
case ENOTNAM: return DRMP3_ERROR; |
| 3772 |
#endif |
| 3773 |
#ifdef ENAVAIL |
| 3774 |
case ENAVAIL: return DRMP3_ERROR; |
| 3775 |
#endif |
| 3776 |
#ifdef EISNAM |
| 3777 |
case EISNAM: return DRMP3_ERROR; |
| 3778 |
#endif |
| 3779 |
#ifdef EREMOTEIO |
| 3780 |
case EREMOTEIO: return DRMP3_IO_ERROR; |
| 3781 |
#endif |
| 3782 |
#ifdef EDQUOT |
| 3783 |
case EDQUOT: return DRMP3_NO_SPACE; |
| 3784 |
#endif |
| 3785 |
#ifdef ENOMEDIUM |
| 3786 |
case ENOMEDIUM: return DRMP3_DOES_NOT_EXIST; |
| 3787 |
#endif |
| 3788 |
#ifdef EMEDIUMTYPE |
| 3789 |
case EMEDIUMTYPE: return DRMP3_ERROR; |
| 3790 |
#endif |
| 3791 |
#ifdef ECANCELED |
| 3792 |
case ECANCELED: return DRMP3_CANCELLED; |
| 3793 |
#endif |
| 3794 |
#ifdef ENOKEY |
| 3795 |
case ENOKEY: return DRMP3_ERROR; |
| 3796 |
#endif |
| 3797 |
#ifdef EKEYEXPIRED |
| 3798 |
case EKEYEXPIRED: return DRMP3_ERROR; |
| 3799 |
#endif |
| 3800 |
#ifdef EKEYREVOKED |
| 3801 |
case EKEYREVOKED: return DRMP3_ERROR; |
| 3802 |
#endif |
| 3803 |
#ifdef EKEYREJECTED |
| 3804 |
case EKEYREJECTED: return DRMP3_ERROR; |
| 3805 |
#endif |
| 3806 |
#ifdef EOWNERDEAD |
| 3807 |
case EOWNERDEAD: return DRMP3_ERROR; |
| 3808 |
#endif |
| 3809 |
#ifdef ENOTRECOVERABLE |
| 3810 |
case ENOTRECOVERABLE: return DRMP3_ERROR; |
| 3811 |
#endif |
| 3812 |
#ifdef ERFKILL |
| 3813 |
case ERFKILL: return DRMP3_ERROR; |
| 3814 |
#endif |
| 3815 |
#ifdef EHWPOISON |
| 3816 |
case EHWPOISON: return DRMP3_ERROR; |
| 3817 |
#endif |
| 3818 |
default: return DRMP3_ERROR; |
| 3819 |
} |
| 3820 |
} |
| 3821 |
|
| 3822 |
|
| 3823 |
|
| 3824 |
static drmp3_result drmp3_fopen(FILE** ppFile, const char* pFilePath, const char* pOpenMode) |
| 3825 |
{ |
| 3826 |
#if defined(_MSC_VER) && _MSC_VER >= 1400 |
| 3827 |
errno_t err; |
| 3828 |
#endif |
| 3829 |
|
| 3830 |
if (ppFile != NULL) { |
| 3831 |
*ppFile = NULL; |
| 3832 |
} |
| 3833 |
|
| 3834 |
if (pFilePath == NULL || pOpenMode == NULL || ppFile == NULL) { |
| 3835 |
return DRMP3_INVALID_ARGS; |
| 3836 |
} |
| 3837 |
|
| 3838 |
#if defined(_MSC_VER) && _MSC_VER >= 1400 |
| 3839 |
err = fopen_s(ppFile, pFilePath, pOpenMode); |
| 3840 |
if (err != 0) { |
| 3841 |
return drmp3_result_from_errno(err); |
| 3842 |
} |
| 3843 |
#else |
| 3844 |
#if defined(_WIN32) || defined(__APPLE__) |
| 3845 |
*ppFile = fopen(pFilePath, pOpenMode); |
| 3846 |
#else |
| 3847 |
#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 && defined(_LARGEFILE64_SOURCE) |
| 3848 |
*ppFile = fopen64(pFilePath, pOpenMode); |
| 3849 |
#else |
| 3850 |
*ppFile = fopen(pFilePath, pOpenMode); |
| 3851 |
#endif |
| 3852 |
#endif |
| 3853 |
if (*ppFile == NULL) { |
| 3854 |
drmp3_result result = drmp3_result_from_errno(errno); |
| 3855 |
if (result == DRMP3_SUCCESS) { |
| 3856 |
result = DRMP3_ERROR; |
| 3857 |
} |
| 3858 |
|
| 3859 |
return result; |
| 3860 |
} |
| 3861 |
#endif |
| 3862 |
|
| 3863 |
return DRMP3_SUCCESS; |
| 3864 |
} |
| 3865 |
|
| 3866 |
|
| 3867 |
|
| 3868 |
|
| 3869 |
|
| 3870 |
|
| 3871 |
|
| 3872 |
|
| 3873 |
|
| 3874 |
|
| 3875 |
|
| 3876 |
|
| 3877 |
|
| 3878 |
#if defined(_WIN32) |
| 3879 |
#if defined(_MSC_VER) || defined(__MINGW64__) || (!defined(__STRICT_ANSI__) && !defined(_NO_EXT_KEYS)) |
| 3880 |
#define DRMP3_HAS_WFOPEN |
| 3881 |
#endif |
| 3882 |
#endif |
| 3883 |
|
| 3884 |
static drmp3_result drmp3_wfopen(FILE** ppFile, const wchar_t* pFilePath, const wchar_t* pOpenMode, const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 3885 |
{ |
| 3886 |
if (ppFile != NULL) { |
| 3887 |
*ppFile = NULL; |
| 3888 |
} |
| 3889 |
|
| 3890 |
if (pFilePath == NULL || pOpenMode == NULL || ppFile == NULL) { |
| 3891 |
return DRMP3_INVALID_ARGS; |
| 3892 |
} |
| 3893 |
|
| 3894 |
#if defined(DRMP3_HAS_WFOPEN) |
| 3895 |
{ |
| 3896 |
|
| 3897 |
#if defined(_MSC_VER) && _MSC_VER >= 1400 |
| 3898 |
errno_t err = _wfopen_s(ppFile, pFilePath, pOpenMode); |
| 3899 |
if (err != 0) { |
| 3900 |
return drmp3_result_from_errno(err); |
| 3901 |
} |
| 3902 |
#else |
| 3903 |
*ppFile = _wfopen(pFilePath, pOpenMode); |
| 3904 |
if (*ppFile == NULL) { |
| 3905 |
return drmp3_result_from_errno(errno); |
| 3906 |
} |
| 3907 |
#endif |
| 3908 |
(void)pAllocationCallbacks; |
| 3909 |
} |
| 3910 |
#else |
| 3911 |
|
| 3912 |
|
| 3913 |
|
| 3914 |
|
| 3915 |
|
| 3916 |
|
| 3917 |
|
| 3918 |
|
| 3919 |
|
| 3920 |
|
| 3921 |
|
| 3922 |
|
| 3923 |
|
| 3924 |
#if defined(__DJGPP__) |
| 3925 |
{ |
| 3926 |
|
| 3927 |
} |
| 3928 |
#else |
| 3929 |
{ |
| 3930 |
mbstate_t mbs; |
| 3931 |
size_t lenMB; |
| 3932 |
const wchar_t* pFilePathTemp = pFilePath; |
| 3933 |
char* pFilePathMB = NULL; |
| 3934 |
char pOpenModeMB[32] = {0}; |
| 3935 |
|
| 3936 |
|
| 3937 |
DRMP3_ZERO_OBJECT(&mbs); |
| 3938 |
lenMB = wcsrtombs(NULL, &pFilePathTemp, 0, &mbs); |
| 3939 |
if (lenMB == (size_t)-1) { |
| 3940 |
return drmp3_result_from_errno(errno); |
| 3941 |
} |
| 3942 |
|
| 3943 |
pFilePathMB = (char*)drmp3__malloc_from_callbacks(lenMB + 1, pAllocationCallbacks); |
| 3944 |
if (pFilePathMB == NULL) { |
| 3945 |
return DRMP3_OUT_OF_MEMORY; |
| 3946 |
} |
| 3947 |
|
| 3948 |
pFilePathTemp = pFilePath; |
| 3949 |
DRMP3_ZERO_OBJECT(&mbs); |
| 3950 |
wcsrtombs(pFilePathMB, &pFilePathTemp, lenMB + 1, &mbs); |
| 3951 |
|
| 3952 |
|
| 3953 |
{ |
| 3954 |
size_t i = 0; |
| 3955 |
for (;;) { |
| 3956 |
if (pOpenMode[i] == 0) { |
| 3957 |
pOpenModeMB[i] = '\0'; |
| 3958 |
break; |
| 3959 |
} |
| 3960 |
|
| 3961 |
pOpenModeMB[i] = (char)pOpenMode[i]; |
| 3962 |
i += 1; |
| 3963 |
} |
| 3964 |
} |
| 3965 |
|
| 3966 |
*ppFile = fopen(pFilePathMB, pOpenModeMB); |
| 3967 |
|
| 3968 |
drmp3__free_from_callbacks(pFilePathMB, pAllocationCallbacks); |
| 3969 |
} |
| 3970 |
#endif |
| 3971 |
|
| 3972 |
if (*ppFile == NULL) { |
| 3973 |
return DRMP3_ERROR; |
| 3974 |
} |
| 3975 |
#endif |
| 3976 |
|
| 3977 |
return DRMP3_SUCCESS; |
| 3978 |
} |
| 3979 |
|
| 3980 |
|
| 3981 |
|
| 3982 |
static size_t drmp3__on_read_stdio(void* pUserData, void* pBufferOut, size_t bytesToRead) |
| 3983 |
{ |
| 3984 |
return fread(pBufferOut, 1, bytesToRead, (FILE*)pUserData); |
| 3985 |
} |
| 3986 |
|
| 3987 |
static drmp3_bool32 drmp3__on_seek_stdio(void* pUserData, int offset, drmp3_seek_origin origin) |
| 3988 |
{ |
| 3989 |
int whence = SEEK_SET; |
| 3990 |
if (origin == DRMP3_SEEK_CUR) { |
| 3991 |
whence = SEEK_CUR; |
| 3992 |
} else if (origin == DRMP3_SEEK_END) { |
| 3993 |
whence = SEEK_END; |
| 3994 |
} |
| 3995 |
|
| 3996 |
return fseek((FILE*)pUserData, offset, whence) == 0; |
| 3997 |
} |
| 3998 |
|
| 3999 |
static drmp3_bool32 drmp3__on_tell_stdio(void* pUserData, drmp3_int64* pCursor) |
| 4000 |
{ |
| 4001 |
FILE* pFileStdio = (FILE*)pUserData; |
| 4002 |
drmp3_int64 result; |
| 4003 |
|
| 4004 |
|
| 4005 |
DRMP3_ASSERT(pFileStdio != NULL); |
| 4006 |
DRMP3_ASSERT(pCursor != NULL); |
| 4007 |
|
| 4008 |
#if defined(_WIN32) && !defined(NXDK) |
| 4009 |
#if defined(_MSC_VER) && _MSC_VER > 1200 |
| 4010 |
result = _ftelli64(pFileStdio); |
| 4011 |
#else |
| 4012 |
result = ftell(pFileStdio); |
| 4013 |
#endif |
| 4014 |
#else |
| 4015 |
result = ftell(pFileStdio); |
| 4016 |
#endif |
| 4017 |
|
| 4018 |
*pCursor = result; |
| 4019 |
|
| 4020 |
return DRMP3_TRUE; |
| 4021 |
} |
| 4022 |
|
| 4023 |
DRMP3_API drmp3_bool32 drmp3_init_file_with_metadata(drmp3* pMP3, const char* pFilePath, drmp3_meta_proc onMeta, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 4024 |
{ |
| 4025 |
drmp3_bool32 result; |
| 4026 |
FILE* pFile; |
| 4027 |
|
| 4028 |
if (pMP3 == NULL) { |
| 4029 |
return DRMP3_FALSE; |
| 4030 |
} |
| 4031 |
|
| 4032 |
DRMP3_ZERO_OBJECT(pMP3); |
| 4033 |
|
| 4034 |
if (drmp3_fopen(&pFile, pFilePath, "rb") != DRMP3_SUCCESS) { |
| 4035 |
return DRMP3_FALSE; |
| 4036 |
} |
| 4037 |
|
| 4038 |
result = drmp3_init_internal(pMP3, drmp3__on_read_stdio, drmp3__on_seek_stdio, drmp3__on_tell_stdio, onMeta, (void*)pFile, pUserDataMeta, pAllocationCallbacks); |
| 4039 |
if (result != DRMP3_TRUE) { |
| 4040 |
fclose(pFile); |
| 4041 |
return result; |
| 4042 |
} |
| 4043 |
|
| 4044 |
return DRMP3_TRUE; |
| 4045 |
} |
| 4046 |
|
| 4047 |
DRMP3_API drmp3_bool32 drmp3_init_file_with_metadata_w(drmp3* pMP3, const wchar_t* pFilePath, drmp3_meta_proc onMeta, void* pUserDataMeta, const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 4048 |
{ |
| 4049 |
drmp3_bool32 result; |
| 4050 |
FILE* pFile; |
| 4051 |
|
| 4052 |
if (pMP3 == NULL) { |
| 4053 |
return DRMP3_FALSE; |
| 4054 |
} |
| 4055 |
|
| 4056 |
DRMP3_ZERO_OBJECT(pMP3); |
| 4057 |
|
| 4058 |
if (drmp3_wfopen(&pFile, pFilePath, L"rb", pAllocationCallbacks) != DRMP3_SUCCESS) { |
| 4059 |
return DRMP3_FALSE; |
| 4060 |
} |
| 4061 |
|
| 4062 |
result = drmp3_init_internal(pMP3, drmp3__on_read_stdio, drmp3__on_seek_stdio, drmp3__on_tell_stdio, onMeta, (void*)pFile, pUserDataMeta, pAllocationCallbacks); |
| 4063 |
if (result != DRMP3_TRUE) { |
| 4064 |
fclose(pFile); |
| 4065 |
return result; |
| 4066 |
} |
| 4067 |
|
| 4068 |
return DRMP3_TRUE; |
| 4069 |
} |
| 4070 |
|
| 4071 |
DRMP3_API drmp3_bool32 drmp3_init_file(drmp3* pMP3, const char* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 4072 |
{ |
| 4073 |
return drmp3_init_file_with_metadata(pMP3, pFilePath, NULL, NULL, pAllocationCallbacks); |
| 4074 |
} |
| 4075 |
|
| 4076 |
DRMP3_API drmp3_bool32 drmp3_init_file_w(drmp3* pMP3, const wchar_t* pFilePath, const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 4077 |
{ |
| 4078 |
return drmp3_init_file_with_metadata_w(pMP3, pFilePath, NULL, NULL, pAllocationCallbacks); |
| 4079 |
} |
| 4080 |
#endif |
| 4081 |
|
| 4082 |
DRMP3_API void drmp3_uninit(drmp3* pMP3) |
| 4083 |
{ |
| 4084 |
if (pMP3 == NULL) { |
| 4085 |
return; |
| 4086 |
} |
| 4087 |
|
| 4088 |
#ifndef DR_MP3_NO_STDIO |
| 4089 |
if (pMP3->onRead == drmp3__on_read_stdio) { |
| 4090 |
FILE* pFile = (FILE*)pMP3->pUserData; |
| 4091 |
if (pFile != NULL) { |
| 4092 |
fclose(pFile); |
| 4093 |
pMP3->pUserData = NULL; |
| 4094 |
} |
| 4095 |
} |
| 4096 |
#endif |
| 4097 |
|
| 4098 |
drmp3__free_from_callbacks(pMP3->pData, &pMP3->allocationCallbacks); |
| 4099 |
} |
| 4100 |
|
| 4101 |
#if defined(DR_MP3_FLOAT_OUTPUT) |
| 4102 |
static void drmp3_f32_to_s16(drmp3_int16* dst, const float* src, drmp3_uint64 sampleCount) |
| 4103 |
{ |
| 4104 |
drmp3_uint64 i; |
| 4105 |
drmp3_uint64 i4; |
| 4106 |
drmp3_uint64 sampleCount4; |
| 4107 |
|
| 4108 |
|
| 4109 |
i = 0; |
| 4110 |
sampleCount4 = sampleCount >> 2; |
| 4111 |
for (i4 = 0; i4 < sampleCount4; i4 += 1) { |
| 4112 |
float x0 = src[i+0]; |
| 4113 |
float x1 = src[i+1]; |
| 4114 |
float x2 = src[i+2]; |
| 4115 |
float x3 = src[i+3]; |
| 4116 |
|
| 4117 |
x0 = ((x0 < -1) ? -1 : ((x0 > 1) ? 1 : x0)); |
| 4118 |
x1 = ((x1 < -1) ? -1 : ((x1 > 1) ? 1 : x1)); |
| 4119 |
x2 = ((x2 < -1) ? -1 : ((x2 > 1) ? 1 : x2)); |
| 4120 |
x3 = ((x3 < -1) ? -1 : ((x3 > 1) ? 1 : x3)); |
| 4121 |
|
| 4122 |
x0 = x0 * 32767.0f; |
| 4123 |
x1 = x1 * 32767.0f; |
| 4124 |
x2 = x2 * 32767.0f; |
| 4125 |
x3 = x3 * 32767.0f; |
| 4126 |
|
| 4127 |
dst[i+0] = (drmp3_int16)x0; |
| 4128 |
dst[i+1] = (drmp3_int16)x1; |
| 4129 |
dst[i+2] = (drmp3_int16)x2; |
| 4130 |
dst[i+3] = (drmp3_int16)x3; |
| 4131 |
|
| 4132 |
i += 4; |
| 4133 |
} |
| 4134 |
|
| 4135 |
|
| 4136 |
for (; i < sampleCount; i += 1) { |
| 4137 |
float x = src[i]; |
| 4138 |
x = ((x < -1) ? -1 : ((x > 1) ? 1 : x)); |
| 4139 |
x = x * 32767.0f; |
| 4140 |
|
| 4141 |
dst[i] = (drmp3_int16)x; |
| 4142 |
} |
| 4143 |
} |
| 4144 |
#endif |
| 4145 |
|
| 4146 |
#if !defined(DR_MP3_FLOAT_OUTPUT) |
| 4147 |
static void drmp3_s16_to_f32(float* dst, const drmp3_int16* src, drmp3_uint64 sampleCount) |
| 4148 |
{ |
| 4149 |
drmp3_uint64 i; |
| 4150 |
for (i = 0; i < sampleCount; i += 1) { |
| 4151 |
float x = (float)src[i]; |
| 4152 |
x = x * 0.000030517578125f; |
| 4153 |
dst[i] = x; |
| 4154 |
} |
| 4155 |
} |
| 4156 |
#endif |
| 4157 |
|
| 4158 |
|
| 4159 |
static drmp3_uint64 drmp3_read_pcm_frames_raw(drmp3* pMP3, drmp3_uint64 framesToRead, void* pBufferOut) |
| 4160 |
{ |
| 4161 |
drmp3_uint64 totalFramesRead = 0; |
| 4162 |
|
| 4163 |
DRMP3_ASSERT(pMP3 != NULL); |
| 4164 |
DRMP3_ASSERT(pMP3->onRead != NULL); |
| 4165 |
|
| 4166 |
while (framesToRead > 0) { |
| 4167 |
drmp3_uint32 framesToConsume; |
| 4168 |
|
| 4169 |
|
| 4170 |
if (pMP3->currentPCMFrame < pMP3->delayInPCMFrames) { |
| 4171 |
drmp3_uint32 framesToSkip = (drmp3_uint32)DRMP3_MIN(pMP3->pcmFramesRemainingInMP3Frame, pMP3->delayInPCMFrames - pMP3->currentPCMFrame); |
| 4172 |
|
| 4173 |
pMP3->currentPCMFrame += framesToSkip; |
| 4174 |
pMP3->pcmFramesConsumedInMP3Frame += framesToSkip; |
| 4175 |
pMP3->pcmFramesRemainingInMP3Frame -= framesToSkip; |
| 4176 |
} |
| 4177 |
|
| 4178 |
framesToConsume = (drmp3_uint32)DRMP3_MIN(pMP3->pcmFramesRemainingInMP3Frame, framesToRead); |
| 4179 |
|
| 4180 |
|
| 4181 |
if (pMP3->totalPCMFrameCount != DRMP3_UINT64_MAX && pMP3->totalPCMFrameCount > pMP3->paddingInPCMFrames) { |
| 4182 |
if (pMP3->currentPCMFrame < (pMP3->totalPCMFrameCount - pMP3->paddingInPCMFrames)) { |
| 4183 |
drmp3_uint64 framesRemainigToPadding = (pMP3->totalPCMFrameCount - pMP3->paddingInPCMFrames) - pMP3->currentPCMFrame; |
| 4184 |
if (framesToConsume > framesRemainigToPadding) { |
| 4185 |
framesToConsume = (drmp3_uint32)framesRemainigToPadding; |
| 4186 |
} |
| 4187 |
} else { |
| 4188 |
|
| 4189 |
break; |
| 4190 |
} |
| 4191 |
} |
| 4192 |
|
| 4193 |
if (pBufferOut != NULL) { |
| 4194 |
#if defined(DR_MP3_FLOAT_OUTPUT) |
| 4195 |
{ |
| 4196 |
|
| 4197 |
float* pFramesOutF32 = (float*)DRMP3_OFFSET_PTR(pBufferOut, sizeof(float) * totalFramesRead * pMP3->channels); |
| 4198 |
float* pFramesInF32 = (float*)DRMP3_OFFSET_PTR(&pMP3->pcmFrames[0], sizeof(float) * pMP3->pcmFramesConsumedInMP3Frame * pMP3->mp3FrameChannels); |
| 4199 |
DRMP3_COPY_MEMORY(pFramesOutF32, pFramesInF32, sizeof(float) * framesToConsume * pMP3->channels); |
| 4200 |
} |
| 4201 |
#else |
| 4202 |
{ |
| 4203 |
|
| 4204 |
drmp3_int16* pFramesOutS16 = (drmp3_int16*)DRMP3_OFFSET_PTR(pBufferOut, sizeof(drmp3_int16) * totalFramesRead * pMP3->channels); |
| 4205 |
drmp3_int16* pFramesInS16 = (drmp3_int16*)DRMP3_OFFSET_PTR(&pMP3->pcmFrames[0], sizeof(drmp3_int16) * pMP3->pcmFramesConsumedInMP3Frame * pMP3->mp3FrameChannels); |
| 4206 |
DRMP3_COPY_MEMORY(pFramesOutS16, pFramesInS16, sizeof(drmp3_int16) * framesToConsume * pMP3->channels); |
| 4207 |
} |
| 4208 |
#endif |
| 4209 |
} |
| 4210 |
|
| 4211 |
pMP3->currentPCMFrame += framesToConsume; |
| 4212 |
pMP3->pcmFramesConsumedInMP3Frame += framesToConsume; |
| 4213 |
pMP3->pcmFramesRemainingInMP3Frame -= framesToConsume; |
| 4214 |
totalFramesRead += framesToConsume; |
| 4215 |
framesToRead -= framesToConsume; |
| 4216 |
|
| 4217 |
if (framesToRead == 0) { |
| 4218 |
break; |
| 4219 |
} |
| 4220 |
|
| 4221 |
|
| 4222 |
if (pMP3->totalPCMFrameCount != DRMP3_UINT64_MAX && pMP3->totalPCMFrameCount > pMP3->paddingInPCMFrames && pMP3->currentPCMFrame >= (pMP3->totalPCMFrameCount - pMP3->paddingInPCMFrames)) { |
| 4223 |
break; |
| 4224 |
} |
| 4225 |
|
| 4226 |
DRMP3_ASSERT(pMP3->pcmFramesRemainingInMP3Frame == 0); |
| 4227 |
|
| 4228 |
|
| 4229 |
if (drmp3_decode_next_frame(pMP3) == 0) { |
| 4230 |
break; |
| 4231 |
} |
| 4232 |
} |
| 4233 |
|
| 4234 |
return totalFramesRead; |
| 4235 |
} |
| 4236 |
|
| 4237 |
|
| 4238 |
DRMP3_API drmp3_uint64 drmp3_read_pcm_frames_f32(drmp3* pMP3, drmp3_uint64 framesToRead, float* pBufferOut) |
| 4239 |
{ |
| 4240 |
if (pMP3 == NULL || pMP3->onRead == NULL) { |
| 4241 |
return 0; |
| 4242 |
} |
| 4243 |
|
| 4244 |
#if defined(DR_MP3_FLOAT_OUTPUT) |
| 4245 |
|
| 4246 |
return drmp3_read_pcm_frames_raw(pMP3, framesToRead, pBufferOut); |
| 4247 |
#else |
| 4248 |
|
| 4249 |
{ |
| 4250 |
drmp3_int16 pTempS16[8192]; |
| 4251 |
drmp3_uint64 totalPCMFramesRead = 0; |
| 4252 |
|
| 4253 |
while (totalPCMFramesRead < framesToRead) { |
| 4254 |
drmp3_uint64 framesJustRead; |
| 4255 |
drmp3_uint64 framesRemaining = framesToRead - totalPCMFramesRead; |
| 4256 |
drmp3_uint64 framesToReadNow = DRMP3_COUNTOF(pTempS16) / pMP3->channels; |
| 4257 |
if (framesToReadNow > framesRemaining) { |
| 4258 |
framesToReadNow = framesRemaining; |
| 4259 |
} |
| 4260 |
|
| 4261 |
framesJustRead = drmp3_read_pcm_frames_raw(pMP3, framesToReadNow, pTempS16); |
| 4262 |
if (framesJustRead == 0) { |
| 4263 |
break; |
| 4264 |
} |
| 4265 |
|
| 4266 |
drmp3_s16_to_f32((float*)DRMP3_OFFSET_PTR(pBufferOut, sizeof(float) * totalPCMFramesRead * pMP3->channels), pTempS16, framesJustRead * pMP3->channels); |
| 4267 |
totalPCMFramesRead += framesJustRead; |
| 4268 |
} |
| 4269 |
|
| 4270 |
return totalPCMFramesRead; |
| 4271 |
} |
| 4272 |
#endif |
| 4273 |
} |
| 4274 |
|
| 4275 |
DRMP3_API drmp3_uint64 drmp3_read_pcm_frames_s16(drmp3* pMP3, drmp3_uint64 framesToRead, drmp3_int16* pBufferOut) |
| 4276 |
{ |
| 4277 |
if (pMP3 == NULL || pMP3->onRead == NULL) { |
| 4278 |
return 0; |
| 4279 |
} |
| 4280 |
|
| 4281 |
#if !defined(DR_MP3_FLOAT_OUTPUT) |
| 4282 |
|
| 4283 |
return drmp3_read_pcm_frames_raw(pMP3, framesToRead, pBufferOut); |
| 4284 |
#else |
| 4285 |
|
| 4286 |
{ |
| 4287 |
float pTempF32[4096]; |
| 4288 |
drmp3_uint64 totalPCMFramesRead = 0; |
| 4289 |
|
| 4290 |
while (totalPCMFramesRead < framesToRead) { |
| 4291 |
drmp3_uint64 framesJustRead; |
| 4292 |
drmp3_uint64 framesRemaining = framesToRead - totalPCMFramesRead; |
| 4293 |
drmp3_uint64 framesToReadNow = DRMP3_COUNTOF(pTempF32) / pMP3->channels; |
| 4294 |
if (framesToReadNow > framesRemaining) { |
| 4295 |
framesToReadNow = framesRemaining; |
| 4296 |
} |
| 4297 |
|
| 4298 |
framesJustRead = drmp3_read_pcm_frames_raw(pMP3, framesToReadNow, pTempF32); |
| 4299 |
if (framesJustRead == 0) { |
| 4300 |
break; |
| 4301 |
} |
| 4302 |
|
| 4303 |
drmp3_f32_to_s16((drmp3_int16*)DRMP3_OFFSET_PTR(pBufferOut, sizeof(drmp3_int16) * totalPCMFramesRead * pMP3->channels), pTempF32, framesJustRead * pMP3->channels); |
| 4304 |
totalPCMFramesRead += framesJustRead; |
| 4305 |
} |
| 4306 |
|
| 4307 |
return totalPCMFramesRead; |
| 4308 |
} |
| 4309 |
#endif |
| 4310 |
} |
| 4311 |
|
| 4312 |
static void drmp3_reset(drmp3* pMP3) |
| 4313 |
{ |
| 4314 |
DRMP3_ASSERT(pMP3 != NULL); |
| 4315 |
|
| 4316 |
pMP3->pcmFramesConsumedInMP3Frame = 0; |
| 4317 |
pMP3->pcmFramesRemainingInMP3Frame = 0; |
| 4318 |
pMP3->currentPCMFrame = 0; |
| 4319 |
pMP3->dataSize = 0; |
| 4320 |
pMP3->atEnd = DRMP3_FALSE; |
| 4321 |
drmp3dec_init(&pMP3->decoder); |
| 4322 |
} |
| 4323 |
|
| 4324 |
static drmp3_bool32 drmp3_seek_to_start_of_stream(drmp3* pMP3) |
| 4325 |
{ |
| 4326 |
DRMP3_ASSERT(pMP3 != NULL); |
| 4327 |
DRMP3_ASSERT(pMP3->onSeek != NULL); |
| 4328 |
|
| 4329 |
|
| 4330 |
if (!drmp3__on_seek_64(pMP3, pMP3->streamStartOffset, DRMP3_SEEK_SET)) { |
| 4331 |
return DRMP3_FALSE; |
| 4332 |
} |
| 4333 |
|
| 4334 |
|
| 4335 |
drmp3_reset(pMP3); |
| 4336 |
return DRMP3_TRUE; |
| 4337 |
} |
| 4338 |
|
| 4339 |
|
| 4340 |
static drmp3_bool32 drmp3_seek_forward_by_pcm_frames__brute_force(drmp3* pMP3, drmp3_uint64 frameOffset) |
| 4341 |
{ |
| 4342 |
drmp3_uint64 framesRead; |
| 4343 |
|
| 4344 |
|
| 4345 |
|
| 4346 |
|
| 4347 |
|
| 4348 |
|
| 4349 |
#if defined(DR_MP3_FLOAT_OUTPUT) |
| 4350 |
framesRead = drmp3_read_pcm_frames_f32(pMP3, frameOffset, NULL); |
| 4351 |
#else |
| 4352 |
framesRead = drmp3_read_pcm_frames_s16(pMP3, frameOffset, NULL); |
| 4353 |
#endif |
| 4354 |
if (framesRead != frameOffset) { |
| 4355 |
return DRMP3_FALSE; |
| 4356 |
} |
| 4357 |
|
| 4358 |
return DRMP3_TRUE; |
| 4359 |
} |
| 4360 |
|
| 4361 |
static drmp3_bool32 drmp3_seek_to_pcm_frame__brute_force(drmp3* pMP3, drmp3_uint64 frameIndex) |
| 4362 |
{ |
| 4363 |
DRMP3_ASSERT(pMP3 != NULL); |
| 4364 |
|
| 4365 |
if (frameIndex == pMP3->currentPCMFrame) { |
| 4366 |
return DRMP3_TRUE; |
| 4367 |
} |
| 4368 |
|
| 4369 |
|
| 4370 |
|
| 4371 |
|
| 4372 |
|
| 4373 |
if (frameIndex < pMP3->currentPCMFrame) { |
| 4374 |
|
| 4375 |
if (!drmp3_seek_to_start_of_stream(pMP3)) { |
| 4376 |
return DRMP3_FALSE; |
| 4377 |
} |
| 4378 |
} |
| 4379 |
|
| 4380 |
DRMP3_ASSERT(frameIndex >= pMP3->currentPCMFrame); |
| 4381 |
return drmp3_seek_forward_by_pcm_frames__brute_force(pMP3, (frameIndex - pMP3->currentPCMFrame)); |
| 4382 |
} |
| 4383 |
|
| 4384 |
static drmp3_bool32 drmp3_find_closest_seek_point(drmp3* pMP3, drmp3_uint64 frameIndex, drmp3_uint32* pSeekPointIndex) |
| 4385 |
{ |
| 4386 |
drmp3_uint32 iSeekPoint; |
| 4387 |
|
| 4388 |
DRMP3_ASSERT(pSeekPointIndex != NULL); |
| 4389 |
|
| 4390 |
*pSeekPointIndex = 0; |
| 4391 |
|
| 4392 |
if (frameIndex < pMP3->pSeekPoints[0].pcmFrameIndex) { |
| 4393 |
return DRMP3_FALSE; |
| 4394 |
} |
| 4395 |
|
| 4396 |
|
| 4397 |
for (iSeekPoint = 0; iSeekPoint < pMP3->seekPointCount; ++iSeekPoint) { |
| 4398 |
if (pMP3->pSeekPoints[iSeekPoint].pcmFrameIndex > frameIndex) { |
| 4399 |
break; |
| 4400 |
} |
| 4401 |
|
| 4402 |
*pSeekPointIndex = iSeekPoint; |
| 4403 |
} |
| 4404 |
|
| 4405 |
return DRMP3_TRUE; |
| 4406 |
} |
| 4407 |
|
| 4408 |
static drmp3_bool32 drmp3_seek_to_pcm_frame__seek_table(drmp3* pMP3, drmp3_uint64 frameIndex) |
| 4409 |
{ |
| 4410 |
drmp3_seek_point seekPoint; |
| 4411 |
drmp3_uint32 priorSeekPointIndex; |
| 4412 |
drmp3_uint16 iMP3Frame; |
| 4413 |
drmp3_uint64 leftoverFrames; |
| 4414 |
|
| 4415 |
DRMP3_ASSERT(pMP3 != NULL); |
| 4416 |
DRMP3_ASSERT(pMP3->pSeekPoints != NULL); |
| 4417 |
DRMP3_ASSERT(pMP3->seekPointCount > 0); |
| 4418 |
|
| 4419 |
|
| 4420 |
if (drmp3_find_closest_seek_point(pMP3, frameIndex, &priorSeekPointIndex)) { |
| 4421 |
seekPoint = pMP3->pSeekPoints[priorSeekPointIndex]; |
| 4422 |
} else { |
| 4423 |
seekPoint.seekPosInBytes = 0; |
| 4424 |
seekPoint.pcmFrameIndex = 0; |
| 4425 |
seekPoint.mp3FramesToDiscard = 0; |
| 4426 |
seekPoint.pcmFramesToDiscard = 0; |
| 4427 |
} |
| 4428 |
|
| 4429 |
|
| 4430 |
if (!drmp3__on_seek_64(pMP3, seekPoint.seekPosInBytes, DRMP3_SEEK_SET)) { |
| 4431 |
return DRMP3_FALSE; |
| 4432 |
} |
| 4433 |
|
| 4434 |
|
| 4435 |
drmp3_reset(pMP3); |
| 4436 |
|
| 4437 |
|
| 4438 |
for (iMP3Frame = 0; iMP3Frame < seekPoint.mp3FramesToDiscard; ++iMP3Frame) { |
| 4439 |
drmp3_uint32 pcmFramesRead; |
| 4440 |
drmp3d_sample_t* pPCMFrames; |
| 4441 |
|
| 4442 |
|
| 4443 |
pPCMFrames = NULL; |
| 4444 |
if (iMP3Frame == seekPoint.mp3FramesToDiscard-1) { |
| 4445 |
pPCMFrames = (drmp3d_sample_t*)pMP3->pcmFrames; |
| 4446 |
} |
| 4447 |
|
| 4448 |
|
| 4449 |
pcmFramesRead = drmp3_decode_next_frame_ex(pMP3, pPCMFrames, NULL, NULL); |
| 4450 |
if (pcmFramesRead == 0) { |
| 4451 |
return DRMP3_FALSE; |
| 4452 |
} |
| 4453 |
} |
| 4454 |
|
| 4455 |
|
| 4456 |
pMP3->currentPCMFrame = seekPoint.pcmFrameIndex - seekPoint.pcmFramesToDiscard; |
| 4457 |
|
| 4458 |
|
| 4459 |
|
| 4460 |
|
| 4461 |
|
| 4462 |
leftoverFrames = frameIndex - pMP3->currentPCMFrame; |
| 4463 |
return drmp3_seek_forward_by_pcm_frames__brute_force(pMP3, leftoverFrames); |
| 4464 |
} |
| 4465 |
|
| 4466 |
DRMP3_API drmp3_bool32 drmp3_seek_to_pcm_frame(drmp3* pMP3, drmp3_uint64 frameIndex) |
| 4467 |
{ |
| 4468 |
if (pMP3 == NULL || pMP3->onSeek == NULL) { |
| 4469 |
return DRMP3_FALSE; |
| 4470 |
} |
| 4471 |
|
| 4472 |
if (frameIndex == 0) { |
| 4473 |
return drmp3_seek_to_start_of_stream(pMP3); |
| 4474 |
} |
| 4475 |
|
| 4476 |
|
| 4477 |
if (pMP3->pSeekPoints != NULL && pMP3->seekPointCount > 0) { |
| 4478 |
return drmp3_seek_to_pcm_frame__seek_table(pMP3, frameIndex); |
| 4479 |
} else { |
| 4480 |
return drmp3_seek_to_pcm_frame__brute_force(pMP3, frameIndex); |
| 4481 |
} |
| 4482 |
} |
| 4483 |
|
| 4484 |
DRMP3_API drmp3_bool32 drmp3_get_mp3_and_pcm_frame_count(drmp3* pMP3, drmp3_uint64* pMP3FrameCount, drmp3_uint64* pPCMFrameCount) |
| 4485 |
{ |
| 4486 |
drmp3_uint64 currentPCMFrame; |
| 4487 |
drmp3_uint64 totalPCMFrameCount; |
| 4488 |
drmp3_uint64 totalMP3FrameCount; |
| 4489 |
|
| 4490 |
if (pMP3 == NULL) { |
| 4491 |
return DRMP3_FALSE; |
| 4492 |
} |
| 4493 |
|
| 4494 |
|
| 4495 |
|
| 4496 |
|
| 4497 |
|
| 4498 |
|
| 4499 |
|
| 4500 |
if (pMP3->onSeek == NULL) { |
| 4501 |
return DRMP3_FALSE; |
| 4502 |
} |
| 4503 |
|
| 4504 |
|
| 4505 |
currentPCMFrame = pMP3->currentPCMFrame; |
| 4506 |
|
| 4507 |
if (!drmp3_seek_to_start_of_stream(pMP3)) { |
| 4508 |
return DRMP3_FALSE; |
| 4509 |
} |
| 4510 |
|
| 4511 |
totalPCMFrameCount = 0; |
| 4512 |
totalMP3FrameCount = 0; |
| 4513 |
|
| 4514 |
for (;;) { |
| 4515 |
drmp3_uint32 pcmFramesInCurrentMP3Frame; |
| 4516 |
|
| 4517 |
pcmFramesInCurrentMP3Frame = drmp3_decode_next_frame_ex(pMP3, NULL, NULL, NULL); |
| 4518 |
if (pcmFramesInCurrentMP3Frame == 0) { |
| 4519 |
break; |
| 4520 |
} |
| 4521 |
|
| 4522 |
totalPCMFrameCount += pcmFramesInCurrentMP3Frame; |
| 4523 |
totalMP3FrameCount += 1; |
| 4524 |
} |
| 4525 |
|
| 4526 |
|
| 4527 |
if (!drmp3_seek_to_start_of_stream(pMP3)) { |
| 4528 |
return DRMP3_FALSE; |
| 4529 |
} |
| 4530 |
|
| 4531 |
if (!drmp3_seek_to_pcm_frame(pMP3, currentPCMFrame)) { |
| 4532 |
return DRMP3_FALSE; |
| 4533 |
} |
| 4534 |
|
| 4535 |
if (pMP3FrameCount != NULL) { |
| 4536 |
*pMP3FrameCount = totalMP3FrameCount; |
| 4537 |
} |
| 4538 |
if (pPCMFrameCount != NULL) { |
| 4539 |
*pPCMFrameCount = totalPCMFrameCount; |
| 4540 |
} |
| 4541 |
|
| 4542 |
return DRMP3_TRUE; |
| 4543 |
} |
| 4544 |
|
| 4545 |
DRMP3_API drmp3_uint64 drmp3_get_pcm_frame_count(drmp3* pMP3) |
| 4546 |
{ |
| 4547 |
drmp3_uint64 totalPCMFrameCount; |
| 4548 |
|
| 4549 |
if (pMP3 == NULL) { |
| 4550 |
return 0; |
| 4551 |
} |
| 4552 |
|
| 4553 |
if (pMP3->totalPCMFrameCount != DRMP3_UINT64_MAX) { |
| 4554 |
totalPCMFrameCount = pMP3->totalPCMFrameCount; |
| 4555 |
|
| 4556 |
if (totalPCMFrameCount >= pMP3->delayInPCMFrames) { |
| 4557 |
totalPCMFrameCount -= pMP3->delayInPCMFrames; |
| 4558 |
} else { |
| 4559 |
|
| 4560 |
} |
| 4561 |
|
| 4562 |
if (totalPCMFrameCount >= pMP3->paddingInPCMFrames) { |
| 4563 |
totalPCMFrameCount -= pMP3->paddingInPCMFrames; |
| 4564 |
} else { |
| 4565 |
|
| 4566 |
} |
| 4567 |
|
| 4568 |
return totalPCMFrameCount; |
| 4569 |
} else { |
| 4570 |
|
| 4571 |
if (!drmp3_get_mp3_and_pcm_frame_count(pMP3, NULL, &totalPCMFrameCount)) { |
| 4572 |
return 0; |
| 4573 |
} |
| 4574 |
|
| 4575 |
return totalPCMFrameCount; |
| 4576 |
} |
| 4577 |
} |
| 4578 |
|
| 4579 |
DRMP3_API drmp3_uint64 drmp3_get_mp3_frame_count(drmp3* pMP3) |
| 4580 |
{ |
| 4581 |
drmp3_uint64 totalMP3FrameCount; |
| 4582 |
if (!drmp3_get_mp3_and_pcm_frame_count(pMP3, &totalMP3FrameCount, NULL)) { |
| 4583 |
return 0; |
| 4584 |
} |
| 4585 |
|
| 4586 |
return totalMP3FrameCount; |
| 4587 |
} |
| 4588 |
|
| 4589 |
static void drmp3__accumulate_running_pcm_frame_count(drmp3* pMP3, drmp3_uint32 pcmFrameCountIn, drmp3_uint64* pRunningPCMFrameCount, float* pRunningPCMFrameCountFractionalPart) |
| 4590 |
{ |
| 4591 |
float srcRatio; |
| 4592 |
float pcmFrameCountOutF; |
| 4593 |
drmp3_uint32 pcmFrameCountOut; |
| 4594 |
|
| 4595 |
srcRatio = (float)pMP3->mp3FrameSampleRate / (float)pMP3->sampleRate; |
| 4596 |
DRMP3_ASSERT(srcRatio > 0); |
| 4597 |
|
| 4598 |
pcmFrameCountOutF = *pRunningPCMFrameCountFractionalPart + (pcmFrameCountIn / srcRatio); |
| 4599 |
pcmFrameCountOut = (drmp3_uint32)pcmFrameCountOutF; |
| 4600 |
*pRunningPCMFrameCountFractionalPart = pcmFrameCountOutF - pcmFrameCountOut; |
| 4601 |
*pRunningPCMFrameCount += pcmFrameCountOut; |
| 4602 |
} |
| 4603 |
|
| 4604 |
typedef struct |
| 4605 |
{ |
| 4606 |
drmp3_uint64 bytePos; |
| 4607 |
drmp3_uint64 pcmFrameIndex; |
| 4608 |
} drmp3__seeking_mp3_frame_info; |
| 4609 |
|
| 4610 |
DRMP3_API drmp3_bool32 drmp3_calculate_seek_points(drmp3* pMP3, drmp3_uint32* pSeekPointCount, drmp3_seek_point* pSeekPoints) |
| 4611 |
{ |
| 4612 |
drmp3_uint32 seekPointCount; |
| 4613 |
drmp3_uint64 currentPCMFrame; |
| 4614 |
drmp3_uint64 totalMP3FrameCount; |
| 4615 |
drmp3_uint64 totalPCMFrameCount; |
| 4616 |
|
| 4617 |
if (pMP3 == NULL || pSeekPointCount == NULL || pSeekPoints == NULL) { |
| 4618 |
return DRMP3_FALSE; |
| 4619 |
} |
| 4620 |
|
| 4621 |
seekPointCount = *pSeekPointCount; |
| 4622 |
if (seekPointCount == 0) { |
| 4623 |
return DRMP3_FALSE; |
| 4624 |
} |
| 4625 |
|
| 4626 |
|
| 4627 |
currentPCMFrame = pMP3->currentPCMFrame; |
| 4628 |
|
| 4629 |
|
| 4630 |
if (!drmp3_get_mp3_and_pcm_frame_count(pMP3, &totalMP3FrameCount, &totalPCMFrameCount)) { |
| 4631 |
return DRMP3_FALSE; |
| 4632 |
} |
| 4633 |
|
| 4634 |
|
| 4635 |
if (totalMP3FrameCount < DRMP3_SEEK_LEADING_MP3_FRAMES+1) { |
| 4636 |
seekPointCount = 1; |
| 4637 |
pSeekPoints[0].seekPosInBytes = 0; |
| 4638 |
pSeekPoints[0].pcmFrameIndex = 0; |
| 4639 |
pSeekPoints[0].mp3FramesToDiscard = 0; |
| 4640 |
pSeekPoints[0].pcmFramesToDiscard = 0; |
| 4641 |
} else { |
| 4642 |
drmp3_uint64 pcmFramesBetweenSeekPoints; |
| 4643 |
drmp3__seeking_mp3_frame_info mp3FrameInfo[DRMP3_SEEK_LEADING_MP3_FRAMES+1]; |
| 4644 |
drmp3_uint64 runningPCMFrameCount = 0; |
| 4645 |
float runningPCMFrameCountFractionalPart = 0; |
| 4646 |
drmp3_uint64 nextTargetPCMFrame; |
| 4647 |
drmp3_uint32 iMP3Frame; |
| 4648 |
drmp3_uint32 iSeekPoint; |
| 4649 |
|
| 4650 |
if (seekPointCount > totalMP3FrameCount-1) { |
| 4651 |
seekPointCount = (drmp3_uint32)totalMP3FrameCount-1; |
| 4652 |
} |
| 4653 |
|
| 4654 |
pcmFramesBetweenSeekPoints = totalPCMFrameCount / (seekPointCount+1); |
| 4655 |
|
| 4656 |
|
| 4657 |
|
| 4658 |
|
| 4659 |
|
| 4660 |
if (!drmp3_seek_to_start_of_stream(pMP3)) { |
| 4661 |
return DRMP3_FALSE; |
| 4662 |
} |
| 4663 |
|
| 4664 |
|
| 4665 |
|
| 4666 |
|
| 4667 |
|
| 4668 |
|
| 4669 |
|
| 4670 |
for (iMP3Frame = 0; iMP3Frame < DRMP3_SEEK_LEADING_MP3_FRAMES+1; ++iMP3Frame) { |
| 4671 |
drmp3_uint32 pcmFramesInCurrentMP3FrameIn; |
| 4672 |
|
| 4673 |
|
| 4674 |
DRMP3_ASSERT(pMP3->streamCursor >= pMP3->dataSize); |
| 4675 |
mp3FrameInfo[iMP3Frame].bytePos = pMP3->streamCursor - pMP3->dataSize; |
| 4676 |
mp3FrameInfo[iMP3Frame].pcmFrameIndex = runningPCMFrameCount; |
| 4677 |
|
| 4678 |
|
| 4679 |
pcmFramesInCurrentMP3FrameIn = drmp3_decode_next_frame_ex(pMP3, NULL, NULL, NULL); |
| 4680 |
if (pcmFramesInCurrentMP3FrameIn == 0) { |
| 4681 |
return DRMP3_FALSE; |
| 4682 |
} |
| 4683 |
|
| 4684 |
drmp3__accumulate_running_pcm_frame_count(pMP3, pcmFramesInCurrentMP3FrameIn, &runningPCMFrameCount, &runningPCMFrameCountFractionalPart); |
| 4685 |
} |
| 4686 |
|
| 4687 |
|
| 4688 |
|
| 4689 |
|
| 4690 |
|
| 4691 |
nextTargetPCMFrame = 0; |
| 4692 |
for (iSeekPoint = 0; iSeekPoint < seekPointCount; ++iSeekPoint) { |
| 4693 |
nextTargetPCMFrame += pcmFramesBetweenSeekPoints; |
| 4694 |
|
| 4695 |
for (;;) { |
| 4696 |
if (nextTargetPCMFrame < runningPCMFrameCount) { |
| 4697 |
|
| 4698 |
pSeekPoints[iSeekPoint].seekPosInBytes = mp3FrameInfo[0].bytePos; |
| 4699 |
pSeekPoints[iSeekPoint].pcmFrameIndex = nextTargetPCMFrame; |
| 4700 |
pSeekPoints[iSeekPoint].mp3FramesToDiscard = DRMP3_SEEK_LEADING_MP3_FRAMES; |
| 4701 |
pSeekPoints[iSeekPoint].pcmFramesToDiscard = (drmp3_uint16)(nextTargetPCMFrame - mp3FrameInfo[DRMP3_SEEK_LEADING_MP3_FRAMES-1].pcmFrameIndex); |
| 4702 |
break; |
| 4703 |
} else { |
| 4704 |
size_t i; |
| 4705 |
drmp3_uint32 pcmFramesInCurrentMP3FrameIn; |
| 4706 |
|
| 4707 |
|
| 4708 |
|
| 4709 |
|
| 4710 |
|
| 4711 |
for (i = 0; i < DRMP3_COUNTOF(mp3FrameInfo)-1; ++i) { |
| 4712 |
mp3FrameInfo[i] = mp3FrameInfo[i+1]; |
| 4713 |
} |
| 4714 |
|
| 4715 |
|
| 4716 |
mp3FrameInfo[DRMP3_COUNTOF(mp3FrameInfo)-1].bytePos = pMP3->streamCursor - pMP3->dataSize; |
| 4717 |
mp3FrameInfo[DRMP3_COUNTOF(mp3FrameInfo)-1].pcmFrameIndex = runningPCMFrameCount; |
| 4718 |
|
| 4719 |
|
| 4720 |
|
| 4721 |
|
| 4722 |
|
| 4723 |
pcmFramesInCurrentMP3FrameIn = drmp3_decode_next_frame_ex(pMP3, NULL, NULL, NULL); |
| 4724 |
if (pcmFramesInCurrentMP3FrameIn == 0) { |
| 4725 |
pSeekPoints[iSeekPoint].seekPosInBytes = mp3FrameInfo[0].bytePos; |
| 4726 |
pSeekPoints[iSeekPoint].pcmFrameIndex = nextTargetPCMFrame; |
| 4727 |
pSeekPoints[iSeekPoint].mp3FramesToDiscard = DRMP3_SEEK_LEADING_MP3_FRAMES; |
| 4728 |
pSeekPoints[iSeekPoint].pcmFramesToDiscard = (drmp3_uint16)(nextTargetPCMFrame - mp3FrameInfo[DRMP3_SEEK_LEADING_MP3_FRAMES-1].pcmFrameIndex); |
| 4729 |
break; |
| 4730 |
} |
| 4731 |
|
| 4732 |
drmp3__accumulate_running_pcm_frame_count(pMP3, pcmFramesInCurrentMP3FrameIn, &runningPCMFrameCount, &runningPCMFrameCountFractionalPart); |
| 4733 |
} |
| 4734 |
} |
| 4735 |
} |
| 4736 |
|
| 4737 |
|
| 4738 |
if (!drmp3_seek_to_start_of_stream(pMP3)) { |
| 4739 |
return DRMP3_FALSE; |
| 4740 |
} |
| 4741 |
if (!drmp3_seek_to_pcm_frame(pMP3, currentPCMFrame)) { |
| 4742 |
return DRMP3_FALSE; |
| 4743 |
} |
| 4744 |
} |
| 4745 |
|
| 4746 |
*pSeekPointCount = seekPointCount; |
| 4747 |
return DRMP3_TRUE; |
| 4748 |
} |
| 4749 |
|
| 4750 |
DRMP3_API drmp3_bool32 drmp3_bind_seek_table(drmp3* pMP3, drmp3_uint32 seekPointCount, drmp3_seek_point* pSeekPoints) |
| 4751 |
{ |
| 4752 |
if (pMP3 == NULL) { |
| 4753 |
return DRMP3_FALSE; |
| 4754 |
} |
| 4755 |
|
| 4756 |
if (seekPointCount == 0 || pSeekPoints == NULL) { |
| 4757 |
|
| 4758 |
pMP3->seekPointCount = 0; |
| 4759 |
pMP3->pSeekPoints = NULL; |
| 4760 |
} else { |
| 4761 |
|
| 4762 |
pMP3->seekPointCount = seekPointCount; |
| 4763 |
pMP3->pSeekPoints = pSeekPoints; |
| 4764 |
} |
| 4765 |
|
| 4766 |
return DRMP3_TRUE; |
| 4767 |
} |
| 4768 |
|
| 4769 |
|
| 4770 |
static float* drmp3__full_read_and_close_f32(drmp3* pMP3, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount) |
| 4771 |
{ |
| 4772 |
drmp3_uint64 totalFramesRead = 0; |
| 4773 |
drmp3_uint64 framesCapacity = 0; |
| 4774 |
float* pFrames = NULL; |
| 4775 |
float temp[4096]; |
| 4776 |
|
| 4777 |
DRMP3_ASSERT(pMP3 != NULL); |
| 4778 |
|
| 4779 |
for (;;) { |
| 4780 |
drmp3_uint64 framesToReadRightNow = DRMP3_COUNTOF(temp) / pMP3->channels; |
| 4781 |
drmp3_uint64 framesJustRead = drmp3_read_pcm_frames_f32(pMP3, framesToReadRightNow, temp); |
| 4782 |
if (framesJustRead == 0) { |
| 4783 |
break; |
| 4784 |
} |
| 4785 |
|
| 4786 |
|
| 4787 |
if (framesCapacity < totalFramesRead + framesJustRead) { |
| 4788 |
drmp3_uint64 oldFramesBufferSize; |
| 4789 |
drmp3_uint64 newFramesBufferSize; |
| 4790 |
drmp3_uint64 newFramesCap; |
| 4791 |
float* pNewFrames; |
| 4792 |
|
| 4793 |
newFramesCap = framesCapacity * 2; |
| 4794 |
if (newFramesCap < totalFramesRead + framesJustRead) { |
| 4795 |
newFramesCap = totalFramesRead + framesJustRead; |
| 4796 |
} |
| 4797 |
|
| 4798 |
oldFramesBufferSize = framesCapacity * pMP3->channels * sizeof(float); |
| 4799 |
newFramesBufferSize = newFramesCap * pMP3->channels * sizeof(float); |
| 4800 |
if (newFramesBufferSize > (drmp3_uint64)DRMP3_SIZE_MAX) { |
| 4801 |
break; |
| 4802 |
} |
| 4803 |
|
| 4804 |
pNewFrames = (float*)drmp3__realloc_from_callbacks(pFrames, (size_t)newFramesBufferSize, (size_t)oldFramesBufferSize, &pMP3->allocationCallbacks); |
| 4805 |
if (pNewFrames == NULL) { |
| 4806 |
drmp3__free_from_callbacks(pFrames, &pMP3->allocationCallbacks); |
| 4807 |
pFrames = NULL; |
| 4808 |
totalFramesRead = 0; |
| 4809 |
break; |
| 4810 |
} |
| 4811 |
|
| 4812 |
pFrames = pNewFrames; |
| 4813 |
framesCapacity = newFramesCap; |
| 4814 |
} |
| 4815 |
|
| 4816 |
DRMP3_COPY_MEMORY(pFrames + totalFramesRead*pMP3->channels, temp, (size_t)(framesJustRead*pMP3->channels*sizeof(float))); |
| 4817 |
totalFramesRead += framesJustRead; |
| 4818 |
|
| 4819 |
|
| 4820 |
if (framesJustRead != framesToReadRightNow) { |
| 4821 |
break; |
| 4822 |
} |
| 4823 |
} |
| 4824 |
|
| 4825 |
if (pConfig != NULL) { |
| 4826 |
pConfig->channels = pMP3->channels; |
| 4827 |
pConfig->sampleRate = pMP3->sampleRate; |
| 4828 |
} |
| 4829 |
|
| 4830 |
drmp3_uninit(pMP3); |
| 4831 |
|
| 4832 |
if (pTotalFrameCount) { |
| 4833 |
*pTotalFrameCount = totalFramesRead; |
| 4834 |
} |
| 4835 |
|
| 4836 |
return pFrames; |
| 4837 |
} |
| 4838 |
|
| 4839 |
static drmp3_int16* drmp3__full_read_and_close_s16(drmp3* pMP3, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount) |
| 4840 |
{ |
| 4841 |
drmp3_uint64 totalFramesRead = 0; |
| 4842 |
drmp3_uint64 framesCapacity = 0; |
| 4843 |
drmp3_int16* pFrames = NULL; |
| 4844 |
drmp3_int16 temp[4096]; |
| 4845 |
|
| 4846 |
DRMP3_ASSERT(pMP3 != NULL); |
| 4847 |
|
| 4848 |
for (;;) { |
| 4849 |
drmp3_uint64 framesToReadRightNow = DRMP3_COUNTOF(temp) / pMP3->channels; |
| 4850 |
drmp3_uint64 framesJustRead = drmp3_read_pcm_frames_s16(pMP3, framesToReadRightNow, temp); |
| 4851 |
if (framesJustRead == 0) { |
| 4852 |
break; |
| 4853 |
} |
| 4854 |
|
| 4855 |
|
| 4856 |
if (framesCapacity < totalFramesRead + framesJustRead) { |
| 4857 |
drmp3_uint64 newFramesBufferSize; |
| 4858 |
drmp3_uint64 oldFramesBufferSize; |
| 4859 |
drmp3_uint64 newFramesCap; |
| 4860 |
drmp3_int16* pNewFrames; |
| 4861 |
|
| 4862 |
newFramesCap = framesCapacity * 2; |
| 4863 |
if (newFramesCap < totalFramesRead + framesJustRead) { |
| 4864 |
newFramesCap = totalFramesRead + framesJustRead; |
| 4865 |
} |
| 4866 |
|
| 4867 |
oldFramesBufferSize = framesCapacity * pMP3->channels * sizeof(drmp3_int16); |
| 4868 |
newFramesBufferSize = newFramesCap * pMP3->channels * sizeof(drmp3_int16); |
| 4869 |
if (newFramesBufferSize > (drmp3_uint64)DRMP3_SIZE_MAX) { |
| 4870 |
break; |
| 4871 |
} |
| 4872 |
|
| 4873 |
pNewFrames = (drmp3_int16*)drmp3__realloc_from_callbacks(pFrames, (size_t)newFramesBufferSize, (size_t)oldFramesBufferSize, &pMP3->allocationCallbacks); |
| 4874 |
if (pNewFrames == NULL) { |
| 4875 |
drmp3__free_from_callbacks(pFrames, &pMP3->allocationCallbacks); |
| 4876 |
pFrames = NULL; |
| 4877 |
totalFramesRead = 0; |
| 4878 |
break; |
| 4879 |
} |
| 4880 |
|
| 4881 |
pFrames = pNewFrames; |
| 4882 |
framesCapacity = newFramesCap; |
| 4883 |
} |
| 4884 |
|
| 4885 |
DRMP3_COPY_MEMORY(pFrames + totalFramesRead*pMP3->channels, temp, (size_t)(framesJustRead*pMP3->channels*sizeof(drmp3_int16))); |
| 4886 |
totalFramesRead += framesJustRead; |
| 4887 |
|
| 4888 |
|
| 4889 |
if (framesJustRead != framesToReadRightNow) { |
| 4890 |
break; |
| 4891 |
} |
| 4892 |
} |
| 4893 |
|
| 4894 |
if (pConfig != NULL) { |
| 4895 |
pConfig->channels = pMP3->channels; |
| 4896 |
pConfig->sampleRate = pMP3->sampleRate; |
| 4897 |
} |
| 4898 |
|
| 4899 |
drmp3_uninit(pMP3); |
| 4900 |
|
| 4901 |
if (pTotalFrameCount) { |
| 4902 |
*pTotalFrameCount = totalFramesRead; |
| 4903 |
} |
| 4904 |
|
| 4905 |
return pFrames; |
| 4906 |
} |
| 4907 |
|
| 4908 |
|
| 4909 |
DRMP3_API float* drmp3_open_and_read_pcm_frames_f32(drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 4910 |
{ |
| 4911 |
drmp3 mp3; |
| 4912 |
if (!drmp3_init(&mp3, onRead, onSeek, onTell, NULL, pUserData, pAllocationCallbacks)) { |
| 4913 |
return NULL; |
| 4914 |
} |
| 4915 |
|
| 4916 |
return drmp3__full_read_and_close_f32(&mp3, pConfig, pTotalFrameCount); |
| 4917 |
} |
| 4918 |
|
| 4919 |
DRMP3_API drmp3_int16* drmp3_open_and_read_pcm_frames_s16(drmp3_read_proc onRead, drmp3_seek_proc onSeek, drmp3_tell_proc onTell, void* pUserData, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 4920 |
{ |
| 4921 |
drmp3 mp3; |
| 4922 |
if (!drmp3_init(&mp3, onRead, onSeek, onTell, NULL, pUserData, pAllocationCallbacks)) { |
| 4923 |
return NULL; |
| 4924 |
} |
| 4925 |
|
| 4926 |
return drmp3__full_read_and_close_s16(&mp3, pConfig, pTotalFrameCount); |
| 4927 |
} |
| 4928 |
|
| 4929 |
|
| 4930 |
DRMP3_API float* drmp3_open_memory_and_read_pcm_frames_f32(const void* pData, size_t dataSize, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 4931 |
{ |
| 4932 |
drmp3 mp3; |
| 4933 |
if (!drmp3_init_memory(&mp3, pData, dataSize, pAllocationCallbacks)) { |
| 4934 |
return NULL; |
| 4935 |
} |
| 4936 |
|
| 4937 |
return drmp3__full_read_and_close_f32(&mp3, pConfig, pTotalFrameCount); |
| 4938 |
} |
| 4939 |
|
| 4940 |
DRMP3_API drmp3_int16* drmp3_open_memory_and_read_pcm_frames_s16(const void* pData, size_t dataSize, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 4941 |
{ |
| 4942 |
drmp3 mp3; |
| 4943 |
if (!drmp3_init_memory(&mp3, pData, dataSize, pAllocationCallbacks)) { |
| 4944 |
return NULL; |
| 4945 |
} |
| 4946 |
|
| 4947 |
return drmp3__full_read_and_close_s16(&mp3, pConfig, pTotalFrameCount); |
| 4948 |
} |
| 4949 |
|
| 4950 |
|
| 4951 |
#ifndef DR_MP3_NO_STDIO |
| 4952 |
DRMP3_API float* drmp3_open_file_and_read_pcm_frames_f32(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 4953 |
{ |
| 4954 |
drmp3 mp3; |
| 4955 |
if (!drmp3_init_file(&mp3, filePath, pAllocationCallbacks)) { |
| 4956 |
return NULL; |
| 4957 |
} |
| 4958 |
|
| 4959 |
return drmp3__full_read_and_close_f32(&mp3, pConfig, pTotalFrameCount); |
| 4960 |
} |
| 4961 |
|
| 4962 |
DRMP3_API drmp3_int16* drmp3_open_file_and_read_pcm_frames_s16(const char* filePath, drmp3_config* pConfig, drmp3_uint64* pTotalFrameCount, const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 4963 |
{ |
| 4964 |
drmp3 mp3; |
| 4965 |
if (!drmp3_init_file(&mp3, filePath, pAllocationCallbacks)) { |
| 4966 |
return NULL; |
| 4967 |
} |
| 4968 |
|
| 4969 |
return drmp3__full_read_and_close_s16(&mp3, pConfig, pTotalFrameCount); |
| 4970 |
} |
| 4971 |
#endif |
| 4972 |
|
| 4973 |
DRMP3_API void* drmp3_malloc(size_t sz, const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 4974 |
{ |
| 4975 |
if (pAllocationCallbacks != NULL) { |
| 4976 |
return drmp3__malloc_from_callbacks(sz, pAllocationCallbacks); |
| 4977 |
} else { |
| 4978 |
return drmp3__malloc_default(sz, NULL); |
| 4979 |
} |
| 4980 |
} |
| 4981 |
|
| 4982 |
DRMP3_API void drmp3_free(void* p, const drmp3_allocation_callbacks* pAllocationCallbacks) |
| 4983 |
{ |
| 4984 |
if (pAllocationCallbacks != NULL) { |
| 4985 |
drmp3__free_from_callbacks(p, pAllocationCallbacks); |
| 4986 |
} else { |
| 4987 |
drmp3__free_default(p, NULL); |
| 4988 |
} |
| 4989 |
} |
| 4990 |
|
| 4991 |
#endif |
| 4992 |
#endif |
| 4993 |
|
| 4994 |
|
| 4995 |
|
| 4996 |
|
| 4997 |
|
| 4998 |
|
| 4999 |
|
| 5000 |
|
| 5001 |
|
| 5002 |
|
| 5003 |
|
| 5004 |
|
| 5005 |
|
| 5006 |
|
| 5007 |
|
| 5008 |
|
| 5009 |
|
| 5010 |
|
| 5011 |
|
| 5012 |
|
| 5013 |
|
| 5014 |
|
| 5015 |
|
| 5016 |
|
| 5017 |
|
| 5018 |
|
| 5019 |
|
| 5020 |
|
| 5021 |
|
| 5022 |
|
| 5023 |
|
| 5024 |
|
| 5025 |
|
| 5026 |
|
| 5027 |
|
| 5028 |
|
| 5029 |
|
| 5030 |
|
| 5031 |
|
| 5032 |
|
| 5033 |
|
| 5034 |
|
| 5035 |
|
| 5036 |
|
| 5037 |
|
| 5038 |
|
| 5039 |
|
| 5040 |
|
| 5041 |
|
| 5042 |
|
| 5043 |
|
| 5044 |
|
| 5045 |
|
| 5046 |
|
| 5047 |
|
| 5048 |
|
| 5049 |
|
| 5050 |
|
| 5051 |
|
| 5052 |
|
| 5053 |
|
| 5054 |
|
| 5055 |
|
| 5056 |
|
| 5057 |
|
| 5058 |
|
| 5059 |
|
| 5060 |
|
| 5061 |
|
| 5062 |
|
| 5063 |
|
| 5064 |
|
| 5065 |
|
| 5066 |
|
| 5067 |
|
| 5068 |
|
| 5069 |
|
| 5070 |
|
| 5071 |
|
| 5072 |
|
| 5073 |
|
| 5074 |
|
| 5075 |
|
| 5076 |
|
| 5077 |
|
| 5078 |
|
| 5079 |
|
| 5080 |
|
| 5081 |
|
| 5082 |
|
| 5083 |
|
| 5084 |
|
| 5085 |
|
| 5086 |
|
| 5087 |
|
| 5088 |
|
| 5089 |
|
| 5090 |
|
| 5091 |
|
| 5092 |
|
| 5093 |
|
| 5094 |
|
| 5095 |
|
| 5096 |
|
| 5097 |
|
| 5098 |
|
| 5099 |
|
| 5100 |
|
| 5101 |
|
| 5102 |
|
| 5103 |
|
| 5104 |
|
| 5105 |
|
| 5106 |
|
| 5107 |
|
| 5108 |
|
| 5109 |
|
| 5110 |
|
| 5111 |
|
| 5112 |
|
| 5113 |
|
| 5114 |
|
| 5115 |
|
| 5116 |
|
| 5117 |
|
| 5118 |
|
| 5119 |
|
| 5120 |
|
| 5121 |
|
| 5122 |
|
| 5123 |
|
| 5124 |
|
| 5125 |
|
| 5126 |
|
| 5127 |
|
| 5128 |
|
| 5129 |
|
| 5130 |
|
| 5131 |
|
| 5132 |
|
| 5133 |
|
| 5134 |
|
| 5135 |
|
| 5136 |
|
| 5137 |
|
| 5138 |
|
| 5139 |
|
| 5140 |
|
| 5141 |
|
| 5142 |
|
| 5143 |
|
| 5144 |
|
| 5145 |
|
| 5146 |
|
| 5147 |
|
| 5148 |
|
| 5149 |
|
| 5150 |
|
| 5151 |
|
| 5152 |
|
| 5153 |
|
| 5154 |
|
| 5155 |
|
| 5156 |
|
| 5157 |
|
| 5158 |
|
| 5159 |
|
| 5160 |
|
| 5161 |
|
| 5162 |
|
| 5163 |
|
| 5164 |
|
| 5165 |
|
| 5166 |
|
| 5167 |
|
| 5168 |
|
| 5169 |
|
| 5170 |
|
| 5171 |
|
| 5172 |
|
| 5173 |
|
| 5174 |
|
| 5175 |
|
| 5176 |
|
| 5177 |
|
| 5178 |
|
| 5179 |
|
| 5180 |
|
| 5181 |
|
| 5182 |
|
| 5183 |
|
| 5184 |
|
| 5185 |
|
| 5186 |
|
| 5187 |
|
| 5188 |
|
| 5189 |
|
| 5190 |
|
| 5191 |
|
| 5192 |
|
| 5193 |
|
| 5194 |
|
| 5195 |
|
| 5196 |
|
| 5197 |
|
| 5198 |
|
| 5199 |
|
| 5200 |
|
| 5201 |
|
| 5202 |
|
| 5203 |
|
| 5204 |
|
| 5205 |
|
| 5206 |
|
| 5207 |
|
| 5208 |
|
| 5209 |
|
| 5210 |
|
| 5211 |
|
| 5212 |
|
| 5213 |
|
| 5214 |
|
| 5215 |
|
| 5216 |
|
| 5217 |
|
| 5218 |
|
| 5219 |
|
| 5220 |
|
| 5221 |
|
| 5222 |
|
| 5223 |
|
| 5224 |
|
| 5225 |
|
| 5226 |
|
| 5227 |
|
| 5228 |
|
| 5229 |
|
| 5230 |
|
| 5231 |
|
| 5232 |
|
| 5233 |
|
| 5234 |
|
| 5235 |
|
| 5236 |
|
| 5237 |
|
| 5238 |
|
| 5239 |
|
| 5240 |
|
| 5241 |
|
| 5242 |
|
| 5243 |
|
| 5244 |
|
| 5245 |
|
| 5246 |
|
| 5247 |
|
| 5248 |
|
| 5249 |
|
| 5250 |
|
| 5251 |
|
| 5252 |
|
| 5253 |
|
| 5254 |
|
| 5255 |
|
| 5256 |
|
| 5257 |
|
| 5258 |
|
| 5259 |
|
| 5260 |
|
| 5261 |
|
| 5262 |
|
| 5263 |
|
| 5264 |
|
| 5265 |
|
| 5266 |
|
| 5267 |
|
| 5268 |
|
| 5269 |
|
| 5270 |
|
| 5271 |
|
| 5272 |
|
| 5273 |
|
| 5274 |
|
| 5275 |
|
| 5276 |
|
| 5277 |
|
| 5278 |
|
| 5279 |
|
| 5280 |
|
| 5281 |
|
| 5282 |
|
| 5283 |
|
| 5284 |
|
| 5285 |
|
| 5286 |
|
| 5287 |
|
| 5288 |
|
| 5289 |
|
| 5290 |
|
| 5291 |
|
| 5292 |
|
| 5293 |
|
| 5294 |
|
| 5295 |
|
| 5296 |
|
| 5297 |
|
| 5298 |
|
| 5299 |
|
| 5300 |
|
| 5301 |
|
| 5302 |
|
| 5303 |
|
| 5304 |
|
| 5305 |
|
| 5306 |
|
| 5307 |
|
| 5308 |
|
| 5309 |
|
| 5310 |
|
| 5311 |
|
| 5312 |
|
| 5313 |
|
| 5314 |
|
| 5315 |
|
| 5316 |
|
| 5317 |
|
| 5318 |
|
| 5319 |
|
| 5320 |
|
| 5321 |
|
| 5322 |
|
| 5323 |
|
| 5324 |
|
| 5325 |
|
| 5326 |
|
| 5327 |
|
| 5328 |
|
| 5329 |
|
| 5330 |
|
| 5331 |
|
| 5332 |
|
| 5333 |
|
| 5334 |
|
| 5335 |
|
| 5336 |
|
| 5337 |
|
| 5338 |
|
| 5339 |
|
| 5340 |
|
| 5341 |
|
| 5342 |
|
| 5343 |
|
| 5344 |
|
| 5345 |
|
| 5346 |
|
| 5347 |
|
| 5348 |
|
| 5349 |
|
| 5350 |
|
| 5351 |
|
| 5352 |
|
| 5353 |
|
| 5354 |
|
| 5355 |
|
| 5356 |
|
| 5357 |
|
| 5358 |
|
| 5359 |
|
| 5360 |
|
| 5361 |
|
| 5362 |
|
| 5363 |
|
| 5364 |
|
| 5365 |
|
| 5366 |
|
| 5367 |
|
| 5368 |
|
| 5369 |
|
| 5370 |
|
| 5371 |
|
| 5372 |
|
| 5373 |
|
| 5374 |
|
| 5375 |
|
| 5376 |
|
| 5377 |
|
| 5378 |
|
| 5379 |
|
| 5380 |
|
| 5381 |
|
| 5382 |
|
| 5383 |
|
| 5384 |
|
| 5385 |
|