| 1 | |
| 2 | |
| 3 | use std::ops::{Bound, Deref, RangeBounds}; |
| 4 | use std::sync::Arc; |
| 5 | |
| 6 | use crate::timebase::TimeBase; |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | #[derive(Clone, Default)] |
| 15 | pub struct Buf { |
| 16 | data: Arc<[u8]>, |
| 17 | off: usize, |
| 18 | len: usize, |
| 19 | } |
| 20 | |
| 21 | impl Buf { |
| 22 | |
| 23 | pub fn new() -> Buf { |
| 24 | Buf::default() |
| 25 | } |
| 26 | |
| 27 | |
| 28 | pub fn from_vec(v: Vec<u8>) -> Buf { |
| 29 | let len = v.len(); |
| 30 | Buf { |
| 31 | data: Arc::from(v), |
| 32 | off: 0, |
| 33 | len, |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | |
| 38 | pub fn copy_from_slice(s: &[u8]) -> Buf { |
| 39 | Buf::from_vec(s.to_vec()) |
| 40 | } |
| 41 | |
| 42 | |
| 43 | pub fn len(&self) -> usize { |
| 44 | self.len |
| 45 | } |
| 46 | |
| 47 | |
| 48 | pub fn is_empty(&self) -> bool { |
| 49 | self.len == 0 |
| 50 | } |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | pub fn slice(&self, range: impl RangeBounds<usize>) -> Buf { |
| 56 | let start = match range.start_bound() { |
| 57 | Bound::Included(&n) => n, |
| 58 | Bound::Excluded(&n) => n + 1, |
| 59 | Bound::Unbounded => 0, |
| 60 | }; |
| 61 | let end = match range.end_bound() { |
| 62 | Bound::Included(&n) => n + 1, |
| 63 | Bound::Excluded(&n) => n, |
| 64 | Bound::Unbounded => self.len, |
| 65 | }; |
| 66 | assert!(start <= end && end <= self.len, "Buf::slice out of bounds"); |
| 67 | Buf { |
| 68 | data: Arc::clone(&self.data), |
| 69 | off: self.off + start, |
| 70 | len: end - start, |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | impl Deref for Buf { |
| 76 | type Target = [u8]; |
| 77 | |
| 78 | fn deref(&self) -> &[u8] { |
| 79 | &self.data[self.off..self.off + self.len] |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | impl AsRef<[u8]> for Buf { |
| 84 | fn as_ref(&self) -> &[u8] { |
| 85 | self |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | impl From<Vec<u8>> for Buf { |
| 90 | fn from(v: Vec<u8>) -> Buf { |
| 91 | Buf::from_vec(v) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | impl From<&[u8]> for Buf { |
| 96 | fn from(s: &[u8]) -> Buf { |
| 97 | Buf::copy_from_slice(s) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | impl PartialEq for Buf { |
| 102 | fn eq(&self, other: &Buf) -> bool { |
| 103 | **self == **other |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl Eq for Buf {} |
| 108 | |
| 109 | impl std::fmt::Debug for Buf { |
| 110 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 111 | write!(f, "Buf({} bytes)", self.len) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | |
| 116 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 117 | pub enum SideData { |
| 118 | |
| 119 | |
| 120 | |
| 121 | CodecConfig(Buf), |
| 122 | |
| 123 | Palette(Buf), |
| 124 | |
| 125 | DisplayMatrix(Buf), |
| 126 | |
| 127 | Custom { |
| 128 | |
| 129 | kind: u32, |
| 130 | |
| 131 | data: Buf, |
| 132 | }, |
| 133 | } |
| 134 | |
| 135 | |
| 136 | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] |
| 137 | pub struct PacketFlags { |
| 138 | |
| 139 | pub keyframe: bool, |
| 140 | |
| 141 | pub header: bool, |
| 142 | |
| 143 | pub corrupt: bool, |
| 144 | |
| 145 | pub discard: bool, |
| 146 | } |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 153 | pub struct Packet { |
| 154 | |
| 155 | pub stream: u32, |
| 156 | |
| 157 | pub time_base: TimeBase, |
| 158 | |
| 159 | pub pts: Option<i64>, |
| 160 | |
| 161 | pub dts: Option<i64>, |
| 162 | |
| 163 | pub duration: Option<i64>, |
| 164 | |
| 165 | pub flags: PacketFlags, |
| 166 | |
| 167 | pub side_data: Vec<SideData>, |
| 168 | |
| 169 | pub data: Buf, |
| 170 | } |
| 171 | |
| 172 | impl Packet { |
| 173 | |
| 174 | pub fn new(stream: u32, time_base: TimeBase, data: impl Into<Buf>) -> Packet { |
| 175 | Packet { |
| 176 | stream, |
| 177 | time_base, |
| 178 | pts: None, |
| 179 | dts: None, |
| 180 | duration: None, |
| 181 | flags: PacketFlags::default(), |
| 182 | side_data: Vec::new(), |
| 183 | data: data.into(), |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | |
| 188 | |
| 189 | pub fn with_pts(mut self, pts: i64) -> Packet { |
| 190 | self.pts = Some(pts); |
| 191 | self |
| 192 | } |
| 193 | |
| 194 | |
| 195 | pub fn with_duration(mut self, duration: i64) -> Packet { |
| 196 | self.duration = Some(duration); |
| 197 | self |
| 198 | } |
| 199 | |
| 200 | |
| 201 | pub fn end_pts(&self) -> Option<i64> { |
| 202 | self.pts?.checked_add(self.duration?) |
| 203 | } |
| 204 | |
| 205 | |
| 206 | pub fn is_keyframe(&self) -> bool { |
| 207 | self.flags.keyframe |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | #[cfg(test)] |
| 212 | mod tests { |
| 213 | use super::*; |
| 214 | |
| 215 | #[test] |
| 216 | fn buf_slices_share_one_allocation() { |
| 217 | let buf = Buf::from_vec(vec![0, 1, 2, 3, 4, 5]); |
| 218 | let mid = buf.slice(2..5); |
| 219 | assert_eq!(&*mid, &[2, 3, 4]); |
| 220 | assert_eq!(&*mid.slice(1..), &[3, 4]); |
| 221 | assert_eq!(mid.len(), 3); |
| 222 | assert!(Buf::new().is_empty()); |
| 223 | |
| 224 | let clone = mid.clone(); |
| 225 | assert_eq!(clone, mid); |
| 226 | assert_eq!(&*buf.slice(..), &[0, 1, 2, 3, 4, 5]); |
| 227 | } |
| 228 | |
| 229 | #[test] |
| 230 | #[should_panic(expected = "out of bounds")] |
| 231 | fn buf_slice_rejects_out_of_range() { |
| 232 | Buf::from_vec(vec![0, 1]).slice(0..3); |
| 233 | } |
| 234 | |
| 235 | #[test] |
| 236 | fn packet_construction() { |
| 237 | let tb = TimeBase::from_rate(48_000); |
| 238 | let mut p = Packet::new(0, tb, vec![0xff; 4]); |
| 239 | assert!(!p.is_keyframe()); |
| 240 | assert_eq!(p.end_pts(), None); |
| 241 | p = p.with_pts(1024).with_duration(1024); |
| 242 | p.flags.keyframe = true; |
| 243 | p.side_data |
| 244 | .push(SideData::CodecConfig(Buf::copy_from_slice(&[0x12, 0x10]))); |
| 245 | assert_eq!(p.end_pts(), Some(2048)); |
| 246 | assert!(p.is_keyframe()); |
| 247 | assert_eq!(p.data.len(), 4); |
| 248 | assert_eq!(p.time_base, TimeBase::new(1, 48_000)); |
| 249 | } |
| 250 | } |