Extract insertSorted function

This commit is contained in:
Vanilagy
2025-08-10 19:55:21 +02:00
parent 12df436e15
commit a9cec387d4
4 changed files with 18 additions and 46 deletions
+5 -15
View File
@@ -55,6 +55,7 @@ import {
roundToMultiple,
normalizeRotation,
Bitstream,
insertSorted,
} from '../misc';
import { EncodedPacket, PLACEHOLDER_DATA } from '../packet';
import { Reader } from '../reader';
@@ -1624,12 +1625,7 @@ export class IsobmffDemuxer extends Demuxer {
this.readContiguousBoxes(boxInfo.contentSize);
const insertionIndex = binarySearchLessOrEqual(
this.fragments,
this.currentFragment.moofOffset,
x => x.moofOffset,
);
this.fragments.splice(insertionIndex + 1, 0, this.currentFragment);
insertSorted(this.fragments, this.currentFragment, x => x.moofOffset);
// Compute the byte range of the sample data in this fragment, so we can load the whole fragment at once
for (const [, trackData] of this.currentFragment.trackData) {
@@ -1661,21 +1657,15 @@ export class IsobmffDemuxer extends Demuxer {
if (trackData) {
// We know there is sample data for this track in this fragment, so let's add it to the
// track's fragments:
const insertionIndex = binarySearchLessOrEqual(
this.currentTrack.fragments,
this.currentFragment.moofOffset,
x => x.moofOffset,
);
this.currentTrack.fragments.splice(insertionIndex + 1, 0, this.currentFragment);
insertSorted(this.currentTrack.fragments, this.currentFragment, x => x.moofOffset);
const hasKeyFrame = trackData.firstKeyFrameTimestamp !== null;
if (hasKeyFrame) {
const insertionIndex = binarySearchLessOrEqual(
insertSorted(
this.currentTrack.fragmentsWithKeyFrame,
this.currentFragment.moofOffset,
this.currentFragment,
x => x.moofOffset,
);
this.currentTrack.fragmentsWithKeyFrame.splice(insertionIndex + 1, 0, this.currentFragment);
}
const { currentFragmentState } = this.currentTrack;
+4 -19
View File
@@ -38,6 +38,7 @@ import {
binarySearchLessOrEqual,
COLOR_PRIMARIES_MAP_INVERSE,
findLastIndex,
insertSorted,
isIso639Dash2LanguageCode,
last,
MATRIX_COEFFICIENTS_MAP_INVERSE,
@@ -570,32 +571,16 @@ export class MatroskaDemuxer extends Demuxer {
trackData.endTimestamp = lastBlock.timestamp + lastBlock.duration;
if (track) {
const insertionIndex = binarySearchLessOrEqual(
track.clusters,
cluster.elementStartPos,
x => x.elementStartPos,
);
track.clusters.splice(insertionIndex + 1, 0, cluster);
insertSorted(track.clusters, cluster, x => x.elementStartPos);
const hasKeyFrame = trackData.firstKeyFrameTimestamp !== null;
if (hasKeyFrame) {
const insertionIndex = binarySearchLessOrEqual(
track.clustersWithKeyFrame,
cluster.elementStartPos,
x => x.elementStartPos,
);
track.clustersWithKeyFrame.splice(insertionIndex + 1, 0, cluster);
insertSorted(track.clustersWithKeyFrame, cluster, x => x.elementStartPos);
}
}
}
const insertionIndex = binarySearchLessOrEqual(
segment.clusters,
elementStartPos,
x => x.elementStartPos,
);
segment.clusters.splice(insertionIndex + 1, 0, cluster);
insertSorted(segment.clusters, cluster, x => x.elementStartPos);
this.currentCluster = null;
return cluster;
+3 -12
View File
@@ -17,6 +17,7 @@ import {
CallSerializer,
getInt24,
getUint24,
insertSorted,
isSafari,
last,
mapAsyncGenerator,
@@ -799,12 +800,7 @@ class VideoDecoderWrapper extends DecoderWrapper<VideoSample> {
this.sampleQueue.length = 0;
}
const insertionIndex = binarySearchLessOrEqual(
this.sampleQueue,
sample.timestamp,
x => x.timestamp,
);
this.sampleQueue.splice(insertionIndex + 1, 0, sample);
insertSorted(this.sampleQueue, sample, x => x.timestamp);
} else {
// Assign it the next earliest timestamp from the input. We do this because browsers, by spec, are
// required to emit decoded frames in presentation order *while* retaining the timestamp of their
@@ -858,12 +854,7 @@ class VideoDecoderWrapper extends DecoderWrapper<VideoSample> {
assert(this.decoder);
if (!isSafari()) {
const insertionIndex = binarySearchLessOrEqual(
this.inputTimestamps,
packet.timestamp,
x => x,
);
this.inputTimestamps.splice(insertionIndex + 1, 0, packet.timestamp);
insertSorted(this.inputTimestamps, packet.timestamp, x => x);
}
this.decoder.decode(packet.toEncodedVideoChunk());
+6
View File
@@ -292,6 +292,12 @@ export const binarySearchLessOrEqual = <T>(arr: T[], key: number, valueGetter: (
return ans;
};
/** Assumes the array is already sorted. */
export const insertSorted = <T>(arr: T[], item: T, valueGetter: (x: T) => number) => {
const insertionIndex = binarySearchLessOrEqual(arr, valueGetter(item), valueGetter);
arr.splice(insertionIndex + 1, 0, item); // This even behaves correctly for the -1 case
};
export const promiseWithResolvers = <T = void>() => {
let resolve: (value: T) => void;
let reject: (reason: unknown) => void;