oxideav_core/packet.rs
1//! Compressed-data packet passed between demuxer → decoder and encoder → muxer.
2
3use crate::time::TimeBase;
4
5/// Metadata flags on a packet.
6#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
7pub struct PacketFlags {
8 /// Packet is (or starts) a keyframe / random-access point.
9 pub keyframe: bool,
10 /// Packet holds codec-level headers rather than media data.
11 pub header: bool,
12 /// Packet's data may be corrupt but decode should still be attempted.
13 pub corrupt: bool,
14 /// Packet should be discarded (e.g., decoder delay padding).
15 pub discard: bool,
16 /// Packet is the last in its source container's natural framing unit
17 /// (Ogg page, MP4 chunk, MKV cluster, …). Container muxers may use this
18 /// signal to recreate similar boundaries in their output. Decoders
19 /// should ignore it.
20 pub unit_boundary: bool,
21}
22
23/// A chunk of compressed (encoded) data belonging to one stream.
24#[derive(Clone, Debug)]
25pub struct Packet {
26 /// Stream index this packet belongs to.
27 pub stream_index: u32,
28 /// Time base in which `pts` and `dts` are expressed.
29 pub time_base: TimeBase,
30 /// Presentation timestamp (display order). `None` if unknown.
31 pub pts: Option<i64>,
32 /// Decode timestamp (decode order). Often equal to `pts` for intra-only codecs.
33 pub dts: Option<i64>,
34 /// Packet duration in `time_base` units, or `None` if unknown.
35 pub duration: Option<i64>,
36 /// Flags describing this packet.
37 pub flags: PacketFlags,
38 /// Compressed payload.
39 pub data: Vec<u8>,
40}
41
42impl Packet {
43 pub fn new(stream_index: u32, time_base: TimeBase, data: Vec<u8>) -> Self {
44 Self {
45 stream_index,
46 time_base,
47 pts: None,
48 dts: None,
49 duration: None,
50 flags: PacketFlags::default(),
51 data,
52 }
53 }
54
55 pub fn with_pts(mut self, pts: i64) -> Self {
56 self.pts = Some(pts);
57 self
58 }
59
60 pub fn with_dts(mut self, dts: i64) -> Self {
61 self.dts = Some(dts);
62 self
63 }
64
65 pub fn with_duration(mut self, d: i64) -> Self {
66 self.duration = Some(d);
67 self
68 }
69
70 pub fn with_keyframe(mut self, kf: bool) -> Self {
71 self.flags.keyframe = kf;
72 self
73 }
74}