Metadata-Version: 2.5
Name: revela
Version: 0.4.0
Summary: An image signal processing pipeline written in NumPy, from which synthesisable Verilog is generated
Project-URL: Homepage, https://github.com/lanserge/revela
Project-URL: Source, https://github.com/lanserge/revela
Author: Serge Rabyking
License-Expression: Apache-2.0 WITH SHL-2.1
License-File: LICENSE
License-File: LICENSE-APACHE
License-File: NOTICE
Keywords: camera,fpga,hls,image-signal-processor,isp,numpy,verilog
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3.11
Classifier: Topic :: Scientific/Engineering :: Image Processing
Classifier: Topic :: Software Development :: Embedded Systems
Requires-Python: >=3.11
Requires-Dist: jsonschema>=4.0
Requires-Dist: np2hw>=0.7.0
Requires-Dist: numpy>=1.24
Provides-Extra: boards
Requires-Dist: bayerlink-fpga>=0.3.0; extra == 'boards'
Provides-Extra: dev
Requires-Dist: cocotb>=2.0; extra == 'dev'
Requires-Dist: pytest>=7.0; extra == 'dev'
Requires-Dist: systemrdl-compiler>=1.29; extra == 'dev'
Description-Content-Type: text/markdown

# revela

**An image signal processing pipeline written in NumPy, from which synthesisable
Verilog is generated.**

An ISP turns what a camera sensor actually produces — a single-colour-per-pixel
mosaic sitting on a pedestal, with the sensor's noise, its dead pixels and its
lens's shading baked in — into an image. revela is that pipeline, written as
NumPy, at the arithmetic the hardware will really use, and compiled to RTL.

```python
def kernel(pixel, offset, bit_depth):
    """One pixel path: add the signed offset, saturate to the datapath range."""
    return (pixel.astype(np.int32) + offset).clip(0, (1 << bit_depth) - 1)
```

That is not a model of the hardware. It **is** the hardware: it is traced into
Verilog, and it is the reference the Verilog is checked against, bit for bit,
under Verilator. There is exactly one of it.

## What makes this different

Most ISP projects have two of everything: a floating-point reference model that
describes the intent, and an RTL implementation that approximates it. Then they
spend their lives arguing about the difference between the two and calling it
"quantisation error". It isn't. The fixed-point algorithm is a *different
algorithm* — different rounding, different saturation, different LUT breakpoints
— and measuring its distance from a float algorithm measures nothing useful.

revela has one model per block, written directly at the hardware's arithmetic:
integer dtypes with explicit widths, explicit rounding, explicit saturation, LUTs
where the hardware will have LUTs, shifts where the hardware will shift. **No
`np.float64` appears anywhere in `revela/blocks/`.** That model is the
specification, the golden reference, and the thing the RTL is generated from.

Image quality is measured where it means something — at pipeline level, against
reference images. Never per block.

## Relationship to np2hw

