1use alloc::vec::Vec;
4use core::num::NonZeroUsize;
5use core::time::Duration;
6
7use crate::analyze::Analysis;
8use crate::candidates::{Candidate, Context, Grid};
9use crate::dp;
10use crate::errors::ConfigError;
11use crate::segment::SegmentConfig;
12
13#[derive(Clone, Copy, Debug, PartialEq)]
15#[non_exhaustive]
16pub enum Placement {
17 Quietest,
19 Center,
21 Ratio(f32),
23}
24
25#[derive(Clone, Copy, Debug, PartialEq, Eq)]
27#[non_exhaustive]
28pub enum Gap {
29 Keep,
31 Drop,
34}
35
36#[derive(Clone, Debug, PartialEq)]
47#[non_exhaustive]
48pub struct Weights {
49 pub cut: f64,
51 pub level: f64,
53 pub pause: f64,
55 pub pause_ref: Duration,
57 pub target: f64,
60 pub short: f64,
62}
63
64impl Default for Weights {
65 fn default() -> Self {
66 Self {
67 cut: 0.1,
68 level: 1.0,
69 pause: 1.0,
70 pause_ref: Duration::from_millis(500),
71 target: 1.0,
72 short: 4.0,
73 }
74 }
75}
76
77#[derive(Clone, Debug, PartialEq)]
79#[non_exhaustive]
80pub struct PlanConfig {
81 pub(crate) min: Duration,
82 pub(crate) target: Duration,
83 pub(crate) max: Duration,
84 pub(crate) count: Option<NonZeroUsize>,
85 pub(crate) gap: Gap,
86 pub(crate) placement: Placement,
87 pub(crate) weights: Weights,
88}
89
90impl PlanConfig {
91 pub fn new(min: Duration, target: Duration, max: Duration) -> Result<Self, ConfigError> {
101 if target.is_zero() || min > target || target > max {
102 return Err(ConfigError::InvalidLengths);
103 }
104 Ok(Self {
105 min,
106 target,
107 max,
108 count: None,
109 gap: Gap::Keep,
110 placement: Placement::Quietest,
111 weights: Weights::default(),
112 })
113 }
114
115 #[must_use]
118 pub fn count(mut self, count: NonZeroUsize) -> Self {
119 self.count = Some(count);
120 self
121 }
122
123 #[must_use]
125 pub fn gap(mut self, gap: Gap) -> Self {
126 self.gap = gap;
127 self
128 }
129
130 pub fn placement(mut self, placement: Placement) -> Result<Self, ConfigError> {
136 if let Placement::Ratio(r) = placement {
137 if !(0.0..=1.0).contains(&r) {
138 return Err(ConfigError::InvalidRatio);
139 }
140 }
141 self.placement = placement;
142 Ok(self)
143 }
144
145 pub fn weights(mut self, weights: Weights) -> Result<Self, ConfigError> {
152 let w = &weights;
153 let valid = [w.level, w.pause, w.target, w.short]
154 .iter()
155 .all(|v| v.is_finite() && *v >= 0.0)
156 && w.cut.is_finite()
157 && w.cut > 0.0
158 && !w.pause_ref.is_zero();
159 if !valid {
160 return Err(ConfigError::InvalidWeights);
161 }
162 self.weights = weights;
163 Ok(self)
164 }
165}
166
167#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
172#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
173#[non_exhaustive]
174pub struct Cut {
175 pub end: u64,
177 pub start: u64,
179 pub reason: CutReason,
181}
182
183#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
185#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
186#[non_exhaustive]
187pub enum CutReason {
188 Silence,
190 Quiet,
192 Forced,
194}
195
196#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
198#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
199#[non_exhaustive]
200pub struct Piece {
201 pub start: u64,
203 pub end: u64,
205 pub too_short: bool,
207 pub too_long: bool,
210}
211
212#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
218#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
219#[non_exhaustive]
220pub struct Plan {
221 pub pieces: Vec<Piece>,
223 pub cuts: Vec<Cut>,
225}
226
227#[must_use]
240pub fn plan(analysis: &Analysis, segment: &SegmentConfig, config: &PlanConfig) -> Plan {
241 let max = analysis.samples_floor(config.max);
242 let block_time = (config.max.saturating_sub(config.min) / 2)
245 .max(config.max / 8)
246 .min(Duration::from_secs(1));
247 let block = analysis.frames_floor(block_time).max(1);
248 let block_samples = analysis.position(block.min(analysis.frames()));
249 let padding_cap = analysis.frames_floor(config.max).saturating_sub(2 * block);
251 let ctx = Context::new(analysis, segment, &config.weights, padding_cap);
252 let Some((first, last)) = ctx.ends(config.gap) else {
253 return Plan::default();
254 };
255 let span = analysis.position(last) - analysis.position(first);
256 let grid = Grid {
259 block,
260 edge_limit: Some(max.saturating_sub(2 * block_samples)),
261 };
262 let mut nodes = ctx.candidates(config.gap, config.placement, &grid, first, last);
263 let target = match config.count {
264 Some(n) => span as f64 / n.get() as f64,
265 None => {
266 ctx.complete_max(&mut nodes, max);
267 analysis.samples_in(config.target)
268 }
269 };
270 let solve = |nodes: &[Candidate], reach: u64| -> Plan {
271 let lengths = dp::Lengths {
272 min: analysis.samples_in(config.min),
273 target,
274 max: reach,
275 weights: &config.weights,
276 };
277 let left: Vec<u64> = nodes.iter().map(|c| analysis.position(c.left)).collect();
278 let right: Vec<u64> = nodes.iter().map(|c| analysis.position(c.right)).collect();
279 let cost: Vec<f64> = nodes.iter().map(|c| c.cost).collect();
280 let path = match config.count {
281 Some(n) => dp::solve_count(&left, &right, &cost, &lengths, n.get()),
282 None => dp::solve(&left, &right, &cost, &lengths),
283 };
284 ctx.to_plan(nodes, &path, lengths.min, max)
285 };
286 let result = solve(&nodes, max);
287 if let Some(n) = config.count {
288 if result.pieces.len() < n.get() || result.pieces.iter().any(|p| p.too_long) {
289 let reach = ctx.complete_count(&mut nodes, max, n.get());
290 return solve(&nodes, reach);
291 }
292 }
293 result
294}
295
296#[must_use]
307pub fn adjust(
308 analysis: &Analysis,
309 segment: &SegmentConfig,
310 points: &[u64],
311 window: Duration,
312) -> Plan {
313 let weights = Weights::default();
314 let ctx = Context::new(analysis, segment, &weights, usize::MAX);
315 let n = analysis.frames();
316 if n == 0 {
317 return Plan::default();
318 }
319 let grid = Grid {
320 block: analysis
321 .frames_floor(window.min(Duration::from_secs(1)))
322 .max(1),
323 edge_limit: Some(0),
324 };
325 let candidates = ctx.candidates(Gap::Keep, Placement::Quietest, &grid, 0, n);
326 let inner = &candidates[1..candidates.len() - 1];
327 let positions: Vec<u64> = inner.iter().map(|c| analysis.position(c.left)).collect();
328 let len = analysis.sample_count();
329 let mut points: Vec<u64> = points
330 .iter()
331 .copied()
332 .filter(|p| *p > 0 && *p < len)
333 .collect();
334 points.sort_unstable();
335 points.dedup();
336
337 let reach = analysis.samples_in(window);
338 let mut chosen: Vec<Candidate> = Vec::with_capacity(points.len() + 2);
339 chosen.push(Candidate::node(0));
340 for (k, &p) in points.iter().enumerate() {
341 let lo = k.checked_sub(1).map_or(0, |i| points[i].midpoint(p));
342 let hi = points.get(k + 1).map_or(len, |next| p.midpoint(*next));
343 let from = positions.partition_point(|x| (*x as f64) < p as f64 - reach || *x < lo);
344 let to = positions.partition_point(|x| *x < hi && *x as f64 <= p as f64 + reach);
345 let best = (from..to.max(from))
346 .map(|i| {
347 let distance = positions[i].abs_diff(p) as f64;
348 let pull = if reach > 0.0 { distance / reach } else { 0.0 };
349 (inner[i].cost + pull, &inner[i])
350 })
351 .fold(None, |best: Option<(f64, &Candidate)>, (v, c)| match best {
352 Some((b, _)) if b <= v => best,
353 _ => Some((v, c)),
354 });
355 let pick = match best {
356 Some((_, c)) => c.clone(),
357 None => ctx.forced_at(p),
358 };
359 let after_previous = chosen.last().is_some_and(|c| c.left < pick.left);
360 if pick.left < n && after_previous {
361 chosen.push(pick);
362 }
363 }
364 chosen.push(Candidate::node(n));
365 let path: Vec<usize> = (0..chosen.len()).collect();
366 ctx.to_plan(&chosen, &path, 0.0, u64::MAX)
367}