[xiph-cvs] cvs commit: speex/libspeex nb_celp.c nb_celp.h sb_celp.c sb_celp.h speex.h speex_bits.h vbr.c

Jean-Marc Valin jm at xiph.org
Thu Nov 14 22:26:50 PST 2002



jm          02/11/15 01:26:50

  Modified:    libspeex nb_celp.c nb_celp.h sb_celp.c sb_celp.h speex.h
                        speex_bits.h vbr.c
  Log:
  Fixed a bug in the VBR analyzer, trying to re-tune it... also, started
  implementing VBR for wideband (still a big kludge for now).

Revision  Changes    Path
1.83      +21 -34    speex/libspeex/nb_celp.c

Index: nb_celp.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/nb_celp.c,v
retrieving revision 1.82
retrieving revision 1.83
diff -u -r1.82 -r1.83
--- nb_celp.c	14 Nov 2002 04:49:14 -0000	1.82
+++ nb_celp.c	15 Nov 2002 06:26:50 -0000	1.83
@@ -226,7 +226,6 @@
    int ol_pitch;
    float ol_pitch_coef;
    float ol_gain;
-   float delta_qual=0;
    float *res, *target, *mem;
    void *stack;
    float *syn_resp;
@@ -365,44 +364,30 @@
    /*Experimental VBR stuff*/
    if (st->vbr)
    {
-      delta_qual = vbr_analysis(st->vbr, in, st->frameSize, ol_pitch, ol_pitch_coef);
+      st->relative_quality = vbr_analysis(st->vbr, in, st->frameSize, ol_pitch, ol_pitch_coef);
       /*if (delta_qual<0)*/
       /*  delta_qual*=.1*(3+st->vbr_quality);*/
       if (st->vbr_enabled) 
       {
-         if (0) 
+         int mode;
+         mode = 7;
+         while (mode)
          {
-         int qual = (int)floor(st->vbr_quality+delta_qual+.5);
-         if (qual<1 && delta_qual>-3.5)
-            qual=1;
-         if (qual<0)
-            qual=0;
-         if (qual>10)
-            qual=10;
-         if (qual==10 && st->vbr_quality<10)
-            qual=9;
-         speex_encoder_ctl(state, SPEEX_SET_QUALITY, &qual);
-         } else {
-            int vqual, mode;
-            float level = delta_qual+7;
-            /*fprintf(stderr, "%f\n", level);*/
-            vqual = (int)floor(.5+st->vbr_quality);
-            mode = 7;
-            while (mode)
-            {
-               int v1;
-               float thresh;
-               v1=(int)floor(st->vbr_quality);
-               if (v1==10)
-                  thresh = vbr_nb_thresh[mode][v1];
-               else
-                  thresh = (st->vbr_quality-v1)*vbr_nb_thresh[mode][v1+1] + (1+v1-st->vbr_quality)*vbr_nb_thresh[mode][v1];
-               if (level > thresh)
-                  break;
-               mode--;
-            }
-            speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);
+            int v1;
+            float thresh;
+            v1=(int)floor(st->vbr_quality);
+            if (v1==10)
+               thresh = vbr_nb_thresh[mode][v1];
+            else
+               thresh = (st->vbr_quality-v1)*vbr_nb_thresh[mode][v1+1] + (1+v1-st->vbr_quality)*vbr_nb_thresh[mode][v1];
+            if (st->relative_quality > thresh)
+               break;
+            mode--;
          }
+         /*fprintf (stderr, "%f %d\n", st->relative_quality, mode);*/
+         speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);
+      } else {
+         st->relative_quality = -1;
       }
    }
    /*printf ("VBR quality = %f\n", vbr_qual);*/
@@ -536,7 +521,6 @@
          st->pi_gain[sub] += tmp*st->interp_qlpc[i];
          tmp = -tmp;
       }
-     
 
       /* Compute bandwidth-expanded (unquantized) LPCs for perceptual weighting */
       bw_lpc(st->gamma1, st->interp_lpc, st->bw_lpc1, st->lpcSize);
