Author SHA1 Message Date
nghianguyenandClaude Opus 5.5 0baca1b20d feat(ANSODEngine): run Darknet/YOLO exports ("darknet" family) in ONNXYOLO and RTYOLO
A Darknet/YOLO model exported by hank-ai's darknet_onnx_export (v5.1+) has
two outputs: confs [B,N,nc], already objectness x class probability, and
boxes [B,N,1,4], corners x1,y1,x2,y2 normalized to the input. It trains and
predicts on a stretched square and has no NMS in the graph. ONNXYOLO took
the 4-D boxes tensor for segmentation proto masks and letterboxed the input,
so such a model decoded as garbage; a sealed package named as YOLOV4 went to
YOLOOD, which cannot open an .ansmodel at all.

- ONNXYOLO / RTYOLO: recognise the layout once at load (package family
  "darknet", output names confs/boxes, or their shapes), before the seg
  check; stretch-resize the input (single, batch, and RTYOLO's NV12 path);
  new postprocessDarknet: best class per candidate, threshold, stretch map
  to the original image, clip, class-aware NMS.
- dllmain: a sealed package passed as modelType 1 (YOLOV4) is routed as
  detection (3), i.e. to RTYOLO on NVIDIA and ONNXYOLO elsewhere, in
  CreateANSODHandle, CreateANSODHandleEx and OptimizeModelStr.
- Every other model is unchanged: all new branches are gated on the layout.

Verified on macOS only: ANSONNXYOLO.cpp passes a syntax check, and the new
decode functions, extracted into a standalone harness running the model
through onnxruntime, reproduce Darknet's own detections on dog.jpg. NOT yet
built on Windows, and the TensorRT path has not run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 18:43:13 +10:00
nghianguyenandClaude Opus 5 cb964907f0 feat(ANSLS): know ANSMIND as 1009, and stop filing unknown products under it
The gateway's product table already runs to 1009 ANSMIND and 1010 ANSGW.
The licensing library stopped at 1008, so a real ANSMIND licence could not
be named, deactivated or listed here, while every product name it did not
recognise quietly became product 1009. With 1009 now a real product, that
fallback would mint an ANSMIND trial for a typo.

ANSPRODUCTS gains ANSMIND = 1009 and ANSGW = 1010, and both are added
wherever the product table is spelled out: ActivateTrialLicense,
DeactivateLicense, ActivateLicense, ActivateLicenseWithCustomHWID and
ListLicenses, which now reports an ANSMIND and an ANSGW row like the
gateway does.

Unknown names no longer map to any id. ActivateTrialLicense returns 0, its
existing failure value, instead of generating a key. ListLicenses skips
the row. ActivateLicense and ActivateLicenseWithCustomHWID keep passing
the name through as the registration name: they never assigned an id, the
key's own validation data rejects a name it was not issued for, and the
gateway resolves an unknown product the same way.

The ANSVIS master-licence rule in LicenseVerification is untouched.

Separately, the fallback loops in ANSUtilities, ANSAWSS3 and ANSLLM asked
for product 1008 under the registration name "ANSSRT". The product is
ANSTS, so a genuine ANSTS licence could never satisfy them. Only that
string changes; the loops still stop at 1008.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 10:38:31 +10:00
nghianguyenandClaude Opus 5 cd99fcf6d9 feat(ANSODEngine): run a sealed model on TensorRT without unsealing it
A .ansmodel reaching the GPU path went to ModelOptimizer, which knows one
way to open a model — unzip it — and failed on the first byte. Even past
that, the TensorRT engine only ever parsed an ONNX file, so the one thing
the format exists to prevent, a decrypted model on disk, was the only way
it could have run.

The builder now takes its bytes from memory when it has them. The path it
is given still names the plan that gets cached beside it, because a plan
is a built artefact and carries no model: the second run loads it and
never needs the ONNX at all. For a package the plan lands in the folder
the zip flow used, so a model converted from a zip reuses the plan that
zip already built.

OptimizeModelStr recognises a package and skips the unzip-and-optimise
leg entirely: the handle below it opens the package in memory and builds
the plan from the decrypted bytes, which is the same work by a shorter
road.

Still on the file path: the other TensorRT handlers (segmentation, pose,
classification). Only the YOLO detector reads a package today.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 09:38:45 +10:00
nghianguyenandClaude Opus 5 1e369fdfef test(ANSODEngine): let the harness run a model this machine actually has
Every test in that file names a model and a video from whichever
workstation it was written on, so anywhere else the harness prints
"Model zip file is not exist" and exits before it reaches the GPU. That
is what happened on the machine that is meant to be testing CUDA and
TensorRT.

Given a model and a video on the command line it runs those instead,
with an optional engine type and detection type. With no arguments it
behaves exactly as before, so nobody's workflow changes.

The model may be a .zip or a sealed .ansmodel, because the engine now
opens either — and that is the only way to watch a sealed model run on
a GPU, which is the last thing in this format that has never been seen
working end to end.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 08:28:50 +10:00
nghianguyenandClaude Opus 5 b69a0fbcba fix(build): find nlohmann/json where the machine actually keeps it
The package library's Windows branch pointed at vcpkg alone. On a real
Windows build machine the header lives in the ANSLibs tree, so anything
linking this library failed with "Cannot open include file:
'nlohmann/json.hpp'" while the engines compiled fine — they add ANSLibs
to their own include path and never noticed.

Now it is searched for by name, with the ANSLibs tree, vcpkg and the
usual system paths as hints, and the configure says where it found it.
One code path for every platform instead of an assumption per platform.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 08:00:51 +10:00
nghianguyenandClaude Opus 5 e76d3b81e2 fix(cuda): spell the fast-math flag the way nvcc spells it
Every .cu file failed on CUDA 13 with "nvcc fatal: Unknown option
'--use-fast-math'". The flag is --use_fast_math, with underscores; older
toolkits accepted the hyphenated form as well and this one does not, so
the engine layer could not compile a single kernel on a current CUDA.

Found on the Windows machine with CUDA 13.0, where it stopped the build
at the first two kernels.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 07:58:49 +10:00
nghianguyenandClaude Opus 5 eb84d48e5c fix(build): do not demand a policy older CMake has never heard of
Line two set CMP0167 unconditionally. That policy arrived in CMake 3.30
and this project asks for 3.28, so every CMake between the two failed
the configure at line two with "Policy CMP0167 is not known to this
version" — before it had done anything at all. Found on the Windows
machine that builds the CUDA engines, where it stopped the build dead.

Guarded, so a newer CMake still gets the quiet FindBoost the policy was
set for and an older one configures instead of refusing.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 07:46:43 +10:00
nghianguyenandClaude Opus 5 3798e3513b fix(YOLO12OD): decode an end-to-end head instead of reading it as a grid
This engine took the feature count from outputShape[1] unconditionally.
For a yolo26 export with end2end on — [1, 300, 6], rows of
(x1 y1 x2 y2 score class) with the suppression already inside the graph
— that means 296 classes, coordinates read as class scores, and boxes
covering the frame. No error, just nonsense, which is exactly what the
Labelling Tool produced from the same model before its decoder was
fixed.

Its sibling ANSONNXYOLO has recognised that head from the tensor shape
for a while; this one had not, and the backend marker cannot tell them
apart because it names the runtime rather than the head.

The branch fires only where the tensor cannot be a grid: the 6 must be
the last axis with a detection count before it, hundreds rather than the
thousands of anchors a two-class [1, 6, 8400] export carries.

The decoder reuses this engine's own scaleCoords rather than repeating
the letterbox arithmetic, so both heads undo the padding the same way
and agree about where a box is, and the rounding into the integral box
type is spelled out instead of left to a narrowing conversion.

NOT COMPILED. The engine layer needs CUDA and the ANSLibs tree, neither
of which exists on this machine, so this is verified by reading: the
shape rule matches the one the other three products now share, every
symbol it uses is declared in this translation unit, and the arithmetic
is delegated to the function the grid path already trusts.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 06:55:45 +10:00
nghianguyenandClaude Opus 5 ea877a61bc fix(keys): decide at configure time whether this build has the key
Whether the model-package key is present was an __has_include test
inside the header, and that test leaves no dependency for the build
system. A key emitted after a build had already compiled the file would
not take effect until someone cleaned, and the resulting engine looks
exactly like one built without a key — which on a release machine means
shipping an engine that silently cannot open a package.

CMake now decides it and says which case it is, so emitting the key and
re-configuring is what installs it, visibly.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 05:56:58 +10:00
nghianguyenandClaude Opus 5 3c9e0b79f5 fix(ANSModelPackage): read the head off the graph, not off the package's word
A package whose family says "yolo" may hold an end-to-end export. Boxes
took the family from the backend marker, and the marker names the
runtime, not the head — so a fast yolo26 export arrived declared as
plain yolo while its graph emits [1, N, 6] with the NMS inside it.
Decoding that as a [1, 4+nc, N] grid reads coordinates as confidences:
detections everywhere, and nothing that looks like a failure.

OpenModelPackage now reads the first output's dims from the model it has
just decrypted, and corrects the family with the reason in the warning
rather than swapping it silently. It costs nothing there: the bytes are
in hand. The legacy marker is left alone, so anything converting back to
a zip still writes what ANSCORE expects.

A two-class v8 export is [1, 6, 8400] and must not be mistaken for one,
which is why the 6 has to be the last axis with a detection count before
it. Both cases are tested, and the package this Mac exported before the
writer was fixed now reads as yolo10 here too. 46 checks, no failures.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 00:19:15 +10:00
nghianguyenandClaude Opus 5 b1196ee8ba chore: ignore the in-tree build/ directory
Only cmake-build-*/ and out/ were ignored, so configuring into build/ —
the directory the ANSCORE_DEBUGVIEW_STRIP usage note in CMakeLists.txt
shows — left an untracked build/ in git status. build-mac/ is untracked
as well and stays that way; this pattern matches build/ alone.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 23:39:56 +10:00
nghianguyenandClaude Opus 5 6abe6d6694 fix(build): configure without CUDA instead of failing at generate
tests/CMakeLists.txt added every test directory unconditionally, so a
machine with no CUDA toolkit — every macOS one — died at generate time on
"links to CUDA::cudart but the target was not found", taking the
cross-platform targets down with it.

Gate each test the way its subject is already gated: the five that need
the toolkit behind ANSCORE_HAS_CUDA, the ANSIO one behind WIN32, and the
ANSONVIF one behind both. ANSONVIF itself uses no CUDA; its test names
CUDA::cudart and CUDA::nvjpeg only because it drives capture through
ANSCV and was copied from the ANSCV test.

Two non-test targets had the same problem. ANSCV genuinely needs the
toolkit — ANSGpuFrameOps.h includes <cuda_runtime.h> unconditionally and
calls cudaMemcpy2D/cudaMallocPitch — so it moves into the existing CUDA
block beside the engine modules. ANSLibsLoader binds cudart at runtime
and ships its own stubs, but still compiles against the runtime header
for the types and links CUDA::cudart_static, so it is gated too.

Also call enable_testing(), which the project never did: without it the
add_test() under tests/ reached nothing and ctest found no tests. Plain
enable_testing(), not include(CTest), to avoid its extra targets.

A CUDA machine is unchanged: every gate holds there, ANSCV keeps its
place in the subdirectory order, and the only new output is
CTestTestfile.cmake.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 23:38:57 +10:00
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
nghianguyen 214f598e18 Support Generate Presigned URL for S3 2026-05-19 10:37:46 +10:00
nghianguyen 26f9b07ac1 Add adaptive padding to improve tracking 2026-05-14 22:01:07 +10:00
nghianguyen a43a99834a Complete RTMPOSE 2026-05-14 21:16:35 +10:00
nghianguyen 71404affb7 Correct logic in RTMPOSE when using generic model to detect person 2026-05-14 19:33:31 +10:00
nghianguyen 52ec13a8e3 improve pose 2026-05-14 19:17:09 +10:00
nghianguyen 8e4d9a9e5d Add person model into RTMPOSE 2026-05-14 19:05:20 +10:00
nghianguyen ee8f0d1163 Suppot RTMPOSE classes 2026-05-14 17:48:09 +10:00
nghianguyen eb1244c936 Support get global IP address 2026-05-09 21:55:01 +10:00
nghianguyen cffdeb223b Update get info 2026-05-09 20:57:13 +10:00
nghianguyen a94bf88a5b Fix other ANSCV classes 2026-05-09 16:47:46 +10:00
nghianguyen aa323a07a3 Fix debug 2026-05-07 23:35:18 +10:00
nghianguyen 75693cadd9 Add jitter buffer 2026-05-07 22:36:27 +10:00
nghianguyen 2e0fee5e3d Fix VideoPlayer from stop after just few frames 2026-05-07 18:17:33 +10:00
nghianguyen a526aa6cdb Fix ALPR order issue 2026-05-06 19:55:52 +10:00
nghianguyen 66c67dff26 Fix onnx runtime loader for custom model 2026-05-05 16:23:01 +10:00
nghianguyen b0002b4a4b Update ALPR developer guide 2026-05-05 04:41:33 +10:00
nghianguyen d03c2c1138 disable ALPR checker by default 2026-05-05 04:37:23 +10:00
nghianguyen 91d81cbd88 fix onnx yolo race condition 2026-05-04 23:45:05 +10:00
nghianguyen 03a63142ec Disable stabilized feature by default. Need ,more time to test 2026-05-04 09:39:07 +10:00
nghianguyen fa6d95de82 Update test app 2026-05-03 16:30:39 +10:00
nghianguyen 4214507e3b Test ALPR model (v1.3) 2026-05-03 07:16:32 +10:00
nghianguyen cff2a90fad Support multiple GPUs 2026-05-02 12:24:53 +10:00
nghianguyen 8359511895 Update unit test 2026-05-01 20:33:09 +10:00
nghianguyen fc9cf9671b Fix ANSONNXYOLO:
1. core/ANSLicensingSystem/ANSLicense.h:268 — added default initializers to ModelConfig POD members (inpWidth = 0, inpHeight = 0, etc.). This was the actual bug: CreateANSODHandle declares a local ModelConfig and only sets a few fields, leaving inpWidth/inpHeight as indeterminate stack memory. On your machine that memory consistently held ~80, slipping past the <=0 → 640 guard and driving the ORT session at 80×80 — which crashes YOLOv11's /model.12/Concat because 2·ceil(80/32)=6 ≠ ceil(80/16)=5.

2. modules/ANSODEngine/ANSONNXYOLO.h:75 — ONNXYOLO::setInputShape() now snaps width/height up to the next multiple of 32 (with a 64-pixel floor). Defense in depth: even if a future caller hands in a non-stride-32 size, the FPN concat alignment is preserved.

3. modules/ANSODEngine/ANSONNXYOLO.cpp — two diagnostic additions:

WarmUpEngine bails with a clear log line if the warm-up dims aren't a multiple of 32, instead of letting the model.12 error fire and mask the real cause.
transform() logs the input shape, canvas shape, source size, and dynamic flag once per session in DebugView — so any future shape-mismatch crash points at the offending HxW directly.
2026-05-01 20:24:08 +10:00
nghianguyen 1c8abf2a88 Fix AWS MinIO 2026-05-01 11:16:47 +10:00
nghianguyen c4ec6f0ed2 Add AMD 16FP conversion plan 2026-05-01 10:09:24 +10:00
nghianguyen 91efbb9a8f Fix AMD ALPR_OCR and custom model 2026-05-01 07:53:18 +10:00
nghianguyen e1f37ec767 Add ANSALPR country format 2026-04-28 19:23:46 +10:00
nghianguyen cd5d6f1923 Remove spaces, ".", and "-" from ALPR 2026-04-28 17:49:37 +10:00
nghianguyen 234f2c68a2 bucket prewarm + batch padding 2026-04-28 17:12:27 +10:00
nghianguyen 4bab357c72 Stress test on multiple streams 2026-04-28 13:49:57 +10:00
nghianguyen dcf974c35c Fix AMD by changing from GetTensorData<T>() to GetTensorMutableData<T>() 2026-04-28 13:25:02 +10:00
nghianguyen f4b74c837e protect sensitive information from logs 2026-04-26 07:53:37 +10:00
nghianguyen da94e7f6da strip sensitive information from logs 2026-04-26 07:35:30 +10:00
nghianguyen f81737ffc6 Add debug and log functions to ANSLIB 2026-04-26 06:59:42 +10:00
nghianguyen 947398483a Unit test before memory leak check 2026-04-26 06:14:44 +10:00
nghianguyen bab94bf9bc Add log and change logger policy 2026-04-25 20:29:58 +10:00
nghianguyen ef2a122fec Add logs to custom models and model optimisation 2026-04-25 13:17:36 +10:00
201 changed files with 32474 additions and 2305 deletions
+1
View File
@@ -0,0 +1 @@
{"sessionId":"b4ba6a5c-5267-4098-8e7c-152012d3df22","pid":5176,"acquiredAt":1776991716766}
-112
View File
@@ -1,112 +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)"
]
}
}
Submodule .claude/worktrees/heuristic-lichterman-bda22b added at 66c67dff26
Submodule .claude/worktrees/lucid-grothendieck-201ca6 added at a526aa6cdb
+7
View File
@@ -6,6 +6,7 @@ Release/
out/ out/
# CLion / CMake # CLion / CMake
build/
.idea/ .idea/
cmake-build-debug/ cmake-build-debug/
cmake-build-release/ cmake-build-release/
@@ -42,3 +43,9 @@ ipch/
# Windows # Windows
Thumbs.db 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
+452
View File
@@ -0,0 +1,452 @@
# AMD DirectML FP16 Converter — Implementation Plan
**Status:** Not started. Pick this up in a future session.
**Estimated effort:** ~1 week of focused work.
**Owner of context:** This document is self-contained — you don't need the
prior conversation to execute it.
---
## 1. Goal
Recover ~30–50% of per-call inference latency on AMD Radeon iGPUs (DML EP)
by running models in FP16 instead of FP32. Pure C++ implementation —
no Python tooling at any stage. Mirror the existing TensorRT pattern
where `OrtHandlerOptions::trtFP16 = true` causes the engine layer to
build/cache an FP16 plan automatically at session-create time.
The current AMD performance baseline (after the always-serial-on-AMD fix)
is ~5.4 fps for full-frame ALPR. Target after FP16: 7–8 fps.
## 2. Why C++ (and not the standard Python toolchain)
The standard tool for this is `onnxconverter-common.float16.convert_float_to_float16`.
Project rule disallows Python anywhere in the workflow, so we must
re-implement the algorithm in C++ inside ANSCORE.
The DirectML EP itself has no `dml_fp16_enable` runtime knob (unlike TRT's
`trt_fp16_enable`). DML runs whatever precision the ONNX model declares.
So the C++ work has to produce a transformed FP16 ONNX, then hand that
to the DML EP.
## 3. Architecture (mirrors the TRT pattern)
```
TRT path (existing):
trtFP16 = true → ORT TRT EP options → TRT engine builder rewrites
→ engine cached on disk → inference
DML path (new):
dmlFP16 = true → ANSCORE C++ rewrite → cached as <hash>.fp16.onnx
→ DML EP loads cached FP16 model → inference
```
Cache invalidation, per-model granularity, and the "first run is slow"
semantics all match TRT. From the call-site perspective the TRT and DML
options are symmetric.
## 4. Current state of the codebase (relevant files)
These files already exist and are the integration points. Don't recreate
them; modify them.
| File | Relevant content | What this plan adds |
|---|---|---|
| `engines/ONNXEngine/ONNXEngine.h` | `OrtHandlerOptions` struct. AMD section commented "OPEN FOR OPTIMIZATION" — empty today | Add `dmlFP16`, `dmlOptimizedModelDir`, `dmlFP16OpBlockList` fields |
| `engines/ONNXEngine/ONNXEngine.cpp` | `BasicOrtHandler::initialize_handler()` — DML EP branch (search for `EngineType::AMD_GPU`) | Insert path-resolution call before session creation |
| `engines/ONNXEngine/ONNXEngine.cpp` | Already houses `DmlSerialState` + `DmlInferenceGuard` (the always-serial-on-AMD fix from previous work) | Reuse the same TU for FP16 helpers |
| `engines/ONNXEngine/Fp16GraphRewriter.{h,cpp}` | **Does not exist yet** | Create — main implementation of this plan |
| `modules/ANSOCR/ANSONNXOCR/PaddleOCRV5Engine.cpp` | `BuildXxxOcrOptions()` helpers populating per-model `OrtHandlerOptions` | Add `o.dmlFP16 = true/false` per model |
| Caller code in `ANSONNXYOLO`, `ONNXOCRDetector`, `ONNXOCRRecognizer` | Already constructs sessions via `BasicOrtHandler` | No changes — the FP16 swap is invisible to callers |
## 5. Step-by-step plan
### Step 1 — Add AMD knobs to `OrtHandlerOptions` (half day)
In `engines/ONNXEngine/ONNXEngine.h`, replace the AMD placeholder:
```cpp
// ----------------------------------------------------------------
// AMD (DirectML EP) — FP16 graph rewrite
// ----------------------------------------------------------------
// Convert FP32 weights to FP16 before session creation. Mirrors
// trtFP16 semantically: an in-process graph rewrite produces an FP16
// ONNX, cached on disk under dmlOptimizedModelDir, and the DML EP
// loads the cached FP16 model. External inputs/outputs stay FP32 via
// Cast nodes inserted at the boundaries — caller code is unchanged.
// Ignored on every non-DML EP. Default off until per-model accuracy
// validation completes.
bool dmlFP16 = false;
// Where to cache rewritten FP16 ONNX models. Empty → default
// %TEMP%/ANSCENTER/DMLOptimizedCache. Cache key is a hash of
// (source ONNX bytes + dmlFP16 flag + opBlockList). Stale entries
// are silently replaced when source changes.
std::string dmlOptimizedModelDir;
// Op types whose outputs MUST remain FP32 (NMS, TopK, RoiAlign, plus
// any op observed to produce NaN/Inf in FP16 for THIS model). Empty
// = use the built-in default block list (see kDefaultFp16BlockList in
// Fp16GraphRewriter.cpp). Per-model override.
std::vector<std::string> dmlFP16OpBlockList;
```
### Step 2 — Add ONNX protobuf to the build (half day)
You need the `onnx.proto3` schema compiled to C++ via `protoc`. Two
sub-options:
**Option 2A (recommended): vendor the proto file.**
1. Download `onnx.proto3` from https://github.com/onnx/onnx (current
release tag). Put it at `engines/ONNXEngine/proto/onnx.proto3`.
2. Add a CMake `add_custom_command` calling `protoc --cpp_out=...` at
configure time, generating `onnx.pb.{h,cc}` into the build directory.
3. Link `Fp16GraphRewriter` against `protobuf::libprotobuf`.
`libprotobuf` is almost certainly already a transitive dep of
`onnxruntime` — confirm by checking
`C:\ANSLibs\onnxruntime\include` for `google/protobuf/...` headers and
`C:\ANSLibs\protobuf` (or vcpkg's protobuf install). Just expose the
include directories to your new target.
**Option 2B: vendor the full ONNX C++ library.**
Bigger dep but provides typed helpers, validators, and a checker. Skip
unless 2A's hand-rolled approach blows up on edge cases.
### Step 3 — Implement `Fp16GraphRewriter` (~2 days for core)
Create `engines/ONNXEngine/Fp16GraphRewriter.h`:
```cpp
#pragma once
#include <string>
#include <vector>
#include <unordered_set>
namespace ANSCENTER {
// Returns the path to an FP16 version of fp32Path. If cacheDir already
// contains a fresh FP16 cache for this model, returns the cached path
// directly (fast path). Otherwise loads fp32Path, runs the in-memory
// FP16 graph rewrite, writes to cacheDir, and returns the new path.
//
// Cache filename embeds a hash of (fp32 file content + opBlockList) so
// a model update or blocklist change automatically invalidates the
// cache — same pattern as TRT engine cache invalidation.
//
// On any failure (parse error, write error, etc.) returns fp32Path
// unchanged and logs a warning via ANS_DBG. The session creation
// continues with the FP32 model — this function NEVER blocks the
// inference path.
std::string ResolveFp16OnnxPath(
const std::string& fp32Path,
const std::string& cacheDir,
const std::vector<std::string>& opBlockList);
} // namespace ANSCENTER
```
Create `engines/ONNXEngine/Fp16GraphRewriter.cpp`. Key components:
#### 3a. IEEE-754 binary32 → binary16 helper
Standard 30-line implementation, round-to-nearest-even, handle NaN/Inf.
Reference: numpy's `numpy/core/src/npymath/halffloat.c` or
`Eigen/src/Core/arch/Default/Half.h`. Self-contained, no deps.
#### 3b. Default op blocklist
```cpp
static const std::unordered_set<std::string> kDefaultFp16BlockList = {
// Object-detection post-processing — known FP16-unsafe
"NonMaxSuppression", "TopK", "RoiAlign",
// Classical ML ops — never have FP16 implementations
"ArrayFeatureExtractor", "Binarizer", "CastMap",
"CategoryMapper", "DictVectorizer", "FeatureVectorizer",
"Imputer", "LabelEncoder", "LinearClassifier",
"LinearRegressor", "Normalizer", "OneHotEncoder",
"RandomUniform", "SVMClassifier", "SVMRegressor",
"Scaler", "TreeEnsembleClassifier", "TreeEnsembleRegressor",
"ZipMap",
};
```
#### 3c. Initializer conversion
Walk `model.graph().initializer()`. For every `TensorProto` with
`data_type == FLOAT`:
1. Pull the float data (it lives in `raw_data` OR `float_data` —
handle both).
2. Convert each float32 to uint16 binary16.
3. Set `data_type = FLOAT16`, clear `float_data`, write the new bytes
to `raw_data`.
Watch for: external data references (when `data_location ==
EXTERNAL`) — for the YOLO and OCR models in this project, weights are
embedded, but write a guard: if external, skip with a warning.
#### 3d. Cast-node insertion at boundaries
For each external input (entries in `model.graph().input()` that are
NOT initializer names):
1. Note the input's tensor name `orig`.
2. Append a new `NodeProto` to the graph: `Cast(input=orig,
output=orig + "_fp16", to=FLOAT16)`.
3. Walk every existing node in the graph; for any input that equals
`orig`, replace it with `orig + "_fp16"`. Be careful not to rewire
the new Cast node itself (skip the last node added).
For each external output: insert `Cast(FP16 → FP32)` symmetrically.
#### 3e. value_info type updates
For every `ValueInfoProto` in `graph().value_info()`:
- If the tensor is the input to OR output of a block-listed op, leave
its `tensor_type.elem_type` as FP32.
- Otherwise update to FP16.
This step is the most error-prone in practice. When in doubt, leave
elem_type alone — the runtime will accept inferred types. Test
correctness empirically with the validation harness in Step 7.
#### 3f. Subgraphs
If/Loop/Scan ops have nested subgraphs (`AttributeProto` of type
GRAPH). Recurse into each. The OCR recognizer doesn't have these;
neither does YOLO post-Ultralytics-export — but write the recursion
defensively, otherwise this fails silently on future models.
#### 3g. Cache management
Cache filename: `<model_basename>.<sha1_8>.fp16.onnx`, where `sha1_8` is
the first 8 hex chars of `SHA1(fp32_file_bytes || serialized
opBlockList || "v1")`. The "v1" string lets you bust the cache for
all models if you fix a converter bug later.
Cache directory: create on demand with `std::filesystem::create_directories`.
Default: `std::filesystem::temp_directory_path() / "ANSCENTER" /
"DMLOptimizedCache"`.
Lookup: if cache file exists AND its mtime is newer than the source
ONNX, use it. Else regenerate.
### Step 4 — Wire into `initialize_handler` (half day)
In `engines/ONNXEngine/ONNXEngine.cpp`, find the DML branch in
`BasicOrtHandler::initialize_handler()`. Insert path resolution before
session creation:
```cpp
case EngineType::AMD_GPU: {
std::string onnxToLoad = _onnx_path;
if (options.dmlFP16) {
const std::string cacheDir =
options.dmlOptimizedModelDir.empty()
? GetDefaultDmlCacheDir()
: options.dmlOptimizedModelDir;
onnxToLoad = ResolveFp16OnnxPath(
_onnx_path, cacheDir, options.dmlFP16OpBlockList);
ANS_DBG("OrtHandler",
"AMD FP16: source=%s cached=%s",
_onnx_path.c_str(), onnxToLoad.c_str());
}
if (!TryAppendDirectML(opts)) { /* existing fallback */ }
ort_session = std::make_unique<Ort::Session>(
env, onnxToLoad.c_str(), opts);
break;
}
```
`GetDefaultDmlCacheDir()` is a small helper in `Fp16GraphRewriter.cpp`.
### Step 5 — Per-model configuration (1 day)
Update the option-builder functions to opt models into FP16:
`modules/ANSOCR/ANSONNXOCR/PaddleOCRV5Engine.cpp` (and equivalents):
```cpp
OrtHandlerOptions BuildLpDetectorOptions() {
OrtHandlerOptions o;
o.preferTensorRT = true;
o.trtFP16 = true;
o.dmlFP16 = true; // YOLO det validated FP16-safe
return o;
}
OrtHandlerOptions BuildOcrDetectorOptions() {
OrtHandlerOptions o;
o.preferTensorRT = true;
o.trtFP16 = true;
o.dmlFP16 = true; // DBNet text-det validated
return o;
}
OrtHandlerOptions BuildOcrRecognizerOptions() {
OrtHandlerOptions o;
o.preferTensorRT = true;
o.trtFP16 = true;
// OCR recognizer is FP16-sensitive on small text — keep AMD FP32
// until per-plate accuracy validation passes (see Step 7).
o.dmlFP16 = false;
return o;
}
```
Helmet OD (in `ANSCUSTOMDetector` model loading) — set `dmlFP16 = true`.
### Step 6 — DebugView logging (per `MEMORY.md` rule)
Sprinkle `ANS_DBG` markers at every decision/error point in
`Fp16GraphRewriter.cpp`:
- Entry: `ANS_DBG("Fp16Rewriter", "ResolveFp16OnnxPath: src=%s cache=%s",
fp32Path.c_str(), cacheDir.c_str());`
- Cache hit: `ANS_DBG("Fp16Rewriter", "cache HIT %s", cachedPath.c_str());`
- Cache miss → rewrite: `ANS_DBG("Fp16Rewriter",
"cache MISS — rewriting (initializers=%zu nodes=%zu)", ...);`
- Per phase: log initializer-conversion count, Cast-insertion count,
value_info update count.
- On error: `ANS_DBG("Fp16Rewriter",
"ERROR: %s — falling back to FP32 source", ex.what());` and return
the original FP32 path (never block inference).
### Step 7 — Per-model accuracy validation (1–2 days)
You can't ship FP16 without per-model accuracy validation. Build a
small C++ harness — separate executable target, runs against
labeled fixtures from your test data:
```
tools/AccuracyComparator/AccuracyComparator.cpp
- Loads two sessions (FP32 and FP16) on CPU EP for determinism.
- Feeds N representative frames through both.
- For YOLO det: compare box IoU, score deltas, count of boxes lost.
- For OCR det: compare text-region masks (pixel-wise IoU on the prob
map binary at threshold).
- For OCR rec: compare DECODED CTC strings — not raw logits.
A single character delta on a plate is a regression.
- Emit pass/fail against a configurable tolerance.
```
Acceptance criteria:
| Model | Tolerance | What "pass" means |
|---|---|---|
| YOLO LP detector | mean IoU ≥ 0.95 vs FP32 boxes | No boxes lost, score delta < 0.05 |
| YOLO Helmet OD | mean IoU ≥ 0.95 | Same |
| OCR DBNet | mask IoU ≥ 0.92 at threshold | No new false negatives |
| OCR CTC recognizer | char accuracy within 0.5% | If fails, leave AMD FP32 |
Run on at least 500 frames per model from production data.
### Step 8 — Build, deploy, measure (half day)
Build artifacts: `ANSODEngine.dll`, `ANSOCR.dll`. No new DLL — the
rewriter lives inside the existing engine layer.
Deploy to AMD test box. First run: each session takes an extra
2–10 s during initial cache build. Look for `[Fp16Rewriter] cache
MISS — rewriting` lines.
Subsequent runs: instant cache hit. Look for `[Fp16Rewriter] cache HIT`.
Measure:
- Per-call DML latency in `STATS` lines (mean, p99).
- Total fps over a 60-second run.
- Compare to the always-serial-on-AMD baseline (5.4 fps full-frame ALPR).
- Target: ≥ 7 fps.
If a model regresses accuracy in the field (rare with the validation
harness in Step 7, but possible), flip its `dmlFP16` back to `false`
and ship a hotfix. No code change needed beyond the option flag.
## 6. Dependencies summary
| Dep | Where it lives | New or existing |
|---|---|---|
| `libprotobuf` (C++ runtime) | Almost certainly already vendored via onnxruntime; otherwise add via vcpkg or `C:\ANSLibs\protobuf` | Existing, just expose includes |
| `onnx.proto3` schema file | Vendor at `engines/ONNXEngine/proto/onnx.proto3` from ONNX repo | New, ~1 KB file |
| `protoc` compiler | Comes with libprotobuf install | Existing, just call from CMake |
| `<filesystem>` for cache dir | C++17 standard | Existing |
| SHA1 helper for cache key | Pull from any tiny public-domain impl, or use libcrypto if linked | Tiny new file (~80 lines) |
No third-party FP16 libraries. No Python. No new DLLs in deployment.
## 7. Risks and mitigations
| Risk | Likelihood | Mitigation |
|---|---|---|
| Conversion produces NaN/Inf in some op | Medium | Default blocklist + per-model `dmlFP16OpBlockList` override. Always validate with Step 7 harness before flipping a model on. |
| Some FP16 op has no DML kernel → falls back to CPU silently → SLOWER than FP32 | Medium | Profile each model on AMD with ORT's profiling JSON output (`SessionOptions::EnableProfiling`). If a CPU fallback shows up, blocklist the offending op for that model. |
| ONNX schema version mismatch (we vendor proto3, models use proto with newer fields) | Low | Models from this project are exported by Ultralytics + PaddleOCR — both target stable opset 12-17 ranges that proto3 covers fully. Validate by attempting parse on a known-good model during Step 3. |
| Cache directory permissions on customer machines | Low | Default to `%TEMP%` which is always writable; allow override via `dmlOptimizedModelDir`. On any cache-write failure, fall back to FP32 source path (no inference impact). |
| Subgraph (If/Loop/Scan) bug | Low for current models | Recurse defensively in Step 3f; assert if a subgraph is encountered, fail-safe to FP32. |
## 8. Out of scope (do NOT do as part of this plan)
- **Auto mixed precision** (per-op accuracy-driven precision selection).
That requires running a representative dataset through the model to
decide which ops can safely be FP16. Doable in C++ but adds 1+ week.
Defer to a follow-up plan.
- **Model quantization to INT8.** Different problem, different tooling
(calibration data needed).
- **Switching from DML to MIGraphX or ROCm.** Different EP, different
port. Defer.
- **Recovering depth=2 parallelism on AMD.** The always-serial fix is
in place and stable; this plan is purely about per-call speedup
while remaining serial. Combine the two for net throughput gain.
## 9. Project rules to follow (from MEMORY.md)
- **Edit directly in main checkout.** No worktree, no branch.
- **No commits without explicit user request.** When this plan ships,
don't auto-commit.
- **Sprinkle `ANS_DBG` on new code paths** — entry, decisions, errors.
See Step 6 for exact placement.
## 10. Acceptance checklist
When this plan is done, the following must all be true:
- [ ] `OrtHandlerOptions::dmlFP16` exists and is documented inline.
- [ ] `Fp16GraphRewriter.{h,cpp}` exists with `ResolveFp16OnnxPath` exposed.
- [ ] `BasicOrtHandler::initialize_handler` calls the resolver in the
AMD branch when `dmlFP16` is set.
- [ ] At least one production model (LP YOLO det) is opted in via its
option-builder.
- [ ] Accuracy harness in `tools/AccuracyComparator/` exists and produces
pass/fail for the opted-in models.
- [ ] AMD test-box measurement shows per-call DML latency reduced by
≥ 25% on the opted-in model vs FP32 baseline.
- [ ] No regression on Intel or NVIDIA — the FP16 path never executes
on those EPs (`if (options.dmlFP16)` is gated on `EngineType::AMD_GPU`
via the case-branch placement).
- [ ] DebugView logs cleanly: cache HIT / cache MISS / rewrite phases
/ errors all visible.
- [ ] First-run cache-build latency is ≤ 10 s per model on AMD test box.
- [ ] Subsequent-run cache-load latency is ≤ 100 ms per model.
## 11. Open questions to resolve at start
When you start this plan in a future session, answer these first:
1. Is `libprotobuf` already linked into ANSODEngine.dll (transitively
via onnxruntime)? Run `dumpbin /imports ANSODEngine.dll | grep
protobuf` to confirm. If yes, skip the vcpkg/manual install step.
2. Where does `C:\ANSLibs\onnxruntime\include` live exactly? Confirm
path for the protoc include directive.
3. Which ONNX opset version do your YOLO and OCR models target?
Run `Ort::Session` with profiling once and check the model
metadata. This confirms which `onnx.proto3` revision to vendor.
4. Is the initial AMD measurement still ~5.4 fps with the always-serial
fix in place? If something regressed in the meantime, fix that
first before adding FP16.
---
*Last updated: when this plan was authored. No code shipped yet.*
+83 -7
View File
@@ -1,16 +1,33 @@
#include "ANSLIB.h" #include "ANSLIB.h"
#include "ANSLibsLoader.h" #include "ANSLibsLoader.h"
#include <memory> #include <memory>
#include <mutex>
#define LOAD_FUNC(name) \ #define LOAD_FUNC(name) \
name##Func = (name##FuncT)GetProcAddress(dllHandle, #name); \ name##Func = (name##FuncT)GetProcAddress(dllHandle, #name); \
if (!name##Func) { success = false; } if (!name##Func) { success = false; }
namespace ANSCENTER namespace ANSCENTER
{ {
ANSLIB::ANSLIB() { namespace {
// Function-pointer typedefs for the ANSLicensingSystem C-facade exports.
// Resolved exactly once during PreloadSharedDllsOnce(); after that the
// pointers are write-once globals — concurrent reads are race-free.
using ANSLogger_LogFnT = void(*)(const char*, const char*, const char*, int);
using ANSCENTER_IsDbgEnFnT = int (*)(void);
using ANSCENTER_DebugLogFnT = void(*)(const char*);
ANSLogger_LogFnT g_ANSLogger_LogInfo = nullptr;
ANSLogger_LogFnT g_ANSLogger_LogError = nullptr;
ANSLogger_LogFnT g_ANSLogger_LogFatal = nullptr;
ANSCENTER_IsDbgEnFnT g_ANSCENTER_IsDebugViewEnabled = nullptr;
ANSCENTER_DebugLogFnT g_ANSCENTER_DebugLog = nullptr;
// Pre-load all ANSCORE DLLs from the Shared folder using full paths. // Pre-load all ANSCORE DLLs from the Shared folder using full paths.
// This ensures the correct versions are loaded regardless of PATH order // This ensures the correct versions are loaded regardless of PATH order
// (e.g., DLHUB_Runtime_Engine may contain older copies on some machines). // (e.g., DLHUB_Runtime_Engine may contain older copies on some machines).
// Once loaded, Windows reuses them for all subsequent implicit dependencies. // Run exactly once per process so LoadLibrary refcounts don't grow per
// ANSLIB instance. The DLLs are intentionally pinned for process lifetime.
void PreloadSharedDllsOnce() {
static std::once_flag s_preloadFlag;
std::call_once(s_preloadFlag, []() {
const char* sharedDir = "C:\\ProgramData\\ANSCENTER\\Shared\\"; const char* sharedDir = "C:\\ProgramData\\ANSCENTER\\Shared\\";
const char* preloadDlls[] = { const char* preloadDlls[] = {
"ANSLicensingSystem.dll", "ANSLicensingSystem.dll",
@@ -23,10 +40,26 @@ namespace ANSCENTER
std::string fullPath = std::string(sharedDir) + preloadDlls[i]; std::string fullPath = std::string(sharedDir) + preloadDlls[i];
LoadLibraryA(fullPath.c_str()); LoadLibraryA(fullPath.c_str());
} }
// Ensure all shared DLLs (OpenCV, OpenVINO, TRT, ORT) are pre-loaded // Ensure all shared DLLs (OpenCV, OpenVINO, TRT, ORT) are pre-loaded
ANSCENTER::ANSLibsLoader::Initialize(); ANSCENTER::ANSLibsLoader::Initialize();
// Resolve logging C-facade symbols from the (now-pinned) licensing DLL.
// Use GetModuleHandleA — the DLL is already in this process's module
// list courtesy of the LoadLibraryA above, so refcount is unchanged.
if (HMODULE lic = GetModuleHandleA("ANSLicensingSystem.dll")) {
g_ANSLogger_LogInfo = (ANSLogger_LogFnT) GetProcAddress(lic, "ANSLogger_LogInfo");
g_ANSLogger_LogError = (ANSLogger_LogFnT) GetProcAddress(lic, "ANSLogger_LogError");
g_ANSLogger_LogFatal = (ANSLogger_LogFnT) GetProcAddress(lic, "ANSLogger_LogFatal");
g_ANSCENTER_IsDebugViewEnabled = (ANSCENTER_IsDbgEnFnT) GetProcAddress(lic, "ANSCENTER_IsDebugViewEnabled");
g_ANSCENTER_DebugLog = (ANSCENTER_DebugLogFnT)GetProcAddress(lic, "ANSCENTER_DebugLog");
}
});
}
}
ANSLIB::ANSLIB() {
PreloadSharedDllsOnce();
const char* dllPath = "C:\\ProgramData\\ANSCENTER\\Shared\\ANSODEngine.dll"; const char* dllPath = "C:\\ProgramData\\ANSCENTER\\Shared\\ANSODEngine.dll";
dllHandle = LoadLibraryA(dllPath); dllHandle = LoadLibraryA(dllPath);
if (!dllHandle) { if (!dllHandle) {
@@ -99,6 +132,10 @@ namespace ANSCENTER
return -1; return -1;
} }
if (CreateANSODHandle_CSFunc) { if (CreateANSODHandle_CSFunc) {
if (ANSHandle && ReleaseANSODHandleFunc) {
ReleaseANSODHandleFunc(&ANSHandle);
ANSHandle = nullptr;
}
const char* result = CreateANSODHandle_CSFunc(&ANSHandle, licenseKey, modelFilePath, modelFileZipPassword, const char* result = CreateANSODHandle_CSFunc(&ANSHandle, licenseKey, modelFilePath, modelFileZipPassword,
modelThreshold, modelConfThreshold, modelNMSThreshold, modelThreshold, modelConfThreshold, modelNMSThreshold,
1, modelType, detectionType, loadEngineOnCreation); 1, modelType, detectionType, loadEngineOnCreation);
@@ -118,9 +155,8 @@ namespace ANSCENTER
return -1; return -1;
} }
if (RunInference_CPPFunc) { if (RunInference_CPPFunc) {
auto imagePtr = std::make_unique<cv::Mat>(cvImage);
try { try {
cv::Mat* rawPtr = imagePtr.get(); cv::Mat* rawPtr = &cvImage;
int result = RunInference_CPPFunc(&ANSHandle, &rawPtr, cameraId, detectionResult); int result = RunInference_CPPFunc(&ANSHandle, &rawPtr, cameraId, detectionResult);
return result; return result;
} }
@@ -135,9 +171,8 @@ namespace ANSCENTER
return -1; return -1;
} }
if (RunInferenceComplete_CPPFunc) { if (RunInferenceComplete_CPPFunc) {
auto imagePtr = std::make_unique<cv::Mat>(cvImage);
try { try {
cv::Mat* rawPtr = imagePtr.get(); cv::Mat* rawPtr = &cvImage;
int result = RunInferenceComplete_CPPFunc(&ANSHandle, &rawPtr, cameraId, activeROIMode,detectionResult); int result = RunInferenceComplete_CPPFunc(&ANSHandle, &rawPtr, cameraId, activeROIMode,detectionResult);
return result; return result;
} }
@@ -193,6 +228,10 @@ namespace ANSCENTER
int autoDetectEngine, int modelType, int detectionType, int loadEngineOnCreation, const char* modelFolder,std::string& labelMap) int autoDetectEngine, int modelType, int detectionType, int loadEngineOnCreation, const char* modelFolder,std::string& labelMap)
{ {
if (!loaded || !LoadModelFromFolderFunc) return -1; if (!loaded || !LoadModelFromFolderFunc) return -1;
if (ANSHandle && ReleaseANSODHandleFunc) {
ReleaseANSODHandleFunc(&ANSHandle);
ANSHandle = nullptr;
}
return LoadModelFromFolderFunc(&ANSHandle, licenseKey, modelName, className, return LoadModelFromFolderFunc(&ANSHandle, licenseKey, modelName, className,
detectionScoreThreshold, modelConfThreshold, modelMNSThreshold, detectionScoreThreshold, modelConfThreshold, modelMNSThreshold,
autoDetectEngine, modelType, detectionType, loadEngineOnCreation,modelFolder, labelMap); autoDetectEngine, modelType, detectionType, loadEngineOnCreation,modelFolder, labelMap);
@@ -241,4 +280,41 @@ namespace ANSCENTER
if (!loaded || !SetTrackerParametersFunc) return -1; if (!loaded || !SetTrackerParametersFunc) return -1;
return SetTrackerParametersFunc(&ANSHandle, trackerParams); return SetTrackerParametersFunc(&ANSHandle, trackerParams);
} }
// ------------------------------------------------------------------------
// Static logging API. Each call ensures the DLL/symbols are resolved (cheap
// after the first call — std::call_once degenerates to an atomic load) then
// forwards to the C facade. Null-pointer guard makes missing symbols a
// silent no-op rather than a crash.
// ------------------------------------------------------------------------
int ANSLIB::IsDebugViewEnabled() {
PreloadSharedDllsOnce();
return g_ANSCENTER_IsDebugViewEnabled ? g_ANSCENTER_IsDebugViewEnabled() : 0;
}
void ANSLIB::DebugLog(const char* preformattedLine) {
PreloadSharedDllsOnce();
if (!preformattedLine) return;
if (g_ANSCENTER_DebugLog) {
// Preferred path: licensing DLL applies the prefix scrubber and emits.
g_ANSCENTER_DebugLog(preformattedLine);
return;
}
// Fallback for environments where ANSLicensingSystem.dll is missing:
// emit raw to DebugView + stderr without filtering. Better than dropping.
OutputDebugStringA(preformattedLine);
std::fputs(preformattedLine, stderr);
std::fflush(stderr);
}
void ANSLIB::LogInfo(const char* src, const char* msg, const char* file, int line) {
PreloadSharedDllsOnce();
if (g_ANSLogger_LogInfo) g_ANSLogger_LogInfo(src, msg, file, line);
}
void ANSLIB::LogError(const char* src, const char* msg, const char* file, int line) {
PreloadSharedDllsOnce();
if (g_ANSLogger_LogError) g_ANSLogger_LogError(src, msg, file, line);
}
void ANSLIB::LogFatal(const char* src, const char* msg, const char* file, int line) {
PreloadSharedDllsOnce();
if (g_ANSLogger_LogFatal) g_ANSLogger_LogFatal(src, msg, file, line);
}
} }
+61 -1
View File
@@ -6,6 +6,7 @@
#include <windows.h> #include <windows.h>
#include <opencv2/opencv.hpp> #include <opencv2/opencv.hpp>
#include <opencv2/core/cuda.hpp> #include <opencv2/core/cuda.hpp>
#include <cstdio>
enum DetectionType { enum DetectionType {
CLASSIFICATION = 0, CLASSIFICATION = 0,
DETECTION = 1, DETECTION = 1,
@@ -49,7 +50,9 @@ enum ModelType {
ONNXSAM3 = 28, ONNXSAM3 = 28,
RTSAM3 = 29, RTSAM3 = 29,
ONNXYOLO = 30, ONNXYOLO = 30,
RTYOLO = 31 RTYOLO = 31,
ONNXRTMPOSE=32,
RTRTMPOSE=33
}; };
enum TrackerType { enum TrackerType {
BYTETRACK = 0, BYTETRACK = 0,
@@ -263,6 +266,10 @@ namespace ANSCENTER {
public: public:
ANSLIB(); ANSLIB();
~ANSLIB() noexcept; ~ANSLIB() noexcept;
ANSLIB(const ANSLIB&) = delete;
ANSLIB& operator=(const ANSLIB&) = delete;
ANSLIB(ANSLIB&&) = delete;
ANSLIB& operator=(ANSLIB&&) = delete;
[[nodiscard]] static ANSLIB* Create(); [[nodiscard]] static ANSLIB* Create();
static void Destroy(ANSLIB* instance); static void Destroy(ANSLIB* instance);
[[nodiscard]] int Initialize(const char* licenseKey, [[nodiscard]] int Initialize(const char* licenseKey,
@@ -286,6 +293,28 @@ namespace ANSCENTER {
[[nodiscard]] int SetPrompt(const char* text); [[nodiscard]] int SetPrompt(const char* text);
[[nodiscard]] int SetTracker(int trackerType, int enableTracker); [[nodiscard]] int SetTracker(int trackerType, int enableTracker);
[[nodiscard]] int SetTrackerParameters(const char* trackerParams); [[nodiscard]] int SetTrackerParameters(const char* trackerParams);
// Logging — process-wide, thread-safe, no instance required.
// Routes to SPDLogger inside ANSLicensingSystem.dll via dynamically-resolved
// C exports. Silently no-ops if the DLL or symbols are unavailable.
static void LogInfo (const char* src, const char* msg, const char* file = "", int line = 0);
static void LogError(const char* src, const char* msg, const char* file = "", int line = 0);
static void LogFatal(const char* src, const char* msg, const char* file = "", int line = 0);
// DebugView gate. Wraps ANSCENTER_IsDebugViewEnabled() — non-zero when
// logging should be emitted (env var ANSCENTER_DBGVIEW or sentinel file
// C:\ProgramData\ANSCENTER\ansvisdebugview.txt). Cheap on the hot path.
// Used by the ANSLIB_DBG macro below; can also be called directly to
// gate expensive log-prep work.
static int IsDebugViewEnabled();
// DebugView emit. Hands a pre-formatted "[Tag] body\n" line to
// ANSCENTER_DebugLog inside the licensing DLL, which strips noise path
// prefixes (e.g. C:\ProgramData\ANSCENTER\, C:\ProgramData\<profile>\)
// before writing to OutputDebugStringA + stderr. Used by ANSLIB_DBG.
// If the licensing DLL is unavailable, falls back to raw emit (no
// filtering) so messages aren't lost.
static void DebugLog(const char* preformattedLine);
private: private:
HMODULE dllHandle = nullptr; HMODULE dllHandle = nullptr;
bool loaded = false; bool loaded = false;
@@ -436,4 +465,35 @@ public:
} }
}; };
// ANSLIB_DBG: DebugView logging macro mirroring ANS_DBG. Both the gate and the
// emit path are dynamic-loaded inside ANSLIB.dll — consumers don't need to
// link or even know about ANSLicensingSystem.dll. Output goes to BOTH
// OutputDebugStringA (DebugView) and stderr, identical format to ANS_DBG:
// "[tag] formatted\n".
//
// Noise path prefixes (e.g. C:\ProgramData\ANSCENTER\, C:\ProgramData\<profile>\)
// are stripped centrally inside ANSCENTER_DebugLog before emit, so log lines
// like "loaded from C:\ProgramData\Jh7O7nUe7vS\Models\Foo" appear as
// "loaded from Models\Foo".
//
// Usage: ANSLIB_DBG("MyModule", "value=%d ptr=%p", val, ptr);
//
// Hot-path cost when gate is OFF: one indirect call. When gate is ON: snprintf
// to a 1024-byte stack buffer + cross-DLL call into ANSCENTER_DebugLog (which
// scrubs prefixes then emits). No heap, no per-call allocation in the macro.
#ifndef ANSCORE_DEBUGVIEW
#define ANSCORE_DEBUGVIEW 1 // 1 = runtime-gated (default), 0 = hard-off (stripped)
#endif
#if ANSCORE_DEBUGVIEW
#define ANSLIB_DBG(tag, fmt, ...) do { \
if (::ANSCENTER::ANSLIB::IsDebugViewEnabled()) { \
char _ansl_dbg_buf[1024]; \
snprintf(_ansl_dbg_buf, sizeof(_ansl_dbg_buf), "[" tag "] " fmt "\n", ##__VA_ARGS__); \
::ANSCENTER::ANSLIB::DebugLog(_ansl_dbg_buf); \
} \
} while(0)
#else
#define ANSLIB_DBG(tag, fmt, ...) ((void)0)
#endif
#endif #endif
+282
View File
@@ -0,0 +1,282 @@
%PDF-1.4
%“Œ‹ž ReportLab Generated PDF document (opensource)
1 0 obj
<<
/F1 2 0 R /F2 3 0 R /F3 4 0 R /F4 5 0 R /F5 6 0 R /F6 10 0 R
/F7 15 0 R /F8 17 0 R
>>
endobj
2 0 obj
<<
/BaseFont /Helvetica /Encoding /WinAnsiEncoding /Name /F1 /Subtype /Type1 /Type /Font
>>
endobj
3 0 obj
<<
/BaseFont /Helvetica-Bold /Encoding /WinAnsiEncoding /Name /F2 /Subtype /Type1 /Type /Font
>>
endobj
4 0 obj
<<
/BaseFont /Courier /Encoding /WinAnsiEncoding /Name /F3 /Subtype /Type1 /Type /Font
>>
endobj
5 0 obj
<<
/BaseFont /Helvetica-Oblique /Encoding /WinAnsiEncoding /Name /F4 /Subtype /Type1 /Type /Font
>>
endobj
6 0 obj
<<
/BaseFont /Symbol /Name /F5 /Subtype /Type1 /Type /Font
>>
endobj
7 0 obj
<<
/Contents 23 0 R /MediaBox [ 0 0 595.2756 841.8898 ] /Parent 22 0 R /Resources <<
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
>> /Rotate 0 /Trans <<
>>
/Type /Page
>>
endobj
8 0 obj
<<
/Contents 24 0 R /MediaBox [ 0 0 595.2756 841.8898 ] /Parent 22 0 R /Resources <<
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
>> /Rotate 0 /Trans <<
>>
/Type /Page
>>
endobj
9 0 obj
<<
/Contents 25 0 R /MediaBox [ 0 0 595.2756 841.8898 ] /Parent 22 0 R /Resources <<
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
>> /Rotate 0 /Trans <<
>>
/Type /Page
>>
endobj
10 0 obj
<<
/BaseFont /Courier-Bold /Encoding /WinAnsiEncoding /Name /F6 /Subtype /Type1 /Type /Font
>>
endobj
11 0 obj
<<
/Contents 26 0 R /MediaBox [ 0 0 595.2756 841.8898 ] /Parent 22 0 R /Resources <<
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
>> /Rotate 0 /Trans <<
>>
/Type /Page
>>
endobj
12 0 obj
<<
/Contents 27 0 R /MediaBox [ 0 0 595.2756 841.8898 ] /Parent 22 0 R /Resources <<
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
>> /Rotate 0 /Trans <<
>>
/Type /Page
>>
endobj
13 0 obj
<<
/Contents 28 0 R /MediaBox [ 0 0 595.2756 841.8898 ] /Parent 22 0 R /Resources <<
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
>> /Rotate 0 /Trans <<
>>
/Type /Page
>>
endobj
14 0 obj
<<
/Contents 29 0 R /MediaBox [ 0 0 595.2756 841.8898 ] /Parent 22 0 R /Resources <<
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
>> /Rotate 0 /Trans <<
>>
/Type /Page
>>
endobj
15 0 obj
<<
/BaseFont /Courier-Oblique /Encoding /WinAnsiEncoding /Name /F7 /Subtype /Type1 /Type /Font
>>
endobj
16 0 obj
<<
/Contents 30 0 R /MediaBox [ 0 0 595.2756 841.8898 ] /Parent 22 0 R /Resources <<
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
>> /Rotate 0 /Trans <<
>>
/Type /Page
>>
endobj
17 0 obj
<<
/BaseFont /ZapfDingbats /Name /F8 /Subtype /Type1 /Type /Font
>>
endobj
18 0 obj
<<
/Contents 31 0 R /MediaBox [ 0 0 595.2756 841.8898 ] /Parent 22 0 R /Resources <<
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
>> /Rotate 0 /Trans <<
>>
/Type /Page
>>
endobj
19 0 obj
<<
/Contents 32 0 R /MediaBox [ 0 0 595.2756 841.8898 ] /Parent 22 0 R /Resources <<
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
>> /Rotate 0 /Trans <<
>>
/Type /Page
>>
endobj
20 0 obj
<<
/PageMode /UseNone /Pages 22 0 R /Type /Catalog
>>
endobj
21 0 obj
<<
/Author (ANSCORE) /CreationDate (D:20260505044053+10'00') /Creator (\(unspecified\)) /Keywords () /ModDate (D:20260505044053+10'00') /Producer (ReportLab PDF Library - \(opensource\))
/Subject (\(unspecified\)) /Title (ANSLPR \204 ALPRChecker & Plate Format Developer Guide) /Trapped /False
>>
endobj
22 0 obj
<<
/Count 10 /Kids [ 7 0 R 8 0 R 9 0 R 11 0 R 12 0 R 13 0 R 14 0 R 16 0 R 18 0 R 19 0 R ] /Type /Pages
>>
endobj
23 0 obj
<<
/Filter [ /ASCII85Decode /FlateDecode ] /Length 2672
>>
stream
Gau0E>?BiE&q9"FeC1!C9oL[]')$>UbJ6g?dL?;Hh1s7H3Pnf0G:T!2(Ee)#motqpa)Pq90KksR=I)odWlq;9_?(B8QMM*B!.UTVq2GOnJ_:)Ua)GF0$eM_KJ"9^'8Codo@XA6*a##-7BAku>`ed_BMg-p["!M^Uq$d@?,/kFs@p?AP#-k)Eo"]4XM+;qaQS?#Kr-3Sg!-<bZ974<SZ/@-*iHK7eC!PX+UhcF:p[";fOg+HlAm\s>*K5FqU6'4rB]r7eBf"=^g5WN0bEpd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jRX&Cn9Q6'#tcSn^^E`V;AQ$1n)0'A,pka6BX5l'HMlrc4@0n]d[1dSBl/GEZUXGs^OjdPjMt8,i[4M:L#~>endstream
endobj
xref
0 33
0000000000 65535 f
0000000061 00000 n
0000000168 00000 n
0000000275 00000 n
0000000387 00000 n
0000000492 00000 n
0000000607 00000 n
0000000684 00000 n
0000000889 00000 n
0000001094 00000 n
0000001299 00000 n
0000001410 00000 n
0000001616 00000 n
0000001822 00000 n
0000002028 00000 n
0000002234 00000 n
0000002348 00000 n
0000002554 00000 n
0000002638 00000 n
0000002844 00000 n
0000003050 00000 n
0000003120 00000 n
0000003436 00000 n
0000003558 00000 n
0000006322 00000 n
0000008504 00000 n
0000010470 00000 n
0000013654 00000 n
0000016372 00000 n
0000018864 00000 n
0000021615 00000 n
0000023024 00000 n
0000026120 00000 n
trailer
<<
/ID
[<704d8733fff16b81e99fe2145e253d08><704d8733fff16b81e99fe2145e253d08>]
% ReportLab generated PDF document -- digest (opensource)
/Info 21 0 R
/Root 20 0 R
/Size 33
>>
startxref
27968
%%EOF
+149
View File
@@ -0,0 +1,149 @@
%PDF-1.4
%“Œ‹ž ReportLab Generated PDF document (opensource)
1 0 obj
<<
/F1 2 0 R /F2 3 0 R /F3 4 0 R /F4 5 0 R /F5 8 0 R
>>
endobj
2 0 obj
<<
/BaseFont /Helvetica /Encoding /WinAnsiEncoding /Name /F1 /Subtype /Type1 /Type /Font
>>
endobj
3 0 obj
<<
/BaseFont /Helvetica-Bold /Encoding /WinAnsiEncoding /Name /F2 /Subtype /Type1 /Type /Font
>>
endobj
4 0 obj
<<
/BaseFont /Courier /Encoding /WinAnsiEncoding /Name /F3 /Subtype /Type1 /Type /Font
>>
endobj
5 0 obj
<<
/BaseFont /Symbol /Name /F4 /Subtype /Type1 /Type /Font
>>
endobj
6 0 obj
<<
/Contents 14 0 R /MediaBox [ 0 0 595.2756 841.8898 ] /Parent 13 0 R /Resources <<
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
>> /Rotate 0 /Trans <<
>>
/Type /Page
>>
endobj
7 0 obj
<<
/Contents 15 0 R /MediaBox [ 0 0 595.2756 841.8898 ] /Parent 13 0 R /Resources <<
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
>> /Rotate 0 /Trans <<
>>
/Type /Page
>>
endobj
8 0 obj
<<
/BaseFont /Helvetica-Oblique /Encoding /WinAnsiEncoding /Name /F5 /Subtype /Type1 /Type /Font
>>
endobj
9 0 obj
<<
/Contents 16 0 R /MediaBox [ 0 0 595.2756 841.8898 ] /Parent 13 0 R /Resources <<
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
>> /Rotate 0 /Trans <<
>>
/Type /Page
>>
endobj
10 0 obj
<<
/Contents 17 0 R /MediaBox [ 0 0 595.2756 841.8898 ] /Parent 13 0 R /Resources <<
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
>> /Rotate 0 /Trans <<
>>
/Type /Page
>>
endobj
11 0 obj
<<
/PageMode /UseNone /Pages 13 0 R /Type /Catalog
>>
endobj
12 0 obj
<<
/Author (ANSCORE) /CreationDate (D:20260504092543+10'00') /Creator (\(unspecified\)) /Keywords () /ModDate (D:20260504092543+10'00') /Producer (ReportLab PDF Library - \(opensource\))
/Subject (\(unspecified\)) /Title (ANSODEngine \204 Tracker & Stabilization Developer Guide) /Trapped /False
>>
endobj
13 0 obj
<<
/Count 4 /Kids [ 6 0 R 7 0 R 9 0 R 10 0 R ] /Type /Pages
>>
endobj
14 0 obj
<<
/Filter [ /ASCII85Decode /FlateDecode ] /Length 2736
>>
stream
Gb!#^D0+_q%fW&,JPS5Y-II_#MsYW_+\eKPp,:#NENdt3cZBkZMRA]q=/%Y:rq_526qOiDU2E".:D?_M5nF%4#@IfSp`]4=g&QN8JG1u*#_dsC`6p5>I"_=TjGst;pTu<Z5u!-O`%ptait*2cA/n,S0C$>^"-e\Zmr'V9WF2VCQ:]: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@#Nrj!X)3.5:^N37)l#9R0:[*!KX@E0Q,nXS4[D'i`1W"a"a0nL-,FkP1"^"3-5X!d9tI-TutXB%:k08F5YjBkBnE$t~>endstream
endobj
xref
0 18
0000000000 65535 f
0000000061 00000 n
0000000132 00000 n
0000000239 00000 n
0000000351 00000 n
0000000456 00000 n
0000000533 00000 n
0000000738 00000 n
0000000943 00000 n
0000001058 00000 n
0000001263 00000 n
0000001469 00000 n
0000001539 00000 n
0000001857 00000 n
0000001936 00000 n
0000004764 00000 n
0000005779 00000 n
0000008933 00000 n
trailer
<<
/ID
[<5308ad286f7ed61e1c31de3c2e9f2ace><5308ad286f7ed61e1c31de3c2e9f2ace>]
% ReportLab generated PDF document -- digest (opensource)
/Info 12 0 R
/Root 11 0 R
/Size 18
>>
startxref
10640
%%EOF
+40 -5
View File
@@ -1,5 +1,11 @@
cmake_minimum_required(VERSION 3.28) cmake_minimum_required(VERSION 3.28)
cmake_policy(SET CMP0167 NEW) # Suppress FindBoost removal warning # CMP0167 (FindBoost removed from CMake) only exists from 3.30, and this
# project asks for 3.28 — so setting it unconditionally makes every older
# CMake fail at line two with "policy is not known to this version". Guarded,
# a newer CMake still gets the quiet FindBoost it was added for.
if(POLICY CMP0167)
cmake_policy(SET CMP0167 NEW)
endif()
# ── CUDA is optional (not available on macOS) ─────────────────── # ── CUDA is optional (not available on macOS) ───────────────────
include(CheckLanguage) include(CheckLanguage)
@@ -9,11 +15,22 @@ if(CMAKE_CUDA_COMPILER)
set(ANSCORE_HAS_CUDA ON) set(ANSCORE_HAS_CUDA ON)
set(CMAKE_CUDA_STANDARD 20) set(CMAKE_CUDA_STANDARD 20)
set(CMAKE_CUDA_STANDARD_REQUIRED ON) 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)
# --use_fast_math, with underscores: that is nvcc's own spelling. Older
# toolkits also accepted the hyphenated form; CUDA 13 does not, and every
# .cu file fails with "Unknown option '--use-fast-math'" before it compiles
# a line.
if(MSVC) if(MSVC)
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --use-fast-math -Xcompiler=/utf-8") set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --use_fast_math -Xcompiler=/utf-8")
else() else()
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --use-fast-math") set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --use_fast_math")
endif() endif()
else() else()
project(ANSCORE LANGUAGES C CXX) project(ANSCORE LANGUAGES C CXX)
@@ -36,6 +53,12 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE 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 # Output all binaries to a single directory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
@@ -141,7 +164,12 @@ include(cmake/Dependencies.cmake)
# Core — licensing & infrastructure # Core — licensing & infrastructure
add_subdirectory(core/anslicensing) add_subdirectory(core/anslicensing)
add_subdirectory(core/ANSLicensingSystem) add_subdirectory(core/ANSLicensingSystem)
# ANSLibsLoader dynamically binds cudart/cuBLAS/nvJPEG at runtime, but it still
# compiles against <cuda_runtime.h> for their types and links CUDA::cudart_static.
if(ANSCORE_HAS_CUDA)
add_subdirectory(core/ANSLibsLoader) add_subdirectory(core/ANSLibsLoader)
endif()
add_subdirectory(core/ANSModelPackage)
# Modules — feature DLLs (cross-platform) # Modules — feature DLLs (cross-platform)
add_subdirectory(modules/ANSMOT) add_subdirectory(modules/ANSMOT)
@@ -164,11 +192,18 @@ if(ANSCORE_HAS_CUDA)
add_subdirectory(modules/ANSOCR) add_subdirectory(modules/ANSOCR)
add_subdirectory(modules/ANSFR) add_subdirectory(modules/ANSFR)
add_subdirectory(modules/ANSLPR) add_subdirectory(modules/ANSLPR)
endif() # ANSCV hosts NVDEC decode, the NV12 GPU frame pool and the nvJPEG encoder:
# ANSGpuFrameOps.h includes <cuda_runtime.h> unconditionally and calls
# cudaMemcpy2D/cudaMallocPitch directly, so it needs the toolkit like the
# engine modules do.
add_subdirectory(modules/ANSCV) add_subdirectory(modules/ANSCV)
endif()
# Facade # Facade
add_subdirectory(ANSLIB) add_subdirectory(ANSLIB)
# Tests # Tests
# enable_testing() (not include(CTest), which would add Nightly/Experimental
# targets) so the add_test() calls under tests/ reach ctest.
enable_testing()
add_subdirectory(tests) add_subdirectory(tests)
+72
View File
@@ -0,0 +1,72 @@
#pragma once
// av_frame_ptr.h — RAII smart pointer for FFmpeg AVFrame*.
//
// Why this exists
// ---------------
// CVideoPlayer::getNV12Frame() / getCudaHWFrame() return cloned AVFrame* and
// the contract is "caller must av_frame_free()". That contract is comment-only
// — nothing in the type system enforces it, so any early return, exception,
// or refactor that drops the pointer leaks the AVFrame *and* the underlying
// NV12/YUV420P/YUVJ420P buffers (~3 MB per 1080p frame, ~50 MB/s under load).
//
// AvFramePtr wraps the raw pointer in std::unique_ptr<AVFrame, AvFrameDeleter>
// so the destructor frees it automatically — leak-by-omission becomes a
// compile-time impossibility on every internal code path that adopts this type.
//
// Scope: INTERNAL TO ANSCORE ONLY.
// -------------------------------
// Do NOT change DLL-exported function signatures to use AvFramePtr.
// std::unique_ptr cannot cross a DLL boundary safely (different runtimes,
// different STL implementations, different allocators). The public
// AVFrame*-returning APIs in ANSRTSP.h / ANSSRT.h / etc. must remain raw
// pointers for ABI stability.
//
// Use this type at the internal *consumer* call site:
//
// AvFramePtr nv12{ player->getNV12Frame() };
// if (nv12) {
// doSomething(nv12.get()); // borrowed access
// } // auto-freed on scope exit
//
// To transfer ownership into ANSCV's pendingFree pipeline (the
// gpu_frame_attach / gpu_frame_attach_cuda functions, which take raw
// AVFrame* and store/defer them), call .release():
//
// AvFramePtr nv12{ player->getNV12Frame() };
// gpu_frame_attach(mat, nv12.release(), gpuIdx, pts);
//
// Every .release() in the codebase is a deliberate ownership hand-off —
// `git grep '\.release()'` over the migrated files gives an audit list.
extern "C" {
#include <libavutil/frame.h>
}
#include <memory>
namespace ansmedia {
// Custom deleter that calls av_frame_free (which takes AVFrame**).
// noexcept because deleters must not throw — av_frame_free is a C function
// and never throws, but tagging makes the contract explicit.
struct AvFrameDeleter {
void operator()(AVFrame* f) const noexcept {
if (f) {
av_frame_free(&f); // takes AVFrame**, sets *f to nullptr internally
}
}
};
// Owning smart pointer for AVFrame*. Frees on destruction via av_frame_free.
//
// Construction:
// AvFramePtr p; // empty
// AvFramePtr p{ av_frame_clone(src) }; // adopt raw clone
//
// Ownership transfer (e.g. handing off to gpu_frame_attach_cuda):
// takes_ownership(p.release()); // raw pointer escapes, no auto-free
//
// Copy is disallowed (unique_ptr semantics). Move is fine.
using AvFramePtr = std::unique_ptr<AVFrame, AvFrameDeleter>;
} // namespace ansmedia
+7
View File
@@ -59,6 +59,7 @@ CFilePlayer::CFilePlayer()
, m_bSeek(0) , m_bSeek(0)
, m_dSeekPos(0) , m_dSeekPos(0)
, m_nNalLength(0) , m_nNalLength(0)
, m_bLooped(FALSE)
, m_pFormatContext(NULL) , m_pFormatContext(NULL)
, m_hReadThread(0) , m_hReadThread(0)
, m_hVideoThread(0) , m_hVideoThread(0)
@@ -282,6 +283,7 @@ BOOL CFilePlayer::play()
} }
m_bPlaying = TRUE; m_bPlaying = TRUE;
m_bLooped = FALSE; // fresh playback — no wrap yet
if (m_nVideoIndex >= 0) if (m_nVideoIndex >= 0)
{ {
@@ -441,6 +443,11 @@ BOOL CFilePlayer::readFrame()
} }
} }
// File wrapped — flag it so the consumer (ANSVideoPlayer) can detect
// EOF without watching m_nCurPos, which is non-monotonic during normal
// playback (decode-order packet PTS + audio thread also writes to it).
m_bLooped = TRUE;
if (av_read_frame(m_pFormatContext, &pkt) != 0) if (av_read_frame(m_pFormatContext, &pkt) != 0)
{ {
return FALSE; return FALSE;
+7
View File
@@ -53,6 +53,12 @@ public:
BOOL seek(int pos); BOOL seek(int pos);
int64 getElapse() {return m_nCurPos;}; int64 getElapse() {return m_nCurPos;};
int64 getDuration() {return m_nDuration;} int64 getDuration() {return m_nDuration;}
// Set inside readFrame() when av_read_frame returns AVERROR_EOF and the
// reader auto-loops via av_seek_frame. Caller (ANSVideoPlayer) polls this
// to detect EOF without relying on m_nCurPos, which is non-monotonic
// (decode-order packet PTS + audio thread also writes to it).
BOOL hasLooped() {return m_bLooped;}
void clearLooped() {m_bLooped = FALSE;}
int getVideoCodec(); int getVideoCodec();
int getAudioCodec(); int getAudioCodec();
BOOL onRecord(); BOOL onRecord();
@@ -79,6 +85,7 @@ private:
int m_bSeek; // seek request int m_bSeek; // seek request
double m_dSeekPos; // seek position, nit is millisecond double m_dSeekPos; // seek position, nit is millisecond
int m_nNalLength; int m_nNalLength;
BOOL m_bLooped; // set by readFrame() when EOF triggered an av_seek_frame back to start
AVFormatContext * m_pFormatContext; // format context AVFormatContext * m_pFormatContext; // format context
+214 -15
View File
@@ -9,10 +9,76 @@
#include "ANSLicense.h" // ANS_DBG macro (gated by ANSCORE_DEBUGVIEW) #include "ANSLicense.h" // ANS_DBG macro (gated by ANSCORE_DEBUGVIEW)
#include <cstdarg>
#include <mutex>
#include <unordered_map>
extern "C" { extern "C" {
#include "libavutil/imgutils.h" #include "libavutil/imgutils.h"
#include "libavutil/buffer.h" #include "libavutil/buffer.h"
#include "libavutil/mem.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; 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 // FIXED: Close codec before freeing context
avcodec_close(m_pContext); avcodec_close(m_pContext);
avcodec_free_context(&m_pContext); avcodec_free_context(&m_pContext);
@@ -602,6 +672,13 @@ BOOL CVideoDecoder::init(enum AVCodecID codec, uint8* extradata, int extradata_s
return FALSE; 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. // Debug: one-shot visibility into which decoder actually got opened.
// m_bHardwareDecoderEnabled is set by hwDecoderInit() on success; when // m_bHardwareDecoderEnabled is set by hwDecoderInit() on success; when
// hwMode == HW_DECODING_DISABLE or hwDecoderInit failed, it stays FALSE // hwMode == HW_DECODING_DISABLE or hwDecoderInit failed, it stays FALSE
@@ -761,6 +838,10 @@ void CVideoDecoder::Start() {
if (m_pContext) { if (m_pContext) {
avcodec_flush_buffers(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; m_bRunning = TRUE;
log_print(HT_LOG_INFO, "%s, Video decoder started\r\n", __FUNCTION__); log_print(HT_LOG_INFO, "%s, Video decoder started\r\n", __FUNCTION__);
} }
@@ -943,18 +1024,20 @@ BOOL CVideoDecoder::decode(AVPacket* pkt)
if (!m_bInited) if (!m_bInited)
{ {
log_print(HT_LOG_ERR, "%s, Decoder not initialized\r\n", __FUNCTION__); // Hot-path pre-check; per-packet during early frames if decode() races
// with init.
ANS_DBG("MEDIA_Decode", "decode(pkt) called but decoder not initialized");
return FALSE; return FALSE;
} }
if (!m_bRunning) if (!m_bRunning)
{ {
log_print(HT_LOG_WARN, "%s, Decoder not running\r\n", __FUNCTION__); ANS_DBG("MEDIA_Decode", "decode(pkt) called but decoder not running");
return FALSE; return FALSE;
} }
if (!m_pContext) { if (!m_pContext) {
log_print(HT_LOG_ERR, "%s, Context is NULL\r\n", __FUNCTION__); ANS_DBG("MEDIA_Decode", "decode(pkt) called but m_pContext is NULL");
return FALSE; return FALSE;
} }
@@ -968,7 +1051,12 @@ BOOL CVideoDecoder::decode(AVPacket* pkt)
{ {
if (!readFrame()) if (!readFrame())
{ {
log_print(HT_LOG_ERR, "%s, Failed to read frame during retry %d\r\n", __FUNCTION__, retryCount); // Hot-path error: was log_print() which fflushes per call and can
// spike per-packet during stream stress. ANS_DBG is silent in
// production; the per-frame error count is also visible via
// MEDIA_Pulse drop(err=) so observability is preserved.
ANS_DBG("MEDIA_Decode",
"send_packet retry %d: readFrame failed", retryCount);
return FALSE; return FALSE;
} }
Sleep(1); // Reduced sleep time Sleep(1); // Reduced sleep time
@@ -980,7 +1068,8 @@ BOOL CVideoDecoder::decode(AVPacket* pkt)
{ {
char error_buf[AV_ERROR_MAX_STRING_SIZE]; char error_buf[AV_ERROR_MAX_STRING_SIZE];
av_strerror(ret, error_buf, sizeof(error_buf)); av_strerror(ret, error_buf, sizeof(error_buf));
log_print(HT_LOG_ERR, "%s, avcodec_send_packet failed: %s (ret=%d)\r\n", __FUNCTION__, error_buf, ret); ANS_DBG("MEDIA_Decode",
"avcodec_send_packet failed: %s (ret=%d)", error_buf, ret);
return FALSE; return FALSE;
} }
@@ -993,25 +1082,29 @@ BOOL CVideoDecoder::decode(uint8* data, int len, int64_t pts)
if (!m_bInited) if (!m_bInited)
{ {
log_print(HT_LOG_ERR, "%s, Decoder not initialized\r\n", __FUNCTION__); // Hot-path: per-packet if decoder isn't initialised. Was log_print
// which fflushes per call.
ANS_DBG("MEDIA_Decode", "decode called but decoder not initialized");
return FALSE; return FALSE;
} }
if (!m_bRunning) if (!m_bRunning)
{ {
log_print(HT_LOG_WARN, "%s, Decoder not running\r\n", __FUNCTION__); // Per-packet during start/stop transitions.
ANS_DBG("MEDIA_Decode", "decode called but decoder not running");
return FALSE; return FALSE;
} }
if (!data || len <= 0) { if (!data || len <= 0) {
log_print(HT_LOG_ERR, "%s, Invalid input data\r\n", __FUNCTION__); ANS_DBG("MEDIA_Decode", "decode called with invalid input (data=%p len=%d)",
(void*)data, len);
return FALSE; return FALSE;
} }
// Allocate packet // Allocate packet
AVPacket* packet = av_packet_alloc(); AVPacket* packet = av_packet_alloc();
if (!packet) { if (!packet) {
log_print(HT_LOG_ERR, "%s, Failed to allocate AVPacket\r\n", __FUNCTION__); ANS_DBG("MEDIA_Decode", "av_packet_alloc failed (OOM?)");
return FALSE; return FALSE;
} }
@@ -1020,7 +1113,8 @@ BOOL CVideoDecoder::decode(uint8* data, int len, int64_t pts)
if (ret < 0) { if (ret < 0) {
char error_buf[AV_ERROR_MAX_STRING_SIZE]; char error_buf[AV_ERROR_MAX_STRING_SIZE];
av_strerror(ret, error_buf, sizeof(error_buf)); av_strerror(ret, error_buf, sizeof(error_buf));
log_print(HT_LOG_ERR, "%s, Failed to allocate packet data: %s\r\n", __FUNCTION__, error_buf); ANS_DBG("MEDIA_Decode",
"av_new_packet failed (len=%d): %s", len, error_buf);
av_packet_free(&packet); av_packet_free(&packet);
return FALSE; return FALSE;
} }
@@ -1061,17 +1155,118 @@ BOOL CVideoDecoder::readFrame()
else if (ret < 0) { else if (ret < 0) {
char error_buf[AV_ERROR_MAX_STRING_SIZE]; char error_buf[AV_ERROR_MAX_STRING_SIZE];
av_strerror(ret, error_buf, sizeof(error_buf)); av_strerror(ret, error_buf, sizeof(error_buf));
log_print(HT_LOG_ERR, "%s, avcodec_receive_frame failed: %s (ret=%d)\r\n", __FUNCTION__, error_buf, ret); // Hot-path error during stream stress (UDP loss, corrupt I-frame).
// Was log_print(HT_LOG_ERR, ...) which fflushes per call and could
// fire ~25/sec on a degraded stream — observed as sudden GetImage
// slowness. ANS_DBG is silent in production; pulse counter
// MEDIA_Pulse drop(err=) preserves observability.
ANS_DBG("MEDIA_Decode",
"avcodec_receive_frame failed: %s (ret=%d)", error_buf, ret);
return FALSE; return FALSE;
} }
// Check if we got a valid frame // Check if we got a valid frame
if (!m_pFrame || m_pFrame->width <= 0 || m_pFrame->height <= 0) { if (!m_pFrame || m_pFrame->width <= 0 || m_pFrame->height <= 0) {
log_print(HT_LOG_WARN, "%s, Received invalid frame\r\n", __FUNCTION__); ANS_DBG("MEDIA_Decode", "received invalid frame (w=%d h=%d)",
m_pFrame ? m_pFrame->width : -1,
m_pFrame ? m_pFrame->height : -1);
av_frame_unref(m_pFrame); av_frame_unref(m_pFrame);
continue; 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) if (m_pFrame->format == m_hwPixFmt)
{ {
// CUDA HW accel: clone the HW frame BEFORE transfer so inference // CUDA HW accel: clone the HW frame BEFORE transfer so inference
@@ -1090,8 +1285,10 @@ BOOL CVideoDecoder::readFrame()
{ {
char error_buf[AV_ERROR_MAX_STRING_SIZE]; char error_buf[AV_ERROR_MAX_STRING_SIZE];
av_strerror(ret, error_buf, sizeof(error_buf)); av_strerror(ret, error_buf, sizeof(error_buf));
log_print(HT_LOG_ERR, "%s, Error transferring hardware frame to system memory: %s (ret=%d)\r\n", // Hot-path: HW→SW transfer failure. Per-frame on degraded HW
__FUNCTION__, error_buf, ret); // decoder. Gated to avoid fflush spike during stress.
ANS_DBG("MEDIA_Decode",
"hwframe_transfer_data failed: %s (ret=%d)", error_buf, ret);
av_frame_unref(m_pFrame); av_frame_unref(m_pFrame);
if (m_pCudaHWFrame) av_frame_free(&m_pCudaHWFrame); if (m_pCudaHWFrame) av_frame_free(&m_pCudaHWFrame);
continue; continue;
@@ -1139,7 +1336,9 @@ int CVideoDecoder::render(AVFrame* frame)
m_pCallback(frame, m_pUserdata); m_pCallback(frame, m_pUserdata);
} }
catch (...) { catch (...) {
log_print(HT_LOG_ERR, "%s, Exception in callback function\r\n", __FUNCTION__); // Hot-path: per-frame if onVideoFrame ever throws. Was log_print
// which fflushes per call.
ANS_DBG("MEDIA_Decode", "exception in onVideoFrame callback");
return 0; return 0;
} }
} }
+13
View File
@@ -165,6 +165,19 @@ private:
std::atomic<BOOL> m_bRunning; std::atomic<BOOL> m_bRunning;
BOOL m_bHardwareDecoderEnabled; // Track if hardware decoder is enabled BOOL m_bHardwareDecoderEnabled; // Track if hardware decoder is enabled
bool m_bCudaHWAccel; // true when using AV_HWDEVICE_TYPE_CUDA 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) int m_hwGpuIndex; // GPU index assigned by HWDecoderPool (-1 = legacy)
AVFrame* m_pCudaHWFrame; // Cloned CUDA HW frame (device ptrs) for inference AVFrame* m_pCudaHWFrame; // Cloned CUDA HW frame (device ptrs) for inference
const AVCodec* m_pCodec; const AVCodec* m_pCodec;
File diff suppressed because it is too large Load Diff
+58 -1
View File
@@ -11,12 +11,14 @@
#include "media_util.h" #include "media_util.h"
#include <list> #include <list>
#include <string> #include <string>
#include <array>
#include <opencv2/imgproc.hpp> #include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp> #include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp> #include <opencv2/opencv.hpp>
#include <turbojpeg.h> #include <turbojpeg.h>
#include <atomic> #include <atomic>
#include <chrono> #include <chrono>
#include "ANSLicense.h" // ANS_DBG (used by inline FrameQueue::pushFrame below)
// Leak diagnostics — net counters surfaced in MEDIA_Leak heartbeat. // Leak diagnostics — net counters surfaced in MEDIA_Leak heartbeat.
// Defined in video_player.cpp; also incremented from FrameQueue here. // Defined in video_player.cpp; also incremented from FrameQueue here.
@@ -51,6 +53,16 @@ private:
uint64_t m_frameSeq = 0; // Sequence counter — increments on each new frame push uint64_t m_frameSeq = 0; // Sequence counter — increments on each new frame push
public: public:
// Pulse counter for evicted-unread frames (instrumentation, not behavior).
// Incremented every time a push has to drop the oldest frame because the
// queue hit maxFrames AND the oldest never got read. With getLatestFrame()
// the consumer reads the back, so anything pushed before the consumer's
// last read sits in the middle/front and gets silently evicted here.
static std::atomic<uint64_t>& evictedCounter() {
static std::atomic<uint64_t> g{0};
return g;
}
// Push a new frame to the queue (removes oldest if full) // Push a new frame to the queue (removes oldest if full)
void pushFrame(AVFrame* newFrame) { void pushFrame(AVFrame* newFrame) {
if (!newFrame) return; if (!newFrame) return;
@@ -60,7 +72,9 @@ public:
// Clone the frame to ensure proper ownership transfer // Clone the frame to ensure proper ownership transfer
AVFrame* frameCopy = av_frame_clone(newFrame); AVFrame* frameCopy = av_frame_clone(newFrame);
if (!frameCopy) { if (!frameCopy) {
std::cerr << "Failed to clone AVFrame!" << std::endl; // av_frame_clone failure (basically OOM). Gated to avoid spamming
// stderr during memory pressure.
ANS_DBG("MEDIA_Queue", "av_frame_clone failed (OOM?)");
return; return;
} }
g_queueClones.fetch_add(1, std::memory_order_relaxed); g_queueClones.fetch_add(1, std::memory_order_relaxed);
@@ -74,6 +88,7 @@ public:
frameQueue.pop(); frameQueue.pop();
av_frame_free(&oldFrame); av_frame_free(&oldFrame);
g_queueFrees.fetch_add(1, std::memory_order_relaxed); g_queueFrees.fetch_add(1, std::memory_order_relaxed);
evictedCounter().fetch_add(1, std::memory_order_relaxed);
} }
} }
@@ -177,6 +192,16 @@ public:
virtual void setImageQuality(int mode) { m_nImageQuality = mode; } virtual void setImageQuality(int mode) { m_nImageQuality = mode; }
void setTargetFPS(double intervalMs); // Set minimum interval between processed frames in ms (0 = no limit, 100 = ~10 FPS) void setTargetFPS(double intervalMs); // Set minimum interval between processed frames in ms (0 = no limit, 100 = ~10 FPS)
double getLastFrameAgeMs(); // Milliseconds since last frame arrived from decoder (0 if no frame yet) double getLastFrameAgeMs(); // Milliseconds since last frame arrived from decoder (0 if no frame yet)
// Jitter buffer: hold decoded frames for ~ms before delivering to consumer.
// Smooths network/decoder jitter at the cost of added end-to-end latency.
// 0 disables (today's getLatest semantics). Default 200 ms.
// Adaptive: if the consumer is slower than the producer, drop-ahead kicks in
// and bypasses the buffer (latency stays ~0 frames).
// Note: applies only to the BGR cv::Mat returned by getImage(); the NV12/CUDA
// fast-path frames (m_currentNV12Frame / m_currentCudaHWFrame) remain
// updated at push time so inference always sees the latest.
void setJitterBufferMs(int ms);
int getJitterBufferMs() const { return m_jitterBufferMs; }
virtual void setRtpMulticast(BOOL flag) {} virtual void setRtpMulticast(BOOL flag) {}
virtual void setRtpOverUdp(BOOL flag) {} virtual void setRtpOverUdp(BOOL flag) {}
@@ -317,6 +342,31 @@ protected:
std::chrono::steady_clock::time_point m_lastDecoderFrameTime; std::chrono::steady_clock::time_point m_lastDecoderFrameTime;
bool m_lastDecoderFrameTimeSet = false; bool m_lastDecoderFrameTimeSet = false;
// --- Jitter buffer (BGR delivery only) ---
// Default 200 ms — absorbs typical network/decoder hiccups.
int m_jitterBufferMs = 200;
// Observed inter-arrival interval, ms. Seeds at 40 ms (=25 fps) and is
// refined to a rolling median of the last m_arrivalCount samples.
double m_observedFrameIntervalMs = 40.0;
// Ring buffer of recent arrival ticks (steady_clock raw ticks). Used to
// compute the rolling median frame interval. Sized at 32 — covers >1 s at
// 30 fps so a single GOP-boundary jitter doesn't dominate.
static constexpr size_t kArrivalRingSize = 32;
std::array<long long, kArrivalRingSize> m_arrivalTicks{};
size_t m_arrivalIdx = 0;
size_t m_arrivalCount = 0;
// Wall-clock of the last frame released to the consumer. Used for rate-paced
// delivery — if the consumer polls faster than the producer rate, between
// releases we return the cached frame so the buffer doesn't drain instantly.
std::chrono::steady_clock::time_point m_lastReleaseTime;
bool m_lastReleaseTimeSet = false;
// Fill-phase: when true, hold releases until queue depth reaches target.
// Set true at startup and again whenever the queue drains to empty mid-stream.
// This is what makes the buffer actually buffer — without it, pacing alone
// can't grow the queue from empty (producer and consumer would just match
// rates with depth=0 forever).
bool m_jitterBufferRefilling = true;
BOOL m_bPlaying; BOOL m_bPlaying;
BOOL m_bPaused; BOOL m_bPaused;
@@ -378,6 +428,13 @@ protected:
int m_nv12LastWidth = 0, m_nv12LastHeight = 0; int m_nv12LastWidth = 0, m_nv12LastHeight = 0;
int m_nv12LastColorspace = -1; int m_nv12LastColorspace = -1;
int m_nv12LastRange = -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 initSwsContext(int width, int height, AVPixelFormat pixFmt, AVPixelFormat outputPixFmt = AV_PIX_FMT_YUVJ420P);
void initNV12SwsContext(const AVFrame* frame); void initNV12SwsContext(const AVFrame* frame);
+21 -6
View File
@@ -197,17 +197,32 @@ BOOL h264_data_rx(H264RXI * p_rxi, uint8 * p_data, int len)
if (p_rxi->rtprxi.rxf_loss) if (p_rxi->rtprxi.rxf_loss)
{ {
// Packet loss, until the next start packet // Packet loss — resume ONLY at an access-unit boundary (see the
// H.265 counterpart in h265_rtp_rx.cpp for the full rationale).
if (!beginPacket) // 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; return FALSE;
} }
else
{
p_rxi->rtprxi.rxf_loss = 0; p_rxi->rtprxi.rxf_loss = 0;
} }
}
// save the H264 parameter sets // save the H264 parameter sets
+25 -6
View File
@@ -265,17 +265,36 @@ BOOL h265_data_rx(H265RXI * p_rxi, uint8 * p_data, int len)
if (p_rxi->rtprxi.rxf_loss) if (p_rxi->rtprxi.rxf_loss)
{ {
// Packet loss, until the next start packet // Packet loss — resume ONLY at an access-unit boundary.
//
if (!beginPacket) // 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; return FALSE;
} }
else
{
p_rxi->rtprxi.rxf_loss = 0; p_rxi->rtprxi.rxf_loss = 0;
} }
}
// save the H265 parameter sets // save the H265 parameter sets
+5
View File
@@ -106,6 +106,11 @@ BOOL rtp_data_rx(RTPRXI * p_rxi, uint8 * p_data, int len)
{ {
p_rxi->rxf_loss = 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->seq = rtpSeq;
+437
View File
@@ -0,0 +1,437 @@
"""Generate the ANSODEngine Tracker / Stabilization developer-guide PDF.
Run:
python build_anscore_devguide_pdf.py
Output:
ANSODEngine_Tracker_Stabilization_Guide.pdf (next to this script)
"""
from pathlib import Path
from reportlab.lib import colors
from reportlab.lib.enums import TA_LEFT
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
from reportlab.lib.units import mm
from reportlab.platypus import (
PageBreak,
Paragraph,
SimpleDocTemplate,
Spacer,
Table,
TableStyle,
)
OUTPUT = Path(__file__).with_name("ANSODEngine_Tracker_Stabilization_Guide.pdf")
# --------------------------------------------------------------------------- #
# Styles #
# --------------------------------------------------------------------------- #
styles = getSampleStyleSheet()
H1 = ParagraphStyle(
"H1", parent=styles["Heading1"],
fontName="Helvetica-Bold", fontSize=18, leading=22,
spaceBefore=4, spaceAfter=10, textColor=colors.HexColor("#0B3D91"),
)
H2 = ParagraphStyle(
"H2", parent=styles["Heading2"],
fontName="Helvetica-Bold", fontSize=13, leading=16,
spaceBefore=14, spaceAfter=6, textColor=colors.HexColor("#0B3D91"),
)
H3 = ParagraphStyle(
"H3", parent=styles["Heading3"],
fontName="Helvetica-Bold", fontSize=11, leading=14,
spaceBefore=10, spaceAfter=4, textColor=colors.HexColor("#222222"),
)
BODY = ParagraphStyle(
"Body", parent=styles["BodyText"],
fontName="Helvetica", fontSize=9.5, leading=13,
spaceBefore=2, spaceAfter=4, alignment=TA_LEFT,
)
NOTE = ParagraphStyle(
"Note", parent=BODY,
leftIndent=10, fontSize=9, textColor=colors.HexColor("#444444"),
)
CODE = ParagraphStyle(
"Code", parent=BODY,
fontName="Courier", fontSize=8.5, leading=11,
leftIndent=8, rightIndent=8, spaceBefore=4, spaceAfter=8,
backColor=colors.HexColor("#F4F4F4"),
borderColor=colors.HexColor("#D0D0D0"), borderWidth=0.5,
borderPadding=4,
)
TABLE_HEADER = ParagraphStyle(
"TH", parent=BODY, fontName="Helvetica-Bold", fontSize=9, leading=12,
textColor=colors.white,
)
TABLE_CELL = ParagraphStyle(
"TC", parent=BODY, fontSize=9, leading=12, spaceBefore=0, spaceAfter=0,
)
TABLE_CELL_MONO = ParagraphStyle(
"TCM", parent=TABLE_CELL, fontName="Courier", fontSize=8.5,
)
# --------------------------------------------------------------------------- #
# Helpers #
# --------------------------------------------------------------------------- #
def p(text, style=BODY):
return Paragraph(text, style)
def code_block(lines):
"""Multi-line code block, preserves indentation by replacing spaces with &nbsp;."""
body = "<br/>".join(
line.replace(" ", "&nbsp;").replace("<", "&lt;").replace(">", "&gt;")
for line in lines
)
# restore tag rendering for our own tags
body = body.replace("&lt;br/&gt;", "<br/>")
return Paragraph(body, CODE)
def make_table(header, rows, col_widths=None, mono_cols=None):
"""rows: list[list[str]]. mono_cols: indices that should use monospace."""
mono_cols = mono_cols or set()
def cell(text, col_idx):
style = TABLE_CELL_MONO if col_idx in mono_cols else TABLE_CELL
return Paragraph(text, style)
data = [[Paragraph(h, TABLE_HEADER) for h in header]]
for row in rows:
data.append([cell(c, i) for i, c in enumerate(row)])
t = Table(data, colWidths=col_widths, repeatRows=1)
t.setStyle(TableStyle([
("BACKGROUND", (0, 0), (-1, 0), colors.HexColor("#0B3D91")),
("TEXTCOLOR", (0, 0), (-1, 0), colors.white),
("ALIGN", (0, 0), (-1, -1), "LEFT"),
("VALIGN", (0, 0), (-1, -1), "TOP"),
("FONTSIZE", (0, 0), (-1, -1), 9),
("BOTTOMPADDING", (0, 0), (-1, 0), 6),
("TOPPADDING", (0, 0), (-1, 0), 6),
("BOTTOMPADDING", (0, 1), (-1, -1), 4),
("TOPPADDING", (0, 1), (-1, -1), 4),
("LEFTPADDING", (0, 0), (-1, -1), 5),
("RIGHTPADDING", (0, 0), (-1, -1), 5),
("ROWBACKGROUNDS", (0, 1), (-1, -1),
[colors.white, colors.HexColor("#F4F7FB")]),
("GRID", (0, 0), (-1, -1), 0.4, colors.HexColor("#B0B7C3")),
]))
return t
# --------------------------------------------------------------------------- #
# Document content #
# --------------------------------------------------------------------------- #
def build_story():
s = []
# Title
s.append(p(
"ANSODEngine &mdash; Tracker &amp; Stabilization Developer Guide",
H1,
))
s.append(p(
"Module: <b>ANSODEngine</b> &nbsp;&middot;&nbsp; "
"Base class: <b>ANSCENTER::ANSODBase</b> &nbsp;&middot;&nbsp; "
"Affects every OD engine subclass "
"(ANSRTYOLO, ANSONNXYOLO, ANSYOLOV12RTOD, etc.)",
NOTE,
))
s.append(Spacer(1, 6))
# ── 1. Default state after model load ─────────────────────────────────
s.append(p("1. Default state after model load", H2))
s.append(p(
"Every engine derived from <b>ANSODBase</b> finishes "
"<font face='Courier'>Initialize</font> / "
"<font face='Courier'>LoadModel</font> / "
"<font face='Courier'>LoadModelFromFolder</font> "
"with these defaults:"
))
s.append(make_table(
["Feature", "Default", "Source of default"],
[
["Tracker (BYTETrack / UCMC / OCSORT)",
"<b>ON</b>",
"Loaders auto-call SetTracker(BYTETRACK, true) "
"(ANSODEngine.cpp:77, 110, 139, 174, 246, 260)"],
["Stabilization (ghost injection + EMA + hysteresis + "
"class consistency)",
"<b>OFF</b>",
"Member initializer _stabilizationEnabled = false; "
"no auto-enable"],
],
col_widths=[55*mm, 18*mm, 95*mm],
))
s.append(Spacer(1, 4))
s.append(p(
"<b>Notes.</b> Tracker assigns a <font face='Courier'>trackId</font> "
"to detections and (when stabilization is on) lets the tracker "
"predict bridging &ldquo;ghost&rdquo; boxes. "
"Stabilization is opt-in and requires the tracker. "
"It cannot run standalone.",
NOTE,
))
# ── 2. Public setters &mdash; invariant table ─────────────────────────
s.append(p("2. Public setters &mdash; invariant table", H2))
s.append(p(
"The single invariant maintained by the API: "
"<b>stabilization requires tracker</b> "
"(<font face='Courier'>stab=true &rArr; tracker=true</font>)."
))
s.append(make_table(
["Call", "_trackerEnabled", "_stabilizationEnabled", "Why"],
[
["SetTracker(type, true)",
"&rarr; true",
"unchanged",
"Tracker is independently useful. Don&rsquo;t side-effect stab."],
["SetTracker(type, false)",
"&rarr; false",
"<b>&rarr; false</b> (forced)",
"Stabilization can&rsquo;t run without tracker. Forced clear "
"keeps the flag honest."],
["SetStabilization(true, hist, miss)",
"<b>&rarr; true</b> (auto if off)",
"&rarr; true",
"Stabilization needs tracker. Auto-enables it."],
["SetStabilization(false)",
"unchanged",
"&rarr; false",
"Tracker stays usable on its own."],
["SetStabilizationParameters(json)",
"unchanged",
"unchanged",
"Tunes thresholds only; does not toggle either flag."],
["SetTrackerParameters(json)",
"unchanged",
"unchanged",
"Tunes tracker params only; no-op if tracker is off."],
],
col_widths=[55*mm, 28*mm, 32*mm, 53*mm],
mono_cols={0},
))
# ── 3. Reachable runtime states ───────────────────────────────────────
s.append(p("3. Reachable runtime states", H2))
s.append(make_table(
["_trackerEnabled", "_stabilizationEnabled", "What runs at inference"],
[
["false", "false",
"Raw detections only. No trackId. No ghosts."],
["true", "false",
"Detections + trackId assignment. <b>No ghosts, no EMA, "
"no hysteresis.</b> &nbsp;&larr; default"],
["true", "true",
"Full pipeline: tracking + ghost injection + EMA confidence + "
"hysteresis + class consistency + stale-history pruning."],
["false", "true",
"<b>Unreachable</b> &mdash; invariant prevents it."],
],
col_widths=[35*mm, 42*mm, 91*mm],
mono_cols={0, 1},
))
# ── 4. Caller recipes ─────────────────────────────────────────────────
s.append(p("4. How to opt in / out (caller recipes)", H2))
s.append(make_table(
["Goal", "Sequence"],
[
["Default (tracker only, no stabilization)",
"Nothing. Just load the model."],
["Tracker only, explicit",
"SetTracker(BYTETRACK, true)"],
["Tracker + stabilization",
"SetStabilization(true, 20, 5) &nbsp; (auto-enables tracker)"],
["Tracker + stabilization with custom thresholds",
"SetStabilization(true) then SetStabilizationParameters(json)"],
["Disable everything",
"SetTracker(BYTETRACK, false) &nbsp; (also clears stabilization)"],
["Switch tracker type without losing stabilization",
"SetTracker(UCMC, true) then SetStabilization(true) "
"if it was on"],
],
col_widths=[70*mm, 98*mm],
mono_cols={1},
))
s.append(PageBreak())
# ── 5. Stabilization parameters ──────────────────────────────────────
s.append(p("5. Stabilization parameters", H2))
s.append(p(
"Apply only when stabilization is on. "
"<font face='Courier'>SetStabilization(enabled, historySize, "
"maxMisses)</font> arguments:"
))
s.append(make_table(
["Param", "Default", "Min / clamp", "Effect"],
[
["historySize", "20", "&ge; 5",
"Frames retained in _detectionQueue per camera."],
["maxMisses", "5", "&ge; 1",
"Frames a ghost may persist before pruning. "
"History pruned at 2&times;maxMisses unseen frames."],
],
col_widths=[30*mm, 18*mm, 22*mm, 98*mm],
mono_cols={0},
))
s.append(Spacer(1, 6))
s.append(p(
"<font face='Courier'>SetStabilizationParameters(json)</font> "
"&mdash; all keys optional:",
))
s.append(make_table(
["Key", "Default", "Range", "Purpose"],
[
["hysteresis_enter", "model threshold", "float, 0 = auto",
"Confidence needed for a <i>new</i> track to appear."],
["hysteresis_keep", "0.65 &times; enter", "float, 0 = auto",
"Lower threshold to <i>keep</i> an established track visible."],
["ema_alpha", "0.30", "[0.01, 1.0]",
"EMA weight for new observations (lower = smoother)."],
["track_boost_min_frames", "10", "&ge; 1",
"Frames before a track is &ldquo;established&rdquo; and gets "
"confidence boost."],
["track_boost_amount", "0.05", "[0.0, 0.5]",
"Confidence bonus for established tracks."],
["class_consistency_frames", "5", "&ge; 1",
"Consecutive frames of a new class required before accepting "
"a class switch on an established track."],
["confidence_decay", "0.85", "[0.1, 1.0]",
"Per-miss multiplier on ghost confidence."],
["min_confidence", "0.15", "[0.0, 1.0]",
"Floor below which ghosts are dropped."],
],
col_widths=[45*mm, 28*mm, 28*mm, 67*mm],
mono_cols={0},
))
# ── 6. Performance & memory ─────────────────────────────────────────
s.append(p("6. Performance &amp; memory characteristics", H2))
s.append(make_table(
["Aspect", "Tracker only", "Tracker + Stabilization"],
[
["Per-frame CPU cost",
"BYTETrack association, ~O(N&middot;M)",
"+ EMA / hysteresis / class-consistency loop O(N) + "
"history-prune O(K)"],
["Mutex acquisitions per inference",
"1 extra inside ApplyTracking",
"2 extra (ApplyTracking + StabilizeDetections) on the "
"engine&rsquo;s _mutex"],
["Result vector size",
"= real detections",
"= real detections + ghost objects in grace window"],
["Per-camera state",
"ANSMOT instance + BYTETrack removed_stracks_ "
"(capped at 1000)",
"+ _trackHistories (pruned at unseen &gt; 2&times;maxMisses) "
"+ _detectionQueue (capped at historySize)"],
["Bounded?",
"Yes &mdash; caps in BYTETrack (kRemovedCap=1000)",
"Yes &mdash; explicit prune in StabilizeDetections Phase 3"],
],
col_widths=[50*mm, 55*mm, 63*mm],
))
# ── 7. Diagnostic logging ────────────────────────────────────────────
s.append(p("7. Diagnostic logging", H2))
s.append(p(
"Every flag transition emits a DebugView line via "
"<font face='Courier'>ANS_DBG(\"ANSOD_StabState\", &hellip;)</font>:"
))
s.append(code_block([
"[ANSOD_StabState] this=<engine_ptr> "
"SetTracker(true,type=N) tracker=0->1 stab=0 (unchanged)",
"[ANSOD_StabState] this=<engine_ptr> "
"SetTracker(false) tracker=1->0 stab=1->0",
"[ANSOD_StabState] this=<engine_ptr> "
"SetStabilization(true,hist=20,miss=5) tracker=1->1 stab=0->1",
"[ANSOD_StabState] this=<engine_ptr> "
"SetStabilization(false) tracker=1 (unchanged) stab=1->0",
]))
s.append(p(
"Capture with DebugView (or any ANSCORE log) and "
"<font face='Courier'>grep ANSOD_StabState</font> to audit which "
"engine instances run with which flags in production.",
NOTE,
))
# ── 8. Behavior change vs previous build ─────────────────────────────
s.append(p("8. Behavior change vs. previous build", H2))
s.append(p(
"For developers updating from the prior version of ANSCORE."
))
s.append(make_table(
["Behavior", "Before", "After"],
[
["SetTracker(true) side-effect on stabilization",
"Auto-set stabilization to true",
"<b>No effect on stabilization</b>"],
["SetTracker(false) side-effect on stabilization",
"None (could leave stab=true while tracker=false)",
"<b>Forced stabilization to false</b>"],
["Default after model load",
"tracker=ON, stabilization=ON",
"tracker=ON, <b>stabilization=OFF</b>"],
["SetStabilization(true) auto-enable tracker",
"Yes",
"Yes (unchanged)"],
["SetStabilization(false) side-effect on tracker",
"None",
"None (unchanged)"],
],
col_widths=[60*mm, 53*mm, 55*mm],
))
s.append(Spacer(1, 6))
s.append(p(
"<b>Migration note.</b> If your application relied on the old "
"auto-enable to get stabilization for free, add an explicit "
"<font face='Courier'>SetStabilization(true)</font> call after "
"model load. ANSLPR&rsquo;s defensive "
"<font face='Courier'>SetStabilization(false)</font> calls remain "
"correct (now redundant but harmless).",
NOTE,
))
# ── Footer / version note ────────────────────────────────────────────
s.append(Spacer(1, 14))
s.append(p(
"<i>This guide reflects the ANSODEngine API as of the &lsquo;tracker/"
"stabilization invariant&rsquo; cleanup. File references point to "
"modules/ANSODEngine/ANSODEngine.cpp and "
"modules/ANSODEngine/ANSEngineCommon.h.</i>",
NOTE,
))
return s
# --------------------------------------------------------------------------- #
# Build #
# --------------------------------------------------------------------------- #
def main():
doc = SimpleDocTemplate(
str(OUTPUT),
pagesize=A4,
leftMargin=18*mm, rightMargin=18*mm,
topMargin=18*mm, bottomMargin=18*mm,
title="ANSODEngine — Tracker & Stabilization Developer Guide",
author="ANSCORE",
)
story = build_story()
doc.build(story)
print(f"Wrote {OUTPUT}")
if __name__ == "__main__":
main()
+951
View File
@@ -0,0 +1,951 @@
"""Generate the ANSLPR developer-guide PDF.
Covers two features:
1. ALPRChecker — tracker-based voting / text stabilization
2. Plate format validation — SetPlateFormat / SetPlateFormats
Run:
python build_anslpr_devguide_pdf.py
Output:
ANSLPR_ALPRChecker_PlateFormat_Guide.pdf (next to this script)
"""
from pathlib import Path
from reportlab.lib import colors
from reportlab.lib.enums import TA_LEFT
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
from reportlab.lib.units import mm
from reportlab.platypus import (
PageBreak,
Paragraph,
SimpleDocTemplate,
Spacer,
Table,
TableStyle,
)
OUTPUT = Path(__file__).with_name("ANSLPR_ALPRChecker_PlateFormat_Guide.pdf")
# --------------------------------------------------------------------------- #
# Styles #
# --------------------------------------------------------------------------- #
styles = getSampleStyleSheet()
H1 = ParagraphStyle(
"H1", parent=styles["Heading1"],
fontName="Helvetica-Bold", fontSize=18, leading=22,
spaceBefore=4, spaceAfter=10, textColor=colors.HexColor("#0B3D91"),
)
H2 = ParagraphStyle(
"H2", parent=styles["Heading2"],
fontName="Helvetica-Bold", fontSize=13, leading=16,
spaceBefore=14, spaceAfter=6, textColor=colors.HexColor("#0B3D91"),
)
H3 = ParagraphStyle(
"H3", parent=styles["Heading3"],
fontName="Helvetica-Bold", fontSize=11, leading=14,
spaceBefore=10, spaceAfter=4, textColor=colors.HexColor("#222222"),
)
BODY = ParagraphStyle(
"Body", parent=styles["BodyText"],
fontName="Helvetica", fontSize=9.5, leading=13,
spaceBefore=2, spaceAfter=4, alignment=TA_LEFT,
)
NOTE = ParagraphStyle(
"Note", parent=BODY,
leftIndent=10, fontSize=9, textColor=colors.HexColor("#444444"),
)
CODE = ParagraphStyle(
"Code", parent=BODY,
fontName="Courier", fontSize=8.5, leading=11,
leftIndent=8, rightIndent=8, spaceBefore=4, spaceAfter=8,
backColor=colors.HexColor("#F4F4F4"),
borderColor=colors.HexColor("#D0D0D0"), borderWidth=0.5,
borderPadding=4,
)
TABLE_HEADER = ParagraphStyle(
"TH", parent=BODY, fontName="Helvetica-Bold", fontSize=9, leading=12,
textColor=colors.white,
)
TABLE_CELL = ParagraphStyle(
"TC", parent=BODY, fontSize=9, leading=12, spaceBefore=0, spaceAfter=0,
)
TABLE_CELL_MONO = ParagraphStyle(
"TCM", parent=TABLE_CELL, fontName="Courier", fontSize=8.5,
)
# --------------------------------------------------------------------------- #
# Helpers #
# --------------------------------------------------------------------------- #
def p(text, style=BODY):
return Paragraph(text, style)
def code_block(lines):
body = "<br/>".join(
line.replace(" ", "&nbsp;").replace("<", "&lt;").replace(">", "&gt;")
for line in lines
)
body = body.replace("&lt;br/&gt;", "<br/>")
return Paragraph(body, CODE)
def make_table(header, rows, col_widths=None, mono_cols=None):
mono_cols = mono_cols or set()
def cell(text, col_idx):
style = TABLE_CELL_MONO if col_idx in mono_cols else TABLE_CELL
return Paragraph(text, style)
data = [[Paragraph(h, TABLE_HEADER) for h in header]]
for row in rows:
data.append([cell(c, i) for i, c in enumerate(row)])
t = Table(data, colWidths=col_widths, repeatRows=1)
t.setStyle(TableStyle([
("BACKGROUND", (0, 0), (-1, 0), colors.HexColor("#0B3D91")),
("TEXTCOLOR", (0, 0), (-1, 0), colors.white),
("ALIGN", (0, 0), (-1, -1), "LEFT"),
("VALIGN", (0, 0), (-1, -1), "TOP"),
("FONTSIZE", (0, 0), (-1, -1), 9),
("BOTTOMPADDING", (0, 0), (-1, 0), 6),
("TOPPADDING", (0, 0), (-1, 0), 6),
("BOTTOMPADDING", (0, 1), (-1, -1), 4),
("TOPPADDING", (0, 1), (-1, -1), 4),
("LEFTPADDING", (0, 0), (-1, -1), 5),
("RIGHTPADDING", (0, 0), (-1, -1), 5),
("ROWBACKGROUNDS", (0, 1), (-1, -1),
[colors.white, colors.HexColor("#F4F7FB")]),
("GRID", (0, 0), (-1, -1), 0.4, colors.HexColor("#B0B7C3")),
]))
return t
# --------------------------------------------------------------------------- #
# Document content #
# --------------------------------------------------------------------------- #
def build_story():
s = []
# ====================================================================
# Title
# ====================================================================
s.append(p(
"ANSLPR &mdash; ALPRChecker &amp; Plate Format Developer Guide",
H1,
))
s.append(p(
"Module: <b>ANSLPR</b> &nbsp;&middot;&nbsp; "
"Base class: <b>ANSCENTER::ANSALPR</b> &nbsp;&middot;&nbsp; "
"Affects every engine subclass "
"(<b>ANSALPR_OD</b>, <b>ANSALPR_OCR</b>, <b>ANSALPR_CPU</b>)",
NOTE,
))
s.append(Spacer(1, 6))
s.append(p(
"This guide documents two independent post-processing features in the "
"ANSLPR pipeline: the <b>ALPRChecker</b> (per-track majority voting + "
"lock + skip-when-locked) and the <b>plate format validator</b> "
"(<font face='Courier'>SetPlateFormat</font> / "
"<font face='Courier'>SetPlateFormats</font>). "
"They share neither state nor toggle; the format validator runs as a "
"filter <i>before</i> the ALPRChecker voter sees any text."
))
# ====================================================================
# PART 1 — ALPRChecker
# ====================================================================
s.append(p("Part 1 &mdash; ALPRChecker (tracker-based voting)", H1))
# ── 1.1 Purpose ──────────────────────────────────────────────────────
s.append(p("1.1 Purpose", H2))
s.append(p(
"Convert noisy per-frame OCR strings for the same physical plate "
"(&ldquo;50H 304.07&rdquo;, &ldquo;50H304O7&rdquo;, &ldquo;50H304.07&rdquo;&hellip;) "
"into a single stable <i>locked</i> text. The mechanism is layered:"
))
s.append(p(
"<b>track identity</b> &rarr; <b>rolling history per track</b> &rarr; "
"<b>recency-weighted majority vote</b> &rarr; <b>stable lock with "
"edit-distance noise rejection</b> &rarr; <b>periodic re-verify</b> "
"so wrong locks can self-correct."
))
# ── 1.2 Default state ────────────────────────────────────────────────
s.append(p("1.2 Default state", H2))
s.append(make_table(
["Flag", "Default", "Source"],
[
["_enableALPRChecker",
"<b>false</b> (opt-in)",
"Member initializer in ANSLPR.h "
"(<font face='Courier'>std::atomic&lt;bool&gt; "
"_enableALPRChecker{false}</font>)"],
["Full-frame auto-detect",
"engages after a few pixel-identical frames "
"(only relevant once enabled)",
"shouldUseALPRChecker / ImageSizeTracker"],
["Skip-when-locked re-verify period",
"<b>30 frames</b>",
"tryReuseLockedText default <font face='Courier'>reverifyEvery=30</font>"],
],
col_widths=[55*mm, 50*mm, 63*mm],
mono_cols={0},
))
s.append(Spacer(1, 4))
s.append(p(
"<b>Out of the box:</b> ALPRChecker is <b>OFF</b>. The pipeline emits "
"raw recognizer text per frame &mdash; no voting, no lock, no "
"skip-when-locked, no spatial dedup. Callers must opt in with "
"<font face='Courier'>SetALPRCheckerEnabled(true)</font> "
"(or <font face='Courier'>ANSALPR_SetALPRCheckerEnabled(h, 1)</font>) "
"to enable cross-frame stabilization. Once enabled, full-frame "
"auto-detect still gates it on per camera, so pre-cropped pipeline "
"calls remain stateless even when the flag is on.",
NOTE,
))
# ── 1.3 Public API ───────────────────────────────────────────────────
s.append(p("1.3 Public API", H2))
s.append(p("<b>C++ class API</b> (<font face='Courier'>ANSCENTER::ANSALPR</font>):"))
s.append(make_table(
["Method", "Effect"],
[
["void SetALPRCheckerEnabled(bool enable)",
"Atomic flip of <font face='Courier'>_enableALPRChecker</font>. "
"Logs <font face='Courier'>[ALPR_Checker]</font> with prev/new."],
["bool IsALPRCheckerEnabled() const",
"Atomic read. Logs the current value."],
],
col_widths=[80*mm, 88*mm],
mono_cols={0},
))
s.append(Spacer(1, 4))
s.append(p("<b>C exports</b> (LabVIEW / .NET / other clients):"))
s.append(make_table(
["Function", "Args", "Return"],
[
["ANSALPR_SetALPRCheckerEnabled",
"Handle, int enable (0|1)",
"1 OK / -1 invalid handle / 0 exception"],
["ANSALPR_GetALPRCheckerEnabled",
"Handle",
"1 enabled / 0 disabled / -1 invalid handle"],
],
col_widths=[60*mm, 55*mm, 53*mm],
mono_cols={0, 1},
))
# ── 1.4 State (data structures) ──────────────────────────────────────
s.append(p("1.4 Internal state (per ANSALPR instance)", H2))
s.append(code_block([
"class ALPRChecker {",
" int maxFrames = 60; // rolling history depth",
" int minVotesToStabilize = 3; // identical reads to lock",
" float iouMatchThreshold = 0.30f; // spatial match (legacy API)",
" std::recursive_mutex _mutex; // protects all maps",
"",
" // trackId-based primary path",
" unordered_map<string, unordered_map<int, TrackedPlateById>>",
" trackedPlatesById;",
"",
" // spatial-IoU legacy path",
" unordered_map<string, vector<TrackedPlate>> trackedPlates;",
"};",
"",
"struct TrackedPlateById {",
" int trackId;",
" deque<string> textHistory; // RAW reads, capped",
" string lockedText; // \"\" until locked",
" int lockCount;",
" int framesSinceLastSeen; // for prune",
" int framesSinceVerify; // for skip-when-locked",
"};",
]))
# ── 1.5 Algorithm ────────────────────────────────────────────────────
s.append(p("1.5 Core algorithm &mdash; checkPlateByTrackId", H2))
s.append(p(
"Primary entry point. Hybrid identity strategy: trackId hash lookup "
"(O(1) common case), Levenshtein-1 fallback for lost tracks.",
))
s.append(code_block([
"LOCK _mutex",
"if detectedPlate empty: return \"\"",
"",
"plates = trackedPlatesById[cameraId]",
"for (id, p) in plates: p.framesSinceLastSeen++ // age all",
"",
"// Periodic prune: every 30 calls, drop entries unseen > 180 frames",
"_pruneCounterById++",
"if _pruneCounterById >= 30 and plates.size() > 20:",
" drop entries where framesSinceLastSeen > maxFrames * 3",
"",
"// PRIMARY: O(1) trackId hit",
"if trackId in plates:",
" tp = plates[trackId]",
" tp.framesSinceLastSeen = 0",
" tp.textHistory.push_back(detectedPlate) // RAW",
" if size > maxFrames: pop_front()",
"",
" voted = majorityVote(tp.textHistory)",
"",
" if tp.lockedText empty:",
" if count(history == voted) >= minVotesToStabilize: // 3",
" tp.lockedText = voted // LOCK",
" return voted",
" else:",
" if Levenshtein(detectedPlate, lockedText) == 0:",
" tp.lockCount++; return lockedText",
" elif Levenshtein(voted, lockedText) > 1:",
" // Re-lock if 3 of last 6 votes agree on new text",
" if count(recent6 == voted) >= 3:",
" tp.lockedText = voted // RE-LOCK",
" return lockedText",
" return lockedText // emit lock, ignore noise",
"",
"// FALLBACK: trackId changed (occlusion, ID switch). Migrate.",
"for (id, p) in plates:",
" if Levenshtein(detectedPlate, p.lockedText) <= 1:",
" migrate p to plates[trackId]; erase old id",
" return p.lockedText",
" elif p.lockedText empty and any of last 3 history within edit-1:",
" migrate; return majorityVote(migrated.history)",
"",
"// No match anywhere: fresh entry",
"plates[trackId] = { trackId, history=[detectedPlate], locked=\"\" }",
"return detectedPlate",
]))
s.append(p("1.6 majorityVote", H3))
s.append(p(
"Recency-weighted vote with Levenshtein-1 group boosting. Newest "
"entries get weight 2.0, oldest 1.0. Variants within edit distance 1 "
"donate 0.5&times; their score to each candidate, so 1-character "
"OCR drift fuses instead of competing."
))
s.append(code_block([
"weightedFreq = {}",
"for i in 0..n-1:",
" weight = 1.0 + i / max(1, n-1) // newest=2.0, oldest=1.0",
" weightedFreq[history[i]] += weight",
"",
"for kv in weightedFreq:",
" total = kv.score",
" for other in weightedFreq:",
" if abs(len(kv) - len(other)) <= 1",
" and Levenshtein(kv, other) == 1:",
" total += other.score * 0.5",
" track best",
"return best",
]))
s.append(p("1.7 tryReuseLockedText &mdash; skip-when-locked fast path", H3))
s.append(p(
"Saves ~50&ndash;95&nbsp;ms per locked plate per frame by skipping "
"the OCR call entirely when a stable lock exists. Periodic re-verify "
"(every 30 frames by default) lets one OCR run through so a wrong "
"lock can self-correct via the normal voting path."
))
s.append(code_block([
"LOCK _mutex",
"tp = trackedPlatesById[cameraId][trackId] // miss -> return \"\"",
"if tp.lockedText empty: return \"\" // not locked yet",
"",
"if reverifyEvery > 0 and tp.framesSinceVerify >= reverifyEvery:",
" tp.framesSinceVerify = 0",
" return \"\" // signal: caller runs OCR this frame",
"",
"tp.framesSinceVerify++",
"tp.framesSinceLastSeen = 0 // critical: keep entry alive across",
" // long skip-when-locked windows",
"return tp.lockedText // skip OCR; emit lock directly",
]))
# ── 1.8 Pipeline integration ─────────────────────────────────────────
s.append(PageBreak())
s.append(p("1.8 Pipeline integration", H2))
s.append(p(
"<b>Pipeline mode always bypasses the ALPRChecker, regardless of "
"how <font face='Courier'>_enableALPRChecker</font> is set.</b> Two "
"independent mechanisms enforce this:"
))
s.append(p(
"<b>Mechanism 1 &mdash; dedicated batch entry point (hard bypass).</b> "
"<font face='Courier'>ANSALPR_RunInferencesBatch_LV</font> / "
"<font face='Courier'>_LV_V2</font> "
"(<font face='Courier'>dllmain.cpp:836, 1405</font>) routes through "
"<font face='Courier'>engine-&gt;RunInferencesBatch(...)</font>. The "
"implementations at "
"<font face='Courier'>ANSALPR_OD::RunInferencesBatch</font> "
"(line 2637) and "
"<font face='Courier'>ANSALPR_OCR::RunInferencesBatch</font> "
"(line 1863) contain <b>zero references</b> to the checker &mdash; "
"no <font face='Courier'>shouldUseALPRChecker</font>, no "
"<font face='Courier'>checkPlateByTrackId</font>, no "
"<font face='Courier'>tryReuseLockedText</font>, no "
"<font face='Courier'>ensureUniquePlateText</font>. The flag is "
"never read on this path. Header contract at "
"<font face='Courier'>ANSLPR.h:163-178</font>: "
"&ldquo;Tracker, voting, spatial dedup, and per-camera "
"accumulating state are all bypassed.&rdquo;"
))
s.append(p(
"<b>Mechanism 2 &mdash; auto-detect floors on the regular "
"<font face='Courier'>RunInference</font> path.</b> "
"<font face='Courier'>shouldUseALPRChecker</font> applies a chain "
"of gates; the last two reject pipeline crops even when the "
"flag is on:"
))
s.append(code_block([
"if !_enableTrackerVoting: return false // OD master (1)",
"if !_enableALPRChecker.load(relaxed): return false // user flag (2)",
"if imageSize.width < MIN_FULLFRAME_WIDTH: return false // HARD FLOOR (3)",
" // = 1000 px",
"tracker = _imageSizeTrackers[cameraId]",
"if imageSize == tracker.lastSize: // CONSISTENCY (4)",
" if ++tracker.consistentCount >= CONFIRM_THRESHOLD: // = 5 frames",
" tracker.detectedFullFrame = true",
"else:",
" tracker.lastSize = imageSize; consistentCount = 1 // reset on any",
" tracker.detectedFullFrame = false // mismatch",
"return tracker.detectedFullFrame",
]))
s.append(p(
"Gates (3) and (4) make the bypass automatic for cropped inputs:"
))
s.append(make_table(
["Pipeline indicator", "Effect", "Independent of flag?"],
[
["<font face='Courier'>width &lt; 1000 px</font> "
"(<font face='Courier'>MIN_FULLFRAME_WIDTH</font>)",
"Returns <font face='Courier'>false</font> immediately, "
"before the auto-detect state machine runs.",
"<b>Yes</b> &mdash; rejects all small crops unconditionally "
"after the flag check passes."],
["Frame size varies between calls",
"<font face='Courier'>consistentCount</font> resets to 1 on "
"every mismatch; <font face='Courier'>detectedFullFrame</font> "
"stays <font face='Courier'>false</font>. Pre-cropped vehicle "
"ROIs (e.g. 496&times;453, 497&times;455, &hellip;) never "
"accumulate consistency.",
"<b>Yes</b> &mdash; the consistency requirement is independent "
"of the user flag."],
],
col_widths=[60*mm, 70*mm, 38*mm],
))
s.append(Spacer(1, 4))
s.append(p(
"<b>Practical guarantee.</b> Even if a caller sets "
"<font face='Courier'>SetALPRCheckerEnabled(true)</font> and feeds "
"pipeline crops, voting stays off per camera because the auto-detect "
"never flips <font face='Courier'>detectedFullFrame</font> to true. "
"Mixing full-frame and pipeline calls on different cameras works "
"cleanly: the per-camera <font face='Courier'>_imageSizeTrackers</font> "
"entry tracks each camera's mode independently.",
NOTE,
))
s.append(p("Caller path &times; flag matrix", H3))
s.append(make_table(
["Caller path", "Input characteristic",
"Flag = OFF", "Flag = ON"],
[
["<font face='Courier'>RunInferencesBatch</font> "
"(pipeline batch, LV/V2)",
"any size, any consistency",
"<b>bypassed</b> (hard)",
"<b>bypassed</b> (hard, flag never read)"],
["<font face='Courier'>RunInference</font>",
"full video frames "
"(&ge; 1000 px, consistent size)",
"bypassed",
"active after 5 consistent frames"],
["<font face='Courier'>RunInference</font>",
"cropped vehicle ROI "
"(&lt; 1000 px)",
"bypassed",
"<b>bypassed by MIN_FULLFRAME_WIDTH floor</b>"],
["<font face='Courier'>RunInference</font>",
"varying-size crops "
"(&ge; 1000 px but inconsistent)",
"bypassed",
"<b>bypassed by consistency reset</b>"],
],
col_widths=[55*mm, 50*mm, 30*mm, 33*mm],
mono_cols={2, 3},
))
s.append(Spacer(1, 4))
s.append(p(
"In effect, the flag answers a single question: <i>&ldquo;if the "
"caller is sending real video frames from this camera, should I "
"stabilize the output?&rdquo;</i> It has no effect on pipeline / "
"cropped inputs &mdash; those are always raw OCR.",
NOTE,
))
s.append(p("1.9 OCR engine wiring (per frame)", H3))
s.append(code_block([
"useChecker = shouldUseALPRChecker(frameSize, cameraId) // once",
"",
"for each detected plate box:",
" if useChecker:",
" locked = tryReuseLockedText(cam, trackId)",
" if locked not empty:",
" emit { className=locked, +color }; SKIP CROP",
" continue",
" build crop(s) (padded, optionally rectified, top/bottom split)",
"",
"ONE batched recognizer call across all crops",
"",
"for each plate (non-skipped):",
" text = StripPlateArtifacts(top + \" \" + bottom)",
" if Country == JAPAN and IsJapaneseIncomplete(text):",
" text = recover kana from bottom-half tile",
" text = StripPlateSeparators(text)",
" text = AnalyseLicensePlateText(text) // <-- format filter",
" if text empty: continue // (drops invalid)",
"",
" if useChecker:",
" className = checkPlateByTrackId(cam, text, trackId)",
" else:",
" className = text // raw",
" emit { className, +color }",
"",
"if useChecker:",
" ensureUniquePlateText(output, cam) // spatial dedup",
]))
s.append(p("1.10 OD engine wiring", H3))
s.append(p(
"Same gate at every <font face='Courier'>alprChecker.checkPlateByTrackId</font> "
"call. The OD path doesn't use skip-when-locked because it already "
"runs character classification per detection &mdash; nothing to skip. "
"Note OD also has a separate master "
"<font face='Courier'>_enableTrackerVoting</font> "
"flag checked <i>before</i> <font face='Courier'>_enableALPRChecker</font>; "
"if that one is off, the disabled-path applies regardless."
))
# ── 1.11 Disabled mode ──────────────────────────────────────────────
s.append(p("1.11 Disabled mode (default) vs enabled &mdash; what runs", H2))
s.append(make_table(
["Stage", "Enabled (opt-in)", "Disabled (default)"],
[
["LP detection",
"Runs", "Runs"],
["ROI cropping + 5% padding",
"Runs", "Runs"],
["Japan rectification + kana recovery",
"Runs (Japan only)", "Runs (Japan only)"],
["Batched recognizer call",
"Runs", "Runs"],
["StripPlateArtifacts / StripPlateSeparators",
"Runs", "Runs"],
["AnalyseLicensePlateText (format filter)",
"Runs (independent toggle)", "Runs (independent toggle)"],
["Skip-when-locked (tryReuseLockedText)",
"Runs", "<b>Bypassed</b>"],
["Per-track voting (checkPlateByTrackId)",
"Runs", "<b>Bypassed</b> &mdash; raw recognizer text"],
["Spatial dedup (ensureUniquePlateText)",
"Runs", "<b>Bypassed</b>"],
["trackedPlatesById state",
"Populated and pruned", "Untouched (existing entries frozen)"],
],
col_widths=[60*mm, 53*mm, 55*mm],
))
s.append(Spacer(1, 4))
s.append(p(
"<b>Cost.</b> Disabling skip-when-locked adds ~50&ndash;95&nbsp;ms "
"per locked plate per frame because every plate now goes through "
"OCR every frame.",
NOTE,
))
# ── 1.12 Thread safety ──────────────────────────────────────────────
s.append(p("1.12 Thread safety", H2))
s.append(make_table(
["Concern", "Resolution"],
[
["Toggle race between config thread and inference thread",
"<font face='Courier'>_enableALPRChecker</font> is "
"<font face='Courier'>std::atomic&lt;bool&gt;</font>; "
"store/load use <font face='Courier'>memory_order_relaxed</font>. "
"Tear-free, no fence cost."],
["Concurrent track map mutation",
"<font face='Courier'>ALPRChecker::_mutex</font> "
"(recursive) wraps every read &amp; write of "
"<font face='Courier'>trackedPlatesById</font> / "
"<font face='Courier'>trackedPlates</font>."],
["Concurrent spatial dedup state",
"<font face='Courier'>_plateIdentitiesMutex</font> in "
"<font face='Courier'>ANSALPR_OCR</font> protects "
"<font face='Courier'>_plateIdentities</font>."],
["Toggle while OCR is in flight",
"Safe. The in-flight frame uses whichever value it observed; "
"the next frame picks up the new value."],
],
col_widths=[60*mm, 108*mm],
))
# ── 1.13 Diagnostic logging ─────────────────────────────────────────
s.append(p("1.13 Diagnostic logging (DebugView)", H2))
s.append(make_table(
["Tag", "Emitted by"],
[
["[ALPR_Checker]",
"Set/Get accessors and C-export wrappers — every flag transition "
"and every read."],
["[ALPR_TrackId]",
"checkPlateByTrackId — LOCKED, RE-LOCK, MIGRATED, "
"noise, NEW; tryReuseLockedText — SKIP-LOCKED, REVERIFY."],
["[ALPR_OCR_Checker]",
"shouldUseALPRChecker — full-frame vs pipeline mode auto-detect "
"transitions per camera."],
["[ALPR_Format]",
"AnalyseLicensePlateText — REJECT lines when a plate doesn't "
"match any configured format (see Part 2)."],
["[OCR_Leak]",
"Once-per-60s heartbeat with sizes of each map — useful for "
"spotting unbounded growth in production."],
],
col_widths=[40*mm, 128*mm],
mono_cols={0},
))
s.append(Spacer(1, 4))
s.append(p(
"Tags are gated behind <font face='Courier'>ANSCORE_DEBUGVIEW</font> "
"and <font face='Courier'>ANSCENTER_IsDebugViewEnabled()</font>. "
"Filter <i>DebugView</i> on the bracketed tag to isolate any layer.",
NOTE,
))
# ====================================================================
# PART 2 — Plate format
# ====================================================================
s.append(PageBreak())
s.append(p("Part 2 &mdash; Plate format validation", H1))
# ── 2.1 Purpose ──────────────────────────────────────────────────────
s.append(p("2.1 Purpose", H2))
s.append(p(
"Reject OCR output that doesn't match any configured plate pattern. "
"Acts as a hard filter <b>upstream</b> of the ALPRChecker voter, so "
"malformed reads never enter <font face='Courier'>textHistory</font> "
"and a track never locks on garbage."
))
# ── 2.2 Pattern syntax ───────────────────────────────────────────────
s.append(p("2.2 Pattern syntax", H2))
s.append(p(
"Patterns are simple character templates. Each character in the "
"pattern matches one character in the candidate plate; lengths must "
"match exactly."
))
s.append(make_table(
["Token", "Matches", "Notes"],
[
["d", "Any digit (<font face='Courier'>0&ndash;9</font>)",
"<font face='Courier'>std::isdigit</font>"],
["l", "Any letter (<font face='Courier'>A&ndash;Z, a&ndash;z</font>)",
"<font face='Courier'>std::isalpha</font>. Plate text is "
"uppercased before matching."],
["any other char",
"Literal &mdash; must match exactly",
"Use uppercase literals (e.g. <font face='Courier'>M</font>, "
"<font face='Courier'>D</font>, <font face='Courier'>NG</font>) "
"since the plate is uppercased first."],
],
col_widths=[20*mm, 80*mm, 68*mm],
mono_cols={0},
))
s.append(Spacer(1, 4))
s.append(p(
"<b>Empty format list</b> = no validation; all plates accepted "
"(<font face='Courier'>MatchesPlateFormat</font> returns true). "
"Calling <font face='Courier'>SetPlateFormats(emptyList)</font> "
"disables filtering.",
NOTE,
))
s.append(p("2.3 Examples", H3))
s.append(make_table(
["Pattern", "Matches", "Doesn't match"],
[
["ddlddddd",
"30A12345 &nbsp;|&nbsp; 51F67890",
"30A1234 (too short) &nbsp;|&nbsp; 30AA12345 "
"(2nd letter where digit expected)"],
["lldddd",
"AB1234 &nbsp;|&nbsp; QC9876",
"A12345 (1st char must be letter) &nbsp;|&nbsp; AB12345 "
"(too long)"],
["dddddNGdd",
"12345NG67",
"12345NN67 (literal mismatch)"],
["ddMDdddddd",
"12MD345678",
"12mD345678 (case mismatch on literal &mdash; plate is "
"uppercased, so 'D' must be 'D')"],
],
col_widths=[35*mm, 65*mm, 68*mm],
mono_cols={0, 1, 2},
))
# ── 2.4 Default formats per country ─────────────────────────────────
s.append(p("2.4 Default formats per country", H2))
s.append(p(
"<font face='Courier'>ApplyCountryDefaultPlateFormats()</font> "
"populates the format list automatically based on "
"<font face='Courier'>_country</font>. Called at the end of "
"<font face='Courier'>Initialize()</font> (after country.txt is "
"read) and from <font face='Courier'>SetCountry()</font>, so a "
"runtime country switch picks up the right format set on the very "
"next frame."
))
s.append(make_table(
["Country", "Formats installed"],
[
["VIETNAM",
"ddlddddd, ddldddd, ddldddddd, ddllddddd, ddMDdddddd, "
"dddddNGdd, dddddQTdd, dddddCVdd, dddddNNdd, lldddd "
"(11 patterns covering car / motorbike / military / "
"diplomatic / police)"],
["JAPAN, CHINA, AUSTRALIA, USA, INDONESIA, &hellip;",
"<i>Empty</i> &mdash; no validation. "
"<font face='Courier'>AnalyseLicensePlateText</font> is also "
"a near-no-op for non-Vietnam countries (preserves kana / "
"kanji / region-specific punctuation verbatim)."],
],
col_widths=[55*mm, 113*mm],
))
# ── 2.5 Public API ──────────────────────────────────────────────────
s.append(p("2.5 Public API", H2))
s.append(p("<b>C++ class API</b> (<font face='Courier'>ANSCENTER::ANSALPR</font>):"))
s.append(make_table(
["Method", "Effect"],
[
["void SetPlateFormat(const string&amp; format)",
"Replace the list with a single pattern."],
["void SetPlateFormats(const vector&lt;string&gt;&amp; formats)",
"Replace the entire list. Pass empty vector to disable validation."],
["vector&lt;string&gt; GetPlateFormats() const",
"Returns the current list."],
["virtual void SetCountry(Country c)",
"Calls ApplyCountryDefaultPlateFormats() so the list resets to "
"the country's defaults."],
],
col_widths=[80*mm, 88*mm],
mono_cols={0},
))
s.append(Spacer(1, 4))
s.append(p("<b>C exports</b>:"))
s.append(make_table(
["Function", "Args", "Return"],
[
["ANSALPR_SetFormat",
"Handle, const char* format",
"1 OK / -1 invalid handle / 0 exception"],
["ANSALPR_SetFormats",
"Handle, const char* formats "
"(<i>semicolon-separated</i>, e.g. \"ddlddddd;ddldddd;lldddd\")",
"1 OK / -1 invalid handle / 0 exception"],
["ANSALPR_GetFormats",
"Handle, LStrHandle outFormats",
"Count written, semicolon-separated"],
["ANSALPR_SetCountry",
"Handle, int country "
"(0=VN, 1=CN, 2=AU, 3=US, 4=ID, 5=JP)",
"1 OK / -1 invalid handle / 0 exception"],
],
col_widths=[55*mm, 65*mm, 48*mm],
mono_cols={0, 1},
))
# ── 2.6 Where it runs ───────────────────────────────────────────────
s.append(PageBreak())
s.append(p("2.6 Where it runs in the pipeline", H2))
s.append(p(
"Sequential post-processing inside the per-plate loop &mdash; "
"<b>before</b> the ALPRChecker voter:"
))
s.append(code_block([
"topText, bottomText = OCR results per row",
"combinedText = StripPlateArtifacts(top + \" \" + bottom) // °, ○, screws",
"[Japan] kana recovery if hiragana missing",
"combinedText = StripPlateSeparators(combinedText) // -, comma, ws",
" v",
"combinedText = AnalyseLicensePlateText(combinedText) // <-- PART 2",
" 1. keep only alnum chars",
" 2. uppercase",
" 3. if _plateFormats not empty:",
" if no pattern matches: log REJECT, return \"\"",
" return cleaned plate",
" v",
"if combinedText empty: continue // dropped — voter never sees it",
" v",
"if useChecker: // <-- PART 1",
" className = checkPlateByTrackId(cam, combinedText, trackId)",
"else:",
" className = combinedText",
]))
s.append(Spacer(1, 4))
s.append(p(
"<b>Consequence.</b> When a format list is configured, the voter's "
"<font face='Courier'>textHistory</font> contains only format-valid "
"strings. <font face='Courier'>lockedText</font> is therefore "
"always format-valid by construction. A track that consistently "
"produces format-invalid OCR will never lock &mdash; it just never "
"reaches the voter, and no output is emitted for that plate.",
NOTE,
))
# ── 2.7 Country interaction ─────────────────────────────────────────
s.append(p("2.7 Country interaction", H2))
s.append(p(
"<font face='Courier'>AnalyseLicensePlateText</font> behaves "
"differently per country (see "
"<font face='Courier'>ANSALPR_CPU.cpp</font>, "
"<font face='Courier'>ANSALPR_OCR.cpp</font>, "
"<font face='Courier'>ANSALPR_OD.cpp</font>):"
))
s.append(make_table(
["Country", "AnalyseLicensePlateText behavior"],
[
["VIETNAM",
"Strips non-alnum, uppercases, applies suburb-code remapping "
"(13&rarr;73, 42&rarr;62, 44&rarr;64, &hellip;), then validates "
"against <font face='Courier'>_plateFormats</font>. Mismatch &rarr; \"\""],
["Other countries",
"Returns input unchanged so kana / kanji / region punctuation "
"flow through verbatim. <font face='Courier'>_plateFormats</font> "
"is empty by default for these countries; even if you populate "
"it manually, the format check still runs in OCR (same code "
"path) but is currently a no-op for non-Vietnam in OD."],
],
col_widths=[35*mm, 133*mm],
))
# ── 2.8 Combined behavior matrix ────────────────────────────────────
s.append(p("2.8 ALPRChecker &times; format combined behavior", H2))
s.append(make_table(
["Format set", "ALPRChecker", "Behavior"],
[
["empty",
"OFF",
"Raw OCR every frame. No filter, no voting. "
"Highest noise, highest recall."],
["empty",
"ON",
"Voted/locked text. Voter learns from raw recognizer output "
"&mdash; can lock on bad reads if they're consistent."],
["configured",
"OFF",
"<b>Default for Vietnam.</b> Format-filtered raw OCR per frame. "
"Invalid reads dropped, valid ones emitted as-is "
"(no cross-frame memory)."],
["configured",
"ON",
"<b>Recommended for stable cameras.</b> Format filters bad "
"reads out, voter locks the surviving canonical text. Best "
"stability, lowest false-positive rate. Opt in with "
"<font face='Courier'>SetALPRCheckerEnabled(true)</font>."],
],
col_widths=[35*mm, 30*mm, 103*mm],
mono_cols={0, 1},
))
s.append(Spacer(1, 4))
s.append(p(
"<b>Toggling at runtime.</b> Both flags are independent and "
"thread-safe. Existing locks survive a format-set change &mdash; "
"they're not retroactively re-validated. To force a clean slate, "
"destroy and recreate the ANSALPR handle (or wait for the prune "
"pass to expire stale tracks).",
NOTE,
))
# ── 2.9 Caller recipes ──────────────────────────────────────────────
s.append(p("2.9 Caller recipes", H2))
s.append(make_table(
["Goal", "C export sequence"],
[
["Default pipeline (raw OCR + format filter, no voting)",
"Nothing &mdash; defaults are fine."],
["Enable tracker voting + skip-when-locked",
"ANSALPR_SetALPRCheckerEnabled(h, 1)"],
["Switch country at runtime",
"ANSALPR_SetCountry(h, 5) &nbsp; // Japan; resets formats"],
["Vietnam-only police plates",
"ANSALPR_SetFormats(h, \"dddddCVdd\")"],
["Vietnam plus a custom long-form pattern",
"ANSALPR_SetFormats(h, \"ddlddddd;ddldddd;dddddCVdd;ddldddddddd\")"],
["Disable format validation entirely",
"ANSALPR_SetFormats(h, \"\") &nbsp; // empty -> accept all"],
["Disable tracker voting (revert to raw OCR every frame)",
"ANSALPR_SetALPRCheckerEnabled(h, 0)"],
["Read current state",
"ANSALPR_GetALPRCheckerEnabled(h); ANSALPR_GetFormats(h, &amp;buf)"],
],
col_widths=[68*mm, 100*mm],
mono_cols={1},
))
# ── 2.10 Diagnostic logging ─────────────────────────────────────────
s.append(p("2.10 Diagnostic logging (DebugView)", H2))
s.append(code_block([
"[ANSLPR] ANSALPR_SetFormat: HandlePtr=0x... format=ddlddddd",
"[ANSLPR] ANSALPR_SetFormats: HandlePtr=0x... formats=ddlddddd;lldddd",
"[ANSLPR] ANSALPR_SetCountry: HandlePtr=0x... country=0",
"[ALPR_Format] REJECT plate='30AA1234' (no _plateFormats match)",
]))
s.append(p(
"Filter on <font face='Courier'>[ALPR_Format]</font> to count how "
"many plates the format filter is rejecting per camera per minute. "
"A high reject rate with a low ALPR result count means the format "
"list is too tight for what the recognizer is producing.",
NOTE,
))
# ── Footer ──────────────────────────────────────────────────────────
s.append(Spacer(1, 14))
s.append(p(
"<i>This guide reflects the ANSLPR API as of the ALPRChecker atomic "
"+ getter export change. File references point to "
"modules/ANSLPR/ANSLPR.{h,cpp}, modules/ANSLPR/ANSLPR_OCR.{h,cpp}, "
"modules/ANSLPR/ANSLPR_OD.{h,cpp}, modules/ANSLPR/ANSLPR_CPU.cpp, "
"and modules/ANSLPR/dllmain.cpp.</i>",
NOTE,
))
return s
# --------------------------------------------------------------------------- #
# Build #
# --------------------------------------------------------------------------- #
def main():
doc = SimpleDocTemplate(
str(OUTPUT),
pagesize=A4,
leftMargin=18*mm, rightMargin=18*mm,
topMargin=18*mm, bottomMargin=18*mm,
title="ANSLPR — ALPRChecker & Plate Format Developer Guide",
author="ANSCORE",
)
story = build_story()
doc.build(story)
print(f"Wrote {OUTPUT}")
if __name__ == "__main__":
main()
+53 -1
View File
@@ -78,6 +78,30 @@ else()
endif() endif()
target_compile_definitions(onnxruntime INTERFACE ORT_API_MANUAL_INIT) 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) ──────────────────────────── # ── TensorRT (Windows + Linux only) ────────────────────────────
if(NOT APPLE) if(NOT APPLE)
set(TENSORRT_INCLUDE_DIR "${ANSLIBS_DIR}/TensorRT/include") set(TENSORRT_INCLUDE_DIR "${ANSLIBS_DIR}/TensorRT/include")
@@ -86,6 +110,9 @@ if(NOT APPLE)
add_library(tensorrt INTERFACE) add_library(tensorrt INTERFACE)
target_include_directories(tensorrt INTERFACE ${TENSORRT_INCLUDE_DIR}) target_include_directories(tensorrt INTERFACE ${TENSORRT_INCLUDE_DIR})
target_link_directories(tensorrt INTERFACE ${TENSORRT_LIB_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) if(WIN32)
target_link_libraries(tensorrt INTERFACE nvinfer_plugin_10.lib) target_link_libraries(tensorrt INTERFACE nvinfer_plugin_10.lib)
else() else()
@@ -211,9 +238,34 @@ endif()
# ── Intel MKL ─────────────────────────────────────────────────── # ── Intel MKL ───────────────────────────────────────────────────
add_library(mkl INTERFACE) add_library(mkl INTERFACE)
if(WIN32) if(WIN32)
set(MKL_LIB_DIR "C:/Program Files (x86)/Intel/oneAPI/mkl/2025.0/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_directories(mkl INTERFACE "${MKL_LIB_DIR}")
target_link_libraries(mkl INTERFACE mkl_rt.lib) 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() else()
# Try system MKL on Linux # Try system MKL on Linux
find_library(MKL_RT_LIB mkl_rt PATHS /opt/intel/oneapi/mkl/latest/lib/intel64) 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."); ") returned null — the DLL may be corrupt.");
s_ort_api = api; s_ort_api = api;
Ort::Global<void>::api_ = api; Ort::InitApi(api);
std::cout << "[EPLoader] ORT loaded successfully." << std::endl; std::cout << "[EPLoader] ORT loaded successfully." << std::endl;
std::cout << "[EPLoader] ORT DLL version : " << base->GetVersionString() << std::endl; std::cout << "[EPLoader] ORT DLL version : " << base->GetVersionString() << std::endl;
File diff suppressed because it is too large Load Diff
+281 -35
View File
@@ -32,34 +32,32 @@
// filesystem check runs at most once per ~2 s. // filesystem check runs at most once per ~2 s.
extern "C" ANSLICENSE_API int ANSCENTER_IsDebugViewEnabled(void); extern "C" ANSLICENSE_API int ANSCENTER_IsDebugViewEnabled(void);
// C-linkage facade over SPDLogger so consumers can dynamic-load these via
// GetProcAddress without depending on the C++ class ABI. Each call routes to
// the process-wide SPDLogger singleton (thread-safe magic-static init).
extern "C" {
ANSLICENSE_API void ANSLogger_LogInfo (const char* src, const char* msg, const char* file, int line);
ANSLICENSE_API void ANSLogger_LogError(const char* src, const char* msg, const char* file, int line);
ANSLICENSE_API void ANSLogger_LogFatal(const char* src, const char* msg, const char* file, int line);
// DebugView choke point shared by ANS_DBG and ANSLIB_DBG. Strips noise
// path prefixes before emitting to OutputDebugStringA + stderr.
ANSLICENSE_API void ANSCENTER_DebugLog(const char* preformattedLine);
}
// ANS_DBG: Debug logging macro for DebugView (OutputDebugStringA on Windows). // ANS_DBG: Debug logging macro for DebugView (OutputDebugStringA on Windows).
// Usage: ANS_DBG("MyModule", "value=%d ptr=%p", val, ptr); // Usage: ANS_DBG("MyModule", "value=%d ptr=%p", val, ptr);
// Output: [MyModule] value=42 ptr=0x1234 // Output: [MyModule] value=42 ptr=0x1234
// NOTE: We avoid #include <windows.h> here to prevent winsock.h/winsock2.h //
// conflicts. Instead, forward-declare OutputDebugStringA directly. // Routes through ANSCENTER_DebugLog so noise path prefixes (e.g. the
#if ANSCORE_DEBUGVIEW && defined(_WIN32) // ProgramData profile dir) are stripped centrally before emit. The macro just
extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const char* lpOutputString); // formats on the caller's stack and hands off the buffer.
// Emit to BOTH DebugView (OutputDebugStringA) AND stderr so console apps #if ANSCORE_DEBUGVIEW
// (ANSCV-UnitTest, ANSLPR-UnitTest, ANSUtilities_test, etc.) show the message
// without needing to attach DebugView. fputs + fflush keeps lines atomic even
// when multiple threads log concurrently.
#define ANS_DBG(tag, fmt, ...) do { \ #define ANS_DBG(tag, fmt, ...) do { \
if (ANSCENTER_IsDebugViewEnabled()) { \ if (ANSCENTER_IsDebugViewEnabled()) { \
char _ans_dbg_buf[1024]; \ char _ans_dbg_buf[1024]; \
snprintf(_ans_dbg_buf, sizeof(_ans_dbg_buf), "[" tag "] " fmt "\n", ##__VA_ARGS__); \ snprintf(_ans_dbg_buf, sizeof(_ans_dbg_buf), "[" tag "] " fmt "\n", ##__VA_ARGS__); \
OutputDebugStringA(_ans_dbg_buf); \ ANSCENTER_DebugLog(_ans_dbg_buf); \
fputs(_ans_dbg_buf, stderr); \
fflush(stderr); \
} \
} while(0)
#elif ANSCORE_DEBUGVIEW
// Non-Windows: stderr only.
#define ANS_DBG(tag, fmt, ...) do { \
if (ANSCENTER_IsDebugViewEnabled()) { \
char _ans_dbg_buf[1024]; \
snprintf(_ans_dbg_buf, sizeof(_ans_dbg_buf), "[" tag "] " fmt "\n", ##__VA_ARGS__); \
fputs(_ans_dbg_buf, stderr); \
fflush(stderr); \
} \ } \
} while(0) } while(0)
#else #else
@@ -243,7 +241,9 @@ namespace ANSCENTER
ONNXSAM3=28, ONNXSAM3=28,
RTSAM3 = 29, RTSAM3 = 29,
ONNXYOLO = 30, ONNXYOLO = 30,
RTYOLO = 31 RTYOLO = 31,
ONNXRTMPOSE=32,
RTRTMPOSE=33
}; };
enum TrackerType { enum TrackerType {
BYTETRACK = 0, BYTETRACK = 0,
@@ -259,7 +259,9 @@ namespace ANSCENTER
ANS_OCR = 1005, ANS_OCR = 1005,
ANS_ALPR =1006, ANS_ALPR =1006,
ANS_CV = 1007, ANS_CV = 1007,
ANS_TS = 1008 ANS_TS = 1008,
ANSMIND = 1009,
ANSGW = 1010
}; };
enum ANSLICENSESTATUS { enum ANSLICENSESTATUS {
ACTIVE = 0, ACTIVE = 0,
@@ -268,18 +270,28 @@ namespace ANSCENTER
UNLICENSE = 3 UNLICENSE = 3
}; };
struct ModelConfig { struct ModelConfig {
DetectionType detectionType; // All POD members default-initialized. Several callers (notably
ModelType modelType; // CreateANSODHandle in ANSODEngine/dllmain.cpp) declare a local
PrecisionType precisionType; // ModelConfig and only set a subset of fields before passing it
int inpWidth; // to ANSODBase::Initialize. Without these defaults inpWidth /
int inpHeight; // inpHeight enter Initialize() as indeterminate stack values; the
float modelConfThreshold; // existing `<=0 → 640` guard in ANSONNXYOLO::Initialize only
float modelMNSThreshold; // catches non-positive garbage, so a small positive stack value
float detectionScoreThreshold; // (e.g. 80) would slip through and end up driving the ORT
int numKPS; // Number of pose keypoint // session at 80x80 — which crashes YOLOv11 in /model.12/Concat
float kpsThreshold; // because 2*ceil(80/32) ≠ ceil(80/16) in the FPN.
bool autoGPUDetection; DetectionType detectionType{};
float unknownPersonThreshold; ModelType modelType{};
PrecisionType precisionType{};
int inpWidth = 0;
int inpHeight = 0;
float modelConfThreshold = 0.0f;
float modelMNSThreshold = 0.0f;
float detectionScoreThreshold = 0.0f;
int numKPS = 0; // Number of pose keypoint
float kpsThreshold = 0.0f;
bool autoGPUDetection = false;
float unknownPersonThreshold = 0.0f;
int gpuOptBatchSize = 1; // The batch size which should be optimized for. int gpuOptBatchSize = 1; // The batch size which should be optimized for.
int gpuMaxBatchSize = 1; // Maximum allowable batch size int gpuMaxBatchSize = 1; // Maximum allowable batch size
int gpuDeviceIndex = 0; // GPU device index int gpuDeviceIndex = 0; // GPU device index
@@ -309,6 +321,110 @@ namespace ANSCENTER
int enableFeature; int enableFeature;
}; };
// ------------------------------------------------------------------------
// Real-time system telemetry. Snapshot of CPU / GPU / RAM / disk / network
// usage at the time of the call.
//
// Cheap to call frequently — values are cached for ~500 ms via a process-
// wide PDH sampler held in ANSLicense.cpp. The first call after process
// start blocks for ~150 ms because PDH counters require two samples to
// produce a delta-rate reading; subsequent calls are sub-millisecond.
//
// Fields the platform could not provide are set to -1 (or -1.0). Common
// reasons:
// - cpuTempDegC: ACPI thermal zone is unavailable / blocked on most
// consumer hardware. Use a vendor SDK (NVML / ADL / IPMI) for real
// thermals if you need them.
// - netLoadPercent: no network adapter advertises a non-zero
// "Current Bandwidth" (e.g. all interfaces are loopback / virtual).
// - valIGPU_MB: Windows < 10 19H1 does not expose GPU Process Memory
// PDH counters.
//
// Memory / disk units use 1024 base (1 GB == 2^30 bytes); throughput uses
// MB == 2^20 bytes for parity with Task Manager.
// ------------------------------------------------------------------------
struct SystemHealthInfo {
// CPU
// cpuLoadPercent — 0..100 always. Source: PDH "% Processor Time".
// The right value for "what fraction of CPU time
// am I spending in non-idle threads?".
// cpuUtilizationPercent — turbo-aware. Source: PDH "% Processor
// Utility". Can exceed 100% during sustained turbo
// boost. This is what Task Manager displays.
// Pick one to bind to the LabVIEW indicator; cpuUtilizationPercent
// matches what users see in TM but breaks fixed-range UI scaling.
double cpuLoadPercent = -1.0;
double cpuUtilizationPercent = -1.0;
double cpuTempDegC = -1.0;
double cpuFreqGHz = -1.0; // current clock (turbo-aware)
std::string cpuModel; // e.g. "Intel(R) Core(TM) i9-..."
// System memory
double memTotalGB = -1.0;
double memoryUsedGB = -1.0;
double memoryFreeGB = -1.0;
double rssPercent = -1.0; // current process WS / total physical
double swapPercent = -1.0; // page file used / page file limit
// Current process
int64_t valIRSS_MB = -1; // working-set size (MB)
int64_t valSwap_MB = -1; // private commit / page-file usage (MB)
int64_t valIGPU_MB = -1; // dedicated GPU memory used by us (MB)
// GPU — values report the *primary* adapter only. On hybrid systems
// (e.g. Iris Xe + RTX 4090) the primary is picked deterministically:
// NVIDIA discrete > AMD discrete > whichever has the most VRAM. Per-
// adapter numbers are available in gpuAdaptersDetail below.
double gpuLoadPercent = -1.0; // 0..100 on the primary adapter only
std::string gpuName; // primary adapter model name
// Per-adapter rollup; one row per physical GPU, comma-separated.
// Row format: <name>|<vendor>|<load%>|<processMemMB>|<vramMB>
// Vendors: NVIDIA / AMD / Intel / Microsoft / Other
// Numbers use the C locale ('.' decimal, 2dp for load%).
std::string gpuAdaptersDetail;
// Disk (system drive — the volume holding %SystemRoot%)
double diskLoadPercent = -1.0;
double diskReadSpeedMBs = -1.0;
double diskWriteSpeedMBs = -1.0;
double diskUsedGB = -1.0;
double diskTotalGB = -1.0;
// Network (sum across all non-loopback interfaces)
double netLoadPercent = -1.0;
double netReceiveSpeedMBs = -1.0;
double netSendSpeedMBs = -1.0;
};
// ------------------------------------------------------------------------
// Public IP + IP-based geolocation, sourced from ipapi.co/json/.
//
// One HTTPS GET per refresh; results cached for 60 minutes (success) or
// 60 seconds (failure backoff). Never throws, never blocks more than 5 s.
//
// `ok == false` means the data could not be obtained — `error` carries
// the reason ("HTTP 429", "WinHttpConnect failed", "JSON parse: ...",
// etc.). Latitude/longitude are 0,0 when unknown.
//
// Privacy note: this performs an outbound HTTPS request to a third-party
// service (ipapi.co). Each request reveals the device's public IP to that
// service. Use the simpler GetPublicIPAddress() if you only need the IP
// and prefer no third-party HTTP hop.
// ------------------------------------------------------------------------
struct PublicNetworkInfo {
bool ok = false;
std::string publicIP; // e.g. "203.0.113.42"
std::string country; // full name, e.g. "Vietnam"
std::string countryCode; // ISO 2-letter, e.g. "VN"
std::string region; // state / province
std::string city;
std::string isp; // organization / provider name
std::string timezone; // IANA tz, e.g. "Asia/Ho_Chi_Minh"
double latitude = 0.0;
double longitude = 0.0;
std::string error; // empty on success
};
class ANSLICENSE_API SPDLogger class ANSLICENSE_API SPDLogger
@@ -409,7 +525,30 @@ namespace ANSCENTER
[[nodiscard]] static bool isPathSafe(const std::string& pathStr); [[nodiscard]] static bool isPathSafe(const std::string& pathStr);
public: public:
[[nodiscard]] static bool LicenseVerification(std::string licenseKey, int productId, std::string registrationName); [[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 EngineType CheckHardwareInformation();
[[nodiscard]] static SystemHealthInfo GetSystemHealthInformation();
// Returns the device's public IPv4 address as seen from the internet,
// or an empty string if it can't be resolved (no connectivity, DNS
// blocked, etc). Result is cached for 10 minutes — subsequent calls
// are a memory read.
//
// Implementation uses a single DNS query to OpenDNS's special name
// 'myip.opendns.com' (the resolver echoes back the client's seen IP
// in an A record). No HTTP, no third-party REST, no database — works
// on any network where outbound DNS to 208.67.222.222 is allowed
// (which is the vast majority, including most corporate firewalls).
[[nodiscard]] static std::string GetPublicIPAddress();
// Public IP + geolocation in one shot via ipapi.co/json/. See the
// PublicNetworkInfo doc-comment for caching, failure semantics, and
// privacy implications. If you only need the IP, prefer the simpler
// GetPublicIPAddress() above (no HTTP, no third-party REST).
[[nodiscard]] static PublicNetworkInfo GetPublicNetworkInformation();
[[nodiscard]] static std::string ListLicenses(); [[nodiscard]] static std::string ListLicenses();
[[nodiscard]] static int ActivateTrialLicense(std::string productName); [[nodiscard]] static int ActivateTrialLicense(std::string productName);
[[nodiscard]] static int DeactivateLicense(std::string productName); [[nodiscard]] static int DeactivateLicense(std::string productName);
@@ -431,6 +570,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 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 LoadLicenseTemplate(ANSCENTER::ANSLSHelper * *Handle, const char* templateFilePath);
extern "C" __declspec(dllexport) int GetCurrentHardwareId(ANSCENTER::ANSLSHelper * *Handle, LStrHandle currentHwID); 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 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 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 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
@@ -447,6 +593,106 @@ extern "C" __declspec(dllexport) int ExtractModelZipFile(const char* modelZipFil
extern "C" __declspec(dllexport) int ZipFolderToZipFile(const char* modelFolderSource,const char* modelZipFileDes, const char* password); extern "C" __declspec(dllexport) int ZipFolderToZipFile(const char* modelFolderSource,const char* modelZipFileDes, const char* password);
extern "C" __declspec(dllexport) int PrepareEdgeModel(const char* modelZipFile, const char* zipFilePassword, const char* edgePassword, const char* modelName, const char* edgeSerialNumber, LStrHandle pathArray); extern "C" __declspec(dllexport) int PrepareEdgeModel(const char* modelZipFile, const char* zipFilePassword, const char* edgePassword, const char* modelName, const char* edgeSerialNumber, LStrHandle pathArray);
extern "C" __declspec(dllexport) int GetSystemHardwareInformation(LStrHandle systemHardwareInformation); extern "C" __declspec(dllexport) int GetSystemHardwareInformation(LStrHandle systemHardwareInformation);
// SystemHealthParameters cluster, one scalar at a time. Pass nullptr for any
// field you do not care about. Returns 1 on success, 0 on failure. Cached
// internally so calling at LabVIEW UI rate (e.g. 1 Hz) is essentially free.
extern "C" __declspec(dllexport) int GetSystemHealthParameters(
double* cpuLoadPercent,
double* cpuTempDegC,
double* cpuFreqGHz,
LStrHandle cpuModel,
double* memTotalGB,
double* rssPercent,
double* swapPercent,
double* gpuLoadPercent,
int64_t* valIRSS_MB,
int64_t* valSwap_MB,
int64_t* valIGPU_MB,
double* memoryUsedGB,
double* memoryFreeGB,
double* diskLoadPercent,
double* diskReadSpeedMBs,
double* diskWriteSpeedMBs,
double* diskUsedGB,
double* diskTotalGB,
double* netLoadPercent,
double* netReceiveSpeedMBs,
double* netSendSpeedMBs,
LStrHandle gpuName, // primary adapter name (e.g. "NVIDIA RTX 4090"); pass nullptr to skip
LStrHandle gpuAdaptersDetail, // per-adapter rollup; pass nullptr to skip — see SystemHealthInfo for format
double* cpuUtilizationPercent); // turbo-aware "% Processor Utility"; matches Task Manager (may exceed 100)
// Same payload as GetSystemHealthParameters, packed into a single string with
// ';' separators in this exact order:
// 1 cpuLoadPercent (%; 0..100, "% Processor Time")
// 2 cpuTempDegC (°C, -1 if unavailable)
// 3 cpuFreqGHz (GHz)
// 4 cpuModel (string; any literal ';' is rewritten to ',')
// 5 memTotalGB
// 6 rssPercent (%)
// 7 swapPercent (%)
// 8 gpuLoadPercent (%)
// 9 valIRSS_MB (current process working set, MB)
// 10 valSwap_MB (current process page-file usage, MB)
// 11 valIGPU_MB (current process GPU dedicated memory, MB)
// 12 memoryUsedGB
// 13 memoryFreeGB
// 14 diskLoadPercent (%)
// 15 diskReadSpeedMBs
// 16 diskWriteSpeedMBs
// 17 diskUsedGB
// 18 diskTotalGB
// 19 netLoadPercent (%)
// 20 netReceiveSpeedMBs
// 21 netSendSpeedMBs
// 22 gpuName (primary adapter model, e.g. "NVIDIA RTX 4090")
// 23 gpuAdaptersDetail (per-adapter rollup — see SystemHealthInfo struct)
// 24 cpuUtilizationPercent (%; turbo-aware, may exceed 100; matches Task Manager)
//
// New fields are appended at the end so existing parsers stay valid. Fields
// 1..23 are stable.
// Numbers use the C locale ('.' decimal point) and 2-decimal fixed format.
// Any literal ';' in gpuName / gpuAdaptersDetail is rewritten to ',' so the
// outer separator is never ambiguous.
// Returns 1 on success, 0 on failure.
extern "C" __declspec(dllexport) int GetSystemHealthParametersString(LStrHandle outString);
// Writes the device's public IPv4 (e.g. "203.0.113.42") into the LStrHandle.
// Returns 1 on success, 0 if the IP could not be resolved. Cached for 10 min.
extern "C" __declspec(dllexport) int GetPublicIPAddress(LStrHandle ipv4);
// Public IP + IP-based geolocation via ipapi.co/json/. Each pointer is
// optional; pass nullptr to skip a field. `errorMsg` carries a human-readable
// reason when the call fails (no internet, rate limit, etc.). Returns 1 on
// success, 0 on failure. Cached for 1 hour on success; 1-minute back-off on
// failure so a flapping network doesn't get hammered.
extern "C" __declspec(dllexport) int GetPublicNetworkParameters(
LStrHandle publicIP,
LStrHandle country,
LStrHandle countryCode,
LStrHandle region,
LStrHandle city,
LStrHandle isp,
LStrHandle timeZone,
double* latitude,
double* longitude,
LStrHandle errorMsg);
// Same payload packed as a single ';'-separated string in this exact order:
// 1 publicIP
// 2 country (full name)
// 3 countryCode (ISO 2-letter)
// 4 region
// 5 city
// 6 isp
// 7 timezone (IANA, e.g. "Asia/Ho_Chi_Minh")
// 8 latitude (6dp, C locale)
// 9 longitude (6dp, C locale)
// Any literal ';' inside string fields is rewritten to ',' so the separator
// stays unambiguous. Returns 1 on success, 0 on failure (string still written
// — values just empty/0).
extern "C" __declspec(dllexport) int GetPublicNetworkParametersString(LStrHandle outString);
extern "C" __declspec(dllexport) int ReleaseLoggers(); extern "C" __declspec(dllexport) int ReleaseLoggers();
extern "C" __declspec(dllexport) int ListANSLicenses(std::string &listLicense); extern "C" __declspec(dllexport) int ListANSLicenses(std::string &listLicense);
extern "C" __declspec(dllexport) int ListANSLicenses_LV(LStrHandle ansProductLicenses); extern "C" __declspec(dllexport) int ListANSLicenses_LV(LStrHandle ansProductLicenses);
+4
View File
@@ -52,6 +52,10 @@ target_link_libraries(ANSLicensingSystem PRIVATE
bz2.lib zip.lib bz2.lib zip.lib
libboost_log-vc143-mt-x64-1_82.lib libboost_log-vc143-mt-x64-1_82.lib
shlwapi.lib winhttp.lib shlwapi.lib winhttp.lib
pdh.lib psapi.lib
wbemuuid.lib oleaut32.lib ole32.lib
dxgi.lib advapi32.lib
dnsapi.lib
${WIN_COMMON_LIBS} ${WIN_COMMON_LIBS}
) )
+271
View File
@@ -2,6 +2,10 @@
#include "pch.h" #include "pch.h"
#include "ANSLicense.h" #include "ANSLicense.h"
#include "Utility.h" #include "Utility.h"
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <locale>
BOOL APIENTRY DllMain( HMODULE hModule, BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call, DWORD ul_reason_for_call,
@@ -105,6 +109,43 @@ extern "C" __declspec(dllexport) int GetCurrentHardwareId(ANSCENTER::ANSLSHelper
return 0; 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) { extern "C" __declspec(dllexport) int GenerateKey(ANSCENTER::ANSLSHelper * *Handle, int productId, const char* registrationName, int productFeature, int trialLicense, LStrHandle generatedKey) {
try { try {
if (!Handle || !(*Handle) || !registrationName) return 0; if (!Handle || !(*Handle) || !registrationName) return 0;
@@ -369,6 +410,236 @@ extern "C" __declspec(dllexport) int GetSystemHardwareInformation(LStrHandle sys
} }
} }
// SystemHealthParameters cluster, decomposed into individual scalar outputs so
// LabVIEW can wire each indicator straight to a Call Library Function Node.
// Any pointer may be nullptr (LabVIEW will pass nullptr for cluster fields the
// caller hasn't bound). cpuModel is optional but recommended; pass an LStrHandle
// or nullptr to skip. Returns 1 on success, 0 on hard failure.
extern "C" __declspec(dllexport) int GetSystemHealthParameters(
double* cpuLoadPercent,
double* cpuTempDegC,
double* cpuFreqGHz,
LStrHandle cpuModel,
double* memTotalGB,
double* rssPercent,
double* swapPercent,
double* gpuLoadPercent,
int64_t* valIRSS_MB,
int64_t* valSwap_MB,
int64_t* valIGPU_MB,
double* memoryUsedGB,
double* memoryFreeGB,
double* diskLoadPercent,
double* diskReadSpeedMBs,
double* diskWriteSpeedMBs,
double* diskUsedGB,
double* diskTotalGB,
double* netLoadPercent,
double* netReceiveSpeedMBs,
double* netSendSpeedMBs,
LStrHandle gpuName,
LStrHandle gpuAdaptersDetail,
double* cpuUtilizationPercent)
{
try {
const ANSCENTER::SystemHealthInfo info =
ANSCENTER::ANSLicenseHelper::GetSystemHealthInformation();
auto writeLStr = [](LStrHandle handle, const std::string& s) {
if (!handle) return;
const int size = static_cast<int>(s.length());
const MgErr err = DSSetHandleSize(handle, sizeof(int32) + size * sizeof(uChar));
if (err == noErr) {
(*handle)->cnt = size;
if (size > 0) memcpy((*handle)->str, s.c_str(), size);
}
};
if (cpuLoadPercent) *cpuLoadPercent = info.cpuLoadPercent;
if (cpuTempDegC) *cpuTempDegC = info.cpuTempDegC;
if (cpuUtilizationPercent) *cpuUtilizationPercent = info.cpuUtilizationPercent;
if (cpuFreqGHz) *cpuFreqGHz = info.cpuFreqGHz;
if (memTotalGB) *memTotalGB = info.memTotalGB;
if (rssPercent) *rssPercent = info.rssPercent;
if (swapPercent) *swapPercent = info.swapPercent;
if (gpuLoadPercent) *gpuLoadPercent = info.gpuLoadPercent;
if (valIRSS_MB) *valIRSS_MB = info.valIRSS_MB;
if (valSwap_MB) *valSwap_MB = info.valSwap_MB;
if (valIGPU_MB) *valIGPU_MB = info.valIGPU_MB;
if (memoryUsedGB) *memoryUsedGB = info.memoryUsedGB;
if (memoryFreeGB) *memoryFreeGB = info.memoryFreeGB;
if (diskLoadPercent) *diskLoadPercent = info.diskLoadPercent;
if (diskReadSpeedMBs) *diskReadSpeedMBs = info.diskReadSpeedMBs;
if (diskWriteSpeedMBs) *diskWriteSpeedMBs = info.diskWriteSpeedMBs;
if (diskUsedGB) *diskUsedGB = info.diskUsedGB;
if (diskTotalGB) *diskTotalGB = info.diskTotalGB;
if (netLoadPercent) *netLoadPercent = info.netLoadPercent;
if (netReceiveSpeedMBs) *netReceiveSpeedMBs = info.netReceiveSpeedMBs;
if (netSendSpeedMBs) *netSendSpeedMBs = info.netSendSpeedMBs;
writeLStr(cpuModel, info.cpuModel);
writeLStr(gpuName, info.gpuName);
writeLStr(gpuAdaptersDetail, info.gpuAdaptersDetail);
return 1;
} catch (...) {
return 0;
}
}
extern "C" __declspec(dllexport) int GetPublicIPAddress(LStrHandle ipv4) {
try {
if (!ipv4) return 0;
const std::string s = ANSCENTER::ANSLicenseHelper::GetPublicIPAddress();
const int size = static_cast<int>(s.length());
const MgErr err = DSSetHandleSize(ipv4, sizeof(int32) + size * sizeof(uChar));
if (err != noErr) return 0;
(*ipv4)->cnt = size;
if (size > 0) memcpy((*ipv4)->str, s.c_str(), size);
return s.empty() ? 0 : 1;
} catch (...) {
return 0;
}
}
namespace {
inline void WriteLStr(LStrHandle h, const std::string& s) {
if (!h) return;
const int size = static_cast<int>(s.length());
const MgErr err = DSSetHandleSize(h, sizeof(int32) + size * sizeof(uChar));
if (err == noErr) {
(*h)->cnt = size;
if (size > 0) memcpy((*h)->str, s.c_str(), size);
}
}
}
extern "C" __declspec(dllexport) int GetPublicNetworkParameters(
LStrHandle publicIP,
LStrHandle country,
LStrHandle countryCode,
LStrHandle region,
LStrHandle city,
LStrHandle isp,
LStrHandle timeZone,
double* latitude,
double* longitude,
LStrHandle errorMsg)
{
try {
const ANSCENTER::PublicNetworkInfo info =
ANSCENTER::ANSLicenseHelper::GetPublicNetworkInformation();
WriteLStr(publicIP, info.publicIP);
WriteLStr(country, info.country);
WriteLStr(countryCode, info.countryCode);
WriteLStr(region, info.region);
WriteLStr(city, info.city);
WriteLStr(isp, info.isp);
WriteLStr(timeZone, info.timezone);
WriteLStr(errorMsg, info.error);
if (latitude) *latitude = info.latitude;
if (longitude) *longitude = info.longitude;
return info.ok ? 1 : 0;
} catch (...) {
return 0;
}
}
extern "C" __declspec(dllexport) int GetPublicNetworkParametersString(LStrHandle outString) {
try {
if (!outString) return 0;
const ANSCENTER::PublicNetworkInfo info =
ANSCENTER::ANSLicenseHelper::GetPublicNetworkInformation();
auto sanitize = [](std::string s) {
std::replace(s.begin(), s.end(), ';', ',');
return s;
};
std::ostringstream os;
os.imbue(std::locale::classic());
os << std::fixed << std::setprecision(6);
os << sanitize(info.publicIP) << ';'
<< sanitize(info.country) << ';'
<< sanitize(info.countryCode) << ';'
<< sanitize(info.region) << ';'
<< sanitize(info.city) << ';'
<< sanitize(info.isp) << ';'
<< sanitize(info.timezone) << ';'
<< info.latitude << ';'
<< info.longitude;
const std::string s = os.str();
const int size = static_cast<int>(s.length());
const MgErr err = DSSetHandleSize(outString, sizeof(int32) + size * sizeof(uChar));
if (err != noErr) return 0;
(*outString)->cnt = size;
if (size > 0) memcpy((*outString)->str, s.c_str(), size);
return info.ok ? 1 : 0;
} catch (...) {
return 0;
}
}
extern "C" __declspec(dllexport) int GetSystemHealthParametersString(LStrHandle outString) {
try {
if (!outString) return 0;
const ANSCENTER::SystemHealthInfo info =
ANSCENTER::ANSLicenseHelper::GetSystemHealthInformation();
// Keep ';' as a clean separator: rewrite any literal ';' inside any
// free-form string field (rare, but vendor strings aren't safe by
// contract).
auto sanitize = [](std::string s) {
std::replace(s.begin(), s.end(), ';', ',');
return s;
};
const std::string model = sanitize(info.cpuModel);
const std::string gpuName_ = sanitize(info.gpuName);
const std::string gpuDetail = sanitize(info.gpuAdaptersDetail);
std::ostringstream os;
os.imbue(std::locale::classic()); // force '.' decimal regardless of system locale
os << std::fixed << std::setprecision(2);
os << info.cpuLoadPercent << ';'
<< info.cpuTempDegC << ';'
<< info.cpuFreqGHz << ';'
<< model << ';'
<< info.memTotalGB << ';'
<< info.rssPercent << ';'
<< info.swapPercent << ';'
<< info.gpuLoadPercent << ';'
<< info.valIRSS_MB << ';'
<< info.valSwap_MB << ';'
<< info.valIGPU_MB << ';'
<< info.memoryUsedGB << ';'
<< info.memoryFreeGB << ';'
<< info.diskLoadPercent << ';'
<< info.diskReadSpeedMBs << ';'
<< info.diskWriteSpeedMBs << ';'
<< info.diskUsedGB << ';'
<< info.diskTotalGB << ';'
<< info.netLoadPercent << ';'
<< info.netReceiveSpeedMBs << ';'
<< info.netSendSpeedMBs << ';'
<< gpuName_ << ';'
<< gpuDetail << ';'
<< info.cpuUtilizationPercent;
const std::string s = os.str();
const int size = static_cast<int>(s.length());
const MgErr err = DSSetHandleSize(outString, sizeof(int32) + size * sizeof(uChar));
if (err != noErr) return 0;
(*outString)->cnt = size;
if (size > 0) memcpy((*outString)->str, s.c_str(), size);
return 1;
} catch (...) {
return 0;
}
}
extern "C" __declspec(dllexport) int ReleaseLoggers() { extern "C" __declspec(dllexport) int ReleaseLoggers() {
try { try {
bool result = ANSCENTER::ANSLSHelper::ReleaseLogger(); bool result = ANSCENTER::ANSLSHelper::ReleaseLogger();
+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
+45
View File
@@ -0,0 +1,45 @@
#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.
// ANSCORE_HAVE_BUILTIN_MODEL_KEYS is set by CMake when the ceremony's
// generated header is there. It is not tested with __has_include on purpose:
// that test leaves no dependency for the build system, so a key emitted after
// a build would not take effect until someone cleaned — and the failure looks
// exactly like a missing key, which on a release machine means shipping an
// engine that silently cannot open a package.
#if defined(ANSCORE_HAVE_BUILTIN_MODEL_KEYS)
#include "ANSModelKeysGenerated.h"
#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
+250
View File
@@ -0,0 +1,250 @@
#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;
// WHAT AN OLDER PACKAGE CANNOT TELL US, and its model can. The input
// size, when the manifest predates the field; and the head, because a
// package whose family is "yolo" may hold an end-to-end export — the box
// used to take the family from the backend marker, and the marker names
// the runtime rather than the head. It costs nothing to ask here: the
// bytes are already decrypted.
{
onnxshape::GraphShape g;
std::string swhy;
if (onnxshape::graphFromMemory(out.model.data(), out.model.size(), g,
swhy)) {
if (out.inputWidth <= 0 || out.inputHeight <= 0) {
out.inputWidth = g.input.width;
out.inputHeight = g.input.height;
}
if (g.endToEnd() && out.family == "yolo") {
out.family = "yolo10";
out.warning = out.warning.empty()
? "this package says family yolo, but its head is "
"end-to-end; reading it as yolo10"
: out.warning;
}
}
}
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
+354
View File
@@ -0,0 +1,354 @@
#ifndef ANSONNXSHAPE_H
#define ANSONNXSHAPE_H
#pragma once
// ANSOnnxShape — the input geometry a model declares, read off the graph.
//
// WHY A READER NEEDS THIS. A node has to know what its model expects before it
// can preprocess a single frame, and until now that answer came from whatever
// the bundle happened to carry: model_config.json's input_size in a folder, an
// `input` object in a package manifest. Neither is present on every model. The
// graph always is, and the graph is the thing the session is built from — so
// where the metadata is silent, ask the model itself rather than assume 640.
//
// TWO SOURCES, ONE PARSER. A package's model is already in memory, decrypted,
// and must never reach the disk; an imported folder's model 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)
// GraphProto.output field 12, repeated
// 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)
// ModelProto.metadata_props field 14, repeated StringStringEntryProto
// StringStringEntryProto.key field 1, string
// StringStringEntryProto.value field 2, string
//
// 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 <cctype>
#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; }
};
// What the whole graph says about itself: what goes in, and what shape comes
// out — which is the difference between a head that needs decoding and one
// that has already done it.
struct GraphShape {
InputShape input;
std::vector<int64_t> output; // the FIRST output's dims; empty when unknown
bool exportSaysEndToEnd = false; // the model's own metadata, when it says
// AN END-TO-END HEAD, [1, N, 6]: rows of (x1 y1 x2 y2 score class) with the
// NMS already inside the graph. Telling one apart from an ordinary YOLO head
// matters more than it looks: feeding [1,N,6] to a v8 decoder yields boxes
// everywhere with coordinates read as confidences — plausible-looking
// garbage rather than an error.
//
// The trap is a two-class v8 export, whose [1, 4+nc, N] is [1, 6, 8400] —
// a 6 in the shape, and not end-to-end. So the 6 must be the LAST axis, and
// the axis before it must be a detection count rather than an anchor count;
// an end-to-end head emits max_det rows, which is hundreds, never thousands.
// When the export's own metadata says end2end, that settles it outright.
bool endToEnd() const {
if (exportSaysEndToEnd) return true;
return output.size() == 3 && output[0] == 1 && output[2] == 6 &&
output[1] > 0 && output[1] <= 1000;
}
};
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;
}
// One ValueInfoProto (a graph input or output), given a stream on its payload:
// its dims, or empty when it is not a shaped tensor.
template <class Src>
std::vector<int64_t> valueDims(Src& s, int64_t valueEnd) {
uint64_t len = 0;
if (!seekField(s, valueEnd, 2, len)) return {}; // ValueInfoProto.type
const int64_t typeEnd = s.tell() + int64_t(len);
if (!seekField(s, typeEnd, 1, len)) return {}; // TypeProto.tensor_type
const int64_t tensorEnd = s.tell() + int64_t(len);
if (!seekField(s, tensorEnd, 2, len)) return {}; // Tensor.shape
return dims(s, s.tell() + int64_t(len));
}
// The graph's first input and first output, and whether the export declared
// itself end-to-end. `end` is the whole file; `graphStart` lets the output be
// found after the input, since both hang off the same graph.
template <class Src>
bool readGraph(Src& s, int64_t end, GraphShape& 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 graphStart = s.tell();
const int64_t graphEnd = graphStart + int64_t(len);
if (!seekField(s, graphEnd, 11, len)) { // GraphProto.input
why = "the graph declares no input";
return false;
}
const std::vector<int64_t> in = valueDims(s, s.tell() + int64_t(len));
// The output, from the graph's start again: it sits after the input on the
// wire, but seeking from the start costs nothing and does not depend on it.
s.seek(graphStart);
if (seekField(s, graphEnd, 12, len)) // GraphProto.output
out.output = valueDims(s, s.tell() + int64_t(len));
// The export's own word on its head, which beats any shape rule.
s.seek(0);
while (s.ok() && s.tell() >= 0 && s.tell() < end) {
uint32_t field = 0, wire = 0;
if (!tag(s, field, wire)) break;
if (field != 14 || wire != 2) { // ModelProto.metadata_props
if (!skip(s, wire)) break;
continue;
}
uint64_t entryLen = 0;
if (!varint(s, entryLen)) break;
const int64_t entryEnd = s.tell() + int64_t(entryLen);
std::string key, value;
while (s.ok() && s.tell() >= 0 && s.tell() < entryEnd) {
uint32_t ef = 0, ew = 0;
if (!tag(s, ef, ew)) break;
if ((ef == 1 || ef == 2) && ew == 2) {
uint64_t n = 0;
if (!varint(s, n)) break;
std::string text;
for (uint64_t i = 0; i < n; ++i) {
const int c = s.get();
if (c < 0) break;
text.push_back(char(c));
}
(ef == 1 ? key : value) = text;
} else if (!skip(s, ew)) {
break;
}
}
s.seek(entryEnd);
if (key == "end2end") {
for (char& c : value) c = char(::tolower(static_cast<unsigned char>(c)));
out.exportSaysEndToEnd = (value == "true" || value == "1");
}
}
// NCHW is the only layout these engines run.
if (in.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.input.channels = axis(in[1]);
out.input.height = axis(in[2]);
out.input.width = axis(in[3]);
if (!out.input.known()) {
why = "the model takes a dynamic input size";
return false;
}
return true;
}
template <class Src>
bool read(Src& s, int64_t end, InputShape& out, std::string& why) {
GraphShape g;
if (!readGraph(s, end, g, why)) return false;
out = g.input;
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);
}
// The whole graph, from a model already in memory.
inline bool graphFromMemory(const void* data, size_t size, GraphShape& 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::readGraph(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);
}
// The whole graph, from a model on disk. Walked, not loaded.
inline bool graphFromFile(const std::string& path, GraphShape& 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::readGraph(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
+43
View File
@@ -0,0 +1,43 @@
# 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, wherever this machine keeps it: the ANSLibs tree on Windows,
# vcpkg, or the system elsewhere. Asked for by name rather than assumed — the
# Windows branch used to point at vcpkg alone, and on a real build machine the
# header lives under ANSLibs, so every target that linked this one failed to
# find it while the engines, which add ANSLibs themselves, compiled fine.
find_path(ANSCORE_NLOHMANN_INCLUDE nlohmann/json.hpp
HINTS ${ANSLIBS_DIR} ${VCPKG_INCLUDE_DIR}
/opt/homebrew/include /usr/local/include /usr/include)
if(ANSCORE_NLOHMANN_INCLUDE)
target_include_directories(ANSModelPackage PUBLIC ${ANSCORE_NLOHMANN_INCLUDE})
message(STATUS "ANSModelPackage: nlohmann/json at ${ANSCORE_NLOHMANN_INCLUDE}")
else()
message(WARNING "nlohmann/json.hpp not found — ANSModelPackage will not build")
endif()
# The model-package key, decided at configure time: see ANSModelKeys.h for why
# this is not an __has_include test. PUBLIC so every engine that opens a
# package is compiled with the same answer.
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/ANSModelKeysGenerated.h")
target_compile_definitions(ANSModelPackage PUBLIC ANSCORE_HAVE_BUILTIN_MODEL_KEYS=1)
message(STATUS "ANSModelPackage: key present — packages can be opened")
else()
message(STATUS "ANSModelPackage: no key — packages cannot be opened "
"(run ansmodel-keys emit --product anscore, then re-run cmake)")
endif()
set_target_properties(ANSModelPackage PROPERTIES POSITION_INDEPENDENT_CODE ON)
+15 -12
View File
@@ -8,9 +8,11 @@ const unsigned char BitStream3::hiMask[] = { 0, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0x
BitStream3::BitStream3() : BitStream3::BitStream3() :
ownMemory(false), ownMemory(false),
buffer(NULL),
bufSize(0),
bitSize(0),
currentBit(0), currentBit(0),
currentByte(0), currentByte(0)
buffer(NULL)
{ {
} }
@@ -41,6 +43,7 @@ void BitStream3::Attach(void * buf, int bitCount)
{ {
buffer = (unsigned char *)buf; buffer = (unsigned char *)buf;
bitSize = bitCount; bitSize = bitCount;
bufSize = (bitCount + 7) >> 3;
currentBit = 0; currentBit = 0;
currentByte = 0; currentByte = 0;
} }
@@ -53,20 +56,20 @@ int BitStream3::Write(const unsigned char * buf, int bitCount)
{ {
if ((gap & 7) != 0) 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++; currentBit += gap; bitCount -= gap; currentByte++;
for (; bitCount >= 8; i++, currentByte++, currentBit += 8, bitCount -= 8) 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 else
for (; bitCount >= 8; currentByte++, i++, currentBit += 8, bitCount -= 8) for (; bitCount >= 8; currentByte++, i++, currentBit += 8, bitCount -= 8)
buffer[currentByte] = buf[i]; setByte(currentByte, buf[i]);
} }
if (bitCount > 0) 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; currentBit += bitCount;
} }
@@ -86,20 +89,20 @@ int BitStream3::Read(unsigned char * readBuf, int bitCount)
if ((gap & 7) != 0) if ((gap & 7) != 0)
{ {
for (; bitCount >= 8 + 8 - gap; bitCount -= 8, currentByte++, currentBit += 8, i++) 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 > 0)
{ {
if (bitCount < 8 - gap) 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; currentBit += bitCount;
bitCount = 0; bitCount = 0;
} }
else else
{ {
readBuf[i] = (unsigned char)(buffer[currentByte] & ((1 << (8 - gap)) - 1)); readBuf[i] = (unsigned char)(byteAt(currentByte) & ((1 << (8 - gap)) - 1));
bitCount -= (8 - gap); bitCount -= (8 - gap);
currentBit += (8 - gap); currentBit += (8 - gap);
@@ -111,15 +114,15 @@ int BitStream3::Read(unsigned char * readBuf, int bitCount)
} }
else else
for (; bitCount >= 8; bitCount -= 8, currentByte++, currentBit += 8, i++) for (; bitCount >= 8; bitCount -= 8, currentByte++, currentBit += 8, i++)
readBuf[i] = buffer[currentByte]; readBuf[i] = byteAt(currentByte);
// already aligned // already aligned
if (bitCount > 0) if (bitCount > 0)
{ {
if (currentBit + 8 <= bitSize) // not last byte if (currentBit + 8 <= bitSize) // not last byte
readBuf[i] |= (unsigned char)((buffer[currentByte] >> (8 - bitCount)) & ((1 << bitCount) - 1)); readBuf[i] |= (unsigned char)((byteAt(currentByte) >> (8 - bitCount)) & ((1 << bitCount) - 1));
else // last byte else // last byte
readBuf[i] = (unsigned char)((buffer[currentByte] >> ((bitSize & 7) - bitCount)) & ((1 << bitCount) - 1)); readBuf[i] = (unsigned char)((byteAt(currentByte) >> ((bitSize & 7) - bitCount)) & ((1 << bitCount) - 1));
currentBit += bitCount; currentBit += bitCount;
} }
+14
View File
@@ -16,6 +16,20 @@ public:
unsigned char * GetBuffer(); unsigned char * GetBuffer();
protected: 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; bool ownMemory;
unsigned char * buffer; unsigned char * buffer;
int bufSize, bitSize; int bufSize, bitSize;
+139 -6
View File
@@ -21,9 +21,10 @@
#include <net/if.h> #include <net/if.h>
#ifdef __linux__ #ifdef __linux__
# include <sys/ioctl.h> # include <sys/ioctl.h>
# include <sys/stat.h> // stat() — physical-NIC test in linuxIsPhysicalNic
# include <netinet/in.h> # include <netinet/in.h>
# include <unistd.h> # include <unistd.h>
# include <string.h> # include <string.h> // strncmp/strlen (same test)
// Match Linux to FreeBSD // Match Linux to FreeBSD
# define AF_LINK AF_PACKET # define AF_LINK AF_PACKET
@@ -82,6 +83,47 @@ void GetCPUIDPropertyList(std::list<std::string>* propList)
propList->push_back(buf); 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) void GetMACPropertyList(std::list<std::string>* propList, int maxCount)
{ {
// Head of the interface address linked list // Head of the interface address linked list
@@ -99,6 +141,14 @@ void GetMACPropertyList(std::list<std::string>* propList, int maxCount)
return; 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) { while (current != NULL && maxCount > 0) {
const sockaddr_in* interfaceAddress = castToIP4(current->ifa_addr); const sockaddr_in* interfaceAddress = castToIP4(current->ifa_addr);
@@ -127,8 +177,18 @@ void GetMACPropertyList(std::list<std::string>* propList, int maxCount)
{ {
char buf[18]; char buf[18];
sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]); 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); propList->push_back(buf);
maxCount--; maxCount--;
#endif
} }
} }
@@ -137,6 +197,21 @@ void GetMACPropertyList(std::list<std::string>* propList, int maxCount)
freeifaddrs(ifap); freeifaddrs(ifap);
ifap = NULL; 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) void GetHDDPropertyList(std::list<std::string>* propList, int maxCount)
@@ -220,7 +295,13 @@ const char* HardwareId::GetCurrentHardwareId()
WmiHelper wmi; WmiHelper wmi;
#endif #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; list<string> propList;
unsigned char version = 1; unsigned char version = 1;
unsigned short hash1, hash2, hash3, hash4; unsigned short hash1, hash2, hash3, hash4;
@@ -256,6 +337,8 @@ const char* HardwareId::GetCurrentHardwareId()
bits.WriteUInt16(hash1); bits.WriteUInt16(hash1);
bool uuidFound = (hash1 != h0);
// CPU IDs - get up to 4 for better resilience // CPU IDs - get up to 4 for better resilience
propList.clear(); propList.clear();
@@ -270,6 +353,8 @@ const char* HardwareId::GetCurrentHardwareId()
cpu = NormalizeHardwareString(cpu); cpu = NormalizeHardwareString(cpu);
} }
int cpuCount = (int)propList.size();
hash1 = (propList.size() > 0) ? HashString(propList.front().c_str()) : h0; hash1 = (propList.size() > 0) ? HashString(propList.front().c_str()) : h0;
hash2 = (propList.size() > 1) ? HashString(propList.back().c_str()) : h0; hash2 = (propList.size() > 1) ? HashString(propList.back().c_str()) : h0;
@@ -298,6 +383,8 @@ const char* HardwareId::GetCurrentHardwareId()
} }
propList.sort(); propList.sort();
int macCount = (int)propList.size();
// Store up to 4 MAC hashes // Store up to 4 MAC hashes
auto macIter = propList.begin(); auto macIter = propList.begin();
hash1 = (propList.size() > 0 && macIter != propList.end()) ? HashString((macIter++)->c_str()) : h0; hash1 = (propList.size() > 0 && macIter != propList.end()) ? HashString((macIter++)->c_str()) : h0;
@@ -325,6 +412,8 @@ const char* HardwareId::GetCurrentHardwareId()
} }
propList.sort(); propList.sort();
int hddCount = (int)propList.size();
auto hddIter = propList.begin(); auto hddIter = propList.begin();
hash1 = (propList.size() > 0 && hddIter != propList.end()) ? HashString((hddIter++)->c_str()) : h0; hash1 = (propList.size() > 0 && hddIter != propList.end()) ? HashString((hddIter++)->c_str()) : h0;
hash2 = (propList.size() > 1 && 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; 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(); return hardwareId.c_str();
} }
bool HardwareId::MatchCurrentHardwareId(const char* hwid) 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 #ifdef _WIN32
WmiHelper wmi; WmiHelper wmi;
#endif #endif
@@ -383,8 +495,14 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
auto buf = base32.decode(hardwareId.c_str(), hardwareId.length(), &bufLen); 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; BitStream3 bits;
bits.Attach(buf.data(), 125); bits.Attach(buf.data(), 160);
unsigned char version = 0; unsigned char version = 0;
bits.Read(&version, 3); bits.Read(&version, 3);
@@ -398,6 +516,7 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
// Use a points-based matching system for resilience // Use a points-based matching system for resilience
int matchPoints = 0; int matchPoints = 0;
bool uuidMatched = false;
const int REQUIRED_POINTS = 2; // Need at least 2 components to match const int REQUIRED_POINTS = 2; // Need at least 2 components to match
// Read UUID hash // Read UUID hash
@@ -430,6 +549,7 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
} }
} }
matchPoints += 2; // UUID/Memory match gives 2 points matchPoints += 2; // UUID/Memory match gives 2 points
uuidMatched = true;
} }
else else
{ {
@@ -437,9 +557,11 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
#ifdef _WIN32 #ifdef _WIN32
wmi.GetPropertyList("SELECT * FROM Win32_ComputerSystemProduct", "UUID", &propList); wmi.GetPropertyList("SELECT * FROM Win32_ComputerSystemProduct", "UUID", &propList);
#endif #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) if ((s.length() > 1)
&& (s != "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF") && (s != "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
@@ -448,6 +570,8 @@ bool HardwareId::MatchCurrentHardwareId(const char* hwid)
if (hash1 != h0 && hash1 == HashString(s.c_str())) if (hash1 != h0 && hash1 == HashString(s.c_str()))
{ {
matchPoints += 2; // UUID match is worth 2 points (most stable) 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) + MAC (1) = 3 points ✓
// - UUID (2) + HDD (1) = 3 points ✓ // - UUID (2) + HDD (1) = 3 points ✓
// - CPU (1) + MAC (1) + HDD (1) = 3 points ✓ (if UUID changed but hardware same) // - 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;
} }
+27 -2
View File
@@ -2,6 +2,7 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <cstdio>
#include "except.h" #include "except.h"
#include "template.h" #include "template.h"
@@ -179,6 +180,8 @@ public:
catch (Exception * ex) catch (Exception * ex)
{ {
ex->Destroy(); 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; activationStatus = STATUS_INVALID_ACTIVATION_KEY;
if (ownActivationTemplate) delete activationTemplate; if (ownActivationTemplate) delete activationTemplate;
return false; return false;
@@ -186,6 +189,7 @@ public:
if (!validator.IsKeyValid()) if (!validator.IsKeyValid())
{ {
fprintf(stderr, "[anslicensing] activation key does not validate against this license key + hardware id\n");
activationStatus = STATUS_INVALID_ACTIVATION_KEY; activationStatus = STATUS_INVALID_ACTIVATION_KEY;
if (ownActivationTemplate) delete activationTemplate; if (ownActivationTemplate) delete activationTemplate;
return false; return false;
@@ -222,6 +226,7 @@ public:
{ {
if (timeValidationMethod == USE_INTERNET_TIME) // internet time was mandatory 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; activationStatus = STATUS_NET_ERROR;
if (ownActivationTemplate) delete activationTemplate; if (ownActivationTemplate) delete activationTemplate;
return false; return false;
@@ -243,6 +248,10 @@ public:
if (m_licenseValidityDays < 0) if (m_licenseValidityDays < 0)
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; activationStatus = STATUS_LICENSE_EXPIRED;
if (ownActivationTemplate) delete activationTemplate; if (ownActivationTemplate) delete activationTemplate;
@@ -465,6 +474,9 @@ public:
generator.SetValidationData("LicenseKeyValidationData", licenseKeyValidationData.data(), licenseKeyValidationDataLen * 8); 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 { try {
activationKey = generator.GenerateKey(); activationKey = generator.GenerateKey();
} }
@@ -472,19 +484,29 @@ public:
{ {
if ((ex->GetCode() == STATUS_NET_ERROR) && (generator.GetSigningServiceStatusCode() == 406)) if ((ex->GetCode() == STATUS_NET_ERROR) && (generator.GetSigningServiceStatusCode() == 406))
{ {
fprintf(stderr, "[anslicensing] licensing service returned HTTP 406 - payment required\n");
m_licenseValidationResult.SetPaymentRequired(true); m_licenseValidationResult.SetPaymentRequired(true);
ex->Destroy(); ex->Destroy();
} }
else else
{
fprintf(stderr, "[anslicensing] activation request failed (service HTTP status %d) - check network access to the licensing service\n",
generator.GetSigningServiceStatusCode());
throw; throw;
} }
}
if (ValidateActivationKey(activationTemplate.get(), false)) if (ValidateActivationKey(activationTemplate.get(), false))
{ {
fprintf(stderr, "[anslicensing] activation key acquired and validated (service HTTP status %d)\n",
generator.GetSigningServiceStatusCode());
activationStatus = STATUS_SUCCESS; activationStatus = STATUS_SUCCESS;
} }
else 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"); throw new LicensingException(STATUS_GENERIC_ERROR, "internal error - invalid returned activation key");
}
m_licenseValidationResult.m_license = std::make_unique<LicenseImpl>(); m_licenseValidationResult.m_license = std::make_unique<LicenseImpl>();
@@ -533,8 +555,10 @@ public:
{ {
if (parentUsesWideChar) if (parentUsesWideChar)
{ {
static string hwid = w2s(GetCurrentHardwareIdW(parentLicensingClient)); // per-instance storage: a function-local static would freeze the
return hwid.c_str(); // first client's HWID for the lifetime of the process
currentHwidCache = w2s(GetCurrentHardwareIdW(parentLicensingClient));
return currentHwidCache.c_str();
} }
else else
{ {
@@ -587,6 +611,7 @@ private:
string licenseKey; string licenseKey;
string activationKey; string activationKey;
string hardwareId; string hardwareId;
string currentHwidCache;
string licensingServiceUrl; string licensingServiceUrl;
vector<unsigned char> licenseKeyValidationData; vector<unsigned char> licenseKeyValidationData;
+34 -12
View File
@@ -2,6 +2,7 @@
#define _WIN32_DCOM #define _WIN32_DCOM
#include <iostream> #include <iostream>
#include <cstdio>
#include <comdef.h> #include <comdef.h>
#include <Wbemidl.h> #include <Wbemidl.h>
#include <list> #include <list>
@@ -52,10 +53,12 @@ public:
if (FAILED(hres)) if (FAILED(hres))
{ {
if (ownCoInitialize) // Fail soft: leave wbemServices NULL so queries return empty results.
CoUninitialize(); // Throwing here would escape callers (e.g. hardware id generation)
// that have no exception handling and crash the host application.
throw new exception("failed to create IWbemLocator", hres); ReportFailure("CoCreateInstance(IWbemLocator)", hres);
wbemLocator = NULL;
return;
} }
// Connect to the root\cimv2 namespace with the // Connect to the root\cimv2 namespace with the
@@ -76,12 +79,11 @@ public:
if (FAILED(hres)) if (FAILED(hres))
{ {
ReportFailure("ConnectServer(ROOT\\CIMV2)", hres);
wbemLocator->Release(); wbemLocator->Release();
wbemLocator = NULL;
if (ownCoInitialize) wbemServices = NULL;
CoUninitialize(); return;
throw new exception("could not connect to WMI", hres);
} }
// Set the IWbemServices proxy so that impersonation // Set the IWbemServices proxy so that impersonation
@@ -99,10 +101,12 @@ public:
if (FAILED(hres)) if (FAILED(hres))
{ {
ReportFailure("CoSetProxyBlanket", hres);
wbemServices->Release(); wbemServices->Release();
wbemLocator->Release(); wbemLocator->Release();
if (ownCoInitialize) CoUninitialize(); wbemServices = NULL;
throw new exception("could not connecto to WMI", hres); wbemLocator = NULL;
return;
} }
} }
@@ -115,6 +119,9 @@ public:
void GetPropertyList(const char * wqlQuery, const char * propertyName, list<string> * propList, int maxElements = 0x7FFFFFFF) 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; HRESULT hres;
// For example, query for all the running processes // For example, query for all the running processes
@@ -128,7 +135,8 @@ public:
if (FAILED(hres)) if (FAILED(hres))
{ {
throw new exception("WMI query failed"); ReportFailure(wqlQuery, hres);
return;
} }
else else
{ {
@@ -141,6 +149,14 @@ public:
hres = pEnumerator->Next(WBEM_INFINITE, 1, hres = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn); &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) if(0 == uReturn)
{ {
break; break;
@@ -203,6 +219,12 @@ public:
} }
private: 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; bool ownCoInitialize;
IWbemServices * wbemServices; IWbemServices * wbemServices;
IWbemLocator * wbemLocator; 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.
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.*
+412
View File
@@ -0,0 +1,412 @@
// Build ANSLIB Logging Guide.docx — examples document for developers.
// Run: node build_anslib_logging_guide.js
const fs = require('fs');
const path = require('path');
const {
Document, Packer, Paragraph, TextRun, Table, TableRow, TableCell,
AlignmentType, LevelFormat, HeadingLevel, BorderStyle, WidthType,
ShadingType, PageOrientation
} = require('docx');
// -------- helpers ----------------------------------------------------------
const FONT_BODY = "Calibri";
const FONT_CODE = "Consolas";
function p(text, opts = {}) {
return new Paragraph({
spacing: { after: 120 },
...opts,
children: [new TextRun({ text, font: FONT_BODY, size: 22, ...(opts.run || {}) })],
});
}
function h1(text) {
return new Paragraph({
heading: HeadingLevel.HEADING_1,
spacing: { before: 360, after: 200 },
children: [new TextRun({ text, font: FONT_BODY, size: 36, bold: true, color: "1F3864" })],
});
}
function h2(text) {
return new Paragraph({
heading: HeadingLevel.HEADING_2,
spacing: { before: 280, after: 160 },
children: [new TextRun({ text, font: FONT_BODY, size: 28, bold: true, color: "2E74B5" })],
});
}
function h3(text) {
return new Paragraph({
heading: HeadingLevel.HEADING_3,
spacing: { before: 200, after: 100 },
children: [new TextRun({ text, font: FONT_BODY, size: 24, bold: true, color: "404040" })],
});
}
// Single-line inline-code style (uses gray-ish background via shading isn't applied to runs;
// we just use a monospace font + light shading via a wrapping single-cell table when block).
function codeLine(text) {
return new TextRun({ text, font: FONT_CODE, size: 20 });
}
// Multi-line code block — render as a one-cell table with light-gray fill,
// each source line becomes a paragraph inside the cell.
function codeBlock(source) {
const lines = source.replace(/\r\n/g, "\n").split("\n");
const paras = lines.map(line =>
new Paragraph({
spacing: { after: 0, line: 260 },
children: [new TextRun({ text: line || " ", font: FONT_CODE, size: 20 })],
})
);
return new Table({
width: { size: 9360, type: WidthType.DXA },
columnWidths: [9360],
rows: [
new TableRow({
children: [
new TableCell({
width: { size: 9360, type: WidthType.DXA },
shading: { fill: "F4F4F4", type: ShadingType.CLEAR },
margins: { top: 120, bottom: 120, left: 180, right: 180 },
borders: {
top: { style: BorderStyle.SINGLE, size: 4, color: "DDDDDD" },
bottom: { style: BorderStyle.SINGLE, size: 4, color: "DDDDDD" },
left: { style: BorderStyle.SINGLE, size: 4, color: "DDDDDD" },
right: { style: BorderStyle.SINGLE, size: 4, color: "DDDDDD" },
},
children: paras,
}),
],
}),
],
});
}
function bullet(text, runs = null) {
return new Paragraph({
numbering: { reference: "bullets", level: 0 },
spacing: { after: 80 },
children: runs || [new TextRun({ text, font: FONT_BODY, size: 22 })],
});
}
function spacer() {
return new Paragraph({ spacing: { after: 120 }, children: [new TextRun(" ")] });
}
// ---- table builder for the cheat sheets -----------------------------------
const CELL_BORDER = { style: BorderStyle.SINGLE, size: 4, color: "BFBFBF" };
const CELL_BORDERS = { top: CELL_BORDER, bottom: CELL_BORDER, left: CELL_BORDER, right: CELL_BORDER };
function headerCell(text, width) {
return new TableCell({
width: { size: width, type: WidthType.DXA },
borders: CELL_BORDERS,
shading: { fill: "1F3864", type: ShadingType.CLEAR },
margins: { top: 100, bottom: 100, left: 140, right: 140 },
children: [new Paragraph({
children: [new TextRun({ text, font: FONT_BODY, size: 22, bold: true, color: "FFFFFF" })],
})],
});
}
function bodyCell(text, width, opts = {}) {
const runs = Array.isArray(text)
? text
: [new TextRun({ text, font: opts.code ? FONT_CODE : FONT_BODY, size: 20 })];
return new TableCell({
width: { size: width, type: WidthType.DXA },
borders: CELL_BORDERS,
shading: opts.shade ? { fill: opts.shade, type: ShadingType.CLEAR } : undefined,
margins: { top: 80, bottom: 80, left: 140, right: 140 },
children: [new Paragraph({ children: runs })],
});
}
function sheetTable(columnWidths, headers, rows) {
const total = columnWidths.reduce((a, b) => a + b, 0);
return new Table({
width: { size: total, type: WidthType.DXA },
columnWidths,
rows: [
new TableRow({
tableHeader: true,
children: headers.map((h, i) => headerCell(h, columnWidths[i])),
}),
...rows.map(row =>
new TableRow({
children: row.map((c, i) => {
if (c && typeof c === 'object' && c.runs) {
return bodyCell(c.runs, columnWidths[i], c.opts || {});
}
return bodyCell(c, columnWidths[i]);
}),
})
),
],
});
}
// =========================================================================
// Document content
// =========================================================================
const children = [];
// ---------- Title ----------
children.push(new Paragraph({
spacing: { after: 80 },
alignment: AlignmentType.LEFT,
children: [new TextRun({ text: "ANSLIB Logging API", font: FONT_BODY, size: 48, bold: true, color: "1F3864" })],
}));
children.push(new Paragraph({
spacing: { after: 360 },
children: [new TextRun({ text: "Developer guide — usage examples for ANSLIB_DBG, LogInfo, LogError, LogFatal", font: FONT_BODY, size: 24, italics: true, color: "595959" })],
}));
// ---------- Overview ----------
children.push(h1("Overview"));
children.push(p("ANSLIB exposes a small logging API that consumers can use without linking against ANSLicensingSystem.dll or any spdlog headers. All symbols are dynamically loaded inside ANSLIB; users only need to include ANSLIB.h and link ANSLIB.lib."));
children.push(p("Two flavours are provided:"));
children.push(bullet("ANSLIB_DBG — a printf-style macro for trace breadcrumbs that can be turned on or off remotely (DebugView-style logging)."));
children.push(bullet("ANSCENTER::ANSLIB::LogInfo / LogError / LogFatal — leveled logging that writes through SPDLogger to the rolling log file."));
// ---------- Setup ----------
children.push(h1("Setup"));
children.push(p("Same prerequisites for every example below. Add this to any consumer translation unit:"));
children.push(codeBlock(
`#include "ANSLIB.h" // brings everything: static methods + ANSLIB_DBG macro
// Optional alias to shorten call sites:
using LOG = ANSCENTER::ANSLIB;`));
children.push(p("No other includes, no extra .lib to link beyond ANSLIB.lib."));
// ---------- Example 1 ----------
children.push(h1("Example 1 — Function entry/exit (ANSLIB_DBG, the workhorse)"));
children.push(codeBlock(
`int LoadConfig(const std::string& path) {
ANSLIB_DBG("Config", "LoadConfig entry path=%s", path.c_str());
if (path.empty()) {
ANSLIB_DBG("Config", "empty path, bailing");
return -1;
}
int rc = ParseFile(path);
ANSLIB_DBG("Config", "ParseFile returned rc=%d", rc);
return rc;
}`));
children.push(p("DebugView output (only when the gate is on):"));
children.push(codeBlock(
`[Config] LoadConfig entry path=C:\\models\\car.cfg
[Config] ParseFile returned rc=0`));
children.push(p("This is the drop-in replacement for ANS_DBG. Use it for breadcrumb logging that you want to be able to flip on/off remotely."));
// ---------- Example 2 ----------
children.push(h1("Example 2 — Error path (LogError, persisted to spdlog file)"));
children.push(p("When something goes wrong and you want it in the rolling log file (not just DebugView), use LogError:"));
children.push(codeBlock(
`int InitEngine(const std::string& modelPath) {
auto* engine = ANSCENTER::ANSLIB::Create();
if (!engine) {
LOG::LogError("InitEngine", "ANSLIB::Create returned null", __FILE__, __LINE__);
return -1;
}
std::string labels;
int rc = engine->Initialize("license-key", modelPath.c_str(), "",
0.5f, 0.5f, 0.4f, 8, 5, 1, labels);
if (rc < 0) {
char detail[256];
snprintf(detail, sizeof(detail),
"Initialize failed rc=%d model=%s", rc, modelPath.c_str());
LOG::LogError("InitEngine", detail, __FILE__, __LINE__);
ANSCENTER::ANSLIB::Destroy(engine);
return rc;
}
LOG::LogInfo("InitEngine", "engine ready", __FILE__, __LINE__);
return 0;
}`));
children.push(p("Rolling log file output (ANSLIB.log or whichever name spdlog has):"));
children.push(codeBlock(
`[2026-04-26 14:32:11.483] [error] [foo.cpp] [42] InitEngine: Initialize failed rc=-1 model=C:\\models\\car.zip
[2026-04-26 14:32:11.484] [info] [foo.cpp] [50] InitEngine: engine ready`));
// ---------- Example 3 ----------
children.push(h1("Example 3 — Fatal / unrecoverable (LogFatal)"));
children.push(p("Use when the process can't continue — e.g. license check failed, GPU lost, mandatory model missing:"));
children.push(codeBlock(
`void OnGpuDeviceLost() {
LOG::LogFatal("Engine",
"CUDA device lost, cannot continue inference",
__FILE__, __LINE__);
std::abort(); // or whatever your shutdown path is
}`));
// ---------- Example 4 ----------
children.push(h1("Example 4 — Mixing both (recommended pattern)"));
children.push(p("Use ANSLIB_DBG for the chatty trace breadcrumbs and LogInfo / LogError / LogFatal for things you want preserved in the file:"));
children.push(codeBlock(
`int RunBatch(const std::vector<cv::Mat>& frames) {
ANSLIB_DBG("Batch", "RunBatch entry frames=%zu", frames.size());
if (frames.empty()) {
LOG::LogError("Batch", "called with empty frame list", __FILE__, __LINE__);
return -1;
}
int processed = 0;
for (const auto& f : frames) {
ANSLIB_DBG("Batch", "processing frame %d (%dx%d)", processed, f.cols, f.rows);
// ...inference...
++processed;
}
char summary[128];
snprintf(summary, sizeof(summary), "RunBatch OK processed=%d", processed);
LOG::LogInfo("Batch", summary, __FILE__, __LINE__);
ANSLIB_DBG("Batch", "RunBatch exit");
return processed;
}`));
children.push(p("DebugView shows the full trace; the log file keeps just the entry/exit summary. This keeps the file small but lets you toggle full tracing remotely when something goes wrong."));
// ---------- Example 5 ----------
children.push(h1("Example 5 — Gating expensive log preparation"));
children.push(p("ANSLIB_DBG already gates the snprintf for you. But if preparing the message itself is expensive (serializing a struct, dumping a Mat, walking a tree), check the gate first manually:"));
children.push(codeBlock(
`void DumpDetections(const std::vector<ANSCENTER::Object>& dets) {
if (!ANSCENTER::ANSLIB::IsDebugViewEnabled()) return; // skip the whole prep
std::string dump;
for (const auto& d : dets) {
dump += d.className + "(" + std::to_string(d.confidence) + ") ";
}
ANSLIB_DBG("Detections", "%zu objects: %s", dets.size(), dump.c_str());
}`));
children.push(p("This skips the entire string-building loop when the gate is off."));
// ---------- Example 6 ----------
children.push(h1("Example 6 — Logging from a class method"));
children.push(p("The __FILE__ / __LINE__ defaults make instance methods cleaner if you don't need them:"));
children.push(codeBlock(
`class MyDetector {
public:
void Process(const cv::Mat& img) {
ANSLIB_DBG("MyDetector", "Process %dx%d", img.cols, img.rows);
if (img.empty()) {
// No file/line — defaults to "" / 0
LOG::LogError("MyDetector", "empty image input");
return;
}
// ...
}
};`));
// ---------- Example 7 ----------
children.push(h1("Example 7 — Macro for \"log everywhere with caller info\" (optional)"));
children.push(p("If you want every LogInfo call to auto-include __FILE__ / __LINE__, define a one-liner in your own code:"));
children.push(codeBlock(
`#define MYAPP_INFO(tag, msg) ::ANSCENTER::ANSLIB::LogInfo (tag, msg, __FILE__, __LINE__)
#define MYAPP_ERROR(tag, msg) ::ANSCENTER::ANSLIB::LogError(tag, msg, __FILE__, __LINE__)
// Usage:
MYAPP_INFO ("Boot", "starting up");
MYAPP_ERROR("Net", "connection refused");`));
children.push(p("This keeps call sites short while preserving file/line tracking in the rolling log."));
// ---------- Cheat sheet 1 ----------
children.push(h1("Cheat sheet — pick the right call"));
children.push(sheetTable(
[3000, 3360, 3000],
["Want to log…", "Use", "Goes to"],
[
["Trace / breadcrumb that's toggled per-incident",
{ runs: [codeLine('ANSLIB_DBG("Tag", "fmt %d", x)')] },
"DebugView + stderr (when gate is on)"],
["Notable event (started, finished, config loaded)",
{ runs: [codeLine('LOG::LogInfo("Tag", "msg", __FILE__, __LINE__)')] },
"spdlog file + console"],
["Recoverable error (will retry, will skip, etc.)",
{ runs: [codeLine('LOG::LogError("Tag", "msg", __FILE__, __LINE__)')] },
"spdlog file + console"],
["Unrecoverable (about to crash / abort)",
{ runs: [codeLine('LOG::LogFatal("Tag", "msg", __FILE__, __LINE__)')] },
"spdlog file + console (critical level)"],
]
));
children.push(spacer());
// ---------- Cheat sheet 2 ----------
children.push(h1("Toggling the DebugView gate at runtime"));
children.push(p("LogInfo / LogError / LogFatal are not gated — they always go to the spdlog file. Only ANSLIB_DBG is gated by the mechanism below."));
children.push(spacer());
children.push(sheetTable(
[4680, 4680],
["Action", "Effect"],
[
[{ runs: [new TextRun({ text: "Drop file ", font: FONT_BODY, size: 20 }),
codeLine("C:\\ProgramData\\ANSCENTER\\ansvisdebugview.txt")] },
{ runs: [new TextRun({ text: "Turns ", font: FONT_BODY, size: 20 }),
codeLine("ANSLIB_DBG"), new TextRun({ text: " (and ", font: FONT_BODY, size: 20 }),
codeLine("ANS_DBG"), new TextRun({ text: ") ON within ~2s", font: FONT_BODY, size: 20 })] }],
["Delete that file", "Turns them OFF within ~2s"],
[{ runs: [codeLine("set ANSCENTER_DBGVIEW=1"), new TextRun({ text: " (or ", font: FONT_BODY, size: 20 }),
codeLine("=0"), new TextRun({ text: ")", font: FONT_BODY, size: 20 })] },
"Forces ON or OFF — overrides the file"],
]
));
// ---------- Operational notes ----------
children.push(h1("Operational notes"));
children.push(bullet("All four entry points (ANSLIB_DBG, LogInfo, LogError, LogFatal) are thread-safe and require no instance — they're static methods that lazy-resolve their underlying DLL symbols on first call."));
children.push(bullet("If ANSLicensingSystem.dll is missing on the target machine, every logging call silently no-ops (no crash, no exception). The application keeps running."));
children.push(bullet("ANSLIB_DBG output format is identical to the original ANS_DBG: \"[tag] formatted\\n\" emitted to both OutputDebugStringA and stderr."));
children.push(bullet("Don't log from static destructors. DLL unload order during process shutdown is undefined on Windows — calling into the licensing DLL after it has been unmapped will crash."));
children.push(bullet("To strip ANSLIB_DBG entirely at compile time (for performance-critical builds), define -DANSCORE_DEBUGVIEW=0 when compiling the consumer translation unit. The macro becomes ((void)0)."));
// =========================================================================
// Document assembly
// =========================================================================
const doc = new Document({
styles: {
default: { document: { run: { font: FONT_BODY, size: 22 } } },
paragraphStyles: [
{ id: "Heading1", name: "Heading 1", basedOn: "Normal", next: "Normal", quickFormat: true,
run: { size: 36, bold: true, font: FONT_BODY, color: "1F3864" },
paragraph: { spacing: { before: 360, after: 200 }, outlineLevel: 0 } },
{ id: "Heading2", name: "Heading 2", basedOn: "Normal", next: "Normal", quickFormat: true,
run: { size: 28, bold: true, font: FONT_BODY, color: "2E74B5" },
paragraph: { spacing: { before: 280, after: 160 }, outlineLevel: 1 } },
{ id: "Heading3", name: "Heading 3", basedOn: "Normal", next: "Normal", quickFormat: true,
run: { size: 24, bold: true, font: FONT_BODY, color: "404040" },
paragraph: { spacing: { before: 200, after: 100 }, outlineLevel: 2 } },
],
},
numbering: {
config: [
{ reference: "bullets",
levels: [{ level: 0, format: LevelFormat.BULLET, text: "•", alignment: AlignmentType.LEFT,
style: { paragraph: { indent: { left: 720, hanging: 360 } } } }] },
],
},
sections: [{
properties: {
page: {
size: { width: 12240, height: 15840 },
margin: { top: 1440, right: 1440, bottom: 1440, left: 1440 },
},
},
children,
}],
});
const outPath = path.join(__dirname, "ANSLIB_Logging_Guide.docx");
Packer.toBuffer(doc).then(buf => {
fs.writeFileSync(outPath, buf);
console.log("Wrote", outPath, "(" + buf.length + " bytes)");
});
+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_0lqyUmmHw232PxRd6JBSIFKyzSz4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HqLkiDA7BtPNsTZoCdyEBnZ2gzd32Cp1DT_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-IzcEADxDgPKTHTG9XBbsJXUW5zrk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rCs8FuzGSMYK_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_PewCqvpYIWKZNSM3mCT9s4uJzsHnat9XKNYnTGrf82ftRPXie3He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C559o9gcqqgh12mUHTTM3Ik6yEjblF03f-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-Dn9QHW115ChmiUZ1qYQdFvpoGjvbRA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gjuOnUO9QDG1FHdTDaZtQxRPcaH3lUsQRNkvS85gKVf1VT_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_NIx7CPLsWC3qZIvdBhFOCDp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fv4ar-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--E14SL29lP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EaPRMqJnUmysJJkrX8Ti8VsLYby-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."); ") returned null — the DLL may be corrupt.");
s_ort_api = api; s_ort_api = api;
Ort::Global<void>::api_ = api; Ort::InitApi(api);
std::cout << "[EPLoader] ORT loaded successfully." << std::endl; std::cout << "[EPLoader] ORT loaded successfully." << std::endl;
std::cout << "[EPLoader] ORT DLL version : " << base->GetVersionString() << std::endl; std::cout << "[EPLoader] ORT DLL version : " << base->GetVersionString() << std::endl;
+326 -10
View File
@@ -10,9 +10,129 @@
#include <fstream> #include <fstream>
#include <cstdlib> #include <cstdlib>
#include <system_error> #include <system_error>
#include <atomic>
#include <mutex>
namespace ANSCENTER { namespace ANSCENTER {
// ====================================================================
// DmlSerialState / DmlInferenceGuard implementation.
//
// Storage is in this single linkage unit (ONNXEngine.dll) so every
// module linking against ONNXEngine sees the same flag and mutex —
// ANSODEngine.dll's DML calls and ANSOCR.dll's DML calls serialize
// against the same process-wide state. See the doc comment on
// DmlSerialState in ONNXEngine.h for the full design rationale.
// ====================================================================
namespace {
// false = parallel (default). Tripped to true (sticky) once any
// DML site sees an exception or a slow Run().
std::atomic<bool> g_dmlSerialMode{ false };
// Held during DML inference when g_dmlSerialMode == true. Plain
// std::mutex (not recursive): one DML Run() at a time across the
// entire process — that's the whole point. Other ORT operations
// (session creation, allocator setup, etc.) DO NOT need this
// mutex and are not gated by it.
std::mutex g_dmlInferenceMutex;
// 1500 ms — comfortably above normal AMD DML latency (50–500 ms)
// and well below WDDM TDR (10 s with TdrDelay=10).
constexpr double kDmlSlowThresholdMs = 1500.0;
}
bool DmlSerialState::IsSerialMode() {
return g_dmlSerialMode.load(std::memory_order_relaxed);
}
double DmlSerialState::SlowThresholdMs() {
return kDmlSlowThresholdMs;
}
void DmlSerialState::TripFromException(const char* context, const char* msg) {
bool expected = false;
if (g_dmlSerialMode.compare_exchange_strong(expected, true)) {
// First caller wins — log the trip event clearly so it's
// obvious in the field which path serialized DML.
std::cerr << "[DmlSerial] TRIPPED from exception in "
<< (context ? context : "?")
<< " - process-wide DML inference is now SERIALIZED. msg="
<< (msg ? msg : "?") << std::endl;
}
}
void DmlSerialState::TripFromLatency(const char* context, double ms) {
bool expected = false;
if (g_dmlSerialMode.compare_exchange_strong(expected, true)) {
std::cerr << "[DmlSerial] TRIPPED from slow inference in "
<< (context ? context : "?")
<< " ms=" << ms
<< " - process-wide DML inference is now SERIALIZED."
<< std::endl;
}
}
void DmlSerialState::AcquireMutex() {
g_dmlInferenceMutex.lock();
}
void DmlSerialState::ReleaseMutex() {
g_dmlInferenceMutex.unlock();
}
DmlInferenceGuard::DmlInferenceGuard(EngineType ep, const char* context)
: _ep(ep), _context(context), _locked(false)
{
// ── Always-serial-on-AMD policy ────────────────────────────
//
// Earlier design (Option A v1) was lazy: parallel by default,
// serialize only after seeing a slow Run() or exception. Field
// testing on AMD Radeon 780M proved this fires too late — by the
// time the latency trip arms, a sibling thread has already
// entered Run() at depth=2 and wedged the D3D12 command queue.
// ORT then crashes inside its own DML EP cleanup when the WDDM
// TDR fires.
//
// The only way to guarantee zero dual-stall on AMD is to never
// let two threads be in flight on DML simultaneously. This
// constructor unconditionally takes the global DML mutex when
// ep == AMD_GPU. Intel (OpenVINO) and NVIDIA (CUDA/TRT) skip
// the lock entirely — guard collapses to a timestamp read +
// comparison, and the destructor is a single comparison. Zero
// measurable overhead on those platforms.
//
// Trade-off: AMD inference becomes serial (depth=1) instead of
// parallel (depth=2). Each individual call still runs on the
// GPU at GPU speeds; only the cross-thread overlap is removed.
// Throughput cost vs broken depth=2 is ~30% lower wall-clock
// throughput per frame, but uptime goes from ~30 s before
// wedge → unbounded.
if (_ep == EngineType::AMD_GPU) {
DmlSerialState::AcquireMutex();
_locked = true;
}
// Start the latency timer AFTER lock acquisition so contention
// on the global mutex doesn't itself trip the slow-Run()
// threshold. We measure GPU Run() time, not queue wait.
_t0 = std::chrono::steady_clock::now();
}
DmlInferenceGuard::~DmlInferenceGuard() {
// Latency-based trip detection: if THIS Run() took longer than
// the slow threshold, future DML inference goes serial. Only
// checked on AMD — Intel/NVIDIA never trip.
if (_ep == EngineType::AMD_GPU) {
const auto t1 = std::chrono::steady_clock::now();
const double ms = std::chrono::duration<double, std::milli>(t1 - _t0).count();
if (ms > kDmlSlowThresholdMs) {
DmlSerialState::TripFromLatency(_context, ms);
}
}
if (_locked) {
DmlSerialState::ReleaseMutex();
}
}
// ==================================================================== // ====================================================================
// BasicOrtHandler — constructors // BasicOrtHandler — constructors
// ==================================================================== // ====================================================================
@@ -21,7 +141,7 @@ namespace ANSCENTER {
{ {
try { try {
// Make sure the Ort API pointer is initialised in THIS DLL. // 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())); Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
} }
@@ -101,8 +221,45 @@ namespace ANSCENTER {
initialize_handler(); 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() 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) { if (ort_session) {
delete ort_session; delete ort_session;
ort_session = nullptr; ort_session = nullptr;
@@ -365,6 +522,85 @@ namespace ANSCENTER {
return false; return false;
} }
// ====================================================================
// Reinitialize — tear down + rebuild session on a new EP.
//
// Used by subclass self-heal paths (ONNXOCRRecognizer /
// ONNXOCRDetector) to recover from Ort::Exception in Run(). The
// typical flow is:
//
// try { ort_session->Run(...); }
// catch (const Ort::Exception&) {
// Reinitialize(EngineType::AMD_GPU); // first attempt: retry DML
// try { ort_session->Run(...); }
// catch (const Ort::Exception&) {
// Reinitialize(EngineType::CPU); // last resort: drop to CPU
// ort_session->Run(...);
// }
// }
//
// After this call returns, ort_session, ort_env, memory_info_handler,
// input_node_names, input_node_dims, output_node_names, etc. are all
// freshly populated for `newEngine`. Subclass state that lives outside
// BasicOrtHandler (e.g. ONNXOCRRecognizer::_warmedUp, ::keys_) is NOT
// touched — the caller should reset whatever flags it owns if they
// become invalid for the new session.
//
// Throws Ort::Exception (or std::exception) on failure to construct
// the new session. Caller should treat that as terminal for the
// current Run() and return an empty result.
// ====================================================================
void BasicOrtHandler::Reinitialize(EngineType newEngine)
{
ANS_DBG("OrtHandler",
"Reinitialize: oldEngine=%d newEngine=%d (model=%ls)",
static_cast<int>(m_engineType),
static_cast<int>(newEngine),
onnx_path ? onnx_path : L"(null)");
// Drop the existing session FIRST so its D3D12 / DML allocations
// are released back to the driver. Order matters: ort_session
// must die before ort_env, otherwise Ort::Session's destructor
// can dereference a dangling env pointer in some EP teardown
// paths (observed on DML 1.15.x).
if (ort_session) {
try { delete ort_session; } catch (...) {}
ort_session = nullptr;
}
if (memory_info_handler) {
try { delete memory_info_handler; } catch (...) {}
memory_info_handler = nullptr;
}
if (ort_env) {
try { delete ort_env; } catch (...) {}
ort_env = nullptr;
}
// Reset shape/name vectors so initialize_handler repopulates
// them from the new session (input names can differ across EPs
// for some models — known DML EP quirk).
input_node_names.clear();
input_node_names_.clear();
input_node_dims.clear();
input_values_handler.clear();
output_node_names.clear();
output_node_names_.clear();
output_node_dims.clear();
input_name = nullptr;
input_tensor_size = 1;
num_outputs = 1;
// Set the requested EP and re-run the standard init. Throws on
// failure — caller's responsibility to handle.
m_engineType = newEngine;
initialize_handler();
ANS_DBG("OrtHandler",
"Reinitialize OK: ort_session=%p engine=%d",
static_cast<void*>(ort_session),
static_cast<int>(m_engineType));
}
// ==================================================================== // ====================================================================
// initialize_handler // initialize_handler
// ==================================================================== // ====================================================================
@@ -375,10 +611,10 @@ namespace ANSCENTER {
const auto& epInfo = EPLoader::Current(); const auto& epInfo = EPLoader::Current();
ANS_DBG("OrtHandler", "initialize_handler: EPLoader type=%d dir=%s", ANS_DBG("OrtHandler", "initialize_handler: EPLoader type=%d dir=%s",
static_cast<int>(epInfo.type), epInfo.libraryDir.c_str()); 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())); 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) EngineType engine = (static_cast<int>(m_engineType) == -1)
? epInfo.type : m_engineType; ? epInfo.type : m_engineType;
@@ -528,20 +764,26 @@ namespace ANSCENTER {
// AddExternalInitializersFromFilesInMemory() so ORT never // AddExternalInitializersFromFilesInMemory() so ORT never
// memory-maps the file itself. // memory-maps the file itself.
// ---------------------------------------------------------------- // ----------------------------------------------------------------
std::filesystem::path modelFsPath(onnx_path); // wchar_t* // A model in memory has no directory, no sibling .onnx_data and
std::filesystem::path modelDir = modelFsPath.parent_path(); // 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(); std::filesystem::path prevCwd = std::filesystem::current_path();
if (!fromMemory()) {
modelFsPath = std::filesystem::path(onnx_path); // wchar_t*
modelDir = modelFsPath.parent_path();
if (!modelDir.empty() && std::filesystem::is_directory(modelDir)) { if (!modelDir.empty() && std::filesystem::is_directory(modelDir)) {
std::filesystem::current_path(modelDir); std::filesystem::current_path(modelDir);
std::cout << "[ORT] CWD -> " << modelDir.string() << std::endl; std::cout << "[ORT] CWD -> " << modelDir.string() << std::endl;
} }
}
// --- Pre-load external data files if they exist ----------------- // --- Pre-load external data files if they exist -----------------
// Keep the buffer alive across session creation (must outlive the // Keep the buffer alive across session creation (must outlive the
// Ort::Session constructor call). // Ort::Session constructor call).
std::vector<char> extDataBuffer; std::vector<char> extDataBuffer;
{ if (!fromMemory()) {
// Build the expected external-data filename: // Build the expected external-data filename:
// <model_stem>.onnx_data (e.g. anssam3.onnx_data) // <model_stem>.onnx_data (e.g. anssam3.onnx_data)
std::filesystem::path extDataPath = std::filesystem::path extDataPath =
@@ -607,7 +849,7 @@ namespace ANSCENTER {
// This avoids ORT opening/mapping ANY files during CreateSession. // This avoids ORT opening/mapping ANY files during CreateSession.
std::vector<char> modelBuffer; std::vector<char> modelBuffer;
bool useModelBuffer = false; bool useModelBuffer = false;
if (!extDataBuffer.empty()) { if (!extDataBuffer.empty() && !fromMemory()) {
// External data was pre-loaded, so also load the .onnx itself // External data was pre-loaded, so also load the .onnx itself
try { try {
auto modelFileSize = std::filesystem::file_size(modelFsPath); auto modelFileSize = std::filesystem::file_size(modelFsPath);
@@ -630,7 +872,10 @@ namespace ANSCENTER {
// --- Attempt session creation (with CUDA → CPU fallback) -------- // --- Attempt session creation (with CUDA → CPU fallback) --------
auto createSession = [&](Ort::SessionOptions& opts, const char* label) { auto createSession = [&](Ort::SessionOptions& opts, const char* label) {
std::cout << "[ORT] Creating session (" << label << ")..." << std::endl; 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, ort_session = new Ort::Session(*ort_env,
modelBuffer.data(), modelBuffer.size(), opts); modelBuffer.data(), modelBuffer.size(), opts);
} else { } else {
@@ -639,6 +884,10 @@ namespace ANSCENTER {
std::cout << "[ORT] Session created OK (" << label << ")." << std::endl; std::cout << "[ORT] Session created OK (" << label << ")." << std::endl;
}; };
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); ANS_DBG("OrtHandler", "Creating session for model: %ls", onnx_path);
try { try {
createSession(session_options, "primary EP"); createSession(session_options, "primary EP");
@@ -819,7 +1068,10 @@ namespace ANSCENTER {
input_values_handler.clear(); input_values_handler.clear();
input_values_handler.shrink_to_fit(); input_values_handler.shrink_to_fit();
const float* vals = output_tensors[0].GetTensorData<float>(); // GetTensorMutableData on DirectML — the const GetTensorData
// triggers a per-call host-readable mapping that on AMD DML
// exhausts a staging-buffer pool after ~8 calls and hangs.
const float* vals = output_tensors[0].GetTensorMutableData<float>();
const unsigned int hidden_dim = const unsigned int hidden_dim =
static_cast<unsigned int>(output_node_dims.at(0).at(1)); static_cast<unsigned int>(output_node_dims.at(0).at(1));
@@ -842,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 // GlintCosFace
// ==================================================================== // ====================================================================
@@ -1377,7 +1683,9 @@ namespace ANSCENTER {
Ort::RunOptions{ nullptr }, Ort::RunOptions{ nullptr },
in_names, &input_tensor, 1, out_names, 1); in_names, &input_tensor, 1, out_names, 1);
out_result = post_processing(outputs[0].GetTensorData<float>()); // GetTensorMutableData (not GetTensorData) — see comment in this
// file's other output-read sites; const GetTensorData hangs on AMD DML.
out_result = post_processing(outputs[0].GetTensorMutableData<float>());
} }
Ort::Value MOVINET::transform(const cv::Mat& mat) Ort::Value MOVINET::transform(const cv::Mat& mat)
@@ -1696,4 +2004,12 @@ namespace ANSCENTER {
return std::fabs(width() * height()); 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 } // namespace ANSCENTER
+192 -3
View File
@@ -8,6 +8,7 @@
#include <typeinfo> #include <typeinfo>
#include <deque> #include <deque>
#include <unordered_map> #include <unordered_map>
#include <chrono> // DmlInferenceGuard latency timing
#include "onnxruntime_cxx_api.h" #include "onnxruntime_cxx_api.h"
#include "opencv2/opencv.hpp" #include "opencv2/opencv.hpp"
@@ -72,9 +73,10 @@ namespace ANSCENTER {
} }
}; };
template class BoundingBoxType<int, float>; // Explicit instantiations now live at the end of ONNXEngine.cpp
template class BoundingBoxType<float, float>; // (after the template member definitions) so the compiler actually
template class BoundingBoxType<double, double>; // 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<int, float> Boxi;
typedef BoundingBoxType<float, float> Boxf; typedef BoundingBoxType<float, float> Boxf;
@@ -337,6 +339,108 @@ namespace ANSCENTER {
// (AMD fields go here — none yet) // (AMD fields go here — none yet)
}; };
// ====================================================================
// DmlSerialState — process-wide DirectML inference serialization
//
// AMD Radeon iGPUs (Radeon 680M / 780M, observed) cannot sustain
// 2-way concurrent inference on DirectML. Under sustained
// parallel load the WDDM driver eventually deadlocks the D3D12
// command queue when multiple ORT sessions submit work
// simultaneously, producing a "dual stall" pattern (both threads
// inside ORT::Run() with no exception thrown) and ultimately an
// access violation deep inside the DirectML EP cleanup path.
//
// Policy: every DirectML inference call on AMD_GPU is serialized
// through DmlInferenceGuard's unconditional lock. Lazy "trip on
// first slow Run()" was tried first (a flag flipped to true on
// exception or >1500 ms latency) but field-proven inadequate —
// the trip fires AFTER a slow Run() returns, by which point a
// sibling thread has already entered Run() concurrently and
// wedged. Always-serialize is the only safe answer on AMD.
//
// This class still tracks an "ever-tripped" flag for diagnostics
// — the TripFromException / TripFromLatency calls log a marker
// the first time a DML hiccup is observed, so field traces
// clearly show whether the AMD driver had a degraded event. The
// flag does NOT gate the lock; the lock is unconditional on AMD.
//
// Intel (OpenVINO) and NVIDIA (CUDA/TRT) bypass the lock entirely
// — DmlInferenceGuard short-circuits on ep != AMD_GPU. They keep
// peakDepth=2 parallel inference unchanged; zero behavioral
// change on those platforms.
//
// Storage lives in the ONNXEngine sources compiled into
// ANSODEngine.dll so all higher-level modules (ANSODEngine,
// ANSOCR, ...) share a single process-global mutex.
// ====================================================================
class ONNXENGINE_API DmlSerialState {
public:
// True when serial mode has been tripped.
static bool IsSerialMode();
// Trip helpers. Both are idempotent — only the first caller
// per process actually flips the flag and emits the
// diagnostic. Called by:
// exception path → ANSONNXYOLO::HandleDmlRunFailure, OCR
// latency path → DmlInferenceGuard destructor on slow Run()
static void TripFromException(const char* context, const char* msg);
static void TripFromLatency (const char* context, double ms);
// 1500 ms — comfortably above normal AMD DML latency and well
// under WDDM TDR.
static double SlowThresholdMs();
// Internal — only DmlInferenceGuard should call these.
static void AcquireMutex();
static void ReleaseMutex();
};
// ====================================================================
// DmlInferenceGuard — RAII guard for DirectML inference call sites
//
// Construct around every Ort::Session::Run() that may execute on
// a DirectML EP. Behavior:
//
// 1. ep != AMD_GPU → entire guard is a no-op (zero overhead on
// Intel / NVIDIA / CPU). The destructor's latency check and
// lock release are also skipped via _ep == AMD_GPU and
// _locked == false guards.
// 2. ep == AMD_GPU → unconditionally acquires the global DML
// mutex; only one DML inference in the entire process runs
// at a time. On scope exit, releases the mutex and (if the
// call exceeded DmlSerialState::SlowThresholdMs()) records a
// diagnostic trip marker — the marker does NOT change
// behavior, since AMD is already serial; it just shows up in
// logs as evidence of a degraded GPU event.
//
// Why unconditional serialization on AMD: empirical testing on
// Radeon 780M shows the WDDM driver wedges when 2 ORT sessions
// submit DML inference concurrently for >~30 s. Lazy "lock only
// after first slow call" was found insufficient — by the time the
// trip fires, a sibling thread is already inside the wedge-prone
// Run() with no way to back out. Always-lock is the only path
// that guarantees the dual-stall window never opens. The cost is
// ~30% lower throughput on AMD (depth=1 vs depth=2) for
// unbounded uptime instead of crashing every 30 s.
// ====================================================================
class ONNXENGINE_API DmlInferenceGuard {
public:
explicit DmlInferenceGuard(EngineType ep, const char* context);
~DmlInferenceGuard();
// Non-copyable / non-movable — RAII scope guard only.
DmlInferenceGuard(const DmlInferenceGuard&) = delete;
DmlInferenceGuard& operator=(const DmlInferenceGuard&) = delete;
DmlInferenceGuard(DmlInferenceGuard&&) = delete;
DmlInferenceGuard& operator=(DmlInferenceGuard&&) = delete;
private:
EngineType _ep;
const char* _context;
bool _locked;
std::chrono::steady_clock::time_point _t0;
};
// ==================================================================== // ====================================================================
// BasicOrtHandler // BasicOrtHandler
// ==================================================================== // ====================================================================
@@ -364,6 +468,25 @@ namespace ANSCENTER {
const LITEORT_CHAR* onnx_path = nullptr; // ← points into onnx_path_w const LITEORT_CHAR* onnx_path = nullptr; // ← points into onnx_path_w
const char* log_id = nullptr; 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: protected:
const unsigned int num_threads; const unsigned int num_threads;
@@ -395,6 +518,23 @@ namespace ANSCENTER {
const OrtHandlerOptions& options, const OrtHandlerOptions& options,
unsigned int _num_threads = 1); 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(); virtual ~BasicOrtHandler();
BasicOrtHandler(const BasicOrtHandler&) = delete; BasicOrtHandler(const BasicOrtHandler&) = delete;
@@ -410,6 +550,21 @@ namespace ANSCENTER {
// (which require the input name) BEFORE the real session is // (which require the input name) BEFORE the real session is
// created. Returns an empty string on failure. // created. Returns an empty string on failure.
static std::string QueryModelInputName(const std::string& onnxPath); static std::string QueryModelInputName(const std::string& onnxPath);
protected:
// Tear down the existing ORT session/env/memory-info and rebuild
// them with `newEngine` as the requested EP. Used by subclass
// self-heal paths (e.g. ONNXOCRRecognizer / ONNXOCRDetector) when
// an Ort::Exception fires from a Run() — the session is recreated
// either on the same EP (transient driver hiccup recovery) or on
// CPU (permanent fallback). Throws on failure; caller should
// treat that as "give up for this call" and return an empty
// result. Subclass-owned auxiliary state (input_node_dims,
// input_node_names, ...) is repopulated by initialize_handler.
// Caller is responsible for any subclass-specific re-init that
// sits on top of the base session (e.g. dictionary loading on
// ONNXOCRRecognizer is not affected because it lives in the
// subclass and survives a session swap).
void Reinitialize(EngineType newEngine);
private: private:
void initialize_handler(); void initialize_handler();
protected: protected:
@@ -555,6 +710,40 @@ namespace ANSCENTER {
Ort::Value transformBatch(const std::vector<cv::Mat>& images) override; 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 // MOVINET — action recognition
// ==================================================================== // ====================================================================
+11 -9
View File
@@ -331,7 +331,7 @@ namespace ANSCENTER
{ {
// Initialize ORT API // Initialize ORT API
const auto& epInfo = EPLoader::Current(); 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())); Ort::InitApi(static_cast<const OrtApi*>(EPLoader::GetOrtApiRaw()));
m_env = new Ort::Env(ORT_LOGGING_LEVEL_ERROR, "ONNXSAM3"); m_env = new Ort::Env(ORT_LOGGING_LEVEL_ERROR, "ONNXSAM3");
@@ -463,7 +463,9 @@ namespace ANSCENTER
auto info = outputs[maskIdx].GetTensorTypeAndShapeInfo(); auto info = outputs[maskIdx].GetTensorTypeAndShapeInfo();
m_cachedLangMaskShape = info.GetShape(); m_cachedLangMaskShape = info.GetShape();
size_t count = info.GetElementCount(); size_t count = info.GetElementCount();
const bool* data = outputs[maskIdx].GetTensorData<bool>(); // GetTensorMutableData not GetTensorData on DML — const variant
// hangs after ~8 calls. Read-only despite the "Mutable" name.
const bool* data = outputs[maskIdx].GetTensorMutableData<bool>();
m_cachedLangMask.resize(count); m_cachedLangMask.resize(count);
for (size_t i = 0; i < count; ++i) for (size_t i = 0; i < count; ++i)
m_cachedLangMask[i] = data[i] ? 1 : 0; m_cachedLangMask[i] = data[i] ? 1 : 0;
@@ -474,7 +476,7 @@ namespace ANSCENTER
auto info = outputs[featIdx].GetTensorTypeAndShapeInfo(); auto info = outputs[featIdx].GetTensorTypeAndShapeInfo();
m_cachedLangFeaturesShape = info.GetShape(); m_cachedLangFeaturesShape = info.GetShape();
size_t count = info.GetElementCount(); size_t count = info.GetElementCount();
const float* data = outputs[featIdx].GetTensorData<float>(); const float* data = outputs[featIdx].GetTensorMutableData<float>();
m_cachedLangFeatures.assign(data, data + count); m_cachedLangFeatures.assign(data, data + count);
} }
@@ -649,7 +651,7 @@ namespace ANSCENTER
if (elemType == ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT && !shape.empty()) { if (elemType == ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT && !shape.empty()) {
size_t numElems = info.GetElementCount(); size_t numElems = info.GetElementCount();
if (numElems > 0 && numElems < 100000000) { if (numElems > 0 && numElems < 100000000) {
const float* data = decInputs[di].GetTensorData<float>(); const float* data = decInputs[di].GetTensorMutableData<float>();
double sum = 0; double sum = 0;
for (size_t k = 0; k < numElems; ++k) sum += data[k]; for (size_t k = 0; k < numElems; ++k) sum += data[k];
double mean = sum / numElems; double mean = sum / numElems;
@@ -661,14 +663,14 @@ namespace ANSCENTER
// Print bool tensor values (for language_mask) // Print bool tensor values (for language_mask)
else if (elemType == ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL && !shape.empty()) { else if (elemType == ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL && !shape.empty()) {
size_t numElems = info.GetElementCount(); size_t numElems = info.GetElementCount();
const bool* data = decInputs[di].GetTensorData<bool>(); const bool* data = decInputs[di].GetTensorMutableData<bool>();
std::cout << " vals:"; std::cout << " vals:";
for (size_t k = 0; k < std::min(numElems, (size_t)32); ++k) for (size_t k = 0; k < std::min(numElems, (size_t)32); ++k)
std::cout << " " << (int)data[k]; std::cout << " " << (int)data[k];
} }
// Print int64 scalar value // Print int64 scalar value
else if (elemType == ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64 && shape.empty()) { else if (elemType == ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64 && shape.empty()) {
const int64_t* data = decInputs[di].GetTensorData<int64_t>(); const int64_t* data = decInputs[di].GetTensorMutableData<int64_t>();
std::cout << " value=" << data[0]; std::cout << " value=" << data[0];
} }
std::cout << std::endl; std::cout << std::endl;
@@ -702,10 +704,10 @@ namespace ANSCENTER
auto boxInfo = decOutputs[boxesIdx].GetTensorTypeAndShapeInfo(); auto boxInfo = decOutputs[boxesIdx].GetTensorTypeAndShapeInfo();
auto boxShape = boxInfo.GetShape(); auto boxShape = boxInfo.GetShape();
int numBoxes = (boxShape.size() >= 1) ? static_cast<int>(boxShape[0]) : 0; int numBoxes = (boxShape.size() >= 1) ? static_cast<int>(boxShape[0]) : 0;
const float* boxesData = decOutputs[boxesIdx].GetTensorData<float>(); const float* boxesData = decOutputs[boxesIdx].GetTensorMutableData<float>();
// Get scores // Get scores
const float* scoresData = decOutputs[scoresIdx].GetTensorData<float>(); const float* scoresData = decOutputs[scoresIdx].GetTensorMutableData<float>();
// Get masks // Get masks
auto maskInfo = decOutputs[masksIdx].GetTensorTypeAndShapeInfo(); auto maskInfo = decOutputs[masksIdx].GetTensorTypeAndShapeInfo();
@@ -713,7 +715,7 @@ namespace ANSCENTER
// masks shape: [N, 1, H, W] // masks shape: [N, 1, H, W]
int maskH = (maskShape.size() >= 3) ? static_cast<int>(maskShape[2]) : 0; int maskH = (maskShape.size() >= 3) ? static_cast<int>(maskShape[2]) : 0;
int maskW = (maskShape.size() >= 4) ? static_cast<int>(maskShape[3]) : 0; int maskW = (maskShape.size() >= 4) ? static_cast<int>(maskShape[3]) : 0;
const bool* masksData = decOutputs[masksIdx].GetTensorData<bool>(); const bool* masksData = decOutputs[masksIdx].GetTensorMutableData<bool>();
m_maskH = maskH; m_maskH = maskH;
m_maskW = maskW; m_maskW = maskW;
@@ -4,6 +4,7 @@
#include <sstream> #include <sstream>
#include "Utility.h" #include "Utility.h"
#include "TRTCompat.h" #include "TRTCompat.h"
#include "GpuMemoryGuard.h"
// ============================================================================ // ============================================================================
// Crash-safe wrappers for TensorRT operations that can crash the process. // 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 // It is full path
std::string engineName = serializeEngineOptions(m_options, onnxModelPath); std::string engineName = serializeEngineOptions(m_options, onnxModelPath);
std::string engineDir = m_options.engineFileDir; std::string engineDir = m_options.engineFileDir;
m_engineFilePath = engineName;
m_rebuiltFromOnnx = false;
if (FileExist(engineName)) { if (FileExist(engineName)) {
if (m_verbose) { std::cout << "Engine file found: " << engineName << std::endl; } if (m_verbose) { std::cout << "Engine file found: " << engineName << std::endl; }
@@ -313,7 +316,7 @@ bool Engine<T>::buildLoadNetwork(std::string onnxModelPath, const std::array<flo
return false; return false;
} }
} }
if (!FileExist(onnxModelPath)) { if (!hasOnnxBytes() && !FileExist(onnxModelPath)) {
// ONNX model does not exist, try to find alternative precision engine // ONNX model does not exist, try to find alternative precision engine
std::cout << "Searching for alternative precision engine..." << std::endl; std::cout << "Searching for alternative precision engine..." << std::endl;
@@ -334,6 +337,7 @@ bool Engine<T>::buildLoadNetwork(std::string onnxModelPath, const std::array<flo
if (FileExist(alternativeEngineName)) { if (FileExist(alternativeEngineName)) {
std::cout << "Found alternative precision engine: " << alternativeEngineName << std::endl; std::cout << "Found alternative precision engine: " << alternativeEngineName << std::endl;
m_engineFilePath = alternativeEngineName;
return loadNetwork(alternativeEngineName, subVals, divVals, normalize); return loadNetwork(alternativeEngineName, subVals, divVals, normalize);
} }
else { 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 << "FP16 engine not found, but FP32 engine exists: " << fp32EngineName << std::endl;
std::cout << "Loading existing FP32 engine..." << std::endl; std::cout << "Loading existing FP32 engine..." << std::endl;
m_options.precision = ANSCENTER::Precision::FP32; m_options.precision = ANSCENTER::Precision::FP32;
m_engineFilePath = fp32EngineName;
return loadNetwork(fp32EngineName, subVals, divVals, normalize); return loadNetwork(fp32EngineName, subVals, divVals, normalize);
} }
} }
// ONNX model exists, generate engine // ONNX model exists, generate engine
m_rebuiltFromOnnx = true;
std::cout << "========================================" << std::endl; std::cout << "========================================" << std::endl;
std::cout << "Engine not found, generating from ONNX model" << std::endl; std::cout << "Engine not found, generating from ONNX model" << std::endl;
std::cout << "========================================" << std::endl; std::cout << "========================================" << std::endl;
@@ -384,12 +390,9 @@ bool Engine<T>::buildLoadNetwork(std::string onnxModelPath, const std::array<flo
auto tempNetwork = std::unique_ptr<nvinfer1::INetworkDefinition>(TRT_CREATE_NETWORK(tempBuilder)); auto tempNetwork = std::unique_ptr<nvinfer1::INetworkDefinition>(TRT_CREATE_NETWORK(tempBuilder));
auto tempParser = std::unique_ptr<nvonnxparser::IParser>(nvonnxparser::createParser(*tempNetwork, m_logger)); auto tempParser = std::unique_ptr<nvonnxparser::IParser>(nvonnxparser::createParser(*tempNetwork, m_logger));
std::ifstream onnxFile(onnxModelPath, std::ios::binary | std::ios::ate); std::vector<char> onnxBuffer;
std::streamsize onnxSize = onnxFile.tellg(); if (!readOnnxBytes(onnxModelPath, onnxBuffer)) {
onnxFile.seekg(0, std::ios::beg); std::cout << "Error: Failed to read ONNX model" << std::endl;
std::vector<char> onnxBuffer(onnxSize);
if (!onnxFile.read(onnxBuffer.data(), onnxSize)) {
std::cout << "Error: Failed to read ONNX file" << std::endl;
return false; return false;
} }
@@ -805,13 +808,23 @@ bool Engine<T>::loadNetwork(std::string trtModelPath, const std::array<float, 3>
// (cudaMalloc failure during inference) when too many tasks are loaded. // (cudaMalloc failure during inference) when too many tasks are loaded.
{ {
size_t freeVRAM = 0, totalVRAM = 0; size_t freeVRAM = 0, totalVRAM = 0;
cudaError_t memErr = cudaMemGetInfo(&freeVRAM, &totalVRAM); bool haveMem = ANSCENTER::GpuMemoryGuard::Query(freeVRAM, totalVRAM);
constexpr size_t kMinFreeBytes = 256ULL * 1024 * 1024; // 256 MiB minimum // (1) Admission control: refuse to load this engine on the GPU if it
if (memErr != cudaSuccess) { // does not fit while keeping the GpuMemoryGuard safety margin free.
// cudaMemGetInfo failed — CUDA context may not be initialized on this thread. // Prevents VRAM over-subscription across concurrent engines (FR/OD/LPR/
// Don't reject: let TRT try to deserialize (it may succeed). // liveness) that would otherwise OOM and permanently poison the CUDA
} else if (freeVRAM < kMinFreeBytes) { // context. The engine file is valid — m_lastLoadFailedVRAM tells the
m_lastLoadFailedVRAM = true; // signal to buildLoadNetwork: engine file is NOT corrupt // 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; return false;
} }
if (m_verbose) { if (m_verbose) {
@@ -1425,7 +1438,15 @@ trt_cache_create_context:
// ============================================================================ // ============================================================================
if (m_verbose) std::cout << "\nInfo: Starting warm-up inference..." << std::endl; 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; if (m_verbose) std::cout << "Info: Warm-up complete" << std::endl;
} // end of trt_cache_create_context scope } // end of trt_cache_create_context scope
@@ -1441,7 +1462,7 @@ bool Engine<T>::build(std::string onnxModelPath, const std::array<float, 3>& sub
std::cout << "Engine file already exists: " << engineName << std::endl; std::cout << "Engine file already exists: " << engineName << std::endl;
return true; return true;
} }
if (!FileExist(onnxModelPath)) { if (!hasOnnxBytes() && !FileExist(onnxModelPath)) {
std::cout << "Error: ONNX model file does not exist: " << onnxModelPath << std::endl; std::cout << "Error: ONNX model file does not exist: " << onnxModelPath << std::endl;
return false; return false;
} }
@@ -1478,16 +1499,14 @@ bool Engine<T>::build(std::string onnxModelPath, const std::array<float, 3>& sub
return false; return false;
} }
// Read the onnx file into memory // Get the onnx bytes: from the in-memory buffer of a sealed package, or
std::ifstream file(onnxModelPath, std::ios::binary | std::ios::ate); // from the file on disk.
std::streamsize size = file.tellg(); std::vector<char> buffer;
file.seekg(0, std::ios::beg); if (!readOnnxBytes(onnxModelPath, buffer)) {
std::cout << "Error: Unable to read ONNX model" << std::endl;
std::vector<char> buffer(size);
if (!file.read(buffer.data(), size)) {
std::cout << "Error: Unable to read ONNX file" << std::endl;
return false; return false;
} }
const std::streamsize size = static_cast<std::streamsize>(buffer.size());
std::cout << "ONNX model size: " << size / (1024 * 1024) << " MiB" << std::endl; std::cout << "ONNX model size: " << size / (1024 * 1024) << " MiB" << std::endl;
@@ -1798,6 +1817,25 @@ bool Engine<T>::build(std::string onnxModelPath, const std::array<float, 3>& sub
// Set hardware compatibility // Set hardware compatibility
config->setHardwareCompatibilityLevel(nvinfer1::HardwareCompatibilityLevel::kNONE); 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 ---------------------------------------- // -- TensorRT 10+ tactic DRAM pool ----------------------------------------
// Separate scratch pool for kernel-selection during build. Without this, // Separate scratch pool for kernel-selection during build. Without this,
// tactic evaluation competes with the workspace allocation and tactics // tactic evaluation competes with the workspace allocation and tactics
@@ -2411,6 +2449,12 @@ bool Engine<T>::buildWithRetry(std::string onnxModelPath,
const std::array<float, 3>& divVals, const std::array<float, 3>& divVals,
bool normalize) bool normalize)
{ {
ANS_DBG("TRT_Build", "buildWithRetry ENTRY onnx=%s normalize=%d optMaxHW=%dx%d optOptHW=%dx%d optMinHW=%dx%d",
onnxModelPath.c_str(), (int)normalize,
m_options.maxInputHeight, m_options.maxInputWidth,
m_options.optInputHeight, m_options.optInputWidth,
m_options.minInputHeight, m_options.minInputWidth);
// -- Quick pre-analysis: detect dynamic spatial dims in ONNX --------------- // -- Quick pre-analysis: detect dynamic spatial dims in ONNX ---------------
bool hasDynamicSpatial = false; bool hasDynamicSpatial = false;
int onnxFixedH = 0, onnxFixedW = 0; // 0 = dynamic (-1 in ONNX) int onnxFixedH = 0, onnxFixedW = 0; // 0 = dynamic (-1 in ONNX)
@@ -2422,16 +2466,20 @@ bool Engine<T>::buildWithRetry(std::string onnxModelPath,
auto tempParser = std::unique_ptr<nvonnxparser::IParser>( auto tempParser = std::unique_ptr<nvonnxparser::IParser>(
nvonnxparser::createParser(*tempNetwork, m_logger)); nvonnxparser::createParser(*tempNetwork, m_logger));
std::ifstream onnxFile(onnxModelPath, std::ios::binary | std::ios::ate); std::vector<char> onnxBuffer;
if (onnxFile.good()) { const bool onnxReadable = readOnnxBytes(onnxModelPath, onnxBuffer);
std::streamsize onnxSize = onnxFile.tellg(); if (!onnxReadable) {
onnxFile.seekg(0, std::ios::beg); ANS_DBG("TRT_Build", "buildWithRetry WARN cannot open ONNX for pre-parse path=%s",
std::vector<char> onnxBuffer(onnxSize); onnxModelPath.c_str());
if (onnxFile.read(onnxBuffer.data(), onnxSize)) { }
if (onnxReadable) {
{
unsigned long sehRetryParse = 0; unsigned long sehRetryParse = 0;
bool retryParsed = parseOnnxModelSafe(tempParser.get(), bool retryParsed = parseOnnxModelSafe(tempParser.get(),
onnxBuffer.data(), onnxBuffer.size(), &sehRetryParse); onnxBuffer.data(), onnxBuffer.size(), &sehRetryParse);
if (sehRetryParse != 0) { if (sehRetryParse != 0) {
ANS_DBG("TRT_Build", "buildWithRetry WARN SEH=0x%lx during pre-parse — fall through to single build()",
sehRetryParse);
// hasDynamicSpatial stays false → single build() attempt // hasDynamicSpatial stays false → single build() attempt
} }
else if (retryParsed && tempNetwork->getNbInputs() > 0) { else if (retryParsed && tempNetwork->getNbInputs() > 0) {
@@ -2441,19 +2489,36 @@ bool Engine<T>::buildWithRetry(std::string onnxModelPath,
hasDynamicSpatial = true; hasDynamicSpatial = true;
onnxFixedH = (dims.d[2] != -1) ? dims.d[2] : 0; onnxFixedH = (dims.d[2] != -1) ? dims.d[2] : 0;
onnxFixedW = (dims.d[3] != -1) ? dims.d[3] : 0; onnxFixedW = (dims.d[3] != -1) ? dims.d[3] : 0;
ANS_DBG("TRT_Build", "buildWithRetry pre-parse OK nbDims=%d dims=[%d,%d,%d,%d] dynSpatial=%d fixedHW=%dx%d",
dims.nbDims,
(int)dims.d[0], (int)dims.d[1], (int)dims.d[2], (int)dims.d[3],
(int)hasDynamicSpatial, onnxFixedH, onnxFixedW);
} else {
ANS_DBG("TRT_Build", "buildWithRetry WARN nbDims=%d (<4) — treating as fixed", dims.nbDims);
}
} else {
ANS_DBG("TRT_Build", "buildWithRetry WARN parse failed or no inputs (parsed=%d nbInputs=%d)",
(int)retryParsed,
retryParsed ? tempNetwork->getNbInputs() : -1);
} }
} }
} }
} } else {
ANS_DBG("TRT_Build", "buildWithRetry SKIP pre-parse (maxInputHW=%dx%d not both >0)",
m_options.maxInputHeight, m_options.maxInputWidth);
} }
// -- Fixed-spatial or no dynamic dims: single build attempt ---------------- // -- Fixed-spatial or no dynamic dims: single build attempt ----------------
if (!hasDynamicSpatial) { if (!hasDynamicSpatial) {
ANS_DBG("TRT_Build", "buildWithRetry FIXED_SPATIAL → single buildSafe() attempt");
unsigned long sehBuild = 0; unsigned long sehBuild = 0;
bool ok = buildSafe(onnxModelPath, subVals, divVals, normalize, &sehBuild); bool ok = buildSafe(onnxModelPath, subVals, divVals, normalize, &sehBuild);
if (sehBuild != 0) { if (sehBuild != 0) {
ANS_DBG("TRT_Build", "buildWithRetry FAIL fixed-spatial SEH=0x%lx", sehBuild);
return false; return false;
} }
ANS_DBG("TRT_Build", "buildWithRetry %s fixed-spatial",
ok ? "SUCCESS" : "FAIL");
return ok; return ok;
} }
@@ -2482,6 +2547,16 @@ bool Engine<T>::buildWithRetry(std::string onnxModelPath,
if (candidates.back() > 640) candidates.push_back(640); if (candidates.back() > 640) candidates.push_back(640);
if (candidates.back() > 320) candidates.push_back(320); if (candidates.back() > 320) candidates.push_back(320);
{
std::ostringstream oss;
for (size_t i = 0; i < candidates.size(); ++i) {
if (i) oss << ",";
oss << candidates[i];
}
ANS_DBG("TRT_Build", "buildWithRetry DYNAMIC_SPATIAL dynH=%d dynW=%d maxDynDim=%d candidates=[%s]",
(int)dynamicH, (int)dynamicW, maxDynDim, oss.str().c_str());
}
// Helper: configure m_options for a given candidate // Helper: configure m_options for a given candidate
auto setCandidateOptions = [&](int candidate) { auto setCandidateOptions = [&](int candidate) {
float scale = static_cast<float>(candidate) / maxDynDim; float scale = static_cast<float>(candidate) / maxDynDim;
@@ -2506,16 +2581,28 @@ bool Engine<T>::buildWithRetry(std::string onnxModelPath,
for (size_t attempt = 0; attempt < candidates.size(); ++attempt) { for (size_t attempt = 0; attempt < candidates.size(); ++attempt) {
setCandidateOptions(candidates[attempt]); setCandidateOptions(candidates[attempt]);
ANS_DBG("TRT_Build", "buildWithRetry ATTEMPT %zu/%zu candidate=%d maxHW=%dx%d optHW=%dx%d minHW=%dx%d",
attempt + 1, candidates.size(), candidates[attempt],
m_options.maxInputHeight, m_options.maxInputWidth,
m_options.optInputHeight, m_options.optInputWidth,
m_options.minInputHeight, m_options.minInputWidth);
{ {
unsigned long sehAttempt = 0; unsigned long sehAttempt = 0;
bool attemptOk = buildSafe(onnxModelPath, subVals, divVals, normalize, &sehAttempt); bool attemptOk = buildSafe(onnxModelPath, subVals, divVals, normalize, &sehAttempt);
if (sehAttempt != 0) { if (sehAttempt != 0) {
ANS_DBG("TRT_Build", "buildWithRetry FAIL SEH=0x%lx on candidate=%d — abort (CUDA may be corrupted)",
sehAttempt, candidates[attempt]);
// CUDA context may be corrupted — no point retrying // CUDA context may be corrupted — no point retrying
return false; return false;
} }
if (attemptOk) { if (attemptOk) {
ANS_DBG("TRT_Build", "buildWithRetry SUCCESS at attempt %zu/%zu candidate=%d (final maxHW=%dx%d)",
attempt + 1, candidates.size(), candidates[attempt],
m_options.maxInputHeight, m_options.maxInputWidth);
return true; return true;
} }
ANS_DBG("TRT_Build", "buildWithRetry attempt %zu FAILED — trying smaller", attempt + 1);
} }
} }
@@ -2527,6 +2614,8 @@ bool Engine<T>::buildWithRetry(std::string onnxModelPath,
m_options.minInputHeight = origMinH; m_options.minInputHeight = origMinH;
m_options.minInputWidth = origMinW; m_options.minInputWidth = origMinW;
ANS_DBG("TRT_Build", "buildWithRetry FAIL all %zu candidates exhausted — restored origMaxHW=%dx%d",
candidates.size(), origMaxH, origMaxW);
return false; return false;
} }
@@ -2553,6 +2642,9 @@ bool Engine<T>::buildLoadNetwork(
int maxSlotsPerGpu, int maxSlotsPerGpu,
double memSafetyFactor) double memSafetyFactor)
{ {
ANS_DBG("TRT_Build", "buildLoadNetwork(6p) ENTRY onnx=%s normalize=%d maxSlotsPerGpu=%d memSafety=%.3f",
onnxModelPath.c_str(), (int)normalize, maxSlotsPerGpu, memSafetyFactor);
// Force single-GPU when: maxSlotsPerGpu==0 (optimizer bypass), // Force single-GPU when: maxSlotsPerGpu==0 (optimizer bypass),
// per-instance forceNoPool, global bypass (OptimizeModelStr), // per-instance forceNoPool, global bypass (OptimizeModelStr),
// exported g_forceNoPool, OR single-GPU system with maxSlotsPerGpu==1. // exported g_forceNoPool, OR single-GPU system with maxSlotsPerGpu==1.
@@ -2563,18 +2655,32 @@ bool Engine<T>::buildLoadNetwork(
{ {
extern std::atomic<bool> g_forceNoPool; extern std::atomic<bool> g_forceNoPool;
int gpuCount = 0; int gpuCount = 0;
cudaGetDeviceCount(&gpuCount); cudaError_t cudaErr = cudaGetDeviceCount(&gpuCount);
if (cudaErr != cudaSuccess) {
ANS_DBG("TRT_Build", "buildLoadNetwork(6p) WARN cudaGetDeviceCount err=%d (%s) — assuming gpuCount=0",
(int)cudaErr, cudaGetErrorString(cudaErr));
}
bool singleGpuNoElastic = (gpuCount <= 1 && maxSlotsPerGpu == 1); bool singleGpuNoElastic = (gpuCount <= 1 && maxSlotsPerGpu == 1);
bool gForceNoPool = g_forceNoPool.load(std::memory_order_relaxed);
bool globalBypass = TRTEngineCache::globalBypass().load(std::memory_order_relaxed);
bool noPool = (maxSlotsPerGpu == 0) || m_forceNoPool || bool noPool = (maxSlotsPerGpu == 0) || m_forceNoPool ||
g_forceNoPool.load(std::memory_order_relaxed) || gForceNoPool || globalBypass || singleGpuNoElastic;
TRTEngineCache::globalBypass().load(std::memory_order_relaxed) ||
singleGpuNoElastic; ANS_DBG("TRT_Build", "buildLoadNetwork(6p) DECISION gpuCount=%d maxSlots==0:%d m_forceNoPool=%d g_forceNoPool=%d globalBypass=%d singleGpuNoElastic=%d → noPool=%d",
gpuCount, (int)(maxSlotsPerGpu == 0), (int)m_forceNoPool,
(int)gForceNoPool, (int)globalBypass, (int)singleGpuNoElastic,
(int)noPool);
if (noPool) { if (noPool) {
std::cout << "Info: buildLoadNetwork -- single-GPU forced (maxSlots=" << maxSlotsPerGpu std::cout << "Info: buildLoadNetwork -- single-GPU forced (maxSlots=" << maxSlotsPerGpu
<< ", forceNoPool=" << m_forceNoPool << ", forceNoPool=" << m_forceNoPool
<< ", g_forceNoPool=" << g_forceNoPool.load() << ", g_forceNoPool=" << g_forceNoPool.load()
<< ", gpuCount=" << gpuCount << ")" << std::endl; << ", gpuCount=" << gpuCount << ")" << std::endl;
return buildLoadNetwork(onnxModelPath, subVals, divVals, normalize); ANS_DBG("TRT_Build", "buildLoadNetwork(6p) → single-GPU 4-param overload");
bool ok = buildLoadNetwork(onnxModelPath, subVals, divVals, normalize);
ANS_DBG("TRT_Build", "buildLoadNetwork(6p) %s (single-GPU path)",
ok ? "SUCCESS" : "FAIL");
return ok;
} }
} }
@@ -2583,11 +2689,16 @@ bool Engine<T>::buildLoadNetwork(
std::cout << "Info: buildLoadNetwork -- activating multi-GPU pool" std::cout << "Info: buildLoadNetwork -- activating multi-GPU pool"
<< " (maxSlotsPerGpu=" << maxSlotsPerGpu << " (maxSlotsPerGpu=" << maxSlotsPerGpu
<< ", memSafetyFactor=" << memSafetyFactor << ")" << std::endl; << ", memSafetyFactor=" << memSafetyFactor << ")" << std::endl;
ANS_DBG("TRT_Build", "buildLoadNetwork(6p) → loadSlots multi-GPU pool maxSlotsPerGpu=%d memSafety=%.3f",
maxSlotsPerGpu, memSafetyFactor);
return loadSlots(m_options, onnxModelPath, bool ok = loadSlots(m_options, onnxModelPath,
subVals, divVals, normalize, subVals, divVals, normalize,
/*fromOnnx=*/true, /*fromOnnx=*/true,
maxSlotsPerGpu, memSafetyFactor); maxSlotsPerGpu, memSafetyFactor);
ANS_DBG("TRT_Build", "buildLoadNetwork(6p) %s (multi-GPU pool path)",
ok ? "SUCCESS" : "FAIL");
return ok;
} }
template <typename T> template <typename T>
@@ -606,6 +606,25 @@ bool Engine<T>::runInferenceFromPool(
bool kickedGrowth = false; bool kickedGrowth = false;
auto _poolAcquireStart = std::chrono::steady_clock::now(); 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); std::unique_lock<std::mutex> lock(m_slotMutex);
@@ -616,6 +635,23 @@ bool Engine<T>::runInferenceFromPool(
const size_t n = m_slots.size(); const size_t n = m_slots.size();
if (n > 0) { if (n > 0) {
const size_t start = m_nextSlotHint.load() % n; const size_t start = m_nextSlotHint.load() % n;
// 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) { for (size_t i = 0; i < n; ++i) {
auto& s = m_slots[(start + i) % n]; auto& s = m_slots[(start + i) % n];
if (!s.busy && s.engine) { // alive and idle if (!s.busy && s.engine) { // alive and idle
@@ -626,6 +662,7 @@ bool Engine<T>::runInferenceFromPool(
} }
} }
} }
}
if (!slot) { if (!slot) {
ANS_DBG("TRT_Pool", "ALL SLOTS BUSY: %zu slots, active=%d — waiting for free slot", ANS_DBG("TRT_Pool", "ALL SLOTS BUSY: %zu slots, active=%d — waiting for free slot",
@@ -88,8 +88,20 @@ public:
const std::array<float, 3>& subVals, const std::array<float, 3>& subVals,
const std::array<float, 3>& divVals, const std::array<float, 3>& divVals,
bool normalize, bool normalize,
int maxSlotsPerGpu) int maxSlotsPerGpu,
const void* onnxData = nullptr,
size_t onnxSize = 0)
{ {
// A sealed .ansmodel is decrypted in memory and never written out, so
// the caller hands the bytes down here: every Engine this call builds
// parses them instead of opening modelPath, which only names the
// TensorRT plan that gets cached.
auto newEngine = [&]() {
auto e = std::make_shared<Engine<T>>(options);
if (onnxData != nullptr && onnxSize > 0) e->setOnnxBytes(onnxData, onnxSize);
return e;
};
// Optimizer / temporary engines: maxSlotsPerGpu==0 means the caller // Optimizer / temporary engines: maxSlotsPerGpu==0 means the caller
// only needs a lightweight, non-shared engine (e.g., OptimizeModelStr). // only needs a lightweight, non-shared engine (e.g., OptimizeModelStr).
// Bypass the pool cache entirely: // Bypass the pool cache entirely:
@@ -99,7 +111,7 @@ public:
// Note: maxSlotsPerGpu==1 is now the normal "1 slot per GPU" multi-GPU // Note: maxSlotsPerGpu==1 is now the normal "1 slot per GPU" multi-GPU
// round-robin mode, so it goes through the pool path below. // round-robin mode, so it goes through the pool path below.
if (maxSlotsPerGpu == 0) { if (maxSlotsPerGpu == 0) {
auto engine = std::make_shared<Engine<T>>(options); auto engine = newEngine();
bool ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize); bool ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize);
return ok ? engine : nullptr; return ok ? engine : nullptr;
} }
@@ -141,13 +153,13 @@ public:
} }
// Cache miss — create new Engine pool // Cache miss — create new Engine pool
auto engine = std::make_shared<Engine<T>>(options); auto engine = newEngine();
bool ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize, maxSlotsPerGpu); bool ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize, maxSlotsPerGpu);
if (!ok) { if (!ok) {
// Step 1: Force-evict all pools with refcount=0 to reclaim VRAM // Step 1: Force-evict all pools with refcount=0 to reclaim VRAM
int evicted = forceEvictPending(); int evicted = forceEvictPending();
if (evicted > 0) { if (evicted > 0) {
engine = std::make_shared<Engine<T>>(options); engine = newEngine();
ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize, maxSlotsPerGpu); ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize, maxSlotsPerGpu);
} }
@@ -156,7 +168,7 @@ public:
// consumes ~300-500 MB vs ~50-100 MB for a simple loadNetwork. // consumes ~300-500 MB vs ~50-100 MB for a simple loadNetwork.
// Lightweight mode: tasks queue for a single shared slot — slower but works. // Lightweight mode: tasks queue for a single shared slot — slower but works.
if (!ok) { if (!ok) {
engine = std::make_shared<Engine<T>>(options); engine = newEngine();
ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize); ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize);
} }
@@ -183,7 +195,7 @@ public:
} }
// Final retry — try lightweight again after delay // Final retry — try lightweight again after delay
engine = std::make_shared<Engine<T>>(options); engine = newEngine();
ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize); ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize);
} }
@@ -130,13 +130,31 @@ static inline cudaError_t cudaStreamSynchronize_Safe(cudaStream_t stream) {
} }
template <typename T> template <typename T>
void Engine<T>::warmUp(int iterations) { bool Engine<T>::warmUp(int iterations) {
if (m_verbose) { if (m_verbose) {
std::cout << "\n========================================" << std::endl; std::cout << "\n========================================" << std::endl;
std::cout << "Engine Warmup" << std::endl; std::cout << "Engine Warmup" << std::endl;
std::cout << "========================================" << 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 // Determine batch sizes to warm up
std::vector<int> batchSizes; 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(); auto firstTime = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
if (!firstSuccess) { 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; continue;
} }
@@ -231,7 +251,9 @@ void Engine<T>::warmUp(int iterations) {
for (int i = 1; i < iters; ++i) { for (int i = 1; i < iters; ++i) {
bool success = runInference(dummyInputs, dummyOutputs); bool success = runInference(dummyInputs, dummyOutputs);
if (!success) { 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; break;
} }
} }
@@ -262,6 +284,7 @@ void Engine<T>::warmUp(int iterations) {
std::cout << "Kernels pre-compiled for all batch sizes." << std::endl; std::cout << "Kernels pre-compiled for all batch sizes." << std::endl;
std::cout << "========================================\n" << std::endl; std::cout << "========================================\n" << std::endl;
} }
return allBatchesOk;
} }
template <typename T> template <typename T>
bool Engine<T>::runInference(const std::vector<std::vector<cv::cuda::GpuMat>>& inputs,std::vector<std::vector<std::vector<T>>>& featureVectors) { 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). // Pass 2: Preprocess + D2D copies — all on m_inferenceStream (no null stream).
// //
// All OpenCV CUDA ops (convertTo, subtract, divide, split) in blobFromGpuMats // 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); preprocessedBuffers.reserve(numInputs);
for (size_t i = 0; i < numInputs; ++i) { 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). // Preprocess on m_inferenceStream (not the null stream).
preprocessedBuffers.push_back( preprocessedBuffers.push_back(
@@ -811,8 +946,23 @@ bool Engine<T>::runInference(const std::vector<std::vector<cv::cuda::GpuMat>>& i
m_inferenceStream); m_inferenceStream);
if (copyErr != cudaSuccess) { 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 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; return false;
} }
} }
@@ -4,6 +4,48 @@
#include <NvInferVersion.h> // also defines TRT version macros #include <NvInferVersion.h> // also defines TRT version macros
#include <cudnn_version.h> // CUDNN_MAJOR/MINOR/PATCHLEVEL #include <cudnn_version.h> // CUDNN_MAJOR/MINOR/PATCHLEVEL
#include <cuda_runtime.h> // cudaRuntimeGetVersion #include <cuda_runtime.h> // cudaRuntimeGetVersion
#include <cstring> // memset for wiping decrypted model bytes
#include <fstream>
// ============================================================================
// ONNX bytes in memory
//
// A sealed .ansmodel is decrypted into RAM and stays there: handing the
// parser a buffer is what keeps the plaintext model off the disk. The build
// path still takes a path, because the path names the TensorRT plan that is
// cached beside it — a plan is a built artefact and carries no model.
// ============================================================================
template <typename T>
void Engine<T>::setOnnxBytes(const void* data, size_t size) {
clearOnnxBytes();
if (data == nullptr || size == 0) return;
const char* p = static_cast<const char*>(data);
m_onnxBytes.assign(p, p + size);
}
template <typename T>
void Engine<T>::clearOnnxBytes() {
if (m_onnxBytes.empty()) return;
std::memset(m_onnxBytes.data(), 0, m_onnxBytes.size());
m_onnxBytes.clear();
m_onnxBytes.shrink_to_fit();
}
template <typename T>
bool Engine<T>::readOnnxBytes(const std::string& onnxModelPath,
std::vector<char>& buffer) const {
if (!m_onnxBytes.empty()) {
buffer = m_onnxBytes;
return true;
}
std::ifstream file(onnxModelPath, std::ios::binary | std::ios::ate);
if (!file.good()) return false;
const std::streamsize size = file.tellg();
if (size <= 0) return false;
file.seekg(0, std::ios::beg);
buffer.resize(static_cast<size_t>(size));
return static_cast<bool>(file.read(buffer.data(), size));
}
template <typename T> template <typename T>
void Engine<T>::transformOutput(std::vector<std::vector<std::vector<T>>> &input, std::vector<std::vector<T>> &output) { void Engine<T>::transformOutput(std::vector<std::vector<std::vector<T>>> &input, std::vector<std::vector<T>> &output) {
@@ -207,6 +249,21 @@ std::string Engine<T>::serializeEngineOptions(const ANSCENTER::Options& options,
+ std::to_string(CUDNN_MAJOR) + "." + std::to_string(CUDNN_MAJOR) + "."
+ std::to_string(CUDNN_MINOR); + 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; return engineName;
} }
@@ -231,6 +288,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_blob;
thread_local cv::cuda::GpuMat tl_floatImg; thread_local cv::cuda::GpuMat tl_floatImg;
thread_local int tl_blobMaxElems = 0; 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) { if (totalElems > tl_blobMaxElems) {
tl_blob = cv::cuda::GpuMat(1, totalElems, CV_32FC1); 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
+90 -53
View File
@@ -1,6 +1,7 @@
#include "ANSFLV.h" #include "ANSFLV.h"
#include "ANSMatRegistry.h" #include "ANSMatRegistry.h"
#include "ANSGpuFrameOps.h" #include "ANSGpuFrameOps.h"
#include "av_frame_ptr.h" // ansmedia::AvFramePtr — RAII for AVFrame*
#include "ANSCVVendorGate.h" // anscv_vendor_gate::IsNvidiaGpuAvailable() #include "ANSCVVendorGate.h" // anscv_vendor_gate::IsNvidiaGpuAvailable()
#include "ANSLicense.h" // ANS_DBG macro #include "ANSLicense.h" // ANS_DBG macro
#include <memory> #include <memory>
@@ -223,13 +224,11 @@ namespace ANSCENTER {
} }
} }
// Honest image-equality check. Same buffer pointer (fast path) or pixel
// compare (fallback). Camera-aliveness uses getLastFrameAgeMs() — separate
// concern. With "same image -> same pts" invariant, callers usually just
// compare pts and skip this function entirely.
bool ANSFLVClient::areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2) { bool ANSFLVClient::areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2) {
// Use decoder frame age — returns "stale" only if no decoder output for 5+ seconds.
double ageMs = _playerClient->getLastFrameAgeMs();
if (ageMs > 5000.0) return true; // Truly stale
if (ageMs > 0.0) return false; // Decoder alive
// Fallback for startup (no frame decoded yet)
if (img1.empty() && img2.empty()) return true; if (img1.empty() && img2.empty()) return true;
if (img1.empty() || img2.empty()) return false; if (img1.empty() || img2.empty()) return false;
if (img1.size() != img2.size() || img1.type() != img2.type()) return false; if (img1.size() != img2.size() || img1.type() != img2.type()) return false;
@@ -287,17 +286,19 @@ namespace ANSCENTER {
return _pLastFrame; return _pLastFrame;
} }
// Use PTS to detect duplicate frames (much faster than pixel comparison) // Duplicate detection via _lastInnerPts (separate from _pts). Invariant:
if (currentPts == _pts && !_pLastFrame.empty()) { // same image -> same pts. Camera-alive check uses getLastFrameAgeMs().
if (currentPts == _lastInnerPts && !_pLastFrame.empty()) {
width = _imageWidth; width = _imageWidth;
height = _imageHeight; height = _imageHeight;
pts = _pts; pts = _pts; // unchanged
return _pLastFrame; return _pLastFrame;
} }
// Update PTS first // Inner advanced — real new frame. Sync cursor and bump _pts monotonically.
_pts = currentPts; _lastInnerPts = currentPts;
pts = currentPts; _pts++;
pts = _pts;
// Handle non-rotated case // Handle non-rotated case
if (_imageRotateDeg == 0) { if (_imageRotateDeg == 0) {
@@ -635,6 +636,14 @@ namespace ANSCENTER {
std::lock_guard<std::recursive_mutex> lock(_mutex); std::lock_guard<std::recursive_mutex> lock(_mutex);
_playerClient->setTargetFPS(intervalMs); // 0=no limit, 100=~10FPS, 200=~5FPS _playerClient->setTargetFPS(intervalMs); // 0=no limit, 100=~10FPS, 200=~5FPS
} }
void ANSFLVClient::SetJitterBufferMs(int ms) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_playerClient->setJitterBufferMs(ms);
ANS_DBG("FLV_Lifecycle", "SetJitterBufferMs: ms=%d url=%s", ms, _url.c_str());
}
int ANSFLVClient::GetJitterBufferMs() const {
return _playerClient->getJitterBufferMs();
}
void ANSFLVClient::SetNV12FastPath(bool enable) { void ANSFLVClient::SetNV12FastPath(bool enable) {
std::lock_guard<std::recursive_mutex> lock(_mutex); std::lock_guard<std::recursive_mutex> lock(_mutex);
_useNV12FastPath = enable; _useNV12FastPath = enable;
@@ -729,11 +738,11 @@ extern "C" __declspec(dllexport) int GetFLVStrImage(ANSCENTER::ANSFLVClient * *H
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error getting FLV image: " << e.what() << std::endl; ANS_DBG("FLV_GetImage", "exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error getting FLV image: Unknown exception." << std::endl; ANS_DBG("FLV_GetImage", "unknown exception");
return 0; return 0;
} }
} }
@@ -751,27 +760,27 @@ extern "C" __declspec(dllexport) int GetFLVImage(ANSCENTER::ANSFLVClient * *Hand
return 1; return 1;
} }
else { else {
std::cerr << "Error resizing jpegImage handle: " << error << std::endl; ANS_DBG("FLV_GetImage", "DSSetHandleSize failed: error=%d", (int)error);
return 0; return 0;
} }
} }
else { else {
std::cerr << "No image data retrieved from FLV client." << std::endl; ANS_DBG("FLV_GetImage", "no image data retrieved (jpegString empty)");
return 0; return 0;
} }
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error getting FLV image: " << e.what() << std::endl; ANS_DBG("FLV_GetImage", "exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error getting FLV image: Unknown exception." << std::endl; ANS_DBG("FLV_GetImage", "unknown exception");
return 0; return 0;
} }
} }
extern "C" __declspec(dllexport) int GetFLVCVImage(ANSCENTER::ANSFLVClient** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image) { extern "C" __declspec(dllexport) int GetFLVCVImage(ANSCENTER::ANSFLVClient** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image) {
if (!Handle || !*Handle || !image) { if (!Handle || !*Handle || !image) {
std::cerr << "Error: Invalid input parameters in GetFLVCVImage" << std::endl; ANS_DBG("FLV_GetImage", "invalid input parameters");
return -1; return -1;
} }
@@ -785,17 +794,25 @@ extern "C" __declspec(dllexport) int GetFLVCVImage(ANSCENTER::ANSFLVClient** Han
// Thread-safe Mat pointer swap (anscv_mat_replace has its own internal lock) // Thread-safe Mat pointer swap (anscv_mat_replace has its own internal lock)
anscv_mat_replace(image, std::move(img)); anscv_mat_replace(image, std::move(img));
// NV12 GPU fast path (optional — disabled by default for stability) // NV12 GPU fast path (optional — disabled by default for stability).
// AvFramePtr auto-frees clones if we don't reach the .release() handoff.
if ((*Handle)->IsNV12FastPath()) { if ((*Handle)->IsNV12FastPath()) {
int gpuIdx = (*Handle)->GetHWDecodingGpuIndex(); int gpuIdx = (*Handle)->GetHWDecodingGpuIndex();
AVFrame* cudaHW = (*Handle)->GetCudaHWFrame(); ansmedia::AvFramePtr cudaHW{ (*Handle)->GetCudaHWFrame() };
if (cudaHW) { if (cudaHW) {
AVFrame* cpuNV12 = (*Handle)->GetNV12Frame(); ansmedia::AvFramePtr cpuNV12{ (*Handle)->GetNV12Frame() };
gpu_frame_attach_cuda(*image, cudaHW, gpuIdx, timeStamp, cpuNV12); ANS_DBG("MEDIA_AvPtr",
"ANSFLV V1 attach_cuda cudaHW=%p cpuNV12=%p gpuIdx=%d",
(void*)cudaHW.get(), (void*)cpuNV12.get(), gpuIdx);
gpu_frame_attach_cuda(*image, cudaHW.release(), gpuIdx, timeStamp,
cpuNV12.release());
} else { } else {
AVFrame* nv12 = (*Handle)->GetNV12Frame(); ansmedia::AvFramePtr nv12{ (*Handle)->GetNV12Frame() };
if (nv12) { if (nv12) {
gpu_frame_attach(*image, nv12, gpuIdx, timeStamp); ANS_DBG("MEDIA_AvPtr",
"ANSFLV V1 attach nv12=%p gpuIdx=%d",
(void*)nv12.get(), gpuIdx);
gpu_frame_attach(*image, nv12.release(), gpuIdx, timeStamp);
} }
} }
} }
@@ -803,15 +820,15 @@ extern "C" __declspec(dllexport) int GetFLVCVImage(ANSCENTER::ANSFLVClient** Han
return 1; return 1;
} }
catch (const cv::Exception& e) { catch (const cv::Exception& e) {
std::cerr << "OpenCV exception in GetFLVCVImage: " << e.what() << std::endl; ANS_DBG("FLV_GetImage", "OpenCV exception: %s", e.what());
return -2; return -2;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Exception in GetFLVCVImage: " << e.what() << std::endl; ANS_DBG("FLV_GetImage", "GetFLVCVImage exception: %s", e.what());
return -2; return -2;
} }
catch (...) { catch (...) {
std::cerr << "Unknown exception in GetFLVCVImage" << std::endl; ANS_DBG("FLV_GetImage", "GetFLVCVImage unknown exception");
return -2; return -2;
} }
} }
@@ -824,11 +841,11 @@ extern "C" __declspec(dllexport) int StartFLV(ANSCENTER::ANSFLVClient * *Handle)
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error starting FLV client: " << e.what() << std::endl; ANS_DBG("FLV_Lifecycle", "StartFLV exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error starting FLV client: Unknown exception." << std::endl; ANS_DBG("FLV_Lifecycle", "StartFLV unknown exception");
return 0; return 0;
} }
} }
@@ -840,11 +857,11 @@ extern "C" __declspec(dllexport) int ReconnectFLV(ANSCENTER::ANSFLVClient * *Han
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error reconnecting FLV client: " << e.what() << std::endl; ANS_DBG("FLV_Lifecycle", "ReconnectFLV exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error reconnecting FLV client: Unknown exception." << std::endl; ANS_DBG("FLV_Lifecycle", "ReconnectFLV unknown exception");
return 0; return 0;
} }
} }
@@ -856,11 +873,11 @@ extern "C" __declspec(dllexport) int StopFLV(ANSCENTER::ANSFLVClient * *Handle)
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error stopping FLV client: " << e.what() << std::endl; ANS_DBG("FLV_Lifecycle", "StopFLV exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error stopping FLV client: Unknown exception." << std::endl; ANS_DBG("FLV_Lifecycle", "StopFLV unknown exception");
return 0; return 0;
} }
} }
@@ -971,6 +988,18 @@ extern "C" __declspec(dllexport) void SetFLVDisplayResolution(ANSCENTER::ANSFLVC
(*Handle)->SetDisplayResolution(width, height); (*Handle)->SetDisplayResolution(width, height);
} catch (...) { } } catch (...) { }
} }
extern "C" __declspec(dllexport) void SetFLVJitterBuffer(ANSCENTER::ANSFLVClient** Handle, int jitterMs) {
if (Handle == nullptr || *Handle == nullptr) return;
try {
(*Handle)->SetJitterBufferMs(jitterMs);
} catch (...) { }
}
extern "C" __declspec(dllexport) int GetFLVJitterBuffer(ANSCENTER::ANSFLVClient** Handle) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
return (*Handle)->GetJitterBufferMs();
} catch (...) { return -1; }
}
extern "C" __declspec(dllexport) void SetFLVTargetFPS(ANSCENTER::ANSFLVClient** Handle, double intervalMs) { extern "C" __declspec(dllexport) void SetFLVTargetFPS(ANSCENTER::ANSFLVClient** Handle, double intervalMs) {
if (Handle == nullptr || *Handle == nullptr) return; if (Handle == nullptr || *Handle == nullptr) return;
try { try {
@@ -1003,21 +1032,21 @@ extern "C" __declspec(dllexport) int GetFLVImage_V2(uint64_t handleVal, int& wid
return 1; return 1;
} }
else { else {
std::cerr << "Error resizing jpegImage handle: " << error << std::endl; ANS_DBG("FLV_GetImage", "DSSetHandleSize failed: error=%d", (int)error);
return 0; return 0;
} }
} }
else { else {
std::cerr << "No image data retrieved from FLV client." << std::endl; ANS_DBG("FLV_GetImage", "no image data retrieved (jpegString empty)");
return 0; return 0;
} }
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error getting FLV image: " << e.what() << std::endl; ANS_DBG("FLV_GetImage", "exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error getting FLV image: Unknown exception." << std::endl; ANS_DBG("FLV_GetImage", "unknown exception");
return 0; return 0;
} }
} }
@@ -1032,31 +1061,39 @@ extern "C" __declspec(dllexport) int GetFLVCVImage_V2(uint64_t handleVal, int& w
} }
anscv_mat_replace(image, std::move(img)); anscv_mat_replace(image, std::move(img));
// Attach NV12 frame for GPU fast-path inference (side-table registry) // Attach NV12 frame for GPU fast-path inference (side-table registry).
// AvFramePtr auto-frees clones if we don't reach the .release() handoff.
int gpuIdx = h->GetHWDecodingGpuIndex(); int gpuIdx = h->GetHWDecodingGpuIndex();
AVFrame* cudaHW = h->GetCudaHWFrame(); ansmedia::AvFramePtr cudaHW{ h->GetCudaHWFrame() };
if (cudaHW) { if (cudaHW) {
AVFrame* cpuNV12 = h->GetNV12Frame(); ansmedia::AvFramePtr cpuNV12{ h->GetNV12Frame() };
gpu_frame_attach_cuda(*image, cudaHW, gpuIdx, timeStamp, cpuNV12); ANS_DBG("MEDIA_AvPtr",
"ANSFLV V2 attach_cuda cudaHW=%p cpuNV12=%p gpuIdx=%d",
(void*)cudaHW.get(), (void*)cpuNV12.get(), gpuIdx);
gpu_frame_attach_cuda(*image, cudaHW.release(), gpuIdx, timeStamp,
cpuNV12.release());
} else { } else {
AVFrame* nv12 = h->GetNV12Frame(); ansmedia::AvFramePtr nv12{ h->GetNV12Frame() };
if (nv12) { if (nv12) {
gpu_frame_attach(*image, nv12, gpuIdx, timeStamp); ANS_DBG("MEDIA_AvPtr",
"ANSFLV V2 attach nv12=%p gpuIdx=%d",
(void*)nv12.get(), gpuIdx);
gpu_frame_attach(*image, nv12.release(), gpuIdx, timeStamp);
} }
} }
return 1; return 1;
} }
catch (const cv::Exception& e) { catch (const cv::Exception& e) {
std::cerr << "OpenCV exception in GetFLVCVImage_V2: " << e.what() << std::endl; ANS_DBG("FLV_GetImage", "GetFLVCVImage_V2 OpenCV exception: %s", e.what());
return -2; return -2;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Exception in GetFLVCVImage_V2: " << e.what() << std::endl; ANS_DBG("FLV_GetImage", "GetFLVCVImage_V2 exception: %s", e.what());
return -2; return -2;
} }
catch (...) { catch (...) {
std::cerr << "Unknown exception in GetFLVCVImage_V2" << std::endl; ANS_DBG("FLV_GetImage", "GetFLVCVImage_V2 unknown exception");
return -2; return -2;
} }
} }
@@ -1069,11 +1106,11 @@ extern "C" __declspec(dllexport) int StartFLV_V2(uint64_t handleVal) {
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error starting FLV client: " << e.what() << std::endl; ANS_DBG("FLV_Lifecycle", "StartFLV exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error starting FLV client: Unknown exception." << std::endl; ANS_DBG("FLV_Lifecycle", "StartFLV unknown exception");
return 0; return 0;
} }
} }
@@ -1086,11 +1123,11 @@ extern "C" __declspec(dllexport) int ReconnectFLV_V2(uint64_t handleVal) {
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error reconnecting FLV client: " << e.what() << std::endl; ANS_DBG("FLV_Lifecycle", "ReconnectFLV exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error reconnecting FLV client: Unknown exception." << std::endl; ANS_DBG("FLV_Lifecycle", "ReconnectFLV unknown exception");
return 0; return 0;
} }
} }
@@ -1103,11 +1140,11 @@ extern "C" __declspec(dllexport) int StopFLV_V2(uint64_t handleVal) {
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error stopping FLV client: " << e.what() << std::endl; ANS_DBG("FLV_Lifecycle", "StopFLV exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error stopping FLV client: Unknown exception." << std::endl; ANS_DBG("FLV_Lifecycle", "StopFLV unknown exception");
return 0; return 0;
} }
} }
+9
View File
@@ -35,6 +35,9 @@ namespace ANSCENTER
std::string _lastJpegImage; std::string _lastJpegImage;
int _imageWidth, _imageHeight; int _imageWidth, _imageHeight;
int64_t _pts; int64_t _pts;
// Last currentPts seen from inner getImage(). Used for duplicate
// detection separately from _pts so "same image -> same pts" holds.
int64_t _lastInnerPts = -1;
bool _isPlaying; bool _isPlaying;
bool _useNV12FastPath = false; // false = original stable CPU path, true = NV12 GPU fast path bool _useNV12FastPath = false; // false = original stable CPU path, true = NV12 GPU fast path
std::recursive_mutex _mutex; std::recursive_mutex _mutex;
@@ -73,6 +76,10 @@ namespace ANSCENTER
void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize) void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize)
void SetImageQuality(int mode); // 0=fast (AI), 1=quality (display) void SetImageQuality(int mode); // 0=fast (AI), 1=quality (display)
void SetTargetFPS(double intervalMs); // Set min interval between processed frames in ms (0 = no limit, 100 = ~10 FPS, 200 = ~5 FPS) void SetTargetFPS(double intervalMs); // Set min interval between processed frames in ms (0 = no limit, 100 = ~10 FPS, 200 = ~5 FPS)
// Jitter buffer for the BGR delivery path (forwarded to CVideoPlayer).
// 0 = no buffering (today's getLatest semantics). Default 200 ms.
void SetJitterBufferMs(int ms);
int GetJitterBufferMs() const;
void SetNV12FastPath(bool enable); // true = NV12 GPU fast path, false = original CPU path (stable) void SetNV12FastPath(bool enable); // true = NV12 GPU fast path, false = original CPU path (stable)
bool IsNV12FastPath() const { return _useNV12FastPath; } bool IsNV12FastPath() const { return _useNV12FastPath; }
AVFrame* GetNV12Frame(); // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free) AVFrame* GetNV12Frame(); // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free)
@@ -114,4 +121,6 @@ extern "C" __declspec(dllexport) void SetFLVImageQuality(ANSCENTER::ANSFLVClient
extern "C" __declspec(dllexport) void SetFLVDisplayResolution(ANSCENTER::ANSFLVClient** Handle, int width, int height); extern "C" __declspec(dllexport) void SetFLVDisplayResolution(ANSCENTER::ANSFLVClient** Handle, int width, int height);
extern "C" __declspec(dllexport) void SetFLVTargetFPS(ANSCENTER::ANSFLVClient** Handle, double intervalMs); extern "C" __declspec(dllexport) void SetFLVTargetFPS(ANSCENTER::ANSFLVClient** Handle, double intervalMs);
extern "C" __declspec(dllexport) void SetFLVNV12FastPath(ANSCENTER::ANSFLVClient** Handle, int enable); extern "C" __declspec(dllexport) void SetFLVNV12FastPath(ANSCENTER::ANSFLVClient** Handle, int enable);
extern "C" __declspec(dllexport) void SetFLVJitterBuffer(ANSCENTER::ANSFLVClient** Handle, int jitterMs);
extern "C" __declspec(dllexport) int GetFLVJitterBuffer(ANSCENTER::ANSFLVClient** Handle);
#endif #endif
+81 -43
View File
@@ -1,6 +1,7 @@
#include "ANSFilePlayer.h" #include "ANSFilePlayer.h"
#include "ANSMatRegistry.h" #include "ANSMatRegistry.h"
#include "ANSGpuFrameOps.h" #include "ANSGpuFrameOps.h"
#include "av_frame_ptr.h" // ansmedia::AvFramePtr — RAII for AVFrame*
#include <memory> #include <memory>
#include <cstdint> #include <cstdint>
#include <libswscale/swscale.h> #include <libswscale/swscale.h>
@@ -218,6 +219,14 @@ namespace ANSCENTER {
_displayWidth = width; _displayWidth = width;
_displayHeight = height; _displayHeight = height;
} }
void ANSFILEPLAYER::SetJitterBufferMs(int ms) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
if (_playerClient) _playerClient->setJitterBufferMs(ms);
ANS_DBG("FilePlayer_Lifecycle", "SetJitterBufferMs: ms=%d url=%s", ms, _url.c_str());
}
int ANSFILEPLAYER::GetJitterBufferMs() const {
return _playerClient ? _playerClient->getJitterBufferMs() : 0;
}
cv::Mat ANSFILEPLAYER::GetInferenceImage() { cv::Mat ANSFILEPLAYER::GetInferenceImage() {
std::lock_guard<std::recursive_mutex> lock(_mutex); std::lock_guard<std::recursive_mutex> lock(_mutex);
return _inferenceImage; return _inferenceImage;
@@ -323,7 +332,7 @@ extern "C" __declspec(dllexport) int GetFilePlayerStrImage(ANSCENTER::ANSFILEPLA
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error getting image data from FilePlayer client: " << e.what() << std::endl; ANS_DBG("FilePlayer_GetImage", "exception: %s", e.what());
return -1; return -1;
} }
catch (...) { catch (...) {
@@ -350,18 +359,18 @@ extern "C" __declspec(dllexport) int GetFilePlayerImage(ANSCENTER::ANSFILEPLAYER
} }
else { else {
// Return failure if there was an error in resizing the handle // Return failure if there was an error in resizing the handle
std::cerr << "Error resizing jpegImage handle: " << error << std::endl; ANS_DBG("FilePlayer_GetImage", "DSSetHandleSize failed: error=%d", (int)error);
return 0; return 0;
} }
} }
else { else {
// If the JPEG image string is empty, return failure // If the JPEG image string is empty, return failure
std::cerr << "No image data retrieved from FilePlayer client." << std::endl; ANS_DBG("FilePlayer_GetImage", "no image data retrieved (jpegString empty)");
return 0; return 0;
} }
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error getting image data from FilePlayer client: " << e.what() << std::endl; ANS_DBG("FilePlayer_GetImage", "exception: %s", e.what());
return -1; return -1;
} }
catch (...) { catch (...) {
@@ -370,7 +379,7 @@ extern "C" __declspec(dllexport) int GetFilePlayerImage(ANSCENTER::ANSFILEPLAYER
} }
extern "C" __declspec(dllexport) int GetFilePlayerCVImage(ANSCENTER::ANSFILEPLAYER** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image) { extern "C" __declspec(dllexport) int GetFilePlayerCVImage(ANSCENTER::ANSFILEPLAYER** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image) {
if (!Handle || !(*Handle) || !image) { if (!Handle || !(*Handle) || !image) {
std::cerr << "Error: Invalid input parameters in GetFilePlayerCVImage." << std::endl; ANS_DBG("FilePlayer_GetImage", "GetFilePlayerCVImage invalid input parameters");
return -1; return -1;
} }
try { try {
@@ -382,15 +391,24 @@ extern "C" __declspec(dllexport) int GetFilePlayerCVImage(ANSCENTER::ANSFILEPLAY
// Attach NV12/CUDA frames to registry for inference zero-copy (same as ANSRTSP/ANSVideoPlayer). // Attach NV12/CUDA frames to registry for inference zero-copy (same as ANSRTSP/ANSVideoPlayer).
// CFilePlayer inherits getNV12Frame()/getCudaHWFrame() from CVideoPlayer. // CFilePlayer inherits getNV12Frame()/getCudaHWFrame() from CVideoPlayer.
// AvFramePtr wraps each clone so it auto-frees on any return path before
// the .release() handoff to gpu_frame_attach[_cuda] — leak-by-omission
// becomes impossible if an exception or early return slips in later.
int gpuIdx = (*Handle)->_playerClient->getHWDecodingGpuIndex(); int gpuIdx = (*Handle)->_playerClient->getHWDecodingGpuIndex();
AVFrame* cudaHW = (*Handle)->_playerClient->getCudaHWFrame(); ansmedia::AvFramePtr cudaHW{ (*Handle)->_playerClient->getCudaHWFrame() };
if (cudaHW) { if (cudaHW) {
AVFrame* cpuNV12 = (*Handle)->_playerClient->getNV12Frame(); ansmedia::AvFramePtr cpuNV12{ (*Handle)->_playerClient->getNV12Frame() };
gpu_frame_attach_cuda(*image, cudaHW, gpuIdx, timeStamp, cpuNV12); ANS_DBG("MEDIA_AvPtr",
"ANSFilePlayer V1 attach_cuda cudaHW=%p cpuNV12=%p gpuIdx=%d",
(void*)cudaHW.get(), (void*)cpuNV12.get(), gpuIdx);
gpu_frame_attach_cuda(*image, cudaHW.release(), gpuIdx, timeStamp, cpuNV12.release());
} else { } else {
AVFrame* nv12 = (*Handle)->_playerClient->getNV12Frame(); ansmedia::AvFramePtr nv12{ (*Handle)->_playerClient->getNV12Frame() };
if (nv12) { if (nv12) {
gpu_frame_attach(*image, nv12, gpuIdx, timeStamp); ANS_DBG("MEDIA_AvPtr",
"ANSFilePlayer V1 attach nv12=%p gpuIdx=%d",
(void*)nv12.get(), gpuIdx);
gpu_frame_attach(*image, nv12.release(), gpuIdx, timeStamp);
} else { } else {
// CPU-only fallback: attach full-res BGR clone for ANSRTYOLO // CPU-only fallback: attach full-res BGR clone for ANSRTYOLO
cv::Mat infImg = (*Handle)->GetInferenceImage(); cv::Mat infImg = (*Handle)->GetInferenceImage();
@@ -429,15 +447,15 @@ extern "C" __declspec(dllexport) int GetFilePlayerCVImage(ANSCENTER::ANSFILEPLAY
return 1; return 1;
} }
catch (const cv::Exception& e) { catch (const cv::Exception& e) {
std::cerr << "OpenCV exception in GetFilePlayerCVImage: " << e.what() << std::endl; ANS_DBG("FilePlayer_GetImage", "GetFilePlayerCVImage OpenCV exception: %s", e.what());
return -2; return -2;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Exception in GetFilePlayerCVImage: " << e.what() << std::endl; ANS_DBG("FilePlayer_GetImage", "GetFilePlayerCVImage exception: %s", e.what());
return -2; return -2;
} }
catch (...) { catch (...) {
std::cerr << "Unknown exception in GetFilePlayerCVImage" << std::endl; ANS_DBG("FilePlayer_GetImage", "GetFilePlayerCVImage unknown exception");
return -2; return -2;
} }
} }
@@ -450,7 +468,7 @@ extern "C" __declspec(dllexport) int StartFilePlayer(ANSCENTER::ANSFILEPLAYER**
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error starting file player: " << e.what() << std::endl; ANS_DBG("FilePlayer_Lifecycle", "StartFilePlayer exception: %s", e.what());
return -1; return -1;
} }
catch (...) { catch (...) {
@@ -465,7 +483,7 @@ extern "C" __declspec(dllexport) int ReconnectFilePlayer(ANSCENTER::ANSFILEPLAYE
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error reconnecting file player: " << e.what() << std::endl; ANS_DBG("FilePlayer_Lifecycle", "ReconnectFilePlayer exception: %s", e.what());
return -1; return -1;
} }
catch (...) { catch (...) {
@@ -480,7 +498,7 @@ extern "C" __declspec(dllexport) int StopFilePlayer(ANSCENTER::ANSFILEPLAYER** H
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error stopping file player: " << e.what() << std::endl; ANS_DBG("FilePlayer_Lifecycle", "StopFilePlayer exception: %s", e.what());
return -1; return -1;
} }
catch (...) { catch (...) {
@@ -495,7 +513,7 @@ extern "C" __declspec(dllexport) int IsFilePlayerPaused(ANSCENTER::ANSFILEPLAYER
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error checking if file player is paused: " << e.what() << std::endl; ANS_DBG("FilePlayer_Lifecycle", "IsFilePlayerPaused exception: %s", e.what());
return -1; return -1;
} }
catch (...) { catch (...) {
@@ -510,7 +528,7 @@ extern "C" __declspec(dllexport) int IsFilePlayerRunning(ANSCENTER::ANSFILEPLAYE
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error checking if file player is running: " << e.what() << std::endl; ANS_DBG("FilePlayer_Lifecycle", "IsFilePlayerRunning exception: %s", e.what());
return -1; return -1;
} }
catch (...) { catch (...) {
@@ -525,7 +543,7 @@ extern "C" __declspec(dllexport) int IsFilePlayerRecording(ANSCENTER::ANSFILEPLA
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error checking if file player is recording: " << e.what() << std::endl; ANS_DBG("FilePlayer_Lifecycle", "IsFilePlayerRecording exception: %s", e.what());
return -1; return -1;
} }
catch (...) { catch (...) {
@@ -549,7 +567,7 @@ extern "C" __declspec(dllexport) void EnableFilePlayerAudioVolume(ANSCENTER::ANS
(*Handle)->EnableAudio(audioStatus); (*Handle)->EnableAudio(audioStatus);
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error enabling audio for file player: " << e.what() << std::endl; ANS_DBG("FilePlayer_Lifecycle", "EnableFilePlayerAudio exception: %s", e.what());
} }
catch (...) { } catch (...) { }
} }
@@ -568,7 +586,7 @@ extern "C" __declspec(dllexport) int SetBBoxFilePlayer(ANSCENTER::ANSFILEPLAYER*
return 1; return 1;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error setting bounding box for file player: " << e.what() << std::endl; ANS_DBG("FilePlayer_Cfg", "SetBBoxFilePlayer exception: %s", e.what());
return -1; return -1;
} }
catch (...) { catch (...) {
@@ -584,7 +602,7 @@ extern "C" __declspec(dllexport) int SetCropFlagFilePlayer(ANSCENTER::ANSFILEPLA
return 1; return 1;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error setting crop flag for file player: " << e.what() << std::endl; ANS_DBG("FilePlayer_Cfg", "SetCropFlagFilePlayer exception: %s", e.what());
return -1; return -1;
} }
catch (...) { catch (...) {
@@ -611,17 +629,17 @@ extern "C" __declspec(dllexport) int GetFilePlayerImage_V2(uint64_t handleVal, i
return 1; return 1;
} }
else { else {
std::cerr << "Error resizing jpegImage handle: " << error << std::endl; ANS_DBG("FilePlayer_GetImage", "DSSetHandleSize failed: error=%d", (int)error);
return 0; return 0;
} }
} }
else { else {
std::cerr << "No image data retrieved from FilePlayer client." << std::endl; ANS_DBG("FilePlayer_GetImage", "no image data retrieved (jpegString empty)");
return 0; return 0;
} }
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error getting image data from FilePlayer client: " << e.what() << std::endl; ANS_DBG("FilePlayer_GetImage", "exception: %s", e.what());
return -1; return -1;
} }
catch (...) { catch (...) {
@@ -639,16 +657,24 @@ extern "C" __declspec(dllexport) int GetFilePlayerCVImage_V2(uint64_t handleVal,
} }
anscv_mat_replace(image, std::move(img)); anscv_mat_replace(image, std::move(img));
// NV12/CUDA registry attach (same as V1) // NV12/CUDA registry attach (same as V1).
// AvFramePtr ensures any clone we obtain is freed automatically if we
// don't reach the .release() handoff (e.g. exception, future early return).
int gpuIdx = h->_playerClient->getHWDecodingGpuIndex(); int gpuIdx = h->_playerClient->getHWDecodingGpuIndex();
AVFrame* cudaHW = h->_playerClient->getCudaHWFrame(); ansmedia::AvFramePtr cudaHW{ h->_playerClient->getCudaHWFrame() };
if (cudaHW) { if (cudaHW) {
AVFrame* cpuNV12 = h->_playerClient->getNV12Frame(); ansmedia::AvFramePtr cpuNV12{ h->_playerClient->getNV12Frame() };
gpu_frame_attach_cuda(*image, cudaHW, gpuIdx, timeStamp, cpuNV12); ANS_DBG("MEDIA_AvPtr",
"ANSFilePlayer V2 attach_cuda cudaHW=%p cpuNV12=%p gpuIdx=%d",
(void*)cudaHW.get(), (void*)cpuNV12.get(), gpuIdx);
gpu_frame_attach_cuda(*image, cudaHW.release(), gpuIdx, timeStamp, cpuNV12.release());
} else { } else {
AVFrame* nv12 = h->_playerClient->getNV12Frame(); ansmedia::AvFramePtr nv12{ h->_playerClient->getNV12Frame() };
if (nv12) { if (nv12) {
gpu_frame_attach(*image, nv12, gpuIdx, timeStamp); ANS_DBG("MEDIA_AvPtr",
"ANSFilePlayer V2 attach nv12=%p gpuIdx=%d",
(void*)nv12.get(), gpuIdx);
gpu_frame_attach(*image, nv12.release(), gpuIdx, timeStamp);
} else { } else {
cv::Mat infImg = h->GetInferenceImage(); cv::Mat infImg = h->GetInferenceImage();
if (!infImg.empty() && infImg.data != (*image)->data) { if (!infImg.empty() && infImg.data != (*image)->data) {
@@ -686,15 +712,15 @@ extern "C" __declspec(dllexport) int GetFilePlayerCVImage_V2(uint64_t handleVal,
return 1; return 1;
} }
catch (const cv::Exception& e) { catch (const cv::Exception& e) {
std::cerr << "OpenCV exception in GetFilePlayerCVImage_V2: " << e.what() << std::endl; ANS_DBG("FilePlayer_GetImage", "GetFilePlayerCVImage_V2 OpenCV exception: %s", e.what());
return -2; return -2;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Exception in GetFilePlayerCVImage_V2: " << e.what() << std::endl; ANS_DBG("FilePlayer_GetImage", "GetFilePlayerCVImage_V2 exception: %s", e.what());
return -2; return -2;
} }
catch (...) { catch (...) {
std::cerr << "Unknown exception in GetFilePlayerCVImage_V2" << std::endl; ANS_DBG("FilePlayer_GetImage", "GetFilePlayerCVImage_V2 unknown exception");
return -2; return -2;
} }
} }
@@ -708,7 +734,7 @@ extern "C" __declspec(dllexport) int StartFilePlayer_V2(uint64_t handleVal) {
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error starting file player: " << e.what() << std::endl; ANS_DBG("FilePlayer_Lifecycle", "StartFilePlayer exception: %s", e.what());
return -1; return -1;
} }
catch (...) { catch (...) {
@@ -725,7 +751,7 @@ extern "C" __declspec(dllexport) int ReconnectFilePlayer_V2(uint64_t handleVal)
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error reconnecting file player: " << e.what() << std::endl; ANS_DBG("FilePlayer_Lifecycle", "ReconnectFilePlayer exception: %s", e.what());
return -1; return -1;
} }
catch (...) { catch (...) {
@@ -742,7 +768,7 @@ extern "C" __declspec(dllexport) int StopFilePlayer_V2(uint64_t handleVal) {
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error stopping file player: " << e.what() << std::endl; ANS_DBG("FilePlayer_Lifecycle", "StopFilePlayer exception: %s", e.what());
return -1; return -1;
} }
catch (...) { catch (...) {
@@ -759,7 +785,7 @@ extern "C" __declspec(dllexport) int IsFilePlayerPaused_V2(uint64_t handleVal) {
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error checking if file player is paused: " << e.what() << std::endl; ANS_DBG("FilePlayer_Lifecycle", "IsFilePlayerPaused exception: %s", e.what());
return -1; return -1;
} }
catch (...) { catch (...) {
@@ -776,7 +802,7 @@ extern "C" __declspec(dllexport) int IsFilePlayerRunning_V2(uint64_t handleVal)
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error checking if file player is running: " << e.what() << std::endl; ANS_DBG("FilePlayer_Lifecycle", "IsFilePlayerRunning exception: %s", e.what());
return -1; return -1;
} }
catch (...) { catch (...) {
@@ -793,7 +819,7 @@ extern "C" __declspec(dllexport) int IsFilePlayerRecording_V2(uint64_t handleVal
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error checking if file player is recording: " << e.what() << std::endl; ANS_DBG("FilePlayer_Lifecycle", "IsFilePlayerRecording exception: %s", e.what());
return -1; return -1;
} }
catch (...) { catch (...) {
@@ -819,7 +845,7 @@ extern "C" __declspec(dllexport) void EnableFilePlayerAudioVolume_V2(uint64_t ha
h->EnableAudio(audioStatus); h->EnableAudio(audioStatus);
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error enabling audio for file player: " << e.what() << std::endl; ANS_DBG("FilePlayer_Lifecycle", "EnableFilePlayerAudio exception: %s", e.what());
} }
catch (...) { } catch (...) { }
} }
@@ -842,7 +868,7 @@ extern "C" __declspec(dllexport) int SetBBoxFilePlayer_V2(uint64_t handleVal, in
return 1; return 1;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error setting bounding box for file player: " << e.what() << std::endl; ANS_DBG("FilePlayer_Cfg", "SetBBoxFilePlayer exception: %s", e.what());
return -1; return -1;
} }
catch (...) { catch (...) {
@@ -860,7 +886,7 @@ extern "C" __declspec(dllexport) int SetCropFlagFilePlayer_V2(uint64_t handleVal
return 1; return 1;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error setting crop flag for file player: " << e.what() << std::endl; ANS_DBG("FilePlayer_Cfg", "SetCropFlagFilePlayer exception: %s", e.what());
return -1; return -1;
} }
catch (...) { catch (...) {
@@ -874,3 +900,15 @@ extern "C" __declspec(dllexport) void SetFilePlayerDisplayResolution(ANSCENTER::
(*Handle)->SetDisplayResolution(width, height); (*Handle)->SetDisplayResolution(width, height);
} catch (...) { } } catch (...) { }
} }
extern "C" __declspec(dllexport) void SetFilePlayerJitterBuffer(ANSCENTER::ANSFILEPLAYER** Handle, int jitterMs) {
if (Handle == nullptr || *Handle == nullptr) return;
try {
(*Handle)->SetJitterBufferMs(jitterMs);
} catch (...) { }
}
extern "C" __declspec(dllexport) int GetFilePlayerJitterBuffer(ANSCENTER::ANSFILEPLAYER** Handle) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
return (*Handle)->GetJitterBufferMs();
} catch (...) { return -1; }
}
+6
View File
@@ -68,6 +68,10 @@ namespace ANSCENTER
if (mode > 360) _imageRotateDeg = 360; if (mode > 360) _imageRotateDeg = 360;
}; };
void SetDisplayResolution(int width, int height); void SetDisplayResolution(int width, int height);
// Jitter buffer for the BGR delivery path (forwarded to CFilePlayer ->
// CVideoPlayer). 0 = no buffering. Default 200 ms (set by CVideoPlayer).
void SetJitterBufferMs(int ms);
int GetJitterBufferMs() const;
[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts); [[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
[[nodiscard]] cv::Mat GetInferenceImage(); [[nodiscard]] cv::Mat GetInferenceImage();
[[nodiscard]] std::string GetJpegImage(int& width, int& height, int64_t& pts); [[nodiscard]] std::string GetJpegImage(int& width, int& height, int64_t& pts);
@@ -99,6 +103,8 @@ extern "C" __declspec(dllexport) void SetFilePlayerImageRotation(ANSCENTER::ANSF
extern "C" __declspec(dllexport) int SetBBoxFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle, int x, int y, int width, int height); extern "C" __declspec(dllexport) int SetBBoxFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle, int x, int y, int width, int height);
extern "C" __declspec(dllexport) int SetCropFlagFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle, int cropFlag); extern "C" __declspec(dllexport) int SetCropFlagFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle, int cropFlag);
extern "C" __declspec(dllexport) void SetFilePlayerDisplayResolution(ANSCENTER::ANSFILEPLAYER** Handle, int width, int height); extern "C" __declspec(dllexport) void SetFilePlayerDisplayResolution(ANSCENTER::ANSFILEPLAYER** Handle, int width, int height);
extern "C" __declspec(dllexport) void SetFilePlayerJitterBuffer(ANSCENTER::ANSFILEPLAYER** Handle, int jitterMs);
extern "C" __declspec(dllexport) int GetFilePlayerJitterBuffer(ANSCENTER::ANSFILEPLAYER** Handle);
// V2 functions: accept uint64_t handleVal by value (LabVIEW-safe) // V2 functions: accept uint64_t handleVal by value (LabVIEW-safe)
extern "C" __declspec(dllexport) int GetFilePlayerImage_V2(uint64_t handleVal, int& width, int& height, int64_t& timeStamp, LStrHandle jpegImage); extern "C" __declspec(dllexport) int GetFilePlayerImage_V2(uint64_t handleVal, int& width, int& height, int64_t& timeStamp, LStrHandle jpegImage);
+90 -51
View File
@@ -1,6 +1,7 @@
#include "ANSMJPEG.h" #include "ANSMJPEG.h"
#include "ANSMatRegistry.h" #include "ANSMatRegistry.h"
#include "ANSGpuFrameOps.h" #include "ANSGpuFrameOps.h"
#include "av_frame_ptr.h" // ansmedia::AvFramePtr — RAII for AVFrame*
#include "ANSCVVendorGate.h" // anscv_vendor_gate::IsNvidiaGpuAvailable() #include "ANSCVVendorGate.h" // anscv_vendor_gate::IsNvidiaGpuAvailable()
#include "ANSLicense.h" // ANS_DBG macro #include "ANSLicense.h" // ANS_DBG macro
#include <memory> #include <memory>
@@ -213,11 +214,11 @@ namespace ANSCENTER {
} }
} }
// Honest image-equality check. Same buffer pointer (fast path) or pixel
// compare (fallback). Camera-aliveness uses getLastFrameAgeMs() — separate
// concern. With "same image -> same pts" invariant, callers usually just
// compare pts and skip this function entirely.
bool ANSMJPEGClient::areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2) { bool ANSMJPEGClient::areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2) {
double ageMs = _playerClient->getLastFrameAgeMs();
if (ageMs > 5000.0) return true;
if (ageMs > 0.0) return false;
if (img1.empty() && img2.empty()) return true; if (img1.empty() && img2.empty()) return true;
if (img1.empty() || img2.empty()) return false; if (img1.empty() || img2.empty()) return false;
if (img1.size() != img2.size() || img1.type() != img2.type()) return false; if (img1.size() != img2.size() || img1.type() != img2.type()) return false;
@@ -275,17 +276,19 @@ namespace ANSCENTER {
return _pLastFrame; return _pLastFrame;
} }
// Use PTS to detect duplicate frames (much faster than pixel comparison) // Duplicate detection via _lastInnerPts (separate from _pts). Invariant:
if (currentPts == _pts && !_pLastFrame.empty()) { // same image -> same pts. Camera-alive check uses getLastFrameAgeMs().
if (currentPts == _lastInnerPts && !_pLastFrame.empty()) {
width = _imageWidth; width = _imageWidth;
height = _imageHeight; height = _imageHeight;
pts = _pts; pts = _pts; // unchanged
return _pLastFrame; return _pLastFrame;
} }
// Update PTS first // Inner advanced — real new frame. Sync cursor and bump _pts monotonically.
_pts = currentPts; _lastInnerPts = currentPts;
pts = currentPts; _pts++;
pts = _pts;
// Handle non-rotated case // Handle non-rotated case
if (_imageRotateDeg == 0) { if (_imageRotateDeg == 0) {
@@ -633,6 +636,14 @@ namespace ANSCENTER {
std::lock_guard<std::recursive_mutex> lock(_mutex); std::lock_guard<std::recursive_mutex> lock(_mutex);
_playerClient->setTargetFPS(intervalMs); // 0=no limit, 100=~10FPS, 200=~5FPS _playerClient->setTargetFPS(intervalMs); // 0=no limit, 100=~10FPS, 200=~5FPS
} }
void ANSMJPEGClient::SetJitterBufferMs(int ms) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_playerClient->setJitterBufferMs(ms);
ANS_DBG("MJPEG_Lifecycle", "SetJitterBufferMs: ms=%d url=%s", ms, _url.c_str());
}
int ANSMJPEGClient::GetJitterBufferMs() const {
return _playerClient->getJitterBufferMs();
}
void ANSMJPEGClient::SetNV12FastPath(bool enable) { void ANSMJPEGClient::SetNV12FastPath(bool enable) {
std::lock_guard<std::recursive_mutex> lock(_mutex); std::lock_guard<std::recursive_mutex> lock(_mutex);
_useNV12FastPath = enable; _useNV12FastPath = enable;
@@ -727,11 +738,11 @@ extern "C" __declspec(dllexport) int GetMJPEGStrImage(ANSCENTER::ANSMJPEGClient
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error getting MJPEG image: " << e.what() << std::endl; ANS_DBG("MJPEG_GetImage", "exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error getting MJPEG image: Unknown exception." << std::endl; ANS_DBG("MJPEG_GetImage", "unknown exception");
return 0; return 0;
} }
} }
@@ -749,28 +760,28 @@ extern "C" __declspec(dllexport) int GetMJPEGImage(ANSCENTER::ANSMJPEGClient * *
return 1; return 1;
} }
else { else {
std::cerr << "Error resizing jpegImage handle: " << error << std::endl; ANS_DBG("MJPEG_GetImage", "DSSetHandleSize failed: error=%d", (int)error);
return 0; return 0;
} }
} }
else { else {
std::cerr << "No image data retrieved from MJPEG client." << std::endl; ANS_DBG("MJPEG_GetImage", "no image data retrieved (jpegString empty)");
return 0; return 0;
} }
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error getting MJPEG image: " << e.what() << std::endl; ANS_DBG("MJPEG_GetImage", "exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error getting MJPEG image: Unknown exception." << std::endl; ANS_DBG("MJPEG_GetImage", "unknown exception");
return 0; return 0;
} }
} }
extern "C" __declspec(dllexport) int GetMJPEGCVImage(ANSCENTER::ANSMJPEGClient** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image) { extern "C" __declspec(dllexport) int GetMJPEGCVImage(ANSCENTER::ANSMJPEGClient** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image) {
if (!Handle || !*Handle || !image) { if (!Handle || !*Handle || !image) {
std::cerr << "Error: Invalid input parameters in GetMJPEGCVImage" << std::endl; ANS_DBG("MJPEG_GetImage", "invalid input parameters");
return -1; return -1;
} }
@@ -784,17 +795,25 @@ extern "C" __declspec(dllexport) int GetMJPEGCVImage(ANSCENTER::ANSMJPEGClient**
// Thread-safe Mat pointer swap (anscv_mat_replace has its own internal lock) // Thread-safe Mat pointer swap (anscv_mat_replace has its own internal lock)
anscv_mat_replace(image, std::move(img)); anscv_mat_replace(image, std::move(img));
// NV12 GPU fast path (optional — disabled by default for stability) // NV12 GPU fast path (optional — disabled by default for stability).
// AvFramePtr auto-frees clones if we don't reach the .release() handoff.
if ((*Handle)->IsNV12FastPath()) { if ((*Handle)->IsNV12FastPath()) {
int gpuIdx = (*Handle)->GetHWDecodingGpuIndex(); int gpuIdx = (*Handle)->GetHWDecodingGpuIndex();
AVFrame* cudaHW = (*Handle)->GetCudaHWFrame(); ansmedia::AvFramePtr cudaHW{ (*Handle)->GetCudaHWFrame() };
if (cudaHW) { if (cudaHW) {
AVFrame* cpuNV12 = (*Handle)->GetNV12Frame(); ansmedia::AvFramePtr cpuNV12{ (*Handle)->GetNV12Frame() };
gpu_frame_attach_cuda(*image, cudaHW, gpuIdx, timeStamp, cpuNV12); ANS_DBG("MEDIA_AvPtr",
"ANSMJPEG V1 attach_cuda cudaHW=%p cpuNV12=%p gpuIdx=%d",
(void*)cudaHW.get(), (void*)cpuNV12.get(), gpuIdx);
gpu_frame_attach_cuda(*image, cudaHW.release(), gpuIdx, timeStamp,
cpuNV12.release());
} else { } else {
AVFrame* nv12 = (*Handle)->GetNV12Frame(); ansmedia::AvFramePtr nv12{ (*Handle)->GetNV12Frame() };
if (nv12) { if (nv12) {
gpu_frame_attach(*image, nv12, gpuIdx, timeStamp); ANS_DBG("MEDIA_AvPtr",
"ANSMJPEG V1 attach nv12=%p gpuIdx=%d",
(void*)nv12.get(), gpuIdx);
gpu_frame_attach(*image, nv12.release(), gpuIdx, timeStamp);
} }
} }
} }
@@ -802,15 +821,15 @@ extern "C" __declspec(dllexport) int GetMJPEGCVImage(ANSCENTER::ANSMJPEGClient**
return 1; return 1;
} }
catch (const cv::Exception& e) { catch (const cv::Exception& e) {
std::cerr << "OpenCV exception in GetMJPEGCVImage: " << e.what() << std::endl; ANS_DBG("MJPEG_GetImage", "OpenCV exception: %s", e.what());
return -2; return -2;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Exception in GetMJPEGCVImage: " << e.what() << std::endl; ANS_DBG("MJPEG_GetImage", "GetMJPEGCVImage exception: %s", e.what());
return -2; return -2;
} }
catch (...) { catch (...) {
std::cerr << "Unknown exception in GetMJPEGCVImage" << std::endl; ANS_DBG("MJPEG_GetImage", "GetMJPEGCVImage unknown exception");
return -2; return -2;
} }
} }
@@ -822,11 +841,11 @@ extern "C" __declspec(dllexport) int StartMJPEG(ANSCENTER::ANSMJPEGClient * *Han
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error starting MJPEG client: " << e.what() << std::endl; ANS_DBG("MJPEG_Lifecycle", "StartMJPEG exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error starting MJPEG client: Unknown exception." << std::endl; ANS_DBG("MJPEG_Lifecycle", "StartMJPEG unknown exception");
return 0; return 0;
} }
} }
@@ -838,11 +857,11 @@ extern "C" __declspec(dllexport) int ReconnectMJPEG(ANSCENTER::ANSMJPEGClient *
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error reconnecting MJPEG client: " << e.what() << std::endl; ANS_DBG("MJPEG_Lifecycle", "ReconnectMJPEG exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error reconnecting MJPEG client: Unknown exception." << std::endl; ANS_DBG("MJPEG_Lifecycle", "ReconnectMJPEG unknown exception");
return 0; return 0;
} }
} }
@@ -854,11 +873,11 @@ extern "C" __declspec(dllexport) int StopMJPEG(ANSCENTER::ANSMJPEGClient * *Hand
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error stopping MJPEG client: " << e.what() << std::endl; ANS_DBG("MJPEG_Lifecycle", "StopMJPEG exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error stopping MJPEG client: Unknown exception." << std::endl; ANS_DBG("MJPEG_Lifecycle", "StopMJPEG unknown exception");
return 0; return 0;
} }
} }
@@ -970,6 +989,18 @@ extern "C" __declspec(dllexport) void SetMJPEGDisplayResolution(ANSCENTER::ANSMJ
(*Handle)->SetDisplayResolution(width, height); (*Handle)->SetDisplayResolution(width, height);
} catch (...) { } } catch (...) { }
} }
extern "C" __declspec(dllexport) void SetMJPEGJitterBuffer(ANSCENTER::ANSMJPEGClient** Handle, int jitterMs) {
if (Handle == nullptr || *Handle == nullptr) return;
try {
(*Handle)->SetJitterBufferMs(jitterMs);
} catch (...) { }
}
extern "C" __declspec(dllexport) int GetMJPEGJitterBuffer(ANSCENTER::ANSMJPEGClient** Handle) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
return (*Handle)->GetJitterBufferMs();
} catch (...) { return -1; }
}
extern "C" __declspec(dllexport) void SetMJPEGTargetFPS(ANSCENTER::ANSMJPEGClient** Handle, double intervalMs) { extern "C" __declspec(dllexport) void SetMJPEGTargetFPS(ANSCENTER::ANSMJPEGClient** Handle, double intervalMs) {
if (Handle == nullptr || *Handle == nullptr) return; if (Handle == nullptr || *Handle == nullptr) return;
try { try {
@@ -1001,21 +1032,21 @@ extern "C" __declspec(dllexport) int GetMJPEGImage_V2(uint64_t handleVal, int& w
return 1; return 1;
} }
else { else {
std::cerr << "Error resizing jpegImage handle: " << error << std::endl; ANS_DBG("MJPEG_GetImage", "DSSetHandleSize failed: error=%d", (int)error);
return 0; return 0;
} }
} }
else { else {
std::cerr << "No image data retrieved from MJPEG client." << std::endl; ANS_DBG("MJPEG_GetImage", "no image data retrieved (jpegString empty)");
return 0; return 0;
} }
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error getting MJPEG image: " << e.what() << std::endl; ANS_DBG("MJPEG_GetImage", "exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error getting MJPEG image: Unknown exception." << std::endl; ANS_DBG("MJPEG_GetImage", "unknown exception");
return 0; return 0;
} }
} }
@@ -1030,31 +1061,39 @@ extern "C" __declspec(dllexport) int GetMJPEGCVImage_V2(uint64_t handleVal, int&
} }
anscv_mat_replace(image, std::move(img)); anscv_mat_replace(image, std::move(img));
// Attach NV12 frame for GPU fast-path inference (side-table registry) // Attach NV12 frame for GPU fast-path inference (side-table registry).
// AvFramePtr auto-frees clones if we don't reach the .release() handoff.
int gpuIdx = h->GetHWDecodingGpuIndex(); int gpuIdx = h->GetHWDecodingGpuIndex();
AVFrame* cudaHW = h->GetCudaHWFrame(); ansmedia::AvFramePtr cudaHW{ h->GetCudaHWFrame() };
if (cudaHW) { if (cudaHW) {
AVFrame* cpuNV12 = h->GetNV12Frame(); ansmedia::AvFramePtr cpuNV12{ h->GetNV12Frame() };
gpu_frame_attach_cuda(*image, cudaHW, gpuIdx, timeStamp, cpuNV12); ANS_DBG("MEDIA_AvPtr",
"ANSMJPEG V2 attach_cuda cudaHW=%p cpuNV12=%p gpuIdx=%d",
(void*)cudaHW.get(), (void*)cpuNV12.get(), gpuIdx);
gpu_frame_attach_cuda(*image, cudaHW.release(), gpuIdx, timeStamp,
cpuNV12.release());
} else { } else {
AVFrame* nv12 = h->GetNV12Frame(); ansmedia::AvFramePtr nv12{ h->GetNV12Frame() };
if (nv12) { if (nv12) {
gpu_frame_attach(*image, nv12, gpuIdx, timeStamp); ANS_DBG("MEDIA_AvPtr",
"ANSMJPEG V2 attach nv12=%p gpuIdx=%d",
(void*)nv12.get(), gpuIdx);
gpu_frame_attach(*image, nv12.release(), gpuIdx, timeStamp);
} }
} }
return 1; return 1;
} }
catch (const cv::Exception& e) { catch (const cv::Exception& e) {
std::cerr << "OpenCV exception in GetMJPEGCVImage_V2: " << e.what() << std::endl; ANS_DBG("MJPEG_GetImage", "GetMJPEGCVImage_V2 OpenCV exception: %s", e.what());
return -2; return -2;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Exception in GetMJPEGCVImage_V2: " << e.what() << std::endl; ANS_DBG("MJPEG_GetImage", "GetMJPEGCVImage_V2 exception: %s", e.what());
return -2; return -2;
} }
catch (...) { catch (...) {
std::cerr << "Unknown exception in GetMJPEGCVImage_V2" << std::endl; ANS_DBG("MJPEG_GetImage", "GetMJPEGCVImage_V2 unknown exception");
return -2; return -2;
} }
} }
@@ -1067,11 +1106,11 @@ extern "C" __declspec(dllexport) int StartMJPEG_V2(uint64_t handleVal) {
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error starting MJPEG client: " << e.what() << std::endl; ANS_DBG("MJPEG_Lifecycle", "StartMJPEG exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error starting MJPEG client: Unknown exception." << std::endl; ANS_DBG("MJPEG_Lifecycle", "StartMJPEG unknown exception");
return 0; return 0;
} }
} }
@@ -1084,11 +1123,11 @@ extern "C" __declspec(dllexport) int ReconnectMJPEG_V2(uint64_t handleVal) {
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error reconnecting MJPEG client: " << e.what() << std::endl; ANS_DBG("MJPEG_Lifecycle", "ReconnectMJPEG exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error reconnecting MJPEG client: Unknown exception." << std::endl; ANS_DBG("MJPEG_Lifecycle", "ReconnectMJPEG unknown exception");
return 0; return 0;
} }
} }
@@ -1101,11 +1140,11 @@ extern "C" __declspec(dllexport) int StopMJPEG_V2(uint64_t handleVal) {
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error stopping MJPEG client: " << e.what() << std::endl; ANS_DBG("MJPEG_Lifecycle", "StopMJPEG exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error stopping MJPEG client: Unknown exception." << std::endl; ANS_DBG("MJPEG_Lifecycle", "StopMJPEG unknown exception");
return 0; return 0;
} }
} }
+9
View File
@@ -34,6 +34,9 @@ namespace ANSCENTER
std::string _lastJpegImage; std::string _lastJpegImage;
int _imageWidth, _imageHeight; int _imageWidth, _imageHeight;
int64_t _pts; int64_t _pts;
// Last currentPts seen from inner getImage(). Used for duplicate
// detection separately from _pts so "same image -> same pts" holds.
int64_t _lastInnerPts = -1;
bool _isPlaying; bool _isPlaying;
bool _useNV12FastPath = false; bool _useNV12FastPath = false;
std::recursive_mutex _mutex; std::recursive_mutex _mutex;
@@ -72,6 +75,10 @@ namespace ANSCENTER
void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize) void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize)
void SetImageQuality(int mode); // 0=fast (AI), 1=quality (display) void SetImageQuality(int mode); // 0=fast (AI), 1=quality (display)
void SetTargetFPS(double intervalMs); // Set min interval between processed frames in ms (0 = no limit, 100 = ~10 FPS, 200 = ~5 FPS) void SetTargetFPS(double intervalMs); // Set min interval between processed frames in ms (0 = no limit, 100 = ~10 FPS, 200 = ~5 FPS)
// Jitter buffer for the BGR delivery path (forwarded to CVideoPlayer).
// 0 = no buffering (today's getLatest semantics). Default 200 ms.
void SetJitterBufferMs(int ms);
int GetJitterBufferMs() const;
void SetNV12FastPath(bool enable); // true = NV12 GPU fast path, false = original CPU path (stable) void SetNV12FastPath(bool enable); // true = NV12 GPU fast path, false = original CPU path (stable)
bool IsNV12FastPath() const { return _useNV12FastPath; } bool IsNV12FastPath() const { return _useNV12FastPath; }
AVFrame* GetNV12Frame(); // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free) AVFrame* GetNV12Frame(); // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free)
@@ -114,4 +121,6 @@ extern "C" __declspec(dllexport) void SetMJPEGImageQuality(ANSCENTER::ANSMJPEGCl
extern "C" __declspec(dllexport) void SetMJPEGDisplayResolution(ANSCENTER::ANSMJPEGClient** Handle, int width, int height); extern "C" __declspec(dllexport) void SetMJPEGDisplayResolution(ANSCENTER::ANSMJPEGClient** Handle, int width, int height);
extern "C" __declspec(dllexport) void SetMJPEGTargetFPS(ANSCENTER::ANSMJPEGClient** Handle, double intervalMs); extern "C" __declspec(dllexport) void SetMJPEGTargetFPS(ANSCENTER::ANSMJPEGClient** Handle, double intervalMs);
extern "C" __declspec(dllexport) void SetMJPEGNV12FastPath(ANSCENTER::ANSMJPEGClient** Handle, int enable); extern "C" __declspec(dllexport) void SetMJPEGNV12FastPath(ANSCENTER::ANSMJPEGClient** Handle, int enable);
extern "C" __declspec(dllexport) void SetMJPEGJitterBuffer(ANSCENTER::ANSMJPEGClient** Handle, int jitterMs);
extern "C" __declspec(dllexport) int GetMJPEGJitterBuffer(ANSCENTER::ANSMJPEGClient** Handle);
#endif #endif
+105 -53
View File
@@ -1,6 +1,7 @@
#include "ANSRTMP.h" #include "ANSRTMP.h"
#include "ANSMatRegistry.h" #include "ANSMatRegistry.h"
#include "ANSGpuFrameOps.h" #include "ANSGpuFrameOps.h"
#include "av_frame_ptr.h" // ansmedia::AvFramePtr — RAII for AVFrame*
#include "ANSCVVendorGate.h" // anscv_vendor_gate::IsNvidiaGpuAvailable() #include "ANSCVVendorGate.h" // anscv_vendor_gate::IsNvidiaGpuAvailable()
#include "ANSLicense.h" // ANS_DBG macro #include "ANSLicense.h" // ANS_DBG macro
#include <memory> #include <memory>
@@ -218,14 +219,23 @@ namespace ANSCENTER {
} }
} }
// Honest image-equality check. Returns true iff the two Mats refer to the
// same pixels. Used by callers who want to know "did I just get the same
// frame back?" — typical pattern after GetImage to skip downstream work
// on rate-limited / buffered duplicates.
//
// Camera-aliveness ("did the decoder go silent?") is a separate concern;
// use GetRTMPLastFrameAgeMs (or _playerClient->getLastFrameAgeMs()) for
// that. With the new pts discipline (same image -> same pts), most callers
// can simply compare pts instead of calling this function at all.
bool ANSRTMPClient::areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2) { bool ANSRTMPClient::areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2) {
double ageMs = _playerClient->getLastFrameAgeMs();
if (ageMs > 5000.0) return true;
if (ageMs > 0.0) return false;
if (img1.empty() && img2.empty()) return true; if (img1.empty() && img2.empty()) return true;
if (img1.empty() || img2.empty()) return false; if (img1.empty() || img2.empty()) return false;
if (img1.size() != img2.size() || img1.type() != img2.type()) return false; if (img1.size() != img2.size() || img1.type() != img2.type()) return false;
// Fast path — same buffer pointer. With the new GetImage discipline,
// the duplicate path returns _pLastFrame so this branch hits and we
// are done in O(1).
if (img1.data == img2.data) return true; if (img1.data == img2.data) return true;
if (img1.isContinuous() && img2.isContinuous()) { if (img1.isContinuous() && img2.isContinuous()) {
@@ -281,19 +291,25 @@ namespace ANSCENTER {
return _pLastFrame; return _pLastFrame;
} }
// Use PTS to detect duplicate frames (much faster than pixel comparison) // Duplicate-frame detection. Track the inner player's currentPts in
// The sequence-based check in getImage() already skips conversion for same frames, // _lastInnerPts (separate from our own _pts). On a duplicate, return
// so if PTS hasn't changed, it's the same frame — no need for expensive memcmp // _pLastFrame and DO NOT advance _pts — the invariant is:
if (currentPts == _pts && !_pLastFrame.empty()) { // same image -> same pts
// so a caller comparing pts (or calling areImagesIdentical()) can
// recognise rate-limited / buffered duplicates.
// Camera-alive detection should use getLastFrameAgeMs() instead.
if (currentPts == _lastInnerPts && !_pLastFrame.empty()) {
width = _imageWidth; width = _imageWidth;
height = _imageHeight; height = _imageHeight;
pts = _pts; pts = _pts; // unchanged
return _pLastFrame; return _pLastFrame;
} }
// Update PTS first // Inner advanced — real new frame. Sync the duplicate cursor and bump
_pts = currentPts; // _pts monotonically (each ++ corresponds to a unique frame).
pts = currentPts; _lastInnerPts = currentPts;
_pts++;
pts = _pts;
// Handle non-rotated case // Handle non-rotated case
if (_imageRotateDeg == 0) { if (_imageRotateDeg == 0) {
@@ -647,6 +663,14 @@ namespace ANSCENTER {
std::lock_guard<std::recursive_mutex> lock(_mutex); std::lock_guard<std::recursive_mutex> lock(_mutex);
_playerClient->setTargetFPS(intervalMs); // 0=no limit, 100=~10FPS, 200=~5FPS _playerClient->setTargetFPS(intervalMs); // 0=no limit, 100=~10FPS, 200=~5FPS
} }
void ANSRTMPClient::SetJitterBufferMs(int ms) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
_playerClient->setJitterBufferMs(ms);
ANS_DBG("RTMP_Lifecycle", "SetJitterBufferMs: ms=%d url=%s", ms, _url.c_str());
}
int ANSRTMPClient::GetJitterBufferMs() const {
return _playerClient->getJitterBufferMs();
}
void ANSRTMPClient::SetNV12FastPath(bool enable) { void ANSRTMPClient::SetNV12FastPath(bool enable) {
std::lock_guard<std::recursive_mutex> lock(_mutex); std::lock_guard<std::recursive_mutex> lock(_mutex);
_useNV12FastPath = enable; _useNV12FastPath = enable;
@@ -741,11 +765,11 @@ extern "C" __declspec(dllexport) int GetRTMPtrImage(ANSCENTER::ANSRTMPClient * *
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error getting RTMP image: " << e.what() << std::endl; ANS_DBG("RTMP_GetImage", "exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error getting RTMP image: Unknown exception." << std::endl; ANS_DBG("RTMP_GetImage", "unknown exception");
return 0; return 0;
} }
} }
@@ -769,22 +793,22 @@ extern "C" __declspec(dllexport) int GetRTMPImage(ANSCENTER::ANSRTMPClient * *Ha
} }
else { else {
// Return failure if there was an error in resizing the handle // Return failure if there was an error in resizing the handle
std::cerr << "Error resizing jpegImage handle: " << error << std::endl; ANS_DBG("RTMP_GetImage", "DSSetHandleSize failed: error=%d", (int)error);
return 0; return 0;
} }
} }
else { else {
// If the JPEG image string is empty, return failure // If the JPEG image string is empty, return failure
std::cerr << "No image data retrieved from RTMP client." << std::endl; ANS_DBG("RTMP_GetImage", "no image data retrieved (jpegString empty)");
return 0; return 0;
} }
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error getting RTMP image: " << e.what() << std::endl; ANS_DBG("RTMP_GetImage", "exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error getting RTMP image: Unknown exception." << std::endl; ANS_DBG("RTMP_GetImage", "unknown exception");
return 0; return 0;
} }
} }
@@ -792,7 +816,7 @@ extern "C" __declspec(dllexport) int GetRTMPImage(ANSCENTER::ANSRTMPClient * *Ha
extern "C" __declspec(dllexport) int GetRTMPCVImage(ANSCENTER::ANSRTMPClient** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image) { extern "C" __declspec(dllexport) int GetRTMPCVImage(ANSCENTER::ANSRTMPClient** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image) {
// Validate input parameters // Validate input parameters
if (!Handle || !*Handle || !image) { if (!Handle || !*Handle || !image) {
std::cerr << "Error: Invalid input parameters in GetRTMPCVImage" << std::endl; ANS_DBG("RTMP_GetImage", "invalid input parameters");
return -1; return -1;
} }
@@ -808,17 +832,25 @@ extern "C" __declspec(dllexport) int GetRTMPCVImage(ANSCENTER::ANSRTMPClient** H
// Thread-safe Mat pointer swap (anscv_mat_replace has its own internal lock) // Thread-safe Mat pointer swap (anscv_mat_replace has its own internal lock)
anscv_mat_replace(image, std::move(img)); anscv_mat_replace(image, std::move(img));
// NV12 GPU fast path (optional — disabled by default for stability) // NV12 GPU fast path (optional — disabled by default for stability).
// AvFramePtr auto-frees clones if we don't reach the .release() handoff.
if ((*Handle)->IsNV12FastPath()) { if ((*Handle)->IsNV12FastPath()) {
int gpuIdx = (*Handle)->GetHWDecodingGpuIndex(); int gpuIdx = (*Handle)->GetHWDecodingGpuIndex();
AVFrame* cudaHW = (*Handle)->GetCudaHWFrame(); ansmedia::AvFramePtr cudaHW{ (*Handle)->GetCudaHWFrame() };
if (cudaHW) { if (cudaHW) {
AVFrame* cpuNV12 = (*Handle)->GetNV12Frame(); ansmedia::AvFramePtr cpuNV12{ (*Handle)->GetNV12Frame() };
gpu_frame_attach_cuda(*image, cudaHW, gpuIdx, timeStamp, cpuNV12); ANS_DBG("MEDIA_AvPtr",
"ANSRTMP V1 attach_cuda cudaHW=%p cpuNV12=%p gpuIdx=%d",
(void*)cudaHW.get(), (void*)cpuNV12.get(), gpuIdx);
gpu_frame_attach_cuda(*image, cudaHW.release(), gpuIdx, timeStamp,
cpuNV12.release());
} else { } else {
AVFrame* nv12 = (*Handle)->GetNV12Frame(); ansmedia::AvFramePtr nv12{ (*Handle)->GetNV12Frame() };
if (nv12) { if (nv12) {
gpu_frame_attach(*image, nv12, gpuIdx, timeStamp); ANS_DBG("MEDIA_AvPtr",
"ANSRTMP V1 attach nv12=%p gpuIdx=%d",
(void*)nv12.get(), gpuIdx);
gpu_frame_attach(*image, nv12.release(), gpuIdx, timeStamp);
} }
} }
} }
@@ -826,15 +858,15 @@ extern "C" __declspec(dllexport) int GetRTMPCVImage(ANSCENTER::ANSRTMPClient** H
return 1; // Success return 1; // Success
} }
catch (const cv::Exception& e) { catch (const cv::Exception& e) {
std::cerr << "OpenCV exception in GetRTMPCVImage: " << e.what() << std::endl; ANS_DBG("RTMP_GetImage", "OpenCV exception: %s", e.what());
return -2; return -2;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Exception in GetRTMPCVImage: " << e.what() << std::endl; ANS_DBG("RTMP_GetImage", "GetRTMPCVImage exception: %s", e.what());
return -2; return -2;
} }
catch (...) { catch (...) {
std::cerr << "Unknown exception in GetRTMPCVImage" << std::endl; ANS_DBG("RTMP_GetImage", "GetRTMPCVImage unknown exception");
return -2; return -2;
} }
} }
@@ -846,11 +878,11 @@ extern "C" __declspec(dllexport) int StartRTMP(ANSCENTER::ANSRTMPClient * *Handl
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error starting RTMP client: " << e.what() << std::endl; ANS_DBG("RTMP_Lifecycle", "StartRTMP exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error starting RTMP client: Unknown exception." << std::endl; ANS_DBG("RTMP_Lifecycle", "StartRTMP unknown exception");
return 0; return 0;
} }
} }
@@ -862,11 +894,11 @@ extern "C" __declspec(dllexport) int ReconnectRTMP(ANSCENTER::ANSRTMPClient * *H
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error reconnecting RTMP client: " << e.what() << std::endl; ANS_DBG("RTMP_Lifecycle", "ReconnectRTMP exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error reconnecting RTMP client: Unknown exception." << std::endl; ANS_DBG("RTMP_Lifecycle", "ReconnectRTMP unknown exception");
return 0; return 0;
} }
} }
@@ -878,11 +910,11 @@ extern "C" __declspec(dllexport) int StopRTMP(ANSCENTER::ANSRTMPClient * *Handle
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error stopping RTMP client: " << e.what() << std::endl; ANS_DBG("RTMP_Lifecycle", "StopRTMP exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error stopping RTMP client: Unknown exception." << std::endl; ANS_DBG("RTMP_Lifecycle", "StopRTMP unknown exception");
return 0; return 0;
} }
} }
@@ -998,6 +1030,18 @@ extern "C" __declspec(dllexport) void SetRTMPTargetFPS(ANSCENTER::ANSRTMPClient*
(*Handle)->SetTargetFPS(intervalMs); (*Handle)->SetTargetFPS(intervalMs);
} catch (...) { } } catch (...) { }
} }
extern "C" __declspec(dllexport) void SetRTMPJitterBuffer(ANSCENTER::ANSRTMPClient** Handle, int jitterMs) {
if (Handle == nullptr || *Handle == nullptr) return;
try {
(*Handle)->SetJitterBufferMs(jitterMs);
} catch (...) { }
}
extern "C" __declspec(dllexport) int GetRTMPJitterBuffer(ANSCENTER::ANSRTMPClient** Handle) {
if (Handle == nullptr || *Handle == nullptr) return -1;
try {
return (*Handle)->GetJitterBufferMs();
} catch (...) { return -1; }
}
extern "C" __declspec(dllexport) void SetRTMPNV12FastPath(ANSCENTER::ANSRTMPClient** Handle, int enable) { extern "C" __declspec(dllexport) void SetRTMPNV12FastPath(ANSCENTER::ANSRTMPClient** Handle, int enable) {
if (Handle == nullptr || *Handle == nullptr) return; if (Handle == nullptr || *Handle == nullptr) return;
try { try {
@@ -1024,21 +1068,21 @@ extern "C" __declspec(dllexport) int GetRTMPImage_V2(uint64_t handleVal, int& wi
return 1; return 1;
} }
else { else {
std::cerr << "Error resizing jpegImage handle: " << error << std::endl; ANS_DBG("RTMP_GetImage", "DSSetHandleSize failed: error=%d", (int)error);
return 0; return 0;
} }
} }
else { else {
std::cerr << "No image data retrieved from RTMP client." << std::endl; ANS_DBG("RTMP_GetImage", "no image data retrieved (jpegString empty)");
return 0; return 0;
} }
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error getting RTMP image: " << e.what() << std::endl; ANS_DBG("RTMP_GetImage", "exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error getting RTMP image: Unknown exception." << std::endl; ANS_DBG("RTMP_GetImage", "unknown exception");
return 0; return 0;
} }
} }
@@ -1053,31 +1097,39 @@ extern "C" __declspec(dllexport) int GetRTMPCVImage_V2(uint64_t handleVal, int&
} }
anscv_mat_replace(image, std::move(img)); anscv_mat_replace(image, std::move(img));
// Attach NV12 frame for GPU fast-path inference (side-table registry) // Attach NV12 frame for GPU fast-path inference (side-table registry).
// AvFramePtr auto-frees clones if we don't reach the .release() handoff.
int gpuIdx = h->GetHWDecodingGpuIndex(); int gpuIdx = h->GetHWDecodingGpuIndex();
AVFrame* cudaHW = h->GetCudaHWFrame(); ansmedia::AvFramePtr cudaHW{ h->GetCudaHWFrame() };
if (cudaHW) { if (cudaHW) {
AVFrame* cpuNV12 = h->GetNV12Frame(); ansmedia::AvFramePtr cpuNV12{ h->GetNV12Frame() };
gpu_frame_attach_cuda(*image, cudaHW, gpuIdx, timeStamp, cpuNV12); ANS_DBG("MEDIA_AvPtr",
"ANSRTMP V2 attach_cuda cudaHW=%p cpuNV12=%p gpuIdx=%d",
(void*)cudaHW.get(), (void*)cpuNV12.get(), gpuIdx);
gpu_frame_attach_cuda(*image, cudaHW.release(), gpuIdx, timeStamp,
cpuNV12.release());
} else { } else {
AVFrame* nv12 = h->GetNV12Frame(); ansmedia::AvFramePtr nv12{ h->GetNV12Frame() };
if (nv12) { if (nv12) {
gpu_frame_attach(*image, nv12, gpuIdx, timeStamp); ANS_DBG("MEDIA_AvPtr",
"ANSRTMP V2 attach nv12=%p gpuIdx=%d",
(void*)nv12.get(), gpuIdx);
gpu_frame_attach(*image, nv12.release(), gpuIdx, timeStamp);
} }
} }
return 1; return 1;
} }
catch (const cv::Exception& e) { catch (const cv::Exception& e) {
std::cerr << "OpenCV exception in GetRTMPCVImage_V2: " << e.what() << std::endl; ANS_DBG("RTMP_GetImage", "GetRTMPCVImage_V2 OpenCV exception: %s", e.what());
return -2; return -2;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Exception in GetRTMPCVImage_V2: " << e.what() << std::endl; ANS_DBG("RTMP_GetImage", "GetRTMPCVImage_V2 exception: %s", e.what());
return -2; return -2;
} }
catch (...) { catch (...) {
std::cerr << "Unknown exception in GetRTMPCVImage_V2" << std::endl; ANS_DBG("RTMP_GetImage", "GetRTMPCVImage_V2 unknown exception");
return -2; return -2;
} }
} }
@@ -1090,11 +1142,11 @@ extern "C" __declspec(dllexport) int StartRTMP_V2(uint64_t handleVal) {
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error starting RTMP client: " << e.what() << std::endl; ANS_DBG("RTMP_Lifecycle", "StartRTMP exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error starting RTMP client: Unknown exception." << std::endl; ANS_DBG("RTMP_Lifecycle", "StartRTMP unknown exception");
return 0; return 0;
} }
} }
@@ -1107,11 +1159,11 @@ extern "C" __declspec(dllexport) int ReconnectRTMP_V2(uint64_t handleVal) {
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error reconnecting RTMP client: " << e.what() << std::endl; ANS_DBG("RTMP_Lifecycle", "ReconnectRTMP exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error reconnecting RTMP client: Unknown exception." << std::endl; ANS_DBG("RTMP_Lifecycle", "ReconnectRTMP unknown exception");
return 0; return 0;
} }
} }
@@ -1124,11 +1176,11 @@ extern "C" __declspec(dllexport) int StopRTMP_V2(uint64_t handleVal) {
else return 0; else return 0;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
std::cerr << "Error stopping RTMP client: " << e.what() << std::endl; ANS_DBG("RTMP_Lifecycle", "StopRTMP exception: %s", e.what());
return 0; return 0;
} }
catch (...) { catch (...) {
std::cerr << "Error stopping RTMP client: Unknown exception." << std::endl; ANS_DBG("RTMP_Lifecycle", "StopRTMP unknown exception");
return 0; return 0;
} }
} }
+12
View File
@@ -35,6 +35,11 @@ namespace ANSCENTER
std::string _lastJpegImage; std::string _lastJpegImage;
int _imageWidth, _imageHeight; int _imageWidth, _imageHeight;
int64_t _pts; int64_t _pts;
// Last currentPts seen from the inner getImage(). Used for duplicate
// detection separately from _pts so the invariant
// same image -> same pts
// holds for callers comparing pts (or calling areImagesIdentical()).
int64_t _lastInnerPts = -1;
bool _isPlaying; bool _isPlaying;
bool _useNV12FastPath = false; bool _useNV12FastPath = false;
std::recursive_mutex _mutex; std::recursive_mutex _mutex;
@@ -73,6 +78,11 @@ namespace ANSCENTER
void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize) void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize)
void SetImageQuality(int mode); // 0=fast (AI), 1=quality (display) void SetImageQuality(int mode); // 0=fast (AI), 1=quality (display)
void SetTargetFPS(double intervalMs); // Set min interval between processed frames in ms (0 = no limit, 100 = ~10 FPS, 200 = ~5 FPS) void SetTargetFPS(double intervalMs); // Set min interval between processed frames in ms (0 = no limit, 100 = ~10 FPS, 200 = ~5 FPS)
// Jitter buffer for the BGR delivery path (forwarded to CVideoPlayer).
// 0 = no buffering (today's getLatest semantics). Default 200 ms (set
// inside CVideoPlayer constructor). Adaptive: slow consumers bypass it.
void SetJitterBufferMs(int ms);
int GetJitterBufferMs() const;
void SetNV12FastPath(bool enable); // true = NV12 GPU fast path, false = original CPU path (stable) void SetNV12FastPath(bool enable); // true = NV12 GPU fast path, false = original CPU path (stable)
bool IsNV12FastPath() const { return _useNV12FastPath; } bool IsNV12FastPath() const { return _useNV12FastPath; }
AVFrame* GetNV12Frame(); // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free) AVFrame* GetNV12Frame(); // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free)
@@ -113,4 +123,6 @@ extern "C" __declspec(dllexport) void SetRTMPImageQuality(ANSCENTER::ANSRTMPClie
extern "C" __declspec(dllexport) void SetRTMPDisplayResolution(ANSCENTER::ANSRTMPClient** Handle, int width, int height); extern "C" __declspec(dllexport) void SetRTMPDisplayResolution(ANSCENTER::ANSRTMPClient** Handle, int width, int height);
extern "C" __declspec(dllexport) void SetRTMPTargetFPS(ANSCENTER::ANSRTMPClient** Handle, double intervalMs); extern "C" __declspec(dllexport) void SetRTMPTargetFPS(ANSCENTER::ANSRTMPClient** Handle, double intervalMs);
extern "C" __declspec(dllexport) void SetRTMPNV12FastPath(ANSCENTER::ANSRTMPClient** Handle, int enable); extern "C" __declspec(dllexport) void SetRTMPNV12FastPath(ANSCENTER::ANSRTMPClient** Handle, int enable);
extern "C" __declspec(dllexport) void SetRTMPJitterBuffer(ANSCENTER::ANSRTMPClient** Handle, int jitterMs);
extern "C" __declspec(dllexport) int GetRTMPJitterBuffer(ANSCENTER::ANSRTMPClient** Handle);
#endif #endif

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