[xiph-cvs] cvs commit: speex/src speexdec.c

Jean-Marc Valin jm at xiph.org
Mon Jan 6 12:43:48 PST 2003



jm          03/01/06 15:43:48

  Modified:    doc      manual.lyx
               libspeex modes.c nb_celp.c nb_celp.h sb_celp.c
               src      speexdec.c
  Log:
  Used the last 4 bits of mode1 for a CNG flag, plus some cleanup, bugfix

Revision  Changes    Path
1.44      +2 -14     speex/doc/manual.lyx

Index: manual.lyx
===================================================================
RCS file: /usr/local/cvsroot/speex/doc/manual.lyx,v
retrieving revision 1.43
retrieving revision 1.44
diff -u -r1.43 -r1.44
--- manual.lyx	6 Jan 2003 04:18:11 -0000	1.43
+++ manual.lyx	6 Jan 2003 20:43:48 -0000	1.44
@@ -706,8 +706,7 @@
 Dynamically-selectable codebooks (LSP, pitch and innovation)
 \layout Itemize
 
-G.728-like fixed codebooks (without backward-adaptive gains because of patent
- issues)
+sub-vector fixed (innovation) codebooks
 \layout Subsection
 
 LPC Analysis
@@ -842,18 +841,7 @@
  the gain at the same time.
  This save many bits that would otherwise be allocated for a separate gain
  at the price of a slight increase in complexity.
- Except for the absence of (backward-adaptive) gain, the technique used
- in Speex is similar to G.728 (LD-CELP).
- However since we do not have a low-delay constraint, the search can be
- made more 
-\begin_inset Quotes eld
-\end_inset 
-
-global
-\begin_inset Quotes erd
-\end_inset 
-
- and make use of the whole information available for a sub-frame.
+ 
 \layout Subsection
 
 Bit allocation

<p><p>1.95      +13 -3     speex/libspeex/modes.c

Index: modes.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/modes.c,v
retrieving revision 1.94
retrieving revision 1.95
diff -u -r1.94 -r1.95
--- modes.c	6 Jan 2003 04:18:11 -0000	1.94
+++ modes.c	6 Jan 2003 20:43:48 -0000	1.95
@@ -158,7 +158,7 @@
 static SpeexSubmode nb_submode1 = {
    0,
    1,
-   1,
+   0,
    0,
    /* LSP quantization */
    lsp_quant_lbr,
@@ -571,7 +571,12 @@
       *((int*)ptr)=m->frameSize;
       break;
    case SPEEX_SUBMODE_BITS_PER_FRAME:
-      *((int*)ptr) = m->submodes[*((int*)ptr)]->bits_per_frame;
+      if (*((int*)ptr)==0)
+         *((int*)ptr) = NB_SUBMODE_BITS+1;
+      else if (m->submodes[*((int*)ptr)]==NULL)
+         *((int*)ptr) = -1;
+      else
+         *((int*)ptr) = m->submodes[*((int*)ptr)]->bits_per_frame;
       break;
    default:
       speex_warning_int("Unknown nb_mode_query request: ", request);
@@ -589,7 +594,12 @@
       *((int*)ptr)=m->frameSize;
       break;
    case SPEEX_SUBMODE_BITS_PER_FRAME:
-      *((int*)ptr) = m->submodes[*((int*)ptr)]->bits_per_frame;
+      if (*((int*)ptr)==0)
+         *((int*)ptr) = SB_SUBMODE_BITS+1;
+      else if (m->submodes[*((int*)ptr)]==NULL)
+         *((int*)ptr) = -1;
+      else
+         *((int*)ptr) = m->submodes[*((int*)ptr)]->bits_per_frame;
       break;
    default:
       speex_warning_int("Unknown wb_mode_query request: ", request);

<p><p>1.107     +59 -52    speex/libspeex/nb_celp.c

Index: nb_celp.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/nb_celp.c,v
retrieving revision 1.106
retrieving revision 1.107
diff -u -r1.106 -r1.107
--- nb_celp.c	6 Jan 2003 06:53:02 -0000	1.106
+++ nb_celp.c	6 Jan 2003 20:43:48 -0000	1.107
@@ -186,7 +186,7 @@
    vbr_destroy(st->vbr);
 
    /*Free state memory... should be last*/
-   speex_free((float*)st);
+   speex_free(st);
 }
 
 int nb_encode(void *state, float *in, SpeexBits *bits)
