Add optional erasure positions to RS decoder

This commit is contained in:
Carles Fernandez 2021-03-26 12:40:35 +01:00
parent 4c51037b14
commit c67ac1247f
No known key found for this signature in database
GPG Key ID: 4C583C52B0C3877D
3 changed files with 60 additions and 40 deletions

View File

@ -540,26 +540,24 @@ void ReedSolomon::encode_rs_8(const uint8_t* data, uint8_t* parity) const
}
int ReedSolomon::decode(std::vector<uint8_t>& data_to_decode) const
int ReedSolomon::decode(std::vector<uint8_t>& data_to_decode, const std::vector<int>& erasure_positions) const
{
if (data_to_decode.size() != d_symbols_per_block)
{
std::cerr << "Reed Solomon error: bad input length\n";
return -1;
}
std::vector<uint8_t> decoded_data(d_data_in_block, 0);
int result = decode_rs_8(data_to_decode.data(), nullptr, 0, d_pad);
int result = decode_rs_8(data_to_decode.data(), erasure_positions.data(), erasure_positions.size(), d_pad);
return result;
}
int ReedSolomon::decode_rs_8(uint8_t* data, int* eras_pos, int no_eras, int pad) const
int ReedSolomon::decode_rs_8(uint8_t* data, const int* eras_pos, int no_eras, int pad) const
{
if (pad < 0 || pad > 222)
{
return -1;
}
int retval;
int deg_lambda;
int el;
int deg_omega;
@ -621,16 +619,7 @@ int ReedSolomon::decode_rs_8(uint8_t* data, int* eras_pos, int no_eras, int pad)
{
// if syndrome is zero, data[] is a codeword and there are no
// errors to correct. So return data[] unmodified
count = 0;
if (eras_pos != nullptr)
{
for (i = 0; i < count; i++)
{
eras_pos[i] = loc[i];
}
}
retval = count;
return retval;
return 0;
}
memset(&lambda[1], 0, d_nroots * sizeof(lambda[0]));
@ -761,16 +750,7 @@ int ReedSolomon::decode_rs_8(uint8_t* data, int* eras_pos, int no_eras, int pad)
{
// deg(lambda) unequal to number of roots => uncorrectable
// error detected
count = -1;
if (eras_pos != nullptr)
{
for (i = 0; i < count; i++)
{
eras_pos[i] = loc[i];
}
}
retval = count;
return retval;
return -1;
}
// Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
@ -821,14 +801,5 @@ int ReedSolomon::decode_rs_8(uint8_t* data, int* eras_pos, int no_eras, int pad)
}
}
if (eras_pos != nullptr)
{
for (i = 0; i < count; i++)
{
eras_pos[i] = loc[i];
}
}
retval = count;
return retval;
return count;
}

View File

