Skip to content

Commit

Permalink
Mostly concerns older toolchains, usage of strtol in the OSD API hand…
Browse files Browse the repository at this point in the history
…ler, missing parts for Anyka
  • Loading branch information
wberube committed Aug 30, 2024
1 parent 3875aee commit ff21d88
Show file tree
Hide file tree
Showing 17 changed files with 279 additions and 28 deletions.
194 changes: 193 additions & 1 deletion src/hal/plus/ak_hal.c
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
#if 0
#ifdef __arm__

#include "ak_hal.h"

ak_aud_impl ak_aud;
ak_sys_impl ak_sys;
ak_venc_impl ak_venc;
ak_vi_impl ak_vi;

hal_chnstate ak_state[AK_VENC_CHN_NUM] = {0};
int (*ak_aud_cb)(hal_audframe*);
int (*ak_vid_cb)(char, hal_vidstream*);

ak_vi_cnf _ak_vi_cnf;

void *_ak_vi_dev;
void *_ak_venc_dev[AK_VENC_CHN_NUM];
void *_ak_venc_strm[AK_VENC_CHN_NUM];

void ak_hal_deinit(void)
{
ak_vi_unload(&ak_vi);
ak_venc_unload(&ak_venc);
ak_aud_unload(&ak_aud);
ak_sys_unload(&ak_sys);
}

int ak_hal_init(void)
{
int ret;

if (ret = ak_sys_load(&ak_sys))
return ret;
if (ret = ak_aud_load(&ak_aud))
return ret;
if (ret = ak_venc_load(&ak_venc))
Expand All @@ -33,9 +41,193 @@ int ak_hal_init(void)
return EXIT_SUCCESS;
}

int ak_channel_bind(char index)
{
if (!(_ak_venc_strm[index] =
ak_venc.fnBindChannel(_ak_vi_dev, _ak_venc_dev[index])))
return EXIT_FAILURE;

return EXIT_SUCCESS;
}

int ak_channel_grayscale(char enable)
{
return ak_vi.fnSetDeviceMode(_ak_vi_dev, enable & 1);
}

int ak_channel_unbind(char index)
{
int ret = ak_venc.fnUnbindChannel(_ak_venc_strm[index]);

_ak_venc_strm[index] = NULL;

return ret;
}

int ak_pipeline_create(char mirror, char flip)
{
int ret;

if (!(_ak_vi_dev = ak_vi.fnEnableDevice(0)))
return EXIT_FAILURE;

if (ret = ak_vi.fnGetSensorResolution(_ak_vi_dev, _ak_vi_cnf.dest))
return ret;

_ak_vi_cnf.capt.x = 0;
_ak_vi_cnf.capt.y = 0;
_ak_vi_cnf.capt.width = _ak_vi_cnf.dest[0].width;
_ak_vi_cnf.capt.height = _ak_vi_cnf.dest[0].height;
if (ret = ak_vi.fnSetDeviceConfig(_ak_vi_dev, &_ak_vi_cnf))
return ret;

if (ret = ak_vi.fnStartDevice(_ak_vi_dev))
return ret;

if (ret = ak_vi.fnSetDeviceFlipMirror(_ak_vi_dev, flip & 1, mirror & 1))
return ret;

return EXIT_SUCCESS;
}

void ak_pipeline_destroy(void)
{
ak_vi.fnStopDevice(_ak_vi_dev);
ak_vi.fnDisableDevice(_ak_vi_dev);
}

int ak_video_create(char index, hal_vidconfig *config)
{
int ret;

ak_venc_codec codec;
char ratemode;
ak_venc_prof profile;

switch (config->mode) {
case HAL_VIDMODE_CBR: ratemode = 0; break;
case HAL_VIDMODE_VBR: ratemode = 1; break;
default: HAL_ERROR("ak_venc", "Video encoder does not support this mode!");
}
switch (config->codec) {
case HAL_VIDCODEC_JPG:
case HAL_VIDCODEC_MJPG:
codec = AK_VENC_CODEC_MJPG;
profile = AK_VENC_PROF_HEVC_MAINSTILL;
break;
case HAL_VIDCODEC_H265:
codec = AK_VENC_CODEC_H265;
profile = AK_VENC_PROF_HEVC_MAIN;
break;
case HAL_VIDCODEC_H264:
codec = AK_VENC_CODEC_H264;
switch (config->profile) {
case HAL_VIDPROFILE_BASELINE: profile = AK_VENC_PROF_BASE; break;
case HAL_VIDPROFILE_MAIN: profile = AK_VENC_PROF_MAIN; break;
default: profile = AK_VENC_PROF_HIGH; break;
} break;
default: HAL_ERROR("ak_venc", "This codec is not supported by the hardware!");
}

{
ak_venc_cnf channel = {.codec = codec, .width = config->width, .height = config->height,
.minQual = config->minQual, .maxQual = config->maxQual, .dstFps = config->framerate,
.gop = config->gop, .maxBitrate = config->maxBitrate, .profile = profile, .subChnOn = index,
.output = index ? AK_VENC_OUT_SUBSTRM : AK_VENC_OUT_MAINSTRM, .vbrModeOn = ratemode};

if (!(_ak_venc_dev[index] = ak_venc.fnEnableChannel(&channel)))
HAL_ERROR("ak_venc", "Creating channel %d failed with %#x!\n%s\n",
index, ret = ak_sys.fnGetErrorNum(), ak_sys.fnGetErrorStr(ret));
}

ak_state[index].payload = config->codec;

return EXIT_SUCCESS;
}

