replace common qcom sources with samsung ones

This commit is contained in:
SaschaNes
2025-08-12 22:13:00 +02:00
parent ba24dcded9
commit 6f7753de11
5682 changed files with 2450203 additions and 103634 deletions

View File

@@ -0,0 +1,3 @@
LOCAL_PATH := $(call my-dir)
include $(call all-subdir-makefiles)

View File

@@ -0,0 +1,98 @@
LOCAL_PATH:= $(call my-dir)
LOCAL_CFLAGS += -Wall -Werror
#--------------------------------------------
# Build bt_bundle LIB
#--------------------------------------------
include $(CLEAR_VARS)
ifeq (true,$(call spf_check,SEC_PRODUCT_FEATURE_BLUETOOTH_SUPPORT_A2DP_OFFLOAD,TRUE))
ifeq ($(BOARD_HAVE_BLUETOOTH_QCOM),true)
LOCAL_CFLAGS += -DQCA_OFFLOAD
endif #BOARD_HAVE_BLUETOOTH_QCOM
endif
LOCAL_SRC_FILES := \
bt_base.c \
bt_bundle.c
LOCAL_CFLAGS += -O2 -fvisibility=hidden
ifeq ($(strip $(AUDIO_FEATURE_ENABLE_BT_A2DP_LPI)),true)
LOCAL_CFLAGS += -DBT_A2DP_LPI_ENABLED
endif
LOCAL_SHARED_LIBRARIES := \
libcutils \
liblog \
libdl
LOCAL_C_INCLUDES += $(TOP)/system/media/audio/include
LOCAL_HEADER_LIBRARIES := \
libspf-headers \
libarosal_headers
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := lib_bt_bundle
LOCAL_MODULE_OWNER := qti
LOCAL_VENDOR_MODULE := true
include $(BUILD_SHARED_LIBRARY)
#--------------------------------------------
# Build bt_aptx LIB
#--------------------------------------------
include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
bt_base.c \
bt_aptx.c
LOCAL_CFLAGS += -O2 -fvisibility=hidden
LOCAL_SHARED_LIBRARIES := \
libcutils \
liblog \
libdl
LOCAL_C_INCLUDES += $(TOP)/system/media/audio/include
LOCAL_HEADER_LIBRARIES := \
libspf-headers \
libarosal_headers
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := lib_bt_aptx
LOCAL_MODULE_OWNER := qti
LOCAL_VENDOR_MODULE := true
include $(BUILD_SHARED_LIBRARY)
#--------------------------------------------
# Build bt_ble LIB
#--------------------------------------------
include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
bt_base.c \
bt_ble.c
LOCAL_CFLAGS += -O2 -fvisibility=hidden
LOCAL_SHARED_LIBRARIES := \
libcutils \
liblog \
libdl
LOCAL_C_INCLUDES += $(TOP)/system/media/audio/include
LOCAL_HEADER_LIBRARIES := \
libspf-headers \
libarosal_headers
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := lib_bt_ble
LOCAL_MODULE_OWNER := qti
LOCAL_VENDOR_MODULE := true
include $(BUILD_SHARED_LIBRARY)

View File

