# Phase 2 Design: Contributor-Rendered Frames and Low-Resolution Sampling Verifier

## Motivation

In Phase 1, contributors submit `shots/<id>/shot.json` (plain UTF-8 text on today's desk) and the project's own release job renders accepted shots in Blender Cycles. That keeps intake lightweight and deterministic, while concentrating render compute on the project's release runner.

Phase 2 lets contributors render their own high-resolution frames (`1920 x 1080`, 64 to 128 samples) on their own CPU or GPU and submit the rendered frame sequence alongside `shot.json`, turning the film into distributed crowd rendering as well as crowd direction.

## Dependency: Wider Desk Step 2 (Binary Store)

A single 4-second shot at 24 fps produces 96 high-resolution PNG or high-quality JPEG frames (~20 to 80 MB total), which exceeds the Rung 1 text-bundle cap (2 MiB). Phase 2 therefore depends on the wider desk's **step 2 binary store**:

1. **Submission layout**:
   - Text slot (`shots/<id>/shot.json`): passes the exact Phase 1 deterministic checker first (validates timing, volume, collision clearance, calm motion, and composition).
   - Binary attachment manifest (`shots/<id>/frames.json` + content-addressed image objects in quarantine storage): lists exact `frame_index`, `sha256`, `width`, `height`, `media_type` (`image/png` or `image/jpeg`), and `byte_length` for every frame in `[frame_start, frame_end]`.
2. **Quarantine before verification**: Uploaded binary frames live on an isolated quarantine origin until the verifier signs a verdict; only accepted frames are promoted to the release bucket for final assembly.

## Verification Strategy: Deterministic Low-Resolution Re-Render Comparison

Re-rendering all 96 frames at full 1080p in CI would defeat distributed rendering. Instead, the desk verifier runs a **cheap sampled audit**:

1. **Deterministic frame sampling**:
   - Derive a seed from `sha256(shot.json || frames.json || lease_nonce)` so a contributor cannot predict in advance which frames will be audited, while the audit remains reproducible from the receipt.
   - Sample `K = 3` frames (first third, middle, final third — or configurable `--samples K`) from `[frame_start, frame_end]`.
2. **Low-resolution Cycles reference render**:
   - Using the pinned scene manifest (`desk/scene-manifest.json`), pinned part bundle digests, and the submitted `shot.json`, render ONLY those `K` frames headless in Blender Cycles at **`320 x 180`**, **8 to 12 samples** with OpenImageDenoise enabled.
   - Cost: roughly 1.5 to 3 seconds total on CPU (under 0.5 seconds on GPU), compared to 5 to 20 minutes for the contributor's full 1080p render — a **100x to 400x compute asymmetry** in favor of verification.
3. **Downscale and perceptual comparison (`tools/phase2/verify-frames.mjs`)**:
   - Downscale each submitted frame to `320 x 180` using area averaging (`ffmpeg -vf scale=320:180:flags=area`).
   - Compare each downscaled submitted frame against its low-res reference frame using four complementary metrics:
     - **Mean Absolute Error (MAE)** in `[0, 1]`: catches gross color shifts, exposure tampering, or wrong lighting presets (`<= 0.085`).
     - **Peak Signal-to-Noise Ratio (PSNR)** in dB: catches local artifacts and substituted geometry (`>= 18.0 dB`).
     - **Windowed Structural Similarity (SSIM)** on luma (`8 x 8` blocks): catches camera offsets, wrong lens, missing parts, or swapped frames while remaining robust to minor Monte Carlo noise differences across CPU/Metal/CUDA/OptiX devices (`>= 0.78`).
     - **Luminance histogram correlation**: catches inverted, clipped, or blank frames (`>= 0.85`).

## Cross-Hardware Tolerance Budget

Because contributors render on diverse hardware (Apple Metal, NVIDIA CUDA/OptiX, AMD HIP, x86/ARM CPU) and with different sample counts (e.g., 64 vs 128 samples), pixel-exact hashing is impossible. Our local calibration shows that genuine renders of the same `shot.json` at different resolutions (1280x720 @ 16 samples vs 320x180 @ 8 samples) achieve:
- **SSIM**: `0.94 to 0.99` (well above the `0.78` floor)
- **PSNR**: `28 to 38 dB` (well above the `18.0 dB` floor)
- **MAE**: `0.008 to 0.025` (well below the `0.085` ceiling)

Meanwhile, common attacks fail decisively:
- Swapping in a different shot or shifted camera angle drops SSIM below `0.45` and PSNR below `14 dB`.
- Substituting a blank frame, solid color, or watermarked banner drops SSIM below `0.35` and histogram correlation below `0.40`.
- Altering lighting preset (e.g., rendering noon instead of blue hour) pushes MAE above `0.18` and fails histogram correlation.

## Threat Model and Remaining Gaps

- **Sub-pixel watermarks or faint text**: A tiny text overlay occupying less than 1% of frame area might not drop whole-frame SSIM below `0.78`. Adding a **worst-tile SSIM floor** (the minimum SSIM across any `32 x 32` region in `tools/phase2/verify-frames.mjs`, default `>= 0.50`) catches localized tampering and injected logos without false positives on denoiser variance.
- **Selective frame tampering**: If a contributor tampers with only 1 of 96 frames, a `K = 3` sample catches it with probability `3/96` (~3.1%). Raising `K` to 5 or 6 during release assembly — plus running an automated inter-frame temporal continuity check across all 96 submitted frames (flagging any sudden 1-frame spike in frame-to-frame MSE) — closes the single-frame splice attack cheaply without extra Cycles renders.

## Local Tool Usage

The verifier is implemented and tested locally in `tools/phase2/verify-frames.mjs`:

```sh
node tools/phase2/verify-frames.mjs \
  --submitted /path/to/submitted-frames \
  --reference /path/to/reference-frames \
  --seed <sha256-hex> \
  --samples 3
```
