zvidlib-core 0.5.11

Shared types, I/O, codec traits and the SIMD override for zvidlib
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
use crate::{Error, ErrorKind, Result};
use std::cell::RefCell;
use std::collections::{HashMap, VecDeque};
use std::future::Future;
use std::pin::Pin;

/// A non-`Send` future suitable for both native and single-threaded WASM use.
pub type IoFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T>> + 'a>>;

/// An asynchronous random-access byte source.
pub trait ByteSource {
    fn len(&self) -> Option<u64>;

    fn is_empty(&self) -> Option<bool> {
        self.len().map(|length| length == 0)
    }

    fn read_at<'a>(&'a self, offset: u64, destination: &'a mut [u8]) -> IoFuture<'a, usize>;
}

/// An asynchronous sequential byte sink with optional seek support.
///
/// `is_seekable()` defaults to `true`, so implementers that cannot seek
/// (e.g. a streaming HTTP upload or a pipe) must override it to return
/// `false` *and* make `seek()` reject every call with
/// [`ErrorKind::Unsupported`]. Callers that require a seekable sink, such as
/// `crate::mp4::Mp4Muxer`, check `is_seekable()` up front and never call
/// `seek()` on a sink that reports `false`; a non-seekable sink's `seek()`
/// implementation exists only to fail safely if that contract is ever
/// violated.
pub trait ByteSink {
    fn position(&self) -> u64;
    fn is_seekable(&self) -> bool {
        true
    }
    fn write<'a>(&'a mut self, bytes: &'a [u8]) -> IoFuture<'a, ()>;
    fn seek<'a>(&'a mut self, position: u64) -> IoFuture<'a, ()>;
    fn flush<'a>(&'a mut self) -> IoFuture<'a, ()> {
        Box::pin(async { Ok(()) })
    }
}

/// An owned in-memory source useful for portable callers and deterministic tests.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct MemorySource {
    bytes: Vec<u8>,
}

impl MemorySource {
    pub fn new(bytes: impl Into<Vec<u8>>) -> Self {
        Self {
            bytes: bytes.into(),
        }
    }

    pub fn as_slice(&self) -> &[u8] {
        &self.bytes
    }
}

impl ByteSource for MemorySource {
    fn len(&self) -> Option<u64> {
        u64::try_from(self.bytes.len()).ok()
    }

    fn read_at<'a>(&'a self, offset: u64, destination: &'a mut [u8]) -> IoFuture<'a, usize> {
        Box::pin(async move {
            let offset = usize::try_from(offset)
                .map_err(|_| Error::new(ErrorKind::InvalidInput, "read offset is too large"))?;
            if offset > self.bytes.len() {
                return Err(Error::new(
                    ErrorKind::InvalidInput,
                    "read offset is beyond the source",
                ));
            }
            let length = destination.len().min(self.bytes.len() - offset);
            destination[..length].copy_from_slice(&self.bytes[offset..offset + length]);
            Ok(length)
        })
    }
}

/// A file on the local file system, read in place at the offsets asked for.
///
/// Every read completes before its future is first polled, so a synchronous
/// caller such as [`crate::codec::ExactFrameReader`] over an on-demand provider
/// can drive it directly. Clones share the open file, and no read moves a
/// cursor another one depends on.
#[cfg(any(unix, windows))]
#[derive(Clone, Debug)]
pub struct FileSource {
    file: std::sync::Arc<std::fs::File>,
    length: u64,
}

#[cfg(any(unix, windows))]
impl FileSource {
    pub fn open(path: impl AsRef<std::path::Path>) -> Result<Self> {
        let path = path.as_ref();
        let file = std::fs::File::open(path).map_err(|error| {
            Error::new(
                ErrorKind::Io,
                format!("cannot open {}: {error}", path.display()),
            )
        })?;
        Self::from_file(file)
    }

    pub fn from_file(file: std::fs::File) -> Result<Self> {
        let length = file
            .metadata()
            .map_err(|error| Error::new(ErrorKind::Io, format!("cannot read file size: {error}")))?
            .len();
        Ok(Self {
            file: std::sync::Arc::new(file),
            length,
        })
    }

