oximedia-videoip 0.1.8

Professional video-over-IP protocol for OxiMedia (patent-free NDI alternative)
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
//! SIMD-accelerated Galois Field GF(2^8) arithmetic.
//!
//! GF(2^8) with the primitive polynomial x^8 + x^4 + x^3 + x^2 + 1
//! (0x11D, commonly used in Reed-Solomon codecs).
//!
//! # Provided functions
//!
//! * `gf_mul_lut(a, b)` — lookup-table GF multiply using 512-byte exp+log tables.
//! * `gf_mul_slice_simd(src, scalar, dst)` — multiply every byte of `src` by
//!   `scalar` using AVX2 VPSHUFB on x86_64 with SSSE3/AVX2; scalar fallback on
//!   all other targets.
//!
//! # SimdRsEncoder
//!
//! A self-contained Reed-Solomon encoder that uses `gf_mul_slice_simd` in its
//! inner loop and whose output is verified to match `reed-solomon-erasure`.

// ---------------------------------------------------------------------------
// GF(2^8) tables — primitive polynomial 0x11D
// ---------------------------------------------------------------------------

const POLY: usize = 0x11D;

/// Generate the 512-byte exp and log lookup tables for GF(2^8).
///
/// Returns `(exp, log)` where `exp[i] = alpha^i mod P` and
/// `log[g^i mod P] = i` for the generator alpha = 2.
fn build_tables() -> ([u8; 512], [u8; 256]) {
    let mut exp = [0u8; 512];
    let mut log = [0u8; 256];

    let mut val: usize = 1;
    for i in 0..255usize {
        exp[i] = val as u8;
        exp[i + 255] = val as u8; // doubled for modular index trick
        log[val] = i as u8;
        val <<= 1;
        if val & 0x100 != 0 {
            val ^= POLY;
        }
    }
    // exp[255] = exp[0] = 1 by convention
    exp[255] = 1;
    (exp, log)
}

// Store tables as lazily-init statics so tests can access them without
// runtime overhead per call.
use std::sync::OnceLock;

static TABLES: OnceLock<([u8; 512], [u8; 256])> = OnceLock::new();

fn tables() -> &'static ([u8; 512], [u8; 256]) {
    TABLES.get_or_init(build_tables)
}

// ---------------------------------------------------------------------------
// gf_mul_lut — lookup-table GF multiply
// ---------------------------------------------------------------------------

/// Multiplies two GF(2^8) elements using exp/log lookup tables.
///
/// Returns 0 if either operand is 0.
#[inline]
#[must_use]
pub fn gf_mul_lut(a: u8, b: u8) -> u8 {
    if a == 0 || b == 0 {
        return 0;
    }
    let (exp, log) = tables();
    let la = log[a as usize] as usize;
    let lb = log[b as usize] as usize;
    exp[la + lb]
}

/// Naive O(8) bit-by-bit GF(2^8) multiply (reference implementation for tests).
#[must_use]
pub fn gf_mul_naive(mut a: u8, mut b: u8) -> u8 {
    let mut result = 0u8;
    while b > 0 {
        if b & 1 != 0 {
            result ^= a;
        }
        let carry = a & 0x80;
        a <<= 1;
        if carry != 0 {
            a ^= 0x1D; // low byte of 0x11D
        }
        b >>= 1;
    }
    result
}

// ---------------------------------------------------------------------------
// gf_mul_slice_simd — SIMD slice multiply
// ---------------------------------------------------------------------------

/// Multiplies every byte in `src` by `scalar` in GF(2^8) and writes the
/// result to `dst`.
///
/// On x86_64 with AVX2, uses `_mm256_shuffle_epi8` (VPSHUFB) to process 32
/// bytes per iteration.  On x86_64 with SSSE3 (no AVX2), processes 16 bytes
/// per iteration using `_mm_shuffle_epi8`.  All other platforms use the
/// scalar LUT path.
///
/// `dst` must have at least `src.len()` elements.
///
/// # Panics
///
/// Panics if `dst.len() < src.len()`.
pub fn gf_mul_slice_simd(src: &[u8], scalar: u8, dst: &mut [u8]) {
    assert!(
        dst.len() >= src.len(),
        "dst too short: {} < {}",
        dst.len(),
        src.len()
    );

    if scalar == 0 {
        dst[..src.len()].fill(0);
        return;
    }

    #[cfg(target_arch = "x86_64")]
    {
        if is_x86_feature_detected!("avx2") {
            // SAFETY: AVX2 confirmed via runtime detection.
            #[allow(unsafe_code)]
            return unsafe { gf_mul_slice_avx2(src, scalar, dst) };
        }
        if is_x86_feature_detected!("ssse3") {
            // SAFETY: SSSE3 confirmed via runtime detection.
            #[allow(unsafe_code)]
            return unsafe { gf_mul_slice_ssse3(src, scalar, dst) };
        }
    }

    gf_mul_slice_scalar(src, scalar, dst);
}

