pickQi method

int pickQi({
  1. required bool isKey,
})

Pick a qi for the next frame. Call before encoding.

Implementation

int pickQi({required bool isKey}) {
  final windowBits = targetBitrateBps * bufferWindowSeconds;
  final fullness = _bufferBits / windowBits;
  var adj = (fullness * qiStepGain).round();
  // For keyframes, bias one step down — they always cost more than a
  // P-frame so without the bias the controller would push qi up the
  // moment a key lands and then back down for the next P-frame.
  if (isKey) adj -= 1;

  var target = (startQi + adj);
  // Slew-rate limit relative to the previous frame's qi.
  final lo = _currentQi - maxQiStepPerFrame;
  final hi = _currentQi + maxQiStepPerFrame;
  target = math.max(lo, math.min(hi, target));
  target = math.max(minQi, math.min(maxQi, target));
  _currentQi = target;
  return target;
}