Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1370 lines
48KB

  1. #ifndef MINIMP3_EXT_H
  2. #define MINIMP3_EXT_H
  3. /*
  4. https://github.com/lieff/minimp3
  5. To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide.
  6. This software is distributed without any warranty.
  7. See <http://creativecommons.org/publicdomain/zero/1.0/>.
  8. */
  9. #include "minimp3.h"
  10. /* flags for mp3dec_ex_open_* functions */
  11. #define MP3D_SEEK_TO_BYTE 0 /* mp3dec_ex_seek seeks to byte in stream */
  12. #define MP3D_SEEK_TO_SAMPLE 1 /* mp3dec_ex_seek precisely seeks to sample using index (created during duration calculation scan or when mp3dec_ex_seek called) */
  13. #define MP3D_DO_NOT_SCAN 2 /* do not scan whole stream for duration if vbrtag not found, mp3dec_ex_t::samples will be filled only if mp3dec_ex_t::vbr_tag_found == 1 */
  14. /* compile-time config */
  15. #define MINIMP3_PREDECODE_FRAMES 2 /* frames to pre-decode and skip after seek (to fill internal structures) */
  16. /*#define MINIMP3_SEEK_IDX_LINEAR_SEARCH*/ /* define to use linear index search instead of binary search on seek */
  17. #define MINIMP3_IO_SIZE (128*1024) /* io buffer size for streaming functions, must be greater than MINIMP3_BUF_SIZE */
  18. #define MINIMP3_BUF_SIZE (16*1024) /* buffer which can hold minimum 10 consecutive mp3 frames (~16KB) worst case */
  19. /*#define MINIMP3_SCAN_LIMIT (256*1024)*/ /* how many bytes will be scanned to search first valid mp3 frame, to prevent stall on large non-mp3 files */
  20. #define MINIMP3_ENABLE_RING 0 /* WIP enable hardware magic ring buffer if available, to make less input buffer memmove(s) in callback IO mode */
  21. /* return error codes */
  22. #define MP3D_E_PARAM -1
  23. #define MP3D_E_MEMORY -2
  24. #define MP3D_E_IOERROR -3
  25. #define MP3D_E_USER -4 /* can be used to stop processing from callbacks without indicating specific error */
  26. #define MP3D_E_DECODE -5 /* decode error which can't be safely skipped, such as sample rate, layer and channels change */
  27. typedef struct
  28. {
  29. mp3d_sample_t *buffer;
  30. size_t samples; /* channels included, byte size = samples*sizeof(mp3d_sample_t) */
  31. int channels, hz, layer, avg_bitrate_kbps;
  32. } mp3dec_file_info_t;
  33. typedef struct
  34. {
  35. const uint8_t *buffer;
  36. size_t size;
  37. } mp3dec_map_info_t;
  38. typedef struct
  39. {
  40. uint64_t sample;
  41. uint64_t offset;
  42. } mp3dec_frame_t;
  43. typedef struct
  44. {
  45. mp3dec_frame_t *frames;
  46. size_t num_frames, capacity;
  47. } mp3dec_index_t;
  48. typedef size_t (*MP3D_READ_CB)(void *buf, size_t size, void *user_data);
  49. typedef int (*MP3D_SEEK_CB)(uint64_t position, void *user_data);
  50. typedef struct
  51. {
  52. MP3D_READ_CB read;
  53. void *read_data;
  54. MP3D_SEEK_CB seek;
  55. void *seek_data;
  56. } mp3dec_io_t;
  57. typedef struct
  58. {
  59. mp3dec_t mp3d;
  60. mp3dec_map_info_t file;
  61. mp3dec_io_t *io;
  62. mp3dec_index_t index;
  63. uint64_t offset, samples, detected_samples, cur_sample, start_offset, end_offset;
  64. mp3dec_frame_info_t info;
  65. mp3d_sample_t buffer[MINIMP3_MAX_SAMPLES_PER_FRAME];
  66. size_t input_consumed, input_filled;
  67. int is_file, flags, vbr_tag_found, indexes_built;
  68. int free_format_bytes;
  69. int buffer_samples, buffer_consumed, to_skip, start_delay;
  70. int last_error;
  71. } mp3dec_ex_t;
  72. typedef int (*MP3D_ITERATE_CB)(void *user_data, const uint8_t *frame, int frame_size, int free_format_bytes, size_t buf_size, uint64_t offset, mp3dec_frame_info_t *info);
  73. typedef int (*MP3D_PROGRESS_CB)(void *user_data, size_t file_size, uint64_t offset, mp3dec_frame_info_t *info);
  74. #ifdef __cplusplus
  75. extern "C" {
  76. #endif
  77. /* detect mp3/mpa format */
  78. int mp3dec_detect_buf(const uint8_t *buf, size_t buf_size);
  79. int mp3dec_detect_cb(mp3dec_io_t *io, uint8_t *buf, size_t buf_size);
  80. /* decode whole buffer block */
  81. int mp3dec_load_buf(mp3dec_t *dec, const uint8_t *buf, size_t buf_size, mp3dec_file_info_t *info, MP3D_PROGRESS_CB progress_cb, void *user_data);
  82. int mp3dec_load_cb(mp3dec_t *dec, mp3dec_io_t *io, uint8_t *buf, size_t buf_size, mp3dec_file_info_t *info, MP3D_PROGRESS_CB progress_cb, void *user_data);
  83. /* iterate through frames */
  84. int mp3dec_iterate_buf(const uint8_t *buf, size_t buf_size, MP3D_ITERATE_CB callback, void *user_data);
  85. int mp3dec_iterate_cb(mp3dec_io_t *io, uint8_t *buf, size_t buf_size, MP3D_ITERATE_CB callback, void *user_data);
  86. /* streaming decoder with seeking capability */
  87. int mp3dec_ex_open_buf(mp3dec_ex_t *dec, const uint8_t *buf, size_t buf_size, int flags);
  88. int mp3dec_ex_open_cb(mp3dec_ex_t *dec, mp3dec_io_t *io, int flags);
  89. void mp3dec_ex_close(mp3dec_ex_t *dec);
  90. int mp3dec_ex_seek(mp3dec_ex_t *dec, uint64_t position);
  91. size_t mp3dec_ex_read(mp3dec_ex_t *dec, mp3d_sample_t *buf, size_t samples);
  92. #ifndef MINIMP3_NO_STDIO
  93. /* stdio versions of file detect, load, iterate and stream */
  94. int mp3dec_detect(const char *file_name);
  95. int mp3dec_load(mp3dec_t *dec, const char *file_name, mp3dec_file_info_t *info, MP3D_PROGRESS_CB progress_cb, void *user_data);
  96. int mp3dec_iterate(const char *file_name, MP3D_ITERATE_CB callback, void *user_data);
  97. int mp3dec_ex_open(mp3dec_ex_t *dec, const char *file_name, int flags);
  98. #ifdef _WIN32
  99. int mp3dec_detect_w(const wchar_t *file_name);
  100. int mp3dec_load_w(mp3dec_t *dec, const wchar_t *file_name, mp3dec_file_info_t *info, MP3D_PROGRESS_CB progress_cb, void *user_data);
  101. int mp3dec_iterate_w(const wchar_t *file_name, MP3D_ITERATE_CB callback, void *user_data);
  102. int mp3dec_ex_open_w(mp3dec_ex_t *dec, const wchar_t *file_name, int flags);
  103. #endif
  104. #endif
  105. #ifdef __cplusplus
  106. }
  107. #endif
  108. #endif /*MINIMP3_EXT_H*/
  109. #ifdef MINIMP3_IMPLEMENTATION
  110. #include <limits.h>
  111. static void mp3dec_skip_id3v1(const uint8_t *buf, size_t *pbuf_size)
  112. {
  113. size_t buf_size = *pbuf_size;
  114. #ifndef MINIMP3_NOSKIP_ID3V1
  115. if (buf_size >= 128 && !memcmp(buf + buf_size - 128, "TAG", 3))
  116. {
  117. buf_size -= 128;
  118. if (buf_size >= 227 && !memcmp(buf + buf_size - 227, "TAG+", 4))
  119. buf_size -= 227;
  120. }
  121. #endif
  122. #ifndef MINIMP3_NOSKIP_APEV2
  123. if (buf_size > 32 && !memcmp(buf + buf_size - 32, "APETAGEX", 8))
  124. {
  125. buf_size -= 32;
  126. const uint8_t *tag = buf + buf_size + 8 + 4;
  127. uint32_t tag_size = (uint32_t)(tag[3] << 24) | (tag[2] << 16) | (tag[1] << 8) | tag[0];
  128. if (buf_size >= tag_size)
  129. buf_size -= tag_size;
  130. }
  131. #endif
  132. *pbuf_size = buf_size;
  133. }
  134. static size_t mp3dec_skip_id3v2(const uint8_t *buf, size_t buf_size)
  135. {
  136. #define MINIMP3_ID3_DETECT_SIZE 10
  137. #ifndef MINIMP3_NOSKIP_ID3V2
  138. if (buf_size >= MINIMP3_ID3_DETECT_SIZE && !memcmp(buf, "ID3", 3) && !((buf[5] & 15) || (buf[6] & 0x80) || (buf[7] & 0x80) || (buf[8] & 0x80) || (buf[9] & 0x80)))
  139. {
  140. size_t id3v2size = (((buf[6] & 0x7f) << 21) | ((buf[7] & 0x7f) << 14) | ((buf[8] & 0x7f) << 7) | (buf[9] & 0x7f)) + 10;
  141. if ((buf[5] & 16))
  142. id3v2size += 10; /* footer */
  143. return id3v2size;
  144. }
  145. #endif
  146. return 0;
  147. }
  148. static void mp3dec_skip_id3(const uint8_t **pbuf, size_t *pbuf_size)
  149. {
  150. uint8_t *buf = (uint8_t *)(*pbuf);
  151. size_t buf_size = *pbuf_size;
  152. size_t id3v2size = mp3dec_skip_id3v2(buf, buf_size);
  153. if (id3v2size)
  154. {
  155. if (id3v2size >= buf_size)
  156. id3v2size = buf_size;
  157. buf += id3v2size;
  158. buf_size -= id3v2size;
  159. }
  160. mp3dec_skip_id3v1(buf, &buf_size);
  161. *pbuf = (const uint8_t *)buf;
  162. *pbuf_size = buf_size;
  163. }
  164. static int mp3dec_check_vbrtag(const uint8_t *frame, int frame_size, uint32_t *frames, int *delay, int *padding)
  165. {
  166. static const char g_xing_tag[4] = { 'X', 'i', 'n', 'g' };
  167. static const char g_info_tag[4] = { 'I', 'n', 'f', 'o' };
  168. #define FRAMES_FLAG 1
  169. #define BYTES_FLAG 2
  170. #define TOC_FLAG 4
  171. #define VBR_SCALE_FLAG 8
  172. /* Side info offsets after header:
  173. / Mono Stereo
  174. / MPEG1 17 32
  175. / MPEG2 & 2.5 9 17*/
  176. bs_t bs[1];
  177. L3_gr_info_t gr_info[4];
  178. bs_init(bs, frame + HDR_SIZE, frame_size - HDR_SIZE);
  179. if (HDR_IS_CRC(frame))
  180. get_bits(bs, 16);
  181. if (L3_read_side_info(bs, gr_info, frame) < 0)
  182. return 0; /* side info corrupted */
  183. const uint8_t *tag = frame + HDR_SIZE + bs->pos/8;
  184. if (memcmp(g_xing_tag, tag, 4) && memcmp(g_info_tag, tag, 4))
  185. return 0;
  186. int flags = tag[7];
  187. if (!((flags & FRAMES_FLAG)))
  188. return -1;
  189. tag += 8;
  190. *frames = (uint32_t)(tag[0] << 24) | (tag[1] << 16) | (tag[2] << 8) | tag[3];
  191. tag += 4;
  192. if (flags & BYTES_FLAG)
  193. tag += 4;
  194. if (flags & TOC_FLAG)
  195. tag += 100;
  196. if (flags & VBR_SCALE_FLAG)
  197. tag += 4;
  198. *delay = *padding = 0;
  199. if (*tag)
  200. { /* extension, LAME, Lavc, etc. Should be the same structure. */
  201. tag += 21;
  202. if (tag - frame + 14 >= frame_size)
  203. return 0;
  204. *delay = ((tag[0] << 4) | (tag[1] >> 4)) + (528 + 1);
  205. *padding = (((tag[1] & 0xF) << 8) | tag[2]) - (528 + 1);
  206. }
  207. return 1;
  208. }
  209. int mp3dec_detect_buf(const uint8_t *buf, size_t buf_size)
  210. {
  211. return mp3dec_detect_cb(0, (uint8_t *)buf, buf_size);
  212. }
  213. int mp3dec_detect_cb(mp3dec_io_t *io, uint8_t *buf, size_t buf_size)
  214. {
  215. if (!buf || (size_t)-1 == buf_size || (io && buf_size < MINIMP3_BUF_SIZE))
  216. return MP3D_E_PARAM;
  217. size_t filled = buf_size;
  218. if (io)
  219. {
  220. if (io->seek(0, io->seek_data))
  221. return MP3D_E_IOERROR;
  222. filled = io->read(buf, MINIMP3_ID3_DETECT_SIZE, io->read_data);
  223. if (filled > MINIMP3_ID3_DETECT_SIZE)
  224. return MP3D_E_IOERROR;
  225. }
  226. if (filled < MINIMP3_ID3_DETECT_SIZE)
  227. return MP3D_E_USER; /* too small, can't be mp3/mpa */
  228. if (mp3dec_skip_id3v2(buf, filled))
  229. return 0; /* id3v2 tag is enough evidence */
  230. if (io)
  231. {
  232. size_t readed = io->read(buf + MINIMP3_ID3_DETECT_SIZE, buf_size - MINIMP3_ID3_DETECT_SIZE, io->read_data);
  233. if (readed > (buf_size - MINIMP3_ID3_DETECT_SIZE))
  234. return MP3D_E_IOERROR;
  235. filled += readed;
  236. if (filled < MINIMP3_BUF_SIZE)
  237. mp3dec_skip_id3v1(buf, &filled);
  238. } else
  239. {
  240. mp3dec_skip_id3v1(buf, &filled);
  241. if (filled > MINIMP3_BUF_SIZE)
  242. filled = MINIMP3_BUF_SIZE;
  243. }
  244. int free_format_bytes, frame_size;
  245. mp3d_find_frame(buf, filled, &free_format_bytes, &frame_size);
  246. if (frame_size)
  247. return 0; /* MAX_FRAME_SYNC_MATCHES consecutive frames found */
  248. return MP3D_E_USER;
  249. }
  250. int mp3dec_load_buf(mp3dec_t *dec, const uint8_t *buf, size_t buf_size, mp3dec_file_info_t *info, MP3D_PROGRESS_CB progress_cb, void *user_data)
  251. {
  252. return mp3dec_load_cb(dec, 0, (uint8_t *)buf, buf_size, info, progress_cb, user_data);
  253. }
  254. int mp3dec_load_cb(mp3dec_t *dec, mp3dec_io_t *io, uint8_t *buf, size_t buf_size, mp3dec_file_info_t *info, MP3D_PROGRESS_CB progress_cb, void *user_data)
  255. {
  256. if (!dec || !buf || !info || (size_t)-1 == buf_size || (io && buf_size < MINIMP3_BUF_SIZE))
  257. return MP3D_E_PARAM;
  258. uint64_t detected_samples = 0;
  259. size_t orig_buf_size = buf_size;
  260. int to_skip = 0;
  261. mp3dec_frame_info_t frame_info;
  262. memset(info, 0, sizeof(*info));
  263. memset(&frame_info, 0, sizeof(frame_info));
  264. /* skip id3 */
  265. size_t filled = 0, consumed = 0;
  266. int eof = 0, ret = 0;
  267. if (io)
  268. {
  269. if (io->seek(0, io->seek_data))
  270. return MP3D_E_IOERROR;
  271. filled = io->read(buf, MINIMP3_ID3_DETECT_SIZE, io->read_data);
  272. if (filled > MINIMP3_ID3_DETECT_SIZE)
  273. return MP3D_E_IOERROR;
  274. if (MINIMP3_ID3_DETECT_SIZE != filled)
  275. return 0;
  276. size_t id3v2size = mp3dec_skip_id3v2(buf, filled);
  277. if (id3v2size)
  278. {
  279. if (io->seek(id3v2size, io->seek_data))
  280. return MP3D_E_IOERROR;
  281. filled = io->read(buf, buf_size, io->read_data);
  282. if (filled > buf_size)
  283. return MP3D_E_IOERROR;
  284. } else
  285. {
  286. size_t readed = io->read(buf + MINIMP3_ID3_DETECT_SIZE, buf_size - MINIMP3_ID3_DETECT_SIZE, io->read_data);
  287. if (readed > (buf_size - MINIMP3_ID3_DETECT_SIZE))
  288. return MP3D_E_IOERROR;
  289. filled += readed;
  290. }
  291. if (filled < MINIMP3_BUF_SIZE)
  292. mp3dec_skip_id3v1(buf, &filled);
  293. } else
  294. {
  295. mp3dec_skip_id3((const uint8_t **)&buf, &buf_size);
  296. if (!buf_size)
  297. return 0;
  298. }
  299. /* try to make allocation size assumption by first frame or vbr tag */
  300. mp3dec_init(dec);
  301. int samples;
  302. do
  303. {
  304. uint32_t frames;
  305. int i, delay, padding, free_format_bytes = 0, frame_size = 0;
  306. const uint8_t *hdr;
  307. if (io)
  308. {
  309. if (!eof && filled - consumed < MINIMP3_BUF_SIZE)
  310. { /* keep minimum 10 consecutive mp3 frames (~16KB) worst case */
  311. memmove(buf, buf + consumed, filled - consumed);
  312. filled -= consumed;
  313. consumed = 0;
  314. size_t readed = io->read(buf + filled, buf_size - filled, io->read_data);
  315. if (readed > (buf_size - filled))
  316. return MP3D_E_IOERROR;
  317. if (readed != (buf_size - filled))
  318. eof = 1;
  319. filled += readed;
  320. if (eof)
  321. mp3dec_skip_id3v1(buf, &filled);
  322. }
  323. i = mp3d_find_frame(buf + consumed, filled - consumed, &free_format_bytes, &frame_size);
  324. consumed += i;
  325. hdr = buf + consumed;
  326. } else
  327. {
  328. i = mp3d_find_frame(buf, buf_size, &free_format_bytes, &frame_size);
  329. buf += i;
  330. buf_size -= i;
  331. hdr = buf;
  332. }
  333. if (i && !frame_size)
  334. continue;
  335. if (!frame_size)
  336. return 0;
  337. frame_info.channels = HDR_IS_MONO(hdr) ? 1 : 2;
  338. frame_info.hz = hdr_sample_rate_hz(hdr);
  339. frame_info.layer = 4 - HDR_GET_LAYER(hdr);
  340. frame_info.bitrate_kbps = hdr_bitrate_kbps(hdr);
  341. frame_info.frame_bytes = frame_size;
  342. samples = hdr_frame_samples(hdr)*frame_info.channels;
  343. if (3 != frame_info.layer)
  344. break;
  345. int ret = mp3dec_check_vbrtag(hdr, frame_size, &frames, &delay, &padding);
  346. if (ret > 0)
  347. {
  348. padding *= frame_info.channels;
  349. to_skip = delay*frame_info.channels;
  350. detected_samples = samples*(uint64_t)frames;
  351. if (detected_samples >= (uint64_t)to_skip)
  352. detected_samples -= to_skip;
  353. if (padding > 0 && detected_samples >= (uint64_t)padding)
  354. detected_samples -= padding;
  355. if (!detected_samples)
  356. return 0;
  357. }
  358. if (ret)
  359. {
  360. if (io)
  361. {
  362. consumed += frame_size;
  363. } else
  364. {
  365. buf += frame_size;
  366. buf_size -= frame_size;
  367. }
  368. }
  369. break;
  370. } while(1);
  371. size_t allocated = MINIMP3_MAX_SAMPLES_PER_FRAME*sizeof(mp3d_sample_t);
  372. if (detected_samples)
  373. allocated += detected_samples*sizeof(mp3d_sample_t);
  374. else
  375. allocated += (buf_size/frame_info.frame_bytes)*samples*sizeof(mp3d_sample_t);
  376. info->buffer = (mp3d_sample_t*)malloc(allocated);
  377. if (!info->buffer)
  378. return MP3D_E_MEMORY;
  379. /* save info */
  380. info->channels = frame_info.channels;
  381. info->hz = frame_info.hz;
  382. info->layer = frame_info.layer;
  383. /* decode all frames */
  384. size_t avg_bitrate_kbps = 0, frames = 0;
  385. do
  386. {
  387. if ((allocated - info->samples*sizeof(mp3d_sample_t)) < MINIMP3_MAX_SAMPLES_PER_FRAME*sizeof(mp3d_sample_t))
  388. {
  389. allocated *= 2;
  390. mp3d_sample_t *alloc_buf = (mp3d_sample_t*)realloc(info->buffer, allocated);
  391. if (!alloc_buf)
  392. return MP3D_E_MEMORY;
  393. info->buffer = alloc_buf;
  394. }
  395. if (io)
  396. {
  397. if (!eof && filled - consumed < MINIMP3_BUF_SIZE)
  398. { /* keep minimum 10 consecutive mp3 frames (~16KB) worst case */
  399. memmove(buf, buf + consumed, filled - consumed);
  400. filled -= consumed;
  401. consumed = 0;
  402. size_t readed = io->read(buf + filled, buf_size - filled, io->read_data);
  403. if (readed != (buf_size - filled))
  404. eof = 1;
  405. filled += readed;
  406. if (eof)
  407. mp3dec_skip_id3v1(buf, &filled);
  408. }
  409. samples = mp3dec_decode_frame(dec, buf + consumed, filled - consumed, info->buffer + info->samples, &frame_info);
  410. consumed += frame_info.frame_bytes;
  411. } else
  412. {
  413. samples = mp3dec_decode_frame(dec, buf, MINIMP3_MIN(buf_size, (size_t)INT_MAX), info->buffer + info->samples, &frame_info);
  414. buf += frame_info.frame_bytes;
  415. buf_size -= frame_info.frame_bytes;
  416. }
  417. if (samples)
  418. {
  419. if (info->hz != frame_info.hz || info->layer != frame_info.layer)
  420. {
  421. ret = MP3D_E_DECODE;
  422. break;
  423. }
  424. if (info->channels && info->channels != frame_info.channels)
  425. {
  426. #ifdef MINIMP3_ALLOW_MONO_STEREO_TRANSITION
  427. info->channels = 0; /* mark file with mono-stereo transition */
  428. #else
  429. ret = MP3D_E_DECODE;
  430. break;
  431. #endif
  432. }
  433. samples *= frame_info.channels;
  434. if (to_skip)
  435. {
  436. size_t skip = MINIMP3_MIN(samples, to_skip);
  437. to_skip -= skip;
  438. samples -= skip;
  439. memmove(info->buffer, info->buffer + skip, samples*sizeof(mp3d_sample_t));
  440. }
  441. info->samples += samples;
  442. avg_bitrate_kbps += frame_info.bitrate_kbps;
  443. frames++;
  444. if (progress_cb)
  445. {
  446. ret = progress_cb(user_data, orig_buf_size, orig_buf_size - buf_size, &frame_info);
  447. if (ret)
  448. break;
  449. }
  450. }
  451. } while (frame_info.frame_bytes);
  452. if (detected_samples && info->samples > detected_samples)
  453. info->samples = detected_samples; /* cut padding */
  454. /* reallocate to normal buffer size */
  455. if (allocated != info->samples*sizeof(mp3d_sample_t))
  456. {
  457. mp3d_sample_t *alloc_buf = (mp3d_sample_t*)realloc(info->buffer, info->samples*sizeof(mp3d_sample_t));
  458. if (!alloc_buf && info->samples)
  459. return MP3D_E_MEMORY;
  460. info->buffer = alloc_buf;
  461. }
  462. if (frames)
  463. info->avg_bitrate_kbps = avg_bitrate_kbps/frames;
  464. return ret;
  465. }
  466. int mp3dec_iterate_buf(const uint8_t *buf, size_t buf_size, MP3D_ITERATE_CB callback, void *user_data)
  467. {
  468. const uint8_t *orig_buf = buf;
  469. if (!buf || (size_t)-1 == buf_size || !callback)
  470. return MP3D_E_PARAM;
  471. /* skip id3 */
  472. mp3dec_skip_id3(&buf, &buf_size);
  473. if (!buf_size)
  474. return 0;
  475. mp3dec_frame_info_t frame_info;
  476. memset(&frame_info, 0, sizeof(frame_info));
  477. do
  478. {
  479. int free_format_bytes = 0, frame_size = 0, ret;
  480. int i = mp3d_find_frame(buf, buf_size, &free_format_bytes, &frame_size);
  481. buf += i;
  482. buf_size -= i;
  483. if (i && !frame_size)
  484. continue;
  485. if (!frame_size)
  486. break;
  487. const uint8_t *hdr = buf;
  488. frame_info.channels = HDR_IS_MONO(hdr) ? 1 : 2;
  489. frame_info.hz = hdr_sample_rate_hz(hdr);
  490. frame_info.layer = 4 - HDR_GET_LAYER(hdr);
  491. frame_info.bitrate_kbps = hdr_bitrate_kbps(hdr);
  492. frame_info.frame_bytes = frame_size;
  493. if (callback)
  494. {
  495. if ((ret = callback(user_data, hdr, frame_size, free_format_bytes, buf_size, hdr - orig_buf, &frame_info)))
  496. return ret;
  497. }
  498. buf += frame_size;
  499. buf_size -= frame_size;
  500. } while (1);
  501. return 0;
  502. }
  503. int mp3dec_iterate_cb(mp3dec_io_t *io, uint8_t *buf, size_t buf_size, MP3D_ITERATE_CB callback, void *user_data)
  504. {
  505. if (!io || !buf || (size_t)-1 == buf_size || buf_size < MINIMP3_BUF_SIZE || !callback)
  506. return MP3D_E_PARAM;
  507. size_t filled = io->read(buf, MINIMP3_ID3_DETECT_SIZE, io->read_data), consumed = 0;
  508. uint64_t readed = 0;
  509. mp3dec_frame_info_t frame_info;
  510. int eof = 0;
  511. memset(&frame_info, 0, sizeof(frame_info));
  512. if (filled > MINIMP3_ID3_DETECT_SIZE)
  513. return MP3D_E_IOERROR;
  514. if (MINIMP3_ID3_DETECT_SIZE != filled)
  515. return 0;
  516. size_t id3v2size = mp3dec_skip_id3v2(buf, filled);
  517. if (id3v2size)
  518. {
  519. if (io->seek(id3v2size, io->seek_data))
  520. return MP3D_E_IOERROR;
  521. filled = io->read(buf, buf_size, io->read_data);
  522. if (filled > buf_size)
  523. return MP3D_E_IOERROR;
  524. readed += id3v2size;
  525. } else
  526. {
  527. size_t readed = io->read(buf + MINIMP3_ID3_DETECT_SIZE, buf_size - MINIMP3_ID3_DETECT_SIZE, io->read_data);
  528. if (readed > (buf_size - MINIMP3_ID3_DETECT_SIZE))
  529. return MP3D_E_IOERROR;
  530. filled += readed;
  531. }
  532. if (filled < MINIMP3_BUF_SIZE)
  533. mp3dec_skip_id3v1(buf, &filled);
  534. do
  535. {
  536. int free_format_bytes = 0, frame_size = 0, ret;
  537. int i = mp3d_find_frame(buf + consumed, filled - consumed, &free_format_bytes, &frame_size);
  538. if (i && !frame_size)
  539. {
  540. consumed += i;
  541. continue;
  542. }
  543. if (!frame_size)
  544. break;
  545. const uint8_t *hdr = buf + consumed + i;
  546. frame_info.channels = HDR_IS_MONO(hdr) ? 1 : 2;
  547. frame_info.hz = hdr_sample_rate_hz(hdr);
  548. frame_info.layer = 4 - HDR_GET_LAYER(hdr);
  549. frame_info.bitrate_kbps = hdr_bitrate_kbps(hdr);
  550. frame_info.frame_bytes = frame_size;
  551. readed += i;
  552. if (callback)
  553. {
  554. if ((ret = callback(user_data, hdr, frame_size, free_format_bytes, filled - consumed, readed, &frame_info)))
  555. return ret;
  556. }
  557. readed += frame_size;
  558. consumed += i + frame_size;
  559. if (!eof && filled - consumed < MINIMP3_BUF_SIZE)
  560. { /* keep minimum 10 consecutive mp3 frames (~16KB) worst case */
  561. memmove(buf, buf + consumed, filled - consumed);
  562. filled -= consumed;
  563. consumed = 0;
  564. size_t readed = io->read(buf + filled, buf_size - filled, io->read_data);
  565. if (readed > (buf_size - filled))
  566. return MP3D_E_IOERROR;
  567. if (readed != (buf_size - filled))
  568. eof = 1;
  569. filled += readed;
  570. if (eof)
  571. mp3dec_skip_id3v1(buf, &filled);
  572. }
  573. } while (1);
  574. return 0;
  575. }
  576. static int mp3dec_load_index(void *user_data, const uint8_t *frame, int frame_size, int free_format_bytes, size_t buf_size, uint64_t offset, mp3dec_frame_info_t *info)
  577. {
  578. mp3dec_frame_t *idx_frame;
  579. mp3dec_ex_t *dec = (mp3dec_ex_t *)user_data;
  580. if (!dec->index.frames && !dec->start_offset)
  581. { /* detect VBR tag and try to avoid full scan */
  582. uint32_t frames;
  583. int delay, padding;
  584. dec->info = *info;
  585. dec->start_offset = dec->offset = offset;
  586. dec->end_offset = offset + buf_size;
  587. dec->free_format_bytes = free_format_bytes; /* should not change */
  588. if (3 == dec->info.layer)
  589. {
  590. int ret = mp3dec_check_vbrtag(frame, frame_size, &frames, &delay, &padding);
  591. if (ret)
  592. dec->start_offset = dec->offset = offset + frame_size;
  593. if (ret > 0)
  594. {
  595. padding *= info->channels;
  596. dec->start_delay = dec->to_skip = delay*info->channels;
  597. dec->samples = hdr_frame_samples(frame)*info->channels*(uint64_t)frames;
  598. if (dec->samples >= (uint64_t)dec->start_delay)
  599. dec->samples -= dec->start_delay;
  600. if (padding > 0 && dec->samples >= (uint64_t)padding)
  601. dec->samples -= padding;
  602. dec->detected_samples = dec->samples;
  603. dec->vbr_tag_found = 1;
  604. return MP3D_E_USER;
  605. } else if (ret < 0)
  606. return 0;
  607. }
  608. }
  609. if (dec->flags & MP3D_DO_NOT_SCAN)
  610. return MP3D_E_USER;
  611. if (dec->index.num_frames + 1 > dec->index.capacity)
  612. {
  613. if (!dec->index.capacity)
  614. dec->index.capacity = 4096;
  615. else
  616. dec->index.capacity *= 2;
  617. mp3dec_frame_t *alloc_buf = (mp3dec_frame_t *)realloc((void*)dec->index.frames, sizeof(mp3dec_frame_t)*dec->index.capacity);
  618. if (!alloc_buf)
  619. return MP3D_E_MEMORY;
  620. dec->index.frames = alloc_buf;
  621. }
  622. idx_frame = &dec->index.frames[dec->index.num_frames++];
  623. idx_frame->offset = offset;
  624. idx_frame->sample = dec->samples;
  625. if (!dec->buffer_samples && dec->index.num_frames < 256)
  626. { /* for some cutted mp3 frames, bit-reservoir not filled and decoding can't be started from first frames */
  627. /* try to decode up to 255 first frames till samples starts to decode */
  628. dec->buffer_samples = mp3dec_decode_frame(&dec->mp3d, frame, MINIMP3_MIN(buf_size, (size_t)INT_MAX), dec->buffer, info);
  629. dec->samples += dec->buffer_samples*info->channels;
  630. } else
  631. dec->samples += hdr_frame_samples(frame)*info->channels;
  632. return 0;
  633. }
  634. int mp3dec_ex_open_buf(mp3dec_ex_t *dec, const uint8_t *buf, size_t buf_size, int flags)
  635. {
  636. if (!dec || !buf || (size_t)-1 == buf_size || (flags & (~3)))
  637. return MP3D_E_PARAM;
  638. memset(dec, 0, sizeof(*dec));
  639. dec->file.buffer = buf;
  640. dec->file.size = buf_size;
  641. dec->flags = flags;
  642. mp3dec_init(&dec->mp3d);
  643. int ret = mp3dec_iterate_buf(dec->file.buffer, dec->file.size, mp3dec_load_index, dec);
  644. if (ret && MP3D_E_USER != ret)
  645. return ret;
  646. mp3dec_init(&dec->mp3d);
  647. dec->buffer_samples = 0;
  648. dec->indexes_built = !(dec->vbr_tag_found || (flags & MP3D_DO_NOT_SCAN));
  649. dec->flags &= (~MP3D_DO_NOT_SCAN);
  650. return 0;
  651. }
  652. #ifndef MINIMP3_SEEK_IDX_LINEAR_SEARCH
  653. static size_t mp3dec_idx_binary_search(mp3dec_index_t *idx, uint64_t position)
  654. {
  655. size_t end = idx->num_frames, start = 0, index = 0;
  656. while (start <= end)
  657. {
  658. size_t mid = (start + end) / 2;
  659. if (idx->frames[mid].sample >= position)
  660. { /* move left side. */
  661. if (idx->frames[mid].sample == position)
  662. return mid;
  663. end = mid - 1;
  664. } else
  665. { /* move to right side */
  666. index = mid;
  667. start = mid + 1;
  668. if (start == idx->num_frames)
  669. break;
  670. }
  671. }
  672. return index;
  673. }
  674. #endif
  675. int mp3dec_ex_seek(mp3dec_ex_t *dec, uint64_t position)
  676. {
  677. size_t i;
  678. if (!dec)
  679. return MP3D_E_PARAM;
  680. if (!(dec->flags & MP3D_SEEK_TO_SAMPLE))
  681. {
  682. if (dec->io)
  683. {
  684. dec->offset = position;
  685. } else
  686. {
  687. dec->offset = MINIMP3_MIN(position, dec->file.size);
  688. }
  689. dec->cur_sample = 0;
  690. goto do_exit;
  691. }
  692. dec->cur_sample = position;
  693. position += dec->start_delay;
  694. if (0 == position)
  695. { /* optimize seek to zero, no index needed */
  696. seek_zero:
  697. dec->offset = dec->start_offset;
  698. dec->to_skip = 0;
  699. goto do_exit;
  700. }
  701. if (!dec->indexes_built)
  702. { /* no index created yet (vbr tag used to calculate track length or MP3D_DO_NOT_SCAN open flag used) */
  703. dec->indexes_built = 1;
  704. dec->samples = 0;
  705. dec->buffer_samples = 0;
  706. if (dec->io)
  707. {
  708. if (dec->io->seek(dec->start_offset, dec->io->seek_data))
  709. return MP3D_E_IOERROR;
  710. int ret = mp3dec_iterate_cb(dec->io, (uint8_t *)dec->file.buffer, dec->file.size, mp3dec_load_index, dec);
  711. if (ret && MP3D_E_USER != ret)
  712. return ret;
  713. } else
  714. {
  715. int ret = mp3dec_iterate_buf(dec->file.buffer + dec->start_offset, dec->file.size - dec->start_offset, mp3dec_load_index, dec);
  716. if (ret && MP3D_E_USER != ret)
  717. return ret;
  718. }
  719. for (i = 0; i < dec->index.num_frames; i++)
  720. dec->index.frames[i].offset += dec->start_offset;
  721. dec->samples = dec->detected_samples;
  722. }
  723. if (!dec->index.frames)
  724. goto seek_zero; /* no frames in file - seek to zero */
  725. #ifdef MINIMP3_SEEK_IDX_LINEAR_SEARCH
  726. for (i = 0; i < dec->index.num_frames; i++)
  727. {
  728. if (dec->index.frames[i].sample >= position)
  729. break;
  730. }
  731. #else
  732. i = mp3dec_idx_binary_search(&dec->index, position);
  733. #endif
  734. if (i)
  735. {
  736. int to_fill_bytes = 511;
  737. int skip_frames = MINIMP3_PREDECODE_FRAMES
  738. #ifdef MINIMP3_SEEK_IDX_LINEAR_SEARCH
  739. + ((dec->index.frames[i].sample == position) ? 0 : 1)
  740. #endif
  741. ;
  742. i -= MINIMP3_MIN(i, (size_t)skip_frames);
  743. if (3 == dec->info.layer)
  744. {
  745. while (i && to_fill_bytes)
  746. { /* make sure bit-reservoir is filled when we start decoding */
  747. bs_t bs[1];
  748. L3_gr_info_t gr_info[4];
  749. int frame_bytes, frame_size;
  750. const uint8_t *hdr;
  751. if (dec->io)
  752. {
  753. hdr = dec->file.buffer;
  754. if (dec->io->seek(dec->index.frames[i - 1].offset, dec->io->seek_data))
  755. return MP3D_E_IOERROR;
  756. size_t readed = dec->io->read((uint8_t *)hdr, HDR_SIZE, dec->io->read_data);
  757. if (readed != HDR_SIZE)
  758. return MP3D_E_IOERROR;
  759. frame_size = hdr_frame_bytes(hdr, dec->free_format_bytes) + hdr_padding(hdr);
  760. readed = dec->io->read((uint8_t *)hdr + HDR_SIZE, frame_size - HDR_SIZE, dec->io->read_data);
  761. if (readed != (size_t)(frame_size - HDR_SIZE))
  762. return MP3D_E_IOERROR;
  763. bs_init(bs, hdr + HDR_SIZE, frame_size - HDR_SIZE);
  764. } else
  765. {
  766. hdr = dec->file.buffer + dec->index.frames[i - 1].offset;
  767. frame_size = hdr_frame_bytes(hdr, dec->free_format_bytes) + hdr_padding(hdr);
  768. bs_init(bs, hdr + HDR_SIZE, frame_size - HDR_SIZE);
  769. }
  770. if (HDR_IS_CRC(hdr))
  771. get_bits(bs, 16);
  772. i--;
  773. if (L3_read_side_info(bs, gr_info, hdr) < 0)
  774. break; /* frame not decodable, we can start from here */
  775. frame_bytes = (bs->limit - bs->pos)/8;
  776. to_fill_bytes -= MINIMP3_MIN(to_fill_bytes, frame_bytes);
  777. }
  778. }
  779. }
  780. dec->offset = dec->index.frames[i].offset;
  781. dec->to_skip = position - dec->index.frames[i].sample;
  782. while ((i + 1) < dec->index.num_frames && !dec->index.frames[i].sample && !dec->index.frames[i + 1].sample)
  783. { /* skip not decodable first frames */
  784. const uint8_t *hdr;
  785. if (dec->io)
  786. {
  787. hdr = dec->file.buffer;
  788. if (dec->io->seek(dec->index.frames[i].offset, dec->io->seek_data))
  789. return MP3D_E_IOERROR;
  790. size_t readed = dec->io->read((uint8_t *)hdr, HDR_SIZE, dec->io->read_data);
  791. if (readed != HDR_SIZE)
  792. return MP3D_E_IOERROR;
  793. } else
  794. hdr = dec->file.buffer + dec->index.frames[i].offset;
  795. dec->to_skip += hdr_frame_samples(hdr)*dec->info.channels;
  796. i++;
  797. }
  798. do_exit:
  799. if (dec->io)
  800. {
  801. if (dec->io->seek(dec->offset, dec->io->seek_data))
  802. return MP3D_E_IOERROR;
  803. }
  804. dec->buffer_samples = 0;
  805. dec->buffer_consumed = 0;
  806. dec->input_consumed = 0;
  807. dec->input_filled = 0;
  808. dec->last_error = 0;
  809. mp3dec_init(&dec->mp3d);
  810. return 0;
  811. }
  812. size_t mp3dec_ex_read(mp3dec_ex_t *dec, mp3d_sample_t *buf, size_t samples)
  813. {
  814. if (!dec || !buf)
  815. {
  816. if (dec)
  817. dec->last_error = MP3D_E_PARAM;
  818. return 0;
  819. }
  820. uint64_t end_offset = dec->end_offset ? dec->end_offset : dec->file.size;
  821. size_t samples_requested = samples;
  822. int eof = 0;
  823. mp3dec_frame_info_t frame_info;
  824. memset(&frame_info, 0, sizeof(frame_info));
  825. if (dec->detected_samples && dec->cur_sample >= dec->detected_samples)
  826. return 0; /* at end of stream */
  827. if (dec->last_error)
  828. return 0; /* error eof state, seek can reset it */
  829. if (dec->buffer_consumed < dec->buffer_samples)
  830. {
  831. size_t to_copy = MINIMP3_MIN((size_t)(dec->buffer_samples - dec->buffer_consumed), samples);
  832. if (dec->detected_samples)
  833. { /* count decoded samples to properly cut padding */
  834. if (dec->cur_sample + to_copy >= dec->detected_samples)
  835. to_copy = dec->detected_samples - dec->cur_sample;
  836. }
  837. dec->cur_sample += to_copy;
  838. memcpy(buf, dec->buffer + dec->buffer_consumed, to_copy*sizeof(mp3d_sample_t));
  839. buf += to_copy;
  840. dec->buffer_consumed += to_copy;
  841. samples -= to_copy;
  842. }
  843. while (samples)
  844. {
  845. if (dec->detected_samples && dec->cur_sample >= dec->detected_samples)
  846. break;
  847. const uint8_t *dec_buf;
  848. if (dec->io)
  849. {
  850. if (!eof && (dec->input_filled - dec->input_consumed) < MINIMP3_BUF_SIZE)
  851. { /* keep minimum 10 consecutive mp3 frames (~16KB) worst case */
  852. memmove((uint8_t*)dec->file.buffer, (uint8_t*)dec->file.buffer + dec->input_consumed, dec->input_filled - dec->input_consumed);
  853. dec->input_filled -= dec->input_consumed;
  854. dec->input_consumed = 0;
  855. size_t readed = dec->io->read((uint8_t*)dec->file.buffer + dec->input_filled, dec->file.size - dec->input_filled, dec->io->read_data);
  856. if (readed > (dec->file.size - dec->input_filled))
  857. {
  858. dec->last_error = MP3D_E_IOERROR;
  859. readed = 0;
  860. }
  861. if (readed != (dec->file.size - dec->input_filled))
  862. eof = 1;
  863. dec->input_filled += readed;
  864. if (eof)
  865. mp3dec_skip_id3v1((uint8_t*)dec->file.buffer, &dec->input_filled);
  866. }
  867. dec_buf = dec->file.buffer + dec->input_consumed;
  868. if (!(dec->input_filled - dec->input_consumed))
  869. break;
  870. dec->buffer_samples = mp3dec_decode_frame(&dec->mp3d, dec_buf, dec->input_filled - dec->input_consumed, dec->buffer, &frame_info);
  871. dec->input_consumed += frame_info.frame_bytes;
  872. } else
  873. {
  874. dec_buf = dec->file.buffer + dec->offset;
  875. uint64_t buf_size = end_offset - dec->offset;
  876. if (!buf_size)
  877. break;
  878. dec->buffer_samples = mp3dec_decode_frame(&dec->mp3d, dec_buf, MINIMP3_MIN(buf_size, (uint64_t)INT_MAX), dec->buffer, &frame_info);
  879. }
  880. dec->buffer_consumed = 0;
  881. if (dec->info.hz != frame_info.hz || dec->info.layer != frame_info.layer
  882. #ifndef MINIMP3_ALLOW_MONO_STEREO_TRANSITION
  883. || dec->info.channels != frame_info.channels
  884. #endif
  885. )
  886. {
  887. dec->last_error = MP3D_E_DECODE;
  888. break;
  889. }
  890. if (dec->buffer_samples)
  891. {
  892. dec->buffer_samples *= frame_info.channels;
  893. if (dec->to_skip)
  894. {
  895. size_t skip = MINIMP3_MIN(dec->buffer_samples, dec->to_skip);
  896. dec->buffer_consumed += skip;
  897. dec->to_skip -= skip;
  898. }
  899. size_t to_copy = MINIMP3_MIN((size_t)(dec->buffer_samples - dec->buffer_consumed), samples);
  900. if (dec->detected_samples)
  901. { /* ^ handle padding */
  902. if (dec->cur_sample + to_copy >= dec->detected_samples)
  903. to_copy = dec->detected_samples - dec->cur_sample;
  904. }
  905. dec->cur_sample += to_copy;
  906. memcpy(buf, dec->buffer + dec->buffer_consumed, to_copy*sizeof(mp3d_sample_t));
  907. buf += to_copy;
  908. dec->buffer_consumed += to_copy;
  909. samples -= to_copy;
  910. } else if (dec->to_skip)
  911. { /* In mp3 decoding not always can start decode from any frame because of bit reservoir,
  912. count skip samples for such frames */
  913. int frame_samples = hdr_frame_samples(dec_buf)*frame_info.channels;
  914. dec->to_skip -= MINIMP3_MIN(frame_samples, dec->to_skip);
  915. }
  916. dec->offset += frame_info.frame_bytes;
  917. }
  918. dec->info.bitrate_kbps = frame_info.bitrate_kbps;
  919. return samples_requested - samples;
  920. }
  921. #ifndef MINIMP3_NO_STDIO
  922. #if defined(__linux__) || defined(__FreeBSD__)
  923. #include <errno.h>
  924. #include <sys/mman.h>
  925. #include <sys/types.h>
  926. #include <sys/stat.h>
  927. #include <unistd.h>
  928. #include <fcntl.h>
  929. #if !defined(_GNU_SOURCE)
  930. #include <sys/ipc.h>
  931. #include <sys/shm.h>
  932. #endif
  933. #if !defined(MAP_POPULATE) && defined(__linux__)
  934. #define MAP_POPULATE 0x08000
  935. #elif !defined(MAP_POPULATE)
  936. #define MAP_POPULATE 0
  937. #endif
  938. static void mp3dec_close_file(mp3dec_map_info_t *map_info)
  939. {
  940. if (map_info->buffer && MAP_FAILED != map_info->buffer)
  941. munmap((void *)map_info->buffer, map_info->size);
  942. map_info->buffer = 0;
  943. map_info->size = 0;
  944. }
  945. static int mp3dec_open_file(const char *file_name, mp3dec_map_info_t *map_info)
  946. {
  947. if (!file_name)
  948. return MP3D_E_PARAM;
  949. int file;
  950. struct stat st;
  951. memset(map_info, 0, sizeof(*map_info));
  952. retry_open:
  953. file = open(file_name, O_RDONLY);
  954. if (file < 0 && (errno == EAGAIN || errno == EINTR))
  955. goto retry_open;
  956. if (file < 0 || fstat(file, &st) < 0)
  957. {
  958. close(file);
  959. return MP3D_E_IOERROR;
  960. }
  961. map_info->size = st.st_size;
  962. retry_mmap:
  963. map_info->buffer = (const uint8_t *)mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE | MAP_POPULATE, file, 0);
  964. if (MAP_FAILED == map_info->buffer && (errno == EAGAIN || errno == EINTR))
  965. goto retry_mmap;
  966. close(file);
  967. if (MAP_FAILED == map_info->buffer)
  968. return MP3D_E_IOERROR;
  969. return 0;
  970. }
  971. #if MINIMP3_ENABLE_RING && defined(__linux__) && defined(_GNU_SOURCE)
  972. #define MINIMP3_HAVE_RING
  973. static void mp3dec_close_ring(mp3dec_map_info_t *map_info)
  974. {
  975. #if defined(__linux__) && defined(_GNU_SOURCE)
  976. if (map_info->buffer && MAP_FAILED != map_info->buffer)
  977. munmap((void *)map_info->buffer, map_info->size*2);
  978. #else
  979. if (map_info->buffer)
  980. {
  981. shmdt(map_info->buffer);
  982. shmdt(map_info->buffer + map_info->size);
  983. }
  984. #endif
  985. map_info->buffer = 0;
  986. map_info->size = 0;
  987. }
  988. static int mp3dec_open_ring(mp3dec_map_info_t *map_info, size_t size)
  989. {
  990. int memfd, page_size;
  991. #if defined(__linux__) && defined(_GNU_SOURCE)
  992. void *buffer;
  993. int res;
  994. #endif
  995. memset(map_info, 0, sizeof(*map_info));
  996. #ifdef _SC_PAGESIZE
  997. page_size = sysconf(_SC_PAGESIZE);
  998. #else
  999. page_size = getpagesize();
  1000. #endif
  1001. map_info->size = (size + page_size - 1)/page_size*page_size;
  1002. #if defined(__linux__) && defined(_GNU_SOURCE)
  1003. memfd = memfd_create("mp3_ring", 0);
  1004. if (memfd < 0)
  1005. return MP3D_E_MEMORY;
  1006. retry_ftruncate:
  1007. res = ftruncate(memfd, map_info->size);
  1008. if (res && (errno == EAGAIN || errno == EINTR))
  1009. goto retry_ftruncate;
  1010. if (res)
  1011. goto error;
  1012. retry_mmap:
  1013. map_info->buffer = (const uint8_t *)mmap(NULL, map_info->size*2, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  1014. if (MAP_FAILED == map_info->buffer && (errno == EAGAIN || errno == EINTR))
  1015. goto retry_mmap;
  1016. if (MAP_FAILED == map_info->buffer || !map_info->buffer)
  1017. goto error;
  1018. retry_mmap2:
  1019. buffer = mmap((void *)map_info->buffer, map_info->size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, memfd, 0);
  1020. if (MAP_FAILED == map_info->buffer && (errno == EAGAIN || errno == EINTR))
  1021. goto retry_mmap2;
  1022. if (MAP_FAILED == map_info->buffer || buffer != (void *)map_info->buffer)
  1023. goto error;
  1024. retry_mmap3:
  1025. buffer = mmap((void *)map_info->buffer + map_info->size, map_info->size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, memfd, 0);
  1026. if (MAP_FAILED == map_info->buffer && (errno == EAGAIN || errno == EINTR))
  1027. goto retry_mmap3;
  1028. if (MAP_FAILED == map_info->buffer || buffer != (void *)(map_info->buffer + map_info->size))
  1029. goto error;
  1030. close(memfd);
  1031. return 0;
  1032. error:
  1033. close(memfd);
  1034. mp3dec_close_ring(map_info);
  1035. return MP3D_E_MEMORY;
  1036. #else
  1037. memfd = shmget(IPC_PRIVATE, map_info->size, IPC_CREAT | 0700);
  1038. if (memfd < 0)
  1039. return MP3D_E_MEMORY;
  1040. retry_mmap:
  1041. map_info->buffer = (const uint8_t *)mmap(NULL, map_info->size*2, PROT_NONE, MAP_PRIVATE, -1, 0);
  1042. if (MAP_FAILED == map_info->buffer && (errno == EAGAIN || errno == EINTR))
  1043. goto retry_mmap;
  1044. if (MAP_FAILED == map_info->buffer)
  1045. goto error;
  1046. if (map_info->buffer != shmat(memfd, map_info->buffer, 0))
  1047. goto error;
  1048. if ((map_info->buffer + map_info->size) != shmat(memfd, map_info->buffer + map_info->size, 0))
  1049. goto error;
  1050. if (shmctl(memfd, IPC_RMID, NULL) < 0)
  1051. return MP3D_E_MEMORY;
  1052. return 0;
  1053. error:
  1054. shmctl(memfd, IPC_RMID, NULL);
  1055. mp3dec_close_ring(map_info);
  1056. return MP3D_E_MEMORY;
  1057. #endif
  1058. }
  1059. #endif /*MINIMP3_ENABLE_RING*/
  1060. #elif defined(_WIN32)
  1061. #include <windows.h>
  1062. static void mp3dec_close_file(mp3dec_map_info_t *map_info)
  1063. {
  1064. if (map_info->buffer)
  1065. UnmapViewOfFile(map_info->buffer);
  1066. map_info->buffer = 0;
  1067. map_info->size = 0;
  1068. }
  1069. static int mp3dec_open_file_h(HANDLE file, mp3dec_map_info_t *map_info)
  1070. {
  1071. memset(map_info, 0, sizeof(*map_info));
  1072. HANDLE mapping = NULL;
  1073. LARGE_INTEGER s;
  1074. s.LowPart = GetFileSize(file, (DWORD*)&s.HighPart);
  1075. if (s.LowPart == INVALID_FILE_SIZE && GetLastError() != NO_ERROR)
  1076. goto error;
  1077. map_info->size = s.QuadPart;
  1078. mapping = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
  1079. if (!mapping)
  1080. goto error;
  1081. map_info->buffer = (const uint8_t*)MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, s.QuadPart);
  1082. CloseHandle(mapping);
  1083. if (!map_info->buffer)
  1084. goto error;
  1085. CloseHandle(file);
  1086. return 0;
  1087. error:
  1088. mp3dec_close_file(map_info);
  1089. CloseHandle(file);
  1090. return MP3D_E_IOERROR;
  1091. }
  1092. static int mp3dec_open_file(const char *file_name, mp3dec_map_info_t *map_info)
  1093. {
  1094. if (!file_name)
  1095. return MP3D_E_PARAM;
  1096. HANDLE file = CreateFileA(file_name, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
  1097. if (INVALID_HANDLE_VALUE == file)
  1098. return MP3D_E_IOERROR;
  1099. return mp3dec_open_file_h(file, map_info);
  1100. }
  1101. static int mp3dec_open_file_w(const wchar_t *file_name, mp3dec_map_info_t *map_info)
  1102. {
  1103. if (!file_name)
  1104. return MP3D_E_PARAM;
  1105. HANDLE file = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
  1106. if (INVALID_HANDLE_VALUE == file)
  1107. return MP3D_E_IOERROR;
  1108. return mp3dec_open_file_h(file, map_info);
  1109. }
  1110. #else
  1111. #include <stdio.h>
  1112. static void mp3dec_close_file(mp3dec_map_info_t *map_info)
  1113. {
  1114. if (map_info->buffer)
  1115. free((void *)map_info->buffer);
  1116. map_info->buffer = 0;
  1117. map_info->size = 0;
  1118. }
  1119. static int mp3dec_open_file(const char *file_name, mp3dec_map_info_t *map_info)
  1120. {
  1121. if (!file_name)
  1122. return MP3D_E_PARAM;
  1123. memset(map_info, 0, sizeof(*map_info));
  1124. FILE *file = fopen(file_name, "rb");
  1125. if (!file)
  1126. return MP3D_E_IOERROR;
  1127. int res = MP3D_E_IOERROR;
  1128. long size = -1;
  1129. if (fseek(file, 0, SEEK_END))
  1130. goto error;
  1131. size = ftell(file);
  1132. if (size < 0)
  1133. goto error;
  1134. map_info->size = (size_t)size;
  1135. if (fseek(file, 0, SEEK_SET))
  1136. goto error;
  1137. map_info->buffer = (uint8_t *)malloc(map_info->size);
  1138. if (!map_info->buffer)
  1139. {
  1140. res = MP3D_E_MEMORY;
  1141. goto error;
  1142. }
  1143. if (fread((void *)map_info->buffer, 1, map_info->size, file) != map_info->size)
  1144. goto error;
  1145. fclose(file);
  1146. return 0;
  1147. error:
  1148. mp3dec_close_file(map_info);
  1149. fclose(file);
  1150. return res;
  1151. }
  1152. #endif
  1153. static int mp3dec_detect_mapinfo(mp3dec_map_info_t *map_info)
  1154. {
  1155. int ret = mp3dec_detect_buf(map_info->buffer, map_info->size);
  1156. mp3dec_close_file(map_info);
  1157. return ret;
  1158. }
  1159. static int mp3dec_load_mapinfo(mp3dec_t *dec, mp3dec_map_info_t *map_info, mp3dec_file_info_t *info, MP3D_PROGRESS_CB progress_cb, void *user_data)
  1160. {
  1161. int ret = mp3dec_load_buf(dec, map_info->buffer, map_info->size, info, progress_cb, user_data);
  1162. mp3dec_close_file(map_info);
  1163. return ret;
  1164. }
  1165. static int mp3dec_iterate_mapinfo(mp3dec_map_info_t *map_info, MP3D_ITERATE_CB callback, void *user_data)
  1166. {
  1167. int ret = mp3dec_iterate_buf(map_info->buffer, map_info->size, callback, user_data);
  1168. mp3dec_close_file(map_info);
  1169. return ret;
  1170. }
  1171. static int mp3dec_ex_open_mapinfo(mp3dec_ex_t *dec, int flags)
  1172. {
  1173. int ret = mp3dec_ex_open_buf(dec, dec->file.buffer, dec->file.size, flags);
  1174. dec->is_file = 1;
  1175. if (ret)
  1176. mp3dec_ex_close(dec);
  1177. return ret;
  1178. }
  1179. int mp3dec_detect(const char *file_name)
  1180. {
  1181. int ret;
  1182. mp3dec_map_info_t map_info;
  1183. if ((ret = mp3dec_open_file(file_name, &map_info)))
  1184. return ret;
  1185. return mp3dec_detect_mapinfo(&map_info);
  1186. }
  1187. int mp3dec_load(mp3dec_t *dec, const char *file_name, mp3dec_file_info_t *info, MP3D_PROGRESS_CB progress_cb, void *user_data)
  1188. {
  1189. int ret;
  1190. mp3dec_map_info_t map_info;
  1191. if ((ret = mp3dec_open_file(file_name, &map_info)))
  1192. return ret;
  1193. return mp3dec_load_mapinfo(dec, &map_info, info, progress_cb, user_data);
  1194. }
  1195. int mp3dec_iterate(const char *file_name, MP3D_ITERATE_CB callback, void *user_data)
  1196. {
  1197. int ret;
  1198. mp3dec_map_info_t map_info;
  1199. if ((ret = mp3dec_open_file(file_name, &map_info)))
  1200. return ret;
  1201. return mp3dec_iterate_mapinfo(&map_info, callback, user_data);
  1202. }
  1203. int mp3dec_ex_open(mp3dec_ex_t *dec, const char *file_name, int flags)
  1204. {
  1205. int ret;
  1206. if (!dec)
  1207. return MP3D_E_PARAM;
  1208. if ((ret = mp3dec_open_file(file_name, &dec->file)))
  1209. return ret;
  1210. return mp3dec_ex_open_mapinfo(dec, flags);
  1211. }
  1212. int mp3dec_ex_open_cb(mp3dec_ex_t *dec, mp3dec_io_t *io, int flags)
  1213. {
  1214. if (!dec || !io || (flags & (~3)))
  1215. return MP3D_E_PARAM;
  1216. memset(dec, 0, sizeof(*dec));
  1217. #ifdef MINIMP3_HAVE_RING
  1218. int ret;
  1219. if (ret = mp3dec_open_ring(&dec->file, MINIMP3_IO_SIZE))
  1220. return ret;
  1221. #else
  1222. dec->file.size = MINIMP3_IO_SIZE;
  1223. dec->file.buffer = (const uint8_t*)malloc(dec->file.size);
  1224. if (!dec->file.buffer)
  1225. return MP3D_E_MEMORY;
  1226. #endif
  1227. dec->flags = flags;
  1228. dec->io = io;
  1229. mp3dec_init(&dec->mp3d);
  1230. if (io->seek(0, io->seek_data))
  1231. return MP3D_E_IOERROR;
  1232. int ret = mp3dec_iterate_cb(io, (uint8_t *)dec->file.buffer, dec->file.size, mp3dec_load_index, dec);
  1233. if (ret && MP3D_E_USER != ret)
  1234. return ret;
  1235. if (dec->io->seek(dec->start_offset, dec->io->seek_data))
  1236. return MP3D_E_IOERROR;
  1237. mp3dec_init(&dec->mp3d);
  1238. dec->buffer_samples = 0;
  1239. dec->indexes_built = !(dec->vbr_tag_found || (flags & MP3D_DO_NOT_SCAN));
  1240. dec->flags &= (~MP3D_DO_NOT_SCAN);
  1241. return 0;
  1242. }
  1243. void mp3dec_ex_close(mp3dec_ex_t *dec)
  1244. {
  1245. #ifdef MINIMP3_HAVE_RING
  1246. if (dec->io)
  1247. mp3dec_close_ring(&dec->file);
  1248. #else
  1249. if (dec->io && dec->file.buffer)
  1250. free((void*)dec->file.buffer);
  1251. #endif
  1252. if (dec->is_file)
  1253. mp3dec_close_file(&dec->file);
  1254. if (dec->index.frames)
  1255. free(dec->index.frames);
  1256. memset(dec, 0, sizeof(*dec));
  1257. }
  1258. #ifdef _WIN32
  1259. int mp3dec_detect_w(const wchar_t *file_name)
  1260. {
  1261. int ret;
  1262. mp3dec_map_info_t map_info;
  1263. if ((ret = mp3dec_open_file_w(file_name, &map_info)))
  1264. return ret;
  1265. return mp3dec_detect_mapinfo(&map_info);
  1266. }
  1267. int mp3dec_load_w(mp3dec_t *dec, const wchar_t *file_name, mp3dec_file_info_t *info, MP3D_PROGRESS_CB progress_cb, void *user_data)
  1268. {
  1269. int ret;
  1270. mp3dec_map_info_t map_info;
  1271. if ((ret = mp3dec_open_file_w(file_name, &map_info)))
  1272. return ret;
  1273. return mp3dec_load_mapinfo(dec, &map_info, info, progress_cb, user_data);
  1274. }
  1275. int mp3dec_iterate_w(const wchar_t *file_name, MP3D_ITERATE_CB callback, void *user_data)
  1276. {
  1277. int ret;
  1278. mp3dec_map_info_t map_info;
  1279. if ((ret = mp3dec_open_file_w(file_name, &map_info)))
  1280. return ret;
  1281. return mp3dec_iterate_mapinfo(&map_info, callback, user_data);
  1282. }
  1283. int mp3dec_ex_open_w(mp3dec_ex_t *dec, const wchar_t *file_name, int flags)
  1284. {
  1285. int ret;
  1286. if ((ret = mp3dec_open_file_w(file_name, &dec->file)))
  1287. return ret;
  1288. return mp3dec_ex_open_mapinfo(dec, flags);
  1289. }
  1290. #endif
  1291. #else /* MINIMP3_NO_STDIO */
  1292. void mp3dec_ex_close(mp3dec_ex_t *dec)
  1293. {
  1294. if (dec->index.frames)
  1295. free(dec->index.frames);
  1296. memset(dec, 0, sizeof(*dec));
  1297. }
  1298. #endif
  1299. #endif /*MINIMP3_IMPLEMENTATION*/