Files
magpie/internal/h264/bits.go
T
yetone 81798f7793 browser: a recording is an MP4, encoded in magpie (GIF on format: gif)
record_stop now makes an H.264 MP4 by default, from the same frames as
the GIF (a second a frame, the last held three, the first frame's
size). record_stop takes format: "mp4" (default) or "gif"; any other
value is refused and the recording goes on, so the agent can ask again.

internal/h264 is a small encoder with no cgo and no ffmpeg:
Constrained Baseline, CAVLC, deblocking off. The first picture is IDR.
After that a macroblock the same as before is P_Skip, and a changed one
is Intra16x16 (V/H/DC by SAD). Colour is BT.709 limited range, and an
odd size is cropped. The MP4 puts its moov before mdat, so a player can
start before it has the whole file. QP 23.

The image MCP's file route serves through http.ServeContent, so a
player's Range request gets 206. Safari plays an MP4 only when it is
served that way. extOf gives video/mp4 the extension .mp4.

Run (on mote unless named):
- internal/h264 TestDecodersMakeTheSamePicture. ffmpeg n7.1 (local;
  mote has none) decodes 7 cases to the encoder's own reconstruction,
  byte for byte (diff 0): 330x190 q24, 800x522 q20, 64x48 q10,
  48x32 q40, 96x64 q0 (escape codes), 96x64 q51, 18x18 q23.
  TestEncodeKeepsThePicture: PSNR 42-44 dB at q24.
- internal/gateway: TestBrowserMP4, TestBrowserGIF*,
  TestBrowserRecordStopWithNothing, TestBrowserToolListIsSmall
  (4585 bytes vs 12138), TestImageMCP*.
- TestBrowserRecordsAVideo, in a real Chrome for Testing (chromium-1234)
  with the extension: the MP4 has video/mp4 and a .mp4 link, and a Range
  request gets 206. Chrome opens the link and plays it: 800 wide, 5 s,
  a frame read back from a canvas. format gif decodes to 3 frames, and
  format webm is refused.
- All of internal/gateway's Browser and ImageMCP tests on origin/main
  3fc6ee411, green. The 3-frame recording was MP4 24 KB, GIF 26 KB.
- go vet for darwin, linux and windows; gofmt -l clean.

Broken by hand:
- w.Write in place of ServeContent: TestBrowserRecordsAVideo is red
  ("a range: 200").
- avcC's NAL length size changed from 4 to 2: Chrome's playback check is
  red (this change also stops the script waiting more than 10 s).
- Before the fix in this change, a non-symmetric Hadamard gave PSNR
  ~20 dB and an ffmpeg mismatch. Both tests were red.

Not tried: Safari or Firefox playback, a Windows or Linux run of the
real-browser test, or a recording of hundreds of frames for time and
size.
2026-11-09 06:03:39 +08:00

80 lines
1.4 KiB
Go

package h264
// bits writes a bit string, most significant bit first.
type bits struct {
buf []byte
acc uint64
n uint // bits in acc not yet in buf, fewer than 8
}
// u writes v's low n bits, n ≤ 32.
func (w *bits) u(n uint, v uint32) {
if n == 0 {
return
}
w.acc = w.acc<<n | uint64(v)&(1<<n-1)
w.n += n
for w.n >= 8 {
w.n -= 8
w.buf = append(w.buf, byte(w.acc>>w.n))
}
}
func (w *bits) flag(b bool) {
if b {
w.u(1, 1)
} else {
w.u(1, 0)
}
}
// ue writes v as Exp-Golomb.
func (w *bits) ue(v uint32) {
x := uint64(v) + 1
n := uint(0)
for x>>n > 1 {
n++
}
w.u(n, 0)
w.u(n+1, uint32(x))
}
// se writes v as signed Exp-Golomb.
func (w *bits) se(v int32) {
if v > 0 {
w.ue(uint32(2*v - 1))
} else {
w.ue(uint32(-2 * v))
}
}
// trailing ends an RBSP: a one, then zeros to the byte.
func (w *bits) trailing() []byte {
w.u(1, 1)
if w.n > 0 {
w.u(8-w.n, 0)
}
return w.buf
}
// nal is an RBSP as a NAL unit: its header, then the payload with an
// emulation-prevention byte wherever two zeros meet a byte under four.
func nal(refIdc, typ byte, rbsp []byte) []byte {
out := make([]byte, 0, len(rbsp)+len(rbsp)/64+2)
out = append(out, refIdc<<5|typ)
zeros := 0
for _, b := range rbsp {
if zeros >= 2 && b <= 3 {
out = append(out, 3)
zeros = 0
}
out = append(out, b)
if b == 0 {
zeros++
} else {
zeros = 0
}
}
return out
}