Skip to main content

Timebase

Struct Timebase 

Source
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

Source

pub const fn new(num: u32, den: NonZeroU32) -> Self

Creates a new Timebase with the given numerator and non-zero denominator.

Source

pub const fn num(&self) -> u32

Returns the numerator.

Source

pub const fn den(&self) -> NonZeroU32

Returns the denominator.

Source

pub const fn with_num(self, num: u32) -> Self

Set the value of the numerator.

Source

pub const fn with_den(self, den: NonZeroU32) -> Self

Set the value of the denominator.

Source

pub const fn set_num(&mut self, num: u32) -> &mut Self

Set the value of the numerator in place.

Source

pub const fn set_den(&mut self, den: NonZeroU32) -> &mut Self

Set the value of the denominator in place.

Source

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).

Source

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).

Source

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).

Source

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).

Trait Implementations§

Source§

impl Clone for Timebase

Source§

fn clone(&self) -> Timebase

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Timebase

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Hash for Timebase

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Timebase

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Timebase

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Timebase

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for Timebase

Source§

impl Eq for Timebase

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.