hal: add support to override volume gain to step mapping table

- if customer mapping is added in platform info file, use that
- in absence of customer table use default mapping

CRs-Fixed: 1089771
Change-Id: I4bf8bcf1913f16ad6298ff1f3fa5dd649c889b2a
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 09c5a96..e2386a8 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -472,6 +472,27 @@
     return 0;
 }
 
+__attribute__ ((visibility ("default")))
+int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
+                                    int table_size) {
+     int ret_val = 0;
+     ALOGV("%s: enter ... ", __func__);
+
+     pthread_mutex_lock(&adev_init_lock);
+     if (adev == NULL) {
+         ALOGW("%s: adev is NULL .... ", __func__);
+         goto done;
+     }
+
+     pthread_mutex_lock(&adev->lock);
+     ret_val = platform_get_gain_level_mapping(mapping_tbl, table_size);
+     pthread_mutex_unlock(&adev->lock);
+done:
+     pthread_mutex_unlock(&adev_init_lock);
+     ALOGV("%s: exit ... ", __func__);
+     return ret_val;
+}
+
 static bool is_supported_format(audio_format_t format)
 {
     if (format == AUDIO_FORMAT_MP3 ||
diff --git a/hal/msm8916/platform.c b/hal/msm8916/platform.c
index c2bcbe0..64b5c17 100644
--- a/hal/msm8916/platform.c
+++ b/hal/msm8916/platform.c
@@ -5835,3 +5835,14 @@
     struct platform_data *my_data = (struct platform_data *)platform;
     return my_data->max_mic_count;
 }
+
+bool platform_add_gain_level_mapping(struct amp_db_and_gain_table *tbl_entry __unused)
+{
+    return false;
+}
+
+int platform_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl __unused,
+                                    int table_size __unused)
+{
+    return 0;
+}
diff --git a/hal/msm8960/platform.c b/hal/msm8960/platform.c
index 33bbc2f..31d86f4 100644
--- a/hal/msm8960/platform.c
+++ b/hal/msm8960/platform.c
@@ -1340,3 +1340,13 @@
     return false;
 }
 
+bool platform_add_gain_level_mapping(struct amp_db_and_gain_table *tbl_entry __unused)
+{
+    return false;
+}
+
+int platform_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl __unused,
+                                    int table_size __unused)
+{
+    return 0;
+}
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index 81741fc..48cd5af 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -5860,3 +5860,53 @@
     struct platform_data *my_data = (struct platform_data *)platform;
     return my_data->max_mic_count;
 }
+
+static struct amp_db_and_gain_table tbl_mapping[MAX_VOLUME_CAL_STEPS];
+static int num_gain_tbl_entry = 0;
+
+bool platform_add_gain_level_mapping(struct amp_db_and_gain_table *tbl_entry) {
+
+    ALOGV("%s: enter .. add %f %f %d", __func__, tbl_entry->amp, tbl_entry->db, tbl_entry->level);
+    if (num_gain_tbl_entry == -1) {
+        ALOGE("%s: num entry beyond valid step levels or corrupted..rejecting custom mapping",
+               __func__);
+        return false;
+    }
+
+    if (num_gain_tbl_entry >= MAX_VOLUME_CAL_STEPS) {
+        ALOGE("%s: max entry reached max[%d] current index[%d]  .. rejecting", __func__,
+               MAX_VOLUME_CAL_STEPS, num_gain_tbl_entry);
+        num_gain_tbl_entry  = -1; // indicates error and no more info will be cached
+        return false;
+    }
+
+    if (num_gain_tbl_entry > 0 && tbl_mapping[num_gain_tbl_entry - 1].amp >= tbl_entry->amp) {
+        ALOGE("%s: value not in ascending order .. rejecting custom mapping", __func__);
+        num_gain_tbl_entry  = -1; // indicates error and no more info will be cached
+        return false;
+    }
+
+    tbl_mapping[num_gain_tbl_entry] = *tbl_entry;
+    ++num_gain_tbl_entry;
+
+    return true;
+}
+
+int platform_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
+                                    int table_size) {
+    int itt = 0;
+    ALOGV("platform_get_gain_level_mapping called ");
+
+    if (num_gain_tbl_entry <= 0 || num_gain_tbl_entry > MAX_VOLUME_CAL_STEPS) {
+        ALOGD("%s: empty or currupted gain_mapping_table", __func__);
+        return 0;
+    }
+
+    for (; itt < num_gain_tbl_entry && itt <= table_size; itt++) {
+        mapping_tbl[itt] = tbl_mapping[itt];
+        ALOGV("%s: added amp[%f] db[%f] level[%d]", __func__,
+               mapping_tbl[itt].amp, mapping_tbl[itt].db, mapping_tbl[itt].level);
+    }
+
+    return num_gain_tbl_entry;
+}
diff --git a/hal/platform_api.h b/hal/platform_api.h
index d3cb23f..e274e48 100644
--- a/hal/platform_api.h
+++ b/hal/platform_api.h
@@ -20,6 +20,8 @@
 #ifndef AUDIO_PLATFORM_API_H
 #define AUDIO_PLATFORM_API_H
 #include <sound/voice_params.h>
