Skip to content

Commit

Permalink
(Continued)
Browse files Browse the repository at this point in the history
  • Loading branch information
wberube committed Jul 10, 2024
1 parent 391cb3e commit 76dd170
Show file tree
Hide file tree
Showing 20 changed files with 258 additions and 608 deletions.
34 changes: 9 additions & 25 deletions src/hal/hisi/v3_aud.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,52 +67,36 @@ typedef struct {
} v3_aud_impl;

static int v3_aud_load(v3_aud_impl *aud_lib) {
if (!(aud_lib->handle = dlopen("libmpi.so", RTLD_LAZY | RTLD_GLOBAL))) {
fprintf(stderr, "[v3_aud] Failed to load library!\nError: %s\n", dlerror());
return EXIT_FAILURE;
}
if (!(aud_lib->handle = dlopen("libmpi.so", RTLD_LAZY | RTLD_GLOBAL)))
HAL_ERROR("v3_aud", "Failed to load library!\nError: %s\n", dlerror());

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");
hal_symbol_load("v3_aud", aud_lib->handle, "HI_MPI_AI_Disable")))
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");
hal_symbol_load("v3_aud", aud_lib->handle, "HI_MPI_AI_Enable")))
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");
hal_symbol_load("v3_aud", aud_lib->handle, "HI_MPI_AI_SetPubAttr")))
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");
hal_symbol_load("v3_aud", aud_lib->handle, "HI_MPI_AI_DisableChn")))
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");
hal_symbol_load("v3_aud", aud_lib->handle, "HI_MPI_AI_EnableChn")))
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");
hal_symbol_load("v3_aud", aud_lib->handle, "HI_MPI_AI_ReleaseFrame")))
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");
hal_symbol_load("v3_aud", aud_lib->handle, "HI_MPI_AI_GetFrame")))
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}
Expand Down
9 changes: 1 addition & 8 deletions src/hal/hisi/v3_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,9 @@
#include <string.h>
#include <sys/select.h>

#include "../symbols.h"
#include "../types.h"

