/*************************************************************************** * * Copyright 2015-2019 BES. * All rights reserved. All unpublished rights reserved. * * No part of this work may be used or reproduced in any form or by any * means, or stored in a database or retrieval system, without prior written * permission of BES. * * Use of this work is governed by a license granted by BES. * This work contains confidential and proprietary information of * BES. which is protected by copyright, trade secret, * trademark and other intellectual property rights. * ****************************************************************************/ // Standard C Included Files #include #include #include #include #include #include "cmsis_os.h" #include "plat_types.h" #include "hal_uart.h" #include "hal_timer.h" #include "hal_trace.h" #include "cqueue.h" #include "app_audio.h" #include "app_overlay.h" #include "app_ring_merge.h" #include "tgt_hardware.h" #include "bt_sco_chain.h" #include "iir_resample.h" #include "hfp_api.h" #include "audio_prompt_sbc.h" #ifdef TX_RX_PCM_MASK #include "bt_drv_interface.h" #endif #define ENABLE_LPC_PLC #define ENABLE_PLC_ENCODER // BT #include "a2dp_api.h" #include "plc_utils.h" extern "C" { #include "plc_8000.h" #include "speech_utils.h" #if defined(HFP_1_6_ENABLE) #include "codec_sbc.h" #ifndef ENABLE_LPC_PLC #include "plc_16000.h" #endif #endif #if defined(CVSD_BYPASS) #include "Pcm8k_Cvsd.h" #endif #ifndef ENABLE_LPC_PLC static void *speech_plc; #endif } #if defined(ENABLE_LPC_PLC) #include "lpc_plc_api.h" #endif #if defined(SPEECH_TX_24BIT) extern int32_t *aec_echo_buf; #else extern short *aec_echo_buf; #endif // #define SPEECH_RX_PLC_DUMP_DATA // #define PLC_DEBUG_PRINT_DATA //#define DEBUG_SCO_DUMP #ifdef SPEECH_RX_PLC_DUMP_DATA #include "audio_dump.h" int16_t *audio_dump_temp_buf = NULL; #endif // app_bt_stream.cpp::bt_sco_player(), used buffer size #define APP_BT_STREAM_USE_BUF_SIZE (1024*2) #if defined(SCO_OPTIMIZE_FOR_RAM) uint8_t *sco_overlay_ram_buf = NULL; int sco_overlay_ram_buf_len = 0; #endif static bool resample_needed_flag = false; static int sco_frame_length; static int codec_frame_length; static int16_t *resample_buf = NULL; static IirResampleState *uplink_resample_st = NULL; static IirResampleState *downlink_resample_st = NULL; #define MSBC_FRAME_SIZE (60) #if defined(HFP_1_6_ENABLE) static btif_sbc_decoder_t *msbc_decoder; static float msbc_eq_band_gain[CFG_HW_AUD_EQ_NUM_BANDS]= {0,0,0,0,0,0,0,0}; #define MSBC_ENCODE_PCM_LEN (240) #ifndef ENABLE_LPC_PLC struct PLC_State msbc_plc_state; #endif #ifdef ENABLE_PLC_ENCODER static btif_sbc_encoder_t *msbc_plc_encoder; static int16_t *msbc_plc_encoder_buffer = NULL; #define MSBC_CODEC_DELAY (73) #endif static btif_sbc_encoder_t *msbc_encoder; #endif #if defined(ENABLE_LPC_PLC) LpcPlcState *msbc_plc_state = NULL; #endif #define VOICEBTPCM_TRACE(s,...) //TRACE(s, ##__VA_ARGS__) #if defined(CHIP_BEST1400) || defined(CHIP_BEST1402) || defined(CHIP_BEST2300P) || defined(CHIP_BEST2300A) || defined(CHIP_BEST2001) #define MSBC_MUTE_PATTERN (0x55) #else #define MSBC_MUTE_PATTERN (0x00) #endif #define MSBC_LEN_FORMBT_PER_FRAME (60) //Bytes; only for BES platform. #define SAMPLES_LEN_PER_FRAME (120) #define MSBC_LEN_PER_FRAME (57+3) #define BYTES_PER_PCM_FRAME (SAMPLES_LEN_PER_FRAME*2) // Add 1 to ensure it never be out of bounds when msbc_offset is 1 unsigned char msbc_buf_all[MSBC_LEN_FORMBT_PER_FRAME*3+1]; #if defined(HFP_1_6_ENABLE) static int msbc_find_first_sync = 0; static unsigned int msbc_offset = 0; static unsigned int next_frame_flag = 0; #endif static PacketLossState pld; extern bool bt_sco_codec_is_msbc(void); int process_downlink_msbc_frames(unsigned char *msbc_buf, unsigned int msbc_len, unsigned char *pcm_buf,unsigned int pcm_len); int process_downlink_cvsd_frames(unsigned char *cvsd_buf, unsigned int cvsd_len, unsigned char *pcm_buf,unsigned int pcm_len); int process_uplink_msbc_frames(unsigned char *pcm_buf, unsigned int pcm_len, unsigned char *msbc_buf,unsigned int msbc_len); int process_uplink_cvsd_frames(unsigned char *pcm_buf, unsigned int pcm_len, unsigned char *cvsd_buf,unsigned int cvsd_len); int process_downlink_bt_voice_frames(uint8_t *in_buf, uint32_t in_len, uint8_t *out_buf,uint32_t out_len,int32_t codec_type) { //TRACE(3,"[%s] in_len = %d, out_len = %d", __FUNCTION__, in_len, out_len); #if defined(SPEECH_RX_24BIT) out_len /= 2; #endif int16_t *pcm_buf = (int16_t *)out_buf; int pcm_len = out_len / sizeof(int16_t); if (resample_needed_flag == true) { pcm_buf = resample_buf; pcm_len = sco_frame_length; } if (bt_sco_codec_is_msbc()) { process_downlink_msbc_frames(in_buf, in_len, (uint8_t *)pcm_buf, pcm_len * sizeof(int16_t)); // Down sampling if (resample_needed_flag) { iir_resample_process(downlink_resample_st, pcm_buf, (int16_t *)out_buf, pcm_len); pcm_buf = (int16_t *)out_buf; pcm_len >>= 1; } } else { process_downlink_cvsd_frames(in_buf, in_len, (uint8_t *)pcm_buf, pcm_len * sizeof(int16_t)); // Up sampling if (resample_needed_flag) { iir_resample_process(downlink_resample_st, pcm_buf, (int16_t *)out_buf, pcm_len); pcm_buf = (int16_t *)out_buf; pcm_len <<= 1; } } #if defined(SPEECH_RX_24BIT) int32_t *buf32 = (int32_t *)out_buf; for (int i = pcm_len - 1; i >= 0; i--) { buf32[i] = ((int32_t)pcm_buf[i] << 8); } #endif speech_rx_process(pcm_buf, &pcm_len); #if defined(SPEECH_RX_24BIT) out_len *= 2; #endif return 0; } int process_uplink_bt_voice_frames(uint8_t *in_buf, uint32_t in_len, uint8_t *ref_buf, uint32_t ref_len, uint8_t *out_buf,uint32_t out_len,int32_t codec_type) { //TRACE(3,"[%s] in_len = %d, out_len = %d", __FUNCTION__, in_len, out_len); #if defined(SPEECH_TX_24BIT) int32_t *pcm_buf = (int32_t *)in_buf; int pcm_len = in_len / sizeof(int32_t); #else int16_t *pcm_buf = (int16_t *)in_buf; int pcm_len = in_len / sizeof(int16_t); #endif #if defined(SPEECH_TX_AEC_CODEC_REF) ASSERT(pcm_len % (SPEECH_CODEC_CAPTURE_CHANNEL_NUM + 1) == 0, "[%s] pcm_len(%d) should be divided by %d", __FUNCTION__, pcm_len, SPEECH_CODEC_CAPTURE_CHANNEL_NUM + 1); // copy reference buffer #if defined(SPEECH_TX_AEC) || defined(SPEECH_TX_AEC2) || defined(SPEECH_TX_AEC3) || defined(SPEECH_TX_AEC2FLOAT) || defined(SPEECH_TX_THIRDPARTY) for (int i = SPEECH_CODEC_CAPTURE_CHANNEL_NUM, j = 0; i < pcm_len; i += SPEECH_CODEC_CAPTURE_CHANNEL_NUM + 1, j++) { aec_echo_buf[j] = pcm_buf[i]; } #endif for (int i = 0, j = 0; i < pcm_len; i += SPEECH_CODEC_CAPTURE_CHANNEL_NUM + 1, j += SPEECH_CODEC_CAPTURE_CHANNEL_NUM) { for (int k = 0; k < SPEECH_CODEC_CAPTURE_CHANNEL_NUM; k++) pcm_buf[j + k] = pcm_buf[i + k]; } pcm_len = pcm_len / (SPEECH_CODEC_CAPTURE_CHANNEL_NUM + 1) * SPEECH_CODEC_CAPTURE_CHANNEL_NUM; #elif (defined(SPEECH_TX_AEC) || defined(SPEECH_TX_AEC2) || defined(SPEECH_TX_AEC3) || defined(SPEECH_TX_AEC2FLOAT) || defined(SPEECH_TX_THIRDPARTY)) int ref_pcm_len = ref_len / sizeof(int16_t); ASSERT(pcm_len / SPEECH_CODEC_CAPTURE_CHANNEL_NUM == ref_pcm_len, "[%s] Length error: %d / %d != %d", __func__, pcm_len, SPEECH_CODEC_CAPTURE_CHANNEL_NUM, ref_pcm_len); for (int i = 0; i < ref_pcm_len; i++) { aec_echo_buf[i] = ref_buf[i]; } #endif speech_tx_process(pcm_buf, aec_echo_buf, &pcm_len); #if defined(SPEECH_TX_24BIT) int32_t *buf24 = (int32_t *)pcm_buf; int16_t *buf16 = (int16_t *)pcm_buf; for (int i = 0; i < pcm_len; i++) buf16[i] = (buf24[i] >> 8); #endif int16_t *pcm_buf_16bits = (int16_t *)pcm_buf; if (bt_sco_codec_is_msbc()) { // Up sampling if (resample_needed_flag) { iir_resample_process(uplink_resample_st, (int16_t *)pcm_buf_16bits, resample_buf, pcm_len); pcm_buf_16bits = resample_buf; pcm_len = sco_frame_length; } process_uplink_msbc_frames((uint8_t *)pcm_buf_16bits, pcm_len * sizeof(int16_t), out_buf, out_len); } else { // Down sampling if (resample_needed_flag) { iir_resample_process(uplink_resample_st, (int16_t *)pcm_buf_16bits, resample_buf, pcm_len); pcm_buf_16bits = resample_buf; pcm_len = sco_frame_length; } process_uplink_cvsd_frames((uint8_t *)pcm_buf_16bits, pcm_len * sizeof(int16_t), out_buf, out_len); } return 0; } int process_downlink_msbc_frames(unsigned char *msbc_buf, unsigned int msbc_len, unsigned char *pcm_buf,unsigned int pcm_len) { #if defined(HFP_1_6_ENABLE) btif_sbc_pcm_data_t pcm_data; unsigned int msbc_offset_lowdelay = 0; unsigned int i,j; unsigned char *msbc_buffer=(unsigned char *)msbc_buf; int frame_flag[6]; // 1: good frame; 0:bad frame; bt_status_t ret; unsigned int frame_counter=0; unsigned short byte_decode = 0; unsigned int msbc_offset_total = 0; int msbc_offset_drift[6] = {0, }; short *dec_pcm_buf=( short *)pcm_buf; unsigned char dec_msbc_buf[MSBC_LEN_PER_FRAME] = {0, }; //unsigned int timer_begin=hal_sys_timer_get(); //TRACE(2,"process_downlink_msbc_frames:pcm_len:%d,msbc_len:%d",pcm_len,msbc_len); //TRACE(1,"decode_msbc_frame,msbc_len:%d",msbc_len); for(i =0; i=msbc_offset_total) { frame_flag[j]=3; } else { frame_flag[j]=1; } } } } for(j =0; j1)msbc_find_first_sync=0; } while((frame_counterstreamInfo.mSbcFlag = 1; msbc_encoder->streamInfo.numChannels = 1; msbc_encoder->streamInfo.channelMode = BTIF_SBC_CHNL_MODE_MONO; msbc_encoder->streamInfo.bitPool = 26; msbc_encoder->streamInfo.sampleFreq = BTIF_SBC_CHNL_SAMPLE_FREQ_16; msbc_encoder->streamInfo.allocMethod = BTIF_SBC_ALLOC_METHOD_LOUDNESS; msbc_encoder->streamInfo.numBlocks = BTIF_MSBC_BLOCKS; msbc_encoder->streamInfo.numSubBands = 8; //init msbc decoder const float EQLevel[25] = { 0.0630957, 0.0794328, 0.1, 0.1258925, 0.1584893, 0.1995262, 0.2511886, 0.3162278, 0.398107, 0.5011872, 0.6309573, 0.794328, 1, 1.258925, 1.584893, 1.995262, 2.5118864, 3.1622776, 3.9810717, 5.011872, 6.309573, 7.943282, 10, 12.589254, 15.848932 };//-12~12 uint8_t i; for (i=0; istreamInfo.mSbcFlag = 1; msbc_decoder->streamInfo.bitPool = 26; msbc_decoder->streamInfo.sampleFreq = BTIF_SBC_CHNL_SAMPLE_FREQ_16; msbc_decoder->streamInfo.channelMode = BTIF_SBC_CHNL_MODE_MONO; msbc_decoder->streamInfo.allocMethod = BTIF_SBC_ALLOC_METHOD_LOUDNESS; /* Number of blocks used to encode the stream (4, 8, 12, or 16) */ msbc_decoder->streamInfo.numBlocks = BTIF_MSBC_BLOCKS; /* The number of subbands in the stream (4 or 8) */ msbc_decoder->streamInfo.numSubBands = 8; msbc_decoder->streamInfo.numChannels = 1; //init msbc plc #ifndef ENABLE_LPC_PLC InitPLC(&msbc_plc_state); #endif next_frame_flag = 0; msbc_find_first_sync = 0; packet_loss_detection_init(&pld); #if defined(ENABLE_PLC_ENCODER) app_audio_mempool_get_buff((uint8_t **)&msbc_plc_encoder, sizeof(btif_sbc_encoder_t)); btif_sbc_init_encoder(msbc_plc_encoder); msbc_plc_encoder->streamInfo.mSbcFlag = 1; msbc_plc_encoder->streamInfo.bitPool = 26; msbc_plc_encoder->streamInfo.sampleFreq = BTIF_SBC_CHNL_SAMPLE_FREQ_16; msbc_plc_encoder->streamInfo.channelMode = BTIF_SBC_CHNL_MODE_MONO; msbc_plc_encoder->streamInfo.allocMethod = BTIF_SBC_ALLOC_METHOD_LOUDNESS; /* Number of blocks used to encode the stream (4, 8, 12, or 16) */ msbc_plc_encoder->streamInfo.numBlocks = BTIF_MSBC_BLOCKS; /* The number of subbands in the stream (4 or 8) */ msbc_plc_encoder->streamInfo.numSubBands = 8; msbc_plc_encoder->streamInfo.numChannels = 1; app_audio_mempool_get_buff((uint8_t **)&msbc_plc_encoder_buffer, sizeof(int16_t) * (SAMPLES_LEN_PER_FRAME + MSBC_CODEC_DELAY)); #endif } else #endif { #ifndef ENABLE_LPC_PLC speech_plc = (PlcSt_8000 *)speech_plc_8000_init(voicebtpcm_get_ext_buff); #endif } #if defined(CVSD_BYPASS) Pcm8k_CvsdInit(); #endif #ifdef SPEECH_RX_PLC_DUMP_DATA audio_dump_temp_buf = (int16_t *)voicebtpcm_get_ext_buff(sizeof(int16_t) * 120); audio_dump_init(120, sizeof(short), 3); #endif resample_needed_flag = (sco_sample_rate == codec_sample_rate) ? 0 : 1; if (resample_needed_flag) { TRACE(1, "[%s] SCO <-- Resample --> CODEC", __func__); resample_buf = (int16_t *)voicebtpcm_get_ext_buff(sizeof(int16_t) * sco_frame_length); } #if defined(SCO_OPTIMIZE_FOR_RAM) sco_overlay_ram_buf_len = hal_overlay_get_text_free_size((enum HAL_OVERLAY_ID_T)APP_OVERLAY_HFP); sco_overlay_ram_buf = (uint8_t *)hal_overlay_get_text_free_addr((enum HAL_OVERLAY_ID_T)APP_OVERLAY_HFP); #endif speech_len = app_audio_mempool_free_buff_size() - APP_BT_STREAM_USE_BUF_SIZE; speech_buf = (uint8_t *)voicebtpcm_get_ext_buff(speech_len); int tx_frame_ms = SPEECH_PROCESS_FRAME_MS; int rx_frame_ms = SPEECH_SCO_FRAME_MS; speech_init(codec_sample_rate, codec_sample_rate, tx_frame_ms, rx_frame_ms, SPEECH_SCO_FRAME_MS, speech_buf, speech_len); if (resample_needed_flag) { uplink_resample_st = iir_resample_init(codec_frame_length, iir_resample_choose_mode(codec_sample_rate, sco_sample_rate)); downlink_resample_st = iir_resample_init(sco_frame_length, iir_resample_choose_mode(sco_sample_rate, codec_sample_rate)); } #if defined(ENABLE_LPC_PLC) msbc_plc_state = lpc_plc_create(sco_sample_rate); #endif return 0; } int voicebtpcm_pcm_audio_deinit(void) { TRACE(1,"[%s] Close...", __func__); // TRACE(2,"[%s] app audio buffer free = %d", __func__, app_audio_mempool_free_buff_size()); #if defined(ENABLE_LPC_PLC) lpc_plc_destroy(msbc_plc_state); #endif if (resample_needed_flag) { iir_resample_destroy(uplink_resample_st); iir_resample_destroy(downlink_resample_st); } speech_deinit(); packet_loss_detection_report(&pld); #if defined(SCO_OPTIMIZE_FOR_RAM) sco_overlay_ram_buf = NULL; sco_overlay_ram_buf_len = 0; #endif // TRACE(1,"Free buf = %d", app_audio_mempool_free_buff_size()); return 0; }