Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(render-farm): bound source MP4 sample tables by their boxes
Richie McIlroy0daed1db7cc3
diff --git a/apps/render-farm/src/mp4.test.ts b/apps/render-farm/src/mp4.test.tsindex 711d70b24..7bcc26915 100644--- a/apps/render-farm/src/mp4.test.ts+++ b/apps/render-farm/src/mp4.test.ts@@ -6,7 +6,13 @@ import {u32,u64,} from "./boxes.test-util";-import { avcC, buildHeader, byteRangeFor, type TrackIndex } from "./mp4";+import {+ avcC,+ buildHeader,+ byteRangeFor,+ indexVideoTrack,+ type TrackIndex,+} from "./mp4";function syntheticIndex(samples: number, gop: number): TrackIndex {const times = new Float64Array(samples);@@ -135,3 +141,92 @@ describe("buildHeader", () => {expect(co64 && u64(co64.body, 8)).toBe(header.byteLength);});});++describe("indexVideoTrack", () => {+ const u32s = (...values: number[]) => {+ const bytes = new Uint8Array(values.length * 4);+ const view = new DataView(bytes.buffer);+ for (const [index, value] of values.entries()) {+ view.setUint32(index * 4, value);+ }+ return bytes;+ };+ const box = (type: string, ...payloads: Uint8Array[]) => {+ const size = 8 + payloads.reduce((sum, part) => sum + part.byteLength, 0);+ const bytes = new Uint8Array(size);+ new DataView(bytes.buffer).setUint32(0, size);+ bytes.set(new TextEncoder().encode(type), 4);+ let offset = 8;+ for (const part of payloads) {+ bytes.set(part, offset);+ offset += part.byteLength;+ }+ return bytes;+ };+ const track = (tables: Partial<Record<string, Uint8Array>>) =>+ box(+ "moov",+ box(+ "trak",+ box(+ "mdia",+ box("mdhd", u32s(0, 0, 0, 30, 0, 0)),+ box("hdlr", u32s(0, 0), new TextEncoder().encode("vide")),+ box(+ "minf",+ box(+ "stbl",+ ...Object.entries({+ stsz: box("stsz", u32s(0, 0, 3, 10, 20, 30)),+ stts: box("stts", u32s(0, 1, 3, 1)),+ stco: box("stco", u32s(0, 1, 1000)),+ stsc: box("stsc", u32s(0, 1, 1, 3, 1)),+ stss: box("stss", u32s(0, 1, 1)),+ ...tables,+ }).map(([, bytes]) => bytes as Uint8Array),+ ),+ ),+ ),+ ),+ );++ test("indexes sizes, offsets, times and keyframes", () => {+ const index = indexVideoTrack(track({}));+ expect([...index.sizes]).toEqual([10, 20, 30]);+ expect([...index.offsets]).toEqual([1000, 1010, 1030]);+ expect([...index.times]).toEqual([0, 1 / 30, 2 / 30]);+ expect([...index.keyframes]).toEqual([0]);+ });++ test.each([+ [+ "a uniform sample size with a huge count",+ "video samples",+ { stsz: box("stsz", u32s(0, 10, 0xffffffff)) },+ ],+ [+ "a sample table shorter than its count",+ "stsz lists more entries",+ { stsz: box("stsz", u32s(0, 0, 1000, 10)) },+ ],+ [+ "a chunk offset table shorter than its count",+ "stco lists more entries",+ { stco: box("stco", u32s(0, 0x7fffffff, 1000)) },+ ],+ [+ "a sync sample table shorter than its count",+ "stss lists more entries",+ { stss: box("stss", u32s(0, 50, 1)) },+ ],+ ])("rejects %s", (_, message, tables) => {+ expect(() => indexVideoTrack(track(tables))).toThrow(message);+ });++ test("a sample-to-chunk run past the offset table ends at the last chunk", () => {+ const index = indexVideoTrack(+ track({ stsc: box("stsc", u32s(0, 2, 1, 1, 1, 0xfffffff0, 1, 1)) }),+ );+ expect(index.offsets[0]).toBe(1000);+ });+});diff --git a/apps/render-farm/src/mp4.ts b/apps/render-farm/src/mp4.tsindex 7be9d3062..481535a8a 100644--- a/apps/render-farm/src/mp4.ts+++ b/apps/render-farm/src/mp4.ts@@ -73,6 +73,10 @@ export type TrackIndex = {keyframes: Uint32Array;};+// Sources are user uploads: a sample count drives several allocations, so it+// is capped (about 23 h at 60 fps) and every table must fit inside its box.+const MAX_SAMPLES = 5_000_000;+/** Index the first video track of a moov box (bytes = the whole moov). */export function indexVideoTrack(moov: Uint8Array): TrackIndex {const root = readBoxes(moov, 0, moov.byteLength).find(@@ -100,26 +104,40 @@ export function indexVideoTrack(moov: Uint8Array): TrackIndex {const timescale = view.getUint32(mdhd.start + mdhd.headerSize + (version === 1 ? 20 : 12),);- const body = (type: string) => {+ // Full-box tables: `countAt` bytes into the body sits the entry count,+ // and `entrySize`-byte entries follow it.+ const table = (type: string, countAt: number, entrySize: number) => {const box = child(moov, stbl, type);- return box ? box.start + box.headerSize : null;+ if (!box) return null;+ const body = box.start + box.headerSize;+ const entries = view.getUint32(body + countAt);+ if (+ countAt + 4 + entries * entrySize > box.size - box.headerSize ||+ entries > MAX_SAMPLES+ ) {+ throw new Error(`${type} lists more entries than it holds`);+ }+ return { body, entries };};- const stsz = body("stsz");- if (stsz === null) throw new Error("no stsz");+ const stszBox = child(moov, stbl, "stsz");+ if (!stszBox) throw new Error("no stsz");+ const stsz = stszBox.start + stszBox.headerSize;const uniform = view.getUint32(stsz + 4);const count = view.getUint32(stsz + 8);+ if (count > MAX_SAMPLES) throw new Error(`${count} video samples`);+ if (!uniform) table("stsz", 8, 4);const sizes = new Uint32Array(count);for (let index = 0; index < count; index++) {sizes[index] = uniform || view.getUint32(stsz + 12 + index * 4);}const times = new Float64Array(count);- const stts = body("stts");- if (stts !== null) {+ const sttsTable = table("stts", 4, 8);+ if (sttsTable !== null) {+ const { body: stts, entries } = sttsTable;let sample = 0;let time = 0;- const entries = view.getUint32(stts + 4);for (let entry = 0; entry < entries; entry++) {const sampleCount = view.getUint32(stts + 8 + entry * 8);const delta = view.getUint32(stts + 12 + entry * 8);@@ -131,32 +149,36 @@ export function indexVideoTrack(moov: Uint8Array): TrackIndex {}const chunkOffsets: number[] = [];- const stco = body("stco");- const co64 = body("co64");+ const stco = table("stco", 4, 4);+ const co64 = stco ? null : table("co64", 4, 8);if (stco !== null) {- const entries = view.getUint32(stco + 4);- for (let index = 0; index < entries; index++) {- chunkOffsets.push(view.getUint32(stco + 8 + index * 4));+ for (let index = 0; index < stco.entries; index++) {+ chunkOffsets.push(view.getUint32(stco.body + 8 + index * 4));}} else if (co64 !== null) {- const entries = view.getUint32(co64 + 4);- for (let index = 0; index < entries; index++) {- chunkOffsets.push(Number(view.getBigUint64(co64 + 8 + index * 8)));+ for (let index = 0; index < co64.entries; index++) {+ chunkOffsets.push(Number(view.getBigUint64(co64.body + 8 + index * 8)));}}- const stsc = body("stsc");- if (stsc === null) throw new Error("no stsc");- const stscEntries = view.getUint32(stsc + 4);+ const stscTable = table("stsc", 4, 12);+ if (stscTable === null) throw new Error("no stsc");+ const { body: stsc, entries: stscEntries } = stscTable;const offsets = new Float64Array(count);let sample = 0;for (let entry = 0; entry < stscEntries; entry++) {const firstChunk = view.getUint32(stsc + 8 + entry * 12) - 1;const perChunk = view.getUint32(stsc + 12 + entry * 12);- const lastChunk =+ const lastChunk = Math.min(+ chunkOffsets.length,entry + 1 < stscEntries? view.getUint32(stsc + 8 + (entry + 1) * 12) - 1- : chunkOffsets.length;- for (let chunk = firstChunk; chunk < lastChunk; chunk++) {+ : chunkOffsets.length,+ );+ for (+ let chunk = firstChunk;+ chunk < lastChunk && sample < count;+ chunk+++ ) {let offset = chunkOffsets[chunk] ?? 0;for (let index = 0; index < perChunk && sample < count; index++) {offsets[sample] = offset;@@ -166,12 +188,12 @@ export function indexVideoTrack(moov: Uint8Array): TrackIndex {}}- const stss = body("stss");+ const stssTable = table("stss", 4, 4);let keyframes: Uint32Array;- if (stss === null) {+ if (stssTable === null) {keyframes = Uint32Array.from({ length: count }, (_, index) => index);} else {- const entries = view.getUint32(stss + 4);+ const { body: stss, entries } = stssTable;keyframes = new Uint32Array(entries);for (let index = 0; index < entries; index++) {keyframes[index] = view.getUint32(stss + 8 + index * 4) - 1;