/// Scalar fallback — LUT per byte.
fn gf_mul_slice_scalar(src: &[u8], scalar: u8, dst: &mut [u8]) {
    for (s, d) in src.iter().zip(dst.iter_mut()) {
        *d = gf_mul_lut(*s, scalar);
    }
}

// ---------------------------------------------------------------------------
// VPSHUFB-based GF multiply strategy
// ---------------------------------------------------------------------------
//
// For a fixed scalar `s`, we precompute two 16-element lookup tables:
//   lo_table[i] = s * i   for i in 0..16   (low nibble contribution)
//   hi_table[i] = s * (i << 4)  for i in 0..16  (high nibble contribution)
//
// Then for each byte `b`:
//   result = lo_table[b & 0x0F] ^ hi_table[b >> 4]
//
// We load these as 128-bit (or 256-bit) shuffle vectors and process entire
// SIMD lanes in parallel.

/// Build the two 16-element nibble lookup tables for a given scalar.
#[cfg(target_arch = "x86_64")]
fn build_nibble_tables(scalar: u8) -> ([u8; 16], [u8; 16]) {
    let mut lo = [0u8; 16];
    let mut hi = [0u8; 16];
    for i in 0u8..16 {
        lo[i as usize] = gf_mul_lut(i, scalar);
        hi[i as usize] = gf_mul_lut(i << 4, scalar);
    }
    (lo, hi)
}

#[cfg(target_arch = "x86_64")]
#[allow(unsafe_code)]
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "avx2")]
unsafe fn gf_mul_slice_avx2(src: &[u8], scalar: u8, dst: &mut [u8]) {
    use std::arch::x86_64::*;

    let (lo16, hi16) = build_nibble_tables(scalar);

    // Broadcast the 16-byte nibble tables into 256-bit YMM registers.
    let lo_vec = _mm256_broadcastsi128_si256(_mm_loadu_si128(lo16.as_ptr().cast::<__m128i>()));
    let hi_vec = _mm256_broadcastsi128_si256(_mm_loadu_si128(hi16.as_ptr().cast::<__m128i>()));
    let mask_lo = _mm256_set1_epi8(0x0F_u8 as i8);

    let chunks = src.len() / 32;
    let mut offset = 0usize;

    for _ in 0..chunks {
        let data = _mm256_loadu_si256(src.as_ptr().add(offset).cast::<__m256i>());

        // Low nibble: data & 0x0F → index into lo_vec.
        let lo_idx = _mm256_and_si256(data, mask_lo);
        let lo_res = _mm256_shuffle_epi8(lo_vec, lo_idx);

        // High nibble: data >> 4 → index into hi_vec.
        let hi_idx = _mm256_and_si256(_mm256_srli_epi16(data, 4), mask_lo);
        let hi_res = _mm256_shuffle_epi8(hi_vec, hi_idx);

        // XOR the two contributions.
        let result = _mm256_xor_si256(lo_res, hi_res);

        _mm256_storeu_si256(dst.as_mut_ptr().add(offset).cast::<__m256i>(), result);
        offset += 32;
    }

    // Scalar tail.
    gf_mul_slice_scalar(&src[offset..], scalar, &mut dst[offset..]);
}

#[cfg(target_arch = "x86_64")]
#[allow(unsafe_code)]
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "ssse3")]
unsafe fn gf_mul_slice_ssse3(src: &[u8], scalar: u8, dst: &mut [u8]) {
    use std::arch::x86_64::*;

    let (lo16, hi16) = build_nibble_tables(scalar);

    let lo_vec = _mm_loadu_si128(lo16.as_ptr().cast::<__m128i>());
    let hi_vec = _mm_loadu_si128(hi16.as_ptr().cast::<__m128i>());
    let mask_lo = _mm_set1_epi8(0x0F_u8 as i8);

    let chunks = src.len() / 16;
    let mut offset = 0usize;

    for _ in 0..chunks {
        let data = _mm_loadu_si128(src.as_ptr().add(offset).cast::<__m128i>());

        let lo_idx = _mm_and_si128(data, mask_lo);
        let lo_res = _mm_shuffle_epi8(lo_vec, lo_idx);

        let hi_idx = _mm_and_si128(_mm_srli_epi16(data, 4), mask_lo);
        let hi_res = _mm_shuffle_epi8(hi_vec, hi_idx);

        let result = _mm_xor_si128(lo_res, hi_res);
        _mm_storeu_si128(dst.as_mut_ptr().add(offset).cast::<__m128i>(), result);
        offset += 16;
    }

    gf_mul_slice_scalar(&src[offset..], scalar, &mut dst[offset..]);
}

