| 1 | /* dvdisaster: Additional error correction for optical media. |
| 2 | * Copyright (C) 2004-2007 Carsten Gnoerlich. |
| 3 | * Project home page: http://www.dvdisaster.com |
| 4 | * Email: [email protected] -or- [email protected] |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA, |
| 19 | * or direct your browser at http://www.gnu.org. |
| 20 | */ |
| 21 | |
| 22 | #include "dvdisaster.h"cxx . . $ `<file>/mednafen/cdrom/dvdisaster.h`/ |
| 23 | |
| 24 | static GaloisTablescxx . . $ GaloisTables# *gtcxx . . $ gt. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; /* for L-EC Reed-Solomon */ |
| 25 | static ReedSolomonTablescxx . . $ ReedSolomonTables# *rtcxx . . $ rt. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 26 | |
| 27 | bool Init_LEC_Correctcxx . . $ Init_LEC_Correct(c9857a73d68e59ba).(void) |
| 28 | { |
| 29 | gtcxx . . $ gt. = CreateGaloisTablescxx . . $ CreateGaloisTables(d23602e4c396cb3b).(0x11d); |
| 30 | rtcxx . . $ rt. = CreateReedSolomonTablescxx . . $ CreateReedSolomonTables(3ca6ffdd841c0c54).(gtcxx . . $ gt., 0, 1, 10); |
| 31 | |
| 32 | return(1); |
| 33 | } |
| 34 | |
| 35 | void Kill_LEC_Correctcxx . . $ Kill_LEC_Correct(49f6e7a06ebc5aa8).(void) |
| 36 | { |
| 37 | FreeGaloisTablescxx . . $ FreeGaloisTables(96bf5e7dfcec2c6b).(gtcxx . . $ gt.); |
| 38 | FreeReedSolomonTablescxx . . $ FreeReedSolomonTables(3061629492dcb198).(rtcxx . . $ rt.); |
| 39 | } |
| 40 | |
| 41 | /*** |
| 42 | *** CD level CRC calculation |
| 43 | ***/ |
| 44 | |
| 45 | /* |
| 46 | * Test raw sector against its 32bit CRC. |
| 47 | * Returns TRUE if frame is good. |
| 48 | */ |
| 49 | |
| 50 | int CheckEDCcxx . . $ CheckEDC(71bb053df918a28d).(const unsigned char *cd_frame, bool xa_mode) |
| 51 | { |
| 52 | unsigned int expected_crc, real_crc; |
| 53 | unsigned int crc_base = xa_mode ? 2072 : 2064; |
| 54 | |
| 55 | expected_crc = cd_frame[crc_base + 0] << 0; |
| 56 | expected_crc |= cd_frame[crc_base + 1] << 8; |
| 57 | expected_crc |= cd_frame[crc_base + 2] << 16; |
| 58 | expected_crc |= cd_frame[crc_base + 3] << 24; |
| 59 | |
| 60 | if(xa_mode) |
| 61 | real_crc = EDCCrc32cxx . . $ EDCCrc32(f38ae389a4b85ca).(cd_frame+16, 2056); |
| 62 | else |
| 63 | real_crc = EDCCrc32cxx . . $ EDCCrc32(f38ae389a4b85ca).(cd_frame, 2064); |
| 64 | |
| 65 | if(expected_crc == real_crc) |
| 66 | return(1); |
| 67 | else |
| 68 | { |
| 69 | //printf("Bad EDC CRC: Calculated: %08x, Recorded: %08x\n", real_crc, expected_crc); |
| 70 | return(0); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /*** |
| 75 | *** A very simple L-EC error correction. |
| 76 | *** |
| 77 | * Perform just one pass over the Q and P vectors to see if everything |
| 78 | * is okay respectively correct minor errors. This is pretty much the |
| 79 | * same stuff the drive is supposed to do in the final L-EC stage. |
| 80 | */ |
| 81 | |
| 82 | static int simple_leccxx . . $ simple_lec(b2e27cdb9bef2230).(unsigned char *frame) |
| 83 | { |
| 84 | unsigned char byte_state[2352]; |
| 85 | unsigned char p_vector[P_VECTOR_SIZEcxx . . $ `mednafen/cdrom/dvdisaster.h:118:9`!]; |
| 86 | unsigned char q_vector[Q_VECTOR_SIZEcxx . . $ `mednafen/cdrom/dvdisaster.h:121:9`!]; |
| 87 | unsigned char p_state[P_VECTOR_SIZEcxx . . $ `mednafen/cdrom/dvdisaster.h:118:9`!]; |
| 88 | int erasures[Q_VECTOR_SIZEcxx . . $ `mednafen/cdrom/dvdisaster.h:121:9`!], erasure_count; |
| 89 | int ignore[2]; |
| 90 | int p_failures, q_failures; |
| 91 | int p_corrected, q_corrected; |
| 92 | int p,q; |
| 93 | |
| 94 | /* Setup */ |
| 95 | |
| 96 | memset(byte_state, 0, 2352); |
| 97 | |
| 98 | p_failures = q_failures = 0; |
| 99 | p_corrected = q_corrected = 0; |
| 100 | |
| 101 | /* Perform Q-Parity error correction */ |
| 102 | |
| 103 | for(q=0; q<N_Q_VECTORScxx . . $ `mednafen/cdrom/dvdisaster.h:120:9`!; q++) |
| 104 | { int err; |
| 105 | |
| 106 | /* We have no erasure information for Q vectors */ |
| 107 | |
| 108 | GetQVectorcxx . . $ GetQVector(233e998648c71b4e).(frame, q_vector, q); |
| 109 | err = DecodePQcxx . . $ DecodePQ(2977444feebd832d).(rtcxx . . $ rt., q_vector, Q_PADDINGcxx . . $ `mednafen/cdrom/dvdisaster.h:124:9`!, ignore, 0); |
| 110 | |
| 111 | /* See what we've got */ |
| 112 | |
| 113 | if(err < 0) /* Uncorrectable. Mark bytes are erasure. */ |
| 114 | { q_failures++; |
| 115 | FillQVectorcxx . . $ FillQVector(ec9eb39fc993c7e5).(byte_state, 1, q); |
| 116 | } |
| 117 | else /* Correctable */ |
| 118 | { if(err == 1 || err == 2) /* Store back corrected vector */ |
| 119 | { SetQVectorcxx . . $ SetQVector(233e998648c71b4e).(frame, q_vector, q); |
| 120 | q_corrected++; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /* Perform P-Parity error correction */ |
| 126 | |
| 127 | for(p=0; p<N_P_VECTORScxx . . $ `mednafen/cdrom/dvdisaster.h:117:9`!; p++) |
| 128 | { int err,i; |
| 129 | |
| 130 | /* Try error correction without erasure information */ |
| 131 | |
| 132 | GetPVectorcxx . . $ GetPVector(233e998648c71b4e).(frame, p_vector, p); |
| 133 | err = DecodePQcxx . . $ DecodePQ(2977444feebd832d).(rtcxx . . $ rt., p_vector, P_PADDINGcxx . . $ `mednafen/cdrom/dvdisaster.h:123:9`!, ignore, 0); |
| 134 | |
| 135 | /* If unsuccessful, try again using erasures. |
| 136 | Erasure information is uncertain, so try this last. */ |
| 137 | |
| 138 | if(err < 0 || err > 2) |
| 139 | { GetPVectorcxx . . $ GetPVector(233e998648c71b4e).(byte_state, p_state, p); |
| 140 | erasure_count = 0; |
| 141 | |
| 142 | for(i=0; i<P_VECTOR_SIZEcxx . . $ `mednafen/cdrom/dvdisaster.h:118:9`!; i++) |
| 143 | if(p_state[i]) |
| 144 | erasures[erasure_count++] = i; |
| 145 | |
| 146 | if(erasure_count > 0 && erasure_count <= 2) |
| 147 | { GetPVectorcxx . . $ GetPVector(233e998648c71b4e).(frame, p_vector, p); |
| 148 | err = DecodePQcxx . . $ DecodePQ(2977444feebd832d).(rtcxx . . $ rt., p_vector, P_PADDINGcxx . . $ `mednafen/cdrom/dvdisaster.h:123:9`!, erasures, erasure_count); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /* See what we've got */ |
| 153 | |
| 154 | if(err < 0) /* Uncorrectable. */ |
| 155 | { p_failures++; |
| 156 | } |
| 157 | else /* Correctable. */ |
| 158 | { if(err == 1 || err == 2) /* Store back corrected vector */ |
| 159 | { SetPVectorcxx . . $ SetPVector(233e998648c71b4e).(frame, p_vector, p); |
| 160 | p_corrected++; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /* Sum up */ |
| 166 | |
| 167 | if(q_failures || p_failures || q_corrected || p_corrected) |
| 168 | { |
| 169 | return 1; |
| 170 | } |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | /*** |
| 176 | *** Validate CD raw sector |
| 177 | ***/ |
| 178 | |
| 179 | int ValidateRawSectorcxx . . $ ValidateRawSector(b17e68b5352f9945).(unsigned char *frame, bool xaMode) |
| 180 | { |
| 181 | int lec_did_sth = FALSEcxx . . $ `mednafen/mednafen-types.h:109:9`!; |
| 182 | |
| 183 | /* Do simple L-EC. |
| 184 | It seems that drives stop their internal L-EC as soon as the |
| 185 | EDC is okay, so we may see uncorrected errors in the parity bytes. |
| 186 | Since we are also interested in the user data only and doing the |
| 187 | L-EC is expensive, we skip our L-EC as well when the EDC is fine. */ |
| 188 | |
| 189 | if(!CheckEDCcxx . . $ CheckEDC(71bb053df918a28d).(frame, xaMode)) |
| 190 | { |
| 191 | lec_did_sth = simple_leccxx . . $ simple_lec(b2e27cdb9bef2230).(frame); |
| 192 | } |
| 193 | /* Test internal sector checksum again */ |
| 194 | |
| 195 | if(!CheckEDCcxx . . $ CheckEDC(71bb053df918a28d).(frame, xaMode)) |
| 196 | { |
| 197 | /* EDC failure in RAW sector */ |
| 198 | return FALSEcxx . . $ `mednafen/mednafen-types.h:109:9`!; |
| 199 | } |
| 200 | |
| 201 | return TRUEcxx . . $ `mednafen/mednafen-types.h:113:9`!; |
| 202 | } |
| 203 |