Add Matroska resync logic for malformed files, add RVE sponsor

This commit is contained in:
Vanilagy
2025-08-20 13:38:54 +02:00
parent b265826b7e
commit 5782219941
9 changed files with 104 additions and 19 deletions
+23 -2
View File
@@ -122,9 +122,8 @@ export const LEVEL_0_EBML_IDS: EBMLId[] = [
EBMLId.Segment,
];
// All the stuff that can appear in a segment, basically
export const LEVEL_1_EBML_IDS: EBMLId[] = [
EBMLId.EBMLMaxIDLength,
EBMLId.EBMLMaxSizeLength,
EBMLId.SeekHead,
EBMLId.Info,
EBMLId.Cluster,
@@ -585,6 +584,28 @@ export class EBMLReader {
return null;
}
/** Searches for the next occurrence of an element ID using a naive byte-wise search. */
async resync(ids: EBMLId[], until: number) {
const loadChunkSize = 2 ** 20; // 1 MiB
const idsSet = new Set(ids);
while (this.pos <= until - MIN_HEADER_SIZE) {
if (!this.reader.rangeIsLoaded(this.pos, Math.min(this.pos + MAX_HEADER_SIZE, until))) {
await this.reader.loadRange(this.pos, Math.min(this.pos + loadChunkSize, until));
}
const elementStartPos = this.pos;
const elementId = this.readElementId();
if (elementId !== null && idsSet.has(elementId)) {
return elementStartPos;
}
this.pos = elementStartPos + 1;
}
return null;
}
}
export const CODEC_STRING_MAP: Partial<Record<MediaCodec, string>> = {