Encoder ELD auto configurator

* AAC-Encoder

   - Introduce ELD auto configurator to get predefined configuration at given
     bitrate and sampling rate. The configurator selects SBR on/off and if SBR
     enabled distinguishes between dualrate and downsampled SBR. In default
     configuration the ELD auto configurator is enabled.
     Modified file(s):
        libAACenc\include\aacenc_lib.h
        libAACenc\src\aacenc_lib.cpp

Bug 9428126

Change-Id: I01acc9c37e57c4154b3a818d496d538b7f8b227d
diff --git a/libAACenc/include/aacenc_lib.h b/libAACenc/include/aacenc_lib.h
index fcdaca5..f693bf5 100644
--- a/libAACenc/include/aacenc_lib.h
+++ b/libAACenc/include/aacenc_lib.h
@@ -342,6 +342,33 @@
 we recommended to activate this feature.
 \code aacEncoder_SetParam(hAacEncoder, AACENC_AFTERBURNER, 1); \endcode
 
+\subsection encELD ELD Auto Configuration Mode
+For ELD configuration a so called auto configurator is available which configures SBR and the SBR ratio by itself.
+The configurator is used when the encoder parameter ::AACENC_SBR_MODE and ::AACENC_SBR_RATIO are not set explicitely.
+
+Based on sampling rate and chosen bitrate per channel a reasonable SBR configuration will be used.
+\verbatim
+------------------------------------------------------------
+  Sampling Rate  | Channel Bitrate |  SBR |       SBR Ratio
+-----------------+-----------------+------+-----------------
+ ]min, 16] kHz   |     min - 27999 |   on | downsampled SBR
+                 |   28000 -   max |  off |             ---
+-----------------+-----------------+------+-----------------
+ ]16 - 24] kHz   |     min - 39999 |   on | downsampled SBR
+                 |   40000 -   max |  off |             ---
+-----------------+-----------------+------+-----------------
+ ]24 - 32] kHz   |     min - 27999 |   on |    dualrate SBR
+                 |   28000 - 55999 |   on | downsampled SBR
+                 |   56000 -   max |  off |             ---
+-----------------+-----------------+------+-----------------
+ ]32 - 44.1] kHz |     min - 63999 |   on |    dualrate SBR
+                 |   64000 -   max |  off |             ---
+-----------------+-----------------+------+-----------------
+ ]44.1 - 48] kHz |     min - 63999 |   on |    dualrate SBR
+                 |   64000 - max   |  off |             ---
+------------------------------------------------------------
+\endverbatim
+
 
 \section audiochCfg Audio Channel Configuration
 The MPEG standard refers often to the so-called Channel Configuration. This Channel Configuration is used for a fixed Channel
@@ -881,8 +908,9 @@
   AACENC_SAMPLERATE               = 0x0103,  /*!< Audio input data sampling rate. Encoder supports following sampling rates:
                                                   8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000 */
 
-  AACENC_SBR_MODE                 = 0x0104,  /*!< Configure SBR independently of the chosen Audio Object Type ::AUDIO_OBJECT_TYPE:.
-                                                  This parameter is only available for ELD.
+  AACENC_SBR_MODE                 = 0x0104,  /*!< Configure SBR independently of the chosen Audio Object Type ::AUDIO_OBJECT_TYPE.
+                                                  This parameter is for ELD audio object type only.
+                                                  - -1: Use ELD SBR auto configurator (default).
                                                   - 0: Disable Spectral Band Replication.
                                                   - 1: Enable Spectral Band Replication. */
 
diff --git a/libAACenc/src/aacenc_lib.cpp b/libAACenc/src/aacenc_lib.cpp
index 8d32f3d..056958b 100644
--- a/libAACenc/src/aacenc_lib.cpp
+++ b/libAACenc/src/aacenc_lib.cpp
@@ -98,7 +98,7 @@
 /* Encoder library info */
 #define AACENCODER_LIB_VL0 3
 #define AACENCODER_LIB_VL1 4
-#define AACENCODER_LIB_VL2 10
+#define AACENCODER_LIB_VL2 11
 #define AACENCODER_LIB_TITLE "AAC Encoder"
 #define AACENCODER_LIB_BUILD_DATE __DATE__
 #define AACENCODER_LIB_BUILD_TIME __TIME__
@@ -235,7 +235,87 @@
 
 } ;
 
