mirx 0.50.0

MIRX binary image format — flat & chunk-based container for ahead-of-time UI assets
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
use crate::wire::read_u32_le;
#[cfg(test)]
use crate::wire::write_u32_le;

pub(crate) const FRAME_TIMING_HEADER_LEN: usize = 4;
const SPARSE_ENTRY_LEN: usize = 8;

/// Physical representation selected for a nonempty FRAME_TIMING section.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum FrameTimingEncoding {
    Dense,
    Sparse,
}

/// Borrowed canonical per-frame timing overrides.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct FrameTiming<'a> {
    body: &'a [u8],
    frame_count: u32,
    default_duration_ticks: u32,
    encoding: FrameTimingEncoding,
}

/// Authoring view that chooses the smaller dense or sparse timing column.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct FrameTimingAsset<'a> {
    durations: &'a [u32],
    default_duration_ticks: u32,
    override_count: usize,
    encoding: Option<FrameTimingEncoding>,
}

impl<'a> FrameTiming<'a> {
    pub fn open(
        bytes: &'a [u8],
        frame_count: u32,
        default_duration_ticks: u32,
    ) -> Result<Self, FrameTimingError> {
        if frame_count == 0 {
            return Err(FrameTimingError::EmptySequence);
        }
        if default_duration_ticks == 0 {
            return Err(FrameTimingError::ZeroDefaultDuration);
        }
        let header = bytes
            .get(..FRAME_TIMING_HEADER_LEN)
            .ok_or(FrameTimingError::Truncated {
                needed: FRAME_TIMING_HEADER_LEN,
                available: bytes.len(),
            })?;
        if header[1..] != [0; 3] {
            return Err(FrameTimingError::NonCanonicalHeader);
        }
        let encoding = match header[0] {
            0 => FrameTimingEncoding::Dense,
            1 => FrameTimingEncoding::Sparse,
            value => return Err(FrameTimingError::UnknownEncoding(value)),
        };
        let body = &bytes[FRAME_TIMING_HEADER_LEN..];
        let dense_len = dense_body_len(frame_count)?;
        let override_count = match encoding {
            FrameTimingEncoding::Dense => {
                if body.len() != dense_len {
                    return Err(FrameTimingError::InvalidDenseLength {
                        expected: dense_len,
                        actual: body.len(),
                    });
                }
                let mut count = 0;
                for frame in 0..frame_count {
                    let duration = read_dense(body, frame).ok_or(FrameTimingError::SizeOverflow)?;
                    validate_duration(frame, duration)?;
                    count += usize::from(duration != default_duration_ticks);
                }
                count
            }
            FrameTimingEncoding::Sparse => {
                if body.is_empty() || body.len() % SPARSE_ENTRY_LEN != 0 {
                    return Err(FrameTimingError::InvalidSparseLength(body.len()));
                }
                let mut previous = None;
                for entry in body.chunks_exact(SPARSE_ENTRY_LEN) {
                    let frame = read_u32_le(entry, 0).expect("complete timing entry");
                    let duration = read_u32_le(entry, 4).expect("complete timing entry");
                    if frame >= frame_count {
                        return Err(FrameTimingError::FrameOutOfBounds { frame, frame_count });
                    }
                    if previous.is_some_and(|previous| frame <= previous) {
                        return Err(FrameTimingError::FramesOutOfOrder {
                            previous: previous.unwrap(),
                            next: frame,
                        });
                    }
                    validate_duration(frame, duration)?;
                    if duration == default_duration_ticks {
                        return Err(FrameTimingError::RedundantDefault { frame });
                    }
                    previous = Some(frame);
                }
                body.len() / SPARSE_ENTRY_LEN
            }
        };
        if override_count == 0 {
            return Err(FrameTimingError::NoOverrides);
        }
        let best = preferred_encoding(frame_count, override_count)?;
        if encoding != best {
            return Err(FrameTimingError::NonCanonicalEncoding {
                expected: best,
                actual: encoding,
            });
        }
        Ok(Self {
            body,
            frame_count,
            default_duration_ticks,
            encoding,
        })
    }

    pub const fn frame_count(self) -> u32 {
        self.frame_count
    }

