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

Jean-Marc Valin jm at xiph.org
Sat Dec 14 22:01:45 PST 2002



jm          02/12/15 01:01:45

  Modified:    libspeex nb_celp.c nb_celp.h
               src      speexenc.c
  Log:
  Average bit-rate (ABR) now seems to work good for narrowband (no wideband
  yet, but shouldn't be hard)

Revision  Changes    Path
1.95      +20 -5     speex/libspeex/nb_celp.c

Index: nb_celp.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/nb_celp.c,v
retrieving revision 1.94
retrieving revision 1.95
diff -u -r1.94 -r1.95
--- nb_celp.c	14 Dec 2002 06:29:08 -0000	1.94
+++ nb_celp.c	15 Dec 2002 06:01:45 -0000	1.95
@@ -375,10 +375,17 @@
       
       if (st->abr_enabled)
       {
-         if (st->abr_drift>0)
-            st->vbr_quality -= .02;
-         else
-            st->vbr_quality += .02;
+         float qual_change=0;
+         if (st->abr_drift2 * st->abr_drift > 0)
+         {
+            /* Only adapt if long-term and short-term drift are the same sign */
+            qual_change = -.00001*st->abr_drift/(1+st->abr_count);
+            if (qual_change>.1)
+               qual_change=.1;
+            if (qual_change<-.1)
+               qual_change=-.1;
+         }
+         st->vbr_quality += qual_change;
          if (st->vbr_quality>10)
             st->vbr_quality=10;
          if (st->vbr_quality<0)
@@ -417,6 +424,8 @@
             int bitrate;
             speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);
             st->abr_drift+=(bitrate-st->abr_enabled);
+            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 {
@@ -1454,8 +1463,10 @@
       break;
    case SPEEX_SET_ABR:
       st->abr_enabled = (*(int*)ptr);
+      st->vbr_enabled = 1;
       {
-         int i=10, rate, target, vbr_qual;
+         int i=10, rate, target;
+         float vbr_qual;
          target = (*(int*)ptr);
          while (i>=0)
          {
@@ -1468,7 +1479,11 @@
          vbr_qual=i;
          if (vbr_qual<0)
             vbr_qual=0;
+         fprintf (stderr, "%f\n", vbr_qual);
          speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual);
+         st->abr_count=0;
+         st->abr_drift=0;
+         st->abr_drift2=0;
       }
       
       break;

<p><p>1.40      +2 -0      speex/libspeex/nb_celp.h

Index: nb_celp.h
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/nb_celp.h,v
retrieving revision 1.39
retrieving revision 1.40
diff -u -r1.39 -r1.40
--- nb_celp.h	13 Dec 2002 22:59:27 -0000	1.39
+++ nb_celp.h	15 Dec 2002 06:01:45 -0000	1.40
@@ -105,6 +105,8 @@
    int    vad_enabled;    /**< 1 for enabling VAD, 0 otherwise */
    int    abr_enabled;    /**< 1 for enabling ABR, 0 otherwise */
    float  abr_drift;
+   float  abr_drift2;
+   float  abr_count;
    int    complexity;     /**< Complexity setting (0-10 from least complex to most complex) */
    int    sampling_rate;
 

<p><p>1.62      +34 -13    speex/src/speexenc.c

Index: speexenc.c
===================================================================
RCS file: /usr/local/cvsroot/speex/src/speexenc.c,v
retrieving revision 1.61
retrieving revision 1.62
diff -u -r1.61 -r1.62
--- speexenc.c	12 Dec 2002 07:51:35 -0000	1.61
+++ speexenc.c	15 Dec 2002 06:01:45 -0000	1.62
@@ -144,6 +144,7 @@
    printf (" --quality n        Encoding quality (0-10), default 3\n"); 
    printf (" --bitrate n        Encoding bit-rate (use bit-rate n or lower)\n"); 
    printf (" --vbr              Enable variable bit-rate (VBR)\n"); 
+   printf (" --abr rate         Enable average bit-rate (ABR) at rate bps\n"); 
    printf (" --vad              Enable voice activity detection (VAD)\n"); 
    printf (" --comp n           Set encoding complexity (0-10), default 3\n"); 
    printf (" --nframes n        Number of frames per Ogg packet (1-10), default 1\n"); 
@@ -178,6 +179,7 @@
    float input[MAX_FRAME_SIZE];
    int frame_size;
    int vbr_enabled=0;
+   int abr_enabled=0;
    int vad_enabled=0;
    int nbBytes;
    SpeexMode *mode=NULL;
@@ -190,6 +192,7 @@
       {"ultra-wideband", no_argument, NULL, 0},
       {"narrowband", no_argument, NULL, 0},
       {"vbr", no_argument, NULL, 0},
+      {"abr", required_argument, NULL, 0},
       {"vad", no_argument, NULL, 0},
       {"quality", required_argument, NULL, 0},
       {"bitrate", required_argument, NULL, 0},
@@ -230,7 +233,7 @@
    int close_in=0, close_out=0;
    int eos=0;
    int bitrate=0;
-
+   int cumul_bits=0, enc_frames=0;
    comment_init(&comments, &comments_length, vendor_string);
 
    /*Process command-line options*/
@@ -256,6 +259,14 @@
          } else if (strcmp(long_options[option_index].name,"vbr")==0)
          {
             vbr_enabled=1;
+         } else if (strcmp(long_options[option_index].name,"abr")==0)
+         {
+            abr_enabled=atoi(optarg);
+            if (!abr_enabled)
+            {
+               fprintf (stderr, "Invalid ABR value: %d\n", abr_enabled);
+               exit(1);
+            }
          } else if (strcmp(long_options[option_index].name,"vad")==0)
          {
             vad_enabled=1;
@@ -534,6 +545,19 @@
    speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &complexity);
    speex_encoder_ctl(st, SPEEX_SET_SAMPLING_RATE, &rate);
 
+   if (quality >= 0)
+   {
+      if (vbr_enabled)
+         speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_quality);
+      else
+         speex_encoder_ctl(st, SPEEX_SET_QUALITY, &quality);
+   }
+   if (bitrate)
+   {
+      if (quality >= 0 && vbr_enabled)
+         fprintf (stderr, "--bitrate option is overriding --quality\n");
+      speex_encoder_ctl(st, SPEEX_SET_BITRATE, &bitrate);
+   }
    if (vbr_enabled)
    {
       int tmp;
@@ -546,18 +570,9 @@
       tmp=1;
       speex_encoder_ctl(st, SPEEX_SET_VAD, &tmp);
    }