@@ -0,0 +1,684 @@
/*
* Copyright (c) 2020, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define LOG_TAG "PAL: bt_aptx"
//#define LOG_NDEBUG 0
#include <log/log.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "bt_aptx.h"
#include "bt_base.h"
#include <aptx_classic_encoder_api.h>
#include <aptx_hd_encoder_api.h>
#include <aptx_adaptive_encoder_api.h>
#include <aptx_adaptive_swb_encoder_api.h>
#include <aptx_adaptive_swb_decoder_api.h>
typedef enum {
SWAP_DISABLE,
SWAP_ENABLE,
} swap_status_t;
#define DEFAULT_FADE_DURATION 255
static int aptx_pack_enc_config(bt_codec_t *codec, void *src, void **dst)
{
audio_aptx_encoder_config_t *aptx_bt_cfg = NULL;
bt_enc_payload_t *enc_payload = NULL;
int ret = 0, num_blks = 1, i = 0;
custom_block_t *blk[1] = {NULL};
ALOGV("%s", __func__);
if ((src == NULL) || (dst == NULL)) {
ALOGE("%s: invalid input parameters", __func__);
return -EINVAL;
}
aptx_bt_cfg = (audio_aptx_encoder_config_t *)src;
enc_payload = (bt_enc_payload_t *)calloc(1, sizeof(bt_enc_payload_t) +
num_blks * sizeof(custom_block_t *));
if (enc_payload == NULL) {
ALOGE("%s: fail to allocate memory", __func__);
return -ENOMEM;
}
enc_payload->bit_format = aptx_bt_cfg->bits_per_sample;
enc_payload->sample_rate = aptx_bt_cfg->sampling_rate;
enc_payload->channel_count = aptx_bt_cfg->channels;
enc_payload->num_blks = num_blks;
for (i = 0; i < num_blks; i++) {
blk[i] = (custom_block_t *)calloc(1, sizeof(custom_block_t));
if (!blk[i]) {
ret = -ENOMEM;
goto free_payload;
}
}
/* populate payload for PARAM_ID_REAL_MODULE_ID */
ret = bt_base_populate_real_module_id(blk[0], MODULE_ID_APTX_CLASSIC_ENC);
if (ret)
goto free_payload;
enc_payload->blocks[0] = blk[0];
*dst = enc_payload;
codec->payload = enc_payload;
return ret;
free_payload:
for (i = 0; i < num_blks; i++) {
if (blk[i]) {
if (blk[i]->payload)
free(blk[i]->payload);
free(blk[i]);
}
}
if (enc_payload)
free(enc_payload);
return ret;
}
static int aptx_dual_mono_pack_enc_config(bt_codec_t *codec, void *src, void **dst)
{
audio_aptx_dual_mono_config_t *aptx_dm_bt_cfg = NULL;
bt_enc_payload_t *enc_payload = NULL;
param_id_aptx_classic_sync_mode_payload_t *aptx_sync_mode = NULL;
int ret = 0, num_blks = 2, i = 0;
custom_block_t *blk[2] = {NULL};
ALOGV("%s", __func__);
if ((src == NULL) || (dst == NULL)) {
ALOGE("%s: invalid input parameters", __func__);
return -EINVAL;
}
aptx_dm_bt_cfg = (audio_aptx_dual_mono_config_t*)src;
enc_payload = (bt_enc_payload_t *)calloc(1, sizeof(bt_enc_payload_t) +
num_blks * sizeof(custom_block_t *));
if (enc_payload == NULL) {
ALOGE("%s: fail to allocate memory", __func__);
return -ENOMEM;
}
enc_payload->bit_format = ENCODER_BIT_FORMAT_DEFAULT;
enc_payload->sample_rate = aptx_dm_bt_cfg->sampling_rate;
enc_payload->channel_count = aptx_dm_bt_cfg->channels;
enc_payload->num_blks = num_blks;
for (i = 0; i < num_blks; i++) {
blk[i] = (custom_block_t *)calloc(1, sizeof(custom_block_t));
if (!blk[i]) {
ret = -ENOMEM;
goto free_payload;
}
}
/* populate payload for PARAM_ID_REAL_MODULE_ID */
ret = bt_base_populate_real_module_id(blk[0], MODULE_ID_APTX_CLASSIC_ENC);
if (ret)
goto free_payload;
/* populate payload for PARAM_ID_APTX_CLASSIC_SET_SYNC_MODE */
aptx_sync_mode = (param_id_aptx_classic_sync_mode_payload_t*)calloc(1,
sizeof(param_id_aptx_classic_sync_mode_payload_t));
if (aptx_sync_mode == NULL) {
ALOGE("%s: fail to allocate memory", __func__);
ret = -ENOMEM;
goto free_payload;
}
/* Sync mode should be DUAL AUTOSYNC (1) for TWS+ */
aptx_sync_mode->sync_mode = aptx_dm_bt_cfg->sync_mode;
if (enc_payload->channel_count == 1)
aptx_sync_mode->startup_mode = 1; /* (L + R)/2 input */
else
aptx_sync_mode->startup_mode = 0; /* L and R separate input */
aptx_sync_mode->cvr_fade_duration = DEFAULT_FADE_DURATION;
ret = bt_base_populate_enc_cmn_param(blk[1], PARAM_ID_APTX_CLASSIC_SET_SYNC_MODE,
aptx_sync_mode, sizeof(param_id_aptx_classic_sync_mode_payload_t));
free(aptx_sync_mode);
if (ret)
goto free_payload;
enc_payload->blocks[0] = blk[0];
enc_payload->blocks[1] = blk[1];
*dst = enc_payload;
codec->payload = enc_payload;
return ret;
free_payload:
for (i = 0; i < num_blks; i++) {
if (blk[i]) {
if (blk[i]->payload)
free(blk[i]->payload);
free(blk[i]);
}
}
if (enc_payload)
free(enc_payload);
return ret;
}
int bt_apt_ad_populate_enc_audiosrc_info(custom_block_t *blk, uint16_t sample_rate)
{
struct param_id_aptx_adaptive_enc_original_samplerate_t *param = NULL;
blk->param_id = PARAM_ID_APTX_ADAPTIVE_ENC_AUDIOSRC_INFO;
blk->payload_sz = sizeof(struct param_id_aptx_adaptive_enc_original_samplerate_t);
blk->payload = calloc(1, blk->payload_sz);
if (!blk->payload) {
blk->payload_sz = 0;
return -ENOMEM;
}
param = (struct param_id_aptx_adaptive_enc_original_samplerate_t*) blk->payload;
param->original_sample_rate = sample_rate;
return 0;
}
static int aptx_ad_pack_enc_config(bt_codec_t *codec, void *src, void **dst)
{
audio_aptx_ad_encoder_config_t *aptx_ad_bt_cfg = NULL;
bt_enc_payload_t *enc_payload = NULL;
struct param_id_aptx_adaptive_enc_init_t *enc_init = NULL;
struct param_id_aptx_adaptive_enc_profile_t *enc_profile = NULL;
int ret = 0, num_blks = 3, i = 0;
custom_block_t *blk[3] = {NULL};
ALOGV("%s", __func__);
if ((src == NULL) || (dst == NULL)) {
ALOGE("%s: invalid input parameters", __func__);
return -EINVAL;
}
aptx_ad_bt_cfg = (audio_aptx_ad_encoder_config_t*)src;
enc_payload = (bt_enc_payload_t *)calloc(1, sizeof(bt_enc_payload_t) +
num_blks * sizeof(custom_block_t *));
if (enc_payload == NULL) {
ALOGE("%s: fail to allocate memory", __func__);
return -ENOMEM;
}
/* ABR is always enabled for APTx Adaptive */
enc_payload->is_abr_enabled = true;
enc_payload->bit_format = aptx_ad_bt_cfg->bits_per_sample;
enc_payload->num_blks = num_blks;
switch(aptx_ad_bt_cfg->sampling_rate) {
case APTX_AD_SR_UNCHANGED:
case APTX_AD_48:
default:
enc_payload->sample_rate = SAMPLING_RATE_48K;
break;
case APTX_AD_44_1:
enc_payload->sample_rate = SAMPLING_RATE_44K;
break;
case APTX_AD_96:
enc_payload->sample_rate = SAMPLING_RATE_96K;
break;
}
switch (aptx_ad_bt_cfg->channel_mode) {
case APTX_AD_CHANNEL_MONO:
enc_payload->channel_count = 1;
break;
default:
enc_payload->channel_count = 2;
break;
}
for (i = 0; i < num_blks; i++) {
blk[i] = (custom_block_t *)calloc(1, sizeof(custom_block_t));
if (!blk[i]) {
ret = -ENOMEM;
goto free_payload;
}
}
/* populate payload for PARAM_ID_APTX_ADAPTIVE_ENC_INIT */
enc_init = (struct param_id_aptx_adaptive_enc_init_t*)calloc(1, sizeof(struct param_id_aptx_adaptive_enc_init_t));
if (enc_init == NULL) {
ALOGE("%s: fail to allocate memory", __func__);
ret = -ENOMEM;
goto free_payload;
}
enc_init->sampleRate = aptx_ad_bt_cfg->sampling_rate;
enc_init->mtulink0 = aptx_ad_bt_cfg->mtu;
enc_init->channelMode0 = aptx_ad_bt_cfg->channel_mode;
enc_init->minsinkBufLL = aptx_ad_bt_cfg->min_sink_modeA;
enc_init->maxsinkBufLL = aptx_ad_bt_cfg->max_sink_modeA;
enc_init->minsinkBufHQ = aptx_ad_bt_cfg->min_sink_modeB;
enc_init->maxsinkBufHQ = aptx_ad_bt_cfg->max_sink_modeB;
enc_init->minsinkBufTws = aptx_ad_bt_cfg->min_sink_modeC;
enc_init->maxsinkBufTws = aptx_ad_bt_cfg->max_sink_modeC;
enc_init->profile = aptx_ad_bt_cfg->encoder_mode;
enc_init->twsplusDualMonoMode = aptx_ad_bt_cfg->input_mode;
enc_init->twsplusFadeDuration = aptx_ad_bt_cfg->fade_duration;
for (int i = 0; i < sizeof(enc_init->sinkCapability); i++)
enc_init->sinkCapability[i] = aptx_ad_bt_cfg->sink_cap[i];
ret = bt_base_populate_enc_cmn_param(blk[0], PARAM_ID_APTX_ADAPTIVE_ENC_INIT,
enc_init, sizeof(struct param_id_aptx_adaptive_enc_init_t));
free(enc_init);
if (ret)
goto free_payload;
/* populate payload for PARAM_ID_APTX_ADAPTIVE_ENC_PROFILE */
enc_profile = (struct param_id_aptx_adaptive_enc_profile_t*)calloc(1, sizeof(struct param_id_aptx_adaptive_enc_profile_t));
if (enc_profile == NULL) {
ALOGE("%s: fail to allocate memory", __func__);
ret = -ENOMEM;
goto free_payload;
}
enc_profile->primprofile = 0;
enc_profile->secprofile = 0;
enc_profile->LLModeLatTarget0 = aptx_ad_bt_cfg->TTP_modeA_low;
enc_profile->LLModeLatTarget1 = aptx_ad_bt_cfg->TTP_modeA_high;
enc_profile->hQLatTarget0 = aptx_ad_bt_cfg->TTP_modeB_low;
enc_profile->hQLatTarget1 = aptx_ad_bt_cfg->TTP_modeB_high;
ret = bt_base_populate_enc_cmn_param(blk[1], PARAM_ID_APTX_ADAPTIVE_ENC_PROFILE,
enc_profile, sizeof(struct param_id_aptx_adaptive_enc_profile_t));
free(enc_profile);
if (ret)
goto free_payload;
/* populate payload for PARAM_ID_APTX_ADAPTIVE_ENC_AUDIOSRC_INFO */
//TODO: hard coded sample rate as 0 - same as system setting
ret = bt_apt_ad_populate_enc_audiosrc_info(blk[2], 0);
if (ret)
goto free_payload;
enc_payload->blocks[0] = blk[0];
enc_payload->blocks[1] = blk[1];
enc_payload->blocks[2] = blk[2];
*dst = enc_payload;
codec->payload = enc_payload;
return ret;
free_payload:
for (i = 0; i < num_blks; i++) {
if (blk[i]) {
if (blk[i]->payload)
free(blk[i]->payload);
free(blk[i]);
}
}
if (enc_payload)
free(enc_payload);
return ret;
}
static int aptx_hd_pack_enc_config(bt_codec_t *codec, void *src, void **dst)
{
audio_aptx_hd_encoder_config_t *aptx_hd_bt_cfg = NULL;
bt_enc_payload_t *enc_payload = NULL;
int ret = 0, num_blks = 1, i = 0;
custom_block_t *blk[1] = {NULL};
ALOGV("%s", __func__);
if ((src == NULL) || (dst == NULL)) {
ALOGE("%s: invalid input parameters", __func__);
return -EINVAL;
}
aptx_hd_bt_cfg = (audio_aptx_hd_encoder_config_t*)src;
enc_payload = (bt_enc_payload_t *)calloc(1, sizeof(bt_enc_payload_t) +
num_blks * sizeof(custom_block_t *));
if (enc_payload == NULL) {
ALOGE("%s: fail to allocate memory", __func__);
return -ENOMEM;
}
enc_payload->bit_format = aptx_hd_bt_cfg->bits_per_sample;
enc_payload->sample_rate = aptx_hd_bt_cfg->sampling_rate;
enc_payload->channel_count = aptx_hd_bt_cfg->channels;
enc_payload->num_blks = num_blks;
for (i = 0; i < num_blks; i++) {
blk[i] = (custom_block_t *)calloc(1, sizeof(custom_block_t));
if (!blk[i]) {
ret = -ENOMEM;
goto free_payload;
}
}
/* populate payload for PARAM_ID_REAL_MODULE_ID */
ret = bt_base_populate_real_module_id(blk[0], MODULE_ID_APTX_HD_ENC);
if (ret)
goto free_payload;
enc_payload->blocks[0] = blk[0];
*dst = enc_payload;
codec->payload = enc_payload;
return ret;
free_payload:
for (i = 0; i < num_blks; i++) {
if (blk[i]) {
if (blk[i]->payload)
free(blk[i]->payload);
free(blk[i]);
}
}
if (enc_payload)
free(enc_payload);
return ret;
}
static int aptx_ad_speech_pack_enc_config(bt_codec_t *codec, void *src, void **dst)
{
param_id_aptx_adaptive_speech_enc_init_t *aptx_ad_speech_cfg = NULL;
bt_enc_payload_t *enc_payload = NULL;
uint32_t *mode;
int ret = 0, num_blks = 1, i = 0;
custom_block_t *blk[1] = {NULL};
ALOGV("%s", __func__);
if ((src == NULL) || (dst == NULL)) {
ALOGE("%s: invalid input parameters", __func__);
return -EINVAL;
}
mode = (uint32_t *)src;
enc_payload = (bt_enc_payload_t *)calloc(1, sizeof(bt_enc_payload_t) +
num_blks * sizeof(custom_block_t *));
if (enc_payload == NULL) {
ALOGE("%s: fail to allocate memory", __func__);
return -ENOMEM;
}
enc_payload->bit_format = ENCODER_BIT_FORMAT_DEFAULT;
enc_payload->sample_rate = SAMPLING_RATE_32K;
enc_payload->channel_count = CH_MONO;
enc_payload->num_blks = num_blks;
enc_payload->is_abr_enabled = true;
for (i = 0; i < num_blks; i++) {
blk[i] = (custom_block_t *)calloc(1, sizeof(custom_block_t));
if (!blk[i]) {
ret = -ENOMEM;
goto free_payload;
}
}
/* populate payload for PARAM_ID_APTX_ADAPTIVE_SPEECH_ENC_INIT */
aptx_ad_speech_cfg = (param_id_aptx_adaptive_speech_enc_init_t *)calloc(1,
sizeof(param_id_aptx_adaptive_speech_enc_init_t));
if (aptx_ad_speech_cfg == NULL) {
ALOGE("%s: fail to allocate memory", __func__);
ret = -ENOMEM;
goto free_payload;
}
aptx_ad_speech_cfg->speechMode = *mode;
aptx_ad_speech_cfg->byteSwap = SWAP_ENABLE;
ret = bt_base_populate_enc_cmn_param(blk[0], PARAM_ID_APTX_ADAPTIVE_SPEECH_ENC_INIT,
aptx_ad_speech_cfg, sizeof(param_id_aptx_adaptive_speech_enc_init_t));
free(aptx_ad_speech_cfg);
if (ret)
goto free_payload;
enc_payload->blocks[0] = blk[0];
*dst = enc_payload;
codec->payload = enc_payload;
return ret;
free_payload:
for (i = 0; i < num_blks; i++) {
if (blk[i]) {
if (blk[i]->payload)
free(blk[i]->payload);
free(blk[i]);
}
}
if (enc_payload)
free(enc_payload);
return ret;
}
static int aptx_ad_speech_pack_dec_config(bt_codec_t *codec, void *src, void **dst)
{
param_id_aptx_adaptive_speech_dec_init_t *aptx_ad_speech_cfg = NULL;
bt_enc_payload_t *enc_payload = NULL;
uint32_t *mode;
int ret = 0, num_blks = 1, i = 0;
custom_block_t *blk[1] = {NULL};
ALOGV("%s", __func__);
if ((src == NULL) || (dst == NULL)) {
ALOGE("%s: invalid input parameters", __func__);
return -EINVAL;
}
mode = (uint32_t *)src;
enc_payload = (bt_enc_payload_t *)calloc(1, sizeof(bt_enc_payload_t) +
num_blks * sizeof(custom_block_t *));
if (enc_payload == NULL) {
ALOGE("%s: fail to allocate memory", __func__);
return -ENOMEM;
}
enc_payload->bit_format = ENCODER_BIT_FORMAT_DEFAULT;
enc_payload->sample_rate = SAMPLING_RATE_32K;
enc_payload->channel_count = CH_MONO;
enc_payload->num_blks = num_blks;
enc_payload->is_abr_enabled = true;
for (i = 0; i < num_blks; i++) {
blk[i] = (custom_block_t *)calloc(1, sizeof(custom_block_t));
if (!blk[i]) {
ret = -ENOMEM;
goto free_payload;
}
}
/* populate payload for PARAM_ID_APTX_ADAPTIVE_SPEECH_DEC_INIT */
aptx_ad_speech_cfg = (param_id_aptx_adaptive_speech_dec_init_t *)calloc(1,
sizeof(param_id_aptx_adaptive_speech_dec_init_t));
if (aptx_ad_speech_cfg == NULL) {
ALOGE("%s: fail to allocate memory", __func__);
ret = -ENOMEM;
goto free_payload;
}
aptx_ad_speech_cfg->speechMode = *mode;
aptx_ad_speech_cfg->byteSwap = SWAP_ENABLE;
ret = bt_base_populate_enc_cmn_param(blk[0], PARAM_ID_APTX_ADAPTIVE_SPEECH_DEC_INIT,
aptx_ad_speech_cfg, sizeof(param_id_aptx_adaptive_speech_dec_init_t));
free(aptx_ad_speech_cfg);
if (ret)
goto free_payload;
enc_payload->blocks[0] = blk[0];
*dst = enc_payload;
codec->payload = enc_payload;
return ret;
free_payload:
for (i = 0; i < num_blks; i++) {
if (blk[i]) {
if (blk[i]->payload)
free(blk[i]->payload);
free(blk[i]);
}
}
if (enc_payload)
free(enc_payload);
return ret;
}
static int bt_aptx_populate_payload(bt_codec_t *codec, void *src, void **dst)
{
config_fn_t config_fn = NULL;
if (codec->payload)
free(codec->payload);
switch (codec->codecFmt) {
case CODEC_TYPE_APTX:
config_fn = ((codec->direction == ENC) ? &aptx_pack_enc_config :
NULL);
break;
case CODEC_TYPE_APTX_AD:
config_fn = ((codec->direction == ENC) ? &aptx_ad_pack_enc_config :
NULL);
break;
case CODEC_TYPE_APTX_HD:
config_fn = ((codec->direction == ENC) ? &aptx_hd_pack_enc_config :
NULL);
break;
case CODEC_TYPE_APTX_DUAL_MONO:
config_fn = ((codec->direction == ENC) ? &aptx_dual_mono_pack_enc_config :
NULL);
break;
case CODEC_TYPE_APTX_AD_SPEECH:
config_fn = ((codec->direction == ENC) ? &aptx_ad_speech_pack_enc_config :
&aptx_ad_speech_pack_dec_config);
break;
default:
ALOGD("%s unsupported codecFmt %d\n", __func__, codec->codecFmt);
}
if (config_fn != NULL) {
return config_fn(codec, src, dst);
}
return -EINVAL;
}
static uint64_t bt_aptx_get_decoder_latency(bt_codec_t *codec)
{
uint32_t latency = 0;
switch (codec->codecFmt) {
case CODEC_TYPE_APTX:
case CODEC_TYPE_APTX_HD:
case CODEC_TYPE_APTX_AD:
case CODEC_TYPE_APTX_DUAL_MONO:
default:
latency = 200;
ALOGD("No valid decoder defined, setting latency to %dms", latency);
break;
}
return (uint64_t)latency;
}
static uint64_t bt_aptx_get_encoder_latency(bt_codec_t *codec)
{
uint32_t latency = 0;
switch (codec->codecFmt) {
case CODEC_TYPE_APTX:
latency = ENCODER_LATENCY_APTX;
break;
case CODEC_TYPE_APTX_HD:
latency = ENCODER_LATENCY_APTX_HD;
break;
case CODEC_TYPE_APTX_AD:
/* for aptx adaptive the latency depends on the mode (HQ/LL) and
* BT IPC will take care of accomodating the mode factor and return latency
*/
latency = 0;
break;
default:
latency = 200;
break;
}
ALOGV("%s: codecFmt %u, direction %d, latency %u",
__func__, codec->codecFmt, codec->direction, latency);
return latency;
}
static uint64_t bt_aptx_get_codec_latency(bt_codec_t *codec)
{
if (codec->direction == ENC)
return bt_aptx_get_encoder_latency(codec);
else
return bt_aptx_get_decoder_latency(codec);
}
int bt_aptx_query_num_codecs(bt_codec_t *codec __unused) {
ALOGV("%s", __func__);
return NUM_CODEC;
}
void bt_aptx_close(bt_codec_t *codec)
{
bt_enc_payload_t *payload = codec->payload;
int num_blks, i;
custom_block_t *blk;
if (payload) {
num_blks = payload->num_blks;
for (i = 0; i < num_blks; i++) {
blk = payload->blocks[i];
if (blk) {
if (blk->payload)
free(blk->payload);
free(blk);
blk = NULL;
}
}
free(payload);
}
free(codec);
}
__attribute__ ((visibility ("default")))
int plugin_open(bt_codec_t **codec, uint32_t codecFmt, codec_type direction)
{
bt_codec_t *bt_codec = NULL;
bt_codec = calloc(1, sizeof(bt_codec_t));
if (!bt_codec) {
ALOGE("%s: Memory allocation failed\n", __func__);
return -ENOMEM;
}
bt_codec->codecFmt = codecFmt;
bt_codec->direction = direction;
bt_codec->plugin_populate_payload = bt_aptx_populate_payload;
bt_codec->plugin_get_codec_latency = bt_aptx_get_codec_latency;
bt_codec->plugin_query_num_codecs = bt_aptx_query_num_codecs;
bt_codec->close_plugin = bt_aptx_close;
*codec = bt_codec;
return 0;
}