@@ -1358,6 +1342,9 @@
          for (i=0;i<st->frameSize;i++)
             e[i]=st->innov[i];
       }
+      break;
+   case SPEEX_GET_RELATIVE_QUALITY:
+      (*(float*)ptr)=st->relative_quality;
       break;
    default:
       fprintf(stderr, "Unknown nb_ctl request: %d\n", request);

<p><p>1.35      +2 -1      speex/libspeex/nb_celp.h

Index: nb_celp.h
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/nb_celp.h,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -r1.34 -r1.35
--- nb_celp.h	10 Nov 2002 05:17:52 -0000	1.34
+++ nb_celp.h	15 Nov 2002 06:26:50 -0000	1.35
@@ -99,7 +99,8 @@
    float *pi_gain;        /**< Gain of LPC filter at theta=pi (fe/2) */
 
    VBRState *vbr;         /**< State of the VBR data */
-   float    vbr_quality;    /**< Quality setting for VBR encoding */
+   float  vbr_quality;    /**< Quality setting for VBR encoding */
+   float  relative_quality; /**< Relative quality that will be needed by VBR */
    int    vbr_enabled;    /**< 1 for enabling VBR, 0 otherwise */
    int    complexity;     /**< Complexity setting (0-10 from least complex to most complex) */
    int    sampling_rate;

<p><p>1.94      +41 -5     speex/libspeex/sb_celp.c

Index: sb_celp.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/sb_celp.c,v
retrieving revision 1.93
retrieving revision 1.94
diff -u -r1.93 -r1.94
--- sb_celp.c	11 Nov 2002 01:08:29 -0000	1.93
+++ sb_celp.c	15 Nov 2002 06:26:50 -0000	1.94
@@ -33,7 +33,7 @@
 #include <stdlib.h>
 #include <math.h>
 #include <stdio.h>
-#include "nb_celp.h"
+/*#include "nb_celp.h"*/
 #include "sb_celp.h"
 #include "stdlib.h"
 #include "filters.h"
@@ -217,6 +217,10 @@
    st->mem_sp = (float*)speex_alloc(st->lpcSize*sizeof(float));
    st->mem_sp2 = (float*)speex_alloc(st->lpcSize*sizeof(float));
    st->mem_sw = (float*)speex_alloc(st->lpcSize*sizeof(float));
+
+   st->vbr_quality = 8;
+   st->vbr_enabled = 0;
+
    st->complexity=2;
    speex_decoder_ctl(st->st_low, SPEEX_GET_SAMPLING_RATE, &st->sampling_rate);
    st->sampling_rate*=2;
@@ -288,9 +292,6 @@
    /* Encode the narrowband part*/
    speex_encode(st->st_low, st->x0d, bits);
 
-   speex_bits_pack(bits, 1, 1);
-   speex_bits_pack(bits, st->submodeID, SB_SUBMODE_BITS);
-
    /* High-band buffering / sync with low band */
    for (i=0;i<st->windowSize-st->frame_size;i++)
       st->high[i] = st->high[st->frame_size+i];
@@ -344,6 +345,41 @@
    for (i=0;i<st->lpcSize;i++)
       st->lsp[i] = acos(st->lsp[i]);
 
