diff --git a/docs/implementation-plan.md b/docs/implementation-plan.md index cbfe454..d40c5a8 100644 --- a/docs/implementation-plan.md +++ b/docs/implementation-plan.md @@ -451,10 +451,13 @@ result under controlled loss/jitter/reorder before changing carriers. The first cut landed in Odin on 2026-07-16: experimental CultLib snapshot `8965f3c0`, epoch/sequence/edge-ack HID delivery, 100 ms default LAN media deadline, CultNet `realtime` A/V delivery, bounded queues, expiring/late-aware -repair, and decode-chain-owned keyframe pressure. Next reliability work is real -encoder keyframe actuation, production parity recovery, audio FEC/concealment, -bounded long-disconnect edge admission, adaptive sender response, and the -fault-injected socket timeline harness. +repair, and decode-chain-owned keyframe pressure. The production video path now +uses typed V3 Cauchy GF(256) FEC with sixteen parity shards per access unit; +the OBS receiver reconstructs arbitrary erasure sets up to the received parity +count, while V2 stripe parity remains a distinct non-authoritative schema. +Sender access units enter the handoff queue independently, and CultMesh catalog +publication runs outside the realtime media loop. The remaining work is the +full direct/proxy impairment matrix, Opus FEC/PLC, and long-duration mixed soak. The socket harness and bounded PCM loss recovery have now landed. Odin's `cultnet-impair` proxy supplies deterministic seeded loss/burst/reorder/ diff --git a/native/obs_stem_source/CMakeLists.txt b/native/obs_stem_source/CMakeLists.txt index 527739d..6e19ee1 100644 --- a/native/obs_stem_source/CMakeLists.txt +++ b/native/obs_stem_source/CMakeLists.txt @@ -46,6 +46,13 @@ target_include_directories(muninn_audio_fec_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) add_test(NAME muninn_audio_fec_tests COMMAND muninn_audio_fec_tests) +add_executable(muninn_video_fec_tests + tests/muninn_video_fec_tests.cpp) +target_compile_features(muninn_video_fec_tests PRIVATE cxx_std_20) +target_include_directories(muninn_video_fec_tests PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}) +add_test(NAME muninn_video_fec_tests COMMAND muninn_video_fec_tests) + add_executable(muninn_udp_socket_tests tests/muninn_udp_socket_tests.cpp) target_compile_features(muninn_udp_socket_tests PRIVATE cxx_std_20) diff --git a/native/obs_stem_source/mimir_obs_muninn_source.cpp b/native/obs_stem_source/mimir_obs_muninn_source.cpp index 9910999..9743073 100644 --- a/native/obs_stem_source/mimir_obs_muninn_source.cpp +++ b/native/obs_stem_source/mimir_obs_muninn_source.cpp @@ -13,6 +13,7 @@ #include "muninn_rudp_fragments.h" #include "muninn_rudp_url.h" #include "muninn_udp_socket.h" +#include "muninn_video_fec.h" #include #include @@ -224,6 +225,7 @@ struct MuninnMediaPayload { uint16_t chunk_count = 1; uint16_t parity_index = 0; uint16_t parity_count = 0; + bool video_fec_cauchy = false; std::vector chunk_payload_lengths; std::vector payload; }; @@ -240,6 +242,7 @@ struct MuninnVideoFrameAssembly { bool repair_requested = false; std::vector chunk_payload_lengths; uint16_t parity_count = 0; + bool video_fec_cauchy = false; std::map> parity_payloads; std::vector> chunks; }; @@ -253,6 +256,7 @@ struct MuninnVideoParityCacheEntry { uint16_t chunk_count = 0; uint16_t parity_index = 0; uint16_t parity_count = 0; + bool video_fec_cauchy = false; std::chrono::steady_clock::time_point received_at; std::vector chunk_payload_lengths; std::vector payload; @@ -655,7 +659,8 @@ static std::optional decode_muninn_media_payload(const uint8 media.payload = record.read_bytes(); return media; } - if (schema_id == "muninn.media_video_parity_shard.v2") { + if (schema_id == "muninn.media_video_parity_shard.v2" || + schema_id == "muninn.media_video_parity_shard.v3") { if (record_len < 17) { return std::nullopt; } @@ -675,6 +680,7 @@ static std::optional decode_muninn_media_payload(const uint8 media.chunk_count = static_cast(record.read_unsigned()); media.parity_index = static_cast(record.read_unsigned()); media.parity_count = static_cast(record.read_unsigned()); + media.video_fec_cauchy = schema_id == "muninn.media_video_parity_shard.v3"; const uint32_t chunk_payload_bytes = static_cast(record.read_unsigned()); const uint32_t last_chunk_payload_bytes = static_cast(record.read_unsigned()); if (media.chunk_count == 0 || @@ -1600,7 +1606,15 @@ private: log_bridge_pressure("dropped video parity with mixed stripe count"); return std::nullopt; } + if (assembly.parity_count != 0 && assembly.video_fec_cauchy != media.video_fec_cauchy) { + video_assemblies_.erase(key); + ++payloads_decoded_dropped_; + ++video_parity_unusable_; + log_bridge_pressure("dropped video parity with mixed FEC scheme"); + return std::nullopt; + } assembly.parity_count = media.parity_count; + assembly.video_fec_cauchy = media.video_fec_cauchy; assembly.parity_payloads[media.parity_index] = media.payload; if (try_recover_video_assembly_from_parity(assembly)) { ++video_parity_recovered_; @@ -1621,6 +1635,7 @@ private: entry.chunk_count = media.chunk_count; entry.parity_index = media.parity_index; entry.parity_count = media.parity_count; + entry.video_fec_cauchy = media.video_fec_cauchy; entry.received_at = std::chrono::steady_clock::now(); entry.chunk_payload_lengths = media.chunk_payload_lengths; entry.payload = media.payload; @@ -1641,9 +1656,12 @@ private: entry.dependency_frame_id == assembly.dependency_frame_id && entry.parity_count != 0 && entry.parity_index < entry.parity_count && - (assembly.parity_count == 0 || assembly.parity_count == entry.parity_count)) { + (assembly.parity_count == 0 || + (assembly.parity_count == entry.parity_count && + assembly.video_fec_cauchy == entry.video_fec_cauchy))) { assembly.chunk_payload_lengths = entry.chunk_payload_lengths; assembly.parity_count = entry.parity_count; + assembly.video_fec_cauchy = entry.video_fec_cauchy; assembly.parity_payloads[entry.parity_index] = entry.payload; } else { ++video_parity_unusable_; @@ -1671,58 +1689,21 @@ private: bool try_recover_video_assembly_from_parity(MuninnVideoFrameAssembly &assembly) { if (assembly.parity_count == 0 || + !assembly.video_fec_cauchy || assembly.parity_payloads.empty() || assembly.chunk_payload_lengths.size() != assembly.chunk_count || assembly.chunks.size() != assembly.chunk_count) { return false; } - bool recovered_any = true; - while (recovered_any && assembly.chunks_received < assembly.chunk_count) { - recovered_any = false; - for (const auto &parity : assembly.parity_payloads) { - const uint16_t parity_index = parity.first; - if (parity_index >= assembly.parity_count || parity.second.empty()) { - continue; - } - uint16_t missing_index = 0; - uint16_t missing_count = 0; - for (uint16_t index = parity_index; index < assembly.chunk_count; index += assembly.parity_count) { - if (assembly.chunks[index].empty()) { - missing_index = index; - ++missing_count; - } - } - if (missing_count != 1) { - continue; - } - const uint32_t missing_len = assembly.chunk_payload_lengths[missing_index]; - if (missing_len == 0 || missing_len > parity.second.size()) { - continue; - } - std::vector recovered = parity.second; - bool stripe_complete = true; - for (uint16_t index = parity_index; index < assembly.chunk_count; index += assembly.parity_count) { - if (index == missing_index) { - continue; - } - const auto &chunk = assembly.chunks[index]; - if (chunk.empty()) { - stripe_complete = false; - break; - } - for (size_t offset = 0; offset < chunk.size() && offset < recovered.size(); ++offset) { - recovered[offset] ^= chunk[offset]; - } - } - if (!stripe_complete) { - continue; - } - recovered.resize(missing_len); - assembly.chunks[missing_index] = std::move(recovered); - ++assembly.chunks_received; - recovered_any = true; - } + const uint16_t missing = assembly.chunk_count - assembly.chunks_received; + if (!muninn_video_fec::recover( + assembly.chunks, + assembly.chunk_payload_lengths, + assembly.parity_count, + assembly.parity_payloads)) { + return false; } + assembly.chunks_received += missing; return assembly.chunks_received == assembly.chunk_count; } diff --git a/native/obs_stem_source/muninn_video_fec.h b/native/obs_stem_source/muninn_video_fec.h new file mode 100644 index 0000000..7bbce51 --- /dev/null +++ b/native/obs_stem_source/muninn_video_fec.h @@ -0,0 +1,127 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace muninn_video_fec { + +inline uint8_t multiply(uint8_t a, uint8_t b) +{ + uint8_t product = 0; + while (b != 0) { + if ((b & 1) != 0) + product ^= a; + const bool carry = (a & 0x80) != 0; + a <<= 1; + if (carry) + a ^= 0x1d; + b >>= 1; + } + return product; +} + +inline uint8_t inverse(uint8_t value) +{ + uint8_t result = 1; + for (size_t index = 0; index < 254; ++index) + result = multiply(result, value); + return result; +} + +inline uint8_t coefficient(uint16_t parity_index, uint16_t parity_count, uint16_t data_index) +{ + return inverse( + static_cast(parity_index) ^ static_cast(parity_count + data_index)); +} + +inline bool recover(std::vector> &chunks, + const std::vector &chunk_lengths, + uint16_t parity_count, + const std::map> &parity) +{ + if (chunks.size() != chunk_lengths.size() || chunks.empty() || + chunks.size() + parity_count > 256) + return false; + std::vector missing; + for (uint16_t index = 0; index < chunks.size(); ++index) { + if (chunks[index].empty()) + missing.push_back(index); + } + if (missing.empty()) + return true; + if (missing.size() > parity.size()) + return false; + + std::vector *>> rows; + for (const auto &shard : parity) { + if (shard.first < parity_count && !shard.second.empty()) + rows.emplace_back(shard.first, &shard.second); + if (rows.size() == missing.size()) + break; + } + if (rows.size() != missing.size()) + return false; + + size_t shard_bytes = 0; + for (uint32_t length : chunk_lengths) + shard_bytes = std::max(shard_bytes, static_cast(length)); + if (shard_bytes == 0 || std::any_of(rows.begin(), rows.end(), + [shard_bytes](const auto &row) { return row.second->size() < shard_bytes; })) + return false; + + std::vector> recovered; + recovered.reserve(missing.size()); + for (uint16_t index : missing) { + if (chunk_lengths[index] == 0 || chunk_lengths[index] > shard_bytes) + return false; + recovered.emplace_back(chunk_lengths[index], 0); + } + + const size_t count = missing.size(); + for (size_t offset = 0; offset < shard_bytes; ++offset) { + std::vector> matrix(count, std::vector(count + 1, 0)); + for (size_t row = 0; row < count; ++row) { + const uint16_t parity_index = rows[row].first; + uint8_t rhs = (*rows[row].second)[offset]; + for (uint16_t data_index = 0; data_index < chunks.size(); ++data_index) { + if (!chunks[data_index].empty() && offset < chunks[data_index].size()) + rhs ^= multiply(coefficient(parity_index, parity_count, data_index), + chunks[data_index][offset]); + } + for (size_t column = 0; column < count; ++column) + matrix[row][column] = coefficient(parity_index, parity_count, missing[column]); + matrix[row][count] = rhs; + } + for (size_t column = 0; column < count; ++column) { + size_t pivot = column; + while (pivot < count && matrix[pivot][column] == 0) + ++pivot; + if (pivot == count) + return false; + std::swap(matrix[column], matrix[pivot]); + const uint8_t scale = inverse(matrix[column][column]); + for (size_t cell = column; cell <= count; ++cell) + matrix[column][cell] = multiply(matrix[column][cell], scale); + for (size_t row = 0; row < count; ++row) { + if (row == column) + continue; + const uint8_t factor = matrix[row][column]; + for (size_t cell = column; cell <= count; ++cell) + matrix[row][cell] ^= multiply(factor, matrix[column][cell]); + } + } + for (size_t index = 0; index < count; ++index) { + if (offset < recovered[index].size()) + recovered[index][offset] = matrix[index][count]; + } + } + for (size_t index = 0; index < count; ++index) + chunks[missing[index]] = std::move(recovered[index]); + return true; +} + +} // namespace muninn_video_fec diff --git a/native/obs_stem_source/tests/muninn_video_fec_tests.cpp b/native/obs_stem_source/tests/muninn_video_fec_tests.cpp new file mode 100644 index 0000000..b5e1533 --- /dev/null +++ b/native/obs_stem_source/tests/muninn_video_fec_tests.cpp @@ -0,0 +1,41 @@ +#include "muninn_video_fec.h" + +#include +#include +#include +#include + +int main() +{ + constexpr uint16_t data_count = 64; + constexpr uint16_t parity_count = 16; + constexpr size_t shard_bytes = 97; + std::vector> original(data_count, std::vector(shard_bytes)); + for (uint16_t data = 0; data < data_count; ++data) { + for (size_t offset = 0; offset < shard_bytes; ++offset) + original[data][offset] = static_cast((data * 29 + offset * 17) & 0xff); + } + std::map> parity; + for (uint16_t row = 0; row < parity_count; ++row) { + auto &shard = parity[row]; + shard.assign(shard_bytes, 0); + for (uint16_t data = 0; data < data_count; ++data) { + const uint8_t coefficient = muninn_video_fec::coefficient(row, parity_count, data); + for (size_t offset = 0; offset < shard_bytes; ++offset) + shard[offset] ^= muninn_video_fec::multiply(original[data][offset], coefficient); + } + } + const std::vector lengths(data_count, shard_bytes); + for (uint16_t burst = 1; burst <= parity_count; ++burst) { + auto damaged = original; + for (uint16_t index = 0; index < burst; ++index) + damaged[20 + index].clear(); + assert(muninn_video_fec::recover(damaged, lengths, parity_count, parity)); + assert(damaged == original); + } + auto excessive = original; + for (uint16_t index = 0; index <= parity_count; ++index) + excessive[index].clear(); + assert(!muninn_video_fec::recover(excessive, lengths, parity_count, parity)); + return 0; +} diff --git a/state/evidence.jsonl b/state/evidence.jsonl index ffda9f8..38c78be 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -194,3 +194,4 @@ {"ts": "2026-07-16T21:26:00+01:00", "type": "verification", "status": "ok", "note": "Receiver decode-chain invalidation now actuates a verified next-frame NVENC IDR through Odin's long-lived libav encoder without restarting A/V or CultNet. Live 2560x1440 D3D11 proof emitted a command-driven IDR at frame 22 under a 600-frame GOP; synthetic proof emitted IDRs at frames 0 and 10 and decoded cleanly. Muninn/Sleipnir edge admission is bounded at 256 via epoch rebase; 174/26 tests pass. Raven field deployment remains pending because 10.77.0.4 SSH and TCP probing timed out."} {"ts": "2026-07-16T21:35:47+01:00", "type": "verification", "status": "ok", "note": "Muninn receiver pressure now drives bounded AIMD bitrate on the long-lived NVENC context. Live 2560x1440 D3D11 proof reconfigured 12 Mbps to 6 Mbps at frame 35, emitted an IDR at frame 35, and decoded cleanly; 176 Muninn tests pass. Cross-host field acceptance remains missing because Raven SSH/TCP 22 timed out again."} {"ts": "2026-07-16T21:38:23+01:00", "type": "blocker", "status": "pending", "note": "Field Moonlight acceptance cannot run while Raven is offline: Starfire's documented SSH/ICMP/TCP route to 10.77.0.4 times out, and Yggdrasil reports Raven's last WireGuard handshake at 2026-07-15T06:04:12Z versus Starfire's live handshake at 2026-07-16T20:36:32Z. No Raven task or host state was mutated."} +{"ts": "2026-07-17T01:37:22+01:00", "type": "verification", "status": "partial", "note": "Muninn video parity v3 uses 16 Cauchy GF(256) shards; native receiver burst tests 1..16 and Odin 180-test suite pass. Direct Raven LAN at 250 ms recovery deadline ran over one minute with no video assembly expiry or A/V failure; full 10-minute and impairment matrix remain open."} diff --git a/state/map.yaml b/state/map.yaml index 041cbc2..b1e0281 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -87,6 +87,16 @@ invariants: are temporary smoke/bootstrap edges only. accepted_design: + - Muninn video FEC is now typed as `muninn.media_video_parity_shard.v3`. + Odin emits 16 Cauchy GF(256) parity shards after each multi-chunk access + unit, so any erasure set up to the received parity count is recoverable; + Mimir's OBS receiver owns the matching bounded matrix recovery before the + assembly deadline. V2 stripe parity is never reinterpreted as V3. Native + receiver tests prove consecutive bursts from one through sixteen shards, + Odin's 180 Muninn tests pass, and the first direct Raven LAN field minute + at a 250 ms recovery deadline had no video assembly expiry or A/V failure. + Normal frames are forwarded immediately; the recovery deadline is not a + playout buffer. The ten-minute and impaired acceptance gates remain open. - Muninn now closes receiver congestion pressure into the live NVENC context. Every 500 ms it applies multiplicative 15 percent backoff for late frames, requested recovery, deferred repair, sender queue loss, or combined @@ -95,7 +105,7 @@ accepted_design: seconds, with a one-quarter/1 Mbps floor. A live 2560x1440 D3D11 proof changed 12 Mbps to 6 Mbps at frame 35; FFmpeg/NVENC reconfigured the same session, emitted an IDR at frame 35, and the Annex-B stream decoded cleanly. - Muninn's full suite now passes 176 tests. + Muninn's full suite now passes 180 tests. - Receiver-driven video recovery now reaches the actual NVENC frame owner. Odin's `native/muninn-video-encoder` is a long-lived libavdevice desktop capture and `h264_nvenc` process; a new decode-chain invalidation edge makes