Initial setup for CLion

This commit is contained in:
2026-03-28 16:54:11 +11:00
parent 239cc02591
commit 7b4134133c
1136 changed files with 811916 additions and 0 deletions
+44
View File
@@ -0,0 +1,44 @@
# Build output directories
x64/
x86/
Debug/
Release/
out/
# CLion / CMake
.idea/
cmake-build-debug/
cmake-build-release/
cmake-build-*/
CMakeFiles/
CMakeCache.txt
cmake_install.cmake
Makefile
# Visual Studio
.vs/
*.user
*.suo
*.sdf
*.opensdf
*.VC.db
*.VC.opendb
ipch/
# Compiled files
*.obj
*.o
*.pch
*.pdb
*.ilk
*.exp
*.lib
*.dll
*.exe
# TortoiseGit
*.orig
# Windows
Thumbs.db
Desktop.ini
+670
View File
@@ -0,0 +1,670 @@
#include "ANSANNHUB.h"
#include <cstdint>
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <memory>
#include <numeric>
#include <sstream>
#include <unordered_map>
#include <condition_variable>
#include <mutex>
// --- Refcounted handle registry infrastructure ---
static std::unordered_map<ANSCENTER::ANNHUBAPI*, int>& ANNHUBHandleRegistry() {
static std::unordered_map<ANSCENTER::ANNHUBAPI*, int> s;
return s;
}
static std::mutex& ANNHUBHandleRegistryMutex() {
static std::mutex m;
return m;
}
static std::condition_variable& ANNHUBHandleRegistryCV() {
static std::condition_variable cv;
return cv;
}
static void RegisterANNHUBHandle(ANSCENTER::ANNHUBAPI* h) {
std::lock_guard<std::mutex> lk(ANNHUBHandleRegistryMutex());
ANNHUBHandleRegistry()[h] = 1;
}
static ANSCENTER::ANNHUBAPI* AcquireANNHUBHandle(ANSCENTER::ANNHUBAPI* h) {
std::lock_guard<std::mutex> lk(ANNHUBHandleRegistryMutex());
auto it = ANNHUBHandleRegistry().find(h);
if (it == ANNHUBHandleRegistry().end()) return nullptr;
it->second++;
return h;
}
static bool ReleaseANNHUBHandleRef(ANSCENTER::ANNHUBAPI* h) {
std::lock_guard<std::mutex> lk(ANNHUBHandleRegistryMutex());
auto it = ANNHUBHandleRegistry().find(h);
if (it == ANNHUBHandleRegistry().end()) return false;
it->second--;
if (it->second <= 0) {
ANNHUBHandleRegistry().erase(it);
ANNHUBHandleRegistryCV().notify_all();
return true;
}
ANNHUBHandleRegistryCV().notify_all();
return false;
}
static bool UnregisterANNHUBHandle(ANSCENTER::ANNHUBAPI* h) {
std::unique_lock<std::mutex> lk(ANNHUBHandleRegistryMutex());
auto it = ANNHUBHandleRegistry().find(h);
if (it == ANNHUBHandleRegistry().end()) return false;
it->second--;
if (!ANNHUBHandleRegistryCV().wait_for(lk, std::chrono::seconds(30), [&]() {
auto it2 = ANNHUBHandleRegistry().find(h);
return it2 == ANNHUBHandleRegistry().end() || it2->second <= 0;
})) {
OutputDebugStringA("WARNING: UnregisterANNHUBHandle timed out after 30s waiting for refcount to reach zero.\n");
}
ANNHUBHandleRegistry().erase(h);
return true;
}
class ANNHUBHandleGuard {
ANSCENTER::ANNHUBAPI* engine;
public:
explicit ANNHUBHandleGuard(ANSCENTER::ANNHUBAPI* e) : engine(e) {}
~ANNHUBHandleGuard() { if (engine) ReleaseANNHUBHandleRef(engine); }
ANNHUBHandleGuard(const ANNHUBHandleGuard&) = delete;
ANNHUBHandleGuard& operator=(const ANNHUBHandleGuard&) = delete;
explicit operator bool() const { return engine != nullptr; }
ANSCENTER::ANNHUBAPI* get() const { return engine; }
};
// --- End handle registry infrastructure ---
static bool ansannhubLicenceValid = false;
// Global once_flag to protect license checking
static std::once_flag ansannhubLicenseOnceFlag;
namespace ANSCENTER {
void ReLu(std::vector<double>& iVal, std::vector<double>& oVal)
{
int dim = iVal.size();
oVal.resize(dim);
for (int i = 0; i < dim; i++)
{
if (iVal[i] >= 0) oVal[i] = iVal[i];
else oVal[i] = 0;
}
}
void LogSig(std::vector<double>& iVal, std::vector<double>& oVal)
{
int dim = iVal.size();
oVal.resize(dim);
for (int i = 0; i < dim; i++)
{
oVal[i] = 1 / (1 + exp(-iVal[i]));
}
}
void TanSig(std::vector<double>& iVal, std::vector<double>& oVal)
{
int dim = iVal.size();
oVal.resize(dim);
for (int i = 0; i < dim; i++)
{
oVal[i] = 2 / (1 + exp(-2 * iVal[i])) - 1;
}
}
void PureLin(std::vector<double>& iVal, std::vector<double>& oVal)
{
oVal = iVal;
}
void SoftMax(std::vector<double>& iVal, std::vector<double>& oVal)
{
double softmaxWeight = 0;
int dim = iVal.size();
oVal.resize(dim);
if (dim == 1) {
oVal[0] = 1 / (1 + exp(-iVal[0]));
}
else {
for (int i = 0; i < dim; i++) {
softmaxWeight = softmaxWeight + exp(iVal[i]);
}
for (int i = 0; i < dim; i++) {
oVal[i] = exp(iVal[i]) / softmaxWeight;
}
}
}
void ActivationFunction(std::vector<double>& iVal, std::vector<double>& oVal, int mode) {
switch (mode) {
case 0:
PureLin(iVal, oVal);
break;
case 1:
ReLu(iVal, oVal);
break;
case 2:
LogSig(iVal, oVal);
break;
case 3:
TanSig(iVal, oVal);
break;
case 4:
SoftMax(iVal, oVal);
break;
default:
TanSig(iVal, oVal);
break;
}
}
static void VerifyGlobalANSANNHUBLicense(const std::string& licenseKey) {
try {
ansannhubLicenceValid = ANSCENTER::ANSLicenseHelper::LicenseVerification(licenseKey, 1000, "ANNHUB-LV");//Default productId=1000
}
catch (std::exception& e) {
//this->_logger.LogFatal("ANSOPENCV::CheckLicense. Error:", e.what(), __FILE__, __LINE__);
}
}
void ANNHUBAPI::CheckLicense() {
//try {
// _licenseValid = ANSCENTER::ANSLicenseHelper::LicenseVerification(_licenseKey, 1000, "ANNHUB-LV");//Default productId=1000
//}
//catch (std::exception& e) {
// //this->_logger.LogFatal("ANSOPENCV::CheckLicense. Error:", e.what(), __FILE__, __LINE__);
//}
try {
// Check once globally
std::call_once(ansannhubLicenseOnceFlag, [this]() {
VerifyGlobalANSANNHUBLicense(_licenseKey);
});
// Update this instance's local license flag
_licenseValid = ansannhubLicenceValid;
}
catch (const std::exception& e) {
//this->_logger.LogFatal("ANNHUBAPI::CheckLicense. Error:", e.what(), __FILE__, __LINE__);
}
}
ANNHUBAPI::ANNHUBAPI()
{
// Control creation
isCreated = 0;
// Structrural parameters
nInputNodes = 1;
nHiddenNodes = 10;
nOutputNodes = 1;
hiddenActivation = 2; // default =2
outputActivation = 2; // default =2;
dataNormalisationModeInput = 0;
dataNormalisationModeOutput = 0;
}
ANNHUBAPI::~ANNHUBAPI() noexcept
{
try {
Destroy();
}
catch (...) {}
}
void ANNHUBAPI::Destroy()
{
if (isCreated != 0) {
FreeNeuralNetwork();
}
}
void ANNHUBAPI::FreeNeuralNetwork()
{
try {
// Clear vectors
IW.clear();
LW.clear();
nInput.clear();
nOutput.clear();
Ib.clear();
Lb.clear();
xminInput.clear();
xmaxInput.clear();
xminOutput.clear();
xmaxOutput.clear();
// Reset the flag indicating whether the network is created
isCreated = 0;
}
catch (const std::exception& e) {
//this->_logger.LogFatal("ANNHUBAPI::FreeNeuralNetwork. Error:", e.what(), __FILE__, __LINE__);
}
}
/*
(x[i]-xmin[i]) (ymax-ymin)
x[i] = ---------------- * +ymin
(xmax[i]-xmin[i])
*/
void ANNHUBAPI::PreProcessing(std::vector<double>& Input)
{
switch (dataNormalisationModeInput) {
case 0: // linear
break;
case 1: // mapminmax
for (int i = 0; i < nInputNodes; i++) {
if (xmaxInput[i] != xminInput[i]) {
Input[i] = (Input[i] - xminInput[i]) * (ymaxInput - yminInput) / (xmaxInput[i] - xminInput[i]) + yminInput;
}
}
break;
default:// minmaxmap
for (int i = 0; i < nInputNodes; i++) {
if (xmaxInput[i] != xminInput[i]) {
Input[i] = (Input[i] - xminInput[i]) * (ymaxInput - yminInput) / (xmaxInput[i] - xminInput[i]) + yminInput;
}
}
break;
}
}
void ANNHUBAPI::PostProcessing(std::vector<double>& Output)
{
switch (dataNormalisationModeOutput) {
case 0: // linear
break;
case 1:
for (int i = 0; i < nOutputNodes; i++) {
if (ymaxOutput != yminOutput) {
Output[i] = (Output[i] - yminOutput) * (xmaxOutput[i] - xminOutput[i]) / (ymaxOutput - yminOutput) + xminOutput[i];
}
}
break;
default:
for (int i = 0; i < nOutputNodes; i++) {
if (ymaxOutput != yminOutput) {
Output[i] = (Output[i] - yminOutput) * (xmaxOutput[i] - xminOutput[i]) / (ymaxOutput - yminOutput) + xminOutput[i];
}
}
break;
}
}
int ANNHUBAPI::ImportANNFromFile(std::string filename)
{
try {
FILE* fp;
int err = fopen_s(&fp, filename.c_str(), "r"); // r: Opens for reading.
if (err != 0) return -2;
float value, randNum;
//0. Check if it the correct type for C language
fscanf_s(fp, "%f", &value);
if (static_cast<int>(value) != 1) return -1; // Assume that the type C is 0
//1. Load random number
fscanf_s(fp, "%f", &randNum);
//2. Structure (IDs)
int trainingEngine, hlAct, olAct, costFunct, prePro, postPro, evalModel;
fscanf_s(fp, "%f", &value); trainingEngine = static_cast<int>(value);
fscanf_s(fp, "%f", &value); hlAct = static_cast<int>(value);
fscanf_s(fp, "%f", &value); olAct = static_cast<int>(value);
fscanf_s(fp, "%f", &value); costFunct = static_cast<int>(value);
fscanf_s(fp, "%f", &value); prePro = static_cast<int>(value);
fscanf_s(fp, "%f", &value); postPro = static_cast<int>(value);
fscanf_s(fp, "%f", &value); evalModel = static_cast<int>(value);
//2.1 Activation function
hiddenActivation = hlAct - 10;
outputActivation = olAct - 10;
//2.2 Data Processing
dataNormalisationModeInput = prePro - 1000;
dataNormalisationModeOutput = postPro - 1000;
// 3. Load neural network structure and min max inputs value for pre-post processing
int ipNodes, hdNodes, opNodes;
fscanf_s(fp, "%f", &value); ipNodes = static_cast<int>(value);
fscanf_s(fp, "%f", &value); hdNodes = static_cast<int>(value);
fscanf_s(fp, "%f", &value); opNodes = static_cast<int>(value);
Create(ipNodes, hdNodes, opNodes);
// 4. Load Input-Hidden weights (extraction formula)
for (int j = 0; j < nInputNodes; j++)
{
for (int i = 0; i < nHiddenNodes; i++)
{
fscanf_s(fp, "%f", &value);
IW[i][j] = value - randNum;
}
}
// 4.1. Load bias Hidden weights
for (int i = 0; i < nHiddenNodes; i++)
{
fscanf_s(fp, "%f", &value);
Ib[i] = value - randNum;
}
// 4.2. Load Hidden_Output weights
for (int j = 0; j < nHiddenNodes; j++)
{
for (int i = 0; i < nOutputNodes ; i++)
{
fscanf_s(fp, "%f", &value);
LW[i][j] = value - randNum;
}
}
// 4.3. Load bias Output weights
for (int i = 0; i < nOutputNodes; i++)
{
fscanf_s(fp, "%f", &value);
Lb[i] = value - randNum;
}
// 5. For Pre-processing
if (dataNormalisationModeInput >= 0) {
// Range settings
fscanf_s(fp, "%f", &value);
yminInput = value - randNum;
fscanf_s(fp, "%f", &value);
ymaxInput = value - randNum;
// Min and max
for (int i = 0; i < nInputNodes; i++) {
fscanf_s(fp, "%f", &value);
xminInput[i] = value - randNum;
}
for (int i = 0; i < nInputNodes; i++) {
fscanf_s(fp, "%f", &value);
xmaxInput[i] = value - randNum;
}
}
// 6. For Post-processing
if (dataNormalisationModeOutput >= 0) {
// Range settings
fscanf_s(fp, "%f", &value);
yminOutput = value - randNum;
fscanf_s(fp, "%f", &value);
ymaxOutput = value - randNum;
for (int i = 0; i < nOutputNodes; i++) {
fscanf_s(fp, "%f", &value);
xminOutput[i] = value - randNum;
}
for (int i = 0; i < nOutputNodes; i++) {
fscanf_s(fp, "%f", &value);
xmaxOutput[i] = value - randNum;
}
}
fclose(fp);
return 0;
}
catch (std::exception& e) {
//this->_logger.LogFatal("ANNHUBAPI::ImportANNFromFile. Error:", e.what(), __FILE__, __LINE__);
return -1;
}
}
void ANNHUBAPI::Create(int inputNodes, int hiddenNodes, int outputNodes)
{
try {
if (isCreated != 0) {
FreeNeuralNetwork();
}
nInputNodes = inputNodes;
nHiddenNodes = hiddenNodes;
nOutputNodes = outputNodes;
nInput.resize(inputNodes);
nOutput.resize(outputNodes);
IW.resize(hiddenNodes, std::vector<double>(inputNodes));
LW.resize(outputNodes, std::vector<double>(hiddenNodes));
Ib.resize(hiddenNodes);
Lb.resize(outputNodes);
xminInput.resize(inputNodes);
xmaxInput.resize(inputNodes);
xminOutput.resize(outputNodes);
xmaxOutput.resize(outputNodes);
isCreated = 1;
}
catch (std::exception& e) {
//this->_logger.LogFatal("ANNHUBAPI::Create. Error:", e.what(), __FILE__, __LINE__);
}
}
bool ANNHUBAPI::Init(std::string licenseKey, std::string modelFilePath) {
try {
_licenseKey = licenseKey;
_modelFilePath = modelFilePath;
CheckLicense();
if (_licenseValid) {
int result = ImportANNFromFile(_modelFilePath);
if (result == 0) return true;
else return false;
}
else {
return false;
}
}
catch (std::exception& e) {
//this->_logger.LogFatal("ANNHUBAPI::Init. Error:", e.what(), __FILE__, __LINE__);
return false;
}
}
std::vector<double> ANNHUBAPI::Inference(std::vector<double> ip) {
try {
int i, j;
std::vector<double> a1(nHiddenNodes), n1(nHiddenNodes), n2(nOutputNodes);
if (!_licenseValid) return {}; // Invalid license
if (isCreated == 0) return {};
// Need to check the input size as well, return {} if it fails
PreProcessing(ip);
//1. Calculate n1
for (i = 0; i < nHiddenNodes; i++) {
n1[i] = 0;
for (j = 0; j < nInputNodes; j++)
{
if (std::isnan(IW[i][j]) || std::isnan(ip[j])) {
continue;
}
n1[i] = n1[i] + IW[i][j] * ip[j];
}
n1[i] = n1[i] + Ib[i];
}
ActivationFunction(n1, a1, hiddenActivation);
// 3. Calculate n2
for (i = 0; i < nOutputNodes; i++) {
n2[i] = 0;
for (j = 0; j < nHiddenNodes; j++) {
n2[i] = n2[i] + LW[i][j] * a1[j];
}
n2[i] = n2[i] + Lb[i];
}
ActivationFunction(n2, nOutput, outputActivation);
PostProcessing(nOutput);
return nOutput;
}
catch (std::exception& e) {
//this->_logger.LogFatal("ANNHUBAPI::Inference. Error:", e.what(), __FILE__, __LINE__);
return {};
}
}
}
extern "C" __declspec(dllexport) int CreateANNHUBHandle(ANSCENTER::ANNHUBAPI * *Handle, const char* licenseKey, const char* modelFilePath) {
if (!Handle || !licenseKey || !modelFilePath) return -1;
try {
if (*Handle) {
if (UnregisterANNHUBHandle(*Handle)) {
(*Handle)->Destroy();
delete *Handle;
}
*Handle = nullptr;
}
auto ptr = std::make_unique<ANSCENTER::ANNHUBAPI>();
bool result = ptr->Init(licenseKey, modelFilePath);
if (result) {
*Handle = ptr.release();
RegisterANNHUBHandle(*Handle);
return 1;
}
*Handle = nullptr;
return 0;
}
catch (...) { return 0; }
}
static int ReleaseANNHUBHandle_Impl(ANSCENTER::ANNHUBAPI** Handle) {
if (Handle == nullptr || *Handle == nullptr) return -1;
UnregisterANNHUBHandle(*Handle);
(*Handle)->Destroy();
delete *Handle;
*Handle = nullptr;
return 0;
}
extern "C" __declspec(dllexport) int ReleaseANNHUBHandle(ANSCENTER::ANNHUBAPI * *Handle) {
__try {
return ReleaseANNHUBHandle_Impl(Handle);
}
__except (EXCEPTION_EXECUTE_HANDLER) {
if (Handle) *Handle = nullptr;
return 0;
}
}
extern "C" __declspec(dllexport) int ANNHUB_Inference(ANSCENTER::ANNHUBAPI * *pHandle, double* inputArray, int inputSize, double** outputArray) {
// Check for null pointers
if (pHandle == nullptr || *pHandle == nullptr || inputArray == nullptr || outputArray == nullptr || inputSize <= 0) {
return -1; // Invalid arguments
}
ANNHUBHandleGuard guard(AcquireANNHUBHandle(*pHandle));
if (!guard) return -1;
try {
// Convert inputArray to std::vector
std::vector<double> inputVector(inputArray, inputArray + inputSize);
// Call Inference
std::vector<double> inferenceResult = guard.get()->Inference(inputVector);
int resultSize = inferenceResult.size();
// Allocate memory for the output array
*outputArray = new double[resultSize];
if (*outputArray == nullptr) return -2; // Memory allocation failed
// Copy the inference result to the output array
std::copy(inferenceResult.begin(), inferenceResult.end(), *outputArray);
return 0; // Success
}
catch (const std::exception& e) {
// Log the exception message if you have a logging system
return -5; // Error code for exception in Inference
}
}
extern "C" __declspec(dllexport) int ANNHUB_InferenceLV(ANSCENTER::ANNHUBAPI * *pHandle, double* inputArray, int inputSize, LStrHandle outputHandle) {
// Check for null pointers
if (pHandle == nullptr || *pHandle == nullptr || inputArray == nullptr || outputHandle == nullptr || inputSize <= 0) {
return 0; // Invalid arguments
}
ANNHUBHandleGuard guard(AcquireANNHUBHandle(*pHandle));
if (!guard) return 0;
try {
// Convert inputArray to std::vector
std::vector<double> inputVector(inputArray, inputArray + inputSize);
// Call Inference
std::vector<double> inferenceResult = guard.get()->Inference(inputVector);
int resultSize = inferenceResult.size();
if (resultSize <= 0) return 0; // Error code for empty inference result
std::stringstream ss;
for (size_t i = 0; i < resultSize; ++i)
{
if (i == resultSize - 1) ss << inferenceResult[i];
else ss << inferenceResult[i]<<";";
}
std::string st= ss.str();
int size = st.length();
if (size > 0) {
MgErr error;
error = DSSetHandleSize(outputHandle, sizeof(int32) + size * sizeof(uChar));
if (error == noErr)
{
(*outputHandle)->cnt = size;
memcpy((*outputHandle)->str, st.c_str(), size);
return 1;
}
else return 0;
}
else return 0;
}
catch (const std::exception& e) {
// Log the exception message if you have a logging system
return 0; // Error code for exception in Inference
}
}
// --- V2 entry points: accept handle by value (uint64_t) to avoid LabVIEW buffer reuse bug ---
extern "C" __declspec(dllexport) int ANNHUB_Inference_V2(uint64_t handleVal, double* inputArray, int inputSize, double** outputArray) {
auto* _v2h = reinterpret_cast<ANSCENTER::ANNHUBAPI*>(handleVal);
if (!_v2h) return -1;
if (inputArray == nullptr || outputArray == nullptr || inputSize <= 0) return -1;
ANNHUBHandleGuard guard(AcquireANNHUBHandle(_v2h));
if (!guard) return -1;
try {
std::vector<double> inputVector(inputArray, inputArray + inputSize);
std::vector<double> inferenceResult = guard.get()->Inference(inputVector);
int resultSize = inferenceResult.size();
*outputArray = new double[resultSize];
if (*outputArray == nullptr) return -2;
std::copy(inferenceResult.begin(), inferenceResult.end(), *outputArray);
return 0;
}
catch (const std::exception& e) {
return -5;
}
}
extern "C" __declspec(dllexport) int ANNHUB_InferenceLV_V2(uint64_t handleVal, double* inputArray, int inputSize, LStrHandle outputHandle) {
auto* _v2h = reinterpret_cast<ANSCENTER::ANNHUBAPI*>(handleVal);
if (!_v2h) return 0;
if (inputArray == nullptr || outputHandle == nullptr || inputSize <= 0) return 0;
ANNHUBHandleGuard guard(AcquireANNHUBHandle(_v2h));
if (!guard) return 0;
try {
std::vector<double> inputVector(inputArray, inputArray + inputSize);
std::vector<double> inferenceResult = guard.get()->Inference(inputVector);
int resultSize = inferenceResult.size();
if (resultSize <= 0) return 0;
std::stringstream ss;
for (size_t i = 0; i < resultSize; ++i) {
if (i == resultSize - 1) ss << inferenceResult[i];
else ss << inferenceResult[i] << ";";
}
std::string st = ss.str();
int size = st.length();
if (size > 0) {
MgErr error;
error = DSSetHandleSize(outputHandle, sizeof(int32) + size * sizeof(uChar));
if (error == noErr) {
(*outputHandle)->cnt = size;
memcpy((*outputHandle)->str, st.c_str(), size);
return 1;
}
else return 0;
}
else return 0;
}
catch (const std::exception& e) {
return 0;
}
}
+69
View File
@@ -0,0 +1,69 @@
#ifndef ANSANNHUB_H
#define ANSANNHUB_H
#define ANSANNHUB_API __declspec(dllexport)
#include <iostream>
#include <cstdint>
#include "ANSLicense.h"
#include <vector>
//#include "LabVIEWHeader/extcode.h"
namespace ANSCENTER
{
class ANSANNHUB_API ANNHUBAPI
{
private:
std::vector<double> nInput; //ANN inputs
std::vector<double> nOutput; //ANN outputs
std::vector<std::vector<double>> IW;
std::vector<std::vector<double>> LW;
std::vector<double> Ib;
std::vector<double> Lb;
// Structural parameters
int nInputNodes, nHiddenNodes, nOutputNodes;
int hiddenActivation; // default =2
int outputActivation; // default =2
int dataNormalisationModeInput; // default =1;
int dataNormalisationModeOutput; // default =1;
// Preprocessing and postprocessing settings
std::vector<double> xmaxInput, xminInput; // Maximum and minimum of inputs
double ymaxInput, yminInput; // Maximum and minimum of inputs
std::vector<double> xmaxOutput, xminOutput; // Maximum and minimum of outputs
double ymaxOutput, yminOutput; // Maximum and minimum of outputs
// Control creation
unsigned char isCreated;
std::string _licenseKey;
bool _licenseValid{ false };
bool _isInitialized{ false };
std::string _modelFilePath;
private:
void PreProcessing(std::vector<double>& Input); // mode =0--> linear, mode =1 mapminmax, mode =2 standarddev
void PostProcessing(std::vector<double>& Output); // mode =0--> linear, mode =1 mapminmax, mode =2 standarddev
void Create(int inputNodes, int HiddenNodes, int outputNodes);
void FreeNeuralNetwork();
void CheckLicense();
int ImportANNFromFile(std::string filename);
public:
ANNHUBAPI();
~ANNHUBAPI() noexcept;
[[nodiscard]] bool Init(std::string licenseKey, std::string modelFilePath);
[[nodiscard]] std::vector<double> Inference(std::vector<double> ip);
void Destroy();
[[nodiscard]] int GetOutputNode() { return nOutputNodes; };
};
}
extern "C" __declspec(dllexport) int CreateANNHUBHandle(ANSCENTER::ANNHUBAPI **Handle, const char* licenseKey, const char* modelFilePath);
extern "C" __declspec(dllexport) int ReleaseANNHUBHandle(ANSCENTER::ANNHUBAPI **Handle);
extern "C" __declspec(dllexport) int ANNHUB_Inference(ANSCENTER::ANNHUBAPI **pHandle, double* inputArray, int inputSize, double** outputArray);
extern "C" __declspec(dllexport) int ANNHUB_InferenceLV(ANSCENTER::ANNHUBAPI **pHandle, double* inputArray, int inputSize, LStrHandle outputHandle);
// --- V2 entry points: accept handle by value (uint64_t) to avoid LabVIEW buffer reuse bug ---
extern "C" __declspec(dllexport) int ANNHUB_Inference_V2(uint64_t handleVal, double* inputArray, int inputSize, double** outputArray);
extern "C" __declspec(dllexport) int ANNHUB_InferenceLV_V2(uint64_t handleVal, double* inputArray, int inputSize, LStrHandle outputHandle);
#endif
+23
View File
@@ -0,0 +1,23 @@
# ANNHUB — Notification Hub DLL
add_library(ANNHUB SHARED
ANSANNHUB.cpp
ANSANNHUB.h
dllmain.cpp
pch.cpp
pch.h
framework.h
)
target_include_directories(ANNHUB PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${SHARED_INCLUDE_DIR}
)
target_link_libraries(ANNHUB
PRIVATE ANSLicensingSystem
PRIVATE anslicensing
PRIVATE labview
)
target_compile_definitions(ANNHUB PRIVATE UNICODE _UNICODE ANNHUB_EXPORTS _USRDLL)
target_precompile_headers(ANNHUB PRIVATE pch.h)
+19
View File
@@ -0,0 +1,19 @@
// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
) noexcept
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
+7
View File
@@ -0,0 +1,7 @@
#pragma once
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#define NOMINMAX // Prevent windows.h from defining min/max macros
// which break std::min / std::max (C2589)
// Windows Header Files
#include <windows.h>
+5
View File
@@ -0,0 +1,5 @@
// pch.cpp: source file corresponding to the pre-compiled header
#include "pch.h"
// When you are using pre-compiled headers, this source file is necessary for compilation to succeed.
+18
View File
@@ -0,0 +1,18 @@
// pch.h: This is a precompiled header file.
// Files listed below are compiled only once, improving build performance for future builds.
// This also affects IntelliSense performance, including code completion and many code browsing features.
// However, files listed here are ALL re-compiled if any one of them is updated between builds.
// Do not add files here that you will be updating frequently as this negates the performance advantage.
#ifndef PCH_H
#define PCH_H
// add headers that you want to pre-compile here
#include "framework.h"
#include <memory>
#include <string>
#include <string_view>
#include <vector>
#include <mutex>
#endif //PCH_H
+1169
View File
File diff suppressed because it is too large Load Diff
+111
View File
@@ -0,0 +1,111 @@
#ifndef ANSFLV_H
#define ANSFLV_H
#define ANSFLV_API __declspec(dllexport)
#include <iostream>
#include "ANSLicense.h"
#include "LabVIEWHeader/extcode.h"
#include <vector>
#include "sys_inc.h"
#include "http_flv_cln.h"
#include "hqueue.h"
#include "http.h"
#include "http_parse.h"
#include "video_decoder.h"
#include "audio_decoder.h"
#include "http_flv_player.h"
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
namespace ANSCENTER
{
class ANSFLV_API ANSFLVClient
{
protected:
std::unique_ptr<CHttpFlvPlayer> _playerClient = std::make_unique<CHttpFlvPlayer>();
std::string _username;
std::string _password;
std::string _url;
bool _useFullURL;
int _imageRotateDeg = 0;
int _displayWidth = 0; // 0 = no resize (return original resolution)
int _displayHeight = 0;
cv::Mat _pLastFrame;
std::string _lastJpegImage;
int _imageWidth, _imageHeight;
int64_t _pts;
bool _isPlaying;
std::recursive_mutex _mutex;
public:
ANSFLVClient();
~ANSFLVClient() noexcept;
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
[[nodiscard]] bool Init(std::string licenseKey, std::string username, std::string password, std::string url);
[[nodiscard]] bool Setup();
[[nodiscard]] bool Reconnect();
[[nodiscard]] bool Start();
[[nodiscard]] bool IsPaused();
[[nodiscard]] bool IsPlaying();
[[nodiscard]] bool IsRecording();
[[nodiscard]] bool Stop();
[[nodiscard]] bool Pause();
void Destroy();
void EnableAudio(bool status);
void SetAudioVolume(int volume);
bool areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2);
[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
[[nodiscard]] std::string GetJpegImage(int& width, int& height, int64_t& pts);
void SetImageRotate(int mode) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_imageRotateDeg = mode;
if (mode > 360) _imageRotateDeg = 360;
};
void SetBBox(cv::Rect bbox);
void SetCrop(bool crop);
static void SetMaxHWDecoders(int maxDecoders);
static int AutoConfigureHWDecoders(int maxPerGpuOverride = 0);
void SetHWDecoding(int hwMode, int preferredGpu = -1);
bool IsHWDecodingActive();
int GetHWDecodingGpuIndex();
void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize)
void SetImageQuality(int mode); // 0=fast (AI), 1=quality (display)
AVFrame* GetNV12Frame(); // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free)
AVFrame* GetCudaHWFrame(); // Returns CUDA HW frame (device ptrs) for zero-copy inference
bool IsCudaHWAccel(); // true when decoder uses CUDA (NV12 stays in GPU VRAM)
public:
void CheckLicense();
SPDLogger& _logger = SPDLogger::GetInstance("ANSFLV", false);
bool _licenseValid{ false };
std::string _licenseKey;
//std::once_flag licenseOnceFlag;
};
}
extern "C" __declspec(dllexport) int CreateANSFLVHandle(ANSCENTER::ANSFLVClient * *Handle, const char* licenseKey, const char* username, const char* password, const char* url);
extern "C" __declspec(dllexport) int ReleaseANSFLVHandle(ANSCENTER::ANSFLVClient * *Handle);
extern "C" __declspec(dllexport) int GetFLVImage(ANSCENTER::ANSFLVClient * *Handle, int& width, int& height, int64_t & timeStamp, LStrHandle jpegImage);
extern "C" __declspec(dllexport) int GetFLVStrImage(ANSCENTER::ANSFLVClient * *Handle, int& width, int& height, int64_t & timeStamp, std::string & jpegImage);
extern "C" __declspec(dllexport) int GetFLVCVImage(ANSCENTER::ANSFLVClient** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
extern "C" __declspec(dllexport) int StartFLV(ANSCENTER::ANSFLVClient * *Handle);
extern "C" __declspec(dllexport) int ReconnectFLV(ANSCENTER::ANSFLVClient * *Handle);
extern "C" __declspec(dllexport) int StopFLV(ANSCENTER::ANSFLVClient * *Handle);
extern "C" __declspec(dllexport) int PauseFLV(ANSCENTER::ANSFLVClient** Handle);
extern "C" __declspec(dllexport) int IsFLVPaused(ANSCENTER::ANSFLVClient * *Handle);
extern "C" __declspec(dllexport) int IsFLVRunning(ANSCENTER::ANSFLVClient * *Handle);
extern "C" __declspec(dllexport) int IsFLVRecording(ANSCENTER::ANSFLVClient * *Handle);
extern "C" __declspec(dllexport) void SetFLVAudioVolume(ANSCENTER::ANSFLVClient * *Handle, int volume);
extern "C" __declspec(dllexport) void EnableFLVAudioVolume(ANSCENTER::ANSFLVClient * *Handle, int status);
extern "C" __declspec(dllexport) void SetFLVImageRotation(ANSCENTER::ANSFLVClient * *Handle, double rotationAngle);
extern "C" __declspec(dllexport) int SetBBoxFLV(ANSCENTER::ANSFLVClient** Handle, int x, int y, int width, int height);
extern "C" __declspec(dllexport) int SetCropFlagFLV(ANSCENTER::ANSFLVClient** Handle, int cropFlag);
extern "C" __declspec(dllexport) void SetFLVMaxHWDecoders(int maxDecoders);
extern "C" __declspec(dllexport) int AutoConfigureFLVHWDecoders(int maxPerGpuOverride);
extern "C" __declspec(dllexport) void SetFLVHWDecoding(ANSCENTER::ANSFLVClient** Handle, int hwMode, int preferredGpu = -1);
extern "C" __declspec(dllexport) int IsFLVHWDecodingActive(ANSCENTER::ANSFLVClient** Handle);
extern "C" __declspec(dllexport) int GetFLVHWDecodingGpuIndex(ANSCENTER::ANSFLVClient** Handle);
extern "C" __declspec(dllexport) void SetFLVImageQuality(ANSCENTER::ANSFLVClient** Handle, int mode);
extern "C" __declspec(dllexport) void SetFLVDisplayResolution(ANSCENTER::ANSFLVClient** Handle, int width, int height);
#endif
+861
View File
@@ -0,0 +1,861 @@
#include "ANSFilePlayer.h"
#include "ANSMatRegistry.h"
#include "ANSGpuFrameOps.h"
#include <memory>
#include <cstdint>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
#include <libavutil/frame.h>
#include "media_codec.h"
static bool ansfileplayerLicenceValid = false;
// Global once_flag to protect license checking
static std::once_flag ansfileplayerLicenseOnceFlag;
namespace ANSCENTER {
#define CHECK_CUDA(call) do { \
cudaError_t err = call; \
if (err != cudaSuccess) { \
std::cout<<"CUDA error: " + std::string(cudaGetErrorString(err)); \
} \
} while (0)
#define CHECK_NVJPEG(call) do { \
nvjpegStatus_t status = call; \
if (status != NVJPEG_STATUS_SUCCESS) { \
std::cout <<"nvJPEG error: " + std::to_string(status); \
} \
} while (0)
ANSFILEPLAYER::ANSFILEPLAYER() {
_url = "";
_imageRotateDeg = 0;
_imageWidth = 0;
_imageHeight = 0;
_pts = 0;
_isPlaying = false;
_lastJpegImage = "";
}
ANSFILEPLAYER::~ANSFILEPLAYER() noexcept {
try {
Destroy();
}
catch (...) {}
}
void ANSFILEPLAYER::Destroy() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
_url = "";
_imageRotateDeg = 0;
_isPlaying = false;
_lastJpegImage = "";
_pLastFrame.release();
if (_playerClient) {
_playerClient->close();
}
}
catch (const std::exception& e) {
_logger.LogError("ANSFILEPLAYER::Destroy. Exception:", e.what(), __FILE__, __LINE__);
}
catch (...) {
_logger.LogError("ANSFILEPLAYER::Destroy.", "Unknown exception", __FILE__, __LINE__);
}
}
void ANSFILEPLAYER::CheckLicense() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
_licenseValid = ANSCENTER::ANSLicenseHelper::LicenseVerification(_licenseKey, 1007, "ANSCV");//Default productId=1005
}
catch (std::exception& e) {
this->_logger.LogFatal("ANSFILEPLAYER::CheckLicense. Error:", e.what(), __FILE__, __LINE__);
}
}
bool ANSFILEPLAYER::Init(std::string licenseKey, std::string url) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_licenseKey = licenseKey;
CheckLicense();
if (!_licenseValid) {
this->_logger.LogError("ANSFILEPLAYER::Init.", "Invalid license", __FILE__, __LINE__);
return false;
}
//network_init();
//sys_buf_init(200);
//rtsp_parse_buf_init(200);
//http_msg_buf_init(200);
_url = url;
return Setup();
}
void ANSFILEPLAYER::SetBBox(cv::Rect bbox) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_playerClient->setBbox(bbox);
}
void ANSFILEPLAYER::SetCrop(bool crop) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_playerClient->setCrop(crop);
}
bool ANSFILEPLAYER::Setup() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
return _playerClient->open(_url);
}
bool ANSFILEPLAYER::Reconnect() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_playerClient->close();
Setup();
return Start();
}
bool ANSFILEPLAYER::Start() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_isPlaying = _playerClient->play();
return _isPlaying;
}
bool ANSFILEPLAYER::Stop() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
if (_playerClient->pause()) {
_isPlaying = false;
return true;
}
else {
return false;
}
}
bool ANSFILEPLAYER::IsPaused() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
return _playerClient->isPaused();
}
bool ANSFILEPLAYER::IsPlaying() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
return _playerClient->isPlaying();
}
bool ANSFILEPLAYER::IsRecording() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
return _playerClient->isRecording();
}
std::string ANSFILEPLAYER::GetJpegImage(int& width, int& height, int64_t& pts) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
// If the player is playing, process the frame
if (_isPlaying) {
// Get a new frame from the player client
_lastJpegImage = _playerClient->getJpegImage(width, height, pts);
// Update internal state variables
_pts = pts;
_imageWidth = width;
_imageHeight = height;
// Return the frame
return _lastJpegImage;
}
// If the player is not playing, return the last known frame
else {
width = _imageWidth;
height = _imageHeight;
pts = _pts;
return _lastJpegImage;
}
}
cv::Mat ANSFILEPLAYER::GetImage(int& width, int& height, int64_t& pts) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
if (!_isPlaying) {
width = _imageWidth;
height = _imageHeight;
pts = _pts;
return _pLastFrame;
}
_pLastFrame.release();
int imgW = 0, imgH = 0;
_pLastFrame = _playerClient->getImage(imgW, imgH, pts);
if (_pLastFrame.empty()) {
width = _imageWidth;
height = _imageHeight;
pts = _pts;
return _pLastFrame;
}
cv::Mat result = _pLastFrame;
// Apply rotation if needed
if (_imageRotateDeg >= 1 && _imageRotateDeg != 0) {
cv::Point2f center(imgW / 2.0f, imgH / 2.0f);
cv::Mat rotMat = cv::getRotationMatrix2D(center, _imageRotateDeg, 1.0);
cv::Mat rotated;
cv::warpAffine(result, rotated, rotMat, result.size(),
cv::INTER_CUBIC, cv::BORDER_CONSTANT, cv::Scalar());
result = rotated;
}
// Store full-res for inference (before display resize)
_inferenceImage = result;
// Resize for display if display resolution is set
if (_displayWidth > 0 && _displayHeight > 0 &&
(result.cols != _displayWidth || result.rows != _displayHeight)) {
cv::Mat displayResult;
cv::resize(result, displayResult, cv::Size(_displayWidth, _displayHeight),
0, 0, cv::INTER_LINEAR);
result = displayResult;
}
_pts = pts;
width = result.cols;
height = result.rows;
_imageWidth = width;
_imageHeight = height;
return result;
}
void ANSFILEPLAYER::SetDisplayResolution(int width, int height) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_displayWidth = width;
_displayHeight = height;
}
cv::Mat ANSFILEPLAYER::GetInferenceImage() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
return _inferenceImage;
}
void ANSFILEPLAYER::EnableAudio(bool status) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_playerClient->enableAudio(status);
}
void ANSFILEPLAYER::SetAudioVolume(int volume) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_playerClient->setVolume(volume);
}
std::string ANSFILEPLAYER::MatToBinaryData(const cv::Mat& image) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
if (!image.empty()) {
if ((image.data != nullptr) && (image.u != nullptr)) {
try {
std::vector<uchar> imageData;
bool success = cv::imencode(".jpg", image, imageData);
if (!success) {
this->_logger.LogError("ANSFILEPLAYER::MatToBinaryData. Error:", "Failed to encode the image.", __FILE__, __LINE__);
return "";
}
std::string binaryData(imageData.begin(), imageData.end());
return binaryData;
}
catch (const std::exception& e) {
this->_logger.LogFatal("ANSFILEPLAYER::MatToBinaryData. Error:", e.what(), __FILE__, __LINE__);
return "";
}
catch (...) {
this->_logger.LogFatal("ANSFILEPLAYER::MatToBinaryData. Error:", "Caught unknown exception!", __FILE__, __LINE__);
return "";
}
}
else return "";
}
else return "";
}
static void VerifyGlobalANSFPLicense(const std::string& licenseKey) {
try {
ansfileplayerLicenceValid = ANSCENTER::ANSLicenseHelper::LicenseVerification(licenseKey, 1007, "ANSCV");//Default productId=1005
if (!ansfileplayerLicenceValid) { // we also support ANSTS license
ansfileplayerLicenceValid = ANSCENTER::ANSLicenseHelper::LicenseVerification(licenseKey, 1003, "ANSVIS");//Default productId=1003 (ANSVIS)
}
if (!ansfileplayerLicenceValid) { // we also support ANSTS license
ansfileplayerLicenceValid = ANSCENTER::ANSLicenseHelper::LicenseVerification(licenseKey, 1008, "ANSTS");//Default productId=1008 (ANSTS)
}
}
catch (std::exception& e) {
ansfileplayerLicenceValid = false;
}
}
}
extern "C" __declspec(dllexport) int CreateANSFilePlayerHandle(ANSCENTER::ANSFILEPLAYER** Handle, const char* licenseKey, const char* url) {
if (!Handle || !licenseKey || !url) return -1;
try {
auto ptr = std::make_unique<ANSCENTER::ANSFILEPLAYER>();
bool result = ptr->Init(licenseKey, url);
if (result) {
*Handle = ptr.release();
extern void anscv_unregister_handle(void*);
extern void anscv_register_handle(void*, void(*)(void*));
anscv_register_handle(*Handle, [](void* p) {
auto* h = static_cast<ANSCENTER::ANSFILEPLAYER*>(p);
try { h->Stop(); } catch (...) {}
try { h->Destroy(); } catch (...) {}
try { delete h; } catch (...) {}
});
return 1;
}
*Handle = nullptr;
return 0;
}
catch (...) { return 0; }
}
extern "C" __declspec(dllexport) int ReleaseANSFilePlayerHandle(ANSCENTER::ANSFILEPLAYER** Handle) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
extern void anscv_unregister_handle(void*);
anscv_unregister_handle(*Handle);
// Destructor calls Destroy() — no need to call it explicitly (avoids double-destroy)
std::unique_ptr<ANSCENTER::ANSFILEPLAYER> ptr(*Handle);
*Handle = nullptr;
return 0;
}
catch (...) {
if (Handle) *Handle = nullptr;
return 0;
}
}
extern "C" __declspec(dllexport) int GetFilePlayerStrImage(ANSCENTER::ANSFILEPLAYER** Handle, int& width, int& height, int64_t& timeStamp, std::string& jpegImage) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
jpegImage = (*Handle)->GetJpegStringImage(width, height, timeStamp);
if (!jpegImage.empty()) return 1;
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error getting image data from FilePlayer client: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int GetFilePlayerImage(ANSCENTER::ANSFILEPLAYER** Handle, int& width, int& height, int64_t& timeStamp, LStrHandle jpegImage) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
std::string jpegString = (*Handle)->GetJpegStringImage(width, height, timeStamp);
int size = jpegString.length();
if (size > 0) {
MgErr error;
// Resize the jpegImage handle to hold the image data
error = DSSetHandleSize(jpegImage, sizeof(int32) + size * sizeof(uChar));
// Check if resizing the handle was successful
if (error == noErr) {
// Set the size of the image in the handle
(*jpegImage)->cnt = size;
// Use memcpy to copy the data from the std::string to the LStrHandle's str buffer
memcpy((*jpegImage)->str, jpegString.c_str(), size);
// Return success
return 1;
}
else {
// Return failure if there was an error in resizing the handle
std::cerr << "Error resizing jpegImage handle: " << error << std::endl;
return 0;
}
}
else {
// If the JPEG image string is empty, return failure
std::cerr << "No image data retrieved from FilePlayer client." << std::endl;
return 0;
}
}
catch (const std::exception& e) {
std::cerr << "Error getting image data from FilePlayer client: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int GetFilePlayerCVImage(ANSCENTER::ANSFILEPLAYER** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image) {
if (!Handle || !(*Handle) || !image) {
std::cerr << "Error: Invalid input parameters in GetFilePlayerCVImage." << std::endl;
return -1;
}
try {
cv::Mat img = (*Handle)->GetImage(width, height, timeStamp);
if (img.empty()) {
return 0;
}
anscv_mat_replace(image, std::move(img));
// Attach NV12/CUDA frames to registry for inference zero-copy (same as ANSRTSP/ANSVideoPlayer).
// CFilePlayer inherits getNV12Frame()/getCudaHWFrame() from CVideoPlayer.
int gpuIdx = (*Handle)->_playerClient->getHWDecodingGpuIndex();
AVFrame* cudaHW = (*Handle)->_playerClient->getCudaHWFrame();
if (cudaHW) {
AVFrame* cpuNV12 = (*Handle)->_playerClient->getNV12Frame();
gpu_frame_attach_cuda(*image, cudaHW, gpuIdx, timeStamp, cpuNV12);
} else {
AVFrame* nv12 = (*Handle)->_playerClient->getNV12Frame();
if (nv12) {
gpu_frame_attach(*image, nv12, gpuIdx, timeStamp);
} else {
// CPU-only fallback: attach full-res BGR clone for ANSRTYOLO
cv::Mat infImg = (*Handle)->GetInferenceImage();
if (!infImg.empty() && infImg.data != (*image)->data) {
(*Handle)->_inferenceClonePrev = (*Handle)->_inferenceCloneCurr;
(*Handle)->_inferenceCloneCurr = infImg.clone();
GpuFrameData data{};
data.avframe = nullptr;
data.yPlane = (*Handle)->_inferenceCloneCurr.data;
data.uvPlane = nullptr;
data.yLinesize = static_cast<int>((*Handle)->_inferenceCloneCurr.step[0]);
data.uvLinesize = 0;
data.width = (*Handle)->_inferenceCloneCurr.cols;
data.height = (*Handle)->_inferenceCloneCurr.rows;
data.pixelFormat = 1000; // ANSCV_PIX_FMT_BGR24
data.gpuIndex = -1;
data.pts = timeStamp;
data.isCudaDevicePtr = false;
data.cpuAvframe = nullptr;
data.cpuYPlane = nullptr;
data.cpuUvPlane = nullptr;
data.cpuYLinesize = 0;
data.cpuUvLinesize = 0;
ANSGpuFrameRegistry::instance().attach(*image, std::move(data));
auto pending = ANSGpuFrameRegistry::instance().drain_pending();
for (void* p : pending) {
AVFrame* stale = static_cast<AVFrame*>(p);
av_frame_free(&stale);
}
}
}
}
return 1;
}
catch (const cv::Exception& e) {
std::cerr << "OpenCV exception in GetFilePlayerCVImage: " << e.what() << std::endl;
return -2;
}
catch (const std::exception& e) {
std::cerr << "Exception in GetFilePlayerCVImage: " << e.what() << std::endl;
return -2;
}
catch (...) {
std::cerr << "Unknown exception in GetFilePlayerCVImage" << std::endl;
return -2;
}
}
extern "C" __declspec(dllexport) int StartFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
bool result = (*Handle)->Start();
if (result) return 1;
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error starting file player: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int ReconnectFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
bool result = (*Handle)->Reconnect();
if (result) return 1;
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error reconnecting file player: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int StopFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
bool result = (*Handle)->Stop();
if (result) return 1;
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error stopping file player: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int IsFilePlayerPaused(ANSCENTER::ANSFILEPLAYER** Handle) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
bool result = (*Handle)->IsPaused();
if (result) return 1;
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error checking if file player is paused: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int IsFilePlayerRunning(ANSCENTER::ANSFILEPLAYER** Handle) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
bool result = (*Handle)->IsPlaying();
if (result) return 1;
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error checking if file player is running: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int IsFilePlayerRecording(ANSCENTER::ANSFILEPLAYER** Handle) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
bool result = (*Handle)->IsRecording();
if (result) return 1;
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error checking if file player is recording: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) void SetFilePlayerAudioVolume(ANSCENTER::ANSFILEPLAYER** Handle, int volume)
{
if (Handle == nullptr || *Handle == nullptr) return;
try {
(*Handle)->SetAudioVolume(volume);
}
catch (...) { }
}
extern "C" __declspec(dllexport) void EnableFilePlayerAudioVolume(ANSCENTER::ANSFILEPLAYER** Handle, int status)
{
if (Handle == nullptr || *Handle == nullptr) return;
try {
bool audioStatus = false;
if (status == 1)audioStatus = true;
(*Handle)->EnableAudio(audioStatus);
}
catch (const std::exception& e) {
std::cerr << "Error enabling audio for file player: " << e.what() << std::endl;
}
catch (...) { }
}
extern "C" __declspec(dllexport) void SetFilePlayerImageRotation(ANSCENTER::ANSFILEPLAYER** Handle, double rotationAngle) {
if (Handle == nullptr || *Handle == nullptr) return;
try {
(*Handle)->SetImageRotate(rotationAngle);
}
catch (...) { }
}
extern "C" __declspec(dllexport) int SetBBoxFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle, int x, int y, int width, int height) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
cv::Rect bbox(x, y, width, height);
(*Handle)->SetBBox(bbox);
return 1;
}
catch (const std::exception& e) {
std::cerr << "Error setting bounding box for file player: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int SetCropFlagFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle, int cropFlag) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
bool crop = false;
if (cropFlag == 1)crop = true;
(*Handle)->SetCrop(crop);
return 1;
}
catch (const std::exception& e) {
std::cerr << "Error setting crop flag for file player: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
// ============================================================================
// V2 functions: accept uint64_t handleVal by value (LabVIEW-safe)
// ============================================================================
extern "C" __declspec(dllexport) int GetFilePlayerImage_V2(uint64_t handleVal, int& width, int& height, int64_t& timeStamp, LStrHandle jpegImage) {
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
if (!h) return -1;
try {
std::string jpegString = h->GetJpegStringImage(width, height, timeStamp);
int size = jpegString.length();
if (size > 0) {
MgErr error;
error = DSSetHandleSize(jpegImage, sizeof(int32) + size * sizeof(uChar));
if (error == noErr) {
(*jpegImage)->cnt = size;
memcpy((*jpegImage)->str, jpegString.c_str(), size);
return 1;
}
else {
std::cerr << "Error resizing jpegImage handle: " << error << std::endl;
return 0;
}
}
else {
std::cerr << "No image data retrieved from FilePlayer client." << std::endl;
return 0;
}
}
catch (const std::exception& e) {
std::cerr << "Error getting image data from FilePlayer client: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int GetFilePlayerCVImage_V2(uint64_t handleVal, int& width, int& height, int64_t& timeStamp, cv::Mat** image) {
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
if (!h || !image) return -1;
try {
cv::Mat img = h->GetImage(width, height, timeStamp);
if (img.empty()) {
return 0;
}
anscv_mat_replace(image, std::move(img));
// NV12/CUDA registry attach (same as V1)
int gpuIdx = h->_playerClient->getHWDecodingGpuIndex();
AVFrame* cudaHW = h->_playerClient->getCudaHWFrame();
if (cudaHW) {
AVFrame* cpuNV12 = h->_playerClient->getNV12Frame();
gpu_frame_attach_cuda(*image, cudaHW, gpuIdx, timeStamp, cpuNV12);
} else {
AVFrame* nv12 = h->_playerClient->getNV12Frame();
if (nv12) {
gpu_frame_attach(*image, nv12, gpuIdx, timeStamp);
} else {
cv::Mat infImg = h->GetInferenceImage();
if (!infImg.empty() && infImg.data != (*image)->data) {
h->_inferenceClonePrev = h->_inferenceCloneCurr;
h->_inferenceCloneCurr = infImg.clone();
GpuFrameData data{};
data.avframe = nullptr;
data.yPlane = h->_inferenceCloneCurr.data;
data.uvPlane = nullptr;
data.yLinesize = static_cast<int>(h->_inferenceCloneCurr.step[0]);
data.uvLinesize = 0;
data.width = h->_inferenceCloneCurr.cols;
data.height = h->_inferenceCloneCurr.rows;
data.pixelFormat = 1000;
data.gpuIndex = -1;
data.pts = timeStamp;
data.isCudaDevicePtr = false;
data.cpuAvframe = nullptr;
data.cpuYPlane = nullptr;
data.cpuUvPlane = nullptr;
data.cpuYLinesize = 0;
data.cpuUvLinesize = 0;
ANSGpuFrameRegistry::instance().attach(*image, std::move(data));
auto pending = ANSGpuFrameRegistry::instance().drain_pending();
for (void* p : pending) {
AVFrame* stale = static_cast<AVFrame*>(p);
av_frame_free(&stale);
}
}
}
}
return 1;
}
catch (const cv::Exception& e) {
std::cerr << "OpenCV exception in GetFilePlayerCVImage_V2: " << e.what() << std::endl;
return -2;
}
catch (const std::exception& e) {
std::cerr << "Exception in GetFilePlayerCVImage_V2: " << e.what() << std::endl;
return -2;
}
catch (...) {
std::cerr << "Unknown exception in GetFilePlayerCVImage_V2" << std::endl;
return -2;
}
}
extern "C" __declspec(dllexport) int StartFilePlayer_V2(uint64_t handleVal) {
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
if (!h) return -1;
try {
bool result = h->Start();
if (result) return 1;
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error starting file player: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int ReconnectFilePlayer_V2(uint64_t handleVal) {
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
if (!h) return -1;
try {
bool result = h->Reconnect();
if (result) return 1;
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error reconnecting file player: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int StopFilePlayer_V2(uint64_t handleVal) {
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
if (!h) return -1;
try {
bool result = h->Stop();
if (result) return 1;
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error stopping file player: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int IsFilePlayerPaused_V2(uint64_t handleVal) {
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
if (!h) return -1;
try {
bool result = h->IsPaused();
if (result) return 1;
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error checking if file player is paused: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int IsFilePlayerRunning_V2(uint64_t handleVal) {
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
if (!h) return -1;
try {
bool result = h->IsPlaying();
if (result) return 1;
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error checking if file player is running: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int IsFilePlayerRecording_V2(uint64_t handleVal) {
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
if (!h) return -1;
try {
bool result = h->IsRecording();
if (result) return 1;
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error checking if file player is recording: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) void SetFilePlayerAudioVolume_V2(uint64_t handleVal, int volume) {
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
if (!h) return;
try {
h->SetAudioVolume(volume);
}
catch (...) { }
}
extern "C" __declspec(dllexport) void EnableFilePlayerAudioVolume_V2(uint64_t handleVal, int status) {
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
if (!h) return;
try {
bool audioStatus = false;
if (status == 1) audioStatus = true;
h->EnableAudio(audioStatus);
}
catch (const std::exception& e) {
std::cerr << "Error enabling audio for file player: " << e.what() << std::endl;
}
catch (...) { }
}
extern "C" __declspec(dllexport) void SetFilePlayerImageRotation_V2(uint64_t handleVal, double rotationAngle) {
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
if (!h) return;
try {
h->SetImageRotate(rotationAngle);
}
catch (...) { }
}
extern "C" __declspec(dllexport) int SetBBoxFilePlayer_V2(uint64_t handleVal, int x, int y, int width, int height) {
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
if (!h) return -1;
try {
cv::Rect bbox(x, y, width, height);
h->SetBBox(bbox);
return 1;
}
catch (const std::exception& e) {
std::cerr << "Error setting bounding box for file player: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int SetCropFlagFilePlayer_V2(uint64_t handleVal, int cropFlag) {
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
if (!h) return -1;
try {
bool crop = false;
if (cropFlag == 1) crop = true;
h->SetCrop(crop);
return 1;
}
catch (const std::exception& e) {
std::cerr << "Error setting crop flag for file player: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) void SetFilePlayerDisplayResolution(ANSCENTER::ANSFILEPLAYER** Handle, int width, int height) {
if (Handle == nullptr || *Handle == nullptr) return;
try {
(*Handle)->SetDisplayResolution(width, height);
} catch (...) { }
}
+117
View File
@@ -0,0 +1,117 @@
#ifndef ANSFILEPLAYER_H
#define ANSFILEPLAYER_H
#define ANSFILEPLAYER_API __declspec(dllexport)
#include <iostream>
#include <cstdint>
#include "ANSLicense.h"
#include "LabVIEWHeader/extcode.h"
#include <vector>
#include "sys_inc.h"
#include "rtsp_cln.h"
#include "hqueue.h"
#include "http.h"
#include "http_parse.h"
#include "video_decoder.h"
#include "audio_decoder.h"
#include "file_player.h"
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <thread>
#include <chrono>
#include <atomic>
#include <turbojpeg.h>
namespace ANSCENTER
{
class ANSFILEPLAYER_API ANSFILEPLAYER
{
public:
std::unique_ptr<CFilePlayer> _playerClient = std::make_unique<CFilePlayer>();
protected:
std::string _url;
double _imageRotateDeg = 0;
cv::Mat _pLastFrame;
std::string _lastJpegImage;
int _imageWidth = 0, _imageHeight = 0;
int64_t _pts = 0;
bool _isPlaying = false;
std::recursive_mutex _mutex;
// Display/inference split (same pattern as ANSVideoPlayer)
int _displayWidth = 0;
int _displayHeight = 0;
cv::Mat _inferenceImage; // full-res, no display resize
public:
// Public: accessed by extern "C" GetFilePlayerCVImage for NV12/CUDA registry attachment
cv::Mat _inferenceCloneCurr; // current clone for registry (keeps data alive)
cv::Mat _inferenceClonePrev; // previous clone (keeps data alive until next frame)
ANSFILEPLAYER();
~ANSFILEPLAYER() noexcept;
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
[[nodiscard]] bool Setup();
[[nodiscard]] bool Reconnect();
[[nodiscard]] bool Start();
void SetBBox(cv::Rect bbox);
void SetCrop(bool crop);
[[nodiscard]] bool IsPaused();
[[nodiscard]] bool IsPlaying();
[[nodiscard]] bool IsRecording();
[[nodiscard]] bool Stop();
void Destroy();
void EnableAudio(bool status);
void SetAudioVolume(int volume);
void SetImageRotate(double mode) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_imageRotateDeg = mode;
if (mode > 360) _imageRotateDeg = 360;
};
void SetDisplayResolution(int width, int height);
[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
[[nodiscard]] cv::Mat GetInferenceImage();
[[nodiscard]] std::string GetJpegImage(int& width, int& height, int64_t& pts);
[[nodiscard]] std::string GetJpegStringImage(int& width, int& height, int64_t& pts) {
return GetJpegImage(width, height, pts);
}
[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
public:
void CheckLicense();
SPDLogger& _logger = SPDLogger::GetInstance("ANSFilePlayer", false);
bool _licenseValid{ false };
std::string _licenseKey;
};
}
extern "C" __declspec(dllexport) int CreateANSFilePlayerHandle(ANSCENTER::ANSFILEPLAYER* *Handle, const char* licenseKey, const char* url);
extern "C" __declspec(dllexport) int ReleaseANSFilePlayerHandle(ANSCENTER::ANSFILEPLAYER* *Handle);
extern "C" __declspec(dllexport) int GetFilePlayerImage(ANSCENTER::ANSFILEPLAYER* *Handle, int& width, int& height, int64_t & timeStamp, LStrHandle jpegImage);
extern "C" __declspec(dllexport) int GetFilePlayerStrImage(ANSCENTER::ANSFILEPLAYER* *Handle, int& width, int& height, int64_t & timeStamp, std::string & jpegImage);
extern "C" __declspec(dllexport) int GetFilePlayerCVImage(ANSCENTER::ANSFILEPLAYER** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
extern "C" __declspec(dllexport) int StartFilePlayer(ANSCENTER::ANSFILEPLAYER* *Handle);
extern "C" __declspec(dllexport) int ReconnectFilePlayer(ANSCENTER::ANSFILEPLAYER* *Handle);
extern "C" __declspec(dllexport) int StopFilePlayer(ANSCENTER::ANSFILEPLAYER* *Handle);
extern "C" __declspec(dllexport) int IsFilePlayerPaused(ANSCENTER::ANSFILEPLAYER* *Handle);
extern "C" __declspec(dllexport) int IsFilePlayerRunning(ANSCENTER::ANSFILEPLAYER* *Handle);
extern "C" __declspec(dllexport) int IsFilePlayerRecording(ANSCENTER::ANSFILEPLAYER* *Handle);
extern "C" __declspec(dllexport) void SetFilePlayerAudioVolume(ANSCENTER::ANSFILEPLAYER* *Handle, int volume);
extern "C" __declspec(dllexport) void EnableFilePlayerAudioVolume(ANSCENTER::ANSFILEPLAYER* *Handle, int status);
extern "C" __declspec(dllexport) void SetFilePlayerImageRotation(ANSCENTER::ANSFILEPLAYER* *Handle, double rotationAngle);
extern "C" __declspec(dllexport) int SetBBoxFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle, int x, int y, int width, int height);
extern "C" __declspec(dllexport) int SetCropFlagFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle, int cropFlag);
extern "C" __declspec(dllexport) void SetFilePlayerDisplayResolution(ANSCENTER::ANSFILEPLAYER** Handle, int width, int height);
// V2 functions: accept uint64_t handleVal by value (LabVIEW-safe)
extern "C" __declspec(dllexport) int GetFilePlayerImage_V2(uint64_t handleVal, int& width, int& height, int64_t& timeStamp, LStrHandle jpegImage);
extern "C" __declspec(dllexport) int GetFilePlayerCVImage_V2(uint64_t handleVal, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
extern "C" __declspec(dllexport) int StartFilePlayer_V2(uint64_t handleVal);
extern "C" __declspec(dllexport) int ReconnectFilePlayer_V2(uint64_t handleVal);
extern "C" __declspec(dllexport) int StopFilePlayer_V2(uint64_t handleVal);
extern "C" __declspec(dllexport) int IsFilePlayerPaused_V2(uint64_t handleVal);
extern "C" __declspec(dllexport) int IsFilePlayerRunning_V2(uint64_t handleVal);
extern "C" __declspec(dllexport) int IsFilePlayerRecording_V2(uint64_t handleVal);
extern "C" __declspec(dllexport) void SetFilePlayerAudioVolume_V2(uint64_t handleVal, int volume);
extern "C" __declspec(dllexport) void EnableFilePlayerAudioVolume_V2(uint64_t handleVal, int status);
extern "C" __declspec(dllexport) void SetFilePlayerImageRotation_V2(uint64_t handleVal, double rotationAngle);
extern "C" __declspec(dllexport) int SetBBoxFilePlayer_V2(uint64_t handleVal, int x, int y, int width, int height);
extern "C" __declspec(dllexport) int SetCropFlagFilePlayer_V2(uint64_t handleVal, int cropFlag);
#endif
+762
View File
@@ -0,0 +1,762 @@
#include "ANSFilePlayer_CV.h"
#include "ANSMatRegistry.h"
#include <memory>
#include <cstdint>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
#include <libavutil/frame.h>
#include "media_codec.h"
static bool ansfileplayerLicenceValid = false;
// Global once_flag to protect license checking
static std::once_flag ansfileplayerLicenseOnceFlag;
namespace ANSCENTER {
#define CHECK_CUDA(call) do { \
cudaError_t err = call; \
if (err != cudaSuccess) { \
std::cout<<"CUDA error: " + std::string(cudaGetErrorString(err)); \
} \
} while (0)
#define CHECK_NVJPEG(call) do { \
nvjpegStatus_t status = call; \
if (status != NVJPEG_STATUS_SUCCESS) { \
std::cout <<"nvJPEG error: " + std::to_string(status); \
} \
} while (0)
ANSFILEPLAYER_CV::ANSFILEPLAYER_CV() :_stopThread(std::make_shared<std::atomic<bool>>(false))
{
_resWidth = 0;
_resHeight = 0;
m_bPaused = false;
_crop = false;
_isPlaying = false;
_lastJpegImage = "";
_bbox = cv::Rect(0, 0, 0, 0);
_jpegCompressor = nullptr;
_previousPTS = 0;
_totalFrames = 0;
}
ANSFILEPLAYER_CV::~ANSFILEPLAYER_CV() noexcept {
try {
StopTimerThread();
Destroy();
}
catch (...) {}
}
void ANSFILEPLAYER_CV::Destroy() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
_previousImage.release();
_lastJpegImage = "";
_isPlaying = false;
_resWidth = 0;
_resHeight = 0;
_currentFrame = 0;
_previousPTS = 0;
if (cap.isOpened()) {
cap.release();
}
}
catch (const std::exception& e) {
_logger.LogError("ANSFILEPLAYER_CV::Destroy. Exception:", e.what(), __FILE__, __LINE__);
}
catch (...) {
_logger.LogError("ANSFILEPLAYER_CV::Destroy.", "Unknown exception", __FILE__, __LINE__);
}
}
static void VerifyGlobalANSFPLicense(const std::string& licenseKey) {
try {
ansfileplayerLicenceValid = ANSCENTER::ANSLicenseHelper::LicenseVerification(licenseKey, 1007, "ANSCV");//Default productId=1005
if (!ansfileplayerLicenceValid) { // we also support ANSTS license
ansfileplayerLicenceValid = ANSCENTER::ANSLicenseHelper::LicenseVerification(licenseKey, 1003, "ANSVIS");//Default productId=1003 (ANSVIS)
}
if (!ansfileplayerLicenceValid) { // we also support ANSTS license
ansfileplayerLicenceValid = ANSCENTER::ANSLicenseHelper::LicenseVerification(licenseKey, 1008, "ANSTS");//Default productId=1008 (ANSTS)
}
}
catch (std::exception& e) {
ansfileplayerLicenceValid = false;
}
}
void ANSFILEPLAYER_CV::CheckLicense() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
// Check once globally
std::call_once(ansfileplayerLicenseOnceFlag, [this]() {
VerifyGlobalANSFPLicense(_licenseKey);
});
// Update this instance's local license flag
_licenseValid = ansfileplayerLicenceValid;
}
catch (const std::exception& e) {
this->_logger.LogFatal("ANSFILEPLAYER_CV::CheckLicense. Error:", e.what(), __FILE__, __LINE__);
}
}
bool ANSFILEPLAYER_CV::Init(std::string licenseKey, std::string url) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_licenseKey = licenseKey;
CheckLicense();
if (!_licenseValid) {
_logger.LogError("ANSFILEPLAYER_CV::Init.", "Invalid license", __FILE__, __LINE__);
return false;
}
_url = url;
return Setup();
}
bool ANSFILEPLAYER_CV::Setup() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_currentFrame = 0;
if (cap.isOpened()) return true;
cap.open(_url, cv::CAP_FFMPEG); // USe FFMPEG for better codec support
if (!cap.isOpened()) {
cap.open(_url, cv::CAP_ANY);
if (!cap.isOpened())return false;
}
try {
_resWidth = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_WIDTH));
_resHeight = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_HEIGHT));
_totalFrames = static_cast<int64_t>(cap.get(cv::CAP_PROP_FRAME_COUNT));
_fps = cap.get(cv::CAP_PROP_FPS);
cap.set(cv::CAP_PROP_POS_FRAMES, _currentFrame);
if (_fps > 0) {
if (_timerThread && _timerThread->joinable()) return true; // Exit if the thread is already running
int interval_ms = static_cast<int>(1000 / _fps);
*_stopThread = false;
_timerThread = std::make_shared<std::thread>(&ANSFILEPLAYER_CV::StartTimer, this, _stopThread, interval_ms);
}
return true;
}
catch (std::exception& e) {
this->_logger.LogError("ANSFILEPLAYER_CV::Setup:", e.what(), __FILE__, __LINE__);
return false;
}
}
void ANSFILEPLAYER_CV::StartTimer(std::shared_ptr<std::atomic<bool>> stopFlag, int interval_ms) {
while (!stopFlag->load()) {
std::unique_lock<std::mutex> lock(_cvMutex);
// Wait for the specified interval or until stopFlag is set
if (_cv.wait_for(lock, std::chrono::milliseconds(interval_ms), [&]() {
return stopFlag->load();
})) {
break; // Exit the loop if stopFlag is true
}
lock.unlock(); // Unlock to avoid deadlocks in further operations
int delayMs = 0;
int actualSleepTime = interval_ms;
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
auto start = std::chrono::system_clock::now();
if (_isPlaying) {
try {
_currentFrame = static_cast<int64_t>(cap.get(cv::CAP_PROP_POS_FRAMES));
// Add check for _stopThread before blocking operations
if (!cap.grab() || _currentFrame >= _totalFrames - 1) {
if (cap.isOpened()) {
cap.release();
cap.open(_url, cv::CAP_FFMPEG); // USe FFMPEG for better codec support
if (!cap.isOpened()) {
cap.open(_url, cv::CAP_ANY);
}
}
if (cap.isOpened()) {
cap.set(cv::CAP_PROP_POS_FRAMES, 0);
_currentFrame = 0;
}
}
}
catch (const std::exception& e) {
this->_logger.LogError("ANSFILEPLAYER_CV::StartTimer. Exception occurred:", e.what(), __FILE__, __LINE__);
}
}
auto end = std::chrono::system_clock::now();
delayMs = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
// Calculate actual sleep time based on any delay introduced
actualSleepTime = interval_ms - delayMs;
if (actualSleepTime < 1) actualSleepTime = 1;
}
std::this_thread::sleep_for(std::chrono::milliseconds(actualSleepTime));
}
}
void ANSFILEPLAYER_CV::StopTimerThread() {
*_stopThread = true; // Signal to stop the thread
_cv.notify_all(); // Notify the condition variable to unblock the thread
if (_timerThread && _timerThread->joinable()) {
_timerThread->join(); // Join the timer thread to ensure cleanup
}
}
bool ANSFILEPLAYER_CV::Reconnect() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
_currentFrame = 0;
if (cap.isOpened()) {
cap.release();
}
Setup();
_isPlaying = cap.isOpened() && cap.grab();
return _isPlaying;
}
catch (const std::exception& e) {
this->_logger.LogError("ANSFILEPLAYER_CV::Reconnect. Exception occurred:", e.what(), __FILE__, __LINE__);
_currentFrame = 0;
return false;
}
}
bool ANSFILEPLAYER_CV::Start() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
if (!cap.isOpened()) {
cap.open(_url, cv::CAP_FFMPEG);
if (!cap.isOpened()) {
cap.open(_url, cv::CAP_ANY);
}
if (!cap.isOpened()) {
this->_logger.LogError("ANSFILEPLAYER_CV::Start. Exception occurred:", "Failed to open video source: ", __FILE__, __LINE__);
return false;
}
try {
if (!cap.set(cv::CAP_PROP_POS_FRAMES, _currentFrame)) {
this->_logger.LogError("ANSFILEPLAYER_CV::Start. Exception occurred:", "Warning: Unable to seek to frame", __FILE__, __LINE__);
}
}
catch (const std::exception& e) {
this->_logger.LogError("ANSFILEPLAYER_CV::Start. Exception occurred:", e.what(), __FILE__, __LINE__);
}
}
_isPlaying = cap.isOpened() && cap.grab();
return _isPlaying;
}
catch (const std::exception& e) {
this->_logger.LogError("ANSFILEPLAYER_CV::Start. Exception occurred:", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSFILEPLAYER_CV::Stop() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
if (cap.isOpened()) {
try {
double frame_pos = cap.get(cv::CAP_PROP_POS_FRAMES);
if (frame_pos >= 0) {
_currentFrame = static_cast<int64_t>(frame_pos);
}
else {
_currentFrame = 0;
this->_logger.LogError("ANSFILEPLAYER_CV::Stop. Exception occurred:", "Unable to retrieve current frame position", __FILE__, __LINE__);
}
}
catch (const std::exception& e) {
this->_logger.LogError("ANSFILEPLAYER_CV::Stop. Exception occurred:", e.what(), __FILE__, __LINE__);
_currentFrame = 0;
}
cap.release();
}
_isPlaying = false;
return true;
}
catch (const std::exception& e) {
this->_logger.LogError("ANSFILEPLAYER_CV::Stop. Exception occurred:", e.what(), __FILE__, __LINE__);
return false;
}
}
void ANSFILEPLAYER_CV::SetBBox(cv::Rect bbox) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_bbox = bbox;
}
void ANSFILEPLAYER_CV::SetCrop(bool crop) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_crop = crop;
}
bool ANSFILEPLAYER_CV::IsPaused() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
return !cap.isOpened();
}
bool ANSFILEPLAYER_CV::IsPlaying() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
return cap.isOpened();
}
bool ANSFILEPLAYER_CV::IsRecording() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
return false;// do not support recording for webcam
}
cv::Mat ANSFILEPLAYER_CV::GetImage(int& width, int& height, int64_t& pts) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
if (!_isPlaying) {
if (!_previousImage.empty()) {
width = _previousImage.cols;
height = _previousImage.rows;
pts = _previousPTS;
return _previousImage;
}
return cv::Mat();
}
try {
if (!cap.isOpened()) {
if (!_previousImage.empty()) {
width = _previousImage.cols;
height = _previousImage.rows;
pts = _previousPTS;
return _previousImage;
}
return cv::Mat();
}
cv::Mat frame;
if (!cap.read(frame) || frame.empty()) {
// Return last good frame if available
if (!_previousImage.empty()) {
width = _previousImage.cols;
height = _previousImage.rows;
pts = _previousPTS;
return _previousImage;
}
return cv::Mat();
}
cv::Mat result;
// Apply cropping if enabled
if (_crop) {
// Validate and clamp crop region
_bbox.x = std::clamp(_bbox.x, 0, frame.cols - 1);
_bbox.y = std::clamp(_bbox.y, 0, frame.rows - 1);
_bbox.width = std::min(_bbox.width, frame.cols - _bbox.x);
_bbox.height = std::min(_bbox.height, frame.rows - _bbox.y);
if (_bbox.width > 0 && _bbox.height > 0) {
// CRITICAL: Clone to avoid dangling reference
result = frame(_bbox).clone();
}
else {
result = frame.clone();
}
}
else {
result = frame.clone();
}
// Apply rotation if specified
if (_imageRotateDeg > 0) {
// Fast path for 90-degree rotations
if (std::abs(_imageRotateDeg - 90.0) < 0.01) {
cv::rotate(result, result, cv::ROTATE_90_CLOCKWISE);
}
else if (std::abs(_imageRotateDeg - 180.0) < 0.01) {
cv::rotate(result, result, cv::ROTATE_180);
}
else if (std::abs(_imageRotateDeg - 270.0) < 0.01) {
cv::rotate(result, result, cv::ROTATE_90_COUNTERCLOCKWISE);
}
else {
// Arbitrary angle rotation
const cv::Point2f center(result.cols / 2.0f, result.rows / 2.0f);
cv::Mat rotationMatrix = cv::getRotationMatrix2D(center, _imageRotateDeg, 1.0);
cv::Mat rotated;
// Use INTER_LINEAR instead of INTER_CUBIC (2-3x faster, minimal quality loss)
cv::warpAffine(result, rotated, rotationMatrix, result.size(),
cv::INTER_LINEAR, cv::BORDER_CONSTANT, cv::Scalar());
result = rotated;
}
}
// Update PTS (presentation timestamp)
if (_previousPTS < INT64_MAX) {
_previousPTS++;
}
else {
_previousPTS = 0;
}
// Update cached frame
_previousImage = result;
// Set output parameters
width = result.cols;
height = result.rows;
pts = _previousPTS;
return result;
}
catch (const cv::Exception& e) {
this->_logger.LogError("ANSFILEPLAYER_CV::GetImage. OpenCV exception:", e.what(), __FILE__, __LINE__);
}
catch (const std::exception& e) {
this->_logger.LogError("ANSFILEPLAYER_CV::GetImage. Exception:", e.what(), __FILE__, __LINE__);
}
catch (...) {
this->_logger.LogError("ANSFILEPLAYER_CV::GetImage. Unknown exception", "", __FILE__, __LINE__);
}
return cv::Mat();
}
void ANSFILEPLAYER_CV::EnableAudio(bool status) {
// please support audio enable for webcam
}
void ANSFILEPLAYER_CV::SetAudioVolume(int volume) {
// support audio volumne
}
std::string ANSFILEPLAYER_CV::encodeJpegString(const cv::Mat& img, int quality) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
if (!_isPlaying) return _lastJpegImage;
unsigned char* jpegBuf = nullptr;
tjhandle jpegCompressor = nullptr;
try {
// Initialize TurboJPEG compressor
jpegCompressor = tjInitCompress();
if (!jpegCompressor) {
this->_logger.LogError("Failed to initialize TurboJPEG compressor.", tjGetErrorStr(), __FILE__, __LINE__);
return _lastJpegImage;
}
int maxBufferSize = img.cols * img.rows * 3;
jpegBuf = tjAlloc(maxBufferSize);
if (!jpegBuf) {
this->_logger.LogError("Failed to allocate memory for JPEG buffer.", tjGetErrorStr(), __FILE__, __LINE__);
tjDestroy(jpegCompressor);
return _lastJpegImage;
}
long unsigned int jpegSize = maxBufferSize;
int subsamp = TJSAMP_444;
int pixelFormat = img.channels() == 3 ? TJPF_BGR : TJPF_GRAY;
// Compress the image
if (tjCompress2(jpegCompressor, img.data, img.cols, 0, img.rows, pixelFormat,
&jpegBuf, &jpegSize, subsamp, quality, TJFLAG_FASTDCT) != 0) {
this->_logger.LogError("Compression error:", tjGetErrorStr(), __FILE__, __LINE__);
tjFree(jpegBuf);
tjDestroy(jpegCompressor);
return _lastJpegImage;
}
// Create string from JPEG buffer
std::string jpegString(reinterpret_cast<char*>(jpegBuf), jpegSize);
_lastJpegImage = jpegString;
}
catch (const std::exception& e) {
this->_logger.LogError("Exception occurred:", e.what(), __FILE__, __LINE__);
}
// Cleanup resources
if (jpegBuf) tjFree(jpegBuf);
if (jpegCompressor) tjDestroy(jpegCompressor);
return _lastJpegImage;
}
std::string ANSFILEPLAYER_CV::MatToBinaryData(const cv::Mat& image) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
// Check if the image is empty or has invalid data
if (!_isPlaying) return _lastJpegImage;
if (image.empty() || !image.data || !image.u) {
return _lastJpegImage;
}
try {
// Encode the image to a memory buffer
return encodeJpegString(image, 85);
}
catch (const std::exception& e) {
this->_logger.LogFatal("ANSFILEPLAYER_CV::MatToBinaryData. Exception occurred:", e.what(), __FILE__, __LINE__);
}
catch (...) {
this->_logger.LogFatal("ANSFILEPLAYER_CV::MatToBinaryData.", "Unknown exception occurred.", __FILE__, __LINE__);
}
// Return an empty string in case of failure
return _lastJpegImage;
}
std::string ANSFILEPLAYER_CV::GetJpegStringImage(int& width, int& height, int64_t& pts) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
if (!_isPlaying) return _lastJpegImage;
try {
cv::Mat image = GetImage(width, height, pts);
std::string jpegString = MatToBinaryData(image);
image.release();
return jpegString;
}
catch (const std::exception& e) {
this->_logger.LogError("ANSFILEPLAYER_CV::GetJpegStringImage. Exception occurred:", e.what(), __FILE__, __LINE__);
return _lastJpegImage;
}
}
}
extern "C" __declspec(dllexport) int CreateANSFileCVPlayerHandle(ANSCENTER::ANSFILEPLAYER_CV** Handle, const char* licenseKey, const char* url) {
if (!Handle || !licenseKey || !url) return -1;
try {
auto ptr = std::make_unique<ANSCENTER::ANSFILEPLAYER_CV>();
bool result = ptr->Init(licenseKey, url);
if (result) {
*Handle = ptr.release();
extern void anscv_unregister_handle(void*);
extern void anscv_register_handle(void*, void(*)(void*));
anscv_register_handle(*Handle, [](void* p) {
auto* h = static_cast<ANSCENTER::ANSFILEPLAYER_CV*>(p);
try { h->Stop(); } catch (...) {}
try { h->Destroy(); } catch (...) {}
try { delete h; } catch (...) {}
});
return 1;
}
*Handle = nullptr;
return 0;
}
catch (...) { return 0; }
}
extern "C" __declspec(dllexport) int ReleaseANSFileCVPlayerHandle(ANSCENTER::ANSFILEPLAYER_CV** Handle) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
extern void anscv_unregister_handle(void*);
anscv_unregister_handle(*Handle);
// Destructor calls Destroy() � no need to call it explicitly (avoids double-destroy)
std::unique_ptr<ANSCENTER::ANSFILEPLAYER_CV> ptr(*Handle);
*Handle = nullptr;
return 0;
}
catch (...) {
if (Handle) *Handle = nullptr;
return 0;
}
}
extern "C" __declspec(dllexport) int GetFileCVPlayerStrImage(ANSCENTER::ANSFILEPLAYER_CV** Handle, int& width, int& height, int64_t& timeStamp, std::string& jpegImage) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
jpegImage = (*Handle)->GetJpegStringImage(width, height, timeStamp);
if (!jpegImage.empty()) return 1;
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error getting image data from FilePlayer client: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int GetFileCVPlayerImage(ANSCENTER::ANSFILEPLAYER_CV** Handle, int& width, int& height, int64_t& timeStamp, LStrHandle jpegImage) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
std::string jpegString = (*Handle)->GetJpegStringImage(width, height, timeStamp);
int size = jpegString.length();
if (size > 0) {
MgErr error;
// Resize the jpegImage handle to hold the image data
error = DSSetHandleSize(jpegImage, sizeof(int32) + size * sizeof(uChar));
// Check if resizing the handle was successful
if (error == noErr) {
// Set the size of the image in the handle
(*jpegImage)->cnt = size;
// Use memcpy to copy the data from the std::string to the LStrHandle's str buffer
memcpy((*jpegImage)->str, jpegString.c_str(), size);
// Return success
return 1;
}
else {
// Return failure if there was an error in resizing the handle
std::cerr << "Error resizing jpegImage handle: " << error << std::endl;
return 0;
}
}
else {
// If the JPEG image string is empty, return failure
std::cerr << "No image data retrieved from FilePlayer client." << std::endl;
return 0;
}
}
catch (const std::exception& e) {
std::cerr << "Error getting image data from FilePlayer client: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int GetFileCVPlayerCVImage(ANSCENTER::ANSFILEPLAYER_CV** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image) {
if (!Handle || !(*Handle) || !image) {
std::cerr << "Error: Invalid input parameters in GetFilePlayerCVImage." << std::endl;
return -1;
}
try {
cv::Mat img = (*Handle)->GetImage(width, height, timeStamp);
if (img.empty()) {
return 0; // No valid image retrieved
}
// anscv_mat_replace has its own internal registry_mutex � no global mutex needed
anscv_mat_replace(image, std::move(img));
return 1; // Success
}
catch (const cv::Exception& e) {
std::cerr << "OpenCV exception in GetFilePlayerCVImage: " << e.what() << std::endl;
return -2;
}
catch (const std::exception& e) {
std::cerr << "Exception in GetFilePlayerCVImage: " << e.what() << std::endl;
return -2;
}
catch (...) {
std::cerr << "Unknown exception in GetFilePlayerCVImage" << std::endl;
return -2;
}
}
extern "C" __declspec(dllexport) int StartFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
bool result = (*Handle)->Start();
if (result) return 1;
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error starting file player: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int ReconnectFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
bool result = (*Handle)->Reconnect();
if (result) return 1;
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error reconnecting file player: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int StopFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
bool result = (*Handle)->Stop();
if (result) return 1;
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error stopping file player: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int IsFileCVPlayerPaused(ANSCENTER::ANSFILEPLAYER_CV** Handle) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
bool result = (*Handle)->IsPaused();
if (result) return 1;
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error checking if file player is paused: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int IsFileCVPlayerRunning(ANSCENTER::ANSFILEPLAYER_CV** Handle) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
bool result = (*Handle)->IsPlaying();
if (result) return 1;
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error checking if file player is running: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int IsFileCVPlayerRecording(ANSCENTER::ANSFILEPLAYER_CV** Handle) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
bool result = (*Handle)->IsRecording();
if (result) return 1;
else return 0;
}
catch (const std::exception& e) {
std::cerr << "Error checking if file player is recording: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) void SetFileCVPlayerAudioVolume(ANSCENTER::ANSFILEPLAYER_CV** Handle, int volume)
{
if (Handle == nullptr || *Handle == nullptr) return;
try {
(*Handle)->SetAudioVolume(volume);
}
catch (...) {}
}
extern "C" __declspec(dllexport) void EnableFileCVPlayerAudioVolume(ANSCENTER::ANSFILEPLAYER_CV** Handle, int status)
{
if (Handle == nullptr || *Handle == nullptr) return;
try {
bool audioStatus = false;
if (status == 1)audioStatus = true;
(*Handle)->EnableAudio(audioStatus);
}
catch (const std::exception& e) {
std::cerr << "Error enabling audio for file player: " << e.what() << std::endl;
}
catch (...) {}
}
extern "C" __declspec(dllexport) void SetFileCVPlayerImageRotation(ANSCENTER::ANSFILEPLAYER_CV** Handle, double rotationAngle) {
if (Handle == nullptr || *Handle == nullptr) return;
try {
(*Handle)->SetImageRotate(rotationAngle);
}
catch (...) {}
}
extern "C" __declspec(dllexport) int SetBBoxFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle, int x, int y, int width, int height) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
cv::Rect bbox(x, y, width, height);
(*Handle)->SetBBox(bbox);
return 1;
}
catch (const std::exception& e) {
std::cerr << "Error setting bounding box for file player: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
extern "C" __declspec(dllexport) int SetCropFlagFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle, int cropFlag) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
bool crop = false;
if (cropFlag == 1)crop = true;
(*Handle)->SetCrop(crop);
return 1;
}
catch (const std::exception& e) {
std::cerr << "Error setting crop flag for file player: " << e.what() << std::endl;
return -1;
}
catch (...) {
return -1;
}
}
+115
View File
@@ -0,0 +1,115 @@
#ifndef ANSFILEPLAYER_H
#define ANSFILEPLAYER_H
#define ANSFILEPLAYER_API __declspec(dllexport)
#include <iostream>
#include <cstdint>
#include "ANSLicense.h"
#include "LabVIEWHeader/extcode.h"
#include <vector>
#include "sys_inc.h"
#include "rtsp_cln.h"
#include "hqueue.h"
#include "http.h"
#include "http_parse.h"
#include "video_decoder.h"
#include "audio_decoder.h"
#include "file_player.h"
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <thread>
#include <chrono>
#include <atomic>
#include <turbojpeg.h>
namespace ANSCENTER
{
class ANSFILEPLAYER_API ANSFILEPLAYER_CV
{
protected:
cv::VideoCapture cap;
int _resWidth;
int _resHeight;
int64_t _totalFrames;
int64_t _currentFrame;
int64_t _previousPTS;
double _fps;
bool _isPlaying;
std::string _lastJpegImage;
//int64_t _previousPTS;
cv::Mat _previousImage;
bool m_bPaused;
int _imageRotateDeg = 0;
std::string _url;
cv::Rect _bbox;
bool _crop;
std::recursive_mutex _mutex;
std::shared_ptr<std::atomic<bool>> _stopThread;
std::shared_ptr<std::thread> _timerThread;
std::mutex _cvMutex;
std::condition_variable _cv;
//EngineType engineType;
//// Initialize nvJPEG structures
//nvjpegHandle_t nv_handle;
//nvjpegEncoderState_t nv_enc_state;
//nvjpegEncoderParams_t nv_enc_params;
//cudaStream_t stream;
tjhandle _jpegCompressor;
public:
ANSFILEPLAYER_CV();
~ANSFILEPLAYER_CV() noexcept;
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
[[nodiscard]] bool Setup();
[[nodiscard]] bool Reconnect();
[[nodiscard]] bool Start();
[[nodiscard]] bool IsPaused();
[[nodiscard]] bool IsPlaying();
[[nodiscard]] bool IsRecording();
void SetBBox(cv::Rect bbox);
void SetCrop(bool crop);
[[nodiscard]] bool Stop();
void Destroy();
void EnableAudio(bool status);
void SetAudioVolume(int volume);
void SetImageRotate(int mode) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_imageRotateDeg = mode;
if (mode > 360) _imageRotateDeg = 360;
};
[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
[[nodiscard]] std::string GetJpegStringImage(int& width, int& height, int64_t& pts);
public:
void CheckLicense();
SPDLogger& _logger = SPDLogger::GetInstance("ANSFilePlayer_CV", false);
bool _licenseValid{ false };
//std::once_flag licenseOnceFlag;
std::string _licenseKey;
private:
std::string encodeJpegString(const cv::Mat& img, int quality = 80);
//std::string encodeMatToJpegWithNvJPEG(const cv::Mat& inputMat, int quality = 80);
//void uploadPlanarBGRToGPU(const cv::Mat& inputMat, unsigned char** data);
void StartTimer(std::shared_ptr<std::atomic<bool>> stopFlag, int interval_ms);
void StopTimerThread();
};
}
extern "C" __declspec(dllexport) int CreateANSFileCVPlayerHandle(ANSCENTER::ANSFILEPLAYER_CV** Handle, const char* licenseKey, const char* url);
extern "C" __declspec(dllexport) int ReleaseANSFileCVPlayerHandle(ANSCENTER::ANSFILEPLAYER_CV** Handle);
extern "C" __declspec(dllexport) int GetFileCVPlayerImage(ANSCENTER::ANSFILEPLAYER_CV** Handle, int& width, int& height, int64_t& timeStamp, LStrHandle jpegImage);
extern "C" __declspec(dllexport) int GetFileCVPlayerStrImage(ANSCENTER::ANSFILEPLAYER_CV** Handle, int& width, int& height, int64_t& timeStamp, std::string& jpegImage);
extern "C" __declspec(dllexport) int GetFileCVPlayerCVImage(ANSCENTER::ANSFILEPLAYER_CV** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
extern "C" __declspec(dllexport) int StartFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle);
extern "C" __declspec(dllexport) int ReconnectFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle);
extern "C" __declspec(dllexport) int StopFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle);
extern "C" __declspec(dllexport) int IsFileCVPlayerPaused(ANSCENTER::ANSFILEPLAYER_CV** Handle);
extern "C" __declspec(dllexport) int IsFileCVPlayerRunning(ANSCENTER::ANSFILEPLAYER_CV** Handle);
extern "C" __declspec(dllexport) int IsFileCVPlayerRecording(ANSCENTER::ANSFILEPLAYER_CV** Handle);
extern "C" __declspec(dllexport) void SetFileCVPlayerAudioVolume(ANSCENTER::ANSFILEPLAYER_CV** Handle, int volume);
extern "C" __declspec(dllexport) void EnableFileCVPlayerAudioVolume(ANSCENTER::ANSFILEPLAYER_CV** Handle, int status);
extern "C" __declspec(dllexport) void SetFileCVPlayerImageRotation(ANSCENTER::ANSFILEPLAYER_CV** Handle, double rotationAngle);
extern "C" __declspec(dllexport) int SetBBoxFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle, int x, int y, int width, int height);
extern "C" __declspec(dllexport) int SetCropFlagFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle, int cropFlag);
#endif
+212
View File
@@ -0,0 +1,212 @@
#pragma once
// ANSGpuFrameOps.h — FFmpeg-aware convenience functions for ANSGpuFrameRegistry.
//
// This header requires FFmpeg headers (libavutil/frame.h) and provides
// typed attach/invalidate/remove operations that handle av_frame_clone/free.
//
// NEW DESIGN: Instead of storing AVFrame* references (which lock NVDEC surfaces),
// we snapshot the CPU NV12 planes into malloc'd buffers and release the AVFrames
// immediately. This prevents decoder surface pool exhaustion when many clones
// hold references to the same frame.
//
// Include this in ANSCV/ANSRTSP (which link FFmpeg). For projects without
// FFmpeg (ANSODEngine), include ANSGpuFrameRegistry.h directly and use
// gpu_frame_lookup() + the GpuFrameData plane pointers.
#include "ANSGpuFrameRegistry.h"
extern "C" {
#include "libavutil/frame.h"
}
#include <cstring>
#include <cstdlib>
namespace anscv_gpu_ops {
namespace detail {
// Snapshot NV12 Y and UV planes from an AVFrame into malloc'd buffers.
// Returns true on success. Caller owns the output buffers.
inline bool snapshotNV12Planes(const AVFrame* nv12,
uint8_t*& outY, int& outYLinesize,
uint8_t*& outUV, int& outUVLinesize,
int& outWidth, int& outHeight) {
if (!nv12 || !nv12->data[0] || !nv12->data[1])
return false;
outWidth = nv12->width;
outHeight = nv12->height;
outYLinesize = nv12->width; // Packed (no alignment padding)
outUVLinesize = nv12->width; // UV interleaved: width bytes per row
size_t yBytes = static_cast<size_t>(outYLinesize) * outHeight;
size_t uvBytes = static_cast<size_t>(outUVLinesize) * (outHeight / 2);
outY = static_cast<uint8_t*>(std::malloc(yBytes));
outUV = static_cast<uint8_t*>(std::malloc(uvBytes));
if (!outY || !outUV) {
std::free(outY);
std::free(outUV);
outY = nullptr;
outUV = nullptr;
return false;
}
// Copy line-by-line (source may have padding via linesize > width)
const int srcYLinesize = nv12->linesize[0];
const int srcUVLinesize = nv12->linesize[1];
for (int row = 0; row < outHeight; ++row) {
std::memcpy(outY + row * outYLinesize,
nv12->data[0] + row * srcYLinesize,
outWidth);
}
for (int row = 0; row < outHeight / 2; ++row) {
std::memcpy(outUV + row * outUVLinesize,
nv12->data[1] + row * srcUVLinesize,
outWidth);
}
return true;
}
} // namespace detail
} // namespace anscv_gpu_ops
// Attach NV12/YUV frame keyed by cv::Mat* pointer.
// Snapshots CPU NV12 planes into owned malloc'd buffers, then releases the AVFrame.
// TAKES OWNERSHIP of nv12 — caller must NOT av_frame_free after this call.
inline void gpu_frame_attach(cv::Mat* mat, AVFrame* nv12, int gpuIdx, int64_t pts) {
if (!mat || !nv12) return;
GpuFrameData data{};
data.gpuIndex = gpuIdx;
data.pts = pts;
data.pixelFormat = nv12->format;
data.width = nv12->width;
data.height = nv12->height;
// Snapshot NV12 planes to owned buffers
bool ok = anscv_gpu_ops::detail::snapshotNV12Planes(
nv12,
data.cpuYPlane, data.cpuYLinesize,
data.cpuUvPlane, data.cpuUvLinesize,
data.width, data.height);
// Keep legacy pointers for backward compat during transition
data.yPlane = data.cpuYPlane;
data.uvPlane = data.cpuUvPlane;
data.yLinesize = data.cpuYLinesize;
data.uvLinesize = data.cpuUvLinesize;
// Store AVFrame for legacy cleanup (will be freed by drain_pending)
data.avframe = nv12;
void* old = ANSGpuFrameRegistry::instance().attach(mat, std::move(data));
if (old) {
AVFrame* oldFrame = static_cast<AVFrame*>(old);
av_frame_free(&oldFrame);
}
// Free stale entries evicted by TTL or previous attach
auto pending = ANSGpuFrameRegistry::instance().drain_pending();
for (void* p : pending) {
AVFrame* stale = static_cast<AVFrame*>(p);
av_frame_free(&stale);
}
}
// Attach CUDA HW frame — keeps CUDA device pointers for zero-copy inference.
// TAKES OWNERSHIP of cudaFrame AND cpuNV12 — caller must NOT av_frame_free after.
//
// Primary path: yPlane/uvPlane point to CUDA device pointers from the cloned
// AVFrame (data[0]/data[1]). The cloned AVFrame keeps the NVDEC surface alive
// until gpu_frame_remove() is called after inference. With 4 cameras each
// holding ~1 surface, this uses 4 of NVDEC's 25-32 surface pool — safe.
//
// Fallback: cpuYPlane/cpuUvPlane hold CPU-side NV12 snapshot for cross-GPU
// inference (when decode GPU != inference GPU, CUDA device ptrs aren't
// accessible from another GPU context).
inline void gpu_frame_attach_cuda(cv::Mat* mat, AVFrame* cudaFrame, int gpuIdx, int64_t pts,
AVFrame* cpuNV12 = nullptr) {
if (!mat || !cudaFrame) return;
GpuFrameData data{};
data.gpuIndex = gpuIdx;
data.pts = pts;
data.width = cudaFrame->width;
data.height = cudaFrame->height;
data.pixelFormat = 23; // AV_PIX_FMT_NV12 — the underlying sw_format
// Primary: CUDA device pointers from NVDEC (zero-copy on same GPU)
data.isCudaDevicePtr = true;
data.yPlane = cudaFrame->data[0]; // CUDA device ptr: Y plane
data.uvPlane = cudaFrame->data[1]; // CUDA device ptr: UV plane
data.yLinesize = cudaFrame->linesize[0];
data.uvLinesize = cudaFrame->linesize[1];
// Fallback: snapshot CPU NV12 for cross-GPU inference
if (cpuNV12) {
anscv_gpu_ops::detail::snapshotNV12Planes(
cpuNV12,
data.cpuYPlane, data.cpuYLinesize,
data.cpuUvPlane, data.cpuUvLinesize,
data.width, data.height);
}
// Store AVFrames for cleanup (cudaFrame keeps NVDEC surface alive)
data.avframe = cudaFrame;
data.cpuAvframe = cpuNV12;
void* old = ANSGpuFrameRegistry::instance().attach(mat, std::move(data));
if (old) {
AVFrame* oldFrame = static_cast<AVFrame*>(old);
av_frame_free(&oldFrame);
}
auto pending = ANSGpuFrameRegistry::instance().drain_pending();
for (void* p : pending) {
AVFrame* stale = static_cast<AVFrame*>(p);
av_frame_free(&stale);
}
}
// Release entry by cv::Mat* and free any returned AVFrames. Safe if not in map (no-op).
inline void gpu_frame_remove(cv::Mat* mat) {
if (!mat) return;
ANSGpuFrameRegistry::instance().release(mat);
// Free any AVFrames that became pending from this release or prior eviction
auto pending = ANSGpuFrameRegistry::instance().drain_pending();
for (void* p : pending) {
AVFrame* stale = static_cast<AVFrame*>(p);
av_frame_free(&stale);
}
// Free any GPU device pointers that became pending
auto gpuPending = gpu_frame_drain_gpu_pending();
// NOTE: cudaFree requires CUDA context — caller must be on a CUDA-capable thread.
// If not, these will leak. In practice, gpu_frame_remove is called from ANSCV
// camera threads which do have CUDA context.
// For safety, we skip cudaFree here and let NV12PreprocessHelper handle it.
// The GPU pointers are tracked in the budget and will be accounted for.
(void)gpuPending;
}
// Alias for remove — used in ANSCV mutating functions to drop stale GPU data.
inline void gpu_frame_invalidate(cv::Mat* mat) {
gpu_frame_remove(mat);
}
// Run TTL eviction + drain pending. Call periodically from camera threads.
inline void gpu_frame_evict_stale() {
ANSGpuFrameRegistry::instance().evictStaleFrames();
auto pending = ANSGpuFrameRegistry::instance().drain_pending();
for (void* p : pending) {
AVFrame* stale = static_cast<AVFrame*>(p);
av_frame_free(&stale);
}
}
+25
View File
@@ -0,0 +1,25 @@
// ANSGpuFrameRegistry.cpp — Process-wide singleton, compiled into ANSCV.dll.
//
// On Windows, header-only singletons (static local in inline function) create
// separate instances in each DLL. Since gpu_frame_attach() runs in ANSCV.dll
// and gpu_frame_lookup() runs in ANSODEngine.dll, we need a single shared
// instance. This file:
// 1. Defines resolveProcessWide() which owns the canonical singleton.
// 2. Exports a C function so other DLLs can find it via GetProcAddress.
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include "ANSGpuFrameRegistry.h"
// ANSCV.dll owns the process-wide singleton.
ANSGpuFrameRegistry* ANSGpuFrameRegistry::resolveProcessWide() {
static ANSGpuFrameRegistry reg;
return &reg;
}
// Exported so other DLLs (ANSODEngine, etc.) can find this instance at runtime.
extern "C" __declspec(dllexport)
ANSGpuFrameRegistry* ANSGpuFrameRegistry_GetInstance() {
return &ANSGpuFrameRegistry::instance();
}
+1175
View File
File diff suppressed because it is too large Load Diff
+111
View File
@@ -0,0 +1,111 @@
#ifndef ANSMJPEG_H
#define ANSMJPEG_H
#define ANSMJPEG_API __declspec(dllexport)
#include <iostream>
#include "ANSLicense.h"
#include "LabVIEWHeader/extcode.h"
#include <vector>
#include "sys_inc.h"
#include "http_mjpeg_cln.h"
#include "hqueue.h"
#include "http.h"
#include "http_parse.h"
#include "video_decoder.h"
#include "audio_decoder.h"
#include "http_mjpeg_player.h"
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
namespace ANSCENTER
{
class ANSMJPEG_API ANSMJPEGClient
{
protected:
std::unique_ptr<CHttpMjpegPlayer> _playerClient = std::make_unique<CHttpMjpegPlayer>();
std::string _username;
std::string _password;
std::string _url;
bool _useFullURL;
int _imageRotateDeg = 0;
int _displayWidth = 0; // 0 = no resize (return original resolution)
int _displayHeight = 0;
cv::Mat _pLastFrame;
std::string _lastJpegImage;
int _imageWidth, _imageHeight;
int64_t _pts;
bool _isPlaying;
std::recursive_mutex _mutex;
public:
ANSMJPEGClient();
~ANSMJPEGClient() noexcept;
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
[[nodiscard]] bool Init(std::string licenseKey, std::string username, std::string password, std::string url);
[[nodiscard]] bool Setup();
[[nodiscard]] bool Reconnect();
[[nodiscard]] bool Start();
[[nodiscard]] bool Pause();
[[nodiscard]] bool IsPaused();
[[nodiscard]] bool IsPlaying();
[[nodiscard]] bool IsRecording();
[[nodiscard]] bool Stop();
void Destroy();
void EnableAudio(bool status);
void SetAudioVolume(int volume);
bool areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2);
[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
[[nodiscard]] std::string GetJpegImage(int& width, int& height, int64_t& pts);
void SetImageRotate(int mode) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_imageRotateDeg = mode;
if (mode > 360) _imageRotateDeg = 360;
};
void SetBBox(cv::Rect bbox);
void SetCrop(bool crop);
static void SetMaxHWDecoders(int maxDecoders);
static int AutoConfigureHWDecoders(int maxPerGpuOverride = 0);
void SetHWDecoding(int hwMode, int preferredGpu = -1);
bool IsHWDecodingActive();
int GetHWDecodingGpuIndex();
void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize)
void SetImageQuality(int mode); // 0=fast (AI), 1=quality (display)
AVFrame* GetNV12Frame(); // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free)
AVFrame* GetCudaHWFrame(); // Returns CUDA HW frame (device ptrs) for zero-copy inference
bool IsCudaHWAccel(); // true when decoder uses CUDA (NV12 stays in GPU VRAM)
public:
void CheckLicense();
SPDLogger& _logger = SPDLogger::GetInstance("ANSMJPEG", false);
bool _licenseValid{ false };
std::string _licenseKey;
//std::once_flag licenseOnceFlag;
};
}
extern "C" __declspec(dllexport) int CreateANSMJPEGHandle(ANSCENTER::ANSMJPEGClient * *Handle, const char* licenseKey, const char* username, const char* password, const char* url);
extern "C" __declspec(dllexport) int ReleaseANSMJPEGHandle(ANSCENTER::ANSMJPEGClient * *Handle);
extern "C" __declspec(dllexport) int GetMJPEGImage(ANSCENTER::ANSMJPEGClient * *Handle, int& width, int& height, int64_t & timeStamp, LStrHandle jpegImage);
extern "C" __declspec(dllexport) int GetMJPEGStrImage(ANSCENTER::ANSMJPEGClient * *Handle, int& width, int& height, int64_t & timeStamp, std::string & jpegImage);
extern "C" __declspec(dllexport) int GetMJPEGCVImage(ANSCENTER::ANSMJPEGClient** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
extern "C" __declspec(dllexport) int StartMJPEG(ANSCENTER::ANSMJPEGClient * *Handle);
extern "C" __declspec(dllexport) int ReconnectMJPEG(ANSCENTER::ANSMJPEGClient * *Handle);
extern "C" __declspec(dllexport) int StopMJPEG(ANSCENTER::ANSMJPEGClient * *Handle);
extern "C" __declspec(dllexport) int PauseMJPEG(ANSCENTER::ANSMJPEGClient** Handle);
extern "C" __declspec(dllexport) int IsMJPEGPaused(ANSCENTER::ANSMJPEGClient * *Handle);
extern "C" __declspec(dllexport) int IsMJPEGRunning(ANSCENTER::ANSMJPEGClient * *Handle);
extern "C" __declspec(dllexport) int IsMJPEGRecording(ANSCENTER::ANSMJPEGClient * *Handle);
extern "C" __declspec(dllexport) void SetMJPEGAudioVolume(ANSCENTER::ANSMJPEGClient * *Handle, int volume);
extern "C" __declspec(dllexport) void EnableMJPEGAudioVolume(ANSCENTER::ANSMJPEGClient * *Handle, int status);
extern "C" __declspec(dllexport) void SetMJPEGImageRotation(ANSCENTER::ANSMJPEGClient * *Handle, double rotationAngle);
extern "C" __declspec(dllexport) int SetBBoxMJPEG(ANSCENTER::ANSMJPEGClient** Handle, int x, int y, int width, int height);
extern "C" __declspec(dllexport) int SetCropFlagMJPEG(ANSCENTER::ANSMJPEGClient** Handle, int cropFlag);
extern "C" __declspec(dllexport) void SetMJPEGMaxHWDecoders(int maxDecoders);
extern "C" __declspec(dllexport) int AutoConfigureMJPEGHWDecoders(int maxPerGpuOverride);
extern "C" __declspec(dllexport) void SetMJPEGHWDecoding(ANSCENTER::ANSMJPEGClient** Handle, int hwMode, int preferredGpu = -1);
extern "C" __declspec(dllexport) int IsMJPEGHWDecodingActive(ANSCENTER::ANSMJPEGClient** Handle);
extern "C" __declspec(dllexport) int GetMJPEGHWDecodingGpuIndex(ANSCENTER::ANSMJPEGClient** Handle);
extern "C" __declspec(dllexport) void SetMJPEGImageQuality(ANSCENTER::ANSMJPEGClient** Handle, int mode);
extern "C" __declspec(dllexport) void SetMJPEGDisplayResolution(ANSCENTER::ANSMJPEGClient** Handle, int width, int height);
#endif
+116
View File
@@ -0,0 +1,116 @@
#pragma once
// ANSMatRegistry.h — Thread-safe cv::Mat* pointer registry.
// Prevents double-free crashes when LabVIEW calls ReleaseImage on a pointer
// that a stream source (RTSP, VideoPlayer, etc.) has already freed and replaced.
//
// Every cv::Mat* allocated through these functions is tracked in a global set.
// Deletion only proceeds if the pointer is still in the set, guaranteeing each
// pointer is freed exactly once regardless of which thread calls delete first.
//
// Usage:
// Allocate: cv::Mat* p = anscv_mat_new(std::move(img));
// Free: anscv_mat_delete(&p); // safe, no-op if already freed
// Swap: anscv_mat_replace(&p, std::move(newImg)); // atomic old-delete + new-alloc
#include <opencv2/core/mat.hpp>
#include <mutex>
#include <unordered_set>
#include "ANSGpuFrameOps.h"
namespace anscv {
namespace detail {
inline std::mutex& registry_mutex() {
static std::mutex m;
return m;
}
inline std::unordered_set<cv::Mat*>& registry() {
static std::unordered_set<cv::Mat*> s;
return s;
}
} // namespace detail
} // namespace anscv
// Allocate a new cv::Mat on the heap and register it.
inline cv::Mat* anscv_mat_new(cv::Mat&& src) {
cv::Mat* p = new cv::Mat(std::move(src));
{
std::lock_guard<std::mutex> lk(anscv::detail::registry_mutex());
anscv::detail::registry().insert(p);
}
return p;
}
inline cv::Mat* anscv_mat_new(const cv::Mat& src) {
cv::Mat* p = new cv::Mat(src);
{
std::lock_guard<std::mutex> lk(anscv::detail::registry_mutex());
anscv::detail::registry().insert(p);
}
return p;
}
// Delete a cv::Mat* only if it is still in the registry.
// Returns true if deleted, false if pointer was null or already freed.
// Thread-safe: the pointer is removed from the registry AND nulled AND deleted
// all inside the lock, so no other thread can race on the same pointer.
inline bool anscv_mat_delete(cv::Mat** pp) {
if (!pp || !(*pp))
return false;
cv::Mat* toDelete = nullptr;
{
std::lock_guard<std::mutex> lk(anscv::detail::registry_mutex());
// Re-check under lock — another thread may have nulled *pp
if (!*pp)
return false;
auto& reg = anscv::detail::registry();
auto it = reg.find(*pp);
if (it == reg.end()) {
// Not in registry — already freed by another thread.
*pp = nullptr;
return false;
}
toDelete = *pp;
reg.erase(it);
*pp = nullptr; // Null the caller's pointer while still under lock
}
// Release GPU frame data (refcount--, frees NV12+GPU cache when refcount=0)
gpu_frame_remove(toDelete);
// Safe to delete outside lock: pointer is removed from registry and
// *pp is nullptr, so no other thread can reach toDelete.
delete toDelete;
return true;
}
// Atomic replace: delete old image (if still registered), allocate new, assign to *pp.
// The old pointer is only deleted if it is in the registry (prevents double-free
// when LabVIEW already freed it via anscv_mat_delete).
inline void anscv_mat_replace(cv::Mat** pp, cv::Mat&& newImg) {
cv::Mat* newPtr = new cv::Mat(std::move(newImg));
cv::Mat* toDelete = nullptr;
{
std::lock_guard<std::mutex> lk(anscv::detail::registry_mutex());
auto& reg = anscv::detail::registry();
if (pp && *pp) {
auto it = reg.find(*pp);
if (it != reg.end()) {
toDelete = *pp;
reg.erase(it);
}
// If not in registry, another thread already freed it — skip delete
}
*pp = newPtr;
reg.insert(newPtr);
}
if (toDelete) {
// Release GPU frame ref for old Mat (safe with refcounting —
// only frees NV12/GPU cache when refcount reaches 0)
gpu_frame_remove(toDelete);
delete toDelete;
}
// Periodically run TTL eviction (piggybacked on camera thread activity)
gpu_frame_evict_stale();
}
+5106
View File
File diff suppressed because it is too large Load Diff
+164
View File
@@ -0,0 +1,164 @@
#ifndef ANSOPENCV_H
#define ANSOPENCV_H
#define ANSOPENCV_API __declspec(dllexport)
#include <iostream>
#include "ANSLicense.h"
#include "LabVIEWHeader/extcode.h"
#include <vector>
#include <opencv2/opencv.hpp>
#define MUTEX_TIMEOUT_MS 45000
namespace fs = std::filesystem;
namespace ANSCENTER
{
std::string CompressJpegToString(const cv::Mat& image, int quality);
struct DetectionObject
{
int classId{ 0 };
int trackId{ 0 };
std::string className{};
float confidence{ 0.0 };
cv::Rect box{};
cv::Mat mask{}; //Json string mask ="point1.x,point1.y,...."
std::vector<float> kps{}; // Pose exsimate keypoint
std::string extraInfo; // More information such as facial recognition
};
class TurboJpegCompressor {
public:
TurboJpegCompressor();
~TurboJpegCompressor() noexcept;
// Delete copy constructor and assignment operator
TurboJpegCompressor(const TurboJpegCompressor&) = delete;
TurboJpegCompressor& operator=(const TurboJpegCompressor&) = delete;
// Your original logic with minimal optimizations
[[nodiscard]] std::string compress(const cv::Mat& image, int quality);
private:
void* _handle = nullptr;
unsigned char* _buffer = nullptr;
unsigned long _bufferSize = 0;
};
/// <summary>
/// // ANSOPENCV class provides various image processing functionalities using OpenCV and ANS Center SDK.
/// </summary>
class ANSOPENCV_API ANSOPENCV
{
public:
[[nodiscard]] bool Init(std::string licenseKey);
void ImageResize(const cv::Mat& inputFrame, int width, int height, cv::Mat& outputFrame);
void ImageResizeWithRatio(const cv::Mat& inputFrame, int width, cv::Mat& outputFrame);
[[nodiscard]] cv::Mat BlurObjects(const cv::Mat& image, const std::vector<cv::Rect>& objects);
[[nodiscard]] cv::Mat BlurBackground(const cv::Mat& image, const std::vector<cv::Rect>& objects);
[[nodiscard]] cv::Mat ToGray(const cv::Mat& image);
[[nodiscard]] cv::Mat ImageDenoise(const cv::Mat& image);
[[nodiscard]] cv::Mat ImageRepair(const cv::Mat& image);
[[nodiscard]] cv::Mat ImageCrop(const cv::Mat& image,const cv::Rect& ROI, int originalImageSize = 0);
[[nodiscard]] cv::Mat ImageResizeV2(const cv::Mat& image, int resizeWidth, int orginalImageSize = 0);
[[nodiscard]] std::string QRDecoder(const cv::Mat& image);
[[nodiscard]] std::string QRDecoderWithBBox(const cv::Mat& image, int maxImageSize, const std::vector<cv::Rect>& bBox);
[[nodiscard]] std::string MatToBase64(const cv::Mat& image);
[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
[[nodiscard]] std::vector<cv::Rect> GetBoundingBoxes(std::string strBBoxes);
[[nodiscard]] std::string VectorDetectionToJsonString(const std::vector<DetectionObject>& dets);
[[nodiscard]] std::string PatternMatches(cv::Mat& image, cv::Mat& templateImage, double threshold);
[[nodiscard]] cv::Mat ImageDarkEnhancement(const cv::Mat& img, double brightnessScaleFactor=1.5);
[[nodiscard]] cv::Mat ImageContrastEnhancement(const cv::Mat& img);
[[nodiscard]] cv::Mat ImageWhiteBalance(const cv::Mat& img);
[[nodiscard]] cv::Mat RotateImage(const cv::Mat& image, double angle);
[[nodiscard]] cv::Mat FlipImage(const cv::Mat& image, int flipCode);//flipCode = 0: Vertical flip (around the x-axis). flipCode = 1: Horizontal flip (around the y-axis). flipCode = -1: Both axes, flipping the image horizontally and vertically.
[[nodiscard]] cv::Mat ShiftImage(const cv::Mat& image, int shiftX, int shiftY);
[[nodiscard]] cv::Mat AddGaussianNoise(const cv::Mat& image, double mean, double stddev);
[[nodiscard]] cv::Mat AddSaltAndPepperNoise(const cv::Mat& image, double amount);
[[nodiscard]] cv::Mat AddSpeckleNoise(const cv::Mat& image, double stddev);
static void InitCameraNetwork();
static void DeinitCameraNetwork();
static cv::Mat resizeImageToFit(const cv::Mat& inputImage, int maxWidth, int maxHeight, int& newWidth, int& newHeight);
static bool resizeImage(cv::Mat& inputImage, int resizeWidth, int orginalImageSize=0);
static bool cropImage(cv::Mat& inputImage, const cv::Rect& resizeROI, int originalImageSize=0);
static bool ImagesToMP4(const std::string& imageFolder, const std::string& outputVideoPath, int targetDurationSec);
private:
void CheckLicense();
double CalculateIoU(const cv::Rect& box1, const cv::Rect& box2);
void NonMaximumSuppression(std::vector<DetectionObject>& detectedObjects, double iouThreshold);
std::string EncodeJpegString(const cv::Mat& img, int quality);
SPDLogger& _logger = SPDLogger::GetInstance("ANSCV", false);
std::string _licenseKey;
std::recursive_mutex _mutex;
//std::once_flag licenseOnceFlag; // For one-time license check
bool _licenseValid = false;
public:
};
}
extern "C" __declspec(dllexport) int CreateANSCVHandle(ANSCENTER::ANSOPENCV **Handle, const char* licenseKey);
extern "C" __declspec(dllexport) int ReleaseANSCVHandle(ANSCENTER::ANSOPENCV **Handle);
extern "C" __declspec(dllexport) int ANSCV_ImageResize(ANSCENTER::ANSOPENCV * *Handle,unsigned char* inputImage, unsigned int bufferLength, int width, int height, LStrHandle outputImage);
extern "C" __declspec(dllexport) int ANSCV_ImageResizeWithRatio(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, int width,LStrHandle outputImage);
extern "C" __declspec(dllexport) int ANSCV_ImageToBase64(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, LStrHandle outputImage);
extern "C" __declspec(dllexport) int ANSCV_ImageToGray(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, LStrHandle outputImage);
extern "C" __declspec(dllexport) int ANSCV_ImageDenoise(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, LStrHandle outputImage);
extern "C" __declspec(dllexport) int ANSCV_ImageRepair(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, LStrHandle outputImage);
extern "C" __declspec(dllexport) int ANSCV_ImageAutoWhiteBalance(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, LStrHandle outputImage);
extern "C" __declspec(dllexport) int ANSCV_ImageBrightEnhance(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, double brightnessScaleFactor, LStrHandle outputImage);
extern "C" __declspec(dllexport) int ANSCV_ImageContrastEnhance(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, LStrHandle outputImage);
extern "C" __declspec(dllexport) int ANSCV_ImageCrop(ANSCENTER::ANSOPENCV** Handle, unsigned char* inputImage, unsigned int bufferLength, int x, int y, int width, int height, LStrHandle outputImage);
extern "C" __declspec(dllexport) int ANSCV_GetImageSize(ANSCENTER::ANSOPENCV** Handle, unsigned char* inputImage, unsigned int bufferLength, LStrHandle imageSize);
extern "C" __declspec(dllexport) int ANSCV_GetImageSizeFromImageFile(ANSCENTER::ANSOPENCV** Handle, const char* imageFilePath, LStrHandle imageSize);
extern "C" __declspec(dllexport) int ANSCV_BlurObjects(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, const char* strBboxes, LStrHandle outputImage);
extern "C" __declspec(dllexport) int ANSCV_BlurBackground(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, const char* strBboxes, LStrHandle outputImage);
extern "C" __declspec(dllexport) int ANSCV_QRDecoder(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, LStrHandle detectedQRText);
extern "C" __declspec(dllexport) int ANSCV_QRDecoderCV(ANSCENTER::ANSOPENCV** Handle, const cv::Mat &image, std::string& detectedQRText);
extern "C" __declspec(dllexport) int ANSCV_PatternMatchs(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, const char* templateFilePath, double theshold, LStrHandle detectedMatchedLocations);
extern "C" __declspec(dllexport) int ANSCV_RotateImage(ANSCENTER::ANSOPENCV** Handle, unsigned char* inputImage, unsigned int bufferLength, double angle, LStrHandle outputImage);
extern "C" __declspec(dllexport) int ANSCV_FlipImage(ANSCENTER::ANSOPENCV** Handle, unsigned char* inputImage, unsigned int bufferLength, int flipCode, LStrHandle outputImage);
extern "C" __declspec(dllexport) int ANSCV_ShiftImage(ANSCENTER::ANSOPENCV** Handle, unsigned char* inputImage, unsigned int bufferLength, int shiftX, int shiftY, LStrHandle outputImage);
extern "C" __declspec(dllexport) int ANSCV_AddGaussianNoise(ANSCENTER::ANSOPENCV** Handle, unsigned char* inputImage, unsigned int bufferLength, double mean, double stddev, LStrHandle outputImage);
extern "C" __declspec(dllexport) int ANSCV_AddSaltAndPepperNoise(ANSCENTER::ANSOPENCV** Handle, unsigned char* inputImage, unsigned int bufferLength, double amount, LStrHandle outputImage);
extern "C" __declspec(dllexport) int ANSCV_AddSpeckleNoise(ANSCENTER::ANSOPENCV** Handle, unsigned char* inputImage, unsigned int bufferLength, double stddev, LStrHandle outputImage);
extern "C" __declspec(dllexport) void ANSCV_InitCameraResource();
extern "C" __declspec(dllexport) void ANSCV_FreeCameraResource();
extern "C" __declspec(dllexport) int ANSCV_ResizeImage_Static(unsigned char* inputImage, unsigned int bufferLength, int width,int height, int& newWidth, int&newHeight, LStrHandle outputImage);
// Image reference management functions
extern "C" __declspec(dllexport) int ANSCV_CloneImage_S(cv::Mat **imageIn, cv::Mat** imageOut);
extern "C" __declspec(dllexport) int ANSCV_ReleaseImage_S(cv::Mat** imageIn);
extern "C" __declspec(dllexport) int ANSCV_ReSizeImage_S(cv::Mat** imageIn, int width, int originalImageSize = 0);
extern "C" __declspec(dllexport) int ANSCV_CropImage_S(cv::Mat** imageIn, int x, int y, int width, int height, int originalImageSize=0);
extern "C" __declspec(dllexport) int ANSCV_GetImage_S(cv::Mat** imageIn, int width, int quality, int& newWidth, int& newHeight, LStrHandle outputImage);
extern "C" __declspec(dllexport) int ANSCV_GetImage_CPP(cv::Mat** imageIn, int width, int quality, int& newWidth, int& newHeight, std::string& outputImage);
extern "C" __declspec(dllexport) int ANSCV_GetImageAndRemoveImgRef_S(cv::Mat** imageIn, int width, int quality, int& newWidth, int& newHeight, LStrHandle outputImage);
extern "C" __declspec(dllexport) int ANSCV_GetImageInfo_S(cv::Mat** imageIn, int &width, int& height);
extern "C" __declspec(dllexport) int ANSCV_CreateImageFromJpegString_S(unsigned char* inputImage, unsigned int bufferLength,cv::Mat** image);
extern "C" __declspec(dllexport) int ANSCV_CreateImageFromFile_S(const char* imageFilePath, cv::Mat** image);
// Pre-process image functions
extern "C" __declspec(dllexport) int ANSCV_ImageAutoWhiteBalance_S(cv::Mat** imageIn);
extern "C" __declspec(dllexport) int ANSCV_ImageBrightEnhance_S(cv::Mat** imageIn, double brightnessScaleFactor);
extern "C" __declspec(dllexport) int ANSCV_ImageContrastEnhance_S(cv::Mat** imageIn);
extern "C" __declspec(dllexport) int ANSCV_ImageDenoise_S(cv::Mat** imageIn);
extern "C" __declspec(dllexport) int ANSCV_ImageRepair_S(cv::Mat** imageIn);
extern "C" __declspec(dllexport) int ANSCV_ImageToGray_S(cv::Mat** imageIn);
extern "C" __declspec(dllexport) int ANSCV_ImageRotate_S(cv::Mat** imageIn, double angle);
extern "C" __declspec(dllexport) int ANSCV_ImageFlip_S(cv::Mat** imageIn, int flipCode);
// Post-process image functions
extern "C" __declspec(dllexport) int ANSCV_ImageBlurObjects_S(cv::Mat** imageIn, const char* strBboxes);
extern "C" __declspec(dllexport) int ANSCV_ImageBlurBackground_S(cv::Mat** imageIn, const char* strBboxes);
extern "C" __declspec(dllexport) int ANSCV_ImageQRDecoder_S(cv::Mat** imageIn, int maxImageWidth, const char* strBboxes, LStrHandle detectedQRText);
extern "C" __declspec(dllexport) int ANSCV_ImagePatternMatchs_S(cv::Mat** imageIn, const char* templateFilePath, double theshold, LStrHandle detectedMatchedLocations);
extern "C" __declspec(dllexport) int ANSCV_ImagesToMP4_S(const char* imageFolder, const char* outputVideoPath, int targetDurationSec);
#endif
+1198
View File
File diff suppressed because it is too large Load Diff
+110
View File
@@ -0,0 +1,110 @@
#ifndef ANSRTMP_H
#define ANSRTMP_H
#define ANSRTMP_API __declspec(dllexport)
#include <iostream>
#include "ANSLicense.h"
#include "LabVIEWHeader/extcode.h"
#include <vector>
#include "sys_inc.h"
#include "rtmp_cln.h"
#include "hqueue.h"
#include "http.h"
#include "http_parse.h"
#include "video_decoder.h"
#include "audio_decoder.h"
#include "rtmp_player.h"
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
namespace ANSCENTER
{
class ANSRTMP_API ANSRTMPClient
{
protected:
std::unique_ptr<CRtmpPlayer> _playerClient = std::make_unique<CRtmpPlayer>();
std::string _username;
std::string _password;
std::string _url;
bool _useFullURL;
int _imageRotateDeg = 0;
int _displayWidth = 0; // 0 = no resize (return original resolution)
int _displayHeight = 0;
cv::Mat _pLastFrame;
std::string _lastJpegImage;
int _imageWidth, _imageHeight;
int64_t _pts;
bool _isPlaying;
std::recursive_mutex _mutex;
public:
ANSRTMPClient();
~ANSRTMPClient() noexcept;
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
[[nodiscard]] bool Init(std::string licenseKey, std::string username, std::string password, std::string url);
[[nodiscard]] bool Setup();
[[nodiscard]] bool Reconnect();
[[nodiscard]] bool Start();
[[nodiscard]] bool IsPaused();
[[nodiscard]] bool IsPlaying();
[[nodiscard]] bool IsRecording();
[[nodiscard]] bool Stop();
[[nodiscard]] bool Pause();
void Destroy();
void EnableAudio(bool status);
void SetAudioVolume(int volume);
bool areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2);
[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
[[nodiscard]] std::string GetJpegImage(int& width, int& height, int64_t& pts);
void SetImageRotate(int mode) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_imageRotateDeg = mode;
if (mode > 360) _imageRotateDeg = 360;
};
void SetBBox(cv::Rect bbox);
void SetCrop(bool crop);
static void SetMaxHWDecoders(int maxDecoders);
static int AutoConfigureHWDecoders(int maxPerGpuOverride = 0);
void SetHWDecoding(int hwMode, int preferredGpu = -1);
bool IsHWDecodingActive();
int GetHWDecodingGpuIndex();
void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize)
void SetImageQuality(int mode); // 0=fast (AI), 1=quality (display)
AVFrame* GetNV12Frame(); // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free)
AVFrame* GetCudaHWFrame(); // Returns CUDA HW frame (device ptrs) for zero-copy inference
bool IsCudaHWAccel(); // true when decoder uses CUDA (NV12 stays in GPU VRAM)
public:
void CheckLicense();
SPDLogger& _logger = SPDLogger::GetInstance("ANSRTMP", false);
bool _licenseValid{ false };
std::string _licenseKey;
//std::once_flag licenseOnceFlag;
};
}
extern "C" __declspec(dllexport) int CreateANSRTMPHandle(ANSCENTER::ANSRTMPClient * *Handle, const char* licenseKey, const char* username, const char* password, const char* url);
extern "C" __declspec(dllexport) int ReleaseANSRTMPHandle(ANSCENTER::ANSRTMPClient * *Handle);
extern "C" __declspec(dllexport) int GetRTMPImage(ANSCENTER::ANSRTMPClient * *Handle, int& width, int& height, int64_t & timeStamp, LStrHandle jpegImage);
extern "C" __declspec(dllexport) int GetRTMPtrImage(ANSCENTER::ANSRTMPClient * *Handle, int& width, int& height, int64_t & timeStamp, std::string & jpegImage);
extern "C" __declspec(dllexport) int GetRTMPCVImage(ANSCENTER::ANSRTMPClient** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
extern "C" __declspec(dllexport) int StartRTMP(ANSCENTER::ANSRTMPClient * *Handle);
extern "C" __declspec(dllexport) int ReconnectRTMP(ANSCENTER::ANSRTMPClient * *Handle);
extern "C" __declspec(dllexport) int StopRTMP(ANSCENTER::ANSRTMPClient * *Handle);
extern "C" __declspec(dllexport) int PauseRTMP(ANSCENTER::ANSRTMPClient** Handle);
extern "C" __declspec(dllexport) int IsRTMPPaused(ANSCENTER::ANSRTMPClient * *Handle);
extern "C" __declspec(dllexport) int IsRTMPRunning(ANSCENTER::ANSRTMPClient * *Handle);
extern "C" __declspec(dllexport) int IsRTMPRecording(ANSCENTER::ANSRTMPClient * *Handle);
extern "C" __declspec(dllexport) void SetRTMPAudioVolume(ANSCENTER::ANSRTMPClient * *Handle, int volume);
extern "C" __declspec(dllexport) void EnableRTMPAudioVolume(ANSCENTER::ANSRTMPClient * *Handle, int status);
extern "C" __declspec(dllexport) void SetRTMPImageRotation(ANSCENTER::ANSRTMPClient * *Handle, double rotationAngle);
extern "C" __declspec(dllexport) int SetBBoxRTMP(ANSCENTER::ANSRTMPClient** Handle, int x, int y, int width, int height);
extern "C" __declspec(dllexport) int SetCropFlagRTMP(ANSCENTER::ANSRTMPClient** Handle, int cropFlag);
extern "C" __declspec(dllexport) void SetRTMPMaxHWDecoders(int maxDecoders);
extern "C" __declspec(dllexport) int AutoConfigureRTMPHWDecoders(int maxPerGpuOverride);
extern "C" __declspec(dllexport) void SetRTMPHWDecoding(ANSCENTER::ANSRTMPClient** Handle, int hwMode, int preferredGpu = -1);
extern "C" __declspec(dllexport) int IsRTMPHWDecodingActive(ANSCENTER::ANSRTMPClient** Handle);
extern "C" __declspec(dllexport) int GetRTMPHWDecodingGpuIndex(ANSCENTER::ANSRTMPClient** Handle);
extern "C" __declspec(dllexport) void SetRTMPImageQuality(ANSCENTER::ANSRTMPClient** Handle, int mode);
extern "C" __declspec(dllexport) void SetRTMPDisplayResolution(ANSCENTER::ANSRTMPClient** Handle, int width, int height);
#endif
+1512
View File
File diff suppressed because it is too large Load Diff
+111
View File
@@ -0,0 +1,111 @@
#ifndef ANSRTSP_H
#define ANSRTSP_H
#define ANSRTSP_API __declspec(dllexport)
#include <iostream>
#include "ANSLicense.h"
#include "LabVIEWHeader/extcode.h"
#include <vector>
#include "sys_inc.h"
#include "rtsp_cln.h"
#include "hqueue.h"
#include "http.h"
#include "http_parse.h"
#include "video_decoder.h"
#include "audio_decoder.h"
#include "rtsp_player.h"
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
namespace ANSCENTER
{
class ANSRTSP_API ANSRTSPClient
{
protected:
std::unique_ptr<CRtspPlayer> _playerClient = std::make_unique<CRtspPlayer>();
std::string _username;
std::string _password;
std::string _url;
bool _useFullURL;
int _imageRotateDeg = 0;
int _displayWidth = 0; // 0 = no resize (return original resolution)
int _displayHeight = 0;
cv::Mat _pLastFrame;
std::string _lastJpegImage;
int _imageWidth,_imageHeight;
int64_t _pts;
bool _isPlaying;
std::recursive_mutex _mutex;
public:
ANSRTSPClient();
~ANSRTSPClient() noexcept;
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
[[nodiscard]] bool Init(std::string licenseKey, std::string username, std::string password, std::string url);
[[nodiscard]] bool Setup();
[[nodiscard]] bool Reconnect();
[[nodiscard]] bool Start();
[[nodiscard]] bool IsPaused();
[[nodiscard]] bool IsPlaying();
[[nodiscard]] bool IsRecording();
[[nodiscard]] bool Stop();
[[nodiscard]] bool Pause();
void Destroy();
void SetBBox(cv::Rect bbox);
void SetCrop(bool crop);
void EnableAudio(bool status);
void SetAudioVolume(int volume);
[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
[[nodiscard]] std::string GetJpegImage(int& width, int& height, int64_t& pts);
bool areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2);
void SetImageRotate(int mode) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_imageRotateDeg = mode;
if (mode > 360) _imageRotateDeg = 360;
};
static void SetMaxHWDecoders(int maxDecoders);
static int AutoConfigureHWDecoders(int maxPerGpuOverride = 0);
void SetHWDecoding(int hwMode, int preferredGpu = -1);
bool IsHWDecodingActive();
int GetHWDecodingGpuIndex();
void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize)
void SetImageQuality(int mode); // 0=fast (AI), 1=quality (display)
AVFrame* GetNV12Frame(); // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free)
AVFrame* GetCudaHWFrame(); // Returns CUDA HW frame (device ptrs) for zero-copy inference
bool IsCudaHWAccel(); // true when decoder uses CUDA (NV12 stays in GPU VRAM)
public:
void CheckLicense();
SPDLogger& _logger = SPDLogger::GetInstance("ANSRTSP", false);
bool _licenseValid{ false };
std::string _licenseKey;
//std::once_flag licenseOnceFlag;
};
}
extern "C" __declspec(dllexport) int CreateANSRTSPHandle(ANSCENTER::ANSRTSPClient * *Handle, const char* licenseKey, const char* username, const char* password, const char* url);
extern "C" __declspec(dllexport) int ReleaseANSRTSPHandle(ANSCENTER::ANSRTSPClient * *Handle);
extern "C" __declspec(dllexport) int GetRTSPImage(ANSCENTER::ANSRTSPClient **Handle, int &width, int &height, int64_t& timeStamp, LStrHandle jpegImage);
extern "C" __declspec(dllexport) int GetRTSPStrImage(ANSCENTER::ANSRTSPClient * *Handle, int& width, int& height, int64_t & timeStamp, std::string& jpegImage);
extern "C" __declspec(dllexport) int GetRTSPCVImage(ANSCENTER::ANSRTSPClient** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
extern "C" __declspec(dllexport) int StartRTSP(ANSCENTER::ANSRTSPClient * *Handle);
extern "C" __declspec(dllexport) int ReconnectRTSP(ANSCENTER::ANSRTSPClient * *Handle);
extern "C" __declspec(dllexport) int StopRTSP(ANSCENTER::ANSRTSPClient * *Handle);
extern "C" __declspec(dllexport) int PauseRTSP(ANSCENTER::ANSRTSPClient** Handle);
extern "C" __declspec(dllexport) int IsRTSPPaused(ANSCENTER::ANSRTSPClient * *Handle);
extern "C" __declspec(dllexport) int IsRTSPRunning(ANSCENTER::ANSRTSPClient * *Handle);
extern "C" __declspec(dllexport) int IsRTSPRecording(ANSCENTER::ANSRTSPClient * *Handle);
extern "C" __declspec(dllexport) void SetRTSPAudioVolume(ANSCENTER::ANSRTSPClient * *Handle, int volume);
extern "C" __declspec(dllexport) void EnableRTSPAudioVolume(ANSCENTER::ANSRTSPClient * *Handle, int status);
extern "C" __declspec(dllexport) void SetRTSPImageRotation(ANSCENTER::ANSRTSPClient * *Handle, double rotationAngle);
extern "C" __declspec(dllexport) int SetBBoxRTSP(ANSCENTER::ANSRTSPClient** Handle, int x, int y, int width, int height);
extern "C" __declspec(dllexport) int SetCropFlagRTSP(ANSCENTER::ANSRTSPClient** Handle, int cropFlag);
extern "C" __declspec(dllexport) void SetMaxHWDecoders(int maxDecoders);
extern "C" __declspec(dllexport) int AutoConfigureHWDecoders(int maxPerGpuOverride);
extern "C" __declspec(dllexport) void SetRTSPHWDecoding(ANSCENTER::ANSRTSPClient** Handle, int hwMode, int preferredGpu = -1);
extern "C" __declspec(dllexport) int IsRTSPHWDecodingActive(ANSCENTER::ANSRTSPClient** Handle);
extern "C" __declspec(dllexport) int GetRTSPHWDecodingGpuIndex(ANSCENTER::ANSRTSPClient** Handle);
extern "C" __declspec(dllexport) void SetRTSPImageQuality(ANSCENTER::ANSRTSPClient** Handle, int mode);
extern "C" __declspec(dllexport) void SetRTSPDisplayResolution(ANSCENTER::ANSRTSPClient** Handle, int width, int height);
#endif
+1221
View File
File diff suppressed because it is too large Load Diff
+110
View File
@@ -0,0 +1,110 @@
#ifndef ANSSRT_H
#define ANSSRT_H
#define ANSSRT_API __declspec(dllexport)
#include <iostream>
#include "ANSLicense.h"
#include "LabVIEWHeader/extcode.h"
#include <vector>
#include "sys_inc.h"
#include "srt_cln.h"
#include "hqueue.h"
#include "http.h"
#include "http_parse.h"
#include "video_decoder.h"
#include "audio_decoder.h"
#include "srt_player.h"
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
namespace ANSCENTER
{
class ANSSRT_API ANSSRTClient
{
protected:
std::unique_ptr<CSrtPlayer> _playerClient = std::make_unique<CSrtPlayer>();
std::string _username;
std::string _password;
std::string _url;
bool _useFullURL;
int _imageRotateDeg = 0;
int _displayWidth = 0; // 0 = no resize (return original resolution)
int _displayHeight = 0;
cv::Mat _pLastFrame;
std::string _lastJpegImage;
int _imageWidth, _imageHeight;
int64_t _pts;
bool _isPlaying;
std::recursive_mutex _mutex;
public:
ANSSRTClient();
~ANSSRTClient() noexcept;
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
[[nodiscard]] bool Init(std::string licenseKey, std::string username, std::string password, std::string url);
[[nodiscard]] bool Setup();
[[nodiscard]] bool Reconnect();
[[nodiscard]] bool Start();
[[nodiscard]] bool IsPaused();
[[nodiscard]] bool IsPlaying();
[[nodiscard]] bool IsRecording();
[[nodiscard]] bool Stop();
[[nodiscard]] bool Pause();
void Destroy();
void SetBBox(cv::Rect bbox);
void SetCrop(bool crop);
void EnableAudio(bool status);
void SetAudioVolume(int volume);
[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
[[nodiscard]] std::string GetJpegImage(int& width, int& height, int64_t& pts);
bool areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2);
void SetImageRotate(int mode) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_imageRotateDeg = mode;
if (mode > 360) _imageRotateDeg = 360;
};
static void SetMaxHWDecoders(int maxDecoders);
static int AutoConfigureHWDecoders(int maxPerGpuOverride = 0);
void SetHWDecoding(int hwMode, int preferredGpu = -1);
bool IsHWDecodingActive();
int GetHWDecodingGpuIndex();
void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize)
void SetImageQuality(int mode); // 0=fast (AI), 1=quality (display)
AVFrame* GetNV12Frame(); // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free)
AVFrame* GetCudaHWFrame(); // Returns CUDA HW frame (device ptrs) for zero-copy inference
bool IsCudaHWAccel(); // true when decoder uses CUDA (NV12 stays in GPU VRAM)
public:
void CheckLicense();
SPDLogger& _logger = SPDLogger::GetInstance("ANSSRT", false);
bool _licenseValid{ false };
std::string _licenseKey;
//std::once_flag licenseOnceFlag;
};
}
extern "C" __declspec(dllexport) int CreateANSSRTHandle(ANSCENTER::ANSSRTClient * *Handle, const char* licenseKey, const char* username, const char* password, const char* url);
extern "C" __declspec(dllexport) int ReleaseANSSRTHandle(ANSCENTER::ANSSRTClient * *Handle);
extern "C" __declspec(dllexport) int GetSRTImage(ANSCENTER::ANSSRTClient * *Handle, int& width, int& height, int64_t& timeStamp, LStrHandle jpegImage);
extern "C" __declspec(dllexport) int GetSRTStrImage(ANSCENTER::ANSSRTClient * *Handle, int& width, int& height, int64_t & timeStamp, std::string & jpegImage);
extern "C" __declspec(dllexport) int GetSRTCVImage(ANSCENTER::ANSSRTClient** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
extern "C" __declspec(dllexport) int StartSRT(ANSCENTER::ANSSRTClient * *Handle);
extern "C" __declspec(dllexport) int ReconnectSRT(ANSCENTER::ANSSRTClient * *Handle);
extern "C" __declspec(dllexport) int StopSRT(ANSCENTER::ANSSRTClient * *Handle);
extern "C" __declspec(dllexport) int PauseSRT(ANSCENTER::ANSSRTClient** Handle);
extern "C" __declspec(dllexport) int IsSRTPaused(ANSCENTER::ANSSRTClient * *Handle);
extern "C" __declspec(dllexport) int IsSRTRunning(ANSCENTER::ANSSRTClient * *Handle);
extern "C" __declspec(dllexport) int IsSRTRecording(ANSCENTER::ANSSRTClient * *Handle);
extern "C" __declspec(dllexport) void SetSRTAudioVolume(ANSCENTER::ANSSRTClient * *Handle, int volume);
extern "C" __declspec(dllexport) void EnableSRTAudioVolume(ANSCENTER::ANSSRTClient * *Handle, int status);
extern "C" __declspec(dllexport) void SetSRTImageRotation(ANSCENTER::ANSSRTClient * *Handle, double rotationAngle);
extern "C" __declspec(dllexport) int SetBBoxSRT(ANSCENTER::ANSSRTClient** Handle, int x, int y, int width, int height);
extern "C" __declspec(dllexport) int SetCropFlagSRT(ANSCENTER::ANSSRTClient** Handle, int cropFlag);
extern "C" __declspec(dllexport) void SetSRTMaxHWDecoders(int maxDecoders);
extern "C" __declspec(dllexport) int AutoConfigureSRTHWDecoders(int maxPerGpuOverride);
extern "C" __declspec(dllexport) void SetSRTHWDecoding(ANSCENTER::ANSSRTClient** Handle, int hwMode, int preferredGpu = -1);
extern "C" __declspec(dllexport) int IsSRTHWDecodingActive(ANSCENTER::ANSSRTClient** Handle);
extern "C" __declspec(dllexport) int GetSRTHWDecodingGpuIndex(ANSCENTER::ANSSRTClient** Handle);
extern "C" __declspec(dllexport) void SetSRTImageQuality(ANSCENTER::ANSSRTClient** Handle, int mode);
extern "C" __declspec(dllexport) void SetSRTDisplayResolution(ANSCENTER::ANSSRTClient** Handle, int width, int height);
#endif
File diff suppressed because it is too large Load Diff
+110
View File
@@ -0,0 +1,110 @@
#ifndef ANSVIDEOPLAYER_H
#define ANSVIDEOPLAYER_H
#define ANSVIDEOPLAYER_API __declspec(dllexport)
#include <iostream>
#include "ANSLicense.h"
#include "LabVIEWHeader/extcode.h"
#include <vector>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <thread>
#include <chrono>
#include <atomic>
#include <turbojpeg.h>
#include "file_player.h" // CFilePlayer (FFmpeg HW decode: CUDA/D3D11VA/DXVA2)
#include "video_decoder.h" // HW_DECODING_AUTO, HWDecoderPool
namespace ANSCENTER
{
class ANSVIDEOPLAYER_API ANSVIDEOPLAYER
{
protected:
cv::VideoCapture cap;
int _resWidth;
int _resHeight;
int64_t _totalFrames;
int64_t _currentFrame;
int64_t _previousPTS;
double _fps;
bool _isPlaying;
std::string _lastJpegImage;
cv::Mat _previousImage;
bool m_bPaused;
int _imageRotateDeg = 0;
std::string _url;
cv::Rect _bbox;
bool _crop;
std::recursive_mutex _mutex;
std::mutex _cvMutex;
std::condition_variable _cv;
tjhandle _jpegCompressor;
public:
// --- HW decode state (CFilePlayer composition) ---
// Public: accessed by extern "C" GetVideoPlayerCVImage for NV12/CUDA registry attachment
std::unique_ptr<CFilePlayer> _hwPlayer; // HW decode path (nullptr = CV fallback)
bool _hwDecodeActive = false; // true when _hwPlayer init succeeded
int _hwGpuIndex = -1; // GPU index for registry (-1 = CPU only)
bool _hwCudaAccel = false; // true = NVIDIA CUDA zero-copy available
bool _hwEOF = false; // true when video reached end of file
int64_t _hwFrameCount = 0; // frame counter for EOF detection
int64_t _hwLastPts = 0; // last video PTS for EOF wrap detection
ANSVIDEOPLAYER();
~ANSVIDEOPLAYER() noexcept;
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
[[nodiscard]] bool Setup();
[[nodiscard]] bool Reconnect();
[[nodiscard]] bool Start();
[[nodiscard]] bool IsPaused();
[[nodiscard]] bool IsPlaying();
[[nodiscard]] bool IsRecording();
void SetBBox(cv::Rect bbox);
void SetCrop(bool crop);
[[nodiscard]] bool Stop();
void Destroy();
void EnableAudio(bool status);
void SetAudioVolume(int volume);
void SetImageRotate(int mode) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_imageRotateDeg = mode;
if (mode > 360) _imageRotateDeg = 360;
};
[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
[[nodiscard]] cv::Mat GetInferenceImage(); // Returns full-res frame for AI inference (no resize/rotation)
[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
[[nodiscard]] std::string GetJpegStringImage(int& width, int& height, int64_t& pts);
void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize)
public:
void CheckLicense();
SPDLogger& _logger = SPDLogger::GetInstance("ANSVideoPlayer", false);
bool _licenseValid{ false };
//std::once_flag licenseOnceFlag;
std::string _licenseKey;
cv::Mat _inferenceCloneCurr; // Heap-cloned full-res (keeps data alive for registry)
cv::Mat _inferenceClonePrev; // Previous clone (keeps data alive during ANSRTYOLO read)
private:
std::string encodeJpegString(const cv::Mat& img, int quality = 80);
int _displayWidth = 0; // 0 = no resize (return original)
int _displayHeight = 0;
cv::Mat _inferenceImage; // Full-res frame for AI inference
};
}
extern "C" __declspec(dllexport) int CreateANSVideoPlayerHandle(ANSCENTER::ANSVIDEOPLAYER** Handle, const char* licenseKey, const char* url);
extern "C" __declspec(dllexport) int ReleaseANSVideoPlayerHandle(ANSCENTER::ANSVIDEOPLAYER** Handle);
extern "C" __declspec(dllexport) int GetVideoPlayerImage(ANSCENTER::ANSVIDEOPLAYER** Handle, int& width, int& height, int64_t& timeStamp, LStrHandle jpegImage);
extern "C" __declspec(dllexport) int GetVideoPlayerStrImage(ANSCENTER::ANSVIDEOPLAYER** Handle, int& width, int& height, int64_t& timeStamp, std::string& jpegImage);
extern "C" __declspec(dllexport) int GetVideoPlayerCVImage(ANSCENTER::ANSVIDEOPLAYER** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
extern "C" __declspec(dllexport) int StartVideoPlayer(ANSCENTER::ANSVIDEOPLAYER** Handle);
extern "C" __declspec(dllexport) int ReconnectVideoPlayer(ANSCENTER::ANSVIDEOPLAYER** Handle);
extern "C" __declspec(dllexport) int StopVideoPlayer(ANSCENTER::ANSVIDEOPLAYER** Handle);
extern "C" __declspec(dllexport) int IsVideoPlayerPaused(ANSCENTER::ANSVIDEOPLAYER** Handle);
extern "C" __declspec(dllexport) int IsVidedoPlayerRunning(ANSCENTER::ANSVIDEOPLAYER** Handle);
extern "C" __declspec(dllexport) int IsVideoPlayerRecording(ANSCENTER::ANSVIDEOPLAYER** Handle);
extern "C" __declspec(dllexport) void SetVideoPlayerAudioVolume(ANSCENTER::ANSVIDEOPLAYER** Handle, int volume);
extern "C" __declspec(dllexport) void EnableVideoPlayerAudioVolume(ANSCENTER::ANSVIDEOPLAYER** Handle, int status);
extern "C" __declspec(dllexport) void SetVideoPlayerImageRotation(ANSCENTER::ANSVIDEOPLAYER** Handle, double rotationAngle);
extern "C" __declspec(dllexport) int SetBBoxVideoPlayer(ANSCENTER::ANSVIDEOPLAYER** Handle, int x, int y, int width, int height);
extern "C" __declspec(dllexport) int SetCropFlagVideoPlayer(ANSCENTER::ANSVIDEOPLAYER** Handle, int cropFlag);
extern "C" __declspec(dllexport) void SetVideoPlayerDisplayResolution(ANSCENTER::ANSVIDEOPLAYER** Handle, int width, int height);
#endif
+1758
View File
File diff suppressed because it is too large Load Diff
+140
View File
@@ -0,0 +1,140 @@
#ifndef ANSWEBCAM_H
#define ANSWEBCAM_H
#define ANSWEBCAM_API __declspec(dllexport)
#include <iostream>
#include "ANSLicense.h"
#include "LabVIEWHeader/extcode.h"
#include <vector>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <dshow.h>
#include <mutex>
#include <nvjpeg.h>
#include <cuda_runtime.h>
#include <turbojpeg.h>
#pragma comment(lib, "strmiids.lib")
#pragma comment(lib, "ole32.lib")
namespace ANSCENTER
{
struct Resolution {
int width;
int height;
Resolution(int w, int h) : width(w), height(h) {}
int getArea() const {
return width * height;
}
bool operator<(const Resolution& other) const {
return getArea() < other.getArea();
}
};
class ANSWEBCAM_API ANSWEBCAMPlayer
{
protected:
// Add this structure definition in the private section
cv::VideoCapture cap;
int _deviceId;
int _resWidth;
int _resHeight;
int _savedResWidth = 0; // Saved resolution for stop/start restore
int _savedResHeight = 0;
bool _enableMaxResolution = false; // false = Full HD default, true = max resolution
int64_t _previousPTS;
cv::Mat _previousImage;
bool m_bPaused;
std::string _lastJpegImage;
bool _isPlaying;
std::vector<std::string> _cameraNames;
int _imageRotateDeg = 0;
std::recursive_mutex _mutex;
cv::Rect _bbox;
bool _crop;
EngineType engineType;
// Initialize nvJPEG structures
nvjpegHandle_t nv_handle;
nvjpegEncoderState_t nv_enc_state;
nvjpegEncoderParams_t nv_enc_params;
cudaStream_t stream;
tjhandle _jpegCompressor;
public:
ANSWEBCAMPlayer();
~ANSWEBCAMPlayer() noexcept;
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
[[nodiscard]] bool Setup();
[[nodiscard]] bool Reconnect();
[[nodiscard]] bool Start();
[[nodiscard]] bool IsPaused();
[[nodiscard]] bool IsPlaying();
[[nodiscard]] bool IsRecording();
[[nodiscard]] bool Stop();
void Destroy();
void EnableAudio(bool status);
void SetAudioVolume(int volume);
[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
[[nodiscard]] std::string GetJpegStringImage(int& width, int& height, int64_t& pts);
[[nodiscard]] static std::string VectorToCommaSeparatedString(const std::vector<std::string>& inputVector);
[[nodiscard]] static std::vector<std::string> ScanWebcamDevices();
[[nodiscard]] static std::string GetSupportedResolutions(int deviceId);
static std::mutex mtx;
void SetImageRotate(int mode) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_imageRotateDeg = mode;
if (mode > 360) _imageRotateDeg = 360;
};
void SetBBox(cv::Rect bbox);
void SetCrop(bool crop);
void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize)
[[nodiscard]] cv::Mat GetInferenceImage(); // Returns full-res frame for AI inference (no resize)
cv::Mat _inferenceCloneCurr; // Heap-cloned full-res (keeps data alive for registry)
cv::Mat _inferenceClonePrev; // Previous clone (keeps data alive during ANSRTYOLO read)
public:
void CheckLicense();
SPDLogger& _logger = SPDLogger::GetInstance("ANSWebcamPlayer", false);
bool _licenseValid{ false };
std::string _licenseKey;
// Resolution management methods
std::vector<Resolution> GetAvailableResolutions(int deviceId);
bool SetBestResolution(int deviceId);
bool SetPreferredResolution(int deviceId);
bool SetResolution(int width, int height);
void SetEnableMaxResolution(bool enable);
std::string GetAvailableResolutionsString(int deviceId);
private:
std::string encodeJpegString(const cv::Mat& img, int quality = 80);
std::string encodeMatToJpegWithNvJPEG(const cv::Mat& inputMat, int quality = 80);
void uploadPlanarBGRToGPU(const cv::Mat& inputMat, unsigned char** data);
int _displayWidth = 0; // 0 = no resize (return original)
int _displayHeight = 0;
cv::Mat _inferenceImage; // Full-res frame for AI inference
};
std::mutex ANSWEBCAMPlayer::mtx; // Definition of the static mutex
}
extern "C" __declspec(dllexport) int CreateANSWebcamPlayerHandle(ANSCENTER::ANSWEBCAMPlayer * *Handle, const char* licenseKey, const char* url);
extern "C" __declspec(dllexport) int CreateANSWebcamPlayerWithMaxResoHandle(ANSCENTER::ANSWEBCAMPlayer * *Handle, const char* licenseKey, const char* url);
extern "C" __declspec(dllexport) int ReleaseANSWebcamPlayerHandle(ANSCENTER::ANSWEBCAMPlayer * *Handle);
extern "C" __declspec(dllexport) int ScanANSWebcamPlayer(LStrHandle cameraNames);
extern "C" __declspec(dllexport) int ScanStrANSWebcamPlayer(std::vector<std::string> &cameraNameList);
extern "C" __declspec(dllexport) int GetWebcamImage(ANSCENTER::ANSWEBCAMPlayer * *Handle, int& width, int& height, int64_t & timeStamp, LStrHandle jpegImage);
extern "C" __declspec(dllexport) int GetWebcamStrImage(ANSCENTER::ANSWEBCAMPlayer * *Handle, int& width, int& height, int64_t & timeStamp, std::string & jpegImage);
extern "C" __declspec(dllexport) int GetWebcamCVImage(ANSCENTER::ANSWEBCAMPlayer** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
extern "C" __declspec(dllexport) int StartWebcamPlayer(ANSCENTER::ANSWEBCAMPlayer * *Handle);
extern "C" __declspec(dllexport) int ReconnectWebcamPlayer(ANSCENTER::ANSWEBCAMPlayer * *Handle);
extern "C" __declspec(dllexport) int StopWebcamPlayer(ANSCENTER::ANSWEBCAMPlayer * *Handle);
extern "C" __declspec(dllexport) int IsWebcamPlayerPaused(ANSCENTER::ANSWEBCAMPlayer * *Handle);
extern "C" __declspec(dllexport) int IsWebcamPlayerRunning(ANSCENTER::ANSWEBCAMPlayer * *Handle);
extern "C" __declspec(dllexport) int IsWebcamPlayerRecording(ANSCENTER::ANSWEBCAMPlayer * *Handle);
extern "C" __declspec(dllexport) void SetWebcamPlayerAudioVolume(ANSCENTER::ANSWEBCAMPlayer * *Handle, int volume);
extern "C" __declspec(dllexport) void EnableWebcamPlayerAudioVolume(ANSCENTER::ANSWEBCAMPlayer * *Handle, int status);
extern "C" __declspec(dllexport) void SetWebcamImageRotation(ANSCENTER::ANSWEBCAMPlayer * *Handle, double rotationAngle);
extern "C" __declspec(dllexport) int ScanSupportedResolutions(std::string cameraName,std::vector<std::string> cameraNameList, std::string &supportedResolution);
extern "C" __declspec(dllexport) int SetBBoxANSWebcamPlayer(ANSCENTER::ANSWEBCAMPlayer** Handle, int x, int y, int width, int height);
extern "C" __declspec(dllexport) int SetCropFlagANSWebcamPlayer(ANSCENTER::ANSWEBCAMPlayer** Handle, int cropFlag);
extern "C" __declspec(dllexport) void SetWebcamDisplayResolution(ANSCENTER::ANSWEBCAMPlayer** Handle, int width, int height);
#endif
+121
View File
@@ -0,0 +1,121 @@
# ANSCV — Computer Vision / Media DLL (video capture, RTSP, FFmpeg)
file(GLOB ANSCV_HEADERS "*.h")
file(GLOB ANSCV_SOURCES "*.cpp")
# Exclude old VideoPlayer.cpp — superseded by MediaClient/media/video_player.cpp
list(REMOVE_ITEM ANSCV_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/VideoPlayer.cpp")
# MediaClient sources compiled into ANSCV (from original vcxproj)
set(MEDIACLIENT_SOURCES
${CMAKE_SOURCE_DIR}/MediaClient/media/audio_capture.cpp
${CMAKE_SOURCE_DIR}/MediaClient/media/audio_capture_win.cpp
${CMAKE_SOURCE_DIR}/MediaClient/media/audio_decoder.cpp
${CMAKE_SOURCE_DIR}/MediaClient/media/audio_encoder.cpp
${CMAKE_SOURCE_DIR}/MediaClient/media/audio_play.cpp
${CMAKE_SOURCE_DIR}/MediaClient/media/audio_play_win.cpp
${CMAKE_SOURCE_DIR}/MediaClient/media/avcodec_mutex.cpp
${CMAKE_SOURCE_DIR}/MediaClient/media/avi_write.cpp
${CMAKE_SOURCE_DIR}/MediaClient/media/file_player.cpp
${CMAKE_SOURCE_DIR}/MediaClient/media/http_flv_player.cpp
${CMAKE_SOURCE_DIR}/MediaClient/media/http_mjpeg_player.cpp
${CMAKE_SOURCE_DIR}/MediaClient/media/media_codec.cpp
${CMAKE_SOURCE_DIR}/MediaClient/media/media_parse.cpp
${CMAKE_SOURCE_DIR}/MediaClient/media/media_util.cpp
${CMAKE_SOURCE_DIR}/MediaClient/media/rtmp_player.cpp
${CMAKE_SOURCE_DIR}/MediaClient/media/rtsp_player.cpp
${CMAKE_SOURCE_DIR}/MediaClient/media/srt_player.cpp
${CMAKE_SOURCE_DIR}/MediaClient/media/video_decoder.cpp
${CMAKE_SOURCE_DIR}/MediaClient/media/video_player.cpp
${CMAKE_SOURCE_DIR}/MediaClient/rtp/h264_rtp_rx.cpp
${CMAKE_SOURCE_DIR}/MediaClient/rtp/h264_util.cpp
${CMAKE_SOURCE_DIR}/MediaClient/rtp/h265_rtp_rx.cpp
${CMAKE_SOURCE_DIR}/MediaClient/rtp/h265_util.cpp
${CMAKE_SOURCE_DIR}/MediaClient/rtp/mjpeg_rtp_rx.cpp
${CMAKE_SOURCE_DIR}/MediaClient/rtp/mjpeg_tables.cpp
${CMAKE_SOURCE_DIR}/MediaClient/rtp/ts_parser.cpp
${CMAKE_SOURCE_DIR}/MediaClient/rtsp/rtsp_backchannel.cpp
${CMAKE_SOURCE_DIR}/MediaClient/rtsp/rtsp_cln.cpp
)
add_library(ANSCV SHARED ${ANSCV_HEADERS} ${ANSCV_SOURCES} ${MEDIACLIENT_SOURCES})
# MediaClient sources skip precompiled headers (they don't include ANSCV's pch.h)
set_source_files_properties(${MEDIACLIENT_SOURCES} PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
target_include_directories(ANSCV PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
# MediaClient includes (referenced from original ANLS)
target_include_directories(ANSCV PRIVATE
${CMAKE_SOURCE_DIR}/MediaClient
${CMAKE_SOURCE_DIR}/MediaClient/media
${CMAKE_SOURCE_DIR}/MediaClient/rtsp
${CMAKE_SOURCE_DIR}/MediaClient/rtp
${CMAKE_SOURCE_DIR}/MediaClient/http
${CMAKE_SOURCE_DIR}/MediaClient/directx
${CMAKE_SOURCE_DIR}/MediaClient/directx/include
${CMAKE_SOURCE_DIR}/MediaClient/ffmpeg/include
${CMAKE_SOURCE_DIR}/MediaClient/openssl/include
${CMAKE_SOURCE_DIR}/MediaClient/libsrt/include
${CMAKE_SOURCE_DIR}/MediaClient/bm
${CMAKE_SOURCE_DIR}/MediaClient/rtmp
${CMAKE_SOURCE_DIR}/MediaClient/librtmp
${CMAKE_SOURCE_DIR}/MediaClient/srt
${SHARED_INCLUDE_DIR}
)
target_link_libraries(ANSCV
PRIVATE ANSLicensingSystem
PRIVATE labview
PRIVATE spdlog_dep
PRIVATE ANSLibsLoader
PRIVATE opencv
PRIVATE ffmpeg
PRIVATE turbojpeg
PRIVATE zxing
PRIVATE zlib_dep
PRIVATE CUDA::cudart_static
PRIVATE CUDA::cublasLt
PRIVATE CUDA::nvjpeg
)
# Platform-specific libs
if(WIN32)
target_link_directories(ANSCV PRIVATE
${CMAKE_SOURCE_DIR}/MediaClient/directx/lib/x64
${CMAKE_SOURCE_DIR}/MediaClient/openssl/lib/x64
${CMAKE_SOURCE_DIR}/MediaClient/libsrt/lib/x64
)
# Prebuilt MediaClient protocol libraries (built from MediaClient VS project)
target_link_directories(ANSCV PRIVATE "${ANLS_ROOT}/MediaClient/x64/Release")
target_link_libraries(ANSCV PRIVATE
RtspClientLibrary HttpFlvClientLibrary HttpMjpegClientLibrary
RtmpClientLibrary SrtClientLibrary
d3d9 d3dx9 dxva2
libcrypto libssl srt
${WIN_COMMON_LIBS}
)
else()
target_link_libraries(ANSCV PRIVATE ${UNIX_COMMON_LIBS})
endif()
target_compile_definitions(ANSCV PRIVATE
ANSCV_EXPORTS
_USRDLL
HT_STATIC
HTTPS
BACKCHANNEL
METADATA
REPLAY
OVER_HTTP
OVER_WEBSOCKET
NOMINMAX
)
if(WIN32)
target_compile_definitions(ANSCV PRIVATE UNICODE _UNICODE __WINDOWS_OS__)
else()
target_compile_definitions(ANSCV PRIVATE __LINUX_OS__)
endif()
target_precompile_headers(ANSCV PRIVATE pch.h)
File diff suppressed because it is too large Load Diff
+199
View File
@@ -0,0 +1,199 @@
/***************************************************************************************
*
* IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
*
* By downloading, copying, installing or using the software you agree to this license.
* If you do not agree to this license, do not download, install,
* copy or use the software.
*
* Copyright (C) 2014-2024, Happytimesoft Corporation, all rights reserved.
*
* Redistribution and use in binary forms, with or without modification, are permitted.
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*
****************************************************************************************/
#ifndef VIDEO_PLAYER_H
#define VIDEO_PLAYER_H
#include "sys_inc.h"
#include "hqueue.h"
#include "video_decoder.h"
#include "audio_decoder.h"
#include "audio_play.h"
#include "avi_write.h"
#include "media_util.h"
#include <list>
#include <string>
typedef struct
{
uint32 SyncTimestamp;
struct timeval SyncTime;
} HTCLOCK;
typedef std::list<AVFrame*> FRAMELIST;
class CVideoPlayer
{
public:
CVideoPlayer();
virtual ~CVideoPlayer();
virtual BOOL open(std::string fileName);
virtual BOOL play() = 0;
virtual void stop() = 0;
virtual BOOL pause() = 0;
virtual void close();
virtual BOOL seek(int pos) { return FALSE; }
virtual BOOL isPlaying() { return m_bPlaying; }
virtual BOOL isPaused() { return m_bPaused; }
virtual void setVolume(int volume);
virtual void snapshot(int videofmt);
virtual BOOL record(std::string baseName);
virtual void stopRecord();
virtual BOOL isRecording() { return m_bRecording; }
virtual BOOL onRecord() { return FALSE; }
virtual int getVideoClock() { return 1000; }
virtual int getAudioClock() { return 1000; }
virtual void setWindowSize(int size);
virtual int64 getElapse() { return 0; }
virtual int64 getDuration() { return 0; }
virtual int getVideoCodec() { return m_nVideoCodec; }
virtual int getAudioCodec() { return m_nAudioCodec; }
virtual void setAuthInfo(std::string acct, std::string pass) { m_acct = acct; m_pass = pass; }
virtual void setRenderMode(int mode) { m_nRenderMode = mode; }
virtual void setHWDecoding(int mode) { m_nHWDecoding = mode; }
virtual void setRtpMulticast(BOOL flag) {}
virtual void setRtpOverUdp(BOOL flag) {}
#ifdef OVER_HTTP
virtual void setRtspOverHttp(int flag, int port) {}
#endif
#ifdef OVER_WEBSOCKET
virtual void setRtspOverWs(int flag, int port) {}
#endif
#ifdef BACKCHANNEL
virtual int getBCFlag() { return 0; }
virtual void setBCFlag(int flag) {}
virtual int getBCDataFlag() { return 0; }
virtual void setBCDataFlag(int flag) {}
virtual void setAudioDevice(int index) {}
#endif
#ifdef REPLAY
virtual int getReplayFlag() { return 0; }
virtual void setReplayFlag(int flag) {}
virtual void setScale(double scale) {}
virtual void setRateControlFlag(int flag) {}
virtual void setImmediateFlag(int flag) {}
virtual void setFramesFlag(int flag, int interval) {}
virtual void setReplayRange(time_t start, time_t end) {}
#endif
BOOL openVideo(int codec, uint8* extradata = NULL, int extradata_size = 0);
BOOL openVideo(enum AVCodecID codec, uint8* extradata = NULL, int extradata_size = 0);
void closeVideo();
BOOL openAudio(int codec, int samplerate, int channels, int bitpersample = 0);
BOOL openAudio(enum AVCodecID codec, int samplerate, int channels, int bitpersample = 0);
void closeAudio();
void playVideo(uint8* data, int len, uint32 ts, uint16 seq);
void playAudio(uint8* data, int len, uint32 ts, uint16 seq);
void recordVideo(uint8* data, int len, uint32 ts, uint16 seq);
void recordAudio(uint8* data, int len, uint32 ts, uint16 seq);
int getVideoWidth();
int getVideoHeight();
double getFrameRate();
int getSampleRate() { return m_nSampleRate; }
int getChannel() { return m_nChannel; }
void onVideoFrame(AVFrame* frame);
void onAudioFrame(AVFrame* frame);
void audioPlayThread();
void videoPlayThread();
//signals:
// void notify(int);
// void snapshoted(AVFrame*);
protected:
void updateClock(HTCLOCK* clock, uint32 ts, int frequency);
BOOL initFrame(AVFrame*& frame, int width, int height, AVPixelFormat pixfmt);
//BOOL initVideoRender(int width, int height);
BOOL doSnapshot(AVFrame* srcframe);
AVFrame* convertFrame(AVFrame* srcframe, AVFrame* dstframe, BOOL updown);
void recordVideoEx(uint8* data, int len, uint32 ts, uint16 seq);
BOOL recordSwitchCheck();
void recordFileSwitch();
protected:
BOOL m_bVideoInited;
BOOL m_bAudioInited;
CVideoDecoder* m_pVideoDecoder;
CAudioDecoder* m_pAudioDecoder;
//CVideoRender* m_pVideoRender;
CAudioPlay* m_pAudioPlay;
BOOL m_bPlaying;
BOOL m_bPaused;
std::string m_acct;
std::string m_pass;
std::string m_sFileName;
std::string m_sBaseName;
//WId m_nVideoWnd;
int m_size;
BOOL m_bSizeChanged;
int m_nRenderMode;
int m_nHWDecoding;
AVPixelFormat m_nDstVideoFmt;
BOOL m_bUpdown;
BOOL m_bSnapshot;
int m_nSnapVideoFmt;
H26XParamSets m_h26XParamSets;
int m_nVideoCodec;
int m_nAudioCodec;
int m_nSampleRate;
int m_nChannel;
int m_nBitPerSample;
AVFrame* m_pSnapFrame;
AVFrame* m_pRenderFrame;
BOOL m_bRecording;
BOOL m_bNalFlag;
AVICTX* m_pAviCtx;
void* m_pRecordMutex;
HTCLOCK m_audioClock;
void* m_pAudioListMutex;
FRAMELIST m_audioFrameList;
BOOL m_audioPlayFlag;
pthread_t m_audioPlayThread;
HTCLOCK m_videoClock;
void* m_pVideoListMutex;
FRAMELIST m_videoFrameList;
BOOL m_videoPlayFlag;
pthread_t m_videoPlayThread;
uint64 m_nLastAudioPts;
time_t m_lastAudioTS;
};
#endif // end of VIDEO_PLAYER_H
+106
View File
@@ -0,0 +1,106 @@
// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"
#include <mutex>
#include <unordered_map>
#include <iostream>
#include <functional>
// ── Global handle registry for shutdown cleanup ──────────────────────────
// Streaming handle creators register a stop-callback on creation.
// DLL_PROCESS_DETACH invokes all callbacks so threads exit before DLL unmaps.
static std::mutex& shutdownMutex() {
static std::mutex m;
return m;
}
static std::unordered_map<void*, std::function<void()>>& shutdownRegistry() {
static std::unordered_map<void*, std::function<void()>> r;
return r;
}
// Called by Create*Handle functions to register a cleanup callback
extern "C" void anscv_register_handle(void* handle, void(*stopFn)(void*)) {
if (!handle || !stopFn) return;
std::lock_guard<std::mutex> lk(shutdownMutex());
shutdownRegistry()[handle] = [=]() { stopFn(handle); };
}
// Called by Release*Handle functions to unregister (handle properly cleaned up)
extern "C" void anscv_unregister_handle(void* handle) {
if (!handle) return;
std::lock_guard<std::mutex> lk(shutdownMutex());
shutdownRegistry().erase(handle);
}
// Stop all leaked handles
static void stopAllHandles() {
std::unordered_map<void*, std::function<void()>> copy;
{
std::lock_guard<std::mutex> lk(shutdownMutex());
copy.swap(shutdownRegistry());
}
for (auto& [ptr, fn] : copy) {
try { fn(); } catch (...) {}
}
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
) noexcept
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
// Pin the DLL so it is never unmapped from the process address space.
// ANSCV creates background threads (RTSP receive, timers) that may
// still be running when LabVIEW's CLR/COM shutdown unloads the DLL.
// If the DLL is unmapped while threads are executing its code, the
// threads crash with an access violation at <Unloaded_ANSCV.dll>.
// Pinning keeps the code pages mapped; the OS kills all threads when
// the process exits, so this is safe and is Microsoft's recommended
// pattern for DLLs that own threads.
{
HMODULE hSelf = nullptr;
GetModuleHandleExW(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_PIN,
reinterpret_cast<LPCWSTR>(&DllMain),
&hSelf);
}
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
// CRITICAL: Do NOT call stopAllHandles() here.
//
// DllMain holds the OS loader lock (LdrpLoaderLock). Destroying
// streaming handles (CRtspPlayer → CVideoPlayer → CWAudioPlay)
// calls dsound!CThread::Terminate which waits for the DirectSound
// admin thread to exit. That thread needs the loader lock to shut
// down → classic deadlock. (Confirmed by crash dump analysis:
// Thread 0 holds LdrpLoaderLock and waits for dsound thread 331,
// which is blocked on LdrpLoaderLock.)
//
// If lpReserved is non-NULL the process is terminating — the OS
// will tear down all threads and reclaim resources; cleanup is
// unnecessary and dangerous. If lpReserved is NULL (dynamic
// FreeLibrary), LabVIEW should have called Release*Handle first.
// Log leaked handles for diagnostics but do NOT stop them here.
if (lpReserved == nullptr) {
// Dynamic unload — warn about leaked handles (non-blocking)
try {
std::lock_guard<std::mutex> lk(shutdownMutex());
if (!shutdownRegistry().empty()) {
std::cerr << "[ANSCV] WARNING: " << shutdownRegistry().size()
<< " streaming handle(s) leaked at DLL unload."
<< " Call Release*Handle before FreeLibrary." << std::endl;
}
} catch (...) {}
}
break;
}
return TRUE;
}
+7
View File
@@ -0,0 +1,7 @@
#pragma once
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#define NOMINMAX // Prevent windows.h from defining min/max macros
// which break std::min / std::max (C2589)
// Windows Header Files
#include <windows.h>
+5
View File
@@ -0,0 +1,5 @@
// pch.cpp: source file corresponding to the pre-compiled header
#include "pch.h"
// When you are using pre-compiled headers, this source file is necessary for compilation to succeed.
+25
View File
@@ -0,0 +1,25 @@
// pch.h: This is a precompiled header file.
// Files listed below are compiled only once, improving build performance for future builds.
// This also affects IntelliSense performance, including code completion and many code browsing features.
// However, files listed here are ALL re-compiled if any one of them is updated between builds.
// Do not add files here that you will be updating frequently as this negates the performance advantage.
#ifndef PCH_H
#define PCH_H
// add headers that you want to pre-compile here
#include "framework.h"
#include <algorithm>
#include <memory>
#include <string>
#include <string_view>
#include <vector>
#include <mutex>
// Legacy code uses bare min/max (without std:: prefix).
// NOMINMAX prevents windows.h from defining min/max macros,
// so pull them in from <algorithm> instead.
using std::min;
using std::max;
#endif //PCH_H
+3435
View File
File diff suppressed because it is too large Load Diff
+352
View File
@@ -0,0 +1,352 @@
#ifndef ANSFR_H
#define ANSFR_H
#define ANSFR_API __declspec(dllexport)
#pragma once
#include "ANSFRCommon.h"
#include "FaceDatabase.h"
#include <shared_mutex>
#include <atomic>
#define MAX_FACE_CHECKER_FRAMES 20
#define FACE_CONFIDENT_LEVEL 5
namespace ANSCENTER
{
struct FaceDetection {
FaceResultObject faceObject; // Bounding box
int faceConfLevel; // face confident level, if face is detected then it will increase otherwise, it will be recreased.
};
class FaceChecker {
public:
FaceChecker(int maxFrames = MAX_FACE_CHECKER_FRAMES, float minScore = 0.7f)
: maxFrames(maxFrames), minScore(minScore) {}
// Add detected faces from a new frame
std::vector<FaceResultObject> ValidateDetectedFaces(const std::vector<FaceResultObject>& faces);
std::vector<FaceResultObject> UniqueFaces(const std::vector<FaceResultObject>& faces);
private:
int maxFrames;
float minScore;
std::recursive_mutex _mutex;
std::unordered_map<std::string, std::vector<std::vector<FaceDetection>>> frameBufferMap; // Keyed by cameraId
};
/////
// Fix #11: ScopedTimer now logs via SPDLogger instead of being a dead no-op
class ScopedTimer {
public:
explicit ScopedTimer(const std::string& operation_name)
: _name(operation_name)
, _start(std::chrono::steady_clock::now())
{
}
~ScopedTimer() noexcept {
try {
auto end = std::chrono::steady_clock::now();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - _start).count();
ANSCENTER::SPDLogger::GetInstance("ANSFR").LogDebug(
"ScopedTimer", _name + " took " + std::to_string(ms) + " ms",
__FILE__, __LINE__);
}
catch (...) {} // Never throw from destructor
}
private:
std::string _name;
std::chrono::steady_clock::time_point _start;
};
class ANSFR_API ANSFacialRecognition
{
public:
[[nodiscard]] int Initialize(const std::string & licenseKey,
const std::string& configFile,
const std::string& databaseFilePath,
const std::string& recogniserFilePath,
const std::string& detectorFilePath="",
int precisionType=0,
float knownPersonThreshold=0.35,
bool enableAgeGender=true,
bool enableFaceEmotions=true,
bool enableHeadPose=true,
int minFaceSize=30,
float faceDetectorScoreThreshold=0.5,
bool faceliveness=true,
bool antiSpoofing=true);
[[nodiscard]] bool LoadEngine();
int InsertUser(const std::string& userCode,const std::string& userName);
int UpdateUser(int userId, const std::string& userCode, const std::string& userName);
int DeleteUser(int userId);
int DeleteUsers(const std::vector<int>& userIds);
int InsertFace(int userId, const cv::Mat& image);
[[nodiscard]] std::vector<int> InsertMultipleFaces(int userId, const cv::Mat& image);
[[nodiscard]] int CheckFace(const cv::Mat& image);// return 0 if face is not valid otherwise 1
int DeleteFace(int faceId);
int DeleteFacesByUser(int userId);
int GetUser(int userId, std::string& userRecord);
int GetUsers(std::string& userRecords, std::vector<int>&userIds);
int GetFace(int faceId, std::string& faceRecord);
int GetFaces(int userId, std::string& faceRecords);
void CheckLicense();
bool Reload();
bool UpdateUserDictionary();
bool UpdateParameters(float knownPersonThreshold = 0.35,bool enableAgeGender = true,bool enableFaceEmotions = true,bool enableHeadPose = true, int minFaceSize=112, float faceDetectorScoreThreshold=0.5, bool faceliveness=true, bool antiSpoof=true, bool removeFakeFace=false);
bool GetFaceParameters(float& knownPersonThreshold, bool& enableAgeGender, bool& enableFaceEmotions, bool& enableHeadPose, int &minFaceSize, float& faceDetectorScoreThreshold, bool& faceliveness, bool& antiSpoof, bool& removeFakeFace);
bool UpdateFaceQueue(int queueSize, int faceThresholdSize, bool enableFaceQueue);
bool GetFaceQueue(int& queueSize, int& faceThresholdSize, bool& enableFaceQueue);
[[nodiscard]] std::vector<FaceResultObject> Recognize(const cv::Mat& frame); // Expect to return on 1 object
[[nodiscard]] std::vector<FaceResultObject> Inference(const cv::Mat& input);
[[nodiscard]] std::vector<FaceResultObject> Detect(const cv::Mat& input);
[[nodiscard]] std::vector<Object> FaceDetect(const cv::Mat& input);
[[nodiscard]] std::vector<FaceResultObject> Recognize(const cv::Mat& frame, const std::string& camera_id); // Expect to return on 1 object
[[nodiscard]] std::vector<FaceResultObject> Inference(const cv::Mat& input, const std::string& camera_id);
[[nodiscard]] std::vector<FaceResultObject> Detect(const cv::Mat& input, const std::string& camera_id);
[[nodiscard]] std::vector<Object> FaceDetect(const cv::Mat& input, const std::string& camera_id);
[[nodiscard]] std::string FaceObjectsToJsonString(const std::vector<FaceResultObject>& faces);
[[nodiscard]] std::string FaceToJsonString(const std::vector<Object>& faces);
[[nodiscard]] std::string PolygonToString(const std::vector<cv::Point2f>& polygon);
[[nodiscard]] std::string KeypointsToString(const std::vector<float>& kps);
enum class LogLevel { Debug, Info, Warn, Error, Fatal };
void LogThreadSafe(const std::string& function, const std::string& message,
LogLevel level = LogLevel::Error);
void LogError(const std::string& function,const std::string& message,const std::string& camera_id);
void MarkAsUnknown(FaceResultObject& face);
ANSFacialRecognition();
~ANSFacialRecognition() noexcept;
void UnloadEngine();
void Destroy();
private:
int GetUser(int userId, UserRecord& userRecord);
int GetUser(int userId, const std::string& userCode,const std::string& userName, UserRecord& userRecord);
int GetUsers(std::vector<UserRecord>& userRecords, std::vector<int>& userIds);
int GetFace(int faceId, FaceRecord& faceRecord);
int GetFaces(int userId, std::vector<FaceRecord>& faceRecords);
std::vector<FaceResultObject> UpdateFaceAttributes(const std::vector<FaceResultObject>& resultObjects,
const std::string& camera_id = "");
std::vector<Object> CheckFaces(const std::vector<Object>& faceObjects,bool checkFaceSize=true);
std::string GetOpenVINODevice();
ModelConfig CreateDetectorModelConfig();
bool InitializeDetector();
bool InitializeRecognizer();
bool InitializeAntispoofingModel(const std::string& deviceName);
bool InitializeAgeGenderModel(const std::string& deviceName);
bool InitializeEmotionModel(const std::string& deviceName);
bool InitializeHeadPoseModel(const std::string& deviceName);
void ensureUniqueUserIdWithHighestConfidence(std::vector<FaceResultObject>& resultObjects);
Object GetLargestObject(const std::vector<Object>& objects);
bool AreFacesSimilar(const FaceResultObject& face1, const FaceResultObject& face2);
size_t GenerateFaceHash(const FaceResultObject& face, const std::vector<FaceResultObject>& detectedObjects);
std::vector<FaceResultObject> FindFrequentFaces(const std::deque<std::vector<FaceResultObject>>& faceQueue,
const std::vector<FaceResultObject>& detectedObjects, int occurrenceThreshold = 8);
protected:
bool _licenseValid;
std::string _licenseKey;
std::string _detectorFilePath;
std::string _recognizerFilePath;
std::string _recognizerModelFolder;
std::string _databaseFilePath;
int _precisionType;
std::map<std::string, std::string> _userDict;
ANSCENTER::FRConfig _config;
std::unique_ptr<FaceDatabase> _db =std::make_unique<FaceDatabase>();
EngineType engineType;
bool _enableAgeGender;
bool _enableFaceEmotions;
bool _enableFaceLandmarks;
bool _enableHeadPose;
bool _enableAntiSpoof;
bool _enableFaceliveness;
bool _removeFakeFaces;
bool _isInitialized{false};
int _minFaceSize;
int _queueSize; // number of frames to store in the queue
int _faceThresholdSize; // number of frames to consider for face recognition
bool _enableFaceQueue;
double _faceDetectorScoreThreshold; // Face threshold score for face checker
int _attributeInterval{ 5 }; // Run slow attributes every N frames (1=every frame)
FaceChecker faceChecker;
// Add fine-grained mutexes for shared resources
std::mutex _detectionMutex; // Protects face detector
std::mutex _recognitionMutex; // Protects face recognizer (Feature/Forward inference only)
std::mutex _databaseMutex; // For database operations
// Double-buffer synchronization: protects atomic swap of recognizer index + userDict
// shared_lock for inference (concurrent reads), unique_lock for reload (exclusive write)
mutable std::shared_mutex _indexSwapMutex;
// Reload guard: prevents redundant index rebuilds.
// Set to true after any DB mutation; cleared by a successful Reload().
// If third-party code calls Reload() after a mutation that already auto-reloaded,
// the flag will be false and Reload() returns immediately (no-op).
std::atomic<bool> _reloadNeeded{ true };
std::mutex _ageGenderMutex; // For age/gender detector
std::mutex _emotionsMutex; // For emotions detector
std::mutex _antispoofMutex; // For antispoof detector
std::mutex _headPoseMutex; // For head pose detector
std::mutex _loggerMutex; // Protects logger (if not thread-safe)
std::mutex _faceQueueMutex; // NEW: Protects face queue operations
std::mutex _configMutex; // For configuration
std::mutex _cameraMutex; // For camera data
//std::unordered_map<std::string, std::deque<std::vector<FaceResultObject>>> _cameraFaceQueues;
std::recursive_mutex _mutex;
ANSCENTER::SPDLogger& _logger = ANSCENTER::SPDLogger::GetInstance("ANSFR");
std::shared_ptr<ov::Core> core; // Persistent OpenVINO core instance
std::unique_ptr<ANSFRBase>_recognizer = nullptr;
std::unique_ptr<ANSFDBase>_detector = nullptr;
std::unique_ptr<AntispoofingClassifier>_antiSpoofDetector = nullptr;
std::unique_ptr<AgeGenderDetection>_ageGenderDetector = nullptr;
std::unique_ptr<HeadPoseDetection>_headPoseDetector = nullptr;
std::unique_ptr<EmotionsDetection>_emotionsDetector = nullptr;
const size_t QUEUE_SIZE = 10;
const size_t FACE_THRESHOLD_SIZE = 5;
// Cached attribute JSON per tracked face (everything except head pose).
// Keyed by trackId from ANSMOT. Updated every _attributeInterval frames.
// On non-attribute frames, head pose is run fresh and spliced into this JSON.
struct CachedFaceAttributes {
std::string attributeJson; // Full JSON from FaceAttributeToJsonString
bool isUnknown = false; // Whether face was marked unknown (extreme pose etc.)
};
struct CameraData
{
std::deque<std::vector<FaceResultObject>> _detectionQueue; // That stores the detection results
int attributeFrameCounter = 0; // counts frames for attribute skip logic
std::unordered_map<int, CachedFaceAttributes> cachedAttributes; // trackId → cached attrs
// Adaptive interval state
int currentAttributeInterval = 5; // current adaptive interval
int stableFrameCount = 0; // frames with no new/lost trackIds
int previousTrackIdCount = 0; // trackId count from last frame
void clear()
{
for (auto& detectionVector : _detectionQueue)
{
detectionVector.clear();
}
_detectionQueue.clear();
attributeFrameCounter = 0;
cachedAttributes.clear();
currentAttributeInterval = 5;
stableFrameCount = 0;
previousTrackIdCount = 0;
}
};
std::unordered_map<std::string, CameraData> _cameras;
CameraData& GetCameraData(const std::string& cameraId) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
// Use try_emplace to insert a default CameraData if cameraId does not exist
auto [iterator, inserted] = _cameras.try_emplace(cameraId, CameraData{});
return iterator->second; // Return the reference to CameraData
}
void EnqueueDetection(const std::vector<FaceResultObject>& detectedObjects, const std::string& cameraId);
std::deque<std::vector<FaceResultObject>> DequeueDetection(const std::string& cameraId);
std::vector<FaceResultObject> PostProcess(const std::vector<FaceResultObject>& detectedObjects, const std::string& cameraId);
std::vector<FaceResultObject> BuildFaceResultObjects(const std::vector<Object>& validFaces, const cv::Mat& originalInput, const std::string& camera_id);
void ProcessFaceAttributes(Face& face, const cv::Mat& mask);
// Validation helper methods
bool ValidateInitialization();
bool ValidateInput(const cv::Mat& input);
bool ValidateComponents();
// Frame preparation
cv::Mat PrepareInputFrame(const cv::Mat& input);
// Pipeline stage methods with timing
std::vector<Object> DetectFaces(const cv::Mat& frame, const std::string& camera_id);
std::vector<FaceResultObject> RecognizeFaces(const cv::Mat& frame, const std::vector<Object>& validFaces);
};
};
extern "C" ANSFR_API int CreateANSRFHandle(ANSCENTER::ANSFacialRecognition * *Handle,
const char* licenseKey,
const char* configFilePath,
const char* databaseFilePath,
const char* recogniserFilePath,
const char* detectorFilePath,
int precisionType=0,
float knownPersonThreshold=0.35,
int enableAgeGender=1,
int enableFaceEmotions=1,
int enableHeadPose=1,
int minFaceSize=30,
float faceDetectorThreshold=0.5,
int enableFaceLiveness=1,
int enableAntiSpoofing=1);
extern "C" ANSFR_API int LoadANSRFEngine(ANSCENTER::ANSFacialRecognition * *Handle);
extern "C" ANSFR_API int ReleaseANSRFHandle(ANSCENTER::ANSFacialRecognition * *Handle);
extern "C" ANSFR_API std::string RunANSRFDetector(ANSCENTER::ANSFacialRecognition * *Handle, unsigned char* jpeg_string, unsigned int bufferLength);
extern "C" ANSFR_API std::string RunANSRFDetectorBinary(ANSCENTER::ANSFacialRecognition * *Handle, unsigned char* jpeg_bytes, unsigned int width, unsigned int height);
extern "C" ANSFR_API std::string RunANSRFInference(ANSCENTER::ANSFacialRecognition * *Handle, unsigned char* jpeg_string, unsigned int bufferLength);
extern "C" ANSFR_API std::string RunANSRFInferenceBinary(ANSCENTER::ANSFacialRecognition * *Handle, unsigned char* jpeg_bytes, unsigned int width, unsigned int height);
extern "C" ANSFR_API std::string RunANSRFRecognition(ANSCENTER::ANSFacialRecognition * *Handle, unsigned char* jpeg_string, unsigned int bufferLength);
extern "C" ANSFR_API std::string RunANSRFRecognitionBinary(ANSCENTER::ANSFacialRecognition * *Handle, unsigned char* jpeg_bytes, unsigned int width, unsigned int height);
extern "C" ANSFR_API std::string RunANSRFFaceDetector(ANSCENTER::ANSFacialRecognition** Handle, unsigned char* jpeg_bytes, unsigned int width, unsigned int height);
//// For LabVIEW API
extern "C" ANSFR_API int RunDetector_LV(ANSCENTER::ANSFacialRecognition * *Handle, unsigned char* jpeg_string, unsigned int bufferLength, LStrHandle detectionResult);
extern "C" ANSFR_API int RunInference_LV(ANSCENTER::ANSFacialRecognition * *Handle, unsigned char* jpeg_string, unsigned int bufferLength, LStrHandle detectionResult);
extern "C" ANSFR_API int RunRecognition_LV(ANSCENTER::ANSFacialRecognition * *Handle, unsigned char* jpeg_string, unsigned int bufferLength, LStrHandle detectionResult);
extern "C" ANSFR_API int RunDetectorWithCamId_LV(ANSCENTER::ANSFacialRecognition** Handle, unsigned char* jpeg_string, unsigned int bufferLength, const char* cameraId,LStrHandle detectionResult);
extern "C" ANSFR_API int RunInferenceWithCamId_LV(ANSCENTER::ANSFacialRecognition** Handle, unsigned char* jpeg_string, unsigned int bufferLength, const char* cameraId,LStrHandle detectionResult);
extern "C" ANSFR_API int RunRecognitionWithCamId_LV(ANSCENTER::ANSFacialRecognition** Handle, unsigned char* jpeg_string, unsigned int bufferLength, const char* cameraId,LStrHandle detectionResult);
extern "C" ANSFR_API int RunFaceDetection_LV(ANSCENTER::ANSFacialRecognition** Handle, unsigned char* jpeg_string, unsigned int bufferLength, const char* cameraId, LStrHandle detectionResult);
extern "C" ANSFR_API int RunInferenceComplete_LV(ANSCENTER::ANSFacialRecognition** Handle, cv::Mat** cvImage, const char* cameraId, int getJpegString, int jpegImageSize, LStrHandle detectionResult, LStrHandle imageStr);
extern "C" ANSFR_API int RunFaceDetectionComplete_LV(ANSCENTER::ANSFacialRecognition** Handle, cv::Mat** cvImage, const char* cameraId, int getJpegString, int jpegImageSize,LStrHandle detectionResult, LStrHandle imageStr);
extern "C" ANSFR_API int RunFaceRecogniserComplete_LV(ANSCENTER::ANSFacialRecognition** Handle, cv::Mat** cvImage, const char* cameraId, int getJpegString, int jpegImageSize,LStrHandle detectionResult, LStrHandle imageStr);
// User management APIs
extern "C" ANSFR_API int InsertUser(ANSCENTER::ANSFacialRecognition * *Handle, const char* userCode, const char* userName);
extern "C" ANSFR_API int UpdateUser(ANSCENTER::ANSFacialRecognition * *Handle, int userId, const char* userCode, const char* userName);
extern "C" ANSFR_API int DeleteUser(ANSCENTER::ANSFacialRecognition * *Handle, int userId);
extern "C" ANSFR_API int DeleteUsers(ANSCENTER::ANSFacialRecognition * *Handle, int* userIds, int count);
extern "C" ANSFR_API int InsertFace(ANSCENTER::ANSFacialRecognition * *Handle, int userId, unsigned char* jpeg_string, unsigned int bufferLength);
extern "C" ANSFR_API int InsertFaces(ANSCENTER::ANSFacialRecognition** Handle, int userId, unsigned char* jpeg_string, unsigned int bufferLength, LStrHandle faceIds);
extern "C" ANSFR_API int CheckFaceEmbedding(ANSCENTER::ANSFacialRecognition** Handle, unsigned char* jpeg_string, unsigned int bufferLength);
extern "C" ANSFR_API int InsertFaceBinary(ANSCENTER::ANSFacialRecognition * *Handle, int userId, unsigned char* jpeg_bytes, unsigned int width, unsigned int height);
extern "C" ANSFR_API int DeleteFace(ANSCENTER::ANSFacialRecognition * *Handle, int faceId); // How to we know the faceId?
extern "C" ANSFR_API int Reload(ANSCENTER::ANSFacialRecognition * *Handle);
extern "C" ANSFR_API int UpdateParameters(ANSCENTER::ANSFacialRecognition * *Handle, float knownPersonThreshold, int enableAgeGender, int enableFaceEmotions, int enableHeadPose, int minFaceSize, float faceDetectorThreshold, int faceliveness, int antiSpoof, int removeFakeFaces);
extern "C" ANSFR_API int GetParamters(ANSCENTER::ANSFacialRecognition** Handle, LStrHandle faceParams);
extern "C" ANSFR_API int UpdateFaceQueue(ANSCENTER::ANSFacialRecognition** Handle, int queueSize, int numKnownFaceInQueue, int enableFaceQueue);
extern "C" ANSFR_API int GetFaceQueue(ANSCENTER::ANSFacialRecognition** Handle, LStrHandle faceQueue);
extern "C" ANSFR_API int GetUserString(ANSCENTER::ANSFacialRecognition * *Handle, int userId, std::string& userRecord);
extern "C" ANSFR_API int GetUsersString(ANSCENTER::ANSFacialRecognition * *Handle, std::string & userRecords, std::vector<int>& userIds);
extern "C" ANSFR_API int GetFaceString(ANSCENTER::ANSFacialRecognition * *Handle, int faceId, std::string & faceRecord);
extern "C" ANSFR_API int GetFacesString(ANSCENTER::ANSFacialRecognition * *Handle, int userId, std::string & faceRecords);
// For LabVIEW
extern "C" ANSFR_API int GetUser(ANSCENTER::ANSFacialRecognition * *Handle,int userId, LStrHandle userRecord);
extern "C" ANSFR_API int GetUsers(ANSCENTER::ANSFacialRecognition * *Handle,LStrHandle userRecords);
extern "C" ANSFR_API int GetFace(ANSCENTER::ANSFacialRecognition * *Handle,int faceId, LStrHandle faceRecord);
extern "C" ANSFR_API int GetFaces(ANSCENTER::ANSFacialRecognition * *Handle,int userId, LStrHandle faceRecords);
extern "C" ANSFR_API int DeleteFacesByUser(ANSCENTER::ANSFacialRecognition * *Handle,int userId);
extern "C" ANSFR_API double BlurCalculation(unsigned char* jpeg_string, unsigned int bufferLength);
#endif
File diff suppressed because it is too large Load Diff
+370
View File
@@ -0,0 +1,370 @@
#ifndef ANSFRCOMMON_H
#define ANSFRCOMMON_H
#define ANSF_API __declspec(dllexport)
#pragma once
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include "NvInfer.h"
//#include <cublasLt.h>
#include "ANSLicense.h"
#include "LabVIEWHeader/extcode.h"
#include "Utility.h"
#include "macros.h"
#include "ANSEngineCommon.h"
#include "openvino/openvino.hpp"
using namespace nvinfer1;
#define CUDACHECK(status) \
do\
{\
auto ret = (status);\
if (ret != 0)\
{\
std::cerr << "Cuda failure: " << ret << std::endl;\
abort();\
}\
} while (0)
namespace ANSCENTER {
struct UserRecord {
int UserId;
std::string UserCode;
std::string UserName;
std::vector<int> FaceIds;
};
struct FaceRecord {
int FaceId;
int UserId;
std::string ImagePath;
};
struct alignas(float) Detection {
float bbox[4]; //x1 y1 x2 y2
float class_confidence;
float landmark[10];
};
struct CroppedFace {
cv::Mat face;
cv::Mat faceMat;
int x1, y1, x2, y2;
};
struct Bbox {
int x1, y1, x2, y2;
float score;
};
struct Paths {
std::string absPath;
std::string className;
};
struct AnchorBox {
float cx;
float cy;
float sx;
float sy;
};
struct FRConfig {
std::string _databasePath;
int _videoFrameWidth;
int _videoFrameHeight;
std::string _detEngineFile;
std::vector<int> _detInputShape;
std::string _detInputName;
std::vector<std::string> _detOutputNames;
int _detMaxBatchSize;
float _detThresholdNMS;
float _detThresholdBbox;
std::vector<int> _recInputShape;
int _recOutputDim;
std::string _recEngineFile;
int _maxFacesPerScene;
float _knownPersonThreshold;
int _recMaxBatchSize;
std::string _recInputName;
std::string _recOutputName;
bool _gen;
std::string _gen_imgSource;
bool _gen_imgIsCropped;
bool _apiImageIsCropped;
};
struct HeadPoseResults {
float angle_r;
float angle_p;
float angle_y;
};
struct AgeGenderResults {
float age;
float maleProb;
};
//// Class for face attribute
class ANSF_API Face {
public:
//using Ptr = std::shared_ptr<Face>;
explicit Face(size_t id, cv::Rect& location);
void updateAge(float value);
void updateGender(float value);
void updateEmotions(std::map<std::string, float> values);
void updateHeadPose(HeadPoseResults values);
void updateLandmarks(std::vector<float> values);
void updateRealFaceConfidence(float value);
void updateFaceLiveness(int value);
int getAge();
bool isMale();
bool isReal();
float getAntispoofingScore();
int getFaceLiveness();
std::map<std::string, float> getEmotions();
std::pair<std::string, float> getMainEmotion();
HeadPoseResults getHeadPose();
const std::vector<float>& getLandmarks();
size_t getId();
void ageGenderEnable(bool value);
void emotionsEnable(bool value);
void headPoseEnable(bool value);
void landmarksEnable(bool value);
void antispoofingEnable(bool value);
void faceLivenessEnable(bool value);
bool isAgeGenderEnabled();
bool isEmotionsEnabled();
bool isHeadPoseEnabled();
bool isLandmarksEnabled();
bool isAntispoofingEnabled();
bool isFaceLivenessEnabled();
public:
cv::Rect _location;
float _intensity_mean;
private:
size_t _id;
float _age;
float _maleScore;
float _femaleScore;
std::map<std::string, float> _emotions;
HeadPoseResults _headPose;
std::vector<float> _landmarks;
float _realFaceConfidence;
int _faceLiveness;
bool _isAgeGenderEnabled;
bool _isEmotionsEnabled;
bool _isHeadPoseEnabled;
bool _isLandmarksEnabled;
bool _isAntispoofingEnabled;
bool _isFaceLivenessEnabled;
};
class ANSF_API BaseDetection {
public:
BaseDetection(const std::string& pathToModel, bool doRawOutputMessages);
virtual ~BaseDetection() = default;
virtual std::shared_ptr<ov::Model> read(const ov::Core& core) = 0;
bool enabled() const;
ov::InferRequest request;
ov::Tensor inTensor;
std::string pathToModel;
ov::Shape inShape;
const bool doRawOutputMessages;
};
class ANSF_API HeadPoseDetection :public BaseDetection {
public:
std::recursive_mutex _mutex;
std::string outputAngleR;
std::string outputAngleP;
std::string outputAngleY;
std::string _modelFilePath;
//size_t enquedFaces;
cv::Mat cameraMatrix;
HeadPoseDetection(const std::string& pathToModel, bool doRawOutputMessages);
virtual std::shared_ptr<ov::Model> read(const ov::Core& core) override;
HeadPoseResults runInfer(const cv::Mat& frame);
//void submitRequest();
//void enqueue(const cv::Mat& face);
//HeadPoseResults getResult(int idx);
//std::vector<HeadPoseResults> getAllResults();
};
class ANSF_API AgeGenderDetection :public BaseDetection {
public:
std::recursive_mutex _mutex;
std::string outputAge;
std::string outputGender;
//size_t enquedFaces;
std::string _modelFilePath;
AgeGenderDetection(const std::string& pathToModel,
bool doRawOutputMessages);
virtual std::shared_ptr<ov::Model> read(const ov::Core& core) override;
AgeGenderResults runInfer(const cv::Mat& frame);
//void submitRequest();
//void enqueue(const cv::Mat& face);
//AgeGenderResults getResult(int idx);
//std::vector<AgeGenderResults> getAllResults();
};
class ANSF_API EmotionsDetection : public BaseDetection {
public:
std::recursive_mutex _mutex;
//size_t enquedFaces;
std::string _modelFilePath;
const std::vector<std::string> emotionsVec = { "neutral", "happy", "sad", "surprise", "anger" };
EmotionsDetection(const std::string& pathToModel,
bool doRawOutputMessages);
virtual std::shared_ptr<ov::Model> read(const ov::Core& core) override;
std::map<std::string, float> runInfer(const cv::Mat& frame);
//void submitRequest();
//void enqueue(const cv::Mat& face);
//std::map<std::string, float> getResult(int idx);
//std::vector<std::map<std::string, float>> getAllResults();
};
class FacialLandmarksDetection : public BaseDetection {
public:
//size_t enquedFaces;
std::vector<std::vector<float>> landmarks_results;
std::vector<cv::Rect> faces_bounding_boxes;
std::string _modelFilePath;
std::recursive_mutex _mutex;
FacialLandmarksDetection(const std::string& pathToModel,
bool doRawOutputMessages);
virtual std::shared_ptr<ov::Model> read(const ov::Core& core) override;
std::vector<float> runInfer(const cv::Mat& frame);
//void submitRequest();
//void enqueue(const cv::Mat& face);
//std::vector<float> getResult(int idx);
};
class ANSF_API AntispoofingClassifier : public BaseDetection {
public:
std::recursive_mutex _mutex;
//size_t enquedFaces;
std::string _modelFilePath;
AntispoofingClassifier(const std::string& pathToModel,bool doRawOutputMessages);
virtual std::shared_ptr<ov::Model> read(const ov::Core& core) override;
float runInfer(const cv::Mat& frame);
//void submitRequest();
//void enqueue(const cv::Mat& frame);
//float getResult(int idx);
};
class ANSFRHelper {
public:
static unsigned char* CVMatToBytes(cv::Mat image, unsigned int& bufferLengh);
static void GetCroppedFaces(const cv::Mat& input, std::vector<Object>& outputBbox, int resize_w, int resize_h, std::vector<struct CroppedFace>& croppedFaces);
static void GetFilePaths(std::string rootPath, std::vector<struct Paths>& paths);
static bool LoadConfigFile(std::string configFile, FRConfig& config);
static std::string StringCurrentDateTime(); // Return current datetime in string format
static std::string FaceObjectsToJsonString(const std::vector<FaceResultObject>& faces);
static cv::Mat PreprocessImg(cv::Mat& img, int inputW, int inputH);
static int ReadFilesInDir(const char* pDirName, std::vector<std::string>& fileNames);
static cv::Rect GetRectAdaptLandmark(cv::Mat& img, int inputW, int inputH, float bBox[4], float lmk[10]);
static float IOU(float lbox[4], float rbox[4]);
static bool CMP(const Detection& a, const Detection& b);
static void NMS(std::vector<Detection>& res, float* output, float nms_thresh = 0.4);
static std::map<std::string, Weights> LoadWeights(const std::string file);
static Weights GetWeights(std::map<std::string, Weights>& weightMap, std::string key);
static IScaleLayer* AddBatchNorm2D(INetworkDefinition* network, std::map<std::string, Weights>& weightMap, ITensor& input, std::string lName, float eps);
static std::string UserRecordToJsonString(const UserRecord userRecord);
static std::string UserRecordsToJsonString(const std::vector<UserRecord> userRecords);
static std::string FaceRecordToJsonString(const FaceRecord faceRecord);
static std::string FaceRecordsToJsonString(const std::vector<FaceRecord> faceRecords);
static std::string FaceAttributeToJsonString(Face face);
};
float calcIoU(cv::Rect& src, cv::Rect& dst);
float calcMean(const cv::Mat& src);
Face matchFace(cv::Rect rect, std::list<Face>& faces);
// Class to extend TensorRT logger
/* class TRTLogger : public nvinfer1::ILogger {
public:
void log(Severity severity, const char* msg) noexcept override {
if (severity <= Severity::kWARNING) {
std::cout << "TENSORRT_ENGINE:" << msg << std::endl;
}
}
void LogDebug(const std::string source, std::string message) {
std::cout << source << ":" << message << std::endl;
}
void LogInfo(const std::string source, std::string message) {
std::cout << source << ":" << message << std::endl;
}
void LogError(const std::string source, std::string message) {
std::cout << source << ":" << message << std::endl;
}
void LogFatal(const std::string source, std::string message) {
std::cout << source << ":" << message << std::endl;
}
~TRTLogger() {
}
};
class MatMul {
public:
MatMul();
~MatMul();
void Init(float* knownEmbeds, int numRow, int numCol);
void Calculate(float* embeds, int embedCount, float* outputs);
private:
cudaDataType_t cudaDataType = CUDA_R_32F;
cublasComputeType_t computeType = CUBLAS_COMPUTE_32F;
cublasLtHandle_t ltHandle;
cublasOperation_t transa = CUBLAS_OP_T;
cublasOperation_t transb = CUBLAS_OP_N;
void* workspace;
const size_t workspaceSize = 1024 * 1024 * 4;
cudaStream_t stream;
float* dA, * dB, * dC;
const float alpha = 1, beta = 0;
int m, n, k, lda, ldb, ldc;
cublasLtMatmulDesc_t operationDesc = NULL;
cublasLtMatrixLayout_t Adesc = NULL;
cublasLtMatmulPreference_t preference = NULL;
};
class Int8EntropyCalibrator2 : public nvinfer1::IInt8EntropyCalibrator2
{
public:
Int8EntropyCalibrator2(int batchsize, int input_w, int input_h, const char* img_dir, const char* calib_table_name, const char* input_blob_name, bool read_cache = true);
virtual ~Int8EntropyCalibrator2();
int getBatchSize() const TRT_NOEXCEPT override;
bool getBatch(void* bindings[], const char* names[], int nbBindings) TRT_NOEXCEPT override;
const void* readCalibrationCache(size_t& length) TRT_NOEXCEPT override;
void writeCalibrationCache(const void* cache, size_t length) TRT_NOEXCEPT override;
private:
int batchsize_;
int input_w_;
int input_h_;
int img_idx_;
std::string img_dir_;
std::vector<std::string> img_files_;
size_t input_count_;
std::string calib_table_name_;
const char* input_blob_name_;
bool read_cache_;
void* device_input_;
std::vector<char> calib_cache_;
};*/
}
#endif
File diff suppressed because it is too large Load Diff
+130
View File
@@ -0,0 +1,130 @@
#ifndef ANSFACERECOGNISER_H
#define ANSFACERECOGNISER_H
#pragma once
#include "ANSFRCommon.h"
#include "hnswlib/hnswlib.h"
#include "cnn.hpp"
#include "face_reid.hpp"
#include "openvino/openvino.hpp"
#include <faiss/IndexFlat.h>
#include <faiss/IndexIDMap.h>
#include <faiss/gpu/GpuIndexFlat.h>
#include <faiss/gpu/StandardGpuResources.h>
#include <unordered_map>
#include "engine.h"
#include "engine/EnginePoolManager.h"
#include "ONNXEngine.h"
#define USE_ONNX_ENGINE
//#define CPU_MODE
//#define USE_CPU_BATCH_MODE
namespace ANSCENTER {
class ANSFaceRecognizer : public ANSFRBase {
public:
virtual bool Initialize(std::string licenseKey,
ModelConfig modelConfig,
const std::string& modelZipFilePath,
const std::string& modelZipPassword,
std::string& labelMap) override;
virtual bool LoadModel(const std::string& modelZipFilePath,
const std::string& modelZipPassword) override;
bool OptimizeModel(bool fp16, std::string& optimizedModelFolder);
// Single face feature
std::vector<float> Feature(const cv::Mat& image,const ANSCENTER::Object& bBox);
// Full pipeline: embeddings -> FAISS search -> results
std::vector<FaceResultObject> Match(const cv::Mat& input, const std::vector<ANSCENTER::Object>& bBox, const std::map<std::string, std::string>& userDict);
cv::Mat GetCropFace(const cv::Mat& input, const ANSCENTER::Object& bBox);
void Init();
void AddEmbedding(const std::string& className, float embedding[]);
void AddEmbedding(const std::string& className, const std::vector<float>& embedding);
// Double-buffer support: atomically swap FAISS index + mapping
void SwapIndex(std::shared_ptr<faiss::IndexIDMap> newIndex,
std::unordered_map<faiss::idx_t, std::string>&& newFaceIdToUserId);
int GetEmbeddingSize() const { return FACE_EMBEDDING_SIZE; }
std::shared_ptr<faiss::gpu::StandardGpuResources> GetGpuResources() const { return m_gpuResources; }
bool UpdateParamater(double knownPersonThreshold) {
_modelConfig.unknownPersonThreshold = knownPersonThreshold;
m_knownPersonThresh = _modelConfig.unknownPersonThreshold;
return true;
}
~ANSFaceRecognizer();
bool Destroy();
// L2-normalize a vector in-place (public — used by ANSFR::Reload)
static void L2NormalizeInPlace(std::vector<float>& vec);
private:
bool LoadEngine(const std::string& xmlModelPath, bool engineOptimisation = true);
// Batched forward: one embedding per Object.mask (caller must hold _mutex)
std::vector<std::vector<float>> ForwardUnlocked(const cv::Mat& input,const std::vector<ANSCENTER::Object>& outputBbox);
// FAISS search (caller must hold _mutex)
std::tuple<std::vector<std::string>, std::vector<float>>
SearchForFacesUnlocked(const std::vector<std::vector<float>>& detectedEmbeddings);
std::string GetOpenVINODevice();
// Single-face GPU inference
std::vector<float> RunArcFace(const cv::Mat& input);
// Batched GPU inference
std::vector<std::vector<float>> RunArcFaceBatch(
const std::vector<cv::Mat>& faceROIs,
const std::vector<cv::cuda::GpuMat>& gpuFaceROIs = {});
protected:
const int GPU_FACE_WIDTH = 112;
const int GPU_FACE_HEIGHT = 112;
const int FACE_EMBEDDING_SIZE = 512;
std::unordered_map<faiss::idx_t, std::string> _faceIdToUserId;
faiss::idx_t _nextFaceId = 0; // Sequential ID counter for backward-compat AddEmbedding
ModelConfig _modelConfig;
std::string _modelFilePath;
std::string _landmarkModelFilePath;
ANSCENTER::Options m_options;
const std::array<float, 3> SUB_VALS{ 0.5f, 0.5f, 0.5f };
const std::array<float, 3> DIV_VALS{ 0.5f, 0.5f, 0.5f };
const bool NORMALIZE = true;
std::recursive_mutex _mutex;
float m_knownPersonThresh = 0.35f;
EngineType engineType;
#ifdef USE_ONNX_ENGINE
std::unique_ptr<GlintArcFace> faceRecognizer = nullptr;
const int CPU_FACE_WIDTH = 112;
const int CPU_FACE_HEIGHT = 112;
#else
std::unique_ptr<VectorCNN> faceRecognizer = nullptr; // OpenVINO
const int CPU_FACE_WIDTH = 160;
const int CPU_FACE_HEIGHT = 160;
#endif
// Pooled GPU buffers to avoid per-frame allocation (Fix #8)
cv::cuda::Stream m_gpuStream;
cv::cuda::GpuMat m_gpuImg;
cv::cuda::GpuMat m_gpuResized;
cv::cuda::GpuMat m_gpuRgb;
std::shared_ptr<Engine<float>> m_trtEngine = nullptr; // NVIDIA TensorRT
EnginePoolManager<float>::PoolKey m_poolKey;
bool m_usingSharedPool = false;
int m_maxSlotsPerGpu{ -1 }; // -1 = elastic mode (on-demand slots, auto-cleanup)
void SetMaxSlotsPerGpu(int n) override { m_maxSlotsPerGpu = n; }
std::shared_ptr<faiss::IndexIDMap> faiss_index;
std::shared_ptr<faiss::gpu::StandardGpuResources> m_gpuResources;
};
}
#endif
+23
View File
@@ -0,0 +1,23 @@
// ANSGpuFrameRegistry.cpp — Cross-DLL singleton resolver for ANSFR.dll.
//
// Finds the canonical ANSGpuFrameRegistry instance exported by ANSCV.dll
// via GetProcAddress. No link dependency on ANSCV.lib needed.
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include "ANSGpuFrameRegistry.h"
ANSGpuFrameRegistry* ANSGpuFrameRegistry::resolveProcessWide() {
// ANSCV.dll is always loaded before inference starts (it provides RTSP).
HMODULE hMod = GetModuleHandleA("ANSCV.dll");
if (hMod) {
typedef ANSGpuFrameRegistry* (*GetInstanceFn)();
auto fn = reinterpret_cast<GetInstanceFn>(
GetProcAddress(hMod, "ANSGpuFrameRegistry_GetInstance"));
if (fn) return fn();
}
// Fallback: local instance (unit tests without ANSCV.dll).
static ANSGpuFrameRegistry local;
return &local;
}
+692
View File
@@ -0,0 +1,692 @@
#include "ARCFaceRT.h"
#include "NvOnnxParser.h"
namespace ANSCENTER {
bool ArcFace::Initialize(std::string licenseKey,
ModelConfig modelConfig,
const std::string& modelZipFilePath,
const std::string& modelZipPassword,
std::string& labelMap) {
bool result = ANSFRBase::Initialize(licenseKey, modelConfig, modelZipFilePath, modelZipPassword, labelMap);
if (!result) return false;
try {
_modelConfig = modelConfig;
_modelConfig.modelType = ModelType::FACERECOGNIZE;
_modelConfig.detectionType = DetectionType::FACERECOGNIZER;
m_knownPersonThresh = _modelConfig.unknownPersonThreshold;
if (m_knownPersonThresh == 0.0f) m_knownPersonThresh = 0.35f;
std::string onnxfile50 = CreateFilePath(_modelFolder, "ansfacerecognizer50.onnx");
if (std::filesystem::exists(onnxfile50)) {
_modelFilePath = onnxfile50;
_logger.LogDebug("ArcFace::Initialize. Loading arcface50 weight", _modelFilePath, __FILE__, __LINE__);
}
else {
std::string onnxfile = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx");
if (std::filesystem::exists(onnxfile)) {
_modelFilePath = onnxfile;
_logger.LogDebug("ArcFace::Initialize. Loading arcface weight", _modelFilePath, __FILE__, __LINE__);
}
else {
_logger.LogError("ArcFace::Initialize. Model arcface.onnx file does not exist", _modelFilePath, __FILE__, __LINE__);
return false;
}
}
// Configure engine with batch support
m_options.precision = ANSCENTER::Precision::FP32;
m_options.optBatchSize = 8; // expected typical batch
m_options.maxBatchSize = 32; // maximum number of faces per frame you want
m_options.calibrationBatchSize = 8;
m_options.deviceIndex = 0;
m_trtEngine.UpdateOptions(m_options);
if (FileExist(_modelFilePath)) {
bool succ = m_trtEngine.buildLoadNetwork(_modelFilePath, SUB_VALS, DIV_VALS, NORMALIZE);
if (!succ) {
_logger.LogError("ArcFace::Initialize. Unable to build or load TensorRT engine.",
_modelFilePath, __FILE__, __LINE__);
return false;
}
}
else {
_logger.LogError("ArcFace::Initialize. Model file does not exist",
_modelFilePath, __FILE__, __LINE__);
return false;
}
Init();
_isInitialized = true;
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ArcFace::Initialize", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ArcFace::LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword) {
try {
bool result = ANSFRBase::LoadModel(modelZipFilePath, modelZipPassword);
if (!result) return false;
std::string onnxfile50 = CreateFilePath(_modelFolder, "ansfacerecognizer50.onnx");
if (std::filesystem::exists(onnxfile50)) {
_modelFilePath = onnxfile50;
_logger.LogDebug("ArcFace::LoadModel. Loading arcface50 weight", _modelFilePath, __FILE__, __LINE__);
}
else {
std::string onnxfile = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx");
if (std::filesystem::exists(onnxfile)) {
_modelFilePath = onnxfile;
_logger.LogDebug("ArcFace::LoadModel. Loading arcface weight", _modelFilePath, __FILE__, __LINE__);
}
else {
_logger.LogError("ArcFace::LoadModel. Model arcface.onnx file does not exist",
_modelFilePath, __FILE__, __LINE__);
return false;
}
}
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ArcFace50::LoadModel", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ArcFace::OptimizeModel(bool fp16, std::string& optimizedModelFolder) {
if (!FileExist(_modelFilePath)) {
optimizedModelFolder = "";
return false;
}
optimizedModelFolder = GetParentFolder(_modelFilePath);
m_options.optBatchSize = 8;
m_options.maxBatchSize = 32;
m_options.engineFileDir = optimizedModelFolder;
m_options.precision = Precision::FP32;
Engine<float> engine(m_options);
auto succ = engine.buildWithRetry(_modelFilePath, SUB_VALS, DIV_VALS, NORMALIZE);
if (!succ) {
const std::string errMsg =
"Error: Unable to build the TensorRT engine. Try increasing TensorRT log severity to kVERBOSE.";
_logger.LogError("ArcFace::OptimizeModel", errMsg, __FILE__, __LINE__);
return false;
}
return true;
}
std::vector<float> ArcFace::Feature(const cv::Mat& image, const ANSCENTER::Object& bBox) {
std::vector<float> embedding;
// Early validation before locking
if (image.empty()) {
return embedding;
}
if (image.cols < 10 || image.rows < 10) {
return embedding;
}
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
return RunArcFace(bBox.mask);
}
catch (const std::exception& e) {
_logger.LogFatal("ArcFace::Feature", e.what(), __FILE__, __LINE__);
return std::vector<float>();
}
}
std::vector<FaceResultObject> ArcFace::Match(
const cv::Mat& input,
const std::vector<ANSCENTER::Object>& bBox,
const std::map<std::string, std::string>& userDict) {
std::vector<FaceResultObject> resultObjects;
// Early validation before locking
if (input.empty()) {
return resultObjects;
}
if (input.cols < 10 || input.rows < 10) {
return resultObjects;
}
std::lock_guard<std::recursive_mutex> lock(_mutex);
if (!_isInitialized) {
_logger.LogError("ArcFace::Match", "Model is not initialized", __FILE__, __LINE__);
return resultObjects;
}
try {
// Get embeddings
std::vector<std::vector<float>> detectedEmbeddings = Forward(input, bBox);
// Search for matches
std::vector<std::string> names;
std::vector<float> sims;
std::tie(names, sims) = SearchForFaces(detectedEmbeddings);
if (names.empty()) {
_logger.LogError("ArcFace::Match", "No face is match", __FILE__, __LINE__);
return resultObjects;
}
// Pre-reserve result space
const size_t resultCount = std::min(names.size(), bBox.size());
resultObjects.reserve(resultCount);
// Build result objects
for (size_t i = 0; i < resultCount; ++i) {
FaceResultObject resultObject;
// Determine if face is known or unknown
const bool isUnknown = (sims[i] > m_knownPersonThresh);
if (isUnknown) {
resultObject.isUnknown = true;
resultObject.userId = "0";
resultObject.userName = "Unknown";
resultObject.confidence = 1.0f;
}
else {
resultObject.isUnknown = false;
resultObject.userId = names[i];
// Safe map lookup with fallback
auto it = userDict.find(names[i]);
resultObject.userName = (it != userDict.end()) ? it->second : names[i];
resultObject.confidence = 1.0f - sims[i];
}
resultObject.similarity = sims[i];
// Copy bounding box and additional data
resultObject.box = bBox[i].box;
resultObject.mask = bBox[i].mask;
resultObject.cameraId = bBox[i].cameraId;
resultObject.trackId = bBox[i].trackId;
resultObject.polygon = bBox[i].polygon;
resultObject.kps = bBox[i].kps;
resultObject.extraInformation = bBox[i].extraInfo;
resultObjects.push_back(std::move(resultObject));
}
return resultObjects;
}
catch (const std::exception& e) {
_logger.LogFatal("ArcFace::Match", e.what(), __FILE__, __LINE__);
return resultObjects;
}
}
cv::Mat ArcFace::GetCropFace(const cv::Mat& input, const ANSCENTER::Object& bBox) {
try {
std::vector<ANSCENTER::Object> outputBbox;
outputBbox.reserve(1);
outputBbox.push_back(bBox);
std::vector<CroppedFace> crFaces;
ANSFRHelper::GetCroppedFaces(input, outputBbox, 112, 112, crFaces);
if (crFaces.empty()) {
return cv::Mat();
}
return crFaces[0].faceMat;
}
catch (const std::exception& e) {
_logger.LogFatal("ArcFace::GetCropFace", e.what(), __FILE__, __LINE__);
return cv::Mat();
}
}
bool ArcFace::LoadEngine(const std::string onnxModelPath, bool engineOptimisation) {
try {
if (!FileExist(onnxModelPath)) {
_logger.LogError("ArcFace::LoadEngine", "Cannot find the raw ONNX model file.", __FILE__, __LINE__);
return false;
}
m_options.precision = ANSCENTER::Precision::FP32;
m_options.optBatchSize = 8;
m_options.maxBatchSize = 32;
m_options.calibrationBatchSize = 8;
m_options.deviceIndex = 0;
m_trtEngine.UpdateOptions(m_options);
if (FileExist(onnxModelPath)) {
bool succ = m_trtEngine.buildLoadNetwork(onnxModelPath, SUB_VALS, DIV_VALS, NORMALIZE);
if (!succ) {
_logger.LogError("ArcFace::LoadEngine. Unable to build or load TensorRT engine.",
onnxModelPath, __FILE__, __LINE__);
return false;
}
}
else {
_logger.LogError("ArcFace::LoadEngine. Model file does not exist",
onnxModelPath, __FILE__, __LINE__);
return false;
}
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ArcFace::LoadEngine", e.what(), __FILE__, __LINE__);
return false;
}
}
std::vector<float> ArcFace::RunArcFace(const cv::Mat& inputImage) {
std::vector<float> embedding;
// Early validation before locking
if (inputImage.empty()) {
_logger.LogError("ArcFace::RunArcFace", "Input image is empty", __FILE__, __LINE__);
return embedding;
}
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
if (!_isInitialized) {
_logger.LogError("ArcFace::RunArcFace", "Model is not initialized", __FILE__, __LINE__);
return embedding;
}
// GPU preprocessing pipeline
cv::cuda::Stream stream;
cv::cuda::GpuMat d_img;
// Upload to GPU
d_img.upload(inputImage, stream);
// Handle grayscale conversion on GPU
if (inputImage.channels() == 1) {
cv::cuda::GpuMat d_bgr;
cv::cuda::cvtColor(d_img, d_bgr, cv::COLOR_GRAY2BGR, 0, stream);
d_img = d_bgr;
}
// Resize on GPU if needed
if (inputImage.cols != FACE_WIDTH || inputImage.rows != FACE_HEIGHT) {
cv::cuda::GpuMat d_resized;
cv::cuda::resize(d_img, d_resized, cv::Size(FACE_WIDTH, FACE_HEIGHT),
0, 0, cv::INTER_LINEAR, stream);
d_img = d_resized;
}
// BGR to RGB conversion on GPU
cv::cuda::GpuMat d_rgb;
cv::cuda::cvtColor(d_img, d_rgb, cv::COLOR_BGR2RGB, 0, stream);
// Prepare inference inputs
std::vector<cv::cuda::GpuMat> inputVec;
inputVec.emplace_back(std::move(d_rgb));
std::vector<std::vector<cv::cuda::GpuMat>> inputs;
inputs.emplace_back(std::move(inputVec));
// Run inference
std::vector<std::vector<std::vector<float>>> featureVectors;
bool succ = m_trtEngine.runInference(inputs, featureVectors);
stream.waitForCompletion();
if (!succ) {
_logger.LogError("ArcFace::RunArcFace", "Failed to run inference.", __FILE__, __LINE__);
return embedding;
}
if (!featureVectors.empty() && !featureVectors[0].empty()) {
embedding = std::move(featureVectors[0][0]);
}
return embedding;
}
catch (const std::exception& e) {
_logger.LogFatal("ArcFace::RunArcFace", e.what(), __FILE__, __LINE__);
return embedding;
}
}
std::vector<std::vector<float>> ArcFace::RunArcFaceBatch(const std::vector<cv::Mat>& faceROIs) {
std::vector<std::vector<float>> embeddings;
try {
if (!_isInitialized) {
_logger.LogError("ArcFace::RunArcFaceBatch", "Model is not initialized", __FILE__, __LINE__);
return embeddings;
}
if (faceROIs.empty()) {
return embeddings;
}
if (faceROIs.size() > static_cast<size_t>(m_options.maxBatchSize)) {
_logger.LogError("ArcFace::RunArcFaceBatch",
"Batch size exceeds maxBatchSize", __FILE__, __LINE__);
return embeddings;
}
const auto& inputDims = m_trtEngine.getInputDims();
if (inputDims.empty() || inputDims[0].nbDims < 3) {
_logger.LogError("ArcFace::RunArcFaceBatch",
"Invalid engine input dims", __FILE__, __LINE__);
return embeddings;
}
// Pre-reserve embeddings space
embeddings.reserve(faceROIs.size());
// GPU preprocessing pipeline
cv::cuda::Stream stream;
std::vector<cv::cuda::GpuMat> batchGpu;
batchGpu.reserve(faceROIs.size());
const cv::Size targetSize(FACE_WIDTH, FACE_HEIGHT);
for (size_t i = 0; i < faceROIs.size(); ++i) {
const cv::Mat& roi = faceROIs[i];
if (roi.empty()) {
_logger.LogWarn("ArcFace::RunArcFaceBatch",
"Empty ROI at index " + std::to_string(i) + ", skipping",
__FILE__, __LINE__);
continue;
}
// Upload to GPU
cv::cuda::GpuMat d_img;
d_img.upload(roi, stream);
// Handle grayscale conversion on GPU
if (roi.channels() == 1) {
cv::cuda::GpuMat d_bgr;
cv::cuda::cvtColor(d_img, d_bgr, cv::COLOR_GRAY2BGR, 0, stream);
d_img = d_bgr;
}
// Resize on GPU if needed
if (roi.cols != FACE_WIDTH || roi.rows != FACE_HEIGHT) {
cv::cuda::GpuMat d_resized;
cv::cuda::resize(d_img, d_resized, targetSize, 0, 0, cv::INTER_LINEAR, stream);
d_img = d_resized;
}
// BGR to RGB conversion on GPU
cv::cuda::GpuMat d_rgb;
cv::cuda::cvtColor(d_img, d_rgb, cv::COLOR_BGR2RGB, 0, stream);
batchGpu.emplace_back(std::move(d_rgb));
}
if (batchGpu.empty()) {
return embeddings;
}
// Prepare inference inputs
std::vector<std::vector<cv::cuda::GpuMat>> inputs;
inputs.reserve(1);
inputs.emplace_back(std::move(batchGpu));
// Run inference
std::vector<std::vector<std::vector<float>>> featureVectors;
bool succ = m_trtEngine.runInference(inputs, featureVectors);
stream.waitForCompletion();
if (!succ) {
_logger.LogError("ArcFace::RunArcFaceBatch", "runInference failed", __FILE__, __LINE__);
return embeddings;
}
if (featureVectors.empty() || featureVectors[0].empty()) {
_logger.LogError("ArcFace::RunArcFaceBatch", "Empty featureVectors", __FILE__, __LINE__);
return embeddings;
}
embeddings = std::move(featureVectors[0]);
return embeddings;
}
catch (const std::exception& e) {
_logger.LogFatal("ArcFace::RunArcFaceBatch", e.what(), __FILE__, __LINE__);
return embeddings;
}
}
std::vector<std::vector<float>> ArcFace::Forward(const cv::Mat& input,const std::vector<ANSCENTER::Object>& outputBbox)
{
std::vector<std::vector<float>> detectedEmbeddings;
// Early validation before locking
if (input.empty()) {
_logger.LogError("ArcFace::Forward",
"Input image is empty", __FILE__, __LINE__);
return detectedEmbeddings;
}
if (outputBbox.empty()) {
return detectedEmbeddings;
}
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
// Pre-reserve output space
detectedEmbeddings.reserve(outputBbox.size());
// Collect valid face ROIs
std::vector<cv::Mat> faceROIs;
faceROIs.reserve(outputBbox.size());
for (const auto& obj : outputBbox) {
if (!obj.mask.empty()) {
faceROIs.push_back(obj.mask);
}
}
if (faceROIs.empty()) {
return detectedEmbeddings;
}
// Run batch inference
detectedEmbeddings = RunArcFaceBatch(faceROIs);
return detectedEmbeddings;
}
catch (const std::exception& e) {
_logger.LogFatal("ArcFace::Forward", e.what(), __FILE__, __LINE__);
return detectedEmbeddings;
}
}
std::tuple<std::vector<std::string>, std::vector<float>>
ArcFace::SearchForFaces(const std::vector<std::vector<float>>& detectedEmbeddings) {
std::vector<std::string> detectedUsers;
std::vector<float> simValues;
// Early exit before locking
if (detectedEmbeddings.empty()) {
return std::make_tuple(detectedUsers, simValues);
}
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
// Pre-reserve output space
detectedUsers.reserve(detectedEmbeddings.size());
simValues.reserve(detectedEmbeddings.size());
if (!classNames.empty() && faiss_index && faiss_index->ntotal > 0) {
// Determine k based on database size
const int k = std::min(3, static_cast<int>(faiss_index->ntotal));
// Pre-allocate search buffers (reuse across iterations)
std::vector<faiss::idx_t> indices(k);
std::vector<float> distances(k);
std::vector<float> matchEmbedding(faiss_index->d);
for (const auto& embedding : detectedEmbeddings) {
if (embedding.size() != FACE_EMBEDDING_SIZE) {
detectedUsers.push_back("0");
simValues.push_back(1.0f);
continue;
}
// Search FAISS index
faiss_index->search(1, embedding.data(), k,
distances.data(), indices.data());
// Find best match (minimum distance for L2)
auto min_it = std::min_element(distances.begin(), distances.end());
int best_index = static_cast<int>(std::distance(distances.begin(), min_it));
// Validate index
faiss::idx_t id = indices[best_index];
if (id < 0 || id >= static_cast<faiss::idx_t>(classNames.size())) {
detectedUsers.push_back("0");
simValues.push_back(1.0f);
continue;
}
// Reconstruct embedding and compute similarity
faiss_index->reconstruct(id, matchEmbedding.data());
float cosine = CosineSimilarity(embedding, matchEmbedding, false);
float similarity = 1.0f - cosine;
detectedUsers.push_back(classNames[id]);
simValues.push_back(std::abs(similarity));
}
}
else {
detectedUsers.assign(detectedEmbeddings.size(), "0");
simValues.assign(detectedEmbeddings.size(), 1.0f);
}
return std::make_tuple(std::move(detectedUsers), std::move(simValues));
}
catch (const std::exception& e) {
_logger.LogFatal("ArcFace::SearchForFaces", e.what(), __FILE__, __LINE__);
// Return appropriate sized vectors on error
detectedUsers.assign(detectedEmbeddings.size(), "0");
simValues.assign(detectedEmbeddings.size(), 1.0f);
return std::make_tuple(detectedUsers, simValues);
}
}
void ArcFace::Init() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
classNames.clear();
if (faiss_index) {
faiss_index->reset();
}
else {
faiss_index = std::make_unique<faiss::IndexFlatL2>(FACE_EMBEDDING_SIZE);
}
}
catch (const std::exception& e) {
_isInitialized = false;
_logger.LogFatal("ArcFace::Init", e.what(), __FILE__, __LINE__);
}
}
ArcFace::~ArcFace() {
try {
Destroy();
}
catch (const std::exception& e) {
// Log but don't throw - exceptions in destructors are dangerous
_logger.LogError("ArcFace::~ArcFace", e.what(), __FILE__, __LINE__);
}
catch (...) {
// Catch all exceptions to prevent std::terminate
_logger.LogError("ArcFace::~ArcFace", "Unknown exception during destruction", __FILE__, __LINE__);
}
}
bool ArcFace::Destroy() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
classNames.clear();
if (faiss_index) {
faiss_index->reset();
faiss_index.reset();
}
_isInitialized = false;
m_trtEngine.clearGpuBuffers();
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ArcFace::Destroy", e.what(), __FILE__, __LINE__);
return false;
}
}
void ArcFace::AddEmbedding(const std::string& className, float embedding[]) {
// Validate input before locking
if (!embedding) {
_logger.LogError("ArcFace::AddEmbedding",
"Null embedding pointer.", __FILE__, __LINE__);
return;
}
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
if (!faiss_index) {
_logger.LogError("ArcFace::AddEmbedding",
"FAISS index is not initialized.", __FILE__, __LINE__);
return;
}
// Direct add without intermediate vector copy
classNames.push_back(className);
faiss_index->add(1, embedding);
}
catch (const std::exception& e) {
_logger.LogFatal("ArcFace::AddEmbedding", e.what(), __FILE__, __LINE__);
}
}
void ArcFace::AddEmbedding(const std::string& className, const std::vector<float>& embedding) {
// Early validation before locking
if (embedding.size() != FACE_EMBEDDING_SIZE) {
_logger.LogError("ArcFace::AddEmbedding",
"Embedding size does not match expected output dimension of 512.", __FILE__, __LINE__);
return;
}
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
if (!faiss_index) {
_logger.LogError("ArcFace::AddEmbedding",
"FAISS index is not initialized.", __FILE__, __LINE__);
return;
}
classNames.push_back(className);
faiss_index->add(1, embedding.data());
}
catch (const std::exception& e) {
_logger.LogFatal("ArcFace::AddEmbedding", e.what(), __FILE__, __LINE__);
}
}
}
+134
View File
@@ -0,0 +1,134 @@
#ifndef ARCFACERT_H
#define ARCFACERT_H
#pragma once
#include "ANSFRCommon.h"
//#include "fastdeploy/vision.h"
#include "hnswlib/hnswlib.h"
#include <faiss/IndexFlat.h>
#include "engine.h"
namespace ANSCENTER {
class ArcFace : public ANSFRBase {
public:
virtual bool Initialize(std::string licenseKey,
ModelConfig modelConfig,
const std::string& modelZipFilePath,
const std::string& modelZipPassword,
std::string& labelMap) override;
virtual bool LoadModel(const std::string& modelZipFilePath,
const std::string& modelZipPassword) override;
bool OptimizeModel(bool fp16, std::string& optimizedModelFolder);
// Single face feature (uses RunArcFace)
std::vector<float> Feature(const cv::Mat& image, const ANSCENTER::Object& bBox);
// Main entry: run recognition for all faces in bBox
std::vector<FaceResultObject> Match(const cv::Mat& input,
const std::vector<ANSCENTER::Object>& bBox,
const std::map<std::string, std::string>& userDict);
cv::Mat GetCropFace(const cv::Mat& input, const ANSCENTER::Object& bBox);
void Init();
void AddEmbedding(const std::string& className, float embedding[]);
void AddEmbedding(const std::string& className, const std::vector<float>& embedding);
bool UpdateParamater(double knownPersonThreshold) {
_modelConfig.unknownPersonThreshold = knownPersonThreshold;
m_knownPersonThresh = _modelConfig.unknownPersonThreshold;
return true;
}
~ArcFace();
bool Destroy();
private:
bool LoadEngine(const std::string onnxModelPath, bool engineOptimisation = true);
// Batched forward: one embedding per Object.mask
std::vector<std::vector<float>> Forward(const cv::Mat& input,
const std::vector<ANSCENTER::Object>& outputBbox);
// Search embeddings in FAISS index
std::tuple<std::vector<std::string>, std::vector<float>>
SearchForFaces(const std::vector<std::vector<float>>& detectedEmbeddings);
// Single-face inference (kept for Feature)
std::vector<float> RunArcFace(const cv::Mat& input);
// New: batched inference helper
std::vector<std::vector<float>> RunArcFaceBatch(const std::vector<cv::Mat>& faceROIs);
protected:
std::vector<std::string> classNames;
ModelConfig _modelConfig;
std::string _modelFilePath;
float m_knownPersonThresh = 0.35f;
const int FACE_WIDTH = 112;
const int FACE_HEIGHT = 112;
const int FACE_EMBEDDING_SIZE = 512;
std::unique_ptr<faiss::IndexFlatL2> faiss_index;
ANSCENTER::Options m_options;
std::recursive_mutex _mutex;
Engine<float> m_trtEngine;
float m_ratio = 1.0f;
float m_imgWidth = 0.0f;
float m_imgHeight = 0.0f;
const std::array<float, 3> SUB_VALS{ 0.5f, 0.5f, 0.5f };
const std::array<float, 3> DIV_VALS{ 0.5f, 0.5f, 0.5f };
const bool NORMALIZE = true;
};
// Using FastDeploy Enging to perform facial recognition
// class ArcFace :public ANSFRBase {
// public:
// virtual bool Initialize(std::string licenseKey, ModelConfig modelConfig, const std::string& modelZipFilePath, const std::string& modelZipPassword, std::string& labelMap) override;
// virtual bool LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword)override;
// bool OptimizeModel(bool fp16, std::string& optimizedModelFolder);
// std::vector<float> Feature(const cv::Mat& input, ANSCENTER::Object bBox); // Run inference and get embedding information from a cropped image
// std::vector<FaceResultObject> Match(const cv::Mat& input, std::vector<ANSCENTER::Object> bBox, std::map<std::string, std::string>userDict); // Run inference and get embedding information from a cropped image (the first bbox)
// cv::Mat GetCropFace(const cv::Mat& input, ANSCENTER::Object bBox);
// void Init();
// void AddEmbedding(const std::string& className, float embedding[]);
// void AddEmbedding(const std::string& className, const std::vector<float>& embedding);
// bool UpdateParamater(double knownPersonThreshold) {
// _modelConfig.unknownPersonThreshold = knownPersonThreshold;
// _modelConfig.unknownPersonThreshold = knownPersonThreshold;
// m_knownPersonThresh = _modelConfig.unknownPersonThreshold;
// return true;
// }
//~ArcFace();
// bool Destroy();
// private:
// bool LoadEngine(const std::string engineFile, bool engineOptimisation = true);
// std::vector<std::vector<float>> Forward(const cv::Mat& input, std::vector<ANSCENTER::Object> outputBbox);
// std::tuple<std::vector<std::string>, std::vector<float>> SearchForFaces(const std::vector<std::vector<float>>& detectedEmbeddings);
//std::vector<float> RunArcFace(const cv::Mat& input);
// protected:
// std::vector<std::string> classNames;
// ModelConfig _modelConfig;
// std::string _modelFilePath;
// float m_knownPersonThresh;
// const int FACE_WIDTH = 112;
// const int FACE_HEIGHT = 112;
// const int FACE_EMBEDDING_SIZE = 512;
// std::unique_ptr<faiss::IndexFlatL2> faiss_index; // Use shared_ptr
// ANSCENTER::Options m_options;
// std::recursive_mutex _mutex;
// Engine<float> m_trtEngine;
// float m_ratio = 1;
// float m_imgWidth = 0;
// float m_imgHeight = 0;
// const std::array<float, 3> SUB_VALS{ 0.5f, 0.5f, 0.5f };
// const std::array<float, 3> DIV_VALS{ 0.5f, 0.5f, 0.5f };
// const bool NORMALIZE = true;
// };
}
#endif
+77
View File
@@ -0,0 +1,77 @@
# ANSFR — Face Recognition DLL (ONNX + TensorRT + FAISS + SQLite)
# Explicit source list from original vcxproj
set(ANSFR_SOURCES
ANSFR.cpp
ANSFRCommon.cpp
ANSFaceRecognizer.cpp
ANSGpuFrameRegistry.cpp
FaceDatabase.cpp
FaceNet.cpp
dllmain.cpp
pch.cpp
sqlite3.c
# Engine sources compiled into ANSFR
${CMAKE_SOURCE_DIR}/engines/ONNXEngine/ONNXEngine.cpp
${CMAKE_SOURCE_DIR}/engines/OpenVINOEngine/src/faceapp/cnn.cpp
${CMAKE_SOURCE_DIR}/engines/OpenVINOEngine/src/utils/args_helper.cpp
${CMAKE_SOURCE_DIR}/engines/OpenVINOEngine/src/utils/common.cpp
)
file(GLOB ANSFR_HEADERS "*.h")
add_library(ANSFR SHARED ${ANSFR_SOURCES} ${ANSFR_HEADERS})
target_include_directories(ANSFR PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
target_include_directories(ANSFR PRIVATE
${CMAKE_SOURCE_DIR}/engines/ONNXEngine
${CMAKE_SOURCE_DIR}/engines/OpenVINOEngine/include
${CMAKE_SOURCE_DIR}/engines/TensorRTAPI/include
${CMAKE_SOURCE_DIR}/ANSODEngine
${SHARED_INCLUDE_DIR}
)
target_include_directories(ANSFR PRIVATE
${CMAKE_SOURCE_DIR}/ANSMOT
${CMAKE_SOURCE_DIR}/engines/OpenVINOEngine/include/faceapp
${ANSLIBS_DIR}/faiss
)
target_link_libraries(ANSFR
PUBLIC ANSLibsLoader
PRIVATE ANSODEngine
PRIVATE ANSLicensingSystem
PRIVATE labview
PRIVATE spdlog_dep
PRIVATE opencv
PRIVATE onnxruntime
PRIVATE tensorrt
PRIVATE openvino
PRIVATE faiss
PRIVATE mkl
PRIVATE boost
PRIVATE ANSMOT
PRIVATE CUDA::cudart_static
PRIVATE CUDA::cublas
PRIVATE CUDA::cublasLt
PRIVATE nvinfer_10.lib
PRIVATE nvonnxparser_10.lib
)
# Boost asio needs full boost install path
target_include_directories(ANSFR PRIVATE ${RESEARCH_DIR}/boost_1_88_0)
target_compile_definitions(ANSFR PRIVATE UNICODE _UNICODE
ANSFR_EXPORTS
ENGINE_EXPORTS
_USRDLL
)
target_precompile_headers(ANSFR PRIVATE pch.h)
# sqlite3.c is pure C — skip precompiled headers and force C language
set_source_files_properties(sqlite3.c PROPERTIES
SKIP_PRECOMPILE_HEADERS ON
LANGUAGE C
)
+909
View File
@@ -0,0 +1,909 @@
#include "FaceDatabase.h"
namespace ANSCENTER {
FaceDatabase::FaceDatabase() {
m_db = nullptr;
m_embedding_dim = 0;
}
bool FaceDatabase::Initialize(std::string path, int embedding_dim, std::string dataFolder) {
std::lock_guard<std::mutex> lock(_mutex);
try {
sqlite3_stmt* stmt;
_dataFolder = dataFolder;
m_embedding_dim = embedding_dim;
std::string sql;
int rc;
int isNotEmpty = 0;
//this->_logger.LogDebug("FaceDatabase::Initialize. Database path: ", path, __FILE__, __LINE__);
std::string faceDatabaseFolder = GetParentFolder(path);
if (!FolderExist(faceDatabaseFolder)) fs::create_directory(faceDatabaseFolder);
std::string imageDataFolder = CreateFilePath(faceDatabaseFolder, "data");
if (!FolderExist(imageDataFolder)) fs::create_directory(imageDataFolder);
rc = sqlite3_open(path.c_str(), &m_db);
if (rc) {
_logger.LogError("FaceDatabase::Initialize", std::string("Can't open database: ") + sqlite3_errmsg(m_db), __FILE__, __LINE__);
}
else {
_logger.LogDebug("FaceDatabase::Initialize", "Opened database successfully", __FILE__, __LINE__);
}
// Fix #2: Enable WAL mode for concurrent read/write from separate processes
sqlite3_exec(m_db, "PRAGMA journal_mode=WAL;", nullptr, nullptr, nullptr);
// Fix #2: 5-second busy timeout for multi-process contention
sqlite3_exec(m_db, "PRAGMA busy_timeout=5000;", nullptr, nullptr, nullptr);
// Fix #2: Enable foreign key enforcement
sqlite3_exec(m_db, "PRAGMA foreign_keys=ON;", nullptr, nullptr, nullptr);
// Prepare
sql = "SELECT name FROM sqlite_master;";
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::Initialize. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
}
// Step
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
isNotEmpty = 1;
break;
}
sqlite3_finalize(stmt);
if (isNotEmpty) {
//this->_logger.LogDebug("FaceDatabase::Initialize.", "Database is not empty", __FILE__, __LINE__);
}
else {
//this->_logger.LogDebug("FaceDatabase::Initialize.", "Empty Database, initiating...", __FILE__, __LINE__);
// Prepare
sql = "CREATE TABLE IF NOT EXISTS USER ( \
ID INTEGER PRIMARY KEY AUTOINCREMENT, \
CODE TEXT, \
NAME TEXT, \
UNIQUE(ID, CODE)\
)";
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::Initialize. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
}
// Step
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
// this->_logger.LogError("FaceDatabase::Initialize. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
}
else {
// this->_logger.LogDebug("FaceDatabase::Initialize.", "Table `USER` created successfully", __FILE__, __LINE__);
}
sqlite3_finalize(stmt);
// Prepare
sql = "CREATE TABLE IF NOT EXISTS FACE ( \
ID INTEGER PRIMARY KEY AUTOINCREMENT, \
USER INTEGER, \
IMG_PATH TEXT, \
EMBEDDING BLOB, \
UNIQUE(ID, USER), \
FOREIGN KEY(USER) REFERENCES USER(ID) \
)";
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::Initialize. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
}
// Step
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
// this->_logger.LogError("FaceDatabase::Initialize. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
}
else {
// this->_logger.LogDebug("FaceDatabase::Initialize", "Table `FACE` created successfully.", __FILE__, __LINE__);
}
sqlite3_finalize(stmt);
}
return true;
}
catch (std::exception& e) {
//this->_logger.LogFatal("FaceDatabase::Initialize", e.what(), __FILE__, __LINE__);
//sqlite3_finalize(stmt);
return false;
}
}
int FaceDatabase::InsertUser(std::string userId, std::string userName) {
std::lock_guard<std::mutex> lock(_mutex);
std::string sql;
sqlite3_stmt* stmt;
int rc;
int resultCode = 0;
// Prepare
sql = "INSERT INTO USER (CODE, NAME) VALUES (?, ?);";
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
try {
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::InsertUser. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
return -2;
}
// Binding
rc = sqlite3_bind_text(stmt, 1, userId.c_str(), int(userId.length()), SQLITE_STATIC);
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::InsertUser. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -3;
}
rc = sqlite3_bind_text(stmt, 2, userName.c_str(), int(userName.length()), SQLITE_STATIC);
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::InsertUser. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -3;
}
// Step
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
//this->_logger.LogError("FaceDatabase::InsertUser. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -4;
}
if (rc == SQLITE_DONE) {
// this->_logger.LogDebug("FaceDatabase::InsertUser:", "Record successfully created", __FILE__, __LINE__);
}
sqlite3_finalize(stmt);
int id = (int)sqlite3_last_insert_rowid(m_db);
resultCode = id;
return resultCode;
}
catch (std::exception& e) {
//this->_logger.LogFatal("FaceDatabase::InsertUser", e.what(), __FILE__, __LINE__);
sqlite3_finalize(stmt);
return -1;
}
}
int FaceDatabase::UpdateUser(int userId, std::string newUserCode, std::string newUserName) {
std::lock_guard<std::mutex> lock(_mutex);
std::string sql;
sqlite3_stmt* stmt;
int resultCode = 0;
int rc;
// Prepare
// UPDATE USER SET CODE ="0003", NAME= "Biden" WHERE ID =1
sql = "UPDATE USER SET CODE=?, NAME=? WHERE ID=?;";
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
try {
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::InsertUser. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
return -1;
}
// Binding
rc = sqlite3_bind_text(stmt, 1, newUserCode.c_str(), int(newUserCode.length()), SQLITE_STATIC);
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::InsertUser. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -2;
}
rc = sqlite3_bind_text(stmt, 2, newUserName.c_str(), int(newUserName.length()), SQLITE_STATIC);
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::InsertUser. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -2;
}
rc = sqlite3_bind_int(stmt, 3, userId);
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::InsertUser. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -2;
}
// Step
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
//this->_logger.LogError("FaceDatabase::InsertUser. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -3;
}
if (rc == SQLITE_DONE) {
// this->_logger.LogDebug("FaceDatabase::InsertUser:", "Record successfully created", __FILE__, __LINE__);
}
sqlite3_finalize(stmt);
resultCode = 1;
return resultCode;
}
catch (std::exception& e) {
//this->_logger.LogFatal("FaceDatabase::InsertUser", e.what(), __FILE__, __LINE__);
sqlite3_finalize(stmt);
return 0;
}
}
int FaceDatabase::InsertFace(int userId, std::string imgPath, float embedding[]) {
std::lock_guard<std::mutex> lock(_mutex);
if (!FileExist(imgPath)) return 0;
std::string sql;
sqlite3_stmt* stmt;
int rc;
int resultCode = 0;
// Prepare
sql = "INSERT INTO FACE (USER, IMG_PATH, EMBEDDING) VALUES (?, ?, ?);";
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
try {
if (rc != SQLITE_OK) {
//std::cout << "SQL error: " << sqlite3_errmsg(m_db);
return -2;
}
// Binding
rc = sqlite3_bind_int(stmt, 1, userId);
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::InsertFace. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -3;
}
rc = sqlite3_bind_text(stmt, 2, imgPath.c_str(), int(imgPath.length()), SQLITE_STATIC);
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::InsertFace. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -3;
}
rc = sqlite3_bind_blob(stmt, 3, embedding, sizeof(float) * m_embedding_dim, SQLITE_STATIC);
if (rc != SQLITE_OK) {
//::cout << "SQL bind error: " << sqlite3_errmsg(m_db);
resultCode = -3;
}
// Step
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
//this->_logger.LogError("FaceDatabase::InsertFace. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -4;
}
if (rc == SQLITE_DONE) {
//this->_logger.LogDebug("FaceDatabase::InsertFace.", "Embedding for user created successfully", __FILE__, __LINE__);
}
sqlite3_finalize(stmt);
int id = (int)sqlite3_last_insert_rowid(m_db);
resultCode = id;
return resultCode;
}
catch (std::exception& e) {
//this->_logger.LogFatal("FaceDatabase::InsertFace", e.what(), __FILE__, __LINE__);
sqlite3_finalize(stmt);
return -1;
}
}
int FaceDatabase::DeleteFace(int id) {
try {
// Fix #4: Single lock scope — GetFaceById + DELETE in one atomic section
std::lock_guard<std::mutex> lock(_mutex);
std::string sql;
sqlite3_stmt* stmt;
int rc;
int userId;
int resultCode = 0;
std::string faceImagePath;
// Look up face image path (now inside lock — no TOCTOU race)
{
sqlite3_stmt* lookupStmt;
rc = sqlite3_prepare_v2(m_db, "SELECT USER, IMG_PATH FROM FACE WHERE ID=?;", -1, &lookupStmt, NULL);
if (rc == SQLITE_OK) {
sqlite3_bind_int(lookupStmt, 1, id);
if (sqlite3_step(lookupStmt) == SQLITE_ROW) {
userId = sqlite3_column_int(lookupStmt, 0);
const char* pathText = reinterpret_cast<const char*>(sqlite3_column_text(lookupStmt, 1));
faceImagePath = pathText ? std::string(pathText) : "";
}
}
sqlite3_finalize(lookupStmt);
}
// Delete image file
if (!faceImagePath.empty() && FileExist(faceImagePath)) {
if (!fs::remove(faceImagePath)) {
_logger.LogError("FaceDatabase::DeleteFace", "Failed to delete face image: " + faceImagePath, __FILE__, __LINE__);
}
}
//_mutex.lock();
// Prepare
sql = "DELETE FROM FACE WHERE ID=?;";
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::DeleteFace. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -1;
}
// Binding
rc = sqlite3_bind_int(stmt, 1, id);
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::DeleteFace. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -2;
}
// Step
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
//this->_logger.LogError("FaceDatabase::DeleteFace. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -3;
}
if (rc == SQLITE_DONE) {
// this->_logger.LogDebug("FaceDatabase::DeleteFace.", "Embedding for user deleted successfully", __FILE__, __LINE__);
}
sqlite3_finalize(stmt);
resultCode = 1;
//_mutex.unlock();
return resultCode;
}
catch (std::exception& e) {
//this->_logger.LogFatal("FaceDatabase::DeleteFace", e.what(), __FILE__, __LINE__);
//_mutex.unlock();
return 0;
}
}
int FaceDatabase::DeleteUser(int userId) {
try {
std::string sql;
sqlite3_stmt* stmt;
int resultCode = 0;
int rc;
std::vector<int> faceIds;
faceIds.clear();
GetFaceIdsByUser(userId, faceIds);
int numOfFaces = (int)faceIds.size();
//this->_logger.LogDebug("FaceDatabase::DeleteUser. Number of Face for this user", std::to_string(numOfFaces), __FILE__, __LINE__);
if (numOfFaces > 0) {
for (int i = 0; i < numOfFaces; i++) {
if (!DeleteFace(faceIds[i])) {
//this->_logger.LogError("FaceDatabase::DeleteUser. Failed to delete face id", std::to_string(numOfFaces), __FILE__, __LINE__);
}
}
}
std::string userImageFolderName = "User" + std::to_string(userId);
std::string userImageFolder = CreateFilePath(_dataFolder, userImageFolderName);
if (FolderExist(userImageFolder)) {
if (!DeleteFolder(userImageFolder)) {
//this->_logger.LogError("FaceDatabase::DeleteUser. Failed to delete user data folder", userImageFolder, __FILE__, __LINE__);
}
}
// Delete person
std::lock_guard<std::mutex> lock(_mutex);
//_mutex.lock();
// Prepare
sql = "DELETE FROM USER WHERE ID=?;";
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::DeleteUser. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -1;
}
// Binding
rc = sqlite3_bind_int(stmt, 1, userId);
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::DeleteUser. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -2;
}
// Step
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
//this->_logger.LogError("FaceDatabase::DeleteUser. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -3;
}
if (rc == SQLITE_DONE) {
//this->_logger.LogDebug("FaceDatabase::DeleteUser.", "Deleted person successfully", __FILE__, __LINE__);
}
sqlite3_finalize(stmt);
resultCode = 1;
//_mutex.unlock();
return resultCode;
}
catch (std::exception& e) {
//this->_logger.LogFatal("FaceDatabase::DeleteUser", e.what(), __FILE__, __LINE__);
return 0;
}
}
int FaceDatabase::GetUserId(std::string code) {
std::lock_guard<std::mutex> lock(_mutex);
std::string sql;
sqlite3_stmt* stmt;
int rc;
// Prepare
sql = "SELECT ID FROM USER WHERE CODE =?;";
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
try {
int resultCode = 0;
int userId = -1;
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::GetUser. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -1;
}
// Binding
rc = sqlite3_bind_text(stmt, 1, code.c_str(), int(code.length()), SQLITE_STATIC);
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::GetUser. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -2;
}
// Step
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
//this->_logger.LogError("FaceDatabase::GetUser. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -3;
}
if (rc == SQLITE_ROW) {
//this->_logger.LogDebug("FaceDatabase::GetUser.", "Get User information successfully", __FILE__, __LINE__);
userId = sqlite3_column_int(stmt, 0);
}
sqlite3_finalize(stmt);
resultCode = userId;
return resultCode;
}
catch (std::exception& e) {
//this->_logger.LogFatal("FaceDatabase::GetUserId", e.what(), __FILE__, __LINE__);
sqlite3_finalize(stmt);
return -1;
}
}
int FaceDatabase::GetUser(int userId, std::string& code, std::string& userName) {
std::lock_guard<std::mutex> lock(_mutex);
std::string sql;
sqlite3_stmt* stmt;
int resultCode = 0;
userName = "";
code = "";
int rc;
// Prepare
sql = "SELECT * FROM USER WHERE ID =?;";
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
try {
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::GetUser. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -1;
}
// Binding
rc = sqlite3_bind_int(stmt, 1, userId);
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::GetUser. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -2;
}
// Step
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
//this->_logger.LogError("FaceDatabase::GetUser. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -3;
}
if (rc == SQLITE_ROW) {
//this->_logger.LogDebug("FaceDatabase::GetUser.", "Get User information successfully", __FILE__, __LINE__);
const char* codeText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
const char* nameText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
code = codeText ? std::string(codeText) : "";
userName = nameText ? std::string(nameText) : "";
}
sqlite3_finalize(stmt);
resultCode = 1;
return resultCode;
}
catch (std::exception& e) {
//this->_logger.LogFatal("FaceDatabase::GetUserId", e.what(), __FILE__, __LINE__);
sqlite3_finalize(stmt);
return -1;
}
}
int FaceDatabase::GetFaceIdsByUser(int userId, std::vector<int>& faceIds)
{
std::lock_guard<std::mutex> lock(_mutex);
faceIds.clear();
std::string sql;
sqlite3_stmt* stmt;
int rc;
sql = "SELECT ID FROM FACE WHERE USER=?;";
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
try {
int resultCode = 0;
int faceId;
if (rc != SQLITE_OK) {
_logger.LogError("FaceDatabase::GetFaceIdsByUser", std::string("SQL error: ") + sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -1;
}
// Binding
rc = sqlite3_bind_int(stmt, 1, userId);
if (rc != SQLITE_OK) {
_logger.LogError("FaceDatabase::GetFaceIdsByUser", std::string("SQL bind error: ") + sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -2;
}
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
faceId = sqlite3_column_int(stmt, 0);
faceIds.push_back(faceId);
}
if (rc != SQLITE_DONE) {
_logger.LogError("FaceDatabase::GetFaceIdsByUser", std::string("SQL step error: ") + sqlite3_errmsg(m_db), __FILE__, __LINE__);
}
sqlite3_finalize(stmt);
resultCode = 1;
return resultCode;
}
catch (std::exception& e) {
//this->_logger.LogFatal("FaceDatabase::GetFaceIdsByUser", e.what(), __FILE__, __LINE__);
sqlite3_finalize(stmt);
return 0;
}
}
int FaceDatabase::GetUsers(std::vector<int>& ids, std::vector<std::string>& codes, std::vector<std::string>& userNames) {
std::lock_guard<std::mutex> lock(_mutex);
std::string sql;
sqlite3_stmt* stmt;
int rc;
int userId;
std::string userCode;
std::string userName;
int resutCode = 0;
ids.clear();
codes.clear();
userNames.clear();
sql = "SELECT * FROM USER;";
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
try {
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::GetUsers. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resutCode = -1;
}
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
userId = sqlite3_column_int(stmt, 0);
const char* codeText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
const char* nameText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
userCode = codeText ? std::string(codeText) : "";
userName = nameText ? std::string(nameText) : "";
ids.push_back(userId);
codes.push_back(userCode);
userNames.push_back(userName);
}
if (rc != SQLITE_DONE) {
//this->_logger.LogError("FaceDatabase::GetUsers. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
}
sqlite3_finalize(stmt);
resutCode = 1;
return resutCode;
}
catch (std::exception& e) {
//this->_logger.LogFatal("FaceDatabase::GetUsers", e.what(), __FILE__, __LINE__);
sqlite3_finalize(stmt);
return 0;
}
}
int FaceDatabase::GetFaceById(int faceId, int& userId, std::string& imagePath) {
std::lock_guard<std::mutex> lock(_mutex);
std::string sql;
sqlite3_stmt* stmt;
int rc;
int resultCode = 0;
userId = -1;
imagePath = "";
// SELECT *FROM FACE WHERE ID =1
sql = "SELECT * FROM FACE WHERE ID =?;";
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
try {
if (rc != SQLITE_OK) {
_logger.LogError("FaceDatabase::GetFaceById", std::string("SQL error: ") + sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -1;
}
// Binding
rc = sqlite3_bind_int(stmt, 1, faceId);
if (rc != SQLITE_OK) {
_logger.LogError("FaceDatabase::GetFaceById", std::string("SQL bind error: ") + sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -2;
}
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
userId = sqlite3_column_int(stmt, 1);
const char* pathText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
imagePath = pathText ? std::string(pathText) : "";
}
if (rc != SQLITE_DONE) {
//this->_logger.LogError("FaceDatabase::GetFaceIds. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
}
sqlite3_finalize(stmt);
resultCode = 1;
return resultCode;
}
catch (std::exception& e) {
//this->_logger.LogFatal("FaceDatabase::GetFaceIds", e.what(), __FILE__, __LINE__);
sqlite3_finalize(stmt);
return 0;
}
}
std::map<std::string, std::string> FaceDatabase::GetUserDict() {
std::lock_guard<std::mutex> lock(_mutex);
std::map<std::string, std::string> userDict;
userDict.clear();
std::string sql;
sqlite3_stmt* stmt;
int rc;
std::string userId;
std::string userCode;
std::string userName;
// get user id and name
sql = "SELECT * FROM USER;";
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
try {
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::GetUserDict. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
}
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
const char* idText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
const char* codeText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
const char* nameText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
userId = idText ? std::string(idText) : "";
userCode = codeText ? std::string(codeText) : "";
userName = nameText ? std::string(nameText) : "";
if (!userCode.empty()) {
userDict[userId] = userName+"(" + userCode + ")";
}
else {
userDict[userId] = userName;
}
}
if (rc != SQLITE_DONE) {
//this->_logger.LogError("FaceDatabase::GetUserDict. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
}
sqlite3_finalize(stmt);
return userDict;
}
catch (std::exception& e) {
//this->_logger.LogFatal("FaceDatabase::GetUserDict", e.what(), __FILE__, __LINE__);
sqlite3_finalize(stmt);
return userDict;
}
}
int FaceDatabase::GetNumEmbeddings() {
std::lock_guard<std::mutex> lock(_mutex);
try {
int numEmbeds = 0;
std::string sql;
sqlite3_stmt* stmt;
int rc;
int resultCode = 0;
// Prepare
sql = "SELECT COUNT(*) FROM FACE;";
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::GetNumEmbeddings. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -1;
}
// Step
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
//this->_logger.LogError("FaceDatabase::GetNumEmbeddings. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -2;
}
numEmbeds = sqlite3_column_int(stmt, 0);
sqlite3_finalize(stmt);
resultCode = numEmbeds;
return resultCode;
}
catch (std::exception& e) {
//this->_logger.LogFatal("FaceDatabase::GetNumEmbeddings", e.what(), __FILE__, __LINE__);
return 0;
}
}
int FaceDatabase::GetEmbeddings(std::unique_ptr<ANSFRBase>& recognizer) {
try {
std::string sql;
sqlite3_stmt* stmt;
int rc;
int resultCode = 0;
std::string userId;
int numEmbeds = GetNumEmbeddings();
_logger.LogDebug("FaceDatabase::GetEmbeddings", "There are " + std::to_string(numEmbeds) + " embeddings in database", __FILE__, __LINE__);
// get embedding
//_mutex.lock();
std::lock_guard<std::mutex> lock(_mutex);
sql = "SELECT * FROM FACE;";
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::GetEmbeddings. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -1;
}
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
const char* userIdText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
if (!userIdText) continue;
userId = std::string(userIdText);
float* embedding = (float*)sqlite3_column_blob(stmt, 3);
if (!embedding) continue;
recognizer->AddEmbedding(userId, embedding);
}
if (rc != SQLITE_DONE) {
//this->_logger.LogError("FaceDatabase::GetEmbeddings. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
}
sqlite3_finalize(stmt);
resultCode = 0;
//_mutex.unlock();
return resultCode;
}
catch (std::exception& e) {
//this->_logger.LogFatal("FaceDatabase::GetEmbeddings", e.what(), __FILE__, __LINE__);
//_mutex.unlock();
return 0;
}
}
int FaceDatabase::GetEmbeddings(ANSFRBase& recognizer) {
try {
std::string sql;
sqlite3_stmt* stmt;
int rc;
int resultCode = 0;
std::string userId;
int numEmbeds = GetNumEmbeddings();
_logger.LogDebug("FaceDatabase::GetEmbeddings", "There are " + std::to_string(numEmbeds) + " embeddings in database", __FILE__, __LINE__);
// get embedding
//_mutex.lock();
std::lock_guard<std::mutex> lock(_mutex);
sql = "SELECT * FROM FACE;";
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
if (rc != SQLITE_OK) {
//this->_logger.LogError("FaceDatabase::GetEmbeddings. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -1;
}
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
const char* userIdText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
if (!userIdText) continue;
userId = std::string(userIdText);
float* embedding = (float*)sqlite3_column_blob(stmt, 3);
if (!embedding) continue;
recognizer.AddEmbedding(userId, embedding);
}
if (rc != SQLITE_DONE) {
//this->_logger.LogError("FaceDatabase::GetEmbeddings. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
}
sqlite3_finalize(stmt);
resultCode = 0;
//_mutex.unlock();
return resultCode;
}
catch (std::exception& e) {
//this->_logger.LogFatal("FaceDatabase::GetEmbeddings", e.what(), __FILE__, __LINE__);
//_mutex.unlock();
return 0;
}
}
int FaceDatabase::GetAllEmbeddingRecords(std::vector<FaceEmbeddingRecord>& records) {
try {
std::lock_guard<std::mutex> lock(_mutex);
records.clear();
int numEmbeds = 0;
{
// Count for pre-allocation (inline, mutex already held)
sqlite3_stmt* countStmt;
int rc = sqlite3_prepare_v2(m_db, "SELECT COUNT(*) FROM FACE;", -1, &countStmt, NULL);
if (rc == SQLITE_OK && sqlite3_step(countStmt) == SQLITE_ROW) {
numEmbeds = sqlite3_column_int(countStmt, 0);
}
sqlite3_finalize(countStmt);
}
records.reserve(numEmbeds);
sqlite3_stmt* stmt;
int rc = sqlite3_prepare_v2(m_db,
"SELECT ID, USER, EMBEDDING FROM FACE;", -1, &stmt, NULL);
if (rc != SQLITE_OK) {
return -1;
}
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
int faceId = sqlite3_column_int(stmt, 0);
const char* userIdText = reinterpret_cast<const char*>(
sqlite3_column_text(stmt, 1));
if (!userIdText) continue;
const float* embData = reinterpret_cast<const float*>(
sqlite3_column_blob(stmt, 2));
int embBytes = sqlite3_column_bytes(stmt, 2);
int embSize = embBytes / static_cast<int>(sizeof(float));
if (!embData || embSize <= 0) continue;
FaceEmbeddingRecord record;
record.faceId = faceId;
record.userId = std::string(userIdText);
record.embedding.assign(embData, embData + embSize);
records.push_back(std::move(record));
}
sqlite3_finalize(stmt);
return 0;
}
catch (const std::exception& e) {
return -1;
}
}
int FaceDatabase::GetUsersWithFaceIds(std::vector<int>& ids, std::vector<std::string>& codes,
std::vector<std::string>& userNames, std::vector<std::vector<int>>& faceIdsByUser) {
// Fix #12: Single JOIN query replaces N+1 GetUser + GetFaceIdsByUser calls
std::lock_guard<std::mutex> lock(_mutex);
ids.clear();
codes.clear();
userNames.clear();
faceIdsByUser.clear();
try {
sqlite3_stmt* stmt;
int rc = sqlite3_prepare_v2(m_db,
"SELECT U.ID, U.CODE, U.NAME, F.ID AS FACE_ID "
"FROM USER U LEFT JOIN FACE F ON U.ID = F.USER "
"ORDER BY U.ID;",
-1, &stmt, NULL);
if (rc != SQLITE_OK) {
_logger.LogError("FaceDatabase::GetUsersWithFaceIds",
std::string("SQL error: ") + sqlite3_errmsg(m_db), __FILE__, __LINE__);
return -1;
}
int lastUserId = -1;
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
int userId = sqlite3_column_int(stmt, 0);
if (userId != lastUserId) {
// New user row
const char* codeText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
const char* nameText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
ids.push_back(userId);
codes.push_back(codeText ? std::string(codeText) : "");
userNames.push_back(nameText ? std::string(nameText) : "");
faceIdsByUser.emplace_back();
lastUserId = userId;
}
// Append face ID (may be NULL if user has no faces — LEFT JOIN)
if (sqlite3_column_type(stmt, 3) != SQLITE_NULL) {
faceIdsByUser.back().push_back(sqlite3_column_int(stmt, 3));
}
}
if (rc != SQLITE_DONE) {
_logger.LogError("FaceDatabase::GetUsersWithFaceIds",
std::string("SQL step error: ") + sqlite3_errmsg(m_db), __FILE__, __LINE__);
}
sqlite3_finalize(stmt);
return 1;
}
catch (const std::exception& e) {
_logger.LogError("FaceDatabase::GetUsersWithFaceIds", e.what(), __FILE__, __LINE__);
return 0;
}
}
FaceDatabase::~FaceDatabase() {
try {
Destroy();
}
catch (std::exception& e) {
_logger.LogError("FaceDatabase::~FaceDatabase", e.what(), __FILE__, __LINE__);
}
}
bool FaceDatabase::Destroy() {
std::lock_guard<std::mutex> lock(_mutex);
try {
if (!m_db) return true;
int rc = sqlite3_close(m_db);
if (rc == SQLITE_OK) {
//this->_logger.LogDebug("Database::Destroy", "Success!", __FILE__, __LINE__);
m_db = nullptr;
return true;
}
else {
std::string errorCode = "Cannot close database connection.Error code:" + std::to_string(rc);
//this->_logger.LogError("Database::Destroy", errorCode, __FILE__, __LINE__);
return false;
}
}
catch (std::exception& e) {
//this->_logger.LogFatal("FaceDatabase::Destroy", e.what(), __FILE__, __LINE__);
return false;
}
}
}
+53
View File
@@ -0,0 +1,53 @@
#ifndef FACEDATABASE_H
#define FACEDATABASE_H
#pragma once
#include <iostream>
#include <map>
//#include "ARCFaceRT.h"
#include "sqlite3.h"
#include <vector>
#include "ANSFRCommon.h"
namespace ANSCENTER {
// Structured embedding record for double-buffer reload
struct FaceEmbeddingRecord {
int faceId; // FACE.ID (unique per face)
std::string userId; // FACE.USER (= USER.ID, as string)
std::vector<float> embedding;
};
class FaceDatabase {
public:
FaceDatabase();
~FaceDatabase();
bool Destroy();
bool Initialize(std::string path, int embeddingDim, std::string dataFolder);
int InsertUser(std::string code, std::string userName);
int UpdateUser(int UserId, std::string newUserCode, std::string newUserName);
int InsertFace(int userId, std::string imgPath, float embedding[]);
int DeleteFace(int id);
int DeleteUser(int UserId);
int GetFaceIdsByUser(int userId, std::vector<int>& faceIds);
int GetUserId(std::string code);
int GetUser(int userId, std::string& code, std::string& userName);
int GetUsers(std::vector<int>& ids, std::vector<std::string>& codes, std::vector<std::string>& userNames);
// Fix #12: Batch query — returns users with their face IDs in a single query
int GetUsersWithFaceIds(std::vector<int>& ids, std::vector<std::string>& codes,
std::vector<std::string>& userNames, std::vector<std::vector<int>>& faceIdsByUser);
int GetFaceById(int faceId, int& userId, std::string& imagePath);
std::map<std::string, std::string> GetUserDict();
int GetEmbeddings(ANSFRBase& recognizer);
int GetEmbeddings(std::unique_ptr<ANSFRBase>& recognizer);
// Returns all face embedding records (FACE.ID + USER.ID + embedding) for double-buffer reload
int GetAllEmbeddingRecords(std::vector<FaceEmbeddingRecord>& records);
private:
sqlite3* m_db;
int m_embedding_dim;
std::string _dataFolder;
std::mutex _mutex;
// Fix #10: Logger for database operations
ANSCENTER::SPDLogger& _logger = ANSCENTER::SPDLogger::GetInstance("FaceDB");
int GetNumEmbeddings();
};
}
#endif
+412
View File
@@ -0,0 +1,412 @@
#include "FaceNet.h"
namespace ANSCENTER {
std::string ANSFaceNet::GetOpenVINODevice() {
ov::Core core;
std::vector<std::string> available_devices = core.get_available_devices();
// Prioritize devices: NPU > GPU > CPU
std::vector<std::string> priority_devices = {"GPU", "CPU" };
for (const auto& device : priority_devices) {
if (std::find(available_devices.begin(), available_devices.end(), device) != available_devices.end()) {
return device; // Return the first available device based on priority
}
}
// Default to CPU if no prioritized devices are found
return "CPU";
}
bool ANSFaceNet::Initialize(std::string licenseKey,ModelConfig modelConfig,
const std::string& modelZipFilePath,const std::string& modelZipPassword,std::string& labelMap)
{
bool result = ANSFRBase::Initialize(licenseKey, modelConfig, modelZipFilePath, modelZipPassword, labelMap);
if (!result) return false;
try {
_modelConfig = modelConfig;
_modelConfig.modelType = ModelType::FACERECOGNIZE;
_modelConfig.detectionType = DetectionType::FACERECOGNIZER;
// We need to get the modelfolder from here
std::string faceidModel = CreateFilePath(_modelFolder, "ansfacenet.xml");
if (std::filesystem::exists(faceidModel)) {
_modelFilePath = faceidModel;
this->_logger.LogDebug("ANSFaceNet::Initialize. Loading ansfacenet weight", _modelFilePath, __FILE__, __LINE__);
}
else {
this->_logger.LogError("ANSFaceNet::Initialize. Model ansfacenet.xml file is not exist", _modelFilePath, __FILE__, __LINE__);
return false;
}
m_knownPersonThresh = _modelConfig.unknownPersonThreshold;
if (m_knownPersonThresh == 0)m_knownPersonThresh = 0.35;
std::string deviceName = GetOpenVINODevice();
ov::Core core;
CnnConfig reid_config(_modelFilePath, "Face Re-Identification");
reid_config.m_deviceName = deviceName;
//core.set_property(deviceName, ov::hint::performance_mode(ov::hint::PerformanceMode::THROUGHPUT));
reid_config.m_max_batch_size = 1;
reid_config.m_core = core;
this->faceRecognizer = std::make_unique<VectorCNN>(reid_config);
if (faceRecognizer == nullptr) {
this->_logger.LogFatal("ANSFaceNet::Initialize", "Failed to initialize face recognizer model", __FILE__, __LINE__);
return false;
}
Init();
_isInitialized = true;
return true;
}
catch (std::exception& e) {
this->_logger.LogFatal("ANSFaceNet::Initialize", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSFaceNet::LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword) {
try {
// We need to get the _modelFolder
bool result = ANSFRBase::LoadModel(modelZipFilePath, modelZipPassword);
if (!result) return false;
// We need to get the modelfolder from here
std::string faceidModel = CreateFilePath(_modelFolder, "ansfacenet.xml");
if (std::filesystem::exists(faceidModel)) {
_modelFilePath = faceidModel;
this->_logger.LogDebug("ANSFaceNet::LoadModel. Loading ansfacenet weight", _modelFilePath, __FILE__, __LINE__);
}
else {
this->_logger.LogError("ANSFaceNet::Initialize. Model ansfacenet.xml file is not exist", _modelFilePath, __FILE__, __LINE__);
return false;
}
return true;
}
catch (std::exception& e) {
this->_logger.LogFatal("ArcFace50::LoadModel", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSFaceNet::OptimizeModel(bool fp16, std::string& optimizedModelFolder) {
if (!FileExist(_modelFilePath)) {
optimizedModelFolder = "";
return false;
}
return true;
}
std::vector<float> ANSFaceNet::Feature(const cv::Mat& image, const ANSCENTER::Object& bBox) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
std::vector<float> embeddingResult;
try {
if (image.empty()) return embeddingResult;
if ((image.cols < 10) || (image.rows < 10)) return embeddingResult;
std::vector<cv::Mat> embeddings;
std::vector<cv::Mat> face_rois;
face_rois.clear();
embeddings.clear();
face_rois.push_back(bBox.mask);
faceRecognizer->Compute(face_rois, &embeddings);
embeddingResult.assign(embeddings[0].begin<float>(), embeddings[0].end<float>());
return embeddingResult;
}
catch (std::exception& e) {
std::vector<float> embeddingResult;
embeddingResult.clear();
this->_logger.LogFatal("ANSFaceNet::Feature", e.what(), __FILE__, __LINE__);
return embeddingResult;
}
}
std::vector<FaceResultObject> ANSFaceNet::Match(
const cv::Mat& input,
const std::vector<ANSCENTER::Object>& bBox,
const std::map<std::string, std::string>& userDict) {
std::vector<FaceResultObject> resultObjects;
// Early validation before locking
if (input.empty()) {
return resultObjects;
}
if (input.cols < 10 || input.rows < 10) {
return resultObjects;
}
std::lock_guard<std::recursive_mutex> lock(_mutex);
if (!_isInitialized) {
_logger.LogError("ANSFaceNet::Match", "Model is not initialized", __FILE__, __LINE__);
return resultObjects;
}
try {
// Convert grayscale to 3-channel BGR if needed
cv::Mat processedImage;
if (input.channels() == 1) {
cv::cvtColor(input, processedImage, cv::COLOR_GRAY2BGR);
}
else {
processedImage = input;
}
// Get embeddings
std::vector<std::vector<float>> detectedEmbeddings = Forward(processedImage, bBox);
// Search for matches
std::vector<std::string> names;
std::vector<float> sims;
std::tie(names, sims) = SearchForFaces(detectedEmbeddings);
// Check if we got results
if (names.empty()) {
_logger.LogError("ANSFaceNet::Match", "No face is match", __FILE__, __LINE__);
return resultObjects;
}
// Pre-reserve result space
const size_t resultCount = std::min(names.size(), bBox.size());
resultObjects.reserve(resultCount);
// Build result objects
for (size_t i = 0; i < resultCount; ++i) {
FaceResultObject resultObject;
// Determine if face is known or unknown
const bool isUnknown = (sims[i] > m_knownPersonThresh);
if (isUnknown) {
resultObject.isUnknown = true;
resultObject.userId = "0";
resultObject.userName = "Unknown";
resultObject.confidence = 1.0f; // 100% unknown
}
else {
resultObject.isUnknown = false;
resultObject.userId = names[i];
// Safe map lookup
auto it = userDict.find(names[i]);
resultObject.userName = (it != userDict.end()) ? it->second : names[i];
resultObject.confidence = 1.0f - sims[i];
}
resultObject.similarity = sims[i];
// Copy bounding box (no clamping - keeping original logic)
resultObject.box.x = bBox[i].box.x;
resultObject.box.y = bBox[i].box.y;
resultObject.box.width = bBox[i].box.width;
resultObject.box.height = bBox[i].box.height;
// Copy additional data
resultObject.mask = bBox[i].mask;
resultObject.cameraId = bBox[i].cameraId;
resultObject.trackId = bBox[i].trackId;
resultObject.polygon = bBox[i].polygon;
resultObject.kps = bBox[i].kps;
resultObject.extraInformation = bBox[i].extraInfo;
resultObjects.push_back(std::move(resultObject));
}
return resultObjects;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSFaceNet::Match", e.what(), __FILE__, __LINE__);
return resultObjects;
}
}
cv::Mat ANSFaceNet::GetCropFace(const cv::Mat& input, const ANSCENTER::Object& bBox) {
try {
cv::Mat processedImage;
if (input.channels() == 1) {
cv::cvtColor(input, processedImage, cv::COLOR_GRAY2BGR);
}
else {
processedImage = input;
}
std::vector<ANSCENTER::Object> outputBbox;
outputBbox.push_back(bBox);
std::vector<struct CroppedFace> crFaces;
crFaces.clear();
ANSFRHelper::GetCroppedFaces(processedImage, outputBbox, FACE_WIDTH, FACE_HEIGHT, crFaces);
return crFaces[0].faceMat;
}
catch (std::exception& e) {
cv::Mat result;
this->_logger.LogFatal("ANSFaceNet::GetCropFace", e.what(), __FILE__, __LINE__);
return result;
}
}
bool ANSFaceNet::LoadEngine(const std::string xmlModelPath, bool engineOptimisation) {
try {
if (!FileExist(xmlModelPath)) {
this->_logger.LogError("ANSFaceNet::LoadEngine", "Cannot find the raw XML model file.", __FILE__, __LINE__);
return false;
}
return true;
}
catch (std::exception& e) {
this->_logger.LogFatal("ANSFaceNet::LoadEngine", e.what(), __FILE__, __LINE__);
return false;
}
}
// Private methods, can be replacable
void ANSFaceNet::AddEmbedding(const std::string& className, float embedding[]) {
//std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
// Check if faiss_index is initialized
if (!faiss_index) {
this->_logger.LogFatal("ANSFaceNet::AddEmbedding", "Search_index is not initialized.", __FILE__, __LINE__);
return;
}
// Convert embedding array to vector and check size
std::vector<float> vec(embedding, embedding + FACE_EMBEDDING_SIZE);
if (vec.size() != FACE_EMBEDDING_SIZE) {
this->_logger.LogFatal("ANSFaceNet::AddEmbedding", "Embedding size does not match expected output dimension of 512.", __FILE__, __LINE__);
return;
}
// Add class name and get index
classNames.push_back(className);
// Add embedding to faiss_index
faiss_index->add(1, vec.data());
}
catch (std::exception& e) {
this->_logger.LogFatal("ANSFaceNet::AddEmbedding", e.what(), __FILE__, __LINE__);
}
}
void ANSFaceNet::AddEmbedding(const std::string& className, const std::vector<float>& embedding) {
//std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
// Ensure faiss_index is initialized
if (!faiss_index) {
this->_logger.LogFatal("ANSFaceNet::AddEmbedding", "Search_index is not initialized.", __FILE__, __LINE__);
return;
}
// Check embedding size
if (embedding.size() != FACE_EMBEDDING_SIZE) {
this->_logger.LogFatal("ANSFaceNet::AddEmbedding", "Embedding size does not match expected output dimension of 512.", __FILE__, __LINE__);
return;
}
// Add class name
classNames.push_back(className);
// Add embedding to faiss_index
faiss_index->add(1, embedding.data());
}
catch (const std::exception& e) {
this->_logger.LogFatal("ANSFaceNet::AddEmbedding", e.what(), __FILE__, __LINE__);
}
}
std::vector<std::vector<float>> ANSFaceNet::Forward(const cv::Mat& input, std::vector<ANSCENTER::Object> outputBbox) {
//std::lock_guard<std::recursive_mutex> lock(_mutex);
std::vector<std::vector<float>> detectedEmbeddings;
try {
std::vector<cv::Mat> embeddings;
std::vector<cv::Mat> face_rois;
detectedEmbeddings.clear();
if (outputBbox.size() > 0) {
for (int i = 0; i < outputBbox.size(); i++) {
face_rois.clear();
embeddings.clear();
std::vector<float> embeddingRs;
face_rois.push_back(outputBbox[i].mask);
faceRecognizer->Compute(face_rois, &embeddings);
embeddingRs.assign(embeddings[0].begin<float>(), embeddings[0].end<float>());
detectedEmbeddings.push_back(embeddingRs);
}
}
return detectedEmbeddings;
}
catch (std::exception& e) {
this->_logger.LogFatal("ANSFaceNet::Forward", e.what(), __FILE__, __LINE__);
return detectedEmbeddings;
}
}
std::tuple<std::vector<std::string>, std::vector<float>> ANSFaceNet::SearchForFaces(const std::vector<std::vector<float>>& detectedEmbeddings)
{
//std::lock_guard<std::recursive_mutex> lock(_mutex);
std::vector<std::string> detectedUsers;
std::vector<float> simValues;
try {
// Check if there are class names available
if (!classNames.empty()) {
const int k = 3; // Number of nearest neighbors to retrieve
// Process each detected embedding
for (const auto& embedding : detectedEmbeddings) {
// Prepare vectors to hold search results
std::vector<faiss::idx_t> indices(k);
std::vector<float> distances(k);
// Perform the search for the k nearest neighbors
faiss_index->search(1, embedding.data(), k, distances.data(), indices.data());
// Find the index with the maximum distance
auto min_it = std::min_element(distances.begin(), distances.end());
int best_index = std::distance(distances.begin(), min_it);
// Map the index to the corresponding class name and calculate similarity
std::vector<float> matchEmbedding(faiss_index->d);
faiss_index->reconstruct(indices[best_index], matchEmbedding.data());
float similarity = 1 - CosineSimilarity(embedding, matchEmbedding, false);
detectedUsers.push_back(classNames.at(indices[best_index]));
simValues.push_back(std::abs(similarity));
}
}
else {
// If no class names are available, mark all users as "unknown"
detectedUsers.assign(detectedEmbeddings.size(), "0");
simValues.assign(detectedEmbeddings.size(), 1.0f);
}
return std::make_tuple(detectedUsers, simValues);
}
catch (const std::exception& e) {
// Log the error and return default values for the failed search
detectedUsers.assign(1, "0");
simValues.assign(1, 1.0f);
this->_logger.LogFatal("ANSFaceNet::SearchForFaces", e.what(), __FILE__, __LINE__);
return std::make_tuple(detectedUsers, simValues);
}
}
void ANSFaceNet::Init() {
try {
classNames.clear();
if (faiss_index) {
faiss_index->reset();
}
else {
faiss_index = std::make_unique<faiss::IndexFlatL2>(FACE_EMBEDDING_SIZE);
}
}
catch (std::exception& e) {
this->_logger.LogFatal("ANSFaceNet::Init", e.what(), __FILE__, __LINE__);
}
}
ANSFaceNet::~ANSFaceNet() {
try {
Destroy();
}
catch (std::exception& e) {
this->_logger.LogFatal("ANSFaceNet::~ANSFaceNet()", e.what(), __FILE__, __LINE__);
}
}
bool ANSFaceNet::Destroy() {
try {
// Clear all resources safely
classNames.clear();
// Reset and release faiss_index
if (faiss_index) {
faiss_index->reset();
faiss_index.reset(); // Release shared pointer ownership
}
faceRecognizer.reset();
return true;
}
catch (std::exception& e) {
this->_logger.LogFatal("ANSFaceNet::Destroy", e.what(), __FILE__, __LINE__);
return false;
}
}
}
+49
View File
@@ -0,0 +1,49 @@
#ifndef FACENET_H
#define FACENET_H
#pragma once
#include "ANSFRCommon.h"
#include "hnswlib/hnswlib.h"
#include "cnn.hpp"
#include "face_reid.hpp"
#include "openvino/openvino.hpp"
#include <faiss/IndexFlat.h>
namespace ANSCENTER {
class ANSFaceNet :public ANSFRBase {
public:
virtual bool Initialize(std::string licenseKey, ModelConfig modelConfig, const std::string& modelZipFilePath, const std::string& modelZipPassword, std::string& labelMap) override;
virtual bool LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword)override;
bool OptimizeModel(bool fp16, std::string& optimizedModelFolder);
std::vector<float> Feature(const cv::Mat& image, const ANSCENTER::Object& bBox); // Run inference and get embedding information from a cropped image
std::vector<FaceResultObject> Match(const cv::Mat& input, const std::vector<ANSCENTER::Object>& bBox, const std::map<std::string, std::string>& userDict); // Run inference and get embedding information from a cropped image (the first bbox)
cv::Mat GetCropFace(const cv::Mat& input, const ANSCENTER::Object& bBox);
void Init();
void AddEmbedding(const std::string& className, float embedding[]);
void AddEmbedding(const std::string& className, const std::vector<float>& embedding);
bool UpdateParamater(double knownPersonThreshold) {
_modelConfig.unknownPersonThreshold = knownPersonThreshold;
m_knownPersonThresh = _modelConfig.unknownPersonThreshold;
return true;
}
~ANSFaceNet();
bool Destroy();
private:
bool LoadEngine(const std::string engineFile, bool engineOptimisation = true);
std::vector<std::vector<float>> Forward(const cv::Mat& input, std::vector<ANSCENTER::Object> outputBbox);
std::tuple<std::vector<std::string>, std::vector<float>> SearchForFaces(const std::vector<std::vector<float>>& detectedEmbeddings);
std::string GetOpenVINODevice();
protected:
std::vector<std::string> classNames;
ModelConfig _modelConfig;
std::string _modelFilePath;
ov::Core core;
float m_knownPersonThresh;
const int FACE_WIDTH = 116;
const int FACE_HEIGHT = 116;
const int FACE_EMBEDDING_SIZE = 512;
std::unique_ptr<VectorCNN>faceRecognizer = nullptr;
std::unique_ptr<faiss::IndexFlatL2> faiss_index; // Use shared_ptr
std::recursive_mutex _mutex;
};
}
#endif
+366
View File
@@ -0,0 +1,366 @@
#include "RetinaFaceTRT.h"
// This is standalone Retina Face detector using TensorRT (We will not use as it is not inherit the ANSFD class)
namespace ANSCENTER {
RetinaFaceTRT::RetinaFaceTRT()
{
m_outputBbox.clear();
}
bool RetinaFaceTRT::Initialize(const std::string engineFile,
int frameWidth,
int frameHeight,
std::string inputName,
std::vector<std::string> outputNames,
std::vector<int> inputShape,
int maxBatchSize,
int maxFacesPerScene,
float nms_threshold,
float bbox_threshold)
{
try
{
assert(inputShape.size() == 3);
m_INPUT_C = static_cast<const int>(inputShape[0]);
m_INPUT_H = static_cast<const int>(inputShape[1]);
m_INPUT_W = static_cast<const int>(inputShape[2]);
m_INPUT_SIZE = static_cast<const int>(m_INPUT_C * m_INPUT_H * m_INPUT_W * sizeof(float));
m_OUTPUT_SIZE_BASE = static_cast<const int>((m_INPUT_H / 8 * m_INPUT_W / 8 + m_INPUT_H / 16 * m_INPUT_W / 16 + m_INPUT_H / 32 * m_INPUT_W / 32) * 2);
m_output0 = new float[m_OUTPUT_SIZE_BASE * 4];
m_output1 = new float[m_OUTPUT_SIZE_BASE * 2];
m_maxBatchSize = static_cast<const int>(maxBatchSize);
m_maxFacesPerScene = static_cast<const int>(maxFacesPerScene);
m_nms_threshold = static_cast<const float>(nms_threshold);
m_bbox_threshold = static_cast<const float>(bbox_threshold);
// load engine from .engine file
LoadEngine(engineFile);
// create stream and pre-allocate GPU buffers memory
PreInference(inputName, outputNames);
return true;
}
catch (std::exception& e) {
this->_logger.LogFatal("RetinaFace::Initialize", e.what(), __FILE__, __LINE__);
return false;
}
}
void RetinaFaceTRT::LoadEngine(const std::string engineFile)
{
try {
if (FileExist(engineFile)) {
this->_logger.LogDebug("RetinaFace::LoadEngine", "Loading RetinaFace Engine...", __FILE__, __LINE__);
std::vector<char> trtModelStream_;
size_t size{ 0 };
std::ifstream file(engineFile, std::ios::binary);
if (file.good()) {
file.seekg(0, file.end);
size = file.tellg();
file.seekg(0, file.beg);
trtModelStream_.resize(size);
file.read(trtModelStream_.data(), size);
file.close();
}
nvinfer1::IRuntime* runtime = nvinfer1::createInferRuntime(m_logger);
assert(runtime != nullptr);
m_engine = runtime->deserializeCudaEngine(trtModelStream_.data(), size);
assert(m_engine != nullptr);
m_context = m_engine->createExecutionContext();
assert(m_context != nullptr);
}
else {
this->_logger.LogError("RetinaFace::LoadEngine", "Cant find engine file", __FILE__, __LINE__);
}
}
catch (std::exception& e) {
this->_logger.LogFatal("RetinaFace::Initialize", e.what(), __FILE__, __LINE__);
}
}
void RetinaFaceTRT::PreInference(std::string inputName, std::vector<std::string> outputNames) {
try {
/*
Does not make landmark head as we do not use face alignment.
*/
// Assert
assert(outputNames.size() == 2);
#if NV_TENSORRT_MAJOR >= 10
// TRT 10+: use named tensor API
assert(m_engine->getNbIOTensors() == 3);
// Look up tensor indices by name
for (int i = 0; i < m_engine->getNbIOTensors(); ++i) {
const char* name = m_engine->getIOTensorName(i);
if (name == inputName) inputIndex = i;
else if (name == outputNames[0]) outputIndex0 = i;
else if (name == outputNames[1]) outputIndex1 = i;
}
#else
// TRT 8.x: use binding API
assert(m_engine->getNbBindings() == 3);
inputIndex = m_engine->getBindingIndex(inputName.c_str());
outputIndex0 = m_engine->getBindingIndex(outputNames[0].c_str());
outputIndex1 = m_engine->getBindingIndex(outputNames[1].c_str());
#endif
// Create GPU buffers on device
ANSFRHelper::CheckCudaStatus(cudaMalloc(&buffers[inputIndex], m_maxBatchSize * m_INPUT_SIZE));
ANSFRHelper::CheckCudaStatus(cudaMalloc(&buffers[outputIndex0], m_maxBatchSize * m_OUTPUT_SIZE_BASE * 4 * sizeof(float)));
ANSFRHelper::CheckCudaStatus(cudaMalloc(&buffers[outputIndex1], m_maxBatchSize * m_OUTPUT_SIZE_BASE * 2 * sizeof(float)));
#if NV_TENSORRT_MAJOR >= 10
// TRT 10+: bind tensor addresses
m_context->setTensorAddress(inputName.c_str(), buffers[inputIndex]);
m_context->setTensorAddress(outputNames[0].c_str(), buffers[outputIndex0]);
m_context->setTensorAddress(outputNames[1].c_str(), buffers[outputIndex1]);
#endif
// Create stream
ANSFRHelper::CheckCudaStatus(cudaStreamCreate(&stream));
}
catch (std::exception& e) {
this->_logger.LogFatal("RetinaFace::PreInference", e.what(), __FILE__, __LINE__);
}
}
void RetinaFaceTRT::PreProcess(cv::Mat& img) {
try {
// Release input vector
m_input.release();
// Resize
float w, h, x, y;
if (m_scale_h > m_scale_w) {
w = float(m_INPUT_W);
h = float(m_scale_w * img.rows);
x = 0;
y = float((m_INPUT_H - h) / 2);
}
else {
w = float(m_scale_h * img.cols);
h = float(m_INPUT_H);
x = float((m_INPUT_W - w) / 2);
y = 0;
}
cv::Mat re((int)h, (int)w, CV_8UC3);
cv::resize(img, re, re.size(), 0, 0, cv::INTER_LINEAR);
cv::Mat out((int)m_INPUT_H, (int)m_INPUT_W, CV_8UC3, cv::Scalar(128, 128, 128));
re.copyTo(out(cv::Rect(int(x), int(y), re.cols, re.rows)));
// Normalize
out.convertTo(out, CV_32F);
out = out - cv::Scalar(104, 117, 123);
std::vector<cv::Mat> temp;
cv::split(out, temp);
for (int i = 0; i < temp.size(); i++) {
m_input.push_back(temp[i]);
}
}
catch (std::exception& e) {
this->_logger.LogFatal("RetinaFace::PreProcess", e.what(), __FILE__, __LINE__);
}
}
void RetinaFaceTRT::RunInference(float* input, float* output0, float* output1) {
try {
// DMA input batch data to device, infer on the batch asynchronously, and DMA output back to host
ANSCENTER::ANSFRHelper::CheckCudaStatus(cudaMemcpyAsync(buffers[inputIndex], input, m_maxBatchSize * m_INPUT_SIZE, cudaMemcpyHostToDevice, stream));
#if NV_TENSORRT_MAJOR >= 10
m_context->enqueueV3(stream);
#else
m_context->enqueueV2(buffers, stream, nullptr);
#endif
ANSFRHelper::CheckCudaStatus(cudaMemcpyAsync(output0, buffers[outputIndex0], m_maxBatchSize * m_OUTPUT_SIZE_BASE * 4 * sizeof(float), cudaMemcpyDeviceToHost, stream));
ANSFRHelper::CheckCudaStatus(cudaMemcpyAsync(output1, buffers[outputIndex1], m_maxBatchSize * m_OUTPUT_SIZE_BASE * 2 * sizeof(float), cudaMemcpyDeviceToHost, stream));
cudaStreamSynchronize(stream);
}
catch (std::exception& e) {
this->_logger.LogFatal("RetinaFace::RunInference", e.what(), __FILE__, __LINE__);
}
}
std::vector<struct Bbox> RetinaFaceTRT::FindFace(cv::Mat& img) {
try {
m_outputBbox.clear();
int out_rows = img.rows;
int out_cols = img.cols;
m_frameWidth = static_cast<const int>(out_cols);
m_frameHeight = static_cast<const int>(out_rows);
m_scale_h = (float)(m_INPUT_H) / float(m_frameHeight);
m_scale_w = (float)(m_INPUT_W) / float(m_frameWidth);
PreProcess(img);
RunInference((float*)m_input.ptr<float>(0), m_output0, m_output1);
PostProcessing(m_output0, m_output1);
return m_outputBbox;
}
catch (std::exception& e) {
m_outputBbox.clear();
this->_logger.LogFatal("RetinaFace::FindFace", e.what(), __FILE__, __LINE__);
return m_outputBbox;
}
}
void RetinaFaceTRT::PostProcessing(float* bbox, float* conf) {
try {
m_outputBbox.clear();
std::vector<AnchorBox> anchor;
CreateAnchorRetinaface(anchor, m_INPUT_W, m_INPUT_H);
for (int i = 0; i < anchor.size(); ++i) {
if (*(conf + 1) > m_bbox_threshold) {
AnchorBox tmp = anchor[i];
AnchorBox tmp1;
Bbox result{};
// decode bbox (y - W; x - H)
tmp1.cx = float(tmp.cx + *bbox * 0.1 * tmp.sx);
tmp1.cy = float(tmp.cy + *(bbox + 1) * 0.1 * tmp.sy);
tmp1.sx = float(tmp.sx * exp(*(bbox + 2) * 0.2));
tmp1.sy = float(tmp.sy * exp(*(bbox + 3) * 0.2));
result.y1 = int((tmp1.cx - tmp1.sx / 2) * m_INPUT_W);
result.x1 = int((tmp1.cy - tmp1.sy / 2) * m_INPUT_H);
result.y2 = int((tmp1.cx + tmp1.sx / 2) * m_INPUT_W);
result.x2 = int((tmp1.cy + tmp1.sy / 2) * m_INPUT_H);
// rescale to original size
if (m_scale_h > m_scale_w) {
result.y1 = int(result.y1 / m_scale_w);
result.y2 = int(result.y2 / m_scale_w);
result.x1 = int((result.x1 - (m_INPUT_H - m_scale_w * m_frameHeight) / 2) / m_scale_w);
result.x2 = int((result.x2 - (m_INPUT_H - m_scale_w * m_frameHeight) / 2) / m_scale_w);
}
else {
result.y1 = int((result.y1 - (m_INPUT_W - m_scale_h * m_frameWidth) / 2) / m_scale_h);
result.y2 = int((result.y2 - (m_INPUT_W - m_scale_h * m_frameWidth) / 2) / m_scale_h);
result.x1 = int(result.x1 / m_scale_h);
result.x2 = int(result.x2 / m_scale_h);
}
// Clip object box coordinates to network resolution
result.y1 = CLIP(result.y1, 0, m_frameWidth - 1);
result.x1 = CLIP(result.x1, 0, m_frameHeight - 1);
result.y2 = CLIP(result.y2, 0, m_frameWidth - 1);
result.x2 = CLIP(result.x2, 0, m_frameHeight - 1);
// Get confidence
result.score = *(conf + 1);
// Push to result vector
m_outputBbox.push_back(result);
}
bbox += 4;
conf += 2;
}
std::sort(m_outputBbox.begin(), m_outputBbox.end(), MCMP);
NMS(m_outputBbox, m_nms_threshold);
if (m_outputBbox.size() > m_maxFacesPerScene)
m_outputBbox.resize(m_maxFacesPerScene);
}
catch (std::exception& e) {
m_outputBbox.clear();
this->_logger.LogFatal("RetinaFace::PostProcessing", e.what(), __FILE__, __LINE__);
}
}
void RetinaFaceTRT::CreateAnchorRetinaface(std::vector<AnchorBox>& anchor, int w, int h) {
try {
anchor.clear();
std::vector<std::vector<int>> feature_map(3), min_sizes(3);
float steps[] = { 8, 16, 32 };
for (int i = 0; i < feature_map.size(); ++i) {
feature_map[i].push_back(int(ceil(h / steps[i])));
feature_map[i].push_back(int(ceil(w / steps[i])));
}
std::vector<int> minsize1 = { 10, 20 };
min_sizes[0] = minsize1;
std::vector<int> minsize2 = { 32, 64 };
min_sizes[1] = minsize2;
std::vector<int> minsize3 = { 128, 256 };
min_sizes[2] = minsize3;
for (int k = 0; k < feature_map.size(); ++k) {
std::vector<int> min_size = min_sizes[k];
for (int i = 0; i < feature_map[k][0]; ++i) {
for (int j = 0; j < feature_map[k][1]; ++j) {
for (int l = 0; l < min_size.size(); ++l) {
float s_kx = float(min_size[l] * 1.0 / w);
float s_ky = float(min_size[l] * 1.0 / h);
float cx = float((j + 0.5) * steps[k] / w);
float cy = float((i + 0.5) * steps[k] / h);
AnchorBox axil = { cx, cy, s_kx, s_ky };
anchor.push_back(axil);
}
}
}
}
}
catch (std::exception& e) {
m_outputBbox.clear();
this->_logger.LogFatal("RetinaFace::CreateAnchorRetinaface", e.what(), __FILE__, __LINE__);
}
}
inline bool RetinaFaceTRT::MCMP(Bbox a, Bbox b) {
if (a.score > b.score)
return true;
return false;
}
void RetinaFaceTRT::NMS(std::vector<Bbox>& input_boxes, float NMS_THRESH) {
try {
std::vector<float> vArea(input_boxes.size());
for (int i = 0; i < int(input_boxes.size()); ++i) {
vArea[i] = float((input_boxes.at(i).x2 - input_boxes.at(i).x1 + 1) * (input_boxes.at(i).y2 - input_boxes.at(i).y1 + 1));
}
for (int i = 0; i < int(input_boxes.size()); ++i) {
for (int j = i + 1; j < int(input_boxes.size());) {
float xx1 = (float)(std::max(input_boxes[i].x1, input_boxes[j].x1));
float yy1 = (float)(std::max(input_boxes[i].y1, input_boxes[j].y1));
float xx2 = (float)(std::min(input_boxes[i].x2, input_boxes[j].x2));
float yy2 = (float)(std::min(input_boxes[i].y2, input_boxes[j].y2));
float w = (float)(std::max(float(0), xx2 - xx1 + 1));
float h = (float)(std::max(float(0), yy2 - yy1 + 1));
float inter = (float)(w * h);
float ovr = (float)(inter / (vArea[i] + vArea[j] - inter));
if (ovr >= NMS_THRESH) {
input_boxes.erase(input_boxes.begin() + j);
vArea.erase(vArea.begin() + j);
}
else {
j++;
}
}
}
}
catch (std::exception& e) {
m_outputBbox.clear();
this->_logger.LogFatal("RetinaFace::NMS", e.what(), __FILE__, __LINE__);
}
}
RetinaFaceTRT::~RetinaFaceTRT() {
try {
// Release stream and buffers
ANSFRHelper::CheckCudaStatus(cudaStreamDestroy(stream));
ANSFRHelper::CheckCudaStatus(cudaFree(buffers[inputIndex]));
ANSFRHelper::CheckCudaStatus(cudaFree(buffers[outputIndex0]));
ANSFRHelper::CheckCudaStatus(cudaFree(buffers[outputIndex1]));
// checkCudaStatus(cudaFree(buffers[outputIndex2]));
}
catch (std::exception& e) {
m_outputBbox.clear();
this->_logger.LogFatal("RetinaFace::~RetinaFace", e.what(), __FILE__, __LINE__);
}
}
}
+51
View File
@@ -0,0 +1,51 @@
#ifndef RETINAFACE_H
#define RETINAFACE_H
#pragma once
#include "ANSFRCommon.h"
#define CLIP(a, b, c) (MAX(MIN(a, c), b)) // MIN, MAX defined in opencv
namespace ANSCENTER {
// Retina will be FD subclass
class RetinaFaceTRT {
public:
RetinaFaceTRT();
~RetinaFaceTRT();
std::vector<struct Bbox> FindFace(cv::Mat& img);
bool Initialize(const std::string engineFile,
int frameWidth,
int frameHeight,
std::string inputName,
std::vector<std::string> outputNames,
std::vector<int> inputShape,
int maxBatchSize,
int maxFacesPerScene,
float nms_threshold,
float bbox_threshold);
private:
ANSCENTER::SPDLogger& _logger = ANSCENTER::SPDLogger::GetInstance("ANSFD", true);
TRTLogger m_logger;
int m_frameWidth, m_frameHeight, m_INPUT_C, m_INPUT_H, m_INPUT_W, m_INPUT_SIZE, m_OUTPUT_SIZE_BASE, m_maxBatchSize, m_maxFacesPerScene;
float m_nms_threshold, m_bbox_threshold, m_scale_h, m_scale_w;
cv::Mat m_input;
float* m_output0, * m_output1;
std::vector<struct Bbox> m_outputBbox;
nvinfer1::ICudaEngine* m_engine;
nvinfer1::IExecutionContext* m_context;
cudaStream_t stream;
void* buffers[3];
int inputIndex, outputIndex0, outputIndex1;
private:
void LoadEngine(const std::string engineFile);
void PreInference(std::string inputNames, std::vector<std::string> outputNames);
void RunInference(float* input, float* output0, float* output1);
void PreProcess(cv::Mat& img);
void PostProcessing(float* bbox, float* conf);
void CreateAnchorRetinaface(std::vector<AnchorBox>& anchor, int w, int h);
static inline bool MCMP(Bbox a, Bbox b);
void NMS(std::vector<Bbox>& input_boxes, float NMS_THRESH);
};
}
#endif
+84
View File
@@ -0,0 +1,84 @@
#include"WebClient.h"
namespace ANSCENTER {
#include "webclient.h"
WebSocketClient::WebSocketClient(std::string host, std::string port, std::string url) {
// Look up the domain name
tcp::resolver resolver{ m_ioc };
auto const results = resolver.resolve(host, port);
// Make the connection on the IP address we get from a lookup
auto ep = net::connect(m_ws.next_layer(), results);
// Update the host_ string. This will provide the value of the
// Host HTTP header during the WebSocket handshake.
// See https://tools.ietf.org/html/rfc7230#section-5.4
host += ':' + std::to_string(ep.port());
// Perform the websocket handshake
m_ws.handshake(host, url);
}
std::string WebSocketClient::Send(std::string s) {
// Clear read buffer
m_buffer.clear();
// Send the message
m_ws.write(net::buffer(s));
// Read a message into our buffer
m_ws.read(m_buffer);
return beast::buffers_to_string(m_buffer.data());
}
WebSocketClient::~WebSocketClient() {
// Close the WebSocket connection
m_ws.close(websocket::close_code::normal);
}
HttpClient::HttpClient(std::string host, std::string port, std::string url) {
// Look up the domain name
tcp::resolver resolver{ m_ioc };
m_results = resolver.resolve(host, port);
// Set up an HTTP POST request message
m_req.method(beast::http::verb::post);
m_req.target(url);
m_req.set(http::field::host, host);
m_req.set(http::field::content_type, "application/json");
}
std::string HttpClient::Send(std::string s) {
// Make the connection on the IP address we get from a lookup
m_stream.connect(m_results);
// Clear read buffer
// m_buffer.clear();
// Clear response
m_res = {};
// Prepare response
m_req.body() = s;
m_req.prepare_payload();
// Send the HTTP request to the remote host
http::write(m_stream, m_req);
// Receive the HTTP response
http::read(m_stream, m_buffer, m_res);
// Gracefully close the socket
beast::error_code ec;
m_stream.socket().shutdown(tcp::socket::shutdown_both, ec);
// not_connected happens sometimes, so don't bother reporting it.
if (ec && ec != beast::errc::not_connected)
throw beast::system_error{ ec };
// If we get here then the connection is closed gracefully
return beast::buffers_to_string(m_res.body().data());
}
HttpClient::~HttpClient() {}
}
+45
View File
@@ -0,0 +1,45 @@
#ifndef WEBCLIENT_H
#define WEBCLIENT_H
#pragma once
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/websocket.hpp>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace ANSCENTER {
class WebSocketClient {
public:
WebSocketClient(std::string host, std::string port, std::string url);
~WebSocketClient();
std::string Send(std::string s);
private:
net::io_context m_ioc; // The io_context is required for all I/O
websocket::stream<tcp::socket> m_ws{ m_ioc }; // perform our I/O
beast::flat_buffer m_buffer;
};
class HttpClient {
public:
HttpClient(std::string host, std::string port, std::string url);
~HttpClient();
std::string Send(std::string s);
private:
net::io_context m_ioc; // The io_context is required for all I/O
beast::tcp_stream m_stream{ m_ioc }; // perform our I/O
beast::flat_buffer m_buffer;
http::request<http::string_body> m_req;
http::response<http::dynamic_body> m_res;
tcp::resolver::results_type m_results;
};
}
#endif
+246
View File
@@ -0,0 +1,246 @@
#include "decode.h"
#include "stdio.h"
#include "device_launch_parameters.h"
namespace nvinfer1
{
DecodePlugin::DecodePlugin()
{
}
DecodePlugin::~DecodePlugin()
{
}
// create the plugin at runtime from a byte stream
DecodePlugin::DecodePlugin(const void* data, size_t length)
{
}
void DecodePlugin::serialize(void* buffer) const TRT_NOEXCEPT
{
}
size_t DecodePlugin::getSerializationSize() const TRT_NOEXCEPT
{
return 0;
}
int DecodePlugin::initialize() TRT_NOEXCEPT
{
return 0;
}
Dims DecodePlugin::getOutputDimensions(int index, const Dims* inputs, int nbInputDims) TRT_NOEXCEPT
{
//output the result to channel
int totalCount = 0;
totalCount += decodeplugin::INPUT_H / 8 * decodeplugin::INPUT_W / 8 * 2 * sizeof(decodeplugin::Detection) / sizeof(float);
totalCount += decodeplugin::INPUT_H / 16 * decodeplugin::INPUT_W / 16 * 2 * sizeof(decodeplugin::Detection) / sizeof(float);
totalCount += decodeplugin::INPUT_H / 32 * decodeplugin::INPUT_W / 32 * 2 * sizeof(decodeplugin::Detection) / sizeof(float);
return Dims3(totalCount + 1, 1, 1);
}
// Set plugin namespace
void DecodePlugin::setPluginNamespace(const char* pluginNamespace) TRT_NOEXCEPT
{
mPluginNamespace = pluginNamespace;
}
const char* DecodePlugin::getPluginNamespace() const TRT_NOEXCEPT
{
return mPluginNamespace;
}
// Return the DataType of the plugin output at the requested index
DataType DecodePlugin::getOutputDataType(int index, const nvinfer1::DataType* inputTypes, int nbInputs) const TRT_NOEXCEPT
{
return DataType::kFLOAT;
}
// Return true if output tensor is broadcast across a batch.
bool DecodePlugin::isOutputBroadcastAcrossBatch(int outputIndex, const bool* inputIsBroadcasted, int nbInputs) const TRT_NOEXCEPT
{
return false;
}
// Return true if plugin can use input that is broadcast across batch without replication.
bool DecodePlugin::canBroadcastInputAcrossBatch(int inputIndex) const TRT_NOEXCEPT
{
return false;
}
void DecodePlugin::configurePlugin(const PluginTensorDesc* in, int nbInput, const PluginTensorDesc* out, int nbOutput) TRT_NOEXCEPT
{
}
// Attach the plugin object to an execution context and grant the plugin the access to some context resource.
void DecodePlugin::attachToContext(cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) TRT_NOEXCEPT
{
}
// Detach the plugin object from its execution context.
void DecodePlugin::detachFromContext() TRT_NOEXCEPT {}
const char* DecodePlugin::getPluginType() const TRT_NOEXCEPT
{
return "Decode_TRT";
}
const char* DecodePlugin::getPluginVersion() const TRT_NOEXCEPT
{
return "1";
}
void DecodePlugin::destroy() TRT_NOEXCEPT
{
delete this;
}
// Clone the plugin
IPluginV2IOExt* DecodePlugin::clone() const TRT_NOEXCEPT
{
DecodePlugin* p = new DecodePlugin();
p->setPluginNamespace(mPluginNamespace);
return p;
}
__device__ float Logist(float data) { return 1. / (1. + expf(-data)); };
__global__ void CalDetection(const float* input, float* output, int num_elem, int step, int anchor, int output_elem) {
int idx = threadIdx.x + blockDim.x * blockIdx.x;
if (idx >= num_elem) return;
int h = decodeplugin::INPUT_H / step;
int w = decodeplugin::INPUT_W / step;
int total_grid = h * w;
int bn_idx = idx / total_grid;
idx = idx - bn_idx * total_grid;
int y = idx / w;
int x = idx % w;
const float* cur_input = input + bn_idx * (4 + 2 + 10) * 2 * total_grid;
const float* bbox_reg = &cur_input[0];
const float* cls_reg = &cur_input[2 * 4 * total_grid];
const float* lmk_reg = &cur_input[2 * 4 * total_grid + 2 * 2 * total_grid];
for (int k = 0; k < 2; ++k) {
float conf1 = cls_reg[idx + k * total_grid * 2];
float conf2 = cls_reg[idx + k * total_grid * 2 + total_grid];
conf2 = expf(conf2) / (expf(conf1) + expf(conf2));
if (conf2 <= 0.02) continue;
float* res_count = output + bn_idx * output_elem;
int count = (int)atomicAdd(res_count, 1);
char* data = (char*)res_count + sizeof(float) + count * sizeof(decodeplugin::Detection);
decodeplugin::Detection* det = (decodeplugin::Detection*)(data);
float prior[4];
prior[0] = ((float)x + 0.5) / w;
prior[1] = ((float)y + 0.5) / h;
prior[2] = (float)anchor * (k + 1) / decodeplugin::INPUT_W;
prior[3] = (float)anchor * (k + 1) / decodeplugin::INPUT_H;
//Location
det->bbox[0] = prior[0] + bbox_reg[idx + k * total_grid * 4] * 0.1 * prior[2];
det->bbox[1] = prior[1] + bbox_reg[idx + k * total_grid * 4 + total_grid] * 0.1 * prior[3];
det->bbox[2] = prior[2] * expf(bbox_reg[idx + k * total_grid * 4 + total_grid * 2] * 0.2);
det->bbox[3] = prior[3] * expf(bbox_reg[idx + k * total_grid * 4 + total_grid * 3] * 0.2);
det->bbox[0] -= det->bbox[2] / 2;
det->bbox[1] -= det->bbox[3] / 2;
det->bbox[2] += det->bbox[0];
det->bbox[3] += det->bbox[1];
det->bbox[0] *= decodeplugin::INPUT_W;
det->bbox[1] *= decodeplugin::INPUT_H;
det->bbox[2] *= decodeplugin::INPUT_W;
det->bbox[3] *= decodeplugin::INPUT_H;
det->class_confidence = conf2;
for (int i = 0; i < 10; i += 2) {
det->landmark[i] = prior[0] + lmk_reg[idx + k * total_grid * 10 + total_grid * i] * 0.1 * prior[2];
det->landmark[i + 1] = prior[1] + lmk_reg[idx + k * total_grid * 10 + total_grid * (i + 1)] * 0.1 * prior[3];
det->landmark[i] *= decodeplugin::INPUT_W;
det->landmark[i + 1] *= decodeplugin::INPUT_H;
}
}
}
void DecodePlugin::forwardGpu(const float* const* inputs, float* output, cudaStream_t stream, int batchSize)
{
int num_elem = 0;
int base_step = 8;
int base_anchor = 16;
int thread_count;
int totalCount = 1;
totalCount += decodeplugin::INPUT_H / 8 * decodeplugin::INPUT_W / 8 * 2 * sizeof(decodeplugin::Detection) / sizeof(float);
totalCount += decodeplugin::INPUT_H / 16 * decodeplugin::INPUT_W / 16 * 2 * sizeof(decodeplugin::Detection) / sizeof(float);
totalCount += decodeplugin::INPUT_H / 32 * decodeplugin::INPUT_W / 32 * 2 * sizeof(decodeplugin::Detection) / sizeof(float);
for (int idx = 0; idx < batchSize; ++idx) {
cudaMemsetAsync(output + idx * totalCount, 0, sizeof(float), stream);
}
for (unsigned int i = 0; i < 3; ++i)
{
num_elem = batchSize * decodeplugin::INPUT_H / base_step * decodeplugin::INPUT_W / base_step;
thread_count = (num_elem < thread_count_) ? num_elem : thread_count_;
CalDetection << < (num_elem + thread_count - 1) / thread_count, thread_count, 0, stream >> >
(inputs[i], output, num_elem, base_step, base_anchor, totalCount);
base_step *= 2;
base_anchor *= 4;
}
}
int DecodePlugin::enqueue(int batchSize, const void* const* inputs, void* TRT_CONST_ENQUEUE* outputs, void* workspace, cudaStream_t stream) TRT_NOEXCEPT
{
//GPU
//CUDA_CHECK(cudaStreamSynchronize(stream));
forwardGpu((const float* const*)inputs, (float*)outputs[0], stream, batchSize);
return 0;
};
PluginFieldCollection DecodePluginCreator::mFC{};
std::vector<PluginField> DecodePluginCreator::mPluginAttributes;
DecodePluginCreator::DecodePluginCreator()
{
mPluginAttributes.clear();
mFC.nbFields = mPluginAttributes.size();
mFC.fields = mPluginAttributes.data();
}
const char* DecodePluginCreator::getPluginName() const TRT_NOEXCEPT
{
return "Decode_TRT";
}
const char* DecodePluginCreator::getPluginVersion() const TRT_NOEXCEPT
{
return "1";
}
const PluginFieldCollection* DecodePluginCreator::getFieldNames() TRT_NOEXCEPT
{
return &mFC;
}
IPluginV2IOExt* DecodePluginCreator::createPlugin(const char* name, const PluginFieldCollection* fc) TRT_NOEXCEPT
{
DecodePlugin* obj = new DecodePlugin();
obj->setPluginNamespace(mNamespace.c_str());
return obj;
}
IPluginV2IOExt* DecodePluginCreator::deserializePlugin(const char* name, const void* serialData, size_t serialLength) TRT_NOEXCEPT
{
// This object will be deleted when the network is destroyed, which will
// call PReluPlugin::destroy()
DecodePlugin* obj = new DecodePlugin(serialData, serialLength);
obj->setPluginNamespace(mNamespace.c_str());
return obj;
}
}
+120
View File
@@ -0,0 +1,120 @@
#ifndef _DECODE_CU_H
#define _DECODE_CU_H
#include <string>
#include <vector>
#include "NvInfer.h"
#include "macros.h"
namespace decodeplugin
{
struct alignas(float) Detection{
float bbox[4]; //x1 y1 x2 y2
float class_confidence;
float landmark[10];
};
static const int INPUT_H = 480;
static const int INPUT_W = 640;
}
namespace nvinfer1
{
class DecodePlugin: public IPluginV2IOExt
{
public:
DecodePlugin();
DecodePlugin(const void* data, size_t length);
~DecodePlugin();
int getNbOutputs() const TRT_NOEXCEPT override
{
return 1;
}
Dims getOutputDimensions(int index, const Dims* inputs, int nbInputDims) TRT_NOEXCEPT override;
int initialize() TRT_NOEXCEPT override;
virtual void terminate() TRT_NOEXCEPT override {};
virtual size_t getWorkspaceSize(int maxBatchSize) const TRT_NOEXCEPT override { return 0;}
virtual int enqueue(int batchSize, const void*const * inputs, void*TRT_CONST_ENQUEUE* outputs, void* workspace, cudaStream_t stream) TRT_NOEXCEPT override;
virtual size_t getSerializationSize() const TRT_NOEXCEPT override;
virtual void serialize(void* buffer) const TRT_NOEXCEPT override;
bool supportsFormatCombination(int pos, const PluginTensorDesc* inOut, int nbInputs, int nbOutputs) const TRT_NOEXCEPT override {
return inOut[pos].format == TensorFormat::kLINEAR && inOut[pos].type == DataType::kFLOAT;
}
const char* getPluginType() const TRT_NOEXCEPT override;
const char* getPluginVersion() const TRT_NOEXCEPT override;
void destroy() TRT_NOEXCEPT override;
IPluginV2IOExt* clone() const TRT_NOEXCEPT override;
void setPluginNamespace(const char* pluginNamespace) TRT_NOEXCEPT override;
const char* getPluginNamespace() const TRT_NOEXCEPT override;
DataType getOutputDataType(int index, const nvinfer1::DataType* inputTypes, int nbInputs) const TRT_NOEXCEPT override;
bool isOutputBroadcastAcrossBatch(int outputIndex, const bool* inputIsBroadcasted, int nbInputs) const TRT_NOEXCEPT override;
bool canBroadcastInputAcrossBatch(int inputIndex) const TRT_NOEXCEPT override;
void attachToContext(
cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) TRT_NOEXCEPT override;
void configurePlugin(const PluginTensorDesc* in, int nbInput, const PluginTensorDesc* out, int nbOutput) TRT_NOEXCEPT override;
void detachFromContext() TRT_NOEXCEPT override;
int input_size_;
private:
void forwardGpu(const float *const * inputs, float* output, cudaStream_t stream, int batchSize = 1);
int thread_count_ = 256;
const char* mPluginNamespace;
};
class DecodePluginCreator : public IPluginCreator
{
public:
DecodePluginCreator();
~DecodePluginCreator() override = default;
const char* getPluginName() const TRT_NOEXCEPT override;
const char* getPluginVersion() const TRT_NOEXCEPT override;
const PluginFieldCollection* getFieldNames() TRT_NOEXCEPT override;
IPluginV2IOExt* createPlugin(const char* name, const PluginFieldCollection* fc) TRT_NOEXCEPT override;
IPluginV2IOExt* deserializePlugin(const char* name, const void* serialData, size_t serialLength) TRT_NOEXCEPT override;
void setPluginNamespace(const char* libNamespace) TRT_NOEXCEPT override
{
mNamespace = libNamespace;
}
const char* getPluginNamespace() const TRT_NOEXCEPT override
{
return mNamespace.c_str();
}
private:
std::string mNamespace;
static PluginFieldCollection mFC;
static std::vector<PluginField> mPluginAttributes;
};
REGISTER_TENSORRT_PLUGIN(DecodePluginCreator);
};
#endif
+1490
View File
File diff suppressed because it is too large Load Diff
+7
View File
@@ -0,0 +1,7 @@
#pragma once
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#define NOMINMAX // Prevent windows.h from defining min/max macros
// which break std::min / std::max (C2589)
// Windows Header Files
#include <windows.h>
+12
View File
@@ -0,0 +1,12 @@
#ifndef __MACROS_H
#define __MACROS_H
#if NV_TENSORRT_MAJOR >= 8
#define TRT_NOEXCEPT noexcept
#define TRT_CONST_ENQUEUE const
#else
#define TRT_NOEXCEPT
#define TRT_CONST_ENQUEUE
#endif
#endif // __MACROS_H
+5
View File
@@ -0,0 +1,5 @@
// pch.cpp: source file corresponding to the pre-compiled header
#include "pch.h"
// When you are using pre-compiled headers, this source file is necessary for compilation to succeed.
+18
View File
@@ -0,0 +1,18 @@
// pch.h: This is a precompiled header file.
// Files listed below are compiled only once, improving build performance for future builds.
// This also affects IntelliSense performance, including code completion and many code browsing features.
// However, files listed here are ALL re-compiled if any one of them is updated between builds.
// Do not add files here that you will be updating frequently as this negates the performance advantage.
#ifndef PCH_H
#define PCH_H
// add headers that you want to pre-compile here
#include "framework.h"
#include <memory>
#include <string>
#include <string_view>
#include <vector>
#include <mutex>
#endif //PCH_H
+28831
View File
File diff suppressed because it is too large Load Diff
+252611
View File
File diff suppressed because it is too large Load Diff
+13257
View File
File diff suppressed because it is too large Load Diff
+719
View File
@@ -0,0 +1,719 @@
/*
** 2006 June 7
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** This header file defines the SQLite interface for use by
** shared libraries that want to be imported as extensions into
** an SQLite instance. Shared libraries that intend to be loaded
** as extensions by SQLite should #include this file instead of
** sqlite3.h.
*/
#ifndef SQLITE3EXT_H
#define SQLITE3EXT_H
#include "sqlite3.h"
/*
** The following structure holds pointers to all of the SQLite API
** routines.
**
** WARNING: In order to maintain backwards compatibility, add new
** interfaces to the end of this structure only. If you insert new
** interfaces in the middle of this structure, then older different
** versions of SQLite will not be able to load each other's shared
** libraries!
*/
struct sqlite3_api_routines {
void * (*aggregate_context)(sqlite3_context*,int nBytes);
int (*aggregate_count)(sqlite3_context*);
int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
int (*bind_double)(sqlite3_stmt*,int,double);
int (*bind_int)(sqlite3_stmt*,int,int);
int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
int (*bind_null)(sqlite3_stmt*,int);
int (*bind_parameter_count)(sqlite3_stmt*);
int (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
const char * (*bind_parameter_name)(sqlite3_stmt*,int);
int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
int (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
int (*busy_timeout)(sqlite3*,int ms);
int (*changes)(sqlite3*);
int (*close)(sqlite3*);
int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
int eTextRep,const char*));
int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
int eTextRep,const void*));
const void * (*column_blob)(sqlite3_stmt*,int iCol);
int (*column_bytes)(sqlite3_stmt*,int iCol);
int (*column_bytes16)(sqlite3_stmt*,int iCol);
int (*column_count)(sqlite3_stmt*pStmt);
const char * (*column_database_name)(sqlite3_stmt*,int);
const void * (*column_database_name16)(sqlite3_stmt*,int);
const char * (*column_decltype)(sqlite3_stmt*,int i);
const void * (*column_decltype16)(sqlite3_stmt*,int);
double (*column_double)(sqlite3_stmt*,int iCol);
int (*column_int)(sqlite3_stmt*,int iCol);
sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol);
const char * (*column_name)(sqlite3_stmt*,int);
const void * (*column_name16)(sqlite3_stmt*,int);
const char * (*column_origin_name)(sqlite3_stmt*,int);
const void * (*column_origin_name16)(sqlite3_stmt*,int);
const char * (*column_table_name)(sqlite3_stmt*,int);
const void * (*column_table_name16)(sqlite3_stmt*,int);
const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
const void * (*column_text16)(sqlite3_stmt*,int iCol);
int (*column_type)(sqlite3_stmt*,int iCol);
sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
int (*complete)(const char*sql);
int (*complete16)(const void*sql);
int (*create_collation)(sqlite3*,const char*,int,void*,
int(*)(void*,int,const void*,int,const void*));
int (*create_collation16)(sqlite3*,const void*,int,void*,
int(*)(void*,int,const void*,int,const void*));
int (*create_function)(sqlite3*,const char*,int,int,void*,
void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
void (*xFinal)(sqlite3_context*));
int (*create_function16)(sqlite3*,const void*,int,int,void*,
void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
void (*xFinal)(sqlite3_context*));
int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
int (*data_count)(sqlite3_stmt*pStmt);
sqlite3 * (*db_handle)(sqlite3_stmt*);
int (*declare_vtab)(sqlite3*,const char*);
int (*enable_shared_cache)(int);
int (*errcode)(sqlite3*db);
const char * (*errmsg)(sqlite3*);
const void * (*errmsg16)(sqlite3*);
int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
int (*expired)(sqlite3_stmt*);
int (*finalize)(sqlite3_stmt*pStmt);
void (*free)(void*);
void (*free_table)(char**result);
int (*get_autocommit)(sqlite3*);
void * (*get_auxdata)(sqlite3_context*,int);
int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
int (*global_recover)(void);
void (*interruptx)(sqlite3*);
sqlite_int64 (*last_insert_rowid)(sqlite3*);
const char * (*libversion)(void);
int (*libversion_number)(void);
void *(*malloc)(int);
char * (*mprintf)(const char*,...);
int (*open)(const char*,sqlite3**);
int (*open16)(const void*,sqlite3**);
int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
void *(*realloc)(void*,int);
int (*reset)(sqlite3_stmt*pStmt);
void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
void (*result_double)(sqlite3_context*,double);
void (*result_error)(sqlite3_context*,const char*,int);
void (*result_error16)(sqlite3_context*,const void*,int);
void (*result_int)(sqlite3_context*,int);
void (*result_int64)(sqlite3_context*,sqlite_int64);
void (*result_null)(sqlite3_context*);
void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
void (*result_value)(sqlite3_context*,sqlite3_value*);
void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
const char*,const char*),void*);
void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
char * (*xsnprintf)(int,char*,const char*,...);
int (*step)(sqlite3_stmt*);
int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
char const**,char const**,int*,int*,int*);
void (*thread_cleanup)(void);
int (*total_changes)(sqlite3*);
void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
sqlite_int64),void*);
void * (*user_data)(sqlite3_context*);
const void * (*value_blob)(sqlite3_value*);
int (*value_bytes)(sqlite3_value*);
int (*value_bytes16)(sqlite3_value*);
double (*value_double)(sqlite3_value*);
int (*value_int)(sqlite3_value*);
sqlite_int64 (*value_int64)(sqlite3_value*);
int (*value_numeric_type)(sqlite3_value*);
const unsigned char * (*value_text)(sqlite3_value*);
const void * (*value_text16)(sqlite3_value*);
const void * (*value_text16be)(sqlite3_value*);
const void * (*value_text16le)(sqlite3_value*);
int (*value_type)(sqlite3_value*);
char *(*vmprintf)(const char*,va_list);
/* Added ??? */
int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
/* Added by 3.3.13 */
int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
int (*clear_bindings)(sqlite3_stmt*);
/* Added by 3.4.1 */
int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
void (*xDestroy)(void *));
/* Added by 3.5.0 */
int (*bind_zeroblob)(sqlite3_stmt*,int,int);
int (*blob_bytes)(sqlite3_blob*);
int (*blob_close)(sqlite3_blob*);
int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
int,sqlite3_blob**);
int (*blob_read)(sqlite3_blob*,void*,int,int);
int (*blob_write)(sqlite3_blob*,const void*,int,int);
int (*create_collation_v2)(sqlite3*,const char*,int,void*,
int(*)(void*,int,const void*,int,const void*),
void(*)(void*));
int (*file_control)(sqlite3*,const char*,int,void*);
sqlite3_int64 (*memory_highwater)(int);
sqlite3_int64 (*memory_used)(void);
sqlite3_mutex *(*mutex_alloc)(int);
void (*mutex_enter)(sqlite3_mutex*);
void (*mutex_free)(sqlite3_mutex*);
void (*mutex_leave)(sqlite3_mutex*);
int (*mutex_try)(sqlite3_mutex*);
int (*open_v2)(const char*,sqlite3**,int,const char*);
int (*release_memory)(int);
void (*result_error_nomem)(sqlite3_context*);
void (*result_error_toobig)(sqlite3_context*);
int (*sleep)(int);
void (*soft_heap_limit)(int);
sqlite3_vfs *(*vfs_find)(const char*);
int (*vfs_register)(sqlite3_vfs*,int);
int (*vfs_unregister)(sqlite3_vfs*);
int (*xthreadsafe)(void);
void (*result_zeroblob)(sqlite3_context*,int);
void (*result_error_code)(sqlite3_context*,int);
int (*test_control)(int, ...);
void (*randomness)(int,void*);
sqlite3 *(*context_db_handle)(sqlite3_context*);
int (*extended_result_codes)(sqlite3*,int);
int (*limit)(sqlite3*,int,int);
sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
const char *(*sql)(sqlite3_stmt*);
int (*status)(int,int*,int*,int);
int (*backup_finish)(sqlite3_backup*);
sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
int (*backup_pagecount)(sqlite3_backup*);
int (*backup_remaining)(sqlite3_backup*);
int (*backup_step)(sqlite3_backup*,int);
const char *(*compileoption_get)(int);
int (*compileoption_used)(const char*);
int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
void (*xFinal)(sqlite3_context*),
void(*xDestroy)(void*));
int (*db_config)(sqlite3*,int,...);
sqlite3_mutex *(*db_mutex)(sqlite3*);
int (*db_status)(sqlite3*,int,int*,int*,int);
int (*extended_errcode)(sqlite3*);
void (*log)(int,const char*,...);
sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
const char *(*sourceid)(void);
int (*stmt_status)(sqlite3_stmt*,int,int);
int (*strnicmp)(const char*,const char*,int);
int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
int (*wal_autocheckpoint)(sqlite3*,int);
int (*wal_checkpoint)(sqlite3*,const char*);
void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
int (*vtab_config)(sqlite3*,int op,...);
int (*vtab_on_conflict)(sqlite3*);
/* Version 3.7.16 and later */
int (*close_v2)(sqlite3*);
const char *(*db_filename)(sqlite3*,const char*);
int (*db_readonly)(sqlite3*,const char*);
int (*db_release_memory)(sqlite3*);
const char *(*errstr)(int);
int (*stmt_busy)(sqlite3_stmt*);
int (*stmt_readonly)(sqlite3_stmt*);
int (*stricmp)(const char*,const char*);
int (*uri_boolean)(const char*,const char*,int);
sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
const char *(*uri_parameter)(const char*,const char*);
char *(*xvsnprintf)(int,char*,const char*,va_list);
int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
/* Version 3.8.7 and later */
int (*auto_extension)(void(*)(void));
int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64,
void(*)(void*));
int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64,
void(*)(void*),unsigned char);
int (*cancel_auto_extension)(void(*)(void));
int (*load_extension)(sqlite3*,const char*,const char*,char**);
void *(*malloc64)(sqlite3_uint64);
sqlite3_uint64 (*msize)(void*);
void *(*realloc64)(void*,sqlite3_uint64);
void (*reset_auto_extension)(void);
void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
void(*)(void*));
void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
void(*)(void*), unsigned char);
int (*strglob)(const char*,const char*);
/* Version 3.8.11 and later */
sqlite3_value *(*value_dup)(const sqlite3_value*);
void (*value_free)(sqlite3_value*);
int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
/* Version 3.9.0 and later */
unsigned int (*value_subtype)(sqlite3_value*);
void (*result_subtype)(sqlite3_context*,unsigned int);
/* Version 3.10.0 and later */
int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
int (*strlike)(const char*,const char*,unsigned int);
int (*db_cacheflush)(sqlite3*);
/* Version 3.12.0 and later */
int (*system_errno)(sqlite3*);
/* Version 3.14.0 and later */
int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
char *(*expanded_sql)(sqlite3_stmt*);
/* Version 3.18.0 and later */
void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64);
/* Version 3.20.0 and later */
int (*prepare_v3)(sqlite3*,const char*,int,unsigned int,
sqlite3_stmt**,const char**);
int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int,
sqlite3_stmt**,const void**);
int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
void *(*value_pointer)(sqlite3_value*,const char*);
int (*vtab_nochange)(sqlite3_context*);
int (*value_nochange)(sqlite3_value*);
const char *(*vtab_collation)(sqlite3_index_info*,int);
/* Version 3.24.0 and later */
int (*keyword_count)(void);
int (*keyword_name)(int,const char**,int*);
int (*keyword_check)(const char*,int);
sqlite3_str *(*str_new)(sqlite3*);
char *(*str_finish)(sqlite3_str*);
void (*str_appendf)(sqlite3_str*, const char *zFormat, ...);
void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list);
void (*str_append)(sqlite3_str*, const char *zIn, int N);
void (*str_appendall)(sqlite3_str*, const char *zIn);
void (*str_appendchar)(sqlite3_str*, int N, char C);
void (*str_reset)(sqlite3_str*);
int (*str_errcode)(sqlite3_str*);
int (*str_length)(sqlite3_str*);
char *(*str_value)(sqlite3_str*);
/* Version 3.25.0 and later */
int (*create_window_function)(sqlite3*,const char*,int,int,void*,
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
void (*xFinal)(sqlite3_context*),
void (*xValue)(sqlite3_context*),
void (*xInv)(sqlite3_context*,int,sqlite3_value**),
void(*xDestroy)(void*));
/* Version 3.26.0 and later */
const char *(*normalized_sql)(sqlite3_stmt*);
/* Version 3.28.0 and later */
int (*stmt_isexplain)(sqlite3_stmt*);
int (*value_frombind)(sqlite3_value*);
/* Version 3.30.0 and later */
int (*drop_modules)(sqlite3*,const char**);
/* Version 3.31.0 and later */
sqlite3_int64 (*hard_heap_limit64)(sqlite3_int64);
const char *(*uri_key)(const char*,int);
const char *(*filename_database)(const char*);
const char *(*filename_journal)(const char*);
const char *(*filename_wal)(const char*);
/* Version 3.32.0 and later */
const char *(*create_filename)(const char*,const char*,const char*,
int,const char**);
void (*free_filename)(const char*);
sqlite3_file *(*database_file_object)(const char*);
/* Version 3.34.0 and later */
int (*txn_state)(sqlite3*,const char*);
/* Version 3.36.1 and later */
sqlite3_int64 (*changes64)(sqlite3*);
sqlite3_int64 (*total_changes64)(sqlite3*);
/* Version 3.37.0 and later */
int (*autovacuum_pages)(sqlite3*,
unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
void*, void(*)(void*));
/* Version 3.38.0 and later */
int (*error_offset)(sqlite3*);
int (*vtab_rhs_value)(sqlite3_index_info*,int,sqlite3_value**);
int (*vtab_distinct)(sqlite3_index_info*);
int (*vtab_in)(sqlite3_index_info*,int,int);
int (*vtab_in_first)(sqlite3_value*,sqlite3_value**);
int (*vtab_in_next)(sqlite3_value*,sqlite3_value**);
/* Version 3.39.0 and later */
int (*deserialize)(sqlite3*,const char*,unsigned char*,
sqlite3_int64,sqlite3_int64,unsigned);
unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*,
unsigned int);
const char *(*db_name)(sqlite3*,int);
/* Version 3.40.0 and later */
int (*value_encoding)(sqlite3_value*);
/* Version 3.41.0 and later */
int (*is_interrupted)(sqlite3*);
/* Version 3.43.0 and later */
int (*stmt_explain)(sqlite3_stmt*,int);
/* Version 3.44.0 and later */
void *(*get_clientdata)(sqlite3*,const char*);
int (*set_clientdata)(sqlite3*, const char*, void*, void(*)(void*));
};
/*
** This is the function signature used for all extension entry points. It
** is also defined in the file "loadext.c".
*/
typedef int (*sqlite3_loadext_entry)(
sqlite3 *db, /* Handle to the database. */
char **pzErrMsg, /* Used to set error string on failure. */
const sqlite3_api_routines *pThunk /* Extension API function pointers. */
);
/*
** The following macros redefine the API routines so that they are
** redirected through the global sqlite3_api structure.
**
** This header file is also used by the loadext.c source file
** (part of the main SQLite library - not an extension) so that
** it can get access to the sqlite3_api_routines structure
** definition. But the main library does not want to redefine
** the API. So the redefinition macros are only valid if the
** SQLITE_CORE macros is undefined.
*/
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
#define sqlite3_aggregate_context sqlite3_api->aggregate_context
#ifndef SQLITE_OMIT_DEPRECATED
#define sqlite3_aggregate_count sqlite3_api->aggregate_count
#endif
#define sqlite3_bind_blob sqlite3_api->bind_blob
#define sqlite3_bind_double sqlite3_api->bind_double
#define sqlite3_bind_int sqlite3_api->bind_int
#define sqlite3_bind_int64 sqlite3_api->bind_int64
#define sqlite3_bind_null sqlite3_api->bind_null
#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
#define sqlite3_bind_text sqlite3_api->bind_text
#define sqlite3_bind_text16 sqlite3_api->bind_text16
#define sqlite3_bind_value sqlite3_api->bind_value
#define sqlite3_busy_handler sqlite3_api->busy_handler
#define sqlite3_busy_timeout sqlite3_api->busy_timeout
#define sqlite3_changes sqlite3_api->changes
#define sqlite3_close sqlite3_api->close
#define sqlite3_collation_needed sqlite3_api->collation_needed
#define sqlite3_collation_needed16 sqlite3_api->collation_needed16
#define sqlite3_column_blob sqlite3_api->column_blob
#define sqlite3_column_bytes sqlite3_api->column_bytes
#define sqlite3_column_bytes16 sqlite3_api->column_bytes16
#define sqlite3_column_count sqlite3_api->column_count
#define sqlite3_column_database_name sqlite3_api->column_database_name
#define sqlite3_column_database_name16 sqlite3_api->column_database_name16
#define sqlite3_column_decltype sqlite3_api->column_decltype
#define sqlite3_column_decltype16 sqlite3_api->column_decltype16
#define sqlite3_column_double sqlite3_api->column_double
#define sqlite3_column_int sqlite3_api->column_int
#define sqlite3_column_int64 sqlite3_api->column_int64
#define sqlite3_column_name sqlite3_api->column_name
#define sqlite3_column_name16 sqlite3_api->column_name16
#define sqlite3_column_origin_name sqlite3_api->column_origin_name
#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
#define sqlite3_column_table_name sqlite3_api->column_table_name
#define sqlite3_column_table_name16 sqlite3_api->column_table_name16
#define sqlite3_column_text sqlite3_api->column_text
#define sqlite3_column_text16 sqlite3_api->column_text16
#define sqlite3_column_type sqlite3_api->column_type
#define sqlite3_column_value sqlite3_api->column_value
#define sqlite3_commit_hook sqlite3_api->commit_hook
#define sqlite3_complete sqlite3_api->complete
#define sqlite3_complete16 sqlite3_api->complete16
#define sqlite3_create_collation sqlite3_api->create_collation
#define sqlite3_create_collation16 sqlite3_api->create_collation16
#define sqlite3_create_function sqlite3_api->create_function
#define sqlite3_create_function16 sqlite3_api->create_function16
#define sqlite3_create_module sqlite3_api->create_module
#define sqlite3_create_module_v2 sqlite3_api->create_module_v2
#define sqlite3_data_count sqlite3_api->data_count
#define sqlite3_db_handle sqlite3_api->db_handle
#define sqlite3_declare_vtab sqlite3_api->declare_vtab
#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
#define sqlite3_errcode sqlite3_api->errcode
#define sqlite3_errmsg sqlite3_api->errmsg
#define sqlite3_errmsg16 sqlite3_api->errmsg16
#define sqlite3_exec sqlite3_api->exec
#ifndef SQLITE_OMIT_DEPRECATED
#define sqlite3_expired sqlite3_api->expired
#endif
#define sqlite3_finalize sqlite3_api->finalize
#define sqlite3_free sqlite3_api->free
#define sqlite3_free_table sqlite3_api->free_table
#define sqlite3_get_autocommit sqlite3_api->get_autocommit
#define sqlite3_get_auxdata sqlite3_api->get_auxdata
#define sqlite3_get_table sqlite3_api->get_table
#ifndef SQLITE_OMIT_DEPRECATED
#define sqlite3_global_recover sqlite3_api->global_recover
#endif
#define sqlite3_interrupt sqlite3_api->interruptx
#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
#define sqlite3_libversion sqlite3_api->libversion
#define sqlite3_libversion_number sqlite3_api->libversion_number
#define sqlite3_malloc sqlite3_api->malloc
#define sqlite3_mprintf sqlite3_api->mprintf
#define sqlite3_open sqlite3_api->open
#define sqlite3_open16 sqlite3_api->open16
#define sqlite3_prepare sqlite3_api->prepare
#define sqlite3_prepare16 sqlite3_api->prepare16
#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
#define sqlite3_profile sqlite3_api->profile
#define sqlite3_progress_handler sqlite3_api->progress_handler
#define sqlite3_realloc sqlite3_api->realloc
#define sqlite3_reset sqlite3_api->reset
#define sqlite3_result_blob sqlite3_api->result_blob
#define sqlite3_result_double sqlite3_api->result_double
#define sqlite3_result_error sqlite3_api->result_error
#define sqlite3_result_error16 sqlite3_api->result_error16
#define sqlite3_result_int sqlite3_api->result_int
#define sqlite3_result_int64 sqlite3_api->result_int64
#define sqlite3_result_null sqlite3_api->result_null
#define sqlite3_result_text sqlite3_api->result_text
#define sqlite3_result_text16 sqlite3_api->result_text16
#define sqlite3_result_text16be sqlite3_api->result_text16be
#define sqlite3_result_text16le sqlite3_api->result_text16le
#define sqlite3_result_value sqlite3_api->result_value
#define sqlite3_rollback_hook sqlite3_api->rollback_hook
#define sqlite3_set_authorizer sqlite3_api->set_authorizer
#define sqlite3_set_auxdata sqlite3_api->set_auxdata
#define sqlite3_snprintf sqlite3_api->xsnprintf
#define sqlite3_step sqlite3_api->step
#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
#define sqlite3_total_changes sqlite3_api->total_changes
#define sqlite3_trace sqlite3_api->trace
#ifndef SQLITE_OMIT_DEPRECATED
#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
#endif
#define sqlite3_update_hook sqlite3_api->update_hook
#define sqlite3_user_data sqlite3_api->user_data
#define sqlite3_value_blob sqlite3_api->value_blob
#define sqlite3_value_bytes sqlite3_api->value_bytes
#define sqlite3_value_bytes16 sqlite3_api->value_bytes16
#define sqlite3_value_double sqlite3_api->value_double
#define sqlite3_value_int sqlite3_api->value_int
#define sqlite3_value_int64 sqlite3_api->value_int64
#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
#define sqlite3_value_text sqlite3_api->value_text
#define sqlite3_value_text16 sqlite3_api->value_text16
#define sqlite3_value_text16be sqlite3_api->value_text16be
#define sqlite3_value_text16le sqlite3_api->value_text16le
#define sqlite3_value_type sqlite3_api->value_type
#define sqlite3_vmprintf sqlite3_api->vmprintf
#define sqlite3_vsnprintf sqlite3_api->xvsnprintf
#define sqlite3_overload_function sqlite3_api->overload_function
#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
#define sqlite3_clear_bindings sqlite3_api->clear_bindings
#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
#define sqlite3_blob_bytes sqlite3_api->blob_bytes
#define sqlite3_blob_close sqlite3_api->blob_close
#define sqlite3_blob_open sqlite3_api->blob_open
#define sqlite3_blob_read sqlite3_api->blob_read
#define sqlite3_blob_write sqlite3_api->blob_write
#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
#define sqlite3_file_control sqlite3_api->file_control
#define sqlite3_memory_highwater sqlite3_api->memory_highwater
#define sqlite3_memory_used sqlite3_api->memory_used
#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
#define sqlite3_mutex_enter sqlite3_api->mutex_enter
#define sqlite3_mutex_free sqlite3_api->mutex_free
#define sqlite3_mutex_leave sqlite3_api->mutex_leave
#define sqlite3_mutex_try sqlite3_api->mutex_try
#define sqlite3_open_v2 sqlite3_api->open_v2
#define sqlite3_release_memory sqlite3_api->release_memory
#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
#define sqlite3_sleep sqlite3_api->sleep
#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
#define sqlite3_vfs_find sqlite3_api->vfs_find
#define sqlite3_vfs_register sqlite3_api->vfs_register
#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
#define sqlite3_threadsafe sqlite3_api->xthreadsafe
#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
#define sqlite3_result_error_code sqlite3_api->result_error_code
#define sqlite3_test_control sqlite3_api->test_control
#define sqlite3_randomness sqlite3_api->randomness
#define sqlite3_context_db_handle sqlite3_api->context_db_handle
#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
#define sqlite3_limit sqlite3_api->limit
#define sqlite3_next_stmt sqlite3_api->next_stmt
#define sqlite3_sql sqlite3_api->sql
#define sqlite3_status sqlite3_api->status
#define sqlite3_backup_finish sqlite3_api->backup_finish
#define sqlite3_backup_init sqlite3_api->backup_init
#define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
#define sqlite3_backup_remaining sqlite3_api->backup_remaining
#define sqlite3_backup_step sqlite3_api->backup_step
#define sqlite3_compileoption_get sqlite3_api->compileoption_get
#define sqlite3_compileoption_used sqlite3_api->compileoption_used
#define sqlite3_create_function_v2 sqlite3_api->create_function_v2
#define sqlite3_db_config sqlite3_api->db_config
#define sqlite3_db_mutex sqlite3_api->db_mutex
#define sqlite3_db_status sqlite3_api->db_status
#define sqlite3_extended_errcode sqlite3_api->extended_errcode
#define sqlite3_log sqlite3_api->log
#define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
#define sqlite3_sourceid sqlite3_api->sourceid
#define sqlite3_stmt_status sqlite3_api->stmt_status
#define sqlite3_strnicmp sqlite3_api->strnicmp
#define sqlite3_unlock_notify sqlite3_api->unlock_notify
#define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
#define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
#define sqlite3_wal_hook sqlite3_api->wal_hook
#define sqlite3_blob_reopen sqlite3_api->blob_reopen
#define sqlite3_vtab_config sqlite3_api->vtab_config
#define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
/* Version 3.7.16 and later */
#define sqlite3_close_v2 sqlite3_api->close_v2
#define sqlite3_db_filename sqlite3_api->db_filename
#define sqlite3_db_readonly sqlite3_api->db_readonly
#define sqlite3_db_release_memory sqlite3_api->db_release_memory
#define sqlite3_errstr sqlite3_api->errstr
#define sqlite3_stmt_busy sqlite3_api->stmt_busy
#define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
#define sqlite3_stricmp sqlite3_api->stricmp
#define sqlite3_uri_boolean sqlite3_api->uri_boolean
#define sqlite3_uri_int64 sqlite3_api->uri_int64
#define sqlite3_uri_parameter sqlite3_api->uri_parameter
#define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
#define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
/* Version 3.8.7 and later */
#define sqlite3_auto_extension sqlite3_api->auto_extension
#define sqlite3_bind_blob64 sqlite3_api->bind_blob64
#define sqlite3_bind_text64 sqlite3_api->bind_text64
#define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
#define sqlite3_load_extension sqlite3_api->load_extension
#define sqlite3_malloc64 sqlite3_api->malloc64
#define sqlite3_msize sqlite3_api->msize
#define sqlite3_realloc64 sqlite3_api->realloc64
#define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
#define sqlite3_result_blob64 sqlite3_api->result_blob64
#define sqlite3_result_text64 sqlite3_api->result_text64
#define sqlite3_strglob sqlite3_api->strglob
/* Version 3.8.11 and later */
#define sqlite3_value_dup sqlite3_api->value_dup
#define sqlite3_value_free sqlite3_api->value_free
#define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
#define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
/* Version 3.9.0 and later */
#define sqlite3_value_subtype sqlite3_api->value_subtype
#define sqlite3_result_subtype sqlite3_api->result_subtype
/* Version 3.10.0 and later */
#define sqlite3_status64 sqlite3_api->status64
#define sqlite3_strlike sqlite3_api->strlike
#define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
/* Version 3.12.0 and later */
#define sqlite3_system_errno sqlite3_api->system_errno
/* Version 3.14.0 and later */
#define sqlite3_trace_v2 sqlite3_api->trace_v2
#define sqlite3_expanded_sql sqlite3_api->expanded_sql
/* Version 3.18.0 and later */
#define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
/* Version 3.20.0 and later */
#define sqlite3_prepare_v3 sqlite3_api->prepare_v3
#define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
#define sqlite3_bind_pointer sqlite3_api->bind_pointer
#define sqlite3_result_pointer sqlite3_api->result_pointer
#define sqlite3_value_pointer sqlite3_api->value_pointer
/* Version 3.22.0 and later */
#define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
#define sqlite3_value_nochange sqlite3_api->value_nochange
#define sqlite3_vtab_collation sqlite3_api->vtab_collation
/* Version 3.24.0 and later */
#define sqlite3_keyword_count sqlite3_api->keyword_count
#define sqlite3_keyword_name sqlite3_api->keyword_name
#define sqlite3_keyword_check sqlite3_api->keyword_check
#define sqlite3_str_new sqlite3_api->str_new
#define sqlite3_str_finish sqlite3_api->str_finish
#define sqlite3_str_appendf sqlite3_api->str_appendf
#define sqlite3_str_vappendf sqlite3_api->str_vappendf
#define sqlite3_str_append sqlite3_api->str_append
#define sqlite3_str_appendall sqlite3_api->str_appendall
#define sqlite3_str_appendchar sqlite3_api->str_appendchar
#define sqlite3_str_reset sqlite3_api->str_reset
#define sqlite3_str_errcode sqlite3_api->str_errcode
#define sqlite3_str_length sqlite3_api->str_length
#define sqlite3_str_value sqlite3_api->str_value
/* Version 3.25.0 and later */
#define sqlite3_create_window_function sqlite3_api->create_window_function
/* Version 3.26.0 and later */
#define sqlite3_normalized_sql sqlite3_api->normalized_sql
/* Version 3.28.0 and later */
#define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
#define sqlite3_value_frombind sqlite3_api->value_frombind
/* Version 3.30.0 and later */
#define sqlite3_drop_modules sqlite3_api->drop_modules
/* Version 3.31.0 and later */
#define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
#define sqlite3_uri_key sqlite3_api->uri_key
#define sqlite3_filename_database sqlite3_api->filename_database
#define sqlite3_filename_journal sqlite3_api->filename_journal
#define sqlite3_filename_wal sqlite3_api->filename_wal
/* Version 3.32.0 and later */
#define sqlite3_create_filename sqlite3_api->create_filename
#define sqlite3_free_filename sqlite3_api->free_filename
#define sqlite3_database_file_object sqlite3_api->database_file_object
/* Version 3.34.0 and later */
#define sqlite3_txn_state sqlite3_api->txn_state
/* Version 3.36.1 and later */
#define sqlite3_changes64 sqlite3_api->changes64
#define sqlite3_total_changes64 sqlite3_api->total_changes64
/* Version 3.37.0 and later */
#define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
/* Version 3.38.0 and later */
#define sqlite3_error_offset sqlite3_api->error_offset
#define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value
#define sqlite3_vtab_distinct sqlite3_api->vtab_distinct
#define sqlite3_vtab_in sqlite3_api->vtab_in
#define sqlite3_vtab_in_first sqlite3_api->vtab_in_first
#define sqlite3_vtab_in_next sqlite3_api->vtab_in_next
/* Version 3.39.0 and later */
#ifndef SQLITE_OMIT_DESERIALIZE
#define sqlite3_deserialize sqlite3_api->deserialize
#define sqlite3_serialize sqlite3_api->serialize
#endif
#define sqlite3_db_name sqlite3_api->db_name
/* Version 3.40.0 and later */
#define sqlite3_value_encoding sqlite3_api->value_encoding
/* Version 3.41.0 and later */
#define sqlite3_is_interrupted sqlite3_api->is_interrupted
/* Version 3.43.0 and later */
#define sqlite3_stmt_explain sqlite3_api->stmt_explain
/* Version 3.44.0 and later */
#define sqlite3_get_clientdata sqlite3_api->get_clientdata
#define sqlite3_set_clientdata sqlite3_api->set_clientdata
#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
/* This case when the file really is being compiled as a loadable
** extension */
# define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0;
# define SQLITE_EXTENSION_INIT2(v) sqlite3_api=v;
# define SQLITE_EXTENSION_INIT3 \
extern const sqlite3_api_routines *sqlite3_api;
#else
/* This case when the file is being statically linked into the
** application */
# define SQLITE_EXTENSION_INIT1 /*no-op*/
# define SQLITE_EXTENSION_INIT2(v) (void)v; /* unused parameter */
# define SQLITE_EXTENSION_INIT3 /*no-op*/
#endif
#endif /* SQLITE3EXT_H */
+38
View File
@@ -0,0 +1,38 @@
# ANSIO — IO Box communication DLL (cJSON + mbedtls)
add_library(ANSIO SHARED
dllmain.cpp
pch.cpp
pch.h
framework.h
# IOBox sources (embedded)
IOBox/cJSON.c
IOBox/cJSON_Utils.c
IOBox/iobox_api.cpp
IOBox/iobox_api.h
IOBox/mbedtls/aes.c
IOBox/mbedtls/base64.c
IOBox/mbedtls/sha256.c
)
target_include_directories(ANSIO PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${SHARED_INCLUDE_DIR}
)
target_include_directories(ANSIO PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/IOBox
)
target_link_libraries(ANSIO
PRIVATE ANSLicensingSystem
PRIVATE anslicensing
PRIVATE labview
)
target_compile_definitions(ANSIO PRIVATE UNICODE _UNICODE ANSIO_EXPORTS _USRDLL)
if(WIN32)
target_link_libraries(ANSIO PRIVATE ${WIN_COMMON_LIBS})
else()
target_link_libraries(ANSIO PRIVATE ${UNIX_COMMON_LIBS})
endif()
target_precompile_headers(ANSIO PRIVATE pch.h)
+3143
View File
File diff suppressed because it is too large Load Diff
+300
View File
@@ -0,0 +1,300 @@
/*
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef cJSON__h
#define cJSON__h
#ifdef __cplusplus
extern "C"
{
#endif
#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
#define __WINDOWS__
#endif
#ifdef __WINDOWS__
/* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention. For windows you have 3 define options:
CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols
CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default)
CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol
For *nix builds that support visibility attribute, you can define similar behavior by
setting default visibility to hidden by adding
-fvisibility=hidden (for gcc)
or
-xldscope=hidden (for sun cc)
to CFLAGS
then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does
*/
#define CJSON_CDECL __cdecl
#define CJSON_STDCALL __stdcall
/* export symbols by default, this is necessary for copy pasting the C and header file */
#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS)
#define CJSON_EXPORT_SYMBOLS
#endif
#if defined(CJSON_HIDE_SYMBOLS)
#define CJSON_PUBLIC(type) type CJSON_STDCALL
#elif defined(CJSON_EXPORT_SYMBOLS)
#define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL
#elif defined(CJSON_IMPORT_SYMBOLS)
#define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL
#endif
#else /* !__WINDOWS__ */
#define CJSON_CDECL
#define CJSON_STDCALL
#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY)
#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type
#else
#define CJSON_PUBLIC(type) type
#endif
#endif
/* project version */
#define CJSON_VERSION_MAJOR 1
#define CJSON_VERSION_MINOR 7
#define CJSON_VERSION_PATCH 18
#include <stddef.h>
/* cJSON Types: */
#define cJSON_Invalid (0)
#define cJSON_False (1 << 0)
#define cJSON_True (1 << 1)
#define cJSON_NULL (1 << 2)
#define cJSON_Number (1 << 3)
#define cJSON_String (1 << 4)
#define cJSON_Array (1 << 5)
#define cJSON_Object (1 << 6)
#define cJSON_Raw (1 << 7) /* raw json */
#define cJSON_IsReference 256
#define cJSON_StringIsConst 512
/* The cJSON structure: */
typedef struct cJSON
{
/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
struct cJSON *next;
struct cJSON *prev;
/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
struct cJSON *child;
/* The type of the item, as above. */
int type;
/* The item's string, if type==cJSON_String and type == cJSON_Raw */
char *valuestring;
/* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
int valueint;
/* The item's number, if type==cJSON_Number */
double valuedouble;
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
char *string;
} cJSON;
typedef struct cJSON_Hooks
{
/* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */
void *(CJSON_CDECL *malloc_fn)(size_t sz);
void (CJSON_CDECL *free_fn)(void *ptr);
} cJSON_Hooks;
typedef int cJSON_bool;
/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them.
* This is to prevent stack overflows. */
#ifndef CJSON_NESTING_LIMIT
#define CJSON_NESTING_LIMIT 1000
#endif
/* returns the version of cJSON as a string */
CJSON_PUBLIC(const char*) cJSON_Version(void);
/* Supply malloc, realloc and free functions to cJSON */
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);
/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length);
/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated);
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated);
/* Render a cJSON entity to text for transfer/storage. */
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
/* Render a cJSON entity to text for transfer/storage without any formatting. */
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */
/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format);
/* Delete a cJSON entity and all subentities. */
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item);
/* Returns the number of items in an array (or object). */
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
/* Get item "string" from object. Case insensitive. */
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string);
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
/* Check item type and return its value */
CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item);
CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item);
/* These functions check the type of an item */
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item);
/* These calls create a cJSON item of the appropriate type. */
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);
/* raw json */
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
/* Create a string where valuestring references a string so
* it will not be freed by cJSON_Delete */
CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string);
/* Create an object/array that only references it's elements so
* they will not be freed by cJSON_Delete */
CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child);
CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child);
/* These utilities create an Array of count items.
* The parameter count cannot be greater than the number of elements in the number array, otherwise array access will be out of bounds.*/
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count);
/* Append item to the specified array/object. */
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object.
* WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before
* writing to `item->string` */
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item);
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
/* Remove/Detach items from Arrays/Objects. */
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);
/* Update array items. */
CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement);
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem);
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem);
/* Duplicate a cJSON item */
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse);
/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
* need to be released. With recurse!=0, it will duplicate any children connected to the item.
* The item->next and ->prev pointers are always zero on return from Duplicate. */
/* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal.
* case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */
CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive);
/* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n') from strings.
* The input pointer json cannot point to a read-only address area, such as a string constant,
* but should point to a readable and writable address area. */
CJSON_PUBLIC(void) cJSON_Minify(char *json);
/* Helper functions for creating and adding items to an object at the same time.
* They return the added item or NULL on failure. */
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean);
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number);
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw);
CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name);
/* When assigning an integer value, it needs to be propagated to valuedouble too. */
#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number))
/* helper for the cJSON_SetNumberValue macro */
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number);
#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number))
/* Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String */
CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring);
/* If the object is not a boolean type this does nothing and returns cJSON_Invalid else it returns the new type*/
#define cJSON_SetBoolValue(object, boolValue) ( \
(object != NULL && ((object)->type & (cJSON_False|cJSON_True))) ? \
(object)->type=((object)->type &(~(cJSON_False|cJSON_True)))|((boolValue)?cJSON_True:cJSON_False) : \
cJSON_Invalid\
)
/* Macro for iterating over an array or object */
#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
/* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */
CJSON_PUBLIC(void *) cJSON_malloc(size_t size);
CJSON_PUBLIC(void) cJSON_free(void *object);
#ifdef __cplusplus
}
#endif
#endif
File diff suppressed because it is too large Load Diff
+88
View File
@@ -0,0 +1,88 @@
/*
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef cJSON_Utils__h
#define cJSON_Utils__h
#ifdef __cplusplus
extern "C"
{
#endif
#include "cJSON.h"
/* Implement RFC6901 (https://tools.ietf.org/html/rfc6901) JSON Pointer spec. */
CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON * const object, const char *pointer);
CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointerCaseSensitive(cJSON * const object, const char *pointer);
/* Implement RFC6902 (https://tools.ietf.org/html/rfc6902) JSON Patch spec. */
/* NOTE: This modifies objects in 'from' and 'to' by sorting the elements by their key */
CJSON_PUBLIC(cJSON *) cJSONUtils_GeneratePatches(cJSON * const from, cJSON * const to);
CJSON_PUBLIC(cJSON *) cJSONUtils_GeneratePatchesCaseSensitive(cJSON * const from, cJSON * const to);
/* Utility for generating patch array entries. */
CJSON_PUBLIC(void) cJSONUtils_AddPatchToArray(cJSON * const array, const char * const operation, const char * const path, const cJSON * const value);
/* Returns 0 for success. */
CJSON_PUBLIC(int) cJSONUtils_ApplyPatches(cJSON * const object, const cJSON * const patches);
CJSON_PUBLIC(int) cJSONUtils_ApplyPatchesCaseSensitive(cJSON * const object, const cJSON * const patches);
/*
// Note that ApplyPatches is NOT atomic on failure. To implement an atomic ApplyPatches, use:
//int cJSONUtils_AtomicApplyPatches(cJSON **object, cJSON *patches)
//{
// cJSON *modme = cJSON_Duplicate(*object, 1);
// int error = cJSONUtils_ApplyPatches(modme, patches);
// if (!error)
// {
// cJSON_Delete(*object);
// *object = modme;
// }
// else
// {
// cJSON_Delete(modme);
// }
//
// return error;
//}
// Code not added to library since this strategy is a LOT slower.
*/
/* Implement RFC7386 (https://tools.ietf.org/html/rfc7396) JSON Merge Patch spec. */
/* target will be modified by patch. return value is new ptr for target. */
CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatch(cJSON *target, const cJSON * const patch);
CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatchCaseSensitive(cJSON *target, const cJSON * const patch);
/* generates a patch to move from -> to */
/* NOTE: This modifies objects in 'from' and 'to' by sorting the elements by their key */
CJSON_PUBLIC(cJSON *) cJSONUtils_GenerateMergePatch(cJSON * const from, cJSON * const to);
CJSON_PUBLIC(cJSON *) cJSONUtils_GenerateMergePatchCaseSensitive(cJSON * const from, cJSON * const to);
/* Given a root object and a target object, construct a pointer from one to the other. */
CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(const cJSON * const object, const cJSON * const target);
/* Sorts the members of the object into alphabetical order. */
CJSON_PUBLIC(void) cJSONUtils_SortObject(cJSON * const object);
CJSON_PUBLIC(void) cJSONUtils_SortObjectCaseSensitive(cJSON * const object);
#ifdef __cplusplus
}
#endif
#endif
File diff suppressed because it is too large Load Diff
+215
View File
@@ -0,0 +1,215 @@
#ifndef _IO_BOX_API_H_
#define _IO_BOX_API_H_
#define ANSIO_API __declspec(dllexport)
#include "LabVIEWHeader/extcode.h"
#include <vector>
#include <string>
#include <iostream>
#include <mutex>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h> // For PIP_ADAPTER_INFO
#include <chrono>
#include <memory>
#include <unordered_map>
#include "ANSLicense.h"
#pragma comment(lib,"WS2_32")
#pragma comment(lib, "Iphlpapi.lib")
// #pragma comment(lib, "Ws2_32.lib")
// #define TEST_FIX_IP
#define DEVICE_TCP_PORT 502
#define DEVICE_DATA_ADDRESS 100
#define DEVICE_DATA_LENGTH 17
#define MESSAGE_CRYPT_ENABLE 0
// Configuration constants - make these configurable if needed
namespace IoboxConfig {
constexpr int MAX_RETRIES = 30; // Reduced from 50 for better timeout control
constexpr int RETRY_DELAY_MS = 100; // Increased from 50ms for better reliability
constexpr int TOTAL_TIMEOUT_MS = 3000; // 3 seconds total timeout
}
namespace ANSCENTER {
typedef struct {
std::string model;
std::string ip_address; //local ip
std::string ip_public_address; //public ip
std::string mac_address;
std::string serial_number;
std::string firmware_version;
std::string hardware_version;
std::vector<std::string> channelNameList;
} iobox_info_t;
typedef struct {
int counting_get;
bool is_connected;
bool is_anthenticated;
SOCKET sock_tcp;
iobox_info_t iobox_info;
} iobox_profile_t;
typedef struct {
std::string adapter_index;
std::string adapter_name;
std::string adapter_description;
std::string adapter_mac;
std::string adapter_ip;
std::string adapter_subnet;
std::string adapter_gateway;
} network_adapter_t;
class ANSIO_API iobox_api
{
private:
// Define the map with MAC address as key and IP address as value
std::unordered_map<std::string, std::string> macToIpMap;
// Define unordered_map to store IP address and device information (std:vector<std::string>)
std::unordered_map<std::string, std::vector<std::string>> ipToDeviceMap;
std::string ip_mcast; //ip multicast
int port_mcast; //port multicast
std::string _username; // default username
std::string _password; // default password
int _port;
const char* aes_key = "a1b2c3d4e5f6g7h8";
std::recursive_mutex _mutex;
std::vector<iobox_profile_t> iobox_profiles;
iobox_profile_t* findProfileByIp(const std::string& ip);
void save_info_iobox(const iobox_info_t& iobox_info);
void setSocketTimeout(SOCKET sock, int timeout);
void sendUnicastMessage(SOCKET sock, const char* ip, const std::string& message);
void sendMulticastMessage(const std::string& message);
bool sendTcpMessage(const std::string& ip, const char* buffer, size_t length, bool needCheckAuthen);
bool receiveTcpMessage(const std::string& ip, char* buffer, size_t& length, int timeout = 3000);
void show_profile_iobox(const iobox_profile_t& profile);
bool connectToIoboxWithoutAuthen(const std::string& remote, int port);
void remove_last_info_iobox();
uint16_t getIndexIoboxFromIp(const std::string& ip);
std::vector<std::string> scanNetworkDevicesManuallyOnNetworkAdapter(network_adapter_t adapter, int timeout);
std::vector<network_adapter_t> getNetworkAdapters();
bool performSetValue(const std::string& ip, const std::string& channelName, const std::string& value);
bool performGetValue(const std::string& ip, const std::string& channelName, std::string& outValue);
bool disconnectToIobox(const std::string& remote);
private:
SPDLogger& _logger = SPDLogger::GetInstance("IOBOX", true);
struct ToggleInfo {
std::chrono::steady_clock::time_point endTime;
std::string originalState;
bool revertFlag;
std::atomic<bool> active{ true };
std::atomic<bool> cancelled{ false };
std::mutex mtx;
std::condition_variable cv;
// Constructor for better initialization
ToggleInfo(bool revert = false) : revertFlag(revert) {}
};
std::unordered_map<std::string, std::shared_ptr<ToggleInfo>> activeToggles;
std::mutex toggleMapMutex;
bool executeToggleOperation(const std::string& remote, const std::string& channelName,
std::shared_ptr<ToggleInfo> toggleInfo, const std::string& key);
public:
iobox_api(const std::string& ip_mcast, int port_mcast);
~iobox_api() noexcept;
void show_profile_iobox(const std::string& remote);
void show_profile_ioboxs();
[[nodiscard]] std::string generateToken(const std::string& remote);
[[nodiscard]] std::vector<std::string> scanNetworkDevicesMulticast(int timeout);
[[nodiscard]] std::vector<std::string> scanNetworkDevicesManually(int timeout);
[[nodiscard]] std::vector<std::string> CreateDeviceChannel(const std::string& multicastInfo);
bool setAuthenticationIobox(const std::string& remote, const std::string& username, const std::string& password);
bool resetAuthenticationIobox(const std::string& remote, const std::string& token);
bool resetIobox(const std::string& remote, const std::string& token);
[[nodiscard]] std::string connectToIobox(const std::string& remote, int port, const std::string& username, const std::string& password);
bool disconnectToIoboxWithResetOutputs(const std::string& remote);
[[nodiscard]] std::vector<std::string> getDeviceChannelNames(const std::string& remote);
bool getValueDataStringIoboxFromChannelName(const std::string& remote, const std::string& channelName, std::string& outValue);
bool setValueDataStringIoboxFromChannelName(const std::string& remote, const std::string& channelName, const std::string& value);
bool checkTcpConnectStatus(const std::string& remote);
bool setValue(const std::string& remote, const std::string& channelName, const std::string& value);// Repeat until success or timeout (3s)
bool getValue(const std::string& remote, const std::string& channelName, std::string& outValue);// Repeat until success or timeout (3s)
//for RS232
bool openRs232Port(const std::string& remote, int baudrate, int dataBits, int stopBits, int parityBits);
bool closeRs232Port(const std::string& remote);
bool writeRs232Port(const std::string& remote, const std::string& data, int timeout_ms);
[[nodiscard]] std::string readRs232Port(const std::string& remote, const std::string& terminatorString, int lenExpect, int timeout_ms);
//for ota: type is "esp" or "mcu"
bool otaFirmwareDevice(const std::string& remote, const std::string& filename, const std::string& type);
// advanced functions (it can connect to ip or mac address and return device channel names)
[[nodiscard]] std::vector<std::string> advancedConnectToIobox(const std::string& remote, int port, const std::string& macAddress, const std::string& username, const std::string& password);
[[nodiscard]] std::vector<std::string> advancedScan(int timeout);
// Add to public section:
bool toggleIobox(const std::string& remote, const std::string& channelName, int timeOut,bool revertFlag, bool resetFlag, bool asyncMode = false);
bool cancelToggle(const std::string& remote, const std::string& channelName);
[[nodiscard]] std::vector<std::string> getActiveToggleChannels();
bool getToggleStatus(const std::string& remote, const std::string& channelName,int& remainingTimeMs, std::string& currentPhase);
// new functions
[[nodiscard]] std::vector<std::string> getStaticIpConfig(const std::string& remote);
bool setStaticIpConfig(const std::string& remote, bool enable, const std::string& ip, const std::string& gw, const std::string& nm);
[[nodiscard]] std::string getValueDataStringIoboxFromChannelName(const std::string& remote, const std::string& channelName);
bool toggleDigitalOutput(const std::string& remote, const std::string& channelName, const std::string& milliseconds, bool invert, bool reset);
bool setAIBValueDataStringIoboxFromChannelName(const std::string& remote, const std::string& channelName, const std::string& value);
};
}
extern "C" ANSIO_API int CreateANSIOHandle(ANSCENTER::iobox_api** Handle, const char* multicastIPAddress, int multicastPort);
extern "C" ANSIO_API int ScanANSIOHandle(ANSCENTER::iobox_api** Handle, int timeout, LStrHandle ipAddresses);
extern "C" ANSIO_API int ScanANSIOUnicastHandle(ANSCENTER::iobox_api** Handle, int timeout, LStrHandle ipAddresses);
extern "C" ANSIO_API int AdvancedScanANSIOHandle(ANSCENTER::iobox_api** Handle, int timeout, LStrHandle deviceInfos);
extern "C" ANSIO_API int AdvancedStaticScanANSIOHandle(int timeout, LStrHandle deviceInfos);
extern "C" ANSIO_API int ConnectANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, int ioBoxPort, const char* userName, const char* passWord, LStrHandle deviceStr);
extern "C" ANSIO_API int AdvancedConnectANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, int ioBoxPort, const char* ioSN, const char* userName, const char* passWord, LStrHandle deviceStr);
extern "C" ANSIO_API int DisconnectANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP);
extern "C" ANSIO_API int GetChannelNamesANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, int timeout, LStrHandle channelNames);
extern "C" ANSIO_API int SetValueANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, const char* channelName, const char* value);
extern "C" ANSIO_API int GetValueANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, const char* channelName, LStrHandle lStrValue);
extern "C" ANSIO_API int SetAuthenticationANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, const char* userName, const char* passWord);
extern "C" ANSIO_API int ReleaseANSIOHandle(ANSCENTER::iobox_api** Handle);
extern "C" ANSIO_API int ResetAuthenticationANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, const char* token);
extern "C" ANSIO_API int ResetANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, const char* token);
extern "C" ANSIO_API int GenerateANSIOTokenHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, LStrHandle tokenStr);
extern "C" ANSIO_API int CheckANSIOStatusHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP);
// RS232
extern "C" ANSIO_API int OpenANSAIRS232Port(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, int baudrate, int DataBits, int stopBits, int parityBits );
extern "C" ANSIO_API int CloseANSAIRS232Port(ANSCENTER::iobox_api** Handle, const char* ioBoxIP);
extern "C" ANSIO_API int WriteANSAIRS232Port(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, const char* data, int timeout_ms);
extern "C" ANSIO_API int ReadANSAIRS232Port(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, const char* terminatorString, int lenExpect, int timeout_ms, LStrHandle receivedData);
// OTA
extern "C" ANSIO_API int OTAANSIOFirmwareDevice(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, const char* filename, const char* type);
// C++ interface
extern "C" ANSIO_API int ScanANSIOHandle_CPP(ANSCENTER::iobox_api** Handle, int timeout, std::string& ipAddresses);
extern "C" ANSIO_API int ScanANSIOUnicastHandle_CPP(ANSCENTER::iobox_api** Handle, int timeout, std::string& ipAddresses);
extern "C" ANSIO_API int AdvancedScanANSIOHandle_CPP(ANSCENTER::iobox_api** Handle, int timeout, std::string& deviceInfos);
extern "C" ANSIO_API int AdvancedStaticScanANSIOHandle_CPP(int timeout, std::string& deviceInfos);
extern "C" ANSIO_API int GetChannelNamesANSIOHandle_CPP(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, int timeout, std::string& channelNames);
extern "C" ANSIO_API int GetValueANSIOHandle_CPP(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, const char* channelName, std::string& lStrValue);
extern "C" ANSIO_API int GenerateANSIOTokenHandle_CPP(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, std::string& tokenStr);
extern "C" ANSIO_API int ConnectANSIOHandle_CPP(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, int ioBoxPort, const char* userName, const char* passWord, std::string& deviceStr);
extern "C" ANSIO_API int AdvancedConnectANSIOHandle_CPP(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, int ioBoxPort, const char* ioBoxSN, const char* userName, const char* passWord, std::string& deviceStr);
// toggle
extern "C" ANSIO_API int ToggleIoboxHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, const char* channelName, int timeOut, int revertFlag, int resetFlag);
extern "C" ANSIO_API int ToggleIoboxHandleAsync(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, const char* channelName, int timeOut, int revertFlag, int resetFlag);
// new functions
extern "C" ANSIO_API int GetStaticIpConfigANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, LStrHandle staticIpConfigInfo);
extern "C" ANSIO_API int SetStaticIpConfigANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, int enable, const char* ip, const char* gw, const char* nm);
extern "C" ANSIO_API int GetValueDataStringIoboxFromChannelNameANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, const char* channelName, LStrHandle dataValue);
extern "C" ANSIO_API int ToggleDigitalOutputANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, const char* channelName, int milliseconds, int invertFlag, int resetFlag);
extern "C" ANSIO_API int SetAIBValueDataStringIoboxFromChannelNameANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, const char* channelName, const char* value);
#endif
+111
View File
@@ -0,0 +1,111 @@
#include "iobox_api.h"
#include <thread>
iobox_api ioBoxApp((char*)"239.255.255.250", 12345);
void userInputThread(iobox_api* api) {
std::string current_ip;
while (true) {
std::string input;
std::cout << std::endl;
std::cout << "Enter command followed by Enter: exit, scan, setip, connect, channels, disconnect, show, get, set, restart, setauthen, resetauthen" << std::endl;
// std::cin >> input; // This will only get the first word
std::getline(std::cin, input);
if (input == "exit") {
break;
}
else if(input == "scan") {
std::vector<std::string> devices = api->scanNetworkDevicesMulticast(5);
std::cout << "Found devices: " << devices.size() << std::endl;
for (const std::string& device : devices) {
std::cout << device << std::endl;
}
continue;
}
else if(input == "setip") {
std::cout << "Enter IP: ";
std::getline(std::cin, current_ip);
continue;
}
else if(input == "connect") {
if(current_ip == "") {
std::cout << "Please setip address first" << std::endl;
continue;
}
std::string username, password;
std::cout << "Enter username: ";
std::cin >> username;
std::cout << "Enter password: ";
std::cin >> password;
bool connect = api->connectToIobox(current_ip, DEVICE_TCP_PORT, username, password);
std::cout << "Connection to " << current_ip << (connect ? " succeeded." : " failed.") << std::endl;
continue;
}
else if(input == "disconnect") {
bool disconnect = api->disconnectToIobox(current_ip);
std::cout << "Disconnect to " << current_ip << (disconnect ? " succeeded." : " failed.") << std::endl;
// current_ip = "";
continue;
}
else if(input == "show") {
api->show_profile_ioboxs();
continue;
}
else if(input == "get") {
std::string channel;
std::cout << "Enter channel: ";
std::cin >> channel;
std::string value = api->getValueDataStringIoboxFromChannelName(current_ip, channel);
std::cout << "Value of " << channel << ": " << value << std::endl;
continue;
}
else if(input == "set") {
std::string value;
std::string channel;
std::cout << "Enter channel: ";
std::cin >> channel;
std::cout << "Enter value: ";
std::cin >> value;
bool set = api->setValueDataStringIoboxFromChannelName(current_ip, channel, value);
std::cout << "Set value to " << current_ip << (set ? " succeeded." : " failed.") << std::endl;
}
else if(input == "restart") {
std::string token = api->generateToken(current_ip);
bool reset = api->resetIobox(current_ip, token);
std::cout << "Restart " << current_ip << (reset ? " succeeded." : " failed.") << std::endl;
}
else if(input == "setauthen") {
std::string username, password;
std::cout << "Enter username: ";
std::cin >> username;
std::cout << "Enter password: ";
std::cin >> password;
bool set = api->setAuthenticationIobox(current_ip, username, password);
std::cout << "Set authentication to " << current_ip << (set ? " succeeded." : " failed.") << std::endl;
}
else if(input == "resetauthen") {
std::string token = api->generateToken(current_ip);
bool reset = api->resetAuthenticationIobox(current_ip, token);
std::cout << "Reset authentication to " << current_ip << (reset ? " succeeded." : " failed.") << std::endl;
}
else if(input == "channels") {
std::vector<std::string> channels = api->getDeviceChannelNames(current_ip);
std::cout << "Channels of " << current_ip << std::endl;
for(const std::string& channel : channels) {
std::cout << channel << std::endl;
}
}
else {
// std::cout << "Invalid command" << std::endl;
}
}
}
int main() {
std::thread userThread(userInputThread, &ioBoxApp);
userThread.join();
std::cout << "Main thread is done" << std::endl;
return 0;
}
File diff suppressed because it is too large Load Diff
+297
View File
@@ -0,0 +1,297 @@
/**
* \file aes.h
*
* \brief AES block cipher
*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
#ifndef MBEDTLS_AES_H
#define MBEDTLS_AES_H
#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include <stddef.h>
#include <stdint.h>
/* padlock.c and aesni.c rely on these values! */
#define MBEDTLS_AES_ENCRYPT 1
#define MBEDTLS_AES_DECRYPT 0
#define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
#define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
#if !defined(MBEDTLS_AES_ALT)
// Regular implementation
//
#ifdef __cplusplus
extern "C" {
#endif
/**
* \brief AES context structure
*
* \note buf is able to hold 32 extra bytes, which can be used:
* - for alignment purposes if VIA padlock is used, and/or
* - to simplify key expansion in the 256-bit case by
* generating an extra round key
*/
typedef struct
{
int nr; /*!< number of rounds */
uint32_t *rk; /*!< AES round keys */
uint32_t buf[68]; /*!< unaligned data */
}
mbedtls_aes_context;
/**
* \brief Initialize AES context
*
* \param ctx AES context to be initialized
*/
void mbedtls_aes_init( mbedtls_aes_context *ctx );
/**
* \brief Clear AES context
*
* \param ctx AES context to be cleared
*/
void mbedtls_aes_free( mbedtls_aes_context *ctx );
/**
* \brief AES key schedule (encryption)
*
* \param ctx AES context to be initialized
* \param key encryption key
* \param keybits must be 128, 192 or 256
*
* \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
*/
int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
unsigned int keybits );
/**
* \brief AES key schedule (decryption)
*
* \param ctx AES context to be initialized
* \param key decryption key
* \param keybits must be 128, 192 or 256
*
* \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
*/
int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
unsigned int keybits );
/**
* \brief AES-ECB block encryption/decryption
*
* \param ctx AES context
* \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
* \param input 16-byte input block
* \param output 16-byte output block
*
* \return 0 if successful
*/
int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
int mode,
const unsigned char input[16],
unsigned char output[16] );
#if defined(MBEDTLS_CIPHER_MODE_CBC)
/**
* \brief AES-CBC buffer encryption/decryption
* Length should be a multiple of the block
* size (16 bytes)
*
* \note Upon exit, the content of the IV is updated so that you can
* call the function same function again on the following
* block(s) of data and get the same result as if it was
* encrypted in one call. This allows a "streaming" usage.
* If on the other hand you need to retain the contents of the
* IV, you should either save it manually or use the cipher
* module instead.
*
* \param ctx AES context
* \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
* \param length length of the input data
* \param iv initialization vector (updated after use)
* \param input buffer holding the input data
* \param output buffer holding the output data
*
* \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
*/
int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
int mode,
size_t length,
unsigned char iv[16],
const unsigned char *input,
unsigned char *output );
#endif /* MBEDTLS_CIPHER_MODE_CBC */
#if defined(MBEDTLS_CIPHER_MODE_CFB)
/**
* \brief AES-CFB128 buffer encryption/decryption.
*
* Note: Due to the nature of CFB you should use the same key schedule for
* both encryption and decryption. So a context initialized with
* mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
*
* \note Upon exit, the content of the IV is updated so that you can
* call the function same function again on the following
* block(s) of data and get the same result as if it was
* encrypted in one call. This allows a "streaming" usage.
* If on the other hand you need to retain the contents of the
* IV, you should either save it manually or use the cipher
* module instead.
*
* \param ctx AES context
* \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
* \param length length of the input data
* \param iv_off offset in IV (updated after use)
* \param iv initialization vector (updated after use)
* \param input buffer holding the input data
* \param output buffer holding the output data
*
* \return 0 if successful
*/
int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
int mode,
size_t length,
size_t *iv_off,
unsigned char iv[16],
const unsigned char *input,
unsigned char *output );
/**
* \brief AES-CFB8 buffer encryption/decryption.
*
* Note: Due to the nature of CFB you should use the same key schedule for
* both encryption and decryption. So a context initialized with
* mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
*
* \note Upon exit, the content of the IV is updated so that you can
* call the function same function again on the following
* block(s) of data and get the same result as if it was
* encrypted in one call. This allows a "streaming" usage.
* If on the other hand you need to retain the contents of the
* IV, you should either save it manually or use the cipher
* module instead.
*
* \param ctx AES context
* \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
* \param length length of the input data
* \param iv initialization vector (updated after use)
* \param input buffer holding the input data
* \param output buffer holding the output data
*
* \return 0 if successful
*/
int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
int mode,
size_t length,
unsigned char iv[16],
const unsigned char *input,
unsigned char *output );
#endif /*MBEDTLS_CIPHER_MODE_CFB */
#if defined(MBEDTLS_CIPHER_MODE_CTR)
/**
* \brief AES-CTR buffer encryption/decryption
*
* Warning: You have to keep the maximum use of your counter in mind!
*
* Note: Due to the nature of CTR you should use the same key schedule for
* both encryption and decryption. So a context initialized with
* mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
*
* \param ctx AES context
* \param length The length of the data
* \param nc_off The offset in the current stream_block (for resuming
* within current cipher stream). The offset pointer to
* should be 0 at the start of a stream.
* \param nonce_counter The 128-bit nonce and counter.
* \param stream_block The saved stream-block for resuming. Is overwritten
* by the function.
* \param input The input data stream
* \param output The output data stream
*
* \return 0 if successful
*/
int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
size_t length,
size_t *nc_off,
unsigned char nonce_counter[16],
unsigned char stream_block[16],
const unsigned char *input,
unsigned char *output );
#endif /* MBEDTLS_CIPHER_MODE_CTR */
/**
* \brief Internal AES block encryption function
* (Only exposed to allow overriding it,
* see MBEDTLS_AES_ENCRYPT_ALT)
*
* \param ctx AES context
* \param input Plaintext block
* \param output Output (ciphertext) block
*/
void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16] );
/**
* \brief Internal AES block decryption function
* (Only exposed to allow overriding it,
* see MBEDTLS_AES_DECRYPT_ALT)
*
* \param ctx AES context
* \param input Ciphertext block
* \param output Output (plaintext) block
*/
void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16] );
#ifdef __cplusplus
}
#endif
#else /* MBEDTLS_AES_ALT */
#include "aes_alt.h"
#endif /* MBEDTLS_AES_ALT */
#ifdef __cplusplus
extern "C" {
#endif
/**
* \brief Checkup routine
*
* \return 0 if successful, or 1 if the test failed
*/
int mbedtls_aes_self_test( int verbose );
#ifdef __cplusplus
}
#endif
#endif /* aes.h */
+289
View File
@@ -0,0 +1,289 @@
/*
* RFC 1521 base64 encoding/decoding
*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#if defined(MBEDTLS_BASE64_C)
#include "base64.h"
#include <stdint.h>
#if defined(MBEDTLS_SELF_TEST)
#include <string.h>
#if defined(MBEDTLS_PLATFORM_C)
#include "platform.h"
#else
#include <stdio.h>
#define mbedtls_printf printf
#endif /* MBEDTLS_PLATFORM_C */
#endif /* MBEDTLS_SELF_TEST */
static const unsigned char base64_enc_map[64] =
{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', '+', '/'
};
static const unsigned char base64_dec_map[128] =
{
127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 62, 127, 127, 127, 63, 52, 53,
54, 55, 56, 57, 58, 59, 60, 61, 127, 127,
127, 64, 127, 127, 127, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 127, 127, 127, 127, 127, 127, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51, 127, 127, 127, 127, 127
};
#define BASE64_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
/*
* Encode a buffer into base64 format
*/
int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
const unsigned char *src, size_t slen )
{
size_t i, n;
int C1, C2, C3;
unsigned char *p;
if( slen == 0 )
{
*olen = 0;
return( 0 );
}
n = slen / 3 + ( slen % 3 != 0 );
if( n > ( BASE64_SIZE_T_MAX - 1 ) / 4 )
{
*olen = BASE64_SIZE_T_MAX;
return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
}
n *= 4;
if( dlen < n + 1 )
{
*olen = n + 1;
return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
}
n = ( slen / 3 ) * 3;
for( i = 0, p = dst; i < n; i += 3 )
{
C1 = *src++;
C2 = *src++;
C3 = *src++;
*p++ = base64_enc_map[(C1 >> 2) & 0x3F];
*p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
*p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F];
*p++ = base64_enc_map[C3 & 0x3F];
}
if( i < slen )
{
C1 = *src++;
C2 = ( ( i + 1 ) < slen ) ? *src++ : 0;
*p++ = base64_enc_map[(C1 >> 2) & 0x3F];
*p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
if( ( i + 1 ) < slen )
*p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F];
else *p++ = '=';
*p++ = '=';
}
*olen = p - dst;
*p = 0;
return( 0 );
}
/*
* Decode a base64-formatted buffer
*/
int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
const unsigned char *src, size_t slen )
{
size_t i, n;
uint32_t j, x;
unsigned char *p;
/* First pass: check for validity and get output length */
for( i = n = j = 0; i < slen; i++ )
{
/* Skip spaces before checking for EOL */
x = 0;
while( i < slen && src[i] == ' ' )
{
++i;
++x;
}
/* Spaces at end of buffer are OK */
if( i == slen )
break;
if( ( slen - i ) >= 2 &&
src[i] == '\r' && src[i + 1] == '\n' )
continue;
if( src[i] == '\n' )
continue;
/* Space inside a line is an error */
if( x != 0 )
return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
if( src[i] == '=' && ++j > 2 )
return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
if( src[i] > 127 || base64_dec_map[src[i]] == 127 )
return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
if( base64_dec_map[src[i]] < 64 && j != 0 )
return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
n++;
}
if( n == 0 )
{
*olen = 0;
return( 0 );
}
n = ( ( n * 6 ) + 7 ) >> 3;
n -= j;
if( dst == NULL || dlen < n )
{
*olen = n;
return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
}
for( j = 3, n = x = 0, p = dst; i > 0; i--, src++ )
{
if( *src == '\r' || *src == '\n' || *src == ' ' )
continue;
j -= ( base64_dec_map[*src] == 64 );
x = ( x << 6 ) | ( base64_dec_map[*src] & 0x3F );
if( ++n == 4 )
{
n = 0;
if( j > 0 ) *p++ = (unsigned char)( x >> 16 );
if( j > 1 ) *p++ = (unsigned char)( x >> 8 );
if( j > 2 ) *p++ = (unsigned char)( x );
}
}
*olen = p - dst;
return( 0 );
}
#if defined(MBEDTLS_SELF_TEST)
static const unsigned char base64_test_dec[64] =
{
0x24, 0x48, 0x6E, 0x56, 0x87, 0x62, 0x5A, 0xBD,
0xBF, 0x17, 0xD9, 0xA2, 0xC4, 0x17, 0x1A, 0x01,
0x94, 0xED, 0x8F, 0x1E, 0x11, 0xB3, 0xD7, 0x09,
0x0C, 0xB6, 0xE9, 0x10, 0x6F, 0x22, 0xEE, 0x13,
0xCA, 0xB3, 0x07, 0x05, 0x76, 0xC9, 0xFA, 0x31,
0x6C, 0x08, 0x34, 0xFF, 0x8D, 0xC2, 0x6C, 0x38,
0x00, 0x43, 0xE9, 0x54, 0x97, 0xAF, 0x50, 0x4B,
0xD1, 0x41, 0xBA, 0x95, 0x31, 0x5A, 0x0B, 0x97
};
static const unsigned char base64_test_enc[] =
"JEhuVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i7hPK"
"swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw==";
/*
* Checkup routine
*/
int mbedtls_base64_self_test( int verbose )
{
size_t len;
const unsigned char *src;
unsigned char buffer[128];
if( verbose != 0 )
mbedtls_printf( " Base64 encoding test: " );
src = base64_test_dec;
if( mbedtls_base64_encode( buffer, sizeof( buffer ), &len, src, 64 ) != 0 ||
memcmp( base64_test_enc, buffer, 88 ) != 0 )
{
if( verbose != 0 )
mbedtls_printf( "failed\n" );
return( 1 );
}
if( verbose != 0 )
mbedtls_printf( "passed\n Base64 decoding test: " );
src = base64_test_enc;
if( mbedtls_base64_decode( buffer, sizeof( buffer ), &len, src, 88 ) != 0 ||
memcmp( base64_test_dec, buffer, 64 ) != 0 )
{
if( verbose != 0 )
mbedtls_printf( "failed\n" );
return( 1 );
}
if( verbose != 0 )
mbedtls_printf( "passed\n\n" );
return( 0 );
}
#endif /* MBEDTLS_SELF_TEST */
#endif /* MBEDTLS_BASE64_C */
+88
View File
@@ -0,0 +1,88 @@
/**
* \file base64.h
*
* \brief RFC 1521 base64 encoding/decoding
*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
#ifndef MBEDTLS_BASE64_H
#define MBEDTLS_BASE64_H
#include <stddef.h>
#define MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL -0x002A /**< Output buffer too small. */
#define MBEDTLS_ERR_BASE64_INVALID_CHARACTER -0x002C /**< Invalid character in input. */
#ifdef __cplusplus
extern "C" {
#endif
/**
* \brief Encode a buffer into base64 format
*
* \param dst destination buffer
* \param dlen size of the destination buffer
* \param olen number of bytes written
* \param src source buffer
* \param slen amount of data to be encoded
*
* \return 0 if successful, or MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL.
* *olen is always updated to reflect the amount
* of data that has (or would have) been written.
* If that length cannot be represented, then no data is
* written to the buffer and *olen is set to the maximum
* length representable as a size_t.
*
* \note Call this function with dlen = 0 to obtain the
* required buffer size in *olen
*/
int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
const unsigned char *src, size_t slen );
/**
* \brief Decode a base64-formatted buffer
*
* \param dst destination buffer (can be NULL for checking size)
* \param dlen size of the destination buffer
* \param olen number of bytes written
* \param src source buffer
* \param slen amount of data to be decoded
*
* \return 0 if successful, MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL, or
* MBEDTLS_ERR_BASE64_INVALID_CHARACTER if the input data is
* not correct. *olen is always updated to reflect the amount
* of data that has (or would have) been written.
*
* \note Call this function with *dst = NULL or dlen = 0 to obtain
* the required buffer size in *olen
*/
int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
const unsigned char *src, size_t slen );
/**
* \brief Checkup routine
*
* \return 0 if successful, or 1 if the test failed
*/
int mbedtls_base64_self_test( int verbose );
#ifdef __cplusplus
}
#endif
#endif /* base64.h */
+540
View File
@@ -0,0 +1,540 @@
/**
* \file check_config.h
*
* \brief Consistency checks for configuration options
*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
/*
* It is recommended to include this file from your config.h
* in order to catch dependency issues early.
*/
#ifndef MBEDTLS_CHECK_CONFIG_H
#define MBEDTLS_CHECK_CONFIG_H
/*
* We assume CHAR_BIT is 8 in many places. In practice, this is true on our
* target platforms, so not an issue, but let's just be extra sure.
*/
#include <limits.h>
#if CHAR_BIT != 8
#error "mbed TLS requires a platform with 8-bit chars"
#endif
#if defined(_WIN32)
#if !defined(MBEDTLS_PLATFORM_C)
#error "MBEDTLS_PLATFORM_C is required on Windows"
#endif
/* Fix the config here. Not convenient to put an #ifdef _WIN32 in config.h as
* it would confuse config.pl. */
#if !defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) && \
!defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
#define MBEDTLS_PLATFORM_SNPRINTF_ALT
#endif
#endif /* _WIN32 */
#if defined(TARGET_LIKE_MBED) && \
( defined(MBEDTLS_NET_C) || defined(MBEDTLS_TIMING_C) )
#error "The NET and TIMING modules are not available for mbed OS - please use the network and timing functions provided by mbed OS"
#endif
#if defined(MBEDTLS_DEPRECATED_WARNING) && \
!defined(__GNUC__) && !defined(__clang__)
#error "MBEDTLS_DEPRECATED_WARNING only works with GCC and Clang"
#endif
#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_HAVE_TIME)
#error "MBEDTLS_HAVE_TIME_DATE without MBEDTLS_HAVE_TIME does not make sense"
#endif
#if defined(MBEDTLS_AESNI_C) && !defined(MBEDTLS_HAVE_ASM)
#error "MBEDTLS_AESNI_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_AES_C)
#error "MBEDTLS_CTR_DRBG_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_DHM_C) && !defined(MBEDTLS_BIGNUM_C)
#error "MBEDTLS_DHM_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_ECDH_C) && !defined(MBEDTLS_ECP_C)
#error "MBEDTLS_ECDH_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_ECDSA_C) && \
( !defined(MBEDTLS_ECP_C) || \
!defined(MBEDTLS_ASN1_PARSE_C) || \
!defined(MBEDTLS_ASN1_WRITE_C) )
#error "MBEDTLS_ECDSA_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_ECJPAKE_C) && \
( !defined(MBEDTLS_ECP_C) || !defined(MBEDTLS_MD_C) )
#error "MBEDTLS_ECJPAKE_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_ECDSA_DETERMINISTIC) && !defined(MBEDTLS_HMAC_DRBG_C)
#error "MBEDTLS_ECDSA_DETERMINISTIC defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_ECP_C) && ( !defined(MBEDTLS_BIGNUM_C) || ( \
!defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) && \
!defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) && \
!defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) && \
!defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) && \
!defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) && \
!defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) && \
!defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) && \
!defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) && \
!defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) && \
!defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) && \
!defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) ) )
#error "MBEDTLS_ECP_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_ENTROPY_C) && (!defined(MBEDTLS_SHA512_C) && \
!defined(MBEDTLS_SHA256_C))
#error "MBEDTLS_ENTROPY_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_SHA512_C) && \
defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) && (MBEDTLS_CTR_DRBG_ENTROPY_LEN > 64)
#error "MBEDTLS_CTR_DRBG_ENTROPY_LEN value too high"
#endif
#if defined(MBEDTLS_ENTROPY_C) && \
( !defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_ENTROPY_FORCE_SHA256) ) \
&& defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) && (MBEDTLS_CTR_DRBG_ENTROPY_LEN > 32)
#error "MBEDTLS_CTR_DRBG_ENTROPY_LEN value too high"
#endif
#if defined(MBEDTLS_ENTROPY_C) && \
defined(MBEDTLS_ENTROPY_FORCE_SHA256) && !defined(MBEDTLS_SHA256_C)
#error "MBEDTLS_ENTROPY_FORCE_SHA256 defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_GCM_C) && ( \
!defined(MBEDTLS_AES_C) && !defined(MBEDTLS_CAMELLIA_C) )
#error "MBEDTLS_GCM_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_HAVEGE_C) && !defined(MBEDTLS_TIMING_C)
#error "MBEDTLS_HAVEGE_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_HMAC_DRBG_C) && !defined(MBEDTLS_MD_C)
#error "MBEDTLS_HMAC_DRBG_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) && \
( !defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) )
#error "MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
( !defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) )
#error "MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) && !defined(MBEDTLS_DHM_C)
#error "MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) && \
!defined(MBEDTLS_ECDH_C)
#error "MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
( !defined(MBEDTLS_DHM_C) || !defined(MBEDTLS_RSA_C) || \
!defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_PKCS1_V15) )
#error "MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
( !defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_RSA_C) || \
!defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_PKCS1_V15) )
#error "MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
( !defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_ECDSA_C) || \
!defined(MBEDTLS_X509_CRT_PARSE_C) )
#error "MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
( !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
!defined(MBEDTLS_PKCS1_V15) )
#error "MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
( !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
!defined(MBEDTLS_PKCS1_V15) )
#error "MBEDTLS_KEY_EXCHANGE_RSA_ENABLED defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
( !defined(MBEDTLS_ECJPAKE_C) || !defined(MBEDTLS_SHA256_C) || \
!defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) )
#error "MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
( !defined(MBEDTLS_PLATFORM_C) || !defined(MBEDTLS_PLATFORM_MEMORY) )
#error "MBEDTLS_MEMORY_BUFFER_ALLOC_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PADLOCK_C) && !defined(MBEDTLS_HAVE_ASM)
#error "MBEDTLS_PADLOCK_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PEM_PARSE_C) && !defined(MBEDTLS_BASE64_C)
#error "MBEDTLS_PEM_PARSE_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PEM_WRITE_C) && !defined(MBEDTLS_BASE64_C)
#error "MBEDTLS_PEM_WRITE_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PK_C) && \
( !defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_ECP_C) )
#error "MBEDTLS_PK_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PK_PARSE_C) && !defined(MBEDTLS_PK_C)
#error "MBEDTLS_PK_PARSE_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PK_WRITE_C) && !defined(MBEDTLS_PK_C)
#error "MBEDTLS_PK_WRITE_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PKCS11_C) && !defined(MBEDTLS_PK_C)
#error "MBEDTLS_PKCS11_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PLATFORM_EXIT_ALT) && !defined(MBEDTLS_PLATFORM_C)
#error "MBEDTLS_PLATFORM_EXIT_ALT defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PLATFORM_EXIT_MACRO) && !defined(MBEDTLS_PLATFORM_C)
#error "MBEDTLS_PLATFORM_EXIT_MACRO defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PLATFORM_EXIT_MACRO) &&\
( defined(MBEDTLS_PLATFORM_STD_EXIT) ||\
defined(MBEDTLS_PLATFORM_EXIT_ALT) )
#error "MBEDTLS_PLATFORM_EXIT_MACRO and MBEDTLS_PLATFORM_STD_EXIT/MBEDTLS_PLATFORM_EXIT_ALT cannot be defined simultaneously"
#endif
#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT) && !defined(MBEDTLS_PLATFORM_C)
#error "MBEDTLS_PLATFORM_FPRINTF_ALT defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO) && !defined(MBEDTLS_PLATFORM_C)
#error "MBEDTLS_PLATFORM_FPRINTF_MACRO defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO) &&\
( defined(MBEDTLS_PLATFORM_STD_FPRINTF) ||\
defined(MBEDTLS_PLATFORM_FPRINTF_ALT) )
#error "MBEDTLS_PLATFORM_FPRINTF_MACRO and MBEDTLS_PLATFORM_STD_FPRINTF/MBEDTLS_PLATFORM_FPRINTF_ALT cannot be defined simultaneously"
#endif
#if defined(MBEDTLS_PLATFORM_FREE_MACRO) &&\
( !defined(MBEDTLS_PLATFORM_C) || !defined(MBEDTLS_PLATFORM_MEMORY) )
#error "MBEDTLS_PLATFORM_FREE_MACRO defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PLATFORM_FREE_MACRO) &&\
defined(MBEDTLS_PLATFORM_STD_FREE)
#error "MBEDTLS_PLATFORM_FREE_MACRO and MBEDTLS_PLATFORM_STD_FREE cannot be defined simultaneously"
#endif
#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && !defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
#error "MBEDTLS_PLATFORM_CALLOC_MACRO must be defined if MBEDTLS_PLATFORM_FREE_MACRO is"
#endif
#if defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&\
( !defined(MBEDTLS_PLATFORM_C) || !defined(MBEDTLS_PLATFORM_MEMORY) )
#error "MBEDTLS_PLATFORM_CALLOC_MACRO defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&\
defined(MBEDTLS_PLATFORM_STD_CALLOC)
#error "MBEDTLS_PLATFORM_CALLOC_MACRO and MBEDTLS_PLATFORM_STD_CALLOC cannot be defined simultaneously"
#endif
#if defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && !defined(MBEDTLS_PLATFORM_FREE_MACRO)
#error "MBEDTLS_PLATFORM_FREE_MACRO must be defined if MBEDTLS_PLATFORM_CALLOC_MACRO is"
#endif
#if defined(MBEDTLS_PLATFORM_MEMORY) && !defined(MBEDTLS_PLATFORM_C)
#error "MBEDTLS_PLATFORM_MEMORY defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PLATFORM_PRINTF_ALT) && !defined(MBEDTLS_PLATFORM_C)
#error "MBEDTLS_PLATFORM_PRINTF_ALT defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO) && !defined(MBEDTLS_PLATFORM_C)
#error "MBEDTLS_PLATFORM_PRINTF_MACRO defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO) &&\
( defined(MBEDTLS_PLATFORM_STD_PRINTF) ||\
defined(MBEDTLS_PLATFORM_PRINTF_ALT) )
#error "MBEDTLS_PLATFORM_PRINTF_MACRO and MBEDTLS_PLATFORM_STD_PRINTF/MBEDTLS_PLATFORM_PRINTF_ALT cannot be defined simultaneously"
#endif
#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) && !defined(MBEDTLS_PLATFORM_C)
#error "MBEDTLS_PLATFORM_SNPRINTF_ALT defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO) && !defined(MBEDTLS_PLATFORM_C)
#error "MBEDTLS_PLATFORM_SNPRINTF_MACRO defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO) &&\
( defined(MBEDTLS_PLATFORM_STD_SNPRINTF) ||\
defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) )
#error "MBEDTLS_PLATFORM_SNPRINTF_MACRO and MBEDTLS_PLATFORM_STD_SNPRINTF/MBEDTLS_PLATFORM_SNPRINTF_ALT cannot be defined simultaneously"
#endif
#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR) &&\
!defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
#error "MBEDTLS_PLATFORM_STD_MEM_HDR defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PLATFORM_STD_CALLOC) && !defined(MBEDTLS_PLATFORM_MEMORY)
#error "MBEDTLS_PLATFORM_STD_CALLOC defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PLATFORM_STD_CALLOC) && !defined(MBEDTLS_PLATFORM_MEMORY)
#error "MBEDTLS_PLATFORM_STD_CALLOC defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PLATFORM_STD_FREE) && !defined(MBEDTLS_PLATFORM_MEMORY)
#error "MBEDTLS_PLATFORM_STD_FREE defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PLATFORM_STD_EXIT) &&\
!defined(MBEDTLS_PLATFORM_EXIT_ALT)
#error "MBEDTLS_PLATFORM_STD_EXIT defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PLATFORM_STD_FPRINTF) &&\
!defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
#error "MBEDTLS_PLATFORM_STD_FPRINTF defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PLATFORM_STD_PRINTF) &&\
!defined(MBEDTLS_PLATFORM_PRINTF_ALT)
#error "MBEDTLS_PLATFORM_STD_PRINTF defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_PLATFORM_STD_SNPRINTF) &&\
!defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
#error "MBEDTLS_PLATFORM_STD_SNPRINTF defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_RSA_C) && ( !defined(MBEDTLS_BIGNUM_C) || \
!defined(MBEDTLS_OID_C) )
#error "MBEDTLS_RSA_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
( !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_PKCS1_V21) )
#error "MBEDTLS_X509_RSASSA_PSS_SUPPORT defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_SSL_PROTO_SSL3) && ( !defined(MBEDTLS_MD5_C) || \
!defined(MBEDTLS_SHA1_C) )
#error "MBEDTLS_SSL_PROTO_SSL3 defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_SSL_PROTO_TLS1) && ( !defined(MBEDTLS_MD5_C) || \
!defined(MBEDTLS_SHA1_C) )
#error "MBEDTLS_SSL_PROTO_TLS1 defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_SSL_PROTO_TLS1_1) && ( !defined(MBEDTLS_MD5_C) || \
!defined(MBEDTLS_SHA1_C) )
#error "MBEDTLS_SSL_PROTO_TLS1_1 defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && ( !defined(MBEDTLS_SHA1_C) && \
!defined(MBEDTLS_SHA256_C) && !defined(MBEDTLS_SHA512_C) )
#error "MBEDTLS_SSL_PROTO_TLS1_2 defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_SSL_PROTO_DTLS) && \
!defined(MBEDTLS_SSL_PROTO_TLS1_1) && \
!defined(MBEDTLS_SSL_PROTO_TLS1_2)
#error "MBEDTLS_SSL_PROTO_DTLS defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_TLS_C)
#error "MBEDTLS_SSL_CLI_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_SSL_TLS_C) && ( !defined(MBEDTLS_CIPHER_C) || \
!defined(MBEDTLS_MD_C) )
#error "MBEDTLS_SSL_TLS_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_TLS_C)
#error "MBEDTLS_SSL_SRV_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_SSL_TLS_C) && (!defined(MBEDTLS_SSL_PROTO_SSL3) && \
!defined(MBEDTLS_SSL_PROTO_TLS1) && !defined(MBEDTLS_SSL_PROTO_TLS1_1) && \
!defined(MBEDTLS_SSL_PROTO_TLS1_2))
#error "MBEDTLS_SSL_TLS_C defined, but no protocols are active"
#endif
#if defined(MBEDTLS_SSL_TLS_C) && (defined(MBEDTLS_SSL_PROTO_SSL3) && \
defined(MBEDTLS_SSL_PROTO_TLS1_1) && !defined(MBEDTLS_SSL_PROTO_TLS1))
#error "Illegal protocol selection"
#endif
#if defined(MBEDTLS_SSL_TLS_C) && (defined(MBEDTLS_SSL_PROTO_TLS1) && \
defined(MBEDTLS_SSL_PROTO_TLS1_2) && !defined(MBEDTLS_SSL_PROTO_TLS1_1))
#error "Illegal protocol selection"
#endif
#if defined(MBEDTLS_SSL_TLS_C) && (defined(MBEDTLS_SSL_PROTO_SSL3) && \
defined(MBEDTLS_SSL_PROTO_TLS1_2) && (!defined(MBEDTLS_SSL_PROTO_TLS1) || \
!defined(MBEDTLS_SSL_PROTO_TLS1_1)))
#error "Illegal protocol selection"
#endif
#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && !defined(MBEDTLS_SSL_PROTO_DTLS)
#error "MBEDTLS_SSL_DTLS_HELLO_VERIFY defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && \
!defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
#error "MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
( !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) )
#error "MBEDTLS_SSL_DTLS_ANTI_REPLAY defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
( !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) )
#error "MBEDTLS_SSL_DTLS_BADMAC_LIMIT defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
!defined(MBEDTLS_SSL_PROTO_TLS1) && \
!defined(MBEDTLS_SSL_PROTO_TLS1_1) && \
!defined(MBEDTLS_SSL_PROTO_TLS1_2)
#error "MBEDTLS_SSL_ENCRYPT_THEN_MAC defined, but not all prerequsites"
#endif
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
!defined(MBEDTLS_SSL_PROTO_TLS1) && \
!defined(MBEDTLS_SSL_PROTO_TLS1_1) && \
!defined(MBEDTLS_SSL_PROTO_TLS1_2)
#error "MBEDTLS_SSL_EXTENDED_MASTER_SECRET defined, but not all prerequsites"
#endif
#if defined(MBEDTLS_SSL_TICKET_C) && !defined(MBEDTLS_CIPHER_C)
#error "MBEDTLS_SSL_TICKET_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) && \
!defined(MBEDTLS_SSL_PROTO_SSL3) && !defined(MBEDTLS_SSL_PROTO_TLS1)
#error "MBEDTLS_SSL_CBC_RECORD_SPLITTING defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
!defined(MBEDTLS_X509_CRT_PARSE_C)
#error "MBEDTLS_SSL_SERVER_NAME_INDICATION defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_THREADING_PTHREAD)
#if !defined(MBEDTLS_THREADING_C) || defined(MBEDTLS_THREADING_IMPL)
#error "MBEDTLS_THREADING_PTHREAD defined, but not all prerequisites"
#endif
#define MBEDTLS_THREADING_IMPL
#endif
#if defined(MBEDTLS_THREADING_ALT)
#if !defined(MBEDTLS_THREADING_C) || defined(MBEDTLS_THREADING_IMPL)
#error "MBEDTLS_THREADING_ALT defined, but not all prerequisites"
#endif
#define MBEDTLS_THREADING_IMPL
#endif
#if defined(MBEDTLS_THREADING_C) && !defined(MBEDTLS_THREADING_IMPL)
#error "MBEDTLS_THREADING_C defined, single threading implementation required"
#endif
#undef MBEDTLS_THREADING_IMPL
#if defined(MBEDTLS_VERSION_FEATURES) && !defined(MBEDTLS_VERSION_C)
#error "MBEDTLS_VERSION_FEATURES defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_X509_USE_C) && ( !defined(MBEDTLS_BIGNUM_C) || \
!defined(MBEDTLS_OID_C) || !defined(MBEDTLS_ASN1_PARSE_C) || \
!defined(MBEDTLS_PK_PARSE_C) )
#error "MBEDTLS_X509_USE_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_X509_CREATE_C) && ( !defined(MBEDTLS_BIGNUM_C) || \
!defined(MBEDTLS_OID_C) || !defined(MBEDTLS_ASN1_WRITE_C) || \
!defined(MBEDTLS_PK_WRITE_C) )
#error "MBEDTLS_X509_CREATE_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_X509_CRT_PARSE_C) && ( !defined(MBEDTLS_X509_USE_C) )
#error "MBEDTLS_X509_CRT_PARSE_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_X509_CRL_PARSE_C) && ( !defined(MBEDTLS_X509_USE_C) )
#error "MBEDTLS_X509_CRL_PARSE_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_X509_CSR_PARSE_C) && ( !defined(MBEDTLS_X509_USE_C) )
#error "MBEDTLS_X509_CSR_PARSE_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_X509_CRT_WRITE_C) && ( !defined(MBEDTLS_X509_CREATE_C) )
#error "MBEDTLS_X509_CRT_WRITE_C defined, but not all prerequisites"
#endif
#if defined(MBEDTLS_X509_CSR_WRITE_C) && ( !defined(MBEDTLS_X509_CREATE_C) )
#error "MBEDTLS_X509_CSR_WRITE_C defined, but not all prerequisites"
#endif
/*
* Avoid warning from -pedantic. This is a convenient place for this
* workaround since this is included by every single file before the
* #if defined(MBEDTLS_xxx_C) that results in emtpy translation units.
*/
typedef int mbedtls_iso_c_forbids_empty_translation_units;
#endif /* MBEDTLS_CHECK_CONFIG_H */
File diff suppressed because it is too large Load Diff
+214
View File
@@ -0,0 +1,214 @@
/**
* \file platform.h
*
* \brief mbed TLS Platform abstraction layer
*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
#ifndef MBEDTLS_PLATFORM_H
#define MBEDTLS_PLATFORM_H
#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#ifdef __cplusplus
extern "C" {
#endif
/**
* \name SECTION: Module settings
*
* The configuration options you can set for this module are in this section.
* Either change them in config.h or define them on the compiler command line.
* \{
*/
#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
#include <stdio.h>
#include <stdlib.h>
#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
#if defined(_WIN32)
#define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf /**< Default snprintf to use */
#else
#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use */
#endif
#endif
#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
#define MBEDTLS_PLATFORM_STD_PRINTF printf /**< Default printf to use */
#endif
#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */
#endif
#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< Default allocator to use */
#endif
#if !defined(MBEDTLS_PLATFORM_STD_FREE)
#define MBEDTLS_PLATFORM_STD_FREE free /**< Default free to use */
#endif
#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
#define MBEDTLS_PLATFORM_STD_EXIT exit /**< Default free to use */
#endif
#else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
#include MBEDTLS_PLATFORM_STD_MEM_HDR
#endif
#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
/* \} name SECTION: Module settings */
/*
* The function pointers for calloc and free
*/
#if defined(MBEDTLS_PLATFORM_MEMORY)
#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
#else
/* For size_t */
#include <stddef.h>
extern void * (*mbedtls_calloc)( size_t n, size_t size );
extern void (*mbedtls_free)( void *ptr );
/**
* \brief Set your own memory implementation function pointers
*
* \param calloc_func the calloc function implementation
* \param free_func the free function implementation
*
* \return 0 if successful
*/
int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
void (*free_func)( void * ) );
#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
#else /* !MBEDTLS_PLATFORM_MEMORY */
#define mbedtls_free free
#define mbedtls_calloc calloc
#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
/*
* The function pointers for fprintf
*/
#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
/* We need FILE * */
#include <stdio.h>
extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
/**
* \brief Set your own fprintf function pointer
*
* \param fprintf_func the fprintf function implementation
*
* \return 0
*/
int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
... ) );
#else
#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
#else
#define mbedtls_fprintf fprintf
#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
/*
* The function pointers for printf
*/
#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
extern int (*mbedtls_printf)( const char *format, ... );
/**
* \brief Set your own printf function pointer
*
* \param printf_func the printf function implementation
*
* \return 0
*/
int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
#else
#define mbedtls_printf printf
#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
/*
* The function pointers for snprintf
*
* The snprintf implementation should conform to C99:
* - it *must* always correctly zero-terminate the buffer
* (except when n == 0, then it must leave the buffer untouched)
* - however it is acceptable to return -1 instead of the required length when
* the destination buffer is too short.
*/
#if defined(_WIN32)
/* For Windows (inc. MSYS2), we provide our own fixed implementation */
int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
#endif
#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
/**
* \brief Set your own snprintf function pointer
*
* \param snprintf_func the snprintf function implementation
*
* \return 0
*/
int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
const char * format, ... ) );
#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
#else
#define mbedtls_snprintf snprintf
#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
/*
* The function pointers for exit
*/
#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
extern void (*mbedtls_exit)( int status );
/**
* \brief Set your own exit function pointer
*
* \param exit_func the exit function implementation
*
* \return 0
*/
int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
#else
#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
#else
#define mbedtls_exit exit
#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
#ifdef __cplusplus
}
#endif
#endif /* platform.h */
+445
View File
@@ -0,0 +1,445 @@
/*
* FIPS-180-2 compliant SHA-256 implementation
*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
/*
* The SHA-256 Secure Hash Standard was published by NIST in 2002.
*
* http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
*/
#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#if defined(MBEDTLS_SHA256_C)
#include "sha256.h"
#include <string.h>
#if defined(MBEDTLS_SELF_TEST)
#if defined(MBEDTLS_PLATFORM_C)
#include "platform.h"
#else
#include <stdio.h>
#define mbedtls_printf printf
#endif /* MBEDTLS_PLATFORM_C */
#endif /* MBEDTLS_SELF_TEST */
#if !defined(MBEDTLS_SHA256_ALT)
/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
}
/*
* 32-bit integer manipulation macros (big endian)
*/
#ifndef GET_UINT32_BE
#define GET_UINT32_BE(n,b,i) \
do { \
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \
| ( (uint32_t) (b)[(i) + 1] << 16 ) \
| ( (uint32_t) (b)[(i) + 2] << 8 ) \
| ( (uint32_t) (b)[(i) + 3] ); \
} while( 0 )
#endif
#ifndef PUT_UINT32_BE
#define PUT_UINT32_BE(n,b,i) \
do { \
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
(b)[(i) + 3] = (unsigned char) ( (n) ); \
} while( 0 )
#endif
void mbedtls_sha256_init( mbedtls_sha256_context *ctx )
{
memset( ctx, 0, sizeof( mbedtls_sha256_context ) );
}
void mbedtls_sha256_free( mbedtls_sha256_context *ctx )
{
if( ctx == NULL )
return;
mbedtls_zeroize( ctx, sizeof( mbedtls_sha256_context ) );
}
void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
const mbedtls_sha256_context *src )
{
*dst = *src;
}
/*
* SHA-256 context setup
*/
void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 )
{
ctx->total[0] = 0;
ctx->total[1] = 0;
if( is224 == 0 )
{
/* SHA-256 */
ctx->state[0] = 0x6A09E667;
ctx->state[1] = 0xBB67AE85;
ctx->state[2] = 0x3C6EF372;
ctx->state[3] = 0xA54FF53A;
ctx->state[4] = 0x510E527F;
ctx->state[5] = 0x9B05688C;
ctx->state[6] = 0x1F83D9AB;
ctx->state[7] = 0x5BE0CD19;
}
else
{
/* SHA-224 */
ctx->state[0] = 0xC1059ED8;
ctx->state[1] = 0x367CD507;
ctx->state[2] = 0x3070DD17;
ctx->state[3] = 0xF70E5939;
ctx->state[4] = 0xFFC00B31;
ctx->state[5] = 0x68581511;
ctx->state[6] = 0x64F98FA7;
ctx->state[7] = 0xBEFA4FA4;
}
ctx->is224 = is224;
}
#if !defined(MBEDTLS_SHA256_PROCESS_ALT)
static const uint32_t K[] =
{
0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
};
#define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
#define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
#define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
#define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
#define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
#define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
#define F0(x,y,z) ((x & y) | (z & (x | y)))
#define F1(x,y,z) (z ^ (x & (y ^ z)))
#define R(t) \
( \
W[t] = S1(W[t - 2]) + W[t - 7] + \
S0(W[t - 15]) + W[t - 16] \
)
#define P(a,b,c,d,e,f,g,h,x,K) \
{ \
temp1 = h + S3(e) + F1(e,f,g) + K + x; \
temp2 = S2(a) + F0(a,b,c); \
d += temp1; h = temp1 + temp2; \
}
void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] )
{
uint32_t temp1, temp2, W[64];
uint32_t A[8];
unsigned int i;
for( i = 0; i < 8; i++ )
A[i] = ctx->state[i];
#if defined(MBEDTLS_SHA256_SMALLER)
for( i = 0; i < 64; i++ )
{
if( i < 16 )
GET_UINT32_BE( W[i], data, 4 * i );
else
R( i );
P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i], K[i] );
temp1 = A[7]; A[7] = A[6]; A[6] = A[5]; A[5] = A[4]; A[4] = A[3];
A[3] = A[2]; A[2] = A[1]; A[1] = A[0]; A[0] = temp1;
}
#else /* MBEDTLS_SHA256_SMALLER */
for( i = 0; i < 16; i++ )
GET_UINT32_BE( W[i], data, 4 * i );
for( i = 0; i < 16; i += 8 )
{
P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i+0], K[i+0] );
P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], W[i+1], K[i+1] );
P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], W[i+2], K[i+2] );
P( A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], W[i+3], K[i+3] );
P( A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], W[i+4], K[i+4] );
P( A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], W[i+5], K[i+5] );
P( A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], W[i+6], K[i+6] );
P( A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], W[i+7], K[i+7] );
}
for( i = 16; i < 64; i += 8 )
{
P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], R(i+0), K[i+0] );
P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], R(i+1), K[i+1] );
P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], R(i+2), K[i+2] );
P( A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], R(i+3), K[i+3] );
P( A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], R(i+4), K[i+4] );
P( A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], R(i+5), K[i+5] );
P( A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], R(i+6), K[i+6] );
P( A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], R(i+7), K[i+7] );
}
#endif /* MBEDTLS_SHA256_SMALLER */
for( i = 0; i < 8; i++ )
ctx->state[i] += A[i];
}
#endif /* !MBEDTLS_SHA256_PROCESS_ALT */
/*
* SHA-256 process buffer
*/
void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input,
size_t ilen )
{
size_t fill;
uint32_t left;
if( ilen == 0 )
return;
left = ctx->total[0] & 0x3F;
fill = 64 - left;
ctx->total[0] += (uint32_t) ilen;
ctx->total[0] &= 0xFFFFFFFF;
if( ctx->total[0] < (uint32_t) ilen )
ctx->total[1]++;
if( left && ilen >= fill )
{
memcpy( (void *) (ctx->buffer + left), input, fill );
mbedtls_sha256_process( ctx, ctx->buffer );
input += fill;
ilen -= fill;
left = 0;
}
while( ilen >= 64 )
{
mbedtls_sha256_process( ctx, input );
input += 64;
ilen -= 64;
}
if( ilen > 0 )
memcpy( (void *) (ctx->buffer + left), input, ilen );
}
static const unsigned char sha256_padding[64] =
{
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
/*
* SHA-256 final digest
*/
void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] )
{
uint32_t last, padn;
uint32_t high, low;
unsigned char msglen[8];
high = ( ctx->total[0] >> 29 )
| ( ctx->total[1] << 3 );
low = ( ctx->total[0] << 3 );
PUT_UINT32_BE( high, msglen, 0 );
PUT_UINT32_BE( low, msglen, 4 );
last = ctx->total[0] & 0x3F;
padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
mbedtls_sha256_update( ctx, sha256_padding, padn );
mbedtls_sha256_update( ctx, msglen, 8 );
PUT_UINT32_BE( ctx->state[0], output, 0 );
PUT_UINT32_BE( ctx->state[1], output, 4 );
PUT_UINT32_BE( ctx->state[2], output, 8 );
PUT_UINT32_BE( ctx->state[3], output, 12 );
PUT_UINT32_BE( ctx->state[4], output, 16 );
PUT_UINT32_BE( ctx->state[5], output, 20 );
PUT_UINT32_BE( ctx->state[6], output, 24 );
if( ctx->is224 == 0 )
PUT_UINT32_BE( ctx->state[7], output, 28 );
}
#endif /* !MBEDTLS_SHA256_ALT */
/*
* output = SHA-256( input buffer )
*/
void mbedtls_sha256( const unsigned char *input, size_t ilen,
unsigned char output[32], int is224 )
{
mbedtls_sha256_context ctx;
mbedtls_sha256_init( &ctx );
mbedtls_sha256_starts( &ctx, is224 );
mbedtls_sha256_update( &ctx, input, ilen );
mbedtls_sha256_finish( &ctx, output );
mbedtls_sha256_free( &ctx );
}
#if defined(MBEDTLS_SELF_TEST)
/*
* FIPS-180-2 test vectors
*/
static const unsigned char sha256_test_buf[3][57] =
{
{ "abc" },
{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
{ "" }
};
static const int sha256_test_buflen[3] =
{
3, 56, 1000
};
static const unsigned char sha256_test_sum[6][32] =
{
/*
* SHA-224 test vectors
*/
{ 0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22,
0x86, 0x42, 0xA4, 0x77, 0xBD, 0xA2, 0x55, 0xB3,
0x2A, 0xAD, 0xBC, 0xE4, 0xBD, 0xA0, 0xB3, 0xF7,
0xE3, 0x6C, 0x9D, 0xA7 },
{ 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC,
0x5D, 0xBA, 0x5D, 0xA1, 0xFD, 0x89, 0x01, 0x50,
0xB0, 0xC6, 0x45, 0x5C, 0xB4, 0xF5, 0x8B, 0x19,
0x52, 0x52, 0x25, 0x25 },
{ 0x20, 0x79, 0x46, 0x55, 0x98, 0x0C, 0x91, 0xD8,
0xBB, 0xB4, 0xC1, 0xEA, 0x97, 0x61, 0x8A, 0x4B,
0xF0, 0x3F, 0x42, 0x58, 0x19, 0x48, 0xB2, 0xEE,
0x4E, 0xE7, 0xAD, 0x67 },
/*
* SHA-256 test vectors
*/
{ 0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA,
0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,
0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C,
0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD },
{ 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,
0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39,
0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67,
0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 },
{ 0xCD, 0xC7, 0x6E, 0x5C, 0x99, 0x14, 0xFB, 0x92,
0x81, 0xA1, 0xC7, 0xE2, 0x84, 0xD7, 0x3E, 0x67,
0xF1, 0x80, 0x9A, 0x48, 0xA4, 0x97, 0x20, 0x0E,
0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0 }
};
/*
* Checkup routine
*/
int mbedtls_sha256_self_test( int verbose )
{
int i, j, k, buflen, ret = 0;
unsigned char buf[1024];
unsigned char sha256sum[32];
mbedtls_sha256_context ctx;
mbedtls_sha256_init( &ctx );
for( i = 0; i < 6; i++ )
{
j = i % 3;
k = i < 3;
if( verbose != 0 )
mbedtls_printf( " SHA-%d test #%d: ", 256 - k * 32, j + 1 );
mbedtls_sha256_starts( &ctx, k );
if( j == 2 )
{
memset( buf, 'a', buflen = 1000 );
for( j = 0; j < 1000; j++ )
mbedtls_sha256_update( &ctx, buf, buflen );
}
else
mbedtls_sha256_update( &ctx, sha256_test_buf[j],
sha256_test_buflen[j] );
mbedtls_sha256_finish( &ctx, sha256sum );
if( memcmp( sha256sum, sha256_test_sum[i], 32 - k * 4 ) != 0 )
{
if( verbose != 0 )
mbedtls_printf( "failed\n" );
ret = 1;
goto exit;
}
if( verbose != 0 )
mbedtls_printf( "passed\n" );
}
if( verbose != 0 )
mbedtls_printf( "\n" );
exit:
mbedtls_sha256_free( &ctx );
return( ret );
}
#endif /* MBEDTLS_SELF_TEST */
#endif /* MBEDTLS_SHA256_C */
+141
View File
@@ -0,0 +1,141 @@
/**
* \file sha256.h
*
* \brief SHA-224 and SHA-256 cryptographic hash function
*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
#ifndef MBEDTLS_SHA256_H
#define MBEDTLS_SHA256_H
#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include <stddef.h>
#include <stdint.h>
#if !defined(MBEDTLS_SHA256_ALT)
// Regular implementation
//
#ifdef __cplusplus
extern "C" {
#endif
/**
* \brief SHA-256 context structure
*/
typedef struct
{
uint32_t total[2]; /*!< number of bytes processed */
uint32_t state[8]; /*!< intermediate digest state */
unsigned char buffer[64]; /*!< data block being processed */
int is224; /*!< 0 => SHA-256, else SHA-224 */
}
mbedtls_sha256_context;
/**
* \brief Initialize SHA-256 context
*
* \param ctx SHA-256 context to be initialized
*/
void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
/**
* \brief Clear SHA-256 context
*
* \param ctx SHA-256 context to be cleared
*/
void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
/**
* \brief Clone (the state of) a SHA-256 context
*
* \param dst The destination context
* \param src The context to be cloned
*/
void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
const mbedtls_sha256_context *src );
/**
* \brief SHA-256 context setup
*
* \param ctx context to be initialized
* \param is224 0 = use SHA256, 1 = use SHA224
*/
void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 );
/**
* \brief SHA-256 process buffer
*
* \param ctx SHA-256 context
* \param input buffer holding the data
* \param ilen length of the input data
*/
void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input,
size_t ilen );
/**
* \brief SHA-256 final digest
*
* \param ctx SHA-256 context
* \param output SHA-224/256 checksum result
*/
void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] );
/* Internal use */
void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] );
#ifdef __cplusplus
}
#endif
#else /* MBEDTLS_SHA256_ALT */
#include "sha256_alt.h"
#endif /* MBEDTLS_SHA256_ALT */
#ifdef __cplusplus
extern "C" {
#endif
/**
* \brief Output = SHA-256( input buffer )
*
* \param input buffer holding the data
* \param ilen length of the input data
* \param output SHA-224/256 checksum result
* \param is224 0 = use SHA256, 1 = use SHA224
*/
void mbedtls_sha256( const unsigned char *input, size_t ilen,
unsigned char output[32], int is224 );
/**
* \brief Checkup routine
*
* \return 0 if successful, or 1 if the test failed
*/
int mbedtls_sha256_self_test( int verbose );
#ifdef __cplusplus
}
#endif
#endif /* mbedtls_sha256.h */
+1141
View File
File diff suppressed because it is too large Load Diff
+7
View File
@@ -0,0 +1,7 @@
#pragma once
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#define NOMINMAX // Prevent windows.h from defining min/max macros
// which break std::min / std::max (C2589)
// Windows Header Files
#include <windows.h>
+5
View File
@@ -0,0 +1,5 @@
// pch.cpp: source file corresponding to the pre-compiled header
#include "pch.h"
// When you are using pre-compiled headers, this source file is necessary for compilation to succeed.
+18
View File
@@ -0,0 +1,18 @@
// pch.h: This is a precompiled header file.
// Files listed below are compiled only once, improving build performance for future builds.
// This also affects IntelliSense performance, including code completion and many code browsing features.
// However, files listed here are ALL re-compiled if any one of them is updated between builds.
// Do not add files here that you will be updating frequently as this negates the performance advantage.
#ifndef PCH_H
#define PCH_H
// add headers that you want to pre-compile here
#include "framework.h"
#include <memory>
#include <string>
#include <string_view>
#include <vector>
#include <mutex>
#endif //PCH_H
+227
View File
@@ -0,0 +1,227 @@
#include "ANSLIB.h"
#include "ANSLibsLoader.h"
#include <memory>
#define LOAD_FUNC(name) \
name##Func = (name##FuncT)GetProcAddress(dllHandle, #name); \
if (!name##Func) { success = false; }
namespace ANSCENTER
{
ANSLIB::ANSLIB() {
// Ensure all shared DLLs (OpenCV, OpenVINO, TRT, ORT) are pre-loaded
ANSCENTER::ANSLibsLoader::Initialize();
const char* dllPath = "C:\\ProgramData\\ANSCENTER\\Shared\\ANSODEngine.dll"; // Hardcoded path
dllHandle = LoadLibraryA(dllPath);
if (!dllHandle) {
return;
}
bool success = true;
LOAD_FUNC(CreateANSODHandle_CS);
LOAD_FUNC(RunInference_CPP);
LOAD_FUNC(RunInferenceComplete_CPP);
LOAD_FUNC(OptimizeModelStr_CS);
LOAD_FUNC(GetEngineType);
LOAD_FUNC(LoadModelFromFolder);
LOAD_FUNC(GetActiveRect);
LOAD_FUNC(DetectMovement);
LOAD_FUNC(Optimize);
LOAD_FUNC(GetODParameters);
LOAD_FUNC(ReleaseANSODHandle);
LOAD_FUNC(UpdateDetectionMinScore);
LOAD_FUNC(SetPrompt);
LOAD_FUNC(SetTracker);
LOAD_FUNC(SetTrackerParameters);
loaded = success;
if (!loaded) {
FreeLibrary(dllHandle);
dllHandle = nullptr;
}
}
ANSLIB* ANSLIB::Create() {
try {
auto ptr = std::make_unique<ANSLIB>();
return ptr.release();
}
catch (...) { return nullptr; }
}
void ANSLIB::Destroy(ANSLIB* instance) {
std::unique_ptr<ANSLIB> ptr(instance);
}
ANSLIB::~ANSLIB() noexcept {
try {
if (ANSHandle && ReleaseANSODHandleFunc) {
ReleaseANSODHandleFunc(&ANSHandle);
ANSHandle = nullptr;
}
if (dllHandle) {
FreeLibrary(dllHandle);
dllHandle = nullptr;
}
}
catch (...) {}
}
bool ANSLIB::IsLoaded() const {
return loaded;
}
// Public APIs
int ANSLIB::Initialize(const char* licenseKey,
const char* modelFilePath,
const char* modelFileZipPassword,
float modelThreshold,
float modelConfThreshold,
float modelNMSThreshold,
int modelType,
int detectionType, int loadEngineOnCreation,std::string& labels)
{
if (!loaded) {
return -1;
}
if (CreateANSODHandle_CSFunc) {
const char* result = CreateANSODHandle_CSFunc(&ANSHandle, licenseKey, modelFilePath, modelFileZipPassword,
modelThreshold, modelConfThreshold, modelNMSThreshold,
1, modelType, detectionType, loadEngineOnCreation);
labels = (result != nullptr) ? std::string(result) : std::string();
if (labels.empty()) {
return -1;
}
else {
return 1;
}
}
return 0;
}
int ANSLIB::RunInference(cv::Mat cvImage, const char* cameraId, std::vector<ANSCENTER::Object>& detectionResult) {
if (!loaded) {
return -1;
}
if (RunInference_CPPFunc) {
auto imagePtr = std::make_unique<cv::Mat>(cvImage);
try {
cv::Mat* rawPtr = imagePtr.get();
int result = RunInference_CPPFunc(&ANSHandle, &rawPtr, cameraId, detectionResult);
return result;
}
catch (...) {
return -1;
}
}
return -1;
}
int ANSLIB::RunInferenceWithOptions(cv::Mat cvImage, const char* cameraId, const char* activeROIMode, std::vector<ANSCENTER::Object>& detectionResult) {
if (!loaded) {
return -1;
}
if (RunInferenceComplete_CPPFunc) {
auto imagePtr = std::make_unique<cv::Mat>(cvImage);
try {
cv::Mat* rawPtr = imagePtr.get();
int result = RunInferenceComplete_CPPFunc(&ANSHandle, &rawPtr, cameraId, activeROIMode,detectionResult);
return result;
}
catch (...) {
return -1;
}
}
return -1;
}
int ANSLIB::OptimizeModel(const char* modelFilePath, const char* modelFileZipPassword, int modelType, int modelDetectionType,int fp16) {
if (!loaded) {
return -1;
}
if (OptimizeModelStr_CSFunc) {
const char* result = OptimizeModelStr_CSFunc(modelFilePath, modelFileZipPassword, modelType, modelDetectionType, fp16);
if (result) {
return 1;
}
else {
return -1;
}
}
return -1;
}
const char* ANSLIB::CreateANSODHandle_CS(void** Handle, const char* licenseKey, const char* modelFilePath,
const char* modelFileZipPassword, float modelThreshold, float modelConfThreshold,
float modelNMSThreshold, int autoDetectEngine, int modelType, int detectionType, int loadEngineOnCreation) {
return CreateANSODHandle_CSFunc ? CreateANSODHandle_CSFunc(Handle, licenseKey, modelFilePath, modelFileZipPassword,
modelThreshold, modelConfThreshold, modelNMSThreshold,
autoDetectEngine, modelType, detectionType, loadEngineOnCreation)
: "Function not loaded";
}
const char* ANSLIB::OptimizeModelStr_CS(const char* modelFilePath, const char* modelFileZipPassword, int modelType, int modelDetectionType,int fp16) {
return OptimizeModelStr_CSFunc ? OptimizeModelStr_CSFunc(modelFilePath, modelFileZipPassword, modelType, modelDetectionType,fp16)
: "Function not loaded";
}
int ANSLIB::RunInference_CPP(void** Handle, cv::Mat** cvImage, const char* cameraId, std::vector<ANSCENTER::Object>& detectionResult) {
return RunInference_CPPFunc ? RunInference_CPPFunc(Handle, cvImage, cameraId, detectionResult) : -1;
}
int ANSLIB::RunInferenceComplete_CPP(void** Handle, cv::Mat** cvImage, const char* cameraId, const char* activeROIMode, std::vector<ANSCENTER::Object>& detectionResult) {
return RunInferenceComplete_CPPFunc ? RunInferenceComplete_CPPFunc(Handle, cvImage, cameraId, activeROIMode, detectionResult) : -1;
}
int ANSLIB::GetEngineType() {
if (!loaded || !GetEngineTypeFunc) return -1;
return GetEngineTypeFunc();
}
int ANSLIB::LoadModelFromFolder(const char* licenseKey, const char* modelName, const char* className,
float detectionScoreThreshold, float modelConfThreshold, float modelMNSThreshold,
int autoDetectEngine, int modelType, int detectionType, int loadEngineOnCreation, const char* modelFolder,std::string& labelMap)
{
if (!loaded || !LoadModelFromFolderFunc) return -1;
return LoadModelFromFolderFunc(&ANSHandle, licenseKey, modelName, className,
detectionScoreThreshold, modelConfThreshold, modelMNSThreshold,
autoDetectEngine, modelType, detectionType, loadEngineOnCreation,modelFolder, labelMap);
}
int ANSLIB::GetActiveRect(cv::Mat cvImage, cv::Rect& activeWindow) {
if (!loaded || !GetActiveRectFunc) return -1;
return GetActiveRectFunc(&ANSHandle, cvImage, activeWindow);
}
int ANSLIB::DetectMovement(cv::Mat image, const char* cameraId, std::vector<Object>& results) {
if (!loaded || !DetectMovementFunc) return -1;
return DetectMovementFunc(&ANSHandle, image, cameraId, results);
}
int ANSLIB::ReleaseANSODHandle(void** Handle) {
return ReleaseANSODHandleFunc ? ReleaseANSODHandleFunc(Handle) : -1;
}
cv::Rect ANSLIB::GetActiveWindow(cv::Mat cvImage) {
cv::Rect activeWindow;
if (GetActiveRect(cvImage, activeWindow) == 0) {
return activeWindow;
}
return cv::Rect(); // Return an empty rectangle if failed
}
int ANSLIB::GetODParameters(ANSCENTER::Params& param) {
if (!loaded || !GetODParametersFunc) return -1;
return GetODParametersFunc(&ANSHandle, param);
}
int ANSLIB::Optimize(bool fp16) {
if (!loaded || !OptimizeFunc) return -1;
return OptimizeFunc(&ANSHandle, fp16);
}
int ANSLIB::UpdateDetectionMinScore(float scoreThreshold) {
if (!loaded || !UpdateDetectionMinScoreFunc) return -1;
return UpdateDetectionMinScoreFunc(&ANSHandle, scoreThreshold);
}
int ANSLIB::SetPrompt(const char* text) {
if (!loaded || !SetPromptFunc) return -1;
return SetPromptFunc(&ANSHandle, text);
}
int ANSLIB::SetTracker(int trackerType, int enableTracker) {
if (!loaded || !SetTrackerFunc) return -1;
return SetTrackerFunc(&ANSHandle, trackerType, enableTracker);
}
int ANSLIB::SetTrackerParameters(const char* trackerParams) {
if (!loaded || !SetTrackerParametersFunc) return -1;
return SetTrackerParametersFunc(&ANSHandle, trackerParams);
}
}
+439
View File
@@ -0,0 +1,439 @@
#ifndef ANSLIB_H
#define ANSLIB_H
#define ANSLIB_API __declspec(dllexport)
#define CUSTOM_API __declspec(dllexport)
#pragma once
#include <windows.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/cuda.hpp>
enum DetectionType {
CLASSIFICATION = 0,
DETECTION = 1,
SEGMENTATION = 2,
FACEDETECTOR = 3,
FACERECOGNIZER = 4,
LICENSEPLATE = 5,
TEXTSCENSE = 6,
KEYPOINT = 7,
OBB = 8 // Oriented Bounding Box
};
enum ModelType {
TENSORFLOW = 0,
YOLOV4 = 1,
YOLOV5 = 2,
YOLOV8 = 3,
TENSORRT = 4,
OPENVINO = 5,
FACEDETECT = 6,
FACERECOGNIZE = 7,
ALPR = 8,
OCR = 9,
ANOMALIB = 10,
POSE = 11,
SAM = 12,
ODHUBMODEL = 13,
YOLOV10RTOD = 14, // TensorRT Object Detection for Yolov10
YOLOV10OVOD = 15, // OpenVINO Object Detection for Yolov10
CUSTOMDETECTOR = 16, // Custom Detector
YOLOV12 = 17, // YoloV12 standard for yolov12
CUSTOMPY = 18, // Custom Python script model
MOTIONDETECTOR = 19, // Motion Detector,
ONNXCL = 20,
ONNXPOSE = 21,
RTPOSE = 22,
ONNXSEG = 23,
RTSEG = 24,
ONNXOBB = 25,
RTOBB = 26,
MOVIENET = 27,
ONNXSAM3 = 28,
RTSAM3 = 29,
ONNXYOLO = 30,
RTYOLO = 31
};
enum TrackerType {
BYTETRACK = 0,
UCMC = 1,
OCSORT = 2
};
namespace ANSCENTER {
struct Point {
int x, y;
};
struct ROIConfig {
bool Rectangle;
bool Polygon;
bool Line;
int MinItems;
int MaxItems;
std::string Name;
std::string ROIMatch;
};
struct Parameter {
std::string Name;
std::string DataType;
int NoOfDecimals;
int MaxValue;
int MinValue;
std::string StartValue;
std::vector<std::string> ListItems;
std::string DefaultValue;
std::string Value;
};
struct ROIValue {
std::string ROIMatch;
std::vector<Point> ROIPoints;
std::string Option;
std::string Name;
int OriginalImageSize;
};
struct Params {
std::vector<ROIConfig> ROI_Config;
std::vector<std::string> ROI_Options;
std::vector<Parameter> Parameters;
std::vector<ROIValue> ROI_Values;
};
/* Example
{
"ROI_Config":[
{
"Rectangle":true,
"Polygon":true,
"Line":false,
"MinItems":0,
"MaxItems":3,
"Name":"Traffic Light",
"ROI-Match":"All Corners"
},
{
"Rectangle":true,
"Polygon":false,
"Line":false,
"MinItems":1,
"MaxItems":1,
"Name":"Car Zone",
"ROI-Match":"All Corners"
},
{
"Rectangle":false,
"Polygon":false,
"Line":true,
"MinItems":1,
"MaxItems":2,
"Name":"Cross Line",
"ROI-Match":"All Corners"
}
],
"ROI_Options":[
"Inside ROI",
"Inside ROI",
"Both Directions"
],
"Parameters":[
{
"Name":"Para1",
"DataType":"Boolean",
"NoOfdecimals":0,
"MaxValue":0,
"MinValue":0,
"StartValue":"",
"ListItems":[],
"DefaultValue":"",
"Value":"true"
},
{
"Name":"Para2",
"DataType":"Integer",
"NoOfdecimals":0,
"MaxValue":5,
"MinValue":1,
"StartValue":"2",
"ListItems":[],
"DefaultValue":"",
"Value":"3"
},
{
"Name":"Para3",
"DataType":"List-Single",
"NoOfdecimals":0,
"MaxValue":0,
"MinValue":0,
"StartValue":"",
"ListItems":["A","B","C"],
"DefaultValue":"",
"Value":"A"
},
{
"Name":"Para4",
"DataType":"Range",
"NoOfdecimals":0,
"MaxValue":100,
"MinValue":50,
"StartValue":">,60",
"ListItems":[">","<"],
"DefaultValue":"",
"Value":">,52.000000"
}
],
"ROI_Values":[
{
"ROI-Match":"Centre Point",
"ROIPoints":[
{"x":269,"y":134},
{"x":777,"y":134},
{"x":777,"y":457},
{"x":269,"y":457}
],
"Option":"Inside ROI",
"Name":"Car Zone 1",
"OriginalImageSize":1920
},
{
"ROI-Match":"Centre Point",
"ROIPoints":[{"x":280,"y":613},{"x":1108,"y":280}],
"Option":"Above",
"Name":"Cross Line 1",
"OriginalImageSize":1920
},
{
"ROI-Match":"Centre Point",
"ROIPoints":[{"x":1511,"y":383},{"x":1283,"y":754}],
"Option":"Left side",
"Name":"Cross Line 2",
"OriginalImageSize":1920
},
{
"ROI-Match":"Centre Point",
"ROIPoints":[
{"x":229,"y":161},
{"x":964,"y":161},
{"x":964,"y":628},
{"x":229,"y":628}
],
"Option":"Left side",
"Name":"Traffic Light 1",
"OriginalImageSize":1920
},
{
"ROI-Match":"Centre Point",
"ROIPoints":[
{"x":1115,"y":304},
{"x":1730,"y":304},
{"x":1730,"y":695},
{"x":1115,"y":695}
],
"Option":"Left side",
"Name":"Traffic Light 2",
"OriginalImageSize":1920
},
{
"ROI-Match":"Centre Point",
"ROIPoints":[
{"x":678,"y":683},
{"x":1217,"y":683},
{"x":1217,"y":1026},
{"x":678,"y":1026}
],
"Option":"Left side",
"Name":"Traffic Light 3",
"OriginalImageSize":1920
}
]
}
*/
struct Object
{
int classId{ 0 };
int trackId{ 0 };
std::string className{};
float confidence{ 0.0 };
cv::Rect box{};
std::vector<cv::Point2f> polygon; // Polygon that contain x1,y1,x2,y2,x3,y3,x4,y4
cv::Mat mask{}; // Face image in box (cropped) or mask image for segmentation
cv::cuda::GpuMat gpuMask{}; // GPU-resident face crop (set by NV12 affine warp, avoids re-upload)
std::vector<float> kps{}; // Pose exsimate 17 keypoints or oriented bouding box (xcenter, ycenter, width, height, angle)
std::string extraInfo; // More information such as facial recognition
std::string cameraId; // Use to check if this object belongs to any camera
//std::string attributes; // Attributes such as keypoint string
};
class ANSLIB_API ANSLIB {
public:
ANSLIB();
~ANSLIB() noexcept;
[[nodiscard]] static ANSLIB* Create();
static void Destroy(ANSLIB* instance);
[[nodiscard]] int Initialize(const char* licenseKey,
const char* modelFilePath,
const char* modelFileZipPassword,
float modelThreshold,
float modelConfThreshold,
float modelNMSThreshold,
int modelType,
int detectionType, int loadEngineOnCreation,std::string &labels);
[[nodiscard]] int RunInference(cv::Mat cvImage, const char* cameraId, std::vector<ANSCENTER::Object>& detectionResult);
[[nodiscard]] int RunInferenceWithOptions(cv::Mat cvImage, const char* cameraId, const char* activeROIMode, std::vector<ANSCENTER::Object>& detectionResult);
[[nodiscard]] int OptimizeModel(const char* modelFilePath, const char* modelFileZipPassword, int modelType, int modelDetectionType,int fp16);
[[nodiscard]] int Optimize(bool fp16); // Perform optimization on the loaded model on current model folder
[[nodiscard]] int GetEngineType();
[[nodiscard]] int LoadModelFromFolder(const char* licenseKey, const char* modelName, const char* className,
float detectionScoreThreshold, float modelConfThreshold, float modelMNSThreshold,
int autoDetectEngine, int modelType, int detectionType, int loadEngineOnCreation, const char* modelFolder, std::string& labelMap);
[[nodiscard]] int DetectMovement(cv::Mat image, const char* cameraId, std::vector<Object>& results);
[[nodiscard]] cv::Rect GetActiveWindow(cv::Mat cvImage);
[[nodiscard]] int SetPrompt(const char* text);
[[nodiscard]] int SetTracker(int trackerType, int enableTracker);
[[nodiscard]] int SetTrackerParameters(const char* trackerParams);
private:
HMODULE dllHandle = nullptr;
bool loaded = false;
void* ANSHandle = nullptr;
const char* CreateANSODHandle_CS(void** Handle,
const char* licenseKey,
const char* modelFilePath,
const char* modelFileZipPassword,
float modelThreshold,
float modelConfThreshold,
float modelNMSThreshold,
int autoDetectEngine,
int modelType,
int detectionType,
int loadEngineOnCreation);
int RunInference_CPP(void** Handle, cv::Mat** cvImage, const char* cameraId, std::vector<ANSCENTER::Object>& detectionResult);
int RunInferenceComplete_CPP(void** Handle, cv::Mat** cvImage, const char* cameraId, const char* activeROIMode,std::vector<ANSCENTER::Object>& detectionResult);
const char* OptimizeModelStr_CS(const char* modelFilePath, const char* modelFileZipPassword, int modelType, int modelDetectionType,int fp16);
int ReleaseANSODHandle(void** Handle);
int GetActiveRect(cv::Mat cvImage, cv::Rect& activeWindow);
int GetODParameters(ANSCENTER::Params& param);
int UpdateDetectionMinScore(float scoreThreshold);
[[nodiscard]] bool IsLoaded() const;
typedef const char* (*CreateANSODHandle_CSFuncT)(void**, const char*, const char*, const char*, float, float, float, int, int, int, int);
typedef const char* (*OptimizeModelStr_CSFuncT)(const char*, const char*, int, int, int);
typedef int (*RunInference_CPPFuncT)(void**, cv::Mat**, const char*, std::vector<ANSCENTER::Object>& detectionResult);
typedef int (*RunInferenceComplete_CPPFuncT)(void**, cv::Mat**, const char*, const char*, std::vector<ANSCENTER::Object>& detectionResult);
typedef int (*ReleaseANSODHandleFuncT)(void**);
typedef int(*GetEngineTypeFuncT)();
typedef int(*LoadModelFromFolderFuncT)(void**, const char*, const char*, const char*, float, float, float, int, int, int,int, const char*,std::string&);
typedef int(*GetActiveRectFuncT)(void**, cv::Mat, cv::Rect&);
typedef int(*DetectMovementFuncT)(void**, cv::Mat, const char*, std::vector<ANSCENTER::Object>&);
typedef int(*OptimizeFuncT)(void**, bool);
typedef int(*GetODParametersFuncT)(void**, ANSCENTER::Params&);
typedef int(*UpdateDetectionMinScoreFuncT)(void**, float); // Add 'Func' to match other typedefs
typedef int(*SetPromptFuncT)(void**, const char*);
typedef int(*SetTrackerFuncT)(void**, int, int);
typedef int(*SetTrackerParametersFuncT)(void**, const char*);
CreateANSODHandle_CSFuncT CreateANSODHandle_CSFunc = nullptr;
OptimizeModelStr_CSFuncT OptimizeModelStr_CSFunc = nullptr;
ReleaseANSODHandleFuncT ReleaseANSODHandleFunc = nullptr;
RunInference_CPPFuncT RunInference_CPPFunc = nullptr;
RunInferenceComplete_CPPFuncT RunInferenceComplete_CPPFunc = nullptr;
GetEngineTypeFuncT GetEngineTypeFunc = nullptr;
LoadModelFromFolderFuncT LoadModelFromFolderFunc = nullptr;
GetActiveRectFuncT GetActiveRectFunc = nullptr;
DetectMovementFuncT DetectMovementFunc = nullptr;
OptimizeFuncT OptimizeFunc = nullptr;
GetODParametersFuncT GetODParametersFunc = nullptr;
UpdateDetectionMinScoreFuncT UpdateDetectionMinScoreFunc = nullptr;
SetPromptFuncT SetPromptFunc = nullptr;
SetTrackerFuncT SetTrackerFunc = nullptr;
SetTrackerParametersFuncT SetTrackerParametersFunc = nullptr;
};
}
struct CustomObject
{
int classId{ 0 };
int trackId{ 0 };
std::string className{};
float confidence{ 0.0 };
cv::Rect box{};
std::vector<cv::Point2f> polygon; // Polygon that contain x1,y1,x2,y2,x3,y3,x4,y4
cv::Mat mask{}; // Face image in box (cropped) or mask image for segmentation
cv::cuda::GpuMat gpuMask{}; // GPU-resident face crop (set by NV12 affine warp, avoids re-upload)
std::vector<float> kps{}; // Pose exsimate 17 keypoints or oriented bouding box (xcenter, ycenter, width, height, angle)
std::string extraInfo; // More information such as facial recognition
std::string cameraId; // Use to check if this object belongs to any camera
//std::string attributes; // Attributes such as keypoint string
};
struct CustomPoint { int x, y; };
struct CustomROIConfig {
bool Rectangle;
bool Polygon;
bool Line;
int MinItems;
int MaxItems;
std::string Name;
std::string ROIMatch;
};
struct CustomParameter {
std::string Name;
std::string DataType;
int NoOfDecimals;
int MaxValue;
int MinValue;
std::string StartValue;
std::vector<std::string> ListItems;
std::string DefaultValue;
std::string Value;
};
struct CustomROIValue {
std::string ROIMatch;
std::vector<CustomPoint> ROIPoints;
std::string Option;
std::string Name;
int OriginalImageSize;
};
struct CustomParams {
std::vector<CustomROIConfig> ROI_Config;
std::vector<std::string> ROI_Options;
std::vector<CustomParameter> Parameters;
std::vector<CustomROIValue> ROI_Values;
};
class CUSTOM_API IANSCustomClass
{
protected:
std::string _modelDirectory; // The directory where the model is located
float _detectionScoreThreshold{ 0.5 };
CustomParams _params; // Parameters for the model
bool _loadEngineOnCreate{ false }; // Load engine on create
public:
virtual bool Initialize(const std::string& modelDirectory, float detectionScoreThreshold, std::string& labelMap) = 0;
virtual bool OptimizeModel(bool fp16) = 0;
virtual std::vector<CustomObject> RunInference(const cv::Mat& input) = 0;
virtual std::vector<CustomObject> RunInference(const cv::Mat& input, const std::string& camera_id) = 0;
virtual bool ConfigureParameters(CustomParams& param) = 0;
virtual bool Destroy() = 0;
bool SetParameters(const CustomParams& param) {
try {
this->_params.ROI_Config.clear();
for (auto& cf : param.ROI_Config) {
this->_params.ROI_Config.push_back(cf);
}
this->_params.ROI_Options.clear();
for (auto& op : param.ROI_Options) {
this->_params.ROI_Options.push_back(op);
}
this->_params.Parameters.clear();
for (auto& par : param.Parameters) {
this->_params.Parameters.push_back(par);
}
this->_params.ROI_Values.clear();
for (auto& roi : param.ROI_Values) {
this->_params.ROI_Values.push_back(roi);
}
return true;
}
catch (...) {
return false;
}
};
void SetLoadEngineOnCreate(bool loadEngineOnCreate) {
this->_loadEngineOnCreate = loadEngineOnCreate;
}
};
#endif
+30
View File
@@ -0,0 +1,30 @@
# ANSLIB — Main facade DLL (aggregates all modules)
add_library(ANSLIB SHARED
ANSLIB.cpp
ANSLIB.h
dllmain.cpp
pch.cpp
pch.h
framework.h
)
target_include_directories(ANSLIB PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${SHARED_INCLUDE_DIR}
)
target_link_libraries(ANSLIB
PUBLIC ANSLibsLoader
PUBLIC opencv
# The facade links against all modules
PRIVATE ANSODEngine
PRIVATE ANSCV
PRIVATE ANSMOT
PRIVATE ANSUtilities
PRIVATE ANSOCR
PRIVATE ANSFR
PRIVATE ANSLPR
)
target_compile_definitions(ANSLIB PRIVATE UNICODE _UNICODE ANSLIB_EXPORTS _USRDLL)
target_precompile_headers(ANSLIB PRIVATE pch.h)
+19
View File
@@ -0,0 +1,19 @@
// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
) noexcept
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
+7
View File
@@ -0,0 +1,7 @@
#pragma once
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#define NOMINMAX // Prevent windows.h from defining min/max macros
// which break std::min / std::max (C2589)
// Windows Header Files
#include <windows.h>
+5
View File
@@ -0,0 +1,5 @@
// pch.cpp: source file corresponding to the pre-compiled header
#include "pch.h"
// When you are using pre-compiled headers, this source file is necessary for compilation to succeed.
+18
View File
@@ -0,0 +1,18 @@
// pch.h: This is a precompiled header file.
// Files listed below are compiled only once, improving build performance for future builds.
// This also affects IntelliSense performance, including code completion and many code browsing features.
// However, files listed here are ALL re-compiled if any one of them is updated between builds.
// Do not add files here that you will be updating frequently as this negates the performance advantage.
#ifndef PCH_H
#define PCH_H
// add headers that you want to pre-compile here
#include "framework.h"
#include <memory>
#include <string>
#include <string_view>
#include <vector>
#include <mutex>
#endif //PCH_H
+666
View File
@@ -0,0 +1,666 @@
#include "ANSLLM.h"
#include <CkGlobal.h>
#include <CkBinData.h>
#include <CkByteData.h>
#include <CkStringTable.h>
#include <CkStringBuilder.h>
#include <CkJsonObject.h>
static bool ansllmLicenceValid = false;
static std::once_flag ansllmLicenseOnceFlag;
namespace ANSCENTER
{
static void VerifyGlobalANSLMLicense(const std::string& licenseKey) {
try {
static const std::vector<std::pair<int, std::string>> licenseChecks = {
{1000, "ANNHUB-LV"},
{1001, "DLHUB-LV"},
{1002, "ODHUB-LV"},
{1003, "ANSVIS"},
{1004, "ANSFR"},
{1005, "ANSOCR"},
{1006, "ANSALPR"},
{1007, "ANSCV"},
{1008, "ANSSRT"}
};
ansllmLicenceValid = false;
for (const auto& [productId, productName] : licenseChecks) {
if (ANSCENTER::ANSLicenseHelper::LicenseVerification(licenseKey, productId, productName)) {
ansllmLicenceValid = true;
break; // Stop at the first valid license
}
}
}
catch (const std::exception& e) {
ansllmLicenceValid = false;
}
}
ANSLLM::ANSLLM() {
_unlockCode = "ANSDRC.CB1122026_MEQCIFwO1IFQCG0BhZwsXFO68QUU6mDB5uge4duOsqOJanEyAiAB67ahqnXin4SRy0vIegISgbFlpldmbuS5gbU21GYVqA==";// "ANSDRC.CB1082025_Ax6P3M7F8B3d";//
}
ANSLLM::~ANSLLM() noexcept = default;
void ANSLLM::CheckLicense() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
// Check once globally
std::call_once(ansllmLicenseOnceFlag, [this]() {
VerifyGlobalANSLMLicense(_licenseKey);
});
// Update this instance's local license flag
_isLicenseValid = ansllmLicenceValid;
}
catch (const std::exception& e) {
this->_logger.LogFatal("ANSLLM::CheckLicense. Error:", e.what(), __FILE__, __LINE__);
}
}
void ANSLLM::CheckUnlockCode() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
CkGlobal glob;
_unlockCode = "ANSDRC.CB1122026_MEQCIFwO1IFQCG0BhZwsXFO68QUU6mDB5uge4duOsqOJanEyAiAB67ahqnXin4SRy0vIegISgbFlpldmbuS5gbU21GYVqA==";// "ANSDRC.CB1082025_Ax6P3M7F8B3d";
_isUnlockCodeValid = glob.UnlockBundle(_unlockCode.c_str());
if (!_isUnlockCodeValid) {
_logger.LogFatal("ANSLLM::CheckUnlockCode", glob.lastErrorText(), __FILE__, __LINE__);
return;
}
int status = glob.get_UnlockStatus();
if (status != 2) {
_logger.LogDebug("ANSLLM::CheckUnlockCode", "Unlocked in trial mode.", __FILE__, __LINE__);
}
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::CheckUnlockCode", e.what(), __FILE__, __LINE__);
}
}
bool ANSLLM::Initialize(const std::string& licenseKey, bool localLLM) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
_licenseKey = licenseKey;
_localLLM = localLLM;
CheckLicense();
CheckUnlockCode();
if (_localLLM) {
// Auto-configure for local Ollama: use custom provider with ChatCompletions API spec
// Must use "custom" — setting "openai" resets BaseUrl to api.openai.com
ai.put_Provider("custom");
ai.put_ApiSpec("ChatCompletions");
ai.put_BaseUrl(_ollamaBaseUrl.c_str());
ai.put_ApiKey("ollama"); // Ollama doesn't require a real API key
_logger.LogDebug("ANSLLM::Initialize", "Configured for local Ollama at " + _ollamaBaseUrl, __FILE__, __LINE__);
}
return _isLicenseValid && _isUnlockCodeValid;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::Initialize", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSLLM::SetLLMProvider(const std::string& llmProvider) {
if (llmProvider.empty()) return false;
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
ai.put_Provider(llmProvider.c_str());
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::SetLLMProvider", e.what(), __FILE__, __LINE__);
return false;
}
}
std::string ANSLLM::GetLLMProvider() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
CkString str;
ai.get_Provider(str);
const char* cstr = str.getString();
return (cstr != nullptr) ? std::string(cstr) : std::string();
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::GetLLMProvider", e.what(), __FILE__, __LINE__);
return "";
}
}
bool ANSLLM::SetApiKey(const std::string& apiKey) {
if (apiKey.empty()) return false;
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
ai.put_ApiKey(apiKey.c_str());
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::SetApiKey", e.what(), __FILE__, __LINE__);
return false;
}
}
std::vector<std::string> ANSLLM::GetModelList() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
std::vector<std::string> modelList;
try {
CkStringTable st;
if (!ai.GetModels(st)) {
const char* errText = ai.lastErrorText();
const std::string errorMsg = (errText != nullptr) ? errText : "Unknown error";
_logger.LogError("ANSLLM::GetModelList",
"Failed to get models: " + errorMsg,
__FILE__, __LINE__);
return modelList;
}
const int count = st.get_Count();
if (count <= 0) {
return modelList;
}
modelList.reserve(static_cast<size_t>(count));
for (int i = 0; i < count; ++i) {
const char* modelName = st.stringAt(i);
if (modelName != nullptr && modelName[0] != '\0') {
modelList.emplace_back(modelName);
}
}
return modelList;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::GetModelList",
std::string("Exception: ") + e.what(),
__FILE__, __LINE__);
return modelList;
}
catch (...) {
_logger.LogFatal("ANSLLM::GetModelList",
"Unknown exception occurred",
__FILE__, __LINE__);
return modelList;
}
}
bool ANSLLM::SetModel(const std::string& modelName) {
if (modelName.empty()) return false;
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
ai.put_Model(modelName.c_str());
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::SetModel", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSLLM::SetTranscriptMode(bool transcriptMode) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
ai.put_FullTranscript(transcriptMode);
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::SetTranscriptMode", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSLLM::InputAddText(const std::string& inputText) {
if (inputText.empty()) return false;
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
ai.InputAddText(inputText.c_str());
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::InputAddText", e.what(), __FILE__, __LINE__);
return false;
}
}
std::string ANSLLM::Ask(const std::string& prompt) {
if (prompt.empty()) return "";
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
// Ask the AI for text output.
bool success = ai.Ask(prompt.c_str());
if (success == false) {
const char* errText = ai.lastErrorText();
return (errText != nullptr) ? std::string(errText) : std::string("Unknown error");
}
// Get the text response.
CkStringBuilder sbResponse;
ai.GetOutputTextSb(sbResponse);
const char* cstr = sbResponse.getAsString();
return (cstr != nullptr) ? std::string(cstr) : std::string();
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::Ask", e.what(), __FILE__, __LINE__);
return "";
}
}
bool ANSLLM::CreateConversation(const std::string& systemMessage, const std::string& developerMessage, const std::string& conversationName) {
if (systemMessage.empty() || developerMessage.empty() || conversationName.empty()) {
return false;
}
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
return ai.NewConvo(conversationName.c_str(), systemMessage.c_str(), developerMessage.c_str());
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::CreateConversation", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSLLM::SetConversation(const std::string& conversationName) {
if (conversationName.empty()) return false;
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
ai.put_SelectedConvo(conversationName.c_str());
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::SetConversation", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSLLM::InputAddImageUrl(const std::string& imageURL, const std::string& summary) {
if (imageURL.empty()) return false;
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
return ai.InputAddImageUrl(imageURL.c_str(), summary.c_str());
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::InputAddImageUrl", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSLLM::InputAddImageFromPath(const std::string& filePath, const char* summary) {
if (filePath.empty()) return false;
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
CkBinData bd;
bool success = bd.LoadFile(filePath.c_str());
if (!success) {
const char* errText = bd.lastErrorText();
_logger.LogError("ANSLLM::InputAddImageFromPath",
(errText != nullptr) ? errText : "Failed to load file",
__FILE__, __LINE__);
return false;
}
return ai.InputAddImageData(bd, summary);
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::InputAddImageFromPath", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSLLM::InputAddImageFromBase64(const std::string& jpegBase64String, const char* summary) {
if (jpegBase64String.empty()) return false;
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
CkBinData bd;
// Load base64 encoded JPEG data into CkBinData
bool success = bd.AppendEncoded(jpegBase64String.c_str(), "base64");
if (!success) {
_logger.LogError("ANSLLM::InputAddImage",
bd.lastErrorText(),
__FILE__, __LINE__);
return false;
}
return ai.InputAddImageData(bd, summary);
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::InputAddImage", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSLLM::InputAddFileData(const std::string& filePath, const char* summary) {
if (filePath.empty()) return false;
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
CkBinData bd;
bool success = bd.LoadFile(filePath.c_str());
if (!success) {
const char* errText = bd.lastErrorText();
_logger.LogError("ANSLLM::InputAddFileData",
(errText != nullptr) ? errText : "Failed to load file",
__FILE__, __LINE__);
return false;
}
return ai.InputAddImageData(bd, summary);
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::InputAddFileData", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSLLM::InputAddFileURL(const std::string& fileURL, const char* summary) {
if (fileURL.empty()) return false;
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
return ai.InputAddFileUrl(fileURL.c_str(), summary);
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::InputAddFileURL", e.what(), __FILE__, __LINE__);
return false;
}
}
// Extra methods
bool ANSLLM::ClearInputs() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
ai.InputClear();
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::ClearInputs", e.what(), __FILE__, __LINE__);
return false;
}
}
std::vector<std::string> ANSLLM::GetConversationList() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
std::vector<std::string> convList;
try {
CkStringTable st;
if (!ai.ListConvos(st)) {
const char* errText = ai.lastErrorText();
const std::string errorMsg = (errText != nullptr) ? errText : "Unknown error";
_logger.LogError("ANSLLM::GetConversationList",
"Failed to get conversations: " + errorMsg,
__FILE__, __LINE__);
return convList;
}
const int count = st.get_Count();
if (count <= 0) {
return convList;
}
convList.reserve(static_cast<size_t>(count));
for (int i = 0; i < count; ++i) {
const char* convName = st.stringAt(i);
if (convName != nullptr && convName[0] != '\0') {
convList.emplace_back(convName);
}
}
return convList;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::GetConversationList", e.what(), __FILE__, __LINE__);
return {};
}
}
bool ANSLLM::DeleteConversation(const std::string& conversationName) {
if (conversationName.empty()) return false;
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
return ai.DeleteConvo(conversationName.c_str());
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::DeleteConversation", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSLLM::SetUTF8(bool b) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
ai.put_Utf8(b);
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::SetUTF8", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSLLM::GetUTF8() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
return ai.get_Utf8();
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::GetUTF8", e.what(), __FILE__, __LINE__);
return false;
}
}
int ANSLLM::PollAi(bool abort) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
return ai.PollAi(abort);
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::PollAi", e.what(), __FILE__, __LINE__);
return -1;
}
}
bool ANSLLM::SetLocalLLM(bool localLLM) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
_localLLM = localLLM;
if (_localLLM) {
ai.put_Provider("custom");
ai.put_ApiSpec("ChatCompletions");
ai.put_BaseUrl(_ollamaBaseUrl.c_str());
ai.put_ApiKey("ollama");
_logger.LogDebug("ANSLLM::SetLocalLLM", "Configured for local Ollama at " + _ollamaBaseUrl, __FILE__, __LINE__);
}
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::SetLocalLLM", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSLLM::SetOllamaBaseUrl(const std::string& baseUrl) {
if (baseUrl.empty()) return false;
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
_ollamaBaseUrl = baseUrl;
if (_localLLM) {
ai.put_BaseUrl(_ollamaBaseUrl.c_str());
}
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::SetOllamaBaseUrl", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSLLM::SetBaseURL(const std::string& baseURL) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
ai.put_BaseUrl(baseURL.c_str());
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::SetBaseURL", e.what(), __FILE__, __LINE__);
return false;
}
}
int ANSLLM::GetHeartbeatMs() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
return ai.get_HeartbeatMs();
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::GetHeartbeatMs", e.what(), __FILE__, __LINE__);
return -1;
}
}
bool ANSLLM::SetHeartbeatMs(int heartbeatMs) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
ai.put_HeartbeatMs(heartbeatMs);
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::SetHeartbeatMs", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSLLM::GetHostedConversation() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
return ai.get_HostedConvo();
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::GetHostedConversation", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSLLM::SetHostedConversation(bool hostedConversation) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
ai.put_HostedConvo(hostedConversation);
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::SetHostedConversation", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSLLM::GetStreamingMode() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
return ai.get_Streaming();
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::GetStreamingMode", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSLLM::SetStreamingMode(bool streamingMode) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
ai.put_Streaming(streamingMode);
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::SetStreamingMode", e.what(), __FILE__, __LINE__);
return false;
}
}
int ANSLLM::GetIdleTimeoutMs() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
return ai.get_IdleTimeoutMs();
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::GetIdleTimeoutMs", e.what(), __FILE__, __LINE__);
return -1;
}
}
bool ANSLLM::SetIdleTimeoutMs(int idleTimeoutMs) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
ai.put_IdleTimeoutMs(idleTimeoutMs);
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::SetIdleTimeoutMs", e.what(), __FILE__, __LINE__);
return false;
}
}
std::string ANSLLM::GetLastErrorMessage() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
const char* errorText = ai.lastErrorText();
return (errorText != nullptr) ? std::string(errorText) : std::string();
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::GetLastErrorMessage", e.what(), __FILE__, __LINE__);
return e.what();
}
}
std::string ANSLLM::GetOutputText() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
CkString str;
ai.GetOutputText(str);
const char* cstr = str.getString();
return (cstr != nullptr) ? std::string(cstr) : std::string();
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::GetOutputText", e.what(), __FILE__, __LINE__);
return "";
}
}
std::string ANSLLM::GetOutputTextSb() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
CkStringBuilder sb;
ai.GetOutputTextSb(sb);
const char* cstr = sb.getAsString();
return (cstr != nullptr) ? std::string(cstr) : std::string();
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::GetOutputTextSb", e.what(), __FILE__, __LINE__);
return "";
}
}
bool ANSLLM::SetSleepMs(int sleepMs) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
ai.SleepMs(sleepMs);
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::SetSleepMs", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSLLM::SetAskParams(double temperature) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
CkJsonObject obj;
std::string strTemp = std::to_string(temperature);
obj.UpdateString("temperature", strTemp.c_str());
ai.SetAskParams(obj);
return true;
}
catch (const std::exception& e) {
_logger.LogFatal("ANSLLM::SetAskParams", e.what(), __FILE__, __LINE__);
return false;
}
}
}
+146
View File
@@ -0,0 +1,146 @@
#ifndef ANSLLM_H
#define ANSLLM_H
#define ANSLLM_API __declspec(dllexport)
#pragma once
#include "LabVIEWHeader/extcode.h"
#include <map>
#include <string>
#include <memory>
#include "ANSLicense.h"
#include <CkAi.h>
//#include <CkRest.h>
//#include "CkAuthGoogle.h"
//#include "CkByteData.h"
//#include <CkAuthAws.h>
//#include <CkXml.h>
//#include <CkCrypt2.h>
//#include <CkSocket.h>
#include <opencv2/opencv.hpp>
namespace ANSCENTER {
class ANSLLM_API ANSLLM {
private:
// Protects all shared mutable data
mutable std::recursive_mutex _mutex;
bool _isUnlockCodeValid{ false };
bool _isLicenseValid{ false };
bool _localLLM{ false };
std::string _licenseKey;
std::string _unlockCode;
std::string _ollamaBaseUrl{ "http://localhost:11434/v1" };
SPDLogger& _logger = SPDLogger::GetInstance("ANSLLM", false);
CkAi ai;
public:
ANSLLM();
~ANSLLM() noexcept;
[[nodiscard]] bool Initialize(const std::string& licenseKey, bool localLLM = false);
void CheckLicense();
void CheckUnlockCode();
bool SetLLMProvider(const std::string& llmProvider);
[[nodiscard]] std::string GetLLMProvider();
bool SetApiKey(const std::string& apiKey);
[[nodiscard]] std::vector<std::string> GetModelList();
bool SetTranscriptMode(bool transcriptMode);
bool SetModel(const std::string& modelName);
bool InputAddText(const std::string& inputText);
bool InputAddImageUrl(const std::string& imageURL, const std::string& summary);
bool InputAddImageFromPath(const std::string& filePath, const char* summary);
bool InputAddImageFromBase64(const std::string& base64Image, const char* summary);
bool InputAddFileURL(const std::string& fileURL, const char* summary);
bool InputAddFileData(const std::string& filePath, const char* summary);
bool ClearInputs();
[[nodiscard]] std::vector<std::string> GetConversationList();
bool CreateConversation(const std::string& systemMessage, const std::string& developerMessage, const std::string& conversationName);
bool SetConversation(const std::string& conversationName);
bool DeleteConversation(const std::string& conversationName);
[[nodiscard]] std::string Ask(const std::string& prompt);
// Other methods and members
bool SetUTF8(bool b);
[[nodiscard]] bool GetUTF8();
// PollAi
[[nodiscard]] int PollAi(bool abort);
// Local LLM methods
bool SetLocalLLM(bool localLLM);
bool SetOllamaBaseUrl(const std::string& baseUrl);
bool SetBaseURL(const std::string& baseURL);
[[nodiscard]] int GetHeartbeatMs();
bool SetHeartbeatMs(int heartbeatMs);
[[nodiscard]] bool GetHostedConversation();
bool SetHostedConversation(bool hostedConversation);
[[nodiscard]] bool GetStreamingMode();
bool SetStreamingMode(bool streamingMode);
[[nodiscard]] int GetIdleTimeoutMs();
bool SetIdleTimeoutMs(int idleTimeoutMs);
bool SetSleepMs(int sleepMs);
[[nodiscard]] std::string GetLastErrorMessage();
// Parameters methods
[[nodiscard]] std::string GetOutputText();
[[nodiscard]] std::string GetOutputTextSb();
bool SetAskParams(double temperature);
//bool SetProviderParams(std::string providerParams);
};
}
extern "C" ANSLLM_API int CreateANSLLMHandle(ANSCENTER::ANSLLM** Handle, const char* licenseKey, int localLLM = 0);
extern "C" ANSLLM_API int ReleaseANSLLMHandle(ANSCENTER::ANSLLM** Handle);
extern "C" ANSLLM_API int SetANSLLMProvider(ANSCENTER::ANSLLM** Handle, const char* provider);
extern "C" ANSLLM_API int GetANSLLMProvider(ANSCENTER::ANSLLM** Handle, LStrHandle provider);
extern "C" ANSLLM_API int SetApiKey(ANSCENTER::ANSLLM** Handle, const char* apiKey);
extern "C" ANSLLM_API int ANSLLMGetModelList(ANSCENTER::ANSLLM** Handle, LStrHandle modelList);
extern "C" ANSLLM_API int ANSLLMSetTranscriptMode(ANSCENTER::ANSLLM** Handle, int transcriptMode);
extern "C" ANSLLM_API int ANSLLMSetModel(ANSCENTER::ANSLLM** Handle, const char* modelName);
extern "C" ANSLLM_API int ANSLLMInputAddText(ANSCENTER::ANSLLM** Handle, const char* inputText);
extern "C" ANSLLM_API int ANSLLMInputAddImageUrl(ANSCENTER::ANSLLM** Handle, const char* imageURL, const char* summary);
extern "C" ANSLLM_API int ANSLLMInputAddImageFromPath(ANSCENTER::ANSLLM** Handle, const char* filePath, const char* summary);
extern "C" ANSLLM_API int ANSLLMInputAddImageFromBase64(ANSCENTER::ANSLLM** Handle, const char* base64Image, const char* summary);
extern "C" ANSLLM_API int ANSLLMInputAddFileURL(ANSCENTER::ANSLLM** Handle, const char* fileURL, const char* summary);
extern "C" ANSLLM_API int ANSLLMInputAddFileData(ANSCENTER::ANSLLM** Handle, const char* filePath, const char* summary);
extern "C" ANSLLM_API int ANSLLMCreateConversation(ANSCENTER::ANSLLM** Handle, const char* systemMessage, const char* developerMessage, const char* conversationName);
extern "C" ANSLLM_API int ANSLLMSetConversation(ANSCENTER::ANSLLM** Handle, const char* conversationName);
extern "C" ANSLLM_API int ANSLLMAsk(ANSCENTER::ANSLLM** Handle, const char* prompt, LStrHandle response);
extern "C" ANSLLM_API int ANSLLMClearInput(ANSCENTER::ANSLLM** Handle);
extern "C" ANSLLM_API int ANSLLMGetConversationList(ANSCENTER::ANSLLM** Handle, LStrHandle conversationList);
extern "C" ANSLLM_API int ANSLLMDeleteConversation(ANSCENTER::ANSLLM** Handle, const char* conversationName);
extern "C" ANSLLM_API int ANSLLMSetUTF8(ANSCENTER::ANSLLM** Handle, int utf8Mode);
extern "C" ANSLLM_API int ANSLLMGetUTF8(ANSCENTER::ANSLLM** Handle);
extern "C" ANSLLM_API int ANSLLMPollAi(ANSCENTER::ANSLLM** Handle, int abort);
extern "C" ANSLLM_API int ANSLLMSetLocalLLM(ANSCENTER::ANSLLM** Handle, int localLLM);
extern "C" ANSLLM_API int ANSLLMSetOllamaBaseUrl(ANSCENTER::ANSLLM** Handle, const char* baseUrl);
extern "C" ANSLLM_API int ANSLLMSetBaseURL(ANSCENTER::ANSLLM** Handle, const char* baseURL);
extern "C" ANSLLM_API int ANSLLMSetHeartbeatMs(ANSCENTER::ANSLLM** Handle, int heartbeatMs);
extern "C" ANSLLM_API int ANSLLMGetHeartbeatMs(ANSCENTER::ANSLLM** Handle);
extern "C" ANSLLM_API int ANSLLMSetHostedConversation(ANSCENTER::ANSLLM** Handle, int hostedConversationMode);
extern "C" ANSLLM_API int ANSLLMGetHostedConversation(ANSCENTER::ANSLLM** Handle);
extern "C" ANSLLM_API int ANSLLMGetStreamingMode(ANSCENTER::ANSLLM** Handle);
extern "C" ANSLLM_API int ANSLLMSetStreamingMode(ANSCENTER::ANSLLM** Handle, int streamingMode);
extern "C" ANSLLM_API int ANSLLMGetIdleTimeoutMs(ANSCENTER::ANSLLM** Handle);
extern "C" ANSLLM_API int ANSLLMSetIdleTimeoutMs(ANSCENTER::ANSLLM** Handle, int idleTimeoutMs);
extern "C" ANSLLM_API int ANSLLMSetSleepMs(ANSCENTER::ANSLLM** Handle, int sleepMs);
extern "C" ANSLLM_API int ANSLLMGetLastErrorMessage(ANSCENTER::ANSLLM** Handle, LStrHandle LastErrorMessage);
extern "C" ANSLLM_API int ANSLLMGetOutputText(ANSCENTER::ANSLLM** Handle, LStrHandle outputText);
extern "C" ANSLLM_API int ANSLLMGetOutputTextSb(ANSCENTER::ANSLLM** Handle, LStrHandle outputTextSb);
#endif
+26
View File
@@ -0,0 +1,26 @@
# ANSLLM — LLM Integration DLL
add_library(ANSLLM SHARED
ANSLLM.cpp
ANSLLM.h
dllmain.cpp
pch.cpp
pch.h
framework.h
)
target_include_directories(ANSLLM PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${SHARED_INCLUDE_DIR}
)
target_link_libraries(ANSLLM
PRIVATE ANSLicensingSystem
PRIVATE anslicensing
PRIVATE labview
PRIVATE opencv
PRIVATE chilkat
PRIVATE boost
)
target_compile_definitions(ANSLLM PRIVATE UNICODE _UNICODE ANSLLM_EXPORTS _USRDLL)
target_precompile_headers(ANSLLM PRIVATE pch.h)
+1228
View File
File diff suppressed because it is too large Load Diff
+7
View File
@@ -0,0 +1,7 @@
#pragma once
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#define NOMINMAX // Prevent windows.h from defining min/max macros
// which break std::min / std::max (C2589)
// Windows Header Files
#include <windows.h>

Some files were not shown because too many files have changed in this diff Show More