Files
DomiStream/crates/domistream/src/fec.rs
T

96 lines
3.9 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Reed-Solomon erasure coding over GF(2^8), polynomial `0x11D` (`docs/PROTOCOL.md` 5.3, 6.4).
//!
//! Systematic code: `data` shards pass through unchanged, `parity` shards are linear combinations of them, and any
//! `data` of the `data + parity` shards rebuild the rest. Every byte position is coded on its own, so all shards of
//! a block have the same length.
//!
//! Two matrices are needed: video (`ReedSolomon::new`, construction UNKNOWN U14) and audio (a fixed 2 × 4 parity
//! matrix, UNKNOWN U16, passed in with [`ReedSolomon::with_parity_matrix`] until it is measured).
/// Maximum shards in one block (data + parity).
pub const MAX_SHARDS: usize = 255;
/// Maximum length of one shard, in bytes.
pub const MAX_SHARD_BYTES: usize = 4096;
/// Parity shards for `data_shards` at `fec_percentage`: `ceil(data × percentage / 100)`.
///
/// # Errors
/// [`crate::Error::UnexpectedValue`] when `data_shards` is 0 or the result would exceed [`MAX_SHARDS`]. Phase A:
/// always [`crate::Error::Unimplemented`].
pub fn parity_count(data_shards: usize, fec_percentage: u8) -> crate::Result<usize> {
let _ = (data_shards, fec_percentage);
Err(crate::Error::Unimplemented)
}
/// One code: shard counts and its parity matrix.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ReedSolomon {
data_shards: usize,
parity_shards: usize,
/// `parity_shards × data_shards` coefficients, row-major.
parity_matrix: Vec<u8>,
}
impl ReedSolomon {
/// The code GameStream video uses for `data_shards` + `parity_shards` (UNKNOWN U14: construction).
///
/// # Errors
/// [`crate::Error::UnexpectedValue`] when either count is 0 or the sum exceeds [`MAX_SHARDS`]. Phase A: always
/// [`crate::Error::Unimplemented`].
pub fn new(data_shards: usize, parity_shards: usize) -> crate::Result<Self> {
let _ = (data_shards, parity_shards);
Err(crate::Error::Unimplemented)
}
/// A code with an explicit parity matrix (`parity_shards × data_shards`, row-major). The matrix must make every
/// square submatrix of `[I; P]` invertible, or some loss patterns cannot be rebuilt; this is checked.
///
/// # Errors
/// As [`Self::new`]; [`crate::Error::Malformed`] for a matrix of the wrong size or one that is not MDS.
pub fn with_parity_matrix(data_shards: usize, parity_shards: usize, parity_matrix: &[u8]) -> crate::Result<Self> {
let _ = (data_shards, parity_shards, parity_matrix);
Err(crate::Error::Unimplemented)
}
/// Number of data shards.
#[must_use]
pub const fn data_shards(&self) -> usize {
self.data_shards
}
/// Number of parity shards.
#[must_use]
pub const fn parity_shards(&self) -> usize {
self.parity_shards
}
/// The parity matrix, row-major.
#[must_use]
pub fn parity_matrix(&self) -> &[u8] {
&self.parity_matrix
}
/// Computes the parity shards from the data shards. `shards` holds `data + parity` buffers of equal length;
/// the parity ones are overwritten.
///
/// # Errors
/// [`crate::Error::Malformed`] for the wrong shard count or unequal lengths, [`crate::Error::TooLarge`] over
/// [`MAX_SHARD_BYTES`]. Phase A: always [`crate::Error::Unimplemented`].
pub fn encode(&self, shards: &mut [Vec<u8>]) -> crate::Result<()> {
let _ = shards;
Err(crate::Error::Unimplemented)
}
/// Rebuilds missing shards in place. `shards` holds `data + parity` entries, `None` for lost ones; on success
/// every entry is `Some`.
///
/// # Errors
/// [`crate::Error::Malformed`] for the wrong count or unequal lengths, [`crate::Error::TooLarge`] over
/// [`MAX_SHARD_BYTES`], [`crate::Error::Truncated`] when fewer than `data` shards are present. Phase A: always
/// [`crate::Error::Unimplemented`].
pub fn reconstruct(&self, shards: &mut [Option<Vec<u8>>]) -> crate::Result<()> {
let _ = shards;
Err(crate::Error::Unimplemented)
}
}