    fn read_some(&self, offset: u64, destination: &mut [u8]) -> std::io::Result<usize> {
        #[cfg(unix)]
        {
            std::os::unix::fs::FileExt::read_at(&*self.file, destination, offset)
        }
        #[cfg(windows)]
        {
            std::os::windows::fs::FileExt::seek_read(&*self.file, destination, offset)
        }
    }
}

#[cfg(any(unix, windows))]
impl ByteSource for FileSource {
    fn len(&self) -> Option<u64> {
        Some(self.length)
    }

    fn read_at<'a>(&'a self, offset: u64, destination: &'a mut [u8]) -> IoFuture<'a, usize> {
        let result = if offset > self.length {
            Err(Error::new(
                ErrorKind::InvalidInput,
                "read offset is beyond the source",
            ))
        } else {
            // A positional read may return fewer bytes than asked for short of
            // the end, so read until the destination is full or the file ends.
            let mut filled = 0;
            loop {
                if filled == destination.len() {
                    break Ok(filled);
                }
                match self.read_some(offset + filled as u64, &mut destination[filled..]) {
                    Ok(0) => break Ok(filled),
                    Ok(read) => filled += read,
                    Err(error) if error.kind() == std::io::ErrorKind::Interrupted => {}
                    Err(error) => {
                        break Err(Error::new(
                            ErrorKind::Io,
                            format!("cannot read file: {error}"),
                        ));
                    }
                }
            }
        };
        Box::pin(async move { result })
    }
}

/// A seekable in-memory sink useful for portable callers and deterministic tests.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct MemorySink {
    bytes: Vec<u8>,
    position: u64,
}

impl MemorySink {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn into_inner(self) -> Vec<u8> {
        self.bytes
    }

    pub fn as_slice(&self) -> &[u8] {
        &self.bytes
    }
}

impl ByteSink for MemorySink {
    fn position(&self) -> u64 {
        self.position
    }

    fn write<'a>(&'a mut self, bytes: &'a [u8]) -> IoFuture<'a, ()> {
        Box::pin(async move {
            let position = usize::try_from(self.position)
                .map_err(|_| Error::new(ErrorKind::ResourceLimit, "sink position is too large"))?;
            let end = position
                .checked_add(bytes.len())
                .ok_or_else(|| Error::new(ErrorKind::ResourceLimit, "sink allocation overflow"))?;
            if end > self.bytes.len() {
                self.bytes.resize(end, 0);
            }
            self.bytes[position..end].copy_from_slice(bytes);
            self.position = u64::try_from(end).map_err(|_| {
                Error::new(
                    ErrorKind::ResourceLimit,
                    "sink position cannot be represented",
                )
            })?;
            Ok(())
        })
    }

    fn seek<'a>(&'a mut self, position: u64) -> IoFuture<'a, ()> {
        Box::pin(async move {
            usize::try_from(position)
                .map_err(|_| Error::new(ErrorKind::InvalidInput, "seek position is too large"))?;
            self.position = position;
            Ok(())
        })
    }
}

/// A sequential-only in-memory sink used to test the non-seekable `ByteSink`
/// contract: it reports `is_seekable() == false` and rejects every `seek()`
/// call with [`ErrorKind::Unsupported`].
#[doc(hidden)]
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct NonSeekableSink {
    bytes: Vec<u8>,
}

impl ByteSink for NonSeekableSink {
    fn position(&self) -> u64 {
        self.bytes.len() as u64
    }

    fn is_seekable(&self) -> bool {
        false
    }

    fn write<'a>(&'a mut self, bytes: &'a [u8]) -> IoFuture<'a, ()> {
        Box::pin(async move {
            self.bytes.extend_from_slice(bytes);
            Ok(())
        })
    }

    fn seek<'a>(&'a mut self, _position: u64) -> IoFuture<'a, ()> {
        Box::pin(async move {
            Err(Error::new(
                ErrorKind::Unsupported,
                "this sink does not support seeking",
            ))
        })
    }
}

