l-ec.c 11.5 KB
Newer Older
1
/*  dvdisaster: Additional error correction for optical media.
2
 *  Copyright (C) 2004-2015 Carsten Gnoerlich.
3
 *
4 5
 *  Email: carsten@dvdisaster.org  -or-  cgnoerlich@fsfe.org
 *  Project homepage: http://www.dvdisaster.org
6
 *
7 8 9
 *  This file is part of dvdisaster.
 *
 *  dvdisaster is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation, either version 3 of the License, or
12 13
 *  (at your option) any later version.
 *
14
 *  dvdisaster is distributed in the hope that it will be useful,
15 16 17 18 19
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
20
 *  along with dvdisaster. If not, see <http://www.gnu.org/licenses/>.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
 */

#include "dvdisaster.h"

#include "galois-inlines.h"


/***
 *** Mapping between cd frame and parity vectors
 ***/

/*
 * Mapping of frame bytes to P/Q Vectors
 */

int PToByteIndex(int p, int i)
{  return 12 + p + i*86;
}

void ByteIndexToP(int b, int *p, int *i)
{  *p = (b-12)%86;
   *i = (b-12)/86;
}

int QToByteIndex(int q, int i)
{  int offset = 12 + (q & 1);

   if(i == 43) return 2248+q;
   if(i == 44) return 2300+q;

   q&=~1;
   return offset + (q*43 + i*88) % 2236;
}

void ByteIndexToQ(int b, int *q, int *i)
{ int x,y,offset;
 
  if(b >= 2300) 
  {  *i = 44;
     *q = (b-2300);
     return;
  }

  if(b >= 2248) 
  {  *i = 43;
     *q = (b-2248);
     return;
  }

  offset = b&1;
  b  = (b-12)/2;
  x  = b/43;
  y  = (b-(x*43))%26;  
  *i = b-(x*43);
  *q = 2*((x+26-y)%26)+offset;
}

/* 
 * Debugging output
 */

void PrintVector(unsigned char *vector, int len, int n)
{  int i;

   g_printf("%c#%2d =", len == P_VECTOR_SIZE ? 'P' : 'Q', n);

   for(i=0; i<len; i++)
     g_printf(" %2x", vector[i]);

   g_printf("\n");
}


/*
 * There are 86 vectors of P-parity, yielding a RS(26,24) code.
 */

void GetPVector(unsigned char *frame, unsigned char *data, int n)
{  int i;
   int w_idx = n+12;

   for(i=0; i<26; i++, w_idx+=86)
     data[i] = frame[w_idx];
}

void SetPVector(unsigned char *frame, unsigned char *data, int n)
{  int i;
   int w_idx = n+12;

   for(i=0; i<26; i++, w_idx+=86)
     frame[w_idx] = data[i];
}

void FillPVector(unsigned char *frame, unsigned char data, int n)
{  int i;
   int w_idx = n+12;

   for(i=0; i<26; i++, w_idx+=86)
     frame[w_idx] = data;
}

void OrPVector(unsigned char *frame, unsigned char value, int n)
{  int i;
   int w_idx = n+12;

   for(i=0; i<26; i++, w_idx+=86)
       frame[w_idx] |= value;
}

void AndPVector(unsigned char *frame, unsigned char value, int n)
{  int i;
   int w_idx = n+12;

   for(i=0; i<26; i++, w_idx+=86)
       frame[w_idx] &= value;
}

/*
 * There are 52 vectors of Q-parity, yielding a RS(45,43) code.
 */

void GetQVector(unsigned char *frame, unsigned char *data, int n)
{  int offset = 12 + (n & 1);
   int w_idx  = (n&~1) * 43;
   int i;

   for(i=0; i<43; i++, w_idx+=88)
     data[i] = frame[(w_idx % 2236) + offset];

   data[43] = frame[2248 + n];
   data[44] = frame[2300 + n];
}

void SetQVector(unsigned char *frame, unsigned char *data, int n)
{  int offset = 12 + (n & 1);
   int w_idx  = (n&~1) * 43;
   int i;

   for(i=0; i<43; i++, w_idx+=88)
     frame[(w_idx % 2236) + offset] = data[i];

   frame[2248 + n] = data[43];
   frame[2300 + n] = data[44];
}

void FillQVector(unsigned char *frame, unsigned char data, int n)
{  int offset = 12 + (n & 1);
   int w_idx  = (n&~1) * 43;
   int i;

   for(i=0; i<43; i++, w_idx+=88)
     frame[(w_idx % 2236) + offset] = data;

   frame[2248 + n] = data;
   frame[2300 + n] = data;
}

void OrQVector(unsigned char *frame, unsigned char data, int n)
{  int offset = 12 + (n & 1);
   int w_idx  = (n&~1) * 43;
   int i;

   for(i=0; i<43; i++, w_idx+=88)
     frame[(w_idx % 2236) + offset] |= data;

   frame[2248 + n] |= data;
   frame[2300 + n] |= data;
}

