pub struct Timebase { /* private fields */ }Expand description
A media timebase represented as a rational number: numerator over non-zero denominator.
Typical values: 1/1000 for millisecond PTS, 1/90000 for MPEG-TS,
1/48000 for audio samples, 30000/1001 for NTSC video (when used as a
frame rate).
§Equality and ordering
Comparison is value-based: 1/2 equals 2/4, and 1/3 < 2/3 < 1/1.
Hash hashes the reduced (lowest-terms) form, so equal rationals hash
the same. Cross-multiplication uses u64 intermediates — exact for any
u32 numerator / denominator.
Implementations§
Source§impl Timebase
impl Timebase
Sourcepub const fn new(num: u32, den: NonZeroU32) -> Self
pub const fn new(num: u32, den: NonZeroU32) -> Self
Creates a new Timebase with the given numerator and non-zero denominator.
Sourcepub const fn den(&self) -> NonZeroU32
pub const fn den(&self) -> NonZeroU32
Returns the denominator.
Sourcepub const fn with_den(self, den: NonZeroU32) -> Self
pub const fn with_den(self, den: NonZeroU32) -> Self
Set the value of the denominator.
Sourcepub const fn set_num(&mut self, num: u32) -> &mut Self
pub const fn set_num(&mut self, num: u32) -> &mut Self
Set the value of the numerator in place.
Sourcepub const fn set_den(&mut self, den: NonZeroU32) -> &mut Self
pub const fn set_den(&mut self, den: NonZeroU32) -> &mut Self
Set the value of the denominator in place.
Sourcepub const fn rescale_pts(pts: i64, from: Self, to: Self) -> i64
pub const fn rescale_pts(pts: i64, from: Self, to: Self) -> i64
Rescales pts from timebase from to timebase to, rounding toward zero.
Equivalent to FFmpeg’s av_rescale_q. Uses a 128-bit intermediate to
avoid overflow for typical video PTS ranges. If the rescaled value
exceeds i64’s range (pathological for real video), the result is
saturated to i64::MIN or i64::MAX — this matches the behavior
promised by duration_to_pts and avoids silent wraparound.
§Panics
Panics if to.num() == 0 (division by zero).
Sourcepub const fn rescale(&self, pts: i64, to: Self) -> i64
pub const fn rescale(&self, pts: i64, to: Self) -> i64
Rescales pts from this timebase to to, rounding toward zero.
Method form of Self::rescale_pts: self is the source timebase.
§Panics
Panics if to.num() == 0 (division by zero).
Sourcepub const fn frames_to_duration(&self, frames: u32) -> Duration
pub const fn frames_to_duration(&self, frames: u32) -> Duration
Treats self as a frame rate (frames per second) and returns the
Duration corresponding to frames frames.
Examples:
- 30 fps:
Timebase::new(30, nz(1)).frames_to_duration(15)→ 500 ms - NTSC:
Timebase::new(30000, nz(1001)).frames_to_duration(30000)→ 1001 ms
Note that “frame rate” and “PTS timebase” are conceptually different
rationals even though both are represented as Timebase. A 30 fps
stream typically has PTS timebase 1/30 (seconds per unit) and frame
rate 30/1 (frames per second) — they are reciprocals.
§Panics
Panics if self.num() == 0 (division by zero).
Sourcepub const fn duration_to_pts(&self, d: Duration) -> i64
pub const fn duration_to_pts(&self, d: Duration) -> i64
Converts a Duration into the number of PTS units this timebase
represents, rounding toward zero.
Inverse of “multiplying a PTS value by this timebase to get seconds”.
Saturates at i64::MAX if the duration is absurdly large for this
timebase. Returns 0 if self.num() == 0 (a degenerate timebase).