oximedia-container 0.1.5

Container demuxer/muxer for OxiMedia
Documentation
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Matroska cue (seek index) writing.

#![forbid(unsafe_code)]
#![allow(clippy::cast_possible_truncation)]

use crate::demux::matroska::ebml::element_id;

// ============================================================================
// Cue Point
// ============================================================================

/// A single cue point for seeking.
#[derive(Debug, Clone)]
pub struct CuePoint {
    /// Timestamp in timecode scale units.
    pub time: u64,

    /// Track number.
    pub track: u64,

    /// Position of the cluster in the segment.
    pub cluster_position: u64,

    /// Relative position within the cluster (optional).
    pub relative_position: Option<u64>,

    /// Block number within the cluster (optional).
    pub block_number: Option<u64>,
}

impl CuePoint {
    /// Creates a new cue point.
    #[must_use]
    pub const fn new(time: u64, track: u64, cluster_position: u64) -> Self {
        Self {
            time,
            track,
            cluster_position,
            relative_position: None,
            block_number: None,
        }
    }

    /// Sets the relative position within the cluster.
    #[must_use]
    pub const fn with_relative_position(mut self, pos: u64) -> Self {
        self.relative_position = Some(pos);
        self
    }

    /// Sets the block number within the cluster.
    #[must_use]
    pub const fn with_block_number(mut self, num: u64) -> Self {
        self.block_number = Some(num);
        self
    }
}

// ============================================================================
// Cue Writer
// ============================================================================

/// Writer for Matroska cues (seek index).
///
/// Cues provide random access points for seeking in a Matroska file.
/// They map timestamps to cluster positions.
#[derive(Debug, Default)]
pub struct CueWriter {
    /// Collected cue points.
    pub cue_points: Vec<CuePoint>,

    /// Minimum interval between cue points (in timecode units).
    min_interval: u64,

    /// Last cue point timestamp per track.
    last_cue_time: Vec<u64>,
}

impl CueWriter {
    /// Creates a new cue writer.
    #[must_use]
    pub fn new() -> Self {
        Self {
            cue_points: Vec::new(),
            min_interval: 1000, // Default: 1 second at default timecode scale
            last_cue_time: Vec::new(),
        }
    }

    /// Sets the minimum interval between cue points.
    #[must_use]
    pub const fn with_min_interval(mut self, interval: u64) -> Self {
        self.min_interval = interval;
        self
    }

    /// Adds a cue point.
    ///
    /// Returns true if the cue point was added, false if it was skipped
    /// due to being too close to a previous cue point.
    pub fn add_cue_point(&mut self, time: u64, track: u64, cluster_position: u64) -> bool {
        // Ensure we have tracking for this track
        let track_idx = track as usize;
        while self.last_cue_time.len() <= track_idx {
            self.last_cue_time.push(0);
        }

        // Check if we should add a cue point (minimum interval)
        let last_time = self.last_cue_time[track_idx];
        if time > 0 && time < last_time + self.min_interval {
            return false;
        }

        self.cue_points
            .push(CuePoint::new(time, track, cluster_position));
        self.last_cue_time[track_idx] = time;
        true
    }

    /// Adds a detailed cue point with all optional fields.
    pub fn add_detailed_cue_point(
        &mut self,
        time: u64,
        track: u64,
        cluster_position: u64,
        relative_position: Option<u64>,
        block_number: Option<u64>,
    ) {
        let mut cue = CuePoint::new(time, track, cluster_position);
        if let Some(rel_pos) = relative_position {
            cue = cue.with_relative_position(rel_pos);
        }
        if let Some(block_num) = block_number {
            cue = cue.with_block_number(block_num);
        }
        self.cue_points.push(cue);
    }

    /// Builds the complete Cues element content.
    ///
    /// Returns the encoded bytes for the Cues element content
    /// (without the Cues element header).
    #[must_use]
    pub fn build(&self) -> Vec<u8> {
        let mut content = Vec::new();

        for cue in &self.cue_points {
            let cue_point = Self::build_cue_point(cue);
            content.extend(cue_point);
        }

        content
    }

