Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions apps/render-farm/src/mp4.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,28 @@ describe("byteRangeFor", () => {

describe("buildHeader", () => {
test("lays out ftyp, moov, padding and a 64-bit mdat header", () => {
const videoSizes = Uint32Array.of(0x01020304, 0x80000000, 300);
const audioSizes = Uint32Array.of(10, 20);
const videoBytes = videoSizes.reduce((sum, size) => sum + size, 0);
const payloadSize = videoBytes + 30;
const header = buildHeader({
width: 1920,
height: 1080,
fps: 30,
video: {
sizes: Uint32Array.from([100, 200, 300]),
sizes: videoSizes,
runs: [{ first: 0, count: 3, offset: 0 }],
keyframes: Uint32Array.from([0]),
avcC: avcC(ANNEX_B_PARAMETER_SETS),
},
audio: {
sizes: Uint32Array.from([10, 10]),
runs: [{ first: 0, count: 2, offset: 600 }],
sizes: audioSizes,
runs: [{ first: 0, count: 2, offset: videoBytes }],
asc: Uint8Array.of(0x11, 0x90),
totalSamples: 1024,
priming: 1024,
},
payloadSize: 620,
payloadSize,
minimumSize: 64 * 1024,
});
expect(header.byteLength).toBe(64 * 1024);
Expand All @@ -128,17 +132,29 @@ describe("buildHeader", () => {
]);
const mdat = boxes[3];
expect(u32(header, mdat?.start ?? 0)).toBe(1);
expect(u64(header, (mdat?.start ?? 0) + 8)).toBe(16 + 620);
expect(u64(header, (mdat?.start ?? 0) + 8)).toBe(16 + payloadSize);
const moov = boxes[1];
if (!moov) throw new Error("no moov");
const traks = parseBoxes(moov.body).filter((box) => box.type === "trak");
expect(traks).toHaveLength(2);
const stbl = child(
child(child(traks[0] ?? moov, "mdia") ?? moov, "minf") ?? moov,
"stbl",
);
const co64 = stbl && child(stbl, "co64");
expect(co64 && u64(co64.body, 8)).toBe(header.byteLength);
for (const [index, sizes] of [videoSizes, audioSizes].entries()) {
const stbl = child(
child(child(traks[index] ?? moov, "mdia") ?? moov, "minf") ?? moov,
"stbl",
);
const stsz = stbl && child(stbl, "stsz");
if (!stsz) throw new Error("no sample sizes");
expect(stsz.body.byteLength).toBe(12 + sizes.length * 4);
expect(u32(stsz.body, 4)).toBe(0);
expect(u32(stsz.body, 8)).toBe(sizes.length);
expect(sizes.map((_, sample) => u32(stsz.body, 12 + sample * 4))).toEqual(
sizes,
);
const co64 = stbl && child(stbl, "co64");
expect(co64 && u64(co64.body, 8)).toBe(
header.byteLength + (index === 0 ? 0 : videoBytes),
);
}
});
});

Expand Down
19 changes: 7 additions & 12 deletions apps/render-farm/src/mp4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,18 +485,13 @@ function sampleTable(
}),
),
);
parts.push(
fullBox(
"stsz",
0,
0,
build((writer) => {
writer.u32(0);
writer.u32(table.sizes.length);
for (const size of table.sizes) writer.u32(size);
}),
),
);
const sampleSizes = new Uint8Array(8 + table.sizes.length * 4);
const sampleSizesView = new DataView(sampleSizes.buffer);
sampleSizesView.setUint32(4, table.sizes.length);
table.sizes.forEach((size, index) => {
sampleSizesView.setUint32(8 + index * 4, size);
});
parts.push(fullBox("stsz", 0, 0, sampleSizes));
parts.push(
fullBox(
"co64",
Expand Down
Loading