/// A [`ByteSource`] wrapper that caches fixed-size pages of the bytes it
/// reads, evicting the least-recently-used page once a configured byte
/// budget is exceeded.
///
/// This is how a [`crate::codec::SampleProvider`] backed by a container
/// track and a plain [`ByteSource`] (such as a file) keeps the memory for
/// compressed sample bytes bounded: samples near the playhead are read
/// through overlapping pages that stay cached, and pages behind the budget
/// are dropped rather than held for the life of the source.
///
/// Reads are not required to be page-aligned or page-sized; a read spanning
/// several pages fetches and caches each of them. Internal caching uses a
/// [`RefCell`], so a `CachingByteSource` is usable from one thread at a time,
/// matching how a reader built over it is used.
pub struct CachingByteSource<S> {
    inner: S,
    page_size: u64,
    budget_bytes: u64,
    pages: RefCell<HashMap<u64, Vec<u8>>>,
    lru: RefCell<VecDeque<u64>>,
    resident_bytes: RefCell<u64>,
}

impl<S: ByteSource> CachingByteSource<S> {
    /// `page_size` and `budget_bytes` must both be nonzero; a page larger
    /// than the budget is still cached (so a single read always succeeds)
    /// but is evicted again on the very next page fetch.
    pub fn new(inner: S, page_size: u64, budget_bytes: u64) -> Result<Self> {
        if page_size == 0 || budget_bytes == 0 {
            return Err(Error::new(
                ErrorKind::InvalidInput,
                "caching byte source requires a nonzero page size and budget",
            ));
        }
        Ok(Self {
            inner,
            page_size,
            budget_bytes,
            pages: RefCell::new(HashMap::new()),
            lru: RefCell::new(VecDeque::new()),
            resident_bytes: RefCell::new(0),
        })
    }

    pub fn into_inner(self) -> S {
        self.inner
    }

    /// Total bytes currently cached, for tests and diagnostics.
    pub fn resident_bytes(&self) -> u64 {
        *self.resident_bytes.borrow()
    }

    fn touch(&self, page: u64) {
        let mut lru = self.lru.borrow_mut();
        lru.retain(|candidate| *candidate != page);
        lru.push_back(page);
    }

    fn evict_until_within_budget(&self) {
        loop {
            if *self.resident_bytes.borrow() <= self.budget_bytes {
                return;
            }
            let Some(evicted) = self.lru.borrow_mut().pop_front() else {
                return;
            };
            if let Some(bytes) = self.pages.borrow_mut().remove(&evicted) {
                *self.resident_bytes.borrow_mut() -= bytes.len() as u64;
            }
        }
    }

    /// Reads `pages` pages from `first` with one read of the inner source,
    /// caches each of them, and returns their bytes, which end early where
    /// the inner source does.
    async fn fetch(&self, first: u64, pages: u64) -> Result<Vec<u8>> {
        let length = usize::try_from(pages * self.page_size).map_err(|_| {
            Error::new(
                ErrorKind::ResourceLimit,
                "caching byte source read is too large",
            )
        })?;
        let mut bytes = vec![0_u8; length];
        let read = self
            .inner
            .read_at(first * self.page_size, &mut bytes)
            .await?;
        bytes.truncate(read);
        for (index, chunk) in bytes.chunks(self.page_size as usize).enumerate() {
            let page = first + index as u64;
            *self.resident_bytes.borrow_mut() += chunk.len() as u64;
            if let Some(replaced) = self.pages.borrow_mut().insert(page, chunk.to_vec()) {
                *self.resident_bytes.borrow_mut() -= replaced.len() as u64;
            }
            self.touch(page);
        }
        self.evict_until_within_budget();
        Ok(bytes)
    }
}

impl<S: ByteSource> ByteSource for CachingByteSource<S> {
    fn len(&self) -> Option<u64> {
        self.inner.len()
    }

