Property to enable AAC 5.1 pass-through.

Bug: 6275957
Change-Id: I8debdce872fc8287a4f6340f1790e02c075886e3
diff --git a/media/libstagefright/codecs/aacdec/Android.mk b/media/libstagefright/codecs/aacdec/Android.mk
index b7c3788..1b2544a 100644
--- a/media/libstagefright/codecs/aacdec/Android.mk
+++ b/media/libstagefright/codecs/aacdec/Android.mk
@@ -24,7 +24,7 @@
           libAACdec libMpegTPDec libSBRdec libPCMutils libFDK libSYS
 
   LOCAL_SHARED_LIBRARIES := \
-          libstagefright_omx libstagefright_foundation libutils
+          libstagefright_omx libstagefright_foundation libutils libcutils
 
   LOCAL_MODULE := libstagefright_soft_aacdec
   LOCAL_MODULE_TAGS := optional
diff --git a/media/libstagefright/codecs/aacdec/SoftAAC2.cpp b/media/libstagefright/codecs/aacdec/SoftAAC2.cpp
index 76c3854..791445c 100644
--- a/media/libstagefright/codecs/aacdec/SoftAAC2.cpp
+++ b/media/libstagefright/codecs/aacdec/SoftAAC2.cpp
@@ -19,6 +19,7 @@
 
 #include "SoftAAC2.h"
 
+#include <cutils/properties.h>
 #include <media/stagefright/foundation/ADebug.h>
 #include <media/stagefright/foundation/hexdump.h>
 #include <media/stagefright/MediaErrors.h>
@@ -185,7 +186,6 @@
         default:
             return SimpleSoftOMXComponent::internalGetParameter(index, params);
     }
-
 }
 
 OMX_ERRORTYPE SoftAAC2::internalSetParameter(
@@ -247,6 +247,18 @@
     return mInputBufferCount > 0;
 }
 
+void SoftAAC2::maybeConfigureDownmix() const {
+    if (mStreamInfo->numChannels > 2) {
+        char value[PROPERTY_VALUE_MAX];
+        if (!(property_get("media.aac_51_output_enabled", value, NULL) &&
+                (!strcmp(value, "1") || !strcasecmp(value, "true")))) {
+            ALOGI("Downmixing multichannel AAC to stereo");
+            aacDecoder_SetParam(mAACDecoder, AAC_PCM_OUTPUT_CHANNELS, 2);
+            mStreamInfo->numChannels = 2;
+        }
+    }
+}
+
 void SoftAAC2::onQueueFilled(OMX_U32 portIndex) {
     if (mSignalledError || mOutputPortSettingsChange != NONE) {
         return;
@@ -281,7 +293,8 @@
         info->mOwnedByUs = false;
         notifyEmptyBufferDone(header);
 
-        ALOGI("Configuring decoder: %d Hz, %d channels",
+        maybeConfigureDownmix();
+        ALOGI("Initially configuring decoder: %d Hz, %d channels",
               mStreamInfo->sampleRate,
               mStreamInfo->numChannels);
         notify(OMX_EventPortSettingsChanged, 1, 0, NULL);
@@ -422,6 +435,7 @@
         if (mInputBufferCount <= 2) {
             if (mStreamInfo->sampleRate != prevSampleRate ||
                 mStreamInfo->numChannels != prevNumChannels) {
+                maybeConfigureDownmix();
                 ALOGI("Reconfiguring decoder: %d Hz, %d channels",
                       mStreamInfo->sampleRate,
                       mStreamInfo->numChannels);
diff --git a/media/libstagefright/codecs/aacdec/SoftAAC2.h b/media/libstagefright/codecs/aacdec/SoftAAC2.h
index 29c9484..ac379f4 100644
--- a/media/libstagefright/codecs/aacdec/SoftAAC2.h
+++ b/media/libstagefright/codecs/aacdec/SoftAAC2.h
@@ -66,6 +66,7 @@
     void initPorts();
     status_t initDecoder();
     bool isConfigured() const;
+    void maybeConfigureDownmix() const;
 
     DISALLOW_EVIL_CONSTRUCTORS(SoftAAC2);
 };