From a85657bd2919f51e442c13d33c7e9f6b017f4627 Mon Sep 17 00:00:00 2001 From: Jean-Marc Valin Date: Wed, 20 Feb 2008 11:59:30 +1100 Subject: [PATCH] removed // comments and added stack_alloc.h (not used everywhere yet) to make the code more C89-friendly. --- libcelt/Makefile.am | 9 ++-- libcelt/arch.h | 2 +- libcelt/bands.c | 52 +++++++++++-------- libcelt/celt.c | 23 +++++---- libcelt/laplace.c | 4 +- libcelt/mdct.c | 4 ++ libcelt/modes.c | 9 ++-- libcelt/pitch.c | 15 +++--- libcelt/psy.c | 8 +-- libcelt/quant_bands.c | 29 +++++------ libcelt/quant_pitch.c | 4 +- libcelt/rate.c | 23 ++++----- libcelt/stack_alloc.h | 115 ++++++++++++++++++++++++++++++++++++++++++ libcelt/testcelt.c | 4 -- libcelt/vq.c | 30 +++++------ 15 files changed, 224 insertions(+), 107 deletions(-) create mode 100644 libcelt/stack_alloc.h diff --git a/libcelt/Makefile.am b/libcelt/Makefile.am index 5200dfcf..7ec1d3f0 100644 --- a/libcelt/Makefile.am +++ b/libcelt/Makefile.am @@ -23,11 +23,10 @@ libcelt_la_SOURCES = bands.c bitrdec.c bitree.c bitrenc.c celt.c cwrs.c \ libcelt_la_LDFLAGS = -version-info @CELT_LT_CURRENT@:@CELT_LT_REVISION@:@CELT_LT_AGE@ -noinst_HEADERS = arch.h bands.h bitrdec.h bitree.h bitrenc.h cwrs.h \ - ecintrin.h entcode.h entdec.h entenc.h kiss_fft.h \ - kiss_fftr.h _kiss_fft_guts.h laplace.h mdct.h \ - mfrngcod.h modes.h os_support.h pgain_table.h pitch.h psy.h \ - quant_bands.h quant_pitch.h rate.h vq.h +noinst_HEADERS = _kiss_fft_guts.h arch.h bands.h bitrdec.h bitree.h bitrenc.h \ + cwrs.h ecintrin.h entcode.h entdec.h entenc.h kiss_fft.h kiss_fftr.h laplace.h \ + mdct.h mfrngcod.h modes.h os_support.h pgain_table.h pitch.h psy.h \ + quant_bands.h quant_pitch.h rate.h stack_alloc.h vq.h noinst_PROGRAMS = testcelt testcelt_SOURCES = testcelt.c diff --git a/libcelt/arch.h b/libcelt/arch.h index 79958cb5..74640115 100644 --- a/libcelt/arch.h +++ b/libcelt/arch.h @@ -36,7 +36,7 @@ #define ARCH_H #include "celt_types.h" - +#include "stack_alloc.h" #define ABS(x) ((x) < 0 ? (-(x)) : (x)) /**< Absolute integer value. */ #define ABS16(x) ((x) < 0 ? (-(x)) : (x)) /**< Absolute 16-bit value. */ diff --git a/libcelt/bands.c b/libcelt/bands.c index 4a5df35a..cad1cc94 100644 --- a/libcelt/bands.c +++ b/libcelt/bands.c @@ -29,6 +29,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include #include "bands.h" #include "modes.h" @@ -81,10 +85,10 @@ void compute_band_energies(const CELTMode *m, float *X, float *bank) for (j=B*eBands[i];jnbEBands*m->nbChannels]; + VARDECL(float *tmpE); + ALLOC(tmpE, m->nbEBands*m->nbChannels, float); compute_band_energies(m, X, tmpE); normalise_bands(m, X, tmpE); } @@ -143,8 +148,9 @@ void compute_pitch_gain(const CELTMode *m, float *X, float *P, float *gains, flo int i, B; const int *eBands = m->eBands; const int *pBands = m->pBands; + VARDECL(float *w); B = m->nbMdctBlocks*m->nbChannels; - float w[B*eBands[m->nbEBands]]; + ALLOC(w, B*eBands[m->nbEBands], float); for (i=0;inbEBands;i++) { int j; @@ -165,9 +171,6 @@ void compute_pitch_gain(const CELTMode *m, float *X, float *P, float *gains, flo Sxx += X[j]*X[j]*w[j]; } gain = Sxy/(1e-10+Sxx); - //gain = Sxy/(2*(pbank[i+1]-pbank[i])); - //if (i<3) - //gain *= 1+.02*gain; if (gain > 1.f) gain = 1.f; if (gain < 0.0f) @@ -175,7 +178,7 @@ void compute_pitch_gain(const CELTMode *m, float *X, float *P, float *gains, flo /* We need to be a bit conservative, otherwise residual doesn't quantise well */ gain *= .9f; gains[i] = gain; - //printf ("%f ", 1-sqrt(1-gain*gain)); + /*printf ("%f ", 1-sqrt(1-gain*gain));*/ } /*if(rand()%10==0) { @@ -198,7 +201,7 @@ void pitch_quant_bands(const CELTMode *m, float *X, float *P, float *gains) int j; for (j=B*pBands[i];jnbPBands];inbPBands+1];i++) P[i] = 0; @@ -210,11 +213,16 @@ void quant_bands(const CELTMode *m, float *X, float *P, float *W, int total_bits { int i, j, B, bits; const int *eBands = m->eBands; - B = m->nbMdctBlocks*m->nbChannels; - float norm[B*eBands[m->nbEBands+1]]; - int pulses[m->nbEBands]; - int offsets[m->nbEBands]; float alpha = .7; + VARDECL(float *norm); + VARDECL(int *pulses); + VARDECL(int *offsets); + + B = m->nbMdctBlocks*m->nbChannels; + + ALLOC(norm, B*eBands[m->nbEBands+1], float); + ALLOC(pulses, m->nbEBands, int); + ALLOC(offsets, m->nbEBands, int); for (i=0;inbEBands;i++) offsets[i] = 0; @@ -232,7 +240,6 @@ void quant_bands(const CELTMode *m, float *X, float *P, float *W, int total_bits int q; float theta, n; q = pulses[i]; - //q = m->nbPulses[i]; n = sqrt(B*(eBands[i+1]-eBands[i])); theta = .007*(B*(eBands[i+1]-eBands[i]))/(.1f+q); @@ -258,10 +265,7 @@ void quant_bands(const CELTMode *m, float *X, float *P, float *W, int total_bits } for (j=B*eBands[i];jnbEBands];inbEBands+1];i++) X[i] = 0; } @@ -271,11 +275,16 @@ void unquant_bands(const CELTMode *m, float *X, float *P, int total_bits, ec_dec { int i, j, B, bits; const int *eBands = m->eBands; - B = m->nbMdctBlocks*m->nbChannels; - float norm[B*eBands[m->nbEBands+1]]; - int pulses[m->nbEBands]; - int offsets[m->nbEBands]; float alpha = .7; + VARDECL(float *norm); + VARDECL(int *pulses); + VARDECL(int *offsets); + + B = m->nbMdctBlocks*m->nbChannels; + + ALLOC(norm, B*eBands[m->nbEBands+1], float); + ALLOC(pulses, m->nbEBands, int); + ALLOC(offsets, m->nbEBands, int); for (i=0;inbEBands;i++) offsets[i] = 0; @@ -288,7 +297,6 @@ void unquant_bands(const CELTMode *m, float *X, float *P, int total_bits, ec_dec int q; float theta, n; q = pulses[i]; - //q = m->nbPulses[i]; n = sqrt(B*(eBands[i+1]-eBands[i])); theta = .007*(B*(eBands[i+1]-eBands[i]))/(.1f+q); diff --git a/libcelt/celt.c b/libcelt/celt.c index 8f8b0193..572f8569 100644 --- a/libcelt/celt.c +++ b/libcelt/celt.c @@ -45,6 +45,9 @@ #define MAX_PERIOD 1024 +#ifndef M_PI +#define M_PI 3.14159263 +#endif struct CELTEncoder { const CELTMode *mode; @@ -234,7 +237,7 @@ int celt_encode(CELTEncoder *st, celt_int16_t *pcm, unsigned char *compressed, i for (i=0;ioverlap;i++) st->in_mem[C*i+c] = in[C*(N*(B+1)-N4-st->overlap+i)+c]; } - //for (i=0;i<(B+1)*C*N;i++) printf ("%f(%d) ", in[i], i); printf ("\n"); + /*for (i=0;i<(B+1)*C*N;i++) printf ("%f(%d) ", in[i], i); printf ("\n");*/ /* Compute MDCTs */ curr_power = compute_mdcts(&st->mdct_lookup, st->window, in, X, N, B, C); @@ -264,7 +267,7 @@ int celt_encode(CELTEncoder *st, celt_int16_t *pcm, unsigned char *compressed, i /* Compute MDCTs of the pitch part */ pitch_power = compute_mdcts(&st->mdct_lookup, st->window, st->out_mem+pitch_index*C, P, N, B, C); - //printf ("%f %f\n", curr_power, pitch_power); + /*printf ("%f %f\n", curr_power, pitch_power);*/ /*int j; for (j=0;jmode, X, bandE); normalise_bands(st->mode, X, bandE); - //for (i=0;imode->nbEBands;i++)printf("%f ", bandE[i]);printf("\n"); - //for (i=0;imode->nbEBands;i++)printf("%f ", bandE[i]);printf("\n");*/ + /*for (i=0;imode, bandE, st->oldBandE, nbCompressedBytes*8/3, &st->enc); @@ -296,8 +299,8 @@ int celt_encode(CELTEncoder *st, celt_int16_t *pcm, unsigned char *compressed, i if (C==2) stereo_mix(st->mode, P, bandE, 1); /* Simulates intensity stereo */ - //for (i=30;imode, X, P, gains, bandE); @@ -316,7 +319,7 @@ int celt_encode(CELTEncoder *st, celt_int16_t *pcm, unsigned char *compressed, i pitch_quant_bands(st->mode, X, P, gains); - //for (i=0;ienc, 0) < nbCompressedBytes*8 - 7) celt_warning_int ("many unused bits: ", nbCompressedBytes*8-ec_enc_tell(&st->enc, 0)); - //printf ("%d\n", ec_enc_tell(&st->enc, 0)-8*nbCompressedBytes); + /*printf ("%d\n", ec_enc_tell(&st->enc, 0)-8*nbCompressedBytes);*/ /* Finishing the stream with a 0101... pattern so that the decoder can check is everything's right */ { int val = 0; @@ -377,7 +380,7 @@ int celt_encode(CELTEncoder *st, celt_int16_t *pcm, unsigned char *compressed, i celt_warning_int ("got too many bytes:", nbBytes); return CELT_INTERNAL_ERROR; } - //printf ("%d\n", *nbBytes); + /*printf ("%d\n", *nbBytes);*/ data = ec_byte_get_buffer(&st->buf); for (i=0;i #include "os_support.h" +#ifndef M_PI +#define M_PI 3.14159263 +#endif + void mdct_init(mdct_lookup *l,int N) { int i; diff --git a/libcelt/modes.c b/libcelt/modes.c index c50202d4..b4d0b8f1 100644 --- a/libcelt/modes.c +++ b/libcelt/modes.c @@ -91,14 +91,14 @@ static int *compute_ebands(int Fs, int frame_size, int *nbEBands) int i, res, min_width, lin, low, high; res = (Fs+frame_size)/(2*frame_size); min_width = MIN_BINS*res; - //printf ("min_width = %d\n", min_width); + /*printf ("min_width = %d\n", min_width);*/ /* Find where the linear part ends (i.e. where the spacing is more than min_width */ for (lin=0;lin= min_width) break; - //printf ("lin = %d (%d Hz)\n", lin, bark_freq[lin]); + /*printf ("lin = %d (%d Hz)\n", lin, bark_freq[lin]);*/ low = ((bark_freq[lin]/res)+(MIN_BINS-1))/MIN_BINS; high = BARK_BANDS-lin; *nbEBands = low+high; @@ -145,7 +145,7 @@ static void compute_pbands(CELTMode *mode, int res) for (j=0;jnbEBands;j++) if (mode->eBands[j] <= pBands[i] && mode->eBands[j+1] > pBands[i]) break; - //printf ("%d %d\n", i, j); + /*printf ("%d %d\n", i, j);*/ if (mode->eBands[j] != pBands[i]) { if (pBands[i]-mode->eBands[j] < mode->eBands[j+1]-pBands[i] && @@ -181,7 +181,6 @@ static void compute_allocation_table(CELTMode *mode, int res) int num, den; num = band_allocation[i*BARK_BANDS+j] * (edge-bark_freq[j]); den = bark_freq[j+1]-bark_freq[j]; - //low = band_allocation[i*BARK_BANDS+j] * (edge-bark_freq[j])/(bark_freq[j+1]-bark_freq[j]); low = (num+den/2)/den; allocVectors[i*mode->nbEBands+eband] += low; eband++; @@ -250,7 +249,7 @@ CELTMode *celt_mode_create(int Fs, int channels, int frame_size, int lookahead, compute_allocation_table(mode, res); compute_alloc_cache(mode); - //printf ("%d bands\n", mode->nbEBands); + /*printf ("%d bands\n", mode->nbEBands);*/ return mode; } diff --git a/libcelt/pitch.c b/libcelt/pitch.c index 206f1580..6f0e245c 100644 --- a/libcelt/pitch.c +++ b/libcelt/pitch.c @@ -56,10 +56,10 @@ void find_spectral_pitch(kiss_fftr_cfg fft, struct PsyDecay *decay, float *x, fl for (i=1;i max_corr) { *pitch = i; max_corr = xx[i]; } } - //printf ("\n"); - //printf ("%d %f\n", *pitch, max_corr); - //printf ("%d\n", *pitch); + /*printf ("\n"); + printf ("%d %f\n", *pitch, max_corr); + printf ("%d\n", *pitch);*/ } diff --git a/libcelt/psy.c b/libcelt/psy.c index 3993bc2a..9b8762dd 100644 --- a/libcelt/psy.c +++ b/libcelt/psy.c @@ -61,7 +61,7 @@ void psydecay_init(struct PsyDecay *decay, int len, int Fs) decay->decayR[i] = pow(.1f, deriv); /* decay corresponding to -25dB/Bark */ decay->decayL[i] = pow(0.0031623f, deriv); - //printf ("%f %f\n", decayL[i], decayR[i]); + /*printf ("%f %f\n", decayL[i], decayR[i]);*/ } } @@ -75,7 +75,7 @@ static void spreading_func(struct PsyDecay *d, float *psd, float *mask, int len, { int i; float mem; - //for (i=0;idecayR[i])*mask[i] + d->decayL[i]*mem; mem = mask[i]; } - //for (i=0;i 4 && qi<-2) - // qi = -2; - //ec_laplace_encode(enc, qi, i==0?11192:6192); - //ec_laplace_encode(enc, qi, 8500-i*200); + /*ec_laplace_encode(enc, qi, i==0?11192:6192);*/ + /*ec_laplace_encode(enc, qi, 8500-i*200);*/ /* If we don't have enough bits to encode all the energy, just assume something safe. */ if (ec_enc_tell(enc, 0) - bits > budget) qi = -1; @@ -75,16 +72,16 @@ static void quant_energy_mono(const CELTMode *m, float *eBands, float *oldEBands q = qi*res; error[i] = f - qi; - //printf("%d ", qi); - //printf("%f %f ", pred+prev+q, x); - //printf("%f ", x-pred); + /*printf("%d ", qi);*/ + /*printf("%f %f ", pred+prev+q, x);*/ + /*printf("%f ", x-pred);*/ oldEBands[i] = mean+coef*oldEBands[i]+prev+q; prev = mean+prev+(1-beta)*q; } - //bits = ec_enc_tell(enc, 0) - bits; - //printf ("%d\n", bits); + /*bits = ec_enc_tell(enc, 0) - bits;*/ + /*printf ("%d\n", bits);*/ for (i=0;inbEBands;i++) { int q2; @@ -98,7 +95,7 @@ static void quant_energy_mono(const CELTMode *m, float *eBands, float *oldEBands ec_enc_uint(enc, q2, frac[i]); offset = ((q2+.5)/frac[i])-.5; oldEBands[i] += 6.*offset; - //printf ("%f ", error[i] - offset); + /*printf ("%f ", error[i] - offset);*/ } for (i=0;inbEBands;i++) { @@ -106,9 +103,9 @@ static void quant_energy_mono(const CELTMode *m, float *eBands, float *oldEBands if (eBands[i] < 0) eBands[i] = 0; } - //printf ("%d\n", ec_enc_tell(enc, 0)-9); + /*printf ("%d\n", ec_enc_tell(enc, 0)-9);*/ - //printf ("\n"); + /*printf ("\n");*/ } static void unquant_energy_mono(const CELTMode *m, float *eBands, float *oldEBands, int budget, ec_dec *dec) @@ -150,12 +147,12 @@ static void unquant_energy_mono(const CELTMode *m, float *eBands, float *oldEBan } for (i=0;inbEBands;i++) { - //printf ("%f ", error[i] - offset); + /*printf ("%f ", error[i] - offset);*/ eBands[i] = pow(10, .05*oldEBands[i])-.3; if (eBands[i] < 0) eBands[i] = 0; } - //printf ("\n"); + /*printf ("\n");*/ } diff --git a/libcelt/quant_pitch.c b/libcelt/quant_pitch.c index 04066ef7..6b6adfd4 100644 --- a/libcelt/quant_pitch.c +++ b/libcelt/quant_pitch.c @@ -61,12 +61,12 @@ int quant_pitch(float *gains, int len, ec_enc *enc) { int i, id; float g2[len]; - //for (i=0;i14) val >>= L-14; else if (L<14) val <<= 14-L; L <<= frac; - //printf ("%d\n", val); + /*printf ("%d\n", val);*/ for (i=0;i> 15; - //printf ("%d\n", val); + /*printf ("%d\n", val);*/ if (val > 16384) L |= (1<<(frac-i-1)); else @@ -73,17 +73,17 @@ static int log2_frac64(ec_uint64 val, int frac) int i; /* EC_ILOG64() actually returns log2()+1, go figure */ int L = EC_ILOG64(val)-1; - //printf ("in: %d %d ", val, L); + /*printf ("in: %d %d ", val, L);*/ if (L>14) val >>= L-14; else if (L<14) val <<= 14-L; L <<= frac; - //printf ("%d\n", val); + /*printf ("%d\n", val);*/ for (i=0;i> 15; - //printf ("%d\n", val); + /*printf ("%d\n", val);*/ if (val > 16384) L |= (1<<(frac-i-1)); else @@ -179,7 +179,7 @@ int vec_bits2pulses(const CELTMode *m, const int *bands, int *bits, int *pulses, pulses[i] = bits2pulses(m, i, bits[i]); sum += m->bits[i][pulses[i]]; } - //printf ("sum = %d\n", sum); + /*printf ("sum = %d\n", sum);*/ return sum; } @@ -201,7 +201,7 @@ int interp_bits2pulses(const CELTMode *m, int *bits1, int *bits2, int total, int else lo = mid; } - //printf ("interp bisection gave %d\n", lo); + /*printf ("interp bisection gave %d\n", lo);*/ for (j=0;jbits[j][pulses[j]+1]-m->bits[j][pulses[j]]; pulses[j] += 1; incremented = 1; - //printf ("INCREMENT %d\n", j); } } } @@ -253,14 +252,14 @@ int compute_allocation(const CELTMode *m, int *offsets, int total, int *pulses) bits[j] = (m->allocVectors[mid*len+j] + offsets[j])<eBands, bits, pulses, len) > total< +# else +# ifdef HAVE_ALLOCA_H +# include +# else +# include +# endif +# endif +#endif + +/** + * @def ALIGN(stack, size) + * + * Aligns the stack to a 'size' boundary + * + * @param stack Stack + * @param size New size boundary + */ + +/** + * @def PUSH(stack, size, type) + * + * Allocates 'size' elements of type 'type' on the stack + * + * @param stack Stack + * @param size Number of elements + * @param type Type of element + */ + +/** + * @def VARDECL(var) + * + * Declare variable on stack + * + * @param var Variable to declare + */ + +/** + * @def ALLOC(var, size, type) + * + * Allocate 'size' elements of 'type' on stack + * + * @param var Name of variable to allocate + * @param size Number of elements + * @param type Type of element + */ + +#ifdef ENABLE_VALGRIND + +#include + +#define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1)) + +#define PUSH(stack, size, type) (VALGRIND_MAKE_NOACCESS(stack, 1000),ALIGN((stack),sizeof(type)),VALGRIND_MAKE_WRITABLE(stack, ((size)*sizeof(type))),(stack)+=((size)*sizeof(type)),(type*)((stack)-((size)*sizeof(type)))) + +#else + +#define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1)) + +#define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)),(stack)+=((size)*sizeof(type)),(type*)((stack)-((size)*sizeof(type)))) + +#endif + +#if defined(VAR_ARRAYS) +#define VARDECL(var) +#define ALLOC(var, size, type) type var[size] +#elif defined(USE_ALLOCA) +#define VARDECL(var) var +#define ALLOC(var, size, type) var = alloca(sizeof(type)*(size)) +#else +#define VARDECL(var) var +#define ALLOC(var, size, type) var = PUSH(stack, size, type) +#endif + + +#endif diff --git a/libcelt/testcelt.c b/libcelt/testcelt.c index 64d39b2e..4ed8a674 100644 --- a/libcelt/testcelt.c +++ b/libcelt/testcelt.c @@ -92,7 +92,6 @@ int main(int argc, char *argv[]) return 1; } - //mode = celt_mode_create(44100, 1, 192, 64); /* Use mode4 for stereo and don't forget to change the value of CHANNEL above */ enc = celt_encoder_new(mode); dec = celt_decoder_new(mode); @@ -112,8 +111,6 @@ int main(int argc, char *argv[]) fprintf (stderr, "celt_encode() returned %d\n", len); return 1; } - //printf ("\n"); - //printf ("%d\n", len); /* This is to simulate packet loss */ #if 1 if (rand()%100==-1) @@ -124,7 +121,6 @@ int main(int argc, char *argv[]) for (i=0;i 3 || N > 30) Lupdate = 1; - //printf ("%d %d %d/%d %d\n", Lupdate, pulsesAtOnce, pulsesLeft, K, N); + /*printf ("%d %d %d/%d %d\n", Lupdate, pulsesAtOnce, pulsesLeft, K, N);*/ L2 = Lupdate; if (L2>maxL) { @@ -147,7 +145,7 @@ void alg_quant(float *x, float *W, int N, int K, float *p, float alpha, ec_enc * /* All pulses at one location must have the same sign. */ if (iy[m][j]*sign < 0) continue; - //fprintf (stderr, "%d/%d %d/%d %d/%d\n", i, K, m, L2, j, N); + /*fprintf (stderr, "%d/%d %d/%d %d/%d\n", i, K, m, L2, j, N);*/ float tmp_xy, tmp_yy, tmp_yp; float score; float g; @@ -232,8 +230,8 @@ void alg_quant(float *x, float *W, int N, int K, float *p, float alpha, ec_enc * float err=0; for (i=0;igain*y[0][i])*(x[i]-nbest[0]->gain*y[0][i]); - //if (N<=10) - //printf ("%f %d %d\n", err, K, N); + /*if (N<=10) + printf ("%f %d %d\n", err, K, N);*/ } for (i=0;igain*y[0][i]; @@ -243,11 +241,11 @@ void alg_quant(float *x, float *W, int N, int K, float *p, float alpha, ec_enc * int ABS = 0; for (i=0;i10) @@ -387,8 +385,8 @@ void intra_prediction(float *x, float *W, int N, int K, float *Y, float *P, int for (j=0;j10)