[**np2hw**](https://github.com/lanserge/np2hw) is the generator: a compiler that
traces NumPy into a streaming line-based IR and emits synthesisable Verilog, with
line buffers, shift registers, edge handling and config registers built for you.

**revela is the ISP, and np2hw's flagship proof case.** The two are separate
projects and revela *depends* on np2hw — it does not vendor or fork it. That
separation is load-bearing in both directions: np2hw stays a general NumPy→RTL
compiler rather than growing ISP-shaped special cases, and revela gets to be a
real ISP rather than a compiler test suite. When revela needs something np2hw
cannot express, the fix goes into np2hw *as a generic capability at the NumPy
level* — never as a revela-side workaround.

**revela emits no Verilog.** Block datapaths are traced from the NumPy models by
np2hw; the top level is built by `np2hw.compose()`, which instantiates the
generated cores and wires the netlist; the AXI4-Lite register file in front of it
is emitted by `np2hw.control_wrap()`. revela supplies the models, the register
declarations and their descriptions, the design JSON, and the software — the
register map with per-instance address allocation, sensor descriptions, the
statistics block the 3A loops read, and the host API.

The split at the control plane is the same one: **revela decides where every
register lives, np2hw decides what an AXI4-Lite slave looks like.** Neither holds
a copy of the other's half, which is why the emitted JSON register map and the
emitted decode cannot drift apart — a test reads the addresses back out of the
generated Verilog and compares them with the map.

## Architecture in one diagram

```
           width, height, bayer_phase, bit_depth  (with each frame, from its header)
              │
sensor ─▶ [ blacklevel ] ─▶ [ whitebalance ] ─▶ [ ha_green ] ─▶ [ ha_rb ] ─▶ [ ccm ] ─▶ out
          └──────────────────────────── bayer_isp ────────────────────────────┘

  pipe @ 0x0000 ── window, commit ──▶ (wires to all)
```

The pipeline context has one owner and reaches every block as wires, never
as a copy per block: a resolution change is one write, not N that must all
land in the same frame. Where each fact comes FROM is the design's to
declare — anything named in `stream.context` arrives as an input port
carried by the stream itself and gets **no register**, because a register
holding a second copy of a header fact is a second answer to one question.
What stays a register is what is genuinely a choice: a crop window is
software's to pick, a line length is not.

Blocks are chained by a direct `valid/ready` stream — no bus between stages, no
DRAM round-trip. One pixel per clock, and backpressure composes all the way back
to the sensor interface. AXI4-Stream Video appears only as an adapter at the
pipeline boundary, where you are talking to somebody else's IP.

## Where the pixels come from

revela generates an ISP, not a receiver: something upstream has to put raw
Bayer on that `valid/ready` stream. Nothing here requires a particular
source — the pipeline takes samples and its context, whatever produced
them — but there is a complete path that works today, and it is worth
naming because the pieces are designed to fit:

| piece | what it does |
| --- | --- |
| [bayerlink](https://github.com/bayerlink/bayerlink) | the wire format: raw Bayer over an ordinary video link, self-described by a header line — geometry, CFA phase and depth travel with the frame |
| [bayerlink-fpga](https://github.com/bayerlink/bayerlink-fpga) | the receiver: decodes that link on an FPGA, behind its TMDS front ends, and aligns sample depth on the way in, so downstream the depth is a build fact |
| **revela** | the ISP: this project |
| [`revela/boards/pynq_z2`](revela/boards/pynq_z2) | all of them on a PYNQ-Z2, with the board glue — the worked example |
| [picam2hdmi](https://github.com/bayerlink/picam2hdmi) | a source at the other end: a Raspberry Pi, genlocked, any libcamera sensor |

Two things make the seam work, and both are worth knowing before wiring
anything to this ISP:

**The boundary is traced, not declared.** `np2hw pipeline generate` writes
`<name>_build.json` beside the Verilog: each output's `data_bits`,
`channels` and `channel_bits`, the geometry the cores were built around,
and the latency. **Read it — do not restate it.** A three-channel word is
three fields wide because the models packed it that way, not because
anyone chose the number, and an integrator that writes the width down is
correct until the day the pipeline changes and then produces a picture
rather than an error.

**The geometry in that file is a maximum.** The cores get neither the
stream's end-of-line nor the header's width, so a frame wider than they
were built for is not something they can notice — it shears, with the CFA
phase inverted on alternate rows and no status bit anywhere. Whoever hands
them a wider one must crop first; the PYNQ-Z2 board's shim shows one way,
cropping to the built width and saying so in a sticky `truncated`.

## Quick start

```bash
git clone https://github.com/lanserge/revela
cd revela
python -m venv .venv && . .venv/bin/activate
pip install -e ".[dev]"          # np2hw arrives from PyPI
pytest                            # includes the bit-exact model-vs-RTL tests
```

Verilator must be on the path — `apt install verilator` or `brew install
verilator`. The bit-exact tests skip without it; everything else still runs.

A pipeline is described in JSON and nowhere else — a **composite**: named
block instances, the connections between their ports, and the stream it is
built for. The standard ISP, `revela.pipelines.bayer_isp`, is one:

```json
{"blocks": {"bayer_isp": {
  "parameters": { "bit_depth": {}, "max_width": {}, "max_height": {} },
  "inputs":  [{ "name": "in", "bits": "bit_depth",
                "max_width": "max_width", "max_height": "max_height",
                "context": ["width", "height", "bayer_phase", "bit_depth"] }],
  "outputs": [{ "name": "out" }],
  "nodes": {
    "blacklevel":   { "block": "revela.blocks.blacklevel" },
    "whitebalance": { "block": "revela.blocks.whitebalance" },
    "ha_green":     { "block": "revela.blocks.demosaic.hamilton_adams:ha_green" },
    "ha_rb":        { "block": "revela.blocks.demosaic.hamilton_adams:ha_rb" },
    "ccm":          { "block": "revela.blocks.ccm" }
  },
  "connections": [
    ["in", "blacklevel.in"], ["blacklevel.out", "whitebalance.in"],
    ["whitebalance.out", "ha_green.in"], ["ha_green.out", "ha_rb.in"],
    ["ha_rb.out", "ccm.in"], ["ccm.out", "out"]
  ]
}}}
```

A netlist rather than a list, because a real ISP is not a chain: statistics
tap the datapath and produce no pixels, a preview path forks off the main one,
HDR merges two exposures. A list of blocks cannot say any of that — which is
the error the format is shaped to prevent.

The ISP is sensor-independent. What it is built for is three numbers — the
depth samples arrive at, the widest frame and the tallest — and they are its
parameters, stated on the input port; a sensor whose mode fits them is
served. Geometry and Bayer phase ride the stream as context, latched each
frame, never registers. Blocks are referenced as `package.module:block`, and a
composite is a block like any other, so designs are built of it: the stereo
pipeline is two of it, and the PYNQ-Z2's ISP is it followed by a display
curve. A node may also set a declared register's build-time attributes —
`"build": { "knots.bits": 11 }` — values only, never addresses; the model, the
RTL, the register map and the host all follow the one declaration.

```bash
np2hw pipeline generate revela.pipelines.stereo:revela_stereo --out build --clock-mhz 148.5
```

That is the shipping path: it builds the design, proves the generated Verilog
bit-exact against the same NumPy models under Verilator, and only then writes
the pack — Verilog, register map, SystemRDL, and a FuseSoC core manifest. It
refuses to emit anything it has not verified, so a failure there is a
verification failure rather than a tool problem. The register documentation is
part of that pack, not a later step. To read a design rather than ship it,
`examples/build_pipeline.py` writes the same pack -- through the same emitter --
and prints the address map, the stream topology and a profile's resolved
values.

`revela/blocks/` holds the blocks -- algorithms, each with its model.
`revela/pipelines/` holds what is composed of them, one to a file: the
standard ISP, `bayer_isp.json`, and designs built of it; the board's design is
in `revela/boards/`. A sensor's tunings are with the sensor, in
`revela/sensors/<name>/`.

A **profile** is a sensor's register values for one block — the ov5647's is
for `bayer_isp` — keyed relative to it, so it tunes every design built of that
block, at every instance: both eyes of the stereo design, the board's one ISP.
It cannot add a block or rewire the graph, and every value is checked against
the built register map — its name, its shape, its range. A design's own values
are its reset values; a profile is the layer on top.

**Inputs carry no addresses.** The design and the profile are *inputs*, and
neither has anywhere to write an address. The register map is the *output*.
That is what stops a builder GUI from emitting a map that disagrees with the
hardware it generated — and a test proves the same design, its connections
written in any order, builds to the same addresses, so allocation is a pure
function of structure.

The register map JSON is the authoritative address map. The host API and the
register-map documentation are both generated from it; nothing anywhere
hardcodes an address.

## The rules this project is built on

Four, and they are not style preferences — they are why the thing works. They are
stated in full in [docs/design-rules.md](docs/design-rules.md).

1. **One model per block**, at the hardware's arithmetic. No float reference, no
   float-versus-fixed comparison.
2. **Parameters are declared once, per block, and allocated globally.** Blocks
   declare local offsets; the pipeline assigns each block *instance* a base
   address. A stereo pipeline instantiates every block twice, and every block is
   independently testable — both fall out of that, and neither works without it.
3. **Verification is bit-exact agreement** between the NumPy model and the
   generated Verilog under Verilator via cocotb. Identical, not "close enough".
4. **Generated Verilog must be readable.** Meaningful signal names, module names
   matching the block, and each parameter's description carried through from its
   declaration. Somebody licensing this will read the output in review and hand
   it to their own verification team. That is a product requirement.

## Status

`blacklevel`, `whitebalance`, `gamma`, `rgb_gamma`, `ccm`, `crop`, `logo`,
`bilinear`, `bicubic` and the two-stage adaptive `hamilton_adams` (`ha_green`,
`ha_rb`) are complete end to end — model, generated Verilog, bit-exact cocotb tests,
fixed-point variants through build-time overrides — and contain **no
hand-written Verilog**: each block, phase mux, window and LUT included, is
one np2hw trace of its NumPy model. `bilinear` is the first block whose tap
combination is selected by CFA position over a shared line-buffered window,
and the first to emit a three-channel stream — its model ends in
`np.stack([r, g, b], axis=-1)`, and the wire packing belongs to the stream
layer alone. `tofquad` and `tofrange` are complete the same way: time-of-flight
phase and amplitude by CORDIC, from the last four frames of the stream's
history, each frame's place in the capture loop bound to the frame count its
header carries, then perpendicular depth in millimetres from the phase and the
lens -- composed as `tof_core`, the pair is bit-exact with its generated
Verilog, and `tof_isp` puts a frame store before them, proved with it. `depthview` makes depth a smooth rainbow a display shows, red near to violet
far, a colour every few millimetres.
`pipe`, `stats`, the register map with its AXI4-Lite control plane, address
allocation, build-time register overrides, the sensor schema and the
composition layer are implemented. The remaining blocks (`lsc`, `defect`,
the corrected demosaics (`malvar` — patent-gated until 2027-01-25 — and
`menon`), `rgb2yuv`, `sharpen`) are declared stubs, each with its intent
documented in its own file. Every declared stub and
documented gap is a sponsorable goal — the
[`sponsorable` label](https://github.com/lanserge/revela/issues?q=label%3Asponsorable)
carries the full slate, scoped and ready to fund.

`stats` has a model and a register map but no generated RTL: np2hw traces a
fold -- each pixel into the entry its key names -- and the model is not yet
written as one. It is the one block that does not currently satisfy rule 3,
and that is stated rather than papered over with a test that only exercises
the NumPy side.

## Licence

**Solderpad Hardware License v2.1** — see [LICENSE](LICENSE). SHL-2.1 is a
wraparound over Apache 2.0, so the Apache text is included as
[LICENSE-APACHE](LICENSE-APACHE), with a [NOTICE](NOTICE) file. Every source file
carries:

```
# SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
```

Solderpad is the right licence here because Apache 2.0 was written for software
and its terms do not cleanly describe what happens when a design is *fabricated*.
SHL-2.1 extends the definitions to cover hardware: "Object form" includes FPGA
bitstreams, mask works and physical instantiation, and "Rights" covers design
right and semiconductor topography rights, not just copyright. You may, at your
option, treat any work released under it as released under Apache 2.0.

Verilog generated by revela carries the same licence, because its datapath is
derived from the models; generated files say so in their header. The structure
np2hw emits around it comes with no obligations at all — see
[docs/LICENSING.md](docs/LICENSING.md).

**A commercial licence, with support and indemnity, is available on request.**

What licence applies to the Verilog revela generates, what you owe by shipping
it, and what the commercial licence adds: **[docs/LICENSING.md](docs/LICENSING.md)**.
Every dependency and its licence: [docs/THIRD-PARTY.md](docs/THIRD-PARTY.md).

## Contributing

Contributions to the ISP require a signed [CLA](CLA.md) before a pull request is
merged; board designs under `revela/boards/` do not — see
[CONTRIBUTING.md](CONTRIBUTING.md). It is a licence grant, not an assignment:
you keep your copyright. Two things that will get a sensor contribution rejected
on sight: register tables transcribed out of the Linux kernel media drivers (they
are GPL-2.0 and incompatible with this licence), and per-unit calibration data
committed as if it described the sensor model.

## Funding

Developed independently. Detail in [FUNDING.md](FUNDING.md); enquiries to
**s.rabykin@gmail.com**.

- **Commercial licence** — patent grant, indemnity, support SLA, and access to
  advanced blocks and verification collateral.
- **Sponsor a block** — fund a declared stub (`lsc`, `defect`, `demosaic`,
  `rgb2yuv`, `sharpen`) or a documented gap; the
  [`sponsorable` label](https://github.com/lanserge/revela/issues?q=label%3Asponsorable)
  is the slate. Done means bit-exact under the four
  rules, and the work lands here openly, immediately — sponsorship buys
  ordering and named credit, not exclusivity.
- **Sponsor a sensor** — fund characterisation of a sensor you need; you pick it
  and the conditions, get the data first, and it is then published here for all.
- **Consulting** — ISP bring-up, tuning, custom blocks.
- **[GitHub Sponsors](https://github.com/sponsors/lanserge)** — recurring support.

---

revela™ is a trademark of Serge Rabyking. The licence does not grant rights to
the name — see [TRADEMARK.md](TRADEMARK.md).
