ffav_sys/avutil/
mathematics.rs

1use crate::AVRounding;
2
3impl From<AVRounding> for u32 {
4    fn from(v: AVRounding) -> u32 {
5        unsafe { std::mem::transmute::<AVRounding, u32>(v) }
6    }
7}
8
9impl Default for AVRounding {
10    fn default() -> Self {
11        AVRounding::new()
12    }
13}
14
15impl AVRounding {
16    /// Create an new AVRounding with Round toward zero.
17    #[inline]
18    pub fn new() -> Self {
19        AVRounding::AV_ROUND_ZERO
20    }
21
22    /// Round toward zero.
23    #[inline]
24    pub fn zero(self) -> Self {
25        unsafe { std::mem::transmute::<u32, AVRounding>(0) }
26    }
27
28    /// Round away from zero.
29    #[inline]
30    pub fn inf(self) -> Self {
31        unsafe { std::mem::transmute::<u32, AVRounding>(self as u32 | 1) }
32    }
33
34    /// Round toward -infinity.
35    #[inline]
36    pub fn down(self) -> Self {
37        unsafe { std::mem::transmute::<u32, AVRounding>(self as u32 | 2) }
38    }
39
40    /// Round toward +infinity.
41    #[inline]
42    pub fn up(self) -> Self {
43        unsafe { std::mem::transmute::<u32, AVRounding>(self as u32 | 3) }
44    }
45
46    /// Round to nearest and halfway cases away from zero.
47    #[inline]
48    pub fn near_inf(self) -> Self {
49        unsafe { std::mem::transmute::<u32, AVRounding>(self as u32 | 5) }
50    }
51
52    /// Flag telling rescaling functions to pass INT64_MIN/MAX through unchanged, avoiding special cases for AV_NOPTS_VALUE.
53    ///
54    /// Unlike other values of the enumeration AVRounding, this value is a bitmask that must be used in conjunction with another value of the enumeration through a bitwise OR, in order to set behavior for normal cases.
55    #[inline]
56    pub fn pass_min_max(self) -> Self {
57        unsafe { std::mem::transmute::<u32, AVRounding>(self as u32 | 8192) }
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn test_avrounding() {
67        assert_eq!(
68            std::mem::size_of::<AVRounding>(),
69            std::mem::size_of::<u32>()
70        );
71        assert_eq!(AVRounding::new(), AVRounding::AV_ROUND_ZERO);
72        assert_eq!(AVRounding::new().zero(), AVRounding::AV_ROUND_ZERO);
73        assert_eq!(AVRounding::new().inf(), AVRounding::AV_ROUND_INF);
74        assert_eq!(AVRounding::new().down(), AVRounding::AV_ROUND_DOWN);
75        assert_eq!(AVRounding::new().up(), AVRounding::AV_ROUND_UP);
76        assert_eq!(AVRounding::new().near_inf(), AVRounding::AV_ROUND_NEAR_INF);
77        assert_eq!(
78            AVRounding::new().pass_min_max(),
79            AVRounding::AV_ROUND_PASS_MINMAX
80        );
81        assert_eq!(AVRounding::new().near_inf().pass_min_max() as u32, 8197);
82    }
83}