[xiph-cvs] cvs commit: speex/libspeex cb_search.c filters.c nb_celp.c

Jean-Marc Valin jm at xiph.org
Thu Oct 24 21:11:15 PDT 2002



jm          02/10/25 00:11:15

  Modified:    libspeex cb_search.c filters.c nb_celp.c
  Log:
  Code cleanup

Revision  Changes    Path
1.75      +5 -5      speex/libspeex/cb_search.c

Index: cb_search.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/cb_search.c,v
retrieving revision 1.74
retrieving revision 1.75
diff -u -r1.74 -r1.75
--- cb_search.c	23 Oct 2002 19:06:19 -0000	1.74
+++ cb_search.c	25 Oct 2002 04:11:15 -0000	1.75
@@ -148,14 +148,16 @@
 
       res = resp+i*subvect_size;
       shape = shape_cb+i*subvect_size;
-      /* Compute codeword response */
 
+      /* Compute codeword response using convolution with impulse response */
       for(j=0;j<subvect_size;j++)
       {
          res[j]=0;
          for (k=0;k<=j;k++)
             res[j] += shape[k]*r[j-k];
       }
+      
+      /* Compute codeword energy */
       E[i]=0;
       for(j=0;j<subvect_size;j++)
          E[i]+=res[j]*res[j];
