resizedSize method

(int, int) resizedSize(
  1. int frames,
  2. int height,
  3. int width
)

Upstream video smart_resize for frames sampled frames.

Implementation

(int height, int width) resizedSize(int frames, int height, int width) {
  if (frames < temporalPatchSize || height <= 0 || width <= 0) {
    throw ArgumentError('invalid video frame count or size');
  }
  var budget = maxPixels;
  if (capPixelsPerFrame) {
    final perFrame = math.max(
      math.min(maxVideoTokens * factor * factor, maxPixels ~/ frames),
      (minPixels * 1.05).floor(),
    );
    budget = perFrame * frames;
  }
  if (height < factor || width < factor) {
    final scale = math.max(factor / height, factor / width);
    height = (height * scale).floor();
    width = (width * scale).floor();
  }
  if (math.max(height, width) / math.min(height, width) > 200) {
    throw ArgumentError('video aspect ratio must be <= 200');
  }
  var h = _roundEven(height / factor) * factor;
  var w = _roundEven(width / factor) * factor;
  final t = _roundEven(frames / temporalPatchSize) * temporalPatchSize;
  if (t * h * w > budget) {
    final beta = math.sqrt(frames * height * width / budget);
    h = math.max(factor, (height / beta / factor).floor() * factor);
    w = math.max(factor, (width / beta / factor).floor() * factor);
  } else if (t * h * w < minPixels) {
    final beta = math.sqrt(minPixels / (frames * height * width));
    h = (height * beta / factor).ceil() * factor;
    w = (width * beta / factor).ceil() * factor;
  }
  return (h, w);
}