View File

@@ -0,0 +1,125 @@
/*
* Copyright (c) 2020, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _BT_APTX_H_
#define _BT_APTX_H_
#include "bt_intf.h"
#define ENCODER_LATENCY_APTX 40
#define ENCODER_LATENCY_APTX_HD 20
#ifdef SEC_PRODUCT_FEATURE_BLUETOOTH_SUPPORT_A2DP_OFFLOAD
#define DEFAULT_SINK_LATENCY_APTX 110
#else
#define DEFAULT_SINK_LATENCY_APTX 160
#endif
#define DEFAULT_SINK_LATENCY_APTX_HD 180
#define MODULE_ID_APTX_CLASSIC_ENC 0x0700107E
#define MODULE_ID_APTX_CLASSIC_DEC 0x0700107D
#define MODULE_ID_APTX_HD_DEC 0x0700107F
#define MODULE_ID_APTX_HD_ENC 0x07001080
#define MODULE_ID_APTX_ADAPTIVE_ENC 0x07001082
#define MODULE_ID_APTX_ADAPTIVE_SWB_DEC 0x07001083
#define MODULE_ID_APTX_ADAPTIVE_SWB_ENC 0x07001084
#define NUM_CODEC 4
/*
* enums which describes the APTX Adaptive
* channel mode, these values are used by encoder
*/
typedef enum {
APTX_AD_CHANNEL_UNCHANGED = -1,
APTX_AD_CHANNEL_JOINT_STEREO = 0, // default
APTX_AD_CHANNEL_MONO = 1,
APTX_AD_CHANNEL_DUAL_MONO = 2,
APTX_AD_CHANNEL_STEREO_TWS = 4,
APTX_AD_CHANNEL_EARBUD = 8,
} enc_aptx_ad_channel_mode;
/*
* enums which describes the APTX Adaptive
* sampling frequency, these values are used
* by encoder
*/
typedef enum {
APTX_AD_SR_UNCHANGED = 0x0,
APTX_AD_48 = 0x1, // 48 KHz default
APTX_AD_44_1 = 0x2, // 44.1kHz
APTX_AD_96 = 0x3, // 96KHz
} enc_aptx_ad_s_rate;
/* Information about BT APTX encoder configuration
* This data is used between audio HAL module and
* BT IPC library to configure DSP encoder
*/
typedef struct audio_aptx_encoder_config_s {
uint16_t sampling_rate;
uint8_t channels;
uint32_t bitrate;
uint32_t bits_per_sample;
} audio_aptx_encoder_config_t;
typedef struct audio_aptx_hd_encoder_config_s {
uint16_t sampling_rate;
uint8_t channels;
uint32_t bitrate;
uint32_t bits_per_sample;
} audio_aptx_hd_encoder_config_t;
typedef struct audio_aptx_dual_mono_config_s {
uint16_t sampling_rate;
uint8_t channels;
uint32_t bitrate;
uint8_t sync_mode;
} audio_aptx_dual_mono_config_t;
typedef struct audio_aptx_ad_encoder_config_s {
uint32_t sampling_rate;
uint32_t mtu;
int32_t channel_mode;
uint32_t min_sink_modeA;
uint32_t max_sink_modeA;
uint32_t min_sink_modeB;
uint32_t max_sink_modeB;
uint32_t min_sink_modeC;
uint32_t max_sink_modeC;
uint32_t encoder_mode;
uint8_t TTP_modeA_low;
uint8_t TTP_modeA_high;
uint8_t TTP_modeB_low;
uint8_t TTP_modeB_high;
uint8_t TTP_TWS_low;
uint8_t TTP_TWS_high;
uint32_t bits_per_sample;
uint32_t input_mode;
uint32_t fade_duration;
uint8_t sink_cap[11];
} audio_aptx_ad_encoder_config_t;
#endif /* _BT_APTX_H_ */

View File

@@ -0,0 +1,108 @@
/*
* Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define LOG_TAG "PAL: bt_base"
#include "bt_base.h"
#include <common_enc_dec_api.h>
#include <media_fmt_api.h>
#include <errno.h>
#include <stdlib.h>
int bt_base_populate_real_module_id(custom_block_t *blk, int32_t real_module_id)
{
struct param_id_placeholder_real_module_id_t *param = NULL;
blk->param_id = PARAM_ID_REAL_MODULE_ID;
blk->payload_sz = sizeof(struct param_id_placeholder_real_module_id_t);
blk->payload = calloc(1, blk->payload_sz);
if (!blk->payload) {
blk->payload_sz = 0;
return -ENOMEM;
}
param = (struct param_id_placeholder_real_module_id_t*) blk->payload;
param->real_module_id = real_module_id;
return 0;
}
int bt_base_populate_enc_bitrate(custom_block_t *blk, int32_t bit_rate)
{
struct param_id_enc_bitrate_param_t *param = NULL;
blk->param_id = PARAM_ID_ENC_BITRATE;
blk->payload_sz = sizeof(struct param_id_enc_bitrate_param_t);
blk->payload = calloc(1, blk->payload_sz);
if (!blk->payload) {
blk->payload_sz = 0;
return -ENOMEM;
}
param = (struct param_id_enc_bitrate_param_t*) blk->payload;
param->bitrate = bit_rate;
return 0;
}
int bt_base_populate_enc_output_cfg(custom_block_t *blk, uint32_t fmt_id,
void *payload, size_t size)
{
struct param_id_encoder_output_config_t *param = NULL;
void *enc_payload;
blk->param_id = PARAM_ID_ENCODER_OUTPUT_CONFIG;
blk->payload_sz = sizeof(struct param_id_encoder_output_config_t) + size;
blk->payload = (uint8_t *)calloc(1, blk->payload_sz);
if (!blk->payload) {
blk->payload_sz = 0;
return -ENOMEM;
}
param = (struct param_id_encoder_output_config_t *) blk->payload;
param->data_format = DATA_FORMAT_FIXED_POINT;
param->fmt_id = fmt_id;
param->payload_size = size;
enc_payload = (void *)(blk->payload + sizeof(struct param_id_encoder_output_config_t));
memcpy(enc_payload, payload, size);
return 0;
}
int bt_base_populate_enc_cmn_param(custom_block_t *blk, uint32_t param_id,
void *payload, size_t size)
{
blk->param_id = param_id;
blk->payload_sz = size;
blk->payload = (uint8_t *)calloc(1, blk->payload_sz);
if (!blk->payload) {
blk->payload_sz = 0;
return -ENOMEM;
}
memcpy(blk->payload, payload, size);
return 0;
}

View File

@@ -0,0 +1,94 @@
/*
* Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _BT_BASE_H_
#define _BT_BASE_H_
#include "bt_intf.h"
#define CH_MONO 1
#define CH_STEREO 2
#define SAMPLING_RATE_16K 16000 // SS_BT_HFP - H_127 : RVP
#define SAMPLING_RATE_32K 32000
#define SAMPLING_RATE_44K 44100
#define SAMPLING_RATE_48K 48000
#define SAMPLING_RATE_96K 96000
#define ENCODER_BIT_FORMAT_DEFAULT 16
#define ENCODER_BIT_FORMAT_PCM_24 24
enum {
AUDIO_LOCATION_NONE = 0x00000000, // Mono/Unspecified
AUDIO_LOCATION_FRONT_LEFT = 0x00000001,
AUDIO_LOCATION_FRONT_RIGHT = 0x00000002,
AUDIO_LOCATION_FRONT_CENTER = 0x00000004,
AUDIO_LOCATION_LOW_FREQUENCY = 0x00000008,
AUDIO_LOCATION_BACK_LEFT = 0x00000010,
AUDIO_LOCATION_BACK_RIGHT = 0x00000020,
AUDIO_LOCATION_FRONT_LEFT_OF_CENTER = 0x00000040,
AUDIO_LOCATION_FRONT_RIGHT_OF_CENTER = 0x00000080,
AUDIO_LOCATION_BACK_CENTER = 0x00000100,
AUDIO_LOCATION_LOW_FREQUENCY_2 = 0x00000200,
AUDIO_LOCATION_SIDE_LEFT = 0x00000400,
AUDIO_LOCATION_SIDE_RIGHT = 0x00000800,
AUDIO_LOCATION_TOP_FRONT_LEFT = 0x00001000,
AUDIO_LOCATION_TOP_FRONT_RIGHT = 0x00002000,
AUDIO_LOCATION_TOP_FRONT_CENTER = 0x00004000,
AUDIO_LOCATION_TOP_CENTER = 0x00008000,
AUDIO_LOCATION_TOP_BACK_LEFT = 0x00010000,
AUDIO_LOCATION_TOP_BACK_RIGHT = 0x00020000,
AUDIO_LOCATION_TOP_SIDE_LEFT = 0x00040000,
AUDIO_LOCATION_TOP_SIDE_RIGHT = 0x00080000,
AUDIO_LOCATION_TOP_BACK_CENTER = 0x00100000,
AUDIO_LOCATION_BOTTOM_FRONT_CENTER = 0x00200000,
AUDIO_LOCATION_BOTTOM_FRONT_LEFT = 0x00400000,
AUDIO_LOCATION_BOTTOM_FRONT_RIGHT = 0x00800000,
AUDIO_LOCATION_FRONT_LEFT_WIDE = 0x01000000,
AUDIO_LOCATION_FRONT_RIGHT_WIDE = 0x02000000,
AUDIO_LOCATION_LEFT_SURROUND = 0x04000000,
AUDIO_LOCATION_RIGHT_SURROUND = 0x08000000,
AUDIO_LOCATION_RESERVED_1 = 0x10000000,
AUDIO_LOCATION_RESERVED_2 = 0x20000000,
AUDIO_LOCATION_RESERVED_3 = 0x40000000,
AUDIO_LOCATION_RESERVED_4 = 0x80000000,
};
int bt_base_populate_real_module_id(custom_block_t *blk, int32_t real_module_id);
int bt_base_populate_enc_bitrate(custom_block_t *blk, int32_t bit_rate);
int bt_base_populate_enc_output_cfg(custom_block_t *blk, uint32_t fmt_id,
void *payload, size_t size);
int bt_base_populate_enc_cmn_param(custom_block_t *blk, uint32_t param_id,
void *payload, size_t size);
#endif /* _BT_BASE_H_ */

View File