    pub const fn encoding(self) -> FrameTimingEncoding {
        self.encoding
    }

    pub fn duration(self, frame: u32) -> Option<u32> {
        if frame >= self.frame_count {
            return None;
        }
        match self.encoding {
            FrameTimingEncoding::Dense => read_dense(self.body, frame),
            FrameTimingEncoding::Sparse => self
                .sparse_duration(frame)
                .or(Some(self.default_duration_ticks)),
        }
    }

    pub(crate) fn cycle_duration_ticks(self) -> u64 {
        match self.encoding {
            FrameTimingEncoding::Dense => (0..self.frame_count)
                .map(|frame| {
                    u64::from(read_dense(self.body, frame).expect("validated dense duration"))
                })
                .sum(),
            FrameTimingEncoding::Sparse => {
                let mut total =
                    u128::from(self.frame_count) * u128::from(self.default_duration_ticks);
                for entry in self.body.chunks_exact(SPARSE_ENTRY_LEN) {
                    let duration = read_u32_le(entry, 4).expect("validated sparse duration");
                    total = total + u128::from(duration) - u128::from(self.default_duration_ticks);
                }
                u64::try_from(total).expect("u32 frame counts and durations fit u64")
            }
        }
    }

    pub(crate) fn locate(self, elapsed_ticks: u64) -> Option<(u32, u32)> {
        if elapsed_ticks >= self.cycle_duration_ticks() {
            return None;
        }
        match self.encoding {
            FrameTimingEncoding::Dense => {
                let mut remaining = elapsed_ticks;
                for frame in 0..self.frame_count {
                    let duration =
                        u64::from(read_dense(self.body, frame).expect("validated dense duration"));
                    if remaining < duration {
                        return Some((frame, remaining as u32));
                    }
                    remaining -= duration;
                }
                unreachable!("validated durations cover one complete cycle")
            }
            FrameTimingEncoding::Sparse => self.locate_sparse(elapsed_ticks),
        }
    }

    fn locate_sparse(self, mut remaining: u64) -> Option<(u32, u32)> {
        let default = u64::from(self.default_duration_ticks);
        let mut frame = 0u32;
        for entry in self.body.chunks_exact(SPARSE_ENTRY_LEN) {
            let override_frame = read_u32_le(entry, 0).expect("validated sparse frame");
            let default_run = u64::from(override_frame - frame) * default;
            if remaining < default_run {
                let offset = u32::try_from(remaining / default)
                    .expect("default run offset fits frame ordinal");
                return Some((frame + offset, (remaining % default) as u32));
            }
            remaining -= default_run;

            let duration = u64::from(read_u32_le(entry, 4).expect("validated sparse duration"));
            if remaining < duration {
                return Some((override_frame, remaining as u32));
            }
            remaining -= duration;
            frame = override_frame + 1;
        }

        let offset =
            u32::try_from(remaining / default).expect("default tail offset fits frame ordinal");
        Some((frame + offset, (remaining % default) as u32))
    }

    fn sparse_duration(self, frame: u32) -> Option<u32> {
        let mut left = 0usize;
        let mut right = self.body.len() / SPARSE_ENTRY_LEN;
        while left < right {
            let middle = left + (right - left) / 2;
            let entry = &self.body[middle * SPARSE_ENTRY_LEN..][..SPARSE_ENTRY_LEN];
            match read_u32_le(entry, 0)?.cmp(&frame) {
                core::cmp::Ordering::Less => left = middle + 1,
                core::cmp::Ordering::Greater => right = middle,
                core::cmp::Ordering::Equal => return read_u32_le(entry, 4),
            }
        }
        None
    }
}

impl<'a> FrameTimingAsset<'a> {
    pub(super) fn new(
        durations: &'a [u32],
        default_duration_ticks: u32,
    ) -> Result<Self, FrameTimingError> {
        if durations.is_empty() {
            return Err(FrameTimingError::EmptySequence);
        }
        if default_duration_ticks == 0 {
            return Err(FrameTimingError::ZeroDefaultDuration);
        }
        u32::try_from(durations.len()).map_err(|_| FrameTimingError::SizeOverflow)?;
        let mut override_count = 0;
        for (frame, &duration) in durations.iter().enumerate() {
            validate_duration(frame as u32, duration)?;
            override_count += usize::from(duration != default_duration_ticks);
        }
        let encoding = if override_count == 0 {
            None
        } else {
            Some(preferred_encoding(durations.len() as u32, override_count)?)
        };
        Ok(Self {
            durations,
            default_duration_ticks,
            override_count,
            encoding,
        })
    }

