Skip to main content

wedeo_format/
demuxer.rs

1use bitflags::bitflags;
2
3use wedeo_codec::decoder::CodecParameters;
4use wedeo_core::error::Result;
5use wedeo_core::metadata::Metadata;
6use wedeo_core::packet::Packet;
7use wedeo_core::rational::Rational;
8
9use crate::io::BufferedIo;
10
11bitflags! {
12    /// Input format flags.
13    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
14    pub struct InputFormatFlags: u32 {
15        const NOFILE       = 1 << 0;
16        const NEEDNUMBER   = 1 << 1;
17        const NOBINSEARCH  = 1 << 3;
18        const NOGENSEARCH  = 1 << 4;
19        const NOBYTESEEK   = 1 << 5;
20        const SEEK_TO_PTS  = 1 << 26;
21    }
22}
23
24bitflags! {
25    /// Seek flags.
26    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
27    pub struct SeekFlags: u32 {
28        const BACKWARD = 1;
29        const BYTE     = 2;
30        const ANY      = 4;
31        const FRAME    = 8;
32    }
33}
34
35/// Discard level for stream packets.
36#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
37#[repr(i32)]
38pub enum Discard {
39    None = -16,
40    Default = 0,
41    NonRef = 8,
42    Bidir = 16,
43    NonIntra = 24,
44    NonKey = 32,
45    All = 48,
46}
47
48/// Data used for format probing.
49#[derive(Debug)]
50pub struct ProbeData<'a> {
51    pub filename: &'a str,
52    pub buf: &'a [u8],
53}
54
55/// Probe score thresholds.
56pub const PROBE_SCORE_EXTENSION: i32 = 50;
57pub const PROBE_SCORE_MAX: i32 = 100;
58
59/// A stream within a demuxed file.
60#[derive(Debug, Clone)]
61pub struct Stream {
62    pub index: usize,
63    pub codec_params: CodecParameters,
64    pub time_base: Rational,
65    pub duration: i64,
66    pub nb_frames: i64,
67    pub metadata: Metadata,
68    pub discard: Discard,
69}
70
71impl Stream {
72    pub fn new(index: usize, codec_params: CodecParameters) -> Self {
73        Self {
74            index,
75            codec_params,
76            time_base: Rational::new(0, 1),
77            duration: 0,
78            nb_frames: 0,
79            metadata: Metadata::new(),
80            discard: Discard::Default,
81        }
82    }
83}
84
85/// Result of reading a demuxer header.
86pub struct DemuxerHeader {
87    pub streams: Vec<Stream>,
88    pub metadata: Metadata,
89    pub duration: i64,
90    pub start_time: i64,
91}
92
93/// Demuxer trait — the main abstraction for all demuxers.
94pub trait Demuxer: Send {
95    /// Read the file header, returning streams and metadata.
96    fn read_header(&mut self, io: &mut BufferedIo) -> Result<DemuxerHeader>;
97
98    /// Read the next packet from the file.
99    fn read_packet(&mut self, io: &mut BufferedIo) -> Result<Packet>;
100
101    /// Seek to a timestamp.
102    fn seek(
103        &mut self,
104        io: &mut BufferedIo,
105        stream_index: usize,
106        timestamp: i64,
107        flags: SeekFlags,
108    ) -> Result<()>;
109}
110
111/// Descriptor for an input format.
112#[derive(Debug, Clone)]
113pub struct InputFormatDescriptor {
114    pub name: &'static str,
115    pub long_name: &'static str,
116    pub extensions: &'static str,
117    pub mime_types: &'static str,
118    pub flags: InputFormatFlags,
119    /// Priority for probe tie-breaking. Higher priority wins when multiple
120    /// demuxers return the same probe score. Native implementations use 100,
121    /// wrapper/adapter implementations use 50.
122    pub priority: i32,
123}