@@ -0,0 +1,458 @@
/*
* Copyright (c) 2021, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Changes from Qualcomm Innovation Center, Inc. are provided under the following license:
*
* Copyright (c) 2023-2024 Qualcomm Innovation Center, Inc. All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#define LOG_TAG "PAL: bt_ble"
//#define LOG_NDEBUG 0
#include <log/log.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "bt_ble.h"
#include <lc3_encoder_api.h>
#include <lc3_decoder_api.h>
static int ble_pack_def_enc_config(bt_codec_t *codec, void *src, void **dst)
{
audio_lc3_codec_cfg_t *ble_bt_cfg = NULL;
bt_enc_payload_t *enc_payload = NULL;
struct param_id_lc3_encoder_config_payload_t *enc_init = NULL;
int ret = 0, num_blks = 1, payload_sz = 0, i = 0;
custom_block_t *blk[1] = {NULL};
ble_bt_cfg = (audio_lc3_codec_cfg_t *)src;
enc_payload = (bt_enc_payload_t *)calloc(1, sizeof(bt_enc_payload_t) +
num_blks * sizeof(custom_block_t *));
if (enc_payload == NULL) {
ALOGE("%s: fail to allocate memory", __func__);
return -ENOMEM;
}
enc_payload->bit_format = def_toair_cfg.bit_depth;
enc_payload->sample_rate = def_toair_cfg.sampling_freq;
enc_payload->num_blks = def_toair_cfg.num_blocks;
enc_payload->channel_count = CH_STEREO;
enc_payload->is_abr_enabled = true;
enc_payload->is_enc_config_set = ble_bt_cfg->is_enc_config_set;
enc_payload->is_dec_config_set = ble_bt_cfg->is_dec_config_set;
for (i = 0; i < num_blks; i++) {
blk[i] = (custom_block_t *)calloc(1, sizeof(custom_block_t));
if (!blk[i]) {
ret = -ENOMEM;
goto free_payload;
}
}
/* populate payload for PARAM_ID_LC3_ENC_INIT */
payload_sz = sizeof(struct param_id_lc3_encoder_config_payload_t) +
sizeof(stream_map_t) * DEF_STREAM_MAP_SZ;
enc_init = (struct param_id_lc3_encoder_config_payload_t *)calloc(1, payload_sz);
if (enc_init == NULL) {
ALOGE("%s: fail to allocate memory", __func__);
ret = -ENOMEM;
goto free_payload;
}
enc_init->stream_map_size = DEF_STREAM_MAP_SZ;
enc_init->toAirConfig.num_blocks = def_toair_cfg.num_blocks;
enc_init->toAirConfig.api_version = def_toair_cfg.api_version;
enc_init->toAirConfig.sampling_Frequency = def_toair_cfg.sampling_freq;
enc_init->toAirConfig.max_octets_per_frame = def_toair_cfg.max_octets_per_frame;
enc_init->toAirConfig.frame_duration = def_toair_cfg.frame_duration;
enc_init->toAirConfig.bit_depth = def_toair_cfg.bit_depth;
enc_init->toAirConfig.mode = def_toair_cfg.mode;
for (i = 0; i < 16; i++) {
enc_init->toAirConfig.vendor_specific[i] =
ble_bt_cfg->enc_cfg.toAirConfig.vendor_specific[i];
}
for (i = 0; i < enc_init->stream_map_size; i++) {
enc_init->streamMapOut[i].audio_location = def_stream_map_out[i].audio_location;
enc_init->streamMapOut[i].stream_id = def_stream_map_out[i].stream_id;
enc_init->streamMapOut[i].direction = def_stream_map_out[i].direction;
}
ret = bt_base_populate_enc_cmn_param(blk[0], PARAM_ID_LC3_ENC_INIT,
enc_init, payload_sz);
free(enc_init);
if (ret)
goto free_payload;
enc_payload->blocks[0] = blk[0];
*dst = enc_payload;
codec->payload = enc_payload;
return ret;
free_payload:
for (i = 0; i < num_blks; i++) {
if (blk[i]) {
if (blk[i]->payload)
free(blk[i]->payload);
free(blk[i]);
}
}
if (enc_payload)
free(enc_payload);
return ret;
}
static int ble_pack_enc_config(bt_codec_t *codec, void *src, void **dst)
{
audio_lc3_codec_cfg_t *ble_bt_cfg = NULL;
bt_enc_payload_t *enc_payload = NULL;
struct param_id_lc3_encoder_config_payload_t *enc_init = NULL;
int ret = 0, num_blks = 1, payload_sz = 0, i = 0;
custom_block_t *blk[1] = {NULL};
ALOGV("%s", __func__);
if ((src == NULL) || (dst == NULL)) {
ALOGE("%s: invalid input parameters", __func__);
return -EINVAL;
}
ble_bt_cfg = (audio_lc3_codec_cfg_t *)src;
if (!ble_bt_cfg->is_enc_config_set) {
ALOGI("%s: is_enc_config_set is false; setting default enc params",
__func__);
return ble_pack_def_enc_config(codec, src, dst);
}
enc_payload = (bt_enc_payload_t *)calloc(1, sizeof(bt_enc_payload_t) +
num_blks * sizeof(custom_block_t *));
if (enc_payload == NULL) {
ALOGE("%s: fail to allocate memory", __func__);
return -ENOMEM;
}
enc_payload->bit_format = ble_bt_cfg->enc_cfg.toAirConfig.bit_depth;
enc_payload->sample_rate = ble_bt_cfg->enc_cfg.toAirConfig.sampling_freq;
enc_payload->num_blks = num_blks;
if (ble_bt_cfg->enc_cfg.stream_map_size) {
if ((1 == ble_bt_cfg->enc_cfg.stream_map_size) &&
(ble_bt_cfg->enc_cfg.streamMapOut[0].audio_location != 3))
enc_payload->channel_count = CH_MONO;
else
enc_payload->channel_count = CH_STEREO;
}
enc_payload->is_abr_enabled = true;
enc_payload->is_enc_config_set = ble_bt_cfg->is_enc_config_set;
enc_payload->is_dec_config_set = ble_bt_cfg->is_dec_config_set;
for (i = 0; i < num_blks; i++) {
blk[i] = (custom_block_t *)calloc(1, sizeof(custom_block_t));
if (!blk[i]) {
ret = -ENOMEM;
goto free_payload;
}
}
/* populate payload for PARAM_ID_LC3_ENC_INIT */
payload_sz = sizeof(struct param_id_lc3_encoder_config_payload_t) +
sizeof(stream_map_t) * ble_bt_cfg->enc_cfg.stream_map_size;
enc_init = (struct param_id_lc3_encoder_config_payload_t *)calloc(1, payload_sz);
if (enc_init == NULL) {
ALOGE("%s: fail to allocate memory", __func__);
ret = -ENOMEM;
goto free_payload;
}
enc_init->stream_map_size = ble_bt_cfg->enc_cfg.stream_map_size;
enc_init->toAirConfig.api_version = ble_bt_cfg->enc_cfg.toAirConfig.api_version;
enc_init->toAirConfig.sampling_Frequency = ble_bt_cfg->enc_cfg.toAirConfig.sampling_freq;
enc_init->toAirConfig.max_octets_per_frame = ble_bt_cfg->enc_cfg.toAirConfig.max_octets_per_frame;
enc_init->toAirConfig.frame_duration = ble_bt_cfg->enc_cfg.toAirConfig.frame_duration;
enc_init->toAirConfig.bit_depth = ble_bt_cfg->enc_cfg.toAirConfig.bit_depth;
enc_init->toAirConfig.num_blocks = ble_bt_cfg->enc_cfg.toAirConfig.num_blocks;
enc_init->toAirConfig.default_q_level = ble_bt_cfg->enc_cfg.toAirConfig.default_q_level;
enc_init->toAirConfig.mode = ble_bt_cfg->enc_cfg.toAirConfig.mode;
for (i = 0; i < 16; i++) {
enc_init->toAirConfig.vendor_specific[i] =
ble_bt_cfg->enc_cfg.toAirConfig.vendor_specific[i];
if (i == VERSION_IDX)
enc_payload->codec_version = enc_init->toAirConfig.vendor_specific[i];
}
for (i = 0; i < enc_init->stream_map_size; i++) {
enc_init->streamMapOut[i].audio_location = ble_bt_cfg->enc_cfg.streamMapOut[i].audio_location;
enc_init->streamMapOut[i].stream_id = ble_bt_cfg->enc_cfg.streamMapOut[i].stream_id;
enc_init->streamMapOut[i].direction = ble_bt_cfg->enc_cfg.streamMapOut[i].direction;
}
ret = bt_base_populate_enc_cmn_param(blk[0], PARAM_ID_LC3_ENC_INIT,
enc_init, payload_sz);
free(enc_init);
if (ret)
goto free_payload;
enc_payload->blocks[0] = blk[0];
*dst = enc_payload;
codec->payload = enc_payload;
return ret;
free_payload:
for (i = 0; i < num_blks; i++) {
if (blk[i]) {
if (blk[i]->payload)
free(blk[i]->payload);
free(blk[i]);
}
}
if (enc_payload)
free(enc_payload);
return ret;
}
static int ble_pack_dec_config(bt_codec_t *codec, void *src, void **dst)
{
audio_lc3_codec_cfg_t *ble_bt_cfg = NULL;
bt_enc_payload_t *enc_payload = NULL;
struct param_id_lc3_decoder_config_payload_t *dec_init = NULL;
int ret = 0, num_blks = 1, payload_sz = 0, i = 0;
custom_block_t *blk[1] = {NULL};
ALOGV("%s", __func__);
if ((src == NULL) || (dst == NULL)) {
ALOGE("%s: invalid input parameters", __func__);
return -EINVAL;
}
ble_bt_cfg = (audio_lc3_codec_cfg_t *)src;
enc_payload = (bt_enc_payload_t *)calloc(1, sizeof(bt_enc_payload_t) +
num_blks * sizeof(custom_block_t *));
if (enc_payload == NULL) {
ALOGE("%s: fail to allocate memory", __func__);
return -ENOMEM;
}
enc_payload->bit_format = ble_bt_cfg->dec_cfg.fromAirConfig.bit_depth;
enc_payload->sample_rate = ble_bt_cfg->dec_cfg.fromAirConfig.sampling_freq;
enc_payload->channel_count = ble_bt_cfg->dec_cfg.decoder_output_channel;
enc_payload->num_blks = num_blks;
enc_payload->is_abr_enabled = true;
enc_payload->is_enc_config_set = ble_bt_cfg->is_enc_config_set;
enc_payload->is_dec_config_set = ble_bt_cfg->is_dec_config_set;
for (i = 0; i < num_blks; i++) {
blk[i] = (custom_block_t *)calloc(1, sizeof(custom_block_t));
if (!blk[i]) {
ret = -ENOMEM;
goto free_payload;
}
}
/* populate payload for PARAM_ID_LC3_DEC_INIT */
payload_sz = sizeof(struct param_id_lc3_decoder_config_payload_t) +
sizeof(stream_map_t) * ble_bt_cfg->dec_cfg.stream_map_size;
dec_init = (struct param_id_lc3_decoder_config_payload_t *)calloc(1, payload_sz);
if (dec_init == NULL) {
ALOGE("%s: fail to allocate memory", __func__);
ret = -ENOMEM;
goto free_payload;
}
dec_init->decoder_output_channel = ble_bt_cfg->dec_cfg.decoder_output_channel;
dec_init->stream_map_size = ble_bt_cfg->dec_cfg.stream_map_size;
dec_init->fromAirConfig.api_version = ble_bt_cfg->dec_cfg.fromAirConfig.api_version;
dec_init->fromAirConfig.sampling_Frequency = ble_bt_cfg->dec_cfg.fromAirConfig.sampling_freq;
dec_init->fromAirConfig.max_octets_per_frame = ble_bt_cfg->dec_cfg.fromAirConfig.max_octets_per_frame;
dec_init->fromAirConfig.frame_duration = ble_bt_cfg->dec_cfg.fromAirConfig.frame_duration;
dec_init->fromAirConfig.bit_depth = ble_bt_cfg->dec_cfg.fromAirConfig.bit_depth;
dec_init->fromAirConfig.num_blocks = ble_bt_cfg->dec_cfg.fromAirConfig.num_blocks;
dec_init->fromAirConfig.default_q_level = ble_bt_cfg->dec_cfg.fromAirConfig.default_q_level;
dec_init->fromAirConfig.mode = ble_bt_cfg->dec_cfg.fromAirConfig.mode;
for (i = 0; i < 16; i++) {
dec_init->fromAirConfig.vendor_specific[i] =
ble_bt_cfg->dec_cfg.fromAirConfig.vendor_specific[i];
if (i == VERSION_IDX)
enc_payload->codec_version = dec_init->fromAirConfig.vendor_specific[i];
}
for (i = 0; i < dec_init->stream_map_size; i++) {
dec_init->streamMapIn[i].audio_location = ble_bt_cfg->dec_cfg.streamMapIn[i].audio_location;
dec_init->streamMapIn[i].stream_id = ble_bt_cfg->dec_cfg.streamMapIn[i].stream_id;
dec_init->streamMapIn[i].direction = ble_bt_cfg->dec_cfg.streamMapIn[i].direction;
}
ret = bt_base_populate_enc_cmn_param(blk[0], PARAM_ID_LC3_DEC_INIT,
dec_init, payload_sz);
free(dec_init);
if (ret)
goto free_payload;
enc_payload->blocks[0] = blk[0];
*dst = enc_payload;
codec->payload = enc_payload;
return ret;
free_payload:
for (i = 0; i < num_blks; i++) {
if (blk[i]) {
if (blk[i]->payload)
free(blk[i]->payload);
free(blk[i]);
}
}
if (enc_payload)
free(enc_payload);
return ret;
}
static int bt_ble_populate_payload(bt_codec_t *codec, void *src, void **dst)
{
config_fn_t config_fn = NULL;
if (codec->payload)
free(codec->payload);
switch (codec->codecFmt) {
case CODEC_TYPE_LC3:
case CODEC_TYPE_APTX_AD_R4:
config_fn = ((codec->direction == ENC) ? &ble_pack_enc_config :
&ble_pack_dec_config);
break;
case CODEC_TYPE_APTX_AD_QLEA:
config_fn = ((codec->direction == ENC) ? &ble_pack_enc_config :
NULL);
break;
default:
ALOGD("%s unsupported codecFmt %d\n", __func__, codec->codecFmt);
}
if (config_fn != NULL) {
return config_fn(codec, src, dst);
}
return -EINVAL;
}
static uint64_t bt_ble_get_decoder_latency(bt_codec_t *codec)
{
uint32_t latency = 0;
switch (codec->codecFmt) {
case CODEC_TYPE_LC3:
latency = 0;
break;
default:
latency = 200;
ALOGD("No valid decoder defined, setting latency to %dms", latency);
break;
}
return (uint64_t)latency;
}
static uint64_t bt_ble_get_encoder_latency(bt_codec_t *codec)
{
uint32_t latency = 0;
switch (codec->codecFmt) {
case CODEC_TYPE_LC3:
case CODEC_TYPE_APTX_AD_QLEA:
case CODEC_TYPE_APTX_AD_R4:
/* for BLE, the latency depends on the mode (HQ/LL) and
* BT IPC will take care of accomodating the mode factor and return latency
*/
latency = 0;
break;
default:
latency = 200;
break;
}
ALOGV("%s: codecFmt %u, direction %d, latency %u",
__func__, codec->codecFmt, codec->direction, latency);
return latency;
}
static uint64_t bt_ble_get_codec_latency(bt_codec_t *codec)
{
if (codec->direction == ENC)
return bt_ble_get_encoder_latency(codec);
else
return bt_ble_get_decoder_latency(codec);
}
int bt_ble_query_num_codecs(bt_codec_t *codec __unused) {
ALOGV("%s", __func__);
return NUM_CODEC;
}
void bt_ble_close(bt_codec_t *codec)
{
bt_enc_payload_t *payload = codec->payload;
int num_blks, i;
custom_block_t *blk;
if (payload) {
num_blks = payload->num_blks;
for (i = 0; i < num_blks; i++) {
blk = payload->blocks[i];
if (blk) {
if (blk->payload)
free(blk->payload);
free(blk);
blk = NULL;
}
}
free(payload);
}
free(codec);
}
__attribute__ ((visibility ("default")))
int plugin_open(bt_codec_t **codec, uint32_t codecFmt, codec_type direction)
{
bt_codec_t *bt_codec = NULL;
bt_codec = calloc(1, sizeof(bt_codec_t));
if (!bt_codec) {
ALOGE("%s: Memory allocation failed\n", __func__);
return -ENOMEM;
}
bt_codec->codecFmt = codecFmt;
bt_codec->direction = direction;
bt_codec->plugin_populate_payload = bt_ble_populate_payload;
bt_codec->plugin_get_codec_latency = bt_ble_get_codec_latency;
bt_codec->plugin_query_num_codecs = bt_ble_query_num_codecs;
bt_codec->close_plugin = bt_ble_close;
*codec = bt_codec;
return 0;
}

