audio_hw: Clean up out_write and in_read frame computation

Bug: 62375627
Bug: 62918461
Test: AudioTrackTest AudioRecordTest CTS
Change-Id: Icdccce108ac7a0fa5190032a29ea927f5c82049b
(cherry picked from commit 8e7f03f051f0dee1f838acf3aae4043642740149)
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index cb54e80..ee25242 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -2518,7 +2518,8 @@
 
     lock_output_stream(out);
     // this is always nonzero
-    const int frame_size = audio_stream_out_frame_size(stream);
+    const size_t frame_size = audio_stream_out_frame_size(stream);
+    const size_t frames = bytes / frame_size;
 
     if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
         error_code = ERROR_CODE_WRITE;
@@ -2609,7 +2610,7 @@
 exit:
     // For PCM we always consume the buffer and return #bytes regardless of ret.
     if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
-        out->written += bytes / (out->config.channels * sizeof(short));
+        out->written += frames;
     }
     long long sleeptime_us = 0;
 
@@ -2623,8 +2624,7 @@
         if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
             ALOGE_IF(out->pcm != NULL,
                     "%s: error %zd - %s", __func__, ret, pcm_get_error(out->pcm));
-            sleeptime_us = bytes * 1000000LL / frame_size /
-                out_get_sample_rate(&out->stream.common);
+            sleeptime_us = frames * 1000000LL / out_get_sample_rate(&out->stream.common);
             // usleep not guaranteed for values over 1 second but we don't limit here.
         }
     }
@@ -2637,7 +2637,7 @@
             usleep(sleeptime_us);
     } else {
         // only log if the data is properly written (out->power_log may be null)
-        power_log_log(out->power_log, buffer, bytes / frame_size, now_ns);
+        power_log_log(out->power_log, buffer, frames, now_ns);
     }
     return bytes;
 }
@@ -3198,6 +3198,8 @@
     int *int_buf_stream = NULL;
 
     lock_input_stream(in);
+    const size_t frame_size = audio_stream_in_frame_size(stream);
+    const size_t frames = bytes / frame_size;
 
     if (in->is_st_session) {
         ALOGVV(" %s: reading on st session bytes=%zu", __func__, bytes);
@@ -3269,12 +3271,11 @@
     if (ret != 0) {
         in_standby(&in->stream.common);
         ALOGV("%s: read failed - sleeping for buffer duration", __func__);
-        usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
-               in_get_sample_rate(&in->stream.common));
+        usleep(frames * 1000000LL / in_get_sample_rate(&in->stream.common));
         memset(buffer, 0, bytes); // clear return data
     }
     if (bytes > 0) {
-        in->frames_read += bytes / audio_stream_in_frame_size(stream);
+        in->frames_read += frames;
     }
     return bytes;
 }
@@ -3608,7 +3609,8 @@
         out->config = is_hdmi ? pcm_config_hdmi_multi : pcm_config_hifi;
         out->config.rate = config->sample_rate;
         out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
-        out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
+        out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels *
+                                                         audio_bytes_per_sample(config->format));
         out->config.format = pcm_format_from_audio_format(out->format);
     } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
         pthread_mutex_lock(&adev->lock);