-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working on implementing new parser...
- Loading branch information
Showing
19 changed files
with
424 additions
and
279 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
}(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[], | ||
}; | ||
} |
Oops, something went wrong.