hal: Upstream offload fixes

hal: Fix backend configuration during stream switch

-Currently, input stream sample rate and/or bit width is compared
with current backend sample rate and/or bit width. Backend is
re-configured only if input stream's rate/width is higher. This
causes incorrect sampling rate during stream switch of varying
sample rates.
-The backend is configured at highest bit width and/or sample rate
amongst all active playback usecases.
-By setting the backend re-configuration flag only when the selected
sample rate and/or bit width differ with current backend sample rate
and/or bit width, re-routing is enabled and backend is re-configured.
-Exception: 16 bit playbacks is allowed through 16/48 backend only.
-Enable logs for app type, acdb id and sample rate.

Change-Id: I09bfe30ebf8bdb3f9b481d7375ce7450fa67270c

hal: allow to get render position for non-offload use cases

For pcm voip use case which goes with direct output, render
position can be queried from HAL, while currently it is not
supported and always return with invalid status.

Return with written frames for pcm use cases when render
position queried from HAL.

CRs-Fixed: 828864

Change-Id: I5fec7f5fc1f61dc36cbeb41a4121c2c83f63b7f4

hal: Fix backend configuration for 24 bit offload playback

Backend sample rate configuration follows:
16 bit playback - 48khz for streams at any valid sample rate
24 bit playback - 48khz for stream sample rate upto 48khz
24 bit playback - 96khz for sample rate range of 48khz to 96khz
24 bit playback - 192khz for sample rate range of 96khz to 192 khz
For invalid or out of range sample rate, default rate is 48K
Upper limit is inclusive in the sample rate range

Change-Id: I48ace6d9135b1e0a450bcc5cc39498da619115a5

hal: Fix backend for 24 bit playback on speaker

Configure backend at 48khz for 24 bit playback on speakers.
Currently, ADM can be configured only at the begining of stream
playback. During device switch, in the middle of a playback, the
backend remains at the previous configuration.
By allowing ADM and backend re-configuration during playback, 24-bit
playback is enforced on appropriate backend.

Change-Id: I6a6df5699972b0992a217fb2a4c9dc94fd2230ce

hal: Fix combo device configuration for 24 bit playback on speaker

-Fix combo device configuration for 24 bit playback on speaker
-Move exception hadndling code to audio_extn

Change-Id: Ia0985a284042a5ac5e3de64aaf5e4d57462ceb85

hal: fix voice call mute issue

The backend is always configured with default bit width and sample
rate for voice calls. The backend reconfiguration should not be done
when a new stream starts during voice call.

Change-Id: I9fda27c4fb1925b3ddeec929ad986886dc4d695e
CRs-fixed: 747001

hal: expose offload functions only for offload playback

- Expose compress offload related functions only when
  AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD is set.
- Do not use them for direct PCM format to avoid unnecessary
  calls to compress driver.

CRs-Fixed: 913925
Change-Id: I8f93b1c697794e686d19c6c78823a095333b5369

hal: set output bitwidth to 24 bit for 32 bit data.

 - parser gives 32 bit data for 24 bit AIFF playback and same
   is sent down to HAL.
 - So update bit width to 24 bit if offload info bit width is 32 bit
   to configure back end to 24 bit for 24 bit e2e playback.

Change-Id: I17cf4430b9d401c42817b75c4456617bdda78647

hal: Increase PCM offload buffering

Increase PCM offload buffering to 40ms

CRs-Fixed: 924443
Change-Id: I67be148f85acc7c155ed222d652d5fedfb6a5e9b

hal: Cleanup dead code

Change-Id: I21cc1a85cbad67bf67d13b72ca2bdaa5069a8f89
diff --git a/hal/Android.mk b/hal/Android.mk
index d65ab1f..35158f9 100644
--- a/hal/Android.mk
+++ b/hal/Android.mk
@@ -99,7 +99,7 @@
 endif
 
 ifneq ($(strip $(AUDIO_FEATURE_ENABLED_EXTN_FORMATS)),false)
-LOCAL_CFLAGS += -DFORMATS_ENABLED
+LOCAL_CFLAGS += -DAUDIO_EXTN_FORMATS_ENABLED
 endif
 
 ifneq ($(filter apq8084 msm8974,$(TARGET_BOARD_PLATFORM)),)
