@hyperframes/core 0.9.36 → 0.9.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ import type { PreviewSourceFacts } from "../previewProxyBox.js";
2
+ declare global {
3
+ interface Window {
4
+ __HF_MEDIA_SOURCE_FACTS__?: Record<string, PreviewSourceFacts>;
5
+ }
6
+ }
7
+ export declare function currentSrcValue(el: HTMLMediaElement): string;
8
+ /** The decoded, query-free pathname the server's maps are keyed by; null with no src or a cross-origin one. */
9
+ export declare function deriveCodecMapKey(el: HTMLMediaElement): string | null;
10
+ export declare function lookupCodecMapEntry<T>(pathname: string, map: Record<string, T>): T | null;
11
+ export declare function sourceFactsOf(el: HTMLMediaElement): PreviewSourceFacts | null;
@@ -0,0 +1,66 @@
1
+ export function currentSrcValue(el) {
2
+ return el.currentSrc || el.src;
3
+ }
4
+ /** The decoded, query-free pathname the server's maps are keyed by; null with no src or a cross-origin one. */
5
+ export function deriveCodecMapKey(el) {
6
+ const raw = currentSrcValue(el);
7
+ if (!raw)
8
+ return null;
9
+ let url;
10
+ try {
11
+ url = new URL(raw, document.baseURI);
12
+ }
13
+ catch {
14
+ return null;
15
+ }
16
+ if (url.origin !== window.location.origin)
17
+ return null;
18
+ try {
19
+ return decodeURIComponent(url.pathname);
20
+ }
21
+ catch {
22
+ return url.pathname;
23
+ }
24
+ }
25
+ /** The keys carry no route prefix, so the longest key that ends the pathname wins. */
26
+ function lookupLongestExactSuffix(pathname, map) {
27
+ let bestKey = null;
28
+ for (const key of Object.keys(map)) {
29
+ if (!pathname.endsWith(key))
30
+ continue;
31
+ if (bestKey === null || key.length > bestKey.length)
32
+ bestKey = key;
33
+ }
34
+ return bestKey ? (map[bestKey] ?? null) : null;
35
+ }
36
+ function lookupNormalizedSuffix(pathname, map) {
37
+ // Exact stays authoritative; a case or Unicode spelling difference matches only when one longest key is unambiguous.
38
+ const normalizedPathname = pathname.normalize("NFC").toLowerCase();
39
+ let normalizedBestLength = -1;
40
+ let normalizedBest = null;
41
+ let ambiguous = false;
42
+ for (const [key, entry] of Object.entries(map)) {
43
+ const normalizedKey = key.normalize("NFC").toLowerCase();
44
+ if (!normalizedPathname.endsWith(normalizedKey))
45
+ continue;
46
+ if (normalizedKey.length > normalizedBestLength) {
47
+ normalizedBestLength = normalizedKey.length;
48
+ normalizedBest = entry;
49
+ ambiguous = false;
50
+ }
51
+ else if (normalizedKey.length === normalizedBestLength) {
52
+ ambiguous = true;
53
+ }
54
+ }
55
+ return ambiguous ? null : normalizedBest;
56
+ }
57
+ export function lookupCodecMapEntry(pathname, map) {
58
+ return (map[pathname] ??
59
+ lookupLongestExactSuffix(pathname, map) ??
60
+ lookupNormalizedSuffix(pathname, map));
61
+ }
62
+ export function sourceFactsOf(el) {
63
+ const facts = window.__HF_MEDIA_SOURCE_FACTS__;
64
+ const key = deriveCodecMapKey(el);
65
+ return facts && key !== null ? lookupCodecMapEntry(key, facts) : null;
66
+ }
@@ -0,0 +1,10 @@
1
+ /** The one owner of making a canvas frame look like the browser's own video layer, which on macOS Chromium
2
+ * shows 8-bit SDR video with Apple's gamma (about 1.961) where a canvas shows sRGB. Display-only, so canvas
3
+ * readers keep the pixels a playing video gives them. */
4
+ export declare const NATIVE_VIDEO_LOOK_FILTER_ID = "__hf_native_video_look";
5
+ /** The 8-bit value the video layer shows for a canvas value, 0 to 255. */
6
+ export declare function nativeVideoLookValue(canvasValue: number): number;
7
+ /** Chromium on macOS: the only browser measured to draw a canvas differently from its video layer. */
8
+ export declare function nativeVideoLookNeeded(): boolean;
9
+ /** Gives the visible stand-in canvas the look its video layer has, ahead of any authored filter. */
10
+ export declare function applyNativeVideoLook(canvas: HTMLCanvasElement, video: HTMLVideoElement, authoredFilter: string): void;
@@ -0,0 +1,44 @@
1
+ import { isWebKit } from "./mediaClock.js";
2
+ import { PREVIEW_VIDEO_CANVAS_ATTR } from "../studioPreviewMark.js";
3
+ import { sourceFactsOf } from "./mediaSourceLookup.js";
4
+ /** The one owner of making a canvas frame look like the browser's own video layer, which on macOS Chromium
5
+ * shows 8-bit SDR video with Apple's gamma (about 1.961) where a canvas shows sRGB. Display-only, so canvas
6
+ * readers keep the pixels a playing video gives them. */
7
+ export const NATIVE_VIDEO_LOOK_FILTER_ID = "__hf_native_video_look";
8
+ const VIDEO_LAYER_GAMMA = 1.961;
9
+ const srgbEncode = (linear) => linear <= 0.0031308 ? 12.92 * linear : 1.055 * Math.pow(linear, 1 / 2.4) - 0.055;
10
+ /** The 8-bit value the video layer shows for a canvas value, 0 to 255. */
11
+ export function nativeVideoLookValue(canvasValue) {
12
+ return Math.round(255 * srgbEncode(Math.pow(canvasValue / 255, VIDEO_LAYER_GAMMA)));
13
+ }
14
+ /** Chromium on macOS: the only browser measured to draw a canvas differently from its video layer. */
15
+ export function nativeVideoLookNeeded() {
16
+ const { userAgent } = navigator;
17
+ return /Macintosh/.test(userAgent) && !isWebKit() && /Chrom(e|ium)\//.test(userAgent);
18
+ }
19
+ /** Only a source the studio's scan positively reports as 8-bit is measured; unknown or higher depth is left alone. */
20
+ function isKnownSdrSource(video) {
21
+ const bitDepth = sourceFactsOf(video)?.bitDepth;
22
+ return typeof bitDepth === "number" && bitDepth > 0 && bitDepth <= 8;
23
+ }
24
+ function ensureFilter(doc) {
25
+ if (doc.getElementById(NATIVE_VIDEO_LOOK_FILTER_ID))
26
+ return;
27
+ const values = Array.from({ length: 256 }, (_, level) => (nativeVideoLookValue(level) / 255).toFixed(4)).join(" ");
28
+ const channel = (name) => `<feFunc${name} type="table" tableValues="${values}"/>`;
29
+ const holder = doc.createElement("div");
30
+ holder.innerHTML =
31
+ `<svg width="0" height="0" style="position:absolute" aria-hidden="true" data-hyperframes-ignore data-hf-ignore ` +
32
+ `data-layout-ignore data-hyperframes-picker-ignore ${PREVIEW_VIDEO_CANVAS_ATTR}><filter id="${NATIVE_VIDEO_LOOK_FILTER_ID}" ` +
33
+ `color-interpolation-filters="sRGB"><feComponentTransfer>${channel("R")}${channel("G")}${channel("B")}` +
34
+ `</feComponentTransfer></filter></svg>`;
35
+ (doc.body ?? doc.documentElement).append(holder.firstElementChild);
36
+ }
37
+ /** Gives the visible stand-in canvas the look its video layer has, ahead of any authored filter. */
38
+ export function applyNativeVideoLook(canvas, video, authoredFilter) {
39
+ if (!nativeVideoLookNeeded() || !isKnownSdrSource(video))
40
+ return;
41
+ ensureFilter(canvas.ownerDocument);
42
+ const look = `url(#${NATIVE_VIDEO_LOOK_FILTER_ID})`;
43
+ canvas.style.setProperty("filter", authoredFilter === "none" ? look : `${look} ${authoredFilter}`);
44
+ }
@@ -1,5 +1,6 @@
1
1
  import { MEDIA_ELEMENT_LEAD_SECONDS } from "./mediaClock.js";