    /// Builds a single `CuePoint` element.
    fn build_cue_point(cue: &CuePoint) -> Vec<u8> {
        let mut content = Vec::new();

        // CueTime
        content.extend(encode_element_id(element_id::CUE_TIME));
        let time_bytes = encode_uint(cue.time);
        content.extend(encode_vint_size(time_bytes.len() as u64));
        content.extend(time_bytes);

        // CueTrackPositions
        let track_positions = Self::build_cue_track_positions(cue);
        content.extend(encode_element_id(element_id::CUE_TRACK_POSITIONS));
        content.extend(encode_vint_size(track_positions.len() as u64));
        content.extend(track_positions);

        // Wrap in CuePoint element
        let mut result = Vec::new();
        result.extend(encode_element_id(element_id::CUE_POINT));
        result.extend(encode_vint_size(content.len() as u64));
        result.extend(content);

        result
    }

    /// Builds a `CueTrackPositions` element.
    fn build_cue_track_positions(cue: &CuePoint) -> Vec<u8> {
        let mut content = Vec::new();

        // CueTrack
        content.extend(encode_element_id(element_id::CUE_TRACK));
        let track_bytes = encode_uint(cue.track);
        content.extend(encode_vint_size(track_bytes.len() as u64));
        content.extend(track_bytes);

        // CueClusterPosition
        content.extend(encode_element_id(element_id::CUE_CLUSTER_POSITION));
        let pos_bytes = encode_uint(cue.cluster_position);
        content.extend(encode_vint_size(pos_bytes.len() as u64));
        content.extend(pos_bytes);

        // CueRelativePosition (optional)
        if let Some(rel_pos) = cue.relative_position {
            content.extend(encode_element_id(element_id::CUE_RELATIVE_POSITION));
            let rel_bytes = encode_uint(rel_pos);
            content.extend(encode_vint_size(rel_bytes.len() as u64));
            content.extend(rel_bytes);
        }

        // CueBlockNumber (optional)
        if let Some(block_num) = cue.block_number {
            content.extend(encode_element_id(element_id::CUE_BLOCK_NUMBER));
            let block_bytes = encode_uint(block_num);
            content.extend(encode_vint_size(block_bytes.len() as u64));
            content.extend(block_bytes);
        }

        content
    }

    /// Returns the number of cue points.
    #[must_use]
    pub fn len(&self) -> usize {
        self.cue_points.len()
    }

    /// Returns true if there are no cue points.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.cue_points.is_empty()
    }

    /// Clears all cue points.
    pub fn clear(&mut self) {
        self.cue_points.clear();
        self.last_cue_time.clear();
    }
}

// ============================================================================
// Encoding Helpers
// ============================================================================

/// Encodes an element ID to bytes.
///
/// EBML element IDs already include their class marker in the value,
/// so we just output the bytes that make up the ID.
fn encode_element_id(id: u32) -> Vec<u8> {
    if id <= 0xFF {
        vec![id as u8]
    } else if id <= 0xFFFF {
        vec![(id >> 8) as u8, id as u8]
    } else if id <= 0xFF_FFFF {
        vec![(id >> 16) as u8, (id >> 8) as u8, id as u8]
    } else {
        vec![
            (id >> 24) as u8,
            (id >> 16) as u8,
            (id >> 8) as u8,
            id as u8,
        ]
    }
}

/// Encodes a VINT size.
fn encode_vint_size(size: u64) -> Vec<u8> {
    if size < 0x80 {
        vec![0x80 | size as u8]
    } else if size < 0x4000 {
        vec![0x40 | (size >> 8) as u8, size as u8]
    } else if size < 0x1F_FFFF {
        vec![0x20 | (size >> 16) as u8, (size >> 8) as u8, size as u8]
    } else if size < 0x0FFF_FFFF {
        vec![
            0x10 | (size >> 24) as u8,
            (size >> 16) as u8,
            (size >> 8) as u8,
            size as u8,
        ]
    } else {
        vec![
            0x01,
            (size >> 48) as u8,
            (size >> 40) as u8,
            (size >> 32) as u8,
            (size >> 24) as u8,
            (size >> 16) as u8,
            (size >> 8) as u8,
            size as u8,
        ]
    }
}

