Implement optimized fragmented MP4 demuxing

This commit is contained in:
David Payr
2024-12-14 21:50:51 +01:00
parent 5978110de9
commit 07689fbd8d
14 changed files with 2543 additions and 163 deletions
+1 -1
View File
@@ -989,7 +989,7 @@ export const tfra = (trackData: IsobmffTrackData, trackIndex: number) => {
u32(0b111111), // This specifies that traf number, trun number and sample number are 32-bit ints
u32(trackData.finalizedChunks.length), // Number of entries
trackData.finalizedChunks.map(chunk => [
u64(intoTimescale(chunk.startTimestamp, trackData.timescale)), // Time
u64(intoTimescale(chunk.samples[0]!.timestamp, trackData.timescale)), // Time (in presentation time)
u64(chunk.moofOffset!), // moof offset
u32(trackIndex + 1), // traf number
u32(1), // trun number
File diff suppressed because it is too large Load Diff
+10 -8
View File
@@ -701,20 +701,22 @@ export class IsobmffMuxer extends Muxer {
this.boxWriter.writeBox(movieBox);
}
// Not all tracks need to be present in every fragment
const tracksInFragment = this.trackDatas.filter(x => x.currentChunk);
// Write out an initial moof box; will be overwritten later once actual chunk offsets are known
const moofOffset = this.writer.getPos();
const moofBox = moof(fragmentNumber, this.trackDatas);
const moofBox = moof(fragmentNumber, tracksInFragment);
this.boxWriter.writeBox(moofBox);
// Create the mdat box
{
const mdatBox = mdat(false); // Initially assume no fragment is larger than 4 GiB
const mdatBox = mdat(false); // Initially assume the fragment is not larger than 4 GiB
let totalTrackSampleSize = 0;
// Compute the size of the mdat box
for (const trackData of this.trackDatas) {
assert(trackData.currentChunk);
for (const sample of trackData.currentChunk.samples) {
for (const trackData of tracksInFragment) {
for (const sample of trackData.currentChunk!.samples) {
totalTrackSampleSize += sample.size;
}
}
@@ -731,7 +733,7 @@ export class IsobmffMuxer extends Muxer {
}
// Write sample data
for (const trackData of this.trackDatas) {
for (const trackData of tracksInFragment) {
trackData.currentChunk!.offset = this.writer.getPos();
trackData.currentChunk!.moofOffset = moofOffset;
@@ -744,11 +746,11 @@ export class IsobmffMuxer extends Muxer {
// Now that we set the actual chunk offsets, fix the moof box
const endPos = this.writer.getPos();
this.writer.seek(this.boxWriter.offsets.get(moofBox)!);
const newMoofBox = moof(fragmentNumber, this.trackDatas);
const newMoofBox = moof(fragmentNumber, tracksInFragment);
this.boxWriter.writeBox(newMoofBox);
this.writer.seek(endPos);
for (const trackData of this.trackDatas) {
for (const trackData of tracksInFragment) {
trackData.finalizedChunks.push(trackData.currentChunk!);
this.finalizedChunks.push(trackData.currentChunk!);
trackData.currentChunk = null;