Allow for out-of-presentation-order chunks, enabling P and B frames

This commit is contained in:
Vanilagy
2024-11-12 23:24:43 +01:00
parent d01d32f08c
commit a065a5ef12
13 changed files with 682 additions and 427 deletions
+26 -2
View File
@@ -1,7 +1,7 @@
export interface EBMLElement {
id: number,
size?: number,
data: number | string | Uint8Array | EBMLFloat32 | EBMLFloat64 | (EBML | null)[]
data: number | string | Uint8Array | EBMLFloat32 | EBMLFloat64 | EBMLSignedInt | (EBML | null)[]
}
export type EBML = EBMLElement | Uint8Array | (EBML | null)[];
@@ -24,6 +24,15 @@ export class EBMLFloat64 {
}
}
/** Wrapper around a number to be able to differentiate it in the writer. */
export class EBMLSignedInt {
value: number;
constructor(value: number) {
this.value = value;
}
}
/** Defines some of the EBML IDs used by Matroska files. */
export enum EBMLId {
EBML = 0x1a45dfa3,
@@ -83,7 +92,6 @@ export enum EBMLId {
}
export const measureUnsignedInt = (value: number) => {
// Force to 32-bit unsigned integer
if (value < (1 << 8)) {
return 1;
} else if (value < (1 << 16)) {
@@ -99,6 +107,22 @@ export const measureUnsignedInt = (value: number) => {
}
};
export const measureSignedInt = (value: number) => {
if (value >= -(1 << 6) && value < (1 << 6)) {
return 1;
} else if (value >= -(1 << 13) && value < (1 << 13)) {
return 2;
} else if (value >= -(1 << 20) && value < (1 << 20)) {
return 3;
} else if (value >= -(1 << 27) && value < (1 << 27)) {
return 4;
} else if (value >= -(2**34) && value < 2**34) {
return 5;
} else {
return 6;
}
};
export const measureEBMLVarInt = (value: number) => {
if (value < (1 << 7) - 1) {
/** Top bit is set, leaving 7 bits to hold the integer, but we can't store