x264_next/
data.rs

1use core::marker::PhantomData;
2use core::slice;
3use x264::*;
4
5//TODO: Iterator over the units.
6
7/// The encoded data, to be used in chunks or in its entirety.
8pub struct Data<'a> {
9    ptr: *mut x264_nal_t,
10    len: usize,
11    spooky: PhantomData<&'a [x264_nal_t]>,
12}
13
14impl<'a> Data<'a> {
15    #[doc(hidden)]
16    pub unsafe fn from_raw_parts(ptr: *mut x264_nal_t, len: usize) -> Self {
17        Data {
18            ptr,
19            len,
20            spooky: PhantomData,
21        }
22    }
23
24    /// The length (in NAL units, **not** in bytes) of this data sequence.
25    pub fn len(&self) -> usize {
26        self.len
27    }
28
29    //TODO: Use index trait(s) once IndexMove becomes a thing.
30
31    /// The `i`th unit.
32    ///
33    /// # Panics
34    ///
35    /// Panics if `i` is out-of-bounds. In order to be within the bounds,
36    /// `i` must be less than `len`.
37    pub fn unit(&self, i: usize) -> Unit<'a> {
38        const D: i32 = Priority::Disposable as i32;
39        const L: i32 = Priority::Low as i32;
40        const H: i32 = Priority::High as i32;
41
42        assert!(i < self.len);
43
44        let nal = unsafe { *self.ptr.add(i) };
45
46        Unit {
47            priority: match nal.i_ref_idc {
48                D => Priority::Disposable,
49                L => Priority::Low,
50                H => Priority::High,
51                _ => Priority::Highest,
52            },
53            payload: unsafe { slice::from_raw_parts(nal.p_payload, nal.i_payload as usize) },
54        }
55    }
56
57    /// The entire chunk of data, as one big byte-slice.
58    pub fn entirety(&self) -> &[u8] {
59        if self.len == 0 {
60            &[]
61        } else {
62            let (a, b) = unsafe {
63                let a = *self.ptr;
64                let b = *self.ptr.add(self.len - 1);
65                (a, b)
66            };
67
68            let start = a.p_payload;
69            let length = b.p_payload as usize + b.i_payload as usize - start as usize;
70
71            unsafe { slice::from_raw_parts(start, length) }
72        }
73    }
74}
75
76/// A single NAL unit.
77pub struct Unit<'a> {
78    priority: Priority,
79    payload: &'a [u8],
80}
81
82impl<'a> Unit<'a> {
83    /// How crucial this unit is regarding the decoding of the video.
84    pub fn priority(&self) -> Priority {
85        self.priority
86    }
87}
88
89impl<'a> AsRef<[u8]> for Unit<'a> {
90    fn as_ref(&self) -> &[u8] {
91        self.payload
92    }
93}
94
95#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
96#[repr(i32)]
97/// The importance of a given unit.
98pub enum Priority {
99    /// Not important at all.
100    Disposable = 0,
101    /// Not very important.
102    Low = 1,
103    /// Pretty important.
104    High = 2,
105    /// Extremely important.
106    Highest = 3,
107}