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).
Trait Implementations§
Source§impl<'a> Arbitrary<'a> for Timebase
Available on crate feature arbitrary only.
impl<'a> Arbitrary<'a> for Timebase
arbitrary only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Self from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Unstructured this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured this type
needs to construct itself. Read moreimpl Copy for Timebase
Source§impl DefaultInstance for Timebase
Available on crate feature buffa only.
impl DefaultInstance for Timebase
buffa only.Source§fn default_instance() -> &'static Self
fn default_instance() -> &'static Self
Source§impl<'de> Deserialize<'de> for Timebase
impl<'de> Deserialize<'de> for Timebase
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for Timebase
Source§impl Message for Timebase
Available on crate feature buffa only.
impl Message for Timebase
buffa only.Source§fn compute_size(&self, _cache: &mut SizeCache) -> u32
fn compute_size(&self, _cache: &mut SizeCache) -> u32
cache for write_to to consume. Read moreSource§fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut)
fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl BufMut)
cache (populated by a prior
compute_size call on the same cache). Read moreSource§fn merge_field(
&mut self,
tag: Tag,
buf: &mut impl Buf,
ctx: DecodeContext<'_>,
) -> Result<(), DecodeError>
fn merge_field( &mut self, tag: Tag, buf: &mut impl Buf, ctx: DecodeContext<'_>, ) -> Result<(), DecodeError>
buf. Read moreSource§fn encode(&self, buf: &mut impl BufMut)
fn encode(&self, buf: &mut impl BufMut)
Source§fn encode_with_cache(&self, cache: &mut SizeCache, buf: &mut impl BufMut)
fn encode_with_cache(&self, cache: &mut SizeCache, buf: &mut impl BufMut)
SizeCache, for
reuse across many encodes in a hot loop. Clears the cache first.Source§fn encoded_len(&self) -> u32
fn encoded_len(&self) -> u32
Source§fn encode_length_delimited(&self, buf: &mut impl BufMut)
fn encode_length_delimited(&self, buf: &mut impl BufMut)
Source§fn encode_to_bytes(&self) -> Bytes
fn encode_to_bytes(&self) -> Bytes
bytes::Bytes. Read moreSource§fn decode(buf: &mut impl Buf) -> Result<Self, DecodeError>where
Self: Sized,
fn decode(buf: &mut impl Buf) -> Result<Self, DecodeError>where
Self: Sized,
Source§fn decode_from_slice(data: &[u8]) -> Result<Self, DecodeError>where
Self: Sized,
fn decode_from_slice(data: &[u8]) -> Result<Self, DecodeError>where
Self: Sized,
Source§fn decode_length_delimited(buf: &mut impl Buf) -> Result<Self, DecodeError>where
Self: Sized,
fn decode_length_delimited(buf: &mut impl Buf) -> Result<Self, DecodeError>where
Self: Sized,
Source§fn merge_to_limit(
&mut self,
buf: &mut impl Buf,
ctx: DecodeContext<'_>,
limit: usize,
) -> Result<(), DecodeError>
fn merge_to_limit( &mut self, buf: &mut impl Buf, ctx: DecodeContext<'_>, limit: usize, ) -> Result<(), DecodeError>
Source§fn merge_group(
&mut self,
buf: &mut impl Buf,
ctx: DecodeContext<'_>,
field_number: u32,
) -> Result<(), DecodeError>
fn merge_group( &mut self, buf: &mut impl Buf, ctx: DecodeContext<'_>, field_number: u32, ) -> Result<(), DecodeError>
buf, reading fields until an
EndGroup tag with the given field_number is encountered. Read more