Skip to main content

motioncam_decoder/
container.rs

1use crate::error::McRawError;
2
3/// Types of items stored in the MCRAW container.
4#[repr(u32)]
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum ItemType {
7    BufferIndex = 0,
8    BufferIndexData = 1,
9    Buffer = 2,
10    Metadata = 3,
11    AudioIndex = 4,
12    AudioData = 5,
13    AudioDataMetadata = 6,
14}
15
16impl TryFrom<u32> for ItemType {
17    type Error = McRawError;
18    fn try_from(v: u32) -> std::result::Result<Self, Self::Error> {
19        match v {
20            0 => Ok(ItemType::BufferIndex),
21            1 => Ok(ItemType::BufferIndexData),
22            2 => Ok(ItemType::Buffer),
23            3 => Ok(ItemType::Metadata),
24            4 => Ok(ItemType::AudioIndex),
25            5 => Ok(ItemType::AudioData),
26            6 => Ok(ItemType::AudioDataMetadata),
27            _ => Err(McRawError::InvalidItemType(v)),
28        }
29    }
30}
31
32/// A typed item header in the container stream.
33#[derive(Debug, Clone, Copy)]
34pub struct Item {
35    /// Type of the item.
36    pub item_type: ItemType,
37    /// Size of the item payload in bytes.
38    pub size: u32,
39}
40
41/// Offset and timestamp for a buffer (frame or audio) in the file.
42#[derive(Debug, Clone, Copy)]
43pub struct BufferOffset {
44    /// Byte offset in the file.
45    pub offset: i64,
46    /// Presentation timestamp.
47    pub timestamp: i64,
48}
49
50/// Index entry describing the frame index section.
51#[derive(Debug, Clone, Copy)]
52pub struct BufferIndex {
53    /// Magic number for validation (`INDEX_MAGIC_NUMBER`).
54    pub magic_number: u32,
55    /// Number of offset entries.
56    pub num_offsets: i32,
57    /// File offset where index data begins.
58    pub index_data_offset: i64,
59}
60
61/// Index entry for audio data.
62#[derive(Debug, Clone, Copy)]
63pub struct AudioIndex {
64    /// Number of audio chunk offsets.
65    pub num_offsets: i64,
66    /// Start timestamp in milliseconds.
67    pub start_timestamp_ms: i64,
68}
69
70/// Per-chunk audio metadata.
71#[derive(Debug, Clone, Copy)]
72pub struct AudioMetadata {
73    /// Timestamp of this audio chunk in nanoseconds.
74    pub timestamp_ns: i64,
75}
76
77/// Magic bytes identifying an MCRAW container (`"MOTION "`).
78pub const CONTAINER_ID: [u8; 7] = *b"MOTION ";
79/// Current container version supported by this decoder.
80pub const CONTAINER_VERSION: u8 = 3;
81/// Magic number used to validate frame index data.
82pub const INDEX_MAGIC_NUMBER: u32 = 0x8A905612;