[xiph-commits] r14137 - in trunk/speex: include/speex libspeex

jm at svn.xiph.org jm at svn.xiph.org
Wed Nov 14 03:59:22 PST 2007


Author: jm
Date: 2007-11-14 03:59:22 -0800 (Wed, 14 Nov 2007)
New Revision: 14137

Modified:
   trunk/speex/include/speex/speex_stereo.h
   trunk/speex/libspeex/stereo.c
   trunk/speex/libspeex/vq.c
   trunk/speex/libspeex/vq.h
Log:
fixed-point: Patch by Thom Johansen to convert stereo decoding to fixed-point,
plus, my own ugly API hack to actually maintain compatibility.


Modified: trunk/speex/include/speex/speex_stereo.h
===================================================================
--- trunk/speex/include/speex/speex_stereo.h	2007-11-13 15:42:33 UTC (rev 14136)
+++ trunk/speex/include/speex/speex_stereo.h	2007-11-14 11:59:22 UTC (rev 14137)
@@ -46,7 +46,7 @@
 extern "C" {
 #endif
 
-/** State used for decoding (intensity) stereo information */
+/** If you access any of these fields directly, I'll personally come and bite you */
 typedef struct SpeexStereoState {
    float balance;      /**< Left/right balance info */
    float e_ratio;      /**< Ratio of energies: E(left+right)/[E(left)+E(right)]  */
@@ -56,9 +56,18 @@
    float reserved2;    /**< Reserved for future use */
 } SpeexStereoState;
 
-/** Initialization value for a stereo state */
+/** Deprecated. Use speex_stereo_state_init() instead. */
 #define SPEEX_STEREO_STATE_INIT {1,.5,1,1,0,0}
 
+/** Initialise/create a stereo stereo state */
+SpeexStereoState *speex_stereo_state_init();
+
+/** Reset/re-initialise an already allocated stereo state */
+void speex_stereo_state_reset(SpeexStereoState *stereo);
+
+/** Destroy a stereo stereo state */
+void speex_stereo_state_destroy(SpeexStereoState *stereo);
+
 /** Transforms a stereo frame into a mono frame and stores intensity stereo info in 'bits' */
 void speex_encode_stereo(float *data, int frame_size, SpeexBits *bits);
 

Modified: trunk/speex/libspeex/stereo.c
===================================================================
--- trunk/speex/libspeex/stereo.c	2007-11-13 15:42:33 UTC (rev 14136)
+++ trunk/speex/libspeex/stereo.c	2007-11-14 11:59:22 UTC (rev 14137)
@@ -35,12 +35,71 @@
 
 #include <speex/speex_stereo.h>
 #include <speex/speex_callbacks.h>
+#include "math_approx.h"
 #include "vq.h"
 #include <math.h>
+#include "os_support.h"
 
+typedef struct RealSpeexStereoState {
+   spx_word32_t balance;      /**< Left/right balance info */
+   spx_word32_t e_ratio;      /**< Ratio of energies: E(left+right)/[E(left)+E(right)]  */
+   spx_word32_t smooth_left;  /**< Smoothed left channel gain */
+   spx_word32_t smooth_right; /**< Smoothed right channel gain */
+   spx_uint32_t reserved1;     /**< Reserved for future use */
+   spx_int32_t reserved2;     /**< Reserved for future use */
+} RealSpeexStereoState;
+
+
 /*float e_ratio_quant[4] = {1, 1.26, 1.587, 2};*/
+#ifndef FIXED_POINT
 static const float e_ratio_quant[4] = {.25f, .315f, .397f, .5f};
+static const float e_ratio_quant_bounds[3] = {0.2825f, 0.356f, 0.4485f};
+#else
+static const spx_word16_t e_ratio_quant[4] = {8192, 10332, 13009, 16384};
+static const spx_word16_t e_ratio_quant_bounds[3] = {9257, 11665, 14696};
+#endif
 
+/* This is an ugly compatibility hack that properly resets the stereo state
+   In case it it compiled in fixed-point, but initialised with the deprecated
+   floating point static initialiser */
+#ifdef FIXED_POINT
+#define COMPATIBILITY_HACK(s) do {if ((s)->reserved1 != 0xdeadbeef) speex_stereo_state_reset((SpeexStereoState*)s); } while (0);
+#else
+#define COMPATIBILITY_HACK(s) 
+#endif
+
+SpeexStereoState *speex_stereo_state_init()
+{
+   SpeexStereoState *stereo = speex_alloc(sizeof(SpeexStereoState));
+   speex_stereo_state_reset(stereo);
+   return stereo;
+}
+
+void speex_stereo_state_reset(SpeexStereoState *_stereo)
+{
+   RealSpeexStereoState *stereo = (RealSpeexStereoState*)_stereo;
+#ifdef FIXED_POINT
+   stereo->balance = 65536;
+   stereo->e_ratio = 16384;
+   stereo->smooth_left = 16384;
+   stereo->smooth_right = 16384;
+   stereo->reserved1 = 0xdeadbeef;
+   stereo->reserved2 = 0;
+#else
+   stereo->balance = 1.0f;
+   stereo->e_ratio = .5f;
+   stereo->smooth_left = 1.f;
+   stereo->smooth_right = 1.f;
+   stereo->reserved1 = 0;
+   stereo->reserved2 = 0;
+#endif   
+}
+
+void speex_stereo_state_destroy(SpeexStereoState *stereo)
+{
+   speex_free(stereo);
+}
+
 void speex_encode_stereo(float *data, int frame_size, SpeexBits *bits)
 {
    int i, tmp;
@@ -73,8 +132,8 @@
    
    speex_bits_pack(bits, (int)balance, 5);
    
-   /*Quantize energy ratio*/
-   tmp=vq_index(&e_ratio, e_ratio_quant, 1, 4);
+   /* FIXME: this is a hack */
+   tmp=scal_quant(e_ratio*Q15_ONE, e_ratio_quant_bounds, 3);
    speex_bits_pack(bits, tmp, 2);
 }
 
@@ -110,81 +169,81 @@
    
    speex_bits_pack(bits, (int)balance, 5);
    
-   /*Quantize energy ratio*/
-   tmp=vq_index(&e_ratio, e_ratio_quant, 1, 4);
+   /* FIXME: this is a hack */
+   tmp=scal_quant(e_ratio*Q15_ONE, e_ratio_quant_bounds, 3);
    speex_bits_pack(bits, tmp, 2);
 }
 