void AndQVector(unsigned char *frame, unsigned char data, int n)
{  int offset = 12 + (n & 1);
   int w_idx  = (n&~1) * 43;
   int i;

   for(i=0; i<43; i++, w_idx+=88)
     frame[(w_idx % 2236) + offset] &= data;

   frame[2248 + n] &= data;
   frame[2300 + n] &= data;
}

/***
 *** C2 error counting
 ***/

int CountC2Errors(unsigned char *frame)
{  int i,count = 0;
   frame += 2352;

   for(i=0; i<294; i++, frame++)
   {  if(*frame & 0x01) count++;
      if(*frame & 0x02) count++;
      if(*frame & 0x04) count++;
      if(*frame & 0x08) count++;
      if(*frame & 0x10) count++;
      if(*frame & 0x20) count++;
      if(*frame & 0x40) count++;
      if(*frame & 0x80) count++;
   }

   return count;
}

/***
 *** L-EC error correction for CD raw data sectors
 ***/

/*
 * These could be used from ReedSolomonTables,
 * but hardcoding them is faster.
 */

#define NROOTS 2
#define LEC_FIRST_ROOT 0 //GF_ALPHA0
#define LEC_PRIM_ELEM 1
#define LEC_PRIMTH_ROOT 1

/*
 * Calculate the error syndrome
 */