@ -79,11 +79,18 @@ public:
const std::vector<std::vector<uint8_t>>& gen_matrix = std::vector<std::vector<uint8_t>>{});
/*!
* \brief Decode an encoded block. The decoded symbols are at the first
* 255-nroots elements of the input vector. Returns the number of corrected
* errors or -1 if decoding failed.
* \brief Decode an encoded block.
*
* The decoded symbols are at the first 255-nroots elements
* of the input vector.
*
* The second parameter is optional, and contains a vector of erasure
* positions to be passed to the decoding algorithm.
*
* Returns the number of corrected errors or -1 if decoding failed.
*/
int decode(std::vector<uint8_t>& data_to_decode) const;
int decode(std::vector<uint8_t>& data_to_decode,
const std::vector<int>& erasure_positions = std::vector<int>{}) const;
/*!
* \brief Encode data with the generator matrix (for testing purposes)
@ -98,7 +105,7 @@ public:
private:
static const int d_symbols_per_block = 255; // the total number of symbols in a RS block.
int decode_rs_8(uint8_t* data, int* eras_pos, int no_eras, int pad) const;
int decode_rs_8(uint8_t* data, const int* eras_pos, int no_eras, int pad) const;
int mod255(int x) const;
int rs_min(int a, int b) const;

View File

@ -493,3 +493,45 @@ TEST(ReedSolomonTest, Decode111ErrorsCustomConstructor)
std::vector<uint8_t> decoded(encoded_input.begin(), encoded_input.begin() + 32);
EXPECT_TRUE(expected_output == decoded);
}
TEST(ReedSolomonTest, Decode112Errors)
{
std::vector<uint8_t> encoded_input = {
71, 12, 25, 210, 178, 81, 243, 9, 112, 98, 196, 203, 48, 125, 114, 165, 181, 193, 71, 174, 168, 42, 31, 128, 245, 87, 150, 58, 192, 66, 130, 179, 133, 210, 122, 224, 75, 138, 20, 205, 14, 245, 209, 187, 246, 228, 12, 39, 244, 238, 223, 217, 84, 233, 137, 168, 153, 8, 94, 26, 99, 169, 149, 203, 115, 69, 211, 43, 70, 96, 70, 38, 160, 1, 232, 153, 223, 165, 93, 205, 101, 170, 60, 188, 198, 82, 168, 79, 95, 23, 118, 215, 187, 136, 24, 99, 252, 3, 144, 166, 117, 45, 168, 239, 77, 42, 246, 33, 122, 97, 242, 236, 13, 217, 96, 186, 71, 250, 242, 177, 125, 87, 27, 13, 118, 181, 178, 12, 27, 66, 31, 74, 127, 46, 112, 127, 116, 122, 190, 71, 240, 95, 78, 194, 113, 80, 46, 126, 74, 136, 118, 133, 105, 176, 47, 230, 162, 195, 93, 157, 72, 119, 13, 232, 151, 200, 191, 143, 75, 161, 111, 29, 158, 16, 181, 165, 92, 39, 17, 218, 228, 58, 176, 233, 55, 211, 195, 73, 37, 137, 232, 241, 150, 236, 152, 153, 53, 74, 81, 91, 160, 244, 21, 95, 176, 179, 141, 39, 61, 136, 16, 58, 160, 51, 210, 31, 134, 63, 203, 96, 219, 44, 231, 61, 220, 0, 241, 220, 207, 17, 52, 150, 117, 54, 222, 128, 101, 213, 164, 234, 74, 224, 57, 246, 70, 27, 202, 229, 4, 243, 128, 211, 158, 199, 4};
// Introduce 112 errors (this should make the decoder fail):
for (int i = 0; i < 224; i += 2)
{
encoded_input[i] = 0;
}
auto rs = std::make_unique<ReedSolomon>();
int result = rs->decode(encoded_input);
EXPECT_TRUE(result == -1);
}
TEST(ReedSolomonTest, Decode113ErrorsWithErasure)
{
std::vector<uint8_t> expected_output = {71, 12, 25, 210, 178, 81, 243, 9, 112, 98, 196, 203, 48, 125, 114, 165, 181, 193, 71, 174, 168, 42, 31, 128, 245, 87, 150, 58, 192, 66, 130, 179};
std::vector<uint8_t> encoded_input = {
71, 12, 25, 210, 178, 81, 243, 9, 112, 98, 196, 203, 48, 125, 114, 165, 181, 193, 71, 174, 168, 42, 31, 128, 245, 87, 150, 58, 192, 66, 130, 179, 133, 210, 122, 224, 75, 138, 20, 205, 14, 245, 209, 187, 246, 228, 12, 39, 244, 238, 223, 217, 84, 233, 137, 168, 153, 8, 94, 26, 99, 169, 149, 203, 115, 69, 211, 43, 70, 96, 70, 38, 160, 1, 232, 153, 223, 165, 93, 205, 101, 170, 60, 188, 198, 82, 168, 79, 95, 23, 118, 215, 187, 136, 24, 99, 252, 3, 144, 166, 117, 45, 168, 239, 77, 42, 246, 33, 122, 97, 242, 236, 13, 217, 96, 186, 71, 250, 242, 177, 125, 87, 27, 13, 118, 181, 178, 12, 27, 66, 31, 74, 127, 46, 112, 127, 116, 122, 190, 71, 240, 95, 78, 194, 113, 80, 46, 126, 74, 136, 118, 133, 105, 176, 47, 230, 162, 195, 93, 157, 72, 119, 13, 232, 151, 200, 191, 143, 75, 161, 111, 29, 158, 16, 181, 165, 92, 39, 17, 218, 228, 58, 176, 233, 55, 211, 195, 73, 37, 137, 232, 241, 150, 236, 152, 153, 53, 74, 81, 91, 160, 244, 21, 95, 176, 179, 141, 39, 61, 136, 16, 58, 160, 51, 210, 31, 134, 63, 203, 96, 219, 44, 231, 61, 220, 0, 241, 220, 207, 17, 52, 150, 117, 54, 222, 128, 101, 213, 164, 234, 74, 224, 57, 246, 70, 27, 202, 229, 4, 243, 128, 211, 158, 199, 4};
// Introduce 113 errors:
for (int i = 0; i < 226; i += 2)
{
encoded_input[i] = 0;
}
std::vector<int> erasure_positions{2, 4, 16, 18, 22, 54};
auto rs = std::make_unique<ReedSolomon>();
int result = rs->decode(encoded_input, erasure_positions);
EXPECT_TRUE(result == 113);
std::vector<uint8_t> decoded(encoded_input.begin(), encoded_input.begin() + 32);
EXPECT_TRUE(expected_output == decoded);
}