Merge commit '2c69188d9160b5e71ccffa6e42759c12fdb5965c' into HEAD
diff --git a/Android.mk b/Android.mk
index 1c5c5b3..1228876 100644
--- a/Android.mk
+++ b/Android.mk
@@ -7,6 +7,7 @@
 else
 include $(MY_LOCAL_PATH)/hal/Android.mk
 include $(MY_LOCAL_PATH)/voice_processing/Android.mk
+include $(MY_LOCAL_PATH)/visualizer/Android.mk
 endif
 
 endif
diff --git a/hal/Android.mk b/hal/Android.mk
index 6db96a9..b5949da 100644
--- a/hal/Android.mk
+++ b/hal/Android.mk
@@ -6,27 +6,33 @@
 
 LOCAL_ARM_MODE := arm
 
+AUDIO_PLATFORM := $(TARGET_BOARD_PLATFORM)
+ifneq ($(filter msm8974 msm8226,$(TARGET_BOARD_PLATFORM)),)
+  # B-family platform uses msm8974 code base
+  AUDIO_PLATFORM = msm8974
+endif
+
 LOCAL_SRC_FILES := \
 	audio_hw.c \
-	edid.c
+	$(AUDIO_PLATFORM)/platform.c
 
 LOCAL_SHARED_LIBRARIES := \
 	liblog \
 	libcutils \
 	libtinyalsa \
+	libtinycompress \
 	libaudioroute \
 	libdl
 
-ifeq ($(TARGET_BOARD_PLATFORM), msm8974)
-LOCAL_CFLAGS += -DMSM8974
-endif
 
 LOCAL_C_INCLUDES += \
 	external/tinyalsa/include \
+	external/tinycompress/include \
 	$(call include-path-for, audio-route) \
-	$(call include-path-for, audio-effects)
+	$(call include-path-for, audio-effects) \
+	$(LOCAL_PATH)/$(AUDIO_PLATFORM)
 
-LOCAL_MODULE := audio.primary.$(TARGET_BOARD_PLATFORM)
+LOCAL_MODULE := audio.primary.$(AUDIO_PLATFORM)
 
 LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
 
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 275272a..08d9c32 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -16,38 +16,105 @@
 
 #define LOG_TAG "audio_hw_primary"
 /*#define LOG_NDEBUG 0*/
-#define LOG_NDDEBUG 0
+/*#define VERY_VERY_VERBOSE_LOGGING*/
+#ifdef VERY_VERY_VERBOSE_LOGGING
+#define ALOGVV ALOGV
+#else
+#define ALOGVV(a...) do { } while(0)
+#endif
 
 #include <errno.h>
 #include <pthread.h>
 #include <stdint.h>
 #include <sys/time.h>
 #include <stdlib.h>
-#include <dlfcn.h>
 #include <math.h>
+#include <dlfcn.h>
+#include <sys/resource.h>
+#include <sys/prctl.h>
 
 #include <cutils/log.h>
 #include <cutils/str_parms.h>
 #include <cutils/properties.h>
-#include <cutils/list.h>
+#include <cutils/atomic.h>
+#include <cutils/sched_policy.h>
 
+#include <hardware/audio_effect.h>
+#include <system/thread_defs.h>
+#include <audio_effects/effect_aec.h>
+#include <audio_effects/effect_ns.h>
 #include "audio_hw.h"
+#include "platform_api.h"
+#include <platform.h>
 
