shithub: aacenc

Download patch

ref: 6bd575e2b1aded2f8581b7863d3cce6f7fb41d89
parent: 9100891c84b449d0de9615aba688364dd630b068
author: Krzysztof Nikiel <knik@users.sourceforge.net>
date: Tue Aug 15 14:03:35 EDT 2017

block switching parametrized: more long blocks at lower bitrates, more short blocks at higher bitrates

--- a/libfaac/aacquant.c
+++ b/libfaac/aacquant.c
@@ -101,7 +101,7 @@
     for (sb = 0; sb < coderInfo->nr_of_sfb; sb++)
         scale_factor[sb] = 0;
 
-    if (BlocQuant(coderInfo, xr, xi, (double)aacquantCfg->quality/100, aacquantCfg->pow43))
+    if (BlocQuant(coderInfo, xr, xi, (double)aacquantCfg->quality/DEFQUAL, aacquantCfg->pow43))
     {
         UpdateRequant(coderInfo, xi, aacquantCfg->pow43);
 
--- a/libfaac/aacquant.h
+++ b/libfaac/aacquant.h
@@ -44,6 +44,8 @@
   } AACQuantCfg;
 #pragma pack(pop)
 
+enum {DEFQUAL = 100, MAXQUAL = 2000, MINQUAL = 10};
+
 void AACQuantizeInit(CoderInfo *coderInfo, unsigned int numChannels,
 		     AACQuantCfg *aacquantCfg);
 void AACQuantizeEnd(CoderInfo *coderInfo, unsigned int numChannels,
--- a/libfaac/blockswitch.c
+++ b/libfaac/blockswitch.c
@@ -62,12 +62,12 @@
   }
 }
 
-static void PsyCheckShort(PsyInfo * psyInfo)
+static void PsyCheckShort(PsyInfo * psyInfo, double quality)
 {
   enum {PREVS = 2, NEXTS = 2};
   psydata_t *psydata = psyInfo->data;
   int lastband = psydata->lastband;
-  int firstband = 1;
+  int firstband = 2;
   int sfb, win;
   psyfloat *lasteng;
 
@@ -96,7 +96,7 @@
               volchg += fabs(eng[sfb] - lasteng[sfb]);
           }
 
-          if ((volchg / toteng) > 1.5)
+          if ((volchg / toteng * quality) > 2.5)
           {
               psyInfo->block_type = ONLY_SHORT_WINDOW;
               break;
@@ -227,7 +227,9 @@
 static void PsyCalculate(ChannelInfo * channelInfo, GlobalPsyInfo * gpsyInfo,
 			 PsyInfo * psyInfo, int *cb_width_long, int
 			 num_cb_long, int *cb_width_short,
-			 int num_cb_short, unsigned int numChannels)
+			 int num_cb_short, unsigned int numChannels,
+			 double quality
+			)
 {
   unsigned int channel;
 
@@ -243,8 +245,8 @@
 	int leftChan = channel;
 	int rightChan = channelInfo[channel].paired_ch;
 
-	PsyCheckShort(&psyInfo[leftChan]);
-	PsyCheckShort(&psyInfo[rightChan]);
+	PsyCheckShort(&psyInfo[leftChan], quality);
+	PsyCheckShort(&psyInfo[rightChan], quality);
       }
       else if (!channelInfo[channel].cpe &&
 	       channelInfo[channel].lfe)
@@ -254,7 +256,7 @@
       }
       else if (!channelInfo[channel].cpe)
       {				/* SCE */
-	PsyCheckShort(&psyInfo[channel]);
+	PsyCheckShort(&psyInfo[channel], quality);
       }
     }
   }
--- a/libfaac/blockswitch.h
+++ b/libfaac/blockswitch.h
@@ -67,7 +67,7 @@
 void (*PsyCalculate) (ChannelInfo *channelInfo, GlobalPsyInfo *gpsyInfo,
 		PsyInfo *psyInfo, int *cb_width_long, int num_cb_long,
 		int *cb_width_short, int num_cb_short,
-		unsigned int numChannels);
+		unsigned int numChannels, double quality);
 void (*PsyBufferUpdate) ( FFT_Tables *fft_tables, GlobalPsyInfo * gpsyInfo, PsyInfo * psyInfo,
 		double *newSamples, unsigned int bandwidth,
 		int *cb_width_short, int num_cb_short);
--- a/libfaac/frame.c
+++ b/libfaac/frame.c
@@ -66,7 +66,6 @@
 
 // default bandwidth/samplerate ratio
 static const double bwfac = 0.42;
-enum {MAXQUAL = 2000, MINQUAL = 10};
 
 int FAACAPI faacEncGetVersion( char **faac_id_string,
 			      				char **faac_copyright_string)
@@ -324,7 +323,7 @@
     hEncoder->config.useTns = 0;
     hEncoder->config.bitRate = 0; /* default bitrate / channel */
     hEncoder->config.bandWidth = bwfac * hEncoder->sampleRate;
-    hEncoder->config.quantqual = 100;
+    hEncoder->config.quantqual = DEFQUAL;
     hEncoder->config.psymodellist = (psymodellist_t *)psymodellist;
     hEncoder->config.psymodelidx = 0;
     hEncoder->psymodel =
@@ -590,7 +589,7 @@
     hEncoder->psymodel->PsyCalculate(channelInfo, &hEncoder->gpsyInfo, hEncoder->psyInfo,
         hEncoder->srInfo->cb_width_long, hEncoder->srInfo->num_cb_long,
         hEncoder->srInfo->cb_width_short,
-        hEncoder->srInfo->num_cb_short, numChannels);
+        hEncoder->srInfo->num_cb_short, numChannels, (double)hEncoder->aacquantCfg.quality / DEFQUAL);
 
     hEncoder->psymodel->BlockSwitch(coderInfo, hEncoder->psyInfo, numChannels);