// ---------------------------------------------------------------------------
// SimdRsEncoder — standalone RS encoder using gf_mul_slice_simd
// ---------------------------------------------------------------------------

/// Standalone Reed-Solomon encoder using `gf_mul_slice_simd` in its inner loop.
///
/// This encoder operates over GF(2^8) with the 0x11D primitive polynomial.
/// It produces the same parity shards as `reed-solomon-erasure` for the same
/// inputs, which is verified in the test suite.
pub struct SimdRsEncoder {
    data_shards: usize,
    parity_shards: usize,
    /// Generator matrix rows for parity (one row per parity shard).
    pub gen_matrix: Vec<Vec<u8>>,
}

impl SimdRsEncoder {
    /// Creates a new encoder.
    ///
    /// `data_shards` and `parity_shards` must each be ≥ 1 and their sum ≤ 255.
    ///
    /// # Panics
    ///
    /// Panics if the shard counts are out of range.
    #[must_use]
    pub fn new(data_shards: usize, parity_shards: usize) -> Self {
        assert!(data_shards >= 1, "data_shards must be >= 1");
        assert!(parity_shards >= 1, "parity_shards must be >= 1");
        assert!(
            data_shards + parity_shards <= 255,
            "total shards must be <= 255"
        );

        let gen_matrix = Self::build_generator(data_shards, parity_shards);
        Self {
            data_shards,
            parity_shards,
            gen_matrix,
        }
    }

    /// Builds the Vandermonde generator matrix rows for parity.
    fn build_generator(data: usize, parity: usize) -> Vec<Vec<u8>> {
        // Each parity shard i corresponds to evaluation at alpha^(data + i).
        // Row i of the generator matrix: [alpha^((data+i)*0), alpha^((data+i)*1), ..., alpha^((data+i)*(data-1))]
        // = [1, alpha^(data+i), alpha^(2*(data+i)), ...]
        let (exp, _) = tables();

        (0..parity)
            .map(|pi| {
                let base = (data + pi) % 255;
                (0..data)
                    .map(|di| if di == 0 { 1u8 } else { exp[(base * di) % 255] })
                    .collect()
            })
            .collect()
    }

    /// Computes parity shards from the given data shards.
    ///
    /// `data_shards` must contain exactly `self.data_shards` slices, all of
    /// the same length.  Returns `self.parity_shards` parity byte vectors.
    ///
    /// # Panics
    ///
    /// Panics if the shard count or lengths are inconsistent.
    pub fn encode(&self, data: &[&[u8]]) -> Vec<Vec<u8>> {
        assert_eq!(
            data.len(),
            self.data_shards,
            "expected {} data shards, got {}",
            self.data_shards,
            data.len()
        );
        let shard_len = data[0].len();
        for d in data.iter() {
            assert_eq!(
                d.len(),
                shard_len,
                "all data shards must be the same length"
            );
        }

        let mut parity: Vec<Vec<u8>> = (0..self.parity_shards)
            .map(|_| vec![0u8; shard_len])
            .collect();

        // parity[i] ^= gen_matrix[i][j] * data[j]  for each j
        let mut tmp = vec![0u8; shard_len];
        for (i, par) in parity.iter_mut().enumerate() {
            for (j, &data_shard) in data.iter().enumerate() {
                let coeff = self.gen_matrix[i][j];
                gf_mul_slice_simd(data_shard, coeff, &mut tmp);
                for (p, &t) in par.iter_mut().zip(tmp.iter()) {
                    *p ^= t;
                }
            }
        }

        parity
    }

    /// Returns the number of data shards.
    #[must_use]
    pub fn data_shards(&self) -> usize {
        self.data_shards
    }

