From f4d4e693325bc15286df2496a3ac47c0d0bac51d Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Wed, 24 Feb 2021 20:50:22 -0800 Subject: [PATCH] Move large OPUS locals to the heap (#374) --- src/libopus/celt/celt_decoder.c | 3 +- src/libopus/config.h | 2 + src/libopus/repacketizer.c | 71 +++++++++++++------ src/libopus/silk/NLSF2A.c | 11 ++- src/libopus/silk/fixed/burg_modified_FIX.c | 18 +++-- .../silk/fixed/warped_autocorrelation_FIX.c | 6 +- src/libopus/silk/resampler_down2_3.c | 4 +- src/opusfile/opusfile.c | 67 ++++++++++------- tests/host/Makefile | 4 +- 9 files changed, 122 insertions(+), 64 deletions(-) diff --git a/src/libopus/celt/celt_decoder.c b/src/libopus/celt/celt_decoder.c index 329b6f6..7e7296e 100644 --- a/src/libopus/celt/celt_decoder.c +++ b/src/libopus/celt/celt_decoder.c @@ -482,7 +482,7 @@ static int celt_plc_pitch_search(celt_sig *decode_mem[2], int C, int arch) int pitch_index; VARDECL( opus_val16, lp_pitch_buf ); SAVE_STACK; - ALLOC( lp_pitch_buf, DECODE_BUFFER_SIZE>>1, opus_val16 ); + opus_val16 *lp_pitch_buf = (opus_val16*)malloc((DECODE_BUFFER_SIZE>>1) * sizeof(opus_val16)); //ALLOC( lp_pitch_buf, DECODE_BUFFER_SIZE>>1, opus_val16 ); pitch_downsample(decode_mem, lp_pitch_buf, DECODE_BUFFER_SIZE, C, arch); pitch_search(lp_pitch_buf+(PLC_PITCH_LAG_MAX>>1), lp_pitch_buf, @@ -490,6 +490,7 @@ static int celt_plc_pitch_search(celt_sig *decode_mem[2], int C, int arch) PLC_PITCH_LAG_MAX-PLC_PITCH_LAG_MIN, &pitch_index, arch); pitch_index = PLC_PITCH_LAG_MAX-pitch_index; RESTORE_STACK; + free(lp_pitch_buf); return pitch_index; } diff --git a/src/libopus/config.h b/src/libopus/config.h index 3577204..dc1ee1e 100644 --- a/src/libopus/config.h +++ b/src/libopus/config.h @@ -206,3 +206,5 @@ # define _Restrict # define __restrict__ #endif + +#include diff --git a/src/libopus/repacketizer.c b/src/libopus/repacketizer.c index 5a1eb67..3673229 100644 --- a/src/libopus/repacketizer.c +++ b/src/libopus/repacketizer.c @@ -239,21 +239,30 @@ opus_int32 opus_repacketizer_out(OpusRepacketizer *rp, unsigned char *data, opus int opus_packet_pad(unsigned char *data, opus_int32 len, opus_int32 new_len) { - OpusRepacketizer rp; + OpusRepacketizer *rp = (OpusRepacketizer*)malloc(sizeof(OpusRepacketizer)); opus_int32 ret; - if (len < 1) + if (len < 1) { + free(rp); return OPUS_BAD_ARG; - if (len==new_len) + } + if (len==new_len) { + free(rp); return OPUS_OK; - else if (len > new_len) + } + else if (len > new_len) { + free(rp); return OPUS_BAD_ARG; - opus_repacketizer_init(&rp); + } + opus_repacketizer_init(rp); /* Moving payload to the end of the packet so we can do in-place padding */ OPUS_MOVE(data+new_len-len, data, len); - ret = opus_repacketizer_cat(&rp, data+new_len-len, len); - if (ret != OPUS_OK) + ret = opus_repacketizer_cat(rp, data+new_len-len, len); + if (ret != OPUS_OK) { + free(rp); return ret; - ret = opus_repacketizer_out_range_impl(&rp, 0, rp.nb_frames, data, new_len, 0, 1); + } + ret = opus_repacketizer_out_range_impl(rp, 0, rp->nb_frames, data, new_len, 0, 1); + free(rp); if (ret > 0) return OPUS_OK; else @@ -262,15 +271,20 @@ int opus_packet_pad(unsigned char *data, opus_int32 len, opus_int32 new_len) opus_int32 opus_packet_unpad(unsigned char *data, opus_int32 len) { - OpusRepacketizer rp; + OpusRepacketizer *rp = (OpusRepacketizer*)malloc(sizeof(OpusRepacketizer)); opus_int32 ret; - if (len < 1) + if (len < 1) { + free(rp); return OPUS_BAD_ARG; - opus_repacketizer_init(&rp); - ret = opus_repacketizer_cat(&rp, data, len); - if (ret < 0) + } + opus_repacketizer_init(rp); + ret = opus_repacketizer_cat(rp, data, len); + if (ret < 0) { + free(rp); return ret; - ret = opus_repacketizer_out_range_impl(&rp, 0, rp.nb_frames, data, len, 0, 0); + } + ret = opus_repacketizer_out_range_impl(rp, 0, rp->nb_frames, data, len, 0, 0); + free(rp); celt_assert(ret > 0 && ret <= len); return ret; } @@ -312,12 +326,14 @@ opus_int32 opus_multistream_packet_unpad(unsigned char *data, opus_int32 len, in unsigned char toc; opus_int16 size[48]; opus_int32 packet_offset; - OpusRepacketizer rp; + OpusRepacketizer *rp = (OpusRepacketizer*)malloc(sizeof(OpusRepacketizer)); unsigned char *dst; opus_int32 dst_len; - if (len < 1) + if (len < 1){ + free(rp); return OPUS_BAD_ARG; + } dst = data; dst_len = 0; /* Unpad all frames */ @@ -325,25 +341,34 @@ opus_int32 opus_multistream_packet_unpad(unsigned char *data, opus_int32 len, in { opus_int32 ret; int self_delimited = s!=nb_streams-1; - if (len<=0) + if (len<=0) { + free(rp); return OPUS_INVALID_PACKET; - opus_repacketizer_init(&rp); + } + opus_repacketizer_init(rp); ret = opus_packet_parse_impl(data, len, self_delimited, &toc, NULL, size, NULL, &packet_offset); - if (ret<0) + if (ret<0) { + free(rp); return ret; - ret = opus_repacketizer_cat_impl(&rp, data, packet_offset, self_delimited); - if (ret < 0) + } + ret = opus_repacketizer_cat_impl(rp, data, packet_offset, self_delimited); + if (ret < 0) { + free(rp); return ret; - ret = opus_repacketizer_out_range_impl(&rp, 0, rp.nb_frames, dst, len, self_delimited, 0); - if (ret < 0) + } + ret = opus_repacketizer_out_range_impl(rp, 0, rp->nb_frames, dst, len, self_delimited, 0); + if (ret < 0) { + free(rp); return ret; + } else dst_len += ret; dst += ret; data += packet_offset; len -= packet_offset; } + free(rp); return dst_len; } diff --git a/src/libopus/silk/NLSF2A.c b/src/libopus/silk/NLSF2A.c index 40718e7..2b3e334 100644 --- a/src/libopus/silk/NLSF2A.c +++ b/src/libopus/silk/NLSF2A.c @@ -80,10 +80,11 @@ void silk_NLSF2A( }; const unsigned char *ordering; opus_int k, i, dd; - opus_int32 cos_LSF_QA[ SILK_MAX_ORDER_LPC ]; - opus_int32 P[ SILK_MAX_ORDER_LPC / 2 + 1 ], Q[ SILK_MAX_ORDER_LPC / 2 + 1 ]; + opus_int32 *cos_LSF_QA = (opus_int32*)malloc(sizeof(opus_int32) * SILK_MAX_ORDER_LPC ); + opus_int32 *P = (opus_int32*)malloc(sizeof(opus_int32) * (SILK_MAX_ORDER_LPC / 2 + 1)); + opus_int32 *Q= (opus_int32*)malloc(sizeof(opus_int32) * (SILK_MAX_ORDER_LPC / 2 + 1)); opus_int32 Ptmp, Qtmp, f_int, f_frac, cos_val, delta; - opus_int32 a32_QA1[ SILK_MAX_ORDER_LPC ]; + opus_int32 *a32_QA1 = (opus_int32*)malloc(sizeof(opus_int32) * SILK_MAX_ORDER_LPC ); silk_assert( LSF_COS_TAB_SZ_FIX == 128 ); celt_assert( d==10 || d==16 ); @@ -137,5 +138,9 @@ void silk_NLSF2A( a_Q12[ k ] = (opus_int16)silk_RSHIFT_ROUND( a32_QA1[ k ], QA + 1 - 12 ); /* QA+1 -> Q12 */ } } + free(cos_LSF_QA); + free(P); + free(Q); + free(a32_QA1); } diff --git a/src/libopus/silk/fixed/burg_modified_FIX.c b/src/libopus/silk/fixed/burg_modified_FIX.c index b4a31d6..dd78629 100644 --- a/src/libopus/silk/fixed/burg_modified_FIX.c +++ b/src/libopus/silk/fixed/burg_modified_FIX.c @@ -57,12 +57,12 @@ void silk_burg_modified_c( opus_int k, n, s, lz, rshifts, reached_max_gain; opus_int32 C0, num, nrg, rc_Q31, invGain_Q30, Atmp_QA, Atmp1, tmp1, tmp2, x1, x2; const opus_int16 *x_ptr; - opus_int32 C_first_row[ SILK_MAX_ORDER_LPC ]; - opus_int32 C_last_row[ SILK_MAX_ORDER_LPC ]; - opus_int32 Af_QA[ SILK_MAX_ORDER_LPC ]; - opus_int32 CAf[ SILK_MAX_ORDER_LPC + 1 ]; - opus_int32 CAb[ SILK_MAX_ORDER_LPC + 1 ]; - opus_int32 xcorr[ SILK_MAX_ORDER_LPC ]; + opus_int32 *C_first_row = (opus_int32*)malloc(sizeof(opus_int32) * SILK_MAX_ORDER_LPC); + opus_int32 *C_last_row = (opus_int32*)malloc(sizeof(opus_int32) * SILK_MAX_ORDER_LPC); + opus_int32 *Af_QA = (opus_int32*)malloc(sizeof(opus_int32) * SILK_MAX_ORDER_LPC); + opus_int32 *CAf = (opus_int32*)malloc(sizeof(opus_int32) * (SILK_MAX_ORDER_LPC+1)); + opus_int32 *CAb = (opus_int32*)malloc(sizeof(opus_int32) * (SILK_MAX_ORDER_LPC+1)); + opus_int32 *xcorr = (opus_int32*)malloc(sizeof(opus_int32) * SILK_MAX_ORDER_LPC); opus_int64 C0_64; celt_assert( subfr_length * nb_subfr <= MAX_FRAME_SIZE ); @@ -277,4 +277,10 @@ void silk_burg_modified_c( *res_nrg = silk_SMLAWW( nrg, silk_SMMUL( SILK_FIX_CONST( FIND_LPC_COND_FAC, 32 ), C0 ), -tmp1 );/* Q( -rshifts ) */ *res_nrg_Q = -rshifts; } + free(C_first_row); + free(C_last_row); + free(Af_QA); + free(CAf); + free(CAb); + free(xcorr); } diff --git a/src/libopus/silk/fixed/warped_autocorrelation_FIX.c b/src/libopus/silk/fixed/warped_autocorrelation_FIX.c index 7ef3a7e..9c21f2a 100644 --- a/src/libopus/silk/fixed/warped_autocorrelation_FIX.c +++ b/src/libopus/silk/fixed/warped_autocorrelation_FIX.c @@ -49,8 +49,8 @@ void silk_warped_autocorrelation_FIX_c( { opus_int n, i, lsh; opus_int32 tmp1_QS, tmp2_QS; - opus_int32 state_QS[ MAX_SHAPE_LPC_ORDER + 1 ] = { 0 }; - opus_int64 corr_QC[ MAX_SHAPE_LPC_ORDER + 1 ] = { 0 }; + opus_int32 *state_QS = (opus_int32*)calloc(MAX_SHAPE_LPC_ORDER + 1, sizeof(opus_int32)); + opus_int64 *corr_QC = (opus_int64*)calloc(MAX_SHAPE_LPC_ORDER + 1, sizeof(opus_int64)); /* Order must be even */ celt_assert( ( order & 1 ) == 0 ); @@ -88,5 +88,7 @@ void silk_warped_autocorrelation_FIX_c( } } silk_assert( corr_QC[ 0 ] >= 0 ); /* If breaking, decrease QC*/ + free(state_QS); + free(corr_QC); } #endif /* OVERRIDE_silk_warped_autocorrelation_FIX_c */ diff --git a/src/libopus/silk/resampler_down2_3.c b/src/libopus/silk/resampler_down2_3.c index d8ce95c..62aab56 100644 --- a/src/libopus/silk/resampler_down2_3.c +++ b/src/libopus/silk/resampler_down2_3.c @@ -48,7 +48,8 @@ void silk_resampler_down2_3( opus_int32 *buf_ptr; SAVE_STACK; - ALLOC( buf, RESAMPLER_MAX_BATCH_SIZE_IN + ORDER_FIR, opus_int32 ); +// ALLOC( buf, RESAMPLER_MAX_BATCH_SIZE_IN + ORDER_FIR, opus_int32 ); + opus_int32 *buf = (opus_int32*)malloc((RESAMPLER_MAX_BATCH_SIZE_IN + ORDER_FIR) * sizeof(opus_int32)); /* Copy buffered samples to start of buffer */ silk_memcpy( buf, S, ORDER_FIR * sizeof( opus_int32 ) ); @@ -99,5 +100,6 @@ void silk_resampler_down2_3( /* Copy last part of filtered signal to the state for the next call */ silk_memcpy( S, &buf[ nSamplesIn ], ORDER_FIR * sizeof( opus_int32 ) ); + free(buf); RESTORE_STACK; } diff --git a/src/opusfile/opusfile.c b/src/opusfile/opusfile.c index bfd3c9e..7ffe32f 100644 --- a/src/opusfile/opusfile.c +++ b/src/opusfile/opusfile.c @@ -90,12 +90,12 @@ int op_test(OpusHead *_head, ogg_sync_init(&oy); data=ogg_sync_buffer(&oy,(long)_initial_bytes); if(data!=NULL){ - ogg_stream_state os; + ogg_stream_state *os = (ogg_stream_state*)malloc(sizeof(ogg_stream_state)); ogg_page og; int ret; memcpy(data,_initial_data,_initial_bytes); ogg_sync_wrote(&oy,(long)_initial_bytes); - ogg_stream_init(&os,-1); + ogg_stream_init(os,-1); err=OP_FALSE; do{ ogg_packet op; @@ -104,11 +104,11 @@ int op_test(OpusHead *_head, if(ret<0)continue; /*Stop if we run out of data.*/ if(!ret)break; - ogg_stream_reset_serialno(&os,ogg_page_serialno(&og)); - ogg_stream_pagein(&os,&og); + ogg_stream_reset_serialno(os,ogg_page_serialno(&og)); + ogg_stream_pagein(os,&og); /*Only process the first packet on this page (if it's a BOS packet, it's required to be the only one).*/ - if(ogg_stream_packetout(&os,&op)==1){ + if(ogg_stream_packetout(os,&op)==1){ if(op.b_o_s){ ret=opus_head_parse(_head,op.packet,op.bytes); /*If this didn't look like Opus, keep going.*/ @@ -122,7 +122,8 @@ int op_test(OpusHead *_head, } } while(err==OP_FALSE); - ogg_stream_clear(&os); + ogg_stream_clear(os); + free(os); } else err=OP_EFAULT; ogg_sync_clear(&oy); @@ -835,7 +836,7 @@ static int op_find_initial_pcm_offset(OggOpusFile *_of, ogg_int64_t cur_page_gp; ogg_uint32_t serialno; opus_int32 total_duration; - int durations[255]; + int *durations = (int*)malloc(255 * sizeof(int)); int cur_page_eos; int op_count; int pi; @@ -852,26 +853,31 @@ static int op_find_initial_pcm_offset(OggOpusFile *_of, Otherwise there are no audio data packets in the whole logical stream.*/ if(OP_UNLIKELY(page_offset<0)){ /*Fail if there was a read error.*/ - if(page_offsethead.pre_skip>0)return OP_EBADTIMESTAMP; + if(_link->head.pre_skip>0) {free(durations); return OP_EBADTIMESTAMP;} _link->pcm_file_offset=0; /*Set pcm_end and end_offset so we can skip the call to op_find_final_pcm_offset().*/ _link->pcm_start=_link->pcm_end=0; _link->end_offset=_link->data_offset; + free(durations); return 0; } /*Similarly, if we hit the next link in the chain, we've gone too far.*/ if(OP_UNLIKELY(ogg_page_bos(_og))){ - if(_link->head.pre_skip>0)return OP_EBADTIMESTAMP; + if(_link->head.pre_skip>0) { + free(durations); + return OP_EBADTIMESTAMP; + } /*Set pcm_end and end_offset so we can skip the call to op_find_final_pcm_offset().*/ _link->pcm_file_offset=0; _link->pcm_start=_link->pcm_end=0; _link->end_offset=_link->data_offset; /*Tell the caller we've got a buffered page for them.*/ + free(durations); return 1; } /*Ignore pages from other streams (not strictly necessary, because of the @@ -901,7 +907,10 @@ static int op_find_initial_pcm_offset(OggOpusFile *_of, cur_page_gp=_of->op[op_count-1].granulepos; /*But getting a packet without a valid granule position on the page is not okay.*/ - if(cur_page_gp==-1)return OP_EBADTIMESTAMP; + if(cur_page_gp==-1) { + free(durations); + return OP_EBADTIMESTAMP; + } cur_page_eos=_of->op[op_count-1].e_o_s; if(OP_LIKELY(!cur_page_eos)){ /*The EOS flag wasn't set. @@ -910,6 +919,7 @@ static int op_find_initial_pcm_offset(OggOpusFile *_of, if(OP_UNLIKELY(op_granpos_add(&pcm_start,cur_page_gp,-total_duration)<0)){ /*The starting granule position MUST not be smaller than the amount of audio on the first page with completed packets.*/ + free(durations); return OP_EBADTIMESTAMP; } } @@ -923,6 +933,7 @@ static int op_find_initial_pcm_offset(OggOpusFile *_of, /*However, the end-trimming MUST not ask us to trim more samples than exist after applying the pre-skip.*/ if(OP_UNLIKELY(op_granpos_cmp(cur_page_gp,_link->head.pre_skip)<0)){ + free(durations); return OP_EBADTIMESTAMP; } } @@ -957,6 +968,7 @@ static int op_find_initial_pcm_offset(OggOpusFile *_of, _link->pcm_file_offset=0; _of->prev_packet_gp=_link->pcm_start=pcm_start; _of->prev_page_offset=page_offset; + free(durations); return 0; } @@ -1391,32 +1403,34 @@ static int op_open_seekable2_impl(OggOpusFile *_of){ /*64 seek records should be enough for anybody. Actually, with a bisection search in a 63-bit range down to OP_CHUNK_SIZE granularity, much more than enough.*/ - OpusSeekRecord sr[64]; + OpusSeekRecord *sr = (OpusSeekRecord*)malloc(64 * sizeof(OpusSeekRecord)); opus_int64 data_offset; int ret; /*We can seek, so set out learning all about this file.*/ (*_of->callbacks.seek)(_of->stream,0,SEEK_END); _of->offset=_of->end=(*_of->callbacks.tell)(_of->stream); - if(OP_UNLIKELY(_of->end<0))return OP_EREAD; + if(OP_UNLIKELY(_of->end<0)){free(sr); return OP_EREAD;} data_offset=_of->links[0].data_offset; - if(OP_UNLIKELY(_of->endendend, _of->links[0].serialno,_of->serialnos,_of->nserialnos); - if(OP_UNLIKELY(ret<0))return ret; + if(OP_UNLIKELY(ret<0)){free(sr); return ret;} /*If there's any trailing junk, forget about it.*/ _of->end=sr[0].offset+sr[0].size; - if(OP_UNLIKELY(_of->endendserialnos,&_of->nserialnos,&_of->cserialnos); + free(sr); + return ret; } static int op_open_seekable2(OggOpusFile *_of){ ogg_sync_state oy_start; - ogg_stream_state os_start; + ogg_stream_state *os_start = (ogg_stream_state*)malloc(sizeof(ogg_stream_state)); ogg_packet *op_start; opus_int64 prev_page_offset; opus_int64 start_offset; @@ -1435,9 +1449,9 @@ static int op_open_seekable2(OggOpusFile *_of){ start_op_count=_of->op_count; /*This is a bit too large to put on the stack unconditionally.*/ op_start=(ogg_packet *)_ogg_malloc(sizeof(*op_start)*start_op_count); - if(op_start==NULL)return OP_EFAULT; + if(op_start==NULL){free(os_start); return OP_EFAULT;} *&oy_start=_of->oy; - *&os_start=_of->os; + *os_start=_of->os; prev_page_offset=_of->prev_page_offset; start_offset=_of->offset; memcpy(op_start,_of->op,sizeof(*op_start)*start_op_count); @@ -1449,7 +1463,7 @@ static int op_open_seekable2(OggOpusFile *_of){ ogg_stream_clear(&_of->os); ogg_sync_clear(&_of->oy); *&_of->oy=*&oy_start; - *&_of->os=*&os_start; + *&_of->os=*os_start; _of->offset=start_offset; _of->op_count=start_op_count; memcpy(_of->op,op_start,sizeof(*_of->op)*start_op_count); @@ -1457,9 +1471,10 @@ static int op_open_seekable2(OggOpusFile *_of){ _of->prev_packet_gp=_of->links[0].pcm_start; _of->prev_page_offset=prev_page_offset; _of->cur_discard_count=_of->links[0].head.pre_skip; - if(OP_UNLIKELY(ret<0))return ret; + if(OP_UNLIKELY(ret<0)){free(os_start); return ret;} /*And restore the position indicator.*/ ret=(*_of->callbacks.seek)(_of->stream,op_position(_of),SEEK_SET); + free(os_start); return OP_UNLIKELY(ret<0)?OP_EREAD:0; } @@ -1980,7 +1995,7 @@ static int op_fetch_and_process_page(OggOpusFile *_of, ogg_stream_pagein(&_of->os,&og); if(OP_LIKELY(_of->ready_state>=OP_INITSET)){ opus_int32 total_duration; - int durations[255]; + int *durations = (int*)malloc(255 * sizeof(int)); int op_count; int report_hole; report_hole=0; @@ -2037,7 +2052,7 @@ static int op_fetch_and_process_page(OggOpusFile *_of, Proceed to the next link, rather than risk playing back some samples that shouldn't have been played.*/ _of->op_count=0; - if(report_hole)return OP_HOLE; + if(report_hole){ free(durations); return OP_HOLE; } continue; } /*By default discard 80 ms of data after a seek, unless we seek @@ -2145,9 +2160,9 @@ static int op_fetch_and_process_page(OggOpusFile *_of, _of->prev_page_offset=_page_offset; _of->op_count=op_count=pi; } - if(report_hole)return OP_HOLE; + if(report_hole) { free(durations); return OP_HOLE; } /*If end-trimming didn't trim all the packets, we're done.*/ - if(op_count>0)return 0; + if(op_count>0) { free(durations); return 0; } } } } diff --git a/tests/host/Makefile b/tests/host/Makefile index 835724f..25156db 100644 --- a/tests/host/Makefile +++ b/tests/host/Makefile @@ -78,8 +78,8 @@ libflac=../../src/libflac/md5.c ../../src/libflac/window.c ../../src/libflac/mem ../../src/libflac/bitreader.c ../../src/libflac/bitmath.c ../../src/libflac/stream_decoder.c ../../src/libflac/float.c -CCOPTS=-g -Wunused-parameter -Wall -m32 -include Arduino.h -CPPOPTS=-g -Wunused-parameter -Wall -std=c++11 -m32 -include Arduino.h +CCOPTS=-g -Wunused-parameter -Wall -m32 -include Arduino.h -Wstack-usage=300 +CPPOPTS=-g -Wunused-parameter -Wall -std=c++11 -m32 -Wstack-usage=300 -include Arduino.h .phony: all