rspc.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #pragma once
  2. //reed-solomon product code
  3. namespace nall::CD::RSPC {
  4. inline auto encodeP(array_view<uint8_t> input, array_span<uint8_t> parity) -> bool {
  5. ReedSolomon<26,24> s;
  6. uint lo = 0, hi = 43 * 2;
  7. for(uint x : range(43)) {
  8. for(uint w : range(2)) { //16-bit words
  9. uint z = 0;
  10. for(uint y : range(24)) {
  11. s[z++] = input[(y * 43 + x) * 2 + w];
  12. }
  13. s.generateParity();
  14. parity[lo++] = s[z++];
  15. parity[hi++] = s[z++];
  16. }
  17. }
  18. return true;
  19. }
  20. inline auto encodeQ(array_view<uint8_t> input, array_span<uint8_t> parity) -> bool {
  21. ReedSolomon<45,43> s;
  22. uint lo = 0, hi = 26 * 2;
  23. for(uint y : range(26)) {
  24. for(uint w : range(2)) {
  25. uint z = 0;
  26. for(uint x : range(43)) {
  27. s[z++] = input[((x * 44 + y * 43) * 2 + w) % (26 * 43 * 2)];
  28. }
  29. s.generateParity();
  30. parity[lo++] = s[z++];
  31. parity[hi++] = s[z++];
  32. }
  33. }
  34. return true;
  35. }
  36. inline auto encodeMode1(array_span<uint8_t> sector) -> bool {
  37. if(sector.size() != 2352) return false;
  38. if(!encodeP({sector + 12, 2064}, {sector + 2076, 172})) return false;
  39. if(!encodeQ({sector + 12, 2236}, {sector + 2248, 104})) return false;
  40. return true;
  41. }
  42. //
  43. inline auto decodeP(array_span<uint8_t> input, array_span<uint8_t> parity) -> int {
  44. bool success = false;
  45. bool failure = false;
  46. ReedSolomon<26,24> s;
  47. uint lo = 0, hi = 43 * 2;
  48. for(uint x : range(43)) {
  49. for(uint w : range(2)) {
  50. uint z = 0;
  51. for(uint y : range(24)) {
  52. s[z++] = input[(y * 43 + x) * 2 + w];
  53. }
  54. s[z++] = parity[lo++];
  55. s[z++] = parity[hi++];
  56. auto count = s.correctErrors();
  57. if(count < 0) {
  58. failure = true;
  59. }
  60. if(count > 0) {
  61. success = true;
  62. z = 0;
  63. for(uint y : range(24)) {
  64. input[(y * 43 + x) * 2 + w] = s[z++];
  65. }
  66. parity[lo - 1] = s[z++];
  67. parity[hi - 1] = s[z++];
  68. }
  69. }
  70. }
  71. if(!success && !failure) return 0; //no errors remaining
  72. return success ? 1 : -1; //return success even if there are some failures
  73. }
  74. inline auto decodeQ(array_span<uint8_t> input, array_span<uint8_t> parity) -> int {
  75. bool success = false;
  76. bool failure = false;
  77. ReedSolomon<45,43> s;
  78. uint lo = 0, hi = 26 * 2;
  79. for(uint y : range(26)) {
  80. for(uint w : range(2)) {
  81. uint z = 0;
  82. for(uint x : range(43)) {
  83. s[z++] = input[((x * 44 + y * 43) * 2 + w) % (26 * 43 * 2)];
  84. }
  85. s[z++] = parity[lo++];
  86. s[z++] = parity[hi++];
  87. auto count = s.correctErrors();
  88. if(count < 0) {
  89. failure = true;
  90. }
  91. if(count > 0) {
  92. success = true;
  93. z = 0;
  94. for(uint x : range(43)) {
  95. input[((x * 44 + y * 43) * 2 + w) % (26 * 43 * 2)] = s[z++];
  96. }
  97. parity[lo - 1] = s[z++];
  98. parity[hi - 1] = s[z++];
  99. }
  100. }
  101. }
  102. if(!success && !failure) return 0;
  103. return success ? 1 : -1;
  104. }
  105. inline auto decodeMode1(array_span<uint8_t> sector) -> bool {
  106. if(sector.size() != 2352) return false;
  107. //P corrections can allow Q corrections that previously failed to succeed, and vice versa.
  108. //the more iterations, the more chances to correct errors, but the more computationally expensive it is.
  109. //there must be a limit on the amount of retries, or this function may get stuck in an infinite loop.
  110. for(uint attempt : range(4)) {
  111. auto p = decodeP({sector + 12, 2064}, {sector + 2076, 172});
  112. auto q = decodeQ({sector + 12, 2236}, {sector + 2248, 104});
  113. if(p == 0 && q == 0) return true; //no errors remaining
  114. if(p < 0 && q < 0) return false; //no more errors correctable
  115. }
  116. return false; //exhausted all retries with errors remaining
  117. }
  118. }