minimp3_ex.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. #define MP3D_SEEK_TO_BYTE 0
  11. #define MP3D_SEEK_TO_SAMPLE 1
  12. #define MP3D_SEEK_TO_SAMPLE_INDEXED 2
  13. typedef struct
  14. {
  15. mp3d_sample_t *buffer;
  16. size_t samples; /* channels included, byte size = samples*sizeof(int16_t) */
  17. int channels, hz, layer, avg_bitrate_kbps;
  18. } mp3dec_file_info_t;
  19. typedef struct
  20. {
  21. const uint8_t *buffer;
  22. size_t size;
  23. } mp3dec_map_info_t;
  24. typedef struct
  25. {
  26. mp3dec_t mp3d;
  27. mp3dec_map_info_t file;
  28. int seek_method;
  29. #ifndef MINIMP3_NO_STDIO
  30. int is_file;
  31. #endif
  32. } mp3dec_ex_t;
  33. typedef int (*MP3D_ITERATE_CB)(void *user_data, const uint8_t *frame, int frame_size, size_t offset, mp3dec_frame_info_t *info);
  34. typedef int (*MP3D_PROGRESS_CB)(void *user_data, size_t file_size, size_t offset, mp3dec_frame_info_t *info);
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /* decode whole buffer block */
  39. void 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);
  40. /* iterate through frames with optional decoding */
  41. void mp3dec_iterate_buf(const uint8_t *buf, size_t buf_size, MP3D_ITERATE_CB callback, void *user_data);
  42. /* decoder with seeking capability */
  43. int mp3dec_ex_open_buf(mp3dec_ex_t *dec, const uint8_t *buf, size_t buf_size, int seek_method);
  44. void mp3dec_ex_close(mp3dec_ex_t *dec);
  45. void mp3dec_ex_seek(mp3dec_ex_t *dec, size_t position);
  46. int mp3dec_ex_read(mp3dec_ex_t *dec, int16_t *buf, int samples);
  47. #ifndef MINIMP3_NO_STDIO
  48. /* stdio versions with file pre-load */
  49. int mp3dec_load(mp3dec_t *dec, const char *file_name, mp3dec_file_info_t *info, MP3D_PROGRESS_CB progress_cb, void *user_data);
  50. int mp3dec_iterate(const char *file_name, MP3D_ITERATE_CB callback, void *user_data);
  51. int mp3dec_ex_open(mp3dec_ex_t *dec, const char *file_name, int seek_method);
  52. #endif
  53. #ifdef __cplusplus
  54. }
  55. #endif
  56. #endif /*MINIMP3_EXT_H*/
  57. #ifdef MINIMP3_IMPLEMENTATION
  58. static size_t mp3dec_skip_id3v2(const uint8_t *buf, size_t buf_size)
  59. {
  60. if (buf_size > 10 && !strncmp((char *)buf, "ID3", 3))
  61. {
  62. return (((buf[6] & 0x7f) << 21) | ((buf[7] & 0x7f) << 14) |
  63. ((buf[8] & 0x7f) << 7) | (buf[9] & 0x7f)) + 10;
  64. }
  65. return 0;
  66. }
  67. void 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)
  68. {
  69. size_t orig_buf_size = buf_size;
  70. mp3d_sample_t pcm[MINIMP3_MAX_SAMPLES_PER_FRAME];
  71. mp3dec_frame_info_t frame_info;
  72. memset(info, 0, sizeof(*info));
  73. memset(&frame_info, 0, sizeof(frame_info));
  74. /* skip id3v2 */
  75. size_t id3v2size = mp3dec_skip_id3v2(buf, buf_size);
  76. if (id3v2size > buf_size)
  77. return;
  78. buf += id3v2size;
  79. buf_size -= id3v2size;
  80. /* try to make allocation size assumption by first frame */
  81. mp3dec_init(dec);
  82. int samples;
  83. do
  84. {
  85. samples = mp3dec_decode_frame(dec, buf, buf_size, pcm, &frame_info);
  86. buf += frame_info.frame_bytes;
  87. buf_size -= frame_info.frame_bytes;
  88. if (samples)
  89. break;
  90. } while (frame_info.frame_bytes);
  91. if (!samples)
  92. return;
  93. samples *= frame_info.channels;
  94. size_t allocated = (buf_size/frame_info.frame_bytes)*samples*sizeof(mp3d_sample_t) + MINIMP3_MAX_SAMPLES_PER_FRAME*sizeof(mp3d_sample_t);
  95. info->buffer = (mp3d_sample_t*)malloc(allocated);
  96. if (!info->buffer)
  97. return;
  98. info->samples = samples;
  99. memcpy(info->buffer, pcm, info->samples*sizeof(mp3d_sample_t));
  100. /* save info */
  101. info->channels = frame_info.channels;
  102. info->hz = frame_info.hz;
  103. info->layer = frame_info.layer;
  104. size_t avg_bitrate_kbps = frame_info.bitrate_kbps;
  105. size_t frames = 1;
  106. /* decode rest frames */
  107. int frame_bytes;
  108. do
  109. {
  110. if ((allocated - info->samples*sizeof(mp3d_sample_t)) < MINIMP3_MAX_SAMPLES_PER_FRAME*sizeof(mp3d_sample_t))
  111. {
  112. allocated *= 2;
  113. info->buffer = (mp3d_sample_t*)realloc(info->buffer, allocated);
  114. }
  115. samples = mp3dec_decode_frame(dec, buf, buf_size, info->buffer + info->samples, &frame_info);
  116. frame_bytes = frame_info.frame_bytes;
  117. buf += frame_bytes;
  118. buf_size -= frame_bytes;
  119. if (samples)
  120. {
  121. if (info->hz != frame_info.hz || info->layer != frame_info.layer)
  122. break;
  123. if (info->channels && info->channels != frame_info.channels)
  124. #ifdef MINIMP3_ALLOW_MONO_STEREO_TRANSITION
  125. info->channels = 0; /* mark file with mono-stereo transition */
  126. #else
  127. break;
  128. #endif
  129. info->samples += samples*frame_info.channels;
  130. avg_bitrate_kbps += frame_info.bitrate_kbps;
  131. frames++;
  132. if (progress_cb)
  133. progress_cb(user_data, orig_buf_size, orig_buf_size - buf_size, &frame_info);
  134. }
  135. } while (frame_bytes);
  136. /* reallocate to normal buffer size */
  137. if (allocated != info->samples*sizeof(mp3d_sample_t))
  138. info->buffer = (mp3d_sample_t*)realloc(info->buffer, info->samples*sizeof(mp3d_sample_t));
  139. info->avg_bitrate_kbps = avg_bitrate_kbps/frames;
  140. }
  141. void mp3dec_iterate_buf(const uint8_t *buf, size_t buf_size, MP3D_ITERATE_CB callback, void *user_data)
  142. {
  143. if (!callback)
  144. return;
  145. mp3dec_frame_info_t frame_info;
  146. memset(&frame_info, 0, sizeof(frame_info));
  147. /* skip id3v2 */
  148. size_t id3v2size = mp3dec_skip_id3v2(buf, buf_size);
  149. if (id3v2size > buf_size)
  150. return;
  151. const uint8_t *orig_buf = buf;
  152. buf += id3v2size;
  153. buf_size -= id3v2size;
  154. do
  155. {
  156. int free_format_bytes = 0, frame_size = 0;
  157. int i = mp3d_find_frame(buf, buf_size, &free_format_bytes, &frame_size);
  158. buf += i;
  159. buf_size -= i;
  160. if (i && !frame_size)
  161. continue;
  162. if (!frame_size)
  163. break;
  164. const uint8_t *hdr = buf;
  165. frame_info.channels = HDR_IS_MONO(hdr) ? 1 : 2;
  166. frame_info.hz = hdr_sample_rate_hz(hdr);
  167. frame_info.layer = 4 - HDR_GET_LAYER(hdr);
  168. frame_info.bitrate_kbps = hdr_bitrate_kbps(hdr);
  169. frame_info.frame_bytes = frame_size;
  170. if (callback(user_data, hdr, frame_size, hdr - orig_buf, &frame_info))
  171. break;
  172. buf += frame_size;
  173. buf_size -= frame_size;
  174. } while (1);
  175. }
  176. int mp3dec_ex_open_buf(mp3dec_ex_t *dec, const uint8_t *buf, size_t buf_size, int seek_method)
  177. {
  178. memset(dec, 0, sizeof(*dec));
  179. dec->file.buffer = buf;
  180. dec->file.size = buf_size;
  181. dec->seek_method = seek_method;
  182. mp3dec_init(&dec->mp3d);
  183. return 0;
  184. }
  185. /*void mp3dec_ex_seek(mp3dec_ex_t *dec, size_t position)
  186. {
  187. }
  188. int mp3dec_ex_read(mp3dec_ex_t *dec, int16_t *buf, int samples)
  189. {
  190. return 0;
  191. }*/
  192. #ifndef MINIMP3_NO_STDIO
  193. #if defined(__linux__) || defined(__FreeBSD__)
  194. #include <errno.h>
  195. #include <sys/mman.h>
  196. #include <sys/types.h>
  197. #include <sys/stat.h>
  198. #include <unistd.h>
  199. #include <fcntl.h>
  200. #if !defined(MAP_POPULATE) && defined(__linux__)
  201. #define MAP_POPULATE 0x08000
  202. #elif !defined(MAP_POPULATE)
  203. #define MAP_POPULATE 0
  204. #endif
  205. static void mp3dec_close_file(mp3dec_map_info_t *map_info)
  206. {
  207. if (map_info->buffer && MAP_FAILED != map_info->buffer)
  208. munmap((void *)map_info->buffer, map_info->size);
  209. map_info->buffer = 0;
  210. map_info->size = 0;
  211. }
  212. static int mp3dec_open_file(const char *file_name, mp3dec_map_info_t *map_info)
  213. {
  214. int file;
  215. struct stat st;
  216. memset(map_info, 0, sizeof(*map_info));
  217. retry_open:
  218. file = open(file_name, O_RDONLY);
  219. if (file < 0 && (errno == EAGAIN || errno == EINTR))
  220. goto retry_open;
  221. if (file < 0 || fstat(file, &st) < 0)
  222. {
  223. close(file);
  224. return -1;
  225. }
  226. map_info->size = st.st_size;
  227. retry_mmap:
  228. map_info->buffer = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE | MAP_POPULATE, file, 0);
  229. if (MAP_FAILED == map_info->buffer && (errno == EAGAIN || errno == EINTR))
  230. goto retry_mmap;
  231. close(file);
  232. if (MAP_FAILED == map_info->buffer)
  233. return -1;
  234. return 0;
  235. }
  236. #elif defined(_WIN32)
  237. #include <windows.h>
  238. static void mp3dec_close_file(mp3dec_map_info_t *map_info)
  239. {
  240. if (map_info->buffer)
  241. UnmapViewOfFile(map_info->buffer);
  242. map_info->buffer = 0;
  243. map_info->size = 0;
  244. }
  245. static int mp3dec_open_file(const char *file_name, mp3dec_map_info_t *map_info)
  246. {
  247. memset(map_info, 0, sizeof(*map_info));
  248. HANDLE file = CreateFileA(file_name, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
  249. if (INVALID_HANDLE_VALUE == file)
  250. return -1;
  251. LARGE_INTEGER s;
  252. s.LowPart = GetFileSize(file, (DWORD*)&s.HighPart);
  253. if (s.LowPart == INVALID_FILE_SIZE && GetLastError() != NO_ERROR)
  254. goto error;
  255. map_info->size = s.QuadPart;
  256. HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
  257. if (!mapping)
  258. goto error;
  259. map_info->buffer = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, s.QuadPart);
  260. CloseHandle(mapping);
  261. if (!map_info->buffer)
  262. goto error;
  263. CloseHandle(file);
  264. return 0;
  265. error:
  266. mp3dec_close_file(map_info);
  267. CloseHandle(file);
  268. return -1;
  269. }
  270. #else
  271. #include <stdio.h>
  272. static void mp3dec_close_file(mp3dec_map_info_t *map_info)
  273. {
  274. if (map_info->buffer)
  275. free((void *)map_info->buffer);
  276. map_info->buffer = 0;
  277. map_info->size = 0;
  278. }
  279. static int mp3dec_open_file(const char *file_name, mp3dec_map_info_t *map_info)
  280. {
  281. memset(map_info, 0, sizeof(*map_info));
  282. FILE *file = fopen(file_name, "rb");
  283. if (!file)
  284. return -1;
  285. if (fseek(file, 0, SEEK_END))
  286. goto error;
  287. long size = ftell(file);
  288. if (size < 0)
  289. goto error;
  290. map_info->size = (size_t)size;
  291. if (fseek(file, 0, SEEK_SET))
  292. goto error;
  293. map_info->buffer = (uint8_t *)malloc(map_info->size);
  294. if (!map_info->buffer)
  295. goto error;
  296. if (fread((void *)map_info->buffer, 1, map_info->size, file) != map_info->size)
  297. goto error;
  298. fclose(file);
  299. return 0;
  300. error:
  301. mp3dec_close_file(map_info);
  302. fclose(file);
  303. return -1;
  304. }
  305. #endif
  306. int mp3dec_load(mp3dec_t *dec, const char *file_name, mp3dec_file_info_t *info, MP3D_PROGRESS_CB progress_cb, void *user_data)
  307. {
  308. int ret;
  309. mp3dec_map_info_t map_info;
  310. if ((ret = mp3dec_open_file(file_name, &map_info)))
  311. return ret;
  312. mp3dec_load_buf(dec, map_info.buffer, map_info.size, info, progress_cb, user_data);
  313. mp3dec_close_file(&map_info);
  314. return 0;
  315. }
  316. int mp3dec_iterate(const char *file_name, MP3D_ITERATE_CB callback, void *user_data)
  317. {
  318. int ret;
  319. mp3dec_map_info_t map_info;
  320. if ((ret = mp3dec_open_file(file_name, &map_info)))
  321. return ret;
  322. mp3dec_iterate_buf(map_info.buffer, map_info.size, callback, user_data);
  323. mp3dec_close_file(&map_info);
  324. return 0;
  325. }
  326. void mp3dec_ex_close(mp3dec_ex_t *dec)
  327. {
  328. if (dec->is_file)
  329. mp3dec_close_file(&dec->file);
  330. else
  331. free((void *)dec->file.buffer);
  332. memset(dec, 0, sizeof(*dec));
  333. }
  334. int mp3dec_ex_open(mp3dec_ex_t *dec, const char *file_name, int seek_method)
  335. {
  336. int ret;
  337. memset(dec, 0, sizeof(*dec));
  338. if ((ret = mp3dec_open_file(file_name, &dec->file)))
  339. return ret;
  340. dec->seek_method = seek_method;
  341. dec->is_file = 1;
  342. mp3dec_init(&dec->mp3d);
  343. return 0;
  344. }
  345. #else
  346. void mp3dec_ex_close(mp3dec_ex_t *dec)
  347. {
  348. free((void*)dec->file.buffer);
  349. memset(dec, 0, sizeof(*dec));
  350. }
  351. #endif
  352. #endif /*MINIMP3_IMPLEMENTATION*/