#define V3_ERROR(x, ...) \
do { \
fprintf(stderr, "[v3_hal] \033[31m"); \
fprintf(stderr, (x), ##__VA_ARGS__); \
fprintf(stderr, "\033[0m"); \
return EXIT_FAILURE; \
} while (0)

typedef enum {
V3_BAYER_RG,
V3_BAYER_GR,
Expand Down
74 changes: 37 additions & 37 deletions src/hal/hisi/v3_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void *v3_audio_thread(void)
while (keepRunning) {
if (ret = v3_aud.fnGetFrame(_v3_aud_dev, _v3_aud_chn,
&frame, &echoFrame, 128)) {
fprintf(stderr, "[v3_aud] Getting the frame failed "
HAL_WARNING("v3_aud", "Getting the frame failed "
"with %#x!\n", ret);
continue;
}
Expand All @@ -128,11 +128,11 @@ void *v3_audio_thread(void)

if (ret = v3_aud.fnFreeFrame(_v3_aud_dev, _v3_aud_chn,
&frame, &echoFrame)) {
fprintf(stderr, "[v3_aud] Releasing the frame failed"
HAL_WARNING("v3_aud", "Releasing the frame failed"
" with %#x!\n", ret);
}
}
fprintf(stderr, "[v3_aud] Shutting down capture thread...\n");
HAL_INFO("v3_aud", "Shutting down capture thread...\n");
}

int v3_channel_bind(char index)
Expand Down Expand Up @@ -220,8 +220,8 @@ void *v3_image_thread(void)
int ret;

if (ret = v3_isp.fnRun(_v3_isp_dev))
fprintf(stderr, "[v3_isp] Operation failed with %#x!\n", ret);
fprintf(stderr, "[v3_isp] Shutting down ISP thread...\n");
HAL_DANGER("v3_isp", "Operation failed with %#x!\n", ret);
HAL_INFO("v3_isp", "Shutting down ISP thread...\n");
}

int v3_pipeline_create(void)
Expand Down Expand Up @@ -355,12 +355,12 @@ int v3_region_create(char handle, hal_rect rect, short opacity)
region.overlay.canvas = handle + 1;

if (v3_rgn.fnGetRegionConfig(handle, &regionCurr)) {
fprintf(stderr, "[v3_rgn] Creating region %d...\n", handle);
HAL_INFO("v3_rgn", "Creating region %d...\n", handle);
if (ret = v3_rgn.fnCreateRegion(handle, &region))
return ret;
} else if (regionCurr.overlay.size.height != region.overlay.size.height ||
regionCurr.overlay.size.width != region.overlay.size.width) {
fprintf(stderr, "[v3_rgn] Parameters are different, recreating "
HAL_INFO("v3_rgn", "Parameters are different, recreating "
"region %d...\n", handle);
v3_rgn.fnDetachChannel(handle, &channel);
v3_rgn.fnDestroyRegion(handle);
Expand All @@ -369,9 +369,9 @@ int v3_region_create(char handle, hal_rect rect, short opacity)
}

if (v3_rgn.fnGetChannelConfig(handle, &channel, &attribCurr))
fprintf(stderr, "[v3_rgn] Attaching region %d...\n", handle);
HAL_INFO("v3_rgn", "Attaching region %d...\n", handle);
else if (attribCurr.overlay.point.x != rect.x || attribCurr.overlay.point.x != rect.y) {
fprintf(stderr, "[v3_rgn] Position has changed, reattaching "
HAL_INFO("v3_rgn", "Position has changed, reattaching "
"region %d...\n", handle);
v3_rgn.fnDetachChannel(handle, &channel);
}
Expand Down Expand Up @@ -422,14 +422,14 @@ int v3_sensor_config(void) {
fd = open(v3_snr_endp, O_RDWR);
else fd = open("/dev/mipi", O_RDWR);
if (fd < 0)
V3_ERROR("Opening imaging device has failed!\n");
HAL_ERROR("v3_snr", "Opening imaging device has failed!\n");

ioctl(fd, _IOW(V3_SNR_IOC_MAGIC, V3_SNR_CMD_RST_MIPI, unsigned int), &config.device);

ioctl(fd, _IOW(V3_SNR_IOC_MAGIC, V3_SNR_CMD_RST_SENS, unsigned int), &config.device);

if (ioctl(fd, _IOW(V3_SNR_IOC_MAGIC, V3_SNR_CMD_CONF_DEV, v3_snr_dev), &config))
V3_ERROR("Configuring imaging device has failed!\n");
HAL_ERROR("v3_snr", "Configuring imaging device has failed!\n");

ioctl(fd, _IOW(V3_SNR_IOC_MAGIC, V3_SNR_CMD_UNRST_MIPI, unsigned int), &config.device);

Expand All @@ -449,7 +449,7 @@ void v3_sensor_deconfig(void) {
fd = open(v3_snr_endp, O_RDWR);
else fd = open("/dev/mipi", O_RDWR);
if (fd < 0)
fprintf(stderr, "[v3_hal] Opening imaging device has failed!\n");
HAL_DANGER("v3_snr", "Opening imaging device has failed!\n");

ioctl(fd, _IOW(V3_SNR_IOC_MAGIC, V3_SNR_CMD_RST_SENS, unsigned int), &config.device);

Expand All @@ -475,12 +475,12 @@ int v3_sensor_init(char *name, char *obj)
if (v3_snr_drv.handle = dlopen(path, RTLD_NOW | RTLD_GLOBAL))
break;
} if (!v3_snr_drv.handle)
V3_ERROR("Failed to load the sensor driver");
HAL_ERROR("v3_snr", "Failed to load the sensor driver");

if (!(v3_snr_drv.fnRegisterCallback = (int(*)(void))dlsym(v3_snr_drv.handle, "sensor_register_callback")))
V3_ERROR("Failed to connect the callback register function");
HAL_ERROR("v3_snr", "Failed to connect the callback register function");
if (!(v3_snr_drv.fnUnRegisterCallback = (int(*)(void))dlsym(v3_snr_drv.handle, "sensor_unregister_callback")))
V3_ERROR("Failed to connect the callback register function");
HAL_ERROR("v3_snr", "Failed to connect the callback register function");

return EXIT_SUCCESS;
}
Expand Down Expand Up @@ -527,7 +527,7 @@ int v3_video_create(char index, hal_vidconfig *config)
channel.rate.mjpgQp = (v3_venc_rate_mjpgqp){ .srcFps = config->framerate,
.dstFps = config->framerate, .quality = config->maxQual }; break;
default:
V3_ERROR("MJPEG encoder can only support CBR, VBR or fixed QP modes!");
HAL_ERROR("v3_venc", "MJPEG encoder can only support CBR, VBR or fixed QP modes!");
}
goto attach;
} else if (config->codec == HAL_VIDCODEC_H265) {
Expand Down Expand Up @@ -556,7 +556,7 @@ int v3_video_create(char index, hal_vidconfig *config)
.statTime = 1, .srcFps = config->framerate, .dstFps = config->framerate,
.bitrate = config->bitrate }; break;
default:
V3_ERROR("H.265 encoder does not support this mode!");
HAL_ERROR("v3_venc", "H.265 encoder does not support this mode!");
}
} else if (config->codec == HAL_VIDCODEC_H264) {
channel.attrib.codec = V3_VENC_CODEC_H264;
Expand Down Expand Up @@ -584,9 +584,9 @@ int v3_video_create(char index, hal_vidconfig *config)
.statTime = 1, .srcFps = config->framerate, .dstFps = config->framerate,
.bitrate = config->bitrate }; break;
default:
V3_ERROR("H.264 encoder does not support this mode!");
HAL_ERROR("v3_venc", "H.264 encoder does not support this mode!");
}
} else V3_ERROR("This codec is not supported by the hardware!");
} else HAL_ERROR("v3_venc", "This codec is not supported by the hardware!");
attrib->maxPic.width = config->width;
attrib->maxPic.height = config->height;
attrib->bufSize = config->height * config->width * 2;
Expand Down Expand Up @@ -656,14 +656,14 @@ int v3_video_snapshot_grab(char index, hal_jpegdata *jpeg)
int ret;

