Skip to content

Commit

Permalink
Working on implementing new parser...
Browse files Browse the repository at this point in the history
  • Loading branch information
BTOdell committed Jun 26, 2021
1 parent fa87c17 commit 28016fe
Show file tree
Hide file tree
Showing 19 changed files with 424 additions and 279 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

# Generated JavaScript files
dist/
boxes/**/cjs/**/*.js
boxes/**/cjs/**/*.js.map
boxes/**/cjs/**/*.d.ts
boxes/**/test/**/*.js
boxes/**/test/**/*.js.map
boxes/**/test/**/*.d.ts
packages/**/cjs/**/*.js
packages/**/cjs/**/*.js.map
packages/**/cjs/**/*.d.ts
Expand Down
3 changes: 2 additions & 1 deletion boxes/ftyp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
},
"types": "./dist/ftyp.d.ts",
"dependencies": {
"@isomp4/core": "file:../../packages/core"
"@isomp4/core": "file:../../packages/core",
"buffer": "^6.0.0"
},
"scripts": {
"clean": "shx rm -rf ./dist ./cjs/**/*.js ./cjs/**/*.js.map ./cjs/**/*.d.ts ./test/**/*.js ./test/**/*.js.map ./test/**/*.d.ts ./**/*.tsbuildinfo"
Expand Down
27 changes: 27 additions & 0 deletions boxes/ftyp/src/ftyp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type {Buffer} from "buffer";
import type {Box, BoxEncoding, BoxHeader, FourCC} from "@isomp4/core";
import {AbstractBoxEncoding} from "@isomp4/core";

export interface FileTypeBox extends Box {
readonly majorBrand: FourCC;
readonly minorBrand: FourCC;
readonly compatibleBrands: readonly FourCC[];
}

export const ftyp: BoxEncoding<FileTypeBox> = new class extends AbstractBoxEncoding<FileTypeBox> {

public override readonly type: FourCC = "ftyp";

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

public override encodeTo(obj: FileTypeBox, buf: Buffer): number {
return 0;
}

public override decodeWithHeader(header: BoxHeader, buffer: Buffer): FileTypeBox | number {
return 0;
}

}();
25 changes: 10 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"types": "./dist/core.d.ts",
"dependencies": {
"@isomp4/encoding": "file:../encoding"
"buffer": "^6.0.0"
},
"scripts": {
"clean": "shx rm -rf ./dist ./cjs/**/*.js ./cjs/**/*.js.map ./cjs/**/*.d.ts ./test/**/*.js ./test/**/*.js.map ./test/**/*.d.ts ./**/*.tsbuildinfo"
Expand Down
151 changes: 150 additions & 1 deletion packages/core/src/Box.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,152 @@
export interface Box {
import {Buffer} from "buffer";

/**
* The size (in bytes) of a compact box header in the ISO base media file format.
* This header includes the 32-bit unsigned `size` field and the 32-bit unsigned `type` field.
* @see ISO/IEC 14496-12.
*/
export const BOX_HEADER_SIZE: number = 8;

const MAX_LARGE_SIZE: bigint = BigInt(Number.MAX_SAFE_INTEGER);

/**
* Represents a four-character code.
*/
export type FourCC = string;

/**
* The header fields of a box structure.
*/
export interface BoxHeader {

/**
* The length (in bytes) of just this header.
*/
readonly length: number;

/**
* The number of bytes of the entire box, including header, fields, and children.
*/
readonly size: number;

/**
* The unique four-character code representing the type of the box.
* Common types are: ftyp, moov, moof, mdat
*/
readonly type: FourCC;

/**
* 64-bit extended size of the box.
*/
readonly largesize?: bigint;

/**
* 16-byte UUID for custom user types.
*/
readonly usertype?: Buffer;

}

export interface Box extends BoxHeader {}

/**
* Parses the given buffer into a box header object.
* @param buffer The buffer to read from (starting at offset 0).
* @return A parsed box header object,
* or if there isn't enough data in the buffer, returns the total
* number of bytes needed to read the box header.
*/
export function parseBoxHeader(buffer: Buffer): BoxHeader | number {
let length: number = BOX_HEADER_SIZE;
// 'size' and 'type' are always required
if (buffer.length < length) {
return length;
}
let size: number = buffer.readUInt32BE(0);
const type: FourCC = buffer.toString("binary", 4, 8);
let offset: number = 8;
let largesize: bigint | undefined;
if (size === 0) {
throw new Error("box cannot extend indefinitely");
} else if (size === 1) {
if (buffer.length < (length += 8)) {
return length;
}
largesize = buffer.readBigUInt64BE(offset).valueOf();
if (largesize > MAX_LARGE_SIZE) {
throw new Error("largesize mode is not supported");
}
offset += 8;
// If the largesize can be stored in the normal size, then do so
size = Number(largesize);
} else if (size < length) {
throw new Error("invalid box size: " + size);
} else if (size === length) {
throw new Error("empty box not supported");
}
// Check for user-defined type
let usertype: Buffer | undefined;
if (type === "uuid") {
if (buffer.length < (length += 16)) {
return length;
}
usertype = Buffer.from(buffer.slice(offset, offset + 16));
}
return {
length,
size,
type,
largesize,
usertype,
};
}

/**
* The header fields of a full box structure.
*/
export interface FullBoxHeader {

/**
* The length (in bytes) of just this header.
*/
readonly length: number;

/**
* Specifies the version of this format of the box.
*/
readonly version: number;

/**
* A bitfield of custom flags.
*/
readonly flags: number;

}

export interface FullBox extends Box, FullBoxHeader {}

/**
* Parses the given buffer into a full box header object.
* @param buffer The buffer to read from (starting at offset 0).
* @return A parsed full box header object,
* or if there isn't enough data in the buffer, returns the total
* number of bytes needed to read the full box header.
*/
export function parseFullBoxHeader(buffer: Buffer): FullBoxHeader | number {
if (buffer.length < 4) {
return 4;
}
const version: number = buffer.readUInt8(0);
const flags: number = buffer.readUIntBE(1, 3);
return {
length: 4,
version,
flags,
};
}

export interface BoxContainer {
children: {
[type: string]: Box | Box[],
};
}
Loading

0 comments on commit 28016fe

Please sign in to comment.