From 7b3db72710e4d0febaaf7f6d2c268c721d631b2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20B=C3=A9rub=C3=A9?= Date: Tue, 21 May 2024 19:26:49 -0400 Subject: [PATCH] Audio support on hisi-v3 --- src/hal/hisi/v3_aud.h | 133 ++++++++++++++++++++++++++++++++++++++++ src/hal/hisi/v3_hal.c | 44 ++++++++++++- src/hal/hisi/v3_hal.h | 1 + src/hal/sstar/i6_hal.c | 2 +- src/hal/sstar/i6c_hal.c | 2 +- src/hal/sstar/i6f_hal.c | 2 +- src/hal/support.c | 1 + 7 files changed, 181 insertions(+), 4 deletions(-) create mode 100644 src/hal/hisi/v3_aud.h diff --git a/src/hal/hisi/v3_aud.h b/src/hal/hisi/v3_aud.h new file mode 100644 index 0000000..7dbc2ac --- /dev/null +++ b/src/hal/hisi/v3_aud.h @@ -0,0 +1,133 @@ +#pragma once + +#include "v3_common.h" + +#define V3_AUD_CHN_NUM 2 + +typedef enum { + V3_AUD_BIT_8, + V3_AUD_BIT_16, + V3_AUD_BIT_24 +} v3_aud_bit; + +typedef enum { + V3_AUD_INTF_I2S_MASTER, + V3_AUD_INTF_I2S_SLAVE, + V3_AUD_INTF_PCM_SLAVE_STD, + V3_AUD_INTF_PCM_SLAVE_NSTD, + V3_AUD_INTF_PCM_MASTER_STD, + V3_AUD_INTF_PCM_MASTER_NSTD, + V3_AUD_INTF_END +} v3_aud_intf; + +typedef struct { + // Accept industry standards from + // 8000 to 96000Hz, plus 64000Hz + int rate; + v3_aud_bit bit; + v3_aud_intf intf; + int stereoOn; + // 8-to-16 bit, expand mode + int expandOn; + unsigned int frmNum; + unsigned int packNumPerFrm; + unsigned int chnNum; + unsigned int syncRxClkOn; +} v3_aud_cnf; + +typedef struct { + v3_aud_bit bit; + int stereoOn; + void *addr[2]; + unsigned int phy[2]; + unsigned long long timestamp; + unsigned int sequence; + unsigned int length; + unsigned int poolId[2]; +} v3_aud_frm; + +typedef struct { + v3_aud_frm frame; + char isValid; + char isSysBound; +} v3_aud_efrm; + +typedef struct { + void *handle; + + int (*fnDisableDevice)(int device); + int (*fnEnableDevice)(int device); + int (*fnSetDeviceConfig)(int device, v3_aud_cnf *config); + + int (*fnDisableChannel)(int device, int channel); + int (*fnEnableChannel)(int device, int channel); + + int (*fnSetVolume)(int device, int channel, int dbLevel); + + int (*fnFreeFrame)(int device, int channel, v3_aud_frm *frame, v3_aud_efrm *encFrame); + int (*fnGetFrame)(int device, int channel, v3_aud_frm *frame, v3_aud_efrm *encFrame, int millis); +} v3_aud_impl; + +static int v3_aud_load(v3_aud_impl *aud_lib) { + if (!(aud_lib->handle = dlopen("libmi_ai.so", RTLD_LAZY | RTLD_GLOBAL))) { + fprintf(stderr, "[v3_aud] Failed to load library!\nError: %s\n", dlerror()); + return EXIT_FAILURE; + } + + if (!(aud_lib->fnDisableDevice = (int(*)(int device)) + dlsym(aud_lib->handle, "HI_MPI_AI_Disable"))) { + fprintf(stderr, "[v3_aud] Failed to acquire symbol HI_MPI_AI_Disable!\n"); + return EXIT_FAILURE; + } + + if (!(aud_lib->fnEnableDevice = (int(*)(int device)) + dlsym(aud_lib->handle, "HI_MPI_AI_Enable"))) { + fprintf(stderr, "[v3_aud] Failed to acquire symbol HI_MPI_AI_Enable!\n"); + return EXIT_FAILURE; + } + + if (!(aud_lib->fnSetDeviceConfig = (int(*)(int device, v3_aud_cnf *config)) + dlsym(aud_lib->handle, "HI_MPI_AI_SetPubAttr"))) { + fprintf(stderr, "[v3_aud] Failed to acquire symbol HI_MPI_AI_SetPubAttr!\n"); + return EXIT_FAILURE; + } + + if (!(aud_lib->fnDisableChannel = (int(*)(int device, int channel)) + dlsym(aud_lib->handle, "HI_MPI_AI_DisableChn"))) { + fprintf(stderr, "[v3_aud] Failed to acquire symbol HI_MPI_AI_DisableChn!\n"); + return EXIT_FAILURE; + } + + if (!(aud_lib->fnEnableChannel = (int(*)(int device, int channel)) + dlsym(aud_lib->handle, "HI_MPI_AI_EnableChn"))) { + fprintf(stderr, "[v3_aud] Failed to acquire symbol HI_MPI_AI_EnableChn!\n"); + return EXIT_FAILURE; + } + + + if (!(aud_lib->fnSetVolume = (int(*)(int device, int channel, int dbLevel)) + dlsym(aud_lib->handle, "HI_MPI_AI_SetVqeVolume"))) { + fprintf(stderr, "[v3_aud] Failed to acquire symbol HI_MPI_AI_SetVqeVolume!\n"); + return EXIT_FAILURE; + } + + if (!(aud_lib->fnFreeFrame = (int(*)(int device, int channel, v3_aud_frm *frame, v3_aud_efrm *encFrame)) + dlsym(aud_lib->handle, "HI_MPI_AI_ReleaseFrame"))) { + fprintf(stderr, "[v3_aud] Failed to acquire symbol HI_MPI_AI_ReleaseFrame!\n"); + return EXIT_FAILURE; + } + + if (!(aud_lib->fnGetFrame = (int(*)(int device, int channel, v3_aud_frm *frame, v3_aud_efrm *encFrame, int millis)) + dlsym(aud_lib->handle, "HI_MPI_AI_GetFrame"))) { + fprintf(stderr, "[v3_aud] Failed to acquire symbol HI_MPI_AI_GetFrame!\n"); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} + +static void v3_aud_unload(v3_aud_impl *aud_lib) { + if (aud_lib->handle) dlclose(aud_lib->handle); + aud_lib->handle = NULL; + memset(aud_lib, 0, sizeof(*aud_lib)); +} \ No newline at end of file diff --git a/src/hal/hisi/v3_hal.c b/src/hal/hisi/v3_hal.c index 70c3a27..8b16f60 100644 --- a/src/hal/hisi/v3_hal.c +++ b/src/hal/hisi/v3_hal.c @@ -1,5 +1,6 @@ #include "v3_hal.h" +v3_aud_impl v3_aud; v3_config_impl v3_config; v3_drv_impl v3_drv; v3_isp_impl v3_isp; @@ -13,6 +14,8 @@ v3_vpss_impl v3_vpss; hal_chnstate v3_state[V3_VENC_CHN_NUM] = {0}; int (*v3_venc_cb)(char, hal_vidstream*); +char _v3_aud_chn = 0; +char _v3_aud_dev = 0; char _v3_isp_chn = 0; char _v3_isp_dev = 0; char _v3_venc_dev = 0; @@ -28,6 +31,7 @@ void v3_hal_deinit(void) v3_vb_unload(&v3_vb); v3_rgn_unload(&v3_rgn); v3_isp_unload(&v3_isp); + v3_aud_unload(&v3_aud); v3_sys_unload(&v3_sys); } @@ -37,6 +41,8 @@ int v3_hal_init(void) if (ret = v3_sys_load(&v3_sys)) return ret; + if (ret = v3_aud_load(&v3_aud)) + return ret; if (ret = v3_isp_load(&v3_isp)) return ret; if (ret = v3_rgn_load(&v3_rgn)) @@ -53,6 +59,42 @@ int v3_hal_init(void) return EXIT_SUCCESS; } +void v3_audio_deinit(void) +{ + v3_aud.fnDisableChannel(_v3_aud_dev, _v3_aud_chn); + + v3_aud.fnDisableDevice(_v3_aud_dev); +} + +int i6_audio_init(void) +{ + int ret; + + { + v3_aud_cnf config; + config.rate = 48000; + config.bit = V3_AUD_BIT_16; + config.intf = V3_AUD_INTF_I2S_SLAVE; + config.stereoOn = 0; + config.expandOn = 0; + config.frmNum = 0; + config.packNumPerFrm = 0; + config.chnNum = 0; + config.syncRxClkOn = 0; + if (ret = v3_aud.fnSetDeviceConfig(_v3_aud_dev, &config)) + return ret; + } + if (ret = v3_aud.fnEnableDevice(_v3_aud_dev)) + return ret; + + if (ret = v3_aud.fnEnableChannel(_v3_aud_dev, _v3_aud_chn)) + return ret; + if (ret = v3_aud.fnSetVolume(_v3_aud_dev, _v3_aud_chn, 0xF6)) + return ret; + + return EXIT_SUCCESS; +} + int v3_channel_bind(char index) { int ret; @@ -160,7 +202,7 @@ int v3_pipeline_create(char mirror, char flip) v3_vpss_grp group; group.imgEnhOn = 0; group.dciOn = 0; - group.noiseRedOn = 0; + group.noiseRedOn = 1; group.histOn = 0; group.deintMode = 1; if (ret = v3_vpss.fnCreateGroup(_v3_vpss_grp, &group)) diff --git a/src/hal/hisi/v3_hal.h b/src/hal/hisi/v3_hal.h index 80fa94a..f15768a 100644 --- a/src/hal/hisi/v3_hal.h +++ b/src/hal/hisi/v3_hal.h @@ -1,6 +1,7 @@ #pragma once #include "v3_common.h" +#include "v3_aud.h" #include "v3_config.h" #include "v3_isp.h" #include "v3_rgn.h" diff --git a/src/hal/sstar/i6_hal.c b/src/hal/sstar/i6_hal.c index dee3b42..43563f7 100644 --- a/src/hal/sstar/i6_hal.c +++ b/src/hal/sstar/i6_hal.c @@ -266,7 +266,7 @@ int i6_pipeline_create(char sensor, short width, short height, char framerate) { i6_vpe_para param; param.hdr = I6_HDR_OFF; - param.level3DNR = 0; + param.level3DNR = 1; param.mirror = 0; param.flip = 0; param.lensAdjOn = 0; diff --git a/src/hal/sstar/i6c_hal.c b/src/hal/sstar/i6c_hal.c index 862f415..3249e8c 100644 --- a/src/hal/sstar/i6c_hal.c +++ b/src/hal/sstar/i6c_hal.c @@ -294,7 +294,7 @@ int i6c_pipeline_create(char sensor, short width, short height, char framerate) { i6c_isp_para param; param.hdr = I6C_HDR_OFF; - param.level3DNR = 0; + param.level3DNR = 1; param.mirror = 0; param.flip = 0; param.rotate = 0; diff --git a/src/hal/sstar/i6f_hal.c b/src/hal/sstar/i6f_hal.c index c52cae9..6fecede 100644 --- a/src/hal/sstar/i6f_hal.c +++ b/src/hal/sstar/i6f_hal.c @@ -272,7 +272,7 @@ int i6f_pipeline_create(char sensor, short width, short height, char framerate) { i6f_isp_para param; param.hdr = I6F_HDR_OFF; - param.level3DNR = 0; + param.level3DNR = 1; param.mirror = 0; param.flip = 0; param.rotate = 0; diff --git a/src/hal/support.c b/src/hal/support.c index cbcdd60..2e09915 100644 --- a/src/hal/support.c +++ b/src/hal/support.c @@ -112,5 +112,6 @@ void hal_identify(void) { plat = HAL_PLATFORM_V3; chnCount = V3_VENC_CHN_NUM; chnState = (hal_chnstate*)v3_state; + isp_thread = v3_image_thread; venc_thread = v3_video_thread; } \ No newline at end of file