-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathutil.ts
More file actions
640 lines (601 loc) · 19.9 KB
/
util.ts
File metadata and controls
640 lines (601 loc) · 19.9 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
import { Fixture, SkipTestCase } from '../../common/framework/fixture.js';
import { getResourcePath } from '../../common/framework/resources.js';
import { keysOf } from '../../common/util/data_tables.js';
import { timeout } from '../../common/util/timeout.js';
import { ErrorWithExtra, raceWithRejectOnTimeout } from '../../common/util/util.js';
import { GPUTest } from '../gpu_test.js';
import { RGBA, srgbToDisplayP3 } from '../util/color_space_conversion.js';
declare global {
interface HTMLMediaElement {
// Add captureStream() support for HTMLMediaElement from
// https://w3c.github.io/mediacapture-fromelement/#dom-htmlmediaelement-capturestream
captureStream(): MediaStream;
}
}
// MAINTENANCE_TODO: Uses raw floats as expectation in external_texture related cases has some diffs.
// Remove this conversion utils and uses raw float data as expectation in external_textrue
// related cases when resolve this.
export function convertToUnorm8(expectation: Readonly<RGBA>): Uint8Array {
const rgba8Unorm = new Uint8ClampedArray(4);
rgba8Unorm[0] = Math.round(expectation.R * 255.0);
rgba8Unorm[1] = Math.round(expectation.G * 255.0);
rgba8Unorm[2] = Math.round(expectation.B * 255.0);
rgba8Unorm[3] = Math.round(expectation.A * 255.0);
return new Uint8Array(rgba8Unorm.buffer);
}
// MAINTENANCE_TODO: Add helper function for BT.601 and BT.709 to remove all magic numbers.
// Expectation values about converting video contents to sRGB color space.
// Source video color space affects expected values.
// The process to calculate these expected pixel values can be found:
// https://github.com/gpuweb/cts/pull/2242#issuecomment-1430382811
// and https://github.com/gpuweb/cts/pull/2242#issuecomment-1463273434
const kBt601PixelValue = {
srgb: {
red: { R: 0.972945567233341, G: 0.141794376683341, B: -0.0209589916711088, A: 1.0 },
green: { R: 0.248234279433399, G: 0.984810378661784, B: -0.0564701319494314, A: 1.0 },
blue: { R: 0.10159735826538, G: 0.135451122863674, B: 1.00262982899724, A: 1.0 },
yellow: { R: 0.995470750775951, G: 0.992742114518355, B: -0.0701036235167653, A: 1.0 },
},
} as const;
const kBt709PixelValue = {
srgb: {
red: { R: 1.0, G: 0.0, B: 0.0, A: 1.0 },
green: { R: 0.0, G: 1.0, B: 0.0, A: 1.0 },
blue: { R: 0.0, G: 0.0, B: 1.0, A: 1.0 },
yellow: { R: 1.0, G: 1.0, B: 0.0, A: 1.0 },
},
} as const;
function makeTable<Table extends { readonly [K: string]: {} }>({
table,
}: {
table: Table;
}): {
readonly [F in keyof Table]: {
readonly [K in keyof Table[F]]: Table[F][K];
};
} {
return Object.fromEntries(
Object.entries(table).map(([k, row]) => [k, { ...row }])
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
) as any;
}
// Video expected pixel value table. Finding expected pixel value
// with video color space and dst color space.
export const kVideoExpectedColors = makeTable({
table: {
bt601: {
'display-p3': {
yellow: srgbToDisplayP3(kBt601PixelValue.srgb.yellow),
red: srgbToDisplayP3(kBt601PixelValue.srgb.red),
blue: srgbToDisplayP3(kBt601PixelValue.srgb.blue),
green: srgbToDisplayP3(kBt601PixelValue.srgb.green),
},
srgb: {
yellow: kBt601PixelValue.srgb.yellow,
red: kBt601PixelValue.srgb.red,
blue: kBt601PixelValue.srgb.blue,
green: kBt601PixelValue.srgb.green,
},
},
bt709: {
'display-p3': {
yellow: srgbToDisplayP3(kBt709PixelValue.srgb.yellow),
red: srgbToDisplayP3(kBt709PixelValue.srgb.red),
blue: srgbToDisplayP3(kBt709PixelValue.srgb.blue),
green: srgbToDisplayP3(kBt709PixelValue.srgb.green),
},
srgb: {
yellow: kBt709PixelValue.srgb.yellow,
red: kBt709PixelValue.srgb.red,
blue: kBt709PixelValue.srgb.blue,
green: kBt709PixelValue.srgb.green,
},
},
},
} as const);
// MAINTENANCE_TODO: Add BT.2020 video in table.
// Video container and codec defines several transform ops to apply to raw decoded frame to display.
// Our test cases covers 'visible rect' and 'rotation'.
// 'visible rect' is associated with the
// video bitstream and should apply to the raw decoded frames before any transformation.
// 'rotation' is associated with the track or presentation and should transform
// the whole visible rect (e.g. 90-degree rotate makes visible rect of vertical video to horizontal)
// The order to apply these transformations is below:
// [raw decoded frame] ----visible rect clipping ---->[visible frame] ---rotation ---> present
// ^ ^
// | |
// coded size display size
// The table holds test videos meta infos, including mimeType to check browser compatibility
// video color space, raw frame content layout and the frame displayed layout.
export const kVideoInfo = makeTable({
table: {
'four-colors-vp8-bt601.webm': {
mimeType: 'video/webm; codecs=vp8',
colorSpace: 'bt601',
coded: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
display: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
},
'four-colors-h264-bt601.mp4': {
mimeType: 'video/mp4; codecs=avc1.4d400c',
colorSpace: 'bt601',
coded: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
display: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
},
'four-colors-vp9-bt601.webm': {
mimeType: 'video/webm; codecs=vp9',
colorSpace: 'bt601',
coded: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
display: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
},
'four-colors-vp9-bt709.webm': {
mimeType: 'video/webm; codecs=vp9',
colorSpace: 'bt709',
coded: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
display: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
},
// video coded content has been rotate
'four-colors-h264-bt601-rotate-90.mp4': {
mimeType: 'video/mp4; codecs=avc1.4d400c',
colorSpace: 'bt601',
coded: {
topLeftColor: 'red',
topRightColor: 'green',
bottomLeftColor: 'yellow',
bottomRightColor: 'blue',
},
display: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
},
'four-colors-h264-bt601-rotate-180.mp4': {
mimeType: 'video/mp4; codecs=avc1.4d400c',
colorSpace: 'bt601',
coded: {
topLeftColor: 'green',
topRightColor: 'blue',
bottomLeftColor: 'red',
bottomRightColor: 'yellow',
},
display: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
},
'four-colors-h264-bt601-rotate-270.mp4': {
mimeType: 'video/mp4; codecs=avc1.4d400c',
colorSpace: 'bt601',
coded: {
topLeftColor: 'blue',
topRightColor: 'yellow',
bottomLeftColor: 'green',
bottomRightColor: 'red',
},
display: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
},
'four-colors-vp9-bt601-rotate-90.mp4': {
mimeType: 'video/mp4; codecs=vp09.00.10.08',
colorSpace: 'bt601',
coded: {
topLeftColor: 'red',
topRightColor: 'green',
bottomLeftColor: 'yellow',
bottomRightColor: 'blue',
},
display: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
},
'four-colors-vp9-bt601-rotate-180.mp4': {
mimeType: 'video/mp4; codecs=vp09.00.10.08',
colorSpace: 'bt601',
coded: {
topLeftColor: 'green',
topRightColor: 'blue',
bottomLeftColor: 'red',
bottomRightColor: 'yellow',
},
display: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
},
'four-colors-vp9-bt601-rotate-270.mp4': {
mimeType: 'video/mp4; codecs=vp09.00.10.08',
colorSpace: 'bt601',
coded: {
topLeftColor: 'blue',
topRightColor: 'yellow',
bottomLeftColor: 'green',
bottomRightColor: 'red',
},
display: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
},
'four-colors-h264-bt601-hflip.mp4': {
mimeType: 'video/mp4; codecs=avc1.4d400c',
colorSpace: 'bt601',
coded: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
display: {
topLeftColor: 'red',
topRightColor: 'yellow',
bottomLeftColor: 'green',
bottomRightColor: 'blue',
},
},
'four-colors-h264-bt601-vflip.mp4': {
mimeType: 'video/mp4; codecs=avc1.4d400c',
colorSpace: 'bt601',
coded: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
display: {
topLeftColor: 'blue',
topRightColor: 'green',
bottomLeftColor: 'yellow',
bottomRightColor: 'red',
},
},
'four-colors-vp9-bt601-hflip.mp4': {
mimeType: 'video/mp4; codecs=vp09.00.10.08',
colorSpace: 'bt601',
coded: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
display: {
topLeftColor: 'red',
topRightColor: 'yellow',
bottomLeftColor: 'green',
bottomRightColor: 'blue',
},
},
'four-colors-vp9-bt601-vflip.mp4': {
mimeType: 'video/mp4; codecs=vp09.00.10.08',
colorSpace: 'bt601',
coded: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
display: {
topLeftColor: 'blue',
topRightColor: 'green',
bottomLeftColor: 'yellow',
bottomRightColor: 'red',
},
},
'four-colors-h264-bt601-scaled-10x1.mp4': {
mimeType: 'video/mp4; codecs=avc1.4d400c',
colorSpace: 'bt601',
coded: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
display: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
},
'four-colors-vp9-bt601-scaled-10x1.mp4': {
mimeType: 'video/webm; codecs=vp9',
colorSpace: 'bt601',
coded: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
display: {
topLeftColor: 'yellow',
topRightColor: 'red',
bottomLeftColor: 'blue',
bottomRightColor: 'green',
},
},
},
} as const);
type VideoName = keyof typeof kVideoInfo;
export const kVideoNames: readonly VideoName[] = keysOf(kVideoInfo);
export const kPredefinedColorSpace = ['display-p3', 'srgb'] as const;
/**
* Starts playing a video and waits for it to be consumable.
* Returns a promise which resolves after `callback` (which may be async) completes.
*
* @param video An HTML5 Video element.
* @param callback Function to call when video is ready.
*
* Adapted from https://github.com/KhronosGroup/WebGL/blob/main/sdk/tests/js/webgl-test-utils.js
*/
export function startPlayingAndWaitForVideo(
video: HTMLVideoElement,
callback: () => unknown | Promise<unknown>
): Promise<void> {
return raceWithRejectOnTimeout(
new Promise((resolve, reject) => {
const callbackAndResolve = () =>
void (async () => {
try {
await callback();
resolve();
} catch (ex) {
reject(ex);
}
})();
if (video.error) {
reject(
new ErrorWithExtra('Video.error: ' + video.error.message, () => ({ error: video.error }))
);
return;
}
video.addEventListener(
'error',
event =>
reject(
new ErrorWithExtra('Video received "error" event, message: ' + event.message, () => ({
event,
}))
),
true
);
if (video.requestVideoFrameCallback) {
video.requestVideoFrameCallback(() => {
callbackAndResolve();
});
} else {
// If requestVideoFrameCallback isn't available, check each frame if the video has advanced.
const timeWatcher = () => {
if (video.currentTime > 0) {
callbackAndResolve();
} else {
requestAnimationFrame(timeWatcher);
}
};
timeWatcher();
}
video.loop = true;
video.muted = true;
video.preload = 'auto';
video.play().catch(reject);
}),
2000,
'Video never became ready'
);
}
/**
* Fire a `callback` when the script animation reaches a new frame.
* Returns a promise which resolves after `callback` (which may be async) completes.
*/
export function waitForNextTask(callback: () => unknown | Promise<unknown>): Promise<void> {
const { promise, callbackAndResolve } = callbackHelper(callback, 'wait for next task timed out');
timeout(() => {
callbackAndResolve();
}, 0);
return promise;
}
/**
* Fire a `callback` when the video reaches a new frame.
* Returns a promise which resolves after `callback` (which may be async) completes.
*
* MAINTENANCE_TODO: Find a way to implement this for browsers without requestVideoFrameCallback as
* well, similar to the timeWatcher path in startPlayingAndWaitForVideo. If that path is proven to
* work well, we can consider getting rid of the requestVideoFrameCallback path.
*/
export function waitForNextFrame(
video: HTMLVideoElement,
callback: () => unknown | Promise<unknown>
): Promise<void> {
const { promise, callbackAndResolve } = callbackHelper(callback, 'waitForNextFrame timed out');
if ('requestVideoFrameCallback' in video) {
video.requestVideoFrameCallback(() => {
callbackAndResolve();
});
} else {
throw new SkipTestCase('waitForNextFrame currently requires requestVideoFrameCallback');
}
return promise;
}
export async function getVideoFrameFromVideoElement(
test: Fixture,
video: HTMLVideoElement
): Promise<VideoFrame> {
if (video.captureStream === undefined) {
test.skip('HTMLVideoElement.captureStream is not supported');
}
return raceWithRejectOnTimeout(
new Promise<VideoFrame>(resolve => {
const videoTrack: MediaStreamVideoTrack = video
.captureStream()
.getVideoTracks()[0] as MediaStreamVideoTrack;
const trackProcessor: MediaStreamTrackProcessor<VideoFrame> = new MediaStreamTrackProcessor({
track: videoTrack,
});
const transformer: TransformStream = new TransformStream({
transform(videoFrame, _controller) {
videoTrack.stop();
test.trackForCleanup(videoFrame);
resolve(videoFrame);
},
flush(controller) {
controller.terminate();
},
});
const trackGenerator: MediaStreamTrackGenerator<VideoFrame> = new MediaStreamTrackGenerator({
kind: 'video',
});
trackProcessor.readable
.pipeThrough(transformer)
.pipeTo(trackGenerator.writable)
.catch(() => {});
}),
2000,
'Video never became ready'
);
}
/**
* Create HTMLVideoElement based on VideoName. Check whether video is playable in current
* browser environment.
* Returns a HTMLVideoElement.
*
* @param t: GPUTest that requires getting HTMLVideoElement
* @param videoName: Required video name
*
*/
export function getVideoElement(t: GPUTest, videoName: VideoName): HTMLVideoElement {
if (typeof HTMLVideoElement === 'undefined') {
t.skip('HTMLVideoElement not available');
}
const videoElement = document.createElement('video');
const videoInfo = kVideoInfo[videoName];
if (videoElement.canPlayType(videoInfo.mimeType) === '') {
t.skip('Video codec is not supported');
}
const videoUrl = getResourcePath(videoName);
videoElement.src = videoUrl;
t.trackForCleanup(videoElement);
return videoElement;
}
/**
* Helper for doing something inside of a (possibly async) callback (directly, not in a following
* microtask), and returning a promise when the callback is done.
* MAINTENANCE_TODO: Use this in startPlayingAndWaitForVideo (and make sure it works).
*/
function callbackHelper(
callback: () => unknown | Promise<unknown>,
timeoutMessage: string
): { promise: Promise<void>; callbackAndResolve: () => void } {
let callbackAndResolve: () => void;
const promiseWithoutTimeout = new Promise<void>((resolve, reject) => {
callbackAndResolve = () =>
void (async () => {
try {
await callback(); // catches both exceptions and rejections
resolve();
} catch (ex) {
reject(ex);
}
})();
});
const promise = raceWithRejectOnTimeout(promiseWithoutTimeout, 2000, timeoutMessage);
return { promise, callbackAndResolve: callbackAndResolve! };
}
/**
* Create VideoFrame from camera captured frame. Check whether browser environment has
* camera supported.
* Returns a webcodec VideoFrame.
*
* @param test: GPUTest that requires getting VideoFrame
*
*/
export async function captureCameraFrame(test: GPUTest): Promise<VideoFrame> {
if (
typeof navigator.mediaDevices === 'undefined' ||
typeof navigator.mediaDevices.getUserMedia === 'undefined'
) {
test.skip("Browser doesn't support capture frame from camera.");
}
const stream = await navigator.mediaDevices.getUserMedia({ video: true });
const track = stream.getVideoTracks()[0] as MediaStreamVideoTrack;
if (!track) {
test.skip("Doesn't have valid camera captured stream for testing.");
}
// Use MediaStreamTrackProcessor and ReadableStream to generate video frame directly.
if (typeof MediaStreamTrackProcessor !== 'undefined') {
const trackProcessor = new MediaStreamTrackProcessor({ track });
const reader = trackProcessor.readable.getReader();
const result = await reader.read();
if (result.done) {
test.skip('MediaStreamTrackProcessor: Cannot get valid frame from readable stream.');
}
return result.value;
}
// Fallback to ImageCapture if MediaStreamTrackProcessor not supported. Using grabFrame() to
// generate imageBitmap and creating video frame from it.
if (typeof ImageCapture !== 'undefined') {
const imageCapture = new ImageCapture(track);
const imageBitmap = await imageCapture.grabFrame();
return new VideoFrame(imageBitmap);
}
// Fallback to using HTMLVideoElement to do capture.
if (typeof HTMLVideoElement === 'undefined') {
test.skip('Try to use HTMLVideoElement do capture but HTMLVideoElement not available.');
}
const video = document.createElement('video');
video.srcObject = stream;
const frame = await getVideoFrameFromVideoElement(test, video);
test.trackForCleanup(frame);
return frame;
}