From 8b547892aaff5c799c3d7ad853fc792b88ffd42c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 22 Oct 2026 21:27:12 -0700 Subject: [PATCH 1/3] chore(quest): claim quest/m1/libav-polyfill-fork.md Quest-Claim: 18a7e1cc135b194d From 5fee6fbb062763680edb184618dac1911c7ad81f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 22 Oct 2026 21:33:25 -0700 Subject: [PATCH 2/3] fix(hang): drop the libav polyfill's planar audio patches The fork's 0.5.6 converts interleaved samples to f32-planar from the right offset and labels AAC's planar decoder output as planar, so Util.Libav.polyfill() no longer patches AudioData.copyTo or relabels AAC frames. The regression tests stay, plus an explicit frameCount. Co-Authored-By: Claude Opus 5.5 --- js/hang/package.json | 2 +- js/hang/src/util/libav.test.ts | 2 +- js/hang/src/util/libav.ts | 74 +-------------------------------- quest/m1/README.md | 1 - quest/m1/libav-polyfill-fork.md | 19 --------- 5 files changed, 3 insertions(+), 95 deletions(-) delete mode 100644 quest/m1/libav-polyfill-fork.md diff --git a/js/hang/package.json b/js/hang/package.json index 089b2f675be..5a8f31858e2 100644 --- a/js/hang/package.json +++ b/js/hang/package.json @@ -24,7 +24,7 @@ "release": "bun ../common/release.ts" }, "dependencies": { - "@kixelated/libavjs-webcodecs-polyfill": "^0.5.5", + "@kixelated/libavjs-webcodecs-polyfill": "^0.5.6", "@moq/clock": "workspace:^", "@moq/json": "workspace:^", "@moq/libav": "workspace:^", diff --git a/js/hang/src/util/libav.test.ts b/js/hang/src/util/libav.test.ts index 8458db93ba0..340d030a137 100644 --- a/js/hang/src/util/libav.test.ts +++ b/js/hang/src/util/libav.test.ts @@ -114,7 +114,7 @@ test("the polyfill deinterleaves every channel to f32-planar", async () => { } const tail = new Float32Array(2); - audio.copyTo(tail, { planeIndex: 1, format: "f32-planar", frameOffset: 1 }); + audio.copyTo(tail, { planeIndex: 1, format: "f32-planar", frameOffset: 1, frameCount: 2 }); expect(Array.from(tail)).toEqual([1, 2].map((frame) => data[frame * 2 + 1] / scale)); // A view smaller than the copy is refused, even with room in the buffer behind it. const backing = new Float32Array(8); diff --git a/js/hang/src/util/libav.ts b/js/hang/src/util/libav.ts index 0cf5e26227c..3e228991162 100644 --- a/js/hang/src/util/libav.ts +++ b/js/hang/src/util/libav.ts @@ -14,12 +14,11 @@ export async function polyfill(): Promise { console.warn("using libav WebCodecs polyfill; performance may be degraded"); // @moq/libav is libav.js's opus-af variant plus the AAC decoder, which no published variant has. - // I forked libavjs-webcodecs-polyfill to avoid Typescript errors; there's no changes otherwise. + // I forked libavjs-webcodecs-polyfill to fix its TypeScript packaging and planar audio. loading = Promise.all([import("@moq/libav"), import("@kixelated/libavjs-webcodecs-polyfill")]).then( async ([{ default: LibAV }, libav]) => { const native = globalThis.AudioDecoder; await libav.load({ LibAV, polyfill: true }); - fixPlanarCopy(libav.AudioData as unknown as typeof AudioData); if (native) return true; type Config = Parameters["configure"]>[0]; @@ -28,14 +27,7 @@ export async function polyfill(): Promise { aac(config) ? { ...config, codec: { libavjs: { codec: "aac" } } } : config; class AudioDecoder extends libav.AudioDecoder { - #planar = false; - - constructor(init: ConstructorParameters[0]) { - super({ ...init, output: (data) => init.output(this.#planar ? planar(data) : data) }); - } - override configure(config: Config): void { - this.#planar = aac(config); super.configure(route(config)); } @@ -51,67 +43,3 @@ export async function polyfill(): Promise { } return await loading; } - -// libav's AAC decoder always emits planar float, but the polyfill concatenates the planes and labels -// the result interleaved "f32". Relabel it so channels read back from the right plane. -function planar(data: T): T { - if (data.format !== "f32") return data; - const samples = new Float32Array(data.numberOfFrames * data.numberOfChannels); - data.copyTo(samples, { planeIndex: 0 }); - const relabeled = new (data.constructor as typeof AudioData)({ - format: "f32-planar", - sampleRate: data.sampleRate, - numberOfFrames: data.numberOfFrames, - numberOfChannels: data.numberOfChannels, - timestamp: data.timestamp, - data: samples, - }); - data.close(); - return relabeled as T; -} - -// Each interleaved sample format, with the offset and scale that map it to [-1, 1]. -const INTERLEAVED = { - u8: [Uint8Array, 0x80, 0x80], - s16: [Int16Array, 0, 0x8000], - s32: [Int32Array, 0, 0x80000000], - f32: [Float32Array, 0, 1], -} as const; - -// The polyfill converts interleaved samples to "f32-planar" from the wrong offset, so every channel -// after the first reads past its frames (NaN) instead of its own samples. libopus decodes -// interleaved, so stereo Opus lost its right channel. Deinterleave from a same-format copy instead, -// which the polyfill gets right. -function fixPlanarCopy(Data: typeof AudioData): void { - const copyTo = Data.prototype.copyTo; - Data.prototype.copyTo = function (this: AudioData, destination: AllowSharedBufferSource, options) { - const format = this.format ?? ""; - if (options.format !== "f32-planar" || !(format in INTERLEAVED)) return copyTo.call(this, destination, options); - const [View, offset, scale] = INTERLEAVED[format as keyof typeof INTERLEAVED]; - const channels = this.numberOfChannels; - const index = (value: number) => Number.isSafeInteger(value) && value >= 0; - if (!index(options.planeIndex) || options.planeIndex >= channels) { - throw new RangeError(`no plane ${options.planeIndex}`); - } - - const start = options.frameOffset ?? 0; - const count = options.frameCount ?? this.numberOfFrames - start; - if (!index(start) || !index(count) || start >= this.numberOfFrames || start + count > this.numberOfFrames) { - throw new RangeError("frames out of range"); - } - // Bounded by the caller's view, not the buffer behind it. - if (count * Float32Array.BYTES_PER_ELEMENT > destination.byteLength) { - throw new RangeError("destination too small"); - } - const samples = new View(this.numberOfFrames * channels); - copyTo.call(this, samples, { planeIndex: 0 }); - const out = new Float32Array(count); - for (let i = 0; i < count; i++) - out[i] = (samples[(start + i) * channels + options.planeIndex] - offset) / scale; - // Copied as bytes: a destination need not be 4-byte aligned. - const bytes = ArrayBuffer.isView(destination) - ? new Uint8Array(destination.buffer, destination.byteOffset, out.byteLength) - : new Uint8Array(destination, 0, out.byteLength); - bytes.set(new Uint8Array(out.buffer)); - }; -} diff --git a/quest/m1/README.md b/quest/m1/README.md index afb1c5d6779..09d939e047a 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -140,7 +140,6 @@ tracks, so per-group stats stop costing cluster announces. - [Wayland display id](/quest/m1/wayland-display-id.md) - a requested display id off X11 is refused or honored, never silently replaced by a portal pick shared across publications - [CI housekeeping](/quest/m1/ci-housekeeping.md) - validate queue nextest at 24 threads and remove the actionlint queue ignore after its release - [Nightly OBS TSan suite](/quest/m1/obs-test-nightly.md) - Nightly runs `just obs test` so a broken suite is seen -- [libav polyfill fork fix](/quest/m1/libav-polyfill-fork.md) - our polyfill fork copies planar audio correctly and `@moq/hang` drops its patch - [Wire-compat audio](/quest/m1/wire-compat-audio.md) - the wire-compat media lanes decode and check the interop tone across releases - [Interop AAC](/quest/m1/interop-aac.md) - the Rust interop publisher adds an AAC tone and every subscriber checks it beside Opus - [Interop pre-skip](/quest/m1/interop-preskip.md) - the interop tone check anchors phase to timestamps, so a misapplied pre-skip fails diff --git a/quest/m1/libav-polyfill-fork.md b/quest/m1/libav-polyfill-fork.md deleted file mode 100644 index 2f65d9dfeb6..00000000000 --- a/quest/m1/libav-polyfill-fork.md +++ /dev/null @@ -1,19 +0,0 @@ -# [XS] Fix the libav polyfill's planar copy in our fork - -## Goal - -The `@kixelated` fork of libavjs-webcodecs-polyfill converts interleaved -samples to planar from the right offset, a release of it is published, and -`@moq/hang` drops the copy patch that works around it. - -## Plan - -Found by [moq-dev/moq#6284](https://github.com/moq-dev/moq/pull/6284): the -polyfill garbles every channel after the first, so stereo Opus under Node, Bun, -or a browser without `AudioDecoder` lost its right channel. #6284 patches the -conversion in `Util.Libav.polyfill()` with a regression test; keep that test -when the patch goes. - -Decided 2026-10-22 (maintainer): fix it in the fork only; post nothing upstream. - -Public API: none. Wire: none. From 00d63e924230aa271722203ad669c6bc74ea8262 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 23 Oct 2026 09:18:48 -0700 Subject: [PATCH 3/3] chore(hang): lock @kixelated/libavjs-webcodecs-polyfill 0.5.6 The fork's 0.5.6 is published with the copyTo offset fix and the AudioDecoder planar relabel. Co-Authored-By: Claude Opus 5.5 --- bun.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bun.lock b/bun.lock index e9ec091413e..f36b52eae8e 100644 --- a/bun.lock +++ b/bun.lock @@ -166,7 +166,7 @@ "name": "@moq/hang", "version": "0.5.2", "dependencies": { - "@kixelated/libavjs-webcodecs-polyfill": "^0.5.5", + "@kixelated/libavjs-webcodecs-polyfill": "^0.5.6", "@moq/clock": "workspace:^", "@moq/json": "workspace:^", "@moq/libav": "workspace:^", @@ -800,7 +800,7 @@ "@jridgewell/trace-mapping": ["@jridgewell/trace-mapping@0.3.31", "", { "dependencies": { "@jridgewell/resolve-uri": "^3.1.0", "@jridgewell/sourcemap-codec": "^1.4.14" } }, "sha512-zzNR+SdQSDJzc8joaeP8QQoCQr8NuYx2dIIytl1QeBEZHJ9uW6hebsrYgbz8hJwUQao3TWCMtmfV8Nu1twOLAw=="], - "@kixelated/libavjs-webcodecs-polyfill": ["@kixelated/libavjs-webcodecs-polyfill@0.5.5", "", { "dependencies": { "@libav.js/types": "^6.7.7", "@ungap/global-this": "^0.4.4" } }, "sha512-Q1zgnTMMQ2F7IE9ylx3C1XzVbg5vYN18jiDINO5U3kNPBOHdYuUlJsMhtBoqr1M6ocLtoiqdHmLs7tHFgrw5KA=="], + "@kixelated/libavjs-webcodecs-polyfill": ["@kixelated/libavjs-webcodecs-polyfill@0.5.6", "", { "dependencies": { "@libav.js/types": "^6.7.7", "@ungap/global-this": "^0.4.4" } }, "sha512-A6FBsEAT/t0cDDaxo3h5XabiHX2RtycQbWV+MNKUghmctrXU1ygl05i3TTtnSF/eLmCLLO8xBMrXO+eGnpLWFw=="], "@libav.js/types": ["@libav.js/types@6.10.9", "", {}, "sha512-AdngxD/VO8OI6/w7wlJC77kuFcsSvCUuo2RuC6gGeSW2DEvbzgBo2JtFLV6UWZp/tRNZhKm6jS0Tv8gwCKBHLg=="],