+#include "audio_hw.h"
+#include "voice.h"
 
 #define CODEC_BACKEND_DEFAULT_BIT_WIDTH 16
 #define CODEC_BACKEND_DEFAULT_SAMPLE_RATE 48000
@@ -28,6 +30,13 @@
 #define SAMPLE_RATE_8000 8000
 #define SAMPLE_RATE_11025 11025
 #define sample_rate_multiple(sr, base) ((sr % base)== 0?true:false)
+#define MAX_VOLUME_CAL_STEPS 15
+
+struct amp_db_and_gain_table {
+    float amp;
+    float db;
+    uint32_t level;
+};
 
 enum {
     NATIVE_AUDIO_MODE_SRC = 1,
@@ -47,6 +56,19 @@
 void *platform_init(struct audio_device *adev);
 void platform_deinit(void *platform);
 const char *platform_get_snd_device_name(snd_device_t snd_device);
+
+/* return true if adding entry success
+   return false if adding entry fails */
+
+bool platform_add_gain_level_mapping(struct amp_db_and_gain_table *tbl_entry);
+
+/* return 0 if no custome mapping table found or when error detected
+            use default mapping in this case
+   return > 0 indicates number of entries in mapping table */
+
+int platform_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
+                                    int table_size);
+
 int platform_get_snd_device_name_extn(void *platform, snd_device_t snd_device,
                                       char *device_name);
 void platform_add_backend_name(char *mixer_path, snd_device_t snd_device,
diff --git a/hal/platform_info.c b/hal/platform_info.c
index 4ccb89c..8e12dd6 100644
--- a/hal/platform_info.c
+++ b/hal/platform_info.c
@@ -38,6 +38,7 @@
 #include <audio_hw.h>
 #include "platform_api.h"
 #include <platform.h>
+#include <math.h>
 
 #define BUF_SIZE                    1024
 
@@ -49,6 +50,7 @@
     BACKEND_NAME,
     INTERFACE_NAME,
     CONFIG_PARAMS,
+    GAIN_LEVEL_MAPPING,
 } section_t;
 
 typedef void (* section_process_fn)(const XML_Char **attr);
@@ -60,6 +62,7 @@
 static void process_interface_name(const XML_Char **attr);
 static void process_config_params(const XML_Char **attr);
 static void process_root(const XML_Char **attr);
+static void process_gain_db_to_level_map(const XML_Char **attr);
 
 static section_process_fn section_table[] = {
     [ROOT] = process_root,
@@ -69,6 +72,7 @@
     [BACKEND_NAME] = process_backend_name,
     [INTERFACE_NAME] = process_interface_name,
     [CONFIG_PARAMS] = process_config_params,
+    [GAIN_LEVEL_MAPPING] = process_gain_db_to_level_map,
 };
 
 static section_t section;
@@ -206,6 +210,30 @@
     return;
 }
 
