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 * include/avx_fec.h - SMPTE ST 2022-1 2D XOR Forward Error Correction
7 */
8  
9 #ifndef AVX_FEC_H
10 #define AVX_FEC_H
11  
12 #include <stddef.h>
13 #include <stdint.h>
14 #include <stdbool.h>
15 #include "avx_platform.h"
16 #include "openavx.h"
17  
18 #define OAVX_FOURCC_FEC_COLUMN 0x31434546 /* 'FEC1' - SMPTE 2022-1 Column Parity */
19 #define OAVX_FOURCC_FEC_ROW 0x32434546 /* 'FEC2' - SMPTE 2022-1 Row Parity */
20 #define AVX_FOURCC_FEC_COLUMN OAVX_FOURCC_FEC_COLUMN
21 #define AVX_FOURCC_FEC_ROW OAVX_FOURCC_FEC_ROW
22  
23 #define AVX_FEC_MAX_COLS 32
24 #define AVX_FEC_MAX_ROWS 32
25 #define AVX_FEC_MAX_BLOCK_FRAGS (AVX_FEC_MAX_COLS * AVX_FEC_MAX_ROWS)
26  
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30  
31 AVX_PACKED_START
32 typedef struct {
33 uint32_t media_fourcc; /* FourCC of the underlying media frame */
34 uint16_t matrix_d; /* Matrix columns D (e.g. 10) */
35 uint16_t matrix_l; /* Matrix rows L (e.g. 5) */
36 uint16_t block_idx; /* Matrix block index within the frame */
37 uint16_t parity_idx; /* Column index (0..D-1) or Row index (0..L-1) */
38 uint16_t base_frag_idx; /* First media fragment index in this block */
39 uint16_t block_frags; /* Number of media fragments in this block */
40 uint16_t parity_len; /* Length in bytes of XOR parity payload */
41 uint16_t last_frag_len; /* Exact length of the last media fragment in this block */
42 } AVX_PACKED avx_fec_wire_header_t;
43 AVX_PACKED_END
44  
45 typedef struct {
46 bool enabled; /* Whether FEC is enabled */
47 uint16_t matrix_d; /* Matrix columns D (default: 10) */
48 uint16_t matrix_l; /* Matrix rows L (default: 5) */
49 bool enable_row_fec; /* Whether 2D row parity is enabled */
50 } avx_fec_config_t;
51  
52 typedef struct {
53 uint8_t *data;
54 size_t len;
55 } avx_fec_slice_t;
56  
57 /**
58 * High-speed vectorized bitwise XOR kernel (AVX2 / ARM NEON / scalar 64-bit).
59 * Computes: dst[i] ^= src[i] for i = 0 .. len - 1.
60 */
61 OPENAVX_API void avx_fec_xor_block(uint8_t *dst, const uint8_t *src, size_t len);
62  
63 /**
64 * Generates SMPTE ST 2022-1 2D XOR column and row parity slices for a block of media fragments.
65 *
66 * @param media_slices Array of media fragment slices (length block_frags).
67 * @param block_frags Number of fragments in this block (<= matrix_d * matrix_l).
68 * @param matrix_d Columns count D (1 .. AVX_FEC_MAX_COLS).
69 * @param matrix_l Rows count L (1 .. AVX_FEC_MAX_ROWS).
70 * @param enable_row_fec If true, computes row parity slices in addition to column parity.
71 * @param out_col_parity Output array of matrix_d column parity slices (preallocated by caller).
72 * @param out_row_parity Output array of matrix_l row parity slices (or NULL if enable_row_fec is false).
73 * @return 0 on success, -1 on invalid parameter.
74 */
75 OPENAVX_API int avx_fec_generate_block(
76 const avx_fec_slice_t *media_slices,
77 uint16_t block_frags,
78 uint16_t matrix_d,
79 uint16_t matrix_l,
80 bool enable_row_fec,
81 avx_fec_slice_t *out_col_parity,
82 avx_fec_slice_t *out_row_parity
83 );
84  
85 /**
86 * Performs iterative 2D SMPTE ST 2022-1 reconstruction on a block with missing fragments.
87 *
88 * @param media_slices Array of block_frags media slices. Reconstructed slices will be written into
89 * caller-provided buffers in media_slices[i].data.
90 * @param is_present Array of block_frags booleans indicating which fragments have arrived (true) or are missing (false).
91 * Will be updated to true for any reconstructed fragment.
92 * @param block_frags Number of fragments in this block.
93 * @param matrix_d Columns count D.
94 * @param matrix_l Rows count L.
95 * @param col_parity Array of matrix_d column parity slices.
96 * @param col_present Array of matrix_d booleans indicating which column parity packets arrived.
97 * @param row_parity Array of matrix_l row parity slices (or NULL if row parity was disabled).
98 * @param row_present Array of matrix_l booleans indicating which row parity packets arrived (or NULL).
99 * @return Number of fragments successfully recovered (>= 0), or -1 on error.
100 */
101 OPENAVX_API int avx_fec_reconstruct_block(
102 avx_fec_slice_t *media_slices,
103 bool *is_present,
104 uint16_t block_frags,
105 uint16_t matrix_d,
106 uint16_t matrix_l,
107 const avx_fec_slice_t *col_parity,
108 const bool *col_present,
109 const avx_fec_slice_t *row_parity,
110 const bool *row_present
111 );
112  
113 #ifdef __cplusplus
114 }
115 #endif
116  
117 #endif /* AVX_FEC_H */