@@ -266,7 +266,6 @@
    lsp_dist=0;
    for (i=0;i<st->lpcSize;i++)
       lsp_dist += (st->old_lsp[i] - st->lsp[i])*(st->old_lsp[i] - st->lsp[i]);
-   /*printf ("%f\n", lsp_dist);*/
 
    /* Whole frame analysis (open-loop estimation of pitch and excitation gain) */
    {
@@ -301,7 +300,6 @@
                                nol_pitch, nol_pitch_coef, 6, stack);
          ol_pitch=nol_pitch[0];
          ol_pitch_coef = nol_pitch_coef[0];
-         /*printf ("%f %d %d %d %d %d %d ", ol_pitch_coef, nol_pitch[0], nol_pitch[1], nol_pitch[2], nol_pitch[3], nol_pitch[4], nol_pitch[5]);*/
          /*Try to remove pitch multiples*/
          for (i=1;i<6;i++)
          {
@@ -313,11 +311,9 @@
                ol_pitch = nol_pitch[i];
             }
          }
-         /*printf ("%d\n", ol_pitch);*/
          /*if (ol_pitch>50)
            ol_pitch/=2;*/
          /*ol_pitch_coef = sqrt(ol_pitch_coef);*/
-         /*printf ("ol_pitch: %d %f\n", ol_pitch, ol_pitch_coef);*/
       } else {
          ol_pitch=0;
          ol_pitch_coef=0;
@@ -329,8 +325,6 @@
       ol_gain=0;
       for (i=0;i<st->frameSize;i++)
          ol_gain += st->exc[i]*st->exc[i];
-      /*for (i=0;i<160;i++)
-        printf ("%f\n", st->exc[i]);*/
       
       ol_gain=sqrt(1+ol_gain/st->frameSize);
    }
@@ -356,7 +350,6 @@
             st->vbr_quality=10;
          if (st->vbr_quality<0)
             st->vbr_quality=0;
-         /*printf ("%f %f\n", st->abr_drift/(.1+st->abr_count), st->vbr_quality);*/
       }
 
       st->relative_quality = vbr_analysis(st->vbr, in, st->frameSize, ol_pitch, ol_pitch_coef);
@@ -382,23 +375,18 @@
          
          if (mode==0)
          {
-            if (st->submodeID>1 || lsp_dist>.05 || st->dtx_count>20)
+            if (st->dtx_count==0 || lsp_dist>.05 || st->dtx_count>20)
             {
                mode=1;
-               st->dtx_count=0;
+               st->dtx_count=1;
             } else {
                mode=0;
                st->dtx_count++;
             }
          } else {
-            if (st->submodeID>1)
-               st->dtx_count=0;
+            st->dtx_count=0;
          }
 
-         /*fprintf(stderr, "");
-         fprintf (stderr, "encode %f %d\n", st->relative_quality, mode);
-         fprintf(stderr, "encode: %d %d\n",st->submodeID, mode);*/
-
          speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);
 
          if (st->abr_enabled)
@@ -409,29 +397,30 @@
             st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);
             st->abr_count += 1.0;
          }
-         /*fprintf(stderr, "encode: %d %d\n",st->submodeID, mode);*/
+
       } else {
          /*VAD only case*/
          int mode;
          if (st->relative_quality<2)
          {
-            if (st->submodeID>1 || lsp_dist>.05 || !st->dtx_enabled || st->dtx_count>20)
+            if (st->dtx_count==0 || lsp_dist>.05 || !st->dtx_enabled || st->dtx_count>20)
             {
-               st->dtx_count=0;
+               st->dtx_count=1;
                mode=1;
             } else {
                mode=0;
                st->dtx_count++;
             }
-         } else
+         } else {
+            st->dtx_count = 0;
             mode=st->submodeSelect;
+         }
          /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
          st->submodeID=mode;
       } 
    } else {
       st->relative_quality = -1;
    }
