Rate control: rebuilt per-frame, wired in, and gated at zero drift
FINDINGS 26 stopped the session-5 rate controller before it shipped: it built a lam-ladder of independent whole-sequence encodes and picked frames off it, so SKIP blocks referenced reconstructions the decoder never saw -- 111 of 120 frames drifted. The fix is the structural one 26.1 said it had to be. vq_hybrid is now frame-drivable -- frame_ctx / decide / paint -- and encode() is a thin loop over it. Rate control drives the same three calls, bisects lam per frame under the leaky bucket, and feeds back the frame it actually emitted. The desync has no way to occur, and 09_ratectl_drift.py goes 111/120 -> 0/120. That test is now part of check.sh, which is ~2 min rather than ~40 s. Both overshoots on the worst sustained window are closed for under 1 dB, totals including audio: sasi 137.4 -> 109.5 KB/s (-0.60 dB), scsi 381.6 -> 280.0 KB/s (-0.91 dB). Zero frames hit the lam=800 cliff, so nothing was destroyed to get there. Rate control also makes the display path cheaper -- scsi's median drops 53.6% -> 47.1% -- because raising lam moves blocks to SKIP and V1. Two knobs measured rather than guessed. --rc-floor is worth 0.00 dB on that window and defaults to the profile lam, so rate control cannot regress content that already fits. --prefill defaults to 0 and is documented as a trap: it buys a permission to overshoot of exactly bucket/nframes, and on a 14-frame clip it disables the controller outright. FINDINGS 26.5 was wrong in both halves and 27.6 records it. _paint was not the bottleneck (14% of a frame, though vectorising it was still right at 17.1x) and the ladder was never "minutes" -- those were k-means in build(). What makes per-frame rate control affordable is that VQ.assign depends on neither lam nor prev, so it is cached one frame deep: a 12-step search over 120 frames costs 0.31 s against 49.1 s. Also caught: fixed-lam sasi was already 5% over target on 00020, the clip everyone called easy. Nothing noticed because the profile table quotes PSNR and not bitrate. check.sh: ALL GREEN. Claude-Session: https://claude.ai/code/session_01194oWYW8DQXK1SZ2DnChW6
This commit is contained in:
+56
-9
@@ -3,6 +3,14 @@
|
||||
|
||||
python3 tools/encoder/encode.py <frames_dir> <out.dlx> [--profile sasi|scsi]
|
||||
[--lam N] [--fps 12] [--preview out.png]
|
||||
[--fixed-lam] [--rc-floor profile|open]
|
||||
|
||||
Rate control is ON by default: lam is bisected per frame under a leaky bucket
|
||||
so the profile's bitrate is a ceiling rather than an average hope. `--fixed-lam`
|
||||
restores session 5's behaviour, which overshoots by 18-34% on sustained action
|
||||
(FINDINGS 25.3). `--rc-floor` picks the quality floor: `profile` (default) never
|
||||
spends more than the fixed-lam profile would, so it can only ever help; `open`
|
||||
lets quiet frames spend the whole allowance and lands the mean ON target.
|
||||
|
||||
Container (little-endian is WRONG here -- the 68000 is big-endian, so every
|
||||
multi-byte field is big-endian and the decoder can read it with a plain move.w):
|
||||
@@ -79,6 +87,16 @@ def main():
|
||||
ap.add_argument("--lam", type=float, default=None)
|
||||
ap.add_argument("--fps", type=int, default=12)
|
||||
ap.add_argument("--iters", type=int, default=16)
|
||||
ap.add_argument("--fixed-lam", action="store_true",
|
||||
help="disable rate control (session 5 behaviour)")
|
||||
ap.add_argument("--rc-floor", choices=("profile", "open"), default="profile",
|
||||
help="quality floor for rate control")
|
||||
ap.add_argument("--bucket-frames", type=int, default=8,
|
||||
help="leaky-bucket depth, in frame budgets")
|
||||
ap.add_argument("--prefill", type=float, default=0.0,
|
||||
help="how full the player's buffer is assumed to be at "
|
||||
"scene start, as a fraction of the bucket (0 = cold "
|
||||
"buffer after a seek, the conservative assumption)")
|
||||
ap.add_argument("--preview")
|
||||
a = ap.parse_args()
|
||||
|
||||
@@ -87,26 +105,43 @@ def main():
|
||||
k1, k4 = prof["k1"], prof["k4"]
|
||||
_IDX_BYTES = 1 if max(k1, k4) <= 256 else 2
|
||||
|
||||
# An explicit --lam is a request for that lam, so it implies --fixed-lam.
|
||||
rc = not (a.fixed_lam or a.lam is not None)
|
||||
lam_lo = lam if a.rc_floor == "profile" else 1.0
|
||||
|
||||
print(f"profile {a.profile}: {prof['desc']}")
|
||||
print(f" target {prof['kbps']} KB/s, lam={lam}, k1={k1} k4={k4}, "
|
||||
f"{_IDX_BYTES}-byte indices")
|
||||
if rc:
|
||||
print(f" target {prof['kbps']} KB/s CEILING, rate-controlled: "
|
||||
f"lam bisected per frame in [{lam_lo:g}, {RC.LAM_CLIFF:g}], "
|
||||
f"{a.bucket_frames}-frame bucket")
|
||||
else:
|
||||
print(f" target {prof['kbps']} KB/s, FIXED lam={lam} (no rate control)")
|
||||
print(f" k1={k1} k4={k4}, {_IDX_BYTES}-byte indices")
|
||||
|
||||
m = H.build(a.frames_dir, k1=k1, k4=k4, iters=a.iters)
|
||||
enc = H.encode(m, lam=lam)
|
||||
if rc:
|
||||
enc = RC.encode_rate_controlled(m, prof["kbps"], fps=a.fps,
|
||||
bucket_frames=a.bucket_frames,
|
||||
lam_lo=lam_lo, prefill=a.prefill)
|
||||
else:
|
||||
enc = H.encode(m, lam=lam)
|
||||
r = H.evaluate(m, enc, fps=a.fps)
|
||||
|
||||
H_, W_ = m["H"], m["W"]; nbx = W_ // 4
|
||||
pal, idx = m["pal"], m["idx"]
|
||||
|
||||
# re-derive the per-frame symbols the same way encode() did
|
||||
# The encoder hands back the symbols it actually chose. Re-deriving them
|
||||
# here (as session 5 did) is a second chance to disagree with the encoder,
|
||||
# and with per-frame rate control the mode map is no longer reproducible
|
||||
# from a single lam anyway.
|
||||
frames = []
|
||||
for f, im in enumerate(idx):
|
||||
B1 = H.blocks_of(im, pal, 4, 4); l1 = VQ.assign(B1, m["C1s"])
|
||||
B4 = H.blocks_of(im, pal, 2, 2); l4 = VQ.assign(B4, m["C4s"])
|
||||
q = H._group_2x2_into_4x4(np.arange(len(l4)), W_)
|
||||
l4g = l4[q].reshape(-1, 4)
|
||||
mode = enc["modes"][f]
|
||||
frames.append(pack_modes(mode) + frame_payload(mode, l1, l4g, im, nbx))
|
||||
frames.append(pack_modes(mode)
|
||||
+ frame_payload(mode, enc["l1"][f], enc["l4g"][f], im, nbx))
|
||||
# the rate controller budgets exactly these bytes -- if that ever drifts
|
||||
# from the container, every bitrate figure below is fiction
|
||||
assert len(frames[-1]) == enc["sizes"][f], (f, len(frames[-1]), enc["sizes"][f])
|
||||
|
||||
palette = m["pal"][:256]
|
||||
if len(palette) < 256:
|
||||
@@ -138,6 +173,18 @@ def main():
|
||||
f"loss {r['loss']:.2f} dB")
|
||||
print(f" modes: SKIP {r['skip']:.1f}% V1 {r['v1']:.1f}% "
|
||||
f"V4 {r['v4']:.1f}% RAW {r['raw']:.1f}%")
|
||||
if rc:
|
||||
rr = RC.summarise(m, enc, prof["kbps"], fps=a.fps)
|
||||
lm = enc["lam"]
|
||||
print(f" rate control: per-frame budget {enc['budget']:.0f} B, "
|
||||
f"bucket {enc['cap']:.0f} B ({a.bucket_frames} frames), "
|
||||
f"prefill {100*a.prefill:.0f}%")
|
||||
print(f" lam: min {lm.min():.1f} median {rr['lam_med']:.1f} "
|
||||
f"p90 {rr['lam_p90']:.1f} max {rr['lam_max']:.1f}")
|
||||
print(f" frames over the per-frame budget (banked by the bucket): "
|
||||
f"{rr['over']:.0f}%")
|
||||
print(f" frames that could not fit even at the lam={RC.LAM_CLIFF:g} "
|
||||
f"cliff: {rr['overrun']}/{len(lm)}")
|
||||
|
||||
# PER-FRAME non-SKIP distribution. The mean above cannot answer the
|
||||
# decoder-architecture question (FINDINGS 24.5): decode-direct-to-GVRAM
|
||||
|
||||
Reference in New Issue
Block a user