    /// Answers each cached page of the read from the cache, and fetches each
    /// run of neighboring pages that is not with one read of the inner source,
    /// so a read spanning many pages, such as a large index element, costs one
    /// request however large it is (issue #692).
    fn read_at<'a>(&'a self, offset: u64, destination: &'a mut [u8]) -> IoFuture<'a, usize> {
        Box::pin(async move {
            let mut produced = 0_usize;
            while produced < destination.len() {
                let position = offset + produced as u64;
                let page_index = position / self.page_size;
                let in_page_offset = (position - page_index * self.page_size) as usize;
                let cached = self.pages.borrow().get(&page_index).cloned();
                let (bytes, pages) = match cached {
                    Some(page) => {
                        self.touch(page_index);
                        (page, 1)
                    }
                    None => {
                        let last_page = (offset + destination.len() as u64 - 1) / self.page_size;
                        let mut run_end = page_index + 1;
                        while run_end <= last_page && !self.pages.borrow().contains_key(&run_end) {
                            run_end += 1;
                        }
                        let pages = run_end - page_index;
                        (self.fetch(page_index, pages).await?, pages)
                    }
                };
                if in_page_offset >= bytes.len() {
                    break;
                }
                let available = (bytes.len() - in_page_offset).min(destination.len() - produced);
                destination[produced..produced + available]
                    .copy_from_slice(&bytes[in_page_offset..in_page_offset + available]);
                produced += available;
                if (bytes.len() as u64) < pages * self.page_size {
                    // The inner source ended partway through these pages.
                    break;
                }
            }
            Ok(produced)
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::task::{Context, Poll, Waker};

    fn ready<T>(mut future: IoFuture<'_, T>) -> Result<T> {
        let waker = Waker::noop();
        let mut context = Context::from_waker(waker);
        match future.as_mut().poll(&mut context) {
            Poll::Ready(result) => result,
            Poll::Pending => panic!("memory I/O unexpectedly returned a pending future"),
        }
    }

    #[test]
    fn memory_source_reads_partially_at_end_of_source() {
        let source = MemorySource::new(b"abcdef".to_vec());
        let mut destination = [0_u8; 4];
        assert_eq!(ready(source.read_at(4, &mut destination)).unwrap(), 2);
        assert_eq!(&destination[..2], b"ef");
    }

    #[test]
    fn memory_source_rejects_offsets_beyond_end() {
        let source = MemorySource::new(b"abc".to_vec());
        let mut destination = [0_u8; 1];
        let error = ready(source.read_at(4, &mut destination)).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::InvalidInput);
    }

    #[test]
    fn memory_sink_seeks_overwrites_and_zero_fills() {
        let mut sink = MemorySink::new();
        ready(sink.write(b"abc")).unwrap();
        ready(sink.seek(1)).unwrap();
        ready(sink.write(b"Z")).unwrap();
        ready(sink.seek(5)).unwrap();
        ready(sink.write(b"!")).unwrap();
        assert_eq!(sink.as_slice(), b"aZc\0\0!");
    }

    #[test]
    fn non_seekable_sink_writes_but_rejects_seek() {
        let mut sink = NonSeekableSink::default();
        assert!(!sink.is_seekable());
        ready(sink.write(b"abc")).unwrap();
        assert_eq!(sink.bytes, b"abc");
        let error = ready(sink.seek(0)).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Unsupported);
    }

    /// A source that counts every byte actually read from it, so a test can
    /// tell a cache hit from a fetch, and every request made of it.
    struct CountingSource {
        inner: MemorySource,
        reads: std::cell::Cell<u64>,
        requests: std::cell::Cell<u64>,
    }

    impl ByteSource for CountingSource {
        fn len(&self) -> Option<u64> {
            self.inner.len()
        }

        fn read_at<'a>(&'a self, offset: u64, destination: &'a mut [u8]) -> IoFuture<'a, usize> {
            Box::pin(async move {
                self.requests.set(self.requests.get() + 1);
                let read = self.inner.read_at(offset, destination).await?;
                self.reads.set(self.reads.get() + read as u64);
                Ok(read)
            })
        }
    }

    fn large_source(bytes: u64) -> CountingSource {
        CountingSource {
            inner: MemorySource::new((0..bytes).map(|value| value as u8).collect::<Vec<u8>>()),
            reads: std::cell::Cell::new(0),
            requests: std::cell::Cell::new(0),
        }
    }

    /// Issue #692: a read over many pages none of which is cached is one
    /// request of the inner source, and a read over cached and uncached pages
    /// fetches each uncached run at once, ending where the source ends.
    #[test]
    fn caching_byte_source_fetches_a_run_of_uncached_pages_in_one_request() {
        let cache = CachingByteSource::new(large_source(1000), 64, 1_000_000).unwrap();
        let mut destination = [0_u8; 500];
        assert_eq!(ready(cache.read_at(10, &mut destination)).unwrap(), 500);
        assert_eq!(cache.inner.requests.get(), 1);
        let expected: Vec<u8> = (10..510).map(|value| value as u8).collect();
        assert_eq!(&destination[..], expected.as_slice());

        // Pages 0..=7 are cached; 8 to the end come in one more request.
        let mut destination = [0_u8; 1000];
        assert_eq!(ready(cache.read_at(0, &mut destination)).unwrap(), 1000);
        assert_eq!(cache.inner.requests.get(), 2);
        let expected: Vec<u8> = (0..1000).map(|value| value as u8).collect();
        assert_eq!(&destination[..], expected.as_slice());
        let mut destination = [0_u8; 1000];
        assert_eq!(ready(cache.read_at(0, &mut destination)).unwrap(), 1000);
        assert_eq!(cache.inner.requests.get(), 2);

        // A run larger than the budget is still read whole and once.
        let small = CachingByteSource::new(large_source(1000), 64, 128).unwrap();
        let mut destination = [0_u8; 600];
        assert_eq!(ready(small.read_at(100, &mut destination)).unwrap(), 600);
        assert_eq!(small.inner.requests.get(), 1);
        assert!(small.resident_bytes() <= 128);
        let expected: Vec<u8> = (100..700).map(|value| value as u8).collect();
        assert_eq!(&destination[..], expected.as_slice());
    }

    #[test]
    fn caching_byte_source_reads_match_the_inner_source_across_page_boundaries() {
        let cache = CachingByteSource::new(large_source(1000), 64, 1_000_000).unwrap();
        let mut destination = [0_u8; 100];
        assert_eq!(ready(cache.read_at(30, &mut destination)).unwrap(), 100);
        let expected: Vec<u8> = (30..130).map(|value| value as u8).collect();
        assert_eq!(&destination, expected.as_slice());
    }

    #[test]
    fn caching_byte_source_answers_a_repeated_read_without_fetching_again() {
        let cache = CachingByteSource::new(large_source(1000), 64, 1_000_000).unwrap();
        let mut destination = [0_u8; 10];
        ready(cache.read_at(0, &mut destination)).unwrap();
        let reads_after_first = cache.inner.reads.get();
        assert!(reads_after_first > 0);
        ready(cache.read_at(0, &mut destination)).unwrap();
        assert_eq!(
            cache.inner.reads.get(),
            reads_after_first,
            "a repeated read fetched from the inner source again instead of the cache"
        );
    }

    /// Reading all over a track much larger than the cache budget - the
    /// shape of scrubbing and sequential playback through a long file - must
    /// never grow resident bytes past the configured budget.
    #[test]
    fn caching_byte_source_stays_within_its_budget_while_scanning_a_large_source() {
        let total = 1_000_000_u64;
        let page_size = 4096_u64;
        let budget = 64 * 1024_u64;
        let cache = CachingByteSource::new(large_source(total), page_size, budget).unwrap();
        let mut destination = [0_u8; 1024];
        let mut offset = 0_u64;
        while offset + destination.len() as u64 <= total {
            ready(cache.read_at(offset, &mut destination)).unwrap();
            assert!(
                cache.resident_bytes() <= budget,
                "resident bytes {} exceeded the budget {budget} at offset {offset}",
                cache.resident_bytes()
            );
            // Jump far enough each time that pages do not all stay resident
            // through overlap alone.
            offset += 97 * 1024;
        }
    }

    #[test]
    fn caching_byte_source_rejects_a_zero_page_size_or_budget() {
        assert_eq!(
            CachingByteSource::new(MemorySource::default(), 0, 1024)
                .err()
                .unwrap()
                .kind(),
            ErrorKind::InvalidInput
        );
        assert_eq!(
            CachingByteSource::new(MemorySource::default(), 1024, 0)
                .err()
                .unwrap()
                .kind(),
            ErrorKind::InvalidInput
        );
    }
}