diff options
Diffstat (limited to '3rdparty/minimp3')
-rw-r--r-- | 3rdparty/minimp3/minimp3.h | 230 | ||||
-rw-r--r-- | 3rdparty/minimp3/minimp3_ex.h | 392 |
2 files changed, 144 insertions, 478 deletions
diff --git a/3rdparty/minimp3/minimp3.h b/3rdparty/minimp3/minimp3.h index 08c03b49ce5..3220ae1a85b 100644 --- a/3rdparty/minimp3/minimp3.h +++ b/3rdparty/minimp3/minimp3.h @@ -12,7 +12,7 @@ typedef struct { - int frame_bytes, channels, hz, layer, bitrate_kbps; + int frame_bytes, frame_offset, channels, hz, layer, bitrate_kbps; } mp3dec_frame_info_t; typedef struct @@ -86,7 +86,7 @@ int mp3dec_decode_frame(mp3dec_t *dec, const uint8_t *mp3, int mp3_bytes, mp3d_s #if !defined(MINIMP3_NO_SIMD) -#if !defined(MINIMP3_ONLY_SIMD) && (defined(_M_X64) || defined(_M_ARM64) || defined(__x86_64__) || defined(__aarch64__)) +#if !defined(MINIMP3_ONLY_SIMD) && (defined(_M_X64) || defined(__x86_64__) || defined(__aarch64__) || defined(_M_ARM64)) /* x64 always have SSE2, arm64 always have neon, no need for generic code */ #define MINIMP3_ONLY_SIMD #endif /* SIMD checks... */ @@ -136,7 +136,7 @@ static __inline__ __attribute__((always_inline)) void minimp3_cpuid(int CPUInfo[ #endif /* defined(__PIC__)*/ } #endif /* defined(_MSC_VER) || defined(MINIMP3_ONLY_SIMD) */ -static int have_simd() +static int have_simd(void) { #ifdef MINIMP3_ONLY_SIMD return 1; @@ -161,8 +161,9 @@ end: return g_have_simd - 1; #endif /* MINIMP3_ONLY_SIMD */ } -#elif defined(__ARM_NEON) || defined(__aarch64__) +#elif defined(__ARM_NEON) || defined(__aarch64__) || defined(_M_ARM64) #include <arm_neon.h> +#define HAVE_SSE 0 #define HAVE_SIMD 1 #define VSTORE vst1q_f32 #define VLD vld1q_f32 @@ -180,6 +181,7 @@ static int have_simd() return 1; } #else /* SIMD checks... */ +#define HAVE_SSE 0 #define HAVE_SIMD 0 #ifdef MINIMP3_ONLY_SIMD #error MINIMP3_ONLY_SIMD used, but SSE/NEON not enabled @@ -189,6 +191,18 @@ static int have_simd() #define HAVE_SIMD 0 #endif /* !defined(MINIMP3_NO_SIMD) */ +#if defined(__ARM_ARCH) && (__ARM_ARCH >= 6) && !defined(__aarch64__) && !defined(_M_ARM64) +#define HAVE_ARMV6 1 +static __inline__ __attribute__((always_inline)) int32_t minimp3_clip_int16_arm(int32_t a) +{ + int32_t x = 0; + __asm__ ("ssat %0, #16, %1" : "=r"(x) : "r"(a)); + return x; +} +#else +#define HAVE_ARMV6 0 +#endif + typedef struct { const uint8_t *buf; @@ -767,41 +781,72 @@ static void L3_huffman(float *dst, bs_t *bs, const L3_gr_info_t *gr_info, const int sfb_cnt = gr_info->region_count[ireg++]; const int16_t *codebook = tabs + tabindex[tab_num]; int linbits = g_linbits[tab_num]; - do + if (linbits) { - np = *sfb++ / 2; - pairs_to_decode = MINIMP3_MIN(big_val_cnt, np); - one = *scf++; do { - int j, w = 5; - int leaf = codebook[PEEK_BITS(w)]; - while (leaf < 0) + np = *sfb++ / 2; + pairs_to_decode = MINIMP3_MIN(big_val_cnt, np); + one = *scf++; + do { - FLUSH_BITS(w); - w = leaf & 7; - leaf = codebook[PEEK_BITS(w) - (leaf >> 3)]; - } - FLUSH_BITS(leaf >> 8); + int j, w = 5; + int leaf = codebook[PEEK_BITS(w)]; + while (leaf < 0) + { + FLUSH_BITS(w); + w = leaf & 7; + leaf = codebook[PEEK_BITS(w) - (leaf >> 3)]; + } + FLUSH_BITS(leaf >> 8); - for (j = 0; j < 2; j++, dst++, leaf >>= 4) + for (j = 0; j < 2; j++, dst++, leaf >>= 4) + { + int lsb = leaf & 0x0F; + if (lsb == 15) + { + lsb += PEEK_BITS(linbits); + FLUSH_BITS(linbits); + CHECK_BITS; + *dst = one*L3_pow_43(lsb)*((int32_t)bs_cache < 0 ? -1: 1); + } else + { + *dst = g_pow43[16 + lsb - 16*(bs_cache >> 31)]*one; + } + FLUSH_BITS(lsb ? 1 : 0); + } + CHECK_BITS; + } while (--pairs_to_decode); + } while ((big_val_cnt -= np) > 0 && --sfb_cnt >= 0); + } else + { + do + { + np = *sfb++ / 2; + pairs_to_decode = MINIMP3_MIN(big_val_cnt, np); + one = *scf++; + do { - int lsb = leaf & 0x0F; - if (lsb == 15 && linbits) + int j, w = 5; + int leaf = codebook[PEEK_BITS(w)]; + while (leaf < 0) { - lsb += PEEK_BITS(linbits); - FLUSH_BITS(linbits); - CHECK_BITS; - *dst = one*L3_pow_43(lsb)*((int32_t)bs_cache < 0 ? -1: 1); - } else + FLUSH_BITS(w); + w = leaf & 7; + leaf = codebook[PEEK_BITS(w) - (leaf >> 3)]; + } + FLUSH_BITS(leaf >> 8); + + for (j = 0; j < 2; j++, dst++, leaf >>= 4) { + int lsb = leaf & 0x0F; *dst = g_pow43[16 + lsb - 16*(bs_cache >> 31)]*one; + FLUSH_BITS(lsb ? 1 : 0); } - FLUSH_BITS(lsb ? 1 : 0); - } - CHECK_BITS; - } while (--pairs_to_decode); - } while ((big_val_cnt -= np) > 0 && --sfb_cnt >= 0); + CHECK_BITS; + } while (--pairs_to_decode); + } while ((big_val_cnt -= np) > 0 && --sfb_cnt >= 0); + } } for (np = 1 - big_val_cnt;; dst += 4) @@ -836,12 +881,22 @@ static void L3_midside_stereo(float *left, int n) int i = 0; float *right = left + 576; #if HAVE_SIMD - if (have_simd()) for (; i < n - 3; i += 4) + if (have_simd()) { - f4 vl = VLD(left + i); - f4 vr = VLD(right + i); - VSTORE(left + i, VADD(vl, vr)); - VSTORE(right + i, VSUB(vl, vr)); + for (; i < n - 3; i += 4) + { + f4 vl = VLD(left + i); + f4 vr = VLD(right + i); + VSTORE(left + i, VADD(vl, vr)); + VSTORE(right + i, VSUB(vl, vr)); + } +#ifdef __GNUC__ + /* Workaround for spurious -Waggressive-loop-optimizations warning from gcc. + * For more info see: https://github.com/lieff/minimp3/issues/88 + */ + if (__builtin_constant_p(n % 4 == 0) && n % 4 == 0) + return; +#endif } #endif /* HAVE_SIMD */ for (; i < n; i++) @@ -1308,7 +1363,7 @@ static void mp3d_DCT_II(float *grbuf, int n) } else #endif /* HAVE_SIMD */ #ifdef MINIMP3_ONLY_SIMD - {} + {} /* for HAVE_SIMD=1, MINIMP3_ONLY_SIMD=1 case we do not need non-intrinsic "else" branch */ #else /* MINIMP3_ONLY_SIMD */ for (; k < n; k++) { @@ -1374,10 +1429,16 @@ static void mp3d_DCT_II(float *grbuf, int n) #ifndef MINIMP3_FLOAT_OUTPUT static int16_t mp3d_scale_pcm(float sample) { +#if HAVE_ARMV6 + int32_t s32 = (int32_t)(sample + .5f); + s32 -= (s32 < 0); + int16_t s = (int16_t)minimp3_clip_int16_arm(s32); +#else if (sample >= 32766.5) return (int16_t) 32767; if (sample <= -32767.5) return (int16_t)-32768; int16_t s = (int16_t)(sample + .5f); s -= (s < 0); /* away from zero, to be compliant */ +#endif return s; } #else /* MINIMP3_FLOAT_OUTPUT */ @@ -1532,7 +1593,7 @@ static void mp3d_synth(float *xl, mp3d_sample_t *dstl, int nch, float *lins) } else #endif /* HAVE_SIMD */ #ifdef MINIMP3_ONLY_SIMD - {} + {} /* for HAVE_SIMD=1, MINIMP3_ONLY_SIMD=1 case we do not need non-intrinsic "else" branch */ #else /* MINIMP3_ONLY_SIMD */ for (i = 14; i >= 0; i--) { @@ -1641,7 +1702,7 @@ static int mp3d_find_frame(const uint8_t *mp3, int mp3_bytes, int *free_format_b } } *ptr_frame_bytes = 0; - return i; + return mp3_bytes; } void mp3dec_init(mp3dec_t *dec) @@ -1670,7 +1731,7 @@ int mp3dec_decode_frame(mp3dec_t *dec, const uint8_t *mp3, int mp3_bytes, mp3d_s i = mp3d_find_frame(mp3, mp3_bytes, &dec->free_format_bytes, &frame_size); if (!frame_size || i + frame_size > mp3_bytes) { - info->frame_bytes = 0; + info->frame_bytes = i; return 0; } } @@ -1678,6 +1739,7 @@ int mp3dec_decode_frame(mp3dec_t *dec, const uint8_t *mp3, int mp3_bytes, mp3d_s hdr = mp3 + i; memcpy(dec->header, hdr, HDR_SIZE); info->frame_bytes = i + frame_size; + info->frame_offset = i; info->channels = HDR_IS_MONO(hdr) ? 1 : 2; info->hz = hdr_sample_rate_hz(hdr); info->layer = 4 - HDR_GET_LAYER(hdr); @@ -1746,60 +1808,56 @@ int mp3dec_decode_frame(mp3dec_t *dec, const uint8_t *mp3, int mp3_bytes, mp3d_s #ifdef MINIMP3_FLOAT_OUTPUT void mp3dec_f32_to_s16(const float *in, int16_t *out, int num_samples) { - if(num_samples > 0) - { - int i = 0; + int i = 0; #if HAVE_SIMD - int aligned_count = num_samples & ~7; - - for(;i < aligned_count;i+=8) - { - static const f4 g_scale = { 32768.0f, 32768.0f, 32768.0f, 32768.0f }; - f4 a = VMUL(VLD(&in[i ]), g_scale); - f4 b = VMUL(VLD(&in[i+4]), g_scale); + int aligned_count = num_samples & ~7; + for(; i < aligned_count; i += 8) + { + static const f4 g_scale = { 32768.0f, 32768.0f, 32768.0f, 32768.0f }; + f4 a = VMUL(VLD(&in[i ]), g_scale); + f4 b = VMUL(VLD(&in[i+4]), g_scale); #if HAVE_SSE - static const f4 g_max = { 32767.0f, 32767.0f, 32767.0f, 32767.0f }; - static const f4 g_min = { -32768.0f, -32768.0f, -32768.0f, -32768.0f }; - __m128i pcm8 = _mm_packs_epi32(_mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(a, g_max), g_min)), - _mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(b, g_max), g_min))); - out[i ] = _mm_extract_epi16(pcm8, 0); - out[i+1] = _mm_extract_epi16(pcm8, 1); - out[i+2] = _mm_extract_epi16(pcm8, 2); - out[i+3] = _mm_extract_epi16(pcm8, 3); - out[i+4] = _mm_extract_epi16(pcm8, 4); - out[i+5] = _mm_extract_epi16(pcm8, 5); - out[i+6] = _mm_extract_epi16(pcm8, 6); - out[i+7] = _mm_extract_epi16(pcm8, 7); + static const f4 g_max = { 32767.0f, 32767.0f, 32767.0f, 32767.0f }; + static const f4 g_min = { -32768.0f, -32768.0f, -32768.0f, -32768.0f }; + __m128i pcm8 = _mm_packs_epi32(_mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(a, g_max), g_min)), + _mm_cvtps_epi32(_mm_max_ps(_mm_min_ps(b, g_max), g_min))); + out[i ] = _mm_extract_epi16(pcm8, 0); + out[i+1] = _mm_extract_epi16(pcm8, 1); + out[i+2] = _mm_extract_epi16(pcm8, 2); + out[i+3] = _mm_extract_epi16(pcm8, 3); + out[i+4] = _mm_extract_epi16(pcm8, 4); + out[i+5] = _mm_extract_epi16(pcm8, 5); + out[i+6] = _mm_extract_epi16(pcm8, 6); + out[i+7] = _mm_extract_epi16(pcm8, 7); #else /* HAVE_SSE */ - int16x4_t pcma, pcmb; - a = VADD(a, VSET(0.5f)); - b = VADD(b, VSET(0.5f)); - pcma = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(a), vreinterpretq_s32_u32(vcltq_f32(a, VSET(0))))); - pcmb = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(b), vreinterpretq_s32_u32(vcltq_f32(b, VSET(0))))); - vst1_lane_s16(out+i , pcma, 0); - vst1_lane_s16(out+i+1, pcma, 1); - vst1_lane_s16(out+i+2, pcma, 2); - vst1_lane_s16(out+i+3, pcma, 3); - vst1_lane_s16(out+i+4, pcmb, 0); - vst1_lane_s16(out+i+5, pcmb, 1); - vst1_lane_s16(out+i+6, pcmb, 2); - vst1_lane_s16(out+i+7, pcmb, 3); + int16x4_t pcma, pcmb; + a = VADD(a, VSET(0.5f)); + b = VADD(b, VSET(0.5f)); + pcma = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(a), vreinterpretq_s32_u32(vcltq_f32(a, VSET(0))))); + pcmb = vqmovn_s32(vqaddq_s32(vcvtq_s32_f32(b), vreinterpretq_s32_u32(vcltq_f32(b, VSET(0))))); + vst1_lane_s16(out+i , pcma, 0); + vst1_lane_s16(out+i+1, pcma, 1); + vst1_lane_s16(out+i+2, pcma, 2); + vst1_lane_s16(out+i+3, pcma, 3); + vst1_lane_s16(out+i+4, pcmb, 0); + vst1_lane_s16(out+i+5, pcmb, 1); + vst1_lane_s16(out+i+6, pcmb, 2); + vst1_lane_s16(out+i+7, pcmb, 3); #endif /* HAVE_SSE */ - } + } #endif /* HAVE_SIMD */ - for(; i < num_samples; i++) + for(; i < num_samples; i++) + { + float sample = in[i] * 32768.0f; + if (sample >= 32766.5) + out[i] = (int16_t) 32767; + else if (sample <= -32767.5) + out[i] = (int16_t)-32768; + else { - float sample = in[i] * 32768.0f; - if (sample >= 32766.5) - out[i] = (int16_t) 32767; - else if (sample <= -32767.5) - out[i] = (int16_t)-32768; - else - { - int16_t s = (int16_t)(sample + .5f); - s -= (s < 0); /* away from zero, to be compliant */ - out[i] = s; - } + int16_t s = (int16_t)(sample + .5f); + s -= (s < 0); /* away from zero, to be compliant */ + out[i] = s; } } } diff --git a/3rdparty/minimp3/minimp3_ex.h b/3rdparty/minimp3/minimp3_ex.h deleted file mode 100644 index 9e370e2c0ed..00000000000 --- a/3rdparty/minimp3/minimp3_ex.h +++ /dev/null @@ -1,392 +0,0 @@ -#ifndef MINIMP3_EXT_H -#define MINIMP3_EXT_H -/* - https://github.com/lieff/minimp3 - 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. - This software is distributed without any warranty. - See <http://creativecommons.org/publicdomain/zero/1.0/>. -*/ -#include "minimp3.h" - -#define MP3D_SEEK_TO_BYTE 0 -#define MP3D_SEEK_TO_SAMPLE 1 -#define MP3D_SEEK_TO_SAMPLE_INDEXED 2 - -typedef struct -{ - mp3d_sample_t *buffer; - size_t samples; /* channels included, byte size = samples*sizeof(int16_t) */ - int channels, hz, layer, avg_bitrate_kbps, frame_bytes; -} mp3dec_file_info_t; - -typedef struct -{ - const uint8_t *buffer; - size_t size; -} mp3dec_map_info_t; - -typedef struct -{ - mp3dec_t mp3d; - mp3dec_map_info_t file; - int seek_method; -#ifndef MINIMP3_NO_STDIO - int is_file; -#endif -} mp3dec_ex_t; - -typedef int (*MP3D_ITERATE_CB)(void *user_data, const uint8_t *frame, int frame_size, size_t offset, mp3dec_frame_info_t *info); -typedef int (*MP3D_PROGRESS_CB)(void *user_data, size_t file_size, size_t offset, mp3dec_frame_info_t *info); - -#ifdef __cplusplus -extern "C" { -#endif -size_t mp3dec_skip_id3v2(const uint8_t *buf, size_t buf_size); - -/* decode whole buffer block */ -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); -/* iterate through frames with optional decoding */ -void mp3dec_iterate_buf(const uint8_t *buf, size_t buf_size, MP3D_ITERATE_CB callback, void *user_data); -/* decoder with seeking capability */ -int mp3dec_ex_open_buf(mp3dec_ex_t *dec, const uint8_t *buf, size_t buf_size, int seek_method); -void mp3dec_ex_close(mp3dec_ex_t *dec); -void mp3dec_ex_seek(mp3dec_ex_t *dec, size_t position); -int mp3dec_ex_read(mp3dec_ex_t *dec, int16_t *buf, int samples); -#ifndef MINIMP3_NO_STDIO -/* stdio versions with file pre-load */ -int mp3dec_load(mp3dec_t *dec, const char *file_name, mp3dec_file_info_t *info, MP3D_PROGRESS_CB progress_cb, void *user_data); -int mp3dec_iterate(const char *file_name, MP3D_ITERATE_CB callback, void *user_data); -int mp3dec_ex_open(mp3dec_ex_t *dec, const char *file_name, int seek_method); -#endif - -#ifdef __cplusplus -} -#endif -#endif /*MINIMP3_EXT_H*/ - -#ifdef MINIMP3_IMPLEMENTATION - -size_t mp3dec_skip_id3v2(const uint8_t *buf, size_t buf_size) -{ - if (buf_size > 10 && !strncmp((char *)buf, "ID3", 3)) - { - return (((buf[6] & 0x7f) << 21) | ((buf[7] & 0x7f) << 14) | - ((buf[8] & 0x7f) << 7) | (buf[9] & 0x7f)) + 10; - } - return 0; -} - -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) -{ - size_t orig_buf_size = buf_size; - mp3d_sample_t pcm[MINIMP3_MAX_SAMPLES_PER_FRAME]; - mp3dec_frame_info_t frame_info; - memset(info, 0, sizeof(*info)); - memset(&frame_info, 0, sizeof(frame_info)); - /* skip id3v2 */ - size_t id3v2size = mp3dec_skip_id3v2(buf, buf_size); - if (id3v2size > buf_size) - return; - buf += id3v2size; - buf_size -= id3v2size; - /* try to make allocation size assumption by first frame */ - mp3dec_init(dec); - int samples; - do - { - samples = mp3dec_decode_frame(dec, buf, buf_size, pcm, &frame_info); - buf += frame_info.frame_bytes; - buf_size -= frame_info.frame_bytes; - if (samples) { - info->frame_bytes += frame_info.frame_bytes + id3v2size; - break; - } - } while (frame_info.frame_bytes); - if (!samples) - return; - samples *= frame_info.channels; - size_t allocated = (buf_size/frame_info.frame_bytes)*samples*sizeof(mp3d_sample_t) + MINIMP3_MAX_SAMPLES_PER_FRAME*sizeof(mp3d_sample_t); - info->buffer = (mp3d_sample_t*)malloc(allocated); - if (!info->buffer) - return; - info->samples = samples; - memcpy(info->buffer, pcm, info->samples*sizeof(mp3d_sample_t)); - /* save info */ - info->channels = frame_info.channels; - info->hz = frame_info.hz; - info->layer = frame_info.layer; - size_t avg_bitrate_kbps = frame_info.bitrate_kbps; - size_t frames = 1; - /* decode rest frames */ - int frame_bytes; - do - { - if ((allocated - info->samples*sizeof(mp3d_sample_t)) < MINIMP3_MAX_SAMPLES_PER_FRAME*sizeof(mp3d_sample_t)) - { - allocated *= 2; - info->buffer = (mp3d_sample_t*)realloc(info->buffer, allocated); - } - samples = mp3dec_decode_frame(dec, buf, buf_size, info->buffer + info->samples, &frame_info); - frame_bytes = frame_info.frame_bytes; - buf += frame_bytes; - buf_size -= frame_bytes; - if (samples) - { - info->frame_bytes += frame_info.frame_bytes; - - if (info->hz != frame_info.hz || info->layer != frame_info.layer) - break; - if (info->channels && info->channels != frame_info.channels) -#ifdef MINIMP3_ALLOW_MONO_STEREO_TRANSITION - info->channels = 0; /* mark file with mono-stereo transition */ -#else - break; -#endif - - info->samples += samples*frame_info.channels; - avg_bitrate_kbps += frame_info.bitrate_kbps; - frames++; - if (progress_cb) - progress_cb(user_data, orig_buf_size, orig_buf_size - buf_size, &frame_info); - } - } while (frame_bytes); - /* reallocate to normal buffer size */ - if (allocated != info->samples*sizeof(mp3d_sample_t)) - info->buffer = (mp3d_sample_t*)realloc(info->buffer, info->samples*sizeof(mp3d_sample_t)); - info->avg_bitrate_kbps = avg_bitrate_kbps/frames; -} - -void mp3dec_iterate_buf(const uint8_t *buf, size_t buf_size, MP3D_ITERATE_CB callback, void *user_data) -{ - if (!callback) - return; - mp3dec_frame_info_t frame_info; - memset(&frame_info, 0, sizeof(frame_info)); - /* skip id3v2 */ - size_t id3v2size = mp3dec_skip_id3v2(buf, buf_size); - if (id3v2size > buf_size) - return; - const uint8_t *orig_buf = buf; - buf += id3v2size; - buf_size -= id3v2size; - do - { - int free_format_bytes = 0, frame_size = 0; - int i = mp3d_find_frame(buf, buf_size, &free_format_bytes, &frame_size); - buf += i; - buf_size -= i; - if (i && !frame_size) - continue; - if (!frame_size) - break; - const uint8_t *hdr = buf; - frame_info.channels = HDR_IS_MONO(hdr) ? 1 : 2; - frame_info.hz = hdr_sample_rate_hz(hdr); - frame_info.layer = 4 - HDR_GET_LAYER(hdr); - frame_info.bitrate_kbps = hdr_bitrate_kbps(hdr); - frame_info.frame_bytes = frame_size; - - if (callback(user_data, hdr, frame_size, hdr - orig_buf, &frame_info)) - break; - buf += frame_size; - buf_size -= frame_size; - } while (1); -} - -int mp3dec_ex_open_buf(mp3dec_ex_t *dec, const uint8_t *buf, size_t buf_size, int seek_method) -{ - memset(dec, 0, sizeof(*dec)); - dec->file.buffer = buf; - dec->file.size = buf_size; - dec->seek_method = seek_method; - mp3dec_init(&dec->mp3d); - return 0; -} - -/*void mp3dec_ex_seek(mp3dec_ex_t *dec, size_t position) -{ -} - -int mp3dec_ex_read(mp3dec_ex_t *dec, int16_t *buf, int samples) -{ - return 0; -}*/ - -#ifndef MINIMP3_NO_STDIO - -#if defined(__linux__) || defined(__FreeBSD__) -#include <errno.h> -#include <sys/mman.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <unistd.h> -#include <fcntl.h> -#if !defined(MAP_POPULATE) && defined(__linux__) -#define MAP_POPULATE 0x08000 -#elif !defined(MAP_POPULATE) -#define MAP_POPULATE 0 -#endif - -static void mp3dec_close_file(mp3dec_map_info_t *map_info) -{ - if (map_info->buffer && MAP_FAILED != map_info->buffer) - munmap((void *)map_info->buffer, map_info->size); - map_info->buffer = 0; - map_info->size = 0; -} - -static int mp3dec_open_file(const char *file_name, mp3dec_map_info_t *map_info) -{ - int file; - struct stat st; - memset(map_info, 0, sizeof(*map_info)); -retry_open: - file = open(file_name, O_RDONLY); - if (file < 0 && (errno == EAGAIN || errno == EINTR)) - goto retry_open; - if (file < 0 || fstat(file, &st) < 0) - { - close(file); - return -1; - } - - map_info->size = st.st_size; -retry_mmap: - map_info->buffer = (const uint8_t *)mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE | MAP_POPULATE, file, 0); - if (MAP_FAILED == map_info->buffer && (errno == EAGAIN || errno == EINTR)) - goto retry_mmap; - close(file); - if (MAP_FAILED == map_info->buffer) - return -1; - return 0; -} -#elif defined(_WIN32) -#include <windows.h> - -static void mp3dec_close_file(mp3dec_map_info_t *map_info) -{ - if (map_info->buffer) - UnmapViewOfFile(map_info->buffer); - map_info->buffer = 0; - map_info->size = 0; -} - -static int mp3dec_open_file(const char *file_name, mp3dec_map_info_t *map_info) -{ - memset(map_info, 0, sizeof(*map_info)); - - HANDLE file = CreateFileA(file_name, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); - if (INVALID_HANDLE_VALUE == file) - return -1; - LARGE_INTEGER s; - s.LowPart = GetFileSize(file, (DWORD*)&s.HighPart); - if (s.LowPart == INVALID_FILE_SIZE && GetLastError() != NO_ERROR) - goto error; - map_info->size = s.QuadPart; - - HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL); - if (!mapping) - goto error; - map_info->buffer = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, s.QuadPart); - CloseHandle(mapping); - if (!map_info->buffer) - goto error; - - CloseHandle(file); - return 0; -error: - mp3dec_close_file(map_info); - CloseHandle(file); - return -1; -} -#else -#include <stdio.h> - -static void mp3dec_close_file(mp3dec_map_info_t *map_info) -{ - if (map_info->buffer) - free((void *)map_info->buffer); - map_info->buffer = 0; - map_info->size = 0; -} - -static int mp3dec_open_file(const char *file_name, mp3dec_map_info_t *map_info) -{ - memset(map_info, 0, sizeof(*map_info)); - FILE *file = fopen(file_name, "rb"); - if (!file) - return -1; - long size = -1; - if (fseek(file, 0, SEEK_END)) - goto error; - size = ftell(file); - if (size < 0) - goto error; - map_info->size = (size_t)size; - if (fseek(file, 0, SEEK_SET)) - goto error; - map_info->buffer = (uint8_t *)malloc(map_info->size); - if (!map_info->buffer) - goto error; - if (fread((void *)map_info->buffer, 1, map_info->size, file) != map_info->size) - goto error; - fclose(file); - return 0; -error: - mp3dec_close_file(map_info); - fclose(file); - return -1; -} -#endif - -int mp3dec_load(mp3dec_t *dec, const char *file_name, mp3dec_file_info_t *info, MP3D_PROGRESS_CB progress_cb, void *user_data) -{ - int ret; - mp3dec_map_info_t map_info; - if ((ret = mp3dec_open_file(file_name, &map_info))) - return ret; - mp3dec_load_buf(dec, map_info.buffer, map_info.size, info, progress_cb, user_data); - mp3dec_close_file(&map_info); - return 0; -} - -int mp3dec_iterate(const char *file_name, MP3D_ITERATE_CB callback, void *user_data) -{ - int ret; - mp3dec_map_info_t map_info; - if ((ret = mp3dec_open_file(file_name, &map_info))) - return ret; - mp3dec_iterate_buf(map_info.buffer, map_info.size, callback, user_data); - mp3dec_close_file(&map_info); - return 0; -} - -void mp3dec_ex_close(mp3dec_ex_t *dec) -{ - if (dec->is_file) - mp3dec_close_file(&dec->file); - else - free((void *)dec->file.buffer); - memset(dec, 0, sizeof(*dec)); -} - -int mp3dec_ex_open(mp3dec_ex_t *dec, const char *file_name, int seek_method) -{ - int ret; - memset(dec, 0, sizeof(*dec)); - if ((ret = mp3dec_open_file(file_name, &dec->file))) - return ret; - dec->seek_method = seek_method; - dec->is_file = 1; - mp3dec_init(&dec->mp3d); - return 0; -} -#else -void mp3dec_ex_close(mp3dec_ex_t *dec) -{ - free((void*)dec->file.buffer); - memset(dec, 0, sizeof(*dec)); -} -#endif - -#endif /*MINIMP3_IMPLEMENTATION*/ |