shithub: aacenc

Download patch

ref: e8526a099fa7669e74e1fbb91bf6d78cb27dcd33
parent: 51bbcf9fd8b246f6e9b3f3bfed51e7346295b358
author: knik <knik>
date: Sun Oct 12 10:30:29 EDT 2003

more accurate average bitrate control

--- a/frontend/main.c
+++ b/frontend/main.c
@@ -16,7 +16,7 @@
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
- * $Id: main.c,v 1.46 2003/09/24 16:30:34 knik Exp $
+ * $Id: main.c,v 1.47 2003/10/12 14:30:29 knik Exp $
  */
 
 #ifdef _MSC_VER
@@ -341,7 +341,7 @@
     {
         printf("\nUsage: %s -options infile outfile\n", progName);
         printf("Options:\n");
-        printf("  -a <x>\tSet average bitrate to approximately x kbps/channel.\n");
+        printf("  -a <x>\tSet average bitrate to x kbps/channel.\n");
         printf("  -c <bandwidth>\tSet the bandwidth in Hz. (default=automatic)\n");
         printf("  -q <quality>\tSet quantizer quality.\n");
 #if !DEFAULT_TNS
@@ -495,7 +495,8 @@
     quantqual = myFormat->quantqual;
     bitRate = myFormat->bitRate;
     if (bitRate)
-        fprintf(stderr, "Approximate ABR: %d kbps/channel\n", bitRate/1000);
+      fprintf(stderr, "Average bitrate: %d kbps/channel\n",
+	      (bitRate + 500)/1000);
     fprintf(stderr, "Quantization quality: %ld\n", quantqual);
     fprintf(stderr, "Bandwidth: %d Hz\n", cutOff);
     fprintf(stderr, "Object type: ");
@@ -678,6 +679,9 @@
 
 /*
 $Log: main.c,v $
+Revision 1.47  2003/10/12 14:30:29  knik
+more accurate average bitrate control
+
 Revision 1.46  2003/09/24 16:30:34  knik
 Added option to enforce block type.
 
--- a/libfaac/frame.c
+++ b/libfaac/frame.c
@@ -16,7 +16,7 @@
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
- * $Id: frame.c,v 1.47 2003/09/24 16:26:54 knik Exp $
+ * $Id: frame.c,v 1.48 2003/10/12 14:29:53 knik Exp $
  */
 
 /*
@@ -229,6 +229,8 @@
     if (config->quantqual < 10)
       config->quantqual = 10;
     hEncoder->config.quantqual = config->quantqual;
+    /* set quantization quality */
+    hEncoder->aacquantCfg.quality = config->quantqual;
 
     // reset psymodel
     hEncoder->psymodel->PsyEnd(&hEncoder->gpsyInfo, hEncoder->psyInfo, hEncoder->numChannels);
@@ -661,7 +663,6 @@
     MSEncode(coderInfo, channelInfo, hEncoder->freqBuff, numChannels, allowMidside);
 
     /* Quantize and code the signal */
-    hEncoder->aacquantCfg.quality = hEncoder->config.quantqual;
     for (channel = 0; channel < numChannels; channel++) {
         if (coderInfo[channel].block_type == ONLY_SHORT_WINDOW) {
             AACQuantize(&coderInfo[channel], &hEncoder->psyInfo[channel],
@@ -742,10 +743,28 @@
     /* Close the bitstream and return the number of bytes written */
     frameBytes = CloseBitStream(bitStream);
 
-#ifdef _DEBUG
-    printf("%4d %4d\n", hEncoder->frameNum-3, frameBytes);
-#endif
+    /* Adjust quality to get correct average bitrate */
+    if (hEncoder->config.bitRate)
+    {
+      double fix;
+      int desbits = numChannels * ((hEncoder->config.bitRate * 1024)
+				   / hEncoder->sampleRate);
 
+      hEncoder->bitDiff += frameBytes * 8;
+      hEncoder->bitDiff -= desbits;
+
+      fix = (double)hEncoder->bitDiff / desbits;
+      fix *= 0.01;
+      fix = max(fix, -0.2);
+      fix = min(fix, 0.2);
+
+      hEncoder->aacquantCfg.quality *= (1.0 - fix);
+      if (hEncoder->aacquantCfg.quality > 200)
+        hEncoder->aacquantCfg.quality = 200;
+      if (hEncoder->aacquantCfg.quality < 70)
+        hEncoder->aacquantCfg.quality = 70;
+    }
+
     return frameBytes;
 }
 
@@ -857,6 +876,9 @@
 
 /*
 $Log: frame.c,v $
+Revision 1.48  2003/10/12 14:29:53  knik
+more accurate average bitrate control
+
 Revision 1.47  2003/09/24 16:26:54  knik
 faacEncStruct: quantizer specific data enclosed in AACQuantCfg structure.
 Added config option to enforce block type.
--- a/libfaac/frame.h
+++ b/libfaac/frame.h
@@ -16,7 +16,7 @@
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
- * $Id: frame.h,v 1.23 2003/09/24 16:27:48 knik Exp $
+ * $Id: frame.h,v 1.24 2003/10/12 14:29:54 knik Exp $
  */
 
 #ifndef FRAME_H
@@ -117,6 +117,9 @@
 
     /* quantizer specific config */
     AACQuantCfg aacquantCfg;
+
+    /* output bits difference in average bitrate mode */
+    int bitDiff;
 } faacEncStruct, *faacEncHandle;
 
 int FAACAPI faacEncGetDecoderSpecificInfo(faacEncHandle hEncoder,