Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7fc5d7dc authored by Pavlin Radoslavov's avatar Pavlin Radoslavov
Browse files

Reduce the number of PCM data reads for aptX and aptX HD

For every periodic aptX / aptX HD data packet, use a single read
operation for all PCM data that needs to be encoded, instead of
using multiple reads of smaller blocks of data.

Bug: 70899260
Test: Manual: stream to aptX/aptX HD headset, change the sample rate
Change-Id: Ifcb8302589f24f5d7ab89c645c50d6905197fb7f
Merged-In: Ifcb8302589f24f5d7ab89c645c50d6905197fb7f
(cherry picked from commit fbfffe53)
parent 15c2e335
Loading
Loading
Loading
Loading
+27 −23
Original line number Original line Diff line number Diff line
@@ -61,7 +61,7 @@ static tAPTX_ENCODER_SIZEOF_PARAMS aptx_encoder_sizeof_params_func;
#define A2DP_APTX_OFFSET (AVDT_MEDIA_OFFSET - AVDT_MEDIA_HDR_SIZE)
#define A2DP_APTX_OFFSET (AVDT_MEDIA_OFFSET - AVDT_MEDIA_HDR_SIZE)
#endif
#endif


#define A2DP_APTX_MAX_PCM_BYTES_PER_READ 1024
#define A2DP_APTX_MAX_PCM_BYTES_PER_READ 4096