diff --git a/hal/audio_extn/audio_extn.h b/hal/audio_extn/audio_extn.h
index ce58f44..1c4aa55 100644
--- a/hal/audio_extn/audio_extn.h
+++ b/hal/audio_extn/audio_extn.h
@@ -49,6 +49,13 @@
 #endif
 
 
+#ifdef AUDIO_EXTN_FORMATS_ENABLED
+#define AUDIO_OUTPUT_BIT_WIDTH ((config->offload_info.bit_width == 32) ? 24\
+                                   :config->offload_info.bit_width)
+#else
+#define AUDIO_OUTPUT_BIT_WIDTH (CODEC_BACKEND_DEFAULT_BIT_WIDTH)
+#endif
+
 #define MAX_LENGTH_MIXER_CONTROL_IN_INT                  (128)
 
 void audio_extn_set_parameters(struct audio_device *adev,
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 04e2d42..f155124 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -65,8 +65,10 @@
 
 #ifdef LOW_LATENCY_PRIMARY
 #define USECASE_AUDIO_PLAYBACK_PRIMARY USECASE_AUDIO_PLAYBACK_LOW_LATENCY
+#define PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY pcm_config_low_latency
 #else
 #define USECASE_AUDIO_PLAYBACK_PRIMARY USECASE_AUDIO_PLAYBACK_DEEP_BUFFER
+#define PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY pcm_config_deep_buffer
 #endif
 
 static unsigned int configured_low_latency_capture_period_size =
@@ -2185,9 +2187,13 @@
                                    uint32_t *dsp_frames)
 {
     struct stream_out *out = (struct stream_out *)stream;
-    if (is_offload_usecase(out->usecase) && (dsp_frames != NULL)) {
-        ssize_t ret =  0;
-        *dsp_frames = 0;
+
+    if (dsp_frames == NULL)
+        return -EINVAL;
+
+    *dsp_frames = 0;
+    if (is_offload_usecase(out->usecase)) {
+        ssize_t ret = 0;
         lock_output_stream(out);
         if (out->compr != NULL) {
             ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
@@ -2203,14 +2209,14 @@
             set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
             return -EINVAL;
         } else if(ret < 0) {
-            if (out->compr == NULL) {
-                return 0;
-            }
             ALOGE(" ERROR: Unable to get time stamp from compress driver ret=%d", ret);
             return -EINVAL;
         } else {
             return 0;
         }
+    } else if (audio_is_linear_pcm(out->format)) {
+        *dsp_frames = out->written;
+        return 0;
     } else
         return -EINVAL;
 }
@@ -2689,7 +2695,6 @@
     struct audio_device *adev = (struct audio_device *)dev;
     struct stream_out *out;
     int i, ret = 0;
-    int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
 
     *stream_out = NULL;
 
@@ -2801,6 +2806,12 @@
         } else {
             ALOGV("%s:: inserting OFFLOAD_USECASE", __func__);
             out->usecase = get_offload_usecase(adev);
+
+            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;
         }
         if (config->offload_info.channel_mask)
             out->channel_mask = config->offload_info.channel_mask;
@@ -2811,11 +2822,6 @@
         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->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
 
         if (audio_extn_is_dolby_format(config->offload_info.format))
@@ -2846,7 +2852,9 @@
         out->compr_config.codec->ch_in =
                     audio_channel_count_from_out_mask(config->channel_mask);
         out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
-        out->bit_width = config->offload_info.bit_width;
+        out->bit_width = AUDIO_OUTPUT_BIT_WIDTH;
+        /*TODO: Do we need to change it for passthrough */
+        out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
 
         if ((config->offload_info.format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC)
             out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
@@ -2862,7 +2870,7 @@
 
 #ifdef FLAC_OFFLOAD_ENABLED
         if (config->offload_info.format == AUDIO_FORMAT_FLAC)
-            out->compr_config.codec->options.flac_dec.sample_size = config->offload_info.bit_width;
+            out->compr_config.codec->options.flac_dec.sample_size = AUDIO_OUTPUT_BIT_WIDTH;
 #endif
 
         if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
@@ -2922,28 +2930,21 @@
         out->config = pcm_config_afe_proxy_playback;
         adev->voice_tx_output = out;
     } else {
-#ifndef LOW_LATENCY_PRIMARY
-        if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
-            out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
-            out->config = pcm_config_low_latency;
-#endif
-#ifdef LOW_LATENCY_PRIMARY
-        if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
-            out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
-            out->config = pcm_config_deep_buffer;
-#endif
-        } else if (out->flags & AUDIO_OUTPUT_FLAG_RAW) {
+        if (out->flags & AUDIO_OUTPUT_FLAG_RAW) {
             out->usecase = USECASE_AUDIO_PLAYBACK_ULL;
             out->config = pcm_config_low_latency;
+        } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
+            out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
+            out->config = pcm_config_low_latency;
+        } if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
+            out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
+            out->config = pcm_config_deep_buffer;
         } else {
             /* primary path is the default path selected if no other outputs are available/suitable */
             out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
-#ifdef LOW_LATENCY_PRIMARY
-            out->config = pcm_config_low_latency;
-#else
-            out->config = pcm_config_deep_buffer;
-#endif
+            out->config = PCM_CONFIG_AUDIO_PLAYBACK_PRIMARY;
         }