int DecodePQ(ReedSolomonTables *rt, unsigned char *data, int padding,
	     int *erasure_list, int erasure_count)
{  GaloisTables *gt = rt->gfTables;
   int syndrome[NROOTS];
   int lambda[NROOTS+1];
   int omega[NROOTS+1];
   int b[NROOTS+1];
   int reg[NROOTS+1];
   int root[NROOTS];
   int loc[NROOTS];
   int syn_error;
   int deg_lambda,lambda_roots;
   int deg_omega;
   int shortened_size = GF_FIELDMAX - padding;
   int corrected = 0;
   int i,j,k;
   int r,el;
  
   /*** Form the syndromes: Evaluate data(x) at roots of g(x) */

   for(i=0; i<NROOTS; i++)
     syndrome[i] = data[0];

   for(j=1; j<shortened_size; j++)
     for(i=0; i<NROOTS; i++)
       if(syndrome[i] == 0) 
             syndrome[i] = data[j];
        else syndrome[i] = data[j] ^ gt->alphaTo[mod_fieldmax(gt->indexOf[syndrome[i]] 
							      + (LEC_FIRST_ROOT+i)*LEC_PRIM_ELEM)];

   /*** Convert syndrome to index form, check for nonzero condition. */

   syn_error = 0;
   for(i=0; i<NROOTS; i++)
   {  syn_error |= syndrome[i];
      syndrome[i] = gt->indexOf[syndrome[i]];
   }

   /*** If the syndrome is zero, everything is fine. */

   if(!syn_error)
     return 0;

   /*** Initialize lambda to be the erasure locator polynomial */

   lambda[0] = 1;
   lambda[1] = lambda[2] = 0;

   erasure_list[0] += padding;
   erasure_list[1] += padding;

   if(erasure_count > 2)  /* sanity check */
     erasure_count = 0;

   if(erasure_count > 0)
   {  lambda[1] = gt->alphaTo[mod_fieldmax(LEC_PRIM_ELEM*(GF_FIELDMAX-1-erasure_list[0]))];

      for(i=1; i<erasure_count; i++) 
      {  int u = mod_fieldmax(LEC_PRIM_ELEM*(GF_FIELDMAX-1-erasure_list[i]));

         for(j=i+1; j>0; j--) 
	 {  int tmp = gt->indexOf[lambda[j-1]];
	  
            if(tmp != GF_ALPHA0)
	      lambda[j] ^= gt->alphaTo[mod_fieldmax(u + tmp)];
	}
      }
   }	

   for(i=0; i<NROOTS+1; i++)
     b[i] = gt->indexOf[lambda[i]];
  
   /*** Berlekamp-Massey algorithm to determine error+erasure locator polynomial */

   r = erasure_count;   /* r is the step number */
   el = erasure_count;

   /* Compute discrepancy at the r-th step in poly-form */

   while(++r <= NROOTS) 
   {  int discr_r = 0;

      for(i=0; i<r; i++)
	if((lambda[i] != 0) && (syndrome[r-i-1] != GF_ALPHA0))
	      discr_r ^= gt->alphaTo[mod_fieldmax(gt->indexOf[lambda[i]] + syndrome[r-i-1])];

      discr_r = gt->indexOf[discr_r];

      if(discr_r == GF_ALPHA0) 
      {  /* B(x) = x*B(x) */
	 memmove(b+1, b, NROOTS*sizeof(b[0]));
	 b[0] = GF_ALPHA0;
      } 
      else 
      {  int t[NROOTS+1];

	 /* T(x) = lambda(x) - discr_r*x*b(x) */
	 t[0] = lambda[0];
	 for(i=0; i<NROOTS; i++) 
	 {  if(b[i] != GF_ALPHA0)
	         t[i+1] = lambda[i+1] ^ gt->alphaTo[mod_fieldmax(discr_r + b[i])];
	    else t[i+1] = lambda[i+1];
	 }

	 if(2*el <= r+erasure_count-1) 
	 {  el = r + erasure_count - el;

	    /* B(x) <-- inv(discr_r) * lambda(x) */
	    for(i=0; i<=NROOTS; i++)
	      b[i] = (lambda[i] == 0) ? GF_ALPHA0 
		                      : mod_fieldmax(gt->indexOf[lambda[i]] - discr_r + GF_FIELDMAX);
	 } 
	 else 
	 {  /* 2 lines below: B(x) <-- x*B(x) */
	    memmove(b+1, b, NROOTS*sizeof(b[0]));
	    b[0] = GF_ALPHA0;
	 }

	 memcpy(lambda, t, (NROOTS+1)*sizeof(t[0]));
      }
   }

   /*** Convert lambda to index form and compute deg(lambda(x)) */

   deg_lambda = 0;
   for(i=0; i<NROOTS+1; i++)
   {  lambda[i] = gt->indexOf[lambda[i]];
      if(lambda[i] != GF_ALPHA0)
	deg_lambda = i;
   }

   /*** Find roots of the error+erasure locator polynomial by Chien search */

   memcpy(reg+1, lambda+1, NROOTS*sizeof(reg[0]));
   lambda_roots = 0;		/* Number of roots of lambda(x) */

   for(i=1, k=LEC_PRIMTH_ROOT-1; i<=GF_FIELDMAX; i++, k=mod_fieldmax(k+LEC_PRIMTH_ROOT))
   {  int q=1; /* lambda[0] is always 0 */

      for(j=deg_lambda; j>0; j--)
      {  if(reg[j] != GF_ALPHA0) 
	 {  reg[j] = mod_fieldmax(reg[j] + j);
	    q ^= gt->alphaTo[reg[j]];
	 }
      }

      if(q != 0) continue; /* Not a root */

      /* store root in index-form and the error location number */

      root[lambda_roots] = i;
      loc[lambda_roots] = k;

      /* If we've already found max possible roots, abort the search to save time */

      if(++lambda_roots == deg_lambda) break;
   }

   /* deg(lambda) unequal to number of roots => uncorrectable error detected
      This is not reliable for very small numbers of roots, e.g. nroots = 2 */

   if(deg_lambda != lambda_roots)
   {  return -1;
   } 

   /* Compute err+eras evaluator poly omega(x) = syn(x)*lambda(x) 
      (modulo x**nroots). in index form. Also find deg(omega). */

   deg_omega = deg_lambda-1;

   for(i=0; i<=deg_omega; i++)
   {  int tmp = 0;

      for(j=i; j>=0; j--)
      {  if((syndrome[i - j] != GF_ALPHA0) && (lambda[j] != GF_ALPHA0))
	  tmp ^= gt->alphaTo[mod_fieldmax(syndrome[i - j] + lambda[j])];
      }

      omega[i] = gt->indexOf[tmp];
   }

   /* Compute error values in poly-form. 
      num1 = omega(inv(X(l))), 
      num2 = inv(X(l))**(FIRST_ROOT-1) and 
      den  = lambda_pr(inv(X(l))) all in poly-form. */

   for(j=lambda_roots-1; j>=0; j--)
   {  int num1 = 0;
      int num2;
      int den;
      int location = loc[j];

      for(i=deg_omega; i>=0; i--) 
      {  if(omega[i] != GF_ALPHA0)
	 num1 ^= gt->alphaTo[mod_fieldmax(omega[i] + i * root[j])];
      }

      num2 = gt->alphaTo[mod_fieldmax(root[j] * (LEC_FIRST_ROOT - 1) + GF_FIELDMAX)];
      den = 0;
    
      /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */

      for(i=MIN(deg_lambda, NROOTS-1) & ~1; i>=0; i-=2) 
      {  if(lambda[i+1] != GF_ALPHA0)
	   den ^= gt->alphaTo[mod_fieldmax(lambda[i+1] + i * root[j])];
      }

      /* Apply error to data */

      if(num1 != 0 && location >= padding)
      {  
	 corrected++;
	 data[location-padding] ^= gt->alphaTo[mod_fieldmax(gt->indexOf[num1] + gt->indexOf[num2] 
							    + GF_FIELDMAX - gt->indexOf[den])];

	 /* If no erasures were given, at most one error was corrected.
	    Return its position in erasure_list[0]. */

	 if(!erasure_count)
	    erasure_list[0] = location-padding;
      }
#if 1
      else return -3;
#endif
   }

   /*** Form the syndromes: Evaluate data(x) at roots of g(x) */

   for(i=0; i<NROOTS; i++)
     syndrome[i] = data[0];

   for(j=1; j<shortened_size; j++)
     for(i=0; i<NROOTS; i++)
     {  if(syndrome[i] == 0) 
             syndrome[i] = data[j];
        else syndrome[i] = data[j] ^ gt->alphaTo[mod_fieldmax(gt->indexOf[syndrome[i]] 
							      + (LEC_FIRST_ROOT+i)*LEC_PRIM_ELEM)];
    }

   /*** Convert syndrome to index form, check for nonzero condition. */
#if 1
   for(i=0; i<NROOTS; i++)
     if(syndrome[i])
       return -2;
#endif

   return corrected;
}