typedef struct {
typedef struct {
  uint64_t sleep_time_ns;
  uint64_t sleep_time_ns;
@@ -391,34 +391,38 @@ void a2dp_vendor_aptx_send_frames(uint64_t timestamp_us) {
  //
  //
  // Read the PCM data and encode it
  // Read the PCM data and encode it
  //
  //
  LOG_VERBOSE(LOG_TAG, "%s: %u PCM reads of size %u", __func__,
  uint16_t read_buffer16[A2DP_APTX_MAX_PCM_BYTES_PER_READ / sizeof(uint16_t)];
              framing_params->pcm_reads, framing_params->pcm_bytes_per_read);
  uint32_t expected_read_bytes =
      framing_params->pcm_reads * framing_params->pcm_bytes_per_read;
  size_t encoded_ptr_index = 0;
  size_t encoded_ptr_index = 0;
  size_t pcm_bytes_encoded = 0;
  size_t pcm_bytes_encoded = 0;
  uint32_t bytes_read = 0;
  uint32_t bytes_read = 0;

  a2dp_aptx_encoder_cb.stats.media_read_total_expected_packets++;
  a2dp_aptx_encoder_cb.stats.media_read_total_expected_packets++;
  a2dp_aptx_encoder_cb.stats.media_read_total_expected_reads_count +=
  a2dp_aptx_encoder_cb.stats.media_read_total_expected_reads_count++;
      framing_params->pcm_reads;
  a2dp_aptx_encoder_cb.stats.media_read_total_expected_read_bytes +=
  a2dp_aptx_encoder_cb.stats.media_read_total_expected_read_bytes +=
      framing_params->pcm_reads * framing_params->pcm_bytes_per_read;
      expected_read_bytes;
  for (size_t reads = 0; reads < framing_params->pcm_reads; reads++) {

    uint16_t read_buffer16[A2DP_APTX_MAX_PCM_BYTES_PER_READ / sizeof(uint16_t)];
  LOG_VERBOSE(LOG_TAG, "%s: PCM read of size %u", __func__,
    size_t pcm_bytes_read = a2dp_aptx_encoder_cb.read_callback(
              expected_read_bytes);
        (uint8_t*)read_buffer16, framing_params->pcm_bytes_per_read);
  bytes_read = a2dp_aptx_encoder_cb.read_callback((uint8_t*)read_buffer16,
    bytes_read += pcm_bytes_read;
                                                  expected_read_bytes);
    a2dp_aptx_encoder_cb.stats.media_read_total_actual_read_bytes +=
  a2dp_aptx_encoder_cb.stats.media_read_total_actual_read_bytes += bytes_read;
        pcm_bytes_read;
  if (bytes_read < expected_read_bytes) {
    if (pcm_bytes_read < framing_params->pcm_bytes_per_read) {
    LOG_WARN(LOG_TAG,
    LOG_WARN(LOG_TAG,
               "%s: underflow at PCM reading iteration %zu: read %zu "
             "%s: underflow at PCM reading: read %u bytes instead of %u",
               "instead of %d",
             __func__, bytes_read, expected_read_bytes);
               __func__, reads, pcm_bytes_read,
    a2dp_aptx_encoder_cb.stats.media_read_total_dropped_packets++;
               framing_params->pcm_bytes_per_read);
    osi_free(p_buf);
      break;
    return;
  }
  }
  a2dp_aptx_encoder_cb.stats.media_read_total_actual_reads_count++;
  a2dp_aptx_encoder_cb.stats.media_read_total_actual_reads_count++;

  for (uint32_t reads = 0, offset = 0; reads < framing_params->pcm_reads;
       reads++, offset +=
                (framing_params->pcm_bytes_per_read / sizeof(uint16_t))) {
    pcm_bytes_encoded += aptx_encode_16bit(framing_params, &encoded_ptr_index,
    pcm_bytes_encoded += aptx_encode_16bit(framing_params, &encoded_ptr_index,
                                           read_buffer16, encoded_ptr);
                                           read_buffer16 + offset, encoded_ptr);
  }
  }


  // Compute the number of encoded bytes
  // Compute the number of encoded bytes
+31 −25
Original line number Original line Diff line number Diff line
@@ -61,7 +61,7 @@ static tAPTX_HD_ENCODER_SIZEOF_PARAMS aptx_hd_encoder_sizeof_params_func;
#define A2DP_APTX_HD_OFFSET AVDT_MEDIA_OFFSET
#define A2DP_APTX_HD_OFFSET AVDT_MEDIA_OFFSET
#endif
#endif


#define A2DP_APTX_HD_MAX_PCM_BYTES_PER_READ 1024
#define A2DP_APTX_HD_MAX_PCM_BYTES_PER_READ 4096


typedef struct {
typedef struct {
  uint64_t sleep_time_ns;
  uint64_t sleep_time_ns;
@@ -376,35 +376,41 @@ void a2dp_vendor_aptx_hd_send_frames(uint64_t timestamp_us) {
  //
  //
  // Read the PCM data and encode it
  // Read the PCM data and encode it
  //
  //
  LOG_VERBOSE(LOG_TAG, "%s: %u PCM reads of size %u", __func__,
  uint32_t
              framing_params->pcm_reads, framing_params->pcm_bytes_per_read);
      read_buffer32[A2DP_APTX_HD_MAX_PCM_BYTES_PER_READ / sizeof(uint32_t)];
  uint32_t expected_read_bytes =
      framing_params->pcm_reads * framing_params->pcm_bytes_per_read;
  size_t encoded_ptr_index = 0;
  size_t encoded_ptr_index = 0;
  size_t pcm_bytes_encoded = 0;
  size_t pcm_bytes_encoded = 0;
  uint32_t bytes_read = 0;
  uint32_t bytes_read = 0;

  a2dp_aptx_hd_encoder_cb.stats.media_read_total_expected_packets++;
  a2dp_aptx_hd_encoder_cb.stats.media_read_total_expected_packets++;
  a2dp_aptx_hd_encoder_cb.stats.media_read_total_expected_reads_count +=
  a2dp_aptx_hd_encoder_cb.stats.media_read_total_expected_reads_count++;
      framing_params->pcm_reads;
  a2dp_aptx_hd_encoder_cb.stats.media_read_total_expected_read_bytes +=
  a2dp_aptx_hd_encoder_cb.stats.media_read_total_expected_read_bytes +=
      framing_params->pcm_reads * framing_params->pcm_bytes_per_read;
      expected_read_bytes;
  for (size_t reads = 0; reads < framing_params->pcm_reads; reads++) {

    uint32_t
  LOG_VERBOSE(LOG_TAG, "%s: PCM read of size %u", __func__,
        read_buffer32[A2DP_APTX_HD_MAX_PCM_BYTES_PER_READ / sizeof(uint32_t)];
              expected_read_bytes);
    size_t pcm_bytes_read = a2dp_aptx_hd_encoder_cb.read_callback(
  bytes_read = a2dp_aptx_hd_encoder_cb.read_callback((uint8_t*)read_buffer32,
        (uint8_t*)read_buffer32, framing_params->pcm_bytes_per_read);
                                                     expected_read_bytes);
    bytes_read += pcm_bytes_read;
  a2dp_aptx_hd_encoder_cb.stats.media_read_total_actual_read_bytes +=
  a2dp_aptx_hd_encoder_cb.stats.media_read_total_actual_read_bytes +=
        pcm_bytes_read;
      bytes_read;
    if (pcm_bytes_read < framing_params->pcm_bytes_per_read) {
  if (bytes_read < expected_read_bytes) {
    LOG_WARN(LOG_TAG,
    LOG_WARN(LOG_TAG,
               "%s: underflow at PCM reading iteration %zu: read %zu "
             "%s: underflow at PCM reading: read %u bytes instead of %u",
               "instead of %d",
             __func__, bytes_read, expected_read_bytes);
               __func__, reads, pcm_bytes_read,
    a2dp_aptx_hd_encoder_cb.stats.media_read_total_dropped_packets++;
               framing_params->pcm_bytes_per_read);
    osi_free(p_buf);
      break;
    return;
  }
  }
  a2dp_aptx_hd_encoder_cb.stats.media_read_total_actual_reads_count++;
  a2dp_aptx_hd_encoder_cb.stats.media_read_total_actual_reads_count++;
    pcm_bytes_encoded += aptx_hd_encode_24bit(

        framing_params, &encoded_ptr_index, read_buffer32, encoded_ptr);
  for (uint32_t reads = 0, offset = 0; reads < framing_params->pcm_reads;
       reads++, offset +=
                framing_params->pcm_bytes_per_read / sizeof(uint32_t)) {
    pcm_bytes_encoded +=
        aptx_hd_encode_24bit(framing_params, &encoded_ptr_index,
                             read_buffer32 + offset, encoded_ptr);
  }
  }


  // Compute the number of encoded bytes
  // Compute the number of encoded bytes