-void speex_decode_stereo(float *data, int frame_size, SpeexStereoState *stereo)
+void speex_decode_stereo(float *data, int frame_size, SpeexStereoState *_stereo)
 {
-   float balance, e_ratio;
    int i;
-   float e_tot=0, e_left, e_right, e_sum;
-
+   spx_word32_t balance;
+   spx_word16_t e_left, e_right, e_ratio;
+   RealSpeexStereoState *stereo = (RealSpeexStereoState*)_stereo;
+   
+   COMPATIBILITY_HACK(stereo);
+   
    balance=stereo->balance;
    e_ratio=stereo->e_ratio;
-   for (i=frame_size-1;i>=0;i--)
-   {
-      e_tot += ((float)data[i])*data[i];
-   }
-   e_sum=e_tot/e_ratio;
-   e_left  = e_sum*balance / (1+balance);
-   e_right = e_sum-e_left;
+   
+   /* These two are Q14, with max value just below 2. */
+   e_right = DIV32(QCONST32(1., 22), spx_sqrt(MULT16_32_Q15(e_ratio, ADD32(QCONST32(1., 16), balance))));
+   e_left = SHR32(MULT16_16(spx_sqrt(balance), e_right), 8);
 
-   e_left  = sqrt(e_left/(e_tot+.01));
-   e_right = sqrt(e_right/(e_tot+.01));
-
    for (i=frame_size-1;i>=0;i--)
    {
-      float ftmp=data[i];
-      stereo->smooth_left  = .98*stereo->smooth_left  + .02*e_left;
-      stereo->smooth_right = .98*stereo->smooth_right + .02*e_right;
-      data[2*i] = stereo->smooth_left*ftmp;
-      data[2*i+1] = stereo->smooth_right*ftmp;
+      spx_word16_t tmp=data[i];
+      stereo->smooth_left = EXTRACT16(PSHR32(MAC16_16(MULT16_16(stereo->smooth_left, QCONST16(0.98, 15)), e_left, QCONST16(0.02, 15)), 15));
+      stereo->smooth_right = EXTRACT16(PSHR32(MAC16_16(MULT16_16(stereo->smooth_right, QCONST16(0.98, 15)), e_right, QCONST16(0.02, 15)), 15));
+      data[2*i] = (float)MULT16_16_P14(stereo->smooth_left, tmp);
+      data[2*i+1] = (float)MULT16_16_P14(stereo->smooth_right, tmp);
    }
 }
 