2
2
  import { COLOR_GRADING_SOURCE_HIDDEN_ATTR } from "../colorGrading.js";
3
+ import { applyNativeVideoLook } from "./nativeVideoLook.js";
3
4
  import { installPausedVideoCallbacks, stagePausedVideoCallbacks, flushPausedVideoCallbacks, releasePausedVideoCallbacks, observeNativeVideoFrames, } from "./pausedVideoCallbacks.js";
4
5
  import { PREVIEW_VIDEO_FRAME_ATTR as FRAME_ATTR, PREVIEW_VIDEO_SOURCE_ATTR, PREVIEW_VIDEO_CANVAS_ATTR, } from "../studioPreviewMark.js";
5
6
  const SURFACE_BUDGET = 128 * 1024 * 1024;
@@ -105,6 +106,7 @@ function refresh(entry) {
105
106
  canvas.style.top = `${video.offsetTop}px`;
106
107
  canvas.style.margin = "0";
107
108
  }
109
+ applyNativeVideoLook(canvas, video, css.filter || "none");
108
110
  canvas.style.setProperty("pointer-events", "none", "important");
109
111
  canvas.style.setProperty("animation", "none", "important");
110
112
  canvas.style.setProperty("transition", "none", "important");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperframes/core",
3
- "version": "0.9.36",
3
+ "version": "0.9.38",
4
4
  "description": "",
5
5
  "repository": {
6
6
  "type": "git",
@@ -441,9 +441,9 @@
441
441
  "linkedom": "^0.18.12",
442
442
  "postcss": "^8.5.8",
443
443
  "wawoff2": "^2.0.1",
444
- "@hyperframes/lint": "0.9.36",
445
- "@hyperframes/studio-server": "0.9.36",
446
- "@hyperframes/parsers": "0.9.36"
444
+ "@hyperframes/lint": "0.9.38",
445
+ "@hyperframes/parsers": "0.9.38",
446
+ "@hyperframes/studio-server": "0.9.38"
447
447
  },
448
448
  "devDependencies": {
449
449
  "@fontsource/archivo-black": "^5.2.8",