From 0fd18dfc951d34f753f285b5a9361237b8b2efb7 Mon Sep 17 00:00:00 2001 From: hhang <17309584214@163.com> Date: Mon, 30 Nov 2026 13:27:12 +0800 Subject: [PATCH] fix(artifacts): read Ogg Opus channel mapping tables (#14975) Co-authored-by: hhang <169027046+xu91102@users.noreply.github.com> --- docs/media-asset-headers.md | 11 ++- .../MediaAssetArtifactReader.test.ts | 71 +++++++++++++++++++ src/artifacts/mediaAsset/MediaAssetLayouts.ts | 36 +++++++++- .../mediaAsset/mediaAsset.fixture.ts | 12 ++-- .../formatMetadata/mediaAssetMetadata.ts | 8 +++ 5 files changed, 131 insertions(+), 7 deletions(-) diff --git a/docs/media-asset-headers.md b/docs/media-asset-headers.md index f1a0ccaf59..6bf796069c 100644 --- a/docs/media-asset-headers.md +++ b/docs/media-asset-headers.md @@ -25,7 +25,14 @@ recognized, and inspection refuses it as truncated with the bytes it needs. - **Ogg Opus**: stream serials and page sequences for the examined identification and comment pages; OpusHead channel count, pre-skip, input - sample rate, output gain and mapping family; OpusTags vendor when the + sample rate, output gain and mapping family. For families 1, 2 and 255, + `channel_mapping_table` contains the declared stream count, coupled + stream count and one index per output channel, in file order. Repeated + indices and the silence value 255 are preserved. Family 0 has no stored + table. Family 3 uses a demixing matrix and is not interpreted as an + index map. A short indexed table is reported as a malformed OpusHead + even when another packet follows it on the same page. The reader also + reports the OpusTags vendor when the comment packet is complete and the vendor string fits the fixed 4096-byte root-fact bound. Regions are each examined page and, after a finished comment packet, `audio.bin` for the remaining bytes. OpusTags packets that @@ -107,7 +114,7 @@ declared byte range and is never loaded during inventory. ## CLI ```sh -rea inspect-artifact sample.opus --detail summary --json +rea inspect-artifact sample.opus --detail summary --evidence_output opus-evidence.json --json rea extract-artifact voice.xma --json ``` diff --git a/src/artifacts/mediaAsset/MediaAssetArtifactReader.test.ts b/src/artifacts/mediaAsset/MediaAssetArtifactReader.test.ts index 7db6626adb..61b48163f3 100644 --- a/src/artifacts/mediaAsset/MediaAssetArtifactReader.test.ts +++ b/src/artifacts/mediaAsset/MediaAssetArtifactReader.test.ts @@ -124,6 +124,77 @@ describe("Ogg Opus headers", () => { expect(result.coverage).toEqual([{ status: "complete", limitations: [] }]); }); + it("reports the stream routing declared by a 5.1 OpusHead", async () => { + const packet = opusHeadPacket(6, 1, [4, 2, 0, 4, 1, 2, 3, 5]); + const result = await read( + "ogg-opus", + Buffer.concat([ + oggPage(0, 0n, packet, 2), + oggPage(1, 0n, opusTagsPacket(Buffer.from("enc")), 0), + ]), + ); + expect(result.metadata).toMatchObject({ + channel_count: 6, + mapping_family: 1, + channel_mapping_table: { + stream_count: 4, + coupled_stream_count: 2, + output_channel_map: [0, 4, 1, 2, 3, 5], + }, + }); + }); + + it.each([2, 255])( + "preserves repeated and silent output indices for family %i", + async (family) => { + const packet = opusHeadPacket(4, family, [2, 0, 0, 255, 1, 0]); + const result = await read("ogg-opus", oggPage(0, 0n, packet, 2)); + expect(result.metadata).toMatchObject({ + mapping_family: family, + channel_mapping_table: { + stream_count: 2, + coupled_stream_count: 0, + output_channel_map: [0, 255, 1, 0], + }, + }); + }, + ); + + it.each([0, 3, 240])( + "does not invent indexed routing for family %i", + async (family) => { + const packet = + family === 0 + ? opusHeadPacket() + : opusHeadPacket(1, family, [1, 0, 1, 0]); + const result = await read("ogg-opus", oggPage(0, 0n, packet, 2)); + expect(result.metadata).not.toHaveProperty("channel_mapping_table"); + }, + ); + + it.each([false, true])( + "rejects an incomplete mapping within OpusHead (following packet: %s)", + async (followingPacket) => { + const short = opusHeadPacket(6, 1, [4, 2, 0, 4, 1, 2, 3]); + const page = followingPacket + ? oggPage(0, 0n, Buffer.concat([short, Buffer.from([5])]), 2, { + segmentSizes: [short.length, 1], + }) + : oggPage(0, 0n, short, 2); + const bytes = Buffer.concat([ + page, + oggPage(1, 0n, opusTagsPacket(Buffer.from("enc")), 0), + ]); + await expect(read("ogg-opus", bytes)).rejects.toMatchObject({ + parseFailure: { + kind: "malformed", + format: "ogg-opus", + offset: 27 + page[26]! + short.length, + }, + }); + }, + ); + it("reports a cut-off later page as truncated coverage after OpusHead", async () => { const whole = oggOpusFixture(); // First page is complete; the second page header is cut mid-way. diff --git a/src/artifacts/mediaAsset/MediaAssetLayouts.ts b/src/artifacts/mediaAsset/MediaAssetLayouts.ts index b92ccf1867..3c272b41bd 100644 --- a/src/artifacts/mediaAsset/MediaAssetLayouts.ts +++ b/src/artifacts/mediaAsset/MediaAssetLayouts.ts @@ -128,6 +128,8 @@ const malformed = ( const OPUS_TAGS = Buffer.from("OpusTags", "latin1"); const OPUS_HEAD_BYTES = 19; +// Two stream counts and at most one index per output channel (an octet). +const MAX_INDEXED_OPUS_HEAD_BYTES = OPUS_HEAD_BYTES + 2 + 0xff; const TAGS_PREFIX_LIMIT = 12 + MAX_VENDOR_BYTES; /** RFC 3533 header_type bits. */ @@ -215,6 +217,32 @@ const readOggPage = async ( }; }; +/** RFC 7845 and RFC 8486: only these families store one index per output channel. */ +const opusIndexedRouting = ( + packet: Buffer, + packetOffset: number, + channelCount: number, + mappingFamily: number, +): Pick => { + if (mappingFamily !== 1 && mappingFamily !== 2 && mappingFamily !== 255) + return {}; + const mapStart = OPUS_HEAD_BYTES + 2; + const mapEnd = mapStart + channelCount; + if (packet.length < mapEnd) + throw malformed( + "ogg-opus", + "the OpusHead packet ends before its channel mapping table is complete", + packetOffset + packet.length, + ); + return { + channel_mapping_table: { + stream_count: packet[OPUS_HEAD_BYTES]!, + coupled_stream_count: packet[OPUS_HEAD_BYTES + 1]!, + output_channel_map: [...packet.subarray(mapStart, mapEnd)], + }, + }; +}; + /** Ogg Opus: OpusHead / OpusTags pages and remaining examined pages. */ export const oggOpusLayout = async ( header: Buffer, @@ -264,7 +292,7 @@ export const oggOpusLayout = async ( if (pages.length === 0) { const headBytes = await readRange( page.packetOffset, - Math.min(page.packetLength, OPUS_HEAD_BYTES), + Math.min(page.packetLength, MAX_INDEXED_OPUS_HEAD_BYTES), ); if (headBytes === "truncated") { cutOff = { kind: "truncated", requiredEnd: page.offset + page.length }; @@ -504,6 +532,12 @@ export const oggOpusLayout = async ( input_sample_rate: inputSampleRate, output_gain: outputGain, mapping_family: mappingFamily, + ...opusIndexedRouting( + headPacket, + headPage.packetOffset, + channelCount, + mappingFamily, + ), vendor, pages_read: pages.length, extent_check: extent, diff --git a/src/artifacts/mediaAsset/mediaAsset.fixture.ts b/src/artifacts/mediaAsset/mediaAsset.fixture.ts index 027d60fd86..1dc628d9ef 100644 --- a/src/artifacts/mediaAsset/mediaAsset.fixture.ts +++ b/src/artifacts/mediaAsset/mediaAsset.fixture.ts @@ -70,15 +70,19 @@ export const oggPage = ( return page; }; -/** An OpusHead packet: version 1, stereo, pre-skip 312, 48 kHz, gain 0. */ -export const opusHeadPacket = (): Buffer => +/** OpusHead v1, with a caller-selected raw channel mapping after the fixed fields. */ +export const opusHeadPacket = ( + channelCount = 2, + mappingFamily = 0, + mappingBytes: readonly number[] = [], +): Buffer => Buffer.concat([ Buffer.from("OpusHead", "latin1"), - Buffer.from([1, 2]), + Buffer.from([1, channelCount]), u16(312), u32(48_000), i16(0), - Buffer.from([0]), + Buffer.from([mappingFamily, ...mappingBytes]), ]); /** An OpusTags packet with the given vendor bytes and no comments. */ diff --git a/src/domain/formatMetadata/mediaAssetMetadata.ts b/src/domain/formatMetadata/mediaAssetMetadata.ts index 2526e8ce84..2e03a101f8 100644 --- a/src/domain/formatMetadata/mediaAssetMetadata.ts +++ b/src/domain/formatMetadata/mediaAssetMetadata.ts @@ -20,6 +20,14 @@ export const oggOpusMetadataSchema = z.strictObject({ input_sample_rate: uint32, output_gain: z.number().int().min(-0x8000).max(0x7fff), mapping_family: uint8, + /** Explicit indexed routing for Opus mapping families 1, 2 and 255. */ + channel_mapping_table: z + .strictObject({ + stream_count: uint8, + coupled_stream_count: uint8, + output_channel_map: z.array(uint8).max(255), + }) + .exactOptional(), /** OpusTags vendor string, when that page was examined and valid. */ vendor: z.string().nullable(), pages_read: uint32,