Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

224 KB
Raw Blame
import { availableParallelism } from "node:os"
import { RC_CQP, RC_VBR, RateControl } from "./ratecontrol"
import {
  AQ_TEXT, AQ_TEXTURE, SideInfo, changeMap, classifyMb, copyBytes, copyRect, diffMb, estimateScroll, estimateShift, fillBytes,
  markRects, matchScroll, mbActivity, mbChange, rowHashes, scrollSpan, weightFit,
} from "./analysis"
import { BitWriter, appendNal, appendNalBytes, seBits, ueBits } from "./bitwriter"
import { CAT_CHROMA_AC, CAT_CHROMA_DC, CAT_LUMA_4x4, CAT_LUMA_8x8, CAT_LUMA_AC, CAT_LUMA_DC, Cabac } from "./cabac"
import { writeResidual } from "./cavlc"
import { filterChromaSegments, filterChromaUV, filterLuma16, filterLumaSegments } from "./deblock"
import { SLICE_B, SLICE_I, SLICE_P, SliceParams, StreamParams, chooseLevel, writePps, writeSliceHeader, writeSps } from "./headers"
import { avgPlanes, computeHalfTile, median3, predictChroma, qpelPlanes, weightedAvgPlanes } from "./inter"
import {
  AV_LEFT, AV_TL, AV_TOP, AV_TR, C_PLANE, I16_DC, I16_PLANE, I4_DC, I4_DDL, I4_H, I4_V, chromaModeAvailable, mode16Available,
  modeMask,
} from "./intra"
import {
  I4_BEST_COST, I4_BEST_MODE, T8_SIZE, UV_SIZE, blocks4, chromaPlaneSatd, chromaSatd, edge4m, edge8m,
  i16PlaneSatd, i16Satd, i4Costs, i8Cost, i8Cost2, i8SrcCosts, pred16v, pred4v, pred8v, predChromaUv, predChromaV,
  unblocks4, unblocks8,
} from "./ipred"
import { KEEP_ONE, LIFE_ONE, Lookahead } from "./lookahead"
import { copyPadded } from "./pixel"
import {
  chromaAcZero, code4, copy16, copy8, dequant4v, dequant8v, dequantChromaDcv, dequantLumaDcv, fwd4, fwd4x2, fwd8, hadamard2v,
  hadamard4v, idct4v, idct8v, idctC8, nonzeroMask, quant4v, quant8v, quantDcv, recon16, recon4, recon8, recon8b, reconDc16,
  reconDcC8, zigzag4, zigzag8, zigzagAc4,
} from "./tq"
import { sa8dPair, sad16, sadAvg16, satd16, satdAvg16 } from "./satd"
import { CBP_CODE_INTER, CBP_CODE_INTRA, CHROMA_QP, QUANT4, ZIGZAG4, ZIGZAG8 } from "./tables"

export type EncoderOptions = {
  width: int,
  height: int,
  fpsNum: int,
  fpsDen: int,
  qp: int,
  keyint: int,
  profile: int,
  preset: string,
  cabac: bool,
  refs: int,
  rcMode: int,
  crf: f64,
  bitrate: int,
  maxrate: int,
  bufsize: int,
  ipOffset: int,
  weightedPred: bool,
  worker: bool,
}

export type SharedState = {
  srcY: Uint8Array, srcU: Uint8Array, srcV: Uint8Array,
  curY: Uint8Array, curU: Uint8Array, curV: Uint8Array,
  refY: Uint8Array[], refU: Uint8Array[], refV: Uint8Array[],
  refHb: Uint8Array[], refHh: Uint8Array[], refHj: Uint8Array[], refTiles: Int32Array[],
  wref: Uint8Array, curVer: Int32Array, refVer: Int32Array[], lastChange: Int32Array, streak: Int32Array,
  changed: Uint8Array, refresh: Uint8Array, mbClass: Uint8Array, mbQp: Int32Array, codedQp: Int32Array,
  mbType: Int32Array, i16Mode: Int32Array, cMode: Int32Array, imode: Uint8Array, t8: Int32Array, cbp: Int32Array,
  dcCbf: Int32Array, nnz: Uint8Array, mvx: Int32Array, mvy: Int32Array, refIdx: Int32Array, mvdx: Int32Array,
  mvdy: Int32Array, prevMvx: Int32Array, prevMvy: Int32Array, pkCoef: Int16Array,
  pkMask: Uint16Array, pkOff: Int32Array, pkTop: Int32Array, qpY: Int32Array, qpDelta: Int32Array,
  srcSame: Uint8Array, recSame: Uint8Array, cType: Uint8Array, cI16: Uint8Array, cCMode: Uint8Array, cCbp: Uint8Array,
  cDcCbf: Uint8Array, cQp: Uint8Array, cImode: Uint8Array, cNnz: Uint8Array, cCoef: Int8Array, cMask: Int64Array,
  cOff: Int32Array,
  idrSaved: Int32Array, idrSlotMb: Int32Array, idrPool: Uint8Array, idrTop: Int32Array, idrRecY: Uint8Array, idrRecU: Uint8Array, idrRecV: Uint8Array,
  mvx1: Int32Array, mvy1: Int32Array, refIdx1: Int32Array, mvdx1: Int32Array, mvdy1: Int32Array, colMvx: Int32Array,
  colMvy: Int32Array, colRef: Int32Array,
}

type BSlot = {
  mbType: Int32Array, i16Mode: Int32Array, cMode: Int32Array, imode: Uint8Array, t8: Int32Array, cbp: Int32Array,
  dcCbf: Int32Array, nnz: Uint8Array, mvx: Int32Array, mvy: Int32Array, refIdx: Int32Array, mvdx: Int32Array,
  mvdy: Int32Array, pkCoef: Int16Array, pkMask: Uint16Array, pkOff: Int32Array, pkTop: Int32Array, qpY: Int32Array,
  qpDelta: Int32Array, mvx1: Int32Array, mvy1: Int32Array, refIdx1: Int32Array, mvdx1: Int32Array, mvdy1: Int32Array,
}

type BMid = {
  mid: int, gate: Int32Array, tileCnt: Int32Array, lazy: bool, oneSlot: bool, colMvx: Int32Array, colMvy: Int32Array,
  colRef: Int32Array, y: Uint8Array, hb: Uint8Array, hh: Uint8Array, hj: Uint8Array, tileGen: Int32Array, gen: int,
}

export type FrameParams = {
  sliceType: int, refsActive: int, refGen: int[], wpOn: bool, wpChromaOn: bool, wpY: int[], wpU: int[], wpV: int[],
  shiftHx: int, shiftVy: int, scrollMvx: int, scrollMvy: int, scrollMb0: int, scrollMb1: int, cursorMvx: int,
  cursorMvy: int, cursorMb: int, idrCache: bool, idrReuse: bool, frameNum: int, idrPicId: int, frameQp: int,
  deblock: bool, filterRecon: bool, counter: int, bTb: int, bTd: int, bTdCol: int, pocBase: int, frameIndex: int,
  params: StreamParams, refreshDelta: int, lifetimeAq: bool, lastAnalysed: int, textMbs: int, laLife: Int32Array,
  laOn: bool, laHz: int, laStrength: f64, nalRef: bool, l0Reorder: int[], mmco: int[],
}

export type IntraJob = {
  seq: int, slot: int, buf: Uint8Array, qp: Int32Array, frameQp: int, idrPicId: int, frameIndex: int, pocBase: int,
  recon: bool, analysis: f64,
}

export type IntraCoded = {
  seq: int, slot: int, frameIndex: int, frameQp: int, bits: u8[], recon: Uint8Array, analysis: f64, mode: f64,
  tq: f64, entropy: f64, deblock: f64, total: f64,
}

export type FrameStats = {
  index: int,
  type: string,
  bytes: int,
  qp: int,
  changedMbs: int,
  analysis: f64,
  motion: f64,
  mode: f64,
  tq: f64,
  entropy: f64,
  deblock: f64,
  total: f64,
  reused: int,
}

export class RefFrame {
  y: Uint8Array = new Uint8Array(0)
  u: Uint8Array = new Uint8Array(0)
  v: Uint8Array = new Uint8Array(0)
  ver: Int32Array = new Int32Array(0)
  hb: Uint8Array = new Uint8Array(0)
  hh: Uint8Array = new Uint8Array(0)
  hj: Uint8Array = new Uint8Array(0)
  tileGen: Int32Array = new Int32Array(0)
  gen = 0
  frameNum = 0
  id = 0
}

export const MB_I16 = 1
export const MB_I4 = 2
export const MB_I8 = 3
export const MB_P16 = 4
export const MB_PSKIP = 5
export const MB_BSKIP = 6
export const MB_BDIRECT = 7
export const MB_BL0 = 8
export const MB_BL1 = 9
export const MB_BBI = 10

export class Pending {
  data: Uint8Array
  index: int
  off = 0
  life: Int32Array = new Int32Array(0)
  keep: Uint8Array = new Uint8Array(0)
  constructor(data: Uint8Array, index: int) {
    this.data = data
    this.index = index
  }
}

// clampMvx/clampMvy keep luma reads within 29 pixels of the picture, so 32 pixels of padding suffice; the half-pel
// tiles of the outermost ring read 2 rows above and 3 rows plus 3 bytes below the padded plane (TILE_GUARD rows).
const PADY = 32
const TILE_GUARD = 4
const MVPAD = 32
const PADC = 16
const ROW_SLOT = 16
// Spin budgets are times, not pause counts: a pause takes tens of nanoseconds on x86 and next to nothing on
// Apple silicon (`yield`). Each thread adapts its own budget between the two bounds (waitRow).
const SPIN_MIN_MS = 0.003
const SPIN_MAX_MS = 0.05
const SLEEP_MAX_MS = 8
const TILE_BUSY = -2
const WP_FIT = 8
const SCAN_WEIGHTS = 0
const SCAN_SHIFT_H = 1
const SCAN_SHIFT_V = 2
const SCAN_HASH = 3
const HASH_ROWS = 128
const PLAN_N = 0
const PLAN_MINX = 1
const PLAN_MAXX = 2
const PLAN_MINY = 3
const PLAN_MAXY = 4
const PLAN_X0 = 5
const PLAN_Y0 = 6
const PLAN_WW = 7
const PLAN_HH = 8
const PLAN_YLO = 9
const PLAN_PREV = 10
const PLAN_CUR = 11
const PLAN_SPAN = 12
const PLAN_SHIFT = 13
const PLAN_DY = 14
const PLAN_JOBS = 15
const PLAN_JOB0 = 16
const PEEK_PREPPED = 0
const PEEK_PHASE = 1
const PEEK_NEXT = 2
const ENTROPY_THREAD_MIN = 4
const ENTROPY_BATCH = 8
const BLK_RASTER: int[] = [0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15]
const DIR_DX: int[] = [-1, 1, 0, 0, -1, 1, -1, 1]
const DIR_DY: int[] = [0, 0, -1, 1, -1, -1, 1, 1]
const COEF_PER_MB = 416
const PK_BLOCK = 8192
const DC_OFF = 256
const CHROMA_DC_OFF = 272
const CHROMA_AC_OFF = 280
const LAMBDA: int[] = [
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 6, 6, 7, 8, 9, 10, 11, 13, 14,
  16, 18, 20, 23, 25, 29, 32, 36, 40, 45, 51, 57, 64, 72, 81, 91,
]
const BIG = 1 << 40
const B_DIRECT_EARLY = 96
const I8_DIR_MODES = 0x1f8
const B_DIRECT_NB = 112
const B_DIRECT_NB_PYRAMID = 128
const B_SKIP_SAD = 16
const B_GATE_QP = 30

function zeroSadMax(qm: int, thr: int, k0: int): int {
  let w = 0
  for (let k = k0; k < 16; k++) {
    const v = ((k & 4) !== 0 ? 2 : 1) * ((k & 1) !== 0 ? 2 : 1) * int(QUANT4[qm + k]!)
    if (v > w) w = v
  }
  return Math.trunc((thr - 1) / w)
}

function sadBlocks16(a: Uint8Array, ao: int, as: int, b: Uint8Array, bo: int, bs: int, out: Int32Array) {
  let lo = i16x8.splat(0)
  let hi = i16x8.splat(0)
  for (let y = 0; y < 4; y++) {
    const d = u8x16.load(a, ao + y * as).absDiff(u8x16.load(b, bo + y * bs))
    lo = lo + d.widenLow()
    hi = hi + d.widenHigh()
  }
  const one = i16x8.splat(1)
  const l = i16x8.madd(lo, one)
  const h = i16x8.madd(hi, one)
  out[0] = l.lane(0) + l.lane(1)
  out[1] = l.lane(2) + l.lane(3)
  out[2] = h.lane(0) + h.lane(1)
  out[3] = h.lane(2) + h.lane(3)
}

function sadBlocks8(a: Uint8Array, ao: int, as: int, b: Uint8Array, bo: int, out: Int32Array) {
  const one = i16x8.splat(1)
  for (let h = 0; h < 2; h++) {
    let acc = i16x8.splat(0)
    for (let y = 0; y < 4; y++) {
      const r = h * 4 + y
      acc = acc + (i16x8.loadU8(a, ao + r * as) - i16x8.loadU8(b, bo + r * 8)).abs()
    }
    const s = i16x8.madd(acc, one)
    out[h * 2] = s.lane(0) + s.lane(1)
    out[h * 2 + 1] = s.lane(2) + s.lane(3)
  }
}
const I8_DIR_LANES = i32x8(3, 4, 5, 6, 7, 8, 9, 10)
const I8_DIR_BITS = i32x8(8, 16, 32, 64, 128, 256, 0, 0)

function blockAvail(bx: int, by: int, mbAv: int, n: int): int {
  let av = 0
  const top = by > 0 || (mbAv & AV_TOP) !== 0
  const left = bx > 0 || (mbAv & AV_LEFT) !== 0
  if (top) av |= AV_TOP
  if (left) av |= AV_LEFT
  if (bx > 0 && by > 0) {
    av |= AV_TL
  } else if (bx > 0) {
    if ((mbAv & AV_TOP) !== 0) av |= AV_TL
  } else if (by > 0) {
    if ((mbAv & AV_LEFT) !== 0) av |= AV_TL
  } else if ((mbAv & AV_TL) !== 0) {
    av |= AV_TL
  }
  const last = n === 4 ? 3 : 1
  if (by === 0) {
    if (bx < last) {
      if ((mbAv & AV_TOP) !== 0) av |= AV_TR
    } else if ((mbAv & AV_TR) !== 0) {
      av |= AV_TR
    }
  } else if (bx < last) {
    if (n === 4) {
      if (zIndex4(bx + 1, by - 1) < zIndex4(bx, by)) av |= AV_TR
    } else if (bx === 0 && by === 1) {
      av |= AV_TR
    }
  }
  return av
}

function blockAvailTable(): Uint8Array {
  const t = new Uint8Array(320)
  for (let av = 0; av < 16; av++) {
    for (let r = 0; r < 16; r++) t[av * 16 + r] = blockAvail(r & 3, r >> 2, av, 4)
    for (let b = 0; b < 4; b++) t[256 + av * 4 + b] = blockAvail(b & 1, b >> 1, av, 8)
  }
  return t
}

function zIndex4(bx: int, by: int): int {
  return ((by >> 1) * 2 + (bx >> 1)) * 4 + (by & 1) * 2 + (bx & 1)
}