View File

@@ -0,0 +1,204 @@
/*
* Copyright (c) 2021, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Changes from Qualcomm Innovation Center, Inc. are provided under the following license:
*
* Copyright (c) 2023-2024 Qualcomm Innovation Center, Inc. All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#ifndef _BT_BLE_H_
#define _BT_BLE_H_
#include "bt_intf.h"
#include "bt_base.h"
#define NUM_CODEC 2
#define AUDIO_LOCATION_MAX 28
#define VERSION_IDX 7
/* Information about BT LC3 encoder configuration
* This data is used between audio HAL module and
* BT IPC library to configure DSP encoder
*/
typedef struct lc3_cfg_s {
uint32_t api_version;
uint32_t sampling_freq;
uint32_t max_octets_per_frame;
uint32_t frame_duration; // 7.5msec, 10msec
uint32_t bit_depth;
uint32_t num_blocks;
uint8_t default_q_level;
uint8_t vendor_specific[16]; // this indicates LC3/LC3Q. 0 => LC3 1.0 , 1 => LC3 1.1
uint32_t mode;
} lc3_cfg_t;
typedef struct lc3_stream_map_s {
uint32_t audio_location;
uint8_t stream_id;
uint8_t direction;
} lc3_stream_map_t;
typedef struct lc3_encoder_cfg_s {
lc3_cfg_t toAirConfig;
uint8_t stream_map_size;
uint8_t actual_stream_map_size; // SS_BT_LEA - A_041 : Stereo audio for SingleDev_OneChanStereoSnk
lc3_stream_map_t *streamMapOut;
} lc3_encoder_cfg_t;
typedef struct lc3_decoder_cfg_s {
lc3_cfg_t fromAirConfig;
uint32_t decoder_output_channel;
uint8_t stream_map_size;
lc3_stream_map_t *streamMapIn;
} lc3_decoder_cfg_t;
typedef struct audio_lc3_codec_cfg_s {
lc3_encoder_cfg_t enc_cfg;
lc3_decoder_cfg_t dec_cfg;
bool is_enc_config_set;
bool is_dec_config_set;
} audio_lc3_codec_cfg_t;
static uint32_t audio_location_map_array[] = {
AUDIO_LOCATION_FRONT_LEFT,
AUDIO_LOCATION_FRONT_RIGHT,
AUDIO_LOCATION_FRONT_CENTER,
AUDIO_LOCATION_LOW_FREQUENCY,
AUDIO_LOCATION_BACK_LEFT,
AUDIO_LOCATION_BACK_RIGHT,
AUDIO_LOCATION_FRONT_LEFT_OF_CENTER,
AUDIO_LOCATION_FRONT_RIGHT_OF_CENTER,
AUDIO_LOCATION_BACK_CENTER,
AUDIO_LOCATION_LOW_FREQUENCY_2,
AUDIO_LOCATION_SIDE_LEFT,
AUDIO_LOCATION_SIDE_RIGHT,
AUDIO_LOCATION_TOP_FRONT_LEFT,
AUDIO_LOCATION_TOP_FRONT_RIGHT,
AUDIO_LOCATION_TOP_FRONT_CENTER,
AUDIO_LOCATION_TOP_CENTER,
AUDIO_LOCATION_TOP_BACK_LEFT,
AUDIO_LOCATION_TOP_BACK_RIGHT,
AUDIO_LOCATION_TOP_SIDE_LEFT,
AUDIO_LOCATION_TOP_SIDE_RIGHT,
AUDIO_LOCATION_TOP_BACK_CENTER,
AUDIO_LOCATION_BOTTOM_FRONT_CENTER,
AUDIO_LOCATION_BOTTOM_FRONT_LEFT,
AUDIO_LOCATION_BOTTOM_FRONT_RIGHT,
AUDIO_LOCATION_FRONT_LEFT_WIDE,
AUDIO_LOCATION_FRONT_RIGHT_WIDE,
AUDIO_LOCATION_LEFT_SURROUND,
AUDIO_LOCATION_RIGHT_SURROUND
};
static int channel_map_array[] = {
PCM_CHANNEL_L,
PCM_CHANNEL_R,
PCM_CHANNEL_C,
PCM_CHANNEL_LFE,
PCM_CHANNEL_LB,
PCM_CHANNEL_RB,
PCM_CHANNEL_FLC,
PCM_CHANNEL_FRC,
PCM_CHANNEL_CB,
PCM_CHANNEL_RS,
PCM_CHANNEL_SL,
PCM_CHANNEL_SR,
PCM_CHANNEL_TFL,
PCM_CHANNEL_TFR,
PCM_CHANNEL_TFC,
PCM_CHANNEL_TC,
PCM_CHANNEL_TBL,
PCM_CHANNEL_TBR,
PCM_CHANNEL_TSL,
PCM_CHANNEL_TSR,
PCM_CHANNEL_TBC,
PCM_CHANNEL_BFC,
PCM_CHANNEL_BFL,
PCM_CHANNEL_BFR,
PCM_CHANNEL_LW,
PCM_CHANNEL_RW,
PCM_CHANNEL_LS,
PCM_CHANNEL_RS
};
__attribute__((unused))
static uint64_t convert_channel_map(uint32_t audio_location)
{
int i;
uint64_t channel_mask = (uint64_t) 0x00000000;
if (!audio_location) {
channel_mask |= 1ULL << PCM_CHANNEL_C;
return channel_mask;
}
for (i = 0; i < AUDIO_LOCATION_MAX; i++) {
if (audio_location & audio_location_map_array[i])
channel_mask |= 1ULL << channel_map_array[i];
}
return channel_mask;
}
struct codec_specific_config {
uint32_t sampling_freq;
uint32_t frame_duration;
uint32_t max_octets_per_frame;
uint32_t bit_depth;
};
#define LC3_CSC_TBL_SIZE 6
static struct codec_specific_config LC3_CSC[LC3_CSC_TBL_SIZE] = {
{8000, 7500, 26, 24},
{8000, 10000, 30, 24},
{16000, 7500, 30, 24},
{16000, 10000, 40, 24},
{32000, 7500, 60, 24},
{32000, 10000, 80, 24},
};
#define DEF_STREAM_MAP_SZ 2
static lc3_stream_map_t def_stream_map_out[DEF_STREAM_MAP_SZ] = {
{2, 0, 0},
{1, 1, 0},
};
static lc3_cfg_t def_toair_cfg = {
33,
16000,
40,
10000,
24,
1,
0,
{0},
1,
};
#endif /* _BT_BLE_H_ */

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,206 @@
/*
* Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _BT_BUNDLE_H_
#define _BT_BUNDLE_H_
#include "bt_intf.h"
#define ENCODER_LATENCY_SBC 10
#define ENCODER_LATENCY_AAC 70
#define ENCODER_LATENCY_CELT 40
#define ENCODER_LATENCY_LDAC 40
#ifdef SEC_PRODUCT_FEATURE_BLUETOOTH_SUPPORT_A2DP_OFFLOAD
#define ENCODER_LATENCY_SSC 10
#define DEFAULT_SINK_LATENCY_SBC 100
#define DEFAULT_SINK_LATENCY_SSC 160
#else
#define DEFAULT_SINK_LATENCY_SBC 140
#endif
#define DEFAULT_SINK_LATENCY_AAC 180
#define DEFAULT_SINK_LATENCY_CELT 180
#define DEFAULT_SINK_LATENCY_LDAC 180
#define MODULE_ID_AAC_ENC 0x0700101E
#define MODULE_ID_AAC_DEC 0x0700101F
#define MODULE_ID_SBC_DEC 0x07001039
#define MODULE_ID_SBC_ENC 0x0700103A
#define MODULE_ID_LDAC_ENC 0x0700107A
#define MODULE_ID_CELT_ENC 0x07001090
#ifdef SEC_PRODUCT_FEATURE_BLUETOOTH_SUPPORT_A2DP_OFFLOAD
#define MODULE_ID_SS_SBC_ENC 0x10010BF5
#define MODULE_ID_SSC_ENC 0x10010BF3
#define NUM_CODEC 5
#define PCM_CHANNEL_L 1
#define PCM_CHANNEL_R 2
#define PCM_CHANNEL_C 3
#else
#define NUM_CODEC 4
#endif
/* Information about BT AAC encoder configuration
* This data is used between audio HAL module and
* BT IPC library to configure DSP encoder
*/
#define MAX_ABR_QUALITY_LEVELS 5
typedef struct bit_rate_level_map_s {
uint32_t link_quality_level;
uint32_t bitrate;
} bit_rate_level_map_t;
struct quality_level_to_bitrate_info {
uint32_t num_levels;
bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS];
};
/* Structure to control frame size of AAC encoded frames. */
enum {
MTU_SIZE = 0,
PEAK_BIT_RATE,
BIT_RATE_MODE,
};
struct aac_frame_size_control_t {
/* Type of frame size control: MTU_SIZE / PEAK_BIT_RATE*/
uint32_t ctl_type;
/* Control value
* MTU_SIZE: MTU size in bytes
* PEAK_BIT_RATE: Peak bitrate in bits per second.
* BIT_RATE_MODE: Bit rate mode such as CBR or VBR
*/
uint32_t ctl_value;
};
struct aac_abr_control_t {
bool is_abr_enabled;
struct quality_level_to_bitrate_info level_to_bitrate_map;
};
typedef struct audio_aac_encoder_config_s {
uint32_t enc_mode; /* LC, SBR, PS */
uint16_t format_flag; /* RAW, ADTS */
uint16_t channels; /* 1-Mono, 2-Stereo */
uint32_t sampling_rate;
uint32_t bitrate;
uint32_t bits_per_sample;
struct aac_frame_size_control_t frame_ctl;
uint8_t size_control_struct;
struct aac_frame_size_control_t* frame_ctl_ptr;
uint8_t abr_size_control_struct;
struct aac_abr_control_t* abr_ctl_ptr;
} audio_aac_encoder_config_t;
/* Information about BT SBC encoder configuration
* This data is used between audio HAL module and
* BT IPC library to configure DSP encoder
*/
typedef struct audio_sbc_encoder_config_s {
uint32_t subband; /* 4, 8 */
uint32_t blk_len; /* 4, 8, 12, 16 */
uint16_t sampling_rate; /* 44.1khz,48khz */
uint8_t channels; /* 0(Mono),1(Dual_mono),2(Stereo),3(JS) */
uint8_t alloc; /* 0(Loudness),1(SNR) */
uint8_t min_bitpool; /* 2 */
uint8_t max_bitpool; /* 53(44.1khz),51 (48khz) */
uint32_t bitrate; /* 320kbps to 512kbps */
uint32_t bits_per_sample;
} audio_sbc_encoder_config_t;
/* Information about BT CELT encoder configuration
* This data is used between audio HAL module and
* BT IPC library to configure DSP encoder
*/
typedef struct audio_celt_encoder_config_s {
uint32_t sampling_rate; /* 32000 - 48000, 48000 */
uint16_t channels; /* 1-Mono, 2-Stereo, 2 */
uint16_t frame_size; /* 64-128-256-512, 512 */
uint16_t complexity; /* 0-10, 1 */
uint16_t prediction_mode; /* 0-1-2, 0 */
uint16_t vbr_flag; /* 0-1, 0 */
uint32_t bitrate; /* 32000 - 1536000, 139500 */
uint32_t bits_per_sample;
} audio_celt_encoder_config_t;
/* Information about BT LDAC encoder configuration
* This data is used between audio HAL module and
* BT IPC library to configure DSP encoder
*/
typedef struct audio_ldac_encoder_config_s {
uint32_t sampling_rate; /* 44100,48000,88200,96000 */
uint32_t bit_rate; /* 303000,606000,909000(in bits per second) */
uint16_t channel_mode; /* 0, 4, 2, 1 */
uint16_t mtu; /* 679 */
uint32_t bits_per_sample;
bool is_abr_enabled;
struct quality_level_to_bitrate_info level_to_bitrate_map;
} audio_ldac_encoder_config_t;
#ifdef SEC_PRODUCT_FEATURE_BLUETOOTH_SUPPORT_A2DP_OFFLOAD
/* Information about BT SS SBC encoder configuration
* This data is used between audio HAL module and
* BT IPC library to configure DSP encoder
*/
typedef struct audio_ss_sbc_encoder_config_s {
uint32_t subband; /* 4, 8 */
uint32_t blk_len; /* 4, 8, 12, 16 */
uint16_t sampling_rate; /* 44.1khz,48khz */
uint8_t channels; /* 0(Mono),1(Dual_mono),2(Stereo),3(JS) */
uint8_t alloc; /* 0(Loudness),1(SNR) */
uint8_t min_bitpool; /* 2 */
uint8_t max_bitpool; /* 53(44.1khz),51 (48khz) */
uint32_t bitrate; /* 320kbps to 512kbps */
uint32_t bits_per_sample;
bool is_abr_enabled;
#ifdef QCA_OFFLOAD
struct quality_level_to_bitrate_info level_to_bitrate_map;
#endif
} audio_ss_sbc_encoder_config_t;
// [ADD_FOR_SAMSUNG - For samsung bt codec
/* Information about BT SCALABLE encoder configuration
* This data is used between audio HAL module and
* BT IPC library to configure DSP encoder
*/
typedef struct audio_ssc_encoder_config_s {
uint32_t sampling_rate;
uint8_t channels;
uint32_t bitrate;
uint32_t bits_per_sample;
bool is_abr_enabled;
#ifdef QCA_OFFLOAD
struct quality_level_to_bitrate_info level_to_bitrate_map;
#endif
} audio_ssc_encoder_config_t;
enum SS_CodecType : uint32_t {
SSC = 0x20,
};
#endif
#endif /* _BT_PLUGIN_BUNDLE_H_ */

