Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce stack usage to help usage on embedded systems #117

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions minimp3.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@ typedef struct
int frame_bytes, frame_offset, channels, hz, layer, bitrate_kbps;
} mp3dec_frame_info_t;

typedef struct
{
float mdct_overlap[2][9*32], qmf_state[15*2*32];
int reserv, free_format_bytes;
unsigned char header[4], reserv_buf[511];
} mp3dec_t;
struct mp3dec_s;
typedef struct mp3dec_s mp3dec_t;

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -238,6 +234,19 @@ typedef struct
uint8_t ist_pos[2][39];
} mp3dec_scratch_t;

struct mp3dec_s
{
float mdct_overlap[2][9*32], qmf_state[15*2*32];
int reserv, free_format_bytes;
unsigned char header[4], reserv_buf[511];

mp3dec_scratch_t scratch;

#if !defined(MINIMP3_ONLY_MP3)
L12_scale_info sci[1];
#endif
};

static void bs_init(bs_t *bs, const uint8_t *data, int bytes)
{
bs->buf = data;
Expand Down Expand Up @@ -1715,7 +1724,6 @@ int mp3dec_decode_frame(mp3dec_t *dec, const uint8_t *mp3, int mp3_bytes, mp3d_s
int i = 0, igr, frame_size = 0, success = 1;
const uint8_t *hdr;
bs_t bs_frame[1];
mp3dec_scratch_t scratch;

if (mp3_bytes > 4 && dec->header[0] == 0xff && hdr_compare(dec->header, mp3))
{
Expand Down Expand Up @@ -1758,40 +1766,39 @@ int mp3dec_decode_frame(mp3dec_t *dec, const uint8_t *mp3, int mp3_bytes, mp3d_s

if (info->layer == 3)
{
int main_data_begin = L3_read_side_info(bs_frame, scratch.gr_info, hdr);
int main_data_begin = L3_read_side_info(bs_frame, dec->scratch.gr_info, hdr);
if (main_data_begin < 0 || bs_frame->pos > bs_frame->limit)
{
mp3dec_init(dec);
return 0;
}
success = L3_restore_reservoir(dec, bs_frame, &scratch, main_data_begin);
success = L3_restore_reservoir(dec, bs_frame, &dec->scratch, main_data_begin);
if (success)
{
for (igr = 0; igr < (HDR_TEST_MPEG1(hdr) ? 2 : 1); igr++, pcm += 576*info->channels)
{
memset(scratch.grbuf[0], 0, 576*2*sizeof(float));
L3_decode(dec, &scratch, scratch.gr_info + igr*info->channels, info->channels);
mp3d_synth_granule(dec->qmf_state, scratch.grbuf[0], 18, info->channels, pcm, scratch.syn[0]);
memset(dec->scratch.grbuf[0], 0, 576*2*sizeof(float));
L3_decode(dec, &dec->scratch, dec->scratch.gr_info + igr*info->channels, info->channels);
mp3d_synth_granule(dec->qmf_state, dec->scratch.grbuf[0], 18, info->channels, pcm, dec->scratch.syn[0]);
}
}
L3_save_reservoir(dec, &scratch);
L3_save_reservoir(dec, &dec->scratch);
} else
{
#ifdef MINIMP3_ONLY_MP3
return 0;
#else /* MINIMP3_ONLY_MP3 */
L12_scale_info sci[1];
L12_read_scale_info(hdr, bs_frame, sci);
L12_read_scale_info(hdr, bs_frame, dec->sci);

memset(scratch.grbuf[0], 0, 576*2*sizeof(float));
memset(dec->scratch.grbuf[0], 0, 576*2*sizeof(float));
for (i = 0, igr = 0; igr < 3; igr++)
{
if (12 == (i += L12_dequantize_granule(scratch.grbuf[0] + i, bs_frame, sci, info->layer | 1)))
if (12 == (i += L12_dequantize_granule(dec->scratch.grbuf[0] + i, bs_frame, dec->sci, info->layer | 1)))
{
i = 0;
L12_apply_scf_384(sci, sci->scf + igr, scratch.grbuf[0]);
mp3d_synth_granule(dec->qmf_state, scratch.grbuf[0], 12, info->channels, pcm, scratch.syn[0]);
memset(scratch.grbuf[0], 0, 576*2*sizeof(float));
L12_apply_scf_384(dec->sci, dec->sci->scf + igr, dec->scratch.grbuf[0]);
mp3d_synth_granule(dec->qmf_state, dec->scratch.grbuf[0], 12, info->channels, pcm, dec->scratch.syn[0]);
memset(dec->scratch.grbuf[0], 0, 576*2*sizeof(float));
pcm += 384*info->channels;
}
if (bs_frame->pos > bs_frame->limit)
Expand Down