Skip to content

Commit

Permalink
Add USE_MALLOC macro which enables the use of malloc() and free() ins…
Browse files Browse the repository at this point in the history
…tead of variable length arrays
  • Loading branch information
davidebeatrici committed Jun 30, 2018
1 parent 91ef401 commit b30f2bb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
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;
}

0 comments on commit b30f2bb

Please sign in to comment.