[xiph-commits] r12145 - trunk/speex/libspeex
jm at svn.xiph.org
jm at svn.xiph.org
Sun Nov 26 01:58:05 PST 2006
Author: jm
Date: 2006-11-26 01:57:19 -0800 (Sun, 26 Nov 2006)
New Revision: 12145
Modified:
trunk/speex/libspeex/filters.c
trunk/speex/libspeex/fixed_debug.h
trunk/speex/libspeex/jitter.c
trunk/speex/libspeex/kiss_fft.c
trunk/speex/libspeex/ltp.c
trunk/speex/libspeex/misc.c
trunk/speex/libspeex/nb_celp.c
trunk/speex/libspeex/preprocess.c
trunk/speex/libspeex/sb_celp.c
Log:
Removed the generic PSHR/SHL/SHR operators and changed them to either the
16-bit or the 32-bit version. Also, partially un-b0rked the AGC.
Modified: trunk/speex/libspeex/filters.c
===================================================================
--- trunk/speex/libspeex/filters.c 2006-11-26 06:31:33 UTC (rev 12144)
+++ trunk/speex/libspeex/filters.c 2006-11-26 09:57:19 UTC (rev 12145)
@@ -598,7 +598,7 @@
for (i=0;i<M-1;i++)
x[i]=mem[M-i-2];
for (i=0;i<N;i++)
- x[i+M-1]=SATURATE(PSHR(xx[i],1),16383);
+ x[i+M-1]=SATURATE(PSHR16(xx[i],1),16383);
for (i=0,k=0;i<N;i+=2,k++)
{
y1[k]=0;
@@ -615,7 +615,7 @@
y2[k] = SHR32(y2[k],1);
}
for (i=0;i<M-1;i++)
- mem[i]=SATURATE(PSHR(xx[N-i-1],1),16383);
+ mem[i]=SATURATE(PSHR16(xx[N-i-1],1),16383);
}
Modified: trunk/speex/libspeex/fixed_debug.h
===================================================================
--- trunk/speex/libspeex/fixed_debug.h 2006-11-26 06:31:33 UTC (rev 12144)
+++ trunk/speex/libspeex/fixed_debug.h 2006-11-26 09:57:19 UTC (rev 12145)
@@ -86,12 +86,13 @@
return res;
}
-static inline int EXTEND32(int x)
+#define EXTEND32(x) _EXTEND32(x, __FILE__, __LINE__)
+static inline int _EXTEND32(int x, char *file, int line)
{
int res;
if (!VERIFY_SHORT(x))
{
- fprintf (stderr, "EXTEND32: input is not short: %d\n", x);
+ fprintf (stderr, "EXTEND32: input is not short: %d in %s: line %d\n", x, file, line);
}
res = x;
spx_mips++;
@@ -163,8 +164,8 @@
#define SATURATE16(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x)))
#define SATURATE32(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x)))
-#define SHR(a,shift) ((a) >> (shift))
-#define SHL(a,shift) ((a) << (shift))
+//#define SHR(a,shift) ((a) >> (shift))
+//#define SHL(a,shift) ((a) << (shift))
static inline short ADD16(int a, int b)
{
@@ -227,7 +228,6 @@
#define ADD64(a,b) (MIPS_INC(a)+(b))
-#define PSHR(a,shift) (SHR((a)+((1<<((shift))>>1)),shift))
/* result fits in 16 bits */
static inline short MULT16_16_16(int a, int b)
{
@@ -258,9 +258,9 @@
}
#define MAC16_16(c,a,b) (spx_mips--,ADD32((c),MULT16_16((a),(b))))
-#define MAC16_16_Q11(c,a,b) (ADD16((c),EXTRACT16(SHR32(MULT16_16((a),(b)),11))))
-#define MAC16_16_Q13(c,a,b) (ADD16((c),EXTRACT16(SHR32(MULT16_16((a),(b)),13))))
-#define MAC16_16_P13(c,a,b) (ADD32((c),SHR(ADD32(4096,MULT16_16((a),(b))),13)))
+#define MAC16_16_Q11(c,a,b) (EXTRACT16(ADD16((c),EXTRACT16(SHR32(MULT16_16((a),(b)),11)))))
+#define MAC16_16_Q13(c,a,b) (EXTRACT16(ADD16((c),EXTRACT16(SHR32(MULT16_16((a),(b)),13)))))
+#define MAC16_16_P13(c,a,b) (EXTRACT16(ADD32((c),SHR32(ADD32(4096,MULT16_16((a),(b))),13))))
static inline int MULT16_32_QX(int a, long long b, int Q)
Modified: trunk/speex/libspeex/jitter.c
===================================================================
--- trunk/speex/libspeex/jitter.c 2006-11-26 06:31:33 UTC (rev 12144)
+++ trunk/speex/libspeex/jitter.c 2006-11-26 09:57:19 UTC (rev 12145)
@@ -41,8 +41,11 @@
#include <speex/speex.h>
#include <speex/speex_bits.h>
#include <speex/speex_jitter.h>
-#include <stdio.h>
+#ifndef NULL
+#define NULL 0
+#endif
+
#define LATE_BINS 10
#define MAX_MARGIN 30 /**< Number of bins in margin histogram */
Modified: trunk/speex/libspeex/kiss_fft.c
===================================================================
--- trunk/speex/libspeex/kiss_fft.c 2006-11-26 06:31:33 UTC (rev 12144)
+++ trunk/speex/libspeex/kiss_fft.c 2006-11-26 09:57:19 UTC (rev 12145)
@@ -54,8 +54,8 @@
kiss_fft_cpx *x=Fout;
for (i=0;i<2*m;i++)
{
- x[i].r = SHR(x[i].r,1);
- x[i].i = SHR(x[i].i,1);
+ x[i].r = SHR16(x[i].r,1);
+ x[i].i = SHR16(x[i].i,1);
}
}
Modified: trunk/speex/libspeex/ltp.c
===================================================================
--- trunk/speex/libspeex/ltp.c 2006-11-26 06:31:33 UTC (rev 12144)
+++ trunk/speex/libspeex/ltp.c 2006-11-26 09:57:19 UTC (rev 12145)
@@ -846,7 +846,7 @@
for (i=0;i<nsf;i++)
{
exc_out[i]=MULT16_16(exc[i-start],SHL16(pitch_coef,7));
- exc[i] = PSHR(exc_out[i],13);
+ exc[i] = EXTRACT16(PSHR32(exc_out[i],13));
}
*pitch_val = start;
gain_val[0]=gain_val[2]=0;
Modified: trunk/speex/libspeex/misc.c
===================================================================
--- trunk/speex/libspeex/misc.c 2006-11-26 06:31:33 UTC (rev 12144)
+++ trunk/speex/libspeex/misc.c 2006-11-26 09:57:19 UTC (rev 12145)
@@ -157,7 +157,7 @@
spx_word32_t res;
*seed = 1664525 * *seed + 1013904223;
res = MULT16_16(EXTRACT16(SHR32(*seed,16)),std);
- return PSHR32(SUB32(res, SHR(res, 3)),14);
+ return EXTRACT16(PSHR32(SUB32(res, SHR32(res, 3)),14));
}
#else
spx_word16_t speex_rand(spx_word16_t std, spx_int32_t *seed)
Modified: trunk/speex/libspeex/nb_celp.c
===================================================================
--- trunk/speex/libspeex/nb_celp.c 2006-11-26 06:31:33 UTC (rev 12144)
+++ trunk/speex/libspeex/nb_celp.c 2006-11-26 09:57:19 UTC (rev 12145)
@@ -1167,7 +1167,7 @@
pitch_gain = st->last_pitch_gain;
if (pitch_gain>54)
pitch_gain = 54;
- pitch_gain = SHL(pitch_gain, 9);
+ pitch_gain = SHL16(pitch_gain, 9);
#else
pitch_gain = GAIN_SCALING_1*st->last_pitch_gain;
if (pitch_gain>.85)
@@ -1201,7 +1201,7 @@
st->first = 0;
st->count_lost++;
- st->pitch_gain_buf[st->pitch_gain_buf_idx++] = PSHR(pitch_gain,9);
+ st->pitch_gain_buf[st->pitch_gain_buf_idx++] = PSHR16(pitch_gain,9);
if (st->pitch_gain_buf_idx > 2) /* rollover */
st->pitch_gain_buf_idx = 0;
}
Modified: trunk/speex/libspeex/preprocess.c
===================================================================
--- trunk/speex/libspeex/preprocess.c 2006-11-26 06:31:33 UTC (rev 12144)
+++ trunk/speex/libspeex/preprocess.c 2006-11-26 09:57:19 UTC (rev 12145)
@@ -491,6 +491,7 @@
for (i=0;i<N;i++)
{
float ff=((float)i)*.5*sampling_rate/((float)N);
+ /*st->loudness_weight[i] = .5f*(1.f/(1.f+ff/8000.f))+1.f*exp(-.5f*(ff-3800.f)*(ff-3800.f)/9e5f);*/
st->loudness_weight[i] = .35f-.35f*ff/16000.f+.73f*exp(-.5f*(ff-3800)*(ff-3800)/9e5f);
if (st->loudness_weight[i]<.01f)
st->loudness_weight[i]=.01f;
@@ -545,63 +546,44 @@
/* FIXME: The AGC doesn't work yet with fixed-point*/
#ifndef FIXED_POINT
-static void speex_compute_agc(SpeexPreprocessState *st)
+static void speex_compute_agc(SpeexPreprocessState *st, spx_word16_t Pframe, spx_word16_t *ft)
{
int i;
int N = st->ps_size;
- float scale=.5f/N;
float agc_gain;
- int freq_start, freq_end;
- float active_bands = 0;
- freq_start = (int)(300.0f*2*N/st->sampling_rate);
- freq_end = (int)(2000.0f*2*N/st->sampling_rate);
- for (i=freq_start;i<freq_end;i++)
+ float loudness=0.f;
+ float rate, rate2=.05f;
+
+ for (i=2;i<N;i++)
{
- if (st->S[i] > 20.f*st->Smin[i]+1000.f)
- active_bands+=1;
+ loudness += 2.f*N*SQR16(st->ft[i])* st->loudness_weight[i];
}
- active_bands /= (freq_end-freq_start+1);
-
- if (active_bands > .2f)
+ loudness=sqrt(loudness);
+ /*if (loudness < 2*pow(st->loudness, 1.0/LOUDNESS_EXP) &&
+ loudness*2 > pow(st->loudness, 1.0/LOUDNESS_EXP))*/
+ if (Pframe>.5 && st->nb_adapt>50)
{
- float loudness=0.f;
- float rate, rate2=.2f;
st->nb_loudness_adapt++;
rate=2.0f/(1+st->nb_loudness_adapt);
- if (rate < .05f)
- rate = .05f;
- if (rate < .1f && pow(loudness, LOUDNESS_EXP) > st->loudness)
- rate = .1f;
- if (rate < .2f && pow(loudness, LOUDNESS_EXP) > 3.f*st->loudness)
- rate = .2f;
- if (rate < .4f && pow(loudness, LOUDNESS_EXP) > 10.f*st->loudness)
- rate = .4f;
-
- for (i=2;i<N;i++)
- {
- loudness += scale*st->ps[i] * FRAC_SCALING_1*FRAC_SCALING_1*st->gain2[i] * st->gain2[i] * st->loudness_weight[i];
- }
- loudness=sqrt(loudness);
- /*if (loudness < 2*pow(st->loudness, 1.0/LOUDNESS_EXP) &&
- loudness*2 > pow(st->loudness, 1.0/LOUDNESS_EXP))*/
st->loudness = (1-rate)*st->loudness + (rate)*pow(loudness, LOUDNESS_EXP);
st->loudness2 = (1-rate2)*st->loudness2 + rate2*pow(st->loudness, 1.0f/LOUDNESS_EXP);
-
- loudness = pow(st->loudness, 1.0f/LOUDNESS_EXP);
-
- /*fprintf (stderr, "%f %f %f\n", loudness, st->loudness2, rate);*/
}
+ printf ("%f %f %f %f\n", Pframe, loudness, pow(st->loudness, 1.0f/LOUDNESS_EXP), st->loudness2);
+ loudness = pow(st->loudness, 1.0f/LOUDNESS_EXP);
+
+ /*fprintf (stderr, "%f %f %f\n", loudness, st->loudness2, rate);*/
+
agc_gain = st->agc_level/st->loudness2;
/*fprintf (stderr, "%f %f %f %f\n", active_bands, st->loudness, st->loudness2, agc_gain);*/
- if (agc_gain>200)
- agc_gain = 200;
-
- for (i=0;i<N;i++)
- st->gain2[i] *= agc_gain;
+ if (agc_gain>30)
+ agc_gain = 30;
+ for (i=0;i<2*N;i++)
+ ft[i] *= agc_gain;
+
}
#endif
@@ -850,8 +832,8 @@
theta = MIN32(theta, EXTEND32(32767));
/*Q8*/tmp = MULT16_16_Q15((SHL32(1,SNR_SHIFT)+st->prior[i]),EXTRACT16(MIN32(Q15ONE,SHR32(spx_exp(-EXTRACT16(theta)),1))));
tmp = MIN16(QCONST16(3.,SNR_SHIFT), tmp); /* Prevent overflows in the next line*/
-/*Q8*/tmp = PSHR(MULT16_16(PDIV32_16(SHL32(EXTEND32(q),8),(Q15_ONE-q)),tmp),8);
- st->gain2[i]=DIV32_16(SHL(EXTEND32(32767),SNR_SHIFT), ADD16(256,tmp));
+/*Q8*/tmp = EXTRACT16(PSHR32(MULT16_16(PDIV32_16(SHL32(EXTEND32(q),8),(Q15_ONE-q)),tmp),8));
+ st->gain2[i]=DIV32_16(SHL32(EXTEND32(32767),SNR_SHIFT), ADD16(256,tmp));
#else
st->gain2[i]=1/(1.f + (q/(1.f-q))*(1+st->prior[i])*exp(-theta));
#endif
@@ -927,13 +909,7 @@
for (i=0;i<N+M;i++)
st->gain2[i]=Q15_ONE;
}
-
- /*FIXME: This *will* not work for fixed-point */
-#ifndef FIXED_POINT
- if (st->agc_enabled)
- speex_compute_agc(st);
-#endif
-
+
/* Apply computed gain */
for (i=1;i<N;i++)
{
@@ -943,6 +919,12 @@
st->ft[0] = MULT16_16_P15(st->gain2[0],st->ft[0]);
st->ft[2*N-1] = MULT16_16_P15(st->gain2[N-1],st->ft[2*N-1]);
+ /*FIXME: This *will* not work for fixed-point */
+#ifndef FIXED_POINT
+ if (st->agc_enabled)
+ speex_compute_agc(st, Pframe, st->ft);
+#endif
+
/* Inverse FFT with 1/N scaling */
spx_ifft(st->fft_lookup, st->ft, st->frame);
/* Scale back to original (lower) amplitude */
Modified: trunk/speex/libspeex/sb_celp.c
===================================================================
--- trunk/speex/libspeex/sb_celp.c 2006-11-26 06:31:33 UTC (rev 12144)
+++ trunk/speex/libspeex/sb_celp.c 2006-11-26 09:57:19 UTC (rev 12145)
@@ -402,13 +402,13 @@
qmf_decomp(in, h0, st->x0d, st->x1d, st->full_frame_size, QMF_ORDER, st->h0_mem, stack);
for (i=0;i<st->frame_size;i++)
- low[i] = SATURATE(PSHR(st->x0d[i],SIG_SHIFT),32767);
+ low[i] = EXTRACT16(SATURATE(PSHR32(st->x0d[i],SIG_SHIFT),32767));
/* Encode the narrowband part*/
speex_encode_native(st->st_low, low, bits);
for (i=0;i<st->frame_size;i++)
- st->x0d[i] = SHL(low[i],SIG_SHIFT);
+ st->x0d[i] = SHL32(EXTEND32(low[i]),SIG_SHIFT);
}
/* High-band buffering / sync with low band */
for (i=0;i<st->windowSize-st->frame_size;i++)
@@ -436,7 +436,7 @@
ALLOC(w_sig, st->windowSize, spx_word16_t);
/* Window for analysis */
for (i=0;i<st->windowSize;i++)
- w_sig[i] = SHR(MULT16_16(SHR((spx_word32_t)(st->high[i]),SIG_SHIFT),st->window[i]),SIG_SHIFT);
+ w_sig[i] = EXTRACT16(SHR32(MULT16_16(EXTRACT16(SHR32(EXTEND32(st->high[i]),SIG_SHIFT)),st->window[i]),SIG_SHIFT));
/* Compute auto-correlation */
_spx_autocorr(w_sig, st->autocorr, st->lpcSize+1, st->windowSize);
@@ -645,7 +645,7 @@
rl = low_pi_gain[sub];
#ifdef FIXED_POINT
- filter_ratio=PDIV32_16(SHL(rl+82,2),SHR(82+rh,5));
+ filter_ratio=PDIV32_16(SHL32(rl+82,2),EXTRACT16(SHR32(82+rh,5)));
#else
filter_ratio=(rl+.01)/(rh+.01);
#endif
@@ -1008,7 +1008,7 @@
ret = speex_decode_native(st->st_low, bits, low);
for (i=0;i<st->frame_size;i++)
- st->x0d[i] = SHL((spx_sig_t)low[i], SIG_SHIFT);
+ st->x0d[i] = SHL32(EXTEND32(low[i]), SIG_SHIFT);
}
speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, &dtx);
@@ -1134,7 +1134,7 @@
rl = low_pi_gain[sub];
#ifdef FIXED_POINT
- filter_ratio=PDIV32_16(SHL(rl+82,2),SHR(82+rh,5));
+ filter_ratio=PDIV32_16(SHL32(rl+82,2),EXTRACT16(SHR32(82+rh,5)));
#else
filter_ratio=(rl+.01)/(rh+.01);
#endif
@@ -1195,7 +1195,7 @@
if (st->subframeSize==80)
gc *= 1.4142;
- scale = SHL(MULT16_16(PDIV32_16(SHL(gc,SIG_SHIFT-6),filter_ratio),(1+el)),6);
+ scale = SHL32(MULT16_16(PDIV32_16(SHL32(EXTEND32(gc),SIG_SHIFT-6),filter_ratio),(1+el)),6);
SUBMODE(innovation_unquant)(exc, SUBMODE(innovation_params), st->subframeSize,
bits, stack, &st->seed);
More information about the commits
mailing list