int ak_video_destroy(char index)
{
if (ak_venc.fnDisableChannel(_ak_venc_dev[index]))
return EXIT_FAILURE;

return EXIT_SUCCESS;
}

int ak_video_destroy_all(void)
{
int ret;

for (char i = 0; i < AK_VENC_CHN_NUM; i++)
if (ak_state[i].enable)
if (ret = ak_video_destroy(i))
return ret;

return EXIT_SUCCESS;
}

void ak_video_request_idr(char index)
{
ak_venc.fnRequestIdr(_ak_venc_dev[index]);
}

void *ak_video_thread(void)
{
int ret;

while (keepRunning) {
for (int i = 0; i < AK_VENC_CHN_NUM; i++) {
if (!ak_state[i].enable) continue;
if (!ak_state[i].mainLoop) continue;

ak_venc_strm stream;
if (ak_venc.fnGetStream(_ak_venc_strm[i], &stream)) {
ret = ak_sys.fnGetErrorNum();
HAL_DANGER("ak_venc", "Getting the stream on "
"channel %d failed with %#x!\n%s\n", i, ret,
ak_sys.fnGetErrorStr(ret));
};
if (!stream.length) continue;

if (ak_vid_cb) {
hal_vidstream outStrm;
hal_vidpack outPack[1];
memset(outPack, 0, sizeof(outPack));
outStrm.count = 1;
outStrm.seq = stream.sequence;
outPack[0].data = stream.data;
outPack[0].length = stream.length;
outPack[0].naluCnt = 1;
outPack[0].nalu[0].length = outPack[0].length;
outPack[0].nalu[0].offset = 0;
outPack[0].nalu[0].type = stream.naluType;
outPack[0].offset = 0;
outPack[0].timestamp = stream.timestamp;
outStrm.pack = outPack;
(*ak_vid_cb)(i, &outStrm);

if (ak_venc.fnFreeStream(_ak_venc_strm[i], &stream)) {
HAL_DANGER("ak_venc", "Releasing the stream on "
"channel %d failed!\n", i);
}
}
}
}
HAL_INFO("ak_venc", "Shutting down encoding thread...\n");
}

void ak_system_deinit(void)
{

}

int ak_system_init(char *snrConfig)
{
int ret;

if (ret = ak_vi.fnLoadSensorConfig(snrConfig))
return ret;

return EXIT_SUCCESS;
}

#endif
3 changes: 2 additions & 1 deletion src/hal/plus/ak_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "ak_common.h"
#include "ak_aud.h"
#include "ak_sys.h"
#include "ak_venc.h"
#include "ak_vi.h"

Expand Down Expand Up @@ -35,4 +36,4 @@ int ak_video_snapshot_grab(char index, hal_jpegdata *jpeg);
void *ak_video_thread(void);

void ak_system_deinit(void);
int ak_system_init(void);
int ak_system_init(char *snrConfig);
41 changes: 41 additions & 0 deletions src/hal/plus/ak_sys.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include "ak_common.h"

typedef struct {
void *handle, *handleAkuio, *handleThread;

int (*fnGetErrorNum)(void);
char* (*fnGetErrorStr)(int error);
} ak_sys_impl;

static int ak_sys_load(ak_sys_impl *sys_lib) {
if (!(sys_lib->handleThread = dlopen("libplat_thread.so", RTLD_LAZY | RTLD_GLOBAL)))
HAL_ERROR("ak_sys", "Failed to load library!\nError: %s\n", dlerror());

if (!(sys_lib->handleAkuio = dlopen("libakuio.so", RTLD_LAZY | RTLD_GLOBAL)))
HAL_ERROR("ak_sys", "Failed to load library!\nError: %s\n", dlerror());

if (!(sys_lib->handle = dlopen("libplat_common.so", RTLD_LAZY | RTLD_GLOBAL)))
HAL_ERROR("ak_sys", "Failed to load library!\nError: %s\n", dlerror());

if (!(sys_lib->fnGetErrorNum = (int(*)(void))
hal_symbol_load("ak_sys", sys_lib->handle, "ak_get_error_no")))
return EXIT_FAILURE;

if (!(sys_lib->fnGetErrorStr = (char*(*)(int error))
hal_symbol_load("ak_sys", sys_lib->handle, "ak_get_error_str")))
return EXIT_FAILURE;

return EXIT_SUCCESS;
}

