12#define MP3D_SEEK_TO_BYTE 0
13#define MP3D_SEEK_TO_SAMPLE 1
14#define MP3D_DO_NOT_SCAN 2
17#define MINIMP3_PREDECODE_FRAMES 2
19#define MINIMP3_IO_SIZE (128*1024)
20#define MINIMP3_BUF_SIZE (16*1024)
22#define MINIMP3_ENABLE_RING 0
25#define MP3D_E_PARAM -1
26#define MP3D_E_MEMORY -2
27#define MP3D_E_IOERROR -3
29#define MP3D_E_DECODE -5
105#ifndef MINIMP3_NO_STDIO
112int mp3dec_detect_w(
const wchar_t *file_name);
115int mp3dec_ex_open_w(
mp3dec_ex_t *dec,
const wchar_t *file_name,
int flags);
124#ifdef MINIMP3_IMPLEMENTATION
130#ifndef MINIMP3_NOSKIP_ID3V1
138#ifndef MINIMP3_NOSKIP_APEV2
143 uint32_t tag_size = (
uint32_t)(tag[3] << 24) | (tag[2] << 16) | (tag[1] << 8) | tag[0];
153#define MINIMP3_ID3_DETECT_SIZE 10
154#ifndef MINIMP3_NOSKIP_ID3V2
155 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)))
157 size_t id3v2size = (((buf[6] & 0x7f) << 21) | ((buf[7] & 0x7f) << 14) | ((buf[8] & 0x7f) << 7) | (buf[9] & 0x7f)) + 10;
185 static const char g_xing_tag[4] = {
'X',
'i',
'n',
'g' };
186 static const char g_info_tag[4] = {
'I',
'n',
'f',
'o' };
190#define VBR_SCALE_FLAG 8
196 L3_gr_info_t gr_info[4];
197 bs_init(bs, frame + HDR_SIZE, frame_size - HDR_SIZE);
198 if (HDR_IS_CRC(frame))
200 if (L3_read_side_info(bs, gr_info, frame) < 0)
203 const uint8_t *tag = frame + HDR_SIZE + bs->pos/8;
204 if (memcmp(g_xing_tag, tag, 4) && memcmp(g_info_tag, tag, 4))
210 *frames = (
uint32_t)(tag[0] << 24) | (tag[1] << 16) | (tag[2] << 8) | tag[3];
218 *delay = *padding = 0;
222 if (tag - frame + 14 >= frame_size)
224 *delay = ((tag[0] << 4) | (tag[1] >> 4)) + (528 + 1);
225 *padding = (((tag[1] & 0xF) << 8) | tag[2]) - (528 + 1);
266 int free_format_bytes, frame_size;
267 mp3d_find_frame(buf, filled, &free_format_bytes, &frame_size);
282 uint64_t detected_samples = 0;
287 memset(&frame_info, 0,
sizeof(frame_info));
290 size_t filled = 0, consumed = 0;
291 int eof = 0, ret = 0;
330 int i, delay, padding, free_format_bytes = 0, frame_size = 0;
336 memmove(buf, buf + consumed, filled - consumed);
348 i = mp3d_find_frame(buf + consumed, filled - consumed, &free_format_bytes, &frame_size);
350 hdr = buf + consumed;
353 i = mp3d_find_frame(buf,
buf_size, &free_format_bytes, &frame_size);
358 if (
i && !frame_size)
362 frame_info.
channels = HDR_IS_MONO(hdr) ? 1 : 2;
363 frame_info.
hz = hdr_sample_rate_hz(hdr);
364 frame_info.
layer = 4 - HDR_GET_LAYER(hdr);
367 samples = hdr_frame_samples(hdr)*frame_info.
channels;
368 if (3 != frame_info.
layer)
374 to_skip = delay*frame_info.
channels;
375 detected_samples = samples*(uint64_t)frames;
376 if (detected_samples >= (uint64_t)to_skip)
377 detected_samples -= to_skip;
378 if (padding > 0 && detected_samples >= (uint64_t)padding)
379 detected_samples -= padding;
380 if (!detected_samples)
387 consumed += frame_size;
397 if (detected_samples)
409 size_t avg_bitrate_kbps = 0, frames = 0;
418 info->buffer = alloc_buf;
424 memmove(buf, buf + consumed, filled - consumed);
451#ifdef MINIMP3_ALLOW_MONO_STEREO_TRANSITION
461 size_t skip = MINIMP3_MIN(samples, to_skip);
466 info->samples += samples;
471 ret = progress_cb(user_data, orig_buf_size, orig_buf_size -
buf_size, &frame_info);
477 if (detected_samples &&
info->samples > detected_samples)
478 info->samples = detected_samples;
483 if (!alloc_buf &&
info->samples)
485 info->buffer = alloc_buf;
488 info->avg_bitrate_kbps = avg_bitrate_kbps/frames;
502 memset(&frame_info, 0,
sizeof(frame_info));
505 int free_format_bytes = 0, frame_size = 0, ret;
506 int i = mp3d_find_frame(buf,
buf_size, &free_format_bytes, &frame_size);
509 if (
i && !frame_size)
514 frame_info.
channels = HDR_IS_MONO(hdr) ? 1 : 2;
515 frame_info.
hz = hdr_sample_rate_hz(hdr);
516 frame_info.
layer = 4 - HDR_GET_LAYER(hdr);
522 if ((ret =
callback(user_data, hdr, frame_size, free_format_bytes,
buf_size, hdr - orig_buf, &frame_info)))
539 memset(&frame_info, 0,
sizeof(frame_info));
564 int free_format_bytes = 0, frame_size = 0, ret;
565 int i = mp3d_find_frame(buf + consumed, filled - consumed, &free_format_bytes, &frame_size);
566 if (
i && !frame_size)
573 const uint8_t *hdr = buf + consumed +
i;
574 frame_info.
channels = HDR_IS_MONO(hdr) ? 1 : 2;
575 frame_info.
hz = hdr_sample_rate_hz(hdr);
576 frame_info.
layer = 4 - HDR_GET_LAYER(hdr);
583 if ((ret =
callback(user_data, hdr, frame_size, free_format_bytes, filled - consumed, readed, &frame_info)))
586 readed += frame_size;
587 consumed +=
i + frame_size;
590 memmove(buf, buf + consumed, filled - consumed);
625 padding *=
info->channels;
627 dec->
samples = hdr_frame_samples(frame)*
info->channels*(uint64_t)frames;
630 if (padding > 0 && dec->
samples >= (uint64_t)padding)
653 idx_frame->
offset = offset;
661 dec->
samples += hdr_frame_samples(frame)*
info->channels;
667 if (!dec || !buf || (
size_t)-1 ==
buf_size || (flags & (~3)))
669 memset(dec, 0,
sizeof(*dec));
684#ifndef MINIMP3_SEEK_IDX_LINEAR_SEARCH
690 size_t mid = (
start + end) / 2;
758#ifdef MINIMP3_SEEK_IDX_LINEAR_SEARCH
769 int to_fill_bytes = 511;
771#ifdef MINIMP3_SEEK_IDX_LINEAR_SEARCH
775 i -= MINIMP3_MIN(
i, (
size_t)skip_frames);
778 while (
i && to_fill_bytes)
781 L3_gr_info_t gr_info[4];
782 int frame_bytes, frame_size;
790 if (readed != HDR_SIZE)
794 if (readed != (
size_t)(frame_size - HDR_SIZE))
796 bs_init(bs, hdr + HDR_SIZE, frame_size - HDR_SIZE);
801 bs_init(bs, hdr + HDR_SIZE, frame_size - HDR_SIZE);
806 if (L3_read_side_info(bs, gr_info, hdr) < 0)
808 frame_bytes = (bs->limit - bs->pos)/8;
809 to_fill_bytes -= MINIMP3_MIN(to_fill_bytes, frame_bytes);
824 if (readed != HDR_SIZE)
855 size_t samples_requested = samples;
858 memset(&frame_info, 0,
sizeof(frame_info));
916#ifndef MINIMP3_ALLOW_MONO_STEREO_TRANSITION
947 int frame_samples = hdr_frame_samples(dec_buf)*frame_info.
channels;
953 return samples_requested - samples;
956#ifndef MINIMP3_NO_STDIO
958#if defined(__linux__) || defined(__FreeBSD__)
961#include <sys/types.h>
965#if !defined(_GNU_SOURCE)
969#if !defined(MAP_POPULATE) && defined(__linux__)
970#define MAP_POPULATE 0x08000
971#elif !defined(MAP_POPULATE)
972#define MAP_POPULATE 0
978 munmap((
void *)map_info->
buffer, map_info->
size);
989 memset(map_info, 0,
sizeof(*map_info));
991 file = open(file_name, O_RDONLY);
992 if (
file < 0 && (errno == EAGAIN || errno == EINTR))
994 if (
file < 0 || fstat(
file, &st) < 0)
1000 map_info->
size = st.st_size;
1002 map_info->
buffer = (
const uint8_t *)mmap(
NULL, st.st_size, PROT_READ, MAP_PRIVATE | MAP_POPULATE,
file, 0);
1003 if (MAP_FAILED == map_info->
buffer && (errno == EAGAIN || errno == EINTR))
1006 if (MAP_FAILED == map_info->
buffer)
1011#if MINIMP3_ENABLE_RING && defined(__linux__) && defined(_GNU_SOURCE)
1012#define MINIMP3_HAVE_RING
1015#if defined(__linux__) && defined(_GNU_SOURCE)
1016 if (map_info->
buffer && MAP_FAILED != map_info->
buffer)
1017 munmap((
void *)map_info->
buffer, map_info->
size*2);
1031 int memfd, page_size;
1032#if defined(__linux__) && defined(_GNU_SOURCE)
1036 memset(map_info, 0,
sizeof(*map_info));
1039 page_size = sysconf(_SC_PAGESIZE);
1041 page_size = getpagesize();
1043 map_info->
size = (
size + page_size - 1)/page_size*page_size;
1045#if defined(__linux__) && defined(_GNU_SOURCE)
1046 memfd = memfd_create(
"mp3_ring", 0);
1051 res = ftruncate(memfd, map_info->
size);
1052 if (res && (errno == EAGAIN || errno == EINTR))
1053 goto retry_ftruncate;
1058 map_info->
buffer = (
const uint8_t *)mmap(
NULL, map_info->
size*2, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
1059 if (MAP_FAILED == map_info->
buffer && (errno == EAGAIN || errno == EINTR))
1061 if (MAP_FAILED == map_info->
buffer || !map_info->
buffer)
1064 buffer = mmap((
void *)map_info->
buffer, map_info->
size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, memfd, 0);
1065 if (MAP_FAILED == map_info->
buffer && (errno == EAGAIN || errno == EINTR))
1067 if (MAP_FAILED == map_info->
buffer || buffer != (
void *)map_info->
buffer)
1070 buffer = mmap((
void *)map_info->
buffer + map_info->
size, map_info->
size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, memfd, 0);
1071 if (MAP_FAILED == map_info->
buffer && (errno == EAGAIN || errno == EINTR))
1073 if (MAP_FAILED == map_info->
buffer || buffer != (
void *)(map_info->
buffer + map_info->
size))
1080 mp3dec_close_ring(map_info);
1083 memfd = shmget(IPC_PRIVATE, map_info->
size, IPC_CREAT | 0700);
1088 if (MAP_FAILED == map_info->
buffer && (errno == EAGAIN || errno == EINTR))
1090 if (MAP_FAILED == map_info->
buffer)
1092 if (map_info->
buffer != shmat(memfd, map_info->
buffer, 0))
1096 if (shmctl(memfd, IPC_RMID,
NULL) < 0)
1100 shmctl(memfd, IPC_RMID,
NULL);
1101 mp3dec_close_ring(map_info);
1106#elif defined(_WIN32)
1112 UnmapViewOfFile(map_info->
buffer);
1119 memset(map_info, 0,
sizeof(*map_info));
1123 s.LowPart = GetFileSize(
file, (
DWORD*)&
s.HighPart);
1124 if (
s.LowPart == INVALID_FILE_SIZE && GetLastError() != NO_ERROR)
1126 map_info->
size =
s.QuadPart;
1128 mapping = CreateFileMapping(
file,
NULL, PAGE_READONLY, 0, 0,
NULL);
1131 map_info->
buffer = (
const uint8_t*)MapViewOfFile(mapping, FILE_MAP_READ, 0, 0,
s.QuadPart);
1148 HANDLE file = CreateFileA(file_name, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
1149 if (INVALID_HANDLE_VALUE ==
file)
1151 return mp3dec_open_file_h(
file, map_info);
1154static int mp3dec_open_file_w(
const wchar_t *file_name,
mp3dec_map_info_t *map_info)
1158 HANDLE file = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
1159 if (INVALID_HANDLE_VALUE ==
file)
1161 return mp3dec_open_file_h(
file, map_info);
1169 free((
void *)map_info->
buffer);
1178 memset(map_info, 0,
sizeof(*map_info));
1179 FILE *
file = fopen(file_name,
"rb");
1278 if (!dec || !io || (flags & (~3)))
1280 memset(dec, 0,
sizeof(*dec));
1281#ifdef MINIMP3_HAVE_RING
1310#ifdef MINIMP3_HAVE_RING
1312 mp3dec_close_ring(&dec->
file);
1321 memset(dec, 0,
sizeof(*dec));
1325int mp3dec_detect_w(
const wchar_t *file_name)
1329 if ((ret = mp3dec_open_file_w(file_name, &map_info)))
1338 if ((ret = mp3dec_open_file_w(file_name, &map_info)))
1347 if ((ret = mp3dec_open_file_w(file_name, &map_info)))
1352int mp3dec_ex_open_w(
mp3dec_ex_t *dec,
const wchar_t *file_name,
int flags)
1355 if ((ret = mp3dec_open_file_w(file_name, &dec->
file)))
1365 memset(dec, 0,
sizeof(*dec));
register unsigned i
Definition inflate.c:1575
unsigned s
Definition inflate.c:1555
virtual ASIOError start()=0
struct backing_store_struct * info
Definition jmemsys.h:183
unsigned int uint32_t
Definition mid.cpp:100
unsigned char uint8_t
Definition mid.cpp:98
#define MINIMP3_MAX_SAMPLES_PER_FRAME
Definition minimp3.h:11
int16_t mp3d_sample_t
Definition minimp3.h:31
void mp3dec_init(mp3dec_t *dec)
int mp3dec_decode_frame(mp3dec_t *dec, const uint8_t *mp3, int mp3_bytes, mp3d_sample_t *pcm, mp3dec_frame_info_t *info)
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)
Definition minimp3_ex.h:83
#define MINIMP3_ID3_DETECT_SIZE
#define MINIMP3_BUF_SIZE
Definition minimp3_ex.h:20
int mp3dec_iterate(const char *file_name, MP3D_ITERATE_CB callback, void *user_data)
Definition minimp3_ex.h:1257
#define MP3D_SEEK_TO_SAMPLE
Definition minimp3_ex.h:13
static void mp3dec_close_file(mp3dec_map_info_t *map_info)
Definition minimp3_ex.h:1166
int mp3dec_load(mp3dec_t *dec, const char *file_name, mp3dec_file_info_t *info, MP3D_PROGRESS_CB progress_cb, void *user_data)
Definition minimp3_ex.h:1248
int(* MP3D_SEEK_CB)(uint64_t position, void *user_data)
Definition minimp3_ex.h:57
int mp3dec_ex_seek(mp3dec_ex_t *dec, uint64_t position)
Definition minimp3_ex.h:708
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)
Definition minimp3_ex.h:278
static int mp3dec_check_vbrtag(const uint8_t *frame, int frame_size, uint32_t *frames, int *delay, int *padding)
Definition minimp3_ex.h:183
int mp3dec_ex_open_buf(mp3dec_ex_t *dec, const uint8_t *buf, size_t buf_size, int flags)
Definition minimp3_ex.h:665
#define MP3D_E_IOERROR
Definition minimp3_ex.h:27
static int mp3dec_open_file(const char *file_name, mp3dec_map_info_t *map_info)
Definition minimp3_ex.h:1174
#define MINIMP3_PREDECODE_FRAMES
Definition minimp3_ex.h:17
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)
Definition minimp3_ex.h:273
static void mp3dec_skip_id3v1(const uint8_t *buf, size_t *pbuf_size)
Definition minimp3_ex.h:127
int(* MP3D_PROGRESS_CB)(void *user_data, size_t file_size, uint64_t offset, mp3dec_frame_info_t *info)
Definition minimp3_ex.h:84
static size_t mp3dec_skip_id3v2(const uint8_t *buf, size_t buf_size)
Definition minimp3_ex.h:151
static void mp3dec_skip_id3(const uint8_t **pbuf, size_t *pbuf_size)
Definition minimp3_ex.h:166
int mp3dec_ex_open_cb(mp3dec_ex_t *dec, mp3dec_io_t *io, int flags)
Definition minimp3_ex.h:1276
#define MP3D_E_MEMORY
Definition minimp3_ex.h:26
void mp3dec_ex_close(mp3dec_ex_t *dec)
Definition minimp3_ex.h:1308
int mp3dec_detect_cb(mp3dec_io_t *io, uint8_t *buf, size_t buf_size)
Definition minimp3_ex.h:235
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)
Definition minimp3_ex.h:1216
#define MP3D_DO_NOT_SCAN
Definition minimp3_ex.h:14
int mp3dec_iterate_buf(const uint8_t *buf, size_t buf_size, MP3D_ITERATE_CB callback, void *user_data)
Definition minimp3_ex.h:492
static int mp3dec_ex_open_mapinfo(mp3dec_ex_t *dec, int flags)
Definition minimp3_ex.h:1230
#define MP3D_E_USER
Definition minimp3_ex.h:28
int mp3dec_iterate_cb(mp3dec_io_t *io, uint8_t *buf, size_t buf_size, MP3D_ITERATE_CB callback, void *user_data)
Definition minimp3_ex.h:531
static size_t mp3dec_idx_binary_search(mp3dec_index_t *idx, uint64_t position)
Definition minimp3_ex.h:685
static int mp3dec_detect_mapinfo(mp3dec_map_info_t *map_info)
Definition minimp3_ex.h:1209
int mp3dec_detect_buf(const uint8_t *buf, size_t buf_size)
Definition minimp3_ex.h:230
#define MP3D_E_DECODE
Definition minimp3_ex.h:29
size_t mp3dec_ex_read(mp3dec_ex_t *dec, mp3d_sample_t *buf, size_t samples)
Definition minimp3_ex.h:846
size_t(* MP3D_READ_CB)(void *buf, size_t size, void *user_data)
Definition minimp3_ex.h:56
#define MINIMP3_IO_SIZE
Definition minimp3_ex.h:19
int mp3dec_ex_open(mp3dec_ex_t *dec, const char *file_name, int flags)
Definition minimp3_ex.h:1266
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)
Definition minimp3_ex.h:606
static int mp3dec_iterate_mapinfo(mp3dec_map_info_t *map_info, MP3D_ITERATE_CB callback, void *user_data)
Definition minimp3_ex.h:1223
#define MP3D_E_PARAM
Definition minimp3_ex.h:25
int mp3dec_detect(const char *file_name)
Definition minimp3_ex.h:1239
JOCTET * buffer
Definition juce_JPEGLoader.cpp:302
Definition minimp3_ex.h:68
uint64_t cur_sample
Definition minimp3_ex.h:73
int to_skip
Definition minimp3_ex.h:79
uint64_t samples
Definition minimp3_ex.h:73
mp3dec_t mp3d
Definition minimp3_ex.h:69
uint64_t end_offset
Definition minimp3_ex.h:73
int flags
Definition minimp3_ex.h:77
mp3dec_frame_info_t info
Definition minimp3_ex.h:74
int free_format_bytes
Definition minimp3_ex.h:78
mp3dec_io_t * io
Definition minimp3_ex.h:71
uint64_t offset
Definition minimp3_ex.h:73
int indexes_built
Definition minimp3_ex.h:77
mp3dec_index_t index
Definition minimp3_ex.h:72
size_t input_filled
Definition minimp3_ex.h:76
int vbr_tag_found
Definition minimp3_ex.h:77
uint64_t start_offset
Definition minimp3_ex.h:73
int buffer_samples
Definition minimp3_ex.h:79
mp3d_sample_t buffer[MINIMP3_MAX_SAMPLES_PER_FRAME]
Definition minimp3_ex.h:75
mp3dec_map_info_t file
Definition minimp3_ex.h:70
int start_delay
Definition minimp3_ex.h:79
int last_error
Definition minimp3_ex.h:80
size_t input_consumed
Definition minimp3_ex.h:76
uint64_t detected_samples
Definition minimp3_ex.h:73
int is_file
Definition minimp3_ex.h:77
int buffer_consumed
Definition minimp3_ex.h:79
Definition minimp3_ex.h:32
int avg_bitrate_kbps
Definition minimp3_ex.h:35
mp3d_sample_t * buffer
Definition minimp3_ex.h:33
int channels
Definition minimp3_ex.h:35
int layer
Definition minimp3_ex.h:35
int hz
Definition minimp3_ex.h:35
size_t samples
Definition minimp3_ex.h:34
int bitrate_kbps
Definition minimp3.h:15
int frame_bytes
Definition minimp3.h:15
int channels
Definition minimp3.h:15
int hz
Definition minimp3.h:15
int layer
Definition minimp3.h:15
Definition minimp3_ex.h:45
uint64_t offset
Definition minimp3_ex.h:47
uint64_t sample
Definition minimp3_ex.h:46
Definition minimp3_ex.h:51
mp3dec_frame_t * frames
Definition minimp3_ex.h:52
size_t num_frames
Definition minimp3_ex.h:53
size_t capacity
Definition minimp3_ex.h:53
Definition minimp3_ex.h:60
MP3D_SEEK_CB seek
Definition minimp3_ex.h:63
MP3D_READ_CB read
Definition minimp3_ex.h:61
void * read_data
Definition minimp3_ex.h:62
void * seek_data
Definition minimp3_ex.h:64
Definition minimp3_ex.h:39
size_t size
Definition minimp3_ex.h:41
const uint8_t * buffer
Definition minimp3_ex.h:40
RECT const char void(* callback)(const char *droppath))) SWELL_API_DEFINE(BOOL
Definition swell-functions.h:1004
unsigned int DWORD
Definition swell-types.h:164
void * HANDLE
Definition swell-types.h:212
BOOL CloseHandle(HANDLE hand)
Definition swell.cpp:157
memcpy(hh, h, RAND_HEAD_LEN)
typedef int(UZ_EXP MsgFn)()
#define SEEK_SET
Definition unzpriv.h:1302
#define SEEK_END
Definition unzpriv.h:1304
struct zdirent * file
Definition win32.c:1500