2 * This file has been modified for the cdrkit suite.
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).
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.
13 /* @(#)edc_ecc.c 1.21 03/04/04 Copyright 1998-2002 Heiko Eissfeldt, Joerg Schilling */
16 * Copyright 1998-2002 by Heiko Eissfeldt
17 * Copyright 2002 by Joerg Schilling
19 * This file contains protected intellectual property.
21 * reed-solomon encoder / decoder for compact discs.
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License version 2
27 * as published by the Free Software Foundation.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License along with
35 * this program; see the file COPYING. If not, write to the Free Software
36 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
48 /*#define memmove(dst, src, size) movebytes((src), (dst), (size))*/
49 #define memmove(d, s, n) bcopy ((s), (d), (n))
52 /* these prototypes will become public when the function are implemented */
53 static int do_decode_L2(unsigned char in[(L2_RAW+L2_Q+L2_P)],
54 unsigned char out[L2_RAW]);
56 static int do_decode_L1(unsigned char in[(L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR],
57 unsigned char out[L1_RAW*FRAMES_PER_SECTOR],
58 int delay1, int delay2, int delay3, int scramble);
61 /* ------------- tables generated by gen_encodes --------------*/
63 #include "scramble_table"
65 #define DO4(a) a;a;a;a;
66 #define DO13(a) a;a;a;a;a;a;a;a;a;a;a;a;a;
69 * Scrambles 2352 - 12 = 2340 bytes
71 int scramble_L2(unsigned char *inout);
73 int scramble_L2(unsigned char *inout)
75 #ifndef EDC_SCRAMBLE_NOSWAP
76 unsigned int *f = (unsigned int *)inout;
79 if (!xaligned(inout + 12, sizeof(UInt32_t)-1)) {
81 Uchar *r = inout + 12;
82 const Uchar *s = yellowbook_scrambler;
85 for (i = (L2_RAW + L2_Q + L2_P +16)/sizeof(unsigned char)/4; --i >= 0;) {
90 UInt32_t *r = (UInt32_t *) (inout + 12);
91 const UInt32_t *s = yellowbook_scrambler_uint32;
94 for (i = (L2_RAW + L2_Q + L2_P +16)/sizeof(UInt32_t)/13; --i >= 0;) {
99 #ifndef EDC_SCRAMBLE_NOSWAP
101 /* generate F1 frames */
102 for (i = 2352/sizeof(unsigned int); i; i--) {
103 *f++ = ((*f & 0xff00ff00UL) >> 8) | ((*f & 0x00ff00ffUL) << 8);
110 #include "l2sq_table"
112 static int encode_L2_Q(unsigned char inout[4 + L2_RAW + 4 + 8 + L2_P + L2_Q]);
114 static int encode_L2_Q(unsigned char inout[4 + L2_RAW + 4 + 8 + L2_P + L2_Q])
122 Q = inout + 4 + L2_RAW + 4 + 8 + L2_P;
125 for (j = 0; j < 26; j++) {
126 register unsigned short a;
127 register unsigned short b;
131 for (i = 0; i < 43; i++) {
140 if (dp >= &inout[(4 + L2_RAW + 4 + 8 + L2_P)]) {
141 dp -= (4 + L2_RAW + 4 + 8 + L2_P);
155 static int encode_L2_P(unsigned char inout[4 + L2_RAW + 4 + 8 + L2_P]);
157 static int encode_L2_P(unsigned char inout[4 + L2_RAW + 4 + 8 + L2_P])
164 P = inout + 4 + L2_RAW + 4 + 8;
166 for (j = 0; j < 43; j++) {
167 register unsigned short a;
168 register unsigned short b;
172 for (i = 19; i < 43; i++) {
193 static unsigned char bin2bcd(unsigned p);
195 static unsigned char bin2bcd(unsigned p)
197 return ((p/10)<<4)|(p%10);
200 static int build_address(unsigned char inout[], int sectortype, unsigned address);
203 build_address(unsigned char inout[], int sectortype, unsigned address)
205 inout[12] = bin2bcd(address / (60*75));
206 inout[13] = bin2bcd((address / 75) % 60);
207 inout[14] = bin2bcd(address % 75);
208 if (sectortype == MODE_0)
210 else if (sectortype == MODE_1)
212 else if (sectortype == MODE_2)
214 else if (sectortype == MODE_2_FORM_1)
216 else if (sectortype == MODE_2_FORM_2)
223 #include "crctable.out"
226 * Called with 2064, 2056 or 2332 byte difference - all dividable by 4.
228 unsigned int build_edc(unsigned char inout[], int from, int upto);
230 unsigned int build_edc(unsigned char inout[], int from, int upto)
232 unsigned char *p = inout+from;
233 unsigned int result = 0;
237 while (--upto >= 0) {
238 result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8);
239 result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8);
240 result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8);
241 result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8);
246 /* Layer 2 Product code en/decoder */
247 int do_encode_L2(unsigned char inout[(12 + 4 + L2_RAW+4+8+L2_Q+L2_P)],
248 int sectortype, unsigned address);
251 do_encode_L2(unsigned char inout[(12 + 4 + L2_RAW+4+8+L2_Q+L2_P)],
252 int sectortype, unsigned address)
256 /* SYNCPATTERN "\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" */
257 #define SYNCPATTERN "\000\377\377\377\377\377\377\377\377\377\377"
259 /* supply initial sync pattern */
260 memcpy(inout, SYNCPATTERN, sizeof(SYNCPATTERN));
262 if (sectortype == MODE_0) {
263 memset(inout + sizeof(SYNCPATTERN), 0, 4 + L2_RAW + 12 + L2_P + L2_Q);
264 build_address(inout, sectortype, address);
268 switch (sectortype) {
271 build_address(inout, sectortype, address);
272 result = build_edc(inout, 0, 16+2048-1);
273 inout[2064+0] = result >> 0L;
274 inout[2064+1] = result >> 8L;
275 inout[2064+2] = result >> 16L;
276 inout[2064+3] = result >> 24L;
277 memset(inout+2064+4, 0, 8);
278 encode_L2_P(inout+12);
279 encode_L2_Q(inout+12);
282 build_address(inout, sectortype, address);
285 result = build_edc(inout, 16, 16+8+2048-1);
286 inout[2072+0] = result >> 0L;
287 inout[2072+1] = result >> 8L;
288 inout[2072+2] = result >> 16L;
289 inout[2072+3] = result >> 24L;
291 /* clear header for P/Q parity calculation */
296 encode_L2_P(inout+12);
297 encode_L2_Q(inout+12);
298 build_address(inout, sectortype, address);
301 build_address(inout, sectortype, address);
302 result = build_edc(inout, 16, 16+8+2324-1);
303 inout[2348+0] = result >> 0L;
304 inout[2348+1] = result >> 8L;
305 inout[2348+2] = result >> 16L;
306 inout[2348+3] = result >> 24L;
316 /*--------------------------------------------------------------------------*/
317 #include "encoder_tables"
319 static int encode_L1_Q(unsigned char inout[L1_RAW + L1_Q]);
321 static int encode_L1_Q(unsigned char inout[L1_RAW + L1_Q])
326 memmove(inout+L1_RAW/2+L1_Q, inout+L1_RAW/2, L1_RAW/2);
327 Q = inout + L1_RAW/2;
330 for (i = 0; i < L1_RAW + L1_Q; i++) {
333 if (i == L1_RAW/2) i += L1_Q;
336 unsigned char base = rs_l12_log[data];
338 Q[0] ^= rs_l12_alog[(base+AQ[0][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
339 Q[1] ^= rs_l12_alog[(base+AQ[1][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
340 Q[2] ^= rs_l12_alog[(base+AQ[2][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
341 Q[3] ^= rs_l12_alog[(base+AQ[3][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
347 static int encode_L1_P(unsigned char inout[L1_RAW + L1_Q + L1_P]);
349 static int encode_L1_P(unsigned char inout[L1_RAW + L1_Q + L1_P])
354 P = inout + L1_RAW + L1_Q;
357 for (i = 0; i < L2_RAW + L2_Q + L2_P; i++) {
362 unsigned char base = rs_l12_log[data];
364 P[0] ^= rs_l12_alog[(base+AP[0][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
365 P[1] ^= rs_l12_alog[(base+AP[1][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
366 P[2] ^= rs_l12_alog[(base+AP[2][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
367 P[3] ^= rs_l12_alog[(base+AP[3][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
373 static int decode_L1_Q(unsigned char inout[L1_RAW + L1_Q]);
375 static int decode_L1_Q(unsigned char inout[L1_RAW + L1_Q])
380 static int decode_L1_P(unsigned char in[L1_RAW + L1_Q + L1_P]);
382 static int decode_L1_P(unsigned char in[L1_RAW + L1_Q + L1_P])
387 int decode_L2_Q(unsigned char inout[4 + L2_RAW + 12 + L2_Q]);
389 int decode_L2_Q(unsigned char inout[4 + L2_RAW + 12 + L2_Q])
394 int decode_L2_P(unsigned char inout[4 + L2_RAW + 12 + L2_Q + L2_P]);
396 int decode_L2_P(unsigned char inout[4 + L2_RAW + 12 + L2_Q + L2_P])
401 static int encode_LSUB_Q(unsigned char inout[LSUB_RAW + LSUB_Q]);
403 static int encode_LSUB_Q(unsigned char inout[LSUB_RAW + LSUB_Q])
408 memmove(inout+LSUB_QRAW+LSUB_Q, inout+LSUB_QRAW, LSUB_RAW-LSUB_QRAW);
409 Q = inout + LSUB_QRAW;
411 memset(Q, 0, LSUB_Q);
413 for (i = 0; i < LSUB_QRAW; i++) {
416 data = inout[i] & 0x3f;
418 unsigned char base = rs_sub_rw_log[data];
420 Q[0] ^= rs_sub_rw_alog[(base+SQ[0][i]) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
421 Q[1] ^= rs_sub_rw_alog[(base+SQ[1][i]) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
428 static int encode_LSUB_P(unsigned char inout[LSUB_RAW + LSUB_Q + LSUB_P]);
430 static int encode_LSUB_P(unsigned char inout[LSUB_RAW + LSUB_Q + LSUB_P])
435 P = inout + LSUB_RAW + LSUB_Q;
437 memset(P, 0, LSUB_P);
438 for (i = 0; i < LSUB_RAW + LSUB_Q; i++) {
441 data = inout[i] & 0x3f;
443 unsigned char base = rs_sub_rw_log[data];
445 P[0] ^= rs_sub_rw_alog[(base+SP[0][i]) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
446 P[1] ^= rs_sub_rw_alog[(base+SP[1][i]) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
447 P[2] ^= rs_sub_rw_alog[(base+SP[2][i]) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
448 P[3] ^= rs_sub_rw_alog[(base+SP[3][i]) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
454 int decode_LSUB_Q(unsigned char inout[LSUB_QRAW + LSUB_Q]);
456 int decode_LSUB_Q(unsigned char inout[LSUB_QRAW + LSUB_Q])
458 unsigned char Q[LSUB_Q];
461 memset(Q, 0, LSUB_Q);
462 for (i = LSUB_QRAW + LSUB_Q -1; i>=0; i--) {
465 data = inout[LSUB_QRAW + LSUB_Q -1 -i] & 0x3f;
467 unsigned char base = rs_sub_rw_log[data];
469 Q[0] ^= rs_sub_rw_alog[(base+0*i) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
470 Q[1] ^= rs_sub_rw_alog[(base+1*i) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
473 return (Q[0] != 0 || Q[1] != 0);
476 int decode_LSUB_P(unsigned char inout[LSUB_RAW + LSUB_Q + LSUB_P]);
478 int decode_LSUB_P(unsigned char inout[LSUB_RAW + LSUB_Q + LSUB_P])
480 unsigned char P[LSUB_P];
483 memset(P, 0, LSUB_P);
484 for (i = LSUB_RAW + LSUB_Q + LSUB_P-1; i>=0; i--) {
487 data = inout[LSUB_RAW + LSUB_Q + LSUB_P -1 -i] & 0x3f;
489 unsigned char base = rs_sub_rw_log[data];
491 P[0] ^= rs_sub_rw_alog[(base+0*i) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
492 P[1] ^= rs_sub_rw_alog[(base+1*i) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
493 P[2] ^= rs_sub_rw_alog[(base+2*i) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
494 P[3] ^= rs_sub_rw_alog[(base+3*i) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
497 return (P[0] != 0 || P[1] != 0 || P[2] != 0 || P[3] != 0);
500 /* Layer 1 CIRC en/decoder */
501 #define MAX_L1_DEL1 2
502 static unsigned char l1_delay_line1[MAX_L1_DEL1][L1_RAW];
503 #define MAX_L1_DEL2 108
504 static unsigned char l1_delay_line2[MAX_L1_DEL2][L1_RAW+L1_Q];
505 #define MAX_L1_DEL3 1
506 static unsigned char l1_delay_line3[MAX_L1_DEL3][L1_RAW+L1_Q+L1_P];
507 static unsigned l1_del_index;
509 int do_encode_L1(unsigned char in[L1_RAW*FRAMES_PER_SECTOR],
510 unsigned char out[(L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR],
511 int delay1, int delay2, int delay3, int permute);
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)
519 for (i = 0; i < FRAMES_PER_SECTOR; i++) {
524 memcpy(out, in, L1_RAW);
527 /* shift through delay line 1 */
528 for (j = 0; j < L1_RAW; j++) {
529 if (((j/4) % MAX_L1_DEL1) == 0) {
530 t = l1_delay_line1[l1_del_index % (MAX_L1_DEL1)][j];
531 l1_delay_line1[l1_del_index % (MAX_L1_DEL1)][j] = out[j];
539 t = out[2]; out[2] = out[8]; out[8] = out[10]; out[10] = out[18];
540 out[18] = out[6]; out [6] = t;
541 t = out[3]; out[3] = out[9]; out[9] = out[11]; out[11] = out[19];
542 out[19] = out[7]; out [7] = t;
543 t = out[4]; out[4] = out[16]; out[16] = out[20]; out[20] = out[14];
544 out[14] = out[12]; out [12] = t;
545 t = out[5]; out[5] = out[17]; out[17] = out[21]; out[21] = out[15];
546 out[15] = out[13]; out [13] = t;
553 /* shift through delay line 2 */
554 for (j = 0; j < L1_RAW+L1_Q; j++) {
556 t = l1_delay_line2[(l1_del_index) % MAX_L1_DEL2][j];
557 l1_delay_line2[(l1_del_index + j*4) % MAX_L1_DEL2][j] = out[j];
567 /* shift through delay line 3 */
568 for (j = 0; j < L1_RAW+L1_Q+L1_P; j++) {
569 if (((j) & MAX_L1_DEL3) == 0) {
570 t = l1_delay_line3[0][j];
571 l1_delay_line3[0][j] = out[j];
577 /* invert Q and P parity */
578 for (j = 0; j < L1_Q; j++)
579 out[j+12] = ~out[j+12];
580 for (j = 0; j < L1_P; j++)
581 out[j+28] = ~out[j+28];
584 out += L1_RAW+L1_Q+L1_P;
591 int do_decode_L1(unsigned char in[(L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR],
592 unsigned char out[L1_RAW*FRAMES_PER_SECTOR],
593 int delay1, int delay2, int delay3, int permute);
595 static /* XXX should be non static XXX*/
597 int do_decode_L1(unsigned char in[(L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR],
598 unsigned char out[L1_RAW*FRAMES_PER_SECTOR],
599 int delay1, int delay2, int delay3, int permute)
603 for (i = 0; i < FRAMES_PER_SECTOR; i++) {
608 /* shift through delay line 3 */
609 for (j = 0; j < L1_RAW+L1_Q+L1_P; j++) {
610 if (((j) & MAX_L1_DEL3) != 0) {
611 t = l1_delay_line3[0][j];
612 l1_delay_line3[0][j] = in[j];
618 /* invert Q and P parity */
619 for (j = 0; j < L1_Q; j++)
620 in[j+12] = ~in[j+12];
621 for (j = 0; j < L1_P; j++)
622 in[j+28] = ~in[j+28];
628 /* shift through delay line 2 */
629 for (j = 0; j < L1_RAW+L1_Q; j++) {
630 if (j != L1_RAW+L1_Q-1) {
631 t = l1_delay_line2[(l1_del_index) % MAX_L1_DEL2][j];
632 l1_delay_line2[(l1_del_index + (MAX_L1_DEL2 - j*4)) % MAX_L1_DEL2][j] = in[j];
643 t = in[2]; in[2] = in[6]; in[6] = in[18]; in[18] = in[10];
644 in[10] = in[8]; in [8] = t;
645 t = in[3]; in[3] = in[7]; in[7] = in[19]; in[19] = in[11];
646 in[11] = in[9]; in [9] = t;
647 t = in[4]; in[4] = in[12]; in[12] = in[14]; in[14] = in[20];
648 in[20] = in[16]; in [16] = t;
649 t = in[5]; in[5] = in[13]; in[13] = in[15]; in[15] = in[21];
650 in[21] = in[17]; in [17] = t;
654 /* shift through delay line 1 */
655 for (j = 0; j < L1_RAW; j++) {
656 if (((j/4) % MAX_L1_DEL1) != 0) {
657 t = l1_delay_line1[l1_del_index % (MAX_L1_DEL1)][j];
658 l1_delay_line1[l1_del_index % (MAX_L1_DEL1)][j] = in[j];
665 memcpy(out, in, (L1_RAW));
668 in += L1_RAW+L1_Q+L1_P;
674 static int do_decode_L2(unsigned char in[(L2_RAW+L2_Q+L2_P)],
675 unsigned char out[L2_RAW]);
677 static int do_decode_L2(unsigned char in[(L2_RAW+L2_Q+L2_P)],
678 unsigned char out[L2_RAW])
685 #define MAX_SUB_DEL 8
686 static unsigned char sub_delay_line[MAX_SUB_DEL][LSUB_RAW+LSUB_Q+LSUB_P];
687 static unsigned sub_del_index;
689 /* R-W Subchannel en/decoder */
691 int do_encode_sub(unsigned char in[LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME],
692 unsigned char out[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME],
693 int delay1, int permute);
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)
701 if (in == out) return -1;
703 for (i = 0; i < PACKETS_PER_SUBCHANNELFRAME; i++) {
707 memcpy(out, in, (LSUB_RAW));
717 t = out[1]; out[1] = out[18]; out[18] = t;
718 t = out[2]; out[2] = out[ 5]; out[ 5] = t;
719 t = out[3]; out[3] = out[23]; out[23] = t;
723 /* shift through delay_line */
724 for (j = 0; j < LSUB_RAW+LSUB_Q+LSUB_P; j++) {
725 if ((j % MAX_SUB_DEL) != 0) {
726 t = sub_delay_line[(sub_del_index) % MAX_SUB_DEL][j];
727 sub_delay_line[(sub_del_index + j) % MAX_SUB_DEL][j] = out[j];
733 out += LSUB_RAW+LSUB_Q+LSUB_P;
741 unsigned char in[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME],
742 unsigned char out[LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME],
743 int delay1, int permute);
746 do_decode_sub(unsigned char in[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME],
747 unsigned char out[LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME],
748 int delay1, int permute)
752 if (in == out) return -1;
754 for (i = 0; i < PACKETS_PER_SUBCHANNELFRAME; i++) {
759 /* shift through delay_line */
760 for (j = 0; j < LSUB_RAW+LSUB_Q+LSUB_P; j++) {
761 if ((j % MAX_SUB_DEL) != MAX_SUB_DEL-1) {
762 t = sub_delay_line[(sub_del_index) % MAX_SUB_DEL][j];
763 sub_delay_line[(sub_del_index + (MAX_SUB_DEL - j)) % MAX_SUB_DEL][j] = in[j];
771 t = in[1]; in[1] = in[18]; in[18] = t;
772 t = in[2]; in[2] = in[ 5]; in[ 5] = t;
773 t = in[3]; in[3] = in[23]; in[23] = t;
782 memcpy(out, in, LSUB_QRAW);
783 memcpy(out+LSUB_QRAW, in+LSUB_QRAW+LSUB_Q, LSUB_RAW-LSUB_QRAW);
786 in += LSUB_RAW+LSUB_Q+LSUB_P;
792 static int sectortype = MODE_0;
794 int get_sector_type(void);
796 int get_sector_type()
801 int set_sector_type(int st);
803 int set_sector_type(int st)
819 /* ------------- --------------*/
826 static const unsigned sect_size[8][2] = {
829 /* Layer 1 decode/encode */
830 { (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR, L1_RAW*FRAMES_PER_SECTOR},
831 /* Layer 2 decode/encode */
832 { 16+L2_RAW+12+L2_Q+L2_P, L2_RAW},
833 /* Layer 1 and 2 decode/encode */
834 { (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR, L1_RAW*FRAMES_PER_SECTOR},
835 /* Subchannel decode/encode */
836 { (LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME,
837 LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME},
838 /* Layer 1 and subchannel decode/encode */
839 { (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR +
840 (LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME,
841 LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME +
842 L1_RAW*FRAMES_PER_SECTOR},
843 /* Layer 2 and subchannel decode/encode */
845 (LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME,
846 LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME +
848 /* Layer 1, 2 and subchannel decode/encode */
849 { (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR +
850 (LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME,
851 LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME +
852 L1_RAW*FRAMES_PER_SECTOR},
855 int main(int argc, char *argv[])
861 unsigned address = 0;
862 unsigned char *l1_inbuf;
863 unsigned char *l1_outbuf;
864 unsigned char *l2_inbuf;
865 unsigned char *l2_outbuf;
866 unsigned char *sub_inbuf;
867 unsigned char *sub_outbuf;
868 unsigned char *last_outbuf;
869 unsigned char inbuf[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME +
870 (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR];
871 unsigned char outbuf[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME +
872 (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR];
873 unsigned load_offset;
875 l1_inbuf = l2_inbuf = sub_inbuf = inbuf;
876 l1_outbuf = l2_outbuf = sub_outbuf = last_outbuf = outbuf;
878 infp = fopen("sectors_in", "rb");
879 outfp = fopen("sectors_out", "wb");
884 switch (sectortype) {
899 if (1 != fread(inbuf+load_offset,
900 sect_size[mask][encode], 1, infp)) {
906 switch (sectortype) {
914 fread(inbuf+load_offset+
915 sect_size[mask][encode],
916 2336 - sect_size[mask][encode],
917 1, infp)) { perror(""); break; }
923 fread(inbuf+load_offset+
924 sect_size[mask][encode],
925 2324 - sect_size[mask][encode],
926 1, infp)) { perror(""); break; }
930 fread(inbuf+load_offset+
931 sect_size[mask][encode],
932 2448 - sect_size[mask][encode],
933 1, infp)) { perror(""); break; }
935 /*memset(inbuf+16+2048,0,12+272);*/
938 do_encode_L2(l2_inbuf, MODE_1, address);
939 if (0) scramble_L2(l2_inbuf);
940 last_outbuf = l1_inbuf = l2_inbuf;
941 l1_outbuf = l2_inbuf;
942 sub_inbuf = l2_inbuf + L2_RAW;
943 sub_outbuf = l2_outbuf + 12 + 4+ L2_RAW+4+ 8+ L2_Q+L2_P;
946 do_encode_L1(l1_inbuf, l1_outbuf,1,1,1,1);
947 last_outbuf = l1_outbuf;
948 sub_inbuf = l1_inbuf + L1_RAW*FRAMES_PER_SECTOR;
949 sub_outbuf = l1_outbuf + (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR;
952 do_encode_sub(sub_inbuf, sub_outbuf, 0, 0);
956 do_decode_L1(l1_inbuf, l1_outbuf,1,1,1,1);
957 last_outbuf = l2_inbuf = l1_outbuf;
958 l2_outbuf = l1_inbuf;
959 sub_inbuf = l1_inbuf + (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR;
960 sub_outbuf = l1_outbuf + L1_RAW*FRAMES_PER_SECTOR;
963 do_decode_L2(l2_inbuf, l2_outbuf);
964 last_outbuf = l2_outbuf;
965 sub_inbuf = l2_inbuf + L2_RAW+L2_Q+L2_P;
966 sub_outbuf = l2_outbuf + L2_RAW;
969 do_decode_sub(sub_inbuf, sub_outbuf, 1, 1);
972 if (1 != fwrite(last_outbuf, sect_size[mask][1 - encode], 1, outfp)) {
979 /* flush the data from the delay lines with zeroed sectors, if necessary */