-   if (quality >= 0)
+   if (abr_enabled)
    {
-      if (vbr_enabled)
-         speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_quality);
-      else
-         speex_encoder_ctl(st, SPEEX_SET_QUALITY, &quality);
-   }
-   if (bitrate)
-   {
-      if (quality >= 0 && vbr_enabled)
-         fprintf (stderr, "--bitrate option is overriding --quality\n");
-      speex_encoder_ctl(st, SPEEX_SET_BITRATE, &bitrate);
+      speex_encoder_ctl(st, SPEEX_SET_ABR, &abr_enabled);
    }
 
    speex_bits_init(&bits);
@@ -579,7 +594,13 @@
          char ch=13;
          speex_encoder_ctl(st, SPEEX_GET_BITRATE, &tmp);
          fputc (ch, stderr);
-         fprintf (stderr, "Bitrate is use: %d bps     ", tmp);
+         cumul_bits += tmp;
+         enc_frames++;
+         if (vad_enabled || vbr_enabled || abr_enabled)
+            fprintf (stderr, "Bitrate is use: %d bps  (average %d bps)   ", tmp, cumul_bits/enc_frames);
+         else
+            fprintf (stderr, "Bitrate is use: %d bps     ", tmp);
+         
       }
 
       if (read_samples(fin,frame_size,fmt,chan,lsb,input))

<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