"""Per-cell tolerance weights from tracked face regions, and the source pre-adjustment. Both exist for the same reason: the encoder's cost metric is fidelity to the source, and two of the user's complaints are not fidelity problems. Faces lack detail because the budget is spread evenly over a frame while viewers look at one part of it; the picture is dull because the source is dull. Neither can be fixed by a better fit to the source, so both are changes to *what* the encoder is asked to fit. ROI: every reuse decision in the encoder is ``error <= base + tolerance``. Scaling the tolerance per cell by a weight below 1 inside a face box makes the encoder spend more there (fewer stale tiles kept, more fresh ones), and a weight a little above 1 outside lets the rate controller take the bytes back from the background. The edge of a box is feathered over one cell so there is no step in quality at its border. Saturation: a push about each pixel's own luma, so brightness - the thing the metric weights most - does not move. Chosen by eye from tools/saturation.py's strips: 1.3. """ from __future__ import annotations from pathlib import Path import numpy as np from .vdp import CELLS_X, CELLS_Y, TILE LUMA = np.array([0.299, 0.587, 0.114]) FACE_WEIGHT = 0.45 # tolerance multiplier inside a face box EDGE_WEIGHT = 0.70 # one cell around it BACKGROUND_WEIGHT = 1.12 def set_weights(face: float, edge: float, background: float) -> None: """Override the three weights (a tuning knob for A/B encodes).""" global FACE_WEIGHT, EDGE_WEIGHT, BACKGROUND_WEIGHT FACE_WEIGHT, EDGE_WEIGHT, BACKGROUND_WEIGHT = face, edge, background def cell_weights(boxes: np.ndarray, count: int) -> np.ndarray: """(CELLS_Y * CELLS_X,) tolerance multipliers for one frame's boxes.""" w = np.full((CELLS_Y, CELLS_X), BACKGROUND_WEIGHT) face = np.zeros((CELLS_Y, CELLS_X), bool) for x, y, bw, bh in boxes[:count]: if bw <= 0 or bh <= 0: continue cx0, cy0 = max(0, int(x) // TILE), max(0, int(y) // TILE) cx1 = min(CELLS_X, -(-(int(x) + int(bw)) // TILE)) cy1 = min(CELLS_Y, -(-(int(y) + int(bh)) // TILE)) face[cy0:cy1, cx0:cx1] = True if face.any(): edge = np.zeros_like(face) edge[1:, :] |= face[:-1, :] edge[:-1, :] |= face[1:, :] edge[:, 1:] |= face[:, :-1] edge[:, :-1] |= face[:, 1:] w[edge & ~face] = EDGE_WEIGHT w[face] = FACE_WEIGHT return w.reshape(-1) def load_roi(path: Path, n_frames: int) -> list[np.ndarray | None]: """Per-frame weight arrays from tools/face_track.py's output; None where no face.""" data = np.load(path, allow_pickle=False) boxes, counts = data["boxes"], data["counts"] out: list[np.ndarray | None] = [] for i in range(n_frames): if i < len(counts) and counts[i] > 0: out.append(cell_weights(boxes[i], int(counts[i]))) else: out.append(None) with_face = sum(1 for w in out if w is not None) print(f"ROI: {with_face} of {n_frames} frames have a face region " f"(weights {FACE_WEIGHT} face / {EDGE_WEIGHT} edge / {BACKGROUND_WEIGHT} background)") return out def saturate(rgb: np.ndarray, factor: float) -> np.ndarray: """Push chroma about luma by ``factor``; 1.0 is the identity.""" if factor == 1.0: return rgb x = rgb.astype(np.float64) grey = (x @ LUMA)[..., None] return np.clip(grey + (x - grey) * factor, 0, 255).astype(np.uint8)