-   /*printf ("VBR quality = %f\n", vbr_qual);*/
 
    /* First, transmit a zero for narrowband */
    speex_bits_pack(bits, 0, 1);
@@ -712,15 +701,6 @@
 
          ener_1 = 1/ener;
 
-#if 0
-         {
-            int start=rand()%35;
-            printf ("norm_exc: ");
-            for (i=start;i<start+5;i++)
-               printf ("%f ", ener_1*st->buf2[i]);
-            printf ("\n");
-         }
-#endif    
          /* Normalize innovation */
          for (i=0;i<st->subframeSize;i++)
             target[i]*=ener_1;
@@ -785,6 +765,14 @@
          st->old_qlsp[i] = st->qlsp[i];
    }
 
+   if (st->submodeID==1)
+   {
+      if (st->dtx_count)
+         speex_bits_pack(bits, 15, 4);
+      else
+         speex_bits_pack(bits, 0, 4);
+   }
+
    /* The next frame will not be the first (Duh!) */
    st->first = 0;
 
@@ -882,7 +870,7 @@
 
    st->voc_m1=st->voc_m2=st->voc_mean=0;
    st->voc_offset=0;
-
+   st->dtx_enabled=0;
    return st;
 }
 
@@ -912,10 +900,6 @@
       pitch_gain=.95;
 
    pitch_gain *= fact;
-   
-#ifdef DEBUG
-   printf ("recovered unquantized pitch: %d, gain: %f (fact=%f, buf=[%f %f %f], med=%f)\n", st->last_pitch, pitch_gain, fact, st->pitch_gain_buf[0], st->pitch_gain_buf[1], st->pitch_gain_buf[2], gain_med);
-#endif
 
    /* Shift all buffers by one frame */
    speex_move(st->inBuf, st->inBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(float));
@@ -960,18 +944,17 @@
       /* THIS CAN BE IMPROVED */
       /*if (pitch_gain>.95)
         pitch_gain=.95;*/
-      /*printf ("%f\n", pitch_gain);*/
       {
          float innov_gain=0;
          for (i=0;i<st->frameSize;i++)
             innov_gain += st->innov[i]*st->innov[i];
-         innov_gain=sqrt(innov_gain/160);
+         innov_gain=sqrt(innov_gain/st->frameSize);
       for (i=0;i<st->subframeSize;i++)
       {
 #if 0
          exc[i] = pitch_gain * exc[i - st->last_pitch] + fact*sqrt(1-pitch_gain)*st->innov[i+offset];
          /*Just so it give the same lost packets as with if 0*/
-         rand();
+         /*rand();*/
 #else
          /*exc[i]=pitch_gain*exc[i-st->last_pitch] +  fact*st->innov[i+offset];*/
          exc[i]=pitch_gain*exc[i-st->last_pitch] + 
@@ -1030,7 +1013,7 @@
    stack=st->stack;
 
    /* Check if we're in DTX mode*/
-   if (!bits && st->submodeID<2)
+   if (!bits && st->dtx_enabled)
    {
       st->submodeID=0;
    } else 
@@ -1051,12 +1034,34 @@
             int advance;
             advance = submode = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
             speex_mode_query(&speex_wb_mode, SPEEX_SUBMODE_BITS_PER_FRAME, &advance);
+            if (advance < 0)
+            {
+               speex_warning ("Invalid wideband mode encountered. Corrupted stream?");
+               return -2;
+            } 
             advance -= (SB_SUBMODE_BITS+1);
             speex_bits_advance(bits, advance);
             wideband = speex_bits_unpack_unsigned(bits, 1);
             if (wideband)
             {
-               speex_error ("Corrupted stream?");
+               int submode;
+               int advance;
+               advance = submode = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
+               speex_mode_query(&speex_wb_mode, SPEEX_SUBMODE_BITS_PER_FRAME, &advance);
+               if (advance < 0)
+               {
+                  speex_warning ("Invalid wideband mode encountered: corrupted stream?");
+                  return -2;
+               } 
+               advance -= (SB_SUBMODE_BITS+1);
+               speex_bits_advance(bits, advance);
+               wideband = speex_bits_unpack_unsigned(bits, 1);
+               if (wideband)
+               {
+                  speex_warning ("More than to wideband layers found: corrupted stream?");
+                  return -2;
+               }
+
             }
          }
 
@@ -1158,7 +1163,6 @@
       int quant;
       quant = speex_bits_unpack_unsigned(bits, 4);
       ol_pitch_coef=0.066667*quant;
-      /*fprintf (stderr, "unquant pitch coef: %f\n", ol_pitch_coef);*/
    }
    
    /* Get global excitation gain */
