MPEG-TS demuxer optimizations: faster Annex B segmentation, better NALU iteration

This commit is contained in:
Vanilagy
2026-01-16 13:26:58 +01:00
parent e3ee9d91a9
commit ae37973878
7 changed files with 267 additions and 238 deletions
+5 -3
View File
@@ -8,7 +8,7 @@ import { Mp4OutputFormat } from '../../src/output-format.js';
import { BufferTarget } from '../../src/target.js';
import { Conversion } from '../../src/conversion.js';
import { EncodedPacketSink } from '../../src/media-sink.js';
import { extractAvcNalUnits } from '../../src/codec-data.js';
import { iterateAvcNalUnits } from '../../src/codec-data.js';
const __dirname = new URL('.', import.meta.url).pathname;
@@ -26,7 +26,8 @@ test('Annex B to length-prefixed conversion, MP4', async () => {
const originalFirstPacket = await originalSink.getFirstPacket();
expect([...originalFirstPacket!.data.slice(0, 4)]).toEqual([0, 0, 0, 1]);
const originalNalUnits = extractAvcNalUnits(originalFirstPacket!.data, originalDecoderConfig);
const originalNalUnits = [...iterateAvcNalUnits(originalFirstPacket!.data, originalDecoderConfig)]
.map(loc => originalFirstPacket!.data.subarray(loc.offset, loc.offset + loc.length));
const output = new Output({
format: new Mp4OutputFormat(),
@@ -49,6 +50,7 @@ test('Annex B to length-prefixed conversion, MP4', async () => {
const newFirstPacket = await newSink.getFirstPacket();
expect([...newFirstPacket!.data.slice(0, 4)]).not.toEqual([0, 0, 0, 1]); // Successfully converted
const newNalUnits = extractAvcNalUnits(newFirstPacket!.data, newDecoderConfig);
const newNalUnits = [...iterateAvcNalUnits(newFirstPacket!.data, newDecoderConfig)]
.map(loc => newFirstPacket!.data.subarray(loc.offset, loc.offset + loc.length));
expect(newNalUnits).toEqual(originalNalUnits); // Content is the same though
});
+5 -5
View File
@@ -385,7 +385,7 @@ test('MPEG-TS video key packets', async () => {
test('MPEG-TS audio key packets', async () => {
using input = new Input({
source: new FilePathSource(path.join(__dirname, '../public/193039199_mp4_h264_aac_fhd_7.ts')),
source: new FilePathSource(path.join(__dirname, '../public/0.ts')),
formats: ALL_FORMATS,
});
@@ -513,9 +513,12 @@ test('MPEG-TS with HEVC video', async () => {
const sink = new EncodedPacketSink(videoTrack);
let i = 0;
for await (const packet of sink.packets()) {
expect(packet.data.slice(0, 4)).toEqual(new Uint8Array([0, 0, 0, 1])); // Annex B
expect(packet.duration).toBeCloseTo(0.04166666666);
expect(packet.type).toBe(i > 0 ? 'delta' : 'key');
i++;
}
});
@@ -543,13 +546,10 @@ test('MPEG-TS with MP3 audio', async () => {
const firstPacket = await sink.getFirstPacket();
assert(firstPacket);
expect(firstPacket.data[0]).toBe(0xff); // MP3 sync byte
expect(firstPacket.type).toBe('key');
expect(firstPacket.duration).toBeGreaterThan(0);
let count = 0;
for await (const packet of sink.packets()) {
expect(packet.data[0]).toBe(0xff);
expect(packet.type).toBe('key');
count++;
}