const DECIMATE4: int[] = [3, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
const DECIMATE8: int[] = [
  3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]

const CBP_BLOCKS: int[] = [
  0x0000, 0x0033, 0x00cc, 0x00ff, 0x3300, 0x3333, 0x33cc, 0x33ff, 0xcc00, 0xcc33, 0xcccc, 0xccff, 0xff00, 0xff33, 0xffcc, 0xffff,
]

function transposeBlocks(m: int): int {
  const a = (m ^ (m >> 3)) & 0x0a0a
  const b = m ^ a ^ (a << 3)
  const c = (b ^ (b >> 6)) & 0x00cc
  return b ^ c ^ (c << 6)
}

function decimateScore(c: Int16Array, o: int, n: int, is8: bool): int {
  let m = nonzeroMask(c, o, n)
  let score = 0
  while (m !== 0) {
    const i = 63 - Math.clz64(m)
    const v = c[o + i]!
    if (v > 1 || v < -1) return 9
    m ^= 1 << i
    const run = i - 64 + Math.clz64(m)
    score += is8 ? DECIMATE8[run]! : DECIMATE4[run]!
  }
  return score
}

function coefChunks(t: int, cbp: int, t8: int): int {
  const cbpL = cbp & 15
  const cbpC = cbp >> 4
  let m = 0
  if (t === MB_I16) {
    m = cbpL !== 0 ? 0x1ff : 0x100
  } else if (t8 === 1) {
    const s = (cbpL & 1) | (cbpL & 2) << 1 | (cbpL & 4) << 2 | (cbpL & 8) << 3
    m = s | s << 1
  } else {
    const q = (cbpL & 3) | (cbpL & 12) << 2
    m = q | q << 2
  }
  if (cbpC !== 0) m |= cbpC === 2 ? 0x1f00 : 0x100
  return m
}

function chunkTable(): Uint16Array {
  const tab = new Uint16Array(192)
  for (let cbp = 0; cbp < 64; cbp++) {
    tab[cbp] = coefChunks(MB_P16, cbp, 0)
    tab[64 + cbp] = coefChunks(MB_P16, cbp, 1)
    tab[128 + cbp] = coefChunks(MB_I16, cbp, 0)
  }
  return tab
}

const CHUNKS = chunkTable()

function iabs(v: int): int {
  return v < 0 ? -v : v
}

function progressArray(n: int): Int32Array {
  return new Int32Array(n * ROW_SLOT)
}

function publish(a: Int32Array, i: int, v: int) {
  const at = i * ROW_SLOT
  Atomics.store(a, at, v)
  if (Atomics.load(a, at + 1) > 0) Atomics.notify(a, at)
}

function publishRows(a: Int32Array, v: int) {
  Atomics.store(a, 0, v)
  if (Atomics.load(a, 1) > 0 && v >= Atomics.load(a, 2)) Atomics.notify(a, 0)
}

function bump(a: Int32Array, i: int) {
  const at = i * ROW_SLOT
  Atomics.add(a, at, 1)
  if (Atomics.load(a, at + 1) > 0) Atomics.notify(a, at)
}

function copyRow16(dst: Uint8Array, d: int, ds: int, src: Uint8Array, so: int, ss: int) {
  for (let y = 0; y < 16; y++) u8x16.load(src, so + y * ss).store(dst, d + y * ds)
}

function weightBlock(p: Uint8Array, o: int, stride: int, w: int, h: int, wt: int, off: int) {
  if (w === 16) {
    const wv = i16x8.splat(wt)
    const ov = i16x8.splat(off)
    const r = i16x8.splat(32)
    for (let y = 0; y < h; y++) {
      const v = u8x16.load(p, o + y * stride)
      const lo = ((v.widenLow() * wv + r) >> 6) + ov
      const hi = ((v.widenHigh() * wv + r) >> 6) + ov
      u8x16.packSat(lo, hi).store(p, o + y * stride)
    }
    return
  }
  for (let y = 0; y < h; y++) {
    for (let x = 0; x < w; x++) {
      const v = ((int(p[o + y * stride + x]!) * wt + 32) >> 6) + off
      const c = v < 0 ? 0 : v > 255 ? 255 : v
      p[o + y * stride + x] = c
    }
  }
}

type FrameScan = {
  frame: Uint8Array, frameOff: int, w: int, h: int, src: Uint8Array, aw: int, x0: int, y0: int, ww: int, hh: int,
  ylo: int, prevHashes: Uint32Array, curHashes: Uint32Array, shifts: Int32Array, refY: Uint8Array, refU: Uint8Array,
  refV: Uint8Array, yorg: int, ys: int, corg: int, cs: int, wpFit: Int32Array,
}

function runScanJob(s: FrameScan, job: int) {
  if (job === SCAN_WEIGHTS) {
    weightFit(s.frame, s.frameOff, s.w, s.h, s.refY, s.refU, s.refV, s.yorg, s.ys, s.corg, s.cs, s.wpFit)
  } else if (job === SCAN_SHIFT_H || job === SCAN_SHIFT_V) {
    const horizontal = job === SCAN_SHIFT_H
    s.shifts[job - SCAN_SHIFT_H] = estimateShift(s.frame, s.w, s.frameOff, s.src, s.aw, 0, s.x0, s.y0, s.ww, s.hh, s.w,
      s.h, horizontal, horizontal ? 512 : 256)
  } else {
    const k = job - SCAN_HASH
    const r = k >> 1
    if ((k & 1) === 0) {
      const n = s.prevHashes.length - r < HASH_ROWS ? s.prevHashes.length - r : HASH_ROWS
      rowHashes(s.prevHashes, r, s.src, (s.ylo + r) * s.aw + s.x0, s.aw, s.ww, n)
    } else {
      const n = s.hh - r < HASH_ROWS ? s.hh - r : HASH_ROWS
      rowHashes(s.curHashes, r, s.frame, s.frameOff + (s.y0 + r) * s.w + s.x0, s.w, s.ww, n)
    }
  }
}

function scanJobs(s: FrameScan, plan: Int32Array, next: Int32Array, jobs: int) {
  while (true) {
    const j = Atomics.add(next, 0, 1)
    if (j >= jobs) return
    runScanJob(s, plan[PLAN_JOB0 + j]!)
    if (Atomics.add(next, 1, 1) === jobs - 1) Atomics.notify(next, 1)
  }
}

function scanDone(s: FrameScan, plan: Int32Array) {
  plan[PLAN_DY] = plan[PLAN_SPAN]! === 1 ? matchScroll(s.prevHashes, s.ylo, s.curHashes, s.y0) : 0
}

type Peek = {
  frame: Uint8Array, frameOff: int, ready: Int32Array, sync: Int32Array, changed: Uint8Array, plan: Int32Array,
  prev: Uint32Array, cur: Uint32Array, shifts: Int32Array,
}

function minPositive(a: int, b: int): int {
  return a >= 0 && b >= 0 ? (a < b ? a : b) : (a > b ? a : b)
}

function newPlane(n: int): Uint8Array {
  return new Uint8Array(n)
}

function bWorker(opts: EncoderOptions, sh: SharedState, fp: FrameParams, tuning: int[], tilesReady: bool): Encoder {
  const w = new Encoder(opts)
  w.attach(sh, fp)
  w.tune(tuning)
  w.tilesReady = tilesReady
  w.sharedTiles = false
  return w
}

function pickBFrame(next: Int32Array, n: int, mbh: int, only: int): int {
  let k = -1
  let most = 0
  for (let i = 0; i < n; i++) {
    if (only >= 0 && i !== only) continue
    const left = mbh - Atomics.load(next, i)
    if (left > most) {
      most = left
      k = i
    }
  }
  return k
}

function padRef(w: Encoder, bm: BMid) {
  for (let y = 0; y < w.mbh; y++) {
    for (let x = 0; x < w.mbw; x++) w.padMb(x, y)
  }
  publish(bm.gate, 0, bm.lazy ? 2 : 1)
}

function helpTiles(w: Encoder, bm: BMid) {
  const rows = w.tilesY
  while (true) {
    const r = Atomics.add(bm.tileCnt, 0, 1)
    if (r >= rows) return
    w.halfTileRow(bm.y, bm.hb, bm.hh, bm.hj, bm.tileGen, bm.gen, r)
    if (Atomics.add(bm.tileCnt, 1, 1) + 1 === rows) publish(bm.gate, 0, 2)
  }
}

function codeBRun(ws: Encoder[], next: Int32Array, done: Int32Array, progress: Int32Array[], stats: Float64Array,
  slot: int, bm: BMid): u8[][] {
  const n = ws.length
  const mbh = ws[0]!.mbh
  let parts: u8[][] = []
  for (let i = 0; i < n; i++) parts.push([])
  let k = bm.mid >= 0 ? bm.mid : slot % n
  while (true) {
    if (k < 0 || Atomics.load(next, k) >= mbh) {
      const st = bm.mid < 0 ? 2 : Atomics.load(bm.gate, 0)
      k = pickBFrame(next, n, mbh, st >= 2 ? -1 : bm.mid)
      if (k < 0) {
        if (st >= 2) break
        if (st === 1) {
          const h0 = performance.now()
          helpTiles(ws[bm.mid]!, bm)
          const om = (slot * n + bm.mid) * 5
          stats[om + 4] = stats[om + 4]! + performance.now() - h0
        }
        ws[0]!.waitRow(bm.gate, 0, st + 1)
        continue
      }
    }
    const r = Atomics.add(next, k, 1)
    if (r >= mbh) continue
    const w = ws[k]!
    w.codeRowWave(r, false, mbh, progress[k]!)
    if (Atomics.add(done, k, 1) + 1 < mbh) continue
    const e0 = performance.now()
    w.finishQpRows(mbh, 0, mbh)
    if (k === bm.mid) {
      w.storeCol(bm.colMvx, bm.colMvy, bm.colRef)
      padRef(w, bm)
    }
    let out: u8[] = []
    w.entropyRows(false, mbh, 0, mbh, &out)
    if (bm.oneSlot) w.pkTop[0] = 0
    parts[k] = out
    stats[(slot * n + k) * 5 + 3] = performance.now() - e0
  }
  for (let i = 0; i < n; i++) {
    const o = (slot * n + i) * 5
    stats[o] = ws[i]!.rMo
    stats[o + 1] = ws[i]!.rMd
    stats[o + 2] = ws[i]!.rTq
  }
  return parts
}

export class Encoder {
  readonly w: int
  readonly h: int
  readonly mbw: int
  readonly mbh: int
  readonly aw: int
  readonly ah: int
  readonly cw: int
  readonly chh: int
  readonly ys: int
  readonly yorg: int
  readonly ytile: int
  readonly ysize: int
  readonly cs: int
  readonly corg: int
  params: StreamParams
  qp: int
  keyint: int
  maxRefs: int
  srcY: Uint8Array = new Uint8Array(0)
  srcU: Uint8Array = new Uint8Array(0)
  srcV: Uint8Array = new Uint8Array(0)
  cur: RefFrame = new RefFrame()
  refs: RefFrame[] = []
  spare: RefFrame[] = []
  genSeq = 0
  mbType: Int32Array = new Int32Array(0)
  i16Mode: Int32Array = new Int32Array(0)
  cMode: Int32Array = new Int32Array(0)
  imode: Uint8Array = new Uint8Array(0)
  t8: Int32Array = new Int32Array(0)
  cbp: Int32Array = new Int32Array(0)
  dcCbf: Int32Array = new Int32Array(0)
  nnz: Uint8Array = new Uint8Array(0)
  mvx: Int32Array = new Int32Array(0)
  mvy: Int32Array = new Int32Array(0)
  refIdx: Int32Array = new Int32Array(0)
  mvdx: Int32Array = new Int32Array(0)
  mvdy: Int32Array = new Int32Array(0)
  prevMvx: Int32Array = new Int32Array(0)
  prevMvy: Int32Array = new Int32Array(0)
  coef: Int16Array = new Int16Array(COEF_PER_MB)
  pkCoef: Int16Array = new Int16Array(0)
  pkMask: Uint16Array = new Uint16Array(0)
  pkOff: Int32Array = new Int32Array(0)
  pkTop: Int32Array = new Int32Array(1)
  pkW = 0
  pkEnd = 0
  blk16: Int16Array = new Int16Array(16)
  pred: Uint8Array = new Uint8Array(0)
  trial: Uint8Array = new Uint8Array(0)
  xb: Uint8Array = new Uint8Array(16)
  p4: Uint8Array = new Uint8Array(144)
  ip8: Uint8Array = new Uint8Array(T8_SIZE)
  srcBlk: Uint8Array = new Uint8Array(256)
  edgeBlk: Uint8Array = new Uint8Array(256)
  uvBlk: Uint8Array = new Uint8Array(UV_SIZE)
  uvPlane: Int16Array = new Int16Array(32)
  uvMb = -1
  cost9: Int32Array = new Int32Array(16)
  g4: Int32Array = new Int32Array(25)
  blkAv: Uint8Array = blockAvailTable()
  modes4: int[] = []
  modes8: int[] = []
  coded4 = -1
  coded8 = -1
  cbp4 = 0
  cbp8 = 0
  rec4: Uint8Array = new Uint8Array(256)
  rec8: Uint8Array = new Uint8Array(256)
  lv8: Int32Array = new Int32Array(256)
  nz8: Int32Array = new Int32Array(4)
  tilesX = 0
  tilesY = 0
  bw: BitWriter = new BitWriter()
  cabac: Cabac
  out: u8[] = []
  frameNum = 0
  idrPicId = 0
  frameIndex = 0
  codedFrames = 0
  sinceIdr = 0
  skipRun = 0
  sliceType = SLICE_I
  refsActive = 1
  meCost = 0
  intraEst = 0
  earlyExit = true
  predReady = false
  mvpX = 0
  mvpY = 0
  skipX = 0
  skipY = 0
  best16 = I16_DC
  bestC = 0
  intraType = MB_I16
  tAnalysis = 0.0
  tMotion = 0.0
  tMode = 0.0
  tTq = 0.0
  tEntropy = 0.0
  tDeblock = 0.0
  frames: FrameStats[] = []
  changed: Uint8Array = new Uint8Array(0)
  held: Uint8Array = new Uint8Array(0)
  heldAt = -1
  changeCodes: Uint8Array = new Uint8Array(0)
  laChanges: Uint8Array = new Uint8Array(0)
  laChangesAt = -1
  laChangesNoise = -1
  prevChanged: Uint8Array = new Uint8Array(0)
  refresh: Uint8Array = new Uint8Array(0)
  mbClass: Uint8Array = new Uint8Array(0)
  mbQp: Int32Array = new Int32Array(0)
  codedQp: Int32Array = new Int32Array(0)
  qpY: Int32Array = new Int32Array(0)
  qpDelta: Int32Array = new Int32Array(0)
  mbq = 26
  qpPred = 26
  lastDeltaNz = false
  aqStrength = 1
  aqTexture = 0
  noise = 0
  refreshDelta = 3
  lifetimeAq = true
  frameQp = 26
  lastChange: Int32Array = new Int32Array(0)
  streak: Int32Array = new Int32Array(0)
  lastAnalysed = -1
  lastCodedIndex = -1
  lastActive = -1
  refineGain = 6
  rate: RateControl
  rcAct = false
  fpsF: f64
  counter = 0
  vfr = true
  fullAnalysis = false
  deblock = true
  scrollMvx = 0
  scrollMvy = 0
  scrollMb0 = -1
  scrollMb1 = -1
  cursorMvx = 0
  cursorMvy = 0
  cursorMb = -1
  shiftHx = 0
  shiftVy = 0
  globalShift = true
  prevCursorX = -1
  prevCursorY = -1
  useI8: bool
  useI4: bool
  inter8x8 = true
  decimate = true
  subpel: int
  subpelDirs: int
  videoHpelDirs = 8
  videoQpelDirs = 8
  meIters: int
  earlySkip = false
  i8PredOnly = false
  hpelSad = false
  qpelSad = false
  textMbs = 0
  classReady = false
  skipProbed = false
  seen: Int32Array = new Int32Array(64 * 64)
  seenGen = 0
  seenX = 0
  seenY = 0
  idrReuse = true
  idrCache = false
  idrSaved: Int32Array = new Int32Array(0)
  idrSlotMb: Int32Array = new Int32Array(0)
  idrPool: Uint8Array = new Uint8Array(0)
  idrTop: Int32Array = new Int32Array(1)
  idrRecY: Uint8Array = new Uint8Array(0)
  idrRecU: Uint8Array = new Uint8Array(0)
  idrRecV: Uint8Array = new Uint8Array(0)
  cType: Uint8Array = new Uint8Array(0)
  cI16: Uint8Array = new Uint8Array(0)
  cCMode: Uint8Array = new Uint8Array(0)
  cCbp: Uint8Array = new Uint8Array(0)
  cDcCbf: Uint8Array = new Uint8Array(0)
  cQp: Uint8Array = new Uint8Array(0)
  cImode: Uint8Array = new Uint8Array(0)
  cNnz: Uint8Array = new Uint8Array(0)
  cCoef: Int8Array = new Int8Array(0)
  cMask: Int64Array = new Int64Array(0)
  cOff: Int32Array = new Int32Array(0)
  cRow = 0
  rowOld: Int8Array = new Int8Array(0)
  rowAt = 0
  srcSame: Uint8Array = new Uint8Array(0)
  recSame: Uint8Array = new Uint8Array(0)
  reusedMbs = 0
  threads = 1
  opts: EncoderOptions
  sharedTiles = false
  tilesReady = false
  bframes = 0
  dpbRefs = 1
  pending: Pending[] = []
  mvx1: Int32Array = new Int32Array(0)
  mvy1: Int32Array = new Int32Array(0)
  refIdx1: Int32Array = new Int32Array(0)
  mvdx1: Int32Array = new Int32Array(0)
  mvdy1: Int32Array = new Int32Array(0)
  colMvx: Int32Array = new Int32Array(0)
  colMvy: Int32Array = new Int32Array(0)
  colRef: Int32Array = new Int32Array(0)
  bSlots: BSlot[] = []
  bTargets: RefFrame[] = []
  bThreads: Thread<u8[][]>[] = []
  bInline: u8[][][] = []
  bRunIdx: int[] = []
  bRunData: Uint8Array[] = []
  bRunRecon: RefFrame[] = []
  bRunStats: Float64Array = new Float64Array(0)
  bRunQps: int[] = []
  bRunAnchorQp = 26
  bRunAnalysis = 0.0
  bRunT1 = 0.0
  inputPool: Uint8Array[] = []
  bMbQp: Int32Array = new Int32Array(0)
  bChanged: Uint8Array = new Uint8Array(0)
  bRefresh: Uint8Array = new Uint8Array(0)
  predB: Uint8Array = new Uint8Array(384)
  predBi: Uint8Array = new Uint8Array(384)
  zsad: Int32Array = new Int32Array(4)
  wantRecon = false
  filterRecon = true
  frameThreads = true
  srcRing: Uint8Array[] = []
  ringBusy: Uint8Array = new Uint8Array(0)
  srcSlot = -1
  intraWork: Channel<IntraJob> | undefined = undefined
  intraDone: Channel<IntraCoded> | undefined = undefined
  intraPool: Thread<void>[] = []
  intraHeld: IntraCoded[] = []
  intraSent = 0
  intraEmitted = 0
  reconHeld: Pending[] = []
  reconReady: Uint8Array[] = []
  nextRecon = 0
  pocBase = 0
  inputIndex = 0
  anchorQp = 26
  bQpOffset = 6
  bRefQpOffset = 5
  bDirectNb = B_DIRECT_NB
  bAdapt = true
  pyramid = false
  nalRef = false
  l0Reorder: int[] = []
  mmco: int[] = []
  bRefNum = -1
  bRefFrame: RefFrame = new RefFrame()
  bColMvx: Int32Array = new Int32Array(0)
  bColMvy: Int32Array = new Int32Array(0)
  bColRef: Int32Array = new Int32Array(0)
  bMbQpRef: Int32Array = new Int32Array(0)
  bRunOrder: int[] = []
  bRunMid = -1
  bReady = false
  dirtyScratch: Uint8Array = new Uint8Array(0)
  dRef0 = 0
  dRef1 = 0
  dMx0 = 0
  dMy0 = 0
  dMx1 = 0
  dMy1 = 0
  sMx = 0
  sMy = 0
  sCost = 0
  sSad = false
  bTb = 1
  bTd = 2
  bTdCol = 2
  lastAnchorIndex = 0
  prevAnchorIndex = 0
  rMo = 0.0
  rMd = 0.0
  rTq = 0.0
  rEnt = 0.0
  rDb = 0.0
  rPrep = 0.0
  rUn = 0.0
  tN = 0
  tNs = 0
  uN = 0
  uNs = 0
  pipeMain = false
  pipeProgress: Int32Array = new Int32Array(0)
  pipeEnt = 0
  pipeIdr = false
  pipeSliceRows = 0
  qpRows: Int32Array = new Int32Array(0)
  spinMs = SPIN_MAX_MS
  spinBudget: Float64Array = new Float64Array(0)
  weighted = true
  wpOn = false
  wpChromaOn = false
  wpY: int[] = [64, 0]
  wpU: int[] = [64, 0]
  wpV: int[] = [64, 0]
  wref: Uint8Array = new Uint8Array(0)
  wpFrames = 0
  wpFit: Int32Array = new Int32Array(WP_FIT)
  wpScanned = false
  sliceRow0 = 0
  lv32: Int32Array = new Int32Array(256)
  c32: Int32Array = new Int32Array(128)
  dc32: Int32Array = new Int32Array(16)
  nz4: int[] = []
  sliceRowsI = 0
  idrSlices = 4
  sliceRowsP = 0
  nextFrame: Uint8Array = new Uint8Array(0)
  nextOff = 0
  nextRead: Int32Array = new Int32Array(0)
  pkSync: Int32Array = new Int32Array(0)
  pkChanged: Uint8Array = new Uint8Array(0)
  pkPlan: Int32Array = new Int32Array(0)
  pkPrev: Uint32Array = new Uint32Array(0)
  pkCur: Uint32Array = new Uint32Array(0)
  pkShifts: Int32Array = new Int32Array(0)
  pkFit: Int32Array = new Int32Array(WP_FIT)
  pkWeighed = false
  peeked = false
  peekedFrame: Uint8Array = new Uint8Array(0)
  peekedOff = 0
  peekedIndex = -1
  laLife: Int32Array = new Int32Array(0)
  laNext = false
  laOn = false
  laHz = 0
  laStrength = 1.75
  laBase = -3.0
  laMax = 8.0
  laSpan = 1
  laL1 = 4.0
  laKeep: Uint8Array = new Uint8Array(0)
  flatLife: Int32Array = new Int32Array(0)

  constructor(opts: EncoderOptions) {
    this.opts = opts
    this.w = opts.width
    this.h = opts.height
    this.mbw = (opts.width + 15) >> 4
    this.mbh = (opts.height + 15) >> 4
    this.cRow = this.mbw * COEF_PER_MB * 2 + 16
    this.aw = this.mbw * 16
    this.ah = this.mbh * 16
    this.cw = this.aw >> 1
    this.chh = this.ah >> 1
    this.ys = this.aw + 2 * PADY
    this.ytile = TILE_GUARD * this.ys
    this.yorg = this.ytile + PADY * this.ys + PADY
    this.ysize = this.ys * (this.ah + 2 * PADY + 2 * TILE_GUARD)
    this.cs = this.cw + 2 * PADC
    this.corg = PADC * this.cs + PADC
    this.qp = opts.qp
    this.keyint = opts.keyint
    this.fpsF = f64(opts.fpsNum) / f64(opts.fpsDen)
    const twoSeconds = Math.round(2.0 * this.fpsF)
    this.laSpan = twoSeconds < 1 ? 1 : twoSeconds < this.keyint ? twoSeconds : this.keyint
    const rcBase = opts.rcMode === RC_CQP ? f64(opts.qp) : opts.crf
    this.rate = new RateControl(opts.rcMode, rcBase, opts.rcMode === RC_CQP ? 0 : opts.ipOffset, opts.bitrate,
      opts.maxrate, opts.bufsize, this.fpsF, opts.keyint)
    this.rcAct = opts.rcMode === RC_VBR && !opts.worker
    this.useI8 = opts.profile === 100
    this.useI4 = true
    this.subpel = 2
    this.subpelDirs = opts.preset === "live" || opts.preset === "fast" ? 4 : 8
    this.videoQpelDirs = opts.preset === "live" ? 4 : 8
    this.earlyExit = opts.preset !== "slow"
    this.earlySkip = opts.preset !== "slow"
    this.i8PredOnly = opts.preset === "live"
    this.hpelSad = opts.preset !== "slow"
    this.qpelSad = opts.preset === "medium"
    this.meIters = opts.preset === "live" ? 4 : opts.preset === "fast" ? 8 : 16
    const fps = f64(opts.fpsNum) / f64(opts.fpsDen)
    const refs = opts.refs < 1 ? 1 : opts.refs > 4 ? 4 : opts.refs
    const level = chooseLevel(this.mbw, this.mbh, fps, refs, 0)
    this.maxRefs = refs
    this.dpbRefs = refs
    this.cabac = new Cabac()
    this.params = {
      width: opts.width, height: opts.height, mbw: this.mbw, mbh: this.mbh, profile: opts.profile, level,
      refs, fpsNum: opts.fpsNum, fpsDen: opts.fpsDen, log2MaxFrameNum: 8, cabac: opts.cabac,
      transform8x8: this.useI8, weightedPred: opts.weightedPred, dpbRefs: refs, bframes: 0, pyramid: false,
    }
    this.tilesX = this.ys >> 4
    this.tilesY = (this.ah + 2 * PADY) >> 4
    for (let i = 0; i < 16; i++) this.nz4.push(0)
    this.pred = newPlane(384)
    this.trial = newPlane(384)
    for (let i = 0; i < 16; i++) this.modes4.push(2)
    for (let i = 0; i < 4; i++) this.modes8.push(2)
    this.threads = 1
    if (opts.worker) return
    this.srcY = newPlane(this.aw * this.ah + 64)
    this.srcU = newPlane(this.cw * this.chh + 64)
    this.srcV = newPlane(this.cw * this.chh + 64)
    this.cur = this.newFrame()
    const mbs = this.mbw * this.mbh
    this.changed = new Uint8Array(mbs)
    this.changed.fill(1)
    this.held = new Uint8Array(mbs)
    this.changeCodes = new Uint8Array(mbs)
    this.prevChanged = new Uint8Array(mbs)
    this.refresh = new Uint8Array(mbs)
    this.mbClass = new Uint8Array(mbs)
    this.mbType = new Int32Array(mbs)
    this.i16Mode = new Int32Array(mbs)
    this.cMode = new Int32Array(mbs)
    this.t8 = new Int32Array(mbs)
    this.cbp = new Int32Array(mbs)
    this.dcCbf = new Int32Array(mbs)
    this.refIdx = new Int32Array(mbs)
    this.prevMvx = new Int32Array(mbs)
    this.prevMvy = new Int32Array(mbs)
    this.mbQp = new Int32Array(mbs)
    this.mbQp.fill(opts.qp)
    this.codedQp = new Int32Array(mbs)
    this.qpY = new Int32Array(mbs)
    this.qpY.fill(opts.qp)
    this.qpDelta = new Int32Array(mbs)
    this.lastChange = new Int32Array(mbs)
    this.lastChange.fill(-1000000)
    this.streak = new Int32Array(mbs)
    this.imode = new Uint8Array(mbs * 16)
    this.imode.fill(2)
    this.mvx = new Int32Array(mbs)
    this.mvy = new Int32Array(mbs)
    this.mvdx = new Int32Array(mbs)
    this.mvdy = new Int32Array(mbs)
    this.nnz = new Uint8Array(mbs * 24)
    this.pkMask = new Uint16Array(mbs)
    this.pkOff = new Int32Array(mbs)
  }

  newFrame(): RefFrame {
    const f = new RefFrame()
    f.y = newPlane(this.ysize)
    f.u = newPlane(this.cs * (this.chh + 2 * PADC))
    f.v = newPlane(this.cs * (this.chh + 2 * PADC))
    f.ver = new Int32Array(this.mbw * this.mbh)
    f.ver.fill(-1)
    return f
  }

  giveHalfPlanes(rf: RefFrame) {
    if (rf.tileGen.length > 0) return
    for (const s of this.spare) {
      if (s.tileGen.length === 0) continue
      const hb = rf.hb
      const hh = rf.hh
      const hj = rf.hj
      const tiles = rf.tileGen
      rf.hb = s.hb
      rf.hh = s.hh
      rf.hj = s.hj
      rf.tileGen = s.tileGen
      s.hb = hb
      s.hh = hh
      s.hj = hj
      s.tileGen = tiles
      return
    }
    rf.hb = newPlane(this.ysize)
    rf.hh = newPlane(this.ysize)
    rf.hj = newPlane(this.ysize)
    rf.tileGen = new Int32Array(this.tilesX * this.tilesY)
  }

  ensureTiles(rf: RefFrame, x0: int, y0: int, w: int, h: int) {
    if (this.tilesReady) return
    const tiles = rf.tileGen
    const gen = rf.gen
    const tw = this.tilesX
    const ys = this.ys
    const org = this.ytile
    const tx0 = (x0 + PADY) >> 4
    const ty0 = (y0 + PADY) >> 4
    const tx1 = (x0 + w - 1 + PADY) >> 4
    const ty1 = (y0 + h - 1 + PADY) >> 4
    for (let ty = ty0; ty <= ty1; ty++) {
      const row = ty * tw
      for (let tx = tx0; tx <= tx1; tx++) {
        const t = row + tx
        if (this.sharedTiles) {
          if (Atomics.load(tiles, t) !== gen) this.claimTile(rf, t, tx, ty)
        } else if (tiles[t]! !== gen) {
          computeHalfTile(rf.hb, rf.hh, rf.hj, rf.y, ys, org + ty * 16 * ys + tx * 16)
          tiles[t] = gen
        }
      }
    }
  }

  precomputeTiles(rf: RefFrame) {
    const tx = this.tilesX
    const rows = this.tilesY
    const n = this.threads
    const gen = rf.gen
    const y = rf.y
    const hb = rf.hb
    const hh = rf.hh
    const hj = rf.hj
    const tiles = rf.tileGen
    const ys = this.ys
    const org = this.ytile
    Thread.parallelFor(n, (k) => {
      for (let r = k; r < rows; r += n) {
        for (let c = 0; c < tx; c++) {
          const t = r * tx + c
          if (tiles[t]! === gen) continue
          computeHalfTile(hb, hh, hj, y, ys, org + r * 16 * ys + c * 16)
          tiles[t] = gen
        }
      }
    })
  }

  claimTile(rf: RefFrame, t: int, tx: int, ty: int) {
    const gen = rf.gen
    while (true) {
      const seen = Atomics.load(rf.tileGen, t)
      if (seen === gen) return
      if (seen !== TILE_BUSY && Atomics.compareExchange(rf.tileGen, t, seen, TILE_BUSY) === seen) {
        computeHalfTile(rf.hb, rf.hh, rf.hj, rf.y, this.ys, this.ytile + ty * 16 * this.ys + tx * 16)
        Atomics.store(rf.tileGen, t, gen)
        return
      }
      Atomics.pause()
    }
  }

  plane(rf: RefFrame, id: int): Uint8Array {
    if (id === 1) return rf.hb
    if (id === 2) return rf.hh
    if (id === 3) return rf.hj
    return rf.y
  }

  predictQpel(dst: Uint8Array, dO: int, ds: int, rf: RefFrame, x: int, y: int, mx: int, my: int, w: int, h: int) {
    const code = qpelPlanes(mx & 3, my & 3)
    const base = this.ry(x + (mx >> 2), y + (my >> 2))
    const ao = (code >> 4) & 3
    const bo = code & 3
    const offA = ao === 1 ? 1 : ao === 2 ? this.ys : 0
    const offB = bo === 1 ? 1 : bo === 2 ? this.ys : 0
    const pa = code >> 6
    const pb = (code >> 2) & 3
    if (pa === 0 && pb === 0) {
      avgPlanes(dst, dO, ds, rf.y, base + offA, rf.y, base + offB, this.ys, w, h)
    } else if (pa === 0 && pb === 1) {
      avgPlanes(dst, dO, ds, rf.y, base + offA, rf.hb, base + offB, this.ys, w, h)
    } else if (pa === 0 && pb === 2) {
      avgPlanes(dst, dO, ds, rf.y, base + offA, rf.hh, base + offB, this.ys, w, h)
    } else if (pa === 0 && pb === 3) {
      avgPlanes(dst, dO, ds, rf.y, base + offA, rf.hj, base + offB, this.ys, w, h)
    } else if (pa === 1 && pb === 0) {
      avgPlanes(dst, dO, ds, rf.hb, base + offA, rf.y, base + offB, this.ys, w, h)
    } else if (pa === 1 && pb === 1) {
      avgPlanes(dst, dO, ds, rf.hb, base + offA, rf.hb, base + offB, this.ys, w, h)
    } else if (pa === 1 && pb === 2) {
      avgPlanes(dst, dO, ds, rf.hb, base + offA, rf.hh, base + offB, this.ys, w, h)
    } else if (pa === 1 && pb === 3) {
      avgPlanes(dst, dO, ds, rf.hb, base + offA, rf.hj, base + offB, this.ys, w, h)
    } else if (pa === 2 && pb === 0) {
      avgPlanes(dst, dO, ds, rf.hh, base + offA, rf.y, base + offB, this.ys, w, h)
    } else if (pa === 2 && pb === 1) {
      avgPlanes(dst, dO, ds, rf.hh, base + offA, rf.hb, base + offB, this.ys, w, h)
    } else if (pa === 2 && pb === 2) {
      avgPlanes(dst, dO, ds, rf.hh, base + offA, rf.hh, base + offB, this.ys, w, h)
    } else if (pa === 2 && pb === 3) {
      avgPlanes(dst, dO, ds, rf.hh, base + offA, rf.hj, base + offB, this.ys, w, h)
    } else if (pa === 3 && pb === 0) {
      avgPlanes(dst, dO, ds, rf.hj, base + offA, rf.y, base + offB, this.ys, w, h)
    } else if (pa === 3 && pb === 1) {
      avgPlanes(dst, dO, ds, rf.hj, base + offA, rf.hb, base + offB, this.ys, w, h)
    } else if (pa === 3 && pb === 2) {
      avgPlanes(dst, dO, ds, rf.hj, base + offA, rf.hh, base + offB, this.ys, w, h)
    } else if (pa === 3 && pb === 3) {
      avgPlanes(dst, dO, ds, rf.hj, base + offA, rf.hj, base + offB, this.ys, w, h)
    }
    if (this.wpOn && rf === this.refs[0]!) weightBlock(dst, dO, ds, w, h, this.wpY[0]!, this.wpY[1]!)
  }

  weightChroma(rf: RefFrame, o: int) {
    if (!this.wpChromaOn || rf !== this.refs[0]!) return
    weightBlock(this.pred, o, 8, 8, 8, this.wpU[0]!, this.wpU[1]!)
    weightBlock(this.pred, o + 64, 8, 8, 8, this.wpV[0]!, this.wpV[1]!)
  }

  mbAvail(mbx: int, mby: int): int {
    let av = 0
    const top = mby > this.sliceRow0
    if (top) av |= AV_TOP
    if (mbx > 0) av |= AV_LEFT
    if (mbx > 0 && top) av |= AV_TL
    if (top && mbx + 1 < this.mbw) av |= AV_TR
    return av
  }

  ry(x: int, y: int): int {
    return this.yorg + y * this.ys + x
  }

  rc(x: int, y: int): int {
    return this.corg + y * this.cs + x
  }

  copyMbFromFrame(frame: Uint8Array, frameOff: int, mbx: int, mby: int) {
    const w = this.w
    const h = this.h
    const px = mbx * 16
    const py = mby * 16
    if (px + 16 <= w && py + 16 <= h) {
      copyRow16(this.srcY, py * this.aw + px, this.aw, frame, frameOff + py * w + px, w)
    } else {
      for (let y = 0; y < 16; y++) {
        const sy = py + y < h ? py + y : h - 1
        const so = frameOff + sy * w
        const d = (py + y) * this.aw
        for (let x = 0; x < 16; x++) {
          const sx = px + x < w ? px + x : w - 1
          this.srcY[d + px + x] = frame[so + sx]!
        }
      }
    }
    const cwIn = w >> 1
    const chIn = h >> 1
    const uo = frameOff + w * h
    const vo = uo + cwIn * chIn
    const cx = mbx * 8
    const cy = mby * 8
    if (cx + 8 <= cwIn && cy + 8 <= chIn) {
      copyRect(this.srcU, cy * this.cw + cx, this.cw, frame, uo + cy * cwIn + cx, cwIn, 8, 8)
      copyRect(this.srcV, cy * this.cw + cx, this.cw, frame, vo + cy * cwIn + cx, cwIn, 8, 8)
      return
    }
    for (let y = 0; y < 8; y++) {
      const sy = cy + y < chIn ? cy + y : chIn - 1
      const d = (cy + y) * this.cw
      for (let x = 0; x < 8; x++) {
        const sx = cx + x < cwIn ? cx + x : cwIn - 1
        this.srcU[d + cx + x] = frame[uo + sy * cwIn + sx]!
        this.srcV[d + cx + x] = frame[vo + sy * cwIn + sx]!
      }
    }
  }

  keepIdrSource(mbx: int, mby: int) {
    const mb = mby * this.mbw + mbx
    const slot = Atomics.add(this.idrTop, 0, 1)
    const po = slot * 384
    this.idrSlotMb[slot] = mb
    this.idrSaved[mb] = slot + 1
    copyRect(this.idrPool, po, 16, this.srcY, mby * 16 * this.aw + mbx * 16, this.aw, 16, 16)
    const co = mby * 8 * this.cw + mbx * 8
    copyRect(this.idrPool, po + 256, 8, this.srcU, co, this.cw, 8, 8)
    copyRect(this.idrPool, po + 320, 8, this.srcV, co, this.cw, 8, 8)
  }

  saveIdrSource(frame: Uint8Array, frameOff: int, mbx: int, mby: int) {
    if (this.idrSaved[mby * this.mbw + mbx]! !== 0) return
    const w = this.w
    const h = this.h
    const cwIn = w >> 1
    const uo = frameOff + w * h
    if (mbChange(this.srcY, 0, this.aw, this.srcU, 0, this.srcV, 0, this.cw, frame, frameOff, w, uo, uo + cwIn * (h >> 1),
      cwIn, w, h, 0, mbx, mby) !== 0) this.keepIdrSource(mbx, mby)
  }

  compactIdrPool() {
    let top = 0
    const n = this.idrTop[0]!
    for (let s = 0; s < n; s++) {
      const mb = this.idrSlotMb[s]!
      if (this.idrSaved[mb]! !== s + 1) continue
      if (top !== s) this.idrPool.copyWithin(top * 384, s * 384, s * 384 + 384)
      this.idrSlotMb[top] = mb
      this.idrSaved[mb] = top + 1
      top++
    }
    this.idrTop[0] = top
  }

  useChanges(map: Uint8Array, index: int, noise: int) {
    this.laChanges = map
    this.laChangesAt = index
    this.laChangesNoise = noise
  }

  markChanged(frame: Uint8Array, frameOff: int): int {
    const w = this.w
    const h = this.h
    const cwIn = w >> 1
    const uo = frameOff + w * h
    const vo = uo + cwIn * (h >> 1)
    const mbw = this.mbw
    const mbs = mbw * this.mbh
    const held = this.held
    const changed = this.changed
    const one = u8x16.splat(1)
    const zero = u8x16.splat(0)
    let n = 0
    if (this.laChangesAt === this.frameIndex && this.heldAt === this.frameIndex - 1 && this.laChangesNoise === this.noise &&
      this.laChanges.length === mbs) {
      const shared = this.laChanges
      let mb = 0
      while (mb < mbs) {
        if (mb + 16 <= mbs && u8x16.load(held, mb).eq(one).mask() === 0xffff) {
          const c = u8x16.load(shared, mb)
          const ch = c.eq(one) & one
          const same = c.eq(zero) & one
          ch.store(changed, mb)
          same.store(held, mb)
          n += ch.sum()
          mb += 16
          continue
        }
        const end = mb + 16 < mbs ? mb + 16 : mbs
        for (; mb < end; mb++) {
          const c = held[mb]! !== 0 ? int(shared[mb]!) : mbChange(this.srcY, 0, this.aw, this.srcU, 0, this.srcV, 0, this.cw,
            frame, frameOff, w, uo, vo, cwIn, w, h, this.noise, mb % mbw, Math.trunc(mb / mbw))
          if (c === 1) {
            changed[mb] = 1
            n++
          }
          held[mb] = c === 0 ? 1 : 0
        }
      }
      return n
    }
    n = this.codesOf(frame, frameOff, this.changeCodes)
    this.takeCodes(this.changeCodes)
    return n
  }

  codesOf(frame: Uint8Array, frameOff: int, codes: Uint8Array): int {
    const w = this.w
    const h = this.h
    const cwIn = w >> 1
    const uo = frameOff + w * h
    codes.fill(0)
    return changeMap(this.srcY, 0, this.aw, this.srcU, 0, this.srcV, 0, this.cw, frame, frameOff, w, uo,
      uo + cwIn * (h >> 1), cwIn, w, h, this.noise, codes)
  }

  takeCodes(codes: Uint8Array) {
    const mbs = this.mbw * this.mbh
    const changed = this.changed
    const held = this.held
    const one = u8x16.splat(1)
    const zero = u8x16.splat(0)
    let mb = 0
    for (; mb + 16 <= mbs; mb += 16) {
      const c = u8x16.load(codes, mb)
      (c.eq(one) & one).store(changed, mb)
      (c.eq(zero) & one).store(held, mb)
    }
    for (; mb < mbs; mb++) {
      const c = codes[mb]!
      changed[mb] = c === 1 ? 1 : 0
      held[mb] = c === 0 ? 1 : 0
    }
  }

  holdChanged() {
    const mbs = this.mbw * this.mbh
    const changed = this.changed
    const held = this.held
    const one = u8x16.splat(1)
    const zero = u8x16.splat(0)
    let mb = 0
    for (; mb + 16 <= mbs; mb += 16) {
      (u8x16.load(held, mb) | (one - (u8x16.load(changed, mb).eq(zero) & one))).store(held, mb)
    }
    for (; mb < mbs; mb++) {
      if (changed[mb]! !== 0) held[mb] = 1
    }
  }

  planBox(changed: Uint8Array, n: int, plan: Int32Array) {
    let minY = this.mbh
    let maxY = -1
    let minX = this.mbw
    let maxX = -1
    if (n > 0) {
      for (let mby = 0; mby < this.mbh; mby++) {
        for (let mbx = 0; mbx < this.mbw; mbx++) {
          if (changed[mby * this.mbw + mbx]! === 1) {
            if (mby < minY) minY = mby
            if (mby > maxY) maxY = mby
            if (mbx < minX) minX = mbx
            if (mbx > maxX) maxX = mbx
          }
        }
      }
    }
    plan[PLAN_N] = n
    plan[PLAN_MINX] = minX
    plan[PLAN_MAXX] = maxX
    plan[PLAN_MINY] = minY
    plan[PLAN_MAXY] = maxY
  }

  analyse(frame: Uint8Array, frameOff: int, side: SideInfo | undefined, full: bool): int {
    const mbs = this.mbw * this.mbh
    const peeked = this.peeked && frame === this.peekedFrame && frameOff === this.peekedOff &&
      this.frameIndex === this.peekedIndex && side === undefined && !full
    const weighed = peeked && this.pkWeighed
    this.peeked = false
    this.pkWeighed = false
    for (let i = 0; i < mbs; i++) this.changed[i] = 0
    this.scrollMvx = 0
    this.scrollMvy = 0
    this.scrollMb0 = -1
    this.scrollMb1 = -1
    this.cursorMvx = 0
    this.cursorMvy = 0
    this.cursorMb = -1
    this.shiftHx = 0
    this.shiftVy = 0
    this.wpScanned = false
    for (let i = 0; i < mbs; i++) this.refresh[i] = 0
    if (full) {
      if (side !== undefined) {
        markRects(this.changed, this.mbw, this.mbh, side.dirty)
      } else if (this.frameIndex > 0) {
        this.markChanged(frame, frameOff)
      }
      for (let i = 0; i < mbs; i++) {
        if (this.changed[i]! === 0) this.changed[i] = 3
      }
      if (this.idrCache) {
        const exact = side === undefined && this.frameIndex > 0
        for (let mby = 0; mby < this.mbh; mby++) {
          for (let mbx = 0; mbx < this.mbw; mbx++) {
            const mb = mby * this.mbw + mbx
            if (!exact) this.saveIdrSource(frame, frameOff, mbx, mby)
            else if (this.held[mb]! === 0 && this.idrSaved[mb]! === 0) this.keepIdrSource(mbx, mby)
          }
        }
      }
      if (this.srcSlot >= 0) this.bindSource(this.srcRing[this.srcSlot]!)
      copyPadded(this.srcY, this.aw, this.ah, frame, frameOff, this.w, this.w, this.h)
      const cwIn = this.w >> 1
      const chIn = this.h >> 1
      copyPadded(this.srcU, this.cw, this.chh, frame, frameOff + this.w * this.h, cwIn, cwIn, chIn)
      copyPadded(this.srcV, this.cw, this.chh, frame, frameOff + this.w * this.h + cwIn * chIn, cwIn, cwIn, chIn)
      this.held.fill(1)
      this.heldAt = this.frameIndex
      if (side !== undefined) this.noteHints(side)
      return mbs
    }
    let n = 0
    if (side !== undefined) {
      this.held.fill(0)
      this.heldAt = this.frameIndex
      n = markRects(this.changed, this.mbw, this.mbh, side.dirty)
      for (const sc of side.scroll) n += markRects(this.changed, this.mbw, this.mbh, [sc.x, sc.y, sc.w, sc.h])
      this.noteHints(side)
      this.estimateGlobalShift(frame, frameOff, n)
      return n
    }
    let plan = this.pkPlan
    if (peeked) {
      this.takeCodes(this.pkChanged)
      n = plan[PLAN_N]!
    } else {
      plan = new Int32Array(this.planSize())
      n = this.markChanged(frame, frameOff)
      this.planBox(this.changed, n, plan)
    }
    this.heldAt = this.frameIndex
    const minX = plan[PLAN_MINX]!
    const maxX = plan[PLAN_MAXX]!
    const minY = plan[PLAN_MINY]!
    const maxY = plan[PLAN_MAXY]!
    if (this.threads > 1 && n * 4 >= mbs && this.refs.length > 0) {
      if (peeked) {
        this.applyScan(plan, this.pkShifts)
        if (weighed) {
          this.wpFit.set(this.pkFit)
          this.wpScanned = true
        }
      } else {
        this.scanConcurrent(frame, frameOff, plan)
      }
      return n
    }
    if (n >= 6 && maxY - minY >= 2 && this.refs.length > 0) {
      const x0 = minX * 16
      const y0 = minY * 16
      const ww = (maxX - minX + 1) * 16 < this.w - x0 ? (maxX - minX + 1) * 16 : this.w - x0
      const hh = (maxY - minY + 1) * 16 < this.h - y0 ? (maxY - minY + 1) * 16 : this.h - y0
      const dy = estimateScroll(frame, this.w, frameOff, this.srcY, this.aw, 0, x0, y0, ww, hh, this.h, 256)
      if (dy !== 0) {
        this.scrollMvx = 0
        this.scrollMvy = dy * 4
        this.scrollMb0 = minY * this.mbw + minX
        this.scrollMb1 = maxY * this.mbw + maxX
      }
    }
    this.estimateGlobalShift(frame, frameOff, n)
    return n
  }

  estimateGlobalShift(frame: Uint8Array, frameOff: int, n: int) {
    const mbs = this.mbw * this.mbh
    if (!this.globalShift || this.refs.length === 0 || n < 48 || n * 50 < mbs) return
    let minY = this.mbh
    let maxY = -1
    let minX = this.mbw
    let maxX = -1
    for (let mby = 0; mby < this.mbh; mby++) {
      for (let mbx = 0; mbx < this.mbw; mbx++) {
        if (this.changed[mby * this.mbw + mbx]! === 0) continue
        if (mby < minY) minY = mby
        if (mby > maxY) maxY = mby
        if (mbx < minX) minX = mbx
        if (mbx > maxX) maxX = mbx
      }
    }
    if (maxY < 0) return
    const x0 = minX * 16
    const y0 = minY * 16
    const ww = (maxX - minX + 1) * 16 < this.w - x0 ? (maxX - minX + 1) * 16 : this.w - x0
    const hh = (maxY - minY + 1) * 16 < this.h - y0 ? (maxY - minY + 1) * 16 : this.h - y0
    const dx = estimateShift(frame, this.w, frameOff, this.srcY, this.aw, 0, x0, y0, ww, hh, this.w, this.h, true, 512)
    this.shiftHx = dx * 4
    if (this.scrollMvy === 0) {
      const dy = estimateShift(frame, this.w, frameOff, this.srcY, this.aw, 0, x0, y0, ww, hh, this.w, this.h, false, 256)
      this.shiftVy = dy * 4
    }
  }

  diffFrame(frame: Uint8Array, frameOff: int, codes: Uint8Array, plan: Int32Array): int {
    const n = this.codesOf(frame, frameOff, codes)
    this.planBox(codes, n, plan)
    return n
  }

  planSize(): int {
    return PLAN_JOB0 + 3 + 2 * Math.trunc((this.h + HASH_ROWS - 1) / HASH_ROWS)
  }

  scanPlan(plan: Int32Array, weigh: bool) {
    const mbs = this.mbw * this.mbh
    const n = plan[PLAN_N]!
    const minX = plan[PLAN_MINX]!
    const maxX = plan[PLAN_MAXX]!
    const minY = plan[PLAN_MINY]!
    const maxY = plan[PLAN_MAXY]!
    const x0 = minX * 16
    const y0 = minY * 16
    const ww = (maxX - minX + 1) * 16 < this.w - x0 ? (maxX - minX + 1) * 16 : this.w - x0
    const hh = (maxY - minY + 1) * 16 < this.h - y0 ? (maxY - minY + 1) * 16 : this.h - y0
    const span = n >= 6 && maxY - minY >= 2 ? scrollSpan(ww, hh, y0, this.h, 256) : []
    const shift = this.globalShift && n >= 48 && n * 50 >= mbs
    const ylo = span.length > 0 ? span[0]! : 0
    const prevRows = span.length > 0 ? span[1]! - ylo : 0
    const curRows = span.length > 0 ? hh : 0
    plan[PLAN_X0] = x0
    plan[PLAN_Y0] = y0
    plan[PLAN_WW] = ww
    plan[PLAN_HH] = hh
    plan[PLAN_YLO] = ylo
    plan[PLAN_PREV] = prevRows
    plan[PLAN_CUR] = curRows
    plan[PLAN_SPAN] = span.length > 0 ? 1 : 0
    plan[PLAN_SHIFT] = shift ? 1 : 0
    plan[PLAN_DY] = 0
    let k = PLAN_JOB0
    if (weigh) {
      plan[k] = SCAN_WEIGHTS
      k++
    }
    if (shift) {
      plan[k] = SCAN_SHIFT_H
      plan[k + 1] = SCAN_SHIFT_V
      k += 2
    }
    for (let r = 0; r < prevRows; r += HASH_ROWS) {
      plan[k] = SCAN_HASH + r * 2
      k++
    }
    for (let r = 0; r < curRows; r += HASH_ROWS) {
      plan[k] = SCAN_HASH + r * 2 + 1
      k++
    }
    plan[PLAN_JOBS] = k - PLAN_JOB0
  }

  scanOf(plan: Int32Array, frame: Uint8Array, frameOff: int, prevHashes: Uint32Array, curHashes: Uint32Array,
    shifts: Int32Array, rf: RefFrame, wpFit: Int32Array): FrameScan {
    return {
      frame, frameOff, w: this.w, h: this.h, src: this.srcY, aw: this.aw, x0: plan[PLAN_X0]!, y0: plan[PLAN_Y0]!,
      ww: plan[PLAN_WW]!, hh: plan[PLAN_HH]!, ylo: plan[PLAN_YLO]!, prevHashes, curHashes, shifts, refY: rf.y,
      refU: rf.u, refV: rf.v, yorg: this.yorg, ys: this.ys, corg: this.corg, cs: this.cs, wpFit,
    }
  }

  applyScan(plan: Int32Array, shifts: Int32Array) {
    const dy = plan[PLAN_DY]!
    if (dy !== 0) {
      this.scrollMvx = 0
      this.scrollMvy = dy * 4
      this.scrollMb0 = plan[PLAN_MINY]! * this.mbw + plan[PLAN_MINX]!
      this.scrollMb1 = plan[PLAN_MAXY]! * this.mbw + plan[PLAN_MAXX]!
    }
    if (plan[PLAN_SHIFT]! === 1) {
      this.shiftHx = shifts[0]! * 4
      if (this.scrollMvy === 0) this.shiftVy = shifts[1]! * 4
    }
  }

  scanConcurrent(frame: Uint8Array, frameOff: int, plan: Int32Array) {
    const mbs = this.mbw * this.mbh
    const weigh = this.params.weightedPred && plan[PLAN_N]! * 20 >= mbs * 19
    this.scanPlan(plan, weigh)
    const s = this.scanOf(plan, frame, frameOff, new Uint32Array(plan[PLAN_PREV]!), new Uint32Array(plan[PLAN_CUR]!),
      new Int32Array(2), this.refs[0]!, this.wpFit)
    this.runScan(s, plan)
    this.applyScan(plan, s.shifts)
    this.wpScanned = weigh
  }

  runScan(s: FrameScan, plan: Int32Array) {
    const jobs = plan[PLAN_JOBS]!
    const next = new Int32Array(2)
    const helpers = this.threads - 1 < jobs - 1 ? this.threads - 1 : jobs - 1
    // Helpers are never joined: one the scheduler starts late finds no job left and touches nothing, so waiting
    // for it would only add its wake-up latency; the completed-job count orders the results instead. The job
    // count goes by value because peekAnchor's plan is rewritten by later peeks.
    for (let k = 0; k < helpers; k++) Thread.spawn(() => scanJobs(s, plan, next, jobs))
    scanJobs(s, plan, next, jobs)
    let done = Atomics.load(next, 1)
    while (done < jobs) {
      Atomics.wait(next, 1, done, 1)
      done = Atomics.load(next, 1)
    }
    scanDone(s, plan)
  }

  peekAnchor(idx: int) {
    const next = idx + 1
    if (this.threads < 2 || this.nextFrame.length === 0 || this.fullAnalysis || this.refs.length === 0 ||
      next - this.pocBase >= this.keyint || Atomics.load(this.nextRead, 0) !== 1) {
      return
    }
    const mbs = this.mbw * this.mbh
    const plan = this.pkPlan
    const n = this.diffFrame(this.nextFrame, this.nextOff, this.pkChanged, plan)
    this.pkWeighed = false
    if (n * 4 >= mbs) {
      const weigh = this.params.weightedPred && n * 20 >= mbs * 19
      this.scanPlan(plan, weigh)
      const s = this.scanOf(plan, this.nextFrame, this.nextOff, this.pkPrev.subarray(0, plan[PLAN_PREV]!),
        this.pkCur.subarray(0, plan[PLAN_CUR]!), this.pkShifts, this.refs[0]!, this.pkFit)
      this.runScan(s, plan)
      this.pkWeighed = weigh
    }
    this.peeked = true
    this.peekedFrame = this.nextFrame
    this.peekedOff = this.nextOff
    this.peekedIndex = next
  }

  expectNext(frame: Uint8Array, frameOff: int, ready: Int32Array) {
    this.nextFrame = frame
    this.nextOff = frameOff
    this.nextRead = ready
    if (frame.length === 0 || this.pkChanged.length > 0) return
    this.pkSync = progressArray(3)
    this.pkChanged = new Uint8Array(this.mbw * this.mbh)
    this.pkPlan = new Int32Array(this.planSize())
    this.pkPrev = new Uint32Array(this.h)
    this.pkCur = new Uint32Array(this.h)
    this.pkShifts = new Int32Array(2)
  }

  peekLead(pk: Peek) {
    // The next frame is compared with srcY, which holds the current frame's source only after every row is prepped;
    // the weighted-prediction fit is left to analyse because it needs the current frame's reconstruction.
    this.waitRow(pk.sync, PEEK_PREPPED, this.mbh)
    if (Atomics.load(pk.ready, 0) !== 1) {
      publish(pk.sync, PEEK_PHASE, 3)
      return
    }
    const n = this.diffFrame(pk.frame, pk.frameOff, pk.changed, pk.plan)
    if (n * 4 >= this.mbw * this.mbh) {
      this.scanPlan(pk.plan, false)
    } else {
      pk.plan[PLAN_JOBS] = 0
    }
    publish(pk.sync, PEEK_PHASE, 2)
  }

  peekWork(pk: Peek) {
    const sync = pk.sync
    if (Atomics.compareExchange(sync, PEEK_PHASE * ROW_SLOT, 0, 1) === 0) this.peekLead(pk)
    this.waitRow(sync, PEEK_PHASE, 2)
    if (Atomics.load(sync, PEEK_PHASE * ROW_SLOT) !== 2) return
    const plan = pk.plan
    const jobs = plan[PLAN_JOBS]!
    if (jobs === 0) return
    const s = this.scanOf(plan, pk.frame, pk.frameOff, pk.prev.subarray(0, plan[PLAN_PREV]!),
      pk.cur.subarray(0, plan[PLAN_CUR]!), pk.shifts, this.cur, this.wpFit)
    while (true) {
      const j = Atomics.add(sync, PEEK_NEXT * ROW_SLOT, 1)
      if (j >= jobs) return
      runScanJob(s, plan[PLAN_JOB0 + j]!)
      if (Atomics.add(sync, PEEK_NEXT * ROW_SLOT + 1, 1) === jobs - 1) scanDone(s, plan)
    }
  }

  estimateWeights(frame: Uint8Array, frameOff: int, nChanged: int): int {
    const rf = this.refs[0]!
    const fit = this.wpFit
    if (!this.wpScanned) {
      weightFit(frame, frameOff, this.w, this.h, rf.y, rf.u, rf.v, this.yorg, this.ys, this.corg, this.cs, fit)
    }
    this.wpScanned = false
    if (fit[0]! === 0) return nChanged
    this.wpOn = true
    this.wpY = [fit[1]!, fit[2]!]
    this.wpU = [fit[3]!, fit[4]!]
    this.wpV = [fit[5]!, fit[6]!]
    this.wpChromaOn = fit[7]! === 1
    this.wpFrames++
    const plane = rf.y.length
    if (this.wref.length !== plane) this.wref = newPlane(plane)
    const wv = i16x8.splat(fit[1]!)
    const ov = i16x8.splat(fit[2]!)
    const rr = i16x8.splat(32)
    for (let i = 0; i + 16 <= plane; i += 16) {
      const v = u8x16.load(rf.y, i)
      u8x16.packSat(((v.widenLow() * wv + rr) >> 6) + ov, ((v.widenHigh() * wv + rr) >> 6) + ov).store(this.wref, i)
    }
    let n2 = nChanged
    const mbs = this.mbw * this.mbh
    for (let i = 0; i < mbs; i++) {
      if (this.changed[i]! !== 0) continue
      this.changed[i] = 1
      n2++
    }
    return n2
  }

  markRefresh(): int {
    if (this.refreshDelta <= 0) return 0
    const mbs = this.mbw * this.mbh
    let n = 0
    for (let mb = 0; mb < mbs; mb++) {
      if (this.prevChanged[mb]! !== 0 && this.changed[mb]! === 0) {
        this.refresh[mb] = 1
        n++
      }
    }
    return n
  }

  markIdleRefine(): int {
    if (!this.rcAct || this.refreshDelta <= 0 || this.lastCodedIndex < 0) return 0
    if (this.frameIndex - this.lastActive < Math.round(this.fpsF * 0.5)) return 0
    const mbs = this.mbw * this.mbh
    const lowest = this.rate.vbrMinQp - this.refreshDelta + this.refineGain
    let n = 0
    for (let mb = 0; mb < mbs; mb++) {
      if (this.codedQp[mb]! >= lowest) n++
    }
    if (n === 0) return 0
    const limit = this.rate.peekQp(n, this.frameIndex - this.lastCodedIndex) - this.refreshDelta + this.refineGain
    n = 0
    for (let mb = 0; mb < mbs; mb++) {
      if (this.codedQp[mb]! >= limit) {
        this.refresh[mb] = 1
        n++
      }
    }
    return n
  }

  classifyCount(mbx: int, mby: int): int {
    const cls = classifyMb(this.srcY, mby * 16 * this.aw + mbx * 16, this.aw)
    if (cls === AQ_TEXT) this.textMbs++
    return cls
  }

  aqOffset(mb: int, mbx: int, mby: int): int {
    if (this.aqStrength <= 0) return 0
    const cls = this.classReady ? this.mbClass[mb]! : this.classifyCount(mbx, mby)
    if (cls === AQ_TEXT) return -3 * this.aqStrength
    return cls === AQ_TEXTURE ? this.aqTexture * this.aqStrength : 0
  }

  // textMbs only reaches motion search through the sub-pel settings tested here; when the text and video
  // settings agree, the wavefront skips the frame-wide text count (and the barrier it needs).
  textGated(): bool {
    if (this.aqStrength <= 0 || this.subpel <= 0) return false
    if (this.subpelDirs !== this.videoHpelDirs) return true
    return this.subpel >= 2 && (this.subpelDirs !== this.videoQpelDirs || (this.hpelSad && !this.qpelSad))
  }

  classifyInput(frame: Uint8Array, frameOff: int, mbx: int, mby: int): int {
    const w = this.w
    const h = this.h
    const px = mbx * 16
    const py = mby * 16
    if (px + 16 <= w && py + 16 <= h) return classifyMb(frame, frameOff + py * w + px, w)
    const b = this.edgeBlk
    for (let y = 0; y < 16; y++) {
      const so = frameOff + (py + y < h ? py + y : h - 1) * w
      for (let x = 0; x < 16; x++) b[y * 16 + x] = frame[so + (px + x < w ? px + x : w - 1)]!
    }
    return classifyMb(b, 0, 16)
  }

  classifyRow(frame: Uint8Array, frameOff: int, full: bool, mby: int): int {
    const mbw = this.mbw
    let n = 0
    for (let mbx = 0; mbx < mbw; mbx++) {
      const mb = mby * mbw + mbx
      const ch = this.changed[mb]! !== 0
      if (!full && !ch && this.refresh[mb]! === 0) continue
      const cls = full || !ch ? classifyMb(this.srcY, mby * 16 * this.aw + mbx * 16, this.aw) :
        this.classifyInput(frame, frameOff, mbx, mby)
      this.mbClass[mb] = cls
      if (cls === AQ_TEXT) n++
    }
    return n
  }

  classifyFrame(frame: Uint8Array, frameOff: int, full: bool, next: Int32Array, done: Int32Array, text: Int32Array) {
    const t = performance.now()
    const mbh = this.mbh
    while (true) {
      const r = Atomics.add(next, 0, 1)
      if (r >= mbh) break
      text[r] = this.classifyRow(frame, frameOff, full, r)
      bump(done, 0)
    }
    this.waitRow(done, 0, mbh)
    let n = 0
    for (let r = 0; r < mbh; r++) n += text[r]!
    this.textMbs = n
    this.classReady = true
    this.rPrep += performance.now() - t
  }

  laHorizon(): int {
    const since = this.bframes > 0 ? this.inputIndex - this.pocBase : this.sinceIdr
    const left = this.refs.length === 0 || since >= this.keyint ? this.keyint : this.keyint - since
    return left < this.laSpan ? left : this.laSpan
  }

  setLookahead(life: Int32Array) {
    this.laLife = life
    this.laNext = life.length > 0
  }

  laHorizonAt(idx: int): int {
    const k = idx % this.keyint
    const left = k === 0 ? this.keyint : this.keyint - k
    return left < this.laSpan ? left : this.laSpan
  }

  wantsKeepMap(): bool {
    return this.bframes > 0 && this.laL1 > 0.0
  }

  flatLookahead(): bool {
    return this.keyint === 1
  }

  takeLookahead(look: Lookahead, life: Int32Array, keep: Uint8Array) {
    if (this.flatLookahead()) {
      if (this.flatLife.length === 0) this.flatLife = new Int32Array(this.mbw * this.mbh)
      this.setLookahead(this.flatLife)
      return
    }
    look.takeInto(this.laHorizon(), life)
    if (this.wantsKeepMap()) look.keepMap(keep)
    this.useLookahead(life, keep)
  }

  useLookahead(life: Int32Array, keep: Uint8Array) {
    this.setLookahead(life)
    if (this.wantsKeepMap()) this.laKeep = keep
  }

  anchorLookahead(life: Int32Array, keep: Uint8Array, idx: int) {
    this.setLookahead(life)
    if (!this.laNext || this.laL1 <= 0.0 || this.pending.length === 0 || keep.length === 0) return
    const mbs = this.mbw * this.mbh
    const p0 = this.lastAnchorIndex
    const span = f64(idx - p0)
    const scale = this.laL1 * f64(LIFE_ONE)
    const one = f64(KEEP_ONE)
    const last = this.pending.length - 1
    for (let mb = 0; mb < mbs; mb++) {
      let chain = f64(keep[mb]!) / one
      let credit = 0.0
      for (let k = last; k >= 0; k--) {
        const p = this.pending[k]!
        credit += chain * f64(p.index - p0) / span
        chain *= f64(p.keep[mb]!) / one
      }
      life[mb] = life[mb]! + Math.trunc(scale * credit + 0.5)
    }
  }

  lifeOffset(life: int, horizon: int): int {
    const l = life < LIFE_ONE ? LIFE_ONE : life < horizon ? life : horizon
    const o = this.laBase + this.laStrength * Math.log2(f64(this.laSpan * LIFE_ONE) / f64(l))
    return Math.round(o < this.laMax ? o : this.laMax)
  }

  assignMbQp(mb: int, mbx: int, mby: int, full: bool, idr: bool, longStatic: int) {
    if (!full && this.changed[mb]! === 0 && this.refresh[mb]! === 0) return
    const q = this.baseMbQp(mb, idr, longStatic) + this.aqOffset(mb, mbx, mby)
    this.mbQp[mb] = q < 0 ? 0 : q > 51 ? 51 : q
  }

  baseMbQp(mb: int, idr: bool, longStatic: int): int {
    let q = this.frameQp
    if (this.laOn) q += this.lifeOffset(this.laLife[mb]!, this.laHz)
    if (this.lifetimeAq && this.frameIndex > 0 && !(this.laOn && idr)) {
      const age = this.frameIndex - this.lastChange[mb]!
      const st = this.streak[mb]!
      const gap = this.frameIndex - this.lastAnalysed
      if (idr) {
        if (age >= longStatic) {
          q -= 2
        } else if (st >= 3 && age <= gap) {
          q += 1
        }
      } else if (this.changed[mb]! !== 0) {
        if (st >= 3 && age <= gap) {
          q += 2
        } else if (age >= longStatic) {
          q -= 2
        }
      }
    }
    if (this.refresh[mb]! !== 0) q -= this.refreshDelta
    this.codedQp[mb] = q
    return q
  }

  aqRow(mby: int) {
    for (let mbx = 0; mbx < this.mbw; mbx++) {
      const mb = mby * this.mbw + mbx
      const q = this.mbQp[mb]! + this.aqOffset(mb, mbx, mby)
      this.mbQp[mb] = q < 0 ? 0 : q > 51 ? 51 : q
    }
  }

  updateMbHistory(mb: int) {
    if (this.changed[mb]! !== 1) return
    this.streak[mb] = this.lastChange[mb]! === this.lastAnalysed ? this.streak[mb]! + 1 : 1
    this.lastChange[mb] = this.frameIndex
  }

  updateHistory() {
    const mbs = this.mbw * this.mbh
    for (let mb = 0; mb < mbs; mb++) this.updateMbHistory(mb)
    this.lastAnalysed = this.frameIndex
  }

  prepRowTimed(frame: Uint8Array, frameOff: int, mby: int, full: bool, idr: bool) {
    const t = performance.now()
    this.prepRow(frame, frameOff, mby, full, idr)
    this.rPrep += performance.now() - t
  }

  prepRow(frame: Uint8Array, frameOff: int, mby: int, full: bool, idr: bool) {
    const mbw = this.mbw
    const longStatic = Math.round(this.fpsF)
    for (let mbx = 0; mbx < mbw; mbx++) {
      const mb = mby * mbw + mbx
      const ch = this.changed[mb]! !== 0
      if (!full && ch) {
        if (this.idrCache) this.saveIdrSource(frame, frameOff, mbx, mby)
        this.copyMbFromFrame(frame, frameOff, mbx, mby)
      }
      this.assignMbQp(mb, mbx, mby, full, idr, longStatic)
      this.updateMbHistory(mb)
      if (!idr && !ch && this.refresh[mb]! === 0) this.syncMb(mb, mbx, mby)
    }
  }

  finishQp(mb: int) {
    const t = this.mbType[mb]!
    const has = t === MB_I16 || (t !== MB_PSKIP && this.cbp[mb]! !== 0)
    if (has) {
      let d = this.mbQp[mb]! - this.qpPred
      if (d > 25) d -= 52
      if (d < -26) d += 52
      this.qpDelta[mb] = d
      this.qpY[mb] = this.mbQp[mb]!
    } else {
      this.qpDelta[mb] = 0
      this.qpY[mb] = this.qpPred
    }
    this.qpPred = this.qpY[mb]!
  }

  noteHints(side: SideInfo) {
    if (side.scroll.length > 0) {
      const sc = side.scroll[0]!
      this.scrollMvx = -sc.dx * 4
      this.scrollMvy = -sc.dy * 4
      const x1 = sc.x + sc.w - 1
      const y1 = sc.y + sc.h - 1
      this.scrollMb0 = (sc.y >> 4) * this.mbw + (sc.x >> 4)
      this.scrollMb1 = ((y1 >> 4) < this.mbh ? y1 >> 4 : this.mbh - 1) * this.mbw + ((x1 >> 4) < this.mbw ? x1 >> 4 : this.mbw - 1)
    }
    if (side.cursorX >= 0 && this.prevCursorX >= 0) {
      this.cursorMvx = -(side.cursorX - this.prevCursorX) * 4
      this.cursorMvy = -(side.cursorY - this.prevCursorY) * 4
      this.cursorMb = (side.cursorY >> 4) * this.mbw + (side.cursorX >> 4)
    }
    this.prevCursorX = side.cursorX
    this.prevCursorY = side.cursorY
  }

  inScroll(mbx: int, mby: int): bool {
    if (this.scrollMb0 < 0) return false
    const y0 = Math.trunc(this.scrollMb0 / this.mbw)
    const x0 = this.scrollMb0 - y0 * this.mbw
    const y1 = Math.trunc(this.scrollMb1 / this.mbw)
    const x1 = this.scrollMb1 - y1 * this.mbw
    return mbx >= x0 && mbx <= x1 && mby >= y0 && mby <= y1
  }

  refMarking(idr: bool) {
    let reorder: int[] = []
    let mmco: int[] = []
    if (this.bRefNum >= 0 && !idr) {
      const max = 1 << this.params.log2MaxFrameNum
      const cur = this.frameNum
      let pred = cur
      for (let i = 0; i < this.refsActive; i++) {
        const f = this.refs[i]!.frameNum
        const pn = f > cur ? f - max : f
        reorder.push(pn < pred ? 0 : 1)
        reorder.push(iabs(pred - pn) - 1)
        pred = pn
      }
      const b = this.bRefNum > cur ? this.bRefNum - max : this.bRefNum
      mmco.push(cur - b - 1)
    }
    this.l0Reorder = reorder
    this.mmco = mmco
    this.bRefNum = -1
  }

  syncMb(mb: int, mbx: int, mby: int) {
    const ref0 = this.refs[0]!
    if (this.cur.ver[mb]! === ref0.ver[mb]!) return
    copyRect(this.cur.y, this.ry(mbx * 16, mby * 16), this.ys, ref0.y, this.ry(mbx * 16, mby * 16), this.ys, 16, 16)
    copyRect(this.cur.u, this.rc(mbx * 8, mby * 8), this.cs, ref0.u, this.rc(mbx * 8, mby * 8), this.cs, 8, 8)
    copyRect(this.cur.v, this.rc(mbx * 8, mby * 8), this.cs, ref0.v, this.rc(mbx * 8, mby * 8), this.cs, 8, 8)
    this.cur.ver[mb] = ref0.ver[mb]!
    this.padMb(mbx, mby)
  }

  padMb(mbx: int, mby: int) {
    const last = mbx === this.mbw - 1
    const bottom = mby === this.mbh - 1
    if (mbx !== 0 && !last && mby !== 0 && !bottom) return
    this.padPlaneMb(this.cur.y, this.ys, this.yorg, mbx, mby, 16, PADY, this.aw, this.ah)
    this.padPlaneMb(this.cur.u, this.cs, this.corg, mbx, mby, 8, PADC, this.cw, this.chh)
    this.padPlaneMb(this.cur.v, this.cs, this.corg, mbx, mby, 8, PADC, this.cw, this.chh)
  }

  padPlaneMb(p: Uint8Array, stride: int, org: int, mbx: int, mby: int, n: int, pad: int, pw: int, ph: int) {
    const x0 = mbx * n
    const y0 = mby * n
    const left = mbx === 0
    const right = mbx === this.mbw - 1
    if (left || right) {
      for (let y = y0; y < y0 + n; y++) {
        const row = org + y * stride
        if (left) fillBytes(p, row - pad, pad, p[row]!)
        if (right) fillBytes(p, row + pw, pad, p[row + pw - 1]!)
      }
    }
    const xa = left ? x0 - pad : x0
    const xb = right ? x0 + n + pad : x0 + n
    if (mby === 0) {
      for (let y = 1; y <= pad; y++) copyBytes(p, org - y * stride + xa, p, org + xa, xb - xa)
    }
    if (mby === this.mbh - 1) {
      const lastRow = org + (ph - 1) * stride
      for (let y = 1; y <= pad; y++) copyBytes(p, lastRow + y * stride + xa, p, lastRow + xa, xb - xa)
    }
  }

  unchangedMb(mbx: int, mby: int) {
    const mb = mby * this.mbw + mbx
    this.computeSkipMv(mbx, mby)
    if (this.skipX === 0 && this.skipY === 0 && this.mbType[mb]! === MB_PSKIP && this.mvx[mb]! === 0 && this.mvy[mb]! === 0) {
      return
    }
    this.cbp[mb] = 0
    this.t8[mb] = 0
    this.dcCbf[mb] = 0
    for (let k = 0; k < 24; k++) this.nnz[mb * 24 + k] = 0
    if (this.skipX === 0 && this.skipY === 0) {
      this.setSkip(mb)
      return
    }
    this.mbType[mb] = MB_P16
    this.refIdx[mb] = 0
    this.computeMvp(mbx, mby, 0)
    this.mvx[mb] = 0
    this.mvy[mb] = 0
    this.mvdx[mb] = -this.mvpX
    this.mvdy[mb] = -this.mvpY
  }

  probeIntra(frame: Uint8Array, frameOff: int, side: SideInfo | undefined) {
    const o = this.opts
    const qp = this.rate.probeQpValue()
    const p = new Encoder({
      width: o.width, height: o.height, fpsNum: o.fpsNum, fpsDen: o.fpsDen, qp, keyint: o.keyint, profile: o.profile,
      preset: o.preset, cabac: o.cabac, refs: o.refs, rcMode: RC_CQP, crf: o.crf, bitrate: 0, maxrate: 0, bufsize: 0,
      ipOffset: 0, weightedPred: o.weightedPred, worker: false,
    })
    p.rate.minQp = 0
    p.threads = this.threads
    p.vfr = false
    p.aqStrength = this.aqStrength
    p.aqTexture = this.aqTexture
    p.noise = this.noise
    p.idrSlices = this.idrSlices
    p.sliceRowsI = this.sliceRowsI
    this.rate.seedIntra(qp, p.encodeFrame(frame, frameOff, side).length * 8)
  }

  frameActivity(): int {
    let a = 0
    for (let mby = 0; mby < this.mbh; mby++) {
      for (let mbx = 0; mbx < this.mbw; mbx++) a += mbActivity(this.srcY, mby * 16 * this.aw + mbx * 16, this.aw)
    }
    return a
  }

  encodeFrame(frame: Uint8Array, frameOff: int, side: SideInfo | undefined): u8[] {
    const t0 = performance.now()
    this.out = []
    const idr = this.refs.length === 0 || this.sinceIdr >= this.keyint
    const full = idr || this.fullAnalysis
    this.filterRecon = !idr || this.keyint > 1 || this.idrReuse || this.wantRecon
    let nChanged = this.analyse(frame, frameOff, side, full)
    this.wpOn = false
    this.wpChromaOn = false
    if (!idr && this.params.weightedPred && this.refs.length > 0 && nChanged * 20 >= this.mbw * this.mbh * 19) {
      nChanged = this.estimateWeights(frame, frameOff, nChanged)
    }
    let nRefresh = full ? 0 : this.markRefresh()
    if (!full && nChanged === 0 && nRefresh === 0) nRefresh = this.markIdleRefine()
    if (full || nChanged > 0 || nRefresh > 0) this.lastActive = this.frameIndex
    const willDrop = !full && nChanged === 0 && nRefresh === 0 && this.vfr
    if (!willDrop) {
      const dt = this.lastCodedIndex < 0 ? 1 : this.frameIndex - this.lastCodedIndex
      const act = idr && this.rcAct ? f64(this.frameActivity()) : 0.0
      if (idr && this.rate.wantsProbe(act)) this.probeIntra(frame, frameOff, side)
      this.frameQp = this.rate.frameQp(idr, act, idr ? this.mbw * this.mbh : nChanged + nRefresh, dt)
    }
    const left = idr ? this.keyint : this.keyint - this.sinceIdr
    this.laOn = this.laNext
    this.laHz = (left < this.laSpan ? left : this.laSpan) * LIFE_ONE
    this.laNext = false
    if (willDrop) {
      this.updateHistory()
      const td = performance.now()
      this.tAnalysis += td - t0
      this.frames.push({
        index: this.frameIndex, type: "D", bytes: 0, qp: this.qp, changedMbs: 0, analysis: td - t0, motion: 0.0,
        mode: 0.0, tq: 0.0, entropy: 0.0, deblock: 0.0, total: td - t0, reused: 0,
      })
      this.frameIndex++
      this.sinceIdr++
      return this.out
    }
    if (!full) this.holdChanged()
    const parallel = this.threads > 1 && this.parallelWorth(idr)
    this.textMbs = 0
    if (!parallel) {
      for (let mby = 0; mby < this.mbh; mby++) this.prepRow(frame, frameOff, mby, full, idr)
    }
    this.resetPacking()
    this.reusedMbs = 0
    if (idr && this.idrReuse) this.prepareIdrCache()
    const t1 = performance.now()
    if (idr) {
      appendNal(&this.out, 3, 7, writeSps(this.params), true)
      appendNal(&this.out, 3, 8, writePps(this.params), true)
      this.frameNum = 0
      while (this.refs.length > 0) this.spare.push(this.refs.pop()!)
      this.sinceIdr = 0
    }
    this.sliceType = idr ? SLICE_I : SLICE_P
    this.refsActive = this.refs.length < this.maxRefs ? (this.refs.length > 0 ? this.refs.length : 1) : this.maxRefs
    this.refMarking(idr)
    this.anchorQp = this.frameQp
    const cabac = this.params.cabac
    const rowsI = this.sliceRowsI > 0 ? this.sliceRowsI : this.idrSlices > 1 ? Math.trunc((this.mbh + this.idrSlices - 1) / this.idrSlices) : this.mbh
    const rowsP = this.sliceRowsP > 0 ? this.sliceRowsP : this.mbh
    const sliceRows = idr ? rowsI : rowsP
    const mbs0 = this.mbw * this.mbh
    let tmo = 0.0
    let tmd = 0.0
    let ttq = 0.0
    let tent = 0.0
    let tdb = 0.0
    let tprep = 0.0
    this.rMo = 0.0
    this.rMd = 0.0
    this.rTq = 0.0
    this.rEnt = 0.0
    this.rDb = 0.0
    this.rPrep = 0.0
    this.rUn = 0.0
    this.tN = 0
    this.tNs = 0
    this.uN = 0
    this.uNs = 0
    const nSlices = Math.trunc((this.mbh + sliceRows - 1) / sliceRows)
    if (parallel && idr && nSlices > 1) {
      const tp = performance.now()
      const sums = this.codeSlicesParallel(frame, frameOff, sliceRows, nSlices)
      const span = performance.now() - tp
      const sum = sums[0]! + sums[1]! + sums[2]! + sums[3]! + sums[4]! + sums[5]!
      if (sum > 0.0) {
        tmo = span * sums[0]! / sum
        tmd = span * sums[1]! / sum
        ttq = span * sums[2]! / sum
        tent = span * sums[3]! / sum
        tdb = span * sums[4]! / sum
        tprep = span * sums[5]! / sum
      }
    } else if (parallel) {
      const pipe = nSlices === 1
      const peek = this.nextFrame.length > 0 && !this.fullAnalysis && this.sinceIdr + 1 < this.keyint &&
        this.nextInputAnalysed()
      const tp = performance.now()
      const tPeek = this.codeFrameParallel(frame, frameOff, idr, full, sliceRows, pipe, peek)
      const span = performance.now() - tp - tPeek
      const sum = this.rMo + this.rMd + this.rTq + this.rEnt + this.rDb + this.rPrep
      if (sum > 0.0) {
        tmo = span * this.rMo / sum
        tmd = span * this.rMd / sum
        ttq = span * this.rTq / sum
        tent = span * this.rEnt / sum
        tdb = span * this.rDb / sum
        tprep = span * this.rPrep / sum
      }
      tprep += tPeek
      if (!pipe) {
        const e0 = performance.now()
        this.entropyParallel(idr, sliceRows)
        tent += performance.now() - e0
      }
    } else {
      const c0 = performance.now()
      for (let mby = 0; mby < this.mbh; mby++) this.codeRow(mby, idr, sliceRows)
      this.scaleTiming()
      const span = performance.now() - c0
      const sum = this.rMo + this.rMd + this.rTq
      if (sum > 0.0) {
        tmo = span * this.rMo / sum
        tmd = span * this.rMd / sum
        ttq = span * this.rTq / sum
      } else {
        tmd = span
      }
      const e0 = performance.now()
      if (this.threads > 1 && sliceRows < this.mbh) {
        this.entropyParallel(idr, sliceRows)
      } else {
        this.finishQpRows(sliceRows, 0, this.mbh)
        this.entropyRows(idr, sliceRows, 0, this.mbh, &this.out)
      }
      tent = performance.now() - e0
    }
    this.sliceRow0 = 0
    this.lastAnalysed = this.frameIndex
    const t2 = performance.now()
    this.counter++
    for (let mb = 0; mb < mbs0; mb++) {
      if (this.refresh[mb]! !== 0) this.changed[mb] = 2
    }
    if (idr && this.idrReuse) {
      this.idrCache = true
      this.compactIdrPool()
    }
    if (!parallel && this.filterRecon) this.deblockFrame(idr)
    const t3 = performance.now()
    tdb += t3 - t2
    if (idr) this.idrPicId = (this.idrPicId + 1) & 1
    this.cur.frameNum = this.frameNum
    this.cur.id = this.codedFrames
    this.genSeq++
    this.cur.gen = this.genSeq
    this.refs.unshift(this.cur)
    const keep = this.pending.length > 0 ? this.dpbRefs : this.maxRefs
    while (this.refs.length > keep) this.spare.push(this.refs.pop()!)
    this.giveHalfPlanes(this.cur)
    this.cur = this.spare.length > 0 ? this.spare.pop()! : this.newFrame()
    const mbs = this.mbw * this.mbh
    for (let mb = 0; mb < mbs; mb++) {
      this.prevMvx[mb] = this.mvx[mb]!
      this.prevMvy[mb] = this.mvy[mb]!
    }
    this.frameNum = (this.frameNum + 1) & ((1 << this.params.log2MaxFrameNum) - 1)
    for (let mb = 0; mb < mbs; mb++) this.prevChanged[mb] = idr || this.changed[mb]! !== 1 ? 0 : 1
    const t4 = performance.now()
    this.tAnalysis += t1 - t0 + (t4 - t3) + tprep
    this.tMotion += tmo
    this.tMode += tmd
    this.tTq += ttq
    this.tEntropy += tent
    this.tDeblock += tdb
    this.frames.push({
      index: this.frameIndex, type: idr ? "I" : "P", bytes: this.out.length, qp: this.frameQp,
      changedMbs: idr ? mbs : nChanged, analysis: t1 - t0 + (t4 - t3) + tprep, motion: tmo, mode: tmd, tq: ttq,
      entropy: tent, deblock: tdb, total: t4 - t0, reused: this.reusedMbs,
    })
    this.rate.update(idr, this.frameQp, this.out.length * 8, idr ? mbs : nChanged + nRefresh)
    this.lastCodedIndex = this.frameIndex
    this.frameIndex++
    this.codedFrames++
    this.sinceIdr++
    return this.out
  }

  prepareIdrCache() {
    const mbs = this.mbw * this.mbh
    if (this.cType.length === 0) {
      this.srcSame = new Uint8Array(mbs)
      this.recSame = new Uint8Array(mbs)
      this.idrSaved = new Int32Array(mbs)
      this.idrSlotMb = new Int32Array(mbs)
      this.idrPool = newPlane(mbs * 384)
      this.idrRecY = newPlane(this.ysize)
      this.idrRecU = newPlane(this.cs * (this.chh + 2 * PADC))
      this.idrRecV = newPlane(this.cs * (this.chh + 2 * PADC))
      this.cType = new Uint8Array(mbs)
      this.cI16 = new Uint8Array(mbs)
      this.cCMode = new Uint8Array(mbs)
      this.cCbp = new Uint8Array(mbs)
      this.cDcCbf = new Uint8Array(mbs)
      this.cQp = new Uint8Array(mbs)
      this.cImode = new Uint8Array(mbs * 16)
      this.cNnz = new Uint8Array(mbs * 24)
      this.cCoef = new Int8Array(this.mbh * this.cRow)
      this.cMask = new Int64Array(mbs)
      this.cOff = new Int32Array(mbs)
    }
    for (let i = 0; i < mbs; i++) this.recSame[i] = 0
    if (!this.idrCache) {
      for (let i = 0; i < mbs; i++) this.srcSame[i] = 0
      return
    }
    for (let mby = 0; mby < this.mbh; mby++) {
      for (let mbx = 0; mbx < this.mbw; mbx++) {
        const mb = mby * this.mbw + mbx
        const slot = this.idrSaved[mb]!
        let same = true
        if (slot !== 0) {
          const yo = mby * 16 * this.aw + mbx * 16
          const co = mby * 8 * this.cw + mbx * 8
          const po = (slot - 1) * 384
          same = !diffMb(this.srcY, yo, this.aw, this.idrPool, po, 16, 16, 16, this.noise)
          if (same) same = !diffMb(this.srcU, co, this.cw, this.idrPool, po + 256, 8, 8, 8, this.noise)
          if (same) same = !diffMb(this.srcV, co, this.cw, this.idrPool, po + 320, 8, 8, 8, this.noise)
        }
        this.srcSame[mb] = same ? 1 : 0
      }
    }
  }

  idrReuseLevel(mb: int, mbx: int, mby: int): int {
    if (!this.idrCache || this.srcSame[mb]! === 0 || this.cQp[mb]! > this.mbq) return 0
    let rec = true
    if (mbx > 0) {
      if (this.srcSame[mb - 1]! === 0) return 0
      if (this.recSame[mb - 1]! === 0) rec = false
    }
    if (mby > this.sliceRow0) {
      const t = mb - this.mbw
      if (this.srcSame[t]! === 0) return 0
      if (this.recSame[t]! === 0) rec = false
      if (mbx > 0) {
        if (this.srcSame[t - 1]! === 0) return 0
        if (this.recSame[t - 1]! === 0) rec = false
      }
      if (mbx + 1 < this.mbw) {
        if (this.srcSame[t + 1]! === 0) return 0
        if (this.recSame[t + 1]! === 0) rec = false
      }
    }
    return rec ? 2 : 1
  }

  restoreIdrDecisions(mb: int) {
    const t = this.cType[mb]!
    this.mbType[mb] = t
    this.refIdx[mb] = -1
    this.i16Mode[mb] = this.cI16[mb]!
    this.cMode[mb] = this.cCMode[mb]!
    this.mvx[mb] = 0
    this.mvy[mb] = 0
    this.mvdx[mb] = 0
    this.mvdy[mb] = 0
    u8x16.load(this.cImode, mb * 16).store(this.imode, mb * 16)
    this.mbq = this.cQp[mb]!
    this.mbQp[mb] = this.mbq
  }

  beginIdrRow(mby: int) {
    this.rowAt = 0
    if (!this.idrCache) return
    const mb0 = mby * this.mbw
    const row = mby * this.cRow
    for (let mbx = 0; mbx < this.mbw; mbx++) {
      const mb = mb0 + mbx
      if (!this.mayRestoreCoding(mb, mbx, mby)) continue
      if (this.rowOld.length === 0) this.rowOld = new Int8Array(this.cRow)
      const o = this.cOff[mb]!
      const n = this.cBytes(mb)
      for (let k = 0; k < n; k += 16) u8x16.load(this.cCoef, row + o + k).store(this.rowOld, o + k)
    }
  }

  cBytes(mb: int): int {
    const g = this.cMask[mb]!
    return g < 0 ? (Math.popcount64(g) - 1) * 16 : Math.popcount64(g) * 8
  }

  mayRestoreCoding(mb: int, mbx: int, mby: int): bool {
    if (this.srcSame[mb]! === 0) return false
    if (mbx > 0 && this.srcSame[mb - 1]! === 0) return false
    if (mby > this.sliceRow0) {
      const t = mb - this.mbw
      if (this.srcSame[t]! === 0) return false
      if (mbx > 0 && this.srcSame[t - 1]! === 0) return false
      if (mbx + 1 < this.mbw && this.srcSame[t + 1]! === 0) return false
    }
    return true
  }

  restoreIdrCoding(mb: int, mbx: int, mby: int) {
    this.reusedMbs++
    this.cbp[mb] = this.cCbp[mb]!
    this.dcCbf[mb] = this.cDcCbf[mb]!
    this.t8[mb] = this.cType[mb]! === MB_I8 ? 1 : 0
    u8x16.load(this.cNnz, mb * 24).store(this.nnz, mb * 24)
    u8x16.load(this.cNnz, mb * 24 + 8).store(this.nnz, mb * 24 + 8)
    const yo = this.ry(mbx * 16, mby * 16)
    const uo = this.rc(mbx * 8, mby * 8)
    copyRect(this.cur.y, yo, this.ys, this.idrRecY, yo, this.ys, 16, 16)
    copyRect(this.cur.u, uo, this.cs, this.idrRecU, uo, this.cs, 8, 8)
    copyRect(this.cur.v, uo, this.cs, this.idrRecV, uo, this.cs, 8, 8)
  }

  packSize(): int {
    return (Math.trunc((this.mbw * this.mbh * COEF_PER_MB - 1) / (PK_BLOCK - COEF_PER_MB)) + this.threads + 2) * PK_BLOCK
  }

  resetPacking() {
    const size = this.packSize()
    if (this.pkCoef.length < size) this.pkCoef = new Int16Array(size)
    this.pkTop[0] = 0
    this.pkW = 0
    this.pkEnd = 0
  }

  packCoef(mb: int) {
    let m = CHUNKS[(this.mbType[mb]! === MB_I16 ? 128 : this.t8[mb]! * 64) + this.cbp[mb]!]!
    if (m === 0) return
    this.pkMask[mb] = m
    const n = Math.popcount32(m) * 32
    if (this.pkW + n > this.pkEnd) {
      this.pkW = Atomics.add(this.pkTop, 0, PK_BLOCK)
      this.pkEnd = this.pkW + PK_BLOCK
    }
    let w = this.pkW
    this.pkW = w + n
    this.pkOff[mb] = w
    while (m !== 0) {
      i16x32.load(this.coef, Math.ctz32(m) * 32).store(this.pkCoef, w)
      w += 32
      m &= m - 1
    }
  }

  loadCoef(mb: int, mby: int, t: int, cbp: int) {
    if (this.sliceType === SLICE_I && this.idrReuse) {
      let m = CHUNKS[(t === MB_I16 ? 128 : this.t8[mb]! * 64) + cbp]!
      const zero = i16x32.splat(0)
      while (m !== 0) {
        zero.store(this.coef, Math.ctz32(m) * 32)
        m &= m - 1
      }
      let g = this.cMask[mb]!
      let r = mby * this.cRow + this.cOff[mb]!
      if (g >= 0) {
        while (g !== 0) {
          i16x8.loadI8(this.cCoef, r).store(this.coef, Math.ctz64(g) * 8)
          r += 8
          g &= g - 1
        }
        return
      }
      g &= 0xfffffffffffff
      while (g !== 0) {
        ((i16x8.loadI8(this.cCoef, r + 8) << 8) | i16x8.loadU8(this.cCoef, r)).store(this.coef, Math.ctz64(g) * 8)
        r += 16
        g &= g - 1
      }
      return
    }
    let m = this.pkMask[mb]!
    let r = this.pkOff[mb]!
    while (m !== 0) {
      i16x32.load(this.pkCoef, r).store(this.coef, Math.ctz32(m) * 32)
      r += 32
      m &= m - 1
    }
  }

  sameRecon(mbx: int, mby: int): bool {
    const yo = this.ry(mbx * 16, mby * 16)
    const uo = this.rc(mbx * 8, mby * 8)
    if (diffMb(this.cur.y, yo, this.ys, this.idrRecY, yo, this.ys, 16, 16, 0)) return false
    if (diffMb(this.cur.u, uo, this.cs, this.idrRecU, uo, this.cs, 8, 8, 0)) return false
    return !diffMb(this.cur.v, uo, this.cs, this.idrRecV, uo, this.cs, 8, 8, 0)
  }

  storeIdrMb(mb: int, mbx: int, mby: int, level: int) {
    const row = mby * this.cRow
    if (level === 2) {
      this.recSame[mb] = 1
      const n = this.cBytes(mb)
      const o = this.cOff[mb]!
      const at = row + this.rowAt
      for (let k = 0; k < n; k += 16) u8x16.load(this.rowOld, o + k).store(this.cCoef, at + k)
      this.cOff[mb] = this.rowAt
      this.rowAt += n
      return
    }
    this.recSame[mb] = this.idrCache && this.sameRecon(mbx, mby) ? 1 : 0
    this.cType[mb] = this.mbType[mb]!
    this.cI16[mb] = this.i16Mode[mb]!
    this.cCMode[mb] = this.cMode[mb]!
    this.cCbp[mb] = this.cbp[mb]!
    this.cDcCbf[mb] = this.dcCbf[mb]!
    this.cQp[mb] = this.mbq
    u8x16.load(this.imode, mb * 16).store(this.cImode, mb * 16)
    u8x16.load(this.nnz, mb * 24).store(this.cNnz, mb * 24)
    u8x16.load(this.nnz, mb * 24 + 8).store(this.cNnz, mb * 24 + 8)
    this.cOff[mb] = this.rowAt
    this.rowAt += this.storeIdrCoef(mb, row + this.rowAt)
    const yo = this.ry(mbx * 16, mby * 16)
    const uo = this.rc(mbx * 8, mby * 8)
    copyRect(this.idrRecY, yo, this.ys, this.cur.y, yo, this.ys, 16, 16)
    copyRect(this.idrRecU, uo, this.cs, this.cur.u, uo, this.cs, 8, 8)
    copyRect(this.idrRecV, uo, this.cs, this.cur.v, uo, this.cs, 8, 8)
    if (level === 0) this.idrSaved[mb] = 0
  }

  storeIdrCoef(mb: int, at: int): int {
    const zero = i16x32.splat(0)
    let mask = 0
    for (let q = 0; q < COEF_PER_MB >> 5; q++) {
      const nz = ~i16x32.load(this.coef, q * 32).eq(zero).mask() & 0xffffffff
      let t = nz | (nz >> 4)
      t |= t >> 2
      t |= t >> 1
      mask |= ((((t & 0x01010101) * 0x204081) >> 21) & 15) << (q * 4)
    }
    this.cMask[mb] = mask
    let w = at
    let m = mask
    let hi = i8x16.splat(0)
    let lo = hi
    while (m !== 0) {
      const v = i16x8.load(this.coef, Math.ctz64(m) * 8)
      m &= m - 1
      const p = i8x16.packSat(v, i16x8.load(this.coef, Math.ctz64(m | 0x8000000000000) * 8))
      p.store(this.cCoef, w)
      hi = hi.max(p)
      lo = lo.min(p)
      w += 16
      m &= m - 1
    }
    if (hi.eq(i8x16.splat(127)).mask() === 0 && lo.eq(i8x16.splat(-128)).mask() === 0) return Math.popcount64(mask) * 8
    this.cMask[mb] = mask | (1 << 63)
    w = at
    m = mask
    const bytes = i16x8.splat(255)
    while (m !== 0) {
      const v = i16x8.load(this.coef, Math.ctz64(m) * 8)
      u8x16.packSat(v & bytes, v).store(this.cCoef, w)
      i8x16.packSat(v >> 8, v).store(this.cCoef, w + 8)
      w += 16
      m &= m - 1
    }
    return w - at
  }

  beginSlice(idr: bool, row0: int) {
    this.sliceRow0 = row0
    const bw = this.bw
    bw.reset()
    const sp: SliceParams = {
      idr, sliceType: this.sliceType, frameNum: this.frameNum, idrPicId: this.idrPicId, qpDelta: this.frameQp - 26,
      refsActive: this.refsActive, deblock: this.deblock ? 0 : 1, alphaOffset: 0, betaOffset: 0, cabacInitIdc: 0,
      firstMb: row0 * this.mbw, wpLuma: this.wpOn && this.wpY.length > 0 && !idr, wpChroma: this.wpChromaOn && !idr,
      wp: [this.wpY[0]!, this.wpY[1]!, this.wpU[0]!, this.wpU[1]!, this.wpV[0]!, this.wpV[1]!],
      poc: 2 * (this.frameIndex - this.pocBase), nalRef: this.nalRef, reorder: this.l0Reorder, mmco: this.mmco,
    }
    writeSliceHeader(bw, this.params, sp)
    if (this.params.cabac) {
      while (!bw.aligned()) bw.putBit(1)
      this.cabac.init(this.frameQp, idr, 0)
    }
    this.skipRun = 0
    this.qpPred = this.frameQp
    this.lastDeltaNz = false
  }

  endSlice(idr: bool, inout out: u8[]) {
    const bw = this.bw
    const refIdc = idr ? 3 : this.sliceType === SLICE_B && !this.nalRef ? 0 : 2
    if (this.params.cabac) {
      appendNalBytes(&out, refIdc, idr ? 5 : 1, bw.buf, this.cabac.out, this.cabac.len)
      return
    }
    if (this.skipRun > 0) bw.ue(this.skipRun)
    bw.trailing()
    appendNal(&out, refIdc, idr ? 5 : 1, bw.buf, true)
  }

  finishQpRows(sliceRows: int, r0: int, r1: int) {
    for (let mby = r0; mby < r1; mby++) {
      if (mby % sliceRows === 0) this.qpPred = this.frameQp
      for (let mbx = 0; mbx < this.mbw; mbx++) this.finishQp(mby * this.mbw + mbx)
    }
  }

  entropyRows(idr: bool, sliceRows: int, r0: int, r1: int, inout out: u8[]) {
    for (let mby = r0; mby < r1; mby++) this.entropyRow(idr, sliceRows, mby, &out)
    this.sliceRow0 = 0
  }

  entropyRow(idr: bool, sliceRows: int, mby: int, inout out: u8[]) {
    const cabac = this.params.cabac
    if (mby % sliceRows === 0) this.beginSlice(idr, mby)
    this.sliceRow0 = mby - mby % sliceRows
    const lastRow = (mby + 1) % sliceRows === 0 || mby === this.mbh - 1
    if (cabac) {
      this.writeRowCabac(mby, lastRow)
    } else {
      for (let mbx = 0; mbx < this.mbw; mbx++) this.writeMbCavlc(mbx, mby)
    }
    if (lastRow) this.endSlice(idr, &out)
  }

  entropyReady(): bool {
    const keep = this.sliceRow0
    let did = false
    while (this.pipeEnt < this.mbh) {
      const at = this.pipeEnt * ROW_SLOT
      if (Atomics.load(this.pipeProgress, at) <= this.mbw) break
      const t = performance.now()
      this.entropyRow(this.pipeIdr, this.pipeSliceRows, this.pipeEnt, &this.out)
      this.rEnt += performance.now() - t
      this.pipeEnt++
      did = true
    }
    this.sliceRow0 = keep
    return did
  }

  workerOpts(): EncoderOptions {
    return {
      width: this.opts.width, height: this.opts.height, fpsNum: this.opts.fpsNum, fpsDen: this.opts.fpsDen,
      qp: this.opts.qp, keyint: this.opts.keyint, profile: this.opts.profile, preset: this.opts.preset,
      cabac: this.opts.cabac, refs: this.opts.refs, rcMode: this.opts.rcMode, crf: this.opts.crf,
      bitrate: this.opts.bitrate, maxrate: this.opts.maxrate, bufsize: this.opts.bufsize, ipOffset: this.opts.ipOffset,
      weightedPred: this.opts.weightedPred, worker: true,
    }
  }

  entropyParallel(idr: bool, sliceRows: int) {
    const sh = this.shareState()
    const fp = this.frameParams()
    const opts = this.workerOpts()
    const mbh = this.mbh
    let threads: Thread<u8[]>[] = []
    for (let r0 = 0; r0 < mbh; r0 += sliceRows) {
      const r1 = r0 + sliceRows < mbh ? r0 + sliceRows : mbh
      threads.push(Thread.spawn(() => {
        const w = new Encoder(opts)
        w.attach(sh, fp)
        let out: u8[] = []
        w.finishQpRows(sliceRows, r0, r1)
        w.entropyRows(idr, sliceRows, r0, r1, &out)
        return out
      }))
    }
    for (const t of threads) {
      const part = t.join()
      for (const b of part) this.out.push(b)
    }
  }

  qpRow(mby: int, sliceRows: int, progress: Int32Array) {
    const mbw = this.mbw
    if (mby % sliceRows === 0) {
      this.qpPred = this.frameQp
    } else {
      this.waitRow(progress, mby - 1, mbw + 1)
      this.qpPred = this.qpY[mby * mbw - 1]!
    }
    for (let mbx = 0; mbx < mbw; mbx++) this.finishQp(mby * mbw + mbx)
    if (this.qpRows.length > 0) publishRows(this.qpRows, mby + 1)
    publish(progress, mby, mbw + 1)
  }

  awaitRows(rows: Int32Array, need: int) {
    // publishRows only wakes the entropy thread once `wake` rows are ready: it then sleeps about once per
    // ENTROPY_BATCH rows instead of once per row, and per row near the end so the frame's tail is not delayed.
    const wake = need + ENTROPY_BATCH < this.mbh ? need + ENTROPY_BATCH - 1 : need
    const start = performance.now()
    while (Atomics.load(rows, 0) < need) {
      if (performance.now() - start < SPIN_MIN_MS) {
        Atomics.pause()
        continue
      }
      Atomics.store(rows, 2, wake)
      Atomics.add(rows, 1, 1)
      const seen = Atomics.load(rows, 0)
      if (seen < need) Atomics.wait(rows, 0, seen, 1)
      Atomics.sub(rows, 1, 1)
    }
  }

  deblockRowWave(mby: int, full: bool, dbp: Int32Array) {
    const mbw = this.mbw
    for (let mbx = 0; mbx < mbw; mbx++) {
      if (this.dbNeed(mbx, mby, full)) {
        if (mby > 0) this.waitRow(dbp, mby - 1, mbx + 2 < mbw ? mbx + 2 : mbw)
        this.deblockMb(mbx, mby)
      }
      publish(dbp, mby, mbx + 1)
    }
  }

  closeRow(mby: int, full: bool, ver: int, dbp: Int32Array) {
    if (!this.filterRecon) return
    if (this.deblock) this.deblockRowWave(mby, full, dbp)
    if (mby > 0) {
      if (this.deblock) this.waitRow(dbp, mby - 1, this.mbw)
      this.finalizeRow(mby - 1, full, ver)
    }
    if (mby === this.mbh - 1) this.finalizeRow(mby, full, ver)
  }

  finishRow(mby: int, sliceRows: int, full: bool, ver: int, progress: Int32Array, dbp: Int32Array) {
    this.qpRow(mby, sliceRows, progress)
    const t = performance.now()
    if (mby > 0) {
      if (mby % sliceRows === 0) this.waitRow(progress, mby - 1, this.mbw + 1)
      this.closeRow(mby - 1, full, ver, dbp)
    }
    if (mby === this.mbh - 1) this.closeRow(mby, full, ver, dbp)
    this.rDb += performance.now() - t
  }

  timedMb(coded: bool): bool {
    if (coded) {
      const s = (this.tN & 7) === 0
      this.tN++
      if (s) this.tNs++
      return s
    }
    const s = (this.uN & 7) === 0
    this.uN++
    if (s) this.uNs++
    return s
  }

  scaleTiming() {
    const k = this.tNs > 0 ? f64(this.tN) / f64(this.tNs) : 0.0
    const ku = this.uNs > 0 ? f64(this.uN) / f64(this.uNs) : 0.0
    this.rMo *= k
    this.rMd = this.rMd * k + this.rUn * ku
    this.rTq *= k
  }

  codeMbAt(mbx: int, mby: int, idr: bool) {
    const mb = mby * this.mbw + mbx
    if (this.sliceType === SLICE_B) {
      this.codeMbB(mbx, mby)
      this.packCoef(mb)
      return
    }
    if (!idr && this.changed[mb]! === 0 && this.refresh[mb]! === 0) {
      if (!this.timedMb(false)) {
        this.unchangedMb(mbx, mby)
        return
      }
      const t0 = performance.now()
      this.unchangedMb(mbx, mby)
      this.rUn += performance.now() - t0
      return
    }
    this.mbq = this.mbQp[mb]!
    const timed = this.timedMb(true)
    const a = timed ? performance.now() : 0.0
    let b = a
    let skipped = false
    this.skipProbed = false
    if (!idr) {
      if (this.refresh[mb]! !== 0) {
        this.zeroMotion(mbx, mby)
      } else if (this.earlySkip && this.earlySkipMb(mbx, mby)) {
        skipped = true
      } else {
        this.motionSearch(mbx, mby)
      }
      if (timed) b = performance.now()
    }
    let level = 0
    if (idr) {
      if (mbx === 0 && this.idrReuse) this.beginIdrRow(mby)
      level = this.idrReuseLevel(mb, mbx, mby)
      if (level === 0) {
        this.decideIntra(mbx, mby, BIG)
        this.commitIntra(mb, mbx, mby)
      } else {
        this.restoreIdrDecisions(mb)
      }
    } else if (!skipped) {
      this.decideInter(mbx, mby)
    }
    const c = timed ? performance.now() : 0.0
    if (level === 2) {
      this.restoreIdrCoding(mb, mbx, mby)
    } else {
      this.codeMb(mbx, mby)
    }
    if (idr && this.idrReuse) {
      this.storeIdrMb(mb, mbx, mby, level)
    } else {
      this.packCoef(mb)
    }
    if (timed) {
      const d = performance.now()
      this.rMo += b - a
      this.rMd += c - b
      this.rTq += d - c
    }
  }

  codeRow(mby: int, idr: bool, sliceRows: int) {
    this.sliceRow0 = mby - mby % sliceRows
    for (let mbx = 0; mbx < this.mbw; mbx++) this.codeMbAt(mbx, mby, idr)
    this.sliceRow0 = 0
  }

  // The spin budget restarts whenever the row moves. A step of the row that outlasts the budget doubles it if it
  // still arrived within SPIN_MAX_MS (spinning longer would have caught it without a sleep and a wake-up) and
  // halves it otherwise (a producer that stalls that long was descheduled or is waiting itself): on a busy host
  // the budget falls to SPIN_MIN_MS and the CPU goes to other work.
  waitRow(progress: Int32Array, row: int, need: int) {
    const at = row * ROW_SLOT
    let last = Atomics.load(progress, at)
    if (last >= need) return
    let since = performance.now()
    let overran = false
    let timeout = 1
    while (true) {
      if (this.pipeMain && this.entropyReady()) {
        since = performance.now()
        overran = false
      } else if (performance.now() - since < this.spinMs) {
        Atomics.pause()
      } else {
        overran = true
        Atomics.add(progress, at + 1, 1)
        const seen = Atomics.load(progress, at)
        if (seen < need && Atomics.wait(progress, at, seen, timeout) === "timed-out" && timeout < SLEEP_MAX_MS) timeout *= 2
        Atomics.sub(progress, at + 1, 1)
      }
      const v = Atomics.load(progress, at)
      if (v === last) continue
      const now = performance.now()
      if (overran) {
        this.spinMs = now - since < SPIN_MAX_MS ? Math.min(this.spinMs * 2, SPIN_MAX_MS) :
          Math.max(this.spinMs / 2, SPIN_MIN_MS)
      }
      if (v >= need) return
      last = v
      since = now
      overran = false
    }
  }

  spinBudgets(): Float64Array {
    if (this.spinBudget.length !== this.threads) {
      this.spinBudget = new Float64Array(this.threads)
      this.spinBudget.fill(SPIN_MAX_MS)
    }
    return this.spinBudget
  }

  codeRowWave(mby: int, idr: bool, sliceRows: int, progress: Int32Array) {
    this.sliceRow0 = mby - mby % sliceRows
    const mbw = this.mbw
    for (let mbx = 0; mbx < mbw; mbx++) {
      if (mby > this.sliceRow0) this.waitRow(progress, mby - 1, mbx + 2 < mbw ? mbx + 2 : mbw)
      this.codeMbAt(mbx, mby, idr)
      publish(progress, mby, mbx + 1)
    }
    this.sliceRow0 = 0
  }

  parallelWorth(idr: bool): bool {
    const mbs = this.mbw * this.mbh
    if (this.mbh < 8) return false
    if (idr) return true
    let n = 0
    for (let i = 0; i < mbs; i++) {
      if (this.changed[i]! !== 0 || this.refresh[i]! !== 0) n++
    }
    return n * 4 >= mbs
  }

  shareState(): SharedState {
    let refY: Uint8Array[] = []
    let refU: Uint8Array[] = []
    let refV: Uint8Array[] = []
    let refHb: Uint8Array[] = []
    let refHh: Uint8Array[] = []
    let refHj: Uint8Array[] = []
    let refTiles: Int32Array[] = []
    let refVer: Int32Array[] = []
    for (const rf of this.refs) {
      refVer.push(rf.ver)
      refY.push(rf.y)
      refU.push(rf.u)
      refV.push(rf.v)
      refHb.push(rf.hb)
      refHh.push(rf.hh)
      refHj.push(rf.hj)
      refTiles.push(rf.tileGen)
    }
    return {
      srcY: this.srcY, srcU: this.srcU, srcV: this.srcV, curY: this.cur.y, curU: this.cur.u, curV: this.cur.v,
      refY, refU, refV, refHb, refHh, refHj, refTiles, wref: this.wref, curVer: this.cur.ver, refVer,
      lastChange: this.lastChange, streak: this.streak, mbClass: this.mbClass,
      changed: this.changed, refresh: this.refresh, mbQp: this.mbQp, codedQp: this.codedQp, mbType: this.mbType,
      i16Mode: this.i16Mode, cMode: this.cMode, imode: this.imode, t8: this.t8, cbp: this.cbp, dcCbf: this.dcCbf, nnz: this.nnz,
      mvx: this.mvx, mvy: this.mvy, refIdx: this.refIdx, mvdx: this.mvdx, mvdy: this.mvdy, prevMvx: this.prevMvx,
      prevMvy: this.prevMvy, pkCoef: this.pkCoef, pkMask: this.pkMask, pkOff: this.pkOff, pkTop: this.pkTop, qpY: this.qpY, qpDelta: this.qpDelta, srcSame: this.srcSame, recSame: this.recSame, cType: this.cType,
      cI16: this.cI16, cCMode: this.cCMode, cCbp: this.cCbp, cDcCbf: this.cDcCbf, cQp: this.cQp, cImode: this.cImode,
      cNnz: this.cNnz, cCoef: this.cCoef, cMask: this.cMask, cOff: this.cOff, idrSaved: this.idrSaved, idrSlotMb: this.idrSlotMb, idrPool: this.idrPool, idrTop: this.idrTop,
      idrRecY: this.idrRecY, idrRecU: this.idrRecU, idrRecV: this.idrRecV, mvx1: this.mvx1, mvy1: this.mvy1,
      refIdx1: this.refIdx1, mvdx1: this.mvdx1, mvdy1: this.mvdy1, colMvx: this.colMvx, colMvy: this.colMvy,
      colRef: this.colRef,
    }
  }

  frameParams(): FrameParams {
    let refGen: int[] = []
    for (const rf of this.refs) refGen.push(rf.gen)
    return {
      sliceType: this.sliceType, refsActive: this.refsActive, refGen, wpOn: this.wpOn, wpChromaOn: this.wpChromaOn,
      wpY: this.wpY, wpU: this.wpU, wpV: this.wpV, shiftHx: this.shiftHx, shiftVy: this.shiftVy,
      scrollMvx: this.scrollMvx, scrollMvy: this.scrollMvy, scrollMb0: this.scrollMb0, scrollMb1: this.scrollMb1,
      cursorMvx: this.cursorMvx, cursorMvy: this.cursorMvy, cursorMb: this.cursorMb, idrCache: this.idrCache,
      idrReuse: this.idrReuse, frameNum: this.frameNum, idrPicId: this.idrPicId, frameQp: this.frameQp,
      deblock: this.deblock, filterRecon: this.filterRecon, counter: this.counter, bTb: this.bTb, bTd: this.bTd,
      bTdCol: this.bTdCol, pocBase: this.pocBase, frameIndex: this.frameIndex, params: this.params,
      refreshDelta: this.refreshDelta, lifetimeAq: this.lifetimeAq, lastAnalysed: this.lastAnalysed,
      textMbs: this.textMbs, laLife: this.laLife, laOn: this.laOn, laHz: this.laHz, laStrength: this.laStrength,
      nalRef: this.nalRef, l0Reorder: this.l0Reorder, mmco: this.mmco,
    }
  }

  attach(sh: SharedState, fp: FrameParams) {
    this.srcY = sh.srcY
    this.srcU = sh.srcU
    this.srcV = sh.srcV
    const cur = new RefFrame()
    cur.y = sh.curY
    cur.u = sh.curU
    cur.v = sh.curV
    cur.ver = sh.curVer
    this.cur = cur
    this.refs = []
    for (let k = 0; k < sh.refY.length; k++) {
      const rf = new RefFrame()
      rf.y = sh.refY[k]!
      rf.u = sh.refU[k]!
      rf.v = sh.refV[k]!
      rf.hb = sh.refHb[k]!
      rf.hh = sh.refHh[k]!
      rf.hj = sh.refHj[k]!
      rf.tileGen = sh.refTiles[k]!
      rf.ver = sh.refVer[k]!
      rf.gen = fp.refGen[k]!
      this.refs.push(rf)
    }
    this.wref = sh.wref
    this.lastChange = sh.lastChange
    this.streak = sh.streak
    this.changed = sh.changed
    this.refresh = sh.refresh
    this.mbClass = sh.mbClass
    this.mbQp = sh.mbQp
    this.codedQp = sh.codedQp
    this.mbType = sh.mbType
    this.i16Mode = sh.i16Mode
    this.cMode = sh.cMode
    this.imode = sh.imode
    this.t8 = sh.t8
    this.cbp = sh.cbp
    this.dcCbf = sh.dcCbf
    this.nnz = sh.nnz
    this.mvx = sh.mvx
    this.mvy = sh.mvy
    this.refIdx = sh.refIdx
    this.mvdx = sh.mvdx
    this.mvdy = sh.mvdy
    this.prevMvx = sh.prevMvx
    this.prevMvy = sh.prevMvy
    this.pkCoef = sh.pkCoef
    this.pkOff = sh.pkOff
    this.pkMask = sh.pkMask
    this.pkTop = sh.pkTop
    this.qpY = sh.qpY
    this.qpDelta = sh.qpDelta
    this.srcSame = sh.srcSame
    this.recSame = sh.recSame
    this.cType = sh.cType
    this.cI16 = sh.cI16
    this.cCMode = sh.cCMode
    this.cCbp = sh.cCbp
    this.cDcCbf = sh.cDcCbf
    this.cQp = sh.cQp
    this.cImode = sh.cImode
    this.cNnz = sh.cNnz
    this.cCoef = sh.cCoef
    this.cMask = sh.cMask
    this.cOff = sh.cOff
    this.idrSaved = sh.idrSaved
    this.idrSlotMb = sh.idrSlotMb
    this.idrPool = sh.idrPool
    this.idrTop = sh.idrTop
    this.idrRecY = sh.idrRecY
    this.idrRecU = sh.idrRecU
    this.idrRecV = sh.idrRecV
    this.mvx1 = sh.mvx1
    this.mvy1 = sh.mvy1
    this.refIdx1 = sh.refIdx1
    this.mvdx1 = sh.mvdx1
    this.mvdy1 = sh.mvdy1
    this.colMvx = sh.colMvx
    this.colMvy = sh.colMvy
    this.colRef = sh.colRef
    this.sliceType = fp.sliceType
    this.refsActive = fp.refsActive
    this.wpOn = fp.wpOn
    this.wpChromaOn = fp.wpChromaOn
    this.wpY = fp.wpY
    this.wpU = fp.wpU
    this.wpV = fp.wpV
    this.shiftHx = fp.shiftHx
    this.shiftVy = fp.shiftVy
    this.scrollMvx = fp.scrollMvx
    this.scrollMvy = fp.scrollMvy
    this.scrollMb0 = fp.scrollMb0
    this.scrollMb1 = fp.scrollMb1
    this.cursorMvx = fp.cursorMvx
    this.cursorMvy = fp.cursorMvy
    this.cursorMb = fp.cursorMb
    this.idrCache = fp.idrCache
    this.idrReuse = fp.idrReuse
    this.frameNum = fp.frameNum
    this.idrPicId = fp.idrPicId
    this.frameQp = fp.frameQp
    this.deblock = fp.deblock
    this.filterRecon = fp.filterRecon
    this.counter = fp.counter
    this.bTb = fp.bTb
    this.bTd = fp.bTd
    this.bTdCol = fp.bTdCol
    this.pocBase = fp.pocBase
    this.frameIndex = fp.frameIndex
    this.params = fp.params
    this.refreshDelta = fp.refreshDelta
    this.lifetimeAq = fp.lifetimeAq
    this.lastAnalysed = fp.lastAnalysed
    this.textMbs = fp.textMbs
    this.laLife = fp.laLife
    this.laOn = fp.laOn
    this.laHz = fp.laHz
    this.laStrength = fp.laStrength
    this.nalRef = fp.nalRef
    this.l0Reorder = fp.l0Reorder
    this.mmco = fp.mmco
    this.sharedTiles = true
  }

  halfTileRow(y: Uint8Array, hb: Uint8Array, hh: Uint8Array, hj: Uint8Array, tileGen: Int32Array, gen: int, row: int) {
    const tx = this.tilesX
    for (let c = 0; c < tx; c++) {
      computeHalfTile(hb, hh, hj, y, this.ys, this.ytile + row * 16 * this.ys + c * 16)
      tileGen[row * tx + c] = gen
    }
  }

  storeCol(cmx: Int32Array, cmy: Int32Array, cref: Int32Array) {
    const mbs = this.mbw * this.mbh
    for (let mb = 0; mb < mbs; mb++) {
      const l0 = this.refIdx[mb]! >= 0
      const intra = this.isIntra(mb)
      cref[mb] = intra ? -1 : l0 ? this.refIdx[mb]! : this.refIdx1[mb]!
      cmx[mb] = intra ? 0 : l0 ? this.mvx[mb]! : this.mvx1[mb]!
      cmy[mb] = intra ? 0 : l0 ? this.mvy[mb]! : this.mvy1[mb]!
    }
  }

  tuning(): int[] {
    return [this.inter8x8 ? 1 : 0, this.decimate ? 1 : 0, this.subpel, this.subpelDirs, this.meIters,
      this.earlyExit ? 1 : 0, this.globalShift ? 1 : 0, this.useI4 ? 1 : 0, this.aqStrength, this.aqTexture,
      this.earlySkip ? 1 : 0, this.hpelSad ? 1 : 0, this.qpelSad ? 1 : 0, this.videoHpelDirs, this.videoQpelDirs,
      this.i8PredOnly ? 1 : 0, this.noise, this.bDirectNb]
  }

  tune(t: int[]) {
    this.inter8x8 = t[0]! === 1
    this.decimate = t[1]! === 1
    this.subpel = t[2]!
    this.subpelDirs = t[3]!
    this.meIters = t[4]!
    this.earlyExit = t[5]! === 1
    this.globalShift = t[6]! === 1
    this.useI4 = t[7]! === 1
    this.aqStrength = t[8]!
    this.aqTexture = t[9]!
    this.earlySkip = t[10]! === 1
    this.hpelSad = t[11]! === 1
    this.qpelSad = t[12]! === 1
    this.videoHpelDirs = t[13]!
    this.videoQpelDirs = t[14]!
    this.i8PredOnly = t[15]! === 1
    this.noise = t[16]!
    this.bDirectNb = t[17]!
  }

  sliceLen(s: int, sliceRows: int): int {
    const r0 = s * sliceRows
    return r0 + sliceRows < this.mbh ? sliceRows : this.mbh - r0
  }

  sliceWork(frame: Uint8Array, frameOff: int, sliceRows: int, next: Int32Array, done: Int32Array, ready: Int32Array,
    sync: Int32Array, coded: Int32Array, filtered: Int32Array, stats: Float64Array, slot: int): u8[][] {
    const mbh = this.mbh
    const nSlices = next.length
    let parts: u8[][] = []
    for (let s = 0; s < nSlices; s++) parts.push([])
    let tEnt = 0.0
    while (true) {
      let s = slot % nSlices
      if (Atomics.load(next, s) >= this.sliceLen(s, sliceRows)) {
        s = -1
        let most = 0
        for (let i = 0; i < nSlices; i++) {
          const left = this.sliceLen(i, sliceRows) - Atomics.load(next, i)
          if (left > most) {
            most = left
            s = i
          }
        }
        if (s < 0) break
      }
      const len = this.sliceLen(s, sliceRows)
      const k = Atomics.add(next, s, 1)
      if (k >= len) continue
      const r0 = s * sliceRows
      this.prepRowTimed(frame, frameOff, r0 + k, true, true)
      this.codeRowWave(r0 + k, true, sliceRows, coded)
      if (Atomics.add(done, s, 1) + 1 < len) continue
      const e0 = performance.now()
      this.finishQpRows(sliceRows, r0, r0 + len)
      publish(ready, s, 1)
      let out: u8[] = []
      this.entropyRows(true, sliceRows, r0, r0 + len, &out)
      parts[s] = out
      tEnt += performance.now() - e0
    }
    const d0 = performance.now()
    if (this.filterRecon) {
      const ver = this.counter + 1
      while (true) {
        const r = Atomics.add(sync, 0, 1)
        if (r >= mbh) break
        const s = Math.trunc(r / sliceRows)
        if (s > 0 && r === s * sliceRows) this.waitRow(ready, s - 1, 1)
        this.waitRow(ready, s, 1)
        this.closeRow(r, true, ver, filtered)
      }
    }
    this.scaleTiming()
    const k = slot * 6
    stats[k] = this.rMo
    stats[k + 1] = this.rMd
    stats[k + 2] = this.rTq
    stats[k + 3] = tEnt
    stats[k + 4] = performance.now() - d0
    stats[k + 5] = this.rPrep
    return parts
  }

  codeSlicesParallel(frame: Uint8Array, frameOff: int, sliceRows: int, nSlices: int): f64[] {
    const sh = this.shareState()
    const fp = this.frameParams()
    const opts = this.workerOpts()
    const tuning = this.tuning()
    const helpers = this.threads - 1
    const next = new Int32Array(nSlices)
    const done = new Int32Array(nSlices)
    const ready = progressArray(nSlices)
    const sync = new Int32Array(2)
    const coded = progressArray(this.mbh)
    const filtered = progressArray(this.mbh)
    const stats = new Float64Array((helpers + 1) * 6)
    let threads: Thread<u8[][]>[] = []
    for (let t = 1; t <= helpers; t++) {
      const slot = t
      threads.push(Thread.spawn(() => {
        const w = new Encoder(opts)
        w.attach(sh, fp)
        w.tune(tuning)
        const parts = w.sliceWork(frame, frameOff, sliceRows, next, done, ready, sync, coded, filtered, stats, slot)
        Atomics.add(sync, 1, w.reusedMbs)
        return parts
      }))
    }
    let results: u8[][][] = [this.sliceWork(frame, frameOff, sliceRows, next, done, ready, sync, coded, filtered, stats, 0)]
    for (const th of threads) results.push(th.join())
    for (let s = 0; s < nSlices; s++) {
      for (const parts of results) {
        for (const b of parts[s]!) this.out.push(b)
      }
    }
    this.reusedMbs += Atomics.load(sync, 1)
    let sums: f64[] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    for (let t = 0; t <= helpers; t++) {
      for (let j = 0; j < 6; j++) sums[j] = sums[j]! + stats[t * 6 + j]!
    }
    return sums
  }

  codeFrameParallel(frame: Uint8Array, frameOff: int, idr: bool, full: bool, sliceRows: int, pipe: bool,
    peek: bool): f64 {
    const sh = this.shareState()
    const fp = this.frameParams()
    const opts = this.workerOpts()
    const tuning = this.tuning()
    const mbh = this.mbh
    const ver = this.counter + 1
    const progress = progressArray(mbh)
    const dbp = progressArray(mbh)
    const next = new Int32Array(1)
    const stats = new Float64Array(5 * this.threads + 1)
    const dedicated = pipe && this.threads >= ENTROPY_THREAD_MIN && this.threads < availableParallelism()
    const qpRows = progressArray(dedicated ? 1 : 0)
    const classify = !idr && this.textGated()
    const clsNext = new Int32Array(1)
    const clsDone = progressArray(1)
    const textRows = new Int32Array(classify ? mbh : 0)
    const pk: Peek = {
      frame: this.nextFrame, frameOff: this.nextOff, ready: this.nextRead, sync: this.pkSync, changed: this.pkChanged,
      plan: this.pkPlan, prev: this.pkPrev, cur: this.pkCur, shifts: this.pkShifts,
    }
    if (peek) pk.sync.fill(0)
    const spin = this.spinBudgets()
    let workers: Thread<int>[] = []
    for (let k = 1; k < this.threads; k++) {
      const slot = k * 5
      workers.push(Thread.spawn(() => {
        const w = new Encoder(opts)
        w.attach(sh, fp)
        w.tune(tuning)
        w.spinMs = spin[k]!
        w.qpRows = qpRows
        if (classify) w.classifyFrame(frame, frameOff, full, clsNext, clsDone, textRows)
        while (true) {
          const r = Atomics.add(next, 0, 1)
          if (r >= mbh) break
          w.prepRowTimed(frame, frameOff, r, full, idr)
          if (peek) bump(pk.sync, PEEK_PREPPED)
          w.codeRowWave(r, idr, sliceRows, progress)
          w.finishRow(r, sliceRows, idr, ver, progress, dbp)
        }
        w.scaleTiming()
        stats[slot] = w.rMo
        stats[slot + 1] = w.rMd
        stats[slot + 2] = w.rTq
        stats[slot + 3] = w.rDb
        stats[slot + 4] = w.rPrep
        if (peek) w.peekWork(pk)
        spin[k] = w.spinMs
        return w.reusedMbs
      }))
    }
    let entropy: Thread<u8[]> | undefined = undefined
    if (dedicated) {
      const slot = 5 * this.threads
      entropy = Thread.spawn(() => {
        const w = new Encoder(opts)
        w.attach(sh, fp)
        let out: u8[] = []
        for (let r = 0; r < mbh; r++) {
          w.awaitRows(qpRows, r + 1)
          const t = performance.now()
          w.entropyRow(idr, sliceRows, r, &out)
          w.rEnt += performance.now() - t
        }
        stats[slot] = w.rEnt
        return out
      })
    }
    if (classify) this.classifyFrame(frame, frameOff, full, clsNext, clsDone, textRows)
    const inline = pipe && !dedicated
    this.qpRows = qpRows
    this.pipeMain = inline
    this.pipeProgress = progress
    this.pipeEnt = 0
    this.pipeIdr = idr
    this.pipeSliceRows = sliceRows
    this.sharedTiles = true
    while (true) {
      if (inline) this.entropyReady()
      const r = Atomics.add(next, 0, 1)
      if (r >= mbh) break
      this.prepRowTimed(frame, frameOff, r, full, idr)
      if (peek) bump(pk.sync, PEEK_PREPPED)
      this.codeRowWave(r, idr, sliceRows, progress)
      this.finishRow(r, sliceRows, idr, ver, progress, dbp)
    }
    if (inline) {
      while (this.pipeEnt < mbh) {
        this.waitRow(progress, this.pipeEnt, this.mbw + 1)
        this.entropyReady()
      }
    }
    this.scaleTiming()
    this.pipeMain = false
    this.pipeProgress = new Int32Array(0)
    let tPeek = 0.0
    if (peek) {
      const t = performance.now()
      this.peekWork(pk)
      tPeek = performance.now() - t
    }
    this.sharedTiles = false
    this.classReady = false
    this.qpRows = new Int32Array(0)
    for (const t of workers) this.reusedMbs += t.join()
    if (entropy !== undefined) {
      for (const b of entropy.join()) this.out.push(b)
      this.rEnt += stats[5 * this.threads]!
    }
    this.peeked = peek && Atomics.load(pk.sync, PEEK_PHASE * ROW_SLOT) === 2
    this.peekedFrame = pk.frame
    this.peekedOff = pk.frameOff
    this.peekedIndex = this.frameIndex + 1
    for (let k = 1; k < this.threads; k++) {
      this.rMo += stats[k * 5]!
      this.rMd += stats[k * 5 + 1]!
      this.rTq += stats[k * 5 + 2]!
      this.rDb += stats[k * 5 + 3]!
      this.rPrep += stats[k * 5 + 4]!
    }
    return tPeek
  }

  configureB(n: int) {
    // A B run needs a P anchor after it inside its GOP, so GOPs of one or two frames never code one.
    this.bframes = this.keyint <= 2 || n < 0 ? 0 : n > 3 ? 3 : n
    this.pyramid = this.pyramid && this.bframes >= 2
    if (this.bframes === 0) return
    this.bDirectNb = this.pyramid ? B_DIRECT_NB_PYRAMID : B_DIRECT_NB
    this.dpbRefs = this.maxRefs < 2 ? 2 : this.maxRefs
    const dpb = this.dpbRefs + (this.pyramid ? 1 : 0)
    const p = this.params
    this.params = {
      width: p.width, height: p.height, mbw: p.mbw, mbh: p.mbh, profile: p.profile,
      level: chooseLevel(this.mbw, this.mbh, this.fpsF, dpb, 0), refs: p.refs, fpsNum: p.fpsNum,
      fpsDen: p.fpsDen, log2MaxFrameNum: p.log2MaxFrameNum, cabac: p.cabac, transform8x8: p.transform8x8,
      weightedPred: p.weightedPred, dpbRefs: dpb, bframes: this.bframes, pyramid: this.pyramid,
    }
    const mbs = this.mbw * this.mbh
    if (this.pyramid) {
      this.bRefFrame = this.newFrame()
      this.giveHalfPlanes(this.bRefFrame)
      this.bColMvx = new Int32Array(mbs)
      this.bColMvy = new Int32Array(mbs)
      this.bColRef = new Int32Array(mbs)
      this.bMbQpRef = new Int32Array(mbs)
    }
    this.mvx1 = new Int32Array(mbs)
    this.mvy1 = new Int32Array(mbs)
    this.refIdx1 = new Int32Array(mbs)
    this.mvdx1 = new Int32Array(mbs)
    this.mvdy1 = new Int32Array(mbs)
    this.colMvx = new Int32Array(mbs)
    this.colMvy = new Int32Array(mbs)
    this.colRef = new Int32Array(mbs)
    this.bSlots = []
    this.bTargets = []
    this.bMbQp = new Int32Array(mbs)
    this.bChanged = new Uint8Array(mbs)
    this.bChanged.fill(1)
    this.bRefresh = new Uint8Array(mbs)
    this.dirtyScratch = new Uint8Array(mbs)
  }

  bSlot(k: int): BSlot {
    while (this.bSlots.length <= k) this.bSlots.push(this.newBSlot())
    return this.bSlots[k]!
  }

  bTarget(j: int): RefFrame {
    while (this.bTargets.length <= j) {
      const f = new RefFrame()
      f.y = newPlane(this.ysize)
      f.u = newPlane(this.cs * (this.chh + 2 * PADC))
      f.v = newPlane(this.cs * (this.chh + 2 * PADC))
      this.bTargets.push(f)
    }
    return this.bTargets[j]!
  }

  newBSlot(): BSlot {
    const mbs = this.mbw * this.mbh
    const imode = new Uint8Array(mbs * 16)
    imode.fill(2)
    const qpY = new Int32Array(mbs)
    qpY.fill(this.opts.qp)
    return {
      mbType: new Int32Array(mbs), i16Mode: new Int32Array(mbs), cMode: new Int32Array(mbs), imode,
      t8: new Int32Array(mbs), cbp: new Int32Array(mbs), dcCbf: new Int32Array(mbs), nnz: new Uint8Array(mbs * 24),
      mvx: new Int32Array(mbs), mvy: new Int32Array(mbs), refIdx: new Int32Array(mbs), mvdx: new Int32Array(mbs),
      mvdy: new Int32Array(mbs), pkCoef: new Int16Array(this.packSize()), pkMask: new Uint16Array(mbs),
      pkOff: new Int32Array(mbs), pkTop: new Int32Array(1), qpY, qpDelta: new Int32Array(mbs),
      mvx1: new Int32Array(mbs), mvy1: new Int32Array(mbs), refIdx1: new Int32Array(mbs),
      mvdx1: new Int32Array(mbs), mvdy1: new Int32Array(mbs),
    }
  }

  bSlotState(s: BSlot, data: Uint8Array, target: RefFrame): SharedState {
    const sh = this.shareState()
    const ySize = this.aw * this.ah + 64
    const cSize = this.cw * this.chh + 64
    sh.srcY = data.subarray(0, ySize)
    sh.srcU = data.subarray(ySize, ySize + cSize)
    sh.srcV = data.subarray(ySize + cSize, ySize + 2 * cSize)
    sh.curY = target.y
    sh.curU = target.u
    sh.curV = target.v
    sh.changed = this.bChanged
    sh.refresh = this.bRefresh
    sh.mbQp = this.bMbQp
    sh.mbType = s.mbType
    sh.i16Mode = s.i16Mode
    sh.cMode = s.cMode
    sh.imode = s.imode
    sh.t8 = s.t8
    sh.cbp = s.cbp
    sh.dcCbf = s.dcCbf
    sh.nnz = s.nnz
    sh.mvx = s.mvx
    sh.mvy = s.mvy
    sh.refIdx = s.refIdx
    sh.mvdx = s.mvdx
    sh.mvdy = s.mvdy
    s.pkTop[0] = 0
    sh.pkCoef = s.pkCoef
    sh.pkMask = s.pkMask
    sh.pkOff = s.pkOff
    sh.pkTop = s.pkTop
    sh.qpY = s.qpY
    sh.qpDelta = s.qpDelta
    sh.mvx1 = s.mvx1
    sh.mvy1 = s.mvy1
    sh.refIdx1 = s.refIdx1
    sh.mvdx1 = s.mvdx1
    sh.mvdy1 = s.mvdy1
    return sh
  }

  useB(side: SideInfo | undefined): bool {
    if (!this.bReady) return false
    if (side === undefined) return true
    if (side.scroll.length > 0) return false
    this.dirtyScratch.fill(0)
    return markRects(this.dirtyScratch, this.mbw, this.mbh, side.dirty) * 2 >= this.mbw * this.mbh
  }

  submit(frame: Uint8Array, frameOff: int, side: SideInfo | undefined): u8[] {
    if (this.intraParallel()) return this.submitIntra(frame, frameOff, side)
    const idx = this.inputIndex
    this.inputIndex++
    const life = this.laNext ? this.laLife : new Int32Array(0)
    this.laNext = false
    let out: u8[] = []
    const idrNext = this.refs.length === 0 || idx - this.pocBase >= this.keyint
    if (idrNext) {
      this.flushPending(&out)
      this.setLookahead(life)
      this.codeAnchor(frame, frameOff, idx, this.bAdapt ? side : undefined, &out)
      return out
    }
    if (this.bAdapt && !this.useB(side)) {
      this.flushPending(&out)
      this.setLookahead(life)
      this.codeAnchor(frame, frameOff, idx, side, &out)
      return out
    }
    if (this.pending.length < this.bframes) {
      const p = new Pending(this.padInput(frame, frameOff), idx)
      if (life.length > 0) {
        p.life = life.slice()
        if (this.laL1 > 0.0) p.keep = this.laKeep.slice()
      }
      this.pending.push(p)
      if (this.pending.length === this.bframes) this.peekAnchor(idx)
      return out
    }
    this.anchorLookahead(life, this.laKeep, idx)
    this.codeAnchor(frame, frameOff, idx, undefined, &out)
    this.codeBFrames(&out)
    return out
  }

  flush(): u8[] {
    let out: u8[] = []
    if (this.intraPool.length > 0) {
      this.drainIntra(&out)
      return out
    }
    this.flushPending(&out)
    this.finishBRun(&out)
    return out
  }

  intraParallel(): bool {
    return this.frameThreads && this.threads > 1 && this.keyint === 1 && !this.idrReuse && this.opts.rcMode !== RC_VBR
  }

  bindSource(buf: Uint8Array) {
    const ySize = this.aw * this.ah + 64
    const cSize = this.cw * this.chh + 64
    this.srcY = buf.subarray(0, ySize)
    this.srcU = buf.subarray(ySize, ySize + cSize)
    this.srcV = buf.subarray(ySize + cSize, ySize + 2 * cSize)
  }

  startIntraPool() {
    const n = this.threads
    if (this.srcRing.length !== n + 2) {
      const size = this.aw * this.ah + 2 * (this.cw * this.chh) + 192
      this.srcRing = []
      for (let k = 0; k < n + 2; k++) this.srcRing.push(newPlane(size))
      this.ringBusy = new Uint8Array(n + 2)
    }
    const work = new Channel<IntraJob>()
    const done = new Channel<IntraCoded>()
    this.intraWork = work
    this.intraDone = done
    const opts = this.opts
    const params = this.params
    const tuning = this.tuning()
    const cfg = [this.deblock ? 1 : 0, this.idrSlices, this.sliceRowsI]
    for (let t = 0; t < n; t++) {
      this.intraPool.push(Thread.spawn(() => {
        const w = new Encoder(opts)
        w.params = params
        w.tune(tuning)
        w.deblock = cfg[0]! === 1
        w.idrSlices = cfg[1]!
        w.sliceRowsI = cfg[2]!
        w.idrReuse = false
        while (true) {
          const job = work.receive()
          if (job === undefined) return
          done.send(w.codeIntraJob(job))
        }
      }))
    }
  }

  submitIntra(frame: Uint8Array, frameOff: int, side: SideInfo | undefined): u8[] {
    const t0 = performance.now()
    if (this.intraPool.length === 0) this.startIntraPool()
    let out: u8[] = []
    const seq = this.intraSent
    const slot = seq % this.srcRing.length
    while (this.ringBusy[slot]! !== 0) this.takeIntra(this.intraDone!.receive()!, &out)
    if (this.bframes > 0) {
      this.pocBase = this.inputIndex
      this.frameIndex = this.inputIndex
      this.inputIndex++
    }
    this.filterRecon = this.wantRecon
    this.srcSlot = slot
    this.analyse(frame, frameOff, side, true)
    this.srcSlot = -1
    this.wpOn = false
    this.wpChromaOn = false
    this.lastActive = this.frameIndex
    const mbs = this.mbw * this.mbh
    const dt = this.lastCodedIndex < 0 ? 1 : this.frameIndex - this.lastCodedIndex
    this.frameQp = this.rate.frameQp(true, 0.0, mbs, dt)
    this.laOn = this.laNext
    this.laHz = (this.keyint < this.laSpan ? this.keyint : this.laSpan) * LIFE_ONE
    this.laNext = false
    const longStatic = Math.round(this.fpsF)
    for (let mb = 0; mb < mbs; mb++) this.mbQp[mb] = this.baseMbQp(mb, true, longStatic)
    this.updateHistory()
    this.ringBusy[slot] = 1
    this.intraWork!.send({
      seq, slot, buf: this.srcRing[slot]!, qp: this.mbQp.slice(), frameQp: this.frameQp, idrPicId: this.idrPicId,
      frameIndex: this.frameIndex, pocBase: this.pocBase, recon: this.wantRecon, analysis: performance.now() - t0,
    })
    this.intraSent++
    this.counter++
    this.idrPicId = (this.idrPicId + 1) & 1
    this.frameNum = 1 & ((1 << this.params.log2MaxFrameNum) - 1)
    this.prevChanged.fill(0)
    this.sliceType = SLICE_I
    this.anchorQp = this.frameQp
    this.sinceIdr = 1
    this.lastCodedIndex = this.frameIndex
    this.frameIndex++
    this.codedFrames++
    while (true) {
      const r = this.intraDone!.tryReceive()
      if (r === undefined) break
      this.takeIntra(r, &out)
    }
    return out
  }

  codeIntraJob(job: IntraJob): IntraCoded {
    const t0 = performance.now()
    this.bindSource(job.buf)
    this.mbQp.set(job.qp)
    this.frameQp = job.frameQp
    this.idrPicId = job.idrPicId
    this.frameIndex = job.frameIndex
    this.pocBase = job.pocBase
    this.filterRecon = job.recon
    this.textMbs = 0
    for (let mby = 0; mby < this.mbh; mby++) this.aqRow(mby)
    let out: u8[] = []
    appendNal(&out, 3, 7, writeSps(this.params), true)
    appendNal(&out, 3, 8, writePps(this.params), true)
    this.frameNum = 0
    this.sliceType = SLICE_I
    this.refsActive = 1
    this.anchorQp = this.frameQp
    const rowsI = this.sliceRowsI > 0 ? this.sliceRowsI : this.idrSlices > 1 ? Math.trunc((this.mbh + this.idrSlices - 1) / this.idrSlices) : this.mbh
    this.rMo = 0.0
    this.rMd = 0.0
    this.rTq = 0.0
    this.rUn = 0.0
    this.tN = 0
    this.tNs = 0
    this.uN = 0
    this.uNs = 0
    this.resetPacking()
    for (let mby = 0; mby < this.mbh; mby++) this.codeRow(mby, true, rowsI)
    this.scaleTiming()
    const e0 = performance.now()
    this.finishQpRows(rowsI, 0, this.mbh)
    this.entropyRows(true, rowsI, 0, this.mbh, &out)
    const e1 = performance.now()
    let recon = new Uint8Array(0)
    if (job.recon) {
      this.counter++
      this.deblockFrame(true)
      recon = this.reconOf(this.cur)
    }
    const t1 = performance.now()
    return {
      seq: job.seq, slot: job.slot, frameIndex: job.frameIndex, frameQp: job.frameQp, bits: out, recon,
      analysis: job.analysis, mode: this.rMd, tq: this.rTq, entropy: e1 - e0, deblock: t1 - e1, total: t1 - t0,
    }
  }

  takeIntra(r: IntraCoded, inout out: u8[]) {
    this.ringBusy[r.slot] = 0
    this.intraHeld.push(r)
    let moved = true
    while (moved) {
      moved = false
      let keep: IntraCoded[] = []
      for (const h of this.intraHeld) {
        if (!moved && h.seq === this.intraEmitted) {
          this.emitIntra(h, &out)
          moved = true
        } else {
          keep.push(h)
        }
      }
      this.intraHeld = keep
    }
  }

  emitIntra(h: IntraCoded, inout out: u8[]) {
    for (const b of h.bits) out.push(b)
    const mbs = this.mbw * this.mbh
    this.tAnalysis += h.analysis
    this.tMode += h.mode
    this.tTq += h.tq
    this.tEntropy += h.entropy
    this.tDeblock += h.deblock
    this.frames.push({
      index: h.frameIndex, type: "I", bytes: h.bits.length, qp: h.frameQp, changedMbs: mbs, analysis: h.analysis,
      motion: 0.0, mode: h.mode, tq: h.tq, entropy: h.entropy, deblock: h.deblock, total: h.analysis + h.total,
      reused: 0,
    })
    this.rate.update(true, h.frameQp, h.bits.length * 8, mbs)
    if (h.recon.length > 0) this.queueRecon(h.recon, h.frameIndex)
    this.intraEmitted++
  }

  drainIntra(inout out: u8[]) {
    while (this.intraEmitted < this.intraSent) this.takeIntra(this.intraDone!.receive()!, &out)
    this.intraWork!.close()
    for (const t of this.intraPool) t.join()
    this.intraPool = []
  }

  padInput(frame: Uint8Array, frameOff: int): Uint8Array {
    const ySize = this.aw * this.ah + 64
    const cSize = this.cw * this.chh + 64
    const buf = this.inputPool.length > 0 ? this.inputPool.pop()! : newPlane(ySize + 2 * cSize)
    const cwIn = this.w >> 1
    const chIn = this.h >> 1
    const uIn = frameOff + this.w * this.h
    copyPadded(buf, this.aw, this.ah, frame, frameOff, this.w, this.w, this.h)
    copyPadded(buf.subarray(ySize), this.cw, this.chh, frame, uIn, cwIn, cwIn, chIn)
    copyPadded(buf.subarray(ySize + cSize), this.cw, this.chh, frame, uIn + cwIn * chIn, cwIn, cwIn, chIn)
    buf.fill(0, ySize - 64, ySize)
    buf.fill(0, ySize + cSize - 64, ySize + cSize)
    buf.fill(0, ySize + 2 * cSize - 64, ySize + 2 * cSize)
    return buf
  }

  unpadInput(buf: Uint8Array) {
    const ySize = this.aw * this.ah + 64
    const cSize = this.cw * this.chh + 64
    const cwIn = this.w >> 1
    const chIn = this.h >> 1
    for (let y = 0; y < this.h; y++) buf.copyWithin(y * this.w, y * this.aw, y * this.aw + this.w)
    const uIn = this.w * this.h
    for (let y = 0; y < chIn; y++) {
      buf.copyWithin(uIn + y * cwIn, ySize + y * this.cw, ySize + y * this.cw + cwIn)
    }
    const vIn = uIn + cwIn * chIn
    for (let y = 0; y < chIn; y++) {
      buf.copyWithin(vIn + y * cwIn, ySize + cSize + y * this.cw, ySize + cSize + y * this.cw + cwIn)
    }
  }

  flushPending(inout out: u8[]) {
    if (this.pending.length === 0) return
    const last = this.pending.pop()!
    this.anchorLookahead(last.life, last.keep, last.index)
    this.unpadInput(last.data)
    this.codeAnchor(last.data, 0, last.index, undefined, &out)
    this.inputPool.push(last.data)
    this.codeBFrames(&out)
  }

  codeAnchor(frame: Uint8Array, frameOff: int, idx: int, side: SideInfo | undefined, inout out: u8[]) {
    const idr = this.refs.length === 0 || idx - this.pocBase >= this.keyint
    if (idr) this.pocBase = idx
    this.frameIndex = idx
    this.sinceIdr = idr ? this.keyint : idx - this.pocBase
    const vfr = this.vfr
    this.vfr = vfr && this.bAdapt && this.pending.length === 0
    const at = this.frames.length
    const bits = this.encodeFrame(frame, frameOff, side)
    this.vfr = vfr
    if (this.bThreads.length > 0) {
      this.finishBRun(&out)
      const last = this.frames.length - 1
      const e = this.frames[at]!
      for (let i = at; i < last; i++) this.frames[i] = this.frames[i + 1]!
      this.frames[last] = e
    }
    for (const b of bits) out.push(b)
    const mbs = this.mbw * this.mbh
    this.bReady = this.mostlyMoving()
    if (bits.length === 0) {
      if (this.wantRecon) this.nextRecon = idx + 1
      return
    }
    this.prevAnchorIndex = this.lastAnchorIndex
    this.lastAnchorIndex = idx
    if (this.pending.length > 0) {
      for (let mb = 0; mb < mbs; mb++) {
        const intra = idr || this.isIntra(mb)
        this.colRef[mb] = intra ? -1 : this.refIdx[mb]!
        this.colMvx[mb] = intra ? 0 : this.mvx[mb]!
        this.colMvy[mb] = intra ? 0 : this.mvy[mb]!
      }
    }
    this.holdRecon(this.refs[0]!, idx)
  }

  mostlyMoving(): bool {
    const mbs = this.mbw * this.mbh
    let moved = 0
    for (let mb = 0; mb < mbs; mb++) {
      if (this.changed[mb]! === 1) moved++
    }
    return moved * 2 >= mbs && !this.wpOn && this.scrollMvy === 0 && this.shiftHx === 0 && this.shiftVy === 0
  }

  nextInputAnalysed(): bool {
    if (this.bframes === 0) return true
    // Under B-frames the next input is analysed next only if submit() coded this anchor directly and the anchor
    // leaves bReady false; otherwise it is queued as a B candidate. changed is final once analysis ends.
    return this.bAdapt && this.frameIndex + 1 === this.inputIndex && !this.mostlyMoving()
  }

  codeBFrames(inout out: u8[]) {
    if (this.pending.length > 0) this.startBRun()
    this.pending = []
    if (!this.bAsync()) this.finishBRun(&out)
  }

  bAsync(): bool {
    return this.threads > 1 && this.mbh >= 8 && this.opts.rcMode !== RC_VBR
  }

  holdRecon(rf: RefFrame, idx: int) {
    if (!this.wantRecon) return
    this.queueRecon(this.reconOf(rf), idx)
  }

  queueRecon(data: Uint8Array, idx: int) {
    this.reconHeld.push(new Pending(data, idx))
    let moved = true
    while (moved) {
      moved = false
      let keep: Pending[] = []
      for (const h of this.reconHeld) {
        if (!moved && h.index === this.nextRecon) {
          this.reconReady.push(h.data)
          this.nextRecon++
          moved = true
        } else {
          keep.push(h)
        }
      }
      this.reconHeld = keep
    }
  }

  takeRecon(): Uint8Array[] {
    const r = this.reconReady
    this.reconReady = []
    return r
  }

  startBRun() {
    const t0 = performance.now()
    const n = this.pending.length
    const mbs = this.mbw * this.mbh
    const mid = this.pyramid && n >= 2 ? (n - 1) >> 1 : -1
    for (let i = 0; i < mbs; i++) {
      const q = this.mbQp[i]! + this.bQpOffset
      this.bMbQp[i] = q < 0 ? 0 : q > 51 ? 51 : q
    }
    if (mid >= 0) {
      for (let i = 0; i < mbs; i++) {
        const q = this.mbQp[i]! + this.bRefQpOffset
        this.bMbQpRef[i] = q < 0 ? 0 : q > 51 ? 51 : q
      }
    }
    const aFrameIndex = this.frameIndex
    const aFrameNum = this.frameNum
    this.sliceType = SLICE_B
    const fq = this.anchorQp + this.bQpOffset
    const bq = fq < 0 ? 0 : fq > 51 ? 51 : fq
    const rq0 = this.anchorQp + this.bRefQpOffset
    const rq = rq0 < 0 ? 0 : rq0 > 51 ? 51 : rq0
    this.refsActive = 1
    this.wpOn = false
    this.wpChromaOn = false
    this.shiftHx = 0
    this.shiftVy = 0
    this.scrollMb0 = -1
    this.scrollMb1 = -1
    this.cursorMb = -1
    this.l0Reorder = []
    this.mmco = []
    const parallel = this.threads > 1 && this.mbh >= 8
    if (parallel) {
      for (let k = 0; k < 2; k++) {
        const r = this.refs[k]!
        if (r.tileGen.length > 0) this.precomputeTiles(r)
      }
    }
    const rf = this.bRefFrame
    if (mid >= 0) {
      this.genSeq++
      rf.gen = this.genSeq
    }
    const midIndex = mid >= 0 ? this.pending[mid]!.index : -1
    const intoCur = !parallel && !this.wantRecon
    if (intoCur) this.cur.ver.fill(-1)
    let shs: SharedState[] = []
    let fps: FrameParams[] = []
    this.bRunIdx = []
    this.bRunQps = []
    this.bRunAnchorQp = this.anchorQp
    this.bRunData = []
    this.bRunRecon = []
    let targets = 0
    for (let k = 0; k < n; k++) {
      const p = this.pending[k]!
      const isMid = k === mid
      const before = mid >= 0 && k < mid
      const after = mid >= 0 && k > mid
      const l0 = after ? midIndex : this.prevAnchorIndex
      const l1 = before ? midIndex : this.lastAnchorIndex
      this.frameIndex = p.index
      this.bTb = p.index - l0
      this.bTd = l1 - l0
      this.bTdCol = before ? this.bTd : this.lastAnchorIndex - this.prevAnchorIndex
      this.frameQp = isMid ? rq : bq
      this.nalRef = isMid
      this.frameNum = before || after ? (aFrameNum + 1) & ((1 << this.params.log2MaxFrameNum) - 1) : aFrameNum
      let target = rf
      if (!isMid) {
        target = intoCur ? this.cur : this.bTarget(targets)
        targets++
      }
      const sh = this.bSlotState(this.bSlot(parallel ? k : 0), p.data, target)
      const fp = this.frameParams()
      fp.deblock = false
      if (isMid) sh.mbQp = this.bMbQpRef
      if (before || after) {
        const j = before ? 0 : 1
        sh.refY[j] = rf.y
        sh.refU[j] = rf.u
        sh.refV[j] = rf.v
        sh.refHb[j] = rf.hb
        sh.refHh[j] = rf.hh
        sh.refHj[j] = rf.hj
        sh.refTiles[j] = rf.tileGen
        fp.refGen[j] = rf.gen
      }
      if (before) {
        sh.colMvx = this.bColMvx
        sh.colMvy = this.bColMvy
        sh.colRef = this.bColRef
      }
      shs.push(sh)
      fps.push(fp)
      this.bRunData.push(p.data)
      this.bRunRecon.push(target)
      this.bRunIdx.push(p.index)
      this.bRunQps.push(this.frameQp)
    }
    this.bRunMid = mid
    this.bRunOrder = []
    if (mid >= 0) this.bRunOrder.push(mid)
    for (let k = 0; k < n; k++) {
      if (k !== mid) this.bRunOrder.push(k)
    }
    this.frameIndex = aFrameIndex
    this.frameQp = bq
    this.nalRef = false
    this.frameNum = mid >= 0 ? (aFrameNum + 1) & ((1 << this.params.log2MaxFrameNum) - 1) : aFrameNum
    if (mid >= 0) this.bRefNum = aFrameNum
    this.sliceType = SLICE_P
    this.codedFrames += n
    const opts = this.workerOpts()
    const tuning = this.tuning()
    const workers = parallel ? this.threads : 1
    let progress: Int32Array[] = []
    for (let k = 0; k < n; k++) progress.push(progressArray(this.mbh))
    const next = new Int32Array(n)
    const done = new Int32Array(n)
    const stats = new Float64Array(workers * n * 5)
    const bm: BMid = {
      mid, gate: new Int32Array(2), tileCnt: new Int32Array(2), lazy: !parallel, oneSlot: !parallel, colMvx: this.bColMvx,
      colMvy: this.bColMvy, colRef: this.bColRef, y: rf.y, hb: rf.hb, hh: rf.hh, hj: rf.hj, tileGen: rf.tileGen,
      gen: rf.gen,
    }
    this.bRunStats = stats
    this.bRunAnalysis = (performance.now() - t0) / f64(n)
    this.bRunT1 = performance.now()
    if (!parallel) {
      let ws: Encoder[] = []
      for (let k = 0; k < n; k++) ws.push(bWorker(opts, shs[k]!, fps[k]!, tuning, false))
      this.bInline.push(codeBRun(ws, next, done, progress, stats, 0, bm))
      return
    }
    for (let t = 0; t < workers; t++) {
      const slot = t
      this.bThreads.push(Thread.spawn(() => {
        let ws: Encoder[] = []
        for (let k = 0; k < n; k++) ws.push(bWorker(opts, shs[k]!, fps[k]!, tuning, parallel))
        return codeBRun(ws, next, done, progress, stats, slot, bm)
      }))
    }
  }

  finishBRun(inout out: u8[]) {
    if (this.bThreads.length === 0 && this.bInline.length === 0) return
    let results: u8[][][] = this.bInline
    this.bInline = []
    for (const th of this.bThreads) results.push(th.join())
    this.bThreads = []
    const span = performance.now() - this.bRunT1
    const stats = this.bRunStats
    const n = this.bRunIdx.length
    const workers = results.length
    const mbs = this.mbw * this.mbh
    let work = 0.0
    for (const v of stats) work += v
    const scale = work > 0.0 ? span / work : 0.0
    const analysis = this.bRunAnalysis
    for (const k of this.bRunOrder) {
      const index = this.bRunIdx[k]!
      const before = out.length
      for (const parts of results) {
        for (const b of parts[k]!) out.push(b)
      }
      let ms: f64[] = [0.0, 0.0, 0.0, 0.0, 0.0]
      for (let t = 0; t < workers; t++) {
        for (let j = 0; j < 5; j++) ms[j] = ms[j]! + stats[(t * n + k) * 5 + j]! * scale
      }
      const total = analysis + ms[0]! + ms[1]! + ms[2]! + ms[3]! + ms[4]!
      this.tAnalysis += analysis
      this.tMotion += ms[0]!
      this.tMode += ms[1]!
      this.tTq += ms[2]!
      this.tEntropy += ms[3]!
      this.tDeblock += ms[4]!
      this.rate.updateB(this.bRunAnchorQp, (out.length - before) * 8)
      this.frames.push({
        index, type: "B", bytes: out.length - before, qp: this.bRunQps[k]!, changedMbs: mbs, analysis,
        motion: ms[0]!, mode: ms[1]!, tq: ms[2]!, entropy: ms[3]!, deblock: ms[4]!, total, reused: 0,
      })
      this.holdRecon(this.bRunRecon[k]!, index)
    }
    for (const d of this.bRunData) this.inputPool.push(d)
    this.bRunData = []
    this.bRunRecon = []
  }

  setBDirect(mb: int, t: int) {
    this.mbType[mb] = t
    this.refIdx[mb] = this.dRef0
    this.refIdx1[mb] = this.dRef1
    this.mvx[mb] = this.dRef0 >= 0 ? this.dMx0 : 0
    this.mvy[mb] = this.dRef0 >= 0 ? this.dMy0 : 0
    this.mvx1[mb] = this.dRef1 >= 0 ? this.dMx1 : 0
    this.mvy1[mb] = this.dRef1 >= 0 ? this.dMy1 : 0
    this.mvdx[mb] = 0
    this.mvdy[mb] = 0
    this.mvdx1[mb] = 0
    this.mvdy1[mb] = 0
  }

  deriveDirect(mbx: int, mby: int) {
    const mb = mby * this.mbw + mbx
    const aAv = mbx > 0
    const bAv = mby > this.sliceRow0
    let cAv = bAv && mbx + 1 < this.mbw
    let mbC = mb - this.mbw + 1
    if (!cAv) {
      cAv = mbx > 0 && bAv
      mbC = mb - this.mbw - 1
    }
    let r0 = this.directRef(this.refIdx, mb, aAv, bAv, cAv, mbC)
    let r1 = this.directRef(this.refIdx1, mb, aAv, bAv, cAv, mbC)
    let zero = false
    if (r0 < 0 && r1 < 0) {
      r0 = 0
      r1 = 0
      zero = true
    }
    const colZero = this.colRef[mb]! === 0 && iabs(this.colMvx[mb]!) <= 1 && iabs(this.colMvy[mb]!) <= 1
    this.dRef0 = r0
    this.dRef1 = r1
    this.dMx0 = 0
    this.dMy0 = 0
    this.dMx1 = 0
    this.dMy1 = 0
    if (!zero && r0 >= 0 && !(r0 === 0 && colZero)) {
      this.computeMvpL(mbx, mby, r0, 0)
      this.dMx0 = this.mvpX
      this.dMy0 = this.mvpY
    }
    if (!zero && r1 >= 0 && !(r1 === 0 && colZero)) {
      this.computeMvpL(mbx, mby, r1, 1)
      this.dMx1 = this.mvpX
      this.dMy1 = this.mvpY
    }
  }

  directRef(refs: Int32Array, mb: int, aAv: bool, bAv: bool, cAv: bool, mbC: int): int {
    const ra = aAv && !this.isIntra(mb - 1) ? refs[mb - 1]! : -1
    const rb = bAv && !this.isIntra(mb - this.mbw) ? refs[mb - this.mbw]! : -1
    const rc = cAv && !this.isIntra(mbC) ? refs[mbC]! : -1
    return minPositive(ra, minPositive(rb, rc))
  }

  directInRange(px: int, py: int): bool {
    if (this.dRef0 >= 0) {
      const ix = this.dMx0 >> 2
      const iy = this.dMy0 >> 2
      if (ix !== this.clampMvx(px, ix) || iy !== this.clampMvy(py, iy)) return false
    }
    if (this.dRef1 >= 0) {
      const ix = this.dMx1 >> 2
      const iy = this.dMy1 >> 2
      if (ix !== this.clampMvx(px, ix) || iy !== this.clampMvy(py, iy)) return false
    }
    return true
  }

  buildPredB(mbx: int, mby: int, use0: bool, use1: bool, m0x: int, m0y: int, m1x: int, m1y: int, luma: bool,
    chroma: bool) {
    this.buildPredInto(this.pred, mbx, mby, use0, use1, m0x, m0y, m1x, m1y, luma, chroma)
  }

  directChroma(mbx: int, mby: int) {
    this.buildPredB(mbx, mby, this.dRef0 >= 0, this.dRef1 >= 0, this.dMx0, this.dMy0, this.dMx1, this.dMy1, false,
      true)
  }

  buildPredInto(dst: Uint8Array, mbx: int, mby: int, use0: bool, use1: bool, m0x: int, m0y: int, m1x: int, m1y: int,
    luma: bool, chroma: bool) {
    if (use0) this.predictList(dst, this.refs[1]!, mbx, mby, m0x, m0y, luma, chroma)
    if (use1) {
      if (use0) {
        this.predictList(this.predB, this.refs[0]!, mbx, mby, m1x, m1y, luma, chroma)
      } else {
        this.predictList(dst, this.refs[0]!, mbx, mby, m1x, m1y, luma, chroma)
      }
    }
    if (use0 && use1) {
      const w1 = this.implicitW1()
      if (w1 === 32) {
        if (luma) avgPlanes(dst, 0, 16, dst, 0, this.predB, 0, 16, 16, 16)
        if (chroma) {
          avgPlanes(dst, 256, 16, dst, 256, this.predB, 256, 16, 16, 8)
        }
        return
      }
      if (luma) weightedAvgPlanes(dst, 0, 16, dst, 0, this.predB, 0, 16, 16, 16, 64 - w1, w1)
      if (chroma) {
        weightedAvgPlanes(dst, 256, 16, dst, 256, this.predB, 256, 16, 16, 8, 64 - w1, w1)
      }
    }
  }

  predictList(dst: Uint8Array, rf: RefFrame, mbx: int, mby: int, mx: int, my: int, luma: bool, chroma: bool) {
    const px = mbx * 16
    const py = mby * 16
    if (luma) {
      this.ensureTiles(rf, px + (mx >> 2) - 1, py + (my >> 2) - 1, 19, 19)
      this.predictQpel(dst, 0, 16, rf, px, py, mx, my, 16, 16)
    }
    if (chroma) {
      const base = this.rc(mbx * 8, mby * 8)
      predictChroma(dst, 256, 8, rf.u, this.cs, base, mx, my, 8, 8)
      predictChroma(dst, 320, 8, rf.v, this.cs, base, mx, my, 8, 8)
    }
  }

  implicitW1(): int {
    if (!this.params.weightedPred || this.bTd <= 0) return 32
    const td = 2 * this.bTd
    const tx = Math.trunc((16384 + (td >> 1)) / td)
    const dsf = (2 * this.bTb * tx + 32) >> 6
    const w1 = (dsf < -1024 ? -1024 : dsf > 1023 ? 1023 : dsf) >> 2
    return w1 < -64 || w1 > 128 ? 32 : w1
  }

  interPred(mb: int, mbx: int, mby: int) {
    if (this.sliceType !== SLICE_B) {
      this.predictInter(mb, mbx, mby, this.mvx[mb]!, this.mvy[mb]!, this.refIdx[mb]!)
      return
    }
    this.buildPredB(mbx, mby, this.refIdx[mb]! >= 0, this.refIdx1[mb]! >= 0, this.mvx[mb]!, this.mvy[mb]!,
      this.mvx1[mb]!, this.mvy1[mb]!, true, true)
  }

  residualZero(mbx: int, mby: int): bool {
    if (!this.lumaZero(mbx * 16, mby * 16)) return false
    return this.chromaZero(mbx, mby)
  }

  storePredZero(mb: int, mbx: int, mby: int) {
    this.storePred(mbx, mby)
    this.cbp[mb] = 0
    for (let k = 0; k < 24; k++) this.nnz[mb * 24 + k] = 0
  }

  finishB(mb: int, mbx: int, mby: int, tested: bool) {
    if (!tested && this.residualZero(mbx, mby)) {
      if (this.mbType[mb]! === MB_BDIRECT) this.mbType[mb] = MB_BSKIP
      this.storePredZero(mb, mbx, mby)
      return
    }
    this.predReady = true
    this.codeMb(mbx, mby)
  }

  searchB(rf: RefFrame, mvxs: Int32Array, mvys: Int32Array, mbx: int, mby: int, lambda: int, list: int) {
    const mb = mby * this.mbw + mbx
    const px = mbx * 16
    const py = mby * 16
    let bix = this.clampMvx(px, (this.mvpX + 2) >> 2)
    let biy = this.clampMvy(py, (this.mvpY + 2) >> 2)
    this.seenGen++
    if (this.seenGen >= 1 << 30) {
      this.seen.fill(0)
      this.seenGen = 1
    }
    this.seenX = bix - 32
    this.seenY = biy - 32
    this.visited(bix, biy)
    let bsad = this.sadAt(rf, px, py, bix, biy)
    let bc = bsad + this.mvCost(bix * 4, biy * 4, lambda)
    const td = this.bTd > 0 ? this.bTd : 1
    const tdc = this.bTdCol > 0 ? this.bTdCol : td
    const scale = list === 0 ? this.bTb : this.bTb - td
    for (let k = 0; k < 6 && bsad > 0; k++) {
      let cx = 0
      let cy = 0
      if (k === 1) {
        const dr = list === 0 ? this.dRef0 : this.dRef1
        if (dr < 0) continue
        cx = list === 0 ? this.dMx0 : this.dMx1
        cy = list === 0 ? this.dMy0 : this.dMy1
      } else if (k === 2) {
        if (mbx === 0) continue
        cx = mvxs[mb - 1]!
        cy = mvys[mb - 1]!
      } else if (k === 3) {
        if (mby <= this.sliceRow0) continue
        cx = mvxs[mb - this.mbw]!
        cy = mvys[mb - this.mbw]!
      } else if (k === 4) {
        if (mby <= this.sliceRow0 || mbx + 1 >= this.mbw) continue
        cx = mvxs[mb - this.mbw + 1]!
        cy = mvys[mb - this.mbw + 1]!
      } else if (k === 5) {
        if (this.colRef[mb]! < 0) continue
        cx = Math.trunc(this.colMvx[mb]! * scale / tdc)
        cy = Math.trunc(this.colMvy[mb]! * scale / tdc)
      }
      const ix = this.clampMvx(px, (cx + 2) >> 2)
      const iy = this.clampMvy(py, (cy + 2) >> 2)
      if (ix === bix && iy === biy) continue
      if (this.visited(ix, iy)) continue
      const sd = this.sadAt(rf, px, py, ix, iy)
      const c = sd + this.mvCost(ix * 4, iy * 4, lambda)
      if (c < bc) {
        bc = c
        bix = ix
        biy = iy
        bsad = sd
      }
    }
    for (let it = 0; it < this.meIters && bsad > 0; it++) {
      let moved = false
      for (let d = 0; d < 4; d++) {
        const ix = this.clampMvx(px, bix + DIR_DX[d]!)
        const iy = this.clampMvy(py, biy + DIR_DY[d]!)
        if (ix === bix && iy === biy) continue
        if (this.visited(ix, iy)) continue
        const c = this.sadAt(rf, px, py, ix, iy) + this.mvCost(ix * 4, iy * 4, lambda)
        if (c < bc) {
          bc = c
          bix = ix
          biy = iy
          moved = true
        }
      }
      if (!moved) break
    }
    this.sMx = bix * 4
    this.sMy = biy * 4
    this.sCost = bc
    this.sSad = true
  }

  refineB(rf: RefFrame, mbx: int, mby: int, lambda: int, first: int, last: int) {
    const px = mbx * 16
    const py = mby * 16
    let bmx = this.sMx
    let bmy = this.sMy
    const halfSad = this.hpelSad && this.subpel > 0
    const video = this.textMbs * 256 < this.mbw * this.mbh
    const quarterSad = halfSad && (this.qpelSad || video) && this.mbq >= this.frameQp
    let bq = this.sCost
    let sadCost = this.sSad
    if (first === 2) {
      this.ensureTiles(rf, px + (bmx >> 2) - 1, py + (bmy >> 2) - 1, 19, 19)
      if (!halfSad) bq = this.satdQpel(rf, px, py, bmx, bmy) + this.mvCost(bmx, bmy, lambda)
      sadCost = halfSad
    }
    for (let step = first; step >= last && this.subpel > 0; step = step >> 1) {
      const cx = bmx
      const cy = bmy
      const useSad = step === 2 ? halfSad : quarterSad
      if (sadCost && !useSad) {
        bq = this.satdQpel(rf, px, py, bmx, bmy) + this.mvCost(bmx, bmy, lambda)
        sadCost = false
      }
      const dirs = !video ? this.subpelDirs : step === 2 ? this.videoHpelDirs : this.videoQpelDirs
      for (let d = 0; d < dirs; d++) {
        const mx = cx + DIR_DX[d]! * step
        const my = cy + DIR_DY[d]! * step
        const dist = !useSad ? this.satdQpel(rf, px, py, mx, my) : step === 2 ? this.sadHalf(rf, px, py, mx, my) :
          this.sadQuarter(rf, px, py, mx, my)
        const c = dist + this.mvCost(mx, my, lambda)
        if (c < bq) {
          bq = c
          bmx = mx
          bmy = my
        }
      }
    }
    if (last === 1 && sadCost) {
      bq = this.satdQpel(rf, px, py, bmx, bmy) + this.mvCost(bmx, bmy, lambda)
      sadCost = false
    }
    this.sMx = bmx
    this.sMy = bmy
    this.sCost = bq
    this.sSad = sadCost
  }

  setSearchB(mx: int, my: int, cost: int, sadCost: bool, pX: int, pY: int) {
    this.sMx = mx
    this.sMy = my
    this.sCost = cost
    this.sSad = sadCost
    this.mvpX = pX
    this.mvpY = pY
  }

  codeMbB(mbx: int, mby: int) {
    const mb = mby * this.mbw + mbx
    this.mbq = this.mbQp[mb]!
    const lambda = LAMBDA[this.mbq]!
    const timed = ((mbx + mby) & 7) === 0
    const a = timed ? performance.now() : 0.0
    const px = mbx * 16
    const py = mby * 16
    const aw = this.aw
    const so = py * aw + px
    this.dcCbf[mb] = 0
    this.t8[mb] = 0
    this.deriveDirect(mbx, mby)
    const gated = this.mbq >= B_GATE_QP
    let dCost = BIG
    let dTested = false
    let dChroma = false
    if (this.directInRange(px, py)) {
      this.buildPredB(mbx, mby, this.dRef0 >= 0, this.dRef1 >= 0, this.dMx0, this.dMy0, this.dMx1, this.dMy1, true,
        false)
      if (sad16(this.srcY, so, aw, this.pred, 0, 16) <= B_SKIP_SAD * lambda) {
        if (this.lumaZero(px, py)) {
          this.directChroma(mbx, mby)
          dChroma = true
          if (this.chromaZero(mbx, mby)) {
            this.setBDirect(mb, MB_BSKIP)
            this.storePredZero(mb, mbx, mby)
            if (timed) this.rMd += (performance.now() - a) * 8.0
            return
          }
        }
        dTested = true
      }
      const dSatd = satd16(this.srcY, so, aw, this.pred, 0, 16)
      dCost = dSatd + lambda
      const nb = mbx > 0 && mby > this.sliceRow0 && this.mbType[mb - 1]! >= MB_BSKIP &&
        this.mbType[mb - 1]! <= MB_BDIRECT && this.mbType[mb - this.mbw]! >= MB_BSKIP &&
        this.mbType[mb - this.mbw]! <= MB_BDIRECT
      if (this.earlyExit && dSatd < (nb && gated ? this.bDirectNb : B_DIRECT_EARLY) * lambda) {
        if (!dChroma) this.directChroma(mbx, mby)
        this.setBDirect(mb, MB_BDIRECT)
        const c = timed ? performance.now() : 0.0
        this.finishB(mb, mbx, mby, dTested)
        if (timed) {
          const d = performance.now()
          this.rMo += (c - a) * 8.0
          this.rTq += (d - c) * 8.0
        }
        return
      }
    }
    this.computeMvpL(mbx, mby, 0, 0)
    const p0x = this.mvpX
    const p0y = this.mvpY
    const qpelBest = this.earlyExit && gated
    const halfLast = qpelBest ? 2 : 1
    this.searchB(this.refs[1]!, this.mvx, this.mvy, mbx, mby, lambda, 0)
    this.refineB(this.refs[1]!, mbx, mby, lambda, 2, halfLast)
    const h0x = this.sMx
    const h0y = this.sMy
    const h0c = this.sCost
    const h0s = this.sSad
    this.computeMvpL(mbx, mby, 0, 1)
    const p1x = this.mvpX
    const p1y = this.mvpY
    this.searchB(this.refs[0]!, this.mvx1, this.mvy1, mbx, mby, lambda, 1)
    this.refineB(this.refs[0]!, mbx, mby, lambda, 2, halfLast)
    const h1c = this.sCost
    this.refineB(this.refs[0]!, mbx, mby, lambda, qpelBest && h1c <= h0c ? 1 : 0, 1)
    const m1x = this.sMx
    const m1y = this.sMy
    const c1 = this.sCost + 3 * lambda
    this.setSearchB(h0x, h0y, h0c, h0s, p0x, p0y)
    this.refineB(this.refs[1]!, mbx, mby, lambda, qpelBest && h0c <= h1c ? 1 : 0, 1)
    const m0x = this.sMx
    const m0y = this.sMy
    const c0 = this.sCost + 3 * lambda
    const asDirect = dCost < BIG && this.dRef0 >= 0 && this.dRef1 >= 0 && m0x === this.dMx0 && m0y === this.dMy0 &&
      m1x === this.dMx1 && m1y === this.dMy1
    let cBi = BIG
    if (!asDirect && (!gated || c0 <= dCost || c1 <= dCost)) {
      this.buildPredInto(this.predBi, mbx, mby, true, true, m0x, m0y, m1x, m1y, true, false)
      cBi = satd16(this.srcY, so, aw, this.predBi, 0, 16) + lambda * (seBits(m0x - p0x) + seBits(m0y - p0y) +
        seBits(m1x - p1x) + seBits(m1y - p1y) + 5)
    }
    let best = MB_BDIRECT
    let bestCost = dCost
    if (c0 < bestCost) {
      best = MB_BL0
      bestCost = c0
    }
    if (c1 < bestCost) {
      best = MB_BL1
      bestCost = c1
    }
    if (cBi < bestCost) {
      best = MB_BBI
      bestCost = cBi
    }
    const b = timed ? performance.now() : 0.0
    this.intraEst = this.intraEstimate(mbx, mby)
    const skipIntra = this.earlyExit && bestCost * 2 < this.intraEst * 3
    // decideIntra codes its I4/I8 blocks through this.pred, which holds the direct prediction finishB reuses.
    const keepDirect = !skipIntra && best === MB_BDIRECT
    if (keepDirect) copyRect(this.predBi, 0, 16, this.pred, 0, 16, 16, 16)
    const intraCost = skipIntra ? BIG : this.decideIntra(mbx, mby, bestCost)
    if (keepDirect && intraCost >= bestCost) copyRect(this.pred, 0, 16, this.predBi, 0, 16, 16, 16)
    const c = timed ? performance.now() : 0.0
    if (intraCost < bestCost) {
      this.commitIntra(mb, mbx, mby)
      this.refIdx[mb] = -1
      this.refIdx1[mb] = -1
      this.mvx[mb] = 0
      this.mvy[mb] = 0
      this.mvx1[mb] = 0
      this.mvy1[mb] = 0
      this.mvdx[mb] = 0
      this.mvdy[mb] = 0
      this.mvdx1[mb] = 0
      this.mvdy1[mb] = 0
      this.codeMb(mbx, mby)
    } else if (best === MB_BDIRECT) {
      if (!dChroma) this.directChroma(mbx, mby)
      this.setBDirect(mb, MB_BDIRECT)
      this.finishB(mb, mbx, mby, dTested)
    } else {
      const u0 = best !== MB_BL1
      const u1 = best !== MB_BL0
      this.mbType[mb] = best
      this.refIdx[mb] = u0 ? 0 : -1
      this.refIdx1[mb] = u1 ? 0 : -1
      this.mvx[mb] = u0 ? m0x : 0
      this.mvy[mb] = u0 ? m0y : 0
      this.mvx1[mb] = u1 ? m1x : 0
      this.mvy1[mb] = u1 ? m1y : 0
      this.mvdx[mb] = u0 ? m0x - p0x : 0
      this.mvdy[mb] = u0 ? m0y - p0y : 0
      this.mvdx1[mb] = u1 ? m1x - p1x : 0
      this.mvdy1[mb] = u1 ? m1y - p1y : 0
      if (best === MB_BBI) {
        copyRect(this.pred, 0, 16, this.predBi, 0, 16, 16, 16)
        this.buildPredB(mbx, mby, true, true, m0x, m0y, m1x, m1y, false, true)
      } else {
        this.buildPredB(mbx, mby, u0, u1, m0x, m0y, m1x, m1y, true, true)
      }
      this.finishB(mb, mbx, mby, false)
    }
    if (timed) {
      const d = performance.now()
      this.rMo += (b - a) * 8.0
      this.rMd += (c - b) * 8.0
      this.rTq += (d - c) * 8.0
    }
  }

  edgeBsB(p: int, q: int): int {
    const p0 = this.refIdx[p]! >= 0
    const p1 = this.refIdx1[p]! >= 0
    const q0 = this.refIdx[q]! >= 0
    const q1 = this.refIdx1[q]! >= 0
    if (p0 !== q0 || p1 !== q1) return 1
    if (p0 && (iabs(this.mvx[p]! - this.mvx[q]!) >= 4 || iabs(this.mvy[p]! - this.mvy[q]!) >= 4)) return 1
    if (p1 && (iabs(this.mvx1[p]! - this.mvx1[q]!) >= 4 || iabs(this.mvy1[p]! - this.mvy1[q]!) >= 4)) return 1
    return 0
  }

  isIntra(mb: int): bool {
    return this.mbType[mb]! <= MB_I8
  }

  computeMvp(mbx: int, mby: int, ref: int) {
    this.computeMvpL(mbx, mby, ref, 0)
  }

  computeMvpL(mbx: int, mby: int, ref: int, list: int) {
    if (list === 0) {
      this.mvpFrom(this.mvx, this.mvy, this.refIdx, mbx, mby, ref)
    } else {
      this.mvpFrom(this.mvx1, this.mvy1, this.refIdx1, mbx, mby, ref)
    }
  }

  mvpFrom(mvxs: Int32Array, mvys: Int32Array, refs: Int32Array, mbx: int, mby: int, ref: int) {
    const mb = mby * this.mbw + mbx
    const aAv = mbx > 0
    const bAv = mby > this.sliceRow0
    let cAv = bAv && mbx + 1 < this.mbw
    let mbC = mb - this.mbw + 1
    let rC = 12
    if (!cAv) {
      cAv = mbx > 0 && bAv
      mbC = mb - this.mbw - 1
      rC = 15
    }
    let refA = -1
    let refB = -1
    let refC = -1
    let ax = 0
    let ay = 0
    let bx = 0
    let by = 0
    let cx = 0
    let cy = 0
    if (aAv && !this.isIntra(mb - 1)) {
      refA = refs[mb - 1]!
      ax = mvxs[mb - 1]!
      ay = mvys[mb - 1]!
    }
    if (bAv && !this.isIntra(mb - this.mbw)) {
      refB = refs[mb - this.mbw]!
      bx = mvxs[mb - this.mbw]!
      by = mvys[mb - this.mbw]!
    }
    if (cAv && !this.isIntra(mbC)) {
      refC = refs[mbC]!
      cx = mvxs[mbC]!
      cy = mvys[mbC]!
    }
    if (!bAv && !cAv && aAv) {
      refB = refA
      refC = refA
      bx = ax
      by = ay
      cx = ax
      cy = ay
    }
    const ma = refA === ref ? 1 : 0
    const mbb = refB === ref ? 1 : 0
    const mc = refC === ref ? 1 : 0
    if (ma + mbb + mc === 1) {
      if (ma === 1) {
        this.mvpX = ax
        this.mvpY = ay
      } else if (mbb === 1) {
        this.mvpX = bx
        this.mvpY = by
      } else {
        this.mvpX = cx
        this.mvpY = cy
      }
      return
    }
    this.mvpX = median3(ax, bx, cx)
    this.mvpY = median3(ay, by, cy)
  }

  computeSkipMv(mbx: int, mby: int) {
    const mb = mby * this.mbw + mbx
    if (mbx === 0 || mby === this.sliceRow0) {
      this.skipX = 0
      this.skipY = 0
      return
    }
    const a = mb - 1
    const b = mb - this.mbw
    if (!this.isIntra(a) && this.refIdx[a]! === 0 && this.mvx[a]! === 0 && this.mvy[a]! === 0) {
      this.skipX = 0
      this.skipY = 0
      return
    }
    if (!this.isIntra(b) && this.refIdx[b]! === 0 && this.mvx[b]! === 0 && this.mvy[b]! === 0) {
      this.skipX = 0
      this.skipY = 0
      return
    }
    this.computeMvp(mbx, mby, 0)
    this.skipX = this.mvpX
    this.skipY = this.mvpY
  }

  clampMvx(px: int, ix: int): int {
    const lo = -MVPAD - px + 6
    const hi = this.aw + MVPAD - px - 24
    return ix < lo ? lo : ix > hi ? hi : ix
  }

  clampMvy(py: int, iy: int): int {
    let lo = -MVPAD - py + 6
    let hi = this.ah + MVPAD - py - 24
    if (lo < -500) lo = -500
    if (hi > 500) hi = 500
    return iy < lo ? lo : iy > hi ? hi : iy
  }

  sadAt(ref: RefFrame, px: int, py: int, ix: int, iy: int): int {
    if (this.wpOn && ref === this.refs[0]!) return sad16(this.srcY, py * this.aw + px, this.aw, this.wref, this.ry(px + ix, py + iy), this.ys)
    return sad16(this.srcY, py * this.aw + px, this.aw, ref.y, this.ry(px + ix, py + iy), this.ys)
  }

  satdQpel(rf: RefFrame, px: int, py: int, mx: int, my: int): int {
    const so = py * this.aw + px
    if (this.wpOn && rf === this.refs[0]!) {
      this.predictQpel(this.trial, 0, 16, rf, px, py, mx, my, 16, 16)
      return satd16(this.srcY, so, this.aw, this.trial, 0, 16)
    }
    const fx = mx & 3
    const fy = my & 3
    const base = this.ry(px + (mx >> 2), py + (my >> 2))
    const ys = this.ys
    if (fx === 0 && fy === 0) return satd16(this.srcY, so, this.aw, rf.y, base, ys)
    if (fx === 2 && fy === 0) return satd16(this.srcY, so, this.aw, rf.hb, base, ys)
    if (fx === 0 && fy === 2) return satd16(this.srcY, so, this.aw, rf.hh, base, ys)
    if (fx === 2 && fy === 2) return satd16(this.srcY, so, this.aw, rf.hj, base, ys)
    if (fy === 0) return satdAvg16(this.srcY, so, this.aw, rf.y, base + (fx >> 1), rf.hb, base, ys)
    if (fx === 0) return satdAvg16(this.srcY, so, this.aw, rf.y, base + (fy >> 1) * ys, rf.hh, base, ys)
    if (fx === 2) return satdAvg16(this.srcY, so, this.aw, rf.hj, base, rf.hb, base + (fy >> 1) * ys, ys)
    if (fy === 2) return satdAvg16(this.srcY, so, this.aw, rf.hj, base, rf.hh, base + (fx >> 1), ys)
    return satdAvg16(this.srcY, so, this.aw, rf.hb, base + (fy >> 1) * ys, rf.hh, base + (fx >> 1), ys)
  }

  visited(ix: int, iy: int): bool {
    const sx = ix - this.seenX
    const sy = iy - this.seenY
    if ((sx | sy) < 0 || sx >= 64 || sy >= 64) return false
    const k = sy * 64 + sx
    if (this.seen[k]! === this.seenGen) return true
    this.seen[k] = this.seenGen
    return false
  }

  sadHalf(rf: RefFrame, px: int, py: int, mx: int, my: int): int {
    const so = py * this.aw + px
    if (this.wpOn && rf === this.refs[0]!) {
      this.predictQpel(this.trial, 0, 16, rf, px, py, mx, my, 16, 16)
      return sad16(this.srcY, so, this.aw, this.trial, 0, 16)
    }
    const base = this.ry(px + (mx >> 2), py + (my >> 2))
    if ((mx & 3) === 0 && (my & 3) === 0) return sad16(this.srcY, so, this.aw, rf.y, base, this.ys)
    if ((my & 3) === 0) return sad16(this.srcY, so, this.aw, rf.hb, base, this.ys)
    if ((mx & 3) === 0) return sad16(this.srcY, so, this.aw, rf.hh, base, this.ys)
    return sad16(this.srcY, so, this.aw, rf.hj, base, this.ys)
  }

  sadQuarter(rf: RefFrame, px: int, py: int, mx: int, my: int): int {
    const so = py * this.aw + px
    if (this.wpOn && rf === this.refs[0]!) {
      this.predictQpel(this.trial, 0, 16, rf, px, py, mx, my, 16, 16)
      return sad16(this.srcY, so, this.aw, this.trial, 0, 16)
    }
    const base = this.ry(px + (mx >> 2), py + (my >> 2))
    const fx = mx & 3
    const fy = my & 3
    const ys = this.ys
    if (fy === 0) return sadAvg16(this.srcY, so, this.aw, rf.y, base + (fx >> 1), rf.hb, base, ys)
    if (fx === 0) return sadAvg16(this.srcY, so, this.aw, rf.y, base + (fy >> 1) * ys, rf.hh, base, ys)
    if (fx === 2) return sadAvg16(this.srcY, so, this.aw, rf.hj, base, rf.hb, base + (fy >> 1) * ys, ys)
    if (fy === 2) return sadAvg16(this.srcY, so, this.aw, rf.hj, base, rf.hh, base + (fx >> 1), ys)
    return sadAvg16(this.srcY, so, this.aw, rf.hb, base + (fy >> 1) * ys, rf.hh, base + (fx >> 1), ys)
  }

  mvCost(mx: int, my: int, lambda: int): int {
    return lambda * (seBits(mx - this.mvpX) + seBits(my - this.mvpY))
  }

  intraEstimate(mbx: int, mby: int): int {
    const px = mbx * 16
    const py = mby * 16
    const av = this.mbAvail(mbx, mby)
    pred16v(this.trial, 0, 16, this.srcY, this.aw, px, py, I16_DC, av)
    return satd16(this.srcY, py * this.aw + px, this.aw, this.trial, 0, 16)
  }

  motionSearch(mbx: int, mby: int) {
    const mb = mby * this.mbw + mbx
    const px = mbx * 16
    const py = mby * 16
    const lambda = LAMBDA[this.mbq]!
    this.intraEst = this.intraEstimate(mbx, mby)
    const giveUp = this.intraEst + (this.intraEst >> 2) + 16 * lambda
    let bestCost = BIG
    let bestRef = 0
    let bestMx = 0
    let bestMy = 0
    for (let ref = 0; ref < this.refsActive; ref++) {
      const rf = this.refs[ref]!
      this.computeMvp(mbx, mby, ref)
      let bix = this.clampMvx(px, (this.mvpX + 2) >> 2)
      let biy = this.clampMvy(py, (this.mvpY + 2) >> 2)
      this.seenGen++
      if (this.seenGen >= 1 << 30) {
        this.seen.fill(0)
        this.seenGen = 1
      }
      this.seenX = bix - 32
      this.seenY = biy - 32
      this.visited(bix, biy)
      let bsad = this.sadAt(rf, px, py, bix, biy)
      let bc = bsad + this.mvCost(bix * 4, biy * 4, lambda)
      const cands = ref === 0 ? 9 : 5
      for (let k = 0; k < cands && bsad > 0; k++) {
        let cx = 0
        let cy = 0
        if (k === 1) {
          if (mbx === 0) continue
          cx = this.mvx[(mb - 1)]!
          cy = this.mvy[(mb - 1)]!
        } else if (k === 2) {
          if (mby <= this.sliceRow0) continue
          cx = this.mvx[(mb - this.mbw)]!
          cy = this.mvy[(mb - this.mbw)]!
        } else if (k === 3) {
          if (mby <= this.sliceRow0 || mbx + 1 >= this.mbw) continue
          cx = this.mvx[(mb - this.mbw + 1)]!
          cy = this.mvy[(mb - this.mbw + 1)]!
        } else if (k === 4) {
          cx = this.prevMvx[mb]!
          cy = this.prevMvy[mb]!
        } else if (k === 5) {
          if (!this.inScroll(mbx, mby)) continue
          cx = this.scrollMvx
          cy = this.scrollMvy
        } else if (k === 6) {
          if (this.cursorMb < 0) continue
          const cmy = Math.trunc(this.cursorMb / this.mbw)
          const cmx = this.cursorMb - cmy * this.mbw
          if (iabs(cmx - mbx) > 2 || iabs(cmy - mby) > 2) continue
          cx = this.cursorMvx
          cy = this.cursorMvy
        } else if (k === 7) {
          if (this.shiftHx === 0) continue
          cx = this.shiftHx
        } else if (k === 8) {
          if (this.shiftVy === 0) continue
          cy = this.shiftVy
        }
        const ix = this.clampMvx(px, (cx + 2) >> 2)
        const iy = this.clampMvy(py, (cy + 2) >> 2)
        if (ix === bix && iy === biy) continue
        if (this.visited(ix, iy)) continue
        const sd = this.sadAt(rf, px, py, ix, iy)
        const c = sd + this.mvCost(ix * 4, iy * 4, lambda)
        if (c < bc || sd === 0) {
          bc = c
          bix = ix
          biy = iy
          bsad = sd
        }
      }
      if (bsad === 0 || (this.earlyExit && bsad > giveUp)) {
        const c0 = bc + lambda * (this.refsActive > 1 ? (this.refsActive === 2 ? 1 : ueBits(ref)) : 0)
        if (c0 < bestCost) {
          bestCost = c0
          bestRef = ref
          bestMx = bix * 4
          bestMy = biy * 4
        }
        continue
      }
      for (let it = 0; it < this.meIters; it++) {
        let moved = false
        for (let d = 0; d < 4; d++) {
          const ix = this.clampMvx(px, bix + DIR_DX[d]!)
          const iy = this.clampMvy(py, biy + DIR_DY[d]!)
          if (ix === bix && iy === biy) continue
          if (this.visited(ix, iy)) continue
          const c = this.sadAt(rf, px, py, ix, iy) + this.mvCost(ix * 4, iy * 4, lambda)
          if (c < bc) {
            bc = c
            bix = ix
            biy = iy
            moved = true
          }
        }
        if (!moved) break
      }
      this.ensureTiles(rf, px + bix - 1, py + biy - 1, 19, 19)
      let bmx = bix * 4
      let bmy = biy * 4
      const halfSad = this.hpelSad && this.subpel > 0
      const video = this.textMbs * 256 < this.mbw * this.mbh
      const quarterSad = halfSad && (this.qpelSad || video) && this.mbq >= this.frameQp
      let bq = halfSad ? bc : this.satdQpel(rf, px, py, bmx, bmy) + this.mvCost(bmx, bmy, lambda)
      let sadCost = halfSad
      for (let step = 2; step >= (this.subpel >= 2 ? 1 : 2) && this.subpel > 0; step = step >> 1) {
        const cx = bmx
        const cy = bmy
        const useSad = step === 2 ? halfSad : quarterSad
        if (sadCost && !useSad) {
          bq = this.satdQpel(rf, px, py, bmx, bmy) + this.mvCost(bmx, bmy, lambda)
          sadCost = false
        }
        const dirs = !video ? this.subpelDirs : step === 2 ? this.videoHpelDirs : this.videoQpelDirs
        for (let d = 0; d < dirs; d++) {
          const mx = cx + DIR_DX[d]! * step
          const my = cy + DIR_DY[d]! * step
          const dist = !useSad ? this.satdQpel(rf, px, py, mx, my) : step === 2 ? this.sadHalf(rf, px, py, mx, my) :
            this.sadQuarter(rf, px, py, mx, my)
          const c = dist + this.mvCost(mx, my, lambda)
          if (c < bq) {
            bq = c
            bmx = mx
            bmy = my
          }
        }
      }
      if (sadCost) bq = this.satdQpel(rf, px, py, bmx, bmy) + this.mvCost(bmx, bmy, lambda)
      bq += lambda * (this.refsActive > 1 ? (this.refsActive === 2 ? 1 : ueBits(ref)) : 0)
      if (bq < bestCost) {
        bestCost = bq
        bestRef = ref
        bestMx = bmx
        bestMy = bmy
      }
    }
    this.refIdx[mb] = bestRef
    this.mvx[mb] = bestMx
    this.mvy[mb] = bestMy
    this.mbType[mb] = MB_P16
    this.meCost = bestCost
  }

  zeroMotion(mbx: int, mby: int) {
    const mb = mby * this.mbw + mbx
    this.refIdx[mb] = 0
    this.mvx[mb] = 0
    this.mvy[mb] = 0
    this.mbType[mb] = MB_P16
    this.computeMvp(mbx, mby, 0)
    this.meCost = this.mvCost(0, 0, LAMBDA[this.mbq]!)
  }

  earlySkipMb(mbx: int, mby: int): bool {
    const mb = mby * this.mbw + mbx
    const top = mby > this.sliceRow0
    const nb = (mbx > 0 && this.mbType[mb - 1]! === MB_PSKIP) || (top && this.mbType[mb - this.mbw]! === MB_PSKIP) ||
      (top && mbx > 0 && this.mbType[mb - this.mbw - 1]! === MB_PSKIP) ||
      (top && mbx + 1 < this.mbw && this.mbType[mb - this.mbw + 1]! === MB_PSKIP)
    if (!nb) return false
    this.computeSkipMv(mbx, mby)
    this.skipProbed = true
    if (!this.probeSkip(mbx, mby)) return false
    this.setSkip(mb)
    return true
  }

  probeSkip(mbx: int, mby: int): bool {
    const px = mbx * 16
    const py = mby * 16
    const rf = this.refs[0]!
    const ix = this.skipX >> 2
    const iy = this.skipY >> 2
    if (ix !== this.clampMvx(px, ix) || iy !== this.clampMvy(py, iy)) return false
    this.ensureTiles(rf, px + ix - 1, py + iy - 1, 19, 19)
    this.predictQpel(this.pred, 0, 16, rf, px, py, this.skipX, this.skipY, 16, 16)
    const s = sad16(this.srcY, py * this.aw + px, this.aw, this.pred, 0, 16)
    if (s === 0) return this.chromaSkipZero(mbx, mby)
    if (s > 32 * LAMBDA[this.mbq]! * 4) return false
    if (!this.lumaZero(px, py)) return false
    return this.chromaSkipZero(mbx, mby)
  }

  lumaZero(px: int, py: int): bool {
    const qbits = 15 + Math.idiv(this.mbq, 6)
    const thr = (1 << qbits) - Math.idiv(1 << qbits, 6)
    const t = i32x8.splat(thr)
    const qm = (this.mbq % 6) * 16
    const q0 = i32x8.load(QUANT4, qm)
    const q1 = i32x8.load(QUANT4, qm + 8)
    const sadMax = zeroSadMax(qm, thr, 0)
    for (let by = 0; by < 16; by += 4) {
      const so = (py + by) * this.aw + px
      sadBlocks16(this.srcY, so, this.aw, this.pred, by * 16, 16, this.zsad)
      for (let bx = 0; bx < 4; bx += 2) {
        if (int(this.zsad[bx]!) <= sadMax && int(this.zsad[bx + 1]!) <= sadMax) continue
        fwd4x2(this.srcY, so + bx * 4, this.aw, this.pred, by * 16 + bx * 4, 16, this.c32, 0)
        const m = (i32x8.load(this.c32, 0).abs() * q0).max(i32x8.load(this.c32, 8).abs() * q1)
          .max((i32x8.load(this.c32, 16).abs() * q0).max(i32x8.load(this.c32, 24).abs() * q1))
        if (m.ge(t).mask() !== 0) return false
      }
    }
    return true
  }

  chromaSkipZero(mbx: int, mby: int): bool {
    const cx = mbx * 8
    const cy = mby * 8
    for (let pl = 0; pl < 2; pl++) {
      const po = 256 + pl * 64
      if (pl === 0) {
        predictChroma(this.pred, po, 8, this.refs[0]!.u, this.cs, this.rc(cx, cy), this.skipX, this.skipY, 8, 8)
      } else {
        predictChroma(this.pred, po, 8, this.refs[0]!.v, this.cs, this.rc(cx, cy), this.skipX, this.skipY, 8, 8)
      }
      if (this.wpChromaOn) weightBlock(this.pred, po, 8, 8, 8, pl === 0 ? this.wpU[0]! : this.wpV[0]!, pl === 0 ? this.wpU[1]! : this.wpV[1]!)
    }
    return this.chromaZero(mbx, mby)
  }

  chromaZero(mbx: int, mby: int): bool {
    const cx = mbx * 8
    const cy = mby * 8
    const qpc = int(CHROMA_QP[this.mbq]!)
    const qbits = 15 + Math.trunc(qpc / 6)
    const t = i32x8.splat((1 << qbits) - Math.trunc((1 << qbits) / 6))
    const dcT = (2 << qbits) - Math.trunc((2 << qbits) / 6)
    const qm = (qpc % 6) * 16
    const mf = int(QUANT4[qm]!)
    const q01 = i32x8.load(QUANT4, qm).withLane(0, 0)
    const q23 = i32x8.load(QUANT4, qm + 8)
    const acMax = zeroSadMax(qm, (1 << qbits) - Math.trunc((1 << qbits) / 6), 1)
    const dcMax = Math.trunc((dcT - 1) / mf)
    for (let pl = 0; pl < 2; pl++) {
      const po = 256 + pl * 64
      if (pl === 0) {
        sadBlocks8(this.srcU, cy * this.cw + cx, this.cw, this.pred, po, this.zsad)
      } else {
        sadBlocks8(this.srcV, cy * this.cw + cx, this.cw, this.pred, po, this.zsad)
      }
      const dcFree = int(this.zsad[0]!) + int(this.zsad[1]!) + int(this.zsad[2]!) + int(this.zsad[3]!) <= dcMax
      for (let b = 0; b < 4; b += 2) {
        if (dcFree && int(this.zsad[b]!) <= acMax && int(this.zsad[b + 1]!) <= acMax) continue
        const by = (b >> 1) * 4
        const so = (cy + by) * this.cw + cx
        if (pl === 0) {
          fwd4x2(this.srcU, so, this.cw, this.pred, po + by * 8, 8, this.c32, 0)
        } else {
          fwd4x2(this.srcV, so, this.cw, this.pred, po + by * 8, 8, this.c32, 0)
        }
        const m = (i32x8.load(this.c32, 0).abs() * q01).max(i32x8.load(this.c32, 8).abs() * q23)
          .max((i32x8.load(this.c32, 16).abs() * q01).max(i32x8.load(this.c32, 24).abs() * q23))
        if (m.ge(t).mask() !== 0) return false
        this.dc32[b] = this.c32[0]!
        this.dc32[b + 1] = this.c32[16]!
      }
      if (dcFree) continue
      hadamard2v(this.dc32, 0)
      for (let k = 0; k < 4; k++) {
        if (iabs(int(this.dc32[k]!)) * mf >= dcT) return false
      }
    }
    return true
  }

  setSkip(mb: int) {
    this.mbType[mb] = MB_PSKIP
    this.refIdx[mb] = 0
    this.mvx[mb] = this.skipX
    this.mvy[mb] = this.skipY
    this.mvdx[mb] = 0
    this.mvdy[mb] = 0
  }

  decideInter(mbx: int, mby: int) {
    const mb = mby * this.mbw + mbx
    const meCost = this.meCost
    const lambda = LAMBDA[this.mbq]!
    this.computeSkipMv(mbx, mby)
    const mvx = this.mvx[mb]!
    const mvy = this.mvy[mb]!
    if (this.refIdx[mb]! === 0 && mvx === this.skipX && mvy === this.skipY && !this.skipProbed && this.probeSkip(mbx, mby)) {
      this.setSkip(mb)
      return
    }
    const costInter = meCost + lambda
    const keepRef = this.refIdx[mb]!
    const skipIntra = this.refresh[mb]! !== 0 || (this.earlyExit && costInter * 4 < this.intraEst * 3)
    const intraCost = skipIntra ? BIG : this.decideIntra(mbx, mby, costInter)
    if (intraCost < costInter) {
      this.commitIntra(mb, mbx, mby)
      return
    }
    this.mbType[mb] = MB_P16
    this.refIdx[mb] = keepRef
    this.mvx[mb] = mvx
    this.mvy[mb] = mvy
  }

  nbMode(n: int, r: int): int {
    const t = this.mbType[n]!
    return t === MB_I4 || t === MB_I8 ? this.imode[n * 16 + r]! : 2
  }

  predMode4(mb: int, mbx: int, mby: int, bx: int, by: int): int {
    let a = -1
    let b = -1
    if (bx > 0) {
      a = this.imode[mb * 16 + by * 4 + bx - 1]!
    } else if (mbx > 0) {
      a = this.nbMode(mb - 1, by * 4 + 3)
    }
    if (by > 0) {
      b = this.imode[mb * 16 + (by - 1) * 4 + bx]!
    } else if (mby > this.sliceRow0) {
      b = this.nbMode(mb - this.mbw, 12 + bx)
    }
    if (a < 0 || b < 0) return 2
    return a < b ? a : b
  }

  loadNbModes(mb: int, mbx: int, mby: int) {
    const top = mby > this.sliceRow0
    for (let k = 0; k < 4; k++) {
      this.g4[(k + 1) * 5] = mbx > 0 ? this.nbMode(mb - 1, k * 4 + 3) : -1
      this.g4[k + 1] = top ? this.nbMode(mb - this.mbw, 12 + k) : -1
    }
  }

  predModeG(g: int): int {
    const a = this.g4[g - 1]!
    const b = this.g4[g - 5]!
    return (a | b) < 0 ? I4_DC : a < b ? a : b
  }

  trial4(mb: int, mbx: int, mby: int, av: int, lambda: int, limit: int): int {
    const px = mbx * 16
    const py = mby * 16
    const aw = this.aw
    const ys = this.ys
    const rec = this.cur.y
    const rx = this.yorg + px
    const b3 = lambda * 3
    const b4 = lambda * 4
    let cost4 = lambda * 2
    let cbp = 0
    blocks4(this.srcBlk, this.srcY, py * aw + px, aw)
    for (let i = 0; i < 16 && cost4 < limit; i++) {
      const r = BLK_RASTER[i]!
      const bx = r & 3
      const by = r >> 2
      const bav = this.blkAv[av * 16 + r]!
      edge4m(this.xb, rec, ys, rx + bx * 4, py + by * 4, bav, this.rec4, r)
      const g = r + by + 6
      i4Costs(this.cost9, this.srcBlk, r * 16, this.xb, bav, this.p4, this.predModeG(g), b3)
      const bm = this.cost9[I4_BEST_MODE]!
      this.g4[g] = bm
      this.modes4[r] = bm
      cost4 += this.cost9[I4_BEST_COST]! + b4
      if (cost4 >= limit) break
      cbp |= this.codeBlock4(r * 16, mb * 24 + r, r, u8x16.load(this.p4, bm * 16)) << (i >> 2)
    }
    if (cost4 < limit) this.cbp4 = cbp
    return cost4
  }

  trial8(mb: int, mbx: int, mby: int, av: int, lambda: int, limit: int): int {
    const px = mbx * 16
    const py = mby * 16
    const aw = this.aw
    const ys = this.ys
    const rec = this.cur.y
    const rx = this.yorg + px
    const b3 = lambda * 3
    const b4 = lambda * 4
    let cost8 = lambda * 2
    let cbp = 0
    for (let b8 = 0; b8 < 4 && cost8 < limit; b8++) {
      const bx = b8 & 1
      const by = b8 >> 1
      const bav = this.blkAv[256 + av * 4 + b8]!
      const x = px + bx * 8
      const y = py + by * 8
      edge8m(this.ip8, rec, ys, rx + bx * 8, y, bav, this.rec8, b8)
      i8SrcCosts(this.cost9, this.srcY, y * aw + x, aw, this.ip8, bav)
      const g = by * 10 + bx * 2 + 6
      const pm = this.predModeG(g)
      const avm = modeMask(bav)
      let bm = I4_DC
      let bc = BIG
      if ((avm & 1) !== 0) {
        bc = this.cost9[I4_V]! - (pm === I4_V ? b3 : 0)
        bm = I4_V
      }
      if ((avm & 2) !== 0) {
        const c = this.cost9[I4_H]! - (pm === I4_H ? b3 : 0)
        if (c < bc) {
          bc = c
          bm = I4_H
        }
      }
      const cd = this.cost9[I4_DC]! - (pm === I4_DC ? b3 : 0)
      if (cd < bc) {
        bc = cd
        bm = I4_DC
      }
      const pmBit = pm > I4_DC ? 1 << pm : 0
      let need = avm & (bc <= 0 || this.i8PredOnly ? pmBit : I8_DIR_MODES)
      if (bc <= -b3) need &= ~pmBit
      const so = y * aw + x
      let left = need
      while (left !== 0) {
        const ma = Math.ctz32(left)
        left &= left - 1
        if (left === 0) {
          this.cost9[ma] = i8Cost(this.srcY, so, aw, this.ip8, ma)
        } else {
          i8Cost2(this.cost9, this.srcY, so, aw, this.ip8, ma, Math.ctz32(left))
          left &= left - 1
        }
      }
      if (need !== 0) {
        const c = i32x8.load(this.cost9, I4_DDL) - (i32x8.splat(pm).eq(I8_DIR_LANES) & i32x8.splat(b3))
        const dir = i32x8.select((i32x8.splat(need) & I8_DIR_BITS).eq(i32x8.splat(0)), i32x8.splat(2147483647), c).minIndex()
        if (dir.value < bc) {
          bc = dir.value
          bm = I4_DDL + dir.index
        }
      }
      bc += b4
      this.modes8[b8] = bm
      this.g4[g] = bm
      this.g4[g + 1] = bm
      this.g4[g + 5] = bm
      this.g4[g + 6] = bm
      cost8 += bc
      if (cost8 >= limit) break
      cbp |= this.codeBlock8(b8, x, y, bm) << b8
    }
    if (cost8 < limit) this.cbp8 = cbp
    return cost8
  }

  nearI8(mb: int, av: int): bool {
    if ((av & AV_LEFT) !== 0 && this.mbType[mb - 1]! === MB_I8) return true
    return (av & AV_TOP) !== 0 && this.mbType[mb - this.mbw]! === MB_I8
  }

  decideIntra(mbx: int, mby: int, bound: int): int {
    const mb = mby * this.mbw + mbx
    const px = mbx * 16
    const py = mby * 16
    const aw = this.aw
    const ys = this.ys
    const rec = this.cur.y
    const rx = this.yorg + px
    const av = this.mbAvail(mbx, mby)
    const lambda = LAMBDA[this.mbq]!
    let best16 = I16_DC
    let cost16 = BIG
    i16Satd(this.cost9, this.srcY, py * aw + px, aw, rec, ys, rx, py, av)
    for (let m = 0; m < I16_PLANE; m++) {
      if (!mode16Available(m, av)) continue
      const c = lambda * (m === I16_DC ? 1 : 3) + this.cost9[m]!
      if (c < cost16) {
        cost16 = c
        best16 = m
      }
    }
    if (cost16 > lambda * 3 && mode16Available(I16_PLANE, av)) {
      const c = lambda * 3 + i16PlaneSatd(this.srcY, py * aw + px, aw, rec, ys, rx, py)
      if (c < cost16) {
        cost16 = c
        best16 = I16_PLANE
      }
    }
    cost16 += lambda * 4
    this.best16 = best16
    let limit = cost16 < bound ? cost16 : bound
    const flat16 = this.earlyExit && cost16 < 256 + lambda * 8
    let cost4 = BIG
    let cost8 = BIG
    let done4 = false
    let done8 = false
    if (!flat16) {
      this.loadNbModes(mb, mbx, mby)
      if (this.useI4 && this.useI8 && bound === BIG && this.nearI8(mb, av)) {
        cost8 = this.trial8(mb, mbx, mby, av, lambda, limit)
        done8 = cost8 < limit
        const limit4 = done8 ? cost8 + 1 : limit
        cost4 = this.trial4(mb, mbx, mby, av, lambda, limit4)
        done4 = cost4 < limit4
      } else {
        if (this.useI4) {
          cost4 = this.trial4(mb, mbx, mby, av, lambda, limit)
          if (cost4 < limit) {
            limit = cost4
            done4 = true
          }
        }
        if (this.useI8) {
          cost8 = this.trial8(mb, mbx, mby, av, lambda, limit)
          done8 = cost8 < limit
        }
      }
    }
    this.coded4 = done4 ? mb : -1
    this.coded8 = done8 ? mb : -1
    for (let r = 0; r < 16; r++) this.imode[mb * 16 + r] = 2
    let best = cost16
    this.intraType = MB_I16
    if (cost8 < cost16 && cost8 < cost4) {
      this.intraType = MB_I8
      best = cost8
    } else if (cost4 < cost16) {
      this.intraType = MB_I4
      best = cost4
    }
    return best
  }

  commitIntra(mb: int, mbx: int, mby: int) {
    const t = this.intraType
    this.mbType[mb] = t
    this.refIdx[mb] = -1
    this.mvx[mb] = 0
    this.mvy[mb] = 0
    this.mvdx[mb] = 0
    this.mvdy[mb] = 0
    if (t === MB_I4) {
      for (let r = 0; r < 16; r++) this.imode[mb * 16 + r] = this.modes4[r]!
    } else if (t === MB_I8) {
      for (let b8 = 0; b8 < 4; b8++) {
        const r0 = (b8 >> 1) * 8 + (b8 & 1) * 2
        const m = this.modes8[b8]!
        this.imode[mb * 16 + r0] = m
        this.imode[mb * 16 + r0 + 1] = m
        this.imode[mb * 16 + r0 + 4] = m
        this.imode[mb * 16 + r0 + 5] = m
      }
    } else {
      this.i16Mode[mb] = this.best16
      for (let r = 0; r < 16; r++) this.imode[mb * 16 + r] = 2
    }
    const av = this.mbAvail(mbx, mby)
    const cx = mbx * 8
    const cy = mby * 8
    const cw = this.cw
    const lambda = LAMBDA[this.mbq]!
    let bestC = 0
    let costC = BIG
    const rcx = this.corg + cx
    chromaSatd(this.cost9, this.srcU, this.srcV, cy * cw + cx, cw, this.cur.u, this.cur.v, this.cs, rcx, cy, av, this.uvBlk)
    this.uvMb = mb
    for (let m = 0; m < C_PLANE; m++) {
      if (!chromaModeAvailable(m, av)) continue
      const c = lambda * (m === 0 ? 1 : 3) + this.cost9[m]!
      if (c < costC) {
        costC = c
        bestC = m
      }
    }
    if (costC > lambda * 3 && chromaModeAvailable(C_PLANE, av)) {
      const c = lambda * 3 + chromaPlaneSatd(this.uvBlk, this.cur.u, this.cur.v, this.cs, rcx, cy, this.uvPlane)
      if (c < costC) bestC = C_PLANE
    }
    this.cMode[mb] = bestC
  }

  codeMb(mbx: int, mby: int) {
    const mb = mby * this.mbw + mbx
    const t = this.mbType[mb]!
    const done4 = this.coded4 === mb
    const done8 = this.coded8 === mb
    const uvOk = this.uvMb === mb
    this.coded4 = -1
    this.coded8 = -1
    this.uvMb = -1
    this.dcCbf[mb] = 0
    this.t8[mb] = 0
    if (t === MB_PSKIP || t === MB_BSKIP) {
      if (t === MB_PSKIP) {
        this.copySkip(mbx, mby)
      } else {
        this.interPred(mb, mbx, mby)
        this.storePred(mbx, mby)
      }
      this.cbp[mb] = 0
      for (let k = 0; k < 24; k++) this.nnz[mb * 24 + k] = 0
      return
    }
    if (t === MB_P16 || t >= MB_BDIRECT) {
      this.codeInter(mb, mbx, mby)
      return
    }
    let cbpL = 0
    if (t === MB_I16) {
      cbpL = this.codeLuma16(mb, mbx, mby)
    } else if (t === MB_I4) {
      if (done4) {
        unblocks4(this.cur.y, this.ry(mbx * 16, mby * 16), this.ys, this.rec4)
        cbpL = this.cbp4
      } else {
        cbpL = this.codeLuma4(mb, mbx, mby)
      }
    } else {
      if (done8) {
        unblocks8(this.cur.y, this.ry(mbx * 16, mby * 16), this.ys, this.rec8)
        this.finishLuma8(mb)
        cbpL = this.cbp8
      } else {
        cbpL = this.codeLuma8(mb, mbx, mby)
      }
      this.t8[mb] = 1
    }
    const cbpC = this.codeChromaIntra(mb, mbx, mby, uvOk)
    this.cbp[mb] = cbpL | (cbpC << 4)
  }

  predictInter(mb: int, mbx: int, mby: int, mx: int, my: int, ref: int) {
    const px = mbx * 16
    const py = mby * 16
    const rf = this.refs[ref]!
    const ix = mx >> 2
    const iy = my >> 2
    this.ensureTiles(rf, px + ix - 1, py + iy - 1, 19, 19)
    this.predictQpel(this.pred, 0, 16, rf, px, py, mx, my, 16, 16)
    const base = this.rc(mbx * 8, mby * 8)
    predictChroma(this.pred, 256, 8, rf.u, this.cs, base, mx, my, 8, 8)
    predictChroma(this.pred, 320, 8, rf.v, this.cs, base, mx, my, 8, 8)
    this.weightChroma(rf, 256)
  }

  copySkip(mbx: int, mby: int) {
    const mb = mby * this.mbw + mbx
    this.predictInter(mb, mbx, mby, this.skipX, this.skipY, 0)
    this.storePred(mbx, mby)
  }

  storePred(mbx: int, mby: int) {
    const cur = this.cur
    copyRect(cur.y, this.ry(mbx * 16, mby * 16), this.ys, this.pred, 0, 16, 16, 16)
    copy8(cur.u, this.rc(mbx * 8, mby * 8), this.cs, this.pred, 256, 8)
    copy8(cur.v, this.rc(mbx * 8, mby * 8), this.cs, this.pred, 320, 8)
  }

  codeInter(mb: int, mbx: int, mby: int) {
    const px = mbx * 16
    const py = mby * 16
    const aw = this.aw
    const qp = this.mbq
    const mx = this.mvx[mb]!
    const my = this.mvy[mb]!
    if (this.predReady) {
      this.predReady = false
    } else {
      this.interPred(mb, mbx, mby)
    }
    let use8 = false
    if (this.useI8 && this.inter8x8) {
      const s4 = satd16(this.srcY, py * aw + px, aw, this.pred, 0, 16)
      const s8 = sa8dPair(this.srcY, py * aw + px, aw, this.pred, 0, 16) +
        sa8dPair(this.srcY, (py + 8) * aw + px, aw, this.pred, 128, 16)
      use8 = s8 * 2 < s4 * 2 - (s4 >> 4)
    }
    let cbpL = 0
    let mbScore = 0
    const cabac = this.params.cabac
    if (use8) {
      for (let b8 = 0; b8 < 4; b8++) {
        const bx = (b8 & 1) * 8
        const by = (b8 >> 1) * 8
        fwd8(this.srcY, (py + by) * aw + px + bx, aw, this.pred, by * 16 + bx, 16, this.lv32, b8 * 64)
        const nz = quant8v(this.lv32, b8 * 64, qp, false)
        const zo = b8 * 64
        zigzag8(this.lv32, b8 * 64, this.coef, zo)
        let sc = 0
        if (nz > 0) sc = this.decimate ? decimateScore(this.coef, zo, 64, true) : 9
        if (sc < 4) {
          for (let k = 0; k < 64; k += 16) i16x16.splat(0).store(this.coef, zo + k)
        } else {
          cbpL |= 1 << b8
        }
        mbScore += sc
      }
    } else {
      for (let b8 = 0; b8 < 4; b8++) {
        let sc8 = 0
        for (let s = 0; s < 4; s++) {
          const r = BLK_RASTER[b8 * 4 + s]!
          if ((s & 1) === 0) {
            const bx = (r & 3) * 4
            const by = (r >> 2) * 4
            fwd4x2(this.srcY, (py + by) * aw + px + bx, aw, this.pred, by * 16 + bx, 16, this.lv32, r * 16)
          }
          const nz = quant4v(this.lv32, r * 16, qp, false, 0)
          this.nz4[r] = nz
          if (nz > 0) {
            zigzag4(this.lv32, r * 16, this.coef, r * 16)
            sc8 += this.decimate ? decimateScore(this.coef, r * 16, 16, false) : 9
          } else if (!cabac) {
            i16x16.splat(0).store(this.coef, r * 16)
          }
        }
        if (sc8 < 4) {
          for (let s = 0; s < 4; s++) {
            const r = BLK_RASTER[b8 * 4 + s]!
            this.nz4[r] = 0
            if (!cabac) {
              i16x16.splat(0).store(this.coef, r * 16)
            }
          }
        } else {
          cbpL |= 1 << b8
        }
        mbScore += sc8
      }
    }
    if (this.decimate && mbScore < 6 && cbpL !== 0) {
      cbpL = 0
      for (let r = 0; r < 16; r++) this.nz4[r] = 0
      if (!cabac || use8) {
        for (let k = 0; k < 256; k += 16) i16x16.splat(0).store(this.coef, k)
      }
    }
    this.t8[mb] = use8 && cbpL !== 0 ? 1 : 0
    copyRect(this.cur.y, this.ry(px, py), this.ys, this.pred, 0, 16, 16, 16)
    if (use8) {
      for (let b8 = 0; b8 < 4; b8++) {
        const bx = (b8 & 1) * 8
        const by = (b8 >> 1) * 8
        let nz = 0
        for (let k = 0; k < 64; k++) {
          if (this.coef[b8 * 64 + k]! !== 0) nz++
        }
        for (let s = 0; s < 4; s++) {
          let c = 0
          if (this.params.cabac) {
            c = nz
          } else {
            for (let k = s; k < 64; k += 4) {
              if (this.coef[b8 * 64 + k]! !== 0) c++
            }
          }
          this.nnz[mb * 24 + BLK_RASTER[b8 * 4 + s]!] = c
        }
        if ((cbpL >> b8 & 1) !== 0) {
          dequant8v(this.lv32, b8 * 64, this.lv32, b8 * 64, qp)
          idct8v(this.lv32, b8 * 64)
          recon8(this.cur.y, this.ry(px + bx, py + by), this.ys, this.lv32, b8 * 64, this.pred, by * 16 + bx, 16)
        }
      }
    } else {
      for (let r = 0; r < 16; r++) {
        const bx = (r & 3) * 4
        const by = (r >> 2) * 4
        const nz = this.nz4[r]!
        this.nnz[mb * 24 + r] = nz
        if (nz > 0) {
          dequant4v(this.lv32, r * 16, qp)
          idct4v(this.lv32, r * 16)
          recon4(this.cur.y, this.ry(px + bx, py + by), this.ys, this.lv32, r * 16, this.pred, by * 16 + bx, 16)
        }
      }
    }
    const cbpC = this.codeChromaResidual(mb, mbx, mby, false)
    this.cbp[mb] = cbpL | (cbpC << 4)
    if (this.sliceType === SLICE_B) {
      if (this.mbType[mb]! === MB_BDIRECT && this.cbp[mb]! === 0) {
        this.mbType[mb] = MB_BSKIP
        this.t8[mb] = 0
        for (let k = 0; k < 24; k++) this.nnz[mb * 24 + k] = 0
      }
      return
    }
    this.computeMvp(mbx, mby, this.refIdx[mb]!)
    this.mvdx[mb] = mx - this.mvpX
    this.mvdy[mb] = my - this.mvpY
    if (this.cbp[mb]! === 0 && this.refIdx[mb]! === 0 && mx === this.skipX && my === this.skipY) {
      this.setSkip(mb)
      this.t8[mb] = 0
      for (let k = 0; k < 24; k++) this.nnz[mb * 24 + k] = 0
    }
  }

  codeLuma16(mb: int, mbx: int, mby: int): int {
    const px = mbx * 16
    const py = mby * 16
    const aw = this.aw
    const qp = this.mbq
    const av = this.mbAvail(mbx, mby)
    pred16v(this.pred, 0, 16, this.cur.y, this.ys, this.yorg + px, py, this.i16Mode[mb]!, av)
    for (let b = 0; b < 16; b += 2) {
      const bx = (b & 3) * 4
      const by = (b >> 2) * 4
      fwd4x2(this.srcY, (py + by) * aw + px + bx, aw, this.pred, by * 16 + bx, 16, this.lv32, b * 16)
      this.dc32[b] = this.lv32[b * 16]!
      this.dc32[b + 1] = this.lv32[b * 16 + 16]!
    }
    hadamard4v(this.dc32, 0, true)
    const dcNz = quantDcv(this.dc32, 0, 16, qp, true)
    this.dcCbf[mb] = dcNz > 0 ? 1 : 0
    zigzag4(this.dc32, 0, this.coef, DC_OFF)
    let acNz = 0
    for (let b = 0; b < 16; b++) {
      const nz = quant4v(this.lv32, b * 16, qp, true, 1)
      this.nnz[mb * 24 + b] = nz
      acNz += nz
      zigzagAc4(this.lv32, b * 16, this.coef, b * 16)
    }
    const ro = this.ry(px, py)
    if (acNz === 0 && dcNz === 0) {
      copy16(this.cur.y, ro, this.ys, this.pred, 0)
      return 0
    }
    hadamard4v(this.dc32, 0, false)
    dequantLumaDcv(this.dc32, 0, qp)
    if (acNz === 0) {
      reconDc16(this.cur.y, ro, this.ys, this.dc32, this.pred, 0)
      return 0
    }
    for (let b = 0; b < 16; b++) {
      if (this.nnz[mb * 24 + b]! === 0) {
        const k = i32x4.splat((this.dc32[b]! + 32) >> 6)
        k.store(this.lv32, b * 16)
        k.store(this.lv32, b * 16 + 4)
        k.store(this.lv32, b * 16 + 8)
        k.store(this.lv32, b * 16 + 12)
      } else {
        dequant4v(this.lv32, b * 16, qp)
        this.lv32[b * 16] = this.dc32[b]!
        idct4v(this.lv32, b * 16)
      }
    }
    recon16(this.cur.y, ro, this.ys, this.lv32, this.pred, 0)
    return 15
  }

  codeLuma4(mb: int, mbx: int, mby: int): int {
    const px = mbx * 16
    const py = mby * 16
    const av = this.mbAvail(mbx, mby)
    let cbpL = 0
    blocks4(this.srcBlk, this.srcY, py * this.aw + px, this.aw)
    for (let i = 0; i < 16; i++) {
      const r = BLK_RASTER[i]!
      const bav = this.blkAv[av * 16 + r]!
      edge4m(this.xb, this.cur.y, this.ys, this.yorg + px + (r & 3) * 4, py + (r >> 2) * 4, bav, this.rec4, r)
      cbpL |= this.codeBlock4(r * 16, mb * 24 + r, r, pred4v(this.xb, this.imode[mb * 16 + r]!, bav)) << (i >> 2)
    }
    unblocks4(this.cur.y, this.ry(px, py), this.ys, this.rec4)
    return cbpL
  }

  codeBlock4(zo: int, no: int, r: int, pred: u8x16): int {
    const nz = code4(u8x16.load(this.srcBlk, r * 16), pred, this.mbq, true, this.coef, zo, this.rec4, r * 16)
    this.nnz[no] = nz
    return nz > 0 ? 1 : 0
  }

  codeLuma8(mb: int, mbx: int, mby: int): int {
    const px = mbx * 16
    const py = mby * 16
    const av = this.mbAvail(mbx, mby)
    let cbpL = 0
    for (let b8 = 0; b8 < 4; b8++) {
      const bx = b8 & 1
      const by = b8 >> 1
      const bav = this.blkAv[256 + av * 4 + b8]!
      const x = px + bx * 8
      const y = py + by * 8
      edge8m(this.ip8, this.cur.y, this.ys, this.yorg + x, y, bav, this.rec8, b8)
      const mode = this.imode[mb * 16 + by * 8 + bx * 2]!
      cbpL |= this.codeBlock8(b8, x, y, mode) << b8
    }
    unblocks8(this.cur.y, this.ry(px, py), this.ys, this.rec8)
    this.finishLuma8(mb)
    return cbpL
  }

  codeBlock8(b8: int, x: int, y: int, mode: int): int {
    const qp = this.mbq
    const lo = b8 * 64
    pred8v(this.pred, 0, this.ip8, mode)
    fwd8(this.srcY, y * this.aw + x, this.aw, this.pred, 0, 8, this.lv8, lo)
    const nz = quant8v(this.lv8, lo, qp, true)
    this.nz8[b8] = nz
    if (nz > 0) {
      dequant8v(this.lv8, lo, this.lv32, 0, qp)
      idct8v(this.lv32, 0)
      recon8b(this.rec8, lo, this.lv32, 0, this.pred, 0)
      return 1
    }
    for (let k = 0; k < 64; k += 16) u8x16.load(this.pred, k).store(this.rec8, lo + k)
    return 0
  }

  finishLuma8(mb: int) {
    for (let b8 = 0; b8 < 4; b8++) {
      const zo = b8 * 64
      const nz = this.nz8[b8]!
      if (nz === 0) {
        const z = i16x16.splat(0)
        for (let k = 0; k < 64; k += 16) z.store(this.coef, zo + k)
      } else {
        zigzag8(this.lv8, b8 * 64, this.coef, zo)
      }
      for (let s = 0; s < 4; s++) {
        let n = 0
        if (this.params.cabac) {
          n = nz
        } else if (nz > 0) {
          for (let k = s; k < 64; k += 4) {
            if (this.coef[zo + k]! !== 0) n++
          }
        }
        this.nnz[mb * 24 + BLK_RASTER[b8 * 4 + s]!] = n
      }
    }
  }

  codeChromaIntra(mb: int, mbx: int, mby: int, uvOk: bool): int {
    const mode = this.cMode[mb]!
    if (uvOk) {
      predChromaUv(this.pred, 256, this.uvBlk, this.uvPlane, mode)
    } else {
      const cx = mbx * 8
      const cy = mby * 8
      const av = this.mbAvail(mbx, mby)
      predChromaV(this.pred, 256, 8, this.cur.u, this.cs, this.corg + cx, cy, mode, av)
      predChromaV(this.pred, 320, 8, this.cur.v, this.cs, this.corg + cx, cy, mode, av)
    }
    return this.codeChromaResidual(mb, mbx, mby, true)
  }

  codeChromaResidual(mb: int, mbx: int, mby: int, intra: bool): int {
    const qpc = int(CHROMA_QP[this.mbq]!)
    const cx = mbx * 8
    const cy = mby * 8
    const cw = this.cw
    let chromaAc = 0
    let chromaDc = 0
    let acU = 0
    for (let pl = 0; pl < 2; pl++) {
      if (pl === 1) acU = chromaAc
      const po = 256 + pl * 64
      const cb = pl * 64
      for (let b = 0; b < 4; b += 2) {
        const by = (b >> 1) * 4
        const so = (cy + by) * cw + cx
        if (pl === 0) {
          fwd4x2(this.srcU, so, cw, this.pred, po + by * 8, 8, this.c32, cb + b * 16)
        } else {
          fwd4x2(this.srcV, so, cw, this.pred, po + by * 8, 8, this.c32, cb + b * 16)
        }
        this.dc32[pl * 4 + b] = this.c32[cb + b * 16]!
        this.dc32[pl * 4 + b + 1] = this.c32[cb + b * 16 + 16]!
      }
      hadamard2v(this.dc32, pl * 4)
      const dn = quantDcv(this.dc32, pl * 4, 4, qpc, intra)
      chromaDc += dn
      if (dn > 0) this.dcCbf[mb] = this.dcCbf[mb]! | (2 << pl)
      for (let k = 0; k < 4; k++) this.coef[CHROMA_DC_OFF + pl * 4 + k] = this.dc32[pl * 4 + k]!
      const acZero = chromaAcZero(this.c32, cb, qpc, intra)
      for (let b = 0; b < 4; b++) {
        const zo = CHROMA_AC_OFF + (pl * 4 + b) * 16
        const nz = acZero ? 0 : quant4v(this.c32, cb + b * 16, qpc, intra, 1)
        this.nnz[mb * 24 + 16 + pl * 4 + b] = nz
        chromaAc += nz
        if (nz === 0) {
          i16x16.splat(0).store(this.coef, zo)
        } else {
          zigzagAc4(this.c32, cb + b * 16, this.coef, zo)
        }
      }
    }
    if (!intra && this.decimate && chromaAc > 0) {
      let sc = 0
      for (let pl = 0; pl < 2 && sc < 7; pl++) {
        for (let b = 0; b < 4 && sc < 7; b++) {
          sc += decimateScore(this.coef, CHROMA_AC_OFF + (pl * 4 + b) * 16, 15, false)
        }
      }
      if (sc < 7) {
        chromaAc = 0
        for (let b = 0; b < 8; b++) this.nnz[mb * 24 + 16 + b] = 0
        for (let k = 0; k < 128; k += 16) i16x16.splat(0).store(this.coef, CHROMA_AC_OFF + k)
      }
    }
    const cbpC = chromaAc > 0 ? 2 : chromaDc > 0 ? 1 : 0
    const ro = this.rc(cx, cy)
    for (let pl = 0; pl < 2; pl++) {
      const po = 256 + pl * 64
      const cb = pl * 64
      if (cbpC === 0) {
        if (pl === 0) {
          copy8(this.cur.u, ro, this.cs, this.pred, po, 8)
        } else {
          copy8(this.cur.v, ro, this.cs, this.pred, po, 8)
        }
        continue
      }
      hadamard2v(this.dc32, pl * 4)
      dequantChromaDcv(this.dc32, pl * 4, qpc)
      if (cbpC === 1 || (pl === 0 ? acU : chromaAc - acU) === 0) {
        if (pl === 0) {
          reconDcC8(this.cur.u, ro, this.cs, this.dc32, 0, this.pred, po)
        } else {
          reconDcC8(this.cur.v, ro, this.cs, this.dc32, 4, this.pred, po)
        }
        continue
      }
      for (let b = 0; b < 4; b++) {
        const o = cb + b * 16
        if (cbpC === 2) {
          dequant4v(this.c32, o, qpc)
        } else {
          for (let k = 0; k < 16; k += 4) i32x4.splat(0).store(this.c32, o + k)
        }
        this.c32[o] = this.dc32[pl * 4 + b]!
      }
      idctC8(this.c32, cb, pl === 0 ? this.cur.u : this.cur.v, ro, this.cs, this.pred, po)
    }
    if (cbpC !== 2) {
      for (let k = 16; k < 24; k++) this.nnz[mb * 24 + k] = 0
    }
    return cbpC
  }

  lumaNc(mb: int, mbx: int, mby: int, b: int): int {
    const bx = b & 3
    const by = b >> 2
    let na = -1
    let nb = -1
    if (bx > 0) {
      na = this.nnz[mb * 24 + b - 1]!
    } else if (mbx > 0) {
      na = this.nnz[(mb - 1) * 24 + b + 3]!
    }
    if (by > 0) {
      nb = this.nnz[mb * 24 + b - 4]!
    } else if (mby > this.sliceRow0) {
      nb = this.nnz[(mb - this.mbw) * 24 + b + 12]!
    }
    if (na >= 0 && nb >= 0) return (na + nb + 1) >> 1
    if (na >= 0) return na
    if (nb >= 0) return nb
    return 0
  }

  chromaNc(mb: int, mbx: int, mby: int, pl: int, b: int): int {
    const bx = b & 1
    const by = b >> 1
    const base = 16 + pl * 4
    let na = -1
    let nb = -1
    if (bx > 0) {
      na = this.nnz[mb * 24 + base + b - 1]!
    } else if (mbx > 0) {
      na = this.nnz[(mb - 1) * 24 + base + b + 1]!
    }
    if (by > 0) {
      nb = this.nnz[mb * 24 + base + b - 2]!
    } else if (mby > this.sliceRow0) {
      nb = this.nnz[(mb - this.mbw) * 24 + base + b + 2]!
    }
    if (na >= 0 && nb >= 0) return (na + nb + 1) >> 1
    if (na >= 0) return na
    if (nb >= 0) return nb
    return 0
  }

  writeMbCavlc(mbx: int, mby: int) {
    const bw = this.bw
    const mb = mby * this.mbw + mbx
    const t = this.mbType[mb]!
    if (t === MB_PSKIP || t === MB_BSKIP) {
      this.skipRun++
      return
    }
    if (this.sliceType !== SLICE_I) {
      bw.ue(this.skipRun)
      this.skipRun = 0
    }
    const cbp = this.cbp[mb]!
    if (t === MB_I16 || cbp !== 0) this.loadCoef(mb, mby, t, cbp)
    const cbpL = cbp & 15
    const cbpC = cbp >> 4
    const off = this.sliceType === SLICE_P ? 5 : this.sliceType === SLICE_B ? 23 : 0
    if (t >= MB_BDIRECT) {
      bw.ue(t === MB_BDIRECT ? 0 : t === MB_BL0 ? 1 : t === MB_BL1 ? 2 : 3)
      if (t === MB_BL0 || t === MB_BBI) {
        bw.se(this.mvdx[mb]!)
        bw.se(this.mvdy[mb]!)
      }
      if (t === MB_BL1 || t === MB_BBI) {
        bw.se(this.mvdx1[mb]!)
        bw.se(this.mvdy1[mb]!)
      }
      bw.ue(int(CBP_CODE_INTER[cbp]!))
      if (cbpL !== 0 && this.params.transform8x8) bw.putBit(this.t8[mb]!)
      if (cbp !== 0) bw.se(this.qpDelta[mb]!)
      this.writeLumaResidualCavlc(mb, mbx, mby, cbpL, this.t8[mb]! === 1)
    } else if (t === MB_P16) {
      bw.ue(0)
      if (this.refsActive === 2) {
        bw.putBit(this.refIdx[mb]! === 0 ? 1 : 0)
      } else if (this.refsActive > 2) {
        bw.ue(this.refIdx[mb]!)
      }
      bw.se(this.mvdx[mb]!)
      bw.se(this.mvdy[mb]!)
      bw.ue(int(CBP_CODE_INTER[cbp]!))
      if (cbpL !== 0 && this.params.transform8x8) bw.putBit(this.t8[mb]!)
      if (cbp !== 0) bw.se(this.qpDelta[mb]!)
      this.writeLumaResidualCavlc(mb, mbx, mby, cbpL, this.t8[mb]! === 1)
    } else if (t === MB_I16) {
      bw.ue(off + 1 + this.i16Mode[mb]! + 4 * cbpC + (cbpL !== 0 ? 12 : 0))
      bw.ue(this.cMode[mb]!)
      bw.se(this.qpDelta[mb]!)
      writeResidual(bw, this.coef, DC_OFF, 16, this.lumaNc(mb, mbx, mby, 0))
      if (cbpL !== 0) {
        for (let i = 0; i < 16; i++) {
          const b = BLK_RASTER[i]!
          writeResidual(bw, this.coef, b * 16, 15, this.lumaNc(mb, mbx, mby, b))
        }
      } else {
        for (let b = 0; b < 16; b++) this.nnz[mb * 24 + b] = 0
      }
    } else {
      bw.ue(off)
      if (this.params.transform8x8) bw.putBit(t === MB_I8 ? 1 : 0)
      if (t === MB_I4) {
        for (let i = 0; i < 16; i++) {
          const r = BLK_RASTER[i]!
          this.writeModeFlag(this.imode[mb * 16 + r]!, this.predMode4(mb, mbx, mby, r & 3, r >> 2))
        }
      } else {
        for (let b8 = 0; b8 < 4; b8++) {
          const bx = (b8 & 1) * 2
          const by = (b8 >> 1) * 2
          this.writeModeFlag(this.imode[mb * 16 + by * 4 + bx]!, this.predMode4(mb, mbx, mby, bx, by))
        }
      }
      bw.ue(this.cMode[mb]!)
      bw.ue(int(CBP_CODE_INTRA[cbp]!))
      if (cbp !== 0) bw.se(this.qpDelta[mb]!)
      this.writeLumaResidualCavlc(mb, mbx, mby, cbpL, t === MB_I8)
    }
    if (cbpC !== 0) {
      writeResidual(bw, this.coef, CHROMA_DC_OFF, 4, -1)
      writeResidual(bw, this.coef, CHROMA_DC_OFF + 4, 4, -1)
    }
    if (cbpC === 2) {
      for (let pl = 0; pl < 2; pl++) {
        for (let b = 0; b < 4; b++) {
          writeResidual(bw, this.coef, CHROMA_AC_OFF + (pl * 4 + b) * 16, 15, this.chromaNc(mb, mbx, mby, pl, b))
        }
      }
    }
  }

  writeLumaResidualCavlc(mb: int, mbx: int, mby: int, cbpL: int, is8: bool) {
    const bw = this.bw
    for (let b8 = 0; b8 < 4; b8++) {
      if ((cbpL & (1 << b8)) === 0) {
        // Entropy rows run while the deblocking wavefront reads nnz; coding already zeroed these, so don't store.
        for (let s = 0; s < 4; s++) {
          const k = mb * 24 + BLK_RASTER[b8 * 4 + s]!
          if (this.nnz[k]! !== 0) this.nnz[k] = 0
        }
        continue
      }
      for (let s = 0; s < 4; s++) {
        const r = BLK_RASTER[b8 * 4 + s]!
        if (is8) {
          for (let k = 0; k < 16; k++) this.blk16[k] = this.coef[b8 * 64 + 4 * k + s]!
          writeResidual(bw, this.blk16, 0, 16, this.lumaNc(mb, mbx, mby, r))
        } else {
          writeResidual(bw, this.coef, r * 16, 16, this.lumaNc(mb, mbx, mby, r))
        }
      }
    }
  }

  leftMb(mb: int, mbx: int): int {
    return mbx > 0 ? mb - 1 : -1
  }

  topMb(mb: int, mby: int): int {
    return mby > this.sliceRow0 ? mb - this.mbw : -1
  }

  cbfLuma4(mb: int, mbx: int, mby: int, r: int, intraCur: bool): int {
    const bx = r & 3
    const by = r >> 2
    let a = 0
    let b = 0
    if (bx > 0) {
      a = this.lumaBlkCoded(mb, r - 1)
    } else if (mbx > 0) {
      a = this.lumaBlkCoded(mb - 1, r + 3)
    } else {
      a = intraCur ? 1 : 0
    }
    if (by > 0) {
      b = this.lumaBlkCoded(mb, r - 4)
    } else if (mby > this.sliceRow0) {
      b = this.lumaBlkCoded(mb - this.mbw, r + 12)
    } else {
      b = intraCur ? 1 : 0
    }
    return a + 2 * b
  }

  lumaBlkCoded(mb: int, r: int): int {
    if (this.mbType[mb]! === MB_PSKIP || this.mbType[mb]! === MB_BSKIP) return 0
    const b8 = ((r >> 3) << 1) | ((r & 3) >> 1)
    if (((this.cbp[mb]! & 15) >> b8 & 1) === 0) return 0
    if (this.t8[mb]! === 1) return 1
    return this.nnz[mb * 24 + r]! > 0 ? 1 : 0
  }

  cbfChroma4(mb: int, mbx: int, mby: int, pl: int, b: int, intraCur: bool): int {
    const bx = b & 1
    const by = b >> 1
    const base = 16 + pl * 4
    let a = 0
    let c = 0
    if (bx > 0) {
      a = this.nnz[mb * 24 + base + b - 1]! > 0 ? 1 : 0
    } else if (mbx > 0) {
      a = (this.cbp[mb - 1]! >> 4) === 2 && this.nnz[(mb - 1) * 24 + base + b + 1]! > 0 ? 1 : 0
    } else {
      a = intraCur ? 1 : 0
    }
    if (by > 0) {
      c = this.nnz[mb * 24 + base + b - 2]! > 0 ? 1 : 0
    } else if (mby > this.sliceRow0) {
      c = (this.cbp[mb - this.mbw]! >> 4) === 2 && this.nnz[(mb - this.mbw) * 24 + base + b + 2]! > 0 ? 1 : 0
    } else {
      c = intraCur ? 1 : 0
    }
    return a + 2 * c
  }

  cbfDc(mb: int, mbx: int, mby: int, bit: int, intraCur: bool): int {
    const l = this.leftMb(mb, mbx)
    const t = this.topMb(mb, mby)
    let a = intraCur ? 1 : 0
    let b = intraCur ? 1 : 0
    if (l >= 0) a = this.dcCoded(l, bit)
    if (t >= 0) b = this.dcCoded(t, bit)
    return a + 2 * b
  }

  dcCoded(mb: int, bit: int): int {
    if (this.mbType[mb]! === MB_PSKIP || this.mbType[mb]! === MB_BSKIP) return 0
    if (bit === 0 && this.mbType[mb]! !== MB_I16) return 0
    if (bit > 0 && (this.cbp[mb]! >> 4) === 0) return 0
    return ((this.dcCbf[mb]! >> bit) & 1)
  }

  writeMvdCabac(base: int, v: int, sumAbs: int) {
    const c = this.cabac
    const inc0 = sumAbs < 3 ? 0 : sumAbs > 32 ? 2 : 1
    const a = v < 0 ? -v : v
    const pre = a < 9 ? a : 9
    for (let k = 0; k < pre; k++) {
      const inc = k === 0 ? inc0 : k === 1 ? 3 : k === 2 ? 4 : k === 3 ? 5 : 6
      c.decision(base + inc, 1)
    }
    if (a < 9) {
      const inc = pre === 0 ? inc0 : pre === 1 ? 3 : pre === 2 ? 4 : pre === 3 ? 5 : 6
      c.decision(base + inc, 0)
    } else {
      const k = 63 - Math.clz64(a - 1)
      c.bypassBits((((1 << (k - 3)) - 1) << (k + 2)) | ((a - 1 - (1 << k)) << 1) | (v < 0 ? 1 : 0), 2 * k - 1)
      return
    }
    if (a !== 0) c.bypass(v < 0 ? 1 : 0)
  }

  writeRowCabac(mby: int, lastRow: bool) {
    const c = this.cabac
    const mbw = this.mbw
    const types = this.mbType
    const skip = this.sliceType === SLICE_P ? MB_PSKIP : this.sliceType === SLICE_B ? MB_BSKIP : 0
    const ctxIdx = this.sliceType === SLICE_P ? 11 : 24
    const row = mby * mbw
    const end = skip === 0 ? 0 : lastRow ? mbw - 1 : mbw
    let mbx = 0
    while (mbx < mbw) {
      if (mbx < end && types[row + mbx]! === skip) {
        const x0 = mbx
        mbx++
        while (mbx < end && types[row + mbx]! === skip) mbx++
        const left = x0 > 0 && types[row + x0 - 1]! !== skip ? 1 : 0
        c.skipRun(types, mby > this.sliceRow0 ? row - mbw + x0 : -1, mbx - x0, left, skip, ctxIdx)
        this.lastDeltaNz = false
      } else {
        this.writeMbCabac(mbx, mby)
        c.terminate(lastRow && mbx === mbw - 1 ? 1 : 0)
        mbx++
      }
    }
  }

  writeMbCabac(mbx: int, mby: int) {
    const c = this.cabac
    c.room()
    const mb = mby * this.mbw + mbx
    const t = this.mbType[mb]!
    const l = this.leftMb(mb, mbx)
    const tp = this.topMb(mb, mby)
    const pslice = this.sliceType === SLICE_P
    const bslice = this.sliceType === SLICE_B
    if (pslice) {
      const sa = l >= 0 && this.mbType[l]! !== MB_PSKIP ? 1 : 0
      const sb = tp >= 0 && this.mbType[tp]! !== MB_PSKIP ? 1 : 0
      c.decision(11 + sa + sb, t === MB_PSKIP ? 1 : 0)
      if (t === MB_PSKIP) {
        this.lastDeltaNz = false
        return
      }
    } else if (bslice) {
      const sa = l >= 0 && this.mbType[l]! !== MB_BSKIP ? 1 : 0
      const sb = tp >= 0 && this.mbType[tp]! !== MB_BSKIP ? 1 : 0
      c.decision(24 + sa + sb, t === MB_BSKIP ? 1 : 0)
      if (t === MB_BSKIP) {
        this.lastDeltaNz = false
        return
      }
    }
    const cbp = this.cbp[mb]!
    if (t === MB_I16 || cbp !== 0) this.loadCoef(mb, mby, t, cbp)
    const cbpL = cbp & 15
    const cbpC = cbp >> 4
    const intra = t <= MB_I8
    if (bslice) {
      const ca = l >= 0 && this.mbType[l]! !== MB_BSKIP && this.mbType[l]! !== MB_BDIRECT ? 1 : 0
      const cb = tp >= 0 && this.mbType[tp]! !== MB_BSKIP && this.mbType[tp]! !== MB_BDIRECT ? 1 : 0
      if (t === MB_BDIRECT) {
        c.decision(27 + ca + cb, 0)
      } else {
        c.decision(27 + ca + cb, 1)
        if (t === MB_BL0 || t === MB_BL1) {
          c.decision(30, 0)
          c.decision(32, t === MB_BL1 ? 1 : 0)
        } else if (t === MB_BBI) {
          c.decision(30, 1)
          c.decision(31, 0)
          c.decision(32, 0)
          c.decision(32, 0)
          c.decision(32, 0)
        } else {
          c.decision(30, 1)
          c.decision(31, 1)
          c.decision(32, 1)
          c.decision(32, 0)
          c.decision(32, 1)
        }
      }
      if (t === MB_BL0 || t === MB_BBI) {
        const ax = l >= 0 ? iabs(this.mvdx[l]!) : 0
        const bx = tp >= 0 ? iabs(this.mvdx[tp]!) : 0
        this.writeMvdCabac(40, this.mvdx[mb]!, ax + bx)
        const ay = l >= 0 ? iabs(this.mvdy[l]!) : 0
        const by = tp >= 0 ? iabs(this.mvdy[tp]!) : 0
        this.writeMvdCabac(47, this.mvdy[mb]!, ay + by)
      }
      if (t === MB_BL1 || t === MB_BBI) {
        const ax = l >= 0 ? iabs(this.mvdx1[l]!) : 0
        const bx = tp >= 0 ? iabs(this.mvdx1[tp]!) : 0
        this.writeMvdCabac(40, this.mvdx1[mb]!, ax + bx)
        const ay = l >= 0 ? iabs(this.mvdy1[l]!) : 0
        const by = tp >= 0 ? iabs(this.mvdy1[tp]!) : 0
        this.writeMvdCabac(47, this.mvdy1[mb]!, ay + by)
      }
    }
    if (t === MB_P16) {
      c.decision(14, 0)
      c.decision(15, 0)
      c.decision(16, 0)
      if (this.refsActive > 1) {
        const ra = l >= 0 && !this.isIntra(l) && this.mbType[l]! !== MB_PSKIP && this.refIdx[l]! > 0 ? 1 : 0
        const rb = tp >= 0 && !this.isIntra(tp) && this.mbType[tp]! !== MB_PSKIP && this.refIdx[tp]! > 0 ? 1 : 0
        const ref = this.refIdx[mb]!
        for (let k = 0; k < ref; k++) c.decision(54 + (k === 0 ? ra + 2 * rb : k === 1 ? 4 : 5), 1)
        c.decision(54 + (ref === 0 ? ra + 2 * rb : ref === 1 ? 4 : 5), 0)
      }
      const ax = l >= 0 ? iabs(this.mvdx[l]!) : 0
      const bx = tp >= 0 ? iabs(this.mvdx[tp]!) : 0
      this.writeMvdCabac(40, this.mvdx[mb]!, ax + bx)
      const ay = l >= 0 ? iabs(this.mvdy[l]!) : 0
      const by = tp >= 0 ? iabs(this.mvdy[tp]!) : 0
      this.writeMvdCabac(47, this.mvdy[mb]!, ay + by)
    } else if (intra) {
      const incA = l >= 0 && this.mbType[l]! !== MB_I4 && this.mbType[l]! !== MB_I8 ? 1 : 0
      const incB = tp >= 0 && this.mbType[tp]! !== MB_I4 && this.mbType[tp]! !== MB_I8 ? 1 : 0
      if (pslice) c.decision(14, 1)
      const sufOff = pslice ? 17 : 32
      const b0 = pslice || bslice ? sufOff : 3 + incA + incB
      if (t === MB_I16) {
        c.decision(b0, 1)
        c.terminate(0)
        const m = this.i16Mode[mb]!
        if (pslice || bslice) {
          c.decision(sufOff + 1, cbpL !== 0 ? 1 : 0)
          c.decision(sufOff + 2, cbpC !== 0 ? 1 : 0)
          if (cbpC !== 0) c.decision(sufOff + 2, cbpC === 2 ? 1 : 0)
          c.decision(sufOff + 3, m >> 1)
          c.decision(sufOff + 3, m & 1)
        } else {
          c.decision(6, cbpL !== 0 ? 1 : 0)
          c.decision(7, cbpC !== 0 ? 1 : 0)
          if (cbpC !== 0) c.decision(8, cbpC === 2 ? 1 : 0)
          c.decision(9, m >> 1)
          c.decision(10, m & 1)
        }
      } else {
        c.decision(b0, 0)
        if (this.params.transform8x8) {
          const ta = l >= 0 && this.t8[l]! === 1 ? 1 : 0
          const tb = tp >= 0 && this.t8[tp]! === 1 ? 1 : 0
          c.decision(399 + ta + tb, t === MB_I8 ? 1 : 0)
        }
        if (t === MB_I4) {
          for (let i = 0; i < 16; i++) {
            const r = BLK_RASTER[i]!
            this.cabacModeFlag(this.imode[mb * 16 + r]!, this.predMode4(mb, mbx, mby, r & 3, r >> 2))
          }
        } else {
          for (let b8 = 0; b8 < 4; b8++) {
            const bx = (b8 & 1) * 2
            const by = (b8 >> 1) * 2
            this.cabacModeFlag(this.imode[mb * 16 + by * 4 + bx]!, this.predMode4(mb, mbx, mby, bx, by))
          }
        }
      }
      const cm = this.cMode[mb]!
      const ca = l >= 0 && this.isIntra(l) && this.cMode[l]! !== 0 ? 1 : 0
      const cb = tp >= 0 && this.isIntra(tp) && this.cMode[tp]! !== 0 ? 1 : 0
      c.decision(64 + ca + cb, cm > 0 ? 1 : 0)
      if (cm > 0) {
        c.decision(67, cm > 1 ? 1 : 0)
        if (cm > 1) c.decision(67, cm > 2 ? 1 : 0)
      }
    }
    if (t !== MB_I16) {
      for (let b8 = 0; b8 < 4; b8++) {
        let ca8 = 0
        let cb8 = 0
        if ((b8 & 1) === 1) {
          ca8 = (cbpL >> (b8 - 1) & 1) === 0 ? 1 : 0
        } else if (l >= 0) {
          ca8 = ((this.cbp[l]! & 15) >> (b8 + 1) & 1) === 0 ? 1 : 0
        }
        if (b8 >= 2) {
          cb8 = (cbpL >> (b8 - 2) & 1) === 0 ? 1 : 0
        } else if (tp >= 0) {
          cb8 = ((this.cbp[tp]! & 15) >> (b8 + 2) & 1) === 0 ? 1 : 0
        }
        c.decision(73 + ca8 + 2 * cb8, (cbpL >> b8) & 1)
      }
      const la = l >= 0 && (this.cbp[l]! >> 4) !== 0 ? 1 : 0
      const lb = tp >= 0 && (this.cbp[tp]! >> 4) !== 0 ? 1 : 0
      c.decision(77 + la + 2 * lb, cbpC !== 0 ? 1 : 0)
      if (cbpC !== 0) {
        const ma = l >= 0 && (this.cbp[l]! >> 4) === 2 ? 1 : 0
        const mb2 = tp >= 0 && (this.cbp[tp]! >> 4) === 2 ? 1 : 0
        c.decision(81 + ma + 2 * mb2, cbpC === 2 ? 1 : 0)
      }
      if (!intra && cbpL !== 0 && this.params.transform8x8) {
        const ta = l >= 0 && this.t8[l]! === 1 ? 1 : 0
        const tb = tp >= 0 && this.t8[tp]! === 1 ? 1 : 0
        c.decision(399 + ta + tb, this.t8[mb]!)
      }
    }
    if (t === MB_I16 || cbp !== 0) {
      const d = this.qpDelta[mb]!
      const k = d > 0 ? 2 * d - 1 : -2 * d
      c.decision(60 + (this.lastDeltaNz ? 1 : 0), k > 0 ? 1 : 0)
      for (let i = 1; i <= k; i++) c.decision(i === 1 ? 62 : 63, i < k ? 1 : 0)
      this.lastDeltaNz = d !== 0
    } else {
      this.lastDeltaNz = false
    }
    if (t === MB_I16) {
      const dcCoded = (this.dcCbf[mb]! & 1) !== 0
      c.cbf(CAT_LUMA_DC, this.cbfDc(mb, mbx, mby, 0, true), dcCoded)
      if (dcCoded) c.residual(CAT_LUMA_DC, this.coef, DC_OFF, 16)
      if (cbpL !== 0) {
        for (let i = 0; i < 16; i++) {
          const r = BLK_RASTER[i]!
          const coded = this.nnz[mb * 24 + r]! > 0
          c.cbf(CAT_LUMA_AC, this.cbfLuma4(mb, mbx, mby, r, true), coded)
          if (coded) c.residual(CAT_LUMA_AC, this.coef, r * 16, 15)
        }
      }
    } else {
      for (let b8 = 0; b8 < 4; b8++) {
        if (((cbpL >> b8) & 1) === 0) continue
        if (this.t8[mb]! === 1) {
          c.residual(CAT_LUMA_8x8, this.coef, b8 * 64, 64)
          continue
        }
        for (let s = 0; s < 4; s++) {
          const r = BLK_RASTER[b8 * 4 + s]!
          const coded = this.nnz[mb * 24 + r]! > 0
          c.cbf(CAT_LUMA_4x4, this.cbfLuma4(mb, mbx, mby, r, intra), coded)
          if (coded) c.residual(CAT_LUMA_4x4, this.coef, r * 16, 16)
        }
      }
    }
    if (cbpC !== 0) {
      for (let pl = 0; pl < 2; pl++) {
        const coded = ((this.dcCbf[mb]! >> (pl + 1)) & 1) !== 0
        c.cbf(CAT_CHROMA_DC, this.cbfDc(mb, mbx, mby, pl + 1, intra), coded)
        if (coded) c.residual(CAT_CHROMA_DC, this.coef, CHROMA_DC_OFF + pl * 4, 4)
      }
    }
    if (cbpC === 2) {
      for (let pl = 0; pl < 2; pl++) {
        for (let b = 0; b < 4; b++) {
          const coded = this.nnz[mb * 24 + 16 + pl * 4 + b]! > 0
          c.cbf(CAT_CHROMA_AC, this.cbfChroma4(mb, mbx, mby, pl, b, intra), coded)
          if (coded) c.residual(CAT_CHROMA_AC, this.coef, CHROMA_AC_OFF + (pl * 4 + b) * 16, 15)
        }
      }
    }
  }

  cabacModeFlag(mode: int, pred: int) {
    const c = this.cabac
    if (mode === pred) {
      c.decision(68, 1)
    } else {
      c.decision(68, 0)
      const rem = mode < pred ? mode : mode - 1
      c.decision(69, rem & 1)
      c.decision(69, (rem >> 1) & 1)
      c.decision(69, (rem >> 2) & 1)
    }
  }

  writeModeFlag(mode: int, pred: int) {
    if (mode === pred) {
      this.bw.putBit(1)
    } else {
      this.bw.putBit(0)
      this.bw.putBits(mode < pred ? mode : mode - 1, 3)
    }
  }

  codedBlocks(mb: int): int {
    if (this.t8[mb]! === 1) return CBP_BLOCKS[this.cbp[mb]! & 15]!
    return i8x16.load(this.nnz, mb * 24).gt(i8x16.splat(0)).mask()
  }

  motionBs(mbP: int, mbQ: int): int {
    if (this.sliceType === SLICE_B) return this.edgeBsB(mbP, mbQ)
    if (this.refs[this.refIdx[mbP]!]! !== this.refs[this.refIdx[mbQ]!]!) return 1
    if (iabs(this.mvx[mbP]! - this.mvx[mbQ]!) >= 4) return 1
    if (iabs(this.mvy[mbP]! - this.mvy[mbQ]!) >= 4) return 1
    return 0
  }

  dirty(mb: int): bool {
    return this.changed[mb]! !== 0 || this.refresh[mb]! !== 0
  }

  dbNeed(mbx: int, mby: int, full: bool): bool {
    if (full) return true
    const mb = mby * this.mbw + mbx
    return this.dirty(mb) || (mbx > 0 && this.dirty(mb - 1)) || (mby > 0 && this.dirty(mb - this.mbw))
  }

  finalizeRow(mby: int, full: bool, ver: int) {
    const mbw = this.mbw
    const below = mby + 1 < this.mbh
    for (let mbx = 0; mbx < mbw; mbx++) {
      const hit = this.dbNeed(mbx, mby, full) || (mbx + 1 < mbw && this.dbNeed(mbx + 1, mby, full)) ||
        (below && this.dbNeed(mbx, mby + 1, full))
      if (!hit) continue
      this.cur.ver[mby * mbw + mbx] = ver
      this.padMb(mbx, mby)
    }
  }

  deblockFrame(full: bool) {
    for (let mby = 0; mby < this.mbh; mby++) {
      if (this.deblock) {
        for (let mbx = 0; mbx < this.mbw; mbx++) {
          if (this.dbNeed(mbx, mby, full)) this.deblockMb(mbx, mby)
        }
      }
      if (mby > 0) this.finalizeRow(mby - 1, full, this.counter)
    }
    this.finalizeRow(this.mbh - 1, full, this.counter)
  }

  deblockMb(mbx: int, mby: int) {
    const mb = mby * this.mbw + mbx
    const t8 = this.t8[mb]! === 1
    const qpQ = this.qpY[mb]!
    if (this.isIntra(mb)) {
      for (let dir = 0; dir < 2; dir++) {
        for (let e = 0; e < 4; e++) {
          if (e === 0 && (dir === 0 ? mbx === 0 : mby === 0)) continue
          if (t8 && (e & 1) === 1) continue
          const qpP = e === 0 ? this.qpY[dir === 0 ? mb - 1 : mb - this.mbw]! : qpQ
          const qpc = (int(CHROMA_QP[qpP]!) + int(CHROMA_QP[qpQ]!) + 1) >> 1
          this.filterEdge(mbx, mby, dir, e, 0, e === 0 ? 4 : 3, (qpP + qpQ + 1) >> 1, qpc)
        }
      }
      return
    }
    const codedQ = this.codedBlocks(mb)
    for (let dir = 0; dir < 2; dir++) {
      const sideQ = dir === 0 ? transposeBlocks(codedQ) : codedQ
      for (let e = 0; e < 4; e++) {
        if (e === 0 && (dir === 0 ? mbx === 0 : mby === 0)) continue
        if (t8 && (e & 1) === 1) continue
        const mbP = e === 0 ? (dir === 0 ? mb - 1 : mb - this.mbw) : mb
        const qpP = this.qpY[mbP]!
        const ia = (qpP + qpQ + 1) >> 1
        const qpc = (int(CHROMA_QP[qpP]!) + int(CHROMA_QP[qpQ]!) + 1) >> 1
        if (e === 0 && this.isIntra(mbP)) {
          this.filterEdge(mbx, mby, dir, e, 0, 4, ia, qpc)
          continue
        }
        let coded = (sideQ >> (4 * e)) & 15
        let motion = 0
        if (e === 0) {
          const codedP = this.codedBlocks(mbP)
          coded |= ((dir === 0 ? transposeBlocks(codedP) : codedP) >> 12) & 15
          motion = this.motionBs(mbP, mb)
        } else {
          coded |= (sideQ >> (4 * e - 4)) & 15
        }
        if (coded === 15) {
          this.filterEdge(mbx, mby, dir, e, 0, 2, ia, qpc)
        } else if (coded !== 0 || motion !== 0) {
          this.filterEdge(mbx, mby, dir, e, coded, motion, ia, qpc)
        }
      }
    }
  }

  filterEdge(mbx: int, mby: int, dir: int, e: int, coded: int, bs: int, ia: int, qpc: int) {
    const o = dir === 0 ? this.ry(mbx * 16 + e * 4, mby * 16) : this.ry(mbx * 16, mby * 16 + e * 4)
    const across = dir === 0 ? 1 : this.ys
    const along = dir === 0 ? this.ys : 1
    if (coded === 0) {
      filterLuma16(this.cur.y, o, across, along, bs, ia, ia)
    } else {
      filterLumaSegments(this.cur.y, o, across, along, coded, bs, ia, ia)
    }
    if ((e & 1) !== 0) return
    const oc = dir === 0 ? this.rc(mbx * 8 + e * 2, mby * 8) : this.rc(mbx * 8, mby * 8 + e * 2)
    const acrossC = dir === 0 ? 1 : this.cs
    const alongC = dir === 0 ? this.cs : 1
    if (coded === 0) {
      filterChromaUV(this.cur.u, this.cur.v, oc, acrossC, alongC, bs, qpc, qpc)
    } else {
      filterChromaSegments(this.cur.u, this.cur.v, oc, acrossC, alongC, coded, bs, qpc, qpc)
    }
  }

  reconFrame(): Uint8Array {
    return this.reconOf(this.refs[0]!)
  }

  reconOf(f: RefFrame): Uint8Array {
    const cwOut = this.w >> 1
    const out = new Uint8Array(this.w * this.h + 2 * cwOut * (this.h >> 1))
    let k = 0
    for (let y = 0; y < this.h; y++) {
      out.set(f.y.subarray(this.ry(0, y), this.ry(0, y) + this.w), k)
      k += this.w
    }
    for (let pl = 0; pl < 2; pl++) {
      const src = pl === 0 ? f.u : f.v
      for (let y = 0; y < this.h >> 1; y++) {
        out.set(src.subarray(this.rc(0, y), this.rc(0, y) + cwOut), k)
        k += cwOut
      }
    }
    return out
  }
}

function testFrame(w: int, h: int, shift: int): Uint8Array {
  const f = new Uint8Array(w * h + (w * h >> 1))
  for (let y = 0; y < h; y++) {
    for (let x = 0; x < w; x++) f[y * w + x] = ((x + shift) * 37 + y * 11 + ((x + shift) >> 2) * y) & 255
  }
  for (let i = w * h; i < f.length; i++) f[i] = 128 + (i & 7)
  return f
}

function adaptiveRun(adapt: bool): string {
  const enc = new Encoder({
    width: 64, height: 64, fpsNum: 30, fpsDen: 1, qp: 26, keyint: 100, profile: 100, preset: "medium", cabac: true,
    refs: 1, rcMode: RC_CQP, crf: 26.0, bitrate: 0, maxrate: 0, bufsize: 0, ipOffset: 3, weightedPred: false, worker: false,
  })
  enc.bAdapt = adapt
  enc.wantRecon = true
  enc.configureB(2)
  let recon = 0
  for (let i = 0; i < 12; i++) {
    const moving = i >= 4 && i <= 8
    const side = new SideInfo()
    if (moving) side.dirty = [0, 0, 64, 64]
    enc.submit(testFrame(64, 64, i < 4 ? 3 : i > 8 ? 8 : i), 0, side)
    recon += enc.takeRecon().length
  }
  enc.flush()
  recon += enc.takeRecon().length
  let types = ""
  let coded = 0
  for (const f of enc.frames) {
    types += f.type
    if (f.bytes > 0) coded++
  }
  return `${types} ${coded} ${recon}`
}

function idleRefineRun(rcMode: int, bitrate: int): string {
  const enc = new Encoder({
    width: 64, height: 64, fpsNum: 30, fpsDen: 1, qp: 26, keyint: 1000, profile: 100, preset: "live", cabac: true,
    refs: 1, rcMode, crf: 26.0, bitrate, maxrate: (bitrate * 3) >> 1, bufsize: 0, ipOffset: 3, weightedPred: false,
    worker: false,
  })
  for (let i = 0; i < 48; i++) {
    const side = new SideInfo()
    if (i < 8) side.dirty = [0, 0, 64, 64]
    enc.encodeFrame(testFrame(64, 64, i < 8 ? i : 7), 0, side)
  }
  let s = ""
  for (const f of enc.frames) s += f.bytes === 0 ? "D" : f.type === "I" || f.changedMbs > 0 ? f.type : `R${f.qp}`
  return s
}

test("bitrate mode re-codes macroblocks left at a high qp once the screen has idled and the budget allows it", () => {
  expect(idleRefineRun(RC_VBR, 60)).toBe("IPPPPPPPR14DDDDDDDDDDDDDDR8DDDDDDDDDDDDDDDDDDDDDDDD")
  expect(idleRefineRun(RC_CQP, 0)).toBe("IPPPPPPPR26DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD")
})

test("adaptive B-frames drop or P-code static frames and use B-frames only while the frame moves", () => {
  expect(adaptiveRun(true)).toBe("IDDDPPBBPPDD 7 7")
  expect(adaptiveRun(false)).toBe("IPBBPBBPBBPB 12 12")
})

test("with B-frames every anchor takes the lookahead map of its own display index", () => {
  const enc = new Encoder({
    width: 64, height: 64, fpsNum: 30, fpsDen: 1, qp: 26, keyint: 100, profile: 100, preset: "medium", cabac: true,
    refs: 1, rcMode: RC_CQP, crf: 26.0, bitrate: 0, maxrate: 0, bufsize: 0, ipOffset: 3, weightedPred: false, worker: false,
  })
  enc.aqStrength = 0
  enc.lifetimeAq = false
  enc.refreshDelta = 0
  enc.configureB(2)
  const lives = [60, 20, 1, 1, 4, 30, 1, 1]
  let seen: string[] = []
  for (let i = 0; i < lives.length; i++) {
    const life = new Int32Array(16)
    life.fill(lives[i]! * LIFE_ONE)
    enc.setLookahead(life)
    const moving = i > 0 && i < 6
    const side = new SideInfo()
    if (moving) side.dirty = [0, 0, 64, 64]
    enc.submit(testFrame(64, 64, moving ? i : i === 0 ? 0 : 5), 0, side)
    seen.push(`${enc.lastAnchorIndex}:${enc.mbQp[0]! - enc.anchorQp}`)
  }
  expect(seen.join(" ")).toBe("0:-3 1:0 1:0 1:0 4:4 4:4 5:-1 5:-1")
})

function driftFrames(w: int, h: int, n: int): Uint8Array {
  const fb = w * h + ((w * h) >> 1)
  const all = new Uint8Array(fb * n)
  all.set(testFrame(w, h, 0), 0)
  let s = 4242
  for (let i = 1; i < n; i++) {
    const o = i * fb
    all.copyWithin(o, i % 6 === 0 ? 0 : o - fb, i % 6 === 0 ? fb : o)
    for (let k = 0; k < 12; k++) {
      s = (s * 1103515245 + 12345) & 0x7fffffff
      const x = (s >> 4) % w
      const y = (s >> 12) % h
      const kind = (s >> 20) & 3
      const p = kind === 3 ? o + w * h + (y >> 1) * (w >> 1) + (x >> 1) : o + y * w + x
      const v = int(all[p]!) + (kind === 0 ? 40 : kind === 1 ? 1 : kind === 2 ? -1 : 2)
      all[p] = v < 0 ? 0 : v > 255 ? 255 : v
    }
  }
  return all
}

test("macroblock changes taken from the lookahead's map are the ones the encoder finds itself", () => {
  const w = 80
  const h = 40
  const n = 18
  const fb = w * h + ((w * h) >> 1)
  const all = driftFrames(w, h, n)
  const runs: string[] = []
  for (let v = 0; v < 3; v++) {
    const enc = new Encoder({
      width: w, height: h, fpsNum: 30, fpsDen: 1, qp: 26, keyint: 100, profile: 100, preset: "medium", cabac: true,
      refs: 1, rcMode: RC_CQP, crf: 26.0, bitrate: 0, maxrate: 0, bufsize: 0, ipOffset: 3, weightedPred: false, worker: false,
    })
    enc.noise = 2
    const la = new Lookahead(w, h, 2, false)
    la.noise = 2
    let s = ""
    for (let i = 0; i < n; i++) {
      la.push(all, i * fb, i > 0 ? (i - 1) * fb : 0, undefined)
      const map = la.changesOf(i)
      if (v === 1) enc.useChanges(map, i, 2)
      if (v === 2) {
        const wrong = new Uint8Array(map.length)
        wrong.fill(1)
        enc.useChanges(wrong, i, 2)
      }
      const bits = enc.encodeFrame(all, i * fb, undefined)
      s += `${bits.length}:${enc.frames[enc.frames.length - 1]!.changedMbs} `
    }
    runs.push(s)
  }
  expect(`${runs[0]! === runs[1]!} ${runs[0]! !== runs[2]!}`).toBe("true true")
})

test("the lookahead horizon of a frame is known before the frames ahead of it are coded", () => {
  let seen = ""
  for (const b of [0, 2]) {
    const enc = new Encoder({
      width: 64, height: 64, fpsNum: 30, fpsDen: 1, qp: 26, keyint: 5, profile: 100, preset: "medium", cabac: true,
      refs: 1, rcMode: RC_CQP, crf: 26.0, bitrate: 0, maxrate: 0, bufsize: 0, ipOffset: 3, weightedPred: false, worker: false,
    })
    if (b > 0) enc.configureB(b)
    for (let i = 0; i < 13; i++) {
      seen += enc.laHorizonAt(i) === enc.laHorizon() ? `${enc.laHorizon()}` : "x"
      const moving = i % 4 !== 3
      const side = new SideInfo()
      if (moving) side.dirty = [0, 0, 64, 64]
      const frame = testFrame(64, 64, moving ? i : i - 1)
      if (b > 0) {
        enc.submit(frame, 0, side)
      } else {
        enc.encodeFrame(frame, 0, side)
      }
    }
    seen += " "
  }
  expect(seen).toBe("5432154321543 5432154321543 ")
})

test("an anchor gains lifetime from the B-frames that predict from it through list 1", () => {
  const enc = new Encoder({
    width: 64, height: 64, fpsNum: 30, fpsDen: 1, qp: 26, keyint: 100, profile: 100, preset: "medium", cabac: true,
    refs: 1, rcMode: RC_CQP, crf: 26.0, bitrate: 0, maxrate: 0, bufsize: 0, ipOffset: 3, weightedPred: false, worker: false,
  })
  enc.configureB(2)
  enc.lastAnchorIndex = 4
  const b5 = new Pending(new Uint8Array(0), 5)
  b5.keep = new Uint8Array(16)
  b5.keep.fill(KEEP_ONE)
  const b6 = new Pending(new Uint8Array(0), 6)
  b6.keep = new Uint8Array(16)
  b6.keep.fill(KEEP_ONE)
  b6.keep[0] = KEEP_ONE >> 1
  enc.pending = [b5, b6]
  const keep = new Uint8Array(16)
  keep.fill(KEEP_ONE)
  keep[1] = 0
  const life = new Int32Array(16)
  life.fill(10 * LIFE_ONE)
  enc.anchorLookahead(life, keep, 7)
  const whole = Math.trunc(enc.laL1 * f64(LIFE_ONE) + 0.5)
  const partial = Math.trunc(enc.laL1 * f64(LIFE_ONE) * (2.0 / 3.0 + 0.5 / 3.0) + 0.5)
  expect(`${life[0]! - 10 * LIFE_ONE} ${life[1]! - 10 * LIFE_ONE} ${life[2]! - 10 * LIFE_ONE}`).toBe(`${partial} 0 ${whole}`)
})

test("GOPs too short for a B run declare no B-frames", () => {
  let seen = ""
  for (const k of [1, 2, 3]) {
    const enc = new Encoder({
      width: 64, height: 64, fpsNum: 30, fpsDen: 1, qp: 26, keyint: k, profile: 100, preset: "medium", cabac: true,
      refs: 1, rcMode: RC_CQP, crf: 26.0, bitrate: 0, maxrate: 0, bufsize: 0, ipOffset: 3, weightedPred: false, worker: false,
    })
    enc.pyramid = true
    enc.configureB(3)
    seen += `${enc.bframes}/${enc.params.bframes}/${enc.params.dpbRefs}/${enc.pyramid} `
  }
  expect(seen).toBe("0/0/1/false 0/0/1/false 3/3/3/true ")
})

test("transposeBlocks swaps the row and column of every 4x4 block bit", () => {
  let bad = 0
  for (let m = 0; m < 65536; m++) {
    let want = 0
    for (let r = 0; r < 16; r++) {
      if (((m >> r) & 1) !== 0) want |= 1 << ((r & 3) * 4 + (r >> 2))
    }
    if (transposeBlocks(m) !== want) bad++
  }
  expect(bad).toBe(0)
})