@@ -221,8 +223,6 @@
             {
 
                /*previous target (we don't care what happened before*/
-               for (m=0;m<(i+1)*subvect_size;m++)
-                  t[m]=ct[m];
                for (m=(i+1)*subvect_size;m<nsf;m++)
                   t[m]=ct[m];
                /* New code: update the rest of the target only if it's worth it */
@@ -251,13 +251,13 @@
                   {
                      for (n=N-1;n>m;n--)
                      {
-                        for (q=0;q<nsf;q++)
+                        for (q=(i+1)*subvect_size;q<nsf;q++)
                            nt[n][q]=nt[n-1][q];
                         for (q=0;q<nb_subvect;q++)
                            nind[n][q]=nind[n-1][q];
                         ndist[n]=ndist[n-1];
                      }
-                     for (q=0;q<nsf;q++)
+                     for (q=(i+1)*subvect_size;q<nsf;q++)
                         nt[m][q]=t[q];
                      for (q=0;q<nb_subvect;q++)
                         nind[m][q]=oind[j][q];

<p><p>1.25      +0 -4      speex/libspeex/filters.c

Index: filters.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/filters.c,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -r1.24 -r1.25
--- filters.c	23 Oct 2002 06:24:57 -0000	1.24
+++ filters.c	25 Oct 2002 04:11:15 -0000	1.25
@@ -35,10 +35,6 @@
 #include <stdio.h>
 #include <math.h>
 
-#define min(a,b) ((a) < (b) ? (a) : (b))
-
-#define MAX_ORD 20
-
 void print_vec(float *vec, int len, char *name)
 {
    int i;

<p><p>1.70      +34 -14    speex/libspeex/nb_celp.c

Index: nb_celp.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/nb_celp.c,v
retrieving revision 1.69
retrieving revision 1.70
diff -u -r1.69 -r1.70
--- nb_celp.c	24 Oct 2002 06:29:13 -0000	1.69
+++ nb_celp.c	25 Oct 2002 04:11:15 -0000	1.70
@@ -259,12 +259,14 @@
 
    /* LPC to LSPs (x-domain) transform */
    roots=lpc_to_lsp (st->lpc, st->lpcSize, st->lsp, 15, 0.2, stack);
+   /* Check if we found all the roots */
    if (roots==st->lpcSize)
    {
       /* LSP x-domain to angle domain*/
       for (i=0;i<st->lpcSize;i++)
          st->lsp[i] = acos(st->lsp[i]);
    } else {
+      /* Search again if we can afford it */
       if (st->complexity>1)
          roots = lpc_to_lsp (st->lpc, st->lpcSize, st->lsp, 11, 0.05, stack);
       if (roots==st->lpcSize) 
@@ -517,6 +519,7 @@
             st->bw_lpc2[i]=0;
       }
 
+      /* Compute impulse response of A(z/g1) / ( A(z)*A(z/g2) )*/
       for (i=0;i<st->subframeSize;i++)
          exc[i]=0;
       exc[0]=1;
@@ -549,7 +552,7 @@
       for (i=0;i<st->subframeSize;i++)
          exc[i]=exc2[i]=0;
 
-      /* If we have a long-term predictor (not all sub-modes have one) */
+      /* If we have a long-term predictor (otherwise, something's wrong) */
       if (SUBMODE(ltp_quant))
       {
          int pit_min, pit_max;
@@ -575,14 +578,16 @@
             pit_max = st->max_pitch;
          }
          
+         /* Force pitch to use only the current frame if needed */
          if (st->bounded_pitch && pit_max>offset)
             pit_max=offset;
+
+         /* Perform pitch search */
          pitch = SUBMODE(ltp_quant)(target, sw, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2,
                                     exc, SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef,
                                     st->lpcSize, st->subframeSize, bits, stack, 
                                     exc2, syn_resp, st->complexity);
 
-         /*printf ("cl_pitch: %d\n", pitch);*/
          st->pitch[sub]=pitch;
       } else {
          fprintf (stderr, "No pitch prediction, what's wrong\n");
@@ -591,7 +596,7 @@
       /* Update target for adaptive codebook contribution */
       syn_percep_zero(exc, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2, res, st->subframeSize, st->lpcSize, stack);
       for (i=0;i<st->subframeSize;i++)
-        target[i]-=res[i];
+         target[i]-=res[i];
 
 
       /* Quantization of innovation */
@@ -611,9 +616,7 @@
          
          ener /= ol_gain;
 
-         if (0)
-            printf ("ener: %f %f %f\n", ener, ol_gain, ol_pitch_coef);
-
+         /* Calculate gain correction for the sub-frame (if any) */
          if (SUBMODE(have_subframe_gain)) 
          {
             int qe;
@@ -629,13 +632,12 @@
                ener=exc_gain_quant_scal1[qe];               
             }
             ener=exp(ener);
-            /*printf ("encode gain: %d %f\n", qe, ener);*/
          } else {
             ener=1;
          }
 
          ener*=ol_gain;
-         /*printf ("transmit gain: %f\n", ener);*/
+
          ener_1 = 1/ener;
 
          if (0) {
@@ -646,15 +648,19 @@
             printf ("\n");
          }
          
+         /* Normalize innovation */
          for (i=0;i<st->subframeSize;i++)
             target[i]*=ener_1;
          
+         /* Quantize innovation */
          if (SUBMODE(innovation_quant))
          {
-            /* Normal quantization */
+            /* Codebook search */
             SUBMODE(innovation_quant)(target, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2, 
                                       SUBMODE(innovation_params), st->lpcSize, st->subframeSize, 
                                       innov, syn_resp, bits, stack, st->complexity);
+            
+            /* De-normalize innovation and update excitation */
             for (i=0;i<st->subframeSize;i++)
                innov[i]*=ener;
             for (i=0;i<st->subframeSize;i++)
@@ -663,6 +669,7 @@
             fprintf(stderr, "No fixed codebook\n");
          }
 
+         /* In some (rare) modes, we do a second search (more bits) to reduce noise even more */
          if (SUBMODE(double_codebook)) {
             float *tmp_stack=stack;
             float *innov2 = PUSH(tmp_stack, st->subframeSize);
@@ -822,6 +829,7 @@
       exc=st->exc+offset;
       /* Excitation after post-filter*/
 
+      /* Calculate perceptually enhanced LPC filter */
       if (st->lpc_enh_enabled)
       {
          float r=.9;
@@ -841,6 +849,8 @@
          
       }
         
+      /* Make up a plausible excitation */
+      /* THIS CAN BE IMPROVED */
       for (i=0;i<st->subframeSize;i++)
       {
          exc[i]=st->last_pitch_gain*exc[i-st->last_pitch] + 
@@ -850,6 +860,7 @@
       for (i=0;i<st->subframeSize;i++)
          sp[i]=exc[i];
       
+      /* Signal synthesis */
       if (st->lpc_enh_enabled)
       {
          filter_mem2(sp, awk2, awk1, sp, st->subframeSize, st->lpcSize, 
@@ -893,15 +904,17 @@
    st=state;
    stack=st->stack;
 
+   /* If bits is NULL, consider the packet to be lost (what could we do anyway) */
    if (!bits)
    {
       nb_decode_lost(st, out, stack);
       return 0;
    }
 
+   /* Search for next narrwoband block (handle requests, skip wideband blocks) */
    do {
       wideband = speex_bits_unpack_unsigned(bits, 1);
-      if (wideband)
+      if (wideband) /* Skip wideband block (for compatibility) */
       {
          int submode;
          int advance;
@@ -918,20 +931,20 @@
       }
 
       m = speex_bits_unpack_unsigned(bits, 4);
-      if (m==15)
+      if (m==15) /* We found a terminator */
       {
          return -1;
-      } else if (m==14)
+      } else if (m==14) /* Speex in-band request */
       {
          int ret = speex_inband_handler(bits, st->speex_callbacks, state);
          if (ret)
             return ret;
-      } else if (m==13)
+      } else if (m==13) /* User in-band request */
       {
          int ret = st->user_callback.func(bits, state, st->user_callback.data);
          if (ret)
             return ret;
-      } else if (m>7)
+      } else if (m>7) /* Invalid mode */
       {
          return -2;
       }
@@ -1018,6 +1031,7 @@
       for (i=0;i<st->lpcSize;i++)
          st->interp_qlsp[i] = (1-tmp)*st->old_qlsp[i] + tmp*st->qlsp[i];
 
+      /* Make sure the LSP's are stable */
       lsp_enforce_margin(st->interp_qlsp, st->lpcSize, .002);
 
 
@@ -1110,6 +1124,7 @@
          for (i=0;i<st->subframeSize;i++)
             innov[i]=0;
 
+         /* Decode sub-frame gain correction */
          if (SUBMODE(have_subframe_gain)==3)
          {
             q_energy = speex_bits_unpack_unsigned(bits, 3);
@@ -1132,11 +1147,13 @@
             fprintf(stderr, "No fixed codebook\n");
          }
 
+         /* De-normalize innovation and update excitation */
          for (i=0;i<st->subframeSize;i++)
             innov[i]*=ener;
          for (i=0;i<st->subframeSize;i++)
             exc[i]+=innov[i];
 
+         /* Decode second codebook (only for some modes) */
          if (SUBMODE(double_codebook))
          {
             float *tmp_stack=stack;
@@ -1155,16 +1172,19 @@
       for (i=0;i<st->subframeSize;i++)
          sp[i]=exc[i];
 
+      /* Signal synthesis */
       if (st->lpc_enh_enabled && SUBMODE(comb_gain>0))
          comb_filter(exc, sp, st->interp_qlpc, st->lpcSize, st->subframeSize,
                               pitch, pitch_gain, .5);
       if (st->lpc_enh_enabled)
       {
+         /* Use enhanced LPC filter */
          filter_mem2(sp, awk2, awk1, sp, st->subframeSize, st->lpcSize, 
                      st->mem_sp+st->lpcSize);
          filter_mem2(sp, awk3, st->interp_qlpc, sp, st->subframeSize, st->lpcSize, 
                      st->mem_sp);
       } else {
+         /* Use regular filter */
          for (i=0;i<st->lpcSize;i++)
             st->mem_sp[st->lpcSize+i] = 0;
          iir_mem2(sp, st->interp_qlpc, sp, st->subframeSize, st->lpcSize, 

<p><p>--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the commits mailing list