    /// Returns true when every frame uses the sequence default and no section is emitted.
    pub(super) const fn is_empty(self) -> bool {
        self.encoding.is_none()
    }

    pub(super) const fn encoding(self) -> Option<FrameTimingEncoding> {
        self.encoding
    }

    pub(crate) const fn durations(self) -> &'a [u32] {
        self.durations
    }

    pub(crate) const fn default_duration_ticks(self) -> u32 {
        self.default_duration_ticks
    }

    pub(super) fn encoded_len(self) -> usize {
        match self.encoding {
            None => 0,
            Some(FrameTimingEncoding::Dense) => FRAME_TIMING_HEADER_LEN + self.durations.len() * 4,
            Some(FrameTimingEncoding::Sparse) => {
                FRAME_TIMING_HEADER_LEN + self.override_count * SPARSE_ENTRY_LEN
            }
        }
    }

    #[cfg(test)]
    pub(super) fn encode_into(self, output: &mut [u8]) -> usize {
        let needed = self.encoded_len();
        assert!(output.len() >= needed);
        let Some(encoding) = self.encoding else {
            return 0;
        };
        output[..needed].fill(0);
        output[0] = match encoding {
            FrameTimingEncoding::Dense => 0,
            FrameTimingEncoding::Sparse => 1,
        };
        let mut cursor = FRAME_TIMING_HEADER_LEN;
        for (frame, &duration) in self.durations.iter().enumerate() {
            if encoding == FrameTimingEncoding::Sparse && duration == self.default_duration_ticks {
                continue;
            }
            if encoding == FrameTimingEncoding::Sparse {
                write_u32_le(&mut output[..needed], cursor, frame as u32);
                cursor += 4;
            }
            write_u32_le(&mut output[..needed], cursor, duration);
            cursor += 4;
        }
        debug_assert_eq!(cursor, needed);
        needed
    }
}

fn preferred_encoding(
    frame_count: u32,
    override_count: usize,
) -> Result<FrameTimingEncoding, FrameTimingError> {
    let dense = dense_body_len(frame_count)?;
    let sparse = override_count
        .checked_mul(SPARSE_ENTRY_LEN)
        .ok_or(FrameTimingError::SizeOverflow)?;
    Ok(if sparse < dense {
        FrameTimingEncoding::Sparse
    } else {
        FrameTimingEncoding::Dense
    })
}

fn dense_body_len(frame_count: u32) -> Result<usize, FrameTimingError> {
    usize::try_from(frame_count)
        .ok()
        .and_then(|count| count.checked_mul(4))
        .ok_or(FrameTimingError::SizeOverflow)
}

fn read_dense(bytes: &[u8], frame: u32) -> Option<u32> {
    read_u32_le(bytes, usize::try_from(frame).ok()?.checked_mul(4)?)
}

fn validate_duration(frame: u32, duration: u32) -> Result<(), FrameTimingError> {
    if duration == 0 {
        Err(FrameTimingError::ZeroDuration { frame })
    } else {
        Ok(())
    }
}

