12#ifndef OPENSHOT_ADAPTIVE_PREROLL_H
13#define OPENSHOT_ADAPTIVE_PREROLL_H
35 if (duration_us < 0)
return;
36 const double value =
static_cast<double>(duration_us);
37 if (!samples) mean = value;
39 deviation += 0.25 * (std::abs(value - mean) - deviation);
40 mean += 0.25 * (value - mean);
45 int64_t
Target(
double fps,
int speed, int64_t capacity,
46 int64_t conservative, int64_t maximum) {
47 const int64_t stride = std::max<int64_t>(1, std::abs(
static_cast<int64_t
>(speed)));
48 const int64_t limit = std::max<int64_t>(0, std::min({capacity, maximum,
MaxProbeFrames}));
49 if (conservative < 0 || limit == 0)
return target = 0;
50 int64_t wanted = std::min(conservative, limit);
51 if (samples >= 3 && fps > 0) {
52 const double budget = 1000000.0 / fps / stride;
53 const double cost = (mean + 3 * deviation) / budget;
54 const double demand = cost < 0.5 ? 2.0 * stride
55 : std::ceil(2 + 4 * cost) * stride;
58 wanted =
static_cast<int64_t
>(std::min<double>(limit, demand));
60 wanted = std::clamp<int64_t>(wanted, 0, limit);
62 if (!target || wanted > target || wanted + 2 <= target) target = wanted;
63 return target = std::min(target, limit);
65 bool Ready(int64_t now, int64_t contiguous_ahead,
bool current_available,
66 int64_t required, int64_t minimum_step = 1) {
68 const int64_t threshold = released || timed_out ? minimum_step : required;
69 const bool ready = current_available && contiguous_ahead >= threshold;
70 if (ready) released =
true;
76 int64_t started = 0, target = 0;
78 double mean = 0, deviation = 0;
79 bool released =
false, timed_out =
false;
void Observe(int64_t duration_us)
int64_t Target(double fps, int speed, int64_t capacity, int64_t conservative, int64_t maximum)
bool Ready(int64_t now, int64_t contiguous_ahead, bool current_available, int64_t required, int64_t minimum_step=1)
static constexpr int64_t MaxProbeFrames
static constexpr int64_t StartupLimitUs
This namespace is the default namespace for all code in the openshot library.