Skip to content

Commit

Permalink
Implemented mfhd and tfhd boxes in the box-moof package.
Browse files Browse the repository at this point in the history
  • Loading branch information
BTOdell committed Jul 23, 2021
1 parent b0ee75b commit f125099
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 26 deletions.
50 changes: 50 additions & 0 deletions boxes/moof/src/mfhd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type {Buffer} from "buffer";
import type {BoxHeader, FullBox} from "@isomp4/core";
import {FullBoxEncoding} from "@isomp4/core";

export interface MovieFragmentHeaderBox extends FullBox {
sequenceNumber: number;
}

class MovieFragmentHeaderBoxEncoding extends FullBoxEncoding {

constructor() {
super("mfhd");
}

public override encodingLength(obj: MovieFragmentHeaderBox): number {
return super.encodingLength(obj); // TODO implement
}

public override encodeTo(obj: MovieFragmentHeaderBox, buffer: Buffer): number {
const requiredBytes = super.encodeTo(obj, buffer);
if (requiredBytes > 0) {
return requiredBytes;
}
// let offset = this.encodedBytes;
// // TODO implementing encoding
// this.encodedBytes = offset;
return 0;
}

public override decode(buffer: Buffer, header?: BoxHeader): MovieFragmentHeaderBox | number {
const superBox = super.decode(buffer, header);
if (typeof superBox === "number") {
return superBox;
}
let offset: number = this.decodedBytes;
if (buffer.length < offset + 4) {
return offset + 4;
}
const sequenceNumber: number = buffer.readUInt32BE(offset);
offset += 4;
this.decodedBytes = offset;
return {
...superBox,
sequenceNumber,
};
}

}

export const mfhd = new MovieFragmentHeaderBoxEncoding();
32 changes: 6 additions & 26 deletions boxes/moof/src/moof.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
import type {Buffer} from "buffer";
import type {Box, BoxHeader} from "@isomp4/core";
import {BoxEncoding} from "@isomp4/core";
import {mfhd} from "./mfhd.js";
import {tfhd} from "./tfhd.js";

export interface MovieFragmentBox extends Box {
}
export const traf = new BoxEncoding("traf", tfhd);
export const moof = new BoxEncoding("moof", mfhd, traf);

class MovieFragmentBoxEncoding extends BoxEncoding {

constructor() {
super("moof");
}

public override encodingLength(obj: MovieFragmentBox): number {
return 0;
}

public override encodeTo(obj: MovieFragmentBox, buf: Buffer): number {
// TODO
throw "implement";
}

public override decode(buffer: Buffer, header?: BoxHeader): MovieFragmentBox | number {
return 0;
}

}

export const moof = new MovieFragmentBoxEncoding();
export * from "./mfhd.js";
export * from "./tfhd.js";
128 changes: 128 additions & 0 deletions boxes/moof/src/tfhd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import type {Buffer} from "buffer";
import type {BoxHeader, FullBox} from "@isomp4/core";
import {FullBoxEncoding, readSafeUInt64BE} from "@isomp4/core";

export interface TrackFragmentHeaderBox extends FullBox {
trackID: number;
// Optional based on flags
baseDataOffset?: number;
sampleDescriptionIndex?: number;
defaultSampleDuration?: number;
defaultSampleSize?: number;
defaultSampleFlags?: number;
}

export namespace TrackFragmentHeaderBox {

export interface Flags {
baseDataOffsetPresent: boolean;
sampleDescriptionIndexPresent: boolean;
defaultSampleDurationPresent: boolean;
defaultSampleSizePresent: boolean;
defaultSampleFlagsPresent: boolean;
durationIsEmpty: boolean;
defaultBaseIsMoof: boolean;
}

export function parseFlags(flags: number): Flags {
return {
baseDataOffsetPresent: (flags & 0x000001) !== 0,
sampleDescriptionIndexPresent: (flags & 0x000002) !== 0,
defaultSampleDurationPresent: (flags & 0x000008) !== 0,
defaultSampleSizePresent: (flags & 0x000010) !== 0,
defaultSampleFlagsPresent: (flags & 0x000020) !== 0,
durationIsEmpty: (flags & 0x010000) !== 0,
defaultBaseIsMoof: (flags & 0x020000) !== 0,
};
}

}

class TrackFragmentHeaderBoxEncoding extends FullBoxEncoding {

constructor() {
super("tfhd");
}

public override encodingLength(obj: TrackFragmentHeaderBox): number {
return super.encodingLength(obj); // TODO implement
}

public override encodeTo(obj: TrackFragmentHeaderBox, buffer: Buffer): number {
const requiredBytes = super.encodeTo(obj, buffer);
if (requiredBytes > 0) {
return requiredBytes;
}
// let offset = this.encodedBytes;
// // TODO implementing encoding
// this.encodedBytes = offset;
return 0;
}

public override decode(buffer: Buffer, header?: BoxHeader): TrackFragmentHeaderBox | number {
const superBox = super.decode(buffer, header);
if (typeof superBox === "number") {
return superBox;
}
let offset: number = this.decodedBytes;
if (buffer.length < offset + 4) {
return offset + 4;
}
const trackID: number = buffer.readUInt32BE(offset);
offset += 4;
const flags: TrackFragmentHeaderBox.Flags = TrackFragmentHeaderBox.parseFlags(superBox.flags);
let baseDataOffset: number | undefined;
if (flags.baseDataOffsetPresent) {
if (buffer.length < offset + 8) {
return offset + 8;
}
baseDataOffset = readSafeUInt64BE(buffer, offset);
offset += 8;
}
let sampleDescriptionIndex: number | undefined;
if (flags.sampleDescriptionIndexPresent) {
if (buffer.length < offset + 4) {
return offset + 4;
}
sampleDescriptionIndex = buffer.readUInt32BE(offset);
offset += 4;
}
let defaultSampleDuration: number | undefined;
if (flags.defaultSampleDurationPresent) {
if (buffer.length < offset + 4) {
return offset + 4;
}
defaultSampleDuration = buffer.readUInt32BE(offset);
offset += 4;
}
let defaultSampleSize: number | undefined;
if (flags.defaultSampleSizePresent) {
if (buffer.length < offset + 4) {
return offset + 4;
}
defaultSampleSize = buffer.readUInt32BE(offset);
offset += 4;
}
let defaultSampleFlags: number | undefined;
if (flags.defaultSampleFlagsPresent) {
if (buffer.length < offset + 4) {
return offset + 4;
}
defaultSampleFlags = buffer.readUInt32BE(offset);
offset += 4;
}
this.decodedBytes = offset;
return {
...superBox,
trackID,
baseDataOffset,
sampleDescriptionIndex,
defaultSampleDuration,
defaultSampleSize,
defaultSampleFlags,
};
}

}

export const tfhd = new TrackFragmentHeaderBoxEncoding();

0 comments on commit f125099

Please sign in to comment.