-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdecoder.ts
More file actions
339 lines (274 loc) · 10.4 KB
/
decoder.ts
File metadata and controls
339 lines (274 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import { EncodedPacketSink, Input, ALL_FORMATS, UrlSource } from "mediabunny";
import { assert } from "./utils";
/**
* Keep this many microseconds of video in the buffer
*/
const BUFFER_RANGE = 1_000_000; // microseconds (1 second to each side)
/**
* Decode more frames when the current frame is this close to the boundaries of the buffer
*/
const FORWARD_THRESHOLD_RANGE = 200_000; // microseconds (200ms)
const BACKWARD_THRESHOLD_RANGE = 300_000; // microseconds (300ms)
// Will decode 8 overlapping frames when seeking backwards (hack to avoid flushing)
const BACKWARD_BUFFER_INTERSECT = 8;
export class FrameDecoder {
private encodedChunks: EncodedVideoChunk[] = [];
private frameBuffer: Map<number, VideoFrame> = new Map();
private decoderQueue: Map<number, PromiseWithResolvers<void>> = new Map();
private decoder: VideoDecoder | null = null;
private forwardIndex: number | null = null;
private backwardIndex: number | null = null;
private duration: number = 0;
private onError?: WebCodecsErrorCallback;
private seekQueue: number[] = [];
private seeking: boolean = false;
private currentTimestamp: number = 0;
private loading: boolean = false;
private canvas: OffscreenCanvas | null = null;
private ctx: OffscreenCanvasRenderingContext2D | null = null;
private lastDrawnTimestamp: number | null = null;
public drawFrame(ctx: CanvasRenderingContext2D, width: number, height: number) {
if (!this.canvas || !this.ctx) return;
let minDiff = Infinity;
let bestFrame: VideoFrame | null = null;
for (const [timestamp, frame] of this.frameBuffer) {
const diff = Math.abs(timestamp - this.currentTimestamp);
if (diff < minDiff) {
minDiff = diff;
bestFrame = frame;
}
}
if (bestFrame && bestFrame.timestamp != this.lastDrawnTimestamp) {
this.ctx.drawImage(
bestFrame,
0,
0,
bestFrame.codedWidth,
bestFrame.codedHeight
);
this.lastDrawnTimestamp = bestFrame.timestamp;
}
ctx.drawImage(this.canvas, 0, 0, width, height);
}
/**
* Binary search to find the chunk index closest to the target timestamp
* @param timestamp - The target timestamp in microseconds
*/
private findClosestChunkIndex(timestamp: number): number {
let minDiff = Infinity;
let bestIndex: number = 0;
for (let i = 0; i < this.encodedChunks.length; i++) {
const diff = Math.abs(timestamp - this.encodedChunks[i].timestamp);
if (diff < minDiff) {
minDiff = diff;
bestIndex = i;
}
}
return bestIndex;
}
/**
* Find the nearest key frame at or before the given chunk index
* @param startIndex - The index to chunk to start searching from
*/
private findClosestKeyFrameIndex(timestamp: number): number {
const index = this.findClosestChunkIndex(timestamp);
// Search backwards from startIndex to find the nearest key frame
for (let i = index; i >= 0; i--) {
if (this.encodedChunks[i].type === 'key') {
return i;
}
}
// If no key frame found, start from the beginning
return 0;
}
/**
* Seek to a specific fraction of the video
* @param fraction - The fraction of the video to seek to (0-1)
*/
public seek(fraction: number): void {
if (this.frameBuffer.size === 0) return;
this.seekQueue.push(fraction);
if (!this.seeking) {
this.processQueue();
};
}
private async processQueue(): Promise<void> {
try {
this.seeking = true;
while (this.seekQueue.length > 0) {
// If multiple seeks are queued, skip to the latest one
const fraction = this.seekQueue.length > 3
? this.seekQueue.pop()
: this.seekQueue.shift();
if (fraction === undefined) continue;
// Clear remaining queue if we're processing the latest seek
if (this.seekQueue.length > 3) {
this.seekQueue = [];
}
await this.dequeueSeek(fraction);
}
} finally {
this.seeking = false;
}
}
private async dequeueSeek(fraction: number): Promise<void> {
const nextTimestamp = Math.floor(this.duration * Math.max(0, Math.min(1, fraction)));
const prevTimestamp = this.currentTimestamp;
const timestamps = [...this.frameBuffer.keys(), ...this.decoderQueue.keys()];
const upperBound = Math.max(...timestamps);
const lowerBound = Math.min(...timestamps);
const outOfBounds = nextTimestamp < lowerBound || nextTimestamp > upperBound + BUFFER_RANGE / 2;
const lowerBoundDiff = nextTimestamp - lowerBound;
const upperBoundDiff = upperBound - nextTimestamp;
let startIndex: number | null = null;
let endIndex: number | null = null;
const promises: (Promise<void> | undefined)[] = [];
if (outOfBounds) {
// Case 1: We need a new buffer
endIndex = this.findClosestChunkIndex(nextTimestamp + BUFFER_RANGE);
startIndex = this.findClosestKeyFrameIndex(nextTimestamp - BUFFER_RANGE);
this.decodeChunks(startIndex, endIndex);
if (this.forwardIndex !== null) {
this.forwardIndex = endIndex;
this.backwardIndex = null;
} else if (this.backwardIndex !== null) {
this.backwardIndex = startIndex;
this.forwardIndex = null;
}
// Wait for the desired frame to be decoded
const closestChunkIndex = this.findClosestChunkIndex(nextTimestamp);
const closestChunk = this.encodedChunks[closestChunkIndex];
promises.push(this.decoderQueue.get(closestChunk.timestamp)?.promise);
} else if (
nextTimestamp < prevTimestamp
&& lowerBoundDiff < BACKWARD_THRESHOLD_RANGE
&& lowerBound > 0
) {
// Case 2: We need to decode backwards
this.forwardIndex = null;
startIndex = this.findClosestKeyFrameIndex(nextTimestamp - BUFFER_RANGE);
// Decode up to the first frame in the buffer
if (this.backwardIndex === null) {
endIndex = this.findClosestChunkIndex(upperBound) + BACKWARD_BUFFER_INTERSECT;
} else {
endIndex = this.backwardIndex + BACKWARD_BUFFER_INTERSECT;
}
if (endIndex !== startIndex + BACKWARD_BUFFER_INTERSECT) {
this.decodeChunks(startIndex, endIndex);
this.backwardIndex = startIndex;
// Wait for the key frame to be decoded
promises.push(this.decoderQueue.get(this.encodedChunks[startIndex].timestamp)?.promise);
}
} else if (
nextTimestamp > prevTimestamp
&& upperBoundDiff < FORWARD_THRESHOLD_RANGE
&& upperBound < this.duration
) {
// Case 3: We need to decode forwards
this.backwardIndex = null;
endIndex = this.findClosestChunkIndex(nextTimestamp + BUFFER_RANGE);
// If we're decoding forwards, start from the next chunk
// Otherwise, start from the nearest key frame before the highest bound
if (this.forwardIndex === null) {
startIndex = this.findClosestKeyFrameIndex(lowerBound);
} else {
startIndex = this.forwardIndex + 1;
}
this.decodeChunks(startIndex, endIndex);
this.forwardIndex = endIndex;
const firstChunk = this.encodedChunks[startIndex];
// Wait for the first chunk to be decoded
if (firstChunk && this.decoderQueue.has(firstChunk.timestamp)) {
await this.decoderQueue.get(firstChunk.timestamp)?.promise;
}
if (endIndex === this.encodedChunks.length - 1) {
promises.push(this.decoder?.flush());
}
}
await Promise.all(promises);
this.currentTimestamp = nextTimestamp;
}
private decodeChunks(startIndex: number, endIndex: number) {
for (let i = startIndex; i <= endIndex; i++) {
// clamp the index to the valid range
this.decodeChunkAt(i);
}
}
private decodeChunkAt(index: number) {
// Clamp the index to the valid range
index = Math.min(Math.max(0, index), this.encodedChunks.length - 1);
const chunk = this.encodedChunks[index];
// add the chunk to the queue
this.decoderQueue.set(chunk.timestamp, Promise.withResolvers<void>());
this.decoder?.decode(chunk);
}
public destroy(): void {
this.frameBuffer.forEach(frame => frame.close());
this.frameBuffer.clear();
this.encodedChunks = [];
this.decoderQueue.clear();
this.decoder?.close();
this.decoder = null;
this.forwardIndex = 0;
this.backwardIndex = null;
this.lastDrawnTimestamp = null;
}
private frameCallback(frame: VideoFrame): void {
this.decoderQueue.get(frame.timestamp)?.resolve();
this.decoderQueue.delete(frame.timestamp);
// Make sure we don't remove frames that could be needed for playback
const lowerBound = this.currentTimestamp - BUFFER_RANGE;
const upperBound = this.currentTimestamp + BUFFER_RANGE;
for (const f of this.frameBuffer.values()) {
// remove frames outside the range
if (f.timestamp <= lowerBound || f.timestamp > upperBound) {
this.frameBuffer.delete(f.timestamp);
f.close();
}
}
if (!this.frameBuffer.has(frame.timestamp)) {
this.frameBuffer.set(frame.timestamp, frame);
} else {
frame.close();
}
}
/**
* Initialize the decoder and demux the video
* @param url - The URL of the video to decode
*/
public async init(url: string) {
if (this.loading) return;
this.loading = true;
const decoder = new VideoDecoder({
output: this.frameCallback.bind(this),
error: this.onError ?? console.error,
});
const input = new Input({
source: new UrlSource(url),
formats: ALL_FORMATS
});
const videoTrack = await input.getPrimaryVideoTrack();
assert(videoTrack, "No video track found");
this.canvas = new OffscreenCanvas(videoTrack.codedWidth, videoTrack.codedHeight);
this.ctx = this.canvas.getContext('2d')!;
this.duration = Math.floor(await videoTrack.computeDuration() * 1_000_000);
const config = await videoTrack.getDecoderConfig();
assert(config, "No decoder config found");
decoder.configure(config);
this.encodedChunks = [];
this.decoder = decoder;
this.forwardIndex = 0;
this.backwardIndex = null;
const sink = new EncodedPacketSink(videoTrack);
for await (const packet of sink.packets()) {
const chunk = packet.toEncodedVideoChunk();
this.encodedChunks.push(chunk);
// Decode initial frames for fast painting
if (chunk.timestamp <= BUFFER_RANGE) {
this.decodeChunkAt(this.forwardIndex);
this.forwardIndex++;
}
}
this.loading = false;
}
}