+
         if (config->format != audio_format_from_pcm_format(out->config.format)) {
             if (k_enable_extended_precision
                     && pcm_params_format_test(adev->use_case_table[out->usecase],
@@ -2964,8 +2965,8 @@
         out->sample_rate = out->config.rate;
     }
 
-    ALOGV("%s flags %x, format %x, sample_rate %d, out->bit_width %d",
-           __func__, flags, out->format, out->sample_rate, out->bit_width);
+    ALOGV("%s devices %d,flags %x, format %x, out->sample_rate %d, out->bit_width %d",
+           __func__, devices, flags, out->format, out->sample_rate, out->bit_width);
 
     if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
             flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index 39c2543..4dff42d 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -59,7 +59,7 @@
  * 24 - lcm of channels supported by DSP
  */
 #define MAX_PCM_OFFLOAD_FRAGMENT_SIZE (240 * 1024)
-#define MIN_PCM_OFFLOAD_FRAGMENT_SIZE 512
+#define MIN_PCM_OFFLOAD_FRAGMENT_SIZE (4 * 1024)
 
 #define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
 #define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
@@ -2343,25 +2343,6 @@
     return ret;
 }
 
-static unsigned int get_best_backend_sample_rate(unsigned int sample_rate) {
-
-    // codec backend can take 48K, 96K, and 192K
-    if (sample_rate <= 48000)
-        return 48000;
-    if (sample_rate <= 96000)
-        return 96000;
-    if (sample_rate <= 192000)
-        return 192000;
-    return CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
-}
-
-static unsigned int get_best_backend_bit_width(unsigned int bit_width) {
-
-    if (bit_width == 24)
-        return 24;
-    return CODEC_BACKEND_DEFAULT_BIT_WIDTH;
-}
-
 bool platform_use_small_buffer(audio_offload_info_t* info)
 {
     return OFFLOAD_USE_SMALL_BUFFER;
@@ -2402,9 +2383,7 @@
      * Upper limit is inclusive in the sample rate range.
      */
     // TODO: This has to be more dynamic based on policy file
-    if ((adev->cur_codec_backend_bit_width == CODEC_BACKEND_DEFAULT_BIT_WIDTH &&
-             adev->cur_codec_backend_samplerate != CODEC_BACKEND_DEFAULT_SAMPLE_RATE) ||
-        (adev->cur_codec_backend_samplerate != sample_rate)) {
+    if (sample_rate != adev->cur_codec_backend_samplerate) {
             char *rate_str = NULL;
             const char * mixer_ctl_name = "SLIM_0_RX SampleRate";
             struct  mixer_ctl *ctl;
@@ -2456,57 +2435,56 @@
     bool backend_change = false;
     struct listnode *node;
     struct stream_out *out = NULL;
-    unsigned int cur_sr, cur_bw, best_bw = 0, best_sr = 0;
+    unsigned int bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
+    unsigned int sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
 
     // For voice calls use default configuration
     // force routing is not required here, caller will do it anyway
-    if (adev->mode == AUDIO_MODE_IN_CALL ||
-        adev->mode == AUDIO_MODE_IN_COMMUNICATION) {
+    if (adev->mode == AUDIO_MODE_IN_CALL || adev->mode == AUDIO_MODE_IN_COMMUNICATION) {
         ALOGW("%s:Use default bw and sr for voice/voip calls ",__func__);
-        *new_bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
-        *new_sample_rate =  CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
-        backend_change = true;
-    }
-
-    /*
-     * The backend should be configured at highest bit width and/or
-     * sample rate amongst all playback usecases.
-     * If the selected sample rate and/or bit width differ with
-     * current backend sample rate and/or bit width, then, we set the
-     * backend re-configuration flag.
-     *
-     * Exception: 16 bit playbacks is allowed through 16 bit/48 khz backend only
-     */
-    if (!backend_change) {
+        bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
+        sample_rate =  CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
+    } else {
+        /*
+         * The backend should be configured at highest bit width and/or
+         * sample rate amongst all playback usecases.
+         * If the selected sample rate and/or bit width differ with
+         * current backend sample rate and/or bit width, then, we set the
+         * backend re-configuration flag.
+         *
+         * Exception: 16 bit playbacks is allowed through 16 bit/48 khz backend only
+         */
         list_for_each(node, &adev->usecase_list) {
             struct audio_usecase *curr_usecase;
             curr_usecase = node_to_item(node, struct audio_usecase, list);
-            struct stream_out *out =
-                       (struct stream_out*) curr_usecase->stream.out;
-            if (out != NULL) {
-                cur_sr = get_best_backend_sample_rate(out->sample_rate);
-                cur_bw = get_best_backend_bit_width(out->bit_width);
-
-                ALOGV("Playback running bw %d sr %d standby %d",
-                          cur_bw, cur_sr, out->standby);
-
-                if (cur_bw > best_bw) {
-                    best_bw = cur_bw;
-                }
-
-                if (cur_sr > best_sr) {
-                    best_sr = cur_sr;
+            if (curr_usecase->type == PCM_PLAYBACK) {
+                struct stream_out *out =
+                           (struct stream_out*) curr_usecase->stream.out;
+                if (out != NULL ) {
+                    ALOGV("Offload playback running bw %d sr %d",
+                              out->bit_width, out->sample_rate);
+                        if (bit_width < out->bit_width)
+                            bit_width = out->bit_width;
+                        if (sample_rate < out->sample_rate)
+                            sample_rate = out->sample_rate;
                 }
             }
         }
-        *new_bit_width = best_bw;
-        *new_sample_rate = best_sr;
     }
 
+    // 24 bit playback on speakers and all 16 bit playbacks is allowed through
+    // 16 bit/48 khz backend only
+    if ((16 == bit_width) ||
+        ((24 == bit_width) &&
+         (usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER))) {
+        sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
+    }
     // Force routing if the expected bitwdith or samplerate
     // is not same as current backend comfiguration
-    if ((*new_bit_width != adev->cur_codec_backend_bit_width) ||
-        (*new_sample_rate != adev->cur_codec_backend_samplerate)) {
+    if ((bit_width != adev->cur_codec_backend_bit_width) ||
+        (sample_rate != adev->cur_codec_backend_samplerate)) {
+        *new_bit_width = bit_width;
+        *new_sample_rate = sample_rate;
         backend_change = true;
         ALOGI("%s Codec backend needs to be updated. new bit width: %d new sample rate: %d",
                __func__, *new_bit_width, *new_sample_rate);
@@ -2522,19 +2500,13 @@
     unsigned int new_bit_width = 0, old_bit_width;
     unsigned int new_sample_rate = 0, old_sample_rate;
 
-    old_bit_width = adev->cur_codec_backend_bit_width;
-    old_sample_rate = adev->cur_codec_backend_samplerate;
+    new_bit_width = old_bit_width = adev->cur_codec_backend_bit_width;
+    new_sample_rate = old_sample_rate = adev->cur_codec_backend_samplerate;
 
     ALOGW("Codec backend bitwidth %d, samplerate %d", old_bit_width, old_sample_rate);
     if (platform_check_codec_backend_cfg(adev, usecase,
                                       &new_bit_width, &new_sample_rate)) {
         platform_set_codec_backend_cfg(adev, new_bit_width, new_sample_rate);
-    }
-
-    if (old_bit_width != adev->cur_codec_backend_bit_width ||
-        old_sample_rate != adev->cur_codec_backend_samplerate) {
-        ALOGW("New codec backend bit width %d, sample rate %d",
-                    adev->cur_codec_backend_bit_width, adev->cur_codec_backend_samplerate);
         return true;
     }