libaac: Add test code for AAC encoder and decoder

-- unit test code for AAC encoder and decoder.

Change-Id: I6af9030d485dd4151a84f5f4870da4b2c4c45dfa
diff --git a/Android.mk b/Android.mk
new file mode 100644
index 0000000..5c069fe
--- /dev/null
+++ b/Android.mk
@@ -0,0 +1,2 @@
+LOCAL_PATH := $(call my-dir)
+include $(call all-makefiles-under, $(LOCAL_PATH))
diff --git a/libaac/Android.mk b/libaac/Android.mk
new file mode 100644
index 0000000..9c875bd
--- /dev/null
+++ b/libaac/Android.mk
@@ -0,0 +1,18 @@
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := aacEncode.cpp
+LOCAL_SRC_FILES += aacDecode.cpp
+
+LOCAL_C_INCLUDES := $(LOCAL_PATH)
+LOCAL_C_INCLUDES += $(TOP)/external/aac/libAACenc/include
+LOCAL_C_INCLUDES += $(TOP)/external/aac/libAACdec/include
+LOCAL_C_INCLUDES += $(TOP)/external/aac/libSYS/include
+
+LOCAL_SHARED_LIBRARIES := liblog
+LOCAL_STATIC_LIBRARIES := libFraunhoferAAC
+
+LOCAL_32_BIT_ONLY := true
+LOCAL_MODULE := libwfdaac
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/libaac/aacDecode.cpp b/libaac/aacDecode.cpp
new file mode 100644
index 0000000..44296b6
--- /dev/null
+++ b/libaac/aacDecode.cpp
@@ -0,0 +1,125 @@
+/*
+ * Copyright (c) 2018, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ *       copyright notice, this list of conditions and the following
+ *       disclaimer in the documentation and/or other materials provided
+ *       with the distribution.
+ *     * Neither the name of The Linux Foundation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * */
+
+#include "aacDecode.h"
+#include "aacdecoder_lib.h"
+#include <utils/Log.h>
+#include <string.h>
+
+aacDecode::aacDecode() {
+    p_aacHandle = NULL;
+    p_aacInfo = NULL;
+    memset(&s_aacConfig, 0, sizeof(s_aacConfig));
+}
+
+aacDecode::~aacDecode() {
+    if(!p_aacHandle) {
+        return;
+    }
+
+    aacDecoder_Close((HANDLE_AACDECODER)p_aacHandle);
+    p_aacHandle = NULL;
+}
+
+bool aacDecode::aacConfigure(aacConfigType * p_aacConfig) {
+    if(!p_aacConfig) {
+        return false;
+    }
+
+    memcpy(&s_aacConfig, p_aacConfig, sizeof(s_aacConfig));
+
+    p_aacHandle = aacDecoder_Open(TT_MP4_ADTS, 1);
+
+    if(!p_aacHandle) {
+        ALOGE("Failed to open AAC decoder");
+        return false;
+    }
+
+    p_aacInfo = (void*)aacDecoder_GetStreamInfo((HANDLE_AACDECODER)p_aacHandle);
+
+    if(!p_aacInfo) {
+        ALOGE("Failed to get stream info");
+        return false;
+    }
+
+    /* Configure AAC decoder */
+
+    aacDecoder_SetParam((HANDLE_AACDECODER)p_aacHandle,
+                        AAC_DRC_REFERENCE_LEVEL,
+                        64);
+
+    aacDecoder_SetParam((HANDLE_AACDECODER)p_aacHandle,
+                        AAC_DRC_ATTENUATION_FACTOR,
+                        127);
+
+    aacDecoder_SetParam((HANDLE_AACDECODER)p_aacHandle,
+                        AAC_PCM_MAX_OUTPUT_CHANNELS,
+                        s_aacConfig.n_channels > 6 ? -1 : s_aacConfig.n_channels);
+    return true;
+}
+
+bool aacDecode::aacDecodeFrame(unsigned char* p_buffer, unsigned int n_size) {
+    if(!p_buffer || n_size < 7) {
+        ALOGE("No/Incorrect buffer provided for AAC decoder");
+        return false;
+    }
+
+    if(!p_aacHandle) {
+        ALOGE("Decoder handle not available");
+        return false;
+    }
+
+    AAC_DECODER_ERROR err = AAC_DEC_UNKNOWN;
+
+    UINT nSize[] = {(UINT)n_size};
+    UINT nOcc[] = {(UINT)n_size};
+    UCHAR* pBuf[] = {p_buffer};
+
+    err = aacDecoder_Fill((HANDLE_AACDECODER)p_aacHandle, pBuf, nSize, nOcc);
+
+    if(err != AAC_DEC_OK || nOcc[0] != 0) {
+        ALOGE("Error in aacDecoder_Fill");
+        return false;
+    }
+
+    /* Fix the alloc length being passed to AAC Decoder since it expects INT_PCM
+       as input buffer but we have it as UINT8. INT_PCM is of type SHORT */
+
+    int factor = sizeof(INT_PCM)/sizeof(unsigned char);
+    int allocLen = (factor == 0) ? n_size : n_size/factor;
+
+    err = aacDecoder_DecodeFrame((HANDLE_AACDECODER)p_aacHandle,
+                                  (INT_PCM*)p_buffer, allocLen, 0);
+
+    if(err != AAC_DEC_OK) {
+        ALOGE("Failed to decode frame");
+        return false;
+    }
+    return true;
+}
diff --git a/libaac/aacDecode.h b/libaac/aacDecode.h
new file mode 100644
index 0000000..41b9df5
--- /dev/null
+++ b/libaac/aacDecode.h
@@ -0,0 +1,52 @@
+#ifndef __AAC_DECODE__
+#define __AAC_DECODE__
+/*
+ * Copyright (c) 2018, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ *       copyright notice, this list of conditions and the following
+ *       disclaimer in the documentation and/or other materials provided
+ *       with the distribution.
+ *     * Neither the name of The Linux Foundation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * */
+
+typedef struct aacConfig {
+    unsigned int n_sampleRate;
+    unsigned int n_channels;
+    unsigned int n_bitsPerSample;
+    unsigned int n_bitrate;
+    unsigned int n_samplesPerFrame;
+}aacConfigType;
+
+class aacDecode {
+public:
+    aacDecode();
+    ~aacDecode();
+    bool aacConfigure(aacConfigType* p_aacConfig);
+    bool aacDecodeFrame(unsigned char* p_Buffer, unsigned int n_size);
+
+private:
+    void* p_aacHandle;
+    void* p_aacInfo;
+    aacConfigType s_aacConfig;
+};
+#endif
diff --git a/libaac/aacEncode.cpp b/libaac/aacEncode.cpp
new file mode 100644
index 0000000..1d5b273
--- /dev/null
+++ b/libaac/aacEncode.cpp
@@ -0,0 +1,151 @@
+/*
+ * Copyright (c) 2018, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ *       copyright notice, this list of conditions and the following
+ *       disclaimer in the documentation and/or other materials provided
+ *       with the distribution.
+ *     * Neither the name of The Linux Foundation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * */
+
+#include "aacEncode.h"
+#include "aacenc_lib.h"
+#include <utils/Log.h>
+#include <string.h>
+
+struct aacInfo
+{
+    AACENC_BufDesc inBuff;
+    AACENC_BufDesc outBuff;
+    AACENC_InArgs  inArg;
+    AACENC_OutArgs outArg;
+};
+
+aacEncode::aacEncode() {
+    p_aacHandle = NULL;
+    p_aacInfo = NULL;
+    memset(&s_aacConfig, 0, sizeof(s_aacConfig));
+}
+
+aacEncode::~aacEncode() {
+    if(!p_aacHandle) {
+        return;
+    }
+
+    if(aacEncClose((HANDLE_AACENCODER*)(&p_aacHandle)) != AACENC_OK) {
+        ALOGE("aacEncClose Failed");
+        return;
+    }
+}
+
+bool aacEncode::aacConfigure(aacConfigType * p_aacConfig) {
+    if(!p_aacConfig) {
+        return false;
+    }
+
+    memcpy(&s_aacConfig, p_aacConfig, sizeof(s_aacConfig));
+
+    /* Configure AAC encoder here */
+
+    AACENC_ERROR err = AACENC_OK;
+
+    p_aacInfo = (void*)new(aacInfo);
+    if(!p_aacInfo) {
+        ALOGE("Failed to allocate aacInfo");
+        return false;
+    }
+
+    /* Open AAC encoder */
+    err = aacEncOpen((HANDLE_AACENCODER*)(&p_aacHandle),
+                      0x01 /* AAC */,
+                      s_aacConfig.n_channels);
+
+    if(err != AACENC_OK) {
+        ALOGE("Failed top open AAC encoder");
+        return false;
+    }
+
+    /* Set Bitrate and SampleRate */
+    err = aacEncoder_SetParam((HANDLE_AACENCODER)p_aacHandle,
+                               AACENC_BITRATE,
+                               s_aacConfig.n_bitrate);
+
+    if(err != AACENC_OK) {
+        ALOGE("Failed to set bitrate param to AAC encoder");
+        return false;
+    }
+
+    err = aacEncoder_SetParam((HANDLE_AACENCODER)p_aacHandle,
+                                   AACENC_SAMPLERATE,
+                                   s_aacConfig.n_sampleRate);
+
+    if(err != AACENC_OK) {
+        ALOGE("Failed to set samplerate param to AAC encoder");
+        return false;
+    }
+
+    /* Fix Channel mode and order */
+    /* TODO */
+
+    /* Prefill encode structures */
+    /* TODO */
+
+    return true;
+}
+
+bool aacEncode::aacEncodeFrame(unsigned char * p_inBuffer,
+                              unsigned int n_inSize,
+                              unsigned char * p_outBuffer,
+                              unsigned int n_outSize,
+                              unsigned int * p_length) {
+    (void)n_inSize;
+    (void)n_outSize;
+    (void)p_length;
+    if(!p_inBuffer || !p_outBuffer) {
+        ALOGE("No buffers provided for AAC encoder");
+        return false;
+    }
+
+    aacInfo *tempAacInfo = (aacInfo*)p_aacInfo;
+    tempAacInfo->inBuff.bufs = (void**) (&p_inBuffer);
+    tempAacInfo->outBuff.bufs = (void**) (&p_outBuffer);
+
+    AACENC_ERROR err = AACENC_OK;
+
+    if(p_aacHandle) {
+        err = aacEncEncode((HANDLE_AACENCODER)p_aacHandle,
+                           &tempAacInfo->inBuff,
+                           &tempAacInfo->outBuff,
+                           &tempAacInfo->inArg,
+                           &tempAacInfo->outArg);
+
+        if(err != AACENC_OK) {
+            ALOGE("Failed to encode buffer");
+            return false;
+        }
+    } else {
+        ALOGE("No encoder available");
+        return false;
+    }
+
+    return true;
+}
diff --git a/libaac/aacEncode.h b/libaac/aacEncode.h
new file mode 100644
index 0000000..46a7734
--- /dev/null
+++ b/libaac/aacEncode.h
@@ -0,0 +1,55 @@
+#ifndef __AAC_ENCODE__
+#define __AAC_ENCODE__
+/*
+ * Copyright (c) 2018, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ *       copyright notice, this list of conditions and the following
+ *       disclaimer in the documentation and/or other materials provided
+ *       with the distribution.
+ *     * Neither the name of The Linux Foundation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * */
+typedef struct aacConfig {
+    unsigned int n_sampleRate;
+    unsigned int n_channels;
+    unsigned int n_bitsPerSample;
+    unsigned int n_bitrate;
+    unsigned int n_samplesPerFrame;
+}aacConfigType;
+
+class aacEncode {
+public:
+    aacEncode();
+    ~aacEncode();
+    bool aacConfigure(aacConfigType* p_aacConfig);
+    bool aacEncodeFrame(unsigned char* p_inBuffer,
+                   unsigned int n_inSize,
+                   unsigned char* p_outBuffer,
+                   unsigned int n_outSize,
+                   unsigned int* p_length);
+
+private:
+    void* p_aacHandle;
+    void* p_aacInfo;
+    aacConfigType s_aacConfig;
+};
+#endif