Skip to main content

wedeo_core/
packet.rs

1use bitflags::bitflags;
2
3use crate::buffer::Buffer;
4use crate::timestamp::NOPTS_VALUE;
5
6bitflags! {
7    /// Packet flags, matching FFmpeg's AV_PKT_FLAG_*.
8    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
9    pub struct PacketFlags: u32 {
10        const KEY     = 0x0001;
11        const CORRUPT = 0x0002;
12        const DISCARD = 0x0004;
13        const TRUSTED = 0x0008;
14        const DISPOSABLE = 0x0010;
15    }
16}
17
18/// Packet side data type, matching FFmpeg's AVPacketSideDataType.
19/// Values are sequential starting from 0, matching libavcodec/packet.h.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
21#[repr(u32)]
22pub enum PacketSideDataType {
23    Palette = 0,
24    NewExtradata = 1,
25    ParamChange = 2,
26    H263MbInfo = 3,
27    ReplayGain = 4,
28    DisplayMatrix = 5,
29    Stereo3d = 6,
30    AudioServiceType = 7,
31    QualityStats = 8,
32    FallbackTrack = 9,
33    CpbProperties = 10,
34    SkipSamples = 11,
35    JpDualmono = 12,
36    StringsMetadata = 13,
37    SubtitlePosition = 14,
38    MatroskaBlockadditional = 15,
39    WebvttIdentifier = 16,
40    WebvttSettings = 17,
41    MetadataUpdate = 18,
42    MpegtsStreamId = 19,
43    MasteringDisplayMetadata = 20,
44    Spherical = 21,
45    ContentLightLevel = 22,
46    A53Cc = 23,
47    EncryptionInitInfo = 24,
48    EncryptionInfo = 25,
49    Afd = 26,
50    Prft = 27,
51    IccProfile = 28,
52    DoviConf = 29,
53    S12mTimecode = 30,
54    DynamicHdr10Plus = 31,
55    IamfMixGainParam = 32,
56    IamfDemixingInfoParam = 33,
57    IamfReconGainInfoParam = 34,
58    AmbientViewingEnvironment = 35,
59    FrameCropping = 36,
60    Lcevc = 37,
61    ReferenceDisplays3d = 38,
62    RtcpSr = 39,
63    Exif = 40,
64}
65
66/// A piece of side data attached to a packet.
67#[derive(Debug, Clone)]
68pub struct PacketSideData {
69    pub data_type: PacketSideDataType,
70    pub data: Vec<u8>,
71}
72
73/// Encoded packet, matching FFmpeg's AVPacket concept.
74#[derive(Debug, Clone)]
75pub struct Packet {
76    /// Compressed data buffer.
77    pub data: Buffer,
78    /// Presentation timestamp in time_base units.
79    pub pts: i64,
80    /// Decompression timestamp in time_base units.
81    pub dts: i64,
82    /// Duration in time_base units.
83    pub duration: i64,
84    /// Stream index this packet belongs to.
85    pub stream_index: usize,
86    pub flags: PacketFlags,
87    /// Position in the stream (byte offset), or -1 if unknown.
88    pub pos: i64,
89    pub side_data: Vec<PacketSideData>,
90    /// Number of decoded samples to trim from the start of the decoded packet
91    /// (encoder delay / priming). Used for gapless playback.
92    pub trim_start: u32,
93    /// Number of decoded samples to trim from the end of the decoded packet
94    /// (encoder padding). Used for gapless playback.
95    pub trim_end: u32,
96}
97
98impl Packet {
99    /// Create a new empty packet.
100    pub fn new() -> Self {
101        Self {
102            data: Buffer::new(0),
103            pts: NOPTS_VALUE,
104            dts: NOPTS_VALUE,
105            duration: 0,
106            stream_index: 0,
107            flags: PacketFlags::empty(),
108            pos: -1,
109            side_data: Vec::new(),
110            trim_start: 0,
111            trim_end: 0,
112        }
113    }
114
115    /// Create a packet from raw data.
116    pub fn from_slice(data: &[u8]) -> Self {
117        Self {
118            data: Buffer::from_slice(data),
119            pts: NOPTS_VALUE,
120            dts: NOPTS_VALUE,
121            duration: 0,
122            stream_index: 0,
123            flags: PacketFlags::empty(),
124            pos: -1,
125            side_data: Vec::new(),
126            trim_start: 0,
127            trim_end: 0,
128        }
129    }
130
131    /// Get the size of the packet data.
132    pub fn size(&self) -> usize {
133        self.data.size()
134    }
135}
136
137impl Default for Packet {
138    fn default() -> Self {
139        Self::new()
140    }
141}