46 Commits
Author SHA1 Message Date
nghianguyenandClaude Opus 5 c82b2a67bc feat(ANSModelPackage): take the base key the ceremony gives this build
The reader has been able to open packages for a while and has held no
key, so it refused all of them. This is the seam that hands it one.

The base private key is not in this repository and never will be. It is
written by the ceremony (ANSMINDCORE's ansmodel-keys) into a generated
header that is gitignored and produced from the key vault on the machine
that builds a release. ANSModelKeys.h uses that file when it is there
and says "no keys" when it is not, so a developer's checkout builds and
runs with no secret in it: every zip still loads, and a package is
refused with a sentence that says why.

Keys are settled once — what the build carries, plus a developer's
environment — unless a caller sets a keyring outright, which now means
exactly that rather than depending on which call ran first.

Checked with the real keys against the packages ANSMINDCORE's side
produced: a package sealed by a box that holds only the public half
opens here byte for byte, and one signed by the new release key verifies
against the built-in root. If those two products disagreed, a model that
runs in one would not run in the other, which is the failure this format
exists to avoid. 40 checks in the package test, no failures.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 23:05:00 +10:00
nghianguyenandClaude Opus 5 8e50120759 feat(ANSModelPackage): ask the model its input size when nothing else says
A package now carries what its model expects on the input, but every
package written before that does not — and neither does the zip, which
is most of what is in the field. The engine was then left to assume
640x640, which is wrong for a classification export at 224 and for
anything trained at a rectangular size.

The model itself always knows. ANSOnnxShape reads the first input's
shape straight out of the ONNX protobuf, from memory or from a file,
walking the top level and SEEKING past the nodes and the initializers
rather than reading them — the fields it wants sit after all of the
weight on the wire, and a 324 MB model answers in ten milliseconds.

OpenModelPackage uses it only when the manifest is silent, which costs
nothing there because the bytes are already decrypted in hand. An axis
the graph leaves open is reported as zero and a file that cannot be
parsed is a failure: both mean "this model does not say", and the caller
keeps the default it had. Being wrong about an input size would be worse
than being silent about it.

Checked against python-onnx on all 46 models on this machine, static and
dynamic, four-dimensional and not, with no disagreements. The test
writes ONNX graphs byte by byte, with node and initializer ahead of the
input as a real export has them, and packs one into a package whose
manifest says nothing — which is the case this exists for. 40 checks,
no failures.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 22:47:47 +10:00
nghianguyenandClaude Opus 5 080910c8ac feat(ODEngine): open a model package and run it without unpacking it
An engine's model has always been a path to a password-protected zip:
ANSODBase inflates it into a folder, and every engine then picks out
train_last.onnx, classes.names and model_config.json. A .ansmodel
package replaces all four with one sealed file, and this is the part
that turns it back into what the engines already understand.

ANSODBase::LoadModel and ::Initialize now recognise a .ansmodel path and
take a second route: the package is opened with the process keyring, the
model is decrypted into memory, and the classes, task and input geometry
come out of the manifest instead of loose files. Nothing is extracted,
so the unprotected model never touches the disk — which is the reason
the format exists, and it is one function's job to keep.

The engines follow it down. BasicOrtHandler gains a constructor that
takes the bytes rather than a path; it keeps a view of them, not a copy,
because the weights are in RAM either way once a session exists and a
copy would only double what every loaded model costs. The owner of the
plaintext is ANSODBase, which holds it for as long as the engine object
lives — that is what lets the DirectML recovery path rebuild a session
after a driver fault — and wipes it on teardown.

Wired through ANSONNXYOLO (and so the classification facade), ANSONNXOBB,
ANSONNXPOSE, ANSONNXSEG and YOLO12OD. YOLOOD is left alone: it loads
darknet weights and OpenCV DNN graphs, neither of which the writer
produces. A zip still loads exactly as before, on every engine.

Keys come from SetProcessModelKeys, as in ANSMINDCORE, with the same
development fallback from the environment. With none set, a package is
refused with a sentence saying so and every zip still works, so a build
made before the key ceremony runs everything except packages.

The engine layer needs CUDA to compile, so the contract it depends on is
checked where it can be: ANSModelPackage-UnitTest now opens the fixture
ANSMINDCORE wrote through the same glue ANSODBase uses, and covers the
refusal a keyless build gives. 32 checks, no failures.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 22:21:39 +10:00
nghianguyenandClaude Opus 5 6c164c3d0f feat(ANSModelPackage): read the ANS model package here too
The second reader. A package is one encrypted file — cleartext header,
sealed manifest, model bytes sealed in chunks — and this opens it: the
content key unwrapped with X25519 against the base key, the header
verified as Ed25519 signed by a release key the root vouches for, the
model decrypted into memory and handed to ANSModelSource, which the
engines already take.

It is a new library rather than code inside the engine layer, and
deliberately free of the heavy dependencies around it: no CUDA, no
OpenCV, no LabVIEW. That is what lets it build and be tested on every
platform, including the ones where the engine layer does not build.

THE TWO IMPLEMENTATIONS MUST AGREE, so this does not merely claim to:
tests/ANSModelPackage-UnitTest/data/interop.ansmodel was written by
ANSMINDCORE's packer, and the test opens it here — manifest, classes and
a chunked payload, byte for byte. A change to either wire format shows
up as this failing. The fixture's keys are derived by published rules,
recorded beside it, so anyone can rebuild it.

The AEAD and the small primitives are copies of ANSMINDCORE's, not
reimplementations, for the same reason. Monocypher 4.0.2 is vendored
here as it is there, including the RFC 8032 pair — its default EdDSA
signs with BLAKE2b and would not interoperate.

Verified by compiling and running the test directly: 19 checks, RFC
8439, RFC 7748 and RFC 8032 vectors, the interop fixture, a round trip,
a flipped bit, an unknown root, a revoked signer, and rubbish bytes.
ANSCORE's own CMake configure fails on this machine for reasons that
predate this change — eight errors with or without it, all missing
dependencies — so the new CMake targets still need a real build box.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 21:24:53 +10:00
nghianguyenandClaude Opus 5 5820a5e61b feat(ODEngine): load a model from memory, not only from a path
Every engine built its session from a file path, which is why opening an
encrypted model package means inflating it to a directory first: the
unprotected model lands on disk, is optimised, and is zipped back into a
cache. The new package format cannot avoid that from the outside — the
engines have to be able to take bytes.

ANSModelSource.h introduces a ModelSource (a path, or bytes with a name
for logs) and one MakeSession that builds from either. The six ONNX
engines — classification, OBB, pose, segmentation, YOLO12 and YOLO —
each gain a load overload taking it, with the existing string form
delegating so no caller changes. The Windows wide-path conversion and
the from-memory call now live in one place instead of the eleven copies
they were spread across; YOLOOD, which had only the Windows branch,
becomes portable as a side effect.

The factories are named rather than implicit, so a call with a plain
string literal still resolves to the path overload without ambiguity.

Not compiled here: this layer builds only where CUDA is present, so
verification on this machine was the new header compiled and run, plus a
structural check that every engine declares and defines both overloads,
calls MakeSession exactly once, and has no raw session construction
left. It needs a build on a CUDA box before it ships.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 20:29:59 +10:00
nghianguyenandClaude Fable 5 430c8057b8 ANSFR: report v1.4 emotion class names verbatim in the attribute JSON
The legacy display remap in FaceAttributeToJsonString rewrote most
emotions to "Attentive", which hid the v1.4 classifier's real classes.
The remap now applies ONLY to the legacy OpenVINO model's lowercase
classes (sad/happy/neutral/anger/surprise - v1.1 output unchanged);
every other name, i.e. the classes read from emotionsrecognition.names
(Angry, Fear, Happy, Neutral, Sad), passes through verbatim. A retrained
classifier with a different .names list needs no code change.

Verified on the classroom video: faces previously labelled "Attentive"
now read "Angry (100%)"; smiling faces read "Happy (100%)". Parity test
updated to check each pack against its own emotion vocabulary.

Note for client code: v1.4 deployments no longer emit "Attentive" -
anything matching that string must handle Angry/Fear/Sad instead.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-28 16:50:47 +10:00
nghianguyenandClaude Fable 5 024d55197b ANSFR: raise default slow-attribute refresh interval to 20 frames
With the heavier v1.4 ONNX age-gender model, interval 20 benchmarks at
throughput parity with the v1.1 pack (16.8 ms / 59 fps vs 16.0 ms / 62
fps on RTX 3070, classroom.mp4, ~3.1 faces/frame) versus 19.9 ms / 50
fps at the old default of 5. Attributes refresh every ~0.8 s on a 25 fps
camera (1.6 s on stable scenes, faster when faces appear), which is
ample for age/gender/emotion; head pose and recognition still run every
frame. Deployments wanting fresher attributes can lower it at runtime
via SetANSRFAttributeInterval. Full test suite passes at the new
default; GetANSRFModelInfo reports "attribute_interval":20.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-27 02:09:29 +10:00
nghianguyenandClaude Fable 5 f34817c620 ANSFR: configurable slow-attribute refresh interval
New API SetANSRFAttributeInterval(Handle, frames) / UpdateAttributeInterval
(1..60, default 5) controls how often the slow attributes (age/gender +
emotions) refresh per camera; head pose and recognition are unaffected.
GetANSRFModelInfo now reports "attribute_interval". The adaptive logic's
stable-scene branch previously capped at a hardcoded 10 - it now doubles
the configured interval instead (default behaviour 10/5/2 unchanged).

Motivation: the v1.4 ViT age-gender model is heavier per attribute frame
than the old OpenVINO IR. Interval sweep on RTX 3070 (classroom.mp4,
~3.1 faces/frame) vs the v1.1 baseline at 16.0 ms / 62 fps:
  interval  5 (default): 19.9 ms / 50 fps  (+3.9 ms)
  interval 10:           17.1 ms / 58 fps  (+1.1 ms)
  interval 20:           16.5 ms / 61 fps  (parity)
Recommendation: default on 8 GB GPUs; interval 10 on small-VRAM machines.

ANSFR-UnitTest: --bench old|new [interval] runs the sweep.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-27 01:57:54 +10:00
nghianguyenandClaude Fable 5 89b2efe182 ANSFR-UnitTest: point tests at the official SharedCore model packs
The re-packed ANS_FaceRecognizer_v1.4.zip (ONNX attribute models only, no
OpenVINO agegender/emotion IRs) is now the official pack and lives in the
canonical distribution folder C:\Projects\SharedCore next to v1.1.

Centralize every hardcoded pack path into three file-scope constants
(kFaceRecognizerV14Zip, kFaceRecognizerV11Zip, kFaceDetectorZip) used by
the model-switch test, classroom demo, benchmark, and parity test - a
future pack bump is a one-line edit. Verified against the official path:
adaface + ONNX attribute backends load and bench at 17.9 ms avg. The
slimmer pack was separately validated from a clean extraction (all
TestModelSwitchV14 checks pass without the OpenVINO fallback files).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-27 01:40:59 +10:00
nghianguyenandClaude Fable 5 13eacc4ab2 ANSFR: persist explicit recognizer-model choice across restarts
SetANSRFRecognizerModel now records the choice in the face database
(SETTINGS key RECOGNIZER_MODEL_PREF), and LoadEngine restores it when no
explicit choice was made in the current session - so a runtime switch to
ArcFace (or AdaFace) survives server restarts without re-calling the API.

Kept separate from EMBEDDING_MODEL on purpose: only an actual API call is
recorded as a preference. An automatic ArcFace fallback (pre-v1.4 zip
without adafacerecognizer.onnx) is not persisted, so upgrading such a
deployment to the v1.4 pack still gets the AdaFace default.

ANSFR-UnitTest: TestModelSwitchV14 (now also runnable via --test) gains a
simulated restart - release handle, recreate on the same database, and
assert the persisted ArcFace choice is restored and still recognizes the
enrolled user. Verified ALL PASSED.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-26 11:47:32 +10:00
nghianguyenandClaude Fable 5 06829ae0e3 ANSFR-UnitTest: output-format parity test between v1.1 and v1.4 packs
TestOutputFormatParity (--parity) runs the identical enroll + inference
flow through the v1.1 pack (arcface + OpenVINO attributes) and the v1.4
pack (adaface + ONNX attributes) and asserts the emitted JSON is
format-identical: same result-object fields, same attribute keys
(Gender/Age/AgeYears/Emotion/Pose), same value vocabularies (Male/Female,
the four Age buckets, Happy/Neutral/Attentive emotion labels with (NN%)),
and that both packs recognize the enrolled user. Verified ALL PASSED on
the RTX 3070 test machine.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-26 11:34:46 +10:00
nghianguyenandClaude Fable 5 103c053ebb ANSFR: emit numeric AgeYears in face attributes; clearer demo overlays
- FaceAttributeToJsonString now adds "AgeYears":"<n>" beside the existing
  "Age" bucket (Kids/Young Adults/Adults/Elders) - additive key, existing
  consumers parsing "Age" are unaffected.
- Classroom demo overlay: explicit per-line labels (Gender: / Age: /
  Emotion:) and the numeric age in parentheses, e.g. "Age: Adults (28)".

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-26 11:27:25 +10:00
nghianguyenandClaude Fable 5 775d4d6b16 ANSFR-UnitTest: classroom attribute-overlay demo + v1.1 vs v1.4 benchmark
- ClassroomAttributesVideoTest (default main): plays classroom.mp4 through
  the v1.4 pipeline and overlays user name, Gender/Age, and Emotion parsed
  from each result's extra_info JSON; saves an annotated frame every 30
  frames to C:\Programs\DemoAssets for headless review.
- --bench old|new|both: per-pack speed benchmark (100 measured frames after
  10 warmup, full pipeline with attributes on) run one pack per process.
  RTX 3070 laptop results: v1.1 arcface+OpenVINO 17.8 ms avg (56.2 fps) vs
  v1.4 adaface+ONNX/TRT 18.2 ms avg (55.1 fps) at 3.1 faces/frame — the
  new models carry no throughput cost.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-26 11:20:43 +10:00
nghianguyenandClaude Fable 5 ac4369302a ANSFR: support ANS_FaceRecognizer v1.4 models (AdaFace + ONNX attributes)
- Recognizer: adafacerecognizer.onnx (AdaFace, new default) with automatic
  fallback to ansfacerecognizer.onnx (ArcFace) for pre-v1.4 zips. AdaFace
  consumes BGR: new AdaFace handler in ONNXEngine skips the BGR2RGB swap,
  and the TensorRT paths gate their cvtColor on m_swapRB.
- New C API: SetANSRFRecognizerModel (0=AdaFace, 1=ArcFace),
  GetANSRFRecognizerModel, GetANSRFModelInfo. A live switch reloads the
  recognizer and regenerates all stored embeddings from the enrolled face
  crops; the generating model is persisted in a new SETTINGS table
  (EMBEDDING_MODEL) so upgrading an ArcFace database to the AdaFace
  default migrates automatically on LoadEngine.
- Attributes: prefer agegender.onnx (ViT: gender_logits [male,female] +
  age_pred in years; also handles the single-tensor research export) and
  emotionsrecognition.onnx (YOLOv11-cls, classes from .names) behind new
  IAgeGenderEstimator/IEmotionsClassifier interfaces, falling back to the
  legacy OpenVINO IRs. EP follows hardware: NVIDIA uses TensorRT EP with
  disk-cached engines, AMD DirectML, Intel OpenVINO.
- Emotion JSON mapping: handle the new Fear class and the never-matched
  surprise label (both map to Attentive).
- ANSFR-UnitTest: TestModelSwitchV14 covers default model selection, ONNX
  attribute backends, enrollment, and adaface<->arcface switching.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-26 11:06:10 +10:00
nghianguyenandClaude Opus 5 881d783fa3 ANSFR: re-enable CheckFaceEmbedding validation
Comments out the unconditional `return 1` added in b5a88b9, so the function
runs its real body again: the argument guards (null Handle / zero-length
buffer -> -1), cv::imdecode, and ANSFacialRecognition::CheckFace with the
detector confidence threshold, isValidFace's geometry gate and the
_minFaceSize filter.

The bypass line is left in place commented rather than deleted, so it stays
easy to flip back on.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-22 20:05:54 +10:00
nghianguyenandClaude Opus 5 c9ff9ee2f3 ANSFR-UnitTest: point the harness at the local ANSVIS deployment
Repoints the hardcoded fixtures at the installed ANSVIS Server layout rather
than the old E:/C: DemoAssets trees:

  - classroom.mp4 now read from C:\ProgramData\ANSCENTER\Shared
  - TestCompleteFR1 loads the GPU-specific ServerOptimised zips
    (ANS_FaceRecognizer_v1.1_NVIDIAGeForceRTX4070LaptopGPU.zip) and re-enables
    the InsertUser call ahead of InsertFace
  - main() runs ANSVISTest()

Also comments out the --gpu argument handling in main(), so the harness no
longer sets CUDA_VISIBLE_DEVICES and the single-GPU bisect flag is inert until
that block is restored.

Paths remain machine-specific; this is a developer harness, not a test suite.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-22 19:38:57 +10:00
nghianguyenandClaude Opus 5 b5a88b9335 ANSFR: bypass CheckFaceEmbedding validation for now
CheckFaceEmbedding returns 1 unconditionally, ahead of the JPEG decode and the
CheckFace() call, so every caller is told the submitted image is acceptable.

Deliberate temporary bypass, not a fix. What stops running behind it:

  - argument guards -- a null Handle or a zero-length buffer used to return -1
    and now returns 1
  - cv::imdecode, so an undecodable JPEG is no longer rejected
  - ANSFacialRecognition::CheckFace, and with it the detector confidence
    threshold, isValidFace's geometry gate (5 landmarks inside the box, roll
    <= 27 deg, yaw asymmetry <= 0.5, pitch 0.15-0.75, mouth/eye ratio 0.6-1.6)
    and the _minFaceSize filter

Enrollment callers that relied on this as a pre-check now accept any input.
Revert this commit to restore the checks.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-22 17:37:20 +10:00
nghianguyenandClaude Opus 5 b300cbf7a9 ANSLS: enumerate GPUs over DXGI so detection survives a missing OpenCL ICD
hwinfo::getAllGPUs() is an OpenCL device list -- the prebuilt HWiNFO.lib has no
Win32_VideoController path, only clGetPlatformIDs/clGetDeviceIDs/clGetDeviceInfo.
A machine whose driver never registered an OpenCL ICD therefore reported no GPU
at all, even with a healthy WDDM driver driving the display. Two consequences:
the system-info JSON emitted "GPU":"", and CheckHardwareInformation() silently
fell back to the CPU engine on a box holding a perfectly good CUDA card.

DXGI needs only a WDDM display driver, so it sees the adapter either way.

CheckHardwareInformation() now classifies adapters by PCI vendor id. Exact ids
also retire the substring matching and its "ATI" inside "CORPORATION" workaround.
It falls back to the OpenCL list when DXGI returns nothing.

GetSystemHardwareInformation() keeps its OpenCL loop untouched and merely appends
DXGI adapters that OpenCL did not already report, so the JSON stays byte-identical
-- same keys, order, indices and values -- on any machine that already worked.
Appended entries reuse OpenCL's vendor spelling (OpenClStyleVendor) and read the
driver version from the display-adapter class key, since DXGI exposes neither.

Verified on a dual-adapter box: the GPU node matches the pre-change output
exactly, and forcing the append path yields the same shape with values sourced
from DXGI plus the registry.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-22 16:16:38 +10:00
nghianguyenandClaude Opus 4.8 fa48516792 gitignore Claude Code per-user local settings
.claude/settings.local.json is machine-specific permission config; it
was tracked by accident and showed as perpetually modified on every
machine. Untracked (kept on disk) and ignored.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-21 11:57:24 +10:00
nghianguyenandClaude Opus 4.8 fda0f6f86f ANSFR: name the real cause when enrollment cannot compute embeddings
Enrolling a face while the recognizer engine is down used to store the
user row and silently drop the embedding: InsertUser is a pure DB write,
Feature() returns empty when uninitialized, and InsertFace's size check
misreported that as "Invalid embedding generated" — blaming the face.
The result surfaces hours later as every match returning Unknown/2.0
with nothing in the log saying why (seen on the A400: 45 users, zero
embeddings).

- ANSFRBase gains a public IsInitialized() accessor (flag existed but
  was protected).
- InsertFace / InsertMultipleFaces gate on it before computing the
  embedding and log the true cause: recognizer not initialized, face
  NOT enrolled, fix engine errors then re-enroll.
- The residual invalid-embedding message now reports actual vs expected
  size and the remaining causes (unusable crop / recognizer reloading).
- Reload() warns when the database has users but zero embeddings — the
  one line that identifies stale half-enrolled databases at startup.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-21 11:36:22 +10:00
nghianguyenandClaude Opus 4.8 799f755d5a ANSFR: purge stale ServerOptimised zip when engine rebuilds from ONNX
A ServerOptimised zip is a per-GPU cache of a previous engine build. When
the engine profile changes (batch gate, TRT/CUDA upgrade), the cached
engine inside it goes stale: every re-extract (e.g. after Temp cleanup)
restores the old engine and costs a full ONNX rebuild, forever, because
the server only regenerates the zip when the file is missing.

Engine now records rebuiltFromOnnx() + engineFilePath() in
buildLoadNetwork (set only on the generate-from-ONNX branch; cache hits
and alternative-precision loads don't raise it). After a rebuild,
ANSFaceRecognizer::Initialize deletes the stale same-GPU engine variants
from the model folder and the supplied optimised zip, so the server's
next optimize pass recreates it with the fresh engine.

Safety: only zips whose parent folder is ServerOptimised/-ized qualify —
base model zips can never match. Engine purge is scoped to the
"<model>.engine.<GPU>." prefix, leaving other GPUs' engines intact on
multi-GPU machines. All deletes are best-effort (error_code); multi-GPU
pool paths never set the flag, so the purge is a no-op there.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-21 11:21:48 +10:00
nghianguyenandClaude Opus 4.8 da9d922498 ANSFR: gate recognizer batch size by GPU VRAM
Batch-4 was tuned for 8 GB GPUs; its TRT workspace (~54 MiB per batch
unit) crowds out the other engines on smaller cards. VramCappedBatch
keeps the configured batch on 8 GB-class cards and up (threshold
7.5 GiB — 8 GB cards report ~8188 MiB) and drops to single-face batches
below that: identical results, multi-face frames just run sequentially,
~160 MiB freed on a 4 GB RTX A400. Also caps the legacy 8/32 LoadEngine
profile, whose .b32 engine (~1.7 GB workspace) would be fatal on small
cards. Initialize() and OptimizeModel() share the same call so their
profiles cannot drift.

Small cards rebuild the recognizer engine once (filename loses the .b4
suffix); 8 GB+ machines keep their cached engines unchanged.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-21 10:44:22 +10:00
nghianguyen 6939775960 Merge branch 'main' of ssh://anscenter.ddns.net:2222/anscenter/ANSCORE
# Conflicts:
#	core/anslicensing/hwid.cpp
2026-08-21 10:31:45 +10:00
nghianguyenandClaude Opus 5 86b520d85c ANSAWSS3: recover from dead sockets, and report why an upload failed
Field logs showed clusters of uploads taking exactly 20004/20007/20003 ms —
the default _idleTimeoutMs — across several workers at the same instant, then
recurring. Cause: a connection whose request failed was returned to the pool,
so a socket silently dropped by a middlebox was handed to worker after worker,
each paying the full idle timeout to rediscover it. Chilkat's autoReconnect
only covers a cleanly closed connection, not a half-open one.

Connection handling:
- DiscardConnection(): a TRANSPORT failure closes the connection instead of
  pooling it (23 sites). An HTTP error status is not a transport failure - the
  server answered - so those connections are still reused.
- Pooled connections idle longer than 15 s are dropped at acquire time.
- First request on a reused connection uses a short idle timeout (a quarter of
  the configured one, 5 s by default); a fresh connection keeps the full one.
  A dead socket now costs ~5 s once, not 20 s repeatedly.
- One upload call is capped at 30 s wall-clock. The attempt count alone was not
  a time bound: 20 attempts x 20 s could block the caller for ~400 s.
- RetryLoop no longer gives up after one failed connect. Callers call Connect()
  once at startup, so returning left the handle dead for the process lifetime.
  Now backs off 3 s -> 30 s until connected or stopped.
- Connections carry the config generation they were built against, so a connect
  completing just after SetAuthentication cleared the pool cannot leave an
  unauthenticated connection behind (those uploads would 403).
- Stale per-request state is cleared on acquire: multipart uploadId/partNumber
  query params, and the gzip Content-Encoding set by UploadTextData. Not
  ClearAllHeaders - that wipes SigV4 state (fcc985e).

Return codes - upload exports now say whether a retry is worthwhile:
-20 permanent (bad argument, licence, or HTTP 4xx), -21 not connected yet,
-22 transient. Recorded per thread, since one handle is shared by many workers.
Success stays the only positive value, so rc == 1 and rc != 1 are unchanged.
Handle and exception codes (-1/-3/-4) made consistent across all upload
wrappers; some previously returned 0 for these.

Also fixes a misclassification this surfaced: Chilkat reports failure for an
HTTP 4xx delivered during an Expect: 100-continue exchange, so NoSuchBucket was
treated as a network error and retried 20 times before reporting "transient".
The failure path now checks the response status. Chilkat's ~2 KB error dump per
failure is compressed to one line carrying the actual S3 error code.

Verified against live S3: 14,701 uploads across 15 threads with a 150 s idle
gap, 0 failures, no call over 5 s (avg 90 ms, p95 136 ms), matching the
pre-change baseline; and all six return-code classes confirmed end to end.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-12 21:24:12 +10:00
nghianguyenandClaude Fable 5 6f1d069d14 anslicensing: Linux hardware id uses PHYSICAL NICs only
The id is built from up to 6 enumerated MACs. On Linux getifaddrs() also
returns virtual interfaces — docker0 and the veth* pairs each container
creates, tun*/wg* from a VPN, br-*/virbr* bridges. Container veths carry
RANDOM MACs and appear/disappear with the containers, so the enumerated set
can differ between calls: the hardware id changes and an activated licence
silently stops matching its machine. Boxes running Docker (the Coder feature)
would hit this routinely.

This matters more on Linux than elsewhere because two of the four id
components are already constant there: the system-UUID slot is a literal and
GetHDDPropertyList is an empty function on non-Windows. The id rests on CPU id
+ MACs, so unstable MACs are most of the instability.

Filter mirrors what the Windows branch already does via WMI (PNPDeviceId LIKE
%PCI% — physical adapters only). The test is /sys/class/net/<name>/device,
the kernel's own record of a hardware-backed interface, so it stays correct as
new virtual-interface flavours appear; a name-prefix denylist is kept only for
environments without /sys. MACs are bucketed during the walk and chosen after
it, so a veth appearing earlier in the list cannot consume a slot and push a
real NIC out.

FAIL-SAFE: if no physical NIC is found, fall back to the unfiltered set. An
empty list would drop all MAC entropy and — with the UUID and disk slots
already constant — leave near-identical ids across different machines.

Windows and macOS/BSD paths are untouched (both #else branches preserve the
existing behaviour exactly). Logic verified against a simulated Docker host:
enp3s0/wlp2s0 accepted; lo, docker0, veth*, br-*, tun0 rejected.

ORDERING: this CHANGES the id on any Linux box that has virtual interfaces, so
it must land BEFORE the first Linux licences are issued — afterwards it would
invalidate them.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-28 19:34:26 +10:00
nghianguyenandClaude Fable 5 4f392ce95e anslicensing: fix 3-byte heap overflow in hardware-id BitStream3
GetCurrentHardwareId sized its bit stream at 125 bits (16 bytes) but
writes 147 (3 version + 9 x 16-bit hashes), so the last two hash slots
overflowed the heap block by 3 bytes on every id computation. The
Windows heap absorbed it silently for years; it SIGSEGVs under macOS
malloc — found when ANSGateway enabled this code on POSIX (fixed there
in ANSGateway 3a34c68/6c3cdfd; this is the upstream port).

Two parts, both id-preserving (only the first 16 bytes feed the base32
encode — verified byte-identical ids before/after in the gateway):
- hwid.cpp: size the stream to the 147 bits actually written.
- BitStream3: bounds-safe byteAt/setByte accessors on every Write/Read,
  Attach records the real buffer size, default ctor zero-inits — an
  under-sized stream can no longer corrupt the heap, and out-of-range
  reads return 0 (identical to the base32 padding zeros the Match path
  always read there).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-27 09:29:05 +10:00
nghianguyenandClaude Opus 4.8 60a8b39d24 licensing: HWID diagnosability, matcher widening, delete-condition fix
- hwid.cpp: BitStream3 buffer 125 -> 160 bits (writes totalled 147 bits
  and overflowed the old allocation; encoded HWID bytes unchanged).
  Log component availability (uuid/cpu/mac/hdd) once per distinct HWID,
  and warn when WMI yields no components at all.
- Matcher widened only, generation untouched: exact-string match
  short-circuits, UUID match accepts any WMI list entry (generator
  hashes the last valid one), fuzzy-match point breakdown is logged so
  field status-12 rejections are diagnosable.
- ANSLicense.cpp: fix inverted delete condition (!p != NULL) at 6 sites;
  detect the shared "empty fingerprint" HWID of WMI-broken PCs and warn
  with repair steps; mask activation keys in error strings.
- licensingclient.h: explain SetKey/validation/NTP failures on stderr.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-17 13:11:16 +10:00
nghianguyenandClaude Opus 4.8 d2479b9b07 GpuMemoryGuard: scale VRAM safety margin to card size
The margin was a fixed 768 MiB — 3% of a 24 GiB compute card but 19% of
a 4 GiB card (RTX A400), where free VRAM never clears it, so CanLoad()
refused every engine and UnderPressure() skipped every frame. Now
total/8 clamped to [192, 768] MiB: cards >= 6 GiB keep exactly the old
margin, only small cards change. ANSCENTER_GPU_MARGIN_MB / SetMargin()
still override (0 = auto). Refusal log now reports total VRAM alongside
free, which is what made this diagnosable.

Verified on the A400 test PC: detector + recognizer load and match.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-17 13:10:10 +10:00
nghianguyenandClaude Fable 5 ac79e51d17 ANSCV test: camera-dead watchdog with ReconnectRTSP (production parity)
Overnight soak revealed the gap: ANSRTSPClient does not auto-reconnect
by design (the application layer polls staleness and calls
ReconnectRTSP - ANSVIS/LabVIEW does this in production), but the unit
test never did, so when the laptop's Modern Standby killed the TCP
session at ~1 AM the client returned the cached frame until morning.

Poll GetRTSPLastFrameAgeMs each loop; at 20 s without a decoded frame,
call ReconnectRTSP (timestamped console line), retrying at most every
15 s. Threshold sits above the corruption keyframe gate's ~12 s
worst-case hold so a reconnect never preempts an in-flight recovery.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-08 10:42:31 +10:00
nghianguyenandClaude Fable 5 7909c8db2b RTP: resume at access-unit boundary after packet loss, not NAL start
After a sequence gap the H.264/H.265 depacketizers resumed reassembly at
the next NAL/FU start. On multi-slice streams every slice starts a NAL,
so reassembly resumed MID-FRAME and delivered access units missing their
leading slices — manufacturing "First slice in a frame missing" /
"Ignoring POC change between slices" / "Could not find ref" decoder
errors and green/blocky concealment output far beyond the actual loss.

Resume only at a true AU boundary instead:
  H.265 - non-VCL NAL (type >= 32) or VCL slice with
          first_slice_segment_in_pic_flag set (first bit after the
          2-byte NAL header)
  H.264 - non-VCL NAL (type >= 6) or VCL slice with
          first_mb_in_slice == 0 (ue(v): MSB set after 1-byte header)

Also: stderr visibility for RTP sequence gaps (rtp_rx logs went only to
the sys_log file) and for av_log-bridge latches in video_decoder — the
global av_log level filters decoder errors from the console, which made
keyframe-gate arms look causeless during diagnosis.

Live verification, HEVC camera on lossy Wi-Fi (NVDEC decode):
  before: 271 decoder errors / 3 min, delivery collapsed to ~2 fps
  after:  3 errors / 2 min, full ~21 fps, zero corrupt frames displayed
Remaining rare errors show POC ref misses with ZERO RTP sequence gaps,
i.e. pre-decode queue eviction under TCP burst clumps - a separate,
much smaller issue absorbed invisibly by the keyframe gate.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-07 06:09:37 +10:00
nghianguyenandClaude Fable 5 b341392e0f video_decoder: console visibility for keyframe gate arm/release
log_print writes to the sys_log file (unopened in unit tests / field
debugging), so the gate looked inert next to FFmpeg's stderr error lines.
Emit arm/release one-liners to stderr alongside them.

Live verification during a severe Wi-Fi loss burst (271 HEVC errors /
3 min, NVDEC path): gate armed 19x, released 17x, dropping 29-118
corrupt frames per burst and resuming on the next clean keyframe.
The av_log bridge (273dd44) is confirmed working end-to-end.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-07 05:40:34 +10:00
nghianguyenandClaude Fable 5 273dd44a6d video_decoder: av_log bridge — corruption detection for hwaccel decode
The corruption re-arm added in 6344062 keyed off AVFrame::decode_error_flags,
but the HEVC decoder on the hwaccel (NVDEC) path never sets it: 120 s of live
Wi-Fi packet loss produced 10+ "First slice in a frame missing" / "Could not
find ref with POC n" log bursts with decode_error_flags == 0 on every frame,
so the keyframe gate never fired.

Bridge: install a process-wide av_log callback (chains to
av_log_default_callback) that maps the logging AVCodecContext back to its
CVideoDecoder and latches a per-decoder m_bCorruptSeen flag on AV_LOG_ERROR
messages. readFrame() consumes the latch alongside decode_error_flags to
re-arm the keyframe gate. With hwaccel the decoder is single-threaded, so
the logging context is exactly our AVCodecContext; SW frame-threaded paths
log from per-thread clones but set decode_error_flags instead, which the
existing check covers.

Registered after avcodec_open2, unregistered in uninit() before the context
is freed. Map lookup only on error-level messages (rare). Also promote the
gate-arm message to log_print(HT_LOG_WARN) for console visibility.

Clean-path verified live: 6105 frames / 5 min NVDEC RTSP with zero errors
and no regressions. Loss-path verification pending a naturally lossy window.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-07 05:01:22 +10:00
nghianguyenandClaude Fable 5 6344062179 MediaClient: keyframe gating (startup + corruption re-arm), libyuv NV12 path
Restores the previously shelved decoder/player hardening and extends it:

1. Startup keyframe gating (video_decoder): after (re)start the decoder
   joins the stream mid-GOP; frames referencing unseen pictures render as
   green/blocky concealment garbage. Drop everything until the first IDR
   (300-frame safety valve so unmarked streams can't blank forever).

2. NEW - corruption re-arm: on lossy links (Wi-Fi cameras) dropped slices
   surface as "First slice in a frame missing" / "Could not find ref with
   POC n" and FFmpeg marks the output via AVFrame::decode_error_flags.
   Re-arm the keyframe gate and drop the rest of the GOP so the display
   holds the last clean frame instead of showing concealment artifacts;
   corrupt drops count toward the safety valve.

3. libyuv NV12->BGR conversion (video_player): honors NVDEC/D3D11VA padded
   strides (1080->1088 rows) that cv::cvtColorTwoPlane mishandled (green
   cast + striped noise), picks BT.601/709/2020 coefficients from the
   frame's signalled colorspace, and is ~3-5x faster than sws_scale.

Verified live against an RTSP HEVC camera (NVDEC path): steady frame flow,
no regressions on the clean-stream path.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 21:49:22 +10:00
nghianguyenandClaude Fable 5 044fedd344 ANSCV test: pump GUI messages while waiting for frames
The empty-frame path slept 1 s without calling cv::waitKey, so the
HighGUI window stopped pumping Windows messages and went "Not
Responding" whenever the RTSP stream stalled for a few seconds.
Wait via cv::waitKey(1000) instead (ESC still exits). Same fix in
all three test loops that shared the pattern.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 21:39:39 +10:00
nghianguyenandClaude Fable 5 005f3bc3d5 ANSCV tests: update RTSP camera address, drop unused ans_imshow include
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 20:23:31 +10:00
nghianguyenandClaude Fable 5 13180921c9 Pool dispatch: prefer slot on the GPU where inputs already reside
Two-pass slot acquisition in runInferenceFromPool: Pass A picks an idle
slot whose deviceIndex matches the input GpuMats' resident device
(zero-copy dispatch); Pass B falls back to plain round-robin, where
runInference migrates the inputs via the pinned-host two-hop copy.

Callers that prepare frames on GPU A no longer pay the ~1 ms/frame PCIe
migration when a same-GPU slot is idle, while spillover to the other GPU
under load still works. Input residency is queried outside the slot
mutex via cudaPointerGetAttributes (cheap driver lookup).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 20:05:00 +10:00
nghianguyenandClaude Fable 5 a4ea532f36 Fix multi-GPU inference on Blackwell (dual RTX 5080): four stacked fixes
Verified on dual RTX 5080: 355/355 detections, both GPUs in round-robin,
zero CUDA errors. Requires TensorRT >= 10.16.1 in SharedCore (10.15.1 has
a Myelin l2cm crash when creating a context on a second Blackwell device).

1. blobFromGpuMats: thread_local GpuMat caches (tl_blob/tl_floatImg) were
   device-blind — a pool thread warming GPU[1] then GPU[0] reused device-1
   memory with a device-0 stream, causing an async illegal memory access
   that poisoned every CUDA context (sticky 700). Track owning device and
   rebuild the cache on device switch.

2. runInference: pool round-robin can dispatch to a slot on a different
   GPU than the one the caller prepared inputs on. Detect foreign input
   GpuMats via cudaPointerGetAttributes and migrate them through a pinned
   host staging buffer (two-hop D2H+H2D; direct cross-device cudaMemcpy2D
   is unsupported and GeForce has no P2P).

3. warmUp: now pins its own CUDA device before uploading dummy inputs and
   returns success/failure; loadNetwork rejects engines whose warmup fails
   so broken slots can never join the pool silently.

4. Blackwell builder workaround: setMaxAuxStreams(0) on SM 12.x plus an
   .aux0 engine-cache suffix so pre-workaround caches rebuild.

Also: ANSFR-UnitTest gains --gpu N flag (sets CUDA_VISIBLE_DEVICES) for
single-GPU bisection, and rich diagnostics on input-copy failures.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 20:00:19 +10:00
nghianguyen 43ebd25d07 Unitiest 2026-06-23 05:56:06 +10:00
nghianguyenandClaude Opus 4.8 eb29945cb7 ANSCV tests: add ans_imshow include + headless ConsoleTest
Add ConsoleTest.cpp, a headless (no-GUI) smoke test for ANSOPENCV image
ops plus an optional RTSP capture path, built as its own executable
(ANSCV-ConsoleTest) so it does not collide with ANSCV-UnitTest's GUI main.

Make the ans_imshow.hpp include (shipped with ANSMIND, outside the repo)
configurable via the ANSMIND_INCLUDE_DIR cache variable, defaulting to
C:/ProgramData/ANSCENTER/ANSMIND. Warn rather than fail configure when the
header is absent so unrelated targets still build.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 05:51:01 +10:00
nghianguyen cb3a32cdfb Merge remote-tracking branch 'ANSCORE/main' 2026-06-18 05:55:50 +10:00
nghianguyen b3436a61ee Prevent OOM 2026-06-18 05:55:31 +10:00
nghianguyen bc1517598d Plans for CMS 2026-06-12 14:13:26 +10:00
nghianguyen ebba70b9d9 Support Get log events 2026-05-24 09:19:24 +10:00
nghianguyen 47b80102a8 Add port to scanning devices 2026-05-24 08:33:34 +10:00
nghianguyen a2fa2c8ceb Support scanning ANSVIS servers device 2026-05-23 18:03:22 +10:00
nghianguyen 4678d87e33 Add plan to port to linux 2026-05-23 17:29:57 +10:00
138 changed files with 18329 additions and 813 deletions
-238
View File
@@ -1,238 +0,0 @@
{
"permissions": {
"allow": [
"Bash(grep -v \"\\\\[EzMemoryManagerPlugin\\\\]\\\\|filesystem_win\\\\|at System\\\\|at NationalInstruments\\\\|Memory Leak Tracking\\\\|System\\\\.Argument\\\\|DNError\\\\|Parameter name\\\\|Inner Exception\\\\|^[0-9]*\t[0-9.]*\t[0-9]*\t$\" \"C:/Users/nghia/Downloads/ANSLEGION1.log\")",
"Bash(grep -v \"^$\")",
"Bash(grep -v \"^[^\t]*\t[^\t]*\t18924\t *$\")",
"Bash(dumpbin //EXPORTS \"C:/ProgramData/ANSCENTER/Shared/ANSLicensingSystem.dll\")",
"Bash(\"/c/Program Files \\(x86\\)/Windows Kits/10/bin/10.0.26100.0/x64/dumpbin.exe\" //EXPORTS \"C:/ProgramData/ANSCENTER/Shared/ANSLicensingSystem.dll\")",
"Bash(\"/c/Program Files \\(x86\\)/Windows Kits/10/bin/10.0.26100.0/x64/dumpbin.exe\" /EXPORTS \"C:/ProgramData/ANSCENTER/Shared/ANSLicensingSystem.dll\")",
"Bash(\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/dumpbin.exe\" /EXPORTS \"C:/ProgramData/ANSCENTER/Shared/ANSLicensingSystem.dll\")",
"Bash(\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/dumpbin.exe\" //EXPORTS \"C:/ProgramData/ANSCENTER/Shared/ANSLicensingSystem.dll\")",
"Read(//c/Program Files/ANSCENTER/**)",
"Bash(powershell.exe -NoProfile -Command \"[System.Environment]::GetEnvironmentVariable\\('PATH','Machine'\\) -split ';' | Select-String -Pattern 'ANSCENTER|Shared'\")",
"Bash(cmd.exe //c 'dir /AL \"C:\\\\Program Files\\\\ANSCENTER\\\\ANSVIS\\\\data\" 2>&1 | findstr /i \"junction symlink\"')",
"Bash(cmd.exe //c 'dir /AL \"C:\\\\Program Files\\\\ANSCENTER\\\\ANSVIS\\\\data\"')",
"PowerShell(Get-ChildItem \"C:\\\\Program Files\\\\ANSCENTER\\\\ANSVIS\\\\data\" -Force | Where-Object { $_.LinkType } | Select-Object Name, LinkType, Target | Format-Table -AutoSize)",
"Bash(awk '{print \"start: \"$2\"s\"}')",
"Bash(awk '{print \"end: \"$2\"s\"}')",
"Bash(awk '{ *)",
"Bash(awk '{v[NR]=$1} END {asort\\(v\\); n=length\\(v\\); printf \"count=%d\\\\nmedian=%.1fms\\\\np90=%.1fms\\\\np95=%.1fms\\\\np99=%.1fms\\\\nmax=%.1fms\\\\n\", n, v[int\\(n*0.5\\)], v[int\\(n*0.9\\)], v[int\\(n*0.95\\)], v[int\\(n*0.99\\)], v[n]}')",
"Bash(awk '{v[NR]=$1} END {asort\\(v\\); n=length\\(v\\); printf \"slow_inf_count=%d \\(over %d total inferences = %.1f%%\\)\\\\nmedian=%.1fms max=%.1fms\\\\n\", n, 10456, 100.0*n/10456, v[int\\(n*0.5\\)], v[n]}')",
"Bash(awk '{v[NR]=$1} END {asort\\(v\\); n=length\\(v\\); if\\(n>0\\){printf \"slow_getImage_count=%d median=%.1fms max=%.1fms\\\\n\", n, v[int\\(n*0.5\\)], v[n]}}')",
"Bash(awk -F= '{print $2}')",
"Bash(git -C \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\" status --short engines/ONNXEngine/ONNXEngine.cpp)",
"Bash(git -C \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\" diff engines/ONNXEngine/ONNXEngine.cpp)",
"Bash(git -C \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\" status --short)",
"Bash(grep -E \"\\\\.\\(cpp|h\\)$\")",
"Bash(awk '{print \"MEDIA_Leak heartbeats: \"$1}')",
"Bash(awk '{print \"ANSMOT heartbeats: \"$1}')",
"Bash(awk '{print \"PROC_MEM samples: \"$1}')",
"Bash(awk 'NR==1 || NR%15==0 { *)",
"Bash(awk 'NR==1 || NR%10==0 || NR==78 {print}')",
"Bash(sort -t= -k2 -n)",
"Bash(awk ' *)",
"Bash(sort -k3 -t= -n)",
"Bash(awk '{print \"MEDIA_Leak: \"$1}')",
"Bash(awk '{print \"BYTETracker heartbeats: \"$1}')",
"Bash(awk '{print \"PROC_MEM: \"$1}')",
"Bash(sort -k2 -t= -n)",
"Bash(awk 'NR==1 || NR%30==0 || NR==571 { *)",
"Bash(awk 'NR==2 || NR==80 || NR==160 || NR==240 || NR==285 {print}')",
"Bash(grep -n \"^\\\\s*return\\\\s\\\\+\\\\\\(true\\\\|false\\\\\\);$\\\\|^}\\\\s*$\" C:/Projects/CLionProjects/ANSCORE/engines/TensorRTAPI/include/engine/EngineRunInference.inl)",
"Bash(awk '$1 > 267 && $1 < 1100')",
"Bash(awk 'NR>267 && /^}/ {print NR\": \"$0; c++; if\\(c>=3\\)exit}' C:/Projects/CLionProjects/ANSCORE/engines/TensorRTAPI/include/engine/EngineRunInference.inl)",
"Bash(awk '{print \"TRT_Leak: \"$1}')",
"Bash(awk '{print \"Pool_Leak: \"$1}')",
"Bash(awk '{print \"BYTETracker: \"$1}')",
"Bash(awk 'NR==1 || NR%70==0 || NR==889 { *)",
"Read(//c/Users/nghia/Downloads/**)",
"Bash(awk '{print \"OCR_Leak: \"$1}')",
"Bash(awk 'NR==1 || NR%20==0 || NR==157 { *)",
"Bash(xargs file *)",
"Bash(xargs grep *)",
"Bash(awk '{print \"BYTETracker heartbeat: \"$1}')",
"Bash(awk 'NR==1 || NR%30==0 || NR==352 { *)",
"Bash(awk '{for\\(i=1;i<=NF;i++\\){if\\($i~/total=/\\)print substr\\($i, index\\($i,\"=\"\\)+1\\)}}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{p50=int\\(c*0.50\\); p95=int\\(c*0.95\\); p99=int\\(c*0.99\\); print \"YOLO_Timing total ms: n=\"c, \"p50=\"a[p50], \"p95=\"a[p95], \"p99=\"a[p99], \"max=\"a[c-1]}')",
"Bash(awk '{for\\(i=1;i<=NF;i++\\){if\\($i~/preproc=/\\)print substr\\($i, index\\($i,\"=\"\\)+1\\)}}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{p50=int\\(c*0.50\\); p95=int\\(c*0.95\\); p99=int\\(c*0.99\\); print \"YOLO_Timing preproc ms: n=\"c, \"p50=\"a[p50], \"p95=\"a[p95], \"p99=\"a[p99], \"max=\"a[c-1]}')",
"Bash(awk '{for\\(i=1;i<=NF;i++\\){if\\($i~/inf=/\\)print substr\\($i, index\\($i,\"=\"\\)+1\\)}}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{p50=int\\(c*0.50\\); p95=int\\(c*0.95\\); p99=int\\(c*0.99\\); print \"YOLO_Timing inference ms: n=\"c, \"p50=\"a[p50], \"p95=\"a[p95], \"p99=\"a[p99], \"max=\"a[c-1]}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{p50=int\\(c*0.50\\); p95=int\\(c*0.95\\); p99=int\\(c*0.99\\); print \"MEDIA_Timing getImage total ms: n=\"c, \"p50=\"a[p50], \"p95=\"a[p95], \"p99=\"a[p99], \"max=\"a[c-1]}')",
"Bash(awk '{for\\(i=1;i<=NF;i++\\){if\\($i~/pull=/\\)print substr\\($i, index\\($i,\"=\"\\)+1\\)}}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{p50=int\\(c*0.50\\); p95=int\\(c*0.95\\); p99=int\\(c*0.99\\); print \"MEDIA_Timing pull ms: n=\"c, \"p50=\"a[p50], \"p95=\"a[p95], \"p99=\"a[p99], \"max=\"a[c-1]}')",
"Bash(awk '{for\\(i=1;i<=NF;i++\\){if\\($i~/convert=/\\)print substr\\($i, index\\($i,\"=\"\\)+1\\)}}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{p50=int\\(c*0.50\\); p95=int\\(c*0.95\\); p99=int\\(c*0.99\\); print \"MEDIA_Timing convert ms: n=\"c, \"p50=\"a[p50], \"p95=\"a[p95], \"p99=\"a[p99], \"max=\"a[c-1]}')",
"Bash(awk '{print $2}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{p50=int\\(c*0.50\\); p95=int\\(c*0.95\\); p99=int\\(c*0.99\\); print \"TRT_Slow timestamps \\(sec\\): n=\"c, \"p50=\"a[p50], \"p95=\"a[p95], \"p99=\"a[p99], \"max=\"a[c-1]}')",
"Bash(awk -F'total=' '{print $2}')",
"Bash(awk '{print $1}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{p50=int\\(c*0.50\\); p95=int\\(c*0.95\\); p99=int\\(c*0.99\\); print \"TRT_Slow total ms: n=\"c, \"p50=\"a[p50], \"p95=\"a[p95], \"p99=\"a[p99], \"max=\"a[c-1]}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{p50=int\\(c*0.50\\); p95=int\\(c*0.95\\); p99=int\\(c*0.99\\); print \"SLOW memcpy timestamps \\(sec\\): n=\"c, \"p50=\"a[p50], \"p95=\"a[p95], \"p99=\"a[p99], \"max=\"a[c-1]}')",
"Bash(awk -F. '{print int\\($1/100\\)}')",
"Bash(awk -F' ' '{print $3}')",
"Bash(awk '$1 > 200')",
"Bash(awk -F'net=' '{print $2}')",
"Bash(awk -F'bytesMB=' '{print $2}')",
"Bash(awk -F'bytesMB=' '{gsub\\(/[^0-9.]/,\"\",$2\\); print $2}')",
"Bash(awk -F'ids_tot=' '{print $2}')",
"Bash(dumpbin /symbols \"C:/Projects/CLionProjects/ANSCORE/cmake-build-release/bin/ANSCV.dll\")",
"Bash(mkdir -p \"C:/ProgramData/ANSCENTER\")",
"Bash(touch \"C:/ProgramData/ANSCENTER/ansvisdebugview.txt\")",
"Read(//c/ProgramData/ANSCENTER/**)",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{printf \"YOLO_total: n=%d p50=%.1f p95=%.1f p99=%.1f max=%.1f\\\\n\", c, a[int\\(c*0.5\\)], a[int\\(c*0.95\\)], a[int\\(c*0.99\\)], a[c-1]}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{printf \"YOLO_preproc: n=%d p50=%.1f p95=%.1f p99=%.1f max=%.1f\\\\n\", c, a[int\\(c*0.5\\)], a[int\\(c*0.95\\)], a[int\\(c*0.99\\)], a[c-1]}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{printf \"YOLO_inf: n=%d p50=%.1f p95=%.1f p99=%.1f max=%.1f\\\\n\", c, a[int\\(c*0.5\\)], a[int\\(c*0.95\\)], a[int\\(c*0.99\\)], a[c-1]}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{printf \"MEDIA_total: n=%d p50=%.2f p95=%.2f p99=%.2f max=%.2f\\\\n\", c, a[int\\(c*0.5\\)], a[int\\(c*0.95\\)], a[int\\(c*0.99\\)], a[c-1]}')",
"Bash(awk 'BEGIN{c=0}{a[c++]=$1}END{printf \"MEDIA_convert: n=%d p50=%.2f p95=%.2f p99=%.2f max=%.2f\\\\n\", c, a[int\\(c*0.5\\)], a[int\\(c*0.95\\)], a[int\\(c*0.99\\)], a[c-1]}')",
"Bash(python -c ' *)",
"Bash(python)",
"Bash(git config *)",
"Bash(git submodule *)",
"Bash(git rm *)",
"Bash(rm -rf .git/modules/3rdparty/libyuv)",
"Bash(git add *)",
"Read(//c/ProgramData/Jh7O7nUe7vS/Models/EngineModels/B-IN_ANS_VehicleDetection_v2.0_67345015/**)",
"Bash(xxd)",
"Bash(icacls \"C:\\\\ProgramData\\\\Jh7O7nUe7vS\\\\Models\\\\EngineModels\\\\B-IN_ANS_VehicleDetection_v2.0_67345015\\\\train_last.onnx\")",
"Bash(grep -oE '<Data[^>]*>[^<]{0,400}</Data>' \"C:/Users/nghia/Downloads/error.xml\")",
"Bash(grep -oE '<Data[^>]*>[^<]{0,500}</Data>' \"/c/Users/nghia/Downloads/error.xml\")",
"Read(//tmp/**)",
"Bash(grep -oE '<Data[^>]*>[^<]{0,400}</Data>' \"/c/Users/nghia/Downloads/error.xml\")",
"Bash(echo \"Exit: $?\")",
"Bash(python -)",
"Bash(git show *)",
"Bash(dumpbin //dependents \"C:\\\\\\\\ProgramData\\\\\\\\ANSCENTER\\\\\\\\Shared\\\\\\\\ANSLicensingSystem.dll\")",
"Bash(grep -v \"//\")",
"Bash(grep -v \"^ *//\")",
"Bash(python strip_folder_lock.py)",
"Bash(tasklist //FI \"IMAGENAME eq ANSLPR-UnitTest.exe\")",
"Bash(taskkill //PID 41856 //F)",
"PowerShell(Stop-Process -Id 41856 -Force)",
"PowerShell(Start-Process -FilePath \"taskkill.exe\" -ArgumentList \"/PID\",\"41856\",\"/F\" -Verb RunAs -Wait)",
"Bash(npm root *)",
"Bash(npm list *)",
"Bash(npm install *)",
"Bash(NODE_PATH=\"C:/home/alex/.npm-global/node_modules\" node build_anslib_logging_guide.js)",
"Bash(python \"C:/Users/nghia/AppData/Roaming/Claude/local-agent-mode-sessions/skills-plugin/d8e35aa4-a14e-4e20-b921-ba1b9a3cce86/cdda7cc8-a1c7-42ff-98b4-473ec3e8b9fb/skills/docx/scripts/office/validate.py\" \"C:/Projects/CLionProjects/ANSCORE/docs/ANSLIB_Logging_Guide.docx\")",
"Bash(python \"C:/Users/nghia/AppData/Roaming/Claude/local-agent-mode-sessions/skills-plugin/d8e35aa4-a14e-4e20-b921-ba1b9a3cce86/cdda7cc8-a1c7-42ff-98b4-473ec3e8b9fb/skills/docx/scripts/office/soffice.py\" --headless --convert-to pdf --outdir \"C:/Projects/CLionProjects/ANSCORE/docs\" \"C:/Projects/CLionProjects/ANSCORE/docs/ANSLIB_Logging_Guide.docx\")",
"Bash(sort -t: -k1 -u)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE diff core/ANSLicensingSystem/ANSLicense.cpp)",
"Bash(awk 'NR>=154022 && NR<=156428 {print}' \"C:/Users/nghia/Downloads/AVNET-8845HS2.log\")",
"Bash(awk 'NR>=156350 && NR<=156428 {print}' \"C:/Users/nghia/Downloads/AVNET-8845HS2.log\")",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE log --oneline -10 -- modules/ANSFR/OpenVINODeviceConfig.h modules/ANSFR/OpenVINODeviceConfig.cpp)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE log --oneline -5 -- core/ANSLibsLoader/EPLoader.cpp)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE show --stat 69787b0)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE show --stat 97d8149)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE show 97d8149 -- engines/ONNXEngine/OpenVINODeviceConfig.h modules/ANSFR/ANSFR.cpp)",
"Bash(awk 'NR>=7106 && NR<=7250 && /ONNXYOLO|ANSCUSTOM|DetectObjects/ {print}' \"C:/Users/nghia/Downloads/AVNET-8845HS5.log\")",
"Bash(awk 'NR>=5850 && NR<=7115 && /ONNXYOLO|ANSCUSTOM_Infer|ANSONNXCL_pp|cls\\\\] calling/' \"C:/Users/nghia/Downloads/AVNET-8845HS5.log\")",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE show --stat 3a527d2)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE show 3a527d2 -- modules/ANSODEngine/ANSCUSTOMDETECTOR.cpp modules/ANSODEngine/ANSONNXYOLO.cpp modules/ANSODEngine/ANSONNXCL.cpp)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE show 3a527d2 -- modules/ANSODEngine/ANSEngineCommon.h)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE diff HEAD modules/ANSODEngine/ANSONNXYOLO.cpp modules/ANSODEngine/ANSONNXCL.cpp)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE diff HEAD modules/ANSODEngine/ANSONNXYOLO.cpp)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE diff HEAD modules/ANSODEngine/ANSONNXCL.cpp)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE log --all --oneline --since='3 weeks ago' -- modules/ANSODEngine/ANSONNXYOLO.cpp modules/ANSODEngine/ANSONNXCL.cpp engines/ONNXEngine/ONNXEngine.cpp engines/ONNXEngine/ONNXEngine.h)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE show --stat 844d739)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE log -p --grep=\"mutex\\\\|lock\\\\|hang\\\\|stall\\\\|concurrent\\\\|thread\" --since=\"3 weeks ago\" -- modules/ANSODEngine/ANSONNXYOLO.cpp)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE show 844d739 -- modules/ANSODEngine/ANSONNXYOLO.cpp)",
"Bash(awk '$2>=835 && $2<=860 && /1853990010|ANSCUSTOM|ANSONNXCL|ANSONNXYOLO/' \"C:/Users/nghia/Downloads/AVNET-8845HS7.log\")",
"Bash(awk '$2>=836 && $2<=837 && /ANSCUSTOM|ANSONNXCL/' \"C:/Users/nghia/Downloads/AVNET-8845HS7.log\")",
"Bash(awk '$2>=836.0 && $2<=836.9' \"C:/Users/nghia/Downloads/AVNET-8845HS7.log\")",
"Bash(grep -nB1 -A3 \"DirectML\\\\|DisableMemPattern\\\\|SetExecutionMode\\\\|ExtendedOptimization\\\\|ORT_SEQUENTIAL\" \"C:/Projects/CLionProjects/ANSCORE/engines/ONNXEngine/ONNXEngine.cpp\")",
"Bash(grep -nE \"^\\\\}$|warmupModel\\\\\\(|#if 0|#endif|ANSONNXCL_legacy_Init\" C:/Projects/CLionProjects/ANSCORE/modules/ANSODEngine/ANSONNXCL.cpp)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE restore modules/ANSODEngine/ANSONNXCL.h modules/ANSODEngine/ANSONNXCL.cpp)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE status --short modules/)",
"Bash(git -C C:/Projects/CLionProjects/ANSCORE diff --stat HEAD modules/ANSODEngine/ANSONNXCL.cpp modules/ANSODEngine/ANSONNXOBB.cpp modules/ANSODEngine/ANSONNXPOSE.cpp modules/ANSODEngine/ANSONNXSEG.cpp modules/ANSODEngine/ANSYOLO12OD.cpp engines/ONNXEngine/ONNXEngine.cpp engines/ONNXEngine/ONNXSAM3.cpp)",
"Bash(awk -F'[:\\(\\)]' '{gsub\\(/^ +| +$/,\"\",$2\\); split\\($2,a,\" \"\\); ms=a[1]; if \\(ms+0 >= 500\\) print $0}')",
"Bash(awk -F'[:\\(\\)]' '{split\\($2,a,\" \"\\); ms=a[1]; if \\(ms+0 >= 100\\) print $0}')",
"Bash(grep -cE \"^\\\\s*Plate: [^ ]+$\" \"C:/Users/nghia/Downloads/Log4.txt\")",
"Bash(grep -oE \"Plate: [^ ]+$\" \"C:/Users/nghia/Downloads/Log5.txt\")",
"Bash(awk -F'\\\\t' '{print $3}')",
"Bash(awk -F'\\\\t' '{print $3}' \"C:/Users/nghia/Downloads/AVNET-8845HS_twoconsoleapps.log\")",
"Bash(awk -F'\\\\t' '{print $3}' \"C:/Users/nghia/Downloads/AVNET-8845HS_issue.log\")",
"Bash(awk -F'\\\\t' '$2+0 >= 49 && $2+0 <= 52 {print NR\": \"$0}' \"C:/Users/nghia/Downloads/AVNET-8845HS_issue.log\")",
"Bash(awk -F'\\\\t' 'BEGIN{prev=0} {t=$2+0; if\\(t-prev>5\\){print NR\": GAP=\"t-prev\"s \"$0} prev=t}' \"C:/Users/nghia/Downloads/AVNET-8845HS3.log\")",
"Bash(awk -F'\\\\t' '{print $3}' \"C:/Users/nghia/Downloads/AVNET-8845HS3.log\")",
"Bash(awk -F'\\\\t' '{print $3}' \"C:/Users/nghia/Downloads/AVNET-8845HS4_5.log\")",
"Bash(awk -F'\\\\t' '$2+0>=20 && $2+0<=22' \"C:/Users/nghia/Downloads/AVNET-8845HS4_5.log\")",
"Bash(awk -F'\\\\t' '$2+0>=20.5 && $2+0<=21.5' \"C:/Users/nghia/Downloads/AVNET-8845HS4_5.log\")",
"Bash(awk -F'\\\\t' '$2+0>=21.0 && $2+0<=21.7' \"C:/Users/nghia/Downloads/AVNET-8845HS4_5.log\")",
"Bash(awk -F'\\\\t' '$2+0>=21.05 && $2+0<=21.23' \"C:/Users/nghia/Downloads/AVNET-8845HS4_5.log\")",
"Bash(awk -F'\\\\t' '$2+0>=17 && $2+0<=20' \"C:/Users/nghia/Downloads/AVNET-8845HS6.log\")",
"Bash(awk -F'\\\\t' '$2+0>=17.4 && $2+0<=17.7' \"C:/Users/nghia/Downloads/AVNET-8845HS6.log\")",
"Bash(awk -F'\\\\t' '$2+0>=12 && $2+0<=18' \"C:/Users/nghia/Downloads/AVNET-8845HS6.log\")",
"Bash(git -C \"C:/Projects/CLionProjects/ANSCORE\" status)",
"Bash(git -C \"C:/Projects/CLionProjects/ANSCORE\" diff --stat modules/ANSODEngine/ANSONNXCL.h modules/ANSODEngine/ANSONNXCL.cpp modules/ANSODEngine/ANSONNXYOLO.cpp)",
"Bash(awk -F'\\\\t' '{print $4}' \"/c/Users/nghia/Downloads/AVNET-8845HS7.log\")",
"Bash(\"C:/Users/nghia/AppData/Local/Programs/CLion 2026.1/bin/cmake/win/x64/bin/cmake.exe\" --build C:/Projects/CLionProjects/ANSCORE/cmake-build-release --target ANSLPR-UnitTest -j 30)",
"Bash(cmd.exe /c '\"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvars64.bat\" >NUL && \"C:\\\\Users\\\\nghia\\\\AppData\\\\Local\\\\Programs\\\\CLion 2026.1\\\\bin\\\\cmake\\\\win\\\\x64\\\\bin\\\\cmake.exe\" --build C:/Projects/CLionProjects/ANSCORE/cmake-build-release --target ANSLPR-UnitTest -j 30')",
"Bash(cmd.exe //c 'call \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvars64.bat\" >NUL && \"C:\\\\Users\\\\nghia\\\\AppData\\\\Local\\\\Programs\\\\CLion 2026.1\\\\bin\\\\cmake\\\\win\\\\x64\\\\bin\\\\cmake.exe\" --build \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\" --target ANSLPR-UnitTest -j 30')",
"PowerShell($env:VS_VARS = '\"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvars64.bat\"'; $cmd = \"call $env:VS_VARS && `\"C:\\\\Users\\\\nghia\\\\AppData\\\\Local\\\\Programs\\\\CLion 2026.1\\\\bin\\\\cmake\\\\win\\\\x64\\\\bin\\\\cmake.exe`\" --build `\"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release`\" --target ANSLPR-UnitTest -j 30\"; cmd.exe /c $cmd 2>&1 | Select-Object -Last 60)",
"PowerShell(cmd.exe /c $cmd 2>&1)",
"PowerShell($cmd = 'call \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvars64.bat\" >NUL && \"C:\\\\Users\\\\nghia\\\\AppData\\\\Local\\\\Programs\\\\CLion 2026.1\\\\bin\\\\cmake\\\\win\\\\x64\\\\bin\\\\cmake.exe\" --build \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\" --target ANSLPR-UnitTest -j 30')",
"Bash(command -v dumpbin)",
"Bash(dumpbin /SYMBOLS \"/c/Projects/CLionProjects/ANSCORE/cmake-build-release/bin/ANSOCR.dll\")",
"Bash(\"/c/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.44.35207/bin/Hostx64/x64/dumpbin.exe\" //SYMBOLS \"/c/Projects/CLionProjects/ANSCORE/cmake-build-release/bin/ANSOCR.dll\")",
"Bash(cmake --build . --target ANSODEngine -j 8)",
"PowerShell(@\\(Get-ChildItem -Path \"C:\\\\Program Files\\\\Microsoft Visual Studio\" -Filter \"vcvars64.bat\" -Recurse -ErrorAction SilentlyContinue\\) + @\\(Get-ChildItem -Path \"C:\\\\Program Files \\(x86\\)\\\\Microsoft Visual Studio\" -Filter \"vcvars64.bat\" -Recurse -ErrorAction SilentlyContinue\\) | ForEach-Object { $_.FullName })",
"PowerShell($d = Get-Item C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\\\\bin\\\\ANSODEngine.dll; \"$\\($d.Name\\) | $\\($d.Length\\) bytes | $\\($d.LastWriteTime\\)\")",
"Bash(awk '{if \\($0 ~ /=0+$/\\) print \"ZERO\"; else print \"NONZERO\"}')",
"Bash(awk '{print $4}' \"C:\\\\Users\\\\nghia\\\\Downloads\\\\ANSAIBOX-Q1A01S3_memoryleak1.log\")",
"Bash(awk -F'\\\\t' 'NF>=4 && $3 ~ /^[0-9]+$/ {print $3}' \"C:\\\\Users\\\\nghia\\\\Downloads\\\\ANSAIBOX-Q1A01S3_memoryleak1.log\")",
"PowerShell($dll = \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\bin\\\\anslicensing.dll\"; if \\(Test-Path $dll\\) { Get-Process | Where-Object { try { $_.Modules | Where-Object { $_.FileName -eq $dll } } catch {} } | Select-Object Id, ProcessName } else { \"DLL not present at $dll\" })",
"PowerShell($p = \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\\\\bin\\\\anslicensing.dll\"; try { $fs = [System.IO.File]::Open\\($p, 'Open', 'ReadWrite', 'None'\\); $fs.Close\\(\\); \"OK: writable, no other handle\" } catch { \"LOCKED: $\\($_.Exception.Message\\)\" })",
"PowerShell($candidates = @\\(\"handle64.exe\",\"handle.exe\"\\); $found = $null; foreach \\($c in $candidates\\) { $p = Get-Command $c -ErrorAction SilentlyContinue; if \\($p\\) { $found = $p.Source; break } }; if \\($found\\) { Write-Output \"FOUND: $found\"; & $found -nobanner -accepteula \"anslicensing.dll\" 2>$null } else { Write-Output \"handle.exe not in PATH\" })",
"PowerShell(Get-CimInstance Win32_Process -Filter \"ProcessId=22688\")",
"PowerShell(Get-CimInstance Win32_Process -Filter \"ParentProcessId=22688\")",
"Bash(taskkill //F //PID 50352)",
"Bash(awk -F'\\\\t' '/\\\\[TRT_Leak\\\\]/' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk -F'\\\\t' '/\\\\[MEDIA_Leak\\\\]/' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk -F'\\\\t' '/\\\\[MEDIA_Leak\\\\]/ {print $2, $4}' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk 'NR==1 || NR%50==0 || NR>510')",
"Bash(awk -F'\\\\t' '/\\\\[TRT_Leak\\\\]/ {if \\($4 !~ /creates=0 destroys=0 net=0 cached=0/\\) print NR\": \"$0}' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk '-F\\\\t' ' *)",
"Bash(awk -F'\\\\t' '/\\\\[TRT_Build\\\\]/' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk -F'\\\\t' '/VRAM/ {print $2, $4}' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk 'NR==1 || NR%2000==0')",
"Bash(awk -F'\\\\t' '/\\\\[ALPR_Track\\\\]|\\\\[ALPR_TrackId\\\\]/' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk -F'\\\\t' '/\\\\[ANSCUSTOM_Infer\\\\]/' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(sort -t= -k2 -n -r)",
"Bash(awk -F'\\\\t' '/\\\\[ALPR_TrackId\\\\]/' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk -F'\\\\t' '/\\\\[ANSMOT\\\\]/' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk '-F\\\\t' '/\\\\[ANSMOT\\\\] BYTETracker state/ { *)",
"Bash(awk -F'\\\\t' '/setInputShape/' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk -F'\\\\t' '/CUDA graph CAPTURE starting/' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk '-F\\\\t' '/setInputShape/{ *)",
"Bash(python -m pip install reportlab --quiet)",
"Bash(python build_anscore_devguide_pdf.py)",
"Bash(python -c \"from pypdf import PdfReader; r = PdfReader\\(r'C:/Projects/CLionProjects/ANSCORE/ANSODEngine_Tracker_Stabilization_Guide.pdf'\\); print\\('pages:', len\\(r.pages\\)\\)\")",
"Bash(python -c \"import reportlab.pdfbase.pdfmetrics; print\\('pages: \\(pypdf not available — file size confirms\\)'\\)\")",
"Bash(python -m pip install pypdf --quiet)",
"Bash(python -c \"from pypdf import PdfReader; r = PdfReader\\(r'C:/Projects/CLionProjects/ANSCORE/ANSODEngine_Tracker_Stabilization_Guide.pdf'\\); print\\('pages:', len\\(r.pages\\)\\); print\\('first page text \\(head\\):', r.pages[0].extract_text\\(\\)[:300].replace\\(chr\\(10\\), ' '\\)\\)\")",
"Bash(awk -F'\\\\t' '{print NR}' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(awk -F'\\\\t' 'END {print \"lines:\", NR, \" duration_s:\", $2, \" rate_lines/s:\", NR/$2}' \"C:/Users/nghia/Downloads/ANSLEGION.log1.log\")",
"Bash(grep -nE \"RunInference|RunInferencesBatch|infer\\\\\\\\\\(\\\\\\\\\\)|set_input_tensor|inference_request|m_inferRequest|mutex|lock_guard\" C:/Projects/CLionProjects/ANSCORE/engines/OpenVINOEngine/src/**/*.cpp C:/Projects/CLionProjects/ANSCORE/engines/OpenVINOEngine/include/**/*.h)",
"Bash(grep -nE \"DetectObjects|TryLockWithTimeout|ortRun|->Run\\\\\\\\\\(|m_ortEngine|infer|RunInference\" C:/Projects/CLionProjects/ANSCORE/modules/ANSODEngine/ANSONNXYOLO.cpp)",
"Bash(grep -nE \"ONNXYOLO::detect|ort_session->Run|set_input|detect\\\\\\\\\\(.*const.*Mat|m_inputBuffer|m_input_tensor|allocator|ortInput|run_options|preprocess|postprocess|class ONNXYOLO\" C:/Projects/CLionProjects/ANSCORE/modules/ANSODEngine/ANSONNXYOLO.cpp C:/Projects/CLionProjects/ANSCORE/modules/ANSODEngine/ANSONNXYOLO.h)",
"Bash(ninja ANSODEngine)",
"PowerShell($env:Path = \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Tools\\\\Llvm\\\\x64\\\\bin;\" + $env:Path; & \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvarsall.bat\" x64 2>&1 | Out-Null; Get-Command ninja -ErrorAction SilentlyContinue | Select-Object Source)",
"PowerShell($ninjaPaths = @\\(\"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\Common7\\\\IDE\\\\CommonExtensions\\\\Microsoft\\\\CMake\\\\Ninja\\\\ninja.exe\", \"C:\\\\ProgramData\\\\chocolatey\\\\bin\\\\ninja.exe\", \"C:\\\\tools\\\\ninja\\\\ninja.exe\"\\); foreach \\($p in $ninjaPaths\\) { if \\(Test-Path $p\\) { Write-Output $p } })",
"PowerShell(cmd /c '\"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvarsall.bat\" x64 >nul 2>&1 && cd /d C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release && \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\Common7\\\\IDE\\\\CommonExtensions\\\\Microsoft\\\\CMake\\\\Ninja\\\\ninja.exe\" ANSODEngine 2>&1')",
"Bash(python build_anslpr_devguide_pdf.py)",
"Bash(where dumpbin.exe)",
"Bash(where llvm-symbolizer.exe)",
"Bash(where cdb.exe)",
"Bash(\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/Llvm/x64/bin/llvm-symbolizer.exe\" --obj=\"C:/Projects/AIVISION/ANSFallDetection/ANSCustomFall/Dll1/x64/Release/ANSCustomFall.dll\" 0xb1d1)",
"Bash(cmake --build C:/Projects/CLionProjects/ANSCORE/cmake-build-release --target ANSLicensingSystem -- /m)",
"Bash(cmake --build C:/Projects/CLionProjects/ANSCORE/cmake-build-release --target ANSLicensingSystem)",
"Bash(cmd /c \"\\\\\"C:\\\\\\\\Program Files\\\\\\\\Microsoft Visual Studio\\\\\\\\2022\\\\\\\\Community\\\\\\\\VC\\\\\\\\Auxiliary\\\\\\\\Build\\\\\\\\vcvars64.bat\\\\\" >nul && cmake --build C:\\\\\\\\Projects\\\\\\\\CLionProjects\\\\\\\\ANSCORE\\\\\\\\cmake-build-release --target ANSLicensingSystem 2>&1\")",
"Bash(cmd /c \"call \\\\\"C:\\\\\\\\Program Files\\\\\\\\Microsoft Visual Studio\\\\\\\\2022\\\\\\\\Community\\\\\\\\VC\\\\\\\\Auxiliary\\\\\\\\Build\\\\\\\\vcvars64.bat\\\\\" && cmake --build C:\\\\\\\\Projects\\\\\\\\CLionProjects\\\\\\\\ANSCORE\\\\\\\\cmake-build-release --target ANSLicensingSystem\")",
"PowerShell($vcvars = \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvars64.bat\"; cmd /c \"`\"$vcvars`\" && cmake --build C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release --target ANSLicensingSystem 2>&1\" 2>&1 | Select-Object -Last 120)",
"Bash(grep -rn \"^using namespace std\" \"C:/ANSLibs/anslicensing/\" 2>/dev/null | head -5; grep -rn \"^using namespace std\" \"C:/Projects/CLionProjects/ANSCORE/core/anslicensing/\" 2>/dev/null | head -5)",
"PowerShell(Start-Process powershell -Verb RunAs -ArgumentList '-NoProfile','-Command',\"try { Get-WmiObject MSAcpi_ThermalZoneTemperature -Namespace root\\\\wmi | Select-Object InstanceName,CurrentTemperature | Out-File C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\\\\_smoke\\\\thermal.txt } catch { '$_' | Out-File C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\\\\_smoke\\\\thermal.txt }\" -Wait; Get-Content C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\\\\_smoke\\\\thermal.txt)"
]
}
}
+7 -1
View File
@@ -41,4 +41,10 @@ ipch/
# Windows
Thumbs.db
Desktop.ini
Desktop.ini
# Claude Code per-user local settings (machine-specific permissions)
.claude/settings.local.json
# The key ceremony writes this; it carries the base private key and must
# never be committed. See ANSMINDCORE docs/model-package-keys.md.
core/ANSModelPackage/ANSModelKeysGenerated.h
+167
View File
@@ -0,0 +1,167 @@
Monocypher as a whole is dual-licensed. Choose whichever licence you
want from the two licences listed below.
The first licence is a regular 2-clause BSD licence. The second licence
is the CC-0 from Creative Commons. It is intended to release Monocypher
to the public domain. The BSD licence serves as a fallback option.
See the individual files for specific information about who contributed
to what file during which years. See below for special notes.
Licence 1 (2-clause BSD)
------------------------
Copyright (c) 2017-2023, Loup Vaillant
Copyright (c) 2017-2019, Michael Savage
Copyright (c) 2017-2023, Fabio Scotoni
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Licence 2 (CC-0)
----------------
> CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
> LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
> ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
> INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
> REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
> PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
> THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
> HEREUNDER.
### Statement of Purpose
The laws of most jurisdictions throughout the world automatically confer
exclusive Copyright and Related Rights (defined below) upon the creator
and subsequent owner(s) (each and all, an "owner") of an original work
of authorship and/or a database (each, a "Work").
Certain owners wish to permanently relinquish those rights to a Work for
the purpose of contributing to a commons of creative, cultural and
scientific works ("Commons") that the public can reliably and without
fear of later claims of infringement build upon, modify, incorporate in
other works, reuse and redistribute as freely as possible in any form
whatsoever and for any purposes, including without limitation commercial
purposes. These owners may contribute to the Commons to promote the
ideal of a free culture and the further production of creative, cultural
and scientific works, or to gain reputation or greater distribution for
their Work in part through the use and efforts of others.
For these and/or other purposes and motivations, and without any
expectation of additional consideration or compensation, the person
associating CC0 with a Work (the "Affirmer"), to the extent that he or
she is an owner of Copyright and Related Rights in the Work, voluntarily
elects to apply CC0 to the Work and publicly distribute the Work under
its terms, with knowledge of his or her Copyright and Related Rights in
the Work and the meaning and intended legal effect of CC0 on those
rights.
1. **Copyright and Related Rights.** A Work made available under CC0 may
be protected by copyright and related or neighboring rights
("Copyright and Related Rights"). Copyright and Related Rights
include, but are not limited to, the following:
- the right to reproduce, adapt, distribute, perform, display,
communicate, and translate a Work;
- moral rights retained by the original author(s) and/or
performer(s); publicity and privacy rights pertaining to a person's
image or likeness depicted in a Work;
- rights protecting against unfair competition in regards to a Work,
subject to the limitations in paragraph 4(a), below;
- rights protecting the extraction, dissemination, use and reuse of
data in a Work;
- database rights (such as those arising under Directive 96/9/EC of
the European Parliament and of the Council of 11 March 1996 on the
legal protection of databases, and under any national
implementation thereof, including any amended or successor version
of such directive); and
- other similar, equivalent or corresponding rights throughout the
world based on applicable law or treaty, and any national
implementations thereof.
2. **Waiver.** To the greatest extent permitted by, but not in
contravention of, applicable law, Affirmer hereby overtly, fully,
permanently, irrevocably and unconditionally waives, abandons, and
surrenders all of Affirmer's Copyright and Related Rights and
associated claims and causes of action, whether now known or unknown
(including existing as well as future claims and causes of action),
in the Work (i) in all territories worldwide, (ii) for the maximum
duration provided by applicable law or treaty (including future time
extensions), (iii) in any current or future medium and for any number
of copies, and (iv) for any purpose whatsoever, including without
limitation commercial, advertising or promotional purposes (the
"Waiver"). Affirmer makes the Waiver for the benefit of each member
of the public at large and to the detriment of Affirmer's heirs and
successors, fully intending that such Waiver shall not be subject to
revocation, rescission, cancellation, termination, or any other legal
or equitable action to disrupt the quiet enjoyment of the Work by the
public as contemplated by Affirmer's express Statement of Purpose.
3. **Public License Fallback.** Should any part of the Waiver for any
reason be judged legally invalid or ineffective under applicable law,
then the Waiver shall be preserved to the maximum extent permitted
taking into account Affirmer's express Statement of Purpose. In
addition, to the extent the Waiver is so judged Affirmer hereby
grants to each affected person a royalty-free, non transferable, non
sublicensable, non exclusive, irrevocable and unconditional license
to exercise Affirmer's Copyright and Related Rights in the Work (i)
in all territories worldwide, (ii) for the maximum duration provided
by applicable law or treaty (including future time extensions), (iii)
in any current or future medium and for any number of copies, and
(iv) for any purpose whatsoever, including without limitation
commercial, advertising or promotional purposes (the "License"). The
License shall be deemed effective as of the date CC0 was applied by
Affirmer to the Work. Should any part of the License for any reason
be judged legally invalid or ineffective under applicable law, such
partial invalidity or ineffectiveness shall not invalidate the
remainder of the License, and in such case Affirmer hereby affirms
that he or she will not (i) exercise any of his or her remaining
Copyright and Related Rights in the Work or (ii) assert any
associated claims and causes of action with respect to the Work, in
either case contrary to Affirmer's express Statement of Purpose.
4. **Limitations and Disclaimers.**
- No trademark or patent rights held by Affirmer are waived,
abandoned, surrendered, licensed or otherwise affected by this
document.
- Affirmer offers the Work as-is and makes no representations or
warranties of any kind concerning the Work, express, implied,
statutory or otherwise, including without limitation warranties of
title, merchantability, fitness for a particular purpose, non
infringement, or the absence of latent or other defects, accuracy,
or the present or absence of errors, whether or not discoverable,
all to the greatest extent permissible under applicable law.
- Affirmer disclaims responsibility for clearing rights of other
persons that may apply to the Work or any use thereof, including
without limitation any person's Copyright and Related Rights in the
Work. Further, Affirmer disclaims responsibility for obtaining any
necessary consents, permissions or other rights required for any
use of the Work.
- Affirmer understands and acknowledges that Creative Commons is not
a party to this document and has no duty or obligation with respect
to this CC0 or use of the Work.
+13
View File
@@ -0,0 +1,13 @@
# Monocypher 4.0.2 — vendored
Source: https://monocypher.org/download/monocypher-4.0.2.tar.gz
Files: `monocypher.c/.h`, `monocypher-ed25519.c/.h`, `LICENCE.md`. Unmodified.
Licence: dual 2-clause BSD / CC-0.
Here for the model package format (`core/ANSModelPackage`): X25519 to unwrap a
package's content key, and RFC 8032 Ed25519 to prove where the package came
from. The `-ed25519` pair is not optional — Monocypher's own EdDSA signs with
BLAKE2b, which would not interoperate with a package signed anywhere else.
ANSMINDCORE vendors the same release for the same reason. The two readers must
stay byte-compatible, so update them together and run both suites.
+500
View File
@@ -0,0 +1,500 @@
// Monocypher version 4.0.2
//
// This file is dual-licensed. Choose whichever licence you want from
// the two licences listed below.
//
// The first licence is a regular 2-clause BSD licence. The second licence
// is the CC-0 from Creative Commons. It is intended to release Monocypher
// to the public domain. The BSD licence serves as a fallback option.
//
// SPDX-License-Identifier: BSD-2-Clause OR CC0-1.0
//
// ------------------------------------------------------------------------
//
// Copyright (c) 2017-2019, Loup Vaillant
// All rights reserved.
//
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// ------------------------------------------------------------------------
//
// Written in 2017-2019 by Loup Vaillant
//
// To the extent possible under law, the author(s) have dedicated all copyright
// and related neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along
// with this software. If not, see
// <https://creativecommons.org/publicdomain/zero/1.0/>
#include "monocypher-ed25519.h"
#ifdef MONOCYPHER_CPP_NAMESPACE
namespace MONOCYPHER_CPP_NAMESPACE {
#endif
/////////////////
/// Utilities ///
/////////////////
#define FOR(i, min, max) for (size_t i = min; i < max; i++)
#define COPY(dst, src, size) FOR(_i_, 0, size) (dst)[_i_] = (src)[_i_]
#define ZERO(buf, size) FOR(_i_, 0, size) (buf)[_i_] = 0
#define WIPE_CTX(ctx) crypto_wipe(ctx , sizeof(*(ctx)))
#define WIPE_BUFFER(buffer) crypto_wipe(buffer, sizeof(buffer))
#define MIN(a, b) ((a) <= (b) ? (a) : (b))
typedef uint8_t u8;
typedef uint64_t u64;
// Returns the smallest positive integer y such that
// (x + y) % pow_2 == 0
// Basically, it's how many bytes we need to add to "align" x.
// Only works when pow_2 is a power of 2.
// Note: we use ~x+1 instead of -x to avoid compiler warnings
static size_t align(size_t x, size_t pow_2)
{
return (~x + 1) & (pow_2 - 1);
}
static u64 load64_be(const u8 s[8])
{
return((u64)s[0] << 56)
| ((u64)s[1] << 48)
| ((u64)s[2] << 40)
| ((u64)s[3] << 32)
| ((u64)s[4] << 24)
| ((u64)s[5] << 16)
| ((u64)s[6] << 8)
| (u64)s[7];
}
static void store64_be(u8 out[8], u64 in)
{
out[0] = (in >> 56) & 0xff;
out[1] = (in >> 48) & 0xff;
out[2] = (in >> 40) & 0xff;
out[3] = (in >> 32) & 0xff;
out[4] = (in >> 24) & 0xff;
out[5] = (in >> 16) & 0xff;
out[6] = (in >> 8) & 0xff;
out[7] = in & 0xff;
}
static void load64_be_buf (u64 *dst, const u8 *src, size_t size) {
FOR(i, 0, size) { dst[i] = load64_be(src + i*8); }
}
///////////////
/// SHA 512 ///
///////////////
static u64 rot(u64 x, int c ) { return (x >> c) | (x << (64 - c)); }
static u64 ch (u64 x, u64 y, u64 z) { return (x & y) ^ (~x & z); }
static u64 maj(u64 x, u64 y, u64 z) { return (x & y) ^ ( x & z) ^ (y & z); }
static u64 big_sigma0(u64 x) { return rot(x, 28) ^ rot(x, 34) ^ rot(x, 39); }
static u64 big_sigma1(u64 x) { return rot(x, 14) ^ rot(x, 18) ^ rot(x, 41); }
static u64 lit_sigma0(u64 x) { return rot(x, 1) ^ rot(x, 8) ^ (x >> 7); }
static u64 lit_sigma1(u64 x) { return rot(x, 19) ^ rot(x, 61) ^ (x >> 6); }
static const u64 K[80] = {
0x428a2f98d728ae22,0x7137449123ef65cd,0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc,
0x3956c25bf348b538,0x59f111f1b605d019,0x923f82a4af194f9b,0xab1c5ed5da6d8118,
0xd807aa98a3030242,0x12835b0145706fbe,0x243185be4ee4b28c,0x550c7dc3d5ffb4e2,
0x72be5d74f27b896f,0x80deb1fe3b1696b1,0x9bdc06a725c71235,0xc19bf174cf692694,
0xe49b69c19ef14ad2,0xefbe4786384f25e3,0x0fc19dc68b8cd5b5,0x240ca1cc77ac9c65,
0x2de92c6f592b0275,0x4a7484aa6ea6e483,0x5cb0a9dcbd41fbd4,0x76f988da831153b5,
0x983e5152ee66dfab,0xa831c66d2db43210,0xb00327c898fb213f,0xbf597fc7beef0ee4,
0xc6e00bf33da88fc2,0xd5a79147930aa725,0x06ca6351e003826f,0x142929670a0e6e70,
0x27b70a8546d22ffc,0x2e1b21385c26c926,0x4d2c6dfc5ac42aed,0x53380d139d95b3df,
0x650a73548baf63de,0x766a0abb3c77b2a8,0x81c2c92e47edaee6,0x92722c851482353b,
0xa2bfe8a14cf10364,0xa81a664bbc423001,0xc24b8b70d0f89791,0xc76c51a30654be30,
0xd192e819d6ef5218,0xd69906245565a910,0xf40e35855771202a,0x106aa07032bbd1b8,
0x19a4c116b8d2d0c8,0x1e376c085141ab53,0x2748774cdf8eeb99,0x34b0bcb5e19b48a8,
0x391c0cb3c5c95a63,0x4ed8aa4ae3418acb,0x5b9cca4f7763e373,0x682e6ff3d6b2b8a3,
0x748f82ee5defb2fc,0x78a5636f43172f60,0x84c87814a1f0ab72,0x8cc702081a6439ec,
0x90befffa23631e28,0xa4506cebde82bde9,0xbef9a3f7b2c67915,0xc67178f2e372532b,
0xca273eceea26619c,0xd186b8c721c0c207,0xeada7dd6cde0eb1e,0xf57d4f7fee6ed178,
0x06f067aa72176fba,0x0a637dc5a2c898a6,0x113f9804bef90dae,0x1b710b35131c471b,
0x28db77f523047d84,0x32caab7b40c72493,0x3c9ebe0a15c9bebc,0x431d67c49c100d4c,
0x4cc5d4becb3e42b6,0x597f299cfc657e2a,0x5fcb6fab3ad6faec,0x6c44198c4a475817
};
static void sha512_compress(crypto_sha512_ctx *ctx)
{
u64 a = ctx->hash[0]; u64 b = ctx->hash[1];
u64 c = ctx->hash[2]; u64 d = ctx->hash[3];
u64 e = ctx->hash[4]; u64 f = ctx->hash[5];
u64 g = ctx->hash[6]; u64 h = ctx->hash[7];
FOR (j, 0, 16) {
u64 in = K[j] + ctx->input[j];
u64 t1 = big_sigma1(e) + ch (e, f, g) + h + in;
u64 t2 = big_sigma0(a) + maj(a, b, c);
h = g; g = f; f = e; e = d + t1;
d = c; c = b; b = a; a = t1 + t2;
}
size_t i16 = 0;
FOR(i, 1, 5) {
i16 += 16;
FOR (j, 0, 16) {
ctx->input[j] += lit_sigma1(ctx->input[(j- 2) & 15]);
ctx->input[j] += lit_sigma0(ctx->input[(j-15) & 15]);
ctx->input[j] += ctx->input[(j- 7) & 15];
u64 in = K[i16 + j] + ctx->input[j];
u64 t1 = big_sigma1(e) + ch (e, f, g) + h + in;
u64 t2 = big_sigma0(a) + maj(a, b, c);
h = g; g = f; f = e; e = d + t1;
d = c; c = b; b = a; a = t1 + t2;
}
}
ctx->hash[0] += a; ctx->hash[1] += b;
ctx->hash[2] += c; ctx->hash[3] += d;
ctx->hash[4] += e; ctx->hash[5] += f;
ctx->hash[6] += g; ctx->hash[7] += h;
}
// Write 1 input byte
static void sha512_set_input(crypto_sha512_ctx *ctx, u8 input)
{
size_t word = ctx->input_idx >> 3;
size_t byte = ctx->input_idx & 7;
ctx->input[word] |= (u64)input << (8 * (7 - byte));
}
// Increment a 128-bit "word".
static void sha512_incr(u64 x[2], u64 y)
{
x[1] += y;
if (x[1] < y) {
x[0]++;
}
}
void crypto_sha512_init(crypto_sha512_ctx *ctx)
{
ctx->hash[0] = 0x6a09e667f3bcc908;
ctx->hash[1] = 0xbb67ae8584caa73b;
ctx->hash[2] = 0x3c6ef372fe94f82b;
ctx->hash[3] = 0xa54ff53a5f1d36f1;
ctx->hash[4] = 0x510e527fade682d1;
ctx->hash[5] = 0x9b05688c2b3e6c1f;
ctx->hash[6] = 0x1f83d9abfb41bd6b;
ctx->hash[7] = 0x5be0cd19137e2179;
ctx->input_size[0] = 0;
ctx->input_size[1] = 0;
ctx->input_idx = 0;
ZERO(ctx->input, 16);
}
void crypto_sha512_update(crypto_sha512_ctx *ctx,
const u8 *message, size_t message_size)
{
// Avoid undefined NULL pointer increments with empty messages
if (message_size == 0) {
return;
}
// Align ourselves with word boundaries
if ((ctx->input_idx & 7) != 0) {
size_t nb_bytes = MIN(align(ctx->input_idx, 8), message_size);
FOR (i, 0, nb_bytes) {
sha512_set_input(ctx, message[i]);
ctx->input_idx++;
}
message += nb_bytes;
message_size -= nb_bytes;
}
// Align ourselves with block boundaries
if ((ctx->input_idx & 127) != 0) {
size_t nb_words = MIN(align(ctx->input_idx, 128), message_size) >> 3;
load64_be_buf(ctx->input + (ctx->input_idx >> 3), message, nb_words);
ctx->input_idx += nb_words << 3;
message += nb_words << 3;
message_size -= nb_words << 3;
}
// Compress block if needed
if (ctx->input_idx == 128) {
sha512_incr(ctx->input_size, 1024); // size is in bits
sha512_compress(ctx);
ctx->input_idx = 0;
ZERO(ctx->input, 16);
}
// Process the message block by block
FOR (i, 0, message_size >> 7) { // number of blocks
load64_be_buf(ctx->input, message, 16);
sha512_incr(ctx->input_size, 1024); // size is in bits
sha512_compress(ctx);
ctx->input_idx = 0;
ZERO(ctx->input, 16);
message += 128;
}
message_size &= 127;
if (message_size != 0) {
// Remaining words
size_t nb_words = message_size >> 3;
load64_be_buf(ctx->input, message, nb_words);
ctx->input_idx += nb_words << 3;
message += nb_words << 3;
message_size -= nb_words << 3;
// Remaining bytes
FOR (i, 0, message_size) {
sha512_set_input(ctx, message[i]);
ctx->input_idx++;
}
}
}
void crypto_sha512_final(crypto_sha512_ctx *ctx, u8 hash[64])
{
// Add padding bit
if (ctx->input_idx == 0) {
ZERO(ctx->input, 16);
}
sha512_set_input(ctx, 128);
// Update size
sha512_incr(ctx->input_size, ctx->input_idx * 8);
// Compress penultimate block (if any)
if (ctx->input_idx > 111) {
sha512_compress(ctx);
ZERO(ctx->input, 14);
}
// Compress last block
ctx->input[14] = ctx->input_size[0];
ctx->input[15] = ctx->input_size[1];
sha512_compress(ctx);
// Copy hash to output (big endian)
FOR (i, 0, 8) {
store64_be(hash + i*8, ctx->hash[i]);
}
WIPE_CTX(ctx);
}
void crypto_sha512(u8 hash[64], const u8 *message, size_t message_size)
{
crypto_sha512_ctx ctx;
crypto_sha512_init (&ctx);
crypto_sha512_update(&ctx, message, message_size);
crypto_sha512_final (&ctx, hash);
}
////////////////////
/// HMAC SHA 512 ///
////////////////////
void crypto_sha512_hmac_init(crypto_sha512_hmac_ctx *ctx,
const u8 *key, size_t key_size)
{
// hash key if it is too long
if (key_size > 128) {
crypto_sha512(ctx->key, key, key_size);
key = ctx->key;
key_size = 64;
}
// Compute inner key: padded key XOR 0x36
FOR (i, 0, key_size) { ctx->key[i] = key[i] ^ 0x36; }
FOR (i, key_size, 128) { ctx->key[i] = 0x36; }
// Start computing inner hash
crypto_sha512_init (&ctx->ctx);
crypto_sha512_update(&ctx->ctx, ctx->key, 128);
}
void crypto_sha512_hmac_update(crypto_sha512_hmac_ctx *ctx,
const u8 *message, size_t message_size)
{
crypto_sha512_update(&ctx->ctx, message, message_size);
}
void crypto_sha512_hmac_final(crypto_sha512_hmac_ctx *ctx, u8 hmac[64])
{
// Finish computing inner hash
crypto_sha512_final(&ctx->ctx, hmac);
// Compute outer key: padded key XOR 0x5c
FOR (i, 0, 128) {
ctx->key[i] ^= 0x36 ^ 0x5c;
}
// Compute outer hash
crypto_sha512_init (&ctx->ctx);
crypto_sha512_update(&ctx->ctx, ctx->key , 128);
crypto_sha512_update(&ctx->ctx, hmac, 64);
crypto_sha512_final (&ctx->ctx, hmac); // outer hash
WIPE_CTX(ctx);
}
void crypto_sha512_hmac(u8 hmac[64], const u8 *key, size_t key_size,
const u8 *message, size_t message_size)
{
crypto_sha512_hmac_ctx ctx;
crypto_sha512_hmac_init (&ctx, key, key_size);
crypto_sha512_hmac_update(&ctx, message, message_size);
crypto_sha512_hmac_final (&ctx, hmac);
}
////////////////////
/// HKDF SHA 512 ///
////////////////////
void crypto_sha512_hkdf_expand(u8 *okm, size_t okm_size,
const u8 *prk, size_t prk_size,
const u8 *info, size_t info_size)
{
int not_first = 0;
u8 ctr = 1;
u8 blk[64];
while (okm_size > 0) {
size_t out_size = MIN(okm_size, sizeof(blk));
crypto_sha512_hmac_ctx ctx;
crypto_sha512_hmac_init(&ctx, prk , prk_size);
if (not_first) {
// For some reason HKDF uses some kind of CBC mode.
// For some reason CTR mode alone wasn't enough.
// Like what, they didn't trust HMAC in 2010? Really??
crypto_sha512_hmac_update(&ctx, blk , sizeof(blk));
}
crypto_sha512_hmac_update(&ctx, info, info_size);
crypto_sha512_hmac_update(&ctx, &ctr, 1);
crypto_sha512_hmac_final(&ctx, blk);
COPY(okm, blk, out_size);
not_first = 1;
okm += out_size;
okm_size -= out_size;
ctr++;
}
}
void crypto_sha512_hkdf(u8 *okm , size_t okm_size,
const u8 *ikm , size_t ikm_size,
const u8 *salt, size_t salt_size,
const u8 *info, size_t info_size)
{
// Extract
u8 prk[64];
crypto_sha512_hmac(prk, salt, salt_size, ikm, ikm_size);
// Expand
crypto_sha512_hkdf_expand(okm, okm_size, prk, sizeof(prk), info, info_size);
}
///////////////
/// Ed25519 ///
///////////////
void crypto_ed25519_key_pair(u8 secret_key[64], u8 public_key[32], u8 seed[32])
{
u8 a[64];
COPY(a, seed, 32); // a[ 0..31] = seed
crypto_wipe(seed, 32);
COPY(secret_key, a, 32); // secret key = seed
crypto_sha512(a, a, 32); // a[ 0..31] = scalar
crypto_eddsa_trim_scalar(a, a); // a[ 0..31] = trimmed scalar
crypto_eddsa_scalarbase(public_key, a); // public key = [trimmed scalar]B
COPY(secret_key + 32, public_key, 32); // secret key includes public half
WIPE_BUFFER(a);
}
static void hash_reduce(u8 h[32],
const u8 *a, size_t a_size,
const u8 *b, size_t b_size,
const u8 *c, size_t c_size,
const u8 *d, size_t d_size)
{
u8 hash[64];
crypto_sha512_ctx ctx;
crypto_sha512_init (&ctx);
crypto_sha512_update(&ctx, a, a_size);
crypto_sha512_update(&ctx, b, b_size);
crypto_sha512_update(&ctx, c, c_size);
crypto_sha512_update(&ctx, d, d_size);
crypto_sha512_final (&ctx, hash);
crypto_eddsa_reduce(h, hash);
}
static void ed25519_dom_sign(u8 signature [64], const u8 secret_key[32],
const u8 *dom, size_t dom_size,
const u8 *message, size_t message_size)
{
u8 a[64]; // secret scalar and prefix
u8 r[32]; // secret deterministic "random" nonce
u8 h[32]; // publically verifiable hash of the message (not wiped)
u8 R[32]; // first half of the signature (allows overlapping inputs)
const u8 *pk = secret_key + 32;
crypto_sha512(a, secret_key, 32);
crypto_eddsa_trim_scalar(a, a);
hash_reduce(r, dom, dom_size, a + 32, 32, message, message_size, 0, 0);
crypto_eddsa_scalarbase(R, r);
hash_reduce(h, dom, dom_size, R, 32, pk, 32, message, message_size);
COPY(signature, R, 32);
crypto_eddsa_mul_add(signature + 32, h, a, r);
WIPE_BUFFER(a);
WIPE_BUFFER(r);
}
void crypto_ed25519_sign(u8 signature [64], const u8 secret_key[64],
const u8 *message, size_t message_size)
{
ed25519_dom_sign(signature, secret_key, 0, 0, message, message_size);
}
int crypto_ed25519_check(const u8 signature[64], const u8 public_key[32],
const u8 *msg, size_t msg_size)
{
u8 h_ram[32];
hash_reduce(h_ram, signature, 32, public_key, 32, msg, msg_size, 0, 0);
return crypto_eddsa_check_equation(signature, public_key, h_ram);
}
static const u8 domain[34] = "SigEd25519 no Ed25519 collisions\1";
void crypto_ed25519_ph_sign(uint8_t signature[64], const uint8_t secret_key[64],
const uint8_t message_hash[64])
{
ed25519_dom_sign(signature, secret_key, domain, sizeof(domain),
message_hash, 64);
}
int crypto_ed25519_ph_check(const uint8_t sig[64], const uint8_t pk[32],
const uint8_t msg_hash[64])
{
u8 h_ram[32];
hash_reduce(h_ram, domain, sizeof(domain), sig, 32, pk, 32, msg_hash, 64);
return crypto_eddsa_check_equation(sig, pk, h_ram);
}
#ifdef MONOCYPHER_CPP_NAMESPACE
}
#endif
+140
View File
@@ -0,0 +1,140 @@
// Monocypher version 4.0.2
//
// This file is dual-licensed. Choose whichever licence you want from
// the two licences listed below.
//
// The first licence is a regular 2-clause BSD licence. The second licence
// is the CC-0 from Creative Commons. It is intended to release Monocypher
// to the public domain. The BSD licence serves as a fallback option.
//
// SPDX-License-Identifier: BSD-2-Clause OR CC0-1.0
//
// ------------------------------------------------------------------------
//
// Copyright (c) 2017-2019, Loup Vaillant
// All rights reserved.
//
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// ------------------------------------------------------------------------
//
// Written in 2017-2019 by Loup Vaillant
//
// To the extent possible under law, the author(s) have dedicated all copyright
// and related neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along
// with this software. If not, see
// <https://creativecommons.org/publicdomain/zero/1.0/>
#ifndef ED25519_H
#define ED25519_H
#include "monocypher.h"
#ifdef MONOCYPHER_CPP_NAMESPACE
namespace MONOCYPHER_CPP_NAMESPACE {
#elif defined(__cplusplus)
extern "C" {
#endif
////////////////////////
/// Type definitions ///
////////////////////////
// Do not rely on the size or content on any of those types,
// they may change without notice.
typedef struct {
uint64_t hash[8];
uint64_t input[16];
uint64_t input_size[2];
size_t input_idx;
} crypto_sha512_ctx;
typedef struct {
uint8_t key[128];
crypto_sha512_ctx ctx;
} crypto_sha512_hmac_ctx;
// SHA 512
// -------
void crypto_sha512_init (crypto_sha512_ctx *ctx);
void crypto_sha512_update(crypto_sha512_ctx *ctx,
const uint8_t *message, size_t message_size);
void crypto_sha512_final (crypto_sha512_ctx *ctx, uint8_t hash[64]);
void crypto_sha512(uint8_t hash[64],
const uint8_t *message, size_t message_size);
// SHA 512 HMAC
// ------------
void crypto_sha512_hmac_init(crypto_sha512_hmac_ctx *ctx,
const uint8_t *key, size_t key_size);
void crypto_sha512_hmac_update(crypto_sha512_hmac_ctx *ctx,
const uint8_t *message, size_t message_size);
void crypto_sha512_hmac_final(crypto_sha512_hmac_ctx *ctx, uint8_t hmac[64]);
void crypto_sha512_hmac(uint8_t hmac[64],
const uint8_t *key , size_t key_size,
const uint8_t *message, size_t message_size);
// SHA 512 HKDF
// ------------
void crypto_sha512_hkdf_expand(uint8_t *okm, size_t okm_size,
const uint8_t *prk, size_t prk_size,
const uint8_t *info, size_t info_size);
void crypto_sha512_hkdf(uint8_t *okm , size_t okm_size,
const uint8_t *ikm , size_t ikm_size,
const uint8_t *salt, size_t salt_size,
const uint8_t *info, size_t info_size);
// Ed25519
// -------
// Signatures (EdDSA with curve25519 + SHA-512)
// --------------------------------------------
void crypto_ed25519_key_pair(uint8_t secret_key[64],
uint8_t public_key[32],
uint8_t seed[32]);
void crypto_ed25519_sign(uint8_t signature [64],
const uint8_t secret_key[64],
const uint8_t *message, size_t message_size);
int crypto_ed25519_check(const uint8_t signature [64],
const uint8_t public_key[32],
const uint8_t *message, size_t message_size);
// Pre-hash variants
void crypto_ed25519_ph_sign(uint8_t signature [64],
const uint8_t secret_key [64],
const uint8_t message_hash[64]);
int crypto_ed25519_ph_check(const uint8_t signature [64],
const uint8_t public_key [32],
const uint8_t message_hash[64]);
#ifdef __cplusplus
}
#endif
#endif // ED25519_H
+2956
View File
File diff suppressed because it is too large Load Diff
+321
View File
@@ -0,0 +1,321 @@
// Monocypher version 4.0.2
//
// This file is dual-licensed. Choose whichever licence you want from
// the two licences listed below.
//
// The first licence is a regular 2-clause BSD licence. The second licence
// is the CC-0 from Creative Commons. It is intended to release Monocypher
// to the public domain. The BSD licence serves as a fallback option.
//
// SPDX-License-Identifier: BSD-2-Clause OR CC0-1.0
//
// ------------------------------------------------------------------------
//
// Copyright (c) 2017-2019, Loup Vaillant
// All rights reserved.
//
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// ------------------------------------------------------------------------
//
// Written in 2017-2019 by Loup Vaillant
//
// To the extent possible under law, the author(s) have dedicated all copyright
// and related neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along
// with this software. If not, see
// <https://creativecommons.org/publicdomain/zero/1.0/>
#ifndef MONOCYPHER_H
#define MONOCYPHER_H
#include <stddef.h>
#include <stdint.h>
#ifdef MONOCYPHER_CPP_NAMESPACE
namespace MONOCYPHER_CPP_NAMESPACE {
#elif defined(__cplusplus)
extern "C" {
#endif
// Constant time comparisons
// -------------------------
// Return 0 if a and b are equal, -1 otherwise
int crypto_verify16(const uint8_t a[16], const uint8_t b[16]);
int crypto_verify32(const uint8_t a[32], const uint8_t b[32]);
int crypto_verify64(const uint8_t a[64], const uint8_t b[64]);
// Erase sensitive data
// --------------------
void crypto_wipe(void *secret, size_t size);
// Authenticated encryption
// ------------------------
void crypto_aead_lock(uint8_t *cipher_text,
uint8_t mac [16],
const uint8_t key [32],
const uint8_t nonce[24],
const uint8_t *ad, size_t ad_size,
const uint8_t *plain_text, size_t text_size);
int crypto_aead_unlock(uint8_t *plain_text,
const uint8_t mac [16],
const uint8_t key [32],
const uint8_t nonce[24],
const uint8_t *ad, size_t ad_size,
const uint8_t *cipher_text, size_t text_size);
// Authenticated stream
// --------------------
typedef struct {
uint64_t counter;
uint8_t key[32];
uint8_t nonce[8];
} crypto_aead_ctx;
void crypto_aead_init_x(crypto_aead_ctx *ctx,
const uint8_t key[32], const uint8_t nonce[24]);
void crypto_aead_init_djb(crypto_aead_ctx *ctx,
const uint8_t key[32], const uint8_t nonce[8]);
void crypto_aead_init_ietf(crypto_aead_ctx *ctx,
const uint8_t key[32], const uint8_t nonce[12]);
void crypto_aead_write(crypto_aead_ctx *ctx,
uint8_t *cipher_text,
uint8_t mac[16],
const uint8_t *ad , size_t ad_size,
const uint8_t *plain_text, size_t text_size);
int crypto_aead_read(crypto_aead_ctx *ctx,
uint8_t *plain_text,
const uint8_t mac[16],
const uint8_t *ad , size_t ad_size,
const uint8_t *cipher_text, size_t text_size);
// General purpose hash (BLAKE2b)
// ------------------------------
// Direct interface
void crypto_blake2b(uint8_t *hash, size_t hash_size,
const uint8_t *message, size_t message_size);
void crypto_blake2b_keyed(uint8_t *hash, size_t hash_size,
const uint8_t *key, size_t key_size,
const uint8_t *message, size_t message_size);
// Incremental interface
typedef struct {
// Do not rely on the size or contents of this type,
// for they may change without notice.
uint64_t hash[8];
uint64_t input_offset[2];
uint64_t input[16];
size_t input_idx;
size_t hash_size;
} crypto_blake2b_ctx;
void crypto_blake2b_init(crypto_blake2b_ctx *ctx, size_t hash_size);
void crypto_blake2b_keyed_init(crypto_blake2b_ctx *ctx, size_t hash_size,
const uint8_t *key, size_t key_size);
void crypto_blake2b_update(crypto_blake2b_ctx *ctx,
const uint8_t *message, size_t message_size);
void crypto_blake2b_final(crypto_blake2b_ctx *ctx, uint8_t *hash);
// Password key derivation (Argon2)
// --------------------------------
#define CRYPTO_ARGON2_D 0
#define CRYPTO_ARGON2_I 1
#define CRYPTO_ARGON2_ID 2
typedef struct {
uint32_t algorithm; // Argon2d, Argon2i, Argon2id
uint32_t nb_blocks; // memory hardness, >= 8 * nb_lanes
uint32_t nb_passes; // CPU hardness, >= 1 (>= 3 recommended for Argon2i)
uint32_t nb_lanes; // parallelism level (single threaded anyway)
} crypto_argon2_config;
typedef struct {
const uint8_t *pass;
const uint8_t *salt;
uint32_t pass_size;
uint32_t salt_size; // 16 bytes recommended
} crypto_argon2_inputs;
typedef struct {
const uint8_t *key; // may be NULL if no key
const uint8_t *ad; // may be NULL if no additional data
uint32_t key_size; // 0 if no key (32 bytes recommended otherwise)
uint32_t ad_size; // 0 if no additional data
} crypto_argon2_extras;
extern const crypto_argon2_extras crypto_argon2_no_extras;
void crypto_argon2(uint8_t *hash, uint32_t hash_size, void *work_area,
crypto_argon2_config config,
crypto_argon2_inputs inputs,
crypto_argon2_extras extras);
// Key exchange (X-25519)
// ----------------------
// Shared secrets are not quite random.
// Hash them to derive an actual shared key.
void crypto_x25519_public_key(uint8_t public_key[32],
const uint8_t secret_key[32]);
void crypto_x25519(uint8_t raw_shared_secret[32],
const uint8_t your_secret_key [32],
const uint8_t their_public_key [32]);
// Conversion to EdDSA
void crypto_x25519_to_eddsa(uint8_t eddsa[32], const uint8_t x25519[32]);
// scalar "division"
// Used for OPRF. Be aware that exponential blinding is less secure
// than Diffie-Hellman key exchange.
void crypto_x25519_inverse(uint8_t blind_salt [32],
const uint8_t private_key[32],
const uint8_t curve_point[32]);
// "Dirty" versions of x25519_public_key().
// Use with crypto_elligator_rev().
// Leaks 3 bits of the private key.
void crypto_x25519_dirty_small(uint8_t pk[32], const uint8_t sk[32]);
void crypto_x25519_dirty_fast (uint8_t pk[32], const uint8_t sk[32]);
// Signatures
// ----------
// EdDSA with curve25519 + BLAKE2b
void crypto_eddsa_key_pair(uint8_t secret_key[64],
uint8_t public_key[32],
uint8_t seed[32]);
void crypto_eddsa_sign(uint8_t signature [64],
const uint8_t secret_key[64],
const uint8_t *message, size_t message_size);
int crypto_eddsa_check(const uint8_t signature [64],
const uint8_t public_key[32],
const uint8_t *message, size_t message_size);
// Conversion to X25519
void crypto_eddsa_to_x25519(uint8_t x25519[32], const uint8_t eddsa[32]);
// EdDSA building blocks
void crypto_eddsa_trim_scalar(uint8_t out[32], const uint8_t in[32]);
void crypto_eddsa_reduce(uint8_t reduced[32], const uint8_t expanded[64]);
void crypto_eddsa_mul_add(uint8_t r[32],
const uint8_t a[32],
const uint8_t b[32],
const uint8_t c[32]);
void crypto_eddsa_scalarbase(uint8_t point[32], const uint8_t scalar[32]);
int crypto_eddsa_check_equation(const uint8_t signature[64],
const uint8_t public_key[32],
const uint8_t h_ram[32]);
// Chacha20
// --------
// Specialised hash.
// Used to hash X25519 shared secrets.
void crypto_chacha20_h(uint8_t out[32],
const uint8_t key[32],
const uint8_t in [16]);
// Unauthenticated stream cipher.
// Don't forget to add authentication.
uint64_t crypto_chacha20_djb(uint8_t *cipher_text,
const uint8_t *plain_text,
size_t text_size,
const uint8_t key[32],
const uint8_t nonce[8],
uint64_t ctr);
uint32_t crypto_chacha20_ietf(uint8_t *cipher_text,
const uint8_t *plain_text,
size_t text_size,
const uint8_t key[32],
const uint8_t nonce[12],
uint32_t ctr);
uint64_t crypto_chacha20_x(uint8_t *cipher_text,
const uint8_t *plain_text,
size_t text_size,
const uint8_t key[32],
const uint8_t nonce[24],
uint64_t ctr);
// Poly 1305
// ---------
// This is a *one time* authenticator.
// Disclosing the mac reveals the key.
// See crypto_lock() on how to use it properly.
// Direct interface
void crypto_poly1305(uint8_t mac[16],
const uint8_t *message, size_t message_size,
const uint8_t key[32]);
// Incremental interface
typedef struct {
// Do not rely on the size or contents of this type,
// for they may change without notice.
uint8_t c[16]; // chunk of the message
size_t c_idx; // How many bytes are there in the chunk.
uint32_t r [4]; // constant multiplier (from the secret key)
uint32_t pad[4]; // random number added at the end (from the secret key)
uint32_t h [5]; // accumulated hash
} crypto_poly1305_ctx;
void crypto_poly1305_init (crypto_poly1305_ctx *ctx, const uint8_t key[32]);
void crypto_poly1305_update(crypto_poly1305_ctx *ctx,
const uint8_t *message, size_t message_size);
void crypto_poly1305_final (crypto_poly1305_ctx *ctx, uint8_t mac[16]);
// Elligator 2
// -----------
// Elligator mappings proper
void crypto_elligator_map(uint8_t curve [32], const uint8_t hidden[32]);
int crypto_elligator_rev(uint8_t hidden[32], const uint8_t curve [32],
uint8_t tweak);
// Easy to use key pair generation
void crypto_elligator_key_pair(uint8_t hidden[32], uint8_t secret_key[32],
uint8_t seed[32]);
#ifdef __cplusplus
}
#endif
#endif // MONOCYPHER_H
+15 -1
View File
@@ -9,7 +9,14 @@ if(CMAKE_CUDA_COMPILER)
set(ANSCORE_HAS_CUDA ON)
set(CMAKE_CUDA_STANDARD 20)
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
set(CMAKE_CUDA_ARCHITECTURES 75 80 86 87 88 89 90 100)
# SASS (-real) for each shipping arch from Turing (sm_75) through Blackwell
# consumer (sm_120), plus compute_75 PTX (-virtual) so any newer/unlisted GPU
# JIT-compiles at first run (forward compatibility). NOTE: this only governs
# this project's own .cu files; prebuilt deps (odhublib, TensorRT, ORT, cuBLAS)
# carry their own arch coverage independently.
set(CMAKE_CUDA_ARCHITECTURES
75-real 80-real 86-real 87-real 88-real 89-real 90-real 100-real 120-real
75-virtual)
if(MSVC)
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --use-fast-math -Xcompiler=/utf-8")
else()
@@ -36,6 +43,12 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# ANSCORE uses no C++20 modules. CMake auto-enables module dependency scanning
# for C++20 + Ninja, which adds a fragile per-TU scan/dyndep (.ddi/.modmap) step
# that can fail (empty .ddi -> "cannot open ...modmap" / dyndep parse errors),
# especially after an interrupted build. Disable it: faster builds, no dyndep.
set(CMAKE_CXX_SCAN_FOR_MODULES OFF)
# Output all binaries to a single directory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
@@ -142,6 +155,7 @@ include(cmake/Dependencies.cmake)
add_subdirectory(core/anslicensing)
add_subdirectory(core/ANSLicensingSystem)
add_subdirectory(core/ANSLibsLoader)
add_subdirectory(core/ANSModelPackage)
# Modules — feature DLLs (cross-platform)
add_subdirectory(modules/ANSMOT)
+174
View File
@@ -9,10 +9,76 @@
#include "ANSLicense.h" // ANS_DBG macro (gated by ANSCORE_DEBUGVIEW)
#include <cstdarg>
#include <mutex>
#include <unordered_map>
extern "C" {
#include "libavutil/imgutils.h"
#include "libavutil/buffer.h"
#include "libavutil/mem.h"
#include "libavutil/log.h"
}
// ---------------------------------------------------------------------------
// FFmpeg error-log bridge — corruption detection for hwaccel decode.
//
// On slice loss the HEVC decoder logs "First slice in a frame missing" /
// "Could not find ref with POC n" at AV_LOG_ERROR, but with hwaccel (NVDEC)
// it does NOT set AVFrame::decode_error_flags — verified empirically — so
// frame-level inspection cannot see the corruption. This bridge installs a
// process-wide av_log callback that maps the logging AVCodecContext back to
// its CVideoDecoder and latches a per-decoder corruption flag, which
// readFrame() consumes to re-arm the keyframe gate.
//
// Notes: with hwaccel the decoder runs single-threaded, so the logging ctx
// is exactly our AVCodecContext (SW frame-threading logs from per-thread
// clones and would not match — those paths set decode_error_flags instead).
// The callback chains to av_log_default_callback so console output remains.
// ---------------------------------------------------------------------------
namespace {
std::mutex s_logBridgeMutex;
std::unordered_map<void*, std::atomic<bool>*> s_logBridgeMap; // AVCodecContext* → corrupt flag
void ansAvLogBridge(void* ptr, int level, const char* fmt, va_list vl)
{
if (ptr && level <= AV_LOG_ERROR) {
bool latched = false;
{
std::lock_guard<std::mutex> lk(s_logBridgeMutex);
auto it = s_logBridgeMap.find(ptr);
if (it != s_logBridgeMap.end()) {
it->second->store(true, std::memory_order_relaxed);
latched = true;
}
}
if (latched) {
// Show exactly which decoder-context error latched the corrupt
// flag — av_log_default_callback may filter it out (global log
// level), which would otherwise make gate arms look causeless.
va_list vc;
va_copy(vc, vl);
fprintf(stderr, "[FFmpeg-latch] ");
vfprintf(stderr, fmt, vc);
va_end(vc);
}
}
av_log_default_callback(ptr, level, fmt, vl);
}
void registerLogBridge(void* codecCtx, std::atomic<bool>* corruptFlag)
{
static std::once_flag s_installOnce;
std::call_once(s_installOnce, []() { av_log_set_callback(ansAvLogBridge); });
std::lock_guard<std::mutex> lk(s_logBridgeMutex);
s_logBridgeMap[codecCtx] = corruptFlag;
}
void unregisterLogBridge(void* codecCtx)
{
std::lock_guard<std::mutex> lk(s_logBridgeMutex);
s_logBridgeMap.erase(codecCtx);
}
}
// ---------------------------------------------------------------------------
@@ -428,6 +494,10 @@ void CVideoDecoder::uninit()
m_pContext->hw_device_ctx = NULL;
}
// Stop the av_log bridge from touching this decoder's flag BEFORE the
// context is destroyed (the map key is the context pointer).
unregisterLogBridge(m_pContext);
// FIXED: Close codec before freeing context
avcodec_close(m_pContext);
avcodec_free_context(&m_pContext);
@@ -602,6 +672,13 @@ BOOL CVideoDecoder::init(enum AVCodecID codec, uint8* extradata, int extradata_s
return FALSE;
}
// Route this context's FFmpeg error logs to our corruption flag so the
// keyframe gate can react to slice loss on the hwaccel path (which never
// sets decode_error_flags). Unregistered in uninit() before the context
// is freed.
m_bCorruptSeen.store(false, std::memory_order_relaxed);
registerLogBridge(m_pContext, &m_bCorruptSeen);
// Debug: one-shot visibility into which decoder actually got opened.
// m_bHardwareDecoderEnabled is set by hwDecoderInit() on success; when
// hwMode == HW_DECODING_DISABLE or hwDecoderInit failed, it stays FALSE
@@ -761,6 +838,10 @@ void CVideoDecoder::Start() {
if (m_pContext) {
avcodec_flush_buffers(m_pContext);
}
// Re-arm keyframe gating: after a flush the DPB is empty, so wait for the
// next IDR before delivering frames again (avoids post-restart green/blocky).
m_bWaitKeyframe = true;
m_nKeyframeWaitCount = 0;
m_bRunning = TRUE;
log_print(HT_LOG_INFO, "%s, Video decoder started\r\n", __FUNCTION__);
}
@@ -1093,6 +1174,99 @@ BOOL CVideoDecoder::readFrame()
continue;
}
// Mid-stream corruption gating — re-arm the keyframe gate on decode errors.
//
// Packet loss on lossy links (Wi-Fi cameras) drops slices mid-GOP:
// FFmpeg logs "First slice in a frame missing" / "Could not find ref
// with POC n" and conceals the damage. Every subsequent frame in the
// GOP references the concealed one, so rather than display green/
// blocky garbage until the next IDR, drop the frame and re-arm the
// keyframe gate — the display holds the last clean frame and resumes
// on the next clean keyframe.
//
// Two detection signals, both required:
// • decode_error_flags — set by SW decode paths (H.264 always,
// HEVC frame-threaded).
// • m_bCorruptSeen — latched by the av_log bridge; the ONLY signal
// on the hwaccel (NVDEC) path, which logs the error but leaves
// decode_error_flags at 0 (verified empirically).
const bool corruptByFlags = (m_pFrame->decode_error_flags != 0);
const bool corruptByBridge = m_bCorruptSeen.exchange(false, std::memory_order_relaxed);
const bool frameCorrupt = corruptByFlags || corruptByBridge;
if (frameCorrupt) {
if (!m_bWaitKeyframe.load(std::memory_order_relaxed)) {
m_bWaitKeyframe.store(true, std::memory_order_relaxed);
m_nKeyframeWaitCount = 0;
log_print(HT_LOG_WARN,
"%s, corrupt frame (decode_error_flags=0x%x) -- gating until next keyframe\r\n",
__FUNCTION__, m_pFrame->decode_error_flags);
// Also to stderr so it appears next to FFmpeg's own [hevc @ ...]
// error lines (log_print goes to the log file, which unit tests
// and field debugging sessions usually don't have open).
fprintf(stderr, "[MediaClient] corrupt frame detected (flags=0x%x bridge=%d) -- gating until next keyframe\n",
m_pFrame->decode_error_flags, (int)corruptByBridge);
ANS_DBG("MEDIA_Decode",
"corrupt frame (decode_error_flags=0x%x) — re-arming keyframe gate",
m_pFrame->decode_error_flags);
} else {
// Count corrupt drops toward the safety valve so a stream that
// is corrupt for a long stretch eventually shows *something*
// (concealed frames) instead of freezing indefinitely.
++m_nKeyframeWaitCount;
}
if (m_nKeyframeWaitCount < 300) {
av_frame_unref(m_pFrame);
continue;
}
// Safety valve tripped: fall through and let the frame pass
// (concealment artifacts beat a permanently frozen display).
m_bWaitKeyframe.store(false, std::memory_order_relaxed);
}
// Startup keyframe gating — discard everything until the first IDR.
//
// On (re)connect we join the stream mid-GOP. Until a keyframe arrives,
// every frame references pictures we never received; the decoder (and
// NVDEC especially) conceals them into green / blocky garbage and reports
// success, so there is no error flag to test. Waiting for the first
// keyframe gives a clean intra reference, after which inter frames decode
// correctly. This is the standard RTSP "wait for IDR" behaviour.
//
// Keyframe flag is set from the bitstream parse and is reliable for both
// SW and HW decode (it does not depend on the HW surface). pict_type is a
// fallback. The counter is a safety valve: if a stream never advertises a
// keyframe within ~300 frames (~10 s @30fps) we stop gating rather than
// blank the display forever.
if (m_bWaitKeyframe.load(std::memory_order_relaxed)) {
// pict_type==I is version-independent and set for HEVC IDR/I frames
// on both SW and HW decode. AV_FRAME_FLAG_KEY (FFmpeg 5.1+) is added
// when available for robustness; guarded so it also builds against
// the older vendored headers that predate that flag.
bool isKey = (m_pFrame->pict_type == AV_PICTURE_TYPE_I);
#if defined(AV_FRAME_FLAG_KEY)
isKey = isKey || ((m_pFrame->flags & AV_FRAME_FLAG_KEY) != 0);
#endif
if (isKey) {
m_bWaitKeyframe.store(false, std::memory_order_relaxed);
ANS_DBG("MEDIA_Decode",
"keyframe acquired after %d dropped frame(s) — resuming output",
m_nKeyframeWaitCount);
if (m_nKeyframeWaitCount > 0) {
fprintf(stderr, "[MediaClient] keyframe acquired after %d dropped frame(s) -- resuming\n",
m_nKeyframeWaitCount);
}
} else if (++m_nKeyframeWaitCount >= 300) {
m_bWaitKeyframe.store(false, std::memory_order_relaxed);
ANS_DBG("MEDIA_Decode",
"no keyframe after %d frames — releasing gate (safety valve)",
m_nKeyframeWaitCount);
} else {
// Pre-IDR frame: drop it (would render as green/blocky garbage).
av_frame_unref(m_pFrame);
continue;
}
}
if (m_pFrame->format == m_hwPixFmt)
{
// CUDA HW accel: clone the HW frame BEFORE transfer so inference
+13
View File
@@ -165,6 +165,19 @@ private:
std::atomic<BOOL> m_bRunning;
BOOL m_bHardwareDecoderEnabled; // Track if hardware decoder is enabled
bool m_bCudaHWAccel; // true when using AV_HWDEVICE_TYPE_CUDA
// Startup keyframe gating. On connect/reconnect we join the stream mid-GOP,
// before the first IDR, so early frames reference data we never received and
// the decoder conceals them (green / blocky macroblocks). We drop everything
// until the first keyframe (clean reference point), then deliver normally.
// The frame counter is a safety valve so a stream that never advertises a
// keyframe can't blank the display forever.
std::atomic<bool> m_bWaitKeyframe{ true }; // true until first IDR after (re)start
int m_nKeyframeWaitCount{ 0 }; // frames dropped while waiting
// Latched by the av_log bridge (video_decoder.cpp) when FFmpeg logs a decode
// error for this decoder's AVCodecContext. Needed because hwaccel (NVDEC)
// decode does NOT set AVFrame::decode_error_flags on slice loss — the log
// is the only corruption signal on that path. Consumed by readFrame().
std::atomic<bool> m_bCorruptSeen{ false };
int m_hwGpuIndex; // GPU index assigned by HWDecoderPool (-1 = legacy)
AVFrame* m_pCudaHWFrame; // Cloned CUDA HW frame (device ptrs) for inference
const AVCodec* m_pCodec;
+82 -3
View File
@@ -1405,6 +1405,80 @@ cv::Mat CVideoPlayer::avframeNV12ToCvMat(const AVFrame* frame)
m_nv12OrigWidth = width;
m_nv12OrigHeight = height;
// Preferred path: libyuv NV12→RGB24 (BGR in memory, matches cv::Mat).
// SIMD (SSSE3/AVX2) and ~3-5x faster than sws_scale's quality config.
// Crucially it honors src_stride_uv, so the padded strides/height that
// NVDEC and D3D11VA emit (e.g. 1080→1088 rows, aligned strides) convert
// correctly — that padding is what cv::cvtColorTwoPlane mishandled,
// reading the chroma plane at the wrong offset and rendering a green
// cast with striped noise toward the bottom of the frame.
// Coefficients are chosen from the frame's signalled colorspace/range,
// mirroring initNV12SwsContext() (BT.709 for HD/4K, BT.601 for SD,
// BT.2020 if tagged). The constants map studio-range YUV to full-range
// BGR, so the result is display-ready — no manual range expansion.
#if defined(ANSCORE_HAS_LIBYUV) && ANSCORE_HAS_LIBYUV
{
const bool fullRange = (frame->color_range == AVCOL_RANGE_JPEG);
const libyuv::YuvConstants* yuvc;
switch (frame->colorspace) {
case AVCOL_SPC_BT470BG:
case AVCOL_SPC_SMPTE170M:
yuvc = fullRange ? &libyuv::kYuvJPEGConstants : &libyuv::kYuvI601Constants;
break;
case AVCOL_SPC_BT2020_NCL:
case AVCOL_SPC_BT2020_CL:
yuvc = fullRange ? &libyuv::kYuvV2020Constants : &libyuv::kYuv2020Constants;
break;
case AVCOL_SPC_BT709:
yuvc = fullRange ? &libyuv::kYuvF709Constants : &libyuv::kYuvH709Constants;
break;
default:
// Auto-detect by resolution (matches initNV12SwsContext()).
if (width >= 1280 || height >= 720)
yuvc = fullRange ? &libyuv::kYuvF709Constants : &libyuv::kYuvH709Constants;
else
yuvc = fullRange ? &libyuv::kYuvJPEGConstants : &libyuv::kYuvI601Constants;
break;
}
cv::Mat bgrImage(height, width, CV_8UC3);
int ret = libyuv::NV12ToRGB24Matrix(
frame->data[0], frame->linesize[0],
frame->data[1], frame->linesize[1],
bgrImage.data, static_cast<int>(bgrImage.step),
yuvc, width, height);
if (ret == 0) {
return bgrImage;
}
ANS_DBG("MEDIA_Convert",
"libyuv NV12ToRGB24 failed (ret=%d) — falling back to sws_scale", ret);
}
#endif
// Fallback path: sws_scale via the dedicated NV12→BGR24 context. Also
// stride-safe (honors frame->linesize[]) and colorspace/range-correct;
// used on non-libyuv builds or if libyuv ever reports an error.
{
// Dedicated lock (not _mutex) so close()/dtor cannot free the context
// mid-scale, while still keeping onVideoFrame off the hot _mutex.
std::lock_guard<std::mutex> nv12Lock(m_nv12SwsMutex);
initNV12SwsContext(frame);
if (m_nv12SwsCtx) {
cv::Mat bgrImage(height, width, CV_8UC3);
uint8_t* dst[1] = { bgrImage.data };
int dstStride[1] = { static_cast<int>(bgrImage.step[0]) };
int ret = sws_scale(m_nv12SwsCtx, frame->data, frame->linesize, 0,
height, dst, dstStride);
if (ret > 0) {
return bgrImage;
}
ANS_DBG("MEDIA_Convert",
"NV12 sws_scale failed (ret=%d) — falling back to cvtColorTwoPlane", ret);
}
}
// Fallback: OpenCV two-plane conversion. Correct only when the planes
// are unpadded (linesize == width); kept for builds/inputs where the
// sws context could not be created.
cv::Mat yPlane(height, width, CV_8UC1, frame->data[0], frame->linesize[0]);
cv::Mat uvPlane(height / 2, width / 2, CV_8UC2, frame->data[1], frame->linesize[1]);
@@ -2200,9 +2274,14 @@ CVideoPlayer::~CVideoPlayer()
sws_freeContext(swsCtx);
swsCtx = nullptr;
}
if (m_nv12SwsCtx != nullptr) {
sws_freeContext(m_nv12SwsCtx);
m_nv12SwsCtx = nullptr;
{
// Lock order: _mutex (held) → m_nv12SwsMutex. Protects against a
// consumer thread still inside avframeNV12ToCvMat's sws_scale.
std::lock_guard<std::mutex> nv12Lock(m_nv12SwsMutex);
if (m_nv12SwsCtx != nullptr) {
sws_freeContext(m_nv12SwsCtx);
m_nv12SwsCtx = nullptr;
}
}
if (this->_tjInstance) {
tjDestroy(this->_tjInstance);
+7
View File
@@ -428,6 +428,13 @@ protected:
int m_nv12LastWidth = 0, m_nv12LastHeight = 0;
int m_nv12LastColorspace = -1;
int m_nv12LastRange = -1;
// Guards m_nv12SwsCtx create/use/free. The NV12→BGR conversion deliberately
// runs WITHOUT _mutex (see avframeToCvMat, commit "Fix RTSP stall by
// releasing _mutex during BGR conversion"), so this dedicated lock protects
// the context against close()/dtor freeing it mid-sws_scale without
// reintroducing contention with onVideoFrame. Lock order: _mutex → this
// (close()/dtor hold _mutex first); avframeNV12ToCvMat takes only this lock.
std::mutex m_nv12SwsMutex;
void initSwsContext(int width, int height, AVPixelFormat pixFmt, AVPixelFormat outputPixFmt = AV_PIX_FMT_YUVJ420P);
void initNV12SwsContext(const AVFrame* frame);
+22 -7
View File
@@ -197,16 +197,31 @@ BOOL h264_data_rx(H264RXI * p_rxi, uint8 * p_data, int len)
if (p_rxi->rtprxi.rxf_loss)
{
// Packet loss, until the next start packet
if (!beginPacket)
// Packet loss — resume ONLY at an access-unit boundary (see the
// H.265 counterpart in h265_rtp_rx.cpp for the full rationale).
// Resuming at a mere NAL/FU start delivers frames missing their
// leading slices on multi-slice streams. An AU boundary is a
// non-VCL NAL (SEI/SPS/PPS/AUD, type >= 6) or a VCL slice whose
// first_mb_in_slice == 0 — ue(v)-coded, so value 0 is a single
// '1' bit: MSB of the byte after the 1-byte NAL header.
BOOL auStart = FALSE;
if (beginPacket)
{
uint8* payload = headerStart + numBytesToSkip;
int payloadLen = (int)packetSize - (int)numBytesToSkip;
if (payloadLen >= 2)
{
uint8 nt = payload[0] & 0x1F;
if (nt >= 6) auStart = TRUE; // non-VCL
else if (payload[1] & 0x80) auStart = TRUE; // first_mb_in_slice == 0
}
}
if (!auStart)
{
p_rxi->d_offset = 0;
return FALSE;
}
else
{
p_rxi->rtprxi.rxf_loss = 0;
}
p_rxi->rtprxi.rxf_loss = 0;
}
// save the H264 parameter sets
+26 -7
View File
@@ -265,16 +265,35 @@ BOOL h265_data_rx(H265RXI * p_rxi, uint8 * p_data, int len)
if (p_rxi->rtprxi.rxf_loss)
{
// Packet loss, until the next start packet
if (!beginPacket)
// Packet loss — resume ONLY at an access-unit boundary.
//
// The old behaviour resumed at the next NAL/FU start, but with
// multi-slice frames every slice starts a NAL, so reassembly resumed
// MID-FRAME and delivered access units missing their leading slices.
// The decoder then logs "First slice in a frame missing" / "Ignoring
// POC change between slices" and conceals the damage as green/blocky
// garbage. An AU boundary is either a non-VCL NAL (VPS/SPS/PPS/SEI,
// type >= 32, which precede an IDR) or a VCL slice with
// first_slice_segment_in_pic_flag set — the first bit after the
// 2-byte NAL header (H.265 spec 7.3.6.1).
BOOL auStart = FALSE;
if (beginPacket)
{
uint8* payload = headerStart + numBytesToSkip;
int payloadLen = (int)packetSize - (int)numBytesToSkip;
if (payloadLen >= 3)
{
uint8 nt = (payload[0] >> 1) & 0x3F;
if (nt >= 32) auStart = TRUE; // non-VCL (param sets/SEI)
else if (payload[2] & 0x80) auStart = TRUE; // first slice in picture
}
}
if (!auStart)
{
p_rxi->d_offset = 0;
return FALSE;
}
else
{
p_rxi->rtprxi.rxf_loss = 0;
}
p_rxi->rtprxi.rxf_loss = 0;
}
// save the H265 parameter sets
+7 -2
View File
@@ -105,8 +105,13 @@ BOOL rtp_data_rx(RTPRXI * p_rxi, uint8 * p_data, int len)
if (p_rxi->seq && p_rxi->seq != (uint16)(rtpSeq - 1))
{
p_rxi->rxf_loss = 1;
log_print(HT_LOG_WARN, "%s, prev seq[%u], cur seq[%u]!!!\r\n", __FUNCTION__, p_rxi->seq, rtpSeq);
}
log_print(HT_LOG_WARN, "%s, prev seq[%u], cur seq[%u]!!!\r\n", __FUNCTION__, p_rxi->seq, rtpSeq);
// stderr too (log_print goes to the log file): over TCP transport a
// sequence gap means the SENDER (camera) skipped packets — usually
// backpressure from a slow receiver. Gap size shows how much vanished.
fprintf(stderr, "[RTP] seq gap: prev=%u cur=%u (lost ~%u pkt)\n",
p_rxi->seq, rtpSeq, (uint16)(rtpSeq - p_rxi->seq - 1));
}
p_rxi->seq = rtpSeq;
p_rxi->rxf_marker = rtpMarkerBit;
+55 -3
View File
@@ -78,6 +78,30 @@ else()
endif()
target_compile_definitions(onnxruntime INTERFACE ORT_API_MANUAL_INIT)
# ── cuDNN ───────────────────────────────────────────────────────
# TensorRT API headers (engines/TensorRTAPI) include <cudnn_version.h>, so
# cuDNN's include dir must be on the path. cuDNN ships separately from the
# CUDA toolkit, so wire it explicitly rather than relying on it being copied
# into the CUDA install.
if(NOT APPLE)
if(WIN32)
set(CUDNN_INCLUDE_DIR "${ANSLIBS_DIR}/cudnn/include")
set(CUDNN_LIB_DIR "${ANSLIBS_DIR}/cudnn/lib/x64")
else()
set(CUDNN_INCLUDE_DIR "${ANSLIBS_DIR}/cudnn/include")
set(CUDNN_LIB_DIR "${ANSLIBS_DIR}/cudnn/lib")
endif()
add_library(cudnn INTERFACE)
target_include_directories(cudnn INTERFACE ${CUDNN_INCLUDE_DIR})
target_link_directories(cudnn INTERFACE ${CUDNN_LIB_DIR})
if(WIN32)
target_link_libraries(cudnn INTERFACE cudnn.lib)
else()
target_link_libraries(cudnn INTERFACE cudnn)
endif()
message(STATUS "cuDNN: using ANSLibs at ${CUDNN_INCLUDE_DIR}")
endif()
# ── TensorRT (Windows + Linux only) ────────────────────────────
if(NOT APPLE)
set(TENSORRT_INCLUDE_DIR "${ANSLIBS_DIR}/TensorRT/include")
@@ -86,6 +110,9 @@ if(NOT APPLE)
add_library(tensorrt INTERFACE)
target_include_directories(tensorrt INTERFACE ${TENSORRT_INCLUDE_DIR})
target_link_directories(tensorrt INTERFACE ${TENSORRT_LIB_DIR})
# cuDNN is a transitive dependency of the TensorRT API headers/engine,
# so anything linking `tensorrt` also gets cuDNN's headers and import lib.
target_link_libraries(tensorrt INTERFACE cudnn)
if(WIN32)
target_link_libraries(tensorrt INTERFACE nvinfer_plugin_10.lib)
else()
@@ -211,9 +238,34 @@ endif()
# ── Intel MKL ───────────────────────────────────────────────────
add_library(mkl INTERFACE)
if(WIN32)
set(MKL_LIB_DIR "C:/Program Files (x86)/Intel/oneAPI/mkl/2025.0/lib")
target_link_directories(mkl INTERFACE "${MKL_LIB_DIR}")
target_link_libraries(mkl INTERFACE mkl_rt.lib)
# oneAPI installs under .../oneAPI/mkl/<version>/ where <version> varies
# by install (e.g. 2025.0, 2025.3, or a "latest" junction) and the import
# lib lives in either lib/ (newer flat layout) or lib/intel64/ (older).
# Auto-detect rather than hardcoding so it survives MKL version bumps.
set(MKL_LIB_DIR "")
file(GLOB _mkl_versions "C:/Program Files (x86)/Intel/oneAPI/mkl/*")
list(SORT _mkl_versions)
list(REVERSE _mkl_versions) # prefer the newest version dir
foreach(_mkl_root IN LISTS _mkl_versions)
foreach(_sub "lib" "lib/intel64")
if(EXISTS "${_mkl_root}/${_sub}/mkl_rt.lib")
set(MKL_LIB_DIR "${_mkl_root}/${_sub}")
break()
endif()
endforeach()
if(MKL_LIB_DIR)
break()
endif()
endforeach()
if(MKL_LIB_DIR)
target_link_directories(mkl INTERFACE "${MKL_LIB_DIR}")
target_link_libraries(mkl INTERFACE mkl_rt.lib)
message(STATUS "Intel MKL: using ${MKL_LIB_DIR}")
else()
message(WARNING "Intel MKL not found under C:/Program Files (x86)/Intel/oneAPI/mkl. "
"ANSFR (and ANSLIB/ANSFR-UnitTest that link it) will fail to link. "
"Install Intel oneMKL or adjust MKL_LIB_DIR in cmake/Dependencies.cmake.")
endif()
else()
# Try system MKL on Linux
find_library(MKL_RT_LIB mkl_rt PATHS /opt/intel/oneapi/mkl/latest/lib/intel64)
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -461,7 +461,7 @@ namespace ANSCENTER {
") returned null — the DLL may be corrupt.");
s_ort_api = api;
Ort::Global<void>::api_ = api;
Ort::InitApi(api);
std::cout << "[EPLoader] ORT loaded successfully." << std::endl;
std::cout << "[EPLoader] ORT DLL version : " << base->GetVersionString() << std::endl;
+235 -45
View File
@@ -98,6 +98,16 @@ namespace {
return s.substr(0, 4) + std::string(s.size() - 8, '*') + s.substr(s.size() - 4);
}
// The HWID emitted when WMI returns no hardware components at all (every
// hash field is the "absent" marker). Every PC with broken WMI produces
// this exact string, so it identifies "WMI broken" in customer logs.
constexpr const char* kEmptyFingerprintHWID = "ESWJJ-LEEFC-CCTBB-JSSWJ-JLEEF";
constexpr const char* kEmptyFingerprintWarning =
"Hardware fingerprint is EMPTY - WMI returned no hardware components on this PC. "
"Licensing proceeds but the license is weakly bound to this machine. "
"Repair WMI: run 'winmgmt /verifyrepository' (then 'winmgmt /salvagerepository' if inconsistent).";
// SDK identifier passed to ANSLSHelper for licensing-service auth. Despite
// the variable name "privateKey" used at call sites, this is an 11-byte
// SDK token that identifies the ANSCENTER product to the licensing
@@ -690,6 +700,7 @@ struct GpuAdapter {
DWORD vendorId{0};
std::string name; // UTF-8
int64_t dedicatedVramMB{0};
int64_t sharedSysMemMB{0}; // iGPUs report ~0 dedicated; this is their pool
bool isDiscrete{false}; // heuristic: dedicated VRAM ≥ 1 GiB
// running totals — reset every Sample()
@@ -697,6 +708,19 @@ struct GpuAdapter {
int64_t processMemMB{0};
};
// Vendor name in the spelling OpenCL uses, for adapters OpenCL did not
// report. Callers that string-match the JSON "vendor" field see the same
// text whichever source filled it in.
const char* OpenClStyleVendor(DWORD vendorId) {
switch (vendorId) {
case 0x10DE: return "NVIDIA";
case 0x8086: return "Intel Corporation";
case 0x1002: // fallthrough — AMD and some AMD APUs
case 0x1022: return "Advanced Micro Devices, Inc.";
default: return "";
}
}
const char* VendorString(DWORD vendorId) {
switch (vendorId) {
case 0x10DE: return "NVIDIA";
@@ -737,6 +761,7 @@ std::vector<GpuAdapter> EnumerateGpuAdapters() {
a.vendorId = desc.VendorId;
a.name = WideToUtf8(desc.Description);
a.dedicatedVramMB = static_cast<int64_t>(desc.DedicatedVideoMemory / (1024ULL * 1024ULL));
a.sharedSysMemMB = static_cast<int64_t>(desc.SharedSystemMemory / (1024ULL * 1024ULL));
a.isDiscrete = a.dedicatedVramMB >= 1024; // ≥ 1 GiB ⇒ probably dGPU
out.push_back(std::move(a));
}
@@ -747,6 +772,66 @@ std::vector<GpuAdapter> EnumerateGpuAdapters() {
return out;
}
// Driver version for a DXGI adapter.
//
// DXGI does not expose it, and WMI (Win32_VideoController) costs a COM/WMI
// round trip. The display-adapter class key has it: every installed adapter
// gets a numbered subkey holding DriverDesc — the Device Manager name, which
// is the same text DXGI returns as Description — plus DriverVersion.
// Returns "" when the adapter has no match (rare: a driver installed after
// the key was enumerated, or a name mismatch), which is what the JSON then
// reports rather than a fabricated value.
std::string GpuDriverVersion(const std::string& description) {
if (description.empty()) return {};
static const wchar_t* kDisplayClassKey =
L"SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e968-e325-11ce-bfc1-08002be10318}";
HKEY classKey = nullptr;
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, kDisplayClassKey, 0,
KEY_READ, &classKey) != ERROR_SUCCESS) {
ANS_DBG("HWInfo", "GpuDriverVersion: cannot open display class key");
return {};
}
std::string version;
constexpr DWORD kSubNameChars = 256;
wchar_t subName[kSubNameChars];
for (DWORD i = 0; version.empty(); ++i) {
DWORD subLen = kSubNameChars;
if (RegEnumKeyExW(classKey, i, subName, &subLen,
nullptr, nullptr, nullptr, nullptr) != ERROR_SUCCESS)
break; // ERROR_NO_MORE_ITEMS — done
HKEY adapterKey = nullptr;
if (RegOpenKeyExW(classKey, subName, 0, KEY_READ, &adapterKey) != ERROR_SUCCESS)
continue;
auto readString = [&adapterKey](const wchar_t* value) -> std::string {
constexpr DWORD kBufChars = 512;
wchar_t buf[kBufChars];
DWORD len = sizeof(buf);
DWORD type = 0;
if (RegQueryValueExW(adapterKey, value, nullptr, &type,
reinterpret_cast<LPBYTE>(buf), &len) != ERROR_SUCCESS ||
type != REG_SZ)
return {};
// RegQueryValueExW does not guarantee NUL termination.
const DWORD chars = len / sizeof(wchar_t);
buf[chars < kBufChars ? chars : kBufChars - 1] = L'\0';
return WideToUtf8(buf);
};
if (readString(L"DriverDesc") == description)
version = readString(L"DriverVersion");
RegCloseKey(adapterKey);
}
RegCloseKey(classKey);
return version;
}
bool ParseLuidFromInstance(const wchar_t* inst, LUID& out) {
if (!inst) return false;
const wchar_t* p = wcsstr(inst, L"luid_0x");
@@ -1361,32 +1446,67 @@ namespace ANSCENTER
}
// ----------------------------------------------------------------
// GPU — classify all adapters by vendor string
// GPU — classify all adapters by PCI vendor ID.
//
// Enumerated over DXGI, not OpenCL. hwinfo::getAllGPUs() is an
// OpenCL device list (the prebuilt HWiNFO.lib has no
// Win32_VideoController path), so it reports nothing on a machine
// whose driver did not register an OpenCL ICD — and we would then
// silently drop a perfectly good CUDA card down to the CPU engine.
// DXGI only needs a WDDM display driver, which any usable GPU has.
//
// Vendor IDs are exact, so no substring matching and none of its
// traps (e.g. "ATI" matching inside "CORPOR-ATI-ON").
// ----------------------------------------------------------------
auto gpus = hwinfo::getAllGPUs();
ANS_DBG("HWInfo", "Found %zu GPU adapter(s)", gpus.size());
for (auto& gpu : gpus) {
std::string vendor = gpu.vendor();
ANS_DBG("HWInfo", "GPU: name=%s vendor=%s", gpu.name().c_str(), vendor.c_str());
std::cout << "[HWInfo] Detected GPU: " << gpu.name() << " (Vendor: " << vendor << ")" << std::endl;
// Normalize to uppercase for reliable substring matching
std::transform(vendor.begin(), vendor.end(),
vendor.begin(), ::toupper);
auto adapters = EnumerateGpuAdapters();
ANS_DBG("HWInfo", "DXGI: found %zu GPU adapter(s)", adapters.size());
for (const auto& a : adapters) {
ANS_DBG("HWInfo", "GPU: name=%s vendorId=0x%04X vram=%lldMiB discrete=%d",
a.name.c_str(), a.vendorId,
static_cast<long long>(a.dedicatedVramMB), a.isDiscrete ? 1 : 0);
std::cout << "[HWInfo] Detected GPU: " << a.name
<< " (Vendor: " << VendorString(a.vendorId) << ")" << std::endl;
if (vendor.find("NVIDIA") != std::string::npos)
gpuNvidiaAvailable = true;
if (vendor.find("INTEL") != std::string::npos)
gpuOpenVINOAvailable = true;
// AMD cards report as "AMD", "Advanced Micro Devices", or
// legacy "ATI Technologies" branding.
// NOTE: "ATI" alone is too broad — "INTEL CORPORATION" contains
// "ATI" inside "CORPOR-ATI-ON", causing a false positive.
// Use "ATI " (with trailing space) to match "ATI Technologies"
// or "ATI Radeon" without matching "CORPORATION".
if (vendor.find("AMD") != std::string::npos ||
vendor.find("ADVANCED MICRO") != std::string::npos ||
vendor.find("ATI ") != std::string::npos)
gpuAMDAvailable = true;
switch (a.vendorId) {
case 0x10DE: gpuNvidiaAvailable = true; break; // NVIDIA
case 0x1002: gpuAMDAvailable = true; break; // AMD / ATI
case 0x1022: gpuAMDAvailable = true; break; // AMD (some APUs)
case 0x8086: gpuOpenVINOAvailable = true; break; // Intel
default: break;
}
}
// DXGI came back empty — no WDDM driver at all, or a session with no
// graphics stack. Fall back to the OpenCL list rather than assuming
// there is no GPU.
if (adapters.empty()) {
auto gpus = hwinfo::getAllGPUs();
ANS_DBG("HWInfo", "DXGI empty; OpenCL fallback found %zu device(s)", gpus.size());
for (auto& gpu : gpus) {
std::string vendor = gpu.vendor();
ANS_DBG("HWInfo", "OpenCL GPU: name=%s vendor=%s",
gpu.name().c_str(), vendor.c_str());
std::cout << "[HWInfo] Detected GPU (OpenCL): " << gpu.name()
<< " (Vendor: " << vendor << ")" << std::endl;
// Normalize to uppercase for reliable substring matching
std::transform(vendor.begin(), vendor.end(),
vendor.begin(), ::toupper);
if (vendor.find("NVIDIA") != std::string::npos)
gpuNvidiaAvailable = true;
if (vendor.find("INTEL") != std::string::npos)
gpuOpenVINOAvailable = true;
// AMD cards report as "AMD", "Advanced Micro Devices", or
// legacy "ATI Technologies" branding.
// NOTE: "ATI" alone is too broad — "INTEL CORPORATION" contains
// "ATI" inside "CORPOR-ATI-ON", causing a false positive.
// Use "ATI " (with trailing space) to match "ATI Technologies"
// or "ATI Radeon" without matching "CORPORATION".
if (vendor.find("AMD") != std::string::npos ||
vendor.find("ADVANCED MICRO") != std::string::npos ||
vendor.find("ATI ") != std::string::npos)
gpuAMDAvailable = true;
}
}
ANS_DBG("HWInfo", "Detection: cpuIntel=%d gpuNvidia=%d gpuAMD=%d gpuOpenVINO=%d",
cpuIntelAvailable, gpuNvidiaAvailable, gpuAMDAvailable, gpuOpenVINOAvailable);
@@ -1427,6 +1547,27 @@ namespace ANSCENTER
return str.c_str();
}
// Static, handle-free hardware id. Mirrors ANSLSHelper::GetCurrentHardwareId but has no
// instance/_logger, so diagnostics go to DebugView via ANS_DBG. Returns "" on failure.
std::string ANSLicenseHelper::GetHardwareId() {
try {
const wchar_t* hwid = KeyHelper::GetCurrentHardwareId();
std::wstring result_t = std::wstring(hwid);
std::string result = WString2String(result_t);
ANS_DBG("ANSLicenseHelper", "GetHardwareId -> '%s'", result.c_str());
return result;
}
catch (ANSCENTER::Licensing::Exception* ex) {
ANS_DBG("ANSLicenseHelper", "GetHardwareId failed: %s", ex->GetExceptionMessage());
ex->Destroy();
return "";
}
catch (...) {
ANS_DBG("ANSLicenseHelper", "GetHardwareId failed: unknown exception");
return "";
}
}
bool ANSLicenseHelper::isPathSafe(const std::string& pathStr) {
// Basic safety checks - customize as needed
if (pathStr.empty()) return false;
@@ -2145,7 +2286,7 @@ namespace ANSCENTER
}
void ANSLSHelper::LoadLicense(std::string licenseKey) {
if (!this->_license != NULL) delete this->_license;
if (this->_license != NULL) delete this->_license;
this->_license = new License();
std::string licenseFileName = Dash2Underscore(licenseKey)+".json";
std::string licenseFolder = GetLicenseDir();
@@ -2791,6 +2932,9 @@ namespace ANSCENTER
std::wstring _registrationName = String2WString(registrationName);
std::wstring hardwareId = KeyHelper::GetCurrentHardwareId();
std::string _hardwareId = WString2String(hardwareId);
if (_hardwareId == kEmptyFingerprintHWID) {
this->_logger.LogWarn("ANSLSHelper::ValidateLicenseKey", kEmptyFingerprintWarning, __FILE__, __LINE__);
}
bool _overrideLicenseFile = false;
if (activationKey.empty()) {
if (this->_license != NULL) {// get it from there
@@ -2867,14 +3011,13 @@ namespace ANSCENTER
case STATUS_INVALID_HARDWARE_ID:
_licenseKeyStatus = INVALID_HARDWARE_ID;
activationStatusStr = "Hardware id does not match this computer. current HWID is " + _hardwareId +
"; client HWID is " + WString2String(licensingClient.GetHardwareId()) +
"; Current activation key is " + WString2String(_activationKey) +
"; client Activation key is " + WString2String(licensingClient.GetActivationKey()) +
"; URL is " + WString2String(this->_licenseServiceURL.c_str())+
activationStatusStr = "Local hardware fingerprint check rejected this computer (hardware changed, or WMI returned no components). Current HWID is " + _hardwareId +
"; stored activation key is " + mask_secret(WString2String(_activationKey)) +
"; server activation key is " + mask_secret(WString2String(licensingClient.GetActivationKey())) +
"; URL is " + WString2String(this->_licenseServiceURL.c_str())+
"; ProductId is " + WString2String(wProductId) +
"; RegistrationName is " + WString2String(_registrationName) +
"; Status is" + std::to_string(activationStatus);
"; Status is " + std::to_string(activationStatus);
break;
case STATUS_LICENSE_EXPIRED:
@@ -2948,7 +3091,7 @@ namespace ANSCENTER
if (_overrideLicenseFile) {
this->_logger.LogDebug("ANSLSHelper::ValidateLicenseKey", "Override license key.", __FILE__, __LINE__);
if (!this->_license != NULL) delete this->_license;
if (this->_license != NULL) delete this->_license;
this->_license = new License();
this->_license->SetLicenseKey(_licenseKey.c_str());
this->_license->SetLicenseKeyValidationData(validationData, validationDataLen);
@@ -2975,7 +3118,7 @@ namespace ANSCENTER
if (_overrideLicenseFile) {
// Save license
this->_logger.LogDebug("ANSLSHelper::ValidateLicenseKey", "Override license key.", __FILE__, __LINE__);
if (!this->_license != NULL) delete this->_license;
if (this->_license != NULL) delete this->_license;
this->_license = new License();
this->_license->SetLicenseKey(_licenseKey.c_str());
this->_license->SetLicenseKeyValidationData(validationData, validationDataLen);
@@ -3005,6 +3148,9 @@ namespace ANSCENTER
std::wstring _registrationName = String2WString(registrationName);
std::string _hardwareId = customHWID;
std::wstring hardwareId = String2WString(customHWID);
if (_hardwareId == kEmptyFingerprintHWID) {
this->_logger.LogWarn("ANSLSHelper::ValidateLicenseWithCustomHWID", kEmptyFingerprintWarning, __FILE__, __LINE__);
}
bool _overrideLicenseFile = false;
if (activationKey.empty()) {
if (this->_license != NULL) {// get it from there
@@ -3081,14 +3227,13 @@ namespace ANSCENTER
case STATUS_INVALID_HARDWARE_ID:
_licenseKeyStatus = INVALID_HARDWARE_ID;
activationStatusStr = "Hardware id does not match this computer. current HWID is " + _hardwareId +
"; client HWID is " + WString2String(licensingClient.GetHardwareId()) +
"; Current activation key is " + WString2String(_activationKey) +
"; client Activation key is " + WString2String(licensingClient.GetActivationKey()) +
activationStatusStr = "Local hardware fingerprint check rejected this computer (hardware changed, or WMI returned no components). Current HWID is " + _hardwareId +
"; stored activation key is " + mask_secret(WString2String(_activationKey)) +
"; server activation key is " + mask_secret(WString2String(licensingClient.GetActivationKey())) +
"; URL is " + WString2String(this->_licenseServiceURL.c_str()) +
"; ProductId is " + WString2String(wProductId) +
"; RegistrationName is " + WString2String(_registrationName) +
"; Status is" + std::to_string(activationStatus);
"; Status is " + std::to_string(activationStatus);
break;
case STATUS_LICENSE_EXPIRED:
@@ -3162,7 +3307,7 @@ namespace ANSCENTER
if (_overrideLicenseFile) {
this->_logger.LogDebug("ANSLSHelper::ValidateLicenseKey", "Override license key.", __FILE__, __LINE__);
if (!this->_license != NULL) delete this->_license;
if (this->_license != NULL) delete this->_license;
this->_license = new License();
this->_license->SetLicenseKey(_licenseKey.c_str());
this->_license->SetLicenseKeyValidationData(validationData, validationDataLen);
@@ -3189,7 +3334,7 @@ namespace ANSCENTER
if (_overrideLicenseFile) {
// Save license
this->_logger.LogDebug("ANSLSHelper::ValidateLicenseKey", "Override license key.", __FILE__, __LINE__);
if (!this->_license != NULL) delete this->_license;
if (this->_license != NULL) delete this->_license;
this->_license = new License();
this->_license->SetLicenseKey(_licenseKey.c_str());
this->_license->SetLicenseKeyValidationData(validationData, validationDataLen);
@@ -3475,7 +3620,7 @@ namespace ANSCENTER
return 0; // invalid
}
else {
if (!this->_license != NULL) delete this->_license;
if (this->_license != NULL) delete this->_license;
this->_license = new License();
this->_license->SetLicenseKey(_licenseKey.c_str());
this->_license->SetLicenseKeyValidationData(validationData, validationDataLen);
@@ -3852,9 +3997,25 @@ namespace ANSCENTER
osNode.put("endianess", os.isLittleEndian() ? "little endian" : "big endian");
root.add_child("OS", osNode);
// GPU information
auto gpus = hwinfo::getAllGPUs();
for (auto& gpu : gpus) {
// GPU information.
//
// The OpenCL loop below is unchanged — same keys, same order,
// same values — so on a machine that already reported its GPUs
// the JSON is byte-identical to before.
//
// But hwinfo::getAllGPUs() *is* an OpenCL device list, and it
// reports nothing when no vendor registered an OpenCL ICD. That
// is how this field ended up as "" on machines that plainly have
// a GPU. So afterwards we append any DXGI adapter OpenCL did not
// already cover: DXGI needs only a WDDM display driver, so it
// sees the adapter either way. Nothing is appended when OpenCL
// works, which is why the identical case stays identical.
auto clDevices = hwinfo::getAllGPUs();
auto adapters = EnumerateGpuAdapters();
ANS_DBG("HWInfo", "GetSystemHardwareInformation: OpenCL %zu device(s), DXGI %zu adapter(s)",
clDevices.size(), adapters.size());
for (auto& gpu : clDevices) {
boost::property_tree::ptree gpuNodeChild;
gpuNodeChild.put("GPU", gpu.id());
gpuNodeChild.put("vendor", gpu.vendor());
@@ -3865,8 +4026,37 @@ namespace ANSCENTER
gpuNode.push_back(std::make_pair("", gpuNodeChild));
}
for (const auto& a : adapters) {
bool covered = false;
for (const auto& d : clDevices) {
if (d.name() == a.name) { covered = true; break; }
}
if (covered) continue;
ANS_DBG("HWInfo", "GPU not seen by OpenCL, adding from DXGI: name=%s vendorId=0x%04X",
a.name.c_str(), a.vendorId);
// Dedicated VRAM, or the shared pool for an iGPU. An iGPU
// carves out a token amount of dedicated memory (128 MiB is
// typical) and works out of system RAM, so reporting the
// carve-out would badly understate it.
const double memMiB = static_cast<double>(
a.dedicatedVramMB >= 512 ? a.dedicatedVramMB : a.sharedSysMemMB);
const char* vendor = OpenClStyleVendor(a.vendorId);
boost::property_tree::ptree gpuNodeChild;
gpuNodeChild.put("GPU", 0); // hwinfo never set a per-device id either
gpuNodeChild.put("vendor", std::string(*vendor ? vendor : VendorString(a.vendorId)));
gpuNodeChild.put("model", a.name);
gpuNodeChild.put("diver_version", GpuDriverVersion(a.name));
gpuNodeChild.put("memory[MiB]", memMiB);
// DXGI exposes no clock; 0 rather than a fabricated number.
gpuNodeChild.put("frequency", static_cast<int64_t>(0));
gpuNode.push_back(std::make_pair("", gpuNodeChild));
}
root.add_child("GPU", gpuNode);
// RAM information
hwinfo::RAM ram;
+11
View File
@@ -523,6 +523,10 @@ namespace ANSCENTER
[[nodiscard]] static bool isPathSafe(const std::string& pathStr);
public:
[[nodiscard]] static bool LicenseVerification(std::string licenseKey, int productId, std::string registrationName);
// Returns this machine's hardware id (same value as ANSLSHelper::GetCurrentHardwareId)
// but as a static call requiring no handle/instance — convenient for LabVIEW and other
// callers that just need the HWID. Returns "" on failure.
[[nodiscard]] static std::string GetHardwareId();
[[nodiscard]] static EngineType CheckHardwareInformation();
[[nodiscard]] static SystemHealthInfo GetSystemHealthInformation();
@@ -564,6 +568,13 @@ extern "C" __declspec(dllexport) void SetLicenseServiceURL(ANSCENTER::ANSLSHelpe
extern "C" __declspec(dllexport) int GetLicenseData(ANSCENTER::ANSLSHelper * *Handle, const char* licenseKey, LStrHandle licenseData);
extern "C" __declspec(dllexport) int LoadLicenseTemplate(ANSCENTER::ANSLSHelper * *Handle, const char* templateFilePath);
extern "C" __declspec(dllexport) int GetCurrentHardwareId(ANSCENTER::ANSLSHelper * *Handle, LStrHandle currentHwID);
// Handle-free static hardware id, two flavours:
// - GetHardwareId: LabVIEW native string handle (LStrHandle) output. Returns 1 on success, 0 on failure.
// - GetHardwareIdBuffer: caller-provided C buffer; works from plain C/C++ too (no LabVIEW manager).
// Writes a null-terminated string into 'buffer' (capacity 'bufferSize') and returns the number of
// characters written (excluding the null terminator), or 0 on failure / insufficient buffer.
extern "C" __declspec(dllexport) int GetHardwareId(LStrHandle currentHwID);
extern "C" __declspec(dllexport) int GetHardwareIdBuffer(char* buffer, int bufferSize);
extern "C" __declspec(dllexport) int GenerateKey(ANSCENTER::ANSLSHelper * *Handle, int productId, const char* registrationName, int productFeature, int trialLicense, LStrHandle generatedKey);
extern "C" __declspec(dllexport) int GenerateKeyWithExpiredDate(ANSCENTER::ANSLSHelper * *Handle, int productId, const char* registrationName, int productFeature, int expiredYear, int expiredMonth, int expiredDate, LStrHandle generatedKey);
extern "C" __declspec(dllexport) int CheckLicenseKey(ANSCENTER::ANSLSHelper * *Handle, const char* licenseKey, const char* registrationName, int productId);// , LStrHandle strEnableFeature); // Check if the license key is valid
+1 -1
View File
@@ -54,7 +54,7 @@ target_link_libraries(ANSLicensingSystem PRIVATE
shlwapi.lib winhttp.lib
pdh.lib psapi.lib
wbemuuid.lib oleaut32.lib ole32.lib
dxgi.lib
dxgi.lib advapi32.lib
dnsapi.lib
${WIN_COMMON_LIBS}
)
+37
View File
@@ -109,6 +109,43 @@ extern "C" __declspec(dllexport) int GetCurrentHardwareId(ANSCENTER::ANSLSHelper
return 0;
}
}
extern "C" __declspec(dllexport) int GetHardwareId(LStrHandle currentHwID) {
try {
std::string st = ANSCENTER::ANSLicenseHelper::GetHardwareId();
int size = (int)st.length();
if (size > 0) {
MgErr error;
error = DSSetHandleSize(currentHwID, sizeof(int32) + size * sizeof(uChar));
if (error == noErr)
{
(*currentHwID)->cnt = size;
memcpy((*currentHwID)->str, st.c_str(), size);
return 1;
}
else return 0;
}
else return 0;
}
catch (...) {
return 0;
}
}
extern "C" __declspec(dllexport) int GetHardwareIdBuffer(char* buffer, int bufferSize) {
try {
if (!buffer || bufferSize <= 0) return 0;
std::string st = ANSCENTER::ANSLicenseHelper::GetHardwareId();
int size = (int)st.length();
if (size <= 0) return 0;
// Need room for the string plus the null terminator.
if (size + 1 > bufferSize) return 0;
memcpy(buffer, st.c_str(), size);
buffer[size] = '\0';
return size;
}
catch (...) {
return 0;
}
}
extern "C" __declspec(dllexport) int GenerateKey(ANSCENTER::ANSLSHelper * *Handle, int productId, const char* registrationName, int productFeature, int trialLicense, LStrHandle generatedKey) {
try {
if (!Handle || !(*Handle) || !registrationName) return 0;
+314
View File
@@ -0,0 +1,314 @@
#ifndef ANSAEAD_H
#define ANSAEAD_H
#pragma once
// ANSAead — the AEAD the model package format is sealed with (RFC 8439).
//
// KEPT IN STEP WITH ANSMINDCORE's src/core/chacha20poly1305.hpp — the same
// algorithm, byte for byte, because a package written by one product must open
// in the other. Change them together or not at all.
//
// WHY IN-TREE, and why this cipher. A versioned crypto dependency on every
// platform we ship, for a primitive with published test vectors, is a worse
// trade than the algorithm itself. ChaCha20-Poly1305 is the one modern AEAD
// that is a few
// hundred lines of 32-bit arithmetic with no tables, no S-boxes and no
// timing-sensitive lookups — AES-GCM needs either hardware instructions or a
// GHASH that is easy to get subtly wrong. The suite checks this against the
// RFC's own vectors, so it is verified rather than merely present.
//
// NOT a general crypto library. It seals and opens model packages. The nonce
// rule is the caller's to keep: a key and nonce pair must never encrypt twice,
// which the package format satisfies by deriving the nonce from a random
// per-payload base and the chunk index.
#include <array>
#include <cstdint>
#include <cstring>
#include <string>
#include <vector>
namespace ANSCENTER {
namespace aead {
// ── ChaCha20 (RFC 8439 §2.4) ──────────────────────────────────────────────
namespace detail {
inline uint32_t rotl32(uint32_t v, int n) { return (v << n) | (v >> (32 - n)); }
inline uint32_t load32(const uint8_t* p) {
return uint32_t(p[0]) | (uint32_t(p[1]) << 8) | (uint32_t(p[2]) << 16) |
(uint32_t(p[3]) << 24);
}
inline void store32(uint8_t* p, uint32_t v) {
p[0] = uint8_t(v);
p[1] = uint8_t(v >> 8);
p[2] = uint8_t(v >> 16);
p[3] = uint8_t(v >> 24);
}
inline void chachaBlock(const uint8_t key[32], uint32_t counter,
const uint8_t nonce[12], uint8_t out[64]) {
static const char kSigma[] = "expand 32-byte k";
uint32_t s[16];
s[0] = load32(reinterpret_cast<const uint8_t*>(kSigma));
s[1] = load32(reinterpret_cast<const uint8_t*>(kSigma) + 4);
s[2] = load32(reinterpret_cast<const uint8_t*>(kSigma) + 8);
s[3] = load32(reinterpret_cast<const uint8_t*>(kSigma) + 12);
for (int i = 0; i < 8; ++i) s[4 + i] = load32(key + 4 * i);
s[12] = counter;
for (int i = 0; i < 3; ++i) s[13 + i] = load32(nonce + 4 * i);
uint32_t x[16];
std::memcpy(x, s, sizeof(x));
auto qr = [&x](int a, int b, int c, int d) {
x[a] += x[b]; x[d] = rotl32(x[d] ^ x[a], 16);
x[c] += x[d]; x[b] = rotl32(x[b] ^ x[c], 12);
x[a] += x[b]; x[d] = rotl32(x[d] ^ x[a], 8);
x[c] += x[d]; x[b] = rotl32(x[b] ^ x[c], 7);
};
for (int i = 0; i < 10; ++i) { // 20 rounds = 10 double rounds
qr(0, 4, 8, 12); qr(1, 5, 9, 13); qr(2, 6, 10, 14); qr(3, 7, 11, 15);
qr(0, 5, 10, 15); qr(1, 6, 11, 12); qr(2, 7, 8, 13); qr(3, 4, 9, 14);
}
for (int i = 0; i < 16; ++i) store32(out + 4 * i, x[i] + s[i]);
}
// XOR a buffer with the keystream, starting at `counter`.
inline void chacha20Xor(const uint8_t key[32], uint32_t counter,
const uint8_t nonce[12], const uint8_t* in, size_t n,
uint8_t* out) {
uint8_t block[64];
size_t done = 0;
while (done < n) {
chachaBlock(key, counter++, nonce, block);
const size_t take = (n - done < 64) ? (n - done) : 64;
for (size_t i = 0; i < take; ++i) out[done + i] = in[done + i] ^ block[i];
done += take;
}
}
// ── Poly1305 (RFC 8439 §2.5), 130-bit accumulator in 26-bit limbs ─────────
class Poly1305 {
public:
explicit Poly1305(const uint8_t key[32]) {
r_[0] = (load32(key) ) & 0x3ffffff;
r_[1] = (load32(key + 3) >> 2) & 0x3ffff03;
r_[2] = (load32(key + 6) >> 4) & 0x3ffc0ff;
r_[3] = (load32(key + 9) >> 6) & 0x3f03fff;
r_[4] = (load32(key + 12) >> 8) & 0x00fffff;
for (int i = 0; i < 4; ++i) pad_[i] = load32(key + 16 + 4 * i);
}
void update(const uint8_t* m, size_t n) {
while (n > 0) {
const size_t take = (16 - bufLen_ < n) ? (16 - bufLen_) : n;
std::memcpy(buf_ + bufLen_, m, take);
bufLen_ += take;
m += take;
n -= take;
if (bufLen_ == 16) {
block(buf_, false);
bufLen_ = 0;
}
}
}
void finish(uint8_t tag[16]) {
if (bufLen_ > 0) {
buf_[bufLen_++] = 1;
while (bufLen_ < 16) buf_[bufLen_++] = 0;
block(buf_, true);
}
// Carry propagation, then the conditional subtraction of p = 2^130-5.
uint32_t c;
c = h_[1] >> 26; h_[1] &= 0x3ffffff; h_[2] += c;
c = h_[2] >> 26; h_[2] &= 0x3ffffff; h_[3] += c;
c = h_[3] >> 26; h_[3] &= 0x3ffffff; h_[4] += c;
c = h_[4] >> 26; h_[4] &= 0x3ffffff; h_[0] += c * 5;
c = h_[0] >> 26; h_[0] &= 0x3ffffff; h_[1] += c;
uint32_t g[5];
g[0] = h_[0] + 5; c = g[0] >> 26; g[0] &= 0x3ffffff;
g[1] = h_[1] + c; c = g[1] >> 26; g[1] &= 0x3ffffff;
g[2] = h_[2] + c; c = g[2] >> 26; g[2] &= 0x3ffffff;
g[3] = h_[3] + c; c = g[3] >> 26; g[3] &= 0x3ffffff;
g[4] = h_[4] + c - (uint32_t(1) << 26);
uint32_t mask = (g[4] >> 31) - 1; // all ones when g >= p
for (int i = 0; i < 5; ++i) g[i] &= mask;
mask = ~mask;
for (int i = 0; i < 5; ++i) h_[i] = (h_[i] & mask) | g[i];
// h modulo 2^128, as four 32-bit words. The narrowing is deliberate: each
// word takes the low 32 bits of two limbs, and the only carry between
// words is the one the pad addition produces. Keeping the spilled limb
// bits in a 64-bit accumulator instead counts them twice — which is a
// tag that verifies against itself and against nothing else.
const uint32_t w0 = uint32_t((h_[0]) | (h_[1] << 26));
const uint32_t w1 = uint32_t((h_[1] >> 6) | (h_[2] << 20));
const uint32_t w2 = uint32_t((h_[2] >> 12) | (h_[3] << 14));
const uint32_t w3 = uint32_t((h_[3] >> 18) | (h_[4] << 8));
uint64_t f = uint64_t(w0) + pad_[0];
store32(tag, uint32_t(f));
f = uint64_t(w1) + pad_[1] + (f >> 32);
store32(tag + 4, uint32_t(f));
f = uint64_t(w2) + pad_[2] + (f >> 32);
store32(tag + 8, uint32_t(f));
f = uint64_t(w3) + pad_[3] + (f >> 32);
store32(tag + 12, uint32_t(f));
}
private:
void block(const uint8_t m[16], bool last) {
const uint32_t hibit = last ? 0 : (uint32_t(1) << 24);
h_[0] += (load32(m) ) & 0x3ffffff;
h_[1] += (load32(m + 3) >> 2) & 0x3ffffff;
h_[2] += (load32(m + 6) >> 4) & 0x3ffffff;
h_[3] += (load32(m + 9) >> 6) & 0x3ffffff;
h_[4] += (load32(m + 12) >> 8) | hibit;
const uint32_t s1 = r_[1] * 5, s2 = r_[2] * 5, s3 = r_[3] * 5, s4 = r_[4] * 5;
uint64_t d0 = uint64_t(h_[0]) * r_[0] + uint64_t(h_[1]) * s4 +
uint64_t(h_[2]) * s3 + uint64_t(h_[3]) * s2 +
uint64_t(h_[4]) * s1;
uint64_t d1 = uint64_t(h_[0]) * r_[1] + uint64_t(h_[1]) * r_[0] +
uint64_t(h_[2]) * s4 + uint64_t(h_[3]) * s3 +
uint64_t(h_[4]) * s2;
uint64_t d2 = uint64_t(h_[0]) * r_[2] + uint64_t(h_[1]) * r_[1] +
uint64_t(h_[2]) * r_[0] + uint64_t(h_[3]) * s4 +
uint64_t(h_[4]) * s3;
uint64_t d3 = uint64_t(h_[0]) * r_[3] + uint64_t(h_[1]) * r_[2] +
uint64_t(h_[2]) * r_[1] + uint64_t(h_[3]) * r_[0] +
uint64_t(h_[4]) * s4;
uint64_t d4 = uint64_t(h_[0]) * r_[4] + uint64_t(h_[1]) * r_[3] +
uint64_t(h_[2]) * r_[2] + uint64_t(h_[3]) * r_[1] +
uint64_t(h_[4]) * r_[0];
uint32_t c = uint32_t(d0 >> 26); h_[0] = uint32_t(d0) & 0x3ffffff;
d1 += c; c = uint32_t(d1 >> 26); h_[1] = uint32_t(d1) & 0x3ffffff;
d2 += c; c = uint32_t(d2 >> 26); h_[2] = uint32_t(d2) & 0x3ffffff;
d3 += c; c = uint32_t(d3 >> 26); h_[3] = uint32_t(d3) & 0x3ffffff;
d4 += c; c = uint32_t(d4 >> 26); h_[4] = uint32_t(d4) & 0x3ffffff;
h_[0] += c * 5; c = h_[0] >> 26; h_[0] &= 0x3ffffff;
h_[1] += c;
}
uint32_t r_[5]{};
uint32_t h_[5]{};
uint32_t pad_[4]{};
uint8_t buf_[16]{};
size_t bufLen_ = 0;
};
inline void poly1305Pad16(Poly1305& p, size_t n) {
static const uint8_t zeros[16] = {0};
const size_t rem = n % 16;
if (rem) p.update(zeros, 16 - rem);
}
inline void lenLE64(uint64_t v, uint8_t out[8]) {
for (int i = 0; i < 8; ++i) out[i] = uint8_t(v >> (8 * i));
}
// Equal in constant time, so a wrong tag leaks nothing through timing.
inline bool equalCT(const uint8_t* a, const uint8_t* b, size_t n) {
uint8_t diff = 0;
for (size_t i = 0; i < n; ++i) diff = uint8_t(diff | (a[i] ^ b[i]));
return diff == 0;
}
} // namespace detail
// ── AEAD (RFC 8439 §2.8) ──────────────────────────────────────────────────
inline constexpr size_t kKeyBytes = 32;
inline constexpr size_t kNonceBytes = 12;
inline constexpr size_t kTagBytes = 16;
// Seals `plain` in place-independent fashion: `out` receives the ciphertext,
// `tag` the authenticator over ciphertext and associated data.
inline void aeadSeal(const uint8_t key[kKeyBytes], const uint8_t nonce[kNonceBytes],
const uint8_t* aad, size_t aadLen, const uint8_t* plain,
size_t plainLen, uint8_t* out, uint8_t tag[kTagBytes]) {
uint8_t polyKey[64];
const uint8_t zeroIn[64] = {0};
detail::chacha20Xor(key, 0, nonce, zeroIn, 64, polyKey); // block 0
if (plainLen) detail::chacha20Xor(key, 1, nonce, plain, plainLen, out);
detail::Poly1305 mac(polyKey);
if (aadLen) {
mac.update(aad, aadLen);
detail::poly1305Pad16(mac, aadLen);
}
if (plainLen) {
mac.update(out, plainLen);
detail::poly1305Pad16(mac, plainLen);
}
uint8_t lens[16];
detail::lenLE64(aadLen, lens);
detail::lenLE64(plainLen, lens + 8);
mac.update(lens, 16);
mac.finish(tag);
}
// Opens what aeadSeal produced. Returns false — without writing plaintext the
// caller can trust — when the tag does not verify.
inline bool aeadOpen(const uint8_t key[kKeyBytes], const uint8_t nonce[kNonceBytes],
const uint8_t* aad, size_t aadLen, const uint8_t* cipher,
size_t cipherLen, const uint8_t tag[kTagBytes],
uint8_t* out) {
uint8_t polyKey[64];
const uint8_t zeroIn[64] = {0};
detail::chacha20Xor(key, 0, nonce, zeroIn, 64, polyKey);
detail::Poly1305 mac(polyKey);
if (aadLen) {
mac.update(aad, aadLen);
detail::poly1305Pad16(mac, aadLen);
}
if (cipherLen) {
mac.update(cipher, cipherLen);
detail::poly1305Pad16(mac, cipherLen);
}
uint8_t lens[16];
detail::lenLE64(aadLen, lens);
detail::lenLE64(cipherLen, lens + 8);
mac.update(lens, 16);
uint8_t want[kTagBytes];
mac.finish(want);
if (!detail::equalCT(want, tag, kTagBytes)) return false;
if (cipherLen) detail::chacha20Xor(key, 1, nonce, cipher, cipherLen, out);
return true;
}
// Convenience over vectors: ciphertext with the 16-byte tag appended.
inline std::vector<uint8_t> seal(const std::array<uint8_t, kKeyBytes>& key,
const std::array<uint8_t, kNonceBytes>& nonce,
const std::string& aad,
const std::vector<uint8_t>& plain) {
std::vector<uint8_t> out(plain.size() + kTagBytes);
aeadSeal(key.data(), nonce.data(),
reinterpret_cast<const uint8_t*>(aad.data()), aad.size(),
plain.data(), plain.size(), out.data(), out.data() + plain.size());
return out;
}
inline bool open(const std::array<uint8_t, kKeyBytes>& key,
const std::array<uint8_t, kNonceBytes>& nonce,
const std::string& aad, const std::vector<uint8_t>& sealed,
std::vector<uint8_t>& plain) {
if (sealed.size() < kTagBytes) return false;
const size_t n = sealed.size() - kTagBytes;
plain.assign(n, 0);
return aeadOpen(key.data(), nonce.data(),
reinterpret_cast<const uint8_t*>(aad.data()), aad.size(),
sealed.data(), n, sealed.data() + n, plain.data());
}
} // namespace aead
} // namespace ANSCENTER
#endif // ANSAEAD_H
+42
View File
@@ -0,0 +1,42 @@
#ifndef ANSMODELKEYS_H
#define ANSMODELKEYS_H
#pragma once
// ANSModelKeys — the keys this build was given, if it was given any.
//
// The base private key is not in this file and is not in the repository. It is
// written by the key ceremony (ANSMINDCORE's tools/ansmodel_keys.cpp) into
// ANSModelKeysGenerated.h, which is ignored by git and produced on the machine
// that builds a release. This header is the seam: it uses that file when it is
// there and says "no keys" when it is not.
//
// A BUILD WITHOUT THE KEY IS A VALID BUILD. Every zip still loads, and a
// package is refused with a sentence that says why. That is what keeps the key
// out of a developer's checkout without keeping developers out of the code.
#if defined(__has_include)
#if __has_include("ANSModelKeysGenerated.h")
#include "ANSModelKeysGenerated.h"
#define ANSCORE_HAVE_BUILTIN_MODEL_KEYS 1
#endif
#endif
#include "ANSModelPackage.h"
namespace ANSCENTER {
namespace modelpkg {
// Add whatever this build ships with to a keyring. False when it ships with
// nothing, which is not an error.
inline bool AddBuiltInKeys(Keyring& ring) {
#if defined(ANSCORE_HAVE_BUILTIN_MODEL_KEYS)
return builtin::addBuiltInKeys(ring);
#else
(void)ring;
return false;
#endif
}
} // namespace modelpkg
} // namespace ANSCENTER
#endif // ANSMODELKEYS_H
+238
View File
@@ -0,0 +1,238 @@
#ifndef ANSMODELOPEN_H
#define ANSMODELOPEN_H
#pragma once
// ANSModelOpen — the one place that knows how a model package reaches an engine.
//
// An engine's "model" has always been a path: a password-protected zip that
// ANSODBase inflates into a folder, after which the engine picks out
// train_last.onnx, classes.names and model_config.json. A .ansmodel package
// replaces all four with one sealed file, and this header is what turns it
// back into the things the engines already understand — the model's bytes, the
// class list, the task and the input size.
//
// THE PLAINTEXT NEVER REACHES THE DISK. That is the whole reason the format
// exists. OpenModelPackage decrypts into a buffer the caller owns; the caller
// hands that buffer to a session and wipes it. Nothing here writes a file.
//
// WHERE THE KEY COMES FROM. Three places, in this order: what the build ships
// with (ANSModelKeys.h, filled in by the key ceremony), what the environment
// offers a developer, and whatever a caller sets with SetProcessModelKeys().
// Nothing here reads a key out of the package or off the disk. With no keys at
// all, a package is refused with a sentence that says so, and every existing
// zip still loads — a build made without the ceremony runs everything except
// packages.
//
// The base private key must never sit in the binary as one contiguous
// constant; assembling it is the job of whatever calls SetProcessModelKeys,
// not of this header, which is why this header has no key material in it.
//
// The development fallback (ANSCORE_MODEL_KEY_ID / ANSCORE_MODEL_BASE_PRIVATE
// / ANSCORE_MODEL_ROOT_ID / ANSCORE_MODEL_ROOT_PUBLIC, all base64) exists so
// the path can be exercised before that ceremony exists. It is read once and
// announced, because a key that came from the environment is not the key a
// shipped product should be using.
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <mutex>
#include <string>
#include <vector>
#include "ANSModelKeys.h"
#include "ANSModelPackage.h"
#include "ANSOnnxShape.h"
namespace ANSCENTER {
namespace modelpkg {
// ── the keys this process opens packages with ─────────────────────────────
namespace detail {
inline std::mutex& keysMutex() {
static std::mutex m;
return m;
}
inline Keyring& keyringStorage() {
static Keyring ring;
return ring;
}
inline bool keyFromB64(const char* b64, Key& out) {
if (!b64 || !*b64) return false;
std::vector<uint8_t> raw;
if (!bits::base64Decode(b64, raw) || raw.size() != out.size()) return false;
std::memcpy(out.data(), raw.data(), out.size());
return true;
}
// True once this process's keyring is settled — either because the build's own
// keys were installed, or because a caller replaced them outright. A caller
// that sets keys means exactly that: what it set is what this process holds.
inline bool& keysSettled() {
static bool settled = false;
return settled;
}
// Development only: keys from the environment, and said out loud.
inline void loadEnvKeys(Keyring& ring) {
const char* id = std::getenv("ANSCORE_MODEL_KEY_ID");
const char* priv = std::getenv("ANSCORE_MODEL_BASE_PRIVATE");
Key k{};
if (id && *id && keyFromB64(priv, k)) {
ring.addX25519(id, k);
std::fprintf(stderr,
"[modelpkg] base key %s taken from the environment - for "
"development, not for a shipped build\n",
id);
}
const char* rootId = std::getenv("ANSCORE_MODEL_ROOT_ID");
const char* rootPub = std::getenv("ANSCORE_MODEL_ROOT_PUBLIC");
Key r{};
if (rootId && *rootId && keyFromB64(rootPub, r)) {
ring.addRoot(rootId, r);
std::fprintf(stderr,
"[modelpkg] root key %s taken from the environment\n",
rootId);
}
}
} // namespace detail
inline void SetProcessModelKeys(Keyring ring) {
std::lock_guard<std::mutex> lk(detail::keysMutex());
detail::keyringStorage() = std::move(ring);
detail::keysSettled() = true;
}
inline Keyring ProcessModelKeys() {
std::lock_guard<std::mutex> lk(detail::keysMutex());
Keyring& ring = detail::keyringStorage();
if (!detail::keysSettled()) {
detail::keysSettled() = true;
AddBuiltInKeys(ring); // what this build was given at the ceremony
detail::loadEnvKeys(ring); // and what a developer's environment adds
}
return ring; // by value: the caller may open on another thread
}
// True once this build can open anything at all. A product that has not had
// keys installed should say "this build cannot open model packages" rather
// than let every load fail one at a time.
inline bool HaveProcessModelKeys() {
const Keyring ring = ProcessModelKeys();
return !ring.x25519.empty() || !ring.keys.empty();
}
// ── recognising one ───────────────────────────────────────────────────────
inline bool IsPackagePath(const std::string& path) {
return path.size() > 9 &&
path.compare(path.size() - 9, 9, ".ansmodel") == 0;
}
// ── what an engine needs out of one ───────────────────────────────────────
// Everything the inflated folder used to carry, in the shape the engines read
// it: the model itself, the classes in order, and the geometry that
// model_config.json held.
struct OpenedModel {
std::vector<uint8_t> model; // decrypted; wipe it once loaded
std::vector<std::string> classes;
std::string task; // detection | classification | segmentation | obb | pose
std::string family; // yolo | yolo10 | rfdetr
std::string runtime; // onnx | openvino-ir
std::string legacyMarker; // what the zip would have carried
std::string name; // what to call this model in a log
int inputWidth = 0; // 0 when the package does not say
int inputHeight = 0;
std::string warning; // show it; the model still loaded
void wipe() {
if (!model.empty()) std::memset(model.data(), 0, model.size());
model.clear();
model.shrink_to_fit();
}
};
// Read what a package says about itself without decrypting the model. Cheap
// enough to call on a folder full of packages.
inline bool DescribeModelPackage(const std::string& path, OpenedModel& out,
std::string& err) {
Package pkg;
if (!open(path, ProcessModelKeys(), pkg, err)) return false;
const Json& m = pkg.manifest;
out.task = m.value("task", std::string("detection"));
out.family = m.value("family", std::string());
out.runtime = m.value("runtime", std::string("onnx"));
out.legacyMarker =
m.value("legacy", Json::object()).value("marker", std::string());
out.warning = pkg.warning;
out.classes.clear();
for (const auto& c : m.value("classes", Json::array()))
out.classes.push_back(c.value("name", std::string()));
const Json in = m.value("input", Json::object());
out.inputWidth = in.value("width", 0);
out.inputHeight = in.value("height", 0);
out.name = m.value("project", std::string());
if (out.name.empty()) out.name = path;
return true;
}
// Open one all the way: the description above, plus the model's bytes.
//
// The caller owns those bytes and should wipe them (OpenedModel::wipe) the
// moment the session has copied what it needs.
inline bool OpenModelPackage(const std::string& path, OpenedModel& out,
std::string& err) {
if (!HaveProcessModelKeys()) {
err = "this build has no model-package key, so it cannot open " + path;
return false;
}
Package pkg;
if (!open(path, ProcessModelKeys(), pkg, err)) return false;
const std::string entry = pkg.entryPayloadId();
if (entry.empty()) {
err = "the package names no model to run";
return false;
}
if (!readPayload(pkg, entry, out.model, err)) return false;
const Json& m = pkg.manifest;
out.task = m.value("task", std::string("detection"));
out.family = m.value("family", std::string());
out.runtime = m.value("runtime", std::string("onnx"));
out.legacyMarker =
m.value("legacy", Json::object()).value("marker", std::string());
out.warning = pkg.warning;
out.classes.clear();
for (const auto& c : m.value("classes", Json::array()))
out.classes.push_back(c.value("name", std::string()));
const Json in = m.value("input", Json::object());
out.inputWidth = in.value("width", 0);
out.inputHeight = in.value("height", 0);
out.name = m.value("project", std::string());
if (out.name.empty()) out.name = path;
// A package written before manifests carried an input size still has to
// answer, and the model in hand is the thing that knows. It costs nothing
// here: the bytes are already decrypted.
if (out.inputWidth <= 0 || out.inputHeight <= 0) {
onnxshape::InputShape shape;
std::string swhy;
if (onnxshape::fromMemory(out.model.data(), out.model.size(), shape,
swhy)) {
out.inputWidth = shape.width;
out.inputHeight = shape.height;
}
}
return true;
}
} // namespace modelpkg
} // namespace ANSCENTER
#endif // ANSMODELOPEN_H
+768
View File
@@ -0,0 +1,768 @@
#ifndef ANSMODELPACKAGE_H
#define ANSMODELPACKAGE_H
#pragma once
// ANSModelPackage — reading (and, for tools and tests, writing) an ANS model
// package: one encrypted file carrying a model, its classes and how to run it.
//
// KEPT IN STEP WITH ANSMINDCORE's src/model/ansmodel.hpp. The two are the same
// format and must stay byte-compatible: the unit test here opens a package
// written by THAT implementation, which is what makes "compatible" a fact
// rather than an intention. Change them together.
//
// WHAT THIS REPLACES. A model shipped as a zip under a password every copy of
// the software knew — a password that has since been recovered — holding
// Categories/<name>.<id>, classes.names, a zero-length backend marker and
// train_last.onnx. Opening one meant inflating it to a directory, so the
// unprotected model sat on disk, was optimised, and was zipped back into a
// cache. This format keeps every fact that bundle carried and changes how it
// is carried: the manifest names the classes and the model family, and the
// bytes go straight from decryption into the runtime (engine/ibackend's
// ModelSource) without touching the filesystem.
//
// LAYOUT (all integers little-endian, offsets absolute):
//
// 0 8 magic "ANSMDL\0\0"
// 8 2 format version (1)
// 10 2 flags, bit 0 = more than one model
// 12 4 header length
// 16 n cleartext header, UTF-8 JSON — readable without any key
// … manifest, sealed — classes, task, family, models
// … payloads, sealed in chunks — the model bytes
//
// WHAT IS IN THE CLEAR, and why so little. The header carries only what a
// reader needs before it can decrypt — which key opens the file, where the
// sealed parts are — plus an optional card of counts for a store listing.
// Class names, metrics and the model's shape are in the manifest, sealed.
//
// SEALING. ChaCha20-Poly1305 (core/chacha20poly1305) with a content key that
// is random per package. Each chunk's nonce is a random per-payload base
// followed by the chunk index, and the associated data binds the key id, the
// payload id and that index — so a chunk cannot be moved between payloads or
// between packages, and a reordered file fails on the first chunk.
//
// WRAPPING. The content key is sealed to the base key with X25519: an
// ephemeral key pair per package, a shared secret against the base public key,
// and a key derived from it with HMAC-SHA256. Packaging tools therefore hold
// only the PUBLIC half and cannot open what they produce — which is the point
// of doing it asymmetrically when a symmetric key would have been simpler. A
// package may carry several wraps; a reader takes the first one it holds a key
// for. The older symmetric wrap is still read, so packages written before this
// keep opening.
//
// SIGNING. Ed25519 (RFC 8032, via the vendored monocypher-ed25519) over the
// header with its own `sig` removed. The signature is made by a RELEASE key
// whose public half travels in the package inside a certificate the ROOT key
// signed — so an engine that has never seen that release still verifies it,
// from the one root key it was built with. That is what lets signing keys
// rotate without stranding an engine that cannot be updated.
//
// EXPIRY AND REVOCATION. An expired certificate WARNS and loads: the date says
// when a key was in service, not whether the package is genuine, and an
// air-gapped box's clock drifts. A revoked key refuses outright. Strict mode
// turns expiry into a refusal for sites that keep accurate time.
#include <algorithm>
#include <array>
#include <chrono>
#include <cstdint>
#include <cstring>
#include <ctime>
#include <fstream>
#include <map>
#include <string>
#include <vector>
#include <nlohmann/json.hpp>
#include "ANSAead.h"
#include "ANSPackageBits.h"
extern "C" {
#include "monocypher-ed25519.h"
#include "monocypher.h"
}
namespace ANSCENTER {
namespace modelpkg {
using Json = nlohmann::json;
inline constexpr char kMagic[8] = {'A', 'N', 'S', 'M', 'D', 'L', '\0', '\0'};
inline constexpr uint16_t kVersion = 1;
inline constexpr uint16_t kFlagMultiModel = 1u << 0;
inline constexpr const char* kWrapSymmetric = "chacha20poly1305-key";
inline constexpr const char* kWrapX25519 = "x25519-hkdf-sha256-chacha20poly1305";
inline constexpr const char* kSigEd25519 = "ed25519";
inline constexpr uint32_t kDefaultChunk = 4u * 1024 * 1024;
using Key = std::array<uint8_t, aead::kKeyBytes>;
// What this build can open and what it trusts.
//
// A reader holds every base key it has ever shipped, because the base key does
// not rotate: a package names the key it was sealed to, and the answer to an
// unknown id is "this package needs a newer engine", not a decryption failure.
// It holds root public keys, which is how a release signing key it has never
// seen is still verified. And it holds the list of signing keys that have been
// burned, which is the only thing that refuses a validly signed package.
struct Keyring {
std::map<std::string, Key> keys; // symmetric base keys (legacy wrap)
std::map<std::string, Key> x25519; // base PRIVATE keys, by id
std::map<std::string, Key> roots; // root PUBLIC keys, by id
std::vector<std::string> revokedSigners;
bool strictExpiry = false; // off: an expired cert warns and loads
int64_t nowSec = 0; // 0 = ask the clock
void add(std::string id, const Key& k) { keys[std::move(id)] = k; }
void addX25519(std::string id, const Key& priv) { x25519[std::move(id)] = priv; }
void addRoot(std::string id, const Key& pub) { roots[std::move(id)] = pub; }
bool revoked(const std::string& signerId) const {
for (const auto& r : revokedSigners)
if (r == signerId) return true;
return false;
}
const Key* find(const std::string& id) const {
const auto it = keys.find(id);
return it == keys.end() ? nullptr : &it->second;
}
};
// One model inside a package: where its bytes are and how they are sealed.
struct PayloadRef {
std::string id;
uint64_t off = 0;
uint64_t len = 0; // sealed length, tags included
uint32_t chunk = kDefaultChunk;
std::array<uint8_t, 4> nonceBase{};
};
// An opened package: everything but the model bytes, which are read on demand
// so a two-gigabyte model is never decrypted to answer "what is this?".
struct Package {
std::string path;
uint16_t version = 0;
uint16_t flags = 0;
std::string keyId;
Json header; // the cleartext header, as read
Json manifest; // decrypted
std::vector<PayloadRef> payloads;
Key contentKey{};
std::string signerId; // the release key that signed it
bool certExpired = false; // loaded anyway; see the header comment
std::string warning; // what a caller should show, if anything
bool multiModel() const { return (flags & kFlagMultiModel) != 0; }
// The model a reader should run when it is given no choice: the manifest's
// entry model, or the first one.
std::string entryPayloadId() const {
const auto models = manifest.value("models", Json::array());
for (const auto& m : models)
if (m.value("entry", false)) return m.value("payload", std::string());
if (!models.empty()) return models[0].value("payload", std::string());
return payloads.empty() ? std::string() : payloads.front().id;
}
};
namespace detail {
inline uint16_t rd16(const uint8_t* p) { return uint16_t(p[0] | (p[1] << 8)); }
inline uint32_t rd32(const uint8_t* p) {
return uint32_t(p[0]) | (uint32_t(p[1]) << 8) | (uint32_t(p[2]) << 16) |
(uint32_t(p[3]) << 24);
}
inline void wr16(std::string& out, uint16_t v) {
out.push_back(char(v & 0xff));
out.push_back(char(v >> 8));
}
inline void wr32(std::string& out, uint32_t v) {
for (int i = 0; i < 4; ++i) out.push_back(char((v >> (8 * i)) & 0xff));
}
inline std::string b64(const std::vector<uint8_t>& v) {
return bits::base64Encode(std::string(v.begin(), v.end()));
}
inline std::vector<uint8_t> unb64(const std::string& s) {
std::vector<uint8_t> out;
if (!bits::base64Decode(s, out)) out.clear();
return out;
}
// The nonce for one chunk: a random per-payload base, then the index. Unique
// per (payload, chunk) under a content key that is unique per package.
inline std::array<uint8_t, aead::kNonceBytes> chunkNonce(
const std::array<uint8_t, 4>& base, uint64_t index) {
std::array<uint8_t, aead::kNonceBytes> n{};
std::memcpy(n.data(), base.data(), 4);
for (int i = 0; i < 8; ++i) n[4 + i] = uint8_t(index >> (8 * i));
return n;
}
// What each chunk is bound to. Moving a chunk to another payload, another
// index or another package changes this, and the tag then fails.
inline std::string chunkAad(const std::string& keyId, const std::string& payloadId,
uint64_t index, uint64_t chunkCount) {
return "ansmodel/v1|" + keyId + "|" + payloadId + "|" + std::to_string(index) +
"|" + std::to_string(chunkCount);
}
// The wrapping key from an X25519 shared secret. HMAC-SHA256 over the secret
// with a fixed label, which is HKDF-Extract: the raw secret is not uniform and
// must never be used as a key directly.
inline Key deriveWrapKey(const uint8_t shared[32], const std::string& keyId) {
const std::string salt = "ansmodel/v1|wrap|" + keyId;
const auto mac = bits::hmacSha256(
reinterpret_cast<const uint8_t*>(salt.data()), salt.size(), shared, 32);
Key k{};
std::memcpy(k.data(), mac.data(), k.size());
return k;
}
// The bytes a signature covers: the header with its own `sig` taken out, as
// canonical JSON. nlohmann orders object keys, so a reader that removes the
// same field and re-serialises reproduces exactly what the packer signed.
inline std::string signedBytes(Json header) {
header.erase("sig");
return header.dump();
}
// What the root vouches for: this release key, this id, until this date.
inline std::string certBytes(const Json& cert) {
return "ansmodel/v1|cert|" + cert.value("keyId", std::string()) + "|" +
cert.value("pub", std::string()) + "|" +
cert.value("notAfter", std::string());
}
inline int64_t nowSeconds() {
return int64_t(std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch())
.count());
}
// "2027-09-01T00:00:00Z" → seconds. Returns 0 when it cannot be read, which
// is treated as "no expiry stated" rather than "expired".
inline int64_t parseIso8601(const std::string& t) {
std::tm tm{};
if (t.size() < 19) return 0;
tm.tm_year = std::atoi(t.substr(0, 4).c_str()) - 1900;
tm.tm_mon = std::atoi(t.substr(5, 2).c_str()) - 1;
tm.tm_mday = std::atoi(t.substr(8, 2).c_str());
tm.tm_hour = std::atoi(t.substr(11, 2).c_str());
tm.tm_min = std::atoi(t.substr(14, 2).c_str());
tm.tm_sec = std::atoi(t.substr(17, 2).c_str());
if (tm.tm_year <= 0 || tm.tm_mon < 0 || tm.tm_mon > 11) return 0;
#if defined(_WIN32)
return int64_t(_mkgmtime(&tm));
#else
return int64_t(timegm(&tm));
#endif
}
inline uint64_t chunkCountFor(uint64_t sealedLen, uint32_t chunk) {
const uint64_t perChunk = chunk + aead::kTagBytes;
return (sealedLen + perChunk - 1) / perChunk;
}
} // namespace detail
// ── reading ───────────────────────────────────────────────────────────────
// Open the header and manifest. Does not read a single model byte.
inline bool open(const std::string& path, const Keyring& ring, Package& out,
std::string& err) {
out = Package{};
out.path = path;
std::ifstream f(path, std::ios::binary);
if (!f) {
err = "cannot open " + path;
return false;
}
uint8_t fixed[16];
f.read(reinterpret_cast<char*>(fixed), sizeof(fixed));
if (f.gcount() != std::streamsize(sizeof(fixed))) {
err = "not a model package: file is too short";
return false;
}
if (std::memcmp(fixed, kMagic, sizeof(kMagic)) != 0) {
err = "not a model package: wrong magic";
return false;
}
out.version = detail::rd16(fixed + 8);
out.flags = detail::rd16(fixed + 10);
if (out.version > kVersion) {
err = "this package is format version " + std::to_string(out.version) +
", which needs a newer ANS engine";
return false;
}
const uint32_t headerLen = detail::rd32(fixed + 12);
if (headerLen == 0 || headerLen > 8u * 1024 * 1024) {
err = "header length is not plausible";
return false;
}
std::string headerText(headerLen, '\0');
f.read(headerText.data(), headerLen);
if (f.gcount() != std::streamsize(headerLen)) {
err = "truncated: the header is not all there";
return false;
}
out.header = Json::parse(headerText, nullptr, false);
if (out.header.is_discarded()) {
err = "the header is not readable JSON";
return false;
}
// ── the signature, before anything is decrypted ────────────────────────
const Json sig = out.header.value("sig", Json::object());
if (sig.is_null() || sig.empty()) {
err = "the package is not signed";
return false;
}
const std::string sigAlg = sig.value("alg", std::string());
if (sigAlg == kSigEd25519) {
const Json cert = sig.value("cert", Json::object());
const std::string signerId = cert.value("keyId", std::string());
const std::string rootId = cert.value("rootKeyId", std::string());
const auto signerPub = detail::unb64(cert.value("pub", std::string()));
const auto rootSig = detail::unb64(cert.value("rootSig", std::string()));
const auto headerSig = detail::unb64(sig.value("value", std::string()));
if (signerPub.size() != 32 || rootSig.size() != 64 || headerSig.size() != 64) {
err = "the signature block is malformed";
return false;
}
if (ring.revoked(signerId)) {
err = "this package was signed with a key that has been revoked (" +
signerId + ")";
return false;
}
const auto rootIt = ring.roots.find(rootId);
if (rootIt == ring.roots.end()) {
err = "the package is vouched for by an unknown root (" + rootId + ")";
return false;
}
// The root vouches for the release key; the release key signs the header.
const std::string cb = detail::certBytes(cert);
if (crypto_ed25519_check(rootSig.data(), rootIt->second.data(),
reinterpret_cast<const uint8_t*>(cb.data()),
cb.size()) != 0) {
err = "the signing key is not vouched for by the root it names";
return false;
}
const std::string sb = detail::signedBytes(out.header);
if (crypto_ed25519_check(headerSig.data(), signerPub.data(),
reinterpret_cast<const uint8_t*>(sb.data()),
sb.size()) != 0) {
err = "the package signature does not verify — it has been altered";
return false;
}
out.signerId = signerId;
// Expiry is a fact about the key's service, not about this package's
// authenticity, and an air-gapped clock drifts. Warn and load; strict
// mode is for sites that would rather fail closed.
const int64_t notAfter = detail::parseIso8601(cert.value("notAfter", std::string()));
const int64_t now = ring.nowSec > 0 ? ring.nowSec : detail::nowSeconds();
if (notAfter > 0 && now > notAfter) {
out.certExpired = true;
out.warning = "signed with a certificate that expired on " +
cert.value("notAfter", std::string()) + " (key " + signerId + ")";
if (ring.strictExpiry) {
err = out.warning + " — refused because strict expiry is on";
return false;
}
}
} else if (sigAlg != "none") {
err = "the package is signed in a way this build does not know (" + sigAlg + ")";
return false;
}
// Which key opens it. Every wrap is tried, so a package sealed to several
// keys opens on any engine holding one of them.
const auto wraps = out.header.value("wraps", Json::array());
if (wraps.empty()) {
err = "the package names no key";
return false;
}
std::vector<std::string> unknown;
bool unwrapped = false;
for (const auto& w : wraps) {
const std::string keyId = w.value("keyId", std::string());
const std::string alg = w.value("alg", std::string());
const auto nonceBytes = detail::unb64(w.value("nonce", std::string()));
const auto ct = detail::unb64(w.value("ct", std::string()));
if (nonceBytes.size() != aead::kNonceBytes ||
ct.size() != aead::kKeyBytes + aead::kTagBytes) {
err = "the wrapped key is malformed";
return false;
}
std::array<uint8_t, aead::kNonceBytes> nonce{};
std::memcpy(nonce.data(), nonceBytes.data(), nonce.size());
// The key that opens THIS wrap: either an X25519 shared secret against the
// ephemeral public key the packer left here, or — for packages written
// before the curve was vendored — the symmetric base key.
Key unwrapKey{};
if (alg == kWrapX25519) {
const auto it = ring.x25519.find(keyId);
if (it == ring.x25519.end()) {
unknown.push_back(keyId);
continue;
}
const auto epk = detail::unb64(w.value("epk", std::string()));
if (epk.size() != 32) {
err = "the wrap carries no usable ephemeral key";
return false;
}
uint8_t shared[32];
crypto_x25519(shared, it->second.data(), epk.data());
unwrapKey = detail::deriveWrapKey(shared, keyId);
crypto_wipe(shared, sizeof(shared));
} else if (alg == kWrapSymmetric) {
const Key* k = ring.find(keyId);
if (!k) {
unknown.push_back(keyId);
continue;
}
unwrapKey = *k;
} else {
err = "the package uses an unsupported wrap (" + alg + ")";
return false;
}
std::vector<uint8_t> plain;
if (!aead::open(unwrapKey, nonce, "ansmodel/v1|wrap|" + keyId, ct, plain)) {
err = "the wrapped key does not open with " + keyId +
" — the file is damaged or was sealed to a different key";
return false;
}
std::memcpy(out.contentKey.data(), plain.data(), out.contentKey.size());
out.keyId = keyId;
unwrapped = true;
break;
}
if (!unwrapped) {
err = "this package needs a newer ANS engine: it is sealed to key " +
(unknown.empty() ? std::string("(unnamed)") : unknown.front());
return false;
}
// Where the sealed parts are.
for (const auto& p : out.header.value("payloads", Json::array())) {
PayloadRef r;
r.id = p.value("id", std::string());
r.off = p.value("off", uint64_t(0));
r.len = p.value("len", uint64_t(0));
r.chunk = p.value("chunk", kDefaultChunk);
const auto nb = detail::unb64(p.value("nonceBase", std::string()));
if (r.id.empty() || r.len == 0 || r.chunk == 0 || nb.size() != 4) {
err = "a payload entry is malformed";
return false;
}
std::memcpy(r.nonceBase.data(), nb.data(), 4);
out.payloads.push_back(r);
}
if (out.payloads.empty()) {
err = "the package carries no model";
return false;
}
// The manifest, sealed as a single chunk under the payload id "manifest".
const Json m = out.header.value("manifest", Json::object());
const uint64_t moff = m.value("off", uint64_t(0));
const uint64_t mlen = m.value("len", uint64_t(0));
const auto mnb = detail::unb64(m.value("nonceBase", std::string()));
if (mlen == 0 || mnb.size() != 4) {
err = "the manifest entry is malformed";
return false;
}
std::vector<uint8_t> sealed(static_cast<size_t>(mlen), 0);
f.seekg(std::streamoff(moff));
f.read(reinterpret_cast<char*>(sealed.data()), std::streamsize(mlen));
if (f.gcount() != std::streamsize(mlen)) {
err = "truncated: the manifest is not all there";
return false;
}
std::array<uint8_t, 4> mbase{};
std::memcpy(mbase.data(), mnb.data(), 4);
std::vector<uint8_t> manifestText;
if (!aead::open(out.contentKey, detail::chunkNonce(mbase, 0),
detail::chunkAad(out.keyId, "manifest", 0, 1), sealed,
manifestText)) {
err = "the manifest does not verify — the package has been altered";
return false;
}
out.manifest =
Json::parse(std::string(manifestText.begin(), manifestText.end()), nullptr,
false);
if (out.manifest.is_discarded()) {
err = "the manifest is not readable JSON";
return false;
}
return true;
}
// Decrypt one model into memory. Chunk by chunk, each verified as it is read,
// so a damaged file fails on the chunk that is wrong rather than after the
// whole thing has been decrypted.
inline bool readPayload(const Package& pkg, const std::string& payloadId,
std::vector<uint8_t>& out, std::string& err) {
const PayloadRef* ref = nullptr;
for (const auto& p : pkg.payloads)
if (p.id == payloadId) ref = &p;
if (!ref) {
err = "no model called " + payloadId + " in this package";
return false;
}
std::ifstream f(pkg.path, std::ios::binary);
if (!f) {
err = "cannot reopen " + pkg.path;
return false;
}
f.seekg(std::streamoff(ref->off));
const uint64_t chunks = detail::chunkCountFor(ref->len, ref->chunk);
out.clear();
out.reserve(size_t(ref->len));
std::vector<uint8_t> sealed;
std::vector<uint8_t> plain;
uint64_t remaining = ref->len;
for (uint64_t i = 0; i < chunks; ++i) {
const uint64_t take =
remaining < uint64_t(ref->chunk) + aead::kTagBytes
? remaining
: uint64_t(ref->chunk) + aead::kTagBytes;
sealed.assign(size_t(take), 0);
f.read(reinterpret_cast<char*>(sealed.data()), std::streamsize(take));
if (f.gcount() != std::streamsize(take)) {
err = "truncated: model " + payloadId + " stops at chunk " +
std::to_string(i);
return false;
}
if (!aead::open(pkg.contentKey, detail::chunkNonce(ref->nonceBase, i),
detail::chunkAad(pkg.keyId, ref->id, i, chunks), sealed,
plain)) {
err = "chunk " + std::to_string(i) + " of " + payloadId +
" does not verify — the package has been altered";
out.clear();
return false;
}
out.insert(out.end(), plain.begin(), plain.end());
remaining -= take;
}
return true;
}
// ── writing (tools and tests) ─────────────────────────────────────────────
//
// The product's packages are written by the training box. This exists so the
// reader can be tested against files built by something other than itself,
// and so a converter can turn today's zip bundles into packages.
struct PackModel {
std::string id; // payload id, e.g. "p0"
std::vector<uint8_t> bytes;
};
struct PackRequest {
std::string keyId;
Key key{}; // symmetric base key (legacy wrap)
// Wrap to a base PUBLIC key instead, which is what a real packer does: it
// can then produce packages it cannot itself open.
bool useX25519 = false;
Key basePublic{};
// Sign as a release key the root vouches for. Empty signerSecret writes an
// unsigned package, which only tests should ever want.
std::string signerId;
std::vector<uint8_t> signerSecret; // 64 bytes, RFC 8032
std::vector<uint8_t> signerPublic; // 32 bytes
std::string rootKeyId;
std::vector<uint8_t> rootSig; // 64 bytes over the certificate
std::string notAfter; // ISO-8601, may be empty
Json manifest;
Json card; // optional, counts only
std::vector<PackModel> models;
uint32_t chunk = kDefaultChunk; // smaller in tests, to exercise chunking
// Deterministic nonces are for TESTS ONLY: a real package draws them from
// the platform CSPRNG, because a repeated (key, nonce) pair is fatal.
uint8_t nonceSeed = 0;
};
inline bool pack(const std::string& path, const PackRequest& req,
std::string& err) {
if (req.models.empty()) {
err = "a package with no model describes nothing";
return false;
}
// A content key per package. Tests pass a seed; a real packer must use the
// platform CSPRNG here.
Key contentKey{};
for (size_t i = 0; i < contentKey.size(); ++i)
contentKey[i] = uint8_t(req.nonceSeed * 31 + i * 7 + 11);
auto sealChunked = [&](const std::vector<uint8_t>& plain,
const std::string& payloadId,
const std::array<uint8_t, 4>& base,
std::string& blob) {
const uint64_t chunks =
plain.empty() ? 1 : (plain.size() + req.chunk - 1) / req.chunk;
for (uint64_t i = 0; i < chunks; ++i) {
const size_t from = size_t(i * req.chunk);
const size_t take = std::min<size_t>(req.chunk, plain.size() - from);
std::vector<uint8_t> part(plain.begin() + from, plain.begin() + from + take);
const auto sealed =
aead::seal(contentKey, detail::chunkNonce(base, i),
detail::chunkAad(req.keyId, payloadId, i, chunks), part);
blob.append(reinterpret_cast<const char*>(sealed.data()), sealed.size());
}
};
// The manifest first, then each model, so the header can name their offsets.
const std::string manifestText = req.manifest.dump();
const std::vector<uint8_t> manifestBytes(manifestText.begin(),
manifestText.end());
std::array<uint8_t, 4> manifestBase{uint8_t(req.nonceSeed + 1), 2, 3, 4};
std::string manifestBlob;
{
const auto sealed =
aead::seal(contentKey, detail::chunkNonce(manifestBase, 0),
detail::chunkAad(req.keyId, "manifest", 0, 1),
manifestBytes);
manifestBlob.assign(reinterpret_cast<const char*>(sealed.data()),
sealed.size());
}
std::vector<std::string> blobs;
std::vector<std::array<uint8_t, 4>> bases;
for (size_t i = 0; i < req.models.size(); ++i) {
std::array<uint8_t, 4> base{uint8_t(req.nonceSeed + 10 + i), 5, 6, 7};
std::string blob;
sealChunked(req.models[i].bytes, req.models[i].id, base, blob);
blobs.push_back(std::move(blob));
bases.push_back(base);
}
// Wrap the content key to the base key. One wrap here; the header's shape
// takes several, which is how a package opens on more than one key.
std::array<uint8_t, aead::kNonceBytes> wrapNonce{};
for (size_t i = 0; i < wrapNonce.size(); ++i)
wrapNonce[i] = uint8_t(req.nonceSeed + 100 + i);
const std::vector<uint8_t> keyPlain(contentKey.begin(), contentKey.end());
Key wrapKey = req.key;
std::vector<uint8_t> epk;
if (req.useX25519) {
// An ephemeral key pair per package: its public half travels in the
// header, its private half dies here, and the packer never holds anything
// that opens the result.
Key esk{};
for (size_t i = 0; i < esk.size(); ++i)
esk[i] = uint8_t(req.nonceSeed * 13 + i * 5 + 3);
epk.assign(32, 0);
crypto_x25519_public_key(epk.data(), esk.data());
uint8_t shared[32];
crypto_x25519(shared, esk.data(), req.basePublic.data());
wrapKey = detail::deriveWrapKey(shared, req.keyId);
crypto_wipe(shared, sizeof(shared));
crypto_wipe(esk.data(), esk.size());
}
const auto wrapped = aead::seal(wrapKey, wrapNonce,
"ansmodel/v1|wrap|" + req.keyId, keyPlain);
// Header offsets are known once the sizes are: the header is written first,
// so its own length has to be settled before the offsets inside it. Build it
// with placeholder offsets, measure, then fill them in — the size cannot
// change, because the numbers are written with a fixed width.
auto buildHeader = [&](uint64_t headerLen) {
Json h;
h["format"] = "ansmodel";
h["version"] = int(kVersion);
Json wrap{{"keyId", req.keyId},
{"alg", req.useX25519 ? kWrapX25519 : kWrapSymmetric},
{"nonce", detail::b64(std::vector<uint8_t>(wrapNonce.begin(),
wrapNonce.end()))},
{"ct", detail::b64(wrapped)}};
if (req.useX25519) wrap["epk"] = detail::b64(epk);
h["wraps"] = Json::array({wrap});
uint64_t at = 16 + headerLen;
h["manifest"] = Json{{"off", at},
{"len", uint64_t(manifestBlob.size())},
{"nonceBase", detail::b64(std::vector<uint8_t>(
manifestBase.begin(), manifestBase.end()))}};
at += manifestBlob.size();
Json payloads = Json::array();
for (size_t i = 0; i < blobs.size(); ++i) {
payloads.push_back(Json{
{"id", req.models[i].id},
{"off", at},
{"len", uint64_t(blobs[i].size())},
{"chunk", req.chunk},
{"nonceBase", detail::b64(std::vector<uint8_t>(bases[i].begin(),
bases[i].end()))},
{"sha256", bits::sha256Hex(std::string(blobs[i].begin(), blobs[i].end()))}});
at += blobs[i].size();
}
h["payloads"] = payloads;
if (!req.card.is_null()) h["card"] = req.card;
// The signature is written last, over everything above: `sig` is added
// with a placeholder of the exact length the real one takes, so signing
// cannot change the header's size and the offsets stay true.
if (req.signerSecret.size() == 64 && req.signerPublic.size() == 32) {
h["sig"] = Json{{"alg", kSigEd25519},
{"value", std::string(88, 'A')},
{"cert", Json{{"keyId", req.signerId},
{"pub", detail::b64(req.signerPublic)},
{"notAfter", req.notAfter},
{"rootKeyId", req.rootKeyId},
{"rootSig", detail::b64(req.rootSig)}}}};
} else {
h["sig"] = Json{{"alg", "none"}, {"value", ""}};
}
return h;
};
// Two passes: the first measures, the second is written. Offsets are plain
// JSON numbers, so a longer offset could lengthen the header — hence the
// loop, which settles in one extra step.
std::string headerText = buildHeader(0).dump();
for (int i = 0; i < 4; ++i) {
const std::string next = buildHeader(headerText.size()).dump();
if (next.size() == headerText.size()) {
headerText = next;
break;
}
headerText = next;
}
Json header = buildHeader(headerText.size());
if (req.signerSecret.size() == 64) {
// Sign the header as the reader will read it: without `sig`, canonically
// serialised. The placeholder above keeps the length identical, so putting
// the real signature in changes no offset.
const std::string sb = detail::signedBytes(header);
std::vector<uint8_t> value(64, 0);
crypto_ed25519_sign(value.data(), req.signerSecret.data(),
reinterpret_cast<const uint8_t*>(sb.data()), sb.size());
header["sig"]["value"] = detail::b64(value);
}
headerText = header.dump();
std::string file(kMagic, sizeof(kMagic));
detail::wr16(file, kVersion);
detail::wr16(file, req.models.size() > 1 ? kFlagMultiModel : uint16_t(0));
detail::wr32(file, uint32_t(headerText.size()));
file += headerText;
file += manifestBlob;
for (const auto& b : blobs) file += b;
std::ofstream o(path, std::ios::binary | std::ios::trunc);
if (!o) {
err = "cannot write " + path;
return false;
}
o.write(file.data(), std::streamsize(file.size()));
if (!o) {
err = "failed while writing " + path;
return false;
}
return true;
}
} // namespace modelpkg
} // namespace ANSCENTER
#endif // ANSMODELPACKAGE_H
+241
View File
@@ -0,0 +1,241 @@
#ifndef ANSONNXSHAPE_H
#define ANSONNXSHAPE_H
#pragma once
// ANSOnnxShape — the input geometry a model declares, read off the graph.
//
// WHY A READER NEEDS THIS. An engine has to know what its model expects before
// it can preprocess a single frame, and that answer has always come from
// whatever the bundle happened to carry: model_config.json's input_size, or an
// `input` object in a package manifest. Neither is present on every model —
// most of the bundles in the field are classes.names, a marker file and the
// export, and nothing else. The graph is always there, and the graph is what
// the session is built from, so where the metadata is silent the model itself
// is asked instead of every engine falling back to 640x640.
//
// TWO SOURCES, ONE PARSER. A package's model is already in memory, decrypted,
// and must never reach the disk; an inflated bundle's is a file that may be
// hundreds of megabytes. The template below reads either, and the file case
// SEEKS past the nodes and the initializers rather than reading them — the
// fields this wants sit after all of the weight on the wire.
//
// The path through the schema, for anyone checking this against onnx.proto:
//
// ModelProto.graph field 7, length-delimited
// GraphProto.input field 11, repeated (after node and
// initializer on the wire)
// ValueInfoProto.type field 2
// TypeProto.tensor_type field 1
// TypeProto.Tensor.shape field 2
// TensorShapeProto.dim field 1, repeated
// Dimension.dim_value field 1, varint (dim_param means the
// axis is dynamic: reported as 0)
//
// An axis the graph leaves open is reported as zero, and a file that cannot be
// parsed is reported as a failure. Both mean the same thing to a caller: this
// model does not say, keep whatever default you had. Being wrong about a
// model's input size is worse than being silent about it.
#include <cstdint>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
namespace ANSCENTER {
namespace onnxshape {
struct InputShape {
int channels = 0; // 0 where the graph does not fix that axis
int height = 0;
int width = 0;
bool known() const { return height > 0 && width > 0; }
};
namespace detail {
// A model already in memory: the decrypted contents of a package.
struct MemSource {
const uint8_t* p = nullptr;
int64_t size = 0;
int64_t at = 0;
int64_t tell() const { return at; }
void seek(int64_t to) { at = to < 0 ? 0 : (to > size ? size : to); }
int get() { return at < size ? int(p[at++]) : -1; }
bool ok() const { return true; }
};
// A model on disk, walked without being read.
struct FileSource {
std::ifstream f;
int64_t size = 0;
int64_t tell() { return int64_t(f.tellg()); }
void seek(int64_t to) { f.clear(); f.seekg(std::streamoff(to), std::ios::beg); }
int get() { return f.get(); }
bool ok() { return bool(f); }
};
template <class Src>
bool varint(Src& s, uint64_t& out) {
out = 0;
for (int shift = 0; shift < 64; shift += 7) {
const int c = s.get();
if (c < 0) return false;
out |= uint64_t(c & 0x7f) << shift;
if ((c & 0x80) == 0) return true;
}
return false;
}
template <class Src>
bool tag(Src& s, uint32_t& field, uint32_t& wire) {
uint64_t key = 0;
if (!varint(s, key)) return false;
field = uint32_t(key >> 3);
wire = uint32_t(key & 0x7);
return field != 0;
}
// Step over a field whose contents are of no interest.
template <class Src>
bool skip(Src& s, uint32_t wire) {
uint64_t v = 0;
switch (wire) {
case 0: return varint(s, v);
case 1: s.seek(s.tell() + 8); return s.ok();
case 5: s.seek(s.tell() + 4); return s.ok();
case 2:
if (!varint(s, v)) return false;
s.seek(s.tell() + int64_t(v));
return s.ok();
default: return false; // groups: not used anywhere in onnx.proto
}
}
// Find one length-delimited field inside [here, end) and stop on its payload.
template <class Src>
bool seekField(Src& s, int64_t end, uint32_t want, uint64_t& len) {
while (s.ok() && s.tell() >= 0 && s.tell() < end) {
uint32_t field = 0, wire = 0;
if (!tag(s, field, wire)) return false;
if (field == want && wire == 2) return varint(s, len);
if (!skip(s, wire)) return false;
}
return false;
}
// TensorShapeProto: the dimensions in order, a dynamic axis as 0.
template <class Src>
std::vector<int64_t> dims(Src& s, int64_t end) {
std::vector<int64_t> out;
while (s.ok() && s.tell() >= 0 && s.tell() < end) {
uint32_t field = 0, wire = 0;
if (!tag(s, field, wire)) break;
if (field != 1 || wire != 2) { // only `dim` matters
if (!skip(s, wire)) break;
continue;
}
uint64_t len = 0;
if (!varint(s, len)) break;
const int64_t dimEnd = s.tell() + int64_t(len);
int64_t value = 0; // stays 0 for dim_param, i.e. a dynamic axis
while (s.ok() && s.tell() >= 0 && s.tell() < dimEnd) {
uint32_t df = 0, dw = 0;
if (!tag(s, df, dw)) break;
if (df == 1 && dw == 0) {
uint64_t v = 0;
if (!varint(s, v)) break;
value = int64_t(v);
} else if (!skip(s, dw)) {
break;
}
}
s.seek(dimEnd);
out.push_back(value);
}
return out;
}
template <class Src>
bool read(Src& s, int64_t end, InputShape& out, std::string& why) {
uint64_t len = 0;
if (!seekField(s, end, 7, len)) { // ModelProto.graph
why = "this does not look like an ONNX model";
return false;
}
const int64_t graphEnd = s.tell() + int64_t(len);
if (!seekField(s, graphEnd, 11, len)) { // GraphProto.input
why = "the graph declares no input";
return false;
}
const int64_t inputEnd = s.tell() + int64_t(len);
if (!seekField(s, inputEnd, 2, len)) { // ValueInfoProto.type
why = "the input declares no type";
return false;
}
const int64_t typeEnd = s.tell() + int64_t(len);
if (!seekField(s, typeEnd, 1, len)) { // TypeProto.tensor_type
why = "the input is not a tensor";
return false;
}
const int64_t tensorEnd = s.tell() + int64_t(len);
if (!seekField(s, tensorEnd, 2, len)) { // Tensor.shape
why = "the input tensor has no shape";
return false;
}
const std::vector<int64_t> d = dims(s, s.tell() + int64_t(len));
// NCHW is the only layout these engines run.
if (d.size() != 4) {
why = "the input is not a four-dimensional image tensor";
return false;
}
auto axis = [](int64_t v) { return (v > 0 && v < 1000000) ? int(v) : 0; };
out.channels = axis(d[1]);
out.height = axis(d[2]);
out.width = axis(d[3]);
if (!out.known()) {
why = "the model takes a dynamic input size";
return false;
}
return true;
}
} // namespace detail
// What a model already in memory declares.
inline bool fromMemory(const void* data, size_t size, InputShape& out,
std::string& why) {
if (!data || size == 0) {
why = "there is no model to read";
return false;
}
detail::MemSource s{static_cast<const uint8_t*>(data), int64_t(size), 0};
return detail::read(s, int64_t(size), out, why);
}
// What a model on disk declares. The file is walked, not loaded.
inline bool fromFile(const std::string& path, InputShape& out,
std::string& why) {
std::error_code ec;
const auto size = std::filesystem::file_size(path, ec);
if (ec || size == 0) {
why = "the model is not there to read";
return false;
}
detail::FileSource s;
s.f.open(path, std::ios::binary);
s.size = int64_t(size);
if (!s.f) {
why = "the model could not be opened";
return false;
}
return detail::read(s, int64_t(size), out, why);
}
} // namespace onnxshape
} // namespace ANSCENTER
#endif // ANSONNXSHAPE_H
+326
View File
@@ -0,0 +1,326 @@
#ifndef ANSPACKAGEBITS_H
#define ANSPACKAGEBITS_H
#pragma once
// ANSPackageBits — base64, SHA-256 and HMAC-SHA256 for the model package.
//
// TAKEN FROM ANSMINDCORE (src/core/base64.hpp, src/core/sha256.hpp) unchanged,
// for the same reason the AEAD is: a package written by one product must open
// in the other, and two implementations of the same transform are two chances
// to disagree. The suite checks these against the published vectors.
#include <array>
#include <cstdint>
#include <cstring>
#include <string>
#include <vector>
namespace ANSCENTER {
namespace bits {
// core/base64 — RFC 4648 base64, encode and decode.
//
// Needed by ONVIF's WS-Security UsernameToken (the nonce and the password
// digest both travel base64-encoded) and useful anywhere bytes must cross a
// text channel. Decode is strict about alphabet and padding: this parses
// values that arrive from a camera on the network, and quietly accepting
// malformed input is how a length assumption downstream turns into a bug.
#include <array>
#include <cstdint>
#include <string>
#include <vector>
// ── base64 ───────────────────────────────────────────────────────────────
inline std::string base64Encode(const uint8_t* p, size_t n) {
static const char* kAlpha =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string out;
out.reserve(((n + 2) / 3) * 4);
size_t i = 0;
for (; i + 2 < n; i += 3) {
const uint32_t v = uint32_t(p[i]) << 16 | uint32_t(p[i + 1]) << 8 | p[i + 2];
out += kAlpha[(v >> 18) & 0x3F];
out += kAlpha[(v >> 12) & 0x3F];
out += kAlpha[(v >> 6) & 0x3F];
out += kAlpha[v & 0x3F];
}
if (i + 1 == n) {
const uint32_t v = uint32_t(p[i]) << 16;
out += kAlpha[(v >> 18) & 0x3F];
out += kAlpha[(v >> 12) & 0x3F];
out += "==";
} else if (i + 2 == n) {
const uint32_t v = uint32_t(p[i]) << 16 | uint32_t(p[i + 1]) << 8;
out += kAlpha[(v >> 18) & 0x3F];
out += kAlpha[(v >> 12) & 0x3F];
out += kAlpha[(v >> 6) & 0x3F];
out += '=';
}
return out;
}
inline std::string base64Encode(const std::string& s) {
return base64Encode(reinterpret_cast<const uint8_t*>(s.data()), s.size());
}
template <size_t N>
inline std::string base64Encode(const std::array<uint8_t, N>& a) {
return base64Encode(a.data(), a.size());
}
// Returns false on any character outside the alphabet, bad padding, or a
// length that is not a multiple of 4. Whitespace is skipped: XML puts
// newlines inside base64 elements.
inline bool base64Decode(const std::string& in, std::vector<uint8_t>& out) {
auto val = [](char c) -> int {
if (c >= 'A' && c <= 'Z') return c - 'A';
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
if (c >= '0' && c <= '9') return c - '0' + 52;
if (c == '+') return 62;
if (c == '/') return 63;
return -1;
};
constexpr int kPad = -2;
out.clear();
int quad[4] = {0, 0, 0, 0};
int have = 0;
bool padded = false; // once padding starts, only more padding may follow
for (char c : in) {
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') continue;
int v;
if (c == '=') {
v = kPad;
padded = true;
} else {
if (padded) return false; // data after padding
v = val(c);
if (v < 0) return false;
}
quad[have++] = v;
if (have < 4) continue;
// Padding is only legal in the last one or two positions.
if (quad[0] == kPad || quad[1] == kPad) return false;
if (quad[2] == kPad && quad[3] != kPad) return false;
const int pad = (quad[2] == kPad ? 2 : 0) + (quad[2] != kPad &&
quad[3] == kPad ? 1 : 0);
const uint32_t acc = uint32_t(quad[0]) << 18 | uint32_t(quad[1]) << 12 |
uint32_t(pad >= 2 ? 0 : quad[2]) << 6 |
uint32_t(pad >= 1 ? 0 : quad[3]);
out.push_back(uint8_t(acc >> 16));
if (pad < 2) out.push_back(uint8_t(acc >> 8));
if (pad < 1) out.push_back(uint8_t(acc));
have = 0;
}
return have == 0; // a trailing partial group is malformed
}
// core/sha256 — SHA-256 and HMAC-SHA256, in-tree.
//
// WHY IN-TREE. The only consumer is AWS Signature Version 4 request signing
// for the S3 evidence store. Linking OpenSSL for two hash functions would put
// a versioned crypto dependency into the worker's Depends on every platform
// we ship — the same trap the bundled ONNX Runtime exists to avoid — in
// exchange for ~150 lines of algorithm that has fixed, published test
// vectors. The suite checks this against the FIPS 180-4 and RFC 4231 vectors,
// so it is verified rather than merely present.
//
// NOT a general crypto library. It exists to sign requests: there is no
// constant-time comparison here, and nothing in this file should ever be
// used for password hashing or key derivation from a low-entropy secret.
#include <array>
#include <cstdint>
#include <cstring>
#include <string>
// ── SHA-256 and HMAC-SHA256 ──────────────────────────────────────────────
class Sha256 {
public:
static constexpr size_t kDigestLen = 32;
Sha256() { reset(); }
void reset() {
len_ = 0;
bufLen_ = 0;
static const uint32_t kInit[8] = {0x6a09e667u, 0xbb67ae85u, 0x3c6ef372u,
0xa54ff53au, 0x510e527fu, 0x9b05688cu,
0x1f83d9abu, 0x5be0cd19u};
std::memcpy(h_, kInit, sizeof h_);
}
void update(const uint8_t* p, size_t n) {
len_ += n;
while (n) {
const size_t take = (n < 64 - bufLen_) ? n : (64 - bufLen_);
std::memcpy(buf_ + bufLen_, p, take);
bufLen_ += take;
p += take;
n -= take;
if (bufLen_ == 64) {
block(buf_);
bufLen_ = 0;
}
}
}
void update(const std::string& s) {
update(reinterpret_cast<const uint8_t*>(s.data()), s.size());
}
// Consumes the state; call reset() to reuse.
void finish(uint8_t out[kDigestLen]) {
const uint64_t bits = uint64_t(len_) * 8; // captured BEFORE padding
uint8_t b = 0x80;
update(&b, 1);
b = 0;
while (bufLen_ != 56) update(&b, 1);
uint8_t lenBe[8];
for (int i = 0; i < 8; ++i) lenBe[i] = uint8_t(bits >> (56 - 8 * i));
update(lenBe, 8);
for (int i = 0; i < 8; ++i) {
out[i * 4 + 0] = uint8_t(h_[i] >> 24);
out[i * 4 + 1] = uint8_t(h_[i] >> 16);
out[i * 4 + 2] = uint8_t(h_[i] >> 8);
out[i * 4 + 3] = uint8_t(h_[i]);
}
}
private:
static uint32_t rotr(uint32_t x, int n) {
return (x >> n) | (x << (32 - n));
}
void block(const uint8_t* p) {
// First 32 bits of the fractional parts of the cube roots of the first
// 64 primes (FIPS 180-4 §4.2.2).
static const uint32_t k[64] = {
0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u, 0x3956c25bu,
0x59f111f1u, 0x923f82a4u, 0xab1c5ed5u, 0xd807aa98u, 0x12835b01u,
0x243185beu, 0x550c7dc3u, 0x72be5d74u, 0x80deb1feu, 0x9bdc06a7u,
0xc19bf174u, 0xe49b69c1u, 0xefbe4786u, 0x0fc19dc6u, 0x240ca1ccu,
0x2de92c6fu, 0x4a7484aau, 0x5cb0a9dcu, 0x76f988dau, 0x983e5152u,
0xa831c66du, 0xb00327c8u, 0xbf597fc7u, 0xc6e00bf3u, 0xd5a79147u,
0x06ca6351u, 0x14292967u, 0x27b70a85u, 0x2e1b2138u, 0x4d2c6dfcu,
0x53380d13u, 0x650a7354u, 0x766a0abbu, 0x81c2c92eu, 0x92722c85u,
0xa2bfe8a1u, 0xa81a664bu, 0xc24b8b70u, 0xc76c51a3u, 0xd192e819u,
0xd6990624u, 0xf40e3585u, 0x106aa070u, 0x19a4c116u, 0x1e376c08u,
0x2748774cu, 0x34b0bcb5u, 0x391c0cb3u, 0x4ed8aa4au, 0x5b9cca4fu,
0x682e6ff3u, 0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u,
0x90befffau, 0xa4506cebu, 0xbef9a3f7u, 0xc67178f2u};
uint32_t w[64];
for (int i = 0; i < 16; ++i)
w[i] = uint32_t(p[i * 4]) << 24 | uint32_t(p[i * 4 + 1]) << 16 |
uint32_t(p[i * 4 + 2]) << 8 | uint32_t(p[i * 4 + 3]);
for (int i = 16; i < 64; ++i) {
const uint32_t s0 =
rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >> 3);
const uint32_t s1 =
rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >> 10);
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
}
uint32_t a = h_[0], b = h_[1], c = h_[2], d = h_[3];
uint32_t e = h_[4], f = h_[5], g = h_[6], hh = h_[7];
for (int i = 0; i < 64; ++i) {
const uint32_t S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
const uint32_t ch = (e & f) ^ (~e & g);
const uint32_t t1 = hh + S1 + ch + k[i] + w[i];
const uint32_t S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
const uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
const uint32_t t2 = S0 + maj;
hh = g; g = f; f = e; e = d + t1;
d = c; c = b; b = a; a = t1 + t2;
}
h_[0] += a; h_[1] += b; h_[2] += c; h_[3] += d;
h_[4] += e; h_[5] += f; h_[6] += g; h_[7] += hh;
}
uint32_t h_[8];
uint8_t buf_[64];
size_t bufLen_ = 0;
uint64_t len_ = 0;
};
using Digest256 = std::array<uint8_t, 32>;
inline Digest256 sha256(const uint8_t* p, size_t n) {
Sha256 s;
s.update(p, n);
Digest256 out{};
s.finish(out.data());
return out;
}
inline Digest256 sha256(const std::string& s) {
return sha256(reinterpret_cast<const uint8_t*>(s.data()), s.size());
}
inline std::string toHex(const uint8_t* p, size_t n) {
static const char* kHex = "0123456789abcdef";
std::string out;
out.reserve(n * 2);
for (size_t i = 0; i < n; ++i) {
out += kHex[p[i] >> 4];
out += kHex[p[i] & 0xF];
}
return out;
}
inline std::string toHex(const Digest256& d) {
return toHex(d.data(), d.size());
}
inline std::string sha256Hex(const uint8_t* p, size_t n) {
return toHex(sha256(p, n));
}
inline std::string sha256Hex(const std::string& s) {
return toHex(sha256(s));
}
// RFC 2104 HMAC with SHA-256.
inline Digest256 hmacSha256(const uint8_t* key, size_t keyLen,
const uint8_t* msg, size_t msgLen) {
uint8_t k[64] = {0};
if (keyLen > 64) {
Sha256 s;
s.update(key, keyLen);
s.finish(k); // remaining 32 bytes stay zero-padded
} else {
std::memcpy(k, key, keyLen);
}
uint8_t ipad[64], opad[64];
for (int i = 0; i < 64; ++i) {
ipad[i] = uint8_t(k[i] ^ 0x36);
opad[i] = uint8_t(k[i] ^ 0x5c);
}
uint8_t inner[32];
{
Sha256 s;
s.update(ipad, 64);
s.update(msg, msgLen);
s.finish(inner);
}
Digest256 out{};
{
Sha256 s;
s.update(opad, 64);
s.update(inner, 32);
s.finish(out.data());
}
return out;
}
inline Digest256 hmacSha256(const std::string& key, const std::string& msg) {
return hmacSha256(reinterpret_cast<const uint8_t*>(key.data()), key.size(),
reinterpret_cast<const uint8_t*>(msg.data()), msg.size());
}
inline Digest256 hmacSha256(const Digest256& key, const std::string& msg) {
return hmacSha256(key.data(), key.size(),
reinterpret_cast<const uint8_t*>(msg.data()), msg.size());
}
} // namespace bits
} // namespace ANSCENTER
#endif // ANSPACKAGEBITS_H
+31
View File
@@ -0,0 +1,31 @@
# ANSModelPackage — reading an ANS model package (.ansmodel).
#
# Header-only apart from the vendored curves, and deliberately free of the
# heavy dependencies around it: no CUDA, no OpenCV, no LabVIEW. That is what
# lets it build — and be tested — on every platform, including the ones where
# the engine layer does not build at all.
add_library(ANSModelPackage STATIC
${CMAKE_SOURCE_DIR}/3rdparty/monocypher/monocypher.c
${CMAKE_SOURCE_DIR}/3rdparty/monocypher/monocypher-ed25519.c
)
target_include_directories(ANSModelPackage PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/3rdparty/monocypher
)
# nlohmann/json comes from vcpkg on Windows and the system elsewhere, the same
# way ANSFR already takes it.
if(WIN32)
target_include_directories(ANSModelPackage PUBLIC ${VCPKG_INCLUDE_DIR})
else()
find_path(ANSCORE_NLOHMANN_INCLUDE nlohmann/json.hpp
HINTS /opt/homebrew/include /usr/local/include /usr/include)
if(ANSCORE_NLOHMANN_INCLUDE)
target_include_directories(ANSModelPackage PUBLIC ${ANSCORE_NLOHMANN_INCLUDE})
else()
message(WARNING "nlohmann/json.hpp not found — ANSModelPackage will not build")
endif()
endif()
set_target_properties(ANSModelPackage PROPERTIES POSITION_INDEPENDENT_CODE ON)
+17 -14
View File
@@ -8,9 +8,11 @@ const unsigned char BitStream3::hiMask[] = { 0, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0x
BitStream3::BitStream3() :
ownMemory(false),
buffer(NULL),
bufSize(0),
bitSize(0),
currentBit(0),
currentByte(0),
buffer(NULL)
currentByte(0)
{
}
@@ -41,6 +43,7 @@ void BitStream3::Attach(void * buf, int bitCount)
{
buffer = (unsigned char *)buf;
bitSize = bitCount;
bufSize = (bitCount + 7) >> 3;
currentBit = 0;
currentByte = 0;
}
@@ -53,20 +56,20 @@ int BitStream3::Write(const unsigned char * buf, int bitCount)
{
if ((gap & 7) != 0)
{
buffer[currentByte] = (unsigned char)((buffer[currentByte] << gap) | (buf[0] >> (bitCount < 8 ? bitCount - gap : 8 - gap)));
setByte(currentByte, (unsigned char)((byteAt(currentByte) << gap) | (buf[0] >> (bitCount < 8 ? bitCount - gap : 8 - gap))));
currentBit += gap; bitCount -= gap; currentByte++;
for (; bitCount >= 8; i++, currentByte++, currentBit += 8, bitCount -= 8)
buffer[currentByte] = (unsigned char)((buf[i] << gap) | (buf[i + 1] >> (8 - gap)));
setByte(currentByte, (unsigned char)((buf[i] << gap) | (buf[i + 1] >> (8 - gap))));
}
else
for (; bitCount >= 8; currentByte++, i++, currentBit += 8, bitCount -= 8)
buffer[currentByte] = buf[i];
setByte(currentByte, buf[i]);
}
if (bitCount > 0)
{
buffer[currentByte] = (unsigned char)((buffer[currentByte] << bitCount) | (buf[i] & ((1 << bitCount) - 1)));
setByte(currentByte, (unsigned char)((byteAt(currentByte) << bitCount) | (buf[i] & ((1 << bitCount) - 1))));
currentBit += bitCount;
}
@@ -86,20 +89,20 @@ int BitStream3::Read(unsigned char * readBuf, int bitCount)
if ((gap & 7) != 0)
{
for (; bitCount >= 8 + 8 - gap; bitCount -= 8, currentByte++, currentBit += 8, i++)
readBuf[i] = (unsigned char)((buffer[currentByte] << gap) | (buffer[currentByte + 1] >> (8 - gap)));
readBuf[i] = (unsigned char)((byteAt(currentByte) << gap) | (byteAt(currentByte + 1) >> (8 - gap)));
// byte align
// byte align
if (bitCount > 0)
{
if (bitCount < 8 - gap)
{
readBuf[i] = (unsigned char)((buffer[currentByte] >> (8 - gap - bitCount)) & ((1 << bitCount) - 1));
readBuf[i] = (unsigned char)((byteAt(currentByte) >> (8 - gap - bitCount)) & ((1 << bitCount) - 1));
currentBit += bitCount;
bitCount = 0;
}
else
{
readBuf[i] = (unsigned char)(buffer[currentByte] & ((1 << (8 - gap)) - 1));
readBuf[i] = (unsigned char)(byteAt(currentByte) & ((1 << (8 - gap)) - 1));
bitCount -= (8 - gap);
currentBit += (8 - gap);
@@ -111,15 +114,15 @@ int BitStream3::Read(unsigned char * readBuf, int bitCount)
}
else
for (; bitCount >= 8; bitCount -= 8, currentByte++, currentBit += 8, i++)
readBuf[i] = buffer[currentByte];
readBuf[i] = byteAt(currentByte);
// already aligned
if (bitCount > 0)
{
if (currentBit + 8 <= bitSize) // not last byte
readBuf[i] |= (unsigned char)((buffer[currentByte] >> (8 - bitCount)) & ((1 << bitCount) - 1));
else // last byte
readBuf[i] = (unsigned char)((buffer[currentByte] >> ((bitSize & 7) - bitCount)) & ((1 << bitCount) - 1));
readBuf[i] |= (unsigned char)((byteAt(currentByte) >> (8 - bitCount)) & ((1 << bitCount) - 1));
else // last byte
readBuf[i] = (unsigned char)((byteAt(currentByte) >> ((bitSize & 7) - bitCount)) & ((1 << bitCount) - 1));
currentBit += bitCount;
}
+14
View File
@@ -16,6 +16,20 @@ public:
unsigned char * GetBuffer();
protected:
// Bounds-safe accessors: every Write/Read goes through these so a caller
// that under-sizes the stream can no longer corrupt the heap (the 125-vs-147
// bug in GetCurrentHardwareId overflowed 3 bytes for years). Out-of-range
// reads return 0 — identical to the base32 padding zeros the hardware-id
// Match path has always effectively read there.
unsigned char byteAt(int i) const
{
return (buffer && i >= 0 && i < bufSize) ? buffer[i] : 0;
}
void setByte(int i, unsigned char v)
{
if (buffer && i >= 0 && i < bufSize) buffer[i] = v;
}
bool ownMemory;
unsigned char * buffer;
int bufSize, bitSize;
+139 -6
View File
@@ -21,9 +21,10 @@
#include <net/if.h>
#ifdef __linux__
# include <sys/ioctl.h>
# include <sys/stat.h> // stat() — physical-NIC test in linuxIsPhysicalNic
# include <netinet/in.h>
# include <unistd.h>
# include <string.h>
# include <string.h> // strncmp/strlen (same test)
// Match Linux to FreeBSD
# define AF_LINK AF_PACKET
@@ -82,6 +83,47 @@ void GetCPUIDPropertyList(std::list<std::string>* propList)
propList->push_back(buf);
}
#ifdef __linux__
// A PHYSICAL network interface, i.e. one backed by a real hardware device.
//
// Why this exists: the hardware id is built from up to 6 enumerated MACs, and
// on Linux getifaddrs() also returns VIRTUAL interfaces — docker0 and the
// veth* pairs a container creates, tun*/wg* from a VPN, br-*/virbr* bridges.
// Container veths get RANDOM MACs and come and go with the containers, so an
// unfiltered enumeration can hand back a different set on every call: the id
// changes and an activated licence silently stops matching its machine. Boxes
// running Docker (the Coder feature) hit this routinely.
//
// The test mirrors what the Windows branch already does via WMI ("PNPDeviceId
// LIKE %PCI%" — physical adapters only). /sys/class/net/<name>/device is a
// symlink to the backing device and exists ONLY for hardware-backed
// interfaces; virtual ones have no such link. That is the kernel's own answer,
// so it stays correct as new virtual-interface flavours appear — unlike a name
// denylist, which is kept only as a cheap second line of defence.
static bool linuxIsPhysicalNic(const char* ifname)
{
if (ifname == NULL || ifname[0] == '\0') return false;
// Names that are virtual by construction. Not exhaustive on purpose — the
// /sys check below is the real test; this only catches oddities where /sys
// is unavailable (unusual containers, minimal namespaces).
static const char* kVirtualPrefixes[] = {
"lo", "docker", "veth", "br-", "virbr", "tun", "tap", "wg",
"vmnet", "vboxnet", "zt", "ppp", "dummy", "kube", "cni", "flannel",
"lxc", "lxd", "nerdctl", "podman", "cali", "weave"
};
for (const char* prefix : kVirtualPrefixes) {
if (strncmp(ifname, prefix, strlen(prefix)) == 0) return false;
}
std::string path = std::string("/sys/class/net/") + ifname + "/device";
struct stat st;
// stat() (not lstat) so the symlink is followed — we want "the target
// exists", i.e. a real backing device.
return stat(path.c_str(), &st) == 0;
}
#endif // __linux__
void GetMACPropertyList(std::list<std::string>* propList, int maxCount)
{
// Head of the interface address linked list
@@ -99,6 +141,14 @@ void GetMACPropertyList(std::list<std::string>* propList, int maxCount)
return;
}
#ifdef __linux__
// Collected separately so physical NICs can be preferred wholesale. Taking
// them inline would let a veth that merely appears EARLIER in the list
// consume one of the maxCount slots and push a real NIC out.
std::list<std::string> physical;
std::list<std::string> fallback;
#endif
while (current != NULL && maxCount > 0) {
const sockaddr_in* interfaceAddress = castToIP4(current->ifa_addr);
@@ -127,8 +177,18 @@ void GetMACPropertyList(std::list<std::string>* propList, int maxCount)
{
char buf[18];
sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);
#ifdef __linux__
// Bucket now, choose after the walk (see the lists above).
// maxCount is NOT decremented here: the cap applies to the
// SELECTED set, not to whatever happened to be enumerated.
if (linuxIsPhysicalNic(current->ifa_name))
physical.push_back(buf);
else
fallback.push_back(buf);
#else
propList->push_back(buf);
maxCount--;
#endif
}
}
@@ -137,6 +197,21 @@ void GetMACPropertyList(std::list<std::string>* propList, int maxCount)
freeifaddrs(ifap);
ifap = NULL;
#ifdef __linux__
// Physical NICs only — that is the whole point. FAIL-SAFE: if the filter
// found none (no /sys, an exotic NIC driver, a network namespace with only
// virtual links) fall back to the unfiltered set rather than returning
// nothing. An empty list would drop ALL MAC entropy from the id, and with
// the system-UUID and disk-serial slots already constant on Linux that
// would leave near-identical ids across different machines — far worse
// than an id that occasionally drifts.
std::list<std::string>& chosen = physical.empty() ? fallback : physical;
for (std::list<std::string>::const_iterator it = chosen.begin();
it != chosen.end() && maxCount > 0; ++it, --maxCount) {
propList->push_back(*it);
}
#endif
}
void GetHDDPropertyList(std::list<std::string>* propList, int maxCount)
@@ -220,7 +295,13 @@ const char* HardwareId::GetCurrentHardwareId()
WmiHelper wmi;
#endif
BitStream3 bits(125);
// 147 bits are actually written below (3 version + 9 x 16-bit hashes). The
// previous size of 125 allocated only 16 bytes and the last two hash slots
// overflowed the heap block by 3 bytes (asymptomatic on the Windows heap,
// SIGSEGV under macOS malloc — found when ANSGateway enabled this code on
// POSIX). Only the first 16 bytes feed the base32 encode, so sizing the
// stream correctly changes no id on any platform.
BitStream3 bits(147);
list<string> propList;
unsigned char version = 1;
unsigned short hash1, hash2, hash3, hash4;
@@ -256,6 +337,8 @@ const char* HardwareId::GetCurrentHardwareId()
bits.WriteUInt16(hash1);
bool uuidFound = (hash1 != h0);
// CPU IDs - get up to 4 for better resilience
propList.clear();
@@ -270,6 +353,8 @@ const char* HardwareId::GetCurrentHardwareId()
cpu = NormalizeHardwareString(cpu);
}
int cpuCount = (int)propList.size();
hash1 = (propList.size() > 0) ? HashString(propList.front().c_str()) : h0;
hash2 = (propList.size() > 1) ? HashString(propList.back().c_str()) : h0;
@@ -298,6 +383,8 @@ const char* HardwareId::GetCurrentHardwareId()
}
propList.sort();
int macCount = (int)propList.size();
// Store up to 4 MAC hashes
auto macIter = propList.begin();
hash1 = (propList.size() > 0 && macIter != propList.end()) ? HashString((macIter++)->c_str()) : h0;
@@ -325,6 +412,8 @@ const char* HardwareId::GetCurrentHardwareId()
}
propList.sort();
int hddCount = (int)propList.size();
auto hddIter = propList.begin();
hash1 = (propList.size() > 0 && hddIter != propList.end()) ? HashString((hddIter++)->c_str()) : h0;
hash2 = (propList.size() > 1 && hddIter != propList.end()) ? HashString((hddIter++)->c_str()) : h0;
@@ -351,11 +440,34 @@ const char* HardwareId::GetCurrentHardwareId()
insertPos += 6;
}
// Field-diagnosability: log the component availability once per distinct
// result so support can tell a weak fingerprint from a healthy one
static string lastLoggedHwid;
if (hardwareId != lastLoggedHwid)
{
lastLoggedHwid = hardwareId;
fprintf(stderr, "[anslicensing] hardware id %s (components: uuid=%s cpu=%d mac=%d hdd=%d)\n",
hardwareId.c_str(), uuidFound ? "ok" : "none", cpuCount, macCount, hddCount);
if (!uuidFound && cpuCount == 0 && macCount == 0)
fprintf(stderr, "[anslicensing] WARNING: no hardware components readable via WMI - hardware id is NOT machine-specific. "
"Check WMI health: winmgmt /verifyrepository (then winmgmt /salvagerepository if inconsistent)\n");
}
return hardwareId.c_str();
}
bool HardwareId::MatchCurrentHardwareId(const char* hwid)
{
// An HWID identical to the one this machine generates right now is by
// definition this computer. The points-based matching below can score
// under REQUIRED_POINTS on machines with an invalid SMBIOS UUID and few
// fingerprintable components (no PCI NIC, no readable disk serial), which
// would reject even a freshly generated local HWID.
const char* currentHwid = GetCurrentHardwareId();
if (currentHwid != NULL && strcmp(hwid, currentHwid) == 0)
return true;
#ifdef _WIN32
WmiHelper wmi;
#endif
@@ -383,8 +495,14 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
auto buf = base32.decode(hardwareId.c_str(), hardwareId.length(), &bufLen);
// The field layout is 147 bits but the HWID only encodes 16 bytes; zero-pad
// the buffer so reading the trailing (truncated) HDD fields stays in bounds
// and yields deterministic values instead of an out-of-bounds read.
if (buf.size() < 20)
buf.resize(20, 0);
BitStream3 bits;
bits.Attach(buf.data(), 125);
bits.Attach(buf.data(), 160);
unsigned char version = 0;
bits.Read(&version, 3);
@@ -398,6 +516,7 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
// Use a points-based matching system for resilience
int matchPoints = 0;
bool uuidMatched = false;
const int REQUIRED_POINTS = 2; // Need at least 2 components to match
// Read UUID hash
@@ -430,6 +549,7 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
}
}
matchPoints += 2; // UUID/Memory match gives 2 points
uuidMatched = true;
}
else
{
@@ -437,9 +557,11 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
#ifdef _WIN32
wmi.GetPropertyList("SELECT * FROM Win32_ComputerSystemProduct", "UUID", &propList);
#endif
if (propList.size() > 0)
// The generator hashes the last valid UUID entry, so accept a match
// against any entry rather than only the first one
for (list<string>::iterator iter = propList.begin(); iter != propList.end(); iter++)
{
string s = NormalizeHardwareString(propList.front());
string s = NormalizeHardwareString(*iter);
if ((s.length() > 1)
&& (s != "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
@@ -448,6 +570,8 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
if (hash1 != h0 && hash1 == HashString(s.c_str()))
{
matchPoints += 2; // UUID match is worth 2 points (most stable)
uuidMatched = true;
break;
}
}
}
@@ -558,5 +682,14 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
// - UUID (2) + MAC (1) = 3 points ✓
// - UUID (2) + HDD (1) = 3 points ✓
// - CPU (1) + MAC (1) + HDD (1) = 3 points ✓ (if UUID changed but hardware same)
return matchPoints >= REQUIRED_POINTS;
bool accepted = (matchPoints >= REQUIRED_POINTS);
// Only reached when the exact-string precheck missed, i.e. the stored HWID
// differs from the one this machine generates now. Log the breakdown so a
// status-12 rejection in the field is diagnosable from the console output.
fprintf(stderr, "[anslicensing] hardware id fuzzy match for %s: uuid=%s cpuMatches=%d macMatches=%d hddMatches=%d -> %d point(s), %d required -> %s\n",
hwid, uuidMatched ? "match" : "no-match", cpuMatches, macMatches, hddMatches,
matchPoints, REQUIRED_POINTS, accepted ? "accepted" : "REJECTED");
return accepted;
}
+33 -8
View File
@@ -2,6 +2,7 @@
#include <memory>
#include <vector>
#include <cstdio>
#include "except.h"
#include "template.h"
@@ -172,13 +173,15 @@ public:
if (!licenseKeyValidationData.empty())
validator.SetValidationData("LicenseKeyValidationData", licenseKeyValidationData.data(), (activationTemplate->GetValidationDataSize() + 7) >> 3);
try
try
{
validator.SetKey(activationKey.c_str());
}
catch (Exception * ex)
{
ex->Destroy();
fprintf(stderr, "[anslicensing] activation key is %s (SetKey failed) - product needs (re)activation\n",
activationKey.empty() ? "empty" : "malformed");
activationStatus = STATUS_INVALID_ACTIVATION_KEY;
if (ownActivationTemplate) delete activationTemplate;
return false;
@@ -186,6 +189,7 @@ public:
if (!validator.IsKeyValid())
{
fprintf(stderr, "[anslicensing] activation key does not validate against this license key + hardware id\n");
activationStatus = STATUS_INVALID_ACTIVATION_KEY;
if (ownActivationTemplate) delete activationTemplate;
return false;
@@ -222,6 +226,7 @@ public:
{
if (timeValidationMethod == USE_INTERNET_TIME) // internet time was mandatory
{
fprintf(stderr, "[anslicensing] could not obtain internet time (NTP) and internet time is mandatory - check network/firewall\n");
activationStatus = STATUS_NET_ERROR;
if (ownActivationTemplate) delete activationTemplate;
return false;
@@ -239,14 +244,18 @@ public:
if (diffSeconds < 0)
{
m_licenseValidityDays = licenseTemplate->GetLicenseGracePeriod() - (int)(((time_t)(-diffSeconds) + 86400 - 1) / 86400);
if (m_licenseValidityDays < 0)
m_licenseValidityDays = 0;
fprintf(stderr, "[anslicensing] license expired on %04d-%02d-%02d (grace days remaining: %d)\n",
1900 + licenseExpirationTime.tm_year, 1 + licenseExpirationTime.tm_mon, licenseExpirationTime.tm_mday,
m_licenseValidityDays);
activationStatus = STATUS_LICENSE_EXPIRED;
if (ownActivationTemplate) delete activationTemplate;
return false;
} else
m_licenseValidityDays = licenseTemplate->GetLicenseGracePeriod() + (int)(((time_t)diffSeconds + 86400 - 1) / 86400);
@@ -465,6 +474,9 @@ public:
generator.SetValidationData("LicenseKeyValidationData", licenseKeyValidationData.data(), licenseKeyValidationDataLen * 8);
}
fprintf(stderr, "[anslicensing] requesting activation key from licensing service: %s (hwid %s)\n",
activationTemplate->GetLicensingServiceUrl(), hardwareId.c_str());
try {
activationKey = generator.GenerateKey();
}
@@ -472,19 +484,29 @@ public:
{
if ((ex->GetCode() == STATUS_NET_ERROR) && (generator.GetSigningServiceStatusCode() == 406))
{
fprintf(stderr, "[anslicensing] licensing service returned HTTP 406 - payment required\n");
m_licenseValidationResult.SetPaymentRequired(true);
ex->Destroy();
}
else
{
fprintf(stderr, "[anslicensing] activation request failed (service HTTP status %d) - check network access to the licensing service\n",
generator.GetSigningServiceStatusCode());
throw;
}
}
if (ValidateActivationKey(activationTemplate.get(), false))
{
fprintf(stderr, "[anslicensing] activation key acquired and validated (service HTTP status %d)\n",
generator.GetSigningServiceStatusCode());
activationStatus = STATUS_SUCCESS;
}
else
{
fprintf(stderr, "[anslicensing] licensing service returned an activation key that does not validate for this hardware id\n");
throw new LicensingException(STATUS_GENERIC_ERROR, "internal error - invalid returned activation key");
}
m_licenseValidationResult.m_license = std::make_unique<LicenseImpl>();
@@ -531,10 +553,12 @@ public:
const char * GetCurrentHardwareIdCallback()
{
if (parentUsesWideChar)
if (parentUsesWideChar)
{
static string hwid = w2s(GetCurrentHardwareIdW(parentLicensingClient));
return hwid.c_str();
// per-instance storage: a function-local static would freeze the
// first client's HWID for the lifetime of the process
currentHwidCache = w2s(GetCurrentHardwareIdW(parentLicensingClient));
return currentHwidCache.c_str();
}
else
{
@@ -587,6 +611,7 @@ private:
string licenseKey;
string activationKey;
string hardwareId;
string currentHwidCache;
string licensingServiceUrl;
vector<unsigned char> licenseKeyValidationData;
+46 -24
View File
@@ -2,6 +2,7 @@
#define _WIN32_DCOM
#include <iostream>
#include <cstdio>
#include <comdef.h>
#include <Wbemidl.h>
#include <list>
@@ -45,17 +46,19 @@ public:
);
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &wbemLocator);
if (FAILED(hres))
{
if (ownCoInitialize)
CoUninitialize();
throw new exception("failed to create IWbemLocator", hres);
// Fail soft: leave wbemServices NULL so queries return empty results.
// Throwing here would escape callers (e.g. hardware id generation)
// that have no exception handling and crash the host application.
ReportFailure("CoCreateInstance(IWbemLocator)", hres);
wbemLocator = NULL;
return;
}
// Connect to the root\cimv2 namespace with the
@@ -76,12 +79,11 @@ public:
if (FAILED(hres))
{
ReportFailure("ConnectServer(ROOT\\CIMV2)", hres);
wbemLocator->Release();
if (ownCoInitialize)
CoUninitialize();
throw new exception("could not connect to WMI", hres);
wbemLocator = NULL;
wbemServices = NULL;
return;
}
// Set the IWbemServices proxy so that impersonation
@@ -99,10 +101,12 @@ public:
if (FAILED(hres))
{
wbemServices->Release();
wbemLocator->Release();
if (ownCoInitialize) CoUninitialize();
throw new exception("could not connecto to WMI", hres);
ReportFailure("CoSetProxyBlanket", hres);
wbemServices->Release();
wbemLocator->Release();
wbemServices = NULL;
wbemLocator = NULL;
return;
}
}
@@ -115,32 +119,44 @@ public:
void GetPropertyList(const char * wqlQuery, const char * propertyName, list<string> * propList, int maxElements = 0x7FFFFFFF)
{
if (wbemServices == NULL)
return; // WMI unavailable; already reported by the constructor
HRESULT hres;
// For example, query for all the running processes
IEnumWbemClassObject* pEnumerator = NULL;
hres = wbemServices->ExecQuery(
bstr_t("WQL"),
bstr_t("WQL"),
bstr_t(wqlQuery),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres))
{
throw new exception("WMI query failed");
ReportFailure(wqlQuery, hres);
return;
}
else
{
{
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
int numElements = 0;
while (1)
{
hres = pEnumerator->Next(WBEM_INFINITE, 1,
hres = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if (FAILED(hres))
{
// An enumeration error is not "no more rows" — report it,
// otherwise a broken WMI silently yields an empty fingerprint
ReportFailure(wqlQuery, hres);
break;
}
if(0 == uReturn)
{
break;
@@ -203,6 +219,12 @@ public:
}
private:
static void ReportFailure(const char * what, HRESULT hres)
{
fprintf(stderr, "[anslicensing] WMI failure (hr=0x%08lX): %s — hardware fingerprint will be degraded\n",
(unsigned long)hres, what);
}
bool ownCoInitialize;
IWbemServices * wbemServices;
IWbemLocator * wbemLocator;
+79
View File
@@ -0,0 +1,79 @@
# ANSAWSS3 — return codes and retry behaviour
Reference for callers of the `*ANSAWSHandle` exports in `ANSUtilities.dll`
(LabVIEW, C++, .NET). Source of truth: `modules/ANSUtilities/ANSUtilities.h`
(`kCall*` constants) and `modules/ANSUtilities/dllmain.cpp`.
## The one rule
**`rc == 1` means success. Everything else is a failure.** Success is the only
positive value, so `rc == 1` and `rc != 1` are both always correct tests. The
negative values exist so a caller can tell *why* it failed, and therefore
whether retrying is worthwhile.
## Upload functions
Applies to every `Upload*ANSAWSHandle`, `Upload*ANSAWSHandle_CPP` and
`Upload*ANSAWSHandle_V2` export (JPEG, binary, text, file-stream, multipart,
with or without prefix).
| Code | Meaning | Retry? |
|-----:|---------|--------|
| `1` | Uploaded. `uploadedFilePath` holds the object URL. | — |
| `0` | A required pointer argument was `NULL`. | **No** — caller bug |
| `-1` | Handle is not in the registry: never created, already released, or a stale value. | **No** — re-create the handle |
| `-2` | Failed, reason not classified. Should not occur; kept as a fallback. | Yes |
| `-3` | A `std::exception` escaped. | Yes |
| `-4` | An unknown exception escaped. | Yes |
| `-20` | **Permanent.** Bad argument (empty bucket/filename, part size < 5 MB, unreadable file), invalid licence or unlock code, or S3 answered with a 4xx — wrong bucket, no permission, bad key. | **No** — fix the configuration; retrying burns time and changes nothing |
| `-21` | **Not connected.** `Connect()` has not succeeded yet, or the link went down and the background retry thread is still working on it. | **Yes, later** — back off a few seconds |
| `-22` | **Transient.** Network error, timeout, or HTTP 5xx, still failing after the DLL's own internal retries. | **Yes** |
A practical LabVIEW policy:
* `rc = 1` → done.
* `rc = -20` or `0` → log it and stop; it will never succeed as-is.
* `rc = -21` → queue the image and try again in a few seconds.
* anything else negative → retry, with backoff.
Note that `-22` already means the DLL retried internally (up to 20 attempts
within a 30 s budget) before giving up, so an outer retry should not be tight.
## Connect
`ConnectANSAWSHandle` / `_V2`:
| Code | Meaning |
|-----:|---------|
| `1` | Connected; ready for uploads once `SetAuthentication` is called. |
| `0` | A required argument was `NULL`, the handle is unknown, the licence is invalid, or the endpoint resolves but refuses the connection. |
| `2` | The endpoint could not be resolved at all (no internet). A background thread keeps retrying — 3 s, doubling to a 30 s ceiling — until it connects or the handle is released. **You do not need to call `Connect` again.** Uploads return `-21` in the meantime. |
## Other functions
`CreateANSAWSHandle`, `ReleaseANSAWSHandle`, `SetAuthenticationANSAWSHandle`,
`SetProxyANSAWSHandle`, the `ListBucket*` / `CreateBucket*` / `DeleteBucket*`
family, `DownloadFileStreamANSAWSHandle` and `GeneratePresignedURLANSAWSHandle`
return `1` on success and `0` on failure. `GeneratePresignedURLANSAWSHandle`
(the `LStrHandle` variant) additionally uses `-1`, `-2`, `-3`, `-4` with the
same meanings as the table above.
## How connections behave
* `SetAuthentication` opens and keeps 8 pooled HTTPS connections; uploads borrow
and return them, so the TLS handshake is not repeated per image.
* A connection whose **transport** failed is closed, never returned to the pool.
A connection that merely received an HTTP error status is healthy and is kept.
* A pooled connection idle longer than 15 s is closed and replaced rather than
handed out, since middleboxes silently drop idle flows.
* The first request on a reused connection uses a short idle timeout (a quarter
of the configured one, 5 s by default); a fresh connection gets the full
timeout. This bounds what a dead socket costs.
* One upload call is capped at 30 s of wall clock across all internal retries.
## Diagnostics
`ANSAWSS3.log` and DebugView (`AWSS3` / `ANSAWS` tags) record the decision
points: pool evictions, discarded connections, the permanent/transient
classification with the S3 error code, deadline expiry, and background
reconnect attempts.
+699
View File
@@ -0,0 +1,699 @@
---
title: "ANSCORE — System Architecture (L2 + L3)"
subtitle: "Developer reference for the ANSVIS Communication Layer (CL) backend and the Qt/QML cross-platform desktop UI"
author: "ANSCENTER"
date: "2026-05-26"
---
# Executive summary
ANSCORE is a Vision-AI Video Management System split into three layers:
- **Layer 1 — Edge runtime** (per target). Owns cameras, AI inference, trackers, rules, triggers. Built into the existing `ANSCORE/modules/*` C++ tree (ANSCV, ANSODEngine, ANSFR, ANSLPR, ANSOCR, ANSMOT). For this document L1 is *abstract*: we describe only the interface it exposes upward to L2.
- **Layer 2 — ANSVIS CL** (one per customer). Aggregates targets, persists configuration, ingests events, brokers live media. Exposes a single REST contract (`/api/v1`) to L3. In v1 it is built as an **adapter layer in front of the existing ANSVIS server** — no rewrite required.
- **Layer 3 — Qt/QML desktop UI** (Win / macOS / Linux). The operator-facing application. Talks to L2 exclusively.
**Build assignment:**
- L1 and L2 are built by the **core team** under architect-level review.
- L3 is built by a **6-engineer team**.
- L2 ↔ L3 is **REST + SSE + (HLS / RTSP / WebRTC)** — defined by `contracts/openapi.v1.json` (67 paths, 102 operations, 91 schemas).
The contract is the only artifact both sides block on. Once published, UI engineers develop against a Prism mock; the backend **core team** implements adapters per resource in parallel. Integration is the joining point.
\newpage
# Part I — System overview
## The three layers
![System context](diagrams/png/01-system-context.png){ width=100% }
L3 only ever talks to L2. L2 brokers everything else: configuration push down to edges, event upload from edges, media relay. This keeps the UI team fully decoupled from L1 velocity.
## Three transports between L3 and L2
| # | Transport | Purpose | Carries |
|---|---|---|---|
| 1 | REST over HTTPS | Control plane: all CRUD, configuration, deploy commands | JSON request/response, RFC 7807 errors |
| 2 | Server-Sent Events (SSE) over HTTPS | Live event tail (push of detections, target status) | `text/event-stream`, JSON per event |
| 3 | HLS / RTSP / WebRTC | Live video | Media segments; transport chosen per stream |
The REST API brokers the other two: the UI calls REST to obtain a stream descriptor or SSE endpoint URL.
## Why this split
The earlier proposal of using gRPC end-to-end was reversed because the existing ANSVIS server already exposes a REST API. Pragmatic v1: **reuse what exists**, adapt it into a standards-compliant `/api/v1` surface, ship faster. gRPC remains an option for a future v2 if needed.
## Build ownership at a glance
| Layer | Pace |
|---|---|
| L1 (edge runtime) | Slower — high complexity (frame bus, inference batching, plugin SDK) |
| L2 (CL) | Faster — modular per-resource adapters |
| L3 (Qt/QML UI) | Independent — mock-first development |
\newpage
# Part II — Layer 1 (abstract overview)
L1 is the **edge runtime** that runs on each target. From L2's perspective it is a black box that:
- Receives **configuration** (cameras, AI models, AI tasks, rules, triggers) from L2 and persists it locally.
- Captures frames from cameras (RTSP / RTMP / SRT / ONVIF / simulated).
- Runs **AI pipelines** (object detection, FR, LPR, OCR, MOT, custom plugins).
- Evaluates **rules** and emits **events** with evidence (snapshot frame, optional clip).
- Dispatches **triggers** (HTTP, MQTT, email, push, broker integrations).
- Exposes **liveview** streams (RTSP / SRT / future WebRTC).
- Reports **health / metrics / logs** upward.
L1 ↔ L2 protocol is out of scope for this document. For v1, L2's adapter calls into the legacy ANSVIS server's existing protocol with edges (this is what currently works in production).
For L2 and L3 developers, L1 is treated as a remote service:
- Configuration changes go: **L3 (UI) → L2 (REST) → L1 (legacy protocol) → target reconciles**.
- Events flow: **target → L1 → L2 (legacy uplink) → L2 event store → L2 SSE → L3**.
- Liveview: **L3 asks L2 for a stream descriptor → L2 media relay pulls from L1 → L3 plays from L2's relay URL**.
The detailed L1 architecture (camera pool, inference server, frame bus, plugin SDK, etc.) is documented separately and does not affect L2 / L3 work.
\newpage
# Part III — Layer 2 (ANSVIS CL)
## Architecture
![Layer 2 internals](diagrams/png/02-l2-internals.png){ width=100% }
L2 is composed of four logical groups:
1. **`/api/v1` surface** — REST router, auth middleware, OpenAPI request validation, RFC 7807 error mapper, rate limiter, SSE broadcaster.
2. **v1 adapter layer** — one module per resource (cameras, models, edges, deployment-groups, ai-tasks, events, fr, alpr, notifications, system, jobs). Each adapter translates v1 requests to one or more legacy ANSVIS calls.
3. **Legacy ANSVIS core** — reused as-is. Hosts the existing config store, AI task engine, FR engine, ALPR engine. The adapter is the only layer that calls into it.
4. **Infrastructure** — auth + token store, async job queue, event metadata DB, clip / image object store (S3 / MinIO / FS), logs + metrics, media relay.
## Ownership inside L2
![L2 build ownership](diagrams/png/03-l2-ownership.png){ width=100% }
- **Architect (human)** keeps direct review on the cross-cutting concerns: REST router, auth, validation pipeline, RFC 7807 errors, RBAC, L2 ↔ L1 tunnel security.
- The **core team** builds the per-resource adapters and infrastructure helpers (job queue, media relay). Each module has a sharp boundary so the team can work in parallel without colliding.
- **Existing ANSVIS** is reused as-is. The adapter is the only entry point.
## REST API surface — `/api/v1`
The full contract is `contracts/openapi.v1.json` (OpenAPI 3.1, 67 paths, 102 operations, 91 typed schemas). Summary by resource group:
| Tag | Operations | Purpose |
|---|---:|---|
| `auth` | 3 | Login, logout, refresh |
| `users` | 5 | System users CRUD |
| `cameras` | 10 | CRUD + snapshot + liveview + ONVIF scan + probe |
| `models` | 7 | CRUD + multipart upload + initiate/complete large upload |
| `edges` | 7 | CRUD + health + status |
| `deployment-groups` | 8 | CRUD + status + start/stop |
| `ai-tasks` | 11 | CRUD + status + start/stop + liveview + catalog |
| `events` | 6 | List + summary + SSE stream + image + clip + single |
| `fr` | 14 | Persons + faces + groups + detection config |
| `fr-recording` | 7 | Face-recording tasks + read/assign |
| `alpr` | 8 | Plate + plate-group CRUD |
| `notifications` | 5 | Recipients + groups |
| `triggers` | 2 | Type catalog + test dispatch |
| `system` | 7 | Status, health, info, logs, version, update |
| `jobs` | 1 | Async job polling |
| `composite` | 1 | Convenience endpoint (camera + tasks in one call) |
| **Total** | **102** | |
## Conventions enforced by `openapi.v1.json`
- Plural kebab-case resource paths (`/cameras`, `/ai-tasks`, `/deployment-groups`).
- `snake_case` field names. Units in the field name (`time_interval_seconds`, not `time_interval` with `(s)` suffix).
- IDs in the path, never as query-string keys (replaces legacy `?867560018`).
- `GET / POST / PUT / PATCH / DELETE` used semantically.
- State changes via `POST /resource/{id}/start` and `POST /resource/{id}/stop` (pragmatic exception).
- All errors as RFC 7807 problem+json with the matching HTTP status code.
- List responses use a uniform pagination wrapper: `{items, page, page_size, total, next_cursor}`.
- Auth: `Authorization: Bearer <token>` header, never in URL.
- File uploads up to 10 MB use `multipart/form-data`; larger uploads use the initiate / complete pattern with a presigned PUT URL.
- Live events as SSE; live media via HLS / RTSP / WebRTC behind a descriptor RPC.
- All previously stringified-JSON fields (`Parameters(json)`, `RuleConfig`, `TriggerConfig`, `ModelConfigJson`) are now fully typed nested objects, validated against the schema.
## Migration from legacy ANSVIS endpoints
Every v1 endpoint description in the OpenAPI spec ends with `Replaces:` listing the legacy ANSVIS path(s) it supersedes. Examples:
| Legacy | v1 |
|---|---|
| `GET /Camera` (list mode) | `GET /api/v1/cameras` |
| `GET /Camera?{id}` | `GET /api/v1/cameras/{id}` |
| `DELETE /Camera?{id}` + `POST /delete/Camera?{id}` | `DELETE /api/v1/cameras/{id}` |
| `GET /ControlAITask?ID=&State=true` | `POST /api/v1/ai-tasks/{id}/start` |
| `POST /event/Get` (filter in body) | `GET /api/v1/events?task_id=&from=&to=&...` |
| `POST /FR/AddFace` (base64 in JSON) | `POST /api/v1/fr/persons/{id}/faces` (multipart) |
| `GET /SW/GetSoftwareVersion`, etc. | `GET /api/v1/system/version`, `/health`, `/info`, `/logs`, `/update` |
Legacy endpoints remain available during one release cycle with `Deprecation:` + `Sunset:` headers (RFC 8594).
\newpage
# Part IV — Layer 3 (Qt / QML desktop UI)
## Architecture
![Layer 3 internals](diagrams/png/04-l3-internals.png){ width=100% }
L3 is organized in four layers inside the Qt app:
1. **App Shell** — main window, navigation router, design system / theme, i18n, auth state + token store, global error toast / dialog.
2. **Network layer** — generated REST client (from `openapi.v1.json`), SSE subscriber (chunked `QNetworkReply`), media client (QtMultimedia or GStreamer), bbox overlay renderer (custom `QQuickItem` with `QSGGeometryNode`), multipart + presigned uploader.
3. **Domain state** — `QObject` + `Q_PROPERTY` + signals models per resource (CamerasModel, ModelsModel, EdgesModel, DeploymentGroupsModel, AITasksModel, EventsModel + LiveBuffer, FRPersonsModel, ALPRPlatesModel, RecipientsModel, SystemHealthModel).
4. **Screens (QML)** — the operator surface: Login, Dashboard, Camera management, Pipeline editor, AI task config, Rules / triggers, Liveview grid, Eventview timeline, FR persons + faces, ALPR plates, Model registry, Users / admin, Settings.
## Screen → endpoint map
| Screen | Reads | Mutates | Notes |
|---|---|---|---|
| Login | — | `POST /auth/login` | Stores token in `AuthState` |
| Dashboard | `GET /edges`, `GET /edges/{id}/health`, `GET /system/health` | — | Auto-refresh every 5 s |
| Camera management | `GET /cameras`, `GET /cameras/{id}/snapshot`, `POST /cameras/probe` | `POST/PATCH/DELETE /cameras`, `POST /cameras/scan` (async job) | ONVIF discovery + calibration |
| Pipeline editor | `GET /deployment-groups/{id}`, `GET /ai-tasks/catalog`, `GET /models` | `PUT /deployment-groups/{id}` | Highest-risk feature; QGraphicsView |
| AI task config | `GET /ai-tasks/{id}`, `GET /ai-tasks/catalog` | `POST/PATCH/DELETE /ai-tasks`, `POST /ai-tasks/{id}/{start,stop}` | Schema-driven forms |
| Rules / triggers | `GET /triggers/types` | Embedded in DG / task body | `POST /triggers/test` |
| Liveview | `GET /cameras/{id}/liveview` | — | HLS v1, WebRTC v2; overlay sync to PTS |
| Eventview | `GET /events?...`, `GET /events/stream` (SSE), `/events/{id}/{image,clip}` | — | Virtualized list for 100k+ events |
| FR persons + faces | `GET /fr/persons`, `GET /fr/persons/{id}/faces` | `POST/PATCH/DELETE /fr/persons`, `POST /fr/persons/{id}/faces` (multipart) | Image upload |
| ALPR plates | `GET /alpr/plates`, `GET /alpr/plate-groups` | `POST/DELETE /alpr/plates`, `PATCH /alpr/plate-groups/{id}` | Bulk register / deregister |
| Model registry | `GET /models` | `POST /models` (multipart) or initiate / complete pattern for >100 MB | Upload progress |
| Users / admin | `GET /users` | `POST/PATCH/DELETE /users/{id}` | Admin role only |
| Settings | `GET /system/info`, `GET /system/version`, `GET /system/update` | `POST /system/update` | |
## Engineer ownership
![L3 engineer ownership](diagrams/png/11-l3-engineer-ownership.png){ width=100% }
\
Six engineers, each owning a vertical slice:
| # | Role | Owns |
|---|---|---|
| **E1** | UI Lead / Shell Architect | Qt 6 + CMake monorepo, generated REST client, auth state, design system, navigation router, packaging (Win/Mac/Linux signing, notarize, auto-update), CI matrix |
| **E2** | Pipeline Editor Specialist | Visual DAG editor (`QGraphicsView` in `QQuickWidget`), node library, connection validation, schema-driven param panels, undo / redo, save / version |
| **E3** | Liveview / Media | QtMultimedia HLS player (v1), multi-stream grid, bbox overlay (`QQuickItem` + `QSGGeometryNode`), clip player, WebRTC via GStreamer (v2) |
| **E4** | Config Forms | Schema-driven QML forms from `openapi.v1.json`, camera + ONVIF discovery flow, camera calibration UI (ROI / zones / homography), model + plugin + trigger forms |
| **E5** | Dashboard / Eventview | Target dashboard cards + charts, virtualized event timeline, SSE subscriber + LiveBuffer, filter / search / evidence viewer, multi-target monitoring |
| **E6** | UX / Cross-platform QA | Design polish, accessibility, i18n, install / upgrade testing on Win / Mac / Linux, crash reporting, QML Profiler perf passes, user docs surface |
**Critical-path order for week 1–2:** E1 alone delivers the shell that unblocks E2–E6. E2 runs a node-editor spike in parallel to de-risk the hardest feature early. Once E1's shell lands, E3 / E4 / E5 work in parallel against the mock CL; E6 starts cross-platform validation.
\newpage
# Part V — L2 ↔ L3 communication
## 1. REST control plane
Every CRUD operation, every state change, every configuration push. JSON in, JSON out, RFC 7807 problem+json on error.
### Request envelope
```http
GET /api/v1/cameras?page=1&page_size=50 HTTP/1.1
Host: cms.example.com
Authorization: Bearer eyJhbGc…
Accept: application/json
```
### Success list response
```json
{
"items": [
{ "id": 123, "name": "Front gate", "driver": "rtsp", "active": true, ... }
],
"page": 1,
"page_size": 50,
"total": 247,
"next_cursor": null
}
```
### Error response
```json
{
"type": "https://anscenter.com/errors/validation",
"title": "Validation failed",
"status": 422,
"detail": "One or more fields are invalid",
"instance": "/api/v1/cameras",
"errors": [
{ "field": "address", "message": "Must be a valid RTSP URL" },
{ "field": "capturing.time_interval_seconds", "message": "Must be > 0" }
]
}
```
### Camera create — body example (clean v1)
```json
{
"name": "Front gate",
"driver": "rtsp",
"address": "rtsp://192.168.1.87:554/h264Preview_01_main",
"credentials": { "username": "root", "password": "admin123" },
"capturing": { "mode": "timer", "time_interval_seconds": 0.1 },
"active": true,
"logging_enabled": false,
"edge_id": null,
"deployment_group_ids": []
}
```
Note: no more stringified `Parameters(json)`. No more keys with parentheses or apostrophes. Credentials are nested. Units are in the field name.
## 2. SSE event stream
For live event tail:
```http
GET /api/v1/events/stream?task_id=456 HTTP/1.1
Host: cms.example.com
Authorization: Bearer eyJhbGc…
Accept: text/event-stream
```
Each event:
```
event: detection
data: {"id":"20260526_181023-3","task_id":456,"camera_id":123,
"timestamp":"2026-05-26T18:10:23Z","object":"car","score":0.91,
"bbox":{"x":340,"y":210,"w":120,"h":80}}
```
Qt side: `QNetworkAccessManager` with chunked reads, parse newline-delimited frames, drive `EventsModel.LiveBuffer`.
## 3. Live media
`GET /api/v1/cameras/{id}/liveview?transport=hls` returns a stream descriptor:
```json
{
"url": "https://cms.example.com/relay/cam123.m3u8",
"transport": "hls",
"auth_token": "tkn_short_lived_…",
"expires_at": "2026-05-26T18:15:00Z",
"overlay_stream": "/api/v1/events/stream?task_id=456"
}
```
Qt side: hand the URL to QtMultimedia for HLS playback (v1). For sub-second latency in v2, switch transport to `webrtc` and use GStreamer `webrtcbin`.
\newpage
# Part VI — Key sequence flows
## 6.1 Operator configures a camera + AI task and deploys
![Configure & deploy sequence](diagrams/png/05-seq-configure-deploy.png){ width=92% }
Walk-through:
1. UI lists cameras via `GET /cameras?page=1`.
2. Operator submits the new-camera form. UI sends `POST /cameras` with the *clean* body (nested credentials, capturing, image_size — no stringified JSON).
3. UI creates an AI task for the new camera via `POST /ai-tasks`.
4. Operator clicks "Start". UI calls `POST /ai-tasks/{id}/start` — returns `202 Accepted` with `{state: starting}`.
5. UI polls `GET /ai-tasks/{id}/status` until `state=running`. (Will be replaced by SSE in a later iteration so polling is optional.)
## 6.2 Live event tail with evidence
![Live event tail sequence](diagrams/png/06-seq-live-events.png){ width=92% }
Walk-through:
1. UI fetches historical events via `GET /events?task_id=&page=1` and renders the timeline.
2. UI opens an SSE stream `GET /events/stream?task_id=` for live tail.
3. Edge produces an event → L2 ingests → SSE broadcaster pushes the `EventRecord` to all subscribed UIs (filtered by their query).
4. Operator clicks an event. UI fetches `GET /events/{id}/image?width=640` for the snapshot.
5. Operator plays the clip. UI fetches `GET /events/{id}/clip` to get a presigned URL, then GETs the MP4 directly from the object store. The CL process never touches the video bytes — they flow client ↔ S3/MinIO.
## 6.3 Liveview with bbox overlay
![Liveview sequence](diagrams/png/07-seq-liveview.png){ width=92% }
Walk-through:
1. UI requests a liveview descriptor for camera 123: `GET /cameras/123/liveview?transport=hls`.
2. L2's media relay ensures an HLS endpoint is running for that camera (pulling RTSP from the edge in the background).
3. L2 returns `{url, transport:hls, auth_token, expires_at}`.
4. UI plays the HLS URL in QtMultimedia.
5. Simultaneously the UI subscribes to `/events/stream?task_id=456` (SSE) for the AI task's detections.
6. The Qt overlay layer (custom `QQuickItem`) renders bounding boxes synced to the video PTS.
For v2 with WebRTC, the descriptor returns `transport:webrtc` and a signaling URL; GStreamer's `webrtcbin` handles negotiation.
## 6.4 Async camera scan via job queue
![Async scan sequence](diagrams/png/08-seq-async-scan.png){ width=92% }
Walk-through:
1. Operator triggers an ONVIF scan of a subnet. UI calls `POST /cameras/scan` with `{ip_range, credentials, timeout_seconds}`.
2. L2 enqueues the work and immediately returns `202 Accepted` with `{job_id, status:queued, links:{self:"/jobs/j1"}}`.
3. The job worker performs the scan asynchronously.
4. UI polls `GET /jobs/{id}` until `status` becomes `succeeded` or `failed`. The terminal response includes `result` with the discovered cameras.
This pattern is used for every potentially-long operation: ONVIF scan, large model upload, deploy-to-many-targets, software update.
## 6.5 Large model upload (presigned URL)
![Large model upload sequence](diagrams/png/09-seq-large-upload.png){ width=92% }
Walk-through:
1. UI calls `POST /models:initiate-upload` with `{filename, size_bytes, checksum}`. L2 returns `{upload_id, presigned_url, expires_at}`.
2. UI PUTs the bytes directly to the object store using the presigned URL. The CL process never sees the bytes — saves CPU and avoids HTTP-body-size limits.
3. UI calls `POST /models:complete-upload` with `{upload_id, metadata}`. L2 verifies the checksum and registers the model.
For files under 10 MB, the simple `POST /models` multipart endpoint can be used instead.
\newpage
# Part VII — Deployment topology
![Deployment topology](diagrams/png/10-deployment-topology.png){ width=100% }
## Hosts
| Host | Runs | Notes |
|---|---|---|
| Operator workstation (Win / Mac / Linux) | Qt UI application | One per concurrent operator |
| ANSVIS server host | Reverse proxy (nginx / caddy) + ANSVIS app (adapter + legacy core) + Media relay + Object store + Config / Event DBs | One per customer site (can be HA later) |
| Edge target | L1 runtime | One per physical capture site |
## Customer network rules
| Direction | Port | Purpose |
|---|---|---|
| Workstation → ANSVIS server | TCP 443 | REST + SSE + HLS over TLS |
| ANSVIS server → workstation | — (response only) | |
| ANSVIS server ↔ edges | TBD per deployment | Existing ANSVIS protocol |
| Workstation → edges | **None** | Workstations never reach edges directly. Media relay proxies. |
## Why workstations don't reach edges
- Edges typically sit behind customer firewalls / NAT.
- One TLS endpoint (ANSVIS) is easier to firewall, monitor, and authenticate against than dozens of edges.
- Token rotation happens centrally.
- The media relay can transcode, cache, or split streams.
\newpage
# Part VIII — Delivery plan
## Mock-first development
The contract `openapi.v1.json` is the only artifact both teams block on. Once it is published:
- **UI team** runs Stoplight Prism (`prism mock contracts/openapi.v1.json`) as a local mock CL and develops against it from day 1. No need to wait for the real adapter.
- The **backend core team** implements the adapter resource-by-resource in parallel, validating each endpoint against the same OpenAPI schema.
- **Integration** is the joining point. UI tests run against both mock and real to detect drift.
## Critical path
![Dependency chain](diagrams/png/12-dependency-chain.png){ width=100% }
## Sequencing
| Phase | Week | Activities |
|---|---|---|
| **Contract freeze** | 1 | Lock `openapi.v1.json` v1.0.0 (3 small decisions outstanding — see Part IX) |
| **Foundations** | 1–2 | E1: Qt 6 + CMake monorepo, generated REST client, Prism mock wired up, design system stub, app shell. <br/> E2: node-editor spike (de-risk). <br/> Core team: cameras / models / edges / system adapters in parallel. |
| **Vertical slices** | 3–6 | E3 (Liveview), E4 (Config forms), E5 (Dashboard / Eventview) build full features against the mock. <br/> Core team: ai-tasks / deployment-groups / events / fr / alpr / notifications adapters. |
| **Real integration** | 7–8 | UI cuts over from mock to real `/api/v1` per screen as adapters land. E6 runs cross-platform install / upgrade testing on Win / Mac / Linux. |
| **Hardening** | 9–10 | Performance passes (QML Profiler, REST tracing), security review (architect-led), crash reporting, packaging signing / notarization. |
| **Ship v1** | 11–12 | Pilot deployment, customer training, feedback loop. |
\newpage
# Part IX — Open decisions
These need to be locked before contract v1.0.0 publishes.
| # | Decision | Default in spec | Notes |
|---|---|---|---|
| 1 | Field naming | `snake_case` | Could switch to `camelCase` — match Qt/C++ style. One find-replace pass on `openapi.v1.json` either way. |
| 2 | Auth scheme | Bearer (existing ANSVIS token) | v1 keeps Bearer; OAuth2/OIDC for v2. |
| 3 | Mock server tool | Stoplight Prism (free) | Alternative: build a richer custom mock with scenarios. Recommend Prism. |
| 4 | Liveview transport for v1 | HLS via QtMultimedia | Easiest. Add WebRTC in v2 if sub-second latency is needed. |
| 5 | Object store | S3 / MinIO / local FS | Choose per deployment; behind a generic `ObjectStore` interface. |
| 6 | Auto-update mechanism | Sparkle (Mac), MSIX / custom (Win), AppImage (Linux) | Confirm budget for code signing per platform. |
| 7 | Contract repo location | `ANSCORE/contracts/` (current) | Separate `anscore-contracts` repo if teams diverge. |
\newpage
# Appendix A — Full endpoint catalog
Complete list of v1 operations. See `contracts/openapi.v1.json` for full request / response schemas.
## auth
| Method | Path | Purpose |
|---|---|---|
| POST | `/auth/login` | Log in (returns `access_token`, `expires_at`, `user`) |
| POST | `/auth/logout` | Invalidate token |
| POST | `/auth/refresh` | Exchange refresh token for new access token |
## users
| Method | Path | Purpose |
|---|---|---|
| GET | `/users` | List users (paginated) |
| POST | `/users` | Create user |
| GET | `/users/{id}` | Get user |
| PATCH | `/users/{id}` | Update user |
| DELETE | `/users/{id}` | Delete user |
## cameras
| Method | Path | Purpose |
|---|---|---|
| GET | `/cameras` | List cameras (filters: driver, active, edge_id, deployment_group_id) |
| POST | `/cameras` | Create camera |
| GET | `/cameras/{id}` | Get camera |
| PUT | `/cameras/{id}` | Replace camera |
| PATCH | `/cameras/{id}` | Patch camera |
| DELETE | `/cameras/{id}` | Delete camera |
| GET | `/cameras/{id}/snapshot` | Get JPEG snapshot (or presigned URL) |
| GET | `/cameras/{id}/liveview` | Get liveview descriptor (`{url, transport, token, expires_at}`) |
| POST | `/cameras/scan` | Async ONVIF scan (returns job) |
| POST | `/cameras/probe` | Probe single IP for ONVIF profiles |
## models
| Method | Path | Purpose |
|---|---|---|
| GET | `/models` | List models (filter by type) |
| POST | `/models` | Upload + register (multipart, ≤10 MB) |
| GET | `/models/{id}` | Get model |
| PATCH | `/models/{id}` | Update model metadata |
| DELETE | `/models/{id}` | Delete model |
| POST | `/models:initiate-upload` | Begin large upload (returns presigned PUT URL) |
| POST | `/models:complete-upload` | Finalize large upload |
## edges
| Method | Path | Purpose |
|---|---|---|
| GET | `/edges` | List edges |
| POST | `/edges` | Register edge |
| GET | `/edges/{id}` | Get edge |
| PATCH | `/edges/{id}` | Update edge |
| DELETE | `/edges/{id}` | Delete edge |
| GET | `/edges/{id}/health` | Resource / temp / uptime |
| GET | `/edges/{id}/status` | State (online/offline/degraded) + counts |
## deployment-groups
| Method | Path | Purpose |
|---|---|---|
| GET | `/deployment-groups` | List groups |
| POST | `/deployment-groups` | Create group |
| GET | `/deployment-groups/{id}` | Get group |
| PUT | `/deployment-groups/{id}` | Replace group |
| DELETE | `/deployment-groups/{id}` | Delete group |
| GET | `/deployment-groups/{id}/status` | Runtime status |
| POST | `/deployment-groups/{id}/start` | Start group |
| POST | `/deployment-groups/{id}/stop` | Stop group |
## ai-tasks
| Method | Path | Purpose |
|---|---|---|
| GET | `/ai-tasks` | List tasks |
| POST | `/ai-tasks` | Create task |
| GET | `/ai-tasks/{id}` | Get task |
| PUT | `/ai-tasks/{id}` | Replace task |
| PATCH | `/ai-tasks/{id}` | Patch task |
| DELETE | `/ai-tasks/{id}` | Delete task |
| GET | `/ai-tasks/{id}/status` | Get task runtime status |
| POST | `/ai-tasks/{id}/start` | Start task |
| POST | `/ai-tasks/{id}/stop` | Stop task |
| GET | `/ai-tasks/{id}/liveview` | Liveview descriptor for annotated stream |
| GET | `/ai-tasks/catalog` | List supported task types |
## events
| Method | Path | Purpose |
|---|---|---|
| GET | `/events` | List events (filters: task_id, camera_id, object, from, to) |
| GET | `/events/summary` | Analytics rollup |
| GET | `/events/stream` | Live event tail (Server-Sent Events) |
| GET | `/events/{id}` | Get one event |
| GET | `/events/{id}/image` | JPEG snapshot |
| GET | `/events/{id}/clip` | Presigned URL for evidence clip |
## fr (face recognition)
| Method | Path | Purpose |
|---|---|---|
| GET | `/fr/persons` | List persons |
| POST | `/fr/persons` | Create person |
| GET | `/fr/persons/{id}` | Get person |
| PATCH | `/fr/persons/{id}` | Update person |
| DELETE | `/fr/persons/{id}` | Delete person |
| GET | `/fr/persons/{id}/faces` | List faces for person |
| POST | `/fr/persons/{id}/faces` | Add face (multipart, no base64) |
| DELETE | `/fr/faces/{face_id}` | Delete face |
| GET | `/fr/person-groups` | List person groups |
| POST | `/fr/person-groups` | Create group |
| PATCH | `/fr/person-groups/{id}` | Update group |
| DELETE | `/fr/person-groups/{id}` | Delete group |
| GET | `/fr/config` | Get FR detection config |
| PATCH | `/fr/config` | Update FR detection config |
## fr-recording
| Method | Path | Purpose |
|---|---|---|
| GET | `/fr/recording-tasks` | List tasks |
| POST | `/fr/recording-tasks` | Create task |
| DELETE | `/fr/recording-tasks/{id}` | Delete task |
| POST | `/fr/recording-tasks/{id}/start` | Start task |
| POST | `/fr/recording-tasks/{id}/stop` | Stop task |
| GET | `/fr/recording-tasks/{id}/faces` | Read recorded faces |
| PATCH | `/fr/recording-tasks/{id}/faces` | Assign recorded faces to a person (bulk) |
## alpr
| Method | Path | Purpose |
|---|---|---|
| GET | `/alpr/plates` | List plates |
| POST | `/alpr/plates` | Register plates (bulk) |
| DELETE | `/alpr/plates/{plate}` | Deregister single plate |
| POST | `/alpr/plates:bulk-delete` | Deregister bulk |
| GET | `/alpr/plate-groups` | List plate groups |
| POST | `/alpr/plate-groups` | Create group |
| PATCH | `/alpr/plate-groups/{id}` | Update group |
| DELETE | `/alpr/plate-groups/{id}` | Delete group |
## notifications
| Method | Path | Purpose |
|---|---|---|
| GET | `/notifications/recipients` | List recipients |
| POST | `/notifications/recipients` | Create recipient |
| PATCH | `/notifications/recipients/{id}` | Update recipient |
| DELETE | `/notifications/recipients/{id}` | Delete recipient |
| GET | `/notifications/recipient-groups` | List recipient groups |
## triggers
| Method | Path | Purpose |
|---|---|---|
| GET | `/triggers/types` | List supported trigger types + config schemas |
| POST | `/triggers/test` | Dispatch a test trigger (HTTP / MQTT / Email / etc.) |
## system
| Method | Path | Purpose |
|---|---|---|
| GET | `/system/status` | Liveness probe |
| GET | `/system/health` | Resources + counts |
| GET | `/system/info` | Hostname / OS / hardware |
| GET | `/system/logs` | Paginated logs |
| GET | `/system/version` | Version + build date + commit |
| GET | `/system/update` | Update status |
| POST | `/system/update` | Trigger update (async job) |
## jobs
| Method | Path | Purpose |
|---|---|---|
| GET | `/jobs/{id}` | Get async job status / result |
## composite
| Method | Path | Purpose |
|---|---|---|
| POST | `/composite/camera-with-tasks` | Create camera + deployment groups in one call |
\newpage
# Appendix B — Artifacts in the repository
```
ANSCORE/
├── contracts/
│ └── openapi.v1.json # The single source of truth for L2 ↔ L3
│
├── docs/
│ ├── ANSCORE-system-architecture.md # This document (source)
│ ├── ANSCORE-system-architecture.pdf # This document (rendered)
│ ├── L2-L3-architecture.md # Same content with Mermaid diagrams
│ └── diagrams/
│ ├── 01-system-context.puml # PlantUML sources
│ ├── 02-l2-internals.puml
│ ├── 03-l2-ownership.puml
│ ├── 04-l3-internals.puml
│ ├── 05-seq-configure-deploy.puml
│ ├── 06-seq-live-events.puml
│ ├── 07-seq-liveview.puml
│ ├── 08-seq-async-scan.puml
│ ├── 09-seq-large-upload.puml
│ ├── 10-deployment-topology.puml
│ ├── 11-l3-engineer-ownership.puml
│ ├── 12-dependency-chain.puml
│ ├── README.md
│ ├── PREVIEW.md # Clickable plantuml.com URLs
│ └── png/ # Pre-rendered PNGs (used in this PDF)
│
└── modules/ # Existing C++ building blocks (L1 ingredients)
├── ANSCV/ ANSODEngine/ ANSFR/ ANSLPR/ ANSOCR/
├── ANSMOT/ ANSPulsar/ ANSRabbitMQ/ ANSONVIF/
└── ...
```
# Appendix C — Glossary
| Term | Meaning |
|---|---|
| **L1** | Layer 1 — edge runtime (per target) |
| **L2** | Layer 2 — ANSVIS CL (per customer) |
| **L3** | Layer 3 — Qt/QML desktop UI (per operator workstation) |
| **Adapter (v1)** | The L2 module that translates `/api/v1` requests to legacy ANSVIS calls |
| **Edge / target** | A physical device running L1 (Windows/Linux on x86 or NVIDIA Jetson) |
| **Deployment group** | A bundle of cameras + models + rules + triggers deployed to a target |
| **AI task** | A configured pipeline (camera + model + rules + triggers) ready to run |
| **Event** | A detection emitted by an AI task, with evidence (snapshot + optional clip) |
| **Liveview** | Real-time video from a camera, with optional bbox overlay |
| **Trigger** | An outbound action when a rule fires (HTTP, MQTT, email, push, broker) |
| **ONVIF** | Camera-discovery and control protocol used by IP cameras |
| **SSE** | Server-Sent Events — one-way push from server to client over HTTPS |
| **RFC 7807** | The `application/problem+json` standard error format |
| **Prism** | Stoplight's CLI for spinning up a mock REST server from an OpenAPI spec |
| **OpenAPI 3.1** | The contract format used in `openapi.v1.json` |
---
*End of document.*
Binary file not shown.
+600
View File
@@ -0,0 +1,600 @@
# ANSCORE — Layer 2 & Layer 3 Architecture
> Scope: Layer 2 (ANSVIS Communication Layer (CL), REST `/api/v1` surface, supporting services) and Layer 3 (Qt/QML cross-platform desktop UI). Layer 1 is summarized only where it interacts.
>
> Contract of record: [`contracts/openapi.v1.json`](../contracts/openapi.v1.json). All UI ↔ CL communication conforms to it.
>
> Transports:
> 1. **REST / HTTPS** — control, CRUD, configuration, deploy
> 2. **SSE (`text/event-stream`)** — live event tail
> 3. **HLS / RTSP / WebRTC** — live video (descriptor obtained via REST)
---
## 1. System context — L1 / L2 / L3
```mermaid
graph TB
subgraph L3["Layer 3 — Qt/QML Desktop UI (Win / Mac / Linux)"]
UI[Qt/QML application]
end
subgraph L2["Layer 2 — ANSVIS CL"]
APIV1["/api/v1 REST surface"]
LEGACY["Legacy ANSVIS core<br/>(reused as-is)"]
RELAY["Media relay<br/>HLS / RTSP / WebRTC"]
SSE["SSE event broadcaster"]
STORE["Event DB<br/>+ clip object store"]
JOBS["Async job queue"]
AUTH["Auth + RBAC"]
end
subgraph L1["Layer 1 — ANSCORE Edge Runtime<br/>(one per target)"]
EDGE["Camera + Inference + Triggers"]
end
UI -- "① REST / HTTPS<br/>control + CRUD" --> APIV1
UI -- "② SSE / HTTPS<br/>live event tail" --> SSE
UI -- "③ HLS / RTSP / WebRTC<br/>live video" --> RELAY
APIV1 --> AUTH
APIV1 --> LEGACY
APIV1 --> JOBS
APIV1 --> STORE
LEGACY --> STORE
STORE --> SSE
RELAY <--> EDGE
LEGACY <--> EDGE
```
**Key idea:** L3 only ever talks to L2. L1 ↔ L2 is a separate protocol (existing ANSVIS behavior, plus new edge work later). The UI team is fully decoupled from L1 velocity.
---
## 2. Layer 2 — internal architecture
```mermaid
graph TB
subgraph CLIENTS["Clients"]
QT["Qt/QML UI"]
EXT["3rd-party integrations<br/>(optional)"]
end
subgraph EDGE_API["L2 / api / v1 surface"]
ROUTER["REST router"]
AUTHMW["Auth middleware<br/>(Bearer)"]
VALID["Request validation<br/>(OpenAPI schemas)"]
ERR["Error mapper<br/>→ RFC 7807"]
RATE["Rate limiter"]
SSE_HUB["SSE broadcaster<br/>(per-subscription filter)"]
end
subgraph ADAPTER["v1 Adapter Layer<br/>(per-resource modules)"]
CAM_AD["cameras"]
MOD_AD["models"]
EDGE_AD["edges"]
DG_AD["deployment-groups"]
TASK_AD["ai-tasks"]
EV_AD["events"]
FR_AD["fr"]
ALPR_AD["alpr"]
NOTIF_AD["notifications"]
SYS_AD["system"]
JOB_AD["jobs"]
end
subgraph LEGACY["Legacy ANSVIS server<br/>(reused as-is)"]
LEG_CFG["Config store"]
LEG_TASKS["AI task engine"]
LEG_FR["FR engine"]
LEG_ALPR["ALPR engine"]
end
subgraph INFRA["Infrastructure"]
AUTH_SVC["Auth + token store"]
JOBS_Q["Job queue<br/>(scans, uploads, deploys, updates)"]
EVENTS_DB["Event metadata DB"]
CLIPS["Clip / image object store<br/>(S3 / MinIO / FS)"]
LOGS["Logs + metrics + health"]
RELAY["Media relay<br/>HLS / RTSP / WebRTC"]
end
subgraph L1["Layer 1 edges"]
E1["Edge target(s)"]
end
QT --> ROUTER
EXT --> ROUTER
ROUTER --> AUTHMW --> VALID --> RATE
RATE --> ADAPTER
ADAPTER --> ERR
ADAPTER -.-> ROUTER
AUTHMW --> AUTH_SVC
CAM_AD --> LEG_CFG
MOD_AD --> LEG_CFG
MOD_AD --> CLIPS
EDGE_AD --> LEG_CFG
DG_AD --> LEG_TASKS
TASK_AD --> LEG_TASKS
EV_AD --> EVENTS_DB
EV_AD --> CLIPS
FR_AD --> LEG_FR
ALPR_AD --> LEG_ALPR
SYS_AD --> LOGS
JOB_AD --> JOBS_Q
CAM_AD -. "scan / probe" .-> JOBS_Q
MOD_AD -. "large upload" .-> JOBS_Q
LEG_TASKS --> EVENTS_DB
EVENTS_DB --> SSE_HUB
SSE_HUB --> ROUTER
LEGACY <--> E1
RELAY <--> E1
```
**Responsibilities by module:**
| Module | Owns |
|---|---|
| REST router | URL → handler, content negotiation |
| Auth middleware | Bearer validation, principal injection, RBAC checks |
| Request validation | OpenAPI schema-based body/query validation |
| Error mapper | Translates legacy errors / exceptions to `application/problem+json` |
| Rate limiter | Per-token / per-IP throttling |
| SSE broadcaster | Subscribes to event bus, fans out to UI clients with filter + auth |
| v1 adapter (per resource) | Translates v1 request → legacy call(s), reshapes response |
| Job queue | Async ops (camera scan, model upload, software update, large deploy) |
| Event DB | Indexed event metadata for `/events?...` queries |
| Clip / object store | Snapshots and evidence video (presigned URLs) |
| Media relay | Brokers live video from edge to UI (HLS now, WebRTC later) |
| Auth + token store | Login, refresh, RBAC |
**Build ownership inside L2** (AI coders managed by you):
```mermaid
graph TB
subgraph HUMAN["Human-owned (architect — you)"]
H1["REST router + auth middleware"]
H2["RFC 7807 error mapper"]
H3["OpenAPI validation pipeline"]
H4["Auth flow + token store"]
H5["RBAC + security review"]
H6["L2 ↔ L1 tunnel + signing"]
end
subgraph AI["AI-coder-built (modular adapters)"]
A1["cameras adapter"]
A2["models adapter + upload pipeline"]
A3["edges adapter"]
A4["deployment-groups adapter"]
A5["ai-tasks adapter"]
A6["events adapter + SSE broadcaster"]
A7["fr adapter"]
A8["alpr adapter"]
A9["notifications adapter"]
A10["system / health / logs adapter"]
A11["job queue + workers"]
A12["media relay"]
end
subgraph EXIST["Existing ANSVIS (reused as-is)"]
L1["Task engine"]
L2["FR engine"]
L3["ALPR engine"]
L4["Config store"]
end
HUMAN --> AI
AI --> EXIST
```
---
## 3. Layer 3 — Qt/QML internal architecture
```mermaid
graph TB
subgraph QT["Qt 6 + QML — Layer 3"]
subgraph SHELL["App Shell"]
MAIN["Main window"]
ROUTE["Navigation router"]
THEME["Design system / theme"]
I18N["i18n (qsTr / Linguist)"]
AUTH_ST["Auth state + token store"]
ERR_UI["Error toast / dialog"]
end
subgraph NET["Network layer"]
REST["Generated REST client<br/>(openapi.v1.json → C++/Qt)"]
SSE_CL["SSE subscriber<br/>(QNetworkReply chunked)"]
MEDIA["Media client<br/>QtMultimedia / GStreamer"]
OVERLAY["Bbox overlay renderer<br/>(custom QQuickItem)"]
UPLOAD["Multipart + presigned uploader"]
end
subgraph STATE["Domain state (QObject + Q_PROPERTY + signals)"]
ST_CAM["CamerasModel"]
ST_MOD["ModelsModel"]
ST_EDGE["EdgesModel"]
ST_DG["DeploymentGroupsModel"]
ST_TASK["AITasksModel"]
ST_EV["EventsModel + LiveBuffer"]
ST_FR["FRPersonsModel"]
ST_ALPR["ALPRPlatesModel"]
ST_NOTIF["RecipientsModel"]
ST_SYS["SystemHealthModel"]
end
subgraph SCREENS["Screens (QML)"]
LOGIN["Login"]
DASH["Dashboard / targets overview"]
CAM_S["Camera management<br/>+ ONVIF discovery<br/>+ calibration"]
DG_EDIT["Pipeline editor<br/>(visual DAG)"]
TASK_EDIT["AI Task config"]
RULE_S["Rules / triggers"]
LIVE["Liveview grid"]
EV_S["Eventview timeline"]
FR_S["FR persons + faces"]
ALPR_S["ALPR plates"]
MODEL_S["Model registry"]
USERS["Users / admin"]
SETTINGS["Settings"]
end
end
subgraph L2["ANSVIS / api / v1"]
BACKEND["REST + SSE + media"]
end
SCREENS --> STATE
STATE --> NET
NET <--> BACKEND
OVERLAY -.-> MEDIA
AUTH_ST -.-> REST
AUTH_ST -.-> SSE_CL
```
**Module → screen map** (for tasking):
| Screen | Reads from | Writes via | Notes |
|---|---|---|---|
| Login | — | `POST /auth/login` | Stores token in `AuthState` |
| Dashboard | `GET /edges`, `GET /edges/{id}/health`, `GET /system/health` | — | Auto-refresh every 5s |
| Camera management | `GET /cameras`, `GET /cameras/{id}/snapshot`, `POST /cameras/probe` | `POST/PATCH/DELETE /cameras`, `POST /cameras/scan` (async job) | ONVIF discovery + calibration overlay |
| Pipeline editor | `GET /deployment-groups/{id}`, `GET /ai-tasks/catalog`, `GET /models` | `PUT /deployment-groups/{id}` | Highest-risk feature; QGraphicsView |
| AI Task config | `GET /ai-tasks/{id}`, `GET /ai-tasks/catalog` | `POST/PATCH/DELETE /ai-tasks`, `POST /ai-tasks/{id}/start|stop` | Schema-driven forms |
| Rules / triggers | `GET /triggers/types` | embedded in deployment-group / task body | `POST /triggers/test` |
| Liveview | `GET /cameras/{id}/liveview` | — | HLS in v1, WebRTC later; overlay sync to PTS |
| Eventview | `GET /events?filters`, `GET /events/stream` (SSE), `GET /events/{id}/image`, `GET /events/{id}/clip` | — | Virtualized list for 100k+ events |
| FR persons + faces | `GET /fr/persons`, `GET /fr/persons/{id}/faces` | `POST/PATCH/DELETE /fr/persons`, `POST /fr/persons/{id}/faces` (multipart) | Multipart image upload |
| ALPR plates | `GET /alpr/plates`, `GET /alpr/plate-groups` | `POST/DELETE /alpr/plates`, `PATCH /alpr/plate-groups/{id}` | Bulk register/deregister |
| Model registry | `GET /models` | `POST /models` (multipart), `POST /models:initiate-upload` for big files | Upload progress UI |
| Users / admin | `GET /users` | `POST/PATCH/DELETE /users/{id}` | Admin role only |
| Settings | `GET /system/info`, `GET /system/version`, `GET /system/update` | `POST /system/update` | |
---
## 4. Sequence flows
### 4.1 Configure & deploy — typical operator workflow
```mermaid
sequenceDiagram
actor User as Operator
participant UI as Qt UI
participant API as ANSVIS /api/v1
participant LEG as ANSVIS core
participant EDGE as L1 edge
User->>UI: Open Cameras screen
UI->>API: GET /cameras?page=1
API->>LEG: legacy list
LEG-->>API: cameras
API-->>UI: 200 {items, page, total}
User->>UI: Add RTSP camera (form)
UI->>API: POST /cameras
Note right of UI: body has nested credentials,<br/>capturing, image_size — no<br/>stringified JSON
API->>LEG: legacy add
LEG-->>API: id=123
API-->>UI: 201 Location: /cameras/123
User->>UI: Configure AI task
UI->>API: POST /ai-tasks {camera_id:123, ...}
API->>LEG: legacy create
LEG-->>API: id=456
API-->>UI: 201 {id:456}
User->>UI: Start task
UI->>API: POST /ai-tasks/456/start
API->>LEG: dispatch
LEG->>EDGE: config + start
EDGE-->>LEG: running
API-->>UI: 202 {state:starting}
loop status poll (or SSE later)
UI->>API: GET /ai-tasks/456/status
API-->>UI: 200 {state:running, fps:14.2}
end
```
### 4.2 Live event tail + clicking into evidence
```mermaid
sequenceDiagram
actor User
participant UI
participant API as /api/v1
participant SSE as SSE broadcaster
participant CLIPS as Clip store
participant EDGE
User->>UI: Open Eventview (task=456)
UI->>API: GET /events?task_id=456&page=1
API-->>UI: 200 historical events
UI->>API: GET /events/stream?task_id=456
API-->>UI: text/event-stream (open)
EDGE->>API: event arrives (via L1↔L2)
API->>SSE: push EventRecord
SSE-->>UI: data: {id:E1, ts, object:car, bbox, ...}
User->>UI: Click event E1
UI->>API: GET /events/E1
API-->>UI: 200 EventRecord (full)
UI->>API: GET /events/E1/image?width=640
API-->>UI: image/jpeg bytes
User->>UI: Play clip
UI->>API: GET /events/E1/clip
API->>CLIPS: presign GET
CLIPS-->>API: presigned URL
API-->>UI: 200 {url, expires_at}
UI->>CLIPS: GET <presigned URL>
CLIPS-->>UI: MP4 bytes
UI->>UI: play in QtMultimedia
```
### 4.3 Liveview with bbox overlay
```mermaid
sequenceDiagram
actor User
participant UI
participant API
participant RELAY as Media relay
participant EDGE
participant SSE
User->>UI: Open Liveview (cam 123, task 456)
UI->>API: GET /cameras/123/liveview?transport=hls
API->>RELAY: ensure HLS for cam 123
RELAY->>EDGE: pull RTSP
RELAY-->>API: hls_url + short-lived token
API-->>UI: 200 {url, transport:hls, auth_token, expires_at}
UI->>RELAY: GET hls_url (token)
RELAY-->>UI: m3u8 + segments
UI->>UI: Render video (QtMultimedia)
UI->>API: GET /events/stream?task_id=456 (SSE)
API-->>UI: text/event-stream
loop per event
SSE-->>UI: bbox + ts
UI->>UI: overlay synced to PTS
end
```
### 4.4 Async job — camera network scan
```mermaid
sequenceDiagram
actor User
participant UI
participant API
participant JOBS as Job queue
participant SCAN as ONVIF scanner
User->>UI: Scan 192.168.1.0/24
UI->>API: POST /cameras/scan
API->>JOBS: enqueue
JOBS-->>API: job_id=j1
API-->>UI: 202 {job_id:j1, status:queued, links:{self:/jobs/j1}}
JOBS->>SCAN: run
loop poll until terminal
UI->>API: GET /jobs/j1
API-->>UI: 200 {status:running, progress:0.4}
end
SCAN-->>JOBS: results
UI->>API: GET /jobs/j1
API-->>UI: 200 {status:succeeded, result:{discovered:[...]}}
```
### 4.5 Large model upload — initiate / complete pattern
```mermaid
sequenceDiagram
actor User
participant UI
participant API
participant OBJ as Object store
User->>UI: Pick model.zip (500 MB)
UI->>API: POST /models:initiate-upload {filename, size_bytes, checksum}
API->>OBJ: presign PUT
OBJ-->>API: presigned_url
API-->>UI: 200 {upload_id, presigned_url, expires_at}
UI->>OBJ: PUT <presigned_url> (binary stream, with progress)
OBJ-->>UI: 200 OK
UI->>API: POST /models:complete-upload {upload_id, metadata}
API->>OBJ: verify checksum
OBJ-->>API: ok
API-->>UI: 201 Model
```
---
## 5. Deployment topology
```mermaid
graph LR
subgraph CUSTOMER["Customer site / cloud"]
subgraph DESKTOPS["Operator workstations"]
D1["Win desktop<br/>Qt UI"]
D2["Mac desktop<br/>Qt UI"]
D3["Linux desktop<br/>Qt UI"]
end
subgraph SERVER["ANSVIS server host"]
NGINX["Reverse proxy<br/>TLS terminator"]
APP["ANSVIS app<br/>(adapter + legacy core)"]
RELAY["Media relay"]
OBJ["Object store<br/>(S3/MinIO/FS)"]
DB["DBs<br/>(config / events)"]
end
subgraph EDGES["Edge targets"]
E1["Linux x86 + NV"]
E2["Win x86 + NV"]
E3["Jetson Orin"]
end
end
D1 -- "TLS 443<br/>REST + SSE + HLS" --> NGINX
D2 -- "TLS 443" --> NGINX
D3 -- "TLS 443" --> NGINX
NGINX --> APP
NGINX --> RELAY
APP --> DB
APP --> OBJ
RELAY <--> EDGES
APP <--> EDGES
```
**Network rules to communicate to the customer:**
- Outbound TCP 443 from each operator workstation to ANSVIS server.
- Inbound 443 on ANSVIS server.
- Whatever protocol L1 ↔ L2 needs (TCP 443 if everything tunneled; or per-edge ports if direct).
- No need for the workstation to reach edges directly — Media relay proxies.
---
## 6. L3 — engineer ownership (6 engineers)
```mermaid
graph TB
subgraph E1["E1 — UI Lead / Shell Architect"]
E1A["Qt 6 + CMake monorepo"]
E1B["Generated REST client from openapi.v1.json"]
E1C["Auth state + token store"]
E1D["Design system / theme"]
E1E["Navigation router"]
E1F["Packaging Win/Mac/Linux<br/>signing, notarize, auto-update"]
E1G["CI matrix"]
end
subgraph E2["E2 — Pipeline Editor Specialist"]
E2A["Visual DAG editor<br/>QGraphicsView in QQuickWidget"]
E2B["Node library + connections"]
E2C["Schema-driven param panels"]
E2D["Undo / redo, save / version"]
end
subgraph E3["E3 — Liveview / Media"]
E3A["QtMultimedia HLS player<br/>(v1)"]
E3B["Multi-stream grid"]
E3C["Bbox overlay (QQuickItem + QSGGeometryNode)"]
E3D["Clip player (evidence)"]
E3E["WebRTC via GStreamer (v2)"]
end
subgraph E4["E4 — Config Forms"]
E4A["Schema-driven QML forms<br/>(from openapi.v1.json)"]
E4B["Camera + ONVIF discovery flow"]
E4C["Camera calibration UI<br/>(ROI / zones / homography)"]
E4D["Model + plugin + trigger forms"]
end
subgraph E5["E5 — Dashboard / Eventview"]
E5A["Target dashboard cards + charts"]
E5B["Event timeline (virtualized)"]
E5C["SSE subscriber + LiveBuffer"]
E5D["Filter / search / evidence viewer"]
E5E["Multi-target monitoring"]
end
subgraph E6["E6 — UX / Cross-platform QA"]
E6A["Design polish, a11y, i18n"]
E6B["Install / upgrade testing<br/>Win + Mac + Linux"]
E6C["Crash reporting (Sentry/Crashpad)"]
E6D["QML Profiler perf passes"]
E6E["User docs surface"]
end
CONTRACT[(openapi.v1.json<br/>frozen contract)]
E1 -.-> CONTRACT
E2 -.-> CONTRACT
E3 -.-> CONTRACT
E4 -.-> CONTRACT
E5 -.-> CONTRACT
```
**Critical-path order:**
1. **Week 1–2 (E1 only)**: monorepo, generated REST client, auth, design system stub, `make dev` brings up Prism mock. This unblocks everyone else.
2. **Week 1–2 (E2 in parallel)**: node-editor spike. Highest schedule risk → de-risk early.
3. **Week 2+ (E3, E4, E5 in parallel)**: each owns a vertical against the mock CL. E6 starts integration testing.
4. **Week 4+**: cut from mock CL to real ANSVIS `/api/v1` as adapter endpoints land.
---
## 7. What gates what (dependency chain)
```mermaid
graph LR
CONTRACT["openapi.v1.json<br/>(frozen)"]
MOCK["Prism mock server"]
ADAPT["ANSVIS /api/v1 adapter<br/>(AI coders, per-resource)"]
LEGACY["Legacy ANSVIS"]
UI_SHELL["L3 shell + REST client (E1)"]
UI_FEAT["L3 screens (E2-E6)"]
REAL["Real backend integration"]
SHIP["Ship v1"]
CONTRACT --> MOCK
CONTRACT --> ADAPT
ADAPT --> LEGACY
MOCK --> UI_SHELL --> UI_FEAT
UI_FEAT --> REAL
ADAPT --> REAL
REAL --> SHIP
```
- The contract is the *only* thing both sides block on.
- UI does not wait for the adapter — develops against Prism.
- Adapter team does not wait for UI — implements per-resource modules in parallel.
- Integration is the joining point.
---
## 8. Open decisions that affect this architecture
These are unchanged from earlier conversations; capturing here so the team has them in one place:
1. **Field naming** — `snake_case` (current default in `openapi.v1.json`) or `camelCase`?
2. **Auth scheme** — keep Bearer (current) or move to OAuth2/OIDC for v1?
3. **Mock-server pinning** — Prism for free, or build a richer custom mock?
4. **Liveview transport for v1** — HLS only (recommended), or RTSP-direct on LAN, or jump straight to WebRTC?
5. **Object store choice** — local FS (single host), MinIO (single host scalable), or S3 (cloud)?
6. **Auto-update** for the Qt client — Sparkle on Mac, Squirrel/MSIX on Windows, AppImage on Linux. Confirm budget for each.
7. **Contract repo** — co-located in `ANSCORE/contracts/` (current) or split into a separate `anscore-contracts` repo?
---
*Last updated: 2026-05-26 — diagrams reflect `openapi.v1.json` v1.0.0 draft.*
+573
View File
@@ -0,0 +1,573 @@
# PLAN — Port ANSCORE Licensing Core to Linux (x86_64 + Jetson aarch64)
**Status:** Draft, awaiting product-side decisions before execution
**Target platforms:** Ubuntu 22.04 x86_64, JetPack 6.x (Ubuntu 22.04 aarch64)
**Components in scope:** `core/anslicensing`, `core/ANSLicensingSystem`, `core/ANSLibsLoader`
**Estimated effort:** ~6–7 focused weeks for one engineer
**Risk:** Medium (HWID compatibility with license server is the dominant risk)
**Owner:** Tuan Nghia Nguyen
**Branch to create:** `linux-licensing-port`
---
## Decisions already made (do not re-litigate)
1. **Three components are in scope, in this order**: `ANSLibsLoader` (easiest), `anslicensing` (medium), `ANSLicensingSystem` (hardest). Engine modules (`ANSODEngine`, `ANSCV`, etc.) are NOT part of this plan — they come after licensing is up.
2. **No LabVIEW support on Linux.** The `_LV` exports and `LabVIEWHeader/extcode.h` includes are stripped from the Linux build via `#ifdef _WIN32`. The C++ and C-string (`_CS`) entry points become the only Linux surface.
3. **`lfreist/hwinfo` stays.** It is already vendored at `C:/ANSLibs/ANS-HWiNFO/` and is cross-platform (verified — its `platform.h` defines `HWINFO_UNIX` on Linux). All 8 `hwinfo::` call sites in `ANSLicense.cpp` are portable as-is. No replacement library needed.
4. **HWID gains real signals on Linux.** Today `hwid.cpp` Linux branches stub the SMBIOS UUID slot with `"4294967296"` and leave `GetHDDPropertyList` empty (2-point HWID strength vs Windows 4-point). We will fill those slots using `hwinfo::MainBoard::serialNumber()` and `hwinfo::RAM::serialNumber()` for parity.
5. **`rand.cpp` Linux path is replaced with `getrandom(2)`**. The current `srand(time(NULL)) + rand()` path is cryptographically broken and a security-correctness issue, not just a portability one.
6. **`DetectClockManipulation` on Linux ships as a best-effort stub.** Reading `journalctl` history is expensive and not always available (containers, no systemd). It will log a warning and return `false`. Acceptable anti-tamper degradation; flagged for follow-up.
7. **MediaClient and other non-licensing Windows surface stays Windows-only.** This plan does not touch them.
8. **HWiNFO is NOT the commercial HWiNFO64.** It's the open-source MIT-licensed [`lfreist/hwinfo`](https://github.com/lfreist/hwinfo) C++ library. No commercial license concern.
## Pre-work already completed (discovery, not code)
- ✅ Confirmed `core/anslicensing/` already has Linux scaffolding: `CMakeLists.txt` branches on `WIN32` and links `curl ssl crypto`; `precomp.h` has POSIX shims; `download.cpp` has a complete libcurl path; `hwid.cpp` has Linux CPU-ID and MAC code.
- ✅ Inventoried Windows API surface in `ANSLicensingSystem/ANSLicense.cpp` (4,021 LOC):
- Only 2 `#ifdef _WIN32` blocks — the file assumes Windows top-to-bottom
- 8 × `hwinfo::` calls — **portable as-is**
- 12 × PDH counter sites — need `/proc` rewrite (Phase 3.3)
- 7 × DXGI sites — need NVML/libdrm rewrite (Phase 3.4)
- 1 × WMI `MSAcpi_ThermalZoneTemperature` query — Linux `/sys/class/thermal/` (Phase 3.2)
- 1 × WinHTTP block — replace with libcurl already in `anslicensing/download.cpp` (Phase 3.5)
- 1 × `DnsQuery_A` — replace with `getaddrinfo` (Phase 3.6)
- Misc: `GlobalMemoryStatusEx`, `GetProcessMemoryInfo`, `GetSystemDirectoryW + GetDiskFreeSpaceExW`, `GetCurrentProcessId` (Phase 3.7)
- ✅ Confirmed `lfreist/hwinfo` cross-platform support via its `platform.h` and the header set at `C:/ANSLibs/ANS-HWiNFO/include/hwinfo/`.
---
## Open product decisions (BLOCK Phase 0)
These must be resolved with the licensing-backend team before any code is written. They determine the shape of Phase 1.
### Decision 1 — HWID format compatibility with the license server
The license server validates a HWID hash that's currently produced from Windows WMI data (`Win32_ComputerSystemProduct.UUID`, `Win32_NetworkAdapter.MACAddress`, `Win32_PhysicalMedia.SerialNumber`, `Win32_Processor.ProcessorId`). On Linux we will produce hashes from different data sources (SMBIOS via `/sys/class/dmi/id/`, NIC MACs via `getifaddrs`, RAM serial via DMI, CPU ID via `__get_cpuid`). The hash format (the bit-packed 125-bit `BitStream3` in `hwid.cpp`) stays identical, but the **values being hashed differ**. Three options:
- **A. Unified HWID, same algorithm**: server treats Windows and Linux licenses identically; admin has to physically activate each device once and the hash is opaque. **Simplest. Recommended.**
- **B. Separate Linux license pool**: server tracks platform field; Linux activations come from a dedicated key range. More work backend-side.
- **C. Backward-compat dance**: keep some Windows-specific fields empty on Linux (status quo) — license matching weakens. **Reject.**
**Owner to confirm with backend team before Phase 0.**
### Decision 2 — HWiNFO `MainBoard::serialNumber()` availability on Jetson
`lfreist/hwinfo` reads `/sys/class/dmi/id/board_serial` on Linux. **On Jetson this file may not exist** — ARM SBCs use device-tree, not DMI. We need to:
- Test on a real Jetson Orin/AGX board early.
- Define fallback signal: `/proc/device-tree/serial-number` (Jetson exposes this) or eMMC CID (`/sys/block/mmcblk*/device/cid`).
**Owner needs a Jetson devkit available before Phase 1.**
### Decision 3 — Boost dependencies on Jetson
`ANSLicensingSystem` currently links `libboost_log-vc143-mt-x64-1_82.lib` and uses `boost::property_tree::ptree` and `boost::date_time`. Ubuntu 22.04 ships Boost 1.74 by default (`libboost-all-dev`). Choices:
- **A. Use system Boost on Linux** (1.74). Accept minor API differences; pin via `find_package(Boost 1.74 REQUIRED)`.
- **B. Vendor Boost 1.82**. Painful build but exact parity.
- **C. Drop Boost.PropertyTree on Linux** and use `nlohmann/json` (already a project dep via `ANSLIBS_DIR/nlohmann`). Smaller dep surface but a code change in `ANSLicense.cpp` JSON output paths.
**Recommendation: A.** Defer until Phase 0.5 spike confirms it builds.
---
## Quick Start for a new session
If you're a fresh Claude session and the user has asked you to execute this plan:
1. **Read this entire document end-to-end** before editing anything. The per-file checklist in Appendix A is the source of truth for what changes.
2. **Confirm prerequisites are in place**:
```bash
ls "C:/ANSLibs/ANS-HWiNFO/include/hwinfo/platform.h" # cross-platform lib present
git status # clean working tree
git branch --show-current # should be linux-licensing-port
```
3. **Verify the three product decisions above are resolved.** Ask the user to paste the answers if they're not in this doc. Do NOT start Phase 1 without Decision 1 (HWID format) answered.
4. **Ask the user which phase to start with.** Default to Phase 0.1.
5. **Use TaskCreate** to track progress through the phases — the seven-phase structure (0 through 4 plus per-3.x sub-phases) is the natural top-level task list.
6. **Work in small commits.** One file, one logical change, verify build on Windows AND Linux before the next change. The Windows build must stay green at every step.
7. **Never modify a `*_LV` function signature.** Even on Linux where we don't compile it, the source still exists on the Windows-shared file. Touch only what's gated by `#ifdef _WIN32`.
### How to invoke this plan
The user can paste any of these to start:
> "Read `docs/PLAN_Linux_Licensing_Port.md` and walk me through Phase 0."
> "Continue the Linux licensing port. Read `docs/PLAN_Linux_Licensing_Port.md` and ask what's done."
> "Execute Phase 1.1 from `docs/PLAN_Linux_Licensing_Port.md`."
### Context the new session might need that isn't in this plan
- **Build system on Linux**: CMake 3.28+, Ninja, GCC 11+ on Ubuntu 22.04. Cross-compile to Jetson via JetPack 6.x SDK Manager + sysroot.
- **CUDA on Linux**: CUDA 12.x, cuDNN, TensorRT 10.x from `apt` or NVIDIA's repo.
- **Licensing server endpoint**: ask the user for the staging URL — it is NOT in source control.
- **Test hardware**: x86 Ubuntu box AND a Jetson Orin Nano/AGX devkit are both required. Do not declare Phase 1 done from x86 testing alone.
---
## 1. Background
ANSCORE targets Linux (Jetson + industrial x86) per the product roadmap. The Flutter mobile app will be replaced by a Qt QML UI talking to ANSCORE over gRPC + RTSP. Licensing is the blocking dependency: **no module can be loaded without `ANSLicensingSystem` validating the license**, so it must come first.
The three components are layered:
```
┌──────────────────────────────┐
│ Module DLLs (ANSCV, ANSOD,…) │ ← consumers, NOT part of this plan
├──────────────────────────────┤
│ ANSLicensingSystem (façade) │ ← Phase 3 (4–5 weeks)
├──────────────────────────────┤
│ anslicensing (SDK) │ ← Phase 1 (2–3 weeks)
│ ANSLibsLoader (dynload) │ ← Phase 2 (1 week)
└──────────────────────────────┘
```
`ANSLicensingSystem` depends on `anslicensing`; `ANSLibsLoader` is independent of both but consumed by `ANSCV` later. Phase ordering keeps the build green at every step.
## 2. Goals
- All three components build on Ubuntu 22.04 x86_64 and JetPack 6.x aarch64.
- `libANSLicensingSystem.so` exports the same C++ symbols and C ABI as the Windows DLL (less LabVIEW exports).
- Linux HWID is a 4-signal hash, equivalent strength to Windows.
- License validation works against the existing license server.
- All Windows behavior preserved unchanged. Zero behavioral regressions on Windows builds.
- `rand.cpp` on Linux uses `getrandom(2)` instead of insecure `rand()`.
## 3. Non-goals
- LabVIEW support on Linux.
- macOS port (separate effort).
- `DetectClockManipulation` feature parity on Linux (acceptable degradation, logged).
- Re-architecting the licensing protocol (wire compat preserved).
- Engine modules (`ANSODEngine`, `ANSCV`, etc.) — separate plans.
- Build infra changes (Conan/vcpkg adoption, etc.).
## 4. Phases
### Phase 0 — Windows-side prep (1 week, zero behavioral change)
Done on Windows. No Linux changes yet. Each step must keep the Windows build green and tests passing.
- **0.1** Add `ANSCORE_API` visibility macro to all three components' public headers:
```cpp
#if defined(_WIN32)
#if defined(ANSCORE_EXPORTS)
#define ANSCORE_API __declspec(dllexport)
#else
#define ANSCORE_API __declspec(dllimport)
#endif
#elif defined(__GNUC__)
#define ANSCORE_API __attribute__((visibility("default")))
#else
#define ANSCORE_API
#endif
```
Replace `__declspec(dllexport)` / `__declspec(dllimport)` site-by-site. Test on Windows that the resulting DLL exports the same symbol set (`dumpbin /exports`).
- **0.2** Split LabVIEW exports out of `ANSLicense.h` into a new `ANSLicense_LV.h` that includes the LabVIEW header and declares only the `_LV` functions. Update Windows-side `ANSLicense.cpp` to include `ANSLicense_LV.h`. **Result: `ANSLicense.h` has zero LabVIEW types**, so Linux consumers don't drag in `extcode.h`.
- **0.3** Strip `<windows.h>`, `<winhttp.h>`, etc. from public headers (`ANSLIB.h:6 #include <windows.h>` line is the worst offender). Use forward declarations or guard with `#ifdef _WIN32` where needed in implementation files only.
- **0.4** Vendor `lfreist/hwinfo` as a git submodule at `3rdparty/hwinfo` (or confirm `C:/ANSLibs/ANS-HWiNFO/` is the canonical copy and update CMake to point at it consistently). Add a top-level CMake option `ANSCORE_USE_VENDORED_HWINFO` that defaults to ON.
- **0.5** Stand up an Ubuntu 22.04 build environment (CI runner or local VM). Install: `build-essential cmake ninja-build libboost-all-dev libcurl4-openssl-dev libssl-dev libzip-dev zlib1g-dev libnvidia-ml-dev libdrm-dev libsensors4-dev`. Verify CUDA 12.x + TensorRT 10.x are reachable. Smoke test: `cmake -B build && ninja -C build core/anslicensing` should currently fail — but it should fail with **predictable errors** (missing hwinfo Linux build, etc.), not config errors.
**Acceptance for Phase 0**:
- Windows build green, all existing tests pass.
- `dumpbin /exports ANSLicensingSystem.dll` symbol set is byte-identical to baseline.
- `ANSLicense.h` does not include `LabVIEWHeader/extcode.h`.
- Ubuntu environment is reproducible.
- Decision 1, 2, 3 above are answered in writing.
### Phase 1 — Port `core/anslicensing` (2–3 weeks)
This is the lowest-level SDK. Already 80% portable.
- **1.1** **Strengthen Linux HWID** in `hwid.cpp`:
- Replace the `propList.push_back("4294967296")` stub at line 254 with `hwinfo::MainBoard board; propList.push_back(board.serialNumber());`. Falls back to `/proc/device-tree/serial-number` on aarch64 if board_serial is missing.
- Implement `GetHDDPropertyList` on Linux at line 142: enumerate block devices via `/sys/block/*/device/wwid` or `/sys/block/*/device/serial`. Filter to non-removable (skip `/sys/block/*/removable == 1`).
- Add the same source to the `MatchCurrentHardwareId` Linux branches (lines 416, 538).
- Cross-test: HWID produced on Ubuntu must validate on the same machine across reboots.
- **1.2** **Replace `rand.cpp` Linux RNG** with `getrandom(2)`:
```cpp
#include <sys/random.h>
// ::srand(time(NULL)); ← DELETE
// rnd = ::rand(); ← DELETE
ssize_t n = getrandom(&rnd, sizeof(rnd), 0);
if (n != sizeof(rnd)) {
// fallback: open /dev/urandom directly
}
```
Keep `random::random()` constructor cheap (no kernel call); make `random::rand()` the syscall site. Document the change in a header comment.
- **1.3** **`DetectClockManipulation` Linux stub** in `helper.cpp` — return `false` with `ANS_DBG("ClockTamper", "skipped on Linux — no-op fallback")`. Document as a known degradation.
- **1.4** **Wire CMake on Linux**. The CMakeLists already has the Linux branch; verify:
- `find_package(CURL REQUIRED)` resolves
- `target_link_libraries(... curl ssl crypto)` links cleanly
- Add `find_package(hwinfo REQUIRED)` or use the submodule
- **1.5** **Round-trip test**: a small test binary that calls `HardwareId::GetCurrentHardwareId()`, persists the string, then calls `HardwareId::MatchCurrentHardwareId(saved)` and asserts true. Run on Ubuntu x86 AND a Jetson devkit.
**Acceptance for Phase 1**:
- `libanslicensing.so` builds on Ubuntu 22.04 x86_64 and Jetson aarch64.
- Round-trip HWID test passes on both targets across at least 3 reboots.
- `strings libanslicensing.so | grep -c '4294967296'` returns 0.
- Static analysis (cppcheck or clang-tidy) shows no new warnings on Linux build.
### Phase 2 — Port `core/ANSLibsLoader` (1 week)
Mechanical port from `LoadLibrary`/`GetProcAddress` to `dlopen`/`dlsym`. Independent of Phase 1 and Phase 3 — can be done in parallel.
- **2.1** Port `DynLibUtils.cpp` core helpers:
```cpp
#ifdef _WIN32
HMODULE LoadLib(const char* p) { return LoadLibraryA(p); }
void* GetSym(HMODULE h, const char* s) { return GetProcAddress(h, s); }
void UnloadLib(HMODULE h) { FreeLibrary(h); }
#else
void* LoadLib(const char* p) { return dlopen(p, RTLD_NOW | RTLD_LOCAL); }
void* GetSym(void* h, const char* s) { return dlsym(h, s); }
void UnloadLib(void* h) { dlclose(h); }
#endif
```
Wrap the handle type behind an opaque `DynLibHandle` typedef so callers don't see the difference.
- **2.2** Update `CvLoader.cpp`, `NvDynLoader.cpp`, `OvLoader.cpp`, `EPLoader.cpp` to use the cross-platform helpers. Most logic is search-path discovery — that needs branching too:
- Windows: `C:/ANSLibs/...` paths
- Linux: `/opt/ANSLibs/...`, `/usr/lib/x86_64-linux-gnu/`, `/usr/local/cuda/lib64/`, and `${LD_LIBRARY_PATH}`
- **2.3** Naming convention: Windows `.dll`, Linux `.so` with `lib` prefix. Centralize in a `PLATFORM_DLL_NAME("opencv_world")` macro that expands to `"opencv_world.dll"` on Windows and `"libopencv_world.so"` on Linux.
- **2.4** Symbol visibility: Linux `.so` exports everything by default unless built with `-fvisibility=hidden`. Apply `-fvisibility=hidden` globally in the top-level CMakeLists and use the `ANSCORE_API` macro to opt in to exports.
- **2.5** Smoke test: `ANSLibsLoader` opens `libopencv_world.so` on Ubuntu and returns valid handles for a known set of OpenCV symbols.
**Acceptance for Phase 2**:
- `libANSLibsLoader.so` builds and runs.
- All four sub-loaders (CV/OV/NV/EP) load their respective libraries successfully on Linux.
- No symbol-visibility regressions on Windows (verify with `dumpbin /exports`).
### Phase 3 — Port `core/ANSLicensingSystem` (4–5 weeks)
The big one. Split into independently mergeable PRs.
#### 3.1 — Misc Win32 cleanup (1 day)
- `GlobalMemoryStatusEx` → read `/proc/meminfo` MemTotal / MemAvailable
- `GetProcessMemoryInfo` → read `/proc/self/status` VmRSS, VmSwap, VmPeak
- `GetSystemDirectoryW + GetDiskFreeSpaceExW` (line 1056-1064) → `statvfs("/")`
- `GetCurrentProcessId` → `getpid()`
- `WideCharToMultiByte` / `WideToUtf8` → `std::wstring_convert` or just use `std::string` throughout the Linux path (no wide chars needed without WMI)
#### 3.2 — CPU thermal (1 day)
Replace `ReadCpuTempCelsiusViaWmi()` (line 603) with a Linux reader:
```cpp
double ReadCpuTempCelsiusLinux() {
// Scan /sys/class/thermal/thermal_zone*/type for "x86_pkg_temp" (x86)
// or "CPU-therm" / "thermal-fan-est" (Jetson)
// Read corresponding /sys/class/thermal/thermal_zone*/temp (milli-Celsius)
// Return max across all CPU zones, or -1 if none found
}
```
Keep the Windows function intact behind `#ifdef _WIN32`. Dispatch through a single `ReadCpuTempCelsius()` wrapper.
#### 3.3 — System health sampler / PDH rewrite (1 week)
The `HealthSampler` class (lines 906–1089) is the largest single port. Build a Linux `HealthSamplerLinux` class with the same public API as the Windows one. Then dispatch at instantiation:
```cpp
#ifdef _WIN32
using HealthSampler = HealthSamplerWin;
#else
using HealthSampler = HealthSamplerLinux;
#endif
```
PDH counter → Linux source mapping:
| PDH counter | Linux source |
|---|---|
| `\Processor Information(_Total)\% Processor Time` | `/proc/stat` delta of (user+nice+system) vs idle |
| `\Processor Information(_Total)\% Processor Performance` | `/sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq` / base freq |
| `\Processor Information(_Total)\% Processor Utility` | Same as Processor Time on Linux (no MS-specific util concept) |
| `\PhysicalDisk(_Total)\% Disk Time` | `/proc/diskstats` field 13 (io_time ms / interval ms) |
| `\PhysicalDisk(_Total)\Disk Read Bytes/sec` | `/proc/diskstats` field 6 × 512 / interval |
| `\PhysicalDisk(_Total)\Disk Write Bytes/sec` | `/proc/diskstats` field 10 × 512 / interval |
| `\Network Interface(*)\Bytes Received/Sent/sec` | `/proc/net/dev` rx_bytes / tx_bytes deltas |
| `\Network Interface(*)\Current Bandwidth` | `/sys/class/net/*/speed` (Mbit/s) |
| `\GPU Engine(*)\Utilization Percentage` | NVML `nvmlDeviceGetUtilizationRates` |
| `\GPU Process Memory(*)\Dedicated Usage` | NVML `nvmlDeviceGetMemoryInfo` |
The sampler keeps a `prev_*` snapshot for delta computation (no PDH built-in differencing on Linux). Sample interval: same 2-second cadence as Windows.
Acceptance: the populated `ANSCENTER::SystemHealthInfo` struct has all the same fields filled on Linux as on Windows (allowing -1 / empty for fields that genuinely don't exist).
#### 3.4 — GPU adapter enumeration (1 week)
Replace `EnumerateGpuAdapters` (line 721) with NVML on systems with NVIDIA drivers, falling back to libdrm:
```cpp
std::vector<GpuAdapter> EnumerateGpuAdaptersLinux() {
std::vector<GpuAdapter> out;
// Try NVML first (covers Jetson + dGPU x86)
if (nvmlInit_v2() == NVML_SUCCESS) {
unsigned int count = 0;
nvmlDeviceGetCount_v2(&count);
for (unsigned int i = 0; i < count; ++i) {
nvmlDevice_t dev;
nvmlDeviceGetHandleByIndex_v2(i, &dev);
GpuAdapter a;
// populate a.name, a.vendorId=0x10DE, a.dedicatedVramMB...
out.push_back(a);
}
nvmlShutdown();
}
// Then enumerate non-NVIDIA via libdrm (/dev/dri/card*)
// for Intel/AMD users on x86 Linux
// ...
return out;
}
```
`AggregateGpuCounter` (line 773) — the LUID-based attribution machinery — collapses on Linux because NVML hands you per-device handles directly. Replace with a simpler `for (auto& dev : nvmlDevices)` loop.
Caveat: `nvmlDeviceGetUtilizationRates` returns whole-device utilization, not per-process. Per-process GPU memory needs `nvmlDeviceGetProcessUtilization` (where supported) or `nvmlDeviceGetComputeRunningProcesses`. On Jetson, NVML support is partial — test early.
#### 3.5 — License-server HTTP (1 day)
Delete the `WinHttpHandle` block (lines 1101–1166) on Linux. Reuse `anslicensing/download.cpp`'s `UrlDownloadToString` libcurl path. The function signature is already compatible. One small wrinkle: `UrlDownloadToString` is in `anslicensing.dll`, so `ANSLicensingSystem` must link against it — it already does.
#### 3.6 — DNS public-IP probe (0.5 day)
Replace `DnsQuery_A` (line 1228) with `getaddrinfo`:
```cpp
struct addrinfo hints{}, *res = nullptr;
hints.ai_family = AF_INET;
if (getaddrinfo("myip.opendns.com", nullptr, &hints, &res) == 0 && res) {
char buf[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &((sockaddr_in*)res->ai_addr)->sin_addr, buf, sizeof(buf));
publicIp = buf;
freeaddrinfo(res);
}
```
If the existing Windows query uses a TXT record specifically, fall back to `libresolv res_query` for parity. Verify with the user which probe is in use.
#### 3.7 — CMake + linking (3 days)
Rewrite `ANSLicensingSystem/CMakeLists.txt` Linux branch:
```cmake
if(WIN32)
target_link_libraries(ANSLicensingSystem PRIVATE
HWiNFO.lib OpenCL.lib OpenCLExt.lib
bz2.lib zip.lib
libboost_log-vc143-mt-x64-1_82.lib
shlwapi.lib winhttp.lib pdh.lib psapi.lib
wbemuuid.lib oleaut32.lib ole32.lib dxgi.lib dnsapi.lib
${WIN_COMMON_LIBS}
)
else()
find_package(Boost REQUIRED COMPONENTS log date_time)
find_package(CURL REQUIRED)
target_link_libraries(ANSLicensingSystem PRIVATE
hwinfo::hwinfo # vendored or apt
Boost::log Boost::date_time
CURL::libcurl
zip # libzip-dev
z # zlib1g-dev
nvidia-ml # libnvidia-ml-dev (NVML)
drm # libdrm-dev (optional, for non-NVIDIA)
pthread dl
)
endif()
target_compile_definitions(ANSLicensingSystem PRIVATE
ANSLICENSE_EXPORTS
ANSLICENSINGSYSTEM_EXPORTS
$<$<PLATFORM_ID:Windows>:UNICODE _UNICODE>
)
```
Drop `ANSLIBS_DIR/ANS-HWiNFO/lib` and `ANSLIBS_DIR/boost/lib` from `target_link_directories` on Linux.
#### 3.8 — End-to-end license activation (3 days)
Run the full activation flow from a Linux client against the staging license server:
1. `GenerateHardwareId()` returns a string
2. Operator pastes it into the activation portal
3. Server returns license blob
4. Linux client `ValidateLicense(blob)` returns `SUCCESS`
5. Subsequent `IsLicenseValid()` calls return true
6. Restart process — license still valid
7. Move drive to second machine — license **invalid** (HWID mismatch detected)
The last item is the critical anti-piracy check. If it fails (license validates on second machine), the HWID is too lax — go back to Phase 1.1.
**Acceptance for Phase 3 overall**:
- `libANSLicensingSystem.so` builds and links cleanly.
- All public symbols (less `_LV` set) match Windows-side exports.
- Full activation round-trip succeeds against staging server.
- `SystemHealthInfo` struct populates all fields with real (non -1) data on Jetson and on x86 Ubuntu.
- Anti-piracy test passes (HWID mismatch is detected).
### Phase 4 — Integration & Hardening (1 week)
- **4.1** **Cross-arch CI**: add an `aarch64` build matrix entry. Cross-compile from x86 host using JetPack 6.x sysroot, or natively on a Jetson runner. Both options work; pick whichever the team prefers.
- **4.2** **Soak test**: run an ANSLicensingSystem-based test harness on a Jetson for 24 hours. Watch for memory leaks (`valgrind --leak-check=full` for a short run, then `top` / smaps tracking long-term). NVML on Jetson has known leak patterns — defend against them.
- **4.3** **Documentation**: write `docs/LINUX_BUILD_GUIDE.md` covering apt deps, Boost version notes, Jetson cross-compile, and the HWID format change (so support knows licenses don't transfer Windows↔Linux without re-issuance).
- **4.4** **Top-level CMake gating**: in the root `CMakeLists.txt`, remove `if(WIN32)` gates around the three components — they now build everywhere. Engine modules stay Windows-only for now since this plan doesn't cover them.
**Acceptance for Phase 4**:
- CI green on Windows, Ubuntu x86, Jetson aarch64.
- 24h soak shows stable memory profile.
- LINUX_BUILD_GUIDE.md merged and reviewed.
---
## 5. Risks & mitigations
| Risk | Likelihood | Impact | Mitigation |
|---|---|---|---|
| License server rejects Linux HWID format | Medium | **Blocks Phase 1 acceptance** | Resolve Decision 1 before Phase 0; coordinate with backend team early |
| Jetson `/sys/class/dmi/id/board_serial` missing | High | Phase 1 HWID is weak on Jetson | Decision 2: validate on real Jetson hardware; fall back to device-tree serial |
| NVML on Jetson missing some APIs | Medium | GPU telemetry sparse on Jetson | Test `nvmlDeviceGetUtilizationRates` early; gracefully degrade fields that aren't available |
| Boost version mismatch (1.74 system vs 1.82 vendored) | Medium | Subtle behavioral differences | Phase 0.5 spike build; switch to nlohmann/json if PropertyTree breaks |
| `lfreist/hwinfo` ARM support gaps | Low | HWID source incomplete on Jetson | Read its `cpuid.h` Linux branch; have device-tree fallback ready |
| Symbol visibility differences cause ABI breakage | Low | Linker errors at integration time | `-fvisibility=hidden` from day one; `ANSCORE_API` discipline |
| WMI thermal-zone has no Linux semantic equivalent | Low | `cpuTempDegC` field is less accurate | Document field as "best-effort, may be -1 on hardware without thermal zones" |
| `libnvidia-ml` not present on dev-only x86 box | Low | Build fails on developer machines | dynamic-load NVML via dlopen with weak symbol; fallback to "no GPU" state |
---
## 6. Dependencies for Ubuntu / Jetson development
```bash
# Build essentials
sudo apt install -y \
build-essential cmake ninja-build pkg-config git \
libboost-all-dev libcurl4-openssl-dev libssl-dev \
libzip-dev zlib1g-dev \
libnvidia-ml-dev libdrm-dev libsensors4-dev \
libgtest-dev cppcheck clang-tidy
# CUDA 12.x + cuDNN + TensorRT 10.x — from NVIDIA's apt repo (separate setup)
# JetPack 6.x — flash via SDK Manager onto target board
```
For Jetson cross-compile: install JetPack 6.x SDK on host, point CMake at `/opt/nvidia/jetson-linux-sysroot/aarch64-ubuntu22.04`.
---
## 7. Testing strategy
- **Unit tests** (`tests/anslicensing-unittest`): exercise each platform-specific helper with known synthetic data. New tests: HWID-determinism, getrandom-fallback, /proc/meminfo parser, /proc/stat delta computation.
- **Integration tests** (`tests/anslicensingsystem-integration`): in-process activation round-trip against a mock license server. Run on Windows + Linux + Jetson.
- **E2E tests**: real staging server. Run **once per phase acceptance**, not on every commit (slow + flaky over real network).
- **Anti-piracy regression test**: documented in Phase 3.8. **Must run before merging Phase 3 to main.**
- **Determinism test**: generate HWID 10× across reboots on the same machine; assert all identical. Generate on two physically different Jetsons; assert different.
---
## Appendix A — Per-file change checklist
Files marked **NEW** are created; **MOD** is modified; **DELETE** is removed.
### `core/anslicensing/`
| File | Action | Phase | Change |
|---|---|---|---|
| `hwid.cpp` | MOD | 1.1 | Linux branches: use `hwinfo::MainBoard::serialNumber()` for UUID slot; implement `GetHDDPropertyList` reading `/sys/block/*/device/wwid` |
| `rand.cpp` | MOD | 1.2 | Replace `srand`/`rand` with `getrandom(2)` + `/dev/urandom` fallback |
| `helper.cpp` | MOD | 1.3 | `DetectClockManipulation` Linux stub with ANS_DBG warning |
| `download.cpp` | — | — | Already done — libcurl path exists |
| `precomp.h` | — | — | Already done — POSIX shims exist |
| `CMakeLists.txt` | MOD | 1.4 | Add `find_package(hwinfo)` on non-Windows |
### `core/ANSLicensingSystem/`
| File | Action | Phase | Change |
|---|---|---|---|
| `ANSLicense.h` | MOD | 0.2 | Remove `#include "LabVIEWHeader/extcode.h"`; remove `LDbl`/`LDblPtr`/`LDblHandle` (moved to `ANSLicense_LV.h`) |
| `ANSLicense_LV.h` | NEW | 0.2 | Windows-only header with all `_LV` exports, includes `extcode.h` |
| `ANSLicense.cpp` | MOD | 3.1–3.6 | Replace each Win32 call site with `#ifdef _WIN32` / `#else` branch |
| `HealthSamplerLinux.cpp` | NEW | 3.3 | New file: `/proc`-based sampler |
| `HealthSamplerLinux.h` | NEW | 3.3 | Class declaration matching `HealthSampler`'s public API |
| `Utility.h` | MOD | 0.3 | Strip any Windows-specific types from declarations |
| `Utility.cpp` | MOD | 3.1 | Verify std::filesystem path operations work on Linux |
| `CMakeLists.txt` | MOD | 3.7 | Add Linux branch with curl/boost/hwinfo/nvml |
### `core/ANSLibsLoader/`
| File | Action | Phase | Change |
|---|---|---|---|
| `DynLibUtils.cpp` | MOD | 2.1 | dlopen/dlsym/dlclose Linux branches |
| `CvLoader.cpp` | MOD | 2.2 | Linux search paths + `.so` naming |
| `NvDynLoader.cpp` | MOD | 2.2 | Linux search paths |
| `OvLoader.cpp` | MOD | 2.2 | Linux search paths |
| `EPLoader.cpp` | MOD | 2.2 | Linux search paths |
| `ANSLibsLoader.def` | — | — | Windows-only `.def`; leave alone, use `__attribute__((visibility))` on Linux |
| `CMakeLists.txt` | MOD | 2.4 | `-fvisibility=hidden`; link `dl` on Linux |
### Top-level
| File | Action | Phase | Change |
|---|---|---|---|
| `CMakeLists.txt` | MOD | 0.4, 4.4 | `ANSCORE_USE_VENDORED_HWINFO` option; ungate three core components from `if(WIN32)` |
| `cmake/Dependencies.cmake` | MOD | 0.4, 3.7 | Add `hwinfo`, `NVML` (`CUDA::nvml`), `libdrm` resolution |
| `3rdparty/hwinfo/` | NEW (submodule) | 0.4 | `git submodule add https://github.com/lfreist/hwinfo 3rdparty/hwinfo` |
| `docs/LINUX_BUILD_GUIDE.md` | NEW | 4.3 | Developer onboarding |
---
## Appendix B — Symbol map (Win32 → Linux)
Reference table for the engineer porting `ANSLicense.cpp`:
| Win32 symbol / API | File:line | Linux replacement |
|---|---|---|
| `PdhOpenQueryW` / `PdhAddEnglishCounterW` / `PdhCollectQueryData` | ANSLicense.cpp:906–928 | Custom `/proc/stat`, `/proc/diskstats`, `/proc/net/dev` delta sampler (Phase 3.3) |
| `IWbemLocator::ConnectServer("ROOT\\WMI")` + `MSAcpi_ThermalZoneTemperature` | ANSLicense.cpp:618–663 | `/sys/class/thermal/thermal_zone*/temp` (Phase 3.2) |
| `CreateDXGIFactory1` / `IDXGIAdapter1::GetDesc1` | ANSLicense.cpp:721–747 | `nvmlDeviceGetCount_v2` + per-device queries; libdrm for non-NVIDIA (Phase 3.4) |
| `WinHttpOpen` / `WinHttpSendRequest` / etc. | ANSLicense.cpp:1101–1166 | Reuse `anslicensing/download.cpp` libcurl path (Phase 3.5) |
| `DnsQuery_A` | ANSLicense.cpp:1228 | `getaddrinfo` or `res_query` (Phase 3.6) |
| `GlobalMemoryStatusEx` | ANSLicense.cpp:1036 | `/proc/meminfo` parser (Phase 3.1) |
| `GetProcessMemoryInfo` | ANSLicense.cpp:1048 | `/proc/self/status` parser (Phase 3.1) |
| `GetSystemDirectoryW` + `GetDiskFreeSpaceExW` | ANSLicense.cpp:1056–1064 | `statvfs("/")` (Phase 3.1) |
| `GetCurrentProcessId` | ANSLicense.cpp:993, 1026 | `getpid()` (Phase 3.1) |
| `WideCharToMultiByte` / `WideToUtf8` | ANSLicense.cpp:712 | drop on Linux — no wide chars needed without WMI |
| `CoInitializeEx` / `CoSetProxyBlanket` / `CoUninitialize` | ANSLicense.cpp:604, 611, 669 | delete on Linux — COM not needed |
| `__declspec(dllexport)` | (everywhere) | `__attribute__((visibility("default")))` via `ANSCORE_API` macro (Phase 0.1) |
| `LStrHandle`, `LDbl`, `extcode.h` | ANSLicense.h:75 + many | move to `ANSLicense_LV.h`, Windows-only (Phase 0.2) |
---
## Appendix C — License-server coordination checklist
Before Phase 1 starts, the license-server team confirms in writing:
- [ ] Server accepts HWID format generated by the new Linux `hwid.cpp` path
- [ ] Server's HWID validation algorithm matches the new client-side hash inputs
- [ ] Decision on license-pool separation: unified vs. Linux-only pool
- [ ] Staging environment endpoint available for E2E testing in Phase 3.8
- [ ] Activation portal supports pasting a Linux-generated HWID
---
## Appendix D — What this plan deliberately does NOT cover
So a future session doesn't accidentally scope-creep:
- **MediaClient port to Linux** — separate plan; ANSCV depends on this but licensing does not
- **GStreamer backend for ANSCV** — separate plan
- **Engine modules** (ANSODEngine, ANSFR, ANSLPR, ANSOCR, etc.) — separate plans, post-licensing
- **gRPC service layer** (ANSService) — planned after engine modules are up
- **Qt QML UI** — separate repo, depends on gRPC layer
- **macOS support** — not in roadmap currently
- **Dropping LabVIEW on Windows** — `_LV` exports stay on Windows builds untouched
- **License protocol changes** — wire compatibility preserved; only the input data to the HWID hash changes on Linux
---
*End of plan.*
+48
View File
@@ -0,0 +1,48 @@
@startuml 01-system-context
title ANSCORE — System Context (L1 / L2 / L3)
skinparam componentStyle rectangle
skinparam packageStyle rectangle
package "Layer 3 — Qt/QML Desktop UI\n(Windows / macOS / Linux)" as L3 {
component "Qt/QML application" as UI
}
package "Layer 2 — ANSVIS Communication Layer (CL)" as L2 {
component "/api/v1\nREST surface" as APIV1
component "Legacy ANSVIS core\n(reused as-is)" as LEGACY
component "Media relay\nHLS / RTSP / WebRTC" as RELAY
component "SSE event\nbroadcaster" as SSEHUB
database "Event DB\n+ clip object store" as STORE
queue "Async job queue" as JOBS
component "Auth + RBAC" as AUTH
}
package "Layer 1 — ANSCORE Edge Runtime\n(one per target)" as L1 {
component "Camera + Inference\n+ Triggers" as EDGE
}
UI --> APIV1 : (1) REST / HTTPS\ncontrol + CRUD
UI --> SSEHUB : (2) SSE / HTTPS\nlive event tail
UI --> RELAY : (3) HLS / RTSP / WebRTC\nlive video
APIV1 --> AUTH
APIV1 --> LEGACY
APIV1 --> JOBS
APIV1 --> STORE
LEGACY --> STORE
STORE --> SSEHUB
RELAY <--> EDGE
LEGACY <--> EDGE
note bottom of L3
L3 only talks to L2 — never directly to edges.
end note
note bottom of L2
All three UI transports are brokered by the REST API:
the URL + token for live media and the SSE filter for
events are obtained via REST calls first.
end note
@enduml
+84
View File
@@ -0,0 +1,84 @@
@startuml 02-l2-internals
title Layer 2 — ANSVIS internal architecture
skinparam componentStyle rectangle
skinparam packageStyle rectangle
actor "Qt / QML UI" as QT
actor "3rd-party\nintegrations" as EXT
package "L2 /api/v1 surface" as EDGE_API {
component "REST router" as ROUTER
component "Auth middleware\n(Bearer)" as AUTHMW
component "Request validation\n(OpenAPI schemas)" as VALID
component "Error mapper\n(RFC 7807)" as ERR
component "Rate limiter" as RATE
component "SSE broadcaster\n(per-subscription filter)" as SSEHUB
}
package "v1 Adapter Layer\n(per-resource modules)" as ADAPTER {
component "cameras" as CAM_AD
component "models" as MOD_AD
component "edges" as EDGE_AD
component "deployment-groups" as DG_AD
component "ai-tasks" as TASK_AD
component "events" as EV_AD
component "fr" as FR_AD
component "alpr" as ALPR_AD
component "notifications" as NOTIF_AD
component "system" as SYS_AD
component "jobs" as JOB_AD
}
package "Legacy ANSVIS server\n(reused as-is)" as LEGACY {
component "Config store" as LEG_CFG
component "AI task engine" as LEG_TASKS
component "FR engine" as LEG_FR
component "ALPR engine" as LEG_ALPR
}
package "Infrastructure" as INFRA {
component "Auth + token store" as AUTH_SVC
queue "Job queue\n(scans, uploads, deploys, updates)" as JOBS_Q
database "Event metadata DB" as EVENTS_DB
database "Clip / image object store\n(S3 / MinIO / FS)" as CLIPS
component "Logs + metrics + health" as LOGS
component "Media relay\nHLS / RTSP / WebRTC" as RELAY
}
actor "L1 edge target(s)" as E1
QT --> ROUTER
EXT --> ROUTER
ROUTER --> AUTHMW
AUTHMW --> VALID
VALID --> RATE
RATE --> ADAPTER
ADAPTER --> ERR
ADAPTER ..> ROUTER : response
AUTHMW --> AUTH_SVC
CAM_AD --> LEG_CFG
MOD_AD --> LEG_CFG
MOD_AD --> CLIPS
EDGE_AD --> LEG_CFG
DG_AD --> LEG_TASKS
TASK_AD --> LEG_TASKS
EV_AD --> EVENTS_DB
EV_AD --> CLIPS
FR_AD --> LEG_FR
ALPR_AD --> LEG_ALPR
SYS_AD --> LOGS
JOB_AD --> JOBS_Q
CAM_AD ..> JOBS_Q : scan / probe
MOD_AD ..> JOBS_Q : large upload
LEG_TASKS --> EVENTS_DB
EVENTS_DB --> SSEHUB
SSEHUB --> ROUTER
LEGACY <--> E1
RELAY <--> E1
@enduml
+51
View File
@@ -0,0 +1,51 @@
@startuml 03-l2-ownership
title Layer 2 — Build ownership
skinparam componentStyle rectangle
skinparam packageStyle rectangle
package "Architect-owned\n(lead review)" as HUMAN #LightYellow {
component "REST router\n+ auth middleware" as H1
component "RFC 7807\nerror mapper" as H2
component "OpenAPI validation\npipeline" as H3
component "Auth flow\n+ token store" as H4
component "RBAC\n+ security review" as H5
component "L2 ↔ L1 tunnel\n+ signing" as H6
}
package "Core team\n(modular adapters)" as AI #LightBlue {
component "cameras adapter" as A1
component "models adapter\n+ upload pipeline" as A2
component "edges adapter" as A3
component "deployment-groups\nadapter" as A4
component "ai-tasks adapter" as A5
component "events adapter\n+ SSE broadcaster" as A6
component "fr adapter" as A7
component "alpr adapter" as A8
component "notifications adapter" as A9
component "system / health /\nlogs adapter" as A10
component "job queue + workers" as A11
component "media relay" as A12
}
package "Existing ANSVIS\n(reused as-is)" as EXIST #LightGray {
component "Task engine" as L_TASKS
component "FR engine" as L_FR
component "ALPR engine" as L_ALPR
component "Config store" as L_CFG
}
HUMAN --> AI
AI --> EXIST
note bottom of HUMAN
Cross-cutting concerns kept under direct architect review:
request validation, errors, auth, RBAC, tunnel security.
end note
note bottom of AI
Highly modular by resource — the core team works in parallel
on independent adapters without colliding.
end note
@enduml
+65
View File
@@ -0,0 +1,65 @@
@startuml 04-l3-internals
title Layer 3 — Qt 6 / QML internal architecture
skinparam componentStyle rectangle
skinparam packageStyle rectangle
package "Qt 6 + QML — Layer 3" as QT {
package "App Shell" as SHELL {
component "Main window" as MAIN
component "Navigation router" as ROUTE
component "Design system / theme" as THEME
component "i18n (qsTr / Linguist)" as I18N
component "Auth state + token store" as AUTH_ST
component "Error toast / dialog" as ERR_UI
}
package "Network layer" as NET {
component "Generated REST client\n(openapi.v1.json → C++/Qt)" as REST
component "SSE subscriber\n(QNetworkReply chunked)" as SSE_CL
component "Media client\nQtMultimedia / GStreamer" as MEDIA
component "Bbox overlay renderer\n(custom QQuickItem)" as OVERLAY
component "Multipart + presigned\nuploader" as UPLOAD
}
package "Domain state (QObject + signals)" as STATE {
component "CamerasModel" as ST_CAM
component "ModelsModel" as ST_MOD
component "EdgesModel" as ST_EDGE
component "DeploymentGroupsModel" as ST_DG
component "AITasksModel" as ST_TASK
component "EventsModel + LiveBuffer" as ST_EV
component "FRPersonsModel" as ST_FR
component "ALPRPlatesModel" as ST_ALPR
component "RecipientsModel" as ST_NOTIF
component "SystemHealthModel" as ST_SYS
}
package "Screens (QML)" as SCREENS {
component "Login" as LOGIN
component "Dashboard /\ntargets overview" as DASH
component "Camera management\n+ ONVIF discovery\n+ calibration" as CAM_S
component "Pipeline editor\n(visual DAG)" as DG_EDIT
component "AI Task config" as TASK_EDIT
component "Rules / triggers" as RULE_S
component "Liveview grid" as LIVE
component "Eventview timeline" as EV_S
component "FR persons + faces" as FR_S
component "ALPR plates" as ALPR_S
component "Model registry" as MODEL_S
component "Users / admin" as USERS
component "Settings" as SETTINGS
}
}
actor "ANSVIS /api/v1" as BACKEND
SCREENS --> STATE : reads / mutates
STATE --> NET : calls
NET <--> BACKEND
OVERLAY ..> MEDIA : sync to PTS
AUTH_ST ..> REST : injects Bearer
AUTH_ST ..> SSE_CL : injects Bearer
@enduml
@@ -0,0 +1,45 @@
@startuml 05-seq-configure-deploy
title Sequence — Operator configures camera + AI task and deploys
actor "Operator" as User
participant "Qt UI" as UI
participant "ANSVIS\n/api/v1" as API
participant "ANSVIS core" as LEG
participant "L1 edge" as EDGE
User -> UI : Open Cameras screen
UI -> API : GET /cameras?page=1
API -> LEG : legacy list
LEG --> API : cameras
API --> UI : 200 {items, page, total}
User -> UI : Add RTSP camera (form)
UI -> API : POST /cameras
note right of UI
body has nested credentials,
capturing, image_size —
no stringified JSON
end note
API -> LEG : legacy add
LEG --> API : id=123
API --> UI : 201 Location: /cameras/123
User -> UI : Configure AI task
UI -> API : POST /ai-tasks\n{camera_id:123, ...}
API -> LEG : legacy create
LEG --> API : id=456
API --> UI : 201 {id:456}
User -> UI : Start task
UI -> API : POST /ai-tasks/456/start
API -> LEG : dispatch
LEG -> EDGE : config + start
EDGE --> LEG : running
API --> UI : 202 {state:starting}
loop status poll (or SSE later)
UI -> API : GET /ai-tasks/456/status
API --> UI : 200 {state:running, fps:14.2}
end
@enduml
+36
View File
@@ -0,0 +1,36 @@
@startuml 06-seq-live-events
title Sequence — Live event tail + clicking into evidence
actor "Operator" as User
participant "Qt UI" as UI
participant "ANSVIS\n/api/v1" as API
participant "SSE broadcaster" as SSE
participant "Clip store\n(S3 / MinIO / FS)" as CLIPS
participant "L1 edge" as EDGE
User -> UI : Open Eventview (task=456)
UI -> API : GET /events?task_id=456&page=1
API --> UI : 200 historical events
UI -> API : GET /events/stream?task_id=456
API --> UI : text/event-stream (open)
EDGE -> API : event arrives (via L1↔L2)
API -> SSE : push EventRecord
SSE --> UI : data: {id:E1, ts, object:car, bbox, ...}
User -> UI : Click event E1
UI -> API : GET /events/E1
API --> UI : 200 EventRecord (full)
UI -> API : GET /events/E1/image?width=640
API --> UI : image/jpeg bytes
User -> UI : Play clip
UI -> API : GET /events/E1/clip
API -> CLIPS : presign GET
CLIPS --> API : presigned URL
API --> UI : 200 {url, expires_at}
UI -> CLIPS : GET <presigned URL>
CLIPS --> UI : MP4 bytes
UI -> UI : play in QtMultimedia
@enduml
+30
View File
@@ -0,0 +1,30 @@
@startuml 07-seq-liveview
title Sequence — Liveview with bbox overlay
actor "Operator" as User
participant "Qt UI" as UI
participant "ANSVIS\n/api/v1" as API
participant "Media relay" as RELAY
participant "L1 edge" as EDGE
participant "SSE broadcaster" as SSE
User -> UI : Open Liveview (cam 123, task 456)
UI -> API : GET /cameras/123/liveview?transport=hls
API -> RELAY : ensure HLS for cam 123
RELAY -> EDGE : pull RTSP
RELAY --> API : hls_url + short-lived token
API --> UI : 200 {url, transport:hls,\nauth_token, expires_at}
UI -> RELAY : GET hls_url (token)
RELAY --> UI : m3u8 + segments
UI -> UI : Render video\n(QtMultimedia)
UI -> API : GET /events/stream?task_id=456 (SSE)
API --> UI : text/event-stream
loop per event
SSE --> UI : bbox + ts
UI -> UI : overlay synced to PTS
end
@enduml
+27
View File
@@ -0,0 +1,27 @@
@startuml 08-seq-async-scan
title Sequence — Async camera network scan (job pattern)
actor "Operator" as User
participant "Qt UI" as UI
participant "ANSVIS\n/api/v1" as API
participant "Job queue" as JOBS
participant "ONVIF scanner" as SCAN
User -> UI : Scan 192.168.1.0/24
UI -> API : POST /cameras/scan
API -> JOBS : enqueue
JOBS --> API : job_id=j1
API --> UI : 202 {job_id:j1,\n status:queued,\n links:{self:/jobs/j1}}
JOBS -> SCAN : run
loop poll until terminal
UI -> API : GET /jobs/j1
API --> UI : 200 {status:running, progress:0.4}
end
SCAN --> JOBS : results
UI -> API : GET /jobs/j1
API --> UI : 200 {status:succeeded,\n result:{discovered:[...]}}
@enduml
+23
View File
@@ -0,0 +1,23 @@
@startuml 09-seq-large-upload
title Sequence — Large model upload (initiate / complete pattern)
actor "Operator" as User
participant "Qt UI" as UI
participant "ANSVIS\n/api/v1" as API
participant "Object store\n(S3 / MinIO / FS)" as OBJ
User -> UI : Pick model.zip (500 MB)
UI -> API : POST /models:initiate-upload\n{filename, size_bytes, checksum}
API -> OBJ : presign PUT
OBJ --> API : presigned_url
API --> UI : 200 {upload_id, presigned_url, expires_at}
UI -> OBJ : PUT <presigned_url>\n(binary stream, with progress)
OBJ --> UI : 200 OK
UI -> API : POST /models:complete-upload\n{upload_id, metadata}
API -> OBJ : verify checksum
OBJ --> API : ok
API --> UI : 201 Model
@enduml
+51
View File
@@ -0,0 +1,51 @@
@startuml 10-deployment-topology
title Deployment Topology
skinparam componentStyle rectangle
node "Customer site / cloud" {
node "Operator workstations" {
component "Windows desktop\nQt UI" as D1
component "macOS desktop\nQt UI" as D2
component "Linux desktop\nQt UI" as D3
}
node "ANSVIS server host" {
component "Reverse proxy\nTLS terminator\n(nginx / caddy)" as NGINX
component "ANSVIS app\n(adapter + legacy core)" as APP
component "Media relay" as RELAY
database "Object store\n(S3 / MinIO / FS)" as OBJ
database "Config + Event DBs" as DB
}
node "Edge targets" {
node "Linux x86 + NVIDIA" as E1
node "Windows x86 + NVIDIA" as E2
node "Jetson Orin" as E3
}
}
D1 --> NGINX : TLS 443\nREST + SSE + HLS
D2 --> NGINX : TLS 443
D3 --> NGINX : TLS 443
NGINX --> APP
NGINX --> RELAY
APP --> DB
APP --> OBJ
RELAY <--> E1
RELAY <--> E2
RELAY <--> E3
APP <--> E1
APP <--> E2
APP <--> E3
note bottom
Network rules for the customer:
- Outbound TCP 443 from each workstation → ANSVIS host
- Inbound TCP 443 on ANSVIS host
- L2 ↔ L1 protocol per edge (port + tunnel TBD)
- Workstations never reach edges directly — Media relay proxies
end note
@enduml
@@ -0,0 +1,71 @@
@startuml 11-l3-engineer-ownership
title Layer 3 — Engineer ownership (6 engineers)
skinparam componentStyle rectangle
skinparam packageStyle rectangle
package "E1 — UI Lead / Shell Architect" as E1 #LightYellow {
component "Qt 6 + CMake monorepo" as E1A
component "Generated REST client\nfrom openapi.v1.json" as E1B
component "Auth state + token store" as E1C
component "Design system / theme" as E1D
component "Navigation router" as E1E
component "Packaging Win/Mac/Linux\nsigning, notarize,\nauto-update" as E1F
component "CI matrix" as E1G
}
package "E2 — Pipeline Editor Specialist" as E2 #LightBlue {
component "Visual DAG editor\n(QGraphicsView in QQuickWidget)" as E2A
component "Node library +\nconnection validation" as E2B
component "Schema-driven param panels" as E2C
component "Undo / redo,\nsave / version" as E2D
}
package "E3 — Liveview / Media" as E3 #LightGreen {
component "QtMultimedia HLS player\n(v1)" as E3A
component "Multi-stream grid" as E3B
component "Bbox overlay\n(QQuickItem + QSGGeometryNode)" as E3C
component "Clip player (evidence)" as E3D
component "WebRTC via GStreamer\n(v2)" as E3E
}
package "E4 — Config Forms" as E4 #LightPink {
component "Schema-driven QML forms\n(from openapi.v1.json)" as E4A
component "Camera + ONVIF discovery flow" as E4B
component "Camera calibration UI\n(ROI / zones / homography)" as E4C
component "Model + plugin +\ntrigger forms" as E4D
}
package "E5 — Dashboard / Eventview" as E5 #LightCoral {
component "Target dashboard\ncards + charts" as E5A
component "Event timeline\n(virtualized)" as E5B
component "SSE subscriber + LiveBuffer" as E5C
component "Filter / search /\nevidence viewer" as E5D
component "Multi-target monitoring" as E5E
}
package "E6 — UX / Cross-platform QA" as E6 #Lavender {
component "Design polish,\na11y, i18n" as E6A
component "Install / upgrade testing\nWin + Mac + Linux" as E6B
component "Crash reporting\n(Sentry / Crashpad)" as E6C
component "QML Profiler perf passes" as E6D
component "User docs surface" as E6E
}
artifact "openapi.v1.json\n(frozen contract)" as CONTRACT
E1 ..> CONTRACT
E2 ..> CONTRACT
E3 ..> CONTRACT
E4 ..> CONTRACT
E5 ..> CONTRACT
note bottom of E1
Week 1–2: E1 alone delivers the shell that unblocks E2–E6.
end note
note bottom of E2
Week 1–2 spike in parallel — de-risks the hardest UI feature.
end note
@enduml
+43
View File
@@ -0,0 +1,43 @@
@startuml 12-dependency-chain
title Dependency chain — what gates what
skinparam componentStyle rectangle
artifact "openapi.v1.json\n(frozen)" as CONTRACT
component "Prism mock server" as MOCK
component "ANSVIS /api/v1 adapter\n(core team, per-resource)" as ADAPT
component "Legacy ANSVIS" as LEGACY
component "L3 shell + REST client\n(E1)" as UI_SHELL
component "L3 screens\n(E2 – E6)" as UI_FEAT
component "Real backend integration" as REAL
component "Ship v1" as SHIP
CONTRACT --> MOCK
CONTRACT --> ADAPT
ADAPT --> LEGACY
MOCK --> UI_SHELL
UI_SHELL --> UI_FEAT
UI_FEAT --> REAL
ADAPT --> REAL
REAL --> SHIP
note bottom of CONTRACT
Only artifact that BOTH sides block on.
Frozen once published; additive changes only.
end note
note bottom of MOCK
UI develops here from day 1.
end note
note bottom of ADAPT
Built in parallel by the core team.
Per-resource modularity = independent progress.
end note
note bottom of REAL
Joining point. UI tests run against both mock and real
to detect drift.
end note
@enduml
+19
View File
@@ -0,0 +1,19 @@
# ANSCORE — L2 & L3 diagrams (preview)
Click each PNG link to view the rendered diagram in your browser.
SVG links are higher-quality (vector). Both come from plantuml.com.
| # | Diagram | What it shows | PNG | SVG |
|---|---|---|---|---|
| 01 | **system context** | L1 / L2 / L3 with the 3 transports (REST / SSE / HLS-RTSP-WebRTC). | [PNG](https://www.plantuml.com/plantuml/png/RLJRRjf047ttLupo51HH2dchgYe6h43bD8alYIhnihO7ic6zw-wkQL1LgH_HB-oNT7ODWf2y6E_ud3ENiyELiKpRjXRmSTWtMsEnxfTAMdonWULM88JVqibj4i6_FtyXzG2OT00uZuSmW7Za7fUz831hBXkcMGsbgXibKThKRab3OscPN0ayWZIiNBCLdWA2NHpEOhP53PS-yRqTtD_4C4Mpjgg1VBwGvuzSLkgdeUGrAszJLmINxKll39YXUk1N08Swu6mdmPf6y99PhgG7vlFWzvka8v-KEd-Ofp2vIJl9qQdaW3Lyi1akP1Ab6PXMBrc97XlUpH-6hy4nhbYvtOkMIYFreB4rM16ZpyskyEWwd7n_JRt1YZEQaM3RXPp5hjKaI-_evn6B99juOXB5uGalJIF03RqkPA4Lgqf6zccFfa-pV4pmYbbMC4EjHmu9q_52Na0fU0EgU2PRm5YgjIDbT168ywF5bWYXsSeIdbNH1Jpaw-quVLr4sDedk81a77PrXdasUsVam_t8_MMBAlgIjDBoses9fA0X4DtL5ThTe8QdTanOZPfHhhbSeaPPecibqtorGcqyAPfUHovvFeT-_qjd4to2ys4Fl8S3c6NPNRgGRWUq4YGsIVBf7jzDpH56FNSuu0NVO3ThAfABFS6Rul2NFNZ7k1rlmojKGT1Lu-joCpeSTtVY4F1JFXmxNphKKS0_Z-eEkce-kuWVn8vn20HIMOH2MQjgK4lQ8femhPAIOajzYRK1gzmYEAyajQkXucvttMS5IAwP3m7A2fpGMxaHoOL2W7tIYBHwO3MJfb7Q6c2Qe5gjoRaA2j9xmiuJwlCJyTmvJs9on198mb9fyDEh_NOmIkeWpfKb5tJF7O9utfHENXNaZIJv3H6yTic4C8JNnXxNVKMlz9yO_0S0) | [SVG](https://www.plantuml.com/plantuml/svg/RLJRRjf047ttLupo51HH2dchgYe6h43bD8alYIhnihO7ic6zw-wkQL1LgH_HB-oNT7ODWf2y6E_ud3ENiyELiKpRjXRmSTWtMsEnxfTAMdonWULM88JVqibj4i6_FtyXzG2OT00uZuSmW7Za7fUz831hBXkcMGsbgXibKThKRab3OscPN0ayWZIiNBCLdWA2NHpEOhP53PS-yRqTtD_4C4Mpjgg1VBwGvuzSLkgdeUGrAszJLmINxKll39YXUk1N08Swu6mdmPf6y99PhgG7vlFWzvka8v-KEd-Ofp2vIJl9qQdaW3Lyi1akP1Ab6PXMBrc97XlUpH-6hy4nhbYvtOkMIYFreB4rM16ZpyskyEWwd7n_JRt1YZEQaM3RXPp5hjKaI-_evn6B99juOXB5uGalJIF03RqkPA4Lgqf6zccFfa-pV4pmYbbMC4EjHmu9q_52Na0fU0EgU2PRm5YgjIDbT168ywF5bWYXsSeIdbNH1Jpaw-quVLr4sDedk81a77PrXdasUsVam_t8_MMBAlgIjDBoses9fA0X4DtL5ThTe8QdTanOZPfHhhbSeaPPecibqtorGcqyAPfUHovvFeT-_qjd4to2ys4Fl8S3c6NPNRgGRWUq4YGsIVBf7jzDpH56FNSuu0NVO3ThAfABFS6Rul2NFNZ7k1rlmojKGT1Lu-joCpeSTtVY4F1JFXmxNphKKS0_Z-eEkce-kuWVn8vn20HIMOH2MQjgK4lQ8femhPAIOajzYRK1gzmYEAyajQkXucvttMS5IAwP3m7A2fpGMxaHoOL2W7tIYBHwO3MJfb7Q6c2Qe5gjoRaA2j9xmiuJwlCJyTmvJs9on198mb9fyDEh_NOmIkeWpfKb5tJF7O9utfHENXNaZIJv3H6yTic4C8JNnXxNVKMlz9yO_0S0) |
| 02 | **l2 internals** | L2 adapter modules in front of legacy ANSVIS + infrastructure. | [PNG](https://www.plantuml.com/plantuml/png/TLPVRzis47_NfpZagSFcfaaVEWp3KDcM7NLoR4jAkW0535ewosmaIYEf34PHO1zYdt2VP4TIScmPUp6fkzyTx-yl-QWqaxgjIdXtEoflHrneb8ALojDSbmWHEw24M_Ztxt_0lqyUmmHw232PxRd6JBSIFKyzSz4moIh8wggf1GgTw0Eva8HWeYZn1DAmx9aLE0HuBDErXAkrXcjOBo9u2A-0ALYdlUQzp4VaGX--21D78PdcjL0M5VoHUbxd6QwYMxXc3Rz-kG7LoXtBq86cys3ZhqBuvi5hf70L1qaAicufDukBbmzf49zZ_5RleU9vNk9VJE8NyMQCTCeVh8N_aDujFW-yufyjAWqlhEIv3PMCbWqA4u7AzbWnvQmV_IYSdXi7KbBA5MiQb6GMpoRmuUTt7nm-Y0V1nKmZbBpYnmpyD3Y794a0MrcpF6DAMv_aUQJQhSeaRqnqiEEbxXCY-Dt3sFj-KbGgff-pXY1kCZeN4bNToWoXglEsn2ubV-glg8R3IcUiGibSooR-OkCFqYOVM3hrOZczK6DUe3ffvK2TOrFMXuhkeuBQsJZeT7u1P7oacNfs-jHFVhzywOMExgd72-tELNaMNtek6wVpezMbLjIQxtXsChVtopISNU3KWNfKkKOy9HVgh_NMMNzQZetoj4iH5YmxzFkgKBxOJabi5UPaC-9TXw9Wxa-UXWsQr6B72r2qSDZ3DfFPVB0C8PZw0Og2YrUSgMHoZfp5Gz1ik5XKg276oCwo2iMEnaRBrXAEnONtizWVXc-tz4VGzJEAaoJCUcwInmc1QITRSlYfthehrKPbJAYVeANXOJbTt1XP2UrjFz5KwsIp9WyaOrkco4bWXWGgrCp8O3hkvYMuJvFDT7o6dPIy8MBZbScdtdubtdC1KW39Uz8ik0YNTCuIzzua2bU3OaPreIWxUb3opDpso4gzTsLRpWVe1UQS4S6MZ0ZpBah8TvmcApe-upPE9uueWiX_CgNkE3Qw0RDar5vPe7xJvHxSUDuwXT7ejvuRYMzFFzrX9HqLkiDA7BtPNsTZoCdyEBnZ2gzd32Cp1DT_lttRlmA_K3QAqbFqr-B4_x6xdgCKA-pdrj78_ubSaJioEGDPrZXAt6HtJ36GMdvmOHyx_ofpBrYcEDhH2dJqS1JPYNUxxcIcdMwzxNStVLs2fYPEGZKnuqjDRMIznJwtCq1fUjcDj-ST0xy8kRjPUSV_xZZjjDVnng_M-iQp0tJyyZwYoEa_2ky_) | [SVG](https://www.plantuml.com/plantuml/svg/TLPVRzis47_NfpZagSFcfaaVEWp3KDcM7NLoR4jAkW0535ewosmaIYEf34PHO1zYdt2VP4TIScmPUp6fkzyTx-yl-QWqaxgjIdXtEoflHrneb8ALojDSbmWHEw24M_Ztxt_0lqyUmmHw232PxRd6JBSIFKyzSz4moIh8wggf1GgTw0Eva8HWeYZn1DAmx9aLE0HuBDErXAkrXcjOBo9u2A-0ALYdlUQzp4VaGX--21D78PdcjL0M5VoHUbxd6QwYMxXc3Rz-kG7LoXtBq86cys3ZhqBuvi5hf70L1qaAicufDukBbmzf49zZ_5RleU9vNk9VJE8NyMQCTCeVh8N_aDujFW-yufyjAWqlhEIv3PMCbWqA4u7AzbWnvQmV_IYSdXi7KbBA5MiQb6GMpoRmuUTt7nm-Y0V1nKmZbBpYnmpyD3Y794a0MrcpF6DAMv_aUQJQhSeaRqnqiEEbxXCY-Dt3sFj-KbGgff-pXY1kCZeN4bNToWoXglEsn2ubV-glg8R3IcUiGibSooR-OkCFqYOVM3hrOZczK6DUe3ffvK2TOrFMXuhkeuBQsJZeT7u1P7oacNfs-jHFVhzywOMExgd72-tELNaMNtek6wVpezMbLjIQxtXsChVtopISNU3KWNfKkKOy9HVgh_NMMNzQZetoj4iH5YmxzFkgKBxOJabi5UPaC-9TXw9Wxa-UXWsQr6B72r2qSDZ3DfFPVB0C8PZw0Og2YrUSgMHoZfp5Gz1ik5XKg276oCwo2iMEnaRBrXAEnONtizWVXc-tz4VGzJEAaoJCUcwInmc1QITRSlYfthehrKPbJAYVeANXOJbTt1XP2UrjFz5KwsIp9WyaOrkco4bWXWGgrCp8O3hkvYMuJvFDT7o6dPIy8MBZbScdtdubtdC1KW39Uz8ik0YNTCuIzzua2bU3OaPreIWxUb3opDpso4gzTsLRpWVe1UQS4S6MZ0ZpBah8TvmcApe-upPE9uueWiX_CgNkE3Qw0RDar5vPe7xJvHxSUDuwXT7ejvuRYMzFFzrX9HqLkiDA7BtPNsTZoCdyEBnZ2gzd32Cp1DT_lttRlmA_K3QAqbFqr-B4_x6xdgCKA-pdrj78_ubSaJioEGDPrZXAt6HtJ36GMdvmOHyx_ofpBrYcEDhH2dJqS1JPYNUxxcIcdMwzxNStVLs2fYPEGZKnuqjDRMIznJwtCq1fUjcDj-ST0xy8kRjPUSV_xZZjjDVnng_M-iQp0tJyyZwYoEa_2ky_) |
| 03 | **l2 ownership** | Human-owned vs AI-coder-built vs reused legacy modules in L2. | [PNG](https://www.plantuml.com/plantuml/png/PLNRRXCn47tVhnYL5r2xj0tG5XvGjo5f8qAfkWM15AborfEDYTTUR6_32Y7nn1Vq2_aIPYzfO_CKQtNE-FZCcScfTSougf1my2AMWrYl5Hgx56NaX9C8KrQZWG7y_NqFPvMG71uHaLq9LJB32iXqKMg5ogMk9fR1p36LIzo2b2nRiHn3GDH_XvsBgc2g5S1dwYapsL8uWZtR0MRXujE7v1AUJ4M-T5zHIhs6dn4ytWixrwFq1eok79gPsWLMkIKKWdE9QsQmAt8OKCP3E3uvE9xHYumsKB2oHDD11pxqOuagkPh07PE2CoUqcgbIb2Y5wck_y0b9S_-2T3PYd5wX0klqHiZBGCXPCcnm5hFA25UJFtS2rntsbO-TKZF-tCFq45ob5CgM9t8bLDxXZw9VMwucapZJ74qyf-uvChRGl9BC0EEi9AzivsyowSqzanM6rcQiG4EWdj8H0Y-fBCe7I2EgAgLc73oNai1Mv3a6TGCVELAPkg1pd5DtIpjJ7ZnmaedOCRiAQWOMuXtzU5hJT0HpGt8pPXzOHpvhOVoYny7DiWm09pv0QISM8cl34-XxxIDjJI8As8SbCaansfyfgVFG_mEVz4tFuNk5rBrTM6kpejRsmB1Jo0MZX4bMzu21bvZH3s4TfGcIo_Jp9AN86AmiSiB6ee_BwCk4fgrBpBbXTPYO6-e1eCetVP_UtYJf-zG7ZQzzoFWw6A7fLO1ellYOeLOBaM-DrlHsE3vltjDjZ3X-IzcEADxDgPKTHTG9XBbsJXUW5zrkeRBlQ2CXPSp2N50yt1A1tcrWXLXQg1HD4N3HhAv-FjyGoI2PRjtMOjY3TflOlNO7xK5cjBLnLhdMrCs8FuzGSMYK_AU7z09SaBMoXisqpfkTO7LbCcntCJse7MiB6LDjlq4eQ3Qjb2Y9hnLzeE6XMngZDjCEQ-6Mj2I9BKamATeMSaf7-Y-8_W40) | [SVG](https://www.plantuml.com/plantuml/svg/PLNRRXCn47tVhnYL5r2xj0tG5XvGjo5f8qAfkWM15AborfEDYTTUR6_32Y7nn1Vq2_aIPYzfO_CKQtNE-FZCcScfTSougf1my2AMWrYl5Hgx56NaX9C8KrQZWG7y_NqFPvMG71uHaLq9LJB32iXqKMg5ogMk9fR1p36LIzo2b2nRiHn3GDH_XvsBgc2g5S1dwYapsL8uWZtR0MRXujE7v1AUJ4M-T5zHIhs6dn4ytWixrwFq1eok79gPsWLMkIKKWdE9QsQmAt8OKCP3E3uvE9xHYumsKB2oHDD11pxqOuagkPh07PE2CoUqcgbIb2Y5wck_y0b9S_-2T3PYd5wX0klqHiZBGCXPCcnm5hFA25UJFtS2rntsbO-TKZF-tCFq45ob5CgM9t8bLDxXZw9VMwucapZJ74qyf-uvChRGl9BC0EEi9AzivsyowSqzanM6rcQiG4EWdj8H0Y-fBCe7I2EgAgLc73oNai1Mv3a6TGCVELAPkg1pd5DtIpjJ7ZnmaedOCRiAQWOMuXtzU5hJT0HpGt8pPXzOHpvhOVoYny7DiWm09pv0QISM8cl34-XxxIDjJI8As8SbCaansfyfgVFG_mEVz4tFuNk5rBrTM6kpejRsmB1Jo0MZX4bMzu21bvZH3s4TfGcIo_Jp9AN86AmiSiB6ee_BwCk4fgrBpBbXTPYO6-e1eCetVP_UtYJf-zG7ZQzzoFWw6A7fLO1ellYOeLOBaM-DrlHsE3vltjDjZ3X-IzcEADxDgPKTHTG9XBbsJXUW5zrkeRBlQ2CXPSp2N50yt1A1tcrWXLXQg1HD4N3HhAv-FjyGoI2PRjtMOjY3TflOlNO7xK5cjBLnLhdMrCs8FuzGSMYK_AU7z09SaBMoXisqpfkTO7LbCcntCJse7MiB6LDjlq4eQ3Qjb2Y9hnLzeE6XMngZDjCEQ-6Mj2I9BKamATeMSaf7-Y-8_W40) |
| 04 | **l3 internals** | Qt/QML layers: app shell, network, domain state, screens. | [PNG](https://www.plantuml.com/plantuml/png/RPRTQkD65CVlzHI7NAM4n0rRoXBAieebEsBbBqasB0J2M3gHPosDr9cHiwOKUjK7A7t2VPAU6IcXzYGtiSU_cVFrdx_qMMacTLTNyFClrzM7QouqIi4gvMckAuIO7L721_Znrx-mql0hZ60rY-4L0oRp7TUOwqwYvwaz5osJh8QygTj6eD2fFj8naWWcoWh_XxGitxCIpm5lM8SB6-_AnZFXXrGkW2bOPV27vm6yiNxRGhh3gh8_fmzX71C1zFUM2LpC61VmmaNHl5XivaVpSsRE3hnacZS2PDDHbPPC5kiiF4S3LBmKe8vAOqsDqJkiqUBPGpXpS7xxKS3bxogJnCPSb1rN-YVBHxSVdKJyJk-0fgEHUg2RFL8atSW-WB_E7fxIx7nFA6KZ2MPAKuo2iwefBHycoTCw8lpFawxDKRyqSW-LQQq5vs7czcsA0YKbKa0IfXda5QVbHt7Pj2XOosyEjpVV53Ninz__mFZgQhGQoZBq-Lbf6eBgjYgNV8kI3baDIIJOLaV8TvtOOz7l9_Pf73jJH2hiBOcLddMLvhLT7C4qrH9PFLGp2uF8Fzz_lss-GtD0ILMJxaI1qYQITzJV6bQhZkVxY4RQ9x7OX4diVtMoC559o9gcqqgh12mUHTTM3Ik6yEjblF03f-j1KniXzhEzN2sstqZyT8mvWsxUK7lcPw4xYh6fZQbPK-0WzUnfxC-SxCplfzHi4JXoAKeyXS9W-exCgQPZJP-dT2FQqmt1rD5jb36rFwKoF_tY13_GlnwZscD-mFlk-Nded4bbSxvZaYnHaivEpvuaJWRnCbbMrDnJq2oVemdclELlYRoYyqKMJHpbsdl-WApIknCw_PewCqvpYIWKZNSM3mCT9s4uJzsHnat9XKNYnTGrf82ftRPXie3Heo2pBb4hAzy3nzx60ZzzU5ydK3D1wTJsfbp1Ohw99cGBAZVxZsOfPnNVIkjtzYpIqbDwVjYIjrXnWK2NZ2o8xig1guxSF_2dVN71bAGJEPVTZy28WPR4C--To4ZXNJRfAbJ6HIKlIvfpxo3hE7JpCM8nrKCfUT5tBjeukhK2ivYn1vD_RuKRzy190cqlBPBYCyknZpv9NDIe25ghhjw9wRjBzRAMM9B5osFlHOiWZ5rohIWmbSsAUb31EWqJ1qjHQtfaz37JCCkY-RHN7UcEvJGLIcsURg8KHcJ9eyEj9U_zyPTm7dZUg_gkhpyDrd97wR72XAuxuqJAwzSDOPu2TqORz1vWFlzc5b_F6WmHRcu-zGPBg3gAd9uxiCnIRtWssT_jy-AENXUCnL4woCXhJuZUurt6-qpEJEybtdy0) | [SVG](https://www.plantuml.com/plantuml/svg/RPRTQkD65CVlzHI7NAM4n0rRoXBAieebEsBbBqasB0J2M3gHPosDr9cHiwOKUjK7A7t2VPAU6IcXzYGtiSU_cVFrdx_qMMacTLTNyFClrzM7QouqIi4gvMckAuIO7L721_Znrx-mql0hZ60rY-4L0oRp7TUOwqwYvwaz5osJh8QygTj6eD2fFj8naWWcoWh_XxGitxCIpm5lM8SB6-_AnZFXXrGkW2bOPV27vm6yiNxRGhh3gh8_fmzX71C1zFUM2LpC61VmmaNHl5XivaVpSsRE3hnacZS2PDDHbPPC5kiiF4S3LBmKe8vAOqsDqJkiqUBPGpXpS7xxKS3bxogJnCPSb1rN-YVBHxSVdKJyJk-0fgEHUg2RFL8atSW-WB_E7fxIx7nFA6KZ2MPAKuo2iwefBHycoTCw8lpFawxDKRyqSW-LQQq5vs7czcsA0YKbKa0IfXda5QVbHt7Pj2XOosyEjpVV53Ninz__mFZgQhGQoZBq-Lbf6eBgjYgNV8kI3baDIIJOLaV8TvtOOz7l9_Pf73jJH2hiBOcLddMLvhLT7C4qrH9PFLGp2uF8Fzz_lss-GtD0ILMJxaI1qYQITzJV6bQhZkVxY4RQ9x7OX4diVtMoC559o9gcqqgh12mUHTTM3Ik6yEjblF03f-j1KniXzhEzN2sstqZyT8mvWsxUK7lcPw4xYh6fZQbPK-0WzUnfxC-SxCplfzHi4JXoAKeyXS9W-exCgQPZJP-dT2FQqmt1rD5jb36rFwKoF_tY13_GlnwZscD-mFlk-Nded4bbSxvZaYnHaivEpvuaJWRnCbbMrDnJq2oVemdclELlYRoYyqKMJHpbsdl-WApIknCw_PewCqvpYIWKZNSM3mCT9s4uJzsHnat9XKNYnTGrf82ftRPXie3Heo2pBb4hAzy3nzx60ZzzU5ydK3D1wTJsfbp1Ohw99cGBAZVxZsOfPnNVIkjtzYpIqbDwVjYIjrXnWK2NZ2o8xig1guxSF_2dVN71bAGJEPVTZy28WPR4C--To4ZXNJRfAbJ6HIKlIvfpxo3hE7JpCM8nrKCfUT5tBjeukhK2ivYn1vD_RuKRzy190cqlBPBYCyknZpv9NDIe25ghhjw9wRjBzRAMM9B5osFlHOiWZ5rohIWmbSsAUb31EWqJ1qjHQtfaz37JCCkY-RHN7UcEvJGLIcsURg8KHcJ9eyEj9U_zyPTm7dZUg_gkhpyDrd97wR72XAuxuqJAwzSDOPu2TqORz1vWFlzc5b_F6WmHRcu-zGPBg3gAd9uxiCnIRtWssT_jy-AENXUCnL4woCXhJuZUurt6-qpEJEybtdy0) |
| 05 | **seq configure deploy** | Operator workflow: add camera → AI task → deploy → start. | [PNG](https://www.plantuml.com/plantuml/png/XLJHRjem57ttLrpnr6e98QpT0rAxeWuX9bHOq-wfKkN59bWpTchVJA88QH-nBzoNz3efJ0IalUJ1vvnxp_6zper7xh1QQUXTnbw-nBar2rLKJiP2bjgk6IhK4ZBvKacJI_Zx-m_CIkauMWTxieUSh-WGFi1m0iZzJ-16G5F2CyRpGE_iX1tW7XwzTAoaxYfN9JS8dMy8ZvC6cnmYmxli-oHxCWalLV8hhJd3-KaIcNAo9an7uqF2D0KfYWOSVHcF60iU8BwcVZ08iGpSrZay-Dn9QHW115ChmiUZ1qYQdFvpoGjvbRA0485Q4K7BWkThqCeZ2oVnNlgkQkYxVlrU3pOAvSf74Cf5W1QvthPi3OM0-uTilhlYiuLrg_C3P_DPziyQCnObE5Ki4UmYN2N03olMiAHOHdgK0YYSa0OLrpuYEESbLawP8WAr8YVFNhtMeoRCMF0OCBLGfFoQpUwOfDc6BYVZSo5QwPMuIliVs-5JcDgSex9ciFUU1DvX_DlTZkqswqHohkA0-2UpQGezAp6WKX5qkztjIPTq0vm271czkFnqR7H3vGXeJoOBR-T_dX8I9lKhE_GXb2yvvil6mdMza65JwhZqZXf9VHhlHQuoXaRHTjY73T5H3ceH4SYfjhOCHR3oK5gjuOnUO9QDG1FHdTDaZtQxRPcaH3lUsQRNkvS85gKVf1VT_ZRi1MCtzAKV2di3) | [SVG](https://www.plantuml.com/plantuml/svg/XLJHRjem57ttLrpnr6e98QpT0rAxeWuX9bHOq-wfKkN59bWpTchVJA88QH-nBzoNz3efJ0IalUJ1vvnxp_6zper7xh1QQUXTnbw-nBar2rLKJiP2bjgk6IhK4ZBvKacJI_Zx-m_CIkauMWTxieUSh-WGFi1m0iZzJ-16G5F2CyRpGE_iX1tW7XwzTAoaxYfN9JS8dMy8ZvC6cnmYmxli-oHxCWalLV8hhJd3-KaIcNAo9an7uqF2D0KfYWOSVHcF60iU8BwcVZ08iGpSrZay-Dn9QHW115ChmiUZ1qYQdFvpoGjvbRA0485Q4K7BWkThqCeZ2oVnNlgkQkYxVlrU3pOAvSf74Cf5W1QvthPi3OM0-uTilhlYiuLrg_C3P_DPziyQCnObE5Ki4UmYN2N03olMiAHOHdgK0YYSa0OLrpuYEESbLawP8WAr8YVFNhtMeoRCMF0OCBLGfFoQpUwOfDc6BYVZSo5QwPMuIliVs-5JcDgSex9ciFUU1DvX_DlTZkqswqHohkA0-2UpQGezAp6WKX5qkztjIPTq0vm271czkFnqR7H3vGXeJoOBR-T_dX8I9lKhE_GXb2yvvil6mdMza65JwhZqZXf9VHhlHQuoXaRHTjY73T5H3ceH4SYfjhOCHR3oK5gjuOnUO9QDG1FHdTDaZtQxRPcaH3lUsQRNkvS85gKVf1VT_ZRi1MCtzAKV2di3) |
| 06 | **seq live events** | Eventview: SSE event tail + click into evidence (image + clip). | [PNG](https://www.plantuml.com/plantuml/png/VLJ1Rjf04BtlLumuLA1YZ5FA0HMIYBeHacb8N7gA52ts52PPR6TtJOYYI3tr2_A5-PBksawAQUd9gtblthnvk_A9qbpgV2sWstSKtZc2DkZW1XEjc2Oj44Ayop6945v-FaDWO2XWq9m4l8T8K7HBoH8eqQc1ABPandYaKmcDymmbDwS6S0LpXP9bPY15b76ZqRZGC9-KsAIEd7uDlq_2gyJb6RaRh-2SplP8OUZ3GgOyZhZIM0unjJffB2W3PJpWLT8CFu0BKqecv-Rx9MmLBUDWCWlhJO4762-nWFtFPpvZrZmu8sCK1c3MIi2tEMm8xw6fkReTzZxsMyp0XcIS6jQP_mtSCinZoxYcs9BUPNo9GuzPalEgUDJjmegiJOguWEeA3gYvIalawrtHkfh6hIwvJac5PceijnYpo_pHBA-IIsdkLK5pGnm2x-NNSt3KAWL7Daz3p7ArAXU-n2YLCRFbjt4nrtm0ZnGFVAyDMhKXNTnWf0SHbsrOBD9j6pgTpjDUYcFxTYeFldTmMVyVKUrOWUQFN8Y3oPjsbzOcyUDxYlLgsEzrwt85wDvak8J5WqQrPt8c-8DzvTd_1XHu5LZnccna4XKj4qjbPSrvQwymZ65-6VoztMCkHHjmcv7XNNFzLCr-bRRJFzKqHZiZ2fdfh5UjKxOMnSpkGWbSw6akDAqn9ixO2IQn-GMmtm00) | [SVG](https://www.plantuml.com/plantuml/svg/VLJ1Rjf04BtlLumuLA1YZ5FA0HMIYBeHacb8N7gA52ts52PPR6TtJOYYI3tr2_A5-PBksawAQUd9gtblthnvk_A9qbpgV2sWstSKtZc2DkZW1XEjc2Oj44Ayop6945v-FaDWO2XWq9m4l8T8K7HBoH8eqQc1ABPandYaKmcDymmbDwS6S0LpXP9bPY15b76ZqRZGC9-KsAIEd7uDlq_2gyJb6RaRh-2SplP8OUZ3GgOyZhZIM0unjJffB2W3PJpWLT8CFu0BKqecv-Rx9MmLBUDWCWlhJO4762-nWFtFPpvZrZmu8sCK1c3MIi2tEMm8xw6fkReTzZxsMyp0XcIS6jQP_mtSCinZoxYcs9BUPNo9GuzPalEgUDJjmegiJOguWEeA3gYvIalawrtHkfh6hIwvJac5PceijnYpo_pHBA-IIsdkLK5pGnm2x-NNSt3KAWL7Daz3p7ArAXU-n2YLCRFbjt4nrtm0ZnGFVAyDMhKXNTnWf0SHbsrOBD9j6pgTpjDUYcFxTYeFldTmMVyVKUrOWUQFN8Y3oPjsbzOcyUDxYlLgsEzrwt85wDvak8J5WqQrPt8c-8DzvTd_1XHu5LZnccna4XKj4qjbPSrvQwymZ65-6VoztMCkHHjmcv7XNNFzLCr-bRRJFzKqHZiZ2fdfh5UjKxOMnSpkGWbSw6akDAqn9ixO2IQn-GMmtm00) |
| 07 | **seq liveview** | Liveview: HLS player + bbox overlay synced to PTS. | [PNG](https://www.plantuml.com/plantuml/png/PLFHQjj047o_hrZyP1CRsKdRb42Q62fIWzCwbbCe6CnPsiP7JYVbRgKub48_8b_OB-dUIMhYzaK8pUpEpExgqf6qLEKQngSZXmyZhMgi5JuAKgGH4doeqAG8ltyzmxp5u57H3hRROWz5ZLRB9o5aIeM5tfSIhUIt7aW7jmwjABc_IbKf3K7lXk1src2pGsJwEVawIzOcagMAwadWJ1V_aAunKn8iic8WBEFvzDiXPJu1pEummF77g_WGJP8OjhQGMIeTOUEIlmdXhSBe0zk2C-0GvYLiFvKvJ8vFXa3ItSERj-y6WcbCPd_ClefN4367Wxk8UL4tmWkoqhYoi7I-qqvuDXS5pro6nbKMuTCyWUyykLP4D33pl7ccbPNMi5mbYmxvgyizDvNLS0HknnfXSnbGSO-cqUhY78_7y8EP7A0pTCR5mxMH5UqseM88k2-LHRUHz5Eq-JghFc6drW_qmIitGIC_gTvx9tYNeo7NDWZG4at6mwrLXiNQz6_ekjAaSh_AWVX_abZx1f4ZYpA_y2FVgEoSfmvztjJWC1hXdfgAKLCWX2wA4lWA8NmMuFVxKX2EzWZO8S0hZ-qPWtlYM_T3XCKg4MnSY4j-ylyX_W00) | [SVG](https://www.plantuml.com/plantuml/svg/PLFHQjj047o_hrZyP1CRsKdRb42Q62fIWzCwbbCe6CnPsiP7JYVbRgKub48_8b_OB-dUIMhYzaK8pUpEpExgqf6qLEKQngSZXmyZhMgi5JuAKgGH4doeqAG8ltyzmxp5u57H3hRROWz5ZLRB9o5aIeM5tfSIhUIt7aW7jmwjABc_IbKf3K7lXk1src2pGsJwEVawIzOcagMAwadWJ1V_aAunKn8iic8WBEFvzDiXPJu1pEummF77g_WGJP8OjhQGMIeTOUEIlmdXhSBe0zk2C-0GvYLiFvKvJ8vFXa3ItSERj-y6WcbCPd_ClefN4367Wxk8UL4tmWkoqhYoi7I-qqvuDXS5pro6nbKMuTCyWUyykLP4D33pl7ccbPNMi5mbYmxvgyizDvNLS0HknnfXSnbGSO-cqUhY78_7y8EP7A0pTCR5mxMH5UqseM88k2-LHRUHz5Eq-JghFc6drW_qmIitGIC_gTvx9tYNeo7NDWZG4at6mwrLXiNQz6_ekjAaSh_AWVX_abZx1f4ZYpA_y2FVgEoSfmvztjJWC1hXdfgAKLCWX2wA4lWA8NmMuFVxKX2EzWZO8S0hZ-qPWtlYM_T3XCKg4MnSY4j-ylyX_W00) |
| 08 | **seq async scan** | ONVIF camera scan via async job queue (poll /jobs/{id}). | [PNG](https://www.plantuml.com/plantuml/png/TPBTJjj048Nlvob6kMeb_1UXYbe24Qgs2XS9oC0DL6XP3sZ3PcnsrrHL58c7u0dx98pNuLVYRktlUCuvuzttGRdGBItcEucdkqJvVwmJhnL3CC4ILdJN4Ml2_m-FEEafQhKafv0f_6tSBVPY_B9ehh1L8P3Zhm1AXyRXQDwAK4uZL1vFFJbenSveqoeEE3eEU3eTsFGzcSogisbrmPbgJNPVHCtaw8FeK2mbN4SH7yuFglTyFZkR_ehnc8O8rO_93A0FWicUc6A9LP---3vEYsyxQP7csNWR10WMF-57y-e4iw6op-9YUY2yDnG1SSm0yJ5v-KxsSMdgtKKno9_jnlaOLmCh5yNM1QFyWT3vCWwf-nVMyAqlLvxiTPc9r6UBOhs6ZS5UB26JNCS0jcbQR1jhiUDWBChwbuQL1SIt9Nx_b0wRKO8-9CfnjGaXCzdmpHQshhbnv7sPfzjh8Au1ectosbjmPuE7Jtq-TV6TraJrq7OOKwvgutLpJuxgyZnDqpzzutqnbhi9Jm00) | [SVG](https://www.plantuml.com/plantuml/svg/TPBTJjj048Nlvob6kMeb_1UXYbe24Qgs2XS9oC0DL6XP3sZ3PcnsrrHL58c7u0dx98pNuLVYRktlUCuvuzttGRdGBItcEucdkqJvVwmJhnL3CC4ILdJN4Ml2_m-FEEafQhKafv0f_6tSBVPY_B9ehh1L8P3Zhm1AXyRXQDwAK4uZL1vFFJbenSveqoeEE3eEU3eTsFGzcSogisbrmPbgJNPVHCtaw8FeK2mbN4SH7yuFglTyFZkR_ehnc8O8rO_93A0FWicUc6A9LP---3vEYsyxQP7csNWR10WMF-57y-e4iw6op-9YUY2yDnG1SSm0yJ5v-KxsSMdgtKKno9_jnlaOLmCh5yNM1QFyWT3vCWwf-nVMyAqlLvxiTPc9r6UBOhs6ZS5UB26JNCS0jcbQR1jhiUDWBChwbuQL1SIt9Nx_b0wRKO8-9CfnjGaXCzdmpHQshhbnv7sPfzjh8Au1ectosbjmPuE7Jtq-TV6TraJrq7OOKwvgutLpJuxgyZnDqpzzutqnbhi9Jm00) |
| 09 | **seq large upload** | Large model upload: initiate → presigned PUT → complete. | [PNG](https://www.plantuml.com/plantuml/png/TPB1Jjj048Rl-nIZd1B9RWA80wX2mA5I2i519Zr5YYRs4AQnrylkc3OW91wYJzWdwJXEqYPIJxlQ-NU-V_xToo3efIuB69p5WPxZ0ls2ujeL5UP6M0g2b9vhiXd1xuzVSDlKeQnoAg1LGPSj2wCGz26hIbUGRXsAaBSzOp2JoaCdSUHHTnt009D0tZWbSyOEhK3dNc0oR6l3_ShLNVfjc4vj7nttNuxMcglnWIYPVwTC82Y0fhQRdgYN4TjXekkNjBU-b5n_DQOXGtoX53Y7CMVBTfPFh-oWUpeOmEYwPxIe4eKqcYHzWFvQ5Cwtasu2cjgtHox8OaaH17wbsNmb52B8dYXRXhfyDqqJxQLixUKy1LvO64yUJ7CIxoYR2kMpsXVjfQt9OpNrrl9cd4VxqWZefsCzcQ6ycutn5gOG-Bmdlj1avcpHhpGeJrX6y8FbIJjM2zM5tixK3fpScF-diNtilsdyOxCamHm53o9u8S-Fgrr01p5KoyFPZs3Km8ov99lhFpL_0000) | [SVG](https://www.plantuml.com/plantuml/svg/TPB1Jjj048Rl-nIZd1B9RWA80wX2mA5I2i519Zr5YYRs4AQnrylkc3OW91wYJzWdwJXEqYPIJxlQ-NU-V_xToo3efIuB69p5WPxZ0ls2ujeL5UP6M0g2b9vhiXd1xuzVSDlKeQnoAg1LGPSj2wCGz26hIbUGRXsAaBSzOp2JoaCdSUHHTnt009D0tZWbSyOEhK3dNc0oR6l3_ShLNVfjc4vj7nttNuxMcglnWIYPVwTC82Y0fhQRdgYN4TjXekkNjBU-b5n_DQOXGtoX53Y7CMVBTfPFh-oWUpeOmEYwPxIe4eKqcYHzWFvQ5Cwtasu2cjgtHox8OaaH17wbsNmb52B8dYXRXhfyDqqJxQLixUKy1LvO64yUJ7CIxoYR2kMpsXVjfQt9OpNrrl9cd4VxqWZefsCzcQ6ycutn5gOG-Bmdlj1avcpHhpGeJrX6y8FbIJjM2zM5tixK3fpScF-diNtilsdyOxCamHm53o9u8S-Fgrr01p5KoyFPZs3Km8ov99lhFpL_0000) |
| 10 | **deployment topology** | Workstations / ANSVIS server / edges + ports + customer network. | [PNG](https://www.plantuml.com/plantuml/png/RLJ1Rjf04BtxAqQSY2hKWgEgYgeg4Bkj8m8qfaahSLdiYREDlMljhXEig59F_O0gNvWlwQmtW25mC3kUDxCpR-RvL1kcJ5Naq7_NIx7CPLsWC3qZIvdBhFOCDpb2iF70VEtnz1qN9LEiW4GMfHJaZKrDQ8M9OIBBqVE4J14wPvKsia05cXk4jv3aiaexyEXv00unBL4n8nKyI7L7DHakXRO8eDycFNIkkKZbWuOKzHtLk13V37oFEi0q1FrzSC6IQNmGEjY7ZhceLWUXFa5_RmiTJkAhA0QDwfxwkPNQ7AZo4icf4KebL_L2pCSn65G55xR5XUYAZ8kLvO6bQNtKt3Fv4ar-xETvkOoLL5ANfQoaBF06SinOKXDGeGiUpcRxeHUOSaQJo5dTG2x3yV1d0qgPOKkcBUdBNpGfeDaef0jYdqgwu2AQql_dsAMUZixteiwakE4PbH7Usvk2aNP4ZNQ92jCCWPOhGxCPfFCuhbSVtbEEoLKKHCCcGTXlOTPpVeqQj53db5iAc2ekdDCDYwe8-j3hVNAimWdO2HmV-mjn6SPpIXZ78Js_ZcCl61m2Ue5_yBMphSTIlhKSk_IkiOYAzT7IrpZXepMfnxOrsB7y9cgDsvu7hRDl9KKQMaf3Yg9k9sYiP41LEMgu8GMPMuJaHN0dbgmUJ2kpb9L8ONusitt0ZP8584jkstg3vx__u6Nbx6Axs4ZiXXBk5MOyeDWd6FVjnXkPo1n8pu1s1RgbL8R8DfKGcCDy51oveEkMqa5OnT2wsf9i62cSsyz8NiFpdoTeRNCZAexQGohASk5vftIaJvZt7m00) | [SVG](https://www.plantuml.com/plantuml/svg/RLJ1Rjf04BtxAqQSY2hKWgEgYgeg4Bkj8m8qfaahSLdiYREDlMljhXEig59F_O0gNvWlwQmtW25mC3kUDxCpR-RvL1kcJ5Naq7_NIx7CPLsWC3qZIvdBhFOCDpb2iF70VEtnz1qN9LEiW4GMfHJaZKrDQ8M9OIBBqVE4J14wPvKsia05cXk4jv3aiaexyEXv00unBL4n8nKyI7L7DHakXRO8eDycFNIkkKZbWuOKzHtLk13V37oFEi0q1FrzSC6IQNmGEjY7ZhceLWUXFa5_RmiTJkAhA0QDwfxwkPNQ7AZo4icf4KebL_L2pCSn65G55xR5XUYAZ8kLvO6bQNtKt3Fv4ar-xETvkOoLL5ANfQoaBF06SinOKXDGeGiUpcRxeHUOSaQJo5dTG2x3yV1d0qgPOKkcBUdBNpGfeDaef0jYdqgwu2AQql_dsAMUZixteiwakE4PbH7Usvk2aNP4ZNQ92jCCWPOhGxCPfFCuhbSVtbEEoLKKHCCcGTXlOTPpVeqQj53db5iAc2ekdDCDYwe8-j3hVNAimWdO2HmV-mjn6SPpIXZ78Js_ZcCl61m2Ue5_yBMphSTIlhKSk_IkiOYAzT7IrpZXepMfnxOrsB7y9cgDsvu7hRDl9KKQMaf3Yg9k9sYiP41LEMgu8GMPMuJaHN0dbgmUJ2kpb9L8ONusitt0ZP8584jkstg3vx__u6Nbx6Axs4ZiXXBk5MOyeDWd6FVjnXkPo1n8pu1s1RgbL8R8DfKGcCDy51oveEkMqa5OnT2wsf9i62cSsyz8NiFpdoTeRNCZAexQGohASk5vftIaJvZt7m00) |
| 11 | **l3 engineer ownership** | L3 engineers 1–6 and the components each owns. | [PNG](https://www.plantuml.com/plantuml/png/RLRRRYet57tdAxRIbnmb194BglfG7K88HI8NGYwjn8kPsGmkWpsoFIJagDBvX_OBpvTqUSOamISl8VQiVLlxvg_M2UFALKxjTZC_QRBAf68sJVsisDY5B1fEkfnf93Piw8H-VF-F-W54Ro3Qxz1Mr7vfDEnIga8OiQ94hmgjMBc9sq2DuSG9bUNy0LA8P2aoZW6DS4zx_NPbzc588nOfjMYou3odhaaMqW6_Hy8IGB-CPBPmV-6RVgPl3NetJdjZHnqweDwLM3AjjDA62nq4kxlG0ICguJYbk_xadf9SudggvaQlI1UiH26FrksZlwrMGS7vhe9kwHO4OXt3ejDBLZZ1O43tTj4NR6K6nCOwNY4uj-3L5dgn2xqMQva99xKYeql79i3wkx3RYZYfCdgIgdKbajP8gl9bghmXN1-IqaYxVEN3gHAbqysoIE5jq7Qvgwqtf9LmHhw4puF6FnzpSrpbvbOMd2Fzr4ybGgL9mOaKkRGXESSXEUTvoN5g7gKjHKuNtG5n9JrL--E14SL29lPHyZD9HUDnAPFbaqmpTb-2pYXlrpfbokNC2BEXWwbAj5Ae3i_M6gwa5N51DChO907fefaQkKQojcMfEBS17-NiGQKQgJASQb1enPfnMgFstorSxD9KTyu8-jS-eXPT8LPHOqy2EGF3iFvJuLwLkPCh3wS_HXCgSj-Au6ZT3aISH4HK0ath32ECpCWqmAAWpsVwXJIyXa9FUSNmq1VX0OqdWm7h5JkpyRHk3KKqz7AqVkqFxIEmb5NoXergzubdT_SzMYEAmQHohGxYU8llxn9sMX7MqsekCxhKPXLISHh8kfLg6NErcyRnrOZcNX1cFclUOFWqOg_dFHFWuERwSNX9gRI99sb3SuoL87B-gKWYgkghAkvX2AjtDqDa-XKeYz-5NkdCb_LcQpcYymfCvp1Sv2MwrvSmsYxBGExyG_nHOPrLF5q8kvXfOVnqx2Duvwki5ZWBXFMqGPT5ZDqBWuQYT2kEhi5V2oUI1HP3i7aMKLHP85-KlkTz5YMM2A9_vJJ4TXPtsAHFjfpPnCWP0ZgeMk6ydC-tG-mi8kDIvXXm2CUomA2drbHjAunyS6zo5vyLlwlZmg3t0mKpBu2Z8klKY-LFcEaPRMqJnUmysJJkrX8Ti8VsLYby-VRfu2uqnjp2Jz9sUtD8ilrh606TYBMXmax0tcfHMQ0ECAmSMmVVfkh9fvimgIjYCAk3XhZE3F94VdEPMcv_WdkKfdSVdmgnfRyJiUbxuTRekSmHHy5cZl5cBOVyTY8E7onGgKuiKcRc8WcReLEnXxAGk0CiwgUwpLxHUPYypW1IEzExkRw_w_Rk6mtiwAEZtzyli1PspoVH-JGwd-sU6zXZJ3FjdE_kEPOJudXYNbBxn_T_Zt_pZmAH8oX2Oqa_d_rs9Liz7zn2E2hLBDV9qWziIFGxHmsasgz7_bdvyOvoieN4KqBMwoBFqRc-bb9k6ccNjI6qK8eK-vVBd8Kh3N_K_nN_ukNL-1y0) | [SVG](https://www.plantuml.com/plantuml/svg/RLRRRYet57tdAxRIbnmb194BglfG7K88HI8NGYwjn8kPsGmkWpsoFIJagDBvX_OBpvTqUSOamISl8VQiVLlxvg_M2UFALKxjTZC_QRBAf68sJVsisDY5B1fEkfnf93Piw8H-VF-F-W54Ro3Qxz1Mr7vfDEnIga8OiQ94hmgjMBc9sq2DuSG9bUNy0LA8P2aoZW6DS4zx_NPbzc588nOfjMYou3odhaaMqW6_Hy8IGB-CPBPmV-6RVgPl3NetJdjZHnqweDwLM3AjjDA62nq4kxlG0ICguJYbk_xadf9SudggvaQlI1UiH26FrksZlwrMGS7vhe9kwHO4OXt3ejDBLZZ1O43tTj4NR6K6nCOwNY4uj-3L5dgn2xqMQva99xKYeql79i3wkx3RYZYfCdgIgdKbajP8gl9bghmXN1-IqaYxVEN3gHAbqysoIE5jq7Qvgwqtf9LmHhw4puF6FnzpSrpbvbOMd2Fzr4ybGgL9mOaKkRGXESSXEUTvoN5g7gKjHKuNtG5n9JrL--E14SL29lPHyZD9HUDnAPFbaqmpTb-2pYXlrpfbokNC2BEXWwbAj5Ae3i_M6gwa5N51DChO907fefaQkKQojcMfEBS17-NiGQKQgJASQb1enPfnMgFstorSxD9KTyu8-jS-eXPT8LPHOqy2EGF3iFvJuLwLkPCh3wS_HXCgSj-Au6ZT3aISH4HK0ath32ECpCWqmAAWpsVwXJIyXa9FUSNmq1VX0OqdWm7h5JkpyRHk3KKqz7AqVkqFxIEmb5NoXergzubdT_SzMYEAmQHohGxYU8llxn9sMX7MqsekCxhKPXLISHh8kfLg6NErcyRnrOZcNX1cFclUOFWqOg_dFHFWuERwSNX9gRI99sb3SuoL87B-gKWYgkghAkvX2AjtDqDa-XKeYz-5NkdCb_LcQpcYymfCvp1Sv2MwrvSmsYxBGExyG_nHOPrLF5q8kvXfOVnqx2Duvwki5ZWBXFMqGPT5ZDqBWuQYT2kEhi5V2oUI1HP3i7aMKLHP85-KlkTz5YMM2A9_vJJ4TXPtsAHFjfpPnCWP0ZgeMk6ydC-tG-mi8kDIvXXm2CUomA2drbHjAunyS6zo5vyLlwlZmg3t0mKpBu2Z8klKY-LFcEaPRMqJnUmysJJkrX8Ti8VsLYby-VRfu2uqnjp2Jz9sUtD8ilrh606TYBMXmax0tcfHMQ0ECAmSMmVVfkh9fvimgIjYCAk3XhZE3F94VdEPMcv_WdkKfdSVdmgnfRyJiUbxuTRekSmHHy5cZl5cBOVyTY8E7onGgKuiKcRc8WcReLEnXxAGk0CiwgUwpLxHUPYypW1IEzExkRw_w_Rk6mtiwAEZtzyli1PspoVH-JGwd-sU6zXZJ3FjdE_kEPOJudXYNbBxn_T_Zt_pZmAH8oX2Oqa_d_rs9Liz7zn2E2hLBDV9qWziIFGxHmsasgz7_bdvyOvoieN4KqBMwoBFqRc-bb9k6ccNjI6qK8eK-vVBd8Kh3N_K_nN_ukNL-1y0) |
| 12 | **dependency chain** | Critical path: contract → mock → UI + adapter → integration → ship. | [PNG](https://www.plantuml.com/plantuml/png/VLFRRjD047ttLnpb2GH9bIBnWa1rKvS6GXF50Ga92MtiYRDqlMljJeB24_y0NyYNC5utQG9INxoxupDdRcSk0YlFczfWUDulgI5RaYrs_MAjj4rOio5S7So8Plpv-HlVrufHAQOGhqaIxhHjb5Sr2bStpfBbd7VYxgbWPIj3IIAXz4eLZ9uJIjNemNOu-1QS_MAVhBpxGVPf3ofWDBrTpDFH8ZbGeJVpEjIeNN67G7vBFY8_J4VlZr7fRVvfdEDCkC-sGwXIDKnUwDEnv5MI3y_HaEzx2cxZ2-hYfLVfx2JOX2ebvNPi4JB9tgQZpoUO5mXhCWRFCC_o1GgZnIwXic77-d7yDR_99fD_dGfFP4CBF9TM_aBsye2_pjAJFEQa39Qgk9C1G5kcoYlMpaRyF4jFkFErRh0Tnd_vpNYM9Fi-ejz_qxNgnDAL7R_nVLzZ2uplGm7xozuQqxm_eoscya0Kd-qdlhfSh6F2qZ6x6cxrC69WQiqE1sbmAwpBwU86GPUYhgLfH-xiG93NKIFoA0ZDPccq3A1y9LCkDUijjVAqbVWuOHmaRS_QgF_5Zfs058AIjcHS4x0cJn09rYZL3iD7dBkc0PSRRLXcWbRqnf3CIGIpLrcRxUn8PoBSScEKrxp3Q_7QhngZyQuIL7WaP6mdyCvfgss5HauUjEdBzd60trYeIXOpSEkqxfP42PiN-OWZEocKPGrHUhtYuqWNSfNjJ_u2) | [SVG](https://www.plantuml.com/plantuml/svg/VLFRRjD047ttLnpb2GH9bIBnWa1rKvS6GXF50Ga92MtiYRDqlMljJeB24_y0NyYNC5utQG9INxoxupDdRcSk0YlFczfWUDulgI5RaYrs_MAjj4rOio5S7So8Plpv-HlVrufHAQOGhqaIxhHjb5Sr2bStpfBbd7VYxgbWPIj3IIAXz4eLZ9uJIjNemNOu-1QS_MAVhBpxGVPf3ofWDBrTpDFH8ZbGeJVpEjIeNN67G7vBFY8_J4VlZr7fRVvfdEDCkC-sGwXIDKnUwDEnv5MI3y_HaEzx2cxZ2-hYfLVfx2JOX2ebvNPi4JB9tgQZpoUO5mXhCWRFCC_o1GgZnIwXic77-d7yDR_99fD_dGfFP4CBF9TM_aBsye2_pjAJFEQa39Qgk9C1G5kcoYlMpaRyF4jFkFErRh0Tnd_vpNYM9Fi-ejz_qxNgnDAL7R_nVLzZ2uplGm7xozuQqxm_eoscya0Kd-qdlhfSh6F2qZ6x6cxrC69WQiqE1sbmAwpBwU86GPUYhgLfH-xiG93NKIFoA0ZDPccq3A1y9LCkDUijjVAqbVWuOHmaRS_QgF_5Zfs058AIjcHS4x0cJn09rYZL3iD7dBkc0PSRRLXcWbRqnf3CIGIpLrcRxUn8PoBSScEKrxp3Q_7QhngZyQuIL7WaP6mdyCvfgss5HauUjEdBzd60trYeIXOpSEkqxfP42PiN-OWZEocKPGrHUhtYuqWNSfNjJ_u2) |
+78
View File
@@ -0,0 +1,78 @@
# ANSCORE — L2 & L3 Architecture Diagrams (PlantUML)
12 PlantUML diagrams covering Layer 2 (ANSVIS Communication Layer (CL)) and Layer 3 (Qt/QML UI).
## Files
| # | File | Type | Purpose |
|---|---|---|---|
| 01 | `01-system-context.puml` | Component | L1 / L2 / L3 overview with the 3 transports |
| 02 | `02-l2-internals.puml` | Component | L2 adapter layer + legacy + infrastructure |
| 03 | `03-l2-ownership.puml` | Component | Human-owned vs AI-coder vs reused legacy modules |
| 04 | `04-l3-internals.puml` | Component | Qt/QML shell / network / state / screens |
| 05 | `05-seq-configure-deploy.puml` | Sequence | Operator adds camera → AI task → deploys |
| 06 | `06-seq-live-events.puml` | Sequence | SSE event tail + evidence image/clip |
| 07 | `07-seq-liveview.puml` | Sequence | Liveview with bbox overlay sync |
| 08 | `08-seq-async-scan.puml` | Sequence | Async ONVIF scan via job queue |
| 09 | `09-seq-large-upload.puml` | Sequence | Large model upload via presigned URL |
| 10 | `10-deployment-topology.puml` | Deployment | Workstations / server / edges + ports |
| 11 | `11-l3-engineer-ownership.puml` | Component | 6-engineer L3 task allocation |
| 12 | `12-dependency-chain.puml` | Component | What gates what, critical path |
---
## How to view these in StarUML
StarUML doesn't render `.puml` natively, but the official **PlantUML extension** does:
1. In StarUML: `Tools → Extension Manager → search "PlantUML" → Install`
2. Restart StarUML.
3. `File → Open` → pick any `.puml` file from this folder, **or** use `Tools → PlantUML → Preview Diagram`.
4. To get an editable StarUML diagram (`.mdj`), use the extension's "Generate Diagram from PlantUML" feature, then save as `.mdj`.
Caveat: the extension renders accurately but the auto-generated StarUML elements may need light layout cleanup. If you want fully native StarUML diagrams, use these `.puml` files as the specification and recreate in StarUML — they're small enough that this takes ~10 min each.
---
## Alternative viewers (no install needed)
| Tool | How |
|---|---|
| **PlantUML web server** | <https://www.plantuml.com/plantuml> → paste any `.puml` content → get PNG/SVG/PDF |
| **VS Code** | Install the "PlantUML" extension by jebbs → `Alt+D` to preview |
| **JetBrains (CLion / IntelliJ / Rider)** | Install the "PlantUML Integration" plugin → preview on the side panel |
| **PlantUML CLI** (batch export) | `brew install plantuml` then `plantuml -tpng docs/diagrams/*.puml -o ../out/` |
| **PlantUML CLI** (SVG for slides) | `plantuml -tsvg docs/diagrams/*.puml -o ../out/` |
The web server is the fastest path if you just want to grab images for a slide deck or PDF.
---
## Render all to PNG in one shot
```bash
# from the repo root
brew install plantuml # macOS; or: apt install plantuml
mkdir -p out/diagrams
plantuml -tpng -o "$(pwd)/out/diagrams" docs/diagrams/*.puml
```
Output: one PNG per diagram in `out/diagrams/`.
For high-DPI presentations use `-tsvg` instead of `-tpng`.
---
## Editing tips
- Diagrams use only stable PlantUML features — should render in any PlantUML version ≥ 1.2022.0.
- Colors used: `#LightYellow` (human/lead), `#LightBlue` (AI/E2), `#LightGreen` (E3), `#LightPink` (E4), `#LightCoral` (E5), `#Lavender` (E6), `#LightGray` (reused legacy). Change consistently if you want a different palette.
- Each file declares its own `@startuml NAME` so multiple files can render to predictable filenames.
- Notes (`note bottom of ...`) are intentional — leave them in; they explain non-obvious decisions.
---
## Related
- `docs/L2-L3-architecture.md` — same diagrams in Mermaid + accompanying prose.
- `contracts/openapi.v1.json` — the REST contract these diagrams reference.
Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

+191
View File
@@ -0,0 +1,191 @@
@page {
size: A4;
margin: 22mm 18mm 22mm 18mm;
@top-right {
content: "ANSCORE — System Architecture (L2 + L3)";
font-family: -apple-system, "Helvetica Neue", Arial, sans-serif;
font-size: 8.5pt;
color: #888;
}
@bottom-right {
content: "Page " counter(page) " / " counter(pages);
font-family: -apple-system, "Helvetica Neue", Arial, sans-serif;
font-size: 8.5pt;
color: #888;
}
@bottom-left {
content: "v1.0 • 2026-05-26 • ANSCENTER";
font-family: -apple-system, "Helvetica Neue", Arial, sans-serif;
font-size: 8.5pt;
color: #888;
}
}
@page :first {
@top-right { content: ""; }
@bottom-right { content: ""; }
@bottom-left { content: ""; }
}
html, body {
font-family: -apple-system, "Helvetica Neue", "Segoe UI", Arial, "Apple Symbols", "STIXGeneral", "Lucida Grande", sans-serif;
font-size: 10.5pt;
line-height: 1.45;
color: #222;
}
h1 {
font-size: 22pt;
color: #102a55;
border-bottom: 2px solid #102a55;
padding-bottom: 4pt;
margin-top: 0;
page-break-before: always;
page-break-after: avoid;
}
h1:first-of-type {
page-break-before: avoid;
}
h2 {
font-size: 15pt;
color: #1a4080;
margin-top: 1.4em;
page-break-after: avoid;
}
h3 {
font-size: 12.5pt;
color: #1a4080;
margin-top: 1.1em;
page-break-after: avoid;
}
h4 {
font-size: 11pt;
color: #333;
margin-top: 0.9em;
page-break-after: avoid;
}
p, ul, ol {
margin: 0.5em 0;
}
ul, ol {
padding-left: 1.4em;
}
li {
margin: 0.15em 0;
}
a {
color: #1a4080;
text-decoration: none;
}
code {
font-family: "SF Mono", "Menlo", Consolas, monospace;
font-size: 9pt;
background: #f4f4f6;
padding: 1px 4px;
border-radius: 3px;
color: #2c2c2c;
}
pre {
font-family: "SF Mono", "Menlo", Consolas, monospace;
font-size: 9pt;
background: #f6f8fa;
border: 1px solid #e1e4e8;
border-radius: 4px;
padding: 10pt 12pt;
line-height: 1.4;
overflow-x: auto;
page-break-inside: avoid;
}
pre code {
background: transparent;
padding: 0;
font-size: 9pt;
}
table {
border-collapse: collapse;
width: 100%;
margin: 0.8em 0;
font-size: 9.5pt;
page-break-inside: avoid;
}
th, td {
border: 1px solid #d0d4dc;
padding: 5pt 7pt;
text-align: left;
vertical-align: top;
}
th {
background: #eef2f8;
color: #102a55;
font-weight: 600;
}
tr:nth-child(even) td {
background: #fafbfd;
}
img {
max-width: 100%;
display: block;
margin: 0.6em auto;
page-break-inside: avoid;
}
hr {
border: 0;
border-top: 1px solid #d0d4dc;
margin: 1.5em 0;
}
blockquote {
border-left: 3px solid #c3cad6;
margin: 0.6em 0;
padding: 0.2em 0.9em;
color: #555;
background: #f7f8fa;
font-style: italic;
}
/* Title block (pandoc emits a header div) */
header#title-block-header {
text-align: center;
margin-top: 18%;
padding-bottom: 35%;
border-bottom: none;
}
header#title-block-header h1.title {
font-size: 30pt;
color: #102a55;
border-bottom: none;
page-break-before: avoid;
margin-bottom: 12pt;
}
header#title-block-header p.subtitle {
font-size: 14pt;
color: #555;
font-style: italic;
margin: 6pt 0;
}
header#title-block-header p.author,
header#title-block-header p.date {
font-size: 11pt;
color: #777;
margin: 4pt 0;
}
+1 -1
View File
@@ -540,7 +540,7 @@ namespace ANSCENTER {
") returned null — the DLL may be corrupt.");
s_ort_api = api;
Ort::Global<void>::api_ = api;
Ort::InitApi(api);
std::cout << "[EPLoader] ORT loaded successfully." << std::endl;
std::cout << "[EPLoader] ORT DLL version : " << base->GetVersionString() << std::endl;
+124 -12
View File
@@ -141,7 +141,7 @@ namespace ANSCENTER {
{
try {
// Make sure the Ort API pointer is initialised in THIS DLL.
if (Ort::Global<void>::api_ == nullptr) {
if (Ort::detail::Global::Api() == nullptr) {
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
}
@@ -221,8 +221,45 @@ namespace ANSCENTER {
initialize_handler();
}
// ────────────────────────────────────────────────────────────────────
// A model that is already in memory — the decrypted contents of an
// .ansmodel package. There is no path, so onnx_path stays null and
// every file-shaped step in initialize_handler is skipped: no CWD
// switch, no external-data lookup, no file for ORT to open.
// ────────────────────────────────────────────────────────────────────
BasicOrtHandler::BasicOrtHandler(const void* modelData, size_t modelSize,
const std::string& logName,
unsigned int _num_threads)
: num_threads(_num_threads),
m_engineType(static_cast<EngineType>(-1))
{
log_id_storage = logName;
log_id = log_id_storage.c_str();
model_data = modelData;
model_size = modelSize;
initialize_handler();
}
BasicOrtHandler::BasicOrtHandler(const void* modelData, size_t modelSize,
const std::string& logName,
EngineType engineType,
unsigned int _num_threads)
: num_threads(_num_threads),
m_engineType(engineType)
{
log_id_storage = logName;
log_id = log_id_storage.c_str();
model_data = modelData;
model_size = modelSize;
initialize_handler();
}
BasicOrtHandler::~BasicOrtHandler()
{
// The model bytes, when there are any, belong to whoever handed
// them over; that owner wipes them. Only the view goes here.
model_data = nullptr;
model_size = 0;
if (ort_session) {
delete ort_session;
ort_session = nullptr;
@@ -574,10 +611,10 @@ namespace ANSCENTER {
const auto& epInfo = EPLoader::Current();
ANS_DBG("OrtHandler", "initialize_handler: EPLoader type=%d dir=%s",
static_cast<int>(epInfo.type), epInfo.libraryDir.c_str());
if (Ort::Global<void>::api_ == nullptr)
if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
std::cout << "[ORT] api_ = " << (void*)Ort::Global<void>::api_ << std::endl;
std::cout << "[ORT] api_ = " << (void*)Ort::detail::Global::Api() << std::endl;
EngineType engine = (static_cast<int>(m_engineType) == -1)
? epInfo.type : m_engineType;
@@ -727,20 +764,26 @@ namespace ANSCENTER {
// AddExternalInitializersFromFilesInMemory() so ORT never
// memory-maps the file itself.
// ----------------------------------------------------------------
std::filesystem::path modelFsPath(onnx_path); // wchar_t*
std::filesystem::path modelDir = modelFsPath.parent_path();
// A model in memory has no directory, no sibling .onnx_data and
// nothing for ORT to open, so all of this is skipped for one.
std::filesystem::path modelFsPath;
std::filesystem::path modelDir;
std::filesystem::path prevCwd = std::filesystem::current_path();
if (!modelDir.empty() && std::filesystem::is_directory(modelDir)) {
std::filesystem::current_path(modelDir);
std::cout << "[ORT] CWD -> " << modelDir.string() << std::endl;
if (!fromMemory()) {
modelFsPath = std::filesystem::path(onnx_path); // wchar_t*
modelDir = modelFsPath.parent_path();
if (!modelDir.empty() && std::filesystem::is_directory(modelDir)) {
std::filesystem::current_path(modelDir);
std::cout << "[ORT] CWD -> " << modelDir.string() << std::endl;
}
}
// --- Pre-load external data files if they exist -----------------
// Keep the buffer alive across session creation (must outlive the
// Ort::Session constructor call).
std::vector<char> extDataBuffer;
{
if (!fromMemory()) {
// Build the expected external-data filename:
// <model_stem>.onnx_data (e.g. anssam3.onnx_data)
std::filesystem::path extDataPath =
@@ -806,7 +849,7 @@ namespace ANSCENTER {
// This avoids ORT opening/mapping ANY files during CreateSession.
std::vector<char> modelBuffer;
bool useModelBuffer = false;
if (!extDataBuffer.empty()) {
if (!extDataBuffer.empty() && !fromMemory()) {
// External data was pre-loaded, so also load the .onnx itself
try {
auto modelFileSize = std::filesystem::file_size(modelFsPath);
@@ -829,7 +872,10 @@ namespace ANSCENTER {
// --- Attempt session creation (with CUDA → CPU fallback) --------
auto createSession = [&](Ort::SessionOptions& opts, const char* label) {
std::cout << "[ORT] Creating session (" << label << ")..." << std::endl;
if (useModelBuffer) {
if (fromMemory()) {
ort_session = new Ort::Session(*ort_env,
model_data, model_size, opts);
} else if (useModelBuffer) {
ort_session = new Ort::Session(*ort_env,
modelBuffer.data(), modelBuffer.size(), opts);
} else {
@@ -838,7 +884,11 @@ namespace ANSCENTER {
std::cout << "[ORT] Session created OK (" << label << ")." << std::endl;
};
ANS_DBG("OrtHandler", "Creating session for model: %ls", onnx_path);
if (fromMemory())
ANS_DBG("OrtHandler", "Creating session for model in memory: %s (%zu bytes)",
log_id ? log_id : "<memory>", model_size);
else
ANS_DBG("OrtHandler", "Creating session for model: %ls", onnx_path);
try {
createSession(session_options, "primary EP");
ANS_DBG("OrtHandler", "Session created OK with primary EP");
@@ -1044,6 +1094,60 @@ namespace ANSCENTER {
}
}
// ====================================================================
// AdaFace
// ====================================================================
Ort::Value AdaFace::transform(const cv::Mat& mat)
{
if (mat.empty())
throw std::runtime_error("AdaFace::transform — input is empty.");
// AdaFace consumes BGR — OpenCV Mats are already BGR, no channel swap.
cv::Mat canvas;
cv::resize(mat, canvas, cv::Size(input_node_dims.at(3), input_node_dims.at(2)));
if (canvas.type() != CV_32FC3)
canvas.convertTo(canvas, CV_32FC3);
utils::transform::normalize_inplace(canvas, mean_val, scale_val);
std::vector<int64_t> shape = input_node_dims;
if (shape[0] == -1) shape[0] = 1;
return utils::transform::create_tensor(
canvas, shape, *memory_info_handler,
input_values_handler, utils::transform::CHW);
}
Ort::Value AdaFace::transformBatch(const std::vector<cv::Mat>& images)
{
if (images.empty())
throw std::runtime_error("AdaFace::transformBatch — batch is empty.");
const int width = input_node_dims.at(3);
const int height = input_node_dims.at(2);
std::vector<cv::Mat> batch;
batch.reserve(images.size());
for (const auto& mat : images) {
if (mat.empty())
throw std::runtime_error("AdaFace::transformBatch — empty image in batch.");
cv::Mat canvas;
cv::resize(mat, canvas, cv::Size(width, height));
if (canvas.type() != CV_32FC3)
canvas.convertTo(canvas, CV_32FC3);
utils::transform::normalize_inplace(canvas, mean_val, scale_val);
batch.push_back(std::move(canvas));
}
std::vector<int64_t> shape = input_node_dims;
shape[0] = static_cast<int64_t>(images.size());
return utils::transform::create_tensor_batch(
batch, shape, *memory_info_handler,
input_values_handler, utils::transform::CHW);
}
// ====================================================================
// GlintCosFace
// ====================================================================
@@ -1900,4 +2004,12 @@ namespace ANSCENTER {
return std::fabs(width() * height());
}
// Explicit instantiations of every BoundingBoxType member must live
// AFTER the template definitions above so the compiler actually emits
// symbols (the directives in ONNXEngine.h sit before the definitions
// and ClangCL, unlike MSVC, refuses to emit symbols from there).
template struct ANSCENTER::types::BoundingBoxType<int, float>;
template struct ANSCENTER::types::BoundingBoxType<float, float>;
template struct ANSCENTER::types::BoundingBoxType<double, double>;
} // namespace ANSCENTER
+74 -3
View File
@@ -73,9 +73,10 @@ namespace ANSCENTER {
}
};
template class BoundingBoxType<int, float>;
template class BoundingBoxType<float, float>;
template class BoundingBoxType<double, double>;
// Explicit instantiations now live at the end of ONNXEngine.cpp
// (after the template member definitions) so the compiler actually
// emits symbols. ClangCL refuses to emit them from a directive that
// precedes the definitions, whereas MSVC silently tolerated it.
typedef BoundingBoxType<int, float> Boxi;
typedef BoundingBoxType<float, float> Boxf;
@@ -467,6 +468,25 @@ namespace ANSCENTER {
const LITEORT_CHAR* onnx_path = nullptr; // ← points into onnx_path_w
const char* log_id = nullptr;
// ────────────────────────────────────────────────────────────────
// A model handed over as bytes rather than as a path.
//
// An .ansmodel package is decrypted into memory and never written
// out, so there is no file for ORT to open and no directory to
// resolve external data against.
//
// This is a VIEW, not a copy: the owner of the plaintext keeps it
// alive for at least as long as this handler, because
// Reinitialize() rebuilds the session after a driver fault and has
// nothing else to rebuild it from. A copy here would double the
// resident size of every loaded model and protect nothing extra —
// the weights are in RAM either way once a session exists.
// ────────────────────────────────────────────────────────────────
const void* model_data = nullptr; // null for a file model
size_t model_size = 0;
std::string log_id_storage; // owns what log_id points at
bool fromMemory() const { return model_data != nullptr && model_size > 0; }
protected:
const unsigned int num_threads;
@@ -498,6 +518,23 @@ namespace ANSCENTER {
const OrtHandlerOptions& options,
unsigned int _num_threads = 1);
// A model already in memory — the decrypted contents of an
// .ansmodel package. `logName` is what logs call it, since there
// is no file name to fall back on.
//
// The caller KEEPS THE BYTES ALIVE until this handler is destroyed,
// and wipes them then; see model_data above for why a copy is not
// taken here.
explicit BasicOrtHandler(const void* modelData, size_t modelSize,
const std::string& logName,
unsigned int _num_threads = 1);
// The same, with an explicit engine override.
explicit BasicOrtHandler(const void* modelData, size_t modelSize,
const std::string& logName,
EngineType engineType,
unsigned int _num_threads = 1);
virtual ~BasicOrtHandler();
BasicOrtHandler(const BasicOrtHandler&) = delete;
@@ -673,6 +710,40 @@ namespace ANSCENTER {
Ort::Value transformBatch(const std::vector<cv::Mat>& images) override;
};
// ====================================================================
// AdaFace — face recognition
//
// Same session/inference path as GlintArcFace (detect/detectBatch are
// inherited and dispatch through the virtual transform), but AdaFace
// was trained on BGR input — the official repo flips RGB→BGR before
// normalize — so the BGR2RGB conversion is omitted: OpenCV Mats are
// already BGR. Normalization is identical: (x - 127.5) / 127.5.
// ====================================================================
class AdaFace : public GlintArcFace
{
public:
explicit AdaFace(const std::string& _onnx_path,
unsigned int _num_threads = 1)
: GlintArcFace(_onnx_path, _num_threads)
{
}
explicit AdaFace(const std::string& _onnx_path,
EngineType engineType,
unsigned int _num_threads = 1)
: GlintArcFace(_onnx_path, engineType, _num_threads) {
}
~AdaFace() override = default;
private:
static constexpr float mean_val = 127.5f;
static constexpr float scale_val = 1.f / 127.5f;
Ort::Value transform(const cv::Mat& mat) override;
Ort::Value transformBatch(const std::vector<cv::Mat>& images) override;
};
// ====================================================================
// MOVINET — action recognition
// ====================================================================
+1 -1
View File
@@ -331,7 +331,7 @@ namespace ANSCENTER
{
// Initialize ORT API
const auto& epInfo = EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr)
if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
m_env = new Ort::Env(ORT_LOGGING_LEVEL_ERROR, "ONNXSAM3");
@@ -4,6 +4,7 @@
#include <sstream>
#include "Utility.h"
#include "TRTCompat.h"
#include "GpuMemoryGuard.h"
// ============================================================================
// Crash-safe wrappers for TensorRT operations that can crash the process.
@@ -264,6 +265,8 @@ bool Engine<T>::buildLoadNetwork(std::string onnxModelPath, const std::array<flo
// It is full path
std::string engineName = serializeEngineOptions(m_options, onnxModelPath);
std::string engineDir = m_options.engineFileDir;
m_engineFilePath = engineName;
m_rebuiltFromOnnx = false;
if (FileExist(engineName)) {
if (m_verbose) { std::cout << "Engine file found: " << engineName << std::endl; }
@@ -334,6 +337,7 @@ bool Engine<T>::buildLoadNetwork(std::string onnxModelPath, const std::array<flo
if (FileExist(alternativeEngineName)) {
std::cout << "Found alternative precision engine: " << alternativeEngineName << std::endl;
m_engineFilePath = alternativeEngineName;
return loadNetwork(alternativeEngineName, subVals, divVals, normalize);
}
else {
@@ -357,11 +361,13 @@ bool Engine<T>::buildLoadNetwork(std::string onnxModelPath, const std::array<flo
std::cout << "FP16 engine not found, but FP32 engine exists: " << fp32EngineName << std::endl;
std::cout << "Loading existing FP32 engine..." << std::endl;
m_options.precision = ANSCENTER::Precision::FP32;
m_engineFilePath = fp32EngineName;
return loadNetwork(fp32EngineName, subVals, divVals, normalize);
}
}
// ONNX model exists, generate engine
m_rebuiltFromOnnx = true;
std::cout << "========================================" << std::endl;
std::cout << "Engine not found, generating from ONNX model" << std::endl;
std::cout << "========================================" << std::endl;
@@ -805,13 +811,23 @@ bool Engine<T>::loadNetwork(std::string trtModelPath, const std::array<float, 3>
// (cudaMalloc failure during inference) when too many tasks are loaded.
{
size_t freeVRAM = 0, totalVRAM = 0;
cudaError_t memErr = cudaMemGetInfo(&freeVRAM, &totalVRAM);
constexpr size_t kMinFreeBytes = 256ULL * 1024 * 1024; // 256 MiB minimum
if (memErr != cudaSuccess) {
// cudaMemGetInfo failed — CUDA context may not be initialized on this thread.
// Don't reject: let TRT try to deserialize (it may succeed).
} else if (freeVRAM < kMinFreeBytes) {
m_lastLoadFailedVRAM = true; // signal to buildLoadNetwork: engine file is NOT corrupt
bool haveMem = ANSCENTER::GpuMemoryGuard::Query(freeVRAM, totalVRAM);
// (1) Admission control: refuse to load this engine on the GPU if it
// does not fit while keeping the GpuMemoryGuard safety margin free.
// Prevents VRAM over-subscription across concurrent engines (FR/OD/LPR/
// liveness) that would otherwise OOM and permanently poison the CUDA
// context. The engine file is valid — m_lastLoadFailedVRAM tells the
// caller to preserve it (no rebuild) and just skip GPU load for now.
if (haveMem && !ANSCENTER::GpuMemoryGuard::CanLoad(buffer.size())) {
m_lastLoadFailedVRAM = true;
std::cerr << "[GpuMemGuard] REFUSING engine load on GPU "
<< m_options.deviceIndex << ": need ~"
<< (buffer.size() / (1024 * 1024)) << " MiB + "
<< (ANSCENTER::GpuMemoryGuard::MarginFor(totalVRAM) / (1024 * 1024))
<< " MiB margin, only " << (freeVRAM / (1024 * 1024))
<< " MiB free of " << (totalVRAM / (1024 * 1024))
<< " MiB total. Reduce concurrent engines/cameras or batch size."
<< std::endl;
return false;
}
if (m_verbose) {
@@ -1425,7 +1441,15 @@ trt_cache_create_context:
// ============================================================================
if (m_verbose) std::cout << "\nInfo: Starting warm-up inference..." << std::endl;
warmUp(m_verbose ? 10 : 1);
if (!warmUp(m_verbose ? 10 : 1)) {
// A failed warmup means this engine cannot run inference (wrong-device
// buffers, poisoned context, ...). Report failure so pool creation
// rejects the slot instead of silently adding a broken engine that
// corrupts every subsequent inference dispatched to it.
std::cout << "Error: Warm-up inference FAILED on GPU[" << m_options.deviceIndex
<< "] -- engine rejected (loadNetwork returns false)" << std::endl;
return false;
}
if (m_verbose) std::cout << "Info: Warm-up complete" << std::endl;
} // end of trt_cache_create_context scope
@@ -1798,6 +1822,25 @@ bool Engine<T>::build(std::string onnxModelPath, const std::array<float, 3>& sub
// Set hardware compatibility
config->setHardwareCompatibilityLevel(nvinfer1::HardwareCompatibilityLevel::kNONE);
// -- Blackwell (SM 12.x) aux-stream workaround ----------------------------
// On sm_120 (RTX 50-series), TRT 10.15's Myelin runtime faults when a second
// concurrent execution context runs with auto-selected aux streams:
// "Internal Error: MyelinCheckException ... l2cm_layer_internal.cpp:174"
// followed by CUDA error 700 (illegal memory access) poisoning the context.
// Aux streams only provide intra-inference parallelism (irrelevant for the
// small per-slot models we run); throughput comes from the engine pool's
// one-context-per-slot design, which is unaffected. Force 0 aux streams on
// Blackwell until NVIDIA fixes the Myelin multi-stream path.
// NOTE: baked in at build time -- stale cached .engine files must be deleted
// for this to take effect.
#if NV_TENSORRT_MAJOR >= 10
if (prop.major >= 12) {
config->setMaxAuxStreams(0);
std::cout << "Aux streams disabled on SM " << prop.major << "." << prop.minor
<< " (Blackwell Myelin multi-stream workaround)" << std::endl;
}
#endif
// -- TensorRT 10+ tactic DRAM pool ----------------------------------------
// Separate scratch pool for kernel-selection during build. Without this,
// tactic evaluation competes with the workspace allocation and tactics
@@ -606,6 +606,25 @@ bool Engine<T>::runInferenceFromPool(
bool kickedGrowth = false;
auto _poolAcquireStart = std::chrono::steady_clock::now();
// -- DEVICE AFFINITY: find which GPU the caller's inputs live on ------
// Dispatching to a slot on the same GPU avoids the pinned-host migration
// copy (~1 ms per 1080p frame) that runInference performs for foreign
// inputs. Queried outside the slot mutex — it's a cheap driver lookup.
int inputDevice = -1;
for (const auto& batch : inputs) {
for (const auto& img : batch) {
if (!img.data) continue;
cudaPointerAttributes attr{};
if (cudaPointerGetAttributes(&attr, img.data) == cudaSuccess &&
attr.type == cudaMemoryTypeDevice) {
inputDevice = attr.device;
}
cudaGetLastError(); // clear any error the query left behind
break; // first image is representative
}
if (inputDevice >= 0) break;
}
{
std::unique_lock<std::mutex> lock(m_slotMutex);
@@ -616,13 +635,31 @@ bool Engine<T>::runInferenceFromPool(
const size_t n = m_slots.size();
if (n > 0) {
const size_t start = m_nextSlotHint.load() % n;
for (size_t i = 0; i < n; ++i) {
auto& s = m_slots[(start + i) % n];
if (!s.busy && s.engine) { // alive and idle
s.busy = true;
slot = &s;
m_nextSlotHint = (start + i + 1) % n;
break;
// Pass A: idle slot on the inputs' own GPU (zero-copy dispatch)
if (inputDevice >= 0) {
for (size_t i = 0; i < n; ++i) {
auto& s = m_slots[(start + i) % n];
if (!s.busy && s.engine && s.deviceIndex == inputDevice) {
s.busy = true;
slot = &s;
m_nextSlotHint = (start + i + 1) % n;
break;
}
}
}
// Pass B: any idle slot (round-robin) — runInference migrates
// the inputs across GPUs when needed.
if (!slot) {
for (size_t i = 0; i < n; ++i) {
auto& s = m_slots[(start + i) % n];
if (!s.busy && s.engine) { // alive and idle
s.busy = true;
slot = &s;
m_nextSlotHint = (start + i + 1) % n;
break;
}
}
}
}
@@ -130,13 +130,31 @@ static inline cudaError_t cudaStreamSynchronize_Safe(cudaStream_t stream) {
}
template <typename T>
void Engine<T>::warmUp(int iterations) {
bool Engine<T>::warmUp(int iterations) {
if (m_verbose) {
std::cout << "\n========================================" << std::endl;
std::cout << "Engine Warmup" << std::endl;
std::cout << "========================================" << std::endl;
}
// Ensure this thread's CUDA device matches the engine BEFORE the dummy-input
// uploads below. warmUp() does GPU work (GpuMat uploads) ahead of
// runInference()'s own device guard, so it must not rely on the caller
// having left the right device current (multi-GPU pool creation touches
// several devices on one thread).
{
int currentDev = -1;
cudaGetDevice(&currentDev);
if (currentDev != m_options.deviceIndex) {
std::cout << "Warning [Warmup]: thread was on GPU[" << currentDev
<< "], engine expects GPU[" << m_options.deviceIndex
<< "] -- switching device" << std::endl;
cudaSetDevice(m_options.deviceIndex);
}
}
bool allBatchesOk = true;
// Determine batch sizes to warm up
std::vector<int> batchSizes;
@@ -212,7 +230,9 @@ void Engine<T>::warmUp(int iterations) {
auto firstTime = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
if (!firstSuccess) {
if (m_verbose) std::cout << " ✗ ERROR: First iteration failed for batch=" << batchSize << std::endl;
std::cout << " ✗ ERROR: Warmup first iteration failed for batch=" << batchSize
<< " on GPU[" << m_options.deviceIndex << "]" << std::endl;
allBatchesOk = false;
continue;
}
@@ -231,7 +251,9 @@ void Engine<T>::warmUp(int iterations) {
for (int i = 1; i < iters; ++i) {
bool success = runInference(dummyInputs, dummyOutputs);
if (!success) {
if (m_verbose) std::cout << " ✗ ERROR: Iteration " << i << " failed" << std::endl;
std::cout << " ✗ ERROR: Warmup iteration " << i << " failed for batch="
<< batchSize << " on GPU[" << m_options.deviceIndex << "]" << std::endl;
allBatchesOk = false;
break;
}
}
@@ -262,6 +284,7 @@ void Engine<T>::warmUp(int iterations) {
std::cout << "Kernels pre-compiled for all batch sizes." << std::endl;
std::cout << "========================================\n" << std::endl;
}
return allBatchesOk;
}
template <typename T>
bool Engine<T>::runInference(const std::vector<std::vector<cv::cuda::GpuMat>>& inputs,std::vector<std::vector<std::vector<T>>>& featureVectors) {
@@ -778,6 +801,118 @@ bool Engine<T>::runInference(const std::vector<std::vector<cv::cuda::GpuMat>>& i
}
}
// Pass 1.5: INPUT DEVICE MIGRATION (multi-GPU pool dispatch).
//
// The caller prepares input GpuMats on ITS device (e.g. camera preprocessing
// on GPU 0), but round-robin dispatch may select a slot on another GPU.
// Kernels cannot touch cross-device memory without peer access: preprocessing
// a device-A GpuMat with a device-B stream raises an async illegal memory
// access that permanently poisons every CUDA context in the process
// (sticky error 700). Detect foreign inputs and migrate them onto this
// engine's device via a UVA copy (cudaMemcpyDefault routes over PCIe).
std::vector<std::vector<cv::cuda::GpuMat>> migratedInputs;
const std::vector<std::vector<cv::cuda::GpuMat>>* activeInputs = &inputs;
{
bool anyForeign = false;
for (size_t i = 0; i < numInputs && !anyForeign; ++i) {
for (const auto& img : inputs[i]) {
if (!img.data) continue;
cudaPointerAttributes attr{};
if (cudaPointerGetAttributes(&attr, img.data) == cudaSuccess &&
attr.type == cudaMemoryTypeDevice &&
attr.device != m_options.deviceIndex) {
anyForeign = true;
}
cudaGetLastError(); // clear any error the attribute query left behind
break; // first image per tensor is representative
}
}
if (anyForeign) {
migratedInputs.resize(numInputs);
for (size_t i = 0; i < numInputs; ++i) {
migratedInputs[i].reserve(inputs[i].size());
for (const auto& src : inputs[i]) {
cudaPointerAttributes attr{};
const bool foreign = src.data &&
cudaPointerGetAttributes(&attr, src.data) == cudaSuccess &&
attr.type == cudaMemoryTypeDevice &&
attr.device != m_options.deviceIndex;
cudaGetLastError();
if (!foreign) { migratedInputs[i].push_back(src); continue; }
// Two-hop copy through pinned host staging. A direct
// device-to-device cudaMemcpy2D across GPUs is not supported
// (2D copies have no peer path, and GeForce has no P2P), so:
// hop 1: strided D2H from the foreign GPU into pinned host
// hop 2: strided H2D onto this engine's GPU
// Synchronous copies keep the single staging buffer safe to
// reuse across images; ~1 ms per 1080p frame over PCIe 4.
struct PinnedStaging { // thread-local, grow-only
void* ptr = nullptr;
size_t cap = 0;
~PinnedStaging() { if (ptr) cudaFreeHost(ptr); }
};
thread_local PinnedStaging tl_staging;
const size_t rowBytes = static_cast<size_t>(src.cols) * src.elemSize();
const size_t needed = rowBytes * static_cast<size_t>(src.rows);
if (needed > tl_staging.cap) {
if (tl_staging.ptr) cudaFreeHost(tl_staging.ptr);
tl_staging.ptr = nullptr; tl_staging.cap = 0;
if (cudaMallocHost(&tl_staging.ptr, needed) != cudaSuccess) {
std::cout << "Error [MultiGPU]: pinned staging alloc failed ("
<< needed << " bytes)" << std::endl;
return false;
}
tl_staging.cap = needed;
}
// Hop 1: D2H. UVA lets us read the foreign pointer from this
// device's context; if the runtime rejects it, retry with the
// source device current.
cudaError_t mErr = cudaMemcpy2D(
tl_staging.ptr, rowBytes, src.data, src.step,
rowBytes, src.rows, cudaMemcpyDeviceToHost);
if (mErr != cudaSuccess) {
cudaGetLastError();
cudaSetDevice(attr.device);
mErr = cudaMemcpy2D(
tl_staging.ptr, rowBytes, src.data, src.step,
rowBytes, src.rows, cudaMemcpyDeviceToHost);
cudaSetDevice(m_options.deviceIndex);
}
if (mErr != cudaSuccess) {
std::cout << "Error [MultiGPU]: migration hop1 (D2H from GPU["
<< attr.device << "]) failed: " << cudaGetErrorString(mErr) << std::endl;
return false;
}
// Hop 2: H2D onto this engine's GPU.
cv::cuda::GpuMat dst(src.rows, src.cols, src.type());
mErr = cudaMemcpy2D(
dst.data, dst.step, tl_staging.ptr, rowBytes,
rowBytes, src.rows, cudaMemcpyHostToDevice);
if (mErr != cudaSuccess) {
std::cout << "Error [MultiGPU]: migration hop2 (H2D to GPU["
<< m_options.deviceIndex << "]) failed: "
<< cudaGetErrorString(mErr) << std::endl;
return false;
}
migratedInputs[i].push_back(dst);
}
}
activeInputs = &migratedInputs;
static std::atomic<bool> s_migrationLogged{false};
if (!s_migrationLogged.exchange(true)) {
std::cout << "Info [MultiGPU]: inputs resided on another GPU -- migrated to GPU["
<< m_options.deviceIndex << "] via UVA copy (logged once; recurs per"
<< " cross-device dispatch)" << std::endl;
}
}
}
// Pass 2: Preprocess + D2D copies — all on m_inferenceStream (no null stream).
//
// All OpenCV CUDA ops (convertTo, subtract, divide, split) in blobFromGpuMats
@@ -794,7 +929,7 @@ bool Engine<T>::runInference(const std::vector<std::vector<cv::cuda::GpuMat>>& i
preprocessedBuffers.reserve(numInputs);
for (size_t i = 0; i < numInputs; ++i) {
const auto& batchInput = inputs[i];
const auto& batchInput = (*activeInputs)[i];
// Preprocess on m_inferenceStream (not the null stream).
preprocessedBuffers.push_back(
@@ -811,8 +946,23 @@ bool Engine<T>::runInference(const std::vector<std::vector<cv::cuda::GpuMat>>& i
m_inferenceStream);
if (copyErr != cudaSuccess) {
// Rich diagnostics — this fires on multi-GPU slot warmup failures
// (Blackwell debugging). Identify WHICH precondition broke:
// wrong current device, empty blob (failed allocation), null dest
// buffer, or a pre-existing sticky async error.
int curDev = -1; cudaGetDevice(&curDev);
std::cout << "Error: Failed to copy input " << i
<< " to inference buffer: " << cudaGetErrorString(copyErr) << std::endl;
<< " to inference buffer: " << cudaGetErrorString(copyErr) << "\n"
<< " [diag] engine deviceIndex = " << m_options.deviceIndex
<< ", thread current device = " << curDev << "\n"
<< " [diag] src blob ptr = " << static_cast<const void*>(blobMat.data)
<< " (" << blobMat.rows << "x" << blobMat.cols
<< ", elemSize " << blobMat.elemSize() << ")\n"
<< " [diag] dst buffer ptr = " << m_buffers[i]
<< ", copySize = " << copySize << " bytes\n"
<< " [diag] inference stream = " << static_cast<const void*>(m_inferenceStream)
<< ", pending lastError = " << cudaGetErrorString(cudaGetLastError())
<< std::endl;
return false;
}
}
@@ -207,6 +207,21 @@ std::string Engine<T>::serializeEngineOptions(const ANSCENTER::Options& options,
+ std::to_string(CUDNN_MAJOR) + "."
+ std::to_string(CUDNN_MINOR);
// -- Blackwell aux-stream workaround marker --------------------------------
// On SM 12.x the builder forces maxAuxStreams=0 (see EngineBuildLoadNetwork.inl:
// TRT 10.15 Myelin faults with concurrent contexts + aux streams). Tag those
// engines so caches built *before* the workaround (with aux streams baked in)
// miss and rebuild automatically instead of loading and crashing.
#if NV_TENSORRT_MAJOR >= 10
{
cudaDeviceProp prop{};
if (cudaGetDeviceProperties(&prop, options.deviceIndex) == cudaSuccess
&& prop.major >= 12) {
engineName += ".aux0";
}
}
#endif
return engineName;
}
@@ -231,6 +246,32 @@ cv::cuda::GpuMat Engine<T>::blobFromGpuMats(const std::vector<cv::cuda::GpuMat>
thread_local cv::cuda::GpuMat tl_blob;
thread_local cv::cuda::GpuMat tl_floatImg;
thread_local int tl_blobMaxElems = 0;
thread_local int tl_blobDevice = -1;
// DEVICE AFFINITY: the cached GpuMats live on ONE device. When the same
// thread serves engines on different GPUs (multi-GPU pool creation warms
// probe on GPU A, then slots on GPU B), reusing a device-A blob with a
// device-B stream causes invalid-argument launches and an async illegal
// memory access that permanently poisons both CUDA contexts (sticky 700).
// Detect the device switch and rebuild the cache on the current device.
{
int curDev = -1;
cudaGetDevice(&curDev);
if (curDev != tl_blobDevice) {
if (tl_blobDevice >= 0) {
// Free on the owning device — the async allocator frees via the
// CURRENT device's null-stream pool, so switch, release, switch back.
cudaSetDevice(tl_blobDevice);
tl_blob.release();
tl_floatImg.release();
cudaSetDevice(curDev);
ANS_DBG("TRT_Preproc", "blobFromGpuMats: device switch GPU[%d]->GPU[%d], preproc cache rebuilt",
tl_blobDevice, curDev);
}
tl_blobMaxElems = 0;
tl_blobDevice = curDev;
}
}
if (totalElems > tl_blobMaxElems) {
tl_blob = cv::cuda::GpuMat(1, totalElems, CV_32FC1);
+123
View File
@@ -0,0 +1,123 @@
#pragma once
// ============================================================================
// GpuMemoryGuard — process-wide GPU VRAM safeguard.
//
// Purpose: prevent the out-of-memory cascade that happens when many TensorRT /
// CUDA engines (FR-SCRFD, OD-YOLO, LPR, liveness, ORT-CUDA, OpenCV-CUDA) share
// one GPU and collectively exhaust VRAM. The first cudaMalloc/stream OOM
// poisons the CUDA context ("permanently corrupted after N failures"), after
// which every GPU op fails and the service appears dead until restart.
//
// Three coordinated layers (all process-wide, thread-safe, lock-free):
// (1) CanLoad(need) — admission control: refuse to load an engine on GPU
// if it won't fit while keeping a safety margin free.
// (2) UnderPressure() — pressure guard: skip a frame's GPU work when free
// VRAM is below the margin (or breaker is open).
// (3) NoteOom()/Breaker— circuit breaker: after a caught OOM, open a short
// cooldown so callers back off instead of hammering
// the fragile context (which is what corrupts it).
//
// The margin scales with the device: total/8, clamped to [192 MiB, 768 MiB].
// A fixed margin cannot work across the fleet — 768 MiB is 3% of a 24 GiB
// compute card but 19% of a 4 GiB display card (RTX A400), where the desktop
// plus the ORT CUDA arena never leave that much free and admission control can
// therefore never pass. The 768 MiB ceiling keeps every card >= 6 GiB on
// exactly the previous margin, so only small cards change behaviour.
// Override at runtime with env ANSCENTER_GPU_MARGIN_MB (or SetMargin); an
// explicit override wins over scaling. All queries operate on the CURRENT CUDA
// device (callers cudaSetDevice first).
// ============================================================================
#include <cuda_runtime.h>
#include <atomic>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
namespace ANSCENTER {
class GpuMemoryGuard {
public:
// ---- configuration ----------------------------------------------------
// Effective margin for a device with 'totalB' VRAM. An explicit override
// (env or SetMargin) wins; otherwise scale to the card.
static size_t MarginFor(size_t totalB) {
if (size_t ov = s_marginOverride.load(std::memory_order_relaxed)) return ov;
size_t m = totalB / kMarginDivisor;
if (m < kMarginMin) m = kMarginMin;
if (m > kMarginMax) m = kMarginMax;
return m;
}
// Effective margin for the CURRENT device. Prefer MarginFor() where the
// caller already has a total from Query() — this re-queries.
static size_t Margin() {
size_t freeB = 0, totalB = 0;
if (!Query(freeB, totalB)) return kMarginMax;
return MarginFor(totalB);
}
// b == 0 restores device-scaled margins.
static void SetMargin(size_t b) { s_marginOverride.store(b, std::memory_order_relaxed); }
// ---- (1) admission control at load ------------------------------------
// True if 'needBytes' can be committed on the current device while keeping
// the safety margin free. If VRAM can't be queried, returns true (never
// block on a failed query — let the normal path handle it).
static bool CanLoad(size_t needBytes) {
size_t freeB = 0, totalB = 0;
if (!Query(freeB, totalB)) return true;
return freeB >= needBytes + MarginFor(totalB);
}
// ---- (2) pressure guard at inference ----------------------------------
// True if this frame should SKIP GPU work: free VRAM below margin, or the
// circuit breaker is currently open.
static bool UnderPressure() {
if (BreakerOpen()) return true;
size_t freeB = 0, totalB = 0;
if (!Query(freeB, totalB)) return false;
return freeB < MarginFor(totalB);
}
// ---- (3) OOM-safe alloc + circuit breaker -----------------------------
static bool IsOom(cudaError_t e) { return e == cudaErrorMemoryAllocation; }
// Open the breaker for a short cooldown after a (recoverable) OOM so all
// callers back off together instead of repeatedly faulting the context.
static void NoteOom() {
s_breakerUntilMs.store(NowMs() + kCooldownMs, std::memory_order_relaxed);
}
static bool BreakerOpen() {
return NowMs() < s_breakerUntilMs.load(std::memory_order_relaxed);
}
// Lightweight current-device VRAM query. Clears the non-fatal sticky error
// that a failed query can leave on the context.
static bool Query(size_t& freeB, size_t& totalB) {
cudaError_t e = cudaMemGetInfo(&freeB, &totalB);
cudaGetLastError();
return e == cudaSuccess;
}
private:
static int64_t NowMs() {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
}
// 0 = no override, use device-scaled margin.
static size_t InitMarginOverride() {
if (const char* e = std::getenv("ANSCENTER_GPU_MARGIN_MB")) {
long mb = std::atol(e);
if (mb > 0) return static_cast<size_t>(mb) * 1024ULL * 1024ULL;
}
return 0;
}
static constexpr int64_t kCooldownMs = 1500; // breaker cooldown after OOM
static constexpr size_t kMarginDivisor = 8; // margin = total/8 ...
static constexpr size_t kMarginMin = 192ULL * 1024 * 1024; // ... clamped
static constexpr size_t kMarginMax = 768ULL * 1024 * 1024; // (prev. fixed value)
inline static std::atomic<size_t> s_marginOverride{ InitMarginOverride() };
inline static std::atomic<int64_t> s_breakerUntilMs{ 0 };
};
} // namespace ANSCENTER
+372 -19
View File
@@ -4,6 +4,7 @@
#include "ANSOVFaceDetector.h"
#include "SCRFDFaceDetector.h"
#include "ANSFaceRecognizer.h"
#include "ONNXFaceAttributes.h"
#include <faiss/IndexIDMap.h>
#include <faiss/IndexFlat.h>
#include <boost/uuid/uuid.hpp>
@@ -374,6 +375,27 @@ namespace ANSCENTER {
bool ANSFacialRecognition::LoadEngine() {
try {
ANS_DBG("ANSFR", "LoadEngine: starting");
// Restore the last explicitly chosen recognizer model so a
// runtime SetRecognizerModelType survives restarts. A choice
// made this session (before LoadEngine) takes precedence.
if (!_recModelExplicit && _db) {
std::string pref;
{
std::lock_guard<std::mutex> lock(_databaseMutex);
pref = _db->GetSetting("RECOGNIZER_MODEL_PREF");
}
if (pref == "arcface")
_requestedRecModel = FaceRecognizerModel::ARCFACE;
else if (pref == "adaface")
_requestedRecModel = FaceRecognizerModel::ADAFACE;
if (!pref.empty()) {
LogThreadSafe("ANSFacialRecognition::LoadEngine",
"Restored persisted recognizer model preference: " + pref,
LogLevel::Info);
}
}
// Unload existing engine (handles its own locking)
UnloadEngine();
@@ -463,6 +485,16 @@ namespace ANSCENTER {
attributeModelsSuccess = false;
}
// Embedding/model consistency: stored embeddings must come from
// the recognizer model that just loaded. A v1.4 upgrade flips the
// default from ArcFace to AdaFace, which silently invalidates a
// pre-existing database — regenerate before the index builds.
if (!SyncEmbeddingsWithActiveModel()) {
LogThreadSafe("ANSFacialRecognition::LoadEngine",
"Warning: embedding/model sync failed — recognition may return "
"Unknown until faces are re-enrolled");
}
// Reload database/embeddings
if (!Reload()) {
LogThreadSafe("ANSFacialRecognition::LoadEngine",
@@ -634,6 +666,19 @@ namespace ANSCENTER {
"Built new index with " + std::to_string(newFaissIndex->ntotal) +
" embeddings, " + std::to_string(newUserDict.size()) + " users", LogLevel::Info);
// Users without a single embedding cannot ever match — every face
// comes back Unknown/2.0 and nothing else in the log says why. The
// usual cause: faces were enrolled while the recognizer engine
// could not load (InsertFace stores the user but no embedding).
if (newFaissIndex->ntotal == 0 && !newUserDict.empty()) {
LogThreadSafe("ANSFacialRecognition::Reload",
"Face database has " + std::to_string(newUserDict.size()) +
" users but ZERO embeddings — all matches will return Unknown. "
"Faces were likely enrolled while the recognizer engine was "
"failing to load (check earlier engine errors). Re-enroll the "
"users' faces once the engine loads.", LogLevel::Warn);
}
// ============================================================
// PHASE 2: Atomic swap (brief exclusive lock ~microseconds)
// Inference is blocked only during this swap.
@@ -805,9 +850,12 @@ namespace ANSCENTER {
}
bool ANSFacialRecognition::InitializeRecognizer() {
try {
ANS_DBG("ANSFR", "InitializeRecognizer: starting, modelPath=%s", _recognizerFilePath.c_str());
ANS_DBG("ANSFR", "InitializeRecognizer: starting, modelPath=%s model=%s",
_recognizerFilePath.c_str(), FaceRecognizerModelName(_requestedRecModel));
// Create recognizer instance
_recognizer = std::make_unique<ANSFaceRecognizer>();
auto recognizer = std::make_unique<ANSFaceRecognizer>();
recognizer->SetPreferredModel(_requestedRecModel);
_recognizer = std::move(recognizer);
_recognizer->SetMaxSlotsPerGpu(m_maxSlotsPerGpu);
// Configure model
@@ -848,7 +896,13 @@ namespace ANSCENTER {
return false;
}
ANS_DBG("ANSFR", "InitializeRecognizer: SUCCESS");
// Record what actually loaded — ADAFACE silently falls back to
// ARCFACE when the model pack has no adafacerecognizer.onnx.
if (auto* concrete = dynamic_cast<ANSFaceRecognizer*>(_recognizer.get())) {
_activeRecModel = concrete->GetActiveModel();
}
ANS_DBG("ANSFR", "InitializeRecognizer: SUCCESS, active model=%s",
FaceRecognizerModelName(_activeRecModel));
return true;
}
catch (const std::exception& e) {
@@ -911,8 +965,50 @@ namespace ANSCENTER {
return false;
}
}
namespace {
// EP options for the ONNX attribute models. On NVIDIA the models are
// built into TensorRT engines through the TRT EP and cached on disk
// next to the model pack (first load pays the build, every later load
// is instant). AMD/Intel/CPU resolve to DirectML/OpenVINO/CPU inside
// BasicOrtHandler and ignore every TRT field.
OrtHandlerOptions BuildAttributeOrtOptions(EngineType engineType,
const std::string& modelFolder) {
OrtHandlerOptions opts;
if (engineType == EngineType::NVIDIA_GPU) {
opts.preferTensorRT = true;
opts.trtFP16 = true;
opts.trtEngineCacheDir = CreateFilePath(modelFolder, "TRTCache");
std::error_code ec;
std::filesystem::create_directories(opts.trtEngineCacheDir, ec);
}
return opts;
}
}
bool ANSFacialRecognition::InitializeAgeGenderModel(const std::string& deviceName) {
try {
// Prefer the ONNX model shipped since v1.4; fall back to the
// legacy OpenVINO IR when it is absent (or fails to load).
std::string onnxModelPath = CreateFilePath(_recognizerModelFolder, "agegender.onnx");
if (FileExist(onnxModelPath)) {
try {
auto detector = std::make_unique<OnnxAgeGenderEstimator>(
onnxModelPath, engineType,
BuildAttributeOrtOptions(engineType, _recognizerModelFolder));
std::lock_guard<std::mutex> lock(_ageGenderMutex);
_ageGenderDetector = std::move(detector);
LogThreadSafe("ANSFacialRecognition::InitializeAgeGenderModel",
"Loaded ONNX age-gender model: " + onnxModelPath, LogLevel::Info);
return true;
}
catch (const std::exception& e) {
LogThreadSafe("ANSFacialRecognition::InitializeAgeGenderModel",
"ONNX age-gender model failed (" + std::string(e.what()) +
") — falling back to OpenVINO model");
}
}
std::string ageGenderModelPath = CreateFilePath(_recognizerModelFolder, "agegender.xml");
if (!FileExist(ageGenderModelPath)) {
@@ -936,18 +1032,20 @@ namespace ANSCENTER {
_ageGenderDetector.reset();
// Create new detector
_ageGenderDetector = std::make_unique<AgeGenderDetection>(ageGenderModelPath, true);
auto detector = std::make_unique<AgeGenderDetection>(ageGenderModelPath, true);
// Compile model for target device
ov::CompiledModel compiledModel = core->compile_model(
_ageGenderDetector->read(*core),
detector->read(*core),
deviceName
);
// Create inference request
_ageGenderDetector->request = compiledModel.create_infer_request();
_ageGenderDetector->inTensor = _ageGenderDetector->request.get_input_tensor();
_ageGenderDetector->inTensor.set_shape(_ageGenderDetector->inShape);
detector->request = compiledModel.create_infer_request();
detector->inTensor = detector->request.get_input_tensor();
detector->inTensor.set_shape(detector->inShape);
_ageGenderDetector = std::move(detector);
}
return true;
@@ -966,6 +1064,30 @@ namespace ANSCENTER {
}
bool ANSFacialRecognition::InitializeEmotionModel(const std::string& deviceName) {
try {
// Prefer the YOLOv11 classifier shipped since v1.4 (class list in
// emotionsrecognition.names); fall back to the OpenVINO IR.
std::string onnxModelPath = CreateFilePath(_recognizerModelFolder, "emotionsrecognition.onnx");
if (FileExist(onnxModelPath)) {
try {
std::string namesPath = CreateFilePath(_recognizerModelFolder, "emotionsrecognition.names");
auto classNames = OnnxEmotionsClassifier::LoadClassNames(namesPath);
auto detector = std::make_unique<OnnxEmotionsClassifier>(
onnxModelPath, std::move(classNames), engineType,
BuildAttributeOrtOptions(engineType, _recognizerModelFolder));
std::lock_guard<std::mutex> lock(_emotionsMutex);
_emotionsDetector = std::move(detector);
LogThreadSafe("ANSFacialRecognition::InitializeEmotionModel",
"Loaded ONNX emotion model: " + onnxModelPath, LogLevel::Info);
return true;
}
catch (const std::exception& e) {
LogThreadSafe("ANSFacialRecognition::InitializeEmotionModel",
"ONNX emotion model failed (" + std::string(e.what()) +
") — falling back to OpenVINO model");
}
}
std::string emotionModelPath = CreateFilePath(_recognizerModelFolder, "emotionsrecognition.xml");
if (!FileExist(emotionModelPath)) {
@@ -989,18 +1111,20 @@ namespace ANSCENTER {
_emotionsDetector.reset();
// Create new detector
_emotionsDetector = std::make_unique<EmotionsDetection>(emotionModelPath, true);
auto detector = std::make_unique<EmotionsDetection>(emotionModelPath, true);
// Compile model for target device
ov::CompiledModel compiledModel = core->compile_model(
_emotionsDetector->read(*core),
detector->read(*core),
deviceName
);
// Create inference request
_emotionsDetector->request = compiledModel.create_infer_request();
_emotionsDetector->inTensor = _emotionsDetector->request.get_input_tensor();
_emotionsDetector->inTensor.set_shape(_emotionsDetector->inShape);
detector->request = compiledModel.create_infer_request();
detector->inTensor = detector->request.get_input_tensor();
detector->inTensor.set_shape(detector->inShape);
_emotionsDetector = std::move(detector);
}
return true;
@@ -1071,7 +1195,209 @@ namespace ANSCENTER {
return false;
}
}
bool ANSFacialRecognition::SyncEmbeddingsWithActiveModel() {
try {
if (!_db || !_recognizer) return false;
const std::string activeName = FaceRecognizerModelName(_activeRecModel);
std::string storedName;
std::vector<FaceRecord> faces;
{
std::lock_guard<std::mutex> lock(_databaseMutex);
storedName = _db->GetSetting("EMBEDDING_MODEL");
if (_db->GetAllFaces(faces) != 0) return false;
}
if (storedName.empty()) {
// Databases created before v1.4 never recorded the model —
// every embedding in them came from ArcFace.
storedName = faces.empty() ? activeName : "arcface";
}
if (storedName != activeName && !faces.empty()) {
LogThreadSafe("ANSFacialRecognition::SyncEmbeddingsWithActiveModel",
"Stored embeddings were generated by '" + storedName +
"' but the active recognizer is '" + activeName +
"' — regenerating " + std::to_string(faces.size()) +
" embeddings from the enrolled face images", LogLevel::Info);
if (!RegenerateEmbeddings()) return false;
}
{
std::lock_guard<std::mutex> lock(_databaseMutex);
_db->SetSetting("EMBEDDING_MODEL", activeName);
}
return true;
}
catch (const std::exception& e) {
LogThreadSafe("ANSFacialRecognition::SyncEmbeddingsWithActiveModel", e.what());
return false;
}
}
bool ANSFacialRecognition::RegenerateEmbeddings() {
try {
if (!_db || !_recognizer || !_recognizer->IsInitialized()) {
LogThreadSafe("ANSFacialRecognition::RegenerateEmbeddings",
"Recognizer/database not ready");
return false;
}
std::vector<FaceRecord> faces;
{
std::lock_guard<std::mutex> lock(_databaseMutex);
if (_db->GetAllFaces(faces) != 0) return false;
}
if (faces.empty()) return true;
size_t updated = 0, failed = 0;
for (const auto& face : faces) {
// InsertFace stored the aligned face crop — compute the new
// embedding directly on it, no re-detection needed.
cv::Mat crop = cv::imread(face.ImagePath, cv::IMREAD_COLOR);
if (crop.empty()) {
LogThreadSafe("ANSFacialRecognition::RegenerateEmbeddings",
"Cannot read enrolled face image (face ID " +
std::to_string(face.FaceId) + "): " + face.ImagePath);
++failed;
continue;
}
Object faceObject;
faceObject.mask = crop;
std::vector<float> embedding;
{
std::lock_guard<std::mutex> lock(_recognitionMutex);
embedding = _recognizer->Feature(crop, faceObject);
}
if (embedding.size() != static_cast<size_t>(_config._recOutputDim)) {
LogThreadSafe("ANSFacialRecognition::RegenerateEmbeddings",
"Embedding failed for face ID " + std::to_string(face.FaceId));
++failed;
continue;
}
std::lock_guard<std::mutex> lock(_databaseMutex);
if (_db->UpdateFaceEmbedding(face.FaceId, embedding.data()) > 0) ++updated;
else ++failed;
}
LogThreadSafe("ANSFacialRecognition::RegenerateEmbeddings",
"Regenerated " + std::to_string(updated) + "/" +
std::to_string(faces.size()) + " embeddings (" +
std::to_string(failed) + " failed)", LogLevel::Info);
_reloadNeeded.store(true);
return updated > 0 || failed == 0;
}
catch (const std::exception& e) {
LogThreadSafe("ANSFacialRecognition::RegenerateEmbeddings", e.what());
return false;
}
}
bool ANSFacialRecognition::SetRecognizerModelType(int modelType) {
try {
if (modelType < 0 || modelType > 1) {
LogThreadSafe("ANSFacialRecognition::SetRecognizerModelType",
"Invalid model type: " + std::to_string(modelType) +
" (0 = AdaFace, 1 = ArcFace)");
return false;
}
const auto requested = static_cast<FaceRecognizerModel>(modelType);
_requestedRecModel = requested;
_recModelExplicit = true;
// Persist the explicit choice so it survives restarts (restored
// by LoadEngine when no choice is made in that session).
if (_db) {
std::lock_guard<std::mutex> lock(_databaseMutex);
_db->SetSetting("RECOGNIZER_MODEL_PREF", FaceRecognizerModelName(requested));
}
// Engine not loaded yet — the preference is applied by LoadEngine.
if (!_recognizer) return true;
if (requested == _activeRecModel) return true;
LogThreadSafe("ANSFacialRecognition::SetRecognizerModelType",
"Switching recognizer model to '" +
std::string(FaceRecognizerModelName(requested)) + "'", LogLevel::Info);
if (!InitializeRecognizer()) {
LogThreadSafe("ANSFacialRecognition::SetRecognizerModelType",
"Failed to load recognizer for model '" +
std::string(FaceRecognizerModelName(requested)) + "'");
return false;
}
_recognizerModelFolder = _recognizer->GetModelFolder();
if (!SyncEmbeddingsWithActiveModel()) {
LogThreadSafe("ANSFacialRecognition::SetRecognizerModelType",
"Warning: embedding regeneration failed after model switch");
}
_reloadNeeded.store(true);
return Reload();
}
catch (const std::exception& e) {
LogThreadSafe("ANSFacialRecognition::SetRecognizerModelType", e.what());
return false;
}
}
bool ANSFacialRecognition::UpdateAttributeInterval(int frames) {
if (frames < 1 || frames > 60) {
LogThreadSafe("ANSFacialRecognition::UpdateAttributeInterval",
"Invalid interval " + std::to_string(frames) + " (expected 1..60)");
return false;
}
{
std::lock_guard<std::mutex> lock(_configMutex);
_attributeInterval = frames;
}
LogThreadSafe("ANSFacialRecognition::UpdateAttributeInterval",
"Attribute refresh interval set to " + std::to_string(frames) + " frames",
LogLevel::Info);
return true;
}
int ANSFacialRecognition::GetRecognizerModelType() const {
return static_cast<int>(_recognizer ? _activeRecModel : _requestedRecModel);
}
std::string ANSFacialRecognition::GetActiveModelInfo() {
try {
const char* agegenderBackend = "disabled";
const char* emotionBackend = "disabled";
{
std::lock_guard<std::mutex> lock(_ageGenderMutex);
if (_ageGenderDetector && _ageGenderDetector->enabled())
agegenderBackend = _ageGenderDetector->backendName();
}
{
std::lock_guard<std::mutex> lock(_emotionsMutex);
if (_emotionsDetector && _emotionsDetector->enabled())
emotionBackend = _emotionsDetector->backendName();
}
nlohmann::json info;
info["recognizer_model"] = FaceRecognizerModelName(
_recognizer ? _activeRecModel : _requestedRecModel);
info["recognizer_loaded"] = (_recognizer != nullptr);
info["agegender_backend"] = agegenderBackend;
info["emotion_backend"] = emotionBackend;
info["attribute_interval"] = _attributeInterval;
return info.dump();
}
catch (const std::exception& e) {
LogThreadSafe("ANSFacialRecognition::GetActiveModelInfo", e.what());
return "{}";
}
}
bool ANSFacialRecognition::UpdateParameters(
float knownPersonThreshold,
bool enableAgeGender,
@@ -1353,6 +1679,17 @@ namespace ANSCENTER {
// Get largest face (CPU operation)
Object faceObject = GetLargestObject(detectedFaces);
// The recognizer must be up before we can compute an embedding.
// Feature() returns empty when uninitialized, which the size check
// below would misreport as a bad face — name the real cause here.
if (!_recognizer || !_recognizer->IsInitialized()) {
LogThreadSafe("ANSFacialRecognition::InsertFace",
"Cannot enroll face for user ID " + std::to_string(userId) +
": recognizer is NOT initialized (engine failed to load?). "
"No embedding stored — fix the engine errors, then re-enroll.");
return 0;
}
// LOCK ONLY DURING RECOGNITION (shared recognizer)
std::vector<float> embedding;
{
@@ -1363,8 +1700,10 @@ namespace ANSCENTER {
// Validate embedding
if (embedding.empty() || embedding.size() != _config._recOutputDim) {
LogThreadSafe("ANSFacialRecognition::InsertFace",
"Invalid embedding generated for user ID " + std::to_string(userId) +
" (size: " + std::to_string(embedding.size()) + ")");
"Invalid embedding for user ID " + std::to_string(userId) +
" (size: " + std::to_string(embedding.size()) + ", expected " +
std::to_string(_config._recOutputDim) + ") — face crop unusable "
"or recognizer busy/reloading. Face NOT enrolled.");
return 0;
}
@@ -1461,6 +1800,16 @@ namespace ANSCENTER {
// Get largest face
Object faceObject = GetLargestObject(detectedFaces);
// Same readiness gate as InsertFace — name the real cause instead
// of letting the size check blame the face crop.
if (!_recognizer || !_recognizer->IsInitialized()) {
LogThreadSafe("ANSFacialRecognition::InsertMultipleFaces",
"Cannot enroll face for user ID " + std::to_string(userId) +
": recognizer is NOT initialized (engine failed to load?). "
"No embedding stored — fix the engine errors, then re-enroll.");
return faceIds;
}
// LOCK ONLY DURING RECOGNITION (shared recognizer)
std::vector<float> embedding;
{
@@ -1470,7 +1819,10 @@ namespace ANSCENTER {
if (embedding.empty() || embedding.size() != _config._recOutputDim) {
LogThreadSafe("ANSFacialRecognition::InsertMultipleFaces",
"Invalid embedding generated for user ID " + std::to_string(userId));
"Invalid embedding for user ID " + std::to_string(userId) +
" (size: " + std::to_string(embedding.size()) + ", expected " +
std::to_string(_config._recOutputDim) + ") — face crop unusable "
"or recognizer busy/reloading. Face NOT enrolled.");
return faceIds;
}
@@ -2737,9 +3089,10 @@ namespace ANSCENTER {
}
camDataPtr->previousTrackIdCount = currentTrackIdCount;
// Adapt interval: stable scene → increase (max 10), changes → decrease (min 2)
// Adapt interval: stable scene → double the configured
// interval, changes → decrease (min 2)
if (camDataPtr->stableFrameCount > 30) {
camDataPtr->currentAttributeInterval = std::min(10, _attributeInterval * 2);
camDataPtr->currentAttributeInterval = _attributeInterval * 2;
} else if (camDataPtr->stableFrameCount > 10) {
camDataPtr->currentAttributeInterval = _attributeInterval;
} else {
+64 -5
View File
@@ -97,6 +97,26 @@ namespace ANSCENTER
bool UpdateFaceQueue(int queueSize, int faceThresholdSize, bool enableFaceQueue);
bool GetFaceQueue(int& queueSize, int& faceThresholdSize, bool& enableFaceQueue);
// How often the slow attributes (age/gender + emotions) refresh:
// every N frames per camera (1 = every frame). Head pose still runs
// every frame. The adaptive logic doubles N on stable scenes and
// halves it (min 2) when faces appear/disappear. Default 20 —
// benchmarked at throughput parity with the v1.1 pack while the
// heavier v1.4 ONNX age-gender model is loaded.
bool UpdateAttributeInterval(int frames);
int GetAttributeInterval() const { return _attributeInterval; }
// Recognizer model switching (0 = AdaFace [default], 1 = ArcFace).
// Before LoadEngine: just records the preference. After LoadEngine:
// re-initializes the recognizer, regenerates every stored embedding
// from the enrolled face crops (embeddings from different models are
// not comparable), and rebuilds the FAISS index.
bool SetRecognizerModelType(int modelType);
int GetRecognizerModelType() const;
// JSON summary of what is actually loaded: recognizer model plus the
// backend ("onnx" | "openvino" | "disabled") serving each attribute.
[[nodiscard]] std::string GetActiveModelInfo();
[[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);
@@ -141,6 +161,11 @@ namespace ANSCENTER
bool InitializeAgeGenderModel(const std::string& deviceName);
bool InitializeEmotionModel(const std::string& deviceName);
bool InitializeHeadPoseModel(const std::string& deviceName);
// Regenerates stored embeddings when they were produced by a
// different recognizer model than the one currently loaded
// (tracked via the EMBEDDING_MODEL key in the face database).
bool SyncEmbeddingsWithActiveModel();
bool RegenerateEmbeddings();
void ensureUniqueUserIdWithHighestConfidence(std::vector<FaceResultObject>& resultObjects, CameraData* camData = nullptr);
Object GetLargestObject(const std::vector<Object>& objects);
bool AreFacesSimilar(const FaceResultObject& face1, const FaceResultObject& face2);
@@ -172,7 +197,7 @@ namespace ANSCENTER
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)
int _attributeInterval{ 20 }; // Run slow attributes every N frames (1=every frame)
FaceChecker faceChecker;
@@ -214,9 +239,18 @@ namespace ANSCENTER
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<IAgeGenderEstimator>_ageGenderDetector = nullptr;
std::unique_ptr<HeadPoseDetection>_headPoseDetector = nullptr;
std::unique_ptr<EmotionsDetection>_emotionsDetector = nullptr;
std::unique_ptr<IEmotionsClassifier>_emotionsDetector = nullptr;
// Recognizer model preference (settable before/after LoadEngine)
// and what actually loaded (fallback may downgrade ADAFACE->ARCFACE).
FaceRecognizerModel _requestedRecModel{ FaceRecognizerModel::ADAFACE };
FaceRecognizerModel _activeRecModel{ FaceRecognizerModel::ADAFACE };
// True once SetRecognizerModelType was called this session. An
// explicit choice is persisted (RECOGNIZER_MODEL_PREF in the face
// DB) and restored by LoadEngine on later runs; without it the
// stored preference (if any) wins over the built-in default.
bool _recModelExplicit{ false };
const size_t QUEUE_SIZE = 10;
const size_t FACE_THRESHOLD_SIZE = 5;
// Cached attribute JSON per tracked face (everything except head pose).
@@ -244,7 +278,7 @@ namespace ANSCENTER
std::unordered_map<int, TrackIdentity> trackIdentities; // trackId → accumulated identity
// Adaptive interval state
int currentAttributeInterval = 5; // current adaptive interval
int currentAttributeInterval = 20; // current adaptive interval
int stableFrameCount = 0; // frames with no new/lost trackIds
int previousTrackIdCount = 0; // trackId count from last frame
@@ -258,7 +292,7 @@ namespace ANSCENTER
attributeFrameCounter = 0;
cachedAttributes.clear();
trackIdentities.clear();
currentAttributeInterval = 5;
currentAttributeInterval = 20;
stableFrameCount = 0;
previousTrackIdCount = 0;
}
@@ -348,6 +382,31 @@ extern "C" ANSFR_API int GetParamters(ANSCENTER::ANSFacialRecognition** Handl
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);
// Recognizer model switching (ANS_FaceRecognizer v1.4+).
// modelType: 0 = AdaFace (default), 1 = ArcFace.
// Call before LoadANSRFEngine to pick the model for the initial load, or
// after it to switch live — a live switch reloads the recognizer engine and
// regenerates every stored face embedding from the enrolled face images
// (embeddings produced by different models are not comparable), so it can
// take a while on large databases. The explicit choice is persisted in the
// face database (RECOGNIZER_MODEL_PREF) and restored automatically on the
// next LoadANSRFEngine, so it survives restarts without re-calling this.
// Returns 1 on success, 0 on failure, -1 on invalid arguments.
extern "C" ANSFR_API int SetANSRFRecognizerModel(ANSCENTER::ANSFacialRecognition** Handle, int modelType);
// Returns the ACTIVE model (0 = AdaFace, 1 = ArcFace) or -1 on error. May
// differ from what was requested when the model pack has no
// adafacerecognizer.onnx (pre-v1.4 zips fall back to ArcFace).
extern "C" ANSFR_API int GetANSRFRecognizerModel(ANSCENTER::ANSFacialRecognition** Handle);
// JSON describing the loaded models, e.g.
// {"recognizer_model":"adaface","agegender_backend":"onnx","emotion_backend":"onnx",...}
extern "C" ANSFR_API int GetANSRFModelInfo(ANSCENTER::ANSFacialRecognition** Handle, LStrHandle modelInfo);
// Slow-attribute (age/gender + emotions) refresh interval in frames per
// camera (1 = every frame, default 20; clamped to [1, 60]). Larger values
// cut the attribute-model GPU cost proportionally at the price of slower
// attribute updates; lower it for the freshest attributes on GPUs with
// headroom. Returns 1 on success, -1 on invalid arguments.
extern "C" ANSFR_API int SetANSRFAttributeInterval(ANSCENTER::ANSFacialRecognition** Handle, int frames);
extern "C" ANSFR_API int GetUserString(ANSCENTER::ANSFacialRecognition * *Handle, int userId, std::string& userRecord);
+12 -13
View File
@@ -638,30 +638,29 @@ namespace ANSCENTER {
detectedNode.put("Age", "Adults");
else
detectedNode.put("Age", "Elders");
// Numeric age alongside the bucket — additive key, existing
// consumers keep parsing "Age" unchanged.
detectedNode.put("AgeYears", std::to_string(age));
}
if (face.isEmotionsEnabled()) {
const auto& [emotionName, emotionConfidence] = face.getMainEmotion();
std::ostringstream emotionStr;
// The v1.4 ONNX classifier's classes (emotionsrecognition.names:
// Angry, Fear, Happy, Neutral, Sad) are reported VERBATIM. Only
// the legacy OpenVINO model's lowercase classes keep the
// historical display remap so v1.1 output is unchanged.
std::string adjustedEmotionName = emotionName;
if (emotionName == "Sad" || emotionName == "sad")
if (emotionName == "sad")
adjustedEmotionName = "Attentive";
else if (emotionName == "Happy" || emotionName == "happy")
else if (emotionName == "happy")
adjustedEmotionName = "Happy";
else if (emotionName == "Neutral" || emotionName == "neutral")
else if (emotionName == "neutral")
adjustedEmotionName = "Neutral";
else if (emotionName == "Angry" || emotionName == "angry")
else if (emotionName == "anger")
adjustedEmotionName = "Attentive";
else if (emotionName == "Surprised" || emotionName == "surprised")
else if (emotionName == "surprise")
adjustedEmotionName = "Attentive";
else if (emotionName == "Disgusted" || emotionName == "disgusted")
adjustedEmotionName = "Attentive";
else if (emotionName == "Fearful" || emotionName == "fearful")
adjustedEmotionName = "Attentive";
else if (emotionName == "Anger" || emotionName == "anger")
adjustedEmotionName = "Attentive";
else
adjustedEmotionName = "Neutral";
emotionStr << adjustedEmotionName << " (" << static_cast<int>(emotionConfidence * 100) << "%)";
detectedNode.put("Emotion", emotionStr.str());
+44 -4
View File
@@ -107,6 +107,42 @@ namespace ANSCENTER {
float age;
float maleProb;
};
// Which face-embedding model the recognizer runs. ADAFACE is the default
// since ANS_FaceRecognizer v1.4 (adafacerecognizer.onnx); when that file
// is missing from the model zip the recognizer silently falls back to
// ARCFACE (ansfacerecognizer.onnx) so v1.3-and-older zips keep working.
enum class FaceRecognizerModel : int {
ADAFACE = 0,
ARCFACE = 1,
};
inline const char* FaceRecognizerModelName(FaceRecognizerModel m) {
return (m == FaceRecognizerModel::ARCFACE) ? "arcface" : "adaface";
}
// ----------------------------------------------------------------
// Attribute-model backends. The v1.4 model pack ships ONNX attribute
// models (agegender.onnx / emotionsrecognition.onnx) next to the
// legacy OpenVINO IR pairs (.xml/.bin). These interfaces let the
// pipeline hold either backend behind the same two calls it already
// makes: runInfer() and enabled().
// ----------------------------------------------------------------
class ANSF_API IAgeGenderEstimator {
public:
virtual ~IAgeGenderEstimator() = default;
virtual AgeGenderResults runInfer(const cv::Mat& frame) = 0;
virtual bool enabled() const = 0;
virtual const char* backendName() const = 0; // "openvino" | "onnx"
};
class ANSF_API IEmotionsClassifier {
public:
virtual ~IEmotionsClassifier() = default;
virtual std::map<std::string, float> runInfer(const cv::Mat& frame) = 0;
virtual bool enabled() const = 0;
virtual const char* backendName() const = 0; // "openvino" | "onnx"
};
//// Class for face attribute
class ANSF_API Face {
public:
@@ -200,7 +236,7 @@ namespace ANSCENTER {
//std::vector<HeadPoseResults> getAllResults();
};
class ANSF_API AgeGenderDetection :public BaseDetection {
class ANSF_API AgeGenderDetection : public BaseDetection, public IAgeGenderEstimator {
public:
std::recursive_mutex _mutex;
std::string outputAge;
@@ -211,7 +247,9 @@ namespace ANSCENTER {
bool doRawOutputMessages);
virtual std::shared_ptr<ov::Model> read(const ov::Core& core) override;
AgeGenderResults runInfer(const cv::Mat& frame);
AgeGenderResults runInfer(const cv::Mat& frame) override;
bool enabled() const override { return BaseDetection::enabled(); }
const char* backendName() const override { return "openvino"; }
//void submitRequest();
//void enqueue(const cv::Mat& face);
//AgeGenderResults getResult(int idx);
@@ -219,7 +257,7 @@ namespace ANSCENTER {
};
class ANSF_API EmotionsDetection : public BaseDetection {
class ANSF_API EmotionsDetection : public BaseDetection, public IEmotionsClassifier {
public:
std::recursive_mutex _mutex;
//size_t enquedFaces;
@@ -230,7 +268,9 @@ namespace ANSCENTER {
bool doRawOutputMessages);
virtual std::shared_ptr<ov::Model> read(const ov::Core& core) override;
std::map<std::string, float> runInfer(const cv::Mat& frame);
std::map<std::string, float> runInfer(const cv::Mat& frame) override;
bool enabled() const override { return BaseDetection::enabled(); }
const char* backendName() const override { return "openvino"; }
//void submitRequest();
//void enqueue(const cv::Mat& face);
//std::map<std::string, float> getResult(int idx);
+163 -37
View File
@@ -1,5 +1,7 @@
#include "ANSFaceRecognizer.h"
#include "engine.h"
#include <cctype>
#include <filesystem>
//#define DEBUG_TIME
#ifdef DEBUG_TIME
@@ -17,7 +19,99 @@
#define FR_END_TIMER(name, label)
#endif
namespace {
// Batch >1 only helps when several faces arrive in one frame, but the TRT
// workspace grows ~linearly with maxBatchSize (~54 MiB per batch unit for
// this model). The batch-4 profile was tuned for 8 GB GPUs; on smaller
// cards (e.g. 4 GB RTX A400) that workspace crowds out the other engines
// sharing the card. Below the 8 GB class, fall back to single-face
// batches — results are identical, multi-face frames just run
// sequentially. Threshold is 7.5 GiB because 8 GB cards report slightly
// under 8192 MiB (~8188).
int VramCappedBatch(int desiredBatch, int deviceIndex) {
cudaDeviceProp prop{};
if (cudaGetDeviceProperties(&prop, deviceIndex >= 0 ? deviceIndex : 0) != cudaSuccess)
return desiredBatch; // can't query — keep the configured profile
const size_t totalMiB = prop.totalGlobalMem / (1024ULL * 1024ULL);
return (totalMiB >= 7680) ? desiredBatch : 1;
}
// A ServerOptimised zip is a per-GPU cache of a previous engine build. When
// one was supplied but the engine still had to be rebuilt from ONNX, the
// zip's cached engine is stale (batch profile or TRT/CUDA version changed).
// Delete the stale engine variants for this GPU from the model folder (so a
// regenerated zip doesn't carry two engines) and the zip itself — the
// ANSVIS Server recreates the zip from the model folder when it is missing.
// Base model zips (the ONNX source artifact) are never touched: only zips
// whose parent folder is ServerOptimised qualify.
void PurgeStaleOptimisedModel(const std::string& modelZipFilePath,
const std::string& currentEnginePath) {
namespace fs = std::filesystem;
try {
const fs::path zip(modelZipFilePath);
std::string parent = zip.parent_path().filename().string();
for (char& c : parent) c = (char)std::tolower((unsigned char)c);
if (parent != "serveroptimised" && parent != "serveroptimized")
return;
// Stale variants share the "<model>.engine.<GPU>." prefix of the
// freshly built engine; other GPUs' engines on multi-GPU machines
// have a different prefix and are left alone.
const fs::path engine(currentEnginePath);
const std::string engineName = engine.filename().string();
const size_t tagPos = engineName.find(".engine.");
if (tagPos != std::string::npos && engine.has_parent_path()) {
const size_t gpuEnd = engineName.find('.', tagPos + 8);
const std::string prefix = (gpuEnd != std::string::npos)
? engineName.substr(0, gpuEnd + 1) : engineName;
for (const auto& entry : fs::directory_iterator(engine.parent_path())) {
if (!entry.is_regular_file()) continue;
const std::string name = entry.path().filename().string();
if (name != engineName && name.rfind(prefix, 0) == 0) {
std::error_code ec;
fs::remove(entry.path(), ec);
if (!ec) std::cout << "[ANSFR] Purged stale engine: " << name << std::endl;
}
}
}
std::error_code ec;
fs::remove(zip, ec);
if (!ec) std::cout << "[ANSFR] Purged stale optimised model zip (server will regenerate): "
<< modelZipFilePath << std::endl;
}
catch (const std::exception&) {
// Cleanup is best-effort; a leftover stale zip only costs a rebuild.
}
}
}
namespace ANSCENTER {
std::string ANSFaceRecognizer::ResolveRecognizerModelFile() {
const std::string adaPath = CreateFilePath(_modelFolder, "adafacerecognizer.onnx");
const std::string arcPath = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx");
if (_preferredModel == FaceRecognizerModel::ADAFACE) {
if (std::filesystem::exists(adaPath)) {
_activeModel = FaceRecognizerModel::ADAFACE;
m_swapRB = false; // AdaFace consumes BGR
ANS_DBG("FaceRecognizer", "Recognizer model resolved: ADAFACE (%s)", adaPath.c_str());
return adaPath;
}
_logger.LogWarn("ANSFaceRecognizer::ResolveRecognizerModelFile",
"adafacerecognizer.onnx not found in model pack — falling back to ArcFace",
__FILE__, __LINE__);
}
_activeModel = FaceRecognizerModel::ARCFACE;
m_swapRB = true; // ArcFace consumes RGB
if (std::filesystem::exists(arcPath)) {
ANS_DBG("FaceRecognizer", "Recognizer model resolved: ARCFACE (%s)", arcPath.c_str());
return arcPath;
}
return std::string();
}
std::string ANSFaceRecognizer::GetOpenVINODevice() {
ov::Core core;
std::vector<std::string> available_devices = core.get_available_devices();
@@ -77,16 +171,16 @@ namespace ANSCENTER {
static_cast<int>(EngineType::AMD_GPU),
static_cast<int>(EngineType::CPU));
if (engineType == EngineType::NVIDIA_GPU) {
// 1. Load ONNX model
std::string onnxfile = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx");
if (std::filesystem::exists(onnxfile)) {
// 1. Load ONNX model (adaface by default, arcface fallback)
std::string onnxfile = ResolveRecognizerModelFile();
if (!onnxfile.empty()) {
_modelFilePath = onnxfile;
_logger.LogDebug("ANSFaceRecognizer::Initialize. Loading ansfacerecognizer weight",
_logger.LogDebug("ANSFaceRecognizer::Initialize. Loading face recognizer weight",
_modelFilePath, __FILE__, __LINE__);
}
else {
_logger.LogError("ANSFaceRecognizer::Initialize. Model ansfacerecognizer.onnx does not exist",
_modelFilePath, __FILE__, __LINE__);
_logger.LogError("ANSFaceRecognizer::Initialize. No recognizer model (adafacerecognizer.onnx / ansfacerecognizer.onnx) exists",
_modelFolder, __FILE__, __LINE__);
return false;
}
@@ -97,8 +191,10 @@ namespace ANSCENTER {
// comfortably on 8 GB GPUs while freeing ~1.5 GB VRAM vs max=32
// — most scenes have ≤4 faces visible, so throughput cost is
// near-zero (amortized per-face latency drops too at lower batch).
m_options.optBatchSize = 4;
m_options.maxBatchSize = 4;
// VramCappedBatch drops this to 1 on sub-8 GB cards.
const int frBatch = VramCappedBatch(4, _modelConfig.gpuDeviceIndex);
m_options.optBatchSize = frBatch;
m_options.maxBatchSize = frBatch;
m_options.maxInputHeight = GPU_FACE_HEIGHT;
m_options.minInputHeight = GPU_FACE_HEIGHT;
@@ -127,6 +223,13 @@ namespace ANSCENTER {
return false;
}
// If we were handed a ServerOptimised zip yet still had to
// rebuild from ONNX, that zip's engine is stale — remove it
// so the server regenerates it with the fresh engine.
if (m_trtEngine->rebuiltFromOnnx()) {
PurgeStaleOptimisedModel(modelZipFilePath, m_trtEngine->engineFilePath());
}
// Create CUDA stream for GPU preprocessing (lazy init)
if (!m_gpuStream) {
m_gpuStream = std::make_unique<cv::cuda::Stream>();
@@ -135,24 +238,28 @@ namespace ANSCENTER {
}
else {
#ifdef USE_ONNX_ENGINE
std::string faceidModel = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx");
if (std::filesystem::exists(faceidModel)) {
std::string faceidModel = ResolveRecognizerModelFile();
if (!faceidModel.empty()) {
_modelFilePath = faceidModel;
_logger.LogDebug("ANSFaceRecognizer::Initialize. Loading ANSFaceRecognizer weight",
_modelFilePath, __FILE__, __LINE__);
}
else {
_logger.LogError("ANSFaceRecognizer::Initialize. Model ansfacerecognizer.onnx does not exist",
_modelFilePath, __FILE__, __LINE__);
_logger.LogError("ANSFaceRecognizer::Initialize. No recognizer model (adafacerecognizer.onnx / ansfacerecognizer.onnx) exists",
_modelFolder, __FILE__, __LINE__);
return false;
}
ANS_DBG("FaceRecognizer", "Creating GlintArcFace with engineType=%d model=%s",
ANS_DBG("FaceRecognizer", "Creating %s with engineType=%d model=%s",
FaceRecognizerModelName(_activeModel),
static_cast<int>(engineType), faceidModel.c_str());
faceRecognizer = std::make_unique<GlintArcFace>(faceidModel, engineType);
if (_activeModel == FaceRecognizerModel::ADAFACE)
faceRecognizer = std::make_unique<AdaFace>(faceidModel, engineType);
else
faceRecognizer = std::make_unique<GlintArcFace>(faceidModel, engineType);
if (!faceRecognizer) {
_logger.LogFatal("ANSFaceRecognizer::Initialize",
"Failed to initialize ONNX face recognizer", __FILE__, __LINE__);
ANS_DBG("FaceRecognizer", "FAILED: GlintArcFace returned null");
ANS_DBG("FaceRecognizer", "FAILED: ONNX face recognizer returned null");
return false;
}
#else
@@ -201,29 +308,29 @@ namespace ANSCENTER {
if (!result) return false;
if (engineType == EngineType::NVIDIA_GPU) {
std::string onnxfile = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx");
if (std::filesystem::exists(onnxfile)) {
std::string onnxfile = ResolveRecognizerModelFile();
if (!onnxfile.empty()) {
_modelFilePath = onnxfile;
_logger.LogDebug("ANSFaceRecognizer::LoadModel. Loading ansfacerecognizer weight",
_logger.LogDebug("ANSFaceRecognizer::LoadModel. Loading face recognizer weight",
_modelFilePath, __FILE__, __LINE__);
}
else {
_logger.LogError("ANSFaceRecognizer::LoadModel. ansfacerecognizer.onnx not found",
_modelFilePath, __FILE__, __LINE__);
_logger.LogError("ANSFaceRecognizer::LoadModel. No recognizer model (adafacerecognizer.onnx / ansfacerecognizer.onnx) found",
_modelFolder, __FILE__, __LINE__);
return false;
}
}
else {
std::string faceidModel = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx");
std::string faceidModel = ResolveRecognizerModelFile();
#ifdef USE_ONNX_ENGINE
if (std::filesystem::exists(faceidModel)) {
if (!faceidModel.empty()) {
_modelFilePath = faceidModel;
_logger.LogDebug("ANSFaceRecognizer::LoadModel. Loading ansfacerecognizer weight",
_logger.LogDebug("ANSFaceRecognizer::LoadModel. Loading face recognizer weight",
_modelFilePath, __FILE__, __LINE__);
}
else {
_logger.LogError("ANSFaceRecognizer::LoadModel. ansfacerecognizer.onnx not found",
_modelFilePath, __FILE__, __LINE__);
_logger.LogError("ANSFaceRecognizer::LoadModel. No recognizer model (adafacerecognizer.onnx / ansfacerecognizer.onnx) found",
_modelFolder, __FILE__, __LINE__);
return false;
}
#else
@@ -259,9 +366,10 @@ namespace ANSCENTER {
// engine built/looked-up here has a different profile than the one
// Initialize() expects, causing either a stale `.b32` rebuild every
// launch (wasting 30-60s) or a load-time profile mismatch.
// Keep in sync with lines 100-114.
m_options.optBatchSize = 4;
m_options.maxBatchSize = 4;
// Keep in sync with the Initialize() block (same VramCappedBatch call).
const int frBatch = VramCappedBatch(4, _modelConfig.gpuDeviceIndex);
m_options.optBatchSize = frBatch;
m_options.maxBatchSize = frBatch;
m_options.maxInputHeight = GPU_FACE_HEIGHT;
m_options.minInputHeight = GPU_FACE_HEIGHT;
@@ -490,8 +598,12 @@ namespace ANSCENTER {
if (engineType == EngineType::NVIDIA_GPU) {
// Initialize TensorRT engine via shared pool
if (!m_trtEngine) {
m_options.optBatchSize = 8;
m_options.maxBatchSize = 32;
// Note this path still uses the legacy 8/32 profile on 8 GB+
// cards (a .b32 engine costs ~1.5 GB more workspace than .b4).
// The VRAM cap only stops it from being fatal on small cards.
const int frMaxBatch = VramCappedBatch(32, _modelConfig.gpuDeviceIndex);
m_options.optBatchSize = (frMaxBatch < 8) ? frMaxBatch : 8;
m_options.maxBatchSize = frMaxBatch;
m_options.calibrationBatchSize = 8;
m_options.deviceIndex = _modelConfig.gpuDeviceIndex;
m_options.engineFileDir = _modelFolder;
@@ -751,7 +863,7 @@ namespace ANSCENTER {
}
try {
// CPU preprocessing: resize + BGR→RGB before GPU upload
// CPU preprocessing: resize + channel order before GPU upload
// Reduces PCIe transfer and eliminates GPU cvtColor/resize overhead
cv::Mat srcImg = inputImage;
if (srcImg.channels() == 1) {
@@ -763,8 +875,13 @@ namespace ANSCENTER {
} else {
cpuResized = srcImg;
}
// ArcFace needs RGB; AdaFace consumes the Mat's native BGR.
cv::Mat cpuRGB;
cv::cvtColor(cpuResized, cpuRGB, cv::COLOR_BGR2RGB);
if (m_swapRB) {
cv::cvtColor(cpuResized, cpuRGB, cv::COLOR_BGR2RGB);
} else {
cpuRGB = cpuResized;
}
m_gpuRgb.upload(cpuRGB, *m_gpuStream);
m_gpuStream->waitForCompletion();
@@ -880,14 +997,19 @@ namespace ANSCENTER {
cv::cuda::resize(d_img, d_resized, targetSize, 0, 0, cv::INTER_LINEAR, *m_gpuStream);
d_img = d_resized;
}
cv::cuda::GpuMat d_rgb;
cv::cuda::cvtColor(d_img, d_rgb, cv::COLOR_BGR2RGB, 0, *m_gpuStream);
batchGpu.emplace_back(std::move(d_rgb));
// ArcFace needs RGB; AdaFace consumes native BGR.
if (m_swapRB) {
cv::cuda::GpuMat d_rgb;
cv::cuda::cvtColor(d_img, d_rgb, cv::COLOR_BGR2RGB, 0, *m_gpuStream);
batchGpu.emplace_back(std::move(d_rgb));
} else {
batchGpu.emplace_back(std::move(d_img));
}
} else {
const auto& roi = faceROIs[i];
if (roi.empty()) continue;
// CPU preprocessing: resize + BGR→RGB before upload
// CPU preprocessing: resize + channel order before upload
cv::Mat srcImg = roi;
if (srcImg.channels() == 1) {
cv::cvtColor(srcImg, srcImg, cv::COLOR_GRAY2BGR);
@@ -899,7 +1021,11 @@ namespace ANSCENTER {
cpuResized = srcImg;
}
cv::Mat cpuRGB;
cv::cvtColor(cpuResized, cpuRGB, cv::COLOR_BGR2RGB);
if (m_swapRB) {
cv::cvtColor(cpuResized, cpuRGB, cv::COLOR_BGR2RGB);
} else {
cpuRGB = cpuResized;
}
cv::cuda::GpuMat d_rgb;
d_rgb.upload(cpuRGB, *m_gpuStream);
batchGpu.emplace_back(std::move(d_rgb));
+18
View File
@@ -53,6 +53,12 @@ namespace ANSCENTER {
int GetEmbeddingSize() const { return FACE_EMBEDDING_SIZE; }
std::shared_ptr<faiss::gpu::StandardGpuResources> GetGpuResources() const { return m_gpuResources; }
// Which embedding model to load (call BEFORE Initialize). Default is
// ADAFACE; Initialize falls back to ARCFACE when the model pack has
// no adafacerecognizer.onnx (pre-v1.4 zips).
void SetPreferredModel(FaceRecognizerModel m) { _preferredModel = m; }
FaceRecognizerModel GetActiveModel() const { return _activeModel; }
bool UpdateParamater(double knownPersonThreshold) {
_modelConfig.unknownPersonThreshold = knownPersonThreshold;
m_knownPersonThresh = _modelConfig.unknownPersonThreshold;
@@ -68,6 +74,11 @@ namespace ANSCENTER {
private:
bool LoadEngine(const std::string& xmlModelPath, bool engineOptimisation = true);
// Resolves the .onnx file for _preferredModel, falling back
// ADAFACE -> ARCFACE when adafacerecognizer.onnx is absent. Sets
// _activeModel and m_swapRB; returns "" when nothing usable exists.
std::string ResolveRecognizerModelFile();
// 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);
@@ -127,6 +138,13 @@ namespace ANSCENTER {
float m_knownPersonThresh = 0.35f;
EngineType engineType;
FaceRecognizerModel _preferredModel = FaceRecognizerModel::ADAFACE;
FaceRecognizerModel _activeModel = FaceRecognizerModel::ADAFACE;
// ArcFace was trained on RGB (needs the BGR->RGB swap); AdaFace on
// BGR (OpenCV native order, no swap). Gates every cvtColor in the
// TensorRT preprocessing paths.
bool m_swapRB = true;
#ifdef USE_ONNX_ENGINE
std::unique_ptr<GlintArcFace> faceRecognizer = nullptr;
const int CPU_FACE_WIDTH = 112;
+1
View File
@@ -4,6 +4,7 @@ set(ANSFR_SOURCES
ANSFR.cpp
ANSFRCommon.cpp
ANSFaceRecognizer.cpp
ONNXFaceAttributes.cpp
ANSGpuFrameRegistry.cpp
GpuNV12SlotPool.cpp
FaceDatabase.cpp
+79
View File
@@ -265,6 +265,85 @@ namespace ANSCENTER {
}
}
int FaceDatabase::GetAllFaces(std::vector<FaceRecord>& faces) {
std::lock_guard<std::mutex> lock(_mutex);
faces.clear();
sqlite3_stmt* stmt = nullptr;
int rc = sqlite3_prepare_v2(m_db, "SELECT ID, USER, IMG_PATH FROM FACE;", -1, &stmt, NULL);
if (rc != SQLITE_OK) {
_logger.LogError("FaceDatabase::GetAllFaces", sqlite3_errmsg(m_db), __FILE__, __LINE__);
return -1;
}
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
FaceRecord record;
record.FaceId = sqlite3_column_int(stmt, 0);
record.UserId = sqlite3_column_int(stmt, 1);
const unsigned char* path = sqlite3_column_text(stmt, 2);
record.ImagePath = path ? reinterpret_cast<const char*>(path) : "";
faces.push_back(std::move(record));
}
sqlite3_finalize(stmt);
return 0;
}
int FaceDatabase::UpdateFaceEmbedding(int faceId, const float embedding[]) {
std::lock_guard<std::mutex> lock(_mutex);
sqlite3_stmt* stmt = nullptr;
int rc = sqlite3_prepare_v2(m_db, "UPDATE FACE SET EMBEDDING=? WHERE ID=?;", -1, &stmt, NULL);
if (rc != SQLITE_OK) {
_logger.LogError("FaceDatabase::UpdateFaceEmbedding", sqlite3_errmsg(m_db), __FILE__, __LINE__);
return -1;
}
int resultCode = 1;
if (sqlite3_bind_blob(stmt, 1, embedding, sizeof(float) * m_embedding_dim, SQLITE_STATIC) != SQLITE_OK)
resultCode = -2;
if (sqlite3_bind_int(stmt, 2, faceId) != SQLITE_OK)
resultCode = -2;
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
_logger.LogError("FaceDatabase::UpdateFaceEmbedding", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -3;
}
sqlite3_finalize(stmt);
return resultCode;
}
bool FaceDatabase::SetSetting(const std::string& key, const std::string& value) {
std::lock_guard<std::mutex> lock(_mutex);
// Older databases predate the SETTINGS table — create on demand.
sqlite3_exec(m_db,
"CREATE TABLE IF NOT EXISTS SETTINGS (KEY TEXT PRIMARY KEY, VALUE TEXT);",
nullptr, nullptr, nullptr);
sqlite3_stmt* stmt = nullptr;
int rc = sqlite3_prepare_v2(m_db,
"INSERT INTO SETTINGS (KEY, VALUE) VALUES (?, ?) "
"ON CONFLICT(KEY) DO UPDATE SET VALUE=excluded.VALUE;",
-1, &stmt, NULL);
if (rc != SQLITE_OK) {
_logger.LogError("FaceDatabase::SetSetting", sqlite3_errmsg(m_db), __FILE__, __LINE__);
return false;
}
sqlite3_bind_text(stmt, 1, key.c_str(), int(key.length()), SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, value.c_str(), int(value.length()), SQLITE_STATIC);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return (rc == SQLITE_ROW || rc == SQLITE_DONE);
}
std::string FaceDatabase::GetSetting(const std::string& key) {
std::lock_guard<std::mutex> lock(_mutex);
sqlite3_stmt* stmt = nullptr;
int rc = sqlite3_prepare_v2(m_db, "SELECT VALUE FROM SETTINGS WHERE KEY=?;", -1, &stmt, NULL);
if (rc != SQLITE_OK) {
// Missing table (legacy database) — treat as "no value".
return std::string();
}
std::string value;
sqlite3_bind_text(stmt, 1, key.c_str(), int(key.length()), SQLITE_STATIC);
if (sqlite3_step(stmt) == SQLITE_ROW) {
const unsigned char* v = sqlite3_column_text(stmt, 0);
if (v) value = reinterpret_cast<const char*>(v);
}
sqlite3_finalize(stmt);
return value;
}
int FaceDatabase::DeleteFace(int id) {
try {
// Fix #4: Single lock scope — GetFaceById + DELETE in one atomic section
+9
View File
@@ -35,6 +35,15 @@ namespace ANSCENTER {
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);
// All enrolled faces (ID, USER, IMG_PATH) — used to regenerate
// embeddings after a recognizer model switch.
int GetAllFaces(std::vector<FaceRecord>& faces);
// Replaces the stored embedding of one face (same dim as Initialize).
int UpdateFaceEmbedding(int faceId, const float embedding[]);
// Simple key/value store (SETTINGS table, created on demand) — used
// to persist which recognizer model generated the stored embeddings.
bool SetSetting(const std::string& key, const std::string& value);
std::string GetSetting(const std::string& key);
std::map<std::string, std::string> GetUserDict();
int GetEmbeddings(ANSFRBase& recognizer);
int GetEmbeddings(std::unique_ptr<ANSFRBase>& recognizer);
+256
View File
@@ -0,0 +1,256 @@
#include "ONNXFaceAttributes.h"
#include <algorithm>
#include <cmath>
#include <fstream>
#include <filesystem>
namespace {
// The exported classifier may or may not end with a softmax node
// (Ultralytics kept flipping this across versions). Treat the output
// as probabilities only when it already looks like a distribution.
void SoftmaxIfNeeded(std::vector<float>& v) {
if (v.empty()) return;
float sum = 0.0f;
bool inRange = true;
for (float x : v) {
if (x < 0.0f || x > 1.0f) { inRange = false; break; }
sum += x;
}
if (inRange && std::fabs(sum - 1.0f) < 0.05f) return;
float maxVal = *std::max_element(v.begin(), v.end());
float denom = 0.0f;
for (float& x : v) {
x = std::exp(x - maxVal);
denom += x;
}
if (denom > 0.0f) {
for (float& x : v) x /= denom;
}
}
void ResolveInputSize(const std::vector<int64_t>& dims, int& width, int& height) {
// NCHW; dynamic dims come back as -1 — the shipped models are 224x224.
width = 224;
height = 224;
if (dims.size() >= 4) {
if (dims[2] > 0) height = static_cast<int>(dims[2]);
if (dims[3] > 0) width = static_cast<int>(dims[3]);
}
}
}
namespace ANSCENTER {
// ================================================================
// OnnxAgeGenderEstimator
// ================================================================
OnnxAgeGenderEstimator::OnnxAgeGenderEstimator(const std::string& onnxPath,
EngineType engineType,
const OrtHandlerOptions& options,
unsigned int numThreads)
: BasicOrtHandler(onnxPath, engineType, options, numThreads)
{
_ready = true;
}
Ort::Value OnnxAgeGenderEstimator::transform(const cv::Mat& mat)
{
if (mat.empty())
throw std::runtime_error("OnnxAgeGenderEstimator::transform — input is empty.");
int width = 0, height = 0;
ResolveInputSize(input_node_dims, width, height);
cv::Mat canvas = mat;
if (canvas.channels() == 1)
cv::cvtColor(canvas, canvas, cv::COLOR_GRAY2BGR);
cv::Mat resized;
cv::resize(canvas, resized, cv::Size(width, height), 0, 0, cv::INTER_LINEAR);
cv::cvtColor(resized, resized, cv::COLOR_BGR2RGB);
resized.convertTo(resized, CV_32FC3, 1.0 / 255.0);
// ImageNet normalization (ViTFeatureExtractor defaults)
static const float mean_vals[3] = { 0.485f, 0.456f, 0.406f };
static const float scale_vals[3] = { 1.f / 0.229f, 1.f / 0.224f, 1.f / 0.225f };
utils::transform::normalize_inplace(resized, mean_vals, scale_vals);
std::vector<int64_t> shape = input_node_dims;
if (shape.size() < 4) shape = { 1, 3, height, width };
if (shape[0] <= 0) shape[0] = 1;
if (shape[2] <= 0) shape[2] = height;
if (shape[3] <= 0) shape[3] = width;
return utils::transform::create_tensor(
resized, shape, *memory_info_handler,
input_values_handler, utils::transform::CHW);
}
Ort::Value OnnxAgeGenderEstimator::transformBatch(const std::vector<cv::Mat>&)
{
throw std::runtime_error("OnnxAgeGenderEstimator::transformBatch — not supported.");
}
AgeGenderResults OnnxAgeGenderEstimator::runInfer(const cv::Mat& frame)
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
if (!_ready || frame.empty()) return { -1, -1 };
Ort::Value input_tensor = transform(frame);
DmlInferenceGuard dmlGuard(m_engineType, "OnnxAgeGenderEstimator::runInfer");
auto output_tensors = ort_session->Run(
Ort::RunOptions{ nullptr },
input_node_names.data(), &input_tensor, 1,
output_node_names.data(), num_outputs);
// Two known export layouts, distinguished by tensor sizes:
// (a) v1.4 pack: age_pred [1] (years) + gender_logits [1,2]
// with class order [male, female]
// (b) research export: a single [1,2] tensor of
// [age_years, gender_logit] where sigmoid >= 0.5 -> Female
const float* agePtr = nullptr;
const float* genderLogits = nullptr;
for (auto& tensor : output_tensors) {
const size_t count =
tensor.GetTensorTypeAndShapeInfo().GetElementCount();
if (count == 1 && !agePtr)
agePtr = tensor.GetTensorMutableData<float>();
else if (count >= 2 && !genderLogits)
genderLogits = tensor.GetTensorMutableData<float>();
}
if (agePtr && genderLogits) { // layout (a)
const float age = std::clamp(agePtr[0], 0.0f, 100.0f);
const float m = genderLogits[0], f = genderLogits[1];
const float mx = std::max(m, f);
const float em = std::exp(m - mx), ef = std::exp(f - mx);
return { age, em / (em + ef) };
}
if (genderLogits) { // layout (b)
const float age = std::clamp(genderLogits[0], 0.0f, 100.0f);
const float femaleProb = 1.0f / (1.0f + std::exp(-genderLogits[1]));
return { age, 1.0f - femaleProb };
}
return { -1, -1 };
}
catch (const std::exception& error) {
std::cerr << "OnnxAgeGenderEstimator error: " << error.what() << std::endl;
return { -1, -1 };
}
}
// ================================================================
// OnnxEmotionsClassifier
// ================================================================
std::vector<std::string> OnnxEmotionsClassifier::LoadClassNames(const std::string& namesFilePath)
{
std::vector<std::string> names;
try {
std::ifstream file(namesFilePath);
std::string line;
while (std::getline(file, line)) {
while (!line.empty() && (line.back() == '\r' || line.back() == '\n'
|| line.back() == ' ' || line.back() == '\t'))
line.pop_back();
if (!line.empty()) names.push_back(line);
}
}
catch (const std::exception&) {
names.clear();
}
if (names.empty()) {
names = { "neutral", "happy", "sad", "surprise", "anger" };
}
return names;
}
OnnxEmotionsClassifier::OnnxEmotionsClassifier(const std::string& onnxPath,
std::vector<std::string> classNames,
EngineType engineType,
const OrtHandlerOptions& options,
unsigned int numThreads)
: BasicOrtHandler(onnxPath, engineType, options, numThreads)
, _classNames(std::move(classNames))
{
if (_classNames.empty())
_classNames = { "neutral", "happy", "sad", "surprise", "anger" };
_ready = true;
}
Ort::Value OnnxEmotionsClassifier::transform(const cv::Mat& mat)
{
if (mat.empty())
throw std::runtime_error("OnnxEmotionsClassifier::transform — input is empty.");
int width = 0, height = 0;
ResolveInputSize(input_node_dims, width, height);
cv::Mat canvas = mat;
if (canvas.channels() == 1)
cv::cvtColor(canvas, canvas, cv::COLOR_GRAY2BGR);
cv::Mat resized;
cv::resize(canvas, resized, cv::Size(width, height), 0, 0, cv::INTER_LINEAR);
cv::cvtColor(resized, resized, cv::COLOR_BGR2RGB);
// Ultralytics classifier preprocessing: RGB, scaled to [0, 1]
resized.convertTo(resized, CV_32FC3, 1.0 / 255.0);
std::vector<int64_t> shape = input_node_dims;
if (shape.size() < 4) shape = { 1, 3, height, width };
if (shape[0] <= 0) shape[0] = 1;
if (shape[2] <= 0) shape[2] = height;
if (shape[3] <= 0) shape[3] = width;
return utils::transform::create_tensor(
resized, shape, *memory_info_handler,
input_values_handler, utils::transform::CHW);
}
Ort::Value OnnxEmotionsClassifier::transformBatch(const std::vector<cv::Mat>&)
{
throw std::runtime_error("OnnxEmotionsClassifier::transformBatch — not supported.");
}
std::map<std::string, float> OnnxEmotionsClassifier::runInfer(const cv::Mat& frame)
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
if (!_ready || frame.empty()) return { };
Ort::Value input_tensor = transform(frame);
DmlInferenceGuard dmlGuard(m_engineType, "OnnxEmotionsClassifier::runInfer");
auto output_tensors = ort_session->Run(
Ort::RunOptions{ nullptr },
input_node_names.data(), &input_tensor, 1,
output_node_names.data(), num_outputs);
auto info = output_tensors.at(0).GetTensorTypeAndShapeInfo();
const size_t count = info.GetElementCount();
if (count == 0) return { };
const float* vals = output_tensors.at(0).GetTensorMutableData<float>();
std::vector<float> scores(vals, vals + count);
SoftmaxIfNeeded(scores);
std::map<std::string, float> emotions;
for (size_t i = 0; i < scores.size(); ++i) {
const std::string name = (i < _classNames.size())
? _classNames[i]
: ("class_" + std::to_string(i));
emotions[name] = scores[i];
}
return emotions;
}
catch (const std::exception& error) {
std::cerr << "OnnxEmotionsClassifier error: " << error.what() << std::endl;
return { };
}
}
}
+69
View File
@@ -0,0 +1,69 @@
#ifndef ONNXFACEATTRIBUTES_H
#define ONNXFACEATTRIBUTES_H
#pragma once
// ONNX Runtime backends for the face attribute models shipped with
// ANS_FaceRecognizer v1.4:
// - agegender.onnx ViT-Base age/gender head (224x224, ImageNet norm,
// 2 logits: [age 0..100, gender sigmoid -> Female >= 0.5])
// - emotionsrecognition.onnx YOLOv11 classifier (class list in
// emotionsrecognition.names)
//
// Execution provider follows the detected hardware through BasicOrtHandler:
// NVIDIA -> TensorRT EP (disk-cached engines) with CUDA EP fallback,
// AMD -> DirectML, Intel -> OpenVINO, otherwise CPU.
#include "ANSFRCommon.h"
#include "ONNXEngine.h"
#include <mutex>
namespace ANSCENTER {
class OnnxAgeGenderEstimator : public BasicOrtHandler, public IAgeGenderEstimator {
public:
OnnxAgeGenderEstimator(const std::string& onnxPath,
EngineType engineType,
const OrtHandlerOptions& options,
unsigned int numThreads = 2);
~OnnxAgeGenderEstimator() override = default;
AgeGenderResults runInfer(const cv::Mat& frame) override;
bool enabled() const override { return _ready; }
const char* backendName() const override { return "onnx"; }
private:
Ort::Value transform(const cv::Mat& mat) override;
Ort::Value transformBatch(const std::vector<cv::Mat>& images) override;
std::recursive_mutex _mutex;
bool _ready{ false };
};
class OnnxEmotionsClassifier : public BasicOrtHandler, public IEmotionsClassifier {
public:
OnnxEmotionsClassifier(const std::string& onnxPath,
std::vector<std::string> classNames,
EngineType engineType,
const OrtHandlerOptions& options,
unsigned int numThreads = 2);
~OnnxEmotionsClassifier() override = default;
std::map<std::string, float> runInfer(const cv::Mat& frame) override;
bool enabled() const override { return _ready; }
const char* backendName() const override { return "onnx"; }
// Reads one class name per line; returns the legacy 5-emotion list
// when the file is missing/empty so the classifier still produces
// labelled output.
static std::vector<std::string> LoadClassNames(const std::string& namesFilePath);
private:
Ort::Value transform(const cv::Mat& mat) override;
Ort::Value transformBatch(const std::vector<cv::Mat>& images) override;
std::recursive_mutex _mutex;
std::vector<std::string> _classNames;
bool _ready{ false };
};
}
#endif
+50
View File
@@ -887,6 +887,8 @@ extern "C" ANSFR_API int InsertFaces(ANSCENTER::ANSFacialRecognition**
extern "C" ANSFR_API int CheckFaceEmbedding(ANSCENTER::ANSFacialRecognition** Handle, unsigned char* jpeg_string, unsigned int bufferLength) {
ANS_DBG("ANSFR","CheckFaceEmbedding: HandlePtr=%p, *Handle=%p, bufLen=%u",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), bufferLength);
//return 1; // by pass
try {
if (!Handle || !*Handle || !jpeg_string || bufferLength == 0) return -1;
cv::Mat frame = cv::imdecode(cv::Mat(1, bufferLength, CV_8UC1, jpeg_string), cv::IMREAD_COLOR);
@@ -1292,6 +1294,54 @@ extern "C" ANSFR_API int GetFaceQueue(ANSCENTER::ANSFacialRecognition** Handl
}
}
extern "C" ANSFR_API int SetANSRFRecognizerModel(ANSCENTER::ANSFacialRecognition** Handle, int modelType) {
ANS_DBG("ANSFR","SetANSRFRecognizerModel: HandlePtr=%p, *Handle=%p, modelType=%d (0=AdaFace,1=ArcFace)",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), modelType);
try {
if (!Handle || !*Handle) return -1;
if (modelType < 0 || modelType > 1) return -1;
FRHandleGuard guard(AcquireFRHandle(*Handle));
if (!guard) return -1;
return (*Handle)->SetRecognizerModelType(modelType) ? 1 : 0;
}
catch (const std::exception& e) {
ANS_DBG("ANSFR","SetANSRFRecognizerModel EXCEPTION: %s", e.what());
return -1;
}
}
extern "C" ANSFR_API int GetANSRFRecognizerModel(ANSCENTER::ANSFacialRecognition** Handle) {
try {
if (!Handle || !*Handle) return -1;
return (*Handle)->GetRecognizerModelType();
}
catch (const std::exception&) {
return -1;
}
}
extern "C" ANSFR_API int GetANSRFModelInfo(ANSCENTER::ANSFacialRecognition** Handle, LStrHandle modelInfo) {
try {
if (!Handle || !*Handle || !modelInfo) return -1;
return CopyToLStrHandle(modelInfo, (*Handle)->GetActiveModelInfo());
}
catch (const std::exception&) {
return -1;
}
}
extern "C" ANSFR_API int SetANSRFAttributeInterval(ANSCENTER::ANSFacialRecognition** Handle, int frames) {
ANS_DBG("ANSFR","SetANSRFAttributeInterval: HandlePtr=%p, *Handle=%p, frames=%d",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), frames);
try {
if (!Handle || !*Handle) return -1;
return (*Handle)->UpdateAttributeInterval(frames) ? 1 : -1;
}
catch (const std::exception&) {
return -1;
}
}
extern "C" ANSFR_API int RunInferenceComplete_LV(
ANSCENTER::ANSFacialRecognition** Handle,
cv::Mat** cvImage,
@@ -162,7 +162,7 @@ bool PaddleOCRV5Engine::Initialize(const std::string& detModelPath,
// Ensure this DLL's copy of Ort::Global<void>::api_ is initialized.
// BasicOrtHandler sets it in ONNXEngine.dll, but each DLL has its own
// inline-static copy. Without this, inference calls from ANSOCR.dll crash.
if (Ort::Global<void>::api_ == nullptr) {
if (Ort::detail::Global::Api() == nullptr) {
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
}
+72
View File
@@ -20,6 +20,7 @@
#include <chrono>
#include "ANSMOT.h"
#include "onnxruntime_cxx_api.h"
#include "ANSModelSource.h"
#define USEONNXOV
#define RETAINDETECTEDFRAMES 80
@@ -754,6 +755,11 @@ namespace ANSCENTER
virtual void AddEmbedding(const std::string& className, const std::vector<float>& embedding) =0;
virtual bool Destroy()=0;
virtual void SetMaxSlotsPerGpu(int n) {} // override in TRT-based subclasses
// True once Initialize() fully succeeded (license + model + engine).
// Callers that compute embeddings (enrollment) must check this first:
// Feature() returns an empty vector when uninitialized, which is
// indistinguishable from a bad face crop at the call site.
bool IsInitialized() const { return _isInitialized; }
~ANSFRBase();
// Utility functions
@@ -761,6 +767,16 @@ namespace ANSCENTER
float CosineSimilarity(const std::vector<float>& a, const std::vector<float>& b, bool normalized);
};
class ANSENGINE_API ANSODBase {
public:
// Explicit non-copy/non-move: _modelLoading (std::atomic) deletes the
// implicit copy ops; spelling them out here gives ClangCL a clean
// anchor and documents that subclasses are never copied/moved (they
// are heap-allocated and passed by shared_ptr<...>).
ANSODBase(const ANSODBase&) = delete;
ANSODBase& operator=(const ANSODBase&) = delete;
ANSODBase(ANSODBase&&) = delete;
ANSODBase& operator=(ANSODBase&&) = delete;
ANSODBase() = default;
protected:
bool _licenseValid{ false };
bool _isInitialized{ false };
@@ -782,6 +798,55 @@ namespace ANSCENTER
void CheckLicense();
void LoadClassesFromString();
void LoadClassesFromFile();
// ────────────────────────────────────────────────────────────────
// A model that arrived as an .ansmodel package.
//
// A package replaces the whole inflated folder: the model, the
// classes and the geometry model_config.json used to carry all
// come out of it, and nothing is written to disk. LoadModelPackage
// fills these in; the engine then builds its session from
// _packageModel instead of from _modelFilePath.
//
// The bytes stay for as long as the engine object does, because
// the ONNX handler holds a view of them and rebuilds its session
// from that view when a GPU driver fault forces a recreate.
// WipeModelPackage() clears them on teardown.
// ────────────────────────────────────────────────────────────────
bool _fromPackage{ false };
std::vector<unsigned char> _packageModel;
std::string _packageTask; // detection, classification, …
std::string _packageFamily; // yolo | yolo10 | rfdetr
std::string _packageMarker; // what the zip would have carried
std::string _packageName; // what logs call this model
int _packageInputWidth{ 0 }; // 0: the package does not say
int _packageInputHeight{ 0 };
// Where this engine's session should read its model from: the
// package's bytes when one was opened, the file otherwise. Engines
// keep their own _modelFilePath, so it is passed in.
ModelSource CurrentModelSource(const std::string& filePath) const {
if (_fromPackage && !_packageModel.empty())
return ModelSource::Memory(_packageModel.data(),
_packageModel.size(), _packageName);
return ModelSource::File(filePath);
}
// Engines reset the input geometry to their own default after the
// base class has loaded, which would throw away a package's own
// (a classification model is 224, not 640). This puts it back.
void ApplyPackageInputSize() {
if (_packageInputWidth > 0) _modelConfig.inpWidth = _packageInputWidth;
if (_packageInputHeight > 0) _modelConfig.inpHeight = _packageInputHeight;
}
static bool IsModelPackagePath(const std::string& path);
// Opens one, decrypts the model into _packageModel, and fills
// _classes and _modelConfig's geometry from the manifest. Returns
// false with the reason logged.
bool LoadModelPackage(const std::string& path);
void WipeModelPackage();
std::recursive_mutex _mutex;
// Guard: true while Initialize/LoadModel is in progress on any thread.
// Inference callers can check this to fail-fast instead of blocking.
@@ -1085,6 +1150,13 @@ namespace ANSCENTER
std::vector<Object> RunInferenceInTrackingMode(const cv::Mat& input, const std::string& camera_id, std::vector<int> trackingObjectIds);// Get detected objects
};
class ANSENGINE_API ANSFDBase {
public:
// Explicit non-copy/non-move (see ANSODBase for rationale).
ANSFDBase(const ANSFDBase&) = delete;
ANSFDBase& operator=(const ANSFDBase&) = delete;
ANSFDBase(ANSFDBase&&) = delete;
ANSFDBase& operator=(ANSFDBase&&) = delete;
ANSFDBase() = default;
protected:
bool _licenseValid{ false };
bool _isInitialized{ false };
@@ -1316,7 +1316,7 @@ namespace ANSCENTER
{
try {
const auto& ep = ANSCENTER::EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr)
if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(ANSCENTER::EPLoader::GetOrtApiRaw()));
std::cout << "[ANSFDBase] EP ready: "<< ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl;
+76
View File
@@ -0,0 +1,76 @@
#ifndef ANSMODELSOURCE_H
#define ANSMODELSOURCE_H
#pragma once
// ANSModelSource — where a model's bytes come from: a file, or memory.
//
// Every engine here built its session from a file path, which is why loading
// an encrypted model package means inflating it to a directory first: the
// unprotected model lands on disk, is optimised, and is zipped back into a
// cache. Handing the runtime a buffer instead removes both the round trip and
// the plaintext on disk, and it is the one change the new .ansmodel package
// format cannot work without.
//
// A model in memory still needs two things a path gave for free. `name` is
// what logs call it, since there is no file name to fall back on. And
// ONNX Runtime copies what it needs out of the buffer while the session is
// being built, so the caller may wipe its plaintext the moment MakeSession
// returns — which is exactly what a package reader should do.
#include <string>
#include <onnxruntime_cxx_api.h>
namespace ANSCENTER {
struct ModelSource {
std::string path; // file on disk; empty for a model already in memory
std::string name; // what to call it in logs
const void* data = nullptr;
size_t size = 0;
bool inMemory() const { return data != nullptr && size > 0; }
// Named factories, never implicit conversions: an engine that offers
// both Init(std::string) and Init(ModelSource) must not become
// ambiguous for a caller passing a plain string literal.
static ModelSource File(std::string p) {
ModelSource s;
s.name = p;
s.path = std::move(p);
return s;
}
static ModelSource Memory(const void* d, size_t n, std::string name) {
ModelSource s;
s.data = d;
s.size = n;
s.name = std::move(name);
return s;
}
};
// What to call this model in a log line.
inline std::string ModelSourceName(const ModelSource& src) {
if (!src.name.empty()) return src.name;
if (!src.path.empty()) return src.path;
return "<memory>";
}
// Build a session from wherever the bytes are. One place, so the Windows
// wide-path conversion and the from-memory call are not repeated in every
// engine — they were already duplicated eleven times.
inline Ort::Session MakeSession(Ort::Env& env, const ModelSource& src,
const Ort::SessionOptions& options) {
if (src.inMemory())
return Ort::Session(env, src.data, src.size, options);
#ifdef _WIN32
// ORT's Windows API takes a wide (ORTCHAR_T = wchar_t) path.
const std::wstring wide(src.path.begin(), src.path.end());
return Ort::Session(env, wide.c_str(), options);
#else
return Ort::Session(env, src.path.c_str(), options);
#endif
}
} // namespace ANSCENTER
#endif // ANSMODELSOURCE_H
+105
View File
@@ -1,9 +1,11 @@
#pragma once
#include <set>
#include <cstring>
#include <cmath>
#include <json.hpp>
#include "ANSODEngine.h"
#include "ANSLicense.h" // ANS_DBG macro
#include "ANSModelOpen.h"
#include "OpenVINODeviceConfig.h"
#include "ANSYOLOOD.h"
#include "ANSTENSORRTOD.h"
@@ -61,6 +63,93 @@ namespace ANSCENTER
this->_logger.LogFatal("ANSODBase::CheckLicense. Error:", e.what(), __FILE__, __LINE__);
}
}
// ────────────────────────────────────────────────────────────────────────
// Model packages (.ansmodel)
//
// A package carries what the zip's loose files carried — the model, the
// class names, the task and the input geometry — sealed to a key only
// ANSCORE, ANSMINDCORE and the engines hold. Opening one fills in the
// same members the inflated folder used to fill in, with one difference
// that is the point of the whole format: the model stays in memory.
// ────────────────────────────────────────────────────────────────────────
bool ANSODBase::IsModelPackagePath(const std::string& path) {
return ANSCENTER::modelpkg::IsPackagePath(path);
}
void ANSODBase::WipeModelPackage() {
if (!_packageModel.empty()) {
std::memset(_packageModel.data(), 0, _packageModel.size());
_packageModel.clear();
_packageModel.shrink_to_fit();
}
_fromPackage = false;
_packageInputWidth = 0;
_packageInputHeight = 0;
}
bool ANSODBase::LoadModelPackage(const std::string& path) {
try {
WipeModelPackage();
ANSCENTER::modelpkg::OpenedModel opened;
std::string err;
if (!ANSCENTER::modelpkg::OpenModelPackage(path, opened, err)) {
this->_logger.LogError("ANSODBase::LoadModelPackage", err, __FILE__, __LINE__);
return false;
}
if (!opened.warning.empty()) {
// An expired release certificate warns and loads, by design:
// a model that worked yesterday keeps working on a box that
// has been offline for a year.
this->_logger.LogWarn("ANSODBase::LoadModelPackage", opened.warning, __FILE__, __LINE__);
}
_packageModel = std::move(opened.model);
if (_packageModel.empty()) {
this->_logger.LogError("ANSODBase::LoadModelPackage",
"The package carries no model", __FILE__, __LINE__);
return false;
}
_fromPackage = true;
_packageTask = opened.task;
_packageFamily = opened.family;
_packageMarker = opened.legacyMarker;
_packageName = opened.name;
// The classes come out in order, so there is nothing to parse and
// no classes.names to look for.
if (!opened.classes.empty()) _classes = opened.classes;
_packageInputWidth = opened.inputWidth;
_packageInputHeight = opened.inputHeight;
ApplyPackageInputSize();
// The manifest says in a word what the zip said by the presence of
// a marker file.
if (opened.task == "classification") _modelConfig.detectionType = ANSCENTER::DetectionType::CLASSIFICATION;
else if (opened.task == "segmentation") _modelConfig.detectionType = ANSCENTER::DetectionType::SEGMENTATION;
else if (opened.task == "obb") _modelConfig.detectionType = ANSCENTER::DetectionType::OBB;
else if (opened.task == "pose") _modelConfig.detectionType = ANSCENTER::DetectionType::KEYPOINT;
else _modelConfig.detectionType = ANSCENTER::DetectionType::DETECTION;
// There is no folder and no config file; every engine below tests
// for these, and must find them empty rather than stale.
_modelFolder.clear();
_modelConfigFile.clear();
_classFilePath.clear();
this->_logger.LogDebug("ANSODBase::LoadModelPackage",
"Opened " + path + " (" + opened.task + "/" + opened.family + ", "
+ std::to_string(_classes.size()) + " classes, "
+ std::to_string(_packageModel.size()) + " bytes)", __FILE__, __LINE__);
// Auto-enable tracker + stabilization by default (BYTETRACK)
SetTracker(TrackerType::BYTETRACK, true);
return true;
}
catch (std::exception& e) {
this->_logger.LogFatal("ANSODBase::LoadModelPackage", e.what(), __FILE__, __LINE__);
return false;
}
}
bool ANSODBase::LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword)
{
try {
@@ -69,6 +158,7 @@ namespace ANSCENTER
_modelConfigFile = "";
_modelFolder.clear();
_modelConfigFile.clear();
WipeModelPackage(); // whatever was loaded before, package or not
// 0. Check if the modelZipFilePath exist?
if (!FileExist(modelZipFilePath)) {
if (modelZipFilePath.empty()) {// Support motion detector that does not need model file
@@ -82,6 +172,10 @@ namespace ANSCENTER
return false;
}
}
// 0b. A model package replaces the zip entirely: it is opened in
// memory, and no password, no folder and no loose files are involved.
if (IsModelPackagePath(modelZipFilePath))
return LoadModelPackage(modelZipFilePath);
// 1. Unzip model zip file to a special location with folder name as model file (and version)
std::string outputFolder;
std::vector<std::string> passwordArray;
@@ -126,6 +220,7 @@ namespace ANSCENTER
_modelFolder.clear();
_modelConfigFile.clear();
_modelConfig = modelConfig;
WipeModelPackage(); // whatever was loaded before, package or not
CheckLicense();
if (!_licenseValid) {
this->_logger.LogError("ANSODBase::Initialize", "Invalid License", __FILE__, __LINE__);
@@ -145,6 +240,12 @@ namespace ANSCENTER
}
}
// 0b. A model package replaces the zip entirely; see LoadModel.
if (IsModelPackagePath(modelZipFilePath)) {
if (!LoadModelPackage(modelZipFilePath)) return false;
_isInitialized = true;
return true;
}
// 1. Unzip model zip file to a special location with folder name as model file (and version)
std::string outputFolder;
std::vector<std::string> passwordArray;
@@ -952,6 +1053,10 @@ namespace ANSCENTER
ANSODBase::~ANSODBase() {
try {
// A decrypted model is what the package format exists to protect.
// This runs after the derived destructor has dropped the session,
// so nothing is left holding a view of these bytes.
WipeModelPackage();
// Per-camera trackers are released by CameraData::clear()
if (!_cameras.empty())
{
+8 -8
View File
@@ -55,13 +55,18 @@ namespace ANSCENTER
return std::accumulate(vector.begin(), vector.end(), 1LL, std::multiplies<int64_t>());
}
bool ANSONNXCL::Init(const std::string& modelPath, const cv::Size& targetInputShape, bool useGPU)
{
return Init(ModelSource::File(modelPath), targetInputShape, useGPU);
}
bool ANSONNXCL::Init(const ModelSource& model, const cv::Size& targetInputShape, bool useGPU)
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
inputImageShape_ = targetInputShape;
const auto& ep = ANSCENTER::EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr)
if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
std::cout << "[ANSONNXCL] EP ready: "
<< ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl;
@@ -198,12 +203,7 @@ namespace ANSCENTER
}
// ── Load model ──────────────────────────────────────────────────────
#ifdef _WIN32
std::wstring w_modelPath = std::wstring(modelPath.begin(), modelPath.end());
session_ = Ort::Session(env_, w_modelPath.c_str(), sessionOptions_);
#else
session_ = Ort::Session(env_, modelPath.c_str(), sessionOptions_);
#endif
session_ = MakeSession(env_, model, sessionOptions_);
Ort::AllocatorWithDefaultOptions allocator;
@@ -300,7 +300,7 @@ namespace ANSCENTER
std::cout << "Warning: No class names loaded. Predictions will use numeric IDs." << std::endl;
}
std::cout << "[ANSONNXCL] Initialized successfully. Model: " << modelPath << std::endl;
std::cout << "[ANSONNXCL] Initialized successfully. Model: " << ModelSourceName(model) << std::endl;
// ── Warmup ──────────────────────────────────────────────────────────
DEBUG_PRINT("Starting model warmup...");
+10
View File
@@ -2,6 +2,7 @@
#define ANSONNXCL_H
#pragma once
#include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include "engine.h"
#include <memory>
@@ -25,6 +26,13 @@ namespace ANSCENTER {
// ────────────────────────────────────────────────────────────────────────
class ANSENGINE_API ANSONNXCL :public ANSODBase {
public:
// Explicit non-copy/non-move (see ANSONNXYOLO for dllexport rationale).
ANSONNXCL(const ANSONNXCL&) = delete;
ANSONNXCL& operator=(const ANSONNXCL&) = delete;
ANSONNXCL(ANSONNXCL&&) = delete;
ANSONNXCL& operator=(ANSONNXCL&&) = delete;
ANSONNXCL() = default;
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;
virtual bool LoadModelFromFolder(std::string licenseKey, ModelConfig modelConfig, std::string modelName, std::string className, const std::string& modelFolder, std::string& labelMap)override;
@@ -58,6 +66,8 @@ namespace ANSCENTER {
const std::string& strategy = "resize");
void warmupModel();
bool Init(const std::string& modelPath, const cv::Size& targetInputShape, bool useGPU = true);
// Same load, from a model already in memory (an opened package).
bool Init(const ModelSource& model, const cv::Size& targetInputShape, bool useGPU = true);
bool preprocess(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape);
// outputTensors is non-const because GetTensorMutableData() (the
// ORT API that doesn't hang on AMD DirectML) requires a non-const
+31 -7
View File
@@ -686,13 +686,18 @@ namespace ANSCENTER {
}
}
bool ANSONNXOBB::Init(const std::string& modelPath, bool useGPU, int deviceId)
{
return Init(ModelSource::File(modelPath), useGPU, deviceId);
}
bool ANSONNXOBB::Init(const ModelSource& model, bool useGPU, int deviceId)
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
deviceId_ = deviceId;
const auto& ep = ANSCENTER::EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr)
if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
std::cout << "[ANSONNXOBB] EP ready: "
<< ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl;
@@ -834,12 +839,7 @@ namespace ANSCENTER {
}
// ── Load model ──────────────────────────────────────────────────────
#ifdef _WIN32
std::wstring w_modelPath = std::wstring(modelPath.begin(), modelPath.end());
session = Ort::Session(env, w_modelPath.c_str(), sessionOptions);
#else
session = Ort::Session(env, modelPath.c_str(), sessionOptions);
#endif
session = MakeSession(env, model, sessionOptions);
Ort::AllocatorWithDefaultOptions allocator;
@@ -1394,6 +1394,19 @@ namespace ANSCENTER {
if (_modelConfig.kpsThreshold == 0)_modelConfig.kpsThreshold = 0.5; // If not define
_fp16 = (modelConfig.precisionType == PrecisionType::FP16);
// A package brought its own classes and geometry, and its model never
// touched the disk: the session reads it straight from memory.
if (_fromPackage) {
ApplyPackageInputSize();
labelMap.clear();
if (!_classes.empty()) labelMap = VectorToCommaSeparatedString(_classes);
instanceId_ = instanceCounter_.fetch_add(1);
result = Init(CurrentModelSource(_modelFilePath), true, 0);
_modelLoadValid = result;
_isInitialized = result;
return result;
}
if (FileExist(_modelConfigFile)) {
ModelType modelType;
std::vector<int> inputShape;
@@ -1454,6 +1467,17 @@ namespace ANSCENTER {
// if (_modelConfig.precisionType == PrecisionType::FP16)_fp16 = true;
_fp16 = true; // Load Model from Here
// A package brought its own classes and geometry, and its model never
// touched the disk: the session reads it straight from memory.
if (_fromPackage) {
ApplyPackageInputSize();
instanceId_ = instanceCounter_.fetch_add(1);
result = Init(CurrentModelSource(_modelFilePath), true, 0);
_modelLoadValid = result;
_isInitialized = result;
return result;
}
// 0. Check if the configuration file exist
if (FileExist(_modelConfigFile)) {
ModelType modelType;
+3
View File
@@ -2,6 +2,7 @@
#define ANSONNXOBB_H
#pragma once
#include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include "engine.h"
namespace ANSCENTER {
@@ -73,6 +74,8 @@ namespace ANSCENTER {
void warmupModel();
bool Init(const std::string& modelPath, bool useGPU=true, int deviceId = 0);
// Same load, from a model already in memory (an opened package).
bool Init(const ModelSource& model, bool useGPU = true, int deviceId = 0);
cv::Mat preprocess(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape);
// outputTensors is non-const because GetTensorMutableData() requires
// a non-const Ort::Value receiver — see ANSONNXCL.h for full note.
+31 -7
View File
@@ -270,13 +270,18 @@ namespace ANSCENTER {
}
bool ANSONNXPOSE::Init(const std::string& modelPath, bool useGPU, int deviceId)
{
return Init(ModelSource::File(modelPath), useGPU, deviceId);
}
bool ANSONNXPOSE::Init(const ModelSource& model, bool useGPU, int deviceId)
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
deviceId_ = deviceId;
const auto& ep = ANSCENTER::EPLoader::Current();
if (Ort::Global<void>::api_ == nullptr)
if (Ort::detail::Global::Api() == nullptr)
Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
std::cout << "[ANSONNXPOSE] EP ready: "
<< ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl;
@@ -418,12 +423,7 @@ namespace ANSCENTER {
}
// ── Load model ──────────────────────────────────────────────────────
#ifdef _WIN32
std::wstring w_modelPath = std::wstring(modelPath.begin(), modelPath.end());
session = Ort::Session(env, w_modelPath.c_str(), sessionOptions);
#else
session = Ort::Session(env, modelPath.c_str(), sessionOptions);
#endif
session = MakeSession(env, model, sessionOptions);
Ort::AllocatorWithDefaultOptions allocator;
@@ -1282,6 +1282,19 @@ namespace ANSCENTER {
if (_modelConfig.kpsThreshold == 0)_modelConfig.kpsThreshold = 0.5; // If not define
_fp16 = (modelConfig.precisionType == PrecisionType::FP16);
// A package brought its own classes and geometry, and its model never
// touched the disk: the session reads it straight from memory.
if (_fromPackage) {
ApplyPackageInputSize();
labelMap.clear();
if (!_classes.empty()) labelMap = VectorToCommaSeparatedString(_classes);
instanceId_ = instanceCounter_.fetch_add(1);
result = Init(CurrentModelSource(_modelFilePath), true, 0);
_modelLoadValid = result;
_isInitialized = result;
return result;
}
if (FileExist(_modelConfigFile)) {
ModelType modelType;
std::vector<int> inputShape;
@@ -1343,6 +1356,17 @@ namespace ANSCENTER {
// if (_modelConfig.precisionType == PrecisionType::FP16)_fp16 = true;
_fp16 = true; // Load Model from Here
// A package brought its own classes and geometry, and its model never
// touched the disk: the session reads it straight from memory.
if (_fromPackage) {
ApplyPackageInputSize();
instanceId_ = instanceCounter_.fetch_add(1);
result = Init(CurrentModelSource(_modelFilePath), true, 0);
_modelLoadValid = result;
_isInitialized = result;
return result;
}
// 0. Check if the configuration file exist
if (FileExist(_modelConfigFile)) {
ModelType modelType;
+3
View File
@@ -2,6 +2,7 @@
#define ANSONNXPOSE_H
#pragma once
#include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include "engine.h"
namespace ANSCENTER {
@@ -40,6 +41,8 @@ namespace ANSCENTER {
float kptThreshold = 0.5);
void warmupModel();
bool Init(const std::string& modelPath, bool useGPU=true, int deviceId = 0);
// Same load, from a model already in memory (an opened package).
bool Init(const ModelSource& model, bool useGPU = true, int deviceId = 0);
cv::Mat preprocess(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape);
// outputTensors is non-const because GetTensorMutableData() requires
// a non-const Ort::Value receiver — see ANSONNXCL.h for full note.
+7
View File
@@ -108,6 +108,13 @@ namespace ANSCENTER {
// ====================================================================
class ANSENGINE_API ANSONNXRTMPOSE : public ANSODBase {
public:
// Explicit non-copy/non-move (see ANSONNXYOLO for dllexport rationale).
ANSONNXRTMPOSE(const ANSONNXRTMPOSE&) = delete;
ANSONNXRTMPOSE& operator=(const ANSONNXRTMPOSE&) = delete;
ANSONNXRTMPOSE(ANSONNXRTMPOSE&&) = delete;
ANSONNXRTMPOSE& operator=(ANSONNXRTMPOSE&&) = delete;
ANSONNXRTMPOSE() = default;
bool Initialize(std::string licenseKey, ModelConfig modelConfig,
const std::string& modelZipFilePath,
const std::string& modelZipPassword,

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