View File

@@ -0,0 +1,163 @@
/*
* Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Changes from Qualcomm Innovation Center are provided under the following license:
*
* Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#ifndef _BT_PLUGIN_INTF_H_
#define _BT_PLUGIN_INTF_H_
// import data definition expected from Spf
#include <ar_osal_types.h>
#include <common_enc_dec_api.h>
#include <media_fmt_api.h>
#include "SecProductFeature_BLUETOOTH.h"
/*
* Below enum values are extended from audio_base.h to
* to keep encoder and decoder type local to bthost_ipc
* and audio_hal as these are intended only for handshake
* between IPC lib and Audio HAL.
*/
typedef enum {
CODEC_TYPE_INVALID = 0xFFFFFFFFu,
CODEC_TYPE_AAC = 0x04000000u,
CODEC_TYPE_SBC = 0x1F000000u,
CODEC_TYPE_APTX = 0x20000000u,
CODEC_TYPE_APTX_HD = 0x21000000u,
CODEC_TYPE_APTX_DUAL_MONO = 0x22000000u,
CODEC_TYPE_LDAC = 0x23000000u,
CODEC_TYPE_CELT = 0x24000000u,
CODEC_TYPE_APTX_AD = 0x25000000u,
CODEC_TYPE_APTX_AD_SPEECH = 0x26000000u,
CODEC_TYPE_RVP = 0x28000000u, // SS_BT_HFP - H_127 : RVP
CODEC_TYPE_LC3 = 0x2B000000u,
CODEC_TYPE_PCM = 0x1u,
CODEC_TYPE_APTX_AD_QLEA = 0x30000000u,
CODEC_TYPE_APTX_AD_R4 = 0x31000000u,
#ifdef SEC_PRODUCT_FEATURE_BLUETOOTH_SUPPORT_A2DP_OFFLOAD
CODEC_TYPE_SSC = 0x27000000UL,
#endif
} codec_format_t;
/*
* Update the below lookup table as well when new codec formats get added
* This will be used by Payload Builder to popualte the Graph KVs
*/
#ifdef __cplusplus
#include <map>
#include <string>
const std::map<uint32_t, std::string> btCodecFormatLUT {
{CODEC_TYPE_INVALID, std::string{ "CODEC_TYPE_INVALID"} },
{CODEC_TYPE_AAC, std::string{ "CODEC_TYPE_AAC"} },
{CODEC_TYPE_SBC, std::string{ "CODEC_TYPE_SBC"} },
{CODEC_TYPE_APTX, std::string{ "CODEC_TYPE_APTX"} },
{CODEC_TYPE_APTX_HD, std::string{ "CODEC_TYPE_APTX_HD"} },
{CODEC_TYPE_APTX_DUAL_MONO, std::string{ "CODEC_TYPE_APTX_DUAL_MONO"} },
{CODEC_TYPE_LDAC, std::string{ "CODEC_TYPE_LDAC"} },
{CODEC_TYPE_CELT, std::string{ "CODEC_TYPE_CELT"} },
{CODEC_TYPE_APTX_AD, std::string{ "CODEC_TYPE_APTX_AD"} },
{CODEC_TYPE_APTX_AD_SPEECH, std::string{ "CODEC_TYPE_APTX_AD_SPEECH"} },
{CODEC_TYPE_RVP, std::string{ "CODEC_TYPE_RVP"} }, // SS_BT_HFP - H_127 : RVP
{CODEC_TYPE_LC3, std::string{ "CODEC_TYPE_LC3"} },
{CODEC_TYPE_PCM, std::string{ "CODEC_TYPE_PCM"} },
{CODEC_TYPE_APTX_AD_QLEA, std::string{ "CODEC_TYPE_APTX_AD_QLEA"} },
{CODEC_TYPE_APTX_AD_R4, std::string{ "CODEC_TYPE_APTX_AD_R4"} },
#ifdef SEC_PRODUCT_FEATURE_BLUETOOTH_SUPPORT_A2DP_OFFLOAD
{CODEC_TYPE_SSC, std::string{ "CODEC_TYPE_SSC"} },
#endif
};
#endif
typedef enum {
ENC = 1,
DEC
} codec_type;
typedef enum {
V1=1,
V2
}codec_version_t;
#ifdef SEC_PRODUCT_FEATURE_BLUETOOTH_SUPPORT_A2DP_OFFLOAD
#define DEFAULT_SINK_LATENCY_SBC 100
#define DEFAULT_SINK_LATENCY_SSC 160
#else
#define DEFAULT_SINK_LATENCY_SBC 140
#endif
#define DEFAULT_SINK_LATENCY_AAC 180
#define DEFAULT_SINK_LATENCY_CELT 180
#define DEFAULT_SINK_LATENCY_LDAC 180
#ifdef SEC_PRODUCT_FEATURE_BLUETOOTH_SUPPORT_A2DP_OFFLOAD
#define DEFAULT_SINK_LATENCY_APTX 110
#else
#define DEFAULT_SINK_LATENCY_APTX 160
#endif
#define DEFAULT_SINK_LATENCY_APTX_HD 180
#define DEFAULT_SINK_LATENCY 200
/* encoder payload sent for BT device */
typedef struct custom_block_s {
uint32_t param_id;
uint32_t payload_sz;
uint8_t *payload;
} custom_block_t;
typedef struct bt_enc_payload {
uint32_t channel_count;
uint32_t bit_format;
uint32_t sample_rate;
bool is_abr_enabled;
uint32_t num_blks;
bool is_enc_config_set;
bool is_dec_config_set;
codec_version_t codec_version;
custom_block_t *blocks[];
} bt_enc_payload_t;
typedef struct bt_codec_library_s bt_codec_t;
struct bt_codec_library_s {
const char *name;
uint32_t codecFmt;
codec_type direction;
bt_enc_payload_t *payload;
int (*plugin_populate_payload)(bt_codec_t *codec, void *src, void **dst);
uint64_t (*plugin_get_codec_latency)(bt_codec_t *codec);
int (*plugin_query_num_codecs)(bt_codec_t *codec);
void (*close_plugin) (bt_codec_t *codec);
};
typedef int (*config_fn_t) (bt_codec_t *codec, void *src, void **dst);
typedef int (*open_fn_t) (bt_codec_t **codec, uint32_t codecFmt, codec_type direction);
#endif /* _BT_PLUGIN_INTF_H_ */

View File

@@ -0,0 +1,33 @@
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libcustomva_intf
LOCAL_MODULE_OWNER := qti
LOCAL_VENDOR_MODULE := true
LOCAL_CPPFLAGS += -fexceptions
LOCAL_SRC_FILES := \
src/CustomVAInterface.cpp
LOCAL_C_INCLUDES += \
$(LOCAL_PATH)/inc
# { SEC_AUDIO_COMMON
LOCAL_C_INCLUDES += $(TOP)/system/media/audio/include
# } SEC_AUDIO_COMMON
LOCAL_HEADER_LIBRARIES := \
libarpal_headers \
libarvui_intf_headers \
libspf-headers \
liblisten_headers \
libarosal_headers
LOCAL_SHARED_LIBRARIES := \
liblog \
libcutils \
liblx-osal
include $(BUILD_SHARED_LIBRARY)

View File

