From 9635fcbcc2e40c097b5c6e837e85c79ed33adb61 Mon Sep 17 00:00:00 2001 From: Chris Morgan Date: Wed, 4 Oct 2023 20:26:23 -0400 Subject: [PATCH 1/2] minimp3.h - Move mp3dec_scratch_t from stack to mp3dec_s to reduce stack consumption by ~16k mp3dec_s can be placed in the memory type of choosing by the caller but in particular this helps embedded use where stacks are often fixed sized. --- minimp3.h | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/minimp3.h b/minimp3.h index 3220ae1..810285b 100644 --- a/minimp3.h +++ b/minimp3.h @@ -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" { @@ -238,6 +234,15 @@ 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; +}; + static void bs_init(bs_t *bs, const uint8_t *data, int bytes) { bs->buf = data; @@ -1715,7 +1720,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)) { @@ -1758,23 +1762,23 @@ 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 @@ -1783,15 +1787,15 @@ int mp3dec_decode_frame(mp3dec_t *dec, const uint8_t *mp3, int mp3_bytes, mp3d_s L12_scale_info sci[1]; L12_read_scale_info(hdr, bs_frame, 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, 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(sci, 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) From 9deb1dcd97b476cfaea30b986dad2617d8c50264 Mon Sep 17 00:00:00 2001 From: Chris Morgan Date: Wed, 4 Oct 2023 20:50:37 -0400 Subject: [PATCH 2/2] minimp3.h - Move L12_scale_info from stack to mp3dec_s to reduce stack consumption by ~900 bytes --- minimp3.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/minimp3.h b/minimp3.h index 810285b..5e3f4f1 100644 --- a/minimp3.h +++ b/minimp3.h @@ -241,6 +241,10 @@ struct mp3dec_s 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) @@ -1784,16 +1788,15 @@ int mp3dec_decode_frame(mp3dec_t *dec, const uint8_t *mp3, int mp3_bytes, mp3d_s #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(dec->scratch.grbuf[0], 0, 576*2*sizeof(float)); for (i = 0, igr = 0; igr < 3; igr++) { - if (12 == (i += L12_dequantize_granule(dec->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, dec->scratch.grbuf[0]); + 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;