-void speex_decode_stereo_int(spx_int16_t *data, int frame_size, SpeexStereoState *stereo)
+void speex_decode_stereo_int(spx_int16_t *data, int frame_size, SpeexStereoState *_stereo)
 {
-   float balance, e_ratio;
    int i;
-   float e_tot=0, e_left, e_right, e_sum;
+   spx_word32_t balance;
+   spx_word16_t e_left, e_right, e_ratio;
+   RealSpeexStereoState *stereo = (RealSpeexStereoState*)_stereo;
 
+   COMPATIBILITY_HACK(stereo);
+   
    balance=stereo->balance;
    e_ratio=stereo->e_ratio;
-   for (i=frame_size-1;i>=0;i--)
-   {
-      e_tot += ((float)data[i])*data[i];
-   }
-   e_sum=e_tot/e_ratio;
-   e_left  = e_sum*balance / (1+balance);
-   e_right = e_sum-e_left;
+   
+   /* These two are Q14, with max value just below 2. */
+   e_right = DIV32(QCONST32(1., 22), spx_sqrt(MULT16_32_Q15(e_ratio, ADD32(QCONST32(1., 16), balance))));
+   e_left = SHR32(MULT16_16(spx_sqrt(balance), e_right), 8);
 
-   e_left  = sqrt(e_left/(e_tot+.01));
-   e_right = sqrt(e_right/(e_tot+.01));
-
    for (i=frame_size-1;i>=0;i--)
    {
-      float ftmp=data[i];
-      stereo->smooth_left  = .98*stereo->smooth_left  + .02*e_left;
-      stereo->smooth_right = .98*stereo->smooth_right + .02*e_right;
-      data[2*i] = stereo->smooth_left*ftmp;
-      data[2*i+1] = stereo->smooth_right*ftmp;
+      spx_int16_t tmp=data[i];
+      stereo->smooth_left = EXTRACT16(PSHR32(MAC16_16(MULT16_16(stereo->smooth_left, QCONST16(0.98, 15)), e_left, QCONST16(0.02, 15)), 15));
+      stereo->smooth_right = EXTRACT16(PSHR32(MAC16_16(MULT16_16(stereo->smooth_right, QCONST16(0.98, 15)), e_right, QCONST16(0.02, 15)), 15));
+      data[2*i] = (spx_int16_t)MULT16_16_P14(stereo->smooth_left, tmp);
+      data[2*i+1] = (spx_int16_t)MULT16_16_P14(stereo->smooth_right, tmp);
    }
 }
 
 int speex_std_stereo_request_handler(SpeexBits *bits, void *state, void *data)
 {
-   SpeexStereoState *stereo;
-   float sign=1;
+   RealSpeexStereoState *stereo;
+   spx_word16_t sign=1, dexp;
    int tmp;
 
-   stereo = (SpeexStereoState*)data;
+   stereo = (RealSpeexStereoState*)data;
+   
+   COMPATIBILITY_HACK(stereo);
+
    if (speex_bits_unpack_unsigned(bits, 1))
       sign=-1;
-   tmp = speex_bits_unpack_unsigned(bits, 5);
-   stereo->balance = exp(sign*.25*tmp);
-
+   dexp = speex_bits_unpack_unsigned(bits, 5);
+#ifndef FIXED_POINT
+   stereo->balance = exp(sign*.25*dexp);
+#else
+   stereo->balance = spx_exp(MULT16_16(sign, SHL16(dexp, 9)));
+#endif
    tmp = speex_bits_unpack_unsigned(bits, 2);
    stereo->e_ratio = e_ratio_quant[tmp];
 

Modified: trunk/speex/libspeex/vq.c
===================================================================
--- trunk/speex/libspeex/vq.c	2007-11-13 15:42:33 UTC (rev 14136)
+++ trunk/speex/libspeex/vq.c	2007-11-14 11:59:22 UTC (rev 14137)
@@ -70,30 +70,7 @@
    return i;
 }
 
-/*Finds the index of the entry in a codebook that best matches the input*/
-int vq_index(float *in, const float *codebook, int len, int entries)
-{
-   int i,j;
-   float min_dist=0;
-   int best_index=0;
-   for (i=0;i<entries;i++)
-   {
-      float dist=0;
-      for (j=0;j<len;j++)
-      {
-         float tmp = in[j]-*codebook++;
-         dist += tmp*tmp;
-      }
-      if (i==0 || dist<min_dist)
-      {
-         min_dist=dist;
-         best_index=i;
-      }
-   }
-   return best_index;
-}
 
-
 #ifndef OVERRIDE_VQ_NBEST
 /*Finds the indices of the n-best entries in a codebook*/
 void vq_nbest(spx_word16_t *in, const spx_word16_t *codebook, int len, int entries, spx_word32_t *E, int N, int *nbest, spx_word32_t *best_dist, char *stack)

Modified: trunk/speex/libspeex/vq.h
===================================================================
--- trunk/speex/libspeex/vq.h	2007-11-13 15:42:33 UTC (rev 14136)
+++ trunk/speex/libspeex/vq.h	2007-11-14 11:59:22 UTC (rev 14137)
@@ -40,7 +40,6 @@
 int scal_quant(spx_word16_t in, const spx_word16_t *boundary, int entries);
 int scal_quant32(spx_word32_t in, const spx_word32_t *boundary, int entries);
 
-int vq_index(float *in, const float *codebook, int len, int entries);
 #ifdef _USE_SSE
 #include <xmmintrin.h>
 void vq_nbest(spx_word16_t *in, const __m128 *codebook, int len, int entries, __m128 *E, int N, int *nbest, spx_word32_t *best_dist, char *stack);



More information about the commits mailing list