/// Encodes an unsigned integer with minimal bytes.
fn encode_uint(value: u64) -> Vec<u8> {
    if value == 0 {
        vec![0]
    } else if value <= 0xFF {
        vec![value as u8]
    } else if value <= 0xFFFF {
        vec![(value >> 8) as u8, value as u8]
    } else if value <= 0xFF_FFFF {
        vec![(value >> 16) as u8, (value >> 8) as u8, value as u8]
    } else if value <= 0xFFFF_FFFF {
        vec![
            (value >> 24) as u8,
            (value >> 16) as u8,
            (value >> 8) as u8,
            value as u8,
        ]
    } else {
        vec![
            (value >> 56) as u8,
            (value >> 48) as u8,
            (value >> 40) as u8,
            (value >> 32) as u8,
            (value >> 24) as u8,
            (value >> 16) as u8,
            (value >> 8) as u8,
            value as u8,
        ]
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cue_point_new() {
        let cue = CuePoint::new(1000, 1, 500);
        assert_eq!(cue.time, 1000);
        assert_eq!(cue.track, 1);
        assert_eq!(cue.cluster_position, 500);
        assert!(cue.relative_position.is_none());
        assert!(cue.block_number.is_none());
    }

    #[test]
    fn test_cue_point_with_optional_fields() {
        let cue = CuePoint::new(1000, 1, 500)
            .with_relative_position(100)
            .with_block_number(5);

        assert_eq!(cue.relative_position, Some(100));
        assert_eq!(cue.block_number, Some(5));
    }

    #[test]
    fn test_cue_writer_new() {
        let writer = CueWriter::new();
        assert!(writer.is_empty());
        assert_eq!(writer.len(), 0);
    }

    #[test]
    fn test_cue_writer_add_cue_point() {
        let mut writer = CueWriter::new().with_min_interval(0);

        assert!(writer.add_cue_point(0, 1, 0));
        assert!(writer.add_cue_point(1000, 1, 1000));
        assert_eq!(writer.len(), 2);
    }

    #[test]
    fn test_cue_writer_min_interval() {
        let mut writer = CueWriter::new().with_min_interval(1000);

        assert!(writer.add_cue_point(0, 1, 0));
        // Should be skipped (too close)
        assert!(!writer.add_cue_point(500, 1, 500));
        // Should be added (>= min_interval)
        assert!(writer.add_cue_point(1000, 1, 1000));

        assert_eq!(writer.len(), 2);
    }

    #[test]
    fn test_cue_writer_add_detailed_cue_point() {
        let mut writer = CueWriter::new();

        writer.add_detailed_cue_point(1000, 1, 500, Some(100), Some(5));

        assert_eq!(writer.len(), 1);
        let cue = &writer.cue_points[0];
        assert_eq!(cue.relative_position, Some(100));
        assert_eq!(cue.block_number, Some(5));
    }

    #[test]
    fn test_cue_writer_build() {
        let mut writer = CueWriter::new().with_min_interval(0);

        writer.add_cue_point(0, 1, 0);
        writer.add_cue_point(5000, 1, 10000);

        let content = writer.build();
        assert!(!content.is_empty());

        // Check that it starts with CuePoint element ID (0xBB)
        assert_eq!(content[0], 0xBB);
    }

    #[test]
    fn test_cue_writer_clear() {
        let mut writer = CueWriter::new().with_min_interval(0);

        writer.add_cue_point(0, 1, 0);
        writer.add_cue_point(1000, 1, 1000);

        writer.clear();
        assert!(writer.is_empty());
    }

    #[test]
    fn test_encode_element_id() {
        // 1-byte ID
        assert_eq!(encode_element_id(0xBB), vec![0xBB]);
        // 2-byte ID
        assert_eq!(encode_element_id(0x53AB), vec![0x53, 0xAB]);
    }

    #[test]
    fn test_encode_uint() {
        assert_eq!(encode_uint(0), vec![0]);
        assert_eq!(encode_uint(127), vec![127]);
        assert_eq!(encode_uint(255), vec![255]);
        assert_eq!(encode_uint(256), vec![1, 0]);
        assert_eq!(encode_uint(1000), vec![0x03, 0xE8]);
    }
}