+static void process_gain_db_to_level_map(const XML_Char **attr)
+{
+    struct amp_db_and_gain_table tbl_entry;
+
+    if ((strcmp(attr[0], "db") != 0) ||
+        (strcmp(attr[2], "level") != 0)) {
+        ALOGE("%s: invalid attribute passed  %s %sexpected amp db level",
+               __func__, attr[0], attr[2]);
+        goto done;
+    }
+
+    tbl_entry.db = atof(attr[1]);
+    tbl_entry.amp = exp(tbl_entry.db * 0.115129f);
+    tbl_entry.level = atoi(attr[3]);
+
+    ALOGV("%s: amp [%f]  db [%f] level [%d]", __func__,
+           tbl_entry.amp, tbl_entry.db, tbl_entry.level);
+    platform_add_gain_level_mapping(&tbl_entry);
+
+done:
+    return;
+}
+
+
 static void process_acdb_id(const XML_Char **attr)
 {
     int index;
@@ -337,6 +365,8 @@
         section = CONFIG_PARAMS;
     } else if (strcmp(tag_name, "interface_names") == 0) {
         section = INTERFACE_NAME;
+    } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
+        section = GAIN_LEVEL_MAPPING;
     } else if (strcmp(tag_name, "device") == 0) {
         if ((section != ACDB) && (section != BACKEND_NAME) && (section != BITWIDTH) &&
             (section != INTERFACE_NAME)) {
@@ -347,6 +377,14 @@
         /* call into process function for the current section */
         section_process_fn fn = section_table[section];
         fn(attr);
+    } else if (strcmp(tag_name, "gain_level_map") == 0) {
+        if (section != GAIN_LEVEL_MAPPING) {
+            ALOGE("usecase tag only supported with GAIN_LEVEL_MAPPING section");
+            return;
+        }
+
+        section_process_fn fn = section_table[GAIN_LEVEL_MAPPING];
+        fn(attr);
     } else if (strcmp(tag_name, "usecase") == 0) {
         if (section != PCM_ID) {
             ALOGE("usecase tag only supported with PCM_ID section");
@@ -383,6 +421,8 @@
         platform_set_parameters(my_data.platform, my_data.kvpairs);
     } else if (strcmp(tag_name, "interface_names") == 0) {
         section = ROOT;
+    } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
+        section = ROOT;
     }
 }
 
diff --git a/post_proc/Android.mk b/post_proc/Android.mk
index 01805af..7e507d9 100644
--- a/post_proc/Android.mk
+++ b/post_proc/Android.mk
@@ -8,13 +8,13 @@
 endif
 
 LOCAL_SRC_FILES:= \
-	bundle.c \
-	equalizer.c \
-	bass_boost.c \
-	virtualizer.c \
-	reverb.c \
-	effect_api.c \
-	effect_util.c
+        bundle.c \
+        equalizer.c \
+        bass_boost.c \
+        virtualizer.c \
+        reverb.c \
+        effect_api.c \
+        effect_util.c
 
 ifeq ($(strip $(AUDIO_FEATURE_ENABLED_HW_ACCELERATED_EFFECTS)),true)
     LOCAL_CFLAGS += -DHW_ACCELERATED_EFFECTS
@@ -33,10 +33,10 @@
 endif
 
 LOCAL_SHARED_LIBRARIES := \
-	libcutils \
-	liblog \
-	libtinyalsa \
-	libdl
+        libcutils \
+        liblog \
+        libtinyalsa \
+        libdl
 
 LOCAL_MODULE_TAGS := optional
 
@@ -46,9 +46,9 @@
 LOCAL_ADDITIONAL_DEPENDENCIES += $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr
 
 LOCAL_C_INCLUDES := \
-	external/tinyalsa/include \
+        external/tinyalsa/include \
         $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/include \
-	$(call include-path-for, audio-effects)
+        $(call include-path-for, audio-effects)
 
 include $(BUILD_SHARED_LIBRARY)
 
@@ -101,7 +101,13 @@
 LOCAL_MODULE:= libvolumelistener
 
 LOCAL_C_INCLUDES := \
-        $(call include-path-for, audio-effects)
+        hardware/qcom/audio/hal \
+        $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/include \
+        external/tinyalsa/include \
+        $(call include-path-for, audio-effects) \
+        $(call include-path-for, audio-route) \
+        hardware/qcom/audio/hal/audio_extn \
+        external/tinycompress/include
 
 include $(BUILD_SHARED_LIBRARY)
 
