From 1e7f4c3de1fd2036e911693f65802d74d0832d92 Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Wed, 18 Nov 2026 17:37:10 +0300
Subject: [PATCH] update minimp3 from mainstream (Nov.18, 2026)
---
Makefile.os2 | 3 -
src/codecs/minimp3/minimp3.h | 71 +++++++++++++++--
src/codecs/minimp3/minimp3_ex.h | 137 ++++++++++++++++++++------------
3 files changed, 150 insertions(+), 61 deletions(-)
diff --git a/Makefile.os2 b/Makefile.os2
index 0b34b097..604e9a50 100644
--- a/Makefile.os2
+++ b/Makefile.os2
@@ -200,9 +200,6 @@ $(LNKFILE):
.c.obj:
wcc386 $(CFLAGS) -fo=$^@ $<
-music_minimp3.obj: music_minimp3.c
- wcc386 $(CFLAGS) -za99 -fo=$^@ $<
-
playmus.obj: playmus.c
wcc386 $(CFLAGS_BASE) -fo=$^@ $<
diff --git a/src/codecs/minimp3/minimp3.h b/src/codecs/minimp3/minimp3.h
index 88cd91c0..a99bd35c 100644
--- a/src/codecs/minimp3/minimp3.h
+++ b/src/codecs/minimp3/minimp3.h
@@ -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(__x86_64__) || defined(__aarch64__) || defined(_M_ARM64))
+#if !defined(MINIMP3_ONLY_SIMD) && (defined(_M_X64) || defined(__x86_64__) || defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC))
/* x64 always have SSE2, arm64 always have neon, no need for generic code */
#define MINIMP3_ONLY_SIMD
#endif /* SIMD checks... */
@@ -161,7 +161,7 @@ static int have_simd(void)
return g_have_simd - 1;
#endif /* MINIMP3_ONLY_SIMD */
}
-#elif defined(__ARM_NEON) || defined(__aarch64__) || defined(_M_ARM64)
+#elif defined(__ARM_NEON) || defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
#include <arm_neon.h>
#define HAVE_SSE 0
#define HAVE_SIMD 1
@@ -191,7 +191,7 @@ static int have_simd(void)
#define HAVE_SIMD 0
#endif /* !defined(MINIMP3_NO_SIMD) */
-#if defined(__ARM_ARCH) && (__ARM_ARCH >= 6) && !defined(__aarch64__) && !defined(_M_ARM64) && !defined(__ARM_ARCH_6M__)
+#if defined(__ARM_ARCH) && (__ARM_ARCH >= 6) && !defined(__aarch64__) && !defined(_M_ARM64) && !defined(_M_ARM64EC) && !defined(__ARM_ARCH_6M__)
#define HAVE_ARMV6 1
static __inline__ __attribute__((always_inline)) int32_t minimp3_clip_int16_arm(int32_t a)
{
@@ -293,11 +293,26 @@ static unsigned hdr_sample_rate_hz(const uint8_t *h)
return g_hz[HDR_GET_SAMPLE_RATE(h)] >> (int)!HDR_TEST_MPEG1(h) >> (int)!HDR_TEST_NOT_MPEG25(h);
}
+/**
+ * Returns the number of PCM samples per channel stored in one frame.
+ * Layer I always has 384 samples; Layer II always has 1152. Layer III has
+ * 1152 samples for MPEG-1 and 576 for MPEG-2/MPEG-2.5.
+ * @param h pointer to a 4-byte frame header
+ * @return samples per channel
+ */
static unsigned hdr_frame_samples(const uint8_t *h)
{
return HDR_IS_LAYER_1(h) ? 384 : (1152 >> (int)HDR_IS_FRAME_576(h));
}
+/**
+ * Computes the frame length in bytes, including the 4-byte header but not the
+ * padding slot (see hdr_padding). Free-format frames (bitrate index 0) do not
+ * carry their length, so free_format_size is returned for them.
+ * @param h pointer to a 4-byte frame header
+ * @param free_format_size fallback frame length for free-format streams
+ * @return frame length in bytes
+ */
static int hdr_frame_bytes(const uint8_t *h, int free_format_size)
{
int frame_bytes = hdr_frame_samples(h)*hdr_bitrate_kbps(h)*125/hdr_sample_rate_hz(h);
@@ -516,8 +531,8 @@ static int L3_read_side_info(bs_t *bs, L3_gr_info_t *gr, const uint8_t *hdr)
unsigned tables, scfsi = 0;
int main_data_begin, part_23_sum = 0;
- int sr_idx = HDR_GET_MY_SAMPLE_RATE(hdr); sr_idx -= (sr_idx != 0);
int gr_count = HDR_IS_MONO(hdr) ? 1 : 2;
+ int sr_idx = HDR_GET_MY_SAMPLE_RATE(hdr); sr_idx -= (sr_idx != 0);
if (HDR_TEST_MPEG1(hdr))
{
@@ -1437,14 +1452,15 @@ static void mp3d_DCT_II(float *grbuf, int n)
#ifndef MINIMP3_FLOAT_OUTPUT
static int16_t mp3d_scale_pcm(float sample)
{
+ int16_t s;
#if HAVE_ARMV6
int32_t s32 = (int32_t)(sample + .5f);
s32 -= (s32 < 0);
- int16_t s = (int16_t)minimp3_clip_int16_arm(s32);
+ 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 = (int16_t)(sample + .5f);
s -= (s < 0); /* away from zero, to be compliant */
#endif
return s;
@@ -1680,22 +1696,52 @@ static int hdr_is_null_or_tag(const uint8_t *hdr, int bytes_left)
return hdr_is_null(hdr) || hdr_is_id3v1(hdr, bytes_left);
}
+/**
+ * Verifies that a run of consecutive frame headers all match, so that a sync
+ * that only looks like a header can be rejected. The scan ends early (with
+ * success) once at least one header matched, either at the end of the buffer
+ * or at a trailing null/ID3v1 region.
+ * @param hdr pointer to the first 4-byte frame header
+ * @param mp3_bytes bytes remaining in the buffer starting at hdr
+ * @param frame_bytes free-format frame length to pass through to
+ * hdr_frame_bytes (see its free_format_size parameter); only
+ * consulted if a later frame has bitrate index 0
+ * @return 1 if the headers match, 0 if a following header differs
+ */
static int mp3d_match_frame(const uint8_t *hdr, int mp3_bytes, int frame_bytes)
{
int i, nmatch;
for (i = 0, nmatch = 0; nmatch < MAX_FRAME_SYNC_MATCHES; nmatch++)
{
+ /* advance to the start of the next frame */
i += hdr_frame_bytes(hdr + i, frame_bytes) + hdr_padding(hdr + i);
+ /* end of buffer, or trailing null/ID3v1 data: succeed if we already
+ matched at least one header */
if (i + HDR_SIZE > mp3_bytes)
return nmatch > 0;
if (hdr_is_null_or_tag(hdr + i, mp3_bytes - i))
return nmatch > 0;
+ /* a following header differs: the first header was not a real sync */
if (!hdr_compare(hdr, hdr + i))
return 0;
}
return 1;
}
+/**
+ * Searches a buffer for the first valid MPEG frame and returns its offset. A
+ * candidate header is accepted when its computed length walks across up to
+ * MAX_FRAME_SYNC_MATCHES matching headers (or, at offset 0, exactly fills the
+ * buffer).
+ * @param mp3 pointer to the start of the buffer
+ * @param mp3_bytes number of bytes available from mp3
+ * @param free_format_bytes in/out free-format frame length used while scanning;
+ * reset to 0 when a candidate is rejected
+ * @param ptr_frame_bytes out: length in bytes (including the padding slot) of
+ * the found frame, or 0 if none was found
+ * @return byte offset of the found frame relative to mp3, or mp3_bytes if no
+ * frame was found
+ */
static int mp3d_find_frame(const uint8_t *mp3, int mp3_bytes, int *free_format_bytes, int *ptr_frame_bytes)
{
int i, k;
@@ -1706,6 +1752,8 @@ static int mp3d_find_frame(const uint8_t *mp3, int mp3_bytes, int *free_format_b
int frame_bytes = hdr_frame_bytes(mp3, *free_format_bytes);
int frame_and_padding = frame_bytes + hdr_padding(mp3);
+ /* free-format frame: the header carries no length, so look ahead
+ for two more matching headers to derive the frame size */
for (k = HDR_SIZE; !frame_bytes && k < MAX_FREE_FORMAT_FRAME_SIZE && i + 2*k < mp3_bytes - HDR_SIZE; k++)
{
if (hdr_compare(mp3, mp3 + k))
@@ -1738,6 +1786,17 @@ void mp3dec_init(mp3dec_t *dec)
dec->header[0] = 0;
}
+/**
+ * Decodes one MPEG frame, first locating the next frame and skipping any
+ * leading garbage or ID3v2 tag.
+ * @param dec decoder state, initialized with mp3dec_init
+ * @param mp3 buffer of compressed MPEG data
+ * @param mp3_bytes number of bytes available in mp3
+ * @param pcm output buffer for PCM samples, or NULL to only inspect the header
+ * @param info receives frame metadata; info->frame_bytes is always set to the
+ * number of consumed (or skippable) bytes, even when 0 is returned
+ * @return number of PCM samples per channel, or 0 on error/EOF
+ */
int mp3dec_decode_frame(mp3dec_t *dec, const uint8_t *mp3, int mp3_bytes, mp3d_sample_t *pcm, mp3dec_frame_info_t *info)
{
int i = 0, igr, frame_size = 0, success = 1;
diff --git a/src/codecs/minimp3/minimp3_ex.h b/src/codecs/minimp3/minimp3_ex.h
index e84d3dce..efc942ad 100644
--- a/src/codecs/minimp3/minimp3_ex.h
+++ b/src/codecs/minimp3/minimp3_ex.h
@@ -158,9 +158,11 @@ static void mp3dec_skip_id3v1(const uint8_t *buf, size_t *pbuf_size)
#ifndef MINIMP3_NOSKIP_APEV2
if (buf_size > 32 && !memcmp(buf + buf_size - 32, "APETAGEX", 8))
{
+ const uint8_t *tag;
+ uint32_t tag_size;
buf_size -= 32;
- const uint8_t *tag = buf + buf_size + 8 + 4;
- uint32_t tag_size = ((uint32_t)tag[3] << 24) | (tag[2] << 16) | (tag[1] << 8) | tag[0];
+ tag = buf + buf_size + 8 + 4;
+ tag_size = ((uint32_t)tag[3] << 24) | (tag[2] << 16) | (tag[1] << 8) | tag[0];
if (buf_size >= tag_size)
buf_size -= tag_size;
}
@@ -214,6 +216,10 @@ static int mp3dec_check_vbrtag(const uint8_t *frame, int frame_size, uint32_t *f
/ MPEG2 & 2.5 9 17*/
bs_t bs[1];
L3_gr_info_t gr_info[4];
+ size_t frame_size_u;
+ size_t tag_off;
+ const uint8_t *tag;
+ int flags;
if (frame_size < (int)(HDR_SIZE + 8))
return 0;
bs_init(bs, frame + HDR_SIZE, frame_size - HDR_SIZE);
@@ -222,14 +228,14 @@ static int mp3dec_check_vbrtag(const uint8_t *frame, int frame_size, uint32_t *f
if (L3_read_side_info(bs, gr_info, frame) < 0)
return 0; /* side info corrupted */
- size_t frame_size_u = (size_t)frame_size;
- size_t tag_off = HDR_SIZE + bs->pos/8;
+ frame_size_u = (size_t)frame_size;
+ tag_off = HDR_SIZE + bs->pos/8;
if (tag_off > frame_size_u || frame_size_u - tag_off < 8)
return 0;
- const uint8_t *tag = frame + tag_off;
+ tag = frame + tag_off;
if (memcmp(g_xing_tag, tag, 4) && memcmp(g_info_tag, tag, 4))
return 0;
- int flags = tag[7];
+ flags = tag[7];
if (!((flags & FRAMES_FLAG)))
return -1;
tag_off += 8;
@@ -279,9 +285,11 @@ int mp3dec_detect_buf(const uint8_t *buf, size_t buf_size)
int mp3dec_detect_cb(mp3dec_io_t *io, uint8_t *buf, size_t buf_size)
{
+ int free_format_bytes, frame_size;
+ size_t filled;
if (!buf || (size_t)-1 == buf_size || (io && buf_size < MINIMP3_BUF_SIZE))
return MP3D_E_PARAM;
- size_t filled = buf_size;
+ filled = buf_size;
if (io)
{
if (io->seek(0, io->seek_data))
@@ -308,7 +316,6 @@ int mp3dec_detect_cb(mp3dec_io_t *io, uint8_t *buf, size_t buf_size)
if (filled > MINIMP3_BUF_SIZE)
filled = MINIMP3_BUF_SIZE;
}
- int free_format_bytes, frame_size;
mp3d_find_frame(buf, (int)filled, &free_format_bytes, &frame_size);
if (frame_size)
return 0; /* MAX_FRAME_SYNC_MATCHES consecutive frames found */
@@ -322,20 +329,26 @@ int mp3dec_load_buf(mp3dec_t *dec, const uint8_t *buf, size_t buf_size, mp3dec_f
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)
{
- if (!dec || !buf || !info || (size_t)-1 == buf_size || (io && buf_size < MINIMP3_BUF_SIZE))
- return MP3D_E_PARAM;
uint64_t detected_samples = 0;
size_t orig_buf_size = buf_size;
int to_skip = 0;
mp3dec_frame_info_t frame_info;
+ size_t filled = 0, consumed = 0;
+ int eof = 0, ret = 0;
+ int samples;
+ uint64_t samples_bytes;
+ size_t allocated;
+ size_t avg_bitrate_kbps = 0, frames = 0;
+
+ if (!dec || !buf || !info || (size_t)-1 == buf_size || (io && buf_size < MINIMP3_BUF_SIZE))
+ return MP3D_E_PARAM;
memset(info, 0, sizeof(*info));
memset(&frame_info, 0, sizeof(frame_info));
/* skip id3 */
- size_t filled = 0, consumed = 0;
- int eof = 0, ret = 0;
if (io)
{
+ size_t id3v2size;
if (io->seek(0, io->seek_data))
return MP3D_E_IOERROR;
filled = io->read(buf, MINIMP3_ID3_DETECT_SIZE, io->read_data);
@@ -343,7 +356,7 @@ int mp3dec_load_cb(mp3dec_t *dec, mp3dec_io_t *io, uint8_t *buf, size_t buf_size
return MP3D_E_IOERROR;
if (MINIMP3_ID3_DETECT_SIZE != filled)
return 0;
- size_t id3v2size = mp3dec_skip_id3v2(buf, filled);
+ id3v2size = mp3dec_skip_id3v2(buf, filled);
if (id3v2size)
{
if (io->seek(id3v2size, io->seek_data))
@@ -368,20 +381,20 @@ int mp3dec_load_cb(mp3dec_t *dec, mp3dec_io_t *io, uint8_t *buf, size_t buf_size
}
/* try to make allocation size assumption by first frame or vbr tag */
mp3dec_init(dec);
- int samples;
do
{
uint32_t frames;
- int i, delay, padding, free_format_bytes = 0, frame_size = 0;
+ int i, delay, padding, ret, free_format_bytes = 0, frame_size = 0;
const uint8_t *hdr;
if (io)
{
if (!eof && filled - consumed < MINIMP3_BUF_SIZE)
{ /* keep minimum 10 consecutive mp3 frames (~16KB) worst case */
+ size_t readed;
memmove(buf, buf + consumed, filled - consumed);
filled -= consumed;
consumed = 0;
- size_t readed = io->read(buf + filled, buf_size - filled, io->read_data);
+ readed = io->read(buf + filled, buf_size - filled, io->read_data);
if (readed > (buf_size - filled))
return MP3D_E_IOERROR;
if (readed != (buf_size - filled))
@@ -412,7 +425,7 @@ int mp3dec_load_cb(mp3dec_t *dec, mp3dec_io_t *io, uint8_t *buf, size_t buf_size
samples = hdr_frame_samples(hdr)*frame_info.channels;
if (3 != frame_info.layer)
break;
- int ret = mp3dec_check_vbrtag(hdr, frame_size, &frames, &delay, &padding);
+ ret = mp3dec_check_vbrtag(hdr, frame_size, &frames, &delay, &padding);
if (ret > 0)
{
padding *= frame_info.channels;
@@ -438,14 +451,13 @@ int mp3dec_load_cb(mp3dec_t *dec, mp3dec_io_t *io, uint8_t *buf, size_t buf_size
}
break;
} while(1);
- uint64_t samples_bytes;
if (detected_samples)
samples_bytes = detected_samples*sizeof(mp3d_sample_t);
else
samples_bytes = (uint64_t)(buf_size/frame_info.frame_bytes)*samples*sizeof(mp3d_sample_t);
if (samples_bytes > (uint64_t)((size_t)-1) - MINIMP3_MAX_SAMPLES_PER_FRAME*sizeof(mp3d_sample_t))
return MP3D_E_MEMORY; /* prevent size_t overflow from malicious VBR frame count (issue #140) */
- size_t allocated = MINIMP3_MAX_SAMPLES_PER_FRAME*sizeof(mp3d_sample_t) + (size_t)samples_bytes;
+ allocated = MINIMP3_MAX_SAMPLES_PER_FRAME*sizeof(mp3d_sample_t) + (size_t)samples_bytes;
info->buffer = (mp3d_sample_t*)MINIMP3_MALLOC(allocated);
if (!info->buffer)
return MP3D_E_MEMORY;
@@ -454,13 +466,13 @@ int mp3dec_load_cb(mp3dec_t *dec, mp3dec_io_t *io, uint8_t *buf, size_t buf_size
info->hz = frame_info.hz;
info->layer = frame_info.layer;
/* decode all frames */
- size_t avg_bitrate_kbps = 0, frames = 0;
do
{
if ((allocated - info->samples*sizeof(mp3d_sample_t)) < MINIMP3_MAX_SAMPLES_PER_FRAME*sizeof(mp3d_sample_t))
{
+ mp3d_sample_t *alloc_buf;
allocated *= 2;
- mp3d_sample_t *alloc_buf = (mp3d_sample_t*)MINIMP3_REALLOC(info->buffer, allocated);
+ alloc_buf = (mp3d_sample_t*)MINIMP3_REALLOC(info->buffer, allocated);
if (!alloc_buf)
return MP3D_E_MEMORY;
info->buffer = alloc_buf;
@@ -469,10 +481,11 @@ int mp3dec_load_cb(mp3dec_t *dec, mp3dec_io_t *io, uint8_t *buf, size_t buf_size
{
if (!eof && filled - consumed < MINIMP3_BUF_SIZE)
{ /* keep minimum 10 consecutive mp3 frames (~16KB) worst case */
+ size_t readed;
memmove(buf, buf + consumed, filled - consumed);
filled -= consumed;
consumed = 0;
- size_t readed = io->read(buf + filled, buf_size - filled, io->read_data);
+ readed = io->read(buf + filled, buf_size - filled, io->read_data);
if (readed != (buf_size - filled))
eof = 1;
filled += readed;
@@ -540,25 +553,26 @@ int mp3dec_load_cb(mp3dec_t *dec, mp3dec_io_t *io, uint8_t *buf, size_t buf_size
int mp3dec_iterate_buf(const uint8_t *buf, size_t buf_size, MP3D_ITERATE_CB callback, void *user_data)
{
const uint8_t *orig_buf = buf;
+ mp3dec_frame_info_t frame_info;
if (!buf || (size_t)-1 == buf_size || !callback)
return MP3D_E_PARAM;
/* skip id3 */
mp3dec_skip_id3(&buf, &buf_size);
if (!buf_size)
return 0;
- mp3dec_frame_info_t frame_info;
memset(&frame_info, 0, sizeof(frame_info));
do
{
int free_format_bytes = 0, frame_size = 0, ret;
int i = mp3d_find_frame(buf, (int)buf_size, &free_format_bytes, &frame_size);
+ const uint8_t *hdr;
buf += i;
buf_size -= i;
if (i && !frame_size)
continue;
if (!frame_size)
break;
- const uint8_t *hdr = buf;
+ 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);
@@ -578,18 +592,21 @@ int mp3dec_iterate_buf(const uint8_t *buf, size_t buf_size, MP3D_ITERATE_CB call
int mp3dec_iterate_cb(mp3dec_io_t *io, uint8_t *buf, size_t buf_size, MP3D_ITERATE_CB callback, void *user_data)
{
- if (!io || !buf || (size_t)-1 == buf_size || buf_size < MINIMP3_BUF_SIZE || !callback)
- return MP3D_E_PARAM;
- size_t filled = io->read(buf, MINIMP3_ID3_DETECT_SIZE, io->read_data), consumed = 0;
+ size_t filled, consumed = 0;
+ size_t id3v2size;
uint64_t readed = 0;
mp3dec_frame_info_t frame_info;
int eof = 0;
+
+ if (!io || !buf || (size_t)-1 == buf_size || buf_size < MINIMP3_BUF_SIZE || !callback)
+ return MP3D_E_PARAM;
+ filled = io->read(buf, MINIMP3_ID3_DETECT_SIZE, io->read_data), consumed = 0;
memset(&frame_info, 0, sizeof(frame_info));
if (filled > MINIMP3_ID3_DETECT_SIZE)
return MP3D_E_IOERROR;
if (MINIMP3_ID3_DETECT_SIZE != filled)
return 0;
- size_t id3v2size = mp3dec_skip_id3v2(buf, filled);
+ id3v2size = mp3dec_skip_id3v2(buf, filled);
if (id3v2size)
{
if (io->seek(id3v2size, io->seek_data))
@@ -611,6 +628,7 @@ int mp3dec_iterate_cb(mp3dec_io_t *io, uint8_t *buf, size_t buf_size, MP3D_ITERA
{
int free_format_bytes = 0, frame_size = 0, ret;
int i = mp3d_find_frame(buf + consumed, (int)(filled - consumed), &free_format_bytes, &frame_size);
+ const uint8_t *hdr;
if (i && !frame_size)
{
consumed += i;
@@ -618,7 +636,7 @@ int mp3dec_iterate_cb(mp3dec_io_t *io, uint8_t *buf, size_t buf_size, MP3D_ITERA
}
if (!frame_size)
break;
- const uint8_t *hdr = buf + consumed + i;
+ hdr = buf + consumed + i;
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);
@@ -635,10 +653,11 @@ int mp3dec_iterate_cb(mp3dec_io_t *io, uint8_t *buf, size_t buf_size, MP3D_ITERA
consumed += i + frame_size;
if (!eof && filled - consumed < MINIMP3_BUF_SIZE)
{ /* keep minimum 10 consecutive mp3 frames (~16KB) worst case */
+ size_t readed;
memmove(buf, buf + consumed, filled - consumed);
filled -= consumed;
consumed = 0;
- size_t readed = io->read(buf + filled, buf_size - filled, io->read_data);
+ readed = io->read(buf + filled, buf_size - filled, io->read_data);
if (readed > (buf_size - filled))
return MP3D_E_IOERROR;
if (readed != (buf_size - filled))
@@ -688,11 +707,12 @@ static int mp3dec_load_index(void *user_data, const uint8_t *frame, int frame_si
return MP3D_E_USER;
if (dec->index.num_frames + 1 > dec->index.capacity)
{
+ mp3dec_frame_t *alloc_buf;
if (!dec->index.capacity)
dec->index.capacity = 4096;
else
dec->index.capacity *= 2;
- mp3dec_frame_t *alloc_buf = (mp3dec_frame_t *)MINIMP3_REALLOC((void*)dec->index.frames, sizeof(mp3dec_frame_t)*dec->index.capacity);
+ alloc_buf = (mp3dec_frame_t *)MINIMP3_REALLOC((void*)dec->index.frames, sizeof(mp3dec_frame_t)*dec->index.capacity);
if (!alloc_buf)
return MP3D_E_MEMORY;
dec->index.frames = alloc_buf;
@@ -712,6 +732,7 @@ static int mp3dec_load_index(void *user_data, const uint8_t *frame, int frame_si
int mp3dec_ex_open_buf(mp3dec_ex_t *dec, const uint8_t *buf, size_t buf_size, int flags)
{
+ int ret;
if (!dec || !buf || (size_t)-1 == buf_size || (flags & (~MP3D_FLAGS_MASK)))
return MP3D_E_PARAM;
memset(dec, 0, sizeof(*dec));
@@ -719,7 +740,7 @@ int mp3dec_ex_open_buf(mp3dec_ex_t *dec, const uint8_t *buf, size_t buf_size, in
dec->file.size = buf_size;
dec->flags = flags;
mp3dec_init(&dec->mp3d);
- int ret = mp3dec_iterate_buf(dec->file.buffer, dec->file.size, mp3dec_load_index, dec);
+ ret = mp3dec_iterate_buf(dec->file.buffer, dec->file.size, mp3dec_load_index, dec);
if (ret && MP3D_E_USER != ret)
return ret;
mp3dec_init(&dec->mp3d);
@@ -791,9 +812,10 @@ int mp3dec_ex_seek(mp3dec_ex_t *dec, uint64_t position)
dec->buffer_samples = 0;
if (dec->io)
{
+ int ret;
if (dec->io->seek(dec->start_offset, dec->io->seek_data))
return MP3D_E_IOERROR;
- int ret = mp3dec_iterate_cb(dec->io, (uint8_t *)dec->file.buffer, dec->file.size, mp3dec_load_index, dec);
+ ret = mp3dec_iterate_cb(dec->io, (uint8_t *)dec->file.buffer, dec->file.size, mp3dec_load_index, dec);
if (ret && MP3D_E_USER != ret)
return ret;
} else
@@ -836,10 +858,11 @@ int mp3dec_ex_seek(mp3dec_ex_t *dec, uint64_t position)
const uint8_t *hdr;
if (dec->io)
{
+ size_t readed;
hdr = dec->file.buffer;
if (dec->io->seek(dec->index.frames[i - 1].offset, dec->io->seek_data))
return MP3D_E_IOERROR;
- size_t readed = dec->io->read((uint8_t *)hdr, HDR_SIZE, dec->io->read_data);
+ readed = dec->io->read((uint8_t *)hdr, HDR_SIZE, dec->io->read_data);
if (readed != HDR_SIZE)
return MP3D_E_IOERROR;
frame_size = hdr_frame_bytes(hdr, dec->free_format_bytes) + hdr_padding(hdr);
@@ -873,17 +896,19 @@ int mp3dec_ex_seek(mp3dec_ex_t *dec, uint64_t position)
while ((i + 1) < dec->index.num_frames && !dec->index.frames[i].sample && !dec->index.frames[i + 1].sample)
{ /* skip not decodable first frames */
const uint8_t *hdr;
+ int frame_samples;
if (dec->io)
{
+ size_t readed;
hdr = dec->file.buffer;
if (dec->io->seek(dec->index.frames[i].offset, dec->io->seek_data))
return MP3D_E_IOERROR;
- size_t readed = dec->io->read((uint8_t *)hdr, HDR_SIZE, dec->io->read_data);
+ readed = dec->io->read((uint8_t *)hdr, HDR_SIZE, dec->io->read_data);
if (readed != HDR_SIZE)
return MP3D_E_IOERROR;
} else
hdr = dec->file.buffer + dec->index.frames[i].offset;
- int frame_samples = hdr_frame_samples(hdr)*dec->info.channels;
+ frame_samples = hdr_frame_samples(hdr)*dec->info.channels;
if (dec->to_skip > (INT_MAX - frame_samples))
dec->to_skip = INT_MAX;
else
@@ -907,6 +932,9 @@ int mp3dec_ex_seek(mp3dec_ex_t *dec, uint64_t position)
size_t mp3dec_ex_read_frame(mp3dec_ex_t *dec, mp3d_sample_t **buf, mp3dec_frame_info_t *frame_info, size_t max_samples)
{
+ uint64_t end_offset;
+ size_t out_samples;
+ int eof = 0;
if (!dec || !buf || !frame_info)
{
if (dec)
@@ -918,8 +946,7 @@ size_t mp3dec_ex_read_frame(mp3dec_ex_t *dec, mp3d_sample_t **buf, mp3dec_frame_
if (dec->last_error)
return 0; /* error eof state, seek can reset it */
*buf = NULL;
- uint64_t end_offset = dec->end_offset ? dec->end_offset : dec->file.size;
- int eof = 0;
+ end_offset = dec->end_offset ? dec->end_offset : dec->file.size;
while (dec->buffer_consumed == dec->buffer_samples)
{
const uint8_t *dec_buf;
@@ -927,10 +954,11 @@ size_t mp3dec_ex_read_frame(mp3dec_ex_t *dec, mp3d_sample_t **buf, mp3dec_frame_
{
if (!eof && (dec->input_filled - dec->input_consumed) < MINIMP3_BUF_SIZE)
{ /* keep minimum 10 consecutive mp3 frames (~16KB) worst case */
+ size_t readed;
memmove((uint8_t*)dec->file.buffer, (uint8_t*)dec->file.buffer + dec->input_consumed, dec->input_filled - dec->input_consumed);
dec->input_filled -= dec->input_consumed;
dec->input_consumed = 0;
- size_t readed = dec->io->read((uint8_t*)dec->file.buffer + dec->input_filled, dec->file.size - dec->input_filled, dec->io->read_data);
+ readed = dec->io->read((uint8_t*)dec->file.buffer + dec->input_filled, dec->file.size - dec->input_filled, dec->io->read_data);
if (readed > (dec->file.size - dec->input_filled))
{
dec->last_error = MP3D_E_IOERROR;
@@ -949,10 +977,11 @@ size_t mp3dec_ex_read_frame(mp3dec_ex_t *dec, mp3d_sample_t **buf, mp3dec_frame_
dec->input_consumed += frame_info->frame_bytes;
} else
{
+ uint64_t buf_size;
if (dec->offset >= end_offset)
return 0;
dec_buf = dec->file.buffer + dec->offset;
- uint64_t buf_size = end_offset - dec->offset;
+ buf_size = end_offset - dec->offset;
if (!buf_size)
return 0;
dec->buffer_samples = mp3dec_decode_frame(&dec->mp3d, dec_buf, (int)MINIMP3_MIN(buf_size, (uint64_t)INT_MAX), dec->buffer, frame_info);
@@ -989,7 +1018,7 @@ size_t mp3dec_ex_read_frame(mp3dec_ex_t *dec, mp3d_sample_t **buf, mp3dec_frame_
}
dec->offset += frame_info->frame_bytes;
}
- size_t out_samples = MINIMP3_MIN((size_t)(dec->buffer_samples - dec->buffer_consumed), max_samples);
+ out_samples = MINIMP3_MIN((size_t)(dec->buffer_samples - dec->buffer_consumed), max_samples);
if (dec->detected_samples)
{ /* count decoded samples to properly cut padding */
if (dec->cur_sample + out_samples >= dec->detected_samples)
@@ -1003,15 +1032,16 @@ size_t mp3dec_ex_read_frame(mp3dec_ex_t *dec, mp3d_sample_t **buf, mp3dec_frame_
size_t mp3dec_ex_read(mp3dec_ex_t *dec, mp3d_sample_t *buf, size_t samples)
{
+ mp3dec_frame_info_t frame_info;
+ size_t samples_requested;
if (!dec || !buf)
{
if (dec)
dec->last_error = MP3D_E_PARAM;
return 0;
}
- mp3dec_frame_info_t frame_info;
memset(&frame_info, 0, sizeof(frame_info));
- size_t samples_requested = samples;
+ samples_requested = samples;
while (samples)
{
mp3d_sample_t *buf_frame = NULL;
@@ -1029,11 +1059,11 @@ size_t mp3dec_ex_read(mp3dec_ex_t *dec, mp3d_sample_t *buf, size_t samples)
int mp3dec_ex_open_cb(mp3dec_ex_t *dec, mp3dec_io_t *io, int flags)
{
+ int ret;
if (!dec || !io || (flags & (~MP3D_FLAGS_MASK)))
return MP3D_E_PARAM;
memset(dec, 0, sizeof(*dec));
#ifdef MINIMP3_HAVE_RING
- int ret;
if (ret = mp3dec_open_ring(&dec->file, MINIMP3_IO_SIZE))
return ret;
#else
@@ -1047,7 +1077,7 @@ int mp3dec_ex_open_cb(mp3dec_ex_t *dec, mp3dec_io_t *io, int flags)
mp3dec_init(&dec->mp3d);
if (io->seek(0, io->seek_data))
return MP3D_E_IOERROR;
- int ret = mp3dec_iterate_cb(io, (uint8_t *)dec->file.buffer, dec->file.size, mp3dec_load_index, dec);
+ ret = mp3dec_iterate_cb(io, (uint8_t *)dec->file.buffer, dec->file.size, mp3dec_load_index, dec);
if (ret && MP3D_E_USER != ret)
return ret;
if (dec->io->seek(dec->start_offset, dec->io->seek_data))
@@ -1089,10 +1119,10 @@ static void mp3dec_close_file(mp3dec_map_info_t *map_info)
static int mp3dec_open_file(const char *file_name, mp3dec_map_info_t *map_info)
{
+ struct stat st;
+ int file;
if (!file_name)
return MP3D_E_PARAM;
- int file;
- struct stat st;
memset(map_info, 0, sizeof(*map_info));
retry_open:
file = open(file_name, O_RDONLY);
@@ -1239,10 +1269,11 @@ static void mp3dec_close_file(mp3dec_map_info_t *map_info)
static int mp3dec_open_file_h(HANDLE file, mp3dec_map_info_t *map_info)
{
- memset(map_info, 0, sizeof(*map_info));
-
HANDLE mapping = NULL;
LARGE_INTEGER s;
+
+ memset(map_info, 0, sizeof(*map_info));
+
if (!GetFileSizeEx(file, &s)) /* returns BOOL and fills all 64 bits of s, unlike GetFileSize */
goto error;
map_info->size = s.QuadPart;
@@ -1326,14 +1357,16 @@ static void mp3dec_close_file(mp3dec_map_info_t *map_info)
static int mp3dec_open_file(const char *file_name, mp3dec_map_info_t *map_info)
{
+ FILE *file;
+ long size = -1;
+ int res;
if (!file_name)
return MP3D_E_PARAM;
memset(map_info, 0, sizeof(*map_info));
- FILE *file = fopen(file_name, "rb");
+ file = fopen(file_name, "rb");
if (!file)
return MP3D_E_IOERROR;
- int res = MP3D_E_IOERROR;
- long size = -1;
+ res = MP3D_E_IOERROR;
if (fseek(file, 0, SEEK_END))
goto error;
size = ftell(file);