1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/gdrom/edc_ecc.c Sun Mar 02 11:38:08 2008 +0000
1.5 + * Note: This file has been extracted from crkit 1.1.6 and modified to work within
1.6 + * lxdream, but is otherwise unchanged.
1.9 + * This file has been modified for the cdrkit suite.
1.11 + * The behaviour and appearence of the program code below can differ to a major
1.12 + * extent from the version distributed by the original author(s).
1.14 + * For details, see Changelog file distributed with the cdrkit package. If you
1.15 + * received this file from another source then ask the distributing person for
1.16 + * a log of modifications.
1.20 +/* @(#)edc_ecc.c 1.21 03/04/04 Copyright 1998-2002 Heiko Eissfeldt, Joerg Schilling */
1.23 + * Copyright 1998-2002 by Heiko Eissfeldt
1.24 + * Copyright 2002 by Joerg Schilling
1.26 + * This file contains protected intellectual property.
1.28 + * reed-solomon encoder / decoder for compact discs.
1.32 + * This program is free software; you can redistribute it and/or modify
1.33 + * it under the terms of the GNU General Public License version 2
1.34 + * as published by the Free Software Foundation.
1.36 + * This program is distributed in the hope that it will be useful,
1.37 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.38 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.39 + * GNU General Public License for more details.
1.41 + * You should have received a copy of the GNU General Public License along with
1.42 + * this program; see the file COPYING. If not, write to the Free Software
1.43 + * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1.46 +#include <stdint.h>
1.48 +#include <string.h>
1.52 +#define xaligned(a, s) ((((uintptr_t)(a)) & (s)) == 0 )
1.54 +/* these prototypes will become public when the function are implemented */
1.55 +static int do_decode_L2(unsigned char in[(L2_RAW+L2_Q+L2_P)],
1.56 + unsigned char out[L2_RAW]);
1.58 +static int do_decode_L1(unsigned char in[(L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR],
1.59 + unsigned char out[L1_RAW*FRAMES_PER_SECTOR],
1.60 + int delay1, int delay2, int delay3, int scramble);
1.63 +/* ------------- tables generated by gen_encodes --------------*/
1.65 +#include "edc_scramble.h"
1.67 +#define DO4(a) a;a;a;a;
1.68 +#define DO13(a) a;a;a;a;a;a;a;a;a;a;a;a;a;
1.71 + * Scrambles 2352 - 12 = 2340 bytes
1.73 +int scramble_L2(unsigned char *inout);
1.75 +int scramble_L2(unsigned char *inout)
1.77 +#ifndef EDC_SCRAMBLE_NOSWAP
1.78 + unsigned int *f = (unsigned int *)inout;
1.81 + if (!xaligned(inout + 12, sizeof(uint32_t)-1)) {
1.83 + uint8_t *r = inout + 12;
1.84 + const uint8_t *s = yellowbook_scrambler;
1.87 + for (i = (L2_RAW + L2_Q + L2_P +16)/sizeof(unsigned char)/4; --i >= 0;) {
1.88 + DO4(*r++ ^= *s++);
1.92 + uint32_t *r = (uint32_t *) (inout + 12);
1.93 + const uint32_t *s = yellowbook_scrambler_uint32;
1.96 + for (i = (L2_RAW + L2_Q + L2_P +16)/sizeof(uint32_t)/13; --i >= 0;) {
1.97 + DO13(*r++ ^= *s++);
1.101 +#ifndef EDC_SCRAMBLE_NOSWAP
1.103 + /* generate F1 frames */
1.104 + for (i = 2352/sizeof(unsigned int); i; i--) {
1.105 + *f++ = ((*f & 0xff00ff00UL) >> 8) | ((*f & 0x00ff00ffUL) << 8);
1.112 +#include "edc_l2sq.h"
1.114 +static int encode_L2_Q(unsigned char inout[4 + L2_RAW + 4 + 8 + L2_P + L2_Q]);
1.116 +static int encode_L2_Q(unsigned char inout[4 + L2_RAW + 4 + 8 + L2_P + L2_Q])
1.118 + unsigned char *dps;
1.119 + unsigned char *dp;
1.120 + unsigned char *Q;
1.124 + Q = inout + 4 + L2_RAW + 4 + 8 + L2_P;
1.127 + for (j = 0; j < 26; j++) {
1.128 + register unsigned short a;
1.129 + register unsigned short b;
1.133 + for (i = 0; i < 43; i++) {
1.136 + a ^= L2sq[i][*dp++];
1.139 + b ^= L2sq[i][*dp];
1.142 + if (dp >= &inout[(4 + L2_RAW + 4 + 8 + L2_P)]) {
1.143 + dp -= (4 + L2_RAW + 4 + 8 + L2_P);
1.157 +static int encode_L2_P(unsigned char inout[4 + L2_RAW + 4 + 8 + L2_P]);
1.159 +static int encode_L2_P(unsigned char inout[4 + L2_RAW + 4 + 8 + L2_P])
1.161 + unsigned char *dp;
1.162 + unsigned char *P;
1.166 + P = inout + 4 + L2_RAW + 4 + 8;
1.168 + for (j = 0; j < 43; j++) {
1.169 + register unsigned short a;
1.170 + register unsigned short b;
1.174 + for (i = 19; i < 43; i++) {
1.177 + a ^= L2sq[i][*dp++];
1.180 + b ^= L2sq[i][*dp];
1.195 +static unsigned char bin2bcd(unsigned p);
1.197 +static unsigned char bin2bcd(unsigned p)
1.199 + return ((p/10)<<4)|(p%10);
1.202 +int cd_build_address(unsigned char inout[], int sectortype, unsigned address)
1.204 + inout[12] = bin2bcd(address / (60*75));
1.205 + inout[13] = bin2bcd((address / 75) % 60);
1.206 + inout[14] = bin2bcd(address % 75);
1.207 + if (sectortype == MODE_0)
1.209 + else if (sectortype == MODE_1)
1.211 + else if (sectortype == MODE_2)
1.213 + else if (sectortype == MODE_2_FORM_1)
1.215 + else if (sectortype == MODE_2_FORM_2)
1.222 +#include "edc_crctable.h"
1.225 + * Called with 2064, 2056 or 2332 byte difference - all dividable by 4.
1.227 +unsigned int build_edc(unsigned char inout[], int from, int upto);
1.229 +unsigned int build_edc(unsigned char inout[], int from, int upto)
1.231 + unsigned char *p = inout+from;
1.232 + unsigned int result = 0;
1.236 + while (--upto >= 0) {
1.237 + result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8);
1.238 + result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8);
1.239 + result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8);
1.240 + result = EDC_crctable[(result ^ *p++) & 0xffL] ^ (result >> 8);
1.245 +/* Layer 2 Product code en/decoder */
1.246 +int do_encode_L2(unsigned char inout[(12 + 4 + L2_RAW+4+8+L2_Q+L2_P)],
1.247 + int sectortype, unsigned address);
1.250 +do_encode_L2(unsigned char inout[(12 + 4 + L2_RAW+4+8+L2_Q+L2_P)],
1.251 + int sectortype, unsigned address)
1.253 + unsigned int result;
1.255 +/* SYNCPATTERN "\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" */
1.256 +#define SYNCPATTERN "\000\377\377\377\377\377\377\377\377\377\377"
1.258 + /* supply initial sync pattern */
1.259 + memcpy(inout, SYNCPATTERN, sizeof(SYNCPATTERN));
1.261 + if (sectortype == MODE_0) {
1.262 + memset(inout + sizeof(SYNCPATTERN), 0, 4 + L2_RAW + 12 + L2_P + L2_Q);
1.263 + cd_build_address(inout, sectortype, address);
1.267 + switch (sectortype) {
1.270 + cd_build_address(inout, sectortype, address);
1.271 + result = build_edc(inout, 0, 16+2048-1);
1.272 + inout[2064+0] = result >> 0L;
1.273 + inout[2064+1] = result >> 8L;
1.274 + inout[2064+2] = result >> 16L;
1.275 + inout[2064+3] = result >> 24L;
1.276 + memset(inout+2064+4, 0, 8);
1.277 + encode_L2_P(inout+12);
1.278 + encode_L2_Q(inout+12);
1.281 + cd_build_address(inout, sectortype, address);
1.283 + case MODE_2_FORM_1:
1.284 + result = build_edc(inout, 16, 16+8+2048-1);
1.285 + inout[2072+0] = result >> 0L;
1.286 + inout[2072+1] = result >> 8L;
1.287 + inout[2072+2] = result >> 16L;
1.288 + inout[2072+3] = result >> 24L;
1.290 + /* clear header for P/Q parity calculation */
1.295 + encode_L2_P(inout+12);
1.296 + encode_L2_Q(inout+12);
1.297 + cd_build_address(inout, sectortype, address);
1.299 + case MODE_2_FORM_2:
1.300 + cd_build_address(inout, sectortype, address);
1.301 + result = build_edc(inout, 16, 16+8+2324-1);
1.302 + inout[2348+0] = result >> 0L;
1.303 + inout[2348+1] = result >> 8L;
1.304 + inout[2348+2] = result >> 16L;
1.305 + inout[2348+3] = result >> 24L;
1.315 +/*--------------------------------------------------------------------------*/
1.316 +#include "edc_encoder.h"
1.318 +static int encode_L1_Q(unsigned char inout[L1_RAW + L1_Q]);
1.320 +static int encode_L1_Q(unsigned char inout[L1_RAW + L1_Q])
1.322 + unsigned char *Q;
1.325 + memmove(inout+L1_RAW/2+L1_Q, inout+L1_RAW/2, L1_RAW/2);
1.326 + Q = inout + L1_RAW/2;
1.328 + memset(Q, 0, L1_Q);
1.329 + for (i = 0; i < L1_RAW + L1_Q; i++) {
1.330 + unsigned char data;
1.332 + if (i == L1_RAW/2) i += L1_Q;
1.335 + unsigned char base = rs_l12_log[data];
1.337 + Q[0] ^= rs_l12_alog[(base+AQ[0][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
1.338 + Q[1] ^= rs_l12_alog[(base+AQ[1][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
1.339 + Q[2] ^= rs_l12_alog[(base+AQ[2][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
1.340 + Q[3] ^= rs_l12_alog[(base+AQ[3][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
1.346 +static int encode_L1_P(unsigned char inout[L1_RAW + L1_Q + L1_P]);
1.348 +static int encode_L1_P(unsigned char inout[L1_RAW + L1_Q + L1_P])
1.350 + unsigned char *P;
1.353 + P = inout + L1_RAW + L1_Q;
1.355 + memset(P, 0, L1_P);
1.356 + for (i = 0; i < L2_RAW + L2_Q + L2_P; i++) {
1.357 + unsigned char data;
1.361 + unsigned char base = rs_l12_log[data];
1.363 + P[0] ^= rs_l12_alog[(base+AP[0][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
1.364 + P[1] ^= rs_l12_alog[(base+AP[1][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
1.365 + P[2] ^= rs_l12_alog[(base+AP[2][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
1.366 + P[3] ^= rs_l12_alog[(base+AP[3][i]) % (unsigned)((1 << RS_L12_BITS)-1)];
1.372 +static int decode_L1_Q(unsigned char inout[L1_RAW + L1_Q]);
1.374 +static int decode_L1_Q(unsigned char inout[L1_RAW + L1_Q])
1.379 +static int decode_L1_P(unsigned char in[L1_RAW + L1_Q + L1_P]);
1.381 +static int decode_L1_P(unsigned char in[L1_RAW + L1_Q + L1_P])
1.386 +int decode_L2_Q(unsigned char inout[4 + L2_RAW + 12 + L2_Q]);
1.388 +int decode_L2_Q(unsigned char inout[4 + L2_RAW + 12 + L2_Q])
1.393 +int decode_L2_P(unsigned char inout[4 + L2_RAW + 12 + L2_Q + L2_P]);
1.395 +int decode_L2_P(unsigned char inout[4 + L2_RAW + 12 + L2_Q + L2_P])
1.400 +static int encode_LSUB_Q(unsigned char inout[LSUB_RAW + LSUB_Q]);
1.402 +static int encode_LSUB_Q(unsigned char inout[LSUB_RAW + LSUB_Q])
1.404 + unsigned char *Q;
1.407 + memmove(inout+LSUB_QRAW+LSUB_Q, inout+LSUB_QRAW, LSUB_RAW-LSUB_QRAW);
1.408 + Q = inout + LSUB_QRAW;
1.410 + memset(Q, 0, LSUB_Q);
1.412 + for (i = 0; i < LSUB_QRAW; i++) {
1.413 + unsigned char data;
1.415 + data = inout[i] & 0x3f;
1.417 + unsigned char base = rs_sub_rw_log[data];
1.419 + Q[0] ^= rs_sub_rw_alog[(base+SQ[0][i]) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
1.420 + Q[1] ^= rs_sub_rw_alog[(base+SQ[1][i]) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
1.427 +static int encode_LSUB_P(unsigned char inout[LSUB_RAW + LSUB_Q + LSUB_P]);
1.429 +static int encode_LSUB_P(unsigned char inout[LSUB_RAW + LSUB_Q + LSUB_P])
1.431 + unsigned char *P;
1.434 + P = inout + LSUB_RAW + LSUB_Q;
1.436 + memset(P, 0, LSUB_P);
1.437 + for (i = 0; i < LSUB_RAW + LSUB_Q; i++) {
1.438 + unsigned char data;
1.440 + data = inout[i] & 0x3f;
1.442 + unsigned char base = rs_sub_rw_log[data];
1.444 + P[0] ^= rs_sub_rw_alog[(base+SP[0][i]) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
1.445 + P[1] ^= rs_sub_rw_alog[(base+SP[1][i]) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
1.446 + P[2] ^= rs_sub_rw_alog[(base+SP[2][i]) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
1.447 + P[3] ^= rs_sub_rw_alog[(base+SP[3][i]) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
1.453 +int decode_LSUB_Q(unsigned char inout[LSUB_QRAW + LSUB_Q]);
1.455 +int decode_LSUB_Q(unsigned char inout[LSUB_QRAW + LSUB_Q])
1.457 + unsigned char Q[LSUB_Q];
1.460 + memset(Q, 0, LSUB_Q);
1.461 + for (i = LSUB_QRAW + LSUB_Q -1; i>=0; i--) {
1.462 + unsigned char data;
1.464 + data = inout[LSUB_QRAW + LSUB_Q -1 -i] & 0x3f;
1.466 + unsigned char base = rs_sub_rw_log[data];
1.468 + Q[0] ^= rs_sub_rw_alog[(base+0*i) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
1.469 + Q[1] ^= rs_sub_rw_alog[(base+1*i) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
1.472 + return (Q[0] != 0 || Q[1] != 0);
1.475 +int decode_LSUB_P(unsigned char inout[LSUB_RAW + LSUB_Q + LSUB_P]);
1.477 +int decode_LSUB_P(unsigned char inout[LSUB_RAW + LSUB_Q + LSUB_P])
1.479 + unsigned char P[LSUB_P];
1.482 + memset(P, 0, LSUB_P);
1.483 + for (i = LSUB_RAW + LSUB_Q + LSUB_P-1; i>=0; i--) {
1.484 + unsigned char data;
1.486 + data = inout[LSUB_RAW + LSUB_Q + LSUB_P -1 -i] & 0x3f;
1.488 + unsigned char base = rs_sub_rw_log[data];
1.490 + P[0] ^= rs_sub_rw_alog[(base+0*i) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
1.491 + P[1] ^= rs_sub_rw_alog[(base+1*i) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
1.492 + P[2] ^= rs_sub_rw_alog[(base+2*i) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
1.493 + P[3] ^= rs_sub_rw_alog[(base+3*i) % (unsigned)((1 << RS_SUB_RW_BITS)-1)];
1.496 + return (P[0] != 0 || P[1] != 0 || P[2] != 0 || P[3] != 0);
1.499 +/* Layer 1 CIRC en/decoder */
1.500 +#define MAX_L1_DEL1 2
1.501 +static unsigned char l1_delay_line1[MAX_L1_DEL1][L1_RAW];
1.502 +#define MAX_L1_DEL2 108
1.503 +static unsigned char l1_delay_line2[MAX_L1_DEL2][L1_RAW+L1_Q];
1.504 +#define MAX_L1_DEL3 1
1.505 +static unsigned char l1_delay_line3[MAX_L1_DEL3][L1_RAW+L1_Q+L1_P];
1.506 +static unsigned l1_del_index;
1.508 +int do_encode_L1(unsigned char in[L1_RAW*FRAMES_PER_SECTOR],
1.509 + unsigned char out[(L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR],
1.510 + int delay1, int delay2, int delay3, int permute);
1.512 +int do_encode_L1(unsigned char in[L1_RAW*FRAMES_PER_SECTOR],
1.513 + unsigned char out[(L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR],
1.514 + int delay1, int delay2, int delay3, int permute)
1.518 + for (i = 0; i < FRAMES_PER_SECTOR; i++) {
1.523 + memcpy(out, in, L1_RAW);
1.526 + /* shift through delay line 1 */
1.527 + for (j = 0; j < L1_RAW; j++) {
1.528 + if (((j/4) % MAX_L1_DEL1) == 0) {
1.529 + t = l1_delay_line1[l1_del_index % (MAX_L1_DEL1)][j];
1.530 + l1_delay_line1[l1_del_index % (MAX_L1_DEL1)][j] = out[j];
1.538 + t = out[2]; out[2] = out[8]; out[8] = out[10]; out[10] = out[18];
1.539 + out[18] = out[6]; out [6] = t;
1.540 + t = out[3]; out[3] = out[9]; out[9] = out[11]; out[11] = out[19];
1.541 + out[19] = out[7]; out [7] = t;
1.542 + t = out[4]; out[4] = out[16]; out[16] = out[20]; out[20] = out[14];
1.543 + out[14] = out[12]; out [12] = t;
1.544 + t = out[5]; out[5] = out[17]; out[17] = out[21]; out[21] = out[15];
1.545 + out[15] = out[13]; out [13] = t;
1.548 + /* build Q parity */
1.549 + encode_L1_Q(out);
1.552 + /* shift through delay line 2 */
1.553 + for (j = 0; j < L1_RAW+L1_Q; j++) {
1.555 + t = l1_delay_line2[(l1_del_index) % MAX_L1_DEL2][j];
1.556 + l1_delay_line2[(l1_del_index + j*4) % MAX_L1_DEL2][j] = out[j];
1.562 + /* build P parity */
1.563 + encode_L1_P(out);
1.566 + /* shift through delay line 3 */
1.567 + for (j = 0; j < L1_RAW+L1_Q+L1_P; j++) {
1.568 + if (((j) & MAX_L1_DEL3) == 0) {
1.569 + t = l1_delay_line3[0][j];
1.570 + l1_delay_line3[0][j] = out[j];
1.576 + /* invert Q and P parity */
1.577 + for (j = 0; j < L1_Q; j++)
1.578 + out[j+12] = ~out[j+12];
1.579 + for (j = 0; j < L1_P; j++)
1.580 + out[j+28] = ~out[j+28];
1.583 + out += L1_RAW+L1_Q+L1_P;
1.590 +int do_decode_L1(unsigned char in[(L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR],
1.591 + unsigned char out[L1_RAW*FRAMES_PER_SECTOR],
1.592 + int delay1, int delay2, int delay3, int permute);
1.594 +static /* XXX should be non static XXX*/
1.596 +int do_decode_L1(unsigned char in[(L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR],
1.597 + unsigned char out[L1_RAW*FRAMES_PER_SECTOR],
1.598 + int delay1, int delay2, int delay3, int permute)
1.602 + for (i = 0; i < FRAMES_PER_SECTOR; i++) {
1.607 + /* shift through delay line 3 */
1.608 + for (j = 0; j < L1_RAW+L1_Q+L1_P; j++) {
1.609 + if (((j) & MAX_L1_DEL3) != 0) {
1.610 + t = l1_delay_line3[0][j];
1.611 + l1_delay_line3[0][j] = in[j];
1.617 + /* invert Q and P parity */
1.618 + for (j = 0; j < L1_Q; j++)
1.619 + in[j+12] = ~in[j+12];
1.620 + for (j = 0; j < L1_P; j++)
1.621 + in[j+28] = ~in[j+28];
1.623 + /* build P parity */
1.627 + /* shift through delay line 2 */
1.628 + for (j = 0; j < L1_RAW+L1_Q; j++) {
1.629 + if (j != L1_RAW+L1_Q-1) {
1.630 + t = l1_delay_line2[(l1_del_index) % MAX_L1_DEL2][j];
1.631 + l1_delay_line2[(l1_del_index + (MAX_L1_DEL2 - j*4)) % MAX_L1_DEL2][j] = in[j];
1.637 + /* build Q parity */
1.642 + t = in[2]; in[2] = in[6]; in[6] = in[18]; in[18] = in[10];
1.643 + in[10] = in[8]; in [8] = t;
1.644 + t = in[3]; in[3] = in[7]; in[7] = in[19]; in[19] = in[11];
1.645 + in[11] = in[9]; in [9] = t;
1.646 + t = in[4]; in[4] = in[12]; in[12] = in[14]; in[14] = in[20];
1.647 + in[20] = in[16]; in [16] = t;
1.648 + t = in[5]; in[5] = in[13]; in[13] = in[15]; in[15] = in[21];
1.649 + in[21] = in[17]; in [17] = t;
1.653 + /* shift through delay line 1 */
1.654 + for (j = 0; j < L1_RAW; j++) {
1.655 + if (((j/4) % MAX_L1_DEL1) != 0) {
1.656 + t = l1_delay_line1[l1_del_index % (MAX_L1_DEL1)][j];
1.657 + l1_delay_line1[l1_del_index % (MAX_L1_DEL1)][j] = in[j];
1.664 + memcpy(out, in, (L1_RAW));
1.667 + in += L1_RAW+L1_Q+L1_P;
1.673 +static int do_decode_L2(unsigned char in[(L2_RAW+L2_Q+L2_P)],
1.674 + unsigned char out[L2_RAW]);
1.676 +static int do_decode_L2(unsigned char in[(L2_RAW+L2_Q+L2_P)],
1.677 + unsigned char out[L2_RAW])
1.684 +#define MAX_SUB_DEL 8
1.685 +static unsigned char sub_delay_line[MAX_SUB_DEL][LSUB_RAW+LSUB_Q+LSUB_P];
1.686 +static unsigned sub_del_index;
1.688 +/* R-W Subchannel en/decoder */
1.690 +int do_encode_sub(unsigned char in[LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME],
1.691 + unsigned char out[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME],
1.692 + int delay1, int permute);
1.694 +int do_encode_sub(unsigned char in[LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME],
1.695 + unsigned char out[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME],
1.696 + int delay1, int permute)
1.700 + if (in == out) return -1;
1.702 + for (i = 0; i < PACKETS_PER_SUBCHANNELFRAME; i++) {
1.706 + memcpy(out, in, (LSUB_RAW));
1.708 + /* build Q parity */
1.709 + encode_LSUB_Q(out);
1.711 + /* build P parity */
1.712 + encode_LSUB_P(out);
1.716 + t = out[1]; out[1] = out[18]; out[18] = t;
1.717 + t = out[2]; out[2] = out[ 5]; out[ 5] = t;
1.718 + t = out[3]; out[3] = out[23]; out[23] = t;
1.722 + /* shift through delay_line */
1.723 + for (j = 0; j < LSUB_RAW+LSUB_Q+LSUB_P; j++) {
1.724 + if ((j % MAX_SUB_DEL) != 0) {
1.725 + t = sub_delay_line[(sub_del_index) % MAX_SUB_DEL][j];
1.726 + sub_delay_line[(sub_del_index + j) % MAX_SUB_DEL][j] = out[j];
1.732 + out += LSUB_RAW+LSUB_Q+LSUB_P;
1.740 + unsigned char in[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME],
1.741 + unsigned char out[LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME],
1.742 + int delay1, int permute);
1.745 +do_decode_sub(unsigned char in[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME],
1.746 + unsigned char out[LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME],
1.747 + int delay1, int permute)
1.751 + if (in == out) return -1;
1.753 + for (i = 0; i < PACKETS_PER_SUBCHANNELFRAME; i++) {
1.758 + /* shift through delay_line */
1.759 + for (j = 0; j < LSUB_RAW+LSUB_Q+LSUB_P; j++) {
1.760 + if ((j % MAX_SUB_DEL) != MAX_SUB_DEL-1) {
1.761 + t = sub_delay_line[(sub_del_index) % MAX_SUB_DEL][j];
1.762 + sub_delay_line[(sub_del_index + (MAX_SUB_DEL - j)) % MAX_SUB_DEL][j] = in[j];
1.770 + t = in[1]; in[1] = in[18]; in[18] = t;
1.771 + t = in[2]; in[2] = in[ 5]; in[ 5] = t;
1.772 + t = in[3]; in[3] = in[23]; in[23] = t;
1.775 + /* build P parity */
1.776 + decode_LSUB_P(in);
1.778 + /* build Q parity */
1.779 + decode_LSUB_Q(in);
1.781 + memcpy(out, in, LSUB_QRAW);
1.782 + memcpy(out+LSUB_QRAW, in+LSUB_QRAW+LSUB_Q, LSUB_RAW-LSUB_QRAW);
1.785 + in += LSUB_RAW+LSUB_Q+LSUB_P;
1.791 +static int sectortype = MODE_0;
1.793 +int get_sector_type(void);
1.795 +int get_sector_type()
1.797 + return (sectortype);
1.800 +int set_sector_type(int st);
1.802 +int set_sector_type(int st)
1.809 + case MODE_2_FORM_1:
1.810 + case MODE_2_FORM_2:
1.818 +/* ------------- --------------*/
1.825 +static const unsigned sect_size[8][2] = {
1.828 +/* Layer 1 decode/encode */
1.829 +{ (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR, L1_RAW*FRAMES_PER_SECTOR},
1.830 +/* Layer 2 decode/encode */
1.831 +{ 16+L2_RAW+12+L2_Q+L2_P, L2_RAW},
1.832 +/* Layer 1 and 2 decode/encode */
1.833 +{ (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR, L1_RAW*FRAMES_PER_SECTOR},
1.834 +/* Subchannel decode/encode */
1.835 +{ (LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME,
1.836 + LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME},
1.837 +/* Layer 1 and subchannel decode/encode */
1.838 +{ (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR +
1.839 + (LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME,
1.840 + LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME +
1.841 + L1_RAW*FRAMES_PER_SECTOR},
1.842 +/* Layer 2 and subchannel decode/encode */
1.843 +{ L2_RAW+L2_Q+L2_P+
1.844 + (LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME,
1.845 + LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME +
1.847 +/* Layer 1, 2 and subchannel decode/encode */
1.848 +{ (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR +
1.849 + (LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME,
1.850 + LSUB_RAW*PACKETS_PER_SUBCHANNELFRAME +
1.851 + L1_RAW*FRAMES_PER_SECTOR},
1.854 +int main(int argc, char *argv[])
1.857 + int mask = DO_L2;
1.860 + unsigned address = 0;
1.861 + unsigned char *l1_inbuf;
1.862 + unsigned char *l1_outbuf;
1.863 + unsigned char *l2_inbuf;
1.864 + unsigned char *l2_outbuf;
1.865 + unsigned char *sub_inbuf;
1.866 + unsigned char *sub_outbuf;
1.867 + unsigned char *last_outbuf;
1.868 + unsigned char inbuf[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME +
1.869 + (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR];
1.870 + unsigned char outbuf[(LSUB_RAW+LSUB_Q+LSUB_P)*PACKETS_PER_SUBCHANNELFRAME +
1.871 + (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR];
1.872 + unsigned load_offset;
1.874 + l1_inbuf = l2_inbuf = sub_inbuf = inbuf;
1.875 + l1_outbuf = l2_outbuf = sub_outbuf = last_outbuf = outbuf;
1.877 + infp = fopen("sectors_in", "rb");
1.878 + outfp = fopen("sectors_out", "wb");
1.880 + sectortype= MODE_1;
1.881 + address = 0 + 75*2;
1.883 + switch (sectortype) {
1.887 + load_offset = 16;
1.889 + case MODE_2_FORM_1:
1.890 + case MODE_2_FORM_2:
1.891 + load_offset = 24;
1.898 + if (1 != fread(inbuf+load_offset,
1.899 + sect_size[mask][encode], 1, infp)) {
1.903 + if (encode == 1) {
1.904 + if (mask & DO_L2) {
1.905 + switch (sectortype) {
1.913 + fread(inbuf+load_offset+
1.914 + sect_size[mask][encode],
1.915 + 2336 - sect_size[mask][encode],
1.916 + 1, infp)) { perror(""); break; }
1.918 + case MODE_2_FORM_1:
1.920 + case MODE_2_FORM_2:
1.922 + fread(inbuf+load_offset+
1.923 + sect_size[mask][encode],
1.924 + 2324 - sect_size[mask][encode],
1.925 + 1, infp)) { perror(""); break; }
1.929 + fread(inbuf+load_offset+
1.930 + sect_size[mask][encode],
1.931 + 2448 - sect_size[mask][encode],
1.932 + 1, infp)) { perror(""); break; }
1.933 + memset(inbuf,0,16);
1.934 + /*memset(inbuf+16+2048,0,12+272);*/
1.937 + do_encode_L2(l2_inbuf, MODE_1, address);
1.938 + if (0) scramble_L2(l2_inbuf);
1.939 + last_outbuf = l1_inbuf = l2_inbuf;
1.940 + l1_outbuf = l2_inbuf;
1.941 + sub_inbuf = l2_inbuf + L2_RAW;
1.942 + sub_outbuf = l2_outbuf + 12 + 4+ L2_RAW+4+ 8+ L2_Q+L2_P;
1.944 + if (mask & DO_L1) {
1.945 + do_encode_L1(l1_inbuf, l1_outbuf,1,1,1,1);
1.946 + last_outbuf = l1_outbuf;
1.947 + sub_inbuf = l1_inbuf + L1_RAW*FRAMES_PER_SECTOR;
1.948 + sub_outbuf = l1_outbuf + (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR;
1.950 + if (mask & DO_SUB) {
1.951 + do_encode_sub(sub_inbuf, sub_outbuf, 0, 0);
1.954 + if (mask & DO_L1) {
1.955 + do_decode_L1(l1_inbuf, l1_outbuf,1,1,1,1);
1.956 + last_outbuf = l2_inbuf = l1_outbuf;
1.957 + l2_outbuf = l1_inbuf;
1.958 + sub_inbuf = l1_inbuf + (L1_RAW+L1_Q+L1_P)*FRAMES_PER_SECTOR;
1.959 + sub_outbuf = l1_outbuf + L1_RAW*FRAMES_PER_SECTOR;
1.961 + if (mask & DO_L2) {
1.962 + do_decode_L2(l2_inbuf, l2_outbuf);
1.963 + last_outbuf = l2_outbuf;
1.964 + sub_inbuf = l2_inbuf + L2_RAW+L2_Q+L2_P;
1.965 + sub_outbuf = l2_outbuf + L2_RAW;
1.967 + if (mask & DO_SUB) {
1.968 + do_decode_sub(sub_inbuf, sub_outbuf, 1, 1);
1.971 + if (1 != fwrite(last_outbuf, sect_size[mask][1 - encode], 1, outfp)) {
1.978 + /* flush the data from the delay lines with zeroed sectors, if necessary */