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

Commit fcc335cd authored by En-Shuo Hsu's avatar En-Shuo Hsu
Browse files

floss: Move buffer for decoded PCM out of decoder

We need to be able to modify the decoded PCM frames in order to
reconverge samples after we conduct a packet loss concealment. This
change is to facilitate such follow-up implementation.

Also change to pass mSBC packet starting from sync word as the decoder
doesn't actually need the H2 header part.

Bug: 232463744
Tag: #floss
Test: Build and verify HFP WBS works
BYPASS_LONG_LINES_REASON: Bluetooth likes 120 char lines

Change-Id: I430860f1e6d066e65d16ca9e8d4ef0b24aa2df32
parent fb38966f
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -147,6 +147,8 @@ struct tBTM_MSBC_INFO {
  size_t encode_buf_wo;     /* Write offset of the encode buffer */
  size_t encode_buf_ro;     /* Read offset of the encode buffer */

  int16_t decoded_pcm_buf[120]; /* Buffer to store decoded PCM */

  uint8_t num_encoded_msbc_pkts; /* Number of the encoded mSBC packets */
  static size_t get_supported_packet_size(size_t pkt_size,
                                          size_t* buffer_size) {
@@ -369,11 +371,13 @@ size_t decode(const uint8_t** out_data) {
    goto packet_loss;
  }

  if (!hfp_msbc_decoder_decode_packet(frame_head, out_data)) {
  if (!hfp_msbc_decoder_decode_packet(frame_head, msbc_info->decoded_pcm_buf,
                                      sizeof(msbc_info->decoded_pcm_buf))) {
    LOG_DEBUG("Decoding mSBC packet failed");
    goto packet_loss;
  }

  *out_data = (const uint8_t*)msbc_info->decoded_pcm_buf;
  msbc_info->mark_pkt_decoded();
  return BTM_MSBC_CODE_SIZE;

+14 −11
Original line number Diff line number Diff line
@@ -27,11 +27,11 @@
#include "osi/include/log.h"

#define HFP_MSBC_PKT_LEN 60
#define HFP_MSBC_PCM_BYTES 240

typedef struct {
  OI_CODEC_SBC_DECODER_CONTEXT decoder_context;
  uint32_t context_data[CODEC_DATA_WORDS(2, SBC_CODEC_FAST_FILTER_BUFFERS)];
  int16_t decode_buf[120];
} tHFP_MSBC_DECODER;

static tHFP_MSBC_DECODER hfp_msbc_decoder;
@@ -61,26 +61,29 @@ void hfp_msbc_decoder_cleanup(void) {
}

// Get the HFP MSBC encoded maximum frame size
bool hfp_msbc_decoder_decode_packet(const uint8_t* i_buf,
                                    const uint8_t** o_buf) {
bool hfp_msbc_decoder_decode_packet(const uint8_t* i_buf, int16_t* o_buf,
                                    size_t out_len) {
  if (out_len < HFP_MSBC_PCM_BYTES) {
    LOG_ERROR(
        "Output buffer's size %lu is less than one complete mSBC frame %d",
        (unsigned long)out_len, HFP_MSBC_PCM_BYTES);
    return false;
  }

  const OI_BYTE* oi_data;
  uint32_t oi_size, out_avail;
  int16_t* out_ptr;

  oi_data = i_buf;
  oi_size = HFP_MSBC_PKT_LEN;
  out_avail = sizeof(hfp_msbc_decoder.decode_buf);
  out_ptr = hfp_msbc_decoder.decode_buf;
  out_avail = out_len;

  OI_STATUS status =
      OI_CODEC_SBC_DecodeFrame(&hfp_msbc_decoder.decoder_context, &oi_data,
                               &oi_size, out_ptr, &out_avail);
  if (!OI_SUCCESS(status) || out_avail != 240 || oi_size != 0) {
  OI_STATUS status = OI_CODEC_SBC_DecodeFrame(
      &hfp_msbc_decoder.decoder_context, &oi_data, &oi_size, o_buf, &out_avail);
  if (!OI_SUCCESS(status) || out_avail != HFP_MSBC_PCM_BYTES || oi_size != 0) {
    LOG_ERROR("Decoding failure: %d, %lu, %lu", status,
              (unsigned long)out_avail, (unsigned long)oi_size);
    return false;
  }

  *o_buf = (const uint8_t*)&hfp_msbc_decoder.decode_buf;
  return true;
}
+6 −4
Original line number Diff line number Diff line
@@ -21,7 +21,8 @@
#ifndef HFP_MSBC_DECODER_H
#define HFP_MSBC_DECODER_H

#include <stdint.h>
#include <cstddef>
#include <cstdint>

// Initialize the HFP MSBC decoder.
bool hfp_msbc_decoder_init(void);
@@ -29,8 +30,9 @@ bool hfp_msbc_decoder_init(void);
// Cleanup the HFP MSBC decoder.
void hfp_msbc_decoder_cleanup(void);

// Decodes |i_buf|. |o_buf| will be assigned to the decoded frames if available.
bool hfp_msbc_decoder_decode_packet(const uint8_t* i_buf,
                                    const uint8_t** o_buf);
// Decodes |i_buf| into |o_buf| with size |out_len| in bytes. |i_buf| should
// point to a complete mSBC packet with 60 bytes of data including the header.
bool hfp_msbc_decoder_decode_packet(const uint8_t* i_buf, int16_t* o_buf,
                                    size_t out_len);

#endif  // HFP_MSBC_DECODER_H