1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
use std::marker::PhantomData; use std::mem; use std::slice; use super::{Borrow, Flags, Mut, Ref, SideData}; use ffi::*; use libc::c_int; use {format, Error, Rational}; pub struct Packet(AVPacket); unsafe impl Send for Packet {} unsafe impl Sync for Packet {} impl Packet { #[inline(always)] pub unsafe fn is_empty(&self) -> bool { self.0.size == 0 } } impl Packet { #[inline] pub fn empty() -> Self { unsafe { let mut pkt: AVPacket = mem::zeroed(); av_init_packet(&mut pkt); Packet(pkt) } } #[inline] pub fn new(size: usize) -> Self { unsafe { let mut pkt: AVPacket = mem::zeroed(); av_init_packet(&mut pkt); av_new_packet(&mut pkt, size as c_int); Packet(pkt) } } #[inline] pub fn copy(data: &[u8]) -> Self { use std::io::Write; let mut packet = Packet::new(data.len()); packet.data_mut().unwrap().write_all(data).unwrap(); packet } #[inline] pub fn borrow(data: &[u8]) -> Borrow { Borrow::new(data) } #[inline] pub fn shrink(&mut self, size: usize) { unsafe { av_shrink_packet(&mut self.0, size as c_int); } } #[inline] pub fn grow(&mut self, size: usize) { unsafe { av_grow_packet(&mut self.0, size as c_int); } } #[inline] pub fn rescale_ts<S, D>(&mut self, source: S, destination: D) where S: Into<Rational>, D: Into<Rational>, { unsafe { av_packet_rescale_ts( self.as_mut_ptr(), source.into().into(), destination.into().into(), ); } } #[inline] pub fn flags(&self) -> Flags { Flags::from_bits_truncate(self.0.flags) } #[inline] pub fn set_flags(&mut self, value: Flags) { self.0.flags = value.bits(); } #[inline] pub fn is_key(&self) -> bool { self.flags().contains(Flags::KEY) } #[inline] pub fn is_corrupt(&self) -> bool { self.flags().contains(Flags::CORRUPT) } #[inline] pub fn stream(&self) -> usize { self.0.stream_index as usize } #[inline] pub fn set_stream(&mut self, index: usize) { self.0.stream_index = index as c_int; } #[inline] pub fn pts(&self) -> Option<i64> { match self.0.pts { AV_NOPTS_VALUE => None, pts => Some(pts as i64), } } #[inline] pub fn set_pts(&mut self, value: Option<i64>) { self.0.pts = value.unwrap_or(AV_NOPTS_VALUE); } #[inline] pub fn dts(&self) -> Option<i64> { match self.0.dts { AV_NOPTS_VALUE => None, dts => Some(dts as i64), } } #[inline] pub fn set_dts(&mut self, value: Option<i64>) { self.0.dts = value.unwrap_or(AV_NOPTS_VALUE); } #[inline] pub fn size(&self) -> usize { self.0.size as usize } #[inline] pub fn duration(&self) -> i64 { self.0.duration as i64 } #[inline] pub fn set_duration(&mut self, value: i64) { self.0.duration = value; } #[inline] pub fn position(&self) -> isize { self.0.pos as isize } #[inline] pub fn set_position(&mut self, value: isize) { self.0.pos = value as i64 } #[inline] pub fn convergence(&self) -> isize { self.0.convergence_duration as isize } #[inline] pub fn side_data(&self) -> SideDataIter { SideDataIter::new(&self.0) } #[inline] pub fn data(&self) -> Option<&[u8]> { unsafe { if self.0.data.is_null() { None } else { Some(slice::from_raw_parts(self.0.data, self.0.size as usize)) } } } #[inline] pub fn data_mut(&mut self) -> Option<&mut [u8]> { unsafe { if self.0.data.is_null() { None } else { Some(slice::from_raw_parts_mut(self.0.data, self.0.size as usize)) } } } #[inline] pub fn read(&mut self, format: &mut format::context::Input) -> Result<(), Error> { unsafe { match av_read_frame(format.as_mut_ptr(), self.as_mut_ptr()) { 0 => Ok(()), e => Err(Error::from(e)), } } } #[inline] pub fn write(&self, format: &mut format::context::Output) -> Result<bool, Error> { unsafe { if self.is_empty() { return Err(Error::InvalidData); } match av_write_frame(format.as_mut_ptr(), self.as_ptr() as *mut _) { 1 => Ok(true), 0 => Ok(false), e => Err(Error::from(e)), } } } #[inline] pub fn write_interleaved(&self, format: &mut format::context::Output) -> Result<(), Error> { unsafe { if self.is_empty() { return Err(Error::InvalidData); } match av_interleaved_write_frame(format.as_mut_ptr(), self.as_ptr() as *mut _) { 0 => Ok(()), e => Err(Error::from(e)), } } } } impl Ref for Packet { fn as_ptr(&self) -> *const AVPacket { &self.0 } } impl Mut for Packet { fn as_mut_ptr(&mut self) -> *mut AVPacket { &mut self.0 } } impl Clone for Packet { #[inline] fn clone(&self) -> Self { let mut pkt = Packet::empty(); pkt.clone_from(self); pkt } #[inline] fn clone_from(&mut self, source: &Self) { unsafe { av_copy_packet(&mut self.0, &source.0); } } } impl Drop for Packet { fn drop(&mut self) { unsafe { av_packet_unref(&mut self.0); } } } pub struct SideDataIter<'a> { ptr: *const AVPacket, cur: c_int, _marker: PhantomData<&'a Packet>, } impl<'a> SideDataIter<'a> { pub fn new(ptr: *const AVPacket) -> Self { SideDataIter { ptr, cur: 0, _marker: PhantomData, } } } impl<'a> Iterator for SideDataIter<'a> { type Item = SideData<'a>; fn next(&mut self) -> Option<<Self as Iterator>::Item> { unsafe { if self.cur >= (*self.ptr).side_data_elems { None } else { self.cur += 1; Some(SideData::wrap( (*self.ptr).side_data.offset((self.cur - 1) as isize), )) } } } fn size_hint(&self) -> (usize, Option<usize>) { unsafe { let length = (*self.ptr).side_data_elems as usize; (length - self.cur as usize, Some(length - self.cur as usize)) } } } impl<'a> ExactSizeIterator for SideDataIter<'a> {}