Skip to main content

TimeBase

Struct TimeBase 

Source
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: Rational

Implementations§

Source§

impl TimeBase

Source

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

Source

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.

Source

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.

Source

pub const fn den(&self) -> i64

den of the underlying Rational. Sugar over tb.0.den.

Source

pub fn as_rational(&self) -> Rational

Source

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.

Source

pub fn seconds_of(&self, ticks: i64) -> f64

Convert a tick count in this time base to seconds.

Source

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

pub fn rescale(&self, ts: i64, target: TimeBase) -> i64

Rescale a timestamp from this time base to another.

Source§

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.

Source

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

Source

pub const MILLIS: TimeBase

1/1000 — millisecond ticks (Matroska / WebM Timecode default).

Source

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

Source

pub const NANOS: TimeBase

1/1_000_000_000 — nanosecond ticks.

Source

pub const MPEG_TS: TimeBase

1/90000 — 90 kHz, the MPEG-TS / RTP video PTS clock.

Source

pub const AUDIO_48K: TimeBase

1/48000 — 48 kHz audio sample-clock (Opus, AC-3, most modern AAC, DTS).

Source

pub const AUDIO_44K1: TimeBase

1/44100 — 44.1 kHz audio sample-clock (CD audio, MP3 at 44.1, many FLAC streams).

Source

pub const AUDIO_8K: TimeBase

1/8000 — 8 kHz audio sample-clock (G.711, G.722, G.729, AMR-NB).

Trait Implementations§

Source§

impl Clone for TimeBase

Source§

fn clone(&self) -> TimeBase

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for TimeBase

Source§

impl Debug for TimeBase

Source§

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

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

impl Eq for TimeBase

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 PartialEq for TimeBase

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 StructuralPartialEq 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.