-#define LIB_ACDB_LOADER "/system/lib/libacdbloader.so"
-#define LIB_CSD_CLIENT "/system/lib/libcsd-client.so"
-#define MIXER_XML_PATH "/system/etc/mixer_paths.xml"
-#define MIXER_CARD 0
+#include "sound/compress_params.h"
+
+#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
+#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
+/* ToDo: Check and update a proper value in msec */
+#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
+#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
+
+struct pcm_config pcm_config_deep_buffer = {
+    .channels = 2,
+    .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
+    .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
+    .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
+    .format = PCM_FORMAT_S16_LE,
+    .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
+    .stop_threshold = INT_MAX,
+    .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
+};
+
+struct pcm_config pcm_config_low_latency = {
+    .channels = 2,
+    .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
+    .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
+    .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
+    .format = PCM_FORMAT_S16_LE,
+    .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
+    .stop_threshold = INT_MAX,
+    .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
+};
+
+struct pcm_config pcm_config_hdmi_multi = {
+    .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
+    .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
+    .period_size = HDMI_MULTI_PERIOD_SIZE,
+    .period_count = HDMI_MULTI_PERIOD_COUNT,
+    .format = PCM_FORMAT_S16_LE,
+    .start_threshold = 0,
+    .stop_threshold = INT_MAX,
+    .avail_min = 0,
+};
+
+struct pcm_config pcm_config_audio_capture = {
+    .channels = 2,
+    .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
+    .format = PCM_FORMAT_S16_LE,
+};
+
+struct pcm_config pcm_config_voice_call = {
+    .channels = 1,
+    .rate = 8000,
+    .period_size = 160,
+    .period_count = 2,
+    .format = PCM_FORMAT_S16_LE,
+};
+
+static const char * const use_case_table[AUDIO_USECASE_MAX] = {
+    [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
+    [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
+    [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
+    [USECASE_AUDIO_RECORD] = "audio-record",
+    [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
+    [USECASE_VOICE_CALL] = "voice-call",
+    [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
+};
+
 
 #define STRING_TO_ENUM(string) { #string, string }
 
-/* Flags used to initialize acdb_settings variable that goes to ACDB library */
-#define DMIC_FLAG       0x00000002
-#define TTY_MODE_OFF    0x00000010
-#define TTY_MODE_FULL   0x00000020
-#define TTY_MODE_VCO    0x00000040
-#define TTY_MODE_HCO    0x00000080
-#define TTY_MODE_CLEAR  0xFFFFFF0F
-
 struct string_to_enum {
     const char *name;
     uint32_t value;
@@ -59,175 +126,33 @@
     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
 };
 
-static const char * const use_case_table[AUDIO_USECASE_MAX] = {
-    [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
-    [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
-    [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
-    [USECASE_AUDIO_RECORD] = "audio-record",
-    [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
-    [USECASE_VOICE_CALL] = "voice-call",
-};
+static int set_voice_volume_l(struct audio_device *adev, float volume);
 
-static const int pcm_device_table[AUDIO_USECASE_MAX][2] = {
-    [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
-#ifdef MSM8974
-    [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
-#else
-    [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {14, 14},
-#endif
-    [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
-    [USECASE_AUDIO_RECORD] = {0, 0},
-    [USECASE_AUDIO_RECORD_LOW_LATENCY] = {14, 14},
-    [USECASE_VOICE_CALL] = {12, 12},
-};
-
-/* Array to store sound devices */
-static const char * const device_table[SND_DEVICE_MAX] = {
-    [SND_DEVICE_NONE] = "none",
-    /* Playback sound devices */
-    [SND_DEVICE_OUT_HANDSET] = "handset",
-    [SND_DEVICE_OUT_SPEAKER] = "speaker",
-    [SND_DEVICE_OUT_SPEAKER_REVERSE] = "speaker-reverse",
-    [SND_DEVICE_OUT_HEADPHONES] = "headphones",
-    [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones",
-    [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker",
-    [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones",
-    [SND_DEVICE_OUT_HDMI] = "hdmi",
-    [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi",
-    [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset",
-    [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = "voice-handset-tmus",
-    [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones",
-    [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones",
-    [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset",
-
-    /* Capture sound devices */
-    [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
-    [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic",
-    [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic",
-    [SND_DEVICE_IN_HANDSET_MIC_AEC] = "handset-mic",
-    [SND_DEVICE_IN_SPEAKER_MIC_AEC] = "voice-speaker-mic",
-    [SND_DEVICE_IN_HEADSET_MIC_AEC] = "headset-mic",
-    [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic",
-    [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic",
-    [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic",
-    [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic",
-    [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic",
-    [SND_DEVICE_IN_VOICE_DMIC_EF] = "voice-dmic-ef",
-    [SND_DEVICE_IN_VOICE_DMIC_BS] = "voice-dmic-bs",
-    [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = "voice-dmic-ef-tmus",
-    [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = "voice-speaker-dmic-ef",
-    [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = "voice-speaker-dmic-bs",
-    [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
-    [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
-    [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
-    [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
-    [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = "voice-rec-dmic-ef",
-    [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = "voice-rec-dmic-bs",
-    [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = "voice-rec-dmic-ef-fluence",
-    [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = "voice-rec-dmic-bs-fluence",
-};
-
-/* ACDB IDs (audio DSP path configuration IDs) for each sound device */
-static const int acdb_device_table[SND_DEVICE_MAX] = {
-    [SND_DEVICE_NONE] = -1,
-    [SND_DEVICE_OUT_HANDSET] = 7,
-    [SND_DEVICE_OUT_SPEAKER] = 14,
-    [SND_DEVICE_OUT_SPEAKER_REVERSE] = 14,
-    [SND_DEVICE_OUT_HEADPHONES] = 10,
-    [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = 10,
-    [SND_DEVICE_OUT_VOICE_SPEAKER] = 14,
-    [SND_DEVICE_OUT_VOICE_HEADPHONES] = 10,
-    [SND_DEVICE_OUT_HDMI] = 18,
-    [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 14,
-    [SND_DEVICE_OUT_BT_SCO] = 22,
-    [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = 81,
-    [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = 17,
-    [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = 17,
-    [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = 37,
-
-    [SND_DEVICE_IN_HANDSET_MIC] = 4,
-    [SND_DEVICE_IN_SPEAKER_MIC] = 4,
-    [SND_DEVICE_IN_HEADSET_MIC] = 8,
-    [SND_DEVICE_IN_HANDSET_MIC_AEC] = 40,
-    [SND_DEVICE_IN_SPEAKER_MIC_AEC] = 42,
-    [SND_DEVICE_IN_HEADSET_MIC_AEC] = 47,
-    [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = 11,
-    [SND_DEVICE_IN_VOICE_HEADSET_MIC] = 8,
-    [SND_DEVICE_IN_HDMI_MIC] = 4,
-    [SND_DEVICE_IN_BT_SCO_MIC] = 21,
-    [SND_DEVICE_IN_CAMCORDER_MIC] = 61,
-    [SND_DEVICE_IN_VOICE_DMIC_EF] = 6,
-    [SND_DEVICE_IN_VOICE_DMIC_BS] = 5,
-    [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = 91,
-    [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = 13,
-    [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = 12,
-    [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = 16,
-    [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = 36,
-    [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = 16,
-    [SND_DEVICE_IN_VOICE_REC_MIC] = 62,
-    /* TODO: Update with proper acdb ids */
-    [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = 62,
-    [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = 62,
-    [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = 6,
-    [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = 5,
-};
-
-int edid_get_max_channels(void);
-
-static pthread_once_t check_op_once_ctl = PTHREAD_ONCE_INIT;
-static bool is_tmus = false;
-
-static void check_operator()
+static bool is_supported_format(audio_format_t format)
 {
-    char value[PROPERTY_VALUE_MAX];
-    int mccmnc;
-    property_get("gsm.sim.operator.numeric",value,"0");
-    mccmnc = atoi(value);
-    ALOGD("%s: tmus mccmnc %d", __func__, mccmnc);
-    switch(mccmnc) {
-    /* TMUS MCC(310), MNC(490, 260, 026) */
-    case 310490:
-    case 310260:
-    case 310026:
-        is_tmus = true;
+    if (format == AUDIO_FORMAT_MP3 ||
+            format == AUDIO_FORMAT_AAC)
+        return true;
+
+    return false;
+}
+
+static int get_snd_codec_id(audio_format_t format)
+{
+    int id = 0;
+
+    switch (format) {
+    case AUDIO_FORMAT_MP3:
+        id = SND_AUDIOCODEC_MP3;
         break;
+    case AUDIO_FORMAT_AAC:
+        id = SND_AUDIOCODEC_AAC;
+        break;
+    default:
+        ALOGE("%s: Unsupported audio format", __func__);
     }
-}
 
-static bool is_operator_tmus()
-{
-    pthread_once(&check_op_once_ctl, check_operator);
-    return is_tmus;
-}
-
-static int get_pcm_device_id(struct audio_route *ar,
-                             audio_usecase_t usecase,
-                             int device_type)
-{
-    int device_id;
-    if (device_type == PCM_PLAYBACK)
-        device_id = pcm_device_table[usecase][0];
-    else
-        device_id = pcm_device_table[usecase][1];
-    return device_id;
-}
-
-static int get_acdb_device_id(snd_device_t snd_device)
-{
-    return acdb_device_table[snd_device];
-}
-
-static void add_backend_name(char *mixer_path,
-                             snd_device_t snd_device)
-{
-    if (snd_device == SND_DEVICE_IN_BT_SCO_MIC)
-        strcat(mixer_path, " bt-sco");
-    else if(snd_device == SND_DEVICE_OUT_BT_SCO)
-        strcat(mixer_path, " bt-sco");
-    else if (snd_device == SND_DEVICE_OUT_HDMI)
-        strcat(mixer_path, " hdmi");
-    else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HDMI)
-        strcat(mixer_path, " speaker-and-hdmi");
+    return id;
 }
 
 static int enable_audio_route(struct audio_device *adev,
@@ -248,8 +173,8 @@
         snd_device = usecase->out_snd_device;
 
     strcpy(mixer_path, use_case_table[usecase->id]);
-    add_backend_name(mixer_path, snd_device);
-    ALOGD("%s: apply mixer path: %s", __func__, mixer_path);
+    platform_add_backend_name(mixer_path, snd_device);
+    ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
     audio_route_apply_path(adev->audio_route, mixer_path);
     if (update_mixer)
         audio_route_update_mixer(adev->audio_route);
@@ -274,8 +199,8 @@
     else
         snd_device = usecase->out_snd_device;
     strcpy(mixer_path, use_case_table[usecase->id]);
-    add_backend_name(mixer_path, snd_device);
-    ALOGD("%s: reset mixer path: %s", __func__, mixer_path);
+    platform_add_backend_name(mixer_path, snd_device);
+    ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
     audio_route_reset_path(adev->audio_route, mixer_path);
     if (update_mixer)
         audio_route_update_mixer(adev->audio_route);
@@ -288,8 +213,6 @@
                              snd_device_t snd_device,
                              bool update_mixer)
 {
-    int acdb_dev_id, acdb_dev_type;
-
     if (snd_device < SND_DEVICE_MIN ||
         snd_device >= SND_DEVICE_MAX) {
         ALOGE("%s: Invalid sound device %d", __func__, snd_device);
@@ -298,35 +221,19 @@
 
     adev->snd_dev_ref_cnt[snd_device]++;
     if (adev->snd_dev_ref_cnt[snd_device] > 1) {
-        ALOGD("%s: snd_device(%d: %s) is already active",
-              __func__, snd_device, device_table[snd_device]);
+        ALOGV("%s: snd_device(%d: %s) is already active",
+              __func__, snd_device, platform_get_snd_device_name(snd_device));
         return 0;
     }
 
-    acdb_dev_id = get_acdb_device_id(snd_device);
-    if (acdb_dev_id < 0) {
-        ALOGE("%s: Could not find acdb id for device(%d)",
-              __func__, snd_device);
+    if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
         adev->snd_dev_ref_cnt[snd_device]--;
         return -EINVAL;
     }
-    if (adev->acdb_send_audio_cal) {
-        ALOGD("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
-              __func__, snd_device, acdb_dev_id);
-        if (snd_device >= SND_DEVICE_OUT_BEGIN &&
-                snd_device < SND_DEVICE_OUT_END)
-            acdb_dev_type = ACDB_DEV_TYPE_OUT;
-        else
-            acdb_dev_type = ACDB_DEV_TYPE_IN;
-        adev->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
-    } else {
-        ALOGW("%s: Could not find the symbol acdb_send_audio_cal from %s",
-              __func__, LIB_ACDB_LOADER);
-    }
 
-    ALOGD("%s: snd_device(%d: %s)", __func__,
-          snd_device, device_table[snd_device]);
-    audio_route_apply_path(adev->audio_route, device_table[snd_device]);
+    ALOGV("%s: snd_device(%d: %s)", __func__,
+          snd_device, platform_get_snd_device_name(snd_device));
+    audio_route_apply_path(adev->audio_route, platform_get_snd_device_name(snd_device));
     if (update_mixer)
         audio_route_update_mixer(adev->audio_route);
 
@@ -348,9 +255,9 @@
     }
     adev->snd_dev_ref_cnt[snd_device]--;
     if (adev->snd_dev_ref_cnt[snd_device] == 0) {
-        ALOGD("%s: snd_device(%d: %s)", __func__,
-              snd_device, device_table[snd_device]);
-        audio_route_reset_path(adev->audio_route, device_table[snd_device]);
+        ALOGV("%s: snd_device(%d: %s)", __func__,
+              snd_device, platform_get_snd_device_name(snd_device));
+        audio_route_reset_path(adev->audio_route, platform_get_snd_device_name(snd_device));
         if (update_mixer)
             audio_route_update_mixer(adev->audio_route);
     }
@@ -389,7 +296,7 @@
                 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
             ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
                   __func__, use_case_table[usecase->id],
-                  device_table[usecase->out_snd_device]);
+                  platform_get_snd_device_name(usecase->out_snd_device));
             disable_audio_route(adev, usecase, false);
             switch_device[usecase->id] = true;
             num_uc_to_switch++;
@@ -455,7 +362,7 @@
                 usecase->in_snd_device != snd_device) {
             ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
                   __func__, use_case_table[usecase->id],
-                  device_table[usecase->in_snd_device]);
+                  platform_get_snd_device_name(usecase->in_snd_device));
             disable_audio_route(adev, usecase, false);
             switch_device[usecase->id] = true;
             num_uc_to_switch++;
@@ -492,60 +399,12 @@
     }
 }
 
-static int set_echo_reference(struct mixer *mixer, const char* ec_ref)
-{
-    struct mixer_ctl *ctl;
-    const char *mixer_ctl_name = "EC_REF_RX";
-
-    ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
-    if (!ctl) {
-        ALOGE("%s: Could not get ctl for mixer cmd - %s",
-              __func__, mixer_ctl_name);
-        return -EINVAL;
-    }
-    ALOGV("Setting EC Reference: %s", ec_ref);
-    mixer_ctl_set_enum_by_string(ctl, ec_ref);
-    return 0;
-}
-
-static int set_hdmi_channels(struct mixer *mixer,
-                             int channel_count)
-{
-    struct mixer_ctl *ctl;
-    const char *channel_cnt_str = NULL;
-    const char *mixer_ctl_name = "HDMI_RX Channels";
-    switch (channel_count) {
-    case 8:
-        channel_cnt_str = "Eight"; break;
-    case 7:
-        channel_cnt_str = "Seven"; break;
-    case 6:
-        channel_cnt_str = "Six"; break;
-    case 5:
-        channel_cnt_str = "Five"; break;
-    case 4:
-        channel_cnt_str = "Four"; break;
-    case 3:
-        channel_cnt_str = "Three"; break;
-    default:
-        channel_cnt_str = "Two"; break;
-    }
-    ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
-    if (!ctl) {
-        ALOGE("%s: Could not get ctl for mixer cmd - %s",
-              __func__, mixer_ctl_name);
-        return -EINVAL;
-    }
-    ALOGV("HDMI channel count: %s", channel_cnt_str);
-    mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
-    return 0;
-}
 
 /* must be called with hw device mutex locked */
 static int read_hdmi_channel_masks(struct stream_out *out)
 {
     int ret = 0;
-    int channels = edid_get_max_channels();
+    int channels = platform_edid_get_max_channels(out->dev->platform);
 
     switch (channels) {
         /*
@@ -569,258 +428,6 @@
     return ret;
 }
 
-static snd_device_t get_output_snd_device(struct audio_device *adev,
-                                          audio_devices_t devices)
-{
-    audio_mode_t mode = adev->mode;
-    snd_device_t snd_device = SND_DEVICE_NONE;
-
-    ALOGV("%s: enter: output devices(%#x)", __func__, devices);
-    if (devices == AUDIO_DEVICE_NONE ||
-        devices & AUDIO_DEVICE_BIT_IN) {
-        ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
-        goto exit;
-    }
-
-    if (mode == AUDIO_MODE_IN_CALL) {
-        if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
-            devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
-            if (adev->tty_mode == TTY_MODE_FULL)
-                snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
-            else if (adev->tty_mode == TTY_MODE_VCO)
-                snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
-            else if (adev->tty_mode == TTY_MODE_HCO)
-                snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
-            else
-                snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
-        } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
-            snd_device = SND_DEVICE_OUT_BT_SCO;
-        } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
-            snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
-        } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
-            if (is_operator_tmus())
-                snd_device = SND_DEVICE_OUT_VOICE_HANDSET_TMUS;
-            else
-                snd_device = SND_DEVICE_OUT_HANDSET;
-        }
-        if (snd_device != SND_DEVICE_NONE) {
-            goto exit;
-        }
-    }
-
-    if (popcount(devices) == 2) {
-        if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
-                        AUDIO_DEVICE_OUT_SPEAKER)) {
-            snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
-        } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
-                               AUDIO_DEVICE_OUT_SPEAKER)) {
-            snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
-        } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
-                               AUDIO_DEVICE_OUT_SPEAKER)) {
-            snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
-        } else {
-            ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
-            goto exit;
-        }
-        if (snd_device != SND_DEVICE_NONE) {
-            goto exit;
-        }
-    }
-
-    if (popcount(devices) != 1) {
-        ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
-        goto exit;
-    }
-
-    if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
-        devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
-        snd_device = SND_DEVICE_OUT_HEADPHONES;
-    } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
-        if (adev->speaker_lr_swap) {
-            snd_device = SND_DEVICE_OUT_SPEAKER_REVERSE;
-        } else {
-            snd_device = SND_DEVICE_OUT_SPEAKER;
-        }
-    } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
-        snd_device = SND_DEVICE_OUT_BT_SCO;
-    } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
-        snd_device = SND_DEVICE_OUT_HDMI ;
-    } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
-        snd_device = SND_DEVICE_OUT_HANDSET;
-    } else {
-        ALOGE("%s: Unknown device(s) %#x", __func__, devices);
-    }
-exit:
-    ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
-    return snd_device;
-}
-
-static snd_device_t get_input_snd_device(struct audio_device *adev,
-                                         audio_devices_t out_device)
-{
-    audio_source_t  source = (adev->active_input == NULL) ?
-                                AUDIO_SOURCE_DEFAULT : adev->active_input->source;
-
-    audio_mode_t    mode   = adev->mode;
-    audio_devices_t in_device = ((adev->active_input == NULL) ?
-                                    AUDIO_DEVICE_NONE : adev->active_input->device)
-                                & ~AUDIO_DEVICE_BIT_IN;
-    audio_channel_mask_t channel_mask = (adev->active_input == NULL) ?
-                                AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask;
-    snd_device_t snd_device = SND_DEVICE_NONE;
-
-    ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
-          __func__, out_device, in_device);
-    if (mode == AUDIO_MODE_IN_CALL) {
-        if (out_device == AUDIO_DEVICE_NONE) {
-            ALOGE("%s: No output device set for voice call", __func__);
-            goto exit;
-        }
-        if (adev->tty_mode != TTY_MODE_OFF) {
-            if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
-                out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
-                switch (adev->tty_mode) {
-                case TTY_MODE_FULL:
-                    snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
-                    break;
-                case TTY_MODE_VCO:
-                    snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
-                    break;
-                case TTY_MODE_HCO:
-                    snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
-                    break;
-                default:
-                    ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->tty_mode);
-                }
-                goto exit;
-            }
-        }
-        if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
-            out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
-            if (adev->mic_type_analog || adev->fluence_in_voice_call == false) {
-                snd_device = SND_DEVICE_IN_HANDSET_MIC;
-            } else {
-                if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
-                    if (is_operator_tmus())
-                        snd_device = SND_DEVICE_IN_VOICE_DMIC_EF_TMUS;
-                    else
-                        snd_device = SND_DEVICE_IN_VOICE_DMIC_EF;
-                } else if(adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE)
-                    snd_device = SND_DEVICE_IN_VOICE_DMIC_BS;
-                else
-                    snd_device = SND_DEVICE_IN_HANDSET_MIC;
-            }
-        } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
-            snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
-        } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
-            snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
-        } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
-            if (adev->fluence_in_voice_call && adev->fluence_in_spkr_mode &&
-                    adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
-                snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF;
-            } else if (adev->fluence_in_voice_call && adev->fluence_in_spkr_mode &&
-                       adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
-                snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS;
-            } else {
-                snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
-            }
-        }
-    } else if (source == AUDIO_SOURCE_CAMCORDER) {
-        if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
-            in_device & AUDIO_DEVICE_IN_BACK_MIC) {
-            snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
-        }
-    } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
-        if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
-            if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
-                if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
-                    snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF;
-                else if (adev->fluence_in_voice_rec)
-                    snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE;
-            } else if (adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
-                if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
-                    snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS;
-                else if (adev->fluence_in_voice_rec)
-                    snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE;
-            }
-
-            if (snd_device == SND_DEVICE_NONE) {
-                snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
-            }
-        }
-    } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
-        if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
-            in_device = AUDIO_DEVICE_IN_BACK_MIC;
-        if (adev->active_input) {
-            if (adev->active_input->enable_aec) {
-                if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
-                    if (adev->mic_type_analog)
-                        snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
-                    else
-                        snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
-                } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
-                    snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
-                } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
-                    snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC;
-                }
-                set_echo_reference(adev->mixer, "SLIM_RX");
-            } else
-                set_echo_reference(adev->mixer, "NONE");
-        }
-    } else if (source == AUDIO_SOURCE_DEFAULT) {
-        goto exit;
-    }
-
-
-    if (snd_device != SND_DEVICE_NONE) {
-        goto exit;
-    }
-
-    if (in_device != AUDIO_DEVICE_NONE &&
-            !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
-            !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
-        if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
-            snd_device = SND_DEVICE_IN_HANDSET_MIC;
-        } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
-            if (adev->mic_type_analog)
-                snd_device = SND_DEVICE_IN_HANDSET_MIC;
-            else
-                snd_device = SND_DEVICE_IN_SPEAKER_MIC;
-        } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
-            snd_device = SND_DEVICE_IN_HEADSET_MIC;
-        } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
-            snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
-        } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
-            snd_device = SND_DEVICE_IN_HDMI_MIC;
-        } else {
-            ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
-            ALOGW("%s: Using default handset-mic", __func__);
-            snd_device = SND_DEVICE_IN_HANDSET_MIC;
-        }
-    } else {
-        if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
-            snd_device = SND_DEVICE_IN_HANDSET_MIC;
-        } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
-            snd_device = SND_DEVICE_IN_HEADSET_MIC;
-        } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
-            snd_device = SND_DEVICE_IN_SPEAKER_MIC;
-        } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
-            snd_device = SND_DEVICE_IN_HANDSET_MIC;
-        } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
-            snd_device = SND_DEVICE_IN_BT_SCO_MIC;
-        } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
-            snd_device = SND_DEVICE_IN_HDMI_MIC;
-        } else {
-            ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
-            ALOGW("%s: Using default handset-mic", __func__);
-            snd_device = SND_DEVICE_IN_HANDSET_MIC;
-        }
-    }
-exit:
-    ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
-    return snd_device;
-}
-
 static struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
                                                    audio_usecase_t uc_id)
 {
@@ -843,7 +450,6 @@
     struct audio_usecase *usecase = NULL;
     struct audio_usecase *vc_usecase = NULL;
     struct listnode *node;
-    int acdb_rx_id, acdb_tx_id;
     int status = 0;
 
     usecase = get_usecase_from_list(adev, uc_id);
@@ -853,8 +459,9 @@
     }
 
     if (usecase->type == VOICE_CALL) {
-        out_snd_device = get_output_snd_device(adev, usecase->stream.out->devices);
-        in_snd_device = get_input_snd_device(adev, usecase->stream.out->devices);
+        out_snd_device = platform_get_output_snd_device(adev->platform,
+                                                        usecase->stream.out->devices);
+        in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
         usecase->devices = usecase->stream.out->devices;
     } else {
         /*
@@ -875,7 +482,7 @@
             usecase->devices = usecase->stream.out->devices;
             in_snd_device = SND_DEVICE_NONE;
             if (out_snd_device == SND_DEVICE_NONE) {
-                out_snd_device = get_output_snd_device(adev,
+                out_snd_device = platform_get_output_snd_device(adev->platform,
                                             usecase->stream.out->devices);
                 if (usecase->stream.out == adev->primary_output &&
                         adev->active_input &&
@@ -889,10 +496,11 @@
             if (in_snd_device == SND_DEVICE_NONE) {
                 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
                         adev->primary_output && !adev->primary_output->standby) {
-                    in_snd_device = get_input_snd_device(adev,
+                    in_snd_device = platform_get_input_snd_device(adev->platform,
                                         adev->primary_output->devices);
                 } else {
-                    in_snd_device = get_input_snd_device(adev, AUDIO_DEVICE_NONE);
+                    in_snd_device = platform_get_input_snd_device(adev->platform,
+                                                                  AUDIO_DEVICE_NONE);
                 }
             }
         }
@@ -904,27 +512,16 @@
     }
 
     ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
-          out_snd_device, device_table[out_snd_device],
-          in_snd_device,  device_table[in_snd_device]);
+          out_snd_device, platform_get_snd_device_name(out_snd_device),
+          in_snd_device,  platform_get_snd_device_name(in_snd_device));
 
     /*
      * Limitation: While in call, to do a device switch we need to disable
      * and enable both RX and TX devices though one of them is same as current
      * device.
      */
-    if (usecase->type == VOICE_CALL && adev->csd_client != NULL &&
-            usecase->in_snd_device != SND_DEVICE_NONE &&
-            usecase->out_snd_device != SND_DEVICE_NONE) {
-        /* This must be called before disabling the mixer controls on APQ side */
-        if (adev->csd_disable_device == NULL) {
-            ALOGE("%s: dlsym error for csd_client_disable_device", __func__);
-        } else {
-            status = adev->csd_disable_device();
-            if (status < 0) {
-                ALOGE("%s: csd_client_disable_device, failed, error %d",
-                      __func__, status);
-            }
-        }
+    if (usecase->type == VOICE_CALL) {
+        status = platform_switch_voice_call_device_pre(adev->platform);
     }
 
     /* Disable current sound devices */
@@ -950,6 +547,11 @@
         enable_snd_device(adev, in_snd_device, false);
     }
 
+    if (usecase->type == VOICE_CALL)
+        status = platform_switch_voice_call_device_post(adev->platform,
+                                                        out_snd_device,
+                                                        in_snd_device);
+
     audio_route_update_mixer(adev->audio_route);
 
     usecase->in_snd_device = in_snd_device;
@@ -957,28 +559,6 @@
 
     enable_audio_route(adev, usecase, true);
 
-    if (usecase->type == VOICE_CALL && adev->csd_client) {
-        if (adev->csd_enable_device == NULL) {
-            ALOGE("%s: dlsym error for csd_client_enable_device",
-                  __func__);
-        } else {
-            acdb_rx_id = get_acdb_device_id(out_snd_device);
-            acdb_tx_id = get_acdb_device_id(in_snd_device);
-
-            if (acdb_rx_id > 0 || acdb_tx_id > 0) {
-                status = adev->csd_enable_device(acdb_rx_id, acdb_tx_id,
-                                                 adev->acdb_settings);
-                if (status < 0) {
-                    ALOGE("%s: csd_client_enable_device, failed, error %d",
-                          __func__, status);
-                }
-            } else {
-                ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
-                      acdb_rx_id, acdb_tx_id);
-            }
-        }
-    }
-
     return status;
 }
 
@@ -990,7 +570,7 @@
 
     adev->active_input = NULL;
 
-    ALOGD("%s: enter: usecase(%d: %s)", __func__,
+    ALOGV("%s: enter: usecase(%d: %s)", __func__,
           in->usecase, use_case_table[in->usecase]);
     uc_info = get_usecase_from_list(adev, in->usecase);
     if (uc_info == NULL) {
@@ -1008,7 +588,7 @@
     list_remove(&uc_info->list);
     free(uc_info);
 
-    ALOGD("%s: exit: status(%d)", __func__, ret);
+    ALOGV("%s: exit: status(%d)", __func__, ret);
     return ret;
 }
 
@@ -1019,10 +599,8 @@
     struct audio_usecase *uc_info;
     struct audio_device *adev = in->dev;
 
-    ALOGD("%s: enter: usecase(%d)", __func__, in->usecase);
-    in->pcm_device_id = get_pcm_device_id(adev->audio_route,
-                                          in->usecase,
-                                          PCM_CAPTURE);
+    ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
+    in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
     if (in->pcm_device_id < 0) {
         ALOGE("%s: Could not find PCM device id for the usecase(%d)",
               __func__, in->usecase);
@@ -1053,7 +631,7 @@
         ret = -EIO;
         goto error_open;
     }
-    ALOGD("%s: exit", __func__);
+    ALOGV("%s: exit", __func__);
     return ret;
 
 error_open:
@@ -1066,13 +644,229 @@
     return ret;
 }
 
+/* must be called with out->lock locked */
+static int send_offload_cmd_l(struct stream_out* out, int command)
+{
+    struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
+
+    ALOGVV("%s %d", __func__, command);
+
+    cmd->cmd = command;
+    list_add_tail(&out->offload_cmd_list, &cmd->node);
+    pthread_cond_signal(&out->offload_cond);
+    return 0;
+}
+
+/* must be called iwth out->lock locked */
+static void stop_compressed_output_l(struct stream_out *out)
+{
+    out->offload_state = OFFLOAD_STATE_IDLE;
+    out->playback_started = 0;
+    out->send_new_metadata = 1;
+    if (out->compr != NULL) {
+        compress_stop(out->compr);
+        while (out->offload_thread_blocked) {
+            pthread_cond_wait(&out->cond, &out->lock);
+        }
+    }
+}
+
+static void *offload_thread_loop(void *context)
+{
+    struct stream_out *out = (struct stream_out *) context;
+    struct listnode *item;
+
+    out->offload_state = OFFLOAD_STATE_IDLE;
+    out->playback_started = 0;
+
+    setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
+    set_sched_policy(0, SP_FOREGROUND);
+    prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
+
+    ALOGV("%s", __func__);
+    pthread_mutex_lock(&out->lock);
+    for (;;) {
+        struct offload_cmd *cmd = NULL;
+        stream_callback_event_t event;
+        bool send_callback = false;
+
+        ALOGVV("%s offload_cmd_list %d out->offload_state %d",
+              __func__, list_empty(&out->offload_cmd_list),
+              out->offload_state);
+        if (list_empty(&out->offload_cmd_list)) {
+            ALOGV("%s SLEEPING", __func__);
+            pthread_cond_wait(&out->offload_cond, &out->lock);
+            ALOGV("%s RUNNING", __func__);
+            continue;
+        }
+
+        item = list_head(&out->offload_cmd_list);
+        cmd = node_to_item(item, struct offload_cmd, node);
+        list_remove(item);
+
+        ALOGVV("%s STATE %d CMD %d out->compr %p",
+               __func__, out->offload_state, cmd->cmd, out->compr);
+
+        if (cmd->cmd == OFFLOAD_CMD_EXIT) {
+            free(cmd);
+            break;
+        }
+
+        if (out->compr == NULL) {
+            ALOGE("%s: Compress handle is NULL", __func__);
+            pthread_cond_signal(&out->cond);
+            continue;
+        }
+        out->offload_thread_blocked = true;
+        pthread_mutex_unlock(&out->lock);
+        send_callback = false;
+        switch(cmd->cmd) {
+        case OFFLOAD_CMD_WAIT_FOR_BUFFER:
+            compress_wait(out->compr, -1);
+            send_callback = true;
+            event = STREAM_CBK_EVENT_WRITE_READY;
+            break;
+        case OFFLOAD_CMD_PARTIAL_DRAIN:
+            compress_next_track(out->compr);
+            compress_partial_drain(out->compr);
+            send_callback = true;
+            event = STREAM_CBK_EVENT_DRAIN_READY;
+            break;
+        case OFFLOAD_CMD_DRAIN:
+            compress_drain(out->compr);
+            send_callback = true;
+            event = STREAM_CBK_EVENT_DRAIN_READY;
+            break;
+        default:
+            ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
+            break;
+        }
+        pthread_mutex_lock(&out->lock);
+        out->offload_thread_blocked = false;
+        pthread_cond_signal(&out->cond);
+        if (send_callback) {
+            out->offload_callback(event, NULL, out->offload_cookie);
+        }
+        free(cmd);
+    }
+
+    pthread_cond_signal(&out->cond);
+    while (!list_empty(&out->offload_cmd_list)) {
+        item = list_head(&out->offload_cmd_list);
+        list_remove(item);
+        free(node_to_item(item, struct offload_cmd, node));
+    }
+    pthread_mutex_unlock(&out->lock);
+
+    return NULL;
+}
+
+static int create_offload_callback_thread(struct stream_out *out)
+{
+    pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
+    list_init(&out->offload_cmd_list);
+    pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
+                    offload_thread_loop, out);
+    return 0;
+}
+
+static int destroy_offload_callback_thread(struct stream_out *out)
+{
+    pthread_mutex_lock(&out->lock);
+    stop_compressed_output_l(out);
+    send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
+
+    pthread_mutex_unlock(&out->lock);
+    pthread_join(out->offload_thread, (void **) NULL);
+    pthread_cond_destroy(&out->offload_cond);
+
+    return 0;
+}
+
+static bool allow_hdmi_channel_config(struct audio_device *adev)
+{
+    struct listnode *node;
+    struct audio_usecase *usecase;
+    bool ret = true;
+
+    list_for_each(node, &adev->usecase_list) {
+        usecase = node_to_item(node, struct audio_usecase, list);
+        if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
+            /*
+             * If voice call is already existing, do not proceed further to avoid
+             * disabling/enabling both RX and TX devices, CSD calls, etc.
+             * Once the voice call done, the HDMI channels can be configured to
+             * max channels of remaining use cases.
+             */
+            if (usecase->id == USECASE_VOICE_CALL) {
+                ALOGD("%s: voice call is active, no change in HDMI channels",
+                      __func__);
+                ret = false;
+                break;
+            } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
+                ALOGD("%s: multi channel playback is active, "
+                      "no change in HDMI channels", __func__);
+                ret = false;
+                break;
+            }
+        }
+    }
+    return ret;
+}
+
+static int check_and_set_hdmi_channels(struct audio_device *adev,
+                                       unsigned int channels)
+{
+    struct listnode *node;
+    struct audio_usecase *usecase;
+
+    /* Check if change in HDMI channel config is allowed */
+    if (!allow_hdmi_channel_config(adev))
+        return 0;
+
+    if (channels == adev->cur_hdmi_channels) {
+        ALOGD("%s: Requested channels are same as current", __func__);
+        return 0;
+    }
+
+    platform_set_hdmi_channels(adev->platform, channels);
+    adev->cur_hdmi_channels = channels;
+
+    /*
+     * Deroute all the playback streams routed to HDMI so that
+     * the back end is deactivated. Note that backend will not
+     * be deactivated if any one stream is connected to it.
+     */
+    list_for_each(node, &adev->usecase_list) {
+        usecase = node_to_item(node, struct audio_usecase, list);
+        if (usecase->type == PCM_PLAYBACK &&
+                usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
+            disable_audio_route(adev, usecase, true);
+        }
+    }
+
+    /*
+     * Enable all the streams disabled above. Now the HDMI backend
+     * will be activated with new channel configuration
+     */
+    list_for_each(node, &adev->usecase_list) {
+        usecase = node_to_item(node, struct audio_usecase, list);
+        if (usecase->type == PCM_PLAYBACK &&
+                usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
+            enable_audio_route(adev, usecase, true);
+        }
+    }
+
+    return 0;
+}
+
 static int stop_output_stream(struct stream_out *out)
 {
     int i, ret = 0;
     struct audio_usecase *uc_info;
     struct audio_device *adev = out->dev;
 
-    ALOGD("%s: enter: usecase(%d: %s)", __func__,
+    ALOGV("%s: enter: usecase(%d: %s)", __func__,
           out->usecase, use_case_table[out->usecase]);
     uc_info = get_usecase_from_list(adev, out->usecase);
     if (uc_info == NULL) {
@@ -1081,6 +875,10 @@
         return -EINVAL;
     }
 
+    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
+            adev->visualizer_stop_output != NULL)
+        adev->visualizer_stop_output(out->handle);
+
     /* 1. Get and set stream specific mixer controls */
     disable_audio_route(adev, uc_info, true);
 
@@ -1090,7 +888,11 @@
     list_remove(&uc_info->list);
     free(uc_info);
 
-    ALOGD("%s: exit: status(%d)", __func__, ret);
+    /* Must be called after removing the usecase from list */
+    if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
+        check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
+
+    ALOGV("%s: exit: status(%d)", __func__, ret);
     return ret;
 }
 
@@ -1100,11 +902,9 @@
     struct audio_usecase *uc_info;
     struct audio_device *adev = out->dev;
 
-    ALOGD("%s: enter: usecase(%d: %s) devices(%#x)",
+    ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
           __func__, out->usecase, use_case_table[out->usecase], out->devices);
-    out->pcm_device_id = get_pcm_device_id(adev->audio_route,
-                                           out->usecase,
-                                           PCM_PLAYBACK);
+    out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
     if (out->pcm_device_id < 0) {
         ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
               __func__, out->pcm_device_id, out->usecase);
@@ -1120,24 +920,46 @@
     uc_info->in_snd_device = SND_DEVICE_NONE;
     uc_info->out_snd_device = SND_DEVICE_NONE;
 
+    /* This must be called before adding this usecase to the list */
+    if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
+        check_and_set_hdmi_channels(adev, out->config.channels);
+
     list_add_tail(&adev->usecase_list, &uc_info->list);
 
     select_devices(adev, out->usecase);
 
     ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
           __func__, 0, out->pcm_device_id);
-    out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
-                           PCM_OUT, &out->config);
-    if (out->pcm && !pcm_is_ready(out->pcm)) {
-        ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
-        pcm_close(out->pcm);
+    if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
+        out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
+                               PCM_OUT | PCM_MONOTONIC, &out->config);
+        if (out->pcm && !pcm_is_ready(out->pcm)) {
+            ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
+            pcm_close(out->pcm);
+            out->pcm = NULL;
+            ret = -EIO;
+            goto error_open;
+        }
+    } else {
         out->pcm = NULL;
-        ret = -EIO;
-        goto error_pcm_open;
+        out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
+                                   COMPRESS_IN, &out->compr_config);
+        if (out->compr && !is_compress_ready(out->compr)) {
+            ALOGE("%s: %s", __func__, compress_get_error(out->compr));
+            compress_close(out->compr);
+            out->compr = NULL;
+            ret = -EIO;
+            goto error_open;
+        }
+        if (out->offload_callback)
+            compress_nonblock(out->compr, out->non_blocking);
+
+        if (adev->visualizer_start_output != NULL)
+            adev->visualizer_start_output(out->handle);
     }
-    ALOGD("%s: exit", __func__);
+    ALOGV("%s: exit", __func__);
     return 0;
-error_pcm_open:
+error_open:
     stop_output_stream(out);
 error_config:
     return ret;
@@ -1148,18 +970,10 @@
     int i, ret = 0;
     struct audio_usecase *uc_info;
 
-    ALOGD("%s: enter", __func__);
+    ALOGV("%s: enter", __func__);
     adev->in_call = false;
-    if (adev->csd_client) {
-        if (adev->csd_stop_voice == NULL) {
-            ALOGE("dlsym error for csd_client_disable_device");
-        } else {
-            ret = adev->csd_stop_voice();
-            if (ret < 0) {
-                ALOGE("%s: csd_client error %d\n", __func__, ret);
-            }
-        }
-    }
+
+    ret = platform_stop_voice_call(adev->platform);
 
     /* 1. Close the PCM devices */
     if (adev->voice_call_rx) {
@@ -1188,7 +1002,7 @@
     list_remove(&uc_info->list);
     free(uc_info);
 
-    ALOGD("%s: exit: status(%d)", __func__, ret);
+    ALOGV("%s: exit: status(%d)", __func__, ret);
     return ret;
 }
 
@@ -1198,7 +1012,7 @@
     struct audio_usecase *uc_info;
     int pcm_dev_rx_id, pcm_dev_tx_id;
 
-    ALOGD("%s: enter", __func__);
+    ALOGV("%s: enter", __func__);
 
     uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
     uc_info->id = USECASE_VOICE_CALL;
@@ -1212,10 +1026,8 @@
 
     select_devices(adev, USECASE_VOICE_CALL);
 
-    pcm_dev_rx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
-                                      PCM_PLAYBACK);
-    pcm_dev_tx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
-                                      PCM_CAPTURE);
+    pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
+    pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
 
     if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
         ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
@@ -1228,7 +1040,7 @@
           __func__, SOUND_CARD, pcm_dev_rx_id);
     adev->voice_call_rx = pcm_open(SOUND_CARD,
                                   pcm_dev_rx_id,
-                                  PCM_OUT, &pcm_config_voice_call);
+                                  PCM_OUT | PCM_MONOTONIC, &pcm_config_voice_call);
     if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) {
         ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx));
         ret = -EIO;
@@ -1245,22 +1057,18 @@
         ret = -EIO;
         goto error_start_voice;
     }
+
+    /* set cached volume */
+    set_voice_volume_l(adev, adev->voice_volume);
+
     pcm_start(adev->voice_call_rx);
     pcm_start(adev->voice_call_tx);
 
-    if (adev->csd_client) {
-        if (adev->csd_start_voice == NULL) {
-            ALOGE("dlsym error for csd_client_start_voice");
-            goto error_start_voice;
-        } else {
-            ret = adev->csd_start_voice();
-            if (ret < 0) {
-                ALOGE("%s: csd_start_voice error %d\n", __func__, ret);
-                goto error_start_voice;
-            }
-        }
+    ret = platform_start_voice_call(adev->platform);
+    if (ret < 0) {
+        ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
+        goto error_start_voice;
     }
-
     adev->in_call = true;
     return 0;
 
@@ -1303,26 +1111,26 @@
 {
     size_t size = 0;
 
-    if (check_input_parameters(sample_rate, format, channel_count) != 0) return 0;
+    if (check_input_parameters(sample_rate, format, channel_count) != 0)
+        return 0;
 
-    if (sample_rate == 8000 || sample_rate == 16000 || sample_rate == 32000) {
-        size = (sample_rate * 20) / 1000;
-    } else if (sample_rate == 11025 || sample_rate == 12000) {
-        size = 256;
-    } else if (sample_rate == 22050 || sample_rate == 24000) {
-        size = 512;
-    } else if (sample_rate == 44100 || sample_rate == 48000) {
-        size = 1024;
-    }
+    size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
+    /* ToDo: should use frame_size computed based on the format and
+       channel_count here. */
+    size *= sizeof(short) * channel_count;
 
-    return size * sizeof(short) * channel_count;
+    /* make sure the size is multiple of 64 */
+    size += 0x3f;
+    size &= ~0x3f;
+
+    return size;
 }
 
 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
 {
     struct stream_out *out = (struct stream_out *)stream;
 
-    return out->config.rate;
+    return out->sample_rate;
 }
 
 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
@@ -1334,6 +1142,10 @@
 {
     struct stream_out *out = (struct stream_out *)stream;
 
+    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
+        return out->compr_config.fragment_size;
+    }
+
     return out->config.period_size * audio_stream_frame_size(stream);
 }
 
@@ -1346,7 +1158,9 @@
 
 static audio_format_t out_get_format(const struct audio_stream *stream)
 {
-    return AUDIO_FORMAT_PCM_16_BIT;
+    struct stream_out *out = (struct stream_out *)stream;
+
+    return out->format;
 }
 
 static int out_set_format(struct audio_stream *stream, audio_format_t format)
@@ -1358,22 +1172,33 @@
 {
     struct stream_out *out = (struct stream_out *)stream;
     struct audio_device *adev = out->dev;
-    ALOGD("%s: enter: usecase(%d: %s)", __func__,
-          out->usecase, use_case_table[out->usecase]);
-    pthread_mutex_lock(&out->lock);
 
+    ALOGV("%s: enter: usecase(%d: %s)", __func__,
+          out->usecase, use_case_table[out->usecase]);
+
+    pthread_mutex_lock(&out->lock);
     if (!out->standby) {
         out->standby = true;
-        if (out->pcm) {
-            pcm_close(out->pcm);
-            out->pcm = NULL;
+        if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
+            if (out->pcm) {
+                pcm_close(out->pcm);
+                out->pcm = NULL;
+            }
+        } else {
+            stop_compressed_output_l(out);
+            out->gapless_mdata.encoder_delay = 0;
+            out->gapless_mdata.encoder_padding = 0;
+            if (out->compr != NULL) {
+                compress_close(out->compr);
+                out->compr = NULL;
+            }
         }
         pthread_mutex_lock(&adev->lock);
         stop_output_stream(out);
         pthread_mutex_unlock(&adev->lock);
     }
     pthread_mutex_unlock(&out->lock);
-    ALOGD("%s: exit", __func__);
+    ALOGV("%s: exit", __func__);
     return 0;
 }
 
@@ -1382,6 +1207,39 @@
     return 0;
 }
 
+static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
+{
+    int ret = 0;
+    char value[32];
+    struct compr_gapless_mdata tmp_mdata;
+
+    if (!out || !parms) {
+        return -EINVAL;
+    }
+
+    ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
+    if (ret >= 0) {
+        tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
+    } else {
+        return -EINVAL;
+    }
+
+    ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
+    if (ret >= 0) {
+        tmp_mdata.encoder_padding = atoi(value);
+    } else {
+        return -EINVAL;
+    }
+
+    out->gapless_mdata = tmp_mdata;
+    out->send_new_metadata = 1;
+    ALOGV("%s new encoder delay %u and padding %u", __func__,
+          out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
+
+    return 0;
+}
+
+
 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
 {
     struct stream_out *out = (struct stream_out *)stream;
@@ -1447,7 +1305,7 @@
             }
         }
 
-        if ((adev->mode != AUDIO_MODE_IN_CALL) && adev->in_call &&
+        if ((adev->mode == AUDIO_MODE_NORMAL) && adev->in_call &&
                 (out == adev->primary_output)) {
             stop_voice_call(adev);
         }
@@ -1455,8 +1313,13 @@
         pthread_mutex_unlock(&adev->lock);
         pthread_mutex_unlock(&out->lock);
     }
+
+    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
+        parse_compress_metadata(out, parms);
+    }
+
     str_parms_destroy(parms);
-    ALOGD("%s: exit: code(%d)", __func__, ret);
+    ALOGV("%s: exit: code(%d)", __func__, ret);
     return ret;
 }
 
@@ -1470,7 +1333,7 @@
     size_t i, j;
     int ret;
     bool first = true;
-    ALOGD("%s: enter: keys - %s", __func__, keys);
+    ALOGV("%s: enter: keys - %s", __func__, keys);
     ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
     if (ret >= 0) {
         value[0] = '\0';
@@ -1495,7 +1358,7 @@
     }
     str_parms_destroy(query);
     str_parms_destroy(reply);
-    ALOGD("%s: exit: returns - %s", __func__, str);
+    ALOGV("%s: exit: returns - %s", __func__, str);
     return str;
 }
 
@@ -1503,18 +1366,40 @@
 {
     struct stream_out *out = (struct stream_out *)stream;
 
-    return (out->config.period_count * out->config.period_size * 1000) / (out->config.rate);
+    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
+        return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
+
+    return (out->config.period_count * out->config.period_size * 1000) /
+           (out->config.rate);
 }
 
 static int out_set_volume(struct audio_stream_out *stream, float left,
                           float right)
 {
     struct stream_out *out = (struct stream_out *)stream;
+    int volume[2];
+
     if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
         /* only take left channel into account: the API is for stereo anyway */
         out->muted = (left == 0.0f);
         return 0;
+    } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
+        const char *mixer_ctl_name = "Compress Playback Volume";
+        struct audio_device *adev = out->dev;
+        struct mixer_ctl *ctl;
+
+        ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
+        if (!ctl) {
+            ALOGE("%s: Could not get ctl for mixer cmd - %s",
+                  __func__, mixer_ctl_name);
+            return -EINVAL;
+        }
+        volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
+        volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
+        mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
+        return 0;
     }
+
     return -ENOSYS;
 }
 
@@ -1523,7 +1408,7 @@
 {
     struct stream_out *out = (struct stream_out *)stream;
     struct audio_device *adev = out->dev;
-    int i, ret = -1;
+    ssize_t ret = 0;
 
     pthread_mutex_lock(&out->lock);
     if (out->standby) {
@@ -1531,17 +1416,42 @@
         pthread_mutex_lock(&adev->lock);
         ret = start_output_stream(out);
         pthread_mutex_unlock(&adev->lock);
+        /* ToDo: If use case is compress offload should return 0 */
         if (ret != 0) {
             out->standby = true;
             goto exit;
         }
     }
 
-    if (out->pcm) {
-        if (out->muted)
-            memset((void *)buffer, 0, bytes);
-        //ALOGV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
-        ret = pcm_write(out->pcm, (void *)buffer, bytes);
+    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
+        ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
+        if (out->send_new_metadata) {
+            ALOGVV("send new gapless metadata");
+            compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
+            out->send_new_metadata = 0;
+        }
+
+        ret = compress_write(out->compr, buffer, bytes);
+        ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
+        if (ret >= 0 && ret < (ssize_t)bytes) {
+            send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
+        }
+        if (!out->playback_started) {
+            compress_start(out->compr);
+            out->playback_started = 1;
+            out->offload_state = OFFLOAD_STATE_PLAYING;
+        }
+        pthread_mutex_unlock(&out->lock);
+        return ret;
+    } else {
+        if (out->pcm) {
+            if (out->muted)
+                memset((void *)buffer, 0, bytes);
+            ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
+            ret = pcm_write(out->pcm, (void *)buffer, bytes);
+            if (ret == 0)
+                out->written += bytes / (out->config.channels * sizeof(short));
+        }
     }
 
 exit:
@@ -1560,7 +1470,20 @@
 static int out_get_render_position(const struct audio_stream_out *stream,
                                    uint32_t *dsp_frames)
 {
-    return -EINVAL;
+    struct stream_out *out = (struct stream_out *)stream;
+    *dsp_frames = 0;
+    if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
+        pthread_mutex_lock(&out->lock);
+        if (out->compr != NULL) {
+            compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
+                    &out->sample_rate);
+            ALOGVV("%s rendered frames %d sample_rate %d",
+                   __func__, *dsp_frames, out->sample_rate);
+        }
+        pthread_mutex_unlock(&out->lock);
+        return 0;
+    } else
+        return -EINVAL;
 }
 
 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
@@ -1579,6 +1502,126 @@
     return -EINVAL;
 }
 
+static int out_get_presentation_position(const struct audio_stream_out *stream,
+                                   uint64_t *frames, struct timespec *timestamp)
+{
+    struct stream_out *out = (struct stream_out *)stream;
+    int ret = -1;
+    unsigned long dsp_frames;
+
+    pthread_mutex_lock(&out->lock);
+
+    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
+        if (out->compr != NULL) {
+            compress_get_tstamp(out->compr, &dsp_frames,
+                    &out->sample_rate);
+            ALOGVV("%s rendered frames %ld sample_rate %d",
+                   __func__, dsp_frames, out->sample_rate);
+            *frames = dsp_frames;
+            ret = 0;
+            /* this is the best we can do */
+            clock_gettime(CLOCK_MONOTONIC, timestamp);
+        }
+    } else {
+        if (out->pcm) {
+            size_t avail;
+            if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
+                size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
+                int64_t signed_frames = out->written - kernel_buffer_size + avail;
+                // This adjustment accounts for buffering after app processor.
+                // It is based on estimated DSP latency per use case, rather than exact.
+                signed_frames -=
+                    (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
+
+                // It would be unusual for this value to be negative, but check just in case ...
+                if (signed_frames >= 0) {
+                    *frames = signed_frames;
+                    ret = 0;
+                }
+            }
+        }
+    }
+
+    pthread_mutex_unlock(&out->lock);
+
+    return ret;
+}
+
+static int out_set_callback(struct audio_stream_out *stream,
+            stream_callback_t callback, void *cookie)
+{
+    struct stream_out *out = (struct stream_out *)stream;
+
+    ALOGV("%s", __func__);
+    pthread_mutex_lock(&out->lock);
+    out->offload_callback = callback;
+    out->offload_cookie = cookie;
+    pthread_mutex_unlock(&out->lock);
+    return 0;
+}
+
+static int out_pause(struct audio_stream_out* stream)
+{
+    struct stream_out *out = (struct stream_out *)stream;
+    int status = -ENOSYS;
+    ALOGV("%s", __func__);
+    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
+        pthread_mutex_lock(&out->lock);
+        if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
+            status = compress_pause(out->compr);
+            out->offload_state = OFFLOAD_STATE_PAUSED;
+        }
+        pthread_mutex_unlock(&out->lock);
+    }
+    return status;
+}
+
+static int out_resume(struct audio_stream_out* stream)
+{
+    struct stream_out *out = (struct stream_out *)stream;
+    int status = -ENOSYS;
+    ALOGV("%s", __func__);
+    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
+        status = 0;
+        pthread_mutex_lock(&out->lock);
+        if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
+            status = compress_resume(out->compr);
+            out->offload_state = OFFLOAD_STATE_PLAYING;
+        }
+        pthread_mutex_unlock(&out->lock);
+    }
+    return status;
+}
+
+static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
+{
+    struct stream_out *out = (struct stream_out *)stream;
+    int status = -ENOSYS;
+    ALOGV("%s", __func__);
+    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
+        pthread_mutex_lock(&out->lock);
+        if (type == AUDIO_DRAIN_EARLY_NOTIFY)
+            status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
+        else
+            status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
+        pthread_mutex_unlock(&out->lock);
+    }
+    return status;
+}
+
+static int out_flush(struct audio_stream_out* stream)
+{
+    struct stream_out *out = (struct stream_out *)stream;
+    ALOGV("%s", __func__);
+    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
+        pthread_mutex_lock(&out->lock);
+        stop_compressed_output_l(out);
+        pthread_mutex_unlock(&out->lock);
+        return 0;
+    }
+    return -ENOSYS;
+}
+
 /** audio_stream_in implementation **/
 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
 {
@@ -1621,7 +1664,7 @@
     struct stream_in *in = (struct stream_in *)stream;
     struct audio_device *adev = in->dev;
     int status = 0;
-    ALOGD("%s: enter", __func__);
+    ALOGV("%s: enter", __func__);
     pthread_mutex_lock(&in->lock);
     if (!in->standby) {
         in->standby = true;
@@ -1634,7 +1677,7 @@
         pthread_mutex_unlock(&adev->lock);
     }
     pthread_mutex_unlock(&in->lock);
-    ALOGD("%s: exit:  status(%d)", __func__, status);
+    ALOGV("%s: exit:  status(%d)", __func__, status);
     return status;
 }
 
@@ -1652,7 +1695,7 @@
     char value[32];
     int ret, val = 0;
 
-    ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
+    ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
     parms = str_parms_create_str(kvpairs);
 
     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
@@ -1682,7 +1725,7 @@
     pthread_mutex_unlock(&in->lock);
 
     str_parms_destroy(parms);
-    ALOGD("%s: exit: status(%d)", __func__, ret);
+    ALOGV("%s: exit: status(%d)", __func__, ret);
     return ret;
 }
 
@@ -1773,14 +1816,14 @@
 static int in_add_audio_effect(const struct audio_stream *stream,
                                effect_handle_t effect)
 {
-    ALOGD("%s: effect %p", __func__, effect);
+    ALOGV("%s: effect %p", __func__, effect);
     return add_remove_audio_effect(stream, effect, true);
 }
 
 static int in_remove_audio_effect(const struct audio_stream *stream,
                                   effect_handle_t effect)
 {
-    ALOGD("%s: effect %p", __func__, effect);
+    ALOGV("%s: effect %p", __func__, effect);
     return add_remove_audio_effect(stream, effect, false);
 }
 
@@ -1795,7 +1838,7 @@
     struct stream_out *out;
     int i, ret;
 
-    ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
+    ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
           __func__, config->sample_rate, config->channel_mask, devices, flags);
     *stream_out = NULL;
     out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
@@ -1803,10 +1846,14 @@
     if (devices == AUDIO_DEVICE_NONE)
         devices = AUDIO_DEVICE_OUT_SPEAKER;
 
-    out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
-    out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
     out->flags = flags;
     out->devices = devices;
+    out->dev = adev;
+    out->format = config->format;
+    out->sample_rate = config->sample_rate;
+    out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
+    out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
+    out->handle = handle;
 
     /* Init use case and pcm_config */
     if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
@@ -1814,12 +1861,8 @@
         pthread_mutex_lock(&adev->lock);
         ret = read_hdmi_channel_masks(out);
         pthread_mutex_unlock(&adev->lock);
-        if (ret != 0) {
-            /* If HDMI does not support multi channel playback, set the default */
-            out->config.channels = popcount(out->channel_mask);
-            set_hdmi_channels(adev->mixer, out->config.channels);
+        if (ret != 0)
             goto error_open;
-        }
 
         if (config->sample_rate == 0)
             config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
@@ -1827,18 +1870,70 @@
             config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
 
         out->channel_mask = config->channel_mask;
+        out->sample_rate = config->sample_rate;
         out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
         out->config = pcm_config_hdmi_multi;
         out->config.rate = config->sample_rate;
         out->config.channels = popcount(out->channel_mask);
         out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
-        set_hdmi_channels(adev->mixer, out->config.channels);
     } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
         out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
         out->config = pcm_config_deep_buffer;
+        out->sample_rate = out->config.rate;
+    } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
+        if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
+            config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
+            ALOGE("%s: Unsupported Offload information", __func__);
+            ret = -EINVAL;
+            goto error_open;
+        }
+        if (!is_supported_format(config->offload_info.format)) {
+            ALOGE("%s: Unsupported audio format", __func__);
+            ret = -EINVAL;
+            goto error_open;
+        }
+
+        out->compr_config.codec = (struct snd_codec *)
+                                    calloc(1, sizeof(struct snd_codec));
+
+        out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
+        if (config->offload_info.channel_mask)
+            out->channel_mask = config->offload_info.channel_mask;
+        else if (config->channel_mask)
+            out->channel_mask = config->channel_mask;
+        out->format = config->offload_info.format;
+        out->sample_rate = config->offload_info.sample_rate;
+
+        out->stream.set_callback = out_set_callback;
+        out->stream.pause = out_pause;
+        out->stream.resume = out_resume;
+        out->stream.drain = out_drain;
+        out->stream.flush = out_flush;
+
+        out->compr_config.codec->id =
+                get_snd_codec_id(config->offload_info.format);
+        out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
+        out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
+        out->compr_config.codec->sample_rate =
+                    compress_get_alsa_rate(config->offload_info.sample_rate);
+        out->compr_config.codec->bit_rate =
+                    config->offload_info.bit_rate;
+        out->compr_config.codec->ch_in =
+                    popcount(config->channel_mask);
+        out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
+
+        if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
+            out->non_blocking = 1;
+
+        out->send_new_metadata = 1;
+        create_offload_callback_thread(out);
+        ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
+                __func__, config->offload_info.version,
+                config->offload_info.bit_rate);
     } else {
         out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
         out->config = pcm_config_low_latency;
+        out->sample_rate = out->config.rate;
     }
 
     if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
@@ -1878,17 +1973,21 @@
     out->stream.write = out_write;
     out->stream.get_render_position = out_get_render_position;
     out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
+    out->stream.get_presentation_position = out_get_presentation_position;
 
-    out->dev = adev;
     out->standby = 1;
     /* out->muted = false; by calloc() */
+    /* out->written = 0; by calloc() */
+
+    pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
+    pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
 
     config->format = out->stream.common.get_format(&out->stream.common);
     config->channel_mask = out->stream.common.get_channels(&out->stream.common);
     config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
 
     *stream_out = &out->stream;
-    ALOGD("%s: exit", __func__);
+    ALOGV("%s: exit", __func__);
     return 0;
 
 error_open:
@@ -1901,10 +2000,21 @@
 static void adev_close_output_stream(struct audio_hw_device *dev,
                                      struct audio_stream_out *stream)
 {
-    ALOGD("%s: enter", __func__);
+    struct stream_out *out = (struct stream_out *)stream;
+    struct audio_device *adev = out->dev;
+
+    ALOGV("%s: enter", __func__);
     out_standby(&stream->common);
+    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
+        destroy_offload_callback_thread(out);
+
+        if (out->compr_config.codec != NULL)
+            free(out->compr_config.codec);
+    }
+    pthread_cond_destroy(&out->cond);
+    pthread_mutex_destroy(&out->lock);
     free(stream);
-    ALOGD("%s: exit", __func__);
+    ALOGV("%s: exit", __func__);
 }
 
 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
@@ -1916,7 +2026,7 @@
     int val;
     int ret;
 
-    ALOGD("%s: enter: %s", __func__, kvpairs);
+    ALOGV("%s: enter: %s", __func__, kvpairs);
 
     parms = str_parms_create_str(kvpairs);
     ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
@@ -1998,7 +2108,7 @@
     }
 
     str_parms_destroy(parms);
-    ALOGD("%s: exit with code(%d)", __func__, ret);
+    ALOGV("%s: exit with code(%d)", __func__, ret);
     return ret;
 }
 
@@ -2013,13 +2123,11 @@
     return 0;
 }
 
-static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
+/* always called with adev lock held */
+static int set_voice_volume_l(struct audio_device *adev, float volume)
 {
-    struct audio_device *adev = (struct audio_device *)dev;
     int vol, err = 0;
 
-    pthread_mutex_lock(&adev->lock);
-    adev->voice_volume = volume;
     if (adev->mode == AUDIO_MODE_IN_CALL) {
         if (volume < 0.0) {
             volume = 0.0;
@@ -2034,23 +2142,23 @@
         // So adjust the volume to get the correct volume index in driver
         vol = 100 - vol;
 
-        if (adev->csd_client) {
-            if (adev->csd_volume == NULL) {
-                ALOGE("%s: dlsym error for csd_client_volume", __func__);
-            } else {
-                err = adev->csd_volume(vol);
-                if (err < 0) {
-                    ALOGE("%s: csd_client error %d", __func__, err);
-                }
-            }
-        } else {
-            ALOGE("%s: No CSD Client present", __func__);
-        }
+        err = platform_set_voice_volume(adev->platform, vol);
     }
-    pthread_mutex_unlock(&adev->lock);
     return err;
 }
 
+static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
+{
+    int ret;
+    struct audio_device *adev = (struct audio_device *)dev;
+    pthread_mutex_lock(&adev->lock);
+    /* cache volume */
+    adev->voice_volume = volume;
+    ret = set_voice_volume_l(adev, adev->voice_volume);
+    pthread_mutex_unlock(&adev->lock);
+    return ret;
+}
+
 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
 {
     return -ENOSYS;
@@ -2091,20 +2199,8 @@
 
     pthread_mutex_lock(&adev->lock);
     adev->mic_mute = state;
-    if (adev->mode == AUDIO_MODE_IN_CALL) {
-        if (adev->csd_client) {
-            if (adev->csd_mic_mute == NULL) {
-                ALOGE("%s: dlsym error for csd_mic_mute", __func__);
-            } else {
-                err = adev->csd_mic_mute(state);
-                if (err < 0) {
-                    ALOGE("%s: csd_client error %d", __func__, err);
-                }
-            }
-        } else {
-            ALOGE("%s: No CSD Client present", __func__);
-        }
-    }
+
+    err = platform_set_mic_mute(adev->platform, state);
     pthread_mutex_unlock(&adev->lock);
     return err;
 }
@@ -2137,7 +2233,7 @@
     int ret, buffer_size, frame_size;
     int channel_count = popcount(config->channel_mask);
 
-    ALOGD("%s: enter", __func__);
+    ALOGV("%s: enter", __func__);
     *stream_in = NULL;
     if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
         return -EINVAL;
@@ -2179,7 +2275,7 @@
     in->config.period_size = buffer_size / frame_size;
 
     *stream_in = &in->stream;
-    ALOGD("%s: exit", __func__);
+    ALOGV("%s: exit", __func__);
     return 0;
 
 err_open:
@@ -2191,7 +2287,7 @@
 static void adev_close_input_stream(struct audio_hw_device *dev,
                                     struct audio_stream_in *stream)
 {
-    ALOGD("%s", __func__);
+    ALOGV("%s", __func__);
 
     in_standby(&stream->common);
     free(stream);
@@ -2208,110 +2304,12 @@
 {
     struct audio_device *adev = (struct audio_device *)device;
     audio_route_free(adev->audio_route);
+    free(adev->snd_dev_ref_cnt);
+    platform_deinit(adev->platform);
     free(device);
     return 0;
 }
 
-static void init_platform_data(struct audio_device *adev)
-{
-    char platform[PROPERTY_VALUE_MAX];
-    char baseband[PROPERTY_VALUE_MAX];
-    char value[PROPERTY_VALUE_MAX];
-
-    adev->dualmic_config = DUALMIC_CONFIG_NONE;
-    adev->fluence_in_spkr_mode = false;
-    adev->fluence_in_voice_call = false;
-    adev->fluence_in_voice_rec = false;
-    adev->mic_type_analog = false;
-
-    property_get("persist.audio.handset.mic.type",value,"");
-    if (!strcmp("analog", value))
-        adev->mic_type_analog = true;
-
-    property_get("persist.audio.dualmic.config",value,"");
-    if (!strcmp("broadside", value)) {
-        adev->dualmic_config = DUALMIC_CONFIG_BROADSIDE;
-        adev->acdb_settings |= DMIC_FLAG;
-    } else if (!strcmp("endfire", value)) {
-        adev->dualmic_config = DUALMIC_CONFIG_ENDFIRE;
-        adev->acdb_settings |= DMIC_FLAG;
-    }
-
-    if (adev->dualmic_config != DUALMIC_CONFIG_NONE) {
-        property_get("persist.audio.fluence.voicecall",value,"");
-        if (!strcmp("true", value)) {
-            adev->fluence_in_voice_call = true;
-        }
-
-        property_get("persist.audio.fluence.voicerec",value,"");
-        if (!strcmp("true", value)) {
-            adev->fluence_in_voice_rec = true;
-        }
-
-        property_get("persist.audio.fluence.speaker",value,"");
-        if (!strcmp("true", value)) {
-            adev->fluence_in_spkr_mode = true;
-        }
-    }
-
-    adev->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
-    if (adev->acdb_handle == NULL) {
-        ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
-    } else {
-        ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
-        adev->acdb_deallocate = (acdb_deallocate_t)dlsym(adev->acdb_handle,
-                                                    "acdb_loader_deallocate_ACDB");
-        adev->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(adev->acdb_handle,
-                                                    "acdb_loader_send_audio_cal");
-        adev->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(adev->acdb_handle,
-                                                    "acdb_loader_send_voice_cal");
-        adev->acdb_init = (acdb_init_t)dlsym(adev->acdb_handle,
-                                                    "acdb_loader_init_ACDB");
-        if (adev->acdb_init == NULL)
-            ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
-        else
-            adev->acdb_init();
-    }
-
-    /* If platform is Fusion3, load CSD Client specific symbols
-     * Voice call is handled by MDM and apps processor talks to
-     * MDM through CSD Client
-     */
-    property_get("ro.board.platform", platform, "");
-    property_get("ro.baseband", baseband, "");
-    if (!strcmp("msm8960", platform) && !strcmp("mdm", baseband)) {
-        adev->csd_client = dlopen(LIB_CSD_CLIENT, RTLD_NOW);
-        if (adev->csd_client == NULL)
-            ALOGE("%s: DLOPEN failed for %s", __func__, LIB_CSD_CLIENT);
-    }
-
-    if (adev->csd_client) {
-        ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT);
-        adev->csd_client_deinit = (csd_client_deinit_t)dlsym(adev->csd_client,
-                                                    "csd_client_deinit");
-        adev->csd_disable_device = (csd_disable_device_t)dlsym(adev->csd_client,
-                                                    "csd_client_disable_device");
-        adev->csd_enable_device = (csd_enable_device_t)dlsym(adev->csd_client,
-                                                    "csd_client_enable_device");
-        adev->csd_start_voice = (csd_start_voice_t)dlsym(adev->csd_client,
-                                                    "csd_client_start_voice");
-        adev->csd_stop_voice = (csd_stop_voice_t)dlsym(adev->csd_client,
-                                                    "csd_client_stop_voice");
-        adev->csd_volume = (csd_volume_t)dlsym(adev->csd_client,
-                                                    "csd_client_volume");
-        adev->csd_mic_mute = (csd_mic_mute_t)dlsym(adev->csd_client,
-                                                    "csd_client_mic_mute");
-        adev->csd_client_init = (csd_client_init_t)dlsym(adev->csd_client,
-                                                    "csd_client_init");
-
-        if (adev->csd_client_init == NULL) {
-            ALOGE("%s: dlsym error %s for csd_client_init", __func__, dlerror());
-        } else {
-            adev->csd_client_init();
-        }
-    }
-}
-
 static int adev_open(const hw_module_t *module, const char *name,
                      hw_device_t **device)
 {
@@ -2323,20 +2321,6 @@
 
     adev = calloc(1, sizeof(struct audio_device));
 
-    adev->mixer = mixer_open(MIXER_CARD);
-    if (!adev->mixer) {
-        ALOGE("Unable to open the mixer, aborting.");
-        return -ENOSYS;
-    }
-
-    adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH);
-    if (!adev->audio_route) {
-        free(adev);
-        ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
-        *device = NULL;
-        return -EINVAL;
-    }
-
     adev->device.common.tag = HARDWARE_DEVICE_TAG;
     adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
     adev->device.common.module = (struct hw_module_t *)module;
@@ -2373,19 +2357,39 @@
     adev->bluetooth_nrec = true;
     adev->in_call = false;
     adev->acdb_settings = TTY_MODE_OFF;
-    adev->speaker_lr_swap = false;
-    for (i = 0; i < SND_DEVICE_MAX; i++) {
-        adev->snd_dev_ref_cnt[i] = 0;
-    }
+    /* adev->cur_hdmi_channels = 0;  by calloc() */
+    adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
     list_init(&adev->usecase_list);
     pthread_mutex_unlock(&adev->lock);
 
     /* Loads platform specific libraries dynamically */
-    init_platform_data(adev);
+    adev->platform = platform_init(adev);
+    if (!adev->platform) {
+        free(adev->snd_dev_ref_cnt);
+        free(adev);
+        ALOGE("%s: Failed to init platform data, aborting.", __func__);
+        *device = NULL;
+        return -EINVAL;
+    }
+
+    if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
+        adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
+        if (adev->visualizer_lib == NULL) {
+            ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
+        } else {
+            ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
+            adev->visualizer_start_output =
+                        (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
+                                                        "visualizer_hal_start_output");
+            adev->visualizer_stop_output =
+                        (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
+                                                        "visualizer_hal_stop_output");
+        }
+    }
 
     *device = &adev->device.common;
 
-    ALOGD("%s: exit", __func__);
+    ALOGV("%s: exit", __func__);
     return 0;
 }
 
diff --git a/hal/audio_hw.h b/hal/audio_hw.h
index 08426de..0da4324 100644
--- a/hal/audio_hw.h
+++ b/hal/audio_hw.h
@@ -14,93 +14,34 @@
  * limitations under the License.
  */
 
+#ifndef QCOM_AUDIO_HW_H
+#define QCOM_AUDIO_HW_H
+
+#include <cutils/list.h>
 #include <hardware/audio.h>
-#include <hardware/audio_effect.h>
-#include <audio_effects/effect_aec.h>
-#include <audio_effects/effect_ns.h>
 
 #include <tinyalsa/asoundlib.h>
+#include <tinycompress/tinycompress.h>
 
 #include <audio_route/audio_route.h>
 
+#define VISUALIZER_LIBRARY_PATH "/system/lib/soundfx/libqcomvisualizer.so"
+
+/* Flags used to initialize acdb_settings variable that goes to ACDB library */
+#define DMIC_FLAG       0x00000002
+#define TTY_MODE_OFF    0x00000010
+#define TTY_MODE_FULL   0x00000020
+#define TTY_MODE_VCO    0x00000040
+#define TTY_MODE_HCO    0x00000080
+#define TTY_MODE_CLEAR  0xFFFFFF0F
+
 #define ACDB_DEV_TYPE_OUT 1
 #define ACDB_DEV_TYPE_IN 2
 
-#define DUALMIC_CONFIG_NONE 0      /* Target does not contain 2 mics */
-#define DUALMIC_CONFIG_ENDFIRE 1
-#define DUALMIC_CONFIG_BROADSIDE 2
+#define MAX_SUPPORTED_CHANNEL_MASKS 2
+#define DEFAULT_HDMI_OUT_CHANNELS   2
 
-/*
- * Below are the devices for which is back end is same, SLIMBUS_0_RX.
- * All these devices are handled by the internal HW codec. We can
- * enable any one of these devices at any time
- */
-#define AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND \
-    (AUDIO_DEVICE_OUT_EARPIECE | AUDIO_DEVICE_OUT_SPEAKER | \
-     AUDIO_DEVICE_OUT_WIRED_HEADSET | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)
-
-/* Sound devices specific to the platform
- * The DEVICE_OUT_* and DEVICE_IN_* should be mapped to these sound
- * devices to enable corresponding mixer paths
- */
-typedef enum {
-    SND_DEVICE_NONE = 0,
-
-    /* Playback devices */
-    SND_DEVICE_MIN,
-    SND_DEVICE_OUT_BEGIN = SND_DEVICE_MIN,
-    SND_DEVICE_OUT_HANDSET = SND_DEVICE_OUT_BEGIN,
-    SND_DEVICE_OUT_SPEAKER,
-    SND_DEVICE_OUT_SPEAKER_REVERSE,
-    SND_DEVICE_OUT_HEADPHONES,
-    SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES,
-    SND_DEVICE_OUT_VOICE_SPEAKER,
-    SND_DEVICE_OUT_VOICE_HEADPHONES,
-    SND_DEVICE_OUT_HDMI,
-    SND_DEVICE_OUT_SPEAKER_AND_HDMI,
-    SND_DEVICE_OUT_BT_SCO,
-    SND_DEVICE_OUT_VOICE_HANDSET_TMUS,
-    SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES,
-    SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES,
-    SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET,
-    SND_DEVICE_OUT_END,
-
-    /*
-     * Note: IN_BEGIN should be same as OUT_END because total number of devices
-     * SND_DEVICES_MAX should not exceed MAX_RX + MAX_TX devices.
-     */
-    /* Capture devices */
-    SND_DEVICE_IN_BEGIN = SND_DEVICE_OUT_END,
-    SND_DEVICE_IN_HANDSET_MIC  = SND_DEVICE_IN_BEGIN,
-    SND_DEVICE_IN_SPEAKER_MIC,
-    SND_DEVICE_IN_HEADSET_MIC,
-    SND_DEVICE_IN_HANDSET_MIC_AEC,
-    SND_DEVICE_IN_SPEAKER_MIC_AEC,
-    SND_DEVICE_IN_HEADSET_MIC_AEC,
-    SND_DEVICE_IN_VOICE_SPEAKER_MIC,
-    SND_DEVICE_IN_VOICE_HEADSET_MIC,
-    SND_DEVICE_IN_HDMI_MIC,
-    SND_DEVICE_IN_BT_SCO_MIC,
-    SND_DEVICE_IN_CAMCORDER_MIC,
-    SND_DEVICE_IN_VOICE_DMIC_EF,
-    SND_DEVICE_IN_VOICE_DMIC_BS,
-    SND_DEVICE_IN_VOICE_DMIC_EF_TMUS,
-    SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF,
-    SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS,
-    SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC,
-    SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC,
-    SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC,
-    SND_DEVICE_IN_VOICE_REC_MIC,
-    SND_DEVICE_IN_VOICE_REC_DMIC_EF,
-    SND_DEVICE_IN_VOICE_REC_DMIC_BS,
-    SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE,
-    SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE,
-    SND_DEVICE_IN_END,
-
-    SND_DEVICE_MAX = SND_DEVICE_IN_END,
-
-} snd_device_t;
-
+typedef int snd_device_t;
 
 /* These are the supported use cases by the hardware.
  * Each usecase is mapped to a specific PCM device.
@@ -112,22 +53,18 @@
     USECASE_AUDIO_PLAYBACK_DEEP_BUFFER = 0,
     USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
     USECASE_AUDIO_PLAYBACK_MULTI_CH,
+    USECASE_AUDIO_PLAYBACK_OFFLOAD,
 
     /* Capture usecases */
     USECASE_AUDIO_RECORD,
     USECASE_AUDIO_RECORD_LOW_LATENCY,
 
     USECASE_VOICE_CALL,
-
     AUDIO_USECASE_MAX
 } audio_usecase_t;
 
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
 
-#define SOUND_CARD 0
-
-#define DEFAULT_OUTPUT_SAMPLING_RATE 48000
-
 /*
  * tinyAlsa library interprets period size as number of frames
  * one frame = channel_count * sizeof (pcm sample)
@@ -136,49 +73,60 @@
  * We should take care of returning proper size when AudioFlinger queries for
  * the buffer size of an input/output stream
  */
-#ifdef MSM8974
-#define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 1024
-#else
-#define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 960
-#endif
-#define DEEP_BUFFER_OUTPUT_PERIOD_COUNT 8
 
-#ifdef MSM8974
-#define LOW_LATENCY_OUTPUT_PERIOD_SIZE 256
-#else
-#define LOW_LATENCY_OUTPUT_PERIOD_SIZE 240
-#endif
-#define LOW_LATENCY_OUTPUT_PERIOD_COUNT 2
+enum {
+    OFFLOAD_CMD_EXIT,               /* exit compress offload thread loop*/
+    OFFLOAD_CMD_DRAIN,              /* send a full drain request to DSP */
+    OFFLOAD_CMD_PARTIAL_DRAIN,      /* send a partial drain request to DSP */
+    OFFLOAD_CMD_WAIT_FOR_BUFFER,    /* wait for buffer released by DSP */
+};
 
-#define HDMI_MULTI_PERIOD_SIZE  336
-#define HDMI_MULTI_PERIOD_COUNT 8
-#define HDMI_MULTI_DEFAULT_CHANNEL_COUNT 6
-#define HDMI_MULTI_PERIOD_BYTES (HDMI_MULTI_PERIOD_SIZE * HDMI_MULTI_DEFAULT_CHANNEL_COUNT * 2)
+enum {
+    OFFLOAD_STATE_IDLE,
+    OFFLOAD_STATE_PLAYING,
+    OFFLOAD_STATE_PAUSED,
+};
 
-#ifdef MSM8974
-#define AUDIO_CAPTURE_PERIOD_SIZE 512
-#define AUDIO_CAPTURE_PERIOD_COUNT 16
-#else
-#define AUDIO_CAPTURE_PERIOD_SIZE 320
-#define AUDIO_CAPTURE_PERIOD_COUNT 2
-#endif
-
-#define MAX_SUPPORTED_CHANNEL_MASKS 2
+struct offload_cmd {
+    struct listnode node;
+    int cmd;
+    int data[];
+};
 
 struct stream_out {
     struct audio_stream_out stream;
     pthread_mutex_t lock; /* see note below on mutex acquisition order */
+    pthread_cond_t  cond;
     struct pcm_config config;
+    struct compr_config compr_config;
     struct pcm *pcm;
+    struct compress *compr;
     int standby;
     int pcm_device_id;
+    unsigned int sample_rate;
     audio_channel_mask_t channel_mask;
+    audio_format_t format;
     audio_devices_t devices;
     audio_output_flags_t flags;
     audio_usecase_t usecase;
     /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
     audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
     bool muted;
+    uint64_t written; /* total frames written, not cleared when entering standby */
+    audio_io_handle_t handle;
+
+    int non_blocking;
+    int playback_started;
+    int offload_state;
+    pthread_cond_t offload_cond;
+    pthread_t offload_thread;
+    struct listnode offload_cmd_list;
+    bool offload_thread_blocked;
+
+    stream_callback_t offload_callback;
+    void *offload_cookie;
+    struct compr_gapless_mdata gapless_mdata;
+    int send_new_metadata;
 
     struct audio_device *dev;
 };
@@ -220,20 +168,6 @@
     union stream_ptr stream;
 };
 
-typedef void (*acdb_deallocate_t)();
-typedef int  (*acdb_init_t)();
-typedef void (*acdb_send_audio_cal_t)(int, int);
-typedef void (*acdb_send_voice_cal_t)(int, int);
-
-typedef int (*csd_client_init_t)();
-typedef int (*csd_client_deinit_t)();
-typedef int (*csd_disable_device_t)();
-typedef int (*csd_enable_device_t)(int, int, uint32_t);
-typedef int (*csd_volume_t)(int);
-typedef int (*csd_mic_mute_t)(int);
-typedef int (*csd_start_voice_t)();
-typedef int (*csd_stop_voice_t)();
-
 struct audio_device {
     struct audio_hw_device device;
     pthread_mutex_t lock; /* see note below on mutex acquisition order */
@@ -250,35 +184,18 @@
     bool screen_off;
     struct pcm *voice_call_rx;
     struct pcm *voice_call_tx;
-    int snd_dev_ref_cnt[SND_DEVICE_MAX];
+    int *snd_dev_ref_cnt;
     struct listnode usecase_list;
     struct audio_route *audio_route;
     int acdb_settings;
     bool speaker_lr_swap;
+    unsigned int cur_hdmi_channels;
 
-    bool mic_type_analog;
-    bool fluence_in_spkr_mode;
-    bool fluence_in_voice_call;
-    bool fluence_in_voice_rec;
-    int  dualmic_config;
+    void *platform;
 
-    /* Audio calibration related functions */
-    void *acdb_handle;
-    acdb_init_t acdb_init;
-    acdb_deallocate_t acdb_deallocate;
-    acdb_send_audio_cal_t acdb_send_audio_cal;
-    acdb_send_voice_cal_t acdb_send_voice_cal;
-
-    /* CSD Client related functions for voice call */
-    void *csd_client;
-    csd_client_init_t csd_client_init;
-    csd_client_deinit_t csd_client_deinit;
-    csd_disable_device_t csd_disable_device;
-    csd_enable_device_t csd_enable_device;
-    csd_volume_t csd_volume;
-    csd_mic_mute_t csd_mic_mute;
-    csd_start_voice_t csd_start_voice;
-    csd_stop_voice_t csd_stop_voice;
+    void *visualizer_lib;
+    int (*visualizer_start_output)(audio_io_handle_t);
+    int (*visualizer_stop_output)(audio_io_handle_t);
 };
 
 /*
@@ -286,51 +203,4 @@
  * stream_in or stream_out mutex first, followed by the audio_device mutex.
  */
 
-struct pcm_config pcm_config_deep_buffer = {
-    .channels = 2,
-    .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
-    .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
-    .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
-    .format = PCM_FORMAT_S16_LE,
-    .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
-    .stop_threshold = INT_MAX,
-    .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
-};
-
-struct pcm_config pcm_config_low_latency = {
-    .channels = 2,
-    .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
-    .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
-    .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
-    .format = PCM_FORMAT_S16_LE,
-    .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
-    .stop_threshold = INT_MAX,
-    .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
-};
-
-struct pcm_config pcm_config_hdmi_multi = {
-    .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
-    .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
-    .period_size = HDMI_MULTI_PERIOD_SIZE,
-    .period_count = HDMI_MULTI_PERIOD_COUNT,
-    .format = PCM_FORMAT_S16_LE,
-    .start_threshold = 0,
-    .stop_threshold = INT_MAX,
-    .avail_min = 0,
-};
-
-struct pcm_config pcm_config_audio_capture = {
-    .channels = 2,
-    .period_size = AUDIO_CAPTURE_PERIOD_SIZE,
-    .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
-    .format = PCM_FORMAT_S16_LE,
-};
-
-struct pcm_config pcm_config_voice_call = {
-    .channels = 1,
-    .rate = 8000,
-    .period_size = 160,
-    .period_count = 2,
-    .format = PCM_FORMAT_S16_LE,
-};
-
+#endif // QCOM_AUDIO_HW_H
diff --git a/hal/edid.c b/hal/edid.c
deleted file mode 100644
index 7894ab8..0000000
--- a/hal/edid.c
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#define LOG_TAG "audio_hw_primary"
-
-#include <stdlib.h>
-#include <stdio.h>
-
-#include <cutils/log.h>
-
-/*
- * This is the sysfs path for the HDMI audio data block
- */
-#define AUDIO_DATA_BLOCK_PATH "/sys/class/graphics/fb1/audio_data_block"
-
-/*
- * This file will have a maximum of 38 bytes:
- *
- * 4 bytes: number of audio blocks
- * 4 bytes: total length of Short Audio Descriptor (SAD) blocks
- * Maximum 10 * 3 bytes: SAD blocks
- */
-#define MAX_SAD_BLOCKS		10
-#define SAD_BLOCK_SIZE		3
-
-/* EDID format ID for LPCM audio */
-#define EDID_FORMAT_LPCM	1
-
-struct audio_block_header
-{
-    int reserved;
-    int length;
-};
-
-int edid_get_max_channels(void)
-{
-    FILE *file;
-    struct audio_block_header header;
-    char block[MAX_SAD_BLOCKS * SAD_BLOCK_SIZE];
-    char *sad = block;
-    int num_audio_blocks;
-    int channel_count;
-    int max_channels = 0;
-    int i;
-
-    file = fopen(AUDIO_DATA_BLOCK_PATH, "rb");
-    if (file == NULL) {
-        ALOGE("Unable to open '%s'", AUDIO_DATA_BLOCK_PATH);
-        return 0;
-    }
-
-    /* Read audio block header */
-    fread(&header, 1, sizeof(header), file);
-
-    /* Read SAD blocks, clamping the maximum size for safety */
-    if (header.length > (int)sizeof(block))
-        header.length = (int)sizeof(block);
-    fread(&block, header.length, 1, file);
-
-    fclose(file);
-
-    /* Calculate the number of SAD blocks */
-    num_audio_blocks = header.length / SAD_BLOCK_SIZE;
-
-    for (i = 0; i < num_audio_blocks; i++) {
-        /* Only consider LPCM blocks */
-        if ((sad[0] >> 3) != EDID_FORMAT_LPCM)
-            continue;
-
-        channel_count = (sad[0] & 0x7) + 1;
-        if (channel_count > max_channels)
-            max_channels = channel_count;
-
-        /* Advance to next block */
-        sad += 3;
-    }
-
-    return max_channels;
-}
-
diff --git a/hal/msm8960/platform.c b/hal/msm8960/platform.c
new file mode 100644
index 0000000..b200e27
--- /dev/null
+++ b/hal/msm8960/platform.c
@@ -0,0 +1,898 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "msm8960_platform"
+/*#define LOG_NDEBUG 0*/
+#define LOG_NDDEBUG 0
+
+#include <stdlib.h>
+#include <dlfcn.h>
+#include <cutils/log.h>
+#include <cutils/properties.h>
+#include <audio_hw.h>
+#include <platform_api.h>
+#include "platform.h"
+
+#define LIB_ACDB_LOADER "libacdbloader.so"
+#define LIB_CSD_CLIENT "libcsd-client.so"
+
+#define DUALMIC_CONFIG_NONE 0      /* Target does not contain 2 mics */
+#define DUALMIC_CONFIG_ENDFIRE 1
+#define DUALMIC_CONFIG_BROADSIDE 2
+
+/*
+ * This is the sysfs path for the HDMI audio data block
+ */
+#define AUDIO_DATA_BLOCK_PATH "/sys/class/graphics/fb1/audio_data_block"
+#define MIXER_XML_PATH "/system/etc/mixer_paths.xml"
+
+/*
+ * This file will have a maximum of 38 bytes:
+ *
+ * 4 bytes: number of audio blocks
+ * 4 bytes: total length of Short Audio Descriptor (SAD) blocks
+ * Maximum 10 * 3 bytes: SAD blocks
+ */
+#define MAX_SAD_BLOCKS      10
+#define SAD_BLOCK_SIZE      3
+
+/* EDID format ID for LPCM audio */
+#define EDID_FORMAT_LPCM    1
+
+struct audio_block_header
+{
+    int reserved;
+    int length;
+};
+
+
+typedef void (*acdb_deallocate_t)();
+typedef int  (*acdb_init_t)();
+typedef void (*acdb_send_audio_cal_t)(int, int);
+typedef void (*acdb_send_voice_cal_t)(int, int);
+
+typedef int (*csd_client_init_t)();
+typedef int (*csd_client_deinit_t)();
+typedef int (*csd_disable_device_t)();
+typedef int (*csd_enable_device_t)(int, int, uint32_t);
+typedef int (*csd_volume_t)(int);
+typedef int (*csd_mic_mute_t)(int);
+typedef int (*csd_start_voice_t)();
+typedef int (*csd_stop_voice_t)();
+
+
+/* Audio calibration related functions */
+struct platform_data {
+    struct audio_device *adev;
+    bool fluence_in_spkr_mode;
+    bool fluence_in_voice_call;
+    bool fluence_in_voice_rec;
+    int  dualmic_config;
+
+    void *acdb_handle;
+    acdb_init_t acdb_init;
+    acdb_deallocate_t acdb_deallocate;
+    acdb_send_audio_cal_t acdb_send_audio_cal;
+    acdb_send_voice_cal_t acdb_send_voice_cal;
+
+    /* CSD Client related functions for voice call */
+    void *csd_client;
+    csd_client_init_t csd_client_init;
+    csd_client_deinit_t csd_client_deinit;
+    csd_disable_device_t csd_disable_device;
+    csd_enable_device_t csd_enable_device;
+    csd_volume_t csd_volume;
+    csd_mic_mute_t csd_mic_mute;
+    csd_start_voice_t csd_start_voice;
+    csd_stop_voice_t csd_stop_voice;
+};
+
+static const int pcm_device_table[AUDIO_USECASE_MAX][2] = {
+    [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
+    [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {14, 14},
+    [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
+    [USECASE_AUDIO_RECORD] = {0, 0},
+    [USECASE_AUDIO_RECORD_LOW_LATENCY] = {14, 14},
+    [USECASE_VOICE_CALL] = {12, 12},
+};
+
+/* Array to store sound devices */
+static const char * const device_table[SND_DEVICE_MAX] = {
+    [SND_DEVICE_NONE] = "none",
+    /* Playback sound devices */
+    [SND_DEVICE_OUT_HANDSET] = "handset",
+    [SND_DEVICE_OUT_SPEAKER] = "speaker",
+    [SND_DEVICE_OUT_SPEAKER_REVERSE] = "speaker-reverse",
+    [SND_DEVICE_OUT_HEADPHONES] = "headphones",
+    [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones",
+    [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker",
+    [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones",
+    [SND_DEVICE_OUT_HDMI] = "hdmi",
+    [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi",
+    [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset",
+    [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = "voice-handset-tmus",
+    [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones",
+    [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones",
+    [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset",
+
+    /* Capture sound devices */
+    [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
+    [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic",
+    [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic",
+    [SND_DEVICE_IN_HANDSET_MIC_AEC] = "handset-mic",
+    [SND_DEVICE_IN_SPEAKER_MIC_AEC] = "voice-speaker-mic",
+    [SND_DEVICE_IN_HEADSET_MIC_AEC] = "headset-mic",
+    [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic",
+    [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic",
+    [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic",
+    [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic",
+    [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic",
+    [SND_DEVICE_IN_VOICE_DMIC_EF] = "voice-dmic-ef",
+    [SND_DEVICE_IN_VOICE_DMIC_BS] = "voice-dmic-bs",
+    [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = "voice-dmic-ef-tmus",
+    [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = "voice-speaker-dmic-ef",
+    [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = "voice-speaker-dmic-bs",
+    [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
+    [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
+    [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
+    [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
+    [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = "voice-rec-dmic-ef",
+    [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = "voice-rec-dmic-bs",
+    [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = "voice-rec-dmic-ef-fluence",
+    [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = "voice-rec-dmic-bs-fluence",
+};
+
+/* ACDB IDs (audio DSP path configuration IDs) for each sound device */
+static const int acdb_device_table[SND_DEVICE_MAX] = {
+    [SND_DEVICE_NONE] = -1,
+    [SND_DEVICE_OUT_HANDSET] = 7,
+    [SND_DEVICE_OUT_SPEAKER] = 14,
+    [SND_DEVICE_OUT_SPEAKER_REVERSE] = 14,
+    [SND_DEVICE_OUT_HEADPHONES] = 10,
+    [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = 10,
+    [SND_DEVICE_OUT_VOICE_SPEAKER] = 14,
+    [SND_DEVICE_OUT_VOICE_HEADPHONES] = 10,
+    [SND_DEVICE_OUT_HDMI] = 18,
+    [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 14,
+    [SND_DEVICE_OUT_BT_SCO] = 22,
+    [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = 81,
+    [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = 17,
+    [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = 17,
+    [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = 37,
+
+    [SND_DEVICE_IN_HANDSET_MIC] = 4,
+    [SND_DEVICE_IN_SPEAKER_MIC] = 4,
+    [SND_DEVICE_IN_HEADSET_MIC] = 8,
+    [SND_DEVICE_IN_HANDSET_MIC_AEC] = 40,
+    [SND_DEVICE_IN_SPEAKER_MIC_AEC] = 42,
+    [SND_DEVICE_IN_HEADSET_MIC_AEC] = 47,
+    [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = 11,
+    [SND_DEVICE_IN_VOICE_HEADSET_MIC] = 8,
+    [SND_DEVICE_IN_HDMI_MIC] = 4,
+    [SND_DEVICE_IN_BT_SCO_MIC] = 21,
+    [SND_DEVICE_IN_CAMCORDER_MIC] = 61,
+    [SND_DEVICE_IN_VOICE_DMIC_EF] = 6,
+    [SND_DEVICE_IN_VOICE_DMIC_BS] = 5,
+    [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = 91,
+    [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = 13,
+    [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = 12,
+    [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = 16,
+    [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = 36,
+    [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = 16,
+    [SND_DEVICE_IN_VOICE_REC_MIC] = 62,
+    /* TODO: Update with proper acdb ids */
+    [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = 62,
+    [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = 62,
+    [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = 6,
+    [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = 5,
+};
+
+#define DEEP_BUFFER_PLATFORM_DELAY (29*1000LL)
+#define LOW_LATENCY_PLATFORM_DELAY (13*1000LL)
+
+static pthread_once_t check_op_once_ctl = PTHREAD_ONCE_INIT;
+static bool is_tmus = false;
+
+static void check_operator()
+{
+    char value[PROPERTY_VALUE_MAX];
+    int mccmnc;
+    property_get("gsm.sim.operator.numeric",value,"0");
+    mccmnc = atoi(value);
+    ALOGD("%s: tmus mccmnc %d", __func__, mccmnc);
+    switch(mccmnc) {
+    /* TMUS MCC(310), MNC(490, 260, 026) */
+    case 310490:
+    case 310260:
+    case 310026:
+        is_tmus = true;
+        break;
+    }
+}
+
+bool is_operator_tmus()
+{
+    pthread_once(&check_op_once_ctl, check_operator);
+    return is_tmus;
+}
+
+static int set_echo_reference(struct mixer *mixer, const char* ec_ref)
+{
+    struct mixer_ctl *ctl;
+    const char *mixer_ctl_name = "EC_REF_RX";
+
+    ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
+    if (!ctl) {
+        ALOGE("%s: Could not get ctl for mixer cmd - %s",
+              __func__, mixer_ctl_name);
+        return -EINVAL;
+    }
+    ALOGV("Setting EC Reference: %s", ec_ref);
+    mixer_ctl_set_enum_by_string(ctl, ec_ref);
+    return 0;
+}
+
+void *platform_init(struct audio_device *adev)
+{
+    char platform[PROPERTY_VALUE_MAX];
+    char baseband[PROPERTY_VALUE_MAX];
+    char value[PROPERTY_VALUE_MAX];
+    struct platform_data *my_data;
+
+    adev->mixer = mixer_open(MIXER_CARD);
+
+    if (!adev->mixer) {
+        ALOGE("Unable to open the mixer, aborting.");
+        return NULL;
+    }
+
+    adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH);
+    if (!adev->audio_route) {
+        ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
+        return NULL;
+    }
+
+    my_data = calloc(1, sizeof(struct platform_data));
+
+    my_data->adev = adev;
+    my_data->dualmic_config = DUALMIC_CONFIG_NONE;
+    my_data->fluence_in_spkr_mode = false;
+    my_data->fluence_in_voice_call = false;
+    my_data->fluence_in_voice_rec = false;
+
+    property_get("persist.audio.dualmic.config",value,"");
+    if (!strcmp("broadside", value)) {
+        my_data->dualmic_config = DUALMIC_CONFIG_BROADSIDE;
+        adev->acdb_settings |= DMIC_FLAG;
+    } else if (!strcmp("endfire", value)) {
+        my_data->dualmic_config = DUALMIC_CONFIG_ENDFIRE;
+        adev->acdb_settings |= DMIC_FLAG;
+    }
+
+    if (my_data->dualmic_config != DUALMIC_CONFIG_NONE) {
+        property_get("persist.audio.fluence.voicecall",value,"");
+        if (!strcmp("true", value)) {
+            my_data->fluence_in_voice_call = true;
+        }
+
+        property_get("persist.audio.fluence.voicerec",value,"");
+        if (!strcmp("true", value)) {
+            my_data->fluence_in_voice_rec = true;
+        }
+
+        property_get("persist.audio.fluence.speaker",value,"");
+        if (!strcmp("true", value)) {
+            my_data->fluence_in_spkr_mode = true;
+        }
+    }
+
+    my_data->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
+    if (my_data->acdb_handle == NULL) {
+        ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
+    } else {
+        ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
+        my_data->acdb_deallocate = (acdb_deallocate_t)dlsym(my_data->acdb_handle,
+                                                    "acdb_loader_deallocate_ACDB");
+        my_data->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(my_data->acdb_handle,
+                                                    "acdb_loader_send_audio_cal");
+        if (!my_data->acdb_send_audio_cal)
+            ALOGW("%s: Could not find the symbol acdb_send_audio_cal from %s",
+                  __func__, LIB_ACDB_LOADER);
+        my_data->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(my_data->acdb_handle,
+                                                    "acdb_loader_send_voice_cal");
+        my_data->acdb_init = (acdb_init_t)dlsym(my_data->acdb_handle,
+                                                    "acdb_loader_init_ACDB");
+        if (my_data->acdb_init == NULL)
+            ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
+        else
+            my_data->acdb_init();
+    }
+
+    /* If platform is Fusion3, load CSD Client specific symbols
+     * Voice call is handled by MDM and apps processor talks to
+     * MDM through CSD Client
+     */
+    property_get("ro.board.platform", platform, "");
+    property_get("ro.baseband", baseband, "");
+    if (!strcmp("msm8960", platform) && !strcmp("mdm", baseband)) {
+        my_data->csd_client = dlopen(LIB_CSD_CLIENT, RTLD_NOW);
+        if (my_data->csd_client == NULL)
+            ALOGE("%s: DLOPEN failed for %s", __func__, LIB_CSD_CLIENT);
+    }
+
+    if (my_data->csd_client) {
+        ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT);
+        my_data->csd_client_deinit = (csd_client_deinit_t)dlsym(my_data->csd_client,
+                                                    "csd_client_deinit");
+        my_data->csd_disable_device = (csd_disable_device_t)dlsym(my_data->csd_client,
+                                                    "csd_client_disable_device");
+        my_data->csd_enable_device = (csd_enable_device_t)dlsym(my_data->csd_client,
+                                                    "csd_client_enable_device");
+        my_data->csd_start_voice = (csd_start_voice_t)dlsym(my_data->csd_client,
+                                                    "csd_client_start_voice");
+        my_data->csd_stop_voice = (csd_stop_voice_t)dlsym(my_data->csd_client,
+                                                    "csd_client_stop_voice");
+        my_data->csd_volume = (csd_volume_t)dlsym(my_data->csd_client,
+                                                    "csd_client_volume");
+        my_data->csd_mic_mute = (csd_mic_mute_t)dlsym(my_data->csd_client,
+                                                    "csd_client_mic_mute");
+        my_data->csd_client_init = (csd_client_init_t)dlsym(my_data->csd_client,
+                                                    "csd_client_init");
+
+        if (my_data->csd_client_init == NULL) {
+            ALOGE("%s: dlsym error %s for csd_client_init", __func__, dlerror());
+        } else {
+            my_data->csd_client_init();
+        }
+    }
+
+    return my_data;
+}
+
+void platform_deinit(void *platform)
+{
+    free(platform);
+}
+
+const char *platform_get_snd_device_name(snd_device_t snd_device)
+{
+    if (snd_device >= SND_DEVICE_MIN && snd_device < SND_DEVICE_MAX)
+        return device_table[snd_device];
+    else
+        return "";
+}
+
+void platform_add_backend_name(char *mixer_path, snd_device_t snd_device)
+{
+    if (snd_device == SND_DEVICE_IN_BT_SCO_MIC)
+        strcat(mixer_path, " bt-sco");
+    else if(snd_device == SND_DEVICE_OUT_BT_SCO)
+        strcat(mixer_path, " bt-sco");
+    else if (snd_device == SND_DEVICE_OUT_HDMI)
+        strcat(mixer_path, " hdmi");
+    else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HDMI)
+        strcat(mixer_path, " speaker-and-hdmi");
+}
+
+int platform_get_pcm_device_id(audio_usecase_t usecase, int device_type)
+{
+    int device_id;
+    if (device_type == PCM_PLAYBACK)
+        device_id = pcm_device_table[usecase][0];
+    else
+        device_id = pcm_device_table[usecase][1];
+    return device_id;
+}
+
+int platform_send_audio_calibration(void *platform, snd_device_t snd_device)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    int acdb_dev_id, acdb_dev_type;
+
+    acdb_dev_id = acdb_device_table[snd_device];
+    if (acdb_dev_id < 0) {
+        ALOGE("%s: Could not find acdb id for device(%d)",
+              __func__, snd_device);
+        return -EINVAL;
+    }
+    if (my_data->acdb_send_audio_cal) {
+        ("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
+              __func__, snd_device, acdb_dev_id);
+        if (snd_device >= SND_DEVICE_OUT_BEGIN &&
+                snd_device < SND_DEVICE_OUT_END)
+            acdb_dev_type = ACDB_DEV_TYPE_OUT;
+        else
+            acdb_dev_type = ACDB_DEV_TYPE_IN;
+        my_data->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
+    }
+    return 0;
+}
+
+int platform_switch_voice_call_device_pre(void *platform)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    int ret = 0;
+
+    if (my_data->csd_client != NULL) {
+        /* This must be called before disabling the mixer controls on APQ side */
+        if (my_data->csd_disable_device == NULL) {
+            ALOGE("%s: dlsym error for csd_disable_device", __func__);
+        } else {
+            ret = my_data->csd_disable_device();
+            if (ret < 0) {
+                ALOGE("%s: csd_client_disable_device, failed, error %d",
+                      __func__, ret);
+            }
+        }
+    }
+    return ret;
+}
+
+int platform_switch_voice_call_device_post(void *platform,
+                                           snd_device_t out_snd_device,
+                                           snd_device_t in_snd_device)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    int acdb_rx_id, acdb_tx_id;
+    int ret = 0;
+
+    if (my_data->csd_client) {
+        if (my_data->csd_enable_device == NULL) {
+            ALOGE("%s: dlsym error for csd_enable_device",
+                  __func__);
+        } else {
+            acdb_rx_id = acdb_device_table[out_snd_device];
+            acdb_tx_id = acdb_device_table[in_snd_device];
+
+            if (acdb_rx_id > 0 || acdb_tx_id > 0) {
+                ret = my_data->csd_enable_device(acdb_rx_id, acdb_tx_id,
+                                                    my_data->adev->acdb_settings);
+                if (ret < 0) {
+                    ALOGE("%s: csd_enable_device, failed, error %d",
+                          __func__, ret);
+                }
+            } else {
+                ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
+                      acdb_rx_id, acdb_tx_id);
+            }
+        }
+    }
+
+    return ret;
+}
+
+int platform_start_voice_call(void *platform)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    int ret = 0;
+
+    if (my_data->csd_client) {
+        if (my_data->csd_start_voice == NULL) {
+            ALOGE("dlsym error for csd_client_start_voice");
+            ret = -ENOSYS;
+        } else {
+            ret = my_data->csd_start_voice();
+            if (ret < 0) {
+                ALOGE("%s: csd_start_voice error %d\n", __func__, ret);
+            }
+        }
+    }
+
+    return ret;
+}
+
+int platform_stop_voice_call(void *platform)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    int ret = 0;
+
+    if (my_data->csd_client) {
+        if (my_data->csd_stop_voice == NULL) {
+            ALOGE("dlsym error for csd_stop_voice");
+        } else {
+            ret = my_data->csd_stop_voice();
+            if (ret < 0) {
+                ALOGE("%s: csd_stop_voice error %d\n", __func__, ret);
+            }
+        }
+    }
+
+    return ret;
+}
+
+int platform_set_voice_volume(void *platform, int volume)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    int ret = 0;
+
+    if (my_data->csd_client) {
+        if (my_data->csd_volume == NULL) {
+            ALOGE("%s: dlsym error for csd_volume", __func__);
+        } else {
+            ret = my_data->csd_volume(volume);
+            if (ret < 0) {
+                ALOGE("%s: csd_volume error %d", __func__, ret);
+            }
+        }
+    } else {
+        ALOGE("%s: No CSD Client present", __func__);
+    }
+
+    return ret;
+}
+
+int platform_set_mic_mute(void *platform, bool state)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    int ret = 0;
+
+    if (my_data->adev->mode == AUDIO_MODE_IN_CALL) {
+        if (my_data->csd_client) {
+            if (my_data->csd_mic_mute == NULL) {
+                ALOGE("%s: dlsym error for csd_mic_mute", __func__);
+            } else {
+                ret = my_data->csd_mic_mute(state);
+                if (ret < 0) {
+                    ALOGE("%s: csd_mic_mute error %d", __func__, ret);
+                }
+            }
+        } else {
+            ALOGE("%s: No CSD Client present", __func__);
+        }
+    }
+
+    return ret;
+}
+
+snd_device_t platform_get_output_snd_device(void *platform, audio_devices_t devices)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    struct audio_device *adev = my_data->adev;
+    audio_mode_t mode = adev->mode;
+    snd_device_t snd_device = SND_DEVICE_NONE;
+
+    ALOGV("%s: enter: output devices(%#x)", __func__, devices);
+    if (devices == AUDIO_DEVICE_NONE ||
+        devices & AUDIO_DEVICE_BIT_IN) {
+        ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
+        goto exit;
+    }
+
+    if (mode == AUDIO_MODE_IN_CALL) {
+        if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
+            devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
+            if (adev->tty_mode == TTY_MODE_FULL)
+                snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
+            else if (adev->tty_mode == TTY_MODE_VCO)
+                snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
+            else if (adev->tty_mode == TTY_MODE_HCO)
+                snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
+            else
+                snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
+        } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
+            snd_device = SND_DEVICE_OUT_BT_SCO;
+        } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
+            snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
+        } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
+            if (is_operator_tmus())
+                snd_device = SND_DEVICE_OUT_VOICE_HANDSET_TMUS;
+            else
+                snd_device = SND_DEVICE_OUT_HANDSET;
+        }
+        if (snd_device != SND_DEVICE_NONE) {
+            goto exit;
+        }
+    }
+
+    if (popcount(devices) == 2) {
+        if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
+                        AUDIO_DEVICE_OUT_SPEAKER)) {
+            snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
+        } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
+                               AUDIO_DEVICE_OUT_SPEAKER)) {
+            snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
+        } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
+                               AUDIO_DEVICE_OUT_SPEAKER)) {
+            snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
+        } else {
+            ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
+            goto exit;
+        }
+        if (snd_device != SND_DEVICE_NONE) {
+            goto exit;
+        }
+    }
+
+    if (popcount(devices) != 1) {
+        ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
+        goto exit;
+    }
+
+    if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
+        devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
+        snd_device = SND_DEVICE_OUT_HEADPHONES;
+    } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
+        if (adev->speaker_lr_swap)
+            snd_device = SND_DEVICE_OUT_SPEAKER_REVERSE;
+        else
+            snd_device = SND_DEVICE_OUT_SPEAKER;
+    } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
+        snd_device = SND_DEVICE_OUT_BT_SCO;
+    } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
+        snd_device = SND_DEVICE_OUT_HDMI ;
+    } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
+        snd_device = SND_DEVICE_OUT_HANDSET;
+    } else {
+        ALOGE("%s: Unknown device(s) %#x", __func__, devices);
+    }
+exit:
+    ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
+    return snd_device;
+}
+
+snd_device_t platform_get_input_snd_device(void *platform, audio_devices_t out_device)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    struct audio_device *adev = my_data->adev;
+    audio_source_t  source = (adev->active_input == NULL) ?
+                                AUDIO_SOURCE_DEFAULT : adev->active_input->source;
+
+    audio_mode_t    mode   = adev->mode;
+    audio_devices_t in_device = ((adev->active_input == NULL) ?
+                                    AUDIO_DEVICE_NONE : adev->active_input->device)
+                                & ~AUDIO_DEVICE_BIT_IN;
+    audio_channel_mask_t channel_mask = (adev->active_input == NULL) ?
+                                AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask;
+    snd_device_t snd_device = SND_DEVICE_NONE;
+
+    ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
+          __func__, out_device, in_device);
+    if (mode == AUDIO_MODE_IN_CALL) {
+        if (out_device == AUDIO_DEVICE_NONE) {
+            ALOGE("%s: No output device set for voice call", __func__);
+            goto exit;
+        }
+        if (adev->tty_mode != TTY_MODE_OFF) {
+            if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
+                out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
+                switch (adev->tty_mode) {
+                case TTY_MODE_FULL:
+                    snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
+                    break;
+                case TTY_MODE_VCO:
+                    snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
+                    break;
+                case TTY_MODE_HCO:
+                    snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
+                    break;
+                default:
+                    ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->tty_mode);
+                }
+                goto exit;
+            }
+        }
+        if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
+            out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
+            if (my_data->fluence_in_voice_call == false) {
+                snd_device = SND_DEVICE_IN_HANDSET_MIC;
+            } else {
+                if (my_data->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
+                    if (is_operator_tmus())
+                        snd_device = SND_DEVICE_IN_VOICE_DMIC_EF_TMUS;
+                    else
+                        snd_device = SND_DEVICE_IN_VOICE_DMIC_EF;
+                } else if(my_data->dualmic_config == DUALMIC_CONFIG_BROADSIDE)
+                    snd_device = SND_DEVICE_IN_VOICE_DMIC_BS;
+                else
+                    snd_device = SND_DEVICE_IN_HANDSET_MIC;
+            }
+        } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
+            snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
+        } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
+            snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
+        } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
+            if (my_data->fluence_in_voice_call && my_data->fluence_in_spkr_mode &&
+                    my_data->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
+                snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF;
+            } else if (my_data->fluence_in_voice_call && my_data->fluence_in_spkr_mode &&
+                    my_data->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
+                snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS;
+            } else {
+                snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
+            }
+        }
+    } else if (source == AUDIO_SOURCE_CAMCORDER) {
+        if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
+            in_device & AUDIO_DEVICE_IN_BACK_MIC) {
+            snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
+        }
+    } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
+        if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
+            if (my_data->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
+                if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
+                    snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF;
+                else if (my_data->fluence_in_voice_rec)
+                    snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE;
+            } else if (my_data->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
+                if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
+                    snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS;
+                else if (my_data->fluence_in_voice_rec)
+                    snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE;
+            }
+
+            if (snd_device == SND_DEVICE_NONE) {
+                snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
+            }
+        }
+    } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
+        if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
+            in_device = AUDIO_DEVICE_IN_BACK_MIC;
+        if (adev->active_input) {
+            if (adev->active_input->enable_aec) {
+                if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
+                    snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
+                } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
+                    snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
+                } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
+                    snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC;
+                }
+                set_echo_reference(adev->mixer, "SLIM_RX");
+            } else
+                set_echo_reference(adev->mixer, "NONE");
+        }
+    } else if (source == AUDIO_SOURCE_DEFAULT) {
+        goto exit;
+    }
+
+
+    if (snd_device != SND_DEVICE_NONE) {
+        goto exit;
+    }
+
+    if (in_device != AUDIO_DEVICE_NONE &&
+            !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
+            !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
+        if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
+            snd_device = SND_DEVICE_IN_HANDSET_MIC;
+        } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
+            snd_device = SND_DEVICE_IN_SPEAKER_MIC;
+        } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
+            snd_device = SND_DEVICE_IN_HEADSET_MIC;
+        } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
+            snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
+        } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
+            snd_device = SND_DEVICE_IN_HDMI_MIC;
+        } else {
+            ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
+            ALOGW("%s: Using default handset-mic", __func__);
+            snd_device = SND_DEVICE_IN_HANDSET_MIC;
+        }
+    } else {
+        if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
+            snd_device = SND_DEVICE_IN_HANDSET_MIC;
+        } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
+            snd_device = SND_DEVICE_IN_HEADSET_MIC;
+        } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
+            snd_device = SND_DEVICE_IN_SPEAKER_MIC;
+        } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
+            snd_device = SND_DEVICE_IN_HANDSET_MIC;
+        } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
+            snd_device = SND_DEVICE_IN_BT_SCO_MIC;
+        } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
+            snd_device = SND_DEVICE_IN_HDMI_MIC;
+        } else {
+            ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
+            ALOGW("%s: Using default handset-mic", __func__);
+            snd_device = SND_DEVICE_IN_HANDSET_MIC;
+        }
+    }
+exit:
+    ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
+    return snd_device;
+}
+
+int platform_set_hdmi_channels(void *platform,  int channel_count)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    struct audio_device *adev = my_data->adev;
+    struct mixer_ctl *ctl;
+    const char *channel_cnt_str = NULL;
+    const char *mixer_ctl_name = "HDMI_RX Channels";
+    switch (channel_count) {
+    case 8:
+        channel_cnt_str = "Eight"; break;
+    case 7:
+        channel_cnt_str = "Seven"; break;
+    case 6:
+        channel_cnt_str = "Six"; break;
+    case 5:
+        channel_cnt_str = "Five"; break;
+    case 4:
+        channel_cnt_str = "Four"; break;
+    case 3:
+        channel_cnt_str = "Three"; break;
+    default:
+        channel_cnt_str = "Two"; break;
+    }
+    ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
+    if (!ctl) {
+        ALOGE("%s: Could not get ctl for mixer cmd - %s",
+              __func__, mixer_ctl_name);
+        return -EINVAL;
+    }
+    ALOGV("HDMI channel count: %s", channel_cnt_str);
+    mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
+    return 0;
+}
+
+int platform_edid_get_max_channels(void *platform)
+{
+    FILE *file;
+    struct audio_block_header header;
+    char block[MAX_SAD_BLOCKS * SAD_BLOCK_SIZE];
+    char *sad = block;
+    int num_audio_blocks;
+    int channel_count;
+    int max_channels = 0;
+    int i;
+
+    file = fopen(AUDIO_DATA_BLOCK_PATH, "rb");
+    if (file == NULL) {
+        ALOGE("Unable to open '%s'", AUDIO_DATA_BLOCK_PATH);
+        return 0;
+    }
+
+    /* Read audio block header */
+    fread(&header, 1, sizeof(header), file);
+
+    /* Read SAD blocks, clamping the maximum size for safety */
+    if (header.length > (int)sizeof(block))
+        header.length = (int)sizeof(block);
+    fread(&block, header.length, 1, file);
+
+    fclose(file);
+
+    /* Calculate the number of SAD blocks */
+    num_audio_blocks = header.length / SAD_BLOCK_SIZE;
+
+    for (i = 0; i < num_audio_blocks; i++) {
+        /* Only consider LPCM blocks */
+        if ((sad[0] >> 3) != EDID_FORMAT_LPCM)
+            continue;
+
+        channel_count = (sad[0] & 0x7) + 1;
+        if (channel_count > max_channels)
+            max_channels = channel_count;
+
+        /* Advance to next block */
+        sad += 3;
+    }
+
+    return max_channels;
+}
+
+/* Delay in Us */
+int64_t platform_render_latency(audio_usecase_t usecase)
+{
+    switch (usecase) {
+        case USECASE_AUDIO_PLAYBACK_DEEP_BUFFER:
+            return DEEP_BUFFER_PLATFORM_DELAY;
+        case USECASE_AUDIO_PLAYBACK_LOW_LATENCY:
+            return LOW_LATENCY_PLATFORM_DELAY;
+        default:
+            return 0;
+    }
+}
diff --git a/hal/msm8960/platform.h b/hal/msm8960/platform.h
new file mode 100644
index 0000000..4bc5003
--- /dev/null
+++ b/hal/msm8960/platform.h
@@ -0,0 +1,117 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef QCOM_AUDIO_PLATFORM_H
+#define QCOM_AUDIO_PLATFORM_H
+
+/*
+ * Below are the devices for which is back end is same, SLIMBUS_0_RX.
+ * All these devices are handled by the internal HW codec. We can
+ * enable any one of these devices at any time
+ */
+#define AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND \
+    (AUDIO_DEVICE_OUT_EARPIECE | AUDIO_DEVICE_OUT_SPEAKER | \
+     AUDIO_DEVICE_OUT_WIRED_HEADSET | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)
+
+/* Sound devices specific to the platform
+ * The DEVICE_OUT_* and DEVICE_IN_* should be mapped to these sound
+ * devices to enable corresponding mixer paths
+ */
+enum {
+    SND_DEVICE_NONE = 0,
+
+    /* Playback devices */
+    SND_DEVICE_MIN,
+    SND_DEVICE_OUT_BEGIN = SND_DEVICE_MIN,
+    SND_DEVICE_OUT_HANDSET = SND_DEVICE_OUT_BEGIN,
+    SND_DEVICE_OUT_SPEAKER,
+    SND_DEVICE_OUT_SPEAKER_REVERSE,
+    SND_DEVICE_OUT_HEADPHONES,
+    SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES,
+    SND_DEVICE_OUT_VOICE_SPEAKER,
+    SND_DEVICE_OUT_VOICE_HEADPHONES,
+    SND_DEVICE_OUT_HDMI,
+    SND_DEVICE_OUT_SPEAKER_AND_HDMI,
+    SND_DEVICE_OUT_BT_SCO,
+    SND_DEVICE_OUT_VOICE_HANDSET_TMUS,
+    SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES,
+    SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES,
+    SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET,
+    SND_DEVICE_OUT_END,
+
+    /*
+     * Note: IN_BEGIN should be same as OUT_END because total number of devices
+     * SND_DEVICES_MAX should not exceed MAX_RX + MAX_TX devices.
+     */
+    /* Capture devices */
+    SND_DEVICE_IN_BEGIN = SND_DEVICE_OUT_END,
+    SND_DEVICE_IN_HANDSET_MIC  = SND_DEVICE_IN_BEGIN,
+    SND_DEVICE_IN_SPEAKER_MIC,
+    SND_DEVICE_IN_HEADSET_MIC,
+    SND_DEVICE_IN_HANDSET_MIC_AEC,
+    SND_DEVICE_IN_SPEAKER_MIC_AEC,
+    SND_DEVICE_IN_HEADSET_MIC_AEC,
+    SND_DEVICE_IN_VOICE_SPEAKER_MIC,
+    SND_DEVICE_IN_VOICE_HEADSET_MIC,
+    SND_DEVICE_IN_HDMI_MIC,
+    SND_DEVICE_IN_BT_SCO_MIC,
+    SND_DEVICE_IN_CAMCORDER_MIC,
+    SND_DEVICE_IN_VOICE_DMIC_EF,
+    SND_DEVICE_IN_VOICE_DMIC_BS,
+    SND_DEVICE_IN_VOICE_DMIC_EF_TMUS,
+    SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF,
+    SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS,
+    SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC,
+    SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC,
+    SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC,
+    SND_DEVICE_IN_VOICE_REC_MIC,
+    SND_DEVICE_IN_VOICE_REC_DMIC_EF,
+    SND_DEVICE_IN_VOICE_REC_DMIC_BS,
+    SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE,
+    SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE,
+    SND_DEVICE_IN_END,
+
+    SND_DEVICE_MAX = SND_DEVICE_IN_END,
+
+};
+
+#define MIXER_CARD 0
+#define SOUND_CARD 0
+
+#define DEFAULT_OUTPUT_SAMPLING_RATE 48000
+
+/*
+ * tinyAlsa library interprets period size as number of frames
+ * one frame = channel_count * sizeof (pcm sample)
+ * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
+ * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
+ * We should take care of returning proper size when AudioFlinger queries for
+ * the buffer size of an input/output stream
+ */
+#define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 960
+#define DEEP_BUFFER_OUTPUT_PERIOD_COUNT 8
+#define LOW_LATENCY_OUTPUT_PERIOD_SIZE 240
+#define LOW_LATENCY_OUTPUT_PERIOD_COUNT 2
+
+#define HDMI_MULTI_PERIOD_SIZE  336
+#define HDMI_MULTI_PERIOD_COUNT 8
+#define HDMI_MULTI_DEFAULT_CHANNEL_COUNT 6
+#define HDMI_MULTI_PERIOD_BYTES (HDMI_MULTI_PERIOD_SIZE * HDMI_MULTI_DEFAULT_CHANNEL_COUNT * 2)
+
+#define AUDIO_CAPTURE_PERIOD_DURATION_MSEC 20
+#define AUDIO_CAPTURE_PERIOD_COUNT 2
+
+#endif // QCOM_AUDIO_PLATFORM_H
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
new file mode 100644
index 0000000..b5d568f
--- /dev/null
+++ b/hal/msm8974/platform.c
@@ -0,0 +1,858 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "msm8974_platform"
+/*#define LOG_NDEBUG 0*/
+#define LOG_NDDEBUG 0
+
+#include <stdlib.h>
+#include <dlfcn.h>
+#include <cutils/log.h>
+#include <cutils/properties.h>
+#include <audio_hw.h>
+#include <platform_api.h>
+#include "platform.h"
+
+#define MIXER_XML_PATH "/system/etc/mixer_paths.xml"
+#define LIB_ACDB_LOADER "libacdbloader.so"
+#define AUDIO_DATA_BLOCK_MIXER_CTL "HDMI EDID"
+
+#define DUALMIC_CONFIG_NONE 0      /* Target does not contain 2 mics */
+#define DUALMIC_CONFIG_ENDFIRE 1
+#define DUALMIC_CONFIG_BROADSIDE 2
+
+/*
+ * This file will have a maximum of 38 bytes:
+ *
+ * 4 bytes: number of audio blocks
+ * 4 bytes: total length of Short Audio Descriptor (SAD) blocks
+ * Maximum 10 * 3 bytes: SAD blocks
+ */
+#define MAX_SAD_BLOCKS      10
+#define SAD_BLOCK_SIZE      3
+
+/* EDID format ID for LPCM audio */
+#define EDID_FORMAT_LPCM    1
+
+/* Retry for delay in FW loading*/
+#define RETRY_NUMBER 10
+#define RETRY_US 500000
+
+#define MAX_VOL_INDEX 5
+#define MIN_VOL_INDEX 0
+#define percent_to_index(val, min, max) \
+	        ((val) * ((max) - (min)) * 0.01 + (min) + .5)
+
+struct audio_block_header
+{
+    int reserved;
+    int length;
+};
+
+typedef void (*acdb_deallocate_t)();
+typedef int  (*acdb_init_t)();
+typedef void (*acdb_send_audio_cal_t)(int, int);
+typedef void (*acdb_send_voice_cal_t)(int, int);
+
+/* Audio calibration related functions */
+struct platform_data {
+    struct audio_device *adev;
+    bool fluence_in_spkr_mode;
+    bool fluence_in_voice_call;
+    bool fluence_in_voice_rec;
+    int  dualmic_config;
+
+    void *acdb_handle;
+    acdb_init_t acdb_init;
+    acdb_deallocate_t acdb_deallocate;
+    acdb_send_audio_cal_t acdb_send_audio_cal;
+    acdb_send_voice_cal_t acdb_send_voice_cal;
+};
+
+static const int pcm_device_table[AUDIO_USECASE_MAX][2] = {
+    [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
+    [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
+    [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
+    [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
+    [USECASE_AUDIO_RECORD] = {0, 0},
+    [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
+    [USECASE_VOICE_CALL] = {2, 2},
+};
+
+/* Array to store sound devices */
+static const char * const device_table[SND_DEVICE_MAX] = {
+    [SND_DEVICE_NONE] = "none",
+    /* Playback sound devices */
+    [SND_DEVICE_OUT_HANDSET] = "handset",
+    [SND_DEVICE_OUT_SPEAKER] = "speaker",
+    [SND_DEVICE_OUT_SPEAKER_REVERSE] = "speaker-reverse",
+    [SND_DEVICE_OUT_HEADPHONES] = "headphones",
+    [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones",
+    [SND_DEVICE_OUT_VOICE_HANDSET] = "voice-handset",
+    [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker",
+    [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones",
+    [SND_DEVICE_OUT_HDMI] = "hdmi",
+    [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi",
+    [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset",
+    [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = "voice-handset-tmus",
+    [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones",
+    [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones",
+    [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset",
+
+    /* Capture sound devices */
+    [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
+    [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic",
+    [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic",
+    [SND_DEVICE_IN_HANDSET_MIC_AEC] = "handset-mic",
+    [SND_DEVICE_IN_SPEAKER_MIC_AEC] = "voice-speaker-mic",
+    [SND_DEVICE_IN_HEADSET_MIC_AEC] = "headset-mic",
+    [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic",
+    [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic",
+    [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic",
+    [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic",
+    [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic",
+    [SND_DEVICE_IN_VOICE_DMIC_EF] = "voice-dmic-ef",
+    [SND_DEVICE_IN_VOICE_DMIC_BS] = "voice-dmic-bs",
+    [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = "voice-dmic-ef-tmus",
+    [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = "voice-speaker-dmic-ef",
+    [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = "voice-speaker-dmic-bs",
+    [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
+    [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
+    [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
+    [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
+    [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = "voice-rec-dmic-ef",
+    [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = "voice-rec-dmic-bs",
+    [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = "voice-rec-dmic-ef-fluence",
+    [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = "voice-rec-dmic-bs-fluence",
+};
+
+/* ACDB IDs (audio DSP path configuration IDs) for each sound device */
+static const int acdb_device_table[SND_DEVICE_MAX] = {
+    [SND_DEVICE_NONE] = -1,
+    [SND_DEVICE_OUT_HANDSET] = 7,
+    [SND_DEVICE_OUT_SPEAKER] = 15,
+    [SND_DEVICE_OUT_SPEAKER_REVERSE] = 15,
+    [SND_DEVICE_OUT_HEADPHONES] = 10,
+    [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = 10,
+    [SND_DEVICE_OUT_VOICE_HANDSET] = 7,
+    [SND_DEVICE_OUT_VOICE_SPEAKER] = 15,
+    [SND_DEVICE_OUT_VOICE_HEADPHONES] = 10,
+    [SND_DEVICE_OUT_HDMI] = 18,
+    [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 15,
+    [SND_DEVICE_OUT_BT_SCO] = 22,
+    [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = 88,
+    [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = 17,
+    [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = 17,
+    [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = 37,
+
+    [SND_DEVICE_IN_HANDSET_MIC] = 4,
+    [SND_DEVICE_IN_SPEAKER_MIC] = 4, /* ToDo: Check if this needs to changed to 11 */
+    [SND_DEVICE_IN_HEADSET_MIC] = 8,
+    [SND_DEVICE_IN_HANDSET_MIC_AEC] = 40,
+    [SND_DEVICE_IN_SPEAKER_MIC_AEC] = 42,
+    [SND_DEVICE_IN_HEADSET_MIC_AEC] = 47,
+    [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = 11,
+    [SND_DEVICE_IN_VOICE_HEADSET_MIC] = 8,
+    [SND_DEVICE_IN_HDMI_MIC] = 4,
+    [SND_DEVICE_IN_BT_SCO_MIC] = 21,
+    [SND_DEVICE_IN_CAMCORDER_MIC] = 61,
+    [SND_DEVICE_IN_VOICE_DMIC_EF] = 41,
+    [SND_DEVICE_IN_VOICE_DMIC_BS] = 5,
+    [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = 89,
+    [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = 43,
+    [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = 12,
+    [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = 16,
+    [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = 36,
+    [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = 16,
+    [SND_DEVICE_IN_VOICE_REC_MIC] = 62,
+    /* TODO: Update with proper acdb ids */
+    [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = 62,
+    [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = 62,
+    [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = 6,
+    [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = 5,
+};
+
+#define DEEP_BUFFER_PLATFORM_DELAY (29*1000LL)
+#define LOW_LATENCY_PLATFORM_DELAY (13*1000LL)
+
+static pthread_once_t check_op_once_ctl = PTHREAD_ONCE_INIT;
+static bool is_tmus = false;
+
+static void check_operator()
+{
+    char value[PROPERTY_VALUE_MAX];
+    int mccmnc;
+    property_get("gsm.sim.operator.numeric",value,"0");
+    mccmnc = atoi(value);
+    ALOGD("%s: tmus mccmnc %d", __func__, mccmnc);
+    switch(mccmnc) {
+    /* TMUS MCC(310), MNC(490, 260, 026) */
+    case 310490:
+    case 310260:
+    case 310026:
+    /* Add new TMUS MNC(800, 660, 580, 310, 270, 250, 240, 230, 220, 210, 200, 160) */
+    case 310800:
+    case 310660:
+    case 310580:
+    case 310310:
+    case 310270:
+    case 310250:
+    case 310240:
+    case 310230:
+    case 310220:
+    case 310210:
+    case 310200:
+    case 310160:
+        is_tmus = true;
+        break;
+    }
+}
+
+bool is_operator_tmus()
+{
+    pthread_once(&check_op_once_ctl, check_operator);
+    return is_tmus;
+}
+
+static int set_volume_values(int type, int volume, int* values)
+{
+    values[0] = volume;
+    values[1] = ALL_SESSION_VSID;
+
+    switch(type) {
+    case VOLUME_SET:
+        values[2] = DEFAULT_VOLUME_RAMP_DURATION_MS;
+        break;
+    case MUTE_SET:
+        values[2] = DEFAULT_MUTE_RAMP_DURATION;
+        break;
+    default:
+        return -EINVAL;
+    }
+    return 0;
+}
+
+static int set_echo_reference(struct mixer *mixer, const char* ec_ref)
+{
+    struct mixer_ctl *ctl;
+    const char *mixer_ctl_name = "EC_REF_RX";
+
+    ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
+    if (!ctl) {
+        ALOGE("%s: Could not get ctl for mixer cmd - %s",
+              __func__, mixer_ctl_name);
+        return -EINVAL;
+    }
+    ALOGV("Setting EC Reference: %s", ec_ref);
+    mixer_ctl_set_enum_by_string(ctl, ec_ref);
+    return 0;
+}
+
+void *platform_init(struct audio_device *adev)
+{
+    char value[PROPERTY_VALUE_MAX];
+    struct platform_data *my_data;
+    int retry_num = 0;
+
+    adev->mixer = mixer_open(MIXER_CARD);
+
+    while (!adev->mixer && retry_num < RETRY_NUMBER) {
+        usleep(RETRY_US);
+        adev->mixer = mixer_open(MIXER_CARD);
+        retry_num++;
+    }
+
+    if (!adev->mixer) {
+        ALOGE("Unable to open the mixer, aborting.");
+        return NULL;
+    }
+
+    adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH);
+    if (!adev->audio_route) {
+        ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
+        return NULL;
+    }
+
+    my_data = calloc(1, sizeof(struct platform_data));
+
+    my_data->adev = adev;
+    my_data->dualmic_config = DUALMIC_CONFIG_NONE;
+    my_data->fluence_in_spkr_mode = false;
+    my_data->fluence_in_voice_call = false;
+    my_data->fluence_in_voice_rec = false;
+
+    property_get("persist.audio.dualmic.config",value,"");
+    if (!strcmp("broadside", value)) {
+        my_data->dualmic_config = DUALMIC_CONFIG_BROADSIDE;
+        adev->acdb_settings |= DMIC_FLAG;
+    } else if (!strcmp("endfire", value)) {
+        my_data->dualmic_config = DUALMIC_CONFIG_ENDFIRE;
+        adev->acdb_settings |= DMIC_FLAG;
+    }
+
+    if (my_data->dualmic_config != DUALMIC_CONFIG_NONE) {
+        property_get("persist.audio.fluence.voicecall",value,"");
+        if (!strcmp("true", value)) {
+            my_data->fluence_in_voice_call = true;
+        }
+
+        property_get("persist.audio.fluence.voicerec",value,"");
+        if (!strcmp("true", value)) {
+            my_data->fluence_in_voice_rec = true;
+        }
+
+        property_get("persist.audio.fluence.speaker",value,"");
+        if (!strcmp("true", value)) {
+            my_data->fluence_in_spkr_mode = true;
+        }
+    }
+
+    my_data->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
+    if (my_data->acdb_handle == NULL) {
+        ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
+    } else {
+        ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
+        my_data->acdb_deallocate = (acdb_deallocate_t)dlsym(my_data->acdb_handle,
+                                                    "acdb_loader_deallocate_ACDB");
+        my_data->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(my_data->acdb_handle,
+                                                    "acdb_loader_send_audio_cal");
+        if (!my_data->acdb_send_audio_cal)
+            ALOGW("%s: Could not find the symbol acdb_send_audio_cal from %s",
+                  __func__, LIB_ACDB_LOADER);
+        my_data->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(my_data->acdb_handle,
+                                                    "acdb_loader_send_voice_cal");
+        my_data->acdb_init = (acdb_init_t)dlsym(my_data->acdb_handle,
+                                                    "acdb_loader_init_ACDB");
+        if (my_data->acdb_init == NULL)
+            ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
+        else
+            my_data->acdb_init();
+    }
+
+    return my_data;
+}
+
+void platform_deinit(void *platform)
+{
+    free(platform);
+}
+
+const char *platform_get_snd_device_name(snd_device_t snd_device)
+{
+    if (snd_device >= SND_DEVICE_MIN && snd_device < SND_DEVICE_MAX)
+        return device_table[snd_device];
+    else
+        return "";
+}
+
+void platform_add_backend_name(char *mixer_path, snd_device_t snd_device)
+{
+    if (snd_device == SND_DEVICE_IN_BT_SCO_MIC)
+        strcat(mixer_path, " bt-sco");
+    else if(snd_device == SND_DEVICE_OUT_BT_SCO)
+        strcat(mixer_path, " bt-sco");
+    else if (snd_device == SND_DEVICE_OUT_HDMI)
+        strcat(mixer_path, " hdmi");
+    else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HDMI)
+        strcat(mixer_path, " speaker-and-hdmi");
+}
+
+int platform_get_pcm_device_id(audio_usecase_t usecase, int device_type)
+{
+    int device_id;
+    if (device_type == PCM_PLAYBACK)
+        device_id = pcm_device_table[usecase][0];
+    else
+        device_id = pcm_device_table[usecase][1];
+    return device_id;
+}
+
+int platform_send_audio_calibration(void *platform, snd_device_t snd_device)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    int acdb_dev_id, acdb_dev_type;
+
+    acdb_dev_id = acdb_device_table[snd_device];
+    if (acdb_dev_id < 0) {
+        ALOGE("%s: Could not find acdb id for device(%d)",
+              __func__, snd_device);
+        return -EINVAL;
+    }
+    if (my_data->acdb_send_audio_cal) {
+        ("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
+              __func__, snd_device, acdb_dev_id);
+        if (snd_device >= SND_DEVICE_OUT_BEGIN &&
+                snd_device < SND_DEVICE_OUT_END)
+            acdb_dev_type = ACDB_DEV_TYPE_OUT;
+        else
+            acdb_dev_type = ACDB_DEV_TYPE_IN;
+        my_data->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
+    }
+    return 0;
+}
+
+int platform_switch_voice_call_device_pre(void *platform)
+{
+    return 0;
+}
+
+int platform_switch_voice_call_device_post(void *platform,
+                                           snd_device_t out_snd_device,
+                                           snd_device_t in_snd_device)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    int acdb_rx_id, acdb_tx_id;
+
+    if (my_data->acdb_send_voice_cal == NULL) {
+        ALOGE("%s: dlsym error for acdb_send_voice_call", __func__);
+    } else {
+        acdb_rx_id = acdb_device_table[out_snd_device];
+        acdb_tx_id = acdb_device_table[in_snd_device];
+
+        if (acdb_rx_id > 0 && acdb_tx_id > 0)
+            my_data->acdb_send_voice_cal(acdb_rx_id, acdb_tx_id);
+        else
+            ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
+                  acdb_rx_id, acdb_tx_id);
+    }
+
+    return 0;
+}
+
+int platform_start_voice_call(void *platform)
+{
+    return 0;
+}
+
+int platform_stop_voice_call(void *platform)
+{
+    return 0;
+}
+
+int platform_set_voice_volume(void *platform, int volume)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    struct audio_device *adev = my_data->adev;
+    struct mixer_ctl *ctl;
+    const char *mixer_ctl_name = "Voice Rx Gain";
+    int values[VOLUME_CTL_PARAM_NUM];
+    int ret = 0;
+
+    // Voice volume levels are mapped to adsp volume levels as follows.
+    // 100 -> 5, 80 -> 4, 60 -> 3, 40 -> 2, 20 -> 1  0 -> 0
+    // But this values don't changed in kernel. So, below change is need.
+    volume = (int)percent_to_index(volume, MIN_VOL_INDEX, MAX_VOL_INDEX);
+
+    ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
+    if (!ctl) {
+        ALOGE("%s: Could not get ctl for mixer cmd - %s",
+              __func__, mixer_ctl_name);
+        return -EINVAL;
+    }
+    ret = set_volume_values(VOLUME_SET, volume, values);
+    if (ret < 0) {
+        ALOGV("%s: failed setting volume by incorrect type", __func__);
+        return -EINVAL;
+    }
+    ret = mixer_ctl_set_array(ctl, values, sizeof(values)/sizeof(int));
+    if (ret < 0) {
+        ALOGV("%s: failed set mixer ctl by %d", __func__, ret);
+        return -EINVAL;
+    }
+
+    return 0;
+}
+
+int platform_set_mic_mute(void *platform, bool state)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    struct audio_device *adev = my_data->adev;
+    struct mixer_ctl *ctl;
+    const char *mixer_ctl_name = "Voice Tx Mute";
+    int values[VOLUME_CTL_PARAM_NUM];
+    int ret = 0;
+
+    if (adev->mode == AUDIO_MODE_IN_CALL) {
+        ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
+        if (!ctl) {
+            ALOGE("%s: Could not get ctl for mixer cmd - %s",
+                  __func__, mixer_ctl_name);
+            return -EINVAL;
+        }
+        ALOGV("Setting mic mute: %d", state);
+        ret = set_volume_values(MUTE_SET, state, values);
+        if (ret < 0) {
+            ALOGV("%s: failed setting mute by incorrect type", __func__);
+            return -EINVAL;
+        }
+        ret = mixer_ctl_set_array(ctl, values, sizeof(values)/sizeof(int));
+        if (ret < 0) {
+            ALOGV("%s: failed set mixer ctl by %d", __func__, ret);
+            return -EINVAL;
+        }
+    }
+
+    return 0;
+}
+
+snd_device_t platform_get_output_snd_device(void *platform, audio_devices_t devices)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    struct audio_device *adev = my_data->adev;
+    audio_mode_t mode = adev->mode;
+    snd_device_t snd_device = SND_DEVICE_NONE;
+
+    ALOGV("%s: enter: output devices(%#x)", __func__, devices);
+    if (devices == AUDIO_DEVICE_NONE ||
+        devices & AUDIO_DEVICE_BIT_IN) {
+        ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
+        goto exit;
+    }
+
+    if (mode == AUDIO_MODE_IN_CALL) {
+        if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
+            devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
+            if (adev->tty_mode == TTY_MODE_FULL)
+                snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
+            else if (adev->tty_mode == TTY_MODE_VCO)
+                snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
+            else if (adev->tty_mode == TTY_MODE_HCO)
+                snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
+            else
+                snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
+        } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
+            snd_device = SND_DEVICE_OUT_BT_SCO;
+        } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
+            snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
+        } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
+            if (is_operator_tmus())
+                snd_device = SND_DEVICE_OUT_VOICE_HANDSET_TMUS;
+            else
+                snd_device = SND_DEVICE_OUT_HANDSET;
+        }
+        if (snd_device != SND_DEVICE_NONE) {
+            goto exit;
+        }
+    }
+
+    if (popcount(devices) == 2) {
+        if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
+                        AUDIO_DEVICE_OUT_SPEAKER)) {
+            snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
+        } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
+                               AUDIO_DEVICE_OUT_SPEAKER)) {
+            snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
+        } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
+                               AUDIO_DEVICE_OUT_SPEAKER)) {
+            snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
+        } else {
+            ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
+            goto exit;
+        }
+        if (snd_device != SND_DEVICE_NONE) {
+            goto exit;
+        }
+    }
+
+    if (popcount(devices) != 1) {
+        ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
+        goto exit;
+    }
+
+    if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
+        devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
+        snd_device = SND_DEVICE_OUT_HEADPHONES;
+    } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
+        if (adev->speaker_lr_swap)
+            snd_device = SND_DEVICE_OUT_SPEAKER_REVERSE;
+        else
+            snd_device = SND_DEVICE_OUT_SPEAKER;
+    } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
+        snd_device = SND_DEVICE_OUT_BT_SCO;
+    } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
+        snd_device = SND_DEVICE_OUT_HDMI ;
+    } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
+        snd_device = SND_DEVICE_OUT_HANDSET;
+    } else {
+        ALOGE("%s: Unknown device(s) %#x", __func__, devices);
+    }
+exit:
+    ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
+    return snd_device;
+}
+
+snd_device_t platform_get_input_snd_device(void *platform, audio_devices_t out_device)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    struct audio_device *adev = my_data->adev;
+    audio_source_t  source = (adev->active_input == NULL) ?
+                                AUDIO_SOURCE_DEFAULT : adev->active_input->source;
+
+    audio_mode_t    mode   = adev->mode;
+    audio_devices_t in_device = ((adev->active_input == NULL) ?
+                                    AUDIO_DEVICE_NONE : adev->active_input->device)
+                                & ~AUDIO_DEVICE_BIT_IN;
+    audio_channel_mask_t channel_mask = (adev->active_input == NULL) ?
+                                AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask;
+    snd_device_t snd_device = SND_DEVICE_NONE;
+
+    ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
+          __func__, out_device, in_device);
+    if (mode == AUDIO_MODE_IN_CALL) {
+        if (out_device == AUDIO_DEVICE_NONE) {
+            ALOGE("%s: No output device set for voice call", __func__);
+            goto exit;
+        }
+        if (adev->tty_mode != TTY_MODE_OFF) {
+            if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
+                out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
+                switch (adev->tty_mode) {
+                case TTY_MODE_FULL:
+                    snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
+                    break;
+                case TTY_MODE_VCO:
+                    snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
+                    break;
+                case TTY_MODE_HCO:
+                    snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
+                    break;
+                default:
+                    ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->tty_mode);
+                }
+                goto exit;
+            }
+        }
+        if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
+            out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
+            if (my_data->fluence_in_voice_call == false) {
+                snd_device = SND_DEVICE_IN_HANDSET_MIC;
+            } else {
+                if (my_data->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
+                    if (is_operator_tmus())
+                        snd_device = SND_DEVICE_IN_VOICE_DMIC_EF_TMUS;
+                    else
+                        snd_device = SND_DEVICE_IN_VOICE_DMIC_EF;
+                } else if(my_data->dualmic_config == DUALMIC_CONFIG_BROADSIDE)
+                    snd_device = SND_DEVICE_IN_VOICE_DMIC_BS;
+                else
+                    snd_device = SND_DEVICE_IN_HANDSET_MIC;
+            }
+        } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
+            snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
+        } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
+            snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
+        } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
+            if (my_data->fluence_in_voice_call && my_data->fluence_in_spkr_mode &&
+                    my_data->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
+                snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF;
+            } else if (my_data->fluence_in_voice_call && my_data->fluence_in_spkr_mode &&
+                    my_data->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
+                snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS;
+            } else {
+                snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
+            }
+        }
+    } else if (source == AUDIO_SOURCE_CAMCORDER) {
+        if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
+            in_device & AUDIO_DEVICE_IN_BACK_MIC) {
+            snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
+        }
+    } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
+        if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
+            if (my_data->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
+                if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
+                    snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF;
+                else if (my_data->fluence_in_voice_rec)
+                    snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE;
+            } else if (my_data->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
+                if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
+                    snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS;
+                else if (my_data->fluence_in_voice_rec)
+                    snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE;
+            }
+
+            if (snd_device == SND_DEVICE_NONE) {
+                snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
+            }
+        }
+    } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
+        if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
+            in_device = AUDIO_DEVICE_IN_BACK_MIC;
+        if (adev->active_input) {
+            if (adev->active_input->enable_aec) {
+                if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
+                    snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
+                } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
+                    snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
+                } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
+                    snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC;
+                }
+                set_echo_reference(adev->mixer, "SLIM_RX");
+            } else
+                set_echo_reference(adev->mixer, "NONE");
+        }
+    } else if (source == AUDIO_SOURCE_DEFAULT) {
+        goto exit;
+    }
+
+
+    if (snd_device != SND_DEVICE_NONE) {
+        goto exit;
+    }
+
+    if (in_device != AUDIO_DEVICE_NONE &&
+            !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
+            !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
+        if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
+            snd_device = SND_DEVICE_IN_HANDSET_MIC;
+        } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
+            snd_device = SND_DEVICE_IN_SPEAKER_MIC;
+        } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
+            snd_device = SND_DEVICE_IN_HEADSET_MIC;
+        } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
+            snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
+        } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
+            snd_device = SND_DEVICE_IN_HDMI_MIC;
+        } else {
+            ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
+            ALOGW("%s: Using default handset-mic", __func__);
+            snd_device = SND_DEVICE_IN_HANDSET_MIC;
+        }
+    } else {
+        if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
+            snd_device = SND_DEVICE_IN_HANDSET_MIC;
+        } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
+            snd_device = SND_DEVICE_IN_HEADSET_MIC;
+        } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
+            snd_device = SND_DEVICE_IN_SPEAKER_MIC;
+        } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
+            snd_device = SND_DEVICE_IN_HANDSET_MIC;
+        } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
+            snd_device = SND_DEVICE_IN_BT_SCO_MIC;
+        } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
+            snd_device = SND_DEVICE_IN_HDMI_MIC;
+        } else {
+            ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
+            ALOGW("%s: Using default handset-mic", __func__);
+            snd_device = SND_DEVICE_IN_HANDSET_MIC;
+        }
+    }
+exit:
+    ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
+    return snd_device;
+}
+
+int platform_set_hdmi_channels(void *platform,  int channel_count)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    struct audio_device *adev = my_data->adev;
+    struct mixer_ctl *ctl;
+    const char *channel_cnt_str = NULL;
+    const char *mixer_ctl_name = "HDMI_RX Channels";
+    switch (channel_count) {
+    case 8:
+        channel_cnt_str = "Eight"; break;
+    case 7:
+        channel_cnt_str = "Seven"; break;
+    case 6:
+        channel_cnt_str = "Six"; break;
+    case 5:
+        channel_cnt_str = "Five"; break;
+    case 4:
+        channel_cnt_str = "Four"; break;
+    case 3:
+        channel_cnt_str = "Three"; break;
+    default:
+        channel_cnt_str = "Two"; break;
+    }
+    ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
+    if (!ctl) {
+        ALOGE("%s: Could not get ctl for mixer cmd - %s",
+              __func__, mixer_ctl_name);
+        return -EINVAL;
+    }
+    ALOGV("HDMI channel count: %s", channel_cnt_str);
+    mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
+    return 0;
+}
+
+int platform_edid_get_max_channels(void *platform)
+{
+    struct platform_data *my_data = (struct platform_data *)platform;
+    struct audio_device *adev = my_data->adev;
+    char block[MAX_SAD_BLOCKS * SAD_BLOCK_SIZE];
+    char *sad = block;
+    int num_audio_blocks;
+    int channel_count;
+    int max_channels = 0;
+    int i, ret, count;
+
+    struct mixer_ctl *ctl;
+
+    ctl = mixer_get_ctl_by_name(adev->mixer, AUDIO_DATA_BLOCK_MIXER_CTL);
+    if (!ctl) {
+        ALOGE("%s: Could not get ctl for mixer cmd - %s",
+              __func__, AUDIO_DATA_BLOCK_MIXER_CTL);
+        return 0;
+    }
+
+    mixer_ctl_update(ctl);
+
+    count = mixer_ctl_get_num_values(ctl);
+
+    /* Read SAD blocks, clamping the maximum size for safety */
+    if (count > (int)sizeof(block))
+        count = (int)sizeof(block);
+
+    ret = mixer_ctl_get_array(ctl, block, count);
+    if (ret != 0) {
+        ALOGE("%s: mixer_ctl_get_array() failed to get EDID info", __func__);
+        return 0;
+    }
+
+    /* Calculate the number of SAD blocks */
+    num_audio_blocks = count / SAD_BLOCK_SIZE;
+
+    for (i = 0; i < num_audio_blocks; i++) {
+        /* Only consider LPCM blocks */
+        if ((sad[0] >> 3) != EDID_FORMAT_LPCM) {
+            sad += 3;
+            continue;
+        }
+
+        channel_count = (sad[0] & 0x7) + 1;
+        if (channel_count > max_channels)
+            max_channels = channel_count;
+
+        /* Advance to next block */
+        sad += 3;
+    }
+
+    return max_channels;
+}
+
+/* Delay in Us */
+int64_t platform_render_latency(audio_usecase_t usecase)
+{
+    switch (usecase) {
+        case USECASE_AUDIO_PLAYBACK_DEEP_BUFFER:
+            return DEEP_BUFFER_PLATFORM_DELAY;
+        case USECASE_AUDIO_PLAYBACK_LOW_LATENCY:
+            return LOW_LATENCY_PLATFORM_DELAY;
+        default:
+            return 0;
+    }
+}
diff --git a/hal/msm8974/platform.h b/hal/msm8974/platform.h
new file mode 100644
index 0000000..a6b3c31
--- /dev/null
+++ b/hal/msm8974/platform.h
@@ -0,0 +1,125 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef QCOM_AUDIO_PLATFORM_H
+#define QCOM_AUDIO_PLATFORM_H
+
+/*
+ * Below are the devices for which is back end is same, SLIMBUS_0_RX.
+ * All these devices are handled by the internal HW codec. We can
+ * enable any one of these devices at any time
+ */
+#define AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND \
+    (AUDIO_DEVICE_OUT_EARPIECE | AUDIO_DEVICE_OUT_SPEAKER | \
+     AUDIO_DEVICE_OUT_WIRED_HEADSET | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)
+
+/* Sound devices specific to the platform
+ * The DEVICE_OUT_* and DEVICE_IN_* should be mapped to these sound
+ * devices to enable corresponding mixer paths
+ */
+enum {
+    SND_DEVICE_NONE = 0,
+
+    /* Playback devices */
+    SND_DEVICE_MIN,
+    SND_DEVICE_OUT_BEGIN = SND_DEVICE_MIN,
+    SND_DEVICE_OUT_HANDSET = SND_DEVICE_OUT_BEGIN,
+    SND_DEVICE_OUT_SPEAKER,
+    SND_DEVICE_OUT_SPEAKER_REVERSE,
+    SND_DEVICE_OUT_HEADPHONES,
+    SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES,
+    SND_DEVICE_OUT_VOICE_HANDSET,
+    SND_DEVICE_OUT_VOICE_SPEAKER,
+    SND_DEVICE_OUT_VOICE_HEADPHONES,
+    SND_DEVICE_OUT_HDMI,
+    SND_DEVICE_OUT_SPEAKER_AND_HDMI,
+    SND_DEVICE_OUT_BT_SCO,
+    SND_DEVICE_OUT_VOICE_HANDSET_TMUS,
+    SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES,
+    SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES,
+    SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET,
+    SND_DEVICE_OUT_END,
+
+    /*
+     * Note: IN_BEGIN should be same as OUT_END because total number of devices
+     * SND_DEVICES_MAX should not exceed MAX_RX + MAX_TX devices.
+     */
+    /* Capture devices */
+    SND_DEVICE_IN_BEGIN = SND_DEVICE_OUT_END,
+    SND_DEVICE_IN_HANDSET_MIC  = SND_DEVICE_IN_BEGIN,
+    SND_DEVICE_IN_SPEAKER_MIC,
+    SND_DEVICE_IN_HEADSET_MIC,
+    SND_DEVICE_IN_HANDSET_MIC_AEC,
+    SND_DEVICE_IN_SPEAKER_MIC_AEC,
+    SND_DEVICE_IN_HEADSET_MIC_AEC,
+    SND_DEVICE_IN_VOICE_SPEAKER_MIC,
+    SND_DEVICE_IN_VOICE_HEADSET_MIC,
+    SND_DEVICE_IN_HDMI_MIC,
+    SND_DEVICE_IN_BT_SCO_MIC,
+    SND_DEVICE_IN_CAMCORDER_MIC,
+    SND_DEVICE_IN_VOICE_DMIC_EF,
+    SND_DEVICE_IN_VOICE_DMIC_BS,
+    SND_DEVICE_IN_VOICE_DMIC_EF_TMUS,
+    SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF,
+    SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS,
+    SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC,
+    SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC,
+    SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC,
+    SND_DEVICE_IN_VOICE_REC_MIC,
+    SND_DEVICE_IN_VOICE_REC_DMIC_EF,
+    SND_DEVICE_IN_VOICE_REC_DMIC_BS,
+    SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE,
+    SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE,
+    SND_DEVICE_IN_END,
+
+    SND_DEVICE_MAX = SND_DEVICE_IN_END,
+
+};
+
+#define MIXER_CARD 0
+#define SOUND_CARD 0
+
+#define DEFAULT_OUTPUT_SAMPLING_RATE 48000
+
+#define ALL_SESSION_VSID    0xFFFFFFFF
+#define DEFAULT_MUTE_RAMP_DURATION      500
+#define DEFAULT_VOLUME_RAMP_DURATION_MS 20
+#define VOLUME_SET 0
+#define MUTE_SET 1
+#define VOLUME_CTL_PARAM_NUM 3
+
+/*
+ * tinyAlsa library interprets period size as number of frames
+ * one frame = channel_count * sizeof (pcm sample)
+ * so if format = 16-bit PCM and channels = Stereo, frame size = 2 ch * 2 = 4 bytes
+ * DEEP_BUFFER_OUTPUT_PERIOD_SIZE = 1024 means 1024 * 4 = 4096 bytes
+ * We should take care of returning proper size when AudioFlinger queries for
+ * the buffer size of an input/output stream
+ */
+#define DEEP_BUFFER_OUTPUT_PERIOD_SIZE 960
+#define DEEP_BUFFER_OUTPUT_PERIOD_COUNT 8
+#define LOW_LATENCY_OUTPUT_PERIOD_SIZE 240
+#define LOW_LATENCY_OUTPUT_PERIOD_COUNT 2
+
+#define HDMI_MULTI_PERIOD_SIZE  336
+#define HDMI_MULTI_PERIOD_COUNT 8
+#define HDMI_MULTI_DEFAULT_CHANNEL_COUNT 6
+#define HDMI_MULTI_PERIOD_BYTES (HDMI_MULTI_PERIOD_SIZE * HDMI_MULTI_DEFAULT_CHANNEL_COUNT * 2)
+
+#define AUDIO_CAPTURE_PERIOD_DURATION_MSEC 20
+#define AUDIO_CAPTURE_PERIOD_COUNT 2
+
+#endif // QCOM_AUDIO_PLATFORM_H
diff --git a/hal/platform_api.h b/hal/platform_api.h
new file mode 100644
index 0000000..afd2ee4
--- /dev/null
+++ b/hal/platform_api.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef QCOM_AUDIO_PLATFORM_API_H
+#define QCOM_AUDIO_PLATFORM_API_H
+
+void *platform_init(struct audio_device *adev);
+void platform_deinit(void *platform);
+const char *platform_get_snd_device_name(snd_device_t snd_device);
+void platform_add_backend_name(char *mixer_path, snd_device_t snd_device);
+int platform_get_pcm_device_id(audio_usecase_t usecase, int device_type);
+int platform_send_audio_calibration(void *platform, snd_device_t snd_device);
+int platform_switch_voice_call_device_pre(void *platform);
+int platform_switch_voice_call_device_post(void *platform,
+                                           snd_device_t out_snd_device,
+                                           snd_device_t in_snd_device);
+int platform_start_voice_call(void *platform);
+int platform_stop_voice_call(void *platform);
+int platform_set_voice_volume(void *platform, int volume);
+int platform_set_mic_mute(void *platform, bool state);
+snd_device_t platform_get_output_snd_device(void *platform, audio_devices_t devices);
+snd_device_t platform_get_input_snd_device(void *platform, audio_devices_t out_device);
+int platform_set_hdmi_channels(void *platform, int channel_count);
+int platform_edid_get_max_channels(void *platform);
+
+/* returns the latency for a usecase in Us */
+int64_t platform_render_latency(audio_usecase_t usecase);
+
+#endif // QCOM_AUDIO_PLATFORM_API_H
diff --git a/visualizer/Android.mk b/visualizer/Android.mk
new file mode 100644
index 0000000..3c92044
--- /dev/null
+++ b/visualizer/Android.mk
@@ -0,0 +1,36 @@
+# Copyright 2013 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= \
+	offload_visualizer.c
+
+LOCAL_CFLAGS+= -O2 -fvisibility=hidden
+
+LOCAL_SHARED_LIBRARIES := \
+	libcutils \
+	liblog \
+	libtinyalsa
+
+LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/soundfx
+LOCAL_MODULE:= libqcomvisualizer
+
+LOCAL_C_INCLUDES := \
+	external/tinyalsa/include \
+	$(call include-path-for, audio-effects)
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/visualizer/MODULE_LICENSE_APACHE2 b/visualizer/MODULE_LICENSE_APACHE2
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/visualizer/MODULE_LICENSE_APACHE2
diff --git a/visualizer/NOTICE b/visualizer/NOTICE
new file mode 100644
index 0000000..ad6ed94
--- /dev/null
+++ b/visualizer/NOTICE
@@ -0,0 +1,190 @@
+
+   Copyright (c) 2013, The Android Open Source Project
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+
+
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
diff --git a/visualizer/offload_visualizer.c b/visualizer/offload_visualizer.c
new file mode 100644
index 0000000..eb43558
--- /dev/null
+++ b/visualizer/offload_visualizer.c
@@ -0,0 +1,1241 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "offload_visualizer"
+/*#define LOG_NDEBUG 0*/
+#include <assert.h>
+#include <math.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <sys/prctl.h>
+
+#include <cutils/list.h>
+#include <cutils/log.h>
+#include <system/thread_defs.h>
+#include <tinyalsa/asoundlib.h>
+#include <audio_effects/effect_visualizer.h>
+
+
+enum {
+    EFFECT_STATE_UNINITIALIZED,
+    EFFECT_STATE_INITIALIZED,
+    EFFECT_STATE_ACTIVE,
+};
+
+typedef struct effect_context_s effect_context_t;
+
+/* effect specific operations. Only the init() and process() operations must be defined.
+ * Others are optional.
+ */
+typedef struct effect_ops_s {
+    int (*init)(effect_context_t *context);
+    int (*release)(effect_context_t *context);
+    int (*reset)(effect_context_t *context);
+    int (*enable)(effect_context_t *context);
+    int (*disable)(effect_context_t *context);
+    int (*process)(effect_context_t *context, audio_buffer_t *in, audio_buffer_t *out);
+    int (*set_parameter)(effect_context_t *context, effect_param_t *param, uint32_t size);
+    int (*get_parameter)(effect_context_t *context, effect_param_t *param, uint32_t *size);
+    int (*command)(effect_context_t *context, uint32_t cmdCode, uint32_t cmdSize,
+            void *pCmdData, uint32_t *replySize, void *pReplyData);
+} effect_ops_t;
+
+struct effect_context_s {
+    const struct effect_interface_s *itfe;
+    struct listnode effects_list_node;  /* node in created_effects_list */
+    struct listnode output_node;  /* node in output_context_t.effects_list */
+    effect_config_t config;
+    const effect_descriptor_t *desc;
+    audio_io_handle_t out_handle;  /* io handle of the output the effect is attached to */
+    uint32_t state;
+    bool offload_enabled;  /* when offload is enabled we process VISUALIZER_CMD_CAPTURE command.
+                              Otherwise non offloaded visualizer has already processed the command
+                              and we must not overwrite the reply. */
+    effect_ops_t ops;
+};
+
+typedef struct output_context_s {
+    struct listnode outputs_list_node;  /* node in active_outputs_list */
+    audio_io_handle_t handle; /* io handle */
+    struct listnode effects_list; /* list of effects attached to this output */
+} output_context_t;
+
+
+/* maximum time since last capture buffer update before resetting capture buffer. This means
+  that the framework has stopped playing audio and we must start returning silence */
+#define MAX_STALL_TIME_MS 1000
+
+#define CAPTURE_BUF_SIZE 65536 /* "64k should be enough for everyone" */
+
+#define DISCARD_MEASUREMENTS_TIME_MS 2000 /* discard measurements older than this number of ms */
+
+/* maximum number of buffers for which we keep track of the measurements */
+#define MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS 25 /* note: buffer index is stored in uint8_t */
+
+typedef struct buffer_stats_s {
+    bool is_valid;
+    uint16_t peak_u16; /* the positive peak of the absolute value of the samples in a buffer */
+    float rms_squared; /* the average square of the samples in a buffer */
+} buffer_stats_t;
+
+typedef struct visualizer_context_s {
+    effect_context_t common;
+
+    uint32_t capture_idx;
+    uint32_t capture_size;
+    uint32_t scaling_mode;
+    uint32_t last_capture_idx;
+    uint32_t latency;
+    struct timespec buffer_update_time;
+    uint8_t capture_buf[CAPTURE_BUF_SIZE];
+    /* for measurements */
+    uint8_t channel_count; /* to avoid recomputing it every time a buffer is processed */
+    uint32_t meas_mode;
+    uint8_t meas_wndw_size_in_buffers;
+    uint8_t meas_buffer_idx;
+    buffer_stats_t past_meas[MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS];
+} visualizer_context_t;
+
+
+extern const struct effect_interface_s effect_interface;
+
+/* Offload visualizer UUID: 7a8044a0-1a71-11e3-a184-0002a5d5c51b */
+const effect_descriptor_t visualizer_descriptor = {
+        {0xe46b26a0, 0xdddd, 0x11db, 0x8afd, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
+        {0x7a8044a0, 0x1a71, 0x11e3, 0xa184, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}},
+        EFFECT_CONTROL_API_VERSION,
+        (EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_HW_ACC_TUNNEL ),
+        0, /* TODO */
+        1,
+        "QCOM MSM offload visualizer",
+        "The Android Open Source Project",
+};
+
+const effect_descriptor_t *descriptors[] = {
+        &visualizer_descriptor,
+        NULL,
+};
+
+
+pthread_once_t once = PTHREAD_ONCE_INIT;
+int init_status;
+
+/* list of created effects. Updated by visualizer_hal_start_output()
+ * and visualizer_hal_stop_output() */
+struct listnode created_effects_list;
+/* list of active output streams. Updated by visualizer_hal_start_output()
+ * and visualizer_hal_stop_output() */
+struct listnode active_outputs_list;
+
+/* thread capturing PCM from Proxy port and calling the process function on each enabled effect
+ * attached to an active output stream */
+pthread_t capture_thread;
+/* lock must be held when modifying or accessing created_effects_list or active_outputs_list */
+pthread_mutex_t lock;
+/* thread_lock must be held when starting or stopping the capture thread.
+ * Locking order: thread_lock -> lock */
+pthread_mutex_t thread_lock;
+/* cond is signaled when an output is started or stopped or an effect is enabled or disable: the
+ * capture thread will reevaluate the capture and effect rocess conditions. */
+pthread_cond_t cond;
+/* true when requesting the capture thread to exit */
+bool exit_thread;
+/* 0 if the capture thread was created successfully */
+int thread_status;
+
+
+#define DSP_OUTPUT_LATENCY_MS 0 /* Fudge factor for latency after capture point in audio DSP */
+
+/* Retry for delay for mixer open */
+#define RETRY_NUMBER 10
+#define RETRY_US 500000
+
+#define MIXER_CARD 0
+#define SOUND_CARD 0
+#define CAPTURE_DEVICE 8
+
+/* Proxy port supports only MMAP read and those fixed parameters*/
+#define AUDIO_CAPTURE_CHANNEL_COUNT 2
+#define AUDIO_CAPTURE_SMP_RATE 48000
+#define AUDIO_CAPTURE_PERIOD_SIZE (768)
+#define AUDIO_CAPTURE_PERIOD_COUNT 32
+
+struct pcm_config pcm_config_capture = {
+    .channels = AUDIO_CAPTURE_CHANNEL_COUNT,
+    .rate = AUDIO_CAPTURE_SMP_RATE,
+    .period_size = AUDIO_CAPTURE_PERIOD_SIZE,
+    .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
+    .format = PCM_FORMAT_S16_LE,
+    .start_threshold = AUDIO_CAPTURE_PERIOD_SIZE / 4,
+    .stop_threshold = INT_MAX,
+    .avail_min = AUDIO_CAPTURE_PERIOD_SIZE / 4,
+};
+
+
+/*
+ *  Local functions
+ */
+
+static void init_once() {
+    list_init(&created_effects_list);
+    list_init(&active_outputs_list);
+
+    pthread_mutex_init(&lock, NULL);
+    pthread_mutex_init(&thread_lock, NULL);
+    pthread_cond_init(&cond, NULL);
+    exit_thread = false;
+    thread_status = -1;
+
+    init_status = 0;
+}
+
+int lib_init() {
+    pthread_once(&once, init_once);
+    return init_status;
+}
+
+bool effect_exists(effect_context_t *context) {
+    struct listnode *node;
+
+    list_for_each(node, &created_effects_list) {
+        effect_context_t *fx_ctxt = node_to_item(node,
+                                                     effect_context_t,
+                                                     effects_list_node);
+        if (fx_ctxt == context) {
+            return true;
+        }
+    }
+    return false;
+}
+
+output_context_t *get_output(audio_io_handle_t output) {
+    struct listnode *node;
+
+    list_for_each(node, &active_outputs_list) {
+        output_context_t *out_ctxt = node_to_item(node,
+                                                  output_context_t,
+                                                  outputs_list_node);
+        if (out_ctxt->handle == output) {
+            return out_ctxt;
+        }
+    }
+    return NULL;
+}
+
+void add_effect_to_output(output_context_t * output, effect_context_t *context) {
+    struct listnode *fx_node;
+
+    list_for_each(fx_node, &output->effects_list) {
+        effect_context_t *fx_ctxt = node_to_item(fx_node,
+                                                     effect_context_t,
+                                                     output_node);
+        if (fx_ctxt == context)
+            return;
+    }
+    list_add_tail(&output->effects_list, &context->output_node);
+}
+
+void remove_effect_from_output(output_context_t * output, effect_context_t *context) {
+    struct listnode *fx_node;
+
+    list_for_each(fx_node, &output->effects_list) {
+        effect_context_t *fx_ctxt = node_to_item(fx_node,
+                                                     effect_context_t,
+                                                     output_node);
+        if (fx_ctxt == context) {
+            list_remove(&context->output_node);
+            return;
+        }
+    }
+}
+
+bool effects_enabled() {
+    struct listnode *out_node;
+
+    list_for_each(out_node, &active_outputs_list) {
+        struct listnode *fx_node;
+        output_context_t *out_ctxt = node_to_item(out_node,
+                                                  output_context_t,
+                                                  outputs_list_node);
+
+        list_for_each(fx_node, &out_ctxt->effects_list) {
+            effect_context_t *fx_ctxt = node_to_item(fx_node,
+                                                         effect_context_t,
+                                                         output_node);
+            if (fx_ctxt->state == EFFECT_STATE_ACTIVE)
+                return true;
+        }
+    }
+    return false;
+}
+
+int configure_proxy_capture(struct mixer *mixer, int value) {
+    const char *proxy_ctl_name = "AFE_PCM_RX Audio Mixer MultiMedia4";
+    struct mixer_ctl *ctl;
+
+    ctl = mixer_get_ctl_by_name(mixer, proxy_ctl_name);
+    if (ctl == NULL) {
+        ALOGW("%s: could not get %s ctl", __func__, proxy_ctl_name);
+        return -EINVAL;
+    }
+    if (mixer_ctl_set_value(ctl, 0, value) != 0)
+        ALOGW("%s: error setting value %d on %s ", __func__, value, proxy_ctl_name);
+
+    return 0;
+}
+
+
+void *capture_thread_loop(void *arg)
+{
+    int16_t data[AUDIO_CAPTURE_PERIOD_SIZE * AUDIO_CAPTURE_CHANNEL_COUNT * sizeof(int16_t)];
+    audio_buffer_t buf;
+    buf.frameCount = AUDIO_CAPTURE_PERIOD_SIZE;
+    buf.s16 = data;
+    bool capture_enabled = false;
+    struct mixer *mixer;
+    struct pcm *pcm = NULL;
+    int ret;
+    int retry_num = 0;
+
+    ALOGD("thread enter");
+
+    prctl(PR_SET_NAME, (unsigned long)"visualizer capture", 0, 0, 0);
+
+    pthread_mutex_lock(&lock);
+
+    mixer = mixer_open(MIXER_CARD);
+    while (mixer == NULL && retry_num < RETRY_NUMBER) {
+        usleep(RETRY_US);
+        mixer = mixer_open(MIXER_CARD);
+        retry_num++;
+    }
+    if (mixer == NULL) {
+        pthread_mutex_unlock(&lock);
+        return NULL;
+    }
+
+    for (;;) {
+        if (exit_thread) {
+            break;
+        }
+        if (effects_enabled()) {
+            if (!capture_enabled) {
+                ret = configure_proxy_capture(mixer, 1);
+                if (ret == 0) {
+                    pcm = pcm_open(SOUND_CARD, CAPTURE_DEVICE,
+                                   PCM_IN|PCM_MMAP|PCM_NOIRQ, &pcm_config_capture);
+                    if (pcm && !pcm_is_ready(pcm)) {
+                        ALOGW("%s: %s", __func__, pcm_get_error(pcm));
+                        pcm_close(pcm);
+                        pcm = NULL;
+                        configure_proxy_capture(mixer, 0);
+                    } else {
+                        capture_enabled = true;
+                        ALOGD("%s: capture ENABLED", __func__);
+                    }
+                }
+            }
+        } else {
+            if (capture_enabled) {
+                if (pcm != NULL)
+                    pcm_close(pcm);
+                configure_proxy_capture(mixer, 0);
+                ALOGD("%s: capture DISABLED", __func__);
+                capture_enabled = false;
+            }
+            pthread_cond_wait(&cond, &lock);
+        }
+        if (!capture_enabled)
+            continue;
+
+        pthread_mutex_unlock(&lock);
+        ret = pcm_mmap_read(pcm, data, sizeof(data));
+        pthread_mutex_lock(&lock);
+
+        if (ret == 0) {
+            struct listnode *out_node;
+
+            list_for_each(out_node, &active_outputs_list) {
+                output_context_t *out_ctxt = node_to_item(out_node,
+                                                          output_context_t,
+                                                          outputs_list_node);
+                struct listnode *fx_node;
+
+                list_for_each(fx_node, &out_ctxt->effects_list) {
+                    effect_context_t *fx_ctxt = node_to_item(fx_node,
+                                                                effect_context_t,
+                                                                output_node);
+                    fx_ctxt->ops.process(fx_ctxt, &buf, &buf);
+                }
+            }
+        } else {
+            ALOGW("%s: read status %d %s", __func__, ret, pcm_get_error(pcm));
+        }
+    }
+
+    if (capture_enabled) {
+        if (pcm != NULL)
+            pcm_close(pcm);
+        configure_proxy_capture(mixer, 0);
+    }
+    mixer_close(mixer);
+    pthread_mutex_unlock(&lock);
+
+    ALOGD("thread exit");
+
+    return NULL;
+}
+
+/*
+ * Interface from audio HAL
+ */
+
+__attribute__ ((visibility ("default")))
+int visualizer_hal_start_output(audio_io_handle_t output) {
+    int ret;
+    struct listnode *node;
+
+    ALOGV("%s", __func__);
+
+    if (lib_init() != 0)
+        return init_status;
+
+    pthread_mutex_lock(&thread_lock);
+    pthread_mutex_lock(&lock);
+    if (get_output(output) != NULL) {
+        ALOGW("%s output already started", __func__);
+        ret = -ENOSYS;
+        goto exit;
+    }
+
+    output_context_t *out_ctxt = (output_context_t *)malloc(sizeof(output_context_t));
+    out_ctxt->handle = output;
+    list_init(&out_ctxt->effects_list);
+
+    list_for_each(node, &created_effects_list) {
+        effect_context_t *fx_ctxt = node_to_item(node,
+                                                     effect_context_t,
+                                                     effects_list_node);
+        if (fx_ctxt->out_handle == output) {
+            list_add_tail(&out_ctxt->effects_list, &fx_ctxt->output_node);
+        }
+    }
+    if (list_empty(&active_outputs_list)) {
+        exit_thread = false;
+        thread_status = pthread_create(&capture_thread, (const pthread_attr_t *) NULL,
+                        capture_thread_loop, NULL);
+    }
+    list_add_tail(&active_outputs_list, &out_ctxt->outputs_list_node);
+    pthread_cond_signal(&cond);
+
+exit:
+    pthread_mutex_unlock(&lock);
+    pthread_mutex_unlock(&thread_lock);
+    return ret;
+}
+
+__attribute__ ((visibility ("default")))
+int visualizer_hal_stop_output(audio_io_handle_t output) {
+    int ret;
+    struct listnode *node;
+    output_context_t *out_ctxt;
+
+    ALOGV("%s", __func__);
+
+    if (lib_init() != 0)
+        return init_status;
+
+    pthread_mutex_lock(&thread_lock);
+    pthread_mutex_lock(&lock);
+
+    out_ctxt = get_output(output);
+    if (out_ctxt == NULL) {
+        ALOGW("%s output not started", __func__);
+        ret = -ENOSYS;
+        goto exit;
+    }
+
+    list_remove(&out_ctxt->outputs_list_node);
+    pthread_cond_signal(&cond);
+
+    if (list_empty(&active_outputs_list)) {
+        if (thread_status == 0) {
+            exit_thread = true;
+            pthread_cond_signal(&cond);
+            pthread_mutex_unlock(&lock);
+            pthread_join(capture_thread, (void **) NULL);
+            pthread_mutex_lock(&lock);
+            thread_status = -1;
+        }
+    }
+
+    free(out_ctxt);
+
+exit:
+    pthread_mutex_unlock(&lock);
+    pthread_mutex_unlock(&thread_lock);
+    return ret;
+}
+
+
+/*
+ * Effect operations
+ */
+
+int set_config(effect_context_t *context, effect_config_t *config)
+{
+    if (config->inputCfg.samplingRate != config->outputCfg.samplingRate) return -EINVAL;
+    if (config->inputCfg.channels != config->outputCfg.channels) return -EINVAL;
+    if (config->inputCfg.format != config->outputCfg.format) return -EINVAL;
+    if (config->inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) return -EINVAL;
+    if (config->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_WRITE &&
+            config->outputCfg.accessMode != EFFECT_BUFFER_ACCESS_ACCUMULATE) return -EINVAL;
+    if (config->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
+
+    context->config = *config;
+
+    if (context->ops.reset)
+        context->ops.reset(context);
+
+    return 0;
+}
+
+void get_config(effect_context_t *context, effect_config_t *config)
+{
+    *config = context->config;
+}
+
+
+/*
+ * Visualizer operations
+ */
+
+uint32_t visualizer_get_delta_time_ms_from_updated_time(visualizer_context_t* visu_ctxt) {
+    uint32_t delta_ms = 0;
+    if (visu_ctxt->buffer_update_time.tv_sec != 0) {
+        struct timespec ts;
+        if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
+            time_t secs = ts.tv_sec - visu_ctxt->buffer_update_time.tv_sec;
+            long nsec = ts.tv_nsec - visu_ctxt->buffer_update_time.tv_nsec;
+            if (nsec < 0) {
+                --secs;
+                nsec += 1000000000;
+            }
+            delta_ms = secs * 1000 + nsec / 1000000;
+        }
+    }
+    return delta_ms;
+}
+
+int visualizer_reset(effect_context_t *context)
+{
+    visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
+
+    visu_ctxt->capture_idx = 0;
+    visu_ctxt->last_capture_idx = 0;
+    visu_ctxt->buffer_update_time.tv_sec = 0;
+    visu_ctxt->latency = DSP_OUTPUT_LATENCY_MS;
+    memset(visu_ctxt->capture_buf, 0x80, CAPTURE_BUF_SIZE);
+    return 0;
+}
+
+int visualizer_init(effect_context_t *context)
+{
+    int32_t i;
+
+    visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
+
+    context->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
+    context->config.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
+    context->config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
+    context->config.inputCfg.samplingRate = 44100;
+    context->config.inputCfg.bufferProvider.getBuffer = NULL;
+    context->config.inputCfg.bufferProvider.releaseBuffer = NULL;
+    context->config.inputCfg.bufferProvider.cookie = NULL;
+    context->config.inputCfg.mask = EFFECT_CONFIG_ALL;
+    context->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
+    context->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
+    context->config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
+    context->config.outputCfg.samplingRate = 44100;
+    context->config.outputCfg.bufferProvider.getBuffer = NULL;
+    context->config.outputCfg.bufferProvider.releaseBuffer = NULL;
+    context->config.outputCfg.bufferProvider.cookie = NULL;
+    context->config.outputCfg.mask = EFFECT_CONFIG_ALL;
+
+    visu_ctxt->capture_size = VISUALIZER_CAPTURE_SIZE_MAX;
+    visu_ctxt->scaling_mode = VISUALIZER_SCALING_MODE_NORMALIZED;
+
+    // measurement initialization
+    visu_ctxt->channel_count = popcount(context->config.inputCfg.channels);
+    visu_ctxt->meas_mode = MEASUREMENT_MODE_NONE;
+    visu_ctxt->meas_wndw_size_in_buffers = MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS;
+    visu_ctxt->meas_buffer_idx = 0;
+    for (i=0 ; i<visu_ctxt->meas_wndw_size_in_buffers ; i++) {
+        visu_ctxt->past_meas[i].is_valid = false;
+        visu_ctxt->past_meas[i].peak_u16 = 0;
+        visu_ctxt->past_meas[i].rms_squared = 0;
+    }
+
+    set_config(context, &context->config);
+
+    return 0;
+}
+
+int visualizer_get_parameter(effect_context_t *context, effect_param_t *p, uint32_t *size)
+{
+    visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
+
+    p->status = 0;
+    *size = sizeof(effect_param_t) + sizeof(uint32_t);
+    if (p->psize != sizeof(uint32_t)) {
+        p->status = -EINVAL;
+        return 0;
+    }
+    switch (*(uint32_t *)p->data) {
+    case VISUALIZER_PARAM_CAPTURE_SIZE:
+        ALOGV("%s get capture_size = %d", __func__, visu_ctxt->capture_size);
+        *((uint32_t *)p->data + 1) = visu_ctxt->capture_size;
+        p->vsize = sizeof(uint32_t);
+        *size += sizeof(uint32_t);
+        break;
+    case VISUALIZER_PARAM_SCALING_MODE:
+        ALOGV("%s get scaling_mode = %d", __func__, visu_ctxt->scaling_mode);
+        *((uint32_t *)p->data + 1) = visu_ctxt->scaling_mode;
+        p->vsize = sizeof(uint32_t);
+        *size += sizeof(uint32_t);
+        break;
+    case VISUALIZER_PARAM_MEASUREMENT_MODE:
+        ALOGV("%s get meas_mode = %d", __func__, visu_ctxt->meas_mode);
+        *((uint32_t *)p->data + 1) = visu_ctxt->meas_mode;
+        p->vsize = sizeof(uint32_t);
+        *size += sizeof(uint32_t);
+        break;
+    default:
+        p->status = -EINVAL;
+    }
+    return 0;
+}
+
+int visualizer_set_parameter(effect_context_t *context, effect_param_t *p, uint32_t size)
+{
+    visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
+
+    if (p->psize != sizeof(uint32_t) || p->vsize != sizeof(uint32_t))
+        return -EINVAL;
+
+    switch (*(uint32_t *)p->data) {
+    case VISUALIZER_PARAM_CAPTURE_SIZE:
+        visu_ctxt->capture_size = *((uint32_t *)p->data + 1);
+        ALOGV("%s set capture_size = %d", __func__, visu_ctxt->capture_size);
+        break;
+    case VISUALIZER_PARAM_SCALING_MODE:
+        visu_ctxt->scaling_mode = *((uint32_t *)p->data + 1);
+        ALOGV("%s set scaling_mode = %d", __func__, visu_ctxt->scaling_mode);
+        break;
+    case VISUALIZER_PARAM_LATENCY:
+        /* Ignore latency as we capture at DSP output
+         * visu_ctxt->latency = *((uint32_t *)p->data + 1); */
+        ALOGV("%s set latency = %d", __func__, visu_ctxt->latency);
+        break;
+    case VISUALIZER_PARAM_MEASUREMENT_MODE:
+        visu_ctxt->meas_mode = *((uint32_t *)p->data + 1);
+        ALOGV("%s set meas_mode = %d", __func__, visu_ctxt->meas_mode);
+        break;
+    default:
+        return -EINVAL;
+    }
+    return 0;
+}
+
+/* Real process function called from capture thread. Called with lock held */
+int visualizer_process(effect_context_t *context,
+                       audio_buffer_t *inBuffer,
+                       audio_buffer_t *outBuffer)
+{
+    visualizer_context_t *visu_ctxt = (visualizer_context_t *)context;
+
+    if (!effect_exists(context))
+        return -EINVAL;
+
+    if (inBuffer == NULL || inBuffer->raw == NULL ||
+        outBuffer == NULL || outBuffer->raw == NULL ||
+        inBuffer->frameCount != outBuffer->frameCount ||
+        inBuffer->frameCount == 0) {
+        return -EINVAL;
+    }
+
+    // perform measurements if needed
+    if (visu_ctxt->meas_mode & MEASUREMENT_MODE_PEAK_RMS) {
+        // find the peak and RMS squared for the new buffer
+        uint32_t inIdx;
+        int16_t max_sample = 0;
+        float rms_squared_acc = 0;
+        for (inIdx = 0 ; inIdx < inBuffer->frameCount * visu_ctxt->channel_count ; inIdx++) {
+            if (inBuffer->s16[inIdx] > max_sample) {
+                max_sample = inBuffer->s16[inIdx];
+            } else if (-inBuffer->s16[inIdx] > max_sample) {
+                max_sample = -inBuffer->s16[inIdx];
+            }
+            rms_squared_acc += (inBuffer->s16[inIdx] * inBuffer->s16[inIdx]);
+        }
+        // store the measurement
+        visu_ctxt->past_meas[visu_ctxt->meas_buffer_idx].peak_u16 = (uint16_t)max_sample;
+        visu_ctxt->past_meas[visu_ctxt->meas_buffer_idx].rms_squared =
+                rms_squared_acc / (inBuffer->frameCount * visu_ctxt->channel_count);
+        visu_ctxt->past_meas[visu_ctxt->meas_buffer_idx].is_valid = true;
+        if (++visu_ctxt->meas_buffer_idx >= visu_ctxt->meas_wndw_size_in_buffers) {
+            visu_ctxt->meas_buffer_idx = 0;
+        }
+    }
+
+    /* all code below assumes stereo 16 bit PCM output and input */
+    int32_t shift;
+
+    if (visu_ctxt->scaling_mode == VISUALIZER_SCALING_MODE_NORMALIZED) {
+        /* derive capture scaling factor from peak value in current buffer
+         * this gives more interesting captures for display. */
+        shift = 32;
+        int len = inBuffer->frameCount * 2;
+        int i;
+        for (i = 0; i < len; i++) {
+            int32_t smp = inBuffer->s16[i];
+            if (smp < 0) smp = -smp - 1; /* take care to keep the max negative in range */
+            int32_t clz = __builtin_clz(smp);
+            if (shift > clz) shift = clz;
+        }
+        /* A maximum amplitude signal will have 17 leading zeros, which we want to
+         * translate to a shift of 8 (for converting 16 bit to 8 bit) */
+        shift = 25 - shift;
+        /* Never scale by less than 8 to avoid returning unaltered PCM signal. */
+        if (shift < 3) {
+            shift = 3;
+        }
+        /* add one to combine the division by 2 needed after summing
+         * left and right channels below */
+        shift++;
+    } else {
+        assert(visu_ctxt->scaling_mode == VISUALIZER_SCALING_MODE_AS_PLAYED);
+        shift = 9;
+    }
+
+    uint32_t capt_idx;
+    uint32_t in_idx;
+    uint8_t *buf = visu_ctxt->capture_buf;
+    for (in_idx = 0, capt_idx = visu_ctxt->capture_idx;
+         in_idx < inBuffer->frameCount;
+         in_idx++, capt_idx++) {
+        if (capt_idx >= CAPTURE_BUF_SIZE) {
+            /* wrap around */
+            capt_idx = 0;
+        }
+        int32_t smp = inBuffer->s16[2 * in_idx] + inBuffer->s16[2 * in_idx + 1];
+        smp = smp >> shift;
+        buf[capt_idx] = ((uint8_t)smp)^0x80;
+    }
+
+    /* XXX the following two should really be atomic, though it probably doesn't
+     * matter much for visualization purposes */
+    visu_ctxt->capture_idx = capt_idx;
+    /* update last buffer update time stamp */
+    if (clock_gettime(CLOCK_MONOTONIC, &visu_ctxt->buffer_update_time) < 0) {
+        visu_ctxt->buffer_update_time.tv_sec = 0;
+    }
+
+    if (context->state != EFFECT_STATE_ACTIVE) {
+        ALOGV("%s DONE inactive", __func__);
+        return -ENODATA;
+    }
+
+    return 0;
+}
+
+int visualizer_command(effect_context_t * context, uint32_t cmdCode, uint32_t cmdSize,
+        void *pCmdData, uint32_t *replySize, void *pReplyData)
+{
+    visualizer_context_t * visu_ctxt = (visualizer_context_t *)context;
+
+    switch (cmdCode) {
+    case VISUALIZER_CMD_CAPTURE:
+        if (pReplyData == NULL || *replySize != visu_ctxt->capture_size) {
+            ALOGV("%s VISUALIZER_CMD_CAPTURE error *replySize %d context->capture_size %d",
+                  __func__, *replySize, visu_ctxt->capture_size);
+            return -EINVAL;
+        }
+
+        if (!context->offload_enabled)
+            break;
+
+        if (context->state == EFFECT_STATE_ACTIVE) {
+            int32_t latency_ms = visu_ctxt->latency;
+            const uint32_t delta_ms = visualizer_get_delta_time_ms_from_updated_time(visu_ctxt);
+            latency_ms -= delta_ms;
+            if (latency_ms < 0) {
+                latency_ms = 0;
+            }
+            const uint32_t delta_smp = context->config.inputCfg.samplingRate * latency_ms / 1000;
+
+            int32_t capture_point = visu_ctxt->capture_idx - visu_ctxt->capture_size - delta_smp;
+            int32_t capture_size = visu_ctxt->capture_size;
+            if (capture_point < 0) {
+                int32_t size = -capture_point;
+                if (size > capture_size)
+                    size = capture_size;
+
+                memcpy(pReplyData,
+                       visu_ctxt->capture_buf + CAPTURE_BUF_SIZE + capture_point,
+                       size);
+                pReplyData = (void *)((size_t)pReplyData + size);
+                capture_size -= size;
+                capture_point = 0;
+            }
+            memcpy(pReplyData,
+                   visu_ctxt->capture_buf + capture_point,
+                   capture_size);
+
+
+            /* if audio framework has stopped playing audio although the effect is still
+             * active we must clear the capture buffer to return silence */
+            if ((visu_ctxt->last_capture_idx == visu_ctxt->capture_idx) &&
+                    (visu_ctxt->buffer_update_time.tv_sec != 0)) {
+                if (delta_ms > MAX_STALL_TIME_MS) {
+                    ALOGV("%s capture going to idle", __func__);
+                    visu_ctxt->buffer_update_time.tv_sec = 0;
+                    memset(pReplyData, 0x80, visu_ctxt->capture_size);
+                }
+            }
+            visu_ctxt->last_capture_idx = visu_ctxt->capture_idx;
+        } else {
+            memset(pReplyData, 0x80, visu_ctxt->capture_size);
+        }
+        break;
+
+    case VISUALIZER_CMD_MEASURE: {
+        uint16_t peak_u16 = 0;
+        float sum_rms_squared = 0.0f;
+        uint8_t nb_valid_meas = 0;
+        /* reset measurements if last measurement was too long ago (which implies stored
+         * measurements aren't relevant anymore and shouldn't bias the new one) */
+        const int32_t delay_ms = visualizer_get_delta_time_ms_from_updated_time(visu_ctxt);
+        if (delay_ms > DISCARD_MEASUREMENTS_TIME_MS) {
+            uint32_t i;
+            ALOGV("Discarding measurements, last measurement is %dms old", delay_ms);
+            for (i=0 ; i<visu_ctxt->meas_wndw_size_in_buffers ; i++) {
+                visu_ctxt->past_meas[i].is_valid = false;
+                visu_ctxt->past_meas[i].peak_u16 = 0;
+                visu_ctxt->past_meas[i].rms_squared = 0;
+            }
+            visu_ctxt->meas_buffer_idx = 0;
+        } else {
+            /* only use actual measurements, otherwise the first RMS measure happening before
+             * MEASUREMENT_WINDOW_MAX_SIZE_IN_BUFFERS have been played will always be artificially
+             * low */
+            uint32_t i;
+            for (i=0 ; i < visu_ctxt->meas_wndw_size_in_buffers ; i++) {
+                if (visu_ctxt->past_meas[i].is_valid) {
+                    if (visu_ctxt->past_meas[i].peak_u16 > peak_u16) {
+                        peak_u16 = visu_ctxt->past_meas[i].peak_u16;
+                    }
+                    sum_rms_squared += visu_ctxt->past_meas[i].rms_squared;
+                    nb_valid_meas++;
+                }
+            }
+        }
+        float rms = nb_valid_meas == 0 ? 0.0f : sqrtf(sum_rms_squared / nb_valid_meas);
+        int32_t* p_int_reply_data = (int32_t*)pReplyData;
+        /* convert from I16 sample values to mB and write results */
+        if (rms < 0.000016f) {
+            p_int_reply_data[MEASUREMENT_IDX_RMS] = -9600; //-96dB
+        } else {
+            p_int_reply_data[MEASUREMENT_IDX_RMS] = (int32_t) (2000 * log10(rms / 32767.0f));
+        }
+        if (peak_u16 == 0) {
+            p_int_reply_data[MEASUREMENT_IDX_PEAK] = -9600; //-96dB
+        } else {
+            p_int_reply_data[MEASUREMENT_IDX_PEAK] = (int32_t) (2000 * log10(peak_u16 / 32767.0f));
+        }
+        ALOGV("VISUALIZER_CMD_MEASURE peak=%d (%dmB), rms=%.1f (%dmB)",
+                peak_u16, p_int_reply_data[MEASUREMENT_IDX_PEAK],
+                rms, p_int_reply_data[MEASUREMENT_IDX_RMS]);
+        }
+        break;
+
+    default:
+        ALOGW("%s invalid command %d", __func__, cmdCode);
+        return -EINVAL;
+    }
+    return 0;
+}
+
+
+/*
+ * Effect Library Interface Implementation
+ */
+
+int effect_lib_create(const effect_uuid_t *uuid,
+                         int32_t sessionId,
+                         int32_t ioId,
+                         effect_handle_t *pHandle) {
+    int ret;
+    int i;
+
+    if (lib_init() != 0)
+        return init_status;
+
+    if (pHandle == NULL || uuid == NULL)
+        return -EINVAL;
+
+    for (i = 0; descriptors[i] != NULL; i++) {
+        if (memcmp(uuid, &descriptors[i]->uuid, sizeof(effect_uuid_t)) == 0)
+            break;
+    }
+
+    if (descriptors[i] == NULL)
+        return -EINVAL;
+
+    effect_context_t *context;
+    if (memcmp(uuid, &visualizer_descriptor.uuid, sizeof(effect_uuid_t)) == 0) {
+        visualizer_context_t *visu_ctxt = (visualizer_context_t *)calloc(1,
+                                                                     sizeof(visualizer_context_t));
+        context = (effect_context_t *)visu_ctxt;
+        context->ops.init = visualizer_init;
+        context->ops.reset = visualizer_reset;
+        context->ops.process = visualizer_process;
+        context->ops.set_parameter = visualizer_set_parameter;
+        context->ops.get_parameter = visualizer_get_parameter;
+        context->ops.command = visualizer_command;
+    } else {
+        return -EINVAL;
+    }
+
+    context->itfe = &effect_interface;
+    context->state = EFFECT_STATE_UNINITIALIZED;
+    context->out_handle = (audio_io_handle_t)ioId;
+    context->desc = &visualizer_descriptor;
+
+    ret = context->ops.init(context);
+    if (ret < 0) {
+        ALOGW("%s init failed", __func__);
+        free(context);
+        return ret;
+    }
+
+    context->state = EFFECT_STATE_INITIALIZED;
+
+    pthread_mutex_lock(&lock);
+    list_add_tail(&created_effects_list, &context->effects_list_node);
+    output_context_t *out_ctxt = get_output(ioId);
+    if (out_ctxt != NULL)
+        add_effect_to_output(out_ctxt, context);
+    pthread_mutex_unlock(&lock);
+
+    *pHandle = (effect_handle_t)context;
+
+    ALOGV("%s created context %p", __func__, context);
+
+    return 0;
+
+}
+
+int effect_lib_release(effect_handle_t handle) {
+    effect_context_t *context = (effect_context_t *)handle;
+    int status;
+
+    if (lib_init() != 0)
+        return init_status;
+
+    ALOGV("%s context %p", __func__, handle);
+    pthread_mutex_lock(&lock);
+    status = -EINVAL;
+    if (effect_exists(context)) {
+        output_context_t *out_ctxt = get_output(context->out_handle);
+        if (out_ctxt != NULL)
+            remove_effect_from_output(out_ctxt, context);
+        list_remove(&context->effects_list_node);
+        if (context->ops.release)
+            context->ops.release(context);
+        free(context);
+        status = 0;
+    }
+    pthread_mutex_unlock(&lock);
+
+    return status;
+}
+
+int effect_lib_get_descriptor(const effect_uuid_t *uuid,
+                                effect_descriptor_t *descriptor) {
+    int i;
+
+    if (lib_init() != 0)
+        return init_status;
+
+    if (descriptor == NULL || uuid == NULL) {
+        ALOGV("%s called with NULL pointer", __func__);
+        return -EINVAL;
+    }
+
+    for (i = 0; descriptors[i] != NULL; i++) {
+        if (memcmp(uuid, &descriptors[i]->uuid, sizeof(effect_uuid_t)) == 0) {
+            *descriptor = *descriptors[i];
+            return 0;
+        }
+    }
+
+    return  -EINVAL;
+}
+
+/*
+ * Effect Control Interface Implementation
+ */
+
+ /* Stub function for effect interface: never called for offloaded effects */
+int effect_process(effect_handle_t self,
+                       audio_buffer_t *inBuffer,
+                       audio_buffer_t *outBuffer)
+{
+    effect_context_t * context = (effect_context_t *)self;
+    int status = 0;
+
+    ALOGW("%s Called ?????", __func__);
+
+    pthread_mutex_lock(&lock);
+    if (!effect_exists(context)) {
+        status = -EINVAL;
+        goto exit;
+    }
+
+    if (context->state != EFFECT_STATE_ACTIVE) {
+        status = -EINVAL;
+        goto exit;
+    }
+
+exit:
+    pthread_mutex_unlock(&lock);
+    return status;
+}
+
+int effect_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
+        void *pCmdData, uint32_t *replySize, void *pReplyData)
+{
+
+    effect_context_t * context = (effect_context_t *)self;
+    int retsize;
+    int status = 0;
+
+    pthread_mutex_lock(&lock);
+
+    if (!effect_exists(context)) {
+        status = -EINVAL;
+        goto exit;
+    }
+
+    if (context == NULL || context->state == EFFECT_STATE_UNINITIALIZED) {
+        status = -EINVAL;
+        goto exit;
+    }
+
+//    ALOGV_IF(cmdCode != VISUALIZER_CMD_CAPTURE,
+//             "%s command %d cmdSize %d", __func__, cmdCode, cmdSize);
+
+    switch (cmdCode) {
+    case EFFECT_CMD_INIT:
+        if (pReplyData == NULL || *replySize != sizeof(int)) {
+            status = -EINVAL;
+            goto exit;
+        }
+        if (context->ops.init)
+            *(int *) pReplyData = context->ops.init(context);
+        else
+            *(int *) pReplyData = 0;
+        break;
+    case EFFECT_CMD_SET_CONFIG:
+        if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
+                || pReplyData == NULL || *replySize != sizeof(int)) {
+            status = -EINVAL;
+            goto exit;
+        }
+        *(int *) pReplyData = set_config(context, (effect_config_t *) pCmdData);
+        break;
+    case EFFECT_CMD_GET_CONFIG:
+        if (pReplyData == NULL ||
+            *replySize != sizeof(effect_config_t)) {
+            status = -EINVAL;
+            goto exit;
+        }
+        if (!context->offload_enabled) {
+            status = -EINVAL;
+            goto exit;
+        }
+
+        get_config(context, (effect_config_t *)pReplyData);
+        break;
+    case EFFECT_CMD_RESET:
+        if (context->ops.reset)
+            context->ops.reset(context);
+        break;
+    case EFFECT_CMD_ENABLE:
+        if (pReplyData == NULL || *replySize != sizeof(int)) {
+            status = -EINVAL;
+            goto exit;
+        }
+        if (context->state != EFFECT_STATE_INITIALIZED) {
+            status = -ENOSYS;
+            goto exit;
+        }
+        context->state = EFFECT_STATE_ACTIVE;
+        if (context->ops.enable)
+            context->ops.enable(context);
+        pthread_cond_signal(&cond);
+        ALOGV("%s EFFECT_CMD_ENABLE", __func__);
+        *(int *)pReplyData = 0;
+        break;
+    case EFFECT_CMD_DISABLE:
+        if (pReplyData == NULL || *replySize != sizeof(int)) {
+            status = -EINVAL;
+            goto exit;
+        }
+        if (context->state != EFFECT_STATE_ACTIVE) {
+            status = -ENOSYS;
+            goto exit;
+        }
+        context->state = EFFECT_STATE_INITIALIZED;
+        if (context->ops.disable)
+            context->ops.disable(context);
+        pthread_cond_signal(&cond);
+        ALOGV("%s EFFECT_CMD_DISABLE", __func__);
+        *(int *)pReplyData = 0;
+        break;
+    case EFFECT_CMD_GET_PARAM: {
+        if (pCmdData == NULL ||
+            cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t)) ||
+            pReplyData == NULL ||
+            *replySize < (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t))) {
+            status = -EINVAL;
+            goto exit;
+        }
+        if (!context->offload_enabled) {
+            status = -EINVAL;
+            goto exit;
+        }
+        memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(uint32_t));
+        effect_param_t *p = (effect_param_t *)pReplyData;
+        if (context->ops.get_parameter)
+            context->ops.get_parameter(context, p, replySize);
+        } break;
+    case EFFECT_CMD_SET_PARAM: {
+        if (pCmdData == NULL ||
+            cmdSize != (int)(sizeof(effect_param_t) + sizeof(uint32_t) + sizeof(uint32_t)) ||
+            pReplyData == NULL || *replySize != sizeof(int32_t)) {
+            status = -EINVAL;
+            goto exit;
+        }
+        *(int32_t *)pReplyData = 0;
+        effect_param_t *p = (effect_param_t *)pCmdData;
+        if (context->ops.set_parameter)
+            *(int32_t *)pReplyData = context->ops.set_parameter(context, p, *replySize);
+
+        } break;
+    case EFFECT_CMD_SET_DEVICE:
+    case EFFECT_CMD_SET_VOLUME:
+    case EFFECT_CMD_SET_AUDIO_MODE:
+        break;
+
+    case EFFECT_CMD_OFFLOAD: {
+        output_context_t *out_ctxt;
+
+        if (cmdSize != sizeof(effect_offload_param_t) || pCmdData == NULL
+                || pReplyData == NULL || *replySize != sizeof(int)) {
+            ALOGV("%s EFFECT_CMD_OFFLOAD bad format", __func__);
+            status = -EINVAL;
+            break;
+        }
+
+        effect_offload_param_t* offload_param = (effect_offload_param_t*)pCmdData;
+
+        ALOGV("%s EFFECT_CMD_OFFLOAD offload %d output %d",
+              __func__, offload_param->isOffload, offload_param->ioHandle);
+
+        *(int *)pReplyData = 0;
+
+        context->offload_enabled = offload_param->isOffload;
+        if (context->out_handle == offload_param->ioHandle)
+            break;
+
+        out_ctxt = get_output(context->out_handle);
+        if (out_ctxt != NULL)
+            remove_effect_from_output(out_ctxt, context);
+        out_ctxt = get_output(offload_param->ioHandle);
+        if (out_ctxt != NULL)
+            add_effect_to_output(out_ctxt, context);
+
+        context->out_handle = offload_param->ioHandle;
+
+        } break;
+
+
+    default:
+        if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY && context->ops.command)
+            status = context->ops.command(context, cmdCode, cmdSize,
+                                          pCmdData, replySize, pReplyData);
+        else {
+            ALOGW("%s invalid command %d", __func__, cmdCode);
+            status = -EINVAL;
+        }
+        break;
+    }
+
+exit:
+    pthread_mutex_unlock(&lock);
+
+//    ALOGV_IF(cmdCode != VISUALIZER_CMD_CAPTURE,"%s DONE", __func__);
+    return status;
+}
+
+/* Effect Control Interface Implementation: get_descriptor */
+int effect_get_descriptor(effect_handle_t   self,
+                                    effect_descriptor_t *descriptor)
+{
+    effect_context_t *context = (effect_context_t *)self;
+
+    if (!effect_exists(context))
+        return -EINVAL;
+
+    if (descriptor == NULL)
+        return -EINVAL;
+
+    *descriptor = *context->desc;
+
+    return 0;
+}
+
+/* effect_handle_t interface implementation for visualizer effect */
+const struct effect_interface_s effect_interface = {
+        effect_process,
+        effect_command,
+        effect_get_descriptor,
+        NULL,
+};
+
+__attribute__ ((visibility ("default")))
+audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
+    tag : AUDIO_EFFECT_LIBRARY_TAG,
+    version : EFFECT_LIBRARY_API_VERSION,
+    name : "Visualizer Library",
+    implementor : "The Android Open Source Project",
+    create_effect : effect_lib_create,
+    release_effect : effect_lib_release,
+    get_descriptor : effect_lib_get_descriptor,
+};