Skip to content

Commit

Permalink
Move large OPUS locals to the heap (earlephilhower#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
earlephilhower authored Feb 25, 2021
1 parent 714e8cf commit f4d4e69
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 64 deletions.
3 changes: 2 additions & 1 deletion src/libopus/celt/celt_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,14 +482,15 @@ 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,
DECODE_BUFFER_SIZE-PLC_PITCH_LAG_MAX,
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;
}

Expand Down
2 changes: 2 additions & 0 deletions src/libopus/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,5 @@
# define _Restrict
# define __restrict__
#endif

#include <stdlib.h>
71 changes: 48 additions & 23 deletions src/libopus/repacketizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -312,38 +326,49 @@ 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 */
for (s=0;s<nb_streams;s++)
{
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;
}

11 changes: 8 additions & 3 deletions src/libopus/silk/NLSF2A.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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);
}

18 changes: 12 additions & 6 deletions src/libopus/silk/fixed/burg_modified_FIX.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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);
}
6 changes: 4 additions & 2 deletions src/libopus/silk/fixed/warped_autocorrelation_FIX.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 */
4 changes: 3 additions & 1 deletion src/libopus/silk/resampler_down2_3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
Expand Down Expand Up @@ -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;
}
Loading

0 comments on commit f4d4e69

Please sign in to comment.