@tradalab/mediakit 2.1.1 → 2.4.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.
Binary file
package/dist/recipe.d.ts CHANGED
@@ -33,6 +33,10 @@ type BgFilter = {
33
33
  /** A colour as red, green, blue, each 0..1. */
34
34
  type Rgb = [number, number, number];
35
35
  /** Where smoothing (`beautify`) may land. */
36
+ /** An inner layer drawn instead of the picture, for tuning: skin weights, the mask, the tracked faces. */
37
+ type DebugView = "off" | "skin" | "mask" | "face";
38
+ declare const DEBUG_VIEWS: readonly DebugView[];
39
+ declare function debugCode(view: DebugView | undefined): number;
36
40
  type SkinScope = "person" | "face";
37
41
  /** Where the colour LUT lands. */
38
42
  type GradeScope = "frame" | "person" | "skin";
@@ -326,4 +330,4 @@ interface CoreRecipe {
326
330
  }
327
331
  declare function coreRecipe(f: BgFilter, e: Effects): CoreRecipe;
328
332
 
329
- export { type BgFilter, type CoreRecipe, EFFECTS_SCHEMA, type Effects, type FieldSpec, GRADE_SCOPE, type GradeScope, MAKEUP_BLEND, MODE_BLUR, MODE_CUTOUT, MODE_IMAGE, MODE_NONE, NO_EFFECTS, type Rgb, SKIN_SCOPE, type SkinScope, TOUCH_UP, backgroundMode, coreRecipe, needsFace, needsMask, needsSegmentation, needsSkinClasses, normalizeEffects, touchUp, touchUpAmount };
333
+ export { type BgFilter, type CoreRecipe, DEBUG_VIEWS, type DebugView, EFFECTS_SCHEMA, type Effects, type FieldSpec, GRADE_SCOPE, type GradeScope, MAKEUP_BLEND, MODE_BLUR, MODE_CUTOUT, MODE_IMAGE, MODE_NONE, NO_EFFECTS, type Rgb, SKIN_SCOPE, type SkinScope, TOUCH_UP, backgroundMode, coreRecipe, debugCode, needsFace, needsMask, needsSegmentation, needsSkinClasses, normalizeEffects, touchUp, touchUpAmount };
package/dist/recipe.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import {
2
+ DEBUG_VIEWS,
2
3
  EFFECTS_SCHEMA,
3
4
  GRADE_SCOPE,
4
5
  MAKEUP_BLEND,
@@ -11,6 +12,7 @@ import {
11
12
  TOUCH_UP,
12
13
  backgroundMode,
13
14
  coreRecipe,
15
+ debugCode,
14
16
  needsFace,
15
17
  needsMask,
16
18
  needsSegmentation,
@@ -18,8 +20,9 @@ import {
18
20
  normalizeEffects,
19
21
  touchUp,
20
22
  touchUpAmount
21
- } from "./chunk-TBMYRKXR.js";
23
+ } from "./chunk-YW75KJJO.js";
22
24
  export {
25
+ DEBUG_VIEWS,
23
26
  EFFECTS_SCHEMA,
24
27
  GRADE_SCOPE,
25
28
  MAKEUP_BLEND,
@@ -32,6 +35,7 @@ export {
32
35
  TOUCH_UP,
33
36
  backgroundMode,
34
37
  coreRecipe,
38
+ debugCode,
35
39
  needsFace,
36
40
  needsMask,
37
41
  needsSegmentation,
package/dist/worker.js CHANGED
@@ -4783,9 +4783,13 @@
4783
4783
  initMask(model) {
4784
4784
  return this.init("mask", { t: "initMask", model });
4785
4785
  }
4786
+ /** Whether `request` would take a frame of this kind now. */
4787
+ accepts(kind) {
4788
+ return this.worker !== null && this.up.has(kind) && !this.busy.has(kind);
4789
+ }
4786
4790
  /** Transfers the frame. False when that kind is busy or not up; the frame is then closed. */
4787
4791
  request(kind, frame, tsUs) {
4788
- if (!this.worker || !this.up.has(kind) || this.busy.has(kind)) {
4792
+ if (!this.accepts(kind)) {
4789
4793
  frame.close();
4790
4794
  return false;
4791
4795
  }
@@ -4931,6 +4935,10 @@
4931
4935
  }
4932
4936
 
4933
4937
  // src/recipe.ts
4938
+ var DEBUG_VIEWS = ["off", "skin", "mask", "face"];
4939
+ function debugCode(view) {
4940
+ return Math.max(0, DEBUG_VIEWS.indexOf(view ?? "off"));
4941
+ }
4934
4942
  var NO_EFFECTS = {
4935
4943
  beautify: 0,
4936
4944
  exposure: 0,
@@ -5013,11 +5021,11 @@
5013
5021
  return needsMask(f2, e2) && !e2.chromaKey;
5014
5022
  }
5015
5023
  function needsSkinClasses(_f, e2) {
5016
- return (e2.whiten ?? 0) > 0 || (e2.skinLight ?? 0) > 0 || e2.grade > 0 && (e2.gradeScope ?? "frame") !== "frame";
5024
+ return e2.grade > 0 && (e2.gradeScope ?? "frame") !== "frame";
5017
5025
  }
5018
5026
  function needsFace(e2) {
5019
5027
  return e2.teeth > 0 || e2.eyeBright > 0 || e2.underEye > 0 || e2.smileLines > 0 || e2.lips > 0 || e2.blush > 0 || e2.iris > 0 || (e2.eyeShadow ?? 0) > 0 || (e2.lipGloss ?? 0) > 0 || (e2.rosy ?? 0) > 0 || e2.skinScope === "face" && (e2.beautify > 0 || hasSkin(e2)) || // Whitening and skin light measure skin on the face; clarity is held to it.
5020
- hasSkin(e2) || e2.grade > 0 && e2.gradeScope === "skin" || e2.slimFace > 0 || e2.enlargeEyes > 0 || e2.noseSlim > 0 || e2.chin !== 0 || (e2.jaw ?? 0) > 0 || (e2.cheekbone ?? 0) > 0 || (e2.forehead ?? 0) !== 0 || (e2.brow ?? 0) > 0 || (e2.contour ?? 0) > 0 || (e2.highlight ?? 0) > 0 || (e2.makeup ?? 0) > 0 && !!e2.makeupTexture;
5028
+ hasSkin(e2) || e2.grade > 0 && (e2.gradeScope ?? "frame") !== "frame" || e2.slimFace > 0 || e2.enlargeEyes > 0 || e2.noseSlim > 0 || e2.chin !== 0 || (e2.jaw ?? 0) > 0 || (e2.cheekbone ?? 0) > 0 || (e2.forehead ?? 0) !== 0 || (e2.brow ?? 0) > 0 || (e2.contour ?? 0) > 0 || (e2.highlight ?? 0) > 0 || (e2.makeup ?? 0) > 0 && !!e2.makeupTexture;
5021
5029
  }
5022
5030
  function coreRecipe(f2, e2) {
5023
5031
  return {
@@ -5092,8 +5100,19 @@
5092
5100
  faceSize: 0,
5093
5101
  needsFace: false,
5094
5102
  autoExposure: 0,
5095
- faces: 0
5096
- };
5103
+ faces: 0,
5104
+ heldFrames: 0,
5105
+ faceAgeMs: 0,
5106
+ maskAgeMs: 0
5107
+ };
5108
+ var MAX_HOLD = 2;
5109
+ var DEPTH_VOTES = 15;
5110
+ function sourceSize(source) {
5111
+ if (source instanceof VideoFrame) return [source.displayWidth, source.displayHeight];
5112
+ if (typeof HTMLVideoElement !== "undefined" && source instanceof HTMLVideoElement) return [source.videoWidth, source.videoHeight];
5113
+ const s2 = source;
5114
+ return [s2.width, s2.height];
5115
+ }
5097
5116
  var Engine = class _Engine {
5098
5117
  canvas;
5099
5118
  gl;
@@ -5135,13 +5154,35 @@
5135
5154
  // stalls the picture for that long. `face` is the fallback when no worker can be made.
5136
5155
  faceRemote = false;
5137
5156
  faceWorkerMs = 0;
5138
- // The multiclass segmenter in that same worker: its mask comes back as bytes, uploaded here.
5139
5157
  classRemote = false;
5140
5158
  classMaskTex = null;
5141
5159
  classMaskFresh = false;
5160
+ classMaskTs = 0;
5161
+ syncFaces = false;
5162
+ renderedTs = 0;
5163
+ queue = [];
5164
+ spare = [];
5165
+ holdDepth = 1;
5166
+ depthVotes = 0;
5167
+ gapUs = 33333;
5168
+ prevTs = -1;
5169
+ sent = /* @__PURE__ */ new Map();
5170
+ faceLatencyMs = 0;
5171
+ maskLatencyMs = /* @__PURE__ */ new Map();
5172
+ faceTs = -1;
5173
+ maskUsedTs = -1;
5174
+ lastHeld = null;
5175
+ swallowed = false;
5142
5176
  maskWorkerMs = 0;
5143
- detect = null;
5177
+ detects = /* @__PURE__ */ new Map();
5178
+ personRemote = false;
5179
+ personLoading = null;
5180
+ personMaskTex = null;
5181
+ personMaskFresh = false;
5182
+ personMaskTs = 0;
5183
+ personWorkerMs = 0;
5144
5184
  detectCanvas = null;
5185
+ detectCanvasTs = -1;
5145
5186
  // A kind whose worker failed once stays in-thread.
5146
5187
  inThread = /* @__PURE__ */ new Set();
5147
5188
  faceLoading = null;
@@ -5211,6 +5252,11 @@
5211
5252
  get outputCanvas() {
5212
5253
  return this.canvas;
5213
5254
  }
5255
+ /** Draws an inner layer instead of the picture until set back to "off". */
5256
+ setDebug(view) {
5257
+ if (this.closed) return;
5258
+ this.core.setDebug(debugCode(view));
5259
+ }
5214
5260
  /**
5215
5261
  * Change the recipe without rebuilding the pipeline, so dragging a slider does not flash.
5216
5262
  *
@@ -5291,6 +5337,7 @@
5291
5337
  this.core.setRecipe(coreRecipe(filter, effects));
5292
5338
  if (needsSegmentation(filter, effects)) void this.ensureSegmenter();
5293
5339
  if (needsSkinClasses(filter, effects)) void this.ensureClassSegmenter();
5340
+ if (this.personRemoteWanted(filter, effects)) void this.ensurePersonRemote();
5294
5341
  if (needsFace(effects)) void this.ensureFace();
5295
5342
  }
5296
5343
  ensureFace() {
@@ -5320,27 +5367,72 @@
5320
5367
  return this.faceLoading;
5321
5368
  }
5322
5369
  /** The detect worker for `kind`, made on first use; rejects when that kind must stay in-thread. */
5370
+ personRemoteWanted(filter, effects) {
5371
+ return filter.kind === "none" && needsSegmentation(filter, effects) && !needsSkinClasses(filter, effects);
5372
+ }
5373
+ ensurePersonRemote() {
5374
+ if (this.personRemote || this.closed) return Promise.resolve();
5375
+ this.personLoading ??= this.remote("person", (d2) => d2.initMask("person")).then(() => {
5376
+ this.personRemote = true;
5377
+ }).catch(() => {
5378
+ }).finally(() => {
5379
+ this.personLoading = null;
5380
+ });
5381
+ return this.personLoading;
5382
+ }
5323
5383
  async remote(kind, init) {
5324
5384
  if (this.inThread.has(kind) || typeof Worker === "undefined" || typeof VideoFrame === "undefined") {
5325
5385
  throw new Error("in-thread");
5326
5386
  }
5327
- this.detect ??= new DetectWorker({
5387
+ let detect = this.detects.get(kind);
5388
+ if (!detect) {
5389
+ detect = new DetectWorker(this.detectHandlers(kind));
5390
+ this.detects.set(kind, detect);
5391
+ }
5392
+ try {
5393
+ await init(detect);
5394
+ } catch (e2) {
5395
+ this.inThread.add(kind);
5396
+ throw e2;
5397
+ }
5398
+ if (this.closed) throw new Error("closed");
5399
+ }
5400
+ detectHandlers(key) {
5401
+ return {
5328
5402
  face: (tsUs, n2, lm, ms2) => {
5329
5403
  if (this.closed || !this.lmBuf) return;
5330
5404
  this.lmBuf.set(lm);
5331
5405
  const [lo2, hi2] = splitMicros(tsUs);
5332
5406
  this.core.setFace(lo2, hi2, n2);
5333
5407
  this.faceWorkerMs = ms2;
5408
+ this.faceTs = tsUs;
5409
+ const took = this.took("face", tsUs);
5410
+ if (took !== null) this.faceLatencyMs = this.faceLatencyMs ? this.faceLatencyMs + (took - this.faceLatencyMs) * 0.2 : took;
5334
5411
  },
5335
- mask: (_tsUs, width, height, data, ms2) => {
5412
+ mask: (tsUs, width, height, data, ms2) => {
5336
5413
  if (this.closed) return;
5414
+ const took = this.took(key, tsUs);
5415
+ if (took !== null) {
5416
+ const was = this.maskLatencyMs.get(key);
5417
+ this.maskLatencyMs.set(key, was ? was + (took - was) * 0.2 : took);
5418
+ }
5419
+ if (key === "person") {
5420
+ this.personMaskTex = this.uploadMaskBytes(this.personMaskTex, width, height, data);
5421
+ this.personMaskFresh = true;
5422
+ this.personMaskTs = tsUs;
5423
+ this.personWorkerMs = ms2;
5424
+ return;
5425
+ }
5337
5426
  this.uploadClassMask(width, height, data);
5427
+ this.classMaskTs = tsUs;
5338
5428
  this.maskWorkerMs = ms2;
5339
5429
  },
5340
5430
  failed: (k2, reason) => {
5341
- console.warn(`mediakit: ${k2} detection moves into this thread:`, reason);
5342
- this.inThread.add(k2);
5343
- if (k2 === "face") {
5431
+ console.warn(`mediakit: ${key} detection moves into this thread:`, reason);
5432
+ this.inThread.add(key);
5433
+ if (key === "person") {
5434
+ this.personRemote = false;
5435
+ } else if (k2 === "face") {
5344
5436
  this.faceRemote = false;
5345
5437
  void this.ensureFace();
5346
5438
  } else {
@@ -5348,53 +5440,64 @@
5348
5440
  void this.ensureClassSegmenter();
5349
5441
  }
5350
5442
  }
5351
- });
5352
- try {
5353
- await init(this.detect);
5354
- } catch (e2) {
5355
- this.inThread.add(kind);
5356
- throw e2;
5357
- }
5358
- if (this.closed) throw new Error("closed");
5443
+ };
5359
5444
  }
5360
- // Category indices: NEAREST, since a blend of two indices is a third, wrong category.
5361
5445
  uploadClassMask(width, height, data) {
5446
+ this.classMaskTex = this.uploadMaskBytes(this.classMaskTex, width, height, data);
5447
+ this.classMaskFresh = true;
5448
+ }
5449
+ // Category indices: NEAREST, since a blend of two indices is a third, wrong category.
5450
+ uploadMaskBytes(tex, width, height, data) {
5362
5451
  const gl = this.gl;
5363
- if (!this.classMaskTex) {
5364
- this.classMaskTex = gl.createTexture();
5365
- gl.bindTexture(gl.TEXTURE_2D, this.classMaskTex);
5452
+ if (!tex) {
5453
+ tex = gl.createTexture();
5454
+ gl.bindTexture(gl.TEXTURE_2D, tex);
5366
5455
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
5367
5456
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
5368
5457
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
5369
5458
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
5370
5459
  }
5371
- gl.bindTexture(gl.TEXTURE_2D, this.classMaskTex);
5460
+ gl.bindTexture(gl.TEXTURE_2D, tex);
5372
5461
  gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
5373
5462
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.R8, width, height, 0, gl.RED, gl.UNSIGNED_BYTE, data);
5374
5463
  gl.pixelStorei(gl.UNPACK_ALIGNMENT, 4);
5375
5464
  gl.bindTexture(gl.TEXTURE_2D, null);
5376
- this.classMaskFresh = true;
5465
+ return tex;
5377
5466
  }
5378
5467
  /**
5379
5468
  * Sends the detect worker a downscaled copy, never a clone: a camera frame held through a slow
5380
5469
  * detection drains Chrome's few capture buffers and throttles the camera itself.
5381
5470
  */
5382
5471
  requestRemote(kind, source, timestampUs) {
5472
+ const detect = this.detects.get(kind);
5473
+ if (!detect?.accepts(kind === "face" ? "face" : "mask")) return;
5383
5474
  let frame = null;
5384
5475
  try {
5385
- frame = new VideoFrame(source instanceof VideoFrame ? this.detectCopy(source) : source, {
5476
+ const copyable = source instanceof VideoFrame || source instanceof OffscreenCanvas;
5477
+ frame = new VideoFrame(copyable ? this.detectCopy(source, timestampUs) : source, {
5386
5478
  timestamp: timestampUs
5387
5479
  });
5388
5480
  } catch {
5389
5481
  }
5390
- if (frame) this.detect?.request(kind, frame, timestampUs);
5391
- }
5392
- detectCopy(source) {
5393
- const s2 = Math.min(1, 512 / Math.max(source.displayWidth, source.displayHeight));
5394
- const w2 = Math.max(1, Math.round(source.displayWidth * s2));
5395
- const h2 = Math.max(1, Math.round(source.displayHeight * s2));
5396
- if (!this.detectCanvas || this.detectCanvas.width !== w2 || this.detectCanvas.height !== h2) this.detectCanvas = new OffscreenCanvas(w2, h2);
5397
- this.detectCanvas.getContext("2d").drawImage(source, 0, 0, w2, h2);
5482
+ if (frame && detect.request(kind === "face" ? "face" : "mask", frame, timestampUs)) {
5483
+ this.sent.set(kind, { ts: timestampUs, at: performance.now() });
5484
+ }
5485
+ }
5486
+ /** Face and mask asked on the same frame share one draw. */
5487
+ detectCopy(source, timestampUs) {
5488
+ const sw = source instanceof VideoFrame ? source.displayWidth : source.width;
5489
+ const sh2 = source instanceof VideoFrame ? source.displayHeight : source.height;
5490
+ const s2 = Math.min(1, 512 / Math.max(sw, sh2));
5491
+ const w2 = Math.max(1, Math.round(sw * s2));
5492
+ const h2 = Math.max(1, Math.round(sh2 * s2));
5493
+ if (!this.detectCanvas || this.detectCanvas.width !== w2 || this.detectCanvas.height !== h2) {
5494
+ this.detectCanvas = new OffscreenCanvas(w2, h2);
5495
+ this.detectCanvasTs = -1;
5496
+ }
5497
+ if (this.detectCanvasTs !== timestampUs) {
5498
+ this.detectCanvas.getContext("2d").drawImage(source, 0, 0, w2, h2);
5499
+ this.detectCanvasTs = timestampUs;
5500
+ }
5398
5501
  return this.detectCanvas;
5399
5502
  }
5400
5503
  ensureClassSegmenter() {
@@ -5435,8 +5538,7 @@
5435
5538
  }
5436
5539
  /**
5437
5540
  * Draw one source frame onto the output canvas. `false` means nothing was drawn - no mask
5438
- * yet, the segmenter is still loading, or the recipe is inactive; the transport must
5439
- * forward the original frame rather than drop it.
5541
+ * yet, the segmenter is still loading, or the recipe is inactive.
5440
5542
  *
5441
5543
  * ── Why it takes `TexImageSource` and not just `VideoFrame` ───────────────────────
5442
5544
  * `texImage2D` accepts a `VideoFrame` and an `HTMLVideoElement` alike, and so does
@@ -5448,23 +5550,110 @@
5448
5550
  */
5449
5551
  render(source, timestampUs) {
5450
5552
  if (this.closed) return false;
5553
+ if (this.lastHeld) this.spare.push(this.lastHeld);
5554
+ this.lastHeld = null;
5555
+ this.swallowed = false;
5556
+ if (this.prevTs >= 0 && timestampUs > this.prevTs) {
5557
+ this.gapUs += (Math.min(timestampUs - this.prevTs, 2e5) - this.gapUs) * 0.1;
5558
+ }
5559
+ this.prevTs = timestampUs;
5560
+ if (!this.syncing() || typeof ImageData !== "undefined" && source instanceof ImageData) {
5561
+ this.releaseHeld();
5562
+ this.renderedTs = timestampUs;
5563
+ return this.draw(source, timestampUs, false);
5564
+ }
5565
+ this.requestRemote("face", source, timestampUs);
5566
+ const remoteMask = this.remoteMaskKind();
5567
+ if (remoteMask) this.requestRemote(remoteMask, source, timestampUs);
5568
+ this.queue.push({ c: this.hold(source), ts: timestampUs });
5569
+ this.retuneDepth();
5570
+ if (this.queue.length <= this.holdDepth) {
5571
+ this.swallowed = true;
5572
+ return false;
5573
+ }
5574
+ while (this.queue.length > this.holdDepth + 1) this.spare.push(this.queue.shift().c);
5575
+ const front = this.queue.shift();
5576
+ this.lastHeld = front.c;
5577
+ this.renderedTs = front.ts;
5578
+ return this.draw(front.c, front.ts, true);
5579
+ }
5580
+ remoteMaskKind() {
5581
+ if (this.personRemote && this.personRemoteWanted(this.filter, this.effects)) return "person";
5582
+ if (this.classRemote && needsSkinClasses(this.filter, this.effects)) return "mask";
5583
+ return null;
5584
+ }
5585
+ retuneDepth() {
5586
+ const kind = this.remoteMaskKind();
5587
+ const latency = Math.max(this.faceLatencyMs, kind ? this.maskLatencyMs.get(kind) ?? 0 : 0);
5588
+ const want = latency > 0 ? Math.min(MAX_HOLD, Math.max(1, Math.ceil(latency * 1e3 / this.gapUs))) : 1;
5589
+ if (want === this.holdDepth) {
5590
+ this.depthVotes = 0;
5591
+ return;
5592
+ }
5593
+ if (++this.depthVotes >= DEPTH_VOTES) {
5594
+ this.holdDepth = want;
5595
+ this.depthVotes = 0;
5596
+ }
5597
+ }
5598
+ took(kind, tsUs) {
5599
+ const s2 = this.sent.get(kind);
5600
+ if (s2?.ts !== tsUs) return null;
5601
+ this.sent.delete(kind);
5602
+ return performance.now() - s2.at;
5603
+ }
5604
+ releaseHeld() {
5605
+ for (const h2 of this.queue) this.spare.push(h2.c);
5606
+ this.queue = [];
5607
+ }
5608
+ dropped() {
5609
+ return this.swallowed;
5610
+ }
5611
+ passthrough() {
5612
+ return this.lastHeld ? new VideoFrame(this.lastHeld, { timestamp: this.renderedTs }) : null;
5613
+ }
5614
+ syncing() {
5615
+ return this.syncFaces && this.faceRemote && needsFace(this.effects);
5616
+ }
5617
+ hold(source) {
5618
+ const [w2, h2] = sourceSize(source);
5619
+ let c2 = this.spare.pop();
5620
+ while (c2 && (c2.width !== w2 || c2.height !== h2)) c2 = this.spare.pop();
5621
+ c2 ??= new OffscreenCanvas(w2, h2);
5622
+ c2.getContext("2d").drawImage(source, 0, 0, w2, h2);
5623
+ return c2;
5624
+ }
5625
+ draw(source, timestampUs, faceDone) {
5451
5626
  const t0 = performance.now();
5452
5627
  const [lo2, hi2] = splitMicros(timestampUs);
5453
5628
  const needSeg = this.core.decide(lo2, hi2, this.prevTotalMs, this.prevSegMs) === 1;
5454
5629
  let mask = null;
5455
5630
  let maskKind = 0;
5631
+ let maskTs = timestampUs;
5456
5632
  let segMs = 0;
5457
5633
  let faceMs = 0;
5458
5634
  this.used = null;
5459
5635
  let served = false;
5460
- if (needSeg && this.classRemote && needsSkinClasses(this.filter, this.effects)) {
5461
- this.requestRemote("mask", source, timestampUs);
5636
+ const remoteMask = this.remoteMaskKind();
5637
+ if (remoteMask && !faceDone) this.requestRemote(remoteMask, source, timestampUs);
5638
+ if (remoteMask === "person") {
5639
+ if (this.personMaskTex) {
5640
+ served = true;
5641
+ if (this.personMaskFresh) {
5642
+ this.personMaskFresh = false;
5643
+ mask = this.personMaskTex;
5644
+ maskTs = this.personMaskTs;
5645
+ segMs = this.personWorkerMs;
5646
+ }
5647
+ }
5648
+ }
5649
+ if (remoteMask === "mask") {
5462
5650
  if (this.classMaskTex) {
5463
5651
  served = true;
5464
5652
  if (this.classMaskFresh) {
5465
5653
  this.classMaskFresh = false;
5466
5654
  mask = this.classMaskTex;
5467
5655
  maskKind = 1;
5656
+ maskTs = this.classMaskTs;
5468
5657
  segMs = this.maskWorkerMs;
5469
5658
  }
5470
5659
  }
@@ -5482,9 +5671,13 @@
5482
5671
  maskKind = classes ? 1 : 0;
5483
5672
  }
5484
5673
  }
5485
- if (this.core.decideFace() === 1) {
5674
+ if (faceDone) {
5675
+ faceMs = this.faceWorkerMs;
5676
+ } else if (this.faceRemote && needsFace(this.effects)) {
5677
+ this.requestRemote("face", source, timestampUs);
5678
+ faceMs = this.faceWorkerMs;
5679
+ } else if (this.core.decideFace() === 1) {
5486
5680
  if (this.faceRemote) {
5487
- this.requestRemote("face", source, timestampUs);
5488
5681
  faceMs = this.faceWorkerMs;
5489
5682
  } else if (!this.face) {
5490
5683
  void this.ensureFace();
@@ -5493,9 +5686,12 @@
5493
5686
  const n2 = this.face.detect(source, timestampUs / 1e3, this.lmBuf);
5494
5687
  faceMs = performance.now() - f0;
5495
5688
  this.core.setFace(lo2, hi2, n2);
5689
+ this.faceTs = timestampUs;
5496
5690
  }
5497
5691
  }
5498
- const glCalls = this.core.process(source, mask, this.background, maskKind);
5692
+ if (mask) this.maskUsedTs = maskTs;
5693
+ const [maskLo, maskHi] = splitMicros(maskTs);
5694
+ const glCalls = this.core.process(source, mask, this.background, maskKind, maskLo, maskHi);
5499
5695
  if (mask) this.used?.release();
5500
5696
  this.used = null;
5501
5697
  this.prevSegMs = segMs;
@@ -5517,7 +5713,10 @@
5517
5713
  faceSize: s2 ? s2[9] : 0,
5518
5714
  needsFace: s2 ? s2[10] > 0.5 : false,
5519
5715
  autoExposure: s2 ? s2[11] : 0,
5520
- faces: s2 ? s2[12] : 0
5716
+ faces: s2 ? s2[12] : 0,
5717
+ heldFrames: faceDone ? this.holdDepth : 0,
5718
+ faceAgeMs: s2 && s2[10] > 0.5 && this.faceTs >= 0 ? (timestampUs - this.faceTs) / 1e3 : 0,
5719
+ maskAgeMs: (needSeg || remoteMask !== null) && this.maskUsedTs >= 0 ? (timestampUs - this.maskUsedTs) / 1e3 : 0
5521
5720
  };
5522
5721
  return glCalls > 0;
5523
5722
  }
@@ -5542,13 +5741,19 @@
5542
5741
  this.classSegmenter = null;
5543
5742
  this.face?.close();
5544
5743
  this.face = null;
5545
- this.detect?.close();
5546
- this.detect = null;
5744
+ for (const detect of this.detects.values()) detect.close();
5745
+ this.detects.clear();
5547
5746
  this.faceRemote = false;
5548
5747
  this.classRemote = false;
5549
5748
  if (this.classMaskTex) this.gl.deleteTexture(this.classMaskTex);
5749
+ if (this.personMaskTex) this.gl.deleteTexture(this.personMaskTex);
5750
+ this.personMaskTex = null;
5751
+ this.personRemote = false;
5550
5752
  this.classMaskTex = null;
5551
5753
  this.lmBuf = null;
5754
+ this.queue = [];
5755
+ this.spare = [];
5756
+ this.lastHeld = null;
5552
5757
  this.core.close();
5553
5758
  this.background?.close();
5554
5759
  this.background = null;
@@ -5629,6 +5834,8 @@
5629
5834
  async function run(m2, readable, writable, outTrack) {
5630
5835
  const canvas = new OffscreenCanvas(m2.width, m2.height);
5631
5836
  engine = await Engine.on(canvas, m2.filter, m2.effects, m2.background, m2.alpha ?? false, m2.maxFaces);
5837
+ engine.syncFaces = m2.syncFaces ?? false;
5838
+ engine.setDebug(m2.debug ?? "off");
5632
5839
  abort = new AbortController();
5633
5840
  let n2 = 0;
5634
5841
  let lastPost = performance.now();
@@ -5650,6 +5857,20 @@
5650
5857
  } catch (e2) {
5651
5858
  post({ t: "error", message: e2 instanceof Error ? e2.message : String(e2) });
5652
5859
  }
5860
+ if (!drew && engine?.dropped()) {
5861
+ frame.close();
5862
+ return;
5863
+ }
5864
+ const held = !drew && engine ? engine.passthrough() : null;
5865
+ if (held) {
5866
+ if (framesOn) {
5867
+ const copy = held.clone();
5868
+ self.postMessage({ t: "frame", frame: copy }, [copy]);
5869
+ }
5870
+ controller.enqueue(held);
5871
+ frame.close();
5872
+ return;
5873
+ }
5653
5874
  if (!drew || !engine) {
5654
5875
  if (framesOn) {
5655
5876
  const copy = frame.clone();
@@ -5658,9 +5879,9 @@
5658
5879
  controller.enqueue(frame);
5659
5880
  return;
5660
5881
  }
5661
- controller.enqueue(engine.capture(ts2));
5882
+ controller.enqueue(engine.capture(engine.renderedTs));
5662
5883
  if (framesOn) {
5663
- const copy = engine.capture(ts2, true);
5884
+ const copy = engine.capture(engine.renderedTs, true);
5664
5885
  self.postMessage({ t: "frame", frame: copy }, [copy]);
5665
5886
  }
5666
5887
  frame.close();
@@ -5692,6 +5913,8 @@
5692
5913
  });
5693
5914
  } else if (m2.t === "frames") {
5694
5915
  framesOn = m2.on;
5916
+ } else if (m2.t === "debug") {
5917
+ engine?.setDebug(m2.view);
5695
5918
  } else if (m2.t === "recipe") {
5696
5919
  engine?.setRecipe(m2.filter, m2.effects ?? NO_EFFECTS, m2.background).then(() => {
5697
5920
  if (engine?.loading) post({ t: "stats", stats: engine.last, frames: -1, loading: true });