@@ -0,0 +1,127 @@
/*
* Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#ifndef CUSTOM_VA_INTERFACE_H
#define CUSTOM_VA_INTERFACE_H
#include "VoiceUIInterface.h"
#include "detection_cmn_api.h"
#include "ar_osal_mem_op.h"
class CustomVAInterface: public VoiceUIInterface {
public:
CustomVAInterface(vui_intf_param_t *model);
~CustomVAInterface();
void DetachStream(void *stream) override;
int32_t SetParameter(intf_param_id_t param_id,
vui_intf_param_t *param) override;
int32_t GetParameter(intf_param_id_t param_id,
vui_intf_param_t *param) override;
int32_t Process(intf_process_id_t type,
vui_intf_param_t *in_out_param) override;
int32_t RegisterModel(void *s,
struct pal_st_sound_model *model,
const std::vector<sound_model_data_t *> model_list) override;
void DeregisterModel(void *s) override;
private:
static int32_t ParseSoundModel(struct pal_st_sound_model *sound_model,
std::vector<sound_model_data_t *> &model_list);
int32_t ParseRecognitionConfig(void *s,
struct pal_st_recognition_config *config);
void GetBufferingConfigs(void *s,
struct buffer_config *config);
void GetSecondStageConfLevels(void *s,
listen_model_indicator_enum type,
uint32_t *level);
void SetSecondStageDetLevels(void *s,
listen_model_indicator_enum type,
uint32_t level);
int32_t ParseDetectionPayload(void *event, uint32_t size);
void* GetDetectedStream();
void* GetDetectionEventInfo();
int32_t GenerateCallbackEvent(void *s,
struct pal_st_recognition_event **event,
uint32_t *event_size);
void ProcessLab(void *data, uint32_t size) {}
void UpdateFTRTData(void *data, uint32_t size) {}
int32_t ParseOpaqueConfLevels(struct sound_model_info *info,
void *opaque_conf_levels,
uint32_t version,
uint8_t **out_conf_levels,
uint32_t *out_num_conf_levels);
int32_t FillConfLevels(struct sound_model_info *info,
struct pal_st_recognition_config *config,
uint8_t **out_conf_levels,
uint32_t *out_num_conf_levels);
int32_t FillOpaqueConfLevels(uint32_t model_id,
const void *sm_levels_generic,
uint8_t **out_payload,
uint32_t *out_payload_size,
uint32_t version);
int32_t ParseDetectionPayloadPDK(void *event_data);
int32_t ParseDetectionPayloadGMM(void *event_data);
void UpdateKeywordIndex(uint64_t kwd_start_timestamp,
uint64_t kwd_end_timestamp,
uint64_t ftrt_start_timestamp);
void PackEventConfLevels(struct sound_model_info *sm_info,
uint8_t *opaque_data);
void FillCallbackConfLevels(struct sound_model_info *sm_info,
uint8_t *opaque_data,
uint32_t det_keyword_id,
uint32_t best_conf_level);
void CheckAndSetDetectionConfLevels(void *s);
void UpdateDetectionResult(void *s, uint32_t result);
int32_t GetSoundModelLoadPayload(vui_intf_param_t *param);
int32_t GetCustomPayload(vui_intf_param_t *param);
int32_t GetBufferingPayload(vui_intf_param_t *param);
void SetStreamAttributes(struct pal_stream_attributes *attr);
void SetSTModuleType(st_module_type_t model_type) {
module_type_ = model_type;
}
st_module_type_t GetModuleType(void *s) { return module_type_; }
void GetKeywordIndex(struct keyword_index *index);
uint32_t GetFTRTDataSize() { return ftrt_size_; }
uint32_t GetReadOffset() { return read_offset_; }
void SetReadOffset(uint32_t offset) { read_offset_ = offset; }
uint32_t UsToBytes(uint64_t input_us);
st_module_type_t module_type_;
sound_model_info_map_t sm_info_map_;
SoundModelInfo *sound_model_info_;
uint32_t start_index_ = 0;
uint32_t end_index_ = 0;
uint32_t ftrt_size_ = 0;
uint32_t read_offset_ = 0;
bool use_qc_wakeup_config_ = false;
uint32_t conf_levels_intf_version_;
st_confidence_levels_info *st_conf_levels_;
st_confidence_levels_info_v2 *st_conf_levels_v2_;
struct pal_stream_attributes str_attr_;
struct detection_event_info detection_event_info_;
struct detection_event_info_pdk detection_event_info_multi_model_;
uint32_t det_model_id_;
uint8_t *custom_event_;
uint32_t custom_event_size_;
struct buffer_config default_buf_config_;
struct param_id_detection_engine_buffering_config_t buffering_config_;
};
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,33 @@
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libhotword_intf
LOCAL_MODULE_OWNER := qti
LOCAL_VENDOR_MODULE := true
LOCAL_CPPFLAGS += -fexceptions
LOCAL_SRC_FILES := \
src/HotwordInterface.cpp
LOCAL_C_INCLUDES += \
$(LOCAL_PATH)/inc
# { SEC_AUDIO_COMMON
LOCAL_C_INCLUDES += $(TOP)/system/media/audio/include
# } SEC_AUDIO_COMMON
LOCAL_HEADER_LIBRARIES := \
libarpal_headers \
libarvui_intf_headers \
libspf-headers \
liblisten_headers \
libarosal_headers
LOCAL_SHARED_LIBRARIES := \
liblog \
libcutils \
liblx-osal
include $(BUILD_SHARED_LIBRARY)

View File

@@ -0,0 +1,74 @@
/*
* Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#ifndef HOTWORD_INTERFACE_H
#define HOTWORD_INTERFACE_H
#include "VoiceUIInterface.h"
#include "detection_cmn_api.h"
#include "ar_osal_mem_op.h"
class HotwordInterface: public VoiceUIInterface {
public:
HotwordInterface(vui_intf_param_t *model);
~HotwordInterface();
void DetachStream(void *stream) override;
int32_t SetParameter(intf_param_id_t param_id,
vui_intf_param_t *param) override;
int32_t GetParameter(intf_param_id_t param_id,
vui_intf_param_t *param) override;
int32_t Process(intf_process_id_t type,
vui_intf_param_t *in_out_param) { return 0; }
int32_t RegisterModel(void *s,
struct pal_st_sound_model *model,
const std::vector<sound_model_data_t *> model_list) override;
void DeregisterModel(void *s) override;
private:
static int32_t ParseSoundModel(struct pal_st_sound_model *sound_model,
std::vector<sound_model_data_t *> &model_list);
int32_t ParseRecognitionConfig(void *s,
struct pal_st_recognition_config *config);
void GetBufferingConfigs(void *s,
struct buffer_config *config);
int32_t ParseDetectionPayload(void *event, uint32_t size);
void* GetDetectedStream();
void UpdateDetectionResult(void *s, uint32_t result);
int32_t GenerateCallbackEvent(void *s,
struct pal_st_recognition_event **event,
uint32_t *event_size);
int32_t GetSoundModelLoadPayload(vui_intf_param_t *param);
int32_t GetBufferingPayload(vui_intf_param_t *param);
void SetStreamAttributes(struct pal_stream_attributes *attr);
void SetSTModuleType(st_module_type_t model_type) {
module_type_ = model_type;
}
st_module_type_t GetModuleType(void *s) { return module_type_; }
uint32_t GetReadOffset() { return read_offset_; }
void SetReadOffset(uint32_t offset) { read_offset_ = offset; }
uint32_t UsToBytes(uint64_t input_us);
st_module_type_t module_type_;
sound_model_info_map_t sm_info_map_;
struct pal_stream_attributes str_attr_;
uint32_t read_offset_ = 0;
uint8_t *custom_event_;
uint32_t custom_event_size_;
struct buffer_config default_buf_config_;
struct param_id_detection_engine_buffering_config_t buffering_config_;
};
#endif

View File

@@ -0,0 +1,650 @@
/*
* Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Changes from Qualcomm Innovation Center are provided under the following license:
* Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#define LOG_TAG "PAL: HotwordInterface"
//#define LOG_NDEBUG 0
#include <log/log.h>
#include "HotwordInterface.h"
extern "C" int32_t get_vui_interface(struct vui_intf_t *intf,
vui_intf_param_t *model) {
int32_t status = 0;
sound_model_config_t *config = nullptr;
if (!intf || !model || !model->data)
return -EINVAL;
config = (sound_model_config_t *)model->data;
switch (config->module_type) {
case ST_MODULE_TYPE_HW:
intf->interface = std::make_shared<HotwordInterface>(model);
break;
default:
ALOGE("%s: %d: Unsupported module type %d",
__func__, __LINE__, config->module_type);
status = -EINVAL;
break;
}
return status;
}
extern "C" int32_t release_vui_interface(struct vui_intf_t *intf) {
int32_t status = 0;
if (!intf)
return -EINVAL;
if (intf->interface) {
intf->interface = nullptr;
}
return status;
}
HotwordInterface::HotwordInterface(
vui_intf_param_t *model) {
int32_t status = 0;
struct pal_st_sound_model *sound_model = nullptr;
std::vector<sound_model_data_t *> model_list;
sound_model_config_t *config = nullptr;
custom_event_ = nullptr;
custom_event_size_ = 0;
memset(&default_buf_config_, 0, sizeof(default_buf_config_));
memset(&buffering_config_, 0, sizeof(buffering_config_));
if (!model || !model->data) {
ALOGE("%s: %d: Invalid input", __func__, __LINE__);
throw std::runtime_error("Invalid input");
}
config = (sound_model_config_t *)model->data;
sound_model = (struct pal_st_sound_model *)config->sound_model;
module_type_ = config->module_type;
status = HotwordInterface::ParseSoundModel(sound_model, model_list);
if (status) {
ALOGE("%s: %d: Failed to parse sound model, status = %d",
__func__, __LINE__, status);
throw std::runtime_error("Failed to parse sound model");
}
status = RegisterModel(model->stream, sound_model, model_list);
if (status) {
ALOGE("%s: %d: Failed to register sound model, status = %d",
__func__, __LINE__, status);
throw std::runtime_error("Failed to register sound model");
}
}
HotwordInterface::~HotwordInterface() {
ALOGD("%s: %d: Enter", __func__, __LINE__);
if (custom_event_)
free(custom_event_);
ALOGD("%s: %d: Exit", __func__, __LINE__);
}
void HotwordInterface::DetachStream(void *stream) {
DeregisterModel(stream);
}
int32_t HotwordInterface::SetParameter(
intf_param_id_t param_id, vui_intf_param_t *param) {
int32_t status = 0;
ALOGV("%s: %d: Enter", __func__, __LINE__);
if (!param) {
ALOGE("%s: %d: Invalid param", __func__, __LINE__);
return -EINVAL;
}
switch (param_id) {
case PARAM_RECOGNITION_CONFIG: {
status = ParseRecognitionConfig(param->stream,
(struct pal_st_recognition_config *)param->data);
break;
}
case PARAM_DETECTION_EVENT: {
status = ParseDetectionPayload(param->data, param->size);
break;
}
case PARAM_DETECTION_RESULT: {
UpdateDetectionResult(param->stream, *(uint32_t *)param->data);
break;
}
case PARAM_STREAM_ATTRIBUTES: {
SetStreamAttributes((struct pal_stream_attributes *)param->data);
break;
}
case PARAM_DEFAULT_BUFFER_CONFIG: {
struct buffer_config *buf_config =
(struct buffer_config *)param->data;
default_buf_config_.hist_buffer_duration =
buf_config->hist_buffer_duration;
default_buf_config_.pre_roll_duration =
buf_config->pre_roll_duration;
break;
}
default:
ALOGD("%s: %d: Unsupported param id %d",
__func__, __LINE__, param_id);
break;
}
ALOGV("%s: %d: Exit, status = %d", __func__, __LINE__, status);
return status;
}
int32_t HotwordInterface::GetParameter(
intf_param_id_t param_id, vui_intf_param_t *param) {
int32_t status = 0;
ALOGV("%s: %d: Enter", __func__, __LINE__);
switch (param_id) {
case PARAM_INTERFACE_PROPERTY: {
vui_intf_property_t *property = (vui_intf_property_t *)param->data;
if (property) {
property->is_qc_wakeup_config = false;
property->is_multi_model_supported = false;
} else {
ALOGE("%s: %d: Invalid property", __func__, __LINE__);
status = -EINVAL;
}
break;
}
case PARAM_FSTAGE_SOUND_MODEL_TYPE: {
*(st_module_type_t *)param->data = GetModuleType(param->stream);
break;
}
case PARAM_SOUND_MODEL_LIST: {
void *s = param->stream;
sound_model_list_t *sm_list = (sound_model_list_t *)param->data;
if (sm_info_map_.find(s) != sm_info_map_.end() && sm_info_map_[s]) {
for (int i = 0; i < sm_info_map_[s]->model_list.size(); i++) {
sm_list->sm_list.push_back(sm_info_map_[s]->model_list[i]);
}
} else {
ALOGE("%s: %d: stream not registered", __func__, __LINE__);
status = -EINVAL;
}
break;
}
case PARAM_FSTAGE_BUFFERING_CONFIG: {
struct buffer_config *config = (struct buffer_config *)param->data;
GetBufferingConfigs(param->stream, config);
break;
}
case PARAM_DETECTION_EVENT: {
status = GenerateCallbackEvent(param->stream,
(struct pal_st_recognition_event **)param->data, &param->size);
break;
}
case PARAM_DETECTION_STREAM: {
param->stream = GetDetectedStream();
break;
}
case PARAM_SOUND_MODEL_LOAD:
status = GetSoundModelLoadPayload(param);
break;
case PARAM_BUFFERING_CONFIG:
status = GetBufferingPayload(param);
break;
default:
ALOGE("%s: %d: Unsupported param id %d",
__func__, __LINE__, param_id);
break;
}
ALOGV("%s: %d: Exit, status = %d", __func__, __LINE__, status);
return status;
}
int32_t HotwordInterface::ParseSoundModel(
struct pal_st_sound_model *sound_model,
std::vector<sound_model_data_t *> &model_list) {
int32_t status = 0;
struct pal_st_phrase_sound_model *phrase_sm = nullptr;
struct pal_st_sound_model *common_sm = nullptr;
uint8_t *ptr = nullptr;
uint8_t *sm_data = nullptr;
int32_t sm_size = 0;
sound_model_data_t *model_data = nullptr;
ALOGD("%s: %d: Enter", __func__, __LINE__);
if (sound_model->type == PAL_SOUND_MODEL_TYPE_KEYPHRASE) {
// handle for phrase sound model
phrase_sm = (struct pal_st_phrase_sound_model *)sound_model;
sm_size = phrase_sm->common.data_size;
sm_data = (uint8_t *)calloc(1, sm_size);
if (!sm_data) {
ALOGE("%s: %d: Failed to allocate memory for sm_data",
__func__, __LINE__);
status = -ENOMEM;
goto error_exit;
}
ptr = (uint8_t*)phrase_sm + phrase_sm->common.data_offset;
ar_mem_cpy(sm_data, sm_size, ptr, sm_size);
model_data = (sound_model_data_t *)calloc(1, sizeof(sound_model_data_t));
if (!model_data) {
status = -ENOMEM;
ALOGE("%s: %d: model_data allocation failed, status %d",
__func__, __LINE__, status);
goto error_exit;
}
model_data->type = ST_SM_ID_SVA_F_STAGE_GMM;
model_data->data = sm_data;
model_data->size = sm_size;
model_list.push_back(model_data);
} else if (sound_model->type == PAL_SOUND_MODEL_TYPE_GENERIC) {
// handle for generic sound model
common_sm = sound_model;
sm_size = common_sm->data_size;
sm_data = (uint8_t *)calloc(1, sm_size);
if (!sm_data) {
ALOGE("%s: %d: Failed to allocate memory for sm_data",
__func__, __LINE__);
status = -ENOMEM;
goto error_exit;
}
ptr = (uint8_t*)common_sm + common_sm->data_offset;
ar_mem_cpy(sm_data, sm_size, ptr, sm_size);
model_data = (sound_model_data_t *)calloc(1, sizeof(sound_model_data_t));
if (!model_data) {
status = -ENOMEM;
ALOGE("%s: %d: model_data allocation failed, status %d",
__func__, __LINE__, status);
goto error_exit;
}
model_data->type = ST_SM_ID_SVA_F_STAGE_GMM;
model_data->data = sm_data;
model_data->size = sm_size;
model_list.push_back(model_data);
}
ALOGD("%s: %d: Exit, status %d", __func__, __LINE__, status);
return status;
error_exit:
// clean up memory added to model_list in failure case
for (int i = 0; i < model_list.size(); i++) {
model_data = model_list[i];
if (model_data) {
if (model_data->data)
free(model_data->data);
free(model_data);
}
}
model_list.clear();
if (sm_data)
free(sm_data);
ALOGD("%s: %d: Exit, status %d", __func__, __LINE__, status);
return status;
}
int32_t HotwordInterface::ParseRecognitionConfig(void *s,
struct pal_st_recognition_config *config) {
int32_t status = 0;
struct sound_model_info *sm_info = nullptr;
if (sm_info_map_.find(s) != sm_info_map_.end() && sm_info_map_[s]) {
sm_info = sm_info_map_[s];
sm_info->rec_config = config;
} else {
ALOGE("%s: %d: Stream not registered to interface", __func__, __LINE__);
return -EINVAL;
}
ALOGD("%s: %d: Enter", __func__, __LINE__);
if (!config) {
ALOGE("%s: %d: Invalid config", __func__, __LINE__);
status = -EINVAL;
goto exit;
}
// get history buffer duration from sound trigger platform xml
sm_info_map_[s]->buf_config.hist_buffer_duration =
default_buf_config_.hist_buffer_duration;
sm_info_map_[s]->buf_config.pre_roll_duration =
default_buf_config_.pre_roll_duration;
exit:
ALOGD("%s: %d: Exit, status %d", __func__, __LINE__, status);
return status;
}
void HotwordInterface::GetBufferingConfigs(void *s,
struct buffer_config *config) {
if (sm_info_map_.find(s) != sm_info_map_.end() && sm_info_map_[s]) {
config->hist_buffer_duration = sm_info_map_[s]->buf_config.hist_buffer_duration;
config->pre_roll_duration = sm_info_map_[s]->buf_config.pre_roll_duration;
} else {
ALOGE("%s: %d: Stream not registered to interface", __func__, __LINE__);
}
}
int32_t HotwordInterface::ParseDetectionPayload(void *event, uint32_t size) {
int32_t status = 0;
if (!event || size == 0) {
ALOGE("%s: %d: Invalid detection payload", __func__, __LINE__);
return -EINVAL;
}
custom_event_ = (uint8_t *)realloc(custom_event_, size);
if (!custom_event_) {
ALOGE("%s: %d: Failed to allocate memory for detection payload",
__func__, __LINE__);
return -ENOMEM;
}
ar_mem_cpy(custom_event_, size, event, size);
custom_event_size_ = size;
return status;
}
void* HotwordInterface::GetDetectedStream() {
ALOGD("%s: %d: Enter", __func__, __LINE__);
if (sm_info_map_.empty()) {
ALOGE("%s: %d: Unexpected, No streams attached to engine!",
__func__, __LINE__);
return nullptr;
} else if (sm_info_map_.size() == 1) {
return sm_info_map_.begin()->first;
} else {
ALOGE("%s: %d: Only single hotword usecase is supported",
__func__, __LINE__);
return nullptr;
}
}
int32_t HotwordInterface::GenerateCallbackEvent(void *s,
struct pal_st_recognition_event **event,
uint32_t *size) {
struct sound_model_info *sm_info = nullptr;
struct pal_st_phrase_recognition_event *phrase_event = nullptr;
struct pal_st_generic_recognition_event *generic_event = nullptr;
size_t event_size = 0;
uint8_t *opaque_data = nullptr;
int32_t status = 0;
if (sm_info_map_.find(s) != sm_info_map_.end() && sm_info_map_[s]) {
sm_info = sm_info_map_[s];
} else {
ALOGE("%s: %d: Stream not registered to interface", __func__, __LINE__);
return -EINVAL;
}
ALOGD("%s: %d: Enter", __func__, __LINE__);
*event = nullptr;
if (sm_info->type == PAL_SOUND_MODEL_TYPE_KEYPHRASE) {
event_size = sizeof(struct pal_st_phrase_recognition_event) +
custom_event_size_;
// event allocated will be used/deallocated in stream side
phrase_event = (struct pal_st_phrase_recognition_event *)
calloc(1, event_size);
if (!phrase_event) {
ALOGE("%s: %d: Failed to alloc memory for recognition event",
__func__, __LINE__);
status = -ENOMEM;
goto exit;
}
phrase_event->num_phrases = sm_info->rec_config->num_phrases;
memcpy(phrase_event->phrase_extras, sm_info->rec_config->phrases,
phrase_event->num_phrases *
sizeof(struct pal_st_phrase_recognition_extra));
*event = &(phrase_event->common);
(*event)->status = sm_info->det_result;
(*event)->type = sm_info->type;
(*event)->st_handle = (pal_st_handle_t *)this;
(*event)->capture_available = sm_info->rec_config->capture_requested;
// TODO: generate capture session
(*event)->capture_session = 0;
(*event)->capture_delay_ms = 0;
(*event)->capture_preamble_ms = 0;
(*event)->trigger_in_data = true;
(*event)->data_size = custom_event_size_;
(*event)->data_offset = sizeof(struct pal_st_phrase_recognition_event);
(*event)->media_config.sample_rate =
str_attr_.in_media_config.sample_rate;
(*event)->media_config.bit_width =
str_attr_.in_media_config.bit_width;
(*event)->media_config.ch_info.channels =
str_attr_.in_media_config.ch_info.channels;
(*event)->media_config.aud_fmt_id = PAL_AUDIO_FMT_PCM_S16_LE;
// Filling Opaque data
opaque_data = (uint8_t *)phrase_event +
phrase_event->common.data_offset;
ar_mem_cpy(opaque_data, custom_event_size_, custom_event_, custom_event_size_);
} else if (sm_info->type == PAL_SOUND_MODEL_TYPE_GENERIC) {
event_size = sizeof(struct pal_st_generic_recognition_event) +
custom_event_size_;
// event allocated will be used/deallocated in stream side
generic_event = (struct pal_st_generic_recognition_event *)
calloc(1, event_size);
if (!generic_event) {
ALOGE("%s: %d: Failed to alloc memory for recognition event",
__func__, __LINE__);
status = -ENOMEM;
goto exit;
}
*event = &(generic_event->common);
(*event)->status = PAL_RECOGNITION_STATUS_SUCCESS;
(*event)->type = sm_info->type;
(*event)->st_handle = (pal_st_handle_t *)this;
(*event)->capture_available = sm_info->rec_config->capture_requested;
// TODO: generate capture session
(*event)->capture_session = 0;
(*event)->capture_delay_ms = 0;
(*event)->capture_preamble_ms = 0;
(*event)->trigger_in_data = true;
(*event)->data_size = custom_event_size_;
(*event)->data_offset = sizeof(struct pal_st_generic_recognition_event);
(*event)->media_config.sample_rate =
str_attr_.in_media_config.sample_rate;
(*event)->media_config.bit_width =
str_attr_.in_media_config.bit_width;
(*event)->media_config.ch_info.channels =
str_attr_.in_media_config.ch_info.channels;
(*event)->media_config.aud_fmt_id = PAL_AUDIO_FMT_PCM_S16_LE;
// Filling Opaque data
opaque_data = (uint8_t *)generic_event +
generic_event->common.data_offset;
ar_mem_cpy(opaque_data, custom_event_size_, custom_event_, custom_event_size_);
}
*size = event_size;
exit:
ALOGD("%s: %d: Exit", __func__, __LINE__);
return status;
}
void HotwordInterface::UpdateDetectionResult(void *s, uint32_t result) {
if (sm_info_map_.find(s) != sm_info_map_.end() && sm_info_map_[s]) {
sm_info_map_[s]->det_result = result;
}
}
int32_t HotwordInterface::GetSoundModelLoadPayload(vui_intf_param_t *param) {
void *s = nullptr;
sound_model_data_t *sm_data = nullptr;
if (!param) {
ALOGE("%s: %d: Invalid param", __func__, __LINE__);
return -EINVAL;
}
s = param->stream;
if (sm_info_map_.find(s) == sm_info_map_.end()) {
ALOGD("%s: %d: Stream not registered", __func__, __LINE__);
return -EINVAL;
}
if (!sm_info_map_[s]) {
ALOGE("%s: %d: Invalid sound model info", __func__, __LINE__);
return -EINVAL;
}
sm_data = sm_info_map_[s]->model_list[0];
param->data = (void *)sm_data->data;
param->size = sm_data->size;
return 0;
}
int32_t HotwordInterface::GetBufferingPayload(vui_intf_param_t *param) {
void *s = nullptr;
struct sound_model_info *info = nullptr;
if (!param) {
ALOGE("%s: %d: Invalid param", __func__, __LINE__);
return -EINVAL;
}
s = param->stream;
if (sm_info_map_.find(s) == sm_info_map_.end()) {
ALOGD("%s: %d: Stream not registered", __func__, __LINE__);
return -EINVAL;
}
info = sm_info_map_[s];
if (!info) {
ALOGE("%s: %d: Invalid sound model info", __func__, __LINE__);
return -EINVAL;
}
buffering_config_.hist_buffer_duration_msec =
info->buf_config.hist_buffer_duration;
buffering_config_.pre_roll_duration_msec =
info->buf_config.pre_roll_duration;
param->data = (void *)&buffering_config_;
param->size = sizeof(buffering_config_);
return 0;
}
void HotwordInterface::SetStreamAttributes(
struct pal_stream_attributes *attr) {
if (!attr) {
ALOGE("%s: %d: Invalid stream attributes", __func__, __LINE__);
return;
}
ar_mem_cpy(&str_attr_, sizeof(struct pal_stream_attributes),
attr, sizeof(struct pal_stream_attributes));
}
int32_t HotwordInterface::RegisterModel(void *s,
struct pal_st_sound_model *model,
const std::vector<sound_model_data_t *> model_list) {
int32_t status = 0;
if (sm_info_map_.find(s) == sm_info_map_.end()) {
sm_info_map_[s] = (struct sound_model_info *)calloc(1,
sizeof(struct sound_model_info));
if (!sm_info_map_[s]) {
ALOGE("%s: %d: Failed to allocate memory for sm data",
__func__, __LINE__);
status = -ENOMEM;
goto exit;
}
}
sm_info_map_[s]->model_list.clear();
for (auto iter: model_list) {
sm_info_map_[s]->model_list.push_back(iter);
}
sm_info_map_[s]->model = model;
sm_info_map_[s]->type = model->type;
exit:
return status;
}
void HotwordInterface::DeregisterModel(void *s) {
sound_model_data_t *sm_data = nullptr;
auto iter = sm_info_map_.find(s);
if (iter != sm_info_map_.end() && sm_info_map_[s]) {
for (int i = 0; i < sm_info_map_[s]->model_list.size(); i++) {
sm_data = sm_info_map_[s]->model_list[i];
if (sm_data) {
if (sm_data->data)
free(sm_data->data);
free(sm_data);
}
}
sm_info_map_[s]->model_list.clear();
sm_info_map_[s]->model_list.shrink_to_fit();
free(sm_info_map_[s]);
sm_info_map_.erase(iter);
} else {
ALOGD("%s: %d: Cannot deregister unregistered model",
__func__, __LINE__);
}
}
uint32_t HotwordInterface::UsToBytes(uint64_t input_us) {
uint32_t bytes = 0;
bytes = str_attr_.in_media_config.sample_rate *
str_attr_.in_media_config.bit_width *
str_attr_.in_media_config.ch_info.channels * input_us /
(BITS_PER_BYTE * US_PER_SEC);
return bytes;
}