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
use std::{fmt, ops::Drop, ptr::NonNull};

use crate::{avutil::AVRational, ffi, shared::*};

wrap!(AVPacket: ffi::AVPacket);
settable!(AVPacket { stream_index: i32 });

impl AVPacket {
    /// Create an [`AVPacket`] and set its fields to default values.
    pub fn new() -> Self {
        let packet = unsafe { ffi::av_packet_alloc() };
        unsafe { Self::from_raw(NonNull::new(packet).unwrap()) }
    }

    /// Convert valid timing fields (timestamps / durations) in a packet from
    /// one timebase to another. Timestamps with unknown values
    /// (`AV_NOPTS_VALUE`) will be ignored.
    pub fn rescale_ts(&mut self, from: AVRational, to: AVRational) {
        unsafe {
            ffi::av_packet_rescale_ts(self.as_mut_ptr(), from, to);
        }
    }
}

impl fmt::Debug for AVPacket {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AVPacket")
            .field("pts", &self.pts)
            .field("dts", &self.dts)
            .field("size", &self.size)
            .field("stream_index", &self.stream_index)
            .field("flags", &self.flags)
            .field("duration", &self.duration)
            .field("pos", &self.pos)
            .finish()
    }
}

impl Default for AVPacket {
    fn default() -> Self {
        Self::new()
    }
}

impl Drop for AVPacket {
    /// Free the packet, if the packet is reference counted, it will be
    /// unreferenced first.
    fn drop(&mut self) {
        let mut packet = self.as_mut_ptr();
        unsafe {
            ffi::av_packet_free(&mut packet);
        }
    }
}