Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added simple prefetch analyzer #6695

Merged
merged 6 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- More information about task progress on tasks page (<https://github.com/opencv/cvat/pull/5723>)
- Prefetching next chunk when user navigates by frames manually (<https://github.com/opencv/cvat/pull/6695>)

### Changed

Expand Down
2 changes: 1 addition & 1 deletion cvat-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cvat-core",
"version": "11.0.0",
"version": "11.0.1",
"description": "Part of Computer Vision Tool which presents an interface for client-side integration",
"main": "src/api.ts",
"scripts": {
Expand Down
64 changes: 62 additions & 2 deletions cvat-core/src/frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const frameDataCache: Record<string, {
latestFrameDecodeRequest: number | null;
latestContextImagesRequest: number | null;
provider: FrameDecoder;
prefetchAnalizer: PrefetchAnalyzer;
decodedBlocksCacheSize: number;
activeChunkRequest: Promise<void> | null;
activeContextRequest: Promise<Record<number, ImageBitmap>> | null;
Expand Down Expand Up @@ -163,6 +164,55 @@ export class FrameData {
}
}

class PrefetchAnalyzer {
#chunkSize: number;
#requestedFrames: number[];

constructor(chunkSize) {
this.#chunkSize = chunkSize;
this.#requestedFrames = [];
}

shouldPrefetchNext(current: number, isPlaying: boolean, isChunkCached: (chunk) => boolean): boolean {
if (isPlaying) {
return true;
}

const currentChunk = Math.floor(current / this.#chunkSize);
const { length } = this.#requestedFrames;
const isIncreasingOrder = this.#requestedFrames
.every((val, index) => index === 0 || val > this.#requestedFrames[index - 1]);
if (
length && (isIncreasingOrder && current > this.#requestedFrames[length - 1]) &&
(current % this.#chunkSize) >= Math.ceil(this.#chunkSize / 2) &&
!isChunkCached(currentChunk + 1)
) {
// is increasing order including the current frame
// if current is in the middle+ of the chunk
// if the next chunk was not cached yet
return true;
}

return false;
}

addRequested(frame: number): void {
// latest requested frame is bubbling (array is unique)
const indexOf = this.#requestedFrames.indexOf(frame);
if (indexOf !== -1) {
this.#requestedFrames.splice(indexOf, 1);
}

this.#requestedFrames.push(frame);

// only half of chunk size is considered in this logic
const limit = Math.ceil(this.#chunkSize / 2);
if (this.#requestedFrames.length > limit) {
this.#requestedFrames.shift();
}
}
}

Object.defineProperty(FrameData.prototype.data, 'implementation', {
value(this: FrameData, onServerRequest) {
return new Promise<{
Expand All @@ -171,7 +221,7 @@ Object.defineProperty(FrameData.prototype.data, 'implementation', {
imageData: ImageBitmap | Blob;
} | Blob>((resolve, reject) => {
const {
provider, chunkSize, stopFrame, decodeForward, forwardStep, decodedBlocksCacheSize,
provider, prefetchAnalizer, chunkSize, stopFrame, decodeForward, forwardStep, decodedBlocksCacheSize,
} = frameDataCache[this.jobID];

const requestId = +_.uniqueId();
Expand All @@ -194,7 +244,13 @@ Object.defineProperty(FrameData.prototype.data, 'implementation', {
}

if (frame) {
if (decodeForward && decodedBlocksCacheSize > 1 && !frameDataCache[this.jobID].activeChunkRequest) {
if (
prefetchAnalizer.shouldPrefetchNext(
this.number,
decodeForward,
(chunk) => provider.isChunkCached(chunk),
) && decodedBlocksCacheSize > 1 && !frameDataCache[this.jobID].activeChunkRequest
) {
const nextChunkNumber = findTheNextNotDecodedChunk(this.number);
const predecodeChunksMax = Math.floor(decodedBlocksCacheSize / 2);
if (nextChunkNumber * chunkSize <= stopFrame &&
Expand Down Expand Up @@ -229,6 +285,7 @@ Object.defineProperty(FrameData.prototype.data, 'implementation', {
renderHeight: this.height,
imageData: frame,
});
prefetchAnalizer.addRequested(this.number);
return;
}

Expand All @@ -249,6 +306,7 @@ Object.defineProperty(FrameData.prototype.data, 'implementation', {
renderHeight: this.height,
imageData: currentFrame,
});
prefetchAnalizer.addRequested(this.number);
return;
}

Expand Down Expand Up @@ -280,6 +338,7 @@ Object.defineProperty(FrameData.prototype.data, 'implementation', {
renderHeight: this.height,
imageData: bitmap,
});
prefetchAnalizer.addRequested(this.number);
}
}, () => {
frameDataCache[this.jobID].activeChunkRequest = null;
Expand Down Expand Up @@ -471,6 +530,7 @@ export async function getFrame(
decodedBlocksCacheSize,
dimension,
),
prefetchAnalizer: new PrefetchAnalyzer(chunkSize),
decodedBlocksCacheSize,
activeChunkRequest: null,
activeContextRequest: null,
Expand Down