diff --git a/CMakeLists.txt b/CMakeLists.txt index 335c9de5..536190a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,7 +77,7 @@ FetchContent_Declare(monado ) - +add_subdirectory(external) add_subdirectory(common) if (WIVRN_BUILD_CLIENT) diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 5244883d..56727671 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -58,7 +58,7 @@ if(ANDROID) target_link_options(wivrn PRIVATE -Wl,--undefined=ANativeActivity_onCreate) file(GLOB PLATFORM_SOURCE CONFIGURE_DEPENDS - "src/decoder/android/*.cpp") + "decoder/android/*.cpp") target_sources(wivrn PRIVATE ${PLATFORM_SOURCE}) else() add_executable(wivrn) @@ -69,7 +69,7 @@ else() file(GLOB PLATFORM_SOURCE CONFIGURE_DEPENDS - "src/decoder/ffmpeg/*.cpp") + "decoder/ffmpeg/*.cpp") target_sources(wivrn PRIVATE ${PLATFORM_SOURCE}) endif() @@ -78,20 +78,19 @@ target_compile_definitions(wivrn PRIVATE "XR_USE_TIMESPEC=1") set_target_properties(wivrn PROPERTIES CXX_VISIBILITY_PRESET hidden) file(GLOB LOCAL_SOURCE CONFIGURE_DEPENDS - "src/*.cpp" - "src/decoder/*.cpp" - "src/external/*.c" - "src/scenes/*.cpp" - "src/utils/*.cpp" - "src/vk/*.cpp" - "src/xr/*.cpp" - "src/render/*.cpp") + "*.cpp" + "decoder/*.cpp" + "scenes/*.cpp" + "utils/*.cpp" + "vk/*.cpp" + "xr/*.cpp" + "render/*.cpp") -file(GLOB_RECURSE VULKAN_SHADERS CONFIGURE_DEPENDS "src/*.glsl") +file(GLOB_RECURSE VULKAN_SHADERS CONFIGURE_DEPENDS "*.glsl") target_sources(wivrn PRIVATE ${LOCAL_SOURCE} ${VULKAN_SHADERS}) compile_glsl(wivrn ${VULKAN_SHADERS}) -target_link_libraries(wivrn Vulkan::Vulkan spdlog::spdlog glm::glm tinygltf FreetypeHarfbuzz) +target_link_libraries(wivrn Vulkan::Vulkan spdlog::spdlog glm::glm tinygltf FreetypeHarfbuzz reed-solomon) target_compile_definitions(wivrn PRIVATE -DXR_USE_GRAPHICS_API_VULKAN) target_compile_options(wivrn PRIVATE -fdiagnostics-color -Wall -Wextra -pedantic) # -Werror @@ -100,7 +99,7 @@ target_compile_options(wivrn PRIVATE -Wno-missing-field-initializers -Wno-unused $<$:-Wno-deprecated-volatile> ) -target_include_directories(wivrn PRIVATE src) +target_include_directories(wivrn PRIVATE .) target_link_libraries(wivrn wivrn-common) diff --git a/client/application.cpp b/client/application.cpp index bf605e3d..024fe4ac 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -18,7 +18,7 @@ */ #include "application.h" -#include "external/magic_enum.hpp" +#include "magic_enum.hpp" #include "scene.h" #include "spdlog/common.h" #include "spdlog/spdlog.h" diff --git a/client/decoder/android/android_decoder.cpp b/client/decoder/android/android_decoder.cpp index 3c67fb12..d443d0d8 100644 --- a/client/decoder/android/android_decoder.cpp +++ b/client/decoder/android/android_decoder.cpp @@ -36,7 +36,7 @@ #include #include -#include "external/magic_enum.hpp" +#include "magic_enum.hpp" DEUGLIFY(AMediaFormat) diff --git a/client/decoder/shard_accumulator.cpp b/client/decoder/shard_accumulator.cpp index c957e8ee..55c5c359 100644 --- a/client/decoder/shard_accumulator.cpp +++ b/client/decoder/shard_accumulator.cpp @@ -19,7 +19,7 @@ #include "shard_accumulator.h" #include "application.h" -#include "external/rs.h" +#include "rs.h" #include "scenes/stream.h" #include "spdlog/spdlog.h" #include "utils/typename.h" diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index 5c20b4bd..3a722671 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -23,7 +23,7 @@ #include "application.h" #include "decoder/shard_accumulator.h" -#include "external/magic_enum.hpp" +#include "magic_enum.hpp" #include "glm/fwd.hpp" #include "spdlog/spdlog.h" #include "utils/check.h" diff --git a/client/utils/check.h b/client/utils/check.h index 18c4e5a8..f442f747 100644 --- a/client/utils/check.h +++ b/client/utils/check.h @@ -24,13 +24,6 @@ #include #include -#if __has_include() -#include -#else -#include "external/magic_enum.hpp" -#define string_VkResult(x) std::string(magic_enum::enum_name((VkResult)(x))) -#endif - namespace xr { const std::error_category & error_category(); diff --git a/client/vk/vk.h b/client/vk/vk.h index e2faaebe..7fb4481c 100644 --- a/client/vk/vk.h +++ b/client/vk/vk.h @@ -19,7 +19,7 @@ #pragma once -#include "external/magic_enum.hpp" +#include "magic_enum.hpp" #include "utils/check.h" #include "vk/buffer.h" #include "vk/buffer_view.h" @@ -32,11 +32,8 @@ #include #include -#if __has_include() -#include -#else #define string_VkFormat(x) std::string(magic_enum::enum_name((VkFormat)(x))) -#endif +#define string_VkResult(x) std::string(magic_enum::enum_name((VkResult)(x))) template <> struct magic_enum::customize::enum_range diff --git a/client/wivrn_discover.cpp b/client/wivrn_discover.cpp index e96588d8..458193e4 100644 --- a/client/wivrn_discover.cpp +++ b/client/wivrn_discover.cpp @@ -1,5 +1,5 @@ #include "wivrn_discover.h" -#include "external/mdns.h" +#include "mdns.h" #include #include #include diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt new file mode 100644 index 00000000..a60340e0 --- /dev/null +++ b/external/CMakeLists.txt @@ -0,0 +1,2 @@ +add_library(reed-solomon STATIC rs.c rs.h) +target_include_directories(reed-solomon PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/server/driver/external/nvEncodeAPI.h b/external/nvEncodeAPI.h similarity index 100% rename from server/driver/external/nvEncodeAPI.h rename to external/nvEncodeAPI.h diff --git a/external/rs.c b/external/rs.c index bf8fc75a..f196e2ce 100644 --- a/external/rs.c +++ b/external/rs.c @@ -1,9 +1,9 @@ /* * fec.c -- forward error correction based on Vandermonde matrices * - * (C) 1997-98 Luigi Rizzo (luigi@iet.unipi.it) - * (C) 2001 Alain Knaff (alain@knaff.lu) - * (C) 2017 Iwan Timmer (irtimmer@gmail.com) + * Copyright 1997-98 Luigi Rizzo (luigi@iet.unipi.it) + * Copyright 2001 Alain Knaff (alain@knaff.lu) + * Copyright 2017 Iwan Timmer (irtimmer@gmail.com) * * Portions derived from code by Phil Karn (karn@ka9q.ampr.org), * Robert Morelos-Zaragoza (robert@spectra.eng.hawaii.edu) and Hari @@ -33,13 +33,14 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. */ +// SPDX-License-Identifier: BSD-2-Clause #include #include #include -#include #include "rs.h" +#include #ifdef _MSC_VER #define alloca(x) _alloca(x) @@ -47,11 +48,17 @@ typedef unsigned char gf; -#define GF_BITS 8 +#define GF_BITS 8 #define GF_PP "101110001" #define GF_SIZE ((1 << GF_BITS) - 1) -#define SWAP(a,b,t) {t tmp; tmp=a; a=b; b=tmp;} +#define SWAP(a, b, t) \ + { \ + t tmp; \ + tmp = a; \ + a = b; \ + b = tmp; \ + } /* * USE_GF_MULC, GF_MULC0(c) and GF_ADDMULC(x) can be used when multiplying @@ -61,157 +68,171 @@ typedef unsigned char gf; * declared with USE_GF_MULC . See usage in addmul1(). */ #define USE_GF_MULC gf * __gf_mulc_ -#define GF_MULC0(c) __gf_mulc_ = &gf_mul_table[(c)<<8] +#define GF_MULC0(c) __gf_mulc_ = &gf_mul_table[(c) << 8] #define GF_ADDMULC(dst, x) dst ^= __gf_mulc_[x] #define GF_MULC(dst, x) dst = __gf_mulc_[x] -#define gf_mul(x,y) gf_mul_table[(x<<8)+y] +#define gf_mul(x, y) gf_mul_table[(x << 8) + y] /* * To speed up computations, we have tables for logarithm, exponent * multiplication and inverse of a number. */ -static gf gf_exp[2*GF_SIZE]; +static gf gf_exp[2 * GF_SIZE]; static int gf_log[GF_SIZE + 1]; -static gf inverse[GF_SIZE+1]; +static gf inverse[GF_SIZE + 1]; #ifdef _MSC_VER -static gf __declspec(align (256)) gf_mul_table[(GF_SIZE + 1)*(GF_SIZE + 1)]; +static gf __declspec(align(256)) gf_mul_table[(GF_SIZE + 1) * (GF_SIZE + 1)]; #else -static gf gf_mul_table[(GF_SIZE + 1)*(GF_SIZE + 1)] __attribute__((aligned (256))); +static gf gf_mul_table[(GF_SIZE + 1) * (GF_SIZE + 1)] __attribute__((aligned(256))); #endif /* * modnn(x) computes x % GF_SIZE, where GF_SIZE is 2**GF_BITS - 1, * without a slow divide. */ -static inline gf modnn(int x) { - while (x >= GF_SIZE) { - x -= GF_SIZE; - x = (x >> GF_BITS) + (x & GF_SIZE); - } - return x; +static inline gf modnn(int x) +{ + while (x >= GF_SIZE) + { + x -= GF_SIZE; + x = (x >> GF_BITS) + (x & GF_SIZE); + } + return x; } -static void addmul(gf *dst1, gf *src1, gf c, int sz) { - USE_GF_MULC; - if (c != 0) { - gf *dst = dst1, *src = src1; - gf *lim = &dst[sz]; +static void addmul(gf * dst1, gf * src1, gf c, int sz) +{ + USE_GF_MULC; + if (c != 0) + { + gf *dst = dst1, *src = src1; + gf * lim = &dst[sz]; - GF_MULC0(c); - for (; dst < lim; dst++, src++) - GF_ADDMULC(*dst, *src); - } + GF_MULC0(c); + for (; dst < lim; dst++, src++) + GF_ADDMULC(*dst, *src); + } } -static void mul(gf *dst1, gf *src1, gf c, int sz) { - USE_GF_MULC; - if (c != 0) { - gf *dst = dst1, *src = src1; - gf *lim = &dst[sz]; - GF_MULC0(c); - for (; dst < lim; dst++, src++) - GF_MULC(*dst , *src); - } else - memset(dst1, 0, c); +static void mul(gf * dst1, gf * src1, gf c, int sz) +{ + USE_GF_MULC; + if (c != 0) + { + gf *dst = dst1, *src = src1; + gf * lim = &dst[sz]; + GF_MULC0(c); + for (; dst < lim; dst++, src++) + GF_MULC(*dst, *src); + } + else + memset(dst1, 0, c); } /* y = a.dot(b) */ -static gf* multiply1(gf *a, int ar, int ac, gf *b, int br, int bc) { - gf *new_m, tg; - int r, c, i, ptr = 0; +static gf * multiply1(gf * a, int ar, int ac, gf * b, int br, int bc) +{ + gf *new_m, tg; + int r, c, i, ptr = 0; - assert(ac == br); - new_m = (gf*) calloc(1, ar*bc); - if (NULL != new_m) { + assert(ac == br); + new_m = (gf *)calloc(1, ar * bc); + if (NULL != new_m) + { + /* this multiply is slow */ + for (r = 0; r < ar; r++) + { + for (c = 0; c < bc; c++) + { + tg = 0; + for (i = 0; i < ac; i++) + tg ^= gf_mul(a[r * ac + i], b[i * bc + c]); - /* this multiply is slow */ - for (r = 0; r < ar; r++) { - for (c = 0; c < bc; c++) { - tg = 0; - for (i = 0; i < ac; i++) - tg ^= gf_mul(a[r*ac+i], b[i*bc+c]); + new_m[ptr++] = tg; + } + } + } - new_m[ptr++] = tg; - } - } - } - - return new_m; + return new_m; } -static void init_mul_table(void) { - int i, j; - for (i=0; i< GF_SIZE+1; i++) - for (j=0; j< GF_SIZE+1; j++) - gf_mul_table[(i<<8)+j] = gf_exp[modnn(gf_log[i] + gf_log[j]) ] ; +static void init_mul_table(void) +{ + int i, j; + for (i = 0; i < GF_SIZE + 1; i++) + for (j = 0; j < GF_SIZE + 1; j++) + gf_mul_table[(i << 8) + j] = gf_exp[modnn(gf_log[i] + gf_log[j])]; - for (j=0; j< GF_SIZE+1; j++) - gf_mul_table[j] = gf_mul_table[j<<8] = 0; + for (j = 0; j < GF_SIZE + 1; j++) + gf_mul_table[j] = gf_mul_table[j << 8] = 0; } /* * initialize the data structures used for computations in GF. */ -static void generate_gf(void) { - int i; - gf mask; +static void generate_gf(void) +{ + int i; + gf mask; - mask = 1; - gf_exp[GF_BITS] = 0; - /* - * first, generate the (polynomial representation of) powers of \alpha, - * which are stored in gf_exp[i] = \alpha ** i . - * At the same time build gf_log[gf_exp[i]] = i . - * The first GF_BITS powers are simply bits shifted to the left. - */ - for (i = 0; i < GF_BITS; i++, mask <<= 1) { - gf_exp[i] = mask; - gf_log[gf_exp[i]] = i; - /* - * If GF_PP[i] == 1 then \alpha ** i occurs in poly-repr - * gf_exp[GF_BITS] = \alpha ** GF_BITS - */ - if (GF_PP[i] == '1') - gf_exp[GF_BITS] ^= mask; - } - /* - * now gf_exp[GF_BITS] = \alpha ** GF_BITS is complete, so can als - * compute its inverse. - */ - gf_log[gf_exp[GF_BITS]] = GF_BITS; - /* - * Poly-repr of \alpha ** (i+1) is given by poly-repr of - * \alpha ** i shifted left one-bit and accounting for any - * \alpha ** GF_BITS term that may occur when poly-repr of - * \alpha ** i is shifted. - */ - mask = 1 << (GF_BITS - 1) ; - for (i = GF_BITS + 1; i < GF_SIZE; i++) { - if (gf_exp[i - 1] >= mask) - gf_exp[i] = gf_exp[GF_BITS] ^ ((gf_exp[i - 1] ^ mask) << 1); - else - gf_exp[i] = gf_exp[i - 1] << 1; + mask = 1; + gf_exp[GF_BITS] = 0; + /* + * first, generate the (polynomial representation of) powers of \alpha, + * which are stored in gf_exp[i] = \alpha ** i . + * At the same time build gf_log[gf_exp[i]] = i . + * The first GF_BITS powers are simply bits shifted to the left. + */ + for (i = 0; i < GF_BITS; i++, mask <<= 1) + { + gf_exp[i] = mask; + gf_log[gf_exp[i]] = i; + /* + * If GF_PP[i] == 1 then \alpha ** i occurs in poly-repr + * gf_exp[GF_BITS] = \alpha ** GF_BITS + */ + if (GF_PP[i] == '1') + gf_exp[GF_BITS] ^= mask; + } + /* + * now gf_exp[GF_BITS] = \alpha ** GF_BITS is complete, so can als + * compute its inverse. + */ + gf_log[gf_exp[GF_BITS]] = GF_BITS; + /* + * Poly-repr of \alpha ** (i+1) is given by poly-repr of + * \alpha ** i shifted left one-bit and accounting for any + * \alpha ** GF_BITS term that may occur when poly-repr of + * \alpha ** i is shifted. + */ + mask = 1 << (GF_BITS - 1); + for (i = GF_BITS + 1; i < GF_SIZE; i++) + { + if (gf_exp[i - 1] >= mask) + gf_exp[i] = gf_exp[GF_BITS] ^ ((gf_exp[i - 1] ^ mask) << 1); + else + gf_exp[i] = gf_exp[i - 1] << 1; - gf_log[gf_exp[i]] = i; - } - /* - * log(0) is not defined, so use a special value - */ - gf_log[0] = GF_SIZE; - /* set the extended gf_exp values for fast multiply */ - for (i = 0; i < GF_SIZE; i++) - gf_exp[i + GF_SIZE] = gf_exp[i]; + gf_log[gf_exp[i]] = i; + } + /* + * log(0) is not defined, so use a special value + */ + gf_log[0] = GF_SIZE; + /* set the extended gf_exp values for fast multiply */ + for (i = 0; i < GF_SIZE; i++) + gf_exp[i + GF_SIZE] = gf_exp[i]; - /* - * again special cases. 0 has no inverse. This used to - * be initialized to GF_SIZE, but it should make no difference - * since noone is supposed to read from here. - */ - inverse[0] = 0; - inverse[1] = 1; - for (i=2; i<=GF_SIZE; i++) - inverse[i] = gf_exp[GF_SIZE-gf_log[i]]; + /* + * again special cases. 0 has no inverse. This used to + * be initialized to GF_SIZE, but it should make no difference + * since noone is supposed to read from here. + */ + inverse[0] = 0; + inverse[1] = 1; + for (i = 2; i <= GF_SIZE; i++) + inverse[i] = gf_exp[GF_SIZE - gf_log[i]]; } /* @@ -220,260 +241,300 @@ static void generate_gf(void) { * (Gauss-Jordan, adapted from Numerical Recipes in C) * Return non-zero if singular. */ -static int invert_mat(gf *src, int k) { - gf c, *p; - int irow, icol, row, col, i, ix; +static int invert_mat(gf * src, int k) +{ + gf c, *p; + int irow, icol, row, col, i, ix; - int error = 1; - int *indxc = (int*)alloca(k*sizeof(int)); - int *indxr = (int*)alloca(k*sizeof(int)); - int *ipiv = (int*)alloca(k*sizeof(int)); - gf *id_row = (gf*)alloca(k*sizeof(gf)); + int error = 1; + int * indxc = (int *)alloca(k * sizeof(int)); + int * indxr = (int *)alloca(k * sizeof(int)); + int * ipiv = (int *)alloca(k * sizeof(int)); + gf * id_row = (gf *)alloca(k * sizeof(gf)); - memset(id_row, 0, k*sizeof(gf)); - /* - * ipiv marks elements already used as pivots. - */ - for (i = 0; i < k; i++) - ipiv[i] = 0; + memset(id_row, 0, k * sizeof(gf)); + /* + * ipiv marks elements already used as pivots. + */ + for (i = 0; i < k; i++) + ipiv[i] = 0; - for (col = 0; col < k; col++) { - gf *pivot_row; - /* - * Zeroing column 'col', look for a non-zero element. - * First try on the diagonal, if it fails, look elsewhere. - */ - irow = icol = -1; - if (ipiv[col] != 1 && src[col*k + col] != 0) { - irow = col; - icol = col; - goto found_piv; - } - for (row = 0; row < k; row++) { - if (ipiv[row] != 1) { - for (ix = 0; ix < k; ix++) { - if (ipiv[ix] == 0) { - if (src[row*k + ix] != 0) { - irow = row; - icol = ix; - goto found_piv; - } - } else if (ipiv[ix] > 1) { - fprintf(stderr, "singular matrix\n"); - goto fail; - } - } - } - } - if (icol == -1) { - fprintf(stderr, "XXX pivot not found!\n"); - goto fail ; - } - - found_piv: - ++(ipiv[icol]); - /* - * swap rows irow and icol, so afterwards the diagonal - * element will be correct. Rarely done, not worth - * optimizing. - */ - if (irow != icol) { - for (ix = 0; ix < k; ix++) { - SWAP(src[irow*k + ix], src[icol*k + ix], gf); - } - } - indxr[col] = irow; - indxc[col] = icol; - pivot_row = &src[icol*k]; - c = pivot_row[icol]; - if (c == 0) { - fprintf(stderr, "singular matrix 2\n"); - goto fail; - } else if (c != 1 ) { - /* - * this is done often , but optimizing is not so - * fruitful, at least in the obvious ways (unrolling) - */ - c = inverse[ c ]; - pivot_row[icol] = 1; - for (ix = 0; ix < k; ix++) - pivot_row[ix] = gf_mul(c, pivot_row[ix]); - } - /* - * from all rows, remove multiples of the selected row - * to zero the relevant entry (in fact, the entry is not zero - * because we know it must be zero). - * (Here, if we know that the pivot_row is the identity, - * we can optimize the addmul). - */ - id_row[icol] = 1; - if (memcmp(pivot_row, id_row, k*sizeof(gf)) != 0) { - for (p = src, ix = 0 ; ix < k ; ix++, p += k) { - if (ix != icol) { - c = p[icol]; - p[icol] = 0; - addmul(p, pivot_row, c, k); - } - } - } - id_row[icol] = 0; - } - for (col = k-1 ; col >= 0 ; col-- ) { - if (indxr[col] <0 || indxr[col] >= k) - fprintf(stderr, "AARGH, indxr[col] %d\n", indxr[col]); - else if (indxc[col] <0 || indxc[col] >= k) - fprintf(stderr, "AARGH, indxc[col] %d\n", indxc[col]); - else - if (indxr[col] != indxc[col] ) { - for (row = 0 ; row < k ; row++ ) - SWAP( src[row*k + indxr[col]], src[row*k + indxc[col]], gf); - } - } - error = 0; + for (col = 0; col < k; col++) + { + gf * pivot_row; + /* + * Zeroing column 'col', look for a non-zero element. + * First try on the diagonal, if it fails, look elsewhere. + */ + irow = icol = -1; + if (ipiv[col] != 1 && src[col * k + col] != 0) + { + irow = col; + icol = col; + goto found_piv; + } + for (row = 0; row < k; row++) + { + if (ipiv[row] != 1) + { + for (ix = 0; ix < k; ix++) + { + if (ipiv[ix] == 0) + { + if (src[row * k + ix] != 0) + { + irow = row; + icol = ix; + goto found_piv; + } + } + else if (ipiv[ix] > 1) + { + fprintf(stderr, "singular matrix\n"); + goto fail; + } + } + } + } + if (icol == -1) + { + fprintf(stderr, "XXX pivot not found!\n"); + goto fail; + } - fail: - return error ; + found_piv: + ++(ipiv[icol]); + /* + * swap rows irow and icol, so afterwards the diagonal + * element will be correct. Rarely done, not worth + * optimizing. + */ + if (irow != icol) + { + for (ix = 0; ix < k; ix++) + { + SWAP(src[irow * k + ix], src[icol * k + ix], gf); + } + } + indxr[col] = irow; + indxc[col] = icol; + pivot_row = &src[icol * k]; + c = pivot_row[icol]; + if (c == 0) + { + fprintf(stderr, "singular matrix 2\n"); + goto fail; + } + else if (c != 1) + { + /* + * this is done often , but optimizing is not so + * fruitful, at least in the obvious ways (unrolling) + */ + c = inverse[c]; + pivot_row[icol] = 1; + for (ix = 0; ix < k; ix++) + pivot_row[ix] = gf_mul(c, pivot_row[ix]); + } + /* + * from all rows, remove multiples of the selected row + * to zero the relevant entry (in fact, the entry is not zero + * because we know it must be zero). + * (Here, if we know that the pivot_row is the identity, + * we can optimize the addmul). + */ + id_row[icol] = 1; + if (memcmp(pivot_row, id_row, k * sizeof(gf)) != 0) + { + for (p = src, ix = 0; ix < k; ix++, p += k) + { + if (ix != icol) + { + c = p[icol]; + p[icol] = 0; + addmul(p, pivot_row, c, k); + } + } + } + id_row[icol] = 0; + } + for (col = k - 1; col >= 0; col--) + { + if (indxr[col] < 0 || indxr[col] >= k) + fprintf(stderr, "AARGH, indxr[col] %d\n", indxr[col]); + else if (indxc[col] < 0 || indxc[col] >= k) + fprintf(stderr, "AARGH, indxc[col] %d\n", indxc[col]); + else if (indxr[col] != indxc[col]) + { + for (row = 0; row < k; row++) + SWAP(src[row * k + indxr[col]], src[row * k + indxc[col]], gf); + } + } + error = 0; + +fail: + return error; } /* * Not check for input params * */ -static gf* sub_matrix(gf* matrix, int rmin, int cmin, int rmax, int cmax, int nrows, int ncols) { - (void)nrows; - int i, j, ptr = 0; - gf* new_m = (gf*) malloc((rmax-rmin) * (cmax-cmin)); - if (NULL != new_m) { - for (i = rmin; i < rmax; i++) { - for (j = cmin; j < cmax; j++) { - new_m[ptr++] = matrix[i*ncols + j]; - } - } - } +static gf * sub_matrix(gf * matrix, int rmin, int cmin, int rmax, int cmax, int nrows, int ncols) +{ + (void)nrows; + int i, j, ptr = 0; + gf * new_m = (gf *)malloc((rmax - rmin) * (cmax - cmin)); + if (NULL != new_m) + { + for (i = rmin; i < rmax; i++) + { + for (j = cmin; j < cmax; j++) + { + new_m[ptr++] = matrix[i * ncols + j]; + } + } + } - return new_m; + return new_m; } /* copy from golang rs version */ -static inline int code_some_shards(gf* matrixRows, gf** inputs, gf** outputs, int dataShards, int outputCount, int byteCount) { - gf* in; - int iRow, c; - for (c = 0; c < dataShards; c++) { - in = inputs[c]; - for (iRow = 0; iRow < outputCount; iRow++) { - if (0 == c) - mul(outputs[iRow], in, matrixRows[iRow*dataShards+c], byteCount); - else - addmul(outputs[iRow], in, matrixRows[iRow*dataShards+c], byteCount); - } - } +static inline int code_some_shards(gf * matrixRows, gf ** inputs, gf ** outputs, int dataShards, int outputCount, int byteCount) +{ + gf * in; + int iRow, c; + for (c = 0; c < dataShards; c++) + { + in = inputs[c]; + for (iRow = 0; iRow < outputCount; iRow++) + { + if (0 == c) + mul(outputs[iRow], in, matrixRows[iRow * dataShards + c], byteCount); + else + addmul(outputs[iRow], in, matrixRows[iRow * dataShards + c], byteCount); + } + } - return 0; + return 0; } -void reed_solomon_init(void) { - generate_gf(); - init_mul_table(); +void reed_solomon_init(void) +{ + generate_gf(); + init_mul_table(); } -reed_solomon* reed_solomon_new(int data_shards, int parity_shards) { - gf* vm = NULL; - gf* top = NULL; - int err = 0; - reed_solomon* rs = NULL; +reed_solomon * reed_solomon_new(int data_shards, int parity_shards) +{ + gf * vm = NULL; + gf * top = NULL; + int err = 0; + reed_solomon * rs = NULL; - do { - rs = (reed_solomon *)malloc(sizeof(reed_solomon)); - if (NULL == rs) - return NULL; + do + { + rs = (reed_solomon *)malloc(sizeof(reed_solomon)); + if (NULL == rs) + return NULL; - rs->data_shards = data_shards; - rs->parity_shards = parity_shards; - rs->shards = (data_shards + parity_shards); - rs->m = NULL; - rs->parity = NULL; + rs->data_shards = data_shards; + rs->parity_shards = parity_shards; + rs->shards = (data_shards + parity_shards); + rs->m = NULL; + rs->parity = NULL; - if (rs->shards > DATA_SHARDS_MAX || data_shards <= 0 || parity_shards <= 0) { - err = 1; - break; - } + if (rs->shards > DATA_SHARDS_MAX || data_shards <= 0 || parity_shards <= 0) + { + err = 1; + break; + } - vm = (gf*)malloc(data_shards * rs->shards); + vm = (gf *)malloc(data_shards * rs->shards); - if (NULL == vm) { - err = 2; - break; - } - - int ptr = 0; - for (int row = 0; row < rs->shards; row++) { - for (int col = 0; col < data_shards; col++) - vm[ptr++] = row == col ? 1 : 0; - } + if (NULL == vm) + { + err = 2; + break; + } - top = sub_matrix(vm, 0, 0, data_shards, data_shards, rs->shards, data_shards); - if (NULL == top) { - err = 3; - break; - } + int ptr = 0; + for (int row = 0; row < rs->shards; row++) + { + for (int col = 0; col < data_shards; col++) + vm[ptr++] = row == col ? 1 : 0; + } - err = invert_mat(top, data_shards); - assert(0 == err); + top = sub_matrix(vm, 0, 0, data_shards, data_shards, rs->shards, data_shards); + if (NULL == top) + { + err = 3; + break; + } - rs->m = multiply1(vm, rs->shards, data_shards, top, data_shards, data_shards); - if (NULL == rs->m) { - err = 4; - break; - } + err = invert_mat(top, data_shards); + assert(0 == err); - for (int j = 0; j < parity_shards; j++) { - for (int i = 0; i < data_shards; i++) - rs->m[(data_shards + j)*data_shards + i] = inverse[(parity_shards + i) ^ j]; - } + rs->m = multiply1(vm, rs->shards, data_shards, top, data_shards, data_shards); + if (NULL == rs->m) + { + err = 4; + break; + } - rs->parity = sub_matrix(rs->m, data_shards, 0, rs->shards, data_shards, rs->shards, data_shards); - if (NULL == rs->parity) { - err = 5; - break; - } + for (int j = 0; j < parity_shards; j++) + { + for (int i = 0; i < data_shards; i++) + rs->m[(data_shards + j) * data_shards + i] = inverse[(parity_shards + i) ^ j]; + } - free(vm); - free(top); - vm = NULL; - top = NULL; - return rs; + rs->parity = sub_matrix(rs->m, data_shards, 0, rs->shards, data_shards, rs->shards, data_shards); + if (NULL == rs->parity) + { + err = 5; + break; + } - } while(0); + free(vm); + free(top); + vm = NULL; + top = NULL; + return rs; - fprintf(stderr, "err=%d\n", err); - if (NULL != vm) - free(vm); + } while (0); - if (NULL != top) - free(top); + fprintf(stderr, "err=%d\n", err); + if (NULL != vm) + free(vm); - if (NULL != rs) { - if (NULL != rs->m) - free(rs->m); + if (NULL != top) + free(top); - if (NULL != rs->parity) - free(rs->parity); + if (NULL != rs) + { + if (NULL != rs->m) + free(rs->m); - free(rs); - } + if (NULL != rs->parity) + free(rs->parity); - return NULL; + free(rs); + } + + return NULL; } -void reed_solomon_release(reed_solomon* rs) { - if (NULL != rs) { - if (NULL != rs->m) - free(rs->m); +void reed_solomon_release(reed_solomon * rs) +{ + if (NULL != rs) + { + if (NULL != rs->m) + free(rs->m); - if (NULL != rs->parity) - free(rs->parity); + if (NULL != rs->parity) + free(rs->parity); - free(rs); - } + free(rs); + } } /** @@ -487,69 +548,77 @@ void reed_solomon_release(reed_solomon* rs) { * erased_blocks: erased blocks in original data_blocks * nr_fec_blocks: the number of erased blocks * */ -static int reed_solomon_decode(reed_solomon* rs, gf *dataDecodeMatrix, unsigned char **data_blocks, int block_size, unsigned char **dec_fec_blocks, unsigned int *fec_block_nos, unsigned int *erased_blocks, int nr_fec_blocks) { - unsigned char* subShards[DATA_SHARDS_MAX]; - unsigned char* outputs[DATA_SHARDS_MAX]; - gf* m = rs->m; - int i, j, c, swap, subMatrixRow, dataShards; +static int reed_solomon_decode(reed_solomon * rs, gf * dataDecodeMatrix, unsigned char ** data_blocks, int block_size, unsigned char ** dec_fec_blocks, unsigned int * fec_block_nos, unsigned int * erased_blocks, int nr_fec_blocks) +{ + unsigned char * subShards[DATA_SHARDS_MAX]; + unsigned char * outputs[DATA_SHARDS_MAX]; + gf * m = rs->m; + int i, j, c, swap, subMatrixRow, dataShards; - /* the erased_blocks should always sorted - * if sorted, nr_fec_blocks times to check it - * if not, sort it here - * */ - for (i = 0; i < nr_fec_blocks; i++) { - swap = 0; - for (j = i+1; j < nr_fec_blocks; j++) { - if (erased_blocks[i] > erased_blocks[j]) { - /* the prefix is bigger than the following, swap */ - c = erased_blocks[i]; - erased_blocks[i] = erased_blocks[j]; - erased_blocks[j] = c; + /* the erased_blocks should always sorted + * if sorted, nr_fec_blocks times to check it + * if not, sort it here + * */ + for (i = 0; i < nr_fec_blocks; i++) + { + swap = 0; + for (j = i + 1; j < nr_fec_blocks; j++) + { + if (erased_blocks[i] > erased_blocks[j]) + { + /* the prefix is bigger than the following, swap */ + c = erased_blocks[i]; + erased_blocks[i] = erased_blocks[j]; + erased_blocks[j] = c; - swap = 1; - } - } - if (!swap) - break; - } + swap = 1; + } + } + if (!swap) + break; + } - j = 0; - subMatrixRow = 0; - dataShards = rs->data_shards; - for (i = 0; i < dataShards; i++) { - if (j < nr_fec_blocks && i == (int)erased_blocks[j]) - j++; - else { - /* this row is ok */ - for (c = 0; c < dataShards; c++) - dataDecodeMatrix[subMatrixRow*dataShards + c] = m[i*dataShards + c]; + j = 0; + subMatrixRow = 0; + dataShards = rs->data_shards; + for (i = 0; i < dataShards; i++) + { + if (j < nr_fec_blocks && i == (int)erased_blocks[j]) + j++; + else + { + /* this row is ok */ + for (c = 0; c < dataShards; c++) + dataDecodeMatrix[subMatrixRow * dataShards + c] = m[i * dataShards + c]; - subShards[subMatrixRow] = data_blocks[i]; - subMatrixRow++; - } - } + subShards[subMatrixRow] = data_blocks[i]; + subMatrixRow++; + } + } - for (i = 0; i < nr_fec_blocks && subMatrixRow < dataShards; i++) { - subShards[subMatrixRow] = dec_fec_blocks[i]; - j = dataShards + fec_block_nos[i]; - for (c = 0; c < dataShards; c++) - dataDecodeMatrix[subMatrixRow*dataShards + c] = m[j*dataShards + c]; + for (i = 0; i < nr_fec_blocks && subMatrixRow < dataShards; i++) + { + subShards[subMatrixRow] = dec_fec_blocks[i]; + j = dataShards + fec_block_nos[i]; + for (c = 0; c < dataShards; c++) + dataDecodeMatrix[subMatrixRow * dataShards + c] = m[j * dataShards + c]; - subMatrixRow++; - } + subMatrixRow++; + } - if (subMatrixRow < dataShards) - return -1; + if (subMatrixRow < dataShards) + return -1; - invert_mat(dataDecodeMatrix, dataShards); + invert_mat(dataDecodeMatrix, dataShards); - for (i = 0; i < nr_fec_blocks; i++) { - j = erased_blocks[i]; - outputs[i] = data_blocks[j]; - memmove(dataDecodeMatrix+i*dataShards, dataDecodeMatrix+j*dataShards, dataShards); - } + for (i = 0; i < nr_fec_blocks; i++) + { + j = erased_blocks[i]; + outputs[i] = data_blocks[j]; + memmove(dataDecodeMatrix + i * dataShards, dataDecodeMatrix + j * dataShards, dataShards); + } - return code_some_shards(dataDecodeMatrix, subShards, outputs, dataShards, nr_fec_blocks, block_size); + return code_some_shards(dataDecodeMatrix, subShards, outputs, dataShards, nr_fec_blocks, block_size); } /** @@ -559,20 +628,22 @@ static int reed_solomon_decode(reed_solomon* rs, gf *dataDecodeMatrix, unsigned * nr_shards: assert(0 == nr_shards % rs->shards) * shards[nr_shards][block_size] * */ -int reed_solomon_encode(reed_solomon* rs, unsigned char** shards, int nr_shards, int block_size) { - unsigned char** data_blocks; - unsigned char** fec_blocks; - int i, ds = rs->data_shards, ps = rs->parity_shards, ss = rs->shards; - i = nr_shards / ss; - data_blocks = shards; - fec_blocks = &shards[(i*ds)]; +int reed_solomon_encode(reed_solomon * rs, unsigned char ** shards, int nr_shards, int block_size) +{ + unsigned char ** data_blocks; + unsigned char ** fec_blocks; + int i, ds = rs->data_shards, ps = rs->parity_shards, ss = rs->shards; + i = nr_shards / ss; + data_blocks = shards; + fec_blocks = &shards[(i * ds)]; - for (i = 0; i < nr_shards; i += ss) { - code_some_shards(rs->parity, data_blocks, fec_blocks, rs->data_shards, rs->parity_shards, block_size); - data_blocks += ds; - fec_blocks += ps; - } - return 0; + for (i = 0; i < nr_shards; i += ss) + { + code_some_shards(rs->parity, data_blocks, fec_blocks, rs->data_shards, rs->parity_shards, block_size); + data_blocks += ds; + fec_blocks += ps; + } + return 0; } /** @@ -583,50 +654,58 @@ int reed_solomon_encode(reed_solomon* rs, unsigned char** shards, int nr_shards, * shards[nr_shards][block_size] * marks[nr_shards] marks as errors * */ -int reed_solomon_reconstruct(reed_solomon* rs, unsigned char** shards, unsigned char* marks, int nr_shards, int block_size) { - unsigned char *dec_fec_blocks[DATA_SHARDS_MAX]; - unsigned int fec_block_nos[DATA_SHARDS_MAX]; - unsigned int erased_blocks[DATA_SHARDS_MAX]; - unsigned char* fec_marks; - unsigned char **data_blocks, **fec_blocks; - gf * dataDecodeMatrix = malloc(DATA_SHARDS_MAX*DATA_SHARDS_MAX * sizeof(gf)); - int i, j, dn, pn, n; - int ds = rs->data_shards; - int ps = rs->parity_shards; - int err = 0; +int reed_solomon_reconstruct(reed_solomon * rs, unsigned char ** shards, unsigned char * marks, int nr_shards, int block_size) +{ + unsigned char * dec_fec_blocks[DATA_SHARDS_MAX]; + unsigned int fec_block_nos[DATA_SHARDS_MAX]; + unsigned int erased_blocks[DATA_SHARDS_MAX]; + unsigned char * fec_marks; + unsigned char **data_blocks, **fec_blocks; + gf * dataDecodeMatrix = malloc(DATA_SHARDS_MAX * DATA_SHARDS_MAX * sizeof(gf)); + int i, j, dn, pn, n; + int ds = rs->data_shards; + int ps = rs->parity_shards; + int err = 0; - data_blocks = shards; - n = nr_shards / rs->shards; - fec_marks = marks + n*ds; //after all data, is't fec marks - fec_blocks = shards + n*ds; + data_blocks = shards; + n = nr_shards / rs->shards; + fec_marks = marks + n * ds; // after all data, is't fec marks + fec_blocks = shards + n * ds; - for (j = 0; j < n; j++) { - dn = 0; - for (i = 0; i < ds; i++) { - if (marks[i]) - erased_blocks[dn++] = i; - } - if (dn > 0) { - pn = 0; - for (i = 0; i < ps && pn < dn; i++) { - if (!fec_marks[i]) { - //got valid fec row - fec_block_nos[pn] = i; - dec_fec_blocks[pn] = fec_blocks[i]; - pn++; - } - } + for (j = 0; j < n; j++) + { + dn = 0; + for (i = 0; i < ds; i++) + { + if (marks[i]) + erased_blocks[dn++] = i; + } + if (dn > 0) + { + pn = 0; + for (i = 0; i < ps && pn < dn; i++) + { + if (!fec_marks[i]) + { + // got valid fec row + fec_block_nos[pn] = i; + dec_fec_blocks[pn] = fec_blocks[i]; + pn++; + } + } - if (dn == pn) { - reed_solomon_decode(rs, dataDecodeMatrix, data_blocks, block_size, dec_fec_blocks, fec_block_nos, erased_blocks, dn); - } else - err = -1; - } - data_blocks += ds; - marks += ds; - fec_blocks += ps; - fec_marks += ps; - } + if (dn == pn) + { + reed_solomon_decode(rs, dataDecodeMatrix, data_blocks, block_size, dec_fec_blocks, fec_block_nos, erased_blocks, dn); + } + else + err = -1; + } + data_blocks += ds; + marks += ds; + fec_blocks += ps; + fec_marks += ps; + } - return err; + return err; } diff --git a/external/rs.h b/external/rs.h index 52673629..72b981aa 100644 --- a/external/rs.h +++ b/external/rs.h @@ -1,19 +1,58 @@ +/* + * fec.c -- forward error correction based on Vandermonde matrices + * + * Copyright 1997-98 Luigi Rizzo (luigi@iet.unipi.it) + * Copyright 2001 Alain Knaff (alain@knaff.lu) + * Copyright 2017 Iwan Timmer (irtimmer@gmail.com) + * + * Portions derived from code by Phil Karn (karn@ka9q.ampr.org), + * Robert Morelos-Zaragoza (robert@spectra.eng.hawaii.edu) and Hari + * Thirumoorthy (harit@spectra.eng.hawaii.edu), Aug 1995 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + */ +// SPDX-License-Identifier: BSD-2-Clause + #ifndef __RS_H_ #define __RS_H_ #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /* use small value to save memory */ #define DATA_SHARDS_MAX 2048 - typedef struct _reed_solomon { + typedef struct _reed_solomon + { int data_shards; int parity_shards; int shards; - unsigned char* m; - unsigned char* parity; + unsigned char * m; + unsigned char * parity; } reed_solomon; /** @@ -21,8 +60,8 @@ extern "C" { * */ void reed_solomon_init(void); - reed_solomon* reed_solomon_new(int data_shards, int parity_shards); - void reed_solomon_release(reed_solomon* rs); + reed_solomon * reed_solomon_new(int data_shards, int parity_shards); + void reed_solomon_release(reed_solomon * rs); /** * encode a big size of buffer @@ -31,7 +70,7 @@ extern "C" { * nr_shards: assert(0 == nr_shards % rs->data_shards) * shards[nr_shards][block_size] * */ - int reed_solomon_encode(reed_solomon* rs, unsigned char** shards, int nr_shards, int block_size); + int reed_solomon_encode(reed_solomon * rs, unsigned char ** shards, int nr_shards, int block_size); /** * reconstruct a big size of buffer @@ -41,10 +80,9 @@ extern "C" { * shards[nr_shards][block_size] * marks[nr_shards] marks as errors * */ - int reed_solomon_reconstruct(reed_solomon* rs, unsigned char** shards, unsigned char* marks, int nr_shards, int block_size); + int reed_solomon_reconstruct(reed_solomon * rs, unsigned char ** shards, unsigned char * marks, int nr_shards, int block_size); #ifdef __cplusplus }; #endif #endif - diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index d6b21c95..f63e7cf1 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -65,7 +65,6 @@ add_executable(wivrn-server driver/wivrn_session.cpp driver/wivrn_connection.cpp driver/xrt_cast.cpp - driver/external/rs.c ${WIVRN_SHADER_HEADERS} driver/yuv_converter.cpp ) @@ -116,6 +115,7 @@ target_link_libraries( Eigen3::Eigen wivrn-common nlohmann_json::nlohmann_json + reed-solomon ) install(TARGETS wivrn-server) diff --git a/server/driver/external/rs.c b/server/driver/external/rs.c deleted file mode 100644 index 3ea6f6fe..00000000 --- a/server/driver/external/rs.c +++ /dev/null @@ -1,729 +0,0 @@ -/* - * fec.c -- forward error correction based on Vandermonde matrices - * - * Copyright 1997-98 Luigi Rizzo (luigi@iet.unipi.it) - * Copyright 2001 Alain Knaff (alain@knaff.lu) - * Copyright 2017 Iwan Timmer (irtimmer@gmail.com) - * - * Portions derived from code by Phil Karn (karn@ka9q.ampr.org), - * Robert Morelos-Zaragoza (robert@spectra.eng.hawaii.edu) and Hari - * Thirumoorthy (harit@spectra.eng.hawaii.edu), Aug 1995 - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS - * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, - * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, - * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR - * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - */ -// SPDX-License-Identifier: BSD-2-Clause - -#include -#include -#include - -#include "rs.h" -#include - -#ifdef _MSC_VER -#define alloca(x) _alloca(x) -#endif - -typedef unsigned char gf; - -#define GF_BITS 8 -#define GF_PP "101110001" -#define GF_SIZE ((1 << GF_BITS) - 1) - -#define SWAP(a, b, t) \ - { \ - t tmp; \ - tmp = a; \ - a = b; \ - b = tmp; \ - } - -/* - * USE_GF_MULC, GF_MULC0(c) and GF_ADDMULC(x) can be used when multiplying - * many numbers by the same constant. In this case the first - * call sets the constant, and others perform the multiplications. - * A value related to the multiplication is held in a local variable - * declared with USE_GF_MULC . See usage in addmul1(). - */ -#define USE_GF_MULC gf * __gf_mulc_ -#define GF_MULC0(c) __gf_mulc_ = &gf_mul_table[(c) << 8] -#define GF_ADDMULC(dst, x) dst ^= __gf_mulc_[x] -#define GF_MULC(dst, x) dst = __gf_mulc_[x] - -#define gf_mul(x, y) gf_mul_table[(x << 8) + y] - -/* - * To speed up computations, we have tables for logarithm, exponent - * multiplication and inverse of a number. - */ -static gf gf_exp[2 * GF_SIZE]; -static int gf_log[GF_SIZE + 1]; -static gf inverse[GF_SIZE + 1]; -#ifdef _MSC_VER -static gf __declspec(align(256)) gf_mul_table[(GF_SIZE + 1) * (GF_SIZE + 1)]; -#else -static gf gf_mul_table[(GF_SIZE + 1) * (GF_SIZE + 1)] __attribute__((aligned(256))); -#endif - -/* - * modnn(x) computes x % GF_SIZE, where GF_SIZE is 2**GF_BITS - 1, - * without a slow divide. - */ -static inline gf -modnn(int x) -{ - while (x >= GF_SIZE) - { - x -= GF_SIZE; - x = (x >> GF_BITS) + (x & GF_SIZE); - } - return x; -} - -static void -addmul(gf * dst1, gf * src1, gf c, int sz) -{ - USE_GF_MULC; - if (c != 0) - { - gf *dst = dst1, *src = src1; - gf * lim = &dst[sz]; - - GF_MULC0(c); - for (; dst < lim; dst++, src++) - GF_ADDMULC(*dst, *src); - } -} - -static void -mul(gf * dst1, gf * src1, gf c, int sz) -{ - USE_GF_MULC; - if (c != 0) - { - gf *dst = dst1, *src = src1; - gf * lim = &dst[sz]; - GF_MULC0(c); - for (; dst < lim; dst++, src++) - GF_MULC(*dst, *src); - } - else - memset(dst1, 0, c); -} - -/* y = a.dot(b) */ -static gf * -multiply1(gf * a, int ar, int ac, gf * b, int br, int bc) -{ - gf *new_m, tg; - int r, c, i, ptr = 0; - - assert(ac == br); - new_m = (gf *)calloc(1, ar * bc); - if (NULL != new_m) - { - /* this multiply is slow */ - for (r = 0; r < ar; r++) - { - for (c = 0; c < bc; c++) - { - tg = 0; - for (i = 0; i < ac; i++) - tg ^= gf_mul(a[r * ac + i], b[i * bc + c]); - - new_m[ptr++] = tg; - } - } - } - - return new_m; -} - -static void -init_mul_table(void) -{ - int i, j; - for (i = 0; i < GF_SIZE + 1; i++) - for (j = 0; j < GF_SIZE + 1; j++) - gf_mul_table[(i << 8) + j] = gf_exp[modnn(gf_log[i] + gf_log[j])]; - - for (j = 0; j < GF_SIZE + 1; j++) - gf_mul_table[j] = gf_mul_table[j << 8] = 0; -} - -/* - * initialize the data structures used for computations in GF. - */ -static void -generate_gf(void) -{ - int i; - gf mask; - - mask = 1; - gf_exp[GF_BITS] = 0; - /* - * first, generate the (polynomial representation of) powers of \alpha, - * which are stored in gf_exp[i] = \alpha ** i . - * At the same time build gf_log[gf_exp[i]] = i . - * The first GF_BITS powers are simply bits shifted to the left. - */ - for (i = 0; i < GF_BITS; i++, mask <<= 1) - { - gf_exp[i] = mask; - gf_log[gf_exp[i]] = i; - /* - * If GF_PP[i] == 1 then \alpha ** i occurs in poly-repr - * gf_exp[GF_BITS] = \alpha ** GF_BITS - */ - if (GF_PP[i] == '1') - gf_exp[GF_BITS] ^= mask; - } - /* - * now gf_exp[GF_BITS] = \alpha ** GF_BITS is complete, so can also - * compute its inverse. - */ - gf_log[gf_exp[GF_BITS]] = GF_BITS; - /* - * Poly-repr of \alpha ** (i+1) is given by poly-repr of - * \alpha ** i shifted left one-bit and accounting for any - * \alpha ** GF_BITS term that may occur when poly-repr of - * \alpha ** i is shifted. - */ - mask = 1 << (GF_BITS - 1); - for (i = GF_BITS + 1; i < GF_SIZE; i++) - { - if (gf_exp[i - 1] >= mask) - gf_exp[i] = gf_exp[GF_BITS] ^ ((gf_exp[i - 1] ^ mask) << 1); - else - gf_exp[i] = gf_exp[i - 1] << 1; - - gf_log[gf_exp[i]] = i; - } - /* - * log(0) is not defined, so use a special value - */ - gf_log[0] = GF_SIZE; - /* set the extended gf_exp values for fast multiply */ - for (i = 0; i < GF_SIZE; i++) - gf_exp[i + GF_SIZE] = gf_exp[i]; - - /* - * again special cases. 0 has no inverse. This used to - * be initialized to GF_SIZE, but it should make no difference - * since no one is supposed to read from here. - */ - inverse[0] = 0; - inverse[1] = 1; - for (i = 2; i <= GF_SIZE; i++) - inverse[i] = gf_exp[GF_SIZE - gf_log[i]]; -} - -/* - * invert_mat() takes a matrix and produces its inverse - * k is the size of the matrix. - * (Gauss-Jordan, adapted from Numerical Recipes in C) - * Return non-zero if singular. - */ -static int -invert_mat(gf * src, int k) -{ - gf c, *p; - int irow, icol, row, col, i, ix; - - int error = 1; - int * indxc = (int *)alloca(k * sizeof(int)); - int * indxr = (int *)alloca(k * sizeof(int)); - int * ipiv = (int *)alloca(k * sizeof(int)); - gf * id_row = (gf *)alloca(k * sizeof(gf)); - - memset(id_row, 0, k * sizeof(gf)); - /* - * ipiv marks elements already used as pivots. - */ - for (i = 0; i < k; i++) - ipiv[i] = 0; - - for (col = 0; col < k; col++) - { - gf * pivot_row; - /* - * Zeroing column 'col', look for a non-zero element. - * First try on the diagonal, if it fails, look elsewhere. - */ - irow = icol = -1; - if (ipiv[col] != 1 && src[col * k + col] != 0) - { - irow = col; - icol = col; - goto found_piv; - } - for (row = 0; row < k; row++) - { - if (ipiv[row] != 1) - { - for (ix = 0; ix < k; ix++) - { - if (ipiv[ix] == 0) - { - if (src[row * k + ix] != 0) - { - irow = row; - icol = ix; - goto found_piv; - } - } - else if (ipiv[ix] > 1) - { - fprintf(stderr, "singular matrix\n"); - goto fail; - } - } - } - } - if (icol == -1) - { - fprintf(stderr, "XXX pivot not found!\n"); - goto fail; - } - - found_piv: - ++(ipiv[icol]); - /* - * swap rows irow and icol, so afterwards the diagonal - * element will be correct. Rarely done, not worth - * optimizing. - */ - if (irow != icol) - { - for (ix = 0; ix < k; ix++) - { - SWAP(src[irow * k + ix], src[icol * k + ix], gf); - } - } - indxr[col] = irow; - indxc[col] = icol; - pivot_row = &src[icol * k]; - c = pivot_row[icol]; - if (c == 0) - { - fprintf(stderr, "singular matrix 2\n"); - goto fail; - } - else if (c != 1) - { - /* - * this is done often , but optimizing is not so - * fruitful, at least in the obvious ways (unrolling) - */ - c = inverse[c]; - pivot_row[icol] = 1; - for (ix = 0; ix < k; ix++) - pivot_row[ix] = gf_mul(c, pivot_row[ix]); - } - /* - * from all rows, remove multiples of the selected row - * to zero the relevant entry (in fact, the entry is not zero - * because we know it must be zero). - * (Here, if we know that the pivot_row is the identity, - * we can optimize the addmul). - */ - id_row[icol] = 1; - if (memcmp(pivot_row, id_row, k * sizeof(gf)) != 0) - { - for (p = src, ix = 0; ix < k; ix++, p += k) - { - if (ix != icol) - { - c = p[icol]; - p[icol] = 0; - addmul(p, pivot_row, c, k); - } - } - } - id_row[icol] = 0; - } - for (col = k - 1; col >= 0; col--) - { - if (indxr[col] < 0 || indxr[col] >= k) - fprintf(stderr, "AARGH, indxr[col] %d\n", indxr[col]); - else if (indxc[col] < 0 || indxc[col] >= k) - fprintf(stderr, "AARGH, indxc[col] %d\n", indxc[col]); - else if (indxr[col] != indxc[col]) - { - for (row = 0; row < k; row++) - SWAP(src[row * k + indxr[col]], src[row * k + indxc[col]], gf); - } - } - error = 0; - -fail: - return error; -} - -/* - * Not check for input params - * */ -static gf * -sub_matrix(gf * matrix, int rmin, int cmin, int rmax, int cmax, int nrows, int ncols) -{ - (void)nrows; - int i, j, ptr = 0; - gf * new_m = (gf *)malloc((rmax - rmin) * (cmax - cmin)); - if (NULL != new_m) - { - for (i = rmin; i < rmax; i++) - { - for (j = cmin; j < cmax; j++) - { - new_m[ptr++] = matrix[i * ncols + j]; - } - } - } - - return new_m; -} - -/* copy from golang rs version */ -static inline int -code_some_shards(gf * matrixRows, gf ** inputs, gf ** outputs, int dataShards, int outputCount, int byteCount) -{ - gf * in; - int iRow, c; - for (c = 0; c < dataShards; c++) - { - in = inputs[c]; - for (iRow = 0; iRow < outputCount; iRow++) - { - if (0 == c) - mul(outputs[iRow], in, matrixRows[iRow * dataShards + c], byteCount); - else - addmul(outputs[iRow], in, matrixRows[iRow * dataShards + c], byteCount); - } - } - - return 0; -} - -void reed_solomon_init(void) -{ - generate_gf(); - init_mul_table(); -} - -reed_solomon * -reed_solomon_new(int data_shards, int parity_shards) -{ - gf * vm = NULL; - gf * top = NULL; - int err = 0; - reed_solomon * rs = NULL; - - do - { - rs = (reed_solomon *)malloc(sizeof(reed_solomon)); - if (NULL == rs) - return NULL; - - rs->data_shards = data_shards; - rs->parity_shards = parity_shards; - rs->shards = (data_shards + parity_shards); - rs->m = NULL; - rs->parity = NULL; - - if (rs->shards > DATA_SHARDS_MAX || data_shards <= 0 || parity_shards <= 0) - { - err = 1; - break; - } - - vm = (gf *)malloc(data_shards * rs->shards); - - if (NULL == vm) - { - err = 2; - break; - } - - int ptr = 0; - for (int row = 0; row < rs->shards; row++) - { - for (int col = 0; col < data_shards; col++) - vm[ptr++] = row == col ? 1 : 0; - } - - top = sub_matrix(vm, 0, 0, data_shards, data_shards, rs->shards, data_shards); - if (NULL == top) - { - err = 3; - break; - } - - err = invert_mat(top, data_shards); - assert(0 == err); - - rs->m = multiply1(vm, rs->shards, data_shards, top, data_shards, data_shards); - if (NULL == rs->m) - { - err = 4; - break; - } - - for (int j = 0; j < parity_shards; j++) - { - for (int i = 0; i < data_shards; i++) - rs->m[(data_shards + j) * data_shards + i] = inverse[(parity_shards + i) ^ j]; - } - - rs->parity = sub_matrix(rs->m, data_shards, 0, rs->shards, data_shards, rs->shards, data_shards); - if (NULL == rs->parity) - { - err = 5; - break; - } - - free(vm); - free(top); - vm = NULL; - top = NULL; - return rs; - - } while (0); - - fprintf(stderr, "err=%d\n", err); - if (NULL != vm) - free(vm); - - if (NULL != top) - free(top); - - if (NULL != rs) - { - if (NULL != rs->m) - free(rs->m); - - if (NULL != rs->parity) - free(rs->parity); - - free(rs); - } - - return NULL; -} - -void reed_solomon_release(reed_solomon * rs) -{ - if (NULL != rs) - { - if (NULL != rs->m) - free(rs->m); - - if (NULL != rs->parity) - free(rs->parity); - - free(rs); - } -} - -/** - * decode one shard - * input: - * rs - * gf[DATA_SHARDS_MAX*DATA_SHARDS_MAX] array for temporary data - * original data_blocks[rs->data_shards][block_size] - * dec_fec_blocks[nr_fec_blocks][block_size] - * fec_block_nos: fec pos number in original fec_blocks - * erased_blocks: erased blocks in original data_blocks - * nr_fec_blocks: the number of erased blocks - * */ -static int -reed_solomon_decode(reed_solomon * rs, - gf * dataDecodeMatrix, - unsigned char ** data_blocks, - int block_size, - unsigned char ** dec_fec_blocks, - unsigned int * fec_block_nos, - unsigned int * erased_blocks, - int nr_fec_blocks) -{ - unsigned char * subShards[DATA_SHARDS_MAX]; - unsigned char * outputs[DATA_SHARDS_MAX]; - gf * m = rs->m; - int i, j, c, swap, subMatrixRow, dataShards; - - /* the erased_blocks should always sorted - * if sorted, nr_fec_blocks times to check it - * if not, sort it here - * */ - for (i = 0; i < nr_fec_blocks; i++) - { - swap = 0; - for (j = i + 1; j < nr_fec_blocks; j++) - { - if (erased_blocks[i] > erased_blocks[j]) - { - /* the prefix is bigger than the following, swap */ - c = erased_blocks[i]; - erased_blocks[i] = erased_blocks[j]; - erased_blocks[j] = c; - - swap = 1; - } - } - if (!swap) - break; - } - - j = 0; - subMatrixRow = 0; - dataShards = rs->data_shards; - for (i = 0; i < dataShards; i++) - { - if (j < nr_fec_blocks && i == (int)erased_blocks[j]) - j++; - else - { - /* this row is ok */ - for (c = 0; c < dataShards; c++) - dataDecodeMatrix[subMatrixRow * dataShards + c] = m[i * dataShards + c]; - - subShards[subMatrixRow] = data_blocks[i]; - subMatrixRow++; - } - } - - for (i = 0; i < nr_fec_blocks && subMatrixRow < dataShards; i++) - { - subShards[subMatrixRow] = dec_fec_blocks[i]; - j = dataShards + fec_block_nos[i]; - for (c = 0; c < dataShards; c++) - dataDecodeMatrix[subMatrixRow * dataShards + c] = m[j * dataShards + c]; - - subMatrixRow++; - } - - if (subMatrixRow < dataShards) - return -1; - - invert_mat(dataDecodeMatrix, dataShards); - - for (i = 0; i < nr_fec_blocks; i++) - { - j = erased_blocks[i]; - outputs[i] = data_blocks[j]; - memmove(dataDecodeMatrix + i * dataShards, dataDecodeMatrix + j * dataShards, dataShards); - } - - return code_some_shards(dataDecodeMatrix, subShards, outputs, dataShards, nr_fec_blocks, block_size); -} - -/** - * encode a big size of buffer - * input: - * rs - * nr_shards: assert(0 == nr_shards % rs->shards) - * shards[nr_shards][block_size] - * */ -int reed_solomon_encode(reed_solomon * rs, unsigned char ** shards, int nr_shards, int block_size) -{ - unsigned char ** data_blocks; - unsigned char ** fec_blocks; - int i, ds = rs->data_shards, ps = rs->parity_shards, ss = rs->shards; - i = nr_shards / ss; - data_blocks = shards; - fec_blocks = &shards[(i * ds)]; - - for (i = 0; i < nr_shards; i += ss) - { - code_some_shards(rs->parity, data_blocks, fec_blocks, rs->data_shards, rs->parity_shards, block_size); - data_blocks += ds; - fec_blocks += ps; - } - return 0; -} - -/** - * reconstruct a big size of buffer - * input: - * rs - * nr_shards: assert(0 == nr_shards % rs->data_shards) - * shards[nr_shards][block_size] - * marks[nr_shards] marks as errors - * */ -int reed_solomon_reconstruct(reed_solomon * rs, unsigned char ** shards, unsigned char * marks, int nr_shards, int block_size) -{ - unsigned char * dec_fec_blocks[DATA_SHARDS_MAX]; - unsigned int fec_block_nos[DATA_SHARDS_MAX]; - unsigned int erased_blocks[DATA_SHARDS_MAX]; - unsigned char * fec_marks; - unsigned char **data_blocks, **fec_blocks; - gf * dataDecodeMatrix = malloc(DATA_SHARDS_MAX * DATA_SHARDS_MAX * sizeof(gf)); - int i, j, dn, pn, n; - int ds = rs->data_shards; - int ps = rs->parity_shards; - int err = 0; - - data_blocks = shards; - n = nr_shards / rs->shards; - fec_marks = marks + n * ds; // after all data, is't fec marks - fec_blocks = shards + n * ds; - - for (j = 0; j < n; j++) - { - dn = 0; - for (i = 0; i < ds; i++) - { - if (marks[i]) - erased_blocks[dn++] = i; - } - if (dn > 0) - { - pn = 0; - for (i = 0; i < ps && pn < dn; i++) - { - if (!fec_marks[i]) - { - // got valid fec row - fec_block_nos[pn] = i; - dec_fec_blocks[pn] = fec_blocks[i]; - pn++; - } - } - - if (dn == pn) - { - reed_solomon_decode(rs, dataDecodeMatrix, data_blocks, block_size, dec_fec_blocks, fec_block_nos, erased_blocks, dn); - } - else - err = -1; - } - data_blocks += ds; - marks += ds; - fec_blocks += ps; - fec_marks += ps; - } - - return err; -} diff --git a/server/driver/external/rs.h b/server/driver/external/rs.h deleted file mode 100644 index d5514011..00000000 --- a/server/driver/external/rs.h +++ /dev/null @@ -1,93 +0,0 @@ -/* - * fec.c -- forward error correction based on Vandermonde matrices - * - * Copyright 1997-98 Luigi Rizzo (luigi@iet.unipi.it) - * Copyright 2001 Alain Knaff (alain@knaff.lu) - * Copyright 2017 Iwan Timmer (irtimmer@gmail.com) - * - * Portions derived from code by Phil Karn (karn@ka9q.ampr.org), - * Robert Morelos-Zaragoza (robert@spectra.eng.hawaii.edu) and Hari - * Thirumoorthy (harit@spectra.eng.hawaii.edu), Aug 1995 - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS - * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, - * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, - * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR - * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - */ -// SPDX-License-Identifier: BSD-2-Clause - -#ifndef __RS_H_ -#define __RS_H_ - -#ifdef __cplusplus -extern "C" -{ -#endif - -/* use small value to save memory */ -#define DATA_SHARDS_MAX 2048 - - typedef struct _reed_solomon - { - int data_shards; - int parity_shards; - int shards; - unsigned char * m; - unsigned char * parity; - } reed_solomon; - - /** - * MUST initial one time - * */ - void - reed_solomon_init(void); - - reed_solomon * - reed_solomon_new(int data_shards, int parity_shards); - void - reed_solomon_release(reed_solomon * rs); - - /** - * encode a big size of buffer - * input: - * rs - * nr_shards: assert(0 == nr_shards % rs->data_shards) - * shards[nr_shards][block_size] - * */ - int - reed_solomon_encode(reed_solomon * rs, unsigned char ** shards, int nr_shards, int block_size); - - /** - * reconstruct a big size of buffer - * input: - * rs - * nr_shards: assert(0 == nr_shards % rs->data_shards) - * shards[nr_shards][block_size] - * marks[nr_shards] marks as errors - * */ - int - reed_solomon_reconstruct(reed_solomon * rs, unsigned char ** shards, unsigned char * marks, int nr_shards, int block_size); - -#ifdef __cplusplus -}; -#endif -#endif diff --git a/server/driver/video_encoder.cpp b/server/driver/video_encoder.cpp index 6fc8e0e3..c8fb5781 100644 --- a/server/driver/video_encoder.cpp +++ b/server/driver/video_encoder.cpp @@ -18,7 +18,7 @@ */ #include "video_encoder.h" -#include "external/rs.h" +#include "rs.h" #include "util/u_logging.h" #include diff --git a/server/driver/video_encoder_nvenc.cpp b/server/driver/video_encoder_nvenc.cpp index f1203d35..40e8b7af 100644 --- a/server/driver/video_encoder_nvenc.cpp +++ b/server/driver/video_encoder_nvenc.cpp @@ -18,7 +18,6 @@ */ #include "video_encoder_nvenc.h" -#include "external/nvEncodeAPI.h" #include diff --git a/server/driver/video_encoder_nvenc.h b/server/driver/video_encoder_nvenc.h index f26a098e..b62ea61b 100644 --- a/server/driver/video_encoder_nvenc.h +++ b/server/driver/video_encoder_nvenc.h @@ -19,7 +19,7 @@ #pragma once -#include "external/nvEncodeAPI.h" +#include "nvEncodeAPI.h" #include "video_encoder.h" #include