use std::collections::BTreeSet;
use std::fmt;
use std::fs::File;
use std::io::{self, Read, Seek, SeekFrom};
use std::num::NonZeroUsize;
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::path::Path;
use std::sync::Arc;
use std::sync::atomic::{AtomicU32, AtomicUsize, Ordering};
use std::time::Instant;
use symphonia::core::formats::probe::Hint;
use symphonia::core::io::MediaSource;
use tracing::{debug, warn};
use crate::decode::opus::OpusUse;
use crate::decode::{self, Claimed, Decoder, Filled, HeAac, dsd};
use crate::grid::lattice::{self, Uncut};
use crate::grid::segments::Segments;
use crate::grid::{self, Scan};
use crate::limitation::Limitation;
use crate::thresholds::MeasureThresholds;
use crate::totals::Totals;
use crate::window::{Analyzer, FFT_SIZE, Window};
pub type CancelCheck = Arc<dyn Fn() -> bool + Send + Sync>;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum Coverage {
#[default]
Full,
Sampled {
over_seconds: f64,
windows: NonZeroUsize,
},
}
impl Coverage {
fn max_full_seconds(self) -> f64 {
match self {
Self::Full => f64::INFINITY,
Self::Sampled { over_seconds, .. } => over_seconds,
}
}
}
#[derive(Clone, Default)]
pub struct Settings {
pub coverage: Coverage,
pub deadline: Option<Instant>,
pub progress: Option<Arc<AtomicU32>>,
pub canceled: Option<CancelCheck>,
pub thresholds: MeasureThresholds,
}
impl fmt::Debug for Settings {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Settings")
.field("coverage", &self.coverage)
.field("deadline", &self.deadline)
.field("progress", &self.progress)
.field("canceled", &self.canceled.is_some())
.field("thresholds", &self.thresholds)
.finish()
}
}
fn is_canceled(canceled: Option<&CancelCheck>) -> bool {
canceled.is_some_and(|canceled| canceled())
}
fn is_past(deadline: Option<Instant>) -> bool {
deadline.is_some_and(|deadline| Instant::now() >= deadline)
}
impl Settings {
fn report(&self, done: f64) {
if let Some(progress) = &self.progress {
progress.store(thousandths(done), Ordering::Relaxed);
}
}
}
fn thousandths(done: f64) -> u32 {
(done.clamp(0.0, 1.0) * 1000.0) as u32
}
const SCAN_JOB_WINDOWS: f64 = 110.0;
struct Measuring<'a> {
analyzer: Analyzer<'a>,
uncut: Option<Uncut>,
}
impl Measuring<'_> {
fn window(&mut self, second: u64, buffer: &[Vec<f32>], rate: usize, measured: &mut Measured) {
let window = self
.analyzer
.measure(second as f64, buffer, &mut measured.totals);
measured.windows.push(window);
if let Some(uncut) = &mut self.uncut
&& buffer.first().is_some_and(|channel| channel.len() == rate)
{
uncut.take(second * rate as u64, buffer);
}
}
}
struct Run<'a> {
settings: &'a Settings,
windows_share: f64,
}
impl Run<'_> {
fn windows(&self, done: f64) {
self
.settings
.report(done.clamp(0.0, 1.0) * self.windows_share);
}
fn scan(&self, done: f64) {
let done = self.windows_share + (1.0 - self.windows_share) * done;
if let Some(progress) = &self.settings.progress {
progress.fetch_max(thousandths(done), Ordering::Relaxed);
}
}
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct Measured {
pub claimed: Claimed,
pub duration: f64,
pub bitrate_kbps: Option<u32>,
#[cfg_attr(feature = "serde", serde(default))]
pub opus: Vec<OpusUse>,
#[cfg_attr(feature = "serde", serde(default))]
pub he_aac: Option<HeAac>,
#[cfg_attr(feature = "serde", serde(default))]
pub dsd_rate: Option<u32>,
pub windows: Vec<Window>,
pub(crate) totals: Totals,
pub(crate) bits_used: Option<u32>,
pub sampled: bool,
pub grid: Option<Scan>,
pub(crate) channels_left_out: u32,
pub(crate) problems: Vec<Limitation>,
}
impl Measured {
pub fn dsd_multiple(&self) -> Option<u32> {
self.dsd_rate.map(dsd::multiple_of)
}
}
pub(crate) fn grid_looked_for(claimed: &Claimed, dsd_rate: Option<u32>) -> bool {
claimed.lossless && dsd_rate.is_none()
}
#[derive(Debug)]
pub enum Error {
Canceled,
OutOfTime,
Open(io::Error),
NotAudio(String),
Unsupported(String),
Panicked,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Canceled => f.write_str("the analysis was canceled"),
Self::OutOfTime => f.write_str("the analysis ran out of time while opening the file"),
Self::Open(_) => f.write_str("the file could not be opened"),
Self::NotAudio(why) | Self::Unsupported(why) => f.write_str(why),
Self::Panicked => f.write_str("the analyzer failed on this file, which is a bug in choon"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Open(e) => Some(e),
Self::Canceled
| Self::OutOfTime
| Self::NotAudio(_)
| Self::Unsupported(_)
| Self::Panicked => None,
}
}
}
struct Watched {
inner: Box<dyn MediaSource>,
canceled: Option<CancelCheck>,
deadline: Option<Instant>,
}
impl Watched {
fn new(inner: Box<dyn MediaSource>, settings: &Settings) -> Self {
Self {
inner,
canceled: settings.canceled.clone(),
deadline: settings.deadline,
}
}
}
impl Read for Watched {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
if is_canceled(self.canceled.as_ref()) || is_past(self.deadline) {
return Err(std::io::Error::other("the analysis stopped"));
}
self.inner.read(buf)
}
}
impl Seek for Watched {
fn seek(&mut self, to: SeekFrom) -> std::io::Result<u64> {
self.inner.seek(to)
}
}
impl MediaSource for Watched {
fn is_seekable(&self) -> bool {
self.inner.is_seekable()
}
fn byte_len(&self) -> Option<u64> {
self.inner.byte_len()
}
}
pub fn measure(path: impl AsRef<Path>, settings: &Settings) -> Result<Measured, Error> {
let path = path.as_ref();
let file = open(path, settings)?;
if file.metadata().map_err(Error::Open)?.is_dir() {
return Err(Error::Open(io::ErrorKind::IsADirectory.into()));
}
let source = Watched::new(Box::new(file), settings);
guarded(|| {
let opened = Decoder::from_source(Box::new(source), &decode::hint_of(path));
if let Err(problem) = &opened {
debug!(path = %path.display(), %problem, "audio not opened");
}
measure_opened(opened, settings)
})
}
fn open(path: &Path, settings: &Settings) -> Result<File, Error> {
#[cfg(unix)]
{
use std::os::unix::fs::FileTypeExt;
use std::sync::mpsc::{RecvTimeoutError, channel};
use std::time::Duration;
let stoppable = settings.canceled.is_some() || settings.deadline.is_some();
let fifo = std::fs::metadata(path).is_ok_and(|file| file.file_type().is_fifo());
if stoppable && fifo {
let (opened, waiting) = channel();
let path = path.to_path_buf();
std::thread::spawn(move || {
let _ = opened.send(File::open(path));
});
loop {
match waiting.recv_timeout(Duration::from_millis(20)) {
Ok(file) => return file.map_err(Error::Open),
Err(RecvTimeoutError::Timeout) if is_canceled(settings.canceled.as_ref()) => {
return Err(Error::Canceled);
}
Err(RecvTimeoutError::Timeout) if is_past(settings.deadline) => {
return Err(Error::OutOfTime);
}
Err(RecvTimeoutError::Timeout) => {}
Err(RecvTimeoutError::Disconnected) => return Err(Error::Panicked),
}
}
}
}
File::open(path).map_err(Error::Open)
}
fn guarded(analysis: impl FnOnce() -> Result<Measured, Error>) -> Result<Measured, Error> {
catch_unwind(AssertUnwindSafe(analysis)).unwrap_or_else(|_| {
warn!("the analysis panicked, the file not read");
Err(Error::Panicked)
})
}
pub fn measure_bytes(bytes: Vec<u8>, settings: &Settings) -> Result<Measured, Error> {
let source = Watched::new(Box::new(io::Cursor::new(bytes)), settings);
guarded(|| {
measure_opened(
Decoder::from_source(Box::new(source), &Hint::new()),
settings,
)
})
}
fn measure_opened(opened: Result<Decoder, Error>, settings: &Settings) -> Result<Measured, Error> {
match opened {
Ok(decoder) => measure_decoded(decoder, settings),
Err(_) if is_canceled(settings.canceled.as_ref()) => Err(Error::Canceled),
Err(_) if is_past(settings.deadline) => Err(Error::OutOfTime),
Err(problem) => Err(problem),
}
}
fn measure_decoded(mut decoder: Decoder, settings: &Settings) -> Result<Measured, Error> {
let claimed = decoder.claimed.clone();
let rate = claimed.sample_rate as usize;
let stated = claimed.frames.map(|frames| frames as f64 / rate as f64);
let bits = claimed.bit_depth.unwrap_or(24);
let scanned = grid_looked_for(&claimed, decoder.dsd_rate);
let opus_rate = lattice::Rate::of(claimed.sample_rate).filter(|_| scanned);
let mut measuring = Measuring {
analyzer: Analyzer::new(claimed.sample_rate, claimed.bit_depth, &settings.thresholds),
uncut: opus_rate.map(|rate| Uncut::new(rate, bits)),
};
let mut buffer: Vec<Vec<f32>> = (0..decoder.channels)
.map(|_| Vec::with_capacity(rate))
.collect();
let mut segments = claimed.frames.filter(|_| scanned).and_then(Segments::of);
let unscanned = scanned && claimed.frames.is_none();
let mut measured = Measured {
duration: stated.unwrap_or(0.0),
bitrate_kbps: None,
opus: Vec::new(),
he_aac: decoder.he_aac,
dsd_rate: decoder.dsd_rate,
channels_left_out: claimed.channels.saturating_sub(decoder.channels as u32),
claimed,
windows: Vec::new(),
totals: Totals::default(),
bits_used: None,
sampled: false,
grid: None,
problems: Vec::new(),
};
let picks = match settings.coverage {
Coverage::Sampled {
over_seconds,
windows,
} => stated
.filter(|seconds| *seconds > over_seconds)
.map(|seconds| picks(seconds, windows.get())),
Coverage::Full => None,
};
let longest = settings.coverage.max_full_seconds().ceil();
let windows = picks.as_ref().map_or_else(
|| stated.map_or(longest, |seconds| seconds.min(longest)),
|picks| picks.len() as f64,
);
let run = Run {
settings,
windows_share: if segments.is_some() {
let jobs = grid::jobs(
opus_rate.is_some(),
settings.thresholds.scan_other_rate,
settings.thresholds.scan_wma,
);
windows / (windows + jobs as f64 * SCAN_JOB_WINDOWS)
} else {
1.0
},
};
let stretches_first = picks.is_some() && decoder.seekable;
if stretches_first && let Some(segments) = &mut segments {
read_segments(&mut decoder, segments, settings, &mut measured)?;
}
let reached = match picks {
Some(picks) => {
measured.sampled = true;
sampled(
&mut decoder,
&mut measuring,
&mut buffer,
&picks,
&mut segments,
&run,
&mut measured,
)?
}
None => whole(
&mut decoder,
&mut measuring,
&mut buffer,
Windows::Upto(longest as u64),
&mut segments,
&run,
&mut measured,
)?,
};
match reached {
Reached::End(seconds) => {
measured.duration = seconds;
if let Some(stated) = stated
&& stated - seconds > WHOLE_WITHIN
{
let past = past_bytes(&decoder, stated, seconds, &mut measured).is_some();
if !past && decoder.length_is_counted() && decoder.stopped.is_none() {
debug!(
stated,
read = seconds,
"the file holds less audio than it states"
);
measured.problems.push(Limitation::AudioMissing {
seconds: stated - seconds,
});
}
}
}
Reached::Short(seconds) if stated.is_none() => measured.duration = seconds,
Reached::Short(_) | Reached::Sampled => {
let read = measured.windows.last().map_or(0.0, |w| w.start + 1.0);
if let Some(held) = past_bytes(&decoder, measured.duration, read, &mut measured) {
measured.duration = held;
}
}
}
let ended = !measured
.problems
.iter()
.any(|problem| matches!(problem, Limitation::LengthNotStated { .. }));
if unscanned
&& decoder.seekable
&& decoder.ticks_fit_frames()
&& ended
&& let Reached::End(seconds) = reached
{
segments = Segments::of((seconds * rate as f64).round() as u64);
}
if let Some(segments) = &mut segments
&& !segments.complete()
&& (measured.sampled || unscanned)
&& !stretches_first
{
read_segments(&mut decoder, segments, settings, &mut measured)?;
}
measured.bits_used = decoder.bits_used();
measured.opus = decoder.opus_uses();
if !measured.claimed.lossless {
measured.bitrate_kbps = decoder.bitrate_kbps();
}
if decoder.skipped > 0 {
measured.problems.push(Limitation::PacketsSkipped {
count: decoder.skipped,
});
}
if let Some(stopped) = decoder.stopped.take() {
measured.problems.push(Limitation::Decoder(stopped));
}
if decoder.other_tracks > 0 {
measured.problems.push(Limitation::OtherTracks {
tracks: decoder.other_tracks + 1,
});
}
if let Some(bytes) = decoder.left_out {
measured.problems.push(Limitation::Decoder(format!(
"reading stopped at the last Ogg page, {bytes} bytes before the end of the file"
)));
}
if unscanned && segments.is_none() {
measured.problems.push(Limitation::GridNotScanned);
}
if let Some(segments) = &mut segments {
let opus = opus_rate
.zip(measuring.uncut.as_ref())
.map(|(rate, uncut)| grid::Opus {
rate,
bits,
uncut: uncut.counted(),
uncut_short: uncut.short_counted(),
unfiltered: uncut.unfiltered(),
});
measured.grid = scan_grid(segments, opus, &run, &mut measured)?;
}
settings.report(1.0);
debug!(
windows = measured.windows.len(),
sampled = measured.sampled,
skipped = decoder.skipped,
"audio measured"
);
Ok(measured)
}
fn read_segments(
decoder: &mut Decoder,
segments: &mut Segments,
settings: &Settings,
measured: &mut Measured,
) -> Result<(), Error> {
if !decoder.ticks_fit_frames() {
return Ok(());
}
let channels = decoder.claimed.channels as usize;
let mut samples = Vec::new();
for index in segments.missing() {
if segments.untouched(index)
&& let Some(first) = segments.needs(index)
{
if halted(settings)? {
return Ok(());
}
let sought = decoder.seek_frame(first);
if !matches!(sought, Ok(true)) && halted(settings)? {
return Ok(());
}
match sought {
Ok(true) => {}
Ok(false) => {
debug!(
frame = first,
"a grid segment is not among the packets indexed"
);
return Ok(());
}
Err(problem) => {
debug!(problem, frame = first, "seeking to a grid segment failed");
measured.problems.push(Limitation::Decoder(problem));
return Ok(());
}
}
}
while let Some(next) = segments.needs(index) {
if halted(settings)? {
return Ok(());
}
let Some(frames) = decoder.next_interleaved(&mut samples) else {
if halted(settings)? {
decoder.stopped = None;
}
return Ok(());
};
if frames.start > next {
debug!(frame = next, "a grid segment's packets leave a gap");
return Ok(());
}
segments.take_interleaved(frames.start, &samples, channels);
}
}
Ok(())
}
fn scan_grid(
segments: &mut Segments,
opus: Option<grid::Opus>,
run: &Run<'_>,
measured: &mut Measured,
) -> Result<Option<Scan>, Error> {
let settings = run.settings;
if !segments.complete() {
debug!("the grid segments were not all read");
measured.problems.push(Limitation::GridScanIncomplete);
return Ok(None);
}
let replaced = segments.settle();
if replaced > 0 {
debug!(
replaced,
"clipped or quiet grid segments gave way to louder stretches"
);
}
let clipped = segments.clipped();
if clipped > 0 {
measured
.problems
.push(Limitation::ClippedStretches { stretches: clipped });
}
let ended = AtomicUsize::new(0);
let other_rate = settings.thresholds.scan_other_rate;
let wma = settings.thresholds.scan_wma;
let jobs = grid::jobs(opus.is_some(), other_rate, wma);
let scan = grid::scan(
segments,
measured.claimed.sample_rate,
opus,
other_rate,
wma,
|| !is_canceled(settings.canceled.as_ref()) && !is_past(settings.deadline),
|| {
let ended = ended.fetch_add(1, Ordering::Relaxed) + 1;
run.scan(ended as f64 / jobs as f64);
},
);
if scan.is_none() && is_canceled(settings.canceled.as_ref()) {
return Err(Error::Canceled);
}
if scan.is_none() {
debug!("grid scan out of time");
measured.problems.push(Limitation::GridScanOutOfTime);
}
Ok(scan)
}
fn picks(seconds: f64, count: usize) -> BTreeSet<u64> {
let last = (seconds - 1.0).max(0.0);
let starts = last as u64 + 1;
if count as u64 >= starts {
return (0..starts).collect();
}
(0..count)
.map(|i| {
let at = last * i as f64 / (count.max(2) - 1) as f64;
at.floor() as u64
})
.collect()
}
fn stop(settings: &Settings, measured: &mut Measured) -> Result<bool, Error> {
let halted = halted(settings)?;
if halted {
debug!(windows = measured.windows.len(), "analysis out of time");
measured.problems.push(Limitation::OutOfTime);
}
Ok(halted)
}
fn halted(settings: &Settings) -> Result<bool, Error> {
if is_canceled(settings.canceled.as_ref()) {
return Err(Error::Canceled);
}
Ok(is_past(settings.deadline))
}
fn stopped_by_us(
decoder: &mut Decoder,
settings: &Settings,
measured: &mut Measured,
) -> Result<bool, Error> {
let ours = stop(settings, measured)?;
if ours {
decoder.stopped = None;
}
Ok(ours)
}
const WHOLE_WITHIN: f64 = 0.01;
enum Reached {
End(f64),
Short(f64),
Sampled,
}
fn past_bytes(decoder: &Decoder, seconds: f64, read: f64, measured: &mut Measured) -> Option<f64> {
let most = decoder.most_seconds().filter(|most| seconds > *most)?;
debug!(
seconds,
most, "the stated length is past what the file's bytes hold"
);
measured.problems.push(Limitation::LengthPastBytes);
Some(most.max(read))
}
fn usable(filled: &Filled, rate: usize) -> bool {
match filled {
Filled::Full => true,
Filled::End(length) => *length >= FFT_SIZE.min(rate) && length * 2 >= rate,
}
}
enum Windows<'a> {
Upto(u64),
Only(&'a BTreeSet<u64>),
}
fn whole(
decoder: &mut Decoder,
measuring: &mut Measuring<'_>,
buffer: &mut [Vec<f32>],
read: Windows<'_>,
segments: &mut Option<Segments>,
run: &Run<'_>,
measured: &mut Measured,
) -> Result<Reached, Error> {
let settings = run.settings;
let rate = decoder.claimed.sample_rate as usize;
let stated = decoder
.claimed
.frames
.map(|frames| frames as f64 / rate as f64);
let expected = match read {
Windows::Upto(longest) => stated.map_or(longest as f64, |s| s.min(longest as f64)),
Windows::Only(picks) => picks.last().map_or(1.0, |last| (last + 1) as f64),
};
let mut second = 0u64;
loop {
run.windows(second as f64 / expected.max(1.0));
if stop(settings, measured)? {
return Ok(Reached::Short(second as f64));
}
let filled = decoder.fill(buffer, rate);
if filled != Filled::Full && stopped_by_us(decoder, settings, measured)? {
return Ok(Reached::Short(second as f64));
}
if let Windows::Upto(longest) = read
&& second >= longest
{
if filled == Filled::End(0) {
return Ok(Reached::End(second as f64));
}
debug!(
seconds = second,
"reading in full stopped at the longest length read in full"
);
measured.problems.push(match decoder.claimed.frames {
None => Limitation::LengthNotStated { seconds: second },
Some(_) => Limitation::LongerThanStated { seconds: second },
});
return Ok(Reached::End(second as f64));
}
let length = match filled {
Filled::Full => rate,
Filled::End(length) => length,
};
if let Some(segments) = segments {
segments.take(second * rate as u64, buffer);
}
let wanted = match read {
Windows::Upto(_) => true,
Windows::Only(picks) => picks.contains(&second),
};
if usable(&filled, rate) && wanted {
measuring.window(second, buffer, rate, measured);
}
if filled != Filled::Full {
return Ok(Reached::End(second as f64 + length as f64 / rate as f64));
}
second += 1;
}
}
fn sampled(
decoder: &mut Decoder,
measuring: &mut Measuring<'_>,
buffer: &mut [Vec<f32>],
picks: &BTreeSet<u64>,
segments: &mut Option<Segments>,
run: &Run<'_>,
measured: &mut Measured,
) -> Result<Reached, Error> {
let settings = run.settings;
let rate = decoder.claimed.sample_rate as usize;
if seeking_decodes_more(picks, decoder.seek_decodes, rate as u64) {
debug!("seeking to the windows decodes more than reading through to them");
return through(decoder, measuring, buffer, picks, segments, run, measured);
}
let mut furthest = 0.0;
for (index, &second) in picks.iter().enumerate() {
run.windows(index as f64 / picks.len() as f64);
if stop(settings, measured)? {
return Ok(Reached::Sampled);
}
if let Some(held) = past_bytes(decoder, (second + 1) as f64, furthest, measured) {
return Ok(Reached::End(held));
}
if let Err(problem) = decoder.seek(second as f64) {
if stopped_by_us(decoder, settings, measured)? {
return Ok(Reached::Sampled);
}
if index > 0 {
debug!(problem, second, "seeking failed part way, sampling stopped");
measured.problems.push(Limitation::Decoder(problem));
return Ok(Reached::End(furthest));
}
debug!(problem, "the file cannot be sought in, reading it through");
return through(decoder, measuring, buffer, picks, segments, run, measured);
}
let filled = decoder.fill(buffer, rate);
let length = match filled {
Filled::Full => rate,
Filled::End(length) => length,
};
if filled != Filled::Full {
if stopped_by_us(decoder, settings, measured)? {
return Ok(Reached::Sampled);
}
if length > 0 {
furthest = second as f64 + length as f64 / rate as f64;
}
if usable(&filled, rate) {
measuring.window(second, buffer, rate, measured);
}
debug!(second, "the file ended before its last pick");
return Ok(Reached::End(furthest));
}
furthest = (second + 1) as f64;
measuring.window(second, buffer, rate, measured);
}
Ok(Reached::Sampled)
}
fn through(
decoder: &mut Decoder,
measuring: &mut Measuring<'_>,
buffer: &mut [Vec<f32>],
picks: &BTreeSet<u64>,
segments: &mut Option<Segments>,
run: &Run<'_>,
measured: &mut Measured,
) -> Result<Reached, Error> {
let read = Windows::Only(picks);
match whole(decoder, measuring, buffer, read, segments, run, measured)? {
Reached::Short(_) => Ok(Reached::Sampled),
reached => Ok(reached),
}
}
fn seeking_decodes_more(picks: &BTreeSet<u64>, unit: u64, rate: u64) -> bool {
if unit == 0 {
return false;
}
let seeking: u128 = picks
.iter()
.map(|second| u128::from(rate) + u128::from(second.saturating_mul(rate) % unit))
.sum();
let through = picks
.last()
.map_or(0, |last| u128::from(last + 1) * u128::from(rate));
seeking > through
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_panic_in_an_analysis_is_an_error() {
let failed = guarded(|| panic!("a decoder failed"));
assert!(matches!(failed, Err(Error::Panicked)), "{failed:?}");
}
#[test]
fn a_file_of_long_packets_is_read_through_when_seeking_would_decode_more() {
let (frame, rate) = (1_179_648, 44_100);
assert!(seeking_decodes_more(&picks(34.0, 20), frame, rate));
assert!(!seeking_decodes_more(
&picks(6.0 * 3600.0, 240),
frame,
rate
));
assert!(!seeking_decodes_more(&picks(600.0, 20), 73_728, rate));
assert!(!seeking_decodes_more(&picks(34.0, 20), 0, rate));
}
#[test]
fn more_windows_than_a_file_has_seconds_pick_each_second_once() {
assert_eq!(picks(100.0, 3), BTreeSet::from([0, 49, 99]));
assert_eq!(picks(100.0, 1), BTreeSet::from([0]));
assert_eq!(picks(34.5, 34), (0..34).collect());
assert_eq!(picks(34.5, usize::MAX), (0..34).collect());
assert_eq!(picks(0.4, usize::MAX), BTreeSet::from([0]));
}
}