@kriya-ai/harness 0.0.0-stage → 0.2.0
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.
- package/README.md +379 -2
- package/SAFETY.md +162 -0
- package/THIRD_PARTY_NOTICES.md +7911 -0
- package/bin/kriya.js +577 -0
- package/bundle/cordis.patch.yml +397 -0
- package/contract/SOURCE.md +15 -0
- package/contract/contract.json +1355 -0
- package/package.json +57 -4
- package/plugins/anchor/index.js +56 -0
- package/plugins/anchor/package.json +13 -0
- package/plugins/approvals/index.js +183 -0
- package/plugins/approvals/package.json +13 -0
- package/plugins/brand/DESIGN.md +112 -0
- package/plugins/brand/client.js +826 -0
- package/plugins/brand/index.js +15 -0
- package/plugins/brand/package.json +25 -0
- package/plugins/brand/src/client.js +823 -0
- package/plugins/browser/index.js +218 -0
- package/plugins/browser/package.json +13 -0
- package/plugins/budget/index.js +108 -0
- package/plugins/budget/package.json +13 -0
- package/plugins/changes/client.js +245 -0
- package/plugins/changes/index.js +75 -0
- package/plugins/changes/package.json +22 -0
- package/plugins/edit/client.js +113 -0
- package/plugins/edit/index.js +58 -0
- package/plugins/edit/package.json +23 -0
- package/plugins/fs-freshness/index.js +28 -0
- package/plugins/fs-freshness/package.json +13 -0
- package/plugins/graph/client.js +178 -0
- package/plugins/graph/index.js +4 -0
- package/plugins/graph/package.json +22 -0
- package/plugins/kriya-handoff/index.js +194 -0
- package/plugins/kriya-handoff/package.json +13 -0
- package/plugins/kriya-tools/index.js +189 -0
- package/plugins/kriya-tools/package.json +13 -0
- package/plugins/loop/index.js +743 -0
- package/plugins/loop/package.json +13 -0
- package/plugins/mirror/index.js +235 -0
- package/plugins/mirror/package.json +13 -0
- package/plugins/overview/client.js +276 -0
- package/plugins/overview/index.js +95 -0
- package/plugins/overview/package.json +22 -0
- package/plugins/preview/client.js +337 -0
- package/plugins/preview/index.js +4 -0
- package/plugins/preview/package.json +22 -0
- package/plugins/progress/client.js +290 -0
- package/plugins/progress/index.js +100 -0
- package/plugins/progress/package.json +22 -0
- package/plugins/projects/client.js +249 -0
- package/plugins/projects/index.js +30 -0
- package/plugins/projects/package.json +24 -0
- package/plugins/rea/index.js +256 -0
- package/plugins/rea/package.json +10 -0
- package/plugins/rea/skill/LICENSE +21 -0
- package/plugins/rea/skill/SKILL.md +360 -0
- package/plugins/rea/skill/references/android-applications.md +36 -0
- package/plugins/rea/skill/references/connection-and-recovery.md +127 -0
- package/plugins/rea/skill/references/evidence-workflows.md +71 -0
- package/plugins/rea/skill/references/javascript-applications.md +218 -0
- package/plugins/rea/skill/references/native-and-artifacts.md +297 -0
- package/plugins/rea/skill/references/runtime-observation.md +78 -0
- package/plugins/remote/index.js +297 -0
- package/plugins/remote/package.json +10 -0
- package/plugins/router/index.js +84 -0
- package/plugins/router/package.json +13 -0
- package/plugins/sessions/client.js +68 -0
- package/plugins/sessions/index.js +79 -0
- package/plugins/sessions/package.json +20 -0
- package/plugins/skills/client.js +504 -0
- package/plugins/skills/index.js +166 -0
- package/plugins/skills/package.json +23 -0
- package/src/api.js +97 -0
- package/src/browser.js +286 -0
- package/src/changes.js +191 -0
- package/src/checkpoints.js +313 -0
- package/src/daemon.js +503 -0
- package/src/doctor.js +137 -0
- package/src/edit/core/anchors.js +102 -0
- package/src/edit/core/audio.js +158 -0
- package/src/edit/core/faces.js +99 -0
- package/src/edit/core/ffmpeg.js +258 -0
- package/src/edit/core/handoff.js +220 -0
- package/src/edit/core/history.js +90 -0
- package/src/edit/core/layout.js +92 -0
- package/src/edit/core/mix.js +115 -0
- package/src/edit/core/ops.js +602 -0
- package/src/edit/core/plan.js +266 -0
- package/src/edit/core/project.d.ts +134 -0
- package/src/edit/core/project.js +307 -0
- package/src/edit/core/reframe.js +125 -0
- package/src/edit/core/solver.js +416 -0
- package/src/edit/core/time.js +110 -0
- package/src/edit/core/tonemap.js +57 -0
- package/src/edit/director/ask.js +120 -0
- package/src/edit/director/gateway.js +16 -0
- package/src/edit/director/revise.js +186 -0
- package/src/edit/engine/capabilities.js +33 -0
- package/src/edit/engine/captions.js +154 -0
- package/src/edit/engine/color.js +269 -0
- package/src/edit/engine/confine.js +67 -0
- package/src/edit/engine/dna.js +148 -0
- package/src/edit/engine/index.js +473 -0
- package/src/edit/engine/loudness.js +43 -0
- package/src/edit/engine/media.js +57 -0
- package/src/edit/engine/motion.js +281 -0
- package/src/edit/engine/packs.js +87 -0
- package/src/edit/engine/probe.js +101 -0
- package/src/edit/engine/receipt.js +123 -0
- package/src/edit/engine/tool.js +101 -0
- package/src/edit/gates/copy.js +113 -0
- package/src/edit/gates/index.js +430 -0
- package/src/edit/gates/picture.js +130 -0
- package/src/edit/gates/sound.js +58 -0
- package/src/edit/gates/speech.js +127 -0
- package/src/edit/gates/text.js +196 -0
- package/src/edit/gates/text.swift +22 -0
- package/src/edit/graph/index.js +139 -0
- package/src/edit/graph/journal.js +69 -0
- package/src/edit/graph/scheduler.js +79 -0
- package/src/edit/host/client.js +58 -0
- package/src/edit/host/deliver.js +119 -0
- package/src/edit/host/jobs.js +84 -0
- package/src/edit/host/main.js +135 -0
- package/src/edit/host/paths.js +11 -0
- package/src/edit/host/projects.js +415 -0
- package/src/edit/host/server.js +391 -0
- package/src/edit/host/taste.js +135 -0
- package/src/edit/host/tavi.js +417 -0
- package/src/edit/host/watch.js +141 -0
- package/src/edit/index/db.js +109 -0
- package/src/edit/index/index.js +236 -0
- package/src/edit/index/layers/faces.js +78 -0
- package/src/edit/index/layers/faces.swift +18 -0
- package/src/edit/index/layers/meta.js +35 -0
- package/src/edit/index/layers/music.js +146 -0
- package/src/edit/index/layers/quality.js +120 -0
- package/src/edit/index/layers/retakes.js +132 -0
- package/src/edit/index/layers/screen.js +99 -0
- package/src/edit/index/layers/shots.js +29 -0
- package/src/edit/index/layers/sound.js +42 -0
- package/src/edit/index/layers/words.js +64 -0
- package/src/edit/index/story.js +248 -0
- package/src/edit/index/views.js +71 -0
- package/src/edit/loop/governor.js +112 -0
- package/src/edit/loop/loop.js +69 -0
- package/src/edit/loop/make.js +115 -0
- package/src/edit/loop/repair.js +97 -0
- package/src/edit/loop/replay.js +32 -0
- package/src/edit/recipes/multicam.js +461 -0
- package/src/edit/recipes/reel.js +202 -0
- package/src/edit/recipes/short-from-long.js +237 -0
- package/src/edit/recipes/tutorial.js +137 -0
- package/src/edit/recipes/youtube.js +155 -0
- package/src/edit/runtime/cli.js +126 -0
- package/src/edit/runtime/doctor.js +122 -0
- package/src/edit/runtime/fetch.js +15 -0
- package/src/edit/runtime/licences.js +88 -0
- package/src/edit/runtime/manifest.js +73 -0
- package/src/edit/runtime/setup.js +252 -0
- package/src/failure.js +69 -0
- package/src/gateway-proxy.js +213 -0
- package/src/intent.js +130 -0
- package/src/launch.js +315 -0
- package/src/live.js +81 -0
- package/src/local-skills.js +284 -0
- package/src/login.js +82 -0
- package/src/loop.js +428 -0
- package/src/modes.js +128 -0
- package/src/overview.js +71 -0
- package/src/paths.js +30 -0
- package/src/profiles.js +92 -0
- package/src/projects.js +104 -0
- package/src/recon.js +244 -0
- package/src/rules.js +139 -0
- package/src/runner.bundle.js +21172 -0
- package/src/sentinel.js +248 -0
- package/src/skill-switches.js +130 -0
- package/src/store.js +58 -0
- package/src/strict.js +124 -0
- package/src/verify.js +659 -0
- package/src/world.js +268 -0
- package/studio/dist/index.html +14 -0
- package/studio/dist/studio.css +163 -0
- package/studio/dist/studio.js +1 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
// Colour (EDITOR_WORKFLOWS §6, ED13): `.cube` 3D LUTs read and checked before FFmpeg
|
|
2
|
+
// sees them, and colour matching by statistics. A shot's colour is measured in OKLab
|
|
3
|
+
// (mean and covariance over sampled frames); the closed-form Gaussian transport map
|
|
4
|
+
// T = Σs^(−1/2) (Σs^(1/2) Σt Σs^(1/2))^(1/2) Σs^(−1/2) carries one shot's statistics
|
|
5
|
+
// onto another's, within small limits, and is baked into a 33³ `.cube` for `lut3d`.
|
|
6
|
+
// No pixel is kept: only the numbers.
|
|
7
|
+
|
|
8
|
+
import { runTool } from "./tool.js"
|
|
9
|
+
|
|
10
|
+
export const CUBE = { minSize: 2, maxSize: 65, maxBytes: 16 * 1024 * 1024, size: 33 }
|
|
11
|
+
// "Within small limits" (§6 step 2): a match corrects a shot, it doesn't restyle it
|
|
12
|
+
export const MATCH_LIMITS = { scale: [0.5, 2], lightness: 0.25, chroma: 0.08 }
|
|
13
|
+
// A shot is off against its neighbour past these (OKLab units; L is 0–1)
|
|
14
|
+
export const OFF = { lightness: 0.04, chroma: 0.015 }
|
|
15
|
+
|
|
16
|
+
/* ─────────────────────────────── .cube files ─────────────────────────────── */
|
|
17
|
+
|
|
18
|
+
const bad = (why) => {
|
|
19
|
+
throw new RangeError(`This LUT can't be used: ${why}.`)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Reads an Adobe/Resolve `.cube` 3D LUT, refusing anything malformed.
|
|
24
|
+
* @returns {{ title: string | null, size: number, min: number[], max: number[], table: Float32Array }} table: r fastest, then g, then b; 3 values each
|
|
25
|
+
*/
|
|
26
|
+
export function parseCube(text) {
|
|
27
|
+
if (typeof text !== "string") bad("it isn't text")
|
|
28
|
+
if (text.length > CUBE.maxBytes) bad("it is larger than 16 MB")
|
|
29
|
+
let [title, size, min, max] = [null, null, [0, 0, 0], [1, 1, 1]]
|
|
30
|
+
const values = []
|
|
31
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
32
|
+
const line = raw.trim()
|
|
33
|
+
if (!line || line.startsWith("#")) continue
|
|
34
|
+
const [word, ...rest] = line.split(/\s+/)
|
|
35
|
+
if (word === "TITLE") title = line.slice(5).trim().replace(/^"|"$/g, "")
|
|
36
|
+
else if (word === "LUT_3D_SIZE") size = Number(rest[0])
|
|
37
|
+
else if (word === "LUT_1D_SIZE") bad("it is a 1D LUT; only 3D LUTs are read")
|
|
38
|
+
else if (word === "DOMAIN_MIN" || word === "DOMAIN_MAX" || word === "LUT_3D_INPUT_RANGE") {
|
|
39
|
+
const nums = rest.map(Number)
|
|
40
|
+
const triple = word === "LUT_3D_INPUT_RANGE" ? null : nums
|
|
41
|
+
if (word === "LUT_3D_INPUT_RANGE") [min, max] = [[nums[0], nums[0], nums[0]], [nums[1], nums[1], nums[1]]]
|
|
42
|
+
else if (triple.length !== 3 || !triple.every(Number.isFinite)) bad(`${word} needs three numbers`)
|
|
43
|
+
else if (word === "DOMAIN_MIN") min = triple
|
|
44
|
+
else max = triple
|
|
45
|
+
} else if (/^[-+.\d]/.test(word)) {
|
|
46
|
+
const triple = [word, ...rest].map(Number)
|
|
47
|
+
if (triple.length !== 3 || !triple.every((v) => Number.isFinite(v) && Math.abs(v) <= 16)) bad(`a row isn't three numbers: "${line.slice(0, 40)}"`)
|
|
48
|
+
values.push(...triple)
|
|
49
|
+
} else bad(`it has a line this reader doesn't know: "${word.slice(0, 20)}"`)
|
|
50
|
+
}
|
|
51
|
+
if (!Number.isInteger(size) || size < CUBE.minSize || size > CUBE.maxSize) bad(`its size must be ${CUBE.minSize} to ${CUBE.maxSize}`)
|
|
52
|
+
if (values.length !== size ** 3 * 3) bad(`it has ${values.length / 3} rows, not ${size ** 3}`)
|
|
53
|
+
if (min.some((v, i) => !(v < max[i]))) bad("its domain is empty")
|
|
54
|
+
return { title, size, min, max, table: Float32Array.from(values) }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A `.cube` file's text, with the identity domain. */
|
|
58
|
+
export function cubeText({ size, table, title = null }) {
|
|
59
|
+
const lines = [...(title ? [`TITLE "${String(title).replace(/["\r\n]/g, "")}"`] : []), `LUT_3D_SIZE ${size}`]
|
|
60
|
+
for (let i = 0; i < table.length; i += 3) lines.push(`${table[i].toFixed(6)} ${table[i + 1].toFixed(6)} ${table[i + 2].toFixed(6)}`)
|
|
61
|
+
return `${lines.join("\n")}\n`
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** One colour through a LUT, trilinear, as lut3d does by default. */
|
|
65
|
+
export function sampleCube({ size, table, min, max }, rgb) {
|
|
66
|
+
const at = rgb.map((v, c) => Math.min(size - 1, Math.max(0, ((v - min[c]) / (max[c] - min[c])) * (size - 1))))
|
|
67
|
+
const lo = at.map(Math.floor)
|
|
68
|
+
const hi = lo.map((v) => Math.min(size - 1, v + 1))
|
|
69
|
+
const f = at.map((v, c) => v - lo[c])
|
|
70
|
+
const value = (r, g, b, c) => table[((b * size + g) * size + r) * 3 + c]
|
|
71
|
+
return [0, 1, 2].map((c) => {
|
|
72
|
+
let sum = 0
|
|
73
|
+
for (const [r, wr] of [[lo[0], 1 - f[0]], [hi[0], f[0]]])
|
|
74
|
+
for (const [g, wg] of [[lo[1], 1 - f[1]], [hi[1], f[1]]])
|
|
75
|
+
for (const [b, wb] of [[lo[2], 1 - f[2]], [hi[2], f[2]]]) sum += wr * wg * wb * value(r, g, b, c)
|
|
76
|
+
return sum
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/* ──────────────────────────────── OKLab ──────────────────────────────── */
|
|
81
|
+
|
|
82
|
+
const toLinear = (v) => (v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4)
|
|
83
|
+
const toGamma = (v) => (v <= 0.0031308 ? 12.92 * v : 1.055 * v ** (1 / 2.4) - 0.055)
|
|
84
|
+
|
|
85
|
+
/** sRGB-encoded 0–1 → OKLab (Björn Ottosson's matrices). */
|
|
86
|
+
export function toOklab([r, g, b]) {
|
|
87
|
+
const [lr, lg, lb] = [r, g, b].map(toLinear)
|
|
88
|
+
const l = Math.cbrt(0.4122214708 * lr + 0.5363325363 * lg + 0.0514459929 * lb)
|
|
89
|
+
const m = Math.cbrt(0.2119034982 * lr + 0.6806995451 * lg + 0.1073969566 * lb)
|
|
90
|
+
const s = Math.cbrt(0.0883024619 * lr + 0.2817188376 * lg + 0.6299787005 * lb)
|
|
91
|
+
return [0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s, 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s, 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s]
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** OKLab → sRGB-encoded 0–1, clamped. */
|
|
95
|
+
export function fromOklab([L, a, b]) {
|
|
96
|
+
const l = (L + 0.3963377774 * a + 0.2158037573 * b) ** 3
|
|
97
|
+
const m = (L - 0.1055613458 * a - 0.0638541728 * b) ** 3
|
|
98
|
+
const s = (L - 0.0894841775 * a - 1.291485548 * b) ** 3
|
|
99
|
+
const linear = [4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s, -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s, -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s]
|
|
100
|
+
return linear.map((v) => Math.min(1, Math.max(0, toGamma(Math.min(1, Math.max(0, v))))))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/* ───────────────────────────── statistics ───────────────────────────── */
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Colour statistics of RGB24 pixels: OKLab mean and covariance, a 16-bin lightness
|
|
107
|
+
* histogram, contrast (lightness spread) and saturation (mean chroma). Numbers only.
|
|
108
|
+
* @param {Uint8Array} rgb
|
|
109
|
+
*/
|
|
110
|
+
export function statsOf(rgb, step = 1) {
|
|
111
|
+
const mean = [0, 0, 0]
|
|
112
|
+
const moment = [0, 0, 0, 0, 0, 0] // LL La Lb aa ab bb
|
|
113
|
+
const luma = new Array(16).fill(0)
|
|
114
|
+
let [n, chroma] = [0, 0]
|
|
115
|
+
for (let i = 0; i + 2 < rgb.length; i += 3 * step) {
|
|
116
|
+
const [L, a, b] = toOklab([rgb[i] / 255, rgb[i + 1] / 255, rgb[i + 2] / 255])
|
|
117
|
+
mean[0] += L
|
|
118
|
+
mean[1] += a
|
|
119
|
+
mean[2] += b
|
|
120
|
+
moment[0] += L * L
|
|
121
|
+
moment[1] += L * a
|
|
122
|
+
moment[2] += L * b
|
|
123
|
+
moment[3] += a * a
|
|
124
|
+
moment[4] += a * b
|
|
125
|
+
moment[5] += b * b
|
|
126
|
+
luma[Math.min(15, Math.floor(L * 16))]++
|
|
127
|
+
chroma += Math.hypot(a, b)
|
|
128
|
+
n++
|
|
129
|
+
}
|
|
130
|
+
if (!n) throw new RangeError("there are no pixels to measure")
|
|
131
|
+
const m = mean.map((v) => v / n)
|
|
132
|
+
const e = moment.map((v) => v / n)
|
|
133
|
+
const cov = [
|
|
134
|
+
[e[0] - m[0] * m[0], e[1] - m[0] * m[1], e[2] - m[0] * m[2]],
|
|
135
|
+
[e[1] - m[0] * m[1], e[3] - m[1] * m[1], e[4] - m[1] * m[2]],
|
|
136
|
+
[e[2] - m[0] * m[2], e[4] - m[1] * m[2], e[5] - m[2] * m[2]],
|
|
137
|
+
]
|
|
138
|
+
return { mean: m, cov, luma: luma.map((v) => v / n), contrast: Math.sqrt(Math.max(0, cov[0][0])), saturation: chroma / n, n }
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Samples `frames` frames of [from, to) seconds of a file, as small RGB24, through FFmpeg. */
|
|
142
|
+
export async function readFrames(file, { from = 0, to, frames = 12, width = 64, height = 36, run = runTool, signal }) {
|
|
143
|
+
const span = Math.max(0.04, to - from)
|
|
144
|
+
const chunks = []
|
|
145
|
+
await run("ffmpeg", ["-hide_banner", "-nostdin", "-loglevel", "error", "-ss", from.toFixed(3), "-t", span.toFixed(3), "-protocol_whitelist", "file,pipe", "-i", file, "-an", "-sn", "-dn", "-vf", `fps=${(frames / span).toFixed(4)},scale=${width}:${height}:flags=area,format=rgb24`, "-frames:v", String(frames), "-f", "rawvideo", "-"], { onStdout: (chunk) => chunks.push(chunk), signal, stallMs: 60_000 })
|
|
146
|
+
return new Uint8Array(Buffer.concat(chunks))
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** A range of a file measured: statistics of its sampled frames (seconds). */
|
|
150
|
+
export async function measureColour(file, options) {
|
|
151
|
+
return statsOf(await readFrames(file, options))
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** True when one shot is visibly off against another: exposure or white balance. */
|
|
155
|
+
export function offAgainst(shot, neighbour, limits = OFF) {
|
|
156
|
+
return Math.abs(shot.mean[0] - neighbour.mean[0]) > limits.lightness || Math.hypot(shot.mean[1] - neighbour.mean[1], shot.mean[2] - neighbour.mean[2]) > limits.chroma
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/* ───────────────────────────── the match ───────────────────────────── */
|
|
160
|
+
|
|
161
|
+
// Eigen-decomposition of a symmetric 3×3 matrix (Jacobi rotations)
|
|
162
|
+
function eigen(matrix) {
|
|
163
|
+
const a = matrix.map((row) => [...row])
|
|
164
|
+
const v = [
|
|
165
|
+
[1, 0, 0],
|
|
166
|
+
[0, 1, 0],
|
|
167
|
+
[0, 0, 1],
|
|
168
|
+
]
|
|
169
|
+
for (let sweep = 0; sweep < 50; sweep++) {
|
|
170
|
+
let off = 0
|
|
171
|
+
for (let p = 0; p < 3; p++) for (let q = p + 1; q < 3; q++) off += a[p][q] ** 2
|
|
172
|
+
if (off < 1e-30) break
|
|
173
|
+
for (let p = 0; p < 3; p++) {
|
|
174
|
+
for (let q = p + 1; q < 3; q++) {
|
|
175
|
+
if (Math.abs(a[p][q]) < 1e-30) continue
|
|
176
|
+
const theta = (a[q][q] - a[p][p]) / (2 * a[p][q])
|
|
177
|
+
const t = Math.sign(theta || 1) / (Math.abs(theta) + Math.sqrt(theta * theta + 1))
|
|
178
|
+
const c = 1 / Math.sqrt(t * t + 1)
|
|
179
|
+
const s = t * c
|
|
180
|
+
for (let k = 0; k < 3; k++) {
|
|
181
|
+
const [akp, akq] = [a[k][p], a[k][q]]
|
|
182
|
+
a[k][p] = c * akp - s * akq
|
|
183
|
+
a[k][q] = s * akp + c * akq
|
|
184
|
+
}
|
|
185
|
+
for (let k = 0; k < 3; k++) {
|
|
186
|
+
const [apk, aqk] = [a[p][k], a[q][k]]
|
|
187
|
+
a[p][k] = c * apk - s * aqk
|
|
188
|
+
a[q][k] = s * apk + c * aqk
|
|
189
|
+
}
|
|
190
|
+
for (let k = 0; k < 3; k++) {
|
|
191
|
+
const [vkp, vkq] = [v[k][p], v[k][q]]
|
|
192
|
+
v[k][p] = c * vkp - s * vkq
|
|
193
|
+
v[k][q] = s * vkp + c * vkq
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return { values: [a[0][0], a[1][1], a[2][2]], vectors: v }
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const mul = (x, y) => x.map((row) => y[0].map((_, j) => row.reduce((sum, value, k) => sum + value * y[k][j], 0)))
|
|
202
|
+
|
|
203
|
+
// A function of a symmetric matrix through its eigenvalues; the floor only keeps rounding from going negative (squared spreads reach 1e-8)
|
|
204
|
+
function apply(matrix, fn, floor = 1e-14) {
|
|
205
|
+
const { values, vectors } = eigen(matrix)
|
|
206
|
+
const d = values.map((value) => fn(Math.max(value, floor)))
|
|
207
|
+
return vectors.map((row, i) => [0, 1, 2].map((j) => [0, 1, 2].reduce((sum, k) => sum + vectors[i][k] * d[k] * vectors[j][k], 0)))
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* The transport from one shot's colour to another's: x' = μt + T(x − μs), with T
|
|
212
|
+
* limited so a match corrects rather than restyles.
|
|
213
|
+
* @returns {{ matrix: number[][], from: number[], to: number[] }}
|
|
214
|
+
*/
|
|
215
|
+
export function transport(source, target, limits = MATCH_LIMITS) {
|
|
216
|
+
// A ridge (a spread of 0.01 in OKLab) on both, so a grey shot's near-zero chroma spread stays invertible and equal shots map to themselves
|
|
217
|
+
const ridge = (cov) => cov.map((row, i) => row.map((v, j) => v + (i === j ? 1e-4 : 0)))
|
|
218
|
+
const [sc, tc] = [ridge(source.cov), ridge(target.cov)]
|
|
219
|
+
const half = apply(sc, Math.sqrt)
|
|
220
|
+
const inverseHalf = apply(sc, (v) => 1 / Math.sqrt(v))
|
|
221
|
+
const middle = apply(mul(mul(half, tc), half), Math.sqrt)
|
|
222
|
+
// Clamp the map's own scales, so a near-flat shot isn't stretched without end
|
|
223
|
+
const matrix = apply(mul(mul(inverseHalf, middle), inverseHalf), (v) => Math.min(limits.scale[1], Math.max(limits.scale[0], v)))
|
|
224
|
+
const shift = [target.mean[0] - source.mean[0], target.mean[1] - source.mean[1], target.mean[2] - source.mean[2]]
|
|
225
|
+
const chroma = Math.hypot(shift[1], shift[2])
|
|
226
|
+
const keep = chroma > limits.chroma ? limits.chroma / chroma : 1
|
|
227
|
+
const to = [source.mean[0] + Math.max(-limits.lightness, Math.min(limits.lightness, shift[0])), source.mean[1] + shift[1] * keep, source.mean[2] + shift[2] * keep]
|
|
228
|
+
return { matrix, from: source.mean, to }
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* The match baked into a `.cube` (33³ by default), at `strength` 0–1.
|
|
233
|
+
* @returns {{ size: number, table: Float32Array, min: number[], max: number[] }}
|
|
234
|
+
*/
|
|
235
|
+
export function matchCube(source, target, { size = CUBE.size, strength = 1, limits = MATCH_LIMITS } = {}) {
|
|
236
|
+
const { matrix, from, to } = transport(source, target, limits)
|
|
237
|
+
const table = new Float32Array(size ** 3 * 3)
|
|
238
|
+
let i = 0
|
|
239
|
+
for (let b = 0; b < size; b++) {
|
|
240
|
+
for (let g = 0; g < size; g++) {
|
|
241
|
+
for (let r = 0; r < size; r++) {
|
|
242
|
+
const rgb = [r, g, b].map((v) => v / (size - 1))
|
|
243
|
+
const lab = toOklab(rgb)
|
|
244
|
+
const d = lab.map((v, c) => v - from[c])
|
|
245
|
+
const moved = [0, 1, 2].map((c) => to[c] + matrix[c][0] * d[0] + matrix[c][1] * d[1] + matrix[c][2] * d[2])
|
|
246
|
+
const out = fromOklab(moved)
|
|
247
|
+
for (let c = 0; c < 3; c++) table[i++] = rgb[c] + (out[c] - rgb[c]) * strength
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return { size, table, min: [0, 0, 0], max: [1, 1, 1] }
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Per-shot balance (§6 step 2) as operations: each base clip that is off against the
|
|
256
|
+
* one before it is matched to the first clip it isn't off against, walking back.
|
|
257
|
+
* @param {{ clip: string, stats: object }[]} clips in timeline order
|
|
258
|
+
* @returns {{ op: "match_color", clip: string, reference: string }[]}
|
|
259
|
+
*/
|
|
260
|
+
export function balanceOps(clips, limits = OFF) {
|
|
261
|
+
const ops = []
|
|
262
|
+
const settled = []
|
|
263
|
+
for (const { clip, stats } of clips) {
|
|
264
|
+
const before = settled.at(-1)
|
|
265
|
+
if (before && offAgainst(stats, before.stats, limits)) ops.push({ op: "match_color", clip, reference: before.clip })
|
|
266
|
+
else settled.push({ clip, stats })
|
|
267
|
+
}
|
|
268
|
+
return ops
|
|
269
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Chrome confinement (ES-05, EDD-44): HyperFrames starts Chrome with --no-sandbox
|
|
2
|
+
// (EH-27) and doesn't stop a composition reaching the network (EX-14), so the OS
|
|
3
|
+
// sandbox is the boundary. Every Chrome render runs inside it: no network except
|
|
4
|
+
// loopback, no reading people's files, writing only to its own work folder.
|
|
5
|
+
|
|
6
|
+
import { existsSync, realpathSync } from "node:fs"
|
|
7
|
+
import { dirname } from "node:path"
|
|
8
|
+
|
|
9
|
+
import { EngineFailure } from "./tool.js"
|
|
10
|
+
|
|
11
|
+
const SANDBOX_EXEC = "/usr/bin/sandbox-exec"
|
|
12
|
+
// Where people's files and other programs' temporary files live
|
|
13
|
+
const PRIVATE = ["/Users", "/Volumes", "/private/tmp", "/private/var/folders", "/private/var/tmp"]
|
|
14
|
+
|
|
15
|
+
const real = (path) => realpathSync(path)
|
|
16
|
+
// SBPL string literal: our own absolute paths, refused if they could break out of the quotes
|
|
17
|
+
function quoted(path) {
|
|
18
|
+
if (!path.startsWith("/") || /["\\\n\r\0]/.test(path)) throw new RangeError(`a confinement path must be a plain absolute path, got "${path}"`)
|
|
19
|
+
return `"${path}"`
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** What this computer can confine with, or why it can't. @returns {{ kind: "sandbox-exec" } | { kind: null, missing: string }} */
|
|
23
|
+
export function confinement(platform = process.platform, exists = existsSync) {
|
|
24
|
+
if (platform === "darwin" && exists(SANDBOX_EXEC)) return { kind: "sandbox-exec" }
|
|
25
|
+
// shortcut: bubblewrap on Linux and Windows' AppContainer (EQ-11) aren't written yet, so motion is refused there
|
|
26
|
+
return { kind: null, missing: "The motion layer runs Chrome inside the operating system's sandbox, and this system's sandbox isn't supported yet." }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The macOS profile. Later rules win in SBPL, so the denials come first and the few
|
|
31
|
+
* folders a render needs are let back in.
|
|
32
|
+
* @param {{ read: string[], write: string[] }} paths absolute; made real here
|
|
33
|
+
*/
|
|
34
|
+
export function sandboxProfile({ read, write }) {
|
|
35
|
+
const r = [...new Set(read.map(real))]
|
|
36
|
+
const w = [...new Set(write.map(real))]
|
|
37
|
+
return [
|
|
38
|
+
"(version 1)",
|
|
39
|
+
"(allow default)",
|
|
40
|
+
// Chrome and HyperFrames talk over loopback; nothing reaches another computer. Never a
|
|
41
|
+
// `local ip` rule: it matches every outbound connection (found in ED8). SBPL's
|
|
42
|
+
// "localhost" is every address of this computer, not only 127.0.0.1, so services on
|
|
43
|
+
// it stay reachable: the compositions' CSP and the lint are the barrier there (ES-05)
|
|
44
|
+
"(deny network-outbound)",
|
|
45
|
+
'(allow network-outbound (remote ip "localhost:*"))',
|
|
46
|
+
`(deny file-read* file-write* ${PRIVATE.map((p) => `(subpath ${quoted(p)})`).join(" ")})`,
|
|
47
|
+
// Paths resolve through their parents; names only, never contents
|
|
48
|
+
"(allow file-read-metadata)",
|
|
49
|
+
...(r.length ? [`(allow file-read* ${r.map((p) => `(subpath ${quoted(p)})`).join(" ")})`] : []),
|
|
50
|
+
...(w.length ? [`(allow file-read* file-write* ${w.map((p) => `(subpath ${quoted(p)})`).join(" ")})`] : []),
|
|
51
|
+
].join("\n")
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Wraps a command so it runs confined. Refuses, never runs it open, when there is no sandbox.
|
|
56
|
+
* @param {string} command
|
|
57
|
+
* @param {string[]} args
|
|
58
|
+
* @param {{ read: string[], write: string[] }} paths
|
|
59
|
+
* @returns {{ command: string, args: string[] }}
|
|
60
|
+
*/
|
|
61
|
+
export function confine(command, args, paths, which = confinement()) {
|
|
62
|
+
if (!which.kind) throw new EngineFailure("environment", "no_confinement", which.missing)
|
|
63
|
+
return { command: SANDBOX_EXEC, args: ["-p", sandboxProfile(paths), command, ...args] }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The folder a program's whole install lives in: Node's prefix, from its binary. */
|
|
67
|
+
export const installOf = (binary) => dirname(dirname(real(binary)))
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
// Edit DNA (EDITOR_WORKFLOWS §4, EDD-23): "make it like this" by measuring a reference
|
|
2
|
+
// the person supplies, as numbers only. Pace (shot lengths and how they change across
|
|
3
|
+
// the video), structure (first cut, last shot), colour (OKLab mean and covariance, a
|
|
4
|
+
// lightness histogram, contrast, saturation), where captions sit, and sound (loudness,
|
|
5
|
+
// range, peak). Frames and samples are read through a pipe, measured and dropped: no
|
|
6
|
+
// pixel, word or sound of the reference is kept, so none can reach an output.
|
|
7
|
+
// Not measured yet: caption wording or fonts (never kept), motion feel (needs tracked
|
|
8
|
+
// boxes), music level against speech (needs stems).
|
|
9
|
+
|
|
10
|
+
import { readShots, shotArgs } from "../index/layers/shots.js"
|
|
11
|
+
import { readFrames, statsOf } from "./color.js"
|
|
12
|
+
import { loudnessArgs, readLoudness } from "./loudness.js"
|
|
13
|
+
import { probe } from "./probe.js"
|
|
14
|
+
import { runTool } from "./tool.js"
|
|
15
|
+
|
|
16
|
+
const FLICKS = 705_600_000
|
|
17
|
+
export const DNA_VERSION = "dna-1"
|
|
18
|
+
const FRAMES = 24
|
|
19
|
+
const [W, H] = [160, 90]
|
|
20
|
+
const round = (value, places = 3) => (value === null || value === undefined ? null : Math.round(value * 10 ** places) / 10 ** places)
|
|
21
|
+
|
|
22
|
+
const quantile = (sorted, q) => sorted[Math.min(sorted.length - 1, Math.max(0, Math.round(q * (sorted.length - 1))))]
|
|
23
|
+
|
|
24
|
+
/** Pace from shot lengths in seconds: the spread, and the median length in each tenth of the running time. */
|
|
25
|
+
export function paceOf(shots, seconds) {
|
|
26
|
+
const lengths = shots.map((s) => s.length)
|
|
27
|
+
const sorted = [...lengths].sort((a, b) => a - b)
|
|
28
|
+
const curve = Array.from({ length: 10 }, (_, tenth) => {
|
|
29
|
+
const [from, to] = [(tenth * seconds) / 10, ((tenth + 1) * seconds) / 10]
|
|
30
|
+
const inside = shots.filter((s) => s.start + s.length / 2 >= from && s.start + s.length / 2 < to).map((s) => s.length).sort((a, b) => a - b)
|
|
31
|
+
return round(inside.length ? quantile(inside, 0.5) : null)
|
|
32
|
+
})
|
|
33
|
+
return { shots: lengths.length, cutsPerMinute: round(((lengths.length - 1) / Math.max(seconds, 1)) * 60), median: round(quantile(sorted, 0.5)), p10: round(quantile(sorted, 0.1)), p90: round(quantile(sorted, 0.9)), curve }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Where burned-in captions sit, as a share of the frame height: rows where bright,
|
|
38
|
+
* sharp-edged pixels (white text on a picture) recur across the sampled frames. Only
|
|
39
|
+
* the band's position is kept, never what it says.
|
|
40
|
+
* @param {Uint8Array} rgb frames of W × H RGB24
|
|
41
|
+
*/
|
|
42
|
+
export function captionBand(rgb, width = W, height = H) {
|
|
43
|
+
const frames = Math.floor(rgb.length / (width * height * 3))
|
|
44
|
+
const hits = new Array(height).fill(0)
|
|
45
|
+
for (let f = 0; f < frames; f++) {
|
|
46
|
+
for (let y = 0; y < height; y++) {
|
|
47
|
+
let edges = 0
|
|
48
|
+
for (let x = 1; x < width; x++) {
|
|
49
|
+
const i = ((f * height + y) * width + x) * 3
|
|
50
|
+
const bright = rgb[i] > 200 && rgb[i + 1] > 200 && rgb[i + 2] > 200
|
|
51
|
+
const step = Math.abs(rgb[i] - rgb[i - 3]) + Math.abs(rgb[i + 1] - rgb[i - 2]) + Math.abs(rgb[i + 2] - rgb[i - 1])
|
|
52
|
+
if (bright && step > 240) edges++
|
|
53
|
+
}
|
|
54
|
+
if (edges >= width / 40) hits[y]++
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const rows = hits.map((n, y) => (n >= Math.max(1, frames * 0.3) ? y : -1)).filter((y) => y >= 0)
|
|
58
|
+
if (!rows.length) return { present: false, top: null, bottom: null, frames: 0 }
|
|
59
|
+
// The densest run of rows is the band
|
|
60
|
+
const runs = []
|
|
61
|
+
for (const y of rows) runs.at(-1) && y - runs.at(-1).at(-1) <= 2 ? runs.at(-1).push(y) : runs.push([y])
|
|
62
|
+
const band = runs.sort((a, b) => b.length - a.length)[0]
|
|
63
|
+
return { present: true, top: round(band[0] / height), bottom: round((band.at(-1) + 1) / height), frames: round(Math.max(...band.map((y) => hits[y])) / frames) }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Measures a reference video. Every value in the result is a number, a boolean or
|
|
68
|
+
* null: checkDna proves it before anything keeps it.
|
|
69
|
+
* @returns {Promise<object>} an EditDNA
|
|
70
|
+
*/
|
|
71
|
+
export async function extractDna(file, { run = runTool, signal } = {}) {
|
|
72
|
+
const { facts } = await probe(file, { run })
|
|
73
|
+
const seconds = (facts.duration ?? 0) / FLICKS
|
|
74
|
+
if (!(seconds > 0)) throw new RangeError("This reference has no length to measure.")
|
|
75
|
+
const shots = readShots(await run("ffmpeg", shotArgs(file), { signal, timeoutMs: 600_000 }), facts.duration).map((s) => ({ start: s.start / FLICKS, length: (s.end - s.start) / FLICKS }))
|
|
76
|
+
const frames = facts.width ? await readFrames(file, { from: 0, to: seconds, frames: FRAMES, width: W, height: H, run, signal }) : null
|
|
77
|
+
const colour = frames ? statsOf(frames, 4) : null
|
|
78
|
+
const sound = facts.hasAudio === false ? null : await run("ffmpeg", loudnessArgs(file), { signal, timeoutMs: 600_000 })
|
|
79
|
+
const range = sound ? [...sound.matchAll(/lavfi\.r128\.LRA=(-?[\d.]+)/g)].at(-1)?.[1] : null
|
|
80
|
+
const loud = sound ? readLoudness(sound) : null
|
|
81
|
+
const dna = {
|
|
82
|
+
kind: "edit-dna",
|
|
83
|
+
version: DNA_VERSION,
|
|
84
|
+
seconds: round(seconds),
|
|
85
|
+
pace: paceOf(shots, seconds),
|
|
86
|
+
structure: { firstCut: round(shots.length > 1 ? shots[1].start : null), lastShot: round(shots.at(-1)?.length ?? null) },
|
|
87
|
+
colour: colour && { mean: colour.mean.map((v) => round(v, 4)), cov: colour.cov.map((row) => row.map((v) => round(v, 5))), luma: colour.luma.map((v) => round(v, 4)), contrast: round(colour.contrast, 4), saturation: round(colour.saturation, 4) },
|
|
88
|
+
captions: frames ? captionBand(frames) : null,
|
|
89
|
+
sound: loud && { lufs: round(loud.integrated, 1), range: round(range === undefined || range === null ? null : Number(range), 1), truePeak: round(loud.truePeak, 1) },
|
|
90
|
+
}
|
|
91
|
+
return checkDna(dna)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// The shape an EditDNA may have: numbers only (EDD-23). Anything else is refused, so
|
|
95
|
+
// no text, path or pixel from a reference can ride along
|
|
96
|
+
const NUM = "num"
|
|
97
|
+
const SHAPE = {
|
|
98
|
+
kind: "edit-dna",
|
|
99
|
+
version: DNA_VERSION,
|
|
100
|
+
seconds: NUM,
|
|
101
|
+
pace: { shots: NUM, cutsPerMinute: NUM, median: NUM, p10: NUM, p90: NUM, curve: [NUM, 10] },
|
|
102
|
+
structure: { firstCut: NUM, lastShot: NUM },
|
|
103
|
+
colour: { mean: [NUM, 3], cov: [[NUM, 3], 3], luma: [NUM, 16], contrast: NUM, saturation: NUM },
|
|
104
|
+
captions: { present: "bool", top: NUM, bottom: NUM, frames: NUM },
|
|
105
|
+
sound: { lufs: NUM, range: NUM, truePeak: NUM },
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function conforms(value, shape, path) {
|
|
109
|
+
if (typeof shape === "string" && shape !== NUM && shape !== "bool") {
|
|
110
|
+
if (value !== shape) throw new RangeError(`${path} must be "${shape}"`)
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
if (value === null) return
|
|
114
|
+
if (shape === NUM) {
|
|
115
|
+
if (typeof value !== "number" || !Number.isFinite(value)) throw new RangeError(`${path} must be a number`)
|
|
116
|
+
} else if (shape === "bool") {
|
|
117
|
+
if (typeof value !== "boolean") throw new RangeError(`${path} must be true or false`)
|
|
118
|
+
} else if (Array.isArray(shape)) {
|
|
119
|
+
if (!Array.isArray(value) || value.length !== shape[1]) throw new RangeError(`${path} must be a list of ${shape[1]}`)
|
|
120
|
+
value.forEach((item, i) => conforms(item, shape[0], `${path}[${i}]`))
|
|
121
|
+
} else {
|
|
122
|
+
if (typeof value !== "object" || Array.isArray(value)) throw new RangeError(`${path} must be an object`)
|
|
123
|
+
const extra = Object.keys(value).find((key) => !(key in shape))
|
|
124
|
+
if (extra) throw new RangeError(`${path}.${extra} isn't part of an EditDNA`)
|
|
125
|
+
for (const key of Object.keys(shape)) conforms(value[key], shape[key], `${path}.${key}`)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Throws unless `dna` is numbers only, in the EditDNA shape; returns it. */
|
|
130
|
+
export function checkDna(dna) {
|
|
131
|
+
conforms(dna, SHAPE, "dna")
|
|
132
|
+
return dna
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The reference's pacing as a recipe's pace: the longest a picture holds is the
|
|
137
|
+
* reference's median shot, and the opening and closing tenths keep their speed
|
|
138
|
+
* against it. Clamped to what a talk can carry (2 to 30 s).
|
|
139
|
+
* @returns {{ every: number, intro: number, close: number }} every in flicks
|
|
140
|
+
*/
|
|
141
|
+
export function paceFromDna(dna) {
|
|
142
|
+
const { median, curve } = checkDna(dna).pace
|
|
143
|
+
const clamp = (v, lo, hi) => Math.min(hi, Math.max(lo, v))
|
|
144
|
+
const known = curve.filter((v) => v !== null)
|
|
145
|
+
if (median === null || !known.length) throw new RangeError("This reference has too few shots to take its pace from.")
|
|
146
|
+
const at = (tenth) => curve[tenth] ?? median
|
|
147
|
+
return { every: Math.round(clamp(median, 2, 30) * FLICKS), intro: round(clamp(at(0) / median, 0.3, 1.5)), close: round(clamp(at(9) / median, 0.3, 1.5)) }
|
|
148
|
+
}
|