+   /* VBR code */
+   if (0){
+      float e_low=0, e_high=0;
+      float ratio;
+      float low_qual;
+      for (i=0;i<st->frame_size;i++)
+      {
+         e_low  += st->x0d[i]* st->x0d[i];
+         e_high += st->high[i]* st->high[i];
+      }
+      ratio = log((1+e_high)/(1+e_low));
+      speex_encoder_ctl(st->st_low, SPEEX_GET_RELATIVE_QUALITY, &low_qual);
+      if (ratio<-4)
+         ratio=-4;
+      if (ratio>0)
+         ratio=0;
+      /*if (ratio>-2)*/
+      low_qual+=1.0*(ratio+2);
+      {
+         int high_mode=2;
+         if (low_qual>10)
+            high_mode=4;
+         else if (low_qual>7.5)
+            high_mode=3;
+         else if (low_qual>5)
+            high_mode=2;
+         /*high_mode=1;*/
+         speex_encoder_ctl(st, SPEEX_SET_HIGH_MODE, &high_mode);
+      }
+      /*fprintf (stderr, "%f %f\n", ratio, low_qual);*/
+   }
+
+   speex_bits_pack(bits, 1, 1);
+   speex_bits_pack(bits, st->submodeID, SB_SUBMODE_BITS);
+
    /* If null mode (no transmission), just set a couple things to zero*/
    if (st->submodes[st->submodeID] == NULL)
    {
@@ -933,7 +969,7 @@
    case SPEEX_SET_VBR_QUALITY:
       {
          int q;
-         float qual = (*(float*)ptr)+1;
+         float qual = (*(float*)ptr)+.5;
          if (qual>10)
             qual=10;
          q=(int)floor(.5+*(float*)ptr);

<p><p>1.31      +3 -0      speex/libspeex/sb_celp.h

Index: sb_celp.h
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/sb_celp.h,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -r1.30 -r1.31
--- sb_celp.h	10 Nov 2002 05:17:52 -0000	1.30
+++ sb_celp.h	15 Nov 2002 06:26:50 -0000	1.31
@@ -90,6 +90,9 @@
    float *mem_sw;              /**< Perceptual signal memory */
    float *pi_gain;
 
+   float  vbr_quality;         /**< Quality setting for VBR encoding */
+   int    vbr_enabled;         /**< 1 for enabling VBR, 0 otherwise */
+
    SpeexSubmode **submodes;
    int    submodeID;
    int    complexity;

<p><p>1.66      +1 -0      speex/libspeex/speex.h

Index: speex.h
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/speex.h,v
retrieving revision 1.65
retrieving revision 1.66
diff -u -r1.65 -r1.66
--- speex.h	10 Nov 2002 05:17:52 -0000	1.65
+++ speex.h	15 Nov 2002 06:26:50 -0000	1.66
@@ -102,6 +102,7 @@
 #define SPEEX_SET_SAMPLING_RATE 24
 #define SPEEX_GET_SAMPLING_RATE 25
 
+#define SPEEX_GET_RELATIVE_QUALITY 27
 
    /* Used internally, not to be used in applications */
 #define SPEEX_GET_PI_GAIN 100

<p><p>1.16      +1 -1      speex/libspeex/speex_bits.h

Index: speex_bits.h
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/speex_bits.h,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- speex_bits.h	11 Oct 2002 03:39:34 -0000	1.15
+++ speex_bits.h	15 Nov 2002 06:26:50 -0000	1.16
@@ -41,7 +41,7 @@
 #endif
 
 /** Maximum size of the bit-stream (for fixed-size allocation) */
-#define MAX_BYTES_PER_FRAME 1000
+#define MAX_BYTES_PER_FRAME 2000
 
 /** Bit-packing data structure representing (part of) a bit-stream. */
 typedef struct SpeexBits {

<p><p>1.11      +26 -23    speex/libspeex/vbr.c

Index: vbr.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/vbr.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- vbr.c	14 Nov 2002 04:49:14 -0000	1.10
+++ vbr.c	15 Nov 2002 06:26:50 -0000	1.11
@@ -47,13 +47,13 @@
 
 float vbr_nb_thresh[8][11]={
    {-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0}, /* silence */
-   { 4.5,  3.5,  2.5,  1.5,  0.5,  0.0,  0.0,  0.0,  0.0,  0.0, -1.0}, /*  2 kbps */
-   { 9.0,  7.5,  6.5,  5.5,  5.0,  4.0,  4.5,  3.0,  2.0,  1.0,  0.0}, /*  6 kbps */
-   {11.0,  9.5,  8.5,  7.5,  7.0,  6.5,  6.0,  5.0,  4.0,  3.0,  1.0}, /*  8 kbps */
-   {11.0, 11.0, 11.0,  9.5,  8.5,  7.5,  6.5,  6.0,  5.0,  4.0,  2.0}, /* 11 kbps */
-   {11.0, 11.0, 11.0, 11.0,  9.5,  9.0,  8.0,  7.5,  6.5,  5.0,  3.0}, /* 15 kbps */
+   { 3.9,  2.5,  2.0,  1.5,  0.5,  0.0,  0.0,  0.0,  0.0,  0.0, -1.0}, /*  2 kbps */
+   { 8.0,  6.0,  3.9,  4.5,  4.0,  3.5,  3.0,  2.5,  2.0,  1.0,  0.0}, /*  6 kbps */
+   {11.0,  8.5,  7.5,  7.0,  6.5,  6.0,  5.5,  5.0,  4.0,  3.0,  1.0}, /*  8 kbps */
+   {11.0, 11.0,  9.9,  9.0,  8.0,  7.0,  6.5,  6.0,  5.0,  4.0,  2.0}, /* 11 kbps */
+   {11.0, 11.0, 11.0, 11.0,  9.5,  9.0,  8.0,  7.0,  6.5,  5.0,  3.0}, /* 15 kbps */
    {11.0, 11.0, 11.0, 11.0, 11.0, 11.0,  9.5,  8.5,  8.0,  6.5,  4.0}, /* 18 kbps */
-   {11.0, 11.0, 11.0, 11.0, 11.0, 11.0, 11.0, 11.0,  9.5,  7.5,  5.5}  /* 24 kbps */ 
+   {11.0, 11.0, 11.0, 11.0, 11.0, 11.0, 11.0, 11.0,  9.8,  7.5,  5.5}  /* 24 kbps */ 
 };
 
 
@@ -108,7 +108,7 @@
 {
    int i;
    float ener=0, ener1=0, ener2=0;
-   float qual=0;
+   float qual=7;
    int va;
    float log_energy;
    float non_st=0;
@@ -155,11 +155,11 @@
 
    /* Checking for "pseudo temporal masking" */
    if (ener < .1*vbr->average_energy)
-      qual -= .7;
+      qual -= .5;
    if (ener < .01*vbr->average_energy)
-      qual -= .7;
+      qual -= .5;
    if (ener < .001*vbr->average_energy)
-      qual -= .7;
+      qual -= .5;
    /* Checking for very low absolute energy */
    if (ener < 30000)
    {
@@ -174,7 +174,9 @@
          qual += .7;
       if (ener > vbr->last_energy*1.8)
          qual += .7;
-      if (ener > 3*vbr->average_energy)
+      if (ener > 2*vbr->average_energy)
+         qual += .7;
+      if (ener > 4*vbr->average_energy)
          qual += .7;
       if (ener2 > 1.6*ener1)
          qual += .7;
@@ -184,23 +186,24 @@
       if (ener < .3*vbr->last_energy)
          qual -= .6;
    }
+   vbr->last_energy = ener;
    vbr->soft_pitch = .6*vbr->soft_pitch + .4*pitch_coef;
-   qual += 1.0*((pitch_coef-.4) + (vbr->soft_pitch-.4));
+   qual += 2.5*((pitch_coef-.4) + (vbr->soft_pitch-.4));
 
    if (qual < vbr->last_quality)
       qual = .5*qual + .5*vbr->last_quality;
-   if (qual<-3)
-      qual=-3;
-   if (qual>3)
-      qual=3;
-
+   if (qual<4)
+      qual=4;
+   if (qual>10)
+      qual=10;
+   
    if (vbr->consec_noise>=1)
-      qual-=1.2;
-   if (vbr->consec_noise>=4)
-      qual-=1.2;
-   if (vbr->consec_noise>=8)
-      qual-=1.2;
-
+      qual-=1.3;
+   if (vbr->consec_noise>=5)
+      qual-=1.3;
+   if (vbr->consec_noise>=12)
+      qual-=1.3;
+   
    vbr->last_pitch_coef = pitch_coef;
    vbr->last_quality = qual;
 

<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