/[debburn]/cdrkit/branches/experimental-broken-20061128/libedc/edc_ecc.c
ViewVC logotype

Contents of /cdrkit/branches/experimental-broken-20061128/libedc/edc_ecc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 511 - (show annotations) (download)
Tue Nov 28 13:19:08 2006 UTC (6 years, 5 months ago) by blade
File MIME type: text/plain
File size: 24541 byte(s)
Move the old experimental branch out of the way
1 /*
2 * This file has been modified for the cdrkit suite.
3 *
4 * The behaviour and appearence of the program code below can differ to a major
5 * extent from the version distributed by the original author(s).
6 *
7 * For details, see Changelog file distributed with the cdrkit package. If you
8 * received this file from another source then ask the distributing person for
9 * a log of modifications.
10 *
11 */
12
13 /* @(#)edc_ecc.c 1.21 03/04/04 Copyright 1998-2002 Heiko Eissfeldt, Joerg Schilling */
14 #ifndef lint
15 static char sccsid[] =
16 "@(#)edc_ecc.c 1.21 03/04/04 Copyright 1998-2002 Heiko Eissfeldt, Joerg Schilling";
17 #endif
18
19 /*
20 * Copyright 1998-2002 by Heiko Eissfeldt
21 * Copyright 2002 by Joerg Schilling
22 *
23 * This file contains protected intellectual property.
24 *
25 * reed-solomon encoder / decoder for compact discs.
26 *
27 */
28 /*
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License version 2
31 * as published by the Free Software Foundation.
32 *
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
37 *
38 * You should have received a copy of the GNU General Public License along with
39 * this program; see the file COPYING. If not, write to the Free Software
40 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
41 */
42
43 #include <mconfig.h>
44 #include <stdio.h>
45 #include <align.h>
46 #include <utypes.h>
47 #include <stdxlib.h>
48 #include <strdefs.h>
49 #include "ecc.h"
50
51 #ifndef HAVE_MEMMOVE
52 /*#define memmove(dst, src, size) movebytes((src), (dst), (size))*/
53 #define memmove(d, s, n) bcopy ((s), (d), (n))
54 #endif
55
56 /* these prototypes will become public when the function are implemented */
57 static int do_decode_L2(unsigned char in[(L2_RAW+L2_Q+L2_P)],
58 unsigned char out[L2_RAW]);
59
60 static int do_decode_L1(unsigned char in[(L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR],
61 unsigned char out[L1_RAW*FRAMES_PER_SECTOR],
62 int delay1, int delay2, int delay3, int scramble);
63
64
65 /* ------------- tables generated by gen_encodes --------------*/
66
67 #include "scramble_table"
68
69 #define DO4(a) a;a;a;a;
70 #define DO13(a) a;a;a;a;a;a;a;a;a;a;a;a;a;
71
72 /*
73 * Scrambles 2352 - 12 = 2340 bytes
74 */
75 int scramble_L2(unsigned char *inout);
76
77 int scramble_L2(unsigned char *inout)
78 {
79 #ifndef EDC_SCRAMBLE_NOSWAP
80 unsigned int *f = (unsigned int *)inout;
81 #endif
82
83 if (!xaligned(inout + 12, sizeof(UInt32_t)-1)) {
84
85 Uchar *r = inout + 12;
86 const Uchar *s = yellowbook_scrambler;
87 register int i;
88
89 for (i = (L2_RAW + L2_Q + L2_P +16)/sizeof(unsigned char)/4; --i >= 0;) {
90 DO4(*r++ ^= *s++);
91 }
92
93 } else {
94 UInt32_t *r = (UInt32_t *) (inout + 12);
95 const UInt32_t *s = yellowbook_scrambler_uint32;
96 register int i;
97
98 for (i = (L2_RAW + L2_Q + L2_P +16)/sizeof(UInt32_t)/13; --i >= 0;) {
99 DO13(*r++ ^= *s++);
100 }
101 }
102
103 #ifndef EDC_SCRAMBLE_NOSWAP
104
105 /* generate F1 frames */
106 for (i = 2352/sizeof(unsigned int); i; i--) {
107 *f++ = ((*f & 0xff00ff00UL) >> 8) | ((*f & 0x00ff00ffUL) << 8);
108 }
109 #endif
110
111 return (0);
112 }
113
114 #include "l2sq_table"
115
116 static int encode_L2_Q(unsigned char inout[4 + L2_RAW + 4 + 8 + L2_P + L2_Q]);
117
118 static int encode_L2_Q(unsigned char inout[4 + L2_RAW + 4 + 8 + L2_P + L2_Q])
119 {
120 unsigned char *dps;
121 unsigned char *dp;
122 unsigned char *Q;
123 register int i;
124 int j;
125
126 Q = inout + 4 + L2_RAW + 4 + 8 + L2_P;
127
128 dps = inout;
129 for (j = 0; j < 26; j++) {
130 register unsigned short a;
131 register unsigned short b;
132 a = b = 0;
133
134 dp = dps;
135 for (i = 0; i < 43; i++) {
136
137 /* LSB */
138 a ^= L2sq[i][*dp++];
139
140 /* MSB */
141 b ^= L2sq[i][*dp];
142
143 dp += 2*44-1;
144 if (dp >= &inout[(4 + L2_RAW + 4 + 8 + L2_P)]) {
145 dp -= (4 + L2_RAW + 4 + 8 + L2_P);
146 }
147 }
148 Q[0] = a >> 8;
149 Q[26*2] = a;
150 Q[1] = b >> 8;
151 Q[26*2+1] = b;
152
153 Q += 2;
154 dps += 2*43;
155 }
156 return (0);
157 }
158
159 static int encode_L2_P(unsigned char inout[4 + L2_RAW + 4 + 8 + L2_P]);
160
161 static int encode_L2_P(unsigned char inout[4 + L2_RAW + 4 + 8 + L2_P])
162 {
163 unsigned char *dp;
164 unsigned char *P;
165 register int i;
166 int j;
167
168 P = inout + 4 + L2_RAW + 4 + 8;
169
170 for (j = 0; j < 43; j++) {
171 register unsigned short a;
172 register unsigned short b;
173
174 a = b = 0;
175 dp = inout;
176 for (i = 19; i < 43; i++) {
177
178 /* LSB */
179 a ^= L2sq[i][*dp++];
180
181 /* MSB */
182 b ^= L2sq[i][*dp];
183
184 dp += 2*43 -1;
185 }
186 P[0] = a >> 8;
187 P[43*2] = a;
188 P[1] = b >> 8;
189 P[43*2+1] = b;
190
191 P += 2;
192 inout += 2;
193 }
194 return (0);
195 }
196
197 static unsigned char bin2bcd(unsigned p);
198
199 static unsigned char bin2bcd(unsigned p)
200 {
201 return ((p/10)<<4)|(p%10);
202 }
203
204 static int build_address(unsigned char inout[], int sectortype, unsigned address);
205
206 static int
207 build_address(unsigned char inout[], int sectortype, unsigned address)
208 {
209 inout[12] = bin2bcd(address / (60*75));
210 inout[13] = bin2bcd((address / 75) % 60);
211 inout[14] = bin2bcd(address % 75);
212 if (sectortype == MODE_0)
213 inout[15] = 0;
214 else if (sectortype == MODE_1)
215 inout[15] = 1;
216 else if (sectortype == MODE_2)
217 inout[15] = 2;
218 else if (sectortype == MODE_2_FORM_1)
219 inout[15] = 2;
220 else if (sectortype == MODE_2_FORM_2)
221 inout[15] = 2;
222 else
223 return (-1);
224 return (0);
225 }
226
227 #include "crctable.out"
228
229 /*
230 * Called with 2064, 2056 or 2332 byte difference - all dividable by 4.
231 */
232 unsigned int build_edc(unsigned char inout[], int from, int upto);
233
234 unsigned int build_edc(unsigned char inout[], int from, int upto)
235 {
236 unsigned char *p = inout+from;
237 unsigned int result = 0;
238
239 upto -= from-1;
240 upto /= 4;
241 while (--upto >= 0) {
242 result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8);
243 result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8);
244 result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8);
245 result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8);
246 }
247 return (result);
248 }
249
250 /* Layer 2 Product code en/decoder */
251 int do_encode_L2(unsigned char inout[(12 + 4 + L2_RAW+4+8+L2_Q+L2_P)],
252 int sectortype, unsigned address);
253
254 int
255 do_encode_L2(unsigned char inout[(12 + 4 + L2_RAW+4+8+L2_Q+L2_P)],
256 int sectortype, unsigned address)
257 {
258 unsigned int result;
259
260 /* SYNCPATTERN "\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" */
261 #define SYNCPATTERN "\000\377\377\377\377\377\377\377\377\377\377"
262
263 /* supply initial sync pattern */
264 memcpy(inout, SYNCPATTERN, sizeof(SYNCPATTERN));
265
266 if (sectortype == MODE_0) {
267 memset(inout + sizeof(SYNCPATTERN), 0, 4 + L2_RAW + 12 + L2_P + L2_Q);
268 build_address(inout, sectortype, address);
269 return (0);
270 }
271
272 switch (sectortype) {
273
274 case MODE_1:
275 build_address(inout, sectortype, address);
276 result = build_edc(inout, 0, 16+2048-1);
277 inout[2064+0] = result >> 0L;
278 inout[2064+1] = result >> 8L;
279 inout[2064+2] = result >> 16L;
280 inout[2064+3] = result >> 24L;
281 memset(inout+2064+4, 0, 8);
282 encode_L2_P(inout+12);
283 encode_L2_Q(inout+12);
284 break;
285 case MODE_2:
286 build_address(inout, sectortype, address);
287 break;
288 case MODE_2_FORM_1:
289 result = build_edc(inout, 16, 16+8+2048-1);
290 inout[2072+0] = result >> 0L;
291 inout[2072+1] = result >> 8L;
292 inout[2072+2] = result >> 16L;
293 inout[2072+3] = result >> 24L;
294
295 /* clear header for P/Q parity calculation */
296 inout[12] = 0;
297 inout[12+1] = 0;
298 inout[12+2] = 0;
299 inout[12+3] = 0;
300 encode_L2_P(inout+12);
301 encode_L2_Q(inout+12);
302 build_address(inout, sectortype, address);
303 break;
304 case MODE_2_FORM_2:
305 build_address(inout, sectortype, address);
306 result = build_edc(inout, 16, 16+8+2324-1);
307 inout[2348+0] = result >> 0L;
308 inout[2348+1] = result >> 8L;
309 inout[2348+2] = result >> 16L;
310 inout[2348+3] = result >> 24L;
311 break;
312 default:
313 return (-1);
314 }
315
316 return (0);
317 }
318
319
320 /*--------------------------------------------------------------------------*/
321 #include "encoder_tables"
322
323 static int encode_L1_Q(unsigned char inout[L1_RAW + L1_Q]);
324
325 static int encode_L1_Q(unsigned char inout[L1_RAW + L1_Q])
326 {
327 unsigned char *Q;
328 int i;
329
330 memmove(inout+L1_RAW/2+L1_Q, inout+L1_RAW/2, L1_RAW/2);
331 Q = inout + L1_RAW/2;
332
333 memset(Q, 0, L1_Q);
334 for (i = 0; i < L1_RAW + L1_Q; i++) {
335 unsigned char data;
336
337 if (i == L1_RAW/2) i += L1_Q;
338 data = inout[i];
339 if (data != 0) {
340 unsigned char base = rs_l12_log[data];
341
342 Q[0] ^= rs_l12_alog[(base+AQ[0][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
343 Q[1] ^= rs_l12_alog[(base+AQ[1][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
344 Q[2] ^= rs_l12_alog[(base+AQ[2][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
345 Q[3] ^= rs_l12_alog[(base+AQ[3][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
346 }
347 }
348 return (0);
349 }
350
351 static int encode_L1_P(unsigned char inout[L1_RAW + L1_Q + L1_P]);
352
353 static int encode_L1_P(unsigned char inout[L1_RAW + L1_Q + L1_P])
354 {
355 unsigned char *P;
356 int i;
357
358 P = inout + L1_RAW + L1_Q;
359
360 memset(P, 0, L1_P);
361 for (i = 0; i < L2_RAW + L2_Q + L2_P; i++) {
362 unsigned char data;
363
364 data = inout[i];
365 if (data != 0) {
366 unsigned char base = rs_l12_log[data];
367
368 P[0] ^= rs_l12_alog[(base+AP[0][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
369 P[1] ^= rs_l12_alog[(base+AP[1][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
370 P[2] ^= rs_l12_alog[(base+AP[2][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
371 P[3] ^= rs_l12_alog[(base+AP[3][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
372 }
373 }
374 return (0);
375 }
376
377 static int decode_L1_Q(unsigned char inout[L1_RAW + L1_Q]);
378
379 static int decode_L1_Q(unsigned char inout[L1_RAW + L1_Q])
380 {
381 return (0);
382 }
383
384 static int decode_L1_P(unsigned char in[L1_RAW + L1_Q + L1_P]);
385
386 static int decode_L1_P(unsigned char in[L1_RAW + L1_Q + L1_P])
387 {
388 return (0);
389 }
390
391 int decode_L2_Q(unsigned char inout[4 + L2_RAW + 12 + L2_Q]);
392
393 int decode_L2_Q(unsigned char inout[4 + L2_RAW + 12 + L2_Q])
394 {
395 return (0);
396 }
397
398 int decode_L2_P(unsigned char inout[4 + L2_RAW + 12 + L2_Q + L2_P]);
399
400 int decode_L2_P(unsigned char inout[4 + L2_RAW + 12 + L2_Q + L2_P])
401 {
402 return (0);
403 }
404
405 static int encode_LSUB_Q(unsigned char inout[LSUB_RAW + LSUB_Q]);
406
407 static int encode_LSUB_Q(unsigned char inout[LSUB_RAW + LSUB_Q])
408 {
409 unsigned char *Q;
410 int i;
411
412 memmove(inout+LSUB_QRAW+LSUB_Q, inout+LSUB_QRAW, LSUB_RAW-LSUB_QRAW);
413 Q = inout + LSUB_QRAW;
414
415 memset(Q, 0, LSUB_Q);
416
417 for (i = 0; i < LSUB_QRAW; i++) {
418 unsigned char data;
419
420 data = inout[i] & 0x3f;
421 if (data != 0) {
422 unsigned char base = rs_sub_rw_log[data];
423
424 Q[0] ^= rs_sub_rw_alog[(base+SQ[0][i]) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
425 Q[1] ^= rs_sub_rw_alog[(base+SQ[1][i]) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
426 }
427 }
428 return (0);
429 }
430
431
432 static int encode_LSUB_P(unsigned char inout[LSUB_RAW + LSUB_Q + LSUB_P]);
433
434 static int encode_LSUB_P(unsigned char inout[LSUB_RAW + LSUB_Q + LSUB_P])
435 {
436 unsigned char *P;
437 int i;
438
439 P = inout + LSUB_RAW + LSUB_Q;
440
441 memset(P, 0, LSUB_P);
442 for (i = 0; i < LSUB_RAW + LSUB_Q; i++) {
443 unsigned char data;
444
445 data = inout[i] & 0x3f;
446 if (data != 0) {
447 unsigned char base = rs_sub_rw_log[data];
448
449 P[0] ^= rs_sub_rw_alog[(base+SP[0][i]) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
450 P[1] ^= rs_sub_rw_alog[(base+SP[1][i]) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
451 P[2] ^= rs_sub_rw_alog[(base+SP[2][i]) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
452 P[3] ^= rs_sub_rw_alog[(base+SP[3][i]) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
453 }
454 }
455 return (0);
456 }
457
458 int decode_LSUB_Q(unsigned char inout[LSUB_QRAW + LSUB_Q]);
459
460 int decode_LSUB_Q(unsigned char inout[LSUB_QRAW + LSUB_Q])
461 {
462 unsigned char Q[LSUB_Q];
463 int i;
464
465 memset(Q, 0, LSUB_Q);
466 for (i = LSUB_QRAW + LSUB_Q -1; i>=0; i--) {
467 unsigned char data;
468
469 data = inout[LSUB_QRAW + LSUB_Q -1 -i] & 0x3f;
470 if (data != 0) {
471 unsigned char base = rs_sub_rw_log[data];
472
473 Q[0] ^= rs_sub_rw_alog[(base+0*i) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
474 Q[1] ^= rs_sub_rw_alog[(base+1*i) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
475 }
476 }
477 return (Q[0] != 0 || Q[1] != 0);
478 }
479
480 int decode_LSUB_P(unsigned char inout[LSUB_RAW + LSUB_Q + LSUB_P]);
481
482 int decode_LSUB_P(unsigned char inout[LSUB_RAW + LSUB_Q + LSUB_P])
483 {
484 unsigned char P[LSUB_P];
485 int i;
486
487 memset(P, 0, LSUB_P);
488 for (i = LSUB_RAW + LSUB_Q + LSUB_P-1; i>=0; i--) {
489 unsigned char data;
490
491 data = inout[LSUB_RAW + LSUB_Q + LSUB_P -1 -i] & 0x3f;
492 if (data != 0) {
493 unsigned char base = rs_sub_rw_log[data];
494
495 P[0] ^= rs_sub_rw_alog[(base+0*i) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
496 P[1] ^= rs_sub_rw_alog[(base+1*i) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
497 P[2] ^= rs_sub_rw_alog[(base+2*i) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
498 P[3] ^= rs_sub_rw_alog[(base+3*i) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
499 }
500 }
501 return (P[0] != 0 || P[1] != 0 || P[2] != 0 || P[3] != 0);
502 }
503
504 /* Layer 1 CIRC en/decoder */
505 #define MAX_L1_DEL1 2
506 static unsigned char l1_delay_line1[MAX_L1_DEL1][L1_RAW];
507 #define MAX_L1_DEL2 108
508 static unsigned char l1_delay_line2[MAX_L1_DEL2][L1_RAW+L1_Q];
509 #define MAX_L1_DEL3 1
510 static unsigned char l1_delay_line3[MAX_L1_DEL3][L1_RAW+L1_Q+L1_P];
511 static unsigned l1_del_index;
512
513 int do_encode_L1(unsigned char in[L1_RAW*FRAMES_PER_SECTOR],
514 unsigned char out[(L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR],
515 int delay1, int delay2, int delay3, int permute);
516
517 int do_encode_L1(unsigned char in[L1_RAW*FRAMES_PER_SECTOR],
518 unsigned char out[(L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR],
519 int delay1, int delay2, int delay3, int permute)
520 {
521 int i;
522
523 for (i = 0; i < FRAMES_PER_SECTOR; i++) {
524 int j;
525 unsigned char t;
526
527 if (in != out)
528 memcpy(out, in, L1_RAW);
529
530 if (delay1) {
531 /* shift through delay line 1 */
532 for (j = 0; j < L1_RAW; j++) {
533 if (((j/4) % MAX_L1_DEL1) == 0) {
534 t = l1_delay_line1[l1_del_index % (MAX_L1_DEL1)][j];
535 l1_delay_line1[l1_del_index % (MAX_L1_DEL1)][j] = out[j];
536 out[j] = t;
537 }
538 }
539 }
540
541 if (permute) {
542 /* permute */
543 t = out[2]; out[2] = out[8]; out[8] = out[10]; out[10] = out[18];
544 out[18] = out[6]; out [6] = t;
545 t = out[3]; out[3] = out[9]; out[9] = out[11]; out[11] = out[19];
546 out[19] = out[7]; out [7] = t;
547 t = out[4]; out[4] = out[16]; out[16] = out[20]; out[20] = out[14];
548 out[14] = out[12]; out [12] = t;
549 t = out[5]; out[5] = out[17]; out[17] = out[21]; out[21] = out[15];
550 out[15] = out[13]; out [13] = t;
551 }
552
553 /* build Q parity */
554 encode_L1_Q(out);
555
556 if (delay2) {
557 /* shift through delay line 2 */
558 for (j = 0; j < L1_RAW+L1_Q; j++) {
559 if (j != 0) {
560 t = l1_delay_line2[(l1_del_index) % MAX_L1_DEL2][j];
561 l1_delay_line2[(l1_del_index + j*4) % MAX_L1_DEL2][j] = out[j];
562 out[j] = t;
563 }
564 }
565 }
566
567 /* build P parity */
568 encode_L1_P(out);
569
570 if (delay3) {
571 /* shift through delay line 3 */
572 for (j = 0; j < L1_RAW+L1_Q+L1_P; j++) {
573 if (((j) & MAX_L1_DEL3) == 0) {
574 t = l1_delay_line3[0][j];
575 l1_delay_line3[0][j] = out[j];
576 out[j] = t;
577 }
578 }
579 }
580
581 /* invert Q and P parity */
582 for (j = 0; j < L1_Q; j++)
583 out[j+12] = ~out[j+12];
584 for (j = 0; j < L1_P; j++)
585 out[j+28] = ~out[j+28];
586
587 l1_del_index++;
588 out += L1_RAW+L1_Q+L1_P;
589 in += L1_RAW;
590 }
591 return (0);
592 }
593
594 static
595 int do_decode_L1(unsigned char in[(L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR],
596 unsigned char out[L1_RAW*FRAMES_PER_SECTOR],
597 int delay1, int delay2, int delay3, int permute);
598
599 static /* XXX should be non static XXX*/
600
601 int do_decode_L1(unsigned char in[(L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR],
602 unsigned char out[L1_RAW*FRAMES_PER_SECTOR],
603 int delay1, int delay2, int delay3, int permute)
604 {
605 int i;
606
607 for (i = 0; i < FRAMES_PER_SECTOR; i++) {
608 int j;
609 unsigned char t;
610
611 if (delay3) {
612 /* shift through delay line 3 */
613 for (j = 0; j < L1_RAW+L1_Q+L1_P; j++) {
614 if (((j) & MAX_L1_DEL3) != 0) {
615 t = l1_delay_line3[0][j];
616 l1_delay_line3[0][j] = in[j];
617 in[j] = t;
618 }
619 }
620 }
621
622 /* invert Q and P parity */
623 for (j = 0; j < L1_Q; j++)
624 in[j+12] = ~in[j+12];
625 for (j = 0; j < L1_P; j++)
626 in[j+28] = ~in[j+28];
627
628 /* build P parity */
629 decode_L1_P(in);
630
631 if (delay2) {
632 /* shift through delay line 2 */
633 for (j = 0; j < L1_RAW+L1_Q; j++) {
634 if (j != L1_RAW+L1_Q-1) {
635 t = l1_delay_line2[(l1_del_index) % MAX_L1_DEL2][j];
636 l1_delay_line2[(l1_del_index + (MAX_L1_DEL2 - j*4)) % MAX_L1_DEL2][j] = in[j];
637 in[j] = t;
638 }
639 }
640 }
641
642 /* build Q parity */
643 decode_L1_Q(in);
644
645 if (permute) {
646 /* permute */
647 t = in[2]; in[2] = in[6]; in[6] = in[18]; in[18] = in[10];
648 in[10] = in[8]; in [8] = t;
649 t = in[3]; in[3] = in[7]; in[7] = in[19]; in[19] = in[11];
650 in[11] = in[9]; in [9] = t;
651 t = in[4]; in[4] = in[12]; in[12] = in[14]; in[14] = in[20];
652 in[20] = in[16]; in [16] = t;
653 t = in[5]; in[5] = in[13]; in[13] = in[15]; in[15] = in[21];
654 in[21] = in[17]; in [17] = t;
655 }
656
657 if (delay1) {
658 /* shift through delay line 1 */
659 for (j = 0; j < L1_RAW; j++) {
660 if (((j/4) % MAX_L1_DEL1) != 0) {
661 t = l1_delay_line1[l1_del_index % (MAX_L1_DEL1)][j];
662 l1_delay_line1[l1_del_index % (MAX_L1_DEL1)][j] = in[j];
663 in[j] = t;
664 }
665 }
666 }
667
668 if (in != out)
669 memcpy(out, in, (L1_RAW));
670
671 l1_del_index++;
672 in += L1_RAW+L1_Q+L1_P;
673 out += L1_RAW;
674 }
675 return (0);
676 }
677
678 static int do_decode_L2(unsigned char in[(L2_RAW+L2_Q+L2_P)],
679 unsigned char out[L2_RAW]);
680
681 static int do_decode_L2(unsigned char in[(L2_RAW+L2_Q+L2_P)],
682 unsigned char out[L2_RAW])
683 {
684 return (0);
685 }
686
687
688
689 #define MAX_SUB_DEL 8
690 static unsigned char sub_delay_line[MAX_SUB_DEL][LSUB_RAW+LSUB_Q+LSUB_P];
691 static unsigned sub_del_index;
692
693 /* R-W Subchannel en/decoder */
694
695 int do_encode_sub(unsigned char in[LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME],
696 unsigned char out[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME],
697 int delay1, int permute);
698
699 int do_encode_sub(unsigned char in[LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME],
700 unsigned char out[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME],
701 int delay1, int permute)
702 {
703 int i;
704
705 if (in == out) return -1;
706
707 for (i = 0; i < PACKETS_PER_SUBCHANNELFRAME; i++) {
708 int j;
709 unsigned char t;
710
711 memcpy(out, in, (LSUB_RAW));
712
713 /* build Q parity */
714 encode_LSUB_Q(out);
715
716 /* build P parity */
717 encode_LSUB_P(out);
718
719 if (permute) {
720 /* permute */
721 t = out[1]; out[1] = out[18]; out[18] = t;
722 t = out[2]; out[2] = out[ 5]; out[ 5] = t;
723 t = out[3]; out[3] = out[23]; out[23] = t;
724 }
725
726 if (delay1) {
727 /* shift through delay_line */
728 for (j = 0; j < LSUB_RAW+LSUB_Q+LSUB_P; j++) {
729 if ((j % MAX_SUB_DEL) != 0) {
730 t = sub_delay_line[(sub_del_index) % MAX_SUB_DEL][j];
731 sub_delay_line[(sub_del_index + j) % MAX_SUB_DEL][j] = out[j];
732 out[j] = t;
733 }
734 }
735 }
736 sub_del_index++;
737 out += LSUB_RAW+LSUB_Q+LSUB_P;
738 in += LSUB_RAW;
739 }
740 return (0);
741 }
742
743 int
744 do_decode_sub(
745 unsigned char in[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME],
746 unsigned char out[LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME],
747 int delay1, int permute);
748
749 int
750 do_decode_sub(unsigned char in[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME],
751 unsigned char out[LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME],
752 int delay1, int permute)
753 {
754 int i;
755
756 if (in == out) return -1;
757
758 for (i = 0; i < PACKETS_PER_SUBCHANNELFRAME; i++) {
759 int j;
760 unsigned char t;
761
762 if (delay1) {
763 /* shift through delay_line */
764 for (j = 0; j < LSUB_RAW+LSUB_Q+LSUB_P; j++) {
765 if ((j % MAX_SUB_DEL) != MAX_SUB_DEL-1) {
766 t = sub_delay_line[(sub_del_index) % MAX_SUB_DEL][j];
767 sub_delay_line[(sub_del_index + (MAX_SUB_DEL - j)) % MAX_SUB_DEL][j] = in[j];
768 in[j] = t;
769 }
770 }
771 }
772
773 if (permute) {
774 /* permute */
775 t = in[1]; in[1] = in[18]; in[18] = t;
776 t = in[2]; in[2] = in[ 5]; in[ 5] = t;
777 t = in[3]; in[3] = in[23]; in[23] = t;
778 }
779
780 /* build P parity */
781 decode_LSUB_P(in);
782
783 /* build Q parity */
784 decode_LSUB_Q(in);
785
786 memcpy(out, in, LSUB_QRAW);
787 memcpy(out+LSUB_QRAW, in+LSUB_QRAW+LSUB_Q, LSUB_RAW-LSUB_QRAW);
788
789 sub_del_index++;
790 in += LSUB_RAW+LSUB_Q+LSUB_P;
791 out += LSUB_RAW;
792 }
793 return (0);
794 }
795
796 static int sectortype = MODE_0;
797
798 int get_sector_type(void);
799
800 int get_sector_type()
801 {
802 return (sectortype);
803 }
804
805 int set_sector_type(int st);
806
807 int set_sector_type(int st)
808 {
809 switch(st) {
810
811 case MODE_0:
812 case MODE_1:
813 case MODE_2:
814 case MODE_2_FORM_1:
815 case MODE_2_FORM_2:
816 sectortype = st;
817 return 0;
818 default:
819 return -1;
820 }
821 }
822
823 /* ------------- --------------*/
824 #ifdef MAIN
825
826 #define DO_L1 1
827 #define DO_L2 2
828 #define DO_SUB 4
829
830 static const unsigned sect_size[8][2] = {
831 /* nothing */
832 {0,0},
833 /* Layer 1 decode/encode */
834 { (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR, L1_RAW*FRAMES_PER_SECTOR},
835 /* Layer 2 decode/encode */
836 { 16+L2_RAW+12+L2_Q+L2_P, L2_RAW},
837 /* Layer 1 and 2 decode/encode */
838 { (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR, L1_RAW*FRAMES_PER_SECTOR},
839 /* Subchannel decode/encode */
840 { (LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME,
841 LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME},
842 /* Layer 1 and subchannel decode/encode */
843 { (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR +
844 (LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME,
845 LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME +
846 L1_RAW*FRAMES_PER_SECTOR},
847 /* Layer 2 and subchannel decode/encode */
848 { L2_RAW+L2_Q+L2_P+
849 (LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME,
850 LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME +
851 L2_RAW},
852 /* Layer 1, 2 and subchannel decode/encode */
853 { (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR +
854 (LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME,
855 LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME +
856 L1_RAW*FRAMES_PER_SECTOR},
857 };
858
859 int main(int argc, char *argv[])
860 {
861 int encode = 1;
862 int mask = DO_L2;
863 FILE *infp;
864 FILE *outfp;
865 unsigned address = 0;
866 unsigned char *l1_inbuf;
867 unsigned char *l1_outbuf;
868 unsigned char *l2_inbuf;
869 unsigned char *l2_outbuf;
870 unsigned char *sub_inbuf;
871 unsigned char *sub_outbuf;
872 unsigned char *last_outbuf;
873 unsigned char inbuf[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME +
874 (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR];
875 unsigned char outbuf[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME +
876 (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR];
877 unsigned load_offset;
878
879 l1_inbuf = l2_inbuf = sub_inbuf = inbuf;
880 l1_outbuf = l2_outbuf = sub_outbuf = last_outbuf = outbuf;
881
882 infp = fopen("sectors_in", "rb");
883 outfp = fopen("sectors_out", "wb");
884
885 sectortype= MODE_1;
886 address = 0 + 75*2;
887
888 switch (sectortype) {
889
890 case MODE_1:
891 case MODE_2:
892 load_offset = 16;
893 break;
894 case MODE_2_FORM_1:
895 case MODE_2_FORM_2:
896 load_offset = 24;
897 break;
898 default:
899 load_offset = 0;
900 }
901 while(1) {
902
903 if (1 != fread(inbuf+load_offset,
904 sect_size[mask][encode], 1, infp)) {
905 perror("");
906 break;
907 }
908 if (encode == 1) {
909 if (mask & DO_L2) {
910 switch (sectortype) {
911
912 case MODE_0:
913 break;
914 case MODE_1:
915 break;
916 case MODE_2:
917 if (1 !=
918 fread(inbuf+load_offset+
919 sect_size[mask][encode],
920 2336 - sect_size[mask][encode],
921 1, infp)) { perror(""); break; }
922 break;
923 case MODE_2_FORM_1:
924 break;
925 case MODE_2_FORM_2:
926 if (1 !=
927 fread(inbuf+load_offset+
928 sect_size[mask][encode],
929 2324 - sect_size[mask][encode],
930 1, infp)) { perror(""); break; }
931 break;
932 default:
933 if (1 !=
934 fread(inbuf+load_offset+
935 sect_size[mask][encode],
936 2448 - sect_size[mask][encode],
937 1, infp)) { perror(""); break; }
938 memset(inbuf,0,16);
939 /*memset(inbuf+16+2048,0,12+272);*/
940 break;
941 }
942 do_encode_L2(l2_inbuf, MODE_1, address);
943 if (0) scramble_L2(l2_inbuf);
944 last_outbuf = l1_inbuf = l2_inbuf;
945 l1_outbuf = l2_inbuf;
946 sub_inbuf = l2_inbuf + L2_RAW;
947 sub_outbuf = l2_outbuf + 12 + 4+ L2_RAW+4+ 8+ L2_Q+L2_P;
948 }
949 if (mask & DO_L1) {
950 do_encode_L1(l1_inbuf, l1_outbuf,1,1,1,1);
951 last_outbuf = l1_outbuf;
952 sub_inbuf = l1_inbuf + L1_RAW*FRAMES_PER_SECTOR;
953 sub_outbuf = l1_outbuf + (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR;
954 }
955 if (mask & DO_SUB) {
956 do_encode_sub(sub_inbuf, sub_outbuf, 0, 0);
957 }
958 } else {
959 if (mask & DO_L1) {
960 do_decode_L1(l1_inbuf, l1_outbuf,1,1,1,1);
961 last_outbuf = l2_inbuf = l1_outbuf;
962 l2_outbuf = l1_inbuf;
963 sub_inbuf = l1_inbuf + (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR;
964 sub_outbuf = l1_outbuf + L1_RAW*FRAMES_PER_SECTOR;
965 }
966 if (mask & DO_L2) {
967 do_decode_L2(l2_inbuf, l2_outbuf);
968 last_outbuf = l2_outbuf;
969 sub_inbuf = l2_inbuf + L2_RAW+L2_Q+L2_P;
970 sub_outbuf = l2_outbuf + L2_RAW;
971 }
972 if (mask & DO_SUB) {
973 do_decode_sub(sub_inbuf, sub_outbuf, 1, 1);
974 }
975 }
976 if (1 != fwrite(last_outbuf, sect_size[mask][1 - encode], 1, outfp)) {
977 perror("");
978 break;
979 }
980 address++;
981 }
982 #if 0
983 /* flush the data from the delay lines with zeroed sectors, if necessary */
984 #endif
985 return (0);
986 }
987 #endif

  ViewVC Help
Powered by ViewVC 1.1.5