@@ -1166,13 +1170,24 @@
       int qe;
       qe = speex_bits_unpack_unsigned(bits, 5);
       ol_gain = exp(qe/3.5);
-      /*printf ("decode_ol_gain: %f\n", ol_gain);*/
    }
 
    awk1=PUSH(stack, st->lpcSize+1, float);
    awk2=PUSH(stack, st->lpcSize+1, float);
    awk3=PUSH(stack, st->lpcSize+1, float);
 
+   if (st->submodeID==1)
+   {
+      int extra;
+      extra = speex_bits_unpack_unsigned(bits, 4);
+      if (extra==15)
+         st->dtx_enabled=1;
+      else
+         st->dtx_enabled=0;
+   }
+   if (st->submodeID>1)
+      st->dtx_enabled=0;
+
    /*Loop on subframes */
    for (sub=0;sub<st->nbSubframes;sub++)
    {
@@ -1326,9 +1341,7 @@
          } else {
             ener = ol_gain;
          }
-         
-         /*printf ("unquant_energy: %d %f\n", q_energy, ener);*/
-         
+                  
          if (SUBMODE(innovation_unquant))
          {
             /*Fixed codebook contribution*/
@@ -1364,7 +1377,7 @@
                g=1;
             for (i=0;i<st->subframeSize;i++)
             {
-               int tmp=exc[i];
+               float tmp=exc[i];
                exc[i]=.8*g*exc[i]*ol_gain + .6*g*st->voc_m1*ol_gain + .5*g*innov[i] - .5*g*st->voc_m2 + (1-g)*innov[i];
                st->voc_m1 = tmp;
                st->voc_m2=innov[i];
@@ -1375,7 +1388,6 @@
             for (i=0;i<st->subframeSize;i++)
                exc[i]+=innov[i];
          }
-         /*printf ("%f %f\n", ener, ol_gain);*/
          /* Decode second codebook (only for some modes) */
          if (SUBMODE(double_codebook))
          {
@@ -1436,11 +1448,6 @@
       st->pitch_gain_buf_idx = 0;
 
    st->last_ol_gain = ol_gain;
-
-
-#ifdef DEBUG
-printf ("last pitch: {%d, %f} buf=[%f %f %f]\n", st->last_pitch, st->last_pitch_gain, st->pitch_gain_buf[0], st->pitch_gain_buf[1], st->pitch_gain_buf[2]);
-#endif
 
    return 0;
 }

<p><p>1.46      +3 -1      speex/libspeex/nb_celp.h

Index: nb_celp.h
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/nb_celp.h,v
retrieving revision 1.45
retrieving revision 1.46
diff -u -r1.45 -r1.46
--- nb_celp.h	6 Jan 2003 05:56:56 -0000	1.45
+++ nb_celp.h	6 Jan 2003 20:43:48 -0000	1.46
@@ -132,7 +132,7 @@
    int    min_pitch;      /**< Minimum pitch value allowed */
    int    max_pitch;      /**< Maximum pitch value allowed */
    int    sampling_rate;
-   int    last_ol_gain;   /**< Open-loop gain for previous frame */
+   float  last_ol_gain;   /**< Open-loop gain for previous frame */
 
 
    float  gamma1;         /**< Perceptual filter: A(z/gamma1) */
@@ -169,6 +169,8 @@
    float  voc_m2;
    float  voc_mean;
    int    voc_offset;
+
+   int    dtx_enabled;
 } DecState;
 
 /** Initializes encoder state*/

<p><p>1.111     +2 -40     speex/libspeex/sb_celp.c

