Raw ↗
+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
1 /*
2 * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0
3 * Copyright (c) 2026 Saif Mukhtar
4 *
5 * OpenAVX - Open Audio-Video eXchange
6 * src/avx_fec.c - Pure C99 & SIMD SMPTE ST 2022-1 2D XOR Forward Error Correction
7 */
8  
9 #include "avx_fec.h"
10 #include <string.h>
11 #include <stdlib.h>
12  
13 #if defined(__AVX2__)
14 #include <immintrin.h>
15 #endif
16  
17 #if defined(__ARM_NEON) || defined(__ARM_NEON__)
18 #include <arm_neon.h>
19 #endif
20  
21 /*
22 * SIMD XOR accumulation kernel for FEC block parity. Computes dst ^= src using
23 * AVX2 (256-bit) or NEON (128-bit) with 64-bit word and byte-level tail processing.
24 */
25 void avx_fec_xor_block(uint8_t *dst, const uint8_t *src, size_t len) {
26 if (!dst || !src || len == 0) return;
27  
28 size_t i = 0;
29  
30 #if defined(__AVX2__)
31 for (; i + 32 <= len; i += 32) {
32 __m256i vd = _mm256_loadu_si256((const __m256i*)(dst + i));
33 __m256i vs = _mm256_loadu_si256((const __m256i*)(src + i));
34 _mm256_storeu_si256((__m256i*)(dst + i), _mm256_xor_si256(vd, vs));
35 }
36 #elif defined(__ARM_NEON) || defined(__ARM_NEON__)
37 for (; i + 16 <= len; i += 16) {
38 uint8x16_t vd = vld1q_u8(dst + i);
39 uint8x16_t vs = vld1q_u8(src + i);
40 vst1q_u8(dst + i, veorq_u8(vd, vs));
41 }
42 #endif
43  
44 /* 64-bit scalar tail */
45 for (; i + 8 <= len; i += 8) {
46 uint64_t vd, vs;
47 memcpy(&vd, dst + i, 8);
48 memcpy(&vs, src + i, 8);
49 vd ^= vs;
50 memcpy(dst + i, &vd, 8);
51 }
52  
53 /* Byte tail */
54 for (; i < len; ++i) {
55 dst[i] ^= src[i];
56 }
57 }
58  
59 /*
60 * Generates SMPTE ST 2022-1 2D XOR FEC parity matrices. Column parity protects against
61 * long burst packet losses; optional row parity protects against dispersed losses.
62 */
63 int avx_fec_generate_block(
64 const avx_fec_slice_t *media_slices,
65 uint16_t block_frags,
66 uint16_t matrix_d,
67 uint16_t matrix_l,
68 bool enable_row_fec,
69 avx_fec_slice_t *out_col_parity,
70 avx_fec_slice_t *out_row_parity
71 ) {
72 if (!media_slices || block_frags == 0 || matrix_d == 0 || matrix_l == 0 || !out_col_parity) {
73 return -1;
74 }
75 if (matrix_d > AVX_FEC_MAX_COLS || matrix_l > AVX_FEC_MAX_ROWS) {
76 return -1;
77 }
78  
79 /* 1. Generate Column Parity (1D FEC): C[j] = XOR over row i of P[i * D + j] */
80 for (uint16_t j = 0; j < matrix_d; ++j) {
81 /* Find maximum fragment length in this column */
82 size_t max_col_len = 0;
83 for (uint16_t i = 0; i < matrix_l; ++i) {
84 uint16_t idx = (uint16_t)(i * matrix_d + j);
85 if (idx < block_frags && media_slices[idx].data && media_slices[idx].len > max_col_len) {
86 max_col_len = media_slices[idx].len;
87 }
88 }
89  
90 if (max_col_len > 0 && out_col_parity[j].data) {
91 memset(out_col_parity[j].data, 0, max_col_len);
92 for (uint16_t i = 0; i < matrix_l; ++i) {
93 uint16_t idx = (uint16_t)(i * matrix_d + j);
94 if (idx < block_frags && media_slices[idx].data && media_slices[idx].len > 0) {
95 avx_fec_xor_block(out_col_parity[j].data, media_slices[idx].data, media_slices[idx].len);
96 }
97 }
98 out_col_parity[j].len = max_col_len;
99 } else {
100 out_col_parity[j].len = 0;
101 }
102 }
103  
104 /* 2. Generate Row Parity (2D FEC): R[i] = XOR over col j of P[i * D + j] */
105 if (enable_row_fec && out_row_parity) {
106 for (uint16_t i = 0; i < matrix_l; ++i) {
107 size_t max_row_len = 0;
108 for (uint16_t j = 0; j < matrix_d; ++j) {
109 uint16_t idx = (uint16_t)(i * matrix_d + j);
110 if (idx < block_frags && media_slices[idx].data && media_slices[idx].len > max_row_len) {
111 max_row_len = media_slices[idx].len;
112 }
113 }
114  
115 if (max_row_len > 0 && out_row_parity[i].data) {
116 memset(out_row_parity[i].data, 0, max_row_len);
117 for (uint16_t j = 0; j < matrix_d; ++j) {
118 uint16_t idx = (uint16_t)(i * matrix_d + j);
119 if (idx < block_frags && media_slices[idx].data && media_slices[idx].len > 0) {
120 avx_fec_xor_block(out_row_parity[i].data, media_slices[idx].data, media_slices[idx].len);
121 }
122 }
123 out_row_parity[i].len = max_row_len;
124 } else {
125 out_row_parity[i].len = 0;
126 }
127 }
128 }
129  
130 return 0;
131 }
132  
133 /*
134 * Iterative 2D erasure solver. Alternates row and column passes to reconstruct
135 * any vector containing exactly one missing packet until convergence or exhaustion.
136 */
137 int avx_fec_reconstruct_block(
138 avx_fec_slice_t *media_slices,
139 bool *is_present,
140 uint16_t block_frags,
141 uint16_t matrix_d,
142 uint16_t matrix_l,
143 const avx_fec_slice_t *col_parity,
144 const bool *col_present,
145 const avx_fec_slice_t *row_parity,
146 const bool *row_present
147 ) {
148 if (!media_slices || !is_present || block_frags == 0 || matrix_d == 0 || matrix_l == 0) {
149 return -1;
150 }
151 if (matrix_d > AVX_FEC_MAX_COLS || matrix_l > AVX_FEC_MAX_ROWS) {
152 return -1;
153 }
154  
155 int total_recovered = 0;
156 bool progress = true;
157  
158 /* Iterative 2D Solver Loop: continues while rows or columns with single missing packets can be resolved */
159 while (progress) {
160 progress = false;
161  
162 /* Step 1: Scan Columns */
163 if (col_parity && col_present) {
164 for (uint16_t j = 0; j < matrix_d; ++j) {
165 if (!col_present[j] || !col_parity[j].data || col_parity[j].len == 0) {
166 continue;
167 }
168  
169 int missing_count = 0;
170 uint16_t missing_row = 0;
171 for (uint16_t i = 0; i < matrix_l; ++i) {
172 uint16_t idx = (uint16_t)(i * matrix_d + j);
173 if (idx < block_frags && !is_present[idx]) {
174 missing_count++;
175 missing_row = i;
176 }
177 }
178  
179 /* Exactly 1 missing fragment in column j -> Reconstruct it! */
180 if (missing_count == 1) {
181 uint16_t target_idx = (uint16_t)(missing_row * matrix_d + j);
182 if (media_slices[target_idx].data) {
183 size_t parity_len = col_parity[j].len;
184 memcpy(media_slices[target_idx].data, col_parity[j].data, parity_len);
185  
186 for (uint16_t i = 0; i < matrix_l; ++i) {
187 if (i == missing_row) continue;
188 uint16_t other_idx = (uint16_t)(i * matrix_d + j);
189 if (other_idx < block_frags && is_present[other_idx] &&
190 media_slices[other_idx].data && media_slices[other_idx].len > 0) {
191 avx_fec_xor_block(media_slices[target_idx].data,
192 media_slices[other_idx].data,
193 media_slices[other_idx].len);
194 }
195 }
196  
197 media_slices[target_idx].len = parity_len;
198 is_present[target_idx] = true;
199 total_recovered++;
200 progress = true;
201 }
202 }
203 }
204 }
205  
206 /* Step 2: Scan Rows */
207 if (row_parity && row_present) {
208 for (uint16_t i = 0; i < matrix_l; ++i) {
209 if (!row_present[i] || !row_parity[i].data || row_parity[i].len == 0) {
210 continue;
211 }
212  
213 int missing_count = 0;
214 uint16_t missing_col = 0;
215 for (uint16_t j = 0; j < matrix_d; ++j) {
216 uint16_t idx = (uint16_t)(i * matrix_d + j);
217 if (idx < block_frags && !is_present[idx]) {
218 missing_count++;
219 missing_col = j;
220 }
221 }
222  
223 /* Exactly 1 missing fragment in row i -> Reconstruct it! */
224 if (missing_count == 1) {
225 uint16_t target_idx = (uint16_t)(i * matrix_d + missing_col);
226 if (media_slices[target_idx].data) {
227 size_t parity_len = row_parity[i].len;
228 memcpy(media_slices[target_idx].data, row_parity[i].data, parity_len);
229  
230 for (uint16_t j = 0; j < matrix_d; ++j) {
231 if (j == missing_col) continue;
232 uint16_t other_idx = (uint16_t)(i * matrix_d + j);
233 if (other_idx < block_frags && is_present[other_idx] &&
234 media_slices[other_idx].data && media_slices[other_idx].len > 0) {
235 avx_fec_xor_block(media_slices[target_idx].data,
236 media_slices[other_idx].data,
237 media_slices[other_idx].len);
238 }
239 }
240  
241 media_slices[target_idx].len = parity_len;
242 is_present[target_idx] = true;
243 total_recovered++;
244 progress = true;
245 }
246 }
247 }
248 }
249 }
250  
251 return total_recovered;
252 }