diff --git a/post_proc/volume_listener.c b/post_proc/volume_listener.c
index 7b60248..c4894ca 100644
--- a/post_proc/volume_listener.c
+++ b/post_proc/volume_listener.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2015-2016, 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
@@ -36,6 +36,7 @@
 #include <cutils/log.h>
 #include <hardware/audio_effect.h>
 #include <cutils/properties.h>
+#include <platform_api.h>
 
 #define PRIMARY_HAL_PATH XSTR(LIB_AUDIO_HAL)
 #define XSTR(x) STR(x)
@@ -50,13 +51,14 @@
                                                             i == MUSIC?"MUSIC": \
                                                             i == RING?"RING": \
                                                             i == ALARM?"ALARM": \
-                                                            i == VOICE_CALL?"Voice_call": \
+                                                            i == VC_CALL?"Voice_call": \
                                                             i == NOTIFICATION?"Notification":\
                                                             "--INVALID--"); \
 
 #define MAX_GAIN_LEVELS 5
 
 #define AHAL_GAIN_DEPENDENT_INTERFACE_FUNCTION "audio_hw_send_gain_dep_calibration"
+#define AHAL_GAIN_GET_MAPPING_TABLE "audio_hw_get_gain_level_mapping"
 
 enum {
     VOL_LISTENER_STATE_UNINITIALIZED,
@@ -77,7 +79,7 @@
     MUSIC,
     RING,
     ALARM,
-    VOICE_CALL,
+    VC_CALL,
     NOTIFICATION,
     MAX_STREAM_TYPES,
 };
@@ -155,20 +157,26 @@
     "Qualcomm Technologies Inc",
 };
 
-struct amp_db_and_gain_table {
-    float amp;
-    float db;
-    uint32_t level;
-} amp_to_dBLevel_table;
+static int total_volume_cal_step = MAX_GAIN_LEVELS;
 
 // using gain level for non-drc volume curve
-static const struct amp_db_and_gain_table  volume_curve_gain_mapping_table[MAX_GAIN_LEVELS] = {
+struct amp_db_and_gain_table  volume_curve_gain_mapping_table[MAX_VOLUME_CAL_STEPS] = {
     /* Level 0 in the calibration database contains default calibration */
     { 0.001774, -55, 5 },
     { 0.501187,  -6, 4 },
     { 0.630957,  -4, 3 },
     { 0.794328,  -2, 2 },
     { 1.0,        0, 1 },
+    { 0, 0, -1 },
+    { 0, 0, -1 },
+    { 0, 0, -1 },
+    { 0, 0, -1 },
+    { 0, 0, -1 },
+    { 0, 0, -1 },
+    { 0, 0, -1 },
+    { 0, 0, -1 },
+    { 0, 0, -1 },
+    { 0, 0, -1 },
 };
 
 static const effect_descriptor_t *descriptors[] = {
@@ -190,6 +198,8 @@
 /* HAL interface to send calibration */
 static bool (*send_gain_dep_cal)(int);
 
+static int (*get_custom_gain_table)(struct amp_db_and_gain_table *, int);
+
 /* if dumping allowed */
 static bool dumping_enabled = false;
 
@@ -217,7 +227,7 @@
         context->stream_type == MUSIC ? "MUSIC" :
         context->stream_type == RING ? "RING" :
         context->stream_type == ALARM ? "ALARM" :
-        context->stream_type == VOICE_CALL ? "VOICE_CALL" :
+        context->stream_type == VC_CALL ? "VC_CALL" :
         context->stream_type == NOTIFICATION ? "NOTIFICATION" : "--INVALID--",
         context->dev_id, context->state, context->session_id, context->left_vol,context->right_vol);
     }
@@ -261,12 +271,12 @@
             // send Gain dep cal level
             int gain_dep_cal_level = -1;
 
