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

Add USE_MALLOC define, to toggle the use of malloc() and free() instead of VLA #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
25 changes: 25 additions & 0 deletions src/celt_lpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ void celt_fir(
int ord)
{
int i,j;
#ifdef USE_MALLOC
opus_val16 *rnum = malloc(sizeof(*rnum) * ord);
#else
opus_val16 rnum[ord];
#endif
for(i=0;i<ord;i++)
rnum[i] = num[ord-i-1];
for (i=0;i<N-3;i+=4)
Expand All @@ -119,6 +123,10 @@ void celt_fir(
sum = MAC16_16(sum,rnum[j],x[i+j-ord]);
y[i] = ROUND16(sum, SIG_SHIFT);
}

#ifdef USE_MALLOC
free(rnum);
#endif
}

void celt_iir(const opus_val32 *_x,
Expand Down Expand Up @@ -147,8 +155,13 @@ void celt_iir(const opus_val32 *_x,
#else
int i,j;
celt_assert((ord&3)==0);
#ifdef USE_MALLOC
opus_val16 *rden = malloc(sizeof(*rden) * ord);
opus_val16 *y = malloc(sizeof(*y) * (N+ord));
#else
opus_val16 rden[ord];
opus_val16 y[N+ord];
#endif
for(i=0;i<ord;i++)
rden[i] = den[ord-i-1];
for(i=0;i<ord;i++)
Expand Down Expand Up @@ -192,7 +205,11 @@ void celt_iir(const opus_val32 *_x,
}
for(i=0;i<ord;i++)
mem[i] = _y[N-i-1];
#ifdef USE_MALLOC
free(rden);
free(y) ;
#endif
#endif // SMALL_FOOTPRINT
}

int _celt_autocorr(
Expand All @@ -208,7 +225,11 @@ int _celt_autocorr(
int fastN=n-lag;
int shift;
const opus_val16 *xptr;
#ifdef USE_MALLOC
opus_val16 *xx = malloc(sizeof(*xx) * n);
#else
opus_val16 xx[n];
#endif
celt_assert(n>0);
celt_assert(overlap>=0);
if (overlap == 0)
Expand Down Expand Up @@ -275,5 +296,9 @@ int _celt_autocorr(
}
#endif

#ifdef USE_MALLOC
free(xx);
#endif

return shift;
}
30 changes: 30 additions & 0 deletions src/pitch.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
#include "celt_lpc.h"
#include "math.h"

#ifdef USE_MALLOC
#include <stdlib.h>
#endif

static void find_best_pitch(opus_val32 *xcorr, opus_val16 *y, int len,
int max_pitch, int *best_pitch
#ifdef FIXED_POINT
Expand Down Expand Up @@ -297,9 +301,15 @@ void pitch_search(const opus_val16 *x_lp, opus_val16 *y,
celt_assert(max_pitch>0);
lag = len+max_pitch;

#ifdef USE_MALLOC
opus_val16 *x_lp4 = malloc(sizeof(*x_lp4) * len>>2);
opus_val16 *y_lp4 = malloc(sizeof(*y_lp4) * lag>>2);
opus_val32 *xcorr = malloc(sizeof(*xcorr) * max_pitch>>1);
#else
opus_val16 x_lp4[len>>2];
opus_val16 y_lp4[lag>>2];
opus_val32 xcorr[max_pitch>>1];
#endif

/* Downsample by 2 again */
for (j=0;j<len>>2;j++)
Expand Down Expand Up @@ -382,6 +392,12 @@ void pitch_search(const opus_val16 *x_lp, opus_val16 *y,
offset = 0;
}
*pitch = 2*best_pitch[0]-offset;

#ifdef USE_MALLOC
free(x_lp4);
free(y_lp4);
free(xcorr);
#endif
}

#ifdef FIXED_POINT
Expand Down Expand Up @@ -427,7 +443,11 @@ opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod,
opus_val16 g, g0;
opus_val16 pg;
opus_val32 xy,xx,yy,xy2;
#ifdef USE_MALLOC
opus_val32 *xcorr = malloc(sizeof(*xcorr) * 3);
#else
opus_val32 xcorr[3];
#endif
opus_val32 best_xy, best_yy;
int offset;
int minperiod0;
Expand All @@ -443,7 +463,11 @@ opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod,
*T0_=maxperiod-1;

T = T0 = *T0_;
#ifdef USE_MALLOC
opus_val16 *yy_lookup = malloc(sizeof(*yy_lookup) * (maxperiod+1));
#else
opus_val32 yy_lookup[maxperiod+1];
#endif
dual_inner_prod(x, x, x-T0, N, &xx, &xy);
yy_lookup[0] = xx;
yy=xx;
Expand Down Expand Up @@ -522,5 +546,11 @@ opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod,

if (*T0_<minperiod0)
*T0_=minperiod0;

#ifdef USE_MALLOC
free(xcorr);
free(yy_lookup);
#endif

return pg;
}