-////////////////////////////////////////////////////////////////////////////////////
+typedef struct
+{
+    ULONG               samplingRate;   /*!< Encoder output sampling rate. */
+    ULONG               bitrateRange;   /*!< Lower bitrate range for config entry. */
+
+    UCHAR               lowDelaySbr;    /*!< 0: ELD sbr off,
+                                             1: ELD sbr on */
+
+    UCHAR               downsampledSbr; /*!< 0: ELD with dualrate sbr,
+                                             1: ELD with downsampled sbr */
+
+} ELD_SBR_CONFIGURATOR;
+
+/**
+ * \brief  This table defines ELD/SBR default configurations.
+ */
+static const ELD_SBR_CONFIGURATOR eldSbrAutoConfigTab[] =
+{
+  { 48000,     0, 1, 0 },
+  { 48000, 64001, 0, 0 },
+
+  { 44100,     0, 1, 0 },
+  { 44100, 64001, 0, 0 },
+
+  { 32000,     0, 1, 0 },
+  { 32000, 28000, 1, 1 },
+  { 32000, 56000, 0, 0 },
+
+  { 24000,     0, 1, 1 },
+  { 24000, 40000, 0, 0 },
+
+  { 16000,     0, 1, 1 },
+  { 16000, 28000, 0, 0 }
+
+};
+
+/*
+ * \brief  Configure SBR for ELD configuration.
+ *
+ * This function finds default SBR configuration for ELD based on sampling rate and channel bitrate.
+ * Outputparameters are SBR on/off, and SBR ratio.
+ *
+ * \param samplingRate          Audio signal sampling rate.
+ * \param channelMode           Channel configuration to be used.
+ * \param totalBitrate          Overall bitrate.
+ * \param eldSbr                Pointer to eldSbr parameter, filled on return.
+ * \param eldSbrRatio           Pointer to eldSbrRatio parameter, filled on return.
+ *
+ * \return - AACENC_OK, all fine.
+ *         - AACENC_INVALID_CONFIG, on failure.
+ */
+static AACENC_ERROR eldSbrConfigurator(
+        const ULONG                      samplingRate,
+        const CHANNEL_MODE               channelMode,
+        const ULONG                      totalBitrate,
+        UINT * const                     eldSbr,
+        UINT * const                     eldSbrRatio
+        )
+{
+    AACENC_ERROR err = AACENC_OK;
+    int i, cfgIdx = -1;
+    const ULONG channelBitrate = totalBitrate / FDKaacEnc_GetChannelModeConfiguration(channelMode)->nChannelsEff;
+
+    for (i=0; i<(sizeof(eldSbrAutoConfigTab)/sizeof(ELD_SBR_CONFIGURATOR)); i++) {
+      if ( (samplingRate <= eldSbrAutoConfigTab[i].samplingRate)
+        && (channelBitrate >= eldSbrAutoConfigTab[i].bitrateRange) )
+      {
+        cfgIdx = i;
+      }
+    }
+
+    if (cfgIdx != -1) {
+      *eldSbr      = (eldSbrAutoConfigTab[cfgIdx].lowDelaySbr==0) ? 0 : 1;
+      *eldSbrRatio = (eldSbrAutoConfigTab[cfgIdx].downsampledSbr==0) ? 2 : 1;
+    }
+    else {
+      err = AACENC_INVALID_CONFIG; /* no default configuration for eld-sbr available. */
+    }
+
+    return err;
+}
 
 static inline INT isSbrActive(const HANDLE_AACENC_CONFIG hAacConfig)
 {
@@ -800,6 +880,26 @@
     }
 
     /* Initialize SBR parameters */
+    if ( (hAacConfig->audioObjectType==AOT_ER_AAC_ELD)
+      && (config->userSbrEnabled == (UCHAR)-1) && (config->userSbrRatio==0) )
+    {
+      UINT eldSbr = 0;
+      UINT eldSbrRatio = 0;
+
+      if ( AACENC_OK!=(err=eldSbrConfigurator(
+            hAacConfig->sampleRate,
+            hAacConfig->channelMode,
+            hAacConfig->bitRate,
+           &eldSbr,
+           &eldSbrRatio)) )
+      {
+        return err;
+      }
+
+      hAacConfig->syntaxFlags |= ((eldSbr) ? AC_SBR_PRESENT : 0);
+      hAacConfig->sbrRatio = eldSbrRatio;
+    }
+    else
     if ( (config->userSbrRatio==0) && (isSbrActive(hAacConfig)) ) {
       /* Automatic SBR ratio configuration
        * - downsampled SBR for ELD