Open History to see the last commit
1const MAX_CONFIG_BYTES = 64 * 1024;
2const MAX_SPS_BYTES = 4096;
3
4const unsignedGolomb = (value: number) => {
5 const bits = (value + 1).toString(2);
6 return "0".repeat(bits.length - 1) + bits;
7};
8
9function withReorderLimits(sps: Uint8Array): Uint8Array {
10 if (sps.length < 4 || sps.length > MAX_SPS_BYTES || (sps[0]! & 0x9f) !== 7) return sps;
11 const rbsp: number[] = [];
12 let zeros = 0;
13 for (let index = 1; index < sps.length; index++) {
14 const byte = sps[index]!;
15 if (zeros >= 2 && byte === 3) {
16 if (index + 1 >= sps.length || sps[index + 1]! > 3) return sps;
17 zeros = 0;
18 continue;
19 }
20 rbsp.push(byte);
21 zeros = byte === 0 ? zeros + 1 : 0;
22 }
23 const bits = rbsp.map((byte) => byte.toString(2).padStart(8, "0")).join("");
24 let position = 0;
25 const read = (count: number) => {
26 if (position + count > bits.length) throw new Error("Truncated H.264 SPS");
27 const value = Number.parseInt(bits.slice(position, position + count), 2);
28 position += count;
29 return value;
30 };
31 const readUnsignedGolomb = () => {
32 let leadingZeros = 0;
33 while (read(1) === 0) {
34 if (++leadingZeros > 31) throw new Error("Invalid H.264 Exp-Golomb value");
35 }
36 return 2 ** leadingZeros - 1 + (leadingZeros === 0 ? 0 : read(leadingZeros));
37 };
38
39 try {
40 // Only handle the hub's 8-bit 4:2:0 High profile without scaling lists.
41 // Unknown encoder configurations pass through rather than guessing their layout.
42 if (read(8) !== 100) return sps;
43 read(16);
44 if (readUnsignedGolomb() > 31 || readUnsignedGolomb() !== 1) return sps;
45 if (readUnsignedGolomb() !== 0 || readUnsignedGolomb() !== 0) return sps;
46 read(1);
47 if (read(1) !== 0) return sps;
48 if (readUnsignedGolomb() > 12) return sps;
49 const pictureOrderCountType = readUnsignedGolomb();
50 if (pictureOrderCountType === 0) {
51 if (readUnsignedGolomb() > 12) return sps;
52 } else if (pictureOrderCountType === 1) {
53 read(1);
54 readUnsignedGolomb();
55 readUnsignedGolomb();
56 const cycleLength = readUnsignedGolomb();
57 if (cycleLength > 255) return sps;
58 for (let index = 0; index < cycleLength; index++) readUnsignedGolomb();
59 } else if (pictureOrderCountType !== 2) return sps;
60 const maxReferenceFrames = readUnsignedGolomb();
61 if (maxReferenceFrames > 16) return sps;
62 read(1);
63 if (readUnsignedGolomb() > 65535 || readUnsignedGolomb() > 65535) return sps;
64 if (read(1) === 0) read(1);
65 read(1);
66 if (read(1) !== 0) {
67 for (let index = 0; index < 4; index++) readUnsignedGolomb();
68 }
69 const vuiPosition = position;
70 if (read(1) !== 0) return sps;
71 // With no VUI, only rbsp_stop_one_bit and byte padding may follow.
72 if (!/^10{0,7}$/.test(bits.slice(position))) return sps;
73
74 // serve-sim disables reordering, but its fallback encoder omits VUI, so Chromium
75 // infers a large queue despite optimizeForLatency. Add VUI with zero reorder frames.
76 // Flags omit eight optional sections and enable bitstream restrictions
77 // and motion_vectors_over_pic_boundaries_flag.
78 const restricted =
79 bits.slice(0, vuiPosition) +
80 "10000000011" +
81 [2, 1, 16, 16, 0, maxReferenceFrames].map(unsignedGolomb).join("") +
82 "1";
83 const padded = restricted.padEnd(Math.ceil(restricted.length / 8) * 8, "0");
84 const output = [sps[0]!];
85 zeros = 0;
86 for (let index = 0; index < padded.length; index += 8) {
87 const byte = Number.parseInt(padded.slice(index, index + 8), 2);
88 if (zeros >= 2 && byte <= 3) {
89 output.push(3);
90 zeros = 0;
91 }
92 output.push(byte);
93 zeros = byte === 0 ? zeros + 1 : 0;
94 }
95 return Uint8Array.from(output);
96 } catch {
97 return sps;
98 }
99}
100
101/** Repair missing reorder metadata only for serve-sim's non-reordering AVCC feed. */
102export function simulatorAvcConfig(description: Uint8Array): Uint8Array {
103 if (description.length < 7 || description.length > MAX_CONFIG_BYTES || description[0] !== 1)
104 return description;
105 const parameterSets = description[5]! & 0x1f;
106 if (parameterSets === 0) return description;
107 const parts: Uint8Array[] = [description.subarray(0, 6)];
108 let position = 6;
109 let changed = false;
110 for (let index = 0; index < parameterSets; index++) {
111 if (position + 2 > description.length) return description;
112 const length = description[position]! * 256 + description[position + 1]!;
113 position += 2;
114 if (length === 0 || position + length > description.length) return description;
115 const sps = description.subarray(position, position + length);
116 const repaired = withReorderLimits(sps);
117 changed ||= repaired !== sps;
118 parts.push(Uint8Array.of(repaired.length >>> 8, repaired.length & 0xff), repaired);
119 position += length;
120 }
121 const suffix = position;
122 if (position >= description.length) return description;
123 const pictureParameterSets = description[position++]!;
124 for (let index = 0; index < pictureParameterSets; index++) {
125 if (position + 2 > description.length) return description;
126 const length = description[position]! * 256 + description[position + 1]!;
127 position += 2;
128 if (length === 0 || position + length > description.length) return description;
129 position += length;
130 }
131 if (!changed) return description;
132 parts.push(description.subarray(suffix));
133 const output = new Uint8Array(parts.reduce((length, part) => length + part.length, 0));
134 position = 0;
135 for (const part of parts) {
136 output.set(part, position);
137 position += part.length;
138 }
139 return output;
140}