/// Failure while reading or emitting per-frame timing overrides.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum FrameTimingError {
    Truncated {
        needed: usize,
        available: usize,
    },
    EmptySequence,
    ZeroDefaultDuration,
    UnknownEncoding(u8),
    NonCanonicalHeader,
    InvalidDenseLength {
        expected: usize,
        actual: usize,
    },
    InvalidSparseLength(usize),
    FrameOutOfBounds {
        frame: u32,
        frame_count: u32,
    },
    FramesOutOfOrder {
        previous: u32,
        next: u32,
    },
    ZeroDuration {
        frame: u32,
    },
    RedundantDefault {
        frame: u32,
    },
    NoOverrides,
    NonCanonicalEncoding {
        expected: FrameTimingEncoding,
        actual: FrameTimingEncoding,
    },
    SizeOverflow,
}

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

    #[test]
    fn all_default_durations_omit_the_section() {
        let asset = FrameTimingAsset::new(&[40, 40, 40], 40).unwrap();
        assert!(asset.is_empty());
        assert_eq!(asset.encoded_len(), 0);
        assert_eq!(asset.encode_into(&mut []), 0);
    }

    #[test]
    fn sparse_overrides_roundtrip_and_default_missing_frames() {
        let durations = [40, 80, 40, 40, 120];
        let asset = FrameTimingAsset::new(&durations, 40).unwrap();
        assert_eq!(asset.encoding(), Some(FrameTimingEncoding::Sparse));
        let mut bytes = [0; 20];
        let len = asset.encode_into(&mut bytes);
        assert_eq!(len, 20);
        let timing = FrameTiming::open(&bytes[..len], 5, 40).unwrap();
        assert_eq!(timing.encoding(), FrameTimingEncoding::Sparse);
        for (frame, duration) in durations.into_iter().enumerate() {
            assert_eq!(timing.duration(frame as u32), Some(duration));
        }
        assert_eq!(timing.duration(5), None);
    }

    #[test]
    fn sparse_timeline_skips_default_runs_around_overrides() {
        let mut durations = [10; 1_000];
        durations[500] = 25;
        let asset = FrameTimingAsset::new(&durations, 10).unwrap();
        assert_eq!(asset.encoding(), Some(FrameTimingEncoding::Sparse));
        let mut bytes = [0; 12];
        let len = asset.encode_into(&mut bytes);
        let timing = FrameTiming::open(&bytes[..len], 1_000, 10).unwrap();

        assert_eq!(timing.cycle_duration_ticks(), 10_015);
        assert_eq!(timing.locate(4_999), Some((499, 9)));
        assert_eq!(timing.locate(5_000), Some((500, 0)));
        assert_eq!(timing.locate(5_024), Some((500, 24)));
        assert_eq!(timing.locate(5_025), Some((501, 0)));
        assert_eq!(timing.locate(10_014), Some((999, 9)));
        assert_eq!(timing.locate(10_015), None);
    }

    #[test]
    fn dense_column_wins_ties_and_roundtrips() {
        let durations = [20, 40, 60, 40];
        let asset = FrameTimingAsset::new(&durations, 40).unwrap();
        assert_eq!(asset.encoding(), Some(FrameTimingEncoding::Dense));
        let mut bytes = [0; 20];
        asset.encode_into(&mut bytes);
        let timing = FrameTiming::open(&bytes, 4, 40).unwrap();
        assert_eq!(timing.encoding(), FrameTimingEncoding::Dense);
        assert_eq!(timing.duration(2), Some(60));
    }

    #[test]
    fn nonminimal_and_redundant_wire_forms_are_rejected() {
        let mut dense = [0; 24];
        for (index, duration) in [40, 40, 80, 40, 40].into_iter().enumerate() {
            write_u32_le(&mut dense, FRAME_TIMING_HEADER_LEN + index * 4, duration);
        }
        assert_eq!(
            FrameTiming::open(&dense, 5, 40),
            Err(FrameTimingError::NonCanonicalEncoding {
                expected: FrameTimingEncoding::Sparse,
                actual: FrameTimingEncoding::Dense,
            })
        );

        let mut sparse = [0; 12];
        sparse[0] = 1;
        write_u32_le(&mut sparse, 4, 2);
        write_u32_le(&mut sparse, 8, 40);
        assert_eq!(
            FrameTiming::open(&sparse, 5, 40),
            Err(FrameTimingError::RedundantDefault { frame: 2 })
        );
    }

    #[test]
    fn malformed_tables_are_rejected() {
        let mut unordered = [0; 20];
        unordered[0] = 1;
        write_u32_le(&mut unordered, 4, 2);
        write_u32_le(&mut unordered, 8, 80);
        write_u32_le(&mut unordered, 12, 1);
        write_u32_le(&mut unordered, 16, 60);
        assert_eq!(
            FrameTiming::open(&unordered, 5, 40),
            Err(FrameTimingError::FramesOutOfOrder {
                previous: 2,
                next: 1,
            })
        );
    }
}