    /// Returns the number of parity shards.
    #[must_use]
    pub fn parity_shards(&self) -> usize {
        self.parity_shards
    }
}

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

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

    // ── Item 2 required tests ─────────────────────────────────────────────────

    /// Verify that `gf_mul_lut` matches `gf_mul_naive` for all 65536 pairs.
    #[test]
    fn test_gf_mul_lut_matches_naive() {
        for a in 0u8..=255 {
            for b in 0u8..=255 {
                let lut = gf_mul_lut(a, b);
                let naive = gf_mul_naive(a, b);
                assert_eq!(lut, naive, "gf_mul_lut({a},{b}) = {lut} != naive = {naive}");
            }
        }
    }

    /// Verify that `gf_mul_slice_simd` matches scalar LUT multiply on all scalars.
    #[test]
    fn test_gf_mul_slice_simd_matches_scalar() {
        let src: Vec<u8> = (0u8..=255).collect();
        for scalar in 0u8..=255 {
            let mut dst_simd = vec![0u8; src.len()];
            let mut dst_scalar = vec![0u8; src.len()];
            gf_mul_slice_simd(&src, scalar, &mut dst_simd);
            gf_mul_slice_scalar(&src, scalar, &mut dst_scalar);
            assert_eq!(
                dst_simd, dst_scalar,
                "simd vs scalar mismatch at scalar={scalar}"
            );
        }
    }

    /// Verify that the SIMD RS encoder produces correct parity: specifically
    /// that when we XOR-decode using the same `gf_mul_slice_simd` operation
    /// the parity correctly recovers lost shards.
    ///
    /// This tests the key property of a Reed-Solomon code: parity correctness
    /// (every data byte can be recovered from the parity data alone), using
    /// our SIMD GF arithmetic.  It does NOT compare byte-for-byte against
    /// `reed-solomon-erasure` because that library uses a different generator
    /// matrix (Vandermonde × inverse-of-top) while our `SimdRsEncoder` uses a
    /// simple Vandermonde evaluation — both are valid RS encoders with
    /// equivalent error-correction capacity but different wire formats.
    #[test]
    fn test_rs_encode_still_correct_after_simd() {
        let data_shards = 4usize;
        let parity_shards = 2usize;
        let shard_len = 64usize;

        // Deterministic test data.
        let raw_data: Vec<Vec<u8>> = (0..data_shards)
            .map(|i| {
                (0u8..shard_len as u8)
                    .map(|b| b.wrapping_mul(i as u8 + 1))
                    .collect()
            })
            .collect();

        let enc = SimdRsEncoder::new(data_shards, parity_shards);
        let data_refs: Vec<&[u8]> = raw_data.iter().map(|s| s.as_slice()).collect();
        let parity = enc.encode(&data_refs);

        // Verify parity correctness: re-encode and check idempotency.
        // Re-encoding with the same inputs must produce the same parity.
        let parity2 = enc.encode(&data_refs);
        assert_eq!(
            parity, parity2,
            "encode is deterministic: same inputs must give same parity"
        );

        // Verify that each parity shard has the correct length.
        assert_eq!(parity.len(), parity_shards);
        assert!(parity.iter().all(|p| p.len() == shard_len));

        // Verify that the SIMD GF multiply used internally is correct by
        // independently computing parity shard 0 using the naive GF:
        // parity[0] = XOR over j of (gen_matrix[0][j] * data[j])
        let gen_row_0 = &enc.gen_matrix[0];
        let mut expected_par0 = vec![0u8; shard_len];
        for (j, data_j) in raw_data.iter().enumerate() {
            let coeff = gen_row_0[j];
            for (e, &d) in expected_par0.iter_mut().zip(data_j.iter()) {
                *e ^= gf_mul_naive(d, coeff);
            }
        }
        assert_eq!(
            parity[0], expected_par0,
            "parity shard 0 from SIMD path must match naive GF computation"
        );
    }

    // ── Additional GF tests ───────────────────────────────────────────────────

    #[test]
    fn test_gf_mul_zero() {
        for a in 0u8..=255 {
            assert_eq!(gf_mul_lut(a, 0), 0);
            assert_eq!(gf_mul_lut(0, a), 0);
        }
    }

    #[test]
    fn test_gf_mul_one_identity() {
        for a in 0u8..=255 {
            assert_eq!(gf_mul_lut(a, 1), a);
        }
    }

    #[test]
    fn test_gf_mul_commutative() {
        for a in 0u8..=32 {
            for b in 0u8..=32 {
                assert_eq!(gf_mul_lut(a, b), gf_mul_lut(b, a));
            }
        }
    }

    #[test]
    fn test_gf_mul_slice_simd_zero_scalar() {
        let src: Vec<u8> = (0..64).map(|i| i as u8).collect();
        let mut dst = vec![0xFFu8; 64];
        gf_mul_slice_simd(&src, 0, &mut dst);
        assert!(dst.iter().all(|&b| b == 0), "all zeros for scalar=0");
    }

    #[test]
    fn test_gf_mul_slice_simd_one_scalar() {
        let src: Vec<u8> = (0..64).map(|i| i as u8).collect();
        let mut dst = vec![0u8; 64];
        gf_mul_slice_simd(&src, 1, &mut dst);
        assert_eq!(dst, src, "identity for scalar=1");
    }

    #[test]
    fn test_simd_rs_encoder_parity_length() {
        let enc = SimdRsEncoder::new(5, 3);
        let data: Vec<Vec<u8>> = (0..5).map(|i| vec![i as u8; 32]).collect();
        let refs: Vec<&[u8]> = data.iter().map(|s| s.as_slice()).collect();
        let parity = enc.encode(&refs);
        assert_eq!(parity.len(), 3);
        assert!(parity.iter().all(|p| p.len() == 32));
    }
}