Files
magpie/internal/h264/tables.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

197 lines
5.3 KiB
Go

package h264
// The CAVLC code tables of ITU-T H.264 §9.2 (Tables 9-5, 9-7, 9-8, 9-9 and
// 9-10), as code lengths and code values.
// coeffTokenLen and coeffTokenBits are coeff_token for 0≤nC<2, 2≤nC<4,
// 4≤nC<8 and 8≤nC, at [TotalCoeff*4+TrailingOnes].
var coeffTokenLen = [4][4 * 17]uint8{
{
1, 0, 0, 0,
6, 2, 0, 0, 8, 6, 3, 0, 9, 8, 7, 5, 10, 9, 8, 6,
11, 10, 9, 7, 13, 11, 10, 8, 13, 13, 11, 9, 13, 13, 13, 10,
14, 14, 13, 11, 14, 14, 14, 13, 15, 15, 14, 14, 15, 15, 15, 14,
16, 15, 15, 15, 16, 16, 16, 15, 16, 16, 16, 16, 16, 16, 16, 16,
},
{
2, 0, 0, 0,
6, 2, 0, 0, 6, 5, 3, 0, 7, 6, 6, 4, 8, 6, 6, 4,
8, 7, 7, 5, 9, 8, 8, 6, 11, 9, 9, 6, 11, 11, 11, 7,
12, 11, 11, 9, 12, 12, 12, 11, 12, 12, 12, 11, 13, 13, 13, 12,
13, 13, 13, 13, 13, 14, 13, 13, 14, 14, 14, 13, 14, 14, 14, 14,
},
{
4, 0, 0, 0,
6, 4, 0, 0, 6, 5, 4, 0, 6, 5, 5, 4, 7, 5, 5, 4,
7, 5, 5, 4, 7, 6, 6, 4, 7, 6, 6, 4, 8, 7, 7, 5,
8, 8, 7, 6, 9, 8, 8, 7, 9, 9, 8, 8, 9, 9, 9, 8,
10, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
},
{
6, 0, 0, 0,
6, 6, 0, 0, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
},
}
var coeffTokenBits = [4][4 * 17]uint8{
{
1, 0, 0, 0,
5, 1, 0, 0, 7, 4, 1, 0, 7, 6, 5, 3, 7, 6, 5, 3,
7, 6, 5, 4, 15, 6, 5, 4, 11, 14, 5, 4, 8, 10, 13, 4,
15, 14, 9, 4, 11, 10, 13, 12, 15, 14, 9, 12, 11, 10, 13, 8,
15, 1, 9, 12, 11, 14, 13, 8, 7, 10, 9, 12, 4, 6, 5, 8,
},
{
3, 0, 0, 0,
11, 2, 0, 0, 7, 7, 3, 0, 7, 10, 9, 5, 7, 6, 5, 4,
4, 6, 5, 6, 7, 6, 5, 8, 15, 6, 5, 4, 11, 14, 13, 4,
15, 10, 9, 4, 11, 14, 13, 12, 8, 10, 9, 8, 15, 14, 13, 12,
11, 10, 9, 12, 7, 11, 6, 8, 9, 8, 10, 1, 7, 6, 5, 4,
},
{
15, 0, 0, 0,
15, 14, 0, 0, 11, 15, 13, 0, 8, 12, 14, 12, 15, 10, 11, 11,
11, 8, 9, 10, 9, 14, 13, 9, 8, 10, 9, 8, 15, 14, 13, 13,
11, 14, 10, 12, 15, 10, 13, 12, 11, 14, 9, 12, 8, 10, 13, 8,
13, 7, 9, 12, 9, 12, 11, 10, 5, 8, 7, 6, 1, 4, 3, 2,
},
{
3, 0, 0, 0,
0, 1, 0, 0, 4, 5, 6, 0, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
},
}
// chromaDCTokenLen and chromaDCTokenBits are coeff_token for nC = -1.
var chromaDCTokenLen = [4 * 5]uint8{
2, 0, 0, 0,
6, 1, 0, 0,
6, 6, 3, 0,
6, 7, 7, 6,
6, 8, 8, 7,
}
var chromaDCTokenBits = [4 * 5]uint8{
1, 0, 0, 0,
7, 1, 0, 0,
4, 6, 1, 0,
3, 3, 2, 5,
2, 3, 2, 0,
}
// totalZerosLen and totalZerosBits are total_zeros of a 4x4 block, at
// [TotalCoeff-1][total_zeros].
var totalZerosLen = [15][16]uint8{
{1, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 9},
{3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6},
{4, 3, 3, 3, 4, 4, 3, 3, 4, 5, 5, 6, 5, 6},
{5, 3, 4, 4, 3, 3, 3, 4, 3, 4, 5, 5, 5},
{4, 4, 4, 3, 3, 3, 3, 3, 4, 5, 4, 5},
{6, 5, 3, 3, 3, 3, 3, 3, 4, 3, 6},
{6, 5, 3, 3, 3, 2, 3, 4, 3, 6},
{6, 4, 5, 3, 2, 2, 3, 3, 6},
{6, 6, 4, 2, 2, 3, 2, 5},
{5, 5, 3, 2, 2, 2, 4},
{4, 4, 3, 3, 1, 3},
{4, 4, 2, 1, 3},
{3, 3, 1, 2},
{2, 2, 1},
{1, 1},
}
var totalZerosBits = [15][16]uint8{
{1, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 1},
{7, 6, 5, 4, 3, 5, 4, 3, 2, 3, 2, 3, 2, 1, 0},
{5, 7, 6, 5, 4, 3, 4, 3, 2, 3, 2, 1, 1, 0},
{3, 7, 5, 4, 6, 5, 4, 3, 3, 2, 2, 1, 0},
{5, 4, 3, 7, 6, 5, 4, 3, 2, 1, 1, 0},
{1, 1, 7, 6, 5, 4, 3, 2, 1, 1, 0},
{1, 1, 5, 4, 3, 3, 2, 1, 1, 0},
{1, 1, 1, 3, 3, 2, 2, 1, 0},
{1, 0, 1, 3, 2, 1, 1, 1},
{1, 0, 1, 3, 2, 1, 1},
{0, 1, 1, 2, 1, 3},
{0, 1, 1, 1, 1},
{0, 1, 1, 1},
{0, 1, 1},
{0, 1},
}
// chromaDCZerosLen and chromaDCZerosBits are total_zeros of a chroma DC
// block.
var chromaDCZerosLen = [3][4]uint8{
{1, 2, 3, 3},
{1, 2, 2, 0},
{1, 1, 0, 0},
}
var chromaDCZerosBits = [3][4]uint8{
{1, 1, 1, 0},
{1, 1, 0, 0},
{1, 0, 0, 0},
}
// runLen and runBits are run_before, at [min(zerosLeft,7)-1][run_before].
var runLen = [7][15]uint8{
{1, 1},
{1, 2, 2},
{2, 2, 2, 2},
{2, 2, 2, 3, 3},
{2, 2, 3, 3, 3, 3},
{2, 3, 3, 3, 3, 3, 3},
{3, 3, 3, 3, 3, 3, 3, 4, 5, 6, 7, 8, 9, 10, 11},
}
var runBits = [7][15]uint8{
{1, 0},
{1, 1, 0},
{3, 2, 1, 0},
{3, 2, 1, 1, 0},
{3, 2, 3, 2, 1, 0},
{3, 0, 1, 3, 2, 5, 4},
{7, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1},
}
// zigzag is the 4x4 frame scan: scan position → raster position
// (row*4+column).
var zigzag = [16]int{0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15}
// normAdjust is v of §8.5.9: for qP%6, a position with both coordinates
// even, both odd, or one of each.
var normAdjust = [6][3]int32{
{10, 16, 13}, {11, 18, 14}, {13, 20, 16}, {14, 23, 18}, {16, 25, 20}, {18, 29, 23},
}
// quantMF is the encoder's multiplier, matched to normAdjust.
var quantMF = [6][3]int32{
{13107, 5243, 8066}, {11916, 4660, 7490}, {10082, 4194, 6554},
{9362, 3647, 5825}, {8192, 3355, 5243}, {7282, 2893, 4559},
}
// posClass is a raster position's column in normAdjust and quantMF.
func posClass(pos int) int {
r, c := pos>>2, pos&3
switch {
case r&1 == 0 && c&1 == 0:
return 0
case r&1 == 1 && c&1 == 1:
return 1
}
return 2
}
// chromaQP is QPc for qPI ≥ 30 (Table 8-15).
var chromaQP = [22]int{29, 30, 31, 32, 32, 33, 34, 34, 35, 35, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39, 39}
func qpChroma(qp int) int {
if qp < 30 {
return qp
}
return chromaQP[qp-30]
}