if (ret = v3_channel_bind(index)) {
fprintf(stderr, "[v3_venc] Binding the encoder channel "
HAL_DANGER("v3_venc", "Binding the encoder channel "
"%d failed with %#x!\n", index, ret);
goto abort;
}

unsigned int count = 1;
if (v3_venc.fnStartReceivingEx(index, &count)) {
fprintf(stderr, "[v3_venc] Requesting one frame "
HAL_DANGER("v3_venc", "Requesting one frame "
"%d failed with %#x!\n", index, ret);
goto abort;
}
Expand All @@ -676,37 +676,37 @@ int v3_video_snapshot_grab(char index, hal_jpegdata *jpeg)
FD_SET(fd, &readFds);
ret = select(fd + 1, &readFds, NULL, NULL, &timeout);
if (ret < 0) {
fprintf(stderr, "[v3_venc] Select operation failed!\n");
HAL_DANGER("v3_venc", "Select operation failed!\n");
goto abort;
} else if (ret == 0) {
fprintf(stderr, "[v3_venc] Capture stream timed out!\n");
HAL_DANGER("v3_venc", "Capture stream timed out!\n");
goto abort;
}

if (FD_ISSET(fd, &readFds)) {
v3_venc_stat stat;
if (v3_venc.fnQuery(index, &stat)) {
fprintf(stderr, "[v3_venc] Querying the encoder channel "
HAL_DANGER("v3_venc", "Querying the encoder channel "
"%d failed with %#x!\n", index, ret);
goto abort;
}

if (!stat.curPacks) {
fprintf(stderr, "[v3_venc] Current frame is empty, skipping it!\n");
HAL_DANGER("v3_venc", "Current frame is empty, skipping it!\n");
goto abort;
}

v3_venc_strm strm;
memset(&strm, 0, sizeof(strm));
strm.packet = (v3_venc_pack*)malloc(sizeof(v3_venc_pack) * stat.curPacks);
if (!strm.packet) {
fprintf(stderr, "[v3_venc] Memory allocation on channel %d failed!\n", index);
HAL_DANGER("v3_venc", "Memory allocation on channel %d failed!\n", index);
goto abort;
}
strm.count = stat.curPacks;

if (ret = v3_venc.fnGetStream(index, &strm, stat.curPacks)) {
fprintf(stderr, "[v3_venc] Getting the stream on "
HAL_DANGER("v3_venc", "Getting the stream on "
"channel %d failed with %#x!\n", index, ret);
free(strm.packet);
strm.packet = NULL;
Expand Down Expand Up @@ -754,7 +754,7 @@ void *v3_video_thread(void)

ret = v3_venc.fnGetDescriptor(i);
if (ret < 0) {
fprintf(stderr, "[v3_venc] Getting the encoder descriptor failed with %#x!\n", ret);
HAL_DANGER("v3_venc", "Getting the encoder descriptor failed with %#x!\n", ret);
return NULL;
}
v3_state[i].fileDesc = ret;
Expand All @@ -780,10 +780,10 @@ void *v3_video_thread(void)
timeout.tv_usec = 0;
ret = select(maxFd + 1, &readFds, NULL, NULL, &timeout);
if (ret < 0) {
fprintf(stderr, "[v3_venc] Select operation failed!\n");
HAL_DANGER("v3_venc", "Select operation failed!\n");
break;
} else if (ret == 0) {
fprintf(stderr, "[v3_venc] Main stream loop timed out!\n");
HAL_WARNING("v3_venc", "Main stream loop timed out!\n");
continue;
} else {
for (int i = 0; i < V3_VENC_CHN_NUM; i++) {
Expand All @@ -793,26 +793,26 @@ void *v3_video_thread(void)
memset(&stream, 0, sizeof(stream));

if (ret = v3_venc.fnQuery(i, &stat)) {
fprintf(stderr, "[v3_venc] Querying the encoder channel "
HAL_DANGER("v3_venc", "Querying the encoder channel "
"%d failed with %#x!\n", i, ret);
break;
}

if (!stat.curPacks) {
fprintf(stderr, "[v3_venc] Current frame is empty, skipping it!\n");
HAL_WARNING("v3_venc", " Current frame is empty, skipping it!\n");
continue;
}

stream.packet = (v3_venc_pack*)malloc(
sizeof(v3_venc_pack) * stat.curPacks);
if (!stream.packet) {
fprintf(stderr, "[v3_venc] Memory allocation on channel %d failed!\n", i);
HAL_DANGER("v3_venc", "Memory allocation on channel %d failed!\n", i);
break;
}
stream.count = stat.curPacks;

if (ret = v3_venc.fnGetStream(i, &stream, 40)) {
fprintf(stderr, "[v3_venc] Getting the stream on "
HAL_DANGER("v3_venc", "Getting the stream on "
"channel %d failed with %#x!\n", i, ret);
break;
}
Expand Down Expand Up @@ -845,7 +845,7 @@ void *v3_video_thread(void)
}

if (ret = v3_venc.fnFreeStream(i, &stream)) {
fprintf(stderr, "[v3_venc] Releasing the stream on "
HAL_WARNING("v3_venc", "Releasing the stream on "
"channel %d failed with %#x!\n", i, ret);
}
free(stream.packet);
Expand All @@ -854,7 +854,7 @@ void *v3_video_thread(void)
}
}
}
fprintf(stderr, "[v3_venc] Shutting down encoding thread...\n");
HAL_INFO("v3_venc", "Shutting down encoding thread...\n");
}

void v3_system_deinit(void)
Expand All @@ -877,7 +877,7 @@ int v3_system_init(char *snrConfig)
}

if (v3_parse_sensor_config(snrConfig, &v3_config) != CONFIG_OK)
V3_ERROR("Can't load sensor config\n");
HAL_ERROR("v3_sys", "Can't load sensor config\n");

if (ret = v3_sensor_init(v3_config.dll_file, v3_config.sensor_type))
return ret;
Expand Down
Loading

0 comments on commit 76dd170

Please sign in to comment.