ffmpeg_the_third/codec/packet/
borrow.rs

1use std::mem;
2use std::ptr;
3
4use super::Ref;
5use crate::ffi::*;
6use libc::c_int;
7
8pub struct Borrow<'a> {
9    packet: AVPacket,
10    data: &'a [u8],
11}
12
13impl<'a> Borrow<'a> {
14    pub fn new(data: &[u8]) -> Borrow<'_> {
15        unsafe {
16            let mut packet: AVPacket = mem::zeroed();
17
18            packet.data = data.as_ptr() as *mut _;
19            packet.size = data.len() as c_int;
20
21            Borrow { packet, data }
22        }
23    }
24
25    #[inline]
26    pub fn size(&self) -> usize {
27        self.packet.size as usize
28    }
29
30    #[inline]
31    pub fn data(&self) -> Option<&[u8]> {
32        Some(self.data)
33    }
34}
35
36impl<'a> Ref for Borrow<'a> {
37    fn as_ptr(&self) -> *const AVPacket {
38        &self.packet
39    }
40}
41
42impl<'a> Drop for Borrow<'a> {
43    fn drop(&mut self) {
44        unsafe {
45            self.packet.data = ptr::null_mut();
46            self.packet.size = 0;
47
48            av_packet_unref(&mut self.packet);
49        }
50    }
51}