-            if (new_vol >= 1) { // max amplitude, use highest DRC level
-                gain_dep_cal_level = volume_curve_gain_mapping_table[MAX_GAIN_LEVELS - 1].level;
+            if (new_vol >= 1 && total_volume_cal_step > 0) { // max amplitude, use highest DRC level
+                gain_dep_cal_level = volume_curve_gain_mapping_table[total_volume_cal_step - 1].level;
             } else if (new_vol <= 0) {
                 gain_dep_cal_level = volume_curve_gain_mapping_table[0].level;
             } else {
-                for (max_level = 0; max_level + 1 < MAX_GAIN_LEVELS; max_level++) {
+                for (max_level = 0; max_level + 1 < total_volume_cal_step; max_level++) {
                     if (new_vol < volume_curve_gain_mapping_table[max_level + 1].amp &&
                         new_vol >= volume_curve_gain_mapping_table[max_level].amp) {
                         gain_dep_cal_level = volume_curve_gain_mapping_table[max_level].level;
@@ -584,13 +594,16 @@
 
 static void init_once()
 {
-    int i = 0;
+    int max_table_ent = 0;
     if (initialized) {
         ALOGV("%s : already init .. do nothing", __func__);
         return;
     }
 
     ALOGD("%s Called ", __func__);
+    send_gain_dep_cal = NULL;
+    get_custom_gain_table = NULL;
+
     pthread_mutex_init(&vol_listner_init_lock, NULL);
 
     // get hal function pointer
@@ -598,17 +611,52 @@
         void *hal_lib_pointer = dlopen(PRIMARY_HAL_PATH, RTLD_NOW);
         if (hal_lib_pointer == NULL) {
             ALOGE("%s: DLOPEN failed for %s", __func__, PRIMARY_HAL_PATH);
-            send_gain_dep_cal = NULL;
         } else {
             ALOGV("%s: DLOPEN of %s Succes .. next get HAL entry function", __func__, PRIMARY_HAL_PATH);
             send_gain_dep_cal = (bool (*)(int))dlsym(hal_lib_pointer, AHAL_GAIN_DEPENDENT_INTERFACE_FUNCTION);
             if (send_gain_dep_cal == NULL) {
                 ALOGE("Couldnt able to get the function symbol");
             }
+
+            get_custom_gain_table = (int (*) (struct amp_db_and_gain_table *, int))
+               dlsym(hal_lib_pointer, AHAL_GAIN_GET_MAPPING_TABLE);
+            if (get_custom_gain_table == NULL) {
+                ALOGE("Couldnt able to get the function AHAL_GAIN_GET_MAPPING_TABLE  symbol");
+            } else {
+                max_table_ent = get_custom_gain_table(volume_curve_gain_mapping_table,
+                   MAX_VOLUME_CAL_STEPS);
+                // if number of entries is 0 use default
+                // if number of entries > MAX_VOLUME_CAL_STEPS (this should never happen)
+                // then in this case
+                // use only default number of steps but this will result in
+                // unexpected behaviour
+
+                if (max_table_ent > 0 && max_table_ent <= MAX_VOLUME_CAL_STEPS) {
+                    if (max_table_ent < total_volume_cal_step) {
+                        for (int i = max_table_ent; i < total_volume_cal_step; i++ ) {
+                            volume_curve_gain_mapping_table[i].amp = 0;
+                            volume_curve_gain_mapping_table[i].db = 0;
+                            volume_curve_gain_mapping_table[i].level = -1;
+                        }
+                    }
+                    total_volume_cal_step = max_table_ent;
+                    ALOGD("%s: using custome volume table", __func__);
+                } else {
+                    ALOGD("%s: using default volume table", __func__);
+                }
+
+                if (dumping_enabled) {
+                    ALOGD("%s: dumping table here .. size of table received %d",
+                           __func__, max_table_ent);
+                    for (int i = 0; i < MAX_VOLUME_CAL_STEPS ; i++)
+                        ALOGD("[%d]  %f %f %d", i, volume_curve_gain_mapping_table[i].amp,
+                                                   volume_curve_gain_mapping_table[i].db,
+                                                   volume_curve_gain_mapping_table[i].level);
+                }
+            }
         }
     } else {
         ALOGE("%s: not able to acces lib %s ", __func__, PRIMARY_HAL_PATH);
-        send_gain_dep_cal = NULL;
     }
 
     // check system property to see if dumping is required