static void ak_sys_unload(ak_sys_impl *sys_lib) {
if (sys_lib->handle) dlclose(sys_lib->handle);
sys_lib->handle = NULL;
if (sys_lib->handleAkuio) dlclose(sys_lib->handleAkuio);
sys_lib->handleAkuio = NULL;
if (sys_lib->handleThread) dlclose(sys_lib->handleThread);
sys_lib->handleThread = NULL;
memset(sys_lib, 0, sizeof(*sys_lib));
}
2 changes: 1 addition & 1 deletion src/hal/plus/ak_venc.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ typedef enum {
typedef enum {
AK_VENC_PROF_MAIN,
AK_VENC_PROF_HIGH,
AK_VENC_PROF_BASELINE,
AK_VENC_PROF_BASE,
AK_VENC_PROF_HEVC_MAIN,
AK_VENC_PROF_HEVC_MAINSTILL,
AK_VENC_PROF_END
Expand Down
2 changes: 1 addition & 1 deletion src/hal/plus/ak_vi.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static int ak_vi_load(ak_vi_impl *vi_lib) {
return EXIT_FAILURE;

if (!(vi_lib->fnSetDeviceFlipMirror = (int(*)(void *device, int flipOn, int mirrorOn))
hal_symbol_load("ak_vi", vi_lib->handle, "ak_vi_switch_mode")))
hal_symbol_load("ak_vi", vi_lib->handle, "ak_vi_set_flip_mirror")))
return EXIT_FAILURE;

if (!(vi_lib->fnSetDeviceMode = (int(*)(void *device, int nightOn))
Expand Down
6 changes: 3 additions & 3 deletions src/hal/plus/gm_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ int gm_video_create(char index, hal_vidconfig *config)

switch (config->codec) {
case HAL_VIDCODEC_JPG:
case HAL_VIDCODEC_MJPG:
case HAL_VIDCODEC_MJPG: {
GM_DECLARE(gm_lib, mjpgchn, gm_venc_mjpg_cnf, "gm_mjpege_attr_t");
mjpgchn.dest.width = config->width;
mjpgchn.dest.height = config->height;
Expand All @@ -271,7 +271,7 @@ int gm_video_create(char index, hal_vidconfig *config)
mjpgchn.maxBitrate = MAX(config->bitrate, config->maxBitrate);
gm_lib.fnSetDeviceConfig(_gm_venc_dev[index], &mjpgchn);
break;
case HAL_VIDCODEC_H264:
} case HAL_VIDCODEC_H264: {
GM_DECLARE(gm_lib, h264chn, gm_venc_h264_cnf, "gm_h264e_attr_t");
h264chn.dest.width = config->width;
h264chn.dest.height = config->height;
Expand All @@ -298,7 +298,7 @@ int gm_video_create(char index, hal_vidconfig *config)
h264chn.level = 41;
gm_lib.fnSetDeviceConfig(_gm_venc_dev[index], &h264chn);
break;
default: HAL_ERROR("gm_venc", "This codec is not supported by the hardware!");
} default: HAL_ERROR("gm_venc", "This codec is not supported by the hardware!");
}

gm_state[index].payload = config->codec;
Expand Down
1 change: 1 addition & 0 deletions src/hal/star/i3_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

extern char keepRunning;

extern hal_chnstate i3_state[I3_VENC_CHN_NUM];
extern int (*i3_aud_cb)(hal_audframe*);
extern int (*i3_vid_cb)(char, hal_vidstream*);

Expand Down
1 change: 1 addition & 0 deletions src/hal/star/i6_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

extern char keepRunning;

extern hal_chnstate i6_state[I6_VENC_CHN_NUM];
extern int (*i6_aud_cb)(hal_audframe*);
extern int (*i6_vid_cb)(char, hal_vidstream*);

Expand Down
1 change: 1 addition & 0 deletions src/hal/star/i6c_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

extern char keepRunning;

extern hal_chnstate i6c_state[I6C_VENC_CHN_NUM];
extern int (*i6c_aud_cb)(hal_audframe*);
extern int (*i6c_vid_cb)(char, hal_vidstream*);

Expand Down
1 change: 1 addition & 0 deletions src/hal/star/i6f_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

extern char keepRunning;

extern hal_chnstate i6f_state[I6F_VENC_CHN_NUM];
extern int (*i6f_aud_cb)(hal_audframe*);
extern int (*i6f_vid_cb)(char, hal_vidstream*);

Expand Down
11 changes: 11 additions & 0 deletions src/hal/support.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,17 @@ void hal_identify(void) {
vid_thread = gm_video_thread;
return;
}

if (!access("/sys/devices/platform/ak39-uart.0", F_OK)) {
plat = HAL_PLATFORM_AK;
strcpy(chip, "AK3918");
strcpy(family, "anyka");
chnCount = AK_VENC_CHN_NUM;
chnState = (hal_chnstate*)ak_state;
//aud_thread = ak_audio_thread;
vid_thread = ak_video_thread;
return;
}
#endif

#ifdef __mips__
Expand Down
Loading

0 comments on commit ff21d88

Please sign in to comment.