@tradalab/mediakit 1.0.0 → 1.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/dist/recipe.d.ts CHANGED
@@ -21,7 +21,17 @@ type BgFilter = {
21
21
  } | {
22
22
  kind: "image";
23
23
  imagePath: string;
24
+ }
25
+ /**
26
+ * The background becomes TRANSPARENT and the person stays: the output canvas carries the
27
+ * mask as alpha. For a {@link Scene} to put the person over another source; a plain video
28
+ * track has no alpha, so on its own this shows the person over black.
29
+ */
30
+ | {
31
+ kind: "cutout";
24
32
  };
33
+ /** A colour as red, green, blue, each 0..1. */
34
+ type Rgb = [number, number, number];
25
35
  interface Effects {
26
36
  /** Skin smoothing, 0..1. Needs the person mask, so it pulls in the segmentation cost. */
27
37
  beautify: number;
@@ -41,6 +51,45 @@ interface Effects {
41
51
  eyeBright: number;
42
52
  /** Reduce under-eye shadow, 0..1. */
43
53
  underEye: number;
54
+ /** Soften the nasolabial folds, 0..1. */
55
+ smileLines: number;
56
+ /**
57
+ * Makeup, 0..1 each, in its colour. Drawn against the full landmark mesh, so the lip tint
58
+ * follows the lip line rather than an ellipse. Not offered in meet, like reshaping.
59
+ */
60
+ lips: number;
61
+ lipColor: Rgb;
62
+ blush: number;
63
+ blushColor: Rgb;
64
+ /** Iris colour: the ring only, clipped by the eyelids, the pupil left black. */
65
+ iris: number;
66
+ irisColor: Rgb;
67
+ /**
68
+ * A colour LUT: `lut` is a URL the host serves (a 512x512 "lookup" PNG, 64 levels, 8x8 tiles,
69
+ * row 0 at the top), `grade` how strongly it applies. "" means none.
70
+ */
71
+ lut: string;
72
+ grade: number;
73
+ /**
74
+ * Take the person mask from a coloured screen instead of segmentation. Cheaper - MediaPipe
75
+ * is never loaded - and the edge keeps hair the model loses. `keyColor` is the screen's
76
+ * colour; [0, 0, 0] means "find it" from the frame's border. `keyTolerance` widens (+) or
77
+ * narrows (-) what counts as screen, -1..1 around a tuned default.
78
+ */
79
+ chromaKey: boolean;
80
+ keyColor: Rgb;
81
+ keyTolerance: number;
82
+ /**
83
+ * Lift exposure when the room is dark, on top of `exposure`. Only ever brightens. Needs no
84
+ * detector: it reads the frame's own brightness.
85
+ */
86
+ autoLight: boolean;
87
+ /**
88
+ * Flip the picture left to right, as a selfie preview shows it. Costs nothing on top of any
89
+ * other effect; on its own it is one pass. Text on a whiteboard then reads backwards to
90
+ * everyone watching, which is why it is a choice and not the default.
91
+ */
92
+ mirror: boolean;
44
93
  /**
45
94
  * Jawline slimming, 0..1.
46
95
  *
@@ -51,12 +100,42 @@ interface Effects {
51
100
  slimFace: number;
52
101
  /** Eye enlargement, 0..1. Withheld from meet for the same reason as slimFace. */
53
102
  enlargeEyes: number;
103
+ /** Nose narrowing, 0..1. Withheld from meet like slimFace. */
104
+ noseSlim: number;
105
+ /** Chin length, -1 (shorter) .. 1 (longer). Withheld from meet like slimFace. */
106
+ chin: number;
54
107
  }
55
108
  declare const NO_EFFECTS: Effects;
109
+ /**
110
+ * One knob for "look a little fresher": skin, eyes, under-eyes, smile lines and teeth, moved
111
+ * together in proportions tuned to read as rested rather than filtered at 1.
112
+ *
113
+ * A host that shows this instead of five sliders gets the shape Zoom and TikTok ship - people
114
+ * reach for one control, not five, and five half-understood knobs mostly get set too high. The
115
+ * ratios live here, next to the fields they drive, so every host means the same thing by it.
116
+ */
117
+ declare function touchUp(amount: number): Pick<Effects, "beautify" | "eyeBright" | "underEye" | "smileLines" | "teeth">;
118
+ /** What `touchUp(1)` sets. Exported so a host can read a stored recipe back into one knob. */
119
+ declare const TOUCH_UP: {
120
+ readonly beautify: 0.6;
121
+ readonly eyeBright: 0.5;
122
+ readonly underEye: 0.45;
123
+ readonly smileLines: 0.5;
124
+ readonly teeth: 0.4;
125
+ };
126
+ /**
127
+ * The touch-up position a recipe corresponds to: the LARGEST of its five fields relative to
128
+ * `touchUp(1)`. A recipe whose fields were set one by one has no single answer, and reading one
129
+ * field only would show 0 for a recipe still whitening teeth - a knob at 0 that cannot turn off
130
+ * what is running. Taking the largest, any effect still on shows, and moving the knob rewrites
131
+ * all five consistently.
132
+ */
133
+ declare function touchUpAmount(e: Effects): number;
56
134
  /** Go-side `BackgroundMode`. Must match `core/recipe.go`. */
57
135
  declare const MODE_NONE = 0;
58
136
  declare const MODE_BLUR = 1;
59
137
  declare const MODE_IMAGE = 2;
138
+ declare const MODE_CUTOUT = 3;
60
139
  declare function backgroundMode(f: BgFilter): number;
61
140
  /**
62
141
  * Whether this recipe needs the person mask.
@@ -69,6 +148,12 @@ declare function backgroundMode(f: BgFilter): number;
69
148
  * segmenter at all, BEFORE it can ask Go.
70
149
  */
71
150
  declare function needsMask(f: BgFilter, e: Effects): boolean;
151
+ /**
152
+ * Whether this recipe needs the SEGMENTER: a mask, and not one a colour key already gives.
153
+ * Must match `Recipe.NeedsSegmentation` on the Go side. This, not {@link needsMask}, decides
154
+ * whether MediaPipe is loaded - with `chromaKey` on, its 9 MB are never fetched.
155
+ */
156
+ declare function needsSegmentation(f: BgFilter, e: Effects): boolean;
72
157
  /**
73
158
  * Whether this recipe needs face landmarks.
74
159
  *
@@ -91,9 +176,24 @@ interface CoreRecipe {
91
176
  teeth: number;
92
177
  eyeBright: number;
93
178
  underEye: number;
179
+ smileLines: number;
180
+ lips: number;
181
+ lipColor: Rgb;
182
+ blush: number;
183
+ blushColor: Rgb;
184
+ iris: number;
185
+ irisColor: Rgb;
186
+ grade: number;
187
+ chromaKey: boolean;
188
+ keyColor: Rgb;
189
+ keyTolerance: number;
190
+ autoLight: boolean;
191
+ mirror: boolean;
94
192
  slimFace: number;
95
193
  enlargeEyes: number;
194
+ noseSlim: number;
195
+ chin: number;
96
196
  }
97
197
  declare function coreRecipe(f: BgFilter, e: Effects): CoreRecipe;
98
198
 
99
- export { type BgFilter, type CoreRecipe, type Effects, MODE_BLUR, MODE_IMAGE, MODE_NONE, NO_EFFECTS, backgroundMode, coreRecipe, needsFace, needsMask };
199
+ export { type BgFilter, type CoreRecipe, type Effects, MODE_BLUR, MODE_CUTOUT, MODE_IMAGE, MODE_NONE, NO_EFFECTS, type Rgb, TOUCH_UP, backgroundMode, coreRecipe, needsFace, needsMask, needsSegmentation, touchUp, touchUpAmount };
package/dist/recipe.js CHANGED
@@ -1,21 +1,31 @@
1
1
  import {
2
2
  MODE_BLUR,
3
+ MODE_CUTOUT,
3
4
  MODE_IMAGE,
4
5
  MODE_NONE,
5
6
  NO_EFFECTS,
7
+ TOUCH_UP,
6
8
  backgroundMode,
7
9
  coreRecipe,
8
10
  needsFace,
9
- needsMask
10
- } from "./chunk-5KXLFDFV.js";
11
+ needsMask,
12
+ needsSegmentation,
13
+ touchUp,
14
+ touchUpAmount
15
+ } from "./chunk-V47SDU4L.js";
11
16
  export {
12
17
  MODE_BLUR,
18
+ MODE_CUTOUT,
13
19
  MODE_IMAGE,
14
20
  MODE_NONE,
15
21
  NO_EFFECTS,
22
+ TOUCH_UP,
16
23
  backgroundMode,
17
24
  coreRecipe,
18
25
  needsFace,
19
- needsMask
26
+ needsMask,
27
+ needsSegmentation,
28
+ touchUp,
29
+ touchUpAmount
20
30
  };
21
31
  //# sourceMappingURL=recipe.js.map
package/dist/worker.js CHANGED
@@ -4039,15 +4039,16 @@
4039
4039
  // src/face.ts
4040
4040
  var WASM_DIR2 = "/mediakit/mp";
4041
4041
  var MODEL_URL2 = "/mediakit/face_landmarker.task";
4042
- async function createFaceDetector(canvas) {
4042
+ var FACE_FLOATS = 478 * 3;
4043
+ async function createFaceDetector(canvas, maxFaces) {
4043
4044
  const fileset = await Xo.forVisionTasks(WASM_DIR2);
4044
4045
  const landmarker = await yh.createFromOptions(fileset, {
4045
4046
  baseOptions: { modelAssetPath: MODEL_URL2, delegate: "GPU" },
4046
4047
  canvas,
4047
4048
  runningMode: "VIDEO",
4048
- // One face: this is the user's own camera. More faces is more inference for a result
4049
- // the layer above can only use one of.
4050
- numFaces: 1,
4049
+ // As many as the core tracks: one landmark pass returns them all, so the cost of a second
4050
+ // face is the copy below and its own shader passes, not a second inference.
4051
+ numFaces: maxFaces,
4051
4052
  // No blendshapes and no transformation matrix - extra work for data nobody reads.
4052
4053
  outputFaceBlendshapes: false,
4053
4054
  outputFacialTransformationMatrixes: false
@@ -4055,14 +4056,16 @@
4055
4056
  return {
4056
4057
  detect(frame, timestampMs, out) {
4057
4058
  const res = landmarker.detectForVideo(frame, timestampMs);
4058
- const lm = res.faceLandmarks?.[0];
4059
- if (!lm || lm.length === 0) return 0;
4060
- const n2 = Math.min(lm.length * 3, out.length);
4061
- for (let i2 = 0; i2 * 3 < n2; i2++) {
4062
- const p2 = lm[i2];
4063
- out[i2 * 3] = p2.x;
4064
- out[i2 * 3 + 1] = p2.y;
4065
- out[i2 * 3 + 2] = p2.z;
4059
+ let n2 = 0;
4060
+ for (const lm of res.faceLandmarks ?? []) {
4061
+ if (lm.length * 3 !== FACE_FLOATS || n2 + FACE_FLOATS > out.length) break;
4062
+ for (let i2 = 0; i2 < lm.length; i2++) {
4063
+ const p2 = lm[i2];
4064
+ out[n2 + i2 * 3] = p2.x;
4065
+ out[n2 + i2 * 3 + 1] = p2.y;
4066
+ out[n2 + i2 * 3 + 2] = p2.z;
4067
+ }
4068
+ n2 += FACE_FLOATS;
4066
4069
  }
4067
4070
  return n2;
4068
4071
  },
@@ -4155,18 +4158,37 @@
4155
4158
  teeth: 0,
4156
4159
  eyeBright: 0,
4157
4160
  underEye: 0,
4161
+ smileLines: 0,
4162
+ lips: 0,
4163
+ lipColor: [0.55, 0.18, 0.24],
4164
+ blush: 0,
4165
+ blushColor: [0.9, 0.5, 0.42],
4166
+ iris: 0,
4167
+ irisColor: [0.3, 0.5, 0.75],
4168
+ lut: "",
4169
+ grade: 0,
4170
+ chromaKey: false,
4171
+ keyColor: [0, 0, 0],
4172
+ keyTolerance: 0,
4173
+ autoLight: false,
4174
+ mirror: false,
4158
4175
  slimFace: 0,
4159
- enlargeEyes: 0
4176
+ enlargeEyes: 0,
4177
+ noseSlim: 0,
4178
+ chin: 0
4160
4179
  };
4161
4180
  var MODE_NONE = 0;
4162
4181
  var MODE_BLUR = 1;
4163
4182
  var MODE_IMAGE = 2;
4183
+ var MODE_CUTOUT = 3;
4164
4184
  function backgroundMode(f2) {
4165
4185
  switch (f2.kind) {
4166
4186
  case "blur":
4167
4187
  return MODE_BLUR;
4168
4188
  case "image":
4169
4189
  return MODE_IMAGE;
4190
+ case "cutout":
4191
+ return MODE_CUTOUT;
4170
4192
  default:
4171
4193
  return MODE_NONE;
4172
4194
  }
@@ -4174,8 +4196,11 @@
4174
4196
  function needsMask(f2, e2) {
4175
4197
  return f2.kind !== "none" || e2.beautify > 0 || e2.autoframe;
4176
4198
  }
4199
+ function needsSegmentation(f2, e2) {
4200
+ return needsMask(f2, e2) && !e2.chromaKey;
4201
+ }
4177
4202
  function needsFace(e2) {
4178
- return e2.teeth > 0 || e2.eyeBright > 0 || e2.underEye > 0 || e2.slimFace > 0 || e2.enlargeEyes > 0;
4203
+ return e2.teeth > 0 || e2.eyeBright > 0 || e2.underEye > 0 || e2.smileLines > 0 || e2.lips > 0 || e2.blush > 0 || e2.iris > 0 || e2.slimFace > 0 || e2.enlargeEyes > 0 || e2.noseSlim > 0 || e2.chin !== 0;
4179
4204
  }
4180
4205
  function coreRecipe(f2, e2) {
4181
4206
  return {
@@ -4190,8 +4215,23 @@
4190
4215
  teeth: e2.teeth,
4191
4216
  eyeBright: e2.eyeBright,
4192
4217
  underEye: e2.underEye,
4218
+ smileLines: e2.smileLines,
4219
+ lips: e2.lips,
4220
+ lipColor: e2.lipColor,
4221
+ blush: e2.blush,
4222
+ blushColor: e2.blushColor,
4223
+ iris: e2.iris,
4224
+ irisColor: e2.irisColor,
4225
+ grade: e2.grade,
4226
+ chromaKey: e2.chromaKey,
4227
+ keyColor: e2.keyColor,
4228
+ keyTolerance: e2.keyTolerance,
4229
+ autoLight: e2.autoLight,
4230
+ mirror: e2.mirror,
4193
4231
  slimFace: e2.slimFace,
4194
- enlargeEyes: e2.enlargeEyes
4232
+ enlargeEyes: e2.enlargeEyes,
4233
+ noseSlim: e2.noseSlim,
4234
+ chin: e2.chin
4195
4235
  };
4196
4236
  }
4197
4237
 
@@ -4209,7 +4249,9 @@
4209
4249
  needsMask: false,
4210
4250
  faceFound: false,
4211
4251
  faceSize: 0,
4212
- needsFace: false
4252
+ needsFace: false,
4253
+ autoExposure: 0,
4254
+ faces: 0
4213
4255
  };
4214
4256
  var Engine = class _Engine {
4215
4257
  canvas;
@@ -4221,6 +4263,8 @@
4221
4263
  // image background is on costs a fetch plus two createImageBitmap calls, which is a
4222
4264
  // visible stutter under a dragged finger and the exact case setRecipe exists to make cheap.
4223
4265
  backgroundPath = null;
4266
+ /** The LUT the core holds, by path, so a slider drag does not decode it again. */
4267
+ lutPath = "";
4224
4268
  closed = false;
4225
4269
  /**
4226
4270
  * The segmenter loads LAZILY - only once a recipe actually needs a person mask.
@@ -4258,11 +4302,16 @@
4258
4302
  canvas.height = height;
4259
4303
  return _Engine.on(canvas, filter, effects);
4260
4304
  }
4261
- static async on(canvas, filter, effects = NO_EFFECTS) {
4305
+ /**
4306
+ * `background`: see {@link Engine.setRecipe}. `alpha` keeps the output's alpha channel, which
4307
+ * only a `cutout` background writes; it is fixed for the life of the context, so a host that
4308
+ * may switch to `cutout` later has to ask for it here.
4309
+ */
4310
+ static async on(canvas, filter, effects = NO_EFFECTS, background, alpha = false) {
4262
4311
  const width = canvas.width;
4263
4312
  const height = canvas.height;
4264
4313
  const gl = canvas.getContext("webgl2", {
4265
- alpha: false,
4314
+ alpha,
4266
4315
  premultipliedAlpha: false,
4267
4316
  antialias: false,
4268
4317
  depth: false,
@@ -4276,7 +4325,7 @@
4276
4325
  const err = core.init(gl, width, height);
4277
4326
  if (err) throw new Error(`mediakit: ${err}`);
4278
4327
  engine2 = new _Engine(canvas, gl, core);
4279
- await engine2.setRecipe(filter, effects);
4328
+ await engine2.setRecipe(filter, effects, background);
4280
4329
  return engine2;
4281
4330
  } catch (err) {
4282
4331
  try {
@@ -4293,13 +4342,21 @@
4293
4342
  get outputCanvas() {
4294
4343
  return this.canvas;
4295
4344
  }
4296
- /** Change the recipe without rebuilding the pipeline, so dragging a slider does not flash. */
4297
- async setRecipe(filter, effects = this.effects) {
4345
+ /**
4346
+ * Change the recipe without rebuilding the pipeline, so dragging a slider does not flash.
4347
+ *
4348
+ * `background` is `filter.imagePath` already decoded, and the Engine takes ownership of it.
4349
+ * A worker needs it: there is no `Image` there, so an SVG - every background meet bundles -
4350
+ * cannot be decoded at all, and the main thread has to do it and transfer the bitmap.
4351
+ */
4352
+ async setRecipe(filter, effects = this.effects, background) {
4298
4353
  if (this.closed) return;
4299
4354
  this.filter = filter;
4300
4355
  this.effects = effects;
4301
- if (filter.kind === "image" && filter.imagePath !== this.backgroundPath) {
4302
- const bmp = await loadBitmap(filter.imagePath, this.canvas.width, this.canvas.height);
4356
+ if (filter.kind === "image" && (background || filter.imagePath !== this.backgroundPath)) {
4357
+ const w2 = this.canvas.width;
4358
+ const h2 = this.canvas.height;
4359
+ const bmp = background ? await coverCrop(background, w2, h2) : await loadBitmap(filter.imagePath, w2, h2);
4303
4360
  if (this.closed) {
4304
4361
  bmp.close();
4305
4362
  return;
@@ -4308,20 +4365,41 @@
4308
4365
  this.background = bmp;
4309
4366
  this.backgroundPath = filter.imagePath;
4310
4367
  }
4368
+ if (effects.lut !== this.lutPath) {
4369
+ if (effects.lut) {
4370
+ let bmp = null;
4371
+ try {
4372
+ bmp = await decodeImage(effects.lut, { colorSpaceConversion: "none", premultiplyAlpha: "none" });
4373
+ } catch (e2) {
4374
+ console.error("mediakit: LUT failed to load", e2);
4375
+ }
4376
+ if (this.closed || this.filter !== filter || this.effects !== effects) {
4377
+ bmp?.close();
4378
+ return;
4379
+ }
4380
+ this.core.setLut(bmp);
4381
+ bmp?.close();
4382
+ } else {
4383
+ this.core.setLut(null);
4384
+ }
4385
+ this.lutPath = effects.lut;
4386
+ }
4311
4387
  this.core.setRecipe(coreRecipe(filter, effects));
4312
- if (needsMask(filter, effects)) void this.ensureSegmenter();
4388
+ if (needsSegmentation(filter, effects)) void this.ensureSegmenter();
4313
4389
  if (needsFace(effects)) void this.ensureFace();
4314
4390
  }
4315
4391
  ensureFace() {
4316
4392
  if (this.face || this.closed) return Promise.resolve();
4317
4393
  if (this.faceLoading) return this.faceLoading;
4318
- this.faceLoading = createFaceDetector(this.canvas).then((f2) => {
4394
+ const buf = this.core.faceBuffer();
4395
+ if (!buf) return Promise.resolve();
4396
+ this.faceLoading = createFaceDetector(this.canvas, Math.max(1, Math.floor(buf.length / FACE_FLOATS))).then((f2) => {
4319
4397
  if (this.closed) {
4320
4398
  f2.close();
4321
4399
  return;
4322
4400
  }
4323
4401
  this.face = f2;
4324
- this.lmBuf = this.core.faceBuffer();
4402
+ this.lmBuf = buf;
4325
4403
  }).catch((e2) => {
4326
4404
  console.error("mediakit: face detector failed to load", e2);
4327
4405
  }).finally(() => {
@@ -4400,7 +4478,9 @@
4400
4478
  needsMask: s2 ? s2[7] > 0.5 : false,
4401
4479
  faceFound: s2 ? s2[8] > 0.5 : false,
4402
4480
  faceSize: s2 ? s2[9] : 0,
4403
- needsFace: s2 ? s2[10] > 0.5 : false
4481
+ needsFace: s2 ? s2[10] > 0.5 : false,
4482
+ autoExposure: s2 ? s2[11] : 0,
4483
+ faces: s2 ? s2[12] : 0
4404
4484
  };
4405
4485
  return glCalls > 0;
4406
4486
  }
@@ -4428,16 +4508,17 @@
4428
4508
  this.background?.close();
4429
4509
  this.background = null;
4430
4510
  this.backgroundPath = null;
4511
+ this.lutPath = "";
4431
4512
  this.gl.getExtension("WEBGL_lose_context")?.loseContext();
4432
4513
  }
4433
4514
  };
4434
- async function decodeImage(path) {
4515
+ async function decodeImage(path, opts) {
4435
4516
  if (typeof Image === "undefined") {
4436
4517
  const res = await fetch(path);
4437
- if (!res.ok) throw new Error(`mediakit: cannot load background image ${path} (HTTP ${res.status})`);
4518
+ if (!res.ok) throw new Error(`mediakit: cannot load image ${path} (HTTP ${res.status})`);
4438
4519
  const blob = await res.blob();
4439
4520
  try {
4440
- return await createImageBitmap(blob);
4521
+ return await createImageBitmap(blob, opts);
4441
4522
  } catch (err) {
4442
4523
  if (blob.type.includes("svg")) {
4443
4524
  throw new Error(
@@ -4459,10 +4540,12 @@
4459
4540
  `mediakit: background image ${path} has no intrinsic size - an SVG needs width and height, a viewBox alone is not enough`
4460
4541
  );
4461
4542
  }
4462
- return createImageBitmap(img);
4543
+ return createImageBitmap(img, opts);
4463
4544
  }
4464
4545
  async function loadBitmap(path, w2, h2) {
4465
- const src = await decodeImage(path);
4546
+ return coverCrop(await decodeImage(path), w2, h2);
4547
+ }
4548
+ async function coverCrop(src, w2, h2) {
4466
4549
  if (src.width * h2 === src.height * w2) return src;
4467
4550
  const scale = Math.max(w2 / src.width, h2 / src.height);
4468
4551
  const cw = Math.min(src.width, Math.round(w2 / scale));
@@ -4485,7 +4568,7 @@
4485
4568
  var abort = null;
4486
4569
  async function start(m2) {
4487
4570
  const canvas = new OffscreenCanvas(m2.width, m2.height);
4488
- engine = await Engine.on(canvas, m2.filter, m2.effects);
4571
+ engine = await Engine.on(canvas, m2.filter, m2.effects, m2.background);
4489
4572
  abort = new AbortController();
4490
4573
  let n2 = 0;
4491
4574
  const transform = new TransformStream({
@@ -4526,7 +4609,9 @@
4526
4609
  post({ t: "error", message: err instanceof Error ? err.message : String(err) });
4527
4610
  });
4528
4611
  } else if (m2.t === "recipe") {
4529
- void engine?.setRecipe(m2.filter, m2.effects ?? NO_EFFECTS);
4612
+ engine?.setRecipe(m2.filter, m2.effects ?? NO_EFFECTS, m2.background).catch((err) => {
4613
+ post({ t: "error", message: err instanceof Error ? err.message : String(err) });
4614
+ });
4530
4615
  } else {
4531
4616
  stop();
4532
4617
  }