Index: sb_celp.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/sb_celp.c,v
retrieving revision 1.110
retrieving revision 1.111
diff -u -r1.110 -r1.111
--- sb_celp.c	6 Jan 2003 08:35:48 -0000	1.110
+++ sb_celp.c	6 Jan 2003 20:43:48 -0000	1.111
@@ -297,8 +297,6 @@
    {
       roots = lpc_to_lsp (st->lpc, st->lpcSize, st->lsp, 11, 0.02, stack);
       if (roots!=st->lpcSize) {
-         /*fprintf (stderr, "roots!=st->lpcSize (found only %d roots)\n", roots);*/
-
          /*If we can't find all LSP's, do some damage control and use a flat filter*/
          for (i=0;i<st->lpcSize;i++)
          {
@@ -333,7 +331,6 @@
             st->vbr_quality=10;
          if (st->vbr_quality<0)
             st->vbr_quality=0;
-         /*printf ("%f %f\n", st->abr_drift, st->vbr_quality);*/
       }
 
 
@@ -368,9 +365,7 @@
                break;
             modeid--;
          }
-         /*fprintf (stderr, "%f %d\n", low_qual, modeid);*/
          speex_encoder_ctl(state, SPEEX_SET_HIGH_MODE, &modeid);
-         /*fprintf (stderr, "%d %d\n", st->submodeID, modeid);*/
          if (st->abr_enabled)
          {
             int bitrate;
@@ -431,15 +426,7 @@
 
 
    /* LSP quantization */
-   SUBMODE(lsp_quant)(st->lsp, st->qlsp, st->lpcSize, bits);
-   
-   /*printf ("high_lsp:");
-   for (i=0;i<st->lpcSize;i++)
-      printf (" %f", st->lsp[i]);
-      printf ("\n");*/
-   /*for (i=0;i<st->lpcSize;i++)
-     st->qlsp[i]=st->lsp[i];*/
-   
+   SUBMODE(lsp_quant)(st->lsp, st->qlsp, st->lpcSize, bits);   
 
    if (st->first)
    {
@@ -561,21 +548,7 @@
 
          scale = gc*sqrt(1+el)/filter_ratio;
          scale_1 = 1/scale;
-#if 0
-         if (0 && rand()%5==0)
-         {
-            float sc = 1/sqrt(.1+eh/st->subframeSize);
-            if (rand()&1)
-               sc=-sc;
-            for (i=0;i<st->subframeSize;i++)
-            {
-               float tmp=exc[i]*sc;
-               if (i%8==0)
-                  printf ("\nhexc");
-               printf (" %f", tmp);
-            }
-         }
-#endif
+
          for (i=0;i<st->subframeSize;i++)
             exc[i]=0;
          exc[0]=1;
@@ -639,17 +612,6 @@
                exc[i] += innov2[i];
          }
 
-#if 0
-         {
-            float en=0;
-            for (i=0;i<st->subframeSize;i++)
-               en+=exc[i]*exc[i];
-            en=sqrt(eh/(1+en));
-            for (i=0;i<st->subframeSize;i++)
-               exc[i]*=en;
-            printf ("correction high: %f\n", en);
-         }
-#endif
       }
 
          /*Keep the previous memory*/

<p><p>1.67      +10 -2     speex/src/speexdec.c

Index: speexdec.c
===================================================================
RCS file: /usr/local/cvsroot/speex/src/speexdec.c,v
retrieving revision 1.66
retrieving revision 1.67
diff -u -r1.66 -r1.67
--- speexdec.c	5 Jan 2003 08:46:33 -0000	1.66
+++ speexdec.c	6 Jan 2003 20:43:48 -0000	1.67
@@ -553,12 +553,20 @@
                speex_bits_read_from(&bits, (char*)op.packet, op.bytes);
                for (j=0;j<nframes;j++)
                {
+                  int ret;
                   /*Decode frame*/
                   if (!lost)
-                     speex_decode(st, &bits, output);
+                     ret = speex_decode(st, &bits, output);
                   else
-                     speex_decode(st, NULL, output);
+                     ret = speex_decode(st, NULL, output);
 
+                  if (ret==-1)
+                     break;
+                  if (ret==-2)
+                  {
+                     fprintf (stderr, "Decoding error: corrupted stream?\n");
+                     break;
+                  }
                   if (channels==2)
                      speex_decode_stereo(output, frame_size, &stereo);
 

<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