@tradalab/mediakit 1.0.0 → 1.1.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/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,8 +4158,20 @@
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
+ autoLight: false,
4158
4171
  slimFace: 0,
4159
- enlargeEyes: 0
4172
+ enlargeEyes: 0,
4173
+ noseSlim: 0,
4174
+ chin: 0
4160
4175
  };
4161
4176
  var MODE_NONE = 0;
4162
4177
  var MODE_BLUR = 1;
@@ -4175,7 +4190,7 @@
4175
4190
  return f2.kind !== "none" || e2.beautify > 0 || e2.autoframe;
4176
4191
  }
4177
4192
  function needsFace(e2) {
4178
- return e2.teeth > 0 || e2.eyeBright > 0 || e2.underEye > 0 || e2.slimFace > 0 || e2.enlargeEyes > 0;
4193
+ 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
4194
  }
4180
4195
  function coreRecipe(f2, e2) {
4181
4196
  return {
@@ -4190,8 +4205,19 @@
4190
4205
  teeth: e2.teeth,
4191
4206
  eyeBright: e2.eyeBright,
4192
4207
  underEye: e2.underEye,
4208
+ smileLines: e2.smileLines,
4209
+ lips: e2.lips,
4210
+ lipColor: e2.lipColor,
4211
+ blush: e2.blush,
4212
+ blushColor: e2.blushColor,
4213
+ iris: e2.iris,
4214
+ irisColor: e2.irisColor,
4215
+ grade: e2.grade,
4216
+ autoLight: e2.autoLight,
4193
4217
  slimFace: e2.slimFace,
4194
- enlargeEyes: e2.enlargeEyes
4218
+ enlargeEyes: e2.enlargeEyes,
4219
+ noseSlim: e2.noseSlim,
4220
+ chin: e2.chin
4195
4221
  };
4196
4222
  }
4197
4223
 
@@ -4209,7 +4235,9 @@
4209
4235
  needsMask: false,
4210
4236
  faceFound: false,
4211
4237
  faceSize: 0,
4212
- needsFace: false
4238
+ needsFace: false,
4239
+ autoExposure: 0,
4240
+ faces: 0
4213
4241
  };
