pub struct TimeBase(pub Rational);Expand description
A time base expressed as a rational number of seconds per tick.
A TimeBase of 1/48000 means each timestamp unit is 1/48000 second.
Tuple Fields§
§0: RationalImplementations§
Source§impl TimeBase
impl TimeBase
pub const fn new(num: i64, den: i64) -> Self
Sourcepub const fn from_rate(rate: u32) -> Self
pub const fn from_rate(rate: u32) -> Self
Construct a TimeBase representing 1/rate seconds per tick —
the canonical “sample-rate-style” base used by audio codecs
(1/48000 for 48 kHz PCM, 1/44100 for CD audio, 1/8000 for
G.711) and by the common video bases (1/90000 for MPEG-TS,
1/1000000 for microsecond PTS).
Equivalent to TimeBase::new(1, rate as i64), but reads more
clearly at call sites and documents the inverse-of-rate
convention so a reader doesn’t have to mentally swap arguments.
Sourcepub const fn num(&self) -> i64
pub const fn num(&self) -> i64
num of the underlying Rational. Sugar over tb.0.num for
callers that don’t want to reach through the tuple-struct field.
pub fn as_rational(&self) -> Rational
Sourcepub const fn is_valid(&self) -> bool
pub const fn is_valid(&self) -> bool
true when this time base is usable for rescaling — both terms
non-zero. A zero denominator denotes “no defined time base” (the
1/0 placeholder some demuxers stamp on data-only streams);
callers that want to skip rescaling on those streams can branch
on is_valid() instead of re-doing the same den != 0 && num != 0
check at every call site.
Sourcepub fn seconds_of(&self, ticks: i64) -> f64
pub fn seconds_of(&self, ticks: i64) -> f64
Convert a tick count in this time base to seconds.
Sourcepub fn ticks_of(&self, seconds: f64) -> i64
pub fn ticks_of(&self, seconds: f64) -> i64
Convert a fractional-seconds count to the nearest tick count in
this time base. The inverse of [seconds_of]: seconds_of goes
ticks → seconds; ticks_of goes seconds → ticks. Useful
for muxers and encoders that have a target wall-clock duration
and need to land it on the stream’s time base without hand-rolling
the divide-and-round at every call site.
Rounds half-away-from-zero (matches rescale). On an invalid
time base (is_valid() == false) or when the result would exceed
i64 range, returns 0 — pick a defaulted timestamp rather than
panicking, since callers are typically muxing best-effort output.
Source§impl TimeBase
Common time-base constants.
impl TimeBase
Common time-base constants.
These are the rates that show up over and over across the workspace: MPEG-TS / RTP video at 90 kHz, microsecond PTS (most demuxers’ “expose-everything” base), MKV at 1 ms, and the audio sample rates the codec crates spend most of their lives at. Naming them once removes the magic-numbers-at-call-sites that grep-fishing has to distinguish from random integer literals.
Sourcepub const SECONDS: TimeBase
pub const SECONDS: TimeBase
1/1 — one tick per second. The “no rescaling” identity base, useful for placeholders on streams without a defined cadence (e.g. one-shot SVG / image frames).
Sourcepub const MICROS: TimeBase
pub const MICROS: TimeBase
1/1_000_000 — microsecond ticks (the base most demuxers expose to consumers when they want the finest sane resolution without going to nanoseconds).
Sourcepub const AUDIO_48K: TimeBase
pub const AUDIO_48K: TimeBase
1/48000 — 48 kHz audio sample-clock (Opus, AC-3, most modern AAC, DTS).
Sourcepub const AUDIO_44K1: TimeBase
pub const AUDIO_44K1: TimeBase
1/44100 — 44.1 kHz audio sample-clock (CD audio, MP3 at 44.1, many FLAC streams).