4214
4242
  var Engine = class _Engine {
4215
4243
  canvas;
@@ -4221,6 +4249,8 @@
4221
4249
  // image background is on costs a fetch plus two createImageBitmap calls, which is a
4222
4250
  // visible stutter under a dragged finger and the exact case setRecipe exists to make cheap.
4223
4251
  backgroundPath = null;
4252
+ /** The LUT the core holds, by path, so a slider drag does not decode it again. */
4253
+ lutPath = "";
4224
4254
  closed = false;
4225
4255
  /**
4226
4256
  * The segmenter loads LAZILY - only once a recipe actually needs a person mask.
@@ -4258,7 +4288,8 @@
4258
4288
  canvas.height = height;
4259
4289
  return _Engine.on(canvas, filter, effects);
4260
4290
  }
4261
- static async on(canvas, filter, effects = NO_EFFECTS) {
4291
+ /** `background`: see {@link Engine.setRecipe}. */
4292
+ static async on(canvas, filter, effects = NO_EFFECTS, background) {
4262
4293
  const width = canvas.width;
4263
4294
  const height = canvas.height;
4264
4295
  const gl = canvas.getContext("webgl2", {
@@ -4276,7 +4307,7 @@
4276
4307
  const err = core.init(gl, width, height);
4277
4308
  if (err) throw new Error(`mediakit: ${err}`);
4278
4309
  engine2 = new _Engine(canvas, gl, core);
4279
- await engine2.setRecipe(filter, effects);
4310
+ await engine2.setRecipe(filter, effects, background);
4280
4311
  return engine2;
4281
4312
  } catch (err) {
4282
4313
  try {
@@ -4293,13 +4324,21 @@
4293
4324
  get outputCanvas() {
4294
4325
  return this.canvas;
4295
4326
  }
4296
- /** Change the recipe without rebuilding the pipeline, so dragging a slider does not flash. */
4297
- async setRecipe(filter, effects = this.effects) {
4327
+ /**
4328
+ * Change the recipe without rebuilding the pipeline, so dragging a slider does not flash.
4329
+ *
4330
+ * `background` is `filter.imagePath` already decoded, and the Engine takes ownership of it.
4331
+ * A worker needs it: there is no `Image` there, so an SVG - every background meet bundles -
4332
+ * cannot be decoded at all, and the main thread has to do it and transfer the bitmap.
4333
+ */
4334
+ async setRecipe(filter, effects = this.effects, background) {
4298
4335
  if (this.closed) return;
4299
4336
  this.filter = filter;
4300
4337
  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);
4338
+ if (filter.kind === "image" && (background || filter.imagePath !== this.backgroundPath)) {
4339
+ const w2 = this.canvas.width;
4340
+ const h2 = this.canvas.height;
4341
+ const bmp = background ? await coverCrop(background, w2, h2) : await loadBitmap(filter.imagePath, w2, h2);
4303
4342
  if (this.closed) {
4304
4343
  bmp.close();
4305
4344
  return;
@@ -4308,6 +4347,25 @@
4308
4347
  this.background = bmp;
4309
4348
  this.backgroundPath = filter.imagePath;
4310
4349
  }
4350
+ if (effects.lut !== this.lutPath) {
4351
+ if (effects.lut) {
4352
+ let bmp = null;
4353
+ try {
4354
+ bmp = await decodeImage(effects.lut, { colorSpaceConversion: "none", premultiplyAlpha: "none" });
4355
+ } catch (e2) {
4356
+ console.error("mediakit: LUT failed to load", e2);
4357
+ }
4358
+ if (this.closed || this.filter !== filter || this.effects !== effects) {
4359
+ bmp?.close();
4360
+ return;
4361
+ }
4362
+ this.core.setLut(bmp);
4363
+ bmp?.close();
4364
+ } else {
4365
+ this.core.setLut(null);
4366
+ }
4367
+ this.lutPath = effects.lut;
4368
+ }
4311
4369
  this.core.setRecipe(coreRecipe(filter, effects));
4312
4370
  if (needsMask(filter, effects)) void this.ensureSegmenter();
4313
4371
  if (needsFace(effects)) void this.ensureFace();
@@ -4315,13 +4373,15 @@
4315
4373
  ensureFace() {
4316
4374
  if (this.face || this.closed) return Promise.resolve();
4317
4375
  if (this.faceLoading) return this.faceLoading;
4318
- this.faceLoading = createFaceDetector(this.canvas).then((f2) => {
4376
+ const buf = this.core.faceBuffer();
4377
+ if (!buf) return Promise.resolve();
4378
+ this.faceLoading = createFaceDetector(this.canvas, Math.max(1, Math.floor(buf.length / FACE_FLOATS))).then((f2) => {
4319
4379
  if (this.closed) {
4320
4380
  f2.close();
4321
4381
  return;
4322
4382
  }
4323
4383
  this.face = f2;
4324
- this.lmBuf = this.core.faceBuffer();
4384
+ this.lmBuf = buf;
4325
4385
  }).catch((e2) => {
4326
4386
  console.error("mediakit: face detector failed to load", e2);
4327
4387
  }).finally(() => {
@@ -4400,7 +4460,9 @@
4400
4460
  needsMask: s2 ? s2[7] > 0.5 : false,
4401
4461
  faceFound: s2 ? s2[8] > 0.5 : false,
4402
4462
  faceSize: s2 ? s2[9] : 0,
4403
- needsFace: s2 ? s2[10] > 0.5 : false
4463
+ needsFace: s2 ? s2[10] > 0.5 : false,
4464
+ autoExposure: s2 ? s2[11] : 0,
4465
+ faces: s2 ? s2[12] : 0
4404
4466
  };
4405
4467
  return glCalls > 0;
4406
4468
  }
@@ -4428,16 +4490,17 @@
4428
4490
  this.background?.close();
4429
4491
  this.background = null;
4430
4492
  this.backgroundPath = null;
4493
+ this.lutPath = "";
4431
4494
  this.gl.getExtension("WEBGL_lose_context")?.loseContext();
4432
4495
  }
4433
4496
  };
4434
- async function decodeImage(path) {
4497
+ async function decodeImage(path, opts) {
4435
4498
  if (typeof Image === "undefined") {
4436
4499
  const res = await fetch(path);
4437
- if (!res.ok) throw new Error(`mediakit: cannot load background image ${path} (HTTP ${res.status})`);
4500
+ if (!res.ok) throw new Error(`mediakit: cannot load image ${path} (HTTP ${res.status})`);
4438
4501
  const blob = await res.blob();
4439
4502
  try {
4440
- return await createImageBitmap(blob);
4503
+ return await createImageBitmap(blob, opts);
4441
4504
  } catch (err) {
4442
4505
  if (blob.type.includes("svg")) {
4443
4506
  throw new Error(
@@ -4459,10 +4522,12 @@
4459
4522
  `mediakit: background image ${path} has no intrinsic size - an SVG needs width and height, a viewBox alone is not enough`
4460
4523
  );
4461
4524
  }
4462
- return createImageBitmap(img);
4525
+ return createImageBitmap(img, opts);
4463
4526
  }
4464
4527
  async function loadBitmap(path, w2, h2) {
4465
- const src = await decodeImage(path);
4528
+ return coverCrop(await decodeImage(path), w2, h2);
4529
+ }
4530
+ async function coverCrop(src, w2, h2) {
4466
4531
  if (src.width * h2 === src.height * w2) return src;
4467
4532
  const scale = Math.max(w2 / src.width, h2 / src.height);
4468
4533
  const cw = Math.min(src.width, Math.round(w2 / scale));
@@ -4485,7 +4550,7 @@
4485
4550
  var abort = null;
4486
4551
  async function start(m2) {
4487
4552
  const canvas = new OffscreenCanvas(m2.width, m2.height);
4488
- engine = await Engine.on(canvas, m2.filter, m2.effects);
4553
+ engine = await Engine.on(canvas, m2.filter, m2.effects, m2.background);
4489
4554
  abort = new AbortController();
4490
4555
  let n2 = 0;
4491
4556
  const transform = new TransformStream({
@@ -4526,7 +4591,9 @@
4526
4591
  post({ t: "error", message: err instanceof Error ? err.message : String(err) });
4527
4592
  });
4528
4593
  } else if (m2.t === "recipe") {
4529
- void engine?.setRecipe(m2.filter, m2.effects ?? NO_EFFECTS);
4594
+ engine?.setRecipe(m2.filter, m2.effects ?? NO_EFFECTS, m2.background).catch((err) => {
4595
+ post({ t: "error", message: err instanceof Error ? err.message : String(err) });
